1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * raid1.c : Multiple Devices driver for Linux 4 * 5 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat 6 * 7 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman 8 * 9 * RAID-1 management functions. 10 * 11 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 12 * 13 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> 14 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> 15 * 16 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support 17 * bitmapped intelligence in resync: 18 * 19 * - bitmap marked during normal i/o 20 * - bitmap used to skip nondirty blocks during sync 21 * 22 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology: 23 * - persistent bitmap code 24 */ 25 26 #include <linux/slab.h> 27 #include <linux/delay.h> 28 #include <linux/blkdev.h> 29 #include <linux/module.h> 30 #include <linux/seq_file.h> 31 #include <linux/ratelimit.h> 32 #include <linux/interval_tree_generic.h> 33 34 #include <trace/events/block.h> 35 36 #include "md.h" 37 #include "raid1.h" 38 #include "md-bitmap.h" 39 40 #define UNSUPPORTED_MDDEV_FLAGS \ 41 ((1L << MD_HAS_JOURNAL) | \ 42 (1L << MD_JOURNAL_CLEAN) | \ 43 (1L << MD_HAS_PPL) | \ 44 (1L << MD_HAS_MULTIPLE_PPLS)) 45 46 static void allow_barrier(struct r1conf *conf, sector_t sector_nr); 47 static void lower_barrier(struct r1conf *conf, sector_t sector_nr); 48 49 #define RAID_1_10_NAME "raid1" 50 #include "raid1-10.c" 51 52 #define START(node) ((node)->start) 53 #define LAST(node) ((node)->last) 54 INTERVAL_TREE_DEFINE(struct serial_info, node, sector_t, _subtree_last, 55 START, LAST, static inline, raid1_rb); 56 57 static int check_and_add_serial(struct md_rdev *rdev, struct r1bio *r1_bio, 58 struct serial_info *si, int idx) 59 { 60 unsigned long flags; 61 int ret = 0; 62 sector_t lo = r1_bio->sector; 63 sector_t hi = lo + r1_bio->sectors; 64 struct serial_in_rdev *serial = &rdev->serial[idx]; 65 66 spin_lock_irqsave(&serial->serial_lock, flags); 67 /* collision happened */ 68 if (raid1_rb_iter_first(&serial->serial_rb, lo, hi)) 69 ret = -EBUSY; 70 else { 71 si->start = lo; 72 si->last = hi; 73 raid1_rb_insert(si, &serial->serial_rb); 74 } 75 spin_unlock_irqrestore(&serial->serial_lock, flags); 76 77 return ret; 78 } 79 80 static void wait_for_serialization(struct md_rdev *rdev, struct r1bio *r1_bio) 81 { 82 struct mddev *mddev = rdev->mddev; 83 struct serial_info *si; 84 int idx = sector_to_idx(r1_bio->sector); 85 struct serial_in_rdev *serial = &rdev->serial[idx]; 86 87 if (WARN_ON(!mddev->serial_info_pool)) 88 return; 89 si = mempool_alloc(mddev->serial_info_pool, GFP_NOIO); 90 wait_event(serial->serial_io_wait, 91 check_and_add_serial(rdev, r1_bio, si, idx) == 0); 92 } 93 94 static void remove_serial(struct md_rdev *rdev, sector_t lo, sector_t hi) 95 { 96 struct serial_info *si; 97 unsigned long flags; 98 int found = 0; 99 struct mddev *mddev = rdev->mddev; 100 int idx = sector_to_idx(lo); 101 struct serial_in_rdev *serial = &rdev->serial[idx]; 102 103 spin_lock_irqsave(&serial->serial_lock, flags); 104 for (si = raid1_rb_iter_first(&serial->serial_rb, lo, hi); 105 si; si = raid1_rb_iter_next(si, lo, hi)) { 106 if (si->start == lo && si->last == hi) { 107 raid1_rb_remove(si, &serial->serial_rb); 108 mempool_free(si, mddev->serial_info_pool); 109 found = 1; 110 break; 111 } 112 } 113 if (!found) 114 WARN(1, "The write IO is not recorded for serialization\n"); 115 spin_unlock_irqrestore(&serial->serial_lock, flags); 116 wake_up(&serial->serial_io_wait); 117 } 118 119 /* 120 * for resync bio, r1bio pointer can be retrieved from the per-bio 121 * 'struct resync_pages'. 122 */ 123 static inline struct r1bio *get_resync_r1bio(struct bio *bio) 124 { 125 return get_resync_pages(bio)->raid_bio; 126 } 127 128 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data) 129 { 130 struct pool_info *pi = data; 131 int size = offsetof(struct r1bio, bios[pi->raid_disks]); 132 133 /* allocate a r1bio with room for raid_disks entries in the bios array */ 134 return kzalloc(size, gfp_flags); 135 } 136 137 #define RESYNC_DEPTH 32 138 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) 139 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH) 140 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9) 141 #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW) 142 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9) 143 144 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) 145 { 146 struct pool_info *pi = data; 147 struct r1bio *r1_bio; 148 struct bio *bio; 149 int need_pages; 150 int j; 151 struct resync_pages *rps; 152 153 r1_bio = r1bio_pool_alloc(gfp_flags, pi); 154 if (!r1_bio) 155 return NULL; 156 157 rps = kmalloc_array(pi->raid_disks, sizeof(struct resync_pages), 158 gfp_flags); 159 if (!rps) 160 goto out_free_r1bio; 161 162 /* 163 * Allocate bios : 1 for reading, n-1 for writing 164 */ 165 for (j = pi->raid_disks ; j-- ; ) { 166 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags); 167 if (!bio) 168 goto out_free_bio; 169 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0); 170 r1_bio->bios[j] = bio; 171 } 172 /* 173 * Allocate RESYNC_PAGES data pages and attach them to 174 * the first bio. 175 * If this is a user-requested check/repair, allocate 176 * RESYNC_PAGES for each bio. 177 */ 178 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) 179 need_pages = pi->raid_disks; 180 else 181 need_pages = 1; 182 for (j = 0; j < pi->raid_disks; j++) { 183 struct resync_pages *rp = &rps[j]; 184 185 bio = r1_bio->bios[j]; 186 187 if (j < need_pages) { 188 if (resync_alloc_pages(rp, gfp_flags)) 189 goto out_free_pages; 190 } else { 191 memcpy(rp, &rps[0], sizeof(*rp)); 192 resync_get_all_pages(rp); 193 } 194 195 rp->raid_bio = r1_bio; 196 bio->bi_private = rp; 197 } 198 199 r1_bio->master_bio = NULL; 200 201 return r1_bio; 202 203 out_free_pages: 204 while (--j >= 0) 205 resync_free_pages(&rps[j]); 206 207 out_free_bio: 208 while (++j < pi->raid_disks) { 209 bio_uninit(r1_bio->bios[j]); 210 kfree(r1_bio->bios[j]); 211 } 212 kfree(rps); 213 214 out_free_r1bio: 215 rbio_pool_free(r1_bio, data); 216 return NULL; 217 } 218 219 static void r1buf_pool_free(void *__r1_bio, void *data) 220 { 221 struct pool_info *pi = data; 222 int i; 223 struct r1bio *r1bio = __r1_bio; 224 struct resync_pages *rp = NULL; 225 226 for (i = pi->raid_disks; i--; ) { 227 rp = get_resync_pages(r1bio->bios[i]); 228 resync_free_pages(rp); 229 bio_uninit(r1bio->bios[i]); 230 kfree(r1bio->bios[i]); 231 } 232 233 /* resync pages array stored in the 1st bio's .bi_private */ 234 kfree(rp); 235 236 rbio_pool_free(r1bio, data); 237 } 238 239 static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio) 240 { 241 int i; 242 243 for (i = 0; i < conf->raid_disks * 2; i++) { 244 struct bio **bio = r1_bio->bios + i; 245 if (!BIO_SPECIAL(*bio)) 246 bio_put(*bio); 247 *bio = NULL; 248 } 249 } 250 251 static void free_r1bio(struct r1bio *r1_bio) 252 { 253 struct r1conf *conf = r1_bio->mddev->private; 254 255 put_all_bios(conf, r1_bio); 256 mempool_free(r1_bio, &conf->r1bio_pool); 257 } 258 259 static void put_buf(struct r1bio *r1_bio) 260 { 261 struct r1conf *conf = r1_bio->mddev->private; 262 sector_t sect = r1_bio->sector; 263 int i; 264 265 for (i = 0; i < conf->raid_disks * 2; i++) { 266 struct bio *bio = r1_bio->bios[i]; 267 if (bio->bi_end_io) 268 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev); 269 } 270 271 mempool_free(r1_bio, &conf->r1buf_pool); 272 273 lower_barrier(conf, sect); 274 } 275 276 static void reschedule_retry(struct r1bio *r1_bio) 277 { 278 unsigned long flags; 279 struct mddev *mddev = r1_bio->mddev; 280 struct r1conf *conf = mddev->private; 281 int idx; 282 283 idx = sector_to_idx(r1_bio->sector); 284 spin_lock_irqsave(&conf->device_lock, flags); 285 list_add(&r1_bio->retry_list, &conf->retry_list); 286 atomic_inc(&conf->nr_queued[idx]); 287 spin_unlock_irqrestore(&conf->device_lock, flags); 288 289 wake_up(&conf->wait_barrier); 290 md_wakeup_thread(mddev->thread); 291 } 292 293 /* 294 * raid_end_bio_io() is called when we have finished servicing a mirrored 295 * operation and are ready to return a success/failure code to the buffer 296 * cache layer. 297 */ 298 static void call_bio_endio(struct r1bio *r1_bio) 299 { 300 struct bio *bio = r1_bio->master_bio; 301 302 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) 303 bio->bi_status = BLK_STS_IOERR; 304 305 bio_endio(bio); 306 } 307 308 static void raid_end_bio_io(struct r1bio *r1_bio) 309 { 310 struct bio *bio = r1_bio->master_bio; 311 struct r1conf *conf = r1_bio->mddev->private; 312 sector_t sector = r1_bio->sector; 313 314 /* if nobody has done the final endio yet, do it now */ 315 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { 316 pr_debug("raid1: sync end %s on sectors %llu-%llu\n", 317 (bio_data_dir(bio) == WRITE) ? "write" : "read", 318 (unsigned long long) bio->bi_iter.bi_sector, 319 (unsigned long long) bio_end_sector(bio) - 1); 320 321 call_bio_endio(r1_bio); 322 } 323 324 free_r1bio(r1_bio); 325 /* 326 * Wake up any possible resync thread that waits for the device 327 * to go idle. All I/Os, even write-behind writes, are done. 328 */ 329 allow_barrier(conf, sector); 330 } 331 332 /* 333 * Update disk head position estimator based on IRQ completion info. 334 */ 335 static inline void update_head_pos(int disk, struct r1bio *r1_bio) 336 { 337 struct r1conf *conf = r1_bio->mddev->private; 338 339 conf->mirrors[disk].head_position = 340 r1_bio->sector + (r1_bio->sectors); 341 } 342 343 /* 344 * Find the disk number which triggered given bio 345 */ 346 static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio) 347 { 348 int mirror; 349 struct r1conf *conf = r1_bio->mddev->private; 350 int raid_disks = conf->raid_disks; 351 352 for (mirror = 0; mirror < raid_disks * 2; mirror++) 353 if (r1_bio->bios[mirror] == bio) 354 break; 355 356 BUG_ON(mirror == raid_disks * 2); 357 update_head_pos(mirror, r1_bio); 358 359 return mirror; 360 } 361 362 static void raid1_end_read_request(struct bio *bio) 363 { 364 int uptodate = !bio->bi_status; 365 struct r1bio *r1_bio = bio->bi_private; 366 struct r1conf *conf = r1_bio->mddev->private; 367 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev; 368 369 /* 370 * this branch is our 'one mirror IO has finished' event handler: 371 */ 372 update_head_pos(r1_bio->read_disk, r1_bio); 373 374 if (uptodate) 375 set_bit(R1BIO_Uptodate, &r1_bio->state); 376 else if (test_bit(FailFast, &rdev->flags) && 377 test_bit(R1BIO_FailFast, &r1_bio->state)) 378 /* This was a fail-fast read so we definitely 379 * want to retry */ 380 ; 381 else { 382 /* If all other devices have failed, we want to return 383 * the error upwards rather than fail the last device. 384 * Here we redefine "uptodate" to mean "Don't want to retry" 385 */ 386 unsigned long flags; 387 spin_lock_irqsave(&conf->device_lock, flags); 388 if (r1_bio->mddev->degraded == conf->raid_disks || 389 (r1_bio->mddev->degraded == conf->raid_disks-1 && 390 test_bit(In_sync, &rdev->flags))) 391 uptodate = 1; 392 spin_unlock_irqrestore(&conf->device_lock, flags); 393 } 394 395 if (uptodate) { 396 raid_end_bio_io(r1_bio); 397 rdev_dec_pending(rdev, conf->mddev); 398 } else { 399 /* 400 * oops, read error: 401 */ 402 pr_err_ratelimited("md/raid1:%s: %pg: rescheduling sector %llu\n", 403 mdname(conf->mddev), 404 rdev->bdev, 405 (unsigned long long)r1_bio->sector); 406 set_bit(R1BIO_ReadError, &r1_bio->state); 407 reschedule_retry(r1_bio); 408 /* don't drop the reference on read_disk yet */ 409 } 410 } 411 412 static void close_write(struct r1bio *r1_bio) 413 { 414 /* it really is the end of this request */ 415 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { 416 bio_free_pages(r1_bio->behind_master_bio); 417 bio_put(r1_bio->behind_master_bio); 418 r1_bio->behind_master_bio = NULL; 419 } 420 /* clear the bitmap if all writes complete successfully */ 421 md_bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector, 422 r1_bio->sectors, 423 !test_bit(R1BIO_Degraded, &r1_bio->state), 424 test_bit(R1BIO_BehindIO, &r1_bio->state)); 425 md_write_end(r1_bio->mddev); 426 } 427 428 static void r1_bio_write_done(struct r1bio *r1_bio) 429 { 430 if (!atomic_dec_and_test(&r1_bio->remaining)) 431 return; 432 433 if (test_bit(R1BIO_WriteError, &r1_bio->state)) 434 reschedule_retry(r1_bio); 435 else { 436 close_write(r1_bio); 437 if (test_bit(R1BIO_MadeGood, &r1_bio->state)) 438 reschedule_retry(r1_bio); 439 else 440 raid_end_bio_io(r1_bio); 441 } 442 } 443 444 static void raid1_end_write_request(struct bio *bio) 445 { 446 struct r1bio *r1_bio = bio->bi_private; 447 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state); 448 struct r1conf *conf = r1_bio->mddev->private; 449 struct bio *to_put = NULL; 450 int mirror = find_bio_disk(r1_bio, bio); 451 struct md_rdev *rdev = conf->mirrors[mirror].rdev; 452 bool discard_error; 453 sector_t lo = r1_bio->sector; 454 sector_t hi = r1_bio->sector + r1_bio->sectors; 455 456 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD; 457 458 /* 459 * 'one mirror IO has finished' event handler: 460 */ 461 if (bio->bi_status && !discard_error) { 462 set_bit(WriteErrorSeen, &rdev->flags); 463 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 464 set_bit(MD_RECOVERY_NEEDED, & 465 conf->mddev->recovery); 466 467 if (test_bit(FailFast, &rdev->flags) && 468 (bio->bi_opf & MD_FAILFAST) && 469 /* We never try FailFast to WriteMostly devices */ 470 !test_bit(WriteMostly, &rdev->flags)) { 471 md_error(r1_bio->mddev, rdev); 472 } 473 474 /* 475 * When the device is faulty, it is not necessary to 476 * handle write error. 477 */ 478 if (!test_bit(Faulty, &rdev->flags)) 479 set_bit(R1BIO_WriteError, &r1_bio->state); 480 else { 481 /* Fail the request */ 482 set_bit(R1BIO_Degraded, &r1_bio->state); 483 /* Finished with this branch */ 484 r1_bio->bios[mirror] = NULL; 485 to_put = bio; 486 } 487 } else { 488 /* 489 * Set R1BIO_Uptodate in our master bio, so that we 490 * will return a good error code for to the higher 491 * levels even if IO on some other mirrored buffer 492 * fails. 493 * 494 * The 'master' represents the composite IO operation 495 * to user-side. So if something waits for IO, then it 496 * will wait for the 'master' bio. 497 */ 498 r1_bio->bios[mirror] = NULL; 499 to_put = bio; 500 /* 501 * Do not set R1BIO_Uptodate if the current device is 502 * rebuilding or Faulty. This is because we cannot use 503 * such device for properly reading the data back (we could 504 * potentially use it, if the current write would have felt 505 * before rdev->recovery_offset, but for simplicity we don't 506 * check this here. 507 */ 508 if (test_bit(In_sync, &rdev->flags) && 509 !test_bit(Faulty, &rdev->flags)) 510 set_bit(R1BIO_Uptodate, &r1_bio->state); 511 512 /* Maybe we can clear some bad blocks. */ 513 if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors) && 514 !discard_error) { 515 r1_bio->bios[mirror] = IO_MADE_GOOD; 516 set_bit(R1BIO_MadeGood, &r1_bio->state); 517 } 518 } 519 520 if (behind) { 521 if (test_bit(CollisionCheck, &rdev->flags)) 522 remove_serial(rdev, lo, hi); 523 if (test_bit(WriteMostly, &rdev->flags)) 524 atomic_dec(&r1_bio->behind_remaining); 525 526 /* 527 * In behind mode, we ACK the master bio once the I/O 528 * has safely reached all non-writemostly 529 * disks. Setting the Returned bit ensures that this 530 * gets done only once -- we don't ever want to return 531 * -EIO here, instead we'll wait 532 */ 533 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) && 534 test_bit(R1BIO_Uptodate, &r1_bio->state)) { 535 /* Maybe we can return now */ 536 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { 537 struct bio *mbio = r1_bio->master_bio; 538 pr_debug("raid1: behind end write sectors" 539 " %llu-%llu\n", 540 (unsigned long long) mbio->bi_iter.bi_sector, 541 (unsigned long long) bio_end_sector(mbio) - 1); 542 call_bio_endio(r1_bio); 543 } 544 } 545 } else if (rdev->mddev->serialize_policy) 546 remove_serial(rdev, lo, hi); 547 if (r1_bio->bios[mirror] == NULL) 548 rdev_dec_pending(rdev, conf->mddev); 549 550 /* 551 * Let's see if all mirrored write operations have finished 552 * already. 553 */ 554 r1_bio_write_done(r1_bio); 555 556 if (to_put) 557 bio_put(to_put); 558 } 559 560 static sector_t align_to_barrier_unit_end(sector_t start_sector, 561 sector_t sectors) 562 { 563 sector_t len; 564 565 WARN_ON(sectors == 0); 566 /* 567 * len is the number of sectors from start_sector to end of the 568 * barrier unit which start_sector belongs to. 569 */ 570 len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) - 571 start_sector; 572 573 if (len > sectors) 574 len = sectors; 575 576 return len; 577 } 578 579 static void update_read_sectors(struct r1conf *conf, int disk, 580 sector_t this_sector, int len) 581 { 582 struct raid1_info *info = &conf->mirrors[disk]; 583 584 atomic_inc(&info->rdev->nr_pending); 585 if (info->next_seq_sect != this_sector) 586 info->seq_start = this_sector; 587 info->next_seq_sect = this_sector + len; 588 } 589 590 static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio, 591 int *max_sectors) 592 { 593 sector_t this_sector = r1_bio->sector; 594 int len = r1_bio->sectors; 595 int disk; 596 597 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { 598 struct md_rdev *rdev; 599 int read_len; 600 601 if (r1_bio->bios[disk] == IO_BLOCKED) 602 continue; 603 604 rdev = conf->mirrors[disk].rdev; 605 if (!rdev || test_bit(Faulty, &rdev->flags)) 606 continue; 607 608 /* choose the first disk even if it has some bad blocks. */ 609 read_len = raid1_check_read_range(rdev, this_sector, &len); 610 if (read_len > 0) { 611 update_read_sectors(conf, disk, this_sector, read_len); 612 *max_sectors = read_len; 613 return disk; 614 } 615 } 616 617 return -1; 618 } 619 620 static int choose_bb_rdev(struct r1conf *conf, struct r1bio *r1_bio, 621 int *max_sectors) 622 { 623 sector_t this_sector = r1_bio->sector; 624 int best_disk = -1; 625 int best_len = 0; 626 int disk; 627 628 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { 629 struct md_rdev *rdev; 630 int len; 631 int read_len; 632 633 if (r1_bio->bios[disk] == IO_BLOCKED) 634 continue; 635 636 rdev = conf->mirrors[disk].rdev; 637 if (!rdev || test_bit(Faulty, &rdev->flags) || 638 test_bit(WriteMostly, &rdev->flags)) 639 continue; 640 641 /* keep track of the disk with the most readable sectors. */ 642 len = r1_bio->sectors; 643 read_len = raid1_check_read_range(rdev, this_sector, &len); 644 if (read_len > best_len) { 645 best_disk = disk; 646 best_len = read_len; 647 } 648 } 649 650 if (best_disk != -1) { 651 *max_sectors = best_len; 652 update_read_sectors(conf, best_disk, this_sector, best_len); 653 } 654 655 return best_disk; 656 } 657 658 static int choose_slow_rdev(struct r1conf *conf, struct r1bio *r1_bio, 659 int *max_sectors) 660 { 661 sector_t this_sector = r1_bio->sector; 662 int bb_disk = -1; 663 int bb_read_len = 0; 664 int disk; 665 666 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { 667 struct md_rdev *rdev; 668 int len; 669 int read_len; 670 671 if (r1_bio->bios[disk] == IO_BLOCKED) 672 continue; 673 674 rdev = conf->mirrors[disk].rdev; 675 if (!rdev || test_bit(Faulty, &rdev->flags) || 676 !test_bit(WriteMostly, &rdev->flags)) 677 continue; 678 679 /* there are no bad blocks, we can use this disk */ 680 len = r1_bio->sectors; 681 read_len = raid1_check_read_range(rdev, this_sector, &len); 682 if (read_len == r1_bio->sectors) { 683 *max_sectors = read_len; 684 update_read_sectors(conf, disk, this_sector, read_len); 685 return disk; 686 } 687 688 /* 689 * there are partial bad blocks, choose the rdev with largest 690 * read length. 691 */ 692 if (read_len > bb_read_len) { 693 bb_disk = disk; 694 bb_read_len = read_len; 695 } 696 } 697 698 if (bb_disk != -1) { 699 *max_sectors = bb_read_len; 700 update_read_sectors(conf, bb_disk, this_sector, bb_read_len); 701 } 702 703 return bb_disk; 704 } 705 706 static bool is_sequential(struct r1conf *conf, int disk, struct r1bio *r1_bio) 707 { 708 /* TODO: address issues with this check and concurrency. */ 709 return conf->mirrors[disk].next_seq_sect == r1_bio->sector || 710 conf->mirrors[disk].head_position == r1_bio->sector; 711 } 712 713 /* 714 * If buffered sequential IO size exceeds optimal iosize, check if there is idle 715 * disk. If yes, choose the idle disk. 716 */ 717 static bool should_choose_next(struct r1conf *conf, int disk) 718 { 719 struct raid1_info *mirror = &conf->mirrors[disk]; 720 int opt_iosize; 721 722 if (!test_bit(Nonrot, &mirror->rdev->flags)) 723 return false; 724 725 opt_iosize = bdev_io_opt(mirror->rdev->bdev) >> 9; 726 return opt_iosize > 0 && mirror->seq_start != MaxSector && 727 mirror->next_seq_sect > opt_iosize && 728 mirror->next_seq_sect - opt_iosize >= mirror->seq_start; 729 } 730 731 static bool rdev_readable(struct md_rdev *rdev, struct r1bio *r1_bio) 732 { 733 if (!rdev || test_bit(Faulty, &rdev->flags)) 734 return false; 735 736 /* still in recovery */ 737 if (!test_bit(In_sync, &rdev->flags) && 738 rdev->recovery_offset < r1_bio->sector + r1_bio->sectors) 739 return false; 740 741 /* don't read from slow disk unless have to */ 742 if (test_bit(WriteMostly, &rdev->flags)) 743 return false; 744 745 /* don't split IO for bad blocks unless have to */ 746 if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors)) 747 return false; 748 749 return true; 750 } 751 752 struct read_balance_ctl { 753 sector_t closest_dist; 754 int closest_dist_disk; 755 int min_pending; 756 int min_pending_disk; 757 int sequential_disk; 758 int readable_disks; 759 }; 760 761 static int choose_best_rdev(struct r1conf *conf, struct r1bio *r1_bio) 762 { 763 int disk; 764 struct read_balance_ctl ctl = { 765 .closest_dist_disk = -1, 766 .closest_dist = MaxSector, 767 .min_pending_disk = -1, 768 .min_pending = UINT_MAX, 769 .sequential_disk = -1, 770 }; 771 772 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { 773 struct md_rdev *rdev; 774 sector_t dist; 775 unsigned int pending; 776 777 if (r1_bio->bios[disk] == IO_BLOCKED) 778 continue; 779 780 rdev = conf->mirrors[disk].rdev; 781 if (!rdev_readable(rdev, r1_bio)) 782 continue; 783 784 /* At least two disks to choose from so failfast is OK */ 785 if (ctl.readable_disks++ == 1) 786 set_bit(R1BIO_FailFast, &r1_bio->state); 787 788 pending = atomic_read(&rdev->nr_pending); 789 dist = abs(r1_bio->sector - conf->mirrors[disk].head_position); 790 791 /* Don't change to another disk for sequential reads */ 792 if (is_sequential(conf, disk, r1_bio)) { 793 if (!should_choose_next(conf, disk)) 794 return disk; 795 796 /* 797 * Add 'pending' to avoid choosing this disk if 798 * there is other idle disk. 799 */ 800 pending++; 801 /* 802 * If there is no other idle disk, this disk 803 * will be chosen. 804 */ 805 ctl.sequential_disk = disk; 806 } 807 808 if (ctl.min_pending > pending) { 809 ctl.min_pending = pending; 810 ctl.min_pending_disk = disk; 811 } 812 813 if (ctl.closest_dist > dist) { 814 ctl.closest_dist = dist; 815 ctl.closest_dist_disk = disk; 816 } 817 } 818 819 /* 820 * sequential IO size exceeds optimal iosize, however, there is no other 821 * idle disk, so choose the sequential disk. 822 */ 823 if (ctl.sequential_disk != -1 && ctl.min_pending != 0) 824 return ctl.sequential_disk; 825 826 /* 827 * If all disks are rotational, choose the closest disk. If any disk is 828 * non-rotational, choose the disk with less pending request even the 829 * disk is rotational, which might/might not be optimal for raids with 830 * mixed ratation/non-rotational disks depending on workload. 831 */ 832 if (ctl.min_pending_disk != -1 && 833 (READ_ONCE(conf->nonrot_disks) || ctl.min_pending == 0)) 834 return ctl.min_pending_disk; 835 else 836 return ctl.closest_dist_disk; 837 } 838 839 /* 840 * This routine returns the disk from which the requested read should be done. 841 * 842 * 1) If resync is in progress, find the first usable disk and use it even if it 843 * has some bad blocks. 844 * 845 * 2) Now that there is no resync, loop through all disks and skipping slow 846 * disks and disks with bad blocks for now. Only pay attention to key disk 847 * choice. 848 * 849 * 3) If we've made it this far, now look for disks with bad blocks and choose 850 * the one with most number of sectors. 851 * 852 * 4) If we are all the way at the end, we have no choice but to use a disk even 853 * if it is write mostly. 854 * 855 * The rdev for the device selected will have nr_pending incremented. 856 */ 857 static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, 858 int *max_sectors) 859 { 860 int disk; 861 862 clear_bit(R1BIO_FailFast, &r1_bio->state); 863 864 if (raid1_should_read_first(conf->mddev, r1_bio->sector, 865 r1_bio->sectors)) 866 return choose_first_rdev(conf, r1_bio, max_sectors); 867 868 disk = choose_best_rdev(conf, r1_bio); 869 if (disk >= 0) { 870 *max_sectors = r1_bio->sectors; 871 update_read_sectors(conf, disk, r1_bio->sector, 872 r1_bio->sectors); 873 return disk; 874 } 875 876 /* 877 * If we are here it means we didn't find a perfectly good disk so 878 * now spend a bit more time trying to find one with the most good 879 * sectors. 880 */ 881 disk = choose_bb_rdev(conf, r1_bio, max_sectors); 882 if (disk >= 0) 883 return disk; 884 885 return choose_slow_rdev(conf, r1_bio, max_sectors); 886 } 887 888 static void wake_up_barrier(struct r1conf *conf) 889 { 890 if (wq_has_sleeper(&conf->wait_barrier)) 891 wake_up(&conf->wait_barrier); 892 } 893 894 static void flush_bio_list(struct r1conf *conf, struct bio *bio) 895 { 896 /* flush any pending bitmap writes to disk before proceeding w/ I/O */ 897 raid1_prepare_flush_writes(conf->mddev->bitmap); 898 wake_up_barrier(conf); 899 900 while (bio) { /* submit pending writes */ 901 struct bio *next = bio->bi_next; 902 903 raid1_submit_write(bio); 904 bio = next; 905 cond_resched(); 906 } 907 } 908 909 static void flush_pending_writes(struct r1conf *conf) 910 { 911 /* Any writes that have been queued but are awaiting 912 * bitmap updates get flushed here. 913 */ 914 spin_lock_irq(&conf->device_lock); 915 916 if (conf->pending_bio_list.head) { 917 struct blk_plug plug; 918 struct bio *bio; 919 920 bio = bio_list_get(&conf->pending_bio_list); 921 spin_unlock_irq(&conf->device_lock); 922 923 /* 924 * As this is called in a wait_event() loop (see freeze_array), 925 * current->state might be TASK_UNINTERRUPTIBLE which will 926 * cause a warning when we prepare to wait again. As it is 927 * rare that this path is taken, it is perfectly safe to force 928 * us to go around the wait_event() loop again, so the warning 929 * is a false-positive. Silence the warning by resetting 930 * thread state 931 */ 932 __set_current_state(TASK_RUNNING); 933 blk_start_plug(&plug); 934 flush_bio_list(conf, bio); 935 blk_finish_plug(&plug); 936 } else 937 spin_unlock_irq(&conf->device_lock); 938 } 939 940 /* Barriers.... 941 * Sometimes we need to suspend IO while we do something else, 942 * either some resync/recovery, or reconfigure the array. 943 * To do this we raise a 'barrier'. 944 * The 'barrier' is a counter that can be raised multiple times 945 * to count how many activities are happening which preclude 946 * normal IO. 947 * We can only raise the barrier if there is no pending IO. 948 * i.e. if nr_pending == 0. 949 * We choose only to raise the barrier if no-one is waiting for the 950 * barrier to go down. This means that as soon as an IO request 951 * is ready, no other operations which require a barrier will start 952 * until the IO request has had a chance. 953 * 954 * So: regular IO calls 'wait_barrier'. When that returns there 955 * is no backgroup IO happening, It must arrange to call 956 * allow_barrier when it has finished its IO. 957 * backgroup IO calls must call raise_barrier. Once that returns 958 * there is no normal IO happeing. It must arrange to call 959 * lower_barrier when the particular background IO completes. 960 * 961 * If resync/recovery is interrupted, returns -EINTR; 962 * Otherwise, returns 0. 963 */ 964 static int raise_barrier(struct r1conf *conf, sector_t sector_nr) 965 { 966 int idx = sector_to_idx(sector_nr); 967 968 spin_lock_irq(&conf->resync_lock); 969 970 /* Wait until no block IO is waiting */ 971 wait_event_lock_irq(conf->wait_barrier, 972 !atomic_read(&conf->nr_waiting[idx]), 973 conf->resync_lock); 974 975 /* block any new IO from starting */ 976 atomic_inc(&conf->barrier[idx]); 977 /* 978 * In raise_barrier() we firstly increase conf->barrier[idx] then 979 * check conf->nr_pending[idx]. In _wait_barrier() we firstly 980 * increase conf->nr_pending[idx] then check conf->barrier[idx]. 981 * A memory barrier here to make sure conf->nr_pending[idx] won't 982 * be fetched before conf->barrier[idx] is increased. Otherwise 983 * there will be a race between raise_barrier() and _wait_barrier(). 984 */ 985 smp_mb__after_atomic(); 986 987 /* For these conditions we must wait: 988 * A: while the array is in frozen state 989 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O 990 * existing in corresponding I/O barrier bucket. 991 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches 992 * max resync count which allowed on current I/O barrier bucket. 993 */ 994 wait_event_lock_irq(conf->wait_barrier, 995 (!conf->array_frozen && 996 !atomic_read(&conf->nr_pending[idx]) && 997 atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH) || 998 test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery), 999 conf->resync_lock); 1000 1001 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { 1002 atomic_dec(&conf->barrier[idx]); 1003 spin_unlock_irq(&conf->resync_lock); 1004 wake_up(&conf->wait_barrier); 1005 return -EINTR; 1006 } 1007 1008 atomic_inc(&conf->nr_sync_pending); 1009 spin_unlock_irq(&conf->resync_lock); 1010 1011 return 0; 1012 } 1013 1014 static void lower_barrier(struct r1conf *conf, sector_t sector_nr) 1015 { 1016 int idx = sector_to_idx(sector_nr); 1017 1018 BUG_ON(atomic_read(&conf->barrier[idx]) <= 0); 1019 1020 atomic_dec(&conf->barrier[idx]); 1021 atomic_dec(&conf->nr_sync_pending); 1022 wake_up(&conf->wait_barrier); 1023 } 1024 1025 static bool _wait_barrier(struct r1conf *conf, int idx, bool nowait) 1026 { 1027 bool ret = true; 1028 1029 /* 1030 * We need to increase conf->nr_pending[idx] very early here, 1031 * then raise_barrier() can be blocked when it waits for 1032 * conf->nr_pending[idx] to be 0. Then we can avoid holding 1033 * conf->resync_lock when there is no barrier raised in same 1034 * barrier unit bucket. Also if the array is frozen, I/O 1035 * should be blocked until array is unfrozen. 1036 */ 1037 atomic_inc(&conf->nr_pending[idx]); 1038 /* 1039 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then 1040 * check conf->barrier[idx]. In raise_barrier() we firstly increase 1041 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory 1042 * barrier is necessary here to make sure conf->barrier[idx] won't be 1043 * fetched before conf->nr_pending[idx] is increased. Otherwise there 1044 * will be a race between _wait_barrier() and raise_barrier(). 1045 */ 1046 smp_mb__after_atomic(); 1047 1048 /* 1049 * Don't worry about checking two atomic_t variables at same time 1050 * here. If during we check conf->barrier[idx], the array is 1051 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is 1052 * 0, it is safe to return and make the I/O continue. Because the 1053 * array is frozen, all I/O returned here will eventually complete 1054 * or be queued, no race will happen. See code comment in 1055 * frozen_array(). 1056 */ 1057 if (!READ_ONCE(conf->array_frozen) && 1058 !atomic_read(&conf->barrier[idx])) 1059 return ret; 1060 1061 /* 1062 * After holding conf->resync_lock, conf->nr_pending[idx] 1063 * should be decreased before waiting for barrier to drop. 1064 * Otherwise, we may encounter a race condition because 1065 * raise_barrer() might be waiting for conf->nr_pending[idx] 1066 * to be 0 at same time. 1067 */ 1068 spin_lock_irq(&conf->resync_lock); 1069 atomic_inc(&conf->nr_waiting[idx]); 1070 atomic_dec(&conf->nr_pending[idx]); 1071 /* 1072 * In case freeze_array() is waiting for 1073 * get_unqueued_pending() == extra 1074 */ 1075 wake_up_barrier(conf); 1076 /* Wait for the barrier in same barrier unit bucket to drop. */ 1077 1078 /* Return false when nowait flag is set */ 1079 if (nowait) { 1080 ret = false; 1081 } else { 1082 wait_event_lock_irq(conf->wait_barrier, 1083 !conf->array_frozen && 1084 !atomic_read(&conf->barrier[idx]), 1085 conf->resync_lock); 1086 atomic_inc(&conf->nr_pending[idx]); 1087 } 1088 1089 atomic_dec(&conf->nr_waiting[idx]); 1090 spin_unlock_irq(&conf->resync_lock); 1091 return ret; 1092 } 1093 1094 static bool wait_read_barrier(struct r1conf *conf, sector_t sector_nr, bool nowait) 1095 { 1096 int idx = sector_to_idx(sector_nr); 1097 bool ret = true; 1098 1099 /* 1100 * Very similar to _wait_barrier(). The difference is, for read 1101 * I/O we don't need wait for sync I/O, but if the whole array 1102 * is frozen, the read I/O still has to wait until the array is 1103 * unfrozen. Since there is no ordering requirement with 1104 * conf->barrier[idx] here, memory barrier is unnecessary as well. 1105 */ 1106 atomic_inc(&conf->nr_pending[idx]); 1107 1108 if (!READ_ONCE(conf->array_frozen)) 1109 return ret; 1110 1111 spin_lock_irq(&conf->resync_lock); 1112 atomic_inc(&conf->nr_waiting[idx]); 1113 atomic_dec(&conf->nr_pending[idx]); 1114 /* 1115 * In case freeze_array() is waiting for 1116 * get_unqueued_pending() == extra 1117 */ 1118 wake_up_barrier(conf); 1119 /* Wait for array to be unfrozen */ 1120 1121 /* Return false when nowait flag is set */ 1122 if (nowait) { 1123 /* Return false when nowait flag is set */ 1124 ret = false; 1125 } else { 1126 wait_event_lock_irq(conf->wait_barrier, 1127 !conf->array_frozen, 1128 conf->resync_lock); 1129 atomic_inc(&conf->nr_pending[idx]); 1130 } 1131 1132 atomic_dec(&conf->nr_waiting[idx]); 1133 spin_unlock_irq(&conf->resync_lock); 1134 return ret; 1135 } 1136 1137 static bool wait_barrier(struct r1conf *conf, sector_t sector_nr, bool nowait) 1138 { 1139 int idx = sector_to_idx(sector_nr); 1140 1141 return _wait_barrier(conf, idx, nowait); 1142 } 1143 1144 static void _allow_barrier(struct r1conf *conf, int idx) 1145 { 1146 atomic_dec(&conf->nr_pending[idx]); 1147 wake_up_barrier(conf); 1148 } 1149 1150 static void allow_barrier(struct r1conf *conf, sector_t sector_nr) 1151 { 1152 int idx = sector_to_idx(sector_nr); 1153 1154 _allow_barrier(conf, idx); 1155 } 1156 1157 /* conf->resync_lock should be held */ 1158 static int get_unqueued_pending(struct r1conf *conf) 1159 { 1160 int idx, ret; 1161 1162 ret = atomic_read(&conf->nr_sync_pending); 1163 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++) 1164 ret += atomic_read(&conf->nr_pending[idx]) - 1165 atomic_read(&conf->nr_queued[idx]); 1166 1167 return ret; 1168 } 1169 1170 static void freeze_array(struct r1conf *conf, int extra) 1171 { 1172 /* Stop sync I/O and normal I/O and wait for everything to 1173 * go quiet. 1174 * This is called in two situations: 1175 * 1) management command handlers (reshape, remove disk, quiesce). 1176 * 2) one normal I/O request failed. 1177 1178 * After array_frozen is set to 1, new sync IO will be blocked at 1179 * raise_barrier(), and new normal I/O will blocked at _wait_barrier() 1180 * or wait_read_barrier(). The flying I/Os will either complete or be 1181 * queued. When everything goes quite, there are only queued I/Os left. 1182 1183 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the 1184 * barrier bucket index which this I/O request hits. When all sync and 1185 * normal I/O are queued, sum of all conf->nr_pending[] will match sum 1186 * of all conf->nr_queued[]. But normal I/O failure is an exception, 1187 * in handle_read_error(), we may call freeze_array() before trying to 1188 * fix the read error. In this case, the error read I/O is not queued, 1189 * so get_unqueued_pending() == 1. 1190 * 1191 * Therefore before this function returns, we need to wait until 1192 * get_unqueued_pendings(conf) gets equal to extra. For 1193 * normal I/O context, extra is 1, in rested situations extra is 0. 1194 */ 1195 spin_lock_irq(&conf->resync_lock); 1196 conf->array_frozen = 1; 1197 mddev_add_trace_msg(conf->mddev, "raid1 wait freeze"); 1198 wait_event_lock_irq_cmd( 1199 conf->wait_barrier, 1200 get_unqueued_pending(conf) == extra, 1201 conf->resync_lock, 1202 flush_pending_writes(conf)); 1203 spin_unlock_irq(&conf->resync_lock); 1204 } 1205 static void unfreeze_array(struct r1conf *conf) 1206 { 1207 /* reverse the effect of the freeze */ 1208 spin_lock_irq(&conf->resync_lock); 1209 conf->array_frozen = 0; 1210 spin_unlock_irq(&conf->resync_lock); 1211 wake_up(&conf->wait_barrier); 1212 } 1213 1214 static void alloc_behind_master_bio(struct r1bio *r1_bio, 1215 struct bio *bio) 1216 { 1217 int size = bio->bi_iter.bi_size; 1218 unsigned vcnt = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1219 int i = 0; 1220 struct bio *behind_bio = NULL; 1221 1222 behind_bio = bio_alloc_bioset(NULL, vcnt, 0, GFP_NOIO, 1223 &r1_bio->mddev->bio_set); 1224 1225 /* discard op, we don't support writezero/writesame yet */ 1226 if (!bio_has_data(bio)) { 1227 behind_bio->bi_iter.bi_size = size; 1228 goto skip_copy; 1229 } 1230 1231 while (i < vcnt && size) { 1232 struct page *page; 1233 int len = min_t(int, PAGE_SIZE, size); 1234 1235 page = alloc_page(GFP_NOIO); 1236 if (unlikely(!page)) 1237 goto free_pages; 1238 1239 if (!bio_add_page(behind_bio, page, len, 0)) { 1240 put_page(page); 1241 goto free_pages; 1242 } 1243 1244 size -= len; 1245 i++; 1246 } 1247 1248 bio_copy_data(behind_bio, bio); 1249 skip_copy: 1250 r1_bio->behind_master_bio = behind_bio; 1251 set_bit(R1BIO_BehindIO, &r1_bio->state); 1252 1253 return; 1254 1255 free_pages: 1256 pr_debug("%dB behind alloc failed, doing sync I/O\n", 1257 bio->bi_iter.bi_size); 1258 bio_free_pages(behind_bio); 1259 bio_put(behind_bio); 1260 } 1261 1262 static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule) 1263 { 1264 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, 1265 cb); 1266 struct mddev *mddev = plug->cb.data; 1267 struct r1conf *conf = mddev->private; 1268 struct bio *bio; 1269 1270 if (from_schedule) { 1271 spin_lock_irq(&conf->device_lock); 1272 bio_list_merge(&conf->pending_bio_list, &plug->pending); 1273 spin_unlock_irq(&conf->device_lock); 1274 wake_up_barrier(conf); 1275 md_wakeup_thread(mddev->thread); 1276 kfree(plug); 1277 return; 1278 } 1279 1280 /* we aren't scheduling, so we can do the write-out directly. */ 1281 bio = bio_list_get(&plug->pending); 1282 flush_bio_list(conf, bio); 1283 kfree(plug); 1284 } 1285 1286 static void init_r1bio(struct r1bio *r1_bio, struct mddev *mddev, struct bio *bio) 1287 { 1288 r1_bio->master_bio = bio; 1289 r1_bio->sectors = bio_sectors(bio); 1290 r1_bio->state = 0; 1291 r1_bio->mddev = mddev; 1292 r1_bio->sector = bio->bi_iter.bi_sector; 1293 } 1294 1295 static inline struct r1bio * 1296 alloc_r1bio(struct mddev *mddev, struct bio *bio) 1297 { 1298 struct r1conf *conf = mddev->private; 1299 struct r1bio *r1_bio; 1300 1301 r1_bio = mempool_alloc(&conf->r1bio_pool, GFP_NOIO); 1302 /* Ensure no bio records IO_BLOCKED */ 1303 memset(r1_bio->bios, 0, conf->raid_disks * sizeof(r1_bio->bios[0])); 1304 init_r1bio(r1_bio, mddev, bio); 1305 return r1_bio; 1306 } 1307 1308 static void raid1_read_request(struct mddev *mddev, struct bio *bio, 1309 int max_read_sectors, struct r1bio *r1_bio) 1310 { 1311 struct r1conf *conf = mddev->private; 1312 struct raid1_info *mirror; 1313 struct bio *read_bio; 1314 struct bitmap *bitmap = mddev->bitmap; 1315 const enum req_op op = bio_op(bio); 1316 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC; 1317 int max_sectors; 1318 int rdisk; 1319 bool r1bio_existed = !!r1_bio; 1320 char b[BDEVNAME_SIZE]; 1321 1322 /* 1323 * If r1_bio is set, we are blocking the raid1d thread 1324 * so there is a tiny risk of deadlock. So ask for 1325 * emergency memory if needed. 1326 */ 1327 gfp_t gfp = r1_bio ? (GFP_NOIO | __GFP_HIGH) : GFP_NOIO; 1328 1329 if (r1bio_existed) { 1330 /* Need to get the block device name carefully */ 1331 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev; 1332 1333 if (rdev) 1334 snprintf(b, sizeof(b), "%pg", rdev->bdev); 1335 else 1336 strcpy(b, "???"); 1337 } 1338 1339 /* 1340 * Still need barrier for READ in case that whole 1341 * array is frozen. 1342 */ 1343 if (!wait_read_barrier(conf, bio->bi_iter.bi_sector, 1344 bio->bi_opf & REQ_NOWAIT)) { 1345 bio_wouldblock_error(bio); 1346 return; 1347 } 1348 1349 if (!r1_bio) 1350 r1_bio = alloc_r1bio(mddev, bio); 1351 else 1352 init_r1bio(r1_bio, mddev, bio); 1353 r1_bio->sectors = max_read_sectors; 1354 1355 /* 1356 * make_request() can abort the operation when read-ahead is being 1357 * used and no empty request is available. 1358 */ 1359 rdisk = read_balance(conf, r1_bio, &max_sectors); 1360 1361 if (rdisk < 0) { 1362 /* couldn't find anywhere to read from */ 1363 if (r1bio_existed) { 1364 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n", 1365 mdname(mddev), 1366 b, 1367 (unsigned long long)r1_bio->sector); 1368 } 1369 raid_end_bio_io(r1_bio); 1370 return; 1371 } 1372 mirror = conf->mirrors + rdisk; 1373 1374 if (r1bio_existed) 1375 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %pg\n", 1376 mdname(mddev), 1377 (unsigned long long)r1_bio->sector, 1378 mirror->rdev->bdev); 1379 1380 if (test_bit(WriteMostly, &mirror->rdev->flags) && 1381 bitmap) { 1382 /* 1383 * Reading from a write-mostly device must take care not to 1384 * over-take any writes that are 'behind' 1385 */ 1386 mddev_add_trace_msg(mddev, "raid1 wait behind writes"); 1387 wait_event(bitmap->behind_wait, 1388 atomic_read(&bitmap->behind_writes) == 0); 1389 } 1390 1391 if (max_sectors < bio_sectors(bio)) { 1392 struct bio *split = bio_split(bio, max_sectors, 1393 gfp, &conf->bio_split); 1394 bio_chain(split, bio); 1395 submit_bio_noacct(bio); 1396 bio = split; 1397 r1_bio->master_bio = bio; 1398 r1_bio->sectors = max_sectors; 1399 } 1400 1401 r1_bio->read_disk = rdisk; 1402 if (!r1bio_existed) { 1403 md_account_bio(mddev, &bio); 1404 r1_bio->master_bio = bio; 1405 } 1406 read_bio = bio_alloc_clone(mirror->rdev->bdev, bio, gfp, 1407 &mddev->bio_set); 1408 1409 r1_bio->bios[rdisk] = read_bio; 1410 1411 read_bio->bi_iter.bi_sector = r1_bio->sector + 1412 mirror->rdev->data_offset; 1413 read_bio->bi_end_io = raid1_end_read_request; 1414 read_bio->bi_opf = op | do_sync; 1415 if (test_bit(FailFast, &mirror->rdev->flags) && 1416 test_bit(R1BIO_FailFast, &r1_bio->state)) 1417 read_bio->bi_opf |= MD_FAILFAST; 1418 read_bio->bi_private = r1_bio; 1419 mddev_trace_remap(mddev, read_bio, r1_bio->sector); 1420 submit_bio_noacct(read_bio); 1421 } 1422 1423 static void raid1_write_request(struct mddev *mddev, struct bio *bio, 1424 int max_write_sectors) 1425 { 1426 struct r1conf *conf = mddev->private; 1427 struct r1bio *r1_bio; 1428 int i, disks; 1429 struct bitmap *bitmap = mddev->bitmap; 1430 unsigned long flags; 1431 struct md_rdev *blocked_rdev; 1432 int first_clone; 1433 int max_sectors; 1434 bool write_behind = false; 1435 bool is_discard = (bio_op(bio) == REQ_OP_DISCARD); 1436 1437 if (mddev_is_clustered(mddev) && 1438 md_cluster_ops->area_resyncing(mddev, WRITE, 1439 bio->bi_iter.bi_sector, bio_end_sector(bio))) { 1440 1441 DEFINE_WAIT(w); 1442 if (bio->bi_opf & REQ_NOWAIT) { 1443 bio_wouldblock_error(bio); 1444 return; 1445 } 1446 for (;;) { 1447 prepare_to_wait(&conf->wait_barrier, 1448 &w, TASK_IDLE); 1449 if (!md_cluster_ops->area_resyncing(mddev, WRITE, 1450 bio->bi_iter.bi_sector, 1451 bio_end_sector(bio))) 1452 break; 1453 schedule(); 1454 } 1455 finish_wait(&conf->wait_barrier, &w); 1456 } 1457 1458 /* 1459 * Register the new request and wait if the reconstruction 1460 * thread has put up a bar for new requests. 1461 * Continue immediately if no resync is active currently. 1462 */ 1463 if (!wait_barrier(conf, bio->bi_iter.bi_sector, 1464 bio->bi_opf & REQ_NOWAIT)) { 1465 bio_wouldblock_error(bio); 1466 return; 1467 } 1468 1469 retry_write: 1470 r1_bio = alloc_r1bio(mddev, bio); 1471 r1_bio->sectors = max_write_sectors; 1472 1473 /* first select target devices under rcu_lock and 1474 * inc refcount on their rdev. Record them by setting 1475 * bios[x] to bio 1476 * If there are known/acknowledged bad blocks on any device on 1477 * which we have seen a write error, we want to avoid writing those 1478 * blocks. 1479 * This potentially requires several writes to write around 1480 * the bad blocks. Each set of writes gets it's own r1bio 1481 * with a set of bios attached. 1482 */ 1483 1484 disks = conf->raid_disks * 2; 1485 blocked_rdev = NULL; 1486 max_sectors = r1_bio->sectors; 1487 for (i = 0; i < disks; i++) { 1488 struct md_rdev *rdev = conf->mirrors[i].rdev; 1489 1490 /* 1491 * The write-behind io is only attempted on drives marked as 1492 * write-mostly, which means we could allocate write behind 1493 * bio later. 1494 */ 1495 if (!is_discard && rdev && test_bit(WriteMostly, &rdev->flags)) 1496 write_behind = true; 1497 1498 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { 1499 atomic_inc(&rdev->nr_pending); 1500 blocked_rdev = rdev; 1501 break; 1502 } 1503 r1_bio->bios[i] = NULL; 1504 if (!rdev || test_bit(Faulty, &rdev->flags)) { 1505 if (i < conf->raid_disks) 1506 set_bit(R1BIO_Degraded, &r1_bio->state); 1507 continue; 1508 } 1509 1510 atomic_inc(&rdev->nr_pending); 1511 if (test_bit(WriteErrorSeen, &rdev->flags)) { 1512 sector_t first_bad; 1513 int bad_sectors; 1514 int is_bad; 1515 1516 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors, 1517 &first_bad, &bad_sectors); 1518 if (is_bad < 0) { 1519 /* mustn't write here until the bad block is 1520 * acknowledged*/ 1521 set_bit(BlockedBadBlocks, &rdev->flags); 1522 blocked_rdev = rdev; 1523 break; 1524 } 1525 if (is_bad && first_bad <= r1_bio->sector) { 1526 /* Cannot write here at all */ 1527 bad_sectors -= (r1_bio->sector - first_bad); 1528 if (bad_sectors < max_sectors) 1529 /* mustn't write more than bad_sectors 1530 * to other devices yet 1531 */ 1532 max_sectors = bad_sectors; 1533 rdev_dec_pending(rdev, mddev); 1534 /* We don't set R1BIO_Degraded as that 1535 * only applies if the disk is 1536 * missing, so it might be re-added, 1537 * and we want to know to recover this 1538 * chunk. 1539 * In this case the device is here, 1540 * and the fact that this chunk is not 1541 * in-sync is recorded in the bad 1542 * block log 1543 */ 1544 continue; 1545 } 1546 if (is_bad) { 1547 int good_sectors = first_bad - r1_bio->sector; 1548 if (good_sectors < max_sectors) 1549 max_sectors = good_sectors; 1550 } 1551 } 1552 r1_bio->bios[i] = bio; 1553 } 1554 1555 if (unlikely(blocked_rdev)) { 1556 /* Wait for this device to become unblocked */ 1557 int j; 1558 1559 for (j = 0; j < i; j++) 1560 if (r1_bio->bios[j]) 1561 rdev_dec_pending(conf->mirrors[j].rdev, mddev); 1562 mempool_free(r1_bio, &conf->r1bio_pool); 1563 allow_barrier(conf, bio->bi_iter.bi_sector); 1564 1565 if (bio->bi_opf & REQ_NOWAIT) { 1566 bio_wouldblock_error(bio); 1567 return; 1568 } 1569 mddev_add_trace_msg(mddev, "raid1 wait rdev %d blocked", 1570 blocked_rdev->raid_disk); 1571 md_wait_for_blocked_rdev(blocked_rdev, mddev); 1572 wait_barrier(conf, bio->bi_iter.bi_sector, false); 1573 goto retry_write; 1574 } 1575 1576 /* 1577 * When using a bitmap, we may call alloc_behind_master_bio below. 1578 * alloc_behind_master_bio allocates a copy of the data payload a page 1579 * at a time and thus needs a new bio that can fit the whole payload 1580 * this bio in page sized chunks. 1581 */ 1582 if (write_behind && bitmap) 1583 max_sectors = min_t(int, max_sectors, 1584 BIO_MAX_VECS * (PAGE_SIZE >> 9)); 1585 if (max_sectors < bio_sectors(bio)) { 1586 struct bio *split = bio_split(bio, max_sectors, 1587 GFP_NOIO, &conf->bio_split); 1588 bio_chain(split, bio); 1589 submit_bio_noacct(bio); 1590 bio = split; 1591 r1_bio->master_bio = bio; 1592 r1_bio->sectors = max_sectors; 1593 } 1594 1595 md_account_bio(mddev, &bio); 1596 r1_bio->master_bio = bio; 1597 atomic_set(&r1_bio->remaining, 1); 1598 atomic_set(&r1_bio->behind_remaining, 0); 1599 1600 first_clone = 1; 1601 1602 for (i = 0; i < disks; i++) { 1603 struct bio *mbio = NULL; 1604 struct md_rdev *rdev = conf->mirrors[i].rdev; 1605 if (!r1_bio->bios[i]) 1606 continue; 1607 1608 if (first_clone) { 1609 /* do behind I/O ? 1610 * Not if there are too many, or cannot 1611 * allocate memory, or a reader on WriteMostly 1612 * is waiting for behind writes to flush */ 1613 if (bitmap && write_behind && 1614 (atomic_read(&bitmap->behind_writes) 1615 < mddev->bitmap_info.max_write_behind) && 1616 !waitqueue_active(&bitmap->behind_wait)) { 1617 alloc_behind_master_bio(r1_bio, bio); 1618 } 1619 1620 md_bitmap_startwrite(bitmap, r1_bio->sector, r1_bio->sectors, 1621 test_bit(R1BIO_BehindIO, &r1_bio->state)); 1622 first_clone = 0; 1623 } 1624 1625 if (r1_bio->behind_master_bio) { 1626 mbio = bio_alloc_clone(rdev->bdev, 1627 r1_bio->behind_master_bio, 1628 GFP_NOIO, &mddev->bio_set); 1629 if (test_bit(CollisionCheck, &rdev->flags)) 1630 wait_for_serialization(rdev, r1_bio); 1631 if (test_bit(WriteMostly, &rdev->flags)) 1632 atomic_inc(&r1_bio->behind_remaining); 1633 } else { 1634 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, 1635 &mddev->bio_set); 1636 1637 if (mddev->serialize_policy) 1638 wait_for_serialization(rdev, r1_bio); 1639 } 1640 1641 r1_bio->bios[i] = mbio; 1642 1643 mbio->bi_iter.bi_sector = (r1_bio->sector + rdev->data_offset); 1644 mbio->bi_end_io = raid1_end_write_request; 1645 mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA)); 1646 if (test_bit(FailFast, &rdev->flags) && 1647 !test_bit(WriteMostly, &rdev->flags) && 1648 conf->raid_disks - mddev->degraded > 1) 1649 mbio->bi_opf |= MD_FAILFAST; 1650 mbio->bi_private = r1_bio; 1651 1652 atomic_inc(&r1_bio->remaining); 1653 mddev_trace_remap(mddev, mbio, r1_bio->sector); 1654 /* flush_pending_writes() needs access to the rdev so...*/ 1655 mbio->bi_bdev = (void *)rdev; 1656 if (!raid1_add_bio_to_plug(mddev, mbio, raid1_unplug, disks)) { 1657 spin_lock_irqsave(&conf->device_lock, flags); 1658 bio_list_add(&conf->pending_bio_list, mbio); 1659 spin_unlock_irqrestore(&conf->device_lock, flags); 1660 md_wakeup_thread(mddev->thread); 1661 } 1662 } 1663 1664 r1_bio_write_done(r1_bio); 1665 1666 /* In case raid1d snuck in to freeze_array */ 1667 wake_up_barrier(conf); 1668 } 1669 1670 static bool raid1_make_request(struct mddev *mddev, struct bio *bio) 1671 { 1672 sector_t sectors; 1673 1674 if (unlikely(bio->bi_opf & REQ_PREFLUSH) 1675 && md_flush_request(mddev, bio)) 1676 return true; 1677 1678 /* 1679 * There is a limit to the maximum size, but 1680 * the read/write handler might find a lower limit 1681 * due to bad blocks. To avoid multiple splits, 1682 * we pass the maximum number of sectors down 1683 * and let the lower level perform the split. 1684 */ 1685 sectors = align_to_barrier_unit_end( 1686 bio->bi_iter.bi_sector, bio_sectors(bio)); 1687 1688 if (bio_data_dir(bio) == READ) 1689 raid1_read_request(mddev, bio, sectors, NULL); 1690 else { 1691 md_write_start(mddev,bio); 1692 raid1_write_request(mddev, bio, sectors); 1693 } 1694 return true; 1695 } 1696 1697 static void raid1_status(struct seq_file *seq, struct mddev *mddev) 1698 { 1699 struct r1conf *conf = mddev->private; 1700 int i; 1701 1702 lockdep_assert_held(&mddev->lock); 1703 1704 seq_printf(seq, " [%d/%d] [", conf->raid_disks, 1705 conf->raid_disks - mddev->degraded); 1706 for (i = 0; i < conf->raid_disks; i++) { 1707 struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev); 1708 1709 seq_printf(seq, "%s", 1710 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 1711 } 1712 seq_printf(seq, "]"); 1713 } 1714 1715 /** 1716 * raid1_error() - RAID1 error handler. 1717 * @mddev: affected md device. 1718 * @rdev: member device to fail. 1719 * 1720 * The routine acknowledges &rdev failure and determines new @mddev state. 1721 * If it failed, then: 1722 * - &MD_BROKEN flag is set in &mddev->flags. 1723 * - recovery is disabled. 1724 * Otherwise, it must be degraded: 1725 * - recovery is interrupted. 1726 * - &mddev->degraded is bumped. 1727 * 1728 * @rdev is marked as &Faulty excluding case when array is failed and 1729 * &mddev->fail_last_dev is off. 1730 */ 1731 static void raid1_error(struct mddev *mddev, struct md_rdev *rdev) 1732 { 1733 struct r1conf *conf = mddev->private; 1734 unsigned long flags; 1735 1736 spin_lock_irqsave(&conf->device_lock, flags); 1737 1738 if (test_bit(In_sync, &rdev->flags) && 1739 (conf->raid_disks - mddev->degraded) == 1) { 1740 set_bit(MD_BROKEN, &mddev->flags); 1741 1742 if (!mddev->fail_last_dev) { 1743 conf->recovery_disabled = mddev->recovery_disabled; 1744 spin_unlock_irqrestore(&conf->device_lock, flags); 1745 return; 1746 } 1747 } 1748 set_bit(Blocked, &rdev->flags); 1749 if (test_and_clear_bit(In_sync, &rdev->flags)) 1750 mddev->degraded++; 1751 set_bit(Faulty, &rdev->flags); 1752 spin_unlock_irqrestore(&conf->device_lock, flags); 1753 /* 1754 * if recovery is running, make sure it aborts. 1755 */ 1756 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 1757 set_mask_bits(&mddev->sb_flags, 0, 1758 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); 1759 pr_crit("md/raid1:%s: Disk failure on %pg, disabling device.\n" 1760 "md/raid1:%s: Operation continuing on %d devices.\n", 1761 mdname(mddev), rdev->bdev, 1762 mdname(mddev), conf->raid_disks - mddev->degraded); 1763 } 1764 1765 static void print_conf(struct r1conf *conf) 1766 { 1767 int i; 1768 1769 pr_debug("RAID1 conf printout:\n"); 1770 if (!conf) { 1771 pr_debug("(!conf)\n"); 1772 return; 1773 } 1774 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, 1775 conf->raid_disks); 1776 1777 lockdep_assert_held(&conf->mddev->reconfig_mutex); 1778 for (i = 0; i < conf->raid_disks; i++) { 1779 struct md_rdev *rdev = conf->mirrors[i].rdev; 1780 if (rdev) 1781 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n", 1782 i, !test_bit(In_sync, &rdev->flags), 1783 !test_bit(Faulty, &rdev->flags), 1784 rdev->bdev); 1785 } 1786 } 1787 1788 static void close_sync(struct r1conf *conf) 1789 { 1790 int idx; 1791 1792 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++) { 1793 _wait_barrier(conf, idx, false); 1794 _allow_barrier(conf, idx); 1795 } 1796 1797 mempool_exit(&conf->r1buf_pool); 1798 } 1799 1800 static int raid1_spare_active(struct mddev *mddev) 1801 { 1802 int i; 1803 struct r1conf *conf = mddev->private; 1804 int count = 0; 1805 unsigned long flags; 1806 1807 /* 1808 * Find all failed disks within the RAID1 configuration 1809 * and mark them readable. 1810 * Called under mddev lock, so rcu protection not needed. 1811 * device_lock used to avoid races with raid1_end_read_request 1812 * which expects 'In_sync' flags and ->degraded to be consistent. 1813 */ 1814 spin_lock_irqsave(&conf->device_lock, flags); 1815 for (i = 0; i < conf->raid_disks; i++) { 1816 struct md_rdev *rdev = conf->mirrors[i].rdev; 1817 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev; 1818 if (repl 1819 && !test_bit(Candidate, &repl->flags) 1820 && repl->recovery_offset == MaxSector 1821 && !test_bit(Faulty, &repl->flags) 1822 && !test_and_set_bit(In_sync, &repl->flags)) { 1823 /* replacement has just become active */ 1824 if (!rdev || 1825 !test_and_clear_bit(In_sync, &rdev->flags)) 1826 count++; 1827 if (rdev) { 1828 /* Replaced device not technically 1829 * faulty, but we need to be sure 1830 * it gets removed and never re-added 1831 */ 1832 set_bit(Faulty, &rdev->flags); 1833 sysfs_notify_dirent_safe( 1834 rdev->sysfs_state); 1835 } 1836 } 1837 if (rdev 1838 && rdev->recovery_offset == MaxSector 1839 && !test_bit(Faulty, &rdev->flags) 1840 && !test_and_set_bit(In_sync, &rdev->flags)) { 1841 count++; 1842 sysfs_notify_dirent_safe(rdev->sysfs_state); 1843 } 1844 } 1845 mddev->degraded -= count; 1846 spin_unlock_irqrestore(&conf->device_lock, flags); 1847 1848 print_conf(conf); 1849 return count; 1850 } 1851 1852 static bool raid1_add_conf(struct r1conf *conf, struct md_rdev *rdev, int disk, 1853 bool replacement) 1854 { 1855 struct raid1_info *info = conf->mirrors + disk; 1856 1857 if (replacement) 1858 info += conf->raid_disks; 1859 1860 if (info->rdev) 1861 return false; 1862 1863 if (bdev_nonrot(rdev->bdev)) { 1864 set_bit(Nonrot, &rdev->flags); 1865 WRITE_ONCE(conf->nonrot_disks, conf->nonrot_disks + 1); 1866 } 1867 1868 rdev->raid_disk = disk; 1869 info->head_position = 0; 1870 info->seq_start = MaxSector; 1871 WRITE_ONCE(info->rdev, rdev); 1872 1873 return true; 1874 } 1875 1876 static bool raid1_remove_conf(struct r1conf *conf, int disk) 1877 { 1878 struct raid1_info *info = conf->mirrors + disk; 1879 struct md_rdev *rdev = info->rdev; 1880 1881 if (!rdev || test_bit(In_sync, &rdev->flags) || 1882 atomic_read(&rdev->nr_pending)) 1883 return false; 1884 1885 /* Only remove non-faulty devices if recovery is not possible. */ 1886 if (!test_bit(Faulty, &rdev->flags) && 1887 rdev->mddev->recovery_disabled != conf->recovery_disabled && 1888 rdev->mddev->degraded < conf->raid_disks) 1889 return false; 1890 1891 if (test_and_clear_bit(Nonrot, &rdev->flags)) 1892 WRITE_ONCE(conf->nonrot_disks, conf->nonrot_disks - 1); 1893 1894 WRITE_ONCE(info->rdev, NULL); 1895 return true; 1896 } 1897 1898 static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev) 1899 { 1900 struct r1conf *conf = mddev->private; 1901 int err = -EEXIST; 1902 int mirror = 0, repl_slot = -1; 1903 struct raid1_info *p; 1904 int first = 0; 1905 int last = conf->raid_disks - 1; 1906 1907 if (mddev->recovery_disabled == conf->recovery_disabled) 1908 return -EBUSY; 1909 1910 if (rdev->raid_disk >= 0) 1911 first = last = rdev->raid_disk; 1912 1913 /* 1914 * find the disk ... but prefer rdev->saved_raid_disk 1915 * if possible. 1916 */ 1917 if (rdev->saved_raid_disk >= 0 && 1918 rdev->saved_raid_disk >= first && 1919 rdev->saved_raid_disk < conf->raid_disks && 1920 conf->mirrors[rdev->saved_raid_disk].rdev == NULL) 1921 first = last = rdev->saved_raid_disk; 1922 1923 for (mirror = first; mirror <= last; mirror++) { 1924 p = conf->mirrors + mirror; 1925 if (!p->rdev) { 1926 err = mddev_stack_new_rdev(mddev, rdev); 1927 if (err) 1928 return err; 1929 1930 raid1_add_conf(conf, rdev, mirror, false); 1931 /* As all devices are equivalent, we don't need a full recovery 1932 * if this was recently any drive of the array 1933 */ 1934 if (rdev->saved_raid_disk < 0) 1935 conf->fullsync = 1; 1936 break; 1937 } 1938 if (test_bit(WantReplacement, &p->rdev->flags) && 1939 p[conf->raid_disks].rdev == NULL && repl_slot < 0) 1940 repl_slot = mirror; 1941 } 1942 1943 if (err && repl_slot >= 0) { 1944 /* Add this device as a replacement */ 1945 clear_bit(In_sync, &rdev->flags); 1946 set_bit(Replacement, &rdev->flags); 1947 raid1_add_conf(conf, rdev, repl_slot, true); 1948 err = 0; 1949 conf->fullsync = 1; 1950 } 1951 1952 print_conf(conf); 1953 return err; 1954 } 1955 1956 static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 1957 { 1958 struct r1conf *conf = mddev->private; 1959 int err = 0; 1960 int number = rdev->raid_disk; 1961 struct raid1_info *p = conf->mirrors + number; 1962 1963 if (unlikely(number >= conf->raid_disks)) 1964 goto abort; 1965 1966 if (rdev != p->rdev) { 1967 number += conf->raid_disks; 1968 p = conf->mirrors + number; 1969 } 1970 1971 print_conf(conf); 1972 if (rdev == p->rdev) { 1973 if (!raid1_remove_conf(conf, number)) { 1974 err = -EBUSY; 1975 goto abort; 1976 } 1977 1978 if (number < conf->raid_disks && 1979 conf->mirrors[conf->raid_disks + number].rdev) { 1980 /* We just removed a device that is being replaced. 1981 * Move down the replacement. We drain all IO before 1982 * doing this to avoid confusion. 1983 */ 1984 struct md_rdev *repl = 1985 conf->mirrors[conf->raid_disks + number].rdev; 1986 freeze_array(conf, 0); 1987 if (atomic_read(&repl->nr_pending)) { 1988 /* It means that some queued IO of retry_list 1989 * hold repl. Thus, we cannot set replacement 1990 * as NULL, avoiding rdev NULL pointer 1991 * dereference in sync_request_write and 1992 * handle_write_finished. 1993 */ 1994 err = -EBUSY; 1995 unfreeze_array(conf); 1996 goto abort; 1997 } 1998 clear_bit(Replacement, &repl->flags); 1999 WRITE_ONCE(p->rdev, repl); 2000 conf->mirrors[conf->raid_disks + number].rdev = NULL; 2001 unfreeze_array(conf); 2002 } 2003 2004 clear_bit(WantReplacement, &rdev->flags); 2005 err = md_integrity_register(mddev); 2006 } 2007 abort: 2008 2009 print_conf(conf); 2010 return err; 2011 } 2012 2013 static void end_sync_read(struct bio *bio) 2014 { 2015 struct r1bio *r1_bio = get_resync_r1bio(bio); 2016 2017 update_head_pos(r1_bio->read_disk, r1_bio); 2018 2019 /* 2020 * we have read a block, now it needs to be re-written, 2021 * or re-read if the read failed. 2022 * We don't do much here, just schedule handling by raid1d 2023 */ 2024 if (!bio->bi_status) 2025 set_bit(R1BIO_Uptodate, &r1_bio->state); 2026 2027 if (atomic_dec_and_test(&r1_bio->remaining)) 2028 reschedule_retry(r1_bio); 2029 } 2030 2031 static void abort_sync_write(struct mddev *mddev, struct r1bio *r1_bio) 2032 { 2033 sector_t sync_blocks = 0; 2034 sector_t s = r1_bio->sector; 2035 long sectors_to_go = r1_bio->sectors; 2036 2037 /* make sure these bits don't get cleared. */ 2038 do { 2039 md_bitmap_end_sync(mddev->bitmap, s, &sync_blocks, 1); 2040 s += sync_blocks; 2041 sectors_to_go -= sync_blocks; 2042 } while (sectors_to_go > 0); 2043 } 2044 2045 static void put_sync_write_buf(struct r1bio *r1_bio, int uptodate) 2046 { 2047 if (atomic_dec_and_test(&r1_bio->remaining)) { 2048 struct mddev *mddev = r1_bio->mddev; 2049 int s = r1_bio->sectors; 2050 2051 if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 2052 test_bit(R1BIO_WriteError, &r1_bio->state)) 2053 reschedule_retry(r1_bio); 2054 else { 2055 put_buf(r1_bio); 2056 md_done_sync(mddev, s, uptodate); 2057 } 2058 } 2059 } 2060 2061 static void end_sync_write(struct bio *bio) 2062 { 2063 int uptodate = !bio->bi_status; 2064 struct r1bio *r1_bio = get_resync_r1bio(bio); 2065 struct mddev *mddev = r1_bio->mddev; 2066 struct r1conf *conf = mddev->private; 2067 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev; 2068 2069 if (!uptodate) { 2070 abort_sync_write(mddev, r1_bio); 2071 set_bit(WriteErrorSeen, &rdev->flags); 2072 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 2073 set_bit(MD_RECOVERY_NEEDED, & 2074 mddev->recovery); 2075 set_bit(R1BIO_WriteError, &r1_bio->state); 2076 } else if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors) && 2077 !rdev_has_badblock(conf->mirrors[r1_bio->read_disk].rdev, 2078 r1_bio->sector, r1_bio->sectors)) { 2079 set_bit(R1BIO_MadeGood, &r1_bio->state); 2080 } 2081 2082 put_sync_write_buf(r1_bio, uptodate); 2083 } 2084 2085 static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector, 2086 int sectors, struct page *page, blk_opf_t rw) 2087 { 2088 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) 2089 /* success */ 2090 return 1; 2091 if (rw == REQ_OP_WRITE) { 2092 set_bit(WriteErrorSeen, &rdev->flags); 2093 if (!test_and_set_bit(WantReplacement, 2094 &rdev->flags)) 2095 set_bit(MD_RECOVERY_NEEDED, & 2096 rdev->mddev->recovery); 2097 } 2098 /* need to record an error - either for the block or the device */ 2099 if (!rdev_set_badblocks(rdev, sector, sectors, 0)) 2100 md_error(rdev->mddev, rdev); 2101 return 0; 2102 } 2103 2104 static int fix_sync_read_error(struct r1bio *r1_bio) 2105 { 2106 /* Try some synchronous reads of other devices to get 2107 * good data, much like with normal read errors. Only 2108 * read into the pages we already have so we don't 2109 * need to re-issue the read request. 2110 * We don't need to freeze the array, because being in an 2111 * active sync request, there is no normal IO, and 2112 * no overlapping syncs. 2113 * We don't need to check is_badblock() again as we 2114 * made sure that anything with a bad block in range 2115 * will have bi_end_io clear. 2116 */ 2117 struct mddev *mddev = r1_bio->mddev; 2118 struct r1conf *conf = mddev->private; 2119 struct bio *bio = r1_bio->bios[r1_bio->read_disk]; 2120 struct page **pages = get_resync_pages(bio)->pages; 2121 sector_t sect = r1_bio->sector; 2122 int sectors = r1_bio->sectors; 2123 int idx = 0; 2124 struct md_rdev *rdev; 2125 2126 rdev = conf->mirrors[r1_bio->read_disk].rdev; 2127 if (test_bit(FailFast, &rdev->flags)) { 2128 /* Don't try recovering from here - just fail it 2129 * ... unless it is the last working device of course */ 2130 md_error(mddev, rdev); 2131 if (test_bit(Faulty, &rdev->flags)) 2132 /* Don't try to read from here, but make sure 2133 * put_buf does it's thing 2134 */ 2135 bio->bi_end_io = end_sync_write; 2136 } 2137 2138 while(sectors) { 2139 int s = sectors; 2140 int d = r1_bio->read_disk; 2141 int success = 0; 2142 int start; 2143 2144 if (s > (PAGE_SIZE>>9)) 2145 s = PAGE_SIZE >> 9; 2146 do { 2147 if (r1_bio->bios[d]->bi_end_io == end_sync_read) { 2148 /* No rcu protection needed here devices 2149 * can only be removed when no resync is 2150 * active, and resync is currently active 2151 */ 2152 rdev = conf->mirrors[d].rdev; 2153 if (sync_page_io(rdev, sect, s<<9, 2154 pages[idx], 2155 REQ_OP_READ, false)) { 2156 success = 1; 2157 break; 2158 } 2159 } 2160 d++; 2161 if (d == conf->raid_disks * 2) 2162 d = 0; 2163 } while (!success && d != r1_bio->read_disk); 2164 2165 if (!success) { 2166 int abort = 0; 2167 /* Cannot read from anywhere, this block is lost. 2168 * Record a bad block on each device. If that doesn't 2169 * work just disable and interrupt the recovery. 2170 * Don't fail devices as that won't really help. 2171 */ 2172 pr_crit_ratelimited("md/raid1:%s: %pg: unrecoverable I/O read error for block %llu\n", 2173 mdname(mddev), bio->bi_bdev, 2174 (unsigned long long)r1_bio->sector); 2175 for (d = 0; d < conf->raid_disks * 2; d++) { 2176 rdev = conf->mirrors[d].rdev; 2177 if (!rdev || test_bit(Faulty, &rdev->flags)) 2178 continue; 2179 if (!rdev_set_badblocks(rdev, sect, s, 0)) 2180 abort = 1; 2181 } 2182 if (abort) { 2183 conf->recovery_disabled = 2184 mddev->recovery_disabled; 2185 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 2186 md_done_sync(mddev, r1_bio->sectors, 0); 2187 put_buf(r1_bio); 2188 return 0; 2189 } 2190 /* Try next page */ 2191 sectors -= s; 2192 sect += s; 2193 idx++; 2194 continue; 2195 } 2196 2197 start = d; 2198 /* write it back and re-read */ 2199 while (d != r1_bio->read_disk) { 2200 if (d == 0) 2201 d = conf->raid_disks * 2; 2202 d--; 2203 if (r1_bio->bios[d]->bi_end_io != end_sync_read) 2204 continue; 2205 rdev = conf->mirrors[d].rdev; 2206 if (r1_sync_page_io(rdev, sect, s, 2207 pages[idx], 2208 REQ_OP_WRITE) == 0) { 2209 r1_bio->bios[d]->bi_end_io = NULL; 2210 rdev_dec_pending(rdev, mddev); 2211 } 2212 } 2213 d = start; 2214 while (d != r1_bio->read_disk) { 2215 if (d == 0) 2216 d = conf->raid_disks * 2; 2217 d--; 2218 if (r1_bio->bios[d]->bi_end_io != end_sync_read) 2219 continue; 2220 rdev = conf->mirrors[d].rdev; 2221 if (r1_sync_page_io(rdev, sect, s, 2222 pages[idx], 2223 REQ_OP_READ) != 0) 2224 atomic_add(s, &rdev->corrected_errors); 2225 } 2226 sectors -= s; 2227 sect += s; 2228 idx ++; 2229 } 2230 set_bit(R1BIO_Uptodate, &r1_bio->state); 2231 bio->bi_status = 0; 2232 return 1; 2233 } 2234 2235 static void process_checks(struct r1bio *r1_bio) 2236 { 2237 /* We have read all readable devices. If we haven't 2238 * got the block, then there is no hope left. 2239 * If we have, then we want to do a comparison 2240 * and skip the write if everything is the same. 2241 * If any blocks failed to read, then we need to 2242 * attempt an over-write 2243 */ 2244 struct mddev *mddev = r1_bio->mddev; 2245 struct r1conf *conf = mddev->private; 2246 int primary; 2247 int i; 2248 int vcnt; 2249 2250 /* Fix variable parts of all bios */ 2251 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9); 2252 for (i = 0; i < conf->raid_disks * 2; i++) { 2253 blk_status_t status; 2254 struct bio *b = r1_bio->bios[i]; 2255 struct resync_pages *rp = get_resync_pages(b); 2256 if (b->bi_end_io != end_sync_read) 2257 continue; 2258 /* fixup the bio for reuse, but preserve errno */ 2259 status = b->bi_status; 2260 bio_reset(b, conf->mirrors[i].rdev->bdev, REQ_OP_READ); 2261 b->bi_status = status; 2262 b->bi_iter.bi_sector = r1_bio->sector + 2263 conf->mirrors[i].rdev->data_offset; 2264 b->bi_end_io = end_sync_read; 2265 rp->raid_bio = r1_bio; 2266 b->bi_private = rp; 2267 2268 /* initialize bvec table again */ 2269 md_bio_reset_resync_pages(b, rp, r1_bio->sectors << 9); 2270 } 2271 for (primary = 0; primary < conf->raid_disks * 2; primary++) 2272 if (r1_bio->bios[primary]->bi_end_io == end_sync_read && 2273 !r1_bio->bios[primary]->bi_status) { 2274 r1_bio->bios[primary]->bi_end_io = NULL; 2275 rdev_dec_pending(conf->mirrors[primary].rdev, mddev); 2276 break; 2277 } 2278 r1_bio->read_disk = primary; 2279 for (i = 0; i < conf->raid_disks * 2; i++) { 2280 int j = 0; 2281 struct bio *pbio = r1_bio->bios[primary]; 2282 struct bio *sbio = r1_bio->bios[i]; 2283 blk_status_t status = sbio->bi_status; 2284 struct page **ppages = get_resync_pages(pbio)->pages; 2285 struct page **spages = get_resync_pages(sbio)->pages; 2286 struct bio_vec *bi; 2287 int page_len[RESYNC_PAGES] = { 0 }; 2288 struct bvec_iter_all iter_all; 2289 2290 if (sbio->bi_end_io != end_sync_read) 2291 continue; 2292 /* Now we can 'fixup' the error value */ 2293 sbio->bi_status = 0; 2294 2295 bio_for_each_segment_all(bi, sbio, iter_all) 2296 page_len[j++] = bi->bv_len; 2297 2298 if (!status) { 2299 for (j = vcnt; j-- ; ) { 2300 if (memcmp(page_address(ppages[j]), 2301 page_address(spages[j]), 2302 page_len[j])) 2303 break; 2304 } 2305 } else 2306 j = 0; 2307 if (j >= 0) 2308 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches); 2309 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery) 2310 && !status)) { 2311 /* No need to write to this device. */ 2312 sbio->bi_end_io = NULL; 2313 rdev_dec_pending(conf->mirrors[i].rdev, mddev); 2314 continue; 2315 } 2316 2317 bio_copy_data(sbio, pbio); 2318 } 2319 } 2320 2321 static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio) 2322 { 2323 struct r1conf *conf = mddev->private; 2324 int i; 2325 int disks = conf->raid_disks * 2; 2326 struct bio *wbio; 2327 2328 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) 2329 /* ouch - failed to read all of that. */ 2330 if (!fix_sync_read_error(r1_bio)) 2331 return; 2332 2333 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 2334 process_checks(r1_bio); 2335 2336 /* 2337 * schedule writes 2338 */ 2339 atomic_set(&r1_bio->remaining, 1); 2340 for (i = 0; i < disks ; i++) { 2341 wbio = r1_bio->bios[i]; 2342 if (wbio->bi_end_io == NULL || 2343 (wbio->bi_end_io == end_sync_read && 2344 (i == r1_bio->read_disk || 2345 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)))) 2346 continue; 2347 if (test_bit(Faulty, &conf->mirrors[i].rdev->flags)) { 2348 abort_sync_write(mddev, r1_bio); 2349 continue; 2350 } 2351 2352 wbio->bi_opf = REQ_OP_WRITE; 2353 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags)) 2354 wbio->bi_opf |= MD_FAILFAST; 2355 2356 wbio->bi_end_io = end_sync_write; 2357 atomic_inc(&r1_bio->remaining); 2358 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio)); 2359 2360 submit_bio_noacct(wbio); 2361 } 2362 2363 put_sync_write_buf(r1_bio, 1); 2364 } 2365 2366 /* 2367 * This is a kernel thread which: 2368 * 2369 * 1. Retries failed read operations on working mirrors. 2370 * 2. Updates the raid superblock when problems encounter. 2371 * 3. Performs writes following reads for array synchronising. 2372 */ 2373 2374 static void fix_read_error(struct r1conf *conf, struct r1bio *r1_bio) 2375 { 2376 sector_t sect = r1_bio->sector; 2377 int sectors = r1_bio->sectors; 2378 int read_disk = r1_bio->read_disk; 2379 struct mddev *mddev = conf->mddev; 2380 struct md_rdev *rdev = conf->mirrors[read_disk].rdev; 2381 2382 if (exceed_read_errors(mddev, rdev)) { 2383 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED; 2384 return; 2385 } 2386 2387 while(sectors) { 2388 int s = sectors; 2389 int d = read_disk; 2390 int success = 0; 2391 int start; 2392 2393 if (s > (PAGE_SIZE>>9)) 2394 s = PAGE_SIZE >> 9; 2395 2396 do { 2397 rdev = conf->mirrors[d].rdev; 2398 if (rdev && 2399 (test_bit(In_sync, &rdev->flags) || 2400 (!test_bit(Faulty, &rdev->flags) && 2401 rdev->recovery_offset >= sect + s)) && 2402 rdev_has_badblock(rdev, sect, s) == 0) { 2403 atomic_inc(&rdev->nr_pending); 2404 if (sync_page_io(rdev, sect, s<<9, 2405 conf->tmppage, REQ_OP_READ, false)) 2406 success = 1; 2407 rdev_dec_pending(rdev, mddev); 2408 if (success) 2409 break; 2410 } 2411 2412 d++; 2413 if (d == conf->raid_disks * 2) 2414 d = 0; 2415 } while (d != read_disk); 2416 2417 if (!success) { 2418 /* Cannot read from anywhere - mark it bad */ 2419 struct md_rdev *rdev = conf->mirrors[read_disk].rdev; 2420 if (!rdev_set_badblocks(rdev, sect, s, 0)) 2421 md_error(mddev, rdev); 2422 break; 2423 } 2424 /* write it back and re-read */ 2425 start = d; 2426 while (d != read_disk) { 2427 if (d==0) 2428 d = conf->raid_disks * 2; 2429 d--; 2430 rdev = conf->mirrors[d].rdev; 2431 if (rdev && 2432 !test_bit(Faulty, &rdev->flags)) { 2433 atomic_inc(&rdev->nr_pending); 2434 r1_sync_page_io(rdev, sect, s, 2435 conf->tmppage, REQ_OP_WRITE); 2436 rdev_dec_pending(rdev, mddev); 2437 } 2438 } 2439 d = start; 2440 while (d != read_disk) { 2441 if (d==0) 2442 d = conf->raid_disks * 2; 2443 d--; 2444 rdev = conf->mirrors[d].rdev; 2445 if (rdev && 2446 !test_bit(Faulty, &rdev->flags)) { 2447 atomic_inc(&rdev->nr_pending); 2448 if (r1_sync_page_io(rdev, sect, s, 2449 conf->tmppage, REQ_OP_READ)) { 2450 atomic_add(s, &rdev->corrected_errors); 2451 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %pg)\n", 2452 mdname(mddev), s, 2453 (unsigned long long)(sect + 2454 rdev->data_offset), 2455 rdev->bdev); 2456 } 2457 rdev_dec_pending(rdev, mddev); 2458 } 2459 } 2460 sectors -= s; 2461 sect += s; 2462 } 2463 } 2464 2465 static int narrow_write_error(struct r1bio *r1_bio, int i) 2466 { 2467 struct mddev *mddev = r1_bio->mddev; 2468 struct r1conf *conf = mddev->private; 2469 struct md_rdev *rdev = conf->mirrors[i].rdev; 2470 2471 /* bio has the data to be written to device 'i' where 2472 * we just recently had a write error. 2473 * We repeatedly clone the bio and trim down to one block, 2474 * then try the write. Where the write fails we record 2475 * a bad block. 2476 * It is conceivable that the bio doesn't exactly align with 2477 * blocks. We must handle this somehow. 2478 * 2479 * We currently own a reference on the rdev. 2480 */ 2481 2482 int block_sectors; 2483 sector_t sector; 2484 int sectors; 2485 int sect_to_write = r1_bio->sectors; 2486 int ok = 1; 2487 2488 if (rdev->badblocks.shift < 0) 2489 return 0; 2490 2491 block_sectors = roundup(1 << rdev->badblocks.shift, 2492 bdev_logical_block_size(rdev->bdev) >> 9); 2493 sector = r1_bio->sector; 2494 sectors = ((sector + block_sectors) 2495 & ~(sector_t)(block_sectors - 1)) 2496 - sector; 2497 2498 while (sect_to_write) { 2499 struct bio *wbio; 2500 if (sectors > sect_to_write) 2501 sectors = sect_to_write; 2502 /* Write at 'sector' for 'sectors'*/ 2503 2504 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { 2505 wbio = bio_alloc_clone(rdev->bdev, 2506 r1_bio->behind_master_bio, 2507 GFP_NOIO, &mddev->bio_set); 2508 } else { 2509 wbio = bio_alloc_clone(rdev->bdev, r1_bio->master_bio, 2510 GFP_NOIO, &mddev->bio_set); 2511 } 2512 2513 wbio->bi_opf = REQ_OP_WRITE; 2514 wbio->bi_iter.bi_sector = r1_bio->sector; 2515 wbio->bi_iter.bi_size = r1_bio->sectors << 9; 2516 2517 bio_trim(wbio, sector - r1_bio->sector, sectors); 2518 wbio->bi_iter.bi_sector += rdev->data_offset; 2519 2520 if (submit_bio_wait(wbio) < 0) 2521 /* failure! */ 2522 ok = rdev_set_badblocks(rdev, sector, 2523 sectors, 0) 2524 && ok; 2525 2526 bio_put(wbio); 2527 sect_to_write -= sectors; 2528 sector += sectors; 2529 sectors = block_sectors; 2530 } 2531 return ok; 2532 } 2533 2534 static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio) 2535 { 2536 int m; 2537 int s = r1_bio->sectors; 2538 for (m = 0; m < conf->raid_disks * 2 ; m++) { 2539 struct md_rdev *rdev = conf->mirrors[m].rdev; 2540 struct bio *bio = r1_bio->bios[m]; 2541 if (bio->bi_end_io == NULL) 2542 continue; 2543 if (!bio->bi_status && 2544 test_bit(R1BIO_MadeGood, &r1_bio->state)) { 2545 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0); 2546 } 2547 if (bio->bi_status && 2548 test_bit(R1BIO_WriteError, &r1_bio->state)) { 2549 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0)) 2550 md_error(conf->mddev, rdev); 2551 } 2552 } 2553 put_buf(r1_bio); 2554 md_done_sync(conf->mddev, s, 1); 2555 } 2556 2557 static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio) 2558 { 2559 int m, idx; 2560 bool fail = false; 2561 2562 for (m = 0; m < conf->raid_disks * 2 ; m++) 2563 if (r1_bio->bios[m] == IO_MADE_GOOD) { 2564 struct md_rdev *rdev = conf->mirrors[m].rdev; 2565 rdev_clear_badblocks(rdev, 2566 r1_bio->sector, 2567 r1_bio->sectors, 0); 2568 rdev_dec_pending(rdev, conf->mddev); 2569 } else if (r1_bio->bios[m] != NULL) { 2570 /* This drive got a write error. We need to 2571 * narrow down and record precise write 2572 * errors. 2573 */ 2574 fail = true; 2575 if (!narrow_write_error(r1_bio, m)) { 2576 md_error(conf->mddev, 2577 conf->mirrors[m].rdev); 2578 /* an I/O failed, we can't clear the bitmap */ 2579 set_bit(R1BIO_Degraded, &r1_bio->state); 2580 } 2581 rdev_dec_pending(conf->mirrors[m].rdev, 2582 conf->mddev); 2583 } 2584 if (fail) { 2585 spin_lock_irq(&conf->device_lock); 2586 list_add(&r1_bio->retry_list, &conf->bio_end_io_list); 2587 idx = sector_to_idx(r1_bio->sector); 2588 atomic_inc(&conf->nr_queued[idx]); 2589 spin_unlock_irq(&conf->device_lock); 2590 /* 2591 * In case freeze_array() is waiting for condition 2592 * get_unqueued_pending() == extra to be true. 2593 */ 2594 wake_up(&conf->wait_barrier); 2595 md_wakeup_thread(conf->mddev->thread); 2596 } else { 2597 if (test_bit(R1BIO_WriteError, &r1_bio->state)) 2598 close_write(r1_bio); 2599 raid_end_bio_io(r1_bio); 2600 } 2601 } 2602 2603 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio) 2604 { 2605 struct mddev *mddev = conf->mddev; 2606 struct bio *bio; 2607 struct md_rdev *rdev; 2608 sector_t sector; 2609 2610 clear_bit(R1BIO_ReadError, &r1_bio->state); 2611 /* we got a read error. Maybe the drive is bad. Maybe just 2612 * the block and we can fix it. 2613 * We freeze all other IO, and try reading the block from 2614 * other devices. When we find one, we re-write 2615 * and check it that fixes the read error. 2616 * This is all done synchronously while the array is 2617 * frozen 2618 */ 2619 2620 bio = r1_bio->bios[r1_bio->read_disk]; 2621 bio_put(bio); 2622 r1_bio->bios[r1_bio->read_disk] = NULL; 2623 2624 rdev = conf->mirrors[r1_bio->read_disk].rdev; 2625 if (mddev->ro == 0 2626 && !test_bit(FailFast, &rdev->flags)) { 2627 freeze_array(conf, 1); 2628 fix_read_error(conf, r1_bio); 2629 unfreeze_array(conf); 2630 } else if (mddev->ro == 0 && test_bit(FailFast, &rdev->flags)) { 2631 md_error(mddev, rdev); 2632 } else { 2633 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED; 2634 } 2635 2636 rdev_dec_pending(rdev, conf->mddev); 2637 sector = r1_bio->sector; 2638 bio = r1_bio->master_bio; 2639 2640 /* Reuse the old r1_bio so that the IO_BLOCKED settings are preserved */ 2641 r1_bio->state = 0; 2642 raid1_read_request(mddev, bio, r1_bio->sectors, r1_bio); 2643 allow_barrier(conf, sector); 2644 } 2645 2646 static void raid1d(struct md_thread *thread) 2647 { 2648 struct mddev *mddev = thread->mddev; 2649 struct r1bio *r1_bio; 2650 unsigned long flags; 2651 struct r1conf *conf = mddev->private; 2652 struct list_head *head = &conf->retry_list; 2653 struct blk_plug plug; 2654 int idx; 2655 2656 md_check_recovery(mddev); 2657 2658 if (!list_empty_careful(&conf->bio_end_io_list) && 2659 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { 2660 LIST_HEAD(tmp); 2661 spin_lock_irqsave(&conf->device_lock, flags); 2662 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) 2663 list_splice_init(&conf->bio_end_io_list, &tmp); 2664 spin_unlock_irqrestore(&conf->device_lock, flags); 2665 while (!list_empty(&tmp)) { 2666 r1_bio = list_first_entry(&tmp, struct r1bio, 2667 retry_list); 2668 list_del(&r1_bio->retry_list); 2669 idx = sector_to_idx(r1_bio->sector); 2670 atomic_dec(&conf->nr_queued[idx]); 2671 if (mddev->degraded) 2672 set_bit(R1BIO_Degraded, &r1_bio->state); 2673 if (test_bit(R1BIO_WriteError, &r1_bio->state)) 2674 close_write(r1_bio); 2675 raid_end_bio_io(r1_bio); 2676 } 2677 } 2678 2679 blk_start_plug(&plug); 2680 for (;;) { 2681 2682 flush_pending_writes(conf); 2683 2684 spin_lock_irqsave(&conf->device_lock, flags); 2685 if (list_empty(head)) { 2686 spin_unlock_irqrestore(&conf->device_lock, flags); 2687 break; 2688 } 2689 r1_bio = list_entry(head->prev, struct r1bio, retry_list); 2690 list_del(head->prev); 2691 idx = sector_to_idx(r1_bio->sector); 2692 atomic_dec(&conf->nr_queued[idx]); 2693 spin_unlock_irqrestore(&conf->device_lock, flags); 2694 2695 mddev = r1_bio->mddev; 2696 conf = mddev->private; 2697 if (test_bit(R1BIO_IsSync, &r1_bio->state)) { 2698 if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 2699 test_bit(R1BIO_WriteError, &r1_bio->state)) 2700 handle_sync_write_finished(conf, r1_bio); 2701 else 2702 sync_request_write(mddev, r1_bio); 2703 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 2704 test_bit(R1BIO_WriteError, &r1_bio->state)) 2705 handle_write_finished(conf, r1_bio); 2706 else if (test_bit(R1BIO_ReadError, &r1_bio->state)) 2707 handle_read_error(conf, r1_bio); 2708 else 2709 WARN_ON_ONCE(1); 2710 2711 cond_resched(); 2712 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING)) 2713 md_check_recovery(mddev); 2714 } 2715 blk_finish_plug(&plug); 2716 } 2717 2718 static int init_resync(struct r1conf *conf) 2719 { 2720 int buffs; 2721 2722 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; 2723 BUG_ON(mempool_initialized(&conf->r1buf_pool)); 2724 2725 return mempool_init(&conf->r1buf_pool, buffs, r1buf_pool_alloc, 2726 r1buf_pool_free, conf->poolinfo); 2727 } 2728 2729 static struct r1bio *raid1_alloc_init_r1buf(struct r1conf *conf) 2730 { 2731 struct r1bio *r1bio = mempool_alloc(&conf->r1buf_pool, GFP_NOIO); 2732 struct resync_pages *rps; 2733 struct bio *bio; 2734 int i; 2735 2736 for (i = conf->poolinfo->raid_disks; i--; ) { 2737 bio = r1bio->bios[i]; 2738 rps = bio->bi_private; 2739 bio_reset(bio, NULL, 0); 2740 bio->bi_private = rps; 2741 } 2742 r1bio->master_bio = NULL; 2743 return r1bio; 2744 } 2745 2746 /* 2747 * perform a "sync" on one "block" 2748 * 2749 * We need to make sure that no normal I/O request - particularly write 2750 * requests - conflict with active sync requests. 2751 * 2752 * This is achieved by tracking pending requests and a 'barrier' concept 2753 * that can be installed to exclude normal IO requests. 2754 */ 2755 2756 static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr, 2757 sector_t max_sector, int *skipped) 2758 { 2759 struct r1conf *conf = mddev->private; 2760 struct r1bio *r1_bio; 2761 struct bio *bio; 2762 sector_t nr_sectors; 2763 int disk = -1; 2764 int i; 2765 int wonly = -1; 2766 int write_targets = 0, read_targets = 0; 2767 sector_t sync_blocks; 2768 int still_degraded = 0; 2769 int good_sectors = RESYNC_SECTORS; 2770 int min_bad = 0; /* number of sectors that are bad in all devices */ 2771 int idx = sector_to_idx(sector_nr); 2772 int page_idx = 0; 2773 2774 if (!mempool_initialized(&conf->r1buf_pool)) 2775 if (init_resync(conf)) 2776 return 0; 2777 2778 if (sector_nr >= max_sector) { 2779 /* If we aborted, we need to abort the 2780 * sync on the 'current' bitmap chunk (there will 2781 * only be one in raid1 resync. 2782 * We can find the current addess in mddev->curr_resync 2783 */ 2784 if (mddev->curr_resync < max_sector) /* aborted */ 2785 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync, 2786 &sync_blocks, 1); 2787 else /* completed sync */ 2788 conf->fullsync = 0; 2789 2790 md_bitmap_close_sync(mddev->bitmap); 2791 close_sync(conf); 2792 2793 if (mddev_is_clustered(mddev)) { 2794 conf->cluster_sync_low = 0; 2795 conf->cluster_sync_high = 0; 2796 } 2797 return 0; 2798 } 2799 2800 if (mddev->bitmap == NULL && 2801 mddev->recovery_cp == MaxSector && 2802 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 2803 conf->fullsync == 0) { 2804 *skipped = 1; 2805 return max_sector - sector_nr; 2806 } 2807 /* before building a request, check if we can skip these blocks.. 2808 * This call the bitmap_start_sync doesn't actually record anything 2809 */ 2810 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && 2811 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 2812 /* We can skip this block, and probably several more */ 2813 *skipped = 1; 2814 return sync_blocks; 2815 } 2816 2817 /* 2818 * If there is non-resync activity waiting for a turn, then let it 2819 * though before starting on this new sync request. 2820 */ 2821 if (atomic_read(&conf->nr_waiting[idx])) 2822 schedule_timeout_uninterruptible(1); 2823 2824 /* we are incrementing sector_nr below. To be safe, we check against 2825 * sector_nr + two times RESYNC_SECTORS 2826 */ 2827 2828 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, 2829 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high)); 2830 2831 2832 if (raise_barrier(conf, sector_nr)) 2833 return 0; 2834 2835 r1_bio = raid1_alloc_init_r1buf(conf); 2836 2837 /* 2838 * If we get a correctably read error during resync or recovery, 2839 * we might want to read from a different device. So we 2840 * flag all drives that could conceivably be read from for READ, 2841 * and any others (which will be non-In_sync devices) for WRITE. 2842 * If a read fails, we try reading from something else for which READ 2843 * is OK. 2844 */ 2845 2846 r1_bio->mddev = mddev; 2847 r1_bio->sector = sector_nr; 2848 r1_bio->state = 0; 2849 set_bit(R1BIO_IsSync, &r1_bio->state); 2850 /* make sure good_sectors won't go across barrier unit boundary */ 2851 good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors); 2852 2853 for (i = 0; i < conf->raid_disks * 2; i++) { 2854 struct md_rdev *rdev; 2855 bio = r1_bio->bios[i]; 2856 2857 rdev = conf->mirrors[i].rdev; 2858 if (rdev == NULL || 2859 test_bit(Faulty, &rdev->flags)) { 2860 if (i < conf->raid_disks) 2861 still_degraded = 1; 2862 } else if (!test_bit(In_sync, &rdev->flags)) { 2863 bio->bi_opf = REQ_OP_WRITE; 2864 bio->bi_end_io = end_sync_write; 2865 write_targets ++; 2866 } else { 2867 /* may need to read from here */ 2868 sector_t first_bad = MaxSector; 2869 int bad_sectors; 2870 2871 if (is_badblock(rdev, sector_nr, good_sectors, 2872 &first_bad, &bad_sectors)) { 2873 if (first_bad > sector_nr) 2874 good_sectors = first_bad - sector_nr; 2875 else { 2876 bad_sectors -= (sector_nr - first_bad); 2877 if (min_bad == 0 || 2878 min_bad > bad_sectors) 2879 min_bad = bad_sectors; 2880 } 2881 } 2882 if (sector_nr < first_bad) { 2883 if (test_bit(WriteMostly, &rdev->flags)) { 2884 if (wonly < 0) 2885 wonly = i; 2886 } else { 2887 if (disk < 0) 2888 disk = i; 2889 } 2890 bio->bi_opf = REQ_OP_READ; 2891 bio->bi_end_io = end_sync_read; 2892 read_targets++; 2893 } else if (!test_bit(WriteErrorSeen, &rdev->flags) && 2894 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && 2895 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { 2896 /* 2897 * The device is suitable for reading (InSync), 2898 * but has bad block(s) here. Let's try to correct them, 2899 * if we are doing resync or repair. Otherwise, leave 2900 * this device alone for this sync request. 2901 */ 2902 bio->bi_opf = REQ_OP_WRITE; 2903 bio->bi_end_io = end_sync_write; 2904 write_targets++; 2905 } 2906 } 2907 if (rdev && bio->bi_end_io) { 2908 atomic_inc(&rdev->nr_pending); 2909 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset; 2910 bio_set_dev(bio, rdev->bdev); 2911 if (test_bit(FailFast, &rdev->flags)) 2912 bio->bi_opf |= MD_FAILFAST; 2913 } 2914 } 2915 if (disk < 0) 2916 disk = wonly; 2917 r1_bio->read_disk = disk; 2918 2919 if (read_targets == 0 && min_bad > 0) { 2920 /* These sectors are bad on all InSync devices, so we 2921 * need to mark them bad on all write targets 2922 */ 2923 int ok = 1; 2924 for (i = 0 ; i < conf->raid_disks * 2 ; i++) 2925 if (r1_bio->bios[i]->bi_end_io == end_sync_write) { 2926 struct md_rdev *rdev = conf->mirrors[i].rdev; 2927 ok = rdev_set_badblocks(rdev, sector_nr, 2928 min_bad, 0 2929 ) && ok; 2930 } 2931 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 2932 *skipped = 1; 2933 put_buf(r1_bio); 2934 2935 if (!ok) { 2936 /* Cannot record the badblocks, so need to 2937 * abort the resync. 2938 * If there are multiple read targets, could just 2939 * fail the really bad ones ??? 2940 */ 2941 conf->recovery_disabled = mddev->recovery_disabled; 2942 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 2943 return 0; 2944 } else 2945 return min_bad; 2946 2947 } 2948 if (min_bad > 0 && min_bad < good_sectors) { 2949 /* only resync enough to reach the next bad->good 2950 * transition */ 2951 good_sectors = min_bad; 2952 } 2953 2954 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0) 2955 /* extra read targets are also write targets */ 2956 write_targets += read_targets-1; 2957 2958 if (write_targets == 0 || read_targets == 0) { 2959 /* There is nowhere to write, so all non-sync 2960 * drives must be failed - so we are finished 2961 */ 2962 sector_t rv; 2963 if (min_bad > 0) 2964 max_sector = sector_nr + min_bad; 2965 rv = max_sector - sector_nr; 2966 *skipped = 1; 2967 put_buf(r1_bio); 2968 return rv; 2969 } 2970 2971 if (max_sector > mddev->resync_max) 2972 max_sector = mddev->resync_max; /* Don't do IO beyond here */ 2973 if (max_sector > sector_nr + good_sectors) 2974 max_sector = sector_nr + good_sectors; 2975 nr_sectors = 0; 2976 sync_blocks = 0; 2977 do { 2978 struct page *page; 2979 int len = PAGE_SIZE; 2980 if (sector_nr + (len>>9) > max_sector) 2981 len = (max_sector - sector_nr) << 9; 2982 if (len == 0) 2983 break; 2984 if (sync_blocks == 0) { 2985 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr, 2986 &sync_blocks, still_degraded) && 2987 !conf->fullsync && 2988 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 2989 break; 2990 if ((len >> 9) > sync_blocks) 2991 len = sync_blocks<<9; 2992 } 2993 2994 for (i = 0 ; i < conf->raid_disks * 2; i++) { 2995 struct resync_pages *rp; 2996 2997 bio = r1_bio->bios[i]; 2998 rp = get_resync_pages(bio); 2999 if (bio->bi_end_io) { 3000 page = resync_fetch_page(rp, page_idx); 3001 3002 /* 3003 * won't fail because the vec table is big 3004 * enough to hold all these pages 3005 */ 3006 __bio_add_page(bio, page, len, 0); 3007 } 3008 } 3009 nr_sectors += len>>9; 3010 sector_nr += len>>9; 3011 sync_blocks -= (len>>9); 3012 } while (++page_idx < RESYNC_PAGES); 3013 3014 r1_bio->sectors = nr_sectors; 3015 3016 if (mddev_is_clustered(mddev) && 3017 conf->cluster_sync_high < sector_nr + nr_sectors) { 3018 conf->cluster_sync_low = mddev->curr_resync_completed; 3019 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS; 3020 /* Send resync message */ 3021 md_cluster_ops->resync_info_update(mddev, 3022 conf->cluster_sync_low, 3023 conf->cluster_sync_high); 3024 } 3025 3026 /* For a user-requested sync, we read all readable devices and do a 3027 * compare 3028 */ 3029 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 3030 atomic_set(&r1_bio->remaining, read_targets); 3031 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) { 3032 bio = r1_bio->bios[i]; 3033 if (bio->bi_end_io == end_sync_read) { 3034 read_targets--; 3035 md_sync_acct_bio(bio, nr_sectors); 3036 if (read_targets == 1) 3037 bio->bi_opf &= ~MD_FAILFAST; 3038 submit_bio_noacct(bio); 3039 } 3040 } 3041 } else { 3042 atomic_set(&r1_bio->remaining, 1); 3043 bio = r1_bio->bios[r1_bio->read_disk]; 3044 md_sync_acct_bio(bio, nr_sectors); 3045 if (read_targets == 1) 3046 bio->bi_opf &= ~MD_FAILFAST; 3047 submit_bio_noacct(bio); 3048 } 3049 return nr_sectors; 3050 } 3051 3052 static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks) 3053 { 3054 if (sectors) 3055 return sectors; 3056 3057 return mddev->dev_sectors; 3058 } 3059 3060 static struct r1conf *setup_conf(struct mddev *mddev) 3061 { 3062 struct r1conf *conf; 3063 int i; 3064 struct raid1_info *disk; 3065 struct md_rdev *rdev; 3066 int err = -ENOMEM; 3067 3068 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL); 3069 if (!conf) 3070 goto abort; 3071 3072 conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR, 3073 sizeof(atomic_t), GFP_KERNEL); 3074 if (!conf->nr_pending) 3075 goto abort; 3076 3077 conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR, 3078 sizeof(atomic_t), GFP_KERNEL); 3079 if (!conf->nr_waiting) 3080 goto abort; 3081 3082 conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR, 3083 sizeof(atomic_t), GFP_KERNEL); 3084 if (!conf->nr_queued) 3085 goto abort; 3086 3087 conf->barrier = kcalloc(BARRIER_BUCKETS_NR, 3088 sizeof(atomic_t), GFP_KERNEL); 3089 if (!conf->barrier) 3090 goto abort; 3091 3092 conf->mirrors = kzalloc(array3_size(sizeof(struct raid1_info), 3093 mddev->raid_disks, 2), 3094 GFP_KERNEL); 3095 if (!conf->mirrors) 3096 goto abort; 3097 3098 conf->tmppage = alloc_page(GFP_KERNEL); 3099 if (!conf->tmppage) 3100 goto abort; 3101 3102 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL); 3103 if (!conf->poolinfo) 3104 goto abort; 3105 conf->poolinfo->raid_disks = mddev->raid_disks * 2; 3106 err = mempool_init(&conf->r1bio_pool, NR_RAID_BIOS, r1bio_pool_alloc, 3107 rbio_pool_free, conf->poolinfo); 3108 if (err) 3109 goto abort; 3110 3111 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); 3112 if (err) 3113 goto abort; 3114 3115 conf->poolinfo->mddev = mddev; 3116 3117 err = -EINVAL; 3118 spin_lock_init(&conf->device_lock); 3119 conf->raid_disks = mddev->raid_disks; 3120 rdev_for_each(rdev, mddev) { 3121 int disk_idx = rdev->raid_disk; 3122 3123 if (disk_idx >= conf->raid_disks || disk_idx < 0) 3124 continue; 3125 3126 if (!raid1_add_conf(conf, rdev, disk_idx, 3127 test_bit(Replacement, &rdev->flags))) 3128 goto abort; 3129 } 3130 conf->mddev = mddev; 3131 INIT_LIST_HEAD(&conf->retry_list); 3132 INIT_LIST_HEAD(&conf->bio_end_io_list); 3133 3134 spin_lock_init(&conf->resync_lock); 3135 init_waitqueue_head(&conf->wait_barrier); 3136 3137 bio_list_init(&conf->pending_bio_list); 3138 conf->recovery_disabled = mddev->recovery_disabled - 1; 3139 3140 err = -EIO; 3141 for (i = 0; i < conf->raid_disks * 2; i++) { 3142 3143 disk = conf->mirrors + i; 3144 3145 if (i < conf->raid_disks && 3146 disk[conf->raid_disks].rdev) { 3147 /* This slot has a replacement. */ 3148 if (!disk->rdev) { 3149 /* No original, just make the replacement 3150 * a recovering spare 3151 */ 3152 disk->rdev = 3153 disk[conf->raid_disks].rdev; 3154 disk[conf->raid_disks].rdev = NULL; 3155 } else if (!test_bit(In_sync, &disk->rdev->flags)) 3156 /* Original is not in_sync - bad */ 3157 goto abort; 3158 } 3159 3160 if (!disk->rdev || 3161 !test_bit(In_sync, &disk->rdev->flags)) { 3162 disk->head_position = 0; 3163 if (disk->rdev && 3164 (disk->rdev->saved_raid_disk < 0)) 3165 conf->fullsync = 1; 3166 } 3167 } 3168 3169 err = -ENOMEM; 3170 rcu_assign_pointer(conf->thread, 3171 md_register_thread(raid1d, mddev, "raid1")); 3172 if (!conf->thread) 3173 goto abort; 3174 3175 return conf; 3176 3177 abort: 3178 if (conf) { 3179 mempool_exit(&conf->r1bio_pool); 3180 kfree(conf->mirrors); 3181 safe_put_page(conf->tmppage); 3182 kfree(conf->poolinfo); 3183 kfree(conf->nr_pending); 3184 kfree(conf->nr_waiting); 3185 kfree(conf->nr_queued); 3186 kfree(conf->barrier); 3187 bioset_exit(&conf->bio_split); 3188 kfree(conf); 3189 } 3190 return ERR_PTR(err); 3191 } 3192 3193 static int raid1_set_limits(struct mddev *mddev) 3194 { 3195 struct queue_limits lim; 3196 int err; 3197 3198 md_init_stacking_limits(&lim); 3199 lim.max_write_zeroes_sectors = 0; 3200 err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY); 3201 if (err) { 3202 queue_limits_cancel_update(mddev->gendisk->queue); 3203 return err; 3204 } 3205 return queue_limits_set(mddev->gendisk->queue, &lim); 3206 } 3207 3208 static int raid1_run(struct mddev *mddev) 3209 { 3210 struct r1conf *conf; 3211 int i; 3212 int ret; 3213 3214 if (mddev->level != 1) { 3215 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n", 3216 mdname(mddev), mddev->level); 3217 return -EIO; 3218 } 3219 if (mddev->reshape_position != MaxSector) { 3220 pr_warn("md/raid1:%s: reshape_position set but not supported\n", 3221 mdname(mddev)); 3222 return -EIO; 3223 } 3224 3225 /* 3226 * copy the already verified devices into our private RAID1 3227 * bookkeeping area. [whatever we allocate in run(), 3228 * should be freed in raid1_free()] 3229 */ 3230 if (mddev->private == NULL) 3231 conf = setup_conf(mddev); 3232 else 3233 conf = mddev->private; 3234 3235 if (IS_ERR(conf)) 3236 return PTR_ERR(conf); 3237 3238 if (!mddev_is_dm(mddev)) { 3239 ret = raid1_set_limits(mddev); 3240 if (ret) 3241 return ret; 3242 } 3243 3244 mddev->degraded = 0; 3245 for (i = 0; i < conf->raid_disks; i++) 3246 if (conf->mirrors[i].rdev == NULL || 3247 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) || 3248 test_bit(Faulty, &conf->mirrors[i].rdev->flags)) 3249 mddev->degraded++; 3250 /* 3251 * RAID1 needs at least one disk in active 3252 */ 3253 if (conf->raid_disks - mddev->degraded < 1) { 3254 md_unregister_thread(mddev, &conf->thread); 3255 return -EINVAL; 3256 } 3257 3258 if (conf->raid_disks - mddev->degraded == 1) 3259 mddev->recovery_cp = MaxSector; 3260 3261 if (mddev->recovery_cp != MaxSector) 3262 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n", 3263 mdname(mddev)); 3264 pr_info("md/raid1:%s: active with %d out of %d mirrors\n", 3265 mdname(mddev), mddev->raid_disks - mddev->degraded, 3266 mddev->raid_disks); 3267 3268 /* 3269 * Ok, everything is just fine now 3270 */ 3271 rcu_assign_pointer(mddev->thread, conf->thread); 3272 rcu_assign_pointer(conf->thread, NULL); 3273 mddev->private = conf; 3274 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags); 3275 3276 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0)); 3277 3278 ret = md_integrity_register(mddev); 3279 if (ret) 3280 md_unregister_thread(mddev, &mddev->thread); 3281 return ret; 3282 } 3283 3284 static void raid1_free(struct mddev *mddev, void *priv) 3285 { 3286 struct r1conf *conf = priv; 3287 3288 mempool_exit(&conf->r1bio_pool); 3289 kfree(conf->mirrors); 3290 safe_put_page(conf->tmppage); 3291 kfree(conf->poolinfo); 3292 kfree(conf->nr_pending); 3293 kfree(conf->nr_waiting); 3294 kfree(conf->nr_queued); 3295 kfree(conf->barrier); 3296 bioset_exit(&conf->bio_split); 3297 kfree(conf); 3298 } 3299 3300 static int raid1_resize(struct mddev *mddev, sector_t sectors) 3301 { 3302 /* no resync is happening, and there is enough space 3303 * on all devices, so we can resize. 3304 * We need to make sure resync covers any new space. 3305 * If the array is shrinking we should possibly wait until 3306 * any io in the removed space completes, but it hardly seems 3307 * worth it. 3308 */ 3309 sector_t newsize = raid1_size(mddev, sectors, 0); 3310 if (mddev->external_size && 3311 mddev->array_sectors > newsize) 3312 return -EINVAL; 3313 if (mddev->bitmap) { 3314 int ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0); 3315 if (ret) 3316 return ret; 3317 } 3318 md_set_array_sectors(mddev, newsize); 3319 if (sectors > mddev->dev_sectors && 3320 mddev->recovery_cp > mddev->dev_sectors) { 3321 mddev->recovery_cp = mddev->dev_sectors; 3322 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3323 } 3324 mddev->dev_sectors = sectors; 3325 mddev->resync_max_sectors = sectors; 3326 return 0; 3327 } 3328 3329 static int raid1_reshape(struct mddev *mddev) 3330 { 3331 /* We need to: 3332 * 1/ resize the r1bio_pool 3333 * 2/ resize conf->mirrors 3334 * 3335 * We allocate a new r1bio_pool if we can. 3336 * Then raise a device barrier and wait until all IO stops. 3337 * Then resize conf->mirrors and swap in the new r1bio pool. 3338 * 3339 * At the same time, we "pack" the devices so that all the missing 3340 * devices have the higher raid_disk numbers. 3341 */ 3342 mempool_t newpool, oldpool; 3343 struct pool_info *newpoolinfo; 3344 struct raid1_info *newmirrors; 3345 struct r1conf *conf = mddev->private; 3346 int cnt, raid_disks; 3347 unsigned long flags; 3348 int d, d2; 3349 int ret; 3350 3351 memset(&newpool, 0, sizeof(newpool)); 3352 memset(&oldpool, 0, sizeof(oldpool)); 3353 3354 /* Cannot change chunk_size, layout, or level */ 3355 if (mddev->chunk_sectors != mddev->new_chunk_sectors || 3356 mddev->layout != mddev->new_layout || 3357 mddev->level != mddev->new_level) { 3358 mddev->new_chunk_sectors = mddev->chunk_sectors; 3359 mddev->new_layout = mddev->layout; 3360 mddev->new_level = mddev->level; 3361 return -EINVAL; 3362 } 3363 3364 if (!mddev_is_clustered(mddev)) 3365 md_allow_write(mddev); 3366 3367 raid_disks = mddev->raid_disks + mddev->delta_disks; 3368 3369 if (raid_disks < conf->raid_disks) { 3370 cnt=0; 3371 for (d= 0; d < conf->raid_disks; d++) 3372 if (conf->mirrors[d].rdev) 3373 cnt++; 3374 if (cnt > raid_disks) 3375 return -EBUSY; 3376 } 3377 3378 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL); 3379 if (!newpoolinfo) 3380 return -ENOMEM; 3381 newpoolinfo->mddev = mddev; 3382 newpoolinfo->raid_disks = raid_disks * 2; 3383 3384 ret = mempool_init(&newpool, NR_RAID_BIOS, r1bio_pool_alloc, 3385 rbio_pool_free, newpoolinfo); 3386 if (ret) { 3387 kfree(newpoolinfo); 3388 return ret; 3389 } 3390 newmirrors = kzalloc(array3_size(sizeof(struct raid1_info), 3391 raid_disks, 2), 3392 GFP_KERNEL); 3393 if (!newmirrors) { 3394 kfree(newpoolinfo); 3395 mempool_exit(&newpool); 3396 return -ENOMEM; 3397 } 3398 3399 freeze_array(conf, 0); 3400 3401 /* ok, everything is stopped */ 3402 oldpool = conf->r1bio_pool; 3403 conf->r1bio_pool = newpool; 3404 3405 for (d = d2 = 0; d < conf->raid_disks; d++) { 3406 struct md_rdev *rdev = conf->mirrors[d].rdev; 3407 if (rdev && rdev->raid_disk != d2) { 3408 sysfs_unlink_rdev(mddev, rdev); 3409 rdev->raid_disk = d2; 3410 sysfs_unlink_rdev(mddev, rdev); 3411 if (sysfs_link_rdev(mddev, rdev)) 3412 pr_warn("md/raid1:%s: cannot register rd%d\n", 3413 mdname(mddev), rdev->raid_disk); 3414 } 3415 if (rdev) 3416 newmirrors[d2++].rdev = rdev; 3417 } 3418 kfree(conf->mirrors); 3419 conf->mirrors = newmirrors; 3420 kfree(conf->poolinfo); 3421 conf->poolinfo = newpoolinfo; 3422 3423 spin_lock_irqsave(&conf->device_lock, flags); 3424 mddev->degraded += (raid_disks - conf->raid_disks); 3425 spin_unlock_irqrestore(&conf->device_lock, flags); 3426 conf->raid_disks = mddev->raid_disks = raid_disks; 3427 mddev->delta_disks = 0; 3428 3429 unfreeze_array(conf); 3430 3431 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 3432 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3433 md_wakeup_thread(mddev->thread); 3434 3435 mempool_exit(&oldpool); 3436 return 0; 3437 } 3438 3439 static void raid1_quiesce(struct mddev *mddev, int quiesce) 3440 { 3441 struct r1conf *conf = mddev->private; 3442 3443 if (quiesce) 3444 freeze_array(conf, 0); 3445 else 3446 unfreeze_array(conf); 3447 } 3448 3449 static void *raid1_takeover(struct mddev *mddev) 3450 { 3451 /* raid1 can take over: 3452 * raid5 with 2 devices, any layout or chunk size 3453 */ 3454 if (mddev->level == 5 && mddev->raid_disks == 2) { 3455 struct r1conf *conf; 3456 mddev->new_level = 1; 3457 mddev->new_layout = 0; 3458 mddev->new_chunk_sectors = 0; 3459 conf = setup_conf(mddev); 3460 if (!IS_ERR(conf)) { 3461 /* Array must appear to be quiesced */ 3462 conf->array_frozen = 1; 3463 mddev_clear_unsupported_flags(mddev, 3464 UNSUPPORTED_MDDEV_FLAGS); 3465 } 3466 return conf; 3467 } 3468 return ERR_PTR(-EINVAL); 3469 } 3470 3471 static struct md_personality raid1_personality = 3472 { 3473 .name = "raid1", 3474 .level = 1, 3475 .owner = THIS_MODULE, 3476 .make_request = raid1_make_request, 3477 .run = raid1_run, 3478 .free = raid1_free, 3479 .status = raid1_status, 3480 .error_handler = raid1_error, 3481 .hot_add_disk = raid1_add_disk, 3482 .hot_remove_disk= raid1_remove_disk, 3483 .spare_active = raid1_spare_active, 3484 .sync_request = raid1_sync_request, 3485 .resize = raid1_resize, 3486 .size = raid1_size, 3487 .check_reshape = raid1_reshape, 3488 .quiesce = raid1_quiesce, 3489 .takeover = raid1_takeover, 3490 }; 3491 3492 static int __init raid_init(void) 3493 { 3494 return register_md_personality(&raid1_personality); 3495 } 3496 3497 static void raid_exit(void) 3498 { 3499 unregister_md_personality(&raid1_personality); 3500 } 3501 3502 module_init(raid_init); 3503 module_exit(raid_exit); 3504 MODULE_LICENSE("GPL"); 3505 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD"); 3506 MODULE_ALIAS("md-personality-3"); /* RAID1 */ 3507 MODULE_ALIAS("md-raid1"); 3508 MODULE_ALIAS("md-level-1"); 3509