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>
23 #define DM_MSG_PREFIX "raid1"
25 #define DM_RAID1_HANDLE_ERRORS 0x01
27 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
29 /*-----------------------------------------------------------------
32 * The mirror splits itself up into discrete regions. Each
33 * region can be in one of three states: clean, dirty,
34 * nosync. There is no need to put clean regions in the hash.
36 * In addition to being present in the hash table a region _may_
37 * be present on one of three lists.
39 * clean_regions: Regions on this list have no io pending to
40 * them, they are in sync, we are no longer interested in them,
41 * they are dull. rh_update_states() will remove them from the
44 * quiesced_regions: These regions have been spun down, ready
45 * for recovery. rh_recovery_start() will remove regions from
46 * this list and hand them to kmirrord, which will schedule the
47 * recovery io with kcopyd.
49 * recovered_regions: Regions that kcopyd has successfully
50 * recovered. rh_update_states() will now schedule any delayed
51 * io, up the recovery_count, and remove the region from the
55 * A rw spin lock 'hash_lock' protects just the hash table,
56 * this is never held in write mode from interrupt context,
57 * which I believe means that we only have to disable irqs when
60 * An ordinary spin lock 'region_lock' that protects the three
61 * lists in the region_hash, with the 'state', 'list' and
62 * 'bhs_delayed' fields of the regions. This is used from irq
63 * context, so all other uses will have to suspend local irqs.
64 *---------------------------------------------------------------*/
67 struct mirror_set *ms;
69 unsigned region_shift;
71 /* holds persistent region state */
72 struct dirty_log *log;
76 mempool_t *region_pool;
78 unsigned int nr_buckets;
79 struct list_head *buckets;
81 spinlock_t region_lock;
82 atomic_t recovery_in_flight;
83 struct semaphore recovery_count;
84 struct list_head clean_regions;
85 struct list_head quiesced_regions;
86 struct list_head recovered_regions;
97 struct region_hash *rh; /* FIXME: can we get rid of this ? */
101 struct list_head hash_list;
102 struct list_head list;
105 struct bio_list delayed_bios;
109 /*-----------------------------------------------------------------
110 * Mirror set structures.
111 *---------------------------------------------------------------*/
113 atomic_t error_count;
119 struct dm_target *ti;
120 struct list_head list;
121 struct region_hash rh;
122 struct kcopyd_client *kcopyd_client;
125 spinlock_t lock; /* protects the next two lists */
126 struct bio_list reads;
127 struct bio_list writes;
133 struct mirror *default_mirror; /* Default mirror */
135 struct workqueue_struct *kmirrord_wq;
136 struct work_struct kmirrord_work;
138 unsigned int nr_mirrors;
139 struct mirror mirror[0];
145 static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
147 return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
150 static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
152 return region << rh->region_shift;
155 static void wake(struct mirror_set *ms)
157 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
160 /* FIXME move this */
161 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
163 #define MIN_REGIONS 64
164 #define MAX_RECOVERY 1
165 static int rh_init(struct region_hash *rh, struct mirror_set *ms,
166 struct dirty_log *log, uint32_t region_size,
169 unsigned int nr_buckets, max_buckets;
173 * Calculate a suitable number of buckets for our hash
176 max_buckets = nr_regions >> 6;
177 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
183 rh->region_size = region_size;
184 rh->region_shift = ffs(region_size) - 1;
185 rwlock_init(&rh->hash_lock);
186 rh->mask = nr_buckets - 1;
187 rh->nr_buckets = nr_buckets;
189 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
191 DMERR("unable to allocate region hash memory");
195 for (i = 0; i < nr_buckets; i++)
196 INIT_LIST_HEAD(rh->buckets + i);
198 spin_lock_init(&rh->region_lock);
199 sema_init(&rh->recovery_count, 0);
200 atomic_set(&rh->recovery_in_flight, 0);
201 INIT_LIST_HEAD(&rh->clean_regions);
202 INIT_LIST_HEAD(&rh->quiesced_regions);
203 INIT_LIST_HEAD(&rh->recovered_regions);
205 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
206 sizeof(struct region));
207 if (!rh->region_pool) {
216 static void rh_exit(struct region_hash *rh)
219 struct region *reg, *nreg;
221 BUG_ON(!list_empty(&rh->quiesced_regions));
222 for (h = 0; h < rh->nr_buckets; h++) {
223 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
224 BUG_ON(atomic_read(®->pending));
225 mempool_free(reg, rh->region_pool);
230 dm_destroy_dirty_log(rh->log);
232 mempool_destroy(rh->region_pool);
236 #define RH_HASH_MULT 2654435387U
238 static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
240 return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
243 static struct region *__rh_lookup(struct region_hash *rh, region_t region)
247 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
248 if (reg->key == region)
254 static void __rh_insert(struct region_hash *rh, struct region *reg)
256 unsigned int h = rh_hash(rh, reg->key);
257 list_add(®->hash_list, rh->buckets + h);
260 static struct region *__rh_alloc(struct region_hash *rh, region_t region)
262 struct region *reg, *nreg;
264 read_unlock(&rh->hash_lock);
265 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
267 nreg = kmalloc(sizeof(struct region), GFP_NOIO);
268 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
269 RH_CLEAN : RH_NOSYNC;
273 INIT_LIST_HEAD(&nreg->list);
275 atomic_set(&nreg->pending, 0);
276 bio_list_init(&nreg->delayed_bios);
277 write_lock_irq(&rh->hash_lock);
279 reg = __rh_lookup(rh, region);
281 /* we lost the race */
282 mempool_free(nreg, rh->region_pool);
285 __rh_insert(rh, nreg);
286 if (nreg->state == RH_CLEAN) {
287 spin_lock(&rh->region_lock);
288 list_add(&nreg->list, &rh->clean_regions);
289 spin_unlock(&rh->region_lock);
293 write_unlock_irq(&rh->hash_lock);
294 read_lock(&rh->hash_lock);
299 static inline struct region *__rh_find(struct region_hash *rh, region_t region)
303 reg = __rh_lookup(rh, region);
305 reg = __rh_alloc(rh, region);
310 static int rh_state(struct region_hash *rh, region_t region, int may_block)
315 read_lock(&rh->hash_lock);
316 reg = __rh_lookup(rh, region);
317 read_unlock(&rh->hash_lock);
323 * The region wasn't in the hash, so we fall back to the
326 r = rh->log->type->in_sync(rh->log, region, may_block);
329 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
330 * taken as a RH_NOSYNC
332 return r == 1 ? RH_CLEAN : RH_NOSYNC;
335 static inline int rh_in_sync(struct region_hash *rh,
336 region_t region, int may_block)
338 int state = rh_state(rh, region, may_block);
339 return state == RH_CLEAN || state == RH_DIRTY;
342 static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
346 while ((bio = bio_list_pop(bio_list))) {
347 queue_bio(ms, bio, WRITE);
351 static void complete_resync_work(struct region *reg, int success)
353 struct region_hash *rh = reg->rh;
355 rh->log->type->set_region_sync(rh->log, reg->key, success);
356 dispatch_bios(rh->ms, ®->delayed_bios);
357 if (atomic_dec_and_test(&rh->recovery_in_flight))
358 wake_up_all(&_kmirrord_recovery_stopped);
359 up(&rh->recovery_count);
362 static void rh_update_states(struct region_hash *rh)
364 struct region *reg, *next;
367 LIST_HEAD(recovered);
370 * Quickly grab the lists.
372 write_lock_irq(&rh->hash_lock);
373 spin_lock(&rh->region_lock);
374 if (!list_empty(&rh->clean_regions)) {
375 list_splice(&rh->clean_regions, &clean);
376 INIT_LIST_HEAD(&rh->clean_regions);
378 list_for_each_entry (reg, &clean, list) {
379 rh->log->type->clear_region(rh->log, reg->key);
380 list_del(®->hash_list);
384 if (!list_empty(&rh->recovered_regions)) {
385 list_splice(&rh->recovered_regions, &recovered);
386 INIT_LIST_HEAD(&rh->recovered_regions);
388 list_for_each_entry (reg, &recovered, list)
389 list_del(®->hash_list);
391 spin_unlock(&rh->region_lock);
392 write_unlock_irq(&rh->hash_lock);
395 * All the regions on the recovered and clean lists have
396 * now been pulled out of the system, so no need to do
399 list_for_each_entry_safe (reg, next, &recovered, list) {
400 rh->log->type->clear_region(rh->log, reg->key);
401 complete_resync_work(reg, 1);
402 mempool_free(reg, rh->region_pool);
405 if (!list_empty(&recovered))
406 rh->log->type->flush(rh->log);
408 list_for_each_entry_safe (reg, next, &clean, list)
409 mempool_free(reg, rh->region_pool);
412 static void rh_inc(struct region_hash *rh, region_t region)
416 read_lock(&rh->hash_lock);
417 reg = __rh_find(rh, region);
419 spin_lock_irq(&rh->region_lock);
420 atomic_inc(®->pending);
422 if (reg->state == RH_CLEAN) {
423 reg->state = RH_DIRTY;
424 list_del_init(®->list); /* take off the clean list */
425 spin_unlock_irq(&rh->region_lock);
427 rh->log->type->mark_region(rh->log, reg->key);
429 spin_unlock_irq(&rh->region_lock);
432 read_unlock(&rh->hash_lock);
435 static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
439 for (bio = bios->head; bio; bio = bio->bi_next)
440 rh_inc(rh, bio_to_region(rh, bio));
443 static void rh_dec(struct region_hash *rh, region_t region)
449 read_lock(&rh->hash_lock);
450 reg = __rh_lookup(rh, region);
451 read_unlock(&rh->hash_lock);
453 spin_lock_irqsave(&rh->region_lock, flags);
454 if (atomic_dec_and_test(®->pending)) {
456 * There is no pending I/O for this region.
457 * We can move the region to corresponding list for next action.
458 * At this point, the region is not yet connected to any list.
460 * If the state is RH_NOSYNC, the region should be kept off
462 * The hash entry for RH_NOSYNC will remain in memory
463 * until the region is recovered or the map is reloaded.
466 /* do nothing for RH_NOSYNC */
467 if (reg->state == RH_RECOVERING) {
468 list_add_tail(®->list, &rh->quiesced_regions);
469 } else if (reg->state == RH_DIRTY) {
470 reg->state = RH_CLEAN;
471 list_add(®->list, &rh->clean_regions);
475 spin_unlock_irqrestore(&rh->region_lock, flags);
482 * Starts quiescing a region in preparation for recovery.
484 static int __rh_recovery_prepare(struct region_hash *rh)
491 * Ask the dirty log what's next.
493 r = rh->log->type->get_resync_work(rh->log, ®ion);
498 * Get this region, and start it quiescing by setting the
501 read_lock(&rh->hash_lock);
502 reg = __rh_find(rh, region);
503 read_unlock(&rh->hash_lock);
505 spin_lock_irq(&rh->region_lock);
506 reg->state = RH_RECOVERING;
508 /* Already quiesced ? */
509 if (atomic_read(®->pending))
510 list_del_init(®->list);
512 list_move(®->list, &rh->quiesced_regions);
514 spin_unlock_irq(&rh->region_lock);
519 static void rh_recovery_prepare(struct region_hash *rh)
521 /* Extra reference to avoid race with rh_stop_recovery */
522 atomic_inc(&rh->recovery_in_flight);
524 while (!down_trylock(&rh->recovery_count)) {
525 atomic_inc(&rh->recovery_in_flight);
526 if (__rh_recovery_prepare(rh) <= 0) {
527 atomic_dec(&rh->recovery_in_flight);
528 up(&rh->recovery_count);
533 /* Drop the extra reference */
534 if (atomic_dec_and_test(&rh->recovery_in_flight))
535 wake_up_all(&_kmirrord_recovery_stopped);
539 * Returns any quiesced regions.
541 static struct region *rh_recovery_start(struct region_hash *rh)
543 struct region *reg = NULL;
545 spin_lock_irq(&rh->region_lock);
546 if (!list_empty(&rh->quiesced_regions)) {
547 reg = list_entry(rh->quiesced_regions.next,
548 struct region, list);
549 list_del_init(®->list); /* remove from the quiesced list */
551 spin_unlock_irq(&rh->region_lock);
556 /* FIXME: success ignored for now */
557 static void rh_recovery_end(struct region *reg, int success)
559 struct region_hash *rh = reg->rh;
561 spin_lock_irq(&rh->region_lock);
562 list_add(®->list, ®->rh->recovered_regions);
563 spin_unlock_irq(&rh->region_lock);
568 static void rh_flush(struct region_hash *rh)
570 rh->log->type->flush(rh->log);
573 static void rh_delay(struct region_hash *rh, struct bio *bio)
577 read_lock(&rh->hash_lock);
578 reg = __rh_find(rh, bio_to_region(rh, bio));
579 bio_list_add(®->delayed_bios, bio);
580 read_unlock(&rh->hash_lock);
583 static void rh_stop_recovery(struct region_hash *rh)
587 /* wait for any recovering regions */
588 for (i = 0; i < MAX_RECOVERY; i++)
589 down(&rh->recovery_count);
592 static void rh_start_recovery(struct region_hash *rh)
596 for (i = 0; i < MAX_RECOVERY; i++)
597 up(&rh->recovery_count);
603 * Every mirror should look like this one.
605 #define DEFAULT_MIRROR 0
608 * This is yucky. We squirrel the mirror_set struct away inside
609 * bi_next for write buffers. This is safe since the bh
610 * doesn't get submitted to the lower levels of block layer.
612 static struct mirror_set *bio_get_ms(struct bio *bio)
614 return (struct mirror_set *) bio->bi_next;
617 static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
619 bio->bi_next = (struct bio *) ms;
622 /*-----------------------------------------------------------------
625 * When a mirror is first activated we may find that some regions
626 * are in the no-sync state. We have to recover these by
627 * recopying from the default mirror to all the others.
628 *---------------------------------------------------------------*/
629 static void recovery_complete(int read_err, unsigned int write_err,
632 struct region *reg = (struct region *) context;
634 /* FIXME: better error handling */
635 rh_recovery_end(reg, !(read_err || write_err));
638 static int recover(struct mirror_set *ms, struct region *reg)
642 struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
644 unsigned long flags = 0;
646 /* fill in the source */
647 m = ms->default_mirror;
648 from.bdev = m->dev->bdev;
649 from.sector = m->offset + region_to_sector(reg->rh, reg->key);
650 if (reg->key == (ms->nr_regions - 1)) {
652 * The final region may be smaller than
655 from.count = ms->ti->len & (reg->rh->region_size - 1);
657 from.count = reg->rh->region_size;
659 from.count = reg->rh->region_size;
661 /* fill in the destinations */
662 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
663 if (&ms->mirror[i] == ms->default_mirror)
667 dest->bdev = m->dev->bdev;
668 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
669 dest->count = from.count;
674 set_bit(KCOPYD_IGNORE_ERROR, &flags);
675 r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
676 recovery_complete, reg);
681 static void do_recovery(struct mirror_set *ms)
685 struct dirty_log *log = ms->rh.log;
688 * Start quiescing some regions.
690 rh_recovery_prepare(&ms->rh);
693 * Copy any already quiesced regions.
695 while ((reg = rh_recovery_start(&ms->rh))) {
696 r = recover(ms, reg);
698 rh_recovery_end(reg, 0);
702 * Update the in sync flag.
705 (log->type->get_sync_count(log) == ms->nr_regions)) {
706 /* the sync is complete */
707 dm_table_event(ms->ti->table);
712 /*-----------------------------------------------------------------
714 *---------------------------------------------------------------*/
715 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
717 /* FIXME: add read balancing */
718 return ms->default_mirror;
722 * remap a buffer to a particular mirror.
724 static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
726 bio->bi_bdev = m->dev->bdev;
727 bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
730 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
736 while ((bio = bio_list_pop(reads))) {
737 region = bio_to_region(&ms->rh, bio);
740 * We can only read balance if the region is in sync.
742 if (rh_in_sync(&ms->rh, region, 0))
743 m = choose_mirror(ms, bio->bi_sector);
745 m = ms->default_mirror;
748 generic_make_request(bio);
752 /*-----------------------------------------------------------------
755 * We do different things with the write io depending on the
756 * state of the region that it's in:
758 * SYNC: increment pending, use kcopyd to write to *all* mirrors
759 * RECOVERING: delay the io until recovery completes
760 * NOSYNC: increment pending, just write to the default mirror
761 *---------------------------------------------------------------*/
762 static void write_callback(unsigned long error, void *context)
766 struct bio *bio = (struct bio *) context;
767 struct mirror_set *ms;
769 ms = bio_get_ms(bio);
770 bio_set_ms(bio, NULL);
773 * NOTE: We don't decrement the pending count here,
774 * instead it is done by the targets endio function.
775 * This way we handle both writes to SYNC and NOSYNC
776 * regions with the same code.
781 * only error the io if all mirrors failed.
785 for (i = 0; i < ms->nr_mirrors; i++)
786 if (!test_bit(i, &error)) {
791 bio_endio(bio, bio->bi_size, 0);
794 static void do_write(struct mirror_set *ms, struct bio *bio)
797 struct io_region io[KCOPYD_MAX_REGIONS+1];
800 for (i = 0; i < ms->nr_mirrors; i++) {
803 io[i].bdev = m->dev->bdev;
804 io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
805 io[i].count = bio->bi_size >> 9;
809 dm_io_async_bvec(ms->nr_mirrors, io, WRITE,
810 bio->bi_io_vec + bio->bi_idx,
811 write_callback, bio);
814 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
818 struct bio_list sync, nosync, recover, *this_list = NULL;
824 * Classify each write.
826 bio_list_init(&sync);
827 bio_list_init(&nosync);
828 bio_list_init(&recover);
830 while ((bio = bio_list_pop(writes))) {
831 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
843 this_list = &recover;
847 bio_list_add(this_list, bio);
851 * Increment the pending counts for any regions that will
852 * be written to (writes to recover regions are going to
855 rh_inc_pending(&ms->rh, &sync);
856 rh_inc_pending(&ms->rh, &nosync);
862 while ((bio = bio_list_pop(&sync)))
865 while ((bio = bio_list_pop(&recover)))
866 rh_delay(&ms->rh, bio);
868 while ((bio = bio_list_pop(&nosync))) {
869 map_bio(ms, ms->default_mirror, bio);
870 generic_make_request(bio);
874 /*-----------------------------------------------------------------
876 *---------------------------------------------------------------*/
877 static void do_mirror(struct work_struct *work)
879 struct mirror_set *ms =container_of(work, struct mirror_set,
881 struct bio_list reads, writes;
883 spin_lock(&ms->lock);
886 bio_list_init(&ms->reads);
887 bio_list_init(&ms->writes);
888 spin_unlock(&ms->lock);
890 rh_update_states(&ms->rh);
892 do_reads(ms, &reads);
893 do_writes(ms, &writes);
896 /*-----------------------------------------------------------------
898 *---------------------------------------------------------------*/
899 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
900 uint32_t region_size,
901 struct dm_target *ti,
902 struct dirty_log *dl)
905 struct mirror_set *ms = NULL;
907 if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
910 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
912 ms = kmalloc(len, GFP_KERNEL);
914 ti->error = "Cannot allocate mirror context";
919 spin_lock_init(&ms->lock);
922 ms->nr_mirrors = nr_mirrors;
923 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
925 ms->default_mirror = &ms->mirror[DEFAULT_MIRROR];
927 if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
928 ti->error = "Error creating dirty region hash";
936 static void free_context(struct mirror_set *ms, struct dm_target *ti,
940 dm_put_device(ti, ms->mirror[m].dev);
946 static inline int _check_region_size(struct dm_target *ti, uint32_t size)
948 return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) ||
952 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
953 unsigned int mirror, char **argv)
955 unsigned long long offset;
957 if (sscanf(argv[1], "%llu", &offset) != 1) {
958 ti->error = "Invalid offset";
962 if (dm_get_device(ti, argv[0], offset, ti->len,
963 dm_table_get_mode(ti->table),
964 &ms->mirror[mirror].dev)) {
965 ti->error = "Device lookup failure";
969 ms->mirror[mirror].offset = offset;
975 * Create dirty log: log_type #log_params <log_params>
977 static struct dirty_log *create_dirty_log(struct dm_target *ti,
978 unsigned int argc, char **argv,
979 unsigned int *args_used)
981 unsigned int param_count;
982 struct dirty_log *dl;
985 ti->error = "Insufficient mirror log arguments";
989 if (sscanf(argv[1], "%u", ¶m_count) != 1) {
990 ti->error = "Invalid mirror log argument count";
994 *args_used = 2 + param_count;
996 if (argc < *args_used) {
997 ti->error = "Insufficient mirror log arguments";
1001 dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
1003 ti->error = "Error creating mirror dirty log";
1007 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
1008 ti->error = "Invalid region size";
1009 dm_destroy_dirty_log(dl);
1016 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1017 unsigned *args_used)
1019 unsigned num_features;
1020 struct dm_target *ti = ms->ti;
1027 if (sscanf(argv[0], "%u", &num_features) != 1) {
1028 ti->error = "Invalid number of features";
1036 if (num_features > argc) {
1037 ti->error = "Not enough arguments to support feature count";
1041 if (!strcmp("handle_errors", argv[0]))
1042 ms->features |= DM_RAID1_HANDLE_ERRORS;
1044 ti->error = "Unrecognised feature requested";
1054 * Construct a mirror mapping:
1056 * log_type #log_params <log_params>
1057 * #mirrors [mirror_path offset]{2,}
1058 * [#features <features>]
1060 * log_type is "core" or "disk"
1061 * #log_params is between 1 and 3
1063 * If present, features must be "handle_errors".
1065 #define DM_IO_PAGES 64
1066 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1069 unsigned int nr_mirrors, m, args_used;
1070 struct mirror_set *ms;
1071 struct dirty_log *dl;
1073 dl = create_dirty_log(ti, argc, argv, &args_used);
1080 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1081 nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
1082 ti->error = "Invalid number of mirrors";
1083 dm_destroy_dirty_log(dl);
1089 if (argc < nr_mirrors * 2) {
1090 ti->error = "Too few mirror arguments";
1091 dm_destroy_dirty_log(dl);
1095 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1097 dm_destroy_dirty_log(dl);
1101 /* Get the mirror parameter sets */
1102 for (m = 0; m < nr_mirrors; m++) {
1103 r = get_mirror(ms, ti, m, argv);
1105 free_context(ms, ti, m);
1113 ti->split_io = ms->rh.region_size;
1115 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1116 if (!ms->kmirrord_wq) {
1117 DMERR("couldn't start kmirrord");
1118 free_context(ms, ti, m);
1121 INIT_WORK(&ms->kmirrord_work, do_mirror);
1123 r = parse_features(ms, argc, argv, &args_used);
1125 free_context(ms, ti, ms->nr_mirrors);
1133 ti->error = "Too many mirror arguments";
1134 free_context(ms, ti, ms->nr_mirrors);
1138 r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
1140 destroy_workqueue(ms->kmirrord_wq);
1141 free_context(ms, ti, ms->nr_mirrors);
1149 static void mirror_dtr(struct dm_target *ti)
1151 struct mirror_set *ms = (struct mirror_set *) ti->private;
1153 flush_workqueue(ms->kmirrord_wq);
1154 kcopyd_client_destroy(ms->kcopyd_client);
1155 destroy_workqueue(ms->kmirrord_wq);
1156 free_context(ms, ti, ms->nr_mirrors);
1159 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1161 int should_wake = 0;
1162 struct bio_list *bl;
1164 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
1165 spin_lock(&ms->lock);
1166 should_wake = !(bl->head);
1167 bio_list_add(bl, bio);
1168 spin_unlock(&ms->lock);
1175 * Mirror mapping function
1177 static int mirror_map(struct dm_target *ti, struct bio *bio,
1178 union map_info *map_context)
1180 int r, rw = bio_rw(bio);
1182 struct mirror_set *ms = ti->private;
1184 map_context->ll = bio_to_region(&ms->rh, bio);
1187 queue_bio(ms, bio, rw);
1188 return DM_MAPIO_SUBMITTED;
1191 r = ms->rh.log->type->in_sync(ms->rh.log,
1192 bio_to_region(&ms->rh, bio), 0);
1193 if (r < 0 && r != -EWOULDBLOCK)
1196 if (r == -EWOULDBLOCK) /* FIXME: ugly */
1197 r = DM_MAPIO_SUBMITTED;
1200 * We don't want to fast track a recovery just for a read
1201 * ahead. So we just let it silently fail.
1202 * FIXME: get rid of this.
1204 if (!r && rw == READA)
1208 /* Pass this io over to the daemon */
1209 queue_bio(ms, bio, rw);
1210 return DM_MAPIO_SUBMITTED;
1213 m = choose_mirror(ms, bio->bi_sector);
1217 map_bio(ms, m, bio);
1218 return DM_MAPIO_REMAPPED;
1221 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1222 int error, union map_info *map_context)
1224 int rw = bio_rw(bio);
1225 struct mirror_set *ms = (struct mirror_set *) ti->private;
1226 region_t region = map_context->ll;
1229 * We need to dec pending if this was a write.
1232 rh_dec(&ms->rh, region);
1237 static void mirror_postsuspend(struct dm_target *ti)
1239 struct mirror_set *ms = (struct mirror_set *) ti->private;
1240 struct dirty_log *log = ms->rh.log;
1242 rh_stop_recovery(&ms->rh);
1244 /* Wait for all I/O we generated to complete */
1245 wait_event(_kmirrord_recovery_stopped,
1246 !atomic_read(&ms->rh.recovery_in_flight));
1248 if (log->type->suspend && log->type->suspend(log))
1249 /* FIXME: need better error handling */
1250 DMWARN("log suspend failed");
1253 static void mirror_resume(struct dm_target *ti)
1255 struct mirror_set *ms = (struct mirror_set *) ti->private;
1256 struct dirty_log *log = ms->rh.log;
1257 if (log->type->resume && log->type->resume(log))
1258 /* FIXME: need better error handling */
1259 DMWARN("log resume failed");
1260 rh_start_recovery(&ms->rh);
1263 static int mirror_status(struct dm_target *ti, status_type_t type,
1264 char *result, unsigned int maxlen)
1266 unsigned int m, sz = 0;
1267 struct mirror_set *ms = (struct mirror_set *) ti->private;
1270 case STATUSTYPE_INFO:
1271 DMEMIT("%d ", ms->nr_mirrors);
1272 for (m = 0; m < ms->nr_mirrors; m++)
1273 DMEMIT("%s ", ms->mirror[m].dev->name);
1276 (unsigned long long)ms->rh.log->type->
1277 get_sync_count(ms->rh.log),
1278 (unsigned long long)ms->nr_regions);
1280 sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1284 case STATUSTYPE_TABLE:
1285 sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1287 DMEMIT("%d", ms->nr_mirrors);
1288 for (m = 0; m < ms->nr_mirrors; m++)
1289 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1290 (unsigned long long)ms->mirror[m].offset);
1292 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1293 DMEMIT(" 1 handle_errors");
1299 static struct target_type mirror_target = {
1301 .version = {1, 0, 3},
1302 .module = THIS_MODULE,
1306 .end_io = mirror_end_io,
1307 .postsuspend = mirror_postsuspend,
1308 .resume = mirror_resume,
1309 .status = mirror_status,
1312 static int __init dm_mirror_init(void)
1316 r = dm_dirty_log_init();
1320 r = dm_register_target(&mirror_target);
1322 DMERR("%s: Failed to register mirror target",
1323 mirror_target.name);
1324 dm_dirty_log_exit();
1330 static void __exit dm_mirror_exit(void)
1334 r = dm_unregister_target(&mirror_target);
1336 DMERR("%s: unregister failed %d", mirror_target.name, r);
1338 dm_dirty_log_exit();
1342 module_init(dm_mirror_init);
1343 module_exit(dm_mirror_exit);
1345 MODULE_DESCRIPTION(DM_NAME " mirror target");
1346 MODULE_AUTHOR("Joe Thornber");
1347 MODULE_LICENSE("GPL");