2 * Copyright (C) 2003 Sistina Software Limited.
4 * This file is released under the GPL.
8 #include "dm-bio-list.h"
13 #include <linux/ctype.h>
14 #include <linux/init.h>
15 #include <linux/mempool.h>
16 #include <linux/module.h>
17 #include <linux/pagemap.h>
18 #include <linux/slab.h>
19 #include <linux/time.h>
20 #include <linux/vmalloc.h>
21 #include <linux/workqueue.h>
22 #include <linux/log2.h>
24 #define DM_MSG_PREFIX "raid1"
25 #define DM_IO_PAGES 64
27 #define DM_RAID1_HANDLE_ERRORS 0x01
28 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
30 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
32 /*-----------------------------------------------------------------
35 * The mirror splits itself up into discrete regions. Each
36 * region can be in one of three states: clean, dirty,
37 * nosync. There is no need to put clean regions in the hash.
39 * In addition to being present in the hash table a region _may_
40 * be present on one of three lists.
42 * clean_regions: Regions on this list have no io pending to
43 * them, they are in sync, we are no longer interested in them,
44 * they are dull. rh_update_states() will remove them from the
47 * quiesced_regions: These regions have been spun down, ready
48 * for recovery. rh_recovery_start() will remove regions from
49 * this list and hand them to kmirrord, which will schedule the
50 * recovery io with kcopyd.
52 * recovered_regions: Regions that kcopyd has successfully
53 * recovered. rh_update_states() will now schedule any delayed
54 * io, up the recovery_count, and remove the region from the
58 * A rw spin lock 'hash_lock' protects just the hash table,
59 * this is never held in write mode from interrupt context,
60 * which I believe means that we only have to disable irqs when
63 * An ordinary spin lock 'region_lock' that protects the three
64 * lists in the region_hash, with the 'state', 'list' and
65 * 'bhs_delayed' fields of the regions. This is used from irq
66 * context, so all other uses will have to suspend local irqs.
67 *---------------------------------------------------------------*/
70 struct mirror_set *ms;
72 unsigned region_shift;
74 /* holds persistent region state */
75 struct dirty_log *log;
79 mempool_t *region_pool;
81 unsigned int nr_buckets;
82 struct list_head *buckets;
84 spinlock_t region_lock;
85 atomic_t recovery_in_flight;
86 struct semaphore recovery_count;
87 struct list_head clean_regions;
88 struct list_head quiesced_regions;
89 struct list_head recovered_regions;
90 struct list_head failed_recovered_regions;
101 struct region_hash *rh; /* FIXME: can we get rid of this ? */
105 struct list_head hash_list;
106 struct list_head list;
109 struct bio_list delayed_bios;
113 /*-----------------------------------------------------------------
114 * Mirror set structures.
115 *---------------------------------------------------------------*/
117 atomic_t error_count;
123 struct dm_target *ti;
124 struct list_head list;
125 struct region_hash rh;
126 struct kcopyd_client *kcopyd_client;
129 spinlock_t lock; /* protects the next two lists */
130 struct bio_list reads;
131 struct bio_list writes;
133 struct dm_io_client *io_client;
140 struct mirror *default_mirror; /* Default mirror */
142 struct workqueue_struct *kmirrord_wq;
143 struct work_struct kmirrord_work;
145 unsigned int nr_mirrors;
146 struct mirror mirror[0];
152 static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
154 return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
157 static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
159 return region << rh->region_shift;
162 static void wake(struct mirror_set *ms)
164 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
167 /* FIXME move this */
168 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
170 #define MIN_REGIONS 64
171 #define MAX_RECOVERY 1
172 static int rh_init(struct region_hash *rh, struct mirror_set *ms,
173 struct dirty_log *log, uint32_t region_size,
176 unsigned int nr_buckets, max_buckets;
180 * Calculate a suitable number of buckets for our hash
183 max_buckets = nr_regions >> 6;
184 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
190 rh->region_size = region_size;
191 rh->region_shift = ffs(region_size) - 1;
192 rwlock_init(&rh->hash_lock);
193 rh->mask = nr_buckets - 1;
194 rh->nr_buckets = nr_buckets;
196 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
198 DMERR("unable to allocate region hash memory");
202 for (i = 0; i < nr_buckets; i++)
203 INIT_LIST_HEAD(rh->buckets + i);
205 spin_lock_init(&rh->region_lock);
206 sema_init(&rh->recovery_count, 0);
207 atomic_set(&rh->recovery_in_flight, 0);
208 INIT_LIST_HEAD(&rh->clean_regions);
209 INIT_LIST_HEAD(&rh->quiesced_regions);
210 INIT_LIST_HEAD(&rh->recovered_regions);
211 INIT_LIST_HEAD(&rh->failed_recovered_regions);
213 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
214 sizeof(struct region));
215 if (!rh->region_pool) {
224 static void rh_exit(struct region_hash *rh)
227 struct region *reg, *nreg;
229 BUG_ON(!list_empty(&rh->quiesced_regions));
230 for (h = 0; h < rh->nr_buckets; h++) {
231 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
232 BUG_ON(atomic_read(®->pending));
233 mempool_free(reg, rh->region_pool);
238 dm_destroy_dirty_log(rh->log);
240 mempool_destroy(rh->region_pool);
244 #define RH_HASH_MULT 2654435387U
246 static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
248 return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
251 static struct region *__rh_lookup(struct region_hash *rh, region_t region)
255 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
256 if (reg->key == region)
262 static void __rh_insert(struct region_hash *rh, struct region *reg)
264 unsigned int h = rh_hash(rh, reg->key);
265 list_add(®->hash_list, rh->buckets + h);
268 static struct region *__rh_alloc(struct region_hash *rh, region_t region)
270 struct region *reg, *nreg;
272 read_unlock(&rh->hash_lock);
273 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
275 nreg = kmalloc(sizeof(struct region), GFP_NOIO);
276 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
277 RH_CLEAN : RH_NOSYNC;
281 INIT_LIST_HEAD(&nreg->list);
283 atomic_set(&nreg->pending, 0);
284 bio_list_init(&nreg->delayed_bios);
285 write_lock_irq(&rh->hash_lock);
287 reg = __rh_lookup(rh, region);
289 /* we lost the race */
290 mempool_free(nreg, rh->region_pool);
293 __rh_insert(rh, nreg);
294 if (nreg->state == RH_CLEAN) {
295 spin_lock(&rh->region_lock);
296 list_add(&nreg->list, &rh->clean_regions);
297 spin_unlock(&rh->region_lock);
301 write_unlock_irq(&rh->hash_lock);
302 read_lock(&rh->hash_lock);
307 static inline struct region *__rh_find(struct region_hash *rh, region_t region)
311 reg = __rh_lookup(rh, region);
313 reg = __rh_alloc(rh, region);
318 static int rh_state(struct region_hash *rh, region_t region, int may_block)
323 read_lock(&rh->hash_lock);
324 reg = __rh_lookup(rh, region);
325 read_unlock(&rh->hash_lock);
331 * The region wasn't in the hash, so we fall back to the
334 r = rh->log->type->in_sync(rh->log, region, may_block);
337 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
338 * taken as a RH_NOSYNC
340 return r == 1 ? RH_CLEAN : RH_NOSYNC;
343 static inline int rh_in_sync(struct region_hash *rh,
344 region_t region, int may_block)
346 int state = rh_state(rh, region, may_block);
347 return state == RH_CLEAN || state == RH_DIRTY;
350 static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
354 while ((bio = bio_list_pop(bio_list))) {
355 queue_bio(ms, bio, WRITE);
359 static void complete_resync_work(struct region *reg, int success)
361 struct region_hash *rh = reg->rh;
363 rh->log->type->set_region_sync(rh->log, reg->key, success);
364 dispatch_bios(rh->ms, ®->delayed_bios);
365 if (atomic_dec_and_test(&rh->recovery_in_flight))
366 wake_up_all(&_kmirrord_recovery_stopped);
367 up(&rh->recovery_count);
370 static void rh_update_states(struct region_hash *rh)
372 struct region *reg, *next;
375 LIST_HEAD(recovered);
376 LIST_HEAD(failed_recovered);
379 * Quickly grab the lists.
381 write_lock_irq(&rh->hash_lock);
382 spin_lock(&rh->region_lock);
383 if (!list_empty(&rh->clean_regions)) {
384 list_splice(&rh->clean_regions, &clean);
385 INIT_LIST_HEAD(&rh->clean_regions);
387 list_for_each_entry(reg, &clean, list)
388 list_del(®->hash_list);
391 if (!list_empty(&rh->recovered_regions)) {
392 list_splice(&rh->recovered_regions, &recovered);
393 INIT_LIST_HEAD(&rh->recovered_regions);
395 list_for_each_entry (reg, &recovered, list)
396 list_del(®->hash_list);
399 if (!list_empty(&rh->failed_recovered_regions)) {
400 list_splice(&rh->failed_recovered_regions, &failed_recovered);
401 INIT_LIST_HEAD(&rh->failed_recovered_regions);
403 list_for_each_entry(reg, &failed_recovered, list)
404 list_del(®->hash_list);
407 spin_unlock(&rh->region_lock);
408 write_unlock_irq(&rh->hash_lock);
411 * All the regions on the recovered and clean lists have
412 * now been pulled out of the system, so no need to do
415 list_for_each_entry_safe (reg, next, &recovered, list) {
416 rh->log->type->clear_region(rh->log, reg->key);
417 complete_resync_work(reg, 1);
418 mempool_free(reg, rh->region_pool);
421 list_for_each_entry_safe(reg, next, &failed_recovered, list) {
422 complete_resync_work(reg, errors_handled(rh->ms) ? 0 : 1);
423 mempool_free(reg, rh->region_pool);
426 list_for_each_entry_safe(reg, next, &clean, list) {
427 rh->log->type->clear_region(rh->log, reg->key);
428 mempool_free(reg, rh->region_pool);
431 rh->log->type->flush(rh->log);
434 static void rh_inc(struct region_hash *rh, region_t region)
438 read_lock(&rh->hash_lock);
439 reg = __rh_find(rh, region);
441 spin_lock_irq(&rh->region_lock);
442 atomic_inc(®->pending);
444 if (reg->state == RH_CLEAN) {
445 reg->state = RH_DIRTY;
446 list_del_init(®->list); /* take off the clean list */
447 spin_unlock_irq(&rh->region_lock);
449 rh->log->type->mark_region(rh->log, reg->key);
451 spin_unlock_irq(&rh->region_lock);
454 read_unlock(&rh->hash_lock);
457 static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
461 for (bio = bios->head; bio; bio = bio->bi_next)
462 rh_inc(rh, bio_to_region(rh, bio));
465 static void rh_dec(struct region_hash *rh, region_t region)
471 read_lock(&rh->hash_lock);
472 reg = __rh_lookup(rh, region);
473 read_unlock(&rh->hash_lock);
475 spin_lock_irqsave(&rh->region_lock, flags);
476 if (atomic_dec_and_test(®->pending)) {
478 * There is no pending I/O for this region.
479 * We can move the region to corresponding list for next action.
480 * At this point, the region is not yet connected to any list.
482 * If the state is RH_NOSYNC, the region should be kept off
484 * The hash entry for RH_NOSYNC will remain in memory
485 * until the region is recovered or the map is reloaded.
488 /* do nothing for RH_NOSYNC */
489 if (reg->state == RH_RECOVERING) {
490 list_add_tail(®->list, &rh->quiesced_regions);
491 } else if (reg->state == RH_DIRTY) {
492 reg->state = RH_CLEAN;
493 list_add(®->list, &rh->clean_regions);
497 spin_unlock_irqrestore(&rh->region_lock, flags);
504 * Starts quiescing a region in preparation for recovery.
506 static int __rh_recovery_prepare(struct region_hash *rh)
513 * Ask the dirty log what's next.
515 r = rh->log->type->get_resync_work(rh->log, ®ion);
520 * Get this region, and start it quiescing by setting the
523 read_lock(&rh->hash_lock);
524 reg = __rh_find(rh, region);
525 read_unlock(&rh->hash_lock);
527 spin_lock_irq(&rh->region_lock);
528 reg->state = RH_RECOVERING;
530 /* Already quiesced ? */
531 if (atomic_read(®->pending))
532 list_del_init(®->list);
534 list_move(®->list, &rh->quiesced_regions);
536 spin_unlock_irq(&rh->region_lock);
541 static void rh_recovery_prepare(struct region_hash *rh)
543 /* Extra reference to avoid race with rh_stop_recovery */
544 atomic_inc(&rh->recovery_in_flight);
546 while (!down_trylock(&rh->recovery_count)) {
547 atomic_inc(&rh->recovery_in_flight);
548 if (__rh_recovery_prepare(rh) <= 0) {
549 atomic_dec(&rh->recovery_in_flight);
550 up(&rh->recovery_count);
555 /* Drop the extra reference */
556 if (atomic_dec_and_test(&rh->recovery_in_flight))
557 wake_up_all(&_kmirrord_recovery_stopped);
561 * Returns any quiesced regions.
563 static struct region *rh_recovery_start(struct region_hash *rh)
565 struct region *reg = NULL;
567 spin_lock_irq(&rh->region_lock);
568 if (!list_empty(&rh->quiesced_regions)) {
569 reg = list_entry(rh->quiesced_regions.next,
570 struct region, list);
571 list_del_init(®->list); /* remove from the quiesced list */
573 spin_unlock_irq(&rh->region_lock);
578 static void rh_recovery_end(struct region *reg, int success)
580 struct region_hash *rh = reg->rh;
582 spin_lock_irq(&rh->region_lock);
584 list_add(®->list, ®->rh->recovered_regions);
586 reg->state = RH_NOSYNC;
587 list_add(®->list, ®->rh->failed_recovered_regions);
589 spin_unlock_irq(&rh->region_lock);
594 static int rh_flush(struct region_hash *rh)
596 return rh->log->type->flush(rh->log);
599 static void rh_delay(struct region_hash *rh, struct bio *bio)
603 read_lock(&rh->hash_lock);
604 reg = __rh_find(rh, bio_to_region(rh, bio));
605 bio_list_add(®->delayed_bios, bio);
606 read_unlock(&rh->hash_lock);
609 static void rh_stop_recovery(struct region_hash *rh)
613 /* wait for any recovering regions */
614 for (i = 0; i < MAX_RECOVERY; i++)
615 down(&rh->recovery_count);
618 static void rh_start_recovery(struct region_hash *rh)
622 for (i = 0; i < MAX_RECOVERY; i++)
623 up(&rh->recovery_count);
629 * Every mirror should look like this one.
631 #define DEFAULT_MIRROR 0
634 * This is yucky. We squirrel the mirror_set struct away inside
635 * bi_next for write buffers. This is safe since the bh
636 * doesn't get submitted to the lower levels of block layer.
638 static struct mirror_set *bio_get_ms(struct bio *bio)
640 return (struct mirror_set *) bio->bi_next;
643 static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
645 bio->bi_next = (struct bio *) ms;
648 /*-----------------------------------------------------------------
651 * When a mirror is first activated we may find that some regions
652 * are in the no-sync state. We have to recover these by
653 * recopying from the default mirror to all the others.
654 *---------------------------------------------------------------*/
655 static void recovery_complete(int read_err, unsigned int write_err,
658 struct region *reg = (struct region *) context;
661 /* Read error means the failure of default mirror. */
662 DMERR_LIMIT("Unable to read primary mirror during recovery");
665 DMERR_LIMIT("Write error during recovery (error = 0x%x)",
668 rh_recovery_end(reg, !(read_err || write_err));
671 static int recover(struct mirror_set *ms, struct region *reg)
675 struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
677 unsigned long flags = 0;
679 /* fill in the source */
680 m = ms->default_mirror;
681 from.bdev = m->dev->bdev;
682 from.sector = m->offset + region_to_sector(reg->rh, reg->key);
683 if (reg->key == (ms->nr_regions - 1)) {
685 * The final region may be smaller than
688 from.count = ms->ti->len & (reg->rh->region_size - 1);
690 from.count = reg->rh->region_size;
692 from.count = reg->rh->region_size;
694 /* fill in the destinations */
695 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
696 if (&ms->mirror[i] == ms->default_mirror)
700 dest->bdev = m->dev->bdev;
701 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
702 dest->count = from.count;
707 set_bit(KCOPYD_IGNORE_ERROR, &flags);
708 r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
709 recovery_complete, reg);
714 static void do_recovery(struct mirror_set *ms)
718 struct dirty_log *log = ms->rh.log;
721 * Start quiescing some regions.
723 rh_recovery_prepare(&ms->rh);
726 * Copy any already quiesced regions.
728 while ((reg = rh_recovery_start(&ms->rh))) {
729 r = recover(ms, reg);
731 rh_recovery_end(reg, 0);
735 * Update the in sync flag.
738 (log->type->get_sync_count(log) == ms->nr_regions)) {
739 /* the sync is complete */
740 dm_table_event(ms->ti->table);
745 /*-----------------------------------------------------------------
747 *---------------------------------------------------------------*/
748 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
750 /* FIXME: add read balancing */
751 return ms->default_mirror;
755 * remap a buffer to a particular mirror.
757 static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
759 bio->bi_bdev = m->dev->bdev;
760 bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
763 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
769 while ((bio = bio_list_pop(reads))) {
770 region = bio_to_region(&ms->rh, bio);
773 * We can only read balance if the region is in sync.
775 if (rh_in_sync(&ms->rh, region, 1))
776 m = choose_mirror(ms, bio->bi_sector);
778 m = ms->default_mirror;
781 generic_make_request(bio);
785 /*-----------------------------------------------------------------
788 * We do different things with the write io depending on the
789 * state of the region that it's in:
791 * SYNC: increment pending, use kcopyd to write to *all* mirrors
792 * RECOVERING: delay the io until recovery completes
793 * NOSYNC: increment pending, just write to the default mirror
794 *---------------------------------------------------------------*/
795 static void write_callback(unsigned long error, void *context)
799 struct bio *bio = (struct bio *) context;
800 struct mirror_set *ms;
802 ms = bio_get_ms(bio);
803 bio_set_ms(bio, NULL);
806 * NOTE: We don't decrement the pending count here,
807 * instead it is done by the targets endio function.
808 * This way we handle both writes to SYNC and NOSYNC
809 * regions with the same code.
814 * only error the io if all mirrors failed.
818 for (i = 0; i < ms->nr_mirrors; i++)
819 if (!test_bit(i, &error)) {
827 static void do_write(struct mirror_set *ms, struct bio *bio)
830 struct io_region io[KCOPYD_MAX_REGIONS+1];
832 struct dm_io_request io_req = {
834 .mem.type = DM_IO_BVEC,
835 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
836 .notify.fn = write_callback,
837 .notify.context = bio,
838 .client = ms->io_client,
841 for (i = 0; i < ms->nr_mirrors; i++) {
844 io[i].bdev = m->dev->bdev;
845 io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
846 io[i].count = bio->bi_size >> 9;
851 (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
854 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
858 struct bio_list sync, nosync, recover, *this_list = NULL;
864 * Classify each write.
866 bio_list_init(&sync);
867 bio_list_init(&nosync);
868 bio_list_init(&recover);
870 while ((bio = bio_list_pop(writes))) {
871 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
883 this_list = &recover;
887 bio_list_add(this_list, bio);
891 * Increment the pending counts for any regions that will
892 * be written to (writes to recover regions are going to
895 rh_inc_pending(&ms->rh, &sync);
896 rh_inc_pending(&ms->rh, &nosync);
897 ms->log_failure = rh_flush(&ms->rh) ? 1 : 0;
902 if (unlikely(ms->log_failure))
903 while ((bio = bio_list_pop(&sync)))
904 bio_endio(bio, -EIO);
905 else while ((bio = bio_list_pop(&sync)))
908 while ((bio = bio_list_pop(&recover)))
909 rh_delay(&ms->rh, bio);
911 while ((bio = bio_list_pop(&nosync))) {
912 map_bio(ms, ms->default_mirror, bio);
913 generic_make_request(bio);
917 /*-----------------------------------------------------------------
919 *---------------------------------------------------------------*/
920 static void do_mirror(struct work_struct *work)
922 struct mirror_set *ms =container_of(work, struct mirror_set,
924 struct bio_list reads, writes;
926 spin_lock(&ms->lock);
929 bio_list_init(&ms->reads);
930 bio_list_init(&ms->writes);
931 spin_unlock(&ms->lock);
933 rh_update_states(&ms->rh);
935 do_reads(ms, &reads);
936 do_writes(ms, &writes);
939 /*-----------------------------------------------------------------
941 *---------------------------------------------------------------*/
942 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
943 uint32_t region_size,
944 struct dm_target *ti,
945 struct dirty_log *dl)
948 struct mirror_set *ms = NULL;
950 if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
953 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
955 ms = kzalloc(len, GFP_KERNEL);
957 ti->error = "Cannot allocate mirror context";
961 spin_lock_init(&ms->lock);
964 ms->nr_mirrors = nr_mirrors;
965 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
967 ms->default_mirror = &ms->mirror[DEFAULT_MIRROR];
969 ms->io_client = dm_io_client_create(DM_IO_PAGES);
970 if (IS_ERR(ms->io_client)) {
971 ti->error = "Error creating dm_io client";
976 if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
977 ti->error = "Error creating dirty region hash";
978 dm_io_client_destroy(ms->io_client);
986 static void free_context(struct mirror_set *ms, struct dm_target *ti,
990 dm_put_device(ti, ms->mirror[m].dev);
992 dm_io_client_destroy(ms->io_client);
997 static inline int _check_region_size(struct dm_target *ti, uint32_t size)
999 return !(size % (PAGE_SIZE >> 9) || !is_power_of_2(size) ||
1003 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
1004 unsigned int mirror, char **argv)
1006 unsigned long long offset;
1008 if (sscanf(argv[1], "%llu", &offset) != 1) {
1009 ti->error = "Invalid offset";
1013 if (dm_get_device(ti, argv[0], offset, ti->len,
1014 dm_table_get_mode(ti->table),
1015 &ms->mirror[mirror].dev)) {
1016 ti->error = "Device lookup failure";
1020 ms->mirror[mirror].offset = offset;
1026 * Create dirty log: log_type #log_params <log_params>
1028 static struct dirty_log *create_dirty_log(struct dm_target *ti,
1029 unsigned int argc, char **argv,
1030 unsigned int *args_used)
1032 unsigned int param_count;
1033 struct dirty_log *dl;
1036 ti->error = "Insufficient mirror log arguments";
1040 if (sscanf(argv[1], "%u", ¶m_count) != 1) {
1041 ti->error = "Invalid mirror log argument count";
1045 *args_used = 2 + param_count;
1047 if (argc < *args_used) {
1048 ti->error = "Insufficient mirror log arguments";
1052 dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
1054 ti->error = "Error creating mirror dirty log";
1058 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
1059 ti->error = "Invalid region size";
1060 dm_destroy_dirty_log(dl);
1067 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1068 unsigned *args_used)
1070 unsigned num_features;
1071 struct dm_target *ti = ms->ti;
1078 if (sscanf(argv[0], "%u", &num_features) != 1) {
1079 ti->error = "Invalid number of features";
1087 if (num_features > argc) {
1088 ti->error = "Not enough arguments to support feature count";
1092 if (!strcmp("handle_errors", argv[0]))
1093 ms->features |= DM_RAID1_HANDLE_ERRORS;
1095 ti->error = "Unrecognised feature requested";
1105 * Construct a mirror mapping:
1107 * log_type #log_params <log_params>
1108 * #mirrors [mirror_path offset]{2,}
1109 * [#features <features>]
1111 * log_type is "core" or "disk"
1112 * #log_params is between 1 and 3
1114 * If present, features must be "handle_errors".
1116 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1119 unsigned int nr_mirrors, m, args_used;
1120 struct mirror_set *ms;
1121 struct dirty_log *dl;
1123 dl = create_dirty_log(ti, argc, argv, &args_used);
1130 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1131 nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
1132 ti->error = "Invalid number of mirrors";
1133 dm_destroy_dirty_log(dl);
1139 if (argc < nr_mirrors * 2) {
1140 ti->error = "Too few mirror arguments";
1141 dm_destroy_dirty_log(dl);
1145 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1147 dm_destroy_dirty_log(dl);
1151 /* Get the mirror parameter sets */
1152 for (m = 0; m < nr_mirrors; m++) {
1153 r = get_mirror(ms, ti, m, argv);
1155 free_context(ms, ti, m);
1163 ti->split_io = ms->rh.region_size;
1165 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1166 if (!ms->kmirrord_wq) {
1167 DMERR("couldn't start kmirrord");
1169 goto err_free_context;
1171 INIT_WORK(&ms->kmirrord_work, do_mirror);
1173 r = parse_features(ms, argc, argv, &args_used);
1175 goto err_destroy_wq;
1181 * Any read-balancing addition depends on the
1182 * DM_RAID1_HANDLE_ERRORS flag being present.
1183 * This is because the decision to balance depends
1184 * on the sync state of a region. If the above
1185 * flag is not present, we ignore errors; and
1186 * the sync state may be inaccurate.
1190 ti->error = "Too many mirror arguments";
1192 goto err_destroy_wq;
1195 r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
1197 goto err_destroy_wq;
1203 destroy_workqueue(ms->kmirrord_wq);
1205 free_context(ms, ti, ms->nr_mirrors);
1209 static void mirror_dtr(struct dm_target *ti)
1211 struct mirror_set *ms = (struct mirror_set *) ti->private;
1213 flush_workqueue(ms->kmirrord_wq);
1214 kcopyd_client_destroy(ms->kcopyd_client);
1215 destroy_workqueue(ms->kmirrord_wq);
1216 free_context(ms, ti, ms->nr_mirrors);
1219 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1221 int should_wake = 0;
1222 struct bio_list *bl;
1224 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
1225 spin_lock(&ms->lock);
1226 should_wake = !(bl->head);
1227 bio_list_add(bl, bio);
1228 spin_unlock(&ms->lock);
1235 * Mirror mapping function
1237 static int mirror_map(struct dm_target *ti, struct bio *bio,
1238 union map_info *map_context)
1240 int r, rw = bio_rw(bio);
1242 struct mirror_set *ms = ti->private;
1244 map_context->ll = bio_to_region(&ms->rh, bio);
1247 queue_bio(ms, bio, rw);
1248 return DM_MAPIO_SUBMITTED;
1251 r = ms->rh.log->type->in_sync(ms->rh.log,
1252 bio_to_region(&ms->rh, bio), 0);
1253 if (r < 0 && r != -EWOULDBLOCK)
1256 if (r == -EWOULDBLOCK) /* FIXME: ugly */
1257 r = DM_MAPIO_SUBMITTED;
1260 * We don't want to fast track a recovery just for a read
1261 * ahead. So we just let it silently fail.
1262 * FIXME: get rid of this.
1264 if (!r && rw == READA)
1268 /* Pass this io over to the daemon */
1269 queue_bio(ms, bio, rw);
1270 return DM_MAPIO_SUBMITTED;
1273 m = choose_mirror(ms, bio->bi_sector);
1277 map_bio(ms, m, bio);
1278 return DM_MAPIO_REMAPPED;
1281 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1282 int error, union map_info *map_context)
1284 int rw = bio_rw(bio);
1285 struct mirror_set *ms = (struct mirror_set *) ti->private;
1286 region_t region = map_context->ll;
1289 * We need to dec pending if this was a write.
1292 rh_dec(&ms->rh, region);
1297 static void mirror_postsuspend(struct dm_target *ti)
1299 struct mirror_set *ms = (struct mirror_set *) ti->private;
1300 struct dirty_log *log = ms->rh.log;
1302 rh_stop_recovery(&ms->rh);
1304 /* Wait for all I/O we generated to complete */
1305 wait_event(_kmirrord_recovery_stopped,
1306 !atomic_read(&ms->rh.recovery_in_flight));
1308 if (log->type->suspend && log->type->suspend(log))
1309 /* FIXME: need better error handling */
1310 DMWARN("log suspend failed");
1313 static void mirror_resume(struct dm_target *ti)
1315 struct mirror_set *ms = (struct mirror_set *) ti->private;
1316 struct dirty_log *log = ms->rh.log;
1317 if (log->type->resume && log->type->resume(log))
1318 /* FIXME: need better error handling */
1319 DMWARN("log resume failed");
1320 rh_start_recovery(&ms->rh);
1323 static int mirror_status(struct dm_target *ti, status_type_t type,
1324 char *result, unsigned int maxlen)
1326 unsigned int m, sz = 0;
1327 struct mirror_set *ms = (struct mirror_set *) ti->private;
1330 case STATUSTYPE_INFO:
1331 DMEMIT("%d ", ms->nr_mirrors);
1332 for (m = 0; m < ms->nr_mirrors; m++)
1333 DMEMIT("%s ", ms->mirror[m].dev->name);
1335 DMEMIT("%llu/%llu 0 ",
1336 (unsigned long long)ms->rh.log->type->
1337 get_sync_count(ms->rh.log),
1338 (unsigned long long)ms->nr_regions);
1340 sz += ms->rh.log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
1344 case STATUSTYPE_TABLE:
1345 sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1347 DMEMIT("%d", ms->nr_mirrors);
1348 for (m = 0; m < ms->nr_mirrors; m++)
1349 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1350 (unsigned long long)ms->mirror[m].offset);
1352 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1353 DMEMIT(" 1 handle_errors");
1359 static struct target_type mirror_target = {
1361 .version = {1, 0, 3},
1362 .module = THIS_MODULE,
1366 .end_io = mirror_end_io,
1367 .postsuspend = mirror_postsuspend,
1368 .resume = mirror_resume,
1369 .status = mirror_status,
1372 static int __init dm_mirror_init(void)
1376 r = dm_dirty_log_init();
1380 r = dm_register_target(&mirror_target);
1382 DMERR("Failed to register mirror target");
1383 dm_dirty_log_exit();
1389 static void __exit dm_mirror_exit(void)
1393 r = dm_unregister_target(&mirror_target);
1395 DMERR("unregister failed %d", r);
1397 dm_dirty_log_exit();
1401 module_init(dm_mirror_init);
1402 module_exit(dm_mirror_exit);
1404 MODULE_DESCRIPTION(DM_NAME " mirror target");
1405 MODULE_AUTHOR("Joe Thornber");
1406 MODULE_LICENSE("GPL");