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