[PATCH] VFS: extra check inside dentry_unhash()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-raid1.c
blobfc8cbb168e3ed2d5f8ae53bcec7762d0cc53008b
1 /*
2 * Copyright (C) 2003 Sistina Software Limited.
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
8 #include "dm-bio-list.h"
9 #include "dm-io.h"
10 #include "dm-log.h"
11 #include "kcopyd.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 /*-----------------------------------------------------------------
35 * Region hash
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
47 * hash table.
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
57 * hash.
59 * There are 2 locks:
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
63 * doing a write lock.
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 *---------------------------------------------------------------*/
70 struct mirror_set;
71 struct region_hash {
72 struct mirror_set *ms;
73 uint32_t region_size;
74 unsigned region_shift;
76 /* holds persistent region state */
77 struct dirty_log *log;
79 /* hash table */
80 rwlock_t hash_lock;
81 mempool_t *region_pool;
82 unsigned int mask;
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;
94 enum {
95 RH_CLEAN,
96 RH_DIRTY,
97 RH_NOSYNC,
98 RH_RECOVERING
101 struct region {
102 struct region_hash *rh; /* FIXME: can we get rid of this ? */
103 region_t key;
104 int state;
106 struct list_head hash_list;
107 struct list_head list;
109 atomic_t pending;
110 struct bio_list delayed_bios;
114 /*-----------------------------------------------------------------
115 * Mirror set structures.
116 *---------------------------------------------------------------*/
117 struct mirror {
118 atomic_t error_count;
119 struct dm_dev *dev;
120 sector_t offset;
123 struct mirror_set {
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;
133 /* recovery */
134 region_t nr_regions;
135 int in_sync;
137 struct mirror *default_mirror; /* Default mirror */
139 unsigned int nr_mirrors;
140 struct mirror mirror[0];
144 * Conversion fns
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,
163 region_t nr_regions)
165 unsigned int nr_buckets, max_buckets;
166 size_t i;
169 * Calculate a suitable number of buckets for our hash
170 * table.
172 max_buckets = nr_regions >> 6;
173 for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
175 nr_buckets >>= 1;
177 rh->ms = ms;
178 rh->log = log;
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));
186 if (!rh->buckets) {
187 DMERR("unable to allocate region hash memory");
188 return -ENOMEM;
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) {
204 vfree(rh->buckets);
205 rh->buckets = NULL;
206 return -ENOMEM;
209 return 0;
212 static void rh_exit(struct region_hash *rh)
214 unsigned int h;
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(&reg->pending));
221 mempool_free(reg, rh->region_pool);
225 if (rh->log)
226 dm_destroy_dirty_log(rh->log);
227 if (rh->region_pool)
228 mempool_destroy(rh->region_pool);
229 vfree(rh->buckets);
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)
241 struct region *reg;
243 list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
244 if (reg->key == region)
245 return reg;
247 return NULL;
250 static void __rh_insert(struct region_hash *rh, struct region *reg)
252 unsigned int h = rh_hash(rh, reg->key);
253 list_add(&reg->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);
262 if (unlikely(!nreg))
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;
266 nreg->rh = rh;
267 nreg->key = region;
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);
276 if (reg)
277 /* we lost the race */
278 mempool_free(nreg, rh->region_pool);
280 else {
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);
287 reg = nreg;
289 write_unlock_irq(&rh->hash_lock);
290 read_lock(&rh->hash_lock);
292 return reg;
295 static inline struct region *__rh_find(struct region_hash *rh, region_t region)
297 struct region *reg;
299 reg = __rh_lookup(rh, region);
300 if (!reg)
301 reg = __rh_alloc(rh, region);
303 return reg;
306 static int rh_state(struct region_hash *rh, region_t region, int may_block)
308 int r;
309 struct region *reg;
311 read_lock(&rh->hash_lock);
312 reg = __rh_lookup(rh, region);
313 read_unlock(&rh->hash_lock);
315 if (reg)
316 return reg->state;
319 * The region wasn't in the hash, so we fall back to the
320 * dirty log.
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)
340 struct bio *bio;
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;
351 LIST_HEAD(clean);
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(&reg->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(&reg->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
382 * any more locking.
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, &reg->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)
403 struct region *reg;
405 read_lock(&rh->hash_lock);
406 reg = __rh_find(rh, region);
408 spin_lock_irq(&rh->region_lock);
409 atomic_inc(&reg->pending);
411 if (reg->state == RH_CLEAN) {
412 reg->state = RH_DIRTY;
413 list_del_init(&reg->list); /* take off the clean list */
414 spin_unlock_irq(&rh->region_lock);
416 rh->log->type->mark_region(rh->log, reg->key);
417 } else
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)
426 struct bio *bio;
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)
434 unsigned long flags;
435 struct region *reg;
436 int should_wake = 0;
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(&reg->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
450 * from clean list.
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(&reg->list, &rh->quiesced_regions);
458 } else if (reg->state == RH_DIRTY) {
459 reg->state = RH_CLEAN;
460 list_add(&reg->list, &rh->clean_regions);
462 should_wake = 1;
464 spin_unlock_irqrestore(&rh->region_lock, flags);
466 if (should_wake)
467 wake();
471 * Starts quiescing a region in preparation for recovery.
473 static int __rh_recovery_prepare(struct region_hash *rh)
475 int r;
476 struct region *reg;
477 region_t region;
480 * Ask the dirty log what's next.
482 r = rh->log->type->get_resync_work(rh->log, &region);
483 if (r <= 0)
484 return r;
487 * Get this region, and start it quiescing by setting the
488 * recovering flag.
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(&reg->pending))
499 list_del_init(&reg->list);
500 else
501 list_move(&reg->list, &rh->quiesced_regions);
503 spin_unlock_irq(&rh->region_lock);
505 return 1;
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);
518 break;
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(&reg->list); /* remove from the quiesced list */
540 spin_unlock_irq(&rh->region_lock);
542 return reg;
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(&reg->list, &reg->rh->recovered_regions);
552 spin_unlock_irq(&rh->region_lock);
554 wake();
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)
564 struct region *reg;
566 read_lock(&rh->hash_lock);
567 reg = __rh_find(rh, bio_to_region(rh, bio));
568 bio_list_add(&reg->delayed_bios, bio);
569 read_unlock(&rh->hash_lock);
572 static void rh_stop_recovery(struct region_hash *rh)
574 int i;
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)
583 int i;
585 for (i = 0; i < MAX_RECOVERY; i++)
586 up(&rh->recovery_count);
588 wake();
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 /*-----------------------------------------------------------------
612 * Recovery.
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,
619 void *context)
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)
629 int r;
630 unsigned int i;
631 struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
632 struct mirror *m;
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
642 * region_size.
644 from.count = ms->ti->len & (reg->rh->region_size - 1);
645 if (!from.count)
646 from.count = reg->rh->region_size;
647 } else
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)
653 continue;
655 m = ms->mirror + i;
656 dest->bdev = m->dev->bdev;
657 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
658 dest->count = from.count;
659 dest++;
662 /* hand to kcopyd */
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);
667 return r;
670 static void do_recovery(struct mirror_set *ms)
672 int r;
673 struct region *reg;
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);
686 if (r)
687 rh_recovery_end(reg, 0);
691 * Update the in sync flag.
693 if (!ms->in_sync &&
694 (log->type->get_sync_count(log) == ms->nr_regions)) {
695 /* the sync is complete */
696 dm_table_event(ms->ti->table);
697 ms->in_sync = 1;
701 /*-----------------------------------------------------------------
702 * Reads
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)
721 region_t region;
722 struct bio *bio;
723 struct mirror *m;
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);
733 else
734 m = ms->default_mirror;
736 map_bio(ms, m, bio);
737 generic_make_request(bio);
741 /*-----------------------------------------------------------------
742 * Writes.
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)
753 unsigned int i;
754 int uptodate = 1;
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.
768 if (error) {
770 * only error the io if all mirrors failed.
771 * FIXME: bogus
773 uptodate = 0;
774 for (i = 0; i < ms->nr_mirrors; i++)
775 if (!test_bit(i, &error)) {
776 uptodate = 1;
777 break;
780 bio_endio(bio, bio->bi_size, 0);
783 static void do_write(struct mirror_set *ms, struct bio *bio)
785 unsigned int i;
786 struct io_region io[KCOPYD_MAX_REGIONS+1];
787 struct mirror *m;
789 for (i = 0; i < ms->nr_mirrors; i++) {
790 m = ms->mirror + 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;
797 bio_set_ms(bio, ms);
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)
805 int state;
806 struct bio *bio;
807 struct bio_list sync, nosync, recover, *this_list = NULL;
809 if (!writes->head)
810 return;
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);
821 switch (state) {
822 case RH_CLEAN:
823 case RH_DIRTY:
824 this_list = &sync;
825 break;
827 case RH_NOSYNC:
828 this_list = &nosync;
829 break;
831 case RH_RECOVERING:
832 this_list = &recover;
833 break;
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
842 * be delayed).
844 rh_inc_pending(&ms->rh, &sync);
845 rh_inc_pending(&ms->rh, &nosync);
846 rh_flush(&ms->rh);
849 * Dispatch io.
851 while ((bio = bio_list_pop(&sync)))
852 do_write(ms, bio);
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 /*-----------------------------------------------------------------
864 * kmirrord
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);
874 reads = ms->reads;
875 writes = ms->writes;
876 bio_list_init(&ms->reads);
877 bio_list_init(&ms->writes);
878 spin_unlock(&ms->lock);
880 rh_update_states(&ms->rh);
881 do_recovery(ms);
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)
892 do_mirror(ms);
893 up_read(&_mirror_sets_lock);
896 /*-----------------------------------------------------------------
897 * Target functions
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)
904 size_t len;
905 struct mirror_set *ms = NULL;
907 if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
908 return NULL;
910 len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
912 ms = kmalloc(len, GFP_KERNEL);
913 if (!ms) {
914 ti->error = "Cannot allocate mirror context";
915 return NULL;
918 memset(ms, 0, len);
919 spin_lock_init(&ms->lock);
921 ms->ti = ti;
922 ms->nr_mirrors = nr_mirrors;
923 ms->nr_regions = dm_sector_div_up(ti->len, region_size);
924 ms->in_sync = 0;
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";
929 kfree(ms);
930 return NULL;
933 return ms;
936 static void free_context(struct mirror_set *ms, struct dm_target *ti,
937 unsigned int m)
939 while (m--)
940 dm_put_device(ti, ms->mirror[m].dev);
942 rh_exit(&ms->rh);
943 kfree(ms);
946 static inline int _check_region_size(struct dm_target *ti, uint32_t size)
948 return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) ||
949 size > ti->len);
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";
959 return -EINVAL;
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";
966 return -ENXIO;
969 ms->mirror[mirror].offset = offset;
971 return 0;
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);
979 wake();
981 return 0;
984 static void del_mirror_set(struct mirror_set *ms)
986 down_write(&_mirror_sets_lock);
987 list_del(&ms->list);
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;
1001 if (argc < 2) {
1002 ti->error = "Insufficient mirror log arguments";
1003 return NULL;
1006 if (sscanf(argv[1], "%u", &param_count) != 1) {
1007 ti->error = "Invalid mirror log argument count";
1008 return NULL;
1011 *args_used = 2 + param_count;
1013 if (argc < *args_used) {
1014 ti->error = "Insufficient mirror log arguments";
1015 return NULL;
1018 dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
1019 if (!dl) {
1020 ti->error = "Error creating mirror dirty log";
1021 return NULL;
1024 if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
1025 ti->error = "Invalid region size";
1026 dm_destroy_dirty_log(dl);
1027 return NULL;
1030 return 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)
1045 int r;
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);
1051 if (!dl)
1052 return -EINVAL;
1054 argv += args_used;
1055 argc -= 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);
1061 return -EINVAL;
1064 argv++, argc--;
1066 if (argc != nr_mirrors * 2) {
1067 ti->error = "Wrong number of mirror arguments";
1068 dm_destroy_dirty_log(dl);
1069 return -EINVAL;
1072 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1073 if (!ms) {
1074 dm_destroy_dirty_log(dl);
1075 return -ENOMEM;
1078 /* Get the mirror parameter sets */
1079 for (m = 0; m < nr_mirrors; m++) {
1080 r = get_mirror(ms, ti, m, argv);
1081 if (r) {
1082 free_context(ms, ti, m);
1083 return r;
1085 argv += 2;
1086 argc -= 2;
1089 ti->private = ms;
1090 ti->split_io = ms->rh.region_size;
1092 r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
1093 if (r) {
1094 free_context(ms, ti, ms->nr_mirrors);
1095 return r;
1098 add_mirror_set(ms);
1099 return 0;
1102 static void mirror_dtr(struct dm_target *ti)
1104 struct mirror_set *ms = (struct mirror_set *) ti->private;
1106 del_mirror_set(ms);
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);
1122 if (should_wake)
1123 wake();
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);
1133 struct mirror *m;
1134 struct mirror_set *ms = ti->private;
1136 map_context->ll = bio_to_region(&ms->rh, bio);
1138 if (rw == WRITE) {
1139 queue_bio(ms, bio, rw);
1140 return 0;
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)
1146 return r;
1148 if (r == -EWOULDBLOCK) /* FIXME: ugly */
1149 r = 0;
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)
1157 return -EIO;
1159 if (!r) {
1160 /* Pass this io over to the daemon */
1161 queue_bio(ms, bio, rw);
1162 return 0;
1165 m = choose_mirror(ms, bio->bi_sector);
1166 if (!m)
1167 return -EIO;
1169 map_bio(ms, m, bio);
1170 return 1;
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.
1183 if (rw == WRITE)
1184 rh_dec(&ms->rh, region);
1186 return 0;
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)
1218 unsigned int m, sz;
1219 struct mirror_set *ms = (struct mirror_set *) ti->private;
1221 sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1223 switch (type) {
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);
1229 DMEMIT("%llu/%llu",
1230 (unsigned long long)ms->rh.log->type->
1231 get_sync_count(ms->rh.log),
1232 (unsigned long long)ms->nr_regions);
1233 break;
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);
1242 return 0;
1245 static struct target_type mirror_target = {
1246 .name = "mirror",
1247 .version = {1, 0, 2},
1248 .module = THIS_MODULE,
1249 .ctr = mirror_ctr,
1250 .dtr = mirror_dtr,
1251 .map = mirror_map,
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)
1260 int r;
1262 r = dm_dirty_log_init();
1263 if (r)
1264 return r;
1266 _kmirrord_wq = create_singlethread_workqueue("kmirrord");
1267 if (!_kmirrord_wq) {
1268 DMERR("couldn't start kmirrord");
1269 dm_dirty_log_exit();
1270 return r;
1272 INIT_WORK(&_kmirrord_work, do_work);
1274 r = dm_register_target(&mirror_target);
1275 if (r < 0) {
1276 DMERR("%s: Failed to register mirror target",
1277 mirror_target.name);
1278 dm_dirty_log_exit();
1279 destroy_workqueue(_kmirrord_wq);
1282 return r;
1285 static void __exit dm_mirror_exit(void)
1287 int r;
1289 r = dm_unregister_target(&mirror_target);
1290 if (r < 0)
1291 DMERR("%s: unregister failed %d", mirror_target.name, r);
1293 destroy_workqueue(_kmirrord_wq);
1294 dm_dirty_log_exit();
1297 /* Module hooks */
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");