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 = timer_container_of(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);
138 spin_unlock_irqrestore(&ms->lock, flags);
141 static void dispatch_bios(void *context, struct bio_list *bio_list)
143 struct mirror_set *ms = context;
146 while ((bio = bio_list_pop(bio_list)))
147 queue_bio(ms, bio, WRITE);
150 struct dm_raid1_bio_record {
152 /* if details->bi_bdev == NULL, details were not saved */
153 struct dm_bio_details details;
154 region_t write_region;
158 * Every mirror should look like this one.
160 #define DEFAULT_MIRROR 0
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.
167 static struct mirror *bio_get_m(struct bio *bio)
169 return (struct mirror *) bio->bi_next;
172 static void bio_set_m(struct bio *bio, struct mirror *m)
174 bio->bi_next = (struct bio *) m;
177 static struct mirror *get_default_mirror(struct mirror_set *ms)
179 return &ms->mirror[atomic_read(&ms->default_mirror)];
182 static void set_default_mirror(struct mirror *m)
184 struct mirror_set *ms = m->ms;
185 struct mirror *m0 = &(ms->mirror[0]);
187 atomic_set(&ms->default_mirror, m - m0);
190 static struct mirror *get_valid_mirror(struct mirror_set *ms)
194 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
195 if (!atomic_read(&m->error_count))
202 * @m: mirror device to fail
203 * @error_type: one of the enum's, DM_RAID1_*_ERROR
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.
213 * This function must not block.
215 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
217 struct mirror_set *ms = m->ms;
223 * error_count is used for nothing more than a
224 * simple way to tell if a device has encountered
227 atomic_inc(&m->error_count);
229 if (test_and_set_bit(error_type, &m->error_type))
232 if (!errors_handled(ms))
235 if (m != get_default_mirror(ms))
238 if (!ms->in_sync && !keep_log(ms)) {
240 * Better to issue requests to same failing device
241 * than to risk returning corrupt data.
243 DMERR("Primary mirror (%s) failed while out-of-sync: Reads may fail.",
248 new = get_valid_mirror(ms);
250 set_default_mirror(new);
252 DMWARN("All sides of mirror have failed.");
255 queue_work(dm_raid1_wq, &ms->trigger_event);
258 static int mirror_flush(struct dm_target *ti)
260 struct mirror_set *ms = ti->private;
261 unsigned long error_bits;
264 struct dm_io_region io[MAX_NR_MIRRORS];
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,
273 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
274 io[i].bdev = m->dev->bdev;
280 dm_io(&io_req, ms->nr_mirrors, io, &error_bits, IOPRIO_DEFAULT);
281 if (unlikely(error_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);
293 *---------------------------------------------------------------
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 *---------------------------------------------------------------
301 static void recovery_complete(int read_err, unsigned long write_err,
304 struct dm_region *reg = context;
305 struct mirror_set *ms = dm_rh_region_context(reg);
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);
315 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
318 * Bits correspond to devices (excluding default mirror).
319 * The default mirror cannot change during recovery.
321 for (m = 0; m < ms->nr_mirrors; m++) {
322 if (&ms->mirror[m] == get_default_mirror(ms))
324 if (test_bit(bit, &write_err))
325 fail_mirror(ms->mirror + m,
326 DM_RAID1_SYNC_ERROR);
331 dm_rh_recovery_end(reg, !(read_err || write_err));
334 static void recover(struct mirror_set *ms, struct dm_region *reg)
337 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
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);
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)) {
349 * The final region may be smaller than
352 from.count = ms->ti->len & (region_size - 1);
354 from.count = region_size;
356 from.count = region_size;
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))
364 dest->bdev = m->dev->bdev;
365 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
366 dest->count = from.count;
371 if (!errors_handled(ms))
372 flags |= BIT(DM_KCOPYD_IGNORE_ERROR);
374 dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
375 flags, recovery_complete, reg);
378 static void reset_ms_flags(struct mirror_set *ms)
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;
389 static void do_recovery(struct mirror_set *ms)
391 struct dm_region *reg;
392 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
395 * Start quiescing some regions.
397 dm_rh_recovery_prepare(ms->rh);
400 * Copy any already quiesced regions.
402 while ((reg = dm_rh_recovery_start(ms->rh)))
406 * Update the in sync flag.
409 (log->type->get_sync_count(log) == ms->nr_regions)) {
410 /* the sync is complete */
411 dm_table_event(ms->ti->table);
418 *---------------------------------------------------------------
420 *---------------------------------------------------------------
422 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
424 struct mirror *m = get_default_mirror(ms);
427 if (likely(!atomic_read(&m->error_count)))
430 if (m-- == ms->mirror)
432 } while (m != get_default_mirror(ms));
437 static int default_ok(struct mirror *m)
439 struct mirror *default_mirror = get_default_mirror(m->ms);
441 return !atomic_read(&default_mirror->error_count);
444 static int mirror_available(struct mirror_set *ms, struct bio *bio)
446 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
447 region_t region = dm_rh_bio_to_region(ms->rh, bio);
449 if (log->type->in_sync(log, region, 0))
450 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
456 * remap a buffer to a particular mirror.
458 static sector_t map_sector(struct mirror *m, struct bio *bio)
460 if (unlikely(!bio->bi_iter.bi_size))
462 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
465 static void map_bio(struct mirror *m, struct bio *bio)
467 bio_set_dev(bio, m->dev->bdev);
468 bio->bi_iter.bi_sector = map_sector(m, bio);
471 static void map_region(struct dm_io_region *io, struct mirror *m,
474 io->bdev = m->dev->bdev;
475 io->sector = map_sector(m, bio);
476 io->count = bio_sectors(bio);
479 static void hold_bio(struct mirror_set *ms, struct bio *bio)
482 * Lock is required to avoid race condition during suspend
485 spin_lock_irq(&ms->lock);
487 if (atomic_read(&ms->suspend)) {
488 spin_unlock_irq(&ms->lock);
491 * If device is suspended, complete the bio.
493 if (dm_noflush_suspending(ms->ti))
494 bio->bi_status = BLK_STS_DM_REQUEUE;
496 bio->bi_status = BLK_STS_IOERR;
503 * Hold bio until the suspend is complete.
505 bio_list_add(&ms->holds, bio);
506 spin_unlock_irq(&ms->lock);
510 *---------------------------------------------------------------
512 *---------------------------------------------------------------
514 static void read_callback(unsigned long error, void *context)
516 struct bio *bio = context;
520 bio_set_m(bio, NULL);
522 if (likely(!error)) {
527 fail_mirror(m, DM_RAID1_READ_ERROR);
529 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
530 DMWARN_LIMIT("Read failure on mirror device %s. Trying alternative device.",
532 queue_bio(m->ms, bio, bio_data_dir(bio));
536 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
541 /* Asynchronous read. */
542 static void read_async_bio(struct mirror *m, struct bio *bio)
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,
549 .notify.fn = read_callback,
550 .notify.context = bio,
551 .client = m->ms->io_client,
554 map_region(&io, m, bio);
556 BUG_ON(dm_io(&io_req, 1, &io, NULL, IOPRIO_DEFAULT));
559 static inline int region_in_sync(struct mirror_set *ms, region_t region,
562 int state = dm_rh_get_state(ms->rh, region, may_block);
563 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
566 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
572 while ((bio = bio_list_pop(reads))) {
573 region = dm_rh_bio_to_region(ms->rh, bio);
574 m = get_default_mirror(ms);
577 * We can only read balance if the region is in sync.
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))
585 read_async_bio(m, bio);
592 *---------------------------------------------------------------------
595 * We do different things with the write io depending on the
596 * state of the region that it's in:
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 *---------------------------------------------------------------------
603 static void write_callback(unsigned long error, void *context)
606 struct bio *bio = context;
607 struct mirror_set *ms;
611 ms = bio_get_m(bio)->ms;
612 bio_set_m(bio, NULL);
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.
620 if (likely(!error)) {
626 * If the bio is discard, return an error, but do not
629 if (bio_op(bio) == REQ_OP_DISCARD) {
630 bio->bi_status = BLK_STS_NOTSUPP;
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);
640 * Need to raise event. Since raising
641 * events can block, we need to do it in
644 spin_lock_irqsave(&ms->lock, flags);
645 if (!ms->failures.head)
647 bio_list_add(&ms->failures, bio);
650 spin_unlock_irqrestore(&ms->lock, flags);
653 static void do_write(struct mirror_set *ms, struct bio *bio)
656 struct dm_io_region io[MAX_NR_MIRRORS], *dest = io;
658 blk_opf_t op_flags = bio->bi_opf & (REQ_FUA | REQ_PREFLUSH | REQ_ATOMIC);
659 struct dm_io_request io_req = {
660 .bi_opf = REQ_OP_WRITE | op_flags,
661 .mem.type = DM_IO_BIO,
663 .notify.fn = write_callback,
664 .notify.context = bio,
665 .client = ms->io_client,
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;
674 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
675 map_region(dest++, m, bio);
678 * Use default mirror because we only need it to retrieve the reference
679 * to the mirror set in write_callback().
681 bio_set_m(bio, get_default_mirror(ms));
683 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL, IOPRIO_DEFAULT));
686 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
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);
699 * Classify each write.
701 bio_list_init(&sync);
702 bio_list_init(&nosync);
703 bio_list_init(&recover);
704 bio_list_init(&requeue);
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);
713 region = dm_rh_bio_to_region(ms->rh, bio);
715 if (log->type->is_remote_recovering &&
716 log->type->is_remote_recovering(log, region)) {
717 bio_list_add(&requeue, bio);
721 state = dm_rh_get_state(ms->rh, region, 1);
732 case DM_RH_RECOVERING:
733 this_list = &recover;
737 bio_list_add(this_list, bio);
741 * Add bios that are delayed due to remote recovery
742 * back on to the write queue
744 if (unlikely(requeue.head)) {
745 spin_lock_irq(&ms->lock);
746 bio_list_merge(&ms->writes, &requeue);
747 spin_unlock_irq(&ms->lock);
752 * Increment the pending counts for any regions that will
753 * be written to (writes to recover regions are going to
756 dm_rh_inc_pending(ms->rh, &sync);
757 dm_rh_inc_pending(ms->rh, &nosync);
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.
764 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
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);
775 while ((bio = bio_list_pop(&sync)))
778 while ((bio = bio_list_pop(&recover)))
779 dm_rh_delay(ms->rh, bio);
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);
788 map_bio(get_default_mirror(ms), bio);
789 submit_bio_noacct(bio);
794 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
798 if (likely(!failures->head))
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.
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.
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
818 while ((bio = bio_list_pop(failures))) {
819 if (!ms->log_failure) {
821 dm_rh_mark_nosync(ms->rh, bio);
825 * If all the legs are dead, fail the I/O.
826 * If the device has failed and keep_log is enabled,
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.
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.)
837 if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
839 else if (errors_handled(ms) && !keep_log(ms))
846 static void trigger_event(struct work_struct *work)
848 struct mirror_set *ms =
849 container_of(work, struct mirror_set, trigger_event);
851 dm_table_event(ms->ti->table);
855 *---------------------------------------------------------------
857 *---------------------------------------------------------------
859 static void do_mirror(struct work_struct *work)
861 struct mirror_set *ms = container_of(work, struct mirror_set,
863 struct bio_list reads, writes, failures;
866 spin_lock_irqsave(&ms->lock, flags);
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);
875 dm_rh_update_states(ms->rh, errors_handled(ms));
877 do_reads(ms, &reads);
878 do_writes(ms, &writes);
879 do_failures(ms, &failures);
883 *---------------------------------------------------------------
885 *---------------------------------------------------------------
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)
892 struct mirror_set *ms =
893 kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
896 ti->error = "Cannot allocate mirror context";
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);
907 ms->nr_mirrors = nr_mirrors;
908 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
912 atomic_set(&ms->suspend, 0);
913 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
915 ms->io_client = dm_io_client_create();
916 if (IS_ERR(ms->io_client)) {
917 ti->error = "Error creating dm_io client";
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);
936 static void free_context(struct mirror_set *ms, struct dm_target *ti,
940 dm_put_device(ti, ms->mirror[m].dev);
942 dm_io_client_destroy(ms->io_client);
943 dm_region_hash_destroy(ms->rh);
947 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
948 unsigned int mirror, char **argv)
950 unsigned long long offset;
954 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
955 offset != (sector_t)offset) {
956 ti->error = "Invalid offset";
960 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
961 &ms->mirror[mirror].dev);
963 ti->error = "Device lookup failure";
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;
976 * Create dirty log: log_type #log_params <log_params>
978 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
979 unsigned int argc, char **argv,
980 unsigned int *args_used)
982 unsigned int param_count;
983 struct dm_dirty_log *dl;
987 ti->error = "Insufficient mirror log arguments";
991 if (sscanf(argv[1], "%u%c", ¶m_count, &dummy) != 1) {
992 ti->error = "Invalid mirror log argument count";
996 *args_used = 2 + param_count;
998 if (argc < *args_used) {
999 ti->error = "Insufficient mirror log arguments";
1003 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
1006 ti->error = "Error creating mirror dirty log";
1013 static int parse_features(struct mirror_set *ms, unsigned int argc, char **argv,
1014 unsigned int *args_used)
1016 unsigned int num_features;
1017 struct dm_target *ti = ms->ti;
1026 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
1027 ti->error = "Invalid number of features";
1035 if (num_features > argc) {
1036 ti->error = "Not enough arguments to support feature count";
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;
1046 ti->error = "Unrecognised feature requested";
1054 if (!errors_handled(ms) && keep_log(ms)) {
1055 ti->error = "keep_log feature requires the handle_errors feature";
1063 * Construct a mirror mapping:
1065 * log_type #log_params <log_params>
1066 * #mirrors [mirror_path offset]{2,}
1067 * [#features <features>]
1069 * log_type is "core" or "disk"
1070 * #log_params is between 1 and 3
1072 * If present, supported features are "handle_errors" and "keep_log".
1074 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1077 unsigned int nr_mirrors, m, args_used;
1078 struct mirror_set *ms;
1079 struct dm_dirty_log *dl;
1082 dl = create_dirty_log(ti, argc, argv, &args_used);
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);
1098 if (argc < nr_mirrors * 2) {
1099 ti->error = "Too few mirror arguments";
1100 dm_dirty_log_destroy(dl);
1104 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1106 dm_dirty_log_destroy(dl);
1110 /* Get the mirror parameter sets */
1111 for (m = 0; m < nr_mirrors; m++) {
1112 r = get_mirror(ms, ti, m, argv);
1114 free_context(ms, ti, m);
1123 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1125 goto err_free_context;
1127 ti->num_flush_bios = 1;
1128 ti->num_discard_bios = 1;
1129 ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
1131 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
1132 if (!ms->kmirrord_wq) {
1133 DMERR("couldn't start kmirrord");
1135 goto err_free_context;
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);
1142 r = parse_features(ms, argc, argv, &args_used);
1144 goto err_destroy_wq;
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.
1159 ti->error = "Too many mirror arguments";
1161 goto err_destroy_wq;
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;
1174 destroy_workqueue(ms->kmirrord_wq);
1176 free_context(ms, ti, ms->nr_mirrors);
1180 static void mirror_dtr(struct dm_target *ti)
1182 struct mirror_set *ms = ti->private;
1184 timer_delete_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);
1193 * Mirror mapping function
1195 static int mirror_map(struct dm_target *ti, struct bio *bio)
1197 int r, rw = bio_data_dir(bio);
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));
1204 bio_record->details.bi_bdev = NULL;
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;
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;
1218 * If region is not in-sync queue the bio.
1220 if (!r || (r == -EWOULDBLOCK)) {
1221 if (bio->bi_opf & REQ_RAHEAD)
1222 return DM_MAPIO_KILL;
1224 queue_bio(ms, bio, rw);
1225 return DM_MAPIO_SUBMITTED;
1229 * The region is in-sync and we can perform reads directly.
1230 * Store enough information so we can retry if it fails.
1232 m = choose_mirror(ms, bio->bi_iter.bi_sector);
1234 return DM_MAPIO_KILL;
1236 dm_bio_record(&bio_record->details, bio);
1241 return DM_MAPIO_REMAPPED;
1244 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1245 blk_status_t *error)
1247 int rw = bio_data_dir(bio);
1248 struct mirror_set *ms = 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));
1255 * We need to dec pending if this was a 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;
1264 if (*error == BLK_STS_NOTSUPP)
1267 if (bio->bi_opf & REQ_RAHEAD)
1270 if (unlikely(*error)) {
1271 if (!bio_record->details.bi_bdev) {
1273 * There wasn't enough memory to record necessary
1274 * information for a retry or there was no other
1277 DMERR_LIMIT("Mirror read failed.");
1278 return DM_ENDIO_DONE;
1283 DMERR("Mirror read failed from %s. Trying alternative device.",
1286 fail_mirror(m, DM_RAID1_READ_ERROR);
1289 * A failed read is requeued for another attempt using an intact
1292 if (default_ok(m) || mirror_available(ms, bio)) {
1293 bd = &bio_record->details;
1295 dm_bio_restore(bd, bio);
1296 bio_record->details.bi_bdev = NULL;
1299 queue_bio(ms, bio, rw);
1300 return DM_ENDIO_INCOMPLETE;
1302 DMERR("All replicated volumes dead, failing I/O");
1306 bio_record->details.bi_bdev = NULL;
1308 return DM_ENDIO_DONE;
1311 static void mirror_presuspend(struct dm_target *ti)
1313 struct mirror_set *ms = ti->private;
1314 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1316 struct bio_list holds;
1319 atomic_set(&ms->suspend, 1);
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
1327 spin_lock_irq(&ms->lock);
1329 bio_list_init(&ms->holds);
1330 spin_unlock_irq(&ms->lock);
1332 while ((bio = bio_list_pop(&holds)))
1336 * We must finish up all the work that we've
1337 * generated (i.e. recovery work).
1339 dm_rh_stop_recovery(ms->rh);
1341 wait_event(_kmirrord_recovery_stopped,
1342 !dm_rh_recovery_in_flight(ms->rh));
1344 if (log->type->presuspend && log->type->presuspend(log))
1345 /* FIXME: need better error handling */
1346 DMWARN("log presuspend failed");
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.
1354 flush_workqueue(ms->kmirrord_wq);
1357 static void mirror_postsuspend(struct dm_target *ti)
1359 struct mirror_set *ms = ti->private;
1360 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1362 if (log->type->postsuspend && log->type->postsuspend(log))
1363 /* FIXME: need better error handling */
1364 DMWARN("log postsuspend failed");
1367 static void mirror_resume(struct dm_target *ti)
1369 struct mirror_set *ms = ti->private;
1370 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
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);
1380 * device_status_char
1381 * @m: mirror device/leg we want the status of
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
1392 static char device_status_char(struct mirror *m)
1394 if (!atomic_read(&(m->error_count)))
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';
1404 static void mirror_status(struct dm_target *ti, status_type_t type,
1405 unsigned int status_flags, char *result, unsigned int maxlen)
1407 unsigned int m, sz = 0;
1408 int num_feature_args = 0;
1409 struct mirror_set *ms = ti->private;
1410 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1411 char buffer[MAX_NR_MIRRORS + 1];
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]));
1422 DMEMIT("%llu/%llu 1 %s ",
1423 (unsigned long long)log->type->get_sync_count(log),
1424 (unsigned long long)ms->nr_regions, buffer);
1426 sz += log->type->status(log, type, result+sz, maxlen-sz);
1430 case STATUSTYPE_TABLE:
1431 sz = log->type->status(log, type, result, maxlen);
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);
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");
1445 DMEMIT(" keep_log");
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])));
1459 DMEMIT(",handle_errors=%c", errors_handled(ms) ? 'y' : 'n');
1460 DMEMIT(",keep_log=%c", keep_log(ms) ? 'y' : 'n');
1462 DMEMIT(",log_type_status=");
1463 sz += log->type->status(log, type, result+sz, maxlen-sz);
1469 static int mirror_iterate_devices(struct dm_target *ti,
1470 iterate_devices_callout_fn fn, void *data)
1472 struct mirror_set *ms = ti->private;
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);
1483 static struct target_type mirror_target = {
1485 .version = {1, 15, 0},
1486 .module = THIS_MODULE,
1487 .features = DM_TARGET_ATOMIC_WRITES,
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");