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 static struct workqueue_struct
*_kmirrord_wq
;
26 static struct work_struct _kmirrord_work
;
27 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped
);
29 static inline void wake(void)
31 queue_work(_kmirrord_wq
, &_kmirrord_work
);
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 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
;
102 struct region_hash
*rh
; /* FIXME: can we get rid of this ? */
106 struct list_head hash_list
;
107 struct list_head list
;
110 struct bio_list delayed_bios
;
114 /*-----------------------------------------------------------------
115 * Mirror set structures.
116 *---------------------------------------------------------------*/
118 atomic_t error_count
;
124 struct dm_target
*ti
;
125 struct list_head list
;
126 struct region_hash rh
;
127 struct kcopyd_client
*kcopyd_client
;
129 spinlock_t lock
; /* protects the next two lists */
130 struct bio_list reads
;
131 struct bio_list writes
;
137 struct mirror
*default_mirror
; /* Default mirror */
139 unsigned int nr_mirrors
;
140 struct mirror mirror
[0];
146 static inline region_t
bio_to_region(struct region_hash
*rh
, struct bio
*bio
)
148 return (bio
->bi_sector
- rh
->ms
->ti
->begin
) >> rh
->region_shift
;
151 static inline sector_t
region_to_sector(struct region_hash
*rh
, region_t region
)
153 return region
<< rh
->region_shift
;
156 /* FIXME move this */
157 static void queue_bio(struct mirror_set
*ms
, struct bio
*bio
, int rw
);
159 #define MIN_REGIONS 64
160 #define MAX_RECOVERY 1
161 static int rh_init(struct region_hash
*rh
, struct mirror_set
*ms
,
162 struct dirty_log
*log
, uint32_t region_size
,
165 unsigned int nr_buckets
, max_buckets
;
169 * Calculate a suitable number of buckets for our hash
172 max_buckets
= nr_regions
>> 6;
173 for (nr_buckets
= 128u; nr_buckets
< max_buckets
; nr_buckets
<<= 1)
179 rh
->region_size
= region_size
;
180 rh
->region_shift
= ffs(region_size
) - 1;
181 rwlock_init(&rh
->hash_lock
);
182 rh
->mask
= nr_buckets
- 1;
183 rh
->nr_buckets
= nr_buckets
;
185 rh
->buckets
= vmalloc(nr_buckets
* sizeof(*rh
->buckets
));
187 DMERR("unable to allocate region hash memory");
191 for (i
= 0; i
< nr_buckets
; i
++)
192 INIT_LIST_HEAD(rh
->buckets
+ i
);
194 spin_lock_init(&rh
->region_lock
);
195 sema_init(&rh
->recovery_count
, 0);
196 atomic_set(&rh
->recovery_in_flight
, 0);
197 INIT_LIST_HEAD(&rh
->clean_regions
);
198 INIT_LIST_HEAD(&rh
->quiesced_regions
);
199 INIT_LIST_HEAD(&rh
->recovered_regions
);
201 rh
->region_pool
= mempool_create_kmalloc_pool(MIN_REGIONS
,
202 sizeof(struct region
));
203 if (!rh
->region_pool
) {
212 static void rh_exit(struct region_hash
*rh
)
215 struct region
*reg
, *nreg
;
217 BUG_ON(!list_empty(&rh
->quiesced_regions
));
218 for (h
= 0; h
< rh
->nr_buckets
; h
++) {
219 list_for_each_entry_safe(reg
, nreg
, rh
->buckets
+ h
, hash_list
) {
220 BUG_ON(atomic_read(®
->pending
));
221 mempool_free(reg
, rh
->region_pool
);
226 dm_destroy_dirty_log(rh
->log
);
228 mempool_destroy(rh
->region_pool
);
232 #define RH_HASH_MULT 2654435387U
234 static inline unsigned int rh_hash(struct region_hash
*rh
, region_t region
)
236 return (unsigned int) ((region
* RH_HASH_MULT
) >> 12) & rh
->mask
;
239 static struct region
*__rh_lookup(struct region_hash
*rh
, region_t region
)
243 list_for_each_entry (reg
, rh
->buckets
+ rh_hash(rh
, region
), hash_list
)
244 if (reg
->key
== region
)
250 static void __rh_insert(struct region_hash
*rh
, struct region
*reg
)
252 unsigned int h
= rh_hash(rh
, reg
->key
);
253 list_add(®
->hash_list
, rh
->buckets
+ h
);
256 static struct region
*__rh_alloc(struct region_hash
*rh
, region_t region
)
258 struct region
*reg
, *nreg
;
260 read_unlock(&rh
->hash_lock
);
261 nreg
= mempool_alloc(rh
->region_pool
, GFP_ATOMIC
);
263 nreg
= kmalloc(sizeof(struct region
), GFP_NOIO
);
264 nreg
->state
= rh
->log
->type
->in_sync(rh
->log
, region
, 1) ?
265 RH_CLEAN
: RH_NOSYNC
;
269 INIT_LIST_HEAD(&nreg
->list
);
271 atomic_set(&nreg
->pending
, 0);
272 bio_list_init(&nreg
->delayed_bios
);
273 write_lock_irq(&rh
->hash_lock
);
275 reg
= __rh_lookup(rh
, region
);
277 /* we lost the race */
278 mempool_free(nreg
, rh
->region_pool
);
281 __rh_insert(rh
, nreg
);
282 if (nreg
->state
== RH_CLEAN
) {
283 spin_lock(&rh
->region_lock
);
284 list_add(&nreg
->list
, &rh
->clean_regions
);
285 spin_unlock(&rh
->region_lock
);
289 write_unlock_irq(&rh
->hash_lock
);
290 read_lock(&rh
->hash_lock
);
295 static inline struct region
*__rh_find(struct region_hash
*rh
, region_t region
)
299 reg
= __rh_lookup(rh
, region
);
301 reg
= __rh_alloc(rh
, region
);
306 static int rh_state(struct region_hash
*rh
, region_t region
, int may_block
)
311 read_lock(&rh
->hash_lock
);
312 reg
= __rh_lookup(rh
, region
);
313 read_unlock(&rh
->hash_lock
);
319 * The region wasn't in the hash, so we fall back to the
322 r
= rh
->log
->type
->in_sync(rh
->log
, region
, may_block
);
325 * Any error from the dirty log (eg. -EWOULDBLOCK) gets
326 * taken as a RH_NOSYNC
328 return r
== 1 ? RH_CLEAN
: RH_NOSYNC
;
331 static inline int rh_in_sync(struct region_hash
*rh
,
332 region_t region
, int may_block
)
334 int state
= rh_state(rh
, region
, may_block
);
335 return state
== RH_CLEAN
|| state
== RH_DIRTY
;
338 static void dispatch_bios(struct mirror_set
*ms
, struct bio_list
*bio_list
)
342 while ((bio
= bio_list_pop(bio_list
))) {
343 queue_bio(ms
, bio
, WRITE
);
347 static void complete_resync_work(struct region
*reg
, int success
)
349 struct region_hash
*rh
= reg
->rh
;
351 rh
->log
->type
->set_region_sync(rh
->log
, reg
->key
, success
);
352 dispatch_bios(rh
->ms
, ®
->delayed_bios
);
353 if (atomic_dec_and_test(&rh
->recovery_in_flight
))
354 wake_up_all(&_kmirrord_recovery_stopped
);
355 up(&rh
->recovery_count
);
358 static void rh_update_states(struct region_hash
*rh
)
360 struct region
*reg
, *next
;
363 LIST_HEAD(recovered
);
366 * Quickly grab the lists.
368 write_lock_irq(&rh
->hash_lock
);
369 spin_lock(&rh
->region_lock
);
370 if (!list_empty(&rh
->clean_regions
)) {
371 list_splice(&rh
->clean_regions
, &clean
);
372 INIT_LIST_HEAD(&rh
->clean_regions
);
374 list_for_each_entry (reg
, &clean
, list
) {
375 rh
->log
->type
->clear_region(rh
->log
, reg
->key
);
376 list_del(®
->hash_list
);
380 if (!list_empty(&rh
->recovered_regions
)) {
381 list_splice(&rh
->recovered_regions
, &recovered
);
382 INIT_LIST_HEAD(&rh
->recovered_regions
);
384 list_for_each_entry (reg
, &recovered
, list
)
385 list_del(®
->hash_list
);
387 spin_unlock(&rh
->region_lock
);
388 write_unlock_irq(&rh
->hash_lock
);
391 * All the regions on the recovered and clean lists have
392 * now been pulled out of the system, so no need to do
395 list_for_each_entry_safe (reg
, next
, &recovered
, list
) {
396 rh
->log
->type
->clear_region(rh
->log
, reg
->key
);
397 complete_resync_work(reg
, 1);
398 mempool_free(reg
, rh
->region_pool
);
401 if (!list_empty(&recovered
))
402 rh
->log
->type
->flush(rh
->log
);
404 list_for_each_entry_safe (reg
, next
, &clean
, list
)
405 mempool_free(reg
, rh
->region_pool
);
408 static void rh_inc(struct region_hash
*rh
, region_t region
)
412 read_lock(&rh
->hash_lock
);
413 reg
= __rh_find(rh
, region
);
415 spin_lock_irq(&rh
->region_lock
);
416 atomic_inc(®
->pending
);
418 if (reg
->state
== RH_CLEAN
) {
419 reg
->state
= RH_DIRTY
;
420 list_del_init(®
->list
); /* take off the clean list */
421 spin_unlock_irq(&rh
->region_lock
);
423 rh
->log
->type
->mark_region(rh
->log
, reg
->key
);
425 spin_unlock_irq(&rh
->region_lock
);
428 read_unlock(&rh
->hash_lock
);
431 static void rh_inc_pending(struct region_hash
*rh
, struct bio_list
*bios
)
435 for (bio
= bios
->head
; bio
; bio
= bio
->bi_next
)
436 rh_inc(rh
, bio_to_region(rh
, bio
));
439 static void rh_dec(struct region_hash
*rh
, region_t region
)
445 read_lock(&rh
->hash_lock
);
446 reg
= __rh_lookup(rh
, region
);
447 read_unlock(&rh
->hash_lock
);
449 spin_lock_irqsave(&rh
->region_lock
, flags
);
450 if (atomic_dec_and_test(®
->pending
)) {
452 * There is no pending I/O for this region.
453 * We can move the region to corresponding list for next action.
454 * At this point, the region is not yet connected to any list.
456 * If the state is RH_NOSYNC, the region should be kept off
458 * The hash entry for RH_NOSYNC will remain in memory
459 * until the region is recovered or the map is reloaded.
462 /* do nothing for RH_NOSYNC */
463 if (reg
->state
== RH_RECOVERING
) {
464 list_add_tail(®
->list
, &rh
->quiesced_regions
);
465 } else if (reg
->state
== RH_DIRTY
) {
466 reg
->state
= RH_CLEAN
;
467 list_add(®
->list
, &rh
->clean_regions
);
471 spin_unlock_irqrestore(&rh
->region_lock
, flags
);
478 * Starts quiescing a region in preparation for recovery.
480 static int __rh_recovery_prepare(struct region_hash
*rh
)
487 * Ask the dirty log what's next.
489 r
= rh
->log
->type
->get_resync_work(rh
->log
, ®ion
);
494 * Get this region, and start it quiescing by setting the
497 read_lock(&rh
->hash_lock
);
498 reg
= __rh_find(rh
, region
);
499 read_unlock(&rh
->hash_lock
);
501 spin_lock_irq(&rh
->region_lock
);
502 reg
->state
= RH_RECOVERING
;
504 /* Already quiesced ? */
505 if (atomic_read(®
->pending
))
506 list_del_init(®
->list
);
508 list_move(®
->list
, &rh
->quiesced_regions
);
510 spin_unlock_irq(&rh
->region_lock
);
515 static void rh_recovery_prepare(struct region_hash
*rh
)
517 /* Extra reference to avoid race with rh_stop_recovery */
518 atomic_inc(&rh
->recovery_in_flight
);
520 while (!down_trylock(&rh
->recovery_count
)) {
521 atomic_inc(&rh
->recovery_in_flight
);
522 if (__rh_recovery_prepare(rh
) <= 0) {
523 atomic_dec(&rh
->recovery_in_flight
);
524 up(&rh
->recovery_count
);
529 /* Drop the extra reference */
530 if (atomic_dec_and_test(&rh
->recovery_in_flight
))
531 wake_up_all(&_kmirrord_recovery_stopped
);
535 * Returns any quiesced regions.
537 static struct region
*rh_recovery_start(struct region_hash
*rh
)
539 struct region
*reg
= NULL
;
541 spin_lock_irq(&rh
->region_lock
);
542 if (!list_empty(&rh
->quiesced_regions
)) {
543 reg
= list_entry(rh
->quiesced_regions
.next
,
544 struct region
, list
);
545 list_del_init(®
->list
); /* remove from the quiesced list */
547 spin_unlock_irq(&rh
->region_lock
);
552 /* FIXME: success ignored for now */
553 static void rh_recovery_end(struct region
*reg
, int success
)
555 struct region_hash
*rh
= reg
->rh
;
557 spin_lock_irq(&rh
->region_lock
);
558 list_add(®
->list
, ®
->rh
->recovered_regions
);
559 spin_unlock_irq(&rh
->region_lock
);
564 static void rh_flush(struct region_hash
*rh
)
566 rh
->log
->type
->flush(rh
->log
);
569 static void rh_delay(struct region_hash
*rh
, struct bio
*bio
)
573 read_lock(&rh
->hash_lock
);
574 reg
= __rh_find(rh
, bio_to_region(rh
, bio
));
575 bio_list_add(®
->delayed_bios
, bio
);
576 read_unlock(&rh
->hash_lock
);
579 static void rh_stop_recovery(struct region_hash
*rh
)
583 /* wait for any recovering regions */
584 for (i
= 0; i
< MAX_RECOVERY
; i
++)
585 down(&rh
->recovery_count
);
588 static void rh_start_recovery(struct region_hash
*rh
)
592 for (i
= 0; i
< MAX_RECOVERY
; i
++)
593 up(&rh
->recovery_count
);
599 * Every mirror should look like this one.
601 #define DEFAULT_MIRROR 0
604 * This is yucky. We squirrel the mirror_set struct away inside
605 * bi_next for write buffers. This is safe since the bh
606 * doesn't get submitted to the lower levels of block layer.
608 static struct mirror_set
*bio_get_ms(struct bio
*bio
)
610 return (struct mirror_set
*) bio
->bi_next
;
613 static void bio_set_ms(struct bio
*bio
, struct mirror_set
*ms
)
615 bio
->bi_next
= (struct bio
*) ms
;
618 /*-----------------------------------------------------------------
621 * When a mirror is first activated we may find that some regions
622 * are in the no-sync state. We have to recover these by
623 * recopying from the default mirror to all the others.
624 *---------------------------------------------------------------*/
625 static void recovery_complete(int read_err
, unsigned int write_err
,
628 struct region
*reg
= (struct region
*) context
;
630 /* FIXME: better error handling */
631 rh_recovery_end(reg
, !(read_err
|| write_err
));
634 static int recover(struct mirror_set
*ms
, struct region
*reg
)
638 struct io_region from
, to
[KCOPYD_MAX_REGIONS
], *dest
;
640 unsigned long flags
= 0;
642 /* fill in the source */
643 m
= ms
->default_mirror
;
644 from
.bdev
= m
->dev
->bdev
;
645 from
.sector
= m
->offset
+ region_to_sector(reg
->rh
, reg
->key
);
646 if (reg
->key
== (ms
->nr_regions
- 1)) {
648 * The final region may be smaller than
651 from
.count
= ms
->ti
->len
& (reg
->rh
->region_size
- 1);
653 from
.count
= reg
->rh
->region_size
;
655 from
.count
= reg
->rh
->region_size
;
657 /* fill in the destinations */
658 for (i
= 0, dest
= to
; i
< ms
->nr_mirrors
; i
++) {
659 if (&ms
->mirror
[i
] == ms
->default_mirror
)
663 dest
->bdev
= m
->dev
->bdev
;
664 dest
->sector
= m
->offset
+ region_to_sector(reg
->rh
, reg
->key
);
665 dest
->count
= from
.count
;
670 set_bit(KCOPYD_IGNORE_ERROR
, &flags
);
671 r
= kcopyd_copy(ms
->kcopyd_client
, &from
, ms
->nr_mirrors
- 1, to
, flags
,
672 recovery_complete
, reg
);
677 static void do_recovery(struct mirror_set
*ms
)
681 struct dirty_log
*log
= ms
->rh
.log
;
684 * Start quiescing some regions.
686 rh_recovery_prepare(&ms
->rh
);
689 * Copy any already quiesced regions.
691 while ((reg
= rh_recovery_start(&ms
->rh
))) {
692 r
= recover(ms
, reg
);
694 rh_recovery_end(reg
, 0);
698 * Update the in sync flag.
701 (log
->type
->get_sync_count(log
) == ms
->nr_regions
)) {
702 /* the sync is complete */
703 dm_table_event(ms
->ti
->table
);
708 /*-----------------------------------------------------------------
710 *---------------------------------------------------------------*/
711 static struct mirror
*choose_mirror(struct mirror_set
*ms
, sector_t sector
)
713 /* FIXME: add read balancing */
714 return ms
->default_mirror
;
718 * remap a buffer to a particular mirror.
720 static void map_bio(struct mirror_set
*ms
, struct mirror
*m
, struct bio
*bio
)
722 bio
->bi_bdev
= m
->dev
->bdev
;
723 bio
->bi_sector
= m
->offset
+ (bio
->bi_sector
- ms
->ti
->begin
);
726 static void do_reads(struct mirror_set
*ms
, struct bio_list
*reads
)
732 while ((bio
= bio_list_pop(reads
))) {
733 region
= bio_to_region(&ms
->rh
, bio
);
736 * We can only read balance if the region is in sync.
738 if (rh_in_sync(&ms
->rh
, region
, 0))
739 m
= choose_mirror(ms
, bio
->bi_sector
);
741 m
= ms
->default_mirror
;
744 generic_make_request(bio
);
748 /*-----------------------------------------------------------------
751 * We do different things with the write io depending on the
752 * state of the region that it's in:
754 * SYNC: increment pending, use kcopyd to write to *all* mirrors
755 * RECOVERING: delay the io until recovery completes
756 * NOSYNC: increment pending, just write to the default mirror
757 *---------------------------------------------------------------*/
758 static void write_callback(unsigned long error
, void *context
)
762 struct bio
*bio
= (struct bio
*) context
;
763 struct mirror_set
*ms
;
765 ms
= bio_get_ms(bio
);
766 bio_set_ms(bio
, NULL
);
769 * NOTE: We don't decrement the pending count here,
770 * instead it is done by the targets endio function.
771 * This way we handle both writes to SYNC and NOSYNC
772 * regions with the same code.
777 * only error the io if all mirrors failed.
781 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
782 if (!test_bit(i
, &error
)) {
787 bio_endio(bio
, bio
->bi_size
, 0);
790 static void do_write(struct mirror_set
*ms
, struct bio
*bio
)
793 struct io_region io
[KCOPYD_MAX_REGIONS
+1];
796 for (i
= 0; i
< ms
->nr_mirrors
; i
++) {
799 io
[i
].bdev
= m
->dev
->bdev
;
800 io
[i
].sector
= m
->offset
+ (bio
->bi_sector
- ms
->ti
->begin
);
801 io
[i
].count
= bio
->bi_size
>> 9;
805 dm_io_async_bvec(ms
->nr_mirrors
, io
, WRITE
,
806 bio
->bi_io_vec
+ bio
->bi_idx
,
807 write_callback
, bio
);
810 static void do_writes(struct mirror_set
*ms
, struct bio_list
*writes
)
814 struct bio_list sync
, nosync
, recover
, *this_list
= NULL
;
820 * Classify each write.
822 bio_list_init(&sync
);
823 bio_list_init(&nosync
);
824 bio_list_init(&recover
);
826 while ((bio
= bio_list_pop(writes
))) {
827 state
= rh_state(&ms
->rh
, bio_to_region(&ms
->rh
, bio
), 1);
839 this_list
= &recover
;
843 bio_list_add(this_list
, bio
);
847 * Increment the pending counts for any regions that will
848 * be written to (writes to recover regions are going to
851 rh_inc_pending(&ms
->rh
, &sync
);
852 rh_inc_pending(&ms
->rh
, &nosync
);
858 while ((bio
= bio_list_pop(&sync
)))
861 while ((bio
= bio_list_pop(&recover
)))
862 rh_delay(&ms
->rh
, bio
);
864 while ((bio
= bio_list_pop(&nosync
))) {
865 map_bio(ms
, ms
->default_mirror
, bio
);
866 generic_make_request(bio
);
870 /*-----------------------------------------------------------------
872 *---------------------------------------------------------------*/
873 static LIST_HEAD(_mirror_sets
);
874 static DECLARE_RWSEM(_mirror_sets_lock
);
876 static void do_mirror(struct mirror_set
*ms
)
878 struct bio_list reads
, writes
;
880 spin_lock(&ms
->lock
);
883 bio_list_init(&ms
->reads
);
884 bio_list_init(&ms
->writes
);
885 spin_unlock(&ms
->lock
);
887 rh_update_states(&ms
->rh
);
889 do_reads(ms
, &reads
);
890 do_writes(ms
, &writes
);
893 static void do_work(struct work_struct
*ignored
)
895 struct mirror_set
*ms
;
897 down_read(&_mirror_sets_lock
);
898 list_for_each_entry (ms
, &_mirror_sets
, list
)
900 up_read(&_mirror_sets_lock
);
903 /*-----------------------------------------------------------------
905 *---------------------------------------------------------------*/
906 static struct mirror_set
*alloc_context(unsigned int nr_mirrors
,
907 uint32_t region_size
,
908 struct dm_target
*ti
,
909 struct dirty_log
*dl
)
912 struct mirror_set
*ms
= NULL
;
914 if (array_too_big(sizeof(*ms
), sizeof(ms
->mirror
[0]), nr_mirrors
))
917 len
= sizeof(*ms
) + (sizeof(ms
->mirror
[0]) * nr_mirrors
);
919 ms
= kmalloc(len
, GFP_KERNEL
);
921 ti
->error
= "Cannot allocate mirror context";
926 spin_lock_init(&ms
->lock
);
929 ms
->nr_mirrors
= nr_mirrors
;
930 ms
->nr_regions
= dm_sector_div_up(ti
->len
, region_size
);
932 ms
->default_mirror
= &ms
->mirror
[DEFAULT_MIRROR
];
934 if (rh_init(&ms
->rh
, ms
, dl
, region_size
, ms
->nr_regions
)) {
935 ti
->error
= "Error creating dirty region hash";
943 static void free_context(struct mirror_set
*ms
, struct dm_target
*ti
,
947 dm_put_device(ti
, ms
->mirror
[m
].dev
);
953 static inline int _check_region_size(struct dm_target
*ti
, uint32_t size
)
955 return !(size
% (PAGE_SIZE
>> 9) || (size
& (size
- 1)) ||
959 static int get_mirror(struct mirror_set
*ms
, struct dm_target
*ti
,
960 unsigned int mirror
, char **argv
)
962 unsigned long long offset
;
964 if (sscanf(argv
[1], "%llu", &offset
) != 1) {
965 ti
->error
= "Invalid offset";
969 if (dm_get_device(ti
, argv
[0], offset
, ti
->len
,
970 dm_table_get_mode(ti
->table
),
971 &ms
->mirror
[mirror
].dev
)) {
972 ti
->error
= "Device lookup failure";
976 ms
->mirror
[mirror
].offset
= offset
;
981 static int add_mirror_set(struct mirror_set
*ms
)
983 down_write(&_mirror_sets_lock
);
984 list_add_tail(&ms
->list
, &_mirror_sets
);
985 up_write(&_mirror_sets_lock
);
991 static void del_mirror_set(struct mirror_set
*ms
)
993 down_write(&_mirror_sets_lock
);
995 up_write(&_mirror_sets_lock
);
999 * Create dirty log: log_type #log_params <log_params>
1001 static struct dirty_log
*create_dirty_log(struct dm_target
*ti
,
1002 unsigned int argc
, char **argv
,
1003 unsigned int *args_used
)
1005 unsigned int param_count
;
1006 struct dirty_log
*dl
;
1009 ti
->error
= "Insufficient mirror log arguments";
1013 if (sscanf(argv
[1], "%u", ¶m_count
) != 1) {
1014 ti
->error
= "Invalid mirror log argument count";
1018 *args_used
= 2 + param_count
;
1020 if (argc
< *args_used
) {
1021 ti
->error
= "Insufficient mirror log arguments";
1025 dl
= dm_create_dirty_log(argv
[0], ti
, param_count
, argv
+ 2);
1027 ti
->error
= "Error creating mirror dirty log";
1031 if (!_check_region_size(ti
, dl
->type
->get_region_size(dl
))) {
1032 ti
->error
= "Invalid region size";
1033 dm_destroy_dirty_log(dl
);
1041 * Construct a mirror mapping:
1043 * log_type #log_params <log_params>
1044 * #mirrors [mirror_path offset]{2,}
1046 * log_type is "core" or "disk"
1047 * #log_params is between 1 and 3
1049 #define DM_IO_PAGES 64
1050 static int mirror_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1053 unsigned int nr_mirrors
, m
, args_used
;
1054 struct mirror_set
*ms
;
1055 struct dirty_log
*dl
;
1057 dl
= create_dirty_log(ti
, argc
, argv
, &args_used
);
1064 if (!argc
|| sscanf(argv
[0], "%u", &nr_mirrors
) != 1 ||
1065 nr_mirrors
< 2 || nr_mirrors
> KCOPYD_MAX_REGIONS
+ 1) {
1066 ti
->error
= "Invalid number of mirrors";
1067 dm_destroy_dirty_log(dl
);
1073 if (argc
!= nr_mirrors
* 2) {
1074 ti
->error
= "Wrong number of mirror arguments";
1075 dm_destroy_dirty_log(dl
);
1079 ms
= alloc_context(nr_mirrors
, dl
->type
->get_region_size(dl
), ti
, dl
);
1081 dm_destroy_dirty_log(dl
);
1085 /* Get the mirror parameter sets */
1086 for (m
= 0; m
< nr_mirrors
; m
++) {
1087 r
= get_mirror(ms
, ti
, m
, argv
);
1089 free_context(ms
, ti
, m
);
1097 ti
->split_io
= ms
->rh
.region_size
;
1099 r
= kcopyd_client_create(DM_IO_PAGES
, &ms
->kcopyd_client
);
1101 free_context(ms
, ti
, ms
->nr_mirrors
);
1109 static void mirror_dtr(struct dm_target
*ti
)
1111 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1114 kcopyd_client_destroy(ms
->kcopyd_client
);
1115 free_context(ms
, ti
, ms
->nr_mirrors
);
1118 static void queue_bio(struct mirror_set
*ms
, struct bio
*bio
, int rw
)
1120 int should_wake
= 0;
1121 struct bio_list
*bl
;
1123 bl
= (rw
== WRITE
) ? &ms
->writes
: &ms
->reads
;
1124 spin_lock(&ms
->lock
);
1125 should_wake
= !(bl
->head
);
1126 bio_list_add(bl
, bio
);
1127 spin_unlock(&ms
->lock
);
1134 * Mirror mapping function
1136 static int mirror_map(struct dm_target
*ti
, struct bio
*bio
,
1137 union map_info
*map_context
)
1139 int r
, rw
= bio_rw(bio
);
1141 struct mirror_set
*ms
= ti
->private;
1143 map_context
->ll
= bio_to_region(&ms
->rh
, bio
);
1146 queue_bio(ms
, bio
, rw
);
1147 return DM_MAPIO_SUBMITTED
;
1150 r
= ms
->rh
.log
->type
->in_sync(ms
->rh
.log
,
1151 bio_to_region(&ms
->rh
, bio
), 0);
1152 if (r
< 0 && r
!= -EWOULDBLOCK
)
1155 if (r
== -EWOULDBLOCK
) /* FIXME: ugly */
1156 r
= DM_MAPIO_SUBMITTED
;
1159 * We don't want to fast track a recovery just for a read
1160 * ahead. So we just let it silently fail.
1161 * FIXME: get rid of this.
1163 if (!r
&& rw
== READA
)
1167 /* Pass this io over to the daemon */
1168 queue_bio(ms
, bio
, rw
);
1169 return DM_MAPIO_SUBMITTED
;
1172 m
= choose_mirror(ms
, bio
->bi_sector
);
1176 map_bio(ms
, m
, bio
);
1177 return DM_MAPIO_REMAPPED
;
1180 static int mirror_end_io(struct dm_target
*ti
, struct bio
*bio
,
1181 int error
, union map_info
*map_context
)
1183 int rw
= bio_rw(bio
);
1184 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1185 region_t region
= map_context
->ll
;
1188 * We need to dec pending if this was a write.
1191 rh_dec(&ms
->rh
, region
);
1196 static void mirror_postsuspend(struct dm_target
*ti
)
1198 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1199 struct dirty_log
*log
= ms
->rh
.log
;
1201 rh_stop_recovery(&ms
->rh
);
1203 /* Wait for all I/O we generated to complete */
1204 wait_event(_kmirrord_recovery_stopped
,
1205 !atomic_read(&ms
->rh
.recovery_in_flight
));
1207 if (log
->type
->suspend
&& log
->type
->suspend(log
))
1208 /* FIXME: need better error handling */
1209 DMWARN("log suspend failed");
1212 static void mirror_resume(struct dm_target
*ti
)
1214 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1215 struct dirty_log
*log
= ms
->rh
.log
;
1216 if (log
->type
->resume
&& log
->type
->resume(log
))
1217 /* FIXME: need better error handling */
1218 DMWARN("log resume failed");
1219 rh_start_recovery(&ms
->rh
);
1222 static int mirror_status(struct dm_target
*ti
, status_type_t type
,
1223 char *result
, unsigned int maxlen
)
1226 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1228 sz
= ms
->rh
.log
->type
->status(ms
->rh
.log
, type
, result
, maxlen
);
1231 case STATUSTYPE_INFO
:
1232 DMEMIT("%d ", ms
->nr_mirrors
);
1233 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1234 DMEMIT("%s ", ms
->mirror
[m
].dev
->name
);
1237 (unsigned long long)ms
->rh
.log
->type
->
1238 get_sync_count(ms
->rh
.log
),
1239 (unsigned long long)ms
->nr_regions
);
1242 case STATUSTYPE_TABLE
:
1243 DMEMIT("%d", ms
->nr_mirrors
);
1244 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1245 DMEMIT(" %s %llu", ms
->mirror
[m
].dev
->name
,
1246 (unsigned long long)ms
->mirror
[m
].offset
);
1252 static struct target_type mirror_target
= {
1254 .version
= {1, 0, 2},
1255 .module
= THIS_MODULE
,
1259 .end_io
= mirror_end_io
,
1260 .postsuspend
= mirror_postsuspend
,
1261 .resume
= mirror_resume
,
1262 .status
= mirror_status
,
1265 static int __init
dm_mirror_init(void)
1269 r
= dm_dirty_log_init();
1273 _kmirrord_wq
= create_singlethread_workqueue("kmirrord");
1274 if (!_kmirrord_wq
) {
1275 DMERR("couldn't start kmirrord");
1276 dm_dirty_log_exit();
1279 INIT_WORK(&_kmirrord_work
, do_work
);
1281 r
= dm_register_target(&mirror_target
);
1283 DMERR("%s: Failed to register mirror target",
1284 mirror_target
.name
);
1285 dm_dirty_log_exit();
1286 destroy_workqueue(_kmirrord_wq
);
1292 static void __exit
dm_mirror_exit(void)
1296 r
= dm_unregister_target(&mirror_target
);
1298 DMERR("%s: unregister failed %d", mirror_target
.name
, r
);
1300 destroy_workqueue(_kmirrord_wq
);
1301 dm_dirty_log_exit();
1305 module_init(dm_mirror_init
);
1306 module_exit(dm_mirror_exit
);
1308 MODULE_DESCRIPTION(DM_NAME
" mirror target");
1309 MODULE_AUTHOR("Joe Thornber");
1310 MODULE_LICENSE("GPL");