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