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 *---------------------------------------------------------------*/
43 struct mirror_set *ms;
45 unsigned long error_type;
52 struct list_head list;
56 spinlock_t lock; /* protects the lists */
57 struct bio_list reads;
58 struct bio_list writes;
59 struct bio_list failures;
61 struct dm_region_hash *rh;
62 struct dm_kcopyd_client *kcopyd_client;
63 struct dm_io_client *io_client;
64 mempool_t *read_record_pool;
72 atomic_t default_mirror; /* Default mirror */
74 struct workqueue_struct *kmirrord_wq;
75 struct work_struct kmirrord_work;
76 struct timer_list timer;
77 unsigned long timer_pending;
79 struct work_struct trigger_event;
82 struct mirror mirror[0];
85 static void wakeup_mirrord(void *context)
87 struct mirror_set *ms = context;
89 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
92 static void delayed_wake_fn(unsigned long data)
94 struct mirror_set *ms = (struct mirror_set *) data;
96 clear_bit(0, &ms->timer_pending);
100 static void delayed_wake(struct mirror_set *ms)
102 if (test_and_set_bit(0, &ms->timer_pending))
105 ms->timer.expires = jiffies + HZ / 5;
106 ms->timer.data = (unsigned long) ms;
107 ms->timer.function = delayed_wake_fn;
108 add_timer(&ms->timer);
111 static void wakeup_all_recovery_waiters(void *context)
113 wake_up_all(&_kmirrord_recovery_stopped);
116 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
122 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
123 spin_lock_irqsave(&ms->lock, flags);
124 should_wake = !(bl->head);
125 bio_list_add(bl, bio);
126 spin_unlock_irqrestore(&ms->lock, flags);
132 static void dispatch_bios(void *context, struct bio_list *bio_list)
134 struct mirror_set *ms = context;
137 while ((bio = bio_list_pop(bio_list)))
138 queue_bio(ms, bio, WRITE);
141 #define MIN_READ_RECORDS 20
142 struct dm_raid1_read_record {
144 struct dm_bio_details details;
147 static struct kmem_cache *_dm_raid1_read_record_cache;
150 * Every mirror should look like this one.
152 #define DEFAULT_MIRROR 0
155 * This is yucky. We squirrel the mirror struct away inside
156 * bi_next for read/write buffers. This is safe since the bh
157 * doesn't get submitted to the lower levels of block layer.
159 static struct mirror *bio_get_m(struct bio *bio)
161 return (struct mirror *) bio->bi_next;
164 static void bio_set_m(struct bio *bio, struct mirror *m)
166 bio->bi_next = (struct bio *) m;
169 static struct mirror *get_default_mirror(struct mirror_set *ms)
171 return &ms->mirror[atomic_read(&ms->default_mirror)];
174 static void set_default_mirror(struct mirror *m)
176 struct mirror_set *ms = m->ms;
177 struct mirror *m0 = &(ms->mirror[0]);
179 atomic_set(&ms->default_mirror, m - m0);
183 * @m: mirror device to fail
184 * @error_type: one of the enum's, DM_RAID1_*_ERROR
186 * If errors are being handled, record the type of
187 * error encountered for this device. If this type
188 * of error has already been recorded, we can return;
189 * otherwise, we must signal userspace by triggering
190 * an event. Additionally, if the device is the
191 * primary device, we must choose a new primary, but
192 * only if the mirror is in-sync.
194 * This function must not block.
196 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
198 struct mirror_set *ms = m->ms;
202 * error_count is used for nothing more than a
203 * simple way to tell if a device has encountered
206 atomic_inc(&m->error_count);
208 if (test_and_set_bit(error_type, &m->error_type))
211 if (!errors_handled(ms))
214 if (m != get_default_mirror(ms))
219 * Better to issue requests to same failing device
220 * than to risk returning corrupt data.
222 DMERR("Primary mirror (%s) failed while out-of-sync: "
223 "Reads may fail.", m->dev->name);
227 for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
228 if (!atomic_read(&new->error_count)) {
229 set_default_mirror(new);
233 if (unlikely(new == ms->mirror + ms->nr_mirrors))
234 DMWARN("All sides of mirror have failed.");
237 schedule_work(&ms->trigger_event);
240 /*-----------------------------------------------------------------
243 * When a mirror is first activated we may find that some regions
244 * are in the no-sync state. We have to recover these by
245 * recopying from the default mirror to all the others.
246 *---------------------------------------------------------------*/
247 static void recovery_complete(int read_err, unsigned long write_err,
250 struct dm_region *reg = context;
251 struct mirror_set *ms = dm_rh_region_context(reg);
255 /* Read error means the failure of default mirror. */
256 DMERR_LIMIT("Unable to read primary mirror during recovery");
257 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
261 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
264 * Bits correspond to devices (excluding default mirror).
265 * The default mirror cannot change during recovery.
267 for (m = 0; m < ms->nr_mirrors; m++) {
268 if (&ms->mirror[m] == get_default_mirror(ms))
270 if (test_bit(bit, &write_err))
271 fail_mirror(ms->mirror + m,
272 DM_RAID1_SYNC_ERROR);
277 dm_rh_recovery_end(reg, !(read_err || write_err));
280 static int recover(struct mirror_set *ms, struct dm_region *reg)
284 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
286 unsigned long flags = 0;
287 region_t key = dm_rh_get_region_key(reg);
288 sector_t region_size = dm_rh_get_region_size(ms->rh);
290 /* fill in the source */
291 m = get_default_mirror(ms);
292 from.bdev = m->dev->bdev;
293 from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
294 if (key == (ms->nr_regions - 1)) {
296 * The final region may be smaller than
299 from.count = ms->ti->len & (region_size - 1);
301 from.count = region_size;
303 from.count = region_size;
305 /* fill in the destinations */
306 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
307 if (&ms->mirror[i] == get_default_mirror(ms))
311 dest->bdev = m->dev->bdev;
312 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
313 dest->count = from.count;
318 if (!errors_handled(ms))
319 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
321 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
322 flags, recovery_complete, reg);
327 static void do_recovery(struct mirror_set *ms)
329 struct dm_region *reg;
330 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
334 * Start quiescing some regions.
336 dm_rh_recovery_prepare(ms->rh);
339 * Copy any already quiesced regions.
341 while ((reg = dm_rh_recovery_start(ms->rh))) {
342 r = recover(ms, reg);
344 dm_rh_recovery_end(reg, 0);
348 * Update the in sync flag.
351 (log->type->get_sync_count(log) == ms->nr_regions)) {
352 /* the sync is complete */
353 dm_table_event(ms->ti->table);
358 /*-----------------------------------------------------------------
360 *---------------------------------------------------------------*/
361 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
363 struct mirror *m = get_default_mirror(ms);
366 if (likely(!atomic_read(&m->error_count)))
369 if (m-- == ms->mirror)
371 } while (m != get_default_mirror(ms));
376 static int default_ok(struct mirror *m)
378 struct mirror *default_mirror = get_default_mirror(m->ms);
380 return !atomic_read(&default_mirror->error_count);
383 static int mirror_available(struct mirror_set *ms, struct bio *bio)
385 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
386 region_t region = dm_rh_bio_to_region(ms->rh, bio);
388 if (log->type->in_sync(log, region, 0))
389 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
395 * remap a buffer to a particular mirror.
397 static sector_t map_sector(struct mirror *m, struct bio *bio)
399 if (unlikely(!bio->bi_size))
401 return m->offset + (bio->bi_sector - m->ms->ti->begin);
404 static void map_bio(struct mirror *m, struct bio *bio)
406 bio->bi_bdev = m->dev->bdev;
407 bio->bi_sector = map_sector(m, bio);
410 static void map_region(struct dm_io_region *io, struct mirror *m,
413 io->bdev = m->dev->bdev;
414 io->sector = map_sector(m, bio);
415 io->count = bio->bi_size >> 9;
418 /*-----------------------------------------------------------------
420 *---------------------------------------------------------------*/
421 static void read_callback(unsigned long error, void *context)
423 struct bio *bio = context;
427 bio_set_m(bio, NULL);
429 if (likely(!error)) {
434 fail_mirror(m, DM_RAID1_READ_ERROR);
436 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
437 DMWARN_LIMIT("Read failure on mirror device %s. "
438 "Trying alternative device.",
440 queue_bio(m->ms, bio, bio_rw(bio));
444 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
446 bio_endio(bio, -EIO);
449 /* Asynchronous read. */
450 static void read_async_bio(struct mirror *m, struct bio *bio)
452 struct dm_io_region io;
453 struct dm_io_request io_req = {
455 .mem.type = DM_IO_BVEC,
456 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
457 .notify.fn = read_callback,
458 .notify.context = bio,
459 .client = m->ms->io_client,
462 map_region(&io, m, bio);
464 BUG_ON(dm_io(&io_req, 1, &io, NULL));
467 static inline int region_in_sync(struct mirror_set *ms, region_t region,
470 int state = dm_rh_get_state(ms->rh, region, may_block);
471 return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
474 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
480 while ((bio = bio_list_pop(reads))) {
481 region = dm_rh_bio_to_region(ms->rh, bio);
482 m = get_default_mirror(ms);
485 * We can only read balance if the region is in sync.
487 if (likely(region_in_sync(ms, region, 1)))
488 m = choose_mirror(ms, bio->bi_sector);
489 else if (m && atomic_read(&m->error_count))
493 read_async_bio(m, bio);
495 bio_endio(bio, -EIO);
499 /*-----------------------------------------------------------------
502 * We do different things with the write io depending on the
503 * state of the region that it's in:
505 * SYNC: increment pending, use kcopyd to write to *all* mirrors
506 * RECOVERING: delay the io until recovery completes
507 * NOSYNC: increment pending, just write to the default mirror
508 *---------------------------------------------------------------*/
511 static void write_callback(unsigned long error, void *context)
514 struct bio *bio = (struct bio *) context;
515 struct mirror_set *ms;
520 ms = bio_get_m(bio)->ms;
521 bio_set_m(bio, NULL);
524 * NOTE: We don't decrement the pending count here,
525 * instead it is done by the targets endio function.
526 * This way we handle both writes to SYNC and NOSYNC
527 * regions with the same code.
532 for (i = 0; i < ms->nr_mirrors; i++)
533 if (test_bit(i, &error))
534 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
538 if (unlikely(!uptodate)) {
539 DMERR("All replicated volumes dead, failing I/O");
540 /* None of the writes succeeded, fail the I/O. */
542 } else if (errors_handled(ms)) {
544 * Need to raise event. Since raising
545 * events can block, we need to do it in
548 spin_lock_irqsave(&ms->lock, flags);
549 if (!ms->failures.head)
551 bio_list_add(&ms->failures, bio);
552 spin_unlock_irqrestore(&ms->lock, flags);
561 static void do_write(struct mirror_set *ms, struct bio *bio)
564 struct dm_io_region io[ms->nr_mirrors], *dest = io;
566 struct dm_io_request io_req = {
567 .bi_rw = WRITE | (bio->bi_rw & WRITE_BARRIER),
568 .mem.type = DM_IO_BVEC,
569 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
570 .notify.fn = write_callback,
571 .notify.context = bio,
572 .client = ms->io_client,
575 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
576 map_region(dest++, m, bio);
579 * Use default mirror because we only need it to retrieve the reference
580 * to the mirror set in write_callback().
582 bio_set_m(bio, get_default_mirror(ms));
584 BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
587 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
591 struct bio_list sync, nosync, recover, *this_list = NULL;
592 struct bio_list requeue;
593 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
600 * Classify each write.
602 bio_list_init(&sync);
603 bio_list_init(&nosync);
604 bio_list_init(&recover);
605 bio_list_init(&requeue);
607 while ((bio = bio_list_pop(writes))) {
608 if (unlikely(bio_empty_barrier(bio))) {
609 bio_list_add(&sync, bio);
613 region = dm_rh_bio_to_region(ms->rh, bio);
615 if (log->type->is_remote_recovering &&
616 log->type->is_remote_recovering(log, region)) {
617 bio_list_add(&requeue, bio);
621 state = dm_rh_get_state(ms->rh, region, 1);
632 case DM_RH_RECOVERING:
633 this_list = &recover;
637 bio_list_add(this_list, bio);
641 * Add bios that are delayed due to remote recovery
642 * back on to the write queue
644 if (unlikely(requeue.head)) {
645 spin_lock_irq(&ms->lock);
646 bio_list_merge(&ms->writes, &requeue);
647 spin_unlock_irq(&ms->lock);
652 * Increment the pending counts for any regions that will
653 * be written to (writes to recover regions are going to
656 dm_rh_inc_pending(ms->rh, &sync);
657 dm_rh_inc_pending(ms->rh, &nosync);
660 * If the flush fails on a previous call and succeeds here,
661 * we must not reset the log_failure variable. We need
662 * userspace interaction to do that.
664 ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
669 if (unlikely(ms->log_failure)) {
670 spin_lock_irq(&ms->lock);
671 bio_list_merge(&ms->failures, &sync);
672 spin_unlock_irq(&ms->lock);
675 while ((bio = bio_list_pop(&sync)))
678 while ((bio = bio_list_pop(&recover)))
679 dm_rh_delay(ms->rh, bio);
681 while ((bio = bio_list_pop(&nosync))) {
682 map_bio(get_default_mirror(ms), bio);
683 generic_make_request(bio);
687 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
694 if (!ms->log_failure) {
695 while ((bio = bio_list_pop(failures))) {
697 dm_rh_mark_nosync(ms->rh, bio, bio->bi_size, 0);
703 * If the log has failed, unattempted writes are being
704 * put on the failures list. We can't issue those writes
705 * until a log has been marked, so we must store them.
707 * If a 'noflush' suspend is in progress, we can requeue
708 * the I/O's to the core. This give userspace a chance
709 * to reconfigure the mirror, at which point the core
710 * will reissue the writes. If the 'noflush' flag is
711 * not set, we have no choice but to return errors.
713 * Some writes on the failures list may have been
714 * submitted before the log failure and represent a
715 * failure to write to one of the devices. It is ok
716 * for us to treat them the same and requeue them
719 if (dm_noflush_suspending(ms->ti)) {
720 while ((bio = bio_list_pop(failures)))
721 bio_endio(bio, DM_ENDIO_REQUEUE);
725 if (atomic_read(&ms->suspend)) {
726 while ((bio = bio_list_pop(failures)))
727 bio_endio(bio, -EIO);
731 spin_lock_irq(&ms->lock);
732 bio_list_merge(&ms->failures, failures);
733 spin_unlock_irq(&ms->lock);
738 static void trigger_event(struct work_struct *work)
740 struct mirror_set *ms =
741 container_of(work, struct mirror_set, trigger_event);
743 dm_table_event(ms->ti->table);
746 /*-----------------------------------------------------------------
748 *---------------------------------------------------------------*/
749 static void do_mirror(struct work_struct *work)
751 struct mirror_set *ms = container_of(work, struct mirror_set,
753 struct bio_list reads, writes, failures;
756 spin_lock_irqsave(&ms->lock, flags);
759 failures = ms->failures;
760 bio_list_init(&ms->reads);
761 bio_list_init(&ms->writes);
762 bio_list_init(&ms->failures);
763 spin_unlock_irqrestore(&ms->lock, flags);
765 dm_rh_update_states(ms->rh, errors_handled(ms));
767 do_reads(ms, &reads);
768 do_writes(ms, &writes);
769 do_failures(ms, &failures);
771 dm_table_unplug_all(ms->ti->table);
774 /*-----------------------------------------------------------------
776 *---------------------------------------------------------------*/
777 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
778 uint32_t region_size,
779 struct dm_target *ti,
780 struct dm_dirty_log *dl)
783 struct mirror_set *ms = NULL;
785 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
787 ms = kzalloc(len, GFP_KERNEL);
789 ti->error = "Cannot allocate mirror context";
793 spin_lock_init(&ms->lock);
796 ms->nr_mirrors = nr_mirrors;
797 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
800 atomic_set(&ms->suspend, 0);
801 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
803 ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
804 _dm_raid1_read_record_cache);
806 if (!ms->read_record_pool) {
807 ti->error = "Error creating mirror read_record_pool";
812 ms->io_client = dm_io_client_create(DM_IO_PAGES);
813 if (IS_ERR(ms->io_client)) {
814 ti->error = "Error creating dm_io client";
815 mempool_destroy(ms->read_record_pool);
820 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
821 wakeup_all_recovery_waiters,
822 ms->ti->begin, MAX_RECOVERY,
823 dl, region_size, ms->nr_regions);
824 if (IS_ERR(ms->rh)) {
825 ti->error = "Error creating dirty region hash";
826 dm_io_client_destroy(ms->io_client);
827 mempool_destroy(ms->read_record_pool);
835 static void free_context(struct mirror_set *ms, struct dm_target *ti,
839 dm_put_device(ti, ms->mirror[m].dev);
841 dm_io_client_destroy(ms->io_client);
842 dm_region_hash_destroy(ms->rh);
843 mempool_destroy(ms->read_record_pool);
847 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
848 unsigned int mirror, char **argv)
850 unsigned long long offset;
852 if (sscanf(argv[1], "%llu", &offset) != 1) {
853 ti->error = "Invalid offset";
857 if (dm_get_device(ti, argv[0], offset, ti->len,
858 dm_table_get_mode(ti->table),
859 &ms->mirror[mirror].dev)) {
860 ti->error = "Device lookup failure";
864 ms->mirror[mirror].ms = ms;
865 atomic_set(&(ms->mirror[mirror].error_count), 0);
866 ms->mirror[mirror].error_type = 0;
867 ms->mirror[mirror].offset = offset;
873 * Create dirty log: log_type #log_params <log_params>
875 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
876 unsigned argc, char **argv,
879 unsigned param_count;
880 struct dm_dirty_log *dl;
883 ti->error = "Insufficient mirror log arguments";
887 if (sscanf(argv[1], "%u", ¶m_count) != 1) {
888 ti->error = "Invalid mirror log argument count";
892 *args_used = 2 + param_count;
894 if (argc < *args_used) {
895 ti->error = "Insufficient mirror log arguments";
899 dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
901 ti->error = "Error creating mirror dirty log";
908 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
911 unsigned num_features;
912 struct dm_target *ti = ms->ti;
919 if (sscanf(argv[0], "%u", &num_features) != 1) {
920 ti->error = "Invalid number of features";
928 if (num_features > argc) {
929 ti->error = "Not enough arguments to support feature count";
933 if (!strcmp("handle_errors", argv[0]))
934 ms->features |= DM_RAID1_HANDLE_ERRORS;
936 ti->error = "Unrecognised feature requested";
946 * Construct a mirror mapping:
948 * log_type #log_params <log_params>
949 * #mirrors [mirror_path offset]{2,}
950 * [#features <features>]
952 * log_type is "core" or "disk"
953 * #log_params is between 1 and 3
955 * If present, features must be "handle_errors".
957 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
960 unsigned int nr_mirrors, m, args_used;
961 struct mirror_set *ms;
962 struct dm_dirty_log *dl;
964 dl = create_dirty_log(ti, argc, argv, &args_used);
971 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
972 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
973 ti->error = "Invalid number of mirrors";
974 dm_dirty_log_destroy(dl);
980 if (argc < nr_mirrors * 2) {
981 ti->error = "Too few mirror arguments";
982 dm_dirty_log_destroy(dl);
986 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
988 dm_dirty_log_destroy(dl);
992 /* Get the mirror parameter sets */
993 for (m = 0; m < nr_mirrors; m++) {
994 r = get_mirror(ms, ti, m, argv);
996 free_context(ms, ti, m);
1004 ti->split_io = dm_rh_get_region_size(ms->rh);
1005 ti->num_flush_requests = 1;
1007 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1008 if (!ms->kmirrord_wq) {
1009 DMERR("couldn't start kmirrord");
1011 goto err_free_context;
1013 INIT_WORK(&ms->kmirrord_work, do_mirror);
1014 init_timer(&ms->timer);
1015 ms->timer_pending = 0;
1016 INIT_WORK(&ms->trigger_event, trigger_event);
1018 r = parse_features(ms, argc, argv, &args_used);
1020 goto err_destroy_wq;
1026 * Any read-balancing addition depends on the
1027 * DM_RAID1_HANDLE_ERRORS flag being present.
1028 * This is because the decision to balance depends
1029 * on the sync state of a region. If the above
1030 * flag is not present, we ignore errors; and
1031 * the sync state may be inaccurate.
1035 ti->error = "Too many mirror arguments";
1037 goto err_destroy_wq;
1040 r = dm_kcopyd_client_create(DM_KCOPYD_PAGES, &ms->kcopyd_client);
1042 goto err_destroy_wq;
1048 destroy_workqueue(ms->kmirrord_wq);
1050 free_context(ms, ti, ms->nr_mirrors);
1054 static void mirror_dtr(struct dm_target *ti)
1056 struct mirror_set *ms = (struct mirror_set *) ti->private;
1058 del_timer_sync(&ms->timer);
1059 flush_workqueue(ms->kmirrord_wq);
1060 flush_scheduled_work();
1061 dm_kcopyd_client_destroy(ms->kcopyd_client);
1062 destroy_workqueue(ms->kmirrord_wq);
1063 free_context(ms, ti, ms->nr_mirrors);
1067 * Mirror mapping function
1069 static int mirror_map(struct dm_target *ti, struct bio *bio,
1070 union map_info *map_context)
1072 int r, rw = bio_rw(bio);
1074 struct mirror_set *ms = ti->private;
1075 struct dm_raid1_read_record *read_record = NULL;
1076 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1079 /* Save region for mirror_end_io() handler */
1080 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
1081 queue_bio(ms, bio, rw);
1082 return DM_MAPIO_SUBMITTED;
1085 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1086 if (r < 0 && r != -EWOULDBLOCK)
1090 * If region is not in-sync queue the bio.
1092 if (!r || (r == -EWOULDBLOCK)) {
1094 return -EWOULDBLOCK;
1096 queue_bio(ms, bio, rw);
1097 return DM_MAPIO_SUBMITTED;
1101 * The region is in-sync and we can perform reads directly.
1102 * Store enough information so we can retry if it fails.
1104 m = choose_mirror(ms, bio->bi_sector);
1108 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1109 if (likely(read_record)) {
1110 dm_bio_record(&read_record->details, bio);
1111 map_context->ptr = read_record;
1117 return DM_MAPIO_REMAPPED;
1120 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1121 int error, union map_info *map_context)
1123 int rw = bio_rw(bio);
1124 struct mirror_set *ms = (struct mirror_set *) ti->private;
1125 struct mirror *m = NULL;
1126 struct dm_bio_details *bd = NULL;
1127 struct dm_raid1_read_record *read_record = map_context->ptr;
1130 * We need to dec pending if this was a write.
1133 if (likely(!bio_empty_barrier(bio)))
1134 dm_rh_dec(ms->rh, map_context->ll);
1138 if (error == -EOPNOTSUPP)
1141 if ((error == -EWOULDBLOCK) && bio_rw_flagged(bio, BIO_RW_AHEAD))
1144 if (unlikely(error)) {
1147 * There wasn't enough memory to record necessary
1148 * information for a retry or there was no other
1151 DMERR_LIMIT("Mirror read failed.");
1157 DMERR("Mirror read failed from %s. Trying alternative device.",
1160 fail_mirror(m, DM_RAID1_READ_ERROR);
1163 * A failed read is requeued for another attempt using an intact
1166 if (default_ok(m) || mirror_available(ms, bio)) {
1167 bd = &read_record->details;
1169 dm_bio_restore(bd, bio);
1170 mempool_free(read_record, ms->read_record_pool);
1171 map_context->ptr = NULL;
1172 queue_bio(ms, bio, rw);
1175 DMERR("All replicated volumes dead, failing I/O");
1180 mempool_free(read_record, ms->read_record_pool);
1181 map_context->ptr = NULL;
1187 static void mirror_presuspend(struct dm_target *ti)
1189 struct mirror_set *ms = (struct mirror_set *) ti->private;
1190 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1192 atomic_set(&ms->suspend, 1);
1195 * We must finish up all the work that we've
1196 * generated (i.e. recovery work).
1198 dm_rh_stop_recovery(ms->rh);
1200 wait_event(_kmirrord_recovery_stopped,
1201 !dm_rh_recovery_in_flight(ms->rh));
1203 if (log->type->presuspend && log->type->presuspend(log))
1204 /* FIXME: need better error handling */
1205 DMWARN("log presuspend failed");
1208 * Now that recovery is complete/stopped and the
1209 * delayed bios are queued, we need to wait for
1210 * the worker thread to complete. This way,
1211 * we know that all of our I/O has been pushed.
1213 flush_workqueue(ms->kmirrord_wq);
1216 static void mirror_postsuspend(struct dm_target *ti)
1218 struct mirror_set *ms = ti->private;
1219 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1221 if (log->type->postsuspend && log->type->postsuspend(log))
1222 /* FIXME: need better error handling */
1223 DMWARN("log postsuspend failed");
1226 static void mirror_resume(struct dm_target *ti)
1228 struct mirror_set *ms = ti->private;
1229 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1231 atomic_set(&ms->suspend, 0);
1232 if (log->type->resume && log->type->resume(log))
1233 /* FIXME: need better error handling */
1234 DMWARN("log resume failed");
1235 dm_rh_start_recovery(ms->rh);
1239 * device_status_char
1240 * @m: mirror device/leg we want the status of
1242 * We return one character representing the most severe error
1243 * we have encountered.
1244 * A => Alive - No failures
1245 * D => Dead - A write failure occurred leaving mirror out-of-sync
1246 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1247 * R => Read - A read failure occurred, mirror data unaffected
1251 static char device_status_char(struct mirror *m)
1253 if (!atomic_read(&(m->error_count)))
1256 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1257 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1258 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1262 static int mirror_status(struct dm_target *ti, status_type_t type,
1263 char *result, unsigned int maxlen)
1265 unsigned int m, sz = 0;
1266 struct mirror_set *ms = (struct mirror_set *) ti->private;
1267 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1268 char buffer[ms->nr_mirrors + 1];
1271 case STATUSTYPE_INFO:
1272 DMEMIT("%d ", ms->nr_mirrors);
1273 for (m = 0; m < ms->nr_mirrors; m++) {
1274 DMEMIT("%s ", ms->mirror[m].dev->name);
1275 buffer[m] = device_status_char(&(ms->mirror[m]));
1279 DMEMIT("%llu/%llu 1 %s ",
1280 (unsigned long long)log->type->get_sync_count(log),
1281 (unsigned long long)ms->nr_regions, buffer);
1283 sz += log->type->status(log, type, result+sz, maxlen-sz);
1287 case STATUSTYPE_TABLE:
1288 sz = log->type->status(log, type, result, maxlen);
1290 DMEMIT("%d", ms->nr_mirrors);
1291 for (m = 0; m < ms->nr_mirrors; m++)
1292 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1293 (unsigned long long)ms->mirror[m].offset);
1295 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1296 DMEMIT(" 1 handle_errors");
1302 static int mirror_iterate_devices(struct dm_target *ti,
1303 iterate_devices_callout_fn fn, void *data)
1305 struct mirror_set *ms = ti->private;
1309 for (i = 0; !ret && i < ms->nr_mirrors; i++)
1310 ret = fn(ti, ms->mirror[i].dev,
1311 ms->mirror[i].offset, ti->len, data);
1316 static struct target_type mirror_target = {
1318 .version = {1, 12, 0},
1319 .module = THIS_MODULE,
1323 .end_io = mirror_end_io,
1324 .presuspend = mirror_presuspend,
1325 .postsuspend = mirror_postsuspend,
1326 .resume = mirror_resume,
1327 .status = mirror_status,
1328 .iterate_devices = mirror_iterate_devices,
1331 static int __init dm_mirror_init(void)
1335 _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1336 if (!_dm_raid1_read_record_cache) {
1337 DMERR("Can't allocate dm_raid1_read_record cache");
1342 r = dm_register_target(&mirror_target);
1344 DMERR("Failed to register mirror target");
1351 kmem_cache_destroy(_dm_raid1_read_record_cache);
1356 static void __exit dm_mirror_exit(void)
1358 dm_unregister_target(&mirror_target);
1359 kmem_cache_destroy(_dm_raid1_read_record_cache);
1363 module_init(dm_mirror_init);
1364 module_exit(dm_mirror_exit);
1366 MODULE_DESCRIPTION(DM_NAME " mirror target");
1367 MODULE_AUTHOR("Joe Thornber");
1368 MODULE_LICENSE("GPL");