blob: 0b2907d59a3f7190c7e2094c2e718a3700918bf8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Sistina Software (UK) Limited.
Milan Broz373a3922007-05-09 02:33:02 -07003 * Copyright (C) 2006 Red Hat GmbH
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +010012#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "kcopyd.h"
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +010027#include "dm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*-----------------------------------------------------------------
30 * Each kcopyd client has its own little pool of preallocated
31 * pages for kcopyd io.
32 *---------------------------------------------------------------*/
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +010033struct dm_kcopyd_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 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;
Alasdair G Kergon138728dc2006-03-27 01:17:50 -080040
Milan Broz373a3922007-05-09 02:33:02 -070041 struct dm_io_client *io_client;
42
Alasdair G Kergon138728dc2006-03-27 01:17:50 -080043 wait_queue_head_t destroyq;
44 atomic_t nr_jobs;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +010045
Mikulas Patocka08d87572008-04-24 21:43:46 +010046 mempool_t *job_pool;
47
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +010048 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +010066static void wake(struct dm_kcopyd_client *kc)
67{
68 queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static 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
88static void free_pl(struct page_list *pl)
89{
90 __free_page(pl->page);
91 kfree(pl);
92}
93
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +010094static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100117static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
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 */
134static 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
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100145static int client_alloc_pages(struct dm_kcopyd_client *kc, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
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
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100166static void client_free_pages(struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
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 *---------------------------------------------------------------*/
179struct kcopyd_job {
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100180 struct dm_kcopyd_client *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct list_head list;
182 unsigned long flags;
183
184 /*
185 * Error state of the job.
186 */
187 int read_err;
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700188 unsigned long write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /*
191 * Either READ or WRITE
192 */
193 int rw;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100194 struct dm_io_region source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /*
197 * The destinations for the transfer.
198 */
199 unsigned int num_dests;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100200 struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
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 */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100210 dm_kcopyd_notify_fn fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 void *context;
212
213 /*
214 * These fields are only used if the job has been split
215 * into more manageable parts.
216 */
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100217 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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
Christoph Lametere18b8902006-12-06 20:33:20 -0800225static struct kmem_cache *_job_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int jobs_init(void)
228{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100229 _job_cache = KMEM_CACHE(kcopyd_job, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (!_job_cache)
231 return -ENOMEM;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234}
235
236static void jobs_exit(void)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kmem_cache_destroy(_job_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 _job_cache = NULL;
240}
241
242/*
243 * Functions to push and pop a job onto the head of a given job
244 * list.
245 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100246static struct kcopyd_job *pop(struct list_head *jobs,
247 struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct kcopyd_job *job = NULL;
250 unsigned long flags;
251
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100252 spin_lock_irqsave(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!list_empty(jobs)) {
255 job = list_entry(jobs->next, struct kcopyd_job, list);
256 list_del(&job->list);
257 }
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100258 spin_unlock_irqrestore(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 return job;
261}
262
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100263static void push(struct list_head *jobs, struct kcopyd_job *job)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 unsigned long flags;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100266 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100268 spin_lock_irqsave(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 list_add_tail(&job->list, jobs);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100270 spin_unlock_irqrestore(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
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 */
282static int run_complete_job(struct kcopyd_job *job)
283{
284 void *context = job->context;
285 int read_err = job->read_err;
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700286 unsigned long write_err = job->write_err;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100287 dm_kcopyd_notify_fn fn = job->fn;
288 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800290 kcopyd_put_pages(kc, job->pages);
Mikulas Patocka08d87572008-04-24 21:43:46 +0100291 mempool_free(job, kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 fn(read_err, write_err, context);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800293
294 if (atomic_dec_and_test(&kc->nr_jobs))
295 wake_up(&kc->destroyq);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
298}
299
300static void complete_io(unsigned long error, void *context)
301{
302 struct kcopyd_job *job = (struct kcopyd_job *) context;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100303 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (error) {
306 if (job->rw == WRITE)
Jonathan Brassowce503f52006-06-26 00:27:30 -0700307 job->write_err |= error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 else
309 job->read_err = 1;
310
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100311 if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100312 push(&kc->complete_jobs, job);
313 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return;
315 }
316 }
317
318 if (job->rw == WRITE)
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100319 push(&kc->complete_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 else {
322 job->rw = WRITE;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100323 push(&kc->io_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100326 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/*
330 * Request io on as many buffer heads as we can currently get for
331 * a particular job.
332 */
333static int run_io_job(struct kcopyd_job *job)
334{
335 int r;
Milan Broz373a3922007-05-09 02:33:02 -0700336 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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (job->rw == READ)
Milan Broz373a3922007-05-09 02:33:02 -0700347 r = dm_io(&io_req, 1, &job->source, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 else
Milan Broz373a3922007-05-09 02:33:02 -0700349 r = dm_io(&io_req, job->num_dests, job->dests, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 return r;
352}
353
354static 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 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100363 push(&job->kc->io_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 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 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100378static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
379 int (*fn) (struct kcopyd_job *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct kcopyd_job *job;
382 int r, count = 0;
383
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100384 while ((job = pop(jobs, kc))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 r = fn(job);
387
388 if (r < 0) {
389 /* error this rogue job */
390 if (job->rw == WRITE)
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700391 job->write_err = (unsigned long) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 else
393 job->read_err = 1;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100394 push(&kc->complete_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 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 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100416static void do_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100418 struct dm_kcopyd_client *kc = container_of(work,
419 struct dm_kcopyd_client, kcopyd_work);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
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 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100428 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
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 */
438static void dispatch_job(struct kcopyd_job *job)
439{
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100440 struct dm_kcopyd_client *kc = job->kc;
441 atomic_inc(&kc->nr_jobs);
442 push(&kc->pages_jobs, job);
443 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446#define SUB_JOB_SIZE 128
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700447static void segment_complete(int read_err, unsigned long write_err,
448 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
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
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100455 mutex_lock(&job->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* update the error */
458 if (read_err)
459 job->read_err = 1;
460
461 if (write_err)
Jonathan Brassowce503f52006-06-26 00:27:30 -0700462 job->write_err |= write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * Only dispatch more work if there hasn't been an error.
466 */
467 if ((!job->read_err && !job->write_err) ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100468 test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* 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 }
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100479 mutex_unlock(&job->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (count) {
482 int i;
Mikulas Patocka08d87572008-04-24 21:43:46 +0100483 struct kcopyd_job *sub_job = mempool_alloc(job->kc->job_pool,
484 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
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);
Mikulas Patocka08d87572008-04-24 21:43:46 +0100508 mempool_free(job, job->kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510}
511
512/*
513 * Create some little jobs that will do the move between
514 * them.
515 */
516#define SPLIT_COUNT 8
517static 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
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100526int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct kcopyd_job *job;
531
532 /*
533 * Allocate a new job.
534 */
Mikulas Patocka08d87572008-04-24 21:43:46 +0100535 job = mempool_alloc(kc->job_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
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 {
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100562 mutex_init(&job->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 job->progress = 0;
564 split_job(job);
565 }
566
567 return 0;
568}
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100569EXPORT_SYMBOL(dm_kcopyd_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571/*
572 * Cancels a kcopyd job, eg. someone might be deactivating a
573 * mirror.
574 */
Adrian Bunk0b563062006-01-06 00:20:08 -0800575#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576int kcopyd_cancel(struct kcopyd_job *job, int block)
577{
578 /* FIXME: finish */
579 return -1;
580}
Adrian Bunk0b563062006-01-06 00:20:08 -0800581#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583/*-----------------------------------------------------------------
584 * Unit setup
585 *---------------------------------------------------------------*/
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800586static DEFINE_MUTEX(_client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static LIST_HEAD(_clients);
588
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100589static void client_add(struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800591 mutex_lock(&_client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 list_add(&kc->list, &_clients);
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800593 mutex_unlock(&_client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100596static void client_del(struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800598 mutex_lock(&_client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 list_del(&kc->list);
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800600 mutex_unlock(&_client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800603static DEFINE_MUTEX(kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static int kcopyd_clients = 0;
605
606static int kcopyd_init(void)
607{
608 int r;
609
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800610 mutex_lock(&kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (kcopyd_clients) {
613 /* Already initialized. */
614 kcopyd_clients++;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800615 mutex_unlock(&kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
617 }
618
619 r = jobs_init();
620 if (r) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800621 mutex_unlock(&kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return r;
623 }
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 kcopyd_clients++;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800626 mutex_unlock(&kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628}
629
630static void kcopyd_exit(void)
631{
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800632 mutex_lock(&kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 kcopyd_clients--;
634 if (!kcopyd_clients) {
635 jobs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800637 mutex_unlock(&kcopyd_init_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100640int dm_kcopyd_client_create(unsigned int nr_pages,
641 struct dm_kcopyd_client **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int r = 0;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100644 struct dm_kcopyd_client *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 r = kcopyd_init();
647 if (r)
648 return r;
649
650 kc = kmalloc(sizeof(*kc), GFP_KERNEL);
651 if (!kc) {
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100652 r = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 kcopyd_exit();
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100654 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
657 spin_lock_init(&kc->lock);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100658 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
Mikulas Patocka08d87572008-04-24 21:43:46 +0100663 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
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100671 INIT_WORK(&kc->kcopyd_work, do_work);
672 kc->kcopyd_wq = create_singlethread_workqueue("kcopyd");
673 if (!kc->kcopyd_wq) {
674 r = -ENOMEM;
Mikulas Patocka08d87572008-04-24 21:43:46 +0100675 mempool_destroy(kc->job_pool);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100676 kfree(kc);
677 kcopyd_exit();
678 return r;
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 kc->pages = NULL;
682 kc->nr_pages = kc->nr_free_pages = 0;
683 r = client_alloc_pages(kc, nr_pages);
684 if (r) {
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100685 destroy_workqueue(kc->kcopyd_wq);
Mikulas Patocka08d87572008-04-24 21:43:46 +0100686 mempool_destroy(kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 kfree(kc);
688 kcopyd_exit();
689 return r;
690 }
691
Milan Broz373a3922007-05-09 02:33:02 -0700692 kc->io_client = dm_io_client_create(nr_pages);
693 if (IS_ERR(kc->io_client)) {
694 r = PTR_ERR(kc->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 client_free_pages(kc);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100696 destroy_workqueue(kc->kcopyd_wq);
Mikulas Patocka08d87572008-04-24 21:43:46 +0100697 mempool_destroy(kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 kfree(kc);
699 kcopyd_exit();
700 return r;
701 }
702
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800703 init_waitqueue_head(&kc->destroyq);
704 atomic_set(&kc->nr_jobs, 0);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 client_add(kc);
707 *result = kc;
708 return 0;
709}
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100710EXPORT_SYMBOL(dm_kcopyd_client_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100712void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800714 /* Wait for completion of all jobs submitted by this client. */
715 wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
716
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100717 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);
Milan Broz373a3922007-05-09 02:33:02 -0700721 dm_io_client_destroy(kc->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 client_free_pages(kc);
723 client_del(kc);
Mikulas Patocka08d87572008-04-24 21:43:46 +0100724 mempool_destroy(kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 kfree(kc);
726 kcopyd_exit();
727}
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100728EXPORT_SYMBOL(dm_kcopyd_client_destroy);