2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
8 #include "dm-bio-record.h"
10 #include <linux/init.h>
11 #include <linux/mempool.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/device-mapper.h>
17 #include <linux/dm-io.h>
18 #include <linux/dm-dirty-log.h>
19 #include <linux/dm-kcopyd.h>
20 #include <linux/dm-region-hash.h>
22 #define DM_MSG_PREFIX "raid1"
24 #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
26 #define MAX_NR_MIRRORS (DM_KCOPYD_MAX_REGIONS + 1)
28 #define DM_RAID1_HANDLE_ERRORS 0x01
29 #define DM_RAID1_KEEP_LOG 0x02
30 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
31 #define keep_log(p) ((p)->features & DM_RAID1_KEEP_LOG)
33 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
35 /*-----------------------------------------------------------------
36 * Mirror set structures.
37 *---------------------------------------------------------------*/
46 struct mirror_set *ms;
48 unsigned long error_type;
55 struct list_head list;
59 spinlock_t lock; /* protects the lists */
60 struct bio_list reads;
61 struct bio_list writes;
62 struct bio_list failures;
63 struct bio_list holds; /* bios are waiting until suspend */
65 struct dm_region_hash *rh;
66 struct dm_kcopyd_client *kcopyd_client;
67 struct dm_io_client *io_client;
76 atomic_t default_mirror; /* Default mirror */
78 struct workqueue_struct *kmirrord_wq;
79 struct work_struct kmirrord_work;
80 struct timer_list timer;
81 unsigned long timer_pending;
83 struct work_struct trigger_event;
86 struct mirror mirror[];
89 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
90 "A percentage of time allocated for raid resynchronization");
92 static void wakeup_mirrord(void *context)
94 struct mirror_set *ms = context;
96 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
99 static void delayed_wake_fn(struct timer_list *t)
101 struct mirror_set *ms = from_timer(ms, t, timer);
103 clear_bit(0, &ms->timer_pending);
107 static void delayed_wake(struct mirror_set *ms)
109 if (test_and_set_bit(0, &ms->timer_pending))
112 ms->timer.expires = jiffies + HZ / 5;
113 add_timer(&ms->timer);
116 static void wakeup_all_recovery_waiters(void *context)
118 wake_up_all(&_kmirrord_recovery_stopped);
121 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
127 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
128 spin_lock_irqsave(&ms->lock, flags);
129 should_wake = !(bl->head);
130 bio_list_add(bl, bio);
131 spin_unlock_irqrestore(&ms->lock, flags);
137 static void dispatch_bios(void *context, struct bio_list *bio_list)
139 struct mirror_set *ms = context;
142 while ((bio = bio_list_pop(bio_list)))
143 queue_bio(ms, bio, WRITE);
146 struct dm_raid1_bio_record {
148 /* if details->bi_bdev == NULL, details were not saved */
149 struct dm_bio_details details;
150 region_t write_region;
154 * Every mirror should look like this one.
156 #define DEFAULT_MIRROR 0
159 * This is yucky. We squirrel the mirror struct away inside
160 * bi_next for read/write buffers. This is safe since the bh
161 * doesn't get submitted to the lower levels of block layer.
163 static struct mirror *bio_get_m(struct bio *bio)
165 return (struct mirror *) bio->bi_next;
168 static void bio_set_m(struct bio *bio, struct mirror *m)
170 bio->bi_next = (struct bio *) m;
173 static struct mirror *get_default_mirror(struct mirror_set *ms)
175 return &ms->mirror[atomic_read(&ms->default_mirror)];
178 static void set_default_mirror(struct mirror *m)
180 struct mirror_set *ms = m->ms;
181 struct mirror *m0 = &(ms->mirror[0]);
183 atomic_set(&ms->default_mirror, m - m0);
186 static struct mirror *get_valid_mirror(struct mirror_set *ms)
190 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
191 if (!atomic_read(&m->error_count))
198 * @m: mirror device to fail
199 * @error_type: one of the enum's, DM_RAID1_*_ERROR
201 * If errors are being handled, record the type of
202 * error encountered for this device. If this type
203 * of error has already been recorded, we can return;
204 * otherwise, we must signal userspace by triggering
205 * an event. Additionally, if the device is the
206 * primary device, we must choose a new primary, but
207 * only if the mirror is in-sync.
209 * This function must not block.
211 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
213 struct mirror_set *ms = m->ms;
219 * error_count is used for nothing more than a
220 * simple way to tell if a device has encountered
223 atomic_inc(&m->error_count);
225 if (test_and_set_bit(error_type, &m->error_type))
228 if (!errors_handled(ms))
231 if (m != get_default_mirror(ms))
234 if (!ms->in_sync && !keep_log(ms)) {
236 * Better to issue requests to same failing device
237 * than to risk returning corrupt data.
239 DMERR("Primary mirror (%s) failed while out-of-sync: "
240 "Reads may fail.", m->dev->name);
244 new = get_valid_mirror(ms);
246 set_default_mirror(new);
248 DMWARN("All sides of mirror have failed.");
251 schedule_work(&ms->trigger_event);
254 static int mirror_flush(struct dm_target *ti)
256 struct mirror_set *ms = ti->private;
257 unsigned long error_bits;
260 struct dm_io_region io[MAX_NR_MIRRORS];
262 struct dm_io_request io_req = {
263 .bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC,
264 .mem.type = DM_IO_KMEM,
265 .mem.ptr.addr = NULL,
266 .client = ms->io_client,
269 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
270 io[i].bdev = m->dev->bdev;
276 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
277 if (unlikely(error_bits != 0)) {
278 for (i = 0; i < ms->nr_mirrors; i++)
279 if (test_bit(i, &error_bits))
280 fail_mirror(ms->mirror + i,
281 DM_RAID1_FLUSH_ERROR);
288 /*-----------------------------------------------------------------
291 * When a mirror is first activated we may find that some regions
292 * are in the no-sync state. We have to recover these by
293 * recopying from the default mirror to all the others.
294 *---------------------------------------------------------------*/
295 static void recovery_complete(int read_err, unsigned long write_err,
298 struct dm_region *reg = context;
299 struct mirror_set *ms = dm_rh_region_context(reg);
303 /* Read error means the failure of default mirror. */
304 DMERR_LIMIT("Unable to read primary mirror during recovery");
305 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
309 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
312 * Bits correspond to devices (excluding default mirror).
313 * The default mirror cannot change during recovery.
315 for (m = 0; m < ms->nr_mirrors; m++) {
316 if (&ms->mirror[m] == get_default_mirror(ms))
318 if (test_bit(bit, &write_err))
319 fail_mirror(ms->mirror + m,
320 DM_RAID1_SYNC_ERROR);
325 dm_rh_recovery_end(reg, !(read_err || write_err));
328 static void recover(struct mirror_set *ms, struct dm_region *reg)
331 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
333 unsigned long flags = 0;
334 region_t key = dm_rh_get_region_key(reg);
335 sector_t region_size = dm_rh_get_region_size(ms->rh);
337 /* fill in the source */
338 m = get_default_mirror(ms);
339 from.bdev = m->dev->bdev;
340 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
341 if (key == (ms->nr_regions - 1)) {
343 * The final region may be smaller than
346 from.count = ms->ti->len & (region_size - 1);
348 from.count = region_size;
350 from.count = region_size;
352 /* fill in the destinations */
353 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
354 if (&ms->mirror[i] == get_default_mirror(ms))
358 dest->bdev = m->dev->bdev;
359 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
360 dest->count = from.count;
365 if (!errors_handled(ms))
366 flags |= BIT(DM_KCOPYD_IGNORE_ERROR);
368 dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
369 flags, recovery_complete, reg);
372 static void reset_ms_flags(struct mirror_set *ms)
377 for (m = 0; m < ms->nr_mirrors; m++) {
378 atomic_set(&(ms->mirror[m].error_count), 0);
379 ms->mirror[m].error_type = 0;
383 static void do_recovery(struct mirror_set *ms)
385 struct dm_region *reg;
386 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
389 * Start quiescing some regions.
391 dm_rh_recovery_prepare(ms->rh);
394 * Copy any already quiesced regions.
396 while ((reg = dm_rh_recovery_start(ms->rh)))
400 * Update the in sync flag.
403 (log->type->get_sync_count(log) == ms->nr_regions)) {
404 /* the sync is complete */
405 dm_table_event(ms->ti->table);
411 /*-----------------------------------------------------------------
413 *---------------------------------------------------------------*/
414 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
416 struct mirror *m = get_default_mirror(ms);
419 if (likely(!atomic_read(&m->error_count)))
422 if (m-- == ms->mirror)
424 } while (m != get_default_mirror(ms));
429 static int default_ok(struct mirror *m)
431 struct mirror *default_mirror = get_default_mirror(m->ms);
433 return !atomic_read(&default_mirror->error_count);
436 static int mirror_available(struct mirror_set *ms, struct bio *bio)
438 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
439 region_t region = dm_rh_bio_to_region(ms->rh, bio);
441 if (log->type->in_sync(log, region, 0))
442 return choose_mirror(ms, bio->bi_iter.bi_sector) ? 1 : 0;
448 * remap a buffer to a particular mirror.
450 static sector_t map_sector(struct mirror *m, struct bio *bio)
452 if (unlikely(!bio->bi_iter.bi_size))
454 return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
457 static void map_bio(struct mirror *m, struct bio *bio)
459 bio_set_dev(bio, m->dev->bdev);
460 bio->bi_iter.bi_sector = map_sector(m, bio);
463 static void map_region(struct dm_io_region *io, struct mirror *m,
466 io->bdev = m->dev->bdev;
467 io->sector = map_sector(m, bio);
468 io->count = bio_sectors(bio);
471 static void hold_bio(struct mirror_set *ms, struct bio *bio)
474 * Lock is required to avoid race condition during suspend
477 spin_lock_irq(&ms->lock);
479 if (atomic_read(&ms->suspend)) {
480 spin_unlock_irq(&ms->lock);
483 * If device is suspended, complete the bio.
485 if (dm_noflush_suspending(ms->ti))
486 bio->bi_status = BLK_STS_DM_REQUEUE;
488 bio->bi_status = BLK_STS_IOERR;
495 * Hold bio until the suspend is complete.
497 bio_list_add(&ms->holds, bio);
498 spin_unlock_irq(&ms->lock);
501 /*-----------------------------------------------------------------
503 *---------------------------------------------------------------*/
504 static void read_callback(unsigned long error, void *context)
506 struct bio *bio = context;
510 bio_set_m(bio, NULL);
512 if (likely(!error)) {
517 fail_mirror(m, DM_RAID1_READ_ERROR);
519 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
520 DMWARN_LIMIT("Read failure on mirror device %s. "
521 "Trying alternative device.",
523 queue_bio(m->ms, bio, bio_data_dir(bio));
527 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
532 /* Asynchronous read. */
533 static void read_async_bio(struct mirror *m, struct bio *bio)
535 struct dm_io_region io;
536 struct dm_io_request io_req = {
537 .bi_opf = REQ_OP_READ,
538 .mem.type = DM_IO_BIO,
540 .notify.fn = read_callback,
541 .notify.context = bio,
542 .client = m->ms->io_client,
545 map_region(&io, m, bio);
547 BUG_ON(dm_io(&io_req, 1, &io, NULL));
550 static inline int region_in_sync(struct mirror_set *ms, region_t region,
553 int state = dm_rh_get_state(ms->rh, region, may_block);
554 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
557 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
563 while ((bio = bio_list_pop(reads))) {
564 region = dm_rh_bio_to_region(ms->rh, bio);
565 m = get_default_mirror(ms);
568 * We can only read balance if the region is in sync.
570 if (likely(region_in_sync(ms, region, 1)))
571 m = choose_mirror(ms, bio->bi_iter.bi_sector);
572 else if (m && atomic_read(&m->error_count))
576 read_async_bio(m, bio);
582 /*-----------------------------------------------------------------
585 * We do different things with the write io depending on the
586 * state of the region that it's in:
588 * SYNC: increment pending, use kcopyd to write to *all* mirrors
589 * RECOVERING: delay the io until recovery completes
590 * NOSYNC: increment pending, just write to the default mirror
591 *---------------------------------------------------------------*/
594 static void write_callback(unsigned long error, void *context)
597 struct bio *bio = (struct bio *) context;
598 struct mirror_set *ms;
602 ms = bio_get_m(bio)->ms;
603 bio_set_m(bio, NULL);
606 * NOTE: We don't decrement the pending count here,
607 * instead it is done by the targets endio function.
608 * This way we handle both writes to SYNC and NOSYNC
609 * regions with the same code.
611 if (likely(!error)) {
617 * If the bio is discard, return an error, but do not
620 if (bio_op(bio) == REQ_OP_DISCARD) {
621 bio->bi_status = BLK_STS_NOTSUPP;
626 for (i = 0; i < ms->nr_mirrors; i++)
627 if (test_bit(i, &error))
628 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
631 * Need to raise event. Since raising
632 * events can block, we need to do it in
635 spin_lock_irqsave(&ms->lock, flags);
636 if (!ms->failures.head)
638 bio_list_add(&ms->failures, bio);
639 spin_unlock_irqrestore(&ms->lock, flags);
644 static void do_write(struct mirror_set *ms, struct bio *bio)
647 struct dm_io_region io[MAX_NR_MIRRORS], *dest = io;
649 blk_opf_t op_flags = bio->bi_opf & (REQ_FUA | REQ_PREFLUSH);
650 struct dm_io_request io_req = {
651 .bi_opf = REQ_OP_WRITE | op_flags,
652 .mem.type = DM_IO_BIO,
654 .notify.fn = write_callback,
655 .notify.context = bio,
656 .client = ms->io_client,
659 if (bio_op(bio) == REQ_OP_DISCARD) {
660 io_req.bi_opf = REQ_OP_DISCARD | op_flags;
661 io_req.mem.type = DM_IO_KMEM;
662 io_req.mem.ptr.addr = NULL;
665 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
666 map_region(dest++, m, bio);
669 * Use default mirror because we only need it to retrieve the reference
670 * to the mirror set in write_callback().
672 bio_set_m(bio, get_default_mirror(ms));
674 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
677 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
681 struct bio_list sync, nosync, recover, *this_list = NULL;
682 struct bio_list requeue;
683 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
690 * Classify each write.
692 bio_list_init(&sync);
693 bio_list_init(&nosync);
694 bio_list_init(&recover);
695 bio_list_init(&requeue);
697 while ((bio = bio_list_pop(writes))) {
698 if ((bio->bi_opf & REQ_PREFLUSH) ||
699 (bio_op(bio) == REQ_OP_DISCARD)) {
700 bio_list_add(&sync, bio);
704 region = dm_rh_bio_to_region(ms->rh, bio);
706 if (log->type->is_remote_recovering &&
707 log->type->is_remote_recovering(log, region)) {
708 bio_list_add(&requeue, bio);
712 state = dm_rh_get_state(ms->rh, region, 1);
723 case DM_RH_RECOVERING:
724 this_list = &recover;
728 bio_list_add(this_list, bio);
732 * Add bios that are delayed due to remote recovery
733 * back on to the write queue
735 if (unlikely(requeue.head)) {
736 spin_lock_irq(&ms->lock);
737 bio_list_merge(&ms->writes, &requeue);
738 spin_unlock_irq(&ms->lock);
743 * Increment the pending counts for any regions that will
744 * be written to (writes to recover regions are going to
747 dm_rh_inc_pending(ms->rh, &sync);
748 dm_rh_inc_pending(ms->rh, &nosync);
751 * If the flush fails on a previous call and succeeds here,
752 * we must not reset the log_failure variable. We need
753 * userspace interaction to do that.
755 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
760 if (unlikely(ms->log_failure) && errors_handled(ms)) {
761 spin_lock_irq(&ms->lock);
762 bio_list_merge(&ms->failures, &sync);
763 spin_unlock_irq(&ms->lock);
766 while ((bio = bio_list_pop(&sync)))
769 while ((bio = bio_list_pop(&recover)))
770 dm_rh_delay(ms->rh, bio);
772 while ((bio = bio_list_pop(&nosync))) {
773 if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
774 spin_lock_irq(&ms->lock);
775 bio_list_add(&ms->failures, bio);
776 spin_unlock_irq(&ms->lock);
779 map_bio(get_default_mirror(ms), bio);
780 submit_bio_noacct(bio);
785 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
789 if (likely(!failures->head))
793 * If the log has failed, unattempted writes are being
794 * put on the holds list. We can't issue those writes
795 * until a log has been marked, so we must store them.
797 * If a 'noflush' suspend is in progress, we can requeue
798 * the I/O's to the core. This give userspace a chance
799 * to reconfigure the mirror, at which point the core
800 * will reissue the writes. If the 'noflush' flag is
801 * not set, we have no choice but to return errors.
803 * Some writes on the failures list may have been
804 * submitted before the log failure and represent a
805 * failure to write to one of the devices. It is ok
806 * for us to treat them the same and requeue them
809 while ((bio = bio_list_pop(failures))) {
810 if (!ms->log_failure) {
812 dm_rh_mark_nosync(ms->rh, bio);
816 * If all the legs are dead, fail the I/O.
817 * If the device has failed and keep_log is enabled,
820 * If we have been told to handle errors, and keep_log
821 * isn't enabled, hold the bio and wait for userspace to
822 * deal with the problem.
824 * Otherwise pretend that the I/O succeeded. (This would
825 * be wrong if the failed leg returned after reboot and
826 * got replicated back to the good legs.)
828 if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
830 else if (errors_handled(ms) && !keep_log(ms))
837 static void trigger_event(struct work_struct *work)
839 struct mirror_set *ms =
840 container_of(work, struct mirror_set, trigger_event);
842 dm_table_event(ms->ti->table);
845 /*-----------------------------------------------------------------
847 *---------------------------------------------------------------*/
848 static void do_mirror(struct work_struct *work)
850 struct mirror_set *ms = container_of(work, struct mirror_set,
852 struct bio_list reads, writes, failures;
855 spin_lock_irqsave(&ms->lock, flags);
858 failures = ms->failures;
859 bio_list_init(&ms->reads);
860 bio_list_init(&ms->writes);
861 bio_list_init(&ms->failures);
862 spin_unlock_irqrestore(&ms->lock, flags);
864 dm_rh_update_states(ms->rh, errors_handled(ms));
866 do_reads(ms, &reads);
867 do_writes(ms, &writes);
868 do_failures(ms, &failures);
871 /*-----------------------------------------------------------------
873 *---------------------------------------------------------------*/
874 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
875 uint32_t region_size,
876 struct dm_target *ti,
877 struct dm_dirty_log *dl)
879 struct mirror_set *ms =
880 kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
883 ti->error = "Cannot allocate mirror context";
887 spin_lock_init(&ms->lock);
888 bio_list_init(&ms->reads);
889 bio_list_init(&ms->writes);
890 bio_list_init(&ms->failures);
891 bio_list_init(&ms->holds);
894 ms->nr_mirrors = nr_mirrors;
895 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
899 atomic_set(&ms->suspend, 0);
900 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
902 ms->io_client = dm_io_client_create();
903 if (IS_ERR(ms->io_client)) {
904 ti->error = "Error creating dm_io client";
909 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
910 wakeup_all_recovery_waiters,
911 ms->ti->begin, MAX_RECOVERY,
912 dl, region_size, ms->nr_regions);
913 if (IS_ERR(ms->rh)) {
914 ti->error = "Error creating dirty region hash";
915 dm_io_client_destroy(ms->io_client);
923 static void free_context(struct mirror_set *ms, struct dm_target *ti,
927 dm_put_device(ti, ms->mirror[m].dev);
929 dm_io_client_destroy(ms->io_client);
930 dm_region_hash_destroy(ms->rh);
934 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
935 unsigned int mirror, char **argv)
937 unsigned long long offset;
941 if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
942 offset != (sector_t)offset) {
943 ti->error = "Invalid offset";
947 ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
948 &ms->mirror[mirror].dev);
950 ti->error = "Device lookup failure";
954 ms->mirror[mirror].ms = ms;
955 atomic_set(&(ms->mirror[mirror].error_count), 0);
956 ms->mirror[mirror].error_type = 0;
957 ms->mirror[mirror].offset = offset;
963 * Create dirty log: log_type #log_params <log_params>
965 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
966 unsigned argc, char **argv,
969 unsigned param_count;
970 struct dm_dirty_log *dl;
974 ti->error = "Insufficient mirror log arguments";
978 if (sscanf(argv[1], "%u%c", ¶m_count, &dummy) != 1) {
979 ti->error = "Invalid mirror log argument count";
983 *args_used = 2 + param_count;
985 if (argc < *args_used) {
986 ti->error = "Insufficient mirror log arguments";
990 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
993 ti->error = "Error creating mirror dirty log";
1000 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1001 unsigned *args_used)
1003 unsigned num_features;
1004 struct dm_target *ti = ms->ti;
1013 if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
1014 ti->error = "Invalid number of features";
1022 if (num_features > argc) {
1023 ti->error = "Not enough arguments to support feature count";
1027 for (i = 0; i < num_features; i++) {
1028 if (!strcmp("handle_errors", argv[0]))
1029 ms->features |= DM_RAID1_HANDLE_ERRORS;
1030 else if (!strcmp("keep_log", argv[0]))
1031 ms->features |= DM_RAID1_KEEP_LOG;
1033 ti->error = "Unrecognised feature requested";
1041 if (!errors_handled(ms) && keep_log(ms)) {
1042 ti->error = "keep_log feature requires the handle_errors feature";
1050 * Construct a mirror mapping:
1052 * log_type #log_params <log_params>
1053 * #mirrors [mirror_path offset]{2,}
1054 * [#features <features>]
1056 * log_type is "core" or "disk"
1057 * #log_params is between 1 and 3
1059 * If present, supported features are "handle_errors" and "keep_log".
1061 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1064 unsigned int nr_mirrors, m, args_used;
1065 struct mirror_set *ms;
1066 struct dm_dirty_log *dl;
1069 dl = create_dirty_log(ti, argc, argv, &args_used);
1076 if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
1077 nr_mirrors < 2 || nr_mirrors > MAX_NR_MIRRORS) {
1078 ti->error = "Invalid number of mirrors";
1079 dm_dirty_log_destroy(dl);
1085 if (argc < nr_mirrors * 2) {
1086 ti->error = "Too few mirror arguments";
1087 dm_dirty_log_destroy(dl);
1091 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1093 dm_dirty_log_destroy(dl);
1097 /* Get the mirror parameter sets */
1098 for (m = 0; m < nr_mirrors; m++) {
1099 r = get_mirror(ms, ti, m, argv);
1101 free_context(ms, ti, m);
1110 r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
1112 goto err_free_context;
1114 ti->num_flush_bios = 1;
1115 ti->num_discard_bios = 1;
1116 ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
1118 ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
1119 if (!ms->kmirrord_wq) {
1120 DMERR("couldn't start kmirrord");
1122 goto err_free_context;
1124 INIT_WORK(&ms->kmirrord_work, do_mirror);
1125 timer_setup(&ms->timer, delayed_wake_fn, 0);
1126 ms->timer_pending = 0;
1127 INIT_WORK(&ms->trigger_event, trigger_event);
1129 r = parse_features(ms, argc, argv, &args_used);
1131 goto err_destroy_wq;
1137 * Any read-balancing addition depends on the
1138 * DM_RAID1_HANDLE_ERRORS flag being present.
1139 * This is because the decision to balance depends
1140 * on the sync state of a region. If the above
1141 * flag is not present, we ignore errors; and
1142 * the sync state may be inaccurate.
1146 ti->error = "Too many mirror arguments";
1148 goto err_destroy_wq;
1151 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1152 if (IS_ERR(ms->kcopyd_client)) {
1153 r = PTR_ERR(ms->kcopyd_client);
1154 goto err_destroy_wq;
1161 destroy_workqueue(ms->kmirrord_wq);
1163 free_context(ms, ti, ms->nr_mirrors);
1167 static void mirror_dtr(struct dm_target *ti)
1169 struct mirror_set *ms = (struct mirror_set *) ti->private;
1171 del_timer_sync(&ms->timer);
1172 flush_workqueue(ms->kmirrord_wq);
1173 flush_work(&ms->trigger_event);
1174 dm_kcopyd_client_destroy(ms->kcopyd_client);
1175 destroy_workqueue(ms->kmirrord_wq);
1176 free_context(ms, ti, ms->nr_mirrors);
1180 * Mirror mapping function
1182 static int mirror_map(struct dm_target *ti, struct bio *bio)
1184 int r, rw = bio_data_dir(bio);
1186 struct mirror_set *ms = ti->private;
1187 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1188 struct dm_raid1_bio_record *bio_record =
1189 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1191 bio_record->details.bi_bdev = NULL;
1194 /* Save region for mirror_end_io() handler */
1195 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
1196 queue_bio(ms, bio, rw);
1197 return DM_MAPIO_SUBMITTED;
1200 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1201 if (r < 0 && r != -EWOULDBLOCK)
1202 return DM_MAPIO_KILL;
1205 * If region is not in-sync queue the bio.
1207 if (!r || (r == -EWOULDBLOCK)) {
1208 if (bio->bi_opf & REQ_RAHEAD)
1209 return DM_MAPIO_KILL;
1211 queue_bio(ms, bio, rw);
1212 return DM_MAPIO_SUBMITTED;
1216 * The region is in-sync and we can perform reads directly.
1217 * Store enough information so we can retry if it fails.
1219 m = choose_mirror(ms, bio->bi_iter.bi_sector);
1221 return DM_MAPIO_KILL;
1223 dm_bio_record(&bio_record->details, bio);
1228 return DM_MAPIO_REMAPPED;
1231 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1232 blk_status_t *error)
1234 int rw = bio_data_dir(bio);
1235 struct mirror_set *ms = (struct mirror_set *) ti->private;
1236 struct mirror *m = NULL;
1237 struct dm_bio_details *bd = NULL;
1238 struct dm_raid1_bio_record *bio_record =
1239 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1242 * We need to dec pending if this was a write.
1245 if (!(bio->bi_opf & REQ_PREFLUSH) &&
1246 bio_op(bio) != REQ_OP_DISCARD)
1247 dm_rh_dec(ms->rh, bio_record->write_region);
1248 return DM_ENDIO_DONE;
1251 if (*error == BLK_STS_NOTSUPP)
1254 if (bio->bi_opf & REQ_RAHEAD)
1257 if (unlikely(*error)) {
1258 if (!bio_record->details.bi_bdev) {
1260 * There wasn't enough memory to record necessary
1261 * information for a retry or there was no other
1264 DMERR_LIMIT("Mirror read failed.");
1265 return DM_ENDIO_DONE;
1270 DMERR("Mirror read failed from %s. Trying alternative device.",
1273 fail_mirror(m, DM_RAID1_READ_ERROR);
1276 * A failed read is requeued for another attempt using an intact
1279 if (default_ok(m) || mirror_available(ms, bio)) {
1280 bd = &bio_record->details;
1282 dm_bio_restore(bd, bio);
1283 bio_record->details.bi_bdev = NULL;
1286 queue_bio(ms, bio, rw);
1287 return DM_ENDIO_INCOMPLETE;
1289 DMERR("All replicated volumes dead, failing I/O");
1293 bio_record->details.bi_bdev = NULL;
1295 return DM_ENDIO_DONE;
1298 static void mirror_presuspend(struct dm_target *ti)
1300 struct mirror_set *ms = (struct mirror_set *) ti->private;
1301 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1303 struct bio_list holds;
1306 atomic_set(&ms->suspend, 1);
1309 * Process bios in the hold list to start recovery waiting
1310 * for bios in the hold list. After the process, no bio has
1311 * a chance to be added in the hold list because ms->suspend
1314 spin_lock_irq(&ms->lock);
1316 bio_list_init(&ms->holds);
1317 spin_unlock_irq(&ms->lock);
1319 while ((bio = bio_list_pop(&holds)))
1323 * We must finish up all the work that we've
1324 * generated (i.e. recovery work).
1326 dm_rh_stop_recovery(ms->rh);
1328 wait_event(_kmirrord_recovery_stopped,
1329 !dm_rh_recovery_in_flight(ms->rh));
1331 if (log->type->presuspend && log->type->presuspend(log))
1332 /* FIXME: need better error handling */
1333 DMWARN("log presuspend failed");
1336 * Now that recovery is complete/stopped and the
1337 * delayed bios are queued, we need to wait for
1338 * the worker thread to complete. This way,
1339 * we know that all of our I/O has been pushed.
1341 flush_workqueue(ms->kmirrord_wq);
1344 static void mirror_postsuspend(struct dm_target *ti)
1346 struct mirror_set *ms = ti->private;
1347 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1349 if (log->type->postsuspend && log->type->postsuspend(log))
1350 /* FIXME: need better error handling */
1351 DMWARN("log postsuspend failed");
1354 static void mirror_resume(struct dm_target *ti)
1356 struct mirror_set *ms = ti->private;
1357 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1359 atomic_set(&ms->suspend, 0);
1360 if (log->type->resume && log->type->resume(log))
1361 /* FIXME: need better error handling */
1362 DMWARN("log resume failed");
1363 dm_rh_start_recovery(ms->rh);
1367 * device_status_char
1368 * @m: mirror device/leg we want the status of
1370 * We return one character representing the most severe error
1371 * we have encountered.
1372 * A => Alive - No failures
1373 * D => Dead - A write failure occurred leaving mirror out-of-sync
1374 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1375 * R => Read - A read failure occurred, mirror data unaffected
1379 static char device_status_char(struct mirror *m)
1381 if (!atomic_read(&(m->error_count)))
1384 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1385 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1386 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1387 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1391 static void mirror_status(struct dm_target *ti, status_type_t type,
1392 unsigned status_flags, char *result, unsigned maxlen)
1394 unsigned int m, sz = 0;
1395 int num_feature_args = 0;
1396 struct mirror_set *ms = (struct mirror_set *) ti->private;
1397 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1398 char buffer[MAX_NR_MIRRORS + 1];
1401 case STATUSTYPE_INFO:
1402 DMEMIT("%d ", ms->nr_mirrors);
1403 for (m = 0; m < ms->nr_mirrors; m++) {
1404 DMEMIT("%s ", ms->mirror[m].dev->name);
1405 buffer[m] = device_status_char(&(ms->mirror[m]));
1409 DMEMIT("%llu/%llu 1 %s ",
1410 (unsigned long long)log->type->get_sync_count(log),
1411 (unsigned long long)ms->nr_regions, buffer);
1413 sz += log->type->status(log, type, result+sz, maxlen-sz);
1417 case STATUSTYPE_TABLE:
1418 sz = log->type->status(log, type, result, maxlen);
1420 DMEMIT("%d", ms->nr_mirrors);
1421 for (m = 0; m < ms->nr_mirrors; m++)
1422 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1423 (unsigned long long)ms->mirror[m].offset);
1425 num_feature_args += !!errors_handled(ms);
1426 num_feature_args += !!keep_log(ms);
1427 if (num_feature_args) {
1428 DMEMIT(" %d", num_feature_args);
1429 if (errors_handled(ms))
1430 DMEMIT(" handle_errors");
1432 DMEMIT(" keep_log");
1437 case STATUSTYPE_IMA:
1438 DMEMIT_TARGET_NAME_VERSION(ti->type);
1439 DMEMIT(",nr_mirrors=%d", ms->nr_mirrors);
1440 for (m = 0; m < ms->nr_mirrors; m++) {
1441 DMEMIT(",mirror_device_%d=%s", m, ms->mirror[m].dev->name);
1442 DMEMIT(",mirror_device_%d_status=%c",
1443 m, device_status_char(&(ms->mirror[m])));
1446 DMEMIT(",handle_errors=%c", errors_handled(ms) ? 'y' : 'n');
1447 DMEMIT(",keep_log=%c", keep_log(ms) ? 'y' : 'n');
1449 DMEMIT(",log_type_status=");
1450 sz += log->type->status(log, type, result+sz, maxlen-sz);
1456 static int mirror_iterate_devices(struct dm_target *ti,
1457 iterate_devices_callout_fn fn, void *data)
1459 struct mirror_set *ms = ti->private;
1463 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1464 ret = fn(ti, ms->mirror[i].dev,
1465 ms->mirror[i].offset, ti->len, data);
1470 static struct target_type mirror_target = {
1472 .version = {1, 14, 0},
1473 .module = THIS_MODULE,
1477 .end_io = mirror_end_io,
1478 .presuspend = mirror_presuspend,
1479 .postsuspend = mirror_postsuspend,
1480 .resume = mirror_resume,
1481 .status = mirror_status,
1482 .iterate_devices = mirror_iterate_devices,
1485 static int __init dm_mirror_init(void)
1489 r = dm_register_target(&mirror_target);
1491 DMERR("Failed to register mirror target");
1501 static void __exit dm_mirror_exit(void)
1503 dm_unregister_target(&mirror_target);
1507 module_init(dm_mirror_init);
1508 module_exit(dm_mirror_exit);
1510 MODULE_DESCRIPTION(DM_NAME " mirror target");
1511 MODULE_AUTHOR("Joe Thornber");
1512 MODULE_LICENSE("GPL");