1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * raid10.c : Multiple Devices driver for Linux 4 * 5 * Copyright (C) 2000-2004 Neil Brown 6 * 7 * RAID-10 support for md. 8 * 9 * Base on code in raid1.c. See raid1.c for further copyright information. 10 */ 11 12 #include <linux/slab.h> 13 #include <linux/delay.h> 14 #include <linux/blkdev.h> 15 #include <linux/module.h> 16 #include <linux/seq_file.h> 17 #include <linux/ratelimit.h> 18 #include <linux/kthread.h> 19 #include <linux/raid/md_p.h> 20 #include <trace/events/block.h> 21 #include "md.h" 22 23 #define RAID_1_10_NAME "raid10" 24 #include "raid10.h" 25 #include "raid0.h" 26 #include "md-bitmap.h" 27 28 /* 29 * RAID10 provides a combination of RAID0 and RAID1 functionality. 30 * The layout of data is defined by 31 * chunk_size 32 * raid_disks 33 * near_copies (stored in low byte of layout) 34 * far_copies (stored in second byte of layout) 35 * far_offset (stored in bit 16 of layout ) 36 * use_far_sets (stored in bit 17 of layout ) 37 * use_far_sets_bugfixed (stored in bit 18 of layout ) 38 * 39 * The data to be stored is divided into chunks using chunksize. Each device 40 * is divided into far_copies sections. In each section, chunks are laid out 41 * in a style similar to raid0, but near_copies copies of each chunk is stored 42 * (each on a different drive). The starting device for each section is offset 43 * near_copies from the starting device of the previous section. Thus there 44 * are (near_copies * far_copies) of each chunk, and each is on a different 45 * drive. near_copies and far_copies must be at least one, and their product 46 * is at most raid_disks. 47 * 48 * If far_offset is true, then the far_copies are handled a bit differently. 49 * The copies are still in different stripes, but instead of being very far 50 * apart on disk, there are adjacent stripes. 51 * 52 * The far and offset algorithms are handled slightly differently if 53 * 'use_far_sets' is true. In this case, the array's devices are grouped into 54 * sets that are (near_copies * far_copies) in size. The far copied stripes 55 * are still shifted by 'near_copies' devices, but this shifting stays confined 56 * to the set rather than the entire array. This is done to improve the number 57 * of device combinations that can fail without causing the array to fail. 58 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk 59 * on a device): 60 * A B C D A B C D E 61 * ... ... 62 * D A B C E A B C D 63 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s): 64 * [A B] [C D] [A B] [C D E] 65 * |...| |...| |...| | ... | 66 * [B A] [D C] [B A] [E C D] 67 */ 68 69 static void allow_barrier(struct r10conf *conf); 70 static void lower_barrier(struct r10conf *conf); 71 static int _enough(struct r10conf *conf, int previous, int ignore); 72 static int enough(struct r10conf *conf, int ignore); 73 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, 74 int *skipped); 75 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio); 76 static void end_reshape_write(struct bio *bio); 77 static void end_reshape(struct r10conf *conf); 78 79 #include "raid1-10.c" 80 81 #define NULL_CMD 82 #define cmd_before(conf, cmd) \ 83 do { \ 84 write_sequnlock_irq(&(conf)->resync_lock); \ 85 cmd; \ 86 } while (0) 87 #define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock) 88 89 #define wait_event_barrier_cmd(conf, cond, cmd) \ 90 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \ 91 cmd_after(conf)) 92 93 #define wait_event_barrier(conf, cond) \ 94 wait_event_barrier_cmd(conf, cond, NULL_CMD) 95 96 /* 97 * for resync bio, r10bio pointer can be retrieved from the per-bio 98 * 'struct resync_pages'. 99 */ 100 static inline struct r10bio *get_resync_r10bio(struct bio *bio) 101 { 102 return get_resync_pages(bio)->raid_bio; 103 } 104 105 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) 106 { 107 struct r10conf *conf = data; 108 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]); 109 110 /* allocate a r10bio with room for raid_disks entries in the 111 * bios array */ 112 return kzalloc(size, gfp_flags); 113 } 114 115 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) 116 /* amount of memory to reserve for resync requests */ 117 #define RESYNC_WINDOW (1024*1024) 118 /* maximum number of concurrent requests, memory permitting */ 119 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) 120 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW) 121 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9) 122 123 /* 124 * When performing a resync, we need to read and compare, so 125 * we need as many pages are there are copies. 126 * When performing a recovery, we need 2 bios, one for read, 127 * one for write (we recover only one drive per r10buf) 128 * 129 */ 130 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) 131 { 132 struct r10conf *conf = data; 133 struct r10bio *r10_bio; 134 struct bio *bio; 135 int j; 136 int nalloc, nalloc_rp; 137 struct resync_pages *rps; 138 139 r10_bio = r10bio_pool_alloc(gfp_flags, conf); 140 if (!r10_bio) 141 return NULL; 142 143 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || 144 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) 145 nalloc = conf->copies; /* resync */ 146 else 147 nalloc = 2; /* recovery */ 148 149 /* allocate once for all bios */ 150 if (!conf->have_replacement) 151 nalloc_rp = nalloc; 152 else 153 nalloc_rp = nalloc * 2; 154 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags); 155 if (!rps) 156 goto out_free_r10bio; 157 158 /* 159 * Allocate bios. 160 */ 161 for (j = nalloc ; j-- ; ) { 162 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags); 163 if (!bio) 164 goto out_free_bio; 165 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0); 166 r10_bio->devs[j].bio = bio; 167 if (!conf->have_replacement) 168 continue; 169 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags); 170 if (!bio) 171 goto out_free_bio; 172 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0); 173 r10_bio->devs[j].repl_bio = bio; 174 } 175 /* 176 * Allocate RESYNC_PAGES data pages and attach them 177 * where needed. 178 */ 179 for (j = 0; j < nalloc; j++) { 180 struct bio *rbio = r10_bio->devs[j].repl_bio; 181 struct resync_pages *rp, *rp_repl; 182 183 rp = &rps[j]; 184 if (rbio) 185 rp_repl = &rps[nalloc + j]; 186 187 bio = r10_bio->devs[j].bio; 188 189 if (!j || test_bit(MD_RECOVERY_SYNC, 190 &conf->mddev->recovery)) { 191 if (resync_alloc_pages(rp, gfp_flags)) 192 goto out_free_pages; 193 } else { 194 memcpy(rp, &rps[0], sizeof(*rp)); 195 resync_get_all_pages(rp); 196 } 197 198 rp->raid_bio = r10_bio; 199 bio->bi_private = rp; 200 if (rbio) { 201 memcpy(rp_repl, rp, sizeof(*rp)); 202 rbio->bi_private = rp_repl; 203 } 204 } 205 206 return r10_bio; 207 208 out_free_pages: 209 while (--j >= 0) 210 resync_free_pages(&rps[j]); 211 212 j = 0; 213 out_free_bio: 214 for ( ; j < nalloc; j++) { 215 if (r10_bio->devs[j].bio) 216 bio_uninit(r10_bio->devs[j].bio); 217 kfree(r10_bio->devs[j].bio); 218 if (r10_bio->devs[j].repl_bio) 219 bio_uninit(r10_bio->devs[j].repl_bio); 220 kfree(r10_bio->devs[j].repl_bio); 221 } 222 kfree(rps); 223 out_free_r10bio: 224 rbio_pool_free(r10_bio, conf); 225 return NULL; 226 } 227 228 static void r10buf_pool_free(void *__r10_bio, void *data) 229 { 230 struct r10conf *conf = data; 231 struct r10bio *r10bio = __r10_bio; 232 int j; 233 struct resync_pages *rp = NULL; 234 235 for (j = conf->copies; j--; ) { 236 struct bio *bio = r10bio->devs[j].bio; 237 238 if (bio) { 239 rp = get_resync_pages(bio); 240 resync_free_pages(rp); 241 bio_uninit(bio); 242 kfree(bio); 243 } 244 245 bio = r10bio->devs[j].repl_bio; 246 if (bio) { 247 bio_uninit(bio); 248 kfree(bio); 249 } 250 } 251 252 /* resync pages array stored in the 1st bio's .bi_private */ 253 kfree(rp); 254 255 rbio_pool_free(r10bio, conf); 256 } 257 258 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) 259 { 260 int i; 261 262 for (i = 0; i < conf->geo.raid_disks; i++) { 263 struct bio **bio = & r10_bio->devs[i].bio; 264 if (!BIO_SPECIAL(*bio)) 265 bio_put(*bio); 266 *bio = NULL; 267 bio = &r10_bio->devs[i].repl_bio; 268 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio)) 269 bio_put(*bio); 270 *bio = NULL; 271 } 272 } 273 274 static void free_r10bio(struct r10bio *r10_bio) 275 { 276 struct r10conf *conf = r10_bio->mddev->private; 277 278 put_all_bios(conf, r10_bio); 279 mempool_free(r10_bio, &conf->r10bio_pool); 280 } 281 282 static void put_buf(struct r10bio *r10_bio) 283 { 284 struct r10conf *conf = r10_bio->mddev->private; 285 286 mempool_free(r10_bio, &conf->r10buf_pool); 287 288 lower_barrier(conf); 289 } 290 291 static void wake_up_barrier(struct r10conf *conf) 292 { 293 if (wq_has_sleeper(&conf->wait_barrier)) 294 wake_up(&conf->wait_barrier); 295 } 296 297 static void reschedule_retry(struct r10bio *r10_bio) 298 { 299 unsigned long flags; 300 struct mddev *mddev = r10_bio->mddev; 301 struct r10conf *conf = mddev->private; 302 303 spin_lock_irqsave(&conf->device_lock, flags); 304 list_add(&r10_bio->retry_list, &conf->retry_list); 305 conf->nr_queued ++; 306 spin_unlock_irqrestore(&conf->device_lock, flags); 307 308 /* wake up frozen array... */ 309 wake_up(&conf->wait_barrier); 310 311 md_wakeup_thread(mddev->thread); 312 } 313 314 /* 315 * raid_end_bio_io() is called when we have finished servicing a mirrored 316 * operation and are ready to return a success/failure code to the buffer 317 * cache layer. 318 */ 319 static void raid_end_bio_io(struct r10bio *r10_bio) 320 { 321 struct bio *bio = r10_bio->master_bio; 322 struct r10conf *conf = r10_bio->mddev->private; 323 324 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) 325 bio->bi_status = BLK_STS_IOERR; 326 327 bio_endio(bio); 328 /* 329 * Wake up any possible resync thread that waits for the device 330 * to go idle. 331 */ 332 allow_barrier(conf); 333 334 free_r10bio(r10_bio); 335 } 336 337 /* 338 * Update disk head position estimator based on IRQ completion info. 339 */ 340 static inline void update_head_pos(int slot, struct r10bio *r10_bio) 341 { 342 struct r10conf *conf = r10_bio->mddev->private; 343 344 conf->mirrors[r10_bio->devs[slot].devnum].head_position = 345 r10_bio->devs[slot].addr + (r10_bio->sectors); 346 } 347 348 /* 349 * Find the disk number which triggered given bio 350 */ 351 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, 352 struct bio *bio, int *slotp, int *replp) 353 { 354 int slot; 355 int repl = 0; 356 357 for (slot = 0; slot < conf->geo.raid_disks; slot++) { 358 if (r10_bio->devs[slot].bio == bio) 359 break; 360 if (r10_bio->devs[slot].repl_bio == bio) { 361 repl = 1; 362 break; 363 } 364 } 365 366 update_head_pos(slot, r10_bio); 367 368 if (slotp) 369 *slotp = slot; 370 if (replp) 371 *replp = repl; 372 return r10_bio->devs[slot].devnum; 373 } 374 375 static void raid10_end_read_request(struct bio *bio) 376 { 377 int uptodate = !bio->bi_status; 378 struct r10bio *r10_bio = bio->bi_private; 379 int slot; 380 struct md_rdev *rdev; 381 struct r10conf *conf = r10_bio->mddev->private; 382 383 slot = r10_bio->read_slot; 384 rdev = r10_bio->devs[slot].rdev; 385 /* 386 * this branch is our 'one mirror IO has finished' event handler: 387 */ 388 update_head_pos(slot, r10_bio); 389 390 if (uptodate) { 391 /* 392 * Set R10BIO_Uptodate in our master bio, so that 393 * we will return a good error code to the higher 394 * levels even if IO on some other mirrored buffer fails. 395 * 396 * The 'master' represents the composite IO operation to 397 * user-side. So if something waits for IO, then it will 398 * wait for the 'master' bio. 399 */ 400 set_bit(R10BIO_Uptodate, &r10_bio->state); 401 } else { 402 /* If all other devices that store this block have 403 * failed, we want to return the error upwards rather 404 * than fail the last device. Here we redefine 405 * "uptodate" to mean "Don't want to retry" 406 */ 407 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state), 408 rdev->raid_disk)) 409 uptodate = 1; 410 } 411 if (uptodate) { 412 raid_end_bio_io(r10_bio); 413 rdev_dec_pending(rdev, conf->mddev); 414 } else { 415 /* 416 * oops, read error - keep the refcount on the rdev 417 */ 418 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n", 419 mdname(conf->mddev), 420 rdev->bdev, 421 (unsigned long long)r10_bio->sector); 422 set_bit(R10BIO_ReadError, &r10_bio->state); 423 reschedule_retry(r10_bio); 424 } 425 } 426 427 static void close_write(struct r10bio *r10_bio) 428 { 429 struct mddev *mddev = r10_bio->mddev; 430 431 /* clear the bitmap if all writes complete successfully */ 432 mddev->bitmap_ops->endwrite(mddev, r10_bio->sector, r10_bio->sectors, 433 !test_bit(R10BIO_Degraded, &r10_bio->state), 434 false); 435 md_write_end(mddev); 436 } 437 438 static void one_write_done(struct r10bio *r10_bio) 439 { 440 if (atomic_dec_and_test(&r10_bio->remaining)) { 441 if (test_bit(R10BIO_WriteError, &r10_bio->state)) 442 reschedule_retry(r10_bio); 443 else { 444 close_write(r10_bio); 445 if (test_bit(R10BIO_MadeGood, &r10_bio->state)) 446 reschedule_retry(r10_bio); 447 else 448 raid_end_bio_io(r10_bio); 449 } 450 } 451 } 452 453 static void raid10_end_write_request(struct bio *bio) 454 { 455 struct r10bio *r10_bio = bio->bi_private; 456 int dev; 457 int dec_rdev = 1; 458 struct r10conf *conf = r10_bio->mddev->private; 459 int slot, repl; 460 struct md_rdev *rdev = NULL; 461 struct bio *to_put = NULL; 462 bool discard_error; 463 464 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD; 465 466 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 467 468 if (repl) 469 rdev = conf->mirrors[dev].replacement; 470 if (!rdev) { 471 smp_rmb(); 472 repl = 0; 473 rdev = conf->mirrors[dev].rdev; 474 } 475 /* 476 * this branch is our 'one mirror IO has finished' event handler: 477 */ 478 if (bio->bi_status && !discard_error) { 479 if (repl) 480 /* Never record new bad blocks to replacement, 481 * just fail it. 482 */ 483 md_error(rdev->mddev, rdev); 484 else { 485 set_bit(WriteErrorSeen, &rdev->flags); 486 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 487 set_bit(MD_RECOVERY_NEEDED, 488 &rdev->mddev->recovery); 489 490 dec_rdev = 0; 491 if (test_bit(FailFast, &rdev->flags) && 492 (bio->bi_opf & MD_FAILFAST)) { 493 md_error(rdev->mddev, rdev); 494 } 495 496 /* 497 * When the device is faulty, it is not necessary to 498 * handle write error. 499 */ 500 if (!test_bit(Faulty, &rdev->flags)) 501 set_bit(R10BIO_WriteError, &r10_bio->state); 502 else { 503 /* Fail the request */ 504 set_bit(R10BIO_Degraded, &r10_bio->state); 505 r10_bio->devs[slot].bio = NULL; 506 to_put = bio; 507 dec_rdev = 1; 508 } 509 } 510 } else { 511 /* 512 * Set R10BIO_Uptodate in our master bio, so that 513 * we will return a good error code for to the higher 514 * levels even if IO on some other mirrored buffer fails. 515 * 516 * The 'master' represents the composite IO operation to 517 * user-side. So if something waits for IO, then it will 518 * wait for the 'master' bio. 519 * 520 * Do not set R10BIO_Uptodate if the current device is 521 * rebuilding or Faulty. This is because we cannot use 522 * such device for properly reading the data back (we could 523 * potentially use it, if the current write would have felt 524 * before rdev->recovery_offset, but for simplicity we don't 525 * check this here. 526 */ 527 if (test_bit(In_sync, &rdev->flags) && 528 !test_bit(Faulty, &rdev->flags)) 529 set_bit(R10BIO_Uptodate, &r10_bio->state); 530 531 /* Maybe we can clear some bad blocks. */ 532 if (rdev_has_badblock(rdev, r10_bio->devs[slot].addr, 533 r10_bio->sectors) && 534 !discard_error) { 535 bio_put(bio); 536 if (repl) 537 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD; 538 else 539 r10_bio->devs[slot].bio = IO_MADE_GOOD; 540 dec_rdev = 0; 541 set_bit(R10BIO_MadeGood, &r10_bio->state); 542 } 543 } 544 545 /* 546 * 547 * Let's see if all mirrored write operations have finished 548 * already. 549 */ 550 one_write_done(r10_bio); 551 if (dec_rdev) 552 rdev_dec_pending(rdev, conf->mddev); 553 if (to_put) 554 bio_put(to_put); 555 } 556 557 /* 558 * RAID10 layout manager 559 * As well as the chunksize and raid_disks count, there are two 560 * parameters: near_copies and far_copies. 561 * near_copies * far_copies must be <= raid_disks. 562 * Normally one of these will be 1. 563 * If both are 1, we get raid0. 564 * If near_copies == raid_disks, we get raid1. 565 * 566 * Chunks are laid out in raid0 style with near_copies copies of the 567 * first chunk, followed by near_copies copies of the next chunk and 568 * so on. 569 * If far_copies > 1, then after 1/far_copies of the array has been assigned 570 * as described above, we start again with a device offset of near_copies. 571 * So we effectively have another copy of the whole array further down all 572 * the drives, but with blocks on different drives. 573 * With this layout, and block is never stored twice on the one device. 574 * 575 * raid10_find_phys finds the sector offset of a given virtual sector 576 * on each device that it is on. 577 * 578 * raid10_find_virt does the reverse mapping, from a device and a 579 * sector offset to a virtual address 580 */ 581 582 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio) 583 { 584 int n,f; 585 sector_t sector; 586 sector_t chunk; 587 sector_t stripe; 588 int dev; 589 int slot = 0; 590 int last_far_set_start, last_far_set_size; 591 592 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1; 593 last_far_set_start *= geo->far_set_size; 594 595 last_far_set_size = geo->far_set_size; 596 last_far_set_size += (geo->raid_disks % geo->far_set_size); 597 598 /* now calculate first sector/dev */ 599 chunk = r10bio->sector >> geo->chunk_shift; 600 sector = r10bio->sector & geo->chunk_mask; 601 602 chunk *= geo->near_copies; 603 stripe = chunk; 604 dev = sector_div(stripe, geo->raid_disks); 605 if (geo->far_offset) 606 stripe *= geo->far_copies; 607 608 sector += stripe << geo->chunk_shift; 609 610 /* and calculate all the others */ 611 for (n = 0; n < geo->near_copies; n++) { 612 int d = dev; 613 int set; 614 sector_t s = sector; 615 r10bio->devs[slot].devnum = d; 616 r10bio->devs[slot].addr = s; 617 slot++; 618 619 for (f = 1; f < geo->far_copies; f++) { 620 set = d / geo->far_set_size; 621 d += geo->near_copies; 622 623 if ((geo->raid_disks % geo->far_set_size) && 624 (d > last_far_set_start)) { 625 d -= last_far_set_start; 626 d %= last_far_set_size; 627 d += last_far_set_start; 628 } else { 629 d %= geo->far_set_size; 630 d += geo->far_set_size * set; 631 } 632 s += geo->stride; 633 r10bio->devs[slot].devnum = d; 634 r10bio->devs[slot].addr = s; 635 slot++; 636 } 637 dev++; 638 if (dev >= geo->raid_disks) { 639 dev = 0; 640 sector += (geo->chunk_mask + 1); 641 } 642 } 643 } 644 645 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) 646 { 647 struct geom *geo = &conf->geo; 648 649 if (conf->reshape_progress != MaxSector && 650 ((r10bio->sector >= conf->reshape_progress) != 651 conf->mddev->reshape_backwards)) { 652 set_bit(R10BIO_Previous, &r10bio->state); 653 geo = &conf->prev; 654 } else 655 clear_bit(R10BIO_Previous, &r10bio->state); 656 657 __raid10_find_phys(geo, r10bio); 658 } 659 660 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev) 661 { 662 sector_t offset, chunk, vchunk; 663 /* Never use conf->prev as this is only called during resync 664 * or recovery, so reshape isn't happening 665 */ 666 struct geom *geo = &conf->geo; 667 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size; 668 int far_set_size = geo->far_set_size; 669 int last_far_set_start; 670 671 if (geo->raid_disks % geo->far_set_size) { 672 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1; 673 last_far_set_start *= geo->far_set_size; 674 675 if (dev >= last_far_set_start) { 676 far_set_size = geo->far_set_size; 677 far_set_size += (geo->raid_disks % geo->far_set_size); 678 far_set_start = last_far_set_start; 679 } 680 } 681 682 offset = sector & geo->chunk_mask; 683 if (geo->far_offset) { 684 int fc; 685 chunk = sector >> geo->chunk_shift; 686 fc = sector_div(chunk, geo->far_copies); 687 dev -= fc * geo->near_copies; 688 if (dev < far_set_start) 689 dev += far_set_size; 690 } else { 691 while (sector >= geo->stride) { 692 sector -= geo->stride; 693 if (dev < (geo->near_copies + far_set_start)) 694 dev += far_set_size - geo->near_copies; 695 else 696 dev -= geo->near_copies; 697 } 698 chunk = sector >> geo->chunk_shift; 699 } 700 vchunk = chunk * geo->raid_disks + dev; 701 sector_div(vchunk, geo->near_copies); 702 return (vchunk << geo->chunk_shift) + offset; 703 } 704 705 /* 706 * This routine returns the disk from which the requested read should 707 * be done. There is a per-array 'next expected sequential IO' sector 708 * number - if this matches on the next IO then we use the last disk. 709 * There is also a per-disk 'last know head position' sector that is 710 * maintained from IRQ contexts, both the normal and the resync IO 711 * completion handlers update this position correctly. If there is no 712 * perfect sequential match then we pick the disk whose head is closest. 713 * 714 * If there are 2 mirrors in the same 2 devices, performance degrades 715 * because position is mirror, not device based. 716 * 717 * The rdev for the device selected will have nr_pending incremented. 718 */ 719 720 /* 721 * FIXME: possibly should rethink readbalancing and do it differently 722 * depending on near_copies / far_copies geometry. 723 */ 724 static struct md_rdev *read_balance(struct r10conf *conf, 725 struct r10bio *r10_bio, 726 int *max_sectors) 727 { 728 const sector_t this_sector = r10_bio->sector; 729 int disk, slot; 730 int sectors = r10_bio->sectors; 731 int best_good_sectors; 732 sector_t new_distance, best_dist; 733 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL; 734 int do_balance; 735 int best_dist_slot, best_pending_slot; 736 bool has_nonrot_disk = false; 737 unsigned int min_pending; 738 struct geom *geo = &conf->geo; 739 740 raid10_find_phys(conf, r10_bio); 741 best_dist_slot = -1; 742 min_pending = UINT_MAX; 743 best_dist_rdev = NULL; 744 best_pending_rdev = NULL; 745 best_dist = MaxSector; 746 best_good_sectors = 0; 747 do_balance = 1; 748 clear_bit(R10BIO_FailFast, &r10_bio->state); 749 750 if (raid1_should_read_first(conf->mddev, this_sector, sectors)) 751 do_balance = 0; 752 753 for (slot = 0; slot < conf->copies ; slot++) { 754 sector_t first_bad; 755 int bad_sectors; 756 sector_t dev_sector; 757 unsigned int pending; 758 bool nonrot; 759 760 if (r10_bio->devs[slot].bio == IO_BLOCKED) 761 continue; 762 disk = r10_bio->devs[slot].devnum; 763 rdev = conf->mirrors[disk].replacement; 764 if (rdev == NULL || test_bit(Faulty, &rdev->flags) || 765 r10_bio->devs[slot].addr + sectors > 766 rdev->recovery_offset) 767 rdev = conf->mirrors[disk].rdev; 768 if (rdev == NULL || 769 test_bit(Faulty, &rdev->flags)) 770 continue; 771 if (!test_bit(In_sync, &rdev->flags) && 772 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) 773 continue; 774 775 dev_sector = r10_bio->devs[slot].addr; 776 if (is_badblock(rdev, dev_sector, sectors, 777 &first_bad, &bad_sectors)) { 778 if (best_dist < MaxSector) 779 /* Already have a better slot */ 780 continue; 781 if (first_bad <= dev_sector) { 782 /* Cannot read here. If this is the 783 * 'primary' device, then we must not read 784 * beyond 'bad_sectors' from another device. 785 */ 786 bad_sectors -= (dev_sector - first_bad); 787 if (!do_balance && sectors > bad_sectors) 788 sectors = bad_sectors; 789 if (best_good_sectors > sectors) 790 best_good_sectors = sectors; 791 } else { 792 sector_t good_sectors = 793 first_bad - dev_sector; 794 if (good_sectors > best_good_sectors) { 795 best_good_sectors = good_sectors; 796 best_dist_slot = slot; 797 best_dist_rdev = rdev; 798 } 799 if (!do_balance) 800 /* Must read from here */ 801 break; 802 } 803 continue; 804 } else 805 best_good_sectors = sectors; 806 807 if (!do_balance) 808 break; 809 810 nonrot = bdev_nonrot(rdev->bdev); 811 has_nonrot_disk |= nonrot; 812 pending = atomic_read(&rdev->nr_pending); 813 if (min_pending > pending && nonrot) { 814 min_pending = pending; 815 best_pending_slot = slot; 816 best_pending_rdev = rdev; 817 } 818 819 if (best_dist_slot >= 0) 820 /* At least 2 disks to choose from so failfast is OK */ 821 set_bit(R10BIO_FailFast, &r10_bio->state); 822 /* This optimisation is debatable, and completely destroys 823 * sequential read speed for 'far copies' arrays. So only 824 * keep it for 'near' arrays, and review those later. 825 */ 826 if (geo->near_copies > 1 && !pending) 827 new_distance = 0; 828 829 /* for far > 1 always use the lowest address */ 830 else if (geo->far_copies > 1) 831 new_distance = r10_bio->devs[slot].addr; 832 else 833 new_distance = abs(r10_bio->devs[slot].addr - 834 conf->mirrors[disk].head_position); 835 836 if (new_distance < best_dist) { 837 best_dist = new_distance; 838 best_dist_slot = slot; 839 best_dist_rdev = rdev; 840 } 841 } 842 if (slot >= conf->copies) { 843 if (has_nonrot_disk) { 844 slot = best_pending_slot; 845 rdev = best_pending_rdev; 846 } else { 847 slot = best_dist_slot; 848 rdev = best_dist_rdev; 849 } 850 } 851 852 if (slot >= 0) { 853 atomic_inc(&rdev->nr_pending); 854 r10_bio->read_slot = slot; 855 } else 856 rdev = NULL; 857 *max_sectors = best_good_sectors; 858 859 return rdev; 860 } 861 862 static void flush_pending_writes(struct r10conf *conf) 863 { 864 /* Any writes that have been queued but are awaiting 865 * bitmap updates get flushed here. 866 */ 867 spin_lock_irq(&conf->device_lock); 868 869 if (conf->pending_bio_list.head) { 870 struct blk_plug plug; 871 struct bio *bio; 872 873 bio = bio_list_get(&conf->pending_bio_list); 874 spin_unlock_irq(&conf->device_lock); 875 876 /* 877 * As this is called in a wait_event() loop (see freeze_array), 878 * current->state might be TASK_UNINTERRUPTIBLE which will 879 * cause a warning when we prepare to wait again. As it is 880 * rare that this path is taken, it is perfectly safe to force 881 * us to go around the wait_event() loop again, so the warning 882 * is a false-positive. Silence the warning by resetting 883 * thread state 884 */ 885 __set_current_state(TASK_RUNNING); 886 887 blk_start_plug(&plug); 888 raid1_prepare_flush_writes(conf->mddev); 889 wake_up(&conf->wait_barrier); 890 891 while (bio) { /* submit pending writes */ 892 struct bio *next = bio->bi_next; 893 894 raid1_submit_write(bio); 895 bio = next; 896 cond_resched(); 897 } 898 blk_finish_plug(&plug); 899 } else 900 spin_unlock_irq(&conf->device_lock); 901 } 902 903 /* Barriers.... 904 * Sometimes we need to suspend IO while we do something else, 905 * either some resync/recovery, or reconfigure the array. 906 * To do this we raise a 'barrier'. 907 * The 'barrier' is a counter that can be raised multiple times 908 * to count how many activities are happening which preclude 909 * normal IO. 910 * We can only raise the barrier if there is no pending IO. 911 * i.e. if nr_pending == 0. 912 * We choose only to raise the barrier if no-one is waiting for the 913 * barrier to go down. This means that as soon as an IO request 914 * is ready, no other operations which require a barrier will start 915 * until the IO request has had a chance. 916 * 917 * So: regular IO calls 'wait_barrier'. When that returns there 918 * is no backgroup IO happening, It must arrange to call 919 * allow_barrier when it has finished its IO. 920 * backgroup IO calls must call raise_barrier. Once that returns 921 * there is no normal IO happeing. It must arrange to call 922 * lower_barrier when the particular background IO completes. 923 */ 924 925 static void raise_barrier(struct r10conf *conf, int force) 926 { 927 write_seqlock_irq(&conf->resync_lock); 928 929 if (WARN_ON_ONCE(force && !conf->barrier)) 930 force = false; 931 932 /* Wait until no block IO is waiting (unless 'force') */ 933 wait_event_barrier(conf, force || !conf->nr_waiting); 934 935 /* block any new IO from starting */ 936 WRITE_ONCE(conf->barrier, conf->barrier + 1); 937 938 /* Now wait for all pending IO to complete */ 939 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) && 940 conf->barrier < RESYNC_DEPTH); 941 942 write_sequnlock_irq(&conf->resync_lock); 943 } 944 945 static void lower_barrier(struct r10conf *conf) 946 { 947 unsigned long flags; 948 949 write_seqlock_irqsave(&conf->resync_lock, flags); 950 WRITE_ONCE(conf->barrier, conf->barrier - 1); 951 write_sequnlock_irqrestore(&conf->resync_lock, flags); 952 wake_up(&conf->wait_barrier); 953 } 954 955 static bool stop_waiting_barrier(struct r10conf *conf) 956 { 957 struct bio_list *bio_list = current->bio_list; 958 struct md_thread *thread; 959 960 /* barrier is dropped */ 961 if (!conf->barrier) 962 return true; 963 964 /* 965 * If there are already pending requests (preventing the barrier from 966 * rising completely), and the pre-process bio queue isn't empty, then 967 * don't wait, as we need to empty that queue to get the nr_pending 968 * count down. 969 */ 970 if (atomic_read(&conf->nr_pending) && bio_list && 971 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1]))) 972 return true; 973 974 /* daemon thread must exist while handling io */ 975 thread = rcu_dereference_protected(conf->mddev->thread, true); 976 /* 977 * move on if io is issued from raid10d(), nr_pending is not released 978 * from original io(see handle_read_error()). All raise barrier is 979 * blocked until this io is done. 980 */ 981 if (thread->tsk == current) { 982 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0); 983 return true; 984 } 985 986 return false; 987 } 988 989 static bool wait_barrier_nolock(struct r10conf *conf) 990 { 991 unsigned int seq = read_seqbegin(&conf->resync_lock); 992 993 if (READ_ONCE(conf->barrier)) 994 return false; 995 996 atomic_inc(&conf->nr_pending); 997 if (!read_seqretry(&conf->resync_lock, seq)) 998 return true; 999 1000 if (atomic_dec_and_test(&conf->nr_pending)) 1001 wake_up_barrier(conf); 1002 1003 return false; 1004 } 1005 1006 static bool wait_barrier(struct r10conf *conf, bool nowait) 1007 { 1008 bool ret = true; 1009 1010 if (wait_barrier_nolock(conf)) 1011 return true; 1012 1013 write_seqlock_irq(&conf->resync_lock); 1014 if (conf->barrier) { 1015 /* Return false when nowait flag is set */ 1016 if (nowait) { 1017 ret = false; 1018 } else { 1019 conf->nr_waiting++; 1020 mddev_add_trace_msg(conf->mddev, "raid10 wait barrier"); 1021 wait_event_barrier(conf, stop_waiting_barrier(conf)); 1022 conf->nr_waiting--; 1023 } 1024 if (!conf->nr_waiting) 1025 wake_up(&conf->wait_barrier); 1026 } 1027 /* Only increment nr_pending when we wait */ 1028 if (ret) 1029 atomic_inc(&conf->nr_pending); 1030 write_sequnlock_irq(&conf->resync_lock); 1031 return ret; 1032 } 1033 1034 static void allow_barrier(struct r10conf *conf) 1035 { 1036 if ((atomic_dec_and_test(&conf->nr_pending)) || 1037 (conf->array_freeze_pending)) 1038 wake_up_barrier(conf); 1039 } 1040 1041 static void freeze_array(struct r10conf *conf, int extra) 1042 { 1043 /* stop syncio and normal IO and wait for everything to 1044 * go quiet. 1045 * We increment barrier and nr_waiting, and then 1046 * wait until nr_pending match nr_queued+extra 1047 * This is called in the context of one normal IO request 1048 * that has failed. Thus any sync request that might be pending 1049 * will be blocked by nr_pending, and we need to wait for 1050 * pending IO requests to complete or be queued for re-try. 1051 * Thus the number queued (nr_queued) plus this request (extra) 1052 * must match the number of pending IOs (nr_pending) before 1053 * we continue. 1054 */ 1055 write_seqlock_irq(&conf->resync_lock); 1056 conf->array_freeze_pending++; 1057 WRITE_ONCE(conf->barrier, conf->barrier + 1); 1058 conf->nr_waiting++; 1059 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) == 1060 conf->nr_queued + extra, flush_pending_writes(conf)); 1061 conf->array_freeze_pending--; 1062 write_sequnlock_irq(&conf->resync_lock); 1063 } 1064 1065 static void unfreeze_array(struct r10conf *conf) 1066 { 1067 /* reverse the effect of the freeze */ 1068 write_seqlock_irq(&conf->resync_lock); 1069 WRITE_ONCE(conf->barrier, conf->barrier - 1); 1070 conf->nr_waiting--; 1071 wake_up(&conf->wait_barrier); 1072 write_sequnlock_irq(&conf->resync_lock); 1073 } 1074 1075 static sector_t choose_data_offset(struct r10bio *r10_bio, 1076 struct md_rdev *rdev) 1077 { 1078 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) || 1079 test_bit(R10BIO_Previous, &r10_bio->state)) 1080 return rdev->data_offset; 1081 else 1082 return rdev->new_data_offset; 1083 } 1084 1085 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule) 1086 { 1087 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb); 1088 struct mddev *mddev = plug->cb.data; 1089 struct r10conf *conf = mddev->private; 1090 struct bio *bio; 1091 1092 if (from_schedule) { 1093 spin_lock_irq(&conf->device_lock); 1094 bio_list_merge(&conf->pending_bio_list, &plug->pending); 1095 spin_unlock_irq(&conf->device_lock); 1096 wake_up_barrier(conf); 1097 md_wakeup_thread(mddev->thread); 1098 kfree(plug); 1099 return; 1100 } 1101 1102 /* we aren't scheduling, so we can do the write-out directly. */ 1103 bio = bio_list_get(&plug->pending); 1104 raid1_prepare_flush_writes(mddev); 1105 wake_up_barrier(conf); 1106 1107 while (bio) { /* submit pending writes */ 1108 struct bio *next = bio->bi_next; 1109 1110 raid1_submit_write(bio); 1111 bio = next; 1112 cond_resched(); 1113 } 1114 kfree(plug); 1115 } 1116 1117 /* 1118 * 1. Register the new request and wait if the reconstruction thread has put 1119 * up a bar for new requests. Continue immediately if no resync is active 1120 * currently. 1121 * 2. If IO spans the reshape position. Need to wait for reshape to pass. 1122 */ 1123 static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf, 1124 struct bio *bio, sector_t sectors) 1125 { 1126 /* Bail out if REQ_NOWAIT is set for the bio */ 1127 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) { 1128 bio_wouldblock_error(bio); 1129 return false; 1130 } 1131 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && 1132 bio->bi_iter.bi_sector < conf->reshape_progress && 1133 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) { 1134 allow_barrier(conf); 1135 if (bio->bi_opf & REQ_NOWAIT) { 1136 bio_wouldblock_error(bio); 1137 return false; 1138 } 1139 mddev_add_trace_msg(conf->mddev, "raid10 wait reshape"); 1140 wait_event(conf->wait_barrier, 1141 conf->reshape_progress <= bio->bi_iter.bi_sector || 1142 conf->reshape_progress >= bio->bi_iter.bi_sector + 1143 sectors); 1144 wait_barrier(conf, false); 1145 } 1146 return true; 1147 } 1148 1149 static void raid10_read_request(struct mddev *mddev, struct bio *bio, 1150 struct r10bio *r10_bio, bool io_accounting) 1151 { 1152 struct r10conf *conf = mddev->private; 1153 struct bio *read_bio; 1154 const enum req_op op = bio_op(bio); 1155 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC; 1156 int max_sectors; 1157 struct md_rdev *rdev; 1158 char b[BDEVNAME_SIZE]; 1159 int slot = r10_bio->read_slot; 1160 struct md_rdev *err_rdev = NULL; 1161 gfp_t gfp = GFP_NOIO; 1162 int error; 1163 1164 if (slot >= 0 && r10_bio->devs[slot].rdev) { 1165 /* 1166 * This is an error retry, but we cannot 1167 * safely dereference the rdev in the r10_bio, 1168 * we must use the one in conf. 1169 * If it has already been disconnected (unlikely) 1170 * we lose the device name in error messages. 1171 */ 1172 int disk; 1173 /* 1174 * As we are blocking raid10, it is a little safer to 1175 * use __GFP_HIGH. 1176 */ 1177 gfp = GFP_NOIO | __GFP_HIGH; 1178 1179 disk = r10_bio->devs[slot].devnum; 1180 err_rdev = conf->mirrors[disk].rdev; 1181 if (err_rdev) 1182 snprintf(b, sizeof(b), "%pg", err_rdev->bdev); 1183 else { 1184 strcpy(b, "???"); 1185 /* This never gets dereferenced */ 1186 err_rdev = r10_bio->devs[slot].rdev; 1187 } 1188 } 1189 1190 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors)) 1191 return; 1192 rdev = read_balance(conf, r10_bio, &max_sectors); 1193 if (!rdev) { 1194 if (err_rdev) { 1195 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n", 1196 mdname(mddev), b, 1197 (unsigned long long)r10_bio->sector); 1198 } 1199 raid_end_bio_io(r10_bio); 1200 return; 1201 } 1202 if (err_rdev) 1203 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n", 1204 mdname(mddev), 1205 rdev->bdev, 1206 (unsigned long long)r10_bio->sector); 1207 if (max_sectors < bio_sectors(bio)) { 1208 struct bio *split = bio_split(bio, max_sectors, 1209 gfp, &conf->bio_split); 1210 if (IS_ERR(split)) { 1211 error = PTR_ERR(split); 1212 goto err_handle; 1213 } 1214 bio_chain(split, bio); 1215 allow_barrier(conf); 1216 submit_bio_noacct(bio); 1217 wait_barrier(conf, false); 1218 bio = split; 1219 r10_bio->master_bio = bio; 1220 r10_bio->sectors = max_sectors; 1221 } 1222 slot = r10_bio->read_slot; 1223 1224 if (io_accounting) { 1225 md_account_bio(mddev, &bio); 1226 r10_bio->master_bio = bio; 1227 } 1228 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set); 1229 1230 r10_bio->devs[slot].bio = read_bio; 1231 r10_bio->devs[slot].rdev = rdev; 1232 1233 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr + 1234 choose_data_offset(r10_bio, rdev); 1235 read_bio->bi_end_io = raid10_end_read_request; 1236 read_bio->bi_opf = op | do_sync; 1237 if (test_bit(FailFast, &rdev->flags) && 1238 test_bit(R10BIO_FailFast, &r10_bio->state)) 1239 read_bio->bi_opf |= MD_FAILFAST; 1240 read_bio->bi_private = r10_bio; 1241 mddev_trace_remap(mddev, read_bio, r10_bio->sector); 1242 submit_bio_noacct(read_bio); 1243 return; 1244 err_handle: 1245 atomic_dec(&rdev->nr_pending); 1246 bio->bi_status = errno_to_blk_status(error); 1247 set_bit(R10BIO_Uptodate, &r10_bio->state); 1248 raid_end_bio_io(r10_bio); 1249 } 1250 1251 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio, 1252 struct bio *bio, bool replacement, 1253 int n_copy) 1254 { 1255 const enum req_op op = bio_op(bio); 1256 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC; 1257 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA; 1258 unsigned long flags; 1259 struct r10conf *conf = mddev->private; 1260 struct md_rdev *rdev; 1261 int devnum = r10_bio->devs[n_copy].devnum; 1262 struct bio *mbio; 1263 1264 rdev = replacement ? conf->mirrors[devnum].replacement : 1265 conf->mirrors[devnum].rdev; 1266 1267 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set); 1268 if (replacement) 1269 r10_bio->devs[n_copy].repl_bio = mbio; 1270 else 1271 r10_bio->devs[n_copy].bio = mbio; 1272 1273 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr + 1274 choose_data_offset(r10_bio, rdev)); 1275 mbio->bi_end_io = raid10_end_write_request; 1276 mbio->bi_opf = op | do_sync | do_fua; 1277 if (!replacement && test_bit(FailFast, 1278 &conf->mirrors[devnum].rdev->flags) 1279 && enough(conf, devnum)) 1280 mbio->bi_opf |= MD_FAILFAST; 1281 mbio->bi_private = r10_bio; 1282 mddev_trace_remap(mddev, mbio, r10_bio->sector); 1283 /* flush_pending_writes() needs access to the rdev so...*/ 1284 mbio->bi_bdev = (void *)rdev; 1285 1286 atomic_inc(&r10_bio->remaining); 1287 1288 if (!raid1_add_bio_to_plug(mddev, mbio, raid10_unplug, conf->copies)) { 1289 spin_lock_irqsave(&conf->device_lock, flags); 1290 bio_list_add(&conf->pending_bio_list, mbio); 1291 spin_unlock_irqrestore(&conf->device_lock, flags); 1292 md_wakeup_thread(mddev->thread); 1293 } 1294 } 1295 1296 static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio) 1297 { 1298 struct r10conf *conf = mddev->private; 1299 struct md_rdev *blocked_rdev; 1300 int i; 1301 1302 retry_wait: 1303 blocked_rdev = NULL; 1304 for (i = 0; i < conf->copies; i++) { 1305 struct md_rdev *rdev, *rrdev; 1306 1307 rdev = conf->mirrors[i].rdev; 1308 if (rdev) { 1309 sector_t dev_sector = r10_bio->devs[i].addr; 1310 1311 /* 1312 * Discard request doesn't care the write result 1313 * so it doesn't need to wait blocked disk here. 1314 */ 1315 if (test_bit(WriteErrorSeen, &rdev->flags) && 1316 r10_bio->sectors && 1317 rdev_has_badblock(rdev, dev_sector, 1318 r10_bio->sectors) < 0) 1319 /* 1320 * Mustn't write here until the bad 1321 * block is acknowledged 1322 */ 1323 set_bit(BlockedBadBlocks, &rdev->flags); 1324 1325 if (rdev_blocked(rdev)) { 1326 blocked_rdev = rdev; 1327 atomic_inc(&rdev->nr_pending); 1328 break; 1329 } 1330 } 1331 1332 rrdev = conf->mirrors[i].replacement; 1333 if (rrdev && rdev_blocked(rrdev)) { 1334 atomic_inc(&rrdev->nr_pending); 1335 blocked_rdev = rrdev; 1336 break; 1337 } 1338 } 1339 1340 if (unlikely(blocked_rdev)) { 1341 /* Have to wait for this device to get unblocked, then retry */ 1342 allow_barrier(conf); 1343 mddev_add_trace_msg(conf->mddev, 1344 "raid10 %s wait rdev %d blocked", 1345 __func__, blocked_rdev->raid_disk); 1346 md_wait_for_blocked_rdev(blocked_rdev, mddev); 1347 wait_barrier(conf, false); 1348 goto retry_wait; 1349 } 1350 } 1351 1352 static void raid10_write_request(struct mddev *mddev, struct bio *bio, 1353 struct r10bio *r10_bio) 1354 { 1355 struct r10conf *conf = mddev->private; 1356 int i, k; 1357 sector_t sectors; 1358 int max_sectors; 1359 int error; 1360 1361 if ((mddev_is_clustered(mddev) && 1362 md_cluster_ops->area_resyncing(mddev, WRITE, 1363 bio->bi_iter.bi_sector, 1364 bio_end_sector(bio)))) { 1365 DEFINE_WAIT(w); 1366 /* Bail out if REQ_NOWAIT is set for the bio */ 1367 if (bio->bi_opf & REQ_NOWAIT) { 1368 bio_wouldblock_error(bio); 1369 return; 1370 } 1371 for (;;) { 1372 prepare_to_wait(&conf->wait_barrier, 1373 &w, TASK_IDLE); 1374 if (!md_cluster_ops->area_resyncing(mddev, WRITE, 1375 bio->bi_iter.bi_sector, bio_end_sector(bio))) 1376 break; 1377 schedule(); 1378 } 1379 finish_wait(&conf->wait_barrier, &w); 1380 } 1381 1382 sectors = r10_bio->sectors; 1383 if (!regular_request_wait(mddev, conf, bio, sectors)) 1384 return; 1385 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && 1386 (mddev->reshape_backwards 1387 ? (bio->bi_iter.bi_sector < conf->reshape_safe && 1388 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) 1389 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe && 1390 bio->bi_iter.bi_sector < conf->reshape_progress))) { 1391 /* Need to update reshape_position in metadata */ 1392 mddev->reshape_position = conf->reshape_progress; 1393 set_mask_bits(&mddev->sb_flags, 0, 1394 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); 1395 md_wakeup_thread(mddev->thread); 1396 if (bio->bi_opf & REQ_NOWAIT) { 1397 allow_barrier(conf); 1398 bio_wouldblock_error(bio); 1399 return; 1400 } 1401 mddev_add_trace_msg(conf->mddev, 1402 "raid10 wait reshape metadata"); 1403 wait_event(mddev->sb_wait, 1404 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); 1405 1406 conf->reshape_safe = mddev->reshape_position; 1407 } 1408 1409 /* first select target devices under rcu_lock and 1410 * inc refcount on their rdev. Record them by setting 1411 * bios[x] to bio 1412 * If there are known/acknowledged bad blocks on any device 1413 * on which we have seen a write error, we want to avoid 1414 * writing to those blocks. This potentially requires several 1415 * writes to write around the bad blocks. Each set of writes 1416 * gets its own r10_bio with a set of bios attached. 1417 */ 1418 1419 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */ 1420 raid10_find_phys(conf, r10_bio); 1421 1422 wait_blocked_dev(mddev, r10_bio); 1423 1424 max_sectors = r10_bio->sectors; 1425 1426 for (i = 0; i < conf->copies; i++) { 1427 int d = r10_bio->devs[i].devnum; 1428 struct md_rdev *rdev, *rrdev; 1429 1430 rdev = conf->mirrors[d].rdev; 1431 rrdev = conf->mirrors[d].replacement; 1432 if (rdev && (test_bit(Faulty, &rdev->flags))) 1433 rdev = NULL; 1434 if (rrdev && (test_bit(Faulty, &rrdev->flags))) 1435 rrdev = NULL; 1436 1437 r10_bio->devs[i].bio = NULL; 1438 r10_bio->devs[i].repl_bio = NULL; 1439 1440 if (!rdev && !rrdev) { 1441 set_bit(R10BIO_Degraded, &r10_bio->state); 1442 continue; 1443 } 1444 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) { 1445 sector_t first_bad; 1446 sector_t dev_sector = r10_bio->devs[i].addr; 1447 int bad_sectors; 1448 int is_bad; 1449 1450 is_bad = is_badblock(rdev, dev_sector, max_sectors, 1451 &first_bad, &bad_sectors); 1452 if (is_bad && first_bad <= dev_sector) { 1453 /* Cannot write here at all */ 1454 bad_sectors -= (dev_sector - first_bad); 1455 if (bad_sectors < max_sectors) 1456 /* Mustn't write more than bad_sectors 1457 * to other devices yet 1458 */ 1459 max_sectors = bad_sectors; 1460 /* We don't set R10BIO_Degraded as that 1461 * only applies if the disk is missing, 1462 * so it might be re-added, and we want to 1463 * know to recover this chunk. 1464 * In this case the device is here, and the 1465 * fact that this chunk is not in-sync is 1466 * recorded in the bad block log. 1467 */ 1468 continue; 1469 } 1470 if (is_bad) { 1471 int good_sectors = first_bad - dev_sector; 1472 if (good_sectors < max_sectors) 1473 max_sectors = good_sectors; 1474 } 1475 } 1476 if (rdev) { 1477 r10_bio->devs[i].bio = bio; 1478 atomic_inc(&rdev->nr_pending); 1479 } 1480 if (rrdev) { 1481 r10_bio->devs[i].repl_bio = bio; 1482 atomic_inc(&rrdev->nr_pending); 1483 } 1484 } 1485 1486 if (max_sectors < r10_bio->sectors) 1487 r10_bio->sectors = max_sectors; 1488 1489 if (r10_bio->sectors < bio_sectors(bio)) { 1490 struct bio *split = bio_split(bio, r10_bio->sectors, 1491 GFP_NOIO, &conf->bio_split); 1492 if (IS_ERR(split)) { 1493 error = PTR_ERR(split); 1494 goto err_handle; 1495 } 1496 bio_chain(split, bio); 1497 allow_barrier(conf); 1498 submit_bio_noacct(bio); 1499 wait_barrier(conf, false); 1500 bio = split; 1501 r10_bio->master_bio = bio; 1502 } 1503 1504 md_account_bio(mddev, &bio); 1505 r10_bio->master_bio = bio; 1506 atomic_set(&r10_bio->remaining, 1); 1507 mddev->bitmap_ops->startwrite(mddev, r10_bio->sector, r10_bio->sectors, 1508 false); 1509 1510 for (i = 0; i < conf->copies; i++) { 1511 if (r10_bio->devs[i].bio) 1512 raid10_write_one_disk(mddev, r10_bio, bio, false, i); 1513 if (r10_bio->devs[i].repl_bio) 1514 raid10_write_one_disk(mddev, r10_bio, bio, true, i); 1515 } 1516 one_write_done(r10_bio); 1517 return; 1518 err_handle: 1519 for (k = 0; k < i; k++) { 1520 int d = r10_bio->devs[k].devnum; 1521 struct md_rdev *rdev = conf->mirrors[d].rdev; 1522 struct md_rdev *rrdev = conf->mirrors[d].replacement; 1523 1524 if (r10_bio->devs[k].bio) { 1525 rdev_dec_pending(rdev, mddev); 1526 r10_bio->devs[k].bio = NULL; 1527 } 1528 if (r10_bio->devs[k].repl_bio) { 1529 rdev_dec_pending(rrdev, mddev); 1530 r10_bio->devs[k].repl_bio = NULL; 1531 } 1532 } 1533 1534 bio->bi_status = errno_to_blk_status(error); 1535 set_bit(R10BIO_Uptodate, &r10_bio->state); 1536 raid_end_bio_io(r10_bio); 1537 } 1538 1539 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors) 1540 { 1541 struct r10conf *conf = mddev->private; 1542 struct r10bio *r10_bio; 1543 1544 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO); 1545 1546 r10_bio->master_bio = bio; 1547 r10_bio->sectors = sectors; 1548 1549 r10_bio->mddev = mddev; 1550 r10_bio->sector = bio->bi_iter.bi_sector; 1551 r10_bio->state = 0; 1552 r10_bio->read_slot = -1; 1553 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * 1554 conf->geo.raid_disks); 1555 1556 if (bio_data_dir(bio) == READ) 1557 raid10_read_request(mddev, bio, r10_bio, true); 1558 else 1559 raid10_write_request(mddev, bio, r10_bio); 1560 } 1561 1562 static void raid_end_discard_bio(struct r10bio *r10bio) 1563 { 1564 struct r10conf *conf = r10bio->mddev->private; 1565 struct r10bio *first_r10bio; 1566 1567 while (atomic_dec_and_test(&r10bio->remaining)) { 1568 1569 allow_barrier(conf); 1570 1571 if (!test_bit(R10BIO_Discard, &r10bio->state)) { 1572 first_r10bio = (struct r10bio *)r10bio->master_bio; 1573 free_r10bio(r10bio); 1574 r10bio = first_r10bio; 1575 } else { 1576 md_write_end(r10bio->mddev); 1577 bio_endio(r10bio->master_bio); 1578 free_r10bio(r10bio); 1579 break; 1580 } 1581 } 1582 } 1583 1584 static void raid10_end_discard_request(struct bio *bio) 1585 { 1586 struct r10bio *r10_bio = bio->bi_private; 1587 struct r10conf *conf = r10_bio->mddev->private; 1588 struct md_rdev *rdev = NULL; 1589 int dev; 1590 int slot, repl; 1591 1592 /* 1593 * We don't care the return value of discard bio 1594 */ 1595 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) 1596 set_bit(R10BIO_Uptodate, &r10_bio->state); 1597 1598 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 1599 rdev = repl ? conf->mirrors[dev].replacement : 1600 conf->mirrors[dev].rdev; 1601 1602 raid_end_discard_bio(r10_bio); 1603 rdev_dec_pending(rdev, conf->mddev); 1604 } 1605 1606 /* 1607 * There are some limitations to handle discard bio 1608 * 1st, the discard size is bigger than stripe_size*2. 1609 * 2st, if the discard bio spans reshape progress, we use the old way to 1610 * handle discard bio 1611 */ 1612 static int raid10_handle_discard(struct mddev *mddev, struct bio *bio) 1613 { 1614 struct r10conf *conf = mddev->private; 1615 struct geom *geo = &conf->geo; 1616 int far_copies = geo->far_copies; 1617 bool first_copy = true; 1618 struct r10bio *r10_bio, *first_r10bio; 1619 struct bio *split; 1620 int disk; 1621 sector_t chunk; 1622 unsigned int stripe_size; 1623 unsigned int stripe_data_disks; 1624 sector_t split_size; 1625 sector_t bio_start, bio_end; 1626 sector_t first_stripe_index, last_stripe_index; 1627 sector_t start_disk_offset; 1628 unsigned int start_disk_index; 1629 sector_t end_disk_offset; 1630 unsigned int end_disk_index; 1631 unsigned int remainder; 1632 1633 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 1634 return -EAGAIN; 1635 1636 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) { 1637 bio_wouldblock_error(bio); 1638 return 0; 1639 } 1640 wait_barrier(conf, false); 1641 1642 /* 1643 * Check reshape again to avoid reshape happens after checking 1644 * MD_RECOVERY_RESHAPE and before wait_barrier 1645 */ 1646 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 1647 goto out; 1648 1649 if (geo->near_copies) 1650 stripe_data_disks = geo->raid_disks / geo->near_copies + 1651 geo->raid_disks % geo->near_copies; 1652 else 1653 stripe_data_disks = geo->raid_disks; 1654 1655 stripe_size = stripe_data_disks << geo->chunk_shift; 1656 1657 bio_start = bio->bi_iter.bi_sector; 1658 bio_end = bio_end_sector(bio); 1659 1660 /* 1661 * Maybe one discard bio is smaller than strip size or across one 1662 * stripe and discard region is larger than one stripe size. For far 1663 * offset layout, if the discard region is not aligned with stripe 1664 * size, there is hole when we submit discard bio to member disk. 1665 * For simplicity, we only handle discard bio which discard region 1666 * is bigger than stripe_size * 2 1667 */ 1668 if (bio_sectors(bio) < stripe_size*2) 1669 goto out; 1670 1671 /* 1672 * Keep bio aligned with strip size. 1673 */ 1674 div_u64_rem(bio_start, stripe_size, &remainder); 1675 if (remainder) { 1676 split_size = stripe_size - remainder; 1677 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split); 1678 if (IS_ERR(split)) { 1679 bio->bi_status = errno_to_blk_status(PTR_ERR(split)); 1680 bio_endio(bio); 1681 return 0; 1682 } 1683 bio_chain(split, bio); 1684 allow_barrier(conf); 1685 /* Resend the fist split part */ 1686 submit_bio_noacct(split); 1687 wait_barrier(conf, false); 1688 } 1689 div_u64_rem(bio_end, stripe_size, &remainder); 1690 if (remainder) { 1691 split_size = bio_sectors(bio) - remainder; 1692 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split); 1693 if (IS_ERR(split)) { 1694 bio->bi_status = errno_to_blk_status(PTR_ERR(split)); 1695 bio_endio(bio); 1696 return 0; 1697 } 1698 bio_chain(split, bio); 1699 allow_barrier(conf); 1700 /* Resend the second split part */ 1701 submit_bio_noacct(bio); 1702 bio = split; 1703 wait_barrier(conf, false); 1704 } 1705 1706 bio_start = bio->bi_iter.bi_sector; 1707 bio_end = bio_end_sector(bio); 1708 1709 /* 1710 * Raid10 uses chunk as the unit to store data. It's similar like raid0. 1711 * One stripe contains the chunks from all member disk (one chunk from 1712 * one disk at the same HBA address). For layout detail, see 'man md 4' 1713 */ 1714 chunk = bio_start >> geo->chunk_shift; 1715 chunk *= geo->near_copies; 1716 first_stripe_index = chunk; 1717 start_disk_index = sector_div(first_stripe_index, geo->raid_disks); 1718 if (geo->far_offset) 1719 first_stripe_index *= geo->far_copies; 1720 start_disk_offset = (bio_start & geo->chunk_mask) + 1721 (first_stripe_index << geo->chunk_shift); 1722 1723 chunk = bio_end >> geo->chunk_shift; 1724 chunk *= geo->near_copies; 1725 last_stripe_index = chunk; 1726 end_disk_index = sector_div(last_stripe_index, geo->raid_disks); 1727 if (geo->far_offset) 1728 last_stripe_index *= geo->far_copies; 1729 end_disk_offset = (bio_end & geo->chunk_mask) + 1730 (last_stripe_index << geo->chunk_shift); 1731 1732 retry_discard: 1733 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO); 1734 r10_bio->mddev = mddev; 1735 r10_bio->state = 0; 1736 r10_bio->sectors = 0; 1737 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks); 1738 wait_blocked_dev(mddev, r10_bio); 1739 1740 /* 1741 * For far layout it needs more than one r10bio to cover all regions. 1742 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio 1743 * to record the discard bio. Other r10bio->master_bio record the first 1744 * r10bio. The first r10bio only release after all other r10bios finish. 1745 * The discard bio returns only first r10bio finishes 1746 */ 1747 if (first_copy) { 1748 r10_bio->master_bio = bio; 1749 set_bit(R10BIO_Discard, &r10_bio->state); 1750 first_copy = false; 1751 first_r10bio = r10_bio; 1752 } else 1753 r10_bio->master_bio = (struct bio *)first_r10bio; 1754 1755 /* 1756 * first select target devices under rcu_lock and 1757 * inc refcount on their rdev. Record them by setting 1758 * bios[x] to bio 1759 */ 1760 for (disk = 0; disk < geo->raid_disks; disk++) { 1761 struct md_rdev *rdev, *rrdev; 1762 1763 rdev = conf->mirrors[disk].rdev; 1764 rrdev = conf->mirrors[disk].replacement; 1765 r10_bio->devs[disk].bio = NULL; 1766 r10_bio->devs[disk].repl_bio = NULL; 1767 1768 if (rdev && (test_bit(Faulty, &rdev->flags))) 1769 rdev = NULL; 1770 if (rrdev && (test_bit(Faulty, &rrdev->flags))) 1771 rrdev = NULL; 1772 if (!rdev && !rrdev) 1773 continue; 1774 1775 if (rdev) { 1776 r10_bio->devs[disk].bio = bio; 1777 atomic_inc(&rdev->nr_pending); 1778 } 1779 if (rrdev) { 1780 r10_bio->devs[disk].repl_bio = bio; 1781 atomic_inc(&rrdev->nr_pending); 1782 } 1783 } 1784 1785 atomic_set(&r10_bio->remaining, 1); 1786 for (disk = 0; disk < geo->raid_disks; disk++) { 1787 sector_t dev_start, dev_end; 1788 struct bio *mbio, *rbio = NULL; 1789 1790 /* 1791 * Now start to calculate the start and end address for each disk. 1792 * The space between dev_start and dev_end is the discard region. 1793 * 1794 * For dev_start, it needs to consider three conditions: 1795 * 1st, the disk is before start_disk, you can imagine the disk in 1796 * the next stripe. So the dev_start is the start address of next 1797 * stripe. 1798 * 2st, the disk is after start_disk, it means the disk is at the 1799 * same stripe of first disk 1800 * 3st, the first disk itself, we can use start_disk_offset directly 1801 */ 1802 if (disk < start_disk_index) 1803 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors; 1804 else if (disk > start_disk_index) 1805 dev_start = first_stripe_index * mddev->chunk_sectors; 1806 else 1807 dev_start = start_disk_offset; 1808 1809 if (disk < end_disk_index) 1810 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors; 1811 else if (disk > end_disk_index) 1812 dev_end = last_stripe_index * mddev->chunk_sectors; 1813 else 1814 dev_end = end_disk_offset; 1815 1816 /* 1817 * It only handles discard bio which size is >= stripe size, so 1818 * dev_end > dev_start all the time. 1819 * It doesn't need to use rcu lock to get rdev here. We already 1820 * add rdev->nr_pending in the first loop. 1821 */ 1822 if (r10_bio->devs[disk].bio) { 1823 struct md_rdev *rdev = conf->mirrors[disk].rdev; 1824 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, 1825 &mddev->bio_set); 1826 mbio->bi_end_io = raid10_end_discard_request; 1827 mbio->bi_private = r10_bio; 1828 r10_bio->devs[disk].bio = mbio; 1829 r10_bio->devs[disk].devnum = disk; 1830 atomic_inc(&r10_bio->remaining); 1831 md_submit_discard_bio(mddev, rdev, mbio, 1832 dev_start + choose_data_offset(r10_bio, rdev), 1833 dev_end - dev_start); 1834 bio_endio(mbio); 1835 } 1836 if (r10_bio->devs[disk].repl_bio) { 1837 struct md_rdev *rrdev = conf->mirrors[disk].replacement; 1838 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, 1839 &mddev->bio_set); 1840 rbio->bi_end_io = raid10_end_discard_request; 1841 rbio->bi_private = r10_bio; 1842 r10_bio->devs[disk].repl_bio = rbio; 1843 r10_bio->devs[disk].devnum = disk; 1844 atomic_inc(&r10_bio->remaining); 1845 md_submit_discard_bio(mddev, rrdev, rbio, 1846 dev_start + choose_data_offset(r10_bio, rrdev), 1847 dev_end - dev_start); 1848 bio_endio(rbio); 1849 } 1850 } 1851 1852 if (!geo->far_offset && --far_copies) { 1853 first_stripe_index += geo->stride >> geo->chunk_shift; 1854 start_disk_offset += geo->stride; 1855 last_stripe_index += geo->stride >> geo->chunk_shift; 1856 end_disk_offset += geo->stride; 1857 atomic_inc(&first_r10bio->remaining); 1858 raid_end_discard_bio(r10_bio); 1859 wait_barrier(conf, false); 1860 goto retry_discard; 1861 } 1862 1863 raid_end_discard_bio(r10_bio); 1864 1865 return 0; 1866 out: 1867 allow_barrier(conf); 1868 return -EAGAIN; 1869 } 1870 1871 static bool raid10_make_request(struct mddev *mddev, struct bio *bio) 1872 { 1873 struct r10conf *conf = mddev->private; 1874 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask); 1875 int chunk_sects = chunk_mask + 1; 1876 int sectors = bio_sectors(bio); 1877 1878 if (unlikely(bio->bi_opf & REQ_PREFLUSH) 1879 && md_flush_request(mddev, bio)) 1880 return true; 1881 1882 md_write_start(mddev, bio); 1883 1884 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) 1885 if (!raid10_handle_discard(mddev, bio)) 1886 return true; 1887 1888 /* 1889 * If this request crosses a chunk boundary, we need to split 1890 * it. 1891 */ 1892 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) + 1893 sectors > chunk_sects 1894 && (conf->geo.near_copies < conf->geo.raid_disks 1895 || conf->prev.near_copies < 1896 conf->prev.raid_disks))) 1897 sectors = chunk_sects - 1898 (bio->bi_iter.bi_sector & 1899 (chunk_sects - 1)); 1900 __make_request(mddev, bio, sectors); 1901 1902 /* In case raid10d snuck in to freeze_array */ 1903 wake_up_barrier(conf); 1904 return true; 1905 } 1906 1907 static void raid10_status(struct seq_file *seq, struct mddev *mddev) 1908 { 1909 struct r10conf *conf = mddev->private; 1910 int i; 1911 1912 lockdep_assert_held(&mddev->lock); 1913 1914 if (conf->geo.near_copies < conf->geo.raid_disks) 1915 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); 1916 if (conf->geo.near_copies > 1) 1917 seq_printf(seq, " %d near-copies", conf->geo.near_copies); 1918 if (conf->geo.far_copies > 1) { 1919 if (conf->geo.far_offset) 1920 seq_printf(seq, " %d offset-copies", conf->geo.far_copies); 1921 else 1922 seq_printf(seq, " %d far-copies", conf->geo.far_copies); 1923 if (conf->geo.far_set_size != conf->geo.raid_disks) 1924 seq_printf(seq, " %d devices per set", conf->geo.far_set_size); 1925 } 1926 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks, 1927 conf->geo.raid_disks - mddev->degraded); 1928 for (i = 0; i < conf->geo.raid_disks; i++) { 1929 struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev); 1930 1931 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 1932 } 1933 seq_printf(seq, "]"); 1934 } 1935 1936 /* check if there are enough drives for 1937 * every block to appear on atleast one. 1938 * Don't consider the device numbered 'ignore' 1939 * as we might be about to remove it. 1940 */ 1941 static int _enough(struct r10conf *conf, int previous, int ignore) 1942 { 1943 int first = 0; 1944 int has_enough = 0; 1945 int disks, ncopies; 1946 if (previous) { 1947 disks = conf->prev.raid_disks; 1948 ncopies = conf->prev.near_copies; 1949 } else { 1950 disks = conf->geo.raid_disks; 1951 ncopies = conf->geo.near_copies; 1952 } 1953 1954 do { 1955 int n = conf->copies; 1956 int cnt = 0; 1957 int this = first; 1958 while (n--) { 1959 struct md_rdev *rdev; 1960 if (this != ignore && 1961 (rdev = conf->mirrors[this].rdev) && 1962 test_bit(In_sync, &rdev->flags)) 1963 cnt++; 1964 this = (this+1) % disks; 1965 } 1966 if (cnt == 0) 1967 goto out; 1968 first = (first + ncopies) % disks; 1969 } while (first != 0); 1970 has_enough = 1; 1971 out: 1972 return has_enough; 1973 } 1974 1975 static int enough(struct r10conf *conf, int ignore) 1976 { 1977 /* when calling 'enough', both 'prev' and 'geo' must 1978 * be stable. 1979 * This is ensured if ->reconfig_mutex or ->device_lock 1980 * is held. 1981 */ 1982 return _enough(conf, 0, ignore) && 1983 _enough(conf, 1, ignore); 1984 } 1985 1986 /** 1987 * raid10_error() - RAID10 error handler. 1988 * @mddev: affected md device. 1989 * @rdev: member device to fail. 1990 * 1991 * The routine acknowledges &rdev failure and determines new @mddev state. 1992 * If it failed, then: 1993 * - &MD_BROKEN flag is set in &mddev->flags. 1994 * Otherwise, it must be degraded: 1995 * - recovery is interrupted. 1996 * - &mddev->degraded is bumped. 1997 * 1998 * @rdev is marked as &Faulty excluding case when array is failed and 1999 * &mddev->fail_last_dev is off. 2000 */ 2001 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev) 2002 { 2003 struct r10conf *conf = mddev->private; 2004 unsigned long flags; 2005 2006 spin_lock_irqsave(&conf->device_lock, flags); 2007 2008 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) { 2009 set_bit(MD_BROKEN, &mddev->flags); 2010 2011 if (!mddev->fail_last_dev) { 2012 spin_unlock_irqrestore(&conf->device_lock, flags); 2013 return; 2014 } 2015 } 2016 if (test_and_clear_bit(In_sync, &rdev->flags)) 2017 mddev->degraded++; 2018 2019 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 2020 set_bit(Blocked, &rdev->flags); 2021 set_bit(Faulty, &rdev->flags); 2022 set_mask_bits(&mddev->sb_flags, 0, 2023 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); 2024 spin_unlock_irqrestore(&conf->device_lock, flags); 2025 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n" 2026 "md/raid10:%s: Operation continuing on %d devices.\n", 2027 mdname(mddev), rdev->bdev, 2028 mdname(mddev), conf->geo.raid_disks - mddev->degraded); 2029 } 2030 2031 static void print_conf(struct r10conf *conf) 2032 { 2033 int i; 2034 struct md_rdev *rdev; 2035 2036 pr_debug("RAID10 conf printout:\n"); 2037 if (!conf) { 2038 pr_debug("(!conf)\n"); 2039 return; 2040 } 2041 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded, 2042 conf->geo.raid_disks); 2043 2044 lockdep_assert_held(&conf->mddev->reconfig_mutex); 2045 for (i = 0; i < conf->geo.raid_disks; i++) { 2046 rdev = conf->mirrors[i].rdev; 2047 if (rdev) 2048 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n", 2049 i, !test_bit(In_sync, &rdev->flags), 2050 !test_bit(Faulty, &rdev->flags), 2051 rdev->bdev); 2052 } 2053 } 2054 2055 static void close_sync(struct r10conf *conf) 2056 { 2057 wait_barrier(conf, false); 2058 allow_barrier(conf); 2059 2060 mempool_exit(&conf->r10buf_pool); 2061 } 2062 2063 static int raid10_spare_active(struct mddev *mddev) 2064 { 2065 int i; 2066 struct r10conf *conf = mddev->private; 2067 struct raid10_info *tmp; 2068 int count = 0; 2069 unsigned long flags; 2070 2071 /* 2072 * Find all non-in_sync disks within the RAID10 configuration 2073 * and mark them in_sync 2074 */ 2075 for (i = 0; i < conf->geo.raid_disks; i++) { 2076 tmp = conf->mirrors + i; 2077 if (tmp->replacement 2078 && tmp->replacement->recovery_offset == MaxSector 2079 && !test_bit(Faulty, &tmp->replacement->flags) 2080 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { 2081 /* Replacement has just become active */ 2082 if (!tmp->rdev 2083 || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) 2084 count++; 2085 if (tmp->rdev) { 2086 /* Replaced device not technically faulty, 2087 * but we need to be sure it gets removed 2088 * and never re-added. 2089 */ 2090 set_bit(Faulty, &tmp->rdev->flags); 2091 sysfs_notify_dirent_safe( 2092 tmp->rdev->sysfs_state); 2093 } 2094 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); 2095 } else if (tmp->rdev 2096 && tmp->rdev->recovery_offset == MaxSector 2097 && !test_bit(Faulty, &tmp->rdev->flags) 2098 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { 2099 count++; 2100 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); 2101 } 2102 } 2103 spin_lock_irqsave(&conf->device_lock, flags); 2104 mddev->degraded -= count; 2105 spin_unlock_irqrestore(&conf->device_lock, flags); 2106 2107 print_conf(conf); 2108 return count; 2109 } 2110 2111 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) 2112 { 2113 struct r10conf *conf = mddev->private; 2114 int err = -EEXIST; 2115 int mirror, repl_slot = -1; 2116 int first = 0; 2117 int last = conf->geo.raid_disks - 1; 2118 struct raid10_info *p; 2119 2120 if (mddev->recovery_cp < MaxSector) 2121 /* only hot-add to in-sync arrays, as recovery is 2122 * very different from resync 2123 */ 2124 return -EBUSY; 2125 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1)) 2126 return -EINVAL; 2127 2128 if (rdev->raid_disk >= 0) 2129 first = last = rdev->raid_disk; 2130 2131 if (rdev->saved_raid_disk >= first && 2132 rdev->saved_raid_disk < conf->geo.raid_disks && 2133 conf->mirrors[rdev->saved_raid_disk].rdev == NULL) 2134 mirror = rdev->saved_raid_disk; 2135 else 2136 mirror = first; 2137 for ( ; mirror <= last ; mirror++) { 2138 p = &conf->mirrors[mirror]; 2139 if (p->recovery_disabled == mddev->recovery_disabled) 2140 continue; 2141 if (p->rdev) { 2142 if (test_bit(WantReplacement, &p->rdev->flags) && 2143 p->replacement == NULL && repl_slot < 0) 2144 repl_slot = mirror; 2145 continue; 2146 } 2147 2148 err = mddev_stack_new_rdev(mddev, rdev); 2149 if (err) 2150 return err; 2151 p->head_position = 0; 2152 p->recovery_disabled = mddev->recovery_disabled - 1; 2153 rdev->raid_disk = mirror; 2154 err = 0; 2155 if (rdev->saved_raid_disk != mirror) 2156 conf->fullsync = 1; 2157 WRITE_ONCE(p->rdev, rdev); 2158 break; 2159 } 2160 2161 if (err && repl_slot >= 0) { 2162 p = &conf->mirrors[repl_slot]; 2163 clear_bit(In_sync, &rdev->flags); 2164 set_bit(Replacement, &rdev->flags); 2165 rdev->raid_disk = repl_slot; 2166 err = mddev_stack_new_rdev(mddev, rdev); 2167 if (err) 2168 return err; 2169 conf->fullsync = 1; 2170 WRITE_ONCE(p->replacement, rdev); 2171 } 2172 2173 print_conf(conf); 2174 return err; 2175 } 2176 2177 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 2178 { 2179 struct r10conf *conf = mddev->private; 2180 int err = 0; 2181 int number = rdev->raid_disk; 2182 struct md_rdev **rdevp; 2183 struct raid10_info *p; 2184 2185 print_conf(conf); 2186 if (unlikely(number >= mddev->raid_disks)) 2187 return 0; 2188 p = conf->mirrors + number; 2189 if (rdev == p->rdev) 2190 rdevp = &p->rdev; 2191 else if (rdev == p->replacement) 2192 rdevp = &p->replacement; 2193 else 2194 return 0; 2195 2196 if (test_bit(In_sync, &rdev->flags) || 2197 atomic_read(&rdev->nr_pending)) { 2198 err = -EBUSY; 2199 goto abort; 2200 } 2201 /* Only remove non-faulty devices if recovery 2202 * is not possible. 2203 */ 2204 if (!test_bit(Faulty, &rdev->flags) && 2205 mddev->recovery_disabled != p->recovery_disabled && 2206 (!p->replacement || p->replacement == rdev) && 2207 number < conf->geo.raid_disks && 2208 enough(conf, -1)) { 2209 err = -EBUSY; 2210 goto abort; 2211 } 2212 WRITE_ONCE(*rdevp, NULL); 2213 if (p->replacement) { 2214 /* We must have just cleared 'rdev' */ 2215 WRITE_ONCE(p->rdev, p->replacement); 2216 clear_bit(Replacement, &p->replacement->flags); 2217 WRITE_ONCE(p->replacement, NULL); 2218 } 2219 2220 clear_bit(WantReplacement, &rdev->flags); 2221 err = md_integrity_register(mddev); 2222 2223 abort: 2224 2225 print_conf(conf); 2226 return err; 2227 } 2228 2229 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d) 2230 { 2231 struct r10conf *conf = r10_bio->mddev->private; 2232 2233 if (!bio->bi_status) 2234 set_bit(R10BIO_Uptodate, &r10_bio->state); 2235 else 2236 /* The write handler will notice the lack of 2237 * R10BIO_Uptodate and record any errors etc 2238 */ 2239 atomic_add(r10_bio->sectors, 2240 &conf->mirrors[d].rdev->corrected_errors); 2241 2242 /* for reconstruct, we always reschedule after a read. 2243 * for resync, only after all reads 2244 */ 2245 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); 2246 if (test_bit(R10BIO_IsRecover, &r10_bio->state) || 2247 atomic_dec_and_test(&r10_bio->remaining)) { 2248 /* we have read all the blocks, 2249 * do the comparison in process context in raid10d 2250 */ 2251 reschedule_retry(r10_bio); 2252 } 2253 } 2254 2255 static void end_sync_read(struct bio *bio) 2256 { 2257 struct r10bio *r10_bio = get_resync_r10bio(bio); 2258 struct r10conf *conf = r10_bio->mddev->private; 2259 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); 2260 2261 __end_sync_read(r10_bio, bio, d); 2262 } 2263 2264 static void end_reshape_read(struct bio *bio) 2265 { 2266 /* reshape read bio isn't allocated from r10buf_pool */ 2267 struct r10bio *r10_bio = bio->bi_private; 2268 2269 __end_sync_read(r10_bio, bio, r10_bio->read_slot); 2270 } 2271 2272 static void end_sync_request(struct r10bio *r10_bio) 2273 { 2274 struct mddev *mddev = r10_bio->mddev; 2275 2276 while (atomic_dec_and_test(&r10_bio->remaining)) { 2277 if (r10_bio->master_bio == NULL) { 2278 /* the primary of several recovery bios */ 2279 sector_t s = r10_bio->sectors; 2280 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 2281 test_bit(R10BIO_WriteError, &r10_bio->state)) 2282 reschedule_retry(r10_bio); 2283 else 2284 put_buf(r10_bio); 2285 md_done_sync(mddev, s, 1); 2286 break; 2287 } else { 2288 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; 2289 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 2290 test_bit(R10BIO_WriteError, &r10_bio->state)) 2291 reschedule_retry(r10_bio); 2292 else 2293 put_buf(r10_bio); 2294 r10_bio = r10_bio2; 2295 } 2296 } 2297 } 2298 2299 static void end_sync_write(struct bio *bio) 2300 { 2301 struct r10bio *r10_bio = get_resync_r10bio(bio); 2302 struct mddev *mddev = r10_bio->mddev; 2303 struct r10conf *conf = mddev->private; 2304 int d; 2305 int slot; 2306 int repl; 2307 struct md_rdev *rdev = NULL; 2308 2309 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 2310 if (repl) 2311 rdev = conf->mirrors[d].replacement; 2312 else 2313 rdev = conf->mirrors[d].rdev; 2314 2315 if (bio->bi_status) { 2316 if (repl) 2317 md_error(mddev, rdev); 2318 else { 2319 set_bit(WriteErrorSeen, &rdev->flags); 2320 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 2321 set_bit(MD_RECOVERY_NEEDED, 2322 &rdev->mddev->recovery); 2323 set_bit(R10BIO_WriteError, &r10_bio->state); 2324 } 2325 } else if (rdev_has_badblock(rdev, r10_bio->devs[slot].addr, 2326 r10_bio->sectors)) { 2327 set_bit(R10BIO_MadeGood, &r10_bio->state); 2328 } 2329 2330 rdev_dec_pending(rdev, mddev); 2331 2332 end_sync_request(r10_bio); 2333 } 2334 2335 /* 2336 * Note: sync and recover and handled very differently for raid10 2337 * This code is for resync. 2338 * For resync, we read through virtual addresses and read all blocks. 2339 * If there is any error, we schedule a write. The lowest numbered 2340 * drive is authoritative. 2341 * However requests come for physical address, so we need to map. 2342 * For every physical address there are raid_disks/copies virtual addresses, 2343 * which is always are least one, but is not necessarly an integer. 2344 * This means that a physical address can span multiple chunks, so we may 2345 * have to submit multiple io requests for a single sync request. 2346 */ 2347 /* 2348 * We check if all blocks are in-sync and only write to blocks that 2349 * aren't in sync 2350 */ 2351 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) 2352 { 2353 struct r10conf *conf = mddev->private; 2354 int i, first; 2355 struct bio *tbio, *fbio; 2356 int vcnt; 2357 struct page **tpages, **fpages; 2358 2359 atomic_set(&r10_bio->remaining, 1); 2360 2361 /* find the first device with a block */ 2362 for (i=0; i<conf->copies; i++) 2363 if (!r10_bio->devs[i].bio->bi_status) 2364 break; 2365 2366 if (i == conf->copies) 2367 goto done; 2368 2369 first = i; 2370 fbio = r10_bio->devs[i].bio; 2371 fbio->bi_iter.bi_size = r10_bio->sectors << 9; 2372 fbio->bi_iter.bi_idx = 0; 2373 fpages = get_resync_pages(fbio)->pages; 2374 2375 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9); 2376 /* now find blocks with errors */ 2377 for (i=0 ; i < conf->copies ; i++) { 2378 int j, d; 2379 struct md_rdev *rdev; 2380 struct resync_pages *rp; 2381 2382 tbio = r10_bio->devs[i].bio; 2383 2384 if (tbio->bi_end_io != end_sync_read) 2385 continue; 2386 if (i == first) 2387 continue; 2388 2389 tpages = get_resync_pages(tbio)->pages; 2390 d = r10_bio->devs[i].devnum; 2391 rdev = conf->mirrors[d].rdev; 2392 if (!r10_bio->devs[i].bio->bi_status) { 2393 /* We know that the bi_io_vec layout is the same for 2394 * both 'first' and 'i', so we just compare them. 2395 * All vec entries are PAGE_SIZE; 2396 */ 2397 int sectors = r10_bio->sectors; 2398 for (j = 0; j < vcnt; j++) { 2399 int len = PAGE_SIZE; 2400 if (sectors < (len / 512)) 2401 len = sectors * 512; 2402 if (memcmp(page_address(fpages[j]), 2403 page_address(tpages[j]), 2404 len)) 2405 break; 2406 sectors -= len/512; 2407 } 2408 if (j == vcnt) 2409 continue; 2410 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches); 2411 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) 2412 /* Don't fix anything. */ 2413 continue; 2414 } else if (test_bit(FailFast, &rdev->flags)) { 2415 /* Just give up on this device */ 2416 md_error(rdev->mddev, rdev); 2417 continue; 2418 } 2419 /* Ok, we need to write this bio, either to correct an 2420 * inconsistency or to correct an unreadable block. 2421 * First we need to fixup bv_offset, bv_len and 2422 * bi_vecs, as the read request might have corrupted these 2423 */ 2424 rp = get_resync_pages(tbio); 2425 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE); 2426 2427 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size); 2428 2429 rp->raid_bio = r10_bio; 2430 tbio->bi_private = rp; 2431 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr; 2432 tbio->bi_end_io = end_sync_write; 2433 2434 bio_copy_data(tbio, fbio); 2435 2436 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 2437 atomic_inc(&r10_bio->remaining); 2438 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio)); 2439 2440 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags)) 2441 tbio->bi_opf |= MD_FAILFAST; 2442 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset; 2443 submit_bio_noacct(tbio); 2444 } 2445 2446 /* Now write out to any replacement devices 2447 * that are active 2448 */ 2449 for (i = 0; i < conf->copies; i++) { 2450 int d; 2451 2452 tbio = r10_bio->devs[i].repl_bio; 2453 if (!tbio || !tbio->bi_end_io) 2454 continue; 2455 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write 2456 && r10_bio->devs[i].bio != fbio) 2457 bio_copy_data(tbio, fbio); 2458 d = r10_bio->devs[i].devnum; 2459 atomic_inc(&r10_bio->remaining); 2460 md_sync_acct(conf->mirrors[d].replacement->bdev, 2461 bio_sectors(tbio)); 2462 submit_bio_noacct(tbio); 2463 } 2464 2465 done: 2466 if (atomic_dec_and_test(&r10_bio->remaining)) { 2467 md_done_sync(mddev, r10_bio->sectors, 1); 2468 put_buf(r10_bio); 2469 } 2470 } 2471 2472 /* 2473 * Now for the recovery code. 2474 * Recovery happens across physical sectors. 2475 * We recover all non-is_sync drives by finding the virtual address of 2476 * each, and then choose a working drive that also has that virt address. 2477 * There is a separate r10_bio for each non-in_sync drive. 2478 * Only the first two slots are in use. The first for reading, 2479 * The second for writing. 2480 * 2481 */ 2482 static void fix_recovery_read_error(struct r10bio *r10_bio) 2483 { 2484 /* We got a read error during recovery. 2485 * We repeat the read in smaller page-sized sections. 2486 * If a read succeeds, write it to the new device or record 2487 * a bad block if we cannot. 2488 * If a read fails, record a bad block on both old and 2489 * new devices. 2490 */ 2491 struct mddev *mddev = r10_bio->mddev; 2492 struct r10conf *conf = mddev->private; 2493 struct bio *bio = r10_bio->devs[0].bio; 2494 sector_t sect = 0; 2495 int sectors = r10_bio->sectors; 2496 int idx = 0; 2497 int dr = r10_bio->devs[0].devnum; 2498 int dw = r10_bio->devs[1].devnum; 2499 struct page **pages = get_resync_pages(bio)->pages; 2500 2501 while (sectors) { 2502 int s = sectors; 2503 struct md_rdev *rdev; 2504 sector_t addr; 2505 int ok; 2506 2507 if (s > (PAGE_SIZE>>9)) 2508 s = PAGE_SIZE >> 9; 2509 2510 rdev = conf->mirrors[dr].rdev; 2511 addr = r10_bio->devs[0].addr + sect; 2512 ok = sync_page_io(rdev, 2513 addr, 2514 s << 9, 2515 pages[idx], 2516 REQ_OP_READ, false); 2517 if (ok) { 2518 rdev = conf->mirrors[dw].rdev; 2519 addr = r10_bio->devs[1].addr + sect; 2520 ok = sync_page_io(rdev, 2521 addr, 2522 s << 9, 2523 pages[idx], 2524 REQ_OP_WRITE, false); 2525 if (!ok) { 2526 set_bit(WriteErrorSeen, &rdev->flags); 2527 if (!test_and_set_bit(WantReplacement, 2528 &rdev->flags)) 2529 set_bit(MD_RECOVERY_NEEDED, 2530 &rdev->mddev->recovery); 2531 } 2532 } 2533 if (!ok) { 2534 /* We don't worry if we cannot set a bad block - 2535 * it really is bad so there is no loss in not 2536 * recording it yet 2537 */ 2538 rdev_set_badblocks(rdev, addr, s, 0); 2539 2540 if (rdev != conf->mirrors[dw].rdev) { 2541 /* need bad block on destination too */ 2542 struct md_rdev *rdev2 = conf->mirrors[dw].rdev; 2543 addr = r10_bio->devs[1].addr + sect; 2544 ok = rdev_set_badblocks(rdev2, addr, s, 0); 2545 if (!ok) { 2546 /* just abort the recovery */ 2547 pr_notice("md/raid10:%s: recovery aborted due to read error\n", 2548 mdname(mddev)); 2549 2550 conf->mirrors[dw].recovery_disabled 2551 = mddev->recovery_disabled; 2552 set_bit(MD_RECOVERY_INTR, 2553 &mddev->recovery); 2554 break; 2555 } 2556 } 2557 } 2558 2559 sectors -= s; 2560 sect += s; 2561 idx++; 2562 } 2563 } 2564 2565 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) 2566 { 2567 struct r10conf *conf = mddev->private; 2568 int d; 2569 struct bio *wbio = r10_bio->devs[1].bio; 2570 struct bio *wbio2 = r10_bio->devs[1].repl_bio; 2571 2572 /* Need to test wbio2->bi_end_io before we call 2573 * submit_bio_noacct as if the former is NULL, 2574 * the latter is free to free wbio2. 2575 */ 2576 if (wbio2 && !wbio2->bi_end_io) 2577 wbio2 = NULL; 2578 2579 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { 2580 fix_recovery_read_error(r10_bio); 2581 if (wbio->bi_end_io) 2582 end_sync_request(r10_bio); 2583 if (wbio2) 2584 end_sync_request(r10_bio); 2585 return; 2586 } 2587 2588 /* 2589 * share the pages with the first bio 2590 * and submit the write request 2591 */ 2592 d = r10_bio->devs[1].devnum; 2593 if (wbio->bi_end_io) { 2594 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 2595 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio)); 2596 submit_bio_noacct(wbio); 2597 } 2598 if (wbio2) { 2599 atomic_inc(&conf->mirrors[d].replacement->nr_pending); 2600 md_sync_acct(conf->mirrors[d].replacement->bdev, 2601 bio_sectors(wbio2)); 2602 submit_bio_noacct(wbio2); 2603 } 2604 } 2605 2606 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, 2607 int sectors, struct page *page, enum req_op op) 2608 { 2609 if (rdev_has_badblock(rdev, sector, sectors) && 2610 (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags))) 2611 return -1; 2612 if (sync_page_io(rdev, sector, sectors << 9, page, op, false)) 2613 /* success */ 2614 return 1; 2615 if (op == REQ_OP_WRITE) { 2616 set_bit(WriteErrorSeen, &rdev->flags); 2617 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 2618 set_bit(MD_RECOVERY_NEEDED, 2619 &rdev->mddev->recovery); 2620 } 2621 /* need to record an error - either for the block or the device */ 2622 if (!rdev_set_badblocks(rdev, sector, sectors, 0)) 2623 md_error(rdev->mddev, rdev); 2624 return 0; 2625 } 2626 2627 /* 2628 * This is a kernel thread which: 2629 * 2630 * 1. Retries failed read operations on working mirrors. 2631 * 2. Updates the raid superblock when problems encounter. 2632 * 3. Performs writes following reads for array synchronising. 2633 */ 2634 2635 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio) 2636 { 2637 int sect = 0; /* Offset from r10_bio->sector */ 2638 int sectors = r10_bio->sectors, slot = r10_bio->read_slot; 2639 struct md_rdev *rdev; 2640 int d = r10_bio->devs[slot].devnum; 2641 2642 /* still own a reference to this rdev, so it cannot 2643 * have been cleared recently. 2644 */ 2645 rdev = conf->mirrors[d].rdev; 2646 2647 if (test_bit(Faulty, &rdev->flags)) 2648 /* drive has already been failed, just ignore any 2649 more fix_read_error() attempts */ 2650 return; 2651 2652 if (exceed_read_errors(mddev, rdev)) { 2653 r10_bio->devs[slot].bio = IO_BLOCKED; 2654 return; 2655 } 2656 2657 while(sectors) { 2658 int s = sectors; 2659 int sl = slot; 2660 int success = 0; 2661 int start; 2662 2663 if (s > (PAGE_SIZE>>9)) 2664 s = PAGE_SIZE >> 9; 2665 2666 do { 2667 d = r10_bio->devs[sl].devnum; 2668 rdev = conf->mirrors[d].rdev; 2669 if (rdev && 2670 test_bit(In_sync, &rdev->flags) && 2671 !test_bit(Faulty, &rdev->flags) && 2672 rdev_has_badblock(rdev, 2673 r10_bio->devs[sl].addr + sect, 2674 s) == 0) { 2675 atomic_inc(&rdev->nr_pending); 2676 success = sync_page_io(rdev, 2677 r10_bio->devs[sl].addr + 2678 sect, 2679 s<<9, 2680 conf->tmppage, 2681 REQ_OP_READ, false); 2682 rdev_dec_pending(rdev, mddev); 2683 if (success) 2684 break; 2685 } 2686 sl++; 2687 if (sl == conf->copies) 2688 sl = 0; 2689 } while (sl != slot); 2690 2691 if (!success) { 2692 /* Cannot read from anywhere, just mark the block 2693 * as bad on the first device to discourage future 2694 * reads. 2695 */ 2696 int dn = r10_bio->devs[slot].devnum; 2697 rdev = conf->mirrors[dn].rdev; 2698 2699 if (!rdev_set_badblocks( 2700 rdev, 2701 r10_bio->devs[slot].addr 2702 + sect, 2703 s, 0)) { 2704 md_error(mddev, rdev); 2705 r10_bio->devs[slot].bio 2706 = IO_BLOCKED; 2707 } 2708 break; 2709 } 2710 2711 start = sl; 2712 /* write it back and re-read */ 2713 while (sl != slot) { 2714 if (sl==0) 2715 sl = conf->copies; 2716 sl--; 2717 d = r10_bio->devs[sl].devnum; 2718 rdev = conf->mirrors[d].rdev; 2719 if (!rdev || 2720 test_bit(Faulty, &rdev->flags) || 2721 !test_bit(In_sync, &rdev->flags)) 2722 continue; 2723 2724 atomic_inc(&rdev->nr_pending); 2725 if (r10_sync_page_io(rdev, 2726 r10_bio->devs[sl].addr + 2727 sect, 2728 s, conf->tmppage, REQ_OP_WRITE) 2729 == 0) { 2730 /* Well, this device is dead */ 2731 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n", 2732 mdname(mddev), s, 2733 (unsigned long long)( 2734 sect + 2735 choose_data_offset(r10_bio, 2736 rdev)), 2737 rdev->bdev); 2738 pr_notice("md/raid10:%s: %pg: failing drive\n", 2739 mdname(mddev), 2740 rdev->bdev); 2741 } 2742 rdev_dec_pending(rdev, mddev); 2743 } 2744 sl = start; 2745 while (sl != slot) { 2746 if (sl==0) 2747 sl = conf->copies; 2748 sl--; 2749 d = r10_bio->devs[sl].devnum; 2750 rdev = conf->mirrors[d].rdev; 2751 if (!rdev || 2752 test_bit(Faulty, &rdev->flags) || 2753 !test_bit(In_sync, &rdev->flags)) 2754 continue; 2755 2756 atomic_inc(&rdev->nr_pending); 2757 switch (r10_sync_page_io(rdev, 2758 r10_bio->devs[sl].addr + 2759 sect, 2760 s, conf->tmppage, REQ_OP_READ)) { 2761 case 0: 2762 /* Well, this device is dead */ 2763 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n", 2764 mdname(mddev), s, 2765 (unsigned long long)( 2766 sect + 2767 choose_data_offset(r10_bio, rdev)), 2768 rdev->bdev); 2769 pr_notice("md/raid10:%s: %pg: failing drive\n", 2770 mdname(mddev), 2771 rdev->bdev); 2772 break; 2773 case 1: 2774 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n", 2775 mdname(mddev), s, 2776 (unsigned long long)( 2777 sect + 2778 choose_data_offset(r10_bio, rdev)), 2779 rdev->bdev); 2780 atomic_add(s, &rdev->corrected_errors); 2781 } 2782 2783 rdev_dec_pending(rdev, mddev); 2784 } 2785 2786 sectors -= s; 2787 sect += s; 2788 } 2789 } 2790 2791 static int narrow_write_error(struct r10bio *r10_bio, int i) 2792 { 2793 struct bio *bio = r10_bio->master_bio; 2794 struct mddev *mddev = r10_bio->mddev; 2795 struct r10conf *conf = mddev->private; 2796 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; 2797 /* bio has the data to be written to slot 'i' where 2798 * we just recently had a write error. 2799 * We repeatedly clone the bio and trim down to one block, 2800 * then try the write. Where the write fails we record 2801 * a bad block. 2802 * It is conceivable that the bio doesn't exactly align with 2803 * blocks. We must handle this. 2804 * 2805 * We currently own a reference to the rdev. 2806 */ 2807 2808 int block_sectors; 2809 sector_t sector; 2810 int sectors; 2811 int sect_to_write = r10_bio->sectors; 2812 int ok = 1; 2813 2814 if (rdev->badblocks.shift < 0) 2815 return 0; 2816 2817 block_sectors = roundup(1 << rdev->badblocks.shift, 2818 bdev_logical_block_size(rdev->bdev) >> 9); 2819 sector = r10_bio->sector; 2820 sectors = ((r10_bio->sector + block_sectors) 2821 & ~(sector_t)(block_sectors - 1)) 2822 - sector; 2823 2824 while (sect_to_write) { 2825 struct bio *wbio; 2826 sector_t wsector; 2827 if (sectors > sect_to_write) 2828 sectors = sect_to_write; 2829 /* Write at 'sector' for 'sectors' */ 2830 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, 2831 &mddev->bio_set); 2832 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors); 2833 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector); 2834 wbio->bi_iter.bi_sector = wsector + 2835 choose_data_offset(r10_bio, rdev); 2836 wbio->bi_opf = REQ_OP_WRITE; 2837 2838 if (submit_bio_wait(wbio) < 0) 2839 /* Failure! */ 2840 ok = rdev_set_badblocks(rdev, wsector, 2841 sectors, 0) 2842 && ok; 2843 2844 bio_put(wbio); 2845 sect_to_write -= sectors; 2846 sector += sectors; 2847 sectors = block_sectors; 2848 } 2849 return ok; 2850 } 2851 2852 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) 2853 { 2854 int slot = r10_bio->read_slot; 2855 struct bio *bio; 2856 struct r10conf *conf = mddev->private; 2857 struct md_rdev *rdev = r10_bio->devs[slot].rdev; 2858 2859 /* we got a read error. Maybe the drive is bad. Maybe just 2860 * the block and we can fix it. 2861 * We freeze all other IO, and try reading the block from 2862 * other devices. When we find one, we re-write 2863 * and check it that fixes the read error. 2864 * This is all done synchronously while the array is 2865 * frozen. 2866 */ 2867 bio = r10_bio->devs[slot].bio; 2868 bio_put(bio); 2869 r10_bio->devs[slot].bio = NULL; 2870 2871 if (mddev->ro) 2872 r10_bio->devs[slot].bio = IO_BLOCKED; 2873 else if (!test_bit(FailFast, &rdev->flags)) { 2874 freeze_array(conf, 1); 2875 fix_read_error(conf, mddev, r10_bio); 2876 unfreeze_array(conf); 2877 } else 2878 md_error(mddev, rdev); 2879 2880 rdev_dec_pending(rdev, mddev); 2881 r10_bio->state = 0; 2882 raid10_read_request(mddev, r10_bio->master_bio, r10_bio, false); 2883 /* 2884 * allow_barrier after re-submit to ensure no sync io 2885 * can be issued while regular io pending. 2886 */ 2887 allow_barrier(conf); 2888 } 2889 2890 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) 2891 { 2892 /* Some sort of write request has finished and it 2893 * succeeded in writing where we thought there was a 2894 * bad block. So forget the bad block. 2895 * Or possibly if failed and we need to record 2896 * a bad block. 2897 */ 2898 int m; 2899 struct md_rdev *rdev; 2900 2901 if (test_bit(R10BIO_IsSync, &r10_bio->state) || 2902 test_bit(R10BIO_IsRecover, &r10_bio->state)) { 2903 for (m = 0; m < conf->copies; m++) { 2904 int dev = r10_bio->devs[m].devnum; 2905 rdev = conf->mirrors[dev].rdev; 2906 if (r10_bio->devs[m].bio == NULL || 2907 r10_bio->devs[m].bio->bi_end_io == NULL) 2908 continue; 2909 if (!r10_bio->devs[m].bio->bi_status) { 2910 rdev_clear_badblocks( 2911 rdev, 2912 r10_bio->devs[m].addr, 2913 r10_bio->sectors, 0); 2914 } else { 2915 if (!rdev_set_badblocks( 2916 rdev, 2917 r10_bio->devs[m].addr, 2918 r10_bio->sectors, 0)) 2919 md_error(conf->mddev, rdev); 2920 } 2921 rdev = conf->mirrors[dev].replacement; 2922 if (r10_bio->devs[m].repl_bio == NULL || 2923 r10_bio->devs[m].repl_bio->bi_end_io == NULL) 2924 continue; 2925 2926 if (!r10_bio->devs[m].repl_bio->bi_status) { 2927 rdev_clear_badblocks( 2928 rdev, 2929 r10_bio->devs[m].addr, 2930 r10_bio->sectors, 0); 2931 } else { 2932 if (!rdev_set_badblocks( 2933 rdev, 2934 r10_bio->devs[m].addr, 2935 r10_bio->sectors, 0)) 2936 md_error(conf->mddev, rdev); 2937 } 2938 } 2939 put_buf(r10_bio); 2940 } else { 2941 bool fail = false; 2942 for (m = 0; m < conf->copies; m++) { 2943 int dev = r10_bio->devs[m].devnum; 2944 struct bio *bio = r10_bio->devs[m].bio; 2945 rdev = conf->mirrors[dev].rdev; 2946 if (bio == IO_MADE_GOOD) { 2947 rdev_clear_badblocks( 2948 rdev, 2949 r10_bio->devs[m].addr, 2950 r10_bio->sectors, 0); 2951 rdev_dec_pending(rdev, conf->mddev); 2952 } else if (bio != NULL && bio->bi_status) { 2953 fail = true; 2954 if (!narrow_write_error(r10_bio, m)) { 2955 md_error(conf->mddev, rdev); 2956 set_bit(R10BIO_Degraded, 2957 &r10_bio->state); 2958 } 2959 rdev_dec_pending(rdev, conf->mddev); 2960 } 2961 bio = r10_bio->devs[m].repl_bio; 2962 rdev = conf->mirrors[dev].replacement; 2963 if (rdev && bio == IO_MADE_GOOD) { 2964 rdev_clear_badblocks( 2965 rdev, 2966 r10_bio->devs[m].addr, 2967 r10_bio->sectors, 0); 2968 rdev_dec_pending(rdev, conf->mddev); 2969 } 2970 } 2971 if (fail) { 2972 spin_lock_irq(&conf->device_lock); 2973 list_add(&r10_bio->retry_list, &conf->bio_end_io_list); 2974 conf->nr_queued++; 2975 spin_unlock_irq(&conf->device_lock); 2976 /* 2977 * In case freeze_array() is waiting for condition 2978 * nr_pending == nr_queued + extra to be true. 2979 */ 2980 wake_up(&conf->wait_barrier); 2981 md_wakeup_thread(conf->mddev->thread); 2982 } else { 2983 if (test_bit(R10BIO_WriteError, 2984 &r10_bio->state)) 2985 close_write(r10_bio); 2986 raid_end_bio_io(r10_bio); 2987 } 2988 } 2989 } 2990 2991 static void raid10d(struct md_thread *thread) 2992 { 2993 struct mddev *mddev = thread->mddev; 2994 struct r10bio *r10_bio; 2995 unsigned long flags; 2996 struct r10conf *conf = mddev->private; 2997 struct list_head *head = &conf->retry_list; 2998 struct blk_plug plug; 2999 3000 md_check_recovery(mddev); 3001 3002 if (!list_empty_careful(&conf->bio_end_io_list) && 3003 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { 3004 LIST_HEAD(tmp); 3005 spin_lock_irqsave(&conf->device_lock, flags); 3006 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { 3007 while (!list_empty(&conf->bio_end_io_list)) { 3008 list_move(conf->bio_end_io_list.prev, &tmp); 3009 conf->nr_queued--; 3010 } 3011 } 3012 spin_unlock_irqrestore(&conf->device_lock, flags); 3013 while (!list_empty(&tmp)) { 3014 r10_bio = list_first_entry(&tmp, struct r10bio, 3015 retry_list); 3016 list_del(&r10_bio->retry_list); 3017 if (mddev->degraded) 3018 set_bit(R10BIO_Degraded, &r10_bio->state); 3019 3020 if (test_bit(R10BIO_WriteError, 3021 &r10_bio->state)) 3022 close_write(r10_bio); 3023 raid_end_bio_io(r10_bio); 3024 } 3025 } 3026 3027 blk_start_plug(&plug); 3028 for (;;) { 3029 3030 flush_pending_writes(conf); 3031 3032 spin_lock_irqsave(&conf->device_lock, flags); 3033 if (list_empty(head)) { 3034 spin_unlock_irqrestore(&conf->device_lock, flags); 3035 break; 3036 } 3037 r10_bio = list_entry(head->prev, struct r10bio, retry_list); 3038 list_del(head->prev); 3039 conf->nr_queued--; 3040 spin_unlock_irqrestore(&conf->device_lock, flags); 3041 3042 mddev = r10_bio->mddev; 3043 conf = mddev->private; 3044 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 3045 test_bit(R10BIO_WriteError, &r10_bio->state)) 3046 handle_write_completed(conf, r10_bio); 3047 else if (test_bit(R10BIO_IsReshape, &r10_bio->state)) 3048 reshape_request_write(mddev, r10_bio); 3049 else if (test_bit(R10BIO_IsSync, &r10_bio->state)) 3050 sync_request_write(mddev, r10_bio); 3051 else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) 3052 recovery_request_write(mddev, r10_bio); 3053 else if (test_bit(R10BIO_ReadError, &r10_bio->state)) 3054 handle_read_error(mddev, r10_bio); 3055 else 3056 WARN_ON_ONCE(1); 3057 3058 cond_resched(); 3059 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING)) 3060 md_check_recovery(mddev); 3061 } 3062 blk_finish_plug(&plug); 3063 } 3064 3065 static int init_resync(struct r10conf *conf) 3066 { 3067 int ret, buffs, i; 3068 3069 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; 3070 BUG_ON(mempool_initialized(&conf->r10buf_pool)); 3071 conf->have_replacement = 0; 3072 for (i = 0; i < conf->geo.raid_disks; i++) 3073 if (conf->mirrors[i].replacement) 3074 conf->have_replacement = 1; 3075 ret = mempool_init(&conf->r10buf_pool, buffs, 3076 r10buf_pool_alloc, r10buf_pool_free, conf); 3077 if (ret) 3078 return ret; 3079 conf->next_resync = 0; 3080 return 0; 3081 } 3082 3083 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf) 3084 { 3085 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO); 3086 struct rsync_pages *rp; 3087 struct bio *bio; 3088 int nalloc; 3089 int i; 3090 3091 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || 3092 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) 3093 nalloc = conf->copies; /* resync */ 3094 else 3095 nalloc = 2; /* recovery */ 3096 3097 for (i = 0; i < nalloc; i++) { 3098 bio = r10bio->devs[i].bio; 3099 rp = bio->bi_private; 3100 bio_reset(bio, NULL, 0); 3101 bio->bi_private = rp; 3102 bio = r10bio->devs[i].repl_bio; 3103 if (bio) { 3104 rp = bio->bi_private; 3105 bio_reset(bio, NULL, 0); 3106 bio->bi_private = rp; 3107 } 3108 } 3109 return r10bio; 3110 } 3111 3112 /* 3113 * Set cluster_sync_high since we need other nodes to add the 3114 * range [cluster_sync_low, cluster_sync_high] to suspend list. 3115 */ 3116 static void raid10_set_cluster_sync_high(struct r10conf *conf) 3117 { 3118 sector_t window_size; 3119 int extra_chunk, chunks; 3120 3121 /* 3122 * First, here we define "stripe" as a unit which across 3123 * all member devices one time, so we get chunks by use 3124 * raid_disks / near_copies. Otherwise, if near_copies is 3125 * close to raid_disks, then resync window could increases 3126 * linearly with the increase of raid_disks, which means 3127 * we will suspend a really large IO window while it is not 3128 * necessary. If raid_disks is not divisible by near_copies, 3129 * an extra chunk is needed to ensure the whole "stripe" is 3130 * covered. 3131 */ 3132 3133 chunks = conf->geo.raid_disks / conf->geo.near_copies; 3134 if (conf->geo.raid_disks % conf->geo.near_copies == 0) 3135 extra_chunk = 0; 3136 else 3137 extra_chunk = 1; 3138 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors; 3139 3140 /* 3141 * At least use a 32M window to align with raid1's resync window 3142 */ 3143 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ? 3144 CLUSTER_RESYNC_WINDOW_SECTORS : window_size; 3145 3146 conf->cluster_sync_high = conf->cluster_sync_low + window_size; 3147 } 3148 3149 /* 3150 * perform a "sync" on one "block" 3151 * 3152 * We need to make sure that no normal I/O request - particularly write 3153 * requests - conflict with active sync requests. 3154 * 3155 * This is achieved by tracking pending requests and a 'barrier' concept 3156 * that can be installed to exclude normal IO requests. 3157 * 3158 * Resync and recovery are handled very differently. 3159 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. 3160 * 3161 * For resync, we iterate over virtual addresses, read all copies, 3162 * and update if there are differences. If only one copy is live, 3163 * skip it. 3164 * For recovery, we iterate over physical addresses, read a good 3165 * value for each non-in_sync drive, and over-write. 3166 * 3167 * So, for recovery we may have several outstanding complex requests for a 3168 * given address, one for each out-of-sync device. We model this by allocating 3169 * a number of r10_bio structures, one for each out-of-sync device. 3170 * As we setup these structures, we collect all bio's together into a list 3171 * which we then process collectively to add pages, and then process again 3172 * to pass to submit_bio_noacct. 3173 * 3174 * The r10_bio structures are linked using a borrowed master_bio pointer. 3175 * This link is counted in ->remaining. When the r10_bio that points to NULL 3176 * has its remaining count decremented to 0, the whole complex operation 3177 * is complete. 3178 * 3179 */ 3180 3181 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr, 3182 sector_t max_sector, int *skipped) 3183 { 3184 struct r10conf *conf = mddev->private; 3185 struct r10bio *r10_bio; 3186 struct bio *biolist = NULL, *bio; 3187 sector_t nr_sectors; 3188 int i; 3189 int max_sync; 3190 sector_t sync_blocks; 3191 sector_t sectors_skipped = 0; 3192 int chunks_skipped = 0; 3193 sector_t chunk_mask = conf->geo.chunk_mask; 3194 int page_idx = 0; 3195 int error_disk = -1; 3196 3197 /* 3198 * Allow skipping a full rebuild for incremental assembly 3199 * of a clean array, like RAID1 does. 3200 */ 3201 if (mddev->bitmap == NULL && 3202 mddev->recovery_cp == MaxSector && 3203 mddev->reshape_position == MaxSector && 3204 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && 3205 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 3206 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && 3207 conf->fullsync == 0) { 3208 *skipped = 1; 3209 return mddev->dev_sectors - sector_nr; 3210 } 3211 3212 if (!mempool_initialized(&conf->r10buf_pool)) 3213 if (init_resync(conf)) 3214 return 0; 3215 3216 skipped: 3217 if (sector_nr >= max_sector) { 3218 conf->cluster_sync_low = 0; 3219 conf->cluster_sync_high = 0; 3220 3221 /* If we aborted, we need to abort the 3222 * sync on the 'current' bitmap chucks (there can 3223 * be several when recovering multiple devices). 3224 * as we may have started syncing it but not finished. 3225 * We can find the current address in 3226 * mddev->curr_resync, but for recovery, 3227 * we need to convert that to several 3228 * virtual addresses. 3229 */ 3230 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { 3231 end_reshape(conf); 3232 close_sync(conf); 3233 return 0; 3234 } 3235 3236 if (mddev->curr_resync < max_sector) { /* aborted */ 3237 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 3238 mddev->bitmap_ops->end_sync(mddev, 3239 mddev->curr_resync, 3240 &sync_blocks); 3241 else for (i = 0; i < conf->geo.raid_disks; i++) { 3242 sector_t sect = 3243 raid10_find_virt(conf, mddev->curr_resync, i); 3244 3245 mddev->bitmap_ops->end_sync(mddev, sect, 3246 &sync_blocks); 3247 } 3248 } else { 3249 /* completed sync */ 3250 if ((!mddev->bitmap || conf->fullsync) 3251 && conf->have_replacement 3252 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3253 /* Completed a full sync so the replacements 3254 * are now fully recovered. 3255 */ 3256 for (i = 0; i < conf->geo.raid_disks; i++) { 3257 struct md_rdev *rdev = 3258 conf->mirrors[i].replacement; 3259 3260 if (rdev) 3261 rdev->recovery_offset = MaxSector; 3262 } 3263 } 3264 conf->fullsync = 0; 3265 } 3266 mddev->bitmap_ops->close_sync(mddev); 3267 close_sync(conf); 3268 *skipped = 1; 3269 return sectors_skipped; 3270 } 3271 3272 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 3273 return reshape_request(mddev, sector_nr, skipped); 3274 3275 if (chunks_skipped >= conf->geo.raid_disks) { 3276 pr_err("md/raid10:%s: %s fails\n", mdname(mddev), 3277 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? "resync" : "recovery"); 3278 if (error_disk >= 0 && 3279 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3280 /* 3281 * recovery fails, set mirrors.recovery_disabled, 3282 * device shouldn't be added to there. 3283 */ 3284 conf->mirrors[error_disk].recovery_disabled = 3285 mddev->recovery_disabled; 3286 return 0; 3287 } 3288 /* 3289 * if there has been nothing to do on any drive, 3290 * then there is nothing to do at all. 3291 */ 3292 *skipped = 1; 3293 return (max_sector - sector_nr) + sectors_skipped; 3294 } 3295 3296 if (max_sector > mddev->resync_max) 3297 max_sector = mddev->resync_max; /* Don't do IO beyond here */ 3298 3299 /* make sure whole request will fit in a chunk - if chunks 3300 * are meaningful 3301 */ 3302 if (conf->geo.near_copies < conf->geo.raid_disks && 3303 max_sector > (sector_nr | chunk_mask)) 3304 max_sector = (sector_nr | chunk_mask) + 1; 3305 3306 /* 3307 * If there is non-resync activity waiting for a turn, then let it 3308 * though before starting on this new sync request. 3309 */ 3310 if (conf->nr_waiting) 3311 schedule_timeout_uninterruptible(1); 3312 3313 /* Again, very different code for resync and recovery. 3314 * Both must result in an r10bio with a list of bios that 3315 * have bi_end_io, bi_sector, bi_bdev set, 3316 * and bi_private set to the r10bio. 3317 * For recovery, we may actually create several r10bios 3318 * with 2 bios in each, that correspond to the bios in the main one. 3319 * In this case, the subordinate r10bios link back through a 3320 * borrowed master_bio pointer, and the counter in the master 3321 * includes a ref from each subordinate. 3322 */ 3323 /* First, we decide what to do and set ->bi_end_io 3324 * To end_sync_read if we want to read, and 3325 * end_sync_write if we will want to write. 3326 */ 3327 3328 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); 3329 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3330 /* recovery... the complicated one */ 3331 int j; 3332 r10_bio = NULL; 3333 3334 for (i = 0 ; i < conf->geo.raid_disks; i++) { 3335 bool still_degraded; 3336 struct r10bio *rb2; 3337 sector_t sect; 3338 bool must_sync; 3339 int any_working; 3340 struct raid10_info *mirror = &conf->mirrors[i]; 3341 struct md_rdev *mrdev, *mreplace; 3342 3343 mrdev = mirror->rdev; 3344 mreplace = mirror->replacement; 3345 3346 if (mrdev && (test_bit(Faulty, &mrdev->flags) || 3347 test_bit(In_sync, &mrdev->flags))) 3348 mrdev = NULL; 3349 if (mreplace && test_bit(Faulty, &mreplace->flags)) 3350 mreplace = NULL; 3351 3352 if (!mrdev && !mreplace) 3353 continue; 3354 3355 still_degraded = false; 3356 /* want to reconstruct this device */ 3357 rb2 = r10_bio; 3358 sect = raid10_find_virt(conf, sector_nr, i); 3359 if (sect >= mddev->resync_max_sectors) 3360 /* last stripe is not complete - don't 3361 * try to recover this sector. 3362 */ 3363 continue; 3364 /* Unless we are doing a full sync, or a replacement 3365 * we only need to recover the block if it is set in 3366 * the bitmap 3367 */ 3368 must_sync = mddev->bitmap_ops->start_sync(mddev, sect, 3369 &sync_blocks, 3370 true); 3371 if (sync_blocks < max_sync) 3372 max_sync = sync_blocks; 3373 if (!must_sync && 3374 mreplace == NULL && 3375 !conf->fullsync) { 3376 /* yep, skip the sync_blocks here, but don't assume 3377 * that there will never be anything to do here 3378 */ 3379 chunks_skipped = -1; 3380 continue; 3381 } 3382 if (mrdev) 3383 atomic_inc(&mrdev->nr_pending); 3384 if (mreplace) 3385 atomic_inc(&mreplace->nr_pending); 3386 3387 r10_bio = raid10_alloc_init_r10buf(conf); 3388 r10_bio->state = 0; 3389 raise_barrier(conf, rb2 != NULL); 3390 atomic_set(&r10_bio->remaining, 0); 3391 3392 r10_bio->master_bio = (struct bio*)rb2; 3393 if (rb2) 3394 atomic_inc(&rb2->remaining); 3395 r10_bio->mddev = mddev; 3396 set_bit(R10BIO_IsRecover, &r10_bio->state); 3397 r10_bio->sector = sect; 3398 3399 raid10_find_phys(conf, r10_bio); 3400 3401 /* Need to check if the array will still be 3402 * degraded 3403 */ 3404 for (j = 0; j < conf->geo.raid_disks; j++) { 3405 struct md_rdev *rdev = conf->mirrors[j].rdev; 3406 3407 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { 3408 still_degraded = false; 3409 break; 3410 } 3411 } 3412 3413 must_sync = mddev->bitmap_ops->start_sync(mddev, sect, 3414 &sync_blocks, still_degraded); 3415 3416 any_working = 0; 3417 for (j=0; j<conf->copies;j++) { 3418 int k; 3419 int d = r10_bio->devs[j].devnum; 3420 sector_t from_addr, to_addr; 3421 struct md_rdev *rdev = conf->mirrors[d].rdev; 3422 sector_t sector, first_bad; 3423 int bad_sectors; 3424 if (!rdev || 3425 !test_bit(In_sync, &rdev->flags)) 3426 continue; 3427 /* This is where we read from */ 3428 any_working = 1; 3429 sector = r10_bio->devs[j].addr; 3430 3431 if (is_badblock(rdev, sector, max_sync, 3432 &first_bad, &bad_sectors)) { 3433 if (first_bad > sector) 3434 max_sync = first_bad - sector; 3435 else { 3436 bad_sectors -= (sector 3437 - first_bad); 3438 if (max_sync > bad_sectors) 3439 max_sync = bad_sectors; 3440 continue; 3441 } 3442 } 3443 bio = r10_bio->devs[0].bio; 3444 bio->bi_next = biolist; 3445 biolist = bio; 3446 bio->bi_end_io = end_sync_read; 3447 bio->bi_opf = REQ_OP_READ; 3448 if (test_bit(FailFast, &rdev->flags)) 3449 bio->bi_opf |= MD_FAILFAST; 3450 from_addr = r10_bio->devs[j].addr; 3451 bio->bi_iter.bi_sector = from_addr + 3452 rdev->data_offset; 3453 bio_set_dev(bio, rdev->bdev); 3454 atomic_inc(&rdev->nr_pending); 3455 /* and we write to 'i' (if not in_sync) */ 3456 3457 for (k=0; k<conf->copies; k++) 3458 if (r10_bio->devs[k].devnum == i) 3459 break; 3460 BUG_ON(k == conf->copies); 3461 to_addr = r10_bio->devs[k].addr; 3462 r10_bio->devs[0].devnum = d; 3463 r10_bio->devs[0].addr = from_addr; 3464 r10_bio->devs[1].devnum = i; 3465 r10_bio->devs[1].addr = to_addr; 3466 3467 if (mrdev) { 3468 bio = r10_bio->devs[1].bio; 3469 bio->bi_next = biolist; 3470 biolist = bio; 3471 bio->bi_end_io = end_sync_write; 3472 bio->bi_opf = REQ_OP_WRITE; 3473 bio->bi_iter.bi_sector = to_addr 3474 + mrdev->data_offset; 3475 bio_set_dev(bio, mrdev->bdev); 3476 atomic_inc(&r10_bio->remaining); 3477 } else 3478 r10_bio->devs[1].bio->bi_end_io = NULL; 3479 3480 /* and maybe write to replacement */ 3481 bio = r10_bio->devs[1].repl_bio; 3482 if (bio) 3483 bio->bi_end_io = NULL; 3484 /* Note: if replace is not NULL, then bio 3485 * cannot be NULL as r10buf_pool_alloc will 3486 * have allocated it. 3487 */ 3488 if (!mreplace) 3489 break; 3490 bio->bi_next = biolist; 3491 biolist = bio; 3492 bio->bi_end_io = end_sync_write; 3493 bio->bi_opf = REQ_OP_WRITE; 3494 bio->bi_iter.bi_sector = to_addr + 3495 mreplace->data_offset; 3496 bio_set_dev(bio, mreplace->bdev); 3497 atomic_inc(&r10_bio->remaining); 3498 break; 3499 } 3500 if (j == conf->copies) { 3501 /* Cannot recover, so abort the recovery or 3502 * record a bad block */ 3503 if (any_working) { 3504 /* problem is that there are bad blocks 3505 * on other device(s) 3506 */ 3507 int k; 3508 for (k = 0; k < conf->copies; k++) 3509 if (r10_bio->devs[k].devnum == i) 3510 break; 3511 if (mrdev && !test_bit(In_sync, 3512 &mrdev->flags) 3513 && !rdev_set_badblocks( 3514 mrdev, 3515 r10_bio->devs[k].addr, 3516 max_sync, 0)) 3517 any_working = 0; 3518 if (mreplace && 3519 !rdev_set_badblocks( 3520 mreplace, 3521 r10_bio->devs[k].addr, 3522 max_sync, 0)) 3523 any_working = 0; 3524 } 3525 if (!any_working) { 3526 if (!test_and_set_bit(MD_RECOVERY_INTR, 3527 &mddev->recovery)) 3528 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n", 3529 mdname(mddev)); 3530 mirror->recovery_disabled 3531 = mddev->recovery_disabled; 3532 } else { 3533 error_disk = i; 3534 } 3535 put_buf(r10_bio); 3536 if (rb2) 3537 atomic_dec(&rb2->remaining); 3538 r10_bio = rb2; 3539 if (mrdev) 3540 rdev_dec_pending(mrdev, mddev); 3541 if (mreplace) 3542 rdev_dec_pending(mreplace, mddev); 3543 break; 3544 } 3545 if (mrdev) 3546 rdev_dec_pending(mrdev, mddev); 3547 if (mreplace) 3548 rdev_dec_pending(mreplace, mddev); 3549 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) { 3550 /* Only want this if there is elsewhere to 3551 * read from. 'j' is currently the first 3552 * readable copy. 3553 */ 3554 int targets = 1; 3555 for (; j < conf->copies; j++) { 3556 int d = r10_bio->devs[j].devnum; 3557 if (conf->mirrors[d].rdev && 3558 test_bit(In_sync, 3559 &conf->mirrors[d].rdev->flags)) 3560 targets++; 3561 } 3562 if (targets == 1) 3563 r10_bio->devs[0].bio->bi_opf 3564 &= ~MD_FAILFAST; 3565 } 3566 } 3567 if (biolist == NULL) { 3568 while (r10_bio) { 3569 struct r10bio *rb2 = r10_bio; 3570 r10_bio = (struct r10bio*) rb2->master_bio; 3571 rb2->master_bio = NULL; 3572 put_buf(rb2); 3573 } 3574 goto giveup; 3575 } 3576 } else { 3577 /* resync. Schedule a read for every block at this virt offset */ 3578 int count = 0; 3579 3580 /* 3581 * Since curr_resync_completed could probably not update in 3582 * time, and we will set cluster_sync_low based on it. 3583 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for 3584 * safety reason, which ensures curr_resync_completed is 3585 * updated in bitmap_cond_end_sync. 3586 */ 3587 mddev->bitmap_ops->cond_end_sync(mddev, sector_nr, 3588 mddev_is_clustered(mddev) && 3589 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high)); 3590 3591 if (!mddev->bitmap_ops->start_sync(mddev, sector_nr, 3592 &sync_blocks, 3593 mddev->degraded) && 3594 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, 3595 &mddev->recovery)) { 3596 /* We can skip this block */ 3597 *skipped = 1; 3598 return sync_blocks + sectors_skipped; 3599 } 3600 if (sync_blocks < max_sync) 3601 max_sync = sync_blocks; 3602 r10_bio = raid10_alloc_init_r10buf(conf); 3603 r10_bio->state = 0; 3604 3605 r10_bio->mddev = mddev; 3606 atomic_set(&r10_bio->remaining, 0); 3607 raise_barrier(conf, 0); 3608 conf->next_resync = sector_nr; 3609 3610 r10_bio->master_bio = NULL; 3611 r10_bio->sector = sector_nr; 3612 set_bit(R10BIO_IsSync, &r10_bio->state); 3613 raid10_find_phys(conf, r10_bio); 3614 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1; 3615 3616 for (i = 0; i < conf->copies; i++) { 3617 int d = r10_bio->devs[i].devnum; 3618 sector_t first_bad, sector; 3619 int bad_sectors; 3620 struct md_rdev *rdev; 3621 3622 if (r10_bio->devs[i].repl_bio) 3623 r10_bio->devs[i].repl_bio->bi_end_io = NULL; 3624 3625 bio = r10_bio->devs[i].bio; 3626 bio->bi_status = BLK_STS_IOERR; 3627 rdev = conf->mirrors[d].rdev; 3628 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) 3629 continue; 3630 3631 sector = r10_bio->devs[i].addr; 3632 if (is_badblock(rdev, sector, max_sync, 3633 &first_bad, &bad_sectors)) { 3634 if (first_bad > sector) 3635 max_sync = first_bad - sector; 3636 else { 3637 bad_sectors -= (sector - first_bad); 3638 if (max_sync > bad_sectors) 3639 max_sync = bad_sectors; 3640 continue; 3641 } 3642 } 3643 atomic_inc(&rdev->nr_pending); 3644 atomic_inc(&r10_bio->remaining); 3645 bio->bi_next = biolist; 3646 biolist = bio; 3647 bio->bi_end_io = end_sync_read; 3648 bio->bi_opf = REQ_OP_READ; 3649 if (test_bit(FailFast, &rdev->flags)) 3650 bio->bi_opf |= MD_FAILFAST; 3651 bio->bi_iter.bi_sector = sector + rdev->data_offset; 3652 bio_set_dev(bio, rdev->bdev); 3653 count++; 3654 3655 rdev = conf->mirrors[d].replacement; 3656 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) 3657 continue; 3658 3659 atomic_inc(&rdev->nr_pending); 3660 3661 /* Need to set up for writing to the replacement */ 3662 bio = r10_bio->devs[i].repl_bio; 3663 bio->bi_status = BLK_STS_IOERR; 3664 3665 sector = r10_bio->devs[i].addr; 3666 bio->bi_next = biolist; 3667 biolist = bio; 3668 bio->bi_end_io = end_sync_write; 3669 bio->bi_opf = REQ_OP_WRITE; 3670 if (test_bit(FailFast, &rdev->flags)) 3671 bio->bi_opf |= MD_FAILFAST; 3672 bio->bi_iter.bi_sector = sector + rdev->data_offset; 3673 bio_set_dev(bio, rdev->bdev); 3674 count++; 3675 } 3676 3677 if (count < 2) { 3678 for (i=0; i<conf->copies; i++) { 3679 int d = r10_bio->devs[i].devnum; 3680 if (r10_bio->devs[i].bio->bi_end_io) 3681 rdev_dec_pending(conf->mirrors[d].rdev, 3682 mddev); 3683 if (r10_bio->devs[i].repl_bio && 3684 r10_bio->devs[i].repl_bio->bi_end_io) 3685 rdev_dec_pending( 3686 conf->mirrors[d].replacement, 3687 mddev); 3688 } 3689 put_buf(r10_bio); 3690 biolist = NULL; 3691 goto giveup; 3692 } 3693 } 3694 3695 nr_sectors = 0; 3696 if (sector_nr + max_sync < max_sector) 3697 max_sector = sector_nr + max_sync; 3698 do { 3699 struct page *page; 3700 int len = PAGE_SIZE; 3701 if (sector_nr + (len>>9) > max_sector) 3702 len = (max_sector - sector_nr) << 9; 3703 if (len == 0) 3704 break; 3705 for (bio= biolist ; bio ; bio=bio->bi_next) { 3706 struct resync_pages *rp = get_resync_pages(bio); 3707 page = resync_fetch_page(rp, page_idx); 3708 if (WARN_ON(!bio_add_page(bio, page, len, 0))) { 3709 bio->bi_status = BLK_STS_RESOURCE; 3710 bio_endio(bio); 3711 goto giveup; 3712 } 3713 } 3714 nr_sectors += len>>9; 3715 sector_nr += len>>9; 3716 } while (++page_idx < RESYNC_PAGES); 3717 r10_bio->sectors = nr_sectors; 3718 3719 if (mddev_is_clustered(mddev) && 3720 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3721 /* It is resync not recovery */ 3722 if (conf->cluster_sync_high < sector_nr + nr_sectors) { 3723 conf->cluster_sync_low = mddev->curr_resync_completed; 3724 raid10_set_cluster_sync_high(conf); 3725 /* Send resync message */ 3726 md_cluster_ops->resync_info_update(mddev, 3727 conf->cluster_sync_low, 3728 conf->cluster_sync_high); 3729 } 3730 } else if (mddev_is_clustered(mddev)) { 3731 /* This is recovery not resync */ 3732 sector_t sect_va1, sect_va2; 3733 bool broadcast_msg = false; 3734 3735 for (i = 0; i < conf->geo.raid_disks; i++) { 3736 /* 3737 * sector_nr is a device address for recovery, so we 3738 * need translate it to array address before compare 3739 * with cluster_sync_high. 3740 */ 3741 sect_va1 = raid10_find_virt(conf, sector_nr, i); 3742 3743 if (conf->cluster_sync_high < sect_va1 + nr_sectors) { 3744 broadcast_msg = true; 3745 /* 3746 * curr_resync_completed is similar as 3747 * sector_nr, so make the translation too. 3748 */ 3749 sect_va2 = raid10_find_virt(conf, 3750 mddev->curr_resync_completed, i); 3751 3752 if (conf->cluster_sync_low == 0 || 3753 conf->cluster_sync_low > sect_va2) 3754 conf->cluster_sync_low = sect_va2; 3755 } 3756 } 3757 if (broadcast_msg) { 3758 raid10_set_cluster_sync_high(conf); 3759 md_cluster_ops->resync_info_update(mddev, 3760 conf->cluster_sync_low, 3761 conf->cluster_sync_high); 3762 } 3763 } 3764 3765 while (biolist) { 3766 bio = biolist; 3767 biolist = biolist->bi_next; 3768 3769 bio->bi_next = NULL; 3770 r10_bio = get_resync_r10bio(bio); 3771 r10_bio->sectors = nr_sectors; 3772 3773 if (bio->bi_end_io == end_sync_read) { 3774 md_sync_acct_bio(bio, nr_sectors); 3775 bio->bi_status = 0; 3776 submit_bio_noacct(bio); 3777 } 3778 } 3779 3780 if (sectors_skipped) 3781 /* pretend they weren't skipped, it makes 3782 * no important difference in this case 3783 */ 3784 md_done_sync(mddev, sectors_skipped, 1); 3785 3786 return sectors_skipped + nr_sectors; 3787 giveup: 3788 /* There is nowhere to write, so all non-sync 3789 * drives must be failed or in resync, all drives 3790 * have a bad block, so try the next chunk... 3791 */ 3792 if (sector_nr + max_sync < max_sector) 3793 max_sector = sector_nr + max_sync; 3794 3795 sectors_skipped += (max_sector - sector_nr); 3796 chunks_skipped ++; 3797 sector_nr = max_sector; 3798 goto skipped; 3799 } 3800 3801 static sector_t 3802 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) 3803 { 3804 sector_t size; 3805 struct r10conf *conf = mddev->private; 3806 3807 if (!raid_disks) 3808 raid_disks = min(conf->geo.raid_disks, 3809 conf->prev.raid_disks); 3810 if (!sectors) 3811 sectors = conf->dev_sectors; 3812 3813 size = sectors >> conf->geo.chunk_shift; 3814 sector_div(size, conf->geo.far_copies); 3815 size = size * raid_disks; 3816 sector_div(size, conf->geo.near_copies); 3817 3818 return size << conf->geo.chunk_shift; 3819 } 3820 3821 static void calc_sectors(struct r10conf *conf, sector_t size) 3822 { 3823 /* Calculate the number of sectors-per-device that will 3824 * actually be used, and set conf->dev_sectors and 3825 * conf->stride 3826 */ 3827 3828 size = size >> conf->geo.chunk_shift; 3829 sector_div(size, conf->geo.far_copies); 3830 size = size * conf->geo.raid_disks; 3831 sector_div(size, conf->geo.near_copies); 3832 /* 'size' is now the number of chunks in the array */ 3833 /* calculate "used chunks per device" */ 3834 size = size * conf->copies; 3835 3836 /* We need to round up when dividing by raid_disks to 3837 * get the stride size. 3838 */ 3839 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks); 3840 3841 conf->dev_sectors = size << conf->geo.chunk_shift; 3842 3843 if (conf->geo.far_offset) 3844 conf->geo.stride = 1 << conf->geo.chunk_shift; 3845 else { 3846 sector_div(size, conf->geo.far_copies); 3847 conf->geo.stride = size << conf->geo.chunk_shift; 3848 } 3849 } 3850 3851 enum geo_type {geo_new, geo_old, geo_start}; 3852 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new) 3853 { 3854 int nc, fc, fo; 3855 int layout, chunk, disks; 3856 switch (new) { 3857 case geo_old: 3858 layout = mddev->layout; 3859 chunk = mddev->chunk_sectors; 3860 disks = mddev->raid_disks - mddev->delta_disks; 3861 break; 3862 case geo_new: 3863 layout = mddev->new_layout; 3864 chunk = mddev->new_chunk_sectors; 3865 disks = mddev->raid_disks; 3866 break; 3867 default: /* avoid 'may be unused' warnings */ 3868 case geo_start: /* new when starting reshape - raid_disks not 3869 * updated yet. */ 3870 layout = mddev->new_layout; 3871 chunk = mddev->new_chunk_sectors; 3872 disks = mddev->raid_disks + mddev->delta_disks; 3873 break; 3874 } 3875 if (layout >> 19) 3876 return -1; 3877 if (chunk < (PAGE_SIZE >> 9) || 3878 !is_power_of_2(chunk)) 3879 return -2; 3880 nc = layout & 255; 3881 fc = (layout >> 8) & 255; 3882 fo = layout & (1<<16); 3883 geo->raid_disks = disks; 3884 geo->near_copies = nc; 3885 geo->far_copies = fc; 3886 geo->far_offset = fo; 3887 switch (layout >> 17) { 3888 case 0: /* original layout. simple but not always optimal */ 3889 geo->far_set_size = disks; 3890 break; 3891 case 1: /* "improved" layout which was buggy. Hopefully no-one is 3892 * actually using this, but leave code here just in case.*/ 3893 geo->far_set_size = disks/fc; 3894 WARN(geo->far_set_size < fc, 3895 "This RAID10 layout does not provide data safety - please backup and create new array\n"); 3896 break; 3897 case 2: /* "improved" layout fixed to match documentation */ 3898 geo->far_set_size = fc * nc; 3899 break; 3900 default: /* Not a valid layout */ 3901 return -1; 3902 } 3903 geo->chunk_mask = chunk - 1; 3904 geo->chunk_shift = ffz(~chunk); 3905 return nc*fc; 3906 } 3907 3908 static void raid10_free_conf(struct r10conf *conf) 3909 { 3910 if (!conf) 3911 return; 3912 3913 mempool_exit(&conf->r10bio_pool); 3914 kfree(conf->mirrors); 3915 kfree(conf->mirrors_old); 3916 kfree(conf->mirrors_new); 3917 safe_put_page(conf->tmppage); 3918 bioset_exit(&conf->bio_split); 3919 kfree(conf); 3920 } 3921 3922 static struct r10conf *setup_conf(struct mddev *mddev) 3923 { 3924 struct r10conf *conf = NULL; 3925 int err = -EINVAL; 3926 struct geom geo; 3927 int copies; 3928 3929 copies = setup_geo(&geo, mddev, geo_new); 3930 3931 if (copies == -2) { 3932 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n", 3933 mdname(mddev), PAGE_SIZE); 3934 goto out; 3935 } 3936 3937 if (copies < 2 || copies > mddev->raid_disks) { 3938 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n", 3939 mdname(mddev), mddev->new_layout); 3940 goto out; 3941 } 3942 3943 err = -ENOMEM; 3944 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); 3945 if (!conf) 3946 goto out; 3947 3948 /* FIXME calc properly */ 3949 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks), 3950 sizeof(struct raid10_info), 3951 GFP_KERNEL); 3952 if (!conf->mirrors) 3953 goto out; 3954 3955 conf->tmppage = alloc_page(GFP_KERNEL); 3956 if (!conf->tmppage) 3957 goto out; 3958 3959 conf->geo = geo; 3960 conf->copies = copies; 3961 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc, 3962 rbio_pool_free, conf); 3963 if (err) 3964 goto out; 3965 3966 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); 3967 if (err) 3968 goto out; 3969 3970 calc_sectors(conf, mddev->dev_sectors); 3971 if (mddev->reshape_position == MaxSector) { 3972 conf->prev = conf->geo; 3973 conf->reshape_progress = MaxSector; 3974 } else { 3975 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) { 3976 err = -EINVAL; 3977 goto out; 3978 } 3979 conf->reshape_progress = mddev->reshape_position; 3980 if (conf->prev.far_offset) 3981 conf->prev.stride = 1 << conf->prev.chunk_shift; 3982 else 3983 /* far_copies must be 1 */ 3984 conf->prev.stride = conf->dev_sectors; 3985 } 3986 conf->reshape_safe = conf->reshape_progress; 3987 spin_lock_init(&conf->device_lock); 3988 INIT_LIST_HEAD(&conf->retry_list); 3989 INIT_LIST_HEAD(&conf->bio_end_io_list); 3990 3991 seqlock_init(&conf->resync_lock); 3992 init_waitqueue_head(&conf->wait_barrier); 3993 atomic_set(&conf->nr_pending, 0); 3994 3995 err = -ENOMEM; 3996 rcu_assign_pointer(conf->thread, 3997 md_register_thread(raid10d, mddev, "raid10")); 3998 if (!conf->thread) 3999 goto out; 4000 4001 conf->mddev = mddev; 4002 return conf; 4003 4004 out: 4005 raid10_free_conf(conf); 4006 return ERR_PTR(err); 4007 } 4008 4009 static unsigned int raid10_nr_stripes(struct r10conf *conf) 4010 { 4011 unsigned int raid_disks = conf->geo.raid_disks; 4012 4013 if (conf->geo.raid_disks % conf->geo.near_copies) 4014 return raid_disks; 4015 return raid_disks / conf->geo.near_copies; 4016 } 4017 4018 static int raid10_set_queue_limits(struct mddev *mddev) 4019 { 4020 struct r10conf *conf = mddev->private; 4021 struct queue_limits lim; 4022 int err; 4023 4024 md_init_stacking_limits(&lim); 4025 lim.max_write_zeroes_sectors = 0; 4026 lim.io_min = mddev->chunk_sectors << 9; 4027 lim.io_opt = lim.io_min * raid10_nr_stripes(conf); 4028 err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY); 4029 if (err) { 4030 queue_limits_cancel_update(mddev->gendisk->queue); 4031 return err; 4032 } 4033 return queue_limits_set(mddev->gendisk->queue, &lim); 4034 } 4035 4036 static int raid10_run(struct mddev *mddev) 4037 { 4038 struct r10conf *conf; 4039 int i, disk_idx; 4040 struct raid10_info *disk; 4041 struct md_rdev *rdev; 4042 sector_t size; 4043 sector_t min_offset_diff = 0; 4044 int first = 1; 4045 int ret = -EIO; 4046 4047 if (mddev->private == NULL) { 4048 conf = setup_conf(mddev); 4049 if (IS_ERR(conf)) 4050 return PTR_ERR(conf); 4051 mddev->private = conf; 4052 } 4053 conf = mddev->private; 4054 if (!conf) 4055 goto out; 4056 4057 rcu_assign_pointer(mddev->thread, conf->thread); 4058 rcu_assign_pointer(conf->thread, NULL); 4059 4060 if (mddev_is_clustered(conf->mddev)) { 4061 int fc, fo; 4062 4063 fc = (mddev->layout >> 8) & 255; 4064 fo = mddev->layout & (1<<16); 4065 if (fc > 1 || fo > 0) { 4066 pr_err("only near layout is supported by clustered" 4067 " raid10\n"); 4068 goto out_free_conf; 4069 } 4070 } 4071 4072 rdev_for_each(rdev, mddev) { 4073 long long diff; 4074 4075 disk_idx = rdev->raid_disk; 4076 if (disk_idx < 0) 4077 continue; 4078 if (disk_idx >= conf->geo.raid_disks && 4079 disk_idx >= conf->prev.raid_disks) 4080 continue; 4081 disk = conf->mirrors + disk_idx; 4082 4083 if (test_bit(Replacement, &rdev->flags)) { 4084 if (disk->replacement) 4085 goto out_free_conf; 4086 disk->replacement = rdev; 4087 } else { 4088 if (disk->rdev) 4089 goto out_free_conf; 4090 disk->rdev = rdev; 4091 } 4092 diff = (rdev->new_data_offset - rdev->data_offset); 4093 if (!mddev->reshape_backwards) 4094 diff = -diff; 4095 if (diff < 0) 4096 diff = 0; 4097 if (first || diff < min_offset_diff) 4098 min_offset_diff = diff; 4099 4100 disk->head_position = 0; 4101 first = 0; 4102 } 4103 4104 if (!mddev_is_dm(conf->mddev)) { 4105 ret = raid10_set_queue_limits(mddev); 4106 if (ret) 4107 goto out_free_conf; 4108 } 4109 4110 /* need to check that every block has at least one working mirror */ 4111 if (!enough(conf, -1)) { 4112 pr_err("md/raid10:%s: not enough operational mirrors.\n", 4113 mdname(mddev)); 4114 goto out_free_conf; 4115 } 4116 4117 if (conf->reshape_progress != MaxSector) { 4118 /* must ensure that shape change is supported */ 4119 if (conf->geo.far_copies != 1 && 4120 conf->geo.far_offset == 0) 4121 goto out_free_conf; 4122 if (conf->prev.far_copies != 1 && 4123 conf->prev.far_offset == 0) 4124 goto out_free_conf; 4125 } 4126 4127 mddev->degraded = 0; 4128 for (i = 0; 4129 i < conf->geo.raid_disks 4130 || i < conf->prev.raid_disks; 4131 i++) { 4132 4133 disk = conf->mirrors + i; 4134 4135 if (!disk->rdev && disk->replacement) { 4136 /* The replacement is all we have - use it */ 4137 disk->rdev = disk->replacement; 4138 disk->replacement = NULL; 4139 clear_bit(Replacement, &disk->rdev->flags); 4140 } 4141 4142 if (!disk->rdev || 4143 !test_bit(In_sync, &disk->rdev->flags)) { 4144 disk->head_position = 0; 4145 mddev->degraded++; 4146 if (disk->rdev && 4147 disk->rdev->saved_raid_disk < 0) 4148 conf->fullsync = 1; 4149 } 4150 4151 if (disk->replacement && 4152 !test_bit(In_sync, &disk->replacement->flags) && 4153 disk->replacement->saved_raid_disk < 0) { 4154 conf->fullsync = 1; 4155 } 4156 4157 disk->recovery_disabled = mddev->recovery_disabled - 1; 4158 } 4159 4160 if (mddev->recovery_cp != MaxSector) 4161 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n", 4162 mdname(mddev)); 4163 pr_info("md/raid10:%s: active with %d out of %d devices\n", 4164 mdname(mddev), conf->geo.raid_disks - mddev->degraded, 4165 conf->geo.raid_disks); 4166 /* 4167 * Ok, everything is just fine now 4168 */ 4169 mddev->dev_sectors = conf->dev_sectors; 4170 size = raid10_size(mddev, 0, 0); 4171 md_set_array_sectors(mddev, size); 4172 mddev->resync_max_sectors = size; 4173 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags); 4174 4175 if (md_integrity_register(mddev)) 4176 goto out_free_conf; 4177 4178 if (conf->reshape_progress != MaxSector) { 4179 unsigned long before_length, after_length; 4180 4181 before_length = ((1 << conf->prev.chunk_shift) * 4182 conf->prev.far_copies); 4183 after_length = ((1 << conf->geo.chunk_shift) * 4184 conf->geo.far_copies); 4185 4186 if (max(before_length, after_length) > min_offset_diff) { 4187 /* This cannot work */ 4188 pr_warn("md/raid10: offset difference not enough to continue reshape\n"); 4189 goto out_free_conf; 4190 } 4191 conf->offset_diff = min_offset_diff; 4192 4193 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 4194 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 4195 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 4196 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4197 } 4198 4199 return 0; 4200 4201 out_free_conf: 4202 md_unregister_thread(mddev, &mddev->thread); 4203 raid10_free_conf(conf); 4204 mddev->private = NULL; 4205 out: 4206 return ret; 4207 } 4208 4209 static void raid10_free(struct mddev *mddev, void *priv) 4210 { 4211 raid10_free_conf(priv); 4212 } 4213 4214 static void raid10_quiesce(struct mddev *mddev, int quiesce) 4215 { 4216 struct r10conf *conf = mddev->private; 4217 4218 if (quiesce) 4219 raise_barrier(conf, 0); 4220 else 4221 lower_barrier(conf); 4222 } 4223 4224 static int raid10_resize(struct mddev *mddev, sector_t sectors) 4225 { 4226 /* Resize of 'far' arrays is not supported. 4227 * For 'near' and 'offset' arrays we can set the 4228 * number of sectors used to be an appropriate multiple 4229 * of the chunk size. 4230 * For 'offset', this is far_copies*chunksize. 4231 * For 'near' the multiplier is the LCM of 4232 * near_copies and raid_disks. 4233 * So if far_copies > 1 && !far_offset, fail. 4234 * Else find LCM(raid_disks, near_copy)*far_copies and 4235 * multiply by chunk_size. Then round to this number. 4236 * This is mostly done by raid10_size() 4237 */ 4238 struct r10conf *conf = mddev->private; 4239 sector_t oldsize, size; 4240 int ret; 4241 4242 if (mddev->reshape_position != MaxSector) 4243 return -EBUSY; 4244 4245 if (conf->geo.far_copies > 1 && !conf->geo.far_offset) 4246 return -EINVAL; 4247 4248 oldsize = raid10_size(mddev, 0, 0); 4249 size = raid10_size(mddev, sectors, 0); 4250 if (mddev->external_size && 4251 mddev->array_sectors > size) 4252 return -EINVAL; 4253 4254 ret = mddev->bitmap_ops->resize(mddev, size, 0, false); 4255 if (ret) 4256 return ret; 4257 4258 md_set_array_sectors(mddev, size); 4259 if (sectors > mddev->dev_sectors && 4260 mddev->recovery_cp > oldsize) { 4261 mddev->recovery_cp = oldsize; 4262 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4263 } 4264 calc_sectors(conf, sectors); 4265 mddev->dev_sectors = conf->dev_sectors; 4266 mddev->resync_max_sectors = size; 4267 return 0; 4268 } 4269 4270 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs) 4271 { 4272 struct md_rdev *rdev; 4273 struct r10conf *conf; 4274 4275 if (mddev->degraded > 0) { 4276 pr_warn("md/raid10:%s: Error: degraded raid0!\n", 4277 mdname(mddev)); 4278 return ERR_PTR(-EINVAL); 4279 } 4280 sector_div(size, devs); 4281 4282 /* Set new parameters */ 4283 mddev->new_level = 10; 4284 /* new layout: far_copies = 1, near_copies = 2 */ 4285 mddev->new_layout = (1<<8) + 2; 4286 mddev->new_chunk_sectors = mddev->chunk_sectors; 4287 mddev->delta_disks = mddev->raid_disks; 4288 mddev->raid_disks *= 2; 4289 /* make sure it will be not marked as dirty */ 4290 mddev->recovery_cp = MaxSector; 4291 mddev->dev_sectors = size; 4292 4293 conf = setup_conf(mddev); 4294 if (!IS_ERR(conf)) { 4295 rdev_for_each(rdev, mddev) 4296 if (rdev->raid_disk >= 0) { 4297 rdev->new_raid_disk = rdev->raid_disk * 2; 4298 rdev->sectors = size; 4299 } 4300 } 4301 4302 return conf; 4303 } 4304 4305 static void *raid10_takeover(struct mddev *mddev) 4306 { 4307 struct r0conf *raid0_conf; 4308 4309 /* raid10 can take over: 4310 * raid0 - providing it has only two drives 4311 */ 4312 if (mddev->level == 0) { 4313 /* for raid0 takeover only one zone is supported */ 4314 raid0_conf = mddev->private; 4315 if (raid0_conf->nr_strip_zones > 1) { 4316 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n", 4317 mdname(mddev)); 4318 return ERR_PTR(-EINVAL); 4319 } 4320 return raid10_takeover_raid0(mddev, 4321 raid0_conf->strip_zone->zone_end, 4322 raid0_conf->strip_zone->nb_dev); 4323 } 4324 return ERR_PTR(-EINVAL); 4325 } 4326 4327 static int raid10_check_reshape(struct mddev *mddev) 4328 { 4329 /* Called when there is a request to change 4330 * - layout (to ->new_layout) 4331 * - chunk size (to ->new_chunk_sectors) 4332 * - raid_disks (by delta_disks) 4333 * or when trying to restart a reshape that was ongoing. 4334 * 4335 * We need to validate the request and possibly allocate 4336 * space if that might be an issue later. 4337 * 4338 * Currently we reject any reshape of a 'far' mode array, 4339 * allow chunk size to change if new is generally acceptable, 4340 * allow raid_disks to increase, and allow 4341 * a switch between 'near' mode and 'offset' mode. 4342 */ 4343 struct r10conf *conf = mddev->private; 4344 struct geom geo; 4345 4346 if (conf->geo.far_copies != 1 && !conf->geo.far_offset) 4347 return -EINVAL; 4348 4349 if (setup_geo(&geo, mddev, geo_start) != conf->copies) 4350 /* mustn't change number of copies */ 4351 return -EINVAL; 4352 if (geo.far_copies > 1 && !geo.far_offset) 4353 /* Cannot switch to 'far' mode */ 4354 return -EINVAL; 4355 4356 if (mddev->array_sectors & geo.chunk_mask) 4357 /* not factor of array size */ 4358 return -EINVAL; 4359 4360 if (!enough(conf, -1)) 4361 return -EINVAL; 4362 4363 kfree(conf->mirrors_new); 4364 conf->mirrors_new = NULL; 4365 if (mddev->delta_disks > 0) { 4366 /* allocate new 'mirrors' list */ 4367 conf->mirrors_new = 4368 kcalloc(mddev->raid_disks + mddev->delta_disks, 4369 sizeof(struct raid10_info), 4370 GFP_KERNEL); 4371 if (!conf->mirrors_new) 4372 return -ENOMEM; 4373 } 4374 return 0; 4375 } 4376 4377 /* 4378 * Need to check if array has failed when deciding whether to: 4379 * - start an array 4380 * - remove non-faulty devices 4381 * - add a spare 4382 * - allow a reshape 4383 * This determination is simple when no reshape is happening. 4384 * However if there is a reshape, we need to carefully check 4385 * both the before and after sections. 4386 * This is because some failed devices may only affect one 4387 * of the two sections, and some non-in_sync devices may 4388 * be insync in the section most affected by failed devices. 4389 */ 4390 static int calc_degraded(struct r10conf *conf) 4391 { 4392 int degraded, degraded2; 4393 int i; 4394 4395 degraded = 0; 4396 /* 'prev' section first */ 4397 for (i = 0; i < conf->prev.raid_disks; i++) { 4398 struct md_rdev *rdev = conf->mirrors[i].rdev; 4399 4400 if (!rdev || test_bit(Faulty, &rdev->flags)) 4401 degraded++; 4402 else if (!test_bit(In_sync, &rdev->flags)) 4403 /* When we can reduce the number of devices in 4404 * an array, this might not contribute to 4405 * 'degraded'. It does now. 4406 */ 4407 degraded++; 4408 } 4409 if (conf->geo.raid_disks == conf->prev.raid_disks) 4410 return degraded; 4411 degraded2 = 0; 4412 for (i = 0; i < conf->geo.raid_disks; i++) { 4413 struct md_rdev *rdev = conf->mirrors[i].rdev; 4414 4415 if (!rdev || test_bit(Faulty, &rdev->flags)) 4416 degraded2++; 4417 else if (!test_bit(In_sync, &rdev->flags)) { 4418 /* If reshape is increasing the number of devices, 4419 * this section has already been recovered, so 4420 * it doesn't contribute to degraded. 4421 * else it does. 4422 */ 4423 if (conf->geo.raid_disks <= conf->prev.raid_disks) 4424 degraded2++; 4425 } 4426 } 4427 if (degraded2 > degraded) 4428 return degraded2; 4429 return degraded; 4430 } 4431 4432 static int raid10_start_reshape(struct mddev *mddev) 4433 { 4434 /* A 'reshape' has been requested. This commits 4435 * the various 'new' fields and sets MD_RECOVER_RESHAPE 4436 * This also checks if there are enough spares and adds them 4437 * to the array. 4438 * We currently require enough spares to make the final 4439 * array non-degraded. We also require that the difference 4440 * between old and new data_offset - on each device - is 4441 * enough that we never risk over-writing. 4442 */ 4443 4444 unsigned long before_length, after_length; 4445 sector_t min_offset_diff = 0; 4446 int first = 1; 4447 struct geom new; 4448 struct r10conf *conf = mddev->private; 4449 struct md_rdev *rdev; 4450 int spares = 0; 4451 int ret; 4452 4453 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 4454 return -EBUSY; 4455 4456 if (setup_geo(&new, mddev, geo_start) != conf->copies) 4457 return -EINVAL; 4458 4459 before_length = ((1 << conf->prev.chunk_shift) * 4460 conf->prev.far_copies); 4461 after_length = ((1 << conf->geo.chunk_shift) * 4462 conf->geo.far_copies); 4463 4464 rdev_for_each(rdev, mddev) { 4465 if (!test_bit(In_sync, &rdev->flags) 4466 && !test_bit(Faulty, &rdev->flags)) 4467 spares++; 4468 if (rdev->raid_disk >= 0) { 4469 long long diff = (rdev->new_data_offset 4470 - rdev->data_offset); 4471 if (!mddev->reshape_backwards) 4472 diff = -diff; 4473 if (diff < 0) 4474 diff = 0; 4475 if (first || diff < min_offset_diff) 4476 min_offset_diff = diff; 4477 first = 0; 4478 } 4479 } 4480 4481 if (max(before_length, after_length) > min_offset_diff) 4482 return -EINVAL; 4483 4484 if (spares < mddev->delta_disks) 4485 return -EINVAL; 4486 4487 conf->offset_diff = min_offset_diff; 4488 spin_lock_irq(&conf->device_lock); 4489 if (conf->mirrors_new) { 4490 memcpy(conf->mirrors_new, conf->mirrors, 4491 sizeof(struct raid10_info)*conf->prev.raid_disks); 4492 smp_mb(); 4493 kfree(conf->mirrors_old); 4494 conf->mirrors_old = conf->mirrors; 4495 conf->mirrors = conf->mirrors_new; 4496 conf->mirrors_new = NULL; 4497 } 4498 setup_geo(&conf->geo, mddev, geo_start); 4499 smp_mb(); 4500 if (mddev->reshape_backwards) { 4501 sector_t size = raid10_size(mddev, 0, 0); 4502 if (size < mddev->array_sectors) { 4503 spin_unlock_irq(&conf->device_lock); 4504 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n", 4505 mdname(mddev)); 4506 return -EINVAL; 4507 } 4508 mddev->resync_max_sectors = size; 4509 conf->reshape_progress = size; 4510 } else 4511 conf->reshape_progress = 0; 4512 conf->reshape_safe = conf->reshape_progress; 4513 spin_unlock_irq(&conf->device_lock); 4514 4515 if (mddev->delta_disks && mddev->bitmap) { 4516 struct mdp_superblock_1 *sb = NULL; 4517 sector_t oldsize, newsize; 4518 4519 oldsize = raid10_size(mddev, 0, 0); 4520 newsize = raid10_size(mddev, 0, conf->geo.raid_disks); 4521 4522 if (!mddev_is_clustered(mddev)) { 4523 ret = mddev->bitmap_ops->resize(mddev, newsize, 0, false); 4524 if (ret) 4525 goto abort; 4526 else 4527 goto out; 4528 } 4529 4530 rdev_for_each(rdev, mddev) { 4531 if (rdev->raid_disk > -1 && 4532 !test_bit(Faulty, &rdev->flags)) 4533 sb = page_address(rdev->sb_page); 4534 } 4535 4536 /* 4537 * some node is already performing reshape, and no need to 4538 * call bitmap_ops->resize again since it should be called when 4539 * receiving BITMAP_RESIZE msg 4540 */ 4541 if ((sb && (le32_to_cpu(sb->feature_map) & 4542 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize)) 4543 goto out; 4544 4545 ret = mddev->bitmap_ops->resize(mddev, newsize, 0, false); 4546 if (ret) 4547 goto abort; 4548 4549 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize); 4550 if (ret) { 4551 mddev->bitmap_ops->resize(mddev, oldsize, 0, false); 4552 goto abort; 4553 } 4554 } 4555 out: 4556 if (mddev->delta_disks > 0) { 4557 rdev_for_each(rdev, mddev) 4558 if (rdev->raid_disk < 0 && 4559 !test_bit(Faulty, &rdev->flags)) { 4560 if (raid10_add_disk(mddev, rdev) == 0) { 4561 if (rdev->raid_disk >= 4562 conf->prev.raid_disks) 4563 set_bit(In_sync, &rdev->flags); 4564 else 4565 rdev->recovery_offset = 0; 4566 4567 /* Failure here is OK */ 4568 sysfs_link_rdev(mddev, rdev); 4569 } 4570 } else if (rdev->raid_disk >= conf->prev.raid_disks 4571 && !test_bit(Faulty, &rdev->flags)) { 4572 /* This is a spare that was manually added */ 4573 set_bit(In_sync, &rdev->flags); 4574 } 4575 } 4576 /* When a reshape changes the number of devices, 4577 * ->degraded is measured against the larger of the 4578 * pre and post numbers. 4579 */ 4580 spin_lock_irq(&conf->device_lock); 4581 mddev->degraded = calc_degraded(conf); 4582 spin_unlock_irq(&conf->device_lock); 4583 mddev->raid_disks = conf->geo.raid_disks; 4584 mddev->reshape_position = conf->reshape_progress; 4585 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 4586 4587 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 4588 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 4589 clear_bit(MD_RECOVERY_DONE, &mddev->recovery); 4590 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 4591 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4592 conf->reshape_checkpoint = jiffies; 4593 md_new_event(); 4594 return 0; 4595 4596 abort: 4597 mddev->recovery = 0; 4598 spin_lock_irq(&conf->device_lock); 4599 conf->geo = conf->prev; 4600 mddev->raid_disks = conf->geo.raid_disks; 4601 rdev_for_each(rdev, mddev) 4602 rdev->new_data_offset = rdev->data_offset; 4603 smp_wmb(); 4604 conf->reshape_progress = MaxSector; 4605 conf->reshape_safe = MaxSector; 4606 mddev->reshape_position = MaxSector; 4607 spin_unlock_irq(&conf->device_lock); 4608 return ret; 4609 } 4610 4611 /* Calculate the last device-address that could contain 4612 * any block from the chunk that includes the array-address 's' 4613 * and report the next address. 4614 * i.e. the address returned will be chunk-aligned and after 4615 * any data that is in the chunk containing 's'. 4616 */ 4617 static sector_t last_dev_address(sector_t s, struct geom *geo) 4618 { 4619 s = (s | geo->chunk_mask) + 1; 4620 s >>= geo->chunk_shift; 4621 s *= geo->near_copies; 4622 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks); 4623 s *= geo->far_copies; 4624 s <<= geo->chunk_shift; 4625 return s; 4626 } 4627 4628 /* Calculate the first device-address that could contain 4629 * any block from the chunk that includes the array-address 's'. 4630 * This too will be the start of a chunk 4631 */ 4632 static sector_t first_dev_address(sector_t s, struct geom *geo) 4633 { 4634 s >>= geo->chunk_shift; 4635 s *= geo->near_copies; 4636 sector_div(s, geo->raid_disks); 4637 s *= geo->far_copies; 4638 s <<= geo->chunk_shift; 4639 return s; 4640 } 4641 4642 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, 4643 int *skipped) 4644 { 4645 /* We simply copy at most one chunk (smallest of old and new) 4646 * at a time, possibly less if that exceeds RESYNC_PAGES, 4647 * or we hit a bad block or something. 4648 * This might mean we pause for normal IO in the middle of 4649 * a chunk, but that is not a problem as mddev->reshape_position 4650 * can record any location. 4651 * 4652 * If we will want to write to a location that isn't 4653 * yet recorded as 'safe' (i.e. in metadata on disk) then 4654 * we need to flush all reshape requests and update the metadata. 4655 * 4656 * When reshaping forwards (e.g. to more devices), we interpret 4657 * 'safe' as the earliest block which might not have been copied 4658 * down yet. We divide this by previous stripe size and multiply 4659 * by previous stripe length to get lowest device offset that we 4660 * cannot write to yet. 4661 * We interpret 'sector_nr' as an address that we want to write to. 4662 * From this we use last_device_address() to find where we might 4663 * write to, and first_device_address on the 'safe' position. 4664 * If this 'next' write position is after the 'safe' position, 4665 * we must update the metadata to increase the 'safe' position. 4666 * 4667 * When reshaping backwards, we round in the opposite direction 4668 * and perform the reverse test: next write position must not be 4669 * less than current safe position. 4670 * 4671 * In all this the minimum difference in data offsets 4672 * (conf->offset_diff - always positive) allows a bit of slack, 4673 * so next can be after 'safe', but not by more than offset_diff 4674 * 4675 * We need to prepare all the bios here before we start any IO 4676 * to ensure the size we choose is acceptable to all devices. 4677 * The means one for each copy for write-out and an extra one for 4678 * read-in. 4679 * We store the read-in bio in ->master_bio and the others in 4680 * ->devs[x].bio and ->devs[x].repl_bio. 4681 */ 4682 struct r10conf *conf = mddev->private; 4683 struct r10bio *r10_bio; 4684 sector_t next, safe, last; 4685 int max_sectors; 4686 int nr_sectors; 4687 int s; 4688 struct md_rdev *rdev; 4689 int need_flush = 0; 4690 struct bio *blist; 4691 struct bio *bio, *read_bio; 4692 int sectors_done = 0; 4693 struct page **pages; 4694 4695 if (sector_nr == 0) { 4696 /* If restarting in the middle, skip the initial sectors */ 4697 if (mddev->reshape_backwards && 4698 conf->reshape_progress < raid10_size(mddev, 0, 0)) { 4699 sector_nr = (raid10_size(mddev, 0, 0) 4700 - conf->reshape_progress); 4701 } else if (!mddev->reshape_backwards && 4702 conf->reshape_progress > 0) 4703 sector_nr = conf->reshape_progress; 4704 if (sector_nr) { 4705 mddev->curr_resync_completed = sector_nr; 4706 sysfs_notify_dirent_safe(mddev->sysfs_completed); 4707 *skipped = 1; 4708 return sector_nr; 4709 } 4710 } 4711 4712 /* We don't use sector_nr to track where we are up to 4713 * as that doesn't work well for ->reshape_backwards. 4714 * So just use ->reshape_progress. 4715 */ 4716 if (mddev->reshape_backwards) { 4717 /* 'next' is the earliest device address that we might 4718 * write to for this chunk in the new layout 4719 */ 4720 next = first_dev_address(conf->reshape_progress - 1, 4721 &conf->geo); 4722 4723 /* 'safe' is the last device address that we might read from 4724 * in the old layout after a restart 4725 */ 4726 safe = last_dev_address(conf->reshape_safe - 1, 4727 &conf->prev); 4728 4729 if (next + conf->offset_diff < safe) 4730 need_flush = 1; 4731 4732 last = conf->reshape_progress - 1; 4733 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask 4734 & conf->prev.chunk_mask); 4735 if (sector_nr + RESYNC_SECTORS < last) 4736 sector_nr = last + 1 - RESYNC_SECTORS; 4737 } else { 4738 /* 'next' is after the last device address that we 4739 * might write to for this chunk in the new layout 4740 */ 4741 next = last_dev_address(conf->reshape_progress, &conf->geo); 4742 4743 /* 'safe' is the earliest device address that we might 4744 * read from in the old layout after a restart 4745 */ 4746 safe = first_dev_address(conf->reshape_safe, &conf->prev); 4747 4748 /* Need to update metadata if 'next' might be beyond 'safe' 4749 * as that would possibly corrupt data 4750 */ 4751 if (next > safe + conf->offset_diff) 4752 need_flush = 1; 4753 4754 sector_nr = conf->reshape_progress; 4755 last = sector_nr | (conf->geo.chunk_mask 4756 & conf->prev.chunk_mask); 4757 4758 if (sector_nr + RESYNC_SECTORS <= last) 4759 last = sector_nr + RESYNC_SECTORS - 1; 4760 } 4761 4762 if (need_flush || 4763 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { 4764 /* Need to update reshape_position in metadata */ 4765 wait_barrier(conf, false); 4766 mddev->reshape_position = conf->reshape_progress; 4767 if (mddev->reshape_backwards) 4768 mddev->curr_resync_completed = raid10_size(mddev, 0, 0) 4769 - conf->reshape_progress; 4770 else 4771 mddev->curr_resync_completed = conf->reshape_progress; 4772 conf->reshape_checkpoint = jiffies; 4773 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 4774 md_wakeup_thread(mddev->thread); 4775 wait_event(mddev->sb_wait, mddev->sb_flags == 0 || 4776 test_bit(MD_RECOVERY_INTR, &mddev->recovery)); 4777 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { 4778 allow_barrier(conf); 4779 return sectors_done; 4780 } 4781 conf->reshape_safe = mddev->reshape_position; 4782 allow_barrier(conf); 4783 } 4784 4785 raise_barrier(conf, 0); 4786 read_more: 4787 /* Now schedule reads for blocks from sector_nr to last */ 4788 r10_bio = raid10_alloc_init_r10buf(conf); 4789 r10_bio->state = 0; 4790 raise_barrier(conf, 1); 4791 atomic_set(&r10_bio->remaining, 0); 4792 r10_bio->mddev = mddev; 4793 r10_bio->sector = sector_nr; 4794 set_bit(R10BIO_IsReshape, &r10_bio->state); 4795 r10_bio->sectors = last - sector_nr + 1; 4796 rdev = read_balance(conf, r10_bio, &max_sectors); 4797 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state)); 4798 4799 if (!rdev) { 4800 /* Cannot read from here, so need to record bad blocks 4801 * on all the target devices. 4802 */ 4803 // FIXME 4804 mempool_free(r10_bio, &conf->r10buf_pool); 4805 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 4806 return sectors_done; 4807 } 4808 4809 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ, 4810 GFP_KERNEL, &mddev->bio_set); 4811 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr 4812 + rdev->data_offset); 4813 read_bio->bi_private = r10_bio; 4814 read_bio->bi_end_io = end_reshape_read; 4815 r10_bio->master_bio = read_bio; 4816 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum; 4817 4818 /* 4819 * Broadcast RESYNC message to other nodes, so all nodes would not 4820 * write to the region to avoid conflict. 4821 */ 4822 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) { 4823 struct mdp_superblock_1 *sb = NULL; 4824 int sb_reshape_pos = 0; 4825 4826 conf->cluster_sync_low = sector_nr; 4827 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS; 4828 sb = page_address(rdev->sb_page); 4829 if (sb) { 4830 sb_reshape_pos = le64_to_cpu(sb->reshape_position); 4831 /* 4832 * Set cluster_sync_low again if next address for array 4833 * reshape is less than cluster_sync_low. Since we can't 4834 * update cluster_sync_low until it has finished reshape. 4835 */ 4836 if (sb_reshape_pos < conf->cluster_sync_low) 4837 conf->cluster_sync_low = sb_reshape_pos; 4838 } 4839 4840 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low, 4841 conf->cluster_sync_high); 4842 } 4843 4844 /* Now find the locations in the new layout */ 4845 __raid10_find_phys(&conf->geo, r10_bio); 4846 4847 blist = read_bio; 4848 read_bio->bi_next = NULL; 4849 4850 for (s = 0; s < conf->copies*2; s++) { 4851 struct bio *b; 4852 int d = r10_bio->devs[s/2].devnum; 4853 struct md_rdev *rdev2; 4854 if (s&1) { 4855 rdev2 = conf->mirrors[d].replacement; 4856 b = r10_bio->devs[s/2].repl_bio; 4857 } else { 4858 rdev2 = conf->mirrors[d].rdev; 4859 b = r10_bio->devs[s/2].bio; 4860 } 4861 if (!rdev2 || test_bit(Faulty, &rdev2->flags)) 4862 continue; 4863 4864 bio_set_dev(b, rdev2->bdev); 4865 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr + 4866 rdev2->new_data_offset; 4867 b->bi_end_io = end_reshape_write; 4868 b->bi_opf = REQ_OP_WRITE; 4869 b->bi_next = blist; 4870 blist = b; 4871 } 4872 4873 /* Now add as many pages as possible to all of these bios. */ 4874 4875 nr_sectors = 0; 4876 pages = get_resync_pages(r10_bio->devs[0].bio)->pages; 4877 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) { 4878 struct page *page = pages[s / (PAGE_SIZE >> 9)]; 4879 int len = (max_sectors - s) << 9; 4880 if (len > PAGE_SIZE) 4881 len = PAGE_SIZE; 4882 for (bio = blist; bio ; bio = bio->bi_next) { 4883 if (WARN_ON(!bio_add_page(bio, page, len, 0))) { 4884 bio->bi_status = BLK_STS_RESOURCE; 4885 bio_endio(bio); 4886 return sectors_done; 4887 } 4888 } 4889 sector_nr += len >> 9; 4890 nr_sectors += len >> 9; 4891 } 4892 r10_bio->sectors = nr_sectors; 4893 4894 /* Now submit the read */ 4895 md_sync_acct_bio(read_bio, r10_bio->sectors); 4896 atomic_inc(&r10_bio->remaining); 4897 read_bio->bi_next = NULL; 4898 submit_bio_noacct(read_bio); 4899 sectors_done += nr_sectors; 4900 if (sector_nr <= last) 4901 goto read_more; 4902 4903 lower_barrier(conf); 4904 4905 /* Now that we have done the whole section we can 4906 * update reshape_progress 4907 */ 4908 if (mddev->reshape_backwards) 4909 conf->reshape_progress -= sectors_done; 4910 else 4911 conf->reshape_progress += sectors_done; 4912 4913 return sectors_done; 4914 } 4915 4916 static void end_reshape_request(struct r10bio *r10_bio); 4917 static int handle_reshape_read_error(struct mddev *mddev, 4918 struct r10bio *r10_bio); 4919 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio) 4920 { 4921 /* Reshape read completed. Hopefully we have a block 4922 * to write out. 4923 * If we got a read error then we do sync 1-page reads from 4924 * elsewhere until we find the data - or give up. 4925 */ 4926 struct r10conf *conf = mddev->private; 4927 int s; 4928 4929 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) 4930 if (handle_reshape_read_error(mddev, r10_bio) < 0) { 4931 /* Reshape has been aborted */ 4932 md_done_sync(mddev, r10_bio->sectors, 0); 4933 return; 4934 } 4935 4936 /* We definitely have the data in the pages, schedule the 4937 * writes. 4938 */ 4939 atomic_set(&r10_bio->remaining, 1); 4940 for (s = 0; s < conf->copies*2; s++) { 4941 struct bio *b; 4942 int d = r10_bio->devs[s/2].devnum; 4943 struct md_rdev *rdev; 4944 if (s&1) { 4945 rdev = conf->mirrors[d].replacement; 4946 b = r10_bio->devs[s/2].repl_bio; 4947 } else { 4948 rdev = conf->mirrors[d].rdev; 4949 b = r10_bio->devs[s/2].bio; 4950 } 4951 if (!rdev || test_bit(Faulty, &rdev->flags)) 4952 continue; 4953 4954 atomic_inc(&rdev->nr_pending); 4955 md_sync_acct_bio(b, r10_bio->sectors); 4956 atomic_inc(&r10_bio->remaining); 4957 b->bi_next = NULL; 4958 submit_bio_noacct(b); 4959 } 4960 end_reshape_request(r10_bio); 4961 } 4962 4963 static void end_reshape(struct r10conf *conf) 4964 { 4965 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) 4966 return; 4967 4968 spin_lock_irq(&conf->device_lock); 4969 conf->prev = conf->geo; 4970 md_finish_reshape(conf->mddev); 4971 smp_wmb(); 4972 conf->reshape_progress = MaxSector; 4973 conf->reshape_safe = MaxSector; 4974 spin_unlock_irq(&conf->device_lock); 4975 4976 mddev_update_io_opt(conf->mddev, raid10_nr_stripes(conf)); 4977 conf->fullsync = 0; 4978 } 4979 4980 static void raid10_update_reshape_pos(struct mddev *mddev) 4981 { 4982 struct r10conf *conf = mddev->private; 4983 sector_t lo, hi; 4984 4985 md_cluster_ops->resync_info_get(mddev, &lo, &hi); 4986 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo)) 4987 || mddev->reshape_position == MaxSector) 4988 conf->reshape_progress = mddev->reshape_position; 4989 else 4990 WARN_ON_ONCE(1); 4991 } 4992 4993 static int handle_reshape_read_error(struct mddev *mddev, 4994 struct r10bio *r10_bio) 4995 { 4996 /* Use sync reads to get the blocks from somewhere else */ 4997 int sectors = r10_bio->sectors; 4998 struct r10conf *conf = mddev->private; 4999 struct r10bio *r10b; 5000 int slot = 0; 5001 int idx = 0; 5002 struct page **pages; 5003 5004 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO); 5005 if (!r10b) { 5006 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 5007 return -ENOMEM; 5008 } 5009 5010 /* reshape IOs share pages from .devs[0].bio */ 5011 pages = get_resync_pages(r10_bio->devs[0].bio)->pages; 5012 5013 r10b->sector = r10_bio->sector; 5014 __raid10_find_phys(&conf->prev, r10b); 5015 5016 while (sectors) { 5017 int s = sectors; 5018 int success = 0; 5019 int first_slot = slot; 5020 5021 if (s > (PAGE_SIZE >> 9)) 5022 s = PAGE_SIZE >> 9; 5023 5024 while (!success) { 5025 int d = r10b->devs[slot].devnum; 5026 struct md_rdev *rdev = conf->mirrors[d].rdev; 5027 sector_t addr; 5028 if (rdev == NULL || 5029 test_bit(Faulty, &rdev->flags) || 5030 !test_bit(In_sync, &rdev->flags)) 5031 goto failed; 5032 5033 addr = r10b->devs[slot].addr + idx * PAGE_SIZE; 5034 atomic_inc(&rdev->nr_pending); 5035 success = sync_page_io(rdev, 5036 addr, 5037 s << 9, 5038 pages[idx], 5039 REQ_OP_READ, false); 5040 rdev_dec_pending(rdev, mddev); 5041 if (success) 5042 break; 5043 failed: 5044 slot++; 5045 if (slot >= conf->copies) 5046 slot = 0; 5047 if (slot == first_slot) 5048 break; 5049 } 5050 if (!success) { 5051 /* couldn't read this block, must give up */ 5052 set_bit(MD_RECOVERY_INTR, 5053 &mddev->recovery); 5054 kfree(r10b); 5055 return -EIO; 5056 } 5057 sectors -= s; 5058 idx++; 5059 } 5060 kfree(r10b); 5061 return 0; 5062 } 5063 5064 static void end_reshape_write(struct bio *bio) 5065 { 5066 struct r10bio *r10_bio = get_resync_r10bio(bio); 5067 struct mddev *mddev = r10_bio->mddev; 5068 struct r10conf *conf = mddev->private; 5069 int d; 5070 int slot; 5071 int repl; 5072 struct md_rdev *rdev = NULL; 5073 5074 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 5075 rdev = repl ? conf->mirrors[d].replacement : 5076 conf->mirrors[d].rdev; 5077 5078 if (bio->bi_status) { 5079 /* FIXME should record badblock */ 5080 md_error(mddev, rdev); 5081 } 5082 5083 rdev_dec_pending(rdev, mddev); 5084 end_reshape_request(r10_bio); 5085 } 5086 5087 static void end_reshape_request(struct r10bio *r10_bio) 5088 { 5089 if (!atomic_dec_and_test(&r10_bio->remaining)) 5090 return; 5091 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1); 5092 bio_put(r10_bio->master_bio); 5093 put_buf(r10_bio); 5094 } 5095 5096 static void raid10_finish_reshape(struct mddev *mddev) 5097 { 5098 struct r10conf *conf = mddev->private; 5099 5100 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 5101 return; 5102 5103 if (mddev->delta_disks > 0) { 5104 if (mddev->recovery_cp > mddev->resync_max_sectors) { 5105 mddev->recovery_cp = mddev->resync_max_sectors; 5106 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5107 } 5108 mddev->resync_max_sectors = mddev->array_sectors; 5109 } else { 5110 int d; 5111 for (d = conf->geo.raid_disks ; 5112 d < conf->geo.raid_disks - mddev->delta_disks; 5113 d++) { 5114 struct md_rdev *rdev = conf->mirrors[d].rdev; 5115 if (rdev) 5116 clear_bit(In_sync, &rdev->flags); 5117 rdev = conf->mirrors[d].replacement; 5118 if (rdev) 5119 clear_bit(In_sync, &rdev->flags); 5120 } 5121 } 5122 mddev->layout = mddev->new_layout; 5123 mddev->chunk_sectors = 1 << conf->geo.chunk_shift; 5124 mddev->reshape_position = MaxSector; 5125 mddev->delta_disks = 0; 5126 mddev->reshape_backwards = 0; 5127 } 5128 5129 static struct md_personality raid10_personality = 5130 { 5131 .name = "raid10", 5132 .level = 10, 5133 .owner = THIS_MODULE, 5134 .make_request = raid10_make_request, 5135 .run = raid10_run, 5136 .free = raid10_free, 5137 .status = raid10_status, 5138 .error_handler = raid10_error, 5139 .hot_add_disk = raid10_add_disk, 5140 .hot_remove_disk= raid10_remove_disk, 5141 .spare_active = raid10_spare_active, 5142 .sync_request = raid10_sync_request, 5143 .quiesce = raid10_quiesce, 5144 .size = raid10_size, 5145 .resize = raid10_resize, 5146 .takeover = raid10_takeover, 5147 .check_reshape = raid10_check_reshape, 5148 .start_reshape = raid10_start_reshape, 5149 .finish_reshape = raid10_finish_reshape, 5150 .update_reshape_pos = raid10_update_reshape_pos, 5151 }; 5152 5153 static int __init raid_init(void) 5154 { 5155 return register_md_personality(&raid10_personality); 5156 } 5157 5158 static void raid_exit(void) 5159 { 5160 unregister_md_personality(&raid10_personality); 5161 } 5162 5163 module_init(raid_init); 5164 module_exit(raid_exit); 5165 MODULE_LICENSE("GPL"); 5166 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); 5167 MODULE_ALIAS("md-personality-9"); /* RAID10 */ 5168 MODULE_ALIAS("md-raid10"); 5169 MODULE_ALIAS("md-level-10"); 5170