dm-kcopyd.c 20.5 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/delay.h>
26
#include <linux/device-mapper.h>
A
Alasdair G Kergon 已提交
27
#include <linux/dm-kcopyd.h>
L
Linus Torvalds 已提交
28

29
#include "dm-core.h"
L
Linus Torvalds 已提交
30

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

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

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

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

50
	mempool_t job_pool;
M
Mikulas Patocka 已提交
51

M
Mikulas Patocka 已提交
52 53 54
	struct workqueue_struct *kcopyd_wq;
	struct work_struct kcopyd_work;

55 56
	struct dm_kcopyd_throttle *throttle;

M
Mikulas Patocka 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * 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 已提交
70 71
};

72 73
static struct page_list zero_page_list;

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static DEFINE_SPINLOCK(throttle_spinlock);

/*
 * IO/IDLE accounting slowly decays after (1 << ACCOUNT_INTERVAL_SHIFT) period.
 * When total_period >= (1 << ACCOUNT_INTERVAL_SHIFT) the counters are divided
 * by 2.
 */
#define ACCOUNT_INTERVAL_SHIFT		SHIFT_HZ

/*
 * Sleep this number of milliseconds.
 *
 * The value was decided experimentally.
 * Smaller values seem to cause an increased copy rate above the limit.
 * The reason for this is unknown but possibly due to jiffies rounding errors
 * or read/write cache inside the disk.
 */
#define SLEEP_MSEC			100

/*
 * Maximum number of sleep events. There is a theoretical livelock if more
 * kcopyd clients do work simultaneously which this limit avoids.
 */
#define MAX_SLEEPS			10

static void io_job_start(struct dm_kcopyd_throttle *t)
{
	unsigned throttle, now, difference;
	int slept = 0, skew;

	if (unlikely(!t))
		return;

try_again:
	spin_lock_irq(&throttle_spinlock);

110
	throttle = READ_ONCE(t->throttle);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	if (likely(throttle >= 100))
		goto skip_limit;

	now = jiffies;
	difference = now - t->last_jiffies;
	t->last_jiffies = now;
	if (t->num_io_jobs)
		t->io_period += difference;
	t->total_period += difference;

	/*
	 * Maintain sane values if we got a temporary overflow.
	 */
	if (unlikely(t->io_period > t->total_period))
		t->io_period = t->total_period;

	if (unlikely(t->total_period >= (1 << ACCOUNT_INTERVAL_SHIFT))) {
		int shift = fls(t->total_period >> ACCOUNT_INTERVAL_SHIFT);
		t->total_period >>= shift;
		t->io_period >>= shift;
	}

	skew = t->io_period - throttle * t->total_period / 100;

	if (unlikely(skew > 0) && slept < MAX_SLEEPS) {
		slept++;
		spin_unlock_irq(&throttle_spinlock);
		msleep(SLEEP_MSEC);
		goto try_again;
	}

skip_limit:
	t->num_io_jobs++;

	spin_unlock_irq(&throttle_spinlock);
}

static void io_job_finish(struct dm_kcopyd_throttle *t)
{
	unsigned long flags;

	if (unlikely(!t))
		return;

	spin_lock_irqsave(&throttle_spinlock, flags);

	t->num_io_jobs--;

160
	if (likely(READ_ONCE(t->throttle) >= 100))
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		goto skip_limit;

	if (!t->num_io_jobs) {
		unsigned now, difference;

		now = jiffies;
		difference = now - t->last_jiffies;
		t->last_jiffies = now;

		t->io_period += difference;
		t->total_period += difference;

		/*
		 * Maintain sane values if we got a temporary overflow.
		 */
		if (unlikely(t->io_period > t->total_period))
			t->io_period = t->total_period;
	}

skip_limit:
	spin_unlock_irqrestore(&throttle_spinlock, flags);
}


M
Mikulas Patocka 已提交
185 186 187 188 189
static void wake(struct dm_kcopyd_client *kc)
{
	queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
}

190 191 192
/*
 * Obtain one page for the use of kcopyd.
 */
193
static struct page_list *alloc_pl(gfp_t gfp)
L
Linus Torvalds 已提交
194 195 196
{
	struct page_list *pl;

197
	pl = kmalloc(sizeof(*pl), gfp);
L
Linus Torvalds 已提交
198 199 200
	if (!pl)
		return NULL;

201
	pl->page = alloc_page(gfp);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
	if (!pl->page) {
		kfree(pl);
		return NULL;
	}

	return pl;
}

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

216 217 218 219 220
/*
 * 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 已提交
221
{
222
	struct page_list *next;
L
Linus Torvalds 已提交
223

224 225
	do {
		next = pl->next;
L
Linus Torvalds 已提交
226

227 228 229 230 231 232 233
		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 已提交
234

235 236
		pl = next;
	} while (pl);
L
Linus Torvalds 已提交
237 238
}

239 240
static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
			    unsigned int nr, struct page_list **pages)
L
Linus Torvalds 已提交
241
{
242
	struct page_list *pl;
L
Linus Torvalds 已提交
243

244 245 246
	*pages = NULL;

	do {
247
		pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY | __GFP_KSWAPD_RECLAIM);
248 249 250 251 252 253 254 255 256 257 258 259 260
		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 已提交
261

262 263 264 265
out_of_memory:
	if (*pages)
		kcopyd_put_pages(kc, *pages);
	return -ENOMEM;
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

/*
 * 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;
	}
}

282 283 284 285
/*
 * 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 已提交
286
{
287
	unsigned i;
L
Linus Torvalds 已提交
288 289
	struct page_list *pl = NULL, *next;

290
	for (i = 0; i < nr_pages; i++) {
291
		next = alloc_pl(GFP_KERNEL);
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300
		if (!next) {
			if (pl)
				drop_pages(pl);
			return -ENOMEM;
		}
		next->next = pl;
		pl = next;
	}

301
	kc->nr_reserved_pages += nr_pages;
L
Linus Torvalds 已提交
302
	kcopyd_put_pages(kc, pl);
303

L
Linus Torvalds 已提交
304 305 306
	return 0;
}

H
Heinz Mauelshagen 已提交
307
static void client_free_pages(struct dm_kcopyd_client *kc)
L
Linus Torvalds 已提交
308
{
309
	BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
L
Linus Torvalds 已提交
310 311
	drop_pages(kc->pages);
	kc->pages = NULL;
312
	kc->nr_free_pages = kc->nr_reserved_pages = 0;
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320
}

/*-----------------------------------------------------------------
 * 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 已提交
321
	struct dm_kcopyd_client *kc;
L
Linus Torvalds 已提交
322 323 324 325 326 327 328
	struct list_head list;
	unsigned long flags;

	/*
	 * Error state of the job.
	 */
	int read_err;
329
	unsigned long write_err;
L
Linus Torvalds 已提交
330 331 332 333 334

	/*
	 * Either READ or WRITE
	 */
	int rw;
H
Heinz Mauelshagen 已提交
335
	struct dm_io_region source;
L
Linus Torvalds 已提交
336 337 338 339 340

	/*
	 * The destinations for the transfer.
	 */
	unsigned int num_dests;
H
Heinz Mauelshagen 已提交
341
	struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
L
Linus Torvalds 已提交
342 343 344 345 346 347 348

	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 已提交
349
	dm_kcopyd_notify_fn fn;
L
Linus Torvalds 已提交
350 351 352 353 354 355
	void *context;

	/*
	 * These fields are only used if the job has been split
	 * into more manageable parts.
	 */
356
	struct mutex lock;
L
Linus Torvalds 已提交
357 358
	atomic_t sub_jobs;
	sector_t progress;
359
	sector_t write_offset;
L
Linus Torvalds 已提交
360

361 362
	struct kcopyd_job *master_job;
};
L
Linus Torvalds 已提交
363

364
static struct kmem_cache *_job_cache;
L
Linus Torvalds 已提交
365

366
int __init dm_kcopyd_init(void)
L
Linus Torvalds 已提交
367
{
368 369 370
	_job_cache = kmem_cache_create("kcopyd_job",
				sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
				__alignof__(struct kcopyd_job), 0, NULL);
L
Linus Torvalds 已提交
371 372 373
	if (!_job_cache)
		return -ENOMEM;

374 375 376
	zero_page_list.next = &zero_page_list;
	zero_page_list.page = ZERO_PAGE(0);

L
Linus Torvalds 已提交
377 378 379
	return 0;
}

380
void dm_kcopyd_exit(void)
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389
{
	kmem_cache_destroy(_job_cache);
	_job_cache = NULL;
}

/*
 * Functions to push and pop a job onto the head of a given job
 * list.
 */
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static struct kcopyd_job *pop_io_job(struct list_head *jobs,
				     struct dm_kcopyd_client *kc)
{
	struct kcopyd_job *job;

	/*
	 * For I/O jobs, pop any read, any write without sequential write
	 * constraint and sequential writes that are at the right position.
	 */
	list_for_each_entry(job, jobs, list) {
		if (job->rw == READ || !test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags)) {
			list_del(&job->list);
			return job;
		}

		if (job->write_offset == job->master_job->write_offset) {
			job->master_job->write_offset += job->source.count;
			list_del(&job->list);
			return job;
		}
	}

	return NULL;
}

M
Mikulas Patocka 已提交
415 416
static struct kcopyd_job *pop(struct list_head *jobs,
			      struct dm_kcopyd_client *kc)
L
Linus Torvalds 已提交
417 418 419 420
{
	struct kcopyd_job *job = NULL;
	unsigned long flags;

M
Mikulas Patocka 已提交
421
	spin_lock_irqsave(&kc->job_lock, flags);
L
Linus Torvalds 已提交
422 423

	if (!list_empty(jobs)) {
424 425 426 427 428 429
		if (jobs == &kc->io_jobs)
			job = pop_io_job(jobs, kc);
		else {
			job = list_entry(jobs->next, struct kcopyd_job, list);
			list_del(&job->list);
		}
L
Linus Torvalds 已提交
430
	}
M
Mikulas Patocka 已提交
431
	spin_unlock_irqrestore(&kc->job_lock, flags);
L
Linus Torvalds 已提交
432 433 434 435

	return job;
}

A
Alasdair G Kergon 已提交
436
static void push(struct list_head *jobs, struct kcopyd_job *job)
L
Linus Torvalds 已提交
437 438
{
	unsigned long flags;
M
Mikulas Patocka 已提交
439
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
440

M
Mikulas Patocka 已提交
441
	spin_lock_irqsave(&kc->job_lock, flags);
L
Linus Torvalds 已提交
442
	list_add_tail(&job->list, jobs);
M
Mikulas Patocka 已提交
443
	spin_unlock_irqrestore(&kc->job_lock, flags);
L
Linus Torvalds 已提交
444 445
}

K
Kazuo Ito 已提交
446 447 448 449 450 451 452 453 454 455 456

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 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469
/*
 * 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;
470
	unsigned long write_err = job->write_err;
H
Heinz Mauelshagen 已提交
471 472
	dm_kcopyd_notify_fn fn = job->fn;
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
473

474
	if (job->pages && job->pages != &zero_page_list)
475
		kcopyd_put_pages(kc, job->pages);
476 477 478 479
	/*
	 * If this is the master job, the sub jobs have already
	 * completed so we can free everything.
	 */
480 481
	if (job->master_job == job) {
		mutex_destroy(&job->lock);
482
		mempool_free(job, &kc->job_pool);
483
	}
L
Linus Torvalds 已提交
484
	fn(read_err, write_err, context);
485 486 487 488

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

L
Linus Torvalds 已提交
489 490 491 492 493 494
	return 0;
}

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

497 498
	io_job_finish(kc->throttle);

L
Linus Torvalds 已提交
499
	if (error) {
500
		if (op_is_write(job->rw))
501
			job->write_err |= error;
L
Linus Torvalds 已提交
502 503 504
		else
			job->read_err = 1;

H
Heinz Mauelshagen 已提交
505
		if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
M
Mikulas Patocka 已提交
506 507
			push(&kc->complete_jobs, job);
			wake(kc);
L
Linus Torvalds 已提交
508 509 510 511
			return;
		}
	}

512
	if (op_is_write(job->rw))
M
Mikulas Patocka 已提交
513
		push(&kc->complete_jobs, job);
L
Linus Torvalds 已提交
514 515 516

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

M
Mikulas Patocka 已提交
520
	wake(kc);
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529
}

/*
 * 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 已提交
530
	struct dm_io_request io_req = {
M
Mike Christie 已提交
531 532
		.bi_op = job->rw,
		.bi_op_flags = 0,
M
Milan Broz 已提交
533 534
		.mem.type = DM_IO_PAGE_LIST,
		.mem.ptr.pl = job->pages,
535
		.mem.offset = 0,
M
Milan Broz 已提交
536 537 538 539
		.notify.fn = complete_io,
		.notify.context = job,
		.client = job->kc->io_client,
	};
L
Linus Torvalds 已提交
540

541 542 543 544 545 546 547 548
	/*
	 * If we need to write sequentially and some reads or writes failed,
	 * no point in continuing.
	 */
	if (test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags) &&
	    job->master_job->write_err)
		return -EIO;

549 550
	io_job_start(job->kc->throttle);

J
Jens Axboe 已提交
551
	if (job->rw == READ)
M
Milan Broz 已提交
552
		r = dm_io(&io_req, 1, &job->source, NULL);
J
Jens Axboe 已提交
553
	else
M
Milan Broz 已提交
554
		r = dm_io(&io_req, job->num_dests, job->dests, NULL);
L
Linus Torvalds 已提交
555 556 557 558 559 560 561

	return r;
}

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

564
	r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
L
Linus Torvalds 已提交
565 566
	if (!r) {
		/* this job is ready for io */
M
Mikulas Patocka 已提交
567
		push(&job->kc->io_jobs, job);
L
Linus Torvalds 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581
		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 已提交
582 583
static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
			int (*fn) (struct kcopyd_job *))
L
Linus Torvalds 已提交
584 585 586 587
{
	struct kcopyd_job *job;
	int r, count = 0;

M
Mikulas Patocka 已提交
588
	while ((job = pop(jobs, kc))) {
L
Linus Torvalds 已提交
589 590 591 592 593

		r = fn(job);

		if (r < 0) {
			/* error this rogue job */
594
			if (op_is_write(job->rw))
595
				job->write_err = (unsigned long) -1L;
L
Linus Torvalds 已提交
596 597
			else
				job->read_err = 1;
M
Mikulas Patocka 已提交
598
			push(&kc->complete_jobs, job);
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606
			break;
		}

		if (r > 0) {
			/*
			 * We couldn't service this job ATM, so
			 * push this job back onto the list.
			 */
K
Kazuo Ito 已提交
607
			push_head(jobs, job);
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616 617 618 619
			break;
		}

		count++;
	}

	return count;
}

/*
 * kcopyd does this every time it's woken up.
 */
M
Mikulas Patocka 已提交
620
static void do_work(struct work_struct *work)
L
Linus Torvalds 已提交
621
{
M
Mikulas Patocka 已提交
622 623
	struct dm_kcopyd_client *kc = container_of(work,
					struct dm_kcopyd_client, kcopyd_work);
J
Jens Axboe 已提交
624
	struct blk_plug plug;
M
Mikulas Patocka 已提交
625

L
Linus Torvalds 已提交
626 627 628 629 630 631 632
	/*
	 * 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 已提交
633
	blk_start_plug(&plug);
M
Mikulas Patocka 已提交
634 635 636
	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 已提交
637
	blk_finish_plug(&plug);
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646
}

/*
 * 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 已提交
647 648
	struct dm_kcopyd_client *kc = job->kc;
	atomic_inc(&kc->nr_jobs);
M
Mikulas Patocka 已提交
649 650
	if (unlikely(!job->source.count))
		push(&kc->complete_jobs, job);
651 652
	else if (job->pages == &zero_page_list)
		push(&kc->io_jobs, job);
M
Mikulas Patocka 已提交
653 654
	else
		push(&kc->pages_jobs, job);
M
Mikulas Patocka 已提交
655
	wake(kc);
L
Linus Torvalds 已提交
656 657
}

658 659
static void segment_complete(int read_err, unsigned long write_err,
			     void *context)
L
Linus Torvalds 已提交
660 661 662 663
{
	/* FIXME: tidy this function */
	sector_t progress = 0;
	sector_t count = 0;
664 665
	struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
	struct kcopyd_job *job = sub_job->master_job;
666
	struct dm_kcopyd_client *kc = job->kc;
L
Linus Torvalds 已提交
667

668
	mutex_lock(&job->lock);
L
Linus Torvalds 已提交
669 670 671 672 673 674

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

	if (write_err)
675
		job->write_err |= write_err;
L
Linus Torvalds 已提交
676 677 678 679 680

	/*
	 * Only dispatch more work if there hasn't been an error.
	 */
	if ((!job->read_err && !job->write_err) ||
H
Heinz Mauelshagen 已提交
681
	    test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689 690 691
		/* 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;
		}
	}
692
	mutex_unlock(&job->lock);
L
Linus Torvalds 已提交
693 694 695 696 697

	if (count) {
		int i;

		*sub_job = *job;
698
		sub_job->write_offset = progress;
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706 707
		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;
708
		sub_job->context = sub_job;
L
Linus Torvalds 已提交
709 710 711 712 713
		dispatch_job(sub_job);

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

		/*
M
Mikulas Patocka 已提交
714 715 716 717 718 719 720
		 * 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 已提交
721
		 */
M
Mikulas Patocka 已提交
722 723
		push(&kc->complete_jobs, job);
		wake(kc);
L
Linus Torvalds 已提交
724 725 726 727
	}
}

/*
728
 * Create some sub jobs to share the work between them.
L
Linus Torvalds 已提交
729
 */
730
static void split_job(struct kcopyd_job *master_job)
L
Linus Torvalds 已提交
731 732 733
{
	int i;

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

736 737 738 739 740
	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 已提交
741 742
}

H
Heinz Mauelshagen 已提交
743 744 745
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 已提交
746 747
{
	struct kcopyd_job *job;
748
	int i;
L
Linus Torvalds 已提交
749 750

	/*
751 752
	 * Allocate an array of jobs consisting of one master job
	 * followed by SPLIT_COUNT sub jobs.
L
Linus Torvalds 已提交
753
	 */
754
	job = mempool_alloc(&kc->job_pool, GFP_NOIO);
755
	mutex_init(&job->lock);
L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765 766 767

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

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

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
	/*
	 * If one of the destination is a host-managed zoned block device,
	 * we need to write sequentially. If one of the destination is a
	 * host-aware device, then leave it to the caller to choose what to do.
	 */
	if (!test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags)) {
		for (i = 0; i < job->num_dests; i++) {
			if (bdev_zoned_model(dests[i].bdev) == BLK_ZONED_HM) {
				set_bit(DM_KCOPYD_WRITE_SEQ, &job->flags);
				break;
			}
		}
	}

	/*
	 * If we need to write sequentially, errors cannot be ignored.
	 */
	if (test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags) &&
	    test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags))
		clear_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags);

789 790 791 792 793 794 795 796
	if (from) {
		job->source = *from;
		job->pages = NULL;
		job->rw = READ;
	} else {
		memset(&job->source, 0, sizeof job->source);
		job->source.count = job->dests[0].count;
		job->pages = &zero_page_list;
797 798

		/*
799
		 * Use WRITE ZEROES to optimize zeroing if all dests support it.
800
		 */
801
		job->rw = REQ_OP_WRITE_ZEROES;
802
		for (i = 0; i < job->num_dests; i++)
803
			if (!bdev_write_zeroes_sectors(job->dests[i].bdev)) {
804 805 806
				job->rw = WRITE;
				break;
			}
807
	}
L
Linus Torvalds 已提交
808 809 810

	job->fn = fn;
	job->context = context;
811
	job->master_job = job;
812
	job->write_offset = 0;
L
Linus Torvalds 已提交
813

814
	if (job->source.count <= SUB_JOB_SIZE)
L
Linus Torvalds 已提交
815 816 817 818 819 820 821 822
		dispatch_job(job);
	else {
		job->progress = 0;
		split_job(job);
	}

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

825 826 827 828 829 830 831 832
int dm_kcopyd_zero(struct dm_kcopyd_client *kc,
		   unsigned num_dests, struct dm_io_region *dests,
		   unsigned flags, dm_kcopyd_notify_fn fn, void *context)
{
	return dm_kcopyd_copy(kc, NULL, num_dests, dests, flags, fn, context);
}
EXPORT_SYMBOL(dm_kcopyd_zero);

833 834 835 836 837
void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
				 dm_kcopyd_notify_fn fn, void *context)
{
	struct kcopyd_job *job;

838
	job = mempool_alloc(&kc->job_pool, GFP_NOIO);
839 840 841 842 843

	memset(job, 0, sizeof(struct kcopyd_job));
	job->kc = kc;
	job->fn = fn;
	job->context = context;
A
Alasdair G Kergon 已提交
844
	job->master_job = job;
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

	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 已提交
865 866 867 868
/*
 * Cancels a kcopyd job, eg. someone might be deactivating a
 * mirror.
 */
869
#if 0
L
Linus Torvalds 已提交
870 871 872 873 874
int kcopyd_cancel(struct kcopyd_job *job, int block)
{
	/* FIXME: finish */
	return -1;
}
875
#endif  /*  0  */
L
Linus Torvalds 已提交
876 877

/*-----------------------------------------------------------------
878
 * Client setup
L
Linus Torvalds 已提交
879
 *---------------------------------------------------------------*/
880
struct dm_kcopyd_client *dm_kcopyd_client_create(struct dm_kcopyd_throttle *throttle)
L
Linus Torvalds 已提交
881
{
882
	int r;
H
Heinz Mauelshagen 已提交
883
	struct dm_kcopyd_client *kc;
L
Linus Torvalds 已提交
884

885
	kc = kzalloc(sizeof(*kc), GFP_KERNEL);
886
	if (!kc)
887
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
888

M
Mikulas Patocka 已提交
889 890 891 892
	spin_lock_init(&kc->job_lock);
	INIT_LIST_HEAD(&kc->complete_jobs);
	INIT_LIST_HEAD(&kc->io_jobs);
	INIT_LIST_HEAD(&kc->pages_jobs);
893
	kc->throttle = throttle;
M
Mikulas Patocka 已提交
894

895 896
	r = mempool_init_slab_pool(&kc->job_pool, MIN_JOBS, _job_cache);
	if (r)
897
		goto bad_slab;
M
Mikulas Patocka 已提交
898

M
Mikulas Patocka 已提交
899
	INIT_WORK(&kc->kcopyd_work, do_work);
T
Tejun Heo 已提交
900
	kc->kcopyd_wq = alloc_workqueue("kcopyd", WQ_MEM_RECLAIM, 0);
901 902
	if (!kc->kcopyd_wq) {
		r = -ENOMEM;
903
		goto bad_workqueue;
904
	}
M
Mikulas Patocka 已提交
905

L
Linus Torvalds 已提交
906
	kc->pages = NULL;
907
	kc->nr_reserved_pages = kc->nr_free_pages = 0;
M
Mikulas Patocka 已提交
908
	r = client_reserve_pages(kc, RESERVE_PAGES);
909 910
	if (r)
		goto bad_client_pages;
L
Linus Torvalds 已提交
911

912
	kc->io_client = dm_io_client_create();
M
Milan Broz 已提交
913 914
	if (IS_ERR(kc->io_client)) {
		r = PTR_ERR(kc->io_client);
915
		goto bad_io_client;
L
Linus Torvalds 已提交
916 917
	}

918 919 920
	init_waitqueue_head(&kc->destroyq);
	atomic_set(&kc->nr_jobs, 0);

921
	return kc;
922 923 924 925 926 927

bad_io_client:
	client_free_pages(kc);
bad_client_pages:
	destroy_workqueue(kc->kcopyd_wq);
bad_workqueue:
928
	mempool_exit(&kc->job_pool);
929 930 931
bad_slab:
	kfree(kc);

932
	return ERR_PTR(r);
L
Linus Torvalds 已提交
933
}
H
Heinz Mauelshagen 已提交
934
EXPORT_SYMBOL(dm_kcopyd_client_create);
L
Linus Torvalds 已提交
935

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

M
Mikulas Patocka 已提交
941 942 943 944
	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 已提交
945
	dm_io_client_destroy(kc->io_client);
L
Linus Torvalds 已提交
946
	client_free_pages(kc);
947
	mempool_exit(&kc->job_pool);
L
Linus Torvalds 已提交
948 949
	kfree(kc);
}
H
Heinz Mauelshagen 已提交
950
EXPORT_SYMBOL(dm_kcopyd_client_destroy);