1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2003 Sistina Software Limited.
4 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
6 * This file is released under the GPL.
9 #include "dm-bio-record.h"
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>
23 static struct workqueue_struct *dm_raid1_wq;
25 #define DM_MSG_PREFIX "raid1"
27 #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
29 #define MAX_NR_MIRRORS (DM_KCOPYD_MAX_REGIONS + 1)
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)
36 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
39 *---------------------------------------------------------------
40 * Mirror set structures.
41 *---------------------------------------------------------------
51 struct mirror_set *ms;
53 unsigned long error_type;
60 struct list_head list;
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 */
70 struct dm_region_hash *rh;
71 struct dm_kcopyd_client *kcopyd_client;
72 struct dm_io_client *io_client;
81 atomic_t default_mirror; /* Default mirror */
83 struct workqueue_struct *kmirrord_wq;
84 struct work_struct kmirrord_work;
85 struct timer_list timer;
86 unsigned long timer_pending;
88 struct work_struct trigger_event;
90 unsigned int nr_mirrors;
91 struct mirror mirror[];
94 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
95 "A percentage of time allocated for raid resynchronization");
97 static void wakeup_mirrord(void *context)
99 struct mirror_set *ms = context;
101 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
104 static void delayed_wake_fn(struct timer_list *t)
106 struct mirror_set *ms = from_timer(ms, t, timer);
108 clear_bit(0, &ms->timer_pending);
112 static void delayed_wake(struct mirror_set *ms)
114 if (test_and_set_bit(0, &ms->timer_pending))
117 ms->timer.expires = jiffies + HZ / 5;
118 add_timer(&ms->timer);
121 static void wakeup_all_recovery_waiters(void *context)
123 wake_up_all(&_kmirrord_recovery_stopped);
126 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
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 spin_unlock_irqrestore(&ms->lock, flags);
142 static void dispatch_bios(void *context, struct bio_list *bio_list)
144 struct mirror_set *ms = context;
147 while ((bio = bio_list_pop(bio_list)))
148 queue_bio(ms, bio, WRITE);
151 struct dm_raid1_bio_record {
153 /* if details->bi_bdev == NULL, details were not saved */
154 struct dm_bio_details details;
155 region_t write_region;
159 * Every mirror should look like this one.
161 #define DEFAULT_MIRROR 0
164 * This is yucky. We squirrel the mirror struct away inside
165 * bi_next for read/write buffers. This is safe since the bh
166 * doesn't get submitted to the lower levels of block layer.
168 static struct mirror *bio_get_m(struct bio *bio)
170 return (struct mirror *) bio->bi_next;
173 static void bio_set_m(struct bio *bio, struct mirror *m)
175 bio->bi_next = (struct bio *) m;
178 static struct mirror *get_default_mirror(struct mirror_set *ms)
180 return &ms->mirror[atomic_read(&ms->default_mirror)];
183 static void set_default_mirror(struct mirror *m)
185 struct mirror_set *ms = m->ms;
186 struct mirror *m0 = &(ms->mirror[0]);
188 atomic_set(&ms->default_mirror, m - m0);
191 static struct mirror *get_valid_mirror(struct mirror_set *ms)
195 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
196 if (!atomic_read(&m->error_count))
203 * @m: mirror device to fail
204 * @error_type: one of the enum's, DM_RAID1_*_ERROR
206 * If errors are being handled, record the type of
207 * error encountered for this device. If this type
208 * of error has already been recorded, we can return;
209 * otherwise, we must signal userspace by triggering
210 * an event. Additionally, if the device is the
211 * primary device, we must choose a new primary, but
212 * only if the mirror is in-sync.
214 * This function must not block.
216 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
218 struct mirror_set *ms = m->ms;
224 * error_count is used for nothing more than a
225 * simple way to tell if a device has encountered
228 atomic_inc(&m->error_count);
230 if (test_and_set_bit(error_type, &m->error_type))
233 if (!errors_handled(ms))
236 if (m != get_default_mirror(ms))
239 if (!ms->in_sync && !keep_log(ms)) {
241 * Better to issue requests to same failing device
242 * than to risk returning corrupt data.
244 DMERR("Primary mirror (%s) failed while out-of-sync: Reads may fail.",
249 new = get_valid_mirror(ms);
251 set_default_mirror(new);
253 DMWARN("All sides of mirror have failed.");
256 queue_work(dm_raid1_wq, &ms->trigger_event);
259 static int mirror_flush(struct dm_target *ti)
261 struct mirror_set *ms = ti->private;
262 unsigned long error_bits;
265 struct dm_io_region io[MAX_NR_MIRRORS];
267 struct dm_io_request io_req = {
268 .bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC,
269 .mem.type = DM_IO_KMEM,
270 .mem.ptr.addr = NULL,
271 .client = ms->io_client,
274 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
275 io[i].bdev = m->dev->bdev;
281 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
282 if (unlikely(error_bits != 0)) {
283 for (i = 0; i < ms->nr_mirrors; i++)
284 if (test_bit(i, &error_bits))
285 fail_mirror(ms->mirror + i,
286 DM_RAID1_FLUSH_ERROR);
294 *---------------------------------------------------------------
297 * When a mirror is first activated we may find that some regions
298 * are in the no-sync state. We have to recover these by
299 * recopying from the default mirror to all the others.
300 *---------------------------------------------------------------
302 static void recovery_complete(int read_err, unsigned long write_err,
305 struct dm_region *reg = context;
306 struct mirror_set *ms = dm_rh_region_context(reg);
310 /* Read error means the failure of default mirror. */
311 DMERR_LIMIT("Unable to read primary mirror during recovery");
312 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
316 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
319 * Bits correspond to devices (excluding default mirror).
320 * The default mirror cannot change during recovery.
322 for (m = 0; m < ms->nr_mirrors; m++) {
323 if (&ms->mirror[m] == get_default_mirror(ms))
325 if (test_bit(bit, &write_err))
326 fail_mirror(ms->mirror + m,
327 DM_RAID1_SYNC_ERROR);
332 dm_rh_recovery_end(reg, !(read_err || write_err));
335 static void recover(struct mirror_set *ms, struct dm_region *reg)
338 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
340 unsigned long flags = 0;
341 region_t key = dm_rh_get_region_key(reg);
342 sector_t region_size = dm_rh_get_region_size(ms->rh);
344 /* fill in the source */
345 m = get_default_mirror(ms);
346 from.bdev = m->dev->bdev;
347 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
348 if (key == (ms->nr_regions - 1)) {
350 * The final region may be smaller than
353 from.count = ms->ti->len & (region_size - 1);
355 from.count = region_size;
357 from.count = region_size;
359 /* fill in the destinations */
360 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
361 if (&ms->mirror[i] == get_default_mirror(ms))
365 dest->bdev = m->dev->bdev;
366 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
367 dest->count = from.count;
372 if (!errors_handled(ms))
373 flags |= BIT(DM_KCOPYD_IGNORE_ERROR);
375 dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
376 flags, recovery_complete, reg);
379 static void reset_ms_flags(struct mirror_set *ms)
384 for (m = 0; m < ms->nr_mirrors; m++) {
385 atomic_set(&(ms->mirror[m].error_count), 0);
386 ms->mirror[m].error_type = 0;
390 static void do_recovery(struct mirror_set *ms)
392 struct dm_region *reg;
393 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
396 * Start quiescing some regions.
398 dm_rh_recovery_prepare(ms->rh);
401 * Copy any already quiesced regions.
403 while ((reg = dm_rh_recovery_start(ms->rh)))
407 * Update the in sync flag.
410 (log->type->get_sync_count(log) == ms->nr_regions)) {
411 /* the sync is complete */
412 dm_table_event(ms->ti->table);
419 *---------------------------------------------------------------
421 *---------------------------------------------------------------
423 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
425 struct mirror *m = get_default_mirror(ms);
428 if (likely(!atomic_read(&m->error_count)))
431 if (m-- == ms->mirror)
433 } while (m != get_default_mirror(ms));
438 static int default_ok(struct mirror *m)
440 struct mirror *default_mirror = get_default_mirror(m->ms);
442 return !atomic_read(&default_mirror->error_count);
445 static int mirror_available(struct mirror_set *ms, struct bio *bio)
447 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
448 region_t region = dm_rh_bio_to_region(ms->rh, bio);
450 if (log->type->in_sync(log, region, 0))
451 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
457 * remap a buffer to a particular mirror.
459 static sector_t map_sector(struct mirror *m, struct bio *bio)
461 if (unlikely(!bio->bi_iter.bi_size))
463 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
466 static void map_bio(struct mirror *m, struct bio *bio)
468 bio_set_dev(bio, m->dev->bdev);
469 bio->bi_iter.bi_sector = map_sector(m, bio);
472 static void map_region(struct dm_io_region *io, struct mirror *m,
475 io->bdev = m->dev->bdev;
476 io->sector = map_sector(m, bio);
477 io->count = bio_sectors(bio);
480 static void hold_bio(struct mirror_set *ms, struct bio *bio)
483 * Lock is required to avoid race condition during suspend
486 spin_lock_irq(&ms->lock);
488 if (atomic_read(&ms->suspend)) {
489 spin_unlock_irq(&ms->lock);
492 * If device is suspended, complete the bio.
494 if (dm_noflush_suspending(ms->ti))
495 bio->bi_status = BLK_STS_DM_REQUEUE;
497 bio->bi_status = BLK_STS_IOERR;
504 * Hold bio until the suspend is complete.
506 bio_list_add(&ms->holds, bio);
507 spin_unlock_irq(&ms->lock);
511 *---------------------------------------------------------------
513 *---------------------------------------------------------------
515 static void read_callback(unsigned long error, void *context)
517 struct bio *bio = context;
521 bio_set_m(bio, NULL);
523 if (likely(!error)) {
528 fail_mirror(m, DM_RAID1_READ_ERROR);
530 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
531 DMWARN_LIMIT("Read failure on mirror device %s. Trying alternative device.",
533 queue_bio(m->ms, bio, bio_data_dir(bio));
537 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
542 /* Asynchronous read. */
543 static void read_async_bio(struct mirror *m, struct bio *bio)
545 struct dm_io_region io;
546 struct dm_io_request io_req = {
547 .bi_opf = REQ_OP_READ,
548 .mem.type = DM_IO_BIO,
550 .notify.fn = read_callback,
551 .notify.context = bio,
552 .client = m->ms->io_client,
555 map_region(&io, m, bio);
557 BUG_ON(dm_io(&io_req, 1, &io, NULL));
560 static inline int region_in_sync(struct mirror_set *ms, region_t region,
563 int state = dm_rh_get_state(ms->rh, region, may_block);
564 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
567 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
573 while ((bio = bio_list_pop(reads))) {
574 region = dm_rh_bio_to_region(ms->rh, bio);
575 m = get_default_mirror(ms);
578 * We can only read balance if the region is in sync.
580 if (likely(region_in_sync(ms, region, 1)))
581 m = choose_mirror(ms, bio->bi_iter.bi_sector);
582 else if (m && atomic_read(&m->error_count))
586 read_async_bio(m, bio);
593 *---------------------------------------------------------------------
596 * We do different things with the write io depending on the
597 * state of the region that it's in:
599 * SYNC: increment pending, use kcopyd to write to *all* mirrors
600 * RECOVERING: delay the io until recovery completes
601 * NOSYNC: increment pending, just write to the default mirror
602 *---------------------------------------------------------------------
604 static void write_callback(unsigned long error, void *context)
607 struct bio *bio = context;
608 struct mirror_set *ms;
612 ms = bio_get_m(bio)->ms;
613 bio_set_m(bio, NULL);
616 * NOTE: We don't decrement the pending count here,
617 * instead it is done by the targets endio function.
618 * This way we handle both writes to SYNC and NOSYNC
619 * regions with the same code.
621 if (likely(!error)) {
627 * If the bio is discard, return an error, but do not
630 if (bio_op(bio) == REQ_OP_DISCARD) {
631 bio->bi_status = BLK_STS_NOTSUPP;
636 for (i = 0; i < ms->nr_mirrors; i++)
637 if (test_bit(i, &error))
638 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
641 * Need to raise event. Since raising
642 * events can block, we need to do it in
645 spin_lock_irqsave(&ms->lock, flags);
646 if (!ms->failures.head)
648 bio_list_add(&ms->failures, bio);
649 spin_unlock_irqrestore(&ms->lock, flags);
654 static void do_write(struct mirror_set *ms, struct bio *bio)
657 struct dm_io_region io[MAX_NR_MIRRORS], *dest = io;
659 blk_opf_t op_flags = bio->bi_opf & (REQ_FUA | REQ_PREFLUSH);
660 struct dm_io_request io_req = {
661 .bi_opf = REQ_OP_WRITE | op_flags,
662 .mem.type = DM_IO_BIO,
664 .notify.fn = write_callback,
665 .notify.context = bio,
666 .client = ms->io_client,
669 if (bio_op(bio) == REQ_OP_DISCARD) {
670 io_req.bi_opf = REQ_OP_DISCARD | op_flags;
671 io_req.mem.type = DM_IO_KMEM;
672 io_req.mem.ptr.addr = NULL;
675 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
676 map_region(dest++, m, bio);
679 * Use default mirror because we only need it to retrieve the reference
680 * to the mirror set in write_callback().
682 bio_set_m(bio, get_default_mirror(ms));
684 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
687 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
691 struct bio_list sync, nosync, recover, *this_list = NULL;
692 struct bio_list requeue;
693 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
700 * Classify each write.
702 bio_list_init(&sync);
703 bio_list_init(&nosync);
704 bio_list_init(&recover);
705 bio_list_init(&requeue);
707 while ((bio = bio_list_pop(writes))) {
708 if ((bio->bi_opf & REQ_PREFLUSH) ||
709 (bio_op(bio) == REQ_OP_DISCARD)) {
710 bio_list_add(&sync, bio);
714 region = dm_rh_bio_to_region(ms->rh, bio);
716 if (log->type->is_remote_recovering &&
717 log->type->is_remote_recovering(log, region)) {
718 bio_list_add(&requeue, bio);
722 state = dm_rh_get_state(ms->rh, region, 1);
733 case DM_RH_RECOVERING:
734 this_list = &recover;
738 bio_list_add(this_list, bio);
742 * Add bios that are delayed due to remote recovery
743 * back on to the write queue
745 if (unlikely(requeue.head)) {
746 spin_lock_irq(&ms->lock);
747 bio_list_merge(&ms->writes, &requeue);
748 spin_unlock_irq(&ms->lock);
753 * Increment the pending counts for any regions that will
754 * be written to (writes to recover regions are going to
757 dm_rh_inc_pending(ms->rh, &sync);
758 dm_rh_inc_pending(ms->rh, &nosync);
761 * If the flush fails on a previous call and succeeds here,
762 * we must not reset the log_failure variable. We need
763 * userspace interaction to do that.
765 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
770 if (unlikely(ms->log_failure) && errors_handled(ms)) {
771 spin_lock_irq(&ms->lock);
772 bio_list_merge(&ms->failures, &sync);
773 spin_unlock_irq(&ms->lock);
776 while ((bio = bio_list_pop(&sync)))
779 while ((bio = bio_list_pop(&recover)))
780 dm_rh_delay(ms->rh, bio);
782 while ((bio = bio_list_pop(&nosync))) {
783 if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
784 spin_lock_irq(&ms->lock);
785 bio_list_add(&ms->failures, bio);
786 spin_unlock_irq(&ms->lock);
789 map_bio(get_default_mirror(ms), bio);
790 submit_bio_noacct(bio);
795 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
799 if (likely(!failures->head))
803 * If the log has failed, unattempted writes are being
804 * put on the holds list. We can't issue those writes
805 * until a log has been marked, so we must store them.
807 * If a 'noflush' suspend is in progress, we can requeue
808 * the I/O's to the core. This give userspace a chance
809 * to reconfigure the mirror, at which point the core
810 * will reissue the writes. If the 'noflush' flag is
811 * not set, we have no choice but to return errors.
813 * Some writes on the failures list may have been
814 * submitted before the log failure and represent a
815 * failure to write to one of the devices. It is ok
816 * for us to treat them the same and requeue them
819 while ((bio = bio_list_pop(failures))) {
820 if (!ms->log_failure) {
822 dm_rh_mark_nosync(ms->rh, bio);
826 * If all the legs are dead, fail the I/O.
827 * If the device has failed and keep_log is enabled,
830 * If we have been told to handle errors, and keep_log
831 * isn't enabled, hold the bio and wait for userspace to
832 * deal with the problem.
834 * Otherwise pretend that the I/O succeeded. (This would
835 * be wrong if the failed leg returned after reboot and
836 * got replicated back to the good legs.)
838 if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
840 else if (errors_handled(ms) && !keep_log(ms))
847 static void trigger_event(struct work_struct *work)
849 struct mirror_set *ms =
850 container_of(work, struct mirror_set, trigger_event);
852 dm_table_event(ms->ti->table);
856 *---------------------------------------------------------------
858 *---------------------------------------------------------------
860 static void do_mirror(struct work_struct *work)
862 struct mirror_set *ms = container_of(work, struct mirror_set,
864 struct bio_list reads, writes, failures;
867 spin_lock_irqsave(&ms->lock, flags);
870 failures = ms->failures;
871 bio_list_init(&ms->reads);
872 bio_list_init(&ms->writes);
873 bio_list_init(&ms->failures);
874 spin_unlock_irqrestore(&ms->lock, flags);
876 dm_rh_update_states(ms->rh, errors_handled(ms));
878 do_reads(ms, &reads);
879 do_writes(ms, &writes);
880 do_failures(ms, &failures);
884 *---------------------------------------------------------------
886 *---------------------------------------------------------------
888 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
889 uint32_t region_size,
890 struct dm_target *ti,
891 struct dm_dirty_log *dl)
893 struct mirror_set *ms =
894 kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
897 ti->error = "Cannot allocate mirror context";
901 spin_lock_init(&ms->lock);
902 bio_list_init(&ms->reads);
903 bio_list_init(&ms->writes);
904 bio_list_init(&ms->failures);
905 bio_list_init(&ms->holds);
908 ms->nr_mirrors = nr_mirrors;
909 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
913 atomic_set(&ms->suspend, 0);
914 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
916 ms->io_client = dm_io_client_create();
917 if (IS_ERR(ms->io_client)) {
918 ti->error = "Error creating dm_io client";
923 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
924 wakeup_all_recovery_waiters,
925 ms->ti->begin, MAX_RECOVERY,
926 dl, region_size, ms->nr_regions);
927 if (IS_ERR(ms->rh)) {
928 ti->error = "Error creating dirty region hash";
929 dm_io_client_destroy(ms->io_client);
937 static void free_context(struct mirror_set *ms, struct dm_target *ti,
941 dm_put_device(ti, ms->mirror[m].dev);
943 dm_io_client_destroy(ms->io_client);
944 dm_region_hash_destroy(ms->rh);
948 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
949 unsigned int mirror, char **argv)
951 unsigned long long offset;
955 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
956 offset != (sector_t)offset) {
957 ti->error = "Invalid offset";
961 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
962 &ms->mirror[mirror].dev);
964 ti->error = "Device lookup failure";
968 ms->mirror[mirror].ms = ms;
969 atomic_set(&(ms->mirror[mirror].error_count), 0);
970 ms->mirror[mirror].error_type = 0;
971 ms->mirror[mirror].offset = offset;
977 * Create dirty log: log_type #log_params <log_params>
979 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
980 unsigned int argc, char **argv,
981 unsigned int *args_used)
983 unsigned int param_count;
984 struct dm_dirty_log *dl;
988 ti->error = "Insufficient mirror log arguments";
992 if (sscanf(argv[1], "%u%c", ¶m_count, &dummy) != 1) {
993 ti->error = "Invalid mirror log argument count";
997 *args_used = 2 + param_count;
999 if (argc < *args_used) {
1000 ti->error = "Insufficient mirror log arguments";
1004 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
1007 ti->error = "Error creating mirror dirty log";
1014 static int parse_features(struct mirror_set *ms, unsigned int argc, char **argv,
1015 unsigned int *args_used)
1017 unsigned int num_features;
1018 struct dm_target *ti = ms->ti;
1027 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
1028 ti->error = "Invalid number of features";
1036 if (num_features > argc) {
1037 ti->error = "Not enough arguments to support feature count";
1041 for (i = 0; i < num_features; i++) {
1042 if (!strcmp("handle_errors", argv[0]))
1043 ms->features |= DM_RAID1_HANDLE_ERRORS;
1044 else if (!strcmp("keep_log", argv[0]))
1045 ms->features |= DM_RAID1_KEEP_LOG;
1047 ti->error = "Unrecognised feature requested";
1055 if (!errors_handled(ms) && keep_log(ms)) {
1056 ti->error = "keep_log feature requires the handle_errors feature";
1064 * Construct a mirror mapping:
1066 * log_type #log_params <log_params>
1067 * #mirrors [mirror_path offset]{2,}
1068 * [#features <features>]
1070 * log_type is "core" or "disk"
1071 * #log_params is between 1 and 3
1073 * If present, supported features are "handle_errors" and "keep_log".
1075 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1078 unsigned int nr_mirrors, m, args_used;
1079 struct mirror_set *ms;
1080 struct dm_dirty_log *dl;
1083 dl = create_dirty_log(ti, argc, argv, &args_used);
1090 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
1091 nr_mirrors < 2 || nr_mirrors > MAX_NR_MIRRORS) {
1092 ti->error = "Invalid number of mirrors";
1093 dm_dirty_log_destroy(dl);
1099 if (argc < nr_mirrors * 2) {
1100 ti->error = "Too few mirror arguments";
1101 dm_dirty_log_destroy(dl);
1105 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1107 dm_dirty_log_destroy(dl);
1111 /* Get the mirror parameter sets */
1112 for (m = 0; m < nr_mirrors; m++) {
1113 r = get_mirror(ms, ti, m, argv);
1115 free_context(ms, ti, m);
1124 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1126 goto err_free_context;
1128 ti->num_flush_bios = 1;
1129 ti->num_discard_bios = 1;
1130 ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
1132 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
1133 if (!ms->kmirrord_wq) {
1134 DMERR("couldn't start kmirrord");
1136 goto err_free_context;
1138 INIT_WORK(&ms->kmirrord_work, do_mirror);
1139 timer_setup(&ms->timer, delayed_wake_fn, 0);
1140 ms->timer_pending = 0;
1141 INIT_WORK(&ms->trigger_event, trigger_event);
1143 r = parse_features(ms, argc, argv, &args_used);
1145 goto err_destroy_wq;
1151 * Any read-balancing addition depends on the
1152 * DM_RAID1_HANDLE_ERRORS flag being present.
1153 * This is because the decision to balance depends
1154 * on the sync state of a region. If the above
1155 * flag is not present, we ignore errors; and
1156 * the sync state may be inaccurate.
1160 ti->error = "Too many mirror arguments";
1162 goto err_destroy_wq;
1165 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1166 if (IS_ERR(ms->kcopyd_client)) {
1167 r = PTR_ERR(ms->kcopyd_client);
1168 goto err_destroy_wq;
1175 destroy_workqueue(ms->kmirrord_wq);
1177 free_context(ms, ti, ms->nr_mirrors);
1181 static void mirror_dtr(struct dm_target *ti)
1183 struct mirror_set *ms = ti->private;
1185 del_timer_sync(&ms->timer);
1186 flush_workqueue(ms->kmirrord_wq);
1187 flush_work(&ms->trigger_event);
1188 dm_kcopyd_client_destroy(ms->kcopyd_client);
1189 destroy_workqueue(ms->kmirrord_wq);
1190 free_context(ms, ti, ms->nr_mirrors);
1194 * Mirror mapping function
1196 static int mirror_map(struct dm_target *ti, struct bio *bio)
1198 int r, rw = bio_data_dir(bio);
1200 struct mirror_set *ms = ti->private;
1201 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1202 struct dm_raid1_bio_record *bio_record =
1203 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1205 bio_record->details.bi_bdev = NULL;
1208 /* Save region for mirror_end_io() handler */
1209 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
1210 queue_bio(ms, bio, rw);
1211 return DM_MAPIO_SUBMITTED;
1214 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1215 if (r < 0 && r != -EWOULDBLOCK)
1216 return DM_MAPIO_KILL;
1219 * If region is not in-sync queue the bio.
1221 if (!r || (r == -EWOULDBLOCK)) {
1222 if (bio->bi_opf & REQ_RAHEAD)
1223 return DM_MAPIO_KILL;
1225 queue_bio(ms, bio, rw);
1226 return DM_MAPIO_SUBMITTED;
1230 * The region is in-sync and we can perform reads directly.
1231 * Store enough information so we can retry if it fails.
1233 m = choose_mirror(ms, bio->bi_iter.bi_sector);
1235 return DM_MAPIO_KILL;
1237 dm_bio_record(&bio_record->details, bio);
1242 return DM_MAPIO_REMAPPED;
1245 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1246 blk_status_t *error)
1248 int rw = bio_data_dir(bio);
1249 struct mirror_set *ms = ti->private;
1250 struct mirror *m = NULL;
1251 struct dm_bio_details *bd = NULL;
1252 struct dm_raid1_bio_record *bio_record =
1253 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1256 * We need to dec pending if this was a write.
1259 if (!(bio->bi_opf & REQ_PREFLUSH) &&
1260 bio_op(bio) != REQ_OP_DISCARD)
1261 dm_rh_dec(ms->rh, bio_record->write_region);
1262 return DM_ENDIO_DONE;
1265 if (*error == BLK_STS_NOTSUPP)
1268 if (bio->bi_opf & REQ_RAHEAD)
1271 if (unlikely(*error)) {
1272 if (!bio_record->details.bi_bdev) {
1274 * There wasn't enough memory to record necessary
1275 * information for a retry or there was no other
1278 DMERR_LIMIT("Mirror read failed.");
1279 return DM_ENDIO_DONE;
1284 DMERR("Mirror read failed from %s. Trying alternative device.",
1287 fail_mirror(m, DM_RAID1_READ_ERROR);
1290 * A failed read is requeued for another attempt using an intact
1293 if (default_ok(m) || mirror_available(ms, bio)) {
1294 bd = &bio_record->details;
1296 dm_bio_restore(bd, bio);
1297 bio_record->details.bi_bdev = NULL;
1300 queue_bio(ms, bio, rw);
1301 return DM_ENDIO_INCOMPLETE;
1303 DMERR("All replicated volumes dead, failing I/O");
1307 bio_record->details.bi_bdev = NULL;
1309 return DM_ENDIO_DONE;
1312 static void mirror_presuspend(struct dm_target *ti)
1314 struct mirror_set *ms = ti->private;
1315 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1317 struct bio_list holds;
1320 atomic_set(&ms->suspend, 1);
1323 * Process bios in the hold list to start recovery waiting
1324 * for bios in the hold list. After the process, no bio has
1325 * a chance to be added in the hold list because ms->suspend
1328 spin_lock_irq(&ms->lock);
1330 bio_list_init(&ms->holds);
1331 spin_unlock_irq(&ms->lock);
1333 while ((bio = bio_list_pop(&holds)))
1337 * We must finish up all the work that we've
1338 * generated (i.e. recovery work).
1340 dm_rh_stop_recovery(ms->rh);
1342 wait_event(_kmirrord_recovery_stopped,
1343 !dm_rh_recovery_in_flight(ms->rh));
1345 if (log->type->presuspend && log->type->presuspend(log))
1346 /* FIXME: need better error handling */
1347 DMWARN("log presuspend failed");
1350 * Now that recovery is complete/stopped and the
1351 * delayed bios are queued, we need to wait for
1352 * the worker thread to complete. This way,
1353 * we know that all of our I/O has been pushed.
1355 flush_workqueue(ms->kmirrord_wq);
1358 static void mirror_postsuspend(struct dm_target *ti)
1360 struct mirror_set *ms = ti->private;
1361 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1363 if (log->type->postsuspend && log->type->postsuspend(log))
1364 /* FIXME: need better error handling */
1365 DMWARN("log postsuspend failed");
1368 static void mirror_resume(struct dm_target *ti)
1370 struct mirror_set *ms = ti->private;
1371 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1373 atomic_set(&ms->suspend, 0);
1374 if (log->type->resume && log->type->resume(log))
1375 /* FIXME: need better error handling */
1376 DMWARN("log resume failed");
1377 dm_rh_start_recovery(ms->rh);
1381 * device_status_char
1382 * @m: mirror device/leg we want the status of
1384 * We return one character representing the most severe error
1385 * we have encountered.
1386 * A => Alive - No failures
1387 * D => Dead - A write failure occurred leaving mirror out-of-sync
1388 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1389 * R => Read - A read failure occurred, mirror data unaffected
1393 static char device_status_char(struct mirror *m)
1395 if (!atomic_read(&(m->error_count)))
1398 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1399 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1400 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1401 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1405 static void mirror_status(struct dm_target *ti, status_type_t type,
1406 unsigned int status_flags, char *result, unsigned int maxlen)
1408 unsigned int m, sz = 0;
1409 int num_feature_args = 0;
1410 struct mirror_set *ms = ti->private;
1411 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1412 char buffer[MAX_NR_MIRRORS + 1];
1415 case STATUSTYPE_INFO:
1416 DMEMIT("%d ", ms->nr_mirrors);
1417 for (m = 0; m < ms->nr_mirrors; m++) {
1418 DMEMIT("%s ", ms->mirror[m].dev->name);
1419 buffer[m] = device_status_char(&(ms->mirror[m]));
1423 DMEMIT("%llu/%llu 1 %s ",
1424 (unsigned long long)log->type->get_sync_count(log),
1425 (unsigned long long)ms->nr_regions, buffer);
1427 sz += log->type->status(log, type, result+sz, maxlen-sz);
1431 case STATUSTYPE_TABLE:
1432 sz = log->type->status(log, type, result, maxlen);
1434 DMEMIT("%d", ms->nr_mirrors);
1435 for (m = 0; m < ms->nr_mirrors; m++)
1436 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1437 (unsigned long long)ms->mirror[m].offset);
1439 num_feature_args += !!errors_handled(ms);
1440 num_feature_args += !!keep_log(ms);
1441 if (num_feature_args) {
1442 DMEMIT(" %d", num_feature_args);
1443 if (errors_handled(ms))
1444 DMEMIT(" handle_errors");
1446 DMEMIT(" keep_log");
1451 case STATUSTYPE_IMA:
1452 DMEMIT_TARGET_NAME_VERSION(ti->type);
1453 DMEMIT(",nr_mirrors=%d", ms->nr_mirrors);
1454 for (m = 0; m < ms->nr_mirrors; m++) {
1455 DMEMIT(",mirror_device_%d=%s", m, ms->mirror[m].dev->name);
1456 DMEMIT(",mirror_device_%d_status=%c",
1457 m, device_status_char(&(ms->mirror[m])));
1460 DMEMIT(",handle_errors=%c", errors_handled(ms) ? 'y' : 'n');
1461 DMEMIT(",keep_log=%c", keep_log(ms) ? 'y' : 'n');
1463 DMEMIT(",log_type_status=");
1464 sz += log->type->status(log, type, result+sz, maxlen-sz);
1470 static int mirror_iterate_devices(struct dm_target *ti,
1471 iterate_devices_callout_fn fn, void *data)
1473 struct mirror_set *ms = ti->private;
1477 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1478 ret = fn(ti, ms->mirror[i].dev,
1479 ms->mirror[i].offset, ti->len, data);
1484 static struct target_type mirror_target = {
1486 .version = {1, 14, 0},
1487 .module = THIS_MODULE,
1491 .end_io = mirror_end_io,
1492 .presuspend = mirror_presuspend,
1493 .postsuspend = mirror_postsuspend,
1494 .resume = mirror_resume,
1495 .status = mirror_status,
1496 .iterate_devices = mirror_iterate_devices,
1499 static int __init dm_mirror_init(void)
1503 dm_raid1_wq = alloc_workqueue("dm_raid1_wq", 0, 0);
1505 DMERR("Failed to alloc workqueue");
1509 r = dm_register_target(&mirror_target);
1511 destroy_workqueue(dm_raid1_wq);
1518 static void __exit dm_mirror_exit(void)
1520 destroy_workqueue(dm_raid1_wq);
1521 dm_unregister_target(&mirror_target);
1525 module_init(dm_mirror_init);
1526 module_exit(dm_mirror_exit);
1528 MODULE_DESCRIPTION(DM_NAME " mirror target");
1529 MODULE_AUTHOR("Joe Thornber");
1530 MODULE_LICENSE("GPL");