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 r10_bio->master_bio = bio; 1739 set_bit(R10BIO_Discard, &r10_bio->state); 1740 first_copy = false; 1741 first_r10bio = r10_bio; 1742 } else 1743 r10_bio->master_bio = (struct bio *)first_r10bio; 1744 1745 /* 1746 * first select target devices under rcu_lock and 1747 * inc refcount on their rdev. Record them by setting 1748 * bios[x] to bio 1749 */ 1750 for (disk = 0; disk < geo->raid_disks; disk++) { 1751 struct md_rdev *rdev, *rrdev; 1752 1753 rdev = conf->mirrors[disk].rdev; 1754 rrdev = conf->mirrors[disk].replacement; 1755 r10_bio->devs[disk].bio = NULL; 1756 r10_bio->devs[disk].repl_bio = NULL; 1757 1758 if (rdev && (test_bit(Faulty, &rdev->flags))) 1759 rdev = NULL; 1760 if (rrdev && (test_bit(Faulty, &rrdev->flags))) 1761 rrdev = NULL; 1762 if (!rdev && !rrdev) 1763 continue; 1764 1765 if (rdev) { 1766 r10_bio->devs[disk].bio = bio; 1767 atomic_inc(&rdev->nr_pending); 1768 } 1769 if (rrdev) { 1770 r10_bio->devs[disk].repl_bio = bio; 1771 atomic_inc(&rrdev->nr_pending); 1772 } 1773 } 1774 1775 atomic_set(&r10_bio->remaining, 1); 1776 for (disk = 0; disk < geo->raid_disks; disk++) { 1777 sector_t dev_start, dev_end; 1778 struct bio *mbio, *rbio = NULL; 1779 1780 /* 1781 * Now start to calculate the start and end address for each disk. 1782 * The space between dev_start and dev_end is the discard region. 1783 * 1784 * For dev_start, it needs to consider three conditions: 1785 * 1st, the disk is before start_disk, you can imagine the disk in 1786 * the next stripe. So the dev_start is the start address of next 1787 * stripe. 1788 * 2st, the disk is after start_disk, it means the disk is at the 1789 * same stripe of first disk 1790 * 3st, the first disk itself, we can use start_disk_offset directly 1791 */ 1792 if (disk < start_disk_index) 1793 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors; 1794 else if (disk > start_disk_index) 1795 dev_start = first_stripe_index * mddev->chunk_sectors; 1796 else 1797 dev_start = start_disk_offset; 1798 1799 if (disk < end_disk_index) 1800 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors; 1801 else if (disk > end_disk_index) 1802 dev_end = last_stripe_index * mddev->chunk_sectors; 1803 else 1804 dev_end = end_disk_offset; 1805 1806 /* 1807 * It only handles discard bio which size is >= stripe size, so 1808 * dev_end > dev_start all the time. 1809 * It doesn't need to use rcu lock to get rdev here. We already 1810 * add rdev->nr_pending in the first loop. 1811 */ 1812 if (r10_bio->devs[disk].bio) { 1813 struct md_rdev *rdev = conf->mirrors[disk].rdev; 1814 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, 1815 &mddev->bio_set); 1816 mbio->bi_end_io = raid10_end_discard_request; 1817 mbio->bi_private = r10_bio; 1818 r10_bio->devs[disk].bio = mbio; 1819 r10_bio->devs[disk].devnum = disk; 1820 atomic_inc(&r10_bio->remaining); 1821 md_submit_discard_bio(mddev, rdev, mbio, 1822 dev_start + choose_data_offset(r10_bio, rdev), 1823 dev_end - dev_start); 1824 bio_endio(mbio); 1825 } 1826 if (r10_bio->devs[disk].repl_bio) { 1827 struct md_rdev *rrdev = conf->mirrors[disk].replacement; 1828 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, 1829 &mddev->bio_set); 1830 rbio->bi_end_io = raid10_end_discard_request; 1831 rbio->bi_private = r10_bio; 1832 r10_bio->devs[disk].repl_bio = rbio; 1833 r10_bio->devs[disk].devnum = disk; 1834 atomic_inc(&r10_bio->remaining); 1835 md_submit_discard_bio(mddev, rrdev, rbio, 1836 dev_start + choose_data_offset(r10_bio, rrdev), 1837 dev_end - dev_start); 1838 bio_endio(rbio); 1839 } 1840 } 1841 1842 if (!geo->far_offset && --far_copies) { 1843 first_stripe_index += geo->stride >> geo->chunk_shift; 1844 start_disk_offset += geo->stride; 1845 last_stripe_index += geo->stride >> geo->chunk_shift; 1846 end_disk_offset += geo->stride; 1847 atomic_inc(&first_r10bio->remaining); 1848 raid_end_discard_bio(r10_bio); 1849 wait_barrier(conf, false); 1850 goto retry_discard; 1851 } 1852 1853 raid_end_discard_bio(r10_bio); 1854 1855 return 0; 1856 out: 1857 allow_barrier(conf); 1858 return -EAGAIN; 1859 } 1860 1861 static bool raid10_make_request(struct mddev *mddev, struct bio *bio) 1862 { 1863 struct r10conf *conf = mddev->private; 1864 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask); 1865 int chunk_sects = chunk_mask + 1; 1866 int sectors = bio_sectors(bio); 1867 1868 if (unlikely(bio->bi_opf & REQ_PREFLUSH) 1869 && md_flush_request(mddev, bio)) 1870 return true; 1871 1872 md_write_start(mddev, bio); 1873 1874 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) 1875 if (!raid10_handle_discard(mddev, bio)) 1876 return true; 1877 1878 /* 1879 * If this request crosses a chunk boundary, we need to split 1880 * it. 1881 */ 1882 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) + 1883 sectors > chunk_sects 1884 && (conf->geo.near_copies < conf->geo.raid_disks 1885 || conf->prev.near_copies < 1886 conf->prev.raid_disks))) 1887 sectors = chunk_sects - 1888 (bio->bi_iter.bi_sector & 1889 (chunk_sects - 1)); 1890 __make_request(mddev, bio, sectors); 1891 1892 /* In case raid10d snuck in to freeze_array */ 1893 wake_up_barrier(conf); 1894 return true; 1895 } 1896 1897 static void raid10_status(struct seq_file *seq, struct mddev *mddev) 1898 { 1899 struct r10conf *conf = mddev->private; 1900 int i; 1901 1902 lockdep_assert_held(&mddev->lock); 1903 1904 if (conf->geo.near_copies < conf->geo.raid_disks) 1905 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); 1906 if (conf->geo.near_copies > 1) 1907 seq_printf(seq, " %d near-copies", conf->geo.near_copies); 1908 if (conf->geo.far_copies > 1) { 1909 if (conf->geo.far_offset) 1910 seq_printf(seq, " %d offset-copies", conf->geo.far_copies); 1911 else 1912 seq_printf(seq, " %d far-copies", conf->geo.far_copies); 1913 if (conf->geo.far_set_size != conf->geo.raid_disks) 1914 seq_printf(seq, " %d devices per set", conf->geo.far_set_size); 1915 } 1916 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks, 1917 conf->geo.raid_disks - mddev->degraded); 1918 for (i = 0; i < conf->geo.raid_disks; i++) { 1919 struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev); 1920 1921 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 1922 } 1923 seq_printf(seq, "]"); 1924 } 1925 1926 /* check if there are enough drives for 1927 * every block to appear on atleast one. 1928 * Don't consider the device numbered 'ignore' 1929 * as we might be about to remove it. 1930 */ 1931 static int _enough(struct r10conf *conf, int previous, int ignore) 1932 { 1933 int first = 0; 1934 int has_enough = 0; 1935 int disks, ncopies; 1936 if (previous) { 1937 disks = conf->prev.raid_disks; 1938 ncopies = conf->prev.near_copies; 1939 } else { 1940 disks = conf->geo.raid_disks; 1941 ncopies = conf->geo.near_copies; 1942 } 1943 1944 do { 1945 int n = conf->copies; 1946 int cnt = 0; 1947 int this = first; 1948 while (n--) { 1949 struct md_rdev *rdev; 1950 if (this != ignore && 1951 (rdev = conf->mirrors[this].rdev) && 1952 test_bit(In_sync, &rdev->flags)) 1953 cnt++; 1954 this = (this+1) % disks; 1955 } 1956 if (cnt == 0) 1957 goto out; 1958 first = (first + ncopies) % disks; 1959 } while (first != 0); 1960 has_enough = 1; 1961 out: 1962 return has_enough; 1963 } 1964 1965 static int enough(struct r10conf *conf, int ignore) 1966 { 1967 /* when calling 'enough', both 'prev' and 'geo' must 1968 * be stable. 1969 * This is ensured if ->reconfig_mutex or ->device_lock 1970 * is held. 1971 */ 1972 return _enough(conf, 0, ignore) && 1973 _enough(conf, 1, ignore); 1974 } 1975 1976 /** 1977 * raid10_error() - RAID10 error handler. 1978 * @mddev: affected md device. 1979 * @rdev: member device to fail. 1980 * 1981 * The routine acknowledges &rdev failure and determines new @mddev state. 1982 * If it failed, then: 1983 * - &MD_BROKEN flag is set in &mddev->flags. 1984 * Otherwise, it must be degraded: 1985 * - recovery is interrupted. 1986 * - &mddev->degraded is bumped. 1987 * 1988 * @rdev is marked as &Faulty excluding case when array is failed and 1989 * &mddev->fail_last_dev is off. 1990 */ 1991 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev) 1992 { 1993 struct r10conf *conf = mddev->private; 1994 unsigned long flags; 1995 1996 spin_lock_irqsave(&conf->device_lock, flags); 1997 1998 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) { 1999 set_bit(MD_BROKEN, &mddev->flags); 2000 2001 if (!mddev->fail_last_dev) { 2002 spin_unlock_irqrestore(&conf->device_lock, flags); 2003 return; 2004 } 2005 } 2006 if (test_and_clear_bit(In_sync, &rdev->flags)) 2007 mddev->degraded++; 2008 2009 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 2010 set_bit(Blocked, &rdev->flags); 2011 set_bit(Faulty, &rdev->flags); 2012 set_mask_bits(&mddev->sb_flags, 0, 2013 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); 2014 spin_unlock_irqrestore(&conf->device_lock, flags); 2015 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n" 2016 "md/raid10:%s: Operation continuing on %d devices.\n", 2017 mdname(mddev), rdev->bdev, 2018 mdname(mddev), conf->geo.raid_disks - mddev->degraded); 2019 } 2020 2021 static void print_conf(struct r10conf *conf) 2022 { 2023 int i; 2024 struct md_rdev *rdev; 2025 2026 pr_debug("RAID10 conf printout:\n"); 2027 if (!conf) { 2028 pr_debug("(!conf)\n"); 2029 return; 2030 } 2031 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded, 2032 conf->geo.raid_disks); 2033 2034 lockdep_assert_held(&conf->mddev->reconfig_mutex); 2035 for (i = 0; i < conf->geo.raid_disks; i++) { 2036 rdev = conf->mirrors[i].rdev; 2037 if (rdev) 2038 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n", 2039 i, !test_bit(In_sync, &rdev->flags), 2040 !test_bit(Faulty, &rdev->flags), 2041 rdev->bdev); 2042 } 2043 } 2044 2045 static void close_sync(struct r10conf *conf) 2046 { 2047 wait_barrier(conf, false); 2048 allow_barrier(conf); 2049 2050 mempool_exit(&conf->r10buf_pool); 2051 } 2052 2053 static int raid10_spare_active(struct mddev *mddev) 2054 { 2055 int i; 2056 struct r10conf *conf = mddev->private; 2057 struct raid10_info *tmp; 2058 int count = 0; 2059 unsigned long flags; 2060 2061 /* 2062 * Find all non-in_sync disks within the RAID10 configuration 2063 * and mark them in_sync 2064 */ 2065 for (i = 0; i < conf->geo.raid_disks; i++) { 2066 tmp = conf->mirrors + i; 2067 if (tmp->replacement 2068 && tmp->replacement->recovery_offset == MaxSector 2069 && !test_bit(Faulty, &tmp->replacement->flags) 2070 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { 2071 /* Replacement has just become active */ 2072 if (!tmp->rdev 2073 || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) 2074 count++; 2075 if (tmp->rdev) { 2076 /* Replaced device not technically faulty, 2077 * but we need to be sure it gets removed 2078 * and never re-added. 2079 */ 2080 set_bit(Faulty, &tmp->rdev->flags); 2081 sysfs_notify_dirent_safe( 2082 tmp->rdev->sysfs_state); 2083 } 2084 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); 2085 } else if (tmp->rdev 2086 && tmp->rdev->recovery_offset == MaxSector 2087 && !test_bit(Faulty, &tmp->rdev->flags) 2088 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { 2089 count++; 2090 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); 2091 } 2092 } 2093 spin_lock_irqsave(&conf->device_lock, flags); 2094 mddev->degraded -= count; 2095 spin_unlock_irqrestore(&conf->device_lock, flags); 2096 2097 print_conf(conf); 2098 return count; 2099 } 2100 2101 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) 2102 { 2103 struct r10conf *conf = mddev->private; 2104 int err = -EEXIST; 2105 int mirror, repl_slot = -1; 2106 int first = 0; 2107 int last = conf->geo.raid_disks - 1; 2108 struct raid10_info *p; 2109 2110 if (mddev->recovery_cp < MaxSector) 2111 /* only hot-add to in-sync arrays, as recovery is 2112 * very different from resync 2113 */ 2114 return -EBUSY; 2115 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1)) 2116 return -EINVAL; 2117 2118 if (rdev->raid_disk >= 0) 2119 first = last = rdev->raid_disk; 2120 2121 if (rdev->saved_raid_disk >= first && 2122 rdev->saved_raid_disk < conf->geo.raid_disks && 2123 conf->mirrors[rdev->saved_raid_disk].rdev == NULL) 2124 mirror = rdev->saved_raid_disk; 2125 else 2126 mirror = first; 2127 for ( ; mirror <= last ; mirror++) { 2128 p = &conf->mirrors[mirror]; 2129 if (p->recovery_disabled == mddev->recovery_disabled) 2130 continue; 2131 if (p->rdev) { 2132 if (test_bit(WantReplacement, &p->rdev->flags) && 2133 p->replacement == NULL && repl_slot < 0) 2134 repl_slot = mirror; 2135 continue; 2136 } 2137 2138 err = mddev_stack_new_rdev(mddev, rdev); 2139 if (err) 2140 return err; 2141 p->head_position = 0; 2142 p->recovery_disabled = mddev->recovery_disabled - 1; 2143 rdev->raid_disk = mirror; 2144 err = 0; 2145 if (rdev->saved_raid_disk != mirror) 2146 conf->fullsync = 1; 2147 WRITE_ONCE(p->rdev, rdev); 2148 break; 2149 } 2150 2151 if (err && repl_slot >= 0) { 2152 p = &conf->mirrors[repl_slot]; 2153 clear_bit(In_sync, &rdev->flags); 2154 set_bit(Replacement, &rdev->flags); 2155 rdev->raid_disk = repl_slot; 2156 err = mddev_stack_new_rdev(mddev, rdev); 2157 if (err) 2158 return err; 2159 conf->fullsync = 1; 2160 WRITE_ONCE(p->replacement, rdev); 2161 } 2162 2163 print_conf(conf); 2164 return err; 2165 } 2166 2167 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 2168 { 2169 struct r10conf *conf = mddev->private; 2170 int err = 0; 2171 int number = rdev->raid_disk; 2172 struct md_rdev **rdevp; 2173 struct raid10_info *p; 2174 2175 print_conf(conf); 2176 if (unlikely(number >= mddev->raid_disks)) 2177 return 0; 2178 p = conf->mirrors + number; 2179 if (rdev == p->rdev) 2180 rdevp = &p->rdev; 2181 else if (rdev == p->replacement) 2182 rdevp = &p->replacement; 2183 else 2184 return 0; 2185 2186 if (test_bit(In_sync, &rdev->flags) || 2187 atomic_read(&rdev->nr_pending)) { 2188 err = -EBUSY; 2189 goto abort; 2190 } 2191 /* Only remove non-faulty devices if recovery 2192 * is not possible. 2193 */ 2194 if (!test_bit(Faulty, &rdev->flags) && 2195 mddev->recovery_disabled != p->recovery_disabled && 2196 (!p->replacement || p->replacement == rdev) && 2197 number < conf->geo.raid_disks && 2198 enough(conf, -1)) { 2199 err = -EBUSY; 2200 goto abort; 2201 } 2202 WRITE_ONCE(*rdevp, NULL); 2203 if (p->replacement) { 2204 /* We must have just cleared 'rdev' */ 2205 WRITE_ONCE(p->rdev, p->replacement); 2206 clear_bit(Replacement, &p->replacement->flags); 2207 WRITE_ONCE(p->replacement, NULL); 2208 } 2209 2210 clear_bit(WantReplacement, &rdev->flags); 2211 err = md_integrity_register(mddev); 2212 2213 abort: 2214 2215 print_conf(conf); 2216 return err; 2217 } 2218 2219 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d) 2220 { 2221 struct r10conf *conf = r10_bio->mddev->private; 2222 2223 if (!bio->bi_status) 2224 set_bit(R10BIO_Uptodate, &r10_bio->state); 2225 else 2226 /* The write handler will notice the lack of 2227 * R10BIO_Uptodate and record any errors etc 2228 */ 2229 atomic_add(r10_bio->sectors, 2230 &conf->mirrors[d].rdev->corrected_errors); 2231 2232 /* for reconstruct, we always reschedule after a read. 2233 * for resync, only after all reads 2234 */ 2235 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); 2236 if (test_bit(R10BIO_IsRecover, &r10_bio->state) || 2237 atomic_dec_and_test(&r10_bio->remaining)) { 2238 /* we have read all the blocks, 2239 * do the comparison in process context in raid10d 2240 */ 2241 reschedule_retry(r10_bio); 2242 } 2243 } 2244 2245 static void end_sync_read(struct bio *bio) 2246 { 2247 struct r10bio *r10_bio = get_resync_r10bio(bio); 2248 struct r10conf *conf = r10_bio->mddev->private; 2249 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); 2250 2251 __end_sync_read(r10_bio, bio, d); 2252 } 2253 2254 static void end_reshape_read(struct bio *bio) 2255 { 2256 /* reshape read bio isn't allocated from r10buf_pool */ 2257 struct r10bio *r10_bio = bio->bi_private; 2258 2259 __end_sync_read(r10_bio, bio, r10_bio->read_slot); 2260 } 2261 2262 static void end_sync_request(struct r10bio *r10_bio) 2263 { 2264 struct mddev *mddev = r10_bio->mddev; 2265 2266 while (atomic_dec_and_test(&r10_bio->remaining)) { 2267 if (r10_bio->master_bio == NULL) { 2268 /* the primary of several recovery bios */ 2269 sector_t s = r10_bio->sectors; 2270 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 2271 test_bit(R10BIO_WriteError, &r10_bio->state)) 2272 reschedule_retry(r10_bio); 2273 else 2274 put_buf(r10_bio); 2275 md_done_sync(mddev, s, 1); 2276 break; 2277 } else { 2278 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; 2279 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 2280 test_bit(R10BIO_WriteError, &r10_bio->state)) 2281 reschedule_retry(r10_bio); 2282 else 2283 put_buf(r10_bio); 2284 r10_bio = r10_bio2; 2285 } 2286 } 2287 } 2288 2289 static void end_sync_write(struct bio *bio) 2290 { 2291 struct r10bio *r10_bio = get_resync_r10bio(bio); 2292 struct mddev *mddev = r10_bio->mddev; 2293 struct r10conf *conf = mddev->private; 2294 int d; 2295 int slot; 2296 int repl; 2297 struct md_rdev *rdev = NULL; 2298 2299 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 2300 if (repl) 2301 rdev = conf->mirrors[d].replacement; 2302 else 2303 rdev = conf->mirrors[d].rdev; 2304 2305 if (bio->bi_status) { 2306 if (repl) 2307 md_error(mddev, rdev); 2308 else { 2309 set_bit(WriteErrorSeen, &rdev->flags); 2310 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 2311 set_bit(MD_RECOVERY_NEEDED, 2312 &rdev->mddev->recovery); 2313 set_bit(R10BIO_WriteError, &r10_bio->state); 2314 } 2315 } else if (rdev_has_badblock(rdev, r10_bio->devs[slot].addr, 2316 r10_bio->sectors)) { 2317 set_bit(R10BIO_MadeGood, &r10_bio->state); 2318 } 2319 2320 rdev_dec_pending(rdev, mddev); 2321 2322 end_sync_request(r10_bio); 2323 } 2324 2325 /* 2326 * Note: sync and recover and handled very differently for raid10 2327 * This code is for resync. 2328 * For resync, we read through virtual addresses and read all blocks. 2329 * If there is any error, we schedule a write. The lowest numbered 2330 * drive is authoritative. 2331 * However requests come for physical address, so we need to map. 2332 * For every physical address there are raid_disks/copies virtual addresses, 2333 * which is always are least one, but is not necessarly an integer. 2334 * This means that a physical address can span multiple chunks, so we may 2335 * have to submit multiple io requests for a single sync request. 2336 */ 2337 /* 2338 * We check if all blocks are in-sync and only write to blocks that 2339 * aren't in sync 2340 */ 2341 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) 2342 { 2343 struct r10conf *conf = mddev->private; 2344 int i, first; 2345 struct bio *tbio, *fbio; 2346 int vcnt; 2347 struct page **tpages, **fpages; 2348 2349 atomic_set(&r10_bio->remaining, 1); 2350 2351 /* find the first device with a block */ 2352 for (i=0; i<conf->copies; i++) 2353 if (!r10_bio->devs[i].bio->bi_status) 2354 break; 2355 2356 if (i == conf->copies) 2357 goto done; 2358 2359 first = i; 2360 fbio = r10_bio->devs[i].bio; 2361 fbio->bi_iter.bi_size = r10_bio->sectors << 9; 2362 fbio->bi_iter.bi_idx = 0; 2363 fpages = get_resync_pages(fbio)->pages; 2364 2365 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9); 2366 /* now find blocks with errors */ 2367 for (i=0 ; i < conf->copies ; i++) { 2368 int j, d; 2369 struct md_rdev *rdev; 2370 struct resync_pages *rp; 2371 2372 tbio = r10_bio->devs[i].bio; 2373 2374 if (tbio->bi_end_io != end_sync_read) 2375 continue; 2376 if (i == first) 2377 continue; 2378 2379 tpages = get_resync_pages(tbio)->pages; 2380 d = r10_bio->devs[i].devnum; 2381 rdev = conf->mirrors[d].rdev; 2382 if (!r10_bio->devs[i].bio->bi_status) { 2383 /* We know that the bi_io_vec layout is the same for 2384 * both 'first' and 'i', so we just compare them. 2385 * All vec entries are PAGE_SIZE; 2386 */ 2387 int sectors = r10_bio->sectors; 2388 for (j = 0; j < vcnt; j++) { 2389 int len = PAGE_SIZE; 2390 if (sectors < (len / 512)) 2391 len = sectors * 512; 2392 if (memcmp(page_address(fpages[j]), 2393 page_address(tpages[j]), 2394 len)) 2395 break; 2396 sectors -= len/512; 2397 } 2398 if (j == vcnt) 2399 continue; 2400 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches); 2401 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) 2402 /* Don't fix anything. */ 2403 continue; 2404 } else if (test_bit(FailFast, &rdev->flags)) { 2405 /* Just give up on this device */ 2406 md_error(rdev->mddev, rdev); 2407 continue; 2408 } 2409 /* Ok, we need to write this bio, either to correct an 2410 * inconsistency or to correct an unreadable block. 2411 * First we need to fixup bv_offset, bv_len and 2412 * bi_vecs, as the read request might have corrupted these 2413 */ 2414 rp = get_resync_pages(tbio); 2415 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE); 2416 2417 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size); 2418 2419 rp->raid_bio = r10_bio; 2420 tbio->bi_private = rp; 2421 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr; 2422 tbio->bi_end_io = end_sync_write; 2423 2424 bio_copy_data(tbio, fbio); 2425 2426 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 2427 atomic_inc(&r10_bio->remaining); 2428 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio)); 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 md_sync_acct(conf->mirrors[d].replacement->bdev, 2451 bio_sectors(tbio)); 2452 submit_bio_noacct(tbio); 2453 } 2454 2455 done: 2456 if (atomic_dec_and_test(&r10_bio->remaining)) { 2457 md_done_sync(mddev, r10_bio->sectors, 1); 2458 put_buf(r10_bio); 2459 } 2460 } 2461 2462 /* 2463 * Now for the recovery code. 2464 * Recovery happens across physical sectors. 2465 * We recover all non-is_sync drives by finding the virtual address of 2466 * each, and then choose a working drive that also has that virt address. 2467 * There is a separate r10_bio for each non-in_sync drive. 2468 * Only the first two slots are in use. The first for reading, 2469 * The second for writing. 2470 * 2471 */ 2472 static void fix_recovery_read_error(struct r10bio *r10_bio) 2473 { 2474 /* We got a read error during recovery. 2475 * We repeat the read in smaller page-sized sections. 2476 * If a read succeeds, write it to the new device or record 2477 * a bad block if we cannot. 2478 * If a read fails, record a bad block on both old and 2479 * new devices. 2480 */ 2481 struct mddev *mddev = r10_bio->mddev; 2482 struct r10conf *conf = mddev->private; 2483 struct bio *bio = r10_bio->devs[0].bio; 2484 sector_t sect = 0; 2485 int sectors = r10_bio->sectors; 2486 int idx = 0; 2487 int dr = r10_bio->devs[0].devnum; 2488 int dw = r10_bio->devs[1].devnum; 2489 struct page **pages = get_resync_pages(bio)->pages; 2490 2491 while (sectors) { 2492 int s = sectors; 2493 struct md_rdev *rdev; 2494 sector_t addr; 2495 int ok; 2496 2497 if (s > (PAGE_SIZE>>9)) 2498 s = PAGE_SIZE >> 9; 2499 2500 rdev = conf->mirrors[dr].rdev; 2501 addr = r10_bio->devs[0].addr + sect; 2502 ok = sync_page_io(rdev, 2503 addr, 2504 s << 9, 2505 pages[idx], 2506 REQ_OP_READ, false); 2507 if (ok) { 2508 rdev = conf->mirrors[dw].rdev; 2509 addr = r10_bio->devs[1].addr + sect; 2510 ok = sync_page_io(rdev, 2511 addr, 2512 s << 9, 2513 pages[idx], 2514 REQ_OP_WRITE, false); 2515 if (!ok) { 2516 set_bit(WriteErrorSeen, &rdev->flags); 2517 if (!test_and_set_bit(WantReplacement, 2518 &rdev->flags)) 2519 set_bit(MD_RECOVERY_NEEDED, 2520 &rdev->mddev->recovery); 2521 } 2522 } 2523 if (!ok) { 2524 /* We don't worry if we cannot set a bad block - 2525 * it really is bad so there is no loss in not 2526 * recording it yet 2527 */ 2528 rdev_set_badblocks(rdev, addr, s, 0); 2529 2530 if (rdev != conf->mirrors[dw].rdev) { 2531 /* need bad block on destination too */ 2532 struct md_rdev *rdev2 = conf->mirrors[dw].rdev; 2533 addr = r10_bio->devs[1].addr + sect; 2534 ok = rdev_set_badblocks(rdev2, addr, s, 0); 2535 if (!ok) { 2536 /* just abort the recovery */ 2537 pr_notice("md/raid10:%s: recovery aborted due to read error\n", 2538 mdname(mddev)); 2539 2540 conf->mirrors[dw].recovery_disabled 2541 = mddev->recovery_disabled; 2542 set_bit(MD_RECOVERY_INTR, 2543 &mddev->recovery); 2544 break; 2545 } 2546 } 2547 } 2548 2549 sectors -= s; 2550 sect += s; 2551 idx++; 2552 } 2553 } 2554 2555 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) 2556 { 2557 struct r10conf *conf = mddev->private; 2558 int d; 2559 struct bio *wbio = r10_bio->devs[1].bio; 2560 struct bio *wbio2 = r10_bio->devs[1].repl_bio; 2561 2562 /* Need to test wbio2->bi_end_io before we call 2563 * submit_bio_noacct as if the former is NULL, 2564 * the latter is free to free wbio2. 2565 */ 2566 if (wbio2 && !wbio2->bi_end_io) 2567 wbio2 = NULL; 2568 2569 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { 2570 fix_recovery_read_error(r10_bio); 2571 if (wbio->bi_end_io) 2572 end_sync_request(r10_bio); 2573 if (wbio2) 2574 end_sync_request(r10_bio); 2575 return; 2576 } 2577 2578 /* 2579 * share the pages with the first bio 2580 * and submit the write request 2581 */ 2582 d = r10_bio->devs[1].devnum; 2583 if (wbio->bi_end_io) { 2584 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 2585 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio)); 2586 submit_bio_noacct(wbio); 2587 } 2588 if (wbio2) { 2589 atomic_inc(&conf->mirrors[d].replacement->nr_pending); 2590 md_sync_acct(conf->mirrors[d].replacement->bdev, 2591 bio_sectors(wbio2)); 2592 submit_bio_noacct(wbio2); 2593 } 2594 } 2595 2596 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, 2597 int sectors, struct page *page, enum req_op op) 2598 { 2599 if (rdev_has_badblock(rdev, sector, sectors) && 2600 (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags))) 2601 return -1; 2602 if (sync_page_io(rdev, sector, sectors << 9, page, op, false)) 2603 /* success */ 2604 return 1; 2605 if (op == REQ_OP_WRITE) { 2606 set_bit(WriteErrorSeen, &rdev->flags); 2607 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 2608 set_bit(MD_RECOVERY_NEEDED, 2609 &rdev->mddev->recovery); 2610 } 2611 /* need to record an error - either for the block or the device */ 2612 if (!rdev_set_badblocks(rdev, sector, sectors, 0)) 2613 md_error(rdev->mddev, rdev); 2614 return 0; 2615 } 2616 2617 /* 2618 * This is a kernel thread which: 2619 * 2620 * 1. Retries failed read operations on working mirrors. 2621 * 2. Updates the raid superblock when problems encounter. 2622 * 3. Performs writes following reads for array synchronising. 2623 */ 2624 2625 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio) 2626 { 2627 int sect = 0; /* Offset from r10_bio->sector */ 2628 int sectors = r10_bio->sectors, slot = r10_bio->read_slot; 2629 struct md_rdev *rdev; 2630 int d = r10_bio->devs[slot].devnum; 2631 2632 /* still own a reference to this rdev, so it cannot 2633 * have been cleared recently. 2634 */ 2635 rdev = conf->mirrors[d].rdev; 2636 2637 if (test_bit(Faulty, &rdev->flags)) 2638 /* drive has already been failed, just ignore any 2639 more fix_read_error() attempts */ 2640 return; 2641 2642 if (exceed_read_errors(mddev, rdev)) { 2643 r10_bio->devs[slot].bio = IO_BLOCKED; 2644 return; 2645 } 2646 2647 while(sectors) { 2648 int s = sectors; 2649 int sl = slot; 2650 int success = 0; 2651 int start; 2652 2653 if (s > (PAGE_SIZE>>9)) 2654 s = PAGE_SIZE >> 9; 2655 2656 do { 2657 d = r10_bio->devs[sl].devnum; 2658 rdev = conf->mirrors[d].rdev; 2659 if (rdev && 2660 test_bit(In_sync, &rdev->flags) && 2661 !test_bit(Faulty, &rdev->flags) && 2662 rdev_has_badblock(rdev, 2663 r10_bio->devs[sl].addr + sect, 2664 s) == 0) { 2665 atomic_inc(&rdev->nr_pending); 2666 success = sync_page_io(rdev, 2667 r10_bio->devs[sl].addr + 2668 sect, 2669 s<<9, 2670 conf->tmppage, 2671 REQ_OP_READ, false); 2672 rdev_dec_pending(rdev, mddev); 2673 if (success) 2674 break; 2675 } 2676 sl++; 2677 if (sl == conf->copies) 2678 sl = 0; 2679 } while (sl != slot); 2680 2681 if (!success) { 2682 /* Cannot read from anywhere, just mark the block 2683 * as bad on the first device to discourage future 2684 * reads. 2685 */ 2686 int dn = r10_bio->devs[slot].devnum; 2687 rdev = conf->mirrors[dn].rdev; 2688 2689 if (!rdev_set_badblocks( 2690 rdev, 2691 r10_bio->devs[slot].addr 2692 + sect, 2693 s, 0)) { 2694 md_error(mddev, rdev); 2695 r10_bio->devs[slot].bio 2696 = IO_BLOCKED; 2697 } 2698 break; 2699 } 2700 2701 start = sl; 2702 /* write it back and re-read */ 2703 while (sl != slot) { 2704 if (sl==0) 2705 sl = conf->copies; 2706 sl--; 2707 d = r10_bio->devs[sl].devnum; 2708 rdev = conf->mirrors[d].rdev; 2709 if (!rdev || 2710 test_bit(Faulty, &rdev->flags) || 2711 !test_bit(In_sync, &rdev->flags)) 2712 continue; 2713 2714 atomic_inc(&rdev->nr_pending); 2715 if (r10_sync_page_io(rdev, 2716 r10_bio->devs[sl].addr + 2717 sect, 2718 s, conf->tmppage, REQ_OP_WRITE) 2719 == 0) { 2720 /* Well, this device is dead */ 2721 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n", 2722 mdname(mddev), s, 2723 (unsigned long long)( 2724 sect + 2725 choose_data_offset(r10_bio, 2726 rdev)), 2727 rdev->bdev); 2728 pr_notice("md/raid10:%s: %pg: failing drive\n", 2729 mdname(mddev), 2730 rdev->bdev); 2731 } 2732 rdev_dec_pending(rdev, mddev); 2733 } 2734 sl = start; 2735 while (sl != slot) { 2736 if (sl==0) 2737 sl = conf->copies; 2738 sl--; 2739 d = r10_bio->devs[sl].devnum; 2740 rdev = conf->mirrors[d].rdev; 2741 if (!rdev || 2742 test_bit(Faulty, &rdev->flags) || 2743 !test_bit(In_sync, &rdev->flags)) 2744 continue; 2745 2746 atomic_inc(&rdev->nr_pending); 2747 switch (r10_sync_page_io(rdev, 2748 r10_bio->devs[sl].addr + 2749 sect, 2750 s, conf->tmppage, REQ_OP_READ)) { 2751 case 0: 2752 /* Well, this device is dead */ 2753 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n", 2754 mdname(mddev), s, 2755 (unsigned long long)( 2756 sect + 2757 choose_data_offset(r10_bio, rdev)), 2758 rdev->bdev); 2759 pr_notice("md/raid10:%s: %pg: failing drive\n", 2760 mdname(mddev), 2761 rdev->bdev); 2762 break; 2763 case 1: 2764 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n", 2765 mdname(mddev), s, 2766 (unsigned long long)( 2767 sect + 2768 choose_data_offset(r10_bio, rdev)), 2769 rdev->bdev); 2770 atomic_add(s, &rdev->corrected_errors); 2771 } 2772 2773 rdev_dec_pending(rdev, mddev); 2774 } 2775 2776 sectors -= s; 2777 sect += s; 2778 } 2779 } 2780 2781 static bool narrow_write_error(struct r10bio *r10_bio, int i) 2782 { 2783 struct bio *bio = r10_bio->master_bio; 2784 struct mddev *mddev = r10_bio->mddev; 2785 struct r10conf *conf = mddev->private; 2786 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; 2787 /* bio has the data to be written to slot 'i' where 2788 * we just recently had a write error. 2789 * We repeatedly clone the bio and trim down to one block, 2790 * then try the write. Where the write fails we record 2791 * a bad block. 2792 * It is conceivable that the bio doesn't exactly align with 2793 * blocks. We must handle this. 2794 * 2795 * We currently own a reference to the rdev. 2796 */ 2797 2798 int block_sectors; 2799 sector_t sector; 2800 int sectors; 2801 int sect_to_write = r10_bio->sectors; 2802 bool ok = true; 2803 2804 if (rdev->badblocks.shift < 0) 2805 return false; 2806 2807 block_sectors = roundup(1 << rdev->badblocks.shift, 2808 bdev_logical_block_size(rdev->bdev) >> 9); 2809 sector = r10_bio->sector; 2810 sectors = ((r10_bio->sector + block_sectors) 2811 & ~(sector_t)(block_sectors - 1)) 2812 - sector; 2813 2814 while (sect_to_write) { 2815 struct bio *wbio; 2816 sector_t wsector; 2817 if (sectors > sect_to_write) 2818 sectors = sect_to_write; 2819 /* Write at 'sector' for 'sectors' */ 2820 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, 2821 &mddev->bio_set); 2822 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors); 2823 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector); 2824 wbio->bi_iter.bi_sector = wsector + 2825 choose_data_offset(r10_bio, rdev); 2826 wbio->bi_opf = REQ_OP_WRITE; 2827 2828 if (submit_bio_wait(wbio) < 0) 2829 /* Failure! */ 2830 ok = rdev_set_badblocks(rdev, wsector, 2831 sectors, 0) 2832 && ok; 2833 2834 bio_put(wbio); 2835 sect_to_write -= sectors; 2836 sector += sectors; 2837 sectors = block_sectors; 2838 } 2839 return ok; 2840 } 2841 2842 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) 2843 { 2844 int slot = r10_bio->read_slot; 2845 struct bio *bio; 2846 struct r10conf *conf = mddev->private; 2847 struct md_rdev *rdev = r10_bio->devs[slot].rdev; 2848 2849 /* we got a read error. Maybe the drive is bad. Maybe just 2850 * the block and we can fix it. 2851 * We freeze all other IO, and try reading the block from 2852 * other devices. When we find one, we re-write 2853 * and check it that fixes the read error. 2854 * This is all done synchronously while the array is 2855 * frozen. 2856 */ 2857 bio = r10_bio->devs[slot].bio; 2858 bio_put(bio); 2859 r10_bio->devs[slot].bio = NULL; 2860 2861 if (mddev->ro) 2862 r10_bio->devs[slot].bio = IO_BLOCKED; 2863 else if (!test_bit(FailFast, &rdev->flags)) { 2864 freeze_array(conf, 1); 2865 fix_read_error(conf, mddev, r10_bio); 2866 unfreeze_array(conf); 2867 } else 2868 md_error(mddev, rdev); 2869 2870 rdev_dec_pending(rdev, mddev); 2871 r10_bio->state = 0; 2872 raid10_read_request(mddev, r10_bio->master_bio, r10_bio, false); 2873 /* 2874 * allow_barrier after re-submit to ensure no sync io 2875 * can be issued while regular io pending. 2876 */ 2877 allow_barrier(conf); 2878 } 2879 2880 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) 2881 { 2882 /* Some sort of write request has finished and it 2883 * succeeded in writing where we thought there was a 2884 * bad block. So forget the bad block. 2885 * Or possibly if failed and we need to record 2886 * a bad block. 2887 */ 2888 int m; 2889 struct md_rdev *rdev; 2890 2891 if (test_bit(R10BIO_IsSync, &r10_bio->state) || 2892 test_bit(R10BIO_IsRecover, &r10_bio->state)) { 2893 for (m = 0; m < conf->copies; m++) { 2894 int dev = r10_bio->devs[m].devnum; 2895 rdev = conf->mirrors[dev].rdev; 2896 if (r10_bio->devs[m].bio == NULL || 2897 r10_bio->devs[m].bio->bi_end_io == NULL) 2898 continue; 2899 if (!r10_bio->devs[m].bio->bi_status) { 2900 rdev_clear_badblocks( 2901 rdev, 2902 r10_bio->devs[m].addr, 2903 r10_bio->sectors, 0); 2904 } else { 2905 if (!rdev_set_badblocks( 2906 rdev, 2907 r10_bio->devs[m].addr, 2908 r10_bio->sectors, 0)) 2909 md_error(conf->mddev, rdev); 2910 } 2911 rdev = conf->mirrors[dev].replacement; 2912 if (r10_bio->devs[m].repl_bio == NULL || 2913 r10_bio->devs[m].repl_bio->bi_end_io == NULL) 2914 continue; 2915 2916 if (!r10_bio->devs[m].repl_bio->bi_status) { 2917 rdev_clear_badblocks( 2918 rdev, 2919 r10_bio->devs[m].addr, 2920 r10_bio->sectors, 0); 2921 } else { 2922 if (!rdev_set_badblocks( 2923 rdev, 2924 r10_bio->devs[m].addr, 2925 r10_bio->sectors, 0)) 2926 md_error(conf->mddev, rdev); 2927 } 2928 } 2929 put_buf(r10_bio); 2930 } else { 2931 bool fail = false; 2932 for (m = 0; m < conf->copies; m++) { 2933 int dev = r10_bio->devs[m].devnum; 2934 struct bio *bio = r10_bio->devs[m].bio; 2935 rdev = conf->mirrors[dev].rdev; 2936 if (bio == IO_MADE_GOOD) { 2937 rdev_clear_badblocks( 2938 rdev, 2939 r10_bio->devs[m].addr, 2940 r10_bio->sectors, 0); 2941 rdev_dec_pending(rdev, conf->mddev); 2942 } else if (bio != NULL && bio->bi_status) { 2943 fail = true; 2944 if (!narrow_write_error(r10_bio, m)) 2945 md_error(conf->mddev, rdev); 2946 rdev_dec_pending(rdev, conf->mddev); 2947 } 2948 bio = r10_bio->devs[m].repl_bio; 2949 rdev = conf->mirrors[dev].replacement; 2950 if (rdev && bio == IO_MADE_GOOD) { 2951 rdev_clear_badblocks( 2952 rdev, 2953 r10_bio->devs[m].addr, 2954 r10_bio->sectors, 0); 2955 rdev_dec_pending(rdev, conf->mddev); 2956 } 2957 } 2958 if (fail) { 2959 spin_lock_irq(&conf->device_lock); 2960 list_add(&r10_bio->retry_list, &conf->bio_end_io_list); 2961 conf->nr_queued++; 2962 spin_unlock_irq(&conf->device_lock); 2963 /* 2964 * In case freeze_array() is waiting for condition 2965 * nr_pending == nr_queued + extra to be true. 2966 */ 2967 wake_up(&conf->wait_barrier); 2968 md_wakeup_thread(conf->mddev->thread); 2969 } else { 2970 if (test_bit(R10BIO_WriteError, 2971 &r10_bio->state)) 2972 close_write(r10_bio); 2973 raid_end_bio_io(r10_bio); 2974 } 2975 } 2976 } 2977 2978 static void raid10d(struct md_thread *thread) 2979 { 2980 struct mddev *mddev = thread->mddev; 2981 struct r10bio *r10_bio; 2982 unsigned long flags; 2983 struct r10conf *conf = mddev->private; 2984 struct list_head *head = &conf->retry_list; 2985 struct blk_plug plug; 2986 2987 md_check_recovery(mddev); 2988 2989 if (!list_empty_careful(&conf->bio_end_io_list) && 2990 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { 2991 LIST_HEAD(tmp); 2992 spin_lock_irqsave(&conf->device_lock, flags); 2993 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { 2994 while (!list_empty(&conf->bio_end_io_list)) { 2995 list_move(conf->bio_end_io_list.prev, &tmp); 2996 conf->nr_queued--; 2997 } 2998 } 2999 spin_unlock_irqrestore(&conf->device_lock, flags); 3000 while (!list_empty(&tmp)) { 3001 r10_bio = list_first_entry(&tmp, struct r10bio, 3002 retry_list); 3003 list_del(&r10_bio->retry_list); 3004 3005 if (test_bit(R10BIO_WriteError, 3006 &r10_bio->state)) 3007 close_write(r10_bio); 3008 raid_end_bio_io(r10_bio); 3009 } 3010 } 3011 3012 blk_start_plug(&plug); 3013 for (;;) { 3014 3015 flush_pending_writes(conf); 3016 3017 spin_lock_irqsave(&conf->device_lock, flags); 3018 if (list_empty(head)) { 3019 spin_unlock_irqrestore(&conf->device_lock, flags); 3020 break; 3021 } 3022 r10_bio = list_entry(head->prev, struct r10bio, retry_list); 3023 list_del(head->prev); 3024 conf->nr_queued--; 3025 spin_unlock_irqrestore(&conf->device_lock, flags); 3026 3027 mddev = r10_bio->mddev; 3028 conf = mddev->private; 3029 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 3030 test_bit(R10BIO_WriteError, &r10_bio->state)) 3031 handle_write_completed(conf, r10_bio); 3032 else if (test_bit(R10BIO_IsReshape, &r10_bio->state)) 3033 reshape_request_write(mddev, r10_bio); 3034 else if (test_bit(R10BIO_IsSync, &r10_bio->state)) 3035 sync_request_write(mddev, r10_bio); 3036 else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) 3037 recovery_request_write(mddev, r10_bio); 3038 else if (test_bit(R10BIO_ReadError, &r10_bio->state)) 3039 handle_read_error(mddev, r10_bio); 3040 else 3041 WARN_ON_ONCE(1); 3042 3043 cond_resched(); 3044 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING)) 3045 md_check_recovery(mddev); 3046 } 3047 blk_finish_plug(&plug); 3048 } 3049 3050 static int init_resync(struct r10conf *conf) 3051 { 3052 int ret, buffs, i; 3053 3054 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; 3055 BUG_ON(mempool_initialized(&conf->r10buf_pool)); 3056 conf->have_replacement = 0; 3057 for (i = 0; i < conf->geo.raid_disks; i++) 3058 if (conf->mirrors[i].replacement) 3059 conf->have_replacement = 1; 3060 ret = mempool_init(&conf->r10buf_pool, buffs, 3061 r10buf_pool_alloc, r10buf_pool_free, conf); 3062 if (ret) 3063 return ret; 3064 conf->next_resync = 0; 3065 return 0; 3066 } 3067 3068 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf) 3069 { 3070 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO); 3071 struct rsync_pages *rp; 3072 struct bio *bio; 3073 int nalloc; 3074 int i; 3075 3076 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || 3077 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) 3078 nalloc = conf->copies; /* resync */ 3079 else 3080 nalloc = 2; /* recovery */ 3081 3082 for (i = 0; i < nalloc; i++) { 3083 bio = r10bio->devs[i].bio; 3084 rp = bio->bi_private; 3085 bio_reset(bio, NULL, 0); 3086 bio->bi_private = rp; 3087 bio = r10bio->devs[i].repl_bio; 3088 if (bio) { 3089 rp = bio->bi_private; 3090 bio_reset(bio, NULL, 0); 3091 bio->bi_private = rp; 3092 } 3093 } 3094 return r10bio; 3095 } 3096 3097 /* 3098 * Set cluster_sync_high since we need other nodes to add the 3099 * range [cluster_sync_low, cluster_sync_high] to suspend list. 3100 */ 3101 static void raid10_set_cluster_sync_high(struct r10conf *conf) 3102 { 3103 sector_t window_size; 3104 int extra_chunk, chunks; 3105 3106 /* 3107 * First, here we define "stripe" as a unit which across 3108 * all member devices one time, so we get chunks by use 3109 * raid_disks / near_copies. Otherwise, if near_copies is 3110 * close to raid_disks, then resync window could increases 3111 * linearly with the increase of raid_disks, which means 3112 * we will suspend a really large IO window while it is not 3113 * necessary. If raid_disks is not divisible by near_copies, 3114 * an extra chunk is needed to ensure the whole "stripe" is 3115 * covered. 3116 */ 3117 3118 chunks = conf->geo.raid_disks / conf->geo.near_copies; 3119 if (conf->geo.raid_disks % conf->geo.near_copies == 0) 3120 extra_chunk = 0; 3121 else 3122 extra_chunk = 1; 3123 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors; 3124 3125 /* 3126 * At least use a 32M window to align with raid1's resync window 3127 */ 3128 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ? 3129 CLUSTER_RESYNC_WINDOW_SECTORS : window_size; 3130 3131 conf->cluster_sync_high = conf->cluster_sync_low + window_size; 3132 } 3133 3134 /* 3135 * perform a "sync" on one "block" 3136 * 3137 * We need to make sure that no normal I/O request - particularly write 3138 * requests - conflict with active sync requests. 3139 * 3140 * This is achieved by tracking pending requests and a 'barrier' concept 3141 * that can be installed to exclude normal IO requests. 3142 * 3143 * Resync and recovery are handled very differently. 3144 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. 3145 * 3146 * For resync, we iterate over virtual addresses, read all copies, 3147 * and update if there are differences. If only one copy is live, 3148 * skip it. 3149 * For recovery, we iterate over physical addresses, read a good 3150 * value for each non-in_sync drive, and over-write. 3151 * 3152 * So, for recovery we may have several outstanding complex requests for a 3153 * given address, one for each out-of-sync device. We model this by allocating 3154 * a number of r10_bio structures, one for each out-of-sync device. 3155 * As we setup these structures, we collect all bio's together into a list 3156 * which we then process collectively to add pages, and then process again 3157 * to pass to submit_bio_noacct. 3158 * 3159 * The r10_bio structures are linked using a borrowed master_bio pointer. 3160 * This link is counted in ->remaining. When the r10_bio that points to NULL 3161 * has its remaining count decremented to 0, the whole complex operation 3162 * is complete. 3163 * 3164 */ 3165 3166 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr, 3167 sector_t max_sector, int *skipped) 3168 { 3169 struct r10conf *conf = mddev->private; 3170 struct r10bio *r10_bio; 3171 struct bio *biolist = NULL, *bio; 3172 sector_t nr_sectors; 3173 int i; 3174 int max_sync; 3175 sector_t sync_blocks; 3176 sector_t sectors_skipped = 0; 3177 int chunks_skipped = 0; 3178 sector_t chunk_mask = conf->geo.chunk_mask; 3179 int page_idx = 0; 3180 int error_disk = -1; 3181 3182 /* 3183 * Allow skipping a full rebuild for incremental assembly 3184 * of a clean array, like RAID1 does. 3185 */ 3186 if (mddev->bitmap == NULL && 3187 mddev->recovery_cp == MaxSector && 3188 mddev->reshape_position == MaxSector && 3189 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && 3190 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 3191 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && 3192 conf->fullsync == 0) { 3193 *skipped = 1; 3194 return mddev->dev_sectors - sector_nr; 3195 } 3196 3197 if (!mempool_initialized(&conf->r10buf_pool)) 3198 if (init_resync(conf)) 3199 return 0; 3200 3201 skipped: 3202 if (sector_nr >= max_sector) { 3203 conf->cluster_sync_low = 0; 3204 conf->cluster_sync_high = 0; 3205 3206 /* If we aborted, we need to abort the 3207 * sync on the 'current' bitmap chucks (there can 3208 * be several when recovering multiple devices). 3209 * as we may have started syncing it but not finished. 3210 * We can find the current address in 3211 * mddev->curr_resync, but for recovery, 3212 * we need to convert that to several 3213 * virtual addresses. 3214 */ 3215 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { 3216 end_reshape(conf); 3217 close_sync(conf); 3218 return 0; 3219 } 3220 3221 if (mddev->curr_resync < max_sector) { /* aborted */ 3222 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 3223 mddev->bitmap_ops->end_sync(mddev, 3224 mddev->curr_resync, 3225 &sync_blocks); 3226 else for (i = 0; i < conf->geo.raid_disks; i++) { 3227 sector_t sect = 3228 raid10_find_virt(conf, mddev->curr_resync, i); 3229 3230 mddev->bitmap_ops->end_sync(mddev, sect, 3231 &sync_blocks); 3232 } 3233 } else { 3234 /* completed sync */ 3235 if ((!mddev->bitmap || conf->fullsync) 3236 && conf->have_replacement 3237 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3238 /* Completed a full sync so the replacements 3239 * are now fully recovered. 3240 */ 3241 for (i = 0; i < conf->geo.raid_disks; i++) { 3242 struct md_rdev *rdev = 3243 conf->mirrors[i].replacement; 3244 3245 if (rdev) 3246 rdev->recovery_offset = MaxSector; 3247 } 3248 } 3249 conf->fullsync = 0; 3250 } 3251 mddev->bitmap_ops->close_sync(mddev); 3252 close_sync(conf); 3253 *skipped = 1; 3254 return sectors_skipped; 3255 } 3256 3257 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 3258 return reshape_request(mddev, sector_nr, skipped); 3259 3260 if (chunks_skipped >= conf->geo.raid_disks) { 3261 pr_err("md/raid10:%s: %s fails\n", mdname(mddev), 3262 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? "resync" : "recovery"); 3263 if (error_disk >= 0 && 3264 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3265 /* 3266 * recovery fails, set mirrors.recovery_disabled, 3267 * device shouldn't be added to there. 3268 */ 3269 conf->mirrors[error_disk].recovery_disabled = 3270 mddev->recovery_disabled; 3271 return 0; 3272 } 3273 /* 3274 * if there has been nothing to do on any drive, 3275 * then there is nothing to do at all. 3276 */ 3277 *skipped = 1; 3278 return (max_sector - sector_nr) + sectors_skipped; 3279 } 3280 3281 if (max_sector > mddev->resync_max) 3282 max_sector = mddev->resync_max; /* Don't do IO beyond here */ 3283 3284 /* make sure whole request will fit in a chunk - if chunks 3285 * are meaningful 3286 */ 3287 if (conf->geo.near_copies < conf->geo.raid_disks && 3288 max_sector > (sector_nr | chunk_mask)) 3289 max_sector = (sector_nr | chunk_mask) + 1; 3290 3291 /* 3292 * If there is non-resync activity waiting for a turn, then let it 3293 * though before starting on this new sync request. 3294 */ 3295 if (conf->nr_waiting) 3296 schedule_timeout_uninterruptible(1); 3297 3298 /* Again, very different code for resync and recovery. 3299 * Both must result in an r10bio with a list of bios that 3300 * have bi_end_io, bi_sector, bi_bdev set, 3301 * and bi_private set to the r10bio. 3302 * For recovery, we may actually create several r10bios 3303 * with 2 bios in each, that correspond to the bios in the main one. 3304 * In this case, the subordinate r10bios link back through a 3305 * borrowed master_bio pointer, and the counter in the master 3306 * includes a ref from each subordinate. 3307 */ 3308 /* First, we decide what to do and set ->bi_end_io 3309 * To end_sync_read if we want to read, and 3310 * end_sync_write if we will want to write. 3311 */ 3312 3313 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); 3314 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3315 /* recovery... the complicated one */ 3316 int j; 3317 r10_bio = NULL; 3318 3319 for (i = 0 ; i < conf->geo.raid_disks; i++) { 3320 bool still_degraded; 3321 struct r10bio *rb2; 3322 sector_t sect; 3323 bool must_sync; 3324 int any_working; 3325 struct raid10_info *mirror = &conf->mirrors[i]; 3326 struct md_rdev *mrdev, *mreplace; 3327 3328 mrdev = mirror->rdev; 3329 mreplace = mirror->replacement; 3330 3331 if (mrdev && (test_bit(Faulty, &mrdev->flags) || 3332 test_bit(In_sync, &mrdev->flags))) 3333 mrdev = NULL; 3334 if (mreplace && test_bit(Faulty, &mreplace->flags)) 3335 mreplace = NULL; 3336 3337 if (!mrdev && !mreplace) 3338 continue; 3339 3340 still_degraded = false; 3341 /* want to reconstruct this device */ 3342 rb2 = r10_bio; 3343 sect = raid10_find_virt(conf, sector_nr, i); 3344 if (sect >= mddev->resync_max_sectors) 3345 /* last stripe is not complete - don't 3346 * try to recover this sector. 3347 */ 3348 continue; 3349 /* Unless we are doing a full sync, or a replacement 3350 * we only need to recover the block if it is set in 3351 * the bitmap 3352 */ 3353 must_sync = mddev->bitmap_ops->start_sync(mddev, sect, 3354 &sync_blocks, 3355 true); 3356 if (sync_blocks < max_sync) 3357 max_sync = sync_blocks; 3358 if (!must_sync && 3359 mreplace == NULL && 3360 !conf->fullsync) { 3361 /* yep, skip the sync_blocks here, but don't assume 3362 * that there will never be anything to do here 3363 */ 3364 chunks_skipped = -1; 3365 continue; 3366 } 3367 if (mrdev) 3368 atomic_inc(&mrdev->nr_pending); 3369 if (mreplace) 3370 atomic_inc(&mreplace->nr_pending); 3371 3372 r10_bio = raid10_alloc_init_r10buf(conf); 3373 r10_bio->state = 0; 3374 raise_barrier(conf, rb2 != NULL); 3375 atomic_set(&r10_bio->remaining, 0); 3376 3377 r10_bio->master_bio = (struct bio*)rb2; 3378 if (rb2) 3379 atomic_inc(&rb2->remaining); 3380 r10_bio->mddev = mddev; 3381 set_bit(R10BIO_IsRecover, &r10_bio->state); 3382 r10_bio->sector = sect; 3383 3384 raid10_find_phys(conf, r10_bio); 3385 3386 /* Need to check if the array will still be 3387 * degraded 3388 */ 3389 for (j = 0; j < conf->geo.raid_disks; j++) { 3390 struct md_rdev *rdev = conf->mirrors[j].rdev; 3391 3392 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { 3393 still_degraded = false; 3394 break; 3395 } 3396 } 3397 3398 must_sync = mddev->bitmap_ops->start_sync(mddev, sect, 3399 &sync_blocks, still_degraded); 3400 3401 any_working = 0; 3402 for (j=0; j<conf->copies;j++) { 3403 int k; 3404 int d = r10_bio->devs[j].devnum; 3405 sector_t from_addr, to_addr; 3406 struct md_rdev *rdev = conf->mirrors[d].rdev; 3407 sector_t sector, first_bad; 3408 sector_t bad_sectors; 3409 if (!rdev || 3410 !test_bit(In_sync, &rdev->flags)) 3411 continue; 3412 /* This is where we read from */ 3413 any_working = 1; 3414 sector = r10_bio->devs[j].addr; 3415 3416 if (is_badblock(rdev, sector, max_sync, 3417 &first_bad, &bad_sectors)) { 3418 if (first_bad > sector) 3419 max_sync = first_bad - sector; 3420 else { 3421 bad_sectors -= (sector 3422 - first_bad); 3423 if (max_sync > bad_sectors) 3424 max_sync = bad_sectors; 3425 continue; 3426 } 3427 } 3428 bio = r10_bio->devs[0].bio; 3429 bio->bi_next = biolist; 3430 biolist = bio; 3431 bio->bi_end_io = end_sync_read; 3432 bio->bi_opf = REQ_OP_READ; 3433 if (test_bit(FailFast, &rdev->flags)) 3434 bio->bi_opf |= MD_FAILFAST; 3435 from_addr = r10_bio->devs[j].addr; 3436 bio->bi_iter.bi_sector = from_addr + 3437 rdev->data_offset; 3438 bio_set_dev(bio, rdev->bdev); 3439 atomic_inc(&rdev->nr_pending); 3440 /* and we write to 'i' (if not in_sync) */ 3441 3442 for (k=0; k<conf->copies; k++) 3443 if (r10_bio->devs[k].devnum == i) 3444 break; 3445 BUG_ON(k == conf->copies); 3446 to_addr = r10_bio->devs[k].addr; 3447 r10_bio->devs[0].devnum = d; 3448 r10_bio->devs[0].addr = from_addr; 3449 r10_bio->devs[1].devnum = i; 3450 r10_bio->devs[1].addr = to_addr; 3451 3452 if (mrdev) { 3453 bio = r10_bio->devs[1].bio; 3454 bio->bi_next = biolist; 3455 biolist = bio; 3456 bio->bi_end_io = end_sync_write; 3457 bio->bi_opf = REQ_OP_WRITE; 3458 bio->bi_iter.bi_sector = to_addr 3459 + mrdev->data_offset; 3460 bio_set_dev(bio, mrdev->bdev); 3461 atomic_inc(&r10_bio->remaining); 3462 } else 3463 r10_bio->devs[1].bio->bi_end_io = NULL; 3464 3465 /* and maybe write to replacement */ 3466 bio = r10_bio->devs[1].repl_bio; 3467 if (bio) 3468 bio->bi_end_io = NULL; 3469 /* Note: if replace is not NULL, then bio 3470 * cannot be NULL as r10buf_pool_alloc will 3471 * have allocated it. 3472 */ 3473 if (!mreplace) 3474 break; 3475 bio->bi_next = biolist; 3476 biolist = bio; 3477 bio->bi_end_io = end_sync_write; 3478 bio->bi_opf = REQ_OP_WRITE; 3479 bio->bi_iter.bi_sector = to_addr + 3480 mreplace->data_offset; 3481 bio_set_dev(bio, mreplace->bdev); 3482 atomic_inc(&r10_bio->remaining); 3483 break; 3484 } 3485 if (j == conf->copies) { 3486 /* Cannot recover, so abort the recovery or 3487 * record a bad block */ 3488 if (any_working) { 3489 /* problem is that there are bad blocks 3490 * on other device(s) 3491 */ 3492 int k; 3493 for (k = 0; k < conf->copies; k++) 3494 if (r10_bio->devs[k].devnum == i) 3495 break; 3496 if (mrdev && !test_bit(In_sync, 3497 &mrdev->flags) 3498 && !rdev_set_badblocks( 3499 mrdev, 3500 r10_bio->devs[k].addr, 3501 max_sync, 0)) 3502 any_working = 0; 3503 if (mreplace && 3504 !rdev_set_badblocks( 3505 mreplace, 3506 r10_bio->devs[k].addr, 3507 max_sync, 0)) 3508 any_working = 0; 3509 } 3510 if (!any_working) { 3511 if (!test_and_set_bit(MD_RECOVERY_INTR, 3512 &mddev->recovery)) 3513 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n", 3514 mdname(mddev)); 3515 mirror->recovery_disabled 3516 = mddev->recovery_disabled; 3517 } else { 3518 error_disk = i; 3519 } 3520 put_buf(r10_bio); 3521 if (rb2) 3522 atomic_dec(&rb2->remaining); 3523 r10_bio = rb2; 3524 if (mrdev) 3525 rdev_dec_pending(mrdev, mddev); 3526 if (mreplace) 3527 rdev_dec_pending(mreplace, mddev); 3528 break; 3529 } 3530 if (mrdev) 3531 rdev_dec_pending(mrdev, mddev); 3532 if (mreplace) 3533 rdev_dec_pending(mreplace, mddev); 3534 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) { 3535 /* Only want this if there is elsewhere to 3536 * read from. 'j' is currently the first 3537 * readable copy. 3538 */ 3539 int targets = 1; 3540 for (; j < conf->copies; j++) { 3541 int d = r10_bio->devs[j].devnum; 3542 if (conf->mirrors[d].rdev && 3543 test_bit(In_sync, 3544 &conf->mirrors[d].rdev->flags)) 3545 targets++; 3546 } 3547 if (targets == 1) 3548 r10_bio->devs[0].bio->bi_opf 3549 &= ~MD_FAILFAST; 3550 } 3551 } 3552 if (biolist == NULL) { 3553 while (r10_bio) { 3554 struct r10bio *rb2 = r10_bio; 3555 r10_bio = (struct r10bio*) rb2->master_bio; 3556 rb2->master_bio = NULL; 3557 put_buf(rb2); 3558 } 3559 goto giveup; 3560 } 3561 } else { 3562 /* resync. Schedule a read for every block at this virt offset */ 3563 int count = 0; 3564 3565 /* 3566 * Since curr_resync_completed could probably not update in 3567 * time, and we will set cluster_sync_low based on it. 3568 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for 3569 * safety reason, which ensures curr_resync_completed is 3570 * updated in bitmap_cond_end_sync. 3571 */ 3572 mddev->bitmap_ops->cond_end_sync(mddev, sector_nr, 3573 mddev_is_clustered(mddev) && 3574 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high)); 3575 3576 if (!mddev->bitmap_ops->start_sync(mddev, sector_nr, 3577 &sync_blocks, 3578 mddev->degraded) && 3579 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, 3580 &mddev->recovery)) { 3581 /* We can skip this block */ 3582 *skipped = 1; 3583 return sync_blocks + sectors_skipped; 3584 } 3585 if (sync_blocks < max_sync) 3586 max_sync = sync_blocks; 3587 r10_bio = raid10_alloc_init_r10buf(conf); 3588 r10_bio->state = 0; 3589 3590 r10_bio->mddev = mddev; 3591 atomic_set(&r10_bio->remaining, 0); 3592 raise_barrier(conf, 0); 3593 conf->next_resync = sector_nr; 3594 3595 r10_bio->master_bio = NULL; 3596 r10_bio->sector = sector_nr; 3597 set_bit(R10BIO_IsSync, &r10_bio->state); 3598 raid10_find_phys(conf, r10_bio); 3599 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1; 3600 3601 for (i = 0; i < conf->copies; i++) { 3602 int d = r10_bio->devs[i].devnum; 3603 sector_t first_bad, sector; 3604 sector_t bad_sectors; 3605 struct md_rdev *rdev; 3606 3607 if (r10_bio->devs[i].repl_bio) 3608 r10_bio->devs[i].repl_bio->bi_end_io = NULL; 3609 3610 bio = r10_bio->devs[i].bio; 3611 bio->bi_status = BLK_STS_IOERR; 3612 rdev = conf->mirrors[d].rdev; 3613 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) 3614 continue; 3615 3616 sector = r10_bio->devs[i].addr; 3617 if (is_badblock(rdev, sector, max_sync, 3618 &first_bad, &bad_sectors)) { 3619 if (first_bad > sector) 3620 max_sync = first_bad - sector; 3621 else { 3622 bad_sectors -= (sector - first_bad); 3623 if (max_sync > bad_sectors) 3624 max_sync = bad_sectors; 3625 continue; 3626 } 3627 } 3628 atomic_inc(&rdev->nr_pending); 3629 atomic_inc(&r10_bio->remaining); 3630 bio->bi_next = biolist; 3631 biolist = bio; 3632 bio->bi_end_io = end_sync_read; 3633 bio->bi_opf = REQ_OP_READ; 3634 if (test_bit(FailFast, &rdev->flags)) 3635 bio->bi_opf |= MD_FAILFAST; 3636 bio->bi_iter.bi_sector = sector + rdev->data_offset; 3637 bio_set_dev(bio, rdev->bdev); 3638 count++; 3639 3640 rdev = conf->mirrors[d].replacement; 3641 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) 3642 continue; 3643 3644 atomic_inc(&rdev->nr_pending); 3645 3646 /* Need to set up for writing to the replacement */ 3647 bio = r10_bio->devs[i].repl_bio; 3648 bio->bi_status = BLK_STS_IOERR; 3649 3650 sector = r10_bio->devs[i].addr; 3651 bio->bi_next = biolist; 3652 biolist = bio; 3653 bio->bi_end_io = end_sync_write; 3654 bio->bi_opf = REQ_OP_WRITE; 3655 if (test_bit(FailFast, &rdev->flags)) 3656 bio->bi_opf |= MD_FAILFAST; 3657 bio->bi_iter.bi_sector = sector + rdev->data_offset; 3658 bio_set_dev(bio, rdev->bdev); 3659 count++; 3660 } 3661 3662 if (count < 2) { 3663 for (i=0; i<conf->copies; i++) { 3664 int d = r10_bio->devs[i].devnum; 3665 if (r10_bio->devs[i].bio->bi_end_io) 3666 rdev_dec_pending(conf->mirrors[d].rdev, 3667 mddev); 3668 if (r10_bio->devs[i].repl_bio && 3669 r10_bio->devs[i].repl_bio->bi_end_io) 3670 rdev_dec_pending( 3671 conf->mirrors[d].replacement, 3672 mddev); 3673 } 3674 put_buf(r10_bio); 3675 biolist = NULL; 3676 goto giveup; 3677 } 3678 } 3679 3680 nr_sectors = 0; 3681 if (sector_nr + max_sync < max_sector) 3682 max_sector = sector_nr + max_sync; 3683 do { 3684 struct page *page; 3685 int len = PAGE_SIZE; 3686 if (sector_nr + (len>>9) > max_sector) 3687 len = (max_sector - sector_nr) << 9; 3688 if (len == 0) 3689 break; 3690 for (bio= biolist ; bio ; bio=bio->bi_next) { 3691 struct resync_pages *rp = get_resync_pages(bio); 3692 page = resync_fetch_page(rp, page_idx); 3693 if (WARN_ON(!bio_add_page(bio, page, len, 0))) { 3694 bio->bi_status = BLK_STS_RESOURCE; 3695 bio_endio(bio); 3696 goto giveup; 3697 } 3698 } 3699 nr_sectors += len>>9; 3700 sector_nr += len>>9; 3701 } while (++page_idx < RESYNC_PAGES); 3702 r10_bio->sectors = nr_sectors; 3703 3704 if (mddev_is_clustered(mddev) && 3705 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3706 /* It is resync not recovery */ 3707 if (conf->cluster_sync_high < sector_nr + nr_sectors) { 3708 conf->cluster_sync_low = mddev->curr_resync_completed; 3709 raid10_set_cluster_sync_high(conf); 3710 /* Send resync message */ 3711 mddev->cluster_ops->resync_info_update(mddev, 3712 conf->cluster_sync_low, 3713 conf->cluster_sync_high); 3714 } 3715 } else if (mddev_is_clustered(mddev)) { 3716 /* This is recovery not resync */ 3717 sector_t sect_va1, sect_va2; 3718 bool broadcast_msg = false; 3719 3720 for (i = 0; i < conf->geo.raid_disks; i++) { 3721 /* 3722 * sector_nr is a device address for recovery, so we 3723 * need translate it to array address before compare 3724 * with cluster_sync_high. 3725 */ 3726 sect_va1 = raid10_find_virt(conf, sector_nr, i); 3727 3728 if (conf->cluster_sync_high < sect_va1 + nr_sectors) { 3729 broadcast_msg = true; 3730 /* 3731 * curr_resync_completed is similar as 3732 * sector_nr, so make the translation too. 3733 */ 3734 sect_va2 = raid10_find_virt(conf, 3735 mddev->curr_resync_completed, i); 3736 3737 if (conf->cluster_sync_low == 0 || 3738 conf->cluster_sync_low > sect_va2) 3739 conf->cluster_sync_low = sect_va2; 3740 } 3741 } 3742 if (broadcast_msg) { 3743 raid10_set_cluster_sync_high(conf); 3744 mddev->cluster_ops->resync_info_update(mddev, 3745 conf->cluster_sync_low, 3746 conf->cluster_sync_high); 3747 } 3748 } 3749 3750 while (biolist) { 3751 bio = biolist; 3752 biolist = biolist->bi_next; 3753 3754 bio->bi_next = NULL; 3755 r10_bio = get_resync_r10bio(bio); 3756 r10_bio->sectors = nr_sectors; 3757 3758 if (bio->bi_end_io == end_sync_read) { 3759 md_sync_acct_bio(bio, nr_sectors); 3760 bio->bi_status = 0; 3761 submit_bio_noacct(bio); 3762 } 3763 } 3764 3765 if (sectors_skipped) 3766 /* pretend they weren't skipped, it makes 3767 * no important difference in this case 3768 */ 3769 md_done_sync(mddev, sectors_skipped, 1); 3770 3771 return sectors_skipped + nr_sectors; 3772 giveup: 3773 /* There is nowhere to write, so all non-sync 3774 * drives must be failed or in resync, all drives 3775 * have a bad block, so try the next chunk... 3776 */ 3777 if (sector_nr + max_sync < max_sector) 3778 max_sector = sector_nr + max_sync; 3779 3780 sectors_skipped += (max_sector - sector_nr); 3781 chunks_skipped ++; 3782 sector_nr = max_sector; 3783 goto skipped; 3784 } 3785 3786 static sector_t 3787 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) 3788 { 3789 sector_t size; 3790 struct r10conf *conf = mddev->private; 3791 3792 if (!raid_disks) 3793 raid_disks = min(conf->geo.raid_disks, 3794 conf->prev.raid_disks); 3795 if (!sectors) 3796 sectors = conf->dev_sectors; 3797 3798 size = sectors >> conf->geo.chunk_shift; 3799 sector_div(size, conf->geo.far_copies); 3800 size = size * raid_disks; 3801 sector_div(size, conf->geo.near_copies); 3802 3803 return size << conf->geo.chunk_shift; 3804 } 3805 3806 static void calc_sectors(struct r10conf *conf, sector_t size) 3807 { 3808 /* Calculate the number of sectors-per-device that will 3809 * actually be used, and set conf->dev_sectors and 3810 * conf->stride 3811 */ 3812 3813 size = size >> conf->geo.chunk_shift; 3814 sector_div(size, conf->geo.far_copies); 3815 size = size * conf->geo.raid_disks; 3816 sector_div(size, conf->geo.near_copies); 3817 /* 'size' is now the number of chunks in the array */ 3818 /* calculate "used chunks per device" */ 3819 size = size * conf->copies; 3820 3821 /* We need to round up when dividing by raid_disks to 3822 * get the stride size. 3823 */ 3824 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks); 3825 3826 conf->dev_sectors = size << conf->geo.chunk_shift; 3827 3828 if (conf->geo.far_offset) 3829 conf->geo.stride = 1 << conf->geo.chunk_shift; 3830 else { 3831 sector_div(size, conf->geo.far_copies); 3832 conf->geo.stride = size << conf->geo.chunk_shift; 3833 } 3834 } 3835 3836 enum geo_type {geo_new, geo_old, geo_start}; 3837 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new) 3838 { 3839 int nc, fc, fo; 3840 int layout, chunk, disks; 3841 switch (new) { 3842 case geo_old: 3843 layout = mddev->layout; 3844 chunk = mddev->chunk_sectors; 3845 disks = mddev->raid_disks - mddev->delta_disks; 3846 break; 3847 case geo_new: 3848 layout = mddev->new_layout; 3849 chunk = mddev->new_chunk_sectors; 3850 disks = mddev->raid_disks; 3851 break; 3852 default: /* avoid 'may be unused' warnings */ 3853 case geo_start: /* new when starting reshape - raid_disks not 3854 * updated yet. */ 3855 layout = mddev->new_layout; 3856 chunk = mddev->new_chunk_sectors; 3857 disks = mddev->raid_disks + mddev->delta_disks; 3858 break; 3859 } 3860 if (layout >> 19) 3861 return -1; 3862 if (chunk < (PAGE_SIZE >> 9) || 3863 !is_power_of_2(chunk)) 3864 return -2; 3865 nc = layout & 255; 3866 fc = (layout >> 8) & 255; 3867 fo = layout & (1<<16); 3868 geo->raid_disks = disks; 3869 geo->near_copies = nc; 3870 geo->far_copies = fc; 3871 geo->far_offset = fo; 3872 switch (layout >> 17) { 3873 case 0: /* original layout. simple but not always optimal */ 3874 geo->far_set_size = disks; 3875 break; 3876 case 1: /* "improved" layout which was buggy. Hopefully no-one is 3877 * actually using this, but leave code here just in case.*/ 3878 geo->far_set_size = disks/fc; 3879 WARN(geo->far_set_size < fc, 3880 "This RAID10 layout does not provide data safety - please backup and create new array\n"); 3881 break; 3882 case 2: /* "improved" layout fixed to match documentation */ 3883 geo->far_set_size = fc * nc; 3884 break; 3885 default: /* Not a valid layout */ 3886 return -1; 3887 } 3888 geo->chunk_mask = chunk - 1; 3889 geo->chunk_shift = ffz(~chunk); 3890 return nc*fc; 3891 } 3892 3893 static void raid10_free_conf(struct r10conf *conf) 3894 { 3895 if (!conf) 3896 return; 3897 3898 mempool_exit(&conf->r10bio_pool); 3899 kfree(conf->mirrors); 3900 kfree(conf->mirrors_old); 3901 kfree(conf->mirrors_new); 3902 safe_put_page(conf->tmppage); 3903 bioset_exit(&conf->bio_split); 3904 kfree(conf); 3905 } 3906 3907 static struct r10conf *setup_conf(struct mddev *mddev) 3908 { 3909 struct r10conf *conf = NULL; 3910 int err = -EINVAL; 3911 struct geom geo; 3912 int copies; 3913 3914 copies = setup_geo(&geo, mddev, geo_new); 3915 3916 if (copies == -2) { 3917 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n", 3918 mdname(mddev), PAGE_SIZE); 3919 goto out; 3920 } 3921 3922 if (copies < 2 || copies > mddev->raid_disks) { 3923 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n", 3924 mdname(mddev), mddev->new_layout); 3925 goto out; 3926 } 3927 3928 err = -ENOMEM; 3929 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); 3930 if (!conf) 3931 goto out; 3932 3933 /* FIXME calc properly */ 3934 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks), 3935 sizeof(struct raid10_info), 3936 GFP_KERNEL); 3937 if (!conf->mirrors) 3938 goto out; 3939 3940 conf->tmppage = alloc_page(GFP_KERNEL); 3941 if (!conf->tmppage) 3942 goto out; 3943 3944 conf->geo = geo; 3945 conf->copies = copies; 3946 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc, 3947 rbio_pool_free, conf); 3948 if (err) 3949 goto out; 3950 3951 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); 3952 if (err) 3953 goto out; 3954 3955 calc_sectors(conf, mddev->dev_sectors); 3956 if (mddev->reshape_position == MaxSector) { 3957 conf->prev = conf->geo; 3958 conf->reshape_progress = MaxSector; 3959 } else { 3960 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) { 3961 err = -EINVAL; 3962 goto out; 3963 } 3964 conf->reshape_progress = mddev->reshape_position; 3965 if (conf->prev.far_offset) 3966 conf->prev.stride = 1 << conf->prev.chunk_shift; 3967 else 3968 /* far_copies must be 1 */ 3969 conf->prev.stride = conf->dev_sectors; 3970 } 3971 conf->reshape_safe = conf->reshape_progress; 3972 spin_lock_init(&conf->device_lock); 3973 INIT_LIST_HEAD(&conf->retry_list); 3974 INIT_LIST_HEAD(&conf->bio_end_io_list); 3975 3976 seqlock_init(&conf->resync_lock); 3977 init_waitqueue_head(&conf->wait_barrier); 3978 atomic_set(&conf->nr_pending, 0); 3979 3980 err = -ENOMEM; 3981 rcu_assign_pointer(conf->thread, 3982 md_register_thread(raid10d, mddev, "raid10")); 3983 if (!conf->thread) 3984 goto out; 3985 3986 conf->mddev = mddev; 3987 return conf; 3988 3989 out: 3990 raid10_free_conf(conf); 3991 return ERR_PTR(err); 3992 } 3993 3994 static unsigned int raid10_nr_stripes(struct r10conf *conf) 3995 { 3996 unsigned int raid_disks = conf->geo.raid_disks; 3997 3998 if (conf->geo.raid_disks % conf->geo.near_copies) 3999 return raid_disks; 4000 return raid_disks / conf->geo.near_copies; 4001 } 4002 4003 static int raid10_set_queue_limits(struct mddev *mddev) 4004 { 4005 struct r10conf *conf = mddev->private; 4006 struct queue_limits lim; 4007 int err; 4008 4009 md_init_stacking_limits(&lim); 4010 lim.max_write_zeroes_sectors = 0; 4011 lim.io_min = mddev->chunk_sectors << 9; 4012 lim.io_opt = lim.io_min * raid10_nr_stripes(conf); 4013 lim.features |= BLK_FEAT_ATOMIC_WRITES; 4014 err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY); 4015 if (err) 4016 return err; 4017 return queue_limits_set(mddev->gendisk->queue, &lim); 4018 } 4019 4020 static int raid10_run(struct mddev *mddev) 4021 { 4022 struct r10conf *conf; 4023 int i, disk_idx; 4024 struct raid10_info *disk; 4025 struct md_rdev *rdev; 4026 sector_t size; 4027 sector_t min_offset_diff = 0; 4028 int first = 1; 4029 int ret = -EIO; 4030 4031 if (mddev->private == NULL) { 4032 conf = setup_conf(mddev); 4033 if (IS_ERR(conf)) 4034 return PTR_ERR(conf); 4035 mddev->private = conf; 4036 } 4037 conf = mddev->private; 4038 if (!conf) 4039 goto out; 4040 4041 rcu_assign_pointer(mddev->thread, conf->thread); 4042 rcu_assign_pointer(conf->thread, NULL); 4043 4044 if (mddev_is_clustered(conf->mddev)) { 4045 int fc, fo; 4046 4047 fc = (mddev->layout >> 8) & 255; 4048 fo = mddev->layout & (1<<16); 4049 if (fc > 1 || fo > 0) { 4050 pr_err("only near layout is supported by clustered" 4051 " raid10\n"); 4052 goto out_free_conf; 4053 } 4054 } 4055 4056 rdev_for_each(rdev, mddev) { 4057 long long diff; 4058 4059 disk_idx = rdev->raid_disk; 4060 if (disk_idx < 0) 4061 continue; 4062 if (disk_idx >= conf->geo.raid_disks && 4063 disk_idx >= conf->prev.raid_disks) 4064 continue; 4065 disk = conf->mirrors + disk_idx; 4066 4067 if (test_bit(Replacement, &rdev->flags)) { 4068 if (disk->replacement) 4069 goto out_free_conf; 4070 disk->replacement = rdev; 4071 } else { 4072 if (disk->rdev) 4073 goto out_free_conf; 4074 disk->rdev = rdev; 4075 } 4076 diff = (rdev->new_data_offset - rdev->data_offset); 4077 if (!mddev->reshape_backwards) 4078 diff = -diff; 4079 if (diff < 0) 4080 diff = 0; 4081 if (first || diff < min_offset_diff) 4082 min_offset_diff = diff; 4083 4084 disk->head_position = 0; 4085 first = 0; 4086 } 4087 4088 if (!mddev_is_dm(conf->mddev)) { 4089 int err = raid10_set_queue_limits(mddev); 4090 4091 if (err) { 4092 ret = err; 4093 goto out_free_conf; 4094 } 4095 } 4096 4097 /* need to check that every block has at least one working mirror */ 4098 if (!enough(conf, -1)) { 4099 pr_err("md/raid10:%s: not enough operational mirrors.\n", 4100 mdname(mddev)); 4101 goto out_free_conf; 4102 } 4103 4104 if (conf->reshape_progress != MaxSector) { 4105 /* must ensure that shape change is supported */ 4106 if (conf->geo.far_copies != 1 && 4107 conf->geo.far_offset == 0) 4108 goto out_free_conf; 4109 if (conf->prev.far_copies != 1 && 4110 conf->prev.far_offset == 0) 4111 goto out_free_conf; 4112 } 4113 4114 mddev->degraded = 0; 4115 for (i = 0; 4116 i < conf->geo.raid_disks 4117 || i < conf->prev.raid_disks; 4118 i++) { 4119 4120 disk = conf->mirrors + i; 4121 4122 if (!disk->rdev && disk->replacement) { 4123 /* The replacement is all we have - use it */ 4124 disk->rdev = disk->replacement; 4125 disk->replacement = NULL; 4126 clear_bit(Replacement, &disk->rdev->flags); 4127 } 4128 4129 if (!disk->rdev || 4130 !test_bit(In_sync, &disk->rdev->flags)) { 4131 disk->head_position = 0; 4132 mddev->degraded++; 4133 if (disk->rdev && 4134 disk->rdev->saved_raid_disk < 0) 4135 conf->fullsync = 1; 4136 } 4137 4138 if (disk->replacement && 4139 !test_bit(In_sync, &disk->replacement->flags) && 4140 disk->replacement->saved_raid_disk < 0) { 4141 conf->fullsync = 1; 4142 } 4143 4144 disk->recovery_disabled = mddev->recovery_disabled - 1; 4145 } 4146 4147 if (mddev->recovery_cp != MaxSector) 4148 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n", 4149 mdname(mddev)); 4150 pr_info("md/raid10:%s: active with %d out of %d devices\n", 4151 mdname(mddev), conf->geo.raid_disks - mddev->degraded, 4152 conf->geo.raid_disks); 4153 /* 4154 * Ok, everything is just fine now 4155 */ 4156 mddev->dev_sectors = conf->dev_sectors; 4157 size = raid10_size(mddev, 0, 0); 4158 md_set_array_sectors(mddev, size); 4159 mddev->resync_max_sectors = size; 4160 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags); 4161 4162 if (md_integrity_register(mddev)) 4163 goto out_free_conf; 4164 4165 if (conf->reshape_progress != MaxSector) { 4166 unsigned long before_length, after_length; 4167 4168 before_length = ((1 << conf->prev.chunk_shift) * 4169 conf->prev.far_copies); 4170 after_length = ((1 << conf->geo.chunk_shift) * 4171 conf->geo.far_copies); 4172 4173 if (max(before_length, after_length) > min_offset_diff) { 4174 /* This cannot work */ 4175 pr_warn("md/raid10: offset difference not enough to continue reshape\n"); 4176 goto out_free_conf; 4177 } 4178 conf->offset_diff = min_offset_diff; 4179 4180 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 4181 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 4182 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 4183 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4184 } 4185 4186 return 0; 4187 4188 out_free_conf: 4189 md_unregister_thread(mddev, &mddev->thread); 4190 raid10_free_conf(conf); 4191 mddev->private = NULL; 4192 out: 4193 return ret; 4194 } 4195 4196 static void raid10_free(struct mddev *mddev, void *priv) 4197 { 4198 raid10_free_conf(priv); 4199 } 4200 4201 static void raid10_quiesce(struct mddev *mddev, int quiesce) 4202 { 4203 struct r10conf *conf = mddev->private; 4204 4205 if (quiesce) 4206 raise_barrier(conf, 0); 4207 else 4208 lower_barrier(conf); 4209 } 4210 4211 static int raid10_resize(struct mddev *mddev, sector_t sectors) 4212 { 4213 /* Resize of 'far' arrays is not supported. 4214 * For 'near' and 'offset' arrays we can set the 4215 * number of sectors used to be an appropriate multiple 4216 * of the chunk size. 4217 * For 'offset', this is far_copies*chunksize. 4218 * For 'near' the multiplier is the LCM of 4219 * near_copies and raid_disks. 4220 * So if far_copies > 1 && !far_offset, fail. 4221 * Else find LCM(raid_disks, near_copy)*far_copies and 4222 * multiply by chunk_size. Then round to this number. 4223 * This is mostly done by raid10_size() 4224 */ 4225 struct r10conf *conf = mddev->private; 4226 sector_t oldsize, size; 4227 int ret; 4228 4229 if (mddev->reshape_position != MaxSector) 4230 return -EBUSY; 4231 4232 if (conf->geo.far_copies > 1 && !conf->geo.far_offset) 4233 return -EINVAL; 4234 4235 oldsize = raid10_size(mddev, 0, 0); 4236 size = raid10_size(mddev, sectors, 0); 4237 if (mddev->external_size && 4238 mddev->array_sectors > size) 4239 return -EINVAL; 4240 4241 ret = mddev->bitmap_ops->resize(mddev, size, 0, false); 4242 if (ret) 4243 return ret; 4244 4245 md_set_array_sectors(mddev, size); 4246 if (sectors > mddev->dev_sectors && 4247 mddev->recovery_cp > oldsize) { 4248 mddev->recovery_cp = oldsize; 4249 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4250 } 4251 calc_sectors(conf, sectors); 4252 mddev->dev_sectors = conf->dev_sectors; 4253 mddev->resync_max_sectors = size; 4254 return 0; 4255 } 4256 4257 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs) 4258 { 4259 struct md_rdev *rdev; 4260 struct r10conf *conf; 4261 4262 if (mddev->degraded > 0) { 4263 pr_warn("md/raid10:%s: Error: degraded raid0!\n", 4264 mdname(mddev)); 4265 return ERR_PTR(-EINVAL); 4266 } 4267 sector_div(size, devs); 4268 4269 /* Set new parameters */ 4270 mddev->new_level = 10; 4271 /* new layout: far_copies = 1, near_copies = 2 */ 4272 mddev->new_layout = (1<<8) + 2; 4273 mddev->new_chunk_sectors = mddev->chunk_sectors; 4274 mddev->delta_disks = mddev->raid_disks; 4275 mddev->raid_disks *= 2; 4276 /* make sure it will be not marked as dirty */ 4277 mddev->recovery_cp = MaxSector; 4278 mddev->dev_sectors = size; 4279 4280 conf = setup_conf(mddev); 4281 if (!IS_ERR(conf)) { 4282 rdev_for_each(rdev, mddev) 4283 if (rdev->raid_disk >= 0) { 4284 rdev->new_raid_disk = rdev->raid_disk * 2; 4285 rdev->sectors = size; 4286 } 4287 } 4288 4289 return conf; 4290 } 4291 4292 static void *raid10_takeover(struct mddev *mddev) 4293 { 4294 struct r0conf *raid0_conf; 4295 4296 /* raid10 can take over: 4297 * raid0 - providing it has only two drives 4298 */ 4299 if (mddev->level == 0) { 4300 /* for raid0 takeover only one zone is supported */ 4301 raid0_conf = mddev->private; 4302 if (raid0_conf->nr_strip_zones > 1) { 4303 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n", 4304 mdname(mddev)); 4305 return ERR_PTR(-EINVAL); 4306 } 4307 return raid10_takeover_raid0(mddev, 4308 raid0_conf->strip_zone->zone_end, 4309 raid0_conf->strip_zone->nb_dev); 4310 } 4311 return ERR_PTR(-EINVAL); 4312 } 4313 4314 static int raid10_check_reshape(struct mddev *mddev) 4315 { 4316 /* Called when there is a request to change 4317 * - layout (to ->new_layout) 4318 * - chunk size (to ->new_chunk_sectors) 4319 * - raid_disks (by delta_disks) 4320 * or when trying to restart a reshape that was ongoing. 4321 * 4322 * We need to validate the request and possibly allocate 4323 * space if that might be an issue later. 4324 * 4325 * Currently we reject any reshape of a 'far' mode array, 4326 * allow chunk size to change if new is generally acceptable, 4327 * allow raid_disks to increase, and allow 4328 * a switch between 'near' mode and 'offset' mode. 4329 */ 4330 struct r10conf *conf = mddev->private; 4331 struct geom geo; 4332 4333 if (conf->geo.far_copies != 1 && !conf->geo.far_offset) 4334 return -EINVAL; 4335 4336 if (setup_geo(&geo, mddev, geo_start) != conf->copies) 4337 /* mustn't change number of copies */ 4338 return -EINVAL; 4339 if (geo.far_copies > 1 && !geo.far_offset) 4340 /* Cannot switch to 'far' mode */ 4341 return -EINVAL; 4342 4343 if (mddev->array_sectors & geo.chunk_mask) 4344 /* not factor of array size */ 4345 return -EINVAL; 4346 4347 if (!enough(conf, -1)) 4348 return -EINVAL; 4349 4350 kfree(conf->mirrors_new); 4351 conf->mirrors_new = NULL; 4352 if (mddev->delta_disks > 0) { 4353 /* allocate new 'mirrors' list */ 4354 conf->mirrors_new = 4355 kcalloc(mddev->raid_disks + mddev->delta_disks, 4356 sizeof(struct raid10_info), 4357 GFP_KERNEL); 4358 if (!conf->mirrors_new) 4359 return -ENOMEM; 4360 } 4361 return 0; 4362 } 4363 4364 /* 4365 * Need to check if array has failed when deciding whether to: 4366 * - start an array 4367 * - remove non-faulty devices 4368 * - add a spare 4369 * - allow a reshape 4370 * This determination is simple when no reshape is happening. 4371 * However if there is a reshape, we need to carefully check 4372 * both the before and after sections. 4373 * This is because some failed devices may only affect one 4374 * of the two sections, and some non-in_sync devices may 4375 * be insync in the section most affected by failed devices. 4376 */ 4377 static int calc_degraded(struct r10conf *conf) 4378 { 4379 int degraded, degraded2; 4380 int i; 4381 4382 degraded = 0; 4383 /* 'prev' section first */ 4384 for (i = 0; i < conf->prev.raid_disks; i++) { 4385 struct md_rdev *rdev = conf->mirrors[i].rdev; 4386 4387 if (!rdev || test_bit(Faulty, &rdev->flags)) 4388 degraded++; 4389 else if (!test_bit(In_sync, &rdev->flags)) 4390 /* When we can reduce the number of devices in 4391 * an array, this might not contribute to 4392 * 'degraded'. It does now. 4393 */ 4394 degraded++; 4395 } 4396 if (conf->geo.raid_disks == conf->prev.raid_disks) 4397 return degraded; 4398 degraded2 = 0; 4399 for (i = 0; i < conf->geo.raid_disks; i++) { 4400 struct md_rdev *rdev = conf->mirrors[i].rdev; 4401 4402 if (!rdev || test_bit(Faulty, &rdev->flags)) 4403 degraded2++; 4404 else if (!test_bit(In_sync, &rdev->flags)) { 4405 /* If reshape is increasing the number of devices, 4406 * this section has already been recovered, so 4407 * it doesn't contribute to degraded. 4408 * else it does. 4409 */ 4410 if (conf->geo.raid_disks <= conf->prev.raid_disks) 4411 degraded2++; 4412 } 4413 } 4414 if (degraded2 > degraded) 4415 return degraded2; 4416 return degraded; 4417 } 4418 4419 static int raid10_start_reshape(struct mddev *mddev) 4420 { 4421 /* A 'reshape' has been requested. This commits 4422 * the various 'new' fields and sets MD_RECOVER_RESHAPE 4423 * This also checks if there are enough spares and adds them 4424 * to the array. 4425 * We currently require enough spares to make the final 4426 * array non-degraded. We also require that the difference 4427 * between old and new data_offset - on each device - is 4428 * enough that we never risk over-writing. 4429 */ 4430 4431 unsigned long before_length, after_length; 4432 sector_t min_offset_diff = 0; 4433 int first = 1; 4434 struct geom new; 4435 struct r10conf *conf = mddev->private; 4436 struct md_rdev *rdev; 4437 int spares = 0; 4438 int ret; 4439 4440 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 4441 return -EBUSY; 4442 4443 if (setup_geo(&new, mddev, geo_start) != conf->copies) 4444 return -EINVAL; 4445 4446 before_length = ((1 << conf->prev.chunk_shift) * 4447 conf->prev.far_copies); 4448 after_length = ((1 << conf->geo.chunk_shift) * 4449 conf->geo.far_copies); 4450 4451 rdev_for_each(rdev, mddev) { 4452 if (!test_bit(In_sync, &rdev->flags) 4453 && !test_bit(Faulty, &rdev->flags)) 4454 spares++; 4455 if (rdev->raid_disk >= 0) { 4456 long long diff = (rdev->new_data_offset 4457 - rdev->data_offset); 4458 if (!mddev->reshape_backwards) 4459 diff = -diff; 4460 if (diff < 0) 4461 diff = 0; 4462 if (first || diff < min_offset_diff) 4463 min_offset_diff = diff; 4464 first = 0; 4465 } 4466 } 4467 4468 if (max(before_length, after_length) > min_offset_diff) 4469 return -EINVAL; 4470 4471 if (spares < mddev->delta_disks) 4472 return -EINVAL; 4473 4474 conf->offset_diff = min_offset_diff; 4475 spin_lock_irq(&conf->device_lock); 4476 if (conf->mirrors_new) { 4477 memcpy(conf->mirrors_new, conf->mirrors, 4478 sizeof(struct raid10_info)*conf->prev.raid_disks); 4479 smp_mb(); 4480 kfree(conf->mirrors_old); 4481 conf->mirrors_old = conf->mirrors; 4482 conf->mirrors = conf->mirrors_new; 4483 conf->mirrors_new = NULL; 4484 } 4485 setup_geo(&conf->geo, mddev, geo_start); 4486 smp_mb(); 4487 if (mddev->reshape_backwards) { 4488 sector_t size = raid10_size(mddev, 0, 0); 4489 if (size < mddev->array_sectors) { 4490 spin_unlock_irq(&conf->device_lock); 4491 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n", 4492 mdname(mddev)); 4493 return -EINVAL; 4494 } 4495 mddev->resync_max_sectors = size; 4496 conf->reshape_progress = size; 4497 } else 4498 conf->reshape_progress = 0; 4499 conf->reshape_safe = conf->reshape_progress; 4500 spin_unlock_irq(&conf->device_lock); 4501 4502 if (mddev->delta_disks && mddev->bitmap) { 4503 struct mdp_superblock_1 *sb = NULL; 4504 sector_t oldsize, newsize; 4505 4506 oldsize = raid10_size(mddev, 0, 0); 4507 newsize = raid10_size(mddev, 0, conf->geo.raid_disks); 4508 4509 if (!mddev_is_clustered(mddev)) { 4510 ret = mddev->bitmap_ops->resize(mddev, newsize, 0, false); 4511 if (ret) 4512 goto abort; 4513 else 4514 goto out; 4515 } 4516 4517 rdev_for_each(rdev, mddev) { 4518 if (rdev->raid_disk > -1 && 4519 !test_bit(Faulty, &rdev->flags)) 4520 sb = page_address(rdev->sb_page); 4521 } 4522 4523 /* 4524 * some node is already performing reshape, and no need to 4525 * call bitmap_ops->resize again since it should be called when 4526 * receiving BITMAP_RESIZE msg 4527 */ 4528 if ((sb && (le32_to_cpu(sb->feature_map) & 4529 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize)) 4530 goto out; 4531 4532 ret = mddev->bitmap_ops->resize(mddev, newsize, 0, false); 4533 if (ret) 4534 goto abort; 4535 4536 ret = mddev->cluster_ops->resize_bitmaps(mddev, newsize, oldsize); 4537 if (ret) { 4538 mddev->bitmap_ops->resize(mddev, oldsize, 0, false); 4539 goto abort; 4540 } 4541 } 4542 out: 4543 if (mddev->delta_disks > 0) { 4544 rdev_for_each(rdev, mddev) 4545 if (rdev->raid_disk < 0 && 4546 !test_bit(Faulty, &rdev->flags)) { 4547 if (raid10_add_disk(mddev, rdev) == 0) { 4548 if (rdev->raid_disk >= 4549 conf->prev.raid_disks) 4550 set_bit(In_sync, &rdev->flags); 4551 else 4552 rdev->recovery_offset = 0; 4553 4554 /* Failure here is OK */ 4555 sysfs_link_rdev(mddev, rdev); 4556 } 4557 } else if (rdev->raid_disk >= conf->prev.raid_disks 4558 && !test_bit(Faulty, &rdev->flags)) { 4559 /* This is a spare that was manually added */ 4560 set_bit(In_sync, &rdev->flags); 4561 } 4562 } 4563 /* When a reshape changes the number of devices, 4564 * ->degraded is measured against the larger of the 4565 * pre and post numbers. 4566 */ 4567 spin_lock_irq(&conf->device_lock); 4568 mddev->degraded = calc_degraded(conf); 4569 spin_unlock_irq(&conf->device_lock); 4570 mddev->raid_disks = conf->geo.raid_disks; 4571 mddev->reshape_position = conf->reshape_progress; 4572 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 4573 4574 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 4575 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 4576 clear_bit(MD_RECOVERY_DONE, &mddev->recovery); 4577 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 4578 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 4579 conf->reshape_checkpoint = jiffies; 4580 md_new_event(); 4581 return 0; 4582 4583 abort: 4584 mddev->recovery = 0; 4585 spin_lock_irq(&conf->device_lock); 4586 conf->geo = conf->prev; 4587 mddev->raid_disks = conf->geo.raid_disks; 4588 rdev_for_each(rdev, mddev) 4589 rdev->new_data_offset = rdev->data_offset; 4590 smp_wmb(); 4591 conf->reshape_progress = MaxSector; 4592 conf->reshape_safe = MaxSector; 4593 mddev->reshape_position = MaxSector; 4594 spin_unlock_irq(&conf->device_lock); 4595 return ret; 4596 } 4597 4598 /* Calculate the last device-address that could contain 4599 * any block from the chunk that includes the array-address 's' 4600 * and report the next address. 4601 * i.e. the address returned will be chunk-aligned and after 4602 * any data that is in the chunk containing 's'. 4603 */ 4604 static sector_t last_dev_address(sector_t s, struct geom *geo) 4605 { 4606 s = (s | geo->chunk_mask) + 1; 4607 s >>= geo->chunk_shift; 4608 s *= geo->near_copies; 4609 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks); 4610 s *= geo->far_copies; 4611 s <<= geo->chunk_shift; 4612 return s; 4613 } 4614 4615 /* Calculate the first device-address that could contain 4616 * any block from the chunk that includes the array-address 's'. 4617 * This too will be the start of a chunk 4618 */ 4619 static sector_t first_dev_address(sector_t s, struct geom *geo) 4620 { 4621 s >>= geo->chunk_shift; 4622 s *= geo->near_copies; 4623 sector_div(s, geo->raid_disks); 4624 s *= geo->far_copies; 4625 s <<= geo->chunk_shift; 4626 return s; 4627 } 4628 4629 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, 4630 int *skipped) 4631 { 4632 /* We simply copy at most one chunk (smallest of old and new) 4633 * at a time, possibly less if that exceeds RESYNC_PAGES, 4634 * or we hit a bad block or something. 4635 * This might mean we pause for normal IO in the middle of 4636 * a chunk, but that is not a problem as mddev->reshape_position 4637 * can record any location. 4638 * 4639 * If we will want to write to a location that isn't 4640 * yet recorded as 'safe' (i.e. in metadata on disk) then 4641 * we need to flush all reshape requests and update the metadata. 4642 * 4643 * When reshaping forwards (e.g. to more devices), we interpret 4644 * 'safe' as the earliest block which might not have been copied 4645 * down yet. We divide this by previous stripe size and multiply 4646 * by previous stripe length to get lowest device offset that we 4647 * cannot write to yet. 4648 * We interpret 'sector_nr' as an address that we want to write to. 4649 * From this we use last_device_address() to find where we might 4650 * write to, and first_device_address on the 'safe' position. 4651 * If this 'next' write position is after the 'safe' position, 4652 * we must update the metadata to increase the 'safe' position. 4653 * 4654 * When reshaping backwards, we round in the opposite direction 4655 * and perform the reverse test: next write position must not be 4656 * less than current safe position. 4657 * 4658 * In all this the minimum difference in data offsets 4659 * (conf->offset_diff - always positive) allows a bit of slack, 4660 * so next can be after 'safe', but not by more than offset_diff 4661 * 4662 * We need to prepare all the bios here before we start any IO 4663 * to ensure the size we choose is acceptable to all devices. 4664 * The means one for each copy for write-out and an extra one for 4665 * read-in. 4666 * We store the read-in bio in ->master_bio and the others in 4667 * ->devs[x].bio and ->devs[x].repl_bio. 4668 */ 4669 struct r10conf *conf = mddev->private; 4670 struct r10bio *r10_bio; 4671 sector_t next, safe, last; 4672 int max_sectors; 4673 int nr_sectors; 4674 int s; 4675 struct md_rdev *rdev; 4676 int need_flush = 0; 4677 struct bio *blist; 4678 struct bio *bio, *read_bio; 4679 int sectors_done = 0; 4680 struct page **pages; 4681 4682 if (sector_nr == 0) { 4683 /* If restarting in the middle, skip the initial sectors */ 4684 if (mddev->reshape_backwards && 4685 conf->reshape_progress < raid10_size(mddev, 0, 0)) { 4686 sector_nr = (raid10_size(mddev, 0, 0) 4687 - conf->reshape_progress); 4688 } else if (!mddev->reshape_backwards && 4689 conf->reshape_progress > 0) 4690 sector_nr = conf->reshape_progress; 4691 if (sector_nr) { 4692 mddev->curr_resync_completed = sector_nr; 4693 sysfs_notify_dirent_safe(mddev->sysfs_completed); 4694 *skipped = 1; 4695 return sector_nr; 4696 } 4697 } 4698 4699 /* We don't use sector_nr to track where we are up to 4700 * as that doesn't work well for ->reshape_backwards. 4701 * So just use ->reshape_progress. 4702 */ 4703 if (mddev->reshape_backwards) { 4704 /* 'next' is the earliest device address that we might 4705 * write to for this chunk in the new layout 4706 */ 4707 next = first_dev_address(conf->reshape_progress - 1, 4708 &conf->geo); 4709 4710 /* 'safe' is the last device address that we might read from 4711 * in the old layout after a restart 4712 */ 4713 safe = last_dev_address(conf->reshape_safe - 1, 4714 &conf->prev); 4715 4716 if (next + conf->offset_diff < safe) 4717 need_flush = 1; 4718 4719 last = conf->reshape_progress - 1; 4720 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask 4721 & conf->prev.chunk_mask); 4722 if (sector_nr + RESYNC_SECTORS < last) 4723 sector_nr = last + 1 - RESYNC_SECTORS; 4724 } else { 4725 /* 'next' is after the last device address that we 4726 * might write to for this chunk in the new layout 4727 */ 4728 next = last_dev_address(conf->reshape_progress, &conf->geo); 4729 4730 /* 'safe' is the earliest device address that we might 4731 * read from in the old layout after a restart 4732 */ 4733 safe = first_dev_address(conf->reshape_safe, &conf->prev); 4734 4735 /* Need to update metadata if 'next' might be beyond 'safe' 4736 * as that would possibly corrupt data 4737 */ 4738 if (next > safe + conf->offset_diff) 4739 need_flush = 1; 4740 4741 sector_nr = conf->reshape_progress; 4742 last = sector_nr | (conf->geo.chunk_mask 4743 & conf->prev.chunk_mask); 4744 4745 if (sector_nr + RESYNC_SECTORS <= last) 4746 last = sector_nr + RESYNC_SECTORS - 1; 4747 } 4748 4749 if (need_flush || 4750 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { 4751 /* Need to update reshape_position in metadata */ 4752 wait_barrier(conf, false); 4753 mddev->reshape_position = conf->reshape_progress; 4754 if (mddev->reshape_backwards) 4755 mddev->curr_resync_completed = raid10_size(mddev, 0, 0) 4756 - conf->reshape_progress; 4757 else 4758 mddev->curr_resync_completed = conf->reshape_progress; 4759 conf->reshape_checkpoint = jiffies; 4760 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 4761 md_wakeup_thread(mddev->thread); 4762 wait_event(mddev->sb_wait, mddev->sb_flags == 0 || 4763 test_bit(MD_RECOVERY_INTR, &mddev->recovery)); 4764 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { 4765 allow_barrier(conf); 4766 return sectors_done; 4767 } 4768 conf->reshape_safe = mddev->reshape_position; 4769 allow_barrier(conf); 4770 } 4771 4772 raise_barrier(conf, 0); 4773 read_more: 4774 /* Now schedule reads for blocks from sector_nr to last */ 4775 r10_bio = raid10_alloc_init_r10buf(conf); 4776 r10_bio->state = 0; 4777 raise_barrier(conf, 1); 4778 atomic_set(&r10_bio->remaining, 0); 4779 r10_bio->mddev = mddev; 4780 r10_bio->sector = sector_nr; 4781 set_bit(R10BIO_IsReshape, &r10_bio->state); 4782 r10_bio->sectors = last - sector_nr + 1; 4783 rdev = read_balance(conf, r10_bio, &max_sectors); 4784 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state)); 4785 4786 if (!rdev) { 4787 /* Cannot read from here, so need to record bad blocks 4788 * on all the target devices. 4789 */ 4790 // FIXME 4791 mempool_free(r10_bio, &conf->r10buf_pool); 4792 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 4793 return sectors_done; 4794 } 4795 4796 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ, 4797 GFP_KERNEL, &mddev->bio_set); 4798 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr 4799 + rdev->data_offset); 4800 read_bio->bi_private = r10_bio; 4801 read_bio->bi_end_io = end_reshape_read; 4802 r10_bio->master_bio = read_bio; 4803 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum; 4804 4805 /* 4806 * Broadcast RESYNC message to other nodes, so all nodes would not 4807 * write to the region to avoid conflict. 4808 */ 4809 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) { 4810 struct mdp_superblock_1 *sb = NULL; 4811 int sb_reshape_pos = 0; 4812 4813 conf->cluster_sync_low = sector_nr; 4814 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS; 4815 sb = page_address(rdev->sb_page); 4816 if (sb) { 4817 sb_reshape_pos = le64_to_cpu(sb->reshape_position); 4818 /* 4819 * Set cluster_sync_low again if next address for array 4820 * reshape is less than cluster_sync_low. Since we can't 4821 * update cluster_sync_low until it has finished reshape. 4822 */ 4823 if (sb_reshape_pos < conf->cluster_sync_low) 4824 conf->cluster_sync_low = sb_reshape_pos; 4825 } 4826 4827 mddev->cluster_ops->resync_info_update(mddev, conf->cluster_sync_low, 4828 conf->cluster_sync_high); 4829 } 4830 4831 /* Now find the locations in the new layout */ 4832 __raid10_find_phys(&conf->geo, r10_bio); 4833 4834 blist = read_bio; 4835 read_bio->bi_next = NULL; 4836 4837 for (s = 0; s < conf->copies*2; s++) { 4838 struct bio *b; 4839 int d = r10_bio->devs[s/2].devnum; 4840 struct md_rdev *rdev2; 4841 if (s&1) { 4842 rdev2 = conf->mirrors[d].replacement; 4843 b = r10_bio->devs[s/2].repl_bio; 4844 } else { 4845 rdev2 = conf->mirrors[d].rdev; 4846 b = r10_bio->devs[s/2].bio; 4847 } 4848 if (!rdev2 || test_bit(Faulty, &rdev2->flags)) 4849 continue; 4850 4851 bio_set_dev(b, rdev2->bdev); 4852 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr + 4853 rdev2->new_data_offset; 4854 b->bi_end_io = end_reshape_write; 4855 b->bi_opf = REQ_OP_WRITE; 4856 b->bi_next = blist; 4857 blist = b; 4858 } 4859 4860 /* Now add as many pages as possible to all of these bios. */ 4861 4862 nr_sectors = 0; 4863 pages = get_resync_pages(r10_bio->devs[0].bio)->pages; 4864 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) { 4865 struct page *page = pages[s / (PAGE_SIZE >> 9)]; 4866 int len = (max_sectors - s) << 9; 4867 if (len > PAGE_SIZE) 4868 len = PAGE_SIZE; 4869 for (bio = blist; bio ; bio = bio->bi_next) { 4870 if (WARN_ON(!bio_add_page(bio, page, len, 0))) { 4871 bio->bi_status = BLK_STS_RESOURCE; 4872 bio_endio(bio); 4873 return sectors_done; 4874 } 4875 } 4876 sector_nr += len >> 9; 4877 nr_sectors += len >> 9; 4878 } 4879 r10_bio->sectors = nr_sectors; 4880 4881 /* Now submit the read */ 4882 md_sync_acct_bio(read_bio, r10_bio->sectors); 4883 atomic_inc(&r10_bio->remaining); 4884 read_bio->bi_next = NULL; 4885 submit_bio_noacct(read_bio); 4886 sectors_done += nr_sectors; 4887 if (sector_nr <= last) 4888 goto read_more; 4889 4890 lower_barrier(conf); 4891 4892 /* Now that we have done the whole section we can 4893 * update reshape_progress 4894 */ 4895 if (mddev->reshape_backwards) 4896 conf->reshape_progress -= sectors_done; 4897 else 4898 conf->reshape_progress += sectors_done; 4899 4900 return sectors_done; 4901 } 4902 4903 static void end_reshape_request(struct r10bio *r10_bio); 4904 static int handle_reshape_read_error(struct mddev *mddev, 4905 struct r10bio *r10_bio); 4906 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio) 4907 { 4908 /* Reshape read completed. Hopefully we have a block 4909 * to write out. 4910 * If we got a read error then we do sync 1-page reads from 4911 * elsewhere until we find the data - or give up. 4912 */ 4913 struct r10conf *conf = mddev->private; 4914 int s; 4915 4916 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) 4917 if (handle_reshape_read_error(mddev, r10_bio) < 0) { 4918 /* Reshape has been aborted */ 4919 md_done_sync(mddev, r10_bio->sectors, 0); 4920 return; 4921 } 4922 4923 /* We definitely have the data in the pages, schedule the 4924 * writes. 4925 */ 4926 atomic_set(&r10_bio->remaining, 1); 4927 for (s = 0; s < conf->copies*2; s++) { 4928 struct bio *b; 4929 int d = r10_bio->devs[s/2].devnum; 4930 struct md_rdev *rdev; 4931 if (s&1) { 4932 rdev = conf->mirrors[d].replacement; 4933 b = r10_bio->devs[s/2].repl_bio; 4934 } else { 4935 rdev = conf->mirrors[d].rdev; 4936 b = r10_bio->devs[s/2].bio; 4937 } 4938 if (!rdev || test_bit(Faulty, &rdev->flags)) 4939 continue; 4940 4941 atomic_inc(&rdev->nr_pending); 4942 md_sync_acct_bio(b, r10_bio->sectors); 4943 atomic_inc(&r10_bio->remaining); 4944 b->bi_next = NULL; 4945 submit_bio_noacct(b); 4946 } 4947 end_reshape_request(r10_bio); 4948 } 4949 4950 static void end_reshape(struct r10conf *conf) 4951 { 4952 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) 4953 return; 4954 4955 spin_lock_irq(&conf->device_lock); 4956 conf->prev = conf->geo; 4957 md_finish_reshape(conf->mddev); 4958 smp_wmb(); 4959 conf->reshape_progress = MaxSector; 4960 conf->reshape_safe = MaxSector; 4961 spin_unlock_irq(&conf->device_lock); 4962 4963 mddev_update_io_opt(conf->mddev, raid10_nr_stripes(conf)); 4964 conf->fullsync = 0; 4965 } 4966 4967 static void raid10_update_reshape_pos(struct mddev *mddev) 4968 { 4969 struct r10conf *conf = mddev->private; 4970 sector_t lo, hi; 4971 4972 mddev->cluster_ops->resync_info_get(mddev, &lo, &hi); 4973 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo)) 4974 || mddev->reshape_position == MaxSector) 4975 conf->reshape_progress = mddev->reshape_position; 4976 else 4977 WARN_ON_ONCE(1); 4978 } 4979 4980 static int handle_reshape_read_error(struct mddev *mddev, 4981 struct r10bio *r10_bio) 4982 { 4983 /* Use sync reads to get the blocks from somewhere else */ 4984 int sectors = r10_bio->sectors; 4985 struct r10conf *conf = mddev->private; 4986 struct r10bio *r10b; 4987 int slot = 0; 4988 int idx = 0; 4989 struct page **pages; 4990 4991 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO); 4992 if (!r10b) { 4993 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 4994 return -ENOMEM; 4995 } 4996 4997 /* reshape IOs share pages from .devs[0].bio */ 4998 pages = get_resync_pages(r10_bio->devs[0].bio)->pages; 4999 5000 r10b->sector = r10_bio->sector; 5001 __raid10_find_phys(&conf->prev, r10b); 5002 5003 while (sectors) { 5004 int s = sectors; 5005 int success = 0; 5006 int first_slot = slot; 5007 5008 if (s > (PAGE_SIZE >> 9)) 5009 s = PAGE_SIZE >> 9; 5010 5011 while (!success) { 5012 int d = r10b->devs[slot].devnum; 5013 struct md_rdev *rdev = conf->mirrors[d].rdev; 5014 sector_t addr; 5015 if (rdev == NULL || 5016 test_bit(Faulty, &rdev->flags) || 5017 !test_bit(In_sync, &rdev->flags)) 5018 goto failed; 5019 5020 addr = r10b->devs[slot].addr + idx * PAGE_SIZE; 5021 atomic_inc(&rdev->nr_pending); 5022 success = sync_page_io(rdev, 5023 addr, 5024 s << 9, 5025 pages[idx], 5026 REQ_OP_READ, false); 5027 rdev_dec_pending(rdev, mddev); 5028 if (success) 5029 break; 5030 failed: 5031 slot++; 5032 if (slot >= conf->copies) 5033 slot = 0; 5034 if (slot == first_slot) 5035 break; 5036 } 5037 if (!success) { 5038 /* couldn't read this block, must give up */ 5039 set_bit(MD_RECOVERY_INTR, 5040 &mddev->recovery); 5041 kfree(r10b); 5042 return -EIO; 5043 } 5044 sectors -= s; 5045 idx++; 5046 } 5047 kfree(r10b); 5048 return 0; 5049 } 5050 5051 static void end_reshape_write(struct bio *bio) 5052 { 5053 struct r10bio *r10_bio = get_resync_r10bio(bio); 5054 struct mddev *mddev = r10_bio->mddev; 5055 struct r10conf *conf = mddev->private; 5056 int d; 5057 int slot; 5058 int repl; 5059 struct md_rdev *rdev = NULL; 5060 5061 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 5062 rdev = repl ? conf->mirrors[d].replacement : 5063 conf->mirrors[d].rdev; 5064 5065 if (bio->bi_status) { 5066 /* FIXME should record badblock */ 5067 md_error(mddev, rdev); 5068 } 5069 5070 rdev_dec_pending(rdev, mddev); 5071 end_reshape_request(r10_bio); 5072 } 5073 5074 static void end_reshape_request(struct r10bio *r10_bio) 5075 { 5076 if (!atomic_dec_and_test(&r10_bio->remaining)) 5077 return; 5078 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1); 5079 bio_put(r10_bio->master_bio); 5080 put_buf(r10_bio); 5081 } 5082 5083 static void raid10_finish_reshape(struct mddev *mddev) 5084 { 5085 struct r10conf *conf = mddev->private; 5086 5087 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 5088 return; 5089 5090 if (mddev->delta_disks > 0) { 5091 if (mddev->recovery_cp > mddev->resync_max_sectors) { 5092 mddev->recovery_cp = mddev->resync_max_sectors; 5093 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5094 } 5095 mddev->resync_max_sectors = mddev->array_sectors; 5096 } else { 5097 int d; 5098 for (d = conf->geo.raid_disks ; 5099 d < conf->geo.raid_disks - mddev->delta_disks; 5100 d++) { 5101 struct md_rdev *rdev = conf->mirrors[d].rdev; 5102 if (rdev) 5103 clear_bit(In_sync, &rdev->flags); 5104 rdev = conf->mirrors[d].replacement; 5105 if (rdev) 5106 clear_bit(In_sync, &rdev->flags); 5107 } 5108 } 5109 mddev->layout = mddev->new_layout; 5110 mddev->chunk_sectors = 1 << conf->geo.chunk_shift; 5111 mddev->reshape_position = MaxSector; 5112 mddev->delta_disks = 0; 5113 mddev->reshape_backwards = 0; 5114 } 5115 5116 static struct md_personality raid10_personality = 5117 { 5118 .head = { 5119 .type = MD_PERSONALITY, 5120 .id = ID_RAID10, 5121 .name = "raid10", 5122 .owner = THIS_MODULE, 5123 }, 5124 5125 .make_request = raid10_make_request, 5126 .run = raid10_run, 5127 .free = raid10_free, 5128 .status = raid10_status, 5129 .error_handler = raid10_error, 5130 .hot_add_disk = raid10_add_disk, 5131 .hot_remove_disk= raid10_remove_disk, 5132 .spare_active = raid10_spare_active, 5133 .sync_request = raid10_sync_request, 5134 .quiesce = raid10_quiesce, 5135 .size = raid10_size, 5136 .resize = raid10_resize, 5137 .takeover = raid10_takeover, 5138 .check_reshape = raid10_check_reshape, 5139 .start_reshape = raid10_start_reshape, 5140 .finish_reshape = raid10_finish_reshape, 5141 .update_reshape_pos = raid10_update_reshape_pos, 5142 }; 5143 5144 static int __init raid10_init(void) 5145 { 5146 return register_md_submodule(&raid10_personality.head); 5147 } 5148 5149 static void __exit raid10_exit(void) 5150 { 5151 unregister_md_submodule(&raid10_personality.head); 5152 } 5153 5154 module_init(raid10_init); 5155 module_exit(raid10_exit); 5156 MODULE_LICENSE("GPL"); 5157 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); 5158 MODULE_ALIAS("md-personality-9"); /* RAID10 */ 5159 MODULE_ALIAS("md-raid10"); 5160 MODULE_ALIAS("md-level-10"); 5161