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