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. */
25 #define DM_IO_PAGES 64
26 #define DM_KCOPYD_PAGES 64
28 #define DM_RAID1_HANDLE_ERRORS 0x01
29 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
31 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped
);
33 /*-----------------------------------------------------------------
34 * Mirror set structures.
35 *---------------------------------------------------------------*/
44 struct mirror_set
*ms
;
46 unsigned long error_type
;
53 struct list_head list
;
57 spinlock_t lock
; /* protects the lists */
58 struct bio_list reads
;
59 struct bio_list writes
;
60 struct bio_list failures
;
61 struct bio_list holds
; /* bios are waiting until suspend */
63 struct dm_region_hash
*rh
;
64 struct dm_kcopyd_client
*kcopyd_client
;
65 struct dm_io_client
*io_client
;
66 mempool_t
*read_record_pool
;
75 atomic_t default_mirror
; /* Default mirror */
77 struct workqueue_struct
*kmirrord_wq
;
78 struct work_struct kmirrord_work
;
79 struct timer_list timer
;
80 unsigned long timer_pending
;
82 struct work_struct trigger_event
;
85 struct mirror mirror
[0];
88 static void wakeup_mirrord(void *context
)
90 struct mirror_set
*ms
= context
;
92 queue_work(ms
->kmirrord_wq
, &ms
->kmirrord_work
);
95 static void delayed_wake_fn(unsigned long data
)
97 struct mirror_set
*ms
= (struct mirror_set
*) data
;
99 clear_bit(0, &ms
->timer_pending
);
103 static void delayed_wake(struct mirror_set
*ms
)
105 if (test_and_set_bit(0, &ms
->timer_pending
))
108 ms
->timer
.expires
= jiffies
+ HZ
/ 5;
109 ms
->timer
.data
= (unsigned long) ms
;
110 ms
->timer
.function
= delayed_wake_fn
;
111 add_timer(&ms
->timer
);
114 static void wakeup_all_recovery_waiters(void *context
)
116 wake_up_all(&_kmirrord_recovery_stopped
);
119 static void queue_bio(struct mirror_set
*ms
, struct bio
*bio
, int rw
)
125 bl
= (rw
== WRITE
) ? &ms
->writes
: &ms
->reads
;
126 spin_lock_irqsave(&ms
->lock
, flags
);
127 should_wake
= !(bl
->head
);
128 bio_list_add(bl
, bio
);
129 spin_unlock_irqrestore(&ms
->lock
, flags
);
135 static void dispatch_bios(void *context
, struct bio_list
*bio_list
)
137 struct mirror_set
*ms
= context
;
140 while ((bio
= bio_list_pop(bio_list
)))
141 queue_bio(ms
, bio
, WRITE
);
144 #define MIN_READ_RECORDS 20
145 struct dm_raid1_read_record
{
147 struct dm_bio_details details
;
150 static struct kmem_cache
*_dm_raid1_read_record_cache
;
153 * Every mirror should look like this one.
155 #define DEFAULT_MIRROR 0
158 * This is yucky. We squirrel the mirror struct away inside
159 * bi_next for read/write buffers. This is safe since the bh
160 * doesn't get submitted to the lower levels of block layer.
162 static struct mirror
*bio_get_m(struct bio
*bio
)
164 return (struct mirror
*) bio
->bi_next
;
167 static void bio_set_m(struct bio
*bio
, struct mirror
*m
)
169 bio
->bi_next
= (struct bio
*) m
;
172 static struct mirror
*get_default_mirror(struct mirror_set
*ms
)
174 return &ms
->mirror
[atomic_read(&ms
->default_mirror
)];
177 static void set_default_mirror(struct mirror
*m
)
179 struct mirror_set
*ms
= m
->ms
;
180 struct mirror
*m0
= &(ms
->mirror
[0]);
182 atomic_set(&ms
->default_mirror
, m
- m0
);
185 static struct mirror
*get_valid_mirror(struct mirror_set
*ms
)
189 for (m
= ms
->mirror
; m
< ms
->mirror
+ ms
->nr_mirrors
; m
++)
190 if (!atomic_read(&m
->error_count
))
197 * @m: mirror device to fail
198 * @error_type: one of the enum's, DM_RAID1_*_ERROR
200 * If errors are being handled, record the type of
201 * error encountered for this device. If this type
202 * of error has already been recorded, we can return;
203 * otherwise, we must signal userspace by triggering
204 * an event. Additionally, if the device is the
205 * primary device, we must choose a new primary, but
206 * only if the mirror is in-sync.
208 * This function must not block.
210 static void fail_mirror(struct mirror
*m
, enum dm_raid1_error error_type
)
212 struct mirror_set
*ms
= m
->ms
;
218 * error_count is used for nothing more than a
219 * simple way to tell if a device has encountered
222 atomic_inc(&m
->error_count
);
224 if (test_and_set_bit(error_type
, &m
->error_type
))
227 if (!errors_handled(ms
))
230 if (m
!= get_default_mirror(ms
))
235 * Better to issue requests to same failing device
236 * than to risk returning corrupt data.
238 DMERR("Primary mirror (%s) failed while out-of-sync: "
239 "Reads may fail.", m
->dev
->name
);
243 new = get_valid_mirror(ms
);
245 set_default_mirror(new);
247 DMWARN("All sides of mirror have failed.");
250 schedule_work(&ms
->trigger_event
);
253 static int mirror_flush(struct dm_target
*ti
)
255 struct mirror_set
*ms
= ti
->private;
256 unsigned long error_bits
;
259 struct dm_io_region io
[ms
->nr_mirrors
];
261 struct dm_io_request io_req
= {
262 .bi_rw
= WRITE_FLUSH
,
263 .mem
.type
= DM_IO_KMEM
,
264 .mem
.ptr
.bvec
= NULL
,
265 .client
= ms
->io_client
,
268 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++) {
269 io
[i
].bdev
= m
->dev
->bdev
;
275 dm_io(&io_req
, ms
->nr_mirrors
, io
, &error_bits
);
276 if (unlikely(error_bits
!= 0)) {
277 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
278 if (test_bit(i
, &error_bits
))
279 fail_mirror(ms
->mirror
+ i
,
280 DM_RAID1_FLUSH_ERROR
);
287 /*-----------------------------------------------------------------
290 * When a mirror is first activated we may find that some regions
291 * are in the no-sync state. We have to recover these by
292 * recopying from the default mirror to all the others.
293 *---------------------------------------------------------------*/
294 static void recovery_complete(int read_err
, unsigned long write_err
,
297 struct dm_region
*reg
= context
;
298 struct mirror_set
*ms
= dm_rh_region_context(reg
);
302 /* Read error means the failure of default mirror. */
303 DMERR_LIMIT("Unable to read primary mirror during recovery");
304 fail_mirror(get_default_mirror(ms
), DM_RAID1_SYNC_ERROR
);
308 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
311 * Bits correspond to devices (excluding default mirror).
312 * The default mirror cannot change during recovery.
314 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
315 if (&ms
->mirror
[m
] == get_default_mirror(ms
))
317 if (test_bit(bit
, &write_err
))
318 fail_mirror(ms
->mirror
+ m
,
319 DM_RAID1_SYNC_ERROR
);
324 dm_rh_recovery_end(reg
, !(read_err
|| write_err
));
327 static int recover(struct mirror_set
*ms
, struct dm_region
*reg
)
331 struct dm_io_region from
, to
[DM_KCOPYD_MAX_REGIONS
], *dest
;
333 unsigned long flags
= 0;
334 region_t key
= dm_rh_get_region_key(reg
);
335 sector_t region_size
= dm_rh_get_region_size(ms
->rh
);
337 /* fill in the source */
338 m
= get_default_mirror(ms
);
339 from
.bdev
= m
->dev
->bdev
;
340 from
.sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
341 if (key
== (ms
->nr_regions
- 1)) {
343 * The final region may be smaller than
346 from
.count
= ms
->ti
->len
& (region_size
- 1);
348 from
.count
= region_size
;
350 from
.count
= region_size
;
352 /* fill in the destinations */
353 for (i
= 0, dest
= to
; i
< ms
->nr_mirrors
; i
++) {
354 if (&ms
->mirror
[i
] == get_default_mirror(ms
))
358 dest
->bdev
= m
->dev
->bdev
;
359 dest
->sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
360 dest
->count
= from
.count
;
365 if (!errors_handled(ms
))
366 set_bit(DM_KCOPYD_IGNORE_ERROR
, &flags
);
368 r
= dm_kcopyd_copy(ms
->kcopyd_client
, &from
, ms
->nr_mirrors
- 1, to
,
369 flags
, recovery_complete
, reg
);
374 static void do_recovery(struct mirror_set
*ms
)
376 struct dm_region
*reg
;
377 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
381 * Start quiescing some regions.
383 dm_rh_recovery_prepare(ms
->rh
);
386 * Copy any already quiesced regions.
388 while ((reg
= dm_rh_recovery_start(ms
->rh
))) {
389 r
= recover(ms
, reg
);
391 dm_rh_recovery_end(reg
, 0);
395 * Update the in sync flag.
398 (log
->type
->get_sync_count(log
) == ms
->nr_regions
)) {
399 /* the sync is complete */
400 dm_table_event(ms
->ti
->table
);
405 /*-----------------------------------------------------------------
407 *---------------------------------------------------------------*/
408 static struct mirror
*choose_mirror(struct mirror_set
*ms
, sector_t sector
)
410 struct mirror
*m
= get_default_mirror(ms
);
413 if (likely(!atomic_read(&m
->error_count
)))
416 if (m
-- == ms
->mirror
)
418 } while (m
!= get_default_mirror(ms
));
423 static int default_ok(struct mirror
*m
)
425 struct mirror
*default_mirror
= get_default_mirror(m
->ms
);
427 return !atomic_read(&default_mirror
->error_count
);
430 static int mirror_available(struct mirror_set
*ms
, struct bio
*bio
)
432 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
433 region_t region
= dm_rh_bio_to_region(ms
->rh
, bio
);
435 if (log
->type
->in_sync(log
, region
, 0))
436 return choose_mirror(ms
, bio
->bi_sector
) ? 1 : 0;
442 * remap a buffer to a particular mirror.
444 static sector_t
map_sector(struct mirror
*m
, struct bio
*bio
)
446 if (unlikely(!bio
->bi_size
))
448 return m
->offset
+ dm_target_offset(m
->ms
->ti
, bio
->bi_sector
);
451 static void map_bio(struct mirror
*m
, struct bio
*bio
)
453 bio
->bi_bdev
= m
->dev
->bdev
;
454 bio
->bi_sector
= map_sector(m
, bio
);
457 static void map_region(struct dm_io_region
*io
, struct mirror
*m
,
460 io
->bdev
= m
->dev
->bdev
;
461 io
->sector
= map_sector(m
, bio
);
462 io
->count
= bio
->bi_size
>> 9;
465 static void hold_bio(struct mirror_set
*ms
, struct bio
*bio
)
468 * Lock is required to avoid race condition during suspend
471 spin_lock_irq(&ms
->lock
);
473 if (atomic_read(&ms
->suspend
)) {
474 spin_unlock_irq(&ms
->lock
);
477 * If device is suspended, complete the bio.
479 if (dm_noflush_suspending(ms
->ti
))
480 bio_endio(bio
, DM_ENDIO_REQUEUE
);
482 bio_endio(bio
, -EIO
);
487 * Hold bio until the suspend is complete.
489 bio_list_add(&ms
->holds
, bio
);
490 spin_unlock_irq(&ms
->lock
);
493 /*-----------------------------------------------------------------
495 *---------------------------------------------------------------*/
496 static void read_callback(unsigned long error
, void *context
)
498 struct bio
*bio
= context
;
502 bio_set_m(bio
, NULL
);
504 if (likely(!error
)) {
509 fail_mirror(m
, DM_RAID1_READ_ERROR
);
511 if (likely(default_ok(m
)) || mirror_available(m
->ms
, bio
)) {
512 DMWARN_LIMIT("Read failure on mirror device %s. "
513 "Trying alternative device.",
515 queue_bio(m
->ms
, bio
, bio_rw(bio
));
519 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
521 bio_endio(bio
, -EIO
);
524 /* Asynchronous read. */
525 static void read_async_bio(struct mirror
*m
, struct bio
*bio
)
527 struct dm_io_region io
;
528 struct dm_io_request io_req
= {
530 .mem
.type
= DM_IO_BVEC
,
531 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
532 .notify
.fn
= read_callback
,
533 .notify
.context
= bio
,
534 .client
= m
->ms
->io_client
,
537 map_region(&io
, m
, bio
);
539 BUG_ON(dm_io(&io_req
, 1, &io
, NULL
));
542 static inline int region_in_sync(struct mirror_set
*ms
, region_t region
,
545 int state
= dm_rh_get_state(ms
->rh
, region
, may_block
);
546 return state
== DM_RH_CLEAN
|| state
== DM_RH_DIRTY
;
549 static void do_reads(struct mirror_set
*ms
, struct bio_list
*reads
)
555 while ((bio
= bio_list_pop(reads
))) {
556 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
557 m
= get_default_mirror(ms
);
560 * We can only read balance if the region is in sync.
562 if (likely(region_in_sync(ms
, region
, 1)))
563 m
= choose_mirror(ms
, bio
->bi_sector
);
564 else if (m
&& atomic_read(&m
->error_count
))
568 read_async_bio(m
, bio
);
570 bio_endio(bio
, -EIO
);
574 /*-----------------------------------------------------------------
577 * We do different things with the write io depending on the
578 * state of the region that it's in:
580 * SYNC: increment pending, use kcopyd to write to *all* mirrors
581 * RECOVERING: delay the io until recovery completes
582 * NOSYNC: increment pending, just write to the default mirror
583 *---------------------------------------------------------------*/
586 static void write_callback(unsigned long error
, void *context
)
589 struct bio
*bio
= (struct bio
*) context
;
590 struct mirror_set
*ms
;
594 ms
= bio_get_m(bio
)->ms
;
595 bio_set_m(bio
, NULL
);
598 * NOTE: We don't decrement the pending count here,
599 * instead it is done by the targets endio function.
600 * This way we handle both writes to SYNC and NOSYNC
601 * regions with the same code.
603 if (likely(!error
)) {
608 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
609 if (test_bit(i
, &error
))
610 fail_mirror(ms
->mirror
+ i
, DM_RAID1_WRITE_ERROR
);
613 * Need to raise event. Since raising
614 * events can block, we need to do it in
617 spin_lock_irqsave(&ms
->lock
, flags
);
618 if (!ms
->failures
.head
)
620 bio_list_add(&ms
->failures
, bio
);
621 spin_unlock_irqrestore(&ms
->lock
, flags
);
626 static void do_write(struct mirror_set
*ms
, struct bio
*bio
)
629 struct dm_io_region io
[ms
->nr_mirrors
], *dest
= io
;
631 struct dm_io_request io_req
= {
632 .bi_rw
= WRITE
| (bio
->bi_rw
& WRITE_FLUSH_FUA
),
633 .mem
.type
= DM_IO_BVEC
,
634 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
635 .notify
.fn
= write_callback
,
636 .notify
.context
= bio
,
637 .client
= ms
->io_client
,
640 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++)
641 map_region(dest
++, m
, bio
);
644 * Use default mirror because we only need it to retrieve the reference
645 * to the mirror set in write_callback().
647 bio_set_m(bio
, get_default_mirror(ms
));
649 BUG_ON(dm_io(&io_req
, ms
->nr_mirrors
, io
, NULL
));
652 static void do_writes(struct mirror_set
*ms
, struct bio_list
*writes
)
656 struct bio_list sync
, nosync
, recover
, *this_list
= NULL
;
657 struct bio_list requeue
;
658 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
665 * Classify each write.
667 bio_list_init(&sync
);
668 bio_list_init(&nosync
);
669 bio_list_init(&recover
);
670 bio_list_init(&requeue
);
672 while ((bio
= bio_list_pop(writes
))) {
673 if (bio
->bi_rw
& REQ_FLUSH
) {
674 bio_list_add(&sync
, bio
);
678 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
680 if (log
->type
->is_remote_recovering
&&
681 log
->type
->is_remote_recovering(log
, region
)) {
682 bio_list_add(&requeue
, bio
);
686 state
= dm_rh_get_state(ms
->rh
, region
, 1);
697 case DM_RH_RECOVERING
:
698 this_list
= &recover
;
702 bio_list_add(this_list
, bio
);
706 * Add bios that are delayed due to remote recovery
707 * back on to the write queue
709 if (unlikely(requeue
.head
)) {
710 spin_lock_irq(&ms
->lock
);
711 bio_list_merge(&ms
->writes
, &requeue
);
712 spin_unlock_irq(&ms
->lock
);
717 * Increment the pending counts for any regions that will
718 * be written to (writes to recover regions are going to
721 dm_rh_inc_pending(ms
->rh
, &sync
);
722 dm_rh_inc_pending(ms
->rh
, &nosync
);
725 * If the flush fails on a previous call and succeeds here,
726 * we must not reset the log_failure variable. We need
727 * userspace interaction to do that.
729 ms
->log_failure
= dm_rh_flush(ms
->rh
) ? 1 : ms
->log_failure
;
734 if (unlikely(ms
->log_failure
) && errors_handled(ms
)) {
735 spin_lock_irq(&ms
->lock
);
736 bio_list_merge(&ms
->failures
, &sync
);
737 spin_unlock_irq(&ms
->lock
);
740 while ((bio
= bio_list_pop(&sync
)))
743 while ((bio
= bio_list_pop(&recover
)))
744 dm_rh_delay(ms
->rh
, bio
);
746 while ((bio
= bio_list_pop(&nosync
))) {
747 if (unlikely(ms
->leg_failure
) && errors_handled(ms
)) {
748 spin_lock_irq(&ms
->lock
);
749 bio_list_add(&ms
->failures
, bio
);
750 spin_unlock_irq(&ms
->lock
);
753 map_bio(get_default_mirror(ms
), bio
);
754 generic_make_request(bio
);
759 static void do_failures(struct mirror_set
*ms
, struct bio_list
*failures
)
763 if (likely(!failures
->head
))
767 * If the log has failed, unattempted writes are being
768 * put on the holds list. We can't issue those writes
769 * until a log has been marked, so we must store them.
771 * If a 'noflush' suspend is in progress, we can requeue
772 * the I/O's to the core. This give userspace a chance
773 * to reconfigure the mirror, at which point the core
774 * will reissue the writes. If the 'noflush' flag is
775 * not set, we have no choice but to return errors.
777 * Some writes on the failures list may have been
778 * submitted before the log failure and represent a
779 * failure to write to one of the devices. It is ok
780 * for us to treat them the same and requeue them
783 while ((bio
= bio_list_pop(failures
))) {
784 if (!ms
->log_failure
) {
786 dm_rh_mark_nosync(ms
->rh
, bio
);
790 * If all the legs are dead, fail the I/O.
791 * If we have been told to handle errors, hold the bio
792 * and wait for userspace to deal with the problem.
793 * Otherwise pretend that the I/O succeeded. (This would
794 * be wrong if the failed leg returned after reboot and
795 * got replicated back to the good legs.)
797 if (!get_valid_mirror(ms
))
798 bio_endio(bio
, -EIO
);
799 else if (errors_handled(ms
))
806 static void trigger_event(struct work_struct
*work
)
808 struct mirror_set
*ms
=
809 container_of(work
, struct mirror_set
, trigger_event
);
811 dm_table_event(ms
->ti
->table
);
814 /*-----------------------------------------------------------------
816 *---------------------------------------------------------------*/
817 static void do_mirror(struct work_struct
*work
)
819 struct mirror_set
*ms
= container_of(work
, struct mirror_set
,
821 struct bio_list reads
, writes
, failures
;
824 spin_lock_irqsave(&ms
->lock
, flags
);
827 failures
= ms
->failures
;
828 bio_list_init(&ms
->reads
);
829 bio_list_init(&ms
->writes
);
830 bio_list_init(&ms
->failures
);
831 spin_unlock_irqrestore(&ms
->lock
, flags
);
833 dm_rh_update_states(ms
->rh
, errors_handled(ms
));
835 do_reads(ms
, &reads
);
836 do_writes(ms
, &writes
);
837 do_failures(ms
, &failures
);
839 dm_table_unplug_all(ms
->ti
->table
);
842 /*-----------------------------------------------------------------
844 *---------------------------------------------------------------*/
845 static struct mirror_set
*alloc_context(unsigned int nr_mirrors
,
846 uint32_t region_size
,
847 struct dm_target
*ti
,
848 struct dm_dirty_log
*dl
)
851 struct mirror_set
*ms
= NULL
;
853 len
= sizeof(*ms
) + (sizeof(ms
->mirror
[0]) * nr_mirrors
);
855 ms
= kzalloc(len
, GFP_KERNEL
);
857 ti
->error
= "Cannot allocate mirror context";
861 spin_lock_init(&ms
->lock
);
862 bio_list_init(&ms
->reads
);
863 bio_list_init(&ms
->writes
);
864 bio_list_init(&ms
->failures
);
865 bio_list_init(&ms
->holds
);
868 ms
->nr_mirrors
= nr_mirrors
;
869 ms
->nr_regions
= dm_sector_div_up(ti
->len
, region_size
);
873 atomic_set(&ms
->suspend
, 0);
874 atomic_set(&ms
->default_mirror
, DEFAULT_MIRROR
);
876 ms
->read_record_pool
= mempool_create_slab_pool(MIN_READ_RECORDS
,
877 _dm_raid1_read_record_cache
);
879 if (!ms
->read_record_pool
) {
880 ti
->error
= "Error creating mirror read_record_pool";
885 ms
->io_client
= dm_io_client_create(DM_IO_PAGES
);
886 if (IS_ERR(ms
->io_client
)) {
887 ti
->error
= "Error creating dm_io client";
888 mempool_destroy(ms
->read_record_pool
);
893 ms
->rh
= dm_region_hash_create(ms
, dispatch_bios
, wakeup_mirrord
,
894 wakeup_all_recovery_waiters
,
895 ms
->ti
->begin
, MAX_RECOVERY
,
896 dl
, region_size
, ms
->nr_regions
);
897 if (IS_ERR(ms
->rh
)) {
898 ti
->error
= "Error creating dirty region hash";
899 dm_io_client_destroy(ms
->io_client
);
900 mempool_destroy(ms
->read_record_pool
);
908 static void free_context(struct mirror_set
*ms
, struct dm_target
*ti
,
912 dm_put_device(ti
, ms
->mirror
[m
].dev
);
914 dm_io_client_destroy(ms
->io_client
);
915 dm_region_hash_destroy(ms
->rh
);
916 mempool_destroy(ms
->read_record_pool
);
920 static int get_mirror(struct mirror_set
*ms
, struct dm_target
*ti
,
921 unsigned int mirror
, char **argv
)
923 unsigned long long offset
;
925 if (sscanf(argv
[1], "%llu", &offset
) != 1) {
926 ti
->error
= "Invalid offset";
930 if (dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
),
931 &ms
->mirror
[mirror
].dev
)) {
932 ti
->error
= "Device lookup failure";
936 ms
->mirror
[mirror
].ms
= ms
;
937 atomic_set(&(ms
->mirror
[mirror
].error_count
), 0);
938 ms
->mirror
[mirror
].error_type
= 0;
939 ms
->mirror
[mirror
].offset
= offset
;
945 * Create dirty log: log_type #log_params <log_params>
947 static struct dm_dirty_log
*create_dirty_log(struct dm_target
*ti
,
948 unsigned argc
, char **argv
,
951 unsigned param_count
;
952 struct dm_dirty_log
*dl
;
955 ti
->error
= "Insufficient mirror log arguments";
959 if (sscanf(argv
[1], "%u", ¶m_count
) != 1) {
960 ti
->error
= "Invalid mirror log argument count";
964 *args_used
= 2 + param_count
;
966 if (argc
< *args_used
) {
967 ti
->error
= "Insufficient mirror log arguments";
971 dl
= dm_dirty_log_create(argv
[0], ti
, mirror_flush
, param_count
,
974 ti
->error
= "Error creating mirror dirty log";
981 static int parse_features(struct mirror_set
*ms
, unsigned argc
, char **argv
,
984 unsigned num_features
;
985 struct dm_target
*ti
= ms
->ti
;
992 if (sscanf(argv
[0], "%u", &num_features
) != 1) {
993 ti
->error
= "Invalid number of features";
1001 if (num_features
> argc
) {
1002 ti
->error
= "Not enough arguments to support feature count";
1006 if (!strcmp("handle_errors", argv
[0]))
1007 ms
->features
|= DM_RAID1_HANDLE_ERRORS
;
1009 ti
->error
= "Unrecognised feature requested";
1019 * Construct a mirror mapping:
1021 * log_type #log_params <log_params>
1022 * #mirrors [mirror_path offset]{2,}
1023 * [#features <features>]
1025 * log_type is "core" or "disk"
1026 * #log_params is between 1 and 3
1028 * If present, features must be "handle_errors".
1030 static int mirror_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1033 unsigned int nr_mirrors
, m
, args_used
;
1034 struct mirror_set
*ms
;
1035 struct dm_dirty_log
*dl
;
1037 dl
= create_dirty_log(ti
, argc
, argv
, &args_used
);
1044 if (!argc
|| sscanf(argv
[0], "%u", &nr_mirrors
) != 1 ||
1045 nr_mirrors
< 2 || nr_mirrors
> DM_KCOPYD_MAX_REGIONS
+ 1) {
1046 ti
->error
= "Invalid number of mirrors";
1047 dm_dirty_log_destroy(dl
);
1053 if (argc
< nr_mirrors
* 2) {
1054 ti
->error
= "Too few mirror arguments";
1055 dm_dirty_log_destroy(dl
);
1059 ms
= alloc_context(nr_mirrors
, dl
->type
->get_region_size(dl
), ti
, dl
);
1061 dm_dirty_log_destroy(dl
);
1065 /* Get the mirror parameter sets */
1066 for (m
= 0; m
< nr_mirrors
; m
++) {
1067 r
= get_mirror(ms
, ti
, m
, argv
);
1069 free_context(ms
, ti
, m
);
1077 ti
->split_io
= dm_rh_get_region_size(ms
->rh
);
1078 ti
->num_flush_requests
= 1;
1080 ms
->kmirrord_wq
= create_singlethread_workqueue("kmirrord");
1081 if (!ms
->kmirrord_wq
) {
1082 DMERR("couldn't start kmirrord");
1084 goto err_free_context
;
1086 INIT_WORK(&ms
->kmirrord_work
, do_mirror
);
1087 init_timer(&ms
->timer
);
1088 ms
->timer_pending
= 0;
1089 INIT_WORK(&ms
->trigger_event
, trigger_event
);
1091 r
= parse_features(ms
, argc
, argv
, &args_used
);
1093 goto err_destroy_wq
;
1099 * Any read-balancing addition depends on the
1100 * DM_RAID1_HANDLE_ERRORS flag being present.
1101 * This is because the decision to balance depends
1102 * on the sync state of a region. If the above
1103 * flag is not present, we ignore errors; and
1104 * the sync state may be inaccurate.
1108 ti
->error
= "Too many mirror arguments";
1110 goto err_destroy_wq
;
1113 r
= dm_kcopyd_client_create(DM_KCOPYD_PAGES
, &ms
->kcopyd_client
);
1115 goto err_destroy_wq
;
1121 destroy_workqueue(ms
->kmirrord_wq
);
1123 free_context(ms
, ti
, ms
->nr_mirrors
);
1127 static void mirror_dtr(struct dm_target
*ti
)
1129 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1131 del_timer_sync(&ms
->timer
);
1132 flush_workqueue(ms
->kmirrord_wq
);
1133 flush_scheduled_work();
1134 dm_kcopyd_client_destroy(ms
->kcopyd_client
);
1135 destroy_workqueue(ms
->kmirrord_wq
);
1136 free_context(ms
, ti
, ms
->nr_mirrors
);
1140 * Mirror mapping function
1142 static int mirror_map(struct dm_target
*ti
, struct bio
*bio
,
1143 union map_info
*map_context
)
1145 int r
, rw
= bio_rw(bio
);
1147 struct mirror_set
*ms
= ti
->private;
1148 struct dm_raid1_read_record
*read_record
= NULL
;
1149 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1152 /* Save region for mirror_end_io() handler */
1153 map_context
->ll
= dm_rh_bio_to_region(ms
->rh
, bio
);
1154 queue_bio(ms
, bio
, rw
);
1155 return DM_MAPIO_SUBMITTED
;
1158 r
= log
->type
->in_sync(log
, dm_rh_bio_to_region(ms
->rh
, bio
), 0);
1159 if (r
< 0 && r
!= -EWOULDBLOCK
)
1163 * If region is not in-sync queue the bio.
1165 if (!r
|| (r
== -EWOULDBLOCK
)) {
1167 return -EWOULDBLOCK
;
1169 queue_bio(ms
, bio
, rw
);
1170 return DM_MAPIO_SUBMITTED
;
1174 * The region is in-sync and we can perform reads directly.
1175 * Store enough information so we can retry if it fails.
1177 m
= choose_mirror(ms
, bio
->bi_sector
);
1181 read_record
= mempool_alloc(ms
->read_record_pool
, GFP_NOIO
);
1182 if (likely(read_record
)) {
1183 dm_bio_record(&read_record
->details
, bio
);
1184 map_context
->ptr
= read_record
;
1190 return DM_MAPIO_REMAPPED
;
1193 static int mirror_end_io(struct dm_target
*ti
, struct bio
*bio
,
1194 int error
, union map_info
*map_context
)
1196 int rw
= bio_rw(bio
);
1197 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1198 struct mirror
*m
= NULL
;
1199 struct dm_bio_details
*bd
= NULL
;
1200 struct dm_raid1_read_record
*read_record
= map_context
->ptr
;
1203 * We need to dec pending if this was a write.
1206 if (!(bio
->bi_rw
& REQ_FLUSH
))
1207 dm_rh_dec(ms
->rh
, map_context
->ll
);
1211 if (error
== -EOPNOTSUPP
)
1214 if ((error
== -EWOULDBLOCK
) && (bio
->bi_rw
& REQ_RAHEAD
))
1217 if (unlikely(error
)) {
1220 * There wasn't enough memory to record necessary
1221 * information for a retry or there was no other
1224 DMERR_LIMIT("Mirror read failed.");
1230 DMERR("Mirror read failed from %s. Trying alternative device.",
1233 fail_mirror(m
, DM_RAID1_READ_ERROR
);
1236 * A failed read is requeued for another attempt using an intact
1239 if (default_ok(m
) || mirror_available(ms
, bio
)) {
1240 bd
= &read_record
->details
;
1242 dm_bio_restore(bd
, bio
);
1243 mempool_free(read_record
, ms
->read_record_pool
);
1244 map_context
->ptr
= NULL
;
1245 queue_bio(ms
, bio
, rw
);
1248 DMERR("All replicated volumes dead, failing I/O");
1253 mempool_free(read_record
, ms
->read_record_pool
);
1254 map_context
->ptr
= NULL
;
1260 static void mirror_presuspend(struct dm_target
*ti
)
1262 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1263 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1265 struct bio_list holds
;
1268 atomic_set(&ms
->suspend
, 1);
1271 * Process bios in the hold list to start recovery waiting
1272 * for bios in the hold list. After the process, no bio has
1273 * a chance to be added in the hold list because ms->suspend
1276 spin_lock_irq(&ms
->lock
);
1278 bio_list_init(&ms
->holds
);
1279 spin_unlock_irq(&ms
->lock
);
1281 while ((bio
= bio_list_pop(&holds
)))
1285 * We must finish up all the work that we've
1286 * generated (i.e. recovery work).
1288 dm_rh_stop_recovery(ms
->rh
);
1290 wait_event(_kmirrord_recovery_stopped
,
1291 !dm_rh_recovery_in_flight(ms
->rh
));
1293 if (log
->type
->presuspend
&& log
->type
->presuspend(log
))
1294 /* FIXME: need better error handling */
1295 DMWARN("log presuspend failed");
1298 * Now that recovery is complete/stopped and the
1299 * delayed bios are queued, we need to wait for
1300 * the worker thread to complete. This way,
1301 * we know that all of our I/O has been pushed.
1303 flush_workqueue(ms
->kmirrord_wq
);
1306 static void mirror_postsuspend(struct dm_target
*ti
)
1308 struct mirror_set
*ms
= ti
->private;
1309 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1311 if (log
->type
->postsuspend
&& log
->type
->postsuspend(log
))
1312 /* FIXME: need better error handling */
1313 DMWARN("log postsuspend failed");
1316 static void mirror_resume(struct dm_target
*ti
)
1318 struct mirror_set
*ms
= ti
->private;
1319 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1321 atomic_set(&ms
->suspend
, 0);
1322 if (log
->type
->resume
&& log
->type
->resume(log
))
1323 /* FIXME: need better error handling */
1324 DMWARN("log resume failed");
1325 dm_rh_start_recovery(ms
->rh
);
1329 * device_status_char
1330 * @m: mirror device/leg we want the status of
1332 * We return one character representing the most severe error
1333 * we have encountered.
1334 * A => Alive - No failures
1335 * D => Dead - A write failure occurred leaving mirror out-of-sync
1336 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1337 * R => Read - A read failure occurred, mirror data unaffected
1341 static char device_status_char(struct mirror
*m
)
1343 if (!atomic_read(&(m
->error_count
)))
1346 return (test_bit(DM_RAID1_FLUSH_ERROR
, &(m
->error_type
))) ? 'F' :
1347 (test_bit(DM_RAID1_WRITE_ERROR
, &(m
->error_type
))) ? 'D' :
1348 (test_bit(DM_RAID1_SYNC_ERROR
, &(m
->error_type
))) ? 'S' :
1349 (test_bit(DM_RAID1_READ_ERROR
, &(m
->error_type
))) ? 'R' : 'U';
1353 static int mirror_status(struct dm_target
*ti
, status_type_t type
,
1354 char *result
, unsigned int maxlen
)
1356 unsigned int m
, sz
= 0;
1357 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1358 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1359 char buffer
[ms
->nr_mirrors
+ 1];
1362 case STATUSTYPE_INFO
:
1363 DMEMIT("%d ", ms
->nr_mirrors
);
1364 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
1365 DMEMIT("%s ", ms
->mirror
[m
].dev
->name
);
1366 buffer
[m
] = device_status_char(&(ms
->mirror
[m
]));
1370 DMEMIT("%llu/%llu 1 %s ",
1371 (unsigned long long)log
->type
->get_sync_count(log
),
1372 (unsigned long long)ms
->nr_regions
, buffer
);
1374 sz
+= log
->type
->status(log
, type
, result
+sz
, maxlen
-sz
);
1378 case STATUSTYPE_TABLE
:
1379 sz
= log
->type
->status(log
, type
, result
, maxlen
);
1381 DMEMIT("%d", ms
->nr_mirrors
);
1382 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1383 DMEMIT(" %s %llu", ms
->mirror
[m
].dev
->name
,
1384 (unsigned long long)ms
->mirror
[m
].offset
);
1386 if (ms
->features
& DM_RAID1_HANDLE_ERRORS
)
1387 DMEMIT(" 1 handle_errors");
1393 static int mirror_iterate_devices(struct dm_target
*ti
,
1394 iterate_devices_callout_fn fn
, void *data
)
1396 struct mirror_set
*ms
= ti
->private;
1400 for (i
= 0; !ret
&& i
< ms
->nr_mirrors
; i
++)
1401 ret
= fn(ti
, ms
->mirror
[i
].dev
,
1402 ms
->mirror
[i
].offset
, ti
->len
, data
);
1407 static struct target_type mirror_target
= {
1409 .version
= {1, 12, 0},
1410 .module
= THIS_MODULE
,
1414 .end_io
= mirror_end_io
,
1415 .presuspend
= mirror_presuspend
,
1416 .postsuspend
= mirror_postsuspend
,
1417 .resume
= mirror_resume
,
1418 .status
= mirror_status
,
1419 .iterate_devices
= mirror_iterate_devices
,
1422 static int __init
dm_mirror_init(void)
1426 _dm_raid1_read_record_cache
= KMEM_CACHE(dm_raid1_read_record
, 0);
1427 if (!_dm_raid1_read_record_cache
) {
1428 DMERR("Can't allocate dm_raid1_read_record cache");
1433 r
= dm_register_target(&mirror_target
);
1435 DMERR("Failed to register mirror target");
1442 kmem_cache_destroy(_dm_raid1_read_record_cache
);
1447 static void __exit
dm_mirror_exit(void)
1449 dm_unregister_target(&mirror_target
);
1450 kmem_cache_destroy(_dm_raid1_read_record_cache
);
1454 module_init(dm_mirror_init
);
1455 module_exit(dm_mirror_exit
);
1457 MODULE_DESCRIPTION(DM_NAME
" mirror target");
1458 MODULE_AUTHOR("Joe Thornber");
1459 MODULE_LICENSE("GPL");