dm-raid1.c 33.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (C) 2003 Sistina Software Limited.
3
 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
L
Linus Torvalds 已提交
4 5 6 7
 *
 * This file is released under the GPL.
 */

8
#include "dm-bio-record.h"
L
Linus Torvalds 已提交
9 10 11 12 13 14 15

#include <linux/init.h>
#include <linux/mempool.h>
#include <linux/module.h>
#include <linux/pagemap.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
16
#include <linux/device-mapper.h>
A
Alasdair G Kergon 已提交
17 18 19
#include <linux/dm-io.h>
#include <linux/dm-dirty-log.h>
#include <linux/dm-kcopyd.h>
20
#include <linux/dm-region-hash.h>
L
Linus Torvalds 已提交
21

22
#define DM_MSG_PREFIX "raid1"
23 24

#define MAX_RECOVERY 1	/* Maximum number of regions recovered in parallel. */
M
Milan Broz 已提交
25
#define DM_IO_PAGES 64
26
#define DM_KCOPYD_PAGES 64
27

28
#define DM_RAID1_HANDLE_ERRORS 0x01
29
#define errors_handled(p)	((p)->features & DM_RAID1_HANDLE_ERRORS)
30

31
static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
L
Linus Torvalds 已提交
32

33 34 35
/*-----------------------------------------------------------------
 * Mirror set structures.
 *---------------------------------------------------------------*/
36 37
enum dm_raid1_error {
	DM_RAID1_WRITE_ERROR,
38
	DM_RAID1_FLUSH_ERROR,
39 40 41 42
	DM_RAID1_SYNC_ERROR,
	DM_RAID1_READ_ERROR
};

43
struct mirror {
44
	struct mirror_set *ms;
45
	atomic_t error_count;
A
Al Viro 已提交
46
	unsigned long error_type;
47 48 49 50 51 52 53
	struct dm_dev *dev;
	sector_t offset;
};

struct mirror_set {
	struct dm_target *ti;
	struct list_head list;
54

55
	uint64_t features;
56

57
	spinlock_t lock;	/* protects the lists */
58 59
	struct bio_list reads;
	struct bio_list writes;
60
	struct bio_list failures;
61
	struct bio_list holds;	/* bios are waiting until suspend */
62

63 64
	struct dm_region_hash *rh;
	struct dm_kcopyd_client *kcopyd_client;
M
Milan Broz 已提交
65
	struct dm_io_client *io_client;
66
	mempool_t *read_record_pool;
M
Milan Broz 已提交
67

68 69 70
	/* recovery */
	region_t nr_regions;
	int in_sync;
J
Jonathan Brassow 已提交
71
	int log_failure;
72
	int leg_failure;
73
	atomic_t suspend;
74

75
	atomic_t default_mirror;	/* Default mirror */
76

77 78
	struct workqueue_struct *kmirrord_wq;
	struct work_struct kmirrord_work;
M
Mikulas Patocka 已提交
79 80 81
	struct timer_list timer;
	unsigned long timer_pending;

82
	struct work_struct trigger_event;
83

84
	unsigned nr_mirrors;
85 86 87
	struct mirror mirror[0];
};

88
static void wakeup_mirrord(void *context)
L
Linus Torvalds 已提交
89
{
90
	struct mirror_set *ms = context;
L
Linus Torvalds 已提交
91

92 93 94
	queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
}

M
Mikulas Patocka 已提交
95 96 97 98 99
static void delayed_wake_fn(unsigned long data)
{
	struct mirror_set *ms = (struct mirror_set *) data;

	clear_bit(0, &ms->timer_pending);
100
	wakeup_mirrord(ms);
M
Mikulas Patocka 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
}

static void delayed_wake(struct mirror_set *ms)
{
	if (test_and_set_bit(0, &ms->timer_pending))
		return;

	ms->timer.expires = jiffies + HZ / 5;
	ms->timer.data = (unsigned long) ms;
	ms->timer.function = delayed_wake_fn;
	add_timer(&ms->timer);
}

114
static void wakeup_all_recovery_waiters(void *context)
L
Linus Torvalds 已提交
115
{
116
	wake_up_all(&_kmirrord_recovery_stopped);
L
Linus Torvalds 已提交
117 118
}

119
static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
L
Linus Torvalds 已提交
120 121 122
{
	unsigned long flags;
	int should_wake = 0;
123
	struct bio_list *bl;
L
Linus Torvalds 已提交
124

125 126 127 128 129
	bl = (rw == WRITE) ? &ms->writes : &ms->reads;
	spin_lock_irqsave(&ms->lock, flags);
	should_wake = !(bl->head);
	bio_list_add(bl, bio);
	spin_unlock_irqrestore(&ms->lock, flags);
L
Linus Torvalds 已提交
130 131

	if (should_wake)
132
		wakeup_mirrord(ms);
L
Linus Torvalds 已提交
133 134
}

135
static void dispatch_bios(void *context, struct bio_list *bio_list)
L
Linus Torvalds 已提交
136
{
137 138
	struct mirror_set *ms = context;
	struct bio *bio;
L
Linus Torvalds 已提交
139

140 141
	while ((bio = bio_list_pop(bio_list)))
		queue_bio(ms, bio, WRITE);
L
Linus Torvalds 已提交
142 143
}

144 145 146 147 148 149
#define MIN_READ_RECORDS 20
struct dm_raid1_read_record {
	struct mirror *m;
	struct dm_bio_details details;
};

150 151
static struct kmem_cache *_dm_raid1_read_record_cache;

L
Linus Torvalds 已提交
152 153 154 155 156 157
/*
 * Every mirror should look like this one.
 */
#define DEFAULT_MIRROR 0

/*
158 159
 * This is yucky.  We squirrel the mirror struct away inside
 * bi_next for read/write buffers.  This is safe since the bh
L
Linus Torvalds 已提交
160 161
 * doesn't get submitted to the lower levels of block layer.
 */
162
static struct mirror *bio_get_m(struct bio *bio)
L
Linus Torvalds 已提交
163
{
164
	return (struct mirror *) bio->bi_next;
L
Linus Torvalds 已提交
165 166
}

167
static void bio_set_m(struct bio *bio, struct mirror *m)
L
Linus Torvalds 已提交
168
{
169
	bio->bi_next = (struct bio *) m;
L
Linus Torvalds 已提交
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184
static struct mirror *get_default_mirror(struct mirror_set *ms)
{
	return &ms->mirror[atomic_read(&ms->default_mirror)];
}

static void set_default_mirror(struct mirror *m)
{
	struct mirror_set *ms = m->ms;
	struct mirror *m0 = &(ms->mirror[0]);

	atomic_set(&ms->default_mirror, m - m0);
}

185 186 187 188 189 190 191 192 193 194 195
static struct mirror *get_valid_mirror(struct mirror_set *ms)
{
	struct mirror *m;

	for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
		if (!atomic_read(&m->error_count))
			return m;

	return NULL;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/* fail_mirror
 * @m: mirror device to fail
 * @error_type: one of the enum's, DM_RAID1_*_ERROR
 *
 * If errors are being handled, record the type of
 * error encountered for this device.  If this type
 * of error has already been recorded, we can return;
 * otherwise, we must signal userspace by triggering
 * an event.  Additionally, if the device is the
 * primary device, we must choose a new primary, but
 * only if the mirror is in-sync.
 *
 * This function must not block.
 */
static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
{
	struct mirror_set *ms = m->ms;
	struct mirror *new;

215 216
	ms->leg_failure = 1;

217 218 219 220 221 222 223 224 225 226
	/*
	 * error_count is used for nothing more than a
	 * simple way to tell if a device has encountered
	 * errors.
	 */
	atomic_inc(&m->error_count);

	if (test_and_set_bit(error_type, &m->error_type))
		return;

J
Jonathan Brassow 已提交
227 228 229
	if (!errors_handled(ms))
		return;

230 231 232 233 234 235 236 237 238 239 240 241 242
	if (m != get_default_mirror(ms))
		goto out;

	if (!ms->in_sync) {
		/*
		 * Better to issue requests to same failing device
		 * than to risk returning corrupt data.
		 */
		DMERR("Primary mirror (%s) failed while out-of-sync: "
		      "Reads may fail.", m->dev->name);
		goto out;
	}

243 244 245 246
	new = get_valid_mirror(ms);
	if (new)
		set_default_mirror(new);
	else
247 248 249 250 251 252
		DMWARN("All sides of mirror have failed.");

out:
	schedule_work(&ms->trigger_event);
}

M
Mikulas Patocka 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static int mirror_flush(struct dm_target *ti)
{
	struct mirror_set *ms = ti->private;
	unsigned long error_bits;

	unsigned int i;
	struct dm_io_region io[ms->nr_mirrors];
	struct mirror *m;
	struct dm_io_request io_req = {
		.bi_rw = WRITE_BARRIER,
		.mem.type = DM_IO_KMEM,
		.mem.ptr.bvec = NULL,
		.client = ms->io_client,
	};

	for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
		io[i].bdev = m->dev->bdev;
		io[i].sector = 0;
		io[i].count = 0;
	}

	error_bits = -1;
	dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
	if (unlikely(error_bits != 0)) {
		for (i = 0; i < ms->nr_mirrors; i++)
			if (test_bit(i, &error_bits))
				fail_mirror(ms->mirror + i,
280
					    DM_RAID1_FLUSH_ERROR);
M
Mikulas Patocka 已提交
281 282 283 284 285 286
		return -EIO;
	}

	return 0;
}

L
Linus Torvalds 已提交
287 288 289 290 291 292 293
/*-----------------------------------------------------------------
 * Recovery.
 *
 * When a mirror is first activated we may find that some regions
 * are in the no-sync state.  We have to recover these by
 * recopying from the default mirror to all the others.
 *---------------------------------------------------------------*/
294
static void recovery_complete(int read_err, unsigned long write_err,
L
Linus Torvalds 已提交
295 296
			      void *context)
{
297 298
	struct dm_region *reg = context;
	struct mirror_set *ms = dm_rh_region_context(reg);
299
	int m, bit = 0;
L
Linus Torvalds 已提交
300

301
	if (read_err) {
302 303
		/* Read error means the failure of default mirror. */
		DMERR_LIMIT("Unable to read primary mirror during recovery");
304 305
		fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
	}
306

307
	if (write_err) {
308
		DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
309
			    write_err);
310 311 312 313 314 315 316 317 318 319 320 321 322
		/*
		 * Bits correspond to devices (excluding default mirror).
		 * The default mirror cannot change during recovery.
		 */
		for (m = 0; m < ms->nr_mirrors; m++) {
			if (&ms->mirror[m] == get_default_mirror(ms))
				continue;
			if (test_bit(bit, &write_err))
				fail_mirror(ms->mirror + m,
					    DM_RAID1_SYNC_ERROR);
			bit++;
		}
	}
323

324
	dm_rh_recovery_end(reg, !(read_err || write_err));
L
Linus Torvalds 已提交
325 326
}

327
static int recover(struct mirror_set *ms, struct dm_region *reg)
L
Linus Torvalds 已提交
328 329
{
	int r;
330
	unsigned i;
H
Heinz Mauelshagen 已提交
331
	struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
L
Linus Torvalds 已提交
332 333
	struct mirror *m;
	unsigned long flags = 0;
334 335
	region_t key = dm_rh_get_region_key(reg);
	sector_t region_size = dm_rh_get_region_size(ms->rh);
L
Linus Torvalds 已提交
336 337

	/* fill in the source */
338
	m = get_default_mirror(ms);
L
Linus Torvalds 已提交
339
	from.bdev = m->dev->bdev;
340 341
	from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
	if (key == (ms->nr_regions - 1)) {
L
Linus Torvalds 已提交
342 343 344 345
		/*
		 * The final region may be smaller than
		 * region_size.
		 */
346
		from.count = ms->ti->len & (region_size - 1);
L
Linus Torvalds 已提交
347
		if (!from.count)
348
			from.count = region_size;
L
Linus Torvalds 已提交
349
	} else
350
		from.count = region_size;
L
Linus Torvalds 已提交
351 352 353

	/* fill in the destinations */
	for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
354
		if (&ms->mirror[i] == get_default_mirror(ms))
L
Linus Torvalds 已提交
355 356 357 358
			continue;

		m = ms->mirror + i;
		dest->bdev = m->dev->bdev;
359
		dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
L
Linus Torvalds 已提交
360 361 362 363 364
		dest->count = from.count;
		dest++;
	}

	/* hand to kcopyd */
365 366 367
	if (!errors_handled(ms))
		set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);

H
Heinz Mauelshagen 已提交
368 369
	r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
			   flags, recovery_complete, reg);
L
Linus Torvalds 已提交
370 371 372 373 374 375

	return r;
}

static void do_recovery(struct mirror_set *ms)
{
376 377
	struct dm_region *reg;
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
L
Linus Torvalds 已提交
378 379 380 381 382
	int r;

	/*
	 * Start quiescing some regions.
	 */
383
	dm_rh_recovery_prepare(ms->rh);
L
Linus Torvalds 已提交
384 385 386 387

	/*
	 * Copy any already quiesced regions.
	 */
388
	while ((reg = dm_rh_recovery_start(ms->rh))) {
L
Linus Torvalds 已提交
389 390
		r = recover(ms, reg);
		if (r)
391
			dm_rh_recovery_end(reg, 0);
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	}

	/*
	 * Update the in sync flag.
	 */
	if (!ms->in_sync &&
	    (log->type->get_sync_count(log) == ms->nr_regions)) {
		/* the sync is complete */
		dm_table_event(ms->ti->table);
		ms->in_sync = 1;
	}
}

/*-----------------------------------------------------------------
 * Reads
 *---------------------------------------------------------------*/
static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
{
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	struct mirror *m = get_default_mirror(ms);

	do {
		if (likely(!atomic_read(&m->error_count)))
			return m;

		if (m-- == ms->mirror)
			m += ms->nr_mirrors;
	} while (m != get_default_mirror(ms));

	return NULL;
}

static int default_ok(struct mirror *m)
{
	struct mirror *default_mirror = get_default_mirror(m->ms);

	return !atomic_read(&default_mirror->error_count);
}

static int mirror_available(struct mirror_set *ms, struct bio *bio)
{
432 433
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
	region_t region = dm_rh_bio_to_region(ms->rh, bio);
434

435
	if (log->type->in_sync(log, region, 0))
436 437 438
		return choose_mirror(ms,  bio->bi_sector) ? 1 : 0;

	return 0;
L
Linus Torvalds 已提交
439 440 441 442 443
}

/*
 * remap a buffer to a particular mirror.
 */
444 445
static sector_t map_sector(struct mirror *m, struct bio *bio)
{
M
Mikulas Patocka 已提交
446 447
	if (unlikely(!bio->bi_size))
		return 0;
448 449 450 451
	return m->offset + (bio->bi_sector - m->ms->ti->begin);
}

static void map_bio(struct mirror *m, struct bio *bio)
L
Linus Torvalds 已提交
452 453
{
	bio->bi_bdev = m->dev->bdev;
454 455 456
	bio->bi_sector = map_sector(m, bio);
}

H
Heinz Mauelshagen 已提交
457
static void map_region(struct dm_io_region *io, struct mirror *m,
458 459 460 461 462 463 464
		       struct bio *bio)
{
	io->bdev = m->dev->bdev;
	io->sector = map_sector(m, bio);
	io->count = bio->bi_size >> 9;
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static void hold_bio(struct mirror_set *ms, struct bio *bio)
{
	/*
	 * If device is suspended, complete the bio.
	 */
	if (atomic_read(&ms->suspend)) {
		if (dm_noflush_suspending(ms->ti))
			bio_endio(bio, DM_ENDIO_REQUEUE);
		else
			bio_endio(bio, -EIO);
		return;
	}

	/*
	 * Hold bio until the suspend is complete.
	 */
	spin_lock_irq(&ms->lock);
	bio_list_add(&ms->holds, bio);
	spin_unlock_irq(&ms->lock);
}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/*-----------------------------------------------------------------
 * Reads
 *---------------------------------------------------------------*/
static void read_callback(unsigned long error, void *context)
{
	struct bio *bio = context;
	struct mirror *m;

	m = bio_get_m(bio);
	bio_set_m(bio, NULL);

	if (likely(!error)) {
		bio_endio(bio, 0);
		return;
	}

	fail_mirror(m, DM_RAID1_READ_ERROR);

	if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
		DMWARN_LIMIT("Read failure on mirror device %s.  "
			     "Trying alternative device.",
			     m->dev->name);
		queue_bio(m->ms, bio, bio_rw(bio));
		return;
	}

	DMERR_LIMIT("Read failure on mirror device %s.  Failing I/O.",
		    m->dev->name);
	bio_endio(bio, -EIO);
}

/* Asynchronous read. */
static void read_async_bio(struct mirror *m, struct bio *bio)
{
H
Heinz Mauelshagen 已提交
520
	struct dm_io_region io;
521 522 523 524 525 526 527 528 529 530 531
	struct dm_io_request io_req = {
		.bi_rw = READ,
		.mem.type = DM_IO_BVEC,
		.mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
		.notify.fn = read_callback,
		.notify.context = bio,
		.client = m->ms->io_client,
	};

	map_region(&io, m, bio);
	bio_set_m(bio, m);
532 533 534 535 536 537 538 539
	BUG_ON(dm_io(&io_req, 1, &io, NULL));
}

static inline int region_in_sync(struct mirror_set *ms, region_t region,
				 int may_block)
{
	int state = dm_rh_get_state(ms->rh, region, may_block);
	return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
L
Linus Torvalds 已提交
540 541 542 543 544 545 546 547 548
}

static void do_reads(struct mirror_set *ms, struct bio_list *reads)
{
	region_t region;
	struct bio *bio;
	struct mirror *m;

	while ((bio = bio_list_pop(reads))) {
549
		region = dm_rh_bio_to_region(ms->rh, bio);
550
		m = get_default_mirror(ms);
L
Linus Torvalds 已提交
551 552 553 554

		/*
		 * We can only read balance if the region is in sync.
		 */
555
		if (likely(region_in_sync(ms, region, 1)))
L
Linus Torvalds 已提交
556
			m = choose_mirror(ms, bio->bi_sector);
557 558
		else if (m && atomic_read(&m->error_count))
			m = NULL;
L
Linus Torvalds 已提交
559

560 561 562 563
		if (likely(m))
			read_async_bio(m, bio);
		else
			bio_endio(bio, -EIO);
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
	}
}

/*-----------------------------------------------------------------
 * Writes.
 *
 * We do different things with the write io depending on the
 * state of the region that it's in:
 *
 * SYNC: 	increment pending, use kcopyd to write to *all* mirrors
 * RECOVERING:	delay the io until recovery completes
 * NOSYNC:	increment pending, just write to the default mirror
 *---------------------------------------------------------------*/
577 578


L
Linus Torvalds 已提交
579 580
static void write_callback(unsigned long error, void *context)
{
581
	unsigned i, ret = 0;
L
Linus Torvalds 已提交
582 583
	struct bio *bio = (struct bio *) context;
	struct mirror_set *ms;
584 585
	int should_wake = 0;
	unsigned long flags;
L
Linus Torvalds 已提交
586

587 588
	ms = bio_get_m(bio)->ms;
	bio_set_m(bio, NULL);
L
Linus Torvalds 已提交
589 590 591 592 593 594 595

	/*
	 * NOTE: We don't decrement the pending count here,
	 * instead it is done by the targets endio function.
	 * This way we handle both writes to SYNC and NOSYNC
	 * regions with the same code.
	 */
596 597 598 599
	if (likely(!error)) {
		bio_endio(bio, ret);
		return;
	}
L
Linus Torvalds 已提交
600

601 602 603 604
	for (i = 0; i < ms->nr_mirrors; i++)
		if (test_bit(i, &error))
			fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);

605 606 607 608 609 610 611 612 613 614 615 616
	/*
	 * Need to raise event.  Since raising
	 * events can block, we need to do it in
	 * the main thread.
	 */
	spin_lock_irqsave(&ms->lock, flags);
	if (!ms->failures.head)
		should_wake = 1;
	bio_list_add(&ms->failures, bio);
	spin_unlock_irqrestore(&ms->lock, flags);
	if (should_wake)
		wakeup_mirrord(ms);
L
Linus Torvalds 已提交
617 618 619 620 621
}

static void do_write(struct mirror_set *ms, struct bio *bio)
{
	unsigned int i;
H
Heinz Mauelshagen 已提交
622
	struct dm_io_region io[ms->nr_mirrors], *dest = io;
L
Linus Torvalds 已提交
623
	struct mirror *m;
M
Milan Broz 已提交
624
	struct dm_io_request io_req = {
M
Mikulas Patocka 已提交
625
		.bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
M
Milan Broz 已提交
626 627 628 629 630 631
		.mem.type = DM_IO_BVEC,
		.mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
		.notify.fn = write_callback,
		.notify.context = bio,
		.client = ms->io_client,
	};
L
Linus Torvalds 已提交
632

633 634
	for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
		map_region(dest++, m, bio);
L
Linus Torvalds 已提交
635

636 637 638 639 640
	/*
	 * Use default mirror because we only need it to retrieve the reference
	 * to the mirror set in write_callback().
	 */
	bio_set_m(bio, get_default_mirror(ms));
M
Milan Broz 已提交
641

642
	BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
L
Linus Torvalds 已提交
643 644 645 646 647 648 649
}

static void do_writes(struct mirror_set *ms, struct bio_list *writes)
{
	int state;
	struct bio *bio;
	struct bio_list sync, nosync, recover, *this_list = NULL;
650 651 652
	struct bio_list requeue;
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
	region_t region;
L
Linus Torvalds 已提交
653 654 655 656 657 658 659 660 661 662

	if (!writes->head)
		return;

	/*
	 * Classify each write.
	 */
	bio_list_init(&sync);
	bio_list_init(&nosync);
	bio_list_init(&recover);
663
	bio_list_init(&requeue);
L
Linus Torvalds 已提交
664 665

	while ((bio = bio_list_pop(writes))) {
M
Mikulas Patocka 已提交
666 667 668 669 670
		if (unlikely(bio_empty_barrier(bio))) {
			bio_list_add(&sync, bio);
			continue;
		}

671 672 673 674 675 676 677 678 679
		region = dm_rh_bio_to_region(ms->rh, bio);

		if (log->type->is_remote_recovering &&
		    log->type->is_remote_recovering(log, region)) {
			bio_list_add(&requeue, bio);
			continue;
		}

		state = dm_rh_get_state(ms->rh, region, 1);
L
Linus Torvalds 已提交
680
		switch (state) {
681 682
		case DM_RH_CLEAN:
		case DM_RH_DIRTY:
L
Linus Torvalds 已提交
683 684 685
			this_list = &sync;
			break;

686
		case DM_RH_NOSYNC:
L
Linus Torvalds 已提交
687 688 689
			this_list = &nosync;
			break;

690
		case DM_RH_RECOVERING:
L
Linus Torvalds 已提交
691 692 693 694 695 696 697
			this_list = &recover;
			break;
		}

		bio_list_add(this_list, bio);
	}

698 699 700 701 702 703 704 705
	/*
	 * Add bios that are delayed due to remote recovery
	 * back on to the write queue
	 */
	if (unlikely(requeue.head)) {
		spin_lock_irq(&ms->lock);
		bio_list_merge(&ms->writes, &requeue);
		spin_unlock_irq(&ms->lock);
706
		delayed_wake(ms);
707 708
	}

L
Linus Torvalds 已提交
709 710 711 712 713
	/*
	 * Increment the pending counts for any regions that will
	 * be written to (writes to recover regions are going to
	 * be delayed).
	 */
714 715
	dm_rh_inc_pending(ms->rh, &sync);
	dm_rh_inc_pending(ms->rh, &nosync);
716 717 718 719 720 721 722

	/*
	 * If the flush fails on a previous call and succeeds here,
	 * we must not reset the log_failure variable.  We need
	 * userspace interaction to do that.
	 */
	ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
L
Linus Torvalds 已提交
723 724 725 726

	/*
	 * Dispatch io.
	 */
727
	if (unlikely(ms->log_failure) && errors_handled(ms)) {
728 729 730
		spin_lock_irq(&ms->lock);
		bio_list_merge(&ms->failures, &sync);
		spin_unlock_irq(&ms->lock);
731
		wakeup_mirrord(ms);
732
	} else
J
Jonathan Brassow 已提交
733
		while ((bio = bio_list_pop(&sync)))
734
			do_write(ms, bio);
L
Linus Torvalds 已提交
735 736

	while ((bio = bio_list_pop(&recover)))
737
		dm_rh_delay(ms->rh, bio);
L
Linus Torvalds 已提交
738 739

	while ((bio = bio_list_pop(&nosync))) {
740 741 742 743 744 745
		if (unlikely(ms->leg_failure) && errors_handled(ms)) {
			spin_lock_irq(&ms->lock);
			bio_list_add(&ms->failures, bio);
			spin_unlock_irq(&ms->lock);
			wakeup_mirrord(ms);
		} else {
746 747 748
			map_bio(get_default_mirror(ms), bio);
			generic_make_request(bio);
		}
L
Linus Torvalds 已提交
749 750 751
	}
}

752 753 754 755
static void do_failures(struct mirror_set *ms, struct bio_list *failures)
{
	struct bio *bio;

756
	if (likely(!failures->head))
757 758
		return;

759 760
	/*
	 * If the log has failed, unattempted writes are being
761
	 * put on the holds list.  We can't issue those writes
762 763 764 765 766 767 768 769 770 771 772 773 774 775
	 * until a log has been marked, so we must store them.
	 *
	 * If a 'noflush' suspend is in progress, we can requeue
	 * the I/O's to the core.  This give userspace a chance
	 * to reconfigure the mirror, at which point the core
	 * will reissue the writes.  If the 'noflush' flag is
	 * not set, we have no choice but to return errors.
	 *
	 * Some writes on the failures list may have been
	 * submitted before the log failure and represent a
	 * failure to write to one of the devices.  It is ok
	 * for us to treat them the same and requeue them
	 * as well.
	 */
776
	while ((bio = bio_list_pop(failures))) {
777
		if (!ms->log_failure) {
778
			ms->in_sync = 0;
779
			dm_rh_mark_nosync(ms->rh, bio);
780
		}
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795

		/*
		 * If all the legs are dead, fail the I/O.
		 * If we have been told to handle errors, hold the bio
		 * and wait for userspace to deal with the problem.
		 * Otherwise pretend that the I/O succeeded. (This would
		 * be wrong if the failed leg returned after reboot and
		 * got replicated back to the good legs.)
		 */
		if (!get_valid_mirror(ms))
			bio_endio(bio, -EIO);
		else if (errors_handled(ms))
			hold_bio(ms, bio);
		else
			bio_endio(bio, 0);
796
	}
797 798 799 800 801 802 803 804 805 806
}

static void trigger_event(struct work_struct *work)
{
	struct mirror_set *ms =
		container_of(work, struct mirror_set, trigger_event);

	dm_table_event(ms->ti->table);
}

L
Linus Torvalds 已提交
807 808 809
/*-----------------------------------------------------------------
 * kmirrord
 *---------------------------------------------------------------*/
M
Mikulas Patocka 已提交
810
static void do_mirror(struct work_struct *work)
L
Linus Torvalds 已提交
811
{
812 813
	struct mirror_set *ms = container_of(work, struct mirror_set,
					     kmirrord_work);
814 815
	struct bio_list reads, writes, failures;
	unsigned long flags;
L
Linus Torvalds 已提交
816

817
	spin_lock_irqsave(&ms->lock, flags);
L
Linus Torvalds 已提交
818 819
	reads = ms->reads;
	writes = ms->writes;
820
	failures = ms->failures;
L
Linus Torvalds 已提交
821 822
	bio_list_init(&ms->reads);
	bio_list_init(&ms->writes);
823 824
	bio_list_init(&ms->failures);
	spin_unlock_irqrestore(&ms->lock, flags);
L
Linus Torvalds 已提交
825

826
	dm_rh_update_states(ms->rh, errors_handled(ms));
L
Linus Torvalds 已提交
827 828 829
	do_recovery(ms);
	do_reads(ms, &reads);
	do_writes(ms, &writes);
830
	do_failures(ms, &failures);
M
Mikulas Patocka 已提交
831 832

	dm_table_unplug_all(ms->ti->table);
L
Linus Torvalds 已提交
833 834 835 836 837 838 839 840
}

/*-----------------------------------------------------------------
 * Target functions
 *---------------------------------------------------------------*/
static struct mirror_set *alloc_context(unsigned int nr_mirrors,
					uint32_t region_size,
					struct dm_target *ti,
H
Heinz Mauelshagen 已提交
841
					struct dm_dirty_log *dl)
L
Linus Torvalds 已提交
842 843 844 845 846 847
{
	size_t len;
	struct mirror_set *ms = NULL;

	len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);

848
	ms = kzalloc(len, GFP_KERNEL);
L
Linus Torvalds 已提交
849
	if (!ms) {
850
		ti->error = "Cannot allocate mirror context";
L
Linus Torvalds 已提交
851 852 853 854
		return NULL;
	}

	spin_lock_init(&ms->lock);
855 856 857 858
	bio_list_init(&ms->reads);
	bio_list_init(&ms->writes);
	bio_list_init(&ms->failures);
	bio_list_init(&ms->holds);
L
Linus Torvalds 已提交
859 860 861 862 863

	ms->ti = ti;
	ms->nr_mirrors = nr_mirrors;
	ms->nr_regions = dm_sector_div_up(ti->len, region_size);
	ms->in_sync = 0;
864
	ms->log_failure = 0;
865
	ms->leg_failure = 0;
866
	atomic_set(&ms->suspend, 0);
867
	atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
L
Linus Torvalds 已提交
868

869 870 871
	ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
						_dm_raid1_read_record_cache);

872 873 874 875 876 877
	if (!ms->read_record_pool) {
		ti->error = "Error creating mirror read_record_pool";
		kfree(ms);
		return NULL;
	}

M
Milan Broz 已提交
878 879 880
	ms->io_client = dm_io_client_create(DM_IO_PAGES);
	if (IS_ERR(ms->io_client)) {
		ti->error = "Error creating dm_io client";
881
		mempool_destroy(ms->read_record_pool);
M
Milan Broz 已提交
882 883 884 885
		kfree(ms);
 		return NULL;
	}

886 887 888 889 890
	ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
				       wakeup_all_recovery_waiters,
				       ms->ti->begin, MAX_RECOVERY,
				       dl, region_size, ms->nr_regions);
	if (IS_ERR(ms->rh)) {
891
		ti->error = "Error creating dirty region hash";
D
Dmitry Monakhov 已提交
892
		dm_io_client_destroy(ms->io_client);
893
		mempool_destroy(ms->read_record_pool);
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906
		kfree(ms);
		return NULL;
	}

	return ms;
}

static void free_context(struct mirror_set *ms, struct dm_target *ti,
			 unsigned int m)
{
	while (m--)
		dm_put_device(ti, ms->mirror[m].dev);

M
Milan Broz 已提交
907
	dm_io_client_destroy(ms->io_client);
908
	dm_region_hash_destroy(ms->rh);
909
	mempool_destroy(ms->read_record_pool);
L
Linus Torvalds 已提交
910 911 912 913 914 915
	kfree(ms);
}

static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
		      unsigned int mirror, char **argv)
{
A
Andrew Morton 已提交
916
	unsigned long long offset;
L
Linus Torvalds 已提交
917

A
Andrew Morton 已提交
918
	if (sscanf(argv[1], "%llu", &offset) != 1) {
919
		ti->error = "Invalid offset";
L
Linus Torvalds 已提交
920 921 922 923 924 925
		return -EINVAL;
	}

	if (dm_get_device(ti, argv[0], offset, ti->len,
			  dm_table_get_mode(ti->table),
			  &ms->mirror[mirror].dev)) {
926
		ti->error = "Device lookup failure";
L
Linus Torvalds 已提交
927 928 929
		return -ENXIO;
	}

930
	ms->mirror[mirror].ms = ms;
931 932
	atomic_set(&(ms->mirror[mirror].error_count), 0);
	ms->mirror[mirror].error_type = 0;
L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940
	ms->mirror[mirror].offset = offset;

	return 0;
}

/*
 * Create dirty log: log_type #log_params <log_params>
 */
H
Heinz Mauelshagen 已提交
941
static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
942 943
					     unsigned argc, char **argv,
					     unsigned *args_used)
L
Linus Torvalds 已提交
944
{
945
	unsigned param_count;
H
Heinz Mauelshagen 已提交
946
	struct dm_dirty_log *dl;
L
Linus Torvalds 已提交
947 948

	if (argc < 2) {
949
		ti->error = "Insufficient mirror log arguments";
L
Linus Torvalds 已提交
950 951 952 953
		return NULL;
	}

	if (sscanf(argv[1], "%u", &param_count) != 1) {
954
		ti->error = "Invalid mirror log argument count";
L
Linus Torvalds 已提交
955 956 957 958 959 960
		return NULL;
	}

	*args_used = 2 + param_count;

	if (argc < *args_used) {
961
		ti->error = "Insufficient mirror log arguments";
L
Linus Torvalds 已提交
962 963 964
		return NULL;
	}

M
Mikulas Patocka 已提交
965 966
	dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
				 argv + 2);
L
Linus Torvalds 已提交
967
	if (!dl) {
968
		ti->error = "Error creating mirror dirty log";
L
Linus Torvalds 已提交
969 970 971 972 973 974
		return NULL;
	}

	return dl;
}

975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
			  unsigned *args_used)
{
	unsigned num_features;
	struct dm_target *ti = ms->ti;

	*args_used = 0;

	if (!argc)
		return 0;

	if (sscanf(argv[0], "%u", &num_features) != 1) {
		ti->error = "Invalid number of features";
		return -EINVAL;
	}

	argc--;
	argv++;
	(*args_used)++;

	if (num_features > argc) {
		ti->error = "Not enough arguments to support feature count";
		return -EINVAL;
	}

	if (!strcmp("handle_errors", argv[0]))
		ms->features |= DM_RAID1_HANDLE_ERRORS;
	else {
		ti->error = "Unrecognised feature requested";
		return -EINVAL;
	}

	(*args_used)++;

	return 0;
}

L
Linus Torvalds 已提交
1012 1013 1014 1015 1016
/*
 * Construct a mirror mapping:
 *
 * log_type #log_params <log_params>
 * #mirrors [mirror_path offset]{2,}
1017
 * [#features <features>]
L
Linus Torvalds 已提交
1018 1019 1020
 *
 * log_type is "core" or "disk"
 * #log_params is between 1 and 3
1021 1022
 *
 * If present, features must be "handle_errors".
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028
 */
static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
	int r;
	unsigned int nr_mirrors, m, args_used;
	struct mirror_set *ms;
H
Heinz Mauelshagen 已提交
1029
	struct dm_dirty_log *dl;
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038

	dl = create_dirty_log(ti, argc, argv, &args_used);
	if (!dl)
		return -EINVAL;

	argv += args_used;
	argc -= args_used;

	if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
H
Heinz Mauelshagen 已提交
1039
	    nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
1040
		ti->error = "Invalid number of mirrors";
H
Heinz Mauelshagen 已提交
1041
		dm_dirty_log_destroy(dl);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046
		return -EINVAL;
	}

	argv++, argc--;

1047 1048
	if (argc < nr_mirrors * 2) {
		ti->error = "Too few mirror arguments";
H
Heinz Mauelshagen 已提交
1049
		dm_dirty_log_destroy(dl);
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054
		return -EINVAL;
	}

	ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
	if (!ms) {
H
Heinz Mauelshagen 已提交
1055
		dm_dirty_log_destroy(dl);
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		return -ENOMEM;
	}

	/* Get the mirror parameter sets */
	for (m = 0; m < nr_mirrors; m++) {
		r = get_mirror(ms, ti, m, argv);
		if (r) {
			free_context(ms, ti, m);
			return r;
		}
		argv += 2;
		argc -= 2;
	}

	ti->private = ms;
1071
	ti->split_io = dm_rh_get_region_size(ms->rh);
M
Mikulas Patocka 已提交
1072
	ti->num_flush_requests = 1;
L
Linus Torvalds 已提交
1073

1074 1075 1076
	ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
	if (!ms->kmirrord_wq) {
		DMERR("couldn't start kmirrord");
D
Dmitry Monakhov 已提交
1077 1078
		r = -ENOMEM;
		goto err_free_context;
1079 1080
	}
	INIT_WORK(&ms->kmirrord_work, do_mirror);
M
Mikulas Patocka 已提交
1081 1082
	init_timer(&ms->timer);
	ms->timer_pending = 0;
1083
	INIT_WORK(&ms->trigger_event, trigger_event);
1084

1085
	r = parse_features(ms, argc, argv, &args_used);
D
Dmitry Monakhov 已提交
1086 1087
	if (r)
		goto err_destroy_wq;
1088 1089 1090 1091

	argv += args_used;
	argc -= args_used;

1092 1093 1094 1095 1096 1097 1098 1099 1100
	/*
	 * Any read-balancing addition depends on the
	 * DM_RAID1_HANDLE_ERRORS flag being present.
	 * This is because the decision to balance depends
	 * on the sync state of a region.  If the above
	 * flag is not present, we ignore errors; and
	 * the sync state may be inaccurate.
	 */

1101 1102
	if (argc) {
		ti->error = "Too many mirror arguments";
D
Dmitry Monakhov 已提交
1103 1104
		r = -EINVAL;
		goto err_destroy_wq;
1105 1106
	}

1107
	r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
D
Dmitry Monakhov 已提交
1108 1109
	if (r)
		goto err_destroy_wq;
L
Linus Torvalds 已提交
1110

1111
	wakeup_mirrord(ms);
L
Linus Torvalds 已提交
1112
	return 0;
D
Dmitry Monakhov 已提交
1113 1114 1115 1116 1117 1118

err_destroy_wq:
	destroy_workqueue(ms->kmirrord_wq);
err_free_context:
	free_context(ms, ti, ms->nr_mirrors);
	return r;
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124
}

static void mirror_dtr(struct dm_target *ti)
{
	struct mirror_set *ms = (struct mirror_set *) ti->private;

M
Mikulas Patocka 已提交
1125
	del_timer_sync(&ms->timer);
1126
	flush_workqueue(ms->kmirrord_wq);
1127
	flush_scheduled_work();
H
Heinz Mauelshagen 已提交
1128
	dm_kcopyd_client_destroy(ms->kcopyd_client);
1129
	destroy_workqueue(ms->kmirrord_wq);
L
Linus Torvalds 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	free_context(ms, ti, ms->nr_mirrors);
}

/*
 * Mirror mapping function
 */
static int mirror_map(struct dm_target *ti, struct bio *bio,
		      union map_info *map_context)
{
	int r, rw = bio_rw(bio);
	struct mirror *m;
	struct mirror_set *ms = ti->private;
1142
	struct dm_raid1_read_record *read_record = NULL;
1143
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
L
Linus Torvalds 已提交
1144 1145

	if (rw == WRITE) {
1146
		/* Save region for mirror_end_io() handler */
1147
		map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
L
Linus Torvalds 已提交
1148
		queue_bio(ms, bio, rw);
1149
		return DM_MAPIO_SUBMITTED;
L
Linus Torvalds 已提交
1150 1151
	}

1152
	r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
L
Linus Torvalds 已提交
1153 1154 1155 1156
	if (r < 0 && r != -EWOULDBLOCK)
		return r;

	/*
1157
	 * If region is not in-sync queue the bio.
L
Linus Torvalds 已提交
1158
	 */
1159 1160 1161
	if (!r || (r == -EWOULDBLOCK)) {
		if (rw == READA)
			return -EWOULDBLOCK;
L
Linus Torvalds 已提交
1162 1163

		queue_bio(ms, bio, rw);
1164
		return DM_MAPIO_SUBMITTED;
L
Linus Torvalds 已提交
1165 1166
	}

1167 1168 1169 1170
	/*
	 * The region is in-sync and we can perform reads directly.
	 * Store enough information so we can retry if it fails.
	 */
L
Linus Torvalds 已提交
1171
	m = choose_mirror(ms, bio->bi_sector);
1172
	if (unlikely(!m))
L
Linus Torvalds 已提交
1173 1174
		return -EIO;

1175 1176 1177 1178 1179 1180 1181 1182 1183
	read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
	if (likely(read_record)) {
		dm_bio_record(&read_record->details, bio);
		map_context->ptr = read_record;
		read_record->m = m;
	}

	map_bio(m, bio);

1184
	return DM_MAPIO_REMAPPED;
L
Linus Torvalds 已提交
1185 1186 1187 1188 1189 1190 1191
}

static int mirror_end_io(struct dm_target *ti, struct bio *bio,
			 int error, union map_info *map_context)
{
	int rw = bio_rw(bio);
	struct mirror_set *ms = (struct mirror_set *) ti->private;
1192 1193 1194
	struct mirror *m = NULL;
	struct dm_bio_details *bd = NULL;
	struct dm_raid1_read_record *read_record = map_context->ptr;
L
Linus Torvalds 已提交
1195 1196 1197 1198

	/*
	 * We need to dec pending if this was a write.
	 */
1199
	if (rw == WRITE) {
M
Mikulas Patocka 已提交
1200 1201
		if (likely(!bio_empty_barrier(bio)))
			dm_rh_dec(ms->rh, map_context->ll);
1202 1203
		return error;
	}
L
Linus Torvalds 已提交
1204

1205 1206 1207
	if (error == -EOPNOTSUPP)
		goto out;

1208
	if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
1209 1210 1211 1212 1213 1214 1215 1216 1217
		goto out;

	if (unlikely(error)) {
		if (!read_record) {
			/*
			 * There wasn't enough memory to record necessary
			 * information for a retry or there was no other
			 * mirror in-sync.
			 */
A
Adrian Bunk 已提交
1218
			DMERR_LIMIT("Mirror read failed.");
1219 1220
			return -EIO;
		}
A
Adrian Bunk 已提交
1221 1222 1223

		m = read_record->m;

1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		DMERR("Mirror read failed from %s. Trying alternative device.",
		      m->dev->name);

		fail_mirror(m, DM_RAID1_READ_ERROR);

		/*
		 * A failed read is requeued for another attempt using an intact
		 * mirror.
		 */
		if (default_ok(m) || mirror_available(ms, bio)) {
			bd = &read_record->details;

			dm_bio_restore(bd, bio);
			mempool_free(read_record, ms->read_record_pool);
			map_context->ptr = NULL;
			queue_bio(ms, bio, rw);
			return 1;
		}
		DMERR("All replicated volumes dead, failing I/O");
	}

out:
	if (read_record) {
		mempool_free(read_record, ms->read_record_pool);
		map_context->ptr = NULL;
	}

	return error;
L
Linus Torvalds 已提交
1252 1253
}

1254
static void mirror_presuspend(struct dm_target *ti)
L
Linus Torvalds 已提交
1255 1256
{
	struct mirror_set *ms = (struct mirror_set *) ti->private;
1257
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
L
Linus Torvalds 已提交
1258

1259 1260 1261
	struct bio_list holds;
	struct bio *bio;

1262 1263 1264 1265 1266 1267
	atomic_set(&ms->suspend, 1);

	/*
	 * We must finish up all the work that we've
	 * generated (i.e. recovery work).
	 */
1268
	dm_rh_stop_recovery(ms->rh);
1269 1270

	wait_event(_kmirrord_recovery_stopped,
1271
		   !dm_rh_recovery_in_flight(ms->rh));
1272

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
	if (log->type->presuspend && log->type->presuspend(log))
		/* FIXME: need better error handling */
		DMWARN("log presuspend failed");

	/*
	 * Now that recovery is complete/stopped and the
	 * delayed bios are queued, we need to wait for
	 * the worker thread to complete.  This way,
	 * we know that all of our I/O has been pushed.
	 */
	flush_workqueue(ms->kmirrord_wq);
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

	/*
	 * Now set ms->suspend is set and the workqueue flushed, no more
	 * entries can be added to ms->hold list, so process it.
	 *
	 * Bios can still arrive concurrently with or after this
	 * presuspend function, but they cannot join the hold list
	 * because ms->suspend is set.
	 */
	spin_lock_irq(&ms->lock);
	holds = ms->holds;
	bio_list_init(&ms->holds);
	spin_unlock_irq(&ms->lock);

	while ((bio = bio_list_pop(&holds)))
		hold_bio(ms, bio);
1300 1301 1302 1303 1304
}

static void mirror_postsuspend(struct dm_target *ti)
{
	struct mirror_set *ms = ti->private;
1305
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1306

J
Jonathan Brassow 已提交
1307
	if (log->type->postsuspend && log->type->postsuspend(log))
L
Linus Torvalds 已提交
1308
		/* FIXME: need better error handling */
1309
		DMWARN("log postsuspend failed");
L
Linus Torvalds 已提交
1310 1311 1312 1313
}

static void mirror_resume(struct dm_target *ti)
{
1314
	struct mirror_set *ms = ti->private;
1315
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1316 1317

	atomic_set(&ms->suspend, 0);
L
Linus Torvalds 已提交
1318 1319 1320
	if (log->type->resume && log->type->resume(log))
		/* FIXME: need better error handling */
		DMWARN("log resume failed");
1321
	dm_rh_start_recovery(ms->rh);
L
Linus Torvalds 已提交
1322 1323
}

J
Jonathan Brassow 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
/*
 * device_status_char
 * @m: mirror device/leg we want the status of
 *
 * We return one character representing the most severe error
 * we have encountered.
 *    A => Alive - No failures
 *    D => Dead - A write failure occurred leaving mirror out-of-sync
 *    S => Sync - A sychronization failure occurred, mirror out-of-sync
 *    R => Read - A read failure occurred, mirror data unaffected
 *
 * Returns: <char>
 */
static char device_status_char(struct mirror *m)
{
	if (!atomic_read(&(m->error_count)))
		return 'A';

1342 1343
	return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
		(test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
J
Jonathan Brassow 已提交
1344 1345 1346 1347 1348
		(test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
		(test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
}


L
Linus Torvalds 已提交
1349 1350 1351
static int mirror_status(struct dm_target *ti, status_type_t type,
			 char *result, unsigned int maxlen)
{
J
Jonathan E Brassow 已提交
1352
	unsigned int m, sz = 0;
L
Linus Torvalds 已提交
1353
	struct mirror_set *ms = (struct mirror_set *) ti->private;
1354
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
J
Jonathan Brassow 已提交
1355
	char buffer[ms->nr_mirrors + 1];
L
Linus Torvalds 已提交
1356 1357 1358 1359

	switch (type) {
	case STATUSTYPE_INFO:
		DMEMIT("%d ", ms->nr_mirrors);
J
Jonathan Brassow 已提交
1360
		for (m = 0; m < ms->nr_mirrors; m++) {
L
Linus Torvalds 已提交
1361
			DMEMIT("%s ", ms->mirror[m].dev->name);
J
Jonathan Brassow 已提交
1362 1363 1364
			buffer[m] = device_status_char(&(ms->mirror[m]));
		}
		buffer[m] = '\0';
L
Linus Torvalds 已提交
1365

J
Jonathan Brassow 已提交
1366
		DMEMIT("%llu/%llu 1 %s ",
1367
		      (unsigned long long)log->type->get_sync_count(log),
J
Jonathan Brassow 已提交
1368
		      (unsigned long long)ms->nr_regions, buffer);
J
Jonathan E Brassow 已提交
1369

1370
		sz += log->type->status(log, type, result+sz, maxlen-sz);
J
Jonathan E Brassow 已提交
1371

L
Linus Torvalds 已提交
1372 1373 1374
		break;

	case STATUSTYPE_TABLE:
1375
		sz = log->type->status(log, type, result, maxlen);
J
Jonathan E Brassow 已提交
1376

1377
		DMEMIT("%d", ms->nr_mirrors);
L
Linus Torvalds 已提交
1378
		for (m = 0; m < ms->nr_mirrors; m++)
1379
			DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1380
			       (unsigned long long)ms->mirror[m].offset);
1381 1382 1383

		if (ms->features & DM_RAID1_HANDLE_ERRORS)
			DMEMIT(" 1 handle_errors");
L
Linus Torvalds 已提交
1384 1385 1386 1387 1388
	}

	return 0;
}

1389 1390 1391 1392 1393 1394 1395 1396 1397
static int mirror_iterate_devices(struct dm_target *ti,
				  iterate_devices_callout_fn fn, void *data)
{
	struct mirror_set *ms = ti->private;
	int ret = 0;
	unsigned i;

	for (i = 0; !ret && i < ms->nr_mirrors; i++)
		ret = fn(ti, ms->mirror[i].dev,
1398
			 ms->mirror[i].offset, ti->len, data);
1399 1400 1401 1402

	return ret;
}

L
Linus Torvalds 已提交
1403 1404
static struct target_type mirror_target = {
	.name	 = "mirror",
1405
	.version = {1, 12, 0},
L
Linus Torvalds 已提交
1406 1407 1408 1409 1410
	.module	 = THIS_MODULE,
	.ctr	 = mirror_ctr,
	.dtr	 = mirror_dtr,
	.map	 = mirror_map,
	.end_io	 = mirror_end_io,
1411
	.presuspend = mirror_presuspend,
L
Linus Torvalds 已提交
1412 1413 1414
	.postsuspend = mirror_postsuspend,
	.resume	 = mirror_resume,
	.status	 = mirror_status,
1415
	.iterate_devices = mirror_iterate_devices,
L
Linus Torvalds 已提交
1416 1417 1418 1419 1420 1421
};

static int __init dm_mirror_init(void)
{
	int r;

1422 1423 1424 1425 1426 1427 1428
	_dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
	if (!_dm_raid1_read_record_cache) {
		DMERR("Can't allocate dm_raid1_read_record cache");
		r = -ENOMEM;
		goto bad_cache;
	}

L
Linus Torvalds 已提交
1429
	r = dm_register_target(&mirror_target);
1430
	if (r < 0) {
1431
		DMERR("Failed to register mirror target");
1432 1433 1434 1435
		goto bad_target;
	}

	return 0;
L
Linus Torvalds 已提交
1436

1437 1438 1439
bad_target:
	kmem_cache_destroy(_dm_raid1_read_record_cache);
bad_cache:
L
Linus Torvalds 已提交
1440 1441 1442 1443 1444
	return r;
}

static void __exit dm_mirror_exit(void)
{
1445
	dm_unregister_target(&mirror_target);
1446
	kmem_cache_destroy(_dm_raid1_read_record_cache);
L
Linus Torvalds 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455
}

/* Module hooks */
module_init(dm_mirror_init);
module_exit(dm_mirror_exit);

MODULE_DESCRIPTION(DM_NAME " mirror target");
MODULE_AUTHOR("Joe Thornber");
MODULE_LICENSE("GPL");