dm-kcopyd.c 15.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (C) 2002 Sistina Software (UK) Limited.
M
Milan Broz 已提交
3
 * Copyright (C) 2006 Red Hat GmbH
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11
 *
 * This file is released under the GPL.
 *
 * Kcopyd provides a simple interface for copying an area of one
 * block-device to one or more other block-devices, with an asynchronous
 * completion notification.
 */

H
Heinz Mauelshagen 已提交
12
#include <linux/types.h>
A
Arun Sharma 已提交
13
#include <linux/atomic.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23
#include <linux/blkdev.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/mempool.h>
#include <linux/module.h>
#include <linux/pagemap.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/workqueue.h>
A
Arjan van de Ven 已提交
24
#include <linux/mutex.h>
25
#include <linux/device-mapper.h>
A
Alasdair G Kergon 已提交
26
#include <linux/dm-kcopyd.h>
L
Linus Torvalds 已提交
27

H
Heinz Mauelshagen 已提交
28
#include "dm.h"
L
Linus Torvalds 已提交
29

30 31 32
#define SUB_JOB_SIZE	128
#define SPLIT_COUNT	8
#define MIN_JOBS	8
M
Mikulas Patocka 已提交
33
#define RESERVE_PAGES	(DIV_ROUND_UP(SUB_JOB_SIZE << SECTOR_SHIFT, PAGE_SIZE))
34

L
Linus Torvalds 已提交
35 36 37 38
/*-----------------------------------------------------------------
 * Each kcopyd client has its own little pool of preallocated
 * pages for kcopyd io.
 *---------------------------------------------------------------*/
H
Heinz Mauelshagen 已提交
39
struct dm_kcopyd_client {
L
Linus Torvalds 已提交
40
	struct page_list *pages;
41 42
	unsigned nr_reserved_pages;
	unsigned nr_free_pages;
43

M
Milan Broz 已提交
44 45
	struct dm_io_client *io_client;

46 47
	wait_queue_head_t destroyq;
	atomic_t nr_jobs;
M
Mikulas Patocka 已提交
48

M
Mikulas Patocka 已提交
49 50
	mempool_t *job_pool;

M
Mikulas Patocka 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	struct workqueue_struct *kcopyd_wq;
	struct work_struct kcopyd_work;

/*
 * We maintain three lists of jobs:
 *
 * i)   jobs waiting for pages
 * ii)  jobs that have pages, and are waiting for the io to be issued.
 * iii) jobs that have completed.
 *
 * All three of these are protected by job_lock.
 */
	spinlock_t job_lock;
	struct list_head complete_jobs;
	struct list_head io_jobs;
	struct list_head pages_jobs;
L
Linus Torvalds 已提交
67 68
};

M
Mikulas Patocka 已提交
69 70 71 72 73
static void wake(struct dm_kcopyd_client *kc)
{
	queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
}

74 75 76
/*
 * Obtain one page for the use of kcopyd.
 */
77
static struct page_list *alloc_pl(gfp_t gfp)
L
Linus Torvalds 已提交
78 79 80
{
	struct page_list *pl;

81
	pl = kmalloc(sizeof(*pl), gfp);
L
Linus Torvalds 已提交
82 83 84
	if (!pl)
		return NULL;

85
	pl->page = alloc_page(gfp);
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if (!pl->page) {
		kfree(pl);
		return NULL;
	}

	return pl;
}

static void free_pl(struct page_list *pl)
{
	__free_page(pl->page);
	kfree(pl);
}

100 101 102 103 104
/*
 * Add the provided pages to a client's free page list, releasing
 * back to the system any beyond the reserved_pages limit.
 */
static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
L
Linus Torvalds 已提交
105
{
106
	struct page_list *next;
L
Linus Torvalds 已提交
107

108 109
	do {
		next = pl->next;
L
Linus Torvalds 已提交
110

111 112 113 114 115 116 117
		if (kc->nr_free_pages >= kc->nr_reserved_pages)
			free_pl(pl);
		else {
			pl->next = kc->pages;
			kc->pages = pl;
			kc->nr_free_pages++;
		}
L
Linus Torvalds 已提交
118

119 120
		pl = next;
	} while (pl);
L
Linus Torvalds 已提交
121 122
}

123 124
static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
			    unsigned int nr, struct page_list **pages)
L
Linus Torvalds 已提交
125
{
126
	struct page_list *pl;
L
Linus Torvalds 已提交
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	*pages = NULL;

	do {
		pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY);
		if (unlikely(!pl)) {
			/* Use reserved pages */
			pl = kc->pages;
			if (unlikely(!pl))
				goto out_of_memory;
			kc->pages = pl->next;
			kc->nr_free_pages--;
		}
		pl->next = *pages;
		*pages = pl;
	} while (--nr);

	return 0;
L
Linus Torvalds 已提交
145

146 147 148 149
out_of_memory:
	if (*pages)
		kcopyd_put_pages(kc, *pages);
	return -ENOMEM;
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

/*
 * These three functions resize the page pool.
 */
static void drop_pages(struct page_list *pl)
{
	struct page_list *next;

	while (pl) {
		next = pl->next;
		free_pl(pl);
		pl = next;
	}
}

166 167 168 169
/*
 * Allocate and reserve nr_pages for the use of a specific client.
 */
static int client_reserve_pages(struct dm_kcopyd_client *kc, unsigned nr_pages)
L
Linus Torvalds 已提交
170
{
171
	unsigned i;
L
Linus Torvalds 已提交
172 173
	struct page_list *pl = NULL, *next;

174
	for (i = 0; i < nr_pages; i++) {
175
		next = alloc_pl(GFP_KERNEL);
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184
		if (!next) {
			if (pl)
				drop_pages(pl);
			return -ENOMEM;
		}
		next->next = pl;
		pl = next;
	}

185
	kc->nr_reserved_pages += nr_pages;
L
Linus Torvalds 已提交
186
	kcopyd_put_pages(kc, pl);
187

L
Linus Torvalds 已提交
188 189 190
	return 0;
}

H
Heinz Mauelshagen 已提交
191
static void client_free_pages(struct dm_kcopyd_client *kc)
L
Linus Torvalds 已提交
192
{
193
	BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
L
Linus Torvalds 已提交
194 195
	drop_pages(kc->pages);
	kc->pages = NULL;
196
	kc->nr_free_pages = kc->nr_reserved_pages = 0;
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204
}

/*-----------------------------------------------------------------
 * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
 * for this reason we use a mempool to prevent the client from
 * ever having to do io (which could cause a deadlock).
 *---------------------------------------------------------------*/
struct kcopyd_job {
H
Heinz Mauelshagen 已提交
205
	struct dm_kcopyd_client *kc;
L
Linus Torvalds 已提交
206 207 208 209 210 211 212
	struct list_head list;
	unsigned long flags;

	/*
	 * Error state of the job.
	 */
	int read_err;
213
	unsigned long write_err;
L
Linus Torvalds 已提交
214 215 216 217 218

	/*
	 * Either READ or WRITE
	 */
	int rw;
H
Heinz Mauelshagen 已提交
219
	struct dm_io_region source;
L
Linus Torvalds 已提交
220 221 222 223 224

	/*
	 * The destinations for the transfer.
	 */
	unsigned int num_dests;
H
Heinz Mauelshagen 已提交
225
	struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
L
Linus Torvalds 已提交
226 227 228 229 230 231 232

	struct page_list *pages;

	/*
	 * Set this to ensure you are notified when the job has
	 * completed.  'context' is for callback to use.
	 */
H
Heinz Mauelshagen 已提交
233
	dm_kcopyd_notify_fn fn;
L
Linus Torvalds 已提交
234 235 236 237 238 239
	void *context;

	/*
	 * These fields are only used if the job has been split
	 * into more manageable parts.
	 */
240
	struct mutex lock;
L
Linus Torvalds 已提交
241 242 243
	atomic_t sub_jobs;
	sector_t progress;

244 245
	struct kcopyd_job *master_job;
};
L
Linus Torvalds 已提交
246

247
static struct kmem_cache *_job_cache;
L
Linus Torvalds 已提交
248

249
int __init dm_kcopyd_init(void)
L
Linus Torvalds 已提交
250
{
251 252 253
	_job_cache = kmem_cache_create("kcopyd_job",
				sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
				__alignof__(struct kcopyd_job), 0, NULL);
L
Linus Torvalds 已提交
254 255 256 257 258 259
	if (!_job_cache)
		return -ENOMEM;

	return 0;
}

260
void dm_kcopyd_exit(void)
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269
{
	kmem_cache_destroy(_job_cache);
	_job_cache = NULL;
}

/*
 * Functions to push and pop a job onto the head of a given job
 * list.
 */
M
Mikulas Patocka 已提交
270 271
static struct kcopyd_job *pop(struct list_head *jobs,
			      struct dm_kcopyd_client *kc)
L
Linus Torvalds 已提交
272 273 274 275
{
	struct kcopyd_job *job = NULL;
	unsigned long flags;

M
Mikulas Patocka 已提交
276
	spin_lock_irqsave(&kc->job_lock, flags);
L
Linus Torvalds 已提交
277 278 279 280 281

	if (!list_empty(jobs)) {
		job = list_entry(jobs->next, struct kcopyd_job, list);
		list_del(&job->list);
	}
M
Mikulas Patocka 已提交
282
	spin_unlock_irqrestore(&kc->job_lock, flags);
L
Linus Torvalds 已提交
283 284 285 286

	return job;
}

A
Alasdair G Kergon 已提交
287
static void push(struct list_head *jobs, struct kcopyd_job *job)
L
Linus Torvalds 已提交
288 289
{
	unsigned long flags;
M
Mikulas Patocka 已提交
290
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
291

M
Mikulas Patocka 已提交
292
	spin_lock_irqsave(&kc->job_lock, flags);
L
Linus Torvalds 已提交
293
	list_add_tail(&job->list, jobs);
M
Mikulas Patocka 已提交
294
	spin_unlock_irqrestore(&kc->job_lock, flags);
L
Linus Torvalds 已提交
295 296
}

K
Kazuo Ito 已提交
297 298 299 300 301 302 303 304 305 306 307

static void push_head(struct list_head *jobs, struct kcopyd_job *job)
{
	unsigned long flags;
	struct dm_kcopyd_client *kc = job->kc;

	spin_lock_irqsave(&kc->job_lock, flags);
	list_add(&job->list, jobs);
	spin_unlock_irqrestore(&kc->job_lock, flags);
}

L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320
/*
 * These three functions process 1 item from the corresponding
 * job list.
 *
 * They return:
 * < 0: error
 *   0: success
 * > 0: can't process yet.
 */
static int run_complete_job(struct kcopyd_job *job)
{
	void *context = job->context;
	int read_err = job->read_err;
321
	unsigned long write_err = job->write_err;
H
Heinz Mauelshagen 已提交
322 323
	dm_kcopyd_notify_fn fn = job->fn;
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
324

325 326
	if (job->pages)
		kcopyd_put_pages(kc, job->pages);
327 328 329 330 331 332
	/*
	 * If this is the master job, the sub jobs have already
	 * completed so we can free everything.
	 */
	if (job->master_job == job)
		mempool_free(job, kc->job_pool);
L
Linus Torvalds 已提交
333
	fn(read_err, write_err, context);
334 335 336 337

	if (atomic_dec_and_test(&kc->nr_jobs))
		wake_up(&kc->destroyq);

L
Linus Torvalds 已提交
338 339 340 341 342 343
	return 0;
}

static void complete_io(unsigned long error, void *context)
{
	struct kcopyd_job *job = (struct kcopyd_job *) context;
M
Mikulas Patocka 已提交
344
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
345 346 347

	if (error) {
		if (job->rw == WRITE)
348
			job->write_err |= error;
L
Linus Torvalds 已提交
349 350 351
		else
			job->read_err = 1;

H
Heinz Mauelshagen 已提交
352
		if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
M
Mikulas Patocka 已提交
353 354
			push(&kc->complete_jobs, job);
			wake(kc);
L
Linus Torvalds 已提交
355 356 357 358 359
			return;
		}
	}

	if (job->rw == WRITE)
M
Mikulas Patocka 已提交
360
		push(&kc->complete_jobs, job);
L
Linus Torvalds 已提交
361 362 363

	else {
		job->rw = WRITE;
M
Mikulas Patocka 已提交
364
		push(&kc->io_jobs, job);
L
Linus Torvalds 已提交
365 366
	}

M
Mikulas Patocka 已提交
367
	wake(kc);
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376
}

/*
 * Request io on as many buffer heads as we can currently get for
 * a particular job.
 */
static int run_io_job(struct kcopyd_job *job)
{
	int r;
M
Milan Broz 已提交
377
	struct dm_io_request io_req = {
M
Mikulas Patocka 已提交
378
		.bi_rw = job->rw,
M
Milan Broz 已提交
379 380
		.mem.type = DM_IO_PAGE_LIST,
		.mem.ptr.pl = job->pages,
381
		.mem.offset = 0,
M
Milan Broz 已提交
382 383 384 385
		.notify.fn = complete_io,
		.notify.context = job,
		.client = job->kc->io_client,
	};
L
Linus Torvalds 已提交
386

J
Jens Axboe 已提交
387
	if (job->rw == READ)
M
Milan Broz 已提交
388
		r = dm_io(&io_req, 1, &job->source, NULL);
J
Jens Axboe 已提交
389
	else
M
Milan Broz 已提交
390
		r = dm_io(&io_req, job->num_dests, job->dests, NULL);
L
Linus Torvalds 已提交
391 392 393 394 395 396 397

	return r;
}

static int run_pages_job(struct kcopyd_job *job)
{
	int r;
398
	unsigned nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
L
Linus Torvalds 已提交
399

400
	r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
L
Linus Torvalds 已提交
401 402
	if (!r) {
		/* this job is ready for io */
M
Mikulas Patocka 已提交
403
		push(&job->kc->io_jobs, job);
L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417
		return 0;
	}

	if (r == -ENOMEM)
		/* can't complete now */
		return 1;

	return r;
}

/*
 * Run through a list for as long as possible.  Returns the count
 * of successful jobs.
 */
M
Mikulas Patocka 已提交
418 419
static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
			int (*fn) (struct kcopyd_job *))
L
Linus Torvalds 已提交
420 421 422 423
{
	struct kcopyd_job *job;
	int r, count = 0;

M
Mikulas Patocka 已提交
424
	while ((job = pop(jobs, kc))) {
L
Linus Torvalds 已提交
425 426 427 428 429 430

		r = fn(job);

		if (r < 0) {
			/* error this rogue job */
			if (job->rw == WRITE)
431
				job->write_err = (unsigned long) -1L;
L
Linus Torvalds 已提交
432 433
			else
				job->read_err = 1;
M
Mikulas Patocka 已提交
434
			push(&kc->complete_jobs, job);
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442
			break;
		}

		if (r > 0) {
			/*
			 * We couldn't service this job ATM, so
			 * push this job back onto the list.
			 */
K
Kazuo Ito 已提交
443
			push_head(jobs, job);
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454 455
			break;
		}

		count++;
	}

	return count;
}

/*
 * kcopyd does this every time it's woken up.
 */
M
Mikulas Patocka 已提交
456
static void do_work(struct work_struct *work)
L
Linus Torvalds 已提交
457
{
M
Mikulas Patocka 已提交
458 459
	struct dm_kcopyd_client *kc = container_of(work,
					struct dm_kcopyd_client, kcopyd_work);
J
Jens Axboe 已提交
460
	struct blk_plug plug;
M
Mikulas Patocka 已提交
461

L
Linus Torvalds 已提交
462 463 464 465 466 467 468
	/*
	 * The order that these are called is *very* important.
	 * complete jobs can free some pages for pages jobs.
	 * Pages jobs when successful will jump onto the io jobs
	 * list.  io jobs call wake when they complete and it all
	 * starts again.
	 */
J
Jens Axboe 已提交
469
	blk_start_plug(&plug);
M
Mikulas Patocka 已提交
470 471 472
	process_jobs(&kc->complete_jobs, kc, run_complete_job);
	process_jobs(&kc->pages_jobs, kc, run_pages_job);
	process_jobs(&kc->io_jobs, kc, run_io_job);
J
Jens Axboe 已提交
473
	blk_finish_plug(&plug);
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482
}

/*
 * If we are copying a small region we just dispatch a single job
 * to do the copy, otherwise the io has to be split up into many
 * jobs.
 */
static void dispatch_job(struct kcopyd_job *job)
{
M
Mikulas Patocka 已提交
483 484
	struct dm_kcopyd_client *kc = job->kc;
	atomic_inc(&kc->nr_jobs);
M
Mikulas Patocka 已提交
485 486 487 488
	if (unlikely(!job->source.count))
		push(&kc->complete_jobs, job);
	else
		push(&kc->pages_jobs, job);
M
Mikulas Patocka 已提交
489
	wake(kc);
L
Linus Torvalds 已提交
490 491
}

492 493
static void segment_complete(int read_err, unsigned long write_err,
			     void *context)
L
Linus Torvalds 已提交
494 495 496 497
{
	/* FIXME: tidy this function */
	sector_t progress = 0;
	sector_t count = 0;
498 499
	struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
	struct kcopyd_job *job = sub_job->master_job;
500
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
501

502
	mutex_lock(&job->lock);
L
Linus Torvalds 已提交
503 504 505 506 507 508

	/* update the error */
	if (read_err)
		job->read_err = 1;

	if (write_err)
509
		job->write_err |= write_err;
L
Linus Torvalds 已提交
510 511 512 513 514

	/*
	 * Only dispatch more work if there hasn't been an error.
	 */
	if ((!job->read_err && !job->write_err) ||
H
Heinz Mauelshagen 已提交
515
	    test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525
		/* get the next chunk of work */
		progress = job->progress;
		count = job->source.count - progress;
		if (count) {
			if (count > SUB_JOB_SIZE)
				count = SUB_JOB_SIZE;

			job->progress += count;
		}
	}
526
	mutex_unlock(&job->lock);
L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540

	if (count) {
		int i;

		*sub_job = *job;
		sub_job->source.sector += progress;
		sub_job->source.count = count;

		for (i = 0; i < job->num_dests; i++) {
			sub_job->dests[i].sector += progress;
			sub_job->dests[i].count = count;
		}

		sub_job->fn = segment_complete;
541
		sub_job->context = sub_job;
L
Linus Torvalds 已提交
542 543 544 545 546
		dispatch_job(sub_job);

	} else if (atomic_dec_and_test(&job->sub_jobs)) {

		/*
M
Mikulas Patocka 已提交
547 548 549 550 551 552 553
		 * Queue the completion callback to the kcopyd thread.
		 *
		 * Some callers assume that all the completions are called
		 * from a single thread and don't race with each other.
		 *
		 * We must not call the callback directly here because this
		 * code may not be executing in the thread.
L
Linus Torvalds 已提交
554
		 */
M
Mikulas Patocka 已提交
555 556
		push(&kc->complete_jobs, job);
		wake(kc);
L
Linus Torvalds 已提交
557 558 559 560
	}
}

/*
561
 * Create some sub jobs to share the work between them.
L
Linus Torvalds 已提交
562
 */
563
static void split_job(struct kcopyd_job *master_job)
L
Linus Torvalds 已提交
564 565 566
{
	int i;

567
	atomic_inc(&master_job->kc->nr_jobs);
M
Mikulas Patocka 已提交
568

569 570 571 572 573
	atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
	for (i = 0; i < SPLIT_COUNT; i++) {
		master_job[i + 1].master_job = master_job;
		segment_complete(0, 0u, &master_job[i + 1]);
	}
L
Linus Torvalds 已提交
574 575
}

H
Heinz Mauelshagen 已提交
576 577 578
int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
		   unsigned int num_dests, struct dm_io_region *dests,
		   unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
L
Linus Torvalds 已提交
579 580 581 582
{
	struct kcopyd_job *job;

	/*
583 584
	 * Allocate an array of jobs consisting of one master job
	 * followed by SPLIT_COUNT sub jobs.
L
Linus Torvalds 已提交
585
	 */
M
Mikulas Patocka 已提交
586
	job = mempool_alloc(kc->job_pool, GFP_NOIO);
L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

	/*
	 * set up for the read.
	 */
	job->kc = kc;
	job->flags = flags;
	job->read_err = 0;
	job->write_err = 0;
	job->rw = READ;

	job->source = *from;

	job->num_dests = num_dests;
	memcpy(&job->dests, dests, sizeof(*dests) * num_dests);

	job->pages = NULL;

	job->fn = fn;
	job->context = context;
606
	job->master_job = job;
L
Linus Torvalds 已提交
607

608
	if (job->source.count <= SUB_JOB_SIZE)
L
Linus Torvalds 已提交
609 610
		dispatch_job(job);
	else {
611
		mutex_init(&job->lock);
L
Linus Torvalds 已提交
612 613 614 615 616 617
		job->progress = 0;
		split_job(job);
	}

	return 0;
}
H
Heinz Mauelshagen 已提交
618
EXPORT_SYMBOL(dm_kcopyd_copy);
L
Linus Torvalds 已提交
619

620 621 622 623 624 625 626 627 628 629 630
void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
				 dm_kcopyd_notify_fn fn, void *context)
{
	struct kcopyd_job *job;

	job = mempool_alloc(kc->job_pool, GFP_NOIO);

	memset(job, 0, sizeof(struct kcopyd_job));
	job->kc = kc;
	job->fn = fn;
	job->context = context;
A
Alasdair G Kergon 已提交
631
	job->master_job = job;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

	atomic_inc(&kc->nr_jobs);

	return job;
}
EXPORT_SYMBOL(dm_kcopyd_prepare_callback);

void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
{
	struct kcopyd_job *job = j;
	struct dm_kcopyd_client *kc = job->kc;

	job->read_err = read_err;
	job->write_err = write_err;

	push(&kc->complete_jobs, job);
	wake(kc);
}
EXPORT_SYMBOL(dm_kcopyd_do_callback);

L
Linus Torvalds 已提交
652 653 654 655
/*
 * Cancels a kcopyd job, eg. someone might be deactivating a
 * mirror.
 */
656
#if 0
L
Linus Torvalds 已提交
657 658 659 660 661
int kcopyd_cancel(struct kcopyd_job *job, int block)
{
	/* FIXME: finish */
	return -1;
}
662
#endif  /*  0  */
L
Linus Torvalds 已提交
663 664

/*-----------------------------------------------------------------
665
 * Client setup
L
Linus Torvalds 已提交
666
 *---------------------------------------------------------------*/
667
struct dm_kcopyd_client *dm_kcopyd_client_create(void)
L
Linus Torvalds 已提交
668
{
669
	int r = -ENOMEM;
H
Heinz Mauelshagen 已提交
670
	struct dm_kcopyd_client *kc;
L
Linus Torvalds 已提交
671 672

	kc = kmalloc(sizeof(*kc), GFP_KERNEL);
673
	if (!kc)
674
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
675

M
Mikulas Patocka 已提交
676 677 678 679 680
	spin_lock_init(&kc->job_lock);
	INIT_LIST_HEAD(&kc->complete_jobs);
	INIT_LIST_HEAD(&kc->io_jobs);
	INIT_LIST_HEAD(&kc->pages_jobs);

M
Mikulas Patocka 已提交
681
	kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
682 683
	if (!kc->job_pool)
		goto bad_slab;
M
Mikulas Patocka 已提交
684

M
Mikulas Patocka 已提交
685
	INIT_WORK(&kc->kcopyd_work, do_work);
686 687
	kc->kcopyd_wq = alloc_workqueue("kcopyd",
					WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
688 689
	if (!kc->kcopyd_wq)
		goto bad_workqueue;
M
Mikulas Patocka 已提交
690

L
Linus Torvalds 已提交
691
	kc->pages = NULL;
692
	kc->nr_reserved_pages = kc->nr_free_pages = 0;
M
Mikulas Patocka 已提交
693
	r = client_reserve_pages(kc, RESERVE_PAGES);
694 695
	if (r)
		goto bad_client_pages;
L
Linus Torvalds 已提交
696

697
	kc->io_client = dm_io_client_create();
M
Milan Broz 已提交
698 699
	if (IS_ERR(kc->io_client)) {
		r = PTR_ERR(kc->io_client);
700
		goto bad_io_client;
L
Linus Torvalds 已提交
701 702
	}

703 704 705
	init_waitqueue_head(&kc->destroyq);
	atomic_set(&kc->nr_jobs, 0);

706
	return kc;
707 708 709 710 711 712 713 714 715 716

bad_io_client:
	client_free_pages(kc);
bad_client_pages:
	destroy_workqueue(kc->kcopyd_wq);
bad_workqueue:
	mempool_destroy(kc->job_pool);
bad_slab:
	kfree(kc);

717
	return ERR_PTR(r);
L
Linus Torvalds 已提交
718
}
H
Heinz Mauelshagen 已提交
719
EXPORT_SYMBOL(dm_kcopyd_client_create);
L
Linus Torvalds 已提交
720

H
Heinz Mauelshagen 已提交
721
void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
L
Linus Torvalds 已提交
722
{
723 724 725
	/* Wait for completion of all jobs submitted by this client. */
	wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));

M
Mikulas Patocka 已提交
726 727 728 729
	BUG_ON(!list_empty(&kc->complete_jobs));
	BUG_ON(!list_empty(&kc->io_jobs));
	BUG_ON(!list_empty(&kc->pages_jobs));
	destroy_workqueue(kc->kcopyd_wq);
M
Milan Broz 已提交
730
	dm_io_client_destroy(kc->io_client);
L
Linus Torvalds 已提交
731
	client_free_pages(kc);
M
Mikulas Patocka 已提交
732
	mempool_destroy(kc->job_pool);
L
Linus Torvalds 已提交
733 734
	kfree(kc);
}
H
Heinz Mauelshagen 已提交
735
EXPORT_SYMBOL(dm_kcopyd_client_destroy);