Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2002 Sistina Software (UK) Limited. | |
373a392b | 3 | * Copyright (C) 2006 Red Hat GmbH |
1da177e4 LT |
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 | ||
eb69aca5 | 12 | #include <linux/types.h> |
1da177e4 | 13 | #include <asm/atomic.h> |
1da177e4 | 14 | #include <linux/blkdev.h> |
1da177e4 LT |
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> | |
48c9c27b | 24 | #include <linux/mutex.h> |
1da177e4 LT |
25 | |
26 | #include "kcopyd.h" | |
22a1ceb1 | 27 | #include "dm.h" |
1da177e4 | 28 | |
1da177e4 LT |
29 | /*----------------------------------------------------------------- |
30 | * Each kcopyd client has its own little pool of preallocated | |
31 | * pages for kcopyd io. | |
32 | *---------------------------------------------------------------*/ | |
eb69aca5 | 33 | struct dm_kcopyd_client { |
1da177e4 LT |
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; | |
138728dc | 40 | |
373a392b MB |
41 | struct dm_io_client *io_client; |
42 | ||
138728dc AK |
43 | wait_queue_head_t destroyq; |
44 | atomic_t nr_jobs; | |
8c0cbc2f | 45 | |
08d8757a MP |
46 | mempool_t *job_pool; |
47 | ||
8c0cbc2f MP |
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; | |
1da177e4 LT |
64 | }; |
65 | ||
8c0cbc2f MP |
66 | static void wake(struct dm_kcopyd_client *kc) |
67 | { | |
68 | queue_work(kc->kcopyd_wq, &kc->kcopyd_work); | |
69 | } | |
70 | ||
1da177e4 LT |
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 | ||
eb69aca5 | 94 | static int kcopyd_get_pages(struct dm_kcopyd_client *kc, |
1da177e4 LT |
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 | ||
eb69aca5 | 117 | static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl) |
1da177e4 LT |
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 | ||
eb69aca5 | 145 | static int client_alloc_pages(struct dm_kcopyd_client *kc, unsigned int nr) |
1da177e4 LT |
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 | ||
eb69aca5 | 166 | static void client_free_pages(struct dm_kcopyd_client *kc) |
1da177e4 LT |
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 { | |
eb69aca5 | 180 | struct dm_kcopyd_client *kc; |
1da177e4 LT |
181 | struct list_head list; |
182 | unsigned long flags; | |
183 | ||
184 | /* | |
185 | * Error state of the job. | |
186 | */ | |
187 | int read_err; | |
4cdc1d1f | 188 | unsigned long write_err; |
1da177e4 LT |
189 | |
190 | /* | |
191 | * Either READ or WRITE | |
192 | */ | |
193 | int rw; | |
22a1ceb1 | 194 | struct dm_io_region source; |
1da177e4 LT |
195 | |
196 | /* | |
197 | * The destinations for the transfer. | |
198 | */ | |
199 | unsigned int num_dests; | |
eb69aca5 | 200 | struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS]; |
1da177e4 LT |
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 | */ | |
eb69aca5 | 210 | dm_kcopyd_notify_fn fn; |
1da177e4 LT |
211 | void *context; |
212 | ||
213 | /* | |
214 | * These fields are only used if the job has been split | |
215 | * into more manageable parts. | |
216 | */ | |
def5b5b2 | 217 | struct mutex lock; |
1da177e4 LT |
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 | ||
e18b890b | 225 | static struct kmem_cache *_job_cache; |
1da177e4 | 226 | |
1da177e4 LT |
227 | static int jobs_init(void) |
228 | { | |
028867ac | 229 | _job_cache = KMEM_CACHE(kcopyd_job, 0); |
1da177e4 LT |
230 | if (!_job_cache) |
231 | return -ENOMEM; | |
232 | ||
1da177e4 LT |
233 | return 0; |
234 | } | |
235 | ||
236 | static void jobs_exit(void) | |
237 | { | |
1da177e4 | 238 | kmem_cache_destroy(_job_cache); |
1da177e4 LT |
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 | */ | |
8c0cbc2f MP |
246 | static struct kcopyd_job *pop(struct list_head *jobs, |
247 | struct dm_kcopyd_client *kc) | |
1da177e4 LT |
248 | { |
249 | struct kcopyd_job *job = NULL; | |
250 | unsigned long flags; | |
251 | ||
8c0cbc2f | 252 | spin_lock_irqsave(&kc->job_lock, flags); |
1da177e4 LT |
253 | |
254 | if (!list_empty(jobs)) { | |
255 | job = list_entry(jobs->next, struct kcopyd_job, list); | |
256 | list_del(&job->list); | |
257 | } | |
8c0cbc2f | 258 | spin_unlock_irqrestore(&kc->job_lock, flags); |
1da177e4 LT |
259 | |
260 | return job; | |
261 | } | |
262 | ||
028867ac | 263 | static void push(struct list_head *jobs, struct kcopyd_job *job) |
1da177e4 LT |
264 | { |
265 | unsigned long flags; | |
8c0cbc2f | 266 | struct dm_kcopyd_client *kc = job->kc; |
1da177e4 | 267 | |
8c0cbc2f | 268 | spin_lock_irqsave(&kc->job_lock, flags); |
1da177e4 | 269 | list_add_tail(&job->list, jobs); |
8c0cbc2f | 270 | spin_unlock_irqrestore(&kc->job_lock, flags); |
1da177e4 LT |
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; | |
4cdc1d1f | 286 | unsigned long write_err = job->write_err; |
eb69aca5 HM |
287 | dm_kcopyd_notify_fn fn = job->fn; |
288 | struct dm_kcopyd_client *kc = job->kc; | |
1da177e4 | 289 | |
138728dc | 290 | kcopyd_put_pages(kc, job->pages); |
08d8757a | 291 | mempool_free(job, kc->job_pool); |
1da177e4 | 292 | fn(read_err, write_err, context); |
138728dc AK |
293 | |
294 | if (atomic_dec_and_test(&kc->nr_jobs)) | |
295 | wake_up(&kc->destroyq); | |
296 | ||
1da177e4 LT |
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; | |
8c0cbc2f | 303 | struct dm_kcopyd_client *kc = job->kc; |
1da177e4 LT |
304 | |
305 | if (error) { | |
306 | if (job->rw == WRITE) | |
ce503f59 | 307 | job->write_err |= error; |
1da177e4 LT |
308 | else |
309 | job->read_err = 1; | |
310 | ||
eb69aca5 | 311 | if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) { |
8c0cbc2f MP |
312 | push(&kc->complete_jobs, job); |
313 | wake(kc); | |
1da177e4 LT |
314 | return; |
315 | } | |
316 | } | |
317 | ||
318 | if (job->rw == WRITE) | |
8c0cbc2f | 319 | push(&kc->complete_jobs, job); |
1da177e4 LT |
320 | |
321 | else { | |
322 | job->rw = WRITE; | |
8c0cbc2f | 323 | push(&kc->io_jobs, job); |
1da177e4 LT |
324 | } |
325 | ||
8c0cbc2f | 326 | wake(kc); |
1da177e4 LT |
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; | |
373a392b MB |
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 | }; | |
1da177e4 LT |
345 | |
346 | if (job->rw == READ) | |
373a392b | 347 | r = dm_io(&io_req, 1, &job->source, NULL); |
1da177e4 | 348 | else |
373a392b | 349 | r = dm_io(&io_req, job->num_dests, job->dests, NULL); |
1da177e4 LT |
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 */ | |
8c0cbc2f | 363 | push(&job->kc->io_jobs, job); |
1da177e4 LT |
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 | */ | |
8c0cbc2f MP |
378 | static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc, |
379 | int (*fn) (struct kcopyd_job *)) | |
1da177e4 LT |
380 | { |
381 | struct kcopyd_job *job; | |
382 | int r, count = 0; | |
383 | ||
8c0cbc2f | 384 | while ((job = pop(jobs, kc))) { |
1da177e4 LT |
385 | |
386 | r = fn(job); | |
387 | ||
388 | if (r < 0) { | |
389 | /* error this rogue job */ | |
390 | if (job->rw == WRITE) | |
4cdc1d1f | 391 | job->write_err = (unsigned long) -1L; |
1da177e4 LT |
392 | else |
393 | job->read_err = 1; | |
8c0cbc2f | 394 | push(&kc->complete_jobs, job); |
1da177e4 LT |
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 | */ | |
8c0cbc2f | 416 | static void do_work(struct work_struct *work) |
1da177e4 | 417 | { |
8c0cbc2f MP |
418 | struct dm_kcopyd_client *kc = container_of(work, |
419 | struct dm_kcopyd_client, kcopyd_work); | |
420 | ||
1da177e4 LT |
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 | */ | |
8c0cbc2f MP |
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); | |
1da177e4 LT |
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 | { | |
8c0cbc2f MP |
440 | struct dm_kcopyd_client *kc = job->kc; |
441 | atomic_inc(&kc->nr_jobs); | |
442 | push(&kc->pages_jobs, job); | |
443 | wake(kc); | |
1da177e4 LT |
444 | } |
445 | ||
446 | #define SUB_JOB_SIZE 128 | |
4cdc1d1f AK |
447 | static void segment_complete(int read_err, unsigned long write_err, |
448 | void *context) | |
1da177e4 LT |
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 | ||
def5b5b2 | 455 | mutex_lock(&job->lock); |
1da177e4 LT |
456 | |
457 | /* update the error */ | |
458 | if (read_err) | |
459 | job->read_err = 1; | |
460 | ||
461 | if (write_err) | |
ce503f59 | 462 | job->write_err |= write_err; |
1da177e4 LT |
463 | |
464 | /* | |
465 | * Only dispatch more work if there hasn't been an error. | |
466 | */ | |
467 | if ((!job->read_err && !job->write_err) || | |
eb69aca5 | 468 | test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) { |
1da177e4 LT |
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 | } | |
def5b5b2 | 479 | mutex_unlock(&job->lock); |
1da177e4 LT |
480 | |
481 | if (count) { | |
482 | int i; | |
08d8757a MP |
483 | struct kcopyd_job *sub_job = mempool_alloc(job->kc->job_pool, |
484 | GFP_NOIO); | |
1da177e4 LT |
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); | |
08d8757a | 508 | mempool_free(job, job->kc->job_pool); |
1da177e4 LT |
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 | ||
eb69aca5 HM |
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) | |
1da177e4 LT |
529 | { |
530 | struct kcopyd_job *job; | |
531 | ||
532 | /* | |
533 | * Allocate a new job. | |
534 | */ | |
08d8757a | 535 | job = mempool_alloc(kc->job_pool, GFP_NOIO); |
1da177e4 LT |
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 { | |
def5b5b2 | 562 | mutex_init(&job->lock); |
1da177e4 LT |
563 | job->progress = 0; |
564 | split_job(job); | |
565 | } | |
566 | ||
567 | return 0; | |
568 | } | |
eb69aca5 | 569 | EXPORT_SYMBOL(dm_kcopyd_copy); |
1da177e4 LT |
570 | |
571 | /* | |
572 | * Cancels a kcopyd job, eg. someone might be deactivating a | |
573 | * mirror. | |
574 | */ | |
0b56306e | 575 | #if 0 |
1da177e4 LT |
576 | int kcopyd_cancel(struct kcopyd_job *job, int block) |
577 | { | |
578 | /* FIXME: finish */ | |
579 | return -1; | |
580 | } | |
0b56306e | 581 | #endif /* 0 */ |
1da177e4 LT |
582 | |
583 | /*----------------------------------------------------------------- | |
584 | * Unit setup | |
585 | *---------------------------------------------------------------*/ | |
48c9c27b | 586 | static DEFINE_MUTEX(_client_lock); |
1da177e4 LT |
587 | static LIST_HEAD(_clients); |
588 | ||
eb69aca5 | 589 | static void client_add(struct dm_kcopyd_client *kc) |
1da177e4 | 590 | { |
48c9c27b | 591 | mutex_lock(&_client_lock); |
1da177e4 | 592 | list_add(&kc->list, &_clients); |
48c9c27b | 593 | mutex_unlock(&_client_lock); |
1da177e4 LT |
594 | } |
595 | ||
eb69aca5 | 596 | static void client_del(struct dm_kcopyd_client *kc) |
1da177e4 | 597 | { |
48c9c27b | 598 | mutex_lock(&_client_lock); |
1da177e4 | 599 | list_del(&kc->list); |
48c9c27b | 600 | mutex_unlock(&_client_lock); |
1da177e4 LT |
601 | } |
602 | ||
14cc3e2b | 603 | static DEFINE_MUTEX(kcopyd_init_lock); |
1da177e4 LT |
604 | static int kcopyd_clients = 0; |
605 | ||
606 | static int kcopyd_init(void) | |
607 | { | |
608 | int r; | |
609 | ||
14cc3e2b | 610 | mutex_lock(&kcopyd_init_lock); |
1da177e4 LT |
611 | |
612 | if (kcopyd_clients) { | |
613 | /* Already initialized. */ | |
614 | kcopyd_clients++; | |
14cc3e2b | 615 | mutex_unlock(&kcopyd_init_lock); |
1da177e4 LT |
616 | return 0; |
617 | } | |
618 | ||
619 | r = jobs_init(); | |
620 | if (r) { | |
14cc3e2b | 621 | mutex_unlock(&kcopyd_init_lock); |
1da177e4 LT |
622 | return r; |
623 | } | |
624 | ||
1da177e4 | 625 | kcopyd_clients++; |
14cc3e2b | 626 | mutex_unlock(&kcopyd_init_lock); |
1da177e4 LT |
627 | return 0; |
628 | } | |
629 | ||
630 | static void kcopyd_exit(void) | |
631 | { | |
14cc3e2b | 632 | mutex_lock(&kcopyd_init_lock); |
1da177e4 LT |
633 | kcopyd_clients--; |
634 | if (!kcopyd_clients) { | |
635 | jobs_exit(); | |
1da177e4 | 636 | } |
14cc3e2b | 637 | mutex_unlock(&kcopyd_init_lock); |
1da177e4 LT |
638 | } |
639 | ||
eb69aca5 HM |
640 | int dm_kcopyd_client_create(unsigned int nr_pages, |
641 | struct dm_kcopyd_client **result) | |
1da177e4 LT |
642 | { |
643 | int r = 0; | |
eb69aca5 | 644 | struct dm_kcopyd_client *kc; |
1da177e4 LT |
645 | |
646 | r = kcopyd_init(); | |
647 | if (r) | |
648 | return r; | |
649 | ||
650 | kc = kmalloc(sizeof(*kc), GFP_KERNEL); | |
651 | if (!kc) { | |
8c0cbc2f | 652 | r = -ENOMEM; |
1da177e4 | 653 | kcopyd_exit(); |
8c0cbc2f | 654 | return r; |
1da177e4 LT |
655 | } |
656 | ||
657 | spin_lock_init(&kc->lock); | |
8c0cbc2f MP |
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 | ||
08d8757a MP |
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 | ||
8c0cbc2f MP |
671 | INIT_WORK(&kc->kcopyd_work, do_work); |
672 | kc->kcopyd_wq = create_singlethread_workqueue("kcopyd"); | |
673 | if (!kc->kcopyd_wq) { | |
674 | r = -ENOMEM; | |
08d8757a | 675 | mempool_destroy(kc->job_pool); |
8c0cbc2f MP |
676 | kfree(kc); |
677 | kcopyd_exit(); | |
678 | return r; | |
679 | } | |
680 | ||
1da177e4 LT |
681 | kc->pages = NULL; |
682 | kc->nr_pages = kc->nr_free_pages = 0; | |
683 | r = client_alloc_pages(kc, nr_pages); | |
684 | if (r) { | |
8c0cbc2f | 685 | destroy_workqueue(kc->kcopyd_wq); |
08d8757a | 686 | mempool_destroy(kc->job_pool); |
1da177e4 LT |
687 | kfree(kc); |
688 | kcopyd_exit(); | |
689 | return r; | |
690 | } | |
691 | ||
373a392b MB |
692 | kc->io_client = dm_io_client_create(nr_pages); |
693 | if (IS_ERR(kc->io_client)) { | |
694 | r = PTR_ERR(kc->io_client); | |
1da177e4 | 695 | client_free_pages(kc); |
8c0cbc2f | 696 | destroy_workqueue(kc->kcopyd_wq); |
08d8757a | 697 | mempool_destroy(kc->job_pool); |
1da177e4 LT |
698 | kfree(kc); |
699 | kcopyd_exit(); | |
700 | return r; | |
701 | } | |
702 | ||
138728dc AK |
703 | init_waitqueue_head(&kc->destroyq); |
704 | atomic_set(&kc->nr_jobs, 0); | |
705 | ||
1da177e4 LT |
706 | client_add(kc); |
707 | *result = kc; | |
708 | return 0; | |
709 | } | |
eb69aca5 | 710 | EXPORT_SYMBOL(dm_kcopyd_client_create); |
1da177e4 | 711 | |
eb69aca5 | 712 | void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc) |
1da177e4 | 713 | { |
138728dc AK |
714 | /* Wait for completion of all jobs submitted by this client. */ |
715 | wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs)); | |
716 | ||
8c0cbc2f MP |
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); | |
373a392b | 721 | dm_io_client_destroy(kc->io_client); |
1da177e4 LT |
722 | client_free_pages(kc); |
723 | client_del(kc); | |
08d8757a | 724 | mempool_destroy(kc->job_pool); |
1da177e4 LT |
725 | kfree(kc); |
726 | kcopyd_exit(); | |
727 | } | |
eb69aca5 | 728 | EXPORT_SYMBOL(dm_kcopyd_client_destroy); |