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(ms->kmirrord_wq, &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(0, &ms->timer_pending); 109 wakeup_mirrord(ms); 110 } 111 112 static void delayed_wake(struct mirror_set *ms) 113 { 114 if (test_and_set_bit(0, &ms->timer_pending)) 115 return; 116 117 ms->timer.expires = jiffies + HZ / 5; 118 add_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(ms); 138 spin_unlock_irqrestore(&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(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(&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(&ms->default_mirror, 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(&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(&m->error_count); 228 229 if (test_and_set_bit(error_type, &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(dm_raid1_wq, &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, ms->nr_mirrors, io, &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(ms->mirror + i, 285 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(get_default_mirror(ms), 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(ms->mirror + m, 326 DM_RAID1_SYNC_ERROR); 327 bit++; 328 } 329 } 330 331 dm_rh_recovery_end(reg, !(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(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(ms->rh, 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(ms->rh, 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(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to, 375 flags, recovery_complete, 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(&(ms->mirror[m].error_count), 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(ms->rh); 393 394 /* 395 * Start quiescing some regions. 396 */ 397 dm_rh_recovery_prepare(ms->rh); 398 399 /* 400 * Copy any already quiesced regions. 401 */ 402 while ((reg = dm_rh_recovery_start(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(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(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 { 446 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); 447 region_t region = dm_rh_bio_to_region(ms->rh, bio); 448 449 if (log->type->in_sync(log, region, 0)) 450 return choose_mirror(ms, 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, 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(&ms->lock); 486 487 if (atomic_read(&ms->suspend)) { 488 spin_unlock_irq(&ms->lock); 489 490 /* 491 * If device is suspended, complete the bio. 492 */ 493 if (dm_noflush_suspending(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(&ms->holds, bio); 506 spin_unlock_irq(&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, DM_RAID1_READ_ERROR); 528 529 if (likely(default_ok(m)) || mirror_available(m->ms, bio)) { 530 DMWARN_LIMIT("Read failure on mirror device %s. Trying alternative device.", 531 m->dev->name); 532 queue_bio(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, 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(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(reads))) { 573 region = dm_rh_bio_to_region(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, bio->bi_iter.bi_sector); 581 else if (m && atomic_read(&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(ms->mirror + i, 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(&ms->failures, bio); 648 if (should_wake) 649 wakeup_mirrord(ms); 650 spin_unlock_irqrestore(&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(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, 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(ms->rh); 693 region_t region; 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); 704 bio_list_init(&requeue); 705 706 while ((bio = bio_list_pop(writes))) { 707 if ((bio->bi_opf & REQ_PREFLUSH) || 708 (bio_op(bio) == REQ_OP_DISCARD)) { 709 bio_list_add(&sync, bio); 710 continue; 711 } 712 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); 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(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(&ms->lock); 746 bio_list_merge(&ms->writes, &requeue); 747 spin_unlock_irq(&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(ms->rh, &sync); 757 dm_rh_inc_pending(ms->rh, &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(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(&ms->lock); 771 bio_list_merge(&ms->failures, &sync); 772 spin_unlock_irq(&ms->lock); 773 wakeup_mirrord(ms); 774 } else 775 while ((bio = bio_list_pop(&sync))) 776 do_write(ms, bio); 777 778 while ((bio = bio_list_pop(&recover))) 779 dm_rh_delay(ms->rh, bio); 780 781 while ((bio = bio_list_pop(&nosync))) { 782 if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) { 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 { 788 map_bio(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(failures))) { 819 if (!ms->log_failure) { 820 ms->in_sync = 0; 821 dm_rh_mark_nosync(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(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(&ms->reads); 871 bio_list_init(&ms->writes); 872 bio_list_init(&ms->failures); 873 spin_unlock_irqrestore(&ms->lock, flags); 874 875 dm_rh_update_states(ms->rh, errors_handled(ms)); 876 do_recovery(ms); 877 do_reads(ms, &reads); 878 do_writes(ms, &writes); 879 do_failures(ms, &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(&ms->reads); 902 bio_list_init(&ms->writes); 903 bio_list_init(&ms->failures); 904 bio_list_init(&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(&ms->suspend, 0); 913 atomic_set(&ms->default_mirror, DEFAULT_MIRROR); 914 915 ms->io_client = dm_io_client_create(); 916 if (IS_ERR(ms->io_client)) { 917 ti->error = "Error creating dm_io client"; 918 kfree(ms); 919 return NULL; 920 } 921 922 ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord, 923 wakeup_all_recovery_waiters, 924 ms->ti->begin, MAX_RECOVERY, 925 dl, region_size, ms->nr_regions); 926 if (IS_ERR(ms->rh)) { 927 ti->error = "Error creating dirty region hash"; 928 dm_io_client_destroy(ms->io_client); 929 kfree(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, ms->mirror[m].dev); 941 942 dm_io_client_destroy(ms->io_client); 943 dm_region_hash_destroy(ms->rh); 944 kfree(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, argv[0], dm_table_get_mode(ti->table), 961 &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(&(ms->mirror[mirror].error_count), 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(argv[0], ti, mirror_flush, param_count, 1004 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); 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(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(dl); 1101 return -EINVAL; 1102 } 1103 1104 ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl); 1105 if (!ms) { 1106 dm_dirty_log_destroy(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, 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, dm_rh_get_region_size(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", 1132 WQ_MEM_RECLAIM | WQ_PERCPU, 0); 1133 if (!ms->kmirrord_wq) { 1134 DMERR("couldn't start kmirrord"); 1135 r = -ENOMEM; 1136 goto err_free_context; 1137 } 1138 INIT_WORK(&ms->kmirrord_work, do_mirror); 1139 timer_setup(&ms->timer, delayed_wake_fn, 0); 1140 ms->timer_pending = 0; 1141 INIT_WORK(&ms->trigger_event, trigger_event); 1142 1143 r = parse_features(ms, argc, argv, &args_used); 1144 if (r) 1145 goto err_destroy_wq; 1146 1147 argv += args_used; 1148 argc -= args_used; 1149 1150 /* 1151 * Any read-balancing addition depends on the 1152 * DM_RAID1_HANDLE_ERRORS flag being present. 1153 * This is because the decision to balance depends 1154 * on the sync state of a region. If the above 1155 * flag is not present, we ignore errors; and 1156 * the sync state may be inaccurate. 1157 */ 1158 1159 if (argc) { 1160 ti->error = "Too many mirror arguments"; 1161 r = -EINVAL; 1162 goto err_destroy_wq; 1163 } 1164 1165 ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle); 1166 if (IS_ERR(ms->kcopyd_client)) { 1167 r = PTR_ERR(ms->kcopyd_client); 1168 goto err_destroy_wq; 1169 } 1170 1171 wakeup_mirrord(ms); 1172 return 0; 1173 1174 err_destroy_wq: 1175 destroy_workqueue(ms->kmirrord_wq); 1176 err_free_context: 1177 free_context(ms, ti, ms->nr_mirrors); 1178 return r; 1179 } 1180 1181 static void mirror_dtr(struct dm_target *ti) 1182 { 1183 struct mirror_set *ms = ti->private; 1184 1185 timer_delete_sync(&ms->timer); 1186 flush_workqueue(ms->kmirrord_wq); 1187 flush_work(&ms->trigger_event); 1188 dm_kcopyd_client_destroy(ms->kcopyd_client); 1189 destroy_workqueue(ms->kmirrord_wq); 1190 free_context(ms, ti, ms->nr_mirrors); 1191 } 1192 1193 /* 1194 * Mirror mapping function 1195 */ 1196 static int mirror_map(struct dm_target *ti, struct bio *bio) 1197 { 1198 int r, rw = bio_data_dir(bio); 1199 struct mirror *m; 1200 struct mirror_set *ms = ti->private; 1201 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); 1202 struct dm_raid1_bio_record *bio_record = 1203 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record)); 1204 1205 bio_record->details.bi_bdev = NULL; 1206 1207 if (rw == WRITE) { 1208 /* Save region for mirror_end_io() handler */ 1209 bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio); 1210 queue_bio(ms, bio, rw); 1211 return DM_MAPIO_SUBMITTED; 1212 } 1213 1214 r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0); 1215 if (r < 0 && r != -EWOULDBLOCK) 1216 return DM_MAPIO_KILL; 1217 1218 /* 1219 * If region is not in-sync queue the bio. 1220 */ 1221 if (!r || (r == -EWOULDBLOCK)) { 1222 if (bio->bi_opf & REQ_RAHEAD) 1223 return DM_MAPIO_KILL; 1224 1225 queue_bio(ms, bio, rw); 1226 return DM_MAPIO_SUBMITTED; 1227 } 1228 1229 /* 1230 * The region is in-sync and we can perform reads directly. 1231 * Store enough information so we can retry if it fails. 1232 */ 1233 m = choose_mirror(ms, bio->bi_iter.bi_sector); 1234 if (unlikely(!m)) 1235 return DM_MAPIO_KILL; 1236 1237 dm_bio_record(&bio_record->details, bio); 1238 bio_record->m = m; 1239 1240 map_bio(m, bio); 1241 1242 return DM_MAPIO_REMAPPED; 1243 } 1244 1245 static int mirror_end_io(struct dm_target *ti, struct bio *bio, 1246 blk_status_t *error) 1247 { 1248 int rw = bio_data_dir(bio); 1249 struct mirror_set *ms = ti->private; 1250 struct mirror *m = NULL; 1251 struct dm_bio_details *bd = NULL; 1252 struct dm_raid1_bio_record *bio_record = 1253 dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record)); 1254 1255 /* 1256 * We need to dec pending if this was a write. 1257 */ 1258 if (rw == WRITE) { 1259 if (!(bio->bi_opf & REQ_PREFLUSH) && 1260 bio_op(bio) != REQ_OP_DISCARD) 1261 dm_rh_dec(ms->rh, bio_record->write_region); 1262 return DM_ENDIO_DONE; 1263 } 1264 1265 if (*error == BLK_STS_NOTSUPP) 1266 goto out; 1267 1268 if (bio->bi_opf & REQ_RAHEAD) 1269 goto out; 1270 1271 if (unlikely(*error)) { 1272 if (!bio_record->details.bi_bdev) { 1273 /* 1274 * There wasn't enough memory to record necessary 1275 * information for a retry or there was no other 1276 * mirror in-sync. 1277 */ 1278 DMERR_LIMIT("Mirror read failed."); 1279 return DM_ENDIO_DONE; 1280 } 1281 1282 m = bio_record->m; 1283 1284 DMERR("Mirror read failed from %s. Trying alternative device.", 1285 m->dev->name); 1286 1287 fail_mirror(m, DM_RAID1_READ_ERROR); 1288 1289 /* 1290 * A failed read is requeued for another attempt using an intact 1291 * mirror. 1292 */ 1293 if (default_ok(m) || mirror_available(ms, bio)) { 1294 bd = &bio_record->details; 1295 1296 dm_bio_restore(bd, bio); 1297 bio_record->details.bi_bdev = NULL; 1298 bio->bi_status = 0; 1299 1300 queue_bio(ms, bio, rw); 1301 return DM_ENDIO_INCOMPLETE; 1302 } 1303 DMERR("All replicated volumes dead, failing I/O"); 1304 } 1305 1306 out: 1307 bio_record->details.bi_bdev = NULL; 1308 1309 return DM_ENDIO_DONE; 1310 } 1311 1312 static void mirror_presuspend(struct dm_target *ti) 1313 { 1314 struct mirror_set *ms = ti->private; 1315 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); 1316 1317 struct bio_list holds; 1318 struct bio *bio; 1319 1320 atomic_set(&ms->suspend, 1); 1321 1322 /* 1323 * Process bios in the hold list to start recovery waiting 1324 * for bios in the hold list. After the process, no bio has 1325 * a chance to be added in the hold list because ms->suspend 1326 * is set. 1327 */ 1328 spin_lock_irq(&ms->lock); 1329 holds = ms->holds; 1330 bio_list_init(&ms->holds); 1331 spin_unlock_irq(&ms->lock); 1332 1333 while ((bio = bio_list_pop(&holds))) 1334 hold_bio(ms, bio); 1335 1336 /* 1337 * We must finish up all the work that we've 1338 * generated (i.e. recovery work). 1339 */ 1340 dm_rh_stop_recovery(ms->rh); 1341 1342 wait_event(_kmirrord_recovery_stopped, 1343 !dm_rh_recovery_in_flight(ms->rh)); 1344 1345 if (log->type->presuspend && log->type->presuspend(log)) 1346 /* FIXME: need better error handling */ 1347 DMWARN("log presuspend failed"); 1348 1349 /* 1350 * Now that recovery is complete/stopped and the 1351 * delayed bios are queued, we need to wait for 1352 * the worker thread to complete. This way, 1353 * we know that all of our I/O has been pushed. 1354 */ 1355 flush_workqueue(ms->kmirrord_wq); 1356 } 1357 1358 static void mirror_postsuspend(struct dm_target *ti) 1359 { 1360 struct mirror_set *ms = ti->private; 1361 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); 1362 1363 if (log->type->postsuspend && log->type->postsuspend(log)) 1364 /* FIXME: need better error handling */ 1365 DMWARN("log postsuspend failed"); 1366 } 1367 1368 static void mirror_resume(struct dm_target *ti) 1369 { 1370 struct mirror_set *ms = ti->private; 1371 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); 1372 1373 atomic_set(&ms->suspend, 0); 1374 if (log->type->resume && log->type->resume(log)) 1375 /* FIXME: need better error handling */ 1376 DMWARN("log resume failed"); 1377 dm_rh_start_recovery(ms->rh); 1378 } 1379 1380 /* 1381 * device_status_char 1382 * @m: mirror device/leg we want the status of 1383 * 1384 * We return one character representing the most severe error 1385 * we have encountered. 1386 * A => Alive - No failures 1387 * D => Dead - A write failure occurred leaving mirror out-of-sync 1388 * S => Sync - A sychronization failure occurred, mirror out-of-sync 1389 * R => Read - A read failure occurred, mirror data unaffected 1390 * 1391 * Returns: <char> 1392 */ 1393 static char device_status_char(struct mirror *m) 1394 { 1395 if (!atomic_read(&(m->error_count))) 1396 return 'A'; 1397 1398 return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' : 1399 (test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' : 1400 (test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' : 1401 (test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U'; 1402 } 1403 1404 1405 static void mirror_status(struct dm_target *ti, status_type_t type, 1406 unsigned int status_flags, char *result, unsigned int maxlen) 1407 { 1408 unsigned int m, sz = 0; 1409 int num_feature_args = 0; 1410 struct mirror_set *ms = ti->private; 1411 struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh); 1412 char buffer[MAX_NR_MIRRORS + 1]; 1413 1414 switch (type) { 1415 case STATUSTYPE_INFO: 1416 DMEMIT("%d ", ms->nr_mirrors); 1417 for (m = 0; m < ms->nr_mirrors; m++) { 1418 DMEMIT("%s ", ms->mirror[m].dev->name); 1419 buffer[m] = device_status_char(&(ms->mirror[m])); 1420 } 1421 buffer[m] = '\0'; 1422 1423 DMEMIT("%llu/%llu 1 %s ", 1424 (unsigned long long)log->type->get_sync_count(log), 1425 (unsigned long long)ms->nr_regions, buffer); 1426 1427 sz += log->type->status(log, type, result+sz, maxlen-sz); 1428 1429 break; 1430 1431 case STATUSTYPE_TABLE: 1432 sz = log->type->status(log, type, result, maxlen); 1433 1434 DMEMIT("%d", ms->nr_mirrors); 1435 for (m = 0; m < ms->nr_mirrors; m++) 1436 DMEMIT(" %s %llu", ms->mirror[m].dev->name, 1437 (unsigned long long)ms->mirror[m].offset); 1438 1439 num_feature_args += !!errors_handled(ms); 1440 num_feature_args += !!keep_log(ms); 1441 if (num_feature_args) { 1442 DMEMIT(" %d", num_feature_args); 1443 if (errors_handled(ms)) 1444 DMEMIT(" handle_errors"); 1445 if (keep_log(ms)) 1446 DMEMIT(" keep_log"); 1447 } 1448 1449 break; 1450 1451 case STATUSTYPE_IMA: 1452 DMEMIT_TARGET_NAME_VERSION(ti->type); 1453 DMEMIT(",nr_mirrors=%d", ms->nr_mirrors); 1454 for (m = 0; m < ms->nr_mirrors; m++) { 1455 DMEMIT(",mirror_device_%d=%s", m, ms->mirror[m].dev->name); 1456 DMEMIT(",mirror_device_%d_status=%c", 1457 m, device_status_char(&(ms->mirror[m]))); 1458 } 1459 1460 DMEMIT(",handle_errors=%c", errors_handled(ms) ? 'y' : 'n'); 1461 DMEMIT(",keep_log=%c", keep_log(ms) ? 'y' : 'n'); 1462 1463 DMEMIT(",log_type_status="); 1464 sz += log->type->status(log, type, result+sz, maxlen-sz); 1465 DMEMIT(";"); 1466 break; 1467 } 1468 } 1469 1470 static int mirror_iterate_devices(struct dm_target *ti, 1471 iterate_devices_callout_fn fn, void *data) 1472 { 1473 struct mirror_set *ms = ti->private; 1474 int ret = 0; 1475 unsigned int i; 1476 1477 for (i = 0; !ret && i < ms->nr_mirrors; i++) 1478 ret = fn(ti, ms->mirror[i].dev, 1479 ms->mirror[i].offset, ti->len, data); 1480 1481 return ret; 1482 } 1483 1484 static struct target_type mirror_target = { 1485 .name = "mirror", 1486 .version = {1, 15, 0}, 1487 .module = THIS_MODULE, 1488 .features = DM_TARGET_ATOMIC_WRITES, 1489 .ctr = mirror_ctr, 1490 .dtr = mirror_dtr, 1491 .map = mirror_map, 1492 .end_io = mirror_end_io, 1493 .presuspend = mirror_presuspend, 1494 .postsuspend = mirror_postsuspend, 1495 .resume = mirror_resume, 1496 .status = mirror_status, 1497 .iterate_devices = mirror_iterate_devices, 1498 }; 1499 1500 static int __init dm_mirror_init(void) 1501 { 1502 int r; 1503 1504 dm_raid1_wq = alloc_workqueue("dm_raid1_wq", WQ_PERCPU, 0); 1505 if (!dm_raid1_wq) { 1506 DMERR("Failed to alloc workqueue"); 1507 return -ENOMEM; 1508 } 1509 1510 r = dm_register_target(&mirror_target); 1511 if (r < 0) { 1512 destroy_workqueue(dm_raid1_wq); 1513 return r; 1514 } 1515 1516 return 0; 1517 } 1518 1519 static void __exit dm_mirror_exit(void) 1520 { 1521 destroy_workqueue(dm_raid1_wq); 1522 dm_unregister_target(&mirror_target); 1523 } 1524 1525 /* Module hooks */ 1526 module_init(dm_mirror_init); 1527 module_exit(dm_mirror_exit); 1528 1529 MODULE_DESCRIPTION(DM_NAME " mirror target"); 1530 MODULE_AUTHOR("Joe Thornber"); 1531 MODULE_LICENSE("GPL"); 1532