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