Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Sistina Software Limited. |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 3 | * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 8 | #include "dm-bio-record.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 16 | #include <linux/device-mapper.h> |
Alasdair G Kergon | a765e20 | 2008-04-24 22:02:01 +0100 | [diff] [blame] | 17 | #include <linux/dm-io.h> |
| 18 | #include <linux/dm-dirty-log.h> |
| 19 | #include <linux/dm-kcopyd.h> |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 20 | #include <linux/dm-region-hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 22 | #define DM_MSG_PREFIX "raid1" |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 23 | |
| 24 | #define MAX_RECOVERY 1 /* Maximum number of regions recovered in parallel. */ |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 25 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 26 | #define DM_RAID1_HANDLE_ERRORS 0x01 |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 27 | #define errors_handled(p) ((p)->features & DM_RAID1_HANDLE_ERRORS) |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 28 | |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 29 | static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /*----------------------------------------------------------------- |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 32 | * Mirror set structures. |
| 33 | *---------------------------------------------------------------*/ |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 34 | enum dm_raid1_error { |
| 35 | DM_RAID1_WRITE_ERROR, |
Mikulas Patocka | 64b30c4 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 36 | DM_RAID1_FLUSH_ERROR, |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 37 | DM_RAID1_SYNC_ERROR, |
| 38 | DM_RAID1_READ_ERROR |
| 39 | }; |
| 40 | |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 41 | struct mirror { |
Jonathan Brassow | aa5617c | 2007-10-19 22:47:58 +0100 | [diff] [blame] | 42 | struct mirror_set *ms; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 43 | atomic_t error_count; |
Al Viro | 39ed7ad | 2008-02-13 03:53:00 +0000 | [diff] [blame] | 44 | unsigned long error_type; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 45 | struct dm_dev *dev; |
| 46 | sector_t offset; |
| 47 | }; |
| 48 | |
| 49 | struct mirror_set { |
| 50 | struct dm_target *ti; |
| 51 | struct list_head list; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 52 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 53 | uint64_t features; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 54 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 55 | spinlock_t lock; /* protects the lists */ |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 56 | struct bio_list reads; |
| 57 | struct bio_list writes; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 58 | struct bio_list failures; |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 59 | struct bio_list holds; /* bios are waiting until suspend */ |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 60 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 61 | struct dm_region_hash *rh; |
| 62 | struct dm_kcopyd_client *kcopyd_client; |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 63 | struct dm_io_client *io_client; |
| 64 | |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 65 | /* recovery */ |
| 66 | region_t nr_regions; |
| 67 | int in_sync; |
Jonathan Brassow | fc1ff95 | 2007-07-12 17:29:15 +0100 | [diff] [blame] | 68 | int log_failure; |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 69 | int leg_failure; |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 70 | atomic_t suspend; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 71 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 72 | atomic_t default_mirror; /* Default mirror */ |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 73 | |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 74 | struct workqueue_struct *kmirrord_wq; |
| 75 | struct work_struct kmirrord_work; |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 76 | struct timer_list timer; |
| 77 | unsigned long timer_pending; |
| 78 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 79 | struct work_struct trigger_event; |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 80 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 81 | unsigned nr_mirrors; |
Neil Brown | e4c8b3b | 2006-06-26 00:27:26 -0700 | [diff] [blame] | 82 | struct mirror mirror[0]; |
| 83 | }; |
| 84 | |
Mikulas Patocka | df5d2e9 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 85 | DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle, |
| 86 | "A percentage of time allocated for raid resynchronization"); |
| 87 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 88 | static void wakeup_mirrord(void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 90 | struct mirror_set *ms = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 92 | queue_work(ms->kmirrord_wq, &ms->kmirrord_work); |
| 93 | } |
| 94 | |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 95 | static void delayed_wake_fn(unsigned long data) |
| 96 | { |
| 97 | struct mirror_set *ms = (struct mirror_set *) data; |
| 98 | |
| 99 | clear_bit(0, &ms->timer_pending); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 100 | wakeup_mirrord(ms); |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void delayed_wake(struct mirror_set *ms) |
| 104 | { |
| 105 | if (test_and_set_bit(0, &ms->timer_pending)) |
| 106 | return; |
| 107 | |
| 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); |
| 112 | } |
| 113 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 114 | static void wakeup_all_recovery_waiters(void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 116 | wake_up_all(&_kmirrord_recovery_stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 119 | static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | int should_wake = 0; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 123 | struct bio_list *bl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | if (should_wake) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 132 | wakeup_mirrord(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 135 | static void dispatch_bios(void *context, struct bio_list *bio_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 137 | struct mirror_set *ms = context; |
| 138 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 140 | while ((bio = bio_list_pop(bio_list))) |
| 141 | queue_bio(ms, bio, WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Mikulas Patocka | 89c7cd8 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 144 | struct dm_raid1_bio_record { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 145 | struct mirror *m; |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 146 | /* if details->bi_bdev == NULL, details were not saved */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 147 | struct dm_bio_details details; |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 148 | region_t write_region; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Every mirror should look like this one. |
| 153 | */ |
| 154 | #define DEFAULT_MIRROR 0 |
| 155 | |
| 156 | /* |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 157 | * This is yucky. We squirrel the mirror struct away inside |
| 158 | * bi_next for read/write buffers. This is safe since the bh |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | * doesn't get submitted to the lower levels of block layer. |
| 160 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 161 | static struct mirror *bio_get_m(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 163 | return (struct mirror *) bio->bi_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 166 | static void bio_set_m(struct bio *bio, struct mirror *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 168 | bio->bi_next = (struct bio *) m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 171 | static struct mirror *get_default_mirror(struct mirror_set *ms) |
| 172 | { |
| 173 | return &ms->mirror[atomic_read(&ms->default_mirror)]; |
| 174 | } |
| 175 | |
| 176 | static void set_default_mirror(struct mirror *m) |
| 177 | { |
| 178 | struct mirror_set *ms = m->ms; |
| 179 | struct mirror *m0 = &(ms->mirror[0]); |
| 180 | |
| 181 | atomic_set(&ms->default_mirror, m - m0); |
| 182 | } |
| 183 | |
Mikulas Patocka | 87968dd | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 184 | static struct mirror *get_valid_mirror(struct mirror_set *ms) |
| 185 | { |
| 186 | struct mirror *m; |
| 187 | |
| 188 | for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++) |
| 189 | if (!atomic_read(&m->error_count)) |
| 190 | return m; |
| 191 | |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 195 | /* fail_mirror |
| 196 | * @m: mirror device to fail |
| 197 | * @error_type: one of the enum's, DM_RAID1_*_ERROR |
| 198 | * |
| 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. |
| 206 | * |
| 207 | * This function must not block. |
| 208 | */ |
| 209 | static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type) |
| 210 | { |
| 211 | struct mirror_set *ms = m->ms; |
| 212 | struct mirror *new; |
| 213 | |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 214 | ms->leg_failure = 1; |
| 215 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 216 | /* |
| 217 | * error_count is used for nothing more than a |
| 218 | * simple way to tell if a device has encountered |
| 219 | * errors. |
| 220 | */ |
| 221 | atomic_inc(&m->error_count); |
| 222 | |
| 223 | if (test_and_set_bit(error_type, &m->error_type)) |
| 224 | return; |
| 225 | |
Jonathan Brassow | d460c65 | 2009-01-06 03:04:57 +0000 | [diff] [blame] | 226 | if (!errors_handled(ms)) |
| 227 | return; |
| 228 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 229 | if (m != get_default_mirror(ms)) |
| 230 | goto out; |
| 231 | |
| 232 | if (!ms->in_sync) { |
| 233 | /* |
| 234 | * Better to issue requests to same failing device |
| 235 | * than to risk returning corrupt data. |
| 236 | */ |
| 237 | DMERR("Primary mirror (%s) failed while out-of-sync: " |
| 238 | "Reads may fail.", m->dev->name); |
| 239 | goto out; |
| 240 | } |
| 241 | |
Mikulas Patocka | 87968dd | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 242 | new = get_valid_mirror(ms); |
| 243 | if (new) |
| 244 | set_default_mirror(new); |
| 245 | else |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 246 | DMWARN("All sides of mirror have failed."); |
| 247 | |
| 248 | out: |
| 249 | schedule_work(&ms->trigger_event); |
| 250 | } |
| 251 | |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 252 | static int mirror_flush(struct dm_target *ti) |
| 253 | { |
| 254 | struct mirror_set *ms = ti->private; |
| 255 | unsigned long error_bits; |
| 256 | |
| 257 | unsigned int i; |
| 258 | struct dm_io_region io[ms->nr_mirrors]; |
| 259 | struct mirror *m; |
| 260 | struct dm_io_request io_req = { |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 261 | .bi_rw = WRITE_FLUSH, |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 262 | .mem.type = DM_IO_KMEM, |
Mike Snitzer | 5fc2ffe | 2011-01-13 19:59:48 +0000 | [diff] [blame] | 263 | .mem.ptr.addr = NULL, |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 264 | .client = ms->io_client, |
| 265 | }; |
| 266 | |
| 267 | for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) { |
| 268 | io[i].bdev = m->dev->bdev; |
| 269 | io[i].sector = 0; |
| 270 | io[i].count = 0; |
| 271 | } |
| 272 | |
| 273 | error_bits = -1; |
| 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, |
Mikulas Patocka | 64b30c4 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 279 | DM_RAID1_FLUSH_ERROR); |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 280 | return -EIO; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /*----------------------------------------------------------------- |
| 287 | * Recovery. |
| 288 | * |
| 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 | *---------------------------------------------------------------*/ |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 293 | static void recovery_complete(int read_err, unsigned long write_err, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | void *context) |
| 295 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 296 | struct dm_region *reg = context; |
| 297 | struct mirror_set *ms = dm_rh_region_context(reg); |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 298 | int m, bit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 300 | if (read_err) { |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 301 | /* Read error means the failure of default mirror. */ |
| 302 | DMERR_LIMIT("Unable to read primary mirror during recovery"); |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 303 | fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR); |
| 304 | } |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 305 | |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 306 | if (write_err) { |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 307 | DMERR_LIMIT("Write error during recovery (error = 0x%lx)", |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 308 | write_err); |
Jonathan Brassow | 8f0205b | 2008-02-08 02:11:32 +0000 | [diff] [blame] | 309 | /* |
| 310 | * Bits correspond to devices (excluding default mirror). |
| 311 | * The default mirror cannot change during recovery. |
| 312 | */ |
| 313 | for (m = 0; m < ms->nr_mirrors; m++) { |
| 314 | if (&ms->mirror[m] == get_default_mirror(ms)) |
| 315 | continue; |
| 316 | if (test_bit(bit, &write_err)) |
| 317 | fail_mirror(ms->mirror + m, |
| 318 | DM_RAID1_SYNC_ERROR); |
| 319 | bit++; |
| 320 | } |
| 321 | } |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 322 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 323 | dm_rh_recovery_end(reg, !(read_err || write_err)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 326 | static int recover(struct mirror_set *ms, struct dm_region *reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
| 328 | int r; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 329 | unsigned i; |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 330 | struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct mirror *m; |
| 332 | unsigned long flags = 0; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 333 | region_t key = dm_rh_get_region_key(reg); |
| 334 | sector_t region_size = dm_rh_get_region_size(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /* fill in the source */ |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 337 | m = get_default_mirror(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | from.bdev = m->dev->bdev; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 339 | from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key); |
| 340 | if (key == (ms->nr_regions - 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | /* |
| 342 | * The final region may be smaller than |
| 343 | * region_size. |
| 344 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 345 | from.count = ms->ti->len & (region_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (!from.count) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 347 | from.count = region_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } else |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 349 | from.count = region_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | /* fill in the destinations */ |
| 352 | for (i = 0, dest = to; i < ms->nr_mirrors; i++) { |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 353 | if (&ms->mirror[i] == get_default_mirror(ms)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | continue; |
| 355 | |
| 356 | m = ms->mirror + i; |
| 357 | dest->bdev = m->dev->bdev; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 358 | dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | dest->count = from.count; |
| 360 | dest++; |
| 361 | } |
| 362 | |
| 363 | /* hand to kcopyd */ |
Jonathan Brassow | f7c83e2 | 2008-10-10 13:36:59 +0100 | [diff] [blame] | 364 | if (!errors_handled(ms)) |
| 365 | set_bit(DM_KCOPYD_IGNORE_ERROR, &flags); |
| 366 | |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 367 | r = dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, |
| 368 | flags, recovery_complete, reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | return r; |
| 371 | } |
| 372 | |
| 373 | static void do_recovery(struct mirror_set *ms) |
| 374 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 375 | struct dm_region *reg; |
| 376 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Start quiescing some regions. |
| 381 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 382 | dm_rh_recovery_prepare(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Copy any already quiesced regions. |
| 386 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 387 | while ((reg = dm_rh_recovery_start(ms->rh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | r = recover(ms, reg); |
| 389 | if (r) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 390 | dm_rh_recovery_end(reg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Update the in sync flag. |
| 395 | */ |
| 396 | if (!ms->in_sync && |
| 397 | (log->type->get_sync_count(log) == ms->nr_regions)) { |
| 398 | /* the sync is complete */ |
| 399 | dm_table_event(ms->ti->table); |
| 400 | ms->in_sync = 1; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /*----------------------------------------------------------------- |
| 405 | * Reads |
| 406 | *---------------------------------------------------------------*/ |
| 407 | static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector) |
| 408 | { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 409 | struct mirror *m = get_default_mirror(ms); |
| 410 | |
| 411 | do { |
| 412 | if (likely(!atomic_read(&m->error_count))) |
| 413 | return m; |
| 414 | |
| 415 | if (m-- == ms->mirror) |
| 416 | m += ms->nr_mirrors; |
| 417 | } while (m != get_default_mirror(ms)); |
| 418 | |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | static int default_ok(struct mirror *m) |
| 423 | { |
| 424 | struct mirror *default_mirror = get_default_mirror(m->ms); |
| 425 | |
| 426 | return !atomic_read(&default_mirror->error_count); |
| 427 | } |
| 428 | |
| 429 | static int mirror_available(struct mirror_set *ms, struct bio *bio) |
| 430 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 431 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
| 432 | region_t region = dm_rh_bio_to_region(ms->rh, bio); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 433 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 434 | if (log->type->in_sync(log, region, 0)) |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 435 | return choose_mirror(ms, bio->bi_sector) ? 1 : 0; |
| 436 | |
| 437 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
| 441 | * remap a buffer to a particular mirror. |
| 442 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 443 | static sector_t map_sector(struct mirror *m, struct bio *bio) |
| 444 | { |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 445 | if (unlikely(!bio->bi_size)) |
| 446 | return 0; |
Alasdair G Kergon | b441a262 | 2010-08-12 04:14:11 +0100 | [diff] [blame] | 447 | return m->offset + dm_target_offset(m->ms->ti, bio->bi_sector); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void map_bio(struct mirror *m, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
| 452 | bio->bi_bdev = m->dev->bdev; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 453 | bio->bi_sector = map_sector(m, bio); |
| 454 | } |
| 455 | |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 456 | static void map_region(struct dm_io_region *io, struct mirror *m, |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 457 | struct bio *bio) |
| 458 | { |
| 459 | io->bdev = m->dev->bdev; |
| 460 | io->sector = map_sector(m, bio); |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 461 | io->count = bio_sectors(bio); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 464 | static void hold_bio(struct mirror_set *ms, struct bio *bio) |
| 465 | { |
| 466 | /* |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 467 | * Lock is required to avoid race condition during suspend |
| 468 | * process. |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 469 | */ |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 470 | spin_lock_irq(&ms->lock); |
| 471 | |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 472 | if (atomic_read(&ms->suspend)) { |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 473 | spin_unlock_irq(&ms->lock); |
| 474 | |
| 475 | /* |
| 476 | * If device is suspended, complete the bio. |
| 477 | */ |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 478 | if (dm_noflush_suspending(ms->ti)) |
| 479 | bio_endio(bio, DM_ENDIO_REQUEUE); |
| 480 | else |
| 481 | bio_endio(bio, -EIO); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Hold bio until the suspend is complete. |
| 487 | */ |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 488 | bio_list_add(&ms->holds, bio); |
| 489 | spin_unlock_irq(&ms->lock); |
| 490 | } |
| 491 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 492 | /*----------------------------------------------------------------- |
| 493 | * Reads |
| 494 | *---------------------------------------------------------------*/ |
| 495 | static void read_callback(unsigned long error, void *context) |
| 496 | { |
| 497 | struct bio *bio = context; |
| 498 | struct mirror *m; |
| 499 | |
| 500 | m = bio_get_m(bio); |
| 501 | bio_set_m(bio, NULL); |
| 502 | |
| 503 | if (likely(!error)) { |
| 504 | bio_endio(bio, 0); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | fail_mirror(m, DM_RAID1_READ_ERROR); |
| 509 | |
| 510 | if (likely(default_ok(m)) || mirror_available(m->ms, bio)) { |
| 511 | DMWARN_LIMIT("Read failure on mirror device %s. " |
| 512 | "Trying alternative device.", |
| 513 | m->dev->name); |
| 514 | queue_bio(m->ms, bio, bio_rw(bio)); |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | DMERR_LIMIT("Read failure on mirror device %s. Failing I/O.", |
| 519 | m->dev->name); |
| 520 | bio_endio(bio, -EIO); |
| 521 | } |
| 522 | |
| 523 | /* Asynchronous read. */ |
| 524 | static void read_async_bio(struct mirror *m, struct bio *bio) |
| 525 | { |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 526 | struct dm_io_region io; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 527 | struct dm_io_request io_req = { |
| 528 | .bi_rw = READ, |
| 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, |
| 534 | }; |
| 535 | |
| 536 | map_region(&io, m, bio); |
| 537 | bio_set_m(bio, m); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 538 | BUG_ON(dm_io(&io_req, 1, &io, NULL)); |
| 539 | } |
| 540 | |
| 541 | static inline int region_in_sync(struct mirror_set *ms, region_t region, |
| 542 | int may_block) |
| 543 | { |
| 544 | int state = dm_rh_get_state(ms->rh, region, may_block); |
| 545 | return state == DM_RH_CLEAN || state == DM_RH_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static void do_reads(struct mirror_set *ms, struct bio_list *reads) |
| 549 | { |
| 550 | region_t region; |
| 551 | struct bio *bio; |
| 552 | struct mirror *m; |
| 553 | |
| 554 | while ((bio = bio_list_pop(reads))) { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 555 | region = dm_rh_bio_to_region(ms->rh, bio); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 556 | m = get_default_mirror(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * We can only read balance if the region is in sync. |
| 560 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 561 | if (likely(region_in_sync(ms, region, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | m = choose_mirror(ms, bio->bi_sector); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 563 | else if (m && atomic_read(&m->error_count)) |
| 564 | m = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 566 | if (likely(m)) |
| 567 | read_async_bio(m, bio); |
| 568 | else |
| 569 | bio_endio(bio, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | /*----------------------------------------------------------------- |
| 574 | * Writes. |
| 575 | * |
| 576 | * We do different things with the write io depending on the |
| 577 | * state of the region that it's in: |
| 578 | * |
| 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 | *---------------------------------------------------------------*/ |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 583 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | static void write_callback(unsigned long error, void *context) |
| 586 | { |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 587 | unsigned i, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | struct bio *bio = (struct bio *) context; |
| 589 | struct mirror_set *ms; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 590 | int should_wake = 0; |
| 591 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 593 | ms = bio_get_m(bio)->ms; |
| 594 | bio_set_m(bio, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /* |
| 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. |
| 601 | */ |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 602 | if (likely(!error)) { |
| 603 | bio_endio(bio, ret); |
| 604 | return; |
| 605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 607 | for (i = 0; i < ms->nr_mirrors; i++) |
| 608 | if (test_bit(i, &error)) |
| 609 | fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 610 | |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 611 | /* |
| 612 | * Need to raise event. Since raising |
| 613 | * events can block, we need to do it in |
| 614 | * the main thread. |
| 615 | */ |
| 616 | spin_lock_irqsave(&ms->lock, flags); |
| 617 | if (!ms->failures.head) |
| 618 | should_wake = 1; |
| 619 | bio_list_add(&ms->failures, bio); |
| 620 | spin_unlock_irqrestore(&ms->lock, flags); |
| 621 | if (should_wake) |
| 622 | wakeup_mirrord(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static void do_write(struct mirror_set *ms, struct bio *bio) |
| 626 | { |
| 627 | unsigned int i; |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 628 | struct dm_io_region io[ms->nr_mirrors], *dest = io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | struct mirror *m; |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 630 | struct dm_io_request io_req = { |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 631 | .bi_rw = WRITE | (bio->bi_rw & WRITE_FLUSH_FUA), |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 632 | .mem.type = DM_IO_BVEC, |
| 633 | .mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx, |
| 634 | .notify.fn = write_callback, |
| 635 | .notify.context = bio, |
| 636 | .client = ms->io_client, |
| 637 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Mike Snitzer | 5fc2ffe | 2011-01-13 19:59:48 +0000 | [diff] [blame] | 639 | if (bio->bi_rw & REQ_DISCARD) { |
| 640 | io_req.bi_rw |= REQ_DISCARD; |
| 641 | io_req.mem.type = DM_IO_KMEM; |
| 642 | io_req.mem.ptr.addr = NULL; |
| 643 | } |
| 644 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 645 | for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) |
| 646 | map_region(dest++, m, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 648 | /* |
| 649 | * Use default mirror because we only need it to retrieve the reference |
| 650 | * to the mirror set in write_callback(). |
| 651 | */ |
| 652 | bio_set_m(bio, get_default_mirror(ms)); |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 653 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 654 | BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static void do_writes(struct mirror_set *ms, struct bio_list *writes) |
| 658 | { |
| 659 | int state; |
| 660 | struct bio *bio; |
| 661 | struct bio_list sync, nosync, recover, *this_list = NULL; |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 662 | struct bio_list requeue; |
| 663 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
| 664 | region_t region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | if (!writes->head) |
| 667 | return; |
| 668 | |
| 669 | /* |
| 670 | * Classify each write. |
| 671 | */ |
| 672 | bio_list_init(&sync); |
| 673 | bio_list_init(&nosync); |
| 674 | bio_list_init(&recover); |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 675 | bio_list_init(&requeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
| 677 | while ((bio = bio_list_pop(writes))) { |
Mike Snitzer | 5fc2ffe | 2011-01-13 19:59:48 +0000 | [diff] [blame] | 678 | if ((bio->bi_rw & REQ_FLUSH) || |
| 679 | (bio->bi_rw & REQ_DISCARD)) { |
Mikulas Patocka | 4184153 | 2009-12-10 23:51:59 +0000 | [diff] [blame] | 680 | bio_list_add(&sync, bio); |
| 681 | continue; |
| 682 | } |
| 683 | |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 684 | region = dm_rh_bio_to_region(ms->rh, bio); |
| 685 | |
| 686 | if (log->type->is_remote_recovering && |
| 687 | log->type->is_remote_recovering(log, region)) { |
| 688 | bio_list_add(&requeue, bio); |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | state = dm_rh_get_state(ms->rh, region, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | switch (state) { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 694 | case DM_RH_CLEAN: |
| 695 | case DM_RH_DIRTY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | this_list = &sync; |
| 697 | break; |
| 698 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 699 | case DM_RH_NOSYNC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | this_list = &nosync; |
| 701 | break; |
| 702 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 703 | case DM_RH_RECOVERING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | this_list = &recover; |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | bio_list_add(this_list, bio); |
| 709 | } |
| 710 | |
| 711 | /* |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 712 | * Add bios that are delayed due to remote recovery |
| 713 | * back on to the write queue |
| 714 | */ |
| 715 | if (unlikely(requeue.head)) { |
| 716 | spin_lock_irq(&ms->lock); |
| 717 | bio_list_merge(&ms->writes, &requeue); |
| 718 | spin_unlock_irq(&ms->lock); |
Mikulas Patocka | 6988568 | 2009-07-23 20:30:37 +0100 | [diff] [blame] | 719 | delayed_wake(ms); |
Jonathan Brassow | 7513c2a | 2009-04-02 19:55:30 +0100 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | * Increment the pending counts for any regions that will |
| 724 | * be written to (writes to recover regions are going to |
| 725 | * be delayed). |
| 726 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 727 | dm_rh_inc_pending(ms->rh, &sync); |
| 728 | dm_rh_inc_pending(ms->rh, &nosync); |
Jonathan Brassow | d2b6986 | 2009-09-04 20:40:32 +0100 | [diff] [blame] | 729 | |
| 730 | /* |
| 731 | * If the flush fails on a previous call and succeeds here, |
| 732 | * we must not reset the log_failure variable. We need |
| 733 | * userspace interaction to do that. |
| 734 | */ |
| 735 | ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Dispatch io. |
| 739 | */ |
Mikulas Patocka | 5528d17 | 2010-02-16 18:42:55 +0000 | [diff] [blame] | 740 | if (unlikely(ms->log_failure) && errors_handled(ms)) { |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 741 | spin_lock_irq(&ms->lock); |
| 742 | bio_list_merge(&ms->failures, &sync); |
| 743 | spin_unlock_irq(&ms->lock); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 744 | wakeup_mirrord(ms); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 745 | } else |
Jonathan Brassow | fc1ff95 | 2007-07-12 17:29:15 +0100 | [diff] [blame] | 746 | while ((bio = bio_list_pop(&sync))) |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 747 | do_write(ms, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | while ((bio = bio_list_pop(&recover))) |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 750 | dm_rh_delay(ms->rh, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | while ((bio = bio_list_pop(&nosync))) { |
Mikulas Patocka | ede5ea0 | 2010-03-06 02:32:22 +0000 | [diff] [blame] | 753 | if (unlikely(ms->leg_failure) && errors_handled(ms)) { |
| 754 | spin_lock_irq(&ms->lock); |
| 755 | bio_list_add(&ms->failures, bio); |
| 756 | spin_unlock_irq(&ms->lock); |
| 757 | wakeup_mirrord(ms); |
| 758 | } else { |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 759 | map_bio(get_default_mirror(ms), bio); |
| 760 | generic_make_request(bio); |
| 761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 765 | static void do_failures(struct mirror_set *ms, struct bio_list *failures) |
| 766 | { |
| 767 | struct bio *bio; |
| 768 | |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 769 | if (likely(!failures->head)) |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 770 | return; |
| 771 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 772 | /* |
| 773 | * If the log has failed, unattempted writes are being |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 774 | * put on the holds list. We can't issue those writes |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 775 | * until a log has been marked, so we must store them. |
| 776 | * |
| 777 | * If a 'noflush' suspend is in progress, we can requeue |
| 778 | * the I/O's to the core. This give userspace a chance |
| 779 | * to reconfigure the mirror, at which point the core |
| 780 | * will reissue the writes. If the 'noflush' flag is |
| 781 | * not set, we have no choice but to return errors. |
| 782 | * |
| 783 | * Some writes on the failures list may have been |
| 784 | * submitted before the log failure and represent a |
| 785 | * failure to write to one of the devices. It is ok |
| 786 | * for us to treat them the same and requeue them |
| 787 | * as well. |
| 788 | */ |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 789 | while ((bio = bio_list_pop(failures))) { |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 790 | if (!ms->log_failure) { |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 791 | ms->in_sync = 0; |
Mikulas Patocka | c58098b | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 792 | dm_rh_mark_nosync(ms->rh, bio); |
Mikulas Patocka | 0f398a8 | 2009-12-10 23:52:04 +0000 | [diff] [blame] | 793 | } |
Mikulas Patocka | 60f355e | 2009-12-10 23:52:05 +0000 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | * If all the legs are dead, fail the I/O. |
| 797 | * If we have been told to handle errors, hold the bio |
| 798 | * and wait for userspace to deal with the problem. |
| 799 | * Otherwise pretend that the I/O succeeded. (This would |
| 800 | * be wrong if the failed leg returned after reboot and |
| 801 | * got replicated back to the good legs.) |
| 802 | */ |
| 803 | if (!get_valid_mirror(ms)) |
| 804 | bio_endio(bio, -EIO); |
| 805 | else if (errors_handled(ms)) |
| 806 | hold_bio(ms, bio); |
| 807 | else |
| 808 | bio_endio(bio, 0); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 809 | } |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static void trigger_event(struct work_struct *work) |
| 813 | { |
| 814 | struct mirror_set *ms = |
| 815 | container_of(work, struct mirror_set, trigger_event); |
| 816 | |
| 817 | dm_table_event(ms->ti->table); |
| 818 | } |
| 819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | /*----------------------------------------------------------------- |
| 821 | * kmirrord |
| 822 | *---------------------------------------------------------------*/ |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 823 | static void do_mirror(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 825 | struct mirror_set *ms = container_of(work, struct mirror_set, |
| 826 | kmirrord_work); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 827 | struct bio_list reads, writes, failures; |
| 828 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 830 | spin_lock_irqsave(&ms->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | reads = ms->reads; |
| 832 | writes = ms->writes; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 833 | failures = ms->failures; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | bio_list_init(&ms->reads); |
| 835 | bio_list_init(&ms->writes); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 836 | bio_list_init(&ms->failures); |
| 837 | spin_unlock_irqrestore(&ms->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 839 | dm_rh_update_states(ms->rh, errors_handled(ms)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | do_recovery(ms); |
| 841 | do_reads(ms, &reads); |
| 842 | do_writes(ms, &writes); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 843 | do_failures(ms, &failures); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | /*----------------------------------------------------------------- |
| 847 | * Target functions |
| 848 | *---------------------------------------------------------------*/ |
| 849 | static struct mirror_set *alloc_context(unsigned int nr_mirrors, |
| 850 | uint32_t region_size, |
| 851 | struct dm_target *ti, |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 852 | struct dm_dirty_log *dl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
| 854 | size_t len; |
| 855 | struct mirror_set *ms = NULL; |
| 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | len = sizeof(*ms) + (sizeof(ms->mirror[0]) * nr_mirrors); |
| 858 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 859 | ms = kzalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | if (!ms) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 861 | ti->error = "Cannot allocate mirror context"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | return NULL; |
| 863 | } |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | spin_lock_init(&ms->lock); |
Mikulas Patocka | 5339fc2 | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 866 | bio_list_init(&ms->reads); |
| 867 | bio_list_init(&ms->writes); |
| 868 | bio_list_init(&ms->failures); |
| 869 | bio_list_init(&ms->holds); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | ms->ti = ti; |
| 872 | ms->nr_mirrors = nr_mirrors; |
| 873 | ms->nr_regions = dm_sector_div_up(ti->len, region_size); |
| 874 | ms->in_sync = 0; |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 875 | ms->log_failure = 0; |
Mikulas Patocka | 929be8f | 2009-12-10 23:52:06 +0000 | [diff] [blame] | 876 | ms->leg_failure = 0; |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 877 | atomic_set(&ms->suspend, 0); |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 878 | atomic_set(&ms->default_mirror, DEFAULT_MIRROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Mikulas Patocka | bda8efe | 2011-05-29 13:03:09 +0100 | [diff] [blame] | 880 | ms->io_client = dm_io_client_create(); |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 881 | if (IS_ERR(ms->io_client)) { |
| 882 | ti->error = "Error creating dm_io client"; |
| 883 | kfree(ms); |
| 884 | return NULL; |
| 885 | } |
| 886 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 887 | ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord, |
| 888 | wakeup_all_recovery_waiters, |
| 889 | ms->ti->begin, MAX_RECOVERY, |
| 890 | dl, region_size, ms->nr_regions); |
| 891 | if (IS_ERR(ms->rh)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 892 | ti->error = "Error creating dirty region hash"; |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 893 | dm_io_client_destroy(ms->io_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | kfree(ms); |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | return ms; |
| 899 | } |
| 900 | |
| 901 | static void free_context(struct mirror_set *ms, struct dm_target *ti, |
| 902 | unsigned int m) |
| 903 | { |
| 904 | while (m--) |
| 905 | dm_put_device(ti, ms->mirror[m].dev); |
| 906 | |
Milan Broz | 88be163 | 2007-05-09 02:33:04 -0700 | [diff] [blame] | 907 | dm_io_client_destroy(ms->io_client); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 908 | dm_region_hash_destroy(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | kfree(ms); |
| 910 | } |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | static int get_mirror(struct mirror_set *ms, struct dm_target *ti, |
| 913 | unsigned int mirror, char **argv) |
| 914 | { |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 915 | unsigned long long offset; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 916 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 918 | if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 919 | ti->error = "Invalid offset"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | return -EINVAL; |
| 921 | } |
| 922 | |
Nikanth Karthikesan | 8215d6e | 2010-03-06 02:32:27 +0000 | [diff] [blame] | 923 | if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | &ms->mirror[mirror].dev)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 925 | ti->error = "Device lookup failure"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return -ENXIO; |
| 927 | } |
| 928 | |
Jonathan Brassow | aa5617c | 2007-10-19 22:47:58 +0100 | [diff] [blame] | 929 | ms->mirror[mirror].ms = ms; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 930 | atomic_set(&(ms->mirror[mirror].error_count), 0); |
| 931 | ms->mirror[mirror].error_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | ms->mirror[mirror].offset = offset; |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | /* |
| 938 | * Create dirty log: log_type #log_params <log_params> |
| 939 | */ |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 940 | static struct dm_dirty_log *create_dirty_log(struct dm_target *ti, |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 941 | unsigned argc, char **argv, |
| 942 | unsigned *args_used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 944 | unsigned param_count; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 945 | struct dm_dirty_log *dl; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 946 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
| 948 | if (argc < 2) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 949 | ti->error = "Insufficient mirror log arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return NULL; |
| 951 | } |
| 952 | |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 953 | if (sscanf(argv[1], "%u%c", ¶m_count, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 954 | ti->error = "Invalid mirror log argument count"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | *args_used = 2 + param_count; |
| 959 | |
| 960 | if (argc < *args_used) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 961 | ti->error = "Insufficient mirror log arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return NULL; |
| 963 | } |
| 964 | |
Mikulas Patocka | c0da374 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 965 | dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count, |
| 966 | argv + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | if (!dl) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 968 | ti->error = "Error creating mirror dirty log"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return NULL; |
| 970 | } |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | return dl; |
| 973 | } |
| 974 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 975 | static int parse_features(struct mirror_set *ms, unsigned argc, char **argv, |
| 976 | unsigned *args_used) |
| 977 | { |
| 978 | unsigned num_features; |
| 979 | struct dm_target *ti = ms->ti; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 980 | char dummy; |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 981 | |
| 982 | *args_used = 0; |
| 983 | |
| 984 | if (!argc) |
| 985 | return 0; |
| 986 | |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 987 | if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) { |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 988 | ti->error = "Invalid number of features"; |
| 989 | return -EINVAL; |
| 990 | } |
| 991 | |
| 992 | argc--; |
| 993 | argv++; |
| 994 | (*args_used)++; |
| 995 | |
| 996 | if (num_features > argc) { |
| 997 | ti->error = "Not enough arguments to support feature count"; |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | |
| 1001 | if (!strcmp("handle_errors", argv[0])) |
| 1002 | ms->features |= DM_RAID1_HANDLE_ERRORS; |
| 1003 | else { |
| 1004 | ti->error = "Unrecognised feature requested"; |
| 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
| 1008 | (*args_used)++; |
| 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * Construct a mirror mapping: |
| 1015 | * |
| 1016 | * log_type #log_params <log_params> |
| 1017 | * #mirrors [mirror_path offset]{2,} |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1018 | * [#features <features>] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | * |
| 1020 | * log_type is "core" or "disk" |
| 1021 | * #log_params is between 1 and 3 |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1022 | * |
| 1023 | * If present, features must be "handle_errors". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1026 | { |
| 1027 | int r; |
| 1028 | unsigned int nr_mirrors, m, args_used; |
| 1029 | struct mirror_set *ms; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1030 | struct dm_dirty_log *dl; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1031 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
| 1033 | dl = create_dirty_log(ti, argc, argv, &args_used); |
| 1034 | if (!dl) |
| 1035 | return -EINVAL; |
| 1036 | |
| 1037 | argv += args_used; |
| 1038 | argc -= args_used; |
| 1039 | |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1040 | if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 || |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1041 | nr_mirrors < 2 || nr_mirrors > DM_KCOPYD_MAX_REGIONS + 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1042 | ti->error = "Invalid number of mirrors"; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1043 | dm_dirty_log_destroy(dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | |
| 1047 | argv++, argc--; |
| 1048 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1049 | if (argc < nr_mirrors * 2) { |
| 1050 | ti->error = "Too few mirror arguments"; |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1051 | dm_dirty_log_destroy(dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
| 1055 | ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl); |
| 1056 | if (!ms) { |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 1057 | dm_dirty_log_destroy(dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | return -ENOMEM; |
| 1059 | } |
| 1060 | |
| 1061 | /* Get the mirror parameter sets */ |
| 1062 | for (m = 0; m < nr_mirrors; m++) { |
| 1063 | r = get_mirror(ms, ti, m, argv); |
| 1064 | if (r) { |
| 1065 | free_context(ms, ti, m); |
| 1066 | return r; |
| 1067 | } |
| 1068 | argv += 2; |
| 1069 | argc -= 2; |
| 1070 | } |
| 1071 | |
| 1072 | ti->private = ms; |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1073 | |
| 1074 | r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh)); |
| 1075 | if (r) |
| 1076 | goto err_free_context; |
| 1077 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1078 | ti->num_flush_bios = 1; |
| 1079 | ti->num_discard_bios = 1; |
Mikulas Patocka | 89c7cd8 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 1080 | ti->per_bio_data_size = sizeof(struct dm_raid1_bio_record); |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 1081 | ti->discard_zeroes_data_unsupported = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Tejun Heo | 670368a | 2013-07-30 08:40:21 -0400 | [diff] [blame^] | 1083 | ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1084 | if (!ms->kmirrord_wq) { |
| 1085 | DMERR("couldn't start kmirrord"); |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1086 | r = -ENOMEM; |
| 1087 | goto err_free_context; |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1088 | } |
| 1089 | INIT_WORK(&ms->kmirrord_work, do_mirror); |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 1090 | init_timer(&ms->timer); |
| 1091 | ms->timer_pending = 0; |
Jonathan Brassow | 72f4b31 | 2008-02-08 02:11:29 +0000 | [diff] [blame] | 1092 | INIT_WORK(&ms->trigger_event, trigger_event); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1093 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1094 | r = parse_features(ms, argc, argv, &args_used); |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1095 | if (r) |
| 1096 | goto err_destroy_wq; |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1097 | |
| 1098 | argv += args_used; |
| 1099 | argc -= args_used; |
| 1100 | |
Jonathan Brassow | f44db67 | 2007-07-12 17:29:04 +0100 | [diff] [blame] | 1101 | /* |
| 1102 | * Any read-balancing addition depends on the |
| 1103 | * DM_RAID1_HANDLE_ERRORS flag being present. |
| 1104 | * This is because the decision to balance depends |
| 1105 | * on the sync state of a region. If the above |
| 1106 | * flag is not present, we ignore errors; and |
| 1107 | * the sync state may be inaccurate. |
| 1108 | */ |
| 1109 | |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1110 | if (argc) { |
| 1111 | ti->error = "Too many mirror arguments"; |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1112 | r = -EINVAL; |
| 1113 | goto err_destroy_wq; |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Mikulas Patocka | df5d2e9 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 1116 | ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle); |
Mikulas Patocka | fa34ce7 | 2011-05-29 13:03:13 +0100 | [diff] [blame] | 1117 | if (IS_ERR(ms->kcopyd_client)) { |
| 1118 | r = PTR_ERR(ms->kcopyd_client); |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1119 | goto err_destroy_wq; |
Mikulas Patocka | fa34ce7 | 2011-05-29 13:03:13 +0100 | [diff] [blame] | 1120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1122 | wakeup_mirrord(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | return 0; |
Dmitry Monakhov | a72cf73 | 2007-10-19 22:38:39 +0100 | [diff] [blame] | 1124 | |
| 1125 | err_destroy_wq: |
| 1126 | destroy_workqueue(ms->kmirrord_wq); |
| 1127 | err_free_context: |
| 1128 | free_context(ms, ti, ms->nr_mirrors); |
| 1129 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | static void mirror_dtr(struct dm_target *ti) |
| 1133 | { |
| 1134 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
| 1135 | |
Mikulas Patocka | a2aebe0 | 2008-04-24 22:10:42 +0100 | [diff] [blame] | 1136 | del_timer_sync(&ms->timer); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1137 | flush_workqueue(ms->kmirrord_wq); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 1138 | flush_work(&ms->trigger_event); |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1139 | dm_kcopyd_client_destroy(ms->kcopyd_client); |
Holger Smolinski | 6ad36fe | 2007-05-09 02:32:50 -0700 | [diff] [blame] | 1140 | destroy_workqueue(ms->kmirrord_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | free_context(ms, ti, ms->nr_mirrors); |
| 1142 | } |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | /* |
| 1145 | * Mirror mapping function |
| 1146 | */ |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1147 | static int mirror_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | { |
| 1149 | int r, rw = bio_rw(bio); |
| 1150 | struct mirror *m; |
| 1151 | struct mirror_set *ms = ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1152 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1153 | struct dm_raid1_bio_record *bio_record = |
| 1154 | dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record)); |
| 1155 | |
| 1156 | bio_record->details.bi_bdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | if (rw == WRITE) { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1159 | /* Save region for mirror_end_io() handler */ |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1160 | bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | queue_bio(ms, bio, rw); |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1162 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1165 | r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (r < 0 && r != -EWOULDBLOCK) |
| 1167 | return r; |
| 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | /* |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1170 | * If region is not in-sync queue the bio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1172 | if (!r || (r == -EWOULDBLOCK)) { |
| 1173 | if (rw == READA) |
| 1174 | return -EWOULDBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | queue_bio(ms, bio, rw); |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1177 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1180 | /* |
| 1181 | * The region is in-sync and we can perform reads directly. |
| 1182 | * Store enough information so we can retry if it fails. |
| 1183 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | m = choose_mirror(ms, bio->bi_sector); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1185 | if (unlikely(!m)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return -EIO; |
| 1187 | |
Mikulas Patocka | 89c7cd8 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 1188 | dm_bio_record(&bio_record->details, bio); |
Mikulas Patocka | 89c7cd8 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 1189 | bio_record->m = m; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1190 | |
| 1191 | map_bio(m, bio); |
| 1192 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1193 | return DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1196 | static int mirror_end_io(struct dm_target *ti, struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | { |
| 1198 | int rw = bio_rw(bio); |
| 1199 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1200 | struct mirror *m = NULL; |
| 1201 | struct dm_bio_details *bd = NULL; |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1202 | struct dm_raid1_bio_record *bio_record = |
| 1203 | dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
| 1205 | /* |
| 1206 | * We need to dec pending if this was a write. |
| 1207 | */ |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1208 | if (rw == WRITE) { |
Mikulas Patocka | 751f188 | 2012-07-20 14:25:03 +0100 | [diff] [blame] | 1209 | if (!(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1210 | dm_rh_dec(ms->rh, bio_record->write_region); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1211 | return error; |
| 1212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1214 | if (error == -EOPNOTSUPP) |
| 1215 | goto out; |
| 1216 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1217 | if ((error == -EWOULDBLOCK) && (bio->bi_rw & REQ_RAHEAD)) |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1218 | goto out; |
| 1219 | |
| 1220 | if (unlikely(error)) { |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1221 | if (!bio_record->details.bi_bdev) { |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1222 | /* |
| 1223 | * There wasn't enough memory to record necessary |
| 1224 | * information for a retry or there was no other |
| 1225 | * mirror in-sync. |
| 1226 | */ |
Adrian Bunk | e03f1a8 | 2008-02-19 19:44:19 +0000 | [diff] [blame] | 1227 | DMERR_LIMIT("Mirror read failed."); |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1228 | return -EIO; |
| 1229 | } |
Adrian Bunk | e03f1a8 | 2008-02-19 19:44:19 +0000 | [diff] [blame] | 1230 | |
Mikulas Patocka | 89c7cd8 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 1231 | m = bio_record->m; |
Adrian Bunk | e03f1a8 | 2008-02-19 19:44:19 +0000 | [diff] [blame] | 1232 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1233 | DMERR("Mirror read failed from %s. Trying alternative device.", |
| 1234 | m->dev->name); |
| 1235 | |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1236 | fail_mirror(m, DM_RAID1_READ_ERROR); |
| 1237 | |
| 1238 | /* |
| 1239 | * A failed read is requeued for another attempt using an intact |
| 1240 | * mirror. |
| 1241 | */ |
| 1242 | if (default_ok(m) || mirror_available(ms, bio)) { |
Mikulas Patocka | 89c7cd8 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 1243 | bd = &bio_record->details; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1244 | |
| 1245 | dm_bio_restore(bd, bio); |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1246 | bio_record->details.bi_bdev = NULL; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1247 | queue_bio(ms, bio, rw); |
Mikulas Patocka | 19cbbc6 | 2012-12-21 20:23:32 +0000 | [diff] [blame] | 1248 | return DM_ENDIO_INCOMPLETE; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1249 | } |
| 1250 | DMERR("All replicated volumes dead, failing I/O"); |
| 1251 | } |
| 1252 | |
| 1253 | out: |
Mikulas Patocka | 0045d61 | 2012-12-21 20:23:40 +0000 | [diff] [blame] | 1254 | bio_record->details.bi_bdev = NULL; |
Jonathan Brassow | 06386bb | 2008-02-08 02:11:37 +0000 | [diff] [blame] | 1255 | |
| 1256 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1259 | static void mirror_presuspend(struct dm_target *ti) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | { |
| 1261 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1262 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Mikulas Patocka | 0478850 | 2009-12-10 23:52:03 +0000 | [diff] [blame] | 1264 | struct bio_list holds; |
| 1265 | struct bio *bio; |
| 1266 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1267 | atomic_set(&ms->suspend, 1); |
| 1268 | |
| 1269 | /* |
Takahiro Yasui | f070304 | 2010-03-06 02:32:35 +0000 | [diff] [blame] | 1270 | * Process bios in the hold list to start recovery waiting |
| 1271 | * for bios in the hold list. After the process, no bio has |
| 1272 | * a chance to be added in the hold list because ms->suspend |
| 1273 | * is set. |
| 1274 | */ |
| 1275 | spin_lock_irq(&ms->lock); |
| 1276 | holds = ms->holds; |
| 1277 | bio_list_init(&ms->holds); |
| 1278 | spin_unlock_irq(&ms->lock); |
| 1279 | |
| 1280 | while ((bio = bio_list_pop(&holds))) |
| 1281 | hold_bio(ms, bio); |
| 1282 | |
| 1283 | /* |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1284 | * We must finish up all the work that we've |
| 1285 | * generated (i.e. recovery work). |
| 1286 | */ |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1287 | dm_rh_stop_recovery(ms->rh); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 1288 | |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 1289 | wait_event(_kmirrord_recovery_stopped, |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1290 | !dm_rh_recovery_in_flight(ms->rh)); |
Jonathan E Brassow | 3318404 | 2006-11-08 17:44:44 -0800 | [diff] [blame] | 1291 | |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1292 | if (log->type->presuspend && log->type->presuspend(log)) |
| 1293 | /* FIXME: need better error handling */ |
| 1294 | DMWARN("log presuspend failed"); |
| 1295 | |
| 1296 | /* |
| 1297 | * Now that recovery is complete/stopped and the |
| 1298 | * delayed bios are queued, we need to wait for |
| 1299 | * the worker thread to complete. This way, |
| 1300 | * we know that all of our I/O has been pushed. |
| 1301 | */ |
| 1302 | flush_workqueue(ms->kmirrord_wq); |
| 1303 | } |
| 1304 | |
| 1305 | static void mirror_postsuspend(struct dm_target *ti) |
| 1306 | { |
| 1307 | struct mirror_set *ms = ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1308 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1309 | |
Jonathan Brassow | 6b3df0d | 2007-10-19 22:47:57 +0100 | [diff] [blame] | 1310 | if (log->type->postsuspend && log->type->postsuspend(log)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | /* FIXME: need better error handling */ |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1312 | DMWARN("log postsuspend failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | static void mirror_resume(struct dm_target *ti) |
| 1316 | { |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1317 | struct mirror_set *ms = ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1318 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1319 | |
| 1320 | atomic_set(&ms->suspend, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | if (log->type->resume && log->type->resume(log)) |
| 1322 | /* FIXME: need better error handling */ |
| 1323 | DMWARN("log resume failed"); |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1324 | dm_rh_start_recovery(ms->rh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1327 | /* |
| 1328 | * device_status_char |
| 1329 | * @m: mirror device/leg we want the status of |
| 1330 | * |
| 1331 | * We return one character representing the most severe error |
| 1332 | * we have encountered. |
| 1333 | * A => Alive - No failures |
| 1334 | * D => Dead - A write failure occurred leaving mirror out-of-sync |
| 1335 | * S => Sync - A sychronization failure occurred, mirror out-of-sync |
| 1336 | * R => Read - A read failure occurred, mirror data unaffected |
| 1337 | * |
| 1338 | * Returns: <char> |
| 1339 | */ |
| 1340 | static char device_status_char(struct mirror *m) |
| 1341 | { |
| 1342 | if (!atomic_read(&(m->error_count))) |
| 1343 | return 'A'; |
| 1344 | |
Mikulas Patocka | 64b30c4 | 2009-12-10 23:52:02 +0000 | [diff] [blame] | 1345 | return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' : |
| 1346 | (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' : |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1347 | (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' : |
| 1348 | (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U'; |
| 1349 | } |
| 1350 | |
| 1351 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1352 | static void mirror_status(struct dm_target *ti, status_type_t type, |
| 1353 | unsigned status_flags, char *result, unsigned maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | { |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1355 | unsigned int m, sz = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | struct mirror_set *ms = (struct mirror_set *) ti->private; |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1357 | struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1358 | char buffer[ms->nr_mirrors + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | switch (type) { |
| 1361 | case STATUSTYPE_INFO: |
| 1362 | DMEMIT("%d ", ms->nr_mirrors); |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1363 | for (m = 0; m < ms->nr_mirrors; m++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | DMEMIT("%s ", ms->mirror[m].dev->name); |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1365 | buffer[m] = device_status_char(&(ms->mirror[m])); |
| 1366 | } |
| 1367 | buffer[m] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1369 | DMEMIT("%llu/%llu 1 %s ", |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1370 | (unsigned long long)log->type->get_sync_count(log), |
Jonathan Brassow | af195ac | 2008-02-08 02:11:39 +0000 | [diff] [blame] | 1371 | (unsigned long long)ms->nr_regions, buffer); |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1372 | |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1373 | sz += log->type->status(log, type, result+sz, maxlen-sz); |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | break; |
| 1376 | |
| 1377 | case STATUSTYPE_TABLE: |
Heinz Mauelshagen | 1f965b1 | 2008-10-21 17:45:06 +0100 | [diff] [blame] | 1378 | sz = log->type->status(log, type, result, maxlen); |
Jonathan E Brassow | 315dcc2 | 2007-05-09 02:32:58 -0700 | [diff] [blame] | 1379 | |
Jonathan Brassow | e52b8f6 | 2006-10-03 01:15:32 -0700 | [diff] [blame] | 1380 | DMEMIT("%d", ms->nr_mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | for (m = 0; m < ms->nr_mirrors; m++) |
Jonathan Brassow | e52b8f6 | 2006-10-03 01:15:32 -0700 | [diff] [blame] | 1382 | DMEMIT(" %s %llu", ms->mirror[m].dev->name, |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1383 | (unsigned long long)ms->mirror[m].offset); |
Jonathan E Brassow | a8e6afa | 2007-05-09 02:32:59 -0700 | [diff] [blame] | 1384 | |
| 1385 | if (ms->features & DM_RAID1_HANDLE_ERRORS) |
| 1386 | DMEMIT(" 1 handle_errors"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1390 | static int mirror_iterate_devices(struct dm_target *ti, |
| 1391 | iterate_devices_callout_fn fn, void *data) |
| 1392 | { |
| 1393 | struct mirror_set *ms = ti->private; |
| 1394 | int ret = 0; |
| 1395 | unsigned i; |
| 1396 | |
| 1397 | for (i = 0; !ret && i < ms->nr_mirrors; i++) |
| 1398 | ret = fn(ti, ms->mirror[i].dev, |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 1399 | ms->mirror[i].offset, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1400 | |
| 1401 | return ret; |
| 1402 | } |
| 1403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | static struct target_type mirror_target = { |
| 1405 | .name = "mirror", |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1406 | .version = {1, 13, 2}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | .module = THIS_MODULE, |
| 1408 | .ctr = mirror_ctr, |
| 1409 | .dtr = mirror_dtr, |
| 1410 | .map = mirror_map, |
| 1411 | .end_io = mirror_end_io, |
Jonathan Brassow | b80aa7a | 2008-02-08 02:11:35 +0000 | [diff] [blame] | 1412 | .presuspend = mirror_presuspend, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | .postsuspend = mirror_postsuspend, |
| 1414 | .resume = mirror_resume, |
| 1415 | .status = mirror_status, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1416 | .iterate_devices = mirror_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | }; |
| 1418 | |
| 1419 | static int __init dm_mirror_init(void) |
| 1420 | { |
| 1421 | int r; |
| 1422 | |
Mikulas Patocka | 95f8fac | 2009-04-02 19:55:24 +0100 | [diff] [blame] | 1423 | r = dm_register_target(&mirror_target); |
| 1424 | if (r < 0) { |
| 1425 | DMERR("Failed to register mirror target"); |
| 1426 | goto bad_target; |
| 1427 | } |
| 1428 | |
| 1429 | return 0; |
| 1430 | |
| 1431 | bad_target: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | return r; |
| 1433 | } |
| 1434 | |
| 1435 | static void __exit dm_mirror_exit(void) |
| 1436 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 1437 | dm_unregister_target(&mirror_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* Module hooks */ |
| 1441 | module_init(dm_mirror_init); |
| 1442 | module_exit(dm_mirror_exit); |
| 1443 | |
| 1444 | MODULE_DESCRIPTION(DM_NAME " mirror target"); |
| 1445 | MODULE_AUTHOR("Joe Thornber"); |
| 1446 | MODULE_LICENSE("GPL"); |