1 /* 2 * raid5.c : Multiple Devices driver for Linux 3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman 4 * Copyright (C) 1999, 2000 Ingo Molnar 5 * Copyright (C) 2002, 2003 H. Peter Anvin 6 * 7 * RAID-4/5/6 management functions. 8 * Thanks to Penguin Computing for making the RAID-6 development possible 9 * by donating a test server! 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 /* 22 * BITMAP UNPLUGGING: 23 * 24 * The sequencing for updating the bitmap reliably is a little 25 * subtle (and I got it wrong the first time) so it deserves some 26 * explanation. 27 * 28 * We group bitmap updates into batches. Each batch has a number. 29 * We may write out several batches at once, but that isn't very important. 30 * conf->seq_write is the number of the last batch successfully written. 31 * conf->seq_flush is the number of the last batch that was closed to 32 * new additions. 33 * When we discover that we will need to write to any block in a stripe 34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq 35 * the number of the batch it will be in. This is seq_flush+1. 36 * When we are ready to do a write, if that batch hasn't been written yet, 37 * we plug the array and queue the stripe for later. 38 * When an unplug happens, we increment bm_flush, thus closing the current 39 * batch. 40 * When we notice that bm_flush > bm_write, we write out all pending updates 41 * to the bitmap, and advance bm_write to where bm_flush was. 42 * This may occasionally write a bit out twice, but is sure never to 43 * miss any bits. 44 */ 45 46 #include <linux/blkdev.h> 47 #include <linux/kthread.h> 48 #include <linux/raid/pq.h> 49 #include <linux/async_tx.h> 50 #include <linux/module.h> 51 #include <linux/async.h> 52 #include <linux/seq_file.h> 53 #include <linux/cpu.h> 54 #include <linux/slab.h> 55 #include <linux/ratelimit.h> 56 #include <trace/events/block.h> 57 58 #include "md.h" 59 #include "raid5.h" 60 #include "raid0.h" 61 #include "bitmap.h" 62 63 /* 64 * Stripe cache 65 */ 66 67 #define NR_STRIPES 256 68 #define STRIPE_SIZE PAGE_SIZE 69 #define STRIPE_SHIFT (PAGE_SHIFT - 9) 70 #define STRIPE_SECTORS (STRIPE_SIZE>>9) 71 #define IO_THRESHOLD 1 72 #define BYPASS_THRESHOLD 1 73 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) 74 #define HASH_MASK (NR_HASH - 1) 75 76 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) 77 { 78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; 79 return &conf->stripe_hashtbl[hash]; 80 } 81 82 /* bio's attached to a stripe+device for I/O are linked together in bi_sector 83 * order without overlap. There may be several bio's per stripe+device, and 84 * a bio could span several devices. 85 * When walking this list for a particular stripe+device, we must never proceed 86 * beyond a bio that extends past this device, as the next bio might no longer 87 * be valid. 88 * This function is used to determine the 'next' bio in the list, given the sector 89 * of the current stripe+device 90 */ 91 static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector) 92 { 93 int sectors = bio->bi_size >> 9; 94 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS) 95 return bio->bi_next; 96 else 97 return NULL; 98 } 99 100 /* 101 * We maintain a biased count of active stripes in the bottom 16 bits of 102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits 103 */ 104 static inline int raid5_bi_processed_stripes(struct bio *bio) 105 { 106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; 107 return (atomic_read(segments) >> 16) & 0xffff; 108 } 109 110 static inline int raid5_dec_bi_active_stripes(struct bio *bio) 111 { 112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; 113 return atomic_sub_return(1, segments) & 0xffff; 114 } 115 116 static inline void raid5_inc_bi_active_stripes(struct bio *bio) 117 { 118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; 119 atomic_inc(segments); 120 } 121 122 static inline void raid5_set_bi_processed_stripes(struct bio *bio, 123 unsigned int cnt) 124 { 125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; 126 int old, new; 127 128 do { 129 old = atomic_read(segments); 130 new = (old & 0xffff) | (cnt << 16); 131 } while (atomic_cmpxchg(segments, old, new) != old); 132 } 133 134 static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt) 135 { 136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; 137 atomic_set(segments, cnt); 138 } 139 140 /* Find first data disk in a raid6 stripe */ 141 static inline int raid6_d0(struct stripe_head *sh) 142 { 143 if (sh->ddf_layout) 144 /* ddf always start from first device */ 145 return 0; 146 /* md starts just after Q block */ 147 if (sh->qd_idx == sh->disks - 1) 148 return 0; 149 else 150 return sh->qd_idx + 1; 151 } 152 static inline int raid6_next_disk(int disk, int raid_disks) 153 { 154 disk++; 155 return (disk < raid_disks) ? disk : 0; 156 } 157 158 /* When walking through the disks in a raid5, starting at raid6_d0, 159 * We need to map each disk to a 'slot', where the data disks are slot 160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk 161 * is raid_disks-1. This help does that mapping. 162 */ 163 static int raid6_idx_to_slot(int idx, struct stripe_head *sh, 164 int *count, int syndrome_disks) 165 { 166 int slot = *count; 167 168 if (sh->ddf_layout) 169 (*count)++; 170 if (idx == sh->pd_idx) 171 return syndrome_disks; 172 if (idx == sh->qd_idx) 173 return syndrome_disks + 1; 174 if (!sh->ddf_layout) 175 (*count)++; 176 return slot; 177 } 178 179 static void return_io(struct bio *return_bi) 180 { 181 struct bio *bi = return_bi; 182 while (bi) { 183 184 return_bi = bi->bi_next; 185 bi->bi_next = NULL; 186 bi->bi_size = 0; 187 bio_endio(bi, 0); 188 bi = return_bi; 189 } 190 } 191 192 static void print_raid5_conf (struct r5conf *conf); 193 194 static int stripe_operations_active(struct stripe_head *sh) 195 { 196 return sh->check_state || sh->reconstruct_state || 197 test_bit(STRIPE_BIOFILL_RUN, &sh->state) || 198 test_bit(STRIPE_COMPUTE_RUN, &sh->state); 199 } 200 201 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh) 202 { 203 BUG_ON(!list_empty(&sh->lru)); 204 BUG_ON(atomic_read(&conf->active_stripes)==0); 205 if (test_bit(STRIPE_HANDLE, &sh->state)) { 206 if (test_bit(STRIPE_DELAYED, &sh->state) && 207 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 208 list_add_tail(&sh->lru, &conf->delayed_list); 209 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && 210 sh->bm_seq - conf->seq_write > 0) 211 list_add_tail(&sh->lru, &conf->bitmap_list); 212 else { 213 clear_bit(STRIPE_DELAYED, &sh->state); 214 clear_bit(STRIPE_BIT_DELAY, &sh->state); 215 list_add_tail(&sh->lru, &conf->handle_list); 216 } 217 md_wakeup_thread(conf->mddev->thread); 218 } else { 219 BUG_ON(stripe_operations_active(sh)); 220 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 221 if (atomic_dec_return(&conf->preread_active_stripes) 222 < IO_THRESHOLD) 223 md_wakeup_thread(conf->mddev->thread); 224 atomic_dec(&conf->active_stripes); 225 if (!test_bit(STRIPE_EXPANDING, &sh->state)) { 226 list_add_tail(&sh->lru, &conf->inactive_list); 227 wake_up(&conf->wait_for_stripe); 228 if (conf->retry_read_aligned) 229 md_wakeup_thread(conf->mddev->thread); 230 } 231 } 232 } 233 234 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh) 235 { 236 if (atomic_dec_and_test(&sh->count)) 237 do_release_stripe(conf, sh); 238 } 239 240 static void release_stripe(struct stripe_head *sh) 241 { 242 struct r5conf *conf = sh->raid_conf; 243 unsigned long flags; 244 245 local_irq_save(flags); 246 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { 247 do_release_stripe(conf, sh); 248 spin_unlock(&conf->device_lock); 249 } 250 local_irq_restore(flags); 251 } 252 253 static inline void remove_hash(struct stripe_head *sh) 254 { 255 pr_debug("remove_hash(), stripe %llu\n", 256 (unsigned long long)sh->sector); 257 258 hlist_del_init(&sh->hash); 259 } 260 261 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) 262 { 263 struct hlist_head *hp = stripe_hash(conf, sh->sector); 264 265 pr_debug("insert_hash(), stripe %llu\n", 266 (unsigned long long)sh->sector); 267 268 hlist_add_head(&sh->hash, hp); 269 } 270 271 272 /* find an idle stripe, make sure it is unhashed, and return it. */ 273 static struct stripe_head *get_free_stripe(struct r5conf *conf) 274 { 275 struct stripe_head *sh = NULL; 276 struct list_head *first; 277 278 if (list_empty(&conf->inactive_list)) 279 goto out; 280 first = conf->inactive_list.next; 281 sh = list_entry(first, struct stripe_head, lru); 282 list_del_init(first); 283 remove_hash(sh); 284 atomic_inc(&conf->active_stripes); 285 out: 286 return sh; 287 } 288 289 static void shrink_buffers(struct stripe_head *sh) 290 { 291 struct page *p; 292 int i; 293 int num = sh->raid_conf->pool_size; 294 295 for (i = 0; i < num ; i++) { 296 p = sh->dev[i].page; 297 if (!p) 298 continue; 299 sh->dev[i].page = NULL; 300 put_page(p); 301 } 302 } 303 304 static int grow_buffers(struct stripe_head *sh) 305 { 306 int i; 307 int num = sh->raid_conf->pool_size; 308 309 for (i = 0; i < num; i++) { 310 struct page *page; 311 312 if (!(page = alloc_page(GFP_KERNEL))) { 313 return 1; 314 } 315 sh->dev[i].page = page; 316 } 317 return 0; 318 } 319 320 static void raid5_build_block(struct stripe_head *sh, int i, int previous); 321 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, 322 struct stripe_head *sh); 323 324 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) 325 { 326 struct r5conf *conf = sh->raid_conf; 327 int i; 328 329 BUG_ON(atomic_read(&sh->count) != 0); 330 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); 331 BUG_ON(stripe_operations_active(sh)); 332 333 pr_debug("init_stripe called, stripe %llu\n", 334 (unsigned long long)sh->sector); 335 336 remove_hash(sh); 337 338 sh->generation = conf->generation - previous; 339 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; 340 sh->sector = sector; 341 stripe_set_idx(sector, conf, previous, sh); 342 sh->state = 0; 343 344 345 for (i = sh->disks; i--; ) { 346 struct r5dev *dev = &sh->dev[i]; 347 348 if (dev->toread || dev->read || dev->towrite || dev->written || 349 test_bit(R5_LOCKED, &dev->flags)) { 350 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n", 351 (unsigned long long)sh->sector, i, dev->toread, 352 dev->read, dev->towrite, dev->written, 353 test_bit(R5_LOCKED, &dev->flags)); 354 WARN_ON(1); 355 } 356 dev->flags = 0; 357 raid5_build_block(sh, i, previous); 358 } 359 insert_hash(conf, sh); 360 } 361 362 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, 363 short generation) 364 { 365 struct stripe_head *sh; 366 367 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); 368 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) 369 if (sh->sector == sector && sh->generation == generation) 370 return sh; 371 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); 372 return NULL; 373 } 374 375 /* 376 * Need to check if array has failed when deciding whether to: 377 * - start an array 378 * - remove non-faulty devices 379 * - add a spare 380 * - allow a reshape 381 * This determination is simple when no reshape is happening. 382 * However if there is a reshape, we need to carefully check 383 * both the before and after sections. 384 * This is because some failed devices may only affect one 385 * of the two sections, and some non-in_sync devices may 386 * be insync in the section most affected by failed devices. 387 */ 388 static int calc_degraded(struct r5conf *conf) 389 { 390 int degraded, degraded2; 391 int i; 392 393 rcu_read_lock(); 394 degraded = 0; 395 for (i = 0; i < conf->previous_raid_disks; i++) { 396 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 397 if (rdev && test_bit(Faulty, &rdev->flags)) 398 rdev = rcu_dereference(conf->disks[i].replacement); 399 if (!rdev || test_bit(Faulty, &rdev->flags)) 400 degraded++; 401 else if (test_bit(In_sync, &rdev->flags)) 402 ; 403 else 404 /* not in-sync or faulty. 405 * If the reshape increases the number of devices, 406 * this is being recovered by the reshape, so 407 * this 'previous' section is not in_sync. 408 * If the number of devices is being reduced however, 409 * the device can only be part of the array if 410 * we are reverting a reshape, so this section will 411 * be in-sync. 412 */ 413 if (conf->raid_disks >= conf->previous_raid_disks) 414 degraded++; 415 } 416 rcu_read_unlock(); 417 if (conf->raid_disks == conf->previous_raid_disks) 418 return degraded; 419 rcu_read_lock(); 420 degraded2 = 0; 421 for (i = 0; i < conf->raid_disks; i++) { 422 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 423 if (rdev && test_bit(Faulty, &rdev->flags)) 424 rdev = rcu_dereference(conf->disks[i].replacement); 425 if (!rdev || test_bit(Faulty, &rdev->flags)) 426 degraded2++; 427 else if (test_bit(In_sync, &rdev->flags)) 428 ; 429 else 430 /* not in-sync or faulty. 431 * If reshape increases the number of devices, this 432 * section has already been recovered, else it 433 * almost certainly hasn't. 434 */ 435 if (conf->raid_disks <= conf->previous_raid_disks) 436 degraded2++; 437 } 438 rcu_read_unlock(); 439 if (degraded2 > degraded) 440 return degraded2; 441 return degraded; 442 } 443 444 static int has_failed(struct r5conf *conf) 445 { 446 int degraded; 447 448 if (conf->mddev->reshape_position == MaxSector) 449 return conf->mddev->degraded > conf->max_degraded; 450 451 degraded = calc_degraded(conf); 452 if (degraded > conf->max_degraded) 453 return 1; 454 return 0; 455 } 456 457 static struct stripe_head * 458 get_active_stripe(struct r5conf *conf, sector_t sector, 459 int previous, int noblock, int noquiesce) 460 { 461 struct stripe_head *sh; 462 463 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); 464 465 spin_lock_irq(&conf->device_lock); 466 467 do { 468 wait_event_lock_irq(conf->wait_for_stripe, 469 conf->quiesce == 0 || noquiesce, 470 conf->device_lock); 471 sh = __find_stripe(conf, sector, conf->generation - previous); 472 if (!sh) { 473 if (!conf->inactive_blocked) 474 sh = get_free_stripe(conf); 475 if (noblock && sh == NULL) 476 break; 477 if (!sh) { 478 conf->inactive_blocked = 1; 479 wait_event_lock_irq(conf->wait_for_stripe, 480 !list_empty(&conf->inactive_list) && 481 (atomic_read(&conf->active_stripes) 482 < (conf->max_nr_stripes *3/4) 483 || !conf->inactive_blocked), 484 conf->device_lock); 485 conf->inactive_blocked = 0; 486 } else 487 init_stripe(sh, sector, previous); 488 } else { 489 if (atomic_read(&sh->count)) { 490 BUG_ON(!list_empty(&sh->lru) 491 && !test_bit(STRIPE_EXPANDING, &sh->state) 492 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)); 493 } else { 494 if (!test_bit(STRIPE_HANDLE, &sh->state)) 495 atomic_inc(&conf->active_stripes); 496 if (list_empty(&sh->lru) && 497 !test_bit(STRIPE_EXPANDING, &sh->state)) 498 BUG(); 499 list_del_init(&sh->lru); 500 } 501 } 502 } while (sh == NULL); 503 504 if (sh) 505 atomic_inc(&sh->count); 506 507 spin_unlock_irq(&conf->device_lock); 508 return sh; 509 } 510 511 /* Determine if 'data_offset' or 'new_data_offset' should be used 512 * in this stripe_head. 513 */ 514 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) 515 { 516 sector_t progress = conf->reshape_progress; 517 /* Need a memory barrier to make sure we see the value 518 * of conf->generation, or ->data_offset that was set before 519 * reshape_progress was updated. 520 */ 521 smp_rmb(); 522 if (progress == MaxSector) 523 return 0; 524 if (sh->generation == conf->generation - 1) 525 return 0; 526 /* We are in a reshape, and this is a new-generation stripe, 527 * so use new_data_offset. 528 */ 529 return 1; 530 } 531 532 static void 533 raid5_end_read_request(struct bio *bi, int error); 534 static void 535 raid5_end_write_request(struct bio *bi, int error); 536 537 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) 538 { 539 struct r5conf *conf = sh->raid_conf; 540 int i, disks = sh->disks; 541 542 might_sleep(); 543 544 for (i = disks; i--; ) { 545 int rw; 546 int replace_only = 0; 547 struct bio *bi, *rbi; 548 struct md_rdev *rdev, *rrdev = NULL; 549 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { 550 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) 551 rw = WRITE_FUA; 552 else 553 rw = WRITE; 554 if (test_bit(R5_Discard, &sh->dev[i].flags)) 555 rw |= REQ_DISCARD; 556 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) 557 rw = READ; 558 else if (test_and_clear_bit(R5_WantReplace, 559 &sh->dev[i].flags)) { 560 rw = WRITE; 561 replace_only = 1; 562 } else 563 continue; 564 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) 565 rw |= REQ_SYNC; 566 567 bi = &sh->dev[i].req; 568 rbi = &sh->dev[i].rreq; /* For writing to replacement */ 569 570 bi->bi_rw = rw; 571 rbi->bi_rw = rw; 572 if (rw & WRITE) { 573 bi->bi_end_io = raid5_end_write_request; 574 rbi->bi_end_io = raid5_end_write_request; 575 } else 576 bi->bi_end_io = raid5_end_read_request; 577 578 rcu_read_lock(); 579 rrdev = rcu_dereference(conf->disks[i].replacement); 580 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ 581 rdev = rcu_dereference(conf->disks[i].rdev); 582 if (!rdev) { 583 rdev = rrdev; 584 rrdev = NULL; 585 } 586 if (rw & WRITE) { 587 if (replace_only) 588 rdev = NULL; 589 if (rdev == rrdev) 590 /* We raced and saw duplicates */ 591 rrdev = NULL; 592 } else { 593 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) 594 rdev = rrdev; 595 rrdev = NULL; 596 } 597 598 if (rdev && test_bit(Faulty, &rdev->flags)) 599 rdev = NULL; 600 if (rdev) 601 atomic_inc(&rdev->nr_pending); 602 if (rrdev && test_bit(Faulty, &rrdev->flags)) 603 rrdev = NULL; 604 if (rrdev) 605 atomic_inc(&rrdev->nr_pending); 606 rcu_read_unlock(); 607 608 /* We have already checked bad blocks for reads. Now 609 * need to check for writes. We never accept write errors 610 * on the replacement, so we don't to check rrdev. 611 */ 612 while ((rw & WRITE) && rdev && 613 test_bit(WriteErrorSeen, &rdev->flags)) { 614 sector_t first_bad; 615 int bad_sectors; 616 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, 617 &first_bad, &bad_sectors); 618 if (!bad) 619 break; 620 621 if (bad < 0) { 622 set_bit(BlockedBadBlocks, &rdev->flags); 623 if (!conf->mddev->external && 624 conf->mddev->flags) { 625 /* It is very unlikely, but we might 626 * still need to write out the 627 * bad block log - better give it 628 * a chance*/ 629 md_check_recovery(conf->mddev); 630 } 631 /* 632 * Because md_wait_for_blocked_rdev 633 * will dec nr_pending, we must 634 * increment it first. 635 */ 636 atomic_inc(&rdev->nr_pending); 637 md_wait_for_blocked_rdev(rdev, conf->mddev); 638 } else { 639 /* Acknowledged bad block - skip the write */ 640 rdev_dec_pending(rdev, conf->mddev); 641 rdev = NULL; 642 } 643 } 644 645 if (rdev) { 646 if (s->syncing || s->expanding || s->expanded 647 || s->replacing) 648 md_sync_acct(rdev->bdev, STRIPE_SECTORS); 649 650 set_bit(STRIPE_IO_STARTED, &sh->state); 651 652 bi->bi_bdev = rdev->bdev; 653 pr_debug("%s: for %llu schedule op %ld on disc %d\n", 654 __func__, (unsigned long long)sh->sector, 655 bi->bi_rw, i); 656 atomic_inc(&sh->count); 657 if (use_new_offset(conf, sh)) 658 bi->bi_sector = (sh->sector 659 + rdev->new_data_offset); 660 else 661 bi->bi_sector = (sh->sector 662 + rdev->data_offset); 663 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) 664 bi->bi_rw |= REQ_FLUSH; 665 666 bi->bi_flags = 1 << BIO_UPTODATE; 667 bi->bi_idx = 0; 668 bi->bi_io_vec[0].bv_len = STRIPE_SIZE; 669 bi->bi_io_vec[0].bv_offset = 0; 670 bi->bi_size = STRIPE_SIZE; 671 bi->bi_next = NULL; 672 if (rrdev) 673 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); 674 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), 675 bi, disk_devt(conf->mddev->gendisk), 676 sh->dev[i].sector); 677 generic_make_request(bi); 678 } 679 if (rrdev) { 680 if (s->syncing || s->expanding || s->expanded 681 || s->replacing) 682 md_sync_acct(rrdev->bdev, STRIPE_SECTORS); 683 684 set_bit(STRIPE_IO_STARTED, &sh->state); 685 686 rbi->bi_bdev = rrdev->bdev; 687 pr_debug("%s: for %llu schedule op %ld on " 688 "replacement disc %d\n", 689 __func__, (unsigned long long)sh->sector, 690 rbi->bi_rw, i); 691 atomic_inc(&sh->count); 692 if (use_new_offset(conf, sh)) 693 rbi->bi_sector = (sh->sector 694 + rrdev->new_data_offset); 695 else 696 rbi->bi_sector = (sh->sector 697 + rrdev->data_offset); 698 rbi->bi_flags = 1 << BIO_UPTODATE; 699 rbi->bi_idx = 0; 700 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; 701 rbi->bi_io_vec[0].bv_offset = 0; 702 rbi->bi_size = STRIPE_SIZE; 703 rbi->bi_next = NULL; 704 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), 705 rbi, disk_devt(conf->mddev->gendisk), 706 sh->dev[i].sector); 707 generic_make_request(rbi); 708 } 709 if (!rdev && !rrdev) { 710 if (rw & WRITE) 711 set_bit(STRIPE_DEGRADED, &sh->state); 712 pr_debug("skip op %ld on disc %d for sector %llu\n", 713 bi->bi_rw, i, (unsigned long long)sh->sector); 714 clear_bit(R5_LOCKED, &sh->dev[i].flags); 715 set_bit(STRIPE_HANDLE, &sh->state); 716 } 717 } 718 } 719 720 static struct dma_async_tx_descriptor * 721 async_copy_data(int frombio, struct bio *bio, struct page *page, 722 sector_t sector, struct dma_async_tx_descriptor *tx) 723 { 724 struct bio_vec *bvl; 725 struct page *bio_page; 726 int i; 727 int page_offset; 728 struct async_submit_ctl submit; 729 enum async_tx_flags flags = 0; 730 731 if (bio->bi_sector >= sector) 732 page_offset = (signed)(bio->bi_sector - sector) * 512; 733 else 734 page_offset = (signed)(sector - bio->bi_sector) * -512; 735 736 if (frombio) 737 flags |= ASYNC_TX_FENCE; 738 init_async_submit(&submit, flags, tx, NULL, NULL, NULL); 739 740 bio_for_each_segment(bvl, bio, i) { 741 int len = bvl->bv_len; 742 int clen; 743 int b_offset = 0; 744 745 if (page_offset < 0) { 746 b_offset = -page_offset; 747 page_offset += b_offset; 748 len -= b_offset; 749 } 750 751 if (len > 0 && page_offset + len > STRIPE_SIZE) 752 clen = STRIPE_SIZE - page_offset; 753 else 754 clen = len; 755 756 if (clen > 0) { 757 b_offset += bvl->bv_offset; 758 bio_page = bvl->bv_page; 759 if (frombio) 760 tx = async_memcpy(page, bio_page, page_offset, 761 b_offset, clen, &submit); 762 else 763 tx = async_memcpy(bio_page, page, b_offset, 764 page_offset, clen, &submit); 765 } 766 /* chain the operations */ 767 submit.depend_tx = tx; 768 769 if (clen < len) /* hit end of page */ 770 break; 771 page_offset += len; 772 } 773 774 return tx; 775 } 776 777 static void ops_complete_biofill(void *stripe_head_ref) 778 { 779 struct stripe_head *sh = stripe_head_ref; 780 struct bio *return_bi = NULL; 781 int i; 782 783 pr_debug("%s: stripe %llu\n", __func__, 784 (unsigned long long)sh->sector); 785 786 /* clear completed biofills */ 787 for (i = sh->disks; i--; ) { 788 struct r5dev *dev = &sh->dev[i]; 789 790 /* acknowledge completion of a biofill operation */ 791 /* and check if we need to reply to a read request, 792 * new R5_Wantfill requests are held off until 793 * !STRIPE_BIOFILL_RUN 794 */ 795 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { 796 struct bio *rbi, *rbi2; 797 798 BUG_ON(!dev->read); 799 rbi = dev->read; 800 dev->read = NULL; 801 while (rbi && rbi->bi_sector < 802 dev->sector + STRIPE_SECTORS) { 803 rbi2 = r5_next_bio(rbi, dev->sector); 804 if (!raid5_dec_bi_active_stripes(rbi)) { 805 rbi->bi_next = return_bi; 806 return_bi = rbi; 807 } 808 rbi = rbi2; 809 } 810 } 811 } 812 clear_bit(STRIPE_BIOFILL_RUN, &sh->state); 813 814 return_io(return_bi); 815 816 set_bit(STRIPE_HANDLE, &sh->state); 817 release_stripe(sh); 818 } 819 820 static void ops_run_biofill(struct stripe_head *sh) 821 { 822 struct dma_async_tx_descriptor *tx = NULL; 823 struct async_submit_ctl submit; 824 int i; 825 826 pr_debug("%s: stripe %llu\n", __func__, 827 (unsigned long long)sh->sector); 828 829 for (i = sh->disks; i--; ) { 830 struct r5dev *dev = &sh->dev[i]; 831 if (test_bit(R5_Wantfill, &dev->flags)) { 832 struct bio *rbi; 833 spin_lock_irq(&sh->stripe_lock); 834 dev->read = rbi = dev->toread; 835 dev->toread = NULL; 836 spin_unlock_irq(&sh->stripe_lock); 837 while (rbi && rbi->bi_sector < 838 dev->sector + STRIPE_SECTORS) { 839 tx = async_copy_data(0, rbi, dev->page, 840 dev->sector, tx); 841 rbi = r5_next_bio(rbi, dev->sector); 842 } 843 } 844 } 845 846 atomic_inc(&sh->count); 847 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); 848 async_trigger_callback(&submit); 849 } 850 851 static void mark_target_uptodate(struct stripe_head *sh, int target) 852 { 853 struct r5dev *tgt; 854 855 if (target < 0) 856 return; 857 858 tgt = &sh->dev[target]; 859 set_bit(R5_UPTODATE, &tgt->flags); 860 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 861 clear_bit(R5_Wantcompute, &tgt->flags); 862 } 863 864 static void ops_complete_compute(void *stripe_head_ref) 865 { 866 struct stripe_head *sh = stripe_head_ref; 867 868 pr_debug("%s: stripe %llu\n", __func__, 869 (unsigned long long)sh->sector); 870 871 /* mark the computed target(s) as uptodate */ 872 mark_target_uptodate(sh, sh->ops.target); 873 mark_target_uptodate(sh, sh->ops.target2); 874 875 clear_bit(STRIPE_COMPUTE_RUN, &sh->state); 876 if (sh->check_state == check_state_compute_run) 877 sh->check_state = check_state_compute_result; 878 set_bit(STRIPE_HANDLE, &sh->state); 879 release_stripe(sh); 880 } 881 882 /* return a pointer to the address conversion region of the scribble buffer */ 883 static addr_conv_t *to_addr_conv(struct stripe_head *sh, 884 struct raid5_percpu *percpu) 885 { 886 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); 887 } 888 889 static struct dma_async_tx_descriptor * 890 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) 891 { 892 int disks = sh->disks; 893 struct page **xor_srcs = percpu->scribble; 894 int target = sh->ops.target; 895 struct r5dev *tgt = &sh->dev[target]; 896 struct page *xor_dest = tgt->page; 897 int count = 0; 898 struct dma_async_tx_descriptor *tx; 899 struct async_submit_ctl submit; 900 int i; 901 902 pr_debug("%s: stripe %llu block: %d\n", 903 __func__, (unsigned long long)sh->sector, target); 904 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 905 906 for (i = disks; i--; ) 907 if (i != target) 908 xor_srcs[count++] = sh->dev[i].page; 909 910 atomic_inc(&sh->count); 911 912 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, 913 ops_complete_compute, sh, to_addr_conv(sh, percpu)); 914 if (unlikely(count == 1)) 915 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); 916 else 917 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); 918 919 return tx; 920 } 921 922 /* set_syndrome_sources - populate source buffers for gen_syndrome 923 * @srcs - (struct page *) array of size sh->disks 924 * @sh - stripe_head to parse 925 * 926 * Populates srcs in proper layout order for the stripe and returns the 927 * 'count' of sources to be used in a call to async_gen_syndrome. The P 928 * destination buffer is recorded in srcs[count] and the Q destination 929 * is recorded in srcs[count+1]]. 930 */ 931 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) 932 { 933 int disks = sh->disks; 934 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); 935 int d0_idx = raid6_d0(sh); 936 int count; 937 int i; 938 939 for (i = 0; i < disks; i++) 940 srcs[i] = NULL; 941 942 count = 0; 943 i = d0_idx; 944 do { 945 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); 946 947 srcs[slot] = sh->dev[i].page; 948 i = raid6_next_disk(i, disks); 949 } while (i != d0_idx); 950 951 return syndrome_disks; 952 } 953 954 static struct dma_async_tx_descriptor * 955 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) 956 { 957 int disks = sh->disks; 958 struct page **blocks = percpu->scribble; 959 int target; 960 int qd_idx = sh->qd_idx; 961 struct dma_async_tx_descriptor *tx; 962 struct async_submit_ctl submit; 963 struct r5dev *tgt; 964 struct page *dest; 965 int i; 966 int count; 967 968 if (sh->ops.target < 0) 969 target = sh->ops.target2; 970 else if (sh->ops.target2 < 0) 971 target = sh->ops.target; 972 else 973 /* we should only have one valid target */ 974 BUG(); 975 BUG_ON(target < 0); 976 pr_debug("%s: stripe %llu block: %d\n", 977 __func__, (unsigned long long)sh->sector, target); 978 979 tgt = &sh->dev[target]; 980 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 981 dest = tgt->page; 982 983 atomic_inc(&sh->count); 984 985 if (target == qd_idx) { 986 count = set_syndrome_sources(blocks, sh); 987 blocks[count] = NULL; /* regenerating p is not necessary */ 988 BUG_ON(blocks[count+1] != dest); /* q should already be set */ 989 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, 990 ops_complete_compute, sh, 991 to_addr_conv(sh, percpu)); 992 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); 993 } else { 994 /* Compute any data- or p-drive using XOR */ 995 count = 0; 996 for (i = disks; i-- ; ) { 997 if (i == target || i == qd_idx) 998 continue; 999 blocks[count++] = sh->dev[i].page; 1000 } 1001 1002 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, 1003 NULL, ops_complete_compute, sh, 1004 to_addr_conv(sh, percpu)); 1005 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); 1006 } 1007 1008 return tx; 1009 } 1010 1011 static struct dma_async_tx_descriptor * 1012 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) 1013 { 1014 int i, count, disks = sh->disks; 1015 int syndrome_disks = sh->ddf_layout ? disks : disks-2; 1016 int d0_idx = raid6_d0(sh); 1017 int faila = -1, failb = -1; 1018 int target = sh->ops.target; 1019 int target2 = sh->ops.target2; 1020 struct r5dev *tgt = &sh->dev[target]; 1021 struct r5dev *tgt2 = &sh->dev[target2]; 1022 struct dma_async_tx_descriptor *tx; 1023 struct page **blocks = percpu->scribble; 1024 struct async_submit_ctl submit; 1025 1026 pr_debug("%s: stripe %llu block1: %d block2: %d\n", 1027 __func__, (unsigned long long)sh->sector, target, target2); 1028 BUG_ON(target < 0 || target2 < 0); 1029 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 1030 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); 1031 1032 /* we need to open-code set_syndrome_sources to handle the 1033 * slot number conversion for 'faila' and 'failb' 1034 */ 1035 for (i = 0; i < disks ; i++) 1036 blocks[i] = NULL; 1037 count = 0; 1038 i = d0_idx; 1039 do { 1040 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); 1041 1042 blocks[slot] = sh->dev[i].page; 1043 1044 if (i == target) 1045 faila = slot; 1046 if (i == target2) 1047 failb = slot; 1048 i = raid6_next_disk(i, disks); 1049 } while (i != d0_idx); 1050 1051 BUG_ON(faila == failb); 1052 if (failb < faila) 1053 swap(faila, failb); 1054 pr_debug("%s: stripe: %llu faila: %d failb: %d\n", 1055 __func__, (unsigned long long)sh->sector, faila, failb); 1056 1057 atomic_inc(&sh->count); 1058 1059 if (failb == syndrome_disks+1) { 1060 /* Q disk is one of the missing disks */ 1061 if (faila == syndrome_disks) { 1062 /* Missing P+Q, just recompute */ 1063 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, 1064 ops_complete_compute, sh, 1065 to_addr_conv(sh, percpu)); 1066 return async_gen_syndrome(blocks, 0, syndrome_disks+2, 1067 STRIPE_SIZE, &submit); 1068 } else { 1069 struct page *dest; 1070 int data_target; 1071 int qd_idx = sh->qd_idx; 1072 1073 /* Missing D+Q: recompute D from P, then recompute Q */ 1074 if (target == qd_idx) 1075 data_target = target2; 1076 else 1077 data_target = target; 1078 1079 count = 0; 1080 for (i = disks; i-- ; ) { 1081 if (i == data_target || i == qd_idx) 1082 continue; 1083 blocks[count++] = sh->dev[i].page; 1084 } 1085 dest = sh->dev[data_target].page; 1086 init_async_submit(&submit, 1087 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, 1088 NULL, NULL, NULL, 1089 to_addr_conv(sh, percpu)); 1090 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, 1091 &submit); 1092 1093 count = set_syndrome_sources(blocks, sh); 1094 init_async_submit(&submit, ASYNC_TX_FENCE, tx, 1095 ops_complete_compute, sh, 1096 to_addr_conv(sh, percpu)); 1097 return async_gen_syndrome(blocks, 0, count+2, 1098 STRIPE_SIZE, &submit); 1099 } 1100 } else { 1101 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, 1102 ops_complete_compute, sh, 1103 to_addr_conv(sh, percpu)); 1104 if (failb == syndrome_disks) { 1105 /* We're missing D+P. */ 1106 return async_raid6_datap_recov(syndrome_disks+2, 1107 STRIPE_SIZE, faila, 1108 blocks, &submit); 1109 } else { 1110 /* We're missing D+D. */ 1111 return async_raid6_2data_recov(syndrome_disks+2, 1112 STRIPE_SIZE, faila, failb, 1113 blocks, &submit); 1114 } 1115 } 1116 } 1117 1118 1119 static void ops_complete_prexor(void *stripe_head_ref) 1120 { 1121 struct stripe_head *sh = stripe_head_ref; 1122 1123 pr_debug("%s: stripe %llu\n", __func__, 1124 (unsigned long long)sh->sector); 1125 } 1126 1127 static struct dma_async_tx_descriptor * 1128 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, 1129 struct dma_async_tx_descriptor *tx) 1130 { 1131 int disks = sh->disks; 1132 struct page **xor_srcs = percpu->scribble; 1133 int count = 0, pd_idx = sh->pd_idx, i; 1134 struct async_submit_ctl submit; 1135 1136 /* existing parity data subtracted */ 1137 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; 1138 1139 pr_debug("%s: stripe %llu\n", __func__, 1140 (unsigned long long)sh->sector); 1141 1142 for (i = disks; i--; ) { 1143 struct r5dev *dev = &sh->dev[i]; 1144 /* Only process blocks that are known to be uptodate */ 1145 if (test_bit(R5_Wantdrain, &dev->flags)) 1146 xor_srcs[count++] = dev->page; 1147 } 1148 1149 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, 1150 ops_complete_prexor, sh, to_addr_conv(sh, percpu)); 1151 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); 1152 1153 return tx; 1154 } 1155 1156 static struct dma_async_tx_descriptor * 1157 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) 1158 { 1159 int disks = sh->disks; 1160 int i; 1161 1162 pr_debug("%s: stripe %llu\n", __func__, 1163 (unsigned long long)sh->sector); 1164 1165 for (i = disks; i--; ) { 1166 struct r5dev *dev = &sh->dev[i]; 1167 struct bio *chosen; 1168 1169 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { 1170 struct bio *wbi; 1171 1172 spin_lock_irq(&sh->stripe_lock); 1173 chosen = dev->towrite; 1174 dev->towrite = NULL; 1175 BUG_ON(dev->written); 1176 wbi = dev->written = chosen; 1177 spin_unlock_irq(&sh->stripe_lock); 1178 1179 while (wbi && wbi->bi_sector < 1180 dev->sector + STRIPE_SECTORS) { 1181 if (wbi->bi_rw & REQ_FUA) 1182 set_bit(R5_WantFUA, &dev->flags); 1183 if (wbi->bi_rw & REQ_SYNC) 1184 set_bit(R5_SyncIO, &dev->flags); 1185 if (wbi->bi_rw & REQ_DISCARD) 1186 set_bit(R5_Discard, &dev->flags); 1187 else 1188 tx = async_copy_data(1, wbi, dev->page, 1189 dev->sector, tx); 1190 wbi = r5_next_bio(wbi, dev->sector); 1191 } 1192 } 1193 } 1194 1195 return tx; 1196 } 1197 1198 static void ops_complete_reconstruct(void *stripe_head_ref) 1199 { 1200 struct stripe_head *sh = stripe_head_ref; 1201 int disks = sh->disks; 1202 int pd_idx = sh->pd_idx; 1203 int qd_idx = sh->qd_idx; 1204 int i; 1205 bool fua = false, sync = false, discard = false; 1206 1207 pr_debug("%s: stripe %llu\n", __func__, 1208 (unsigned long long)sh->sector); 1209 1210 for (i = disks; i--; ) { 1211 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); 1212 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); 1213 discard |= test_bit(R5_Discard, &sh->dev[i].flags); 1214 } 1215 1216 for (i = disks; i--; ) { 1217 struct r5dev *dev = &sh->dev[i]; 1218 1219 if (dev->written || i == pd_idx || i == qd_idx) { 1220 if (!discard) 1221 set_bit(R5_UPTODATE, &dev->flags); 1222 if (fua) 1223 set_bit(R5_WantFUA, &dev->flags); 1224 if (sync) 1225 set_bit(R5_SyncIO, &dev->flags); 1226 } 1227 } 1228 1229 if (sh->reconstruct_state == reconstruct_state_drain_run) 1230 sh->reconstruct_state = reconstruct_state_drain_result; 1231 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) 1232 sh->reconstruct_state = reconstruct_state_prexor_drain_result; 1233 else { 1234 BUG_ON(sh->reconstruct_state != reconstruct_state_run); 1235 sh->reconstruct_state = reconstruct_state_result; 1236 } 1237 1238 set_bit(STRIPE_HANDLE, &sh->state); 1239 release_stripe(sh); 1240 } 1241 1242 static void 1243 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, 1244 struct dma_async_tx_descriptor *tx) 1245 { 1246 int disks = sh->disks; 1247 struct page **xor_srcs = percpu->scribble; 1248 struct async_submit_ctl submit; 1249 int count = 0, pd_idx = sh->pd_idx, i; 1250 struct page *xor_dest; 1251 int prexor = 0; 1252 unsigned long flags; 1253 1254 pr_debug("%s: stripe %llu\n", __func__, 1255 (unsigned long long)sh->sector); 1256 1257 for (i = 0; i < sh->disks; i++) { 1258 if (pd_idx == i) 1259 continue; 1260 if (!test_bit(R5_Discard, &sh->dev[i].flags)) 1261 break; 1262 } 1263 if (i >= sh->disks) { 1264 atomic_inc(&sh->count); 1265 set_bit(R5_Discard, &sh->dev[pd_idx].flags); 1266 ops_complete_reconstruct(sh); 1267 return; 1268 } 1269 /* check if prexor is active which means only process blocks 1270 * that are part of a read-modify-write (written) 1271 */ 1272 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { 1273 prexor = 1; 1274 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; 1275 for (i = disks; i--; ) { 1276 struct r5dev *dev = &sh->dev[i]; 1277 if (dev->written) 1278 xor_srcs[count++] = dev->page; 1279 } 1280 } else { 1281 xor_dest = sh->dev[pd_idx].page; 1282 for (i = disks; i--; ) { 1283 struct r5dev *dev = &sh->dev[i]; 1284 if (i != pd_idx) 1285 xor_srcs[count++] = dev->page; 1286 } 1287 } 1288 1289 /* 1/ if we prexor'd then the dest is reused as a source 1290 * 2/ if we did not prexor then we are redoing the parity 1291 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST 1292 * for the synchronous xor case 1293 */ 1294 flags = ASYNC_TX_ACK | 1295 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); 1296 1297 atomic_inc(&sh->count); 1298 1299 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, 1300 to_addr_conv(sh, percpu)); 1301 if (unlikely(count == 1)) 1302 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); 1303 else 1304 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); 1305 } 1306 1307 static void 1308 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, 1309 struct dma_async_tx_descriptor *tx) 1310 { 1311 struct async_submit_ctl submit; 1312 struct page **blocks = percpu->scribble; 1313 int count, i; 1314 1315 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); 1316 1317 for (i = 0; i < sh->disks; i++) { 1318 if (sh->pd_idx == i || sh->qd_idx == i) 1319 continue; 1320 if (!test_bit(R5_Discard, &sh->dev[i].flags)) 1321 break; 1322 } 1323 if (i >= sh->disks) { 1324 atomic_inc(&sh->count); 1325 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); 1326 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); 1327 ops_complete_reconstruct(sh); 1328 return; 1329 } 1330 1331 count = set_syndrome_sources(blocks, sh); 1332 1333 atomic_inc(&sh->count); 1334 1335 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, 1336 sh, to_addr_conv(sh, percpu)); 1337 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); 1338 } 1339 1340 static void ops_complete_check(void *stripe_head_ref) 1341 { 1342 struct stripe_head *sh = stripe_head_ref; 1343 1344 pr_debug("%s: stripe %llu\n", __func__, 1345 (unsigned long long)sh->sector); 1346 1347 sh->check_state = check_state_check_result; 1348 set_bit(STRIPE_HANDLE, &sh->state); 1349 release_stripe(sh); 1350 } 1351 1352 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) 1353 { 1354 int disks = sh->disks; 1355 int pd_idx = sh->pd_idx; 1356 int qd_idx = sh->qd_idx; 1357 struct page *xor_dest; 1358 struct page **xor_srcs = percpu->scribble; 1359 struct dma_async_tx_descriptor *tx; 1360 struct async_submit_ctl submit; 1361 int count; 1362 int i; 1363 1364 pr_debug("%s: stripe %llu\n", __func__, 1365 (unsigned long long)sh->sector); 1366 1367 count = 0; 1368 xor_dest = sh->dev[pd_idx].page; 1369 xor_srcs[count++] = xor_dest; 1370 for (i = disks; i--; ) { 1371 if (i == pd_idx || i == qd_idx) 1372 continue; 1373 xor_srcs[count++] = sh->dev[i].page; 1374 } 1375 1376 init_async_submit(&submit, 0, NULL, NULL, NULL, 1377 to_addr_conv(sh, percpu)); 1378 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, 1379 &sh->ops.zero_sum_result, &submit); 1380 1381 atomic_inc(&sh->count); 1382 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); 1383 tx = async_trigger_callback(&submit); 1384 } 1385 1386 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) 1387 { 1388 struct page **srcs = percpu->scribble; 1389 struct async_submit_ctl submit; 1390 int count; 1391 1392 pr_debug("%s: stripe %llu checkp: %d\n", __func__, 1393 (unsigned long long)sh->sector, checkp); 1394 1395 count = set_syndrome_sources(srcs, sh); 1396 if (!checkp) 1397 srcs[count] = NULL; 1398 1399 atomic_inc(&sh->count); 1400 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, 1401 sh, to_addr_conv(sh, percpu)); 1402 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, 1403 &sh->ops.zero_sum_result, percpu->spare_page, &submit); 1404 } 1405 1406 static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request) 1407 { 1408 int overlap_clear = 0, i, disks = sh->disks; 1409 struct dma_async_tx_descriptor *tx = NULL; 1410 struct r5conf *conf = sh->raid_conf; 1411 int level = conf->level; 1412 struct raid5_percpu *percpu; 1413 unsigned long cpu; 1414 1415 cpu = get_cpu(); 1416 percpu = per_cpu_ptr(conf->percpu, cpu); 1417 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { 1418 ops_run_biofill(sh); 1419 overlap_clear++; 1420 } 1421 1422 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { 1423 if (level < 6) 1424 tx = ops_run_compute5(sh, percpu); 1425 else { 1426 if (sh->ops.target2 < 0 || sh->ops.target < 0) 1427 tx = ops_run_compute6_1(sh, percpu); 1428 else 1429 tx = ops_run_compute6_2(sh, percpu); 1430 } 1431 /* terminate the chain if reconstruct is not set to be run */ 1432 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) 1433 async_tx_ack(tx); 1434 } 1435 1436 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) 1437 tx = ops_run_prexor(sh, percpu, tx); 1438 1439 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { 1440 tx = ops_run_biodrain(sh, tx); 1441 overlap_clear++; 1442 } 1443 1444 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { 1445 if (level < 6) 1446 ops_run_reconstruct5(sh, percpu, tx); 1447 else 1448 ops_run_reconstruct6(sh, percpu, tx); 1449 } 1450 1451 if (test_bit(STRIPE_OP_CHECK, &ops_request)) { 1452 if (sh->check_state == check_state_run) 1453 ops_run_check_p(sh, percpu); 1454 else if (sh->check_state == check_state_run_q) 1455 ops_run_check_pq(sh, percpu, 0); 1456 else if (sh->check_state == check_state_run_pq) 1457 ops_run_check_pq(sh, percpu, 1); 1458 else 1459 BUG(); 1460 } 1461 1462 if (overlap_clear) 1463 for (i = disks; i--; ) { 1464 struct r5dev *dev = &sh->dev[i]; 1465 if (test_and_clear_bit(R5_Overlap, &dev->flags)) 1466 wake_up(&sh->raid_conf->wait_for_overlap); 1467 } 1468 put_cpu(); 1469 } 1470 1471 #ifdef CONFIG_MULTICORE_RAID456 1472 static void async_run_ops(void *param, async_cookie_t cookie) 1473 { 1474 struct stripe_head *sh = param; 1475 unsigned long ops_request = sh->ops.request; 1476 1477 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state); 1478 wake_up(&sh->ops.wait_for_ops); 1479 1480 __raid_run_ops(sh, ops_request); 1481 release_stripe(sh); 1482 } 1483 1484 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) 1485 { 1486 /* since handle_stripe can be called outside of raid5d context 1487 * we need to ensure sh->ops.request is de-staged before another 1488 * request arrives 1489 */ 1490 wait_event(sh->ops.wait_for_ops, 1491 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state)); 1492 sh->ops.request = ops_request; 1493 1494 atomic_inc(&sh->count); 1495 async_schedule(async_run_ops, sh); 1496 } 1497 #else 1498 #define raid_run_ops __raid_run_ops 1499 #endif 1500 1501 static int grow_one_stripe(struct r5conf *conf) 1502 { 1503 struct stripe_head *sh; 1504 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); 1505 if (!sh) 1506 return 0; 1507 1508 sh->raid_conf = conf; 1509 #ifdef CONFIG_MULTICORE_RAID456 1510 init_waitqueue_head(&sh->ops.wait_for_ops); 1511 #endif 1512 1513 spin_lock_init(&sh->stripe_lock); 1514 1515 if (grow_buffers(sh)) { 1516 shrink_buffers(sh); 1517 kmem_cache_free(conf->slab_cache, sh); 1518 return 0; 1519 } 1520 /* we just created an active stripe so... */ 1521 atomic_set(&sh->count, 1); 1522 atomic_inc(&conf->active_stripes); 1523 INIT_LIST_HEAD(&sh->lru); 1524 release_stripe(sh); 1525 return 1; 1526 } 1527 1528 static int grow_stripes(struct r5conf *conf, int num) 1529 { 1530 struct kmem_cache *sc; 1531 int devs = max(conf->raid_disks, conf->previous_raid_disks); 1532 1533 if (conf->mddev->gendisk) 1534 sprintf(conf->cache_name[0], 1535 "raid%d-%s", conf->level, mdname(conf->mddev)); 1536 else 1537 sprintf(conf->cache_name[0], 1538 "raid%d-%p", conf->level, conf->mddev); 1539 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); 1540 1541 conf->active_name = 0; 1542 sc = kmem_cache_create(conf->cache_name[conf->active_name], 1543 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), 1544 0, 0, NULL); 1545 if (!sc) 1546 return 1; 1547 conf->slab_cache = sc; 1548 conf->pool_size = devs; 1549 while (num--) 1550 if (!grow_one_stripe(conf)) 1551 return 1; 1552 return 0; 1553 } 1554 1555 /** 1556 * scribble_len - return the required size of the scribble region 1557 * @num - total number of disks in the array 1558 * 1559 * The size must be enough to contain: 1560 * 1/ a struct page pointer for each device in the array +2 1561 * 2/ room to convert each entry in (1) to its corresponding dma 1562 * (dma_map_page()) or page (page_address()) address. 1563 * 1564 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we 1565 * calculate over all devices (not just the data blocks), using zeros in place 1566 * of the P and Q blocks. 1567 */ 1568 static size_t scribble_len(int num) 1569 { 1570 size_t len; 1571 1572 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); 1573 1574 return len; 1575 } 1576 1577 static int resize_stripes(struct r5conf *conf, int newsize) 1578 { 1579 /* Make all the stripes able to hold 'newsize' devices. 1580 * New slots in each stripe get 'page' set to a new page. 1581 * 1582 * This happens in stages: 1583 * 1/ create a new kmem_cache and allocate the required number of 1584 * stripe_heads. 1585 * 2/ gather all the old stripe_heads and transfer the pages across 1586 * to the new stripe_heads. This will have the side effect of 1587 * freezing the array as once all stripe_heads have been collected, 1588 * no IO will be possible. Old stripe heads are freed once their 1589 * pages have been transferred over, and the old kmem_cache is 1590 * freed when all stripes are done. 1591 * 3/ reallocate conf->disks to be suitable bigger. If this fails, 1592 * we simple return a failre status - no need to clean anything up. 1593 * 4/ allocate new pages for the new slots in the new stripe_heads. 1594 * If this fails, we don't bother trying the shrink the 1595 * stripe_heads down again, we just leave them as they are. 1596 * As each stripe_head is processed the new one is released into 1597 * active service. 1598 * 1599 * Once step2 is started, we cannot afford to wait for a write, 1600 * so we use GFP_NOIO allocations. 1601 */ 1602 struct stripe_head *osh, *nsh; 1603 LIST_HEAD(newstripes); 1604 struct disk_info *ndisks; 1605 unsigned long cpu; 1606 int err; 1607 struct kmem_cache *sc; 1608 int i; 1609 1610 if (newsize <= conf->pool_size) 1611 return 0; /* never bother to shrink */ 1612 1613 err = md_allow_write(conf->mddev); 1614 if (err) 1615 return err; 1616 1617 /* Step 1 */ 1618 sc = kmem_cache_create(conf->cache_name[1-conf->active_name], 1619 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), 1620 0, 0, NULL); 1621 if (!sc) 1622 return -ENOMEM; 1623 1624 for (i = conf->max_nr_stripes; i; i--) { 1625 nsh = kmem_cache_zalloc(sc, GFP_KERNEL); 1626 if (!nsh) 1627 break; 1628 1629 nsh->raid_conf = conf; 1630 #ifdef CONFIG_MULTICORE_RAID456 1631 init_waitqueue_head(&nsh->ops.wait_for_ops); 1632 #endif 1633 spin_lock_init(&nsh->stripe_lock); 1634 1635 list_add(&nsh->lru, &newstripes); 1636 } 1637 if (i) { 1638 /* didn't get enough, give up */ 1639 while (!list_empty(&newstripes)) { 1640 nsh = list_entry(newstripes.next, struct stripe_head, lru); 1641 list_del(&nsh->lru); 1642 kmem_cache_free(sc, nsh); 1643 } 1644 kmem_cache_destroy(sc); 1645 return -ENOMEM; 1646 } 1647 /* Step 2 - Must use GFP_NOIO now. 1648 * OK, we have enough stripes, start collecting inactive 1649 * stripes and copying them over 1650 */ 1651 list_for_each_entry(nsh, &newstripes, lru) { 1652 spin_lock_irq(&conf->device_lock); 1653 wait_event_lock_irq(conf->wait_for_stripe, 1654 !list_empty(&conf->inactive_list), 1655 conf->device_lock); 1656 osh = get_free_stripe(conf); 1657 spin_unlock_irq(&conf->device_lock); 1658 atomic_set(&nsh->count, 1); 1659 for(i=0; i<conf->pool_size; i++) 1660 nsh->dev[i].page = osh->dev[i].page; 1661 for( ; i<newsize; i++) 1662 nsh->dev[i].page = NULL; 1663 kmem_cache_free(conf->slab_cache, osh); 1664 } 1665 kmem_cache_destroy(conf->slab_cache); 1666 1667 /* Step 3. 1668 * At this point, we are holding all the stripes so the array 1669 * is completely stalled, so now is a good time to resize 1670 * conf->disks and the scribble region 1671 */ 1672 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); 1673 if (ndisks) { 1674 for (i=0; i<conf->raid_disks; i++) 1675 ndisks[i] = conf->disks[i]; 1676 kfree(conf->disks); 1677 conf->disks = ndisks; 1678 } else 1679 err = -ENOMEM; 1680 1681 get_online_cpus(); 1682 conf->scribble_len = scribble_len(newsize); 1683 for_each_present_cpu(cpu) { 1684 struct raid5_percpu *percpu; 1685 void *scribble; 1686 1687 percpu = per_cpu_ptr(conf->percpu, cpu); 1688 scribble = kmalloc(conf->scribble_len, GFP_NOIO); 1689 1690 if (scribble) { 1691 kfree(percpu->scribble); 1692 percpu->scribble = scribble; 1693 } else { 1694 err = -ENOMEM; 1695 break; 1696 } 1697 } 1698 put_online_cpus(); 1699 1700 /* Step 4, return new stripes to service */ 1701 while(!list_empty(&newstripes)) { 1702 nsh = list_entry(newstripes.next, struct stripe_head, lru); 1703 list_del_init(&nsh->lru); 1704 1705 for (i=conf->raid_disks; i < newsize; i++) 1706 if (nsh->dev[i].page == NULL) { 1707 struct page *p = alloc_page(GFP_NOIO); 1708 nsh->dev[i].page = p; 1709 if (!p) 1710 err = -ENOMEM; 1711 } 1712 release_stripe(nsh); 1713 } 1714 /* critical section pass, GFP_NOIO no longer needed */ 1715 1716 conf->slab_cache = sc; 1717 conf->active_name = 1-conf->active_name; 1718 conf->pool_size = newsize; 1719 return err; 1720 } 1721 1722 static int drop_one_stripe(struct r5conf *conf) 1723 { 1724 struct stripe_head *sh; 1725 1726 spin_lock_irq(&conf->device_lock); 1727 sh = get_free_stripe(conf); 1728 spin_unlock_irq(&conf->device_lock); 1729 if (!sh) 1730 return 0; 1731 BUG_ON(atomic_read(&sh->count)); 1732 shrink_buffers(sh); 1733 kmem_cache_free(conf->slab_cache, sh); 1734 atomic_dec(&conf->active_stripes); 1735 return 1; 1736 } 1737 1738 static void shrink_stripes(struct r5conf *conf) 1739 { 1740 while (drop_one_stripe(conf)) 1741 ; 1742 1743 if (conf->slab_cache) 1744 kmem_cache_destroy(conf->slab_cache); 1745 conf->slab_cache = NULL; 1746 } 1747 1748 static void raid5_end_read_request(struct bio * bi, int error) 1749 { 1750 struct stripe_head *sh = bi->bi_private; 1751 struct r5conf *conf = sh->raid_conf; 1752 int disks = sh->disks, i; 1753 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); 1754 char b[BDEVNAME_SIZE]; 1755 struct md_rdev *rdev = NULL; 1756 sector_t s; 1757 1758 for (i=0 ; i<disks; i++) 1759 if (bi == &sh->dev[i].req) 1760 break; 1761 1762 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", 1763 (unsigned long long)sh->sector, i, atomic_read(&sh->count), 1764 uptodate); 1765 if (i == disks) { 1766 BUG(); 1767 return; 1768 } 1769 if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) 1770 /* If replacement finished while this request was outstanding, 1771 * 'replacement' might be NULL already. 1772 * In that case it moved down to 'rdev'. 1773 * rdev is not removed until all requests are finished. 1774 */ 1775 rdev = conf->disks[i].replacement; 1776 if (!rdev) 1777 rdev = conf->disks[i].rdev; 1778 1779 if (use_new_offset(conf, sh)) 1780 s = sh->sector + rdev->new_data_offset; 1781 else 1782 s = sh->sector + rdev->data_offset; 1783 if (uptodate) { 1784 set_bit(R5_UPTODATE, &sh->dev[i].flags); 1785 if (test_bit(R5_ReadError, &sh->dev[i].flags)) { 1786 /* Note that this cannot happen on a 1787 * replacement device. We just fail those on 1788 * any error 1789 */ 1790 printk_ratelimited( 1791 KERN_INFO 1792 "md/raid:%s: read error corrected" 1793 " (%lu sectors at %llu on %s)\n", 1794 mdname(conf->mddev), STRIPE_SECTORS, 1795 (unsigned long long)s, 1796 bdevname(rdev->bdev, b)); 1797 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); 1798 clear_bit(R5_ReadError, &sh->dev[i].flags); 1799 clear_bit(R5_ReWrite, &sh->dev[i].flags); 1800 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) 1801 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); 1802 1803 if (atomic_read(&rdev->read_errors)) 1804 atomic_set(&rdev->read_errors, 0); 1805 } else { 1806 const char *bdn = bdevname(rdev->bdev, b); 1807 int retry = 0; 1808 int set_bad = 0; 1809 1810 clear_bit(R5_UPTODATE, &sh->dev[i].flags); 1811 atomic_inc(&rdev->read_errors); 1812 if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) 1813 printk_ratelimited( 1814 KERN_WARNING 1815 "md/raid:%s: read error on replacement device " 1816 "(sector %llu on %s).\n", 1817 mdname(conf->mddev), 1818 (unsigned long long)s, 1819 bdn); 1820 else if (conf->mddev->degraded >= conf->max_degraded) { 1821 set_bad = 1; 1822 printk_ratelimited( 1823 KERN_WARNING 1824 "md/raid:%s: read error not correctable " 1825 "(sector %llu on %s).\n", 1826 mdname(conf->mddev), 1827 (unsigned long long)s, 1828 bdn); 1829 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { 1830 /* Oh, no!!! */ 1831 set_bad = 1; 1832 printk_ratelimited( 1833 KERN_WARNING 1834 "md/raid:%s: read error NOT corrected!! " 1835 "(sector %llu on %s).\n", 1836 mdname(conf->mddev), 1837 (unsigned long long)s, 1838 bdn); 1839 } else if (atomic_read(&rdev->read_errors) 1840 > conf->max_nr_stripes) 1841 printk(KERN_WARNING 1842 "md/raid:%s: Too many read errors, failing device %s.\n", 1843 mdname(conf->mddev), bdn); 1844 else 1845 retry = 1; 1846 if (retry) 1847 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { 1848 set_bit(R5_ReadError, &sh->dev[i].flags); 1849 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); 1850 } else 1851 set_bit(R5_ReadNoMerge, &sh->dev[i].flags); 1852 else { 1853 clear_bit(R5_ReadError, &sh->dev[i].flags); 1854 clear_bit(R5_ReWrite, &sh->dev[i].flags); 1855 if (!(set_bad 1856 && test_bit(In_sync, &rdev->flags) 1857 && rdev_set_badblocks( 1858 rdev, sh->sector, STRIPE_SECTORS, 0))) 1859 md_error(conf->mddev, rdev); 1860 } 1861 } 1862 rdev_dec_pending(rdev, conf->mddev); 1863 clear_bit(R5_LOCKED, &sh->dev[i].flags); 1864 set_bit(STRIPE_HANDLE, &sh->state); 1865 release_stripe(sh); 1866 } 1867 1868 static void raid5_end_write_request(struct bio *bi, int error) 1869 { 1870 struct stripe_head *sh = bi->bi_private; 1871 struct r5conf *conf = sh->raid_conf; 1872 int disks = sh->disks, i; 1873 struct md_rdev *uninitialized_var(rdev); 1874 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); 1875 sector_t first_bad; 1876 int bad_sectors; 1877 int replacement = 0; 1878 1879 for (i = 0 ; i < disks; i++) { 1880 if (bi == &sh->dev[i].req) { 1881 rdev = conf->disks[i].rdev; 1882 break; 1883 } 1884 if (bi == &sh->dev[i].rreq) { 1885 rdev = conf->disks[i].replacement; 1886 if (rdev) 1887 replacement = 1; 1888 else 1889 /* rdev was removed and 'replacement' 1890 * replaced it. rdev is not removed 1891 * until all requests are finished. 1892 */ 1893 rdev = conf->disks[i].rdev; 1894 break; 1895 } 1896 } 1897 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", 1898 (unsigned long long)sh->sector, i, atomic_read(&sh->count), 1899 uptodate); 1900 if (i == disks) { 1901 BUG(); 1902 return; 1903 } 1904 1905 if (replacement) { 1906 if (!uptodate) 1907 md_error(conf->mddev, rdev); 1908 else if (is_badblock(rdev, sh->sector, 1909 STRIPE_SECTORS, 1910 &first_bad, &bad_sectors)) 1911 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); 1912 } else { 1913 if (!uptodate) { 1914 set_bit(WriteErrorSeen, &rdev->flags); 1915 set_bit(R5_WriteError, &sh->dev[i].flags); 1916 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 1917 set_bit(MD_RECOVERY_NEEDED, 1918 &rdev->mddev->recovery); 1919 } else if (is_badblock(rdev, sh->sector, 1920 STRIPE_SECTORS, 1921 &first_bad, &bad_sectors)) 1922 set_bit(R5_MadeGood, &sh->dev[i].flags); 1923 } 1924 rdev_dec_pending(rdev, conf->mddev); 1925 1926 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) 1927 clear_bit(R5_LOCKED, &sh->dev[i].flags); 1928 set_bit(STRIPE_HANDLE, &sh->state); 1929 release_stripe(sh); 1930 } 1931 1932 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous); 1933 1934 static void raid5_build_block(struct stripe_head *sh, int i, int previous) 1935 { 1936 struct r5dev *dev = &sh->dev[i]; 1937 1938 bio_init(&dev->req); 1939 dev->req.bi_io_vec = &dev->vec; 1940 dev->req.bi_vcnt++; 1941 dev->req.bi_max_vecs++; 1942 dev->req.bi_private = sh; 1943 dev->vec.bv_page = dev->page; 1944 1945 bio_init(&dev->rreq); 1946 dev->rreq.bi_io_vec = &dev->rvec; 1947 dev->rreq.bi_vcnt++; 1948 dev->rreq.bi_max_vecs++; 1949 dev->rreq.bi_private = sh; 1950 dev->rvec.bv_page = dev->page; 1951 1952 dev->flags = 0; 1953 dev->sector = compute_blocknr(sh, i, previous); 1954 } 1955 1956 static void error(struct mddev *mddev, struct md_rdev *rdev) 1957 { 1958 char b[BDEVNAME_SIZE]; 1959 struct r5conf *conf = mddev->private; 1960 unsigned long flags; 1961 pr_debug("raid456: error called\n"); 1962 1963 spin_lock_irqsave(&conf->device_lock, flags); 1964 clear_bit(In_sync, &rdev->flags); 1965 mddev->degraded = calc_degraded(conf); 1966 spin_unlock_irqrestore(&conf->device_lock, flags); 1967 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 1968 1969 set_bit(Blocked, &rdev->flags); 1970 set_bit(Faulty, &rdev->flags); 1971 set_bit(MD_CHANGE_DEVS, &mddev->flags); 1972 printk(KERN_ALERT 1973 "md/raid:%s: Disk failure on %s, disabling device.\n" 1974 "md/raid:%s: Operation continuing on %d devices.\n", 1975 mdname(mddev), 1976 bdevname(rdev->bdev, b), 1977 mdname(mddev), 1978 conf->raid_disks - mddev->degraded); 1979 } 1980 1981 /* 1982 * Input: a 'big' sector number, 1983 * Output: index of the data and parity disk, and the sector # in them. 1984 */ 1985 static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, 1986 int previous, int *dd_idx, 1987 struct stripe_head *sh) 1988 { 1989 sector_t stripe, stripe2; 1990 sector_t chunk_number; 1991 unsigned int chunk_offset; 1992 int pd_idx, qd_idx; 1993 int ddf_layout = 0; 1994 sector_t new_sector; 1995 int algorithm = previous ? conf->prev_algo 1996 : conf->algorithm; 1997 int sectors_per_chunk = previous ? conf->prev_chunk_sectors 1998 : conf->chunk_sectors; 1999 int raid_disks = previous ? conf->previous_raid_disks 2000 : conf->raid_disks; 2001 int data_disks = raid_disks - conf->max_degraded; 2002 2003 /* First compute the information on this sector */ 2004 2005 /* 2006 * Compute the chunk number and the sector offset inside the chunk 2007 */ 2008 chunk_offset = sector_div(r_sector, sectors_per_chunk); 2009 chunk_number = r_sector; 2010 2011 /* 2012 * Compute the stripe number 2013 */ 2014 stripe = chunk_number; 2015 *dd_idx = sector_div(stripe, data_disks); 2016 stripe2 = stripe; 2017 /* 2018 * Select the parity disk based on the user selected algorithm. 2019 */ 2020 pd_idx = qd_idx = -1; 2021 switch(conf->level) { 2022 case 4: 2023 pd_idx = data_disks; 2024 break; 2025 case 5: 2026 switch (algorithm) { 2027 case ALGORITHM_LEFT_ASYMMETRIC: 2028 pd_idx = data_disks - sector_div(stripe2, raid_disks); 2029 if (*dd_idx >= pd_idx) 2030 (*dd_idx)++; 2031 break; 2032 case ALGORITHM_RIGHT_ASYMMETRIC: 2033 pd_idx = sector_div(stripe2, raid_disks); 2034 if (*dd_idx >= pd_idx) 2035 (*dd_idx)++; 2036 break; 2037 case ALGORITHM_LEFT_SYMMETRIC: 2038 pd_idx = data_disks - sector_div(stripe2, raid_disks); 2039 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; 2040 break; 2041 case ALGORITHM_RIGHT_SYMMETRIC: 2042 pd_idx = sector_div(stripe2, raid_disks); 2043 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; 2044 break; 2045 case ALGORITHM_PARITY_0: 2046 pd_idx = 0; 2047 (*dd_idx)++; 2048 break; 2049 case ALGORITHM_PARITY_N: 2050 pd_idx = data_disks; 2051 break; 2052 default: 2053 BUG(); 2054 } 2055 break; 2056 case 6: 2057 2058 switch (algorithm) { 2059 case ALGORITHM_LEFT_ASYMMETRIC: 2060 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 2061 qd_idx = pd_idx + 1; 2062 if (pd_idx == raid_disks-1) { 2063 (*dd_idx)++; /* Q D D D P */ 2064 qd_idx = 0; 2065 } else if (*dd_idx >= pd_idx) 2066 (*dd_idx) += 2; /* D D P Q D */ 2067 break; 2068 case ALGORITHM_RIGHT_ASYMMETRIC: 2069 pd_idx = sector_div(stripe2, raid_disks); 2070 qd_idx = pd_idx + 1; 2071 if (pd_idx == raid_disks-1) { 2072 (*dd_idx)++; /* Q D D D P */ 2073 qd_idx = 0; 2074 } else if (*dd_idx >= pd_idx) 2075 (*dd_idx) += 2; /* D D P Q D */ 2076 break; 2077 case ALGORITHM_LEFT_SYMMETRIC: 2078 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 2079 qd_idx = (pd_idx + 1) % raid_disks; 2080 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; 2081 break; 2082 case ALGORITHM_RIGHT_SYMMETRIC: 2083 pd_idx = sector_div(stripe2, raid_disks); 2084 qd_idx = (pd_idx + 1) % raid_disks; 2085 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; 2086 break; 2087 2088 case ALGORITHM_PARITY_0: 2089 pd_idx = 0; 2090 qd_idx = 1; 2091 (*dd_idx) += 2; 2092 break; 2093 case ALGORITHM_PARITY_N: 2094 pd_idx = data_disks; 2095 qd_idx = data_disks + 1; 2096 break; 2097 2098 case ALGORITHM_ROTATING_ZERO_RESTART: 2099 /* Exactly the same as RIGHT_ASYMMETRIC, but or 2100 * of blocks for computing Q is different. 2101 */ 2102 pd_idx = sector_div(stripe2, raid_disks); 2103 qd_idx = pd_idx + 1; 2104 if (pd_idx == raid_disks-1) { 2105 (*dd_idx)++; /* Q D D D P */ 2106 qd_idx = 0; 2107 } else if (*dd_idx >= pd_idx) 2108 (*dd_idx) += 2; /* D D P Q D */ 2109 ddf_layout = 1; 2110 break; 2111 2112 case ALGORITHM_ROTATING_N_RESTART: 2113 /* Same a left_asymmetric, by first stripe is 2114 * D D D P Q rather than 2115 * Q D D D P 2116 */ 2117 stripe2 += 1; 2118 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 2119 qd_idx = pd_idx + 1; 2120 if (pd_idx == raid_disks-1) { 2121 (*dd_idx)++; /* Q D D D P */ 2122 qd_idx = 0; 2123 } else if (*dd_idx >= pd_idx) 2124 (*dd_idx) += 2; /* D D P Q D */ 2125 ddf_layout = 1; 2126 break; 2127 2128 case ALGORITHM_ROTATING_N_CONTINUE: 2129 /* Same as left_symmetric but Q is before P */ 2130 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 2131 qd_idx = (pd_idx + raid_disks - 1) % raid_disks; 2132 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; 2133 ddf_layout = 1; 2134 break; 2135 2136 case ALGORITHM_LEFT_ASYMMETRIC_6: 2137 /* RAID5 left_asymmetric, with Q on last device */ 2138 pd_idx = data_disks - sector_div(stripe2, raid_disks-1); 2139 if (*dd_idx >= pd_idx) 2140 (*dd_idx)++; 2141 qd_idx = raid_disks - 1; 2142 break; 2143 2144 case ALGORITHM_RIGHT_ASYMMETRIC_6: 2145 pd_idx = sector_div(stripe2, raid_disks-1); 2146 if (*dd_idx >= pd_idx) 2147 (*dd_idx)++; 2148 qd_idx = raid_disks - 1; 2149 break; 2150 2151 case ALGORITHM_LEFT_SYMMETRIC_6: 2152 pd_idx = data_disks - sector_div(stripe2, raid_disks-1); 2153 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); 2154 qd_idx = raid_disks - 1; 2155 break; 2156 2157 case ALGORITHM_RIGHT_SYMMETRIC_6: 2158 pd_idx = sector_div(stripe2, raid_disks-1); 2159 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); 2160 qd_idx = raid_disks - 1; 2161 break; 2162 2163 case ALGORITHM_PARITY_0_6: 2164 pd_idx = 0; 2165 (*dd_idx)++; 2166 qd_idx = raid_disks - 1; 2167 break; 2168 2169 default: 2170 BUG(); 2171 } 2172 break; 2173 } 2174 2175 if (sh) { 2176 sh->pd_idx = pd_idx; 2177 sh->qd_idx = qd_idx; 2178 sh->ddf_layout = ddf_layout; 2179 } 2180 /* 2181 * Finally, compute the new sector number 2182 */ 2183 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; 2184 return new_sector; 2185 } 2186 2187 2188 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous) 2189 { 2190 struct r5conf *conf = sh->raid_conf; 2191 int raid_disks = sh->disks; 2192 int data_disks = raid_disks - conf->max_degraded; 2193 sector_t new_sector = sh->sector, check; 2194 int sectors_per_chunk = previous ? conf->prev_chunk_sectors 2195 : conf->chunk_sectors; 2196 int algorithm = previous ? conf->prev_algo 2197 : conf->algorithm; 2198 sector_t stripe; 2199 int chunk_offset; 2200 sector_t chunk_number; 2201 int dummy1, dd_idx = i; 2202 sector_t r_sector; 2203 struct stripe_head sh2; 2204 2205 2206 chunk_offset = sector_div(new_sector, sectors_per_chunk); 2207 stripe = new_sector; 2208 2209 if (i == sh->pd_idx) 2210 return 0; 2211 switch(conf->level) { 2212 case 4: break; 2213 case 5: 2214 switch (algorithm) { 2215 case ALGORITHM_LEFT_ASYMMETRIC: 2216 case ALGORITHM_RIGHT_ASYMMETRIC: 2217 if (i > sh->pd_idx) 2218 i--; 2219 break; 2220 case ALGORITHM_LEFT_SYMMETRIC: 2221 case ALGORITHM_RIGHT_SYMMETRIC: 2222 if (i < sh->pd_idx) 2223 i += raid_disks; 2224 i -= (sh->pd_idx + 1); 2225 break; 2226 case ALGORITHM_PARITY_0: 2227 i -= 1; 2228 break; 2229 case ALGORITHM_PARITY_N: 2230 break; 2231 default: 2232 BUG(); 2233 } 2234 break; 2235 case 6: 2236 if (i == sh->qd_idx) 2237 return 0; /* It is the Q disk */ 2238 switch (algorithm) { 2239 case ALGORITHM_LEFT_ASYMMETRIC: 2240 case ALGORITHM_RIGHT_ASYMMETRIC: 2241 case ALGORITHM_ROTATING_ZERO_RESTART: 2242 case ALGORITHM_ROTATING_N_RESTART: 2243 if (sh->pd_idx == raid_disks-1) 2244 i--; /* Q D D D P */ 2245 else if (i > sh->pd_idx) 2246 i -= 2; /* D D P Q D */ 2247 break; 2248 case ALGORITHM_LEFT_SYMMETRIC: 2249 case ALGORITHM_RIGHT_SYMMETRIC: 2250 if (sh->pd_idx == raid_disks-1) 2251 i--; /* Q D D D P */ 2252 else { 2253 /* D D P Q D */ 2254 if (i < sh->pd_idx) 2255 i += raid_disks; 2256 i -= (sh->pd_idx + 2); 2257 } 2258 break; 2259 case ALGORITHM_PARITY_0: 2260 i -= 2; 2261 break; 2262 case ALGORITHM_PARITY_N: 2263 break; 2264 case ALGORITHM_ROTATING_N_CONTINUE: 2265 /* Like left_symmetric, but P is before Q */ 2266 if (sh->pd_idx == 0) 2267 i--; /* P D D D Q */ 2268 else { 2269 /* D D Q P D */ 2270 if (i < sh->pd_idx) 2271 i += raid_disks; 2272 i -= (sh->pd_idx + 1); 2273 } 2274 break; 2275 case ALGORITHM_LEFT_ASYMMETRIC_6: 2276 case ALGORITHM_RIGHT_ASYMMETRIC_6: 2277 if (i > sh->pd_idx) 2278 i--; 2279 break; 2280 case ALGORITHM_LEFT_SYMMETRIC_6: 2281 case ALGORITHM_RIGHT_SYMMETRIC_6: 2282 if (i < sh->pd_idx) 2283 i += data_disks + 1; 2284 i -= (sh->pd_idx + 1); 2285 break; 2286 case ALGORITHM_PARITY_0_6: 2287 i -= 1; 2288 break; 2289 default: 2290 BUG(); 2291 } 2292 break; 2293 } 2294 2295 chunk_number = stripe * data_disks + i; 2296 r_sector = chunk_number * sectors_per_chunk + chunk_offset; 2297 2298 check = raid5_compute_sector(conf, r_sector, 2299 previous, &dummy1, &sh2); 2300 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx 2301 || sh2.qd_idx != sh->qd_idx) { 2302 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", 2303 mdname(conf->mddev)); 2304 return 0; 2305 } 2306 return r_sector; 2307 } 2308 2309 2310 static void 2311 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, 2312 int rcw, int expand) 2313 { 2314 int i, pd_idx = sh->pd_idx, disks = sh->disks; 2315 struct r5conf *conf = sh->raid_conf; 2316 int level = conf->level; 2317 2318 if (rcw) { 2319 /* if we are not expanding this is a proper write request, and 2320 * there will be bios with new data to be drained into the 2321 * stripe cache 2322 */ 2323 if (!expand) { 2324 sh->reconstruct_state = reconstruct_state_drain_run; 2325 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); 2326 } else 2327 sh->reconstruct_state = reconstruct_state_run; 2328 2329 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); 2330 2331 for (i = disks; i--; ) { 2332 struct r5dev *dev = &sh->dev[i]; 2333 2334 if (dev->towrite) { 2335 set_bit(R5_LOCKED, &dev->flags); 2336 set_bit(R5_Wantdrain, &dev->flags); 2337 if (!expand) 2338 clear_bit(R5_UPTODATE, &dev->flags); 2339 s->locked++; 2340 } 2341 } 2342 if (s->locked + conf->max_degraded == disks) 2343 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) 2344 atomic_inc(&conf->pending_full_writes); 2345 } else { 2346 BUG_ON(level == 6); 2347 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || 2348 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); 2349 2350 sh->reconstruct_state = reconstruct_state_prexor_drain_run; 2351 set_bit(STRIPE_OP_PREXOR, &s->ops_request); 2352 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); 2353 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); 2354 2355 for (i = disks; i--; ) { 2356 struct r5dev *dev = &sh->dev[i]; 2357 if (i == pd_idx) 2358 continue; 2359 2360 if (dev->towrite && 2361 (test_bit(R5_UPTODATE, &dev->flags) || 2362 test_bit(R5_Wantcompute, &dev->flags))) { 2363 set_bit(R5_Wantdrain, &dev->flags); 2364 set_bit(R5_LOCKED, &dev->flags); 2365 clear_bit(R5_UPTODATE, &dev->flags); 2366 s->locked++; 2367 } 2368 } 2369 } 2370 2371 /* keep the parity disk(s) locked while asynchronous operations 2372 * are in flight 2373 */ 2374 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); 2375 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); 2376 s->locked++; 2377 2378 if (level == 6) { 2379 int qd_idx = sh->qd_idx; 2380 struct r5dev *dev = &sh->dev[qd_idx]; 2381 2382 set_bit(R5_LOCKED, &dev->flags); 2383 clear_bit(R5_UPTODATE, &dev->flags); 2384 s->locked++; 2385 } 2386 2387 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", 2388 __func__, (unsigned long long)sh->sector, 2389 s->locked, s->ops_request); 2390 } 2391 2392 /* 2393 * Each stripe/dev can have one or more bion attached. 2394 * toread/towrite point to the first in a chain. 2395 * The bi_next chain must be in order. 2396 */ 2397 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) 2398 { 2399 struct bio **bip; 2400 struct r5conf *conf = sh->raid_conf; 2401 int firstwrite=0; 2402 2403 pr_debug("adding bi b#%llu to stripe s#%llu\n", 2404 (unsigned long long)bi->bi_sector, 2405 (unsigned long long)sh->sector); 2406 2407 /* 2408 * If several bio share a stripe. The bio bi_phys_segments acts as a 2409 * reference count to avoid race. The reference count should already be 2410 * increased before this function is called (for example, in 2411 * make_request()), so other bio sharing this stripe will not free the 2412 * stripe. If a stripe is owned by one stripe, the stripe lock will 2413 * protect it. 2414 */ 2415 spin_lock_irq(&sh->stripe_lock); 2416 if (forwrite) { 2417 bip = &sh->dev[dd_idx].towrite; 2418 if (*bip == NULL) 2419 firstwrite = 1; 2420 } else 2421 bip = &sh->dev[dd_idx].toread; 2422 while (*bip && (*bip)->bi_sector < bi->bi_sector) { 2423 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) 2424 goto overlap; 2425 bip = & (*bip)->bi_next; 2426 } 2427 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) 2428 goto overlap; 2429 2430 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); 2431 if (*bip) 2432 bi->bi_next = *bip; 2433 *bip = bi; 2434 raid5_inc_bi_active_stripes(bi); 2435 2436 if (forwrite) { 2437 /* check if page is covered */ 2438 sector_t sector = sh->dev[dd_idx].sector; 2439 for (bi=sh->dev[dd_idx].towrite; 2440 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && 2441 bi && bi->bi_sector <= sector; 2442 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { 2443 if (bi->bi_sector + (bi->bi_size>>9) >= sector) 2444 sector = bi->bi_sector + (bi->bi_size>>9); 2445 } 2446 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) 2447 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); 2448 } 2449 2450 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", 2451 (unsigned long long)(*bip)->bi_sector, 2452 (unsigned long long)sh->sector, dd_idx); 2453 spin_unlock_irq(&sh->stripe_lock); 2454 2455 if (conf->mddev->bitmap && firstwrite) { 2456 bitmap_startwrite(conf->mddev->bitmap, sh->sector, 2457 STRIPE_SECTORS, 0); 2458 sh->bm_seq = conf->seq_flush+1; 2459 set_bit(STRIPE_BIT_DELAY, &sh->state); 2460 } 2461 return 1; 2462 2463 overlap: 2464 set_bit(R5_Overlap, &sh->dev[dd_idx].flags); 2465 spin_unlock_irq(&sh->stripe_lock); 2466 return 0; 2467 } 2468 2469 static void end_reshape(struct r5conf *conf); 2470 2471 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, 2472 struct stripe_head *sh) 2473 { 2474 int sectors_per_chunk = 2475 previous ? conf->prev_chunk_sectors : conf->chunk_sectors; 2476 int dd_idx; 2477 int chunk_offset = sector_div(stripe, sectors_per_chunk); 2478 int disks = previous ? conf->previous_raid_disks : conf->raid_disks; 2479 2480 raid5_compute_sector(conf, 2481 stripe * (disks - conf->max_degraded) 2482 *sectors_per_chunk + chunk_offset, 2483 previous, 2484 &dd_idx, sh); 2485 } 2486 2487 static void 2488 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, 2489 struct stripe_head_state *s, int disks, 2490 struct bio **return_bi) 2491 { 2492 int i; 2493 for (i = disks; i--; ) { 2494 struct bio *bi; 2495 int bitmap_end = 0; 2496 2497 if (test_bit(R5_ReadError, &sh->dev[i].flags)) { 2498 struct md_rdev *rdev; 2499 rcu_read_lock(); 2500 rdev = rcu_dereference(conf->disks[i].rdev); 2501 if (rdev && test_bit(In_sync, &rdev->flags)) 2502 atomic_inc(&rdev->nr_pending); 2503 else 2504 rdev = NULL; 2505 rcu_read_unlock(); 2506 if (rdev) { 2507 if (!rdev_set_badblocks( 2508 rdev, 2509 sh->sector, 2510 STRIPE_SECTORS, 0)) 2511 md_error(conf->mddev, rdev); 2512 rdev_dec_pending(rdev, conf->mddev); 2513 } 2514 } 2515 spin_lock_irq(&sh->stripe_lock); 2516 /* fail all writes first */ 2517 bi = sh->dev[i].towrite; 2518 sh->dev[i].towrite = NULL; 2519 spin_unlock_irq(&sh->stripe_lock); 2520 if (bi) 2521 bitmap_end = 1; 2522 2523 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 2524 wake_up(&conf->wait_for_overlap); 2525 2526 while (bi && bi->bi_sector < 2527 sh->dev[i].sector + STRIPE_SECTORS) { 2528 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); 2529 clear_bit(BIO_UPTODATE, &bi->bi_flags); 2530 if (!raid5_dec_bi_active_stripes(bi)) { 2531 md_write_end(conf->mddev); 2532 bi->bi_next = *return_bi; 2533 *return_bi = bi; 2534 } 2535 bi = nextbi; 2536 } 2537 if (bitmap_end) 2538 bitmap_endwrite(conf->mddev->bitmap, sh->sector, 2539 STRIPE_SECTORS, 0, 0); 2540 bitmap_end = 0; 2541 /* and fail all 'written' */ 2542 bi = sh->dev[i].written; 2543 sh->dev[i].written = NULL; 2544 if (bi) bitmap_end = 1; 2545 while (bi && bi->bi_sector < 2546 sh->dev[i].sector + STRIPE_SECTORS) { 2547 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); 2548 clear_bit(BIO_UPTODATE, &bi->bi_flags); 2549 if (!raid5_dec_bi_active_stripes(bi)) { 2550 md_write_end(conf->mddev); 2551 bi->bi_next = *return_bi; 2552 *return_bi = bi; 2553 } 2554 bi = bi2; 2555 } 2556 2557 /* fail any reads if this device is non-operational and 2558 * the data has not reached the cache yet. 2559 */ 2560 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && 2561 (!test_bit(R5_Insync, &sh->dev[i].flags) || 2562 test_bit(R5_ReadError, &sh->dev[i].flags))) { 2563 spin_lock_irq(&sh->stripe_lock); 2564 bi = sh->dev[i].toread; 2565 sh->dev[i].toread = NULL; 2566 spin_unlock_irq(&sh->stripe_lock); 2567 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 2568 wake_up(&conf->wait_for_overlap); 2569 while (bi && bi->bi_sector < 2570 sh->dev[i].sector + STRIPE_SECTORS) { 2571 struct bio *nextbi = 2572 r5_next_bio(bi, sh->dev[i].sector); 2573 clear_bit(BIO_UPTODATE, &bi->bi_flags); 2574 if (!raid5_dec_bi_active_stripes(bi)) { 2575 bi->bi_next = *return_bi; 2576 *return_bi = bi; 2577 } 2578 bi = nextbi; 2579 } 2580 } 2581 if (bitmap_end) 2582 bitmap_endwrite(conf->mddev->bitmap, sh->sector, 2583 STRIPE_SECTORS, 0, 0); 2584 /* If we were in the middle of a write the parity block might 2585 * still be locked - so just clear all R5_LOCKED flags 2586 */ 2587 clear_bit(R5_LOCKED, &sh->dev[i].flags); 2588 } 2589 2590 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) 2591 if (atomic_dec_and_test(&conf->pending_full_writes)) 2592 md_wakeup_thread(conf->mddev->thread); 2593 } 2594 2595 static void 2596 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, 2597 struct stripe_head_state *s) 2598 { 2599 int abort = 0; 2600 int i; 2601 2602 clear_bit(STRIPE_SYNCING, &sh->state); 2603 s->syncing = 0; 2604 s->replacing = 0; 2605 /* There is nothing more to do for sync/check/repair. 2606 * Don't even need to abort as that is handled elsewhere 2607 * if needed, and not always wanted e.g. if there is a known 2608 * bad block here. 2609 * For recover/replace we need to record a bad block on all 2610 * non-sync devices, or abort the recovery 2611 */ 2612 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { 2613 /* During recovery devices cannot be removed, so 2614 * locking and refcounting of rdevs is not needed 2615 */ 2616 for (i = 0; i < conf->raid_disks; i++) { 2617 struct md_rdev *rdev = conf->disks[i].rdev; 2618 if (rdev 2619 && !test_bit(Faulty, &rdev->flags) 2620 && !test_bit(In_sync, &rdev->flags) 2621 && !rdev_set_badblocks(rdev, sh->sector, 2622 STRIPE_SECTORS, 0)) 2623 abort = 1; 2624 rdev = conf->disks[i].replacement; 2625 if (rdev 2626 && !test_bit(Faulty, &rdev->flags) 2627 && !test_bit(In_sync, &rdev->flags) 2628 && !rdev_set_badblocks(rdev, sh->sector, 2629 STRIPE_SECTORS, 0)) 2630 abort = 1; 2631 } 2632 if (abort) 2633 conf->recovery_disabled = 2634 conf->mddev->recovery_disabled; 2635 } 2636 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); 2637 } 2638 2639 static int want_replace(struct stripe_head *sh, int disk_idx) 2640 { 2641 struct md_rdev *rdev; 2642 int rv = 0; 2643 /* Doing recovery so rcu locking not required */ 2644 rdev = sh->raid_conf->disks[disk_idx].replacement; 2645 if (rdev 2646 && !test_bit(Faulty, &rdev->flags) 2647 && !test_bit(In_sync, &rdev->flags) 2648 && (rdev->recovery_offset <= sh->sector 2649 || rdev->mddev->recovery_cp <= sh->sector)) 2650 rv = 1; 2651 2652 return rv; 2653 } 2654 2655 /* fetch_block - checks the given member device to see if its data needs 2656 * to be read or computed to satisfy a request. 2657 * 2658 * Returns 1 when no more member devices need to be checked, otherwise returns 2659 * 0 to tell the loop in handle_stripe_fill to continue 2660 */ 2661 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, 2662 int disk_idx, int disks) 2663 { 2664 struct r5dev *dev = &sh->dev[disk_idx]; 2665 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], 2666 &sh->dev[s->failed_num[1]] }; 2667 2668 /* is the data in this block needed, and can we get it? */ 2669 if (!test_bit(R5_LOCKED, &dev->flags) && 2670 !test_bit(R5_UPTODATE, &dev->flags) && 2671 (dev->toread || 2672 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || 2673 s->syncing || s->expanding || 2674 (s->replacing && want_replace(sh, disk_idx)) || 2675 (s->failed >= 1 && fdev[0]->toread) || 2676 (s->failed >= 2 && fdev[1]->toread) || 2677 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && 2678 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || 2679 (sh->raid_conf->level == 6 && s->failed && s->to_write))) { 2680 /* we would like to get this block, possibly by computing it, 2681 * otherwise read it if the backing disk is insync 2682 */ 2683 BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); 2684 BUG_ON(test_bit(R5_Wantread, &dev->flags)); 2685 if ((s->uptodate == disks - 1) && 2686 (s->failed && (disk_idx == s->failed_num[0] || 2687 disk_idx == s->failed_num[1]))) { 2688 /* have disk failed, and we're requested to fetch it; 2689 * do compute it 2690 */ 2691 pr_debug("Computing stripe %llu block %d\n", 2692 (unsigned long long)sh->sector, disk_idx); 2693 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 2694 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 2695 set_bit(R5_Wantcompute, &dev->flags); 2696 sh->ops.target = disk_idx; 2697 sh->ops.target2 = -1; /* no 2nd target */ 2698 s->req_compute = 1; 2699 /* Careful: from this point on 'uptodate' is in the eye 2700 * of raid_run_ops which services 'compute' operations 2701 * before writes. R5_Wantcompute flags a block that will 2702 * be R5_UPTODATE by the time it is needed for a 2703 * subsequent operation. 2704 */ 2705 s->uptodate++; 2706 return 1; 2707 } else if (s->uptodate == disks-2 && s->failed >= 2) { 2708 /* Computing 2-failure is *very* expensive; only 2709 * do it if failed >= 2 2710 */ 2711 int other; 2712 for (other = disks; other--; ) { 2713 if (other == disk_idx) 2714 continue; 2715 if (!test_bit(R5_UPTODATE, 2716 &sh->dev[other].flags)) 2717 break; 2718 } 2719 BUG_ON(other < 0); 2720 pr_debug("Computing stripe %llu blocks %d,%d\n", 2721 (unsigned long long)sh->sector, 2722 disk_idx, other); 2723 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 2724 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 2725 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); 2726 set_bit(R5_Wantcompute, &sh->dev[other].flags); 2727 sh->ops.target = disk_idx; 2728 sh->ops.target2 = other; 2729 s->uptodate += 2; 2730 s->req_compute = 1; 2731 return 1; 2732 } else if (test_bit(R5_Insync, &dev->flags)) { 2733 set_bit(R5_LOCKED, &dev->flags); 2734 set_bit(R5_Wantread, &dev->flags); 2735 s->locked++; 2736 pr_debug("Reading block %d (sync=%d)\n", 2737 disk_idx, s->syncing); 2738 } 2739 } 2740 2741 return 0; 2742 } 2743 2744 /** 2745 * handle_stripe_fill - read or compute data to satisfy pending requests. 2746 */ 2747 static void handle_stripe_fill(struct stripe_head *sh, 2748 struct stripe_head_state *s, 2749 int disks) 2750 { 2751 int i; 2752 2753 /* look for blocks to read/compute, skip this if a compute 2754 * is already in flight, or if the stripe contents are in the 2755 * midst of changing due to a write 2756 */ 2757 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && 2758 !sh->reconstruct_state) 2759 for (i = disks; i--; ) 2760 if (fetch_block(sh, s, i, disks)) 2761 break; 2762 set_bit(STRIPE_HANDLE, &sh->state); 2763 } 2764 2765 2766 /* handle_stripe_clean_event 2767 * any written block on an uptodate or failed drive can be returned. 2768 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but 2769 * never LOCKED, so we don't need to test 'failed' directly. 2770 */ 2771 static void handle_stripe_clean_event(struct r5conf *conf, 2772 struct stripe_head *sh, int disks, struct bio **return_bi) 2773 { 2774 int i; 2775 struct r5dev *dev; 2776 2777 for (i = disks; i--; ) 2778 if (sh->dev[i].written) { 2779 dev = &sh->dev[i]; 2780 if (!test_bit(R5_LOCKED, &dev->flags) && 2781 (test_bit(R5_UPTODATE, &dev->flags) || 2782 test_bit(R5_Discard, &dev->flags))) { 2783 /* We can return any write requests */ 2784 struct bio *wbi, *wbi2; 2785 pr_debug("Return write for disc %d\n", i); 2786 if (test_and_clear_bit(R5_Discard, &dev->flags)) 2787 clear_bit(R5_UPTODATE, &dev->flags); 2788 wbi = dev->written; 2789 dev->written = NULL; 2790 while (wbi && wbi->bi_sector < 2791 dev->sector + STRIPE_SECTORS) { 2792 wbi2 = r5_next_bio(wbi, dev->sector); 2793 if (!raid5_dec_bi_active_stripes(wbi)) { 2794 md_write_end(conf->mddev); 2795 wbi->bi_next = *return_bi; 2796 *return_bi = wbi; 2797 } 2798 wbi = wbi2; 2799 } 2800 bitmap_endwrite(conf->mddev->bitmap, sh->sector, 2801 STRIPE_SECTORS, 2802 !test_bit(STRIPE_DEGRADED, &sh->state), 2803 0); 2804 } 2805 } else if (test_bit(R5_Discard, &sh->dev[i].flags)) 2806 clear_bit(R5_Discard, &sh->dev[i].flags); 2807 2808 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) 2809 if (atomic_dec_and_test(&conf->pending_full_writes)) 2810 md_wakeup_thread(conf->mddev->thread); 2811 } 2812 2813 static void handle_stripe_dirtying(struct r5conf *conf, 2814 struct stripe_head *sh, 2815 struct stripe_head_state *s, 2816 int disks) 2817 { 2818 int rmw = 0, rcw = 0, i; 2819 sector_t recovery_cp = conf->mddev->recovery_cp; 2820 2821 /* RAID6 requires 'rcw' in current implementation. 2822 * Otherwise, check whether resync is now happening or should start. 2823 * If yes, then the array is dirty (after unclean shutdown or 2824 * initial creation), so parity in some stripes might be inconsistent. 2825 * In this case, we need to always do reconstruct-write, to ensure 2826 * that in case of drive failure or read-error correction, we 2827 * generate correct data from the parity. 2828 */ 2829 if (conf->max_degraded == 2 || 2830 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) { 2831 /* Calculate the real rcw later - for now make it 2832 * look like rcw is cheaper 2833 */ 2834 rcw = 1; rmw = 2; 2835 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n", 2836 conf->max_degraded, (unsigned long long)recovery_cp, 2837 (unsigned long long)sh->sector); 2838 } else for (i = disks; i--; ) { 2839 /* would I have to read this buffer for read_modify_write */ 2840 struct r5dev *dev = &sh->dev[i]; 2841 if ((dev->towrite || i == sh->pd_idx) && 2842 !test_bit(R5_LOCKED, &dev->flags) && 2843 !(test_bit(R5_UPTODATE, &dev->flags) || 2844 test_bit(R5_Wantcompute, &dev->flags))) { 2845 if (test_bit(R5_Insync, &dev->flags)) 2846 rmw++; 2847 else 2848 rmw += 2*disks; /* cannot read it */ 2849 } 2850 /* Would I have to read this buffer for reconstruct_write */ 2851 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && 2852 !test_bit(R5_LOCKED, &dev->flags) && 2853 !(test_bit(R5_UPTODATE, &dev->flags) || 2854 test_bit(R5_Wantcompute, &dev->flags))) { 2855 if (test_bit(R5_Insync, &dev->flags)) rcw++; 2856 else 2857 rcw += 2*disks; 2858 } 2859 } 2860 pr_debug("for sector %llu, rmw=%d rcw=%d\n", 2861 (unsigned long long)sh->sector, rmw, rcw); 2862 set_bit(STRIPE_HANDLE, &sh->state); 2863 if (rmw < rcw && rmw > 0) { 2864 /* prefer read-modify-write, but need to get some data */ 2865 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d", 2866 (unsigned long long)sh->sector, rmw); 2867 for (i = disks; i--; ) { 2868 struct r5dev *dev = &sh->dev[i]; 2869 if ((dev->towrite || i == sh->pd_idx) && 2870 !test_bit(R5_LOCKED, &dev->flags) && 2871 !(test_bit(R5_UPTODATE, &dev->flags) || 2872 test_bit(R5_Wantcompute, &dev->flags)) && 2873 test_bit(R5_Insync, &dev->flags)) { 2874 if ( 2875 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { 2876 pr_debug("Read_old block " 2877 "%d for r-m-w\n", i); 2878 set_bit(R5_LOCKED, &dev->flags); 2879 set_bit(R5_Wantread, &dev->flags); 2880 s->locked++; 2881 } else { 2882 set_bit(STRIPE_DELAYED, &sh->state); 2883 set_bit(STRIPE_HANDLE, &sh->state); 2884 } 2885 } 2886 } 2887 } 2888 if (rcw <= rmw && rcw > 0) { 2889 /* want reconstruct write, but need to get some data */ 2890 int qread =0; 2891 rcw = 0; 2892 for (i = disks; i--; ) { 2893 struct r5dev *dev = &sh->dev[i]; 2894 if (!test_bit(R5_OVERWRITE, &dev->flags) && 2895 i != sh->pd_idx && i != sh->qd_idx && 2896 !test_bit(R5_LOCKED, &dev->flags) && 2897 !(test_bit(R5_UPTODATE, &dev->flags) || 2898 test_bit(R5_Wantcompute, &dev->flags))) { 2899 rcw++; 2900 if (!test_bit(R5_Insync, &dev->flags)) 2901 continue; /* it's a failed drive */ 2902 if ( 2903 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { 2904 pr_debug("Read_old block " 2905 "%d for Reconstruct\n", i); 2906 set_bit(R5_LOCKED, &dev->flags); 2907 set_bit(R5_Wantread, &dev->flags); 2908 s->locked++; 2909 qread++; 2910 } else { 2911 set_bit(STRIPE_DELAYED, &sh->state); 2912 set_bit(STRIPE_HANDLE, &sh->state); 2913 } 2914 } 2915 } 2916 if (rcw) 2917 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", 2918 (unsigned long long)sh->sector, 2919 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); 2920 } 2921 /* now if nothing is locked, and if we have enough data, 2922 * we can start a write request 2923 */ 2924 /* since handle_stripe can be called at any time we need to handle the 2925 * case where a compute block operation has been submitted and then a 2926 * subsequent call wants to start a write request. raid_run_ops only 2927 * handles the case where compute block and reconstruct are requested 2928 * simultaneously. If this is not the case then new writes need to be 2929 * held off until the compute completes. 2930 */ 2931 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && 2932 (s->locked == 0 && (rcw == 0 || rmw == 0) && 2933 !test_bit(STRIPE_BIT_DELAY, &sh->state))) 2934 schedule_reconstruction(sh, s, rcw == 0, 0); 2935 } 2936 2937 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, 2938 struct stripe_head_state *s, int disks) 2939 { 2940 struct r5dev *dev = NULL; 2941 2942 set_bit(STRIPE_HANDLE, &sh->state); 2943 2944 switch (sh->check_state) { 2945 case check_state_idle: 2946 /* start a new check operation if there are no failures */ 2947 if (s->failed == 0) { 2948 BUG_ON(s->uptodate != disks); 2949 sh->check_state = check_state_run; 2950 set_bit(STRIPE_OP_CHECK, &s->ops_request); 2951 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); 2952 s->uptodate--; 2953 break; 2954 } 2955 dev = &sh->dev[s->failed_num[0]]; 2956 /* fall through */ 2957 case check_state_compute_result: 2958 sh->check_state = check_state_idle; 2959 if (!dev) 2960 dev = &sh->dev[sh->pd_idx]; 2961 2962 /* check that a write has not made the stripe insync */ 2963 if (test_bit(STRIPE_INSYNC, &sh->state)) 2964 break; 2965 2966 /* either failed parity check, or recovery is happening */ 2967 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); 2968 BUG_ON(s->uptodate != disks); 2969 2970 set_bit(R5_LOCKED, &dev->flags); 2971 s->locked++; 2972 set_bit(R5_Wantwrite, &dev->flags); 2973 2974 clear_bit(STRIPE_DEGRADED, &sh->state); 2975 set_bit(STRIPE_INSYNC, &sh->state); 2976 break; 2977 case check_state_run: 2978 break; /* we will be called again upon completion */ 2979 case check_state_check_result: 2980 sh->check_state = check_state_idle; 2981 2982 /* if a failure occurred during the check operation, leave 2983 * STRIPE_INSYNC not set and let the stripe be handled again 2984 */ 2985 if (s->failed) 2986 break; 2987 2988 /* handle a successful check operation, if parity is correct 2989 * we are done. Otherwise update the mismatch count and repair 2990 * parity if !MD_RECOVERY_CHECK 2991 */ 2992 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) 2993 /* parity is correct (on disc, 2994 * not in buffer any more) 2995 */ 2996 set_bit(STRIPE_INSYNC, &sh->state); 2997 else { 2998 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); 2999 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) 3000 /* don't try to repair!! */ 3001 set_bit(STRIPE_INSYNC, &sh->state); 3002 else { 3003 sh->check_state = check_state_compute_run; 3004 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 3005 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 3006 set_bit(R5_Wantcompute, 3007 &sh->dev[sh->pd_idx].flags); 3008 sh->ops.target = sh->pd_idx; 3009 sh->ops.target2 = -1; 3010 s->uptodate++; 3011 } 3012 } 3013 break; 3014 case check_state_compute_run: 3015 break; 3016 default: 3017 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", 3018 __func__, sh->check_state, 3019 (unsigned long long) sh->sector); 3020 BUG(); 3021 } 3022 } 3023 3024 3025 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, 3026 struct stripe_head_state *s, 3027 int disks) 3028 { 3029 int pd_idx = sh->pd_idx; 3030 int qd_idx = sh->qd_idx; 3031 struct r5dev *dev; 3032 3033 set_bit(STRIPE_HANDLE, &sh->state); 3034 3035 BUG_ON(s->failed > 2); 3036 3037 /* Want to check and possibly repair P and Q. 3038 * However there could be one 'failed' device, in which 3039 * case we can only check one of them, possibly using the 3040 * other to generate missing data 3041 */ 3042 3043 switch (sh->check_state) { 3044 case check_state_idle: 3045 /* start a new check operation if there are < 2 failures */ 3046 if (s->failed == s->q_failed) { 3047 /* The only possible failed device holds Q, so it 3048 * makes sense to check P (If anything else were failed, 3049 * we would have used P to recreate it). 3050 */ 3051 sh->check_state = check_state_run; 3052 } 3053 if (!s->q_failed && s->failed < 2) { 3054 /* Q is not failed, and we didn't use it to generate 3055 * anything, so it makes sense to check it 3056 */ 3057 if (sh->check_state == check_state_run) 3058 sh->check_state = check_state_run_pq; 3059 else 3060 sh->check_state = check_state_run_q; 3061 } 3062 3063 /* discard potentially stale zero_sum_result */ 3064 sh->ops.zero_sum_result = 0; 3065 3066 if (sh->check_state == check_state_run) { 3067 /* async_xor_zero_sum destroys the contents of P */ 3068 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); 3069 s->uptodate--; 3070 } 3071 if (sh->check_state >= check_state_run && 3072 sh->check_state <= check_state_run_pq) { 3073 /* async_syndrome_zero_sum preserves P and Q, so 3074 * no need to mark them !uptodate here 3075 */ 3076 set_bit(STRIPE_OP_CHECK, &s->ops_request); 3077 break; 3078 } 3079 3080 /* we have 2-disk failure */ 3081 BUG_ON(s->failed != 2); 3082 /* fall through */ 3083 case check_state_compute_result: 3084 sh->check_state = check_state_idle; 3085 3086 /* check that a write has not made the stripe insync */ 3087 if (test_bit(STRIPE_INSYNC, &sh->state)) 3088 break; 3089 3090 /* now write out any block on a failed drive, 3091 * or P or Q if they were recomputed 3092 */ 3093 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ 3094 if (s->failed == 2) { 3095 dev = &sh->dev[s->failed_num[1]]; 3096 s->locked++; 3097 set_bit(R5_LOCKED, &dev->flags); 3098 set_bit(R5_Wantwrite, &dev->flags); 3099 } 3100 if (s->failed >= 1) { 3101 dev = &sh->dev[s->failed_num[0]]; 3102 s->locked++; 3103 set_bit(R5_LOCKED, &dev->flags); 3104 set_bit(R5_Wantwrite, &dev->flags); 3105 } 3106 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { 3107 dev = &sh->dev[pd_idx]; 3108 s->locked++; 3109 set_bit(R5_LOCKED, &dev->flags); 3110 set_bit(R5_Wantwrite, &dev->flags); 3111 } 3112 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { 3113 dev = &sh->dev[qd_idx]; 3114 s->locked++; 3115 set_bit(R5_LOCKED, &dev->flags); 3116 set_bit(R5_Wantwrite, &dev->flags); 3117 } 3118 clear_bit(STRIPE_DEGRADED, &sh->state); 3119 3120 set_bit(STRIPE_INSYNC, &sh->state); 3121 break; 3122 case check_state_run: 3123 case check_state_run_q: 3124 case check_state_run_pq: 3125 break; /* we will be called again upon completion */ 3126 case check_state_check_result: 3127 sh->check_state = check_state_idle; 3128 3129 /* handle a successful check operation, if parity is correct 3130 * we are done. Otherwise update the mismatch count and repair 3131 * parity if !MD_RECOVERY_CHECK 3132 */ 3133 if (sh->ops.zero_sum_result == 0) { 3134 /* both parities are correct */ 3135 if (!s->failed) 3136 set_bit(STRIPE_INSYNC, &sh->state); 3137 else { 3138 /* in contrast to the raid5 case we can validate 3139 * parity, but still have a failure to write 3140 * back 3141 */ 3142 sh->check_state = check_state_compute_result; 3143 /* Returning at this point means that we may go 3144 * off and bring p and/or q uptodate again so 3145 * we make sure to check zero_sum_result again 3146 * to verify if p or q need writeback 3147 */ 3148 } 3149 } else { 3150 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); 3151 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) 3152 /* don't try to repair!! */ 3153 set_bit(STRIPE_INSYNC, &sh->state); 3154 else { 3155 int *target = &sh->ops.target; 3156 3157 sh->ops.target = -1; 3158 sh->ops.target2 = -1; 3159 sh->check_state = check_state_compute_run; 3160 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 3161 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 3162 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { 3163 set_bit(R5_Wantcompute, 3164 &sh->dev[pd_idx].flags); 3165 *target = pd_idx; 3166 target = &sh->ops.target2; 3167 s->uptodate++; 3168 } 3169 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { 3170 set_bit(R5_Wantcompute, 3171 &sh->dev[qd_idx].flags); 3172 *target = qd_idx; 3173 s->uptodate++; 3174 } 3175 } 3176 } 3177 break; 3178 case check_state_compute_run: 3179 break; 3180 default: 3181 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", 3182 __func__, sh->check_state, 3183 (unsigned long long) sh->sector); 3184 BUG(); 3185 } 3186 } 3187 3188 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) 3189 { 3190 int i; 3191 3192 /* We have read all the blocks in this stripe and now we need to 3193 * copy some of them into a target stripe for expand. 3194 */ 3195 struct dma_async_tx_descriptor *tx = NULL; 3196 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); 3197 for (i = 0; i < sh->disks; i++) 3198 if (i != sh->pd_idx && i != sh->qd_idx) { 3199 int dd_idx, j; 3200 struct stripe_head *sh2; 3201 struct async_submit_ctl submit; 3202 3203 sector_t bn = compute_blocknr(sh, i, 1); 3204 sector_t s = raid5_compute_sector(conf, bn, 0, 3205 &dd_idx, NULL); 3206 sh2 = get_active_stripe(conf, s, 0, 1, 1); 3207 if (sh2 == NULL) 3208 /* so far only the early blocks of this stripe 3209 * have been requested. When later blocks 3210 * get requested, we will try again 3211 */ 3212 continue; 3213 if (!test_bit(STRIPE_EXPANDING, &sh2->state) || 3214 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { 3215 /* must have already done this block */ 3216 release_stripe(sh2); 3217 continue; 3218 } 3219 3220 /* place all the copies on one channel */ 3221 init_async_submit(&submit, 0, tx, NULL, NULL, NULL); 3222 tx = async_memcpy(sh2->dev[dd_idx].page, 3223 sh->dev[i].page, 0, 0, STRIPE_SIZE, 3224 &submit); 3225 3226 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); 3227 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); 3228 for (j = 0; j < conf->raid_disks; j++) 3229 if (j != sh2->pd_idx && 3230 j != sh2->qd_idx && 3231 !test_bit(R5_Expanded, &sh2->dev[j].flags)) 3232 break; 3233 if (j == conf->raid_disks) { 3234 set_bit(STRIPE_EXPAND_READY, &sh2->state); 3235 set_bit(STRIPE_HANDLE, &sh2->state); 3236 } 3237 release_stripe(sh2); 3238 3239 } 3240 /* done submitting copies, wait for them to complete */ 3241 async_tx_quiesce(&tx); 3242 } 3243 3244 /* 3245 * handle_stripe - do things to a stripe. 3246 * 3247 * We lock the stripe by setting STRIPE_ACTIVE and then examine the 3248 * state of various bits to see what needs to be done. 3249 * Possible results: 3250 * return some read requests which now have data 3251 * return some write requests which are safely on storage 3252 * schedule a read on some buffers 3253 * schedule a write of some buffers 3254 * return confirmation of parity correctness 3255 * 3256 */ 3257 3258 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) 3259 { 3260 struct r5conf *conf = sh->raid_conf; 3261 int disks = sh->disks; 3262 struct r5dev *dev; 3263 int i; 3264 int do_recovery = 0; 3265 3266 memset(s, 0, sizeof(*s)); 3267 3268 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); 3269 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); 3270 s->failed_num[0] = -1; 3271 s->failed_num[1] = -1; 3272 3273 /* Now to look around and see what can be done */ 3274 rcu_read_lock(); 3275 for (i=disks; i--; ) { 3276 struct md_rdev *rdev; 3277 sector_t first_bad; 3278 int bad_sectors; 3279 int is_bad = 0; 3280 3281 dev = &sh->dev[i]; 3282 3283 pr_debug("check %d: state 0x%lx read %p write %p written %p\n", 3284 i, dev->flags, 3285 dev->toread, dev->towrite, dev->written); 3286 /* maybe we can reply to a read 3287 * 3288 * new wantfill requests are only permitted while 3289 * ops_complete_biofill is guaranteed to be inactive 3290 */ 3291 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && 3292 !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) 3293 set_bit(R5_Wantfill, &dev->flags); 3294 3295 /* now count some things */ 3296 if (test_bit(R5_LOCKED, &dev->flags)) 3297 s->locked++; 3298 if (test_bit(R5_UPTODATE, &dev->flags)) 3299 s->uptodate++; 3300 if (test_bit(R5_Wantcompute, &dev->flags)) { 3301 s->compute++; 3302 BUG_ON(s->compute > 2); 3303 } 3304 3305 if (test_bit(R5_Wantfill, &dev->flags)) 3306 s->to_fill++; 3307 else if (dev->toread) 3308 s->to_read++; 3309 if (dev->towrite) { 3310 s->to_write++; 3311 if (!test_bit(R5_OVERWRITE, &dev->flags)) 3312 s->non_overwrite++; 3313 } 3314 if (dev->written) 3315 s->written++; 3316 /* Prefer to use the replacement for reads, but only 3317 * if it is recovered enough and has no bad blocks. 3318 */ 3319 rdev = rcu_dereference(conf->disks[i].replacement); 3320 if (rdev && !test_bit(Faulty, &rdev->flags) && 3321 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && 3322 !is_badblock(rdev, sh->sector, STRIPE_SECTORS, 3323 &first_bad, &bad_sectors)) 3324 set_bit(R5_ReadRepl, &dev->flags); 3325 else { 3326 if (rdev) 3327 set_bit(R5_NeedReplace, &dev->flags); 3328 rdev = rcu_dereference(conf->disks[i].rdev); 3329 clear_bit(R5_ReadRepl, &dev->flags); 3330 } 3331 if (rdev && test_bit(Faulty, &rdev->flags)) 3332 rdev = NULL; 3333 if (rdev) { 3334 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, 3335 &first_bad, &bad_sectors); 3336 if (s->blocked_rdev == NULL 3337 && (test_bit(Blocked, &rdev->flags) 3338 || is_bad < 0)) { 3339 if (is_bad < 0) 3340 set_bit(BlockedBadBlocks, 3341 &rdev->flags); 3342 s->blocked_rdev = rdev; 3343 atomic_inc(&rdev->nr_pending); 3344 } 3345 } 3346 clear_bit(R5_Insync, &dev->flags); 3347 if (!rdev) 3348 /* Not in-sync */; 3349 else if (is_bad) { 3350 /* also not in-sync */ 3351 if (!test_bit(WriteErrorSeen, &rdev->flags) && 3352 test_bit(R5_UPTODATE, &dev->flags)) { 3353 /* treat as in-sync, but with a read error 3354 * which we can now try to correct 3355 */ 3356 set_bit(R5_Insync, &dev->flags); 3357 set_bit(R5_ReadError, &dev->flags); 3358 } 3359 } else if (test_bit(In_sync, &rdev->flags)) 3360 set_bit(R5_Insync, &dev->flags); 3361 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) 3362 /* in sync if before recovery_offset */ 3363 set_bit(R5_Insync, &dev->flags); 3364 else if (test_bit(R5_UPTODATE, &dev->flags) && 3365 test_bit(R5_Expanded, &dev->flags)) 3366 /* If we've reshaped into here, we assume it is Insync. 3367 * We will shortly update recovery_offset to make 3368 * it official. 3369 */ 3370 set_bit(R5_Insync, &dev->flags); 3371 3372 if (rdev && test_bit(R5_WriteError, &dev->flags)) { 3373 /* This flag does not apply to '.replacement' 3374 * only to .rdev, so make sure to check that*/ 3375 struct md_rdev *rdev2 = rcu_dereference( 3376 conf->disks[i].rdev); 3377 if (rdev2 == rdev) 3378 clear_bit(R5_Insync, &dev->flags); 3379 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { 3380 s->handle_bad_blocks = 1; 3381 atomic_inc(&rdev2->nr_pending); 3382 } else 3383 clear_bit(R5_WriteError, &dev->flags); 3384 } 3385 if (rdev && test_bit(R5_MadeGood, &dev->flags)) { 3386 /* This flag does not apply to '.replacement' 3387 * only to .rdev, so make sure to check that*/ 3388 struct md_rdev *rdev2 = rcu_dereference( 3389 conf->disks[i].rdev); 3390 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { 3391 s->handle_bad_blocks = 1; 3392 atomic_inc(&rdev2->nr_pending); 3393 } else 3394 clear_bit(R5_MadeGood, &dev->flags); 3395 } 3396 if (test_bit(R5_MadeGoodRepl, &dev->flags)) { 3397 struct md_rdev *rdev2 = rcu_dereference( 3398 conf->disks[i].replacement); 3399 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { 3400 s->handle_bad_blocks = 1; 3401 atomic_inc(&rdev2->nr_pending); 3402 } else 3403 clear_bit(R5_MadeGoodRepl, &dev->flags); 3404 } 3405 if (!test_bit(R5_Insync, &dev->flags)) { 3406 /* The ReadError flag will just be confusing now */ 3407 clear_bit(R5_ReadError, &dev->flags); 3408 clear_bit(R5_ReWrite, &dev->flags); 3409 } 3410 if (test_bit(R5_ReadError, &dev->flags)) 3411 clear_bit(R5_Insync, &dev->flags); 3412 if (!test_bit(R5_Insync, &dev->flags)) { 3413 if (s->failed < 2) 3414 s->failed_num[s->failed] = i; 3415 s->failed++; 3416 if (rdev && !test_bit(Faulty, &rdev->flags)) 3417 do_recovery = 1; 3418 } 3419 } 3420 if (test_bit(STRIPE_SYNCING, &sh->state)) { 3421 /* If there is a failed device being replaced, 3422 * we must be recovering. 3423 * else if we are after recovery_cp, we must be syncing 3424 * else if MD_RECOVERY_REQUESTED is set, we also are syncing. 3425 * else we can only be replacing 3426 * sync and recovery both need to read all devices, and so 3427 * use the same flag. 3428 */ 3429 if (do_recovery || 3430 sh->sector >= conf->mddev->recovery_cp || 3431 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) 3432 s->syncing = 1; 3433 else 3434 s->replacing = 1; 3435 } 3436 rcu_read_unlock(); 3437 } 3438 3439 static void handle_stripe(struct stripe_head *sh) 3440 { 3441 struct stripe_head_state s; 3442 struct r5conf *conf = sh->raid_conf; 3443 int i; 3444 int prexor; 3445 int disks = sh->disks; 3446 struct r5dev *pdev, *qdev; 3447 3448 clear_bit(STRIPE_HANDLE, &sh->state); 3449 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { 3450 /* already being handled, ensure it gets handled 3451 * again when current action finishes */ 3452 set_bit(STRIPE_HANDLE, &sh->state); 3453 return; 3454 } 3455 3456 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { 3457 set_bit(STRIPE_SYNCING, &sh->state); 3458 clear_bit(STRIPE_INSYNC, &sh->state); 3459 } 3460 clear_bit(STRIPE_DELAYED, &sh->state); 3461 3462 pr_debug("handling stripe %llu, state=%#lx cnt=%d, " 3463 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", 3464 (unsigned long long)sh->sector, sh->state, 3465 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, 3466 sh->check_state, sh->reconstruct_state); 3467 3468 analyse_stripe(sh, &s); 3469 3470 if (s.handle_bad_blocks) { 3471 set_bit(STRIPE_HANDLE, &sh->state); 3472 goto finish; 3473 } 3474 3475 if (unlikely(s.blocked_rdev)) { 3476 if (s.syncing || s.expanding || s.expanded || 3477 s.replacing || s.to_write || s.written) { 3478 set_bit(STRIPE_HANDLE, &sh->state); 3479 goto finish; 3480 } 3481 /* There is nothing for the blocked_rdev to block */ 3482 rdev_dec_pending(s.blocked_rdev, conf->mddev); 3483 s.blocked_rdev = NULL; 3484 } 3485 3486 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { 3487 set_bit(STRIPE_OP_BIOFILL, &s.ops_request); 3488 set_bit(STRIPE_BIOFILL_RUN, &sh->state); 3489 } 3490 3491 pr_debug("locked=%d uptodate=%d to_read=%d" 3492 " to_write=%d failed=%d failed_num=%d,%d\n", 3493 s.locked, s.uptodate, s.to_read, s.to_write, s.failed, 3494 s.failed_num[0], s.failed_num[1]); 3495 /* check if the array has lost more than max_degraded devices and, 3496 * if so, some requests might need to be failed. 3497 */ 3498 if (s.failed > conf->max_degraded) { 3499 sh->check_state = 0; 3500 sh->reconstruct_state = 0; 3501 if (s.to_read+s.to_write+s.written) 3502 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); 3503 if (s.syncing + s.replacing) 3504 handle_failed_sync(conf, sh, &s); 3505 } 3506 3507 /* Now we check to see if any write operations have recently 3508 * completed 3509 */ 3510 prexor = 0; 3511 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) 3512 prexor = 1; 3513 if (sh->reconstruct_state == reconstruct_state_drain_result || 3514 sh->reconstruct_state == reconstruct_state_prexor_drain_result) { 3515 sh->reconstruct_state = reconstruct_state_idle; 3516 3517 /* All the 'written' buffers and the parity block are ready to 3518 * be written back to disk 3519 */ 3520 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && 3521 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); 3522 BUG_ON(sh->qd_idx >= 0 && 3523 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && 3524 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); 3525 for (i = disks; i--; ) { 3526 struct r5dev *dev = &sh->dev[i]; 3527 if (test_bit(R5_LOCKED, &dev->flags) && 3528 (i == sh->pd_idx || i == sh->qd_idx || 3529 dev->written)) { 3530 pr_debug("Writing block %d\n", i); 3531 set_bit(R5_Wantwrite, &dev->flags); 3532 if (prexor) 3533 continue; 3534 if (!test_bit(R5_Insync, &dev->flags) || 3535 ((i == sh->pd_idx || i == sh->qd_idx) && 3536 s.failed == 0)) 3537 set_bit(STRIPE_INSYNC, &sh->state); 3538 } 3539 } 3540 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 3541 s.dec_preread_active = 1; 3542 } 3543 3544 /* 3545 * might be able to return some write requests if the parity blocks 3546 * are safe, or on a failed drive 3547 */ 3548 pdev = &sh->dev[sh->pd_idx]; 3549 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) 3550 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); 3551 qdev = &sh->dev[sh->qd_idx]; 3552 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) 3553 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) 3554 || conf->level < 6; 3555 3556 if (s.written && 3557 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) 3558 && !test_bit(R5_LOCKED, &pdev->flags) 3559 && (test_bit(R5_UPTODATE, &pdev->flags) || 3560 test_bit(R5_Discard, &pdev->flags))))) && 3561 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) 3562 && !test_bit(R5_LOCKED, &qdev->flags) 3563 && (test_bit(R5_UPTODATE, &qdev->flags) || 3564 test_bit(R5_Discard, &qdev->flags)))))) 3565 handle_stripe_clean_event(conf, sh, disks, &s.return_bi); 3566 3567 /* Now we might consider reading some blocks, either to check/generate 3568 * parity, or to satisfy requests 3569 * or to load a block that is being partially written. 3570 */ 3571 if (s.to_read || s.non_overwrite 3572 || (conf->level == 6 && s.to_write && s.failed) 3573 || (s.syncing && (s.uptodate + s.compute < disks)) 3574 || s.replacing 3575 || s.expanding) 3576 handle_stripe_fill(sh, &s, disks); 3577 3578 /* Now to consider new write requests and what else, if anything 3579 * should be read. We do not handle new writes when: 3580 * 1/ A 'write' operation (copy+xor) is already in flight. 3581 * 2/ A 'check' operation is in flight, as it may clobber the parity 3582 * block. 3583 */ 3584 if (s.to_write && !sh->reconstruct_state && !sh->check_state) 3585 handle_stripe_dirtying(conf, sh, &s, disks); 3586 3587 /* maybe we need to check and possibly fix the parity for this stripe 3588 * Any reads will already have been scheduled, so we just see if enough 3589 * data is available. The parity check is held off while parity 3590 * dependent operations are in flight. 3591 */ 3592 if (sh->check_state || 3593 (s.syncing && s.locked == 0 && 3594 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && 3595 !test_bit(STRIPE_INSYNC, &sh->state))) { 3596 if (conf->level == 6) 3597 handle_parity_checks6(conf, sh, &s, disks); 3598 else 3599 handle_parity_checks5(conf, sh, &s, disks); 3600 } 3601 3602 if (s.replacing && s.locked == 0 3603 && !test_bit(STRIPE_INSYNC, &sh->state)) { 3604 /* Write out to replacement devices where possible */ 3605 for (i = 0; i < conf->raid_disks; i++) 3606 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && 3607 test_bit(R5_NeedReplace, &sh->dev[i].flags)) { 3608 set_bit(R5_WantReplace, &sh->dev[i].flags); 3609 set_bit(R5_LOCKED, &sh->dev[i].flags); 3610 s.locked++; 3611 } 3612 set_bit(STRIPE_INSYNC, &sh->state); 3613 } 3614 if ((s.syncing || s.replacing) && s.locked == 0 && 3615 test_bit(STRIPE_INSYNC, &sh->state)) { 3616 md_done_sync(conf->mddev, STRIPE_SECTORS, 1); 3617 clear_bit(STRIPE_SYNCING, &sh->state); 3618 } 3619 3620 /* If the failed drives are just a ReadError, then we might need 3621 * to progress the repair/check process 3622 */ 3623 if (s.failed <= conf->max_degraded && !conf->mddev->ro) 3624 for (i = 0; i < s.failed; i++) { 3625 struct r5dev *dev = &sh->dev[s.failed_num[i]]; 3626 if (test_bit(R5_ReadError, &dev->flags) 3627 && !test_bit(R5_LOCKED, &dev->flags) 3628 && test_bit(R5_UPTODATE, &dev->flags) 3629 ) { 3630 if (!test_bit(R5_ReWrite, &dev->flags)) { 3631 set_bit(R5_Wantwrite, &dev->flags); 3632 set_bit(R5_ReWrite, &dev->flags); 3633 set_bit(R5_LOCKED, &dev->flags); 3634 s.locked++; 3635 } else { 3636 /* let's read it back */ 3637 set_bit(R5_Wantread, &dev->flags); 3638 set_bit(R5_LOCKED, &dev->flags); 3639 s.locked++; 3640 } 3641 } 3642 } 3643 3644 3645 /* Finish reconstruct operations initiated by the expansion process */ 3646 if (sh->reconstruct_state == reconstruct_state_result) { 3647 struct stripe_head *sh_src 3648 = get_active_stripe(conf, sh->sector, 1, 1, 1); 3649 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { 3650 /* sh cannot be written until sh_src has been read. 3651 * so arrange for sh to be delayed a little 3652 */ 3653 set_bit(STRIPE_DELAYED, &sh->state); 3654 set_bit(STRIPE_HANDLE, &sh->state); 3655 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, 3656 &sh_src->state)) 3657 atomic_inc(&conf->preread_active_stripes); 3658 release_stripe(sh_src); 3659 goto finish; 3660 } 3661 if (sh_src) 3662 release_stripe(sh_src); 3663 3664 sh->reconstruct_state = reconstruct_state_idle; 3665 clear_bit(STRIPE_EXPANDING, &sh->state); 3666 for (i = conf->raid_disks; i--; ) { 3667 set_bit(R5_Wantwrite, &sh->dev[i].flags); 3668 set_bit(R5_LOCKED, &sh->dev[i].flags); 3669 s.locked++; 3670 } 3671 } 3672 3673 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && 3674 !sh->reconstruct_state) { 3675 /* Need to write out all blocks after computing parity */ 3676 sh->disks = conf->raid_disks; 3677 stripe_set_idx(sh->sector, conf, 0, sh); 3678 schedule_reconstruction(sh, &s, 1, 1); 3679 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { 3680 clear_bit(STRIPE_EXPAND_READY, &sh->state); 3681 atomic_dec(&conf->reshape_stripes); 3682 wake_up(&conf->wait_for_overlap); 3683 md_done_sync(conf->mddev, STRIPE_SECTORS, 1); 3684 } 3685 3686 if (s.expanding && s.locked == 0 && 3687 !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) 3688 handle_stripe_expansion(conf, sh); 3689 3690 finish: 3691 /* wait for this device to become unblocked */ 3692 if (unlikely(s.blocked_rdev)) { 3693 if (conf->mddev->external) 3694 md_wait_for_blocked_rdev(s.blocked_rdev, 3695 conf->mddev); 3696 else 3697 /* Internal metadata will immediately 3698 * be written by raid5d, so we don't 3699 * need to wait here. 3700 */ 3701 rdev_dec_pending(s.blocked_rdev, 3702 conf->mddev); 3703 } 3704 3705 if (s.handle_bad_blocks) 3706 for (i = disks; i--; ) { 3707 struct md_rdev *rdev; 3708 struct r5dev *dev = &sh->dev[i]; 3709 if (test_and_clear_bit(R5_WriteError, &dev->flags)) { 3710 /* We own a safe reference to the rdev */ 3711 rdev = conf->disks[i].rdev; 3712 if (!rdev_set_badblocks(rdev, sh->sector, 3713 STRIPE_SECTORS, 0)) 3714 md_error(conf->mddev, rdev); 3715 rdev_dec_pending(rdev, conf->mddev); 3716 } 3717 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { 3718 rdev = conf->disks[i].rdev; 3719 rdev_clear_badblocks(rdev, sh->sector, 3720 STRIPE_SECTORS, 0); 3721 rdev_dec_pending(rdev, conf->mddev); 3722 } 3723 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { 3724 rdev = conf->disks[i].replacement; 3725 if (!rdev) 3726 /* rdev have been moved down */ 3727 rdev = conf->disks[i].rdev; 3728 rdev_clear_badblocks(rdev, sh->sector, 3729 STRIPE_SECTORS, 0); 3730 rdev_dec_pending(rdev, conf->mddev); 3731 } 3732 } 3733 3734 if (s.ops_request) 3735 raid_run_ops(sh, s.ops_request); 3736 3737 ops_run_io(sh, &s); 3738 3739 if (s.dec_preread_active) { 3740 /* We delay this until after ops_run_io so that if make_request 3741 * is waiting on a flush, it won't continue until the writes 3742 * have actually been submitted. 3743 */ 3744 atomic_dec(&conf->preread_active_stripes); 3745 if (atomic_read(&conf->preread_active_stripes) < 3746 IO_THRESHOLD) 3747 md_wakeup_thread(conf->mddev->thread); 3748 } 3749 3750 return_io(s.return_bi); 3751 3752 clear_bit_unlock(STRIPE_ACTIVE, &sh->state); 3753 } 3754 3755 static void raid5_activate_delayed(struct r5conf *conf) 3756 { 3757 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { 3758 while (!list_empty(&conf->delayed_list)) { 3759 struct list_head *l = conf->delayed_list.next; 3760 struct stripe_head *sh; 3761 sh = list_entry(l, struct stripe_head, lru); 3762 list_del_init(l); 3763 clear_bit(STRIPE_DELAYED, &sh->state); 3764 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 3765 atomic_inc(&conf->preread_active_stripes); 3766 list_add_tail(&sh->lru, &conf->hold_list); 3767 } 3768 } 3769 } 3770 3771 static void activate_bit_delay(struct r5conf *conf) 3772 { 3773 /* device_lock is held */ 3774 struct list_head head; 3775 list_add(&head, &conf->bitmap_list); 3776 list_del_init(&conf->bitmap_list); 3777 while (!list_empty(&head)) { 3778 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); 3779 list_del_init(&sh->lru); 3780 atomic_inc(&sh->count); 3781 __release_stripe(conf, sh); 3782 } 3783 } 3784 3785 int md_raid5_congested(struct mddev *mddev, int bits) 3786 { 3787 struct r5conf *conf = mddev->private; 3788 3789 /* No difference between reads and writes. Just check 3790 * how busy the stripe_cache is 3791 */ 3792 3793 if (conf->inactive_blocked) 3794 return 1; 3795 if (conf->quiesce) 3796 return 1; 3797 if (list_empty_careful(&conf->inactive_list)) 3798 return 1; 3799 3800 return 0; 3801 } 3802 EXPORT_SYMBOL_GPL(md_raid5_congested); 3803 3804 static int raid5_congested(void *data, int bits) 3805 { 3806 struct mddev *mddev = data; 3807 3808 return mddev_congested(mddev, bits) || 3809 md_raid5_congested(mddev, bits); 3810 } 3811 3812 /* We want read requests to align with chunks where possible, 3813 * but write requests don't need to. 3814 */ 3815 static int raid5_mergeable_bvec(struct request_queue *q, 3816 struct bvec_merge_data *bvm, 3817 struct bio_vec *biovec) 3818 { 3819 struct mddev *mddev = q->queuedata; 3820 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); 3821 int max; 3822 unsigned int chunk_sectors = mddev->chunk_sectors; 3823 unsigned int bio_sectors = bvm->bi_size >> 9; 3824 3825 if ((bvm->bi_rw & 1) == WRITE) 3826 return biovec->bv_len; /* always allow writes to be mergeable */ 3827 3828 if (mddev->new_chunk_sectors < mddev->chunk_sectors) 3829 chunk_sectors = mddev->new_chunk_sectors; 3830 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; 3831 if (max < 0) max = 0; 3832 if (max <= biovec->bv_len && bio_sectors == 0) 3833 return biovec->bv_len; 3834 else 3835 return max; 3836 } 3837 3838 3839 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) 3840 { 3841 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); 3842 unsigned int chunk_sectors = mddev->chunk_sectors; 3843 unsigned int bio_sectors = bio->bi_size >> 9; 3844 3845 if (mddev->new_chunk_sectors < mddev->chunk_sectors) 3846 chunk_sectors = mddev->new_chunk_sectors; 3847 return chunk_sectors >= 3848 ((sector & (chunk_sectors - 1)) + bio_sectors); 3849 } 3850 3851 /* 3852 * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) 3853 * later sampled by raid5d. 3854 */ 3855 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) 3856 { 3857 unsigned long flags; 3858 3859 spin_lock_irqsave(&conf->device_lock, flags); 3860 3861 bi->bi_next = conf->retry_read_aligned_list; 3862 conf->retry_read_aligned_list = bi; 3863 3864 spin_unlock_irqrestore(&conf->device_lock, flags); 3865 md_wakeup_thread(conf->mddev->thread); 3866 } 3867 3868 3869 static struct bio *remove_bio_from_retry(struct r5conf *conf) 3870 { 3871 struct bio *bi; 3872 3873 bi = conf->retry_read_aligned; 3874 if (bi) { 3875 conf->retry_read_aligned = NULL; 3876 return bi; 3877 } 3878 bi = conf->retry_read_aligned_list; 3879 if(bi) { 3880 conf->retry_read_aligned_list = bi->bi_next; 3881 bi->bi_next = NULL; 3882 /* 3883 * this sets the active strip count to 1 and the processed 3884 * strip count to zero (upper 8 bits) 3885 */ 3886 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */ 3887 } 3888 3889 return bi; 3890 } 3891 3892 3893 /* 3894 * The "raid5_align_endio" should check if the read succeeded and if it 3895 * did, call bio_endio on the original bio (having bio_put the new bio 3896 * first). 3897 * If the read failed.. 3898 */ 3899 static void raid5_align_endio(struct bio *bi, int error) 3900 { 3901 struct bio* raid_bi = bi->bi_private; 3902 struct mddev *mddev; 3903 struct r5conf *conf; 3904 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); 3905 struct md_rdev *rdev; 3906 3907 bio_put(bi); 3908 3909 rdev = (void*)raid_bi->bi_next; 3910 raid_bi->bi_next = NULL; 3911 mddev = rdev->mddev; 3912 conf = mddev->private; 3913 3914 rdev_dec_pending(rdev, conf->mddev); 3915 3916 if (!error && uptodate) { 3917 bio_endio(raid_bi, 0); 3918 if (atomic_dec_and_test(&conf->active_aligned_reads)) 3919 wake_up(&conf->wait_for_stripe); 3920 return; 3921 } 3922 3923 3924 pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); 3925 3926 add_bio_to_retry(raid_bi, conf); 3927 } 3928 3929 static int bio_fits_rdev(struct bio *bi) 3930 { 3931 struct request_queue *q = bdev_get_queue(bi->bi_bdev); 3932 3933 if ((bi->bi_size>>9) > queue_max_sectors(q)) 3934 return 0; 3935 blk_recount_segments(q, bi); 3936 if (bi->bi_phys_segments > queue_max_segments(q)) 3937 return 0; 3938 3939 if (q->merge_bvec_fn) 3940 /* it's too hard to apply the merge_bvec_fn at this stage, 3941 * just just give up 3942 */ 3943 return 0; 3944 3945 return 1; 3946 } 3947 3948 3949 static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio) 3950 { 3951 struct r5conf *conf = mddev->private; 3952 int dd_idx; 3953 struct bio* align_bi; 3954 struct md_rdev *rdev; 3955 sector_t end_sector; 3956 3957 if (!in_chunk_boundary(mddev, raid_bio)) { 3958 pr_debug("chunk_aligned_read : non aligned\n"); 3959 return 0; 3960 } 3961 /* 3962 * use bio_clone_mddev to make a copy of the bio 3963 */ 3964 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); 3965 if (!align_bi) 3966 return 0; 3967 /* 3968 * set bi_end_io to a new function, and set bi_private to the 3969 * original bio. 3970 */ 3971 align_bi->bi_end_io = raid5_align_endio; 3972 align_bi->bi_private = raid_bio; 3973 /* 3974 * compute position 3975 */ 3976 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, 3977 0, 3978 &dd_idx, NULL); 3979 3980 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9); 3981 rcu_read_lock(); 3982 rdev = rcu_dereference(conf->disks[dd_idx].replacement); 3983 if (!rdev || test_bit(Faulty, &rdev->flags) || 3984 rdev->recovery_offset < end_sector) { 3985 rdev = rcu_dereference(conf->disks[dd_idx].rdev); 3986 if (rdev && 3987 (test_bit(Faulty, &rdev->flags) || 3988 !(test_bit(In_sync, &rdev->flags) || 3989 rdev->recovery_offset >= end_sector))) 3990 rdev = NULL; 3991 } 3992 if (rdev) { 3993 sector_t first_bad; 3994 int bad_sectors; 3995 3996 atomic_inc(&rdev->nr_pending); 3997 rcu_read_unlock(); 3998 raid_bio->bi_next = (void*)rdev; 3999 align_bi->bi_bdev = rdev->bdev; 4000 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); 4001 4002 if (!bio_fits_rdev(align_bi) || 4003 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, 4004 &first_bad, &bad_sectors)) { 4005 /* too big in some way, or has a known bad block */ 4006 bio_put(align_bi); 4007 rdev_dec_pending(rdev, mddev); 4008 return 0; 4009 } 4010 4011 /* No reshape active, so we can trust rdev->data_offset */ 4012 align_bi->bi_sector += rdev->data_offset; 4013 4014 spin_lock_irq(&conf->device_lock); 4015 wait_event_lock_irq(conf->wait_for_stripe, 4016 conf->quiesce == 0, 4017 conf->device_lock); 4018 atomic_inc(&conf->active_aligned_reads); 4019 spin_unlock_irq(&conf->device_lock); 4020 4021 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), 4022 align_bi, disk_devt(mddev->gendisk), 4023 raid_bio->bi_sector); 4024 generic_make_request(align_bi); 4025 return 1; 4026 } else { 4027 rcu_read_unlock(); 4028 bio_put(align_bi); 4029 return 0; 4030 } 4031 } 4032 4033 /* __get_priority_stripe - get the next stripe to process 4034 * 4035 * Full stripe writes are allowed to pass preread active stripes up until 4036 * the bypass_threshold is exceeded. In general the bypass_count 4037 * increments when the handle_list is handled before the hold_list; however, it 4038 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a 4039 * stripe with in flight i/o. The bypass_count will be reset when the 4040 * head of the hold_list has changed, i.e. the head was promoted to the 4041 * handle_list. 4042 */ 4043 static struct stripe_head *__get_priority_stripe(struct r5conf *conf) 4044 { 4045 struct stripe_head *sh; 4046 4047 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", 4048 __func__, 4049 list_empty(&conf->handle_list) ? "empty" : "busy", 4050 list_empty(&conf->hold_list) ? "empty" : "busy", 4051 atomic_read(&conf->pending_full_writes), conf->bypass_count); 4052 4053 if (!list_empty(&conf->handle_list)) { 4054 sh = list_entry(conf->handle_list.next, typeof(*sh), lru); 4055 4056 if (list_empty(&conf->hold_list)) 4057 conf->bypass_count = 0; 4058 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { 4059 if (conf->hold_list.next == conf->last_hold) 4060 conf->bypass_count++; 4061 else { 4062 conf->last_hold = conf->hold_list.next; 4063 conf->bypass_count -= conf->bypass_threshold; 4064 if (conf->bypass_count < 0) 4065 conf->bypass_count = 0; 4066 } 4067 } 4068 } else if (!list_empty(&conf->hold_list) && 4069 ((conf->bypass_threshold && 4070 conf->bypass_count > conf->bypass_threshold) || 4071 atomic_read(&conf->pending_full_writes) == 0)) { 4072 sh = list_entry(conf->hold_list.next, 4073 typeof(*sh), lru); 4074 conf->bypass_count -= conf->bypass_threshold; 4075 if (conf->bypass_count < 0) 4076 conf->bypass_count = 0; 4077 } else 4078 return NULL; 4079 4080 list_del_init(&sh->lru); 4081 atomic_inc(&sh->count); 4082 BUG_ON(atomic_read(&sh->count) != 1); 4083 return sh; 4084 } 4085 4086 struct raid5_plug_cb { 4087 struct blk_plug_cb cb; 4088 struct list_head list; 4089 }; 4090 4091 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) 4092 { 4093 struct raid5_plug_cb *cb = container_of( 4094 blk_cb, struct raid5_plug_cb, cb); 4095 struct stripe_head *sh; 4096 struct mddev *mddev = cb->cb.data; 4097 struct r5conf *conf = mddev->private; 4098 int cnt = 0; 4099 4100 if (cb->list.next && !list_empty(&cb->list)) { 4101 spin_lock_irq(&conf->device_lock); 4102 while (!list_empty(&cb->list)) { 4103 sh = list_first_entry(&cb->list, struct stripe_head, lru); 4104 list_del_init(&sh->lru); 4105 /* 4106 * avoid race release_stripe_plug() sees 4107 * STRIPE_ON_UNPLUG_LIST clear but the stripe 4108 * is still in our list 4109 */ 4110 smp_mb__before_clear_bit(); 4111 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); 4112 __release_stripe(conf, sh); 4113 cnt++; 4114 } 4115 spin_unlock_irq(&conf->device_lock); 4116 } 4117 trace_block_unplug(mddev->queue, cnt, !from_schedule); 4118 kfree(cb); 4119 } 4120 4121 static void release_stripe_plug(struct mddev *mddev, 4122 struct stripe_head *sh) 4123 { 4124 struct blk_plug_cb *blk_cb = blk_check_plugged( 4125 raid5_unplug, mddev, 4126 sizeof(struct raid5_plug_cb)); 4127 struct raid5_plug_cb *cb; 4128 4129 if (!blk_cb) { 4130 release_stripe(sh); 4131 return; 4132 } 4133 4134 cb = container_of(blk_cb, struct raid5_plug_cb, cb); 4135 4136 if (cb->list.next == NULL) 4137 INIT_LIST_HEAD(&cb->list); 4138 4139 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) 4140 list_add_tail(&sh->lru, &cb->list); 4141 else 4142 release_stripe(sh); 4143 } 4144 4145 static void make_discard_request(struct mddev *mddev, struct bio *bi) 4146 { 4147 struct r5conf *conf = mddev->private; 4148 sector_t logical_sector, last_sector; 4149 struct stripe_head *sh; 4150 int remaining; 4151 int stripe_sectors; 4152 4153 if (mddev->reshape_position != MaxSector) 4154 /* Skip discard while reshape is happening */ 4155 return; 4156 4157 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); 4158 last_sector = bi->bi_sector + (bi->bi_size>>9); 4159 4160 bi->bi_next = NULL; 4161 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ 4162 4163 stripe_sectors = conf->chunk_sectors * 4164 (conf->raid_disks - conf->max_degraded); 4165 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, 4166 stripe_sectors); 4167 sector_div(last_sector, stripe_sectors); 4168 4169 logical_sector *= conf->chunk_sectors; 4170 last_sector *= conf->chunk_sectors; 4171 4172 for (; logical_sector < last_sector; 4173 logical_sector += STRIPE_SECTORS) { 4174 DEFINE_WAIT(w); 4175 int d; 4176 again: 4177 sh = get_active_stripe(conf, logical_sector, 0, 0, 0); 4178 prepare_to_wait(&conf->wait_for_overlap, &w, 4179 TASK_UNINTERRUPTIBLE); 4180 spin_lock_irq(&sh->stripe_lock); 4181 for (d = 0; d < conf->raid_disks; d++) { 4182 if (d == sh->pd_idx || d == sh->qd_idx) 4183 continue; 4184 if (sh->dev[d].towrite || sh->dev[d].toread) { 4185 set_bit(R5_Overlap, &sh->dev[d].flags); 4186 spin_unlock_irq(&sh->stripe_lock); 4187 release_stripe(sh); 4188 schedule(); 4189 goto again; 4190 } 4191 } 4192 finish_wait(&conf->wait_for_overlap, &w); 4193 for (d = 0; d < conf->raid_disks; d++) { 4194 if (d == sh->pd_idx || d == sh->qd_idx) 4195 continue; 4196 sh->dev[d].towrite = bi; 4197 set_bit(R5_OVERWRITE, &sh->dev[d].flags); 4198 raid5_inc_bi_active_stripes(bi); 4199 } 4200 spin_unlock_irq(&sh->stripe_lock); 4201 if (conf->mddev->bitmap) { 4202 for (d = 0; 4203 d < conf->raid_disks - conf->max_degraded; 4204 d++) 4205 bitmap_startwrite(mddev->bitmap, 4206 sh->sector, 4207 STRIPE_SECTORS, 4208 0); 4209 sh->bm_seq = conf->seq_flush + 1; 4210 set_bit(STRIPE_BIT_DELAY, &sh->state); 4211 } 4212 4213 set_bit(STRIPE_HANDLE, &sh->state); 4214 clear_bit(STRIPE_DELAYED, &sh->state); 4215 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 4216 atomic_inc(&conf->preread_active_stripes); 4217 release_stripe_plug(mddev, sh); 4218 } 4219 4220 remaining = raid5_dec_bi_active_stripes(bi); 4221 if (remaining == 0) { 4222 md_write_end(mddev); 4223 bio_endio(bi, 0); 4224 } 4225 } 4226 4227 static void make_request(struct mddev *mddev, struct bio * bi) 4228 { 4229 struct r5conf *conf = mddev->private; 4230 int dd_idx; 4231 sector_t new_sector; 4232 sector_t logical_sector, last_sector; 4233 struct stripe_head *sh; 4234 const int rw = bio_data_dir(bi); 4235 int remaining; 4236 4237 if (unlikely(bi->bi_rw & REQ_FLUSH)) { 4238 md_flush_request(mddev, bi); 4239 return; 4240 } 4241 4242 md_write_start(mddev, bi); 4243 4244 if (rw == READ && 4245 mddev->reshape_position == MaxSector && 4246 chunk_aligned_read(mddev,bi)) 4247 return; 4248 4249 if (unlikely(bi->bi_rw & REQ_DISCARD)) { 4250 make_discard_request(mddev, bi); 4251 return; 4252 } 4253 4254 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); 4255 last_sector = bi->bi_sector + (bi->bi_size>>9); 4256 bi->bi_next = NULL; 4257 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ 4258 4259 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { 4260 DEFINE_WAIT(w); 4261 int previous; 4262 4263 retry: 4264 previous = 0; 4265 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); 4266 if (unlikely(conf->reshape_progress != MaxSector)) { 4267 /* spinlock is needed as reshape_progress may be 4268 * 64bit on a 32bit platform, and so it might be 4269 * possible to see a half-updated value 4270 * Of course reshape_progress could change after 4271 * the lock is dropped, so once we get a reference 4272 * to the stripe that we think it is, we will have 4273 * to check again. 4274 */ 4275 spin_lock_irq(&conf->device_lock); 4276 if (mddev->reshape_backwards 4277 ? logical_sector < conf->reshape_progress 4278 : logical_sector >= conf->reshape_progress) { 4279 previous = 1; 4280 } else { 4281 if (mddev->reshape_backwards 4282 ? logical_sector < conf->reshape_safe 4283 : logical_sector >= conf->reshape_safe) { 4284 spin_unlock_irq(&conf->device_lock); 4285 schedule(); 4286 goto retry; 4287 } 4288 } 4289 spin_unlock_irq(&conf->device_lock); 4290 } 4291 4292 new_sector = raid5_compute_sector(conf, logical_sector, 4293 previous, 4294 &dd_idx, NULL); 4295 pr_debug("raid456: make_request, sector %llu logical %llu\n", 4296 (unsigned long long)new_sector, 4297 (unsigned long long)logical_sector); 4298 4299 sh = get_active_stripe(conf, new_sector, previous, 4300 (bi->bi_rw&RWA_MASK), 0); 4301 if (sh) { 4302 if (unlikely(previous)) { 4303 /* expansion might have moved on while waiting for a 4304 * stripe, so we must do the range check again. 4305 * Expansion could still move past after this 4306 * test, but as we are holding a reference to 4307 * 'sh', we know that if that happens, 4308 * STRIPE_EXPANDING will get set and the expansion 4309 * won't proceed until we finish with the stripe. 4310 */ 4311 int must_retry = 0; 4312 spin_lock_irq(&conf->device_lock); 4313 if (mddev->reshape_backwards 4314 ? logical_sector >= conf->reshape_progress 4315 : logical_sector < conf->reshape_progress) 4316 /* mismatch, need to try again */ 4317 must_retry = 1; 4318 spin_unlock_irq(&conf->device_lock); 4319 if (must_retry) { 4320 release_stripe(sh); 4321 schedule(); 4322 goto retry; 4323 } 4324 } 4325 4326 if (rw == WRITE && 4327 logical_sector >= mddev->suspend_lo && 4328 logical_sector < mddev->suspend_hi) { 4329 release_stripe(sh); 4330 /* As the suspend_* range is controlled by 4331 * userspace, we want an interruptible 4332 * wait. 4333 */ 4334 flush_signals(current); 4335 prepare_to_wait(&conf->wait_for_overlap, 4336 &w, TASK_INTERRUPTIBLE); 4337 if (logical_sector >= mddev->suspend_lo && 4338 logical_sector < mddev->suspend_hi) 4339 schedule(); 4340 goto retry; 4341 } 4342 4343 if (test_bit(STRIPE_EXPANDING, &sh->state) || 4344 !add_stripe_bio(sh, bi, dd_idx, rw)) { 4345 /* Stripe is busy expanding or 4346 * add failed due to overlap. Flush everything 4347 * and wait a while 4348 */ 4349 md_wakeup_thread(mddev->thread); 4350 release_stripe(sh); 4351 schedule(); 4352 goto retry; 4353 } 4354 finish_wait(&conf->wait_for_overlap, &w); 4355 set_bit(STRIPE_HANDLE, &sh->state); 4356 clear_bit(STRIPE_DELAYED, &sh->state); 4357 if ((bi->bi_rw & REQ_SYNC) && 4358 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 4359 atomic_inc(&conf->preread_active_stripes); 4360 release_stripe_plug(mddev, sh); 4361 } else { 4362 /* cannot get stripe for read-ahead, just give-up */ 4363 clear_bit(BIO_UPTODATE, &bi->bi_flags); 4364 finish_wait(&conf->wait_for_overlap, &w); 4365 break; 4366 } 4367 } 4368 4369 remaining = raid5_dec_bi_active_stripes(bi); 4370 if (remaining == 0) { 4371 4372 if ( rw == WRITE ) 4373 md_write_end(mddev); 4374 4375 bio_endio(bi, 0); 4376 } 4377 } 4378 4379 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); 4380 4381 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) 4382 { 4383 /* reshaping is quite different to recovery/resync so it is 4384 * handled quite separately ... here. 4385 * 4386 * On each call to sync_request, we gather one chunk worth of 4387 * destination stripes and flag them as expanding. 4388 * Then we find all the source stripes and request reads. 4389 * As the reads complete, handle_stripe will copy the data 4390 * into the destination stripe and release that stripe. 4391 */ 4392 struct r5conf *conf = mddev->private; 4393 struct stripe_head *sh; 4394 sector_t first_sector, last_sector; 4395 int raid_disks = conf->previous_raid_disks; 4396 int data_disks = raid_disks - conf->max_degraded; 4397 int new_data_disks = conf->raid_disks - conf->max_degraded; 4398 int i; 4399 int dd_idx; 4400 sector_t writepos, readpos, safepos; 4401 sector_t stripe_addr; 4402 int reshape_sectors; 4403 struct list_head stripes; 4404 4405 if (sector_nr == 0) { 4406 /* If restarting in the middle, skip the initial sectors */ 4407 if (mddev->reshape_backwards && 4408 conf->reshape_progress < raid5_size(mddev, 0, 0)) { 4409 sector_nr = raid5_size(mddev, 0, 0) 4410 - conf->reshape_progress; 4411 } else if (!mddev->reshape_backwards && 4412 conf->reshape_progress > 0) 4413 sector_nr = conf->reshape_progress; 4414 sector_div(sector_nr, new_data_disks); 4415 if (sector_nr) { 4416 mddev->curr_resync_completed = sector_nr; 4417 sysfs_notify(&mddev->kobj, NULL, "sync_completed"); 4418 *skipped = 1; 4419 return sector_nr; 4420 } 4421 } 4422 4423 /* We need to process a full chunk at a time. 4424 * If old and new chunk sizes differ, we need to process the 4425 * largest of these 4426 */ 4427 if (mddev->new_chunk_sectors > mddev->chunk_sectors) 4428 reshape_sectors = mddev->new_chunk_sectors; 4429 else 4430 reshape_sectors = mddev->chunk_sectors; 4431 4432 /* We update the metadata at least every 10 seconds, or when 4433 * the data about to be copied would over-write the source of 4434 * the data at the front of the range. i.e. one new_stripe 4435 * along from reshape_progress new_maps to after where 4436 * reshape_safe old_maps to 4437 */ 4438 writepos = conf->reshape_progress; 4439 sector_div(writepos, new_data_disks); 4440 readpos = conf->reshape_progress; 4441 sector_div(readpos, data_disks); 4442 safepos = conf->reshape_safe; 4443 sector_div(safepos, data_disks); 4444 if (mddev->reshape_backwards) { 4445 writepos -= min_t(sector_t, reshape_sectors, writepos); 4446 readpos += reshape_sectors; 4447 safepos += reshape_sectors; 4448 } else { 4449 writepos += reshape_sectors; 4450 readpos -= min_t(sector_t, reshape_sectors, readpos); 4451 safepos -= min_t(sector_t, reshape_sectors, safepos); 4452 } 4453 4454 /* Having calculated the 'writepos' possibly use it 4455 * to set 'stripe_addr' which is where we will write to. 4456 */ 4457 if (mddev->reshape_backwards) { 4458 BUG_ON(conf->reshape_progress == 0); 4459 stripe_addr = writepos; 4460 BUG_ON((mddev->dev_sectors & 4461 ~((sector_t)reshape_sectors - 1)) 4462 - reshape_sectors - stripe_addr 4463 != sector_nr); 4464 } else { 4465 BUG_ON(writepos != sector_nr + reshape_sectors); 4466 stripe_addr = sector_nr; 4467 } 4468 4469 /* 'writepos' is the most advanced device address we might write. 4470 * 'readpos' is the least advanced device address we might read. 4471 * 'safepos' is the least address recorded in the metadata as having 4472 * been reshaped. 4473 * If there is a min_offset_diff, these are adjusted either by 4474 * increasing the safepos/readpos if diff is negative, or 4475 * increasing writepos if diff is positive. 4476 * If 'readpos' is then behind 'writepos', there is no way that we can 4477 * ensure safety in the face of a crash - that must be done by userspace 4478 * making a backup of the data. So in that case there is no particular 4479 * rush to update metadata. 4480 * Otherwise if 'safepos' is behind 'writepos', then we really need to 4481 * update the metadata to advance 'safepos' to match 'readpos' so that 4482 * we can be safe in the event of a crash. 4483 * So we insist on updating metadata if safepos is behind writepos and 4484 * readpos is beyond writepos. 4485 * In any case, update the metadata every 10 seconds. 4486 * Maybe that number should be configurable, but I'm not sure it is 4487 * worth it.... maybe it could be a multiple of safemode_delay??? 4488 */ 4489 if (conf->min_offset_diff < 0) { 4490 safepos += -conf->min_offset_diff; 4491 readpos += -conf->min_offset_diff; 4492 } else 4493 writepos += conf->min_offset_diff; 4494 4495 if ((mddev->reshape_backwards 4496 ? (safepos > writepos && readpos < writepos) 4497 : (safepos < writepos && readpos > writepos)) || 4498 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { 4499 /* Cannot proceed until we've updated the superblock... */ 4500 wait_event(conf->wait_for_overlap, 4501 atomic_read(&conf->reshape_stripes)==0); 4502 mddev->reshape_position = conf->reshape_progress; 4503 mddev->curr_resync_completed = sector_nr; 4504 conf->reshape_checkpoint = jiffies; 4505 set_bit(MD_CHANGE_DEVS, &mddev->flags); 4506 md_wakeup_thread(mddev->thread); 4507 wait_event(mddev->sb_wait, mddev->flags == 0 || 4508 kthread_should_stop()); 4509 spin_lock_irq(&conf->device_lock); 4510 conf->reshape_safe = mddev->reshape_position; 4511 spin_unlock_irq(&conf->device_lock); 4512 wake_up(&conf->wait_for_overlap); 4513 sysfs_notify(&mddev->kobj, NULL, "sync_completed"); 4514 } 4515 4516 INIT_LIST_HEAD(&stripes); 4517 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { 4518 int j; 4519 int skipped_disk = 0; 4520 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); 4521 set_bit(STRIPE_EXPANDING, &sh->state); 4522 atomic_inc(&conf->reshape_stripes); 4523 /* If any of this stripe is beyond the end of the old 4524 * array, then we need to zero those blocks 4525 */ 4526 for (j=sh->disks; j--;) { 4527 sector_t s; 4528 if (j == sh->pd_idx) 4529 continue; 4530 if (conf->level == 6 && 4531 j == sh->qd_idx) 4532 continue; 4533 s = compute_blocknr(sh, j, 0); 4534 if (s < raid5_size(mddev, 0, 0)) { 4535 skipped_disk = 1; 4536 continue; 4537 } 4538 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); 4539 set_bit(R5_Expanded, &sh->dev[j].flags); 4540 set_bit(R5_UPTODATE, &sh->dev[j].flags); 4541 } 4542 if (!skipped_disk) { 4543 set_bit(STRIPE_EXPAND_READY, &sh->state); 4544 set_bit(STRIPE_HANDLE, &sh->state); 4545 } 4546 list_add(&sh->lru, &stripes); 4547 } 4548 spin_lock_irq(&conf->device_lock); 4549 if (mddev->reshape_backwards) 4550 conf->reshape_progress -= reshape_sectors * new_data_disks; 4551 else 4552 conf->reshape_progress += reshape_sectors * new_data_disks; 4553 spin_unlock_irq(&conf->device_lock); 4554 /* Ok, those stripe are ready. We can start scheduling 4555 * reads on the source stripes. 4556 * The source stripes are determined by mapping the first and last 4557 * block on the destination stripes. 4558 */ 4559 first_sector = 4560 raid5_compute_sector(conf, stripe_addr*(new_data_disks), 4561 1, &dd_idx, NULL); 4562 last_sector = 4563 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) 4564 * new_data_disks - 1), 4565 1, &dd_idx, NULL); 4566 if (last_sector >= mddev->dev_sectors) 4567 last_sector = mddev->dev_sectors - 1; 4568 while (first_sector <= last_sector) { 4569 sh = get_active_stripe(conf, first_sector, 1, 0, 1); 4570 set_bit(STRIPE_EXPAND_SOURCE, &sh->state); 4571 set_bit(STRIPE_HANDLE, &sh->state); 4572 release_stripe(sh); 4573 first_sector += STRIPE_SECTORS; 4574 } 4575 /* Now that the sources are clearly marked, we can release 4576 * the destination stripes 4577 */ 4578 while (!list_empty(&stripes)) { 4579 sh = list_entry(stripes.next, struct stripe_head, lru); 4580 list_del_init(&sh->lru); 4581 release_stripe(sh); 4582 } 4583 /* If this takes us to the resync_max point where we have to pause, 4584 * then we need to write out the superblock. 4585 */ 4586 sector_nr += reshape_sectors; 4587 if ((sector_nr - mddev->curr_resync_completed) * 2 4588 >= mddev->resync_max - mddev->curr_resync_completed) { 4589 /* Cannot proceed until we've updated the superblock... */ 4590 wait_event(conf->wait_for_overlap, 4591 atomic_read(&conf->reshape_stripes) == 0); 4592 mddev->reshape_position = conf->reshape_progress; 4593 mddev->curr_resync_completed = sector_nr; 4594 conf->reshape_checkpoint = jiffies; 4595 set_bit(MD_CHANGE_DEVS, &mddev->flags); 4596 md_wakeup_thread(mddev->thread); 4597 wait_event(mddev->sb_wait, 4598 !test_bit(MD_CHANGE_DEVS, &mddev->flags) 4599 || kthread_should_stop()); 4600 spin_lock_irq(&conf->device_lock); 4601 conf->reshape_safe = mddev->reshape_position; 4602 spin_unlock_irq(&conf->device_lock); 4603 wake_up(&conf->wait_for_overlap); 4604 sysfs_notify(&mddev->kobj, NULL, "sync_completed"); 4605 } 4606 return reshape_sectors; 4607 } 4608 4609 /* FIXME go_faster isn't used */ 4610 static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster) 4611 { 4612 struct r5conf *conf = mddev->private; 4613 struct stripe_head *sh; 4614 sector_t max_sector = mddev->dev_sectors; 4615 sector_t sync_blocks; 4616 int still_degraded = 0; 4617 int i; 4618 4619 if (sector_nr >= max_sector) { 4620 /* just being told to finish up .. nothing much to do */ 4621 4622 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { 4623 end_reshape(conf); 4624 return 0; 4625 } 4626 4627 if (mddev->curr_resync < max_sector) /* aborted */ 4628 bitmap_end_sync(mddev->bitmap, mddev->curr_resync, 4629 &sync_blocks, 1); 4630 else /* completed sync */ 4631 conf->fullsync = 0; 4632 bitmap_close_sync(mddev->bitmap); 4633 4634 return 0; 4635 } 4636 4637 /* Allow raid5_quiesce to complete */ 4638 wait_event(conf->wait_for_overlap, conf->quiesce != 2); 4639 4640 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 4641 return reshape_request(mddev, sector_nr, skipped); 4642 4643 /* No need to check resync_max as we never do more than one 4644 * stripe, and as resync_max will always be on a chunk boundary, 4645 * if the check in md_do_sync didn't fire, there is no chance 4646 * of overstepping resync_max here 4647 */ 4648 4649 /* if there is too many failed drives and we are trying 4650 * to resync, then assert that we are finished, because there is 4651 * nothing we can do. 4652 */ 4653 if (mddev->degraded >= conf->max_degraded && 4654 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 4655 sector_t rv = mddev->dev_sectors - sector_nr; 4656 *skipped = 1; 4657 return rv; 4658 } 4659 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && 4660 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 4661 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { 4662 /* we can skip this block, and probably more */ 4663 sync_blocks /= STRIPE_SECTORS; 4664 *skipped = 1; 4665 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ 4666 } 4667 4668 bitmap_cond_end_sync(mddev->bitmap, sector_nr); 4669 4670 sh = get_active_stripe(conf, sector_nr, 0, 1, 0); 4671 if (sh == NULL) { 4672 sh = get_active_stripe(conf, sector_nr, 0, 0, 0); 4673 /* make sure we don't swamp the stripe cache if someone else 4674 * is trying to get access 4675 */ 4676 schedule_timeout_uninterruptible(1); 4677 } 4678 /* Need to check if array will still be degraded after recovery/resync 4679 * We don't need to check the 'failed' flag as when that gets set, 4680 * recovery aborts. 4681 */ 4682 for (i = 0; i < conf->raid_disks; i++) 4683 if (conf->disks[i].rdev == NULL) 4684 still_degraded = 1; 4685 4686 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); 4687 4688 set_bit(STRIPE_SYNC_REQUESTED, &sh->state); 4689 4690 handle_stripe(sh); 4691 release_stripe(sh); 4692 4693 return STRIPE_SECTORS; 4694 } 4695 4696 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio) 4697 { 4698 /* We may not be able to submit a whole bio at once as there 4699 * may not be enough stripe_heads available. 4700 * We cannot pre-allocate enough stripe_heads as we may need 4701 * more than exist in the cache (if we allow ever large chunks). 4702 * So we do one stripe head at a time and record in 4703 * ->bi_hw_segments how many have been done. 4704 * 4705 * We *know* that this entire raid_bio is in one chunk, so 4706 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. 4707 */ 4708 struct stripe_head *sh; 4709 int dd_idx; 4710 sector_t sector, logical_sector, last_sector; 4711 int scnt = 0; 4712 int remaining; 4713 int handled = 0; 4714 4715 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); 4716 sector = raid5_compute_sector(conf, logical_sector, 4717 0, &dd_idx, NULL); 4718 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); 4719 4720 for (; logical_sector < last_sector; 4721 logical_sector += STRIPE_SECTORS, 4722 sector += STRIPE_SECTORS, 4723 scnt++) { 4724 4725 if (scnt < raid5_bi_processed_stripes(raid_bio)) 4726 /* already done this stripe */ 4727 continue; 4728 4729 sh = get_active_stripe(conf, sector, 0, 1, 0); 4730 4731 if (!sh) { 4732 /* failed to get a stripe - must wait */ 4733 raid5_set_bi_processed_stripes(raid_bio, scnt); 4734 conf->retry_read_aligned = raid_bio; 4735 return handled; 4736 } 4737 4738 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { 4739 release_stripe(sh); 4740 raid5_set_bi_processed_stripes(raid_bio, scnt); 4741 conf->retry_read_aligned = raid_bio; 4742 return handled; 4743 } 4744 4745 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); 4746 handle_stripe(sh); 4747 release_stripe(sh); 4748 handled++; 4749 } 4750 remaining = raid5_dec_bi_active_stripes(raid_bio); 4751 if (remaining == 0) 4752 bio_endio(raid_bio, 0); 4753 if (atomic_dec_and_test(&conf->active_aligned_reads)) 4754 wake_up(&conf->wait_for_stripe); 4755 return handled; 4756 } 4757 4758 #define MAX_STRIPE_BATCH 8 4759 static int handle_active_stripes(struct r5conf *conf) 4760 { 4761 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; 4762 int i, batch_size = 0; 4763 4764 while (batch_size < MAX_STRIPE_BATCH && 4765 (sh = __get_priority_stripe(conf)) != NULL) 4766 batch[batch_size++] = sh; 4767 4768 if (batch_size == 0) 4769 return batch_size; 4770 spin_unlock_irq(&conf->device_lock); 4771 4772 for (i = 0; i < batch_size; i++) 4773 handle_stripe(batch[i]); 4774 4775 cond_resched(); 4776 4777 spin_lock_irq(&conf->device_lock); 4778 for (i = 0; i < batch_size; i++) 4779 __release_stripe(conf, batch[i]); 4780 return batch_size; 4781 } 4782 4783 /* 4784 * This is our raid5 kernel thread. 4785 * 4786 * We scan the hash table for stripes which can be handled now. 4787 * During the scan, completed stripes are saved for us by the interrupt 4788 * handler, so that they will not have to wait for our next wakeup. 4789 */ 4790 static void raid5d(struct md_thread *thread) 4791 { 4792 struct mddev *mddev = thread->mddev; 4793 struct r5conf *conf = mddev->private; 4794 int handled; 4795 struct blk_plug plug; 4796 4797 pr_debug("+++ raid5d active\n"); 4798 4799 md_check_recovery(mddev); 4800 4801 blk_start_plug(&plug); 4802 handled = 0; 4803 spin_lock_irq(&conf->device_lock); 4804 while (1) { 4805 struct bio *bio; 4806 int batch_size; 4807 4808 if ( 4809 !list_empty(&conf->bitmap_list)) { 4810 /* Now is a good time to flush some bitmap updates */ 4811 conf->seq_flush++; 4812 spin_unlock_irq(&conf->device_lock); 4813 bitmap_unplug(mddev->bitmap); 4814 spin_lock_irq(&conf->device_lock); 4815 conf->seq_write = conf->seq_flush; 4816 activate_bit_delay(conf); 4817 } 4818 raid5_activate_delayed(conf); 4819 4820 while ((bio = remove_bio_from_retry(conf))) { 4821 int ok; 4822 spin_unlock_irq(&conf->device_lock); 4823 ok = retry_aligned_read(conf, bio); 4824 spin_lock_irq(&conf->device_lock); 4825 if (!ok) 4826 break; 4827 handled++; 4828 } 4829 4830 batch_size = handle_active_stripes(conf); 4831 if (!batch_size) 4832 break; 4833 handled += batch_size; 4834 4835 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { 4836 spin_unlock_irq(&conf->device_lock); 4837 md_check_recovery(mddev); 4838 spin_lock_irq(&conf->device_lock); 4839 } 4840 } 4841 pr_debug("%d stripes handled\n", handled); 4842 4843 spin_unlock_irq(&conf->device_lock); 4844 4845 async_tx_issue_pending_all(); 4846 blk_finish_plug(&plug); 4847 4848 pr_debug("--- raid5d inactive\n"); 4849 } 4850 4851 static ssize_t 4852 raid5_show_stripe_cache_size(struct mddev *mddev, char *page) 4853 { 4854 struct r5conf *conf = mddev->private; 4855 if (conf) 4856 return sprintf(page, "%d\n", conf->max_nr_stripes); 4857 else 4858 return 0; 4859 } 4860 4861 int 4862 raid5_set_cache_size(struct mddev *mddev, int size) 4863 { 4864 struct r5conf *conf = mddev->private; 4865 int err; 4866 4867 if (size <= 16 || size > 32768) 4868 return -EINVAL; 4869 while (size < conf->max_nr_stripes) { 4870 if (drop_one_stripe(conf)) 4871 conf->max_nr_stripes--; 4872 else 4873 break; 4874 } 4875 err = md_allow_write(mddev); 4876 if (err) 4877 return err; 4878 while (size > conf->max_nr_stripes) { 4879 if (grow_one_stripe(conf)) 4880 conf->max_nr_stripes++; 4881 else break; 4882 } 4883 return 0; 4884 } 4885 EXPORT_SYMBOL(raid5_set_cache_size); 4886 4887 static ssize_t 4888 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) 4889 { 4890 struct r5conf *conf = mddev->private; 4891 unsigned long new; 4892 int err; 4893 4894 if (len >= PAGE_SIZE) 4895 return -EINVAL; 4896 if (!conf) 4897 return -ENODEV; 4898 4899 if (strict_strtoul(page, 10, &new)) 4900 return -EINVAL; 4901 err = raid5_set_cache_size(mddev, new); 4902 if (err) 4903 return err; 4904 return len; 4905 } 4906 4907 static struct md_sysfs_entry 4908 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, 4909 raid5_show_stripe_cache_size, 4910 raid5_store_stripe_cache_size); 4911 4912 static ssize_t 4913 raid5_show_preread_threshold(struct mddev *mddev, char *page) 4914 { 4915 struct r5conf *conf = mddev->private; 4916 if (conf) 4917 return sprintf(page, "%d\n", conf->bypass_threshold); 4918 else 4919 return 0; 4920 } 4921 4922 static ssize_t 4923 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) 4924 { 4925 struct r5conf *conf = mddev->private; 4926 unsigned long new; 4927 if (len >= PAGE_SIZE) 4928 return -EINVAL; 4929 if (!conf) 4930 return -ENODEV; 4931 4932 if (strict_strtoul(page, 10, &new)) 4933 return -EINVAL; 4934 if (new > conf->max_nr_stripes) 4935 return -EINVAL; 4936 conf->bypass_threshold = new; 4937 return len; 4938 } 4939 4940 static struct md_sysfs_entry 4941 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, 4942 S_IRUGO | S_IWUSR, 4943 raid5_show_preread_threshold, 4944 raid5_store_preread_threshold); 4945 4946 static ssize_t 4947 stripe_cache_active_show(struct mddev *mddev, char *page) 4948 { 4949 struct r5conf *conf = mddev->private; 4950 if (conf) 4951 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); 4952 else 4953 return 0; 4954 } 4955 4956 static struct md_sysfs_entry 4957 raid5_stripecache_active = __ATTR_RO(stripe_cache_active); 4958 4959 static struct attribute *raid5_attrs[] = { 4960 &raid5_stripecache_size.attr, 4961 &raid5_stripecache_active.attr, 4962 &raid5_preread_bypass_threshold.attr, 4963 NULL, 4964 }; 4965 static struct attribute_group raid5_attrs_group = { 4966 .name = NULL, 4967 .attrs = raid5_attrs, 4968 }; 4969 4970 static sector_t 4971 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) 4972 { 4973 struct r5conf *conf = mddev->private; 4974 4975 if (!sectors) 4976 sectors = mddev->dev_sectors; 4977 if (!raid_disks) 4978 /* size is defined by the smallest of previous and new size */ 4979 raid_disks = min(conf->raid_disks, conf->previous_raid_disks); 4980 4981 sectors &= ~((sector_t)mddev->chunk_sectors - 1); 4982 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); 4983 return sectors * (raid_disks - conf->max_degraded); 4984 } 4985 4986 static void raid5_free_percpu(struct r5conf *conf) 4987 { 4988 struct raid5_percpu *percpu; 4989 unsigned long cpu; 4990 4991 if (!conf->percpu) 4992 return; 4993 4994 get_online_cpus(); 4995 for_each_possible_cpu(cpu) { 4996 percpu = per_cpu_ptr(conf->percpu, cpu); 4997 safe_put_page(percpu->spare_page); 4998 kfree(percpu->scribble); 4999 } 5000 #ifdef CONFIG_HOTPLUG_CPU 5001 unregister_cpu_notifier(&conf->cpu_notify); 5002 #endif 5003 put_online_cpus(); 5004 5005 free_percpu(conf->percpu); 5006 } 5007 5008 static void free_conf(struct r5conf *conf) 5009 { 5010 shrink_stripes(conf); 5011 raid5_free_percpu(conf); 5012 kfree(conf->disks); 5013 kfree(conf->stripe_hashtbl); 5014 kfree(conf); 5015 } 5016 5017 #ifdef CONFIG_HOTPLUG_CPU 5018 static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, 5019 void *hcpu) 5020 { 5021 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); 5022 long cpu = (long)hcpu; 5023 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); 5024 5025 switch (action) { 5026 case CPU_UP_PREPARE: 5027 case CPU_UP_PREPARE_FROZEN: 5028 if (conf->level == 6 && !percpu->spare_page) 5029 percpu->spare_page = alloc_page(GFP_KERNEL); 5030 if (!percpu->scribble) 5031 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); 5032 5033 if (!percpu->scribble || 5034 (conf->level == 6 && !percpu->spare_page)) { 5035 safe_put_page(percpu->spare_page); 5036 kfree(percpu->scribble); 5037 pr_err("%s: failed memory allocation for cpu%ld\n", 5038 __func__, cpu); 5039 return notifier_from_errno(-ENOMEM); 5040 } 5041 break; 5042 case CPU_DEAD: 5043 case CPU_DEAD_FROZEN: 5044 safe_put_page(percpu->spare_page); 5045 kfree(percpu->scribble); 5046 percpu->spare_page = NULL; 5047 percpu->scribble = NULL; 5048 break; 5049 default: 5050 break; 5051 } 5052 return NOTIFY_OK; 5053 } 5054 #endif 5055 5056 static int raid5_alloc_percpu(struct r5conf *conf) 5057 { 5058 unsigned long cpu; 5059 struct page *spare_page; 5060 struct raid5_percpu __percpu *allcpus; 5061 void *scribble; 5062 int err; 5063 5064 allcpus = alloc_percpu(struct raid5_percpu); 5065 if (!allcpus) 5066 return -ENOMEM; 5067 conf->percpu = allcpus; 5068 5069 get_online_cpus(); 5070 err = 0; 5071 for_each_present_cpu(cpu) { 5072 if (conf->level == 6) { 5073 spare_page = alloc_page(GFP_KERNEL); 5074 if (!spare_page) { 5075 err = -ENOMEM; 5076 break; 5077 } 5078 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; 5079 } 5080 scribble = kmalloc(conf->scribble_len, GFP_KERNEL); 5081 if (!scribble) { 5082 err = -ENOMEM; 5083 break; 5084 } 5085 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; 5086 } 5087 #ifdef CONFIG_HOTPLUG_CPU 5088 conf->cpu_notify.notifier_call = raid456_cpu_notify; 5089 conf->cpu_notify.priority = 0; 5090 if (err == 0) 5091 err = register_cpu_notifier(&conf->cpu_notify); 5092 #endif 5093 put_online_cpus(); 5094 5095 return err; 5096 } 5097 5098 static struct r5conf *setup_conf(struct mddev *mddev) 5099 { 5100 struct r5conf *conf; 5101 int raid_disk, memory, max_disks; 5102 struct md_rdev *rdev; 5103 struct disk_info *disk; 5104 char pers_name[6]; 5105 5106 if (mddev->new_level != 5 5107 && mddev->new_level != 4 5108 && mddev->new_level != 6) { 5109 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n", 5110 mdname(mddev), mddev->new_level); 5111 return ERR_PTR(-EIO); 5112 } 5113 if ((mddev->new_level == 5 5114 && !algorithm_valid_raid5(mddev->new_layout)) || 5115 (mddev->new_level == 6 5116 && !algorithm_valid_raid6(mddev->new_layout))) { 5117 printk(KERN_ERR "md/raid:%s: layout %d not supported\n", 5118 mdname(mddev), mddev->new_layout); 5119 return ERR_PTR(-EIO); 5120 } 5121 if (mddev->new_level == 6 && mddev->raid_disks < 4) { 5122 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n", 5123 mdname(mddev), mddev->raid_disks); 5124 return ERR_PTR(-EINVAL); 5125 } 5126 5127 if (!mddev->new_chunk_sectors || 5128 (mddev->new_chunk_sectors << 9) % PAGE_SIZE || 5129 !is_power_of_2(mddev->new_chunk_sectors)) { 5130 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", 5131 mdname(mddev), mddev->new_chunk_sectors << 9); 5132 return ERR_PTR(-EINVAL); 5133 } 5134 5135 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); 5136 if (conf == NULL) 5137 goto abort; 5138 spin_lock_init(&conf->device_lock); 5139 init_waitqueue_head(&conf->wait_for_stripe); 5140 init_waitqueue_head(&conf->wait_for_overlap); 5141 INIT_LIST_HEAD(&conf->handle_list); 5142 INIT_LIST_HEAD(&conf->hold_list); 5143 INIT_LIST_HEAD(&conf->delayed_list); 5144 INIT_LIST_HEAD(&conf->bitmap_list); 5145 INIT_LIST_HEAD(&conf->inactive_list); 5146 atomic_set(&conf->active_stripes, 0); 5147 atomic_set(&conf->preread_active_stripes, 0); 5148 atomic_set(&conf->active_aligned_reads, 0); 5149 conf->bypass_threshold = BYPASS_THRESHOLD; 5150 conf->recovery_disabled = mddev->recovery_disabled - 1; 5151 5152 conf->raid_disks = mddev->raid_disks; 5153 if (mddev->reshape_position == MaxSector) 5154 conf->previous_raid_disks = mddev->raid_disks; 5155 else 5156 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; 5157 max_disks = max(conf->raid_disks, conf->previous_raid_disks); 5158 conf->scribble_len = scribble_len(max_disks); 5159 5160 conf->disks = kzalloc(max_disks * sizeof(struct disk_info), 5161 GFP_KERNEL); 5162 if (!conf->disks) 5163 goto abort; 5164 5165 conf->mddev = mddev; 5166 5167 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) 5168 goto abort; 5169 5170 conf->level = mddev->new_level; 5171 if (raid5_alloc_percpu(conf) != 0) 5172 goto abort; 5173 5174 pr_debug("raid456: run(%s) called.\n", mdname(mddev)); 5175 5176 rdev_for_each(rdev, mddev) { 5177 raid_disk = rdev->raid_disk; 5178 if (raid_disk >= max_disks 5179 || raid_disk < 0) 5180 continue; 5181 disk = conf->disks + raid_disk; 5182 5183 if (test_bit(Replacement, &rdev->flags)) { 5184 if (disk->replacement) 5185 goto abort; 5186 disk->replacement = rdev; 5187 } else { 5188 if (disk->rdev) 5189 goto abort; 5190 disk->rdev = rdev; 5191 } 5192 5193 if (test_bit(In_sync, &rdev->flags)) { 5194 char b[BDEVNAME_SIZE]; 5195 printk(KERN_INFO "md/raid:%s: device %s operational as raid" 5196 " disk %d\n", 5197 mdname(mddev), bdevname(rdev->bdev, b), raid_disk); 5198 } else if (rdev->saved_raid_disk != raid_disk) 5199 /* Cannot rely on bitmap to complete recovery */ 5200 conf->fullsync = 1; 5201 } 5202 5203 conf->chunk_sectors = mddev->new_chunk_sectors; 5204 conf->level = mddev->new_level; 5205 if (conf->level == 6) 5206 conf->max_degraded = 2; 5207 else 5208 conf->max_degraded = 1; 5209 conf->algorithm = mddev->new_layout; 5210 conf->max_nr_stripes = NR_STRIPES; 5211 conf->reshape_progress = mddev->reshape_position; 5212 if (conf->reshape_progress != MaxSector) { 5213 conf->prev_chunk_sectors = mddev->chunk_sectors; 5214 conf->prev_algo = mddev->layout; 5215 } 5216 5217 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + 5218 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; 5219 if (grow_stripes(conf, conf->max_nr_stripes)) { 5220 printk(KERN_ERR 5221 "md/raid:%s: couldn't allocate %dkB for buffers\n", 5222 mdname(mddev), memory); 5223 goto abort; 5224 } else 5225 printk(KERN_INFO "md/raid:%s: allocated %dkB\n", 5226 mdname(mddev), memory); 5227 5228 sprintf(pers_name, "raid%d", mddev->new_level); 5229 conf->thread = md_register_thread(raid5d, mddev, pers_name); 5230 if (!conf->thread) { 5231 printk(KERN_ERR 5232 "md/raid:%s: couldn't allocate thread.\n", 5233 mdname(mddev)); 5234 goto abort; 5235 } 5236 5237 return conf; 5238 5239 abort: 5240 if (conf) { 5241 free_conf(conf); 5242 return ERR_PTR(-EIO); 5243 } else 5244 return ERR_PTR(-ENOMEM); 5245 } 5246 5247 5248 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) 5249 { 5250 switch (algo) { 5251 case ALGORITHM_PARITY_0: 5252 if (raid_disk < max_degraded) 5253 return 1; 5254 break; 5255 case ALGORITHM_PARITY_N: 5256 if (raid_disk >= raid_disks - max_degraded) 5257 return 1; 5258 break; 5259 case ALGORITHM_PARITY_0_6: 5260 if (raid_disk == 0 || 5261 raid_disk == raid_disks - 1) 5262 return 1; 5263 break; 5264 case ALGORITHM_LEFT_ASYMMETRIC_6: 5265 case ALGORITHM_RIGHT_ASYMMETRIC_6: 5266 case ALGORITHM_LEFT_SYMMETRIC_6: 5267 case ALGORITHM_RIGHT_SYMMETRIC_6: 5268 if (raid_disk == raid_disks - 1) 5269 return 1; 5270 } 5271 return 0; 5272 } 5273 5274 static int run(struct mddev *mddev) 5275 { 5276 struct r5conf *conf; 5277 int working_disks = 0; 5278 int dirty_parity_disks = 0; 5279 struct md_rdev *rdev; 5280 sector_t reshape_offset = 0; 5281 int i; 5282 long long min_offset_diff = 0; 5283 int first = 1; 5284 5285 if (mddev->recovery_cp != MaxSector) 5286 printk(KERN_NOTICE "md/raid:%s: not clean" 5287 " -- starting background reconstruction\n", 5288 mdname(mddev)); 5289 5290 rdev_for_each(rdev, mddev) { 5291 long long diff; 5292 if (rdev->raid_disk < 0) 5293 continue; 5294 diff = (rdev->new_data_offset - rdev->data_offset); 5295 if (first) { 5296 min_offset_diff = diff; 5297 first = 0; 5298 } else if (mddev->reshape_backwards && 5299 diff < min_offset_diff) 5300 min_offset_diff = diff; 5301 else if (!mddev->reshape_backwards && 5302 diff > min_offset_diff) 5303 min_offset_diff = diff; 5304 } 5305 5306 if (mddev->reshape_position != MaxSector) { 5307 /* Check that we can continue the reshape. 5308 * Difficulties arise if the stripe we would write to 5309 * next is at or after the stripe we would read from next. 5310 * For a reshape that changes the number of devices, this 5311 * is only possible for a very short time, and mdadm makes 5312 * sure that time appears to have past before assembling 5313 * the array. So we fail if that time hasn't passed. 5314 * For a reshape that keeps the number of devices the same 5315 * mdadm must be monitoring the reshape can keeping the 5316 * critical areas read-only and backed up. It will start 5317 * the array in read-only mode, so we check for that. 5318 */ 5319 sector_t here_new, here_old; 5320 int old_disks; 5321 int max_degraded = (mddev->level == 6 ? 2 : 1); 5322 5323 if (mddev->new_level != mddev->level) { 5324 printk(KERN_ERR "md/raid:%s: unsupported reshape " 5325 "required - aborting.\n", 5326 mdname(mddev)); 5327 return -EINVAL; 5328 } 5329 old_disks = mddev->raid_disks - mddev->delta_disks; 5330 /* reshape_position must be on a new-stripe boundary, and one 5331 * further up in new geometry must map after here in old 5332 * geometry. 5333 */ 5334 here_new = mddev->reshape_position; 5335 if (sector_div(here_new, mddev->new_chunk_sectors * 5336 (mddev->raid_disks - max_degraded))) { 5337 printk(KERN_ERR "md/raid:%s: reshape_position not " 5338 "on a stripe boundary\n", mdname(mddev)); 5339 return -EINVAL; 5340 } 5341 reshape_offset = here_new * mddev->new_chunk_sectors; 5342 /* here_new is the stripe we will write to */ 5343 here_old = mddev->reshape_position; 5344 sector_div(here_old, mddev->chunk_sectors * 5345 (old_disks-max_degraded)); 5346 /* here_old is the first stripe that we might need to read 5347 * from */ 5348 if (mddev->delta_disks == 0) { 5349 if ((here_new * mddev->new_chunk_sectors != 5350 here_old * mddev->chunk_sectors)) { 5351 printk(KERN_ERR "md/raid:%s: reshape position is" 5352 " confused - aborting\n", mdname(mddev)); 5353 return -EINVAL; 5354 } 5355 /* We cannot be sure it is safe to start an in-place 5356 * reshape. It is only safe if user-space is monitoring 5357 * and taking constant backups. 5358 * mdadm always starts a situation like this in 5359 * readonly mode so it can take control before 5360 * allowing any writes. So just check for that. 5361 */ 5362 if (abs(min_offset_diff) >= mddev->chunk_sectors && 5363 abs(min_offset_diff) >= mddev->new_chunk_sectors) 5364 /* not really in-place - so OK */; 5365 else if (mddev->ro == 0) { 5366 printk(KERN_ERR "md/raid:%s: in-place reshape " 5367 "must be started in read-only mode " 5368 "- aborting\n", 5369 mdname(mddev)); 5370 return -EINVAL; 5371 } 5372 } else if (mddev->reshape_backwards 5373 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= 5374 here_old * mddev->chunk_sectors) 5375 : (here_new * mddev->new_chunk_sectors >= 5376 here_old * mddev->chunk_sectors + (-min_offset_diff))) { 5377 /* Reading from the same stripe as writing to - bad */ 5378 printk(KERN_ERR "md/raid:%s: reshape_position too early for " 5379 "auto-recovery - aborting.\n", 5380 mdname(mddev)); 5381 return -EINVAL; 5382 } 5383 printk(KERN_INFO "md/raid:%s: reshape will continue\n", 5384 mdname(mddev)); 5385 /* OK, we should be able to continue; */ 5386 } else { 5387 BUG_ON(mddev->level != mddev->new_level); 5388 BUG_ON(mddev->layout != mddev->new_layout); 5389 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); 5390 BUG_ON(mddev->delta_disks != 0); 5391 } 5392 5393 if (mddev->private == NULL) 5394 conf = setup_conf(mddev); 5395 else 5396 conf = mddev->private; 5397 5398 if (IS_ERR(conf)) 5399 return PTR_ERR(conf); 5400 5401 conf->min_offset_diff = min_offset_diff; 5402 mddev->thread = conf->thread; 5403 conf->thread = NULL; 5404 mddev->private = conf; 5405 5406 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; 5407 i++) { 5408 rdev = conf->disks[i].rdev; 5409 if (!rdev && conf->disks[i].replacement) { 5410 /* The replacement is all we have yet */ 5411 rdev = conf->disks[i].replacement; 5412 conf->disks[i].replacement = NULL; 5413 clear_bit(Replacement, &rdev->flags); 5414 conf->disks[i].rdev = rdev; 5415 } 5416 if (!rdev) 5417 continue; 5418 if (conf->disks[i].replacement && 5419 conf->reshape_progress != MaxSector) { 5420 /* replacements and reshape simply do not mix. */ 5421 printk(KERN_ERR "md: cannot handle concurrent " 5422 "replacement and reshape.\n"); 5423 goto abort; 5424 } 5425 if (test_bit(In_sync, &rdev->flags)) { 5426 working_disks++; 5427 continue; 5428 } 5429 /* This disc is not fully in-sync. However if it 5430 * just stored parity (beyond the recovery_offset), 5431 * when we don't need to be concerned about the 5432 * array being dirty. 5433 * When reshape goes 'backwards', we never have 5434 * partially completed devices, so we only need 5435 * to worry about reshape going forwards. 5436 */ 5437 /* Hack because v0.91 doesn't store recovery_offset properly. */ 5438 if (mddev->major_version == 0 && 5439 mddev->minor_version > 90) 5440 rdev->recovery_offset = reshape_offset; 5441 5442 if (rdev->recovery_offset < reshape_offset) { 5443 /* We need to check old and new layout */ 5444 if (!only_parity(rdev->raid_disk, 5445 conf->algorithm, 5446 conf->raid_disks, 5447 conf->max_degraded)) 5448 continue; 5449 } 5450 if (!only_parity(rdev->raid_disk, 5451 conf->prev_algo, 5452 conf->previous_raid_disks, 5453 conf->max_degraded)) 5454 continue; 5455 dirty_parity_disks++; 5456 } 5457 5458 /* 5459 * 0 for a fully functional array, 1 or 2 for a degraded array. 5460 */ 5461 mddev->degraded = calc_degraded(conf); 5462 5463 if (has_failed(conf)) { 5464 printk(KERN_ERR "md/raid:%s: not enough operational devices" 5465 " (%d/%d failed)\n", 5466 mdname(mddev), mddev->degraded, conf->raid_disks); 5467 goto abort; 5468 } 5469 5470 /* device size must be a multiple of chunk size */ 5471 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); 5472 mddev->resync_max_sectors = mddev->dev_sectors; 5473 5474 if (mddev->degraded > dirty_parity_disks && 5475 mddev->recovery_cp != MaxSector) { 5476 if (mddev->ok_start_degraded) 5477 printk(KERN_WARNING 5478 "md/raid:%s: starting dirty degraded array" 5479 " - data corruption possible.\n", 5480 mdname(mddev)); 5481 else { 5482 printk(KERN_ERR 5483 "md/raid:%s: cannot start dirty degraded array.\n", 5484 mdname(mddev)); 5485 goto abort; 5486 } 5487 } 5488 5489 if (mddev->degraded == 0) 5490 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" 5491 " devices, algorithm %d\n", mdname(mddev), conf->level, 5492 mddev->raid_disks-mddev->degraded, mddev->raid_disks, 5493 mddev->new_layout); 5494 else 5495 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" 5496 " out of %d devices, algorithm %d\n", 5497 mdname(mddev), conf->level, 5498 mddev->raid_disks - mddev->degraded, 5499 mddev->raid_disks, mddev->new_layout); 5500 5501 print_raid5_conf(conf); 5502 5503 if (conf->reshape_progress != MaxSector) { 5504 conf->reshape_safe = conf->reshape_progress; 5505 atomic_set(&conf->reshape_stripes, 0); 5506 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 5507 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 5508 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 5509 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 5510 mddev->sync_thread = md_register_thread(md_do_sync, mddev, 5511 "reshape"); 5512 } 5513 5514 5515 /* Ok, everything is just fine now */ 5516 if (mddev->to_remove == &raid5_attrs_group) 5517 mddev->to_remove = NULL; 5518 else if (mddev->kobj.sd && 5519 sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) 5520 printk(KERN_WARNING 5521 "raid5: failed to create sysfs attributes for %s\n", 5522 mdname(mddev)); 5523 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); 5524 5525 if (mddev->queue) { 5526 int chunk_size; 5527 bool discard_supported = true; 5528 /* read-ahead size must cover two whole stripes, which 5529 * is 2 * (datadisks) * chunksize where 'n' is the 5530 * number of raid devices 5531 */ 5532 int data_disks = conf->previous_raid_disks - conf->max_degraded; 5533 int stripe = data_disks * 5534 ((mddev->chunk_sectors << 9) / PAGE_SIZE); 5535 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) 5536 mddev->queue->backing_dev_info.ra_pages = 2 * stripe; 5537 5538 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); 5539 5540 mddev->queue->backing_dev_info.congested_data = mddev; 5541 mddev->queue->backing_dev_info.congested_fn = raid5_congested; 5542 5543 chunk_size = mddev->chunk_sectors << 9; 5544 blk_queue_io_min(mddev->queue, chunk_size); 5545 blk_queue_io_opt(mddev->queue, chunk_size * 5546 (conf->raid_disks - conf->max_degraded)); 5547 /* 5548 * We can only discard a whole stripe. It doesn't make sense to 5549 * discard data disk but write parity disk 5550 */ 5551 stripe = stripe * PAGE_SIZE; 5552 /* Round up to power of 2, as discard handling 5553 * currently assumes that */ 5554 while ((stripe-1) & stripe) 5555 stripe = (stripe | (stripe-1)) + 1; 5556 mddev->queue->limits.discard_alignment = stripe; 5557 mddev->queue->limits.discard_granularity = stripe; 5558 /* 5559 * unaligned part of discard request will be ignored, so can't 5560 * guarantee discard_zerors_data 5561 */ 5562 mddev->queue->limits.discard_zeroes_data = 0; 5563 5564 rdev_for_each(rdev, mddev) { 5565 disk_stack_limits(mddev->gendisk, rdev->bdev, 5566 rdev->data_offset << 9); 5567 disk_stack_limits(mddev->gendisk, rdev->bdev, 5568 rdev->new_data_offset << 9); 5569 /* 5570 * discard_zeroes_data is required, otherwise data 5571 * could be lost. Consider a scenario: discard a stripe 5572 * (the stripe could be inconsistent if 5573 * discard_zeroes_data is 0); write one disk of the 5574 * stripe (the stripe could be inconsistent again 5575 * depending on which disks are used to calculate 5576 * parity); the disk is broken; The stripe data of this 5577 * disk is lost. 5578 */ 5579 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || 5580 !bdev_get_queue(rdev->bdev)-> 5581 limits.discard_zeroes_data) 5582 discard_supported = false; 5583 } 5584 5585 if (discard_supported && 5586 mddev->queue->limits.max_discard_sectors >= stripe && 5587 mddev->queue->limits.discard_granularity >= stripe) 5588 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, 5589 mddev->queue); 5590 else 5591 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, 5592 mddev->queue); 5593 } 5594 5595 return 0; 5596 abort: 5597 md_unregister_thread(&mddev->thread); 5598 print_raid5_conf(conf); 5599 free_conf(conf); 5600 mddev->private = NULL; 5601 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev)); 5602 return -EIO; 5603 } 5604 5605 static int stop(struct mddev *mddev) 5606 { 5607 struct r5conf *conf = mddev->private; 5608 5609 md_unregister_thread(&mddev->thread); 5610 if (mddev->queue) 5611 mddev->queue->backing_dev_info.congested_fn = NULL; 5612 free_conf(conf); 5613 mddev->private = NULL; 5614 mddev->to_remove = &raid5_attrs_group; 5615 return 0; 5616 } 5617 5618 static void status(struct seq_file *seq, struct mddev *mddev) 5619 { 5620 struct r5conf *conf = mddev->private; 5621 int i; 5622 5623 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, 5624 mddev->chunk_sectors / 2, mddev->layout); 5625 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); 5626 for (i = 0; i < conf->raid_disks; i++) 5627 seq_printf (seq, "%s", 5628 conf->disks[i].rdev && 5629 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); 5630 seq_printf (seq, "]"); 5631 } 5632 5633 static void print_raid5_conf (struct r5conf *conf) 5634 { 5635 int i; 5636 struct disk_info *tmp; 5637 5638 printk(KERN_DEBUG "RAID conf printout:\n"); 5639 if (!conf) { 5640 printk("(conf==NULL)\n"); 5641 return; 5642 } 5643 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, 5644 conf->raid_disks, 5645 conf->raid_disks - conf->mddev->degraded); 5646 5647 for (i = 0; i < conf->raid_disks; i++) { 5648 char b[BDEVNAME_SIZE]; 5649 tmp = conf->disks + i; 5650 if (tmp->rdev) 5651 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", 5652 i, !test_bit(Faulty, &tmp->rdev->flags), 5653 bdevname(tmp->rdev->bdev, b)); 5654 } 5655 } 5656 5657 static int raid5_spare_active(struct mddev *mddev) 5658 { 5659 int i; 5660 struct r5conf *conf = mddev->private; 5661 struct disk_info *tmp; 5662 int count = 0; 5663 unsigned long flags; 5664 5665 for (i = 0; i < conf->raid_disks; i++) { 5666 tmp = conf->disks + i; 5667 if (tmp->replacement 5668 && tmp->replacement->recovery_offset == MaxSector 5669 && !test_bit(Faulty, &tmp->replacement->flags) 5670 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { 5671 /* Replacement has just become active. */ 5672 if (!tmp->rdev 5673 || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) 5674 count++; 5675 if (tmp->rdev) { 5676 /* Replaced device not technically faulty, 5677 * but we need to be sure it gets removed 5678 * and never re-added. 5679 */ 5680 set_bit(Faulty, &tmp->rdev->flags); 5681 sysfs_notify_dirent_safe( 5682 tmp->rdev->sysfs_state); 5683 } 5684 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); 5685 } else if (tmp->rdev 5686 && tmp->rdev->recovery_offset == MaxSector 5687 && !test_bit(Faulty, &tmp->rdev->flags) 5688 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { 5689 count++; 5690 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); 5691 } 5692 } 5693 spin_lock_irqsave(&conf->device_lock, flags); 5694 mddev->degraded = calc_degraded(conf); 5695 spin_unlock_irqrestore(&conf->device_lock, flags); 5696 print_raid5_conf(conf); 5697 return count; 5698 } 5699 5700 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 5701 { 5702 struct r5conf *conf = mddev->private; 5703 int err = 0; 5704 int number = rdev->raid_disk; 5705 struct md_rdev **rdevp; 5706 struct disk_info *p = conf->disks + number; 5707 5708 print_raid5_conf(conf); 5709 if (rdev == p->rdev) 5710 rdevp = &p->rdev; 5711 else if (rdev == p->replacement) 5712 rdevp = &p->replacement; 5713 else 5714 return 0; 5715 5716 if (number >= conf->raid_disks && 5717 conf->reshape_progress == MaxSector) 5718 clear_bit(In_sync, &rdev->flags); 5719 5720 if (test_bit(In_sync, &rdev->flags) || 5721 atomic_read(&rdev->nr_pending)) { 5722 err = -EBUSY; 5723 goto abort; 5724 } 5725 /* Only remove non-faulty devices if recovery 5726 * isn't possible. 5727 */ 5728 if (!test_bit(Faulty, &rdev->flags) && 5729 mddev->recovery_disabled != conf->recovery_disabled && 5730 !has_failed(conf) && 5731 (!p->replacement || p->replacement == rdev) && 5732 number < conf->raid_disks) { 5733 err = -EBUSY; 5734 goto abort; 5735 } 5736 *rdevp = NULL; 5737 synchronize_rcu(); 5738 if (atomic_read(&rdev->nr_pending)) { 5739 /* lost the race, try later */ 5740 err = -EBUSY; 5741 *rdevp = rdev; 5742 } else if (p->replacement) { 5743 /* We must have just cleared 'rdev' */ 5744 p->rdev = p->replacement; 5745 clear_bit(Replacement, &p->replacement->flags); 5746 smp_mb(); /* Make sure other CPUs may see both as identical 5747 * but will never see neither - if they are careful 5748 */ 5749 p->replacement = NULL; 5750 clear_bit(WantReplacement, &rdev->flags); 5751 } else 5752 /* We might have just removed the Replacement as faulty- 5753 * clear the bit just in case 5754 */ 5755 clear_bit(WantReplacement, &rdev->flags); 5756 abort: 5757 5758 print_raid5_conf(conf); 5759 return err; 5760 } 5761 5762 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) 5763 { 5764 struct r5conf *conf = mddev->private; 5765 int err = -EEXIST; 5766 int disk; 5767 struct disk_info *p; 5768 int first = 0; 5769 int last = conf->raid_disks - 1; 5770 5771 if (mddev->recovery_disabled == conf->recovery_disabled) 5772 return -EBUSY; 5773 5774 if (rdev->saved_raid_disk < 0 && has_failed(conf)) 5775 /* no point adding a device */ 5776 return -EINVAL; 5777 5778 if (rdev->raid_disk >= 0) 5779 first = last = rdev->raid_disk; 5780 5781 /* 5782 * find the disk ... but prefer rdev->saved_raid_disk 5783 * if possible. 5784 */ 5785 if (rdev->saved_raid_disk >= 0 && 5786 rdev->saved_raid_disk >= first && 5787 conf->disks[rdev->saved_raid_disk].rdev == NULL) 5788 first = rdev->saved_raid_disk; 5789 5790 for (disk = first; disk <= last; disk++) { 5791 p = conf->disks + disk; 5792 if (p->rdev == NULL) { 5793 clear_bit(In_sync, &rdev->flags); 5794 rdev->raid_disk = disk; 5795 err = 0; 5796 if (rdev->saved_raid_disk != disk) 5797 conf->fullsync = 1; 5798 rcu_assign_pointer(p->rdev, rdev); 5799 goto out; 5800 } 5801 } 5802 for (disk = first; disk <= last; disk++) { 5803 p = conf->disks + disk; 5804 if (test_bit(WantReplacement, &p->rdev->flags) && 5805 p->replacement == NULL) { 5806 clear_bit(In_sync, &rdev->flags); 5807 set_bit(Replacement, &rdev->flags); 5808 rdev->raid_disk = disk; 5809 err = 0; 5810 conf->fullsync = 1; 5811 rcu_assign_pointer(p->replacement, rdev); 5812 break; 5813 } 5814 } 5815 out: 5816 print_raid5_conf(conf); 5817 return err; 5818 } 5819 5820 static int raid5_resize(struct mddev *mddev, sector_t sectors) 5821 { 5822 /* no resync is happening, and there is enough space 5823 * on all devices, so we can resize. 5824 * We need to make sure resync covers any new space. 5825 * If the array is shrinking we should possibly wait until 5826 * any io in the removed space completes, but it hardly seems 5827 * worth it. 5828 */ 5829 sector_t newsize; 5830 sectors &= ~((sector_t)mddev->chunk_sectors - 1); 5831 newsize = raid5_size(mddev, sectors, mddev->raid_disks); 5832 if (mddev->external_size && 5833 mddev->array_sectors > newsize) 5834 return -EINVAL; 5835 if (mddev->bitmap) { 5836 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); 5837 if (ret) 5838 return ret; 5839 } 5840 md_set_array_sectors(mddev, newsize); 5841 set_capacity(mddev->gendisk, mddev->array_sectors); 5842 revalidate_disk(mddev->gendisk); 5843 if (sectors > mddev->dev_sectors && 5844 mddev->recovery_cp > mddev->dev_sectors) { 5845 mddev->recovery_cp = mddev->dev_sectors; 5846 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 5847 } 5848 mddev->dev_sectors = sectors; 5849 mddev->resync_max_sectors = sectors; 5850 return 0; 5851 } 5852 5853 static int check_stripe_cache(struct mddev *mddev) 5854 { 5855 /* Can only proceed if there are plenty of stripe_heads. 5856 * We need a minimum of one full stripe,, and for sensible progress 5857 * it is best to have about 4 times that. 5858 * If we require 4 times, then the default 256 4K stripe_heads will 5859 * allow for chunk sizes up to 256K, which is probably OK. 5860 * If the chunk size is greater, user-space should request more 5861 * stripe_heads first. 5862 */ 5863 struct r5conf *conf = mddev->private; 5864 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 5865 > conf->max_nr_stripes || 5866 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 5867 > conf->max_nr_stripes) { 5868 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", 5869 mdname(mddev), 5870 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) 5871 / STRIPE_SIZE)*4); 5872 return 0; 5873 } 5874 return 1; 5875 } 5876 5877 static int check_reshape(struct mddev *mddev) 5878 { 5879 struct r5conf *conf = mddev->private; 5880 5881 if (mddev->delta_disks == 0 && 5882 mddev->new_layout == mddev->layout && 5883 mddev->new_chunk_sectors == mddev->chunk_sectors) 5884 return 0; /* nothing to do */ 5885 if (has_failed(conf)) 5886 return -EINVAL; 5887 if (mddev->delta_disks < 0) { 5888 /* We might be able to shrink, but the devices must 5889 * be made bigger first. 5890 * For raid6, 4 is the minimum size. 5891 * Otherwise 2 is the minimum 5892 */ 5893 int min = 2; 5894 if (mddev->level == 6) 5895 min = 4; 5896 if (mddev->raid_disks + mddev->delta_disks < min) 5897 return -EINVAL; 5898 } 5899 5900 if (!check_stripe_cache(mddev)) 5901 return -ENOSPC; 5902 5903 return resize_stripes(conf, (conf->previous_raid_disks 5904 + mddev->delta_disks)); 5905 } 5906 5907 static int raid5_start_reshape(struct mddev *mddev) 5908 { 5909 struct r5conf *conf = mddev->private; 5910 struct md_rdev *rdev; 5911 int spares = 0; 5912 unsigned long flags; 5913 5914 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 5915 return -EBUSY; 5916 5917 if (!check_stripe_cache(mddev)) 5918 return -ENOSPC; 5919 5920 if (has_failed(conf)) 5921 return -EINVAL; 5922 5923 rdev_for_each(rdev, mddev) { 5924 if (!test_bit(In_sync, &rdev->flags) 5925 && !test_bit(Faulty, &rdev->flags)) 5926 spares++; 5927 } 5928 5929 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) 5930 /* Not enough devices even to make a degraded array 5931 * of that size 5932 */ 5933 return -EINVAL; 5934 5935 /* Refuse to reduce size of the array. Any reductions in 5936 * array size must be through explicit setting of array_size 5937 * attribute. 5938 */ 5939 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) 5940 < mddev->array_sectors) { 5941 printk(KERN_ERR "md/raid:%s: array size must be reduced " 5942 "before number of disks\n", mdname(mddev)); 5943 return -EINVAL; 5944 } 5945 5946 atomic_set(&conf->reshape_stripes, 0); 5947 spin_lock_irq(&conf->device_lock); 5948 conf->previous_raid_disks = conf->raid_disks; 5949 conf->raid_disks += mddev->delta_disks; 5950 conf->prev_chunk_sectors = conf->chunk_sectors; 5951 conf->chunk_sectors = mddev->new_chunk_sectors; 5952 conf->prev_algo = conf->algorithm; 5953 conf->algorithm = mddev->new_layout; 5954 conf->generation++; 5955 /* Code that selects data_offset needs to see the generation update 5956 * if reshape_progress has been set - so a memory barrier needed. 5957 */ 5958 smp_mb(); 5959 if (mddev->reshape_backwards) 5960 conf->reshape_progress = raid5_size(mddev, 0, 0); 5961 else 5962 conf->reshape_progress = 0; 5963 conf->reshape_safe = conf->reshape_progress; 5964 spin_unlock_irq(&conf->device_lock); 5965 5966 /* Add some new drives, as many as will fit. 5967 * We know there are enough to make the newly sized array work. 5968 * Don't add devices if we are reducing the number of 5969 * devices in the array. This is because it is not possible 5970 * to correctly record the "partially reconstructed" state of 5971 * such devices during the reshape and confusion could result. 5972 */ 5973 if (mddev->delta_disks >= 0) { 5974 rdev_for_each(rdev, mddev) 5975 if (rdev->raid_disk < 0 && 5976 !test_bit(Faulty, &rdev->flags)) { 5977 if (raid5_add_disk(mddev, rdev) == 0) { 5978 if (rdev->raid_disk 5979 >= conf->previous_raid_disks) 5980 set_bit(In_sync, &rdev->flags); 5981 else 5982 rdev->recovery_offset = 0; 5983 5984 if (sysfs_link_rdev(mddev, rdev)) 5985 /* Failure here is OK */; 5986 } 5987 } else if (rdev->raid_disk >= conf->previous_raid_disks 5988 && !test_bit(Faulty, &rdev->flags)) { 5989 /* This is a spare that was manually added */ 5990 set_bit(In_sync, &rdev->flags); 5991 } 5992 5993 /* When a reshape changes the number of devices, 5994 * ->degraded is measured against the larger of the 5995 * pre and post number of devices. 5996 */ 5997 spin_lock_irqsave(&conf->device_lock, flags); 5998 mddev->degraded = calc_degraded(conf); 5999 spin_unlock_irqrestore(&conf->device_lock, flags); 6000 } 6001 mddev->raid_disks = conf->raid_disks; 6002 mddev->reshape_position = conf->reshape_progress; 6003 set_bit(MD_CHANGE_DEVS, &mddev->flags); 6004 6005 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 6006 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 6007 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 6008 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 6009 mddev->sync_thread = md_register_thread(md_do_sync, mddev, 6010 "reshape"); 6011 if (!mddev->sync_thread) { 6012 mddev->recovery = 0; 6013 spin_lock_irq(&conf->device_lock); 6014 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; 6015 rdev_for_each(rdev, mddev) 6016 rdev->new_data_offset = rdev->data_offset; 6017 smp_wmb(); 6018 conf->reshape_progress = MaxSector; 6019 mddev->reshape_position = MaxSector; 6020 spin_unlock_irq(&conf->device_lock); 6021 return -EAGAIN; 6022 } 6023 conf->reshape_checkpoint = jiffies; 6024 md_wakeup_thread(mddev->sync_thread); 6025 md_new_event(mddev); 6026 return 0; 6027 } 6028 6029 /* This is called from the reshape thread and should make any 6030 * changes needed in 'conf' 6031 */ 6032 static void end_reshape(struct r5conf *conf) 6033 { 6034 6035 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { 6036 struct md_rdev *rdev; 6037 6038 spin_lock_irq(&conf->device_lock); 6039 conf->previous_raid_disks = conf->raid_disks; 6040 rdev_for_each(rdev, conf->mddev) 6041 rdev->data_offset = rdev->new_data_offset; 6042 smp_wmb(); 6043 conf->reshape_progress = MaxSector; 6044 spin_unlock_irq(&conf->device_lock); 6045 wake_up(&conf->wait_for_overlap); 6046 6047 /* read-ahead size must cover two whole stripes, which is 6048 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices 6049 */ 6050 if (conf->mddev->queue) { 6051 int data_disks = conf->raid_disks - conf->max_degraded; 6052 int stripe = data_disks * ((conf->chunk_sectors << 9) 6053 / PAGE_SIZE); 6054 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) 6055 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; 6056 } 6057 } 6058 } 6059 6060 /* This is called from the raid5d thread with mddev_lock held. 6061 * It makes config changes to the device. 6062 */ 6063 static void raid5_finish_reshape(struct mddev *mddev) 6064 { 6065 struct r5conf *conf = mddev->private; 6066 6067 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { 6068 6069 if (mddev->delta_disks > 0) { 6070 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); 6071 set_capacity(mddev->gendisk, mddev->array_sectors); 6072 revalidate_disk(mddev->gendisk); 6073 } else { 6074 int d; 6075 spin_lock_irq(&conf->device_lock); 6076 mddev->degraded = calc_degraded(conf); 6077 spin_unlock_irq(&conf->device_lock); 6078 for (d = conf->raid_disks ; 6079 d < conf->raid_disks - mddev->delta_disks; 6080 d++) { 6081 struct md_rdev *rdev = conf->disks[d].rdev; 6082 if (rdev) 6083 clear_bit(In_sync, &rdev->flags); 6084 rdev = conf->disks[d].replacement; 6085 if (rdev) 6086 clear_bit(In_sync, &rdev->flags); 6087 } 6088 } 6089 mddev->layout = conf->algorithm; 6090 mddev->chunk_sectors = conf->chunk_sectors; 6091 mddev->reshape_position = MaxSector; 6092 mddev->delta_disks = 0; 6093 mddev->reshape_backwards = 0; 6094 } 6095 } 6096 6097 static void raid5_quiesce(struct mddev *mddev, int state) 6098 { 6099 struct r5conf *conf = mddev->private; 6100 6101 switch(state) { 6102 case 2: /* resume for a suspend */ 6103 wake_up(&conf->wait_for_overlap); 6104 break; 6105 6106 case 1: /* stop all writes */ 6107 spin_lock_irq(&conf->device_lock); 6108 /* '2' tells resync/reshape to pause so that all 6109 * active stripes can drain 6110 */ 6111 conf->quiesce = 2; 6112 wait_event_lock_irq(conf->wait_for_stripe, 6113 atomic_read(&conf->active_stripes) == 0 && 6114 atomic_read(&conf->active_aligned_reads) == 0, 6115 conf->device_lock); 6116 conf->quiesce = 1; 6117 spin_unlock_irq(&conf->device_lock); 6118 /* allow reshape to continue */ 6119 wake_up(&conf->wait_for_overlap); 6120 break; 6121 6122 case 0: /* re-enable writes */ 6123 spin_lock_irq(&conf->device_lock); 6124 conf->quiesce = 0; 6125 wake_up(&conf->wait_for_stripe); 6126 wake_up(&conf->wait_for_overlap); 6127 spin_unlock_irq(&conf->device_lock); 6128 break; 6129 } 6130 } 6131 6132 6133 static void *raid45_takeover_raid0(struct mddev *mddev, int level) 6134 { 6135 struct r0conf *raid0_conf = mddev->private; 6136 sector_t sectors; 6137 6138 /* for raid0 takeover only one zone is supported */ 6139 if (raid0_conf->nr_strip_zones > 1) { 6140 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", 6141 mdname(mddev)); 6142 return ERR_PTR(-EINVAL); 6143 } 6144 6145 sectors = raid0_conf->strip_zone[0].zone_end; 6146 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); 6147 mddev->dev_sectors = sectors; 6148 mddev->new_level = level; 6149 mddev->new_layout = ALGORITHM_PARITY_N; 6150 mddev->new_chunk_sectors = mddev->chunk_sectors; 6151 mddev->raid_disks += 1; 6152 mddev->delta_disks = 1; 6153 /* make sure it will be not marked as dirty */ 6154 mddev->recovery_cp = MaxSector; 6155 6156 return setup_conf(mddev); 6157 } 6158 6159 6160 static void *raid5_takeover_raid1(struct mddev *mddev) 6161 { 6162 int chunksect; 6163 6164 if (mddev->raid_disks != 2 || 6165 mddev->degraded > 1) 6166 return ERR_PTR(-EINVAL); 6167 6168 /* Should check if there are write-behind devices? */ 6169 6170 chunksect = 64*2; /* 64K by default */ 6171 6172 /* The array must be an exact multiple of chunksize */ 6173 while (chunksect && (mddev->array_sectors & (chunksect-1))) 6174 chunksect >>= 1; 6175 6176 if ((chunksect<<9) < STRIPE_SIZE) 6177 /* array size does not allow a suitable chunk size */ 6178 return ERR_PTR(-EINVAL); 6179 6180 mddev->new_level = 5; 6181 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; 6182 mddev->new_chunk_sectors = chunksect; 6183 6184 return setup_conf(mddev); 6185 } 6186 6187 static void *raid5_takeover_raid6(struct mddev *mddev) 6188 { 6189 int new_layout; 6190 6191 switch (mddev->layout) { 6192 case ALGORITHM_LEFT_ASYMMETRIC_6: 6193 new_layout = ALGORITHM_LEFT_ASYMMETRIC; 6194 break; 6195 case ALGORITHM_RIGHT_ASYMMETRIC_6: 6196 new_layout = ALGORITHM_RIGHT_ASYMMETRIC; 6197 break; 6198 case ALGORITHM_LEFT_SYMMETRIC_6: 6199 new_layout = ALGORITHM_LEFT_SYMMETRIC; 6200 break; 6201 case ALGORITHM_RIGHT_SYMMETRIC_6: 6202 new_layout = ALGORITHM_RIGHT_SYMMETRIC; 6203 break; 6204 case ALGORITHM_PARITY_0_6: 6205 new_layout = ALGORITHM_PARITY_0; 6206 break; 6207 case ALGORITHM_PARITY_N: 6208 new_layout = ALGORITHM_PARITY_N; 6209 break; 6210 default: 6211 return ERR_PTR(-EINVAL); 6212 } 6213 mddev->new_level = 5; 6214 mddev->new_layout = new_layout; 6215 mddev->delta_disks = -1; 6216 mddev->raid_disks -= 1; 6217 return setup_conf(mddev); 6218 } 6219 6220 6221 static int raid5_check_reshape(struct mddev *mddev) 6222 { 6223 /* For a 2-drive array, the layout and chunk size can be changed 6224 * immediately as not restriping is needed. 6225 * For larger arrays we record the new value - after validation 6226 * to be used by a reshape pass. 6227 */ 6228 struct r5conf *conf = mddev->private; 6229 int new_chunk = mddev->new_chunk_sectors; 6230 6231 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) 6232 return -EINVAL; 6233 if (new_chunk > 0) { 6234 if (!is_power_of_2(new_chunk)) 6235 return -EINVAL; 6236 if (new_chunk < (PAGE_SIZE>>9)) 6237 return -EINVAL; 6238 if (mddev->array_sectors & (new_chunk-1)) 6239 /* not factor of array size */ 6240 return -EINVAL; 6241 } 6242 6243 /* They look valid */ 6244 6245 if (mddev->raid_disks == 2) { 6246 /* can make the change immediately */ 6247 if (mddev->new_layout >= 0) { 6248 conf->algorithm = mddev->new_layout; 6249 mddev->layout = mddev->new_layout; 6250 } 6251 if (new_chunk > 0) { 6252 conf->chunk_sectors = new_chunk ; 6253 mddev->chunk_sectors = new_chunk; 6254 } 6255 set_bit(MD_CHANGE_DEVS, &mddev->flags); 6256 md_wakeup_thread(mddev->thread); 6257 } 6258 return check_reshape(mddev); 6259 } 6260 6261 static int raid6_check_reshape(struct mddev *mddev) 6262 { 6263 int new_chunk = mddev->new_chunk_sectors; 6264 6265 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) 6266 return -EINVAL; 6267 if (new_chunk > 0) { 6268 if (!is_power_of_2(new_chunk)) 6269 return -EINVAL; 6270 if (new_chunk < (PAGE_SIZE >> 9)) 6271 return -EINVAL; 6272 if (mddev->array_sectors & (new_chunk-1)) 6273 /* not factor of array size */ 6274 return -EINVAL; 6275 } 6276 6277 /* They look valid */ 6278 return check_reshape(mddev); 6279 } 6280 6281 static void *raid5_takeover(struct mddev *mddev) 6282 { 6283 /* raid5 can take over: 6284 * raid0 - if there is only one strip zone - make it a raid4 layout 6285 * raid1 - if there are two drives. We need to know the chunk size 6286 * raid4 - trivial - just use a raid4 layout. 6287 * raid6 - Providing it is a *_6 layout 6288 */ 6289 if (mddev->level == 0) 6290 return raid45_takeover_raid0(mddev, 5); 6291 if (mddev->level == 1) 6292 return raid5_takeover_raid1(mddev); 6293 if (mddev->level == 4) { 6294 mddev->new_layout = ALGORITHM_PARITY_N; 6295 mddev->new_level = 5; 6296 return setup_conf(mddev); 6297 } 6298 if (mddev->level == 6) 6299 return raid5_takeover_raid6(mddev); 6300 6301 return ERR_PTR(-EINVAL); 6302 } 6303 6304 static void *raid4_takeover(struct mddev *mddev) 6305 { 6306 /* raid4 can take over: 6307 * raid0 - if there is only one strip zone 6308 * raid5 - if layout is right 6309 */ 6310 if (mddev->level == 0) 6311 return raid45_takeover_raid0(mddev, 4); 6312 if (mddev->level == 5 && 6313 mddev->layout == ALGORITHM_PARITY_N) { 6314 mddev->new_layout = 0; 6315 mddev->new_level = 4; 6316 return setup_conf(mddev); 6317 } 6318 return ERR_PTR(-EINVAL); 6319 } 6320 6321 static struct md_personality raid5_personality; 6322 6323 static void *raid6_takeover(struct mddev *mddev) 6324 { 6325 /* Currently can only take over a raid5. We map the 6326 * personality to an equivalent raid6 personality 6327 * with the Q block at the end. 6328 */ 6329 int new_layout; 6330 6331 if (mddev->pers != &raid5_personality) 6332 return ERR_PTR(-EINVAL); 6333 if (mddev->degraded > 1) 6334 return ERR_PTR(-EINVAL); 6335 if (mddev->raid_disks > 253) 6336 return ERR_PTR(-EINVAL); 6337 if (mddev->raid_disks < 3) 6338 return ERR_PTR(-EINVAL); 6339 6340 switch (mddev->layout) { 6341 case ALGORITHM_LEFT_ASYMMETRIC: 6342 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; 6343 break; 6344 case ALGORITHM_RIGHT_ASYMMETRIC: 6345 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; 6346 break; 6347 case ALGORITHM_LEFT_SYMMETRIC: 6348 new_layout = ALGORITHM_LEFT_SYMMETRIC_6; 6349 break; 6350 case ALGORITHM_RIGHT_SYMMETRIC: 6351 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; 6352 break; 6353 case ALGORITHM_PARITY_0: 6354 new_layout = ALGORITHM_PARITY_0_6; 6355 break; 6356 case ALGORITHM_PARITY_N: 6357 new_layout = ALGORITHM_PARITY_N; 6358 break; 6359 default: 6360 return ERR_PTR(-EINVAL); 6361 } 6362 mddev->new_level = 6; 6363 mddev->new_layout = new_layout; 6364 mddev->delta_disks = 1; 6365 mddev->raid_disks += 1; 6366 return setup_conf(mddev); 6367 } 6368 6369 6370 static struct md_personality raid6_personality = 6371 { 6372 .name = "raid6", 6373 .level = 6, 6374 .owner = THIS_MODULE, 6375 .make_request = make_request, 6376 .run = run, 6377 .stop = stop, 6378 .status = status, 6379 .error_handler = error, 6380 .hot_add_disk = raid5_add_disk, 6381 .hot_remove_disk= raid5_remove_disk, 6382 .spare_active = raid5_spare_active, 6383 .sync_request = sync_request, 6384 .resize = raid5_resize, 6385 .size = raid5_size, 6386 .check_reshape = raid6_check_reshape, 6387 .start_reshape = raid5_start_reshape, 6388 .finish_reshape = raid5_finish_reshape, 6389 .quiesce = raid5_quiesce, 6390 .takeover = raid6_takeover, 6391 }; 6392 static struct md_personality raid5_personality = 6393 { 6394 .name = "raid5", 6395 .level = 5, 6396 .owner = THIS_MODULE, 6397 .make_request = make_request, 6398 .run = run, 6399 .stop = stop, 6400 .status = status, 6401 .error_handler = error, 6402 .hot_add_disk = raid5_add_disk, 6403 .hot_remove_disk= raid5_remove_disk, 6404 .spare_active = raid5_spare_active, 6405 .sync_request = sync_request, 6406 .resize = raid5_resize, 6407 .size = raid5_size, 6408 .check_reshape = raid5_check_reshape, 6409 .start_reshape = raid5_start_reshape, 6410 .finish_reshape = raid5_finish_reshape, 6411 .quiesce = raid5_quiesce, 6412 .takeover = raid5_takeover, 6413 }; 6414 6415 static struct md_personality raid4_personality = 6416 { 6417 .name = "raid4", 6418 .level = 4, 6419 .owner = THIS_MODULE, 6420 .make_request = make_request, 6421 .run = run, 6422 .stop = stop, 6423 .status = status, 6424 .error_handler = error, 6425 .hot_add_disk = raid5_add_disk, 6426 .hot_remove_disk= raid5_remove_disk, 6427 .spare_active = raid5_spare_active, 6428 .sync_request = sync_request, 6429 .resize = raid5_resize, 6430 .size = raid5_size, 6431 .check_reshape = raid5_check_reshape, 6432 .start_reshape = raid5_start_reshape, 6433 .finish_reshape = raid5_finish_reshape, 6434 .quiesce = raid5_quiesce, 6435 .takeover = raid4_takeover, 6436 }; 6437 6438 static int __init raid5_init(void) 6439 { 6440 register_md_personality(&raid6_personality); 6441 register_md_personality(&raid5_personality); 6442 register_md_personality(&raid4_personality); 6443 return 0; 6444 } 6445 6446 static void raid5_exit(void) 6447 { 6448 unregister_md_personality(&raid6_personality); 6449 unregister_md_personality(&raid5_personality); 6450 unregister_md_personality(&raid4_personality); 6451 } 6452 6453 module_init(raid5_init); 6454 module_exit(raid5_exit); 6455 MODULE_LICENSE("GPL"); 6456 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); 6457 MODULE_ALIAS("md-personality-4"); /* RAID5 */ 6458 MODULE_ALIAS("md-raid5"); 6459 MODULE_ALIAS("md-raid4"); 6460 MODULE_ALIAS("md-level-5"); 6461 MODULE_ALIAS("md-level-4"); 6462 MODULE_ALIAS("md-personality-8"); /* RAID6 */ 6463 MODULE_ALIAS("md-raid6"); 6464 MODULE_ALIAS("md-level-6"); 6465 6466 /* This used to be two separate modules, they were: */ 6467 MODULE_ALIAS("raid5"); 6468 MODULE_ALIAS("raid6"); 6469