[PATCH] mempool: use common mempool kmalloc allocator
[safe/jmp/linux-2.6] / drivers / md / dm-raid1.c
1 /*
2  * Copyright (C) 2003 Sistina Software Limited.
3  *
4  * This file is released under the GPL.
5  */
6
7 #include "dm.h"
8 #include "dm-bio-list.h"
9 #include "dm-io.h"
10 #include "dm-log.h"
11 #include "kcopyd.h"
12
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>
22
23 static struct workqueue_struct *_kmirrord_wq;
24 static struct work_struct _kmirrord_work;
25
26 static inline void wake(void)
27 {
28         queue_work(_kmirrord_wq, &_kmirrord_work);
29 }
30
31 /*-----------------------------------------------------------------
32  * Region hash
33  *
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.
37  *
38  * In addition to being present in the hash table a region _may_
39  * be present on one of three lists.
40  *
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
44  *   hash table.
45  *
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.
50  *
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
54  *   hash.
55  *
56  * There are 2 locks:
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
60  *   doing a write lock.
61  *
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  *---------------------------------------------------------------*/
67 struct mirror_set;
68 struct region_hash {
69         struct mirror_set *ms;
70         uint32_t region_size;
71         unsigned region_shift;
72
73         /* holds persistent region state */
74         struct dirty_log *log;
75
76         /* hash table */
77         rwlock_t hash_lock;
78         mempool_t *region_pool;
79         unsigned int mask;
80         unsigned int nr_buckets;
81         struct list_head *buckets;
82
83         spinlock_t region_lock;
84         struct semaphore recovery_count;
85         struct list_head clean_regions;
86         struct list_head quiesced_regions;
87         struct list_head recovered_regions;
88 };
89
90 enum {
91         RH_CLEAN,
92         RH_DIRTY,
93         RH_NOSYNC,
94         RH_RECOVERING
95 };
96
97 struct region {
98         struct region_hash *rh; /* FIXME: can we get rid of this ? */
99         region_t key;
100         int state;
101
102         struct list_head hash_list;
103         struct list_head list;
104
105         atomic_t pending;
106         struct bio_list delayed_bios;
107 };
108
109 /*
110  * Conversion fns
111  */
112 static inline region_t bio_to_region(struct region_hash *rh, struct bio *bio)
113 {
114         return bio->bi_sector >> rh->region_shift;
115 }
116
117 static inline sector_t region_to_sector(struct region_hash *rh, region_t region)
118 {
119         return region << rh->region_shift;
120 }
121
122 /* FIXME move this */
123 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw);
124
125 #define MIN_REGIONS 64
126 #define MAX_RECOVERY 1
127 static int rh_init(struct region_hash *rh, struct mirror_set *ms,
128                    struct dirty_log *log, uint32_t region_size,
129                    region_t nr_regions)
130 {
131         unsigned int nr_buckets, max_buckets;
132         size_t i;
133
134         /*
135          * Calculate a suitable number of buckets for our hash
136          * table.
137          */
138         max_buckets = nr_regions >> 6;
139         for (nr_buckets = 128u; nr_buckets < max_buckets; nr_buckets <<= 1)
140                 ;
141         nr_buckets >>= 1;
142
143         rh->ms = ms;
144         rh->log = log;
145         rh->region_size = region_size;
146         rh->region_shift = ffs(region_size) - 1;
147         rwlock_init(&rh->hash_lock);
148         rh->mask = nr_buckets - 1;
149         rh->nr_buckets = nr_buckets;
150
151         rh->buckets = vmalloc(nr_buckets * sizeof(*rh->buckets));
152         if (!rh->buckets) {
153                 DMERR("unable to allocate region hash memory");
154                 return -ENOMEM;
155         }
156
157         for (i = 0; i < nr_buckets; i++)
158                 INIT_LIST_HEAD(rh->buckets + i);
159
160         spin_lock_init(&rh->region_lock);
161         sema_init(&rh->recovery_count, 0);
162         INIT_LIST_HEAD(&rh->clean_regions);
163         INIT_LIST_HEAD(&rh->quiesced_regions);
164         INIT_LIST_HEAD(&rh->recovered_regions);
165
166         rh->region_pool = mempool_create_kmalloc_pool(MIN_REGIONS,
167                                                       sizeof(struct region));
168         if (!rh->region_pool) {
169                 vfree(rh->buckets);
170                 rh->buckets = NULL;
171                 return -ENOMEM;
172         }
173
174         return 0;
175 }
176
177 static void rh_exit(struct region_hash *rh)
178 {
179         unsigned int h;
180         struct region *reg, *nreg;
181
182         BUG_ON(!list_empty(&rh->quiesced_regions));
183         for (h = 0; h < rh->nr_buckets; h++) {
184                 list_for_each_entry_safe(reg, nreg, rh->buckets + h, hash_list) {
185                         BUG_ON(atomic_read(&reg->pending));
186                         mempool_free(reg, rh->region_pool);
187                 }
188         }
189
190         if (rh->log)
191                 dm_destroy_dirty_log(rh->log);
192         if (rh->region_pool)
193                 mempool_destroy(rh->region_pool);
194         vfree(rh->buckets);
195 }
196
197 #define RH_HASH_MULT 2654435387U
198
199 static inline unsigned int rh_hash(struct region_hash *rh, region_t region)
200 {
201         return (unsigned int) ((region * RH_HASH_MULT) >> 12) & rh->mask;
202 }
203
204 static struct region *__rh_lookup(struct region_hash *rh, region_t region)
205 {
206         struct region *reg;
207
208         list_for_each_entry (reg, rh->buckets + rh_hash(rh, region), hash_list)
209                 if (reg->key == region)
210                         return reg;
211
212         return NULL;
213 }
214
215 static void __rh_insert(struct region_hash *rh, struct region *reg)
216 {
217         unsigned int h = rh_hash(rh, reg->key);
218         list_add(&reg->hash_list, rh->buckets + h);
219 }
220
221 static struct region *__rh_alloc(struct region_hash *rh, region_t region)
222 {
223         struct region *reg, *nreg;
224
225         read_unlock(&rh->hash_lock);
226         nreg = mempool_alloc(rh->region_pool, GFP_NOIO);
227         nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
228                 RH_CLEAN : RH_NOSYNC;
229         nreg->rh = rh;
230         nreg->key = region;
231
232         INIT_LIST_HEAD(&nreg->list);
233
234         atomic_set(&nreg->pending, 0);
235         bio_list_init(&nreg->delayed_bios);
236         write_lock_irq(&rh->hash_lock);
237
238         reg = __rh_lookup(rh, region);
239         if (reg)
240                 /* we lost the race */
241                 mempool_free(nreg, rh->region_pool);
242
243         else {
244                 __rh_insert(rh, nreg);
245                 if (nreg->state == RH_CLEAN) {
246                         spin_lock(&rh->region_lock);
247                         list_add(&nreg->list, &rh->clean_regions);
248                         spin_unlock(&rh->region_lock);
249                 }
250                 reg = nreg;
251         }
252         write_unlock_irq(&rh->hash_lock);
253         read_lock(&rh->hash_lock);
254
255         return reg;
256 }
257
258 static inline struct region *__rh_find(struct region_hash *rh, region_t region)
259 {
260         struct region *reg;
261
262         reg = __rh_lookup(rh, region);
263         if (!reg)
264                 reg = __rh_alloc(rh, region);
265
266         return reg;
267 }
268
269 static int rh_state(struct region_hash *rh, region_t region, int may_block)
270 {
271         int r;
272         struct region *reg;
273
274         read_lock(&rh->hash_lock);
275         reg = __rh_lookup(rh, region);
276         read_unlock(&rh->hash_lock);
277
278         if (reg)
279                 return reg->state;
280
281         /*
282          * The region wasn't in the hash, so we fall back to the
283          * dirty log.
284          */
285         r = rh->log->type->in_sync(rh->log, region, may_block);
286
287         /*
288          * Any error from the dirty log (eg. -EWOULDBLOCK) gets
289          * taken as a RH_NOSYNC
290          */
291         return r == 1 ? RH_CLEAN : RH_NOSYNC;
292 }
293
294 static inline int rh_in_sync(struct region_hash *rh,
295                              region_t region, int may_block)
296 {
297         int state = rh_state(rh, region, may_block);
298         return state == RH_CLEAN || state == RH_DIRTY;
299 }
300
301 static void dispatch_bios(struct mirror_set *ms, struct bio_list *bio_list)
302 {
303         struct bio *bio;
304
305         while ((bio = bio_list_pop(bio_list))) {
306                 queue_bio(ms, bio, WRITE);
307         }
308 }
309
310 static void rh_update_states(struct region_hash *rh)
311 {
312         struct region *reg, *next;
313
314         LIST_HEAD(clean);
315         LIST_HEAD(recovered);
316
317         /*
318          * Quickly grab the lists.
319          */
320         write_lock_irq(&rh->hash_lock);
321         spin_lock(&rh->region_lock);
322         if (!list_empty(&rh->clean_regions)) {
323                 list_splice(&rh->clean_regions, &clean);
324                 INIT_LIST_HEAD(&rh->clean_regions);
325
326                 list_for_each_entry (reg, &clean, list) {
327                         rh->log->type->clear_region(rh->log, reg->key);
328                         list_del(&reg->hash_list);
329                 }
330         }
331
332         if (!list_empty(&rh->recovered_regions)) {
333                 list_splice(&rh->recovered_regions, &recovered);
334                 INIT_LIST_HEAD(&rh->recovered_regions);
335
336                 list_for_each_entry (reg, &recovered, list)
337                         list_del(&reg->hash_list);
338         }
339         spin_unlock(&rh->region_lock);
340         write_unlock_irq(&rh->hash_lock);
341
342         /*
343          * All the regions on the recovered and clean lists have
344          * now been pulled out of the system, so no need to do
345          * any more locking.
346          */
347         list_for_each_entry_safe (reg, next, &recovered, list) {
348                 rh->log->type->clear_region(rh->log, reg->key);
349                 rh->log->type->complete_resync_work(rh->log, reg->key, 1);
350                 dispatch_bios(rh->ms, &reg->delayed_bios);
351                 up(&rh->recovery_count);
352                 mempool_free(reg, rh->region_pool);
353         }
354
355         if (!list_empty(&recovered))
356                 rh->log->type->flush(rh->log);
357
358         list_for_each_entry_safe (reg, next, &clean, list)
359                 mempool_free(reg, rh->region_pool);
360 }
361
362 static void rh_inc(struct region_hash *rh, region_t region)
363 {
364         struct region *reg;
365
366         read_lock(&rh->hash_lock);
367         reg = __rh_find(rh, region);
368
369         spin_lock_irq(&rh->region_lock);
370         atomic_inc(&reg->pending);
371
372         if (reg->state == RH_CLEAN) {
373                 reg->state = RH_DIRTY;
374                 list_del_init(&reg->list);      /* take off the clean list */
375                 spin_unlock_irq(&rh->region_lock);
376
377                 rh->log->type->mark_region(rh->log, reg->key);
378         } else
379                 spin_unlock_irq(&rh->region_lock);
380
381
382         read_unlock(&rh->hash_lock);
383 }
384
385 static void rh_inc_pending(struct region_hash *rh, struct bio_list *bios)
386 {
387         struct bio *bio;
388
389         for (bio = bios->head; bio; bio = bio->bi_next)
390                 rh_inc(rh, bio_to_region(rh, bio));
391 }
392
393 static void rh_dec(struct region_hash *rh, region_t region)
394 {
395         unsigned long flags;
396         struct region *reg;
397         int should_wake = 0;
398
399         read_lock(&rh->hash_lock);
400         reg = __rh_lookup(rh, region);
401         read_unlock(&rh->hash_lock);
402
403         spin_lock_irqsave(&rh->region_lock, flags);
404         if (atomic_dec_and_test(&reg->pending)) {
405                 if (reg->state == RH_RECOVERING) {
406                         list_add_tail(&reg->list, &rh->quiesced_regions);
407                 } else {
408                         reg->state = RH_CLEAN;
409                         list_add(&reg->list, &rh->clean_regions);
410                 }
411                 should_wake = 1;
412         }
413         spin_unlock_irqrestore(&rh->region_lock, flags);
414
415         if (should_wake)
416                 wake();
417 }
418
419 /*
420  * Starts quiescing a region in preparation for recovery.
421  */
422 static int __rh_recovery_prepare(struct region_hash *rh)
423 {
424         int r;
425         struct region *reg;
426         region_t region;
427
428         /*
429          * Ask the dirty log what's next.
430          */
431         r = rh->log->type->get_resync_work(rh->log, &region);
432         if (r <= 0)
433                 return r;
434
435         /*
436          * Get this region, and start it quiescing by setting the
437          * recovering flag.
438          */
439         read_lock(&rh->hash_lock);
440         reg = __rh_find(rh, region);
441         read_unlock(&rh->hash_lock);
442
443         spin_lock_irq(&rh->region_lock);
444         reg->state = RH_RECOVERING;
445
446         /* Already quiesced ? */
447         if (atomic_read(&reg->pending))
448                 list_del_init(&reg->list);
449
450         else {
451                 list_del_init(&reg->list);
452                 list_add(&reg->list, &rh->quiesced_regions);
453         }
454         spin_unlock_irq(&rh->region_lock);
455
456         return 1;
457 }
458
459 static void rh_recovery_prepare(struct region_hash *rh)
460 {
461         while (!down_trylock(&rh->recovery_count))
462                 if (__rh_recovery_prepare(rh) <= 0) {
463                         up(&rh->recovery_count);
464                         break;
465                 }
466 }
467
468 /*
469  * Returns any quiesced regions.
470  */
471 static struct region *rh_recovery_start(struct region_hash *rh)
472 {
473         struct region *reg = NULL;
474
475         spin_lock_irq(&rh->region_lock);
476         if (!list_empty(&rh->quiesced_regions)) {
477                 reg = list_entry(rh->quiesced_regions.next,
478                                  struct region, list);
479                 list_del_init(&reg->list);      /* remove from the quiesced list */
480         }
481         spin_unlock_irq(&rh->region_lock);
482
483         return reg;
484 }
485
486 /* FIXME: success ignored for now */
487 static void rh_recovery_end(struct region *reg, int success)
488 {
489         struct region_hash *rh = reg->rh;
490
491         spin_lock_irq(&rh->region_lock);
492         list_add(&reg->list, &reg->rh->recovered_regions);
493         spin_unlock_irq(&rh->region_lock);
494
495         wake();
496 }
497
498 static void rh_flush(struct region_hash *rh)
499 {
500         rh->log->type->flush(rh->log);
501 }
502
503 static void rh_delay(struct region_hash *rh, struct bio *bio)
504 {
505         struct region *reg;
506
507         read_lock(&rh->hash_lock);
508         reg = __rh_find(rh, bio_to_region(rh, bio));
509         bio_list_add(&reg->delayed_bios, bio);
510         read_unlock(&rh->hash_lock);
511 }
512
513 static void rh_stop_recovery(struct region_hash *rh)
514 {
515         int i;
516
517         /* wait for any recovering regions */
518         for (i = 0; i < MAX_RECOVERY; i++)
519                 down(&rh->recovery_count);
520 }
521
522 static void rh_start_recovery(struct region_hash *rh)
523 {
524         int i;
525
526         for (i = 0; i < MAX_RECOVERY; i++)
527                 up(&rh->recovery_count);
528
529         wake();
530 }
531
532 /*-----------------------------------------------------------------
533  * Mirror set structures.
534  *---------------------------------------------------------------*/
535 struct mirror {
536         atomic_t error_count;
537         struct dm_dev *dev;
538         sector_t offset;
539 };
540
541 struct mirror_set {
542         struct dm_target *ti;
543         struct list_head list;
544         struct region_hash rh;
545         struct kcopyd_client *kcopyd_client;
546
547         spinlock_t lock;        /* protects the next two lists */
548         struct bio_list reads;
549         struct bio_list writes;
550
551         /* recovery */
552         region_t nr_regions;
553         int in_sync;
554
555         struct mirror *default_mirror;  /* Default mirror */
556
557         unsigned int nr_mirrors;
558         struct mirror mirror[0];
559 };
560
561 /*
562  * Every mirror should look like this one.
563  */
564 #define DEFAULT_MIRROR 0
565
566 /*
567  * This is yucky.  We squirrel the mirror_set struct away inside
568  * bi_next for write buffers.  This is safe since the bh
569  * doesn't get submitted to the lower levels of block layer.
570  */
571 static struct mirror_set *bio_get_ms(struct bio *bio)
572 {
573         return (struct mirror_set *) bio->bi_next;
574 }
575
576 static void bio_set_ms(struct bio *bio, struct mirror_set *ms)
577 {
578         bio->bi_next = (struct bio *) ms;
579 }
580
581 /*-----------------------------------------------------------------
582  * Recovery.
583  *
584  * When a mirror is first activated we may find that some regions
585  * are in the no-sync state.  We have to recover these by
586  * recopying from the default mirror to all the others.
587  *---------------------------------------------------------------*/
588 static void recovery_complete(int read_err, unsigned int write_err,
589                               void *context)
590 {
591         struct region *reg = (struct region *) context;
592
593         /* FIXME: better error handling */
594         rh_recovery_end(reg, read_err || write_err);
595 }
596
597 static int recover(struct mirror_set *ms, struct region *reg)
598 {
599         int r;
600         unsigned int i;
601         struct io_region from, to[KCOPYD_MAX_REGIONS], *dest;
602         struct mirror *m;
603         unsigned long flags = 0;
604
605         /* fill in the source */
606         m = ms->default_mirror;
607         from.bdev = m->dev->bdev;
608         from.sector = m->offset + region_to_sector(reg->rh, reg->key);
609         if (reg->key == (ms->nr_regions - 1)) {
610                 /*
611                  * The final region may be smaller than
612                  * region_size.
613                  */
614                 from.count = ms->ti->len & (reg->rh->region_size - 1);
615                 if (!from.count)
616                         from.count = reg->rh->region_size;
617         } else
618                 from.count = reg->rh->region_size;
619
620         /* fill in the destinations */
621         for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
622                 if (&ms->mirror[i] == ms->default_mirror)
623                         continue;
624
625                 m = ms->mirror + i;
626                 dest->bdev = m->dev->bdev;
627                 dest->sector = m->offset + region_to_sector(reg->rh, reg->key);
628                 dest->count = from.count;
629                 dest++;
630         }
631
632         /* hand to kcopyd */
633         set_bit(KCOPYD_IGNORE_ERROR, &flags);
634         r = kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, flags,
635                         recovery_complete, reg);
636
637         return r;
638 }
639
640 static void do_recovery(struct mirror_set *ms)
641 {
642         int r;
643         struct region *reg;
644         struct dirty_log *log = ms->rh.log;
645
646         /*
647          * Start quiescing some regions.
648          */
649         rh_recovery_prepare(&ms->rh);
650
651         /*
652          * Copy any already quiesced regions.
653          */
654         while ((reg = rh_recovery_start(&ms->rh))) {
655                 r = recover(ms, reg);
656                 if (r)
657                         rh_recovery_end(reg, 0);
658         }
659
660         /*
661          * Update the in sync flag.
662          */
663         if (!ms->in_sync &&
664             (log->type->get_sync_count(log) == ms->nr_regions)) {
665                 /* the sync is complete */
666                 dm_table_event(ms->ti->table);
667                 ms->in_sync = 1;
668         }
669 }
670
671 /*-----------------------------------------------------------------
672  * Reads
673  *---------------------------------------------------------------*/
674 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
675 {
676         /* FIXME: add read balancing */
677         return ms->default_mirror;
678 }
679
680 /*
681  * remap a buffer to a particular mirror.
682  */
683 static void map_bio(struct mirror_set *ms, struct mirror *m, struct bio *bio)
684 {
685         bio->bi_bdev = m->dev->bdev;
686         bio->bi_sector = m->offset + (bio->bi_sector - ms->ti->begin);
687 }
688
689 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
690 {
691         region_t region;
692         struct bio *bio;
693         struct mirror *m;
694
695         while ((bio = bio_list_pop(reads))) {
696                 region = bio_to_region(&ms->rh, bio);
697
698                 /*
699                  * We can only read balance if the region is in sync.
700                  */
701                 if (rh_in_sync(&ms->rh, region, 0))
702                         m = choose_mirror(ms, bio->bi_sector);
703                 else
704                         m = ms->default_mirror;
705
706                 map_bio(ms, m, bio);
707                 generic_make_request(bio);
708         }
709 }
710
711 /*-----------------------------------------------------------------
712  * Writes.
713  *
714  * We do different things with the write io depending on the
715  * state of the region that it's in:
716  *
717  * SYNC:        increment pending, use kcopyd to write to *all* mirrors
718  * RECOVERING:  delay the io until recovery completes
719  * NOSYNC:      increment pending, just write to the default mirror
720  *---------------------------------------------------------------*/
721 static void write_callback(unsigned long error, void *context)
722 {
723         unsigned int i;
724         int uptodate = 1;
725         struct bio *bio = (struct bio *) context;
726         struct mirror_set *ms;
727
728         ms = bio_get_ms(bio);
729         bio_set_ms(bio, NULL);
730
731         /*
732          * NOTE: We don't decrement the pending count here,
733          * instead it is done by the targets endio function.
734          * This way we handle both writes to SYNC and NOSYNC
735          * regions with the same code.
736          */
737
738         if (error) {
739                 /*
740                  * only error the io if all mirrors failed.
741                  * FIXME: bogus
742                  */
743                 uptodate = 0;
744                 for (i = 0; i < ms->nr_mirrors; i++)
745                         if (!test_bit(i, &error)) {
746                                 uptodate = 1;
747                                 break;
748                         }
749         }
750         bio_endio(bio, bio->bi_size, 0);
751 }
752
753 static void do_write(struct mirror_set *ms, struct bio *bio)
754 {
755         unsigned int i;
756         struct io_region io[KCOPYD_MAX_REGIONS+1];
757         struct mirror *m;
758
759         for (i = 0; i < ms->nr_mirrors; i++) {
760                 m = ms->mirror + i;
761
762                 io[i].bdev = m->dev->bdev;
763                 io[i].sector = m->offset + (bio->bi_sector - ms->ti->begin);
764                 io[i].count = bio->bi_size >> 9;
765         }
766
767         bio_set_ms(bio, ms);
768         dm_io_async_bvec(ms->nr_mirrors, io, WRITE,
769                          bio->bi_io_vec + bio->bi_idx,
770                          write_callback, bio);
771 }
772
773 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
774 {
775         int state;
776         struct bio *bio;
777         struct bio_list sync, nosync, recover, *this_list = NULL;
778
779         if (!writes->head)
780                 return;
781
782         /*
783          * Classify each write.
784          */
785         bio_list_init(&sync);
786         bio_list_init(&nosync);
787         bio_list_init(&recover);
788
789         while ((bio = bio_list_pop(writes))) {
790                 state = rh_state(&ms->rh, bio_to_region(&ms->rh, bio), 1);
791                 switch (state) {
792                 case RH_CLEAN:
793                 case RH_DIRTY:
794                         this_list = &sync;
795                         break;
796
797                 case RH_NOSYNC:
798                         this_list = &nosync;
799                         break;
800
801                 case RH_RECOVERING:
802                         this_list = &recover;
803                         break;
804                 }
805
806                 bio_list_add(this_list, bio);
807         }
808
809         /*
810          * Increment the pending counts for any regions that will
811          * be written to (writes to recover regions are going to
812          * be delayed).
813          */
814         rh_inc_pending(&ms->rh, &sync);
815         rh_inc_pending(&ms->rh, &nosync);
816         rh_flush(&ms->rh);
817
818         /*
819          * Dispatch io.
820          */
821         while ((bio = bio_list_pop(&sync)))
822                 do_write(ms, bio);
823
824         while ((bio = bio_list_pop(&recover)))
825                 rh_delay(&ms->rh, bio);
826
827         while ((bio = bio_list_pop(&nosync))) {
828                 map_bio(ms, ms->default_mirror, bio);
829                 generic_make_request(bio);
830         }
831 }
832
833 /*-----------------------------------------------------------------
834  * kmirrord
835  *---------------------------------------------------------------*/
836 static LIST_HEAD(_mirror_sets);
837 static DECLARE_RWSEM(_mirror_sets_lock);
838
839 static void do_mirror(struct mirror_set *ms)
840 {
841         struct bio_list reads, writes;
842
843         spin_lock(&ms->lock);
844         reads = ms->reads;
845         writes = ms->writes;
846         bio_list_init(&ms->reads);
847         bio_list_init(&ms->writes);
848         spin_unlock(&ms->lock);
849
850         rh_update_states(&ms->rh);
851         do_recovery(ms);
852         do_reads(ms, &reads);
853         do_writes(ms, &writes);
854 }
855
856 static void do_work(void *ignored)
857 {
858         struct mirror_set *ms;
859
860         down_read(&_mirror_sets_lock);
861         list_for_each_entry (ms, &_mirror_sets, list)
862                 do_mirror(ms);
863         up_read(&_mirror_sets_lock);
864 }
865
866 /*-----------------------------------------------------------------
867  * Target functions
868  *---------------------------------------------------------------*/
869 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
870                                         uint32_t region_size,
871                                         struct dm_target *ti,
872                                         struct dirty_log *dl)
873 {
874         size_t len;
875         struct mirror_set *ms = NULL;
876
877         if (array_too_big(sizeof(*ms), sizeof(ms->mirror[0]), nr_mirrors))
878                 return NULL;
879
880         len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
881
882         ms = kmalloc(len, GFP_KERNEL);
883         if (!ms) {
884                 ti->error = "dm-mirror: Cannot allocate mirror context";
885                 return NULL;
886         }
887
888         memset(ms, 0, len);
889         spin_lock_init(&ms->lock);
890
891         ms->ti = ti;
892         ms->nr_mirrors = nr_mirrors;
893         ms->nr_regions = dm_sector_div_up(ti->len, region_size);
894         ms->in_sync = 0;
895         ms->default_mirror = &ms->mirror[DEFAULT_MIRROR];
896
897         if (rh_init(&ms->rh, ms, dl, region_size, ms->nr_regions)) {
898                 ti->error = "dm-mirror: Error creating dirty region hash";
899                 kfree(ms);
900                 return NULL;
901         }
902
903         return ms;
904 }
905
906 static void free_context(struct mirror_set *ms, struct dm_target *ti,
907                          unsigned int m)
908 {
909         while (m--)
910                 dm_put_device(ti, ms->mirror[m].dev);
911
912         rh_exit(&ms->rh);
913         kfree(ms);
914 }
915
916 static inline int _check_region_size(struct dm_target *ti, uint32_t size)
917 {
918         return !(size % (PAGE_SIZE >> 9) || (size & (size - 1)) ||
919                  size > ti->len);
920 }
921
922 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
923                       unsigned int mirror, char **argv)
924 {
925         sector_t offset;
926
927         if (sscanf(argv[1], SECTOR_FORMAT, &offset) != 1) {
928                 ti->error = "dm-mirror: Invalid offset";
929                 return -EINVAL;
930         }
931
932         if (dm_get_device(ti, argv[0], offset, ti->len,
933                           dm_table_get_mode(ti->table),
934                           &ms->mirror[mirror].dev)) {
935                 ti->error = "dm-mirror: Device lookup failure";
936                 return -ENXIO;
937         }
938
939         ms->mirror[mirror].offset = offset;
940
941         return 0;
942 }
943
944 static int add_mirror_set(struct mirror_set *ms)
945 {
946         down_write(&_mirror_sets_lock);
947         list_add_tail(&ms->list, &_mirror_sets);
948         up_write(&_mirror_sets_lock);
949         wake();
950
951         return 0;
952 }
953
954 static void del_mirror_set(struct mirror_set *ms)
955 {
956         down_write(&_mirror_sets_lock);
957         list_del(&ms->list);
958         up_write(&_mirror_sets_lock);
959 }
960
961 /*
962  * Create dirty log: log_type #log_params <log_params>
963  */
964 static struct dirty_log *create_dirty_log(struct dm_target *ti,
965                                           unsigned int argc, char **argv,
966                                           unsigned int *args_used)
967 {
968         unsigned int param_count;
969         struct dirty_log *dl;
970
971         if (argc < 2) {
972                 ti->error = "dm-mirror: Insufficient mirror log arguments";
973                 return NULL;
974         }
975
976         if (sscanf(argv[1], "%u", &param_count) != 1) {
977                 ti->error = "dm-mirror: Invalid mirror log argument count";
978                 return NULL;
979         }
980
981         *args_used = 2 + param_count;
982
983         if (argc < *args_used) {
984                 ti->error = "dm-mirror: Insufficient mirror log arguments";
985                 return NULL;
986         }
987
988         dl = dm_create_dirty_log(argv[0], ti, param_count, argv + 2);
989         if (!dl) {
990                 ti->error = "dm-mirror: Error creating mirror dirty log";
991                 return NULL;
992         }
993
994         if (!_check_region_size(ti, dl->type->get_region_size(dl))) {
995                 ti->error = "dm-mirror: Invalid region size";
996                 dm_destroy_dirty_log(dl);
997                 return NULL;
998         }
999
1000         return dl;
1001 }
1002
1003 /*
1004  * Construct a mirror mapping:
1005  *
1006  * log_type #log_params <log_params>
1007  * #mirrors [mirror_path offset]{2,}
1008  *
1009  * log_type is "core" or "disk"
1010  * #log_params is between 1 and 3
1011  */
1012 #define DM_IO_PAGES 64
1013 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1014 {
1015         int r;
1016         unsigned int nr_mirrors, m, args_used;
1017         struct mirror_set *ms;
1018         struct dirty_log *dl;
1019
1020         dl = create_dirty_log(ti, argc, argv, &args_used);
1021         if (!dl)
1022                 return -EINVAL;
1023
1024         argv += args_used;
1025         argc -= args_used;
1026
1027         if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1028             nr_mirrors < 2 || nr_mirrors > KCOPYD_MAX_REGIONS + 1) {
1029                 ti->error = "dm-mirror: Invalid number of mirrors";
1030                 dm_destroy_dirty_log(dl);
1031                 return -EINVAL;
1032         }
1033
1034         argv++, argc--;
1035
1036         if (argc != nr_mirrors * 2) {
1037                 ti->error = "dm-mirror: Wrong number of mirror arguments";
1038                 dm_destroy_dirty_log(dl);
1039                 return -EINVAL;
1040         }
1041
1042         ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1043         if (!ms) {
1044                 dm_destroy_dirty_log(dl);
1045                 return -ENOMEM;
1046         }
1047
1048         /* Get the mirror parameter sets */
1049         for (m = 0; m < nr_mirrors; m++) {
1050                 r = get_mirror(ms, ti, m, argv);
1051                 if (r) {
1052                         free_context(ms, ti, m);
1053                         return r;
1054                 }
1055                 argv += 2;
1056                 argc -= 2;
1057         }
1058
1059         ti->private = ms;
1060         ti->split_io = ms->rh.region_size;
1061
1062         r = kcopyd_client_create(DM_IO_PAGES, &ms->kcopyd_client);
1063         if (r) {
1064                 free_context(ms, ti, ms->nr_mirrors);
1065                 return r;
1066         }
1067
1068         add_mirror_set(ms);
1069         return 0;
1070 }
1071
1072 static void mirror_dtr(struct dm_target *ti)
1073 {
1074         struct mirror_set *ms = (struct mirror_set *) ti->private;
1075
1076         del_mirror_set(ms);
1077         kcopyd_client_destroy(ms->kcopyd_client);
1078         free_context(ms, ti, ms->nr_mirrors);
1079 }
1080
1081 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1082 {
1083         int should_wake = 0;
1084         struct bio_list *bl;
1085
1086         bl = (rw == WRITE) ? &ms->writes : &ms->reads;
1087         spin_lock(&ms->lock);
1088         should_wake = !(bl->head);
1089         bio_list_add(bl, bio);
1090         spin_unlock(&ms->lock);
1091
1092         if (should_wake)
1093                 wake();
1094 }
1095
1096 /*
1097  * Mirror mapping function
1098  */
1099 static int mirror_map(struct dm_target *ti, struct bio *bio,
1100                       union map_info *map_context)
1101 {
1102         int r, rw = bio_rw(bio);
1103         struct mirror *m;
1104         struct mirror_set *ms = ti->private;
1105
1106         map_context->ll = bio->bi_sector >> ms->rh.region_shift;
1107
1108         if (rw == WRITE) {
1109                 queue_bio(ms, bio, rw);
1110                 return 0;
1111         }
1112
1113         r = ms->rh.log->type->in_sync(ms->rh.log,
1114                                       bio_to_region(&ms->rh, bio), 0);
1115         if (r < 0 && r != -EWOULDBLOCK)
1116                 return r;
1117
1118         if (r == -EWOULDBLOCK)  /* FIXME: ugly */
1119                 r = 0;
1120
1121         /*
1122          * We don't want to fast track a recovery just for a read
1123          * ahead.  So we just let it silently fail.
1124          * FIXME: get rid of this.
1125          */
1126         if (!r && rw == READA)
1127                 return -EIO;
1128
1129         if (!r) {
1130                 /* Pass this io over to the daemon */
1131                 queue_bio(ms, bio, rw);
1132                 return 0;
1133         }
1134
1135         m = choose_mirror(ms, bio->bi_sector);
1136         if (!m)
1137                 return -EIO;
1138
1139         map_bio(ms, m, bio);
1140         return 1;
1141 }
1142
1143 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1144                          int error, union map_info *map_context)
1145 {
1146         int rw = bio_rw(bio);
1147         struct mirror_set *ms = (struct mirror_set *) ti->private;
1148         region_t region = map_context->ll;
1149
1150         /*
1151          * We need to dec pending if this was a write.
1152          */
1153         if (rw == WRITE)
1154                 rh_dec(&ms->rh, region);
1155
1156         return 0;
1157 }
1158
1159 static void mirror_postsuspend(struct dm_target *ti)
1160 {
1161         struct mirror_set *ms = (struct mirror_set *) ti->private;
1162         struct dirty_log *log = ms->rh.log;
1163
1164         rh_stop_recovery(&ms->rh);
1165         if (log->type->suspend && log->type->suspend(log))
1166                 /* FIXME: need better error handling */
1167                 DMWARN("log suspend failed");
1168 }
1169
1170 static void mirror_resume(struct dm_target *ti)
1171 {
1172         struct mirror_set *ms = (struct mirror_set *) ti->private;
1173         struct dirty_log *log = ms->rh.log;
1174         if (log->type->resume && log->type->resume(log))
1175                 /* FIXME: need better error handling */
1176                 DMWARN("log resume failed");
1177         rh_start_recovery(&ms->rh);
1178 }
1179
1180 static int mirror_status(struct dm_target *ti, status_type_t type,
1181                          char *result, unsigned int maxlen)
1182 {
1183         unsigned int m, sz;
1184         struct mirror_set *ms = (struct mirror_set *) ti->private;
1185
1186         sz = ms->rh.log->type->status(ms->rh.log, type, result, maxlen);
1187
1188         switch (type) {
1189         case STATUSTYPE_INFO:
1190                 DMEMIT("%d ", ms->nr_mirrors);
1191                 for (m = 0; m < ms->nr_mirrors; m++)
1192                         DMEMIT("%s ", ms->mirror[m].dev->name);
1193
1194                 DMEMIT(SECTOR_FORMAT "/" SECTOR_FORMAT,
1195                        ms->rh.log->type->get_sync_count(ms->rh.log),
1196                        ms->nr_regions);
1197                 break;
1198
1199         case STATUSTYPE_TABLE:
1200                 DMEMIT("%d ", ms->nr_mirrors);
1201                 for (m = 0; m < ms->nr_mirrors; m++)
1202                         DMEMIT("%s " SECTOR_FORMAT " ",
1203                                ms->mirror[m].dev->name, ms->mirror[m].offset);
1204         }
1205
1206         return 0;
1207 }
1208
1209 static struct target_type mirror_target = {
1210         .name    = "mirror",
1211         .version = {1, 0, 1},
1212         .module  = THIS_MODULE,
1213         .ctr     = mirror_ctr,
1214         .dtr     = mirror_dtr,
1215         .map     = mirror_map,
1216         .end_io  = mirror_end_io,
1217         .postsuspend = mirror_postsuspend,
1218         .resume  = mirror_resume,
1219         .status  = mirror_status,
1220 };
1221
1222 static int __init dm_mirror_init(void)
1223 {
1224         int r;
1225
1226         r = dm_dirty_log_init();
1227         if (r)
1228                 return r;
1229
1230         _kmirrord_wq = create_singlethread_workqueue("kmirrord");
1231         if (!_kmirrord_wq) {
1232                 DMERR("couldn't start kmirrord");
1233                 dm_dirty_log_exit();
1234                 return r;
1235         }
1236         INIT_WORK(&_kmirrord_work, do_work, NULL);
1237
1238         r = dm_register_target(&mirror_target);
1239         if (r < 0) {
1240                 DMERR("%s: Failed to register mirror target",
1241                       mirror_target.name);
1242                 dm_dirty_log_exit();
1243                 destroy_workqueue(_kmirrord_wq);
1244         }
1245
1246         return r;
1247 }
1248
1249 static void __exit dm_mirror_exit(void)
1250 {
1251         int r;
1252
1253         r = dm_unregister_target(&mirror_target);
1254         if (r < 0)
1255                 DMERR("%s: unregister failed %d", mirror_target.name, r);
1256
1257         destroy_workqueue(_kmirrord_wq);
1258         dm_dirty_log_exit();
1259 }
1260
1261 /* Module hooks */
1262 module_init(dm_mirror_init);
1263 module_exit(dm_mirror_exit);
1264
1265 MODULE_DESCRIPTION(DM_NAME " mirror target");
1266 MODULE_AUTHOR("Joe Thornber");
1267 MODULE_LICENSE("GPL");