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