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