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 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle
,
86 "A percentage of time allocated for raid resynchronization");
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 struct dm_raid1_bio_record
{
146 /* if details->bi_bdev == NULL, details were not saved */
147 struct dm_bio_details details
;
148 region_t write_region
;
152 * Every mirror should look like this one.
154 #define DEFAULT_MIRROR 0
157 * This is yucky. We squirrel the mirror struct away inside
158 * bi_next for read/write buffers. This is safe since the bh
159 * doesn't get submitted to the lower levels of block layer.
161 static struct mirror
*bio_get_m(struct bio
*bio
)
163 return (struct mirror
*) bio
->bi_next
;
166 static void bio_set_m(struct bio
*bio
, struct mirror
*m
)
168 bio
->bi_next
= (struct bio
*) m
;
171 static struct mirror
*get_default_mirror(struct mirror_set
*ms
)
173 return &ms
->mirror
[atomic_read(&ms
->default_mirror
)];
176 static void set_default_mirror(struct mirror
*m
)
178 struct mirror_set
*ms
= m
->ms
;
179 struct mirror
*m0
= &(ms
->mirror
[0]);
181 atomic_set(&ms
->default_mirror
, m
- m0
);
184 static struct mirror
*get_valid_mirror(struct mirror_set
*ms
)
188 for (m
= ms
->mirror
; m
< ms
->mirror
+ ms
->nr_mirrors
; m
++)
189 if (!atomic_read(&m
->error_count
))
196 * @m: mirror device to fail
197 * @error_type: one of the enum's, DM_RAID1_*_ERROR
199 * If errors are being handled, record the type of
200 * error encountered for this device. If this type
201 * of error has already been recorded, we can return;
202 * otherwise, we must signal userspace by triggering
203 * an event. Additionally, if the device is the
204 * primary device, we must choose a new primary, but
205 * only if the mirror is in-sync.
207 * This function must not block.
209 static void fail_mirror(struct mirror
*m
, enum dm_raid1_error error_type
)
211 struct mirror_set
*ms
= m
->ms
;
217 * error_count is used for nothing more than a
218 * simple way to tell if a device has encountered
221 atomic_inc(&m
->error_count
);
223 if (test_and_set_bit(error_type
, &m
->error_type
))
226 if (!errors_handled(ms
))
229 if (m
!= get_default_mirror(ms
))
234 * Better to issue requests to same failing device
235 * than to risk returning corrupt data.
237 DMERR("Primary mirror (%s) failed while out-of-sync: "
238 "Reads may fail.", m
->dev
->name
);
242 new = get_valid_mirror(ms
);
244 set_default_mirror(new);
246 DMWARN("All sides of mirror have failed.");
249 schedule_work(&ms
->trigger_event
);
252 static int mirror_flush(struct dm_target
*ti
)
254 struct mirror_set
*ms
= ti
->private;
255 unsigned long error_bits
;
258 struct dm_io_region io
[ms
->nr_mirrors
];
260 struct dm_io_request io_req
= {
261 .bi_rw
= WRITE_FLUSH
,
262 .mem
.type
= DM_IO_KMEM
,
263 .mem
.ptr
.addr
= NULL
,
264 .client
= ms
->io_client
,
267 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++) {
268 io
[i
].bdev
= m
->dev
->bdev
;
274 dm_io(&io_req
, ms
->nr_mirrors
, io
, &error_bits
);
275 if (unlikely(error_bits
!= 0)) {
276 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
277 if (test_bit(i
, &error_bits
))
278 fail_mirror(ms
->mirror
+ i
,
279 DM_RAID1_FLUSH_ERROR
);
286 /*-----------------------------------------------------------------
289 * When a mirror is first activated we may find that some regions
290 * are in the no-sync state. We have to recover these by
291 * recopying from the default mirror to all the others.
292 *---------------------------------------------------------------*/
293 static void recovery_complete(int read_err
, unsigned long write_err
,
296 struct dm_region
*reg
= context
;
297 struct mirror_set
*ms
= dm_rh_region_context(reg
);
301 /* Read error means the failure of default mirror. */
302 DMERR_LIMIT("Unable to read primary mirror during recovery");
303 fail_mirror(get_default_mirror(ms
), DM_RAID1_SYNC_ERROR
);
307 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
310 * Bits correspond to devices (excluding default mirror).
311 * The default mirror cannot change during recovery.
313 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
314 if (&ms
->mirror
[m
] == get_default_mirror(ms
))
316 if (test_bit(bit
, &write_err
))
317 fail_mirror(ms
->mirror
+ m
,
318 DM_RAID1_SYNC_ERROR
);
323 dm_rh_recovery_end(reg
, !(read_err
|| write_err
));
326 static int recover(struct mirror_set
*ms
, struct dm_region
*reg
)
330 struct dm_io_region from
, to
[DM_KCOPYD_MAX_REGIONS
], *dest
;
332 unsigned long flags
= 0;
333 region_t key
= dm_rh_get_region_key(reg
);
334 sector_t region_size
= dm_rh_get_region_size(ms
->rh
);
336 /* fill in the source */
337 m
= get_default_mirror(ms
);
338 from
.bdev
= m
->dev
->bdev
;
339 from
.sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
340 if (key
== (ms
->nr_regions
- 1)) {
342 * The final region may be smaller than
345 from
.count
= ms
->ti
->len
& (region_size
- 1);
347 from
.count
= region_size
;
349 from
.count
= region_size
;
351 /* fill in the destinations */
352 for (i
= 0, dest
= to
; i
< ms
->nr_mirrors
; i
++) {
353 if (&ms
->mirror
[i
] == get_default_mirror(ms
))
357 dest
->bdev
= m
->dev
->bdev
;
358 dest
->sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
359 dest
->count
= from
.count
;
364 if (!errors_handled(ms
))
365 set_bit(DM_KCOPYD_IGNORE_ERROR
, &flags
);
367 r
= dm_kcopyd_copy(ms
->kcopyd_client
, &from
, ms
->nr_mirrors
- 1, to
,
368 flags
, recovery_complete
, reg
);
373 static void do_recovery(struct mirror_set
*ms
)
375 struct dm_region
*reg
;
376 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
380 * Start quiescing some regions.
382 dm_rh_recovery_prepare(ms
->rh
);
385 * Copy any already quiesced regions.
387 while ((reg
= dm_rh_recovery_start(ms
->rh
))) {
388 r
= recover(ms
, reg
);
390 dm_rh_recovery_end(reg
, 0);
394 * Update the in sync flag.
397 (log
->type
->get_sync_count(log
) == ms
->nr_regions
)) {
398 /* the sync is complete */
399 dm_table_event(ms
->ti
->table
);
404 /*-----------------------------------------------------------------
406 *---------------------------------------------------------------*/
407 static struct mirror
*choose_mirror(struct mirror_set
*ms
, sector_t sector
)
409 struct mirror
*m
= get_default_mirror(ms
);
412 if (likely(!atomic_read(&m
->error_count
)))
415 if (m
-- == ms
->mirror
)
417 } while (m
!= get_default_mirror(ms
));
422 static int default_ok(struct mirror
*m
)
424 struct mirror
*default_mirror
= get_default_mirror(m
->ms
);
426 return !atomic_read(&default_mirror
->error_count
);
429 static int mirror_available(struct mirror_set
*ms
, struct bio
*bio
)
431 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
432 region_t region
= dm_rh_bio_to_region(ms
->rh
, bio
);
434 if (log
->type
->in_sync(log
, region
, 0))
435 return choose_mirror(ms
, bio
->bi_sector
) ? 1 : 0;
441 * remap a buffer to a particular mirror.
443 static sector_t
map_sector(struct mirror
*m
, struct bio
*bio
)
445 if (unlikely(!bio
->bi_size
))
447 return m
->offset
+ dm_target_offset(m
->ms
->ti
, bio
->bi_sector
);
450 static void map_bio(struct mirror
*m
, struct bio
*bio
)
452 bio
->bi_bdev
= m
->dev
->bdev
;
453 bio
->bi_sector
= map_sector(m
, bio
);
456 static void map_region(struct dm_io_region
*io
, struct mirror
*m
,
459 io
->bdev
= m
->dev
->bdev
;
460 io
->sector
= map_sector(m
, bio
);
461 io
->count
= bio_sectors(bio
);
464 static void hold_bio(struct mirror_set
*ms
, struct bio
*bio
)
467 * Lock is required to avoid race condition during suspend
470 spin_lock_irq(&ms
->lock
);
472 if (atomic_read(&ms
->suspend
)) {
473 spin_unlock_irq(&ms
->lock
);
476 * If device is suspended, complete the bio.
478 if (dm_noflush_suspending(ms
->ti
))
479 bio_endio(bio
, DM_ENDIO_REQUEUE
);
481 bio_endio(bio
, -EIO
);
486 * Hold bio until the suspend is complete.
488 bio_list_add(&ms
->holds
, bio
);
489 spin_unlock_irq(&ms
->lock
);
492 /*-----------------------------------------------------------------
494 *---------------------------------------------------------------*/
495 static void read_callback(unsigned long error
, void *context
)
497 struct bio
*bio
= context
;
501 bio_set_m(bio
, NULL
);
503 if (likely(!error
)) {
508 fail_mirror(m
, DM_RAID1_READ_ERROR
);
510 if (likely(default_ok(m
)) || mirror_available(m
->ms
, bio
)) {
511 DMWARN_LIMIT("Read failure on mirror device %s. "
512 "Trying alternative device.",
514 queue_bio(m
->ms
, bio
, bio_rw(bio
));
518 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
520 bio_endio(bio
, -EIO
);
523 /* Asynchronous read. */
524 static void read_async_bio(struct mirror
*m
, struct bio
*bio
)
526 struct dm_io_region io
;
527 struct dm_io_request io_req
= {
529 .mem
.type
= DM_IO_BVEC
,
530 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
531 .notify
.fn
= read_callback
,
532 .notify
.context
= bio
,
533 .client
= m
->ms
->io_client
,
536 map_region(&io
, m
, bio
);
538 BUG_ON(dm_io(&io_req
, 1, &io
, NULL
));
541 static inline int region_in_sync(struct mirror_set
*ms
, region_t region
,
544 int state
= dm_rh_get_state(ms
->rh
, region
, may_block
);
545 return state
== DM_RH_CLEAN
|| state
== DM_RH_DIRTY
;
548 static void do_reads(struct mirror_set
*ms
, struct bio_list
*reads
)
554 while ((bio
= bio_list_pop(reads
))) {
555 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
556 m
= get_default_mirror(ms
);
559 * We can only read balance if the region is in sync.
561 if (likely(region_in_sync(ms
, region
, 1)))
562 m
= choose_mirror(ms
, bio
->bi_sector
);
563 else if (m
&& atomic_read(&m
->error_count
))
567 read_async_bio(m
, bio
);
569 bio_endio(bio
, -EIO
);
573 /*-----------------------------------------------------------------
576 * We do different things with the write io depending on the
577 * state of the region that it's in:
579 * SYNC: increment pending, use kcopyd to write to *all* mirrors
580 * RECOVERING: delay the io until recovery completes
581 * NOSYNC: increment pending, just write to the default mirror
582 *---------------------------------------------------------------*/
585 static void write_callback(unsigned long error
, void *context
)
588 struct bio
*bio
= (struct bio
*) context
;
589 struct mirror_set
*ms
;
593 ms
= bio_get_m(bio
)->ms
;
594 bio_set_m(bio
, NULL
);
597 * NOTE: We don't decrement the pending count here,
598 * instead it is done by the targets endio function.
599 * This way we handle both writes to SYNC and NOSYNC
600 * regions with the same code.
602 if (likely(!error
)) {
608 * If the bio is discard, return an error, but do not
611 if (bio
->bi_rw
& REQ_DISCARD
) {
612 bio_endio(bio
, -EOPNOTSUPP
);
616 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
617 if (test_bit(i
, &error
))
618 fail_mirror(ms
->mirror
+ i
, DM_RAID1_WRITE_ERROR
);
621 * Need to raise event. Since raising
622 * events can block, we need to do it in
625 spin_lock_irqsave(&ms
->lock
, flags
);
626 if (!ms
->failures
.head
)
628 bio_list_add(&ms
->failures
, bio
);
629 spin_unlock_irqrestore(&ms
->lock
, flags
);
634 static void do_write(struct mirror_set
*ms
, struct bio
*bio
)
637 struct dm_io_region io
[ms
->nr_mirrors
], *dest
= io
;
639 struct dm_io_request io_req
= {
640 .bi_rw
= WRITE
| (bio
->bi_rw
& WRITE_FLUSH_FUA
),
641 .mem
.type
= DM_IO_BVEC
,
642 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
643 .notify
.fn
= write_callback
,
644 .notify
.context
= bio
,
645 .client
= ms
->io_client
,
648 if (bio
->bi_rw
& REQ_DISCARD
) {
649 io_req
.bi_rw
|= REQ_DISCARD
;
650 io_req
.mem
.type
= DM_IO_KMEM
;
651 io_req
.mem
.ptr
.addr
= NULL
;
654 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++)
655 map_region(dest
++, m
, bio
);
658 * Use default mirror because we only need it to retrieve the reference
659 * to the mirror set in write_callback().
661 bio_set_m(bio
, get_default_mirror(ms
));
663 BUG_ON(dm_io(&io_req
, ms
->nr_mirrors
, io
, NULL
));
666 static void do_writes(struct mirror_set
*ms
, struct bio_list
*writes
)
670 struct bio_list sync
, nosync
, recover
, *this_list
= NULL
;
671 struct bio_list requeue
;
672 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
679 * Classify each write.
681 bio_list_init(&sync
);
682 bio_list_init(&nosync
);
683 bio_list_init(&recover
);
684 bio_list_init(&requeue
);
686 while ((bio
= bio_list_pop(writes
))) {
687 if ((bio
->bi_rw
& REQ_FLUSH
) ||
688 (bio
->bi_rw
& REQ_DISCARD
)) {
689 bio_list_add(&sync
, bio
);
693 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
695 if (log
->type
->is_remote_recovering
&&
696 log
->type
->is_remote_recovering(log
, region
)) {
697 bio_list_add(&requeue
, bio
);
701 state
= dm_rh_get_state(ms
->rh
, region
, 1);
712 case DM_RH_RECOVERING
:
713 this_list
= &recover
;
717 bio_list_add(this_list
, bio
);
721 * Add bios that are delayed due to remote recovery
722 * back on to the write queue
724 if (unlikely(requeue
.head
)) {
725 spin_lock_irq(&ms
->lock
);
726 bio_list_merge(&ms
->writes
, &requeue
);
727 spin_unlock_irq(&ms
->lock
);
732 * Increment the pending counts for any regions that will
733 * be written to (writes to recover regions are going to
736 dm_rh_inc_pending(ms
->rh
, &sync
);
737 dm_rh_inc_pending(ms
->rh
, &nosync
);
740 * If the flush fails on a previous call and succeeds here,
741 * we must not reset the log_failure variable. We need
742 * userspace interaction to do that.
744 ms
->log_failure
= dm_rh_flush(ms
->rh
) ? 1 : ms
->log_failure
;
749 if (unlikely(ms
->log_failure
) && errors_handled(ms
)) {
750 spin_lock_irq(&ms
->lock
);
751 bio_list_merge(&ms
->failures
, &sync
);
752 spin_unlock_irq(&ms
->lock
);
755 while ((bio
= bio_list_pop(&sync
)))
758 while ((bio
= bio_list_pop(&recover
)))
759 dm_rh_delay(ms
->rh
, bio
);
761 while ((bio
= bio_list_pop(&nosync
))) {
762 if (unlikely(ms
->leg_failure
) && errors_handled(ms
)) {
763 spin_lock_irq(&ms
->lock
);
764 bio_list_add(&ms
->failures
, bio
);
765 spin_unlock_irq(&ms
->lock
);
768 map_bio(get_default_mirror(ms
), bio
);
769 generic_make_request(bio
);
774 static void do_failures(struct mirror_set
*ms
, struct bio_list
*failures
)
778 if (likely(!failures
->head
))
782 * If the log has failed, unattempted writes are being
783 * put on the holds list. We can't issue those writes
784 * until a log has been marked, so we must store them.
786 * If a 'noflush' suspend is in progress, we can requeue
787 * the I/O's to the core. This give userspace a chance
788 * to reconfigure the mirror, at which point the core
789 * will reissue the writes. If the 'noflush' flag is
790 * not set, we have no choice but to return errors.
792 * Some writes on the failures list may have been
793 * submitted before the log failure and represent a
794 * failure to write to one of the devices. It is ok
795 * for us to treat them the same and requeue them
798 while ((bio
= bio_list_pop(failures
))) {
799 if (!ms
->log_failure
) {
801 dm_rh_mark_nosync(ms
->rh
, bio
);
805 * If all the legs are dead, fail the I/O.
806 * If we have been told to handle errors, hold the bio
807 * and wait for userspace to deal with the problem.
808 * Otherwise pretend that the I/O succeeded. (This would
809 * be wrong if the failed leg returned after reboot and
810 * got replicated back to the good legs.)
812 if (!get_valid_mirror(ms
))
813 bio_endio(bio
, -EIO
);
814 else if (errors_handled(ms
))
821 static void trigger_event(struct work_struct
*work
)
823 struct mirror_set
*ms
=
824 container_of(work
, struct mirror_set
, trigger_event
);
826 dm_table_event(ms
->ti
->table
);
829 /*-----------------------------------------------------------------
831 *---------------------------------------------------------------*/
832 static void do_mirror(struct work_struct
*work
)
834 struct mirror_set
*ms
= container_of(work
, struct mirror_set
,
836 struct bio_list reads
, writes
, failures
;
839 spin_lock_irqsave(&ms
->lock
, flags
);
842 failures
= ms
->failures
;
843 bio_list_init(&ms
->reads
);
844 bio_list_init(&ms
->writes
);
845 bio_list_init(&ms
->failures
);
846 spin_unlock_irqrestore(&ms
->lock
, flags
);
848 dm_rh_update_states(ms
->rh
, errors_handled(ms
));
850 do_reads(ms
, &reads
);
851 do_writes(ms
, &writes
);
852 do_failures(ms
, &failures
);
855 /*-----------------------------------------------------------------
857 *---------------------------------------------------------------*/
858 static struct mirror_set
*alloc_context(unsigned int nr_mirrors
,
859 uint32_t region_size
,
860 struct dm_target
*ti
,
861 struct dm_dirty_log
*dl
)
864 struct mirror_set
*ms
= NULL
;
866 len
= sizeof(*ms
) + (sizeof(ms
->mirror
[0]) * nr_mirrors
);
868 ms
= kzalloc(len
, GFP_KERNEL
);
870 ti
->error
= "Cannot allocate mirror context";
874 spin_lock_init(&ms
->lock
);
875 bio_list_init(&ms
->reads
);
876 bio_list_init(&ms
->writes
);
877 bio_list_init(&ms
->failures
);
878 bio_list_init(&ms
->holds
);
881 ms
->nr_mirrors
= nr_mirrors
;
882 ms
->nr_regions
= dm_sector_div_up(ti
->len
, region_size
);
886 atomic_set(&ms
->suspend
, 0);
887 atomic_set(&ms
->default_mirror
, DEFAULT_MIRROR
);
889 ms
->io_client
= dm_io_client_create();
890 if (IS_ERR(ms
->io_client
)) {
891 ti
->error
= "Error creating dm_io client";
896 ms
->rh
= dm_region_hash_create(ms
, dispatch_bios
, wakeup_mirrord
,
897 wakeup_all_recovery_waiters
,
898 ms
->ti
->begin
, MAX_RECOVERY
,
899 dl
, region_size
, ms
->nr_regions
);
900 if (IS_ERR(ms
->rh
)) {
901 ti
->error
= "Error creating dirty region hash";
902 dm_io_client_destroy(ms
->io_client
);
910 static void free_context(struct mirror_set
*ms
, struct dm_target
*ti
,
914 dm_put_device(ti
, ms
->mirror
[m
].dev
);
916 dm_io_client_destroy(ms
->io_client
);
917 dm_region_hash_destroy(ms
->rh
);
921 static int get_mirror(struct mirror_set
*ms
, struct dm_target
*ti
,
922 unsigned int mirror
, char **argv
)
924 unsigned long long offset
;
927 if (sscanf(argv
[1], "%llu%c", &offset
, &dummy
) != 1) {
928 ti
->error
= "Invalid offset";
932 if (dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
),
933 &ms
->mirror
[mirror
].dev
)) {
934 ti
->error
= "Device lookup failure";
938 ms
->mirror
[mirror
].ms
= ms
;
939 atomic_set(&(ms
->mirror
[mirror
].error_count
), 0);
940 ms
->mirror
[mirror
].error_type
= 0;
941 ms
->mirror
[mirror
].offset
= offset
;
947 * Create dirty log: log_type #log_params <log_params>
949 static struct dm_dirty_log
*create_dirty_log(struct dm_target
*ti
,
950 unsigned argc
, char **argv
,
953 unsigned param_count
;
954 struct dm_dirty_log
*dl
;
958 ti
->error
= "Insufficient mirror log arguments";
962 if (sscanf(argv
[1], "%u%c", ¶m_count
, &dummy
) != 1) {
963 ti
->error
= "Invalid mirror log argument count";
967 *args_used
= 2 + param_count
;
969 if (argc
< *args_used
) {
970 ti
->error
= "Insufficient mirror log arguments";
974 dl
= dm_dirty_log_create(argv
[0], ti
, mirror_flush
, param_count
,
977 ti
->error
= "Error creating mirror dirty log";
984 static int parse_features(struct mirror_set
*ms
, unsigned argc
, char **argv
,
987 unsigned num_features
;
988 struct dm_target
*ti
= ms
->ti
;
996 if (sscanf(argv
[0], "%u%c", &num_features
, &dummy
) != 1) {
997 ti
->error
= "Invalid number of features";
1005 if (num_features
> argc
) {
1006 ti
->error
= "Not enough arguments to support feature count";
1010 if (!strcmp("handle_errors", argv
[0]))
1011 ms
->features
|= DM_RAID1_HANDLE_ERRORS
;
1013 ti
->error
= "Unrecognised feature requested";
1023 * Construct a mirror mapping:
1025 * log_type #log_params <log_params>
1026 * #mirrors [mirror_path offset]{2,}
1027 * [#features <features>]
1029 * log_type is "core" or "disk"
1030 * #log_params is between 1 and 3
1032 * If present, features must be "handle_errors".
1034 static int mirror_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1037 unsigned int nr_mirrors
, m
, args_used
;
1038 struct mirror_set
*ms
;
1039 struct dm_dirty_log
*dl
;
1042 dl
= create_dirty_log(ti
, argc
, argv
, &args_used
);
1049 if (!argc
|| sscanf(argv
[0], "%u%c", &nr_mirrors
, &dummy
) != 1 ||
1050 nr_mirrors
< 2 || nr_mirrors
> DM_KCOPYD_MAX_REGIONS
+ 1) {
1051 ti
->error
= "Invalid number of mirrors";
1052 dm_dirty_log_destroy(dl
);
1058 if (argc
< nr_mirrors
* 2) {
1059 ti
->error
= "Too few mirror arguments";
1060 dm_dirty_log_destroy(dl
);
1064 ms
= alloc_context(nr_mirrors
, dl
->type
->get_region_size(dl
), ti
, dl
);
1066 dm_dirty_log_destroy(dl
);
1070 /* Get the mirror parameter sets */
1071 for (m
= 0; m
< nr_mirrors
; m
++) {
1072 r
= get_mirror(ms
, ti
, m
, argv
);
1074 free_context(ms
, ti
, m
);
1083 r
= dm_set_target_max_io_len(ti
, dm_rh_get_region_size(ms
->rh
));
1085 goto err_free_context
;
1087 ti
->num_flush_bios
= 1;
1088 ti
->num_discard_bios
= 1;
1089 ti
->per_bio_data_size
= sizeof(struct dm_raid1_bio_record
);
1090 ti
->discard_zeroes_data_unsupported
= true;
1092 ms
->kmirrord_wq
= alloc_workqueue("kmirrord", WQ_MEM_RECLAIM
, 0);
1093 if (!ms
->kmirrord_wq
) {
1094 DMERR("couldn't start kmirrord");
1096 goto err_free_context
;
1098 INIT_WORK(&ms
->kmirrord_work
, do_mirror
);
1099 init_timer(&ms
->timer
);
1100 ms
->timer_pending
= 0;
1101 INIT_WORK(&ms
->trigger_event
, trigger_event
);
1103 r
= parse_features(ms
, argc
, argv
, &args_used
);
1105 goto err_destroy_wq
;
1111 * Any read-balancing addition depends on the
1112 * DM_RAID1_HANDLE_ERRORS flag being present.
1113 * This is because the decision to balance depends
1114 * on the sync state of a region. If the above
1115 * flag is not present, we ignore errors; and
1116 * the sync state may be inaccurate.
1120 ti
->error
= "Too many mirror arguments";
1122 goto err_destroy_wq
;
1125 ms
->kcopyd_client
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
1126 if (IS_ERR(ms
->kcopyd_client
)) {
1127 r
= PTR_ERR(ms
->kcopyd_client
);
1128 goto err_destroy_wq
;
1135 destroy_workqueue(ms
->kmirrord_wq
);
1137 free_context(ms
, ti
, ms
->nr_mirrors
);
1141 static void mirror_dtr(struct dm_target
*ti
)
1143 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1145 del_timer_sync(&ms
->timer
);
1146 flush_workqueue(ms
->kmirrord_wq
);
1147 flush_work(&ms
->trigger_event
);
1148 dm_kcopyd_client_destroy(ms
->kcopyd_client
);
1149 destroy_workqueue(ms
->kmirrord_wq
);
1150 free_context(ms
, ti
, ms
->nr_mirrors
);
1154 * Mirror mapping function
1156 static int mirror_map(struct dm_target
*ti
, struct bio
*bio
)
1158 int r
, rw
= bio_rw(bio
);
1160 struct mirror_set
*ms
= ti
->private;
1161 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1162 struct dm_raid1_bio_record
*bio_record
=
1163 dm_per_bio_data(bio
, sizeof(struct dm_raid1_bio_record
));
1165 bio_record
->details
.bi_bdev
= NULL
;
1168 /* Save region for mirror_end_io() handler */
1169 bio_record
->write_region
= dm_rh_bio_to_region(ms
->rh
, bio
);
1170 queue_bio(ms
, bio
, rw
);
1171 return DM_MAPIO_SUBMITTED
;
1174 r
= log
->type
->in_sync(log
, dm_rh_bio_to_region(ms
->rh
, bio
), 0);
1175 if (r
< 0 && r
!= -EWOULDBLOCK
)
1179 * If region is not in-sync queue the bio.
1181 if (!r
|| (r
== -EWOULDBLOCK
)) {
1183 return -EWOULDBLOCK
;
1185 queue_bio(ms
, bio
, rw
);
1186 return DM_MAPIO_SUBMITTED
;
1190 * The region is in-sync and we can perform reads directly.
1191 * Store enough information so we can retry if it fails.
1193 m
= choose_mirror(ms
, bio
->bi_sector
);
1197 dm_bio_record(&bio_record
->details
, bio
);
1202 return DM_MAPIO_REMAPPED
;
1205 static int mirror_end_io(struct dm_target
*ti
, struct bio
*bio
, int error
)
1207 int rw
= bio_rw(bio
);
1208 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1209 struct mirror
*m
= NULL
;
1210 struct dm_bio_details
*bd
= NULL
;
1211 struct dm_raid1_bio_record
*bio_record
=
1212 dm_per_bio_data(bio
, sizeof(struct dm_raid1_bio_record
));
1215 * We need to dec pending if this was a write.
1218 if (!(bio
->bi_rw
& (REQ_FLUSH
| REQ_DISCARD
)))
1219 dm_rh_dec(ms
->rh
, bio_record
->write_region
);
1223 if (error
== -EOPNOTSUPP
)
1226 if ((error
== -EWOULDBLOCK
) && (bio
->bi_rw
& REQ_RAHEAD
))
1229 if (unlikely(error
)) {
1230 if (!bio_record
->details
.bi_bdev
) {
1232 * There wasn't enough memory to record necessary
1233 * information for a retry or there was no other
1236 DMERR_LIMIT("Mirror read failed.");
1242 DMERR("Mirror read failed from %s. Trying alternative device.",
1245 fail_mirror(m
, DM_RAID1_READ_ERROR
);
1248 * A failed read is requeued for another attempt using an intact
1251 if (default_ok(m
) || mirror_available(ms
, bio
)) {
1252 bd
= &bio_record
->details
;
1254 dm_bio_restore(bd
, bio
);
1255 bio_record
->details
.bi_bdev
= NULL
;
1256 queue_bio(ms
, bio
, rw
);
1257 return DM_ENDIO_INCOMPLETE
;
1259 DMERR("All replicated volumes dead, failing I/O");
1263 bio_record
->details
.bi_bdev
= NULL
;
1268 static void mirror_presuspend(struct dm_target
*ti
)
1270 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1271 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1273 struct bio_list holds
;
1276 atomic_set(&ms
->suspend
, 1);
1279 * Process bios in the hold list to start recovery waiting
1280 * for bios in the hold list. After the process, no bio has
1281 * a chance to be added in the hold list because ms->suspend
1284 spin_lock_irq(&ms
->lock
);
1286 bio_list_init(&ms
->holds
);
1287 spin_unlock_irq(&ms
->lock
);
1289 while ((bio
= bio_list_pop(&holds
)))
1293 * We must finish up all the work that we've
1294 * generated (i.e. recovery work).
1296 dm_rh_stop_recovery(ms
->rh
);
1298 wait_event(_kmirrord_recovery_stopped
,
1299 !dm_rh_recovery_in_flight(ms
->rh
));
1301 if (log
->type
->presuspend
&& log
->type
->presuspend(log
))
1302 /* FIXME: need better error handling */
1303 DMWARN("log presuspend failed");
1306 * Now that recovery is complete/stopped and the
1307 * delayed bios are queued, we need to wait for
1308 * the worker thread to complete. This way,
1309 * we know that all of our I/O has been pushed.
1311 flush_workqueue(ms
->kmirrord_wq
);
1314 static void mirror_postsuspend(struct dm_target
*ti
)
1316 struct mirror_set
*ms
= ti
->private;
1317 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1319 if (log
->type
->postsuspend
&& log
->type
->postsuspend(log
))
1320 /* FIXME: need better error handling */
1321 DMWARN("log postsuspend failed");
1324 static void mirror_resume(struct dm_target
*ti
)
1326 struct mirror_set
*ms
= ti
->private;
1327 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1329 atomic_set(&ms
->suspend
, 0);
1330 if (log
->type
->resume
&& log
->type
->resume(log
))
1331 /* FIXME: need better error handling */
1332 DMWARN("log resume failed");
1333 dm_rh_start_recovery(ms
->rh
);
1337 * device_status_char
1338 * @m: mirror device/leg we want the status of
1340 * We return one character representing the most severe error
1341 * we have encountered.
1342 * A => Alive - No failures
1343 * D => Dead - A write failure occurred leaving mirror out-of-sync
1344 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1345 * R => Read - A read failure occurred, mirror data unaffected
1349 static char device_status_char(struct mirror
*m
)
1351 if (!atomic_read(&(m
->error_count
)))
1354 return (test_bit(DM_RAID1_FLUSH_ERROR
, &(m
->error_type
))) ? 'F' :
1355 (test_bit(DM_RAID1_WRITE_ERROR
, &(m
->error_type
))) ? 'D' :
1356 (test_bit(DM_RAID1_SYNC_ERROR
, &(m
->error_type
))) ? 'S' :
1357 (test_bit(DM_RAID1_READ_ERROR
, &(m
->error_type
))) ? 'R' : 'U';
1361 static void mirror_status(struct dm_target
*ti
, status_type_t type
,
1362 unsigned status_flags
, char *result
, unsigned maxlen
)
1364 unsigned int m
, sz
= 0;
1365 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1366 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1367 char buffer
[ms
->nr_mirrors
+ 1];
1370 case STATUSTYPE_INFO
:
1371 DMEMIT("%d ", ms
->nr_mirrors
);
1372 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
1373 DMEMIT("%s ", ms
->mirror
[m
].dev
->name
);
1374 buffer
[m
] = device_status_char(&(ms
->mirror
[m
]));
1378 DMEMIT("%llu/%llu 1 %s ",
1379 (unsigned long long)log
->type
->get_sync_count(log
),
1380 (unsigned long long)ms
->nr_regions
, buffer
);
1382 sz
+= log
->type
->status(log
, type
, result
+sz
, maxlen
-sz
);
1386 case STATUSTYPE_TABLE
:
1387 sz
= log
->type
->status(log
, type
, result
, maxlen
);
1389 DMEMIT("%d", ms
->nr_mirrors
);
1390 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1391 DMEMIT(" %s %llu", ms
->mirror
[m
].dev
->name
,
1392 (unsigned long long)ms
->mirror
[m
].offset
);
1394 if (ms
->features
& DM_RAID1_HANDLE_ERRORS
)
1395 DMEMIT(" 1 handle_errors");
1399 static int mirror_iterate_devices(struct dm_target
*ti
,
1400 iterate_devices_callout_fn fn
, void *data
)
1402 struct mirror_set
*ms
= ti
->private;
1406 for (i
= 0; !ret
&& i
< ms
->nr_mirrors
; i
++)
1407 ret
= fn(ti
, ms
->mirror
[i
].dev
,
1408 ms
->mirror
[i
].offset
, ti
->len
, data
);
1413 static struct target_type mirror_target
= {
1415 .version
= {1, 13, 2},
1416 .module
= THIS_MODULE
,
1420 .end_io
= mirror_end_io
,
1421 .presuspend
= mirror_presuspend
,
1422 .postsuspend
= mirror_postsuspend
,
1423 .resume
= mirror_resume
,
1424 .status
= mirror_status
,
1425 .iterate_devices
= mirror_iterate_devices
,
1428 static int __init
dm_mirror_init(void)
1432 r
= dm_register_target(&mirror_target
);
1434 DMERR("Failed to register mirror target");
1444 static void __exit
dm_mirror_exit(void)
1446 dm_unregister_target(&mirror_target
);
1450 module_init(dm_mirror_init
);
1451 module_exit(dm_mirror_exit
);
1453 MODULE_DESCRIPTION(DM_NAME
" mirror target");
1454 MODULE_AUTHOR("Joe Thornber");
1455 MODULE_LICENSE("GPL");