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"
24 #define DM_IO_PAGES 64
26 #define DM_RAID1_HANDLE_ERRORS 0x01
27 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
29 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
31 /*-----------------------------------------------------------------
34 * The mirror splits itself up into discrete regions. Each
35 * region can be in one of three states: clean, dirty,
36 * nosync. There is no need to put clean regions in the hash.
38 * In addition to being present in the hash table a region _may_
39 * be present on one of three lists.
41 * clean_regions: Regions on this list have no io pending to
42 * them, they are in sync, we are no longer interested in them,
43 * they are dull. rh_update_states() will remove them from the
46 * quiesced_regions: These regions have been spun down, ready
47 * for recovery. rh_recovery_start() will remove regions from
48 * this list and hand them to kmirrord, which will schedule the
49 * recovery io with kcopyd.
51 * recovered_regions: Regions that kcopyd has successfully
52 * recovered. rh_update_states() will now schedule any delayed
53 * io, up the recovery_count, and remove the region from the
57 * A rw spin lock 'hash_lock' protects just the hash table,
58 * this is never held in write mode from interrupt context,
59 * which I believe means that we only have to disable irqs when
62 * An ordinary spin lock 'region_lock' that protects the three
63 * lists in the region_hash, with the 'state', 'list' and
64 * 'bhs_delayed' fields of the regions. This is used from irq
65 * context, so all other uses will have to suspend local irqs.
66 *---------------------------------------------------------------*/
69 struct mirror_set *ms;
71 unsigned region_shift;
73 /* holds persistent region state */
74 struct dirty_log *log;
78 mempool_t *region_pool;
80 unsigned int nr_buckets;
81 struct list_head *buckets;
83 spinlock_t region_lock;
84 atomic_t recovery_in_flight;
85 struct semaphore recovery_count;
86 struct list_head clean_regions;
87 struct list_head quiesced_regions;
88 struct list_head recovered_regions;
89 struct list_head failed_recovered_regions;
100 struct region_hash *rh; /* FIXME: can we get rid of this ? */
104 struct list_head hash_list;
105 struct list_head list;
108 struct bio_list delayed_bios;
112 /*-----------------------------------------------------------------
113 * Mirror set structures.
114 *---------------------------------------------------------------*/
116 atomic_t error_count;
122 struct dm_target *ti;
123 struct list_head list;
124 struct region_hash rh;
125 struct kcopyd_client *kcopyd_client;
128 spinlock_t lock; /* protects the next two lists */
129 struct bio_list reads;
130 struct bio_list writes;
132 struct dm_io_client *io_client;
138 struct mirror *default_mirror; /* Default mirror */
140 struct workqueue_struct *kmirrord_wq;
141 struct work_struct kmirrord_work;
143 unsigned int nr_mirrors;
144 struct mirror mirror[0];
150 static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
152 return (bio->bi_sector - rh->ms->ti->begin) >> rh->region_shift;
155 static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
157 return region << rh->region_shift;
160 static void wake(struct mirror_set *ms)
162 queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
165 /* FIXME move this */
166 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
168 #define MIN_REGIONS 64
169 #define MAX_RECOVERY 1
170 static int rh_init(struct region_hash *rh, struct mirror_set *ms,
171 struct dirty_log *log, uint32_t region_size,
174 unsigned int nr_buckets, max_buckets;
178 * Calculate a suitable number of buckets for our hash
181 max_buckets = nr_regions >> 6;
182 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
188 rh->region_size = region_size;
189 rh->region_shift = ffs(region_size) - 1;
190 rwlock_init(&rh->hash_lock);
191 rh->mask = nr_buckets - 1;
192 rh->nr_buckets = nr_buckets;
194 rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
196 DMERR("unable to allocate region hash memory");
200 for (i = 0; i < nr_buckets; i++)
201 INIT_LIST_HEAD(rh->buckets + i);
203 spin_lock_init(&rh->region_lock);
204 sema_init(&rh->recovery_count, 0);
205 atomic_set(&rh->recovery_in_flight, 0);
206 INIT_LIST_HEAD(&rh->clean_regions);
207 INIT_LIST_HEAD(&rh->quiesced_regions);
208 INIT_LIST_HEAD(&rh->recovered_regions);
209 INIT_LIST_HEAD(&rh->failed_recovered_regions);
211 rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
212 sizeof(struct region));
213 if (!rh->region_pool) {
222 static void rh_exit(struct region_hash *rh)
225 struct region *reg, *nreg;
227 BUG_ON(!list_empty(&rh->quiesced_regions));
228 for (h = 0; h < rh->nr_buckets; h++) {
229 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
230 BUG_ON(atomic_read(®->pending));
231 mempool_free(reg, rh->region_pool);
236 dm_destroy_dirty_log(rh->log);
238 mempool_destroy(rh->region_pool);
242 #define RH_HASH_MULT 2654435387U
244 static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
246 return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
249 static struct region *__rh_lookup(struct region_hash *rh, region_t region)
253 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
254 if (reg->key == region)
260 static void __rh_insert(struct region_hash *rh, struct region *reg)
262 unsigned int h = rh_hash(rh, reg->key);
263 list_add(®->hash_list, rh->buckets + h);
266 static struct region *__rh_alloc(struct region_hash *rh, region_t region)
268 struct region *reg, *nreg;
270 read_unlock(&rh->hash_lock);
271 nreg = mempool_alloc(rh->region_pool, GFP_ATOMIC);
273 nreg = kmalloc(sizeof(struct region), GFP_NOIO);
274 nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
275 RH_CLEAN : RH_NOSYNC;
279 INIT_LIST_HEAD(&nreg->list);
281 atomic_set(&nreg->pending, 0);
282 bio_list_init(&nreg->delayed_bios);
283 write_lock_irq(&rh->hash_lock);
285 reg = __rh_lookup(rh, region);
287 /* we lost the race */
288 mempool_free(nreg, rh->region_pool);
291 __rh_insert(rh, nreg);
292 if (nreg->state == RH_CLEAN) {
293 spin_lock(&rh->region_lock);
294 list_add(&nreg->list, &rh->clean_regions);
295 spin_unlock(&rh->region_lock);
299 write_unlock_irq(&rh->hash_lock);
300 read_lock(&rh->hash_lock);
305 static inline struct region *__rh_find(struct region_hash *rh, region_t region)
309 reg = __rh_lookup(rh, region);
311 reg = __rh_alloc(rh, region);
316 static int rh_state(struct region_hash *rh, region_t region, int may_block)
321 read_lock(&rh->hash_lock);
322 reg = __rh_lookup(rh, region);
323 read_unlock(&rh->hash_lock);
329 * The region wasn't in the hash, so we fall back to the
332 r = rh->log->type->in_sync(rh->log, region, may_block);
335 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
336 * taken as a RH_NOSYNC
338 return r == 1 ? RH_CLEAN : RH_NOSYNC;
341 static inline int rh_in_sync(struct region_hash *rh,
342 region_t region, int may_block)
344 int state = rh_state(rh, region, may_block);
345 return state == RH_CLEAN || state == RH_DIRTY;
348 static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
352 while ((bio = bio_list_pop(bio_list))) {
353 queue_bio(ms, bio, WRITE);
357 static void complete_resync_work(struct region *reg, int success)
359 struct region_hash *rh = reg->rh;
361 rh->log->type->set_region_sync(rh->log, reg->key, success);
362 dispatch_bios(rh->ms, ®->delayed_bios);
363 if (atomic_dec_and_test(&rh->recovery_in_flight))
364 wake_up_all(&_kmirrord_recovery_stopped);
365 up(&rh->recovery_count);
368 static void rh_update_states(struct region_hash *rh)
370 struct region *reg, *next;
373 LIST_HEAD(recovered);
374 LIST_HEAD(failed_recovered);
377 * Quickly grab the lists.
379 write_lock_irq(&rh->hash_lock);
380 spin_lock(&rh->region_lock);
381 if (!list_empty(&rh->clean_regions)) {
382 list_splice(&rh->clean_regions, &clean);
383 INIT_LIST_HEAD(&rh->clean_regions);
385 list_for_each_entry(reg, &clean, list)
386 list_del(®->hash_list);
389 if (!list_empty(&rh->recovered_regions)) {
390 list_splice(&rh->recovered_regions, &recovered);
391 INIT_LIST_HEAD(&rh->recovered_regions);
393 list_for_each_entry (reg, &recovered, list)
394 list_del(®->hash_list);
397 if (!list_empty(&rh->failed_recovered_regions)) {
398 list_splice(&rh->failed_recovered_regions, &failed_recovered);
399 INIT_LIST_HEAD(&rh->failed_recovered_regions);
401 list_for_each_entry(reg, &failed_recovered, list)
402 list_del(®->hash_list);
405 spin_unlock(&rh->region_lock);
406 write_unlock_irq(&rh->hash_lock);
409 * All the regions on the recovered and clean lists have
410 * now been pulled out of the system, so no need to do
413 list_for_each_entry_safe (reg, next, &recovered, list) {
414 rh->log->type->clear_region(rh->log, reg->key);
415 complete_resync_work(reg, 1);
416 mempool_free(reg, rh->region_pool);
419 list_for_each_entry_safe(reg, next, &failed_recovered, list) {
420 complete_resync_work(reg, errors_handled(rh->ms) ? 0 : 1);
421 mempool_free(reg, rh->region_pool);
424 list_for_each_entry_safe(reg, next, &clean, list) {
425 rh->log->type->clear_region(rh->log, reg->key);
426 mempool_free(reg, rh->region_pool);
429 rh->log->type->flush(rh->log);
432 static void rh_inc(struct region_hash *rh, region_t region)
436 read_lock(&rh->hash_lock);
437 reg = __rh_find(rh, region);
439 spin_lock_irq(&rh->region_lock);
440 atomic_inc(®->pending);
442 if (reg->state == RH_CLEAN) {
443 reg->state = RH_DIRTY;
444 list_del_init(®->list); /* take off the clean list */
445 spin_unlock_irq(&rh->region_lock);
447 rh->log->type->mark_region(rh->log, reg->key);
449 spin_unlock_irq(&rh->region_lock);
452 read_unlock(&rh->hash_lock);
455 static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
459 for (bio = bios->head; bio; bio = bio->bi_next)
460 rh_inc(rh, bio_to_region(rh, bio));
463 static void rh_dec(struct region_hash *rh, region_t region)
469 read_lock(&rh->hash_lock);
470 reg = __rh_lookup(rh, region);
471 read_unlock(&rh->hash_lock);
473 spin_lock_irqsave(&rh->region_lock, flags);
474 if (atomic_dec_and_test(®->pending)) {
476 * There is no pending I/O for this region.
477 * We can move the region to corresponding list for next action.
478 * At this point, the region is not yet connected to any list.
480 * If the state is RH_NOSYNC, the region should be kept off
482 * The hash entry for RH_NOSYNC will remain in memory
483 * until the region is recovered or the map is reloaded.
486 /* do nothing for RH_NOSYNC */
487 if (reg->state == RH_RECOVERING) {
488 list_add_tail(®->list, &rh->quiesced_regions);
489 } else if (reg->state == RH_DIRTY) {
490 reg->state = RH_CLEAN;
491 list_add(®->list, &rh->clean_regions);
495 spin_unlock_irqrestore(&rh->region_lock, flags);
502 * Starts quiescing a region in preparation for recovery.
504 static int __rh_recovery_prepare(struct region_hash *rh)
511 * Ask the dirty log what's next.
513 r = rh->log->type->get_resync_work(rh->log, ®ion);
518 * Get this region, and start it quiescing by setting the
521 read_lock(&rh->hash_lock);
522 reg = __rh_find(rh, region);
523 read_unlock(&rh->hash_lock);
525 spin_lock_irq(&rh->region_lock);
526 reg->state = RH_RECOVERING;
528 /* Already quiesced ? */
529 if (atomic_read(®->pending))
530 list_del_init(®->list);
532 list_move(®->list, &rh->quiesced_regions);
534 spin_unlock_irq(&rh->region_lock);
539 static void rh_recovery_prepare(struct region_hash *rh)
541 /* Extra reference to avoid race with rh_stop_recovery */
542 atomic_inc(&rh->recovery_in_flight);
544 while (!down_trylock(&rh->recovery_count)) {
545 atomic_inc(&rh->recovery_in_flight);
546 if (__rh_recovery_prepare(rh) <= 0) {
547 atomic_dec(&rh->recovery_in_flight);
548 up(&rh->recovery_count);
553 /* Drop the extra reference */
554 if (atomic_dec_and_test(&rh->recovery_in_flight))
555 wake_up_all(&_kmirrord_recovery_stopped);
559 * Returns any quiesced regions.
561 static struct region *rh_recovery_start(struct region_hash *rh)
563 struct region *reg = NULL;
565 spin_lock_irq(&rh->region_lock);
566 if (!list_empty(&rh->quiesced_regions)) {
567 reg = list_entry(rh->quiesced_regions.next,
568 struct region, list);
569 list_del_init(®->list); /* remove from the quiesced list */
571 spin_unlock_irq(&rh->region_lock);
576 static void rh_recovery_end(struct region *reg, int success)
578 struct region_hash *rh = reg->rh;
580 spin_lock_irq(&rh->region_lock);
582 list_add(®->list, ®->rh->recovered_regions);
584 reg->state = RH_NOSYNC;
585 list_add(®->list, ®->rh->failed_recovered_regions);
587 spin_unlock_irq(&rh->region_lock);
592 static void rh_flush(struct region_hash *rh)
594 rh->log->type->flush(rh->log);
597 static void rh_delay(struct region_hash *rh, struct bio *bio)
601 read_lock(&rh->hash_lock);
602 reg = __rh_find(rh, bio_to_region(rh, bio));
603 bio_list_add(®->delayed_bios, bio);
604 read_unlock(&rh->hash_lock);
607 static void rh_stop_recovery(struct region_hash *rh)
611 /* wait for any recovering regions */
612 for (i = 0; i < MAX_RECOVERY; i++)
613 down(&rh->recovery_count);
616 static void rh_start_recovery(struct region_hash *rh)
620 for (i = 0; i < MAX_RECOVERY; i++)
621 up(&rh->recovery_count);
627 * Every mirror should look like this one.
629 #define DEFAULT_MIRROR 0
632 * This is yucky. We squirrel the mirror_set struct away inside
633 * bi_next for write buffers. This is safe since the bh
634 * doesn't get submitted to the lower levels of block layer.
636 static struct mirror_set *bio_get_ms(struct bio *bio)
638 return (struct mirror_set *) bio->bi_next;
641 static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
643 bio->bi_next = (struct bio *) ms;
646 /*-----------------------------------------------------------------
649 * When a mirror is first activated we may find that some regions
650 * are in the no-sync state. We have to recover these by
651 * recopying from the default mirror to all the others.
652 *---------------------------------------------------------------*/
653 static void recovery_complete(int read_err, unsigned int write_err,
656 struct region *reg = (struct region *) context;
659 /* Read error means the failure of default mirror. */
660 DMERR_LIMIT("Unable to read primary mirror during recovery");
663 DMERR_LIMIT("Write error during recovery (error = 0x%x)",
666 rh_recovery_end(reg, !(read_err || write_err));
669 static int recover(struct mirror_set *ms, struct region *reg)
673 struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
675 unsigned long flags = 0;
677 /* fill in the source */
678 m = ms->default_mirror;
679 from.bdev = m->dev->bdev;
680 from.sector = m->offset + region_to_sector(reg->rh, reg->key);
681 if (reg->key == (ms->nr_regions - 1)) {
683 * The final region may be smaller than
686 from.count = ms->ti->len & (reg->rh->region_size - 1);
688 from.count = reg->rh->region_size;
690 from.count = reg->rh->region_size;
692 /* fill in the destinations */
693 for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
694 if (&ms->mirror[i] == ms->default_mirror)
698 dest->bdev = m->dev->bdev;
699 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
700 dest->count = from.count;
705 set_bit(KCOPYD_IGNORE_ERROR, &flags);
706 r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
707 recovery_complete, reg);
712 static void do_recovery(struct mirror_set *ms)
716 struct dirty_log *log = ms->rh.log;
719 * Start quiescing some regions.
721 rh_recovery_prepare(&ms->rh);
724 * Copy any already quiesced regions.
726 while ((reg = rh_recovery_start(&ms->rh))) {
727 r = recover(ms, reg);
729 rh_recovery_end(reg, 0);
733 * Update the in sync flag.
736 (log->type->get_sync_count(log) == ms->nr_regions)) {
737 /* the sync is complete */
738 dm_table_event(ms->ti->table);
743 /*-----------------------------------------------------------------
745 *---------------------------------------------------------------*/
746 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
748 /* FIXME: add read balancing */
749 return ms->default_mirror;
753 * remap a buffer to a particular mirror.
755 static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
757 bio->bi_bdev = m->dev->bdev;
758 bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
761 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
767 while ((bio = bio_list_pop(reads))) {
768 region = bio_to_region(&ms->rh, bio);
771 * We can only read balance if the region is in sync.
773 if (rh_in_sync(&ms->rh, region, 1))
774 m = choose_mirror(ms, bio->bi_sector);
776 m = ms->default_mirror;
779 generic_make_request(bio);
783 /*-----------------------------------------------------------------
786 * We do different things with the write io depending on the
787 * state of the region that it's in:
789 * SYNC: increment pending, use kcopyd to write to *all* mirrors
790 * RECOVERING: delay the io until recovery completes
791 * NOSYNC: increment pending, just write to the default mirror
792 *---------------------------------------------------------------*/
793 static void write_callback(unsigned long error, void *context)
797 struct bio *bio = (struct bio *) context;
798 struct mirror_set *ms;
800 ms = bio_get_ms(bio);
801 bio_set_ms(bio, NULL);
804 * NOTE: We don't decrement the pending count here,
805 * instead it is done by the targets endio function.
806 * This way we handle both writes to SYNC and NOSYNC
807 * regions with the same code.
812 * only error the io if all mirrors failed.
816 for (i = 0; i < ms->nr_mirrors; i++)
817 if (!test_bit(i, &error)) {
822 bio_endio(bio, bio->bi_size, 0);
825 static void do_write(struct mirror_set *ms, struct bio *bio)
828 struct io_region io[KCOPYD_MAX_REGIONS+1];
830 struct dm_io_request io_req = {
832 .mem.type = DM_IO_BVEC,
833 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
834 .notify.fn = write_callback,
835 .notify.context = bio,
836 .client = ms->io_client,
839 for (i = 0; i < ms->nr_mirrors; i++) {
842 io[i].bdev = m->dev->bdev;
843 io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
844 io[i].count = bio->bi_size >> 9;
849 (void) dm_io(&io_req, ms->nr_mirrors, io, NULL);
852 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
856 struct bio_list sync, nosync, recover, *this_list = NULL;
862 * Classify each write.
864 bio_list_init(&sync);
865 bio_list_init(&nosync);
866 bio_list_init(&recover);
868 while ((bio = bio_list_pop(writes))) {
869 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
881 this_list = &recover;
885 bio_list_add(this_list, bio);
889 * Increment the pending counts for any regions that will
890 * be written to (writes to recover regions are going to
893 rh_inc_pending(&ms->rh, &sync);
894 rh_inc_pending(&ms->rh, &nosync);
900 while ((bio = bio_list_pop(&sync)))
903 while ((bio = bio_list_pop(&recover)))
904 rh_delay(&ms->rh, bio);
906 while ((bio = bio_list_pop(&nosync))) {
907 map_bio(ms, ms->default_mirror, bio);
908 generic_make_request(bio);
912 /*-----------------------------------------------------------------
914 *---------------------------------------------------------------*/
915 static void do_mirror(struct work_struct *work)
917 struct mirror_set *ms =container_of(work, struct mirror_set,
919 struct bio_list reads, writes;
921 spin_lock(&ms->lock);
924 bio_list_init(&ms->reads);
925 bio_list_init(&ms->writes);
926 spin_unlock(&ms->lock);
928 rh_update_states(&ms->rh);
930 do_reads(ms, &reads);
931 do_writes(ms, &writes);
934 /*-----------------------------------------------------------------
936 *---------------------------------------------------------------*/
937 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
938 uint32_t region_size,
939 struct dm_target *ti,
940 struct dirty_log *dl)
943 struct mirror_set *ms = NULL;
945 if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
948 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
950 ms = kmalloc(len, GFP_KERNEL);
952 ti->error = "Cannot allocate mirror context";
957 spin_lock_init(&ms->lock);
960 ms->nr_mirrors = nr_mirrors;
961 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
963 ms->default_mirror = &ms->mirror[DEFAULT_MIRROR];
965 ms->io_client = dm_io_client_create(DM_IO_PAGES);
966 if (IS_ERR(ms->io_client)) {
967 ti->error = "Error creating dm_io client";
972 if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
973 ti->error = "Error creating dirty region hash";
981 static void free_context(struct mirror_set *ms, struct dm_target *ti,
985 dm_put_device(ti, ms->mirror[m].dev);
987 dm_io_client_destroy(ms->io_client);
992 static inline int _check_region_size(struct dm_target *ti, uint32_t size)
994 return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) ||
998 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
999 unsigned int mirror, char **argv)
1001 unsigned long long offset;
1003 if (sscanf(argv[1], "%llu", &offset) != 1) {
1004 ti->error = "Invalid offset";
1008 if (dm_get_device(ti, argv[0], offset, ti->len,
1009 dm_table_get_mode(ti->table),
1010 &ms->mirror[mirror].dev)) {
1011 ti->error = "Device lookup failure";
1015 ms->mirror[mirror].offset = offset;
1021 * Create dirty log: log_type #log_params <log_params>
1023 static struct dirty_log *create_dirty_log(struct dm_target *ti,
1024 unsigned int argc, char **argv,
1025 unsigned int *args_used)
1027 unsigned int param_count;
1028 struct dirty_log *dl;
1031 ti->error = "Insufficient mirror log arguments";
1035 if (sscanf(argv[1], "%u", ¶m_count) != 1) {
1036 ti->error = "Invalid mirror log argument count";
1040 *args_used = 2 + param_count;
1042 if (argc < *args_used) {
1043 ti->error = "Insufficient mirror log arguments";
1047 dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
1049 ti->error = "Error creating mirror dirty log";
1053 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
1054 ti->error = "Invalid region size";
1055 dm_destroy_dirty_log(dl);
1062 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
1063 unsigned *args_used)
1065 unsigned num_features;
1066 struct dm_target *ti = ms->ti;
1073 if (sscanf(argv[0], "%u", &num_features) != 1) {
1074 ti->error = "Invalid number of features";
1082 if (num_features > argc) {
1083 ti->error = "Not enough arguments to support feature count";
1087 if (!strcmp("handle_errors", argv[0]))
1088 ms->features |= DM_RAID1_HANDLE_ERRORS;
1090 ti->error = "Unrecognised feature requested";
1100 * Construct a mirror mapping:
1102 * log_type #log_params <log_params>
1103 * #mirrors [mirror_path offset]{2,}
1104 * [#features <features>]
1106 * log_type is "core" or "disk"
1107 * #log_params is between 1 and 3
1109 * If present, features must be "handle_errors".
1111 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1114 unsigned int nr_mirrors, m, args_used;
1115 struct mirror_set *ms;
1116 struct dirty_log *dl;
1118 dl = create_dirty_log(ti, argc, argv, &args_used);
1125 if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1126 nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
1127 ti->error = "Invalid number of mirrors";
1128 dm_destroy_dirty_log(dl);
1134 if (argc < nr_mirrors * 2) {
1135 ti->error = "Too few mirror arguments";
1136 dm_destroy_dirty_log(dl);
1140 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1142 dm_destroy_dirty_log(dl);
1146 /* Get the mirror parameter sets */
1147 for (m = 0; m < nr_mirrors; m++) {
1148 r = get_mirror(ms, ti, m, argv);
1150 free_context(ms, ti, m);
1158 ti->split_io = ms->rh.region_size;
1160 ms->kmirrord_wq = create_singlethread_workqueue("kmirrord");
1161 if (!ms->kmirrord_wq) {
1162 DMERR("couldn't start kmirrord");
1163 free_context(ms, ti, m);
1166 INIT_WORK(&ms->kmirrord_work, do_mirror);
1168 r = parse_features(ms, argc, argv, &args_used);
1170 free_context(ms, ti, ms->nr_mirrors);
1178 * Any read-balancing addition depends on the
1179 * DM_RAID1_HANDLE_ERRORS flag being present.
1180 * This is because the decision to balance depends
1181 * on the sync state of a region. If the above
1182 * flag is not present, we ignore errors; and
1183 * the sync state may be inaccurate.
1187 ti->error = "Too many mirror arguments";
1188 free_context(ms, ti, ms->nr_mirrors);
1192 r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
1194 destroy_workqueue(ms->kmirrord_wq);
1195 free_context(ms, ti, ms->nr_mirrors);
1203 static void mirror_dtr(struct dm_target *ti)
1205 struct mirror_set *ms = (struct mirror_set *) ti->private;
1207 flush_workqueue(ms->kmirrord_wq);
1208 kcopyd_client_destroy(ms->kcopyd_client);
1209 destroy_workqueue(ms->kmirrord_wq);
1210 free_context(ms, ti, ms->nr_mirrors);
1213 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1215 int should_wake = 0;
1216 struct bio_list *bl;
1218 bl = (rw == WRITE) ? &ms->writes : &ms->reads;
1219 spin_lock(&ms->lock);
1220 should_wake = !(bl->head);
1221 bio_list_add(bl, bio);
1222 spin_unlock(&ms->lock);
1229 * Mirror mapping function
1231 static int mirror_map(struct dm_target *ti, struct bio *bio,
1232 union map_info *map_context)
1234 int r, rw = bio_rw(bio);
1236 struct mirror_set *ms = ti->private;
1238 map_context->ll = bio_to_region(&ms->rh, bio);
1241 queue_bio(ms, bio, rw);
1242 return DM_MAPIO_SUBMITTED;
1245 r = ms->rh.log->type->in_sync(ms->rh.log,
1246 bio_to_region(&ms->rh, bio), 0);
1247 if (r < 0 && r != -EWOULDBLOCK)
1250 if (r == -EWOULDBLOCK) /* FIXME: ugly */
1251 r = DM_MAPIO_SUBMITTED;
1254 * We don't want to fast track a recovery just for a read
1255 * ahead. So we just let it silently fail.
1256 * FIXME: get rid of this.
1258 if (!r && rw == READA)
1262 /* Pass this io over to the daemon */
1263 queue_bio(ms, bio, rw);
1264 return DM_MAPIO_SUBMITTED;
1267 m = choose_mirror(ms, bio->bi_sector);
1271 map_bio(ms, m, bio);
1272 return DM_MAPIO_REMAPPED;
1275 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1276 int error, union map_info *map_context)
1278 int rw = bio_rw(bio);
1279 struct mirror_set *ms = (struct mirror_set *) ti->private;
1280 region_t region = map_context->ll;
1283 * We need to dec pending if this was a write.
1286 rh_dec(&ms->rh, region);
1291 static void mirror_postsuspend(struct dm_target *ti)
1293 struct mirror_set *ms = (struct mirror_set *) ti->private;
1294 struct dirty_log *log = ms->rh.log;
1296 rh_stop_recovery(&ms->rh);
1298 /* Wait for all I/O we generated to complete */
1299 wait_event(_kmirrord_recovery_stopped,
1300 !atomic_read(&ms->rh.recovery_in_flight));
1302 if (log->type->suspend && log->type->suspend(log))
1303 /* FIXME: need better error handling */
1304 DMWARN("log suspend failed");
1307 static void mirror_resume(struct dm_target *ti)
1309 struct mirror_set *ms = (struct mirror_set *) ti->private;
1310 struct dirty_log *log = ms->rh.log;
1311 if (log->type->resume && log->type->resume(log))
1312 /* FIXME: need better error handling */
1313 DMWARN("log resume failed");
1314 rh_start_recovery(&ms->rh);
1317 static int mirror_status(struct dm_target *ti, status_type_t type,
1318 char *result, unsigned int maxlen)
1320 unsigned int m, sz = 0;
1321 struct mirror_set *ms = (struct mirror_set *) ti->private;
1324 case STATUSTYPE_INFO:
1325 DMEMIT("%d ", ms->nr_mirrors);
1326 for (m = 0; m < ms->nr_mirrors; m++)
1327 DMEMIT("%s ", ms->mirror[m].dev->name);
1329 DMEMIT("%llu/%llu 0 ",
1330 (unsigned long long)ms->rh.log->type->
1331 get_sync_count(ms->rh.log),
1332 (unsigned long long)ms->nr_regions);
1334 sz += ms->rh.log->type->status(ms->rh.log, type, result+sz, maxlen-sz);
1338 case STATUSTYPE_TABLE:
1339 sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1341 DMEMIT("%d", ms->nr_mirrors);
1342 for (m = 0; m < ms->nr_mirrors; m++)
1343 DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1344 (unsigned long long)ms->mirror[m].offset);
1346 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1347 DMEMIT(" 1 handle_errors");
1353 static struct target_type mirror_target = {
1355 .version = {1, 0, 3},
1356 .module = THIS_MODULE,
1360 .end_io = mirror_end_io,
1361 .postsuspend = mirror_postsuspend,
1362 .resume = mirror_resume,
1363 .status = mirror_status,
1366 static int __init dm_mirror_init(void)
1370 r = dm_dirty_log_init();
1374 r = dm_register_target(&mirror_target);
1376 DMERR("Failed to register mirror target");
1377 dm_dirty_log_exit();
1383 static void __exit dm_mirror_exit(void)
1387 r = dm_unregister_target(&mirror_target);
1389 DMERR("unregister failed %d", r);
1391 dm_dirty_log_exit();
1395 module_init(dm_mirror_init);
1396 module_exit(dm_mirror_exit);
1398 MODULE_DESCRIPTION(DM_NAME " mirror target");
1399 MODULE_AUTHOR("Joe Thornber");
1400 MODULE_LICENSE("GPL");