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