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