1 /* 2 * raid10.c : Multiple Devices driver for Linux 3 * 4 * Copyright (C) 2000-2004 Neil Brown 5 * 6 * RAID-10 support for md. 7 * 8 * Base on code in raid1.c. See raid1.c for further copyright information. 9 * 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2, or (at your option) 14 * any later version. 15 * 16 * You should have received a copy of the GNU General Public License 17 * (for example /usr/src/linux/COPYING); if not, write to the Free 18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 21 #include <linux/slab.h> 22 #include <linux/delay.h> 23 #include <linux/blkdev.h> 24 #include <linux/module.h> 25 #include <linux/seq_file.h> 26 #include <linux/ratelimit.h> 27 #include "md.h" 28 #include "raid10.h" 29 #include "raid0.h" 30 #include "bitmap.h" 31 32 /* 33 * RAID10 provides a combination of RAID0 and RAID1 functionality. 34 * The layout of data is defined by 35 * chunk_size 36 * raid_disks 37 * near_copies (stored in low byte of layout) 38 * far_copies (stored in second byte of layout) 39 * far_offset (stored in bit 16 of layout ) 40 * 41 * The data to be stored is divided into chunks using chunksize. 42 * Each device is divided into far_copies sections. 43 * In each section, chunks are laid out in a style similar to raid0, but 44 * near_copies copies of each chunk is stored (each on a different drive). 45 * The starting device for each section is offset near_copies from the starting 46 * device of the previous section. 47 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different 48 * drive. 49 * near_copies and far_copies must be at least one, and their product is at most 50 * raid_disks. 51 * 52 * If far_offset is true, then the far_copies are handled a bit differently. 53 * The copies are still in different stripes, but instead of be very far apart 54 * on disk, there are adjacent stripes. 55 */ 56 57 /* 58 * Number of guaranteed r10bios in case of extreme VM load: 59 */ 60 #define NR_RAID10_BIOS 256 61 62 /* When there are this many requests queue to be written by 63 * the raid10 thread, we become 'congested' to provide back-pressure 64 * for writeback. 65 */ 66 static int max_queued_requests = 1024; 67 68 static void allow_barrier(struct r10conf *conf); 69 static void lower_barrier(struct r10conf *conf); 70 71 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) 72 { 73 struct r10conf *conf = data; 74 int size = offsetof(struct r10bio, devs[conf->copies]); 75 76 /* allocate a r10bio with room for raid_disks entries in the bios array */ 77 return kzalloc(size, gfp_flags); 78 } 79 80 static void r10bio_pool_free(void *r10_bio, void *data) 81 { 82 kfree(r10_bio); 83 } 84 85 /* Maximum size of each resync request */ 86 #define RESYNC_BLOCK_SIZE (64*1024) 87 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) 88 /* amount of memory to reserve for resync requests */ 89 #define RESYNC_WINDOW (1024*1024) 90 /* maximum number of concurrent requests, memory permitting */ 91 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) 92 93 /* 94 * When performing a resync, we need to read and compare, so 95 * we need as many pages are there are copies. 96 * When performing a recovery, we need 2 bios, one for read, 97 * one for write (we recover only one drive per r10buf) 98 * 99 */ 100 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) 101 { 102 struct r10conf *conf = data; 103 struct page *page; 104 struct r10bio *r10_bio; 105 struct bio *bio; 106 int i, j; 107 int nalloc; 108 109 r10_bio = r10bio_pool_alloc(gfp_flags, conf); 110 if (!r10_bio) 111 return NULL; 112 113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) 114 nalloc = conf->copies; /* resync */ 115 else 116 nalloc = 2; /* recovery */ 117 118 /* 119 * Allocate bios. 120 */ 121 for (j = nalloc ; j-- ; ) { 122 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); 123 if (!bio) 124 goto out_free_bio; 125 r10_bio->devs[j].bio = bio; 126 } 127 /* 128 * Allocate RESYNC_PAGES data pages and attach them 129 * where needed. 130 */ 131 for (j = 0 ; j < nalloc; j++) { 132 bio = r10_bio->devs[j].bio; 133 for (i = 0; i < RESYNC_PAGES; i++) { 134 if (j == 1 && !test_bit(MD_RECOVERY_SYNC, 135 &conf->mddev->recovery)) { 136 /* we can share bv_page's during recovery */ 137 struct bio *rbio = r10_bio->devs[0].bio; 138 page = rbio->bi_io_vec[i].bv_page; 139 get_page(page); 140 } else 141 page = alloc_page(gfp_flags); 142 if (unlikely(!page)) 143 goto out_free_pages; 144 145 bio->bi_io_vec[i].bv_page = page; 146 } 147 } 148 149 return r10_bio; 150 151 out_free_pages: 152 for ( ; i > 0 ; i--) 153 safe_put_page(bio->bi_io_vec[i-1].bv_page); 154 while (j--) 155 for (i = 0; i < RESYNC_PAGES ; i++) 156 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page); 157 j = -1; 158 out_free_bio: 159 while ( ++j < nalloc ) 160 bio_put(r10_bio->devs[j].bio); 161 r10bio_pool_free(r10_bio, conf); 162 return NULL; 163 } 164 165 static void r10buf_pool_free(void *__r10_bio, void *data) 166 { 167 int i; 168 struct r10conf *conf = data; 169 struct r10bio *r10bio = __r10_bio; 170 int j; 171 172 for (j=0; j < conf->copies; j++) { 173 struct bio *bio = r10bio->devs[j].bio; 174 if (bio) { 175 for (i = 0; i < RESYNC_PAGES; i++) { 176 safe_put_page(bio->bi_io_vec[i].bv_page); 177 bio->bi_io_vec[i].bv_page = NULL; 178 } 179 bio_put(bio); 180 } 181 } 182 r10bio_pool_free(r10bio, conf); 183 } 184 185 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) 186 { 187 int i; 188 189 for (i = 0; i < conf->copies; i++) { 190 struct bio **bio = & r10_bio->devs[i].bio; 191 if (!BIO_SPECIAL(*bio)) 192 bio_put(*bio); 193 *bio = NULL; 194 } 195 } 196 197 static void free_r10bio(struct r10bio *r10_bio) 198 { 199 struct r10conf *conf = r10_bio->mddev->private; 200 201 put_all_bios(conf, r10_bio); 202 mempool_free(r10_bio, conf->r10bio_pool); 203 } 204 205 static void put_buf(struct r10bio *r10_bio) 206 { 207 struct r10conf *conf = r10_bio->mddev->private; 208 209 mempool_free(r10_bio, conf->r10buf_pool); 210 211 lower_barrier(conf); 212 } 213 214 static void reschedule_retry(struct r10bio *r10_bio) 215 { 216 unsigned long flags; 217 struct mddev *mddev = r10_bio->mddev; 218 struct r10conf *conf = mddev->private; 219 220 spin_lock_irqsave(&conf->device_lock, flags); 221 list_add(&r10_bio->retry_list, &conf->retry_list); 222 conf->nr_queued ++; 223 spin_unlock_irqrestore(&conf->device_lock, flags); 224 225 /* wake up frozen array... */ 226 wake_up(&conf->wait_barrier); 227 228 md_wakeup_thread(mddev->thread); 229 } 230 231 /* 232 * raid_end_bio_io() is called when we have finished servicing a mirrored 233 * operation and are ready to return a success/failure code to the buffer 234 * cache layer. 235 */ 236 static void raid_end_bio_io(struct r10bio *r10_bio) 237 { 238 struct bio *bio = r10_bio->master_bio; 239 int done; 240 struct r10conf *conf = r10_bio->mddev->private; 241 242 if (bio->bi_phys_segments) { 243 unsigned long flags; 244 spin_lock_irqsave(&conf->device_lock, flags); 245 bio->bi_phys_segments--; 246 done = (bio->bi_phys_segments == 0); 247 spin_unlock_irqrestore(&conf->device_lock, flags); 248 } else 249 done = 1; 250 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) 251 clear_bit(BIO_UPTODATE, &bio->bi_flags); 252 if (done) { 253 bio_endio(bio, 0); 254 /* 255 * Wake up any possible resync thread that waits for the device 256 * to go idle. 257 */ 258 allow_barrier(conf); 259 } 260 free_r10bio(r10_bio); 261 } 262 263 /* 264 * Update disk head position estimator based on IRQ completion info. 265 */ 266 static inline void update_head_pos(int slot, struct r10bio *r10_bio) 267 { 268 struct r10conf *conf = r10_bio->mddev->private; 269 270 conf->mirrors[r10_bio->devs[slot].devnum].head_position = 271 r10_bio->devs[slot].addr + (r10_bio->sectors); 272 } 273 274 /* 275 * Find the disk number which triggered given bio 276 */ 277 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, 278 struct bio *bio, int *slotp) 279 { 280 int slot; 281 282 for (slot = 0; slot < conf->copies; slot++) 283 if (r10_bio->devs[slot].bio == bio) 284 break; 285 286 BUG_ON(slot == conf->copies); 287 update_head_pos(slot, r10_bio); 288 289 if (slotp) 290 *slotp = slot; 291 return r10_bio->devs[slot].devnum; 292 } 293 294 static void raid10_end_read_request(struct bio *bio, int error) 295 { 296 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 297 struct r10bio *r10_bio = bio->bi_private; 298 int slot, dev; 299 struct r10conf *conf = r10_bio->mddev->private; 300 301 302 slot = r10_bio->read_slot; 303 dev = r10_bio->devs[slot].devnum; 304 /* 305 * this branch is our 'one mirror IO has finished' event handler: 306 */ 307 update_head_pos(slot, r10_bio); 308 309 if (uptodate) { 310 /* 311 * Set R10BIO_Uptodate in our master bio, so that 312 * we will return a good error code to the higher 313 * levels even if IO on some other mirrored buffer fails. 314 * 315 * The 'master' represents the composite IO operation to 316 * user-side. So if something waits for IO, then it will 317 * wait for the 'master' bio. 318 */ 319 set_bit(R10BIO_Uptodate, &r10_bio->state); 320 raid_end_bio_io(r10_bio); 321 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); 322 } else { 323 /* 324 * oops, read error - keep the refcount on the rdev 325 */ 326 char b[BDEVNAME_SIZE]; 327 printk_ratelimited(KERN_ERR 328 "md/raid10:%s: %s: rescheduling sector %llu\n", 329 mdname(conf->mddev), 330 bdevname(conf->mirrors[dev].rdev->bdev, b), 331 (unsigned long long)r10_bio->sector); 332 set_bit(R10BIO_ReadError, &r10_bio->state); 333 reschedule_retry(r10_bio); 334 } 335 } 336 337 static void close_write(struct r10bio *r10_bio) 338 { 339 /* clear the bitmap if all writes complete successfully */ 340 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, 341 r10_bio->sectors, 342 !test_bit(R10BIO_Degraded, &r10_bio->state), 343 0); 344 md_write_end(r10_bio->mddev); 345 } 346 347 static void one_write_done(struct r10bio *r10_bio) 348 { 349 if (atomic_dec_and_test(&r10_bio->remaining)) { 350 if (test_bit(R10BIO_WriteError, &r10_bio->state)) 351 reschedule_retry(r10_bio); 352 else { 353 close_write(r10_bio); 354 if (test_bit(R10BIO_MadeGood, &r10_bio->state)) 355 reschedule_retry(r10_bio); 356 else 357 raid_end_bio_io(r10_bio); 358 } 359 } 360 } 361 362 static void raid10_end_write_request(struct bio *bio, int error) 363 { 364 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 365 struct r10bio *r10_bio = bio->bi_private; 366 int dev; 367 int dec_rdev = 1; 368 struct r10conf *conf = r10_bio->mddev->private; 369 int slot; 370 371 dev = find_bio_disk(conf, r10_bio, bio, &slot); 372 373 /* 374 * this branch is our 'one mirror IO has finished' event handler: 375 */ 376 if (!uptodate) { 377 set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags); 378 set_bit(R10BIO_WriteError, &r10_bio->state); 379 dec_rdev = 0; 380 } else { 381 /* 382 * Set R10BIO_Uptodate in our master bio, so that 383 * we will return a good error code for to the higher 384 * levels even if IO on some other mirrored buffer fails. 385 * 386 * The 'master' represents the composite IO operation to 387 * user-side. So if something waits for IO, then it will 388 * wait for the 'master' bio. 389 */ 390 sector_t first_bad; 391 int bad_sectors; 392 393 set_bit(R10BIO_Uptodate, &r10_bio->state); 394 395 /* Maybe we can clear some bad blocks. */ 396 if (is_badblock(conf->mirrors[dev].rdev, 397 r10_bio->devs[slot].addr, 398 r10_bio->sectors, 399 &first_bad, &bad_sectors)) { 400 bio_put(bio); 401 r10_bio->devs[slot].bio = IO_MADE_GOOD; 402 dec_rdev = 0; 403 set_bit(R10BIO_MadeGood, &r10_bio->state); 404 } 405 } 406 407 /* 408 * 409 * Let's see if all mirrored write operations have finished 410 * already. 411 */ 412 one_write_done(r10_bio); 413 if (dec_rdev) 414 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); 415 } 416 417 418 /* 419 * RAID10 layout manager 420 * As well as the chunksize and raid_disks count, there are two 421 * parameters: near_copies and far_copies. 422 * near_copies * far_copies must be <= raid_disks. 423 * Normally one of these will be 1. 424 * If both are 1, we get raid0. 425 * If near_copies == raid_disks, we get raid1. 426 * 427 * Chunks are laid out in raid0 style with near_copies copies of the 428 * first chunk, followed by near_copies copies of the next chunk and 429 * so on. 430 * If far_copies > 1, then after 1/far_copies of the array has been assigned 431 * as described above, we start again with a device offset of near_copies. 432 * So we effectively have another copy of the whole array further down all 433 * the drives, but with blocks on different drives. 434 * With this layout, and block is never stored twice on the one device. 435 * 436 * raid10_find_phys finds the sector offset of a given virtual sector 437 * on each device that it is on. 438 * 439 * raid10_find_virt does the reverse mapping, from a device and a 440 * sector offset to a virtual address 441 */ 442 443 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) 444 { 445 int n,f; 446 sector_t sector; 447 sector_t chunk; 448 sector_t stripe; 449 int dev; 450 451 int slot = 0; 452 453 /* now calculate first sector/dev */ 454 chunk = r10bio->sector >> conf->chunk_shift; 455 sector = r10bio->sector & conf->chunk_mask; 456 457 chunk *= conf->near_copies; 458 stripe = chunk; 459 dev = sector_div(stripe, conf->raid_disks); 460 if (conf->far_offset) 461 stripe *= conf->far_copies; 462 463 sector += stripe << conf->chunk_shift; 464 465 /* and calculate all the others */ 466 for (n=0; n < conf->near_copies; n++) { 467 int d = dev; 468 sector_t s = sector; 469 r10bio->devs[slot].addr = sector; 470 r10bio->devs[slot].devnum = d; 471 slot++; 472 473 for (f = 1; f < conf->far_copies; f++) { 474 d += conf->near_copies; 475 if (d >= conf->raid_disks) 476 d -= conf->raid_disks; 477 s += conf->stride; 478 r10bio->devs[slot].devnum = d; 479 r10bio->devs[slot].addr = s; 480 slot++; 481 } 482 dev++; 483 if (dev >= conf->raid_disks) { 484 dev = 0; 485 sector += (conf->chunk_mask + 1); 486 } 487 } 488 BUG_ON(slot != conf->copies); 489 } 490 491 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev) 492 { 493 sector_t offset, chunk, vchunk; 494 495 offset = sector & conf->chunk_mask; 496 if (conf->far_offset) { 497 int fc; 498 chunk = sector >> conf->chunk_shift; 499 fc = sector_div(chunk, conf->far_copies); 500 dev -= fc * conf->near_copies; 501 if (dev < 0) 502 dev += conf->raid_disks; 503 } else { 504 while (sector >= conf->stride) { 505 sector -= conf->stride; 506 if (dev < conf->near_copies) 507 dev += conf->raid_disks - conf->near_copies; 508 else 509 dev -= conf->near_copies; 510 } 511 chunk = sector >> conf->chunk_shift; 512 } 513 vchunk = chunk * conf->raid_disks + dev; 514 sector_div(vchunk, conf->near_copies); 515 return (vchunk << conf->chunk_shift) + offset; 516 } 517 518 /** 519 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged 520 * @q: request queue 521 * @bvm: properties of new bio 522 * @biovec: the request that could be merged to it. 523 * 524 * Return amount of bytes we can accept at this offset 525 * If near_copies == raid_disk, there are no striping issues, 526 * but in that case, the function isn't called at all. 527 */ 528 static int raid10_mergeable_bvec(struct request_queue *q, 529 struct bvec_merge_data *bvm, 530 struct bio_vec *biovec) 531 { 532 struct mddev *mddev = q->queuedata; 533 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); 534 int max; 535 unsigned int chunk_sectors = mddev->chunk_sectors; 536 unsigned int bio_sectors = bvm->bi_size >> 9; 537 538 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; 539 if (max < 0) max = 0; /* bio_add cannot handle a negative return */ 540 if (max <= biovec->bv_len && bio_sectors == 0) 541 return biovec->bv_len; 542 else 543 return max; 544 } 545 546 /* 547 * This routine returns the disk from which the requested read should 548 * be done. There is a per-array 'next expected sequential IO' sector 549 * number - if this matches on the next IO then we use the last disk. 550 * There is also a per-disk 'last know head position' sector that is 551 * maintained from IRQ contexts, both the normal and the resync IO 552 * completion handlers update this position correctly. If there is no 553 * perfect sequential match then we pick the disk whose head is closest. 554 * 555 * If there are 2 mirrors in the same 2 devices, performance degrades 556 * because position is mirror, not device based. 557 * 558 * The rdev for the device selected will have nr_pending incremented. 559 */ 560 561 /* 562 * FIXME: possibly should rethink readbalancing and do it differently 563 * depending on near_copies / far_copies geometry. 564 */ 565 static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_sectors) 566 { 567 const sector_t this_sector = r10_bio->sector; 568 int disk, slot; 569 int sectors = r10_bio->sectors; 570 int best_good_sectors; 571 sector_t new_distance, best_dist; 572 struct md_rdev *rdev; 573 int do_balance; 574 int best_slot; 575 576 raid10_find_phys(conf, r10_bio); 577 rcu_read_lock(); 578 retry: 579 sectors = r10_bio->sectors; 580 best_slot = -1; 581 best_dist = MaxSector; 582 best_good_sectors = 0; 583 do_balance = 1; 584 /* 585 * Check if we can balance. We can balance on the whole 586 * device if no resync is going on (recovery is ok), or below 587 * the resync window. We take the first readable disk when 588 * above the resync window. 589 */ 590 if (conf->mddev->recovery_cp < MaxSector 591 && (this_sector + sectors >= conf->next_resync)) 592 do_balance = 0; 593 594 for (slot = 0; slot < conf->copies ; slot++) { 595 sector_t first_bad; 596 int bad_sectors; 597 sector_t dev_sector; 598 599 if (r10_bio->devs[slot].bio == IO_BLOCKED) 600 continue; 601 disk = r10_bio->devs[slot].devnum; 602 rdev = rcu_dereference(conf->mirrors[disk].rdev); 603 if (rdev == NULL) 604 continue; 605 if (!test_bit(In_sync, &rdev->flags)) 606 continue; 607 608 dev_sector = r10_bio->devs[slot].addr; 609 if (is_badblock(rdev, dev_sector, sectors, 610 &first_bad, &bad_sectors)) { 611 if (best_dist < MaxSector) 612 /* Already have a better slot */ 613 continue; 614 if (first_bad <= dev_sector) { 615 /* Cannot read here. If this is the 616 * 'primary' device, then we must not read 617 * beyond 'bad_sectors' from another device. 618 */ 619 bad_sectors -= (dev_sector - first_bad); 620 if (!do_balance && sectors > bad_sectors) 621 sectors = bad_sectors; 622 if (best_good_sectors > sectors) 623 best_good_sectors = sectors; 624 } else { 625 sector_t good_sectors = 626 first_bad - dev_sector; 627 if (good_sectors > best_good_sectors) { 628 best_good_sectors = good_sectors; 629 best_slot = slot; 630 } 631 if (!do_balance) 632 /* Must read from here */ 633 break; 634 } 635 continue; 636 } else 637 best_good_sectors = sectors; 638 639 if (!do_balance) 640 break; 641 642 /* This optimisation is debatable, and completely destroys 643 * sequential read speed for 'far copies' arrays. So only 644 * keep it for 'near' arrays, and review those later. 645 */ 646 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) 647 break; 648 649 /* for far > 1 always use the lowest address */ 650 if (conf->far_copies > 1) 651 new_distance = r10_bio->devs[slot].addr; 652 else 653 new_distance = abs(r10_bio->devs[slot].addr - 654 conf->mirrors[disk].head_position); 655 if (new_distance < best_dist) { 656 best_dist = new_distance; 657 best_slot = slot; 658 } 659 } 660 if (slot == conf->copies) 661 slot = best_slot; 662 663 if (slot >= 0) { 664 disk = r10_bio->devs[slot].devnum; 665 rdev = rcu_dereference(conf->mirrors[disk].rdev); 666 if (!rdev) 667 goto retry; 668 atomic_inc(&rdev->nr_pending); 669 if (test_bit(Faulty, &rdev->flags)) { 670 /* Cannot risk returning a device that failed 671 * before we inc'ed nr_pending 672 */ 673 rdev_dec_pending(rdev, conf->mddev); 674 goto retry; 675 } 676 r10_bio->read_slot = slot; 677 } else 678 disk = -1; 679 rcu_read_unlock(); 680 *max_sectors = best_good_sectors; 681 682 return disk; 683 } 684 685 static int raid10_congested(void *data, int bits) 686 { 687 struct mddev *mddev = data; 688 struct r10conf *conf = mddev->private; 689 int i, ret = 0; 690 691 if ((bits & (1 << BDI_async_congested)) && 692 conf->pending_count >= max_queued_requests) 693 return 1; 694 695 if (mddev_congested(mddev, bits)) 696 return 1; 697 rcu_read_lock(); 698 for (i = 0; i < conf->raid_disks && ret == 0; i++) { 699 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 700 if (rdev && !test_bit(Faulty, &rdev->flags)) { 701 struct request_queue *q = bdev_get_queue(rdev->bdev); 702 703 ret |= bdi_congested(&q->backing_dev_info, bits); 704 } 705 } 706 rcu_read_unlock(); 707 return ret; 708 } 709 710 static void flush_pending_writes(struct r10conf *conf) 711 { 712 /* Any writes that have been queued but are awaiting 713 * bitmap updates get flushed here. 714 */ 715 spin_lock_irq(&conf->device_lock); 716 717 if (conf->pending_bio_list.head) { 718 struct bio *bio; 719 bio = bio_list_get(&conf->pending_bio_list); 720 conf->pending_count = 0; 721 spin_unlock_irq(&conf->device_lock); 722 /* flush any pending bitmap writes to disk 723 * before proceeding w/ I/O */ 724 bitmap_unplug(conf->mddev->bitmap); 725 wake_up(&conf->wait_barrier); 726 727 while (bio) { /* submit pending writes */ 728 struct bio *next = bio->bi_next; 729 bio->bi_next = NULL; 730 generic_make_request(bio); 731 bio = next; 732 } 733 } else 734 spin_unlock_irq(&conf->device_lock); 735 } 736 737 /* Barriers.... 738 * Sometimes we need to suspend IO while we do something else, 739 * either some resync/recovery, or reconfigure the array. 740 * To do this we raise a 'barrier'. 741 * The 'barrier' is a counter that can be raised multiple times 742 * to count how many activities are happening which preclude 743 * normal IO. 744 * We can only raise the barrier if there is no pending IO. 745 * i.e. if nr_pending == 0. 746 * We choose only to raise the barrier if no-one is waiting for the 747 * barrier to go down. This means that as soon as an IO request 748 * is ready, no other operations which require a barrier will start 749 * until the IO request has had a chance. 750 * 751 * So: regular IO calls 'wait_barrier'. When that returns there 752 * is no backgroup IO happening, It must arrange to call 753 * allow_barrier when it has finished its IO. 754 * backgroup IO calls must call raise_barrier. Once that returns 755 * there is no normal IO happeing. It must arrange to call 756 * lower_barrier when the particular background IO completes. 757 */ 758 759 static void raise_barrier(struct r10conf *conf, int force) 760 { 761 BUG_ON(force && !conf->barrier); 762 spin_lock_irq(&conf->resync_lock); 763 764 /* Wait until no block IO is waiting (unless 'force') */ 765 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, 766 conf->resync_lock, ); 767 768 /* block any new IO from starting */ 769 conf->barrier++; 770 771 /* Now wait for all pending IO to complete */ 772 wait_event_lock_irq(conf->wait_barrier, 773 !conf->nr_pending && conf->barrier < RESYNC_DEPTH, 774 conf->resync_lock, ); 775 776 spin_unlock_irq(&conf->resync_lock); 777 } 778 779 static void lower_barrier(struct r10conf *conf) 780 { 781 unsigned long flags; 782 spin_lock_irqsave(&conf->resync_lock, flags); 783 conf->barrier--; 784 spin_unlock_irqrestore(&conf->resync_lock, flags); 785 wake_up(&conf->wait_barrier); 786 } 787 788 static void wait_barrier(struct r10conf *conf) 789 { 790 spin_lock_irq(&conf->resync_lock); 791 if (conf->barrier) { 792 conf->nr_waiting++; 793 wait_event_lock_irq(conf->wait_barrier, !conf->barrier, 794 conf->resync_lock, 795 ); 796 conf->nr_waiting--; 797 } 798 conf->nr_pending++; 799 spin_unlock_irq(&conf->resync_lock); 800 } 801 802 static void allow_barrier(struct r10conf *conf) 803 { 804 unsigned long flags; 805 spin_lock_irqsave(&conf->resync_lock, flags); 806 conf->nr_pending--; 807 spin_unlock_irqrestore(&conf->resync_lock, flags); 808 wake_up(&conf->wait_barrier); 809 } 810 811 static void freeze_array(struct r10conf *conf) 812 { 813 /* stop syncio and normal IO and wait for everything to 814 * go quiet. 815 * We increment barrier and nr_waiting, and then 816 * wait until nr_pending match nr_queued+1 817 * This is called in the context of one normal IO request 818 * that has failed. Thus any sync request that might be pending 819 * will be blocked by nr_pending, and we need to wait for 820 * pending IO requests to complete or be queued for re-try. 821 * Thus the number queued (nr_queued) plus this request (1) 822 * must match the number of pending IOs (nr_pending) before 823 * we continue. 824 */ 825 spin_lock_irq(&conf->resync_lock); 826 conf->barrier++; 827 conf->nr_waiting++; 828 wait_event_lock_irq(conf->wait_barrier, 829 conf->nr_pending == conf->nr_queued+1, 830 conf->resync_lock, 831 flush_pending_writes(conf)); 832 833 spin_unlock_irq(&conf->resync_lock); 834 } 835 836 static void unfreeze_array(struct r10conf *conf) 837 { 838 /* reverse the effect of the freeze */ 839 spin_lock_irq(&conf->resync_lock); 840 conf->barrier--; 841 conf->nr_waiting--; 842 wake_up(&conf->wait_barrier); 843 spin_unlock_irq(&conf->resync_lock); 844 } 845 846 static int make_request(struct mddev *mddev, struct bio * bio) 847 { 848 struct r10conf *conf = mddev->private; 849 struct mirror_info *mirror; 850 struct r10bio *r10_bio; 851 struct bio *read_bio; 852 int i; 853 int chunk_sects = conf->chunk_mask + 1; 854 const int rw = bio_data_dir(bio); 855 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); 856 const unsigned long do_fua = (bio->bi_rw & REQ_FUA); 857 unsigned long flags; 858 struct md_rdev *blocked_rdev; 859 int plugged; 860 int sectors_handled; 861 int max_sectors; 862 863 if (unlikely(bio->bi_rw & REQ_FLUSH)) { 864 md_flush_request(mddev, bio); 865 return 0; 866 } 867 868 /* If this request crosses a chunk boundary, we need to 869 * split it. This will only happen for 1 PAGE (or less) requests. 870 */ 871 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9) 872 > chunk_sects && 873 conf->near_copies < conf->raid_disks)) { 874 struct bio_pair *bp; 875 /* Sanity check -- queue functions should prevent this happening */ 876 if (bio->bi_vcnt != 1 || 877 bio->bi_idx != 0) 878 goto bad_map; 879 /* This is a one page bio that upper layers 880 * refuse to split for us, so we need to split it. 881 */ 882 bp = bio_split(bio, 883 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); 884 885 /* Each of these 'make_request' calls will call 'wait_barrier'. 886 * If the first succeeds but the second blocks due to the resync 887 * thread raising the barrier, we will deadlock because the 888 * IO to the underlying device will be queued in generic_make_request 889 * and will never complete, so will never reduce nr_pending. 890 * So increment nr_waiting here so no new raise_barriers will 891 * succeed, and so the second wait_barrier cannot block. 892 */ 893 spin_lock_irq(&conf->resync_lock); 894 conf->nr_waiting++; 895 spin_unlock_irq(&conf->resync_lock); 896 897 if (make_request(mddev, &bp->bio1)) 898 generic_make_request(&bp->bio1); 899 if (make_request(mddev, &bp->bio2)) 900 generic_make_request(&bp->bio2); 901 902 spin_lock_irq(&conf->resync_lock); 903 conf->nr_waiting--; 904 wake_up(&conf->wait_barrier); 905 spin_unlock_irq(&conf->resync_lock); 906 907 bio_pair_release(bp); 908 return 0; 909 bad_map: 910 printk("md/raid10:%s: make_request bug: can't convert block across chunks" 911 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2, 912 (unsigned long long)bio->bi_sector, bio->bi_size >> 10); 913 914 bio_io_error(bio); 915 return 0; 916 } 917 918 md_write_start(mddev, bio); 919 920 /* 921 * Register the new request and wait if the reconstruction 922 * thread has put up a bar for new requests. 923 * Continue immediately if no resync is active currently. 924 */ 925 wait_barrier(conf); 926 927 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); 928 929 r10_bio->master_bio = bio; 930 r10_bio->sectors = bio->bi_size >> 9; 931 932 r10_bio->mddev = mddev; 933 r10_bio->sector = bio->bi_sector; 934 r10_bio->state = 0; 935 936 /* We might need to issue multiple reads to different 937 * devices if there are bad blocks around, so we keep 938 * track of the number of reads in bio->bi_phys_segments. 939 * If this is 0, there is only one r10_bio and no locking 940 * will be needed when the request completes. If it is 941 * non-zero, then it is the number of not-completed requests. 942 */ 943 bio->bi_phys_segments = 0; 944 clear_bit(BIO_SEG_VALID, &bio->bi_flags); 945 946 if (rw == READ) { 947 /* 948 * read balancing logic: 949 */ 950 int disk; 951 int slot; 952 953 read_again: 954 disk = read_balance(conf, r10_bio, &max_sectors); 955 slot = r10_bio->read_slot; 956 if (disk < 0) { 957 raid_end_bio_io(r10_bio); 958 return 0; 959 } 960 mirror = conf->mirrors + disk; 961 962 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); 963 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector, 964 max_sectors); 965 966 r10_bio->devs[slot].bio = read_bio; 967 968 read_bio->bi_sector = r10_bio->devs[slot].addr + 969 mirror->rdev->data_offset; 970 read_bio->bi_bdev = mirror->rdev->bdev; 971 read_bio->bi_end_io = raid10_end_read_request; 972 read_bio->bi_rw = READ | do_sync; 973 read_bio->bi_private = r10_bio; 974 975 if (max_sectors < r10_bio->sectors) { 976 /* Could not read all from this device, so we will 977 * need another r10_bio. 978 */ 979 sectors_handled = (r10_bio->sectors + max_sectors 980 - bio->bi_sector); 981 r10_bio->sectors = max_sectors; 982 spin_lock_irq(&conf->device_lock); 983 if (bio->bi_phys_segments == 0) 984 bio->bi_phys_segments = 2; 985 else 986 bio->bi_phys_segments++; 987 spin_unlock(&conf->device_lock); 988 /* Cannot call generic_make_request directly 989 * as that will be queued in __generic_make_request 990 * and subsequent mempool_alloc might block 991 * waiting for it. so hand bio over to raid10d. 992 */ 993 reschedule_retry(r10_bio); 994 995 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); 996 997 r10_bio->master_bio = bio; 998 r10_bio->sectors = ((bio->bi_size >> 9) 999 - sectors_handled); 1000 r10_bio->state = 0; 1001 r10_bio->mddev = mddev; 1002 r10_bio->sector = bio->bi_sector + sectors_handled; 1003 goto read_again; 1004 } else 1005 generic_make_request(read_bio); 1006 return 0; 1007 } 1008 1009 /* 1010 * WRITE: 1011 */ 1012 if (conf->pending_count >= max_queued_requests) { 1013 md_wakeup_thread(mddev->thread); 1014 wait_event(conf->wait_barrier, 1015 conf->pending_count < max_queued_requests); 1016 } 1017 /* first select target devices under rcu_lock and 1018 * inc refcount on their rdev. Record them by setting 1019 * bios[x] to bio 1020 * If there are known/acknowledged bad blocks on any device 1021 * on which we have seen a write error, we want to avoid 1022 * writing to those blocks. This potentially requires several 1023 * writes to write around the bad blocks. Each set of writes 1024 * gets its own r10_bio with a set of bios attached. The number 1025 * of r10_bios is recored in bio->bi_phys_segments just as with 1026 * the read case. 1027 */ 1028 plugged = mddev_check_plugged(mddev); 1029 1030 raid10_find_phys(conf, r10_bio); 1031 retry_write: 1032 blocked_rdev = NULL; 1033 rcu_read_lock(); 1034 max_sectors = r10_bio->sectors; 1035 1036 for (i = 0; i < conf->copies; i++) { 1037 int d = r10_bio->devs[i].devnum; 1038 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); 1039 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { 1040 atomic_inc(&rdev->nr_pending); 1041 blocked_rdev = rdev; 1042 break; 1043 } 1044 r10_bio->devs[i].bio = NULL; 1045 if (!rdev || test_bit(Faulty, &rdev->flags)) { 1046 set_bit(R10BIO_Degraded, &r10_bio->state); 1047 continue; 1048 } 1049 if (test_bit(WriteErrorSeen, &rdev->flags)) { 1050 sector_t first_bad; 1051 sector_t dev_sector = r10_bio->devs[i].addr; 1052 int bad_sectors; 1053 int is_bad; 1054 1055 is_bad = is_badblock(rdev, dev_sector, 1056 max_sectors, 1057 &first_bad, &bad_sectors); 1058 if (is_bad < 0) { 1059 /* Mustn't write here until the bad block 1060 * is acknowledged 1061 */ 1062 atomic_inc(&rdev->nr_pending); 1063 set_bit(BlockedBadBlocks, &rdev->flags); 1064 blocked_rdev = rdev; 1065 break; 1066 } 1067 if (is_bad && first_bad <= dev_sector) { 1068 /* Cannot write here at all */ 1069 bad_sectors -= (dev_sector - first_bad); 1070 if (bad_sectors < max_sectors) 1071 /* Mustn't write more than bad_sectors 1072 * to other devices yet 1073 */ 1074 max_sectors = bad_sectors; 1075 /* We don't set R10BIO_Degraded as that 1076 * only applies if the disk is missing, 1077 * so it might be re-added, and we want to 1078 * know to recover this chunk. 1079 * In this case the device is here, and the 1080 * fact that this chunk is not in-sync is 1081 * recorded in the bad block log. 1082 */ 1083 continue; 1084 } 1085 if (is_bad) { 1086 int good_sectors = first_bad - dev_sector; 1087 if (good_sectors < max_sectors) 1088 max_sectors = good_sectors; 1089 } 1090 } 1091 r10_bio->devs[i].bio = bio; 1092 atomic_inc(&rdev->nr_pending); 1093 } 1094 rcu_read_unlock(); 1095 1096 if (unlikely(blocked_rdev)) { 1097 /* Have to wait for this device to get unblocked, then retry */ 1098 int j; 1099 int d; 1100 1101 for (j = 0; j < i; j++) 1102 if (r10_bio->devs[j].bio) { 1103 d = r10_bio->devs[j].devnum; 1104 rdev_dec_pending(conf->mirrors[d].rdev, mddev); 1105 } 1106 allow_barrier(conf); 1107 md_wait_for_blocked_rdev(blocked_rdev, mddev); 1108 wait_barrier(conf); 1109 goto retry_write; 1110 } 1111 1112 if (max_sectors < r10_bio->sectors) { 1113 /* We are splitting this into multiple parts, so 1114 * we need to prepare for allocating another r10_bio. 1115 */ 1116 r10_bio->sectors = max_sectors; 1117 spin_lock_irq(&conf->device_lock); 1118 if (bio->bi_phys_segments == 0) 1119 bio->bi_phys_segments = 2; 1120 else 1121 bio->bi_phys_segments++; 1122 spin_unlock_irq(&conf->device_lock); 1123 } 1124 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector; 1125 1126 atomic_set(&r10_bio->remaining, 1); 1127 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0); 1128 1129 for (i = 0; i < conf->copies; i++) { 1130 struct bio *mbio; 1131 int d = r10_bio->devs[i].devnum; 1132 if (!r10_bio->devs[i].bio) 1133 continue; 1134 1135 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); 1136 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, 1137 max_sectors); 1138 r10_bio->devs[i].bio = mbio; 1139 1140 mbio->bi_sector = (r10_bio->devs[i].addr+ 1141 conf->mirrors[d].rdev->data_offset); 1142 mbio->bi_bdev = conf->mirrors[d].rdev->bdev; 1143 mbio->bi_end_io = raid10_end_write_request; 1144 mbio->bi_rw = WRITE | do_sync | do_fua; 1145 mbio->bi_private = r10_bio; 1146 1147 atomic_inc(&r10_bio->remaining); 1148 spin_lock_irqsave(&conf->device_lock, flags); 1149 bio_list_add(&conf->pending_bio_list, mbio); 1150 conf->pending_count++; 1151 spin_unlock_irqrestore(&conf->device_lock, flags); 1152 } 1153 1154 /* Don't remove the bias on 'remaining' (one_write_done) until 1155 * after checking if we need to go around again. 1156 */ 1157 1158 if (sectors_handled < (bio->bi_size >> 9)) { 1159 one_write_done(r10_bio); 1160 /* We need another r10_bio. It has already been counted 1161 * in bio->bi_phys_segments. 1162 */ 1163 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); 1164 1165 r10_bio->master_bio = bio; 1166 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled; 1167 1168 r10_bio->mddev = mddev; 1169 r10_bio->sector = bio->bi_sector + sectors_handled; 1170 r10_bio->state = 0; 1171 goto retry_write; 1172 } 1173 one_write_done(r10_bio); 1174 1175 /* In case raid10d snuck in to freeze_array */ 1176 wake_up(&conf->wait_barrier); 1177 1178 if (do_sync || !mddev->bitmap || !plugged) 1179 md_wakeup_thread(mddev->thread); 1180 return 0; 1181 } 1182 1183 static void status(struct seq_file *seq, struct mddev *mddev) 1184 { 1185 struct r10conf *conf = mddev->private; 1186 int i; 1187 1188 if (conf->near_copies < conf->raid_disks) 1189 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); 1190 if (conf->near_copies > 1) 1191 seq_printf(seq, " %d near-copies", conf->near_copies); 1192 if (conf->far_copies > 1) { 1193 if (conf->far_offset) 1194 seq_printf(seq, " %d offset-copies", conf->far_copies); 1195 else 1196 seq_printf(seq, " %d far-copies", conf->far_copies); 1197 } 1198 seq_printf(seq, " [%d/%d] [", conf->raid_disks, 1199 conf->raid_disks - mddev->degraded); 1200 for (i = 0; i < conf->raid_disks; i++) 1201 seq_printf(seq, "%s", 1202 conf->mirrors[i].rdev && 1203 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); 1204 seq_printf(seq, "]"); 1205 } 1206 1207 /* check if there are enough drives for 1208 * every block to appear on atleast one. 1209 * Don't consider the device numbered 'ignore' 1210 * as we might be about to remove it. 1211 */ 1212 static int enough(struct r10conf *conf, int ignore) 1213 { 1214 int first = 0; 1215 1216 do { 1217 int n = conf->copies; 1218 int cnt = 0; 1219 while (n--) { 1220 if (conf->mirrors[first].rdev && 1221 first != ignore) 1222 cnt++; 1223 first = (first+1) % conf->raid_disks; 1224 } 1225 if (cnt == 0) 1226 return 0; 1227 } while (first != 0); 1228 return 1; 1229 } 1230 1231 static void error(struct mddev *mddev, struct md_rdev *rdev) 1232 { 1233 char b[BDEVNAME_SIZE]; 1234 struct r10conf *conf = mddev->private; 1235 1236 /* 1237 * If it is not operational, then we have already marked it as dead 1238 * else if it is the last working disks, ignore the error, let the 1239 * next level up know. 1240 * else mark the drive as failed 1241 */ 1242 if (test_bit(In_sync, &rdev->flags) 1243 && !enough(conf, rdev->raid_disk)) 1244 /* 1245 * Don't fail the drive, just return an IO error. 1246 */ 1247 return; 1248 if (test_and_clear_bit(In_sync, &rdev->flags)) { 1249 unsigned long flags; 1250 spin_lock_irqsave(&conf->device_lock, flags); 1251 mddev->degraded++; 1252 spin_unlock_irqrestore(&conf->device_lock, flags); 1253 /* 1254 * if recovery is running, make sure it aborts. 1255 */ 1256 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 1257 } 1258 set_bit(Blocked, &rdev->flags); 1259 set_bit(Faulty, &rdev->flags); 1260 set_bit(MD_CHANGE_DEVS, &mddev->flags); 1261 printk(KERN_ALERT 1262 "md/raid10:%s: Disk failure on %s, disabling device.\n" 1263 "md/raid10:%s: Operation continuing on %d devices.\n", 1264 mdname(mddev), bdevname(rdev->bdev, b), 1265 mdname(mddev), conf->raid_disks - mddev->degraded); 1266 } 1267 1268 static void print_conf(struct r10conf *conf) 1269 { 1270 int i; 1271 struct mirror_info *tmp; 1272 1273 printk(KERN_DEBUG "RAID10 conf printout:\n"); 1274 if (!conf) { 1275 printk(KERN_DEBUG "(!conf)\n"); 1276 return; 1277 } 1278 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, 1279 conf->raid_disks); 1280 1281 for (i = 0; i < conf->raid_disks; i++) { 1282 char b[BDEVNAME_SIZE]; 1283 tmp = conf->mirrors + i; 1284 if (tmp->rdev) 1285 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", 1286 i, !test_bit(In_sync, &tmp->rdev->flags), 1287 !test_bit(Faulty, &tmp->rdev->flags), 1288 bdevname(tmp->rdev->bdev,b)); 1289 } 1290 } 1291 1292 static void close_sync(struct r10conf *conf) 1293 { 1294 wait_barrier(conf); 1295 allow_barrier(conf); 1296 1297 mempool_destroy(conf->r10buf_pool); 1298 conf->r10buf_pool = NULL; 1299 } 1300 1301 static int raid10_spare_active(struct mddev *mddev) 1302 { 1303 int i; 1304 struct r10conf *conf = mddev->private; 1305 struct mirror_info *tmp; 1306 int count = 0; 1307 unsigned long flags; 1308 1309 /* 1310 * Find all non-in_sync disks within the RAID10 configuration 1311 * and mark them in_sync 1312 */ 1313 for (i = 0; i < conf->raid_disks; i++) { 1314 tmp = conf->mirrors + i; 1315 if (tmp->rdev 1316 && !test_bit(Faulty, &tmp->rdev->flags) 1317 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { 1318 count++; 1319 sysfs_notify_dirent(tmp->rdev->sysfs_state); 1320 } 1321 } 1322 spin_lock_irqsave(&conf->device_lock, flags); 1323 mddev->degraded -= count; 1324 spin_unlock_irqrestore(&conf->device_lock, flags); 1325 1326 print_conf(conf); 1327 return count; 1328 } 1329 1330 1331 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) 1332 { 1333 struct r10conf *conf = mddev->private; 1334 int err = -EEXIST; 1335 int mirror; 1336 int first = 0; 1337 int last = conf->raid_disks - 1; 1338 1339 if (mddev->recovery_cp < MaxSector) 1340 /* only hot-add to in-sync arrays, as recovery is 1341 * very different from resync 1342 */ 1343 return -EBUSY; 1344 if (!enough(conf, -1)) 1345 return -EINVAL; 1346 1347 if (rdev->raid_disk >= 0) 1348 first = last = rdev->raid_disk; 1349 1350 if (rdev->saved_raid_disk >= first && 1351 conf->mirrors[rdev->saved_raid_disk].rdev == NULL) 1352 mirror = rdev->saved_raid_disk; 1353 else 1354 mirror = first; 1355 for ( ; mirror <= last ; mirror++) { 1356 struct mirror_info *p = &conf->mirrors[mirror]; 1357 if (p->recovery_disabled == mddev->recovery_disabled) 1358 continue; 1359 if (!p->rdev) 1360 continue; 1361 1362 disk_stack_limits(mddev->gendisk, rdev->bdev, 1363 rdev->data_offset << 9); 1364 /* as we don't honour merge_bvec_fn, we must 1365 * never risk violating it, so limit 1366 * ->max_segments to one lying with a single 1367 * page, as a one page request is never in 1368 * violation. 1369 */ 1370 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { 1371 blk_queue_max_segments(mddev->queue, 1); 1372 blk_queue_segment_boundary(mddev->queue, 1373 PAGE_CACHE_SIZE - 1); 1374 } 1375 1376 p->head_position = 0; 1377 p->recovery_disabled = mddev->recovery_disabled - 1; 1378 rdev->raid_disk = mirror; 1379 err = 0; 1380 if (rdev->saved_raid_disk != mirror) 1381 conf->fullsync = 1; 1382 rcu_assign_pointer(p->rdev, rdev); 1383 break; 1384 } 1385 1386 md_integrity_add_rdev(rdev, mddev); 1387 print_conf(conf); 1388 return err; 1389 } 1390 1391 static int raid10_remove_disk(struct mddev *mddev, int number) 1392 { 1393 struct r10conf *conf = mddev->private; 1394 int err = 0; 1395 struct md_rdev *rdev; 1396 struct mirror_info *p = conf->mirrors+ number; 1397 1398 print_conf(conf); 1399 rdev = p->rdev; 1400 if (rdev) { 1401 if (test_bit(In_sync, &rdev->flags) || 1402 atomic_read(&rdev->nr_pending)) { 1403 err = -EBUSY; 1404 goto abort; 1405 } 1406 /* Only remove faulty devices in recovery 1407 * is not possible. 1408 */ 1409 if (!test_bit(Faulty, &rdev->flags) && 1410 mddev->recovery_disabled != p->recovery_disabled && 1411 enough(conf, -1)) { 1412 err = -EBUSY; 1413 goto abort; 1414 } 1415 p->rdev = NULL; 1416 synchronize_rcu(); 1417 if (atomic_read(&rdev->nr_pending)) { 1418 /* lost the race, try later */ 1419 err = -EBUSY; 1420 p->rdev = rdev; 1421 goto abort; 1422 } 1423 err = md_integrity_register(mddev); 1424 } 1425 abort: 1426 1427 print_conf(conf); 1428 return err; 1429 } 1430 1431 1432 static void end_sync_read(struct bio *bio, int error) 1433 { 1434 struct r10bio *r10_bio = bio->bi_private; 1435 struct r10conf *conf = r10_bio->mddev->private; 1436 int d; 1437 1438 d = find_bio_disk(conf, r10_bio, bio, NULL); 1439 1440 if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 1441 set_bit(R10BIO_Uptodate, &r10_bio->state); 1442 else 1443 /* The write handler will notice the lack of 1444 * R10BIO_Uptodate and record any errors etc 1445 */ 1446 atomic_add(r10_bio->sectors, 1447 &conf->mirrors[d].rdev->corrected_errors); 1448 1449 /* for reconstruct, we always reschedule after a read. 1450 * for resync, only after all reads 1451 */ 1452 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); 1453 if (test_bit(R10BIO_IsRecover, &r10_bio->state) || 1454 atomic_dec_and_test(&r10_bio->remaining)) { 1455 /* we have read all the blocks, 1456 * do the comparison in process context in raid10d 1457 */ 1458 reschedule_retry(r10_bio); 1459 } 1460 } 1461 1462 static void end_sync_request(struct r10bio *r10_bio) 1463 { 1464 struct mddev *mddev = r10_bio->mddev; 1465 1466 while (atomic_dec_and_test(&r10_bio->remaining)) { 1467 if (r10_bio->master_bio == NULL) { 1468 /* the primary of several recovery bios */ 1469 sector_t s = r10_bio->sectors; 1470 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 1471 test_bit(R10BIO_WriteError, &r10_bio->state)) 1472 reschedule_retry(r10_bio); 1473 else 1474 put_buf(r10_bio); 1475 md_done_sync(mddev, s, 1); 1476 break; 1477 } else { 1478 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; 1479 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 1480 test_bit(R10BIO_WriteError, &r10_bio->state)) 1481 reschedule_retry(r10_bio); 1482 else 1483 put_buf(r10_bio); 1484 r10_bio = r10_bio2; 1485 } 1486 } 1487 } 1488 1489 static void end_sync_write(struct bio *bio, int error) 1490 { 1491 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1492 struct r10bio *r10_bio = bio->bi_private; 1493 struct mddev *mddev = r10_bio->mddev; 1494 struct r10conf *conf = mddev->private; 1495 int d; 1496 sector_t first_bad; 1497 int bad_sectors; 1498 int slot; 1499 1500 d = find_bio_disk(conf, r10_bio, bio, &slot); 1501 1502 if (!uptodate) { 1503 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags); 1504 set_bit(R10BIO_WriteError, &r10_bio->state); 1505 } else if (is_badblock(conf->mirrors[d].rdev, 1506 r10_bio->devs[slot].addr, 1507 r10_bio->sectors, 1508 &first_bad, &bad_sectors)) 1509 set_bit(R10BIO_MadeGood, &r10_bio->state); 1510 1511 rdev_dec_pending(conf->mirrors[d].rdev, mddev); 1512 1513 end_sync_request(r10_bio); 1514 } 1515 1516 /* 1517 * Note: sync and recover and handled very differently for raid10 1518 * This code is for resync. 1519 * For resync, we read through virtual addresses and read all blocks. 1520 * If there is any error, we schedule a write. The lowest numbered 1521 * drive is authoritative. 1522 * However requests come for physical address, so we need to map. 1523 * For every physical address there are raid_disks/copies virtual addresses, 1524 * which is always are least one, but is not necessarly an integer. 1525 * This means that a physical address can span multiple chunks, so we may 1526 * have to submit multiple io requests for a single sync request. 1527 */ 1528 /* 1529 * We check if all blocks are in-sync and only write to blocks that 1530 * aren't in sync 1531 */ 1532 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) 1533 { 1534 struct r10conf *conf = mddev->private; 1535 int i, first; 1536 struct bio *tbio, *fbio; 1537 1538 atomic_set(&r10_bio->remaining, 1); 1539 1540 /* find the first device with a block */ 1541 for (i=0; i<conf->copies; i++) 1542 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) 1543 break; 1544 1545 if (i == conf->copies) 1546 goto done; 1547 1548 first = i; 1549 fbio = r10_bio->devs[i].bio; 1550 1551 /* now find blocks with errors */ 1552 for (i=0 ; i < conf->copies ; i++) { 1553 int j, d; 1554 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); 1555 1556 tbio = r10_bio->devs[i].bio; 1557 1558 if (tbio->bi_end_io != end_sync_read) 1559 continue; 1560 if (i == first) 1561 continue; 1562 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { 1563 /* We know that the bi_io_vec layout is the same for 1564 * both 'first' and 'i', so we just compare them. 1565 * All vec entries are PAGE_SIZE; 1566 */ 1567 for (j = 0; j < vcnt; j++) 1568 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), 1569 page_address(tbio->bi_io_vec[j].bv_page), 1570 PAGE_SIZE)) 1571 break; 1572 if (j == vcnt) 1573 continue; 1574 mddev->resync_mismatches += r10_bio->sectors; 1575 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) 1576 /* Don't fix anything. */ 1577 continue; 1578 } 1579 /* Ok, we need to write this bio, either to correct an 1580 * inconsistency or to correct an unreadable block. 1581 * First we need to fixup bv_offset, bv_len and 1582 * bi_vecs, as the read request might have corrupted these 1583 */ 1584 tbio->bi_vcnt = vcnt; 1585 tbio->bi_size = r10_bio->sectors << 9; 1586 tbio->bi_idx = 0; 1587 tbio->bi_phys_segments = 0; 1588 tbio->bi_flags &= ~(BIO_POOL_MASK - 1); 1589 tbio->bi_flags |= 1 << BIO_UPTODATE; 1590 tbio->bi_next = NULL; 1591 tbio->bi_rw = WRITE; 1592 tbio->bi_private = r10_bio; 1593 tbio->bi_sector = r10_bio->devs[i].addr; 1594 1595 for (j=0; j < vcnt ; j++) { 1596 tbio->bi_io_vec[j].bv_offset = 0; 1597 tbio->bi_io_vec[j].bv_len = PAGE_SIZE; 1598 1599 memcpy(page_address(tbio->bi_io_vec[j].bv_page), 1600 page_address(fbio->bi_io_vec[j].bv_page), 1601 PAGE_SIZE); 1602 } 1603 tbio->bi_end_io = end_sync_write; 1604 1605 d = r10_bio->devs[i].devnum; 1606 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 1607 atomic_inc(&r10_bio->remaining); 1608 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); 1609 1610 tbio->bi_sector += conf->mirrors[d].rdev->data_offset; 1611 tbio->bi_bdev = conf->mirrors[d].rdev->bdev; 1612 generic_make_request(tbio); 1613 } 1614 1615 done: 1616 if (atomic_dec_and_test(&r10_bio->remaining)) { 1617 md_done_sync(mddev, r10_bio->sectors, 1); 1618 put_buf(r10_bio); 1619 } 1620 } 1621 1622 /* 1623 * Now for the recovery code. 1624 * Recovery happens across physical sectors. 1625 * We recover all non-is_sync drives by finding the virtual address of 1626 * each, and then choose a working drive that also has that virt address. 1627 * There is a separate r10_bio for each non-in_sync drive. 1628 * Only the first two slots are in use. The first for reading, 1629 * The second for writing. 1630 * 1631 */ 1632 static void fix_recovery_read_error(struct r10bio *r10_bio) 1633 { 1634 /* We got a read error during recovery. 1635 * We repeat the read in smaller page-sized sections. 1636 * If a read succeeds, write it to the new device or record 1637 * a bad block if we cannot. 1638 * If a read fails, record a bad block on both old and 1639 * new devices. 1640 */ 1641 struct mddev *mddev = r10_bio->mddev; 1642 struct r10conf *conf = mddev->private; 1643 struct bio *bio = r10_bio->devs[0].bio; 1644 sector_t sect = 0; 1645 int sectors = r10_bio->sectors; 1646 int idx = 0; 1647 int dr = r10_bio->devs[0].devnum; 1648 int dw = r10_bio->devs[1].devnum; 1649 1650 while (sectors) { 1651 int s = sectors; 1652 struct md_rdev *rdev; 1653 sector_t addr; 1654 int ok; 1655 1656 if (s > (PAGE_SIZE>>9)) 1657 s = PAGE_SIZE >> 9; 1658 1659 rdev = conf->mirrors[dr].rdev; 1660 addr = r10_bio->devs[0].addr + sect, 1661 ok = sync_page_io(rdev, 1662 addr, 1663 s << 9, 1664 bio->bi_io_vec[idx].bv_page, 1665 READ, false); 1666 if (ok) { 1667 rdev = conf->mirrors[dw].rdev; 1668 addr = r10_bio->devs[1].addr + sect; 1669 ok = sync_page_io(rdev, 1670 addr, 1671 s << 9, 1672 bio->bi_io_vec[idx].bv_page, 1673 WRITE, false); 1674 if (!ok) 1675 set_bit(WriteErrorSeen, &rdev->flags); 1676 } 1677 if (!ok) { 1678 /* We don't worry if we cannot set a bad block - 1679 * it really is bad so there is no loss in not 1680 * recording it yet 1681 */ 1682 rdev_set_badblocks(rdev, addr, s, 0); 1683 1684 if (rdev != conf->mirrors[dw].rdev) { 1685 /* need bad block on destination too */ 1686 struct md_rdev *rdev2 = conf->mirrors[dw].rdev; 1687 addr = r10_bio->devs[1].addr + sect; 1688 ok = rdev_set_badblocks(rdev2, addr, s, 0); 1689 if (!ok) { 1690 /* just abort the recovery */ 1691 printk(KERN_NOTICE 1692 "md/raid10:%s: recovery aborted" 1693 " due to read error\n", 1694 mdname(mddev)); 1695 1696 conf->mirrors[dw].recovery_disabled 1697 = mddev->recovery_disabled; 1698 set_bit(MD_RECOVERY_INTR, 1699 &mddev->recovery); 1700 break; 1701 } 1702 } 1703 } 1704 1705 sectors -= s; 1706 sect += s; 1707 idx++; 1708 } 1709 } 1710 1711 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) 1712 { 1713 struct r10conf *conf = mddev->private; 1714 int d; 1715 struct bio *wbio; 1716 1717 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { 1718 fix_recovery_read_error(r10_bio); 1719 end_sync_request(r10_bio); 1720 return; 1721 } 1722 1723 /* 1724 * share the pages with the first bio 1725 * and submit the write request 1726 */ 1727 wbio = r10_bio->devs[1].bio; 1728 d = r10_bio->devs[1].devnum; 1729 1730 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 1731 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); 1732 generic_make_request(wbio); 1733 } 1734 1735 1736 /* 1737 * Used by fix_read_error() to decay the per rdev read_errors. 1738 * We halve the read error count for every hour that has elapsed 1739 * since the last recorded read error. 1740 * 1741 */ 1742 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev) 1743 { 1744 struct timespec cur_time_mon; 1745 unsigned long hours_since_last; 1746 unsigned int read_errors = atomic_read(&rdev->read_errors); 1747 1748 ktime_get_ts(&cur_time_mon); 1749 1750 if (rdev->last_read_error.tv_sec == 0 && 1751 rdev->last_read_error.tv_nsec == 0) { 1752 /* first time we've seen a read error */ 1753 rdev->last_read_error = cur_time_mon; 1754 return; 1755 } 1756 1757 hours_since_last = (cur_time_mon.tv_sec - 1758 rdev->last_read_error.tv_sec) / 3600; 1759 1760 rdev->last_read_error = cur_time_mon; 1761 1762 /* 1763 * if hours_since_last is > the number of bits in read_errors 1764 * just set read errors to 0. We do this to avoid 1765 * overflowing the shift of read_errors by hours_since_last. 1766 */ 1767 if (hours_since_last >= 8 * sizeof(read_errors)) 1768 atomic_set(&rdev->read_errors, 0); 1769 else 1770 atomic_set(&rdev->read_errors, read_errors >> hours_since_last); 1771 } 1772 1773 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, 1774 int sectors, struct page *page, int rw) 1775 { 1776 sector_t first_bad; 1777 int bad_sectors; 1778 1779 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors) 1780 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags))) 1781 return -1; 1782 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) 1783 /* success */ 1784 return 1; 1785 if (rw == WRITE) 1786 set_bit(WriteErrorSeen, &rdev->flags); 1787 /* need to record an error - either for the block or the device */ 1788 if (!rdev_set_badblocks(rdev, sector, sectors, 0)) 1789 md_error(rdev->mddev, rdev); 1790 return 0; 1791 } 1792 1793 /* 1794 * This is a kernel thread which: 1795 * 1796 * 1. Retries failed read operations on working mirrors. 1797 * 2. Updates the raid superblock when problems encounter. 1798 * 3. Performs writes following reads for array synchronising. 1799 */ 1800 1801 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio) 1802 { 1803 int sect = 0; /* Offset from r10_bio->sector */ 1804 int sectors = r10_bio->sectors; 1805 struct md_rdev*rdev; 1806 int max_read_errors = atomic_read(&mddev->max_corr_read_errors); 1807 int d = r10_bio->devs[r10_bio->read_slot].devnum; 1808 1809 /* still own a reference to this rdev, so it cannot 1810 * have been cleared recently. 1811 */ 1812 rdev = conf->mirrors[d].rdev; 1813 1814 if (test_bit(Faulty, &rdev->flags)) 1815 /* drive has already been failed, just ignore any 1816 more fix_read_error() attempts */ 1817 return; 1818 1819 check_decay_read_errors(mddev, rdev); 1820 atomic_inc(&rdev->read_errors); 1821 if (atomic_read(&rdev->read_errors) > max_read_errors) { 1822 char b[BDEVNAME_SIZE]; 1823 bdevname(rdev->bdev, b); 1824 1825 printk(KERN_NOTICE 1826 "md/raid10:%s: %s: Raid device exceeded " 1827 "read_error threshold [cur %d:max %d]\n", 1828 mdname(mddev), b, 1829 atomic_read(&rdev->read_errors), max_read_errors); 1830 printk(KERN_NOTICE 1831 "md/raid10:%s: %s: Failing raid device\n", 1832 mdname(mddev), b); 1833 md_error(mddev, conf->mirrors[d].rdev); 1834 return; 1835 } 1836 1837 while(sectors) { 1838 int s = sectors; 1839 int sl = r10_bio->read_slot; 1840 int success = 0; 1841 int start; 1842 1843 if (s > (PAGE_SIZE>>9)) 1844 s = PAGE_SIZE >> 9; 1845 1846 rcu_read_lock(); 1847 do { 1848 sector_t first_bad; 1849 int bad_sectors; 1850 1851 d = r10_bio->devs[sl].devnum; 1852 rdev = rcu_dereference(conf->mirrors[d].rdev); 1853 if (rdev && 1854 test_bit(In_sync, &rdev->flags) && 1855 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s, 1856 &first_bad, &bad_sectors) == 0) { 1857 atomic_inc(&rdev->nr_pending); 1858 rcu_read_unlock(); 1859 success = sync_page_io(rdev, 1860 r10_bio->devs[sl].addr + 1861 sect, 1862 s<<9, 1863 conf->tmppage, READ, false); 1864 rdev_dec_pending(rdev, mddev); 1865 rcu_read_lock(); 1866 if (success) 1867 break; 1868 } 1869 sl++; 1870 if (sl == conf->copies) 1871 sl = 0; 1872 } while (!success && sl != r10_bio->read_slot); 1873 rcu_read_unlock(); 1874 1875 if (!success) { 1876 /* Cannot read from anywhere, just mark the block 1877 * as bad on the first device to discourage future 1878 * reads. 1879 */ 1880 int dn = r10_bio->devs[r10_bio->read_slot].devnum; 1881 rdev = conf->mirrors[dn].rdev; 1882 1883 if (!rdev_set_badblocks( 1884 rdev, 1885 r10_bio->devs[r10_bio->read_slot].addr 1886 + sect, 1887 s, 0)) 1888 md_error(mddev, rdev); 1889 break; 1890 } 1891 1892 start = sl; 1893 /* write it back and re-read */ 1894 rcu_read_lock(); 1895 while (sl != r10_bio->read_slot) { 1896 char b[BDEVNAME_SIZE]; 1897 1898 if (sl==0) 1899 sl = conf->copies; 1900 sl--; 1901 d = r10_bio->devs[sl].devnum; 1902 rdev = rcu_dereference(conf->mirrors[d].rdev); 1903 if (!rdev || 1904 !test_bit(In_sync, &rdev->flags)) 1905 continue; 1906 1907 atomic_inc(&rdev->nr_pending); 1908 rcu_read_unlock(); 1909 if (r10_sync_page_io(rdev, 1910 r10_bio->devs[sl].addr + 1911 sect, 1912 s<<9, conf->tmppage, WRITE) 1913 == 0) { 1914 /* Well, this device is dead */ 1915 printk(KERN_NOTICE 1916 "md/raid10:%s: read correction " 1917 "write failed" 1918 " (%d sectors at %llu on %s)\n", 1919 mdname(mddev), s, 1920 (unsigned long long)( 1921 sect + rdev->data_offset), 1922 bdevname(rdev->bdev, b)); 1923 printk(KERN_NOTICE "md/raid10:%s: %s: failing " 1924 "drive\n", 1925 mdname(mddev), 1926 bdevname(rdev->bdev, b)); 1927 } 1928 rdev_dec_pending(rdev, mddev); 1929 rcu_read_lock(); 1930 } 1931 sl = start; 1932 while (sl != r10_bio->read_slot) { 1933 char b[BDEVNAME_SIZE]; 1934 1935 if (sl==0) 1936 sl = conf->copies; 1937 sl--; 1938 d = r10_bio->devs[sl].devnum; 1939 rdev = rcu_dereference(conf->mirrors[d].rdev); 1940 if (!rdev || 1941 !test_bit(In_sync, &rdev->flags)) 1942 continue; 1943 1944 atomic_inc(&rdev->nr_pending); 1945 rcu_read_unlock(); 1946 switch (r10_sync_page_io(rdev, 1947 r10_bio->devs[sl].addr + 1948 sect, 1949 s<<9, conf->tmppage, 1950 READ)) { 1951 case 0: 1952 /* Well, this device is dead */ 1953 printk(KERN_NOTICE 1954 "md/raid10:%s: unable to read back " 1955 "corrected sectors" 1956 " (%d sectors at %llu on %s)\n", 1957 mdname(mddev), s, 1958 (unsigned long long)( 1959 sect + rdev->data_offset), 1960 bdevname(rdev->bdev, b)); 1961 printk(KERN_NOTICE "md/raid10:%s: %s: failing " 1962 "drive\n", 1963 mdname(mddev), 1964 bdevname(rdev->bdev, b)); 1965 break; 1966 case 1: 1967 printk(KERN_INFO 1968 "md/raid10:%s: read error corrected" 1969 " (%d sectors at %llu on %s)\n", 1970 mdname(mddev), s, 1971 (unsigned long long)( 1972 sect + rdev->data_offset), 1973 bdevname(rdev->bdev, b)); 1974 atomic_add(s, &rdev->corrected_errors); 1975 } 1976 1977 rdev_dec_pending(rdev, mddev); 1978 rcu_read_lock(); 1979 } 1980 rcu_read_unlock(); 1981 1982 sectors -= s; 1983 sect += s; 1984 } 1985 } 1986 1987 static void bi_complete(struct bio *bio, int error) 1988 { 1989 complete((struct completion *)bio->bi_private); 1990 } 1991 1992 static int submit_bio_wait(int rw, struct bio *bio) 1993 { 1994 struct completion event; 1995 rw |= REQ_SYNC; 1996 1997 init_completion(&event); 1998 bio->bi_private = &event; 1999 bio->bi_end_io = bi_complete; 2000 submit_bio(rw, bio); 2001 wait_for_completion(&event); 2002 2003 return test_bit(BIO_UPTODATE, &bio->bi_flags); 2004 } 2005 2006 static int narrow_write_error(struct r10bio *r10_bio, int i) 2007 { 2008 struct bio *bio = r10_bio->master_bio; 2009 struct mddev *mddev = r10_bio->mddev; 2010 struct r10conf *conf = mddev->private; 2011 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; 2012 /* bio has the data to be written to slot 'i' where 2013 * we just recently had a write error. 2014 * We repeatedly clone the bio and trim down to one block, 2015 * then try the write. Where the write fails we record 2016 * a bad block. 2017 * It is conceivable that the bio doesn't exactly align with 2018 * blocks. We must handle this. 2019 * 2020 * We currently own a reference to the rdev. 2021 */ 2022 2023 int block_sectors; 2024 sector_t sector; 2025 int sectors; 2026 int sect_to_write = r10_bio->sectors; 2027 int ok = 1; 2028 2029 if (rdev->badblocks.shift < 0) 2030 return 0; 2031 2032 block_sectors = 1 << rdev->badblocks.shift; 2033 sector = r10_bio->sector; 2034 sectors = ((r10_bio->sector + block_sectors) 2035 & ~(sector_t)(block_sectors - 1)) 2036 - sector; 2037 2038 while (sect_to_write) { 2039 struct bio *wbio; 2040 if (sectors > sect_to_write) 2041 sectors = sect_to_write; 2042 /* Write at 'sector' for 'sectors' */ 2043 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev); 2044 md_trim_bio(wbio, sector - bio->bi_sector, sectors); 2045 wbio->bi_sector = (r10_bio->devs[i].addr+ 2046 rdev->data_offset+ 2047 (sector - r10_bio->sector)); 2048 wbio->bi_bdev = rdev->bdev; 2049 if (submit_bio_wait(WRITE, wbio) == 0) 2050 /* Failure! */ 2051 ok = rdev_set_badblocks(rdev, sector, 2052 sectors, 0) 2053 && ok; 2054 2055 bio_put(wbio); 2056 sect_to_write -= sectors; 2057 sector += sectors; 2058 sectors = block_sectors; 2059 } 2060 return ok; 2061 } 2062 2063 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) 2064 { 2065 int slot = r10_bio->read_slot; 2066 int mirror = r10_bio->devs[slot].devnum; 2067 struct bio *bio; 2068 struct r10conf *conf = mddev->private; 2069 struct md_rdev *rdev; 2070 char b[BDEVNAME_SIZE]; 2071 unsigned long do_sync; 2072 int max_sectors; 2073 2074 /* we got a read error. Maybe the drive is bad. Maybe just 2075 * the block and we can fix it. 2076 * We freeze all other IO, and try reading the block from 2077 * other devices. When we find one, we re-write 2078 * and check it that fixes the read error. 2079 * This is all done synchronously while the array is 2080 * frozen. 2081 */ 2082 if (mddev->ro == 0) { 2083 freeze_array(conf); 2084 fix_read_error(conf, mddev, r10_bio); 2085 unfreeze_array(conf); 2086 } 2087 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev); 2088 2089 bio = r10_bio->devs[slot].bio; 2090 bdevname(bio->bi_bdev, b); 2091 r10_bio->devs[slot].bio = 2092 mddev->ro ? IO_BLOCKED : NULL; 2093 read_more: 2094 mirror = read_balance(conf, r10_bio, &max_sectors); 2095 if (mirror == -1) { 2096 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O" 2097 " read error for block %llu\n", 2098 mdname(mddev), b, 2099 (unsigned long long)r10_bio->sector); 2100 raid_end_bio_io(r10_bio); 2101 bio_put(bio); 2102 return; 2103 } 2104 2105 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC); 2106 if (bio) 2107 bio_put(bio); 2108 slot = r10_bio->read_slot; 2109 rdev = conf->mirrors[mirror].rdev; 2110 printk_ratelimited( 2111 KERN_ERR 2112 "md/raid10:%s: %s: redirecting" 2113 "sector %llu to another mirror\n", 2114 mdname(mddev), 2115 bdevname(rdev->bdev, b), 2116 (unsigned long long)r10_bio->sector); 2117 bio = bio_clone_mddev(r10_bio->master_bio, 2118 GFP_NOIO, mddev); 2119 md_trim_bio(bio, 2120 r10_bio->sector - bio->bi_sector, 2121 max_sectors); 2122 r10_bio->devs[slot].bio = bio; 2123 bio->bi_sector = r10_bio->devs[slot].addr 2124 + rdev->data_offset; 2125 bio->bi_bdev = rdev->bdev; 2126 bio->bi_rw = READ | do_sync; 2127 bio->bi_private = r10_bio; 2128 bio->bi_end_io = raid10_end_read_request; 2129 if (max_sectors < r10_bio->sectors) { 2130 /* Drat - have to split this up more */ 2131 struct bio *mbio = r10_bio->master_bio; 2132 int sectors_handled = 2133 r10_bio->sector + max_sectors 2134 - mbio->bi_sector; 2135 r10_bio->sectors = max_sectors; 2136 spin_lock_irq(&conf->device_lock); 2137 if (mbio->bi_phys_segments == 0) 2138 mbio->bi_phys_segments = 2; 2139 else 2140 mbio->bi_phys_segments++; 2141 spin_unlock_irq(&conf->device_lock); 2142 generic_make_request(bio); 2143 bio = NULL; 2144 2145 r10_bio = mempool_alloc(conf->r10bio_pool, 2146 GFP_NOIO); 2147 r10_bio->master_bio = mbio; 2148 r10_bio->sectors = (mbio->bi_size >> 9) 2149 - sectors_handled; 2150 r10_bio->state = 0; 2151 set_bit(R10BIO_ReadError, 2152 &r10_bio->state); 2153 r10_bio->mddev = mddev; 2154 r10_bio->sector = mbio->bi_sector 2155 + sectors_handled; 2156 2157 goto read_more; 2158 } else 2159 generic_make_request(bio); 2160 } 2161 2162 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) 2163 { 2164 /* Some sort of write request has finished and it 2165 * succeeded in writing where we thought there was a 2166 * bad block. So forget the bad block. 2167 * Or possibly if failed and we need to record 2168 * a bad block. 2169 */ 2170 int m; 2171 struct md_rdev *rdev; 2172 2173 if (test_bit(R10BIO_IsSync, &r10_bio->state) || 2174 test_bit(R10BIO_IsRecover, &r10_bio->state)) { 2175 for (m = 0; m < conf->copies; m++) { 2176 int dev = r10_bio->devs[m].devnum; 2177 rdev = conf->mirrors[dev].rdev; 2178 if (r10_bio->devs[m].bio == NULL) 2179 continue; 2180 if (test_bit(BIO_UPTODATE, 2181 &r10_bio->devs[m].bio->bi_flags)) { 2182 rdev_clear_badblocks( 2183 rdev, 2184 r10_bio->devs[m].addr, 2185 r10_bio->sectors); 2186 } else { 2187 if (!rdev_set_badblocks( 2188 rdev, 2189 r10_bio->devs[m].addr, 2190 r10_bio->sectors, 0)) 2191 md_error(conf->mddev, rdev); 2192 } 2193 } 2194 put_buf(r10_bio); 2195 } else { 2196 for (m = 0; m < conf->copies; m++) { 2197 int dev = r10_bio->devs[m].devnum; 2198 struct bio *bio = r10_bio->devs[m].bio; 2199 rdev = conf->mirrors[dev].rdev; 2200 if (bio == IO_MADE_GOOD) { 2201 rdev_clear_badblocks( 2202 rdev, 2203 r10_bio->devs[m].addr, 2204 r10_bio->sectors); 2205 rdev_dec_pending(rdev, conf->mddev); 2206 } else if (bio != NULL && 2207 !test_bit(BIO_UPTODATE, &bio->bi_flags)) { 2208 if (!narrow_write_error(r10_bio, m)) { 2209 md_error(conf->mddev, rdev); 2210 set_bit(R10BIO_Degraded, 2211 &r10_bio->state); 2212 } 2213 rdev_dec_pending(rdev, conf->mddev); 2214 } 2215 } 2216 if (test_bit(R10BIO_WriteError, 2217 &r10_bio->state)) 2218 close_write(r10_bio); 2219 raid_end_bio_io(r10_bio); 2220 } 2221 } 2222 2223 static void raid10d(struct mddev *mddev) 2224 { 2225 struct r10bio *r10_bio; 2226 unsigned long flags; 2227 struct r10conf *conf = mddev->private; 2228 struct list_head *head = &conf->retry_list; 2229 struct blk_plug plug; 2230 2231 md_check_recovery(mddev); 2232 2233 blk_start_plug(&plug); 2234 for (;;) { 2235 2236 flush_pending_writes(conf); 2237 2238 spin_lock_irqsave(&conf->device_lock, flags); 2239 if (list_empty(head)) { 2240 spin_unlock_irqrestore(&conf->device_lock, flags); 2241 break; 2242 } 2243 r10_bio = list_entry(head->prev, struct r10bio, retry_list); 2244 list_del(head->prev); 2245 conf->nr_queued--; 2246 spin_unlock_irqrestore(&conf->device_lock, flags); 2247 2248 mddev = r10_bio->mddev; 2249 conf = mddev->private; 2250 if (test_bit(R10BIO_MadeGood, &r10_bio->state) || 2251 test_bit(R10BIO_WriteError, &r10_bio->state)) 2252 handle_write_completed(conf, r10_bio); 2253 else if (test_bit(R10BIO_IsSync, &r10_bio->state)) 2254 sync_request_write(mddev, r10_bio); 2255 else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) 2256 recovery_request_write(mddev, r10_bio); 2257 else if (test_bit(R10BIO_ReadError, &r10_bio->state)) 2258 handle_read_error(mddev, r10_bio); 2259 else { 2260 /* just a partial read to be scheduled from a 2261 * separate context 2262 */ 2263 int slot = r10_bio->read_slot; 2264 generic_make_request(r10_bio->devs[slot].bio); 2265 } 2266 2267 cond_resched(); 2268 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) 2269 md_check_recovery(mddev); 2270 } 2271 blk_finish_plug(&plug); 2272 } 2273 2274 2275 static int init_resync(struct r10conf *conf) 2276 { 2277 int buffs; 2278 2279 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; 2280 BUG_ON(conf->r10buf_pool); 2281 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); 2282 if (!conf->r10buf_pool) 2283 return -ENOMEM; 2284 conf->next_resync = 0; 2285 return 0; 2286 } 2287 2288 /* 2289 * perform a "sync" on one "block" 2290 * 2291 * We need to make sure that no normal I/O request - particularly write 2292 * requests - conflict with active sync requests. 2293 * 2294 * This is achieved by tracking pending requests and a 'barrier' concept 2295 * that can be installed to exclude normal IO requests. 2296 * 2297 * Resync and recovery are handled very differently. 2298 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. 2299 * 2300 * For resync, we iterate over virtual addresses, read all copies, 2301 * and update if there are differences. If only one copy is live, 2302 * skip it. 2303 * For recovery, we iterate over physical addresses, read a good 2304 * value for each non-in_sync drive, and over-write. 2305 * 2306 * So, for recovery we may have several outstanding complex requests for a 2307 * given address, one for each out-of-sync device. We model this by allocating 2308 * a number of r10_bio structures, one for each out-of-sync device. 2309 * As we setup these structures, we collect all bio's together into a list 2310 * which we then process collectively to add pages, and then process again 2311 * to pass to generic_make_request. 2312 * 2313 * The r10_bio structures are linked using a borrowed master_bio pointer. 2314 * This link is counted in ->remaining. When the r10_bio that points to NULL 2315 * has its remaining count decremented to 0, the whole complex operation 2316 * is complete. 2317 * 2318 */ 2319 2320 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, 2321 int *skipped, int go_faster) 2322 { 2323 struct r10conf *conf = mddev->private; 2324 struct r10bio *r10_bio; 2325 struct bio *biolist = NULL, *bio; 2326 sector_t max_sector, nr_sectors; 2327 int i; 2328 int max_sync; 2329 sector_t sync_blocks; 2330 sector_t sectors_skipped = 0; 2331 int chunks_skipped = 0; 2332 2333 if (!conf->r10buf_pool) 2334 if (init_resync(conf)) 2335 return 0; 2336 2337 skipped: 2338 max_sector = mddev->dev_sectors; 2339 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 2340 max_sector = mddev->resync_max_sectors; 2341 if (sector_nr >= max_sector) { 2342 /* If we aborted, we need to abort the 2343 * sync on the 'current' bitmap chucks (there can 2344 * be several when recovering multiple devices). 2345 * as we may have started syncing it but not finished. 2346 * We can find the current address in 2347 * mddev->curr_resync, but for recovery, 2348 * we need to convert that to several 2349 * virtual addresses. 2350 */ 2351 if (mddev->curr_resync < max_sector) { /* aborted */ 2352 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) 2353 bitmap_end_sync(mddev->bitmap, mddev->curr_resync, 2354 &sync_blocks, 1); 2355 else for (i=0; i<conf->raid_disks; i++) { 2356 sector_t sect = 2357 raid10_find_virt(conf, mddev->curr_resync, i); 2358 bitmap_end_sync(mddev->bitmap, sect, 2359 &sync_blocks, 1); 2360 } 2361 } else /* completed sync */ 2362 conf->fullsync = 0; 2363 2364 bitmap_close_sync(mddev->bitmap); 2365 close_sync(conf); 2366 *skipped = 1; 2367 return sectors_skipped; 2368 } 2369 if (chunks_skipped >= conf->raid_disks) { 2370 /* if there has been nothing to do on any drive, 2371 * then there is nothing to do at all.. 2372 */ 2373 *skipped = 1; 2374 return (max_sector - sector_nr) + sectors_skipped; 2375 } 2376 2377 if (max_sector > mddev->resync_max) 2378 max_sector = mddev->resync_max; /* Don't do IO beyond here */ 2379 2380 /* make sure whole request will fit in a chunk - if chunks 2381 * are meaningful 2382 */ 2383 if (conf->near_copies < conf->raid_disks && 2384 max_sector > (sector_nr | conf->chunk_mask)) 2385 max_sector = (sector_nr | conf->chunk_mask) + 1; 2386 /* 2387 * If there is non-resync activity waiting for us then 2388 * put in a delay to throttle resync. 2389 */ 2390 if (!go_faster && conf->nr_waiting) 2391 msleep_interruptible(1000); 2392 2393 /* Again, very different code for resync and recovery. 2394 * Both must result in an r10bio with a list of bios that 2395 * have bi_end_io, bi_sector, bi_bdev set, 2396 * and bi_private set to the r10bio. 2397 * For recovery, we may actually create several r10bios 2398 * with 2 bios in each, that correspond to the bios in the main one. 2399 * In this case, the subordinate r10bios link back through a 2400 * borrowed master_bio pointer, and the counter in the master 2401 * includes a ref from each subordinate. 2402 */ 2403 /* First, we decide what to do and set ->bi_end_io 2404 * To end_sync_read if we want to read, and 2405 * end_sync_write if we will want to write. 2406 */ 2407 2408 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); 2409 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 2410 /* recovery... the complicated one */ 2411 int j; 2412 r10_bio = NULL; 2413 2414 for (i=0 ; i<conf->raid_disks; i++) { 2415 int still_degraded; 2416 struct r10bio *rb2; 2417 sector_t sect; 2418 int must_sync; 2419 int any_working; 2420 2421 if (conf->mirrors[i].rdev == NULL || 2422 test_bit(In_sync, &conf->mirrors[i].rdev->flags)) 2423 continue; 2424 2425 still_degraded = 0; 2426 /* want to reconstruct this device */ 2427 rb2 = r10_bio; 2428 sect = raid10_find_virt(conf, sector_nr, i); 2429 /* Unless we are doing a full sync, we only need 2430 * to recover the block if it is set in the bitmap 2431 */ 2432 must_sync = bitmap_start_sync(mddev->bitmap, sect, 2433 &sync_blocks, 1); 2434 if (sync_blocks < max_sync) 2435 max_sync = sync_blocks; 2436 if (!must_sync && 2437 !conf->fullsync) { 2438 /* yep, skip the sync_blocks here, but don't assume 2439 * that there will never be anything to do here 2440 */ 2441 chunks_skipped = -1; 2442 continue; 2443 } 2444 2445 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); 2446 raise_barrier(conf, rb2 != NULL); 2447 atomic_set(&r10_bio->remaining, 0); 2448 2449 r10_bio->master_bio = (struct bio*)rb2; 2450 if (rb2) 2451 atomic_inc(&rb2->remaining); 2452 r10_bio->mddev = mddev; 2453 set_bit(R10BIO_IsRecover, &r10_bio->state); 2454 r10_bio->sector = sect; 2455 2456 raid10_find_phys(conf, r10_bio); 2457 2458 /* Need to check if the array will still be 2459 * degraded 2460 */ 2461 for (j=0; j<conf->raid_disks; j++) 2462 if (conf->mirrors[j].rdev == NULL || 2463 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) { 2464 still_degraded = 1; 2465 break; 2466 } 2467 2468 must_sync = bitmap_start_sync(mddev->bitmap, sect, 2469 &sync_blocks, still_degraded); 2470 2471 any_working = 0; 2472 for (j=0; j<conf->copies;j++) { 2473 int k; 2474 int d = r10_bio->devs[j].devnum; 2475 sector_t from_addr, to_addr; 2476 struct md_rdev *rdev; 2477 sector_t sector, first_bad; 2478 int bad_sectors; 2479 if (!conf->mirrors[d].rdev || 2480 !test_bit(In_sync, &conf->mirrors[d].rdev->flags)) 2481 continue; 2482 /* This is where we read from */ 2483 any_working = 1; 2484 rdev = conf->mirrors[d].rdev; 2485 sector = r10_bio->devs[j].addr; 2486 2487 if (is_badblock(rdev, sector, max_sync, 2488 &first_bad, &bad_sectors)) { 2489 if (first_bad > sector) 2490 max_sync = first_bad - sector; 2491 else { 2492 bad_sectors -= (sector 2493 - first_bad); 2494 if (max_sync > bad_sectors) 2495 max_sync = bad_sectors; 2496 continue; 2497 } 2498 } 2499 bio = r10_bio->devs[0].bio; 2500 bio->bi_next = biolist; 2501 biolist = bio; 2502 bio->bi_private = r10_bio; 2503 bio->bi_end_io = end_sync_read; 2504 bio->bi_rw = READ; 2505 from_addr = r10_bio->devs[j].addr; 2506 bio->bi_sector = from_addr + 2507 conf->mirrors[d].rdev->data_offset; 2508 bio->bi_bdev = conf->mirrors[d].rdev->bdev; 2509 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 2510 atomic_inc(&r10_bio->remaining); 2511 /* and we write to 'i' */ 2512 2513 for (k=0; k<conf->copies; k++) 2514 if (r10_bio->devs[k].devnum == i) 2515 break; 2516 BUG_ON(k == conf->copies); 2517 bio = r10_bio->devs[1].bio; 2518 bio->bi_next = biolist; 2519 biolist = bio; 2520 bio->bi_private = r10_bio; 2521 bio->bi_end_io = end_sync_write; 2522 bio->bi_rw = WRITE; 2523 to_addr = r10_bio->devs[k].addr; 2524 bio->bi_sector = to_addr + 2525 conf->mirrors[i].rdev->data_offset; 2526 bio->bi_bdev = conf->mirrors[i].rdev->bdev; 2527 2528 r10_bio->devs[0].devnum = d; 2529 r10_bio->devs[0].addr = from_addr; 2530 r10_bio->devs[1].devnum = i; 2531 r10_bio->devs[1].addr = to_addr; 2532 2533 break; 2534 } 2535 if (j == conf->copies) { 2536 /* Cannot recover, so abort the recovery or 2537 * record a bad block */ 2538 put_buf(r10_bio); 2539 if (rb2) 2540 atomic_dec(&rb2->remaining); 2541 r10_bio = rb2; 2542 if (any_working) { 2543 /* problem is that there are bad blocks 2544 * on other device(s) 2545 */ 2546 int k; 2547 for (k = 0; k < conf->copies; k++) 2548 if (r10_bio->devs[k].devnum == i) 2549 break; 2550 if (!rdev_set_badblocks( 2551 conf->mirrors[i].rdev, 2552 r10_bio->devs[k].addr, 2553 max_sync, 0)) 2554 any_working = 0; 2555 } 2556 if (!any_working) { 2557 if (!test_and_set_bit(MD_RECOVERY_INTR, 2558 &mddev->recovery)) 2559 printk(KERN_INFO "md/raid10:%s: insufficient " 2560 "working devices for recovery.\n", 2561 mdname(mddev)); 2562 conf->mirrors[i].recovery_disabled 2563 = mddev->recovery_disabled; 2564 } 2565 break; 2566 } 2567 } 2568 if (biolist == NULL) { 2569 while (r10_bio) { 2570 struct r10bio *rb2 = r10_bio; 2571 r10_bio = (struct r10bio*) rb2->master_bio; 2572 rb2->master_bio = NULL; 2573 put_buf(rb2); 2574 } 2575 goto giveup; 2576 } 2577 } else { 2578 /* resync. Schedule a read for every block at this virt offset */ 2579 int count = 0; 2580 2581 bitmap_cond_end_sync(mddev->bitmap, sector_nr); 2582 2583 if (!bitmap_start_sync(mddev->bitmap, sector_nr, 2584 &sync_blocks, mddev->degraded) && 2585 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, 2586 &mddev->recovery)) { 2587 /* We can skip this block */ 2588 *skipped = 1; 2589 return sync_blocks + sectors_skipped; 2590 } 2591 if (sync_blocks < max_sync) 2592 max_sync = sync_blocks; 2593 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); 2594 2595 r10_bio->mddev = mddev; 2596 atomic_set(&r10_bio->remaining, 0); 2597 raise_barrier(conf, 0); 2598 conf->next_resync = sector_nr; 2599 2600 r10_bio->master_bio = NULL; 2601 r10_bio->sector = sector_nr; 2602 set_bit(R10BIO_IsSync, &r10_bio->state); 2603 raid10_find_phys(conf, r10_bio); 2604 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1; 2605 2606 for (i=0; i<conf->copies; i++) { 2607 int d = r10_bio->devs[i].devnum; 2608 sector_t first_bad, sector; 2609 int bad_sectors; 2610 2611 bio = r10_bio->devs[i].bio; 2612 bio->bi_end_io = NULL; 2613 clear_bit(BIO_UPTODATE, &bio->bi_flags); 2614 if (conf->mirrors[d].rdev == NULL || 2615 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) 2616 continue; 2617 sector = r10_bio->devs[i].addr; 2618 if (is_badblock(conf->mirrors[d].rdev, 2619 sector, max_sync, 2620 &first_bad, &bad_sectors)) { 2621 if (first_bad > sector) 2622 max_sync = first_bad - sector; 2623 else { 2624 bad_sectors -= (sector - first_bad); 2625 if (max_sync > bad_sectors) 2626 max_sync = max_sync; 2627 continue; 2628 } 2629 } 2630 atomic_inc(&conf->mirrors[d].rdev->nr_pending); 2631 atomic_inc(&r10_bio->remaining); 2632 bio->bi_next = biolist; 2633 biolist = bio; 2634 bio->bi_private = r10_bio; 2635 bio->bi_end_io = end_sync_read; 2636 bio->bi_rw = READ; 2637 bio->bi_sector = sector + 2638 conf->mirrors[d].rdev->data_offset; 2639 bio->bi_bdev = conf->mirrors[d].rdev->bdev; 2640 count++; 2641 } 2642 2643 if (count < 2) { 2644 for (i=0; i<conf->copies; i++) { 2645 int d = r10_bio->devs[i].devnum; 2646 if (r10_bio->devs[i].bio->bi_end_io) 2647 rdev_dec_pending(conf->mirrors[d].rdev, 2648 mddev); 2649 } 2650 put_buf(r10_bio); 2651 biolist = NULL; 2652 goto giveup; 2653 } 2654 } 2655 2656 for (bio = biolist; bio ; bio=bio->bi_next) { 2657 2658 bio->bi_flags &= ~(BIO_POOL_MASK - 1); 2659 if (bio->bi_end_io) 2660 bio->bi_flags |= 1 << BIO_UPTODATE; 2661 bio->bi_vcnt = 0; 2662 bio->bi_idx = 0; 2663 bio->bi_phys_segments = 0; 2664 bio->bi_size = 0; 2665 } 2666 2667 nr_sectors = 0; 2668 if (sector_nr + max_sync < max_sector) 2669 max_sector = sector_nr + max_sync; 2670 do { 2671 struct page *page; 2672 int len = PAGE_SIZE; 2673 if (sector_nr + (len>>9) > max_sector) 2674 len = (max_sector - sector_nr) << 9; 2675 if (len == 0) 2676 break; 2677 for (bio= biolist ; bio ; bio=bio->bi_next) { 2678 struct bio *bio2; 2679 page = bio->bi_io_vec[bio->bi_vcnt].bv_page; 2680 if (bio_add_page(bio, page, len, 0)) 2681 continue; 2682 2683 /* stop here */ 2684 bio->bi_io_vec[bio->bi_vcnt].bv_page = page; 2685 for (bio2 = biolist; 2686 bio2 && bio2 != bio; 2687 bio2 = bio2->bi_next) { 2688 /* remove last page from this bio */ 2689 bio2->bi_vcnt--; 2690 bio2->bi_size -= len; 2691 bio2->bi_flags &= ~(1<< BIO_SEG_VALID); 2692 } 2693 goto bio_full; 2694 } 2695 nr_sectors += len>>9; 2696 sector_nr += len>>9; 2697 } while (biolist->bi_vcnt < RESYNC_PAGES); 2698 bio_full: 2699 r10_bio->sectors = nr_sectors; 2700 2701 while (biolist) { 2702 bio = biolist; 2703 biolist = biolist->bi_next; 2704 2705 bio->bi_next = NULL; 2706 r10_bio = bio->bi_private; 2707 r10_bio->sectors = nr_sectors; 2708 2709 if (bio->bi_end_io == end_sync_read) { 2710 md_sync_acct(bio->bi_bdev, nr_sectors); 2711 generic_make_request(bio); 2712 } 2713 } 2714 2715 if (sectors_skipped) 2716 /* pretend they weren't skipped, it makes 2717 * no important difference in this case 2718 */ 2719 md_done_sync(mddev, sectors_skipped, 1); 2720 2721 return sectors_skipped + nr_sectors; 2722 giveup: 2723 /* There is nowhere to write, so all non-sync 2724 * drives must be failed or in resync, all drives 2725 * have a bad block, so try the next chunk... 2726 */ 2727 if (sector_nr + max_sync < max_sector) 2728 max_sector = sector_nr + max_sync; 2729 2730 sectors_skipped += (max_sector - sector_nr); 2731 chunks_skipped ++; 2732 sector_nr = max_sector; 2733 goto skipped; 2734 } 2735 2736 static sector_t 2737 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) 2738 { 2739 sector_t size; 2740 struct r10conf *conf = mddev->private; 2741 2742 if (!raid_disks) 2743 raid_disks = conf->raid_disks; 2744 if (!sectors) 2745 sectors = conf->dev_sectors; 2746 2747 size = sectors >> conf->chunk_shift; 2748 sector_div(size, conf->far_copies); 2749 size = size * raid_disks; 2750 sector_div(size, conf->near_copies); 2751 2752 return size << conf->chunk_shift; 2753 } 2754 2755 2756 static struct r10conf *setup_conf(struct mddev *mddev) 2757 { 2758 struct r10conf *conf = NULL; 2759 int nc, fc, fo; 2760 sector_t stride, size; 2761 int err = -EINVAL; 2762 2763 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) || 2764 !is_power_of_2(mddev->new_chunk_sectors)) { 2765 printk(KERN_ERR "md/raid10:%s: chunk size must be " 2766 "at least PAGE_SIZE(%ld) and be a power of 2.\n", 2767 mdname(mddev), PAGE_SIZE); 2768 goto out; 2769 } 2770 2771 nc = mddev->new_layout & 255; 2772 fc = (mddev->new_layout >> 8) & 255; 2773 fo = mddev->new_layout & (1<<16); 2774 2775 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks || 2776 (mddev->new_layout >> 17)) { 2777 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n", 2778 mdname(mddev), mddev->new_layout); 2779 goto out; 2780 } 2781 2782 err = -ENOMEM; 2783 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); 2784 if (!conf) 2785 goto out; 2786 2787 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, 2788 GFP_KERNEL); 2789 if (!conf->mirrors) 2790 goto out; 2791 2792 conf->tmppage = alloc_page(GFP_KERNEL); 2793 if (!conf->tmppage) 2794 goto out; 2795 2796 2797 conf->raid_disks = mddev->raid_disks; 2798 conf->near_copies = nc; 2799 conf->far_copies = fc; 2800 conf->copies = nc*fc; 2801 conf->far_offset = fo; 2802 conf->chunk_mask = mddev->new_chunk_sectors - 1; 2803 conf->chunk_shift = ffz(~mddev->new_chunk_sectors); 2804 2805 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, 2806 r10bio_pool_free, conf); 2807 if (!conf->r10bio_pool) 2808 goto out; 2809 2810 size = mddev->dev_sectors >> conf->chunk_shift; 2811 sector_div(size, fc); 2812 size = size * conf->raid_disks; 2813 sector_div(size, nc); 2814 /* 'size' is now the number of chunks in the array */ 2815 /* calculate "used chunks per device" in 'stride' */ 2816 stride = size * conf->copies; 2817 2818 /* We need to round up when dividing by raid_disks to 2819 * get the stride size. 2820 */ 2821 stride += conf->raid_disks - 1; 2822 sector_div(stride, conf->raid_disks); 2823 2824 conf->dev_sectors = stride << conf->chunk_shift; 2825 2826 if (fo) 2827 stride = 1; 2828 else 2829 sector_div(stride, fc); 2830 conf->stride = stride << conf->chunk_shift; 2831 2832 2833 spin_lock_init(&conf->device_lock); 2834 INIT_LIST_HEAD(&conf->retry_list); 2835 2836 spin_lock_init(&conf->resync_lock); 2837 init_waitqueue_head(&conf->wait_barrier); 2838 2839 conf->thread = md_register_thread(raid10d, mddev, NULL); 2840 if (!conf->thread) 2841 goto out; 2842 2843 conf->mddev = mddev; 2844 return conf; 2845 2846 out: 2847 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n", 2848 mdname(mddev)); 2849 if (conf) { 2850 if (conf->r10bio_pool) 2851 mempool_destroy(conf->r10bio_pool); 2852 kfree(conf->mirrors); 2853 safe_put_page(conf->tmppage); 2854 kfree(conf); 2855 } 2856 return ERR_PTR(err); 2857 } 2858 2859 static int run(struct mddev *mddev) 2860 { 2861 struct r10conf *conf; 2862 int i, disk_idx, chunk_size; 2863 struct mirror_info *disk; 2864 struct md_rdev *rdev; 2865 sector_t size; 2866 2867 /* 2868 * copy the already verified devices into our private RAID10 2869 * bookkeeping area. [whatever we allocate in run(), 2870 * should be freed in stop()] 2871 */ 2872 2873 if (mddev->private == NULL) { 2874 conf = setup_conf(mddev); 2875 if (IS_ERR(conf)) 2876 return PTR_ERR(conf); 2877 mddev->private = conf; 2878 } 2879 conf = mddev->private; 2880 if (!conf) 2881 goto out; 2882 2883 mddev->thread = conf->thread; 2884 conf->thread = NULL; 2885 2886 chunk_size = mddev->chunk_sectors << 9; 2887 blk_queue_io_min(mddev->queue, chunk_size); 2888 if (conf->raid_disks % conf->near_copies) 2889 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks); 2890 else 2891 blk_queue_io_opt(mddev->queue, chunk_size * 2892 (conf->raid_disks / conf->near_copies)); 2893 2894 list_for_each_entry(rdev, &mddev->disks, same_set) { 2895 2896 disk_idx = rdev->raid_disk; 2897 if (disk_idx >= conf->raid_disks 2898 || disk_idx < 0) 2899 continue; 2900 disk = conf->mirrors + disk_idx; 2901 2902 disk->rdev = rdev; 2903 disk_stack_limits(mddev->gendisk, rdev->bdev, 2904 rdev->data_offset << 9); 2905 /* as we don't honour merge_bvec_fn, we must never risk 2906 * violating it, so limit max_segments to 1 lying 2907 * within a single page. 2908 */ 2909 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { 2910 blk_queue_max_segments(mddev->queue, 1); 2911 blk_queue_segment_boundary(mddev->queue, 2912 PAGE_CACHE_SIZE - 1); 2913 } 2914 2915 disk->head_position = 0; 2916 } 2917 /* need to check that every block has at least one working mirror */ 2918 if (!enough(conf, -1)) { 2919 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n", 2920 mdname(mddev)); 2921 goto out_free_conf; 2922 } 2923 2924 mddev->degraded = 0; 2925 for (i = 0; i < conf->raid_disks; i++) { 2926 2927 disk = conf->mirrors + i; 2928 2929 if (!disk->rdev || 2930 !test_bit(In_sync, &disk->rdev->flags)) { 2931 disk->head_position = 0; 2932 mddev->degraded++; 2933 if (disk->rdev) 2934 conf->fullsync = 1; 2935 } 2936 disk->recovery_disabled = mddev->recovery_disabled - 1; 2937 } 2938 2939 if (mddev->recovery_cp != MaxSector) 2940 printk(KERN_NOTICE "md/raid10:%s: not clean" 2941 " -- starting background reconstruction\n", 2942 mdname(mddev)); 2943 printk(KERN_INFO 2944 "md/raid10:%s: active with %d out of %d devices\n", 2945 mdname(mddev), conf->raid_disks - mddev->degraded, 2946 conf->raid_disks); 2947 /* 2948 * Ok, everything is just fine now 2949 */ 2950 mddev->dev_sectors = conf->dev_sectors; 2951 size = raid10_size(mddev, 0, 0); 2952 md_set_array_sectors(mddev, size); 2953 mddev->resync_max_sectors = size; 2954 2955 mddev->queue->backing_dev_info.congested_fn = raid10_congested; 2956 mddev->queue->backing_dev_info.congested_data = mddev; 2957 2958 /* Calculate max read-ahead size. 2959 * We need to readahead at least twice a whole stripe.... 2960 * maybe... 2961 */ 2962 { 2963 int stripe = conf->raid_disks * 2964 ((mddev->chunk_sectors << 9) / PAGE_SIZE); 2965 stripe /= conf->near_copies; 2966 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) 2967 mddev->queue->backing_dev_info.ra_pages = 2* stripe; 2968 } 2969 2970 if (conf->near_copies < conf->raid_disks) 2971 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); 2972 2973 if (md_integrity_register(mddev)) 2974 goto out_free_conf; 2975 2976 return 0; 2977 2978 out_free_conf: 2979 md_unregister_thread(&mddev->thread); 2980 if (conf->r10bio_pool) 2981 mempool_destroy(conf->r10bio_pool); 2982 safe_put_page(conf->tmppage); 2983 kfree(conf->mirrors); 2984 kfree(conf); 2985 mddev->private = NULL; 2986 out: 2987 return -EIO; 2988 } 2989 2990 static int stop(struct mddev *mddev) 2991 { 2992 struct r10conf *conf = mddev->private; 2993 2994 raise_barrier(conf, 0); 2995 lower_barrier(conf); 2996 2997 md_unregister_thread(&mddev->thread); 2998 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ 2999 if (conf->r10bio_pool) 3000 mempool_destroy(conf->r10bio_pool); 3001 kfree(conf->mirrors); 3002 kfree(conf); 3003 mddev->private = NULL; 3004 return 0; 3005 } 3006 3007 static void raid10_quiesce(struct mddev *mddev, int state) 3008 { 3009 struct r10conf *conf = mddev->private; 3010 3011 switch(state) { 3012 case 1: 3013 raise_barrier(conf, 0); 3014 break; 3015 case 0: 3016 lower_barrier(conf); 3017 break; 3018 } 3019 } 3020 3021 static void *raid10_takeover_raid0(struct mddev *mddev) 3022 { 3023 struct md_rdev *rdev; 3024 struct r10conf *conf; 3025 3026 if (mddev->degraded > 0) { 3027 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n", 3028 mdname(mddev)); 3029 return ERR_PTR(-EINVAL); 3030 } 3031 3032 /* Set new parameters */ 3033 mddev->new_level = 10; 3034 /* new layout: far_copies = 1, near_copies = 2 */ 3035 mddev->new_layout = (1<<8) + 2; 3036 mddev->new_chunk_sectors = mddev->chunk_sectors; 3037 mddev->delta_disks = mddev->raid_disks; 3038 mddev->raid_disks *= 2; 3039 /* make sure it will be not marked as dirty */ 3040 mddev->recovery_cp = MaxSector; 3041 3042 conf = setup_conf(mddev); 3043 if (!IS_ERR(conf)) { 3044 list_for_each_entry(rdev, &mddev->disks, same_set) 3045 if (rdev->raid_disk >= 0) 3046 rdev->new_raid_disk = rdev->raid_disk * 2; 3047 conf->barrier = 1; 3048 } 3049 3050 return conf; 3051 } 3052 3053 static void *raid10_takeover(struct mddev *mddev) 3054 { 3055 struct r0conf *raid0_conf; 3056 3057 /* raid10 can take over: 3058 * raid0 - providing it has only two drives 3059 */ 3060 if (mddev->level == 0) { 3061 /* for raid0 takeover only one zone is supported */ 3062 raid0_conf = mddev->private; 3063 if (raid0_conf->nr_strip_zones > 1) { 3064 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0" 3065 " with more than one zone.\n", 3066 mdname(mddev)); 3067 return ERR_PTR(-EINVAL); 3068 } 3069 return raid10_takeover_raid0(mddev); 3070 } 3071 return ERR_PTR(-EINVAL); 3072 } 3073 3074 static struct md_personality raid10_personality = 3075 { 3076 .name = "raid10", 3077 .level = 10, 3078 .owner = THIS_MODULE, 3079 .make_request = make_request, 3080 .run = run, 3081 .stop = stop, 3082 .status = status, 3083 .error_handler = error, 3084 .hot_add_disk = raid10_add_disk, 3085 .hot_remove_disk= raid10_remove_disk, 3086 .spare_active = raid10_spare_active, 3087 .sync_request = sync_request, 3088 .quiesce = raid10_quiesce, 3089 .size = raid10_size, 3090 .takeover = raid10_takeover, 3091 }; 3092 3093 static int __init raid_init(void) 3094 { 3095 return register_md_personality(&raid10_personality); 3096 } 3097 3098 static void raid_exit(void) 3099 { 3100 unregister_md_personality(&raid10_personality); 3101 } 3102 3103 module_init(raid_init); 3104 module_exit(raid_exit); 3105 MODULE_LICENSE("GPL"); 3106 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); 3107 MODULE_ALIAS("md-personality-9"); /* RAID10 */ 3108 MODULE_ALIAS("md-raid10"); 3109 MODULE_ALIAS("md-level-10"); 3110 3111 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); 3112