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