2 * Copyright (C) 2003 Sistina Software Limited.
4 * This file is released under the GPL.
8 #include "dm-bio-list.h"
9 #include "dm-bio-record.h"
11 #include <linux/ctype.h>
12 #include <linux/init.h>
13 #include <linux/mempool.h>
14 #include <linux/module.h>
15 #include <linux/pagemap.h>
16 #include <linux/slab.h>
17 #include <linux/time.h>
18 #include <linux/vmalloc.h>
19 #include <linux/workqueue.h>
20 #include <linux/log2.h>
21 #include <linux/hardirq.h>
22 #include <linux/dm-io.h>
23 #include <linux/dm-dirty-log.h>
24 #include <linux/dm-kcopyd.h>
26 #define DM_MSG_PREFIX "raid1"
27 #define DM_IO_PAGES 64
29 #define DM_RAID1_HANDLE_ERRORS 0x01
30 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
32 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
34 /*-----------------------------------------------------------------
37 * The mirror splits itself up into discrete regions. Each
38 * region can be in one of three states: clean, dirty,
39 * nosync. There is no need to put clean regions in the hash.
41 * In addition to being present in the hash table a region _may_
42 * be present on one of three lists.
44 * clean_regions: Regions on this list have no io pending to
45 * them, they are in sync, we are no longer interested in them,
46 * they are dull. rh_update_states() will remove them from the
49 * quiesced_regions: These regions have been spun down, ready
50 * for recovery. rh_recovery_start() will remove regions from
51 * this list and hand them to kmirrord, which will schedule the
52 * recovery io with kcopyd.
54 * recovered_regions: Regions that kcopyd has successfully
55 * recovered. rh_update_states() will now schedule any delayed
56 * io, up the recovery_count, and remove the region from the
60 * A rw spin lock 'hash_lock' protects just the hash table,
61 * this is never held in write mode from interrupt context,
62 * which I believe means that we only have to disable irqs when
65 * An ordinary spin lock 'region_lock' that protects the three
66 * lists in the region_hash, with the 'state', 'list' and
67 * 'bhs_delayed' fields of the regions. This is used from irq
68 * context, so all other uses will have to suspend local irqs.
69 *---------------------------------------------------------------*/
72 struct mirror_set *ms;
74 unsigned region_shift;
76 /* holds persistent region state */
77 struct dm_dirty_log *log;
81 mempool_t *region_pool;
83 unsigned int nr_buckets;
84 struct list_head *buckets;
86 spinlock_t region_lock;
87 atomic_t recovery_in_flight;
88 struct semaphore recovery_count;
89 struct list_head clean_regions;
90 struct list_head quiesced_regions;
91 struct list_head recovered_regions;
92 struct list_head failed_recovered_regions;
103 struct region_hash *rh; /* FIXME: can we get rid of this ? */
107 struct list_head hash_list;
108 struct list_head list;
111 struct bio_list delayed_bios;
115 /*-----------------------------------------------------------------
116 * Mirror set structures.
117 *---------------------------------------------------------------*/
118 enum dm_raid1_error {
119 DM_RAID1_WRITE_ERROR,
125 struct mirror_set *ms;
126 atomic_t error_count;
127 unsigned long error_type;
133 struct dm_target *ti;
134 struct list_head list;
135 struct region_hash rh;
136 struct dm_kcopyd_client *kcopyd_client;
139 spinlock_t lock; /* protects the lists */
140 struct bio_list reads;
141 struct bio_list writes;
142 struct bio_list failures;
144 struct dm_io_client *io_client;
145 mempool_t *read_record_pool;
153 atomic_t default_mirror; /* Default mirror */
155 struct workqueue_struct *kmirrord_wq;
156 struct work_struct kmirrord_work;
157 struct timer_list timer;
158 unsigned long timer_pending;
160 struct work_struct trigger_event;
162 unsigned int nr_mirrors;
163 struct mirror mirror[0];
169 static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
171 return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
174 static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
176 return region << rh->region_shift;
179 static void wake(struct mirror_set *ms)
181 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
184 static void delayed_wake_fn(unsigned long data)
186 struct mirror_set *ms = (struct mirror_set *) data;
188 clear_bit(0, &ms->timer_pending);
192 static void delayed_wake(struct mirror_set *ms)
194 if (test_and_set_bit(0, &ms->timer_pending))
197 ms->timer.expires = jiffies + HZ / 5;
198 ms->timer.data = (unsigned long) ms;
199 ms->timer.function = delayed_wake_fn;
200 add_timer(&ms->timer);
203 /* FIXME move this */
204 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
206 #define MIN_REGIONS 64
207 #define MAX_RECOVERY 1
208 static int rh_init(struct region_hash *rh, struct mirror_set *ms,
209 struct dm_dirty_log *log, uint32_t region_size,
212 unsigned int nr_buckets, max_buckets;
216 * Calculate a suitable number of buckets for our hash
219 max_buckets = nr_regions >> 6;
220 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
226 rh->region_size = region_size;
227 rh->region_shift = ffs(region_size) - 1;
228 rwlock_init(&rh->hash_lock);
229 rh->mask = nr_buckets - 1;
230 rh->nr_buckets = nr_buckets;
232 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
234 DMERR("unable to allocate region hash memory");
238 for (i = 0; i < nr_buckets; i++)
239 INIT_LIST_HEAD(rh->buckets + i);
241 spin_lock_init(&rh->region_lock);
242 sema_init(&rh->recovery_count, 0);
243 atomic_set(&rh->recovery_in_flight, 0);
244 INIT_LIST_HEAD(&rh->clean_regions);
245 INIT_LIST_HEAD(&rh->quiesced_regions);
246 INIT_LIST_HEAD(&rh->recovered_regions);
247 INIT_LIST_HEAD(&rh->failed_recovered_regions);
249 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
250 sizeof(struct region));
251 if (!rh->region_pool) {
260 static void rh_exit(struct region_hash *rh)
263 struct region *reg, *nreg;
265 BUG_ON(!list_empty(&rh->quiesced_regions));
266 for (h = 0; h < rh->nr_buckets; h++) {
267 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
268 BUG_ON(atomic_read(®->pending));
269 mempool_free(reg, rh->region_pool);
274 dm_dirty_log_destroy(rh->log);
276 mempool_destroy(rh->region_pool);
280 #define RH_HASH_MULT 2654435387U
282 static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
284 return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
287 static struct region *__rh_lookup(struct region_hash *rh, region_t region)
291 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
292 if (reg->key == region)
298 static void __rh_insert(struct region_hash *rh, struct region *reg)
300 unsigned int h = rh_hash(rh, reg->key);
301 list_add(®->hash_list, rh->buckets + h);
304 static struct region *__rh_alloc(struct region_hash *rh, region_t region)
306 struct region *reg, *nreg;
308 read_unlock(&rh->hash_lock);
309 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
311 nreg = kmalloc(sizeof(struct region), GFP_NOIO);
312 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
313 RH_CLEAN : RH_NOSYNC;
317 INIT_LIST_HEAD(&nreg->list);
319 atomic_set(&nreg->pending, 0);
320 bio_list_init(&nreg->delayed_bios);
321 write_lock_irq(&rh->hash_lock);
323 reg = __rh_lookup(rh, region);
325 /* we lost the race */
326 mempool_free(nreg, rh->region_pool);
329 __rh_insert(rh, nreg);
330 if (nreg->state == RH_CLEAN) {
331 spin_lock(&rh->region_lock);
332 list_add(&nreg->list, &rh->clean_regions);
333 spin_unlock(&rh->region_lock);
337 write_unlock_irq(&rh->hash_lock);
338 read_lock(&rh->hash_lock);
343 static inline struct region *__rh_find(struct region_hash *rh, region_t region)
347 reg = __rh_lookup(rh, region);
349 reg = __rh_alloc(rh, region);
354 static int rh_state(struct region_hash *rh, region_t region, int may_block)
359 read_lock(&rh->hash_lock);
360 reg = __rh_lookup(rh, region);
361 read_unlock(&rh->hash_lock);
367 * The region wasn't in the hash, so we fall back to the
370 r = rh->log->type->in_sync(rh->log, region, may_block);
373 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
374 * taken as a RH_NOSYNC
376 return r == 1 ? RH_CLEAN : RH_NOSYNC;
379 static inline int rh_in_sync(struct region_hash *rh,
380 region_t region, int may_block)
382 int state = rh_state(rh, region, may_block);
383 return state == RH_CLEAN || state == RH_DIRTY;
386 static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
390 while ((bio = bio_list_pop(bio_list))) {
391 queue_bio(ms, bio, WRITE);
395 static void complete_resync_work(struct region *reg, int success)
397 struct region_hash *rh = reg->rh;
399 rh->log->type->set_region_sync(rh->log, reg->key, success);
402 * Dispatch the bios before we call 'wake_up_all'.
403 * This is important because if we are suspending,
404 * we want to know that recovery is complete and
405 * the work queue is flushed. If we wake_up_all
406 * before we dispatch_bios (queue bios and call wake()),
407 * then we risk suspending before the work queue
408 * has been properly flushed.
410 dispatch_bios(rh->ms, ®->delayed_bios);
411 if (atomic_dec_and_test(&rh->recovery_in_flight))
412 wake_up_all(&_kmirrord_recovery_stopped);
413 up(&rh->recovery_count);
416 static void rh_update_states(struct region_hash *rh)
418 struct region *reg, *next;
421 LIST_HEAD(recovered);
422 LIST_HEAD(failed_recovered);
425 * Quickly grab the lists.
427 write_lock_irq(&rh->hash_lock);
428 spin_lock(&rh->region_lock);
429 if (!list_empty(&rh->clean_regions)) {
430 list_splice_init(&rh->clean_regions, &clean);
432 list_for_each_entry(reg, &clean, list)
433 list_del(®->hash_list);
436 if (!list_empty(&rh->recovered_regions)) {
437 list_splice_init(&rh->recovered_regions, &recovered);
439 list_for_each_entry (reg, &recovered, list)
440 list_del(®->hash_list);
443 if (!list_empty(&rh->failed_recovered_regions)) {
444 list_splice_init(&rh->failed_recovered_regions,
447 list_for_each_entry(reg, &failed_recovered, list)
448 list_del(®->hash_list);
451 spin_unlock(&rh->region_lock);
452 write_unlock_irq(&rh->hash_lock);
455 * All the regions on the recovered and clean lists have
456 * now been pulled out of the system, so no need to do
459 list_for_each_entry_safe (reg, next, &recovered, list) {
460 rh->log->type->clear_region(rh->log, reg->key);
461 complete_resync_work(reg, 1);
462 mempool_free(reg, rh->region_pool);
465 list_for_each_entry_safe(reg, next, &failed_recovered, list) {
466 complete_resync_work(reg, errors_handled(rh->ms) ? 0 : 1);
467 mempool_free(reg, rh->region_pool);
470 list_for_each_entry_safe(reg, next, &clean, list) {
471 rh->log->type->clear_region(rh->log, reg->key);
472 mempool_free(reg, rh->region_pool);
475 rh->log->type->flush(rh->log);
478 static void rh_inc(struct region_hash *rh, region_t region)
482 read_lock(&rh->hash_lock);
483 reg = __rh_find(rh, region);
485 spin_lock_irq(&rh->region_lock);
486 atomic_inc(®->pending);
488 if (reg->state == RH_CLEAN) {
489 reg->state = RH_DIRTY;
490 list_del_init(®->list); /* take off the clean list */
491 spin_unlock_irq(&rh->region_lock);
493 rh->log->type->mark_region(rh->log, reg->key);
495 spin_unlock_irq(&rh->region_lock);
498 read_unlock(&rh->hash_lock);
501 static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
505 for (bio = bios->head; bio; bio = bio->bi_next)
506 rh_inc(rh, bio_to_region(rh, bio));
509 static void rh_dec(struct region_hash *rh, region_t region)
515 read_lock(&rh->hash_lock);
516 reg = __rh_lookup(rh, region);
517 read_unlock(&rh->hash_lock);
519 spin_lock_irqsave(&rh->region_lock, flags);
520 if (atomic_dec_and_test(®->pending)) {
522 * There is no pending I/O for this region.
523 * We can move the region to corresponding list for next action.
524 * At this point, the region is not yet connected to any list.
526 * If the state is RH_NOSYNC, the region should be kept off
528 * The hash entry for RH_NOSYNC will remain in memory
529 * until the region is recovered or the map is reloaded.
532 /* do nothing for RH_NOSYNC */
533 if (reg->state == RH_RECOVERING) {
534 list_add_tail(®->list, &rh->quiesced_regions);
535 } else if (reg->state == RH_DIRTY) {
536 reg->state = RH_CLEAN;
537 list_add(®->list, &rh->clean_regions);
541 spin_unlock_irqrestore(&rh->region_lock, flags);
548 * Starts quiescing a region in preparation for recovery.
550 static int __rh_recovery_prepare(struct region_hash *rh)
557 * Ask the dirty log what's next.
559 r = rh->log->type->get_resync_work(rh->log, ®ion);
564 * Get this region, and start it quiescing by setting the
567 read_lock(&rh->hash_lock);
568 reg = __rh_find(rh, region);
569 read_unlock(&rh->hash_lock);
571 spin_lock_irq(&rh->region_lock);
572 reg->state = RH_RECOVERING;
574 /* Already quiesced ? */
575 if (atomic_read(®->pending))
576 list_del_init(®->list);
578 list_move(®->list, &rh->quiesced_regions);
580 spin_unlock_irq(&rh->region_lock);
585 static void rh_recovery_prepare(struct region_hash *rh)
587 /* Extra reference to avoid race with rh_stop_recovery */
588 atomic_inc(&rh->recovery_in_flight);
590 while (!down_trylock(&rh->recovery_count)) {
591 atomic_inc(&rh->recovery_in_flight);
592 if (__rh_recovery_prepare(rh) <= 0) {
593 atomic_dec(&rh->recovery_in_flight);
594 up(&rh->recovery_count);
599 /* Drop the extra reference */
600 if (atomic_dec_and_test(&rh->recovery_in_flight))
601 wake_up_all(&_kmirrord_recovery_stopped);
605 * Returns any quiesced regions.
607 static struct region *rh_recovery_start(struct region_hash *rh)
609 struct region *reg = NULL;
611 spin_lock_irq(&rh->region_lock);
612 if (!list_empty(&rh->quiesced_regions)) {
613 reg = list_entry(rh->quiesced_regions.next,
614 struct region, list);
615 list_del_init(®->list); /* remove from the quiesced list */
617 spin_unlock_irq(&rh->region_lock);
622 static void rh_recovery_end(struct region *reg, int success)
624 struct region_hash *rh = reg->rh;
626 spin_lock_irq(&rh->region_lock);
628 list_add(®->list, ®->rh->recovered_regions);
630 reg->state = RH_NOSYNC;
631 list_add(®->list, ®->rh->failed_recovered_regions);
633 spin_unlock_irq(&rh->region_lock);
638 static int rh_flush(struct region_hash *rh)
640 return rh->log->type->flush(rh->log);
643 static void rh_delay(struct region_hash *rh, struct bio *bio)
647 read_lock(&rh->hash_lock);
648 reg = __rh_find(rh, bio_to_region(rh, bio));
649 bio_list_add(®->delayed_bios, bio);
650 read_unlock(&rh->hash_lock);
653 static void rh_stop_recovery(struct region_hash *rh)
657 /* wait for any recovering regions */
658 for (i = 0; i < MAX_RECOVERY; i++)
659 down(&rh->recovery_count);
662 static void rh_start_recovery(struct region_hash *rh)
666 for (i = 0; i < MAX_RECOVERY; i++)
667 up(&rh->recovery_count);
672 #define MIN_READ_RECORDS 20
673 struct dm_raid1_read_record {
675 struct dm_bio_details details;
679 * Every mirror should look like this one.
681 #define DEFAULT_MIRROR 0
684 * This is yucky. We squirrel the mirror struct away inside
685 * bi_next for read/write buffers. This is safe since the bh
686 * doesn't get submitted to the lower levels of block layer.
688 static struct mirror *bio_get_m(struct bio *bio)
690 return (struct mirror *) bio->bi_next;
693 static void bio_set_m(struct bio *bio, struct mirror *m)
695 bio->bi_next = (struct bio *) m;
698 static struct mirror *get_default_mirror(struct mirror_set *ms)
700 return &ms->mirror[atomic_read(&ms->default_mirror)];
703 static void set_default_mirror(struct mirror *m)
705 struct mirror_set *ms = m->ms;
706 struct mirror *m0 = &(ms->mirror[0]);
708 atomic_set(&ms->default_mirror, m - m0);
712 * @m: mirror device to fail
713 * @error_type: one of the enum's, DM_RAID1_*_ERROR
715 * If errors are being handled, record the type of
716 * error encountered for this device. If this type
717 * of error has already been recorded, we can return;
718 * otherwise, we must signal userspace by triggering
719 * an event. Additionally, if the device is the
720 * primary device, we must choose a new primary, but
721 * only if the mirror is in-sync.
723 * This function must not block.
725 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
727 struct mirror_set *ms = m->ms;
730 if (!errors_handled(ms))
734 * error_count is used for nothing more than a
735 * simple way to tell if a device has encountered
738 atomic_inc(&m->error_count);
740 if (test_and_set_bit(error_type, &m->error_type))
743 if (m != get_default_mirror(ms))
748 * Better to issue requests to same failing device
749 * than to risk returning corrupt data.
751 DMERR("Primary mirror (%s) failed while out-of-sync: "
752 "Reads may fail.", m->dev->name);
756 for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
757 if (!atomic_read(&new->error_count)) {
758 set_default_mirror(new);
762 if (unlikely(new == ms->mirror + ms->nr_mirrors))
763 DMWARN("All sides of mirror have failed.");
766 schedule_work(&ms->trigger_event);
769 /*-----------------------------------------------------------------
772 * When a mirror is first activated we may find that some regions
773 * are in the no-sync state. We have to recover these by
774 * recopying from the default mirror to all the others.
775 *---------------------------------------------------------------*/
776 static void recovery_complete(int read_err, unsigned long write_err,
779 struct region *reg = (struct region *)context;
780 struct mirror_set *ms = reg->rh->ms;
784 /* Read error means the failure of default mirror. */
785 DMERR_LIMIT("Unable to read primary mirror during recovery");
786 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
790 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
793 * Bits correspond to devices (excluding default mirror).
794 * The default mirror cannot change during recovery.
796 for (m = 0; m < ms->nr_mirrors; m++) {
797 if (&ms->mirror[m] == get_default_mirror(ms))
799 if (test_bit(bit, &write_err))
800 fail_mirror(ms->mirror + m,
801 DM_RAID1_SYNC_ERROR);
806 rh_recovery_end(reg, !(read_err || write_err));
809 static int recover(struct mirror_set *ms, struct region *reg)
813 struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
815 unsigned long flags = 0;
817 /* fill in the source */
818 m = get_default_mirror(ms);
819 from.bdev = m->dev->bdev;
820 from.sector = m->offset + region_to_sector(reg->rh, reg->key);
821 if (reg->key == (ms->nr_regions - 1)) {
823 * The final region may be smaller than
826 from.count = ms->ti->len & (reg->rh->region_size - 1);
828 from.count = reg->rh->region_size;
830 from.count = reg->rh->region_size;
832 /* fill in the destinations */
833 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
834 if (&ms->mirror[i] == get_default_mirror(ms))
838 dest->bdev = m->dev->bdev;
839 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
840 dest->count = from.count;
845 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
846 r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
847 flags, recovery_complete, reg);
852 static void do_recovery(struct mirror_set *ms)
856 struct dm_dirty_log *log = ms->rh.log;
859 * Start quiescing some regions.
861 rh_recovery_prepare(&ms->rh);
864 * Copy any already quiesced regions.
866 while ((reg = rh_recovery_start(&ms->rh))) {
867 r = recover(ms, reg);
869 rh_recovery_end(reg, 0);
873 * Update the in sync flag.
876 (log->type->get_sync_count(log) == ms->nr_regions)) {
877 /* the sync is complete */
878 dm_table_event(ms->ti->table);
883 /*-----------------------------------------------------------------
885 *---------------------------------------------------------------*/
886 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
888 struct mirror *m = get_default_mirror(ms);
891 if (likely(!atomic_read(&m->error_count)))
894 if (m-- == ms->mirror)
896 } while (m != get_default_mirror(ms));
901 static int default_ok(struct mirror *m)
903 struct mirror *default_mirror = get_default_mirror(m->ms);
905 return !atomic_read(&default_mirror->error_count);
908 static int mirror_available(struct mirror_set *ms, struct bio *bio)
910 region_t region = bio_to_region(&ms->rh, bio);
912 if (ms->rh.log->type->in_sync(ms->rh.log, region, 0))
913 return choose_mirror(ms, bio->bi_sector) ? 1 : 0;
919 * remap a buffer to a particular mirror.
921 static sector_t map_sector(struct mirror *m, struct bio *bio)
923 return m->offset + (bio->bi_sector - m->ms->ti->begin);
926 static void map_bio(struct mirror *m, struct bio *bio)
928 bio->bi_bdev = m->dev->bdev;
929 bio->bi_sector = map_sector(m, bio);
932 static void map_region(struct dm_io_region *io, struct mirror *m,
935 io->bdev = m->dev->bdev;
936 io->sector = map_sector(m, bio);
937 io->count = bio->bi_size >> 9;
940 /*-----------------------------------------------------------------
942 *---------------------------------------------------------------*/
943 static void read_callback(unsigned long error, void *context)
945 struct bio *bio = context;
949 bio_set_m(bio, NULL);
951 if (likely(!error)) {
956 fail_mirror(m, DM_RAID1_READ_ERROR);
958 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
959 DMWARN_LIMIT("Read failure on mirror device %s. "
960 "Trying alternative device.",
962 queue_bio(m->ms, bio, bio_rw(bio));
966 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
968 bio_endio(bio, -EIO);
971 /* Asynchronous read. */
972 static void read_async_bio(struct mirror *m, struct bio *bio)
974 struct dm_io_region io;
975 struct dm_io_request io_req = {
977 .mem.type = DM_IO_BVEC,
978 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
979 .notify.fn = read_callback,
980 .notify.context = bio,
981 .client = m->ms->io_client,
984 map_region(&io, m, bio);
986 (void) dm_io(&io_req, 1, &io, NULL);
989 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
995 while ((bio = bio_list_pop(reads))) {
996 region = bio_to_region(&ms->rh, bio);
997 m = get_default_mirror(ms);
1000 * We can only read balance if the region is in sync.
1002 if (likely(rh_in_sync(&ms->rh, region, 1)))
1003 m = choose_mirror(ms, bio->bi_sector);
1004 else if (m && atomic_read(&m->error_count))
1008 read_async_bio(m, bio);
1010 bio_endio(bio, -EIO);
1014 /*-----------------------------------------------------------------
1017 * We do different things with the write io depending on the
1018 * state of the region that it's in:
1020 * SYNC: increment pending, use kcopyd to write to *all* mirrors
1021 * RECOVERING: delay the io until recovery completes
1022 * NOSYNC: increment pending, just write to the default mirror
1023 *---------------------------------------------------------------*/
1025 /* __bio_mark_nosync
1031 * The bio was written on some mirror(s) but failed on other mirror(s).
1032 * We can successfully endio the bio but should avoid the region being
1033 * marked clean by setting the state RH_NOSYNC.
1035 * This function is _not_ safe in interrupt context!
1037 static void __bio_mark_nosync(struct mirror_set *ms,
1038 struct bio *bio, unsigned done, int error)
1040 unsigned long flags;
1041 struct region_hash *rh = &ms->rh;
1042 struct dm_dirty_log *log = ms->rh.log;
1044 region_t region = bio_to_region(rh, bio);
1047 /* We must inform the log that the sync count has changed. */
1048 log->type->set_region_sync(log, region, 0);
1051 read_lock(&rh->hash_lock);
1052 reg = __rh_find(rh, region);
1053 read_unlock(&rh->hash_lock);
1055 /* region hash entry should exist because write was in-flight */
1057 BUG_ON(!list_empty(®->list));
1059 spin_lock_irqsave(&rh->region_lock, flags);
1063 * 2) RH_NOSYNC: was dirty, other preceeding writes failed
1064 * 3) RH_RECOVERING: flushing pending writes
1065 * Either case, the region should have not been connected to list.
1067 recovering = (reg->state == RH_RECOVERING);
1068 reg->state = RH_NOSYNC;
1069 BUG_ON(!list_empty(®->list));
1070 spin_unlock_irqrestore(&rh->region_lock, flags);
1072 bio_endio(bio, error);
1074 complete_resync_work(reg, 0);
1077 static void write_callback(unsigned long error, void *context)
1079 unsigned i, ret = 0;
1080 struct bio *bio = (struct bio *) context;
1081 struct mirror_set *ms;
1083 int should_wake = 0;
1084 unsigned long flags;
1086 ms = bio_get_m(bio)->ms;
1087 bio_set_m(bio, NULL);
1090 * NOTE: We don't decrement the pending count here,
1091 * instead it is done by the targets endio function.
1092 * This way we handle both writes to SYNC and NOSYNC
1093 * regions with the same code.
1098 for (i = 0; i < ms->nr_mirrors; i++)
1099 if (test_bit(i, &error))
1100 fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
1104 if (unlikely(!uptodate)) {
1105 DMERR("All replicated volumes dead, failing I/O");
1106 /* None of the writes succeeded, fail the I/O. */
1108 } else if (errors_handled(ms)) {
1110 * Need to raise event. Since raising
1111 * events can block, we need to do it in
1114 spin_lock_irqsave(&ms->lock, flags);
1115 if (!ms->failures.head)
1117 bio_list_add(&ms->failures, bio);
1118 spin_unlock_irqrestore(&ms->lock, flags);
1124 bio_endio(bio, ret);
1127 static void do_write(struct mirror_set *ms, struct bio *bio)
1130 struct dm_io_region io[ms->nr_mirrors], *dest = io;
1132 struct dm_io_request io_req = {
1134 .mem.type = DM_IO_BVEC,
1135 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
1136 .notify.fn = write_callback,
1137 .notify.context = bio,
1138 .client = ms->io_client,
1141 for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
1142 map_region(dest++, m, bio);
1145 * Use default mirror because we only need it to retrieve the reference
1146 * to the mirror set in write_callback().
1148 bio_set_m(bio, get_default_mirror(ms));
1150 (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
1153 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
1157 struct bio_list sync, nosync, recover, *this_list = NULL;
1163 * Classify each write.
1165 bio_list_init(&sync);
1166 bio_list_init(&nosync);
1167 bio_list_init(&recover);
1169 while ((bio = bio_list_pop(writes))) {
1170 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
1178 this_list = &nosync;
1182 this_list = &recover;
1186 bio_list_add(this_list, bio);
1190 * Increment the pending counts for any regions that will
1191 * be written to (writes to recover regions are going to
1194 rh_inc_pending(&ms->rh, &sync);
1195 rh_inc_pending(&ms->rh, &nosync);
1196 ms->log_failure = rh_flush(&ms->rh) ? 1 : 0;
1201 if (unlikely(ms->log_failure)) {
1202 spin_lock_irq(&ms->lock);
1203 bio_list_merge(&ms->failures, &sync);
1204 spin_unlock_irq(&ms->lock);
1207 while ((bio = bio_list_pop(&sync)))
1210 while ((bio = bio_list_pop(&recover)))
1211 rh_delay(&ms->rh, bio);
1213 while ((bio = bio_list_pop(&nosync))) {
1214 map_bio(get_default_mirror(ms), bio);
1215 generic_make_request(bio);
1219 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
1223 if (!failures->head)
1226 if (!ms->log_failure) {
1227 while ((bio = bio_list_pop(failures)))
1228 __bio_mark_nosync(ms, bio, bio->bi_size, 0);
1233 * If the log has failed, unattempted writes are being
1234 * put on the failures list. We can't issue those writes
1235 * until a log has been marked, so we must store them.
1237 * If a 'noflush' suspend is in progress, we can requeue
1238 * the I/O's to the core. This give userspace a chance
1239 * to reconfigure the mirror, at which point the core
1240 * will reissue the writes. If the 'noflush' flag is
1241 * not set, we have no choice but to return errors.
1243 * Some writes on the failures list may have been
1244 * submitted before the log failure and represent a
1245 * failure to write to one of the devices. It is ok
1246 * for us to treat them the same and requeue them
1249 if (dm_noflush_suspending(ms->ti)) {
1250 while ((bio = bio_list_pop(failures)))
1251 bio_endio(bio, DM_ENDIO_REQUEUE);
1255 if (atomic_read(&ms->suspend)) {
1256 while ((bio = bio_list_pop(failures)))
1257 bio_endio(bio, -EIO);
1261 spin_lock_irq(&ms->lock);
1262 bio_list_merge(&ms->failures, failures);
1263 spin_unlock_irq(&ms->lock);
1268 static void trigger_event(struct work_struct *work)
1270 struct mirror_set *ms =
1271 container_of(work, struct mirror_set, trigger_event);
1273 dm_table_event(ms->ti->table);
1276 /*-----------------------------------------------------------------
1278 *---------------------------------------------------------------*/
1279 static void do_mirror(struct work_struct *work)
1281 struct mirror_set *ms =container_of(work, struct mirror_set,
1283 struct bio_list reads, writes, failures;
1284 unsigned long flags;
1286 spin_lock_irqsave(&ms->lock, flags);
1288 writes = ms->writes;
1289 failures = ms->failures;
1290 bio_list_init(&ms->reads);
1291 bio_list_init(&ms->writes);
1292 bio_list_init(&ms->failures);
1293 spin_unlock_irqrestore(&ms->lock, flags);
1295 rh_update_states(&ms->rh);
1297 do_reads(ms, &reads);
1298 do_writes(ms, &writes);
1299 do_failures(ms, &failures);
1301 dm_table_unplug_all(ms->ti->table);
1305 /*-----------------------------------------------------------------
1307 *---------------------------------------------------------------*/
1308 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
1309 uint32_t region_size,
1310 struct dm_target *ti,
1311 struct dm_dirty_log *dl)
1314 struct mirror_set *ms = NULL;
1316 if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
1319 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
1321 ms = kzalloc(len, GFP_KERNEL);
1323 ti->error = "Cannot allocate mirror context";
1327 spin_lock_init(&ms->lock);
1330 ms->nr_mirrors = nr_mirrors;
1331 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
1333 ms->log_failure = 0;
1334 atomic_set(&ms->suspend, 0);
1335 atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
1337 len = sizeof(struct dm_raid1_read_record);
1338 ms->read_record_pool = mempool_create_kmalloc_pool(MIN_READ_RECORDS,
1340 if (!ms->read_record_pool) {
1341 ti->error = "Error creating mirror read_record_pool";
1346 ms->io_client = dm_io_client_create(DM_IO_PAGES);
1347 if (IS_ERR(ms->io_client)) {
1348 ti->error = "Error creating dm_io client";
1349 mempool_destroy(ms->read_record_pool);
1354 if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
1355 ti->error = "Error creating dirty region hash";
1356 dm_io_client_destroy(ms->io_client);
1357 mempool_destroy(ms->read_record_pool);
1365 static void free_context(struct mirror_set *ms, struct dm_target *ti,
1369 dm_put_device(ti, ms->mirror[m].dev);
1371 dm_io_client_destroy(ms->io_client);
1373 mempool_destroy(ms->read_record_pool);
1377 static inline int _check_region_size(struct dm_target *ti, uint32_t size)
1379 return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
1383 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
1384 unsigned int mirror, char **argv)
1386 unsigned long long offset;
1388 if (sscanf(argv[1], "%llu", &offset) != 1) {
1389 ti->error = "Invalid offset";
1393 if (dm_get_device(ti, argv[0], offset, ti->len,
1394 dm_table_get_mode(ti->table),
1395 &ms->mirror[mirror].dev)) {
1396 ti->error = "Device lookup failure";
1400 ms->mirror[mirror].ms = ms;
1401 atomic_set(&(ms->mirror[mirror].error_count), 0);
1402 ms->mirror[mirror].error_type = 0;
1403 ms->mirror[mirror].offset = offset;
1409 * Create dirty log: log_type #log_params <log_params>
1411 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
1412 unsigned int argc, char **argv,
1413 unsigned int *args_used)
1415 unsigned int param_count;
1416 struct dm_dirty_log *dl;
1419 ti->error = "Insufficient mirror log arguments";
1423 if (sscanf(argv[1], "%u", ¶m_count) != 1) {
1424 ti->error = "Invalid mirror log argument count";
1428 *args_used = 2 + param_count;
1430 if (argc < *args_used) {
1431 ti->error = "Insufficient mirror log arguments";
1435 dl = dm_dirty_log_create(argv[0], ti, param_count, argv + 2);
1437 ti->error = "Error creating mirror dirty log";
1441 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
1442 ti->error = "Invalid region size";
1443 dm_dirty_log_destroy(dl);
1450 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1451 unsigned *args_used)
1453 unsigned num_features;
1454 struct dm_target *ti = ms->ti;
1461 if (sscanf(argv[0], "%u", &num_features) != 1) {
1462 ti->error = "Invalid number of features";
1470 if (num_features > argc) {
1471 ti->error = "Not enough arguments to support feature count";
1475 if (!strcmp("handle_errors", argv[0]))
1476 ms->features |= DM_RAID1_HANDLE_ERRORS;
1478 ti->error = "Unrecognised feature requested";
1488 * Construct a mirror mapping:
1490 * log_type #log_params <log_params>
1491 * #mirrors [mirror_path offset]{2,}
1492 * [#features <features>]
1494 * log_type is "core" or "disk"
1495 * #log_params is between 1 and 3
1497 * If present, features must be "handle_errors".
1499 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1502 unsigned int nr_mirrors, m, args_used;
1503 struct mirror_set *ms;
1504 struct dm_dirty_log *dl;
1506 dl = create_dirty_log(ti, argc, argv, &args_used);
1513 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1514 nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
1515 ti->error = "Invalid number of mirrors";
1516 dm_dirty_log_destroy(dl);
1522 if (argc < nr_mirrors * 2) {
1523 ti->error = "Too few mirror arguments";
1524 dm_dirty_log_destroy(dl);
1528 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1530 dm_dirty_log_destroy(dl);
1534 /* Get the mirror parameter sets */
1535 for (m = 0; m < nr_mirrors; m++) {
1536 r = get_mirror(ms, ti, m, argv);
1538 free_context(ms, ti, m);
1546 ti->split_io = ms->rh.region_size;
1548 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1549 if (!ms->kmirrord_wq) {
1550 DMERR("couldn't start kmirrord");
1552 goto err_free_context;
1554 INIT_WORK(&ms->kmirrord_work, do_mirror);
1555 init_timer(&ms->timer);
1556 ms->timer_pending = 0;
1557 INIT_WORK(&ms->trigger_event, trigger_event);
1559 r = parse_features(ms, argc, argv, &args_used);
1561 goto err_destroy_wq;
1567 * Any read-balancing addition depends on the
1568 * DM_RAID1_HANDLE_ERRORS flag being present.
1569 * This is because the decision to balance depends
1570 * on the sync state of a region. If the above
1571 * flag is not present, we ignore errors; and
1572 * the sync state may be inaccurate.
1576 ti->error = "Too many mirror arguments";
1578 goto err_destroy_wq;
1581 r = dm_kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
1583 goto err_destroy_wq;
1589 destroy_workqueue(ms->kmirrord_wq);
1591 free_context(ms, ti, ms->nr_mirrors);
1595 static void mirror_dtr(struct dm_target *ti)
1597 struct mirror_set *ms = (struct mirror_set *) ti->private;
1599 del_timer_sync(&ms->timer);
1600 flush_workqueue(ms->kmirrord_wq);
1601 dm_kcopyd_client_destroy(ms->kcopyd_client);
1602 destroy_workqueue(ms->kmirrord_wq);
1603 free_context(ms, ti, ms->nr_mirrors);
1606 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1608 unsigned long flags;
1609 int should_wake = 0;
1610 struct bio_list *bl;
1612 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
1613 spin_lock_irqsave(&ms->lock, flags);
1614 should_wake = !(bl->head);
1615 bio_list_add(bl, bio);
1616 spin_unlock_irqrestore(&ms->lock, flags);
1623 * Mirror mapping function
1625 static int mirror_map(struct dm_target *ti, struct bio *bio,
1626 union map_info *map_context)
1628 int r, rw = bio_rw(bio);
1630 struct mirror_set *ms = ti->private;
1631 struct dm_raid1_read_record *read_record = NULL;
1634 /* Save region for mirror_end_io() handler */
1635 map_context->ll = bio_to_region(&ms->rh, bio);
1636 queue_bio(ms, bio, rw);
1637 return DM_MAPIO_SUBMITTED;
1640 r = ms->rh.log->type->in_sync(ms->rh.log,
1641 bio_to_region(&ms->rh, bio), 0);
1642 if (r < 0 && r != -EWOULDBLOCK)
1646 * If region is not in-sync queue the bio.
1648 if (!r || (r == -EWOULDBLOCK)) {
1650 return -EWOULDBLOCK;
1652 queue_bio(ms, bio, rw);
1653 return DM_MAPIO_SUBMITTED;
1657 * The region is in-sync and we can perform reads directly.
1658 * Store enough information so we can retry if it fails.
1660 m = choose_mirror(ms, bio->bi_sector);
1664 read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1665 if (likely(read_record)) {
1666 dm_bio_record(&read_record->details, bio);
1667 map_context->ptr = read_record;
1673 return DM_MAPIO_REMAPPED;
1676 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1677 int error, union map_info *map_context)
1679 int rw = bio_rw(bio);
1680 struct mirror_set *ms = (struct mirror_set *) ti->private;
1681 struct mirror *m = NULL;
1682 struct dm_bio_details *bd = NULL;
1683 struct dm_raid1_read_record *read_record = map_context->ptr;
1686 * We need to dec pending if this was a write.
1689 rh_dec(&ms->rh, map_context->ll);
1693 if (error == -EOPNOTSUPP)
1696 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1699 if (unlikely(error)) {
1702 * There wasn't enough memory to record necessary
1703 * information for a retry or there was no other
1706 DMERR_LIMIT("Mirror read failed.");
1712 DMERR("Mirror read failed from %s. Trying alternative device.",
1715 fail_mirror(m, DM_RAID1_READ_ERROR);
1718 * A failed read is requeued for another attempt using an intact
1721 if (default_ok(m) || mirror_available(ms, bio)) {
1722 bd = &read_record->details;
1724 dm_bio_restore(bd, bio);
1725 mempool_free(read_record, ms->read_record_pool);
1726 map_context->ptr = NULL;
1727 queue_bio(ms, bio, rw);
1730 DMERR("All replicated volumes dead, failing I/O");
1735 mempool_free(read_record, ms->read_record_pool);
1736 map_context->ptr = NULL;
1742 static void mirror_presuspend(struct dm_target *ti)
1744 struct mirror_set *ms = (struct mirror_set *) ti->private;
1745 struct dm_dirty_log *log = ms->rh.log;
1747 atomic_set(&ms->suspend, 1);
1750 * We must finish up all the work that we've
1751 * generated (i.e. recovery work).
1753 rh_stop_recovery(&ms->rh);
1755 wait_event(_kmirrord_recovery_stopped,
1756 !atomic_read(&ms->rh.recovery_in_flight));
1758 if (log->type->presuspend && log->type->presuspend(log))
1759 /* FIXME: need better error handling */
1760 DMWARN("log presuspend failed");
1763 * Now that recovery is complete/stopped and the
1764 * delayed bios are queued, we need to wait for
1765 * the worker thread to complete. This way,
1766 * we know that all of our I/O has been pushed.
1768 flush_workqueue(ms->kmirrord_wq);
1771 static void mirror_postsuspend(struct dm_target *ti)
1773 struct mirror_set *ms = ti->private;
1774 struct dm_dirty_log *log = ms->rh.log;
1776 if (log->type->postsuspend && log->type->postsuspend(log))
1777 /* FIXME: need better error handling */
1778 DMWARN("log postsuspend failed");
1781 static void mirror_resume(struct dm_target *ti)
1783 struct mirror_set *ms = ti->private;
1784 struct dm_dirty_log *log = ms->rh.log;
1786 atomic_set(&ms->suspend, 0);
1787 if (log->type->resume && log->type->resume(log))
1788 /* FIXME: need better error handling */
1789 DMWARN("log resume failed");
1790 rh_start_recovery(&ms->rh);
1794 * device_status_char
1795 * @m: mirror device/leg we want the status of
1797 * We return one character representing the most severe error
1798 * we have encountered.
1799 * A => Alive - No failures
1800 * D => Dead - A write failure occurred leaving mirror out-of-sync
1801 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1802 * R => Read - A read failure occurred, mirror data unaffected
1806 static char device_status_char(struct mirror *m)
1808 if (!atomic_read(&(m->error_count)))
1811 return (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1812 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1813 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1817 static int mirror_status(struct dm_target *ti, status_type_t type,
1818 char *result, unsigned int maxlen)
1820 unsigned int m, sz = 0;
1821 struct mirror_set *ms = (struct mirror_set *) ti->private;
1822 struct dm_dirty_log *log = ms->rh.log;
1823 char buffer[ms->nr_mirrors + 1];
1826 case STATUSTYPE_INFO:
1827 DMEMIT("%d ", ms->nr_mirrors);
1828 for (m = 0; m < ms->nr_mirrors; m++) {
1829 DMEMIT("%s ", ms->mirror[m].dev->name);
1830 buffer[m] = device_status_char(&(ms->mirror[m]));
1834 DMEMIT("%llu/%llu 1 %s ",
1835 (unsigned long long)log->type->get_sync_count(ms->rh.log),
1836 (unsigned long long)ms->nr_regions, buffer);
1838 sz += log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
1842 case STATUSTYPE_TABLE:
1843 sz = log->type->status(ms->rh.log, type, result, maxlen);
1845 DMEMIT("%d", ms->nr_mirrors);
1846 for (m = 0; m < ms->nr_mirrors; m++)
1847 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1848 (unsigned long long)ms->mirror[m].offset);
1850 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1851 DMEMIT(" 1 handle_errors");
1857 static struct target_type mirror_target = {
1859 .version = {1, 0, 20},
1860 .module = THIS_MODULE,
1864 .end_io = mirror_end_io,
1865 .presuspend = mirror_presuspend,
1866 .postsuspend = mirror_postsuspend,
1867 .resume = mirror_resume,
1868 .status = mirror_status,
1871 static int __init dm_mirror_init(void)
1875 r = dm_register_target(&mirror_target);
1877 DMERR("Failed to register mirror target");
1882 static void __exit dm_mirror_exit(void)
1886 r = dm_unregister_target(&mirror_target);
1888 DMERR("unregister failed %d", r);
1892 module_init(dm_mirror_init);
1893 module_exit(dm_mirror_exit);
1895 MODULE_DESCRIPTION(DM_NAME " mirror target");
1896 MODULE_AUTHOR("Joe Thornber");
1897 MODULE_LICENSE("GPL");