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