blob: e7926fa1eef2dd646e11480893b49a9a39f259ce [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>
Arun Sharma60063492011-07-26 16:09:06 -070013#include <linux/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>
Mikulas Patocka586e80e2008-10-21 17:44:59 +010025#include <linux/device-mapper.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010026#include <linux/dm-kcopyd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +010028#include "dm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Mikulas Patockac6ea41f2011-05-29 13:03:00 +010030#define SUB_JOB_SIZE 128
31#define SPLIT_COUNT 8
32#define MIN_JOBS 8
Mikulas Patocka5f43ba22011-05-29 13:03:11 +010033#define RESERVE_PAGES (DIV_ROUND_UP(SUB_JOB_SIZE << SECTOR_SHIFT, PAGE_SIZE))
Mikulas Patockac6ea41f2011-05-29 13:03:00 +010034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*-----------------------------------------------------------------
36 * Each kcopyd client has its own little pool of preallocated
37 * pages for kcopyd io.
38 *---------------------------------------------------------------*/
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +010039struct dm_kcopyd_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct page_list *pages;
Mikulas Patockad0471452011-05-29 13:03:07 +010041 unsigned nr_reserved_pages;
42 unsigned nr_free_pages;
Alasdair G Kergon138728d2006-03-27 01:17:50 -080043
Milan Broz373a3922007-05-09 02:33:02 -070044 struct dm_io_client *io_client;
45
Alasdair G Kergon138728d2006-03-27 01:17:50 -080046 wait_queue_head_t destroyq;
47 atomic_t nr_jobs;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +010048
Mikulas Patocka08d87572008-04-24 21:43:46 +010049 mempool_t *job_pool;
50
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +010051 struct workqueue_struct *kcopyd_wq;
52 struct work_struct kcopyd_work;
53
54/*
55 * We maintain three lists of jobs:
56 *
57 * i) jobs waiting for pages
58 * ii) jobs that have pages, and are waiting for the io to be issued.
59 * iii) jobs that have completed.
60 *
61 * All three of these are protected by job_lock.
62 */
63 spinlock_t job_lock;
64 struct list_head complete_jobs;
65 struct list_head io_jobs;
66 struct list_head pages_jobs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +010069static void wake(struct dm_kcopyd_client *kc)
70{
71 queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
72}
73
Mikulas Patockad0471452011-05-29 13:03:07 +010074/*
75 * Obtain one page for the use of kcopyd.
76 */
Mikulas Patockaf99b55e2011-05-29 13:03:04 +010077static struct page_list *alloc_pl(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct page_list *pl;
80
Mikulas Patockaf99b55e2011-05-29 13:03:04 +010081 pl = kmalloc(sizeof(*pl), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!pl)
83 return NULL;
84
Mikulas Patockaf99b55e2011-05-29 13:03:04 +010085 pl->page = alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (!pl->page) {
87 kfree(pl);
88 return NULL;
89 }
90
91 return pl;
92}
93
94static void free_pl(struct page_list *pl)
95{
96 __free_page(pl->page);
97 kfree(pl);
98}
99
Mikulas Patockad0471452011-05-29 13:03:07 +0100100/*
101 * Add the provided pages to a client's free page list, releasing
102 * back to the system any beyond the reserved_pages limit.
103 */
104static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
105{
106 struct page_list *next;
107
108 do {
109 next = pl->next;
110
111 if (kc->nr_free_pages >= kc->nr_reserved_pages)
112 free_pl(pl);
113 else {
114 pl->next = kc->pages;
115 kc->pages = pl;
116 kc->nr_free_pages++;
117 }
118
119 pl = next;
120 } while (pl);
121}
122
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100123static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned int nr, struct page_list **pages)
125{
126 struct page_list *pl;
127
Mikulas Patockad0471452011-05-29 13:03:07 +0100128 *pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Mikulas Patockad0471452011-05-29 13:03:07 +0100130 do {
131 pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY);
132 if (unlikely(!pl)) {
133 /* Use reserved pages */
134 pl = kc->pages;
135 if (unlikely(!pl))
136 goto out_of_memory;
137 kc->pages = pl->next;
138 kc->nr_free_pages--;
139 }
140 pl->next = *pages;
141 *pages = pl;
142 } while (--nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Mikulas Patockad0471452011-05-29 13:03:07 +0100146out_of_memory:
147 if (*pages)
148 kcopyd_put_pages(kc, *pages);
149 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/*
153 * These three functions resize the page pool.
154 */
155static void drop_pages(struct page_list *pl)
156{
157 struct page_list *next;
158
159 while (pl) {
160 next = pl->next;
161 free_pl(pl);
162 pl = next;
163 }
164}
165
Mikulas Patockad0471452011-05-29 13:03:07 +0100166/*
167 * Allocate and reserve nr_pages for the use of a specific client.
168 */
169static int client_reserve_pages(struct dm_kcopyd_client *kc, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Mikulas Patockad0471452011-05-29 13:03:07 +0100171 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct page_list *pl = NULL, *next;
173
Mikulas Patockad0471452011-05-29 13:03:07 +0100174 for (i = 0; i < nr_pages; i++) {
Mikulas Patockaf99b55e2011-05-29 13:03:04 +0100175 next = alloc_pl(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!next) {
177 if (pl)
178 drop_pages(pl);
179 return -ENOMEM;
180 }
181 next->next = pl;
182 pl = next;
183 }
184
Mikulas Patockad0471452011-05-29 13:03:07 +0100185 kc->nr_reserved_pages += nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kcopyd_put_pages(kc, pl);
Mikulas Patockad0471452011-05-29 13:03:07 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
189}
190
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100191static void client_free_pages(struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Mikulas Patockad0471452011-05-29 13:03:07 +0100193 BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 drop_pages(kc->pages);
195 kc->pages = NULL;
Mikulas Patockad0471452011-05-29 13:03:07 +0100196 kc->nr_free_pages = kc->nr_reserved_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/*-----------------------------------------------------------------
200 * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
201 * for this reason we use a mempool to prevent the client from
202 * ever having to do io (which could cause a deadlock).
203 *---------------------------------------------------------------*/
204struct kcopyd_job {
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100205 struct dm_kcopyd_client *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct list_head list;
207 unsigned long flags;
208
209 /*
210 * Error state of the job.
211 */
212 int read_err;
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700213 unsigned long write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /*
216 * Either READ or WRITE
217 */
218 int rw;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100219 struct dm_io_region source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /*
222 * The destinations for the transfer.
223 */
224 unsigned int num_dests;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100225 struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 unsigned int nr_pages;
228 struct page_list *pages;
229
230 /*
231 * Set this to ensure you are notified when the job has
232 * completed. 'context' is for callback to use.
233 */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100234 dm_kcopyd_notify_fn fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 void *context;
236
237 /*
238 * These fields are only used if the job has been split
239 * into more manageable parts.
240 */
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100241 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 atomic_t sub_jobs;
243 sector_t progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100245 struct kcopyd_job *master_job;
246};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Christoph Lametere18b8902006-12-06 20:33:20 -0800248static struct kmem_cache *_job_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100250int __init dm_kcopyd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100252 _job_cache = kmem_cache_create("kcopyd_job",
253 sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
254 __alignof__(struct kcopyd_job), 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!_job_cache)
256 return -ENOMEM;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259}
260
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100261void dm_kcopyd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kmem_cache_destroy(_job_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 _job_cache = NULL;
265}
266
267/*
268 * Functions to push and pop a job onto the head of a given job
269 * list.
270 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100271static struct kcopyd_job *pop(struct list_head *jobs,
272 struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct kcopyd_job *job = NULL;
275 unsigned long flags;
276
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100277 spin_lock_irqsave(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!list_empty(jobs)) {
280 job = list_entry(jobs->next, struct kcopyd_job, list);
281 list_del(&job->list);
282 }
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100283 spin_unlock_irqrestore(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return job;
286}
287
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100288static void push(struct list_head *jobs, struct kcopyd_job *job)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 unsigned long flags;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100291 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100293 spin_lock_irqsave(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_add_tail(&job->list, jobs);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100295 spin_unlock_irqrestore(&kc->job_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Kazuo Itob673c3a2008-10-21 17:44:50 +0100298
299static void push_head(struct list_head *jobs, struct kcopyd_job *job)
300{
301 unsigned long flags;
302 struct dm_kcopyd_client *kc = job->kc;
303
304 spin_lock_irqsave(&kc->job_lock, flags);
305 list_add(&job->list, jobs);
306 spin_unlock_irqrestore(&kc->job_lock, flags);
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * These three functions process 1 item from the corresponding
311 * job list.
312 *
313 * They return:
314 * < 0: error
315 * 0: success
316 * > 0: can't process yet.
317 */
318static int run_complete_job(struct kcopyd_job *job)
319{
320 void *context = job->context;
321 int read_err = job->read_err;
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700322 unsigned long write_err = job->write_err;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100323 dm_kcopyd_notify_fn fn = job->fn;
324 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Mikulas Patocka73830852009-04-09 00:27:16 +0100326 if (job->pages)
327 kcopyd_put_pages(kc, job->pages);
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100328 /*
329 * If this is the master job, the sub jobs have already
330 * completed so we can free everything.
331 */
332 if (job->master_job == job)
333 mempool_free(job, kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 fn(read_err, write_err, context);
Alasdair G Kergon138728d2006-03-27 01:17:50 -0800335
336 if (atomic_dec_and_test(&kc->nr_jobs))
337 wake_up(&kc->destroyq);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340}
341
342static void complete_io(unsigned long error, void *context)
343{
344 struct kcopyd_job *job = (struct kcopyd_job *) context;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100345 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (error) {
348 if (job->rw == WRITE)
Jonathan Brassowce503f52006-06-26 00:27:30 -0700349 job->write_err |= error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 else
351 job->read_err = 1;
352
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100353 if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100354 push(&kc->complete_jobs, job);
355 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357 }
358 }
359
360 if (job->rw == WRITE)
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100361 push(&kc->complete_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 else {
364 job->rw = WRITE;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100365 push(&kc->io_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100368 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371/*
372 * Request io on as many buffer heads as we can currently get for
373 * a particular job.
374 */
375static int run_io_job(struct kcopyd_job *job)
376{
377 int r;
Milan Broz373a3922007-05-09 02:33:02 -0700378 struct dm_io_request io_req = {
Mikulas Patocka8d35d3e2011-01-13 19:59:50 +0000379 .bi_rw = job->rw,
Milan Broz373a3922007-05-09 02:33:02 -0700380 .mem.type = DM_IO_PAGE_LIST,
381 .mem.ptr.pl = job->pages,
Mikulas Patocka4622afb2011-08-02 12:32:02 +0100382 .mem.offset = 0,
Milan Broz373a3922007-05-09 02:33:02 -0700383 .notify.fn = complete_io,
384 .notify.context = job,
385 .client = job->kc->io_client,
386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Jens Axboe7eaceac2011-03-10 08:52:07 +0100388 if (job->rw == READ)
Milan Broz373a3922007-05-09 02:33:02 -0700389 r = dm_io(&io_req, 1, &job->source, NULL);
Jens Axboe721a9602011-03-09 11:56:30 +0100390 else
Milan Broz373a3922007-05-09 02:33:02 -0700391 r = dm_io(&io_req, job->num_dests, job->dests, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return r;
394}
395
396static int run_pages_job(struct kcopyd_job *job)
397{
398 int r;
399
Mikulas Patocka4622afb2011-08-02 12:32:02 +0100400 job->nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
402 if (!r) {
403 /* this job is ready for io */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100404 push(&job->kc->io_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406 }
407
408 if (r == -ENOMEM)
409 /* can't complete now */
410 return 1;
411
412 return r;
413}
414
415/*
416 * Run through a list for as long as possible. Returns the count
417 * of successful jobs.
418 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100419static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
420 int (*fn) (struct kcopyd_job *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct kcopyd_job *job;
423 int r, count = 0;
424
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100425 while ((job = pop(jobs, kc))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 r = fn(job);
428
429 if (r < 0) {
430 /* error this rogue job */
431 if (job->rw == WRITE)
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700432 job->write_err = (unsigned long) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 else
434 job->read_err = 1;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100435 push(&kc->complete_jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 }
438
439 if (r > 0) {
440 /*
441 * We couldn't service this job ATM, so
442 * push this job back onto the list.
443 */
Kazuo Itob673c3a2008-10-21 17:44:50 +0100444 push_head(jobs, job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446 }
447
448 count++;
449 }
450
451 return count;
452}
453
454/*
455 * kcopyd does this every time it's woken up.
456 */
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100457static void do_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100459 struct dm_kcopyd_client *kc = container_of(work,
460 struct dm_kcopyd_client, kcopyd_work);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100461 struct blk_plug plug;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * The order that these are called is *very* important.
465 * complete jobs can free some pages for pages jobs.
466 * Pages jobs when successful will jump onto the io jobs
467 * list. io jobs call wake when they complete and it all
468 * starts again.
469 */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100470 blk_start_plug(&plug);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100471 process_jobs(&kc->complete_jobs, kc, run_complete_job);
472 process_jobs(&kc->pages_jobs, kc, run_pages_job);
473 process_jobs(&kc->io_jobs, kc, run_io_job);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100474 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
478 * If we are copying a small region we just dispatch a single job
479 * to do the copy, otherwise the io has to be split up into many
480 * jobs.
481 */
482static void dispatch_job(struct kcopyd_job *job)
483{
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100484 struct dm_kcopyd_client *kc = job->kc;
485 atomic_inc(&kc->nr_jobs);
Mikulas Patocka9ca170a2009-12-10 23:52:13 +0000486 if (unlikely(!job->source.count))
487 push(&kc->complete_jobs, job);
488 else
489 push(&kc->pages_jobs, job);
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100490 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700493static void segment_complete(int read_err, unsigned long write_err,
494 void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 /* FIXME: tidy this function */
497 sector_t progress = 0;
498 sector_t count = 0;
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100499 struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
500 struct kcopyd_job *job = sub_job->master_job;
Mikulas Patocka73830852009-04-09 00:27:16 +0100501 struct dm_kcopyd_client *kc = job->kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100503 mutex_lock(&job->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* update the error */
506 if (read_err)
507 job->read_err = 1;
508
509 if (write_err)
Jonathan Brassowce503f52006-06-26 00:27:30 -0700510 job->write_err |= write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /*
513 * Only dispatch more work if there hasn't been an error.
514 */
515 if ((!job->read_err && !job->write_err) ||
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100516 test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* get the next chunk of work */
518 progress = job->progress;
519 count = job->source.count - progress;
520 if (count) {
521 if (count > SUB_JOB_SIZE)
522 count = SUB_JOB_SIZE;
523
524 job->progress += count;
525 }
526 }
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100527 mutex_unlock(&job->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (count) {
530 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 *sub_job = *job;
533 sub_job->source.sector += progress;
534 sub_job->source.count = count;
535
536 for (i = 0; i < job->num_dests; i++) {
537 sub_job->dests[i].sector += progress;
538 sub_job->dests[i].count = count;
539 }
540
541 sub_job->fn = segment_complete;
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100542 sub_job->context = sub_job;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dispatch_job(sub_job);
544
545 } else if (atomic_dec_and_test(&job->sub_jobs)) {
546
547 /*
Mikulas Patocka340cd442009-04-09 00:27:17 +0100548 * Queue the completion callback to the kcopyd thread.
549 *
550 * Some callers assume that all the completions are called
551 * from a single thread and don't race with each other.
552 *
553 * We must not call the callback directly here because this
554 * code may not be executing in the thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Mikulas Patocka340cd442009-04-09 00:27:17 +0100556 push(&kc->complete_jobs, job);
557 wake(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559}
560
561/*
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100562 * Create some sub jobs to share the work between them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100564static void split_job(struct kcopyd_job *master_job)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int i;
567
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100568 atomic_inc(&master_job->kc->nr_jobs);
Mikulas Patocka340cd442009-04-09 00:27:17 +0100569
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100570 atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
571 for (i = 0; i < SPLIT_COUNT; i++) {
572 master_job[i + 1].master_job = master_job;
573 segment_complete(0, 0u, &master_job[i + 1]);
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100577int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
578 unsigned int num_dests, struct dm_io_region *dests,
579 unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 struct kcopyd_job *job;
582
583 /*
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100584 * Allocate an array of jobs consisting of one master job
585 * followed by SPLIT_COUNT sub jobs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Mikulas Patocka08d87572008-04-24 21:43:46 +0100587 job = mempool_alloc(kc->job_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /*
590 * set up for the read.
591 */
592 job->kc = kc;
593 job->flags = flags;
594 job->read_err = 0;
595 job->write_err = 0;
596 job->rw = READ;
597
598 job->source = *from;
599
600 job->num_dests = num_dests;
601 memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 job->nr_pages = 0;
604 job->pages = NULL;
605
606 job->fn = fn;
607 job->context = context;
Mikulas Patockac6ea41f2011-05-29 13:03:00 +0100608 job->master_job = job;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Mikulas Patockaa705a342011-05-29 13:02:58 +0100610 if (job->source.count <= SUB_JOB_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 dispatch_job(job);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 else {
Matthias Kaehlckedef5b5b2007-10-19 22:38:52 +0100613 mutex_init(&job->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 job->progress = 0;
615 split_job(job);
616 }
617
618 return 0;
619}
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100620EXPORT_SYMBOL(dm_kcopyd_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/*
623 * Cancels a kcopyd job, eg. someone might be deactivating a
624 * mirror.
625 */
Adrian Bunk0b563062006-01-06 00:20:08 -0800626#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627int kcopyd_cancel(struct kcopyd_job *job, int block)
628{
629 /* FIXME: finish */
630 return -1;
631}
Adrian Bunk0b563062006-01-06 00:20:08 -0800632#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/*-----------------------------------------------------------------
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100635 * Client setup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *---------------------------------------------------------------*/
Mikulas Patockafa34ce72011-05-29 13:03:13 +0100637struct dm_kcopyd_client *dm_kcopyd_client_create(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100639 int r = -ENOMEM;
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100640 struct dm_kcopyd_client *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 kc = kmalloc(sizeof(*kc), GFP_KERNEL);
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100643 if (!kc)
Mikulas Patockafa34ce72011-05-29 13:03:13 +0100644 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100646 spin_lock_init(&kc->job_lock);
647 INIT_LIST_HEAD(&kc->complete_jobs);
648 INIT_LIST_HEAD(&kc->io_jobs);
649 INIT_LIST_HEAD(&kc->pages_jobs);
650
Mikulas Patocka08d87572008-04-24 21:43:46 +0100651 kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100652 if (!kc->job_pool)
653 goto bad_slab;
Mikulas Patocka08d87572008-04-24 21:43:46 +0100654
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100655 INIT_WORK(&kc->kcopyd_work, do_work);
Tejun Heo9c4376d2011-01-13 19:59:58 +0000656 kc->kcopyd_wq = alloc_workqueue("kcopyd",
657 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100658 if (!kc->kcopyd_wq)
659 goto bad_workqueue;
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 kc->pages = NULL;
Mikulas Patockad0471452011-05-29 13:03:07 +0100662 kc->nr_reserved_pages = kc->nr_free_pages = 0;
Mikulas Patocka5f43ba22011-05-29 13:03:11 +0100663 r = client_reserve_pages(kc, RESERVE_PAGES);
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100664 if (r)
665 goto bad_client_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Mikulas Patockabda8efe2011-05-29 13:03:09 +0100667 kc->io_client = dm_io_client_create();
Milan Broz373a3922007-05-09 02:33:02 -0700668 if (IS_ERR(kc->io_client)) {
669 r = PTR_ERR(kc->io_client);
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100670 goto bad_io_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Alasdair G Kergon138728d2006-03-27 01:17:50 -0800673 init_waitqueue_head(&kc->destroyq);
674 atomic_set(&kc->nr_jobs, 0);
675
Mikulas Patockafa34ce72011-05-29 13:03:13 +0100676 return kc;
Mikulas Patocka945fa4d2008-04-24 21:43:49 +0100677
678bad_io_client:
679 client_free_pages(kc);
680bad_client_pages:
681 destroy_workqueue(kc->kcopyd_wq);
682bad_workqueue:
683 mempool_destroy(kc->job_pool);
684bad_slab:
685 kfree(kc);
686
Mikulas Patockafa34ce72011-05-29 13:03:13 +0100687 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100689EXPORT_SYMBOL(dm_kcopyd_client_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100691void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Alasdair G Kergon138728d2006-03-27 01:17:50 -0800693 /* Wait for completion of all jobs submitted by this client. */
694 wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
695
Mikulas Patocka8c0cbc22008-04-24 21:43:44 +0100696 BUG_ON(!list_empty(&kc->complete_jobs));
697 BUG_ON(!list_empty(&kc->io_jobs));
698 BUG_ON(!list_empty(&kc->pages_jobs));
699 destroy_workqueue(kc->kcopyd_wq);
Milan Broz373a3922007-05-09 02:33:02 -0700700 dm_io_client_destroy(kc->io_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 client_free_pages(kc);
Mikulas Patocka08d87572008-04-24 21:43:46 +0100702 mempool_destroy(kc->job_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 kfree(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100705EXPORT_SYMBOL(dm_kcopyd_client_destroy);