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