2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
8 #include "dm-bio-record.h"
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>
22 #define DM_MSG_PREFIX "raid1"
24 #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
26 #define DM_RAID1_HANDLE_ERRORS 0x01
27 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
29 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped
);
31 /*-----------------------------------------------------------------
32 * Mirror set structures.
33 *---------------------------------------------------------------*/
42 struct mirror_set
*ms
;
44 unsigned long error_type
;
51 struct list_head list
;
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 */
61 struct dm_region_hash
*rh
;
62 struct dm_kcopyd_client
*kcopyd_client
;
63 struct dm_io_client
*io_client
;
72 atomic_t default_mirror
; /* Default mirror */
74 struct workqueue_struct
*kmirrord_wq
;
75 struct work_struct kmirrord_work
;
76 struct timer_list timer
;
77 unsigned long timer_pending
;
79 struct work_struct trigger_event
;
82 struct mirror mirror
[0];
85 static void wakeup_mirrord(void *context
)
87 struct mirror_set
*ms
= context
;
89 queue_work(ms
->kmirrord_wq
, &ms
->kmirrord_work
);
92 static void delayed_wake_fn(unsigned long data
)
94 struct mirror_set
*ms
= (struct mirror_set
*) data
;
96 clear_bit(0, &ms
->timer_pending
);
100 static void delayed_wake(struct mirror_set
*ms
)
102 if (test_and_set_bit(0, &ms
->timer_pending
))
105 ms
->timer
.expires
= jiffies
+ HZ
/ 5;
106 ms
->timer
.data
= (unsigned long) ms
;
107 ms
->timer
.function
= delayed_wake_fn
;
108 add_timer(&ms
->timer
);
111 static void wakeup_all_recovery_waiters(void *context
)
113 wake_up_all(&_kmirrord_recovery_stopped
);
116 static void queue_bio(struct mirror_set
*ms
, struct bio
*bio
, int rw
)
122 bl
= (rw
== WRITE
) ? &ms
->writes
: &ms
->reads
;
123 spin_lock_irqsave(&ms
->lock
, flags
);
124 should_wake
= !(bl
->head
);
125 bio_list_add(bl
, bio
);
126 spin_unlock_irqrestore(&ms
->lock
, flags
);
132 static void dispatch_bios(void *context
, struct bio_list
*bio_list
)
134 struct mirror_set
*ms
= context
;
137 while ((bio
= bio_list_pop(bio_list
)))
138 queue_bio(ms
, bio
, WRITE
);
141 struct dm_raid1_bio_record
{
143 /* if details->bi_bdev == NULL, details were not saved */
144 struct dm_bio_details details
;
145 region_t write_region
;
149 * Every mirror should look like this one.
151 #define DEFAULT_MIRROR 0
154 * This is yucky. We squirrel the mirror struct away inside
155 * bi_next for read/write buffers. This is safe since the bh
156 * doesn't get submitted to the lower levels of block layer.
158 static struct mirror
*bio_get_m(struct bio
*bio
)
160 return (struct mirror
*) bio
->bi_next
;
163 static void bio_set_m(struct bio
*bio
, struct mirror
*m
)
165 bio
->bi_next
= (struct bio
*) m
;
168 static struct mirror
*get_default_mirror(struct mirror_set
*ms
)
170 return &ms
->mirror
[atomic_read(&ms
->default_mirror
)];
173 static void set_default_mirror(struct mirror
*m
)
175 struct mirror_set
*ms
= m
->ms
;
176 struct mirror
*m0
= &(ms
->mirror
[0]);
178 atomic_set(&ms
->default_mirror
, m
- m0
);
181 static struct mirror
*get_valid_mirror(struct mirror_set
*ms
)
185 for (m
= ms
->mirror
; m
< ms
->mirror
+ ms
->nr_mirrors
; m
++)
186 if (!atomic_read(&m
->error_count
))
193 * @m: mirror device to fail
194 * @error_type: one of the enum's, DM_RAID1_*_ERROR
196 * If errors are being handled, record the type of
197 * error encountered for this device. If this type
198 * of error has already been recorded, we can return;
199 * otherwise, we must signal userspace by triggering
200 * an event. Additionally, if the device is the
201 * primary device, we must choose a new primary, but
202 * only if the mirror is in-sync.
204 * This function must not block.
206 static void fail_mirror(struct mirror
*m
, enum dm_raid1_error error_type
)
208 struct mirror_set
*ms
= m
->ms
;
214 * error_count is used for nothing more than a
215 * simple way to tell if a device has encountered
218 atomic_inc(&m
->error_count
);
220 if (test_and_set_bit(error_type
, &m
->error_type
))
223 if (!errors_handled(ms
))
226 if (m
!= get_default_mirror(ms
))
231 * Better to issue requests to same failing device
232 * than to risk returning corrupt data.
234 DMERR("Primary mirror (%s) failed while out-of-sync: "
235 "Reads may fail.", m
->dev
->name
);
239 new = get_valid_mirror(ms
);
241 set_default_mirror(new);
243 DMWARN("All sides of mirror have failed.");
246 schedule_work(&ms
->trigger_event
);
249 static int mirror_flush(struct dm_target
*ti
)
251 struct mirror_set
*ms
= ti
->private;
252 unsigned long error_bits
;
255 struct dm_io_region io
[ms
->nr_mirrors
];
257 struct dm_io_request io_req
= {
258 .bi_rw
= WRITE_FLUSH
,
259 .mem
.type
= DM_IO_KMEM
,
260 .mem
.ptr
.addr
= NULL
,
261 .client
= ms
->io_client
,
264 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++) {
265 io
[i
].bdev
= m
->dev
->bdev
;
271 dm_io(&io_req
, ms
->nr_mirrors
, io
, &error_bits
);
272 if (unlikely(error_bits
!= 0)) {
273 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
274 if (test_bit(i
, &error_bits
))
275 fail_mirror(ms
->mirror
+ i
,
276 DM_RAID1_FLUSH_ERROR
);
283 /*-----------------------------------------------------------------
286 * When a mirror is first activated we may find that some regions
287 * are in the no-sync state. We have to recover these by
288 * recopying from the default mirror to all the others.
289 *---------------------------------------------------------------*/
290 static void recovery_complete(int read_err
, unsigned long write_err
,
293 struct dm_region
*reg
= context
;
294 struct mirror_set
*ms
= dm_rh_region_context(reg
);
298 /* Read error means the failure of default mirror. */
299 DMERR_LIMIT("Unable to read primary mirror during recovery");
300 fail_mirror(get_default_mirror(ms
), DM_RAID1_SYNC_ERROR
);
304 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
307 * Bits correspond to devices (excluding default mirror).
308 * The default mirror cannot change during recovery.
310 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
311 if (&ms
->mirror
[m
] == get_default_mirror(ms
))
313 if (test_bit(bit
, &write_err
))
314 fail_mirror(ms
->mirror
+ m
,
315 DM_RAID1_SYNC_ERROR
);
320 dm_rh_recovery_end(reg
, !(read_err
|| write_err
));
323 static int recover(struct mirror_set
*ms
, struct dm_region
*reg
)
327 struct dm_io_region from
, to
[DM_KCOPYD_MAX_REGIONS
], *dest
;
329 unsigned long flags
= 0;
330 region_t key
= dm_rh_get_region_key(reg
);
331 sector_t region_size
= dm_rh_get_region_size(ms
->rh
);
333 /* fill in the source */
334 m
= get_default_mirror(ms
);
335 from
.bdev
= m
->dev
->bdev
;
336 from
.sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
337 if (key
== (ms
->nr_regions
- 1)) {
339 * The final region may be smaller than
342 from
.count
= ms
->ti
->len
& (region_size
- 1);
344 from
.count
= region_size
;
346 from
.count
= region_size
;
348 /* fill in the destinations */
349 for (i
= 0, dest
= to
; i
< ms
->nr_mirrors
; i
++) {
350 if (&ms
->mirror
[i
] == get_default_mirror(ms
))
354 dest
->bdev
= m
->dev
->bdev
;
355 dest
->sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
356 dest
->count
= from
.count
;
361 if (!errors_handled(ms
))
362 set_bit(DM_KCOPYD_IGNORE_ERROR
, &flags
);
364 r
= dm_kcopyd_copy(ms
->kcopyd_client
, &from
, ms
->nr_mirrors
- 1, to
,
365 flags
, recovery_complete
, reg
);
370 static void do_recovery(struct mirror_set
*ms
)
372 struct dm_region
*reg
;
373 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
377 * Start quiescing some regions.
379 dm_rh_recovery_prepare(ms
->rh
);
382 * Copy any already quiesced regions.
384 while ((reg
= dm_rh_recovery_start(ms
->rh
))) {
385 r
= recover(ms
, reg
);
387 dm_rh_recovery_end(reg
, 0);
391 * Update the in sync flag.
394 (log
->type
->get_sync_count(log
) == ms
->nr_regions
)) {
395 /* the sync is complete */
396 dm_table_event(ms
->ti
->table
);
401 /*-----------------------------------------------------------------
403 *---------------------------------------------------------------*/
404 static struct mirror
*choose_mirror(struct mirror_set
*ms
, sector_t sector
)
406 struct mirror
*m
= get_default_mirror(ms
);
409 if (likely(!atomic_read(&m
->error_count
)))
412 if (m
-- == ms
->mirror
)
414 } while (m
!= get_default_mirror(ms
));
419 static int default_ok(struct mirror
*m
)
421 struct mirror
*default_mirror
= get_default_mirror(m
->ms
);
423 return !atomic_read(&default_mirror
->error_count
);
426 static int mirror_available(struct mirror_set
*ms
, struct bio
*bio
)
428 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
429 region_t region
= dm_rh_bio_to_region(ms
->rh
, bio
);
431 if (log
->type
->in_sync(log
, region
, 0))
432 return choose_mirror(ms
, bio
->bi_sector
) ? 1 : 0;
438 * remap a buffer to a particular mirror.
440 static sector_t
map_sector(struct mirror
*m
, struct bio
*bio
)
442 if (unlikely(!bio
->bi_size
))
444 return m
->offset
+ dm_target_offset(m
->ms
->ti
, bio
->bi_sector
);
447 static void map_bio(struct mirror
*m
, struct bio
*bio
)
449 bio
->bi_bdev
= m
->dev
->bdev
;
450 bio
->bi_sector
= map_sector(m
, bio
);
453 static void map_region(struct dm_io_region
*io
, struct mirror
*m
,
456 io
->bdev
= m
->dev
->bdev
;
457 io
->sector
= map_sector(m
, bio
);
458 io
->count
= bio
->bi_size
>> 9;
461 static void hold_bio(struct mirror_set
*ms
, struct bio
*bio
)
464 * Lock is required to avoid race condition during suspend
467 spin_lock_irq(&ms
->lock
);
469 if (atomic_read(&ms
->suspend
)) {
470 spin_unlock_irq(&ms
->lock
);
473 * If device is suspended, complete the bio.
475 if (dm_noflush_suspending(ms
->ti
))
476 bio_endio(bio
, DM_ENDIO_REQUEUE
);
478 bio_endio(bio
, -EIO
);
483 * Hold bio until the suspend is complete.
485 bio_list_add(&ms
->holds
, bio
);
486 spin_unlock_irq(&ms
->lock
);
489 /*-----------------------------------------------------------------
491 *---------------------------------------------------------------*/
492 static void read_callback(unsigned long error
, void *context
)
494 struct bio
*bio
= context
;
498 bio_set_m(bio
, NULL
);
500 if (likely(!error
)) {
505 fail_mirror(m
, DM_RAID1_READ_ERROR
);
507 if (likely(default_ok(m
)) || mirror_available(m
->ms
, bio
)) {
508 DMWARN_LIMIT("Read failure on mirror device %s. "
509 "Trying alternative device.",
511 queue_bio(m
->ms
, bio
, bio_rw(bio
));
515 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
517 bio_endio(bio
, -EIO
);
520 /* Asynchronous read. */
521 static void read_async_bio(struct mirror
*m
, struct bio
*bio
)
523 struct dm_io_region io
;
524 struct dm_io_request io_req
= {
526 .mem
.type
= DM_IO_BVEC
,
527 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
528 .notify
.fn
= read_callback
,
529 .notify
.context
= bio
,
530 .client
= m
->ms
->io_client
,
533 map_region(&io
, m
, bio
);
535 BUG_ON(dm_io(&io_req
, 1, &io
, NULL
));
538 static inline int region_in_sync(struct mirror_set
*ms
, region_t region
,
541 int state
= dm_rh_get_state(ms
->rh
, region
, may_block
);
542 return state
== DM_RH_CLEAN
|| state
== DM_RH_DIRTY
;
545 static void do_reads(struct mirror_set
*ms
, struct bio_list
*reads
)
551 while ((bio
= bio_list_pop(reads
))) {
552 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
553 m
= get_default_mirror(ms
);
556 * We can only read balance if the region is in sync.
558 if (likely(region_in_sync(ms
, region
, 1)))
559 m
= choose_mirror(ms
, bio
->bi_sector
);
560 else if (m
&& atomic_read(&m
->error_count
))
564 read_async_bio(m
, bio
);
566 bio_endio(bio
, -EIO
);
570 /*-----------------------------------------------------------------
573 * We do different things with the write io depending on the
574 * state of the region that it's in:
576 * SYNC: increment pending, use kcopyd to write to *all* mirrors
577 * RECOVERING: delay the io until recovery completes
578 * NOSYNC: increment pending, just write to the default mirror
579 *---------------------------------------------------------------*/
582 static void write_callback(unsigned long error
, void *context
)
585 struct bio
*bio
= (struct bio
*) context
;
586 struct mirror_set
*ms
;
590 ms
= bio_get_m(bio
)->ms
;
591 bio_set_m(bio
, NULL
);
594 * NOTE: We don't decrement the pending count here,
595 * instead it is done by the targets endio function.
596 * This way we handle both writes to SYNC and NOSYNC
597 * regions with the same code.
599 if (likely(!error
)) {
604 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
605 if (test_bit(i
, &error
))
606 fail_mirror(ms
->mirror
+ i
, DM_RAID1_WRITE_ERROR
);
609 * Need to raise event. Since raising
610 * events can block, we need to do it in
613 spin_lock_irqsave(&ms
->lock
, flags
);
614 if (!ms
->failures
.head
)
616 bio_list_add(&ms
->failures
, bio
);
617 spin_unlock_irqrestore(&ms
->lock
, flags
);
622 static void do_write(struct mirror_set
*ms
, struct bio
*bio
)
625 struct dm_io_region io
[ms
->nr_mirrors
], *dest
= io
;
627 struct dm_io_request io_req
= {
628 .bi_rw
= WRITE
| (bio
->bi_rw
& WRITE_FLUSH_FUA
),
629 .mem
.type
= DM_IO_BVEC
,
630 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
631 .notify
.fn
= write_callback
,
632 .notify
.context
= bio
,
633 .client
= ms
->io_client
,
636 if (bio
->bi_rw
& REQ_DISCARD
) {
637 io_req
.bi_rw
|= REQ_DISCARD
;
638 io_req
.mem
.type
= DM_IO_KMEM
;
639 io_req
.mem
.ptr
.addr
= NULL
;
642 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++)
643 map_region(dest
++, m
, bio
);
646 * Use default mirror because we only need it to retrieve the reference
647 * to the mirror set in write_callback().
649 bio_set_m(bio
, get_default_mirror(ms
));
651 BUG_ON(dm_io(&io_req
, ms
->nr_mirrors
, io
, NULL
));
654 static void do_writes(struct mirror_set
*ms
, struct bio_list
*writes
)
658 struct bio_list sync
, nosync
, recover
, *this_list
= NULL
;
659 struct bio_list requeue
;
660 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
667 * Classify each write.
669 bio_list_init(&sync
);
670 bio_list_init(&nosync
);
671 bio_list_init(&recover
);
672 bio_list_init(&requeue
);
674 while ((bio
= bio_list_pop(writes
))) {
675 if ((bio
->bi_rw
& REQ_FLUSH
) ||
676 (bio
->bi_rw
& REQ_DISCARD
)) {
677 bio_list_add(&sync
, bio
);
681 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
683 if (log
->type
->is_remote_recovering
&&
684 log
->type
->is_remote_recovering(log
, region
)) {
685 bio_list_add(&requeue
, bio
);
689 state
= dm_rh_get_state(ms
->rh
, region
, 1);
700 case DM_RH_RECOVERING
:
701 this_list
= &recover
;
705 bio_list_add(this_list
, bio
);
709 * Add bios that are delayed due to remote recovery
710 * back on to the write queue
712 if (unlikely(requeue
.head
)) {
713 spin_lock_irq(&ms
->lock
);
714 bio_list_merge(&ms
->writes
, &requeue
);
715 spin_unlock_irq(&ms
->lock
);
720 * Increment the pending counts for any regions that will
721 * be written to (writes to recover regions are going to
724 dm_rh_inc_pending(ms
->rh
, &sync
);
725 dm_rh_inc_pending(ms
->rh
, &nosync
);
728 * If the flush fails on a previous call and succeeds here,
729 * we must not reset the log_failure variable. We need
730 * userspace interaction to do that.
732 ms
->log_failure
= dm_rh_flush(ms
->rh
) ? 1 : ms
->log_failure
;
737 if (unlikely(ms
->log_failure
) && errors_handled(ms
)) {
738 spin_lock_irq(&ms
->lock
);
739 bio_list_merge(&ms
->failures
, &sync
);
740 spin_unlock_irq(&ms
->lock
);
743 while ((bio
= bio_list_pop(&sync
)))
746 while ((bio
= bio_list_pop(&recover
)))
747 dm_rh_delay(ms
->rh
, bio
);
749 while ((bio
= bio_list_pop(&nosync
))) {
750 if (unlikely(ms
->leg_failure
) && errors_handled(ms
)) {
751 spin_lock_irq(&ms
->lock
);
752 bio_list_add(&ms
->failures
, bio
);
753 spin_unlock_irq(&ms
->lock
);
756 map_bio(get_default_mirror(ms
), bio
);
757 generic_make_request(bio
);
762 static void do_failures(struct mirror_set
*ms
, struct bio_list
*failures
)
766 if (likely(!failures
->head
))
770 * If the log has failed, unattempted writes are being
771 * put on the holds list. We can't issue those writes
772 * until a log has been marked, so we must store them.
774 * If a 'noflush' suspend is in progress, we can requeue
775 * the I/O's to the core. This give userspace a chance
776 * to reconfigure the mirror, at which point the core
777 * will reissue the writes. If the 'noflush' flag is
778 * not set, we have no choice but to return errors.
780 * Some writes on the failures list may have been
781 * submitted before the log failure and represent a
782 * failure to write to one of the devices. It is ok
783 * for us to treat them the same and requeue them
786 while ((bio
= bio_list_pop(failures
))) {
787 if (!ms
->log_failure
) {
789 dm_rh_mark_nosync(ms
->rh
, bio
);
793 * If all the legs are dead, fail the I/O.
794 * If we have been told to handle errors, hold the bio
795 * and wait for userspace to deal with the problem.
796 * Otherwise pretend that the I/O succeeded. (This would
797 * be wrong if the failed leg returned after reboot and
798 * got replicated back to the good legs.)
800 if (!get_valid_mirror(ms
))
801 bio_endio(bio
, -EIO
);
802 else if (errors_handled(ms
))
809 static void trigger_event(struct work_struct
*work
)
811 struct mirror_set
*ms
=
812 container_of(work
, struct mirror_set
, trigger_event
);
814 dm_table_event(ms
->ti
->table
);
817 /*-----------------------------------------------------------------
819 *---------------------------------------------------------------*/
820 static void do_mirror(struct work_struct
*work
)
822 struct mirror_set
*ms
= container_of(work
, struct mirror_set
,
824 struct bio_list reads
, writes
, failures
;
827 spin_lock_irqsave(&ms
->lock
, flags
);
830 failures
= ms
->failures
;
831 bio_list_init(&ms
->reads
);
832 bio_list_init(&ms
->writes
);
833 bio_list_init(&ms
->failures
);
834 spin_unlock_irqrestore(&ms
->lock
, flags
);
836 dm_rh_update_states(ms
->rh
, errors_handled(ms
));
838 do_reads(ms
, &reads
);
839 do_writes(ms
, &writes
);
840 do_failures(ms
, &failures
);
843 /*-----------------------------------------------------------------
845 *---------------------------------------------------------------*/
846 static struct mirror_set
*alloc_context(unsigned int nr_mirrors
,
847 uint32_t region_size
,
848 struct dm_target
*ti
,
849 struct dm_dirty_log
*dl
)
852 struct mirror_set
*ms
= NULL
;
854 len
= sizeof(*ms
) + (sizeof(ms
->mirror
[0]) * nr_mirrors
);
856 ms
= kzalloc(len
, GFP_KERNEL
);
858 ti
->error
= "Cannot allocate mirror context";
862 spin_lock_init(&ms
->lock
);
863 bio_list_init(&ms
->reads
);
864 bio_list_init(&ms
->writes
);
865 bio_list_init(&ms
->failures
);
866 bio_list_init(&ms
->holds
);
869 ms
->nr_mirrors
= nr_mirrors
;
870 ms
->nr_regions
= dm_sector_div_up(ti
->len
, region_size
);
874 atomic_set(&ms
->suspend
, 0);
875 atomic_set(&ms
->default_mirror
, DEFAULT_MIRROR
);
877 ms
->io_client
= dm_io_client_create();
878 if (IS_ERR(ms
->io_client
)) {
879 ti
->error
= "Error creating dm_io client";
884 ms
->rh
= dm_region_hash_create(ms
, dispatch_bios
, wakeup_mirrord
,
885 wakeup_all_recovery_waiters
,
886 ms
->ti
->begin
, MAX_RECOVERY
,
887 dl
, region_size
, ms
->nr_regions
);
888 if (IS_ERR(ms
->rh
)) {
889 ti
->error
= "Error creating dirty region hash";
890 dm_io_client_destroy(ms
->io_client
);
898 static void free_context(struct mirror_set
*ms
, struct dm_target
*ti
,
902 dm_put_device(ti
, ms
->mirror
[m
].dev
);
904 dm_io_client_destroy(ms
->io_client
);
905 dm_region_hash_destroy(ms
->rh
);
909 static int get_mirror(struct mirror_set
*ms
, struct dm_target
*ti
,
910 unsigned int mirror
, char **argv
)
912 unsigned long long offset
;
915 if (sscanf(argv
[1], "%llu%c", &offset
, &dummy
) != 1) {
916 ti
->error
= "Invalid offset";
920 if (dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
),
921 &ms
->mirror
[mirror
].dev
)) {
922 ti
->error
= "Device lookup failure";
926 ms
->mirror
[mirror
].ms
= ms
;
927 atomic_set(&(ms
->mirror
[mirror
].error_count
), 0);
928 ms
->mirror
[mirror
].error_type
= 0;
929 ms
->mirror
[mirror
].offset
= offset
;
935 * Create dirty log: log_type #log_params <log_params>
937 static struct dm_dirty_log
*create_dirty_log(struct dm_target
*ti
,
938 unsigned argc
, char **argv
,
941 unsigned param_count
;
942 struct dm_dirty_log
*dl
;
946 ti
->error
= "Insufficient mirror log arguments";
950 if (sscanf(argv
[1], "%u%c", ¶m_count
, &dummy
) != 1) {
951 ti
->error
= "Invalid mirror log argument count";
955 *args_used
= 2 + param_count
;
957 if (argc
< *args_used
) {
958 ti
->error
= "Insufficient mirror log arguments";
962 dl
= dm_dirty_log_create(argv
[0], ti
, mirror_flush
, param_count
,
965 ti
->error
= "Error creating mirror dirty log";
972 static int parse_features(struct mirror_set
*ms
, unsigned argc
, char **argv
,
975 unsigned num_features
;
976 struct dm_target
*ti
= ms
->ti
;
984 if (sscanf(argv
[0], "%u%c", &num_features
, &dummy
) != 1) {
985 ti
->error
= "Invalid number of features";
993 if (num_features
> argc
) {
994 ti
->error
= "Not enough arguments to support feature count";
998 if (!strcmp("handle_errors", argv
[0]))
999 ms
->features
|= DM_RAID1_HANDLE_ERRORS
;
1001 ti
->error
= "Unrecognised feature requested";
1011 * Construct a mirror mapping:
1013 * log_type #log_params <log_params>
1014 * #mirrors [mirror_path offset]{2,}
1015 * [#features <features>]
1017 * log_type is "core" or "disk"
1018 * #log_params is between 1 and 3
1020 * If present, features must be "handle_errors".
1022 static int mirror_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1025 unsigned int nr_mirrors
, m
, args_used
;
1026 struct mirror_set
*ms
;
1027 struct dm_dirty_log
*dl
;
1030 dl
= create_dirty_log(ti
, argc
, argv
, &args_used
);
1037 if (!argc
|| sscanf(argv
[0], "%u%c", &nr_mirrors
, &dummy
) != 1 ||
1038 nr_mirrors
< 2 || nr_mirrors
> DM_KCOPYD_MAX_REGIONS
+ 1) {
1039 ti
->error
= "Invalid number of mirrors";
1040 dm_dirty_log_destroy(dl
);
1046 if (argc
< nr_mirrors
* 2) {
1047 ti
->error
= "Too few mirror arguments";
1048 dm_dirty_log_destroy(dl
);
1052 ms
= alloc_context(nr_mirrors
, dl
->type
->get_region_size(dl
), ti
, dl
);
1054 dm_dirty_log_destroy(dl
);
1058 /* Get the mirror parameter sets */
1059 for (m
= 0; m
< nr_mirrors
; m
++) {
1060 r
= get_mirror(ms
, ti
, m
, argv
);
1062 free_context(ms
, ti
, m
);
1071 r
= dm_set_target_max_io_len(ti
, dm_rh_get_region_size(ms
->rh
));
1073 goto err_free_context
;
1075 ti
->num_flush_requests
= 1;
1076 ti
->num_discard_requests
= 1;
1077 ti
->per_bio_data_size
= sizeof(struct dm_raid1_bio_record
);
1078 ti
->discard_zeroes_data_unsupported
= true;
1080 ms
->kmirrord_wq
= alloc_workqueue("kmirrord",
1081 WQ_NON_REENTRANT
| WQ_MEM_RECLAIM
, 0);
1082 if (!ms
->kmirrord_wq
) {
1083 DMERR("couldn't start kmirrord");
1085 goto err_free_context
;
1087 INIT_WORK(&ms
->kmirrord_work
, do_mirror
);
1088 init_timer(&ms
->timer
);
1089 ms
->timer_pending
= 0;
1090 INIT_WORK(&ms
->trigger_event
, trigger_event
);
1092 r
= parse_features(ms
, argc
, argv
, &args_used
);
1094 goto err_destroy_wq
;
1100 * Any read-balancing addition depends on the
1101 * DM_RAID1_HANDLE_ERRORS flag being present.
1102 * This is because the decision to balance depends
1103 * on the sync state of a region. If the above
1104 * flag is not present, we ignore errors; and
1105 * the sync state may be inaccurate.
1109 ti
->error
= "Too many mirror arguments";
1111 goto err_destroy_wq
;
1114 ms
->kcopyd_client
= dm_kcopyd_client_create();
1115 if (IS_ERR(ms
->kcopyd_client
)) {
1116 r
= PTR_ERR(ms
->kcopyd_client
);
1117 goto err_destroy_wq
;
1124 destroy_workqueue(ms
->kmirrord_wq
);
1126 free_context(ms
, ti
, ms
->nr_mirrors
);
1130 static void mirror_dtr(struct dm_target
*ti
)
1132 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1134 del_timer_sync(&ms
->timer
);
1135 flush_workqueue(ms
->kmirrord_wq
);
1136 flush_work(&ms
->trigger_event
);
1137 dm_kcopyd_client_destroy(ms
->kcopyd_client
);
1138 destroy_workqueue(ms
->kmirrord_wq
);
1139 free_context(ms
, ti
, ms
->nr_mirrors
);
1143 * Mirror mapping function
1145 static int mirror_map(struct dm_target
*ti
, struct bio
*bio
,
1146 union map_info
*map_context
)
1148 int r
, rw
= bio_rw(bio
);
1150 struct mirror_set
*ms
= ti
->private;
1151 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1152 struct dm_raid1_bio_record
*bio_record
=
1153 dm_per_bio_data(bio
, sizeof(struct dm_raid1_bio_record
));
1155 bio_record
->details
.bi_bdev
= NULL
;
1158 /* Save region for mirror_end_io() handler */
1159 bio_record
->write_region
= dm_rh_bio_to_region(ms
->rh
, bio
);
1160 queue_bio(ms
, bio
, rw
);
1161 return DM_MAPIO_SUBMITTED
;
1164 r
= log
->type
->in_sync(log
, dm_rh_bio_to_region(ms
->rh
, bio
), 0);
1165 if (r
< 0 && r
!= -EWOULDBLOCK
)
1169 * If region is not in-sync queue the bio.
1171 if (!r
|| (r
== -EWOULDBLOCK
)) {
1173 return -EWOULDBLOCK
;
1175 queue_bio(ms
, bio
, rw
);
1176 return DM_MAPIO_SUBMITTED
;
1180 * The region is in-sync and we can perform reads directly.
1181 * Store enough information so we can retry if it fails.
1183 m
= choose_mirror(ms
, bio
->bi_sector
);
1187 dm_bio_record(&bio_record
->details
, bio
);
1192 return DM_MAPIO_REMAPPED
;
1195 static int mirror_end_io(struct dm_target
*ti
, struct bio
*bio
,
1196 int error
, union map_info
*map_context
)
1198 int rw
= bio_rw(bio
);
1199 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1200 struct mirror
*m
= NULL
;
1201 struct dm_bio_details
*bd
= NULL
;
1202 struct dm_raid1_bio_record
*bio_record
=
1203 dm_per_bio_data(bio
, sizeof(struct dm_raid1_bio_record
));
1206 * We need to dec pending if this was a write.
1209 if (!(bio
->bi_rw
& (REQ_FLUSH
| REQ_DISCARD
)))
1210 dm_rh_dec(ms
->rh
, bio_record
->write_region
);
1214 if (error
== -EOPNOTSUPP
)
1217 if ((error
== -EWOULDBLOCK
) && (bio
->bi_rw
& REQ_RAHEAD
))
1220 if (unlikely(error
)) {
1221 if (!bio_record
->details
.bi_bdev
) {
1223 * There wasn't enough memory to record necessary
1224 * information for a retry or there was no other
1227 DMERR_LIMIT("Mirror read failed.");
1233 DMERR("Mirror read failed from %s. Trying alternative device.",
1236 fail_mirror(m
, DM_RAID1_READ_ERROR
);
1239 * A failed read is requeued for another attempt using an intact
1242 if (default_ok(m
) || mirror_available(ms
, bio
)) {
1243 bd
= &bio_record
->details
;
1245 dm_bio_restore(bd
, bio
);
1246 bio_record
->details
.bi_bdev
= NULL
;
1247 queue_bio(ms
, bio
, rw
);
1248 return DM_ENDIO_INCOMPLETE
;
1250 DMERR("All replicated volumes dead, failing I/O");
1254 bio_record
->details
.bi_bdev
= NULL
;
1259 static void mirror_presuspend(struct dm_target
*ti
)
1261 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1262 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1264 struct bio_list holds
;
1267 atomic_set(&ms
->suspend
, 1);
1270 * Process bios in the hold list to start recovery waiting
1271 * for bios in the hold list. After the process, no bio has
1272 * a chance to be added in the hold list because ms->suspend
1275 spin_lock_irq(&ms
->lock
);
1277 bio_list_init(&ms
->holds
);
1278 spin_unlock_irq(&ms
->lock
);
1280 while ((bio
= bio_list_pop(&holds
)))
1284 * We must finish up all the work that we've
1285 * generated (i.e. recovery work).
1287 dm_rh_stop_recovery(ms
->rh
);
1289 wait_event(_kmirrord_recovery_stopped
,
1290 !dm_rh_recovery_in_flight(ms
->rh
));
1292 if (log
->type
->presuspend
&& log
->type
->presuspend(log
))
1293 /* FIXME: need better error handling */
1294 DMWARN("log presuspend failed");
1297 * Now that recovery is complete/stopped and the
1298 * delayed bios are queued, we need to wait for
1299 * the worker thread to complete. This way,
1300 * we know that all of our I/O has been pushed.
1302 flush_workqueue(ms
->kmirrord_wq
);
1305 static void mirror_postsuspend(struct dm_target
*ti
)
1307 struct mirror_set
*ms
= ti
->private;
1308 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1310 if (log
->type
->postsuspend
&& log
->type
->postsuspend(log
))
1311 /* FIXME: need better error handling */
1312 DMWARN("log postsuspend failed");
1315 static void mirror_resume(struct dm_target
*ti
)
1317 struct mirror_set
*ms
= ti
->private;
1318 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1320 atomic_set(&ms
->suspend
, 0);
1321 if (log
->type
->resume
&& log
->type
->resume(log
))
1322 /* FIXME: need better error handling */
1323 DMWARN("log resume failed");
1324 dm_rh_start_recovery(ms
->rh
);
1328 * device_status_char
1329 * @m: mirror device/leg we want the status of
1331 * We return one character representing the most severe error
1332 * we have encountered.
1333 * A => Alive - No failures
1334 * D => Dead - A write failure occurred leaving mirror out-of-sync
1335 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1336 * R => Read - A read failure occurred, mirror data unaffected
1340 static char device_status_char(struct mirror
*m
)
1342 if (!atomic_read(&(m
->error_count
)))
1345 return (test_bit(DM_RAID1_FLUSH_ERROR
, &(m
->error_type
))) ? 'F' :
1346 (test_bit(DM_RAID1_WRITE_ERROR
, &(m
->error_type
))) ? 'D' :
1347 (test_bit(DM_RAID1_SYNC_ERROR
, &(m
->error_type
))) ? 'S' :
1348 (test_bit(DM_RAID1_READ_ERROR
, &(m
->error_type
))) ? 'R' : 'U';
1352 static int mirror_status(struct dm_target
*ti
, status_type_t type
,
1353 unsigned status_flags
, char *result
, unsigned maxlen
)
1355 unsigned int m
, sz
= 0;
1356 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1357 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1358 char buffer
[ms
->nr_mirrors
+ 1];
1361 case STATUSTYPE_INFO
:
1362 DMEMIT("%d ", ms
->nr_mirrors
);
1363 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
1364 DMEMIT("%s ", ms
->mirror
[m
].dev
->name
);
1365 buffer
[m
] = device_status_char(&(ms
->mirror
[m
]));
1369 DMEMIT("%llu/%llu 1 %s ",
1370 (unsigned long long)log
->type
->get_sync_count(log
),
1371 (unsigned long long)ms
->nr_regions
, buffer
);
1373 sz
+= log
->type
->status(log
, type
, result
+sz
, maxlen
-sz
);
1377 case STATUSTYPE_TABLE
:
1378 sz
= log
->type
->status(log
, type
, result
, maxlen
);
1380 DMEMIT("%d", ms
->nr_mirrors
);
1381 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1382 DMEMIT(" %s %llu", ms
->mirror
[m
].dev
->name
,
1383 (unsigned long long)ms
->mirror
[m
].offset
);
1385 if (ms
->features
& DM_RAID1_HANDLE_ERRORS
)
1386 DMEMIT(" 1 handle_errors");
1392 static int mirror_iterate_devices(struct dm_target
*ti
,
1393 iterate_devices_callout_fn fn
, void *data
)
1395 struct mirror_set
*ms
= ti
->private;
1399 for (i
= 0; !ret
&& i
< ms
->nr_mirrors
; i
++)
1400 ret
= fn(ti
, ms
->mirror
[i
].dev
,
1401 ms
->mirror
[i
].offset
, ti
->len
, data
);
1406 static struct target_type mirror_target
= {
1408 .version
= {1, 13, 1},
1409 .module
= THIS_MODULE
,
1413 .end_io
= mirror_end_io
,
1414 .presuspend
= mirror_presuspend
,
1415 .postsuspend
= mirror_postsuspend
,
1416 .resume
= mirror_resume
,
1417 .status
= mirror_status
,
1418 .iterate_devices
= mirror_iterate_devices
,
1421 static int __init
dm_mirror_init(void)
1425 r
= dm_register_target(&mirror_target
);
1427 DMERR("Failed to register mirror target");
1437 static void __exit
dm_mirror_exit(void)
1439 dm_unregister_target(&mirror_target
);
1443 module_init(dm_mirror_init
);
1444 module_exit(dm_mirror_exit
);
1446 MODULE_DESCRIPTION(DM_NAME
" mirror target");
1447 MODULE_AUTHOR("Joe Thornber");
1448 MODULE_LICENSE("GPL");