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