dn_getsockoptdecnet: move nf_{get/set}sockopt outside sock lock
[pandora-kernel.git] / drivers / md / dm-raid1.c
1 /*
2  * Copyright (C) 2003 Sistina Software Limited.
3  * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the GPL.
6  */
7
8 #include "dm-bio-record.h"
9
10 #include <linux/init.h>
11 #include <linux/mempool.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/device-mapper.h>
17 #include <linux/dm-io.h>
18 #include <linux/dm-dirty-log.h>
19 #include <linux/dm-kcopyd.h>
20 #include <linux/dm-region-hash.h>
21
22 #define DM_MSG_PREFIX "raid1"
23
24 #define MAX_RECOVERY 1  /* Maximum number of regions recovered in parallel. */
25
26 #define DM_RAID1_HANDLE_ERRORS 0x01
27 #define errors_handled(p)       ((p)->features & DM_RAID1_HANDLE_ERRORS)
28
29 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
30
31 /*-----------------------------------------------------------------
32  * Mirror set structures.
33  *---------------------------------------------------------------*/
34 enum dm_raid1_error {
35         DM_RAID1_WRITE_ERROR,
36         DM_RAID1_FLUSH_ERROR,
37         DM_RAID1_SYNC_ERROR,
38         DM_RAID1_READ_ERROR
39 };
40
41 struct mirror {
42         struct mirror_set *ms;
43         atomic_t error_count;
44         unsigned long error_type;
45         struct dm_dev *dev;
46         sector_t offset;
47 };
48
49 struct mirror_set {
50         struct dm_target *ti;
51         struct list_head list;
52
53         uint64_t features;
54
55         spinlock_t lock;        /* protects the lists */
56         struct bio_list reads;
57         struct bio_list writes;
58         struct bio_list failures;
59         struct bio_list holds;  /* bios are waiting until suspend */
60
61         struct dm_region_hash *rh;
62         struct dm_kcopyd_client *kcopyd_client;
63         struct dm_io_client *io_client;
64         mempool_t *read_record_pool;
65
66         /* recovery */
67         region_t nr_regions;
68         int in_sync;
69         int log_failure;
70         int leg_failure;
71         atomic_t suspend;
72
73         atomic_t default_mirror;        /* Default mirror */
74
75         struct workqueue_struct *kmirrord_wq;
76         struct work_struct kmirrord_work;
77         struct timer_list timer;
78         unsigned long timer_pending;
79
80         struct work_struct trigger_event;
81
82         unsigned nr_mirrors;
83         struct mirror mirror[0];
84 };
85
86 static void wakeup_mirrord(void *context)
87 {
88         struct mirror_set *ms = context;
89
90         queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
91 }
92
93 static void delayed_wake_fn(unsigned long data)
94 {
95         struct mirror_set *ms = (struct mirror_set *) data;
96
97         clear_bit(0, &ms->timer_pending);
98         wakeup_mirrord(ms);
99 }
100
101 static void delayed_wake(struct mirror_set *ms)
102 {
103         if (test_and_set_bit(0, &ms->timer_pending))
104                 return;
105
106         ms->timer.expires = jiffies + HZ / 5;
107         ms->timer.data = (unsigned long) ms;
108         ms->timer.function = delayed_wake_fn;
109         add_timer(&ms->timer);
110 }
111
112 static void wakeup_all_recovery_waiters(void *context)
113 {
114         wake_up_all(&_kmirrord_recovery_stopped);
115 }
116
117 static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
118 {
119         unsigned long flags;
120         int should_wake = 0;
121         struct bio_list *bl;
122
123         bl = (rw == WRITE) ? &ms->writes : &ms->reads;
124         spin_lock_irqsave(&ms->lock, flags);
125         should_wake = !(bl->head);
126         bio_list_add(bl, bio);
127         spin_unlock_irqrestore(&ms->lock, flags);
128
129         if (should_wake)
130                 wakeup_mirrord(ms);
131 }
132
133 static void dispatch_bios(void *context, struct bio_list *bio_list)
134 {
135         struct mirror_set *ms = context;
136         struct bio *bio;
137
138         while ((bio = bio_list_pop(bio_list)))
139                 queue_bio(ms, bio, WRITE);
140 }
141
142 #define MIN_READ_RECORDS 20
143 struct dm_raid1_read_record {
144         struct mirror *m;
145         struct dm_bio_details details;
146 };
147
148 static struct kmem_cache *_dm_raid1_read_record_cache;
149
150 /*
151  * Every mirror should look like this one.
152  */
153 #define DEFAULT_MIRROR 0
154
155 /*
156  * This is yucky.  We squirrel the mirror struct away inside
157  * bi_next for read/write buffers.  This is safe since the bh
158  * doesn't get submitted to the lower levels of block layer.
159  */
160 static struct mirror *bio_get_m(struct bio *bio)
161 {
162         return (struct mirror *) bio->bi_next;
163 }
164
165 static void bio_set_m(struct bio *bio, struct mirror *m)
166 {
167         bio->bi_next = (struct bio *) m;
168 }
169
170 static struct mirror *get_default_mirror(struct mirror_set *ms)
171 {
172         return &ms->mirror[atomic_read(&ms->default_mirror)];
173 }
174
175 static void set_default_mirror(struct mirror *m)
176 {
177         struct mirror_set *ms = m->ms;
178         struct mirror *m0 = &(ms->mirror[0]);
179
180         atomic_set(&ms->default_mirror, m - m0);
181 }
182
183 static struct mirror *get_valid_mirror(struct mirror_set *ms)
184 {
185         struct mirror *m;
186
187         for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
188                 if (!atomic_read(&m->error_count))
189                         return m;
190
191         return NULL;
192 }
193
194 /* fail_mirror
195  * @m: mirror device to fail
196  * @error_type: one of the enum's, DM_RAID1_*_ERROR
197  *
198  * If errors are being handled, record the type of
199  * error encountered for this device.  If this type
200  * of error has already been recorded, we can return;
201  * otherwise, we must signal userspace by triggering
202  * an event.  Additionally, if the device is the
203  * primary device, we must choose a new primary, but
204  * only if the mirror is in-sync.
205  *
206  * This function must not block.
207  */
208 static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
209 {
210         struct mirror_set *ms = m->ms;
211         struct mirror *new;
212
213         ms->leg_failure = 1;
214
215         /*
216          * error_count is used for nothing more than a
217          * simple way to tell if a device has encountered
218          * errors.
219          */
220         atomic_inc(&m->error_count);
221
222         if (test_and_set_bit(error_type, &m->error_type))
223                 return;
224
225         if (!errors_handled(ms))
226                 return;
227
228         if (m != get_default_mirror(ms))
229                 goto out;
230
231         if (!ms->in_sync) {
232                 /*
233                  * Better to issue requests to same failing device
234                  * than to risk returning corrupt data.
235                  */
236                 DMERR("Primary mirror (%s) failed while out-of-sync: "
237                       "Reads may fail.", m->dev->name);
238                 goto out;
239         }
240
241         new = get_valid_mirror(ms);
242         if (new)
243                 set_default_mirror(new);
244         else
245                 DMWARN("All sides of mirror have failed.");
246
247 out:
248         schedule_work(&ms->trigger_event);
249 }
250
251 static int mirror_flush(struct dm_target *ti)
252 {
253         struct mirror_set *ms = ti->private;
254         unsigned long error_bits;
255
256         unsigned int i;
257         struct dm_io_region io[ms->nr_mirrors];
258         struct mirror *m;
259         struct dm_io_request io_req = {
260                 .bi_rw = WRITE_FLUSH,
261                 .mem.type = DM_IO_KMEM,
262                 .mem.ptr.addr = NULL,
263                 .client = ms->io_client,
264         };
265
266         for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
267                 io[i].bdev = m->dev->bdev;
268                 io[i].sector = 0;
269                 io[i].count = 0;
270         }
271
272         error_bits = -1;
273         dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
274         if (unlikely(error_bits != 0)) {
275                 for (i = 0; i < ms->nr_mirrors; i++)
276                         if (test_bit(i, &error_bits))
277                                 fail_mirror(ms->mirror + i,
278                                             DM_RAID1_FLUSH_ERROR);
279                 return -EIO;
280         }
281
282         return 0;
283 }
284
285 /*-----------------------------------------------------------------
286  * Recovery.
287  *
288  * When a mirror is first activated we may find that some regions
289  * are in the no-sync state.  We have to recover these by
290  * recopying from the default mirror to all the others.
291  *---------------------------------------------------------------*/
292 static void recovery_complete(int read_err, unsigned long write_err,
293                               void *context)
294 {
295         struct dm_region *reg = context;
296         struct mirror_set *ms = dm_rh_region_context(reg);
297         int m, bit = 0;
298
299         if (read_err) {
300                 /* Read error means the failure of default mirror. */
301                 DMERR_LIMIT("Unable to read primary mirror during recovery");
302                 fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
303         }
304
305         if (write_err) {
306                 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
307                             write_err);
308                 /*
309                  * Bits correspond to devices (excluding default mirror).
310                  * The default mirror cannot change during recovery.
311                  */
312                 for (m = 0; m < ms->nr_mirrors; m++) {
313                         if (&ms->mirror[m] == get_default_mirror(ms))
314                                 continue;
315                         if (test_bit(bit, &write_err))
316                                 fail_mirror(ms->mirror + m,
317                                             DM_RAID1_SYNC_ERROR);
318                         bit++;
319                 }
320         }
321
322         dm_rh_recovery_end(reg, !(read_err || write_err));
323 }
324
325 static int recover(struct mirror_set *ms, struct dm_region *reg)
326 {
327         int r;
328         unsigned i;
329         struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
330         struct mirror *m;
331         unsigned long flags = 0;
332         region_t key = dm_rh_get_region_key(reg);
333         sector_t region_size = dm_rh_get_region_size(ms->rh);
334
335         /* fill in the source */
336         m = get_default_mirror(ms);
337         from.bdev = m->dev->bdev;
338         from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
339         if (key == (ms->nr_regions - 1)) {
340                 /*
341                  * The final region may be smaller than
342                  * region_size.
343                  */
344                 from.count = ms->ti->len & (region_size - 1);
345                 if (!from.count)
346                         from.count = region_size;
347         } else
348                 from.count = region_size;
349
350         /* fill in the destinations */
351         for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
352                 if (&ms->mirror[i] == get_default_mirror(ms))
353                         continue;
354
355                 m = ms->mirror + i;
356                 dest->bdev = m->dev->bdev;
357                 dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
358                 dest->count = from.count;
359                 dest++;
360         }
361
362         /* hand to kcopyd */
363         if (!errors_handled(ms))
364                 set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
365
366         r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
367                            flags, recovery_complete, reg);
368
369         return r;
370 }
371
372 static void do_recovery(struct mirror_set *ms)
373 {
374         struct dm_region *reg;
375         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
376         int r;
377
378         /*
379          * Start quiescing some regions.
380          */
381         dm_rh_recovery_prepare(ms->rh);
382
383         /*
384          * Copy any already quiesced regions.
385          */
386         while ((reg = dm_rh_recovery_start(ms->rh))) {
387                 r = recover(ms, reg);
388                 if (r)
389                         dm_rh_recovery_end(reg, 0);
390         }
391
392         /*
393          * Update the in sync flag.
394          */
395         if (!ms->in_sync &&
396             (log->type->get_sync_count(log) == ms->nr_regions)) {
397                 /* the sync is complete */
398                 dm_table_event(ms->ti->table);
399                 ms->in_sync = 1;
400         }
401 }
402
403 /*-----------------------------------------------------------------
404  * Reads
405  *---------------------------------------------------------------*/
406 static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
407 {
408         struct mirror *m = get_default_mirror(ms);
409
410         do {
411                 if (likely(!atomic_read(&m->error_count)))
412                         return m;
413
414                 if (m-- == ms->mirror)
415                         m += ms->nr_mirrors;
416         } while (m != get_default_mirror(ms));
417
418         return NULL;
419 }
420
421 static int default_ok(struct mirror *m)
422 {
423         struct mirror *default_mirror = get_default_mirror(m->ms);
424
425         return !atomic_read(&default_mirror->error_count);
426 }
427
428 static int mirror_available(struct mirror_set *ms, struct bio *bio)
429 {
430         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
431         region_t region = dm_rh_bio_to_region(ms->rh, bio);
432
433         if (log->type->in_sync(log, region, 0))
434                 return choose_mirror(ms,  bio->bi_sector) ? 1 : 0;
435
436         return 0;
437 }
438
439 /*
440  * remap a buffer to a particular mirror.
441  */
442 static sector_t map_sector(struct mirror *m, struct bio *bio)
443 {
444         if (unlikely(!bio->bi_size))
445                 return 0;
446         return m->offset + dm_target_offset(m->ms->ti, bio->bi_sector);
447 }
448
449 static void map_bio(struct mirror *m, struct bio *bio)
450 {
451         bio->bi_bdev = m->dev->bdev;
452         bio->bi_sector = map_sector(m, bio);
453 }
454
455 static void map_region(struct dm_io_region *io, struct mirror *m,
456                        struct bio *bio)
457 {
458         io->bdev = m->dev->bdev;
459         io->sector = map_sector(m, bio);
460         io->count = bio->bi_size >> 9;
461 }
462
463 static void hold_bio(struct mirror_set *ms, struct bio *bio)
464 {
465         /*
466          * Lock is required to avoid race condition during suspend
467          * process.
468          */
469         spin_lock_irq(&ms->lock);
470
471         if (atomic_read(&ms->suspend)) {
472                 spin_unlock_irq(&ms->lock);
473
474                 /*
475                  * If device is suspended, complete the bio.
476                  */
477                 if (dm_noflush_suspending(ms->ti))
478                         bio_endio(bio, DM_ENDIO_REQUEUE);
479                 else
480                         bio_endio(bio, -EIO);
481                 return;
482         }
483
484         /*
485          * Hold bio until the suspend is complete.
486          */
487         bio_list_add(&ms->holds, bio);
488         spin_unlock_irq(&ms->lock);
489 }
490
491 /*-----------------------------------------------------------------
492  * Reads
493  *---------------------------------------------------------------*/
494 static void read_callback(unsigned long error, void *context)
495 {
496         struct bio *bio = context;
497         struct mirror *m;
498
499         m = bio_get_m(bio);
500         bio_set_m(bio, NULL);
501
502         if (likely(!error)) {
503                 bio_endio(bio, 0);
504                 return;
505         }
506
507         fail_mirror(m, DM_RAID1_READ_ERROR);
508
509         if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
510                 DMWARN_LIMIT("Read failure on mirror device %s.  "
511                              "Trying alternative device.",
512                              m->dev->name);
513                 queue_bio(m->ms, bio, bio_rw(bio));
514                 return;
515         }
516
517         DMERR_LIMIT("Read failure on mirror device %s.  Failing I/O.",
518                     m->dev->name);
519         bio_endio(bio, -EIO);
520 }
521
522 /* Asynchronous read. */
523 static void read_async_bio(struct mirror *m, struct bio *bio)
524 {
525         struct dm_io_region io;
526         struct dm_io_request io_req = {
527                 .bi_rw = READ,
528                 .mem.type = DM_IO_BVEC,
529                 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
530                 .notify.fn = read_callback,
531                 .notify.context = bio,
532                 .client = m->ms->io_client,
533         };
534
535         map_region(&io, m, bio);
536         bio_set_m(bio, m);
537         BUG_ON(dm_io(&io_req, 1, &io, NULL));
538 }
539
540 static inline int region_in_sync(struct mirror_set *ms, region_t region,
541                                  int may_block)
542 {
543         int state = dm_rh_get_state(ms->rh, region, may_block);
544         return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
545 }
546
547 static void do_reads(struct mirror_set *ms, struct bio_list *reads)
548 {
549         region_t region;
550         struct bio *bio;
551         struct mirror *m;
552
553         while ((bio = bio_list_pop(reads))) {
554                 region = dm_rh_bio_to_region(ms->rh, bio);
555                 m = get_default_mirror(ms);
556
557                 /*
558                  * We can only read balance if the region is in sync.
559                  */
560                 if (likely(region_in_sync(ms, region, 1)))
561                         m = choose_mirror(ms, bio->bi_sector);
562                 else if (m && atomic_read(&m->error_count))
563                         m = NULL;
564
565                 if (likely(m))
566                         read_async_bio(m, bio);
567                 else
568                         bio_endio(bio, -EIO);
569         }
570 }
571
572 /*-----------------------------------------------------------------
573  * Writes.
574  *
575  * We do different things with the write io depending on the
576  * state of the region that it's in:
577  *
578  * SYNC:        increment pending, use kcopyd to write to *all* mirrors
579  * RECOVERING:  delay the io until recovery completes
580  * NOSYNC:      increment pending, just write to the default mirror
581  *---------------------------------------------------------------*/
582
583
584 static void write_callback(unsigned long error, void *context)
585 {
586         unsigned i, ret = 0;
587         struct bio *bio = (struct bio *) context;
588         struct mirror_set *ms;
589         int should_wake = 0;
590         unsigned long flags;
591
592         ms = bio_get_m(bio)->ms;
593         bio_set_m(bio, NULL);
594
595         /*
596          * NOTE: We don't decrement the pending count here,
597          * instead it is done by the targets endio function.
598          * This way we handle both writes to SYNC and NOSYNC
599          * regions with the same code.
600          */
601         if (likely(!error)) {
602                 bio_endio(bio, ret);
603                 return;
604         }
605
606         /*
607          * If the bio is discard, return an error, but do not
608          * degrade the array.
609          */
610         if (bio->bi_rw & REQ_DISCARD) {
611                 bio_endio(bio, -EOPNOTSUPP);
612                 return;
613         }
614
615         for (i = 0; i < ms->nr_mirrors; i++)
616                 if (test_bit(i, &error))
617                         fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
618
619         /*
620          * Need to raise event.  Since raising
621          * events can block, we need to do it in
622          * the main thread.
623          */
624         spin_lock_irqsave(&ms->lock, flags);
625         if (!ms->failures.head)
626                 should_wake = 1;
627         bio_list_add(&ms->failures, bio);
628         spin_unlock_irqrestore(&ms->lock, flags);
629         if (should_wake)
630                 wakeup_mirrord(ms);
631 }
632
633 static void do_write(struct mirror_set *ms, struct bio *bio)
634 {
635         unsigned int i;
636         struct dm_io_region io[ms->nr_mirrors], *dest = io;
637         struct mirror *m;
638         struct dm_io_request io_req = {
639                 .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA),
640                 .mem.type = DM_IO_BVEC,
641                 .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx,
642                 .notify.fn = write_callback,
643                 .notify.context = bio,
644                 .client = ms->io_client,
645         };
646
647         if (bio->bi_rw & REQ_DISCARD) {
648                 io_req.bi_rw |= REQ_DISCARD;
649                 io_req.mem.type = DM_IO_KMEM;
650                 io_req.mem.ptr.addr = NULL;
651         }
652
653         for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
654                 map_region(dest++, m, bio);
655
656         /*
657          * Use default mirror because we only need it to retrieve the reference
658          * to the mirror set in write_callback().
659          */
660         bio_set_m(bio, get_default_mirror(ms));
661
662         BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
663 }
664
665 static void do_writes(struct mirror_set *ms, struct bio_list *writes)
666 {
667         int state;
668         struct bio *bio;
669         struct bio_list sync, nosync, recover, *this_list = NULL;
670         struct bio_list requeue;
671         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
672         region_t region;
673
674         if (!writes->head)
675                 return;
676
677         /*
678          * Classify each write.
679          */
680         bio_list_init(&sync);
681         bio_list_init(&nosync);
682         bio_list_init(&recover);
683         bio_list_init(&requeue);
684
685         while ((bio = bio_list_pop(writes))) {
686                 if ((bio->bi_rw & REQ_FLUSH) ||
687                     (bio->bi_rw & REQ_DISCARD)) {
688                         bio_list_add(&sync, bio);
689                         continue;
690                 }
691
692                 region = dm_rh_bio_to_region(ms->rh, bio);
693
694                 if (log->type->is_remote_recovering &&
695                     log->type->is_remote_recovering(log, region)) {
696                         bio_list_add(&requeue, bio);
697                         continue;
698                 }
699
700                 state = dm_rh_get_state(ms->rh, region, 1);
701                 switch (state) {
702                 case DM_RH_CLEAN:
703                 case DM_RH_DIRTY:
704                         this_list = &sync;
705                         break;
706
707                 case DM_RH_NOSYNC:
708                         this_list = &nosync;
709                         break;
710
711                 case DM_RH_RECOVERING:
712                         this_list = &recover;
713                         break;
714                 }
715
716                 bio_list_add(this_list, bio);
717         }
718
719         /*
720          * Add bios that are delayed due to remote recovery
721          * back on to the write queue
722          */
723         if (unlikely(requeue.head)) {
724                 spin_lock_irq(&ms->lock);
725                 bio_list_merge(&ms->writes, &requeue);
726                 spin_unlock_irq(&ms->lock);
727                 delayed_wake(ms);
728         }
729
730         /*
731          * Increment the pending counts for any regions that will
732          * be written to (writes to recover regions are going to
733          * be delayed).
734          */
735         dm_rh_inc_pending(ms->rh, &sync);
736         dm_rh_inc_pending(ms->rh, &nosync);
737
738         /*
739          * If the flush fails on a previous call and succeeds here,
740          * we must not reset the log_failure variable.  We need
741          * userspace interaction to do that.
742          */
743         ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
744
745         /*
746          * Dispatch io.
747          */
748         if (unlikely(ms->log_failure) && errors_handled(ms)) {
749                 spin_lock_irq(&ms->lock);
750                 bio_list_merge(&ms->failures, &sync);
751                 spin_unlock_irq(&ms->lock);
752                 wakeup_mirrord(ms);
753         } else
754                 while ((bio = bio_list_pop(&sync)))
755                         do_write(ms, bio);
756
757         while ((bio = bio_list_pop(&recover)))
758                 dm_rh_delay(ms->rh, bio);
759
760         while ((bio = bio_list_pop(&nosync))) {
761                 if (unlikely(ms->leg_failure) && errors_handled(ms)) {
762                         spin_lock_irq(&ms->lock);
763                         bio_list_add(&ms->failures, bio);
764                         spin_unlock_irq(&ms->lock);
765                         wakeup_mirrord(ms);
766                 } else {
767                         map_bio(get_default_mirror(ms), bio);
768                         generic_make_request(bio);
769                 }
770         }
771 }
772
773 static void do_failures(struct mirror_set *ms, struct bio_list *failures)
774 {
775         struct bio *bio;
776
777         if (likely(!failures->head))
778                 return;
779
780         /*
781          * If the log has failed, unattempted writes are being
782          * put on the holds list.  We can't issue those writes
783          * until a log has been marked, so we must store them.
784          *
785          * If a 'noflush' suspend is in progress, we can requeue
786          * the I/O's to the core.  This give userspace a chance
787          * to reconfigure the mirror, at which point the core
788          * will reissue the writes.  If the 'noflush' flag is
789          * not set, we have no choice but to return errors.
790          *
791          * Some writes on the failures list may have been
792          * submitted before the log failure and represent a
793          * failure to write to one of the devices.  It is ok
794          * for us to treat them the same and requeue them
795          * as well.
796          */
797         while ((bio = bio_list_pop(failures))) {
798                 if (!ms->log_failure) {
799                         ms->in_sync = 0;
800                         dm_rh_mark_nosync(ms->rh, bio);
801                 }
802
803                 /*
804                  * If all the legs are dead, fail the I/O.
805                  * If we have been told to handle errors, hold the bio
806                  * and wait for userspace to deal with the problem.
807                  * Otherwise pretend that the I/O succeeded. (This would
808                  * be wrong if the failed leg returned after reboot and
809                  * got replicated back to the good legs.)
810                  */
811                 if (!get_valid_mirror(ms))
812                         bio_endio(bio, -EIO);
813                 else if (errors_handled(ms))
814                         hold_bio(ms, bio);
815                 else
816                         bio_endio(bio, 0);
817         }
818 }
819
820 static void trigger_event(struct work_struct *work)
821 {
822         struct mirror_set *ms =
823                 container_of(work, struct mirror_set, trigger_event);
824
825         dm_table_event(ms->ti->table);
826 }
827
828 /*-----------------------------------------------------------------
829  * kmirrord
830  *---------------------------------------------------------------*/
831 static void do_mirror(struct work_struct *work)
832 {
833         struct mirror_set *ms = container_of(work, struct mirror_set,
834                                              kmirrord_work);
835         struct bio_list reads, writes, failures;
836         unsigned long flags;
837
838         spin_lock_irqsave(&ms->lock, flags);
839         reads = ms->reads;
840         writes = ms->writes;
841         failures = ms->failures;
842         bio_list_init(&ms->reads);
843         bio_list_init(&ms->writes);
844         bio_list_init(&ms->failures);
845         spin_unlock_irqrestore(&ms->lock, flags);
846
847         dm_rh_update_states(ms->rh, errors_handled(ms));
848         do_recovery(ms);
849         do_reads(ms, &reads);
850         do_writes(ms, &writes);
851         do_failures(ms, &failures);
852 }
853
854 /*-----------------------------------------------------------------
855  * Target functions
856  *---------------------------------------------------------------*/
857 static struct mirror_set *alloc_context(unsigned int nr_mirrors,
858                                         uint32_t region_size,
859                                         struct dm_target *ti,
860                                         struct dm_dirty_log *dl)
861 {
862         size_t len;
863         struct mirror_set *ms = NULL;
864
865         len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors);
866
867         ms = kzalloc(len, GFP_KERNEL);
868         if (!ms) {
869                 ti->error = "Cannot allocate mirror context";
870                 return NULL;
871         }
872
873         spin_lock_init(&ms->lock);
874         bio_list_init(&ms->reads);
875         bio_list_init(&ms->writes);
876         bio_list_init(&ms->failures);
877         bio_list_init(&ms->holds);
878
879         ms->ti = ti;
880         ms->nr_mirrors = nr_mirrors;
881         ms->nr_regions = dm_sector_div_up(ti->len, region_size);
882         ms->in_sync = 0;
883         ms->log_failure = 0;
884         ms->leg_failure = 0;
885         atomic_set(&ms->suspend, 0);
886         atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
887
888         ms->read_record_pool = mempool_create_slab_pool(MIN_READ_RECORDS,
889                                                 _dm_raid1_read_record_cache);
890
891         if (!ms->read_record_pool) {
892                 ti->error = "Error creating mirror read_record_pool";
893                 kfree(ms);
894                 return NULL;
895         }
896
897         ms->io_client = dm_io_client_create();
898         if (IS_ERR(ms->io_client)) {
899                 ti->error = "Error creating dm_io client";
900                 mempool_destroy(ms->read_record_pool);
901                 kfree(ms);
902                 return NULL;
903         }
904
905         ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
906                                        wakeup_all_recovery_waiters,
907                                        ms->ti->begin, MAX_RECOVERY,
908                                        dl, region_size, ms->nr_regions);
909         if (IS_ERR(ms->rh)) {
910                 ti->error = "Error creating dirty region hash";
911                 dm_io_client_destroy(ms->io_client);
912                 mempool_destroy(ms->read_record_pool);
913                 kfree(ms);
914                 return NULL;
915         }
916
917         return ms;
918 }
919
920 static void free_context(struct mirror_set *ms, struct dm_target *ti,
921                          unsigned int m)
922 {
923         while (m--)
924                 dm_put_device(ti, ms->mirror[m].dev);
925
926         dm_io_client_destroy(ms->io_client);
927         dm_region_hash_destroy(ms->rh);
928         mempool_destroy(ms->read_record_pool);
929         kfree(ms);
930 }
931
932 static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
933                       unsigned int mirror, char **argv)
934 {
935         unsigned long long offset;
936
937         if (sscanf(argv[1], "%llu", &offset) != 1) {
938                 ti->error = "Invalid offset";
939                 return -EINVAL;
940         }
941
942         if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
943                           &ms->mirror[mirror].dev)) {
944                 ti->error = "Device lookup failure";
945                 return -ENXIO;
946         }
947
948         ms->mirror[mirror].ms = ms;
949         atomic_set(&(ms->mirror[mirror].error_count), 0);
950         ms->mirror[mirror].error_type = 0;
951         ms->mirror[mirror].offset = offset;
952
953         return 0;
954 }
955
956 /*
957  * Create dirty log: log_type #log_params <log_params>
958  */
959 static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
960                                              unsigned argc, char **argv,
961                                              unsigned *args_used)
962 {
963         unsigned param_count;
964         struct dm_dirty_log *dl;
965
966         if (argc < 2) {
967                 ti->error = "Insufficient mirror log arguments";
968                 return NULL;
969         }
970
971         if (sscanf(argv[1], "%u", &param_count) != 1) {
972                 ti->error = "Invalid mirror log argument count";
973                 return NULL;
974         }
975
976         *args_used = 2 + param_count;
977
978         if (argc < *args_used) {
979                 ti->error = "Insufficient mirror log arguments";
980                 return NULL;
981         }
982
983         dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
984                                  argv + 2);
985         if (!dl) {
986                 ti->error = "Error creating mirror dirty log";
987                 return NULL;
988         }
989
990         return dl;
991 }
992
993 static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
994                           unsigned *args_used)
995 {
996         unsigned num_features;
997         struct dm_target *ti = ms->ti;
998
999         *args_used = 0;
1000
1001         if (!argc)
1002                 return 0;
1003
1004         if (sscanf(argv[0], "%u", &num_features) != 1) {
1005                 ti->error = "Invalid number of features";
1006                 return -EINVAL;
1007         }
1008
1009         argc--;
1010         argv++;
1011         (*args_used)++;
1012
1013         if (num_features > argc) {
1014                 ti->error = "Not enough arguments to support feature count";
1015                 return -EINVAL;
1016         }
1017
1018         if (!strcmp("handle_errors", argv[0]))
1019                 ms->features |= DM_RAID1_HANDLE_ERRORS;
1020         else {
1021                 ti->error = "Unrecognised feature requested";
1022                 return -EINVAL;
1023         }
1024
1025         (*args_used)++;
1026
1027         return 0;
1028 }
1029
1030 /*
1031  * Construct a mirror mapping:
1032  *
1033  * log_type #log_params <log_params>
1034  * #mirrors [mirror_path offset]{2,}
1035  * [#features <features>]
1036  *
1037  * log_type is "core" or "disk"
1038  * #log_params is between 1 and 3
1039  *
1040  * If present, features must be "handle_errors".
1041  */
1042 static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1043 {
1044         int r;
1045         unsigned int nr_mirrors, m, args_used;
1046         struct mirror_set *ms;
1047         struct dm_dirty_log *dl;
1048
1049         dl = create_dirty_log(ti, argc, argv, &args_used);
1050         if (!dl)
1051                 return -EINVAL;
1052
1053         argv += args_used;
1054         argc -= args_used;
1055
1056         if (!argc || sscanf(argv[0], "%u", &nr_mirrors) != 1 ||
1057             nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) {
1058                 ti->error = "Invalid number of mirrors";
1059                 dm_dirty_log_destroy(dl);
1060                 return -EINVAL;
1061         }
1062
1063         argv++, argc--;
1064
1065         if (argc < nr_mirrors * 2) {
1066                 ti->error = "Too few mirror arguments";
1067                 dm_dirty_log_destroy(dl);
1068                 return -EINVAL;
1069         }
1070
1071         ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
1072         if (!ms) {
1073                 dm_dirty_log_destroy(dl);
1074                 return -ENOMEM;
1075         }
1076
1077         /* Get the mirror parameter sets */
1078         for (m = 0; m < nr_mirrors; m++) {
1079                 r = get_mirror(ms, ti, m, argv);
1080                 if (r) {
1081                         free_context(ms, ti, m);
1082                         return r;
1083                 }
1084                 argv += 2;
1085                 argc -= 2;
1086         }
1087
1088         ti->private = ms;
1089         ti->split_io = dm_rh_get_region_size(ms->rh);
1090         ti->num_flush_requests = 1;
1091         ti->num_discard_requests = 1;
1092         ti->discard_zeroes_data_unsupported = 1;
1093
1094         ms->kmirrord_wq = alloc_workqueue("kmirrord",
1095                                           WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1096         if (!ms->kmirrord_wq) {
1097                 DMERR("couldn't start kmirrord");
1098                 r = -ENOMEM;
1099                 goto err_free_context;
1100         }
1101         INIT_WORK(&ms->kmirrord_work, do_mirror);
1102         init_timer(&ms->timer);
1103         ms->timer_pending = 0;
1104         INIT_WORK(&ms->trigger_event, trigger_event);
1105
1106         r = parse_features(ms, argc, argv, &args_used);
1107         if (r)
1108                 goto err_destroy_wq;
1109
1110         argv += args_used;
1111         argc -= args_used;
1112
1113         /*
1114          * Any read-balancing addition depends on the
1115          * DM_RAID1_HANDLE_ERRORS flag being present.
1116          * This is because the decision to balance depends
1117          * on the sync state of a region.  If the above
1118          * flag is not present, we ignore errors; and
1119          * the sync state may be inaccurate.
1120          */
1121
1122         if (argc) {
1123                 ti->error = "Too many mirror arguments";
1124                 r = -EINVAL;
1125                 goto err_destroy_wq;
1126         }
1127
1128         ms->kcopyd_client = dm_kcopyd_client_create();
1129         if (IS_ERR(ms->kcopyd_client)) {
1130                 r = PTR_ERR(ms->kcopyd_client);
1131                 goto err_destroy_wq;
1132         }
1133
1134         wakeup_mirrord(ms);
1135         return 0;
1136
1137 err_destroy_wq:
1138         destroy_workqueue(ms->kmirrord_wq);
1139 err_free_context:
1140         free_context(ms, ti, ms->nr_mirrors);
1141         return r;
1142 }
1143
1144 static void mirror_dtr(struct dm_target *ti)
1145 {
1146         struct mirror_set *ms = (struct mirror_set *) ti->private;
1147
1148         del_timer_sync(&ms->timer);
1149         flush_workqueue(ms->kmirrord_wq);
1150         flush_work_sync(&ms->trigger_event);
1151         dm_kcopyd_client_destroy(ms->kcopyd_client);
1152         destroy_workqueue(ms->kmirrord_wq);
1153         free_context(ms, ti, ms->nr_mirrors);
1154 }
1155
1156 /*
1157  * Mirror mapping function
1158  */
1159 static int mirror_map(struct dm_target *ti, struct bio *bio,
1160                       union map_info *map_context)
1161 {
1162         int r, rw = bio_rw(bio);
1163         struct mirror *m;
1164         struct mirror_set *ms = ti->private;
1165         struct dm_raid1_read_record *read_record = NULL;
1166         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1167
1168         if (rw == WRITE) {
1169                 /* Save region for mirror_end_io() handler */
1170                 map_context->ll = dm_rh_bio_to_region(ms->rh, bio);
1171                 queue_bio(ms, bio, rw);
1172                 return DM_MAPIO_SUBMITTED;
1173         }
1174
1175         r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1176         if (r < 0 && r != -EWOULDBLOCK)
1177                 return r;
1178
1179         /*
1180          * If region is not in-sync queue the bio.
1181          */
1182         if (!r || (r == -EWOULDBLOCK)) {
1183                 if (rw == READA)
1184                         return -EWOULDBLOCK;
1185
1186                 queue_bio(ms, bio, rw);
1187                 return DM_MAPIO_SUBMITTED;
1188         }
1189
1190         /*
1191          * The region is in-sync and we can perform reads directly.
1192          * Store enough information so we can retry if it fails.
1193          */
1194         m = choose_mirror(ms, bio->bi_sector);
1195         if (unlikely(!m))
1196                 return -EIO;
1197
1198         read_record = mempool_alloc(ms->read_record_pool, GFP_NOIO);
1199         if (likely(read_record)) {
1200                 dm_bio_record(&read_record->details, bio);
1201                 map_context->ptr = read_record;
1202                 read_record->m = m;
1203         }
1204
1205         map_bio(m, bio);
1206
1207         return DM_MAPIO_REMAPPED;
1208 }
1209
1210 static int mirror_end_io(struct dm_target *ti, struct bio *bio,
1211                          int error, union map_info *map_context)
1212 {
1213         int rw = bio_rw(bio);
1214         struct mirror_set *ms = (struct mirror_set *) ti->private;
1215         struct mirror *m = NULL;
1216         struct dm_bio_details *bd = NULL;
1217         struct dm_raid1_read_record *read_record = map_context->ptr;
1218
1219         /*
1220          * We need to dec pending if this was a write.
1221          */
1222         if (rw == WRITE) {
1223                 if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD)))
1224                         dm_rh_dec(ms->rh, map_context->ll);
1225                 return error;
1226         }
1227
1228         if (error == -EOPNOTSUPP)
1229                 goto out;
1230
1231         if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD))
1232                 goto out;
1233
1234         if (unlikely(error)) {
1235                 if (!read_record) {
1236                         /*
1237                          * There wasn't enough memory to record necessary
1238                          * information for a retry or there was no other
1239                          * mirror in-sync.
1240                          */
1241                         DMERR_LIMIT("Mirror read failed.");
1242                         return -EIO;
1243                 }
1244
1245                 m = read_record->m;
1246
1247                 DMERR("Mirror read failed from %s. Trying alternative device.",
1248                       m->dev->name);
1249
1250                 fail_mirror(m, DM_RAID1_READ_ERROR);
1251
1252                 /*
1253                  * A failed read is requeued for another attempt using an intact
1254                  * mirror.
1255                  */
1256                 if (default_ok(m) || mirror_available(ms, bio)) {
1257                         bd = &read_record->details;
1258
1259                         dm_bio_restore(bd, bio);
1260                         mempool_free(read_record, ms->read_record_pool);
1261                         map_context->ptr = NULL;
1262                         queue_bio(ms, bio, rw);
1263                         return 1;
1264                 }
1265                 DMERR("All replicated volumes dead, failing I/O");
1266         }
1267
1268 out:
1269         if (read_record) {
1270                 mempool_free(read_record, ms->read_record_pool);
1271                 map_context->ptr = NULL;
1272         }
1273
1274         return error;
1275 }
1276
1277 static void mirror_presuspend(struct dm_target *ti)
1278 {
1279         struct mirror_set *ms = (struct mirror_set *) ti->private;
1280         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1281
1282         struct bio_list holds;
1283         struct bio *bio;
1284
1285         atomic_set(&ms->suspend, 1);
1286
1287         /*
1288          * Process bios in the hold list to start recovery waiting
1289          * for bios in the hold list. After the process, no bio has
1290          * a chance to be added in the hold list because ms->suspend
1291          * is set.
1292          */
1293         spin_lock_irq(&ms->lock);
1294         holds = ms->holds;
1295         bio_list_init(&ms->holds);
1296         spin_unlock_irq(&ms->lock);
1297
1298         while ((bio = bio_list_pop(&holds)))
1299                 hold_bio(ms, bio);
1300
1301         /*
1302          * We must finish up all the work that we've
1303          * generated (i.e. recovery work).
1304          */
1305         dm_rh_stop_recovery(ms->rh);
1306
1307         wait_event(_kmirrord_recovery_stopped,
1308                    !dm_rh_recovery_in_flight(ms->rh));
1309
1310         if (log->type->presuspend && log->type->presuspend(log))
1311                 /* FIXME: need better error handling */
1312                 DMWARN("log presuspend failed");
1313
1314         /*
1315          * Now that recovery is complete/stopped and the
1316          * delayed bios are queued, we need to wait for
1317          * the worker thread to complete.  This way,
1318          * we know that all of our I/O has been pushed.
1319          */
1320         flush_workqueue(ms->kmirrord_wq);
1321 }
1322
1323 static void mirror_postsuspend(struct dm_target *ti)
1324 {
1325         struct mirror_set *ms = ti->private;
1326         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1327
1328         if (log->type->postsuspend && log->type->postsuspend(log))
1329                 /* FIXME: need better error handling */
1330                 DMWARN("log postsuspend failed");
1331 }
1332
1333 static void mirror_resume(struct dm_target *ti)
1334 {
1335         struct mirror_set *ms = ti->private;
1336         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1337
1338         atomic_set(&ms->suspend, 0);
1339         if (log->type->resume && log->type->resume(log))
1340                 /* FIXME: need better error handling */
1341                 DMWARN("log resume failed");
1342         dm_rh_start_recovery(ms->rh);
1343 }
1344
1345 /*
1346  * device_status_char
1347  * @m: mirror device/leg we want the status of
1348  *
1349  * We return one character representing the most severe error
1350  * we have encountered.
1351  *    A => Alive - No failures
1352  *    D => Dead - A write failure occurred leaving mirror out-of-sync
1353  *    S => Sync - A sychronization failure occurred, mirror out-of-sync
1354  *    R => Read - A read failure occurred, mirror data unaffected
1355  *
1356  * Returns: <char>
1357  */
1358 static char device_status_char(struct mirror *m)
1359 {
1360         if (!atomic_read(&(m->error_count)))
1361                 return 'A';
1362
1363         return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
1364                 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
1365                 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
1366                 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
1367 }
1368
1369
1370 static void mirror_status(struct dm_target *ti, status_type_t type,
1371                           char *result, unsigned maxlen)
1372 {
1373         unsigned int m, sz = 0;
1374         struct mirror_set *ms = (struct mirror_set *) ti->private;
1375         struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1376         char buffer[ms->nr_mirrors + 1];
1377
1378         switch (type) {
1379         case STATUSTYPE_INFO:
1380                 DMEMIT("%d ", ms->nr_mirrors);
1381                 for (m = 0; m < ms->nr_mirrors; m++) {
1382                         DMEMIT("%s ", ms->mirror[m].dev->name);
1383                         buffer[m] = device_status_char(&(ms->mirror[m]));
1384                 }
1385                 buffer[m] = '\0';
1386
1387                 DMEMIT("%llu/%llu 1 %s ",
1388                       (unsigned long long)log->type->get_sync_count(log),
1389                       (unsigned long long)ms->nr_regions, buffer);
1390
1391                 sz += log->type->status(log, type, result+sz, maxlen-sz);
1392
1393                 break;
1394
1395         case STATUSTYPE_TABLE:
1396                 sz = log->type->status(log, type, result, maxlen);
1397
1398                 DMEMIT("%d", ms->nr_mirrors);
1399                 for (m = 0; m < ms->nr_mirrors; m++)
1400                         DMEMIT(" %s %llu", ms->mirror[m].dev->name,
1401                                (unsigned long long)ms->mirror[m].offset);
1402
1403                 if (ms->features & DM_RAID1_HANDLE_ERRORS)
1404                         DMEMIT(" 1 handle_errors");
1405         }
1406 }
1407
1408 static int mirror_iterate_devices(struct dm_target *ti,
1409                                   iterate_devices_callout_fn fn, void *data)
1410 {
1411         struct mirror_set *ms = ti->private;
1412         int ret = 0;
1413         unsigned i;
1414
1415         for (i = 0; !ret && i < ms->nr_mirrors; i++)
1416                 ret = fn(ti, ms->mirror[i].dev,
1417                          ms->mirror[i].offset, ti->len, data);
1418
1419         return ret;
1420 }
1421
1422 static struct target_type mirror_target = {
1423         .name    = "mirror",
1424         .version = {1, 12, 2},
1425         .module  = THIS_MODULE,
1426         .ctr     = mirror_ctr,
1427         .dtr     = mirror_dtr,
1428         .map     = mirror_map,
1429         .end_io  = mirror_end_io,
1430         .presuspend = mirror_presuspend,
1431         .postsuspend = mirror_postsuspend,
1432         .resume  = mirror_resume,
1433         .status  = mirror_status,
1434         .iterate_devices = mirror_iterate_devices,
1435 };
1436
1437 static int __init dm_mirror_init(void)
1438 {
1439         int r;
1440
1441         _dm_raid1_read_record_cache = KMEM_CACHE(dm_raid1_read_record, 0);
1442         if (!_dm_raid1_read_record_cache) {
1443                 DMERR("Can't allocate dm_raid1_read_record cache");
1444                 r = -ENOMEM;
1445                 goto bad_cache;
1446         }
1447
1448         r = dm_register_target(&mirror_target);
1449         if (r < 0) {
1450                 DMERR("Failed to register mirror target");
1451                 goto bad_target;
1452         }
1453
1454         return 0;
1455
1456 bad_target:
1457         kmem_cache_destroy(_dm_raid1_read_record_cache);
1458 bad_cache:
1459         return r;
1460 }
1461
1462 static void __exit dm_mirror_exit(void)
1463 {
1464         dm_unregister_target(&mirror_target);
1465         kmem_cache_destroy(_dm_raid1_read_record_cache);
1466 }
1467
1468 /* Module hooks */
1469 module_init(dm_mirror_init);
1470 module_exit(dm_mirror_exit);
1471
1472 MODULE_DESCRIPTION(DM_NAME " mirror target");
1473 MODULE_AUTHOR("Joe Thornber");
1474 MODULE_LICENSE("GPL");