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 rh_update_states(struct region_hash
*rh
)
349 struct region
*reg
, *next
;
352 LIST_HEAD(recovered
);
355 * Quickly grab the lists.
357 write_lock_irq(&rh
->hash_lock
);
358 spin_lock(&rh
->region_lock
);
359 if (!list_empty(&rh
->clean_regions
)) {
360 list_splice(&rh
->clean_regions
, &clean
);
361 INIT_LIST_HEAD(&rh
->clean_regions
);
363 list_for_each_entry (reg
, &clean
, list
) {
364 rh
->log
->type
->clear_region(rh
->log
, reg
->key
);
365 list_del(®
->hash_list
);
369 if (!list_empty(&rh
->recovered_regions
)) {
370 list_splice(&rh
->recovered_regions
, &recovered
);
371 INIT_LIST_HEAD(&rh
->recovered_regions
);
373 list_for_each_entry (reg
, &recovered
, list
)
374 list_del(®
->hash_list
);
376 spin_unlock(&rh
->region_lock
);
377 write_unlock_irq(&rh
->hash_lock
);
380 * All the regions on the recovered and clean lists have
381 * now been pulled out of the system, so no need to do
384 list_for_each_entry_safe (reg
, next
, &recovered
, list
) {
385 rh
->log
->type
->clear_region(rh
->log
, reg
->key
);
386 rh
->log
->type
->complete_resync_work(rh
->log
, reg
->key
, 1);
387 dispatch_bios(rh
->ms
, ®
->delayed_bios
);
388 if (atomic_dec_and_test(&rh
->recovery_in_flight
))
389 wake_up_all(&_kmirrord_recovery_stopped
);
390 up(&rh
->recovery_count
);
391 mempool_free(reg
, rh
->region_pool
);
394 if (!list_empty(&recovered
))
395 rh
->log
->type
->flush(rh
->log
);
397 list_for_each_entry_safe (reg
, next
, &clean
, list
)
398 mempool_free(reg
, rh
->region_pool
);
401 static void rh_inc(struct region_hash
*rh
, region_t region
)
405 read_lock(&rh
->hash_lock
);
406 reg
= __rh_find(rh
, region
);
408 spin_lock_irq(&rh
->region_lock
);
409 atomic_inc(®
->pending
);
411 if (reg
->state
== RH_CLEAN
) {
412 reg
->state
= RH_DIRTY
;
413 list_del_init(®
->list
); /* take off the clean list */
414 spin_unlock_irq(&rh
->region_lock
);
416 rh
->log
->type
->mark_region(rh
->log
, reg
->key
);
418 spin_unlock_irq(&rh
->region_lock
);
421 read_unlock(&rh
->hash_lock
);
424 static void rh_inc_pending(struct region_hash
*rh
, struct bio_list
*bios
)
428 for (bio
= bios
->head
; bio
; bio
= bio
->bi_next
)
429 rh_inc(rh
, bio_to_region(rh
, bio
));
432 static void rh_dec(struct region_hash
*rh
, region_t region
)
438 read_lock(&rh
->hash_lock
);
439 reg
= __rh_lookup(rh
, region
);
440 read_unlock(&rh
->hash_lock
);
442 spin_lock_irqsave(&rh
->region_lock
, flags
);
443 if (atomic_dec_and_test(®
->pending
)) {
445 * There is no pending I/O for this region.
446 * We can move the region to corresponding list for next action.
447 * At this point, the region is not yet connected to any list.
449 * If the state is RH_NOSYNC, the region should be kept off
451 * The hash entry for RH_NOSYNC will remain in memory
452 * until the region is recovered or the map is reloaded.
455 /* do nothing for RH_NOSYNC */
456 if (reg
->state
== RH_RECOVERING
) {
457 list_add_tail(®
->list
, &rh
->quiesced_regions
);
458 } else if (reg
->state
== RH_DIRTY
) {
459 reg
->state
= RH_CLEAN
;
460 list_add(®
->list
, &rh
->clean_regions
);
464 spin_unlock_irqrestore(&rh
->region_lock
, flags
);
471 * Starts quiescing a region in preparation for recovery.
473 static int __rh_recovery_prepare(struct region_hash
*rh
)
480 * Ask the dirty log what's next.
482 r
= rh
->log
->type
->get_resync_work(rh
->log
, ®ion
);
487 * Get this region, and start it quiescing by setting the
490 read_lock(&rh
->hash_lock
);
491 reg
= __rh_find(rh
, region
);
492 read_unlock(&rh
->hash_lock
);
494 spin_lock_irq(&rh
->region_lock
);
495 reg
->state
= RH_RECOVERING
;
497 /* Already quiesced ? */
498 if (atomic_read(®
->pending
))
499 list_del_init(®
->list
);
501 list_move(®
->list
, &rh
->quiesced_regions
);
503 spin_unlock_irq(&rh
->region_lock
);
508 static void rh_recovery_prepare(struct region_hash
*rh
)
510 /* Extra reference to avoid race with rh_stop_recovery */
511 atomic_inc(&rh
->recovery_in_flight
);
513 while (!down_trylock(&rh
->recovery_count
)) {
514 atomic_inc(&rh
->recovery_in_flight
);
515 if (__rh_recovery_prepare(rh
) <= 0) {
516 atomic_dec(&rh
->recovery_in_flight
);
517 up(&rh
->recovery_count
);
522 /* Drop the extra reference */
523 if (atomic_dec_and_test(&rh
->recovery_in_flight
))
524 wake_up_all(&_kmirrord_recovery_stopped
);
528 * Returns any quiesced regions.
530 static struct region
*rh_recovery_start(struct region_hash
*rh
)
532 struct region
*reg
= NULL
;
534 spin_lock_irq(&rh
->region_lock
);
535 if (!list_empty(&rh
->quiesced_regions
)) {
536 reg
= list_entry(rh
->quiesced_regions
.next
,
537 struct region
, list
);
538 list_del_init(®
->list
); /* remove from the quiesced list */
540 spin_unlock_irq(&rh
->region_lock
);
545 /* FIXME: success ignored for now */
546 static void rh_recovery_end(struct region
*reg
, int success
)
548 struct region_hash
*rh
= reg
->rh
;
550 spin_lock_irq(&rh
->region_lock
);
551 list_add(®
->list
, ®
->rh
->recovered_regions
);
552 spin_unlock_irq(&rh
->region_lock
);
557 static void rh_flush(struct region_hash
*rh
)
559 rh
->log
->type
->flush(rh
->log
);
562 static void rh_delay(struct region_hash
*rh
, struct bio
*bio
)
566 read_lock(&rh
->hash_lock
);
567 reg
= __rh_find(rh
, bio_to_region(rh
, bio
));
568 bio_list_add(®
->delayed_bios
, bio
);
569 read_unlock(&rh
->hash_lock
);
572 static void rh_stop_recovery(struct region_hash
*rh
)
576 /* wait for any recovering regions */
577 for (i
= 0; i
< MAX_RECOVERY
; i
++)
578 down(&rh
->recovery_count
);
581 static void rh_start_recovery(struct region_hash
*rh
)
585 for (i
= 0; i
< MAX_RECOVERY
; i
++)
586 up(&rh
->recovery_count
);
592 * Every mirror should look like this one.
594 #define DEFAULT_MIRROR 0
597 * This is yucky. We squirrel the mirror_set struct away inside
598 * bi_next for write buffers. This is safe since the bh
599 * doesn't get submitted to the lower levels of block layer.
601 static struct mirror_set
*bio_get_ms(struct bio
*bio
)
603 return (struct mirror_set
*) bio
->bi_next
;
606 static void bio_set_ms(struct bio
*bio
, struct mirror_set
*ms
)
608 bio
->bi_next
= (struct bio
*) ms
;
611 /*-----------------------------------------------------------------
614 * When a mirror is first activated we may find that some regions
615 * are in the no-sync state. We have to recover these by
616 * recopying from the default mirror to all the others.
617 *---------------------------------------------------------------*/
618 static void recovery_complete(int read_err
, unsigned int write_err
,
621 struct region
*reg
= (struct region
*) context
;
623 /* FIXME: better error handling */
624 rh_recovery_end(reg
, !(read_err
|| write_err
));
627 static int recover(struct mirror_set
*ms
, struct region
*reg
)
631 struct io_region from
, to
[KCOPYD_MAX_REGIONS
], *dest
;
633 unsigned long flags
= 0;
635 /* fill in the source */
636 m
= ms
->default_mirror
;
637 from
.bdev
= m
->dev
->bdev
;
638 from
.sector
= m
->offset
+ region_to_sector(reg
->rh
, reg
->key
);
639 if (reg
->key
== (ms
->nr_regions
- 1)) {
641 * The final region may be smaller than
644 from
.count
= ms
->ti
->len
& (reg
->rh
->region_size
- 1);
646 from
.count
= reg
->rh
->region_size
;
648 from
.count
= reg
->rh
->region_size
;
650 /* fill in the destinations */
651 for (i
= 0, dest
= to
; i
< ms
->nr_mirrors
; i
++) {
652 if (&ms
->mirror
[i
] == ms
->default_mirror
)
656 dest
->bdev
= m
->dev
->bdev
;
657 dest
->sector
= m
->offset
+ region_to_sector(reg
->rh
, reg
->key
);
658 dest
->count
= from
.count
;
663 set_bit(KCOPYD_IGNORE_ERROR
, &flags
);
664 r
= kcopyd_copy(ms
->kcopyd_client
, &from
, ms
->nr_mirrors
- 1, to
, flags
,
665 recovery_complete
, reg
);
670 static void do_recovery(struct mirror_set
*ms
)
674 struct dirty_log
*log
= ms
->rh
.log
;
677 * Start quiescing some regions.
679 rh_recovery_prepare(&ms
->rh
);
682 * Copy any already quiesced regions.
684 while ((reg
= rh_recovery_start(&ms
->rh
))) {
685 r
= recover(ms
, reg
);
687 rh_recovery_end(reg
, 0);
691 * Update the in sync flag.
694 (log
->type
->get_sync_count(log
) == ms
->nr_regions
)) {
695 /* the sync is complete */
696 dm_table_event(ms
->ti
->table
);
701 /*-----------------------------------------------------------------
703 *---------------------------------------------------------------*/
704 static struct mirror
*choose_mirror(struct mirror_set
*ms
, sector_t sector
)
706 /* FIXME: add read balancing */
707 return ms
->default_mirror
;
711 * remap a buffer to a particular mirror.
713 static void map_bio(struct mirror_set
*ms
, struct mirror
*m
, struct bio
*bio
)
715 bio
->bi_bdev
= m
->dev
->bdev
;
716 bio
->bi_sector
= m
->offset
+ (bio
->bi_sector
- ms
->ti
->begin
);
719 static void do_reads(struct mirror_set
*ms
, struct bio_list
*reads
)
725 while ((bio
= bio_list_pop(reads
))) {
726 region
= bio_to_region(&ms
->rh
, bio
);
729 * We can only read balance if the region is in sync.
731 if (rh_in_sync(&ms
->rh
, region
, 0))
732 m
= choose_mirror(ms
, bio
->bi_sector
);
734 m
= ms
->default_mirror
;
737 generic_make_request(bio
);
741 /*-----------------------------------------------------------------
744 * We do different things with the write io depending on the
745 * state of the region that it's in:
747 * SYNC: increment pending, use kcopyd to write to *all* mirrors
748 * RECOVERING: delay the io until recovery completes
749 * NOSYNC: increment pending, just write to the default mirror
750 *---------------------------------------------------------------*/
751 static void write_callback(unsigned long error
, void *context
)
755 struct bio
*bio
= (struct bio
*) context
;
756 struct mirror_set
*ms
;
758 ms
= bio_get_ms(bio
);
759 bio_set_ms(bio
, NULL
);
762 * NOTE: We don't decrement the pending count here,
763 * instead it is done by the targets endio function.
764 * This way we handle both writes to SYNC and NOSYNC
765 * regions with the same code.
770 * only error the io if all mirrors failed.
774 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
775 if (!test_bit(i
, &error
)) {
780 bio_endio(bio
, bio
->bi_size
, 0);
783 static void do_write(struct mirror_set
*ms
, struct bio
*bio
)
786 struct io_region io
[KCOPYD_MAX_REGIONS
+1];
789 for (i
= 0; i
< ms
->nr_mirrors
; i
++) {
792 io
[i
].bdev
= m
->dev
->bdev
;
793 io
[i
].sector
= m
->offset
+ (bio
->bi_sector
- ms
->ti
->begin
);
794 io
[i
].count
= bio
->bi_size
>> 9;
798 dm_io_async_bvec(ms
->nr_mirrors
, io
, WRITE
,
799 bio
->bi_io_vec
+ bio
->bi_idx
,
800 write_callback
, bio
);
803 static void do_writes(struct mirror_set
*ms
, struct bio_list
*writes
)
807 struct bio_list sync
, nosync
, recover
, *this_list
= NULL
;
813 * Classify each write.
815 bio_list_init(&sync
);
816 bio_list_init(&nosync
);
817 bio_list_init(&recover
);
819 while ((bio
= bio_list_pop(writes
))) {
820 state
= rh_state(&ms
->rh
, bio_to_region(&ms
->rh
, bio
), 1);
832 this_list
= &recover
;
836 bio_list_add(this_list
, bio
);
840 * Increment the pending counts for any regions that will
841 * be written to (writes to recover regions are going to
844 rh_inc_pending(&ms
->rh
, &sync
);
845 rh_inc_pending(&ms
->rh
, &nosync
);
851 while ((bio
= bio_list_pop(&sync
)))
854 while ((bio
= bio_list_pop(&recover
)))
855 rh_delay(&ms
->rh
, bio
);
857 while ((bio
= bio_list_pop(&nosync
))) {
858 map_bio(ms
, ms
->default_mirror
, bio
);
859 generic_make_request(bio
);
863 /*-----------------------------------------------------------------
865 *---------------------------------------------------------------*/
866 static LIST_HEAD(_mirror_sets
);
867 static DECLARE_RWSEM(_mirror_sets_lock
);
869 static void do_mirror(struct mirror_set
*ms
)
871 struct bio_list reads
, writes
;
873 spin_lock(&ms
->lock
);
876 bio_list_init(&ms
->reads
);
877 bio_list_init(&ms
->writes
);
878 spin_unlock(&ms
->lock
);
880 rh_update_states(&ms
->rh
);
882 do_reads(ms
, &reads
);
883 do_writes(ms
, &writes
);
886 static void do_work(struct work_struct
*ignored
)
888 struct mirror_set
*ms
;
890 down_read(&_mirror_sets_lock
);
891 list_for_each_entry (ms
, &_mirror_sets
, list
)
893 up_read(&_mirror_sets_lock
);
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
;
974 static int add_mirror_set(struct mirror_set
*ms
)
976 down_write(&_mirror_sets_lock
);
977 list_add_tail(&ms
->list
, &_mirror_sets
);
978 up_write(&_mirror_sets_lock
);
984 static void del_mirror_set(struct mirror_set
*ms
)
986 down_write(&_mirror_sets_lock
);
988 up_write(&_mirror_sets_lock
);
992 * Create dirty log: log_type #log_params <log_params>
994 static struct dirty_log
*create_dirty_log(struct dm_target
*ti
,
995 unsigned int argc
, char **argv
,
996 unsigned int *args_used
)
998 unsigned int param_count
;
999 struct dirty_log
*dl
;
1002 ti
->error
= "Insufficient mirror log arguments";
1006 if (sscanf(argv
[1], "%u", ¶m_count
) != 1) {
1007 ti
->error
= "Invalid mirror log argument count";
1011 *args_used
= 2 + param_count
;
1013 if (argc
< *args_used
) {
1014 ti
->error
= "Insufficient mirror log arguments";
1018 dl
= dm_create_dirty_log(argv
[0], ti
, param_count
, argv
+ 2);
1020 ti
->error
= "Error creating mirror dirty log";
1024 if (!_check_region_size(ti
, dl
->type
->get_region_size(dl
))) {
1025 ti
->error
= "Invalid region size";
1026 dm_destroy_dirty_log(dl
);
1034 * Construct a mirror mapping:
1036 * log_type #log_params <log_params>
1037 * #mirrors [mirror_path offset]{2,}
1039 * log_type is "core" or "disk"
1040 * #log_params is between 1 and 3
1042 #define DM_IO_PAGES 64
1043 static int mirror_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1046 unsigned int nr_mirrors
, m
, args_used
;
1047 struct mirror_set
*ms
;
1048 struct dirty_log
*dl
;
1050 dl
= create_dirty_log(ti
, argc
, argv
, &args_used
);
1057 if (!argc
|| sscanf(argv
[0], "%u", &nr_mirrors
) != 1 ||
1058 nr_mirrors
< 2 || nr_mirrors
> KCOPYD_MAX_REGIONS
+ 1) {
1059 ti
->error
= "Invalid number of mirrors";
1060 dm_destroy_dirty_log(dl
);
1066 if (argc
!= nr_mirrors
* 2) {
1067 ti
->error
= "Wrong number of mirror arguments";
1068 dm_destroy_dirty_log(dl
);
1072 ms
= alloc_context(nr_mirrors
, dl
->type
->get_region_size(dl
), ti
, dl
);
1074 dm_destroy_dirty_log(dl
);
1078 /* Get the mirror parameter sets */
1079 for (m
= 0; m
< nr_mirrors
; m
++) {
1080 r
= get_mirror(ms
, ti
, m
, argv
);
1082 free_context(ms
, ti
, m
);
1090 ti
->split_io
= ms
->rh
.region_size
;
1092 r
= kcopyd_client_create(DM_IO_PAGES
, &ms
->kcopyd_client
);
1094 free_context(ms
, ti
, ms
->nr_mirrors
);
1102 static void mirror_dtr(struct dm_target
*ti
)
1104 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1107 kcopyd_client_destroy(ms
->kcopyd_client
);
1108 free_context(ms
, ti
, ms
->nr_mirrors
);
1111 static void queue_bio(struct mirror_set
*ms
, struct bio
*bio
, int rw
)
1113 int should_wake
= 0;
1114 struct bio_list
*bl
;
1116 bl
= (rw
== WRITE
) ? &ms
->writes
: &ms
->reads
;
1117 spin_lock(&ms
->lock
);
1118 should_wake
= !(bl
->head
);
1119 bio_list_add(bl
, bio
);
1120 spin_unlock(&ms
->lock
);
1127 * Mirror mapping function
1129 static int mirror_map(struct dm_target
*ti
, struct bio
*bio
,
1130 union map_info
*map_context
)
1132 int r
, rw
= bio_rw(bio
);
1134 struct mirror_set
*ms
= ti
->private;
1136 map_context
->ll
= bio_to_region(&ms
->rh
, bio
);
1139 queue_bio(ms
, bio
, rw
);
1143 r
= ms
->rh
.log
->type
->in_sync(ms
->rh
.log
,
1144 bio_to_region(&ms
->rh
, bio
), 0);
1145 if (r
< 0 && r
!= -EWOULDBLOCK
)
1148 if (r
== -EWOULDBLOCK
) /* FIXME: ugly */
1152 * We don't want to fast track a recovery just for a read
1153 * ahead. So we just let it silently fail.
1154 * FIXME: get rid of this.
1156 if (!r
&& rw
== READA
)
1160 /* Pass this io over to the daemon */
1161 queue_bio(ms
, bio
, rw
);
1165 m
= choose_mirror(ms
, bio
->bi_sector
);
1169 map_bio(ms
, m
, bio
);
1173 static int mirror_end_io(struct dm_target
*ti
, struct bio
*bio
,
1174 int error
, union map_info
*map_context
)
1176 int rw
= bio_rw(bio
);
1177 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1178 region_t region
= map_context
->ll
;
1181 * We need to dec pending if this was a write.
1184 rh_dec(&ms
->rh
, region
);
1189 static void mirror_postsuspend(struct dm_target
*ti
)
1191 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1192 struct dirty_log
*log
= ms
->rh
.log
;
1194 rh_stop_recovery(&ms
->rh
);
1196 /* Wait for all I/O we generated to complete */
1197 wait_event(_kmirrord_recovery_stopped
,
1198 !atomic_read(&ms
->rh
.recovery_in_flight
));
1200 if (log
->type
->suspend
&& log
->type
->suspend(log
))
1201 /* FIXME: need better error handling */
1202 DMWARN("log suspend failed");
1205 static void mirror_resume(struct dm_target
*ti
)
1207 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1208 struct dirty_log
*log
= ms
->rh
.log
;
1209 if (log
->type
->resume
&& log
->type
->resume(log
))
1210 /* FIXME: need better error handling */
1211 DMWARN("log resume failed");
1212 rh_start_recovery(&ms
->rh
);
1215 static int mirror_status(struct dm_target
*ti
, status_type_t type
,
1216 char *result
, unsigned int maxlen
)
1219 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1221 sz
= ms
->rh
.log
->type
->status(ms
->rh
.log
, type
, result
, maxlen
);
1224 case STATUSTYPE_INFO
:
1225 DMEMIT("%d ", ms
->nr_mirrors
);
1226 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1227 DMEMIT("%s ", ms
->mirror
[m
].dev
->name
);
1230 (unsigned long long)ms
->rh
.log
->type
->
1231 get_sync_count(ms
->rh
.log
),
1232 (unsigned long long)ms
->nr_regions
);
1235 case STATUSTYPE_TABLE
:
1236 DMEMIT("%d", ms
->nr_mirrors
);
1237 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1238 DMEMIT(" %s %llu", ms
->mirror
[m
].dev
->name
,
1239 (unsigned long long)ms
->mirror
[m
].offset
);
1245 static struct target_type mirror_target
= {
1247 .version
= {1, 0, 2},
1248 .module
= THIS_MODULE
,
1252 .end_io
= mirror_end_io
,
1253 .postsuspend
= mirror_postsuspend
,
1254 .resume
= mirror_resume
,
1255 .status
= mirror_status
,
1258 static int __init
dm_mirror_init(void)
1262 r
= dm_dirty_log_init();
1266 _kmirrord_wq
= create_singlethread_workqueue("kmirrord");
1267 if (!_kmirrord_wq
) {
1268 DMERR("couldn't start kmirrord");
1269 dm_dirty_log_exit();
1272 INIT_WORK(&_kmirrord_work
, do_work
);
1274 r
= dm_register_target(&mirror_target
);
1276 DMERR("%s: Failed to register mirror target",
1277 mirror_target
.name
);
1278 dm_dirty_log_exit();
1279 destroy_workqueue(_kmirrord_wq
);
1285 static void __exit
dm_mirror_exit(void)
1289 r
= dm_unregister_target(&mirror_target
);
1291 DMERR("%s: unregister failed %d", mirror_target
.name
, r
);
1293 destroy_workqueue(_kmirrord_wq
);
1294 dm_dirty_log_exit();
1298 module_init(dm_mirror_init
);
1299 module_exit(dm_mirror_exit
);
1301 MODULE_DESCRIPTION(DM_NAME
" mirror target");
1302 MODULE_AUTHOR("Joe Thornber");
1303 MODULE_LICENSE("GPL");