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-list.h"
9 #include "dm-bio-record.h"
11 #include <linux/init.h>
12 #include <linux/mempool.h>
13 #include <linux/module.h>
14 #include <linux/pagemap.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/device-mapper.h>
18 #include <linux/dm-io.h>
19 #include <linux/dm-dirty-log.h>
20 #include <linux/dm-kcopyd.h>
21 #include <linux/dm-region-hash.h>
23 #define DM_MSG_PREFIX "raid1"
25 #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */
26 #define DM_IO_PAGES 64
27 #define DM_KCOPYD_PAGES 64
29 #define DM_RAID1_HANDLE_ERRORS 0x01
30 #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS)
32 static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped
);
34 /*-----------------------------------------------------------------
35 * Mirror set structures.
36 *---------------------------------------------------------------*/
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
;
62 struct dm_region_hash
*rh
;
63 struct dm_kcopyd_client
*kcopyd_client
;
64 struct dm_io_client
*io_client
;
65 mempool_t
*read_record_pool
;
73 atomic_t default_mirror
; /* Default mirror */
75 struct workqueue_struct
*kmirrord_wq
;
76 struct work_struct kmirrord_work
;
77 struct timer_list timer
;
78 unsigned long timer_pending
;
80 struct work_struct trigger_event
;
83 struct mirror mirror
[0];
86 static void wakeup_mirrord(void *context
)
88 struct mirror_set
*ms
= context
;
90 queue_work(ms
->kmirrord_wq
, &ms
->kmirrord_work
);
93 static void delayed_wake_fn(unsigned long data
)
95 struct mirror_set
*ms
= (struct mirror_set
*) data
;
97 clear_bit(0, &ms
->timer_pending
);
101 static void delayed_wake(struct mirror_set
*ms
)
103 if (test_and_set_bit(0, &ms
->timer_pending
))
106 ms
->timer
.expires
= jiffies
+ HZ
/ 5;
107 ms
->timer
.data
= (unsigned long) ms
;
108 ms
->timer
.function
= delayed_wake_fn
;
109 add_timer(&ms
->timer
);
112 static void wakeup_all_recovery_waiters(void *context
)
114 wake_up_all(&_kmirrord_recovery_stopped
);
117 static void queue_bio(struct mirror_set
*ms
, struct bio
*bio
, int rw
)
123 bl
= (rw
== WRITE
) ? &ms
->writes
: &ms
->reads
;
124 spin_lock_irqsave(&ms
->lock
, flags
);
125 should_wake
= !(bl
->head
);
126 bio_list_add(bl
, bio
);
127 spin_unlock_irqrestore(&ms
->lock
, flags
);
133 static void dispatch_bios(void *context
, struct bio_list
*bio_list
)
135 struct mirror_set
*ms
= context
;
138 while ((bio
= bio_list_pop(bio_list
)))
139 queue_bio(ms
, bio
, WRITE
);
142 #define MIN_READ_RECORDS 20
143 struct dm_raid1_read_record
{
145 struct dm_bio_details details
;
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
);
182 * @m: mirror device to fail
183 * @error_type: one of the enum's, DM_RAID1_*_ERROR
185 * If errors are being handled, record the type of
186 * error encountered for this device. If this type
187 * of error has already been recorded, we can return;
188 * otherwise, we must signal userspace by triggering
189 * an event. Additionally, if the device is the
190 * primary device, we must choose a new primary, but
191 * only if the mirror is in-sync.
193 * This function must not block.
195 static void fail_mirror(struct mirror
*m
, enum dm_raid1_error error_type
)
197 struct mirror_set
*ms
= m
->ms
;
201 * error_count is used for nothing more than a
202 * simple way to tell if a device has encountered
205 atomic_inc(&m
->error_count
);
207 if (test_and_set_bit(error_type
, &m
->error_type
))
210 if (!errors_handled(ms
))
213 if (m
!= get_default_mirror(ms
))
218 * Better to issue requests to same failing device
219 * than to risk returning corrupt data.
221 DMERR("Primary mirror (%s) failed while out-of-sync: "
222 "Reads may fail.", m
->dev
->name
);
226 for (new = ms
->mirror
; new < ms
->mirror
+ ms
->nr_mirrors
; new++)
227 if (!atomic_read(&new->error_count
)) {
228 set_default_mirror(new);
232 if (unlikely(new == ms
->mirror
+ ms
->nr_mirrors
))
233 DMWARN("All sides of mirror have failed.");
236 schedule_work(&ms
->trigger_event
);
239 /*-----------------------------------------------------------------
242 * When a mirror is first activated we may find that some regions
243 * are in the no-sync state. We have to recover these by
244 * recopying from the default mirror to all the others.
245 *---------------------------------------------------------------*/
246 static void recovery_complete(int read_err
, unsigned long write_err
,
249 struct dm_region
*reg
= context
;
250 struct mirror_set
*ms
= dm_rh_region_context(reg
);
254 /* Read error means the failure of default mirror. */
255 DMERR_LIMIT("Unable to read primary mirror during recovery");
256 fail_mirror(get_default_mirror(ms
), DM_RAID1_SYNC_ERROR
);
260 DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
263 * Bits correspond to devices (excluding default mirror).
264 * The default mirror cannot change during recovery.
266 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
267 if (&ms
->mirror
[m
] == get_default_mirror(ms
))
269 if (test_bit(bit
, &write_err
))
270 fail_mirror(ms
->mirror
+ m
,
271 DM_RAID1_SYNC_ERROR
);
276 dm_rh_recovery_end(reg
, !(read_err
|| write_err
));
279 static int recover(struct mirror_set
*ms
, struct dm_region
*reg
)
283 struct dm_io_region from
, to
[DM_KCOPYD_MAX_REGIONS
], *dest
;
285 unsigned long flags
= 0;
286 region_t key
= dm_rh_get_region_key(reg
);
287 sector_t region_size
= dm_rh_get_region_size(ms
->rh
);
289 /* fill in the source */
290 m
= get_default_mirror(ms
);
291 from
.bdev
= m
->dev
->bdev
;
292 from
.sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
293 if (key
== (ms
->nr_regions
- 1)) {
295 * The final region may be smaller than
298 from
.count
= ms
->ti
->len
& (region_size
- 1);
300 from
.count
= region_size
;
302 from
.count
= region_size
;
304 /* fill in the destinations */
305 for (i
= 0, dest
= to
; i
< ms
->nr_mirrors
; i
++) {
306 if (&ms
->mirror
[i
] == get_default_mirror(ms
))
310 dest
->bdev
= m
->dev
->bdev
;
311 dest
->sector
= m
->offset
+ dm_rh_region_to_sector(ms
->rh
, key
);
312 dest
->count
= from
.count
;
317 if (!errors_handled(ms
))
318 set_bit(DM_KCOPYD_IGNORE_ERROR
, &flags
);
320 r
= dm_kcopyd_copy(ms
->kcopyd_client
, &from
, ms
->nr_mirrors
- 1, to
,
321 flags
, recovery_complete
, reg
);
326 static void do_recovery(struct mirror_set
*ms
)
328 struct dm_region
*reg
;
329 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
333 * Start quiescing some regions.
335 dm_rh_recovery_prepare(ms
->rh
);
338 * Copy any already quiesced regions.
340 while ((reg
= dm_rh_recovery_start(ms
->rh
))) {
341 r
= recover(ms
, reg
);
343 dm_rh_recovery_end(reg
, 0);
347 * Update the in sync flag.
350 (log
->type
->get_sync_count(log
) == ms
->nr_regions
)) {
351 /* the sync is complete */
352 dm_table_event(ms
->ti
->table
);
357 /*-----------------------------------------------------------------
359 *---------------------------------------------------------------*/
360 static struct mirror
*choose_mirror(struct mirror_set
*ms
, sector_t sector
)
362 struct mirror
*m
= get_default_mirror(ms
);
365 if (likely(!atomic_read(&m
->error_count
)))
368 if (m
-- == ms
->mirror
)
370 } while (m
!= get_default_mirror(ms
));
375 static int default_ok(struct mirror
*m
)
377 struct mirror
*default_mirror
= get_default_mirror(m
->ms
);
379 return !atomic_read(&default_mirror
->error_count
);
382 static int mirror_available(struct mirror_set
*ms
, struct bio
*bio
)
384 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
385 region_t region
= dm_rh_bio_to_region(ms
->rh
, bio
);
387 if (log
->type
->in_sync(log
, region
, 0))
388 return choose_mirror(ms
, bio
->bi_sector
) ? 1 : 0;
394 * remap a buffer to a particular mirror.
396 static sector_t
map_sector(struct mirror
*m
, struct bio
*bio
)
398 return m
->offset
+ (bio
->bi_sector
- m
->ms
->ti
->begin
);
401 static void map_bio(struct mirror
*m
, struct bio
*bio
)
403 bio
->bi_bdev
= m
->dev
->bdev
;
404 bio
->bi_sector
= map_sector(m
, bio
);
407 static void map_region(struct dm_io_region
*io
, struct mirror
*m
,
410 io
->bdev
= m
->dev
->bdev
;
411 io
->sector
= map_sector(m
, bio
);
412 io
->count
= bio
->bi_size
>> 9;
415 /*-----------------------------------------------------------------
417 *---------------------------------------------------------------*/
418 static void read_callback(unsigned long error
, void *context
)
420 struct bio
*bio
= context
;
424 bio_set_m(bio
, NULL
);
426 if (likely(!error
)) {
431 fail_mirror(m
, DM_RAID1_READ_ERROR
);
433 if (likely(default_ok(m
)) || mirror_available(m
->ms
, bio
)) {
434 DMWARN_LIMIT("Read failure on mirror device %s. "
435 "Trying alternative device.",
437 queue_bio(m
->ms
, bio
, bio_rw(bio
));
441 DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.",
443 bio_endio(bio
, -EIO
);
446 /* Asynchronous read. */
447 static void read_async_bio(struct mirror
*m
, struct bio
*bio
)
449 struct dm_io_region io
;
450 struct dm_io_request io_req
= {
452 .mem
.type
= DM_IO_BVEC
,
453 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
454 .notify
.fn
= read_callback
,
455 .notify
.context
= bio
,
456 .client
= m
->ms
->io_client
,
459 map_region(&io
, m
, bio
);
461 BUG_ON(dm_io(&io_req
, 1, &io
, NULL
));
464 static inline int region_in_sync(struct mirror_set
*ms
, region_t region
,
467 int state
= dm_rh_get_state(ms
->rh
, region
, may_block
);
468 return state
== DM_RH_CLEAN
|| state
== DM_RH_DIRTY
;
471 static void do_reads(struct mirror_set
*ms
, struct bio_list
*reads
)
477 while ((bio
= bio_list_pop(reads
))) {
478 region
= dm_rh_bio_to_region(ms
->rh
, bio
);
479 m
= get_default_mirror(ms
);
482 * We can only read balance if the region is in sync.
484 if (likely(region_in_sync(ms
, region
, 1)))
485 m
= choose_mirror(ms
, bio
->bi_sector
);
486 else if (m
&& atomic_read(&m
->error_count
))
490 read_async_bio(m
, bio
);
492 bio_endio(bio
, -EIO
);
496 /*-----------------------------------------------------------------
499 * We do different things with the write io depending on the
500 * state of the region that it's in:
502 * SYNC: increment pending, use kcopyd to write to *all* mirrors
503 * RECOVERING: delay the io until recovery completes
504 * NOSYNC: increment pending, just write to the default mirror
505 *---------------------------------------------------------------*/
508 static void write_callback(unsigned long error
, void *context
)
511 struct bio
*bio
= (struct bio
*) context
;
512 struct mirror_set
*ms
;
517 ms
= bio_get_m(bio
)->ms
;
518 bio_set_m(bio
, NULL
);
521 * NOTE: We don't decrement the pending count here,
522 * instead it is done by the targets endio function.
523 * This way we handle both writes to SYNC and NOSYNC
524 * regions with the same code.
529 for (i
= 0; i
< ms
->nr_mirrors
; i
++)
530 if (test_bit(i
, &error
))
531 fail_mirror(ms
->mirror
+ i
, DM_RAID1_WRITE_ERROR
);
535 if (unlikely(!uptodate
)) {
536 DMERR("All replicated volumes dead, failing I/O");
537 /* None of the writes succeeded, fail the I/O. */
539 } else if (errors_handled(ms
)) {
541 * Need to raise event. Since raising
542 * events can block, we need to do it in
545 spin_lock_irqsave(&ms
->lock
, flags
);
546 if (!ms
->failures
.head
)
548 bio_list_add(&ms
->failures
, bio
);
549 spin_unlock_irqrestore(&ms
->lock
, flags
);
558 static void do_write(struct mirror_set
*ms
, struct bio
*bio
)
561 struct dm_io_region io
[ms
->nr_mirrors
], *dest
= io
;
563 struct dm_io_request io_req
= {
565 .mem
.type
= DM_IO_BVEC
,
566 .mem
.ptr
.bvec
= bio
->bi_io_vec
+ bio
->bi_idx
,
567 .notify
.fn
= write_callback
,
568 .notify
.context
= bio
,
569 .client
= ms
->io_client
,
572 for (i
= 0, m
= ms
->mirror
; i
< ms
->nr_mirrors
; i
++, m
++)
573 map_region(dest
++, m
, bio
);
576 * Use default mirror because we only need it to retrieve the reference
577 * to the mirror set in write_callback().
579 bio_set_m(bio
, get_default_mirror(ms
));
581 BUG_ON(dm_io(&io_req
, ms
->nr_mirrors
, io
, NULL
));
584 static void do_writes(struct mirror_set
*ms
, struct bio_list
*writes
)
588 struct bio_list sync
, nosync
, recover
, *this_list
= NULL
;
594 * Classify each write.
596 bio_list_init(&sync
);
597 bio_list_init(&nosync
);
598 bio_list_init(&recover
);
600 while ((bio
= bio_list_pop(writes
))) {
601 state
= dm_rh_get_state(ms
->rh
,
602 dm_rh_bio_to_region(ms
->rh
, bio
), 1);
613 case DM_RH_RECOVERING
:
614 this_list
= &recover
;
618 bio_list_add(this_list
, bio
);
622 * Increment the pending counts for any regions that will
623 * be written to (writes to recover regions are going to
626 dm_rh_inc_pending(ms
->rh
, &sync
);
627 dm_rh_inc_pending(ms
->rh
, &nosync
);
628 ms
->log_failure
= dm_rh_flush(ms
->rh
) ? 1 : 0;
633 if (unlikely(ms
->log_failure
)) {
634 spin_lock_irq(&ms
->lock
);
635 bio_list_merge(&ms
->failures
, &sync
);
636 spin_unlock_irq(&ms
->lock
);
639 while ((bio
= bio_list_pop(&sync
)))
642 while ((bio
= bio_list_pop(&recover
)))
643 dm_rh_delay(ms
->rh
, bio
);
645 while ((bio
= bio_list_pop(&nosync
))) {
646 map_bio(get_default_mirror(ms
), bio
);
647 generic_make_request(bio
);
651 static void do_failures(struct mirror_set
*ms
, struct bio_list
*failures
)
658 if (!ms
->log_failure
) {
659 while ((bio
= bio_list_pop(failures
))) {
661 dm_rh_mark_nosync(ms
->rh
, bio
, bio
->bi_size
, 0);
667 * If the log has failed, unattempted writes are being
668 * put on the failures list. We can't issue those writes
669 * until a log has been marked, so we must store them.
671 * If a 'noflush' suspend is in progress, we can requeue
672 * the I/O's to the core. This give userspace a chance
673 * to reconfigure the mirror, at which point the core
674 * will reissue the writes. If the 'noflush' flag is
675 * not set, we have no choice but to return errors.
677 * Some writes on the failures list may have been
678 * submitted before the log failure and represent a
679 * failure to write to one of the devices. It is ok
680 * for us to treat them the same and requeue them
683 if (dm_noflush_suspending(ms
->ti
)) {
684 while ((bio
= bio_list_pop(failures
)))
685 bio_endio(bio
, DM_ENDIO_REQUEUE
);
689 if (atomic_read(&ms
->suspend
)) {
690 while ((bio
= bio_list_pop(failures
)))
691 bio_endio(bio
, -EIO
);
695 spin_lock_irq(&ms
->lock
);
696 bio_list_merge(&ms
->failures
, failures
);
697 spin_unlock_irq(&ms
->lock
);
702 static void trigger_event(struct work_struct
*work
)
704 struct mirror_set
*ms
=
705 container_of(work
, struct mirror_set
, trigger_event
);
707 dm_table_event(ms
->ti
->table
);
710 /*-----------------------------------------------------------------
712 *---------------------------------------------------------------*/
713 static void do_mirror(struct work_struct
*work
)
715 struct mirror_set
*ms
= container_of(work
, struct mirror_set
,
717 struct bio_list reads
, writes
, failures
;
720 spin_lock_irqsave(&ms
->lock
, flags
);
723 failures
= ms
->failures
;
724 bio_list_init(&ms
->reads
);
725 bio_list_init(&ms
->writes
);
726 bio_list_init(&ms
->failures
);
727 spin_unlock_irqrestore(&ms
->lock
, flags
);
729 dm_rh_update_states(ms
->rh
, errors_handled(ms
));
731 do_reads(ms
, &reads
);
732 do_writes(ms
, &writes
);
733 do_failures(ms
, &failures
);
735 dm_table_unplug_all(ms
->ti
->table
);
738 /*-----------------------------------------------------------------
740 *---------------------------------------------------------------*/
741 static struct mirror_set
*alloc_context(unsigned int nr_mirrors
,
742 uint32_t region_size
,
743 struct dm_target
*ti
,
744 struct dm_dirty_log
*dl
)
747 struct mirror_set
*ms
= NULL
;
749 len
= sizeof(*ms
) + (sizeof(ms
->mirror
[0]) * nr_mirrors
);
751 ms
= kzalloc(len
, GFP_KERNEL
);
753 ti
->error
= "Cannot allocate mirror context";
757 spin_lock_init(&ms
->lock
);
760 ms
->nr_mirrors
= nr_mirrors
;
761 ms
->nr_regions
= dm_sector_div_up(ti
->len
, region_size
);
764 atomic_set(&ms
->suspend
, 0);
765 atomic_set(&ms
->default_mirror
, DEFAULT_MIRROR
);
767 len
= sizeof(struct dm_raid1_read_record
);
768 ms
->read_record_pool
= mempool_create_kmalloc_pool(MIN_READ_RECORDS
,
770 if (!ms
->read_record_pool
) {
771 ti
->error
= "Error creating mirror read_record_pool";
776 ms
->io_client
= dm_io_client_create(DM_IO_PAGES
);
777 if (IS_ERR(ms
->io_client
)) {
778 ti
->error
= "Error creating dm_io client";
779 mempool_destroy(ms
->read_record_pool
);
784 ms
->rh
= dm_region_hash_create(ms
, dispatch_bios
, wakeup_mirrord
,
785 wakeup_all_recovery_waiters
,
786 ms
->ti
->begin
, MAX_RECOVERY
,
787 dl
, region_size
, ms
->nr_regions
);
788 if (IS_ERR(ms
->rh
)) {
789 ti
->error
= "Error creating dirty region hash";
790 dm_io_client_destroy(ms
->io_client
);
791 mempool_destroy(ms
->read_record_pool
);
799 static void free_context(struct mirror_set
*ms
, struct dm_target
*ti
,
803 dm_put_device(ti
, ms
->mirror
[m
].dev
);
805 dm_io_client_destroy(ms
->io_client
);
806 dm_region_hash_destroy(ms
->rh
);
807 mempool_destroy(ms
->read_record_pool
);
811 static int get_mirror(struct mirror_set
*ms
, struct dm_target
*ti
,
812 unsigned int mirror
, char **argv
)
814 unsigned long long offset
;
816 if (sscanf(argv
[1], "%llu", &offset
) != 1) {
817 ti
->error
= "Invalid offset";
821 if (dm_get_device(ti
, argv
[0], offset
, ti
->len
,
822 dm_table_get_mode(ti
->table
),
823 &ms
->mirror
[mirror
].dev
)) {
824 ti
->error
= "Device lookup failure";
828 ms
->mirror
[mirror
].ms
= ms
;
829 atomic_set(&(ms
->mirror
[mirror
].error_count
), 0);
830 ms
->mirror
[mirror
].error_type
= 0;
831 ms
->mirror
[mirror
].offset
= offset
;
837 * Create dirty log: log_type #log_params <log_params>
839 static struct dm_dirty_log
*create_dirty_log(struct dm_target
*ti
,
840 unsigned argc
, char **argv
,
843 unsigned param_count
;
844 struct dm_dirty_log
*dl
;
847 ti
->error
= "Insufficient mirror log arguments";
851 if (sscanf(argv
[1], "%u", ¶m_count
) != 1) {
852 ti
->error
= "Invalid mirror log argument count";
856 *args_used
= 2 + param_count
;
858 if (argc
< *args_used
) {
859 ti
->error
= "Insufficient mirror log arguments";
863 dl
= dm_dirty_log_create(argv
[0], ti
, param_count
, argv
+ 2);
865 ti
->error
= "Error creating mirror dirty log";
872 static int parse_features(struct mirror_set
*ms
, unsigned argc
, char **argv
,
875 unsigned num_features
;
876 struct dm_target
*ti
= ms
->ti
;
883 if (sscanf(argv
[0], "%u", &num_features
) != 1) {
884 ti
->error
= "Invalid number of features";
892 if (num_features
> argc
) {
893 ti
->error
= "Not enough arguments to support feature count";
897 if (!strcmp("handle_errors", argv
[0]))
898 ms
->features
|= DM_RAID1_HANDLE_ERRORS
;
900 ti
->error
= "Unrecognised feature requested";
910 * Construct a mirror mapping:
912 * log_type #log_params <log_params>
913 * #mirrors [mirror_path offset]{2,}
914 * [#features <features>]
916 * log_type is "core" or "disk"
917 * #log_params is between 1 and 3
919 * If present, features must be "handle_errors".
921 static int mirror_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
924 unsigned int nr_mirrors
, m
, args_used
;
925 struct mirror_set
*ms
;
926 struct dm_dirty_log
*dl
;
928 dl
= create_dirty_log(ti
, argc
, argv
, &args_used
);
935 if (!argc
|| sscanf(argv
[0], "%u", &nr_mirrors
) != 1 ||
936 nr_mirrors
< 2 || nr_mirrors
> DM_KCOPYD_MAX_REGIONS
+ 1) {
937 ti
->error
= "Invalid number of mirrors";
938 dm_dirty_log_destroy(dl
);
944 if (argc
< nr_mirrors
* 2) {
945 ti
->error
= "Too few mirror arguments";
946 dm_dirty_log_destroy(dl
);
950 ms
= alloc_context(nr_mirrors
, dl
->type
->get_region_size(dl
), ti
, dl
);
952 dm_dirty_log_destroy(dl
);
956 /* Get the mirror parameter sets */
957 for (m
= 0; m
< nr_mirrors
; m
++) {
958 r
= get_mirror(ms
, ti
, m
, argv
);
960 free_context(ms
, ti
, m
);
968 ti
->split_io
= dm_rh_get_region_size(ms
->rh
);
970 ms
->kmirrord_wq
= create_singlethread_workqueue("kmirrord");
971 if (!ms
->kmirrord_wq
) {
972 DMERR("couldn't start kmirrord");
974 goto err_free_context
;
976 INIT_WORK(&ms
->kmirrord_work
, do_mirror
);
977 init_timer(&ms
->timer
);
978 ms
->timer_pending
= 0;
979 INIT_WORK(&ms
->trigger_event
, trigger_event
);
981 r
= parse_features(ms
, argc
, argv
, &args_used
);
989 * Any read-balancing addition depends on the
990 * DM_RAID1_HANDLE_ERRORS flag being present.
991 * This is because the decision to balance depends
992 * on the sync state of a region. If the above
993 * flag is not present, we ignore errors; and
994 * the sync state may be inaccurate.
998 ti
->error
= "Too many mirror arguments";
1000 goto err_destroy_wq
;
1003 r
= dm_kcopyd_client_create(DM_KCOPYD_PAGES
, &ms
->kcopyd_client
);
1005 goto err_destroy_wq
;
1011 destroy_workqueue(ms
->kmirrord_wq
);
1013 free_context(ms
, ti
, ms
->nr_mirrors
);
1017 static void mirror_dtr(struct dm_target
*ti
)
1019 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1021 del_timer_sync(&ms
->timer
);
1022 flush_workqueue(ms
->kmirrord_wq
);
1023 flush_scheduled_work();
1024 dm_kcopyd_client_destroy(ms
->kcopyd_client
);
1025 destroy_workqueue(ms
->kmirrord_wq
);
1026 free_context(ms
, ti
, ms
->nr_mirrors
);
1030 * Mirror mapping function
1032 static int mirror_map(struct dm_target
*ti
, struct bio
*bio
,
1033 union map_info
*map_context
)
1035 int r
, rw
= bio_rw(bio
);
1037 struct mirror_set
*ms
= ti
->private;
1038 struct dm_raid1_read_record
*read_record
= NULL
;
1039 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1042 /* Save region for mirror_end_io() handler */
1043 map_context
->ll
= dm_rh_bio_to_region(ms
->rh
, bio
);
1044 queue_bio(ms
, bio
, rw
);
1045 return DM_MAPIO_SUBMITTED
;
1048 r
= log
->type
->in_sync(log
, dm_rh_bio_to_region(ms
->rh
, bio
), 0);
1049 if (r
< 0 && r
!= -EWOULDBLOCK
)
1053 * If region is not in-sync queue the bio.
1055 if (!r
|| (r
== -EWOULDBLOCK
)) {
1057 return -EWOULDBLOCK
;
1059 queue_bio(ms
, bio
, rw
);
1060 return DM_MAPIO_SUBMITTED
;
1064 * The region is in-sync and we can perform reads directly.
1065 * Store enough information so we can retry if it fails.
1067 m
= choose_mirror(ms
, bio
->bi_sector
);
1071 read_record
= mempool_alloc(ms
->read_record_pool
, GFP_NOIO
);
1072 if (likely(read_record
)) {
1073 dm_bio_record(&read_record
->details
, bio
);
1074 map_context
->ptr
= read_record
;
1080 return DM_MAPIO_REMAPPED
;
1083 static int mirror_end_io(struct dm_target
*ti
, struct bio
*bio
,
1084 int error
, union map_info
*map_context
)
1086 int rw
= bio_rw(bio
);
1087 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1088 struct mirror
*m
= NULL
;
1089 struct dm_bio_details
*bd
= NULL
;
1090 struct dm_raid1_read_record
*read_record
= map_context
->ptr
;
1093 * We need to dec pending if this was a write.
1096 dm_rh_dec(ms
->rh
, map_context
->ll
);
1100 if (error
== -EOPNOTSUPP
)
1103 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1106 if (unlikely(error
)) {
1109 * There wasn't enough memory to record necessary
1110 * information for a retry or there was no other
1113 DMERR_LIMIT("Mirror read failed.");
1119 DMERR("Mirror read failed from %s. Trying alternative device.",
1122 fail_mirror(m
, DM_RAID1_READ_ERROR
);
1125 * A failed read is requeued for another attempt using an intact
1128 if (default_ok(m
) || mirror_available(ms
, bio
)) {
1129 bd
= &read_record
->details
;
1131 dm_bio_restore(bd
, bio
);
1132 mempool_free(read_record
, ms
->read_record_pool
);
1133 map_context
->ptr
= NULL
;
1134 queue_bio(ms
, bio
, rw
);
1137 DMERR("All replicated volumes dead, failing I/O");
1142 mempool_free(read_record
, ms
->read_record_pool
);
1143 map_context
->ptr
= NULL
;
1149 static void mirror_presuspend(struct dm_target
*ti
)
1151 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1152 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1154 atomic_set(&ms
->suspend
, 1);
1157 * We must finish up all the work that we've
1158 * generated (i.e. recovery work).
1160 dm_rh_stop_recovery(ms
->rh
);
1162 wait_event(_kmirrord_recovery_stopped
,
1163 !dm_rh_recovery_in_flight(ms
->rh
));
1165 if (log
->type
->presuspend
&& log
->type
->presuspend(log
))
1166 /* FIXME: need better error handling */
1167 DMWARN("log presuspend failed");
1170 * Now that recovery is complete/stopped and the
1171 * delayed bios are queued, we need to wait for
1172 * the worker thread to complete. This way,
1173 * we know that all of our I/O has been pushed.
1175 flush_workqueue(ms
->kmirrord_wq
);
1178 static void mirror_postsuspend(struct dm_target
*ti
)
1180 struct mirror_set
*ms
= ti
->private;
1181 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1183 if (log
->type
->postsuspend
&& log
->type
->postsuspend(log
))
1184 /* FIXME: need better error handling */
1185 DMWARN("log postsuspend failed");
1188 static void mirror_resume(struct dm_target
*ti
)
1190 struct mirror_set
*ms
= ti
->private;
1191 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1193 atomic_set(&ms
->suspend
, 0);
1194 if (log
->type
->resume
&& log
->type
->resume(log
))
1195 /* FIXME: need better error handling */
1196 DMWARN("log resume failed");
1197 dm_rh_start_recovery(ms
->rh
);
1201 * device_status_char
1202 * @m: mirror device/leg we want the status of
1204 * We return one character representing the most severe error
1205 * we have encountered.
1206 * A => Alive - No failures
1207 * D => Dead - A write failure occurred leaving mirror out-of-sync
1208 * S => Sync - A sychronization failure occurred, mirror out-of-sync
1209 * R => Read - A read failure occurred, mirror data unaffected
1213 static char device_status_char(struct mirror
*m
)
1215 if (!atomic_read(&(m
->error_count
)))
1218 return (test_bit(DM_RAID1_WRITE_ERROR
, &(m
->error_type
))) ? 'D' :
1219 (test_bit(DM_RAID1_SYNC_ERROR
, &(m
->error_type
))) ? 'S' :
1220 (test_bit(DM_RAID1_READ_ERROR
, &(m
->error_type
))) ? 'R' : 'U';
1224 static int mirror_status(struct dm_target
*ti
, status_type_t type
,
1225 char *result
, unsigned int maxlen
)
1227 unsigned int m
, sz
= 0;
1228 struct mirror_set
*ms
= (struct mirror_set
*) ti
->private;
1229 struct dm_dirty_log
*log
= dm_rh_dirty_log(ms
->rh
);
1230 char buffer
[ms
->nr_mirrors
+ 1];
1233 case STATUSTYPE_INFO
:
1234 DMEMIT("%d ", ms
->nr_mirrors
);
1235 for (m
= 0; m
< ms
->nr_mirrors
; m
++) {
1236 DMEMIT("%s ", ms
->mirror
[m
].dev
->name
);
1237 buffer
[m
] = device_status_char(&(ms
->mirror
[m
]));
1241 DMEMIT("%llu/%llu 1 %s ",
1242 (unsigned long long)log
->type
->get_sync_count(log
),
1243 (unsigned long long)ms
->nr_regions
, buffer
);
1245 sz
+= log
->type
->status(log
, type
, result
+sz
, maxlen
-sz
);
1249 case STATUSTYPE_TABLE
:
1250 sz
= log
->type
->status(log
, type
, result
, maxlen
);
1252 DMEMIT("%d", ms
->nr_mirrors
);
1253 for (m
= 0; m
< ms
->nr_mirrors
; m
++)
1254 DMEMIT(" %s %llu", ms
->mirror
[m
].dev
->name
,
1255 (unsigned long long)ms
->mirror
[m
].offset
);
1257 if (ms
->features
& DM_RAID1_HANDLE_ERRORS
)
1258 DMEMIT(" 1 handle_errors");
1264 static struct target_type mirror_target
= {
1266 .version
= {1, 0, 20},
1267 .module
= THIS_MODULE
,
1271 .end_io
= mirror_end_io
,
1272 .presuspend
= mirror_presuspend
,
1273 .postsuspend
= mirror_postsuspend
,
1274 .resume
= mirror_resume
,
1275 .status
= mirror_status
,
1278 static int __init
dm_mirror_init(void)
1282 r
= dm_register_target(&mirror_target
);
1284 DMERR("Failed to register mirror target");
1289 static void __exit
dm_mirror_exit(void)
1291 dm_unregister_target(&mirror_target
);
1295 module_init(dm_mirror_init
);
1296 module_exit(dm_mirror_exit
);
1298 MODULE_DESCRIPTION(DM_NAME
" mirror target");
1299 MODULE_AUTHOR("Joe Thornber");
1300 MODULE_LICENSE("GPL");