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