xref: /linux/drivers/md/dm-raid1.c (revision 56d06fa29edd58c448766014afd833b7ff51247b)
1 /*
2  * Copyright (C) 2003 Sistina Software Limited.
3  * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the GPL.
6  */
7 
8 #include "dm-bio-record.h"
9 
10 #include <linux/init.h>
11 #include <linux/mempool.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/device-mapper.h>
17 #include <linux/dm-io.h>
18 #include <linux/dm-dirty-log.h>
19 #include <linux/dm-kcopyd.h>
20 #include <linux/dm-region-hash.h>
21 
22 #define DM_MSG_PREFIX "raid1"
23 
24 #define MAX_RECOVERY 1	/* Maximum number of regions recovered in parallel. */
25 
26 #define DM_RAID1_HANDLE_ERRORS	0x01
27 #define DM_RAID1_KEEP_LOG	0x02
28 #define errors_handled(p)	((p)->features & DM_RAID1_HANDLE_ERRORS)
29 #define keep_log(p)		((p)->features & DM_RAID1_KEEP_LOG)
30 
31 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
32 
33 /*-----------------------------------------------------------------
34  * Mirror set structures.
35  *---------------------------------------------------------------*/
36 enum dm_raid1_error {
37 	DM_RAID1_WRITE_ERROR,
38 	DM_RAID1_FLUSH_ERROR,
39 	DM_RAID1_SYNC_ERROR,
40 	DM_RAID1_READ_ERROR
41 };
42 
43 struct mirror {
44 	struct mirror_set *ms;
45 	atomic_t error_count;
46 	unsigned long error_type;
47 	struct dm_dev *dev;
48 	sector_t offset;
49 };
50 
51 struct mirror_set {
52 	struct dm_target *ti;
53 	struct list_head list;
54 
55 	uint64_t features;
56 
57 	spinlock_t lock;	/* protects the lists */
58 	struct bio_list reads;
59 	struct bio_list writes;
60 	struct bio_list failures;
61 	struct bio_list holds;	/* bios are waiting until suspend */
62 
63 	struct dm_region_hash *rh;
64 	struct dm_kcopyd_client *kcopyd_client;
65 	struct dm_io_client *io_client;
66 
67 	/* recovery */
68 	region_t nr_regions;
69 	int in_sync;
70 	int log_failure;
71 	int leg_failure;
72 	atomic_t suspend;
73 
74 	atomic_t default_mirror;	/* Default mirror */
75 
76 	struct workqueue_struct *kmirrord_wq;
77 	struct work_struct kmirrord_work;
78 	struct timer_list timer;
79 	unsigned long timer_pending;
80 
81 	struct work_struct trigger_event;
82 
83 	unsigned nr_mirrors;
84 	struct mirror mirror[0];
85 };
86 
87 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
88 		"A percentage of time allocated for raid resynchronization");
89 
90 static void wakeup_mirrord(void *context)
91 {
92 	struct mirror_set *ms = context;
93 
94 	queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
95 }
96 
97 static void delayed_wake_fn(unsigned long data)
98 {
99 	struct mirror_set *ms = (struct mirror_set *) data;
100 
101 	clear_bit(0, &ms->timer_pending);
102 	wakeup_mirrord(ms);
103 }
104 
105 static void delayed_wake(struct mirror_set *ms)
106 {
107 	if (test_and_set_bit(0, &ms->timer_pending))
108 		return;
109 
110 	ms->timer.expires = jiffies + HZ / 5;
111 	ms->timer.data = (unsigned long) ms;
112 	ms->timer.function = delayed_wake_fn;
113 	add_timer(&ms->timer);
114 }
115 
116 static void wakeup_all_recovery_waiters(void *context)
117 {
118 	wake_up_all(&_kmirrord_recovery_stopped);
119 }
120 
121 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
122 {
123 	unsigned long flags;
124 	int should_wake = 0;
125 	struct bio_list *bl;
126 
127 	bl = (rw == WRITE) ? &ms->writes : &ms->reads;
128 	spin_lock_irqsave(&ms->lock, flags);
129 	should_wake = !(bl->head);
130 	bio_list_add(bl, bio);
131 	spin_unlock_irqrestore(&ms->lock, flags);
132 
133 	if (should_wake)
134 		wakeup_mirrord(ms);
135 }
136 
137 static void dispatch_bios(void *context, struct bio_list *bio_list)
138 {
139 	struct mirror_set *ms = context;
140 	struct bio *bio;
141 
142 	while ((bio = bio_list_pop(bio_list)))
143 		queue_bio(ms, bio, WRITE);
144 }
145 
146 struct dm_raid1_bio_record {
147 	struct mirror *m;
148 	/* if details->bi_bdev == NULL, details were not saved */
149 	struct dm_bio_details details;
150 	region_t write_region;
151 };
152 
153 /*
154  * Every mirror should look like this one.
155  */
156 #define DEFAULT_MIRROR 0
157 
158 /*
159  * This is yucky.  We squirrel the mirror struct away inside
160  * bi_next for read/write buffers.  This is safe since the bh
161  * doesn't get submitted to the lower levels of block layer.
162  */
163 static struct mirror *bio_get_m(struct bio *bio)
164 {
165 	return (struct mirror *) bio->bi_next;
166 }
167 
168 static void bio_set_m(struct bio *bio, struct mirror *m)
169 {
170 	bio->bi_next = (struct bio *) m;
171 }
172 
173 static struct mirror *get_default_mirror(struct mirror_set *ms)
174 {
175 	return &ms->mirror[atomic_read(&ms->default_mirror)];
176 }
177 
178 static void set_default_mirror(struct mirror *m)
179 {
180 	struct mirror_set *ms = m->ms;
181 	struct mirror *m0 = &(ms->mirror[0]);
182 
183 	atomic_set(&ms->default_mirror, m - m0);
184 }
185 
186 static struct mirror *get_valid_mirror(struct mirror_set *ms)
187 {
188 	struct mirror *m;
189 
190 	for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
191 		if (!atomic_read(&m->error_count))
192 			return m;
193 
194 	return NULL;
195 }
196 
197 /* fail_mirror
198  * @m: mirror device to fail
199  * @error_type: one of the enum's, DM_RAID1_*_ERROR
200  *
201  * If errors are being handled, record the type of
202  * error encountered for this device.  If this type
203  * of error has already been recorded, we can return;
204  * otherwise, we must signal userspace by triggering
205  * an event.  Additionally, if the device is the
206  * primary device, we must choose a new primary, but
207  * only if the mirror is in-sync.
208  *
209  * This function must not block.
210  */
211 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
212 {
213 	struct mirror_set *ms = m->ms;
214 	struct mirror *new;
215 
216 	ms->leg_failure = 1;
217 
218 	/*
219 	 * error_count is used for nothing more than a
220 	 * simple way to tell if a device has encountered
221 	 * errors.
222 	 */
223 	atomic_inc(&m->error_count);
224 
225 	if (test_and_set_bit(error_type, &m->error_type))
226 		return;
227 
228 	if (!errors_handled(ms))
229 		return;
230 
231 	if (m != get_default_mirror(ms))
232 		goto out;
233 
234 	if (!ms->in_sync && !keep_log(ms)) {
235 		/*
236 		 * Better to issue requests to same failing device
237 		 * than to risk returning corrupt data.
238 		 */
239 		DMERR("Primary mirror (%s) failed while out-of-sync: "
240 		      "Reads may fail.", m->dev->name);
241 		goto out;
242 	}
243 
244 	new = get_valid_mirror(ms);
245 	if (new)
246 		set_default_mirror(new);
247 	else
248 		DMWARN("All sides of mirror have failed.");
249 
250 out:
251 	schedule_work(&ms->trigger_event);
252 }
253 
254 static int mirror_flush(struct dm_target *ti)
255 {
256 	struct mirror_set *ms = ti->private;
257 	unsigned long error_bits;
258 
259 	unsigned int i;
260 	struct dm_io_region io[ms->nr_mirrors];
261 	struct mirror *m;
262 	struct dm_io_request io_req = {
263 		.bi_rw = WRITE_FLUSH,
264 		.mem.type = DM_IO_KMEM,
265 		.mem.ptr.addr = NULL,
266 		.client = ms->io_client,
267 	};
268 
269 	for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
270 		io[i].bdev = m->dev->bdev;
271 		io[i].sector = 0;
272 		io[i].count = 0;
273 	}
274 
275 	error_bits = -1;
276 	dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
277 	if (unlikely(error_bits != 0)) {
278 		for (i = 0; i < ms->nr_mirrors; i++)
279 			if (test_bit(i, &error_bits))
280 				fail_mirror(ms->mirror + i,
281 					    DM_RAID1_FLUSH_ERROR);
282 		return -EIO;
283 	}
284 
285 	return 0;
286 }
287 
288 /*-----------------------------------------------------------------
289  * Recovery.
290  *
291  * When a mirror is first activated we may find that some regions
292  * are in the no-sync state.  We have to recover these by
293  * recopying from the default mirror to all the others.
294  *---------------------------------------------------------------*/
295 static void recovery_complete(int read_err, unsigned long write_err,
296 			      void *context)
297 {
298 	struct dm_region *reg = context;
299 	struct mirror_set *ms = dm_rh_region_context(reg);
300 	int m, bit = 0;
301 
302 	if (read_err) {
303 		/* Read error means the failure of default mirror. */
304 		DMERR_LIMIT("Unable to read primary mirror during recovery");
305 		fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
306 	}
307 
308 	if (write_err) {
309 		DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
310 			    write_err);
311 		/*
312 		 * Bits correspond to devices (excluding default mirror).
313 		 * The default mirror cannot change during recovery.
314 		 */
315 		for (m = 0; m < ms->nr_mirrors; m++) {
316 			if (&ms->mirror[m] == get_default_mirror(ms))
317 				continue;
318 			if (test_bit(bit, &write_err))
319 				fail_mirror(ms->mirror + m,
320 					    DM_RAID1_SYNC_ERROR);
321 			bit++;
322 		}
323 	}
324 
325 	dm_rh_recovery_end(reg, !(read_err || write_err));
326 }
327 
328 static int recover(struct mirror_set *ms, struct dm_region *reg)
329 {
330 	int r;
331 	unsigned i;
332 	struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
333 	struct mirror *m;
334 	unsigned long flags = 0;
335 	region_t key = dm_rh_get_region_key(reg);
336 	sector_t region_size = dm_rh_get_region_size(ms->rh);
337 
338 	/* fill in the source */
339 	m = get_default_mirror(ms);
340 	from.bdev = m->dev->bdev;
341 	from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
342 	if (key == (ms->nr_regions - 1)) {
343 		/*
344 		 * The final region may be smaller than
345 		 * region_size.
346 		 */
347 		from.count = ms->ti->len & (region_size - 1);
348 		if (!from.count)
349 			from.count = region_size;
350 	} else
351 		from.count = region_size;
352 
353 	/* fill in the destinations */
354 	for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
355 		if (&ms->mirror[i] == get_default_mirror(ms))
356 			continue;
357 
358 		m = ms->mirror + i;
359 		dest->bdev = m->dev->bdev;
360 		dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
361 		dest->count = from.count;
362 		dest++;
363 	}
364 
365 	/* hand to kcopyd */
366 	if (!errors_handled(ms))
367 		set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
368 
369 	r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
370 			   flags, recovery_complete, reg);
371 
372 	return r;
373 }
374 
375 static void reset_ms_flags(struct mirror_set *ms)
376 {
377 	unsigned int m;
378 
379 	ms->leg_failure = 0;
380 	for (m = 0; m < ms->nr_mirrors; m++) {
381 		atomic_set(&(ms->mirror[m].error_count), 0);
382 		ms->mirror[m].error_type = 0;
383 	}
384 }
385 
386 static void do_recovery(struct mirror_set *ms)
387 {
388 	struct dm_region *reg;
389 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
390 	int r;
391 
392 	/*
393 	 * Start quiescing some regions.
394 	 */
395 	dm_rh_recovery_prepare(ms->rh);
396 
397 	/*
398 	 * Copy any already quiesced regions.
399 	 */
400 	while ((reg = dm_rh_recovery_start(ms->rh))) {
401 		r = recover(ms, reg);
402 		if (r)
403 			dm_rh_recovery_end(reg, 0);
404 	}
405 
406 	/*
407 	 * Update the in sync flag.
408 	 */
409 	if (!ms->in_sync &&
410 	    (log->type->get_sync_count(log) == ms->nr_regions)) {
411 		/* the sync is complete */
412 		dm_table_event(ms->ti->table);
413 		ms->in_sync = 1;
414 		reset_ms_flags(ms);
415 	}
416 }
417 
418 /*-----------------------------------------------------------------
419  * Reads
420  *---------------------------------------------------------------*/
421 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
422 {
423 	struct mirror *m = get_default_mirror(ms);
424 
425 	do {
426 		if (likely(!atomic_read(&m->error_count)))
427 			return m;
428 
429 		if (m-- == ms->mirror)
430 			m += ms->nr_mirrors;
431 	} while (m != get_default_mirror(ms));
432 
433 	return NULL;
434 }
435 
436 static int default_ok(struct mirror *m)
437 {
438 	struct mirror *default_mirror = get_default_mirror(m->ms);
439 
440 	return !atomic_read(&default_mirror->error_count);
441 }
442 
443 static int mirror_available(struct mirror_set *ms, struct bio *bio)
444 {
445 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
446 	region_t region = dm_rh_bio_to_region(ms->rh, bio);
447 
448 	if (log->type->in_sync(log, region, 0))
449 		return choose_mirror(ms,  bio->bi_iter.bi_sector) ? 1 : 0;
450 
451 	return 0;
452 }
453 
454 /*
455  * remap a buffer to a particular mirror.
456  */
457 static sector_t map_sector(struct mirror *m, struct bio *bio)
458 {
459 	if (unlikely(!bio->bi_iter.bi_size))
460 		return 0;
461 	return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
462 }
463 
464 static void map_bio(struct mirror *m, struct bio *bio)
465 {
466 	bio->bi_bdev = m->dev->bdev;
467 	bio->bi_iter.bi_sector = map_sector(m, bio);
468 }
469 
470 static void map_region(struct dm_io_region *io, struct mirror *m,
471 		       struct bio *bio)
472 {
473 	io->bdev = m->dev->bdev;
474 	io->sector = map_sector(m, bio);
475 	io->count = bio_sectors(bio);
476 }
477 
478 static void hold_bio(struct mirror_set *ms, struct bio *bio)
479 {
480 	/*
481 	 * Lock is required to avoid race condition during suspend
482 	 * process.
483 	 */
484 	spin_lock_irq(&ms->lock);
485 
486 	if (atomic_read(&ms->suspend)) {
487 		spin_unlock_irq(&ms->lock);
488 
489 		/*
490 		 * If device is suspended, complete the bio.
491 		 */
492 		if (dm_noflush_suspending(ms->ti))
493 			bio->bi_error = DM_ENDIO_REQUEUE;
494 		else
495 			bio->bi_error = -EIO;
496 
497 		bio_endio(bio);
498 		return;
499 	}
500 
501 	/*
502 	 * Hold bio until the suspend is complete.
503 	 */
504 	bio_list_add(&ms->holds, bio);
505 	spin_unlock_irq(&ms->lock);
506 }
507 
508 /*-----------------------------------------------------------------
509  * Reads
510  *---------------------------------------------------------------*/
511 static void read_callback(unsigned long error, void *context)
512 {
513 	struct bio *bio = context;
514 	struct mirror *m;
515 
516 	m = bio_get_m(bio);
517 	bio_set_m(bio, NULL);
518 
519 	if (likely(!error)) {
520 		bio_endio(bio);
521 		return;
522 	}
523 
524 	fail_mirror(m, DM_RAID1_READ_ERROR);
525 
526 	if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
527 		DMWARN_LIMIT("Read failure on mirror device %s.  "
528 			     "Trying alternative device.",
529 			     m->dev->name);
530 		queue_bio(m->ms, bio, bio_rw(bio));
531 		return;
532 	}
533 
534 	DMERR_LIMIT("Read failure on mirror device %s.  Failing I/O.",
535 		    m->dev->name);
536 	bio_io_error(bio);
537 }
538 
539 /* Asynchronous read. */
540 static void read_async_bio(struct mirror *m, struct bio *bio)
541 {
542 	struct dm_io_region io;
543 	struct dm_io_request io_req = {
544 		.bi_rw = READ,
545 		.mem.type = DM_IO_BIO,
546 		.mem.ptr.bio = bio,
547 		.notify.fn = read_callback,
548 		.notify.context = bio,
549 		.client = m->ms->io_client,
550 	};
551 
552 	map_region(&io, m, bio);
553 	bio_set_m(bio, m);
554 	BUG_ON(dm_io(&io_req, 1, &io, NULL));
555 }
556 
557 static inline int region_in_sync(struct mirror_set *ms, region_t region,
558 				 int may_block)
559 {
560 	int state = dm_rh_get_state(ms->rh, region, may_block);
561 	return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
562 }
563 
564 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
565 {
566 	region_t region;
567 	struct bio *bio;
568 	struct mirror *m;
569 
570 	while ((bio = bio_list_pop(reads))) {
571 		region = dm_rh_bio_to_region(ms->rh, bio);
572 		m = get_default_mirror(ms);
573 
574 		/*
575 		 * We can only read balance if the region is in sync.
576 		 */
577 		if (likely(region_in_sync(ms, region, 1)))
578 			m = choose_mirror(ms, bio->bi_iter.bi_sector);
579 		else if (m && atomic_read(&m->error_count))
580 			m = NULL;
581 
582 		if (likely(m))
583 			read_async_bio(m, bio);
584 		else
585 			bio_io_error(bio);
586 	}
587 }
588 
589 /*-----------------------------------------------------------------
590  * Writes.
591  *
592  * We do different things with the write io depending on the
593  * state of the region that it's in:
594  *
595  * SYNC: 	increment pending, use kcopyd to write to *all* mirrors
596  * RECOVERING:	delay the io until recovery completes
597  * NOSYNC:	increment pending, just write to the default mirror
598  *---------------------------------------------------------------*/
599 
600 
601 static void write_callback(unsigned long error, void *context)
602 {
603 	unsigned i;
604 	struct bio *bio = (struct bio *) context;
605 	struct mirror_set *ms;
606 	int should_wake = 0;
607 	unsigned long flags;
608 
609 	ms = bio_get_m(bio)->ms;
610 	bio_set_m(bio, NULL);
611 
612 	/*
613 	 * NOTE: We don't decrement the pending count here,
614 	 * instead it is done by the targets endio function.
615 	 * This way we handle both writes to SYNC and NOSYNC
616 	 * regions with the same code.
617 	 */
618 	if (likely(!error)) {
619 		bio_endio(bio);
620 		return;
621 	}
622 
623 	/*
624 	 * If the bio is discard, return an error, but do not
625 	 * degrade the array.
626 	 */
627 	if (bio->bi_rw & REQ_DISCARD) {
628 		bio->bi_error = -EOPNOTSUPP;
629 		bio_endio(bio);
630 		return;
631 	}
632 
633 	for (i = 0; i < ms->nr_mirrors; i++)
634 		if (test_bit(i, &error))
635 			fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
636 
637 	/*
638 	 * Need to raise event.  Since raising
639 	 * events can block, we need to do it in
640 	 * the main thread.
641 	 */
642 	spin_lock_irqsave(&ms->lock, flags);
643 	if (!ms->failures.head)
644 		should_wake = 1;
645 	bio_list_add(&ms->failures, bio);
646 	spin_unlock_irqrestore(&ms->lock, flags);
647 	if (should_wake)
648 		wakeup_mirrord(ms);
649 }
650 
651 static void do_write(struct mirror_set *ms, struct bio *bio)
652 {
653 	unsigned int i;
654 	struct dm_io_region io[ms->nr_mirrors], *dest = io;
655 	struct mirror *m;
656 	struct dm_io_request io_req = {
657 		.bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
658 		.mem.type = DM_IO_BIO,
659 		.mem.ptr.bio = bio,
660 		.notify.fn = write_callback,
661 		.notify.context = bio,
662 		.client = ms->io_client,
663 	};
664 
665 	if (bio->bi_rw & REQ_DISCARD) {
666 		io_req.bi_rw |= REQ_DISCARD;
667 		io_req.mem.type = DM_IO_KMEM;
668 		io_req.mem.ptr.addr = NULL;
669 	}
670 
671 	for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
672 		map_region(dest++, m, bio);
673 
674 	/*
675 	 * Use default mirror because we only need it to retrieve the reference
676 	 * to the mirror set in write_callback().
677 	 */
678 	bio_set_m(bio, get_default_mirror(ms));
679 
680 	BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
681 }
682 
683 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
684 {
685 	int state;
686 	struct bio *bio;
687 	struct bio_list sync, nosync, recover, *this_list = NULL;
688 	struct bio_list requeue;
689 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
690 	region_t region;
691 
692 	if (!writes->head)
693 		return;
694 
695 	/*
696 	 * Classify each write.
697 	 */
698 	bio_list_init(&sync);
699 	bio_list_init(&nosync);
700 	bio_list_init(&recover);
701 	bio_list_init(&requeue);
702 
703 	while ((bio = bio_list_pop(writes))) {
704 		if ((bio->bi_rw & REQ_FLUSH) ||
705 		    (bio->bi_rw & REQ_DISCARD)) {
706 			bio_list_add(&sync, bio);
707 			continue;
708 		}
709 
710 		region = dm_rh_bio_to_region(ms->rh, bio);
711 
712 		if (log->type->is_remote_recovering &&
713 		    log->type->is_remote_recovering(log, region)) {
714 			bio_list_add(&requeue, bio);
715 			continue;
716 		}
717 
718 		state = dm_rh_get_state(ms->rh, region, 1);
719 		switch (state) {
720 		case DM_RH_CLEAN:
721 		case DM_RH_DIRTY:
722 			this_list = &sync;
723 			break;
724 
725 		case DM_RH_NOSYNC:
726 			this_list = &nosync;
727 			break;
728 
729 		case DM_RH_RECOVERING:
730 			this_list = &recover;
731 			break;
732 		}
733 
734 		bio_list_add(this_list, bio);
735 	}
736 
737 	/*
738 	 * Add bios that are delayed due to remote recovery
739 	 * back on to the write queue
740 	 */
741 	if (unlikely(requeue.head)) {
742 		spin_lock_irq(&ms->lock);
743 		bio_list_merge(&ms->writes, &requeue);
744 		spin_unlock_irq(&ms->lock);
745 		delayed_wake(ms);
746 	}
747 
748 	/*
749 	 * Increment the pending counts for any regions that will
750 	 * be written to (writes to recover regions are going to
751 	 * be delayed).
752 	 */
753 	dm_rh_inc_pending(ms->rh, &sync);
754 	dm_rh_inc_pending(ms->rh, &nosync);
755 
756 	/*
757 	 * If the flush fails on a previous call and succeeds here,
758 	 * we must not reset the log_failure variable.  We need
759 	 * userspace interaction to do that.
760 	 */
761 	ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
762 
763 	/*
764 	 * Dispatch io.
765 	 */
766 	if (unlikely(ms->log_failure) && errors_handled(ms)) {
767 		spin_lock_irq(&ms->lock);
768 		bio_list_merge(&ms->failures, &sync);
769 		spin_unlock_irq(&ms->lock);
770 		wakeup_mirrord(ms);
771 	} else
772 		while ((bio = bio_list_pop(&sync)))
773 			do_write(ms, bio);
774 
775 	while ((bio = bio_list_pop(&recover)))
776 		dm_rh_delay(ms->rh, bio);
777 
778 	while ((bio = bio_list_pop(&nosync))) {
779 		if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
780 			spin_lock_irq(&ms->lock);
781 			bio_list_add(&ms->failures, bio);
782 			spin_unlock_irq(&ms->lock);
783 			wakeup_mirrord(ms);
784 		} else {
785 			map_bio(get_default_mirror(ms), bio);
786 			generic_make_request(bio);
787 		}
788 	}
789 }
790 
791 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
792 {
793 	struct bio *bio;
794 
795 	if (likely(!failures->head))
796 		return;
797 
798 	/*
799 	 * If the log has failed, unattempted writes are being
800 	 * put on the holds list.  We can't issue those writes
801 	 * until a log has been marked, so we must store them.
802 	 *
803 	 * If a 'noflush' suspend is in progress, we can requeue
804 	 * the I/O's to the core.  This give userspace a chance
805 	 * to reconfigure the mirror, at which point the core
806 	 * will reissue the writes.  If the 'noflush' flag is
807 	 * not set, we have no choice but to return errors.
808 	 *
809 	 * Some writes on the failures list may have been
810 	 * submitted before the log failure and represent a
811 	 * failure to write to one of the devices.  It is ok
812 	 * for us to treat them the same and requeue them
813 	 * as well.
814 	 */
815 	while ((bio = bio_list_pop(failures))) {
816 		if (!ms->log_failure) {
817 			ms->in_sync = 0;
818 			dm_rh_mark_nosync(ms->rh, bio);
819 		}
820 
821 		/*
822 		 * If all the legs are dead, fail the I/O.
823 		 * If the device has failed and keep_log is enabled,
824 		 * fail the I/O.
825 		 *
826 		 * If we have been told to handle errors, and keep_log
827 		 * isn't enabled, hold the bio and wait for userspace to
828 		 * deal with the problem.
829 		 *
830 		 * Otherwise pretend that the I/O succeeded. (This would
831 		 * be wrong if the failed leg returned after reboot and
832 		 * got replicated back to the good legs.)
833 		 */
834 		if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
835 			bio_io_error(bio);
836 		else if (errors_handled(ms) && !keep_log(ms))
837 			hold_bio(ms, bio);
838 		else
839 			bio_endio(bio);
840 	}
841 }
842 
843 static void trigger_event(struct work_struct *work)
844 {
845 	struct mirror_set *ms =
846 		container_of(work, struct mirror_set, trigger_event);
847 
848 	dm_table_event(ms->ti->table);
849 }
850 
851 /*-----------------------------------------------------------------
852  * kmirrord
853  *---------------------------------------------------------------*/
854 static void do_mirror(struct work_struct *work)
855 {
856 	struct mirror_set *ms = container_of(work, struct mirror_set,
857 					     kmirrord_work);
858 	struct bio_list reads, writes, failures;
859 	unsigned long flags;
860 
861 	spin_lock_irqsave(&ms->lock, flags);
862 	reads = ms->reads;
863 	writes = ms->writes;
864 	failures = ms->failures;
865 	bio_list_init(&ms->reads);
866 	bio_list_init(&ms->writes);
867 	bio_list_init(&ms->failures);
868 	spin_unlock_irqrestore(&ms->lock, flags);
869 
870 	dm_rh_update_states(ms->rh, errors_handled(ms));
871 	do_recovery(ms);
872 	do_reads(ms, &reads);
873 	do_writes(ms, &writes);
874 	do_failures(ms, &failures);
875 }
876 
877 /*-----------------------------------------------------------------
878  * Target functions
879  *---------------------------------------------------------------*/
880 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
881 					uint32_t region_size,
882 					struct dm_target *ti,
883 					struct dm_dirty_log *dl)
884 {
885 	size_t len;
886 	struct mirror_set *ms = NULL;
887 
888 	len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
889 
890 	ms = kzalloc(len, GFP_KERNEL);
891 	if (!ms) {
892 		ti->error = "Cannot allocate mirror context";
893 		return NULL;
894 	}
895 
896 	spin_lock_init(&ms->lock);
897 	bio_list_init(&ms->reads);
898 	bio_list_init(&ms->writes);
899 	bio_list_init(&ms->failures);
900 	bio_list_init(&ms->holds);
901 
902 	ms->ti = ti;
903 	ms->nr_mirrors = nr_mirrors;
904 	ms->nr_regions = dm_sector_div_up(ti->len, region_size);
905 	ms->in_sync = 0;
906 	ms->log_failure = 0;
907 	ms->leg_failure = 0;
908 	atomic_set(&ms->suspend, 0);
909 	atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
910 
911 	ms->io_client = dm_io_client_create();
912 	if (IS_ERR(ms->io_client)) {
913 		ti->error = "Error creating dm_io client";
914 		kfree(ms);
915  		return NULL;
916 	}
917 
918 	ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
919 				       wakeup_all_recovery_waiters,
920 				       ms->ti->begin, MAX_RECOVERY,
921 				       dl, region_size, ms->nr_regions);
922 	if (IS_ERR(ms->rh)) {
923 		ti->error = "Error creating dirty region hash";
924 		dm_io_client_destroy(ms->io_client);
925 		kfree(ms);
926 		return NULL;
927 	}
928 
929 	return ms;
930 }
931 
932 static void free_context(struct mirror_set *ms, struct dm_target *ti,
933 			 unsigned int m)
934 {
935 	while (m--)
936 		dm_put_device(ti, ms->mirror[m].dev);
937 
938 	dm_io_client_destroy(ms->io_client);
939 	dm_region_hash_destroy(ms->rh);
940 	kfree(ms);
941 }
942 
943 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
944 		      unsigned int mirror, char **argv)
945 {
946 	unsigned long long offset;
947 	char dummy;
948 	int ret;
949 
950 	if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) {
951 		ti->error = "Invalid offset";
952 		return -EINVAL;
953 	}
954 
955 	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
956 			    &ms->mirror[mirror].dev);
957 	if (ret) {
958 		ti->error = "Device lookup failure";
959 		return ret;
960 	}
961 
962 	ms->mirror[mirror].ms = ms;
963 	atomic_set(&(ms->mirror[mirror].error_count), 0);
964 	ms->mirror[mirror].error_type = 0;
965 	ms->mirror[mirror].offset = offset;
966 
967 	return 0;
968 }
969 
970 /*
971  * Create dirty log: log_type #log_params <log_params>
972  */
973 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
974 					     unsigned argc, char **argv,
975 					     unsigned *args_used)
976 {
977 	unsigned param_count;
978 	struct dm_dirty_log *dl;
979 	char dummy;
980 
981 	if (argc < 2) {
982 		ti->error = "Insufficient mirror log arguments";
983 		return NULL;
984 	}
985 
986 	if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
987 		ti->error = "Invalid mirror log argument count";
988 		return NULL;
989 	}
990 
991 	*args_used = 2 + param_count;
992 
993 	if (argc < *args_used) {
994 		ti->error = "Insufficient mirror log arguments";
995 		return NULL;
996 	}
997 
998 	dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
999 				 argv + 2);
1000 	if (!dl) {
1001 		ti->error = "Error creating mirror dirty log";
1002 		return NULL;
1003 	}
1004 
1005 	return dl;
1006 }
1007 
1008 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1009 			  unsigned *args_used)
1010 {
1011 	unsigned num_features;
1012 	struct dm_target *ti = ms->ti;
1013 	char dummy;
1014 	int i;
1015 
1016 	*args_used = 0;
1017 
1018 	if (!argc)
1019 		return 0;
1020 
1021 	if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
1022 		ti->error = "Invalid number of features";
1023 		return -EINVAL;
1024 	}
1025 
1026 	argc--;
1027 	argv++;
1028 	(*args_used)++;
1029 
1030 	if (num_features > argc) {
1031 		ti->error = "Not enough arguments to support feature count";
1032 		return -EINVAL;
1033 	}
1034 
1035 	for (i = 0; i < num_features; i++) {
1036 		if (!strcmp("handle_errors", argv[0]))
1037 			ms->features |= DM_RAID1_HANDLE_ERRORS;
1038 		else if (!strcmp("keep_log", argv[0]))
1039 			ms->features |= DM_RAID1_KEEP_LOG;
1040 		else {
1041 			ti->error = "Unrecognised feature requested";
1042 			return -EINVAL;
1043 		}
1044 
1045 		argc--;
1046 		argv++;
1047 		(*args_used)++;
1048 	}
1049 	if (!errors_handled(ms) && keep_log(ms)) {
1050 		ti->error = "keep_log feature requires the handle_errors feature";
1051 		return -EINVAL;
1052 	}
1053 
1054 	return 0;
1055 }
1056 
1057 /*
1058  * Construct a mirror mapping:
1059  *
1060  * log_type #log_params <log_params>
1061  * #mirrors [mirror_path offset]{2,}
1062  * [#features <features>]
1063  *
1064  * log_type is "core" or "disk"
1065  * #log_params is between 1 and 3
1066  *
1067  * If present, supported features are "handle_errors" and "keep_log".
1068  */
1069 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1070 {
1071 	int r;
1072 	unsigned int nr_mirrors, m, args_used;
1073 	struct mirror_set *ms;
1074 	struct dm_dirty_log *dl;
1075 	char dummy;
1076 
1077 	dl = create_dirty_log(ti, argc, argv, &args_used);
1078 	if (!dl)
1079 		return -EINVAL;
1080 
1081 	argv += args_used;
1082 	argc -= args_used;
1083 
1084 	if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
1085 	    nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
1086 		ti->error = "Invalid number of mirrors";
1087 		dm_dirty_log_destroy(dl);
1088 		return -EINVAL;
1089 	}
1090 
1091 	argv++, argc--;
1092 
1093 	if (argc < nr_mirrors * 2) {
1094 		ti->error = "Too few mirror arguments";
1095 		dm_dirty_log_destroy(dl);
1096 		return -EINVAL;
1097 	}
1098 
1099 	ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1100 	if (!ms) {
1101 		dm_dirty_log_destroy(dl);
1102 		return -ENOMEM;
1103 	}
1104 
1105 	/* Get the mirror parameter sets */
1106 	for (m = 0; m < nr_mirrors; m++) {
1107 		r = get_mirror(ms, ti, m, argv);
1108 		if (r) {
1109 			free_context(ms, ti, m);
1110 			return r;
1111 		}
1112 		argv += 2;
1113 		argc -= 2;
1114 	}
1115 
1116 	ti->private = ms;
1117 
1118 	r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1119 	if (r)
1120 		goto err_free_context;
1121 
1122 	ti->num_flush_bios = 1;
1123 	ti->num_discard_bios = 1;
1124 	ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
1125 	ti->discard_zeroes_data_unsupported = true;
1126 
1127 	ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
1128 	if (!ms->kmirrord_wq) {
1129 		DMERR("couldn't start kmirrord");
1130 		r = -ENOMEM;
1131 		goto err_free_context;
1132 	}
1133 	INIT_WORK(&ms->kmirrord_work, do_mirror);
1134 	init_timer(&ms->timer);
1135 	ms->timer_pending = 0;
1136 	INIT_WORK(&ms->trigger_event, trigger_event);
1137 
1138 	r = parse_features(ms, argc, argv, &args_used);
1139 	if (r)
1140 		goto err_destroy_wq;
1141 
1142 	argv += args_used;
1143 	argc -= args_used;
1144 
1145 	/*
1146 	 * Any read-balancing addition depends on the
1147 	 * DM_RAID1_HANDLE_ERRORS flag being present.
1148 	 * This is because the decision to balance depends
1149 	 * on the sync state of a region.  If the above
1150 	 * flag is not present, we ignore errors; and
1151 	 * the sync state may be inaccurate.
1152 	 */
1153 
1154 	if (argc) {
1155 		ti->error = "Too many mirror arguments";
1156 		r = -EINVAL;
1157 		goto err_destroy_wq;
1158 	}
1159 
1160 	ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1161 	if (IS_ERR(ms->kcopyd_client)) {
1162 		r = PTR_ERR(ms->kcopyd_client);
1163 		goto err_destroy_wq;
1164 	}
1165 
1166 	wakeup_mirrord(ms);
1167 	return 0;
1168 
1169 err_destroy_wq:
1170 	destroy_workqueue(ms->kmirrord_wq);
1171 err_free_context:
1172 	free_context(ms, ti, ms->nr_mirrors);
1173 	return r;
1174 }
1175 
1176 static void mirror_dtr(struct dm_target *ti)
1177 {
1178 	struct mirror_set *ms = (struct mirror_set *) ti->private;
1179 
1180 	del_timer_sync(&ms->timer);
1181 	flush_workqueue(ms->kmirrord_wq);
1182 	flush_work(&ms->trigger_event);
1183 	dm_kcopyd_client_destroy(ms->kcopyd_client);
1184 	destroy_workqueue(ms->kmirrord_wq);
1185 	free_context(ms, ti, ms->nr_mirrors);
1186 }
1187 
1188 /*
1189  * Mirror mapping function
1190  */
1191 static int mirror_map(struct dm_target *ti, struct bio *bio)
1192 {
1193 	int r, rw = bio_rw(bio);
1194 	struct mirror *m;
1195 	struct mirror_set *ms = ti->private;
1196 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1197 	struct dm_raid1_bio_record *bio_record =
1198 	  dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1199 
1200 	bio_record->details.bi_bdev = NULL;
1201 
1202 	if (rw == WRITE) {
1203 		/* Save region for mirror_end_io() handler */
1204 		bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
1205 		queue_bio(ms, bio, rw);
1206 		return DM_MAPIO_SUBMITTED;
1207 	}
1208 
1209 	r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1210 	if (r < 0 && r != -EWOULDBLOCK)
1211 		return r;
1212 
1213 	/*
1214 	 * If region is not in-sync queue the bio.
1215 	 */
1216 	if (!r || (r == -EWOULDBLOCK)) {
1217 		if (rw == READA)
1218 			return -EWOULDBLOCK;
1219 
1220 		queue_bio(ms, bio, rw);
1221 		return DM_MAPIO_SUBMITTED;
1222 	}
1223 
1224 	/*
1225 	 * The region is in-sync and we can perform reads directly.
1226 	 * Store enough information so we can retry if it fails.
1227 	 */
1228 	m = choose_mirror(ms, bio->bi_iter.bi_sector);
1229 	if (unlikely(!m))
1230 		return -EIO;
1231 
1232 	dm_bio_record(&bio_record->details, bio);
1233 	bio_record->m = m;
1234 
1235 	map_bio(m, bio);
1236 
1237 	return DM_MAPIO_REMAPPED;
1238 }
1239 
1240 static int mirror_end_io(struct dm_target *ti, struct bio *bio, int error)
1241 {
1242 	int rw = bio_rw(bio);
1243 	struct mirror_set *ms = (struct mirror_set *) ti->private;
1244 	struct mirror *m = NULL;
1245 	struct dm_bio_details *bd = NULL;
1246 	struct dm_raid1_bio_record *bio_record =
1247 	  dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1248 
1249 	/*
1250 	 * We need to dec pending if this was a write.
1251 	 */
1252 	if (rw == WRITE) {
1253 		if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
1254 			dm_rh_dec(ms->rh, bio_record->write_region);
1255 		return error;
1256 	}
1257 
1258 	if (error == -EOPNOTSUPP)
1259 		goto out;
1260 
1261 	if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
1262 		goto out;
1263 
1264 	if (unlikely(error)) {
1265 		if (!bio_record->details.bi_bdev) {
1266 			/*
1267 			 * There wasn't enough memory to record necessary
1268 			 * information for a retry or there was no other
1269 			 * mirror in-sync.
1270 			 */
1271 			DMERR_LIMIT("Mirror read failed.");
1272 			return -EIO;
1273 		}
1274 
1275 		m = bio_record->m;
1276 
1277 		DMERR("Mirror read failed from %s. Trying alternative device.",
1278 		      m->dev->name);
1279 
1280 		fail_mirror(m, DM_RAID1_READ_ERROR);
1281 
1282 		/*
1283 		 * A failed read is requeued for another attempt using an intact
1284 		 * mirror.
1285 		 */
1286 		if (default_ok(m) || mirror_available(ms, bio)) {
1287 			bd = &bio_record->details;
1288 
1289 			dm_bio_restore(bd, bio);
1290 			bio_record->details.bi_bdev = NULL;
1291 
1292 			queue_bio(ms, bio, rw);
1293 			return DM_ENDIO_INCOMPLETE;
1294 		}
1295 		DMERR("All replicated volumes dead, failing I/O");
1296 	}
1297 
1298 out:
1299 	bio_record->details.bi_bdev = NULL;
1300 
1301 	return error;
1302 }
1303 
1304 static void mirror_presuspend(struct dm_target *ti)
1305 {
1306 	struct mirror_set *ms = (struct mirror_set *) ti->private;
1307 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1308 
1309 	struct bio_list holds;
1310 	struct bio *bio;
1311 
1312 	atomic_set(&ms->suspend, 1);
1313 
1314 	/*
1315 	 * Process bios in the hold list to start recovery waiting
1316 	 * for bios in the hold list. After the process, no bio has
1317 	 * a chance to be added in the hold list because ms->suspend
1318 	 * is set.
1319 	 */
1320 	spin_lock_irq(&ms->lock);
1321 	holds = ms->holds;
1322 	bio_list_init(&ms->holds);
1323 	spin_unlock_irq(&ms->lock);
1324 
1325 	while ((bio = bio_list_pop(&holds)))
1326 		hold_bio(ms, bio);
1327 
1328 	/*
1329 	 * We must finish up all the work that we've
1330 	 * generated (i.e. recovery work).
1331 	 */
1332 	dm_rh_stop_recovery(ms->rh);
1333 
1334 	wait_event(_kmirrord_recovery_stopped,
1335 		   !dm_rh_recovery_in_flight(ms->rh));
1336 
1337 	if (log->type->presuspend && log->type->presuspend(log))
1338 		/* FIXME: need better error handling */
1339 		DMWARN("log presuspend failed");
1340 
1341 	/*
1342 	 * Now that recovery is complete/stopped and the
1343 	 * delayed bios are queued, we need to wait for
1344 	 * the worker thread to complete.  This way,
1345 	 * we know that all of our I/O has been pushed.
1346 	 */
1347 	flush_workqueue(ms->kmirrord_wq);
1348 }
1349 
1350 static void mirror_postsuspend(struct dm_target *ti)
1351 {
1352 	struct mirror_set *ms = ti->private;
1353 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1354 
1355 	if (log->type->postsuspend && log->type->postsuspend(log))
1356 		/* FIXME: need better error handling */
1357 		DMWARN("log postsuspend failed");
1358 }
1359 
1360 static void mirror_resume(struct dm_target *ti)
1361 {
1362 	struct mirror_set *ms = ti->private;
1363 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1364 
1365 	atomic_set(&ms->suspend, 0);
1366 	if (log->type->resume && log->type->resume(log))
1367 		/* FIXME: need better error handling */
1368 		DMWARN("log resume failed");
1369 	dm_rh_start_recovery(ms->rh);
1370 }
1371 
1372 /*
1373  * device_status_char
1374  * @m: mirror device/leg we want the status of
1375  *
1376  * We return one character representing the most severe error
1377  * we have encountered.
1378  *    A => Alive - No failures
1379  *    D => Dead - A write failure occurred leaving mirror out-of-sync
1380  *    S => Sync - A sychronization failure occurred, mirror out-of-sync
1381  *    R => Read - A read failure occurred, mirror data unaffected
1382  *
1383  * Returns: <char>
1384  */
1385 static char device_status_char(struct mirror *m)
1386 {
1387 	if (!atomic_read(&(m->error_count)))
1388 		return 'A';
1389 
1390 	return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1391 		(test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1392 		(test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1393 		(test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1394 }
1395 
1396 
1397 static void mirror_status(struct dm_target *ti, status_type_t type,
1398 			  unsigned status_flags, char *result, unsigned maxlen)
1399 {
1400 	unsigned int m, sz = 0;
1401 	int num_feature_args = 0;
1402 	struct mirror_set *ms = (struct mirror_set *) ti->private;
1403 	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1404 	char buffer[ms->nr_mirrors + 1];
1405 
1406 	switch (type) {
1407 	case STATUSTYPE_INFO:
1408 		DMEMIT("%d ", ms->nr_mirrors);
1409 		for (m = 0; m < ms->nr_mirrors; m++) {
1410 			DMEMIT("%s ", ms->mirror[m].dev->name);
1411 			buffer[m] = device_status_char(&(ms->mirror[m]));
1412 		}
1413 		buffer[m] = '\0';
1414 
1415 		DMEMIT("%llu/%llu 1 %s ",
1416 		      (unsigned long long)log->type->get_sync_count(log),
1417 		      (unsigned long long)ms->nr_regions, buffer);
1418 
1419 		sz += log->type->status(log, type, result+sz, maxlen-sz);
1420 
1421 		break;
1422 
1423 	case STATUSTYPE_TABLE:
1424 		sz = log->type->status(log, type, result, maxlen);
1425 
1426 		DMEMIT("%d", ms->nr_mirrors);
1427 		for (m = 0; m < ms->nr_mirrors; m++)
1428 			DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1429 			       (unsigned long long)ms->mirror[m].offset);
1430 
1431 		num_feature_args += !!errors_handled(ms);
1432 		num_feature_args += !!keep_log(ms);
1433 		if (num_feature_args) {
1434 			DMEMIT(" %d", num_feature_args);
1435 			if (errors_handled(ms))
1436 				DMEMIT(" handle_errors");
1437 			if (keep_log(ms))
1438 				DMEMIT(" keep_log");
1439 		}
1440 
1441 		break;
1442 	}
1443 }
1444 
1445 static int mirror_iterate_devices(struct dm_target *ti,
1446 				  iterate_devices_callout_fn fn, void *data)
1447 {
1448 	struct mirror_set *ms = ti->private;
1449 	int ret = 0;
1450 	unsigned i;
1451 
1452 	for (i = 0; !ret && i < ms->nr_mirrors; i++)
1453 		ret = fn(ti, ms->mirror[i].dev,
1454 			 ms->mirror[i].offset, ti->len, data);
1455 
1456 	return ret;
1457 }
1458 
1459 static struct target_type mirror_target = {
1460 	.name	 = "mirror",
1461 	.version = {1, 14, 0},
1462 	.module	 = THIS_MODULE,
1463 	.ctr	 = mirror_ctr,
1464 	.dtr	 = mirror_dtr,
1465 	.map	 = mirror_map,
1466 	.end_io	 = mirror_end_io,
1467 	.presuspend = mirror_presuspend,
1468 	.postsuspend = mirror_postsuspend,
1469 	.resume	 = mirror_resume,
1470 	.status	 = mirror_status,
1471 	.iterate_devices = mirror_iterate_devices,
1472 };
1473 
1474 static int __init dm_mirror_init(void)
1475 {
1476 	int r;
1477 
1478 	r = dm_register_target(&mirror_target);
1479 	if (r < 0) {
1480 		DMERR("Failed to register mirror target");
1481 		goto bad_target;
1482 	}
1483 
1484 	return 0;
1485 
1486 bad_target:
1487 	return r;
1488 }
1489 
1490 static void __exit dm_mirror_exit(void)
1491 {
1492 	dm_unregister_target(&mirror_target);
1493 }
1494 
1495 /* Module hooks */
1496 module_init(dm_mirror_init);
1497 module_exit(dm_mirror_exit);
1498 
1499 MODULE_DESCRIPTION(DM_NAME " mirror target");
1500 MODULE_AUTHOR("Joe Thornber");
1501 MODULE_LICENSE("GPL");
1502