dm-raid1.c 31.4 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 38 39 40 41
enum dm_raid1_error {
	DM_RAID1_WRITE_ERROR,
	DM_RAID1_SYNC_ERROR,
	DM_RAID1_READ_ERROR
};

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

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

54
	uint64_t features;
55

56
	spinlock_t lock;	/* protects the lists */
57 58
	struct bio_list reads;
	struct bio_list writes;
59
	struct bio_list failures;
60

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

66 67 68
	/* recovery */
	region_t nr_regions;
	int in_sync;
J
Jonathan Brassow 已提交
69
	int log_failure;
70
	atomic_t suspend;
71

72
	atomic_t default_mirror;	/* Default mirror */
73

74 75
	struct workqueue_struct *kmirrord_wq;
	struct work_struct kmirrord_work;
M
Mikulas Patocka 已提交
76 77 78
	struct timer_list timer;
	unsigned long timer_pending;

79
	struct work_struct trigger_event;
80

81
	unsigned nr_mirrors;
82 83 84
	struct mirror mirror[0];
};

85
static void wakeup_mirrord(void *context)
L
Linus Torvalds 已提交
86
{
87
	struct mirror_set *ms = context;
L
Linus Torvalds 已提交
88

89 90 91
	queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
}

M
Mikulas Patocka 已提交
92 93 94 95 96
static void delayed_wake_fn(unsigned long data)
{
	struct mirror_set *ms = (struct mirror_set *) data;

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

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);
}

111
static void wakeup_all_recovery_waiters(void *context)
L
Linus Torvalds 已提交
112
{
113
	wake_up_all(&_kmirrord_recovery_stopped);
L
Linus Torvalds 已提交
114 115
}

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

122 123 124 125 126
	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 已提交
127 128

	if (should_wake)
129
		wakeup_mirrord(ms);
L
Linus Torvalds 已提交
130 131
}

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

137 138
	while ((bio = bio_list_pop(bio_list)))
		queue_bio(ms, bio, WRITE);
L
Linus Torvalds 已提交
139 140
}

141 142 143 144 145 146
#define MIN_READ_RECORDS 20
struct dm_raid1_read_record {
	struct mirror *m;
	struct dm_bio_details details;
};

147 148
static struct kmem_cache *_dm_raid1_read_record_cache;

L
Linus Torvalds 已提交
149 150 151 152 153 154
/*
 * Every mirror should look like this one.
 */
#define DEFAULT_MIRROR 0

/*
155 156
 * 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 已提交
157 158
 * doesn't get submitted to the lower levels of block layer.
 */
159
static struct mirror *bio_get_m(struct bio *bio)
L
Linus Torvalds 已提交
160
{
161
	return (struct mirror *) bio->bi_next;
L
Linus Torvalds 已提交
162 163
}

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

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
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);
}

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

	/*
	 * 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 已提交
211 212 213
	if (!errors_handled(ms))
		return;

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	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;
	}

	for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
		if (!atomic_read(&new->error_count)) {
			set_default_mirror(new);
			break;
		}

	if (unlikely(new == ms->mirror + ms->nr_mirrors))
		DMWARN("All sides of mirror have failed.");

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

L
Linus Torvalds 已提交
240 241 242 243 244 245 246
/*-----------------------------------------------------------------
 * 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.
 *---------------------------------------------------------------*/
247
static void recovery_complete(int read_err, unsigned long write_err,
L
Linus Torvalds 已提交
248 249
			      void *context)
{
250 251
	struct dm_region *reg = context;
	struct mirror_set *ms = dm_rh_region_context(reg);
252
	int m, bit = 0;
L
Linus Torvalds 已提交
253

254
	if (read_err) {
255 256
		/* Read error means the failure of default mirror. */
		DMERR_LIMIT("Unable to read primary mirror during recovery");
257 258
		fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
	}
259

260
	if (write_err) {
261
		DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
262
			    write_err);
263 264 265 266 267 268 269 270 271 272 273 274 275
		/*
		 * 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++;
		}
	}
276

277
	dm_rh_recovery_end(reg, !(read_err || write_err));
L
Linus Torvalds 已提交
278 279
}

280
static int recover(struct mirror_set *ms, struct dm_region *reg)
L
Linus Torvalds 已提交
281 282
{
	int r;
283
	unsigned i;
H
Heinz Mauelshagen 已提交
284
	struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
L
Linus Torvalds 已提交
285 286
	struct mirror *m;
	unsigned long flags = 0;
287 288
	region_t key = dm_rh_get_region_key(reg);
	sector_t region_size = dm_rh_get_region_size(ms->rh);
L
Linus Torvalds 已提交
289 290

	/* fill in the source */
291
	m = get_default_mirror(ms);
L
Linus Torvalds 已提交
292
	from.bdev = m->dev->bdev;
293 294
	from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
	if (key == (ms->nr_regions - 1)) {
L
Linus Torvalds 已提交
295 296 297 298
		/*
		 * The final region may be smaller than
		 * region_size.
		 */
299
		from.count = ms->ti->len & (region_size - 1);
L
Linus Torvalds 已提交
300
		if (!from.count)
301
			from.count = region_size;
L
Linus Torvalds 已提交
302
	} else
303
		from.count = region_size;
L
Linus Torvalds 已提交
304 305 306

	/* fill in the destinations */
	for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
307
		if (&ms->mirror[i] == get_default_mirror(ms))
L
Linus Torvalds 已提交
308 309 310 311
			continue;

		m = ms->mirror + i;
		dest->bdev = m->dev->bdev;
312
		dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
L
Linus Torvalds 已提交
313 314 315 316 317
		dest->count = from.count;
		dest++;
	}

	/* hand to kcopyd */
318 319 320
	if (!errors_handled(ms))
		set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);

H
Heinz Mauelshagen 已提交
321 322
	r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
			   flags, recovery_complete, reg);
L
Linus Torvalds 已提交
323 324 325 326 327 328

	return r;
}

static void do_recovery(struct mirror_set *ms)
{
329 330
	struct dm_region *reg;
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
L
Linus Torvalds 已提交
331 332 333 334 335
	int r;

	/*
	 * Start quiescing some regions.
	 */
336
	dm_rh_recovery_prepare(ms->rh);
L
Linus Torvalds 已提交
337 338 339 340

	/*
	 * Copy any already quiesced regions.
	 */
341
	while ((reg = dm_rh_recovery_start(ms->rh))) {
L
Linus Torvalds 已提交
342 343
		r = recover(ms, reg);
		if (r)
344
			dm_rh_recovery_end(reg, 0);
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	}

	/*
	 * 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)
{
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	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)
{
385 386
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
	region_t region = dm_rh_bio_to_region(ms->rh, bio);
387

388
	if (log->type->in_sync(log, region, 0))
389 390 391
		return choose_mirror(ms,  bio->bi_sector) ? 1 : 0;

	return 0;
L
Linus Torvalds 已提交
392 393 394 395 396
}

/*
 * remap a buffer to a particular mirror.
 */
397 398 399 400 401 402
static sector_t map_sector(struct mirror *m, struct bio *bio)
{
	return m->offset + (bio->bi_sector - m->ms->ti->begin);
}

static void map_bio(struct mirror *m, struct bio *bio)
L
Linus Torvalds 已提交
403 404
{
	bio->bi_bdev = m->dev->bdev;
405 406 407
	bio->bi_sector = map_sector(m, bio);
}

H
Heinz Mauelshagen 已提交
408
static void map_region(struct dm_io_region *io, struct mirror *m,
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
		       struct bio *bio)
{
	io->bdev = m->dev->bdev;
	io->sector = map_sector(m, bio);
	io->count = bio->bi_size >> 9;
}

/*-----------------------------------------------------------------
 * 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 已提交
450
	struct dm_io_region io;
451 452 453 454 455 456 457 458 459 460 461
	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);
462 463 464 465 466 467 468 469
	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 已提交
470 471 472 473 474 475 476 477 478
}

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))) {
479
		region = dm_rh_bio_to_region(ms->rh, bio);
480
		m = get_default_mirror(ms);
L
Linus Torvalds 已提交
481 482 483 484

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

490 491 492 493
		if (likely(m))
			read_async_bio(m, bio);
		else
			bio_endio(bio, -EIO);
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506
	}
}

/*-----------------------------------------------------------------
 * 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
 *---------------------------------------------------------------*/
507 508


L
Linus Torvalds 已提交
509 510
static void write_callback(unsigned long error, void *context)
{
511
	unsigned i, ret = 0;
L
Linus Torvalds 已提交
512 513
	struct bio *bio = (struct bio *) context;
	struct mirror_set *ms;
514 515 516
	int uptodate = 0;
	int should_wake = 0;
	unsigned long flags;
L
Linus Torvalds 已提交
517

518 519
	ms = bio_get_m(bio)->ms;
	bio_set_m(bio, NULL);
L
Linus Torvalds 已提交
520 521 522 523 524 525 526

	/*
	 * 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.
	 */
527 528
	if (likely(!error))
		goto out;
L
Linus Torvalds 已提交
529

530 531 532 533 534 535 536 537 538 539 540
	for (i = 0; i < ms->nr_mirrors; i++)
		if (test_bit(i, &error))
			fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
		else
			uptodate = 1;

	if (unlikely(!uptodate)) {
		DMERR("All replicated volumes dead, failing I/O");
		/* None of the writes succeeded, fail the I/O. */
		ret = -EIO;
	} else if (errors_handled(ms)) {
L
Linus Torvalds 已提交
541
		/*
542 543 544
		 * Need to raise event.  Since raising
		 * events can block, we need to do it in
		 * the main thread.
L
Linus Torvalds 已提交
545
		 */
546 547 548 549 550 551
		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)
552
			wakeup_mirrord(ms);
553
		return;
L
Linus Torvalds 已提交
554
	}
555 556
out:
	bio_endio(bio, ret);
L
Linus Torvalds 已提交
557 558 559 560 561
}

static void do_write(struct mirror_set *ms, struct bio *bio)
{
	unsigned int i;
H
Heinz Mauelshagen 已提交
562
	struct dm_io_region io[ms->nr_mirrors], *dest = io;
L
Linus Torvalds 已提交
563
	struct mirror *m;
M
Milan Broz 已提交
564 565 566 567 568 569 570 571
	struct dm_io_request io_req = {
		.bi_rw = WRITE,
		.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 已提交
572

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

576 577 578 579 580
	/*
	 * 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 已提交
581

582
	BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
L
Linus Torvalds 已提交
583 584 585 586 587 588 589
}

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;
590 591 592
	struct bio_list requeue;
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
	region_t region;
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602

	if (!writes->head)
		return;

	/*
	 * Classify each write.
	 */
	bio_list_init(&sync);
	bio_list_init(&nosync);
	bio_list_init(&recover);
603
	bio_list_init(&requeue);
L
Linus Torvalds 已提交
604 605

	while ((bio = bio_list_pop(writes))) {
606 607 608 609 610 611 612 613 614
		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 已提交
615
		switch (state) {
616 617
		case DM_RH_CLEAN:
		case DM_RH_DIRTY:
L
Linus Torvalds 已提交
618 619 620
			this_list = &sync;
			break;

621
		case DM_RH_NOSYNC:
L
Linus Torvalds 已提交
622 623 624
			this_list = &nosync;
			break;

625
		case DM_RH_RECOVERING:
L
Linus Torvalds 已提交
626 627 628 629 630 631 632
			this_list = &recover;
			break;
		}

		bio_list_add(this_list, bio);
	}

633 634 635 636 637 638 639 640
	/*
	 * 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);
641
		delayed_wake(ms);
642 643
	}

L
Linus Torvalds 已提交
644 645 646 647 648
	/*
	 * Increment the pending counts for any regions that will
	 * be written to (writes to recover regions are going to
	 * be delayed).
	 */
649 650 651
	dm_rh_inc_pending(ms->rh, &sync);
	dm_rh_inc_pending(ms->rh, &nosync);
	ms->log_failure = dm_rh_flush(ms->rh) ? 1 : 0;
L
Linus Torvalds 已提交
652 653 654 655

	/*
	 * Dispatch io.
	 */
656 657 658 659
	if (unlikely(ms->log_failure)) {
		spin_lock_irq(&ms->lock);
		bio_list_merge(&ms->failures, &sync);
		spin_unlock_irq(&ms->lock);
660
		wakeup_mirrord(ms);
661
	} else
J
Jonathan Brassow 已提交
662
		while ((bio = bio_list_pop(&sync)))
663
			do_write(ms, bio);
L
Linus Torvalds 已提交
664 665

	while ((bio = bio_list_pop(&recover)))
666
		dm_rh_delay(ms->rh, bio);
L
Linus Torvalds 已提交
667 668

	while ((bio = bio_list_pop(&nosync))) {
669
		map_bio(get_default_mirror(ms), bio);
L
Linus Torvalds 已提交
670 671 672 673
		generic_make_request(bio);
	}
}

674 675 676 677 678 679 680
static void do_failures(struct mirror_set *ms, struct bio_list *failures)
{
	struct bio *bio;

	if (!failures->head)
		return;

681
	if (!ms->log_failure) {
I
Ilpo Jarvinen 已提交
682
		while ((bio = bio_list_pop(failures))) {
683 684
			ms->in_sync = 0;
			dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
I
Ilpo Jarvinen 已提交
685
		}
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
		return;
	}

	/*
	 * If the log has failed, unattempted writes are being
	 * put on the failures list.  We can't issue those writes
	 * 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.
	 */
	if (dm_noflush_suspending(ms->ti)) {
		while ((bio = bio_list_pop(failures)))
			bio_endio(bio, DM_ENDIO_REQUEUE);
		return;
	}

	if (atomic_read(&ms->suspend)) {
		while ((bio = bio_list_pop(failures)))
			bio_endio(bio, -EIO);
		return;
	}

	spin_lock_irq(&ms->lock);
	bio_list_merge(&ms->failures, failures);
	spin_unlock_irq(&ms->lock);

M
Mikulas Patocka 已提交
722
	delayed_wake(ms);
723 724 725 726 727 728 729 730 731 732
}

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 已提交
733 734 735
/*-----------------------------------------------------------------
 * kmirrord
 *---------------------------------------------------------------*/
M
Mikulas Patocka 已提交
736
static void do_mirror(struct work_struct *work)
L
Linus Torvalds 已提交
737
{
738 739
	struct mirror_set *ms = container_of(work, struct mirror_set,
					     kmirrord_work);
740 741
	struct bio_list reads, writes, failures;
	unsigned long flags;
L
Linus Torvalds 已提交
742

743
	spin_lock_irqsave(&ms->lock, flags);
L
Linus Torvalds 已提交
744 745
	reads = ms->reads;
	writes = ms->writes;
746
	failures = ms->failures;
L
Linus Torvalds 已提交
747 748
	bio_list_init(&ms->reads);
	bio_list_init(&ms->writes);
749 750
	bio_list_init(&ms->failures);
	spin_unlock_irqrestore(&ms->lock, flags);
L
Linus Torvalds 已提交
751

752
	dm_rh_update_states(ms->rh, errors_handled(ms));
L
Linus Torvalds 已提交
753 754 755
	do_recovery(ms);
	do_reads(ms, &reads);
	do_writes(ms, &writes);
756
	do_failures(ms, &failures);
M
Mikulas Patocka 已提交
757 758

	dm_table_unplug_all(ms->ti->table);
L
Linus Torvalds 已提交
759 760 761 762 763 764 765 766
}

/*-----------------------------------------------------------------
 * Target functions
 *---------------------------------------------------------------*/
static struct mirror_set *alloc_context(unsigned int nr_mirrors,
					uint32_t region_size,
					struct dm_target *ti,
H
Heinz Mauelshagen 已提交
767
					struct dm_dirty_log *dl)
L
Linus Torvalds 已提交
768 769 770 771 772 773
{
	size_t len;
	struct mirror_set *ms = NULL;

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

774
	ms = kzalloc(len, GFP_KERNEL);
L
Linus Torvalds 已提交
775
	if (!ms) {
776
		ti->error = "Cannot allocate mirror context";
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785
		return NULL;
	}

	spin_lock_init(&ms->lock);

	ms->ti = ti;
	ms->nr_mirrors = nr_mirrors;
	ms->nr_regions = dm_sector_div_up(ti->len, region_size);
	ms->in_sync = 0;
786 787
	ms->log_failure = 0;
	atomic_set(&ms->suspend, 0);
788
	atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
L
Linus Torvalds 已提交
789

790 791 792
	ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
						_dm_raid1_read_record_cache);

793 794 795 796 797 798
	if (!ms->read_record_pool) {
		ti->error = "Error creating mirror read_record_pool";
		kfree(ms);
		return NULL;
	}

M
Milan Broz 已提交
799 800 801
	ms->io_client = dm_io_client_create(DM_IO_PAGES);
	if (IS_ERR(ms->io_client)) {
		ti->error = "Error creating dm_io client";
802
		mempool_destroy(ms->read_record_pool);
M
Milan Broz 已提交
803 804 805 806
		kfree(ms);
 		return NULL;
	}

807 808 809 810 811
	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)) {
812
		ti->error = "Error creating dirty region hash";
D
Dmitry Monakhov 已提交
813
		dm_io_client_destroy(ms->io_client);
814
		mempool_destroy(ms->read_record_pool);
L
Linus Torvalds 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827
		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 已提交
828
	dm_io_client_destroy(ms->io_client);
829
	dm_region_hash_destroy(ms->rh);
830
	mempool_destroy(ms->read_record_pool);
L
Linus Torvalds 已提交
831 832 833 834 835 836
	kfree(ms);
}

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

A
Andrew Morton 已提交
839
	if (sscanf(argv[1], "%llu", &offset) != 1) {
840
		ti->error = "Invalid offset";
L
Linus Torvalds 已提交
841 842 843 844 845 846
		return -EINVAL;
	}

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

851
	ms->mirror[mirror].ms = ms;
852 853
	atomic_set(&(ms->mirror[mirror].error_count), 0);
	ms->mirror[mirror].error_type = 0;
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861
	ms->mirror[mirror].offset = offset;

	return 0;
}

/*
 * Create dirty log: log_type #log_params <log_params>
 */
H
Heinz Mauelshagen 已提交
862
static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
863 864
					     unsigned argc, char **argv,
					     unsigned *args_used)
L
Linus Torvalds 已提交
865
{
866
	unsigned param_count;
H
Heinz Mauelshagen 已提交
867
	struct dm_dirty_log *dl;
L
Linus Torvalds 已提交
868 869

	if (argc < 2) {
870
		ti->error = "Insufficient mirror log arguments";
L
Linus Torvalds 已提交
871 872 873 874
		return NULL;
	}

	if (sscanf(argv[1], "%u", &param_count) != 1) {
875
		ti->error = "Invalid mirror log argument count";
L
Linus Torvalds 已提交
876 877 878 879 880 881
		return NULL;
	}

	*args_used = 2 + param_count;

	if (argc < *args_used) {
882
		ti->error = "Insufficient mirror log arguments";
L
Linus Torvalds 已提交
883 884 885
		return NULL;
	}

H
Heinz Mauelshagen 已提交
886
	dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
L
Linus Torvalds 已提交
887
	if (!dl) {
888
		ti->error = "Error creating mirror dirty log";
L
Linus Torvalds 已提交
889 890 891 892 893 894
		return NULL;
	}

	return dl;
}

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
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 已提交
932 933 934 935 936
/*
 * Construct a mirror mapping:
 *
 * log_type #log_params <log_params>
 * #mirrors [mirror_path offset]{2,}
937
 * [#features <features>]
L
Linus Torvalds 已提交
938 939 940
 *
 * log_type is "core" or "disk"
 * #log_params is between 1 and 3
941 942
 *
 * If present, features must be "handle_errors".
L
Linus Torvalds 已提交
943 944 945 946 947 948
 */
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 已提交
949
	struct dm_dirty_log *dl;
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958

	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 已提交
959
	    nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
960
		ti->error = "Invalid number of mirrors";
H
Heinz Mauelshagen 已提交
961
		dm_dirty_log_destroy(dl);
L
Linus Torvalds 已提交
962 963 964 965 966
		return -EINVAL;
	}

	argv++, argc--;

967 968
	if (argc < nr_mirrors * 2) {
		ti->error = "Too few mirror arguments";
H
Heinz Mauelshagen 已提交
969
		dm_dirty_log_destroy(dl);
L
Linus Torvalds 已提交
970 971 972 973 974
		return -EINVAL;
	}

	ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
	if (!ms) {
H
Heinz Mauelshagen 已提交
975
		dm_dirty_log_destroy(dl);
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		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;
991
	ti->split_io = dm_rh_get_region_size(ms->rh);
L
Linus Torvalds 已提交
992

993 994 995
	ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
	if (!ms->kmirrord_wq) {
		DMERR("couldn't start kmirrord");
D
Dmitry Monakhov 已提交
996 997
		r = -ENOMEM;
		goto err_free_context;
998 999
	}
	INIT_WORK(&ms->kmirrord_work, do_mirror);
M
Mikulas Patocka 已提交
1000 1001
	init_timer(&ms->timer);
	ms->timer_pending = 0;
1002
	INIT_WORK(&ms->trigger_event, trigger_event);
1003

1004
	r = parse_features(ms, argc, argv, &args_used);
D
Dmitry Monakhov 已提交
1005 1006
	if (r)
		goto err_destroy_wq;
1007 1008 1009 1010

	argv += args_used;
	argc -= args_used;

1011 1012 1013 1014 1015 1016 1017 1018 1019
	/*
	 * 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.
	 */

1020 1021
	if (argc) {
		ti->error = "Too many mirror arguments";
D
Dmitry Monakhov 已提交
1022 1023
		r = -EINVAL;
		goto err_destroy_wq;
1024 1025
	}

1026
	r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
D
Dmitry Monakhov 已提交
1027 1028
	if (r)
		goto err_destroy_wq;
L
Linus Torvalds 已提交
1029

1030
	wakeup_mirrord(ms);
L
Linus Torvalds 已提交
1031
	return 0;
D
Dmitry Monakhov 已提交
1032 1033 1034 1035 1036 1037

err_destroy_wq:
	destroy_workqueue(ms->kmirrord_wq);
err_free_context:
	free_context(ms, ti, ms->nr_mirrors);
	return r;
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043
}

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

M
Mikulas Patocka 已提交
1044
	del_timer_sync(&ms->timer);
1045
	flush_workqueue(ms->kmirrord_wq);
1046
	flush_scheduled_work();
H
Heinz Mauelshagen 已提交
1047
	dm_kcopyd_client_destroy(ms->kcopyd_client);
1048
	destroy_workqueue(ms->kmirrord_wq);
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	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;
1061
	struct dm_raid1_read_record *read_record = NULL;
1062
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
L
Linus Torvalds 已提交
1063 1064

	if (rw == WRITE) {
1065
		/* Save region for mirror_end_io() handler */
1066
		map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
L
Linus Torvalds 已提交
1067
		queue_bio(ms, bio, rw);
1068
		return DM_MAPIO_SUBMITTED;
L
Linus Torvalds 已提交
1069 1070
	}

1071
	r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
L
Linus Torvalds 已提交
1072 1073 1074 1075
	if (r < 0 && r != -EWOULDBLOCK)
		return r;

	/*
1076
	 * If region is not in-sync queue the bio.
L
Linus Torvalds 已提交
1077
	 */
1078 1079 1080
	if (!r || (r == -EWOULDBLOCK)) {
		if (rw == READA)
			return -EWOULDBLOCK;
L
Linus Torvalds 已提交
1081 1082

		queue_bio(ms, bio, rw);
1083
		return DM_MAPIO_SUBMITTED;
L
Linus Torvalds 已提交
1084 1085
	}

1086 1087 1088 1089
	/*
	 * The region is in-sync and we can perform reads directly.
	 * Store enough information so we can retry if it fails.
	 */
L
Linus Torvalds 已提交
1090
	m = choose_mirror(ms, bio->bi_sector);
1091
	if (unlikely(!m))
L
Linus Torvalds 已提交
1092 1093
		return -EIO;

1094 1095 1096 1097 1098 1099 1100 1101 1102
	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);

1103
	return DM_MAPIO_REMAPPED;
L
Linus Torvalds 已提交
1104 1105 1106 1107 1108 1109 1110
}

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;
1111 1112 1113
	struct mirror *m = NULL;
	struct dm_bio_details *bd = NULL;
	struct dm_raid1_read_record *read_record = map_context->ptr;
L
Linus Torvalds 已提交
1114 1115 1116 1117

	/*
	 * We need to dec pending if this was a write.
	 */
1118
	if (rw == WRITE) {
1119
		dm_rh_dec(ms->rh, map_context->ll);
1120 1121
		return error;
	}
L
Linus Torvalds 已提交
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	if (error == -EOPNOTSUPP)
		goto out;

	if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
		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 已提交
1136
			DMERR_LIMIT("Mirror read failed.");
1137 1138
			return -EIO;
		}
A
Adrian Bunk 已提交
1139 1140 1141

		m = read_record->m;

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
		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 已提交
1170 1171
}

1172
static void mirror_presuspend(struct dm_target *ti)
L
Linus Torvalds 已提交
1173 1174
{
	struct mirror_set *ms = (struct mirror_set *) ti->private;
1175
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
L
Linus Torvalds 已提交
1176

1177 1178 1179 1180 1181 1182
	atomic_set(&ms->suspend, 1);

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

	wait_event(_kmirrord_recovery_stopped,
1186
		   !dm_rh_recovery_in_flight(ms->rh));
1187

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	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);
}

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

J
Jonathan Brassow 已提交
1206
	if (log->type->postsuspend && log->type->postsuspend(log))
L
Linus Torvalds 已提交
1207
		/* FIXME: need better error handling */
1208
		DMWARN("log postsuspend failed");
L
Linus Torvalds 已提交
1209 1210 1211 1212
}

static void mirror_resume(struct dm_target *ti)
{
1213
	struct mirror_set *ms = ti->private;
1214
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1215 1216

	atomic_set(&ms->suspend, 0);
L
Linus Torvalds 已提交
1217 1218 1219
	if (log->type->resume && log->type->resume(log))
		/* FIXME: need better error handling */
		DMWARN("log resume failed");
1220
	dm_rh_start_recovery(ms->rh);
L
Linus Torvalds 已提交
1221 1222
}

J
Jonathan Brassow 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
/*
 * 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';

	return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
		(test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
		(test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
}


L
Linus Torvalds 已提交
1247 1248 1249
static int mirror_status(struct dm_target *ti, status_type_t type,
			 char *result, unsigned int maxlen)
{
J
Jonathan E Brassow 已提交
1250
	unsigned int m, sz = 0;
L
Linus Torvalds 已提交
1251
	struct mirror_set *ms = (struct mirror_set *) ti->private;
1252
	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
J
Jonathan Brassow 已提交
1253
	char buffer[ms->nr_mirrors + 1];
L
Linus Torvalds 已提交
1254 1255 1256 1257

	switch (type) {
	case STATUSTYPE_INFO:
		DMEMIT("%d ", ms->nr_mirrors);
J
Jonathan Brassow 已提交
1258
		for (m = 0; m < ms->nr_mirrors; m++) {
L
Linus Torvalds 已提交
1259
			DMEMIT("%s ", ms->mirror[m].dev->name);
J
Jonathan Brassow 已提交
1260 1261 1262
			buffer[m] = device_status_char(&(ms->mirror[m]));
		}
		buffer[m] = '\0';
L
Linus Torvalds 已提交
1263

J
Jonathan Brassow 已提交
1264
		DMEMIT("%llu/%llu 1 %s ",
1265
		      (unsigned long long)log->type->get_sync_count(log),
J
Jonathan Brassow 已提交
1266
		      (unsigned long long)ms->nr_regions, buffer);
J
Jonathan E Brassow 已提交
1267

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

L
Linus Torvalds 已提交
1270 1271 1272
		break;

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

1275
		DMEMIT("%d", ms->nr_mirrors);
L
Linus Torvalds 已提交
1276
		for (m = 0; m < ms->nr_mirrors; m++)
1277
			DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1278
			       (unsigned long long)ms->mirror[m].offset);
1279 1280 1281

		if (ms->features & DM_RAID1_HANDLE_ERRORS)
			DMEMIT(" 1 handle_errors");
L
Linus Torvalds 已提交
1282 1283 1284 1285 1286
	}

	return 0;
}

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
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,
			 ms->mirror[i].offset, data);

	return ret;
}

L
Linus Torvalds 已提交
1301 1302
static struct target_type mirror_target = {
	.name	 = "mirror",
1303
	.version = {1, 12, 0},
L
Linus Torvalds 已提交
1304 1305 1306 1307 1308
	.module	 = THIS_MODULE,
	.ctr	 = mirror_ctr,
	.dtr	 = mirror_dtr,
	.map	 = mirror_map,
	.end_io	 = mirror_end_io,
1309
	.presuspend = mirror_presuspend,
L
Linus Torvalds 已提交
1310 1311 1312
	.postsuspend = mirror_postsuspend,
	.resume	 = mirror_resume,
	.status	 = mirror_status,
1313
	.iterate_devices = mirror_iterate_devices,
L
Linus Torvalds 已提交
1314 1315 1316 1317 1318 1319
};

static int __init dm_mirror_init(void)
{
	int r;

1320 1321 1322 1323 1324 1325 1326
	_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 已提交
1327
	r = dm_register_target(&mirror_target);
1328
	if (r < 0) {
1329
		DMERR("Failed to register mirror target");
1330 1331 1332 1333
		goto bad_target;
	}

	return 0;
L
Linus Torvalds 已提交
1334

1335 1336 1337
bad_target:
	kmem_cache_destroy(_dm_raid1_read_record_cache);
bad_cache:
L
Linus Torvalds 已提交
1338 1339 1340 1341 1342
	return r;
}

static void __exit dm_mirror_exit(void)
{
1343
	dm_unregister_target(&mirror_target);
1344
	kmem_cache_destroy(_dm_raid1_read_record_cache);
L
Linus Torvalds 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353
}

/* 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");