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. */
25 #define DM_IO_PAGES 64
26 #define DM_KCOPYD_PAGES 64
28 #define DM_RAID1_HANDLE_ERRORS 0x01
29 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
31 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
33 /*-----------------------------------------------------------------
34 * Mirror set structures.
35 *---------------------------------------------------------------*/
44 struct mirror_set *ms;
46 unsigned long error_type;
53 struct list_head list;
57 spinlock_t lock; /* protects the lists */
58 struct bio_list reads;
59 struct bio_list writes;
60 struct bio_list failures;
61 struct bio_list holds; /* bios are waiting until suspend */
63 struct dm_region_hash *rh;
64 struct dm_kcopyd_client *kcopyd_client;
65 struct dm_io_client *io_client;
66 mempool_t *read_record_pool;
74 atomic_t default_mirror; /* Default mirror */
76 struct workqueue_struct *kmirrord_wq;
77 struct work_struct kmirrord_work;
78 struct timer_list timer;
79 unsigned long timer_pending;
81 struct work_struct trigger_event;
84 struct mirror mirror[0];
87 static void wakeup_mirrord(void *context)
89 struct mirror_set *ms = context;
91 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
94 static void delayed_wake_fn(unsigned long data)
96 struct mirror_set *ms = (struct mirror_set *) data;
98 clear_bit(0, &ms->timer_pending);
102 static void delayed_wake(struct mirror_set *ms)
104 if (test_and_set_bit(0, &ms->timer_pending))
107 ms->timer.expires = jiffies + HZ / 5;
108 ms->timer.data = (unsigned long) ms;
109 ms->timer.function = delayed_wake_fn;
110 add_timer(&ms->timer);
113 static void wakeup_all_recovery_waiters(void *context)
115 wake_up_all(&_kmirrord_recovery_stopped);
118 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
124 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
125 spin_lock_irqsave(&ms->lock, flags);
126 should_wake = !(bl->head);
127 bio_list_add(bl, bio);
128 spin_unlock_irqrestore(&ms->lock, flags);
134 static void dispatch_bios(void *context, struct bio_list *bio_list)
136 struct mirror_set *ms = context;
139 while ((bio = bio_list_pop(bio_list)))
140 queue_bio(ms, bio, WRITE);
143 #define MIN_READ_RECORDS 20
144 struct dm_raid1_read_record {
146 struct dm_bio_details details;
149 static struct kmem_cache *_dm_raid1_read_record_cache;
152 * Every mirror should look like this one.
154 #define DEFAULT_MIRROR 0
157 * This is yucky. We squirrel the mirror struct away inside
158 * bi_next for read/write buffers. This is safe since the bh
159 * doesn't get submitted to the lower levels of block layer.
161 static struct mirror *bio_get_m(struct bio *bio)
163 return (struct mirror *) bio->bi_next;
166 static void bio_set_m(struct bio *bio, struct mirror *m)
168 bio->bi_next = (struct bio *) m;
171 static struct mirror *get_default_mirror(struct mirror_set *ms)
173 return &ms->mirror[atomic_read(&ms->default_mirror)];
176 static void set_default_mirror(struct mirror *m)
178 struct mirror_set *ms = m->ms;
179 struct mirror *m0 = &(ms->mirror[0]);
181 atomic_set(&ms->default_mirror, m - m0);
184 static struct mirror *get_valid_mirror(struct mirror_set *ms)
188 for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
189 if (!atomic_read(&m->error_count))
196 * @m: mirror device to fail
197 * @error_type: one of the enum's, DM_RAID1_*_ERROR
199 * If errors are being handled, record the type of
200 * error encountered for this device. If this type
201 * of error has already been recorded, we can return;
202 * otherwise, we must signal userspace by triggering
203 * an event. Additionally, if the device is the
204 * primary device, we must choose a new primary, but
205 * only if the mirror is in-sync.
207 * This function must not block.
209 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
211 struct mirror_set *ms = m->ms;
215 * error_count is used for nothing more than a
216 * simple way to tell if a device has encountered
219 atomic_inc(&m->error_count);
221 if (test_and_set_bit(error_type, &m->error_type))
224 if (!errors_handled(ms))
227 if (m != get_default_mirror(ms))
232 * Better to issue requests to same failing device
233 * than to risk returning corrupt data.
235 DMERR("Primary mirror (%s) failed while out-of-sync: "
236 "Reads may fail.", m->dev->name);
240 new = get_valid_mirror(ms);
242 set_default_mirror(new);
244 DMWARN("All sides of mirror have failed.");
247 schedule_work(&ms->trigger_event);
250 static int mirror_flush(struct dm_target *ti)
252 struct mirror_set *ms = ti->private;
253 unsigned long error_bits;
256 struct dm_io_region io[ms->nr_mirrors];
258 struct dm_io_request io_req = {
259 .bi_rw = WRITE_BARRIER,
260 .mem.type = DM_IO_KMEM,
261 .mem.ptr.bvec = NULL,
262 .client = ms->io_client,
265 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
266 io[i].bdev = m->dev->bdev;
272 dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
273 if (unlikely(error_bits != 0)) {
274 for (i = 0; i < ms->nr_mirrors; i++)
275 if (test_bit(i, &error_bits))
276 fail_mirror(ms->mirror + i,
277 DM_RAID1_FLUSH_ERROR);
284 /*-----------------------------------------------------------------
287 * When a mirror is first activated we may find that some regions
288 * are in the no-sync state. We have to recover these by
289 * recopying from the default mirror to all the others.
290 *---------------------------------------------------------------*/
291 static void recovery_complete(int read_err, unsigned long write_err,
294 struct dm_region *reg = context;
295 struct mirror_set *ms = dm_rh_region_context(reg);
299 /* Read error means the failure of default mirror. */
300 DMERR_LIMIT("Unable to read primary mirror during recovery");
301 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
305 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
308 * Bits correspond to devices (excluding default mirror).
309 * The default mirror cannot change during recovery.
311 for (m = 0; m < ms->nr_mirrors; m++) {
312 if (&ms->mirror[m] == get_default_mirror(ms))
314 if (test_bit(bit, &write_err))
315 fail_mirror(ms->mirror + m,
316 DM_RAID1_SYNC_ERROR);
321 dm_rh_recovery_end(reg, !(read_err || write_err));
324 static int recover(struct mirror_set *ms, struct dm_region *reg)
328 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
330 unsigned long flags = 0;
331 region_t key = dm_rh_get_region_key(reg);
332 sector_t region_size = dm_rh_get_region_size(ms->rh);
334 /* fill in the source */
335 m = get_default_mirror(ms);
336 from.bdev = m->dev->bdev;
337 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
338 if (key == (ms->nr_regions - 1)) {
340 * The final region may be smaller than
343 from.count = ms->ti->len & (region_size - 1);
345 from.count = region_size;
347 from.count = region_size;
349 /* fill in the destinations */
350 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
351 if (&ms->mirror[i] == get_default_mirror(ms))
355 dest->bdev = m->dev->bdev;
356 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
357 dest->count = from.count;
362 if (!errors_handled(ms))
363 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
365 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
366 flags, recovery_complete, reg);
371 static void do_recovery(struct mirror_set *ms)
373 struct dm_region *reg;
374 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
378 * Start quiescing some regions.
380 dm_rh_recovery_prepare(ms->rh);
383 * Copy any already quiesced regions.
385 while ((reg = dm_rh_recovery_start(ms->rh))) {
386 r = recover(ms, reg);
388 dm_rh_recovery_end(reg, 0);
392 * Update the in sync flag.
395 (log->type->get_sync_count(log) == ms->nr_regions)) {
396 /* the sync is complete */
397 dm_table_event(ms->ti->table);
402 /*-----------------------------------------------------------------
404 *---------------------------------------------------------------*/
405 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
407 struct mirror *m = get_default_mirror(ms);
410 if (likely(!atomic_read(&m->error_count)))
413 if (m-- == ms->mirror)
415 } while (m != get_default_mirror(ms));
420 static int default_ok(struct mirror *m)
422 struct mirror *default_mirror = get_default_mirror(m->ms);
424 return !atomic_read(&default_mirror->error_count);
427 static int mirror_available(struct mirror_set *ms, struct bio *bio)
429 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
430 region_t region = dm_rh_bio_to_region(ms->rh, bio);
432 if (log->type->in_sync(log, region, 0))
433 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
439 * remap a buffer to a particular mirror.
441 static sector_t map_sector(struct mirror *m, struct bio *bio)
443 if (unlikely(!bio->bi_size))
445 return m->offset + (bio->bi_sector - m->ms->ti->begin);
448 static void map_bio(struct mirror *m, struct bio *bio)
450 bio->bi_bdev = m->dev->bdev;
451 bio->bi_sector = map_sector(m, bio);
454 static void map_region(struct dm_io_region *io, struct mirror *m,
457 io->bdev = m->dev->bdev;
458 io->sector = map_sector(m, bio);
459 io->count = bio->bi_size >> 9;
462 static void hold_bio(struct mirror_set *ms, struct bio *bio)
465 * If device is suspended, complete the bio.
467 if (atomic_read(&ms->suspend)) {
468 if (dm_noflush_suspending(ms->ti))
469 bio_endio(bio, DM_ENDIO_REQUEUE);
471 bio_endio(bio, -EIO);
476 * Hold bio until the suspend is complete.
478 spin_lock_irq(&ms->lock);
479 bio_list_add(&ms->holds, bio);
480 spin_unlock_irq(&ms->lock);
483 /*-----------------------------------------------------------------
485 *---------------------------------------------------------------*/
486 static void read_callback(unsigned long error, void *context)
488 struct bio *bio = context;
492 bio_set_m(bio, NULL);
494 if (likely(!error)) {
499 fail_mirror(m, DM_RAID1_READ_ERROR);
501 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
502 DMWARN_LIMIT("Read failure on mirror device %s. "
503 "Trying alternative device.",
505 queue_bio(m->ms, bio, bio_rw(bio));
509 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
511 bio_endio(bio, -EIO);
514 /* Asynchronous read. */
515 static void read_async_bio(struct mirror *m, struct bio *bio)
517 struct dm_io_region io;
518 struct dm_io_request io_req = {
520 .mem.type = DM_IO_BVEC,
521 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
522 .notify.fn = read_callback,
523 .notify.context = bio,
524 .client = m->ms->io_client,
527 map_region(&io, m, bio);
529 BUG_ON(dm_io(&io_req, 1, &io, NULL));
532 static inline int region_in_sync(struct mirror_set *ms, region_t region,
535 int state = dm_rh_get_state(ms->rh, region, may_block);
536 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
539 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
545 while ((bio = bio_list_pop(reads))) {
546 region = dm_rh_bio_to_region(ms->rh, bio);
547 m = get_default_mirror(ms);
550 * We can only read balance if the region is in sync.
552 if (likely(region_in_sync(ms, region, 1)))
553 m = choose_mirror(ms, bio->bi_sector);
554 else if (m && atomic_read(&m->error_count))
558 read_async_bio(m, bio);
560 bio_endio(bio, -EIO);
564 /*-----------------------------------------------------------------
567 * We do different things with the write io depending on the
568 * state of the region that it's in:
570 * SYNC: increment pending, use kcopyd to write to *all* mirrors
571 * RECOVERING: delay the io until recovery completes
572 * NOSYNC: increment pending, just write to the default mirror
573 *---------------------------------------------------------------*/
576 static void write_callback(unsigned long error, void *context)
579 struct bio *bio = (struct bio *) context;
580 struct mirror_set *ms;
585 ms = bio_get_m(bio)->ms;
586 bio_set_m(bio, NULL);
589 * NOTE: We don't decrement the pending count here,
590 * instead it is done by the targets endio function.
591 * This way we handle both writes to SYNC and NOSYNC
592 * regions with the same code.
597 for (i = 0; i < ms->nr_mirrors; i++)
598 if (test_bit(i, &error))
599 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
603 if (unlikely(!uptodate)) {
604 DMERR("All replicated volumes dead, failing I/O");
605 /* None of the writes succeeded, fail the I/O. */
607 } else if (errors_handled(ms)) {
609 * Need to raise event. Since raising
610 * events can block, we need to do it in
613 spin_lock_irqsave(&ms->lock, flags);
614 if (!ms->failures.head)
616 bio_list_add(&ms->failures, bio);
617 spin_unlock_irqrestore(&ms->lock, flags);
626 static void do_write(struct mirror_set *ms, struct bio *bio)
629 struct dm_io_region io[ms->nr_mirrors], *dest = io;
631 struct dm_io_request io_req = {
632 .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
633 .mem.type = DM_IO_BVEC,
634 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
635 .notify.fn = write_callback,
636 .notify.context = bio,
637 .client = ms->io_client,
640 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
641 map_region(dest++, m, bio);
644 * Use default mirror because we only need it to retrieve the reference
645 * to the mirror set in write_callback().
647 bio_set_m(bio, get_default_mirror(ms));
649 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
652 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
656 struct bio_list sync, nosync, recover, *this_list = NULL;
657 struct bio_list requeue;
658 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
665 * Classify each write.
667 bio_list_init(&sync);
668 bio_list_init(&nosync);
669 bio_list_init(&recover);
670 bio_list_init(&requeue);
672 while ((bio = bio_list_pop(writes))) {
673 if (unlikely(bio_empty_barrier(bio))) {
674 bio_list_add(&sync, bio);
678 region = dm_rh_bio_to_region(ms->rh, bio);
680 if (log->type->is_remote_recovering &&
681 log->type->is_remote_recovering(log, region)) {
682 bio_list_add(&requeue, bio);
686 state = dm_rh_get_state(ms->rh, region, 1);
697 case DM_RH_RECOVERING:
698 this_list = &recover;
702 bio_list_add(this_list, bio);
706 * Add bios that are delayed due to remote recovery
707 * back on to the write queue
709 if (unlikely(requeue.head)) {
710 spin_lock_irq(&ms->lock);
711 bio_list_merge(&ms->writes, &requeue);
712 spin_unlock_irq(&ms->lock);
717 * Increment the pending counts for any regions that will
718 * be written to (writes to recover regions are going to
721 dm_rh_inc_pending(ms->rh, &sync);
722 dm_rh_inc_pending(ms->rh, &nosync);
725 * If the flush fails on a previous call and succeeds here,
726 * we must not reset the log_failure variable. We need
727 * userspace interaction to do that.
729 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
734 if (unlikely(ms->log_failure)) {
735 spin_lock_irq(&ms->lock);
736 bio_list_merge(&ms->failures, &sync);
737 spin_unlock_irq(&ms->lock);
740 while ((bio = bio_list_pop(&sync)))
743 while ((bio = bio_list_pop(&recover)))
744 dm_rh_delay(ms->rh, bio);
746 while ((bio = bio_list_pop(&nosync))) {
747 map_bio(get_default_mirror(ms), bio);
748 generic_make_request(bio);
752 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
756 if (likely(!failures->head))
760 * If the log has failed, unattempted writes are being
761 * put on the holds list. We can't issue those writes
762 * until a log has been marked, so we must store them.
764 * If a 'noflush' suspend is in progress, we can requeue
765 * the I/O's to the core. This give userspace a chance
766 * to reconfigure the mirror, at which point the core
767 * will reissue the writes. If the 'noflush' flag is
768 * not set, we have no choice but to return errors.
770 * Some writes on the failures list may have been
771 * submitted before the log failure and represent a
772 * failure to write to one of the devices. It is ok
773 * for us to treat them the same and requeue them
777 while ((bio = bio_list_pop(failures))) {
782 dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
787 static void trigger_event(struct work_struct *work)
789 struct mirror_set *ms =
790 container_of(work, struct mirror_set, trigger_event);
792 dm_table_event(ms->ti->table);
795 /*-----------------------------------------------------------------
797 *---------------------------------------------------------------*/
798 static void do_mirror(struct work_struct *work)
800 struct mirror_set *ms = container_of(work, struct mirror_set,
802 struct bio_list reads, writes, failures;
805 spin_lock_irqsave(&ms->lock, flags);
808 failures = ms->failures;
809 bio_list_init(&ms->reads);
810 bio_list_init(&ms->writes);
811 bio_list_init(&ms->failures);
812 spin_unlock_irqrestore(&ms->lock, flags);
814 dm_rh_update_states(ms->rh, errors_handled(ms));
816 do_reads(ms, &reads);
817 do_writes(ms, &writes);
818 do_failures(ms, &failures);
820 dm_table_unplug_all(ms->ti->table);
823 /*-----------------------------------------------------------------
825 *---------------------------------------------------------------*/
826 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
827 uint32_t region_size,
828 struct dm_target *ti,
829 struct dm_dirty_log *dl)
832 struct mirror_set *ms = NULL;
834 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
836 ms = kzalloc(len, GFP_KERNEL);
838 ti->error = "Cannot allocate mirror context";
842 spin_lock_init(&ms->lock);
845 ms->nr_mirrors = nr_mirrors;
846 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
849 atomic_set(&ms->suspend, 0);
850 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
852 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
853 _dm_raid1_read_record_cache);
855 if (!ms->read_record_pool) {
856 ti->error = "Error creating mirror read_record_pool";
861 ms->io_client = dm_io_client_create(DM_IO_PAGES);
862 if (IS_ERR(ms->io_client)) {
863 ti->error = "Error creating dm_io client";
864 mempool_destroy(ms->read_record_pool);
869 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
870 wakeup_all_recovery_waiters,
871 ms->ti->begin, MAX_RECOVERY,
872 dl, region_size, ms->nr_regions);
873 if (IS_ERR(ms->rh)) {
874 ti->error = "Error creating dirty region hash";
875 dm_io_client_destroy(ms->io_client);
876 mempool_destroy(ms->read_record_pool);
884 static void free_context(struct mirror_set *ms, struct dm_target *ti,
888 dm_put_device(ti, ms->mirror[m].dev);
890 dm_io_client_destroy(ms->io_client);
891 dm_region_hash_destroy(ms->rh);
892 mempool_destroy(ms->read_record_pool);
896 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
897 unsigned int mirror, char **argv)
899 unsigned long long offset;
901 if (sscanf(argv[1], "%llu", &offset) != 1) {
902 ti->error = "Invalid offset";
906 if (dm_get_device(ti, argv[0], offset, ti->len,
907 dm_table_get_mode(ti->table),
908 &ms->mirror[mirror].dev)) {
909 ti->error = "Device lookup failure";
913 ms->mirror[mirror].ms = ms;
914 atomic_set(&(ms->mirror[mirror].error_count), 0);
915 ms->mirror[mirror].error_type = 0;
916 ms->mirror[mirror].offset = offset;
922 * Create dirty log: log_type #log_params <log_params>
924 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
925 unsigned argc, char **argv,
928 unsigned param_count;
929 struct dm_dirty_log *dl;
932 ti->error = "Insufficient mirror log arguments";
936 if (sscanf(argv[1], "%u", ¶m_count) != 1) {
937 ti->error = "Invalid mirror log argument count";
941 *args_used = 2 + param_count;
943 if (argc < *args_used) {
944 ti->error = "Insufficient mirror log arguments";
948 dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
951 ti->error = "Error creating mirror dirty log";
958 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
961 unsigned num_features;
962 struct dm_target *ti = ms->ti;
969 if (sscanf(argv[0], "%u", &num_features) != 1) {
970 ti->error = "Invalid number of features";
978 if (num_features > argc) {
979 ti->error = "Not enough arguments to support feature count";
983 if (!strcmp("handle_errors", argv[0]))
984 ms->features |= DM_RAID1_HANDLE_ERRORS;
986 ti->error = "Unrecognised feature requested";
996 * Construct a mirror mapping:
998 * log_type #log_params <log_params>
999 * #mirrors [mirror_path offset]{2,}
1000 * [#features <features>]
1002 * log_type is "core" or "disk"
1003 * #log_params is between 1 and 3
1005 * If present, features must be "handle_errors".
1007 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1010 unsigned int nr_mirrors, m, args_used;
1011 struct mirror_set *ms;
1012 struct dm_dirty_log *dl;
1014 dl = create_dirty_log(ti, argc, argv, &args_used);
1021 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1022 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
1023 ti->error = "Invalid number of mirrors";
1024 dm_dirty_log_destroy(dl);
1030 if (argc < nr_mirrors * 2) {
1031 ti->error = "Too few mirror arguments";
1032 dm_dirty_log_destroy(dl);
1036 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1038 dm_dirty_log_destroy(dl);
1042 /* Get the mirror parameter sets */
1043 for (m = 0; m < nr_mirrors; m++) {
1044 r = get_mirror(ms, ti, m, argv);
1046 free_context(ms, ti, m);
1054 ti->split_io = dm_rh_get_region_size(ms->rh);
1055 ti->num_flush_requests = 1;
1057 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1058 if (!ms->kmirrord_wq) {
1059 DMERR("couldn't start kmirrord");
1061 goto err_free_context;
1063 INIT_WORK(&ms->kmirrord_work, do_mirror);
1064 init_timer(&ms->timer);
1065 ms->timer_pending = 0;
1066 INIT_WORK(&ms->trigger_event, trigger_event);
1068 r = parse_features(ms, argc, argv, &args_used);
1070 goto err_destroy_wq;
1076 * Any read-balancing addition depends on the
1077 * DM_RAID1_HANDLE_ERRORS flag being present.
1078 * This is because the decision to balance depends
1079 * on the sync state of a region. If the above
1080 * flag is not present, we ignore errors; and
1081 * the sync state may be inaccurate.
1085 ti->error = "Too many mirror arguments";
1087 goto err_destroy_wq;
1090 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
1092 goto err_destroy_wq;
1098 destroy_workqueue(ms->kmirrord_wq);
1100 free_context(ms, ti, ms->nr_mirrors);
1104 static void mirror_dtr(struct dm_target *ti)
1106 struct mirror_set *ms = (struct mirror_set *) ti->private;
1108 del_timer_sync(&ms->timer);
1109 flush_workqueue(ms->kmirrord_wq);
1110 flush_scheduled_work();
1111 dm_kcopyd_client_destroy(ms->kcopyd_client);
1112 destroy_workqueue(ms->kmirrord_wq);
1113 free_context(ms, ti, ms->nr_mirrors);
1117 * Mirror mapping function
1119 static int mirror_map(struct dm_target *ti, struct bio *bio,
1120 union map_info *map_context)
1122 int r, rw = bio_rw(bio);
1124 struct mirror_set *ms = ti->private;
1125 struct dm_raid1_read_record *read_record = NULL;
1126 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1129 /* Save region for mirror_end_io() handler */
1130 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
1131 queue_bio(ms, bio, rw);
1132 return DM_MAPIO_SUBMITTED;
1135 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1136 if (r < 0 && r != -EWOULDBLOCK)
1140 * If region is not in-sync queue the bio.
1142 if (!r || (r == -EWOULDBLOCK)) {
1144 return -EWOULDBLOCK;
1146 queue_bio(ms, bio, rw);
1147 return DM_MAPIO_SUBMITTED;
1151 * The region is in-sync and we can perform reads directly.
1152 * Store enough information so we can retry if it fails.
1154 m = choose_mirror(ms, bio->bi_sector);
1158 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1159 if (likely(read_record)) {
1160 dm_bio_record(&read_record->details, bio);
1161 map_context->ptr = read_record;
1167 return DM_MAPIO_REMAPPED;
1170 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1171 int error, union map_info *map_context)
1173 int rw = bio_rw(bio);
1174 struct mirror_set *ms = (struct mirror_set *) ti->private;
1175 struct mirror *m = NULL;
1176 struct dm_bio_details *bd = NULL;
1177 struct dm_raid1_read_record *read_record = map_context->ptr;
1180 * We need to dec pending if this was a write.
1183 if (likely(!bio_empty_barrier(bio)))
1184 dm_rh_dec(ms->rh, map_context->ll);
1188 if (error == -EOPNOTSUPP)
1191 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
1194 if (unlikely(error)) {
1197 * There wasn't enough memory to record necessary
1198 * information for a retry or there was no other
1201 DMERR_LIMIT("Mirror read failed.");
1207 DMERR("Mirror read failed from %s. Trying alternative device.",
1210 fail_mirror(m, DM_RAID1_READ_ERROR);
1213 * A failed read is requeued for another attempt using an intact
1216 if (default_ok(m) || mirror_available(ms, bio)) {
1217 bd = &read_record->details;
1219 dm_bio_restore(bd, bio);
1220 mempool_free(read_record, ms->read_record_pool);
1221 map_context->ptr = NULL;
1222 queue_bio(ms, bio, rw);
1225 DMERR("All replicated volumes dead, failing I/O");
1230 mempool_free(read_record, ms->read_record_pool);
1231 map_context->ptr = NULL;
1237 static void mirror_presuspend(struct dm_target *ti)
1239 struct mirror_set *ms = (struct mirror_set *) ti->private;
1240 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1242 struct bio_list holds;
1245 atomic_set(&ms->suspend, 1);
1248 * We must finish up all the work that we've
1249 * generated (i.e. recovery work).
1251 dm_rh_stop_recovery(ms->rh);
1253 wait_event(_kmirrord_recovery_stopped,
1254 !dm_rh_recovery_in_flight(ms->rh));
1256 if (log->type->presuspend && log->type->presuspend(log))
1257 /* FIXME: need better error handling */
1258 DMWARN("log presuspend failed");
1261 * Now that recovery is complete/stopped and the
1262 * delayed bios are queued, we need to wait for
1263 * the worker thread to complete. This way,
1264 * we know that all of our I/O has been pushed.
1266 flush_workqueue(ms->kmirrord_wq);
1269 * Now set ms->suspend is set and the workqueue flushed, no more
1270 * entries can be added to ms->hold list, so process it.
1272 * Bios can still arrive concurrently with or after this
1273 * presuspend function, but they cannot join the hold list
1274 * because ms->suspend is set.
1276 spin_lock_irq(&ms->lock);
1278 bio_list_init(&ms->holds);
1279 spin_unlock_irq(&ms->lock);
1281 while ((bio = bio_list_pop(&holds)))
1285 static void mirror_postsuspend(struct dm_target *ti)
1287 struct mirror_set *ms = ti->private;
1288 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1290 if (log->type->postsuspend && log->type->postsuspend(log))
1291 /* FIXME: need better error handling */
1292 DMWARN("log postsuspend failed");
1295 static void mirror_resume(struct dm_target *ti)
1297 struct mirror_set *ms = ti->private;
1298 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1300 atomic_set(&ms->suspend, 0);
1301 if (log->type->resume && log->type->resume(log))
1302 /* FIXME: need better error handling */
1303 DMWARN("log resume failed");
1304 dm_rh_start_recovery(ms->rh);
1308 * device_status_char
1309 * @m: mirror device/leg we want the status of
1311 * We return one character representing the most severe error
1312 * we have encountered.
1313 * A => Alive - No failures
1314 * D => Dead - A write failure occurred leaving mirror out-of-sync
1315 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1316 * R => Read - A read failure occurred, mirror data unaffected
1320 static char device_status_char(struct mirror *m)
1322 if (!atomic_read(&(m->error_count)))
1325 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1326 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1327 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1328 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1332 static int mirror_status(struct dm_target *ti, status_type_t type,
1333 char *result, unsigned int maxlen)
1335 unsigned int m, sz = 0;
1336 struct mirror_set *ms = (struct mirror_set *) ti->private;
1337 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1338 char buffer[ms->nr_mirrors + 1];
1341 case STATUSTYPE_INFO:
1342 DMEMIT("%d ", ms->nr_mirrors);
1343 for (m = 0; m < ms->nr_mirrors; m++) {
1344 DMEMIT("%s ", ms->mirror[m].dev->name);
1345 buffer[m] = device_status_char(&(ms->mirror[m]));
1349 DMEMIT("%llu/%llu 1 %s ",
1350 (unsigned long long)log->type->get_sync_count(log),
1351 (unsigned long long)ms->nr_regions, buffer);
1353 sz += log->type->status(log, type, result+sz, maxlen-sz);
1357 case STATUSTYPE_TABLE:
1358 sz = log->type->status(log, type, result, maxlen);
1360 DMEMIT("%d", ms->nr_mirrors);
1361 for (m = 0; m < ms->nr_mirrors; m++)
1362 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1363 (unsigned long long)ms->mirror[m].offset);
1365 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1366 DMEMIT(" 1 handle_errors");
1372 static int mirror_iterate_devices(struct dm_target *ti,
1373 iterate_devices_callout_fn fn, void *data)
1375 struct mirror_set *ms = ti->private;
1379 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1380 ret = fn(ti, ms->mirror[i].dev,
1381 ms->mirror[i].offset, ti->len, data);
1386 static struct target_type mirror_target = {
1388 .version = {1, 12, 0},
1389 .module = THIS_MODULE,
1393 .end_io = mirror_end_io,
1394 .presuspend = mirror_presuspend,
1395 .postsuspend = mirror_postsuspend,
1396 .resume = mirror_resume,
1397 .status = mirror_status,
1398 .iterate_devices = mirror_iterate_devices,
1401 static int __init dm_mirror_init(void)
1405 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1406 if (!_dm_raid1_read_record_cache) {
1407 DMERR("Can't allocate dm_raid1_read_record cache");
1412 r = dm_register_target(&mirror_target);
1414 DMERR("Failed to register mirror target");
1421 kmem_cache_destroy(_dm_raid1_read_record_cache);
1426 static void __exit dm_mirror_exit(void)
1428 dm_unregister_target(&mirror_target);
1429 kmem_cache_destroy(_dm_raid1_read_record_cache);
1433 module_init(dm_mirror_init);
1434 module_exit(dm_mirror_exit);
1436 MODULE_DESCRIPTION(DM_NAME " mirror target");
1437 MODULE_AUTHOR("Joe Thornber");
1438 MODULE_LICENSE("GPL");