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