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