This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / md / kcopyd.c
1 /*
2  * Copyright (C) 2002 Sistina Software (UK) Limited.
3  *
4  * This file is released under the GPL.
5  *
6  * Kcopyd provides a simple interface for copying an area of one
7  * block-device to one or more other block-devices, with an asynchronous
8  * completion notification.
9  */
10
11 #include <asm/atomic.h>
12
13 #include <linux/blkdev.h>
14 #include <linux/config.h>
15 #include <linux/fs.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/mempool.h>
19 #include <linux/module.h>
20 #include <linux/pagemap.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 #include <linux/workqueue.h>
24
25 #include "kcopyd.h"
26
27 /* FIXME: this is only needed for the DMERR macros */
28 #include "dm.h"
29
30 static struct workqueue_struct *_kcopyd_wq;
31 static struct work_struct _kcopyd_work;
32
33 static inline void wake(void)
34 {
35         queue_work(_kcopyd_wq, &_kcopyd_work);
36 }
37
38 /*-----------------------------------------------------------------
39  * Each kcopyd client has its own little pool of preallocated
40  * pages for kcopyd io.
41  *---------------------------------------------------------------*/
42 struct kcopyd_client {
43         struct list_head list;
44
45         spinlock_t lock;
46         struct page_list *pages;
47         unsigned int nr_pages;
48         unsigned int nr_free_pages;
49 };
50
51 static struct page_list *alloc_pl(void)
52 {
53         struct page_list *pl;
54
55         pl = kmalloc(sizeof(*pl), GFP_KERNEL);
56         if (!pl)
57                 return NULL;
58
59         pl->page = alloc_page(GFP_KERNEL);
60         if (!pl->page) {
61                 kfree(pl);
62                 return NULL;
63         }
64
65         return pl;
66 }
67
68 static void free_pl(struct page_list *pl)
69 {
70         __free_page(pl->page);
71         kfree(pl);
72 }
73
74 static int kcopyd_get_pages(struct kcopyd_client *kc,
75                             unsigned int nr, struct page_list **pages)
76 {
77         struct page_list *pl;
78
79         spin_lock(&kc->lock);
80         if (kc->nr_free_pages < nr) {
81                 spin_unlock(&kc->lock);
82                 return -ENOMEM;
83         }
84
85         kc->nr_free_pages -= nr;
86         for (*pages = pl = kc->pages; --nr; pl = pl->next)
87                 ;
88
89         kc->pages = pl->next;
90         pl->next = 0;
91
92         spin_unlock(&kc->lock);
93
94         return 0;
95 }
96
97 static void kcopyd_put_pages(struct kcopyd_client *kc, struct page_list *pl)
98 {
99         struct page_list *cursor;
100
101         spin_lock(&kc->lock);
102         for (cursor = pl; cursor->next; cursor = cursor->next)
103                 kc->nr_free_pages++;
104
105         kc->nr_free_pages++;
106         cursor->next = kc->pages;
107         kc->pages = pl;
108         spin_unlock(&kc->lock);
109 }
110
111 /*
112  * These three functions resize the page pool.
113  */
114 static void drop_pages(struct page_list *pl)
115 {
116         struct page_list *next;
117
118         while (pl) {
119                 next = pl->next;
120                 free_pl(pl);
121                 pl = next;
122         }
123 }
124
125 static int client_alloc_pages(struct kcopyd_client *kc, unsigned int nr)
126 {
127         unsigned int i;
128         struct page_list *pl = NULL, *next;
129
130         for (i = 0; i < nr; i++) {
131                 next = alloc_pl();
132                 if (!next) {
133                         if (pl)
134                                 drop_pages(pl);
135                         return -ENOMEM;
136                 }
137                 next->next = pl;
138                 pl = next;
139         }
140
141         kcopyd_put_pages(kc, pl);
142         kc->nr_pages += nr;
143         return 0;
144 }
145
146 static void client_free_pages(struct kcopyd_client *kc)
147 {
148         BUG_ON(kc->nr_free_pages != kc->nr_pages);
149         drop_pages(kc->pages);
150         kc->pages = NULL;
151         kc->nr_free_pages = kc->nr_pages = 0;
152 }
153
154 /*-----------------------------------------------------------------
155  * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
156  * for this reason we use a mempool to prevent the client from
157  * ever having to do io (which could cause a deadlock).
158  *---------------------------------------------------------------*/
159 struct kcopyd_job {
160         struct kcopyd_client *kc;
161         struct list_head list;
162         unsigned long flags;
163
164         /*
165          * Error state of the job.
166          */
167         int read_err;
168         unsigned int write_err;
169
170         /*
171          * Either READ or WRITE
172          */
173         int rw;
174         struct io_region source;
175
176         /*
177          * The destinations for the transfer.
178          */
179         unsigned int num_dests;
180         struct io_region dests[KCOPYD_MAX_REGIONS];
181
182         sector_t offset;
183         unsigned int nr_pages;
184         struct page_list *pages;
185
186         /*
187          * Set this to ensure you are notified when the job has
188          * completed.  'context' is for callback to use.
189          */
190         kcopyd_notify_fn fn;
191         void *context;
192
193         /*
194          * These fields are only used if the job has been split
195          * into more manageable parts.
196          */
197         struct semaphore lock;
198         atomic_t sub_jobs;
199         sector_t progress;
200 };
201
202 /* FIXME: this should scale with the number of pages */
203 #define MIN_JOBS 512
204
205 static kmem_cache_t *_job_cache;
206 static mempool_t *_job_pool;
207
208 /*
209  * We maintain three lists of jobs:
210  *
211  * i)   jobs waiting for pages
212  * ii)  jobs that have pages, and are waiting for the io to be issued.
213  * iii) jobs that have completed.
214  *
215  * All three of these are protected by job_lock.
216  */
217 static spinlock_t _job_lock = SPIN_LOCK_UNLOCKED;
218
219 static LIST_HEAD(_complete_jobs);
220 static LIST_HEAD(_io_jobs);
221 static LIST_HEAD(_pages_jobs);
222
223 static int jobs_init(void)
224 {
225         _job_cache = kmem_cache_create("kcopyd-jobs",
226                                        sizeof(struct kcopyd_job),
227                                        __alignof__(struct kcopyd_job),
228                                        0, NULL, NULL);
229         if (!_job_cache)
230                 return -ENOMEM;
231
232         _job_pool = mempool_create(MIN_JOBS, mempool_alloc_slab,
233                                    mempool_free_slab, _job_cache);
234         if (!_job_pool) {
235                 kmem_cache_destroy(_job_cache);
236                 return -ENOMEM;
237         }
238
239         return 0;
240 }
241
242 static void jobs_exit(void)
243 {
244         BUG_ON(!list_empty(&_complete_jobs));
245         BUG_ON(!list_empty(&_io_jobs));
246         BUG_ON(!list_empty(&_pages_jobs));
247
248         mempool_destroy(_job_pool);
249         kmem_cache_destroy(_job_cache);
250         _job_pool = NULL;
251         _job_cache = NULL;
252 }
253
254 /*
255  * Functions to push and pop a job onto the head of a given job
256  * list.
257  */
258 static inline struct kcopyd_job *pop(struct list_head *jobs)
259 {
260         struct kcopyd_job *job = NULL;
261         unsigned long flags;
262
263         spin_lock_irqsave(&_job_lock, flags);
264
265         if (!list_empty(jobs)) {
266                 job = list_entry(jobs->next, struct kcopyd_job, list);
267                 list_del(&job->list);
268         }
269         spin_unlock_irqrestore(&_job_lock, flags);
270
271         return job;
272 }
273
274 static inline void push(struct list_head *jobs, struct kcopyd_job *job)
275 {
276         unsigned long flags;
277
278         spin_lock_irqsave(&_job_lock, flags);
279         list_add_tail(&job->list, jobs);
280         spin_unlock_irqrestore(&_job_lock, flags);
281 }
282
283 /*
284  * These three functions process 1 item from the corresponding
285  * job list.
286  *
287  * They return:
288  * < 0: error
289  *   0: success
290  * > 0: can't process yet.
291  */
292 static int run_complete_job(struct kcopyd_job *job)
293 {
294         void *context = job->context;
295         int read_err = job->read_err;
296         unsigned int write_err = job->write_err;
297         kcopyd_notify_fn fn = job->fn;
298
299         kcopyd_put_pages(job->kc, job->pages);
300         mempool_free(job, _job_pool);
301         fn(read_err, write_err, context);
302         return 0;
303 }
304
305 static void complete_io(unsigned long error, void *context)
306 {
307         struct kcopyd_job *job = (struct kcopyd_job *) context;
308
309         if (error) {
310                 if (job->rw == WRITE)
311                         job->write_err &= error;
312                 else
313                         job->read_err = 1;
314
315                 if (!test_bit(KCOPYD_IGNORE_ERROR, &job->flags)) {
316                         push(&_complete_jobs, job);
317                         wake();
318                         return;
319                 }
320         }
321
322         if (job->rw == WRITE)
323                 push(&_complete_jobs, job);
324
325         else {
326                 job->rw = WRITE;
327                 push(&_io_jobs, job);
328         }
329
330         wake();
331 }
332
333 /*
334  * Request io on as many buffer heads as we can currently get for
335  * a particular job.
336  */
337 static int run_io_job(struct kcopyd_job *job)
338 {
339         int r;
340
341         if (job->rw == READ)
342                 r = dm_io_async(1, &job->source, job->rw,
343                                 job->pages,
344                                 job->offset, complete_io, job);
345
346         else
347                 r = dm_io_async(job->num_dests, job->dests, job->rw,
348                                 job->pages,
349                                 job->offset, complete_io, job);
350
351         return r;
352 }
353
354 static int run_pages_job(struct kcopyd_job *job)
355 {
356         int r;
357
358         job->nr_pages = dm_div_up(job->dests[0].count + job->offset,
359                                   PAGE_SIZE >> 9);
360         r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
361         if (!r) {
362                 /* this job is ready for io */
363                 push(&_io_jobs, job);
364                 return 0;
365         }
366
367         if (r == -ENOMEM)
368                 /* can't complete now */
369                 return 1;
370
371         return r;
372 }
373
374 /*
375  * Run through a list for as long as possible.  Returns the count
376  * of successful jobs.
377  */
378 static int process_jobs(struct list_head *jobs, int (*fn) (struct kcopyd_job *))
379 {
380         struct kcopyd_job *job;
381         int r, count = 0;
382
383         while ((job = pop(jobs))) {
384
385                 r = fn(job);
386
387                 if (r < 0) {
388                         /* error this rogue job */
389                         if (job->rw == WRITE)
390                                 job->write_err = (unsigned int) -1;
391                         else
392                                 job->read_err = 1;
393                         push(&_complete_jobs, job);
394                         break;
395                 }
396
397                 if (r > 0) {
398                         /*
399                          * We couldn't service this job ATM, so
400                          * push this job back onto the list.
401                          */
402                         push(jobs, job);
403                         break;
404                 }
405
406                 count++;
407         }
408
409         return count;
410 }
411
412 /*
413  * kcopyd does this every time it's woken up.
414  */
415 static void do_work(void *ignored)
416 {
417         /*
418          * The order that these are called is *very* important.
419          * complete jobs can free some pages for pages jobs.
420          * Pages jobs when successful will jump onto the io jobs
421          * list.  io jobs call wake when they complete and it all
422          * starts again.
423          */
424         process_jobs(&_complete_jobs, run_complete_job);
425         process_jobs(&_pages_jobs, run_pages_job);
426         process_jobs(&_io_jobs, run_io_job);
427 }
428
429 /*
430  * If we are copying a small region we just dispatch a single job
431  * to do the copy, otherwise the io has to be split up into many
432  * jobs.
433  */
434 static void dispatch_job(struct kcopyd_job *job)
435 {
436         push(&_pages_jobs, job);
437         wake();
438 }
439
440 #define SUB_JOB_SIZE 128
441 static void segment_complete(int read_err,
442                              unsigned int write_err, void *context)
443 {
444         /* FIXME: tidy this function */
445         sector_t progress = 0;
446         sector_t count = 0;
447         struct kcopyd_job *job = (struct kcopyd_job *) context;
448
449         down(&job->lock);
450
451         /* update the error */
452         if (read_err)
453                 job->read_err = 1;
454
455         if (write_err)
456                 job->write_err &= write_err;
457
458         /*
459          * Only dispatch more work if there hasn't been an error.
460          */
461         if ((!job->read_err && !job->write_err) ||
462             test_bit(KCOPYD_IGNORE_ERROR, &job->flags)) {
463                 /* get the next chunk of work */
464                 progress = job->progress;
465                 count = job->source.count - progress;
466                 if (count) {
467                         if (count > SUB_JOB_SIZE)
468                                 count = SUB_JOB_SIZE;
469
470                         job->progress += count;
471                 }
472         }
473         up(&job->lock);
474
475         if (count) {
476                 int i;
477                 struct kcopyd_job *sub_job = mempool_alloc(_job_pool, GFP_NOIO);
478
479                 *sub_job = *job;
480                 sub_job->source.sector += progress;
481                 sub_job->source.count = count;
482
483                 for (i = 0; i < job->num_dests; i++) {
484                         sub_job->dests[i].sector += progress;
485                         sub_job->dests[i].count = count;
486                 }
487
488                 sub_job->fn = segment_complete;
489                 sub_job->context = job;
490                 dispatch_job(sub_job);
491
492         } else if (atomic_dec_and_test(&job->sub_jobs)) {
493
494                 /*
495                  * To avoid a race we must keep the job around
496                  * until after the notify function has completed.
497                  * Otherwise the client may try and stop the job
498                  * after we've completed.
499                  */
500                 job->fn(read_err, write_err, job->context);
501                 mempool_free(job, _job_pool);
502         }
503 }
504
505 /*
506  * Create some little jobs that will do the move between
507  * them.
508  */
509 #define SPLIT_COUNT 8
510 static void split_job(struct kcopyd_job *job)
511 {
512         int i;
513
514         atomic_set(&job->sub_jobs, SPLIT_COUNT);
515         for (i = 0; i < SPLIT_COUNT; i++)
516                 segment_complete(0, 0u, job);
517 }
518
519 int kcopyd_copy(struct kcopyd_client *kc, struct io_region *from,
520                 unsigned int num_dests, struct io_region *dests,
521                 unsigned int flags, kcopyd_notify_fn fn, void *context)
522 {
523         struct kcopyd_job *job;
524
525         /*
526          * Allocate a new job.
527          */
528         job = mempool_alloc(_job_pool, GFP_NOIO);
529
530         /*
531          * set up for the read.
532          */
533         job->kc = kc;
534         job->flags = flags;
535         job->read_err = 0;
536         job->write_err = 0;
537         job->rw = READ;
538
539         job->source = *from;
540
541         job->num_dests = num_dests;
542         memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
543
544         job->offset = 0;
545         job->nr_pages = 0;
546         job->pages = NULL;
547
548         job->fn = fn;
549         job->context = context;
550
551         if (job->source.count < SUB_JOB_SIZE)
552                 dispatch_job(job);
553
554         else {
555                 init_MUTEX(&job->lock);
556                 job->progress = 0;
557                 split_job(job);
558         }
559
560         return 0;
561 }
562
563 /*
564  * Cancels a kcopyd job, eg. someone might be deactivating a
565  * mirror.
566  */
567 int kcopyd_cancel(struct kcopyd_job *job, int block)
568 {
569         /* FIXME: finish */
570         return -1;
571 }
572
573 /*-----------------------------------------------------------------
574  * Unit setup
575  *---------------------------------------------------------------*/
576 static DECLARE_MUTEX(_client_lock);
577 static LIST_HEAD(_clients);
578
579 static int client_add(struct kcopyd_client *kc)
580 {
581         down(&_client_lock);
582         list_add(&kc->list, &_clients);
583         up(&_client_lock);
584         return 0;
585 }
586
587 static void client_del(struct kcopyd_client *kc)
588 {
589         down(&_client_lock);
590         list_del(&kc->list);
591         up(&_client_lock);
592 }
593
594 static DECLARE_MUTEX(kcopyd_init_lock);
595 static int kcopyd_clients = 0;
596
597 static int kcopyd_init(void)
598 {
599         int r;
600
601         down(&kcopyd_init_lock);
602
603         if (kcopyd_clients) {
604                 /* Already initialized. */
605                 kcopyd_clients++;
606                 up(&kcopyd_init_lock);
607                 return 0;
608         }
609
610         r = jobs_init();
611         if (r) {
612                 up(&kcopyd_init_lock);
613                 return r;
614         }
615
616         _kcopyd_wq = create_singlethread_workqueue("kcopyd");
617         if (!_kcopyd_wq) {
618                 jobs_exit();
619                 up(&kcopyd_init_lock);
620                 return -ENOMEM;
621         }
622
623         kcopyd_clients++;
624         INIT_WORK(&_kcopyd_work, do_work, NULL);
625         up(&kcopyd_init_lock);
626         return 0;
627 }
628
629 static void kcopyd_exit(void)
630 {
631         down(&kcopyd_init_lock);
632         kcopyd_clients--;
633         if (!kcopyd_clients) {
634                 jobs_exit();
635                 destroy_workqueue(_kcopyd_wq);
636                 _kcopyd_wq = NULL;
637         }
638         up(&kcopyd_init_lock);
639 }
640
641 int kcopyd_client_create(unsigned int nr_pages, struct kcopyd_client **result)
642 {
643         int r = 0;
644         struct kcopyd_client *kc;
645
646         r = kcopyd_init();
647         if (r)
648                 return r;
649
650         kc = kmalloc(sizeof(*kc), GFP_KERNEL);
651         if (!kc) {
652                 kcopyd_exit();
653                 return -ENOMEM;
654         }
655
656         kc->lock = SPIN_LOCK_UNLOCKED;
657         kc->pages = NULL;
658         kc->nr_pages = kc->nr_free_pages = 0;
659         r = client_alloc_pages(kc, nr_pages);
660         if (r) {
661                 kfree(kc);
662                 kcopyd_exit();
663                 return r;
664         }
665
666         r = dm_io_get(nr_pages);
667         if (r) {
668                 client_free_pages(kc);
669                 kfree(kc);
670                 kcopyd_exit();
671                 return r;
672         }
673
674         r = client_add(kc);
675         if (r) {
676                 dm_io_put(nr_pages);
677                 client_free_pages(kc);
678                 kfree(kc);
679                 kcopyd_exit();
680                 return r;
681         }
682
683         *result = kc;
684         return 0;
685 }
686
687 void kcopyd_client_destroy(struct kcopyd_client *kc)
688 {
689         dm_io_put(kc->nr_pages);
690         client_free_pages(kc);
691         client_del(kc);
692         kfree(kc);
693         kcopyd_exit();
694 }
695
696 EXPORT_SYMBOL(kcopyd_client_create);
697 EXPORT_SYMBOL(kcopyd_client_destroy);
698 EXPORT_SYMBOL(kcopyd_copy);
699 EXPORT_SYMBOL(kcopyd_cancel);