2 * Copyright (C) 2002 Sistina Software (UK) Limited.
4 * This file is released under the GPL.
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.
11 #include <asm/types.h>
12 #include <asm/atomic.h>
14 #include <linux/blkdev.h>
15 #include <linux/config.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/mempool.h>
20 #include <linux/module.h>
21 #include <linux/pagemap.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/workqueue.h>
28 static struct workqueue_struct
*_kcopyd_wq
;
29 static struct work_struct _kcopyd_work
;
31 static inline void wake(void)
33 queue_work(_kcopyd_wq
, &_kcopyd_work
);
36 /*-----------------------------------------------------------------
37 * Each kcopyd client has its own little pool of preallocated
38 * pages for kcopyd io.
39 *---------------------------------------------------------------*/
40 struct kcopyd_client
{
41 struct list_head list
;
44 struct page_list
*pages
;
45 unsigned int nr_pages
;
46 unsigned int nr_free_pages
;
49 static struct page_list
*alloc_pl(void)
53 pl
= kmalloc(sizeof(*pl
), GFP_KERNEL
);
57 pl
->page
= alloc_page(GFP_KERNEL
);
66 static void free_pl(struct page_list
*pl
)
68 __free_page(pl
->page
);
72 static int kcopyd_get_pages(struct kcopyd_client
*kc
,
73 unsigned int nr
, struct page_list
**pages
)
78 if (kc
->nr_free_pages
< nr
) {
79 spin_unlock(&kc
->lock
);
83 kc
->nr_free_pages
-= nr
;
84 for (*pages
= pl
= kc
->pages
; --nr
; pl
= pl
->next
)
90 spin_unlock(&kc
->lock
);
95 static void kcopyd_put_pages(struct kcopyd_client
*kc
, struct page_list
*pl
)
97 struct page_list
*cursor
;
100 for (cursor
= pl
; cursor
->next
; cursor
= cursor
->next
)
104 cursor
->next
= kc
->pages
;
106 spin_unlock(&kc
->lock
);
110 * These three functions resize the page pool.
112 static void drop_pages(struct page_list
*pl
)
114 struct page_list
*next
;
123 static int client_alloc_pages(struct kcopyd_client
*kc
, unsigned int nr
)
126 struct page_list
*pl
= NULL
, *next
;
128 for (i
= 0; i
< nr
; i
++) {
139 kcopyd_put_pages(kc
, pl
);
144 static void client_free_pages(struct kcopyd_client
*kc
)
146 BUG_ON(kc
->nr_free_pages
!= kc
->nr_pages
);
147 drop_pages(kc
->pages
);
149 kc
->nr_free_pages
= kc
->nr_pages
= 0;
152 /*-----------------------------------------------------------------
153 * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
154 * for this reason we use a mempool to prevent the client from
155 * ever having to do io (which could cause a deadlock).
156 *---------------------------------------------------------------*/
158 struct kcopyd_client
*kc
;
159 struct list_head list
;
163 * Error state of the job.
166 unsigned int write_err
;
169 * Either READ or WRITE
172 struct io_region source
;
175 * The destinations for the transfer.
177 unsigned int num_dests
;
178 struct io_region dests
[KCOPYD_MAX_REGIONS
];
181 unsigned int nr_pages
;
182 struct page_list
*pages
;
185 * Set this to ensure you are notified when the job has
186 * completed. 'context' is for callback to use.
192 * These fields are only used if the job has been split
193 * into more manageable parts.
195 struct semaphore lock
;
200 /* FIXME: this should scale with the number of pages */
203 static kmem_cache_t
*_job_cache
;
204 static mempool_t
*_job_pool
;
207 * We maintain three lists of jobs:
209 * i) jobs waiting for pages
210 * ii) jobs that have pages, and are waiting for the io to be issued.
211 * iii) jobs that have completed.
213 * All three of these are protected by job_lock.
215 static DEFINE_SPINLOCK(_job_lock
);
217 static LIST_HEAD(_complete_jobs
);
218 static LIST_HEAD(_io_jobs
);
219 static LIST_HEAD(_pages_jobs
);
221 static int jobs_init(void)
223 _job_cache
= kmem_cache_create("kcopyd-jobs",
224 sizeof(struct kcopyd_job
),
225 __alignof__(struct kcopyd_job
),
230 _job_pool
= mempool_create(MIN_JOBS
, mempool_alloc_slab
,
231 mempool_free_slab
, _job_cache
);
233 kmem_cache_destroy(_job_cache
);
240 static void jobs_exit(void)
242 BUG_ON(!list_empty(&_complete_jobs
));
243 BUG_ON(!list_empty(&_io_jobs
));
244 BUG_ON(!list_empty(&_pages_jobs
));
246 mempool_destroy(_job_pool
);
247 kmem_cache_destroy(_job_cache
);
253 * Functions to push and pop a job onto the head of a given job
256 static inline struct kcopyd_job
*pop(struct list_head
*jobs
)
258 struct kcopyd_job
*job
= NULL
;
261 spin_lock_irqsave(&_job_lock
, flags
);
263 if (!list_empty(jobs
)) {
264 job
= list_entry(jobs
->next
, struct kcopyd_job
, list
);
265 list_del(&job
->list
);
267 spin_unlock_irqrestore(&_job_lock
, flags
);
272 static inline void push(struct list_head
*jobs
, struct kcopyd_job
*job
)
276 spin_lock_irqsave(&_job_lock
, flags
);
277 list_add_tail(&job
->list
, jobs
);
278 spin_unlock_irqrestore(&_job_lock
, flags
);
282 * These three functions process 1 item from the corresponding
288 * > 0: can't process yet.
290 static int run_complete_job(struct kcopyd_job
*job
)
292 void *context
= job
->context
;
293 int read_err
= job
->read_err
;
294 unsigned int write_err
= job
->write_err
;
295 kcopyd_notify_fn fn
= job
->fn
;
297 kcopyd_put_pages(job
->kc
, job
->pages
);
298 mempool_free(job
, _job_pool
);
299 fn(read_err
, write_err
, context
);
303 static void complete_io(unsigned long error
, void *context
)
305 struct kcopyd_job
*job
= (struct kcopyd_job
*) context
;
308 if (job
->rw
== WRITE
)
309 job
->write_err
&= error
;
313 if (!test_bit(KCOPYD_IGNORE_ERROR
, &job
->flags
)) {
314 push(&_complete_jobs
, job
);
320 if (job
->rw
== WRITE
)
321 push(&_complete_jobs
, job
);
325 push(&_io_jobs
, job
);
332 * Request io on as many buffer heads as we can currently get for
335 static int run_io_job(struct kcopyd_job
*job
)
340 r
= dm_io_async(1, &job
->source
, job
->rw
,
342 job
->offset
, complete_io
, job
);
345 r
= dm_io_async(job
->num_dests
, job
->dests
, job
->rw
,
347 job
->offset
, complete_io
, job
);
352 static int run_pages_job(struct kcopyd_job
*job
)
356 job
->nr_pages
= dm_div_up(job
->dests
[0].count
+ job
->offset
,
358 r
= kcopyd_get_pages(job
->kc
, job
->nr_pages
, &job
->pages
);
360 /* this job is ready for io */
361 push(&_io_jobs
, job
);
366 /* can't complete now */
373 * Run through a list for as long as possible. Returns the count
374 * of successful jobs.
376 static int process_jobs(struct list_head
*jobs
, int (*fn
) (struct kcopyd_job
*))
378 struct kcopyd_job
*job
;
381 while ((job
= pop(jobs
))) {
386 /* error this rogue job */
387 if (job
->rw
== WRITE
)
388 job
->write_err
= (unsigned int) -1;
391 push(&_complete_jobs
, job
);
397 * We couldn't service this job ATM, so
398 * push this job back onto the list.
411 * kcopyd does this every time it's woken up.
413 static void do_work(void *ignored
)
416 * The order that these are called is *very* important.
417 * complete jobs can free some pages for pages jobs.
418 * Pages jobs when successful will jump onto the io jobs
419 * list. io jobs call wake when they complete and it all
422 process_jobs(&_complete_jobs
, run_complete_job
);
423 process_jobs(&_pages_jobs
, run_pages_job
);
424 process_jobs(&_io_jobs
, run_io_job
);
428 * If we are copying a small region we just dispatch a single job
429 * to do the copy, otherwise the io has to be split up into many
432 static void dispatch_job(struct kcopyd_job
*job
)
434 push(&_pages_jobs
, job
);
438 #define SUB_JOB_SIZE 128
439 static void segment_complete(int read_err
,
440 unsigned int write_err
, void *context
)
442 /* FIXME: tidy this function */
443 sector_t progress
= 0;
445 struct kcopyd_job
*job
= (struct kcopyd_job
*) context
;
449 /* update the error */
454 job
->write_err
&= write_err
;
457 * Only dispatch more work if there hasn't been an error.
459 if ((!job
->read_err
&& !job
->write_err
) ||
460 test_bit(KCOPYD_IGNORE_ERROR
, &job
->flags
)) {
461 /* get the next chunk of work */
462 progress
= job
->progress
;
463 count
= job
->source
.count
- progress
;
465 if (count
> SUB_JOB_SIZE
)
466 count
= SUB_JOB_SIZE
;
468 job
->progress
+= count
;
475 struct kcopyd_job
*sub_job
= mempool_alloc(_job_pool
, GFP_NOIO
);
478 sub_job
->source
.sector
+= progress
;
479 sub_job
->source
.count
= count
;
481 for (i
= 0; i
< job
->num_dests
; i
++) {
482 sub_job
->dests
[i
].sector
+= progress
;
483 sub_job
->dests
[i
].count
= count
;
486 sub_job
->fn
= segment_complete
;
487 sub_job
->context
= job
;
488 dispatch_job(sub_job
);
490 } else if (atomic_dec_and_test(&job
->sub_jobs
)) {
493 * To avoid a race we must keep the job around
494 * until after the notify function has completed.
495 * Otherwise the client may try and stop the job
496 * after we've completed.
498 job
->fn(read_err
, write_err
, job
->context
);
499 mempool_free(job
, _job_pool
);
504 * Create some little jobs that will do the move between
507 #define SPLIT_COUNT 8
508 static void split_job(struct kcopyd_job
*job
)
512 atomic_set(&job
->sub_jobs
, SPLIT_COUNT
);
513 for (i
= 0; i
< SPLIT_COUNT
; i
++)
514 segment_complete(0, 0u, job
);
517 int kcopyd_copy(struct kcopyd_client
*kc
, struct io_region
*from
,
518 unsigned int num_dests
, struct io_region
*dests
,
519 unsigned int flags
, kcopyd_notify_fn fn
, void *context
)
521 struct kcopyd_job
*job
;
524 * Allocate a new job.
526 job
= mempool_alloc(_job_pool
, GFP_NOIO
);
529 * set up for the read.
539 job
->num_dests
= num_dests
;
540 memcpy(&job
->dests
, dests
, sizeof(*dests
) * num_dests
);
547 job
->context
= context
;
549 if (job
->source
.count
< SUB_JOB_SIZE
)
553 init_MUTEX(&job
->lock
);
562 * Cancels a kcopyd job, eg. someone might be deactivating a
566 int kcopyd_cancel(struct kcopyd_job
*job
, int block
)
573 /*-----------------------------------------------------------------
575 *---------------------------------------------------------------*/
576 static DECLARE_MUTEX(_client_lock
);
577 static LIST_HEAD(_clients
);
579 static void client_add(struct kcopyd_client
*kc
)
582 list_add(&kc
->list
, &_clients
);
586 static void client_del(struct kcopyd_client
*kc
)
593 static DECLARE_MUTEX(kcopyd_init_lock
);
594 static int kcopyd_clients
= 0;
596 static int kcopyd_init(void)
600 down(&kcopyd_init_lock
);
602 if (kcopyd_clients
) {
603 /* Already initialized. */
605 up(&kcopyd_init_lock
);
611 up(&kcopyd_init_lock
);
615 _kcopyd_wq
= create_singlethread_workqueue("kcopyd");
618 up(&kcopyd_init_lock
);
623 INIT_WORK(&_kcopyd_work
, do_work
, NULL
);
624 up(&kcopyd_init_lock
);
628 static void kcopyd_exit(void)
630 down(&kcopyd_init_lock
);
632 if (!kcopyd_clients
) {
634 destroy_workqueue(_kcopyd_wq
);
637 up(&kcopyd_init_lock
);
640 int kcopyd_client_create(unsigned int nr_pages
, struct kcopyd_client
**result
)
643 struct kcopyd_client
*kc
;
649 kc
= kmalloc(sizeof(*kc
), GFP_KERNEL
);
655 spin_lock_init(&kc
->lock
);
657 kc
->nr_pages
= kc
->nr_free_pages
= 0;
658 r
= client_alloc_pages(kc
, nr_pages
);
665 r
= dm_io_get(nr_pages
);
667 client_free_pages(kc
);
678 void kcopyd_client_destroy(struct kcopyd_client
*kc
)
680 dm_io_put(kc
->nr_pages
);
681 client_free_pages(kc
);
687 EXPORT_SYMBOL(kcopyd_client_create
);
688 EXPORT_SYMBOL(kcopyd_client_destroy
);
689 EXPORT_SYMBOL(kcopyd_copy
);