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