1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * raid5.c : Multiple Devices driver for Linux 4 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman 5 * Copyright (C) 1999, 2000 Ingo Molnar 6 * Copyright (C) 2002, 2003 H. Peter Anvin 7 * 8 * RAID-4/5/6 management functions. 9 * Thanks to Penguin Computing for making the RAID-6 development possible 10 * by donating a test server! 11 */ 12 13 /* 14 * BITMAP UNPLUGGING: 15 * 16 * The sequencing for updating the bitmap reliably is a little 17 * subtle (and I got it wrong the first time) so it deserves some 18 * explanation. 19 * 20 * We group bitmap updates into batches. Each batch has a number. 21 * We may write out several batches at once, but that isn't very important. 22 * conf->seq_write is the number of the last batch successfully written. 23 * conf->seq_flush is the number of the last batch that was closed to 24 * new additions. 25 * When we discover that we will need to write to any block in a stripe 26 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq 27 * the number of the batch it will be in. This is seq_flush+1. 28 * When we are ready to do a write, if that batch hasn't been written yet, 29 * we plug the array and queue the stripe for later. 30 * When an unplug happens, we increment bm_flush, thus closing the current 31 * batch. 32 * When we notice that bm_flush > bm_write, we write out all pending updates 33 * to the bitmap, and advance bm_write to where bm_flush was. 34 * This may occasionally write a bit out twice, but is sure never to 35 * miss any bits. 36 */ 37 38 #include <linux/blkdev.h> 39 #include <linux/kthread.h> 40 #include <linux/raid/pq.h> 41 #include <linux/async_tx.h> 42 #include <linux/module.h> 43 #include <linux/async.h> 44 #include <linux/seq_file.h> 45 #include <linux/cpu.h> 46 #include <linux/slab.h> 47 #include <linux/ratelimit.h> 48 #include <linux/nodemask.h> 49 50 #include <trace/events/block.h> 51 #include <linux/list_sort.h> 52 53 #include "md.h" 54 #include "raid5.h" 55 #include "raid0.h" 56 #include "md-bitmap.h" 57 #include "raid5-log.h" 58 59 #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED) 60 61 #define cpu_to_group(cpu) cpu_to_node(cpu) 62 #define ANY_GROUP NUMA_NO_NODE 63 64 static bool devices_handle_discard_safely = false; 65 module_param(devices_handle_discard_safely, bool, 0644); 66 MODULE_PARM_DESC(devices_handle_discard_safely, 67 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions"); 68 static struct workqueue_struct *raid5_wq; 69 70 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) 71 { 72 int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK; 73 return &conf->stripe_hashtbl[hash]; 74 } 75 76 static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect) 77 { 78 return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK; 79 } 80 81 static inline void lock_device_hash_lock(struct r5conf *conf, int hash) 82 __acquires(&conf->device_lock) 83 { 84 spin_lock_irq(conf->hash_locks + hash); 85 spin_lock(&conf->device_lock); 86 } 87 88 static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) 89 __releases(&conf->device_lock) 90 { 91 spin_unlock(&conf->device_lock); 92 spin_unlock_irq(conf->hash_locks + hash); 93 } 94 95 static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) 96 __acquires(&conf->device_lock) 97 { 98 int i; 99 spin_lock_irq(conf->hash_locks); 100 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) 101 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); 102 spin_lock(&conf->device_lock); 103 } 104 105 static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) 106 __releases(&conf->device_lock) 107 { 108 int i; 109 spin_unlock(&conf->device_lock); 110 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--) 111 spin_unlock(conf->hash_locks + i); 112 spin_unlock_irq(conf->hash_locks); 113 } 114 115 /* Find first data disk in a raid6 stripe */ 116 static inline int raid6_d0(struct stripe_head *sh) 117 { 118 if (sh->ddf_layout) 119 /* ddf always start from first device */ 120 return 0; 121 /* md starts just after Q block */ 122 if (sh->qd_idx == sh->disks - 1) 123 return 0; 124 else 125 return sh->qd_idx + 1; 126 } 127 static inline int raid6_next_disk(int disk, int raid_disks) 128 { 129 disk++; 130 return (disk < raid_disks) ? disk : 0; 131 } 132 133 /* When walking through the disks in a raid5, starting at raid6_d0, 134 * We need to map each disk to a 'slot', where the data disks are slot 135 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk 136 * is raid_disks-1. This help does that mapping. 137 */ 138 static int raid6_idx_to_slot(int idx, struct stripe_head *sh, 139 int *count, int syndrome_disks) 140 { 141 int slot = *count; 142 143 if (sh->ddf_layout) 144 (*count)++; 145 if (idx == sh->pd_idx) 146 return syndrome_disks; 147 if (idx == sh->qd_idx) 148 return syndrome_disks + 1; 149 if (!sh->ddf_layout) 150 (*count)++; 151 return slot; 152 } 153 154 static void print_raid5_conf (struct r5conf *conf); 155 156 static int stripe_operations_active(struct stripe_head *sh) 157 { 158 return sh->check_state || sh->reconstruct_state || 159 test_bit(STRIPE_BIOFILL_RUN, &sh->state) || 160 test_bit(STRIPE_COMPUTE_RUN, &sh->state); 161 } 162 163 static bool stripe_is_lowprio(struct stripe_head *sh) 164 { 165 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) || 166 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) && 167 !test_bit(STRIPE_R5C_CACHING, &sh->state); 168 } 169 170 static void raid5_wakeup_stripe_thread(struct stripe_head *sh) 171 __must_hold(&sh->raid_conf->device_lock) 172 { 173 struct r5conf *conf = sh->raid_conf; 174 struct r5worker_group *group; 175 int thread_cnt; 176 int i, cpu = sh->cpu; 177 178 if (!cpu_online(cpu)) { 179 cpu = cpumask_any(cpu_online_mask); 180 sh->cpu = cpu; 181 } 182 183 if (list_empty(&sh->lru)) { 184 struct r5worker_group *group; 185 group = conf->worker_groups + cpu_to_group(cpu); 186 if (stripe_is_lowprio(sh)) 187 list_add_tail(&sh->lru, &group->loprio_list); 188 else 189 list_add_tail(&sh->lru, &group->handle_list); 190 group->stripes_cnt++; 191 sh->group = group; 192 } 193 194 if (conf->worker_cnt_per_group == 0) { 195 md_wakeup_thread(conf->mddev->thread); 196 return; 197 } 198 199 group = conf->worker_groups + cpu_to_group(sh->cpu); 200 201 group->workers[0].working = true; 202 /* at least one worker should run to avoid race */ 203 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); 204 205 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; 206 /* wakeup more workers */ 207 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { 208 if (group->workers[i].working == false) { 209 group->workers[i].working = true; 210 queue_work_on(sh->cpu, raid5_wq, 211 &group->workers[i].work); 212 thread_cnt--; 213 } 214 } 215 } 216 217 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, 218 struct list_head *temp_inactive_list) 219 __must_hold(&conf->device_lock) 220 { 221 int i; 222 int injournal = 0; /* number of date pages with R5_InJournal */ 223 224 BUG_ON(!list_empty(&sh->lru)); 225 BUG_ON(atomic_read(&conf->active_stripes)==0); 226 227 if (r5c_is_writeback(conf->log)) 228 for (i = sh->disks; i--; ) 229 if (test_bit(R5_InJournal, &sh->dev[i].flags)) 230 injournal++; 231 /* 232 * In the following cases, the stripe cannot be released to cached 233 * lists. Therefore, we make the stripe write out and set 234 * STRIPE_HANDLE: 235 * 1. when quiesce in r5c write back; 236 * 2. when resync is requested fot the stripe. 237 */ 238 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) || 239 (conf->quiesce && r5c_is_writeback(conf->log) && 240 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) { 241 if (test_bit(STRIPE_R5C_CACHING, &sh->state)) 242 r5c_make_stripe_write_out(sh); 243 set_bit(STRIPE_HANDLE, &sh->state); 244 } 245 246 if (test_bit(STRIPE_HANDLE, &sh->state)) { 247 if (test_bit(STRIPE_DELAYED, &sh->state) && 248 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 249 list_add_tail(&sh->lru, &conf->delayed_list); 250 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && 251 sh->bm_seq - conf->seq_write > 0) 252 list_add_tail(&sh->lru, &conf->bitmap_list); 253 else { 254 clear_bit(STRIPE_DELAYED, &sh->state); 255 clear_bit(STRIPE_BIT_DELAY, &sh->state); 256 if (conf->worker_cnt_per_group == 0) { 257 if (stripe_is_lowprio(sh)) 258 list_add_tail(&sh->lru, 259 &conf->loprio_list); 260 else 261 list_add_tail(&sh->lru, 262 &conf->handle_list); 263 } else { 264 raid5_wakeup_stripe_thread(sh); 265 return; 266 } 267 } 268 md_wakeup_thread(conf->mddev->thread); 269 } else { 270 BUG_ON(stripe_operations_active(sh)); 271 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 272 if (atomic_dec_return(&conf->preread_active_stripes) 273 < IO_THRESHOLD) 274 md_wakeup_thread(conf->mddev->thread); 275 atomic_dec(&conf->active_stripes); 276 if (!test_bit(STRIPE_EXPANDING, &sh->state)) { 277 if (!r5c_is_writeback(conf->log)) 278 list_add_tail(&sh->lru, temp_inactive_list); 279 else { 280 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); 281 if (injournal == 0) 282 list_add_tail(&sh->lru, temp_inactive_list); 283 else if (injournal == conf->raid_disks - conf->max_degraded) { 284 /* full stripe */ 285 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) 286 atomic_inc(&conf->r5c_cached_full_stripes); 287 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) 288 atomic_dec(&conf->r5c_cached_partial_stripes); 289 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); 290 r5c_check_cached_full_stripe(conf); 291 } else 292 /* 293 * STRIPE_R5C_PARTIAL_STRIPE is set in 294 * r5c_try_caching_write(). No need to 295 * set it again. 296 */ 297 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); 298 } 299 } 300 } 301 } 302 303 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, 304 struct list_head *temp_inactive_list) 305 __must_hold(&conf->device_lock) 306 { 307 if (atomic_dec_and_test(&sh->count)) 308 do_release_stripe(conf, sh, temp_inactive_list); 309 } 310 311 /* 312 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list 313 * 314 * Be careful: Only one task can add/delete stripes from temp_inactive_list at 315 * given time. Adding stripes only takes device lock, while deleting stripes 316 * only takes hash lock. 317 */ 318 static void release_inactive_stripe_list(struct r5conf *conf, 319 struct list_head *temp_inactive_list, 320 int hash) 321 { 322 int size; 323 bool do_wakeup = false; 324 unsigned long flags; 325 326 if (hash == NR_STRIPE_HASH_LOCKS) { 327 size = NR_STRIPE_HASH_LOCKS; 328 hash = NR_STRIPE_HASH_LOCKS - 1; 329 } else 330 size = 1; 331 while (size) { 332 struct list_head *list = &temp_inactive_list[size - 1]; 333 334 /* 335 * We don't hold any lock here yet, raid5_get_active_stripe() might 336 * remove stripes from the list 337 */ 338 if (!list_empty_careful(list)) { 339 spin_lock_irqsave(conf->hash_locks + hash, flags); 340 if (list_empty(conf->inactive_list + hash) && 341 !list_empty(list)) 342 atomic_dec(&conf->empty_inactive_list_nr); 343 list_splice_tail_init(list, conf->inactive_list + hash); 344 do_wakeup = true; 345 spin_unlock_irqrestore(conf->hash_locks + hash, flags); 346 } 347 size--; 348 hash--; 349 } 350 351 if (do_wakeup) { 352 wake_up(&conf->wait_for_stripe); 353 if (atomic_read(&conf->active_stripes) == 0) 354 wake_up(&conf->wait_for_quiescent); 355 if (conf->retry_read_aligned) 356 md_wakeup_thread(conf->mddev->thread); 357 } 358 } 359 360 static int release_stripe_list(struct r5conf *conf, 361 struct list_head *temp_inactive_list) 362 __must_hold(&conf->device_lock) 363 { 364 struct stripe_head *sh, *t; 365 int count = 0; 366 struct llist_node *head; 367 368 head = llist_del_all(&conf->released_stripes); 369 head = llist_reverse_order(head); 370 llist_for_each_entry_safe(sh, t, head, release_list) { 371 int hash; 372 373 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ 374 smp_mb(); 375 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); 376 /* 377 * Don't worry the bit is set here, because if the bit is set 378 * again, the count is always > 1. This is true for 379 * STRIPE_ON_UNPLUG_LIST bit too. 380 */ 381 hash = sh->hash_lock_index; 382 __release_stripe(conf, sh, &temp_inactive_list[hash]); 383 count++; 384 } 385 386 return count; 387 } 388 389 void raid5_release_stripe(struct stripe_head *sh) 390 { 391 struct r5conf *conf = sh->raid_conf; 392 unsigned long flags; 393 struct list_head list; 394 int hash; 395 bool wakeup; 396 397 /* Avoid release_list until the last reference. 398 */ 399 if (atomic_add_unless(&sh->count, -1, 1)) 400 return; 401 402 if (unlikely(!conf->mddev->thread) || 403 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) 404 goto slow_path; 405 wakeup = llist_add(&sh->release_list, &conf->released_stripes); 406 if (wakeup) 407 md_wakeup_thread(conf->mddev->thread); 408 return; 409 slow_path: 410 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */ 411 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) { 412 INIT_LIST_HEAD(&list); 413 hash = sh->hash_lock_index; 414 do_release_stripe(conf, sh, &list); 415 spin_unlock_irqrestore(&conf->device_lock, flags); 416 release_inactive_stripe_list(conf, &list, hash); 417 } 418 } 419 420 static inline void remove_hash(struct stripe_head *sh) 421 { 422 pr_debug("remove_hash(), stripe %llu\n", 423 (unsigned long long)sh->sector); 424 425 hlist_del_init(&sh->hash); 426 } 427 428 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) 429 { 430 struct hlist_head *hp = stripe_hash(conf, sh->sector); 431 432 pr_debug("insert_hash(), stripe %llu\n", 433 (unsigned long long)sh->sector); 434 435 hlist_add_head(&sh->hash, hp); 436 } 437 438 /* find an idle stripe, make sure it is unhashed, and return it. */ 439 static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) 440 { 441 struct stripe_head *sh = NULL; 442 struct list_head *first; 443 444 if (list_empty(conf->inactive_list + hash)) 445 goto out; 446 first = (conf->inactive_list + hash)->next; 447 sh = list_entry(first, struct stripe_head, lru); 448 list_del_init(first); 449 remove_hash(sh); 450 atomic_inc(&conf->active_stripes); 451 BUG_ON(hash != sh->hash_lock_index); 452 if (list_empty(conf->inactive_list + hash)) 453 atomic_inc(&conf->empty_inactive_list_nr); 454 out: 455 return sh; 456 } 457 458 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 459 static void free_stripe_pages(struct stripe_head *sh) 460 { 461 int i; 462 struct page *p; 463 464 /* Have not allocate page pool */ 465 if (!sh->pages) 466 return; 467 468 for (i = 0; i < sh->nr_pages; i++) { 469 p = sh->pages[i]; 470 if (p) 471 put_page(p); 472 sh->pages[i] = NULL; 473 } 474 } 475 476 static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp) 477 { 478 int i; 479 struct page *p; 480 481 for (i = 0; i < sh->nr_pages; i++) { 482 /* The page have allocated. */ 483 if (sh->pages[i]) 484 continue; 485 486 p = alloc_page(gfp); 487 if (!p) { 488 free_stripe_pages(sh); 489 return -ENOMEM; 490 } 491 sh->pages[i] = p; 492 } 493 return 0; 494 } 495 496 static int 497 init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks) 498 { 499 int nr_pages, cnt; 500 501 if (sh->pages) 502 return 0; 503 504 /* Each of the sh->dev[i] need one conf->stripe_size */ 505 cnt = PAGE_SIZE / conf->stripe_size; 506 nr_pages = (disks + cnt - 1) / cnt; 507 508 sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); 509 if (!sh->pages) 510 return -ENOMEM; 511 sh->nr_pages = nr_pages; 512 sh->stripes_per_page = cnt; 513 return 0; 514 } 515 #endif 516 517 static void shrink_buffers(struct stripe_head *sh) 518 { 519 int i; 520 int num = sh->raid_conf->pool_size; 521 522 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE 523 for (i = 0; i < num ; i++) { 524 struct page *p; 525 526 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); 527 p = sh->dev[i].page; 528 if (!p) 529 continue; 530 sh->dev[i].page = NULL; 531 put_page(p); 532 } 533 #else 534 for (i = 0; i < num; i++) 535 sh->dev[i].page = NULL; 536 free_stripe_pages(sh); /* Free pages */ 537 #endif 538 } 539 540 static int grow_buffers(struct stripe_head *sh, gfp_t gfp) 541 { 542 int i; 543 int num = sh->raid_conf->pool_size; 544 545 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE 546 for (i = 0; i < num; i++) { 547 struct page *page; 548 549 if (!(page = alloc_page(gfp))) { 550 return 1; 551 } 552 sh->dev[i].page = page; 553 sh->dev[i].orig_page = page; 554 sh->dev[i].offset = 0; 555 } 556 #else 557 if (alloc_stripe_pages(sh, gfp)) 558 return -ENOMEM; 559 560 for (i = 0; i < num; i++) { 561 sh->dev[i].page = raid5_get_dev_page(sh, i); 562 sh->dev[i].orig_page = sh->dev[i].page; 563 sh->dev[i].offset = raid5_get_page_offset(sh, i); 564 } 565 #endif 566 return 0; 567 } 568 569 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, 570 struct stripe_head *sh); 571 572 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) 573 { 574 struct r5conf *conf = sh->raid_conf; 575 int i, seq; 576 577 BUG_ON(atomic_read(&sh->count) != 0); 578 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); 579 BUG_ON(stripe_operations_active(sh)); 580 BUG_ON(sh->batch_head); 581 582 pr_debug("init_stripe called, stripe %llu\n", 583 (unsigned long long)sector); 584 retry: 585 seq = read_seqcount_begin(&conf->gen_lock); 586 sh->generation = conf->generation - previous; 587 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; 588 sh->sector = sector; 589 stripe_set_idx(sector, conf, previous, sh); 590 sh->state = 0; 591 592 for (i = sh->disks; i--; ) { 593 struct r5dev *dev = &sh->dev[i]; 594 595 if (dev->toread || dev->read || dev->towrite || dev->written || 596 test_bit(R5_LOCKED, &dev->flags)) { 597 pr_err("sector=%llx i=%d %p %p %p %p %d\n", 598 (unsigned long long)sh->sector, i, dev->toread, 599 dev->read, dev->towrite, dev->written, 600 test_bit(R5_LOCKED, &dev->flags)); 601 WARN_ON(1); 602 } 603 dev->flags = 0; 604 dev->sector = raid5_compute_blocknr(sh, i, previous); 605 } 606 if (read_seqcount_retry(&conf->gen_lock, seq)) 607 goto retry; 608 sh->overwrite_disks = 0; 609 insert_hash(conf, sh); 610 sh->cpu = smp_processor_id(); 611 set_bit(STRIPE_BATCH_READY, &sh->state); 612 } 613 614 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, 615 short generation) 616 { 617 struct stripe_head *sh; 618 619 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); 620 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) 621 if (sh->sector == sector && sh->generation == generation) 622 return sh; 623 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); 624 return NULL; 625 } 626 627 /* 628 * Need to check if array has failed when deciding whether to: 629 * - start an array 630 * - remove non-faulty devices 631 * - add a spare 632 * - allow a reshape 633 * This determination is simple when no reshape is happening. 634 * However if there is a reshape, we need to carefully check 635 * both the before and after sections. 636 * This is because some failed devices may only affect one 637 * of the two sections, and some non-in_sync devices may 638 * be insync in the section most affected by failed devices. 639 * 640 * Most calls to this function hold &conf->device_lock. Calls 641 * in raid5_run() do not require the lock as no other threads 642 * have been started yet. 643 */ 644 int raid5_calc_degraded(struct r5conf *conf) 645 { 646 int degraded, degraded2; 647 int i; 648 649 rcu_read_lock(); 650 degraded = 0; 651 for (i = 0; i < conf->previous_raid_disks; i++) { 652 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 653 if (rdev && test_bit(Faulty, &rdev->flags)) 654 rdev = rcu_dereference(conf->disks[i].replacement); 655 if (!rdev || test_bit(Faulty, &rdev->flags)) 656 degraded++; 657 else if (test_bit(In_sync, &rdev->flags)) 658 ; 659 else 660 /* not in-sync or faulty. 661 * If the reshape increases the number of devices, 662 * this is being recovered by the reshape, so 663 * this 'previous' section is not in_sync. 664 * If the number of devices is being reduced however, 665 * the device can only be part of the array if 666 * we are reverting a reshape, so this section will 667 * be in-sync. 668 */ 669 if (conf->raid_disks >= conf->previous_raid_disks) 670 degraded++; 671 } 672 rcu_read_unlock(); 673 if (conf->raid_disks == conf->previous_raid_disks) 674 return degraded; 675 rcu_read_lock(); 676 degraded2 = 0; 677 for (i = 0; i < conf->raid_disks; i++) { 678 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 679 if (rdev && test_bit(Faulty, &rdev->flags)) 680 rdev = rcu_dereference(conf->disks[i].replacement); 681 if (!rdev || test_bit(Faulty, &rdev->flags)) 682 degraded2++; 683 else if (test_bit(In_sync, &rdev->flags)) 684 ; 685 else 686 /* not in-sync or faulty. 687 * If reshape increases the number of devices, this 688 * section has already been recovered, else it 689 * almost certainly hasn't. 690 */ 691 if (conf->raid_disks <= conf->previous_raid_disks) 692 degraded2++; 693 } 694 rcu_read_unlock(); 695 if (degraded2 > degraded) 696 return degraded2; 697 return degraded; 698 } 699 700 static bool has_failed(struct r5conf *conf) 701 { 702 int degraded = conf->mddev->degraded; 703 704 if (test_bit(MD_BROKEN, &conf->mddev->flags)) 705 return true; 706 707 if (conf->mddev->reshape_position != MaxSector) 708 degraded = raid5_calc_degraded(conf); 709 710 return degraded > conf->max_degraded; 711 } 712 713 struct stripe_head * 714 raid5_get_active_stripe(struct r5conf *conf, sector_t sector, 715 int previous, int noblock, int noquiesce) 716 { 717 struct stripe_head *sh; 718 int hash = stripe_hash_locks_hash(conf, sector); 719 int inc_empty_inactive_list_flag; 720 721 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); 722 723 spin_lock_irq(conf->hash_locks + hash); 724 725 do { 726 wait_event_lock_irq(conf->wait_for_quiescent, 727 conf->quiesce == 0 || noquiesce, 728 *(conf->hash_locks + hash)); 729 sh = __find_stripe(conf, sector, conf->generation - previous); 730 if (!sh) { 731 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) { 732 sh = get_free_stripe(conf, hash); 733 if (!sh && !test_bit(R5_DID_ALLOC, 734 &conf->cache_state)) 735 set_bit(R5_ALLOC_MORE, 736 &conf->cache_state); 737 } 738 if (noblock && sh == NULL) 739 break; 740 741 r5c_check_stripe_cache_usage(conf); 742 if (!sh) { 743 set_bit(R5_INACTIVE_BLOCKED, 744 &conf->cache_state); 745 r5l_wake_reclaim(conf->log, 0); 746 wait_event_lock_irq( 747 conf->wait_for_stripe, 748 !list_empty(conf->inactive_list + hash) && 749 (atomic_read(&conf->active_stripes) 750 < (conf->max_nr_stripes * 3 / 4) 751 || !test_bit(R5_INACTIVE_BLOCKED, 752 &conf->cache_state)), 753 *(conf->hash_locks + hash)); 754 clear_bit(R5_INACTIVE_BLOCKED, 755 &conf->cache_state); 756 } else { 757 init_stripe(sh, sector, previous); 758 atomic_inc(&sh->count); 759 } 760 } else if (!atomic_inc_not_zero(&sh->count)) { 761 spin_lock(&conf->device_lock); 762 if (!atomic_read(&sh->count)) { 763 if (!test_bit(STRIPE_HANDLE, &sh->state)) 764 atomic_inc(&conf->active_stripes); 765 BUG_ON(list_empty(&sh->lru) && 766 !test_bit(STRIPE_EXPANDING, &sh->state)); 767 inc_empty_inactive_list_flag = 0; 768 if (!list_empty(conf->inactive_list + hash)) 769 inc_empty_inactive_list_flag = 1; 770 list_del_init(&sh->lru); 771 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) 772 atomic_inc(&conf->empty_inactive_list_nr); 773 if (sh->group) { 774 sh->group->stripes_cnt--; 775 sh->group = NULL; 776 } 777 } 778 atomic_inc(&sh->count); 779 spin_unlock(&conf->device_lock); 780 } 781 } while (sh == NULL); 782 783 spin_unlock_irq(conf->hash_locks + hash); 784 return sh; 785 } 786 787 static bool is_full_stripe_write(struct stripe_head *sh) 788 { 789 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded)); 790 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded); 791 } 792 793 static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) 794 __acquires(&sh1->stripe_lock) 795 __acquires(&sh2->stripe_lock) 796 { 797 if (sh1 > sh2) { 798 spin_lock_irq(&sh2->stripe_lock); 799 spin_lock_nested(&sh1->stripe_lock, 1); 800 } else { 801 spin_lock_irq(&sh1->stripe_lock); 802 spin_lock_nested(&sh2->stripe_lock, 1); 803 } 804 } 805 806 static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) 807 __releases(&sh1->stripe_lock) 808 __releases(&sh2->stripe_lock) 809 { 810 spin_unlock(&sh1->stripe_lock); 811 spin_unlock_irq(&sh2->stripe_lock); 812 } 813 814 /* Only freshly new full stripe normal write stripe can be added to a batch list */ 815 static bool stripe_can_batch(struct stripe_head *sh) 816 { 817 struct r5conf *conf = sh->raid_conf; 818 819 if (raid5_has_log(conf) || raid5_has_ppl(conf)) 820 return false; 821 return test_bit(STRIPE_BATCH_READY, &sh->state) && 822 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) && 823 is_full_stripe_write(sh); 824 } 825 826 /* we only do back search */ 827 static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh) 828 { 829 struct stripe_head *head; 830 sector_t head_sector, tmp_sec; 831 int hash; 832 int dd_idx; 833 int inc_empty_inactive_list_flag; 834 835 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */ 836 tmp_sec = sh->sector; 837 if (!sector_div(tmp_sec, conf->chunk_sectors)) 838 return; 839 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf); 840 841 hash = stripe_hash_locks_hash(conf, head_sector); 842 spin_lock_irq(conf->hash_locks + hash); 843 head = __find_stripe(conf, head_sector, conf->generation); 844 if (head && !atomic_inc_not_zero(&head->count)) { 845 spin_lock(&conf->device_lock); 846 if (!atomic_read(&head->count)) { 847 if (!test_bit(STRIPE_HANDLE, &head->state)) 848 atomic_inc(&conf->active_stripes); 849 BUG_ON(list_empty(&head->lru) && 850 !test_bit(STRIPE_EXPANDING, &head->state)); 851 inc_empty_inactive_list_flag = 0; 852 if (!list_empty(conf->inactive_list + hash)) 853 inc_empty_inactive_list_flag = 1; 854 list_del_init(&head->lru); 855 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) 856 atomic_inc(&conf->empty_inactive_list_nr); 857 if (head->group) { 858 head->group->stripes_cnt--; 859 head->group = NULL; 860 } 861 } 862 atomic_inc(&head->count); 863 spin_unlock(&conf->device_lock); 864 } 865 spin_unlock_irq(conf->hash_locks + hash); 866 867 if (!head) 868 return; 869 if (!stripe_can_batch(head)) 870 goto out; 871 872 lock_two_stripes(head, sh); 873 /* clear_batch_ready clear the flag */ 874 if (!stripe_can_batch(head) || !stripe_can_batch(sh)) 875 goto unlock_out; 876 877 if (sh->batch_head) 878 goto unlock_out; 879 880 dd_idx = 0; 881 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) 882 dd_idx++; 883 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf || 884 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite)) 885 goto unlock_out; 886 887 if (head->batch_head) { 888 spin_lock(&head->batch_head->batch_lock); 889 /* This batch list is already running */ 890 if (!stripe_can_batch(head)) { 891 spin_unlock(&head->batch_head->batch_lock); 892 goto unlock_out; 893 } 894 /* 895 * We must assign batch_head of this stripe within the 896 * batch_lock, otherwise clear_batch_ready of batch head 897 * stripe could clear BATCH_READY bit of this stripe and 898 * this stripe->batch_head doesn't get assigned, which 899 * could confuse clear_batch_ready for this stripe 900 */ 901 sh->batch_head = head->batch_head; 902 903 /* 904 * at this point, head's BATCH_READY could be cleared, but we 905 * can still add the stripe to batch list 906 */ 907 list_add(&sh->batch_list, &head->batch_list); 908 spin_unlock(&head->batch_head->batch_lock); 909 } else { 910 head->batch_head = head; 911 sh->batch_head = head->batch_head; 912 spin_lock(&head->batch_lock); 913 list_add_tail(&sh->batch_list, &head->batch_list); 914 spin_unlock(&head->batch_lock); 915 } 916 917 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 918 if (atomic_dec_return(&conf->preread_active_stripes) 919 < IO_THRESHOLD) 920 md_wakeup_thread(conf->mddev->thread); 921 922 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) { 923 int seq = sh->bm_seq; 924 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) && 925 sh->batch_head->bm_seq > seq) 926 seq = sh->batch_head->bm_seq; 927 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state); 928 sh->batch_head->bm_seq = seq; 929 } 930 931 atomic_inc(&sh->count); 932 unlock_out: 933 unlock_two_stripes(head, sh); 934 out: 935 raid5_release_stripe(head); 936 } 937 938 /* Determine if 'data_offset' or 'new_data_offset' should be used 939 * in this stripe_head. 940 */ 941 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) 942 { 943 sector_t progress = conf->reshape_progress; 944 /* Need a memory barrier to make sure we see the value 945 * of conf->generation, or ->data_offset that was set before 946 * reshape_progress was updated. 947 */ 948 smp_rmb(); 949 if (progress == MaxSector) 950 return 0; 951 if (sh->generation == conf->generation - 1) 952 return 0; 953 /* We are in a reshape, and this is a new-generation stripe, 954 * so use new_data_offset. 955 */ 956 return 1; 957 } 958 959 static void dispatch_bio_list(struct bio_list *tmp) 960 { 961 struct bio *bio; 962 963 while ((bio = bio_list_pop(tmp))) 964 submit_bio_noacct(bio); 965 } 966 967 static int cmp_stripe(void *priv, const struct list_head *a, 968 const struct list_head *b) 969 { 970 const struct r5pending_data *da = list_entry(a, 971 struct r5pending_data, sibling); 972 const struct r5pending_data *db = list_entry(b, 973 struct r5pending_data, sibling); 974 if (da->sector > db->sector) 975 return 1; 976 if (da->sector < db->sector) 977 return -1; 978 return 0; 979 } 980 981 static void dispatch_defer_bios(struct r5conf *conf, int target, 982 struct bio_list *list) 983 { 984 struct r5pending_data *data; 985 struct list_head *first, *next = NULL; 986 int cnt = 0; 987 988 if (conf->pending_data_cnt == 0) 989 return; 990 991 list_sort(NULL, &conf->pending_list, cmp_stripe); 992 993 first = conf->pending_list.next; 994 995 /* temporarily move the head */ 996 if (conf->next_pending_data) 997 list_move_tail(&conf->pending_list, 998 &conf->next_pending_data->sibling); 999 1000 while (!list_empty(&conf->pending_list)) { 1001 data = list_first_entry(&conf->pending_list, 1002 struct r5pending_data, sibling); 1003 if (&data->sibling == first) 1004 first = data->sibling.next; 1005 next = data->sibling.next; 1006 1007 bio_list_merge(list, &data->bios); 1008 list_move(&data->sibling, &conf->free_list); 1009 cnt++; 1010 if (cnt >= target) 1011 break; 1012 } 1013 conf->pending_data_cnt -= cnt; 1014 BUG_ON(conf->pending_data_cnt < 0 || cnt < target); 1015 1016 if (next != &conf->pending_list) 1017 conf->next_pending_data = list_entry(next, 1018 struct r5pending_data, sibling); 1019 else 1020 conf->next_pending_data = NULL; 1021 /* list isn't empty */ 1022 if (first != &conf->pending_list) 1023 list_move_tail(&conf->pending_list, first); 1024 } 1025 1026 static void flush_deferred_bios(struct r5conf *conf) 1027 { 1028 struct bio_list tmp = BIO_EMPTY_LIST; 1029 1030 if (conf->pending_data_cnt == 0) 1031 return; 1032 1033 spin_lock(&conf->pending_bios_lock); 1034 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp); 1035 BUG_ON(conf->pending_data_cnt != 0); 1036 spin_unlock(&conf->pending_bios_lock); 1037 1038 dispatch_bio_list(&tmp); 1039 } 1040 1041 static void defer_issue_bios(struct r5conf *conf, sector_t sector, 1042 struct bio_list *bios) 1043 { 1044 struct bio_list tmp = BIO_EMPTY_LIST; 1045 struct r5pending_data *ent; 1046 1047 spin_lock(&conf->pending_bios_lock); 1048 ent = list_first_entry(&conf->free_list, struct r5pending_data, 1049 sibling); 1050 list_move_tail(&ent->sibling, &conf->pending_list); 1051 ent->sector = sector; 1052 bio_list_init(&ent->bios); 1053 bio_list_merge(&ent->bios, bios); 1054 conf->pending_data_cnt++; 1055 if (conf->pending_data_cnt >= PENDING_IO_MAX) 1056 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp); 1057 1058 spin_unlock(&conf->pending_bios_lock); 1059 1060 dispatch_bio_list(&tmp); 1061 } 1062 1063 static void 1064 raid5_end_read_request(struct bio *bi); 1065 static void 1066 raid5_end_write_request(struct bio *bi); 1067 1068 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) 1069 { 1070 struct r5conf *conf = sh->raid_conf; 1071 int i, disks = sh->disks; 1072 struct stripe_head *head_sh = sh; 1073 struct bio_list pending_bios = BIO_EMPTY_LIST; 1074 struct r5dev *dev; 1075 bool should_defer; 1076 1077 might_sleep(); 1078 1079 if (log_stripe(sh, s) == 0) 1080 return; 1081 1082 should_defer = conf->batch_bio_dispatch && conf->group_cnt; 1083 1084 for (i = disks; i--; ) { 1085 int op, op_flags = 0; 1086 int replace_only = 0; 1087 struct bio *bi, *rbi; 1088 struct md_rdev *rdev, *rrdev = NULL; 1089 1090 sh = head_sh; 1091 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { 1092 op = REQ_OP_WRITE; 1093 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) 1094 op_flags = REQ_FUA; 1095 if (test_bit(R5_Discard, &sh->dev[i].flags)) 1096 op = REQ_OP_DISCARD; 1097 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) 1098 op = REQ_OP_READ; 1099 else if (test_and_clear_bit(R5_WantReplace, 1100 &sh->dev[i].flags)) { 1101 op = REQ_OP_WRITE; 1102 replace_only = 1; 1103 } else 1104 continue; 1105 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) 1106 op_flags |= REQ_SYNC; 1107 1108 again: 1109 dev = &sh->dev[i]; 1110 bi = &dev->req; 1111 rbi = &dev->rreq; /* For writing to replacement */ 1112 1113 rcu_read_lock(); 1114 rrdev = rcu_dereference(conf->disks[i].replacement); 1115 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ 1116 rdev = rcu_dereference(conf->disks[i].rdev); 1117 if (!rdev) { 1118 rdev = rrdev; 1119 rrdev = NULL; 1120 } 1121 if (op_is_write(op)) { 1122 if (replace_only) 1123 rdev = NULL; 1124 if (rdev == rrdev) 1125 /* We raced and saw duplicates */ 1126 rrdev = NULL; 1127 } else { 1128 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) 1129 rdev = rrdev; 1130 rrdev = NULL; 1131 } 1132 1133 if (rdev && test_bit(Faulty, &rdev->flags)) 1134 rdev = NULL; 1135 if (rdev) 1136 atomic_inc(&rdev->nr_pending); 1137 if (rrdev && test_bit(Faulty, &rrdev->flags)) 1138 rrdev = NULL; 1139 if (rrdev) 1140 atomic_inc(&rrdev->nr_pending); 1141 rcu_read_unlock(); 1142 1143 /* We have already checked bad blocks for reads. Now 1144 * need to check for writes. We never accept write errors 1145 * on the replacement, so we don't to check rrdev. 1146 */ 1147 while (op_is_write(op) && rdev && 1148 test_bit(WriteErrorSeen, &rdev->flags)) { 1149 sector_t first_bad; 1150 int bad_sectors; 1151 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 1152 &first_bad, &bad_sectors); 1153 if (!bad) 1154 break; 1155 1156 if (bad < 0) { 1157 set_bit(BlockedBadBlocks, &rdev->flags); 1158 if (!conf->mddev->external && 1159 conf->mddev->sb_flags) { 1160 /* It is very unlikely, but we might 1161 * still need to write out the 1162 * bad block log - better give it 1163 * a chance*/ 1164 md_check_recovery(conf->mddev); 1165 } 1166 /* 1167 * Because md_wait_for_blocked_rdev 1168 * will dec nr_pending, we must 1169 * increment it first. 1170 */ 1171 atomic_inc(&rdev->nr_pending); 1172 md_wait_for_blocked_rdev(rdev, conf->mddev); 1173 } else { 1174 /* Acknowledged bad block - skip the write */ 1175 rdev_dec_pending(rdev, conf->mddev); 1176 rdev = NULL; 1177 } 1178 } 1179 1180 if (rdev) { 1181 if (s->syncing || s->expanding || s->expanded 1182 || s->replacing) 1183 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf)); 1184 1185 set_bit(STRIPE_IO_STARTED, &sh->state); 1186 1187 bio_init(bi, rdev->bdev, &dev->vec, 1, op | op_flags); 1188 bi->bi_end_io = op_is_write(op) 1189 ? raid5_end_write_request 1190 : raid5_end_read_request; 1191 bi->bi_private = sh; 1192 1193 pr_debug("%s: for %llu schedule op %d on disc %d\n", 1194 __func__, (unsigned long long)sh->sector, 1195 bi->bi_opf, i); 1196 atomic_inc(&sh->count); 1197 if (sh != head_sh) 1198 atomic_inc(&head_sh->count); 1199 if (use_new_offset(conf, sh)) 1200 bi->bi_iter.bi_sector = (sh->sector 1201 + rdev->new_data_offset); 1202 else 1203 bi->bi_iter.bi_sector = (sh->sector 1204 + rdev->data_offset); 1205 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) 1206 bi->bi_opf |= REQ_NOMERGE; 1207 1208 if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) 1209 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); 1210 1211 if (!op_is_write(op) && 1212 test_bit(R5_InJournal, &sh->dev[i].flags)) 1213 /* 1214 * issuing read for a page in journal, this 1215 * must be preparing for prexor in rmw; read 1216 * the data into orig_page 1217 */ 1218 sh->dev[i].vec.bv_page = sh->dev[i].orig_page; 1219 else 1220 sh->dev[i].vec.bv_page = sh->dev[i].page; 1221 bi->bi_vcnt = 1; 1222 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); 1223 bi->bi_io_vec[0].bv_offset = sh->dev[i].offset; 1224 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); 1225 /* 1226 * If this is discard request, set bi_vcnt 0. We don't 1227 * want to confuse SCSI because SCSI will replace payload 1228 */ 1229 if (op == REQ_OP_DISCARD) 1230 bi->bi_vcnt = 0; 1231 if (rrdev) 1232 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); 1233 1234 if (conf->mddev->gendisk) 1235 trace_block_bio_remap(bi, 1236 disk_devt(conf->mddev->gendisk), 1237 sh->dev[i].sector); 1238 if (should_defer && op_is_write(op)) 1239 bio_list_add(&pending_bios, bi); 1240 else 1241 submit_bio_noacct(bi); 1242 } 1243 if (rrdev) { 1244 if (s->syncing || s->expanding || s->expanded 1245 || s->replacing) 1246 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf)); 1247 1248 set_bit(STRIPE_IO_STARTED, &sh->state); 1249 1250 bio_init(rbi, rrdev->bdev, &dev->rvec, 1, op | op_flags); 1251 BUG_ON(!op_is_write(op)); 1252 rbi->bi_end_io = raid5_end_write_request; 1253 rbi->bi_private = sh; 1254 1255 pr_debug("%s: for %llu schedule op %d on " 1256 "replacement disc %d\n", 1257 __func__, (unsigned long long)sh->sector, 1258 rbi->bi_opf, i); 1259 atomic_inc(&sh->count); 1260 if (sh != head_sh) 1261 atomic_inc(&head_sh->count); 1262 if (use_new_offset(conf, sh)) 1263 rbi->bi_iter.bi_sector = (sh->sector 1264 + rrdev->new_data_offset); 1265 else 1266 rbi->bi_iter.bi_sector = (sh->sector 1267 + rrdev->data_offset); 1268 if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) 1269 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); 1270 sh->dev[i].rvec.bv_page = sh->dev[i].page; 1271 rbi->bi_vcnt = 1; 1272 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); 1273 rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset; 1274 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); 1275 /* 1276 * If this is discard request, set bi_vcnt 0. We don't 1277 * want to confuse SCSI because SCSI will replace payload 1278 */ 1279 if (op == REQ_OP_DISCARD) 1280 rbi->bi_vcnt = 0; 1281 if (conf->mddev->gendisk) 1282 trace_block_bio_remap(rbi, 1283 disk_devt(conf->mddev->gendisk), 1284 sh->dev[i].sector); 1285 if (should_defer && op_is_write(op)) 1286 bio_list_add(&pending_bios, rbi); 1287 else 1288 submit_bio_noacct(rbi); 1289 } 1290 if (!rdev && !rrdev) { 1291 if (op_is_write(op)) 1292 set_bit(STRIPE_DEGRADED, &sh->state); 1293 pr_debug("skip op %d on disc %d for sector %llu\n", 1294 bi->bi_opf, i, (unsigned long long)sh->sector); 1295 clear_bit(R5_LOCKED, &sh->dev[i].flags); 1296 set_bit(STRIPE_HANDLE, &sh->state); 1297 } 1298 1299 if (!head_sh->batch_head) 1300 continue; 1301 sh = list_first_entry(&sh->batch_list, struct stripe_head, 1302 batch_list); 1303 if (sh != head_sh) 1304 goto again; 1305 } 1306 1307 if (should_defer && !bio_list_empty(&pending_bios)) 1308 defer_issue_bios(conf, head_sh->sector, &pending_bios); 1309 } 1310 1311 static struct dma_async_tx_descriptor * 1312 async_copy_data(int frombio, struct bio *bio, struct page **page, 1313 unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx, 1314 struct stripe_head *sh, int no_skipcopy) 1315 { 1316 struct bio_vec bvl; 1317 struct bvec_iter iter; 1318 struct page *bio_page; 1319 int page_offset; 1320 struct async_submit_ctl submit; 1321 enum async_tx_flags flags = 0; 1322 struct r5conf *conf = sh->raid_conf; 1323 1324 if (bio->bi_iter.bi_sector >= sector) 1325 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; 1326 else 1327 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; 1328 1329 if (frombio) 1330 flags |= ASYNC_TX_FENCE; 1331 init_async_submit(&submit, flags, tx, NULL, NULL, NULL); 1332 1333 bio_for_each_segment(bvl, bio, iter) { 1334 int len = bvl.bv_len; 1335 int clen; 1336 int b_offset = 0; 1337 1338 if (page_offset < 0) { 1339 b_offset = -page_offset; 1340 page_offset += b_offset; 1341 len -= b_offset; 1342 } 1343 1344 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf)) 1345 clen = RAID5_STRIPE_SIZE(conf) - page_offset; 1346 else 1347 clen = len; 1348 1349 if (clen > 0) { 1350 b_offset += bvl.bv_offset; 1351 bio_page = bvl.bv_page; 1352 if (frombio) { 1353 if (conf->skip_copy && 1354 b_offset == 0 && page_offset == 0 && 1355 clen == RAID5_STRIPE_SIZE(conf) && 1356 !no_skipcopy) 1357 *page = bio_page; 1358 else 1359 tx = async_memcpy(*page, bio_page, page_offset + poff, 1360 b_offset, clen, &submit); 1361 } else 1362 tx = async_memcpy(bio_page, *page, b_offset, 1363 page_offset + poff, clen, &submit); 1364 } 1365 /* chain the operations */ 1366 submit.depend_tx = tx; 1367 1368 if (clen < len) /* hit end of page */ 1369 break; 1370 page_offset += len; 1371 } 1372 1373 return tx; 1374 } 1375 1376 static void ops_complete_biofill(void *stripe_head_ref) 1377 { 1378 struct stripe_head *sh = stripe_head_ref; 1379 int i; 1380 struct r5conf *conf = sh->raid_conf; 1381 1382 pr_debug("%s: stripe %llu\n", __func__, 1383 (unsigned long long)sh->sector); 1384 1385 /* clear completed biofills */ 1386 for (i = sh->disks; i--; ) { 1387 struct r5dev *dev = &sh->dev[i]; 1388 1389 /* acknowledge completion of a biofill operation */ 1390 /* and check if we need to reply to a read request, 1391 * new R5_Wantfill requests are held off until 1392 * !STRIPE_BIOFILL_RUN 1393 */ 1394 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { 1395 struct bio *rbi, *rbi2; 1396 1397 BUG_ON(!dev->read); 1398 rbi = dev->read; 1399 dev->read = NULL; 1400 while (rbi && rbi->bi_iter.bi_sector < 1401 dev->sector + RAID5_STRIPE_SECTORS(conf)) { 1402 rbi2 = r5_next_bio(conf, rbi, dev->sector); 1403 bio_endio(rbi); 1404 rbi = rbi2; 1405 } 1406 } 1407 } 1408 clear_bit(STRIPE_BIOFILL_RUN, &sh->state); 1409 1410 set_bit(STRIPE_HANDLE, &sh->state); 1411 raid5_release_stripe(sh); 1412 } 1413 1414 static void ops_run_biofill(struct stripe_head *sh) 1415 { 1416 struct dma_async_tx_descriptor *tx = NULL; 1417 struct async_submit_ctl submit; 1418 int i; 1419 struct r5conf *conf = sh->raid_conf; 1420 1421 BUG_ON(sh->batch_head); 1422 pr_debug("%s: stripe %llu\n", __func__, 1423 (unsigned long long)sh->sector); 1424 1425 for (i = sh->disks; i--; ) { 1426 struct r5dev *dev = &sh->dev[i]; 1427 if (test_bit(R5_Wantfill, &dev->flags)) { 1428 struct bio *rbi; 1429 spin_lock_irq(&sh->stripe_lock); 1430 dev->read = rbi = dev->toread; 1431 dev->toread = NULL; 1432 spin_unlock_irq(&sh->stripe_lock); 1433 while (rbi && rbi->bi_iter.bi_sector < 1434 dev->sector + RAID5_STRIPE_SECTORS(conf)) { 1435 tx = async_copy_data(0, rbi, &dev->page, 1436 dev->offset, 1437 dev->sector, tx, sh, 0); 1438 rbi = r5_next_bio(conf, rbi, dev->sector); 1439 } 1440 } 1441 } 1442 1443 atomic_inc(&sh->count); 1444 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); 1445 async_trigger_callback(&submit); 1446 } 1447 1448 static void mark_target_uptodate(struct stripe_head *sh, int target) 1449 { 1450 struct r5dev *tgt; 1451 1452 if (target < 0) 1453 return; 1454 1455 tgt = &sh->dev[target]; 1456 set_bit(R5_UPTODATE, &tgt->flags); 1457 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 1458 clear_bit(R5_Wantcompute, &tgt->flags); 1459 } 1460 1461 static void ops_complete_compute(void *stripe_head_ref) 1462 { 1463 struct stripe_head *sh = stripe_head_ref; 1464 1465 pr_debug("%s: stripe %llu\n", __func__, 1466 (unsigned long long)sh->sector); 1467 1468 /* mark the computed target(s) as uptodate */ 1469 mark_target_uptodate(sh, sh->ops.target); 1470 mark_target_uptodate(sh, sh->ops.target2); 1471 1472 clear_bit(STRIPE_COMPUTE_RUN, &sh->state); 1473 if (sh->check_state == check_state_compute_run) 1474 sh->check_state = check_state_compute_result; 1475 set_bit(STRIPE_HANDLE, &sh->state); 1476 raid5_release_stripe(sh); 1477 } 1478 1479 /* return a pointer to the address conversion region of the scribble buffer */ 1480 static struct page **to_addr_page(struct raid5_percpu *percpu, int i) 1481 { 1482 return percpu->scribble + i * percpu->scribble_obj_size; 1483 } 1484 1485 /* return a pointer to the address conversion region of the scribble buffer */ 1486 static addr_conv_t *to_addr_conv(struct stripe_head *sh, 1487 struct raid5_percpu *percpu, int i) 1488 { 1489 return (void *) (to_addr_page(percpu, i) + sh->disks + 2); 1490 } 1491 1492 /* 1493 * Return a pointer to record offset address. 1494 */ 1495 static unsigned int * 1496 to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu) 1497 { 1498 return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2); 1499 } 1500 1501 static struct dma_async_tx_descriptor * 1502 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) 1503 { 1504 int disks = sh->disks; 1505 struct page **xor_srcs = to_addr_page(percpu, 0); 1506 unsigned int *off_srcs = to_addr_offs(sh, percpu); 1507 int target = sh->ops.target; 1508 struct r5dev *tgt = &sh->dev[target]; 1509 struct page *xor_dest = tgt->page; 1510 unsigned int off_dest = tgt->offset; 1511 int count = 0; 1512 struct dma_async_tx_descriptor *tx; 1513 struct async_submit_ctl submit; 1514 int i; 1515 1516 BUG_ON(sh->batch_head); 1517 1518 pr_debug("%s: stripe %llu block: %d\n", 1519 __func__, (unsigned long long)sh->sector, target); 1520 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 1521 1522 for (i = disks; i--; ) { 1523 if (i != target) { 1524 off_srcs[count] = sh->dev[i].offset; 1525 xor_srcs[count++] = sh->dev[i].page; 1526 } 1527 } 1528 1529 atomic_inc(&sh->count); 1530 1531 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, 1532 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); 1533 if (unlikely(count == 1)) 1534 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0], 1535 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 1536 else 1537 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, 1538 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 1539 1540 return tx; 1541 } 1542 1543 /* set_syndrome_sources - populate source buffers for gen_syndrome 1544 * @srcs - (struct page *) array of size sh->disks 1545 * @offs - (unsigned int) array of offset for each page 1546 * @sh - stripe_head to parse 1547 * 1548 * Populates srcs in proper layout order for the stripe and returns the 1549 * 'count' of sources to be used in a call to async_gen_syndrome. The P 1550 * destination buffer is recorded in srcs[count] and the Q destination 1551 * is recorded in srcs[count+1]]. 1552 */ 1553 static int set_syndrome_sources(struct page **srcs, 1554 unsigned int *offs, 1555 struct stripe_head *sh, 1556 int srctype) 1557 { 1558 int disks = sh->disks; 1559 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); 1560 int d0_idx = raid6_d0(sh); 1561 int count; 1562 int i; 1563 1564 for (i = 0; i < disks; i++) 1565 srcs[i] = NULL; 1566 1567 count = 0; 1568 i = d0_idx; 1569 do { 1570 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); 1571 struct r5dev *dev = &sh->dev[i]; 1572 1573 if (i == sh->qd_idx || i == sh->pd_idx || 1574 (srctype == SYNDROME_SRC_ALL) || 1575 (srctype == SYNDROME_SRC_WANT_DRAIN && 1576 (test_bit(R5_Wantdrain, &dev->flags) || 1577 test_bit(R5_InJournal, &dev->flags))) || 1578 (srctype == SYNDROME_SRC_WRITTEN && 1579 (dev->written || 1580 test_bit(R5_InJournal, &dev->flags)))) { 1581 if (test_bit(R5_InJournal, &dev->flags)) 1582 srcs[slot] = sh->dev[i].orig_page; 1583 else 1584 srcs[slot] = sh->dev[i].page; 1585 /* 1586 * For R5_InJournal, PAGE_SIZE must be 4KB and will 1587 * not shared page. In that case, dev[i].offset 1588 * is 0. 1589 */ 1590 offs[slot] = sh->dev[i].offset; 1591 } 1592 i = raid6_next_disk(i, disks); 1593 } while (i != d0_idx); 1594 1595 return syndrome_disks; 1596 } 1597 1598 static struct dma_async_tx_descriptor * 1599 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) 1600 { 1601 int disks = sh->disks; 1602 struct page **blocks = to_addr_page(percpu, 0); 1603 unsigned int *offs = to_addr_offs(sh, percpu); 1604 int target; 1605 int qd_idx = sh->qd_idx; 1606 struct dma_async_tx_descriptor *tx; 1607 struct async_submit_ctl submit; 1608 struct r5dev *tgt; 1609 struct page *dest; 1610 unsigned int dest_off; 1611 int i; 1612 int count; 1613 1614 BUG_ON(sh->batch_head); 1615 if (sh->ops.target < 0) 1616 target = sh->ops.target2; 1617 else if (sh->ops.target2 < 0) 1618 target = sh->ops.target; 1619 else 1620 /* we should only have one valid target */ 1621 BUG(); 1622 BUG_ON(target < 0); 1623 pr_debug("%s: stripe %llu block: %d\n", 1624 __func__, (unsigned long long)sh->sector, target); 1625 1626 tgt = &sh->dev[target]; 1627 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 1628 dest = tgt->page; 1629 dest_off = tgt->offset; 1630 1631 atomic_inc(&sh->count); 1632 1633 if (target == qd_idx) { 1634 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); 1635 blocks[count] = NULL; /* regenerating p is not necessary */ 1636 BUG_ON(blocks[count+1] != dest); /* q should already be set */ 1637 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, 1638 ops_complete_compute, sh, 1639 to_addr_conv(sh, percpu, 0)); 1640 tx = async_gen_syndrome(blocks, offs, count+2, 1641 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 1642 } else { 1643 /* Compute any data- or p-drive using XOR */ 1644 count = 0; 1645 for (i = disks; i-- ; ) { 1646 if (i == target || i == qd_idx) 1647 continue; 1648 offs[count] = sh->dev[i].offset; 1649 blocks[count++] = sh->dev[i].page; 1650 } 1651 1652 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, 1653 NULL, ops_complete_compute, sh, 1654 to_addr_conv(sh, percpu, 0)); 1655 tx = async_xor_offs(dest, dest_off, blocks, offs, count, 1656 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 1657 } 1658 1659 return tx; 1660 } 1661 1662 static struct dma_async_tx_descriptor * 1663 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) 1664 { 1665 int i, count, disks = sh->disks; 1666 int syndrome_disks = sh->ddf_layout ? disks : disks-2; 1667 int d0_idx = raid6_d0(sh); 1668 int faila = -1, failb = -1; 1669 int target = sh->ops.target; 1670 int target2 = sh->ops.target2; 1671 struct r5dev *tgt = &sh->dev[target]; 1672 struct r5dev *tgt2 = &sh->dev[target2]; 1673 struct dma_async_tx_descriptor *tx; 1674 struct page **blocks = to_addr_page(percpu, 0); 1675 unsigned int *offs = to_addr_offs(sh, percpu); 1676 struct async_submit_ctl submit; 1677 1678 BUG_ON(sh->batch_head); 1679 pr_debug("%s: stripe %llu block1: %d block2: %d\n", 1680 __func__, (unsigned long long)sh->sector, target, target2); 1681 BUG_ON(target < 0 || target2 < 0); 1682 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); 1683 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); 1684 1685 /* we need to open-code set_syndrome_sources to handle the 1686 * slot number conversion for 'faila' and 'failb' 1687 */ 1688 for (i = 0; i < disks ; i++) { 1689 offs[i] = 0; 1690 blocks[i] = NULL; 1691 } 1692 count = 0; 1693 i = d0_idx; 1694 do { 1695 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); 1696 1697 offs[slot] = sh->dev[i].offset; 1698 blocks[slot] = sh->dev[i].page; 1699 1700 if (i == target) 1701 faila = slot; 1702 if (i == target2) 1703 failb = slot; 1704 i = raid6_next_disk(i, disks); 1705 } while (i != d0_idx); 1706 1707 BUG_ON(faila == failb); 1708 if (failb < faila) 1709 swap(faila, failb); 1710 pr_debug("%s: stripe: %llu faila: %d failb: %d\n", 1711 __func__, (unsigned long long)sh->sector, faila, failb); 1712 1713 atomic_inc(&sh->count); 1714 1715 if (failb == syndrome_disks+1) { 1716 /* Q disk is one of the missing disks */ 1717 if (faila == syndrome_disks) { 1718 /* Missing P+Q, just recompute */ 1719 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, 1720 ops_complete_compute, sh, 1721 to_addr_conv(sh, percpu, 0)); 1722 return async_gen_syndrome(blocks, offs, syndrome_disks+2, 1723 RAID5_STRIPE_SIZE(sh->raid_conf), 1724 &submit); 1725 } else { 1726 struct page *dest; 1727 unsigned int dest_off; 1728 int data_target; 1729 int qd_idx = sh->qd_idx; 1730 1731 /* Missing D+Q: recompute D from P, then recompute Q */ 1732 if (target == qd_idx) 1733 data_target = target2; 1734 else 1735 data_target = target; 1736 1737 count = 0; 1738 for (i = disks; i-- ; ) { 1739 if (i == data_target || i == qd_idx) 1740 continue; 1741 offs[count] = sh->dev[i].offset; 1742 blocks[count++] = sh->dev[i].page; 1743 } 1744 dest = sh->dev[data_target].page; 1745 dest_off = sh->dev[data_target].offset; 1746 init_async_submit(&submit, 1747 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, 1748 NULL, NULL, NULL, 1749 to_addr_conv(sh, percpu, 0)); 1750 tx = async_xor_offs(dest, dest_off, blocks, offs, count, 1751 RAID5_STRIPE_SIZE(sh->raid_conf), 1752 &submit); 1753 1754 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); 1755 init_async_submit(&submit, ASYNC_TX_FENCE, tx, 1756 ops_complete_compute, sh, 1757 to_addr_conv(sh, percpu, 0)); 1758 return async_gen_syndrome(blocks, offs, count+2, 1759 RAID5_STRIPE_SIZE(sh->raid_conf), 1760 &submit); 1761 } 1762 } else { 1763 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, 1764 ops_complete_compute, sh, 1765 to_addr_conv(sh, percpu, 0)); 1766 if (failb == syndrome_disks) { 1767 /* We're missing D+P. */ 1768 return async_raid6_datap_recov(syndrome_disks+2, 1769 RAID5_STRIPE_SIZE(sh->raid_conf), 1770 faila, 1771 blocks, offs, &submit); 1772 } else { 1773 /* We're missing D+D. */ 1774 return async_raid6_2data_recov(syndrome_disks+2, 1775 RAID5_STRIPE_SIZE(sh->raid_conf), 1776 faila, failb, 1777 blocks, offs, &submit); 1778 } 1779 } 1780 } 1781 1782 static void ops_complete_prexor(void *stripe_head_ref) 1783 { 1784 struct stripe_head *sh = stripe_head_ref; 1785 1786 pr_debug("%s: stripe %llu\n", __func__, 1787 (unsigned long long)sh->sector); 1788 1789 if (r5c_is_writeback(sh->raid_conf->log)) 1790 /* 1791 * raid5-cache write back uses orig_page during prexor. 1792 * After prexor, it is time to free orig_page 1793 */ 1794 r5c_release_extra_page(sh); 1795 } 1796 1797 static struct dma_async_tx_descriptor * 1798 ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, 1799 struct dma_async_tx_descriptor *tx) 1800 { 1801 int disks = sh->disks; 1802 struct page **xor_srcs = to_addr_page(percpu, 0); 1803 unsigned int *off_srcs = to_addr_offs(sh, percpu); 1804 int count = 0, pd_idx = sh->pd_idx, i; 1805 struct async_submit_ctl submit; 1806 1807 /* existing parity data subtracted */ 1808 unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset; 1809 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; 1810 1811 BUG_ON(sh->batch_head); 1812 pr_debug("%s: stripe %llu\n", __func__, 1813 (unsigned long long)sh->sector); 1814 1815 for (i = disks; i--; ) { 1816 struct r5dev *dev = &sh->dev[i]; 1817 /* Only process blocks that are known to be uptodate */ 1818 if (test_bit(R5_InJournal, &dev->flags)) { 1819 /* 1820 * For this case, PAGE_SIZE must be equal to 4KB and 1821 * page offset is zero. 1822 */ 1823 off_srcs[count] = dev->offset; 1824 xor_srcs[count++] = dev->orig_page; 1825 } else if (test_bit(R5_Wantdrain, &dev->flags)) { 1826 off_srcs[count] = dev->offset; 1827 xor_srcs[count++] = dev->page; 1828 } 1829 } 1830 1831 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, 1832 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); 1833 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, 1834 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 1835 1836 return tx; 1837 } 1838 1839 static struct dma_async_tx_descriptor * 1840 ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, 1841 struct dma_async_tx_descriptor *tx) 1842 { 1843 struct page **blocks = to_addr_page(percpu, 0); 1844 unsigned int *offs = to_addr_offs(sh, percpu); 1845 int count; 1846 struct async_submit_ctl submit; 1847 1848 pr_debug("%s: stripe %llu\n", __func__, 1849 (unsigned long long)sh->sector); 1850 1851 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN); 1852 1853 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, 1854 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); 1855 tx = async_gen_syndrome(blocks, offs, count+2, 1856 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 1857 1858 return tx; 1859 } 1860 1861 static struct dma_async_tx_descriptor * 1862 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) 1863 { 1864 struct r5conf *conf = sh->raid_conf; 1865 int disks = sh->disks; 1866 int i; 1867 struct stripe_head *head_sh = sh; 1868 1869 pr_debug("%s: stripe %llu\n", __func__, 1870 (unsigned long long)sh->sector); 1871 1872 for (i = disks; i--; ) { 1873 struct r5dev *dev; 1874 struct bio *chosen; 1875 1876 sh = head_sh; 1877 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { 1878 struct bio *wbi; 1879 1880 again: 1881 dev = &sh->dev[i]; 1882 /* 1883 * clear R5_InJournal, so when rewriting a page in 1884 * journal, it is not skipped by r5l_log_stripe() 1885 */ 1886 clear_bit(R5_InJournal, &dev->flags); 1887 spin_lock_irq(&sh->stripe_lock); 1888 chosen = dev->towrite; 1889 dev->towrite = NULL; 1890 sh->overwrite_disks = 0; 1891 BUG_ON(dev->written); 1892 wbi = dev->written = chosen; 1893 spin_unlock_irq(&sh->stripe_lock); 1894 WARN_ON(dev->page != dev->orig_page); 1895 1896 while (wbi && wbi->bi_iter.bi_sector < 1897 dev->sector + RAID5_STRIPE_SECTORS(conf)) { 1898 if (wbi->bi_opf & REQ_FUA) 1899 set_bit(R5_WantFUA, &dev->flags); 1900 if (wbi->bi_opf & REQ_SYNC) 1901 set_bit(R5_SyncIO, &dev->flags); 1902 if (bio_op(wbi) == REQ_OP_DISCARD) 1903 set_bit(R5_Discard, &dev->flags); 1904 else { 1905 tx = async_copy_data(1, wbi, &dev->page, 1906 dev->offset, 1907 dev->sector, tx, sh, 1908 r5c_is_writeback(conf->log)); 1909 if (dev->page != dev->orig_page && 1910 !r5c_is_writeback(conf->log)) { 1911 set_bit(R5_SkipCopy, &dev->flags); 1912 clear_bit(R5_UPTODATE, &dev->flags); 1913 clear_bit(R5_OVERWRITE, &dev->flags); 1914 } 1915 } 1916 wbi = r5_next_bio(conf, wbi, dev->sector); 1917 } 1918 1919 if (head_sh->batch_head) { 1920 sh = list_first_entry(&sh->batch_list, 1921 struct stripe_head, 1922 batch_list); 1923 if (sh == head_sh) 1924 continue; 1925 goto again; 1926 } 1927 } 1928 } 1929 1930 return tx; 1931 } 1932 1933 static void ops_complete_reconstruct(void *stripe_head_ref) 1934 { 1935 struct stripe_head *sh = stripe_head_ref; 1936 int disks = sh->disks; 1937 int pd_idx = sh->pd_idx; 1938 int qd_idx = sh->qd_idx; 1939 int i; 1940 bool fua = false, sync = false, discard = false; 1941 1942 pr_debug("%s: stripe %llu\n", __func__, 1943 (unsigned long long)sh->sector); 1944 1945 for (i = disks; i--; ) { 1946 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); 1947 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); 1948 discard |= test_bit(R5_Discard, &sh->dev[i].flags); 1949 } 1950 1951 for (i = disks; i--; ) { 1952 struct r5dev *dev = &sh->dev[i]; 1953 1954 if (dev->written || i == pd_idx || i == qd_idx) { 1955 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) { 1956 set_bit(R5_UPTODATE, &dev->flags); 1957 if (test_bit(STRIPE_EXPAND_READY, &sh->state)) 1958 set_bit(R5_Expanded, &dev->flags); 1959 } 1960 if (fua) 1961 set_bit(R5_WantFUA, &dev->flags); 1962 if (sync) 1963 set_bit(R5_SyncIO, &dev->flags); 1964 } 1965 } 1966 1967 if (sh->reconstruct_state == reconstruct_state_drain_run) 1968 sh->reconstruct_state = reconstruct_state_drain_result; 1969 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) 1970 sh->reconstruct_state = reconstruct_state_prexor_drain_result; 1971 else { 1972 BUG_ON(sh->reconstruct_state != reconstruct_state_run); 1973 sh->reconstruct_state = reconstruct_state_result; 1974 } 1975 1976 set_bit(STRIPE_HANDLE, &sh->state); 1977 raid5_release_stripe(sh); 1978 } 1979 1980 static void 1981 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, 1982 struct dma_async_tx_descriptor *tx) 1983 { 1984 int disks = sh->disks; 1985 struct page **xor_srcs; 1986 unsigned int *off_srcs; 1987 struct async_submit_ctl submit; 1988 int count, pd_idx = sh->pd_idx, i; 1989 struct page *xor_dest; 1990 unsigned int off_dest; 1991 int prexor = 0; 1992 unsigned long flags; 1993 int j = 0; 1994 struct stripe_head *head_sh = sh; 1995 int last_stripe; 1996 1997 pr_debug("%s: stripe %llu\n", __func__, 1998 (unsigned long long)sh->sector); 1999 2000 for (i = 0; i < sh->disks; i++) { 2001 if (pd_idx == i) 2002 continue; 2003 if (!test_bit(R5_Discard, &sh->dev[i].flags)) 2004 break; 2005 } 2006 if (i >= sh->disks) { 2007 atomic_inc(&sh->count); 2008 set_bit(R5_Discard, &sh->dev[pd_idx].flags); 2009 ops_complete_reconstruct(sh); 2010 return; 2011 } 2012 again: 2013 count = 0; 2014 xor_srcs = to_addr_page(percpu, j); 2015 off_srcs = to_addr_offs(sh, percpu); 2016 /* check if prexor is active which means only process blocks 2017 * that are part of a read-modify-write (written) 2018 */ 2019 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { 2020 prexor = 1; 2021 off_dest = off_srcs[count] = sh->dev[pd_idx].offset; 2022 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; 2023 for (i = disks; i--; ) { 2024 struct r5dev *dev = &sh->dev[i]; 2025 if (head_sh->dev[i].written || 2026 test_bit(R5_InJournal, &head_sh->dev[i].flags)) { 2027 off_srcs[count] = dev->offset; 2028 xor_srcs[count++] = dev->page; 2029 } 2030 } 2031 } else { 2032 xor_dest = sh->dev[pd_idx].page; 2033 off_dest = sh->dev[pd_idx].offset; 2034 for (i = disks; i--; ) { 2035 struct r5dev *dev = &sh->dev[i]; 2036 if (i != pd_idx) { 2037 off_srcs[count] = dev->offset; 2038 xor_srcs[count++] = dev->page; 2039 } 2040 } 2041 } 2042 2043 /* 1/ if we prexor'd then the dest is reused as a source 2044 * 2/ if we did not prexor then we are redoing the parity 2045 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST 2046 * for the synchronous xor case 2047 */ 2048 last_stripe = !head_sh->batch_head || 2049 list_first_entry(&sh->batch_list, 2050 struct stripe_head, batch_list) == head_sh; 2051 if (last_stripe) { 2052 flags = ASYNC_TX_ACK | 2053 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); 2054 2055 atomic_inc(&head_sh->count); 2056 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, 2057 to_addr_conv(sh, percpu, j)); 2058 } else { 2059 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; 2060 init_async_submit(&submit, flags, tx, NULL, NULL, 2061 to_addr_conv(sh, percpu, j)); 2062 } 2063 2064 if (unlikely(count == 1)) 2065 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0], 2066 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 2067 else 2068 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, 2069 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 2070 if (!last_stripe) { 2071 j++; 2072 sh = list_first_entry(&sh->batch_list, struct stripe_head, 2073 batch_list); 2074 goto again; 2075 } 2076 } 2077 2078 static void 2079 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, 2080 struct dma_async_tx_descriptor *tx) 2081 { 2082 struct async_submit_ctl submit; 2083 struct page **blocks; 2084 unsigned int *offs; 2085 int count, i, j = 0; 2086 struct stripe_head *head_sh = sh; 2087 int last_stripe; 2088 int synflags; 2089 unsigned long txflags; 2090 2091 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); 2092 2093 for (i = 0; i < sh->disks; i++) { 2094 if (sh->pd_idx == i || sh->qd_idx == i) 2095 continue; 2096 if (!test_bit(R5_Discard, &sh->dev[i].flags)) 2097 break; 2098 } 2099 if (i >= sh->disks) { 2100 atomic_inc(&sh->count); 2101 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); 2102 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); 2103 ops_complete_reconstruct(sh); 2104 return; 2105 } 2106 2107 again: 2108 blocks = to_addr_page(percpu, j); 2109 offs = to_addr_offs(sh, percpu); 2110 2111 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { 2112 synflags = SYNDROME_SRC_WRITTEN; 2113 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; 2114 } else { 2115 synflags = SYNDROME_SRC_ALL; 2116 txflags = ASYNC_TX_ACK; 2117 } 2118 2119 count = set_syndrome_sources(blocks, offs, sh, synflags); 2120 last_stripe = !head_sh->batch_head || 2121 list_first_entry(&sh->batch_list, 2122 struct stripe_head, batch_list) == head_sh; 2123 2124 if (last_stripe) { 2125 atomic_inc(&head_sh->count); 2126 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, 2127 head_sh, to_addr_conv(sh, percpu, j)); 2128 } else 2129 init_async_submit(&submit, 0, tx, NULL, NULL, 2130 to_addr_conv(sh, percpu, j)); 2131 tx = async_gen_syndrome(blocks, offs, count+2, 2132 RAID5_STRIPE_SIZE(sh->raid_conf), &submit); 2133 if (!last_stripe) { 2134 j++; 2135 sh = list_first_entry(&sh->batch_list, struct stripe_head, 2136 batch_list); 2137 goto again; 2138 } 2139 } 2140 2141 static void ops_complete_check(void *stripe_head_ref) 2142 { 2143 struct stripe_head *sh = stripe_head_ref; 2144 2145 pr_debug("%s: stripe %llu\n", __func__, 2146 (unsigned long long)sh->sector); 2147 2148 sh->check_state = check_state_check_result; 2149 set_bit(STRIPE_HANDLE, &sh->state); 2150 raid5_release_stripe(sh); 2151 } 2152 2153 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) 2154 { 2155 int disks = sh->disks; 2156 int pd_idx = sh->pd_idx; 2157 int qd_idx = sh->qd_idx; 2158 struct page *xor_dest; 2159 unsigned int off_dest; 2160 struct page **xor_srcs = to_addr_page(percpu, 0); 2161 unsigned int *off_srcs = to_addr_offs(sh, percpu); 2162 struct dma_async_tx_descriptor *tx; 2163 struct async_submit_ctl submit; 2164 int count; 2165 int i; 2166 2167 pr_debug("%s: stripe %llu\n", __func__, 2168 (unsigned long long)sh->sector); 2169 2170 BUG_ON(sh->batch_head); 2171 count = 0; 2172 xor_dest = sh->dev[pd_idx].page; 2173 off_dest = sh->dev[pd_idx].offset; 2174 off_srcs[count] = off_dest; 2175 xor_srcs[count++] = xor_dest; 2176 for (i = disks; i--; ) { 2177 if (i == pd_idx || i == qd_idx) 2178 continue; 2179 off_srcs[count] = sh->dev[i].offset; 2180 xor_srcs[count++] = sh->dev[i].page; 2181 } 2182 2183 init_async_submit(&submit, 0, NULL, NULL, NULL, 2184 to_addr_conv(sh, percpu, 0)); 2185 tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, 2186 RAID5_STRIPE_SIZE(sh->raid_conf), 2187 &sh->ops.zero_sum_result, &submit); 2188 2189 atomic_inc(&sh->count); 2190 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); 2191 tx = async_trigger_callback(&submit); 2192 } 2193 2194 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) 2195 { 2196 struct page **srcs = to_addr_page(percpu, 0); 2197 unsigned int *offs = to_addr_offs(sh, percpu); 2198 struct async_submit_ctl submit; 2199 int count; 2200 2201 pr_debug("%s: stripe %llu checkp: %d\n", __func__, 2202 (unsigned long long)sh->sector, checkp); 2203 2204 BUG_ON(sh->batch_head); 2205 count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL); 2206 if (!checkp) 2207 srcs[count] = NULL; 2208 2209 atomic_inc(&sh->count); 2210 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, 2211 sh, to_addr_conv(sh, percpu, 0)); 2212 async_syndrome_val(srcs, offs, count+2, 2213 RAID5_STRIPE_SIZE(sh->raid_conf), 2214 &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit); 2215 } 2216 2217 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) 2218 { 2219 int overlap_clear = 0, i, disks = sh->disks; 2220 struct dma_async_tx_descriptor *tx = NULL; 2221 struct r5conf *conf = sh->raid_conf; 2222 int level = conf->level; 2223 struct raid5_percpu *percpu; 2224 2225 local_lock(&conf->percpu->lock); 2226 percpu = this_cpu_ptr(conf->percpu); 2227 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { 2228 ops_run_biofill(sh); 2229 overlap_clear++; 2230 } 2231 2232 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { 2233 if (level < 6) 2234 tx = ops_run_compute5(sh, percpu); 2235 else { 2236 if (sh->ops.target2 < 0 || sh->ops.target < 0) 2237 tx = ops_run_compute6_1(sh, percpu); 2238 else 2239 tx = ops_run_compute6_2(sh, percpu); 2240 } 2241 /* terminate the chain if reconstruct is not set to be run */ 2242 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) 2243 async_tx_ack(tx); 2244 } 2245 2246 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { 2247 if (level < 6) 2248 tx = ops_run_prexor5(sh, percpu, tx); 2249 else 2250 tx = ops_run_prexor6(sh, percpu, tx); 2251 } 2252 2253 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request)) 2254 tx = ops_run_partial_parity(sh, percpu, tx); 2255 2256 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { 2257 tx = ops_run_biodrain(sh, tx); 2258 overlap_clear++; 2259 } 2260 2261 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { 2262 if (level < 6) 2263 ops_run_reconstruct5(sh, percpu, tx); 2264 else 2265 ops_run_reconstruct6(sh, percpu, tx); 2266 } 2267 2268 if (test_bit(STRIPE_OP_CHECK, &ops_request)) { 2269 if (sh->check_state == check_state_run) 2270 ops_run_check_p(sh, percpu); 2271 else if (sh->check_state == check_state_run_q) 2272 ops_run_check_pq(sh, percpu, 0); 2273 else if (sh->check_state == check_state_run_pq) 2274 ops_run_check_pq(sh, percpu, 1); 2275 else 2276 BUG(); 2277 } 2278 2279 if (overlap_clear && !sh->batch_head) { 2280 for (i = disks; i--; ) { 2281 struct r5dev *dev = &sh->dev[i]; 2282 if (test_and_clear_bit(R5_Overlap, &dev->flags)) 2283 wake_up(&sh->raid_conf->wait_for_overlap); 2284 } 2285 } 2286 local_unlock(&conf->percpu->lock); 2287 } 2288 2289 static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh) 2290 { 2291 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 2292 kfree(sh->pages); 2293 #endif 2294 if (sh->ppl_page) 2295 __free_page(sh->ppl_page); 2296 kmem_cache_free(sc, sh); 2297 } 2298 2299 static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, 2300 int disks, struct r5conf *conf) 2301 { 2302 struct stripe_head *sh; 2303 2304 sh = kmem_cache_zalloc(sc, gfp); 2305 if (sh) { 2306 spin_lock_init(&sh->stripe_lock); 2307 spin_lock_init(&sh->batch_lock); 2308 INIT_LIST_HEAD(&sh->batch_list); 2309 INIT_LIST_HEAD(&sh->lru); 2310 INIT_LIST_HEAD(&sh->r5c); 2311 INIT_LIST_HEAD(&sh->log_list); 2312 atomic_set(&sh->count, 1); 2313 sh->raid_conf = conf; 2314 sh->log_start = MaxSector; 2315 2316 if (raid5_has_ppl(conf)) { 2317 sh->ppl_page = alloc_page(gfp); 2318 if (!sh->ppl_page) { 2319 free_stripe(sc, sh); 2320 return NULL; 2321 } 2322 } 2323 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 2324 if (init_stripe_shared_pages(sh, conf, disks)) { 2325 free_stripe(sc, sh); 2326 return NULL; 2327 } 2328 #endif 2329 } 2330 return sh; 2331 } 2332 static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) 2333 { 2334 struct stripe_head *sh; 2335 2336 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf); 2337 if (!sh) 2338 return 0; 2339 2340 if (grow_buffers(sh, gfp)) { 2341 shrink_buffers(sh); 2342 free_stripe(conf->slab_cache, sh); 2343 return 0; 2344 } 2345 sh->hash_lock_index = 2346 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; 2347 /* we just created an active stripe so... */ 2348 atomic_inc(&conf->active_stripes); 2349 2350 raid5_release_stripe(sh); 2351 conf->max_nr_stripes++; 2352 return 1; 2353 } 2354 2355 static int grow_stripes(struct r5conf *conf, int num) 2356 { 2357 struct kmem_cache *sc; 2358 size_t namelen = sizeof(conf->cache_name[0]); 2359 int devs = max(conf->raid_disks, conf->previous_raid_disks); 2360 2361 if (conf->mddev->gendisk) 2362 snprintf(conf->cache_name[0], namelen, 2363 "raid%d-%s", conf->level, mdname(conf->mddev)); 2364 else 2365 snprintf(conf->cache_name[0], namelen, 2366 "raid%d-%p", conf->level, conf->mddev); 2367 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]); 2368 2369 conf->active_name = 0; 2370 sc = kmem_cache_create(conf->cache_name[conf->active_name], 2371 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), 2372 0, 0, NULL); 2373 if (!sc) 2374 return 1; 2375 conf->slab_cache = sc; 2376 conf->pool_size = devs; 2377 while (num--) 2378 if (!grow_one_stripe(conf, GFP_KERNEL)) 2379 return 1; 2380 2381 return 0; 2382 } 2383 2384 /** 2385 * scribble_alloc - allocate percpu scribble buffer for required size 2386 * of the scribble region 2387 * @percpu: from for_each_present_cpu() of the caller 2388 * @num: total number of disks in the array 2389 * @cnt: scribble objs count for required size of the scribble region 2390 * 2391 * The scribble buffer size must be enough to contain: 2392 * 1/ a struct page pointer for each device in the array +2 2393 * 2/ room to convert each entry in (1) to its corresponding dma 2394 * (dma_map_page()) or page (page_address()) address. 2395 * 2396 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we 2397 * calculate over all devices (not just the data blocks), using zeros in place 2398 * of the P and Q blocks. 2399 */ 2400 static int scribble_alloc(struct raid5_percpu *percpu, 2401 int num, int cnt) 2402 { 2403 size_t obj_size = 2404 sizeof(struct page *) * (num + 2) + 2405 sizeof(addr_conv_t) * (num + 2) + 2406 sizeof(unsigned int) * (num + 2); 2407 void *scribble; 2408 2409 /* 2410 * If here is in raid array suspend context, it is in memalloc noio 2411 * context as well, there is no potential recursive memory reclaim 2412 * I/Os with the GFP_KERNEL flag. 2413 */ 2414 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL); 2415 if (!scribble) 2416 return -ENOMEM; 2417 2418 kvfree(percpu->scribble); 2419 2420 percpu->scribble = scribble; 2421 percpu->scribble_obj_size = obj_size; 2422 return 0; 2423 } 2424 2425 static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) 2426 { 2427 unsigned long cpu; 2428 int err = 0; 2429 2430 /* 2431 * Never shrink. And mddev_suspend() could deadlock if this is called 2432 * from raid5d. In that case, scribble_disks and scribble_sectors 2433 * should equal to new_disks and new_sectors 2434 */ 2435 if (conf->scribble_disks >= new_disks && 2436 conf->scribble_sectors >= new_sectors) 2437 return 0; 2438 mddev_suspend(conf->mddev); 2439 cpus_read_lock(); 2440 2441 for_each_present_cpu(cpu) { 2442 struct raid5_percpu *percpu; 2443 2444 percpu = per_cpu_ptr(conf->percpu, cpu); 2445 err = scribble_alloc(percpu, new_disks, 2446 new_sectors / RAID5_STRIPE_SECTORS(conf)); 2447 if (err) 2448 break; 2449 } 2450 2451 cpus_read_unlock(); 2452 mddev_resume(conf->mddev); 2453 if (!err) { 2454 conf->scribble_disks = new_disks; 2455 conf->scribble_sectors = new_sectors; 2456 } 2457 return err; 2458 } 2459 2460 static int resize_stripes(struct r5conf *conf, int newsize) 2461 { 2462 /* Make all the stripes able to hold 'newsize' devices. 2463 * New slots in each stripe get 'page' set to a new page. 2464 * 2465 * This happens in stages: 2466 * 1/ create a new kmem_cache and allocate the required number of 2467 * stripe_heads. 2468 * 2/ gather all the old stripe_heads and transfer the pages across 2469 * to the new stripe_heads. This will have the side effect of 2470 * freezing the array as once all stripe_heads have been collected, 2471 * no IO will be possible. Old stripe heads are freed once their 2472 * pages have been transferred over, and the old kmem_cache is 2473 * freed when all stripes are done. 2474 * 3/ reallocate conf->disks to be suitable bigger. If this fails, 2475 * we simple return a failure status - no need to clean anything up. 2476 * 4/ allocate new pages for the new slots in the new stripe_heads. 2477 * If this fails, we don't bother trying the shrink the 2478 * stripe_heads down again, we just leave them as they are. 2479 * As each stripe_head is processed the new one is released into 2480 * active service. 2481 * 2482 * Once step2 is started, we cannot afford to wait for a write, 2483 * so we use GFP_NOIO allocations. 2484 */ 2485 struct stripe_head *osh, *nsh; 2486 LIST_HEAD(newstripes); 2487 struct disk_info *ndisks; 2488 int err = 0; 2489 struct kmem_cache *sc; 2490 int i; 2491 int hash, cnt; 2492 2493 md_allow_write(conf->mddev); 2494 2495 /* Step 1 */ 2496 sc = kmem_cache_create(conf->cache_name[1-conf->active_name], 2497 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), 2498 0, 0, NULL); 2499 if (!sc) 2500 return -ENOMEM; 2501 2502 /* Need to ensure auto-resizing doesn't interfere */ 2503 mutex_lock(&conf->cache_size_mutex); 2504 2505 for (i = conf->max_nr_stripes; i; i--) { 2506 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf); 2507 if (!nsh) 2508 break; 2509 2510 list_add(&nsh->lru, &newstripes); 2511 } 2512 if (i) { 2513 /* didn't get enough, give up */ 2514 while (!list_empty(&newstripes)) { 2515 nsh = list_entry(newstripes.next, struct stripe_head, lru); 2516 list_del(&nsh->lru); 2517 free_stripe(sc, nsh); 2518 } 2519 kmem_cache_destroy(sc); 2520 mutex_unlock(&conf->cache_size_mutex); 2521 return -ENOMEM; 2522 } 2523 /* Step 2 - Must use GFP_NOIO now. 2524 * OK, we have enough stripes, start collecting inactive 2525 * stripes and copying them over 2526 */ 2527 hash = 0; 2528 cnt = 0; 2529 list_for_each_entry(nsh, &newstripes, lru) { 2530 lock_device_hash_lock(conf, hash); 2531 wait_event_cmd(conf->wait_for_stripe, 2532 !list_empty(conf->inactive_list + hash), 2533 unlock_device_hash_lock(conf, hash), 2534 lock_device_hash_lock(conf, hash)); 2535 osh = get_free_stripe(conf, hash); 2536 unlock_device_hash_lock(conf, hash); 2537 2538 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 2539 for (i = 0; i < osh->nr_pages; i++) { 2540 nsh->pages[i] = osh->pages[i]; 2541 osh->pages[i] = NULL; 2542 } 2543 #endif 2544 for(i=0; i<conf->pool_size; i++) { 2545 nsh->dev[i].page = osh->dev[i].page; 2546 nsh->dev[i].orig_page = osh->dev[i].page; 2547 nsh->dev[i].offset = osh->dev[i].offset; 2548 } 2549 nsh->hash_lock_index = hash; 2550 free_stripe(conf->slab_cache, osh); 2551 cnt++; 2552 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + 2553 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { 2554 hash++; 2555 cnt = 0; 2556 } 2557 } 2558 kmem_cache_destroy(conf->slab_cache); 2559 2560 /* Step 3. 2561 * At this point, we are holding all the stripes so the array 2562 * is completely stalled, so now is a good time to resize 2563 * conf->disks and the scribble region 2564 */ 2565 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO); 2566 if (ndisks) { 2567 for (i = 0; i < conf->pool_size; i++) 2568 ndisks[i] = conf->disks[i]; 2569 2570 for (i = conf->pool_size; i < newsize; i++) { 2571 ndisks[i].extra_page = alloc_page(GFP_NOIO); 2572 if (!ndisks[i].extra_page) 2573 err = -ENOMEM; 2574 } 2575 2576 if (err) { 2577 for (i = conf->pool_size; i < newsize; i++) 2578 if (ndisks[i].extra_page) 2579 put_page(ndisks[i].extra_page); 2580 kfree(ndisks); 2581 } else { 2582 kfree(conf->disks); 2583 conf->disks = ndisks; 2584 } 2585 } else 2586 err = -ENOMEM; 2587 2588 conf->slab_cache = sc; 2589 conf->active_name = 1-conf->active_name; 2590 2591 /* Step 4, return new stripes to service */ 2592 while(!list_empty(&newstripes)) { 2593 nsh = list_entry(newstripes.next, struct stripe_head, lru); 2594 list_del_init(&nsh->lru); 2595 2596 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 2597 for (i = 0; i < nsh->nr_pages; i++) { 2598 if (nsh->pages[i]) 2599 continue; 2600 nsh->pages[i] = alloc_page(GFP_NOIO); 2601 if (!nsh->pages[i]) 2602 err = -ENOMEM; 2603 } 2604 2605 for (i = conf->raid_disks; i < newsize; i++) { 2606 if (nsh->dev[i].page) 2607 continue; 2608 nsh->dev[i].page = raid5_get_dev_page(nsh, i); 2609 nsh->dev[i].orig_page = nsh->dev[i].page; 2610 nsh->dev[i].offset = raid5_get_page_offset(nsh, i); 2611 } 2612 #else 2613 for (i=conf->raid_disks; i < newsize; i++) 2614 if (nsh->dev[i].page == NULL) { 2615 struct page *p = alloc_page(GFP_NOIO); 2616 nsh->dev[i].page = p; 2617 nsh->dev[i].orig_page = p; 2618 nsh->dev[i].offset = 0; 2619 if (!p) 2620 err = -ENOMEM; 2621 } 2622 #endif 2623 raid5_release_stripe(nsh); 2624 } 2625 /* critical section pass, GFP_NOIO no longer needed */ 2626 2627 if (!err) 2628 conf->pool_size = newsize; 2629 mutex_unlock(&conf->cache_size_mutex); 2630 2631 return err; 2632 } 2633 2634 static int drop_one_stripe(struct r5conf *conf) 2635 { 2636 struct stripe_head *sh; 2637 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; 2638 2639 spin_lock_irq(conf->hash_locks + hash); 2640 sh = get_free_stripe(conf, hash); 2641 spin_unlock_irq(conf->hash_locks + hash); 2642 if (!sh) 2643 return 0; 2644 BUG_ON(atomic_read(&sh->count)); 2645 shrink_buffers(sh); 2646 free_stripe(conf->slab_cache, sh); 2647 atomic_dec(&conf->active_stripes); 2648 conf->max_nr_stripes--; 2649 return 1; 2650 } 2651 2652 static void shrink_stripes(struct r5conf *conf) 2653 { 2654 while (conf->max_nr_stripes && 2655 drop_one_stripe(conf)) 2656 ; 2657 2658 kmem_cache_destroy(conf->slab_cache); 2659 conf->slab_cache = NULL; 2660 } 2661 2662 /* 2663 * This helper wraps rcu_dereference_protected() and can be used when 2664 * it is known that the nr_pending of the rdev is elevated. 2665 */ 2666 static struct md_rdev *rdev_pend_deref(struct md_rdev __rcu *rdev) 2667 { 2668 return rcu_dereference_protected(rdev, 2669 atomic_read(&rcu_access_pointer(rdev)->nr_pending)); 2670 } 2671 2672 /* 2673 * This helper wraps rcu_dereference_protected() and should be used 2674 * when it is known that the mddev_lock() is held. This is safe 2675 * seeing raid5_remove_disk() has the same lock held. 2676 */ 2677 static struct md_rdev *rdev_mdlock_deref(struct mddev *mddev, 2678 struct md_rdev __rcu *rdev) 2679 { 2680 return rcu_dereference_protected(rdev, 2681 lockdep_is_held(&mddev->reconfig_mutex)); 2682 } 2683 2684 static void raid5_end_read_request(struct bio * bi) 2685 { 2686 struct stripe_head *sh = bi->bi_private; 2687 struct r5conf *conf = sh->raid_conf; 2688 int disks = sh->disks, i; 2689 char b[BDEVNAME_SIZE]; 2690 struct md_rdev *rdev = NULL; 2691 sector_t s; 2692 2693 for (i=0 ; i<disks; i++) 2694 if (bi == &sh->dev[i].req) 2695 break; 2696 2697 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", 2698 (unsigned long long)sh->sector, i, atomic_read(&sh->count), 2699 bi->bi_status); 2700 if (i == disks) { 2701 BUG(); 2702 return; 2703 } 2704 if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) 2705 /* If replacement finished while this request was outstanding, 2706 * 'replacement' might be NULL already. 2707 * In that case it moved down to 'rdev'. 2708 * rdev is not removed until all requests are finished. 2709 */ 2710 rdev = rdev_pend_deref(conf->disks[i].replacement); 2711 if (!rdev) 2712 rdev = rdev_pend_deref(conf->disks[i].rdev); 2713 2714 if (use_new_offset(conf, sh)) 2715 s = sh->sector + rdev->new_data_offset; 2716 else 2717 s = sh->sector + rdev->data_offset; 2718 if (!bi->bi_status) { 2719 set_bit(R5_UPTODATE, &sh->dev[i].flags); 2720 if (test_bit(R5_ReadError, &sh->dev[i].flags)) { 2721 /* Note that this cannot happen on a 2722 * replacement device. We just fail those on 2723 * any error 2724 */ 2725 pr_info_ratelimited( 2726 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n", 2727 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf), 2728 (unsigned long long)s, 2729 bdevname(rdev->bdev, b)); 2730 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors); 2731 clear_bit(R5_ReadError, &sh->dev[i].flags); 2732 clear_bit(R5_ReWrite, &sh->dev[i].flags); 2733 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) 2734 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); 2735 2736 if (test_bit(R5_InJournal, &sh->dev[i].flags)) 2737 /* 2738 * end read for a page in journal, this 2739 * must be preparing for prexor in rmw 2740 */ 2741 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); 2742 2743 if (atomic_read(&rdev->read_errors)) 2744 atomic_set(&rdev->read_errors, 0); 2745 } else { 2746 const char *bdn = bdevname(rdev->bdev, b); 2747 int retry = 0; 2748 int set_bad = 0; 2749 2750 clear_bit(R5_UPTODATE, &sh->dev[i].flags); 2751 if (!(bi->bi_status == BLK_STS_PROTECTION)) 2752 atomic_inc(&rdev->read_errors); 2753 if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) 2754 pr_warn_ratelimited( 2755 "md/raid:%s: read error on replacement device (sector %llu on %s).\n", 2756 mdname(conf->mddev), 2757 (unsigned long long)s, 2758 bdn); 2759 else if (conf->mddev->degraded >= conf->max_degraded) { 2760 set_bad = 1; 2761 pr_warn_ratelimited( 2762 "md/raid:%s: read error not correctable (sector %llu on %s).\n", 2763 mdname(conf->mddev), 2764 (unsigned long long)s, 2765 bdn); 2766 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { 2767 /* Oh, no!!! */ 2768 set_bad = 1; 2769 pr_warn_ratelimited( 2770 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", 2771 mdname(conf->mddev), 2772 (unsigned long long)s, 2773 bdn); 2774 } else if (atomic_read(&rdev->read_errors) 2775 > conf->max_nr_stripes) { 2776 if (!test_bit(Faulty, &rdev->flags)) { 2777 pr_warn("md/raid:%s: %d read_errors > %d stripes\n", 2778 mdname(conf->mddev), 2779 atomic_read(&rdev->read_errors), 2780 conf->max_nr_stripes); 2781 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", 2782 mdname(conf->mddev), bdn); 2783 } 2784 } else 2785 retry = 1; 2786 if (set_bad && test_bit(In_sync, &rdev->flags) 2787 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) 2788 retry = 1; 2789 if (retry) 2790 if (sh->qd_idx >= 0 && sh->pd_idx == i) 2791 set_bit(R5_ReadError, &sh->dev[i].flags); 2792 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { 2793 set_bit(R5_ReadError, &sh->dev[i].flags); 2794 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); 2795 } else 2796 set_bit(R5_ReadNoMerge, &sh->dev[i].flags); 2797 else { 2798 clear_bit(R5_ReadError, &sh->dev[i].flags); 2799 clear_bit(R5_ReWrite, &sh->dev[i].flags); 2800 if (!(set_bad 2801 && test_bit(In_sync, &rdev->flags) 2802 && rdev_set_badblocks( 2803 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0))) 2804 md_error(conf->mddev, rdev); 2805 } 2806 } 2807 rdev_dec_pending(rdev, conf->mddev); 2808 bio_uninit(bi); 2809 clear_bit(R5_LOCKED, &sh->dev[i].flags); 2810 set_bit(STRIPE_HANDLE, &sh->state); 2811 raid5_release_stripe(sh); 2812 } 2813 2814 static void raid5_end_write_request(struct bio *bi) 2815 { 2816 struct stripe_head *sh = bi->bi_private; 2817 struct r5conf *conf = sh->raid_conf; 2818 int disks = sh->disks, i; 2819 struct md_rdev *rdev; 2820 sector_t first_bad; 2821 int bad_sectors; 2822 int replacement = 0; 2823 2824 for (i = 0 ; i < disks; i++) { 2825 if (bi == &sh->dev[i].req) { 2826 rdev = rdev_pend_deref(conf->disks[i].rdev); 2827 break; 2828 } 2829 if (bi == &sh->dev[i].rreq) { 2830 rdev = rdev_pend_deref(conf->disks[i].replacement); 2831 if (rdev) 2832 replacement = 1; 2833 else 2834 /* rdev was removed and 'replacement' 2835 * replaced it. rdev is not removed 2836 * until all requests are finished. 2837 */ 2838 rdev = rdev_pend_deref(conf->disks[i].rdev); 2839 break; 2840 } 2841 } 2842 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", 2843 (unsigned long long)sh->sector, i, atomic_read(&sh->count), 2844 bi->bi_status); 2845 if (i == disks) { 2846 BUG(); 2847 return; 2848 } 2849 2850 if (replacement) { 2851 if (bi->bi_status) 2852 md_error(conf->mddev, rdev); 2853 else if (is_badblock(rdev, sh->sector, 2854 RAID5_STRIPE_SECTORS(conf), 2855 &first_bad, &bad_sectors)) 2856 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); 2857 } else { 2858 if (bi->bi_status) { 2859 set_bit(STRIPE_DEGRADED, &sh->state); 2860 set_bit(WriteErrorSeen, &rdev->flags); 2861 set_bit(R5_WriteError, &sh->dev[i].flags); 2862 if (!test_and_set_bit(WantReplacement, &rdev->flags)) 2863 set_bit(MD_RECOVERY_NEEDED, 2864 &rdev->mddev->recovery); 2865 } else if (is_badblock(rdev, sh->sector, 2866 RAID5_STRIPE_SECTORS(conf), 2867 &first_bad, &bad_sectors)) { 2868 set_bit(R5_MadeGood, &sh->dev[i].flags); 2869 if (test_bit(R5_ReadError, &sh->dev[i].flags)) 2870 /* That was a successful write so make 2871 * sure it looks like we already did 2872 * a re-write. 2873 */ 2874 set_bit(R5_ReWrite, &sh->dev[i].flags); 2875 } 2876 } 2877 rdev_dec_pending(rdev, conf->mddev); 2878 2879 if (sh->batch_head && bi->bi_status && !replacement) 2880 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); 2881 2882 bio_uninit(bi); 2883 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) 2884 clear_bit(R5_LOCKED, &sh->dev[i].flags); 2885 set_bit(STRIPE_HANDLE, &sh->state); 2886 raid5_release_stripe(sh); 2887 2888 if (sh->batch_head && sh != sh->batch_head) 2889 raid5_release_stripe(sh->batch_head); 2890 } 2891 2892 static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) 2893 { 2894 char b[BDEVNAME_SIZE]; 2895 struct r5conf *conf = mddev->private; 2896 unsigned long flags; 2897 pr_debug("raid456: error called\n"); 2898 2899 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n", 2900 mdname(mddev), bdevname(rdev->bdev, b)); 2901 2902 spin_lock_irqsave(&conf->device_lock, flags); 2903 set_bit(Faulty, &rdev->flags); 2904 clear_bit(In_sync, &rdev->flags); 2905 mddev->degraded = raid5_calc_degraded(conf); 2906 2907 if (has_failed(conf)) { 2908 set_bit(MD_BROKEN, &conf->mddev->flags); 2909 conf->recovery_disabled = mddev->recovery_disabled; 2910 2911 pr_crit("md/raid:%s: Cannot continue operation (%d/%d failed).\n", 2912 mdname(mddev), mddev->degraded, conf->raid_disks); 2913 } else { 2914 pr_crit("md/raid:%s: Operation continuing on %d devices.\n", 2915 mdname(mddev), conf->raid_disks - mddev->degraded); 2916 } 2917 2918 spin_unlock_irqrestore(&conf->device_lock, flags); 2919 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 2920 2921 set_bit(Blocked, &rdev->flags); 2922 set_mask_bits(&mddev->sb_flags, 0, 2923 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); 2924 r5c_update_on_rdev_error(mddev, rdev); 2925 } 2926 2927 /* 2928 * Input: a 'big' sector number, 2929 * Output: index of the data and parity disk, and the sector # in them. 2930 */ 2931 sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, 2932 int previous, int *dd_idx, 2933 struct stripe_head *sh) 2934 { 2935 sector_t stripe, stripe2; 2936 sector_t chunk_number; 2937 unsigned int chunk_offset; 2938 int pd_idx, qd_idx; 2939 int ddf_layout = 0; 2940 sector_t new_sector; 2941 int algorithm = previous ? conf->prev_algo 2942 : conf->algorithm; 2943 int sectors_per_chunk = previous ? conf->prev_chunk_sectors 2944 : conf->chunk_sectors; 2945 int raid_disks = previous ? conf->previous_raid_disks 2946 : conf->raid_disks; 2947 int data_disks = raid_disks - conf->max_degraded; 2948 2949 /* First compute the information on this sector */ 2950 2951 /* 2952 * Compute the chunk number and the sector offset inside the chunk 2953 */ 2954 chunk_offset = sector_div(r_sector, sectors_per_chunk); 2955 chunk_number = r_sector; 2956 2957 /* 2958 * Compute the stripe number 2959 */ 2960 stripe = chunk_number; 2961 *dd_idx = sector_div(stripe, data_disks); 2962 stripe2 = stripe; 2963 /* 2964 * Select the parity disk based on the user selected algorithm. 2965 */ 2966 pd_idx = qd_idx = -1; 2967 switch(conf->level) { 2968 case 4: 2969 pd_idx = data_disks; 2970 break; 2971 case 5: 2972 switch (algorithm) { 2973 case ALGORITHM_LEFT_ASYMMETRIC: 2974 pd_idx = data_disks - sector_div(stripe2, raid_disks); 2975 if (*dd_idx >= pd_idx) 2976 (*dd_idx)++; 2977 break; 2978 case ALGORITHM_RIGHT_ASYMMETRIC: 2979 pd_idx = sector_div(stripe2, raid_disks); 2980 if (*dd_idx >= pd_idx) 2981 (*dd_idx)++; 2982 break; 2983 case ALGORITHM_LEFT_SYMMETRIC: 2984 pd_idx = data_disks - sector_div(stripe2, raid_disks); 2985 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; 2986 break; 2987 case ALGORITHM_RIGHT_SYMMETRIC: 2988 pd_idx = sector_div(stripe2, raid_disks); 2989 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; 2990 break; 2991 case ALGORITHM_PARITY_0: 2992 pd_idx = 0; 2993 (*dd_idx)++; 2994 break; 2995 case ALGORITHM_PARITY_N: 2996 pd_idx = data_disks; 2997 break; 2998 default: 2999 BUG(); 3000 } 3001 break; 3002 case 6: 3003 3004 switch (algorithm) { 3005 case ALGORITHM_LEFT_ASYMMETRIC: 3006 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 3007 qd_idx = pd_idx + 1; 3008 if (pd_idx == raid_disks-1) { 3009 (*dd_idx)++; /* Q D D D P */ 3010 qd_idx = 0; 3011 } else if (*dd_idx >= pd_idx) 3012 (*dd_idx) += 2; /* D D P Q D */ 3013 break; 3014 case ALGORITHM_RIGHT_ASYMMETRIC: 3015 pd_idx = sector_div(stripe2, raid_disks); 3016 qd_idx = pd_idx + 1; 3017 if (pd_idx == raid_disks-1) { 3018 (*dd_idx)++; /* Q D D D P */ 3019 qd_idx = 0; 3020 } else if (*dd_idx >= pd_idx) 3021 (*dd_idx) += 2; /* D D P Q D */ 3022 break; 3023 case ALGORITHM_LEFT_SYMMETRIC: 3024 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 3025 qd_idx = (pd_idx + 1) % raid_disks; 3026 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; 3027 break; 3028 case ALGORITHM_RIGHT_SYMMETRIC: 3029 pd_idx = sector_div(stripe2, raid_disks); 3030 qd_idx = (pd_idx + 1) % raid_disks; 3031 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; 3032 break; 3033 3034 case ALGORITHM_PARITY_0: 3035 pd_idx = 0; 3036 qd_idx = 1; 3037 (*dd_idx) += 2; 3038 break; 3039 case ALGORITHM_PARITY_N: 3040 pd_idx = data_disks; 3041 qd_idx = data_disks + 1; 3042 break; 3043 3044 case ALGORITHM_ROTATING_ZERO_RESTART: 3045 /* Exactly the same as RIGHT_ASYMMETRIC, but or 3046 * of blocks for computing Q is different. 3047 */ 3048 pd_idx = sector_div(stripe2, raid_disks); 3049 qd_idx = pd_idx + 1; 3050 if (pd_idx == raid_disks-1) { 3051 (*dd_idx)++; /* Q D D D P */ 3052 qd_idx = 0; 3053 } else if (*dd_idx >= pd_idx) 3054 (*dd_idx) += 2; /* D D P Q D */ 3055 ddf_layout = 1; 3056 break; 3057 3058 case ALGORITHM_ROTATING_N_RESTART: 3059 /* Same a left_asymmetric, by first stripe is 3060 * D D D P Q rather than 3061 * Q D D D P 3062 */ 3063 stripe2 += 1; 3064 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 3065 qd_idx = pd_idx + 1; 3066 if (pd_idx == raid_disks-1) { 3067 (*dd_idx)++; /* Q D D D P */ 3068 qd_idx = 0; 3069 } else if (*dd_idx >= pd_idx) 3070 (*dd_idx) += 2; /* D D P Q D */ 3071 ddf_layout = 1; 3072 break; 3073 3074 case ALGORITHM_ROTATING_N_CONTINUE: 3075 /* Same as left_symmetric but Q is before P */ 3076 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); 3077 qd_idx = (pd_idx + raid_disks - 1) % raid_disks; 3078 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; 3079 ddf_layout = 1; 3080 break; 3081 3082 case ALGORITHM_LEFT_ASYMMETRIC_6: 3083 /* RAID5 left_asymmetric, with Q on last device */ 3084 pd_idx = data_disks - sector_div(stripe2, raid_disks-1); 3085 if (*dd_idx >= pd_idx) 3086 (*dd_idx)++; 3087 qd_idx = raid_disks - 1; 3088 break; 3089 3090 case ALGORITHM_RIGHT_ASYMMETRIC_6: 3091 pd_idx = sector_div(stripe2, raid_disks-1); 3092 if (*dd_idx >= pd_idx) 3093 (*dd_idx)++; 3094 qd_idx = raid_disks - 1; 3095 break; 3096 3097 case ALGORITHM_LEFT_SYMMETRIC_6: 3098 pd_idx = data_disks - sector_div(stripe2, raid_disks-1); 3099 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); 3100 qd_idx = raid_disks - 1; 3101 break; 3102 3103 case ALGORITHM_RIGHT_SYMMETRIC_6: 3104 pd_idx = sector_div(stripe2, raid_disks-1); 3105 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); 3106 qd_idx = raid_disks - 1; 3107 break; 3108 3109 case ALGORITHM_PARITY_0_6: 3110 pd_idx = 0; 3111 (*dd_idx)++; 3112 qd_idx = raid_disks - 1; 3113 break; 3114 3115 default: 3116 BUG(); 3117 } 3118 break; 3119 } 3120 3121 if (sh) { 3122 sh->pd_idx = pd_idx; 3123 sh->qd_idx = qd_idx; 3124 sh->ddf_layout = ddf_layout; 3125 } 3126 /* 3127 * Finally, compute the new sector number 3128 */ 3129 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; 3130 return new_sector; 3131 } 3132 3133 sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous) 3134 { 3135 struct r5conf *conf = sh->raid_conf; 3136 int raid_disks = sh->disks; 3137 int data_disks = raid_disks - conf->max_degraded; 3138 sector_t new_sector = sh->sector, check; 3139 int sectors_per_chunk = previous ? conf->prev_chunk_sectors 3140 : conf->chunk_sectors; 3141 int algorithm = previous ? conf->prev_algo 3142 : conf->algorithm; 3143 sector_t stripe; 3144 int chunk_offset; 3145 sector_t chunk_number; 3146 int dummy1, dd_idx = i; 3147 sector_t r_sector; 3148 struct stripe_head sh2; 3149 3150 chunk_offset = sector_div(new_sector, sectors_per_chunk); 3151 stripe = new_sector; 3152 3153 if (i == sh->pd_idx) 3154 return 0; 3155 switch(conf->level) { 3156 case 4: break; 3157 case 5: 3158 switch (algorithm) { 3159 case ALGORITHM_LEFT_ASYMMETRIC: 3160 case ALGORITHM_RIGHT_ASYMMETRIC: 3161 if (i > sh->pd_idx) 3162 i--; 3163 break; 3164 case ALGORITHM_LEFT_SYMMETRIC: 3165 case ALGORITHM_RIGHT_SYMMETRIC: 3166 if (i < sh->pd_idx) 3167 i += raid_disks; 3168 i -= (sh->pd_idx + 1); 3169 break; 3170 case ALGORITHM_PARITY_0: 3171 i -= 1; 3172 break; 3173 case ALGORITHM_PARITY_N: 3174 break; 3175 default: 3176 BUG(); 3177 } 3178 break; 3179 case 6: 3180 if (i == sh->qd_idx) 3181 return 0; /* It is the Q disk */ 3182 switch (algorithm) { 3183 case ALGORITHM_LEFT_ASYMMETRIC: 3184 case ALGORITHM_RIGHT_ASYMMETRIC: 3185 case ALGORITHM_ROTATING_ZERO_RESTART: 3186 case ALGORITHM_ROTATING_N_RESTART: 3187 if (sh->pd_idx == raid_disks-1) 3188 i--; /* Q D D D P */ 3189 else if (i > sh->pd_idx) 3190 i -= 2; /* D D P Q D */ 3191 break; 3192 case ALGORITHM_LEFT_SYMMETRIC: 3193 case ALGORITHM_RIGHT_SYMMETRIC: 3194 if (sh->pd_idx == raid_disks-1) 3195 i--; /* Q D D D P */ 3196 else { 3197 /* D D P Q D */ 3198 if (i < sh->pd_idx) 3199 i += raid_disks; 3200 i -= (sh->pd_idx + 2); 3201 } 3202 break; 3203 case ALGORITHM_PARITY_0: 3204 i -= 2; 3205 break; 3206 case ALGORITHM_PARITY_N: 3207 break; 3208 case ALGORITHM_ROTATING_N_CONTINUE: 3209 /* Like left_symmetric, but P is before Q */ 3210 if (sh->pd_idx == 0) 3211 i--; /* P D D D Q */ 3212 else { 3213 /* D D Q P D */ 3214 if (i < sh->pd_idx) 3215 i += raid_disks; 3216 i -= (sh->pd_idx + 1); 3217 } 3218 break; 3219 case ALGORITHM_LEFT_ASYMMETRIC_6: 3220 case ALGORITHM_RIGHT_ASYMMETRIC_6: 3221 if (i > sh->pd_idx) 3222 i--; 3223 break; 3224 case ALGORITHM_LEFT_SYMMETRIC_6: 3225 case ALGORITHM_RIGHT_SYMMETRIC_6: 3226 if (i < sh->pd_idx) 3227 i += data_disks + 1; 3228 i -= (sh->pd_idx + 1); 3229 break; 3230 case ALGORITHM_PARITY_0_6: 3231 i -= 1; 3232 break; 3233 default: 3234 BUG(); 3235 } 3236 break; 3237 } 3238 3239 chunk_number = stripe * data_disks + i; 3240 r_sector = chunk_number * sectors_per_chunk + chunk_offset; 3241 3242 check = raid5_compute_sector(conf, r_sector, 3243 previous, &dummy1, &sh2); 3244 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx 3245 || sh2.qd_idx != sh->qd_idx) { 3246 pr_warn("md/raid:%s: compute_blocknr: map not correct\n", 3247 mdname(conf->mddev)); 3248 return 0; 3249 } 3250 return r_sector; 3251 } 3252 3253 /* 3254 * There are cases where we want handle_stripe_dirtying() and 3255 * schedule_reconstruction() to delay towrite to some dev of a stripe. 3256 * 3257 * This function checks whether we want to delay the towrite. Specifically, 3258 * we delay the towrite when: 3259 * 3260 * 1. degraded stripe has a non-overwrite to the missing dev, AND this 3261 * stripe has data in journal (for other devices). 3262 * 3263 * In this case, when reading data for the non-overwrite dev, it is 3264 * necessary to handle complex rmw of write back cache (prexor with 3265 * orig_page, and xor with page). To keep read path simple, we would 3266 * like to flush data in journal to RAID disks first, so complex rmw 3267 * is handled in the write patch (handle_stripe_dirtying). 3268 * 3269 * 2. when journal space is critical (R5C_LOG_CRITICAL=1) 3270 * 3271 * It is important to be able to flush all stripes in raid5-cache. 3272 * Therefore, we need reserve some space on the journal device for 3273 * these flushes. If flush operation includes pending writes to the 3274 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe 3275 * for the flush out. If we exclude these pending writes from flush 3276 * operation, we only need (conf->max_degraded + 1) pages per stripe. 3277 * Therefore, excluding pending writes in these cases enables more 3278 * efficient use of the journal device. 3279 * 3280 * Note: To make sure the stripe makes progress, we only delay 3281 * towrite for stripes with data already in journal (injournal > 0). 3282 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to 3283 * no_space_stripes list. 3284 * 3285 * 3. during journal failure 3286 * In journal failure, we try to flush all cached data to raid disks 3287 * based on data in stripe cache. The array is read-only to upper 3288 * layers, so we would skip all pending writes. 3289 * 3290 */ 3291 static inline bool delay_towrite(struct r5conf *conf, 3292 struct r5dev *dev, 3293 struct stripe_head_state *s) 3294 { 3295 /* case 1 above */ 3296 if (!test_bit(R5_OVERWRITE, &dev->flags) && 3297 !test_bit(R5_Insync, &dev->flags) && s->injournal) 3298 return true; 3299 /* case 2 above */ 3300 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && 3301 s->injournal > 0) 3302 return true; 3303 /* case 3 above */ 3304 if (s->log_failed && s->injournal) 3305 return true; 3306 return false; 3307 } 3308 3309 static void 3310 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, 3311 int rcw, int expand) 3312 { 3313 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks; 3314 struct r5conf *conf = sh->raid_conf; 3315 int level = conf->level; 3316 3317 if (rcw) { 3318 /* 3319 * In some cases, handle_stripe_dirtying initially decided to 3320 * run rmw and allocates extra page for prexor. However, rcw is 3321 * cheaper later on. We need to free the extra page now, 3322 * because we won't be able to do that in ops_complete_prexor(). 3323 */ 3324 r5c_release_extra_page(sh); 3325 3326 for (i = disks; i--; ) { 3327 struct r5dev *dev = &sh->dev[i]; 3328 3329 if (dev->towrite && !delay_towrite(conf, dev, s)) { 3330 set_bit(R5_LOCKED, &dev->flags); 3331 set_bit(R5_Wantdrain, &dev->flags); 3332 if (!expand) 3333 clear_bit(R5_UPTODATE, &dev->flags); 3334 s->locked++; 3335 } else if (test_bit(R5_InJournal, &dev->flags)) { 3336 set_bit(R5_LOCKED, &dev->flags); 3337 s->locked++; 3338 } 3339 } 3340 /* if we are not expanding this is a proper write request, and 3341 * there will be bios with new data to be drained into the 3342 * stripe cache 3343 */ 3344 if (!expand) { 3345 if (!s->locked) 3346 /* False alarm, nothing to do */ 3347 return; 3348 sh->reconstruct_state = reconstruct_state_drain_run; 3349 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); 3350 } else 3351 sh->reconstruct_state = reconstruct_state_run; 3352 3353 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); 3354 3355 if (s->locked + conf->max_degraded == disks) 3356 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) 3357 atomic_inc(&conf->pending_full_writes); 3358 } else { 3359 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || 3360 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); 3361 BUG_ON(level == 6 && 3362 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || 3363 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); 3364 3365 for (i = disks; i--; ) { 3366 struct r5dev *dev = &sh->dev[i]; 3367 if (i == pd_idx || i == qd_idx) 3368 continue; 3369 3370 if (dev->towrite && 3371 (test_bit(R5_UPTODATE, &dev->flags) || 3372 test_bit(R5_Wantcompute, &dev->flags))) { 3373 set_bit(R5_Wantdrain, &dev->flags); 3374 set_bit(R5_LOCKED, &dev->flags); 3375 clear_bit(R5_UPTODATE, &dev->flags); 3376 s->locked++; 3377 } else if (test_bit(R5_InJournal, &dev->flags)) { 3378 set_bit(R5_LOCKED, &dev->flags); 3379 s->locked++; 3380 } 3381 } 3382 if (!s->locked) 3383 /* False alarm - nothing to do */ 3384 return; 3385 sh->reconstruct_state = reconstruct_state_prexor_drain_run; 3386 set_bit(STRIPE_OP_PREXOR, &s->ops_request); 3387 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); 3388 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); 3389 } 3390 3391 /* keep the parity disk(s) locked while asynchronous operations 3392 * are in flight 3393 */ 3394 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); 3395 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); 3396 s->locked++; 3397 3398 if (level == 6) { 3399 int qd_idx = sh->qd_idx; 3400 struct r5dev *dev = &sh->dev[qd_idx]; 3401 3402 set_bit(R5_LOCKED, &dev->flags); 3403 clear_bit(R5_UPTODATE, &dev->flags); 3404 s->locked++; 3405 } 3406 3407 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page && 3408 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) && 3409 !test_bit(STRIPE_FULL_WRITE, &sh->state) && 3410 test_bit(R5_Insync, &sh->dev[pd_idx].flags)) 3411 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request); 3412 3413 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", 3414 __func__, (unsigned long long)sh->sector, 3415 s->locked, s->ops_request); 3416 } 3417 3418 /* 3419 * Each stripe/dev can have one or more bion attached. 3420 * toread/towrite point to the first in a chain. 3421 * The bi_next chain must be in order. 3422 */ 3423 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, 3424 int forwrite, int previous) 3425 { 3426 struct bio **bip; 3427 struct r5conf *conf = sh->raid_conf; 3428 int firstwrite=0; 3429 3430 pr_debug("adding bi b#%llu to stripe s#%llu\n", 3431 (unsigned long long)bi->bi_iter.bi_sector, 3432 (unsigned long long)sh->sector); 3433 3434 spin_lock_irq(&sh->stripe_lock); 3435 /* Don't allow new IO added to stripes in batch list */ 3436 if (sh->batch_head) 3437 goto overlap; 3438 if (forwrite) { 3439 bip = &sh->dev[dd_idx].towrite; 3440 if (*bip == NULL) 3441 firstwrite = 1; 3442 } else 3443 bip = &sh->dev[dd_idx].toread; 3444 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { 3445 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) 3446 goto overlap; 3447 bip = & (*bip)->bi_next; 3448 } 3449 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) 3450 goto overlap; 3451 3452 if (forwrite && raid5_has_ppl(conf)) { 3453 /* 3454 * With PPL only writes to consecutive data chunks within a 3455 * stripe are allowed because for a single stripe_head we can 3456 * only have one PPL entry at a time, which describes one data 3457 * range. Not really an overlap, but wait_for_overlap can be 3458 * used to handle this. 3459 */ 3460 sector_t sector; 3461 sector_t first = 0; 3462 sector_t last = 0; 3463 int count = 0; 3464 int i; 3465 3466 for (i = 0; i < sh->disks; i++) { 3467 if (i != sh->pd_idx && 3468 (i == dd_idx || sh->dev[i].towrite)) { 3469 sector = sh->dev[i].sector; 3470 if (count == 0 || sector < first) 3471 first = sector; 3472 if (sector > last) 3473 last = sector; 3474 count++; 3475 } 3476 } 3477 3478 if (first + conf->chunk_sectors * (count - 1) != last) 3479 goto overlap; 3480 } 3481 3482 if (!forwrite || previous) 3483 clear_bit(STRIPE_BATCH_READY, &sh->state); 3484 3485 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); 3486 if (*bip) 3487 bi->bi_next = *bip; 3488 *bip = bi; 3489 bio_inc_remaining(bi); 3490 md_write_inc(conf->mddev, bi); 3491 3492 if (forwrite) { 3493 /* check if page is covered */ 3494 sector_t sector = sh->dev[dd_idx].sector; 3495 for (bi=sh->dev[dd_idx].towrite; 3496 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) && 3497 bi && bi->bi_iter.bi_sector <= sector; 3498 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) { 3499 if (bio_end_sector(bi) >= sector) 3500 sector = bio_end_sector(bi); 3501 } 3502 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf)) 3503 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) 3504 sh->overwrite_disks++; 3505 } 3506 3507 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", 3508 (unsigned long long)(*bip)->bi_iter.bi_sector, 3509 (unsigned long long)sh->sector, dd_idx); 3510 3511 if (conf->mddev->bitmap && firstwrite) { 3512 /* Cannot hold spinlock over bitmap_startwrite, 3513 * but must ensure this isn't added to a batch until 3514 * we have added to the bitmap and set bm_seq. 3515 * So set STRIPE_BITMAP_PENDING to prevent 3516 * batching. 3517 * If multiple add_stripe_bio() calls race here they 3518 * much all set STRIPE_BITMAP_PENDING. So only the first one 3519 * to complete "bitmap_startwrite" gets to set 3520 * STRIPE_BIT_DELAY. This is important as once a stripe 3521 * is added to a batch, STRIPE_BIT_DELAY cannot be changed 3522 * any more. 3523 */ 3524 set_bit(STRIPE_BITMAP_PENDING, &sh->state); 3525 spin_unlock_irq(&sh->stripe_lock); 3526 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector, 3527 RAID5_STRIPE_SECTORS(conf), 0); 3528 spin_lock_irq(&sh->stripe_lock); 3529 clear_bit(STRIPE_BITMAP_PENDING, &sh->state); 3530 if (!sh->batch_head) { 3531 sh->bm_seq = conf->seq_flush+1; 3532 set_bit(STRIPE_BIT_DELAY, &sh->state); 3533 } 3534 } 3535 spin_unlock_irq(&sh->stripe_lock); 3536 3537 if (stripe_can_batch(sh)) 3538 stripe_add_to_batch_list(conf, sh); 3539 return 1; 3540 3541 overlap: 3542 set_bit(R5_Overlap, &sh->dev[dd_idx].flags); 3543 spin_unlock_irq(&sh->stripe_lock); 3544 return 0; 3545 } 3546 3547 static void end_reshape(struct r5conf *conf); 3548 3549 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, 3550 struct stripe_head *sh) 3551 { 3552 int sectors_per_chunk = 3553 previous ? conf->prev_chunk_sectors : conf->chunk_sectors; 3554 int dd_idx; 3555 int chunk_offset = sector_div(stripe, sectors_per_chunk); 3556 int disks = previous ? conf->previous_raid_disks : conf->raid_disks; 3557 3558 raid5_compute_sector(conf, 3559 stripe * (disks - conf->max_degraded) 3560 *sectors_per_chunk + chunk_offset, 3561 previous, 3562 &dd_idx, sh); 3563 } 3564 3565 static void 3566 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, 3567 struct stripe_head_state *s, int disks) 3568 { 3569 int i; 3570 BUG_ON(sh->batch_head); 3571 for (i = disks; i--; ) { 3572 struct bio *bi; 3573 int bitmap_end = 0; 3574 3575 if (test_bit(R5_ReadError, &sh->dev[i].flags)) { 3576 struct md_rdev *rdev; 3577 rcu_read_lock(); 3578 rdev = rcu_dereference(conf->disks[i].rdev); 3579 if (rdev && test_bit(In_sync, &rdev->flags) && 3580 !test_bit(Faulty, &rdev->flags)) 3581 atomic_inc(&rdev->nr_pending); 3582 else 3583 rdev = NULL; 3584 rcu_read_unlock(); 3585 if (rdev) { 3586 if (!rdev_set_badblocks( 3587 rdev, 3588 sh->sector, 3589 RAID5_STRIPE_SECTORS(conf), 0)) 3590 md_error(conf->mddev, rdev); 3591 rdev_dec_pending(rdev, conf->mddev); 3592 } 3593 } 3594 spin_lock_irq(&sh->stripe_lock); 3595 /* fail all writes first */ 3596 bi = sh->dev[i].towrite; 3597 sh->dev[i].towrite = NULL; 3598 sh->overwrite_disks = 0; 3599 spin_unlock_irq(&sh->stripe_lock); 3600 if (bi) 3601 bitmap_end = 1; 3602 3603 log_stripe_write_finished(sh); 3604 3605 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 3606 wake_up(&conf->wait_for_overlap); 3607 3608 while (bi && bi->bi_iter.bi_sector < 3609 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { 3610 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector); 3611 3612 md_write_end(conf->mddev); 3613 bio_io_error(bi); 3614 bi = nextbi; 3615 } 3616 if (bitmap_end) 3617 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, 3618 RAID5_STRIPE_SECTORS(conf), 0, 0); 3619 bitmap_end = 0; 3620 /* and fail all 'written' */ 3621 bi = sh->dev[i].written; 3622 sh->dev[i].written = NULL; 3623 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { 3624 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); 3625 sh->dev[i].page = sh->dev[i].orig_page; 3626 } 3627 3628 if (bi) bitmap_end = 1; 3629 while (bi && bi->bi_iter.bi_sector < 3630 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { 3631 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector); 3632 3633 md_write_end(conf->mddev); 3634 bio_io_error(bi); 3635 bi = bi2; 3636 } 3637 3638 /* fail any reads if this device is non-operational and 3639 * the data has not reached the cache yet. 3640 */ 3641 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && 3642 s->failed > conf->max_degraded && 3643 (!test_bit(R5_Insync, &sh->dev[i].flags) || 3644 test_bit(R5_ReadError, &sh->dev[i].flags))) { 3645 spin_lock_irq(&sh->stripe_lock); 3646 bi = sh->dev[i].toread; 3647 sh->dev[i].toread = NULL; 3648 spin_unlock_irq(&sh->stripe_lock); 3649 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 3650 wake_up(&conf->wait_for_overlap); 3651 if (bi) 3652 s->to_read--; 3653 while (bi && bi->bi_iter.bi_sector < 3654 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { 3655 struct bio *nextbi = 3656 r5_next_bio(conf, bi, sh->dev[i].sector); 3657 3658 bio_io_error(bi); 3659 bi = nextbi; 3660 } 3661 } 3662 if (bitmap_end) 3663 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, 3664 RAID5_STRIPE_SECTORS(conf), 0, 0); 3665 /* If we were in the middle of a write the parity block might 3666 * still be locked - so just clear all R5_LOCKED flags 3667 */ 3668 clear_bit(R5_LOCKED, &sh->dev[i].flags); 3669 } 3670 s->to_write = 0; 3671 s->written = 0; 3672 3673 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) 3674 if (atomic_dec_and_test(&conf->pending_full_writes)) 3675 md_wakeup_thread(conf->mddev->thread); 3676 } 3677 3678 static void 3679 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, 3680 struct stripe_head_state *s) 3681 { 3682 int abort = 0; 3683 int i; 3684 3685 BUG_ON(sh->batch_head); 3686 clear_bit(STRIPE_SYNCING, &sh->state); 3687 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) 3688 wake_up(&conf->wait_for_overlap); 3689 s->syncing = 0; 3690 s->replacing = 0; 3691 /* There is nothing more to do for sync/check/repair. 3692 * Don't even need to abort as that is handled elsewhere 3693 * if needed, and not always wanted e.g. if there is a known 3694 * bad block here. 3695 * For recover/replace we need to record a bad block on all 3696 * non-sync devices, or abort the recovery 3697 */ 3698 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { 3699 /* During recovery devices cannot be removed, so 3700 * locking and refcounting of rdevs is not needed 3701 */ 3702 rcu_read_lock(); 3703 for (i = 0; i < conf->raid_disks; i++) { 3704 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 3705 if (rdev 3706 && !test_bit(Faulty, &rdev->flags) 3707 && !test_bit(In_sync, &rdev->flags) 3708 && !rdev_set_badblocks(rdev, sh->sector, 3709 RAID5_STRIPE_SECTORS(conf), 0)) 3710 abort = 1; 3711 rdev = rcu_dereference(conf->disks[i].replacement); 3712 if (rdev 3713 && !test_bit(Faulty, &rdev->flags) 3714 && !test_bit(In_sync, &rdev->flags) 3715 && !rdev_set_badblocks(rdev, sh->sector, 3716 RAID5_STRIPE_SECTORS(conf), 0)) 3717 abort = 1; 3718 } 3719 rcu_read_unlock(); 3720 if (abort) 3721 conf->recovery_disabled = 3722 conf->mddev->recovery_disabled; 3723 } 3724 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort); 3725 } 3726 3727 static int want_replace(struct stripe_head *sh, int disk_idx) 3728 { 3729 struct md_rdev *rdev; 3730 int rv = 0; 3731 3732 rcu_read_lock(); 3733 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); 3734 if (rdev 3735 && !test_bit(Faulty, &rdev->flags) 3736 && !test_bit(In_sync, &rdev->flags) 3737 && (rdev->recovery_offset <= sh->sector 3738 || rdev->mddev->recovery_cp <= sh->sector)) 3739 rv = 1; 3740 rcu_read_unlock(); 3741 return rv; 3742 } 3743 3744 static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, 3745 int disk_idx, int disks) 3746 { 3747 struct r5dev *dev = &sh->dev[disk_idx]; 3748 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], 3749 &sh->dev[s->failed_num[1]] }; 3750 int i; 3751 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW); 3752 3753 3754 if (test_bit(R5_LOCKED, &dev->flags) || 3755 test_bit(R5_UPTODATE, &dev->flags)) 3756 /* No point reading this as we already have it or have 3757 * decided to get it. 3758 */ 3759 return 0; 3760 3761 if (dev->toread || 3762 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) 3763 /* We need this block to directly satisfy a request */ 3764 return 1; 3765 3766 if (s->syncing || s->expanding || 3767 (s->replacing && want_replace(sh, disk_idx))) 3768 /* When syncing, or expanding we read everything. 3769 * When replacing, we need the replaced block. 3770 */ 3771 return 1; 3772 3773 if ((s->failed >= 1 && fdev[0]->toread) || 3774 (s->failed >= 2 && fdev[1]->toread)) 3775 /* If we want to read from a failed device, then 3776 * we need to actually read every other device. 3777 */ 3778 return 1; 3779 3780 /* Sometimes neither read-modify-write nor reconstruct-write 3781 * cycles can work. In those cases we read every block we 3782 * can. Then the parity-update is certain to have enough to 3783 * work with. 3784 * This can only be a problem when we need to write something, 3785 * and some device has failed. If either of those tests 3786 * fail we need look no further. 3787 */ 3788 if (!s->failed || !s->to_write) 3789 return 0; 3790 3791 if (test_bit(R5_Insync, &dev->flags) && 3792 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 3793 /* Pre-reads at not permitted until after short delay 3794 * to gather multiple requests. However if this 3795 * device is no Insync, the block could only be computed 3796 * and there is no need to delay that. 3797 */ 3798 return 0; 3799 3800 for (i = 0; i < s->failed && i < 2; i++) { 3801 if (fdev[i]->towrite && 3802 !test_bit(R5_UPTODATE, &fdev[i]->flags) && 3803 !test_bit(R5_OVERWRITE, &fdev[i]->flags)) 3804 /* If we have a partial write to a failed 3805 * device, then we will need to reconstruct 3806 * the content of that device, so all other 3807 * devices must be read. 3808 */ 3809 return 1; 3810 3811 if (s->failed >= 2 && 3812 (fdev[i]->towrite || 3813 s->failed_num[i] == sh->pd_idx || 3814 s->failed_num[i] == sh->qd_idx) && 3815 !test_bit(R5_UPTODATE, &fdev[i]->flags)) 3816 /* In max degraded raid6, If the failed disk is P, Q, 3817 * or we want to read the failed disk, we need to do 3818 * reconstruct-write. 3819 */ 3820 force_rcw = true; 3821 } 3822 3823 /* If we are forced to do a reconstruct-write, because parity 3824 * cannot be trusted and we are currently recovering it, there 3825 * is extra need to be careful. 3826 * If one of the devices that we would need to read, because 3827 * it is not being overwritten (and maybe not written at all) 3828 * is missing/faulty, then we need to read everything we can. 3829 */ 3830 if (!force_rcw && 3831 sh->sector < sh->raid_conf->mddev->recovery_cp) 3832 /* reconstruct-write isn't being forced */ 3833 return 0; 3834 for (i = 0; i < s->failed && i < 2; i++) { 3835 if (s->failed_num[i] != sh->pd_idx && 3836 s->failed_num[i] != sh->qd_idx && 3837 !test_bit(R5_UPTODATE, &fdev[i]->flags) && 3838 !test_bit(R5_OVERWRITE, &fdev[i]->flags)) 3839 return 1; 3840 } 3841 3842 return 0; 3843 } 3844 3845 /* fetch_block - checks the given member device to see if its data needs 3846 * to be read or computed to satisfy a request. 3847 * 3848 * Returns 1 when no more member devices need to be checked, otherwise returns 3849 * 0 to tell the loop in handle_stripe_fill to continue 3850 */ 3851 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, 3852 int disk_idx, int disks) 3853 { 3854 struct r5dev *dev = &sh->dev[disk_idx]; 3855 3856 /* is the data in this block needed, and can we get it? */ 3857 if (need_this_block(sh, s, disk_idx, disks)) { 3858 /* we would like to get this block, possibly by computing it, 3859 * otherwise read it if the backing disk is insync 3860 */ 3861 BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); 3862 BUG_ON(test_bit(R5_Wantread, &dev->flags)); 3863 BUG_ON(sh->batch_head); 3864 3865 /* 3866 * In the raid6 case if the only non-uptodate disk is P 3867 * then we already trusted P to compute the other failed 3868 * drives. It is safe to compute rather than re-read P. 3869 * In other cases we only compute blocks from failed 3870 * devices, otherwise check/repair might fail to detect 3871 * a real inconsistency. 3872 */ 3873 3874 if ((s->uptodate == disks - 1) && 3875 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) || 3876 (s->failed && (disk_idx == s->failed_num[0] || 3877 disk_idx == s->failed_num[1])))) { 3878 /* have disk failed, and we're requested to fetch it; 3879 * do compute it 3880 */ 3881 pr_debug("Computing stripe %llu block %d\n", 3882 (unsigned long long)sh->sector, disk_idx); 3883 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 3884 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 3885 set_bit(R5_Wantcompute, &dev->flags); 3886 sh->ops.target = disk_idx; 3887 sh->ops.target2 = -1; /* no 2nd target */ 3888 s->req_compute = 1; 3889 /* Careful: from this point on 'uptodate' is in the eye 3890 * of raid_run_ops which services 'compute' operations 3891 * before writes. R5_Wantcompute flags a block that will 3892 * be R5_UPTODATE by the time it is needed for a 3893 * subsequent operation. 3894 */ 3895 s->uptodate++; 3896 return 1; 3897 } else if (s->uptodate == disks-2 && s->failed >= 2) { 3898 /* Computing 2-failure is *very* expensive; only 3899 * do it if failed >= 2 3900 */ 3901 int other; 3902 for (other = disks; other--; ) { 3903 if (other == disk_idx) 3904 continue; 3905 if (!test_bit(R5_UPTODATE, 3906 &sh->dev[other].flags)) 3907 break; 3908 } 3909 BUG_ON(other < 0); 3910 pr_debug("Computing stripe %llu blocks %d,%d\n", 3911 (unsigned long long)sh->sector, 3912 disk_idx, other); 3913 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 3914 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 3915 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); 3916 set_bit(R5_Wantcompute, &sh->dev[other].flags); 3917 sh->ops.target = disk_idx; 3918 sh->ops.target2 = other; 3919 s->uptodate += 2; 3920 s->req_compute = 1; 3921 return 1; 3922 } else if (test_bit(R5_Insync, &dev->flags)) { 3923 set_bit(R5_LOCKED, &dev->flags); 3924 set_bit(R5_Wantread, &dev->flags); 3925 s->locked++; 3926 pr_debug("Reading block %d (sync=%d)\n", 3927 disk_idx, s->syncing); 3928 } 3929 } 3930 3931 return 0; 3932 } 3933 3934 /* 3935 * handle_stripe_fill - read or compute data to satisfy pending requests. 3936 */ 3937 static void handle_stripe_fill(struct stripe_head *sh, 3938 struct stripe_head_state *s, 3939 int disks) 3940 { 3941 int i; 3942 3943 /* look for blocks to read/compute, skip this if a compute 3944 * is already in flight, or if the stripe contents are in the 3945 * midst of changing due to a write 3946 */ 3947 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && 3948 !sh->reconstruct_state) { 3949 3950 /* 3951 * For degraded stripe with data in journal, do not handle 3952 * read requests yet, instead, flush the stripe to raid 3953 * disks first, this avoids handling complex rmw of write 3954 * back cache (prexor with orig_page, and then xor with 3955 * page) in the read path 3956 */ 3957 if (s->injournal && s->failed) { 3958 if (test_bit(STRIPE_R5C_CACHING, &sh->state)) 3959 r5c_make_stripe_write_out(sh); 3960 goto out; 3961 } 3962 3963 for (i = disks; i--; ) 3964 if (fetch_block(sh, s, i, disks)) 3965 break; 3966 } 3967 out: 3968 set_bit(STRIPE_HANDLE, &sh->state); 3969 } 3970 3971 static void break_stripe_batch_list(struct stripe_head *head_sh, 3972 unsigned long handle_flags); 3973 /* handle_stripe_clean_event 3974 * any written block on an uptodate or failed drive can be returned. 3975 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but 3976 * never LOCKED, so we don't need to test 'failed' directly. 3977 */ 3978 static void handle_stripe_clean_event(struct r5conf *conf, 3979 struct stripe_head *sh, int disks) 3980 { 3981 int i; 3982 struct r5dev *dev; 3983 int discard_pending = 0; 3984 struct stripe_head *head_sh = sh; 3985 bool do_endio = false; 3986 3987 for (i = disks; i--; ) 3988 if (sh->dev[i].written) { 3989 dev = &sh->dev[i]; 3990 if (!test_bit(R5_LOCKED, &dev->flags) && 3991 (test_bit(R5_UPTODATE, &dev->flags) || 3992 test_bit(R5_Discard, &dev->flags) || 3993 test_bit(R5_SkipCopy, &dev->flags))) { 3994 /* We can return any write requests */ 3995 struct bio *wbi, *wbi2; 3996 pr_debug("Return write for disc %d\n", i); 3997 if (test_and_clear_bit(R5_Discard, &dev->flags)) 3998 clear_bit(R5_UPTODATE, &dev->flags); 3999 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { 4000 WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); 4001 } 4002 do_endio = true; 4003 4004 returnbi: 4005 dev->page = dev->orig_page; 4006 wbi = dev->written; 4007 dev->written = NULL; 4008 while (wbi && wbi->bi_iter.bi_sector < 4009 dev->sector + RAID5_STRIPE_SECTORS(conf)) { 4010 wbi2 = r5_next_bio(conf, wbi, dev->sector); 4011 md_write_end(conf->mddev); 4012 bio_endio(wbi); 4013 wbi = wbi2; 4014 } 4015 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, 4016 RAID5_STRIPE_SECTORS(conf), 4017 !test_bit(STRIPE_DEGRADED, &sh->state), 4018 0); 4019 if (head_sh->batch_head) { 4020 sh = list_first_entry(&sh->batch_list, 4021 struct stripe_head, 4022 batch_list); 4023 if (sh != head_sh) { 4024 dev = &sh->dev[i]; 4025 goto returnbi; 4026 } 4027 } 4028 sh = head_sh; 4029 dev = &sh->dev[i]; 4030 } else if (test_bit(R5_Discard, &dev->flags)) 4031 discard_pending = 1; 4032 } 4033 4034 log_stripe_write_finished(sh); 4035 4036 if (!discard_pending && 4037 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { 4038 int hash; 4039 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); 4040 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); 4041 if (sh->qd_idx >= 0) { 4042 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); 4043 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); 4044 } 4045 /* now that discard is done we can proceed with any sync */ 4046 clear_bit(STRIPE_DISCARD, &sh->state); 4047 /* 4048 * SCSI discard will change some bio fields and the stripe has 4049 * no updated data, so remove it from hash list and the stripe 4050 * will be reinitialized 4051 */ 4052 unhash: 4053 hash = sh->hash_lock_index; 4054 spin_lock_irq(conf->hash_locks + hash); 4055 remove_hash(sh); 4056 spin_unlock_irq(conf->hash_locks + hash); 4057 if (head_sh->batch_head) { 4058 sh = list_first_entry(&sh->batch_list, 4059 struct stripe_head, batch_list); 4060 if (sh != head_sh) 4061 goto unhash; 4062 } 4063 sh = head_sh; 4064 4065 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) 4066 set_bit(STRIPE_HANDLE, &sh->state); 4067 4068 } 4069 4070 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) 4071 if (atomic_dec_and_test(&conf->pending_full_writes)) 4072 md_wakeup_thread(conf->mddev->thread); 4073 4074 if (head_sh->batch_head && do_endio) 4075 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); 4076 } 4077 4078 /* 4079 * For RMW in write back cache, we need extra page in prexor to store the 4080 * old data. This page is stored in dev->orig_page. 4081 * 4082 * This function checks whether we have data for prexor. The exact logic 4083 * is: 4084 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) 4085 */ 4086 static inline bool uptodate_for_rmw(struct r5dev *dev) 4087 { 4088 return (test_bit(R5_UPTODATE, &dev->flags)) && 4089 (!test_bit(R5_InJournal, &dev->flags) || 4090 test_bit(R5_OrigPageUPTDODATE, &dev->flags)); 4091 } 4092 4093 static int handle_stripe_dirtying(struct r5conf *conf, 4094 struct stripe_head *sh, 4095 struct stripe_head_state *s, 4096 int disks) 4097 { 4098 int rmw = 0, rcw = 0, i; 4099 sector_t recovery_cp = conf->mddev->recovery_cp; 4100 4101 /* Check whether resync is now happening or should start. 4102 * If yes, then the array is dirty (after unclean shutdown or 4103 * initial creation), so parity in some stripes might be inconsistent. 4104 * In this case, we need to always do reconstruct-write, to ensure 4105 * that in case of drive failure or read-error correction, we 4106 * generate correct data from the parity. 4107 */ 4108 if (conf->rmw_level == PARITY_DISABLE_RMW || 4109 (recovery_cp < MaxSector && sh->sector >= recovery_cp && 4110 s->failed == 0)) { 4111 /* Calculate the real rcw later - for now make it 4112 * look like rcw is cheaper 4113 */ 4114 rcw = 1; rmw = 2; 4115 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", 4116 conf->rmw_level, (unsigned long long)recovery_cp, 4117 (unsigned long long)sh->sector); 4118 } else for (i = disks; i--; ) { 4119 /* would I have to read this buffer for read_modify_write */ 4120 struct r5dev *dev = &sh->dev[i]; 4121 if (((dev->towrite && !delay_towrite(conf, dev, s)) || 4122 i == sh->pd_idx || i == sh->qd_idx || 4123 test_bit(R5_InJournal, &dev->flags)) && 4124 !test_bit(R5_LOCKED, &dev->flags) && 4125 !(uptodate_for_rmw(dev) || 4126 test_bit(R5_Wantcompute, &dev->flags))) { 4127 if (test_bit(R5_Insync, &dev->flags)) 4128 rmw++; 4129 else 4130 rmw += 2*disks; /* cannot read it */ 4131 } 4132 /* Would I have to read this buffer for reconstruct_write */ 4133 if (!test_bit(R5_OVERWRITE, &dev->flags) && 4134 i != sh->pd_idx && i != sh->qd_idx && 4135 !test_bit(R5_LOCKED, &dev->flags) && 4136 !(test_bit(R5_UPTODATE, &dev->flags) || 4137 test_bit(R5_Wantcompute, &dev->flags))) { 4138 if (test_bit(R5_Insync, &dev->flags)) 4139 rcw++; 4140 else 4141 rcw += 2*disks; 4142 } 4143 } 4144 4145 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", 4146 (unsigned long long)sh->sector, sh->state, rmw, rcw); 4147 set_bit(STRIPE_HANDLE, &sh->state); 4148 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) { 4149 /* prefer read-modify-write, but need to get some data */ 4150 if (conf->mddev->queue) 4151 blk_add_trace_msg(conf->mddev->queue, 4152 "raid5 rmw %llu %d", 4153 (unsigned long long)sh->sector, rmw); 4154 for (i = disks; i--; ) { 4155 struct r5dev *dev = &sh->dev[i]; 4156 if (test_bit(R5_InJournal, &dev->flags) && 4157 dev->page == dev->orig_page && 4158 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { 4159 /* alloc page for prexor */ 4160 struct page *p = alloc_page(GFP_NOIO); 4161 4162 if (p) { 4163 dev->orig_page = p; 4164 continue; 4165 } 4166 4167 /* 4168 * alloc_page() failed, try use 4169 * disk_info->extra_page 4170 */ 4171 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, 4172 &conf->cache_state)) { 4173 r5c_use_extra_page(sh); 4174 break; 4175 } 4176 4177 /* extra_page in use, add to delayed_list */ 4178 set_bit(STRIPE_DELAYED, &sh->state); 4179 s->waiting_extra_page = 1; 4180 return -EAGAIN; 4181 } 4182 } 4183 4184 for (i = disks; i--; ) { 4185 struct r5dev *dev = &sh->dev[i]; 4186 if (((dev->towrite && !delay_towrite(conf, dev, s)) || 4187 i == sh->pd_idx || i == sh->qd_idx || 4188 test_bit(R5_InJournal, &dev->flags)) && 4189 !test_bit(R5_LOCKED, &dev->flags) && 4190 !(uptodate_for_rmw(dev) || 4191 test_bit(R5_Wantcompute, &dev->flags)) && 4192 test_bit(R5_Insync, &dev->flags)) { 4193 if (test_bit(STRIPE_PREREAD_ACTIVE, 4194 &sh->state)) { 4195 pr_debug("Read_old block %d for r-m-w\n", 4196 i); 4197 set_bit(R5_LOCKED, &dev->flags); 4198 set_bit(R5_Wantread, &dev->flags); 4199 s->locked++; 4200 } else 4201 set_bit(STRIPE_DELAYED, &sh->state); 4202 } 4203 } 4204 } 4205 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) { 4206 /* want reconstruct write, but need to get some data */ 4207 int qread =0; 4208 rcw = 0; 4209 for (i = disks; i--; ) { 4210 struct r5dev *dev = &sh->dev[i]; 4211 if (!test_bit(R5_OVERWRITE, &dev->flags) && 4212 i != sh->pd_idx && i != sh->qd_idx && 4213 !test_bit(R5_LOCKED, &dev->flags) && 4214 !(test_bit(R5_UPTODATE, &dev->flags) || 4215 test_bit(R5_Wantcompute, &dev->flags))) { 4216 rcw++; 4217 if (test_bit(R5_Insync, &dev->flags) && 4218 test_bit(STRIPE_PREREAD_ACTIVE, 4219 &sh->state)) { 4220 pr_debug("Read_old block " 4221 "%d for Reconstruct\n", i); 4222 set_bit(R5_LOCKED, &dev->flags); 4223 set_bit(R5_Wantread, &dev->flags); 4224 s->locked++; 4225 qread++; 4226 } else 4227 set_bit(STRIPE_DELAYED, &sh->state); 4228 } 4229 } 4230 if (rcw && conf->mddev->queue) 4231 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", 4232 (unsigned long long)sh->sector, 4233 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); 4234 } 4235 4236 if (rcw > disks && rmw > disks && 4237 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 4238 set_bit(STRIPE_DELAYED, &sh->state); 4239 4240 /* now if nothing is locked, and if we have enough data, 4241 * we can start a write request 4242 */ 4243 /* since handle_stripe can be called at any time we need to handle the 4244 * case where a compute block operation has been submitted and then a 4245 * subsequent call wants to start a write request. raid_run_ops only 4246 * handles the case where compute block and reconstruct are requested 4247 * simultaneously. If this is not the case then new writes need to be 4248 * held off until the compute completes. 4249 */ 4250 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && 4251 (s->locked == 0 && (rcw == 0 || rmw == 0) && 4252 !test_bit(STRIPE_BIT_DELAY, &sh->state))) 4253 schedule_reconstruction(sh, s, rcw == 0, 0); 4254 return 0; 4255 } 4256 4257 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, 4258 struct stripe_head_state *s, int disks) 4259 { 4260 struct r5dev *dev = NULL; 4261 4262 BUG_ON(sh->batch_head); 4263 set_bit(STRIPE_HANDLE, &sh->state); 4264 4265 switch (sh->check_state) { 4266 case check_state_idle: 4267 /* start a new check operation if there are no failures */ 4268 if (s->failed == 0) { 4269 BUG_ON(s->uptodate != disks); 4270 sh->check_state = check_state_run; 4271 set_bit(STRIPE_OP_CHECK, &s->ops_request); 4272 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); 4273 s->uptodate--; 4274 break; 4275 } 4276 dev = &sh->dev[s->failed_num[0]]; 4277 fallthrough; 4278 case check_state_compute_result: 4279 sh->check_state = check_state_idle; 4280 if (!dev) 4281 dev = &sh->dev[sh->pd_idx]; 4282 4283 /* check that a write has not made the stripe insync */ 4284 if (test_bit(STRIPE_INSYNC, &sh->state)) 4285 break; 4286 4287 /* either failed parity check, or recovery is happening */ 4288 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); 4289 BUG_ON(s->uptodate != disks); 4290 4291 set_bit(R5_LOCKED, &dev->flags); 4292 s->locked++; 4293 set_bit(R5_Wantwrite, &dev->flags); 4294 4295 clear_bit(STRIPE_DEGRADED, &sh->state); 4296 set_bit(STRIPE_INSYNC, &sh->state); 4297 break; 4298 case check_state_run: 4299 break; /* we will be called again upon completion */ 4300 case check_state_check_result: 4301 sh->check_state = check_state_idle; 4302 4303 /* if a failure occurred during the check operation, leave 4304 * STRIPE_INSYNC not set and let the stripe be handled again 4305 */ 4306 if (s->failed) 4307 break; 4308 4309 /* handle a successful check operation, if parity is correct 4310 * we are done. Otherwise update the mismatch count and repair 4311 * parity if !MD_RECOVERY_CHECK 4312 */ 4313 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) 4314 /* parity is correct (on disc, 4315 * not in buffer any more) 4316 */ 4317 set_bit(STRIPE_INSYNC, &sh->state); 4318 else { 4319 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); 4320 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { 4321 /* don't try to repair!! */ 4322 set_bit(STRIPE_INSYNC, &sh->state); 4323 pr_warn_ratelimited("%s: mismatch sector in range " 4324 "%llu-%llu\n", mdname(conf->mddev), 4325 (unsigned long long) sh->sector, 4326 (unsigned long long) sh->sector + 4327 RAID5_STRIPE_SECTORS(conf)); 4328 } else { 4329 sh->check_state = check_state_compute_run; 4330 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 4331 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 4332 set_bit(R5_Wantcompute, 4333 &sh->dev[sh->pd_idx].flags); 4334 sh->ops.target = sh->pd_idx; 4335 sh->ops.target2 = -1; 4336 s->uptodate++; 4337 } 4338 } 4339 break; 4340 case check_state_compute_run: 4341 break; 4342 default: 4343 pr_err("%s: unknown check_state: %d sector: %llu\n", 4344 __func__, sh->check_state, 4345 (unsigned long long) sh->sector); 4346 BUG(); 4347 } 4348 } 4349 4350 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, 4351 struct stripe_head_state *s, 4352 int disks) 4353 { 4354 int pd_idx = sh->pd_idx; 4355 int qd_idx = sh->qd_idx; 4356 struct r5dev *dev; 4357 4358 BUG_ON(sh->batch_head); 4359 set_bit(STRIPE_HANDLE, &sh->state); 4360 4361 BUG_ON(s->failed > 2); 4362 4363 /* Want to check and possibly repair P and Q. 4364 * However there could be one 'failed' device, in which 4365 * case we can only check one of them, possibly using the 4366 * other to generate missing data 4367 */ 4368 4369 switch (sh->check_state) { 4370 case check_state_idle: 4371 /* start a new check operation if there are < 2 failures */ 4372 if (s->failed == s->q_failed) { 4373 /* The only possible failed device holds Q, so it 4374 * makes sense to check P (If anything else were failed, 4375 * we would have used P to recreate it). 4376 */ 4377 sh->check_state = check_state_run; 4378 } 4379 if (!s->q_failed && s->failed < 2) { 4380 /* Q is not failed, and we didn't use it to generate 4381 * anything, so it makes sense to check it 4382 */ 4383 if (sh->check_state == check_state_run) 4384 sh->check_state = check_state_run_pq; 4385 else 4386 sh->check_state = check_state_run_q; 4387 } 4388 4389 /* discard potentially stale zero_sum_result */ 4390 sh->ops.zero_sum_result = 0; 4391 4392 if (sh->check_state == check_state_run) { 4393 /* async_xor_zero_sum destroys the contents of P */ 4394 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); 4395 s->uptodate--; 4396 } 4397 if (sh->check_state >= check_state_run && 4398 sh->check_state <= check_state_run_pq) { 4399 /* async_syndrome_zero_sum preserves P and Q, so 4400 * no need to mark them !uptodate here 4401 */ 4402 set_bit(STRIPE_OP_CHECK, &s->ops_request); 4403 break; 4404 } 4405 4406 /* we have 2-disk failure */ 4407 BUG_ON(s->failed != 2); 4408 fallthrough; 4409 case check_state_compute_result: 4410 sh->check_state = check_state_idle; 4411 4412 /* check that a write has not made the stripe insync */ 4413 if (test_bit(STRIPE_INSYNC, &sh->state)) 4414 break; 4415 4416 /* now write out any block on a failed drive, 4417 * or P or Q if they were recomputed 4418 */ 4419 dev = NULL; 4420 if (s->failed == 2) { 4421 dev = &sh->dev[s->failed_num[1]]; 4422 s->locked++; 4423 set_bit(R5_LOCKED, &dev->flags); 4424 set_bit(R5_Wantwrite, &dev->flags); 4425 } 4426 if (s->failed >= 1) { 4427 dev = &sh->dev[s->failed_num[0]]; 4428 s->locked++; 4429 set_bit(R5_LOCKED, &dev->flags); 4430 set_bit(R5_Wantwrite, &dev->flags); 4431 } 4432 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { 4433 dev = &sh->dev[pd_idx]; 4434 s->locked++; 4435 set_bit(R5_LOCKED, &dev->flags); 4436 set_bit(R5_Wantwrite, &dev->flags); 4437 } 4438 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { 4439 dev = &sh->dev[qd_idx]; 4440 s->locked++; 4441 set_bit(R5_LOCKED, &dev->flags); 4442 set_bit(R5_Wantwrite, &dev->flags); 4443 } 4444 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags), 4445 "%s: disk%td not up to date\n", 4446 mdname(conf->mddev), 4447 dev - (struct r5dev *) &sh->dev)) { 4448 clear_bit(R5_LOCKED, &dev->flags); 4449 clear_bit(R5_Wantwrite, &dev->flags); 4450 s->locked--; 4451 } 4452 clear_bit(STRIPE_DEGRADED, &sh->state); 4453 4454 set_bit(STRIPE_INSYNC, &sh->state); 4455 break; 4456 case check_state_run: 4457 case check_state_run_q: 4458 case check_state_run_pq: 4459 break; /* we will be called again upon completion */ 4460 case check_state_check_result: 4461 sh->check_state = check_state_idle; 4462 4463 /* handle a successful check operation, if parity is correct 4464 * we are done. Otherwise update the mismatch count and repair 4465 * parity if !MD_RECOVERY_CHECK 4466 */ 4467 if (sh->ops.zero_sum_result == 0) { 4468 /* both parities are correct */ 4469 if (!s->failed) 4470 set_bit(STRIPE_INSYNC, &sh->state); 4471 else { 4472 /* in contrast to the raid5 case we can validate 4473 * parity, but still have a failure to write 4474 * back 4475 */ 4476 sh->check_state = check_state_compute_result; 4477 /* Returning at this point means that we may go 4478 * off and bring p and/or q uptodate again so 4479 * we make sure to check zero_sum_result again 4480 * to verify if p or q need writeback 4481 */ 4482 } 4483 } else { 4484 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); 4485 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { 4486 /* don't try to repair!! */ 4487 set_bit(STRIPE_INSYNC, &sh->state); 4488 pr_warn_ratelimited("%s: mismatch sector in range " 4489 "%llu-%llu\n", mdname(conf->mddev), 4490 (unsigned long long) sh->sector, 4491 (unsigned long long) sh->sector + 4492 RAID5_STRIPE_SECTORS(conf)); 4493 } else { 4494 int *target = &sh->ops.target; 4495 4496 sh->ops.target = -1; 4497 sh->ops.target2 = -1; 4498 sh->check_state = check_state_compute_run; 4499 set_bit(STRIPE_COMPUTE_RUN, &sh->state); 4500 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); 4501 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { 4502 set_bit(R5_Wantcompute, 4503 &sh->dev[pd_idx].flags); 4504 *target = pd_idx; 4505 target = &sh->ops.target2; 4506 s->uptodate++; 4507 } 4508 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { 4509 set_bit(R5_Wantcompute, 4510 &sh->dev[qd_idx].flags); 4511 *target = qd_idx; 4512 s->uptodate++; 4513 } 4514 } 4515 } 4516 break; 4517 case check_state_compute_run: 4518 break; 4519 default: 4520 pr_warn("%s: unknown check_state: %d sector: %llu\n", 4521 __func__, sh->check_state, 4522 (unsigned long long) sh->sector); 4523 BUG(); 4524 } 4525 } 4526 4527 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) 4528 { 4529 int i; 4530 4531 /* We have read all the blocks in this stripe and now we need to 4532 * copy some of them into a target stripe for expand. 4533 */ 4534 struct dma_async_tx_descriptor *tx = NULL; 4535 BUG_ON(sh->batch_head); 4536 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); 4537 for (i = 0; i < sh->disks; i++) 4538 if (i != sh->pd_idx && i != sh->qd_idx) { 4539 int dd_idx, j; 4540 struct stripe_head *sh2; 4541 struct async_submit_ctl submit; 4542 4543 sector_t bn = raid5_compute_blocknr(sh, i, 1); 4544 sector_t s = raid5_compute_sector(conf, bn, 0, 4545 &dd_idx, NULL); 4546 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); 4547 if (sh2 == NULL) 4548 /* so far only the early blocks of this stripe 4549 * have been requested. When later blocks 4550 * get requested, we will try again 4551 */ 4552 continue; 4553 if (!test_bit(STRIPE_EXPANDING, &sh2->state) || 4554 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { 4555 /* must have already done this block */ 4556 raid5_release_stripe(sh2); 4557 continue; 4558 } 4559 4560 /* place all the copies on one channel */ 4561 init_async_submit(&submit, 0, tx, NULL, NULL, NULL); 4562 tx = async_memcpy(sh2->dev[dd_idx].page, 4563 sh->dev[i].page, sh2->dev[dd_idx].offset, 4564 sh->dev[i].offset, RAID5_STRIPE_SIZE(conf), 4565 &submit); 4566 4567 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); 4568 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); 4569 for (j = 0; j < conf->raid_disks; j++) 4570 if (j != sh2->pd_idx && 4571 j != sh2->qd_idx && 4572 !test_bit(R5_Expanded, &sh2->dev[j].flags)) 4573 break; 4574 if (j == conf->raid_disks) { 4575 set_bit(STRIPE_EXPAND_READY, &sh2->state); 4576 set_bit(STRIPE_HANDLE, &sh2->state); 4577 } 4578 raid5_release_stripe(sh2); 4579 4580 } 4581 /* done submitting copies, wait for them to complete */ 4582 async_tx_quiesce(&tx); 4583 } 4584 4585 /* 4586 * handle_stripe - do things to a stripe. 4587 * 4588 * We lock the stripe by setting STRIPE_ACTIVE and then examine the 4589 * state of various bits to see what needs to be done. 4590 * Possible results: 4591 * return some read requests which now have data 4592 * return some write requests which are safely on storage 4593 * schedule a read on some buffers 4594 * schedule a write of some buffers 4595 * return confirmation of parity correctness 4596 * 4597 */ 4598 4599 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) 4600 { 4601 struct r5conf *conf = sh->raid_conf; 4602 int disks = sh->disks; 4603 struct r5dev *dev; 4604 int i; 4605 int do_recovery = 0; 4606 4607 memset(s, 0, sizeof(*s)); 4608 4609 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; 4610 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; 4611 s->failed_num[0] = -1; 4612 s->failed_num[1] = -1; 4613 s->log_failed = r5l_log_disk_error(conf); 4614 4615 /* Now to look around and see what can be done */ 4616 rcu_read_lock(); 4617 for (i=disks; i--; ) { 4618 struct md_rdev *rdev; 4619 sector_t first_bad; 4620 int bad_sectors; 4621 int is_bad = 0; 4622 4623 dev = &sh->dev[i]; 4624 4625 pr_debug("check %d: state 0x%lx read %p write %p written %p\n", 4626 i, dev->flags, 4627 dev->toread, dev->towrite, dev->written); 4628 /* maybe we can reply to a read 4629 * 4630 * new wantfill requests are only permitted while 4631 * ops_complete_biofill is guaranteed to be inactive 4632 */ 4633 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && 4634 !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) 4635 set_bit(R5_Wantfill, &dev->flags); 4636 4637 /* now count some things */ 4638 if (test_bit(R5_LOCKED, &dev->flags)) 4639 s->locked++; 4640 if (test_bit(R5_UPTODATE, &dev->flags)) 4641 s->uptodate++; 4642 if (test_bit(R5_Wantcompute, &dev->flags)) { 4643 s->compute++; 4644 BUG_ON(s->compute > 2); 4645 } 4646 4647 if (test_bit(R5_Wantfill, &dev->flags)) 4648 s->to_fill++; 4649 else if (dev->toread) 4650 s->to_read++; 4651 if (dev->towrite) { 4652 s->to_write++; 4653 if (!test_bit(R5_OVERWRITE, &dev->flags)) 4654 s->non_overwrite++; 4655 } 4656 if (dev->written) 4657 s->written++; 4658 /* Prefer to use the replacement for reads, but only 4659 * if it is recovered enough and has no bad blocks. 4660 */ 4661 rdev = rcu_dereference(conf->disks[i].replacement); 4662 if (rdev && !test_bit(Faulty, &rdev->flags) && 4663 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) && 4664 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 4665 &first_bad, &bad_sectors)) 4666 set_bit(R5_ReadRepl, &dev->flags); 4667 else { 4668 if (rdev && !test_bit(Faulty, &rdev->flags)) 4669 set_bit(R5_NeedReplace, &dev->flags); 4670 else 4671 clear_bit(R5_NeedReplace, &dev->flags); 4672 rdev = rcu_dereference(conf->disks[i].rdev); 4673 clear_bit(R5_ReadRepl, &dev->flags); 4674 } 4675 if (rdev && test_bit(Faulty, &rdev->flags)) 4676 rdev = NULL; 4677 if (rdev) { 4678 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 4679 &first_bad, &bad_sectors); 4680 if (s->blocked_rdev == NULL 4681 && (test_bit(Blocked, &rdev->flags) 4682 || is_bad < 0)) { 4683 if (is_bad < 0) 4684 set_bit(BlockedBadBlocks, 4685 &rdev->flags); 4686 s->blocked_rdev = rdev; 4687 atomic_inc(&rdev->nr_pending); 4688 } 4689 } 4690 clear_bit(R5_Insync, &dev->flags); 4691 if (!rdev) 4692 /* Not in-sync */; 4693 else if (is_bad) { 4694 /* also not in-sync */ 4695 if (!test_bit(WriteErrorSeen, &rdev->flags) && 4696 test_bit(R5_UPTODATE, &dev->flags)) { 4697 /* treat as in-sync, but with a read error 4698 * which we can now try to correct 4699 */ 4700 set_bit(R5_Insync, &dev->flags); 4701 set_bit(R5_ReadError, &dev->flags); 4702 } 4703 } else if (test_bit(In_sync, &rdev->flags)) 4704 set_bit(R5_Insync, &dev->flags); 4705 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset) 4706 /* in sync if before recovery_offset */ 4707 set_bit(R5_Insync, &dev->flags); 4708 else if (test_bit(R5_UPTODATE, &dev->flags) && 4709 test_bit(R5_Expanded, &dev->flags)) 4710 /* If we've reshaped into here, we assume it is Insync. 4711 * We will shortly update recovery_offset to make 4712 * it official. 4713 */ 4714 set_bit(R5_Insync, &dev->flags); 4715 4716 if (test_bit(R5_WriteError, &dev->flags)) { 4717 /* This flag does not apply to '.replacement' 4718 * only to .rdev, so make sure to check that*/ 4719 struct md_rdev *rdev2 = rcu_dereference( 4720 conf->disks[i].rdev); 4721 if (rdev2 == rdev) 4722 clear_bit(R5_Insync, &dev->flags); 4723 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { 4724 s->handle_bad_blocks = 1; 4725 atomic_inc(&rdev2->nr_pending); 4726 } else 4727 clear_bit(R5_WriteError, &dev->flags); 4728 } 4729 if (test_bit(R5_MadeGood, &dev->flags)) { 4730 /* This flag does not apply to '.replacement' 4731 * only to .rdev, so make sure to check that*/ 4732 struct md_rdev *rdev2 = rcu_dereference( 4733 conf->disks[i].rdev); 4734 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { 4735 s->handle_bad_blocks = 1; 4736 atomic_inc(&rdev2->nr_pending); 4737 } else 4738 clear_bit(R5_MadeGood, &dev->flags); 4739 } 4740 if (test_bit(R5_MadeGoodRepl, &dev->flags)) { 4741 struct md_rdev *rdev2 = rcu_dereference( 4742 conf->disks[i].replacement); 4743 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { 4744 s->handle_bad_blocks = 1; 4745 atomic_inc(&rdev2->nr_pending); 4746 } else 4747 clear_bit(R5_MadeGoodRepl, &dev->flags); 4748 } 4749 if (!test_bit(R5_Insync, &dev->flags)) { 4750 /* The ReadError flag will just be confusing now */ 4751 clear_bit(R5_ReadError, &dev->flags); 4752 clear_bit(R5_ReWrite, &dev->flags); 4753 } 4754 if (test_bit(R5_ReadError, &dev->flags)) 4755 clear_bit(R5_Insync, &dev->flags); 4756 if (!test_bit(R5_Insync, &dev->flags)) { 4757 if (s->failed < 2) 4758 s->failed_num[s->failed] = i; 4759 s->failed++; 4760 if (rdev && !test_bit(Faulty, &rdev->flags)) 4761 do_recovery = 1; 4762 else if (!rdev) { 4763 rdev = rcu_dereference( 4764 conf->disks[i].replacement); 4765 if (rdev && !test_bit(Faulty, &rdev->flags)) 4766 do_recovery = 1; 4767 } 4768 } 4769 4770 if (test_bit(R5_InJournal, &dev->flags)) 4771 s->injournal++; 4772 if (test_bit(R5_InJournal, &dev->flags) && dev->written) 4773 s->just_cached++; 4774 } 4775 if (test_bit(STRIPE_SYNCING, &sh->state)) { 4776 /* If there is a failed device being replaced, 4777 * we must be recovering. 4778 * else if we are after recovery_cp, we must be syncing 4779 * else if MD_RECOVERY_REQUESTED is set, we also are syncing. 4780 * else we can only be replacing 4781 * sync and recovery both need to read all devices, and so 4782 * use the same flag. 4783 */ 4784 if (do_recovery || 4785 sh->sector >= conf->mddev->recovery_cp || 4786 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) 4787 s->syncing = 1; 4788 else 4789 s->replacing = 1; 4790 } 4791 rcu_read_unlock(); 4792 } 4793 4794 /* 4795 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or 4796 * a head which can now be handled. 4797 */ 4798 static int clear_batch_ready(struct stripe_head *sh) 4799 { 4800 struct stripe_head *tmp; 4801 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) 4802 return (sh->batch_head && sh->batch_head != sh); 4803 spin_lock(&sh->stripe_lock); 4804 if (!sh->batch_head) { 4805 spin_unlock(&sh->stripe_lock); 4806 return 0; 4807 } 4808 4809 /* 4810 * this stripe could be added to a batch list before we check 4811 * BATCH_READY, skips it 4812 */ 4813 if (sh->batch_head != sh) { 4814 spin_unlock(&sh->stripe_lock); 4815 return 1; 4816 } 4817 spin_lock(&sh->batch_lock); 4818 list_for_each_entry(tmp, &sh->batch_list, batch_list) 4819 clear_bit(STRIPE_BATCH_READY, &tmp->state); 4820 spin_unlock(&sh->batch_lock); 4821 spin_unlock(&sh->stripe_lock); 4822 4823 /* 4824 * BATCH_READY is cleared, no new stripes can be added. 4825 * batch_list can be accessed without lock 4826 */ 4827 return 0; 4828 } 4829 4830 static void break_stripe_batch_list(struct stripe_head *head_sh, 4831 unsigned long handle_flags) 4832 { 4833 struct stripe_head *sh, *next; 4834 int i; 4835 int do_wakeup = 0; 4836 4837 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { 4838 4839 list_del_init(&sh->batch_list); 4840 4841 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | 4842 (1 << STRIPE_SYNCING) | 4843 (1 << STRIPE_REPLACED) | 4844 (1 << STRIPE_DELAYED) | 4845 (1 << STRIPE_BIT_DELAY) | 4846 (1 << STRIPE_FULL_WRITE) | 4847 (1 << STRIPE_BIOFILL_RUN) | 4848 (1 << STRIPE_COMPUTE_RUN) | 4849 (1 << STRIPE_DISCARD) | 4850 (1 << STRIPE_BATCH_READY) | 4851 (1 << STRIPE_BATCH_ERR) | 4852 (1 << STRIPE_BITMAP_PENDING)), 4853 "stripe state: %lx\n", sh->state); 4854 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | 4855 (1 << STRIPE_REPLACED)), 4856 "head stripe state: %lx\n", head_sh->state); 4857 4858 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | 4859 (1 << STRIPE_PREREAD_ACTIVE) | 4860 (1 << STRIPE_DEGRADED) | 4861 (1 << STRIPE_ON_UNPLUG_LIST)), 4862 head_sh->state & (1 << STRIPE_INSYNC)); 4863 4864 sh->check_state = head_sh->check_state; 4865 sh->reconstruct_state = head_sh->reconstruct_state; 4866 spin_lock_irq(&sh->stripe_lock); 4867 sh->batch_head = NULL; 4868 spin_unlock_irq(&sh->stripe_lock); 4869 for (i = 0; i < sh->disks; i++) { 4870 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 4871 do_wakeup = 1; 4872 sh->dev[i].flags = head_sh->dev[i].flags & 4873 (~((1 << R5_WriteError) | (1 << R5_Overlap))); 4874 } 4875 if (handle_flags == 0 || 4876 sh->state & handle_flags) 4877 set_bit(STRIPE_HANDLE, &sh->state); 4878 raid5_release_stripe(sh); 4879 } 4880 spin_lock_irq(&head_sh->stripe_lock); 4881 head_sh->batch_head = NULL; 4882 spin_unlock_irq(&head_sh->stripe_lock); 4883 for (i = 0; i < head_sh->disks; i++) 4884 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) 4885 do_wakeup = 1; 4886 if (head_sh->state & handle_flags) 4887 set_bit(STRIPE_HANDLE, &head_sh->state); 4888 4889 if (do_wakeup) 4890 wake_up(&head_sh->raid_conf->wait_for_overlap); 4891 } 4892 4893 static void handle_stripe(struct stripe_head *sh) 4894 { 4895 struct stripe_head_state s; 4896 struct r5conf *conf = sh->raid_conf; 4897 int i; 4898 int prexor; 4899 int disks = sh->disks; 4900 struct r5dev *pdev, *qdev; 4901 4902 clear_bit(STRIPE_HANDLE, &sh->state); 4903 4904 /* 4905 * handle_stripe should not continue handle the batched stripe, only 4906 * the head of batch list or lone stripe can continue. Otherwise we 4907 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE 4908 * is set for the batched stripe. 4909 */ 4910 if (clear_batch_ready(sh)) 4911 return; 4912 4913 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { 4914 /* already being handled, ensure it gets handled 4915 * again when current action finishes */ 4916 set_bit(STRIPE_HANDLE, &sh->state); 4917 return; 4918 } 4919 4920 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) 4921 break_stripe_batch_list(sh, 0); 4922 4923 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { 4924 spin_lock(&sh->stripe_lock); 4925 /* 4926 * Cannot process 'sync' concurrently with 'discard'. 4927 * Flush data in r5cache before 'sync'. 4928 */ 4929 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) && 4930 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) && 4931 !test_bit(STRIPE_DISCARD, &sh->state) && 4932 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { 4933 set_bit(STRIPE_SYNCING, &sh->state); 4934 clear_bit(STRIPE_INSYNC, &sh->state); 4935 clear_bit(STRIPE_REPLACED, &sh->state); 4936 } 4937 spin_unlock(&sh->stripe_lock); 4938 } 4939 clear_bit(STRIPE_DELAYED, &sh->state); 4940 4941 pr_debug("handling stripe %llu, state=%#lx cnt=%d, " 4942 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", 4943 (unsigned long long)sh->sector, sh->state, 4944 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, 4945 sh->check_state, sh->reconstruct_state); 4946 4947 analyse_stripe(sh, &s); 4948 4949 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) 4950 goto finish; 4951 4952 if (s.handle_bad_blocks || 4953 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { 4954 set_bit(STRIPE_HANDLE, &sh->state); 4955 goto finish; 4956 } 4957 4958 if (unlikely(s.blocked_rdev)) { 4959 if (s.syncing || s.expanding || s.expanded || 4960 s.replacing || s.to_write || s.written) { 4961 set_bit(STRIPE_HANDLE, &sh->state); 4962 goto finish; 4963 } 4964 /* There is nothing for the blocked_rdev to block */ 4965 rdev_dec_pending(s.blocked_rdev, conf->mddev); 4966 s.blocked_rdev = NULL; 4967 } 4968 4969 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { 4970 set_bit(STRIPE_OP_BIOFILL, &s.ops_request); 4971 set_bit(STRIPE_BIOFILL_RUN, &sh->state); 4972 } 4973 4974 pr_debug("locked=%d uptodate=%d to_read=%d" 4975 " to_write=%d failed=%d failed_num=%d,%d\n", 4976 s.locked, s.uptodate, s.to_read, s.to_write, s.failed, 4977 s.failed_num[0], s.failed_num[1]); 4978 /* 4979 * check if the array has lost more than max_degraded devices and, 4980 * if so, some requests might need to be failed. 4981 * 4982 * When journal device failed (log_failed), we will only process 4983 * the stripe if there is data need write to raid disks 4984 */ 4985 if (s.failed > conf->max_degraded || 4986 (s.log_failed && s.injournal == 0)) { 4987 sh->check_state = 0; 4988 sh->reconstruct_state = 0; 4989 break_stripe_batch_list(sh, 0); 4990 if (s.to_read+s.to_write+s.written) 4991 handle_failed_stripe(conf, sh, &s, disks); 4992 if (s.syncing + s.replacing) 4993 handle_failed_sync(conf, sh, &s); 4994 } 4995 4996 /* Now we check to see if any write operations have recently 4997 * completed 4998 */ 4999 prexor = 0; 5000 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) 5001 prexor = 1; 5002 if (sh->reconstruct_state == reconstruct_state_drain_result || 5003 sh->reconstruct_state == reconstruct_state_prexor_drain_result) { 5004 sh->reconstruct_state = reconstruct_state_idle; 5005 5006 /* All the 'written' buffers and the parity block are ready to 5007 * be written back to disk 5008 */ 5009 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && 5010 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); 5011 BUG_ON(sh->qd_idx >= 0 && 5012 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && 5013 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); 5014 for (i = disks; i--; ) { 5015 struct r5dev *dev = &sh->dev[i]; 5016 if (test_bit(R5_LOCKED, &dev->flags) && 5017 (i == sh->pd_idx || i == sh->qd_idx || 5018 dev->written || test_bit(R5_InJournal, 5019 &dev->flags))) { 5020 pr_debug("Writing block %d\n", i); 5021 set_bit(R5_Wantwrite, &dev->flags); 5022 if (prexor) 5023 continue; 5024 if (s.failed > 1) 5025 continue; 5026 if (!test_bit(R5_Insync, &dev->flags) || 5027 ((i == sh->pd_idx || i == sh->qd_idx) && 5028 s.failed == 0)) 5029 set_bit(STRIPE_INSYNC, &sh->state); 5030 } 5031 } 5032 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 5033 s.dec_preread_active = 1; 5034 } 5035 5036 /* 5037 * might be able to return some write requests if the parity blocks 5038 * are safe, or on a failed drive 5039 */ 5040 pdev = &sh->dev[sh->pd_idx]; 5041 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) 5042 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); 5043 qdev = &sh->dev[sh->qd_idx]; 5044 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) 5045 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) 5046 || conf->level < 6; 5047 5048 if (s.written && 5049 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) 5050 && !test_bit(R5_LOCKED, &pdev->flags) 5051 && (test_bit(R5_UPTODATE, &pdev->flags) || 5052 test_bit(R5_Discard, &pdev->flags))))) && 5053 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) 5054 && !test_bit(R5_LOCKED, &qdev->flags) 5055 && (test_bit(R5_UPTODATE, &qdev->flags) || 5056 test_bit(R5_Discard, &qdev->flags)))))) 5057 handle_stripe_clean_event(conf, sh, disks); 5058 5059 if (s.just_cached) 5060 r5c_handle_cached_data_endio(conf, sh, disks); 5061 log_stripe_write_finished(sh); 5062 5063 /* Now we might consider reading some blocks, either to check/generate 5064 * parity, or to satisfy requests 5065 * or to load a block that is being partially written. 5066 */ 5067 if (s.to_read || s.non_overwrite 5068 || (s.to_write && s.failed) 5069 || (s.syncing && (s.uptodate + s.compute < disks)) 5070 || s.replacing 5071 || s.expanding) 5072 handle_stripe_fill(sh, &s, disks); 5073 5074 /* 5075 * When the stripe finishes full journal write cycle (write to journal 5076 * and raid disk), this is the clean up procedure so it is ready for 5077 * next operation. 5078 */ 5079 r5c_finish_stripe_write_out(conf, sh, &s); 5080 5081 /* 5082 * Now to consider new write requests, cache write back and what else, 5083 * if anything should be read. We do not handle new writes when: 5084 * 1/ A 'write' operation (copy+xor) is already in flight. 5085 * 2/ A 'check' operation is in flight, as it may clobber the parity 5086 * block. 5087 * 3/ A r5c cache log write is in flight. 5088 */ 5089 5090 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { 5091 if (!r5c_is_writeback(conf->log)) { 5092 if (s.to_write) 5093 handle_stripe_dirtying(conf, sh, &s, disks); 5094 } else { /* write back cache */ 5095 int ret = 0; 5096 5097 /* First, try handle writes in caching phase */ 5098 if (s.to_write) 5099 ret = r5c_try_caching_write(conf, sh, &s, 5100 disks); 5101 /* 5102 * If caching phase failed: ret == -EAGAIN 5103 * OR 5104 * stripe under reclaim: !caching && injournal 5105 * 5106 * fall back to handle_stripe_dirtying() 5107 */ 5108 if (ret == -EAGAIN || 5109 /* stripe under reclaim: !caching && injournal */ 5110 (!test_bit(STRIPE_R5C_CACHING, &sh->state) && 5111 s.injournal > 0)) { 5112 ret = handle_stripe_dirtying(conf, sh, &s, 5113 disks); 5114 if (ret == -EAGAIN) 5115 goto finish; 5116 } 5117 } 5118 } 5119 5120 /* maybe we need to check and possibly fix the parity for this stripe 5121 * Any reads will already have been scheduled, so we just see if enough 5122 * data is available. The parity check is held off while parity 5123 * dependent operations are in flight. 5124 */ 5125 if (sh->check_state || 5126 (s.syncing && s.locked == 0 && 5127 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && 5128 !test_bit(STRIPE_INSYNC, &sh->state))) { 5129 if (conf->level == 6) 5130 handle_parity_checks6(conf, sh, &s, disks); 5131 else 5132 handle_parity_checks5(conf, sh, &s, disks); 5133 } 5134 5135 if ((s.replacing || s.syncing) && s.locked == 0 5136 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) 5137 && !test_bit(STRIPE_REPLACED, &sh->state)) { 5138 /* Write out to replacement devices where possible */ 5139 for (i = 0; i < conf->raid_disks; i++) 5140 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { 5141 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); 5142 set_bit(R5_WantReplace, &sh->dev[i].flags); 5143 set_bit(R5_LOCKED, &sh->dev[i].flags); 5144 s.locked++; 5145 } 5146 if (s.replacing) 5147 set_bit(STRIPE_INSYNC, &sh->state); 5148 set_bit(STRIPE_REPLACED, &sh->state); 5149 } 5150 if ((s.syncing || s.replacing) && s.locked == 0 && 5151 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && 5152 test_bit(STRIPE_INSYNC, &sh->state)) { 5153 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); 5154 clear_bit(STRIPE_SYNCING, &sh->state); 5155 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) 5156 wake_up(&conf->wait_for_overlap); 5157 } 5158 5159 /* If the failed drives are just a ReadError, then we might need 5160 * to progress the repair/check process 5161 */ 5162 if (s.failed <= conf->max_degraded && !conf->mddev->ro) 5163 for (i = 0; i < s.failed; i++) { 5164 struct r5dev *dev = &sh->dev[s.failed_num[i]]; 5165 if (test_bit(R5_ReadError, &dev->flags) 5166 && !test_bit(R5_LOCKED, &dev->flags) 5167 && test_bit(R5_UPTODATE, &dev->flags) 5168 ) { 5169 if (!test_bit(R5_ReWrite, &dev->flags)) { 5170 set_bit(R5_Wantwrite, &dev->flags); 5171 set_bit(R5_ReWrite, &dev->flags); 5172 } else 5173 /* let's read it back */ 5174 set_bit(R5_Wantread, &dev->flags); 5175 set_bit(R5_LOCKED, &dev->flags); 5176 s.locked++; 5177 } 5178 } 5179 5180 /* Finish reconstruct operations initiated by the expansion process */ 5181 if (sh->reconstruct_state == reconstruct_state_result) { 5182 struct stripe_head *sh_src 5183 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); 5184 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { 5185 /* sh cannot be written until sh_src has been read. 5186 * so arrange for sh to be delayed a little 5187 */ 5188 set_bit(STRIPE_DELAYED, &sh->state); 5189 set_bit(STRIPE_HANDLE, &sh->state); 5190 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, 5191 &sh_src->state)) 5192 atomic_inc(&conf->preread_active_stripes); 5193 raid5_release_stripe(sh_src); 5194 goto finish; 5195 } 5196 if (sh_src) 5197 raid5_release_stripe(sh_src); 5198 5199 sh->reconstruct_state = reconstruct_state_idle; 5200 clear_bit(STRIPE_EXPANDING, &sh->state); 5201 for (i = conf->raid_disks; i--; ) { 5202 set_bit(R5_Wantwrite, &sh->dev[i].flags); 5203 set_bit(R5_LOCKED, &sh->dev[i].flags); 5204 s.locked++; 5205 } 5206 } 5207 5208 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && 5209 !sh->reconstruct_state) { 5210 /* Need to write out all blocks after computing parity */ 5211 sh->disks = conf->raid_disks; 5212 stripe_set_idx(sh->sector, conf, 0, sh); 5213 schedule_reconstruction(sh, &s, 1, 1); 5214 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { 5215 clear_bit(STRIPE_EXPAND_READY, &sh->state); 5216 atomic_dec(&conf->reshape_stripes); 5217 wake_up(&conf->wait_for_overlap); 5218 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); 5219 } 5220 5221 if (s.expanding && s.locked == 0 && 5222 !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) 5223 handle_stripe_expansion(conf, sh); 5224 5225 finish: 5226 /* wait for this device to become unblocked */ 5227 if (unlikely(s.blocked_rdev)) { 5228 if (conf->mddev->external) 5229 md_wait_for_blocked_rdev(s.blocked_rdev, 5230 conf->mddev); 5231 else 5232 /* Internal metadata will immediately 5233 * be written by raid5d, so we don't 5234 * need to wait here. 5235 */ 5236 rdev_dec_pending(s.blocked_rdev, 5237 conf->mddev); 5238 } 5239 5240 if (s.handle_bad_blocks) 5241 for (i = disks; i--; ) { 5242 struct md_rdev *rdev; 5243 struct r5dev *dev = &sh->dev[i]; 5244 if (test_and_clear_bit(R5_WriteError, &dev->flags)) { 5245 /* We own a safe reference to the rdev */ 5246 rdev = rdev_pend_deref(conf->disks[i].rdev); 5247 if (!rdev_set_badblocks(rdev, sh->sector, 5248 RAID5_STRIPE_SECTORS(conf), 0)) 5249 md_error(conf->mddev, rdev); 5250 rdev_dec_pending(rdev, conf->mddev); 5251 } 5252 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { 5253 rdev = rdev_pend_deref(conf->disks[i].rdev); 5254 rdev_clear_badblocks(rdev, sh->sector, 5255 RAID5_STRIPE_SECTORS(conf), 0); 5256 rdev_dec_pending(rdev, conf->mddev); 5257 } 5258 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { 5259 rdev = rdev_pend_deref(conf->disks[i].replacement); 5260 if (!rdev) 5261 /* rdev have been moved down */ 5262 rdev = rdev_pend_deref(conf->disks[i].rdev); 5263 rdev_clear_badblocks(rdev, sh->sector, 5264 RAID5_STRIPE_SECTORS(conf), 0); 5265 rdev_dec_pending(rdev, conf->mddev); 5266 } 5267 } 5268 5269 if (s.ops_request) 5270 raid_run_ops(sh, s.ops_request); 5271 5272 ops_run_io(sh, &s); 5273 5274 if (s.dec_preread_active) { 5275 /* We delay this until after ops_run_io so that if make_request 5276 * is waiting on a flush, it won't continue until the writes 5277 * have actually been submitted. 5278 */ 5279 atomic_dec(&conf->preread_active_stripes); 5280 if (atomic_read(&conf->preread_active_stripes) < 5281 IO_THRESHOLD) 5282 md_wakeup_thread(conf->mddev->thread); 5283 } 5284 5285 clear_bit_unlock(STRIPE_ACTIVE, &sh->state); 5286 } 5287 5288 static void raid5_activate_delayed(struct r5conf *conf) 5289 __must_hold(&conf->device_lock) 5290 { 5291 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { 5292 while (!list_empty(&conf->delayed_list)) { 5293 struct list_head *l = conf->delayed_list.next; 5294 struct stripe_head *sh; 5295 sh = list_entry(l, struct stripe_head, lru); 5296 list_del_init(l); 5297 clear_bit(STRIPE_DELAYED, &sh->state); 5298 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 5299 atomic_inc(&conf->preread_active_stripes); 5300 list_add_tail(&sh->lru, &conf->hold_list); 5301 raid5_wakeup_stripe_thread(sh); 5302 } 5303 } 5304 } 5305 5306 static void activate_bit_delay(struct r5conf *conf, 5307 struct list_head *temp_inactive_list) 5308 __must_hold(&conf->device_lock) 5309 { 5310 struct list_head head; 5311 list_add(&head, &conf->bitmap_list); 5312 list_del_init(&conf->bitmap_list); 5313 while (!list_empty(&head)) { 5314 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); 5315 int hash; 5316 list_del_init(&sh->lru); 5317 atomic_inc(&sh->count); 5318 hash = sh->hash_lock_index; 5319 __release_stripe(conf, sh, &temp_inactive_list[hash]); 5320 } 5321 } 5322 5323 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) 5324 { 5325 struct r5conf *conf = mddev->private; 5326 sector_t sector = bio->bi_iter.bi_sector; 5327 unsigned int chunk_sectors; 5328 unsigned int bio_sectors = bio_sectors(bio); 5329 5330 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); 5331 return chunk_sectors >= 5332 ((sector & (chunk_sectors - 1)) + bio_sectors); 5333 } 5334 5335 /* 5336 * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) 5337 * later sampled by raid5d. 5338 */ 5339 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) 5340 { 5341 unsigned long flags; 5342 5343 spin_lock_irqsave(&conf->device_lock, flags); 5344 5345 bi->bi_next = conf->retry_read_aligned_list; 5346 conf->retry_read_aligned_list = bi; 5347 5348 spin_unlock_irqrestore(&conf->device_lock, flags); 5349 md_wakeup_thread(conf->mddev->thread); 5350 } 5351 5352 static struct bio *remove_bio_from_retry(struct r5conf *conf, 5353 unsigned int *offset) 5354 { 5355 struct bio *bi; 5356 5357 bi = conf->retry_read_aligned; 5358 if (bi) { 5359 *offset = conf->retry_read_offset; 5360 conf->retry_read_aligned = NULL; 5361 return bi; 5362 } 5363 bi = conf->retry_read_aligned_list; 5364 if(bi) { 5365 conf->retry_read_aligned_list = bi->bi_next; 5366 bi->bi_next = NULL; 5367 *offset = 0; 5368 } 5369 5370 return bi; 5371 } 5372 5373 /* 5374 * The "raid5_align_endio" should check if the read succeeded and if it 5375 * did, call bio_endio on the original bio (having bio_put the new bio 5376 * first). 5377 * If the read failed.. 5378 */ 5379 static void raid5_align_endio(struct bio *bi) 5380 { 5381 struct md_io_acct *md_io_acct = bi->bi_private; 5382 struct bio *raid_bi = md_io_acct->orig_bio; 5383 struct mddev *mddev; 5384 struct r5conf *conf; 5385 struct md_rdev *rdev; 5386 blk_status_t error = bi->bi_status; 5387 unsigned long start_time = md_io_acct->start_time; 5388 5389 bio_put(bi); 5390 5391 rdev = (void*)raid_bi->bi_next; 5392 raid_bi->bi_next = NULL; 5393 mddev = rdev->mddev; 5394 conf = mddev->private; 5395 5396 rdev_dec_pending(rdev, conf->mddev); 5397 5398 if (!error) { 5399 if (blk_queue_io_stat(raid_bi->bi_bdev->bd_disk->queue)) 5400 bio_end_io_acct(raid_bi, start_time); 5401 bio_endio(raid_bi); 5402 if (atomic_dec_and_test(&conf->active_aligned_reads)) 5403 wake_up(&conf->wait_for_quiescent); 5404 return; 5405 } 5406 5407 pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); 5408 5409 add_bio_to_retry(raid_bi, conf); 5410 } 5411 5412 static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio) 5413 { 5414 struct r5conf *conf = mddev->private; 5415 struct bio *align_bio; 5416 struct md_rdev *rdev; 5417 sector_t sector, end_sector, first_bad; 5418 int bad_sectors, dd_idx; 5419 struct md_io_acct *md_io_acct; 5420 bool did_inc; 5421 5422 if (!in_chunk_boundary(mddev, raid_bio)) { 5423 pr_debug("%s: non aligned\n", __func__); 5424 return 0; 5425 } 5426 5427 sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0, 5428 &dd_idx, NULL); 5429 end_sector = bio_end_sector(raid_bio); 5430 5431 rcu_read_lock(); 5432 if (r5c_big_stripe_cached(conf, sector)) 5433 goto out_rcu_unlock; 5434 5435 rdev = rcu_dereference(conf->disks[dd_idx].replacement); 5436 if (!rdev || test_bit(Faulty, &rdev->flags) || 5437 rdev->recovery_offset < end_sector) { 5438 rdev = rcu_dereference(conf->disks[dd_idx].rdev); 5439 if (!rdev) 5440 goto out_rcu_unlock; 5441 if (test_bit(Faulty, &rdev->flags) || 5442 !(test_bit(In_sync, &rdev->flags) || 5443 rdev->recovery_offset >= end_sector)) 5444 goto out_rcu_unlock; 5445 } 5446 5447 atomic_inc(&rdev->nr_pending); 5448 rcu_read_unlock(); 5449 5450 if (is_badblock(rdev, sector, bio_sectors(raid_bio), &first_bad, 5451 &bad_sectors)) { 5452 bio_put(raid_bio); 5453 rdev_dec_pending(rdev, mddev); 5454 return 0; 5455 } 5456 5457 align_bio = bio_alloc_clone(rdev->bdev, raid_bio, GFP_NOIO, 5458 &mddev->io_acct_set); 5459 md_io_acct = container_of(align_bio, struct md_io_acct, bio_clone); 5460 raid_bio->bi_next = (void *)rdev; 5461 if (blk_queue_io_stat(raid_bio->bi_bdev->bd_disk->queue)) 5462 md_io_acct->start_time = bio_start_io_acct(raid_bio); 5463 md_io_acct->orig_bio = raid_bio; 5464 5465 align_bio->bi_end_io = raid5_align_endio; 5466 align_bio->bi_private = md_io_acct; 5467 align_bio->bi_iter.bi_sector = sector; 5468 5469 /* No reshape active, so we can trust rdev->data_offset */ 5470 align_bio->bi_iter.bi_sector += rdev->data_offset; 5471 5472 did_inc = false; 5473 if (conf->quiesce == 0) { 5474 atomic_inc(&conf->active_aligned_reads); 5475 did_inc = true; 5476 } 5477 /* need a memory barrier to detect the race with raid5_quiesce() */ 5478 if (!did_inc || smp_load_acquire(&conf->quiesce) != 0) { 5479 /* quiesce is in progress, so we need to undo io activation and wait 5480 * for it to finish 5481 */ 5482 if (did_inc && atomic_dec_and_test(&conf->active_aligned_reads)) 5483 wake_up(&conf->wait_for_quiescent); 5484 spin_lock_irq(&conf->device_lock); 5485 wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0, 5486 conf->device_lock); 5487 atomic_inc(&conf->active_aligned_reads); 5488 spin_unlock_irq(&conf->device_lock); 5489 } 5490 5491 if (mddev->gendisk) 5492 trace_block_bio_remap(align_bio, disk_devt(mddev->gendisk), 5493 raid_bio->bi_iter.bi_sector); 5494 submit_bio_noacct(align_bio); 5495 return 1; 5496 5497 out_rcu_unlock: 5498 rcu_read_unlock(); 5499 return 0; 5500 } 5501 5502 static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) 5503 { 5504 struct bio *split; 5505 sector_t sector = raid_bio->bi_iter.bi_sector; 5506 unsigned chunk_sects = mddev->chunk_sectors; 5507 unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); 5508 5509 if (sectors < bio_sectors(raid_bio)) { 5510 struct r5conf *conf = mddev->private; 5511 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split); 5512 bio_chain(split, raid_bio); 5513 submit_bio_noacct(raid_bio); 5514 raid_bio = split; 5515 } 5516 5517 if (!raid5_read_one_chunk(mddev, raid_bio)) 5518 return raid_bio; 5519 5520 return NULL; 5521 } 5522 5523 /* __get_priority_stripe - get the next stripe to process 5524 * 5525 * Full stripe writes are allowed to pass preread active stripes up until 5526 * the bypass_threshold is exceeded. In general the bypass_count 5527 * increments when the handle_list is handled before the hold_list; however, it 5528 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a 5529 * stripe with in flight i/o. The bypass_count will be reset when the 5530 * head of the hold_list has changed, i.e. the head was promoted to the 5531 * handle_list. 5532 */ 5533 static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) 5534 __must_hold(&conf->device_lock) 5535 { 5536 struct stripe_head *sh, *tmp; 5537 struct list_head *handle_list = NULL; 5538 struct r5worker_group *wg; 5539 bool second_try = !r5c_is_writeback(conf->log) && 5540 !r5l_log_disk_error(conf); 5541 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) || 5542 r5l_log_disk_error(conf); 5543 5544 again: 5545 wg = NULL; 5546 sh = NULL; 5547 if (conf->worker_cnt_per_group == 0) { 5548 handle_list = try_loprio ? &conf->loprio_list : 5549 &conf->handle_list; 5550 } else if (group != ANY_GROUP) { 5551 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list : 5552 &conf->worker_groups[group].handle_list; 5553 wg = &conf->worker_groups[group]; 5554 } else { 5555 int i; 5556 for (i = 0; i < conf->group_cnt; i++) { 5557 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list : 5558 &conf->worker_groups[i].handle_list; 5559 wg = &conf->worker_groups[i]; 5560 if (!list_empty(handle_list)) 5561 break; 5562 } 5563 } 5564 5565 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", 5566 __func__, 5567 list_empty(handle_list) ? "empty" : "busy", 5568 list_empty(&conf->hold_list) ? "empty" : "busy", 5569 atomic_read(&conf->pending_full_writes), conf->bypass_count); 5570 5571 if (!list_empty(handle_list)) { 5572 sh = list_entry(handle_list->next, typeof(*sh), lru); 5573 5574 if (list_empty(&conf->hold_list)) 5575 conf->bypass_count = 0; 5576 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { 5577 if (conf->hold_list.next == conf->last_hold) 5578 conf->bypass_count++; 5579 else { 5580 conf->last_hold = conf->hold_list.next; 5581 conf->bypass_count -= conf->bypass_threshold; 5582 if (conf->bypass_count < 0) 5583 conf->bypass_count = 0; 5584 } 5585 } 5586 } else if (!list_empty(&conf->hold_list) && 5587 ((conf->bypass_threshold && 5588 conf->bypass_count > conf->bypass_threshold) || 5589 atomic_read(&conf->pending_full_writes) == 0)) { 5590 5591 list_for_each_entry(tmp, &conf->hold_list, lru) { 5592 if (conf->worker_cnt_per_group == 0 || 5593 group == ANY_GROUP || 5594 !cpu_online(tmp->cpu) || 5595 cpu_to_group(tmp->cpu) == group) { 5596 sh = tmp; 5597 break; 5598 } 5599 } 5600 5601 if (sh) { 5602 conf->bypass_count -= conf->bypass_threshold; 5603 if (conf->bypass_count < 0) 5604 conf->bypass_count = 0; 5605 } 5606 wg = NULL; 5607 } 5608 5609 if (!sh) { 5610 if (second_try) 5611 return NULL; 5612 second_try = true; 5613 try_loprio = !try_loprio; 5614 goto again; 5615 } 5616 5617 if (wg) { 5618 wg->stripes_cnt--; 5619 sh->group = NULL; 5620 } 5621 list_del_init(&sh->lru); 5622 BUG_ON(atomic_inc_return(&sh->count) != 1); 5623 return sh; 5624 } 5625 5626 struct raid5_plug_cb { 5627 struct blk_plug_cb cb; 5628 struct list_head list; 5629 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; 5630 }; 5631 5632 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) 5633 { 5634 struct raid5_plug_cb *cb = container_of( 5635 blk_cb, struct raid5_plug_cb, cb); 5636 struct stripe_head *sh; 5637 struct mddev *mddev = cb->cb.data; 5638 struct r5conf *conf = mddev->private; 5639 int cnt = 0; 5640 int hash; 5641 5642 if (cb->list.next && !list_empty(&cb->list)) { 5643 spin_lock_irq(&conf->device_lock); 5644 while (!list_empty(&cb->list)) { 5645 sh = list_first_entry(&cb->list, struct stripe_head, lru); 5646 list_del_init(&sh->lru); 5647 /* 5648 * avoid race release_stripe_plug() sees 5649 * STRIPE_ON_UNPLUG_LIST clear but the stripe 5650 * is still in our list 5651 */ 5652 smp_mb__before_atomic(); 5653 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); 5654 /* 5655 * STRIPE_ON_RELEASE_LIST could be set here. In that 5656 * case, the count is always > 1 here 5657 */ 5658 hash = sh->hash_lock_index; 5659 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); 5660 cnt++; 5661 } 5662 spin_unlock_irq(&conf->device_lock); 5663 } 5664 release_inactive_stripe_list(conf, cb->temp_inactive_list, 5665 NR_STRIPE_HASH_LOCKS); 5666 if (mddev->queue) 5667 trace_block_unplug(mddev->queue, cnt, !from_schedule); 5668 kfree(cb); 5669 } 5670 5671 static void release_stripe_plug(struct mddev *mddev, 5672 struct stripe_head *sh) 5673 { 5674 struct blk_plug_cb *blk_cb = blk_check_plugged( 5675 raid5_unplug, mddev, 5676 sizeof(struct raid5_plug_cb)); 5677 struct raid5_plug_cb *cb; 5678 5679 if (!blk_cb) { 5680 raid5_release_stripe(sh); 5681 return; 5682 } 5683 5684 cb = container_of(blk_cb, struct raid5_plug_cb, cb); 5685 5686 if (cb->list.next == NULL) { 5687 int i; 5688 INIT_LIST_HEAD(&cb->list); 5689 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) 5690 INIT_LIST_HEAD(cb->temp_inactive_list + i); 5691 } 5692 5693 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) 5694 list_add_tail(&sh->lru, &cb->list); 5695 else 5696 raid5_release_stripe(sh); 5697 } 5698 5699 static void make_discard_request(struct mddev *mddev, struct bio *bi) 5700 { 5701 struct r5conf *conf = mddev->private; 5702 sector_t logical_sector, last_sector; 5703 struct stripe_head *sh; 5704 int stripe_sectors; 5705 5706 /* We need to handle this when io_uring supports discard/trim */ 5707 if (WARN_ON_ONCE(bi->bi_opf & REQ_NOWAIT)) 5708 return; 5709 5710 if (mddev->reshape_position != MaxSector) 5711 /* Skip discard while reshape is happening */ 5712 return; 5713 5714 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); 5715 last_sector = bio_end_sector(bi); 5716 5717 bi->bi_next = NULL; 5718 5719 stripe_sectors = conf->chunk_sectors * 5720 (conf->raid_disks - conf->max_degraded); 5721 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, 5722 stripe_sectors); 5723 sector_div(last_sector, stripe_sectors); 5724 5725 logical_sector *= conf->chunk_sectors; 5726 last_sector *= conf->chunk_sectors; 5727 5728 for (; logical_sector < last_sector; 5729 logical_sector += RAID5_STRIPE_SECTORS(conf)) { 5730 DEFINE_WAIT(w); 5731 int d; 5732 again: 5733 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); 5734 prepare_to_wait(&conf->wait_for_overlap, &w, 5735 TASK_UNINTERRUPTIBLE); 5736 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); 5737 if (test_bit(STRIPE_SYNCING, &sh->state)) { 5738 raid5_release_stripe(sh); 5739 schedule(); 5740 goto again; 5741 } 5742 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); 5743 spin_lock_irq(&sh->stripe_lock); 5744 for (d = 0; d < conf->raid_disks; d++) { 5745 if (d == sh->pd_idx || d == sh->qd_idx) 5746 continue; 5747 if (sh->dev[d].towrite || sh->dev[d].toread) { 5748 set_bit(R5_Overlap, &sh->dev[d].flags); 5749 spin_unlock_irq(&sh->stripe_lock); 5750 raid5_release_stripe(sh); 5751 schedule(); 5752 goto again; 5753 } 5754 } 5755 set_bit(STRIPE_DISCARD, &sh->state); 5756 finish_wait(&conf->wait_for_overlap, &w); 5757 sh->overwrite_disks = 0; 5758 for (d = 0; d < conf->raid_disks; d++) { 5759 if (d == sh->pd_idx || d == sh->qd_idx) 5760 continue; 5761 sh->dev[d].towrite = bi; 5762 set_bit(R5_OVERWRITE, &sh->dev[d].flags); 5763 bio_inc_remaining(bi); 5764 md_write_inc(mddev, bi); 5765 sh->overwrite_disks++; 5766 } 5767 spin_unlock_irq(&sh->stripe_lock); 5768 if (conf->mddev->bitmap) { 5769 for (d = 0; 5770 d < conf->raid_disks - conf->max_degraded; 5771 d++) 5772 md_bitmap_startwrite(mddev->bitmap, 5773 sh->sector, 5774 RAID5_STRIPE_SECTORS(conf), 5775 0); 5776 sh->bm_seq = conf->seq_flush + 1; 5777 set_bit(STRIPE_BIT_DELAY, &sh->state); 5778 } 5779 5780 set_bit(STRIPE_HANDLE, &sh->state); 5781 clear_bit(STRIPE_DELAYED, &sh->state); 5782 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 5783 atomic_inc(&conf->preread_active_stripes); 5784 release_stripe_plug(mddev, sh); 5785 } 5786 5787 bio_endio(bi); 5788 } 5789 5790 static bool raid5_make_request(struct mddev *mddev, struct bio * bi) 5791 { 5792 struct r5conf *conf = mddev->private; 5793 int dd_idx; 5794 sector_t new_sector; 5795 sector_t logical_sector, last_sector; 5796 struct stripe_head *sh; 5797 const int rw = bio_data_dir(bi); 5798 DEFINE_WAIT(w); 5799 bool do_prepare; 5800 bool do_flush = false; 5801 5802 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { 5803 int ret = log_handle_flush_request(conf, bi); 5804 5805 if (ret == 0) 5806 return true; 5807 if (ret == -ENODEV) { 5808 if (md_flush_request(mddev, bi)) 5809 return true; 5810 } 5811 /* ret == -EAGAIN, fallback */ 5812 /* 5813 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, 5814 * we need to flush journal device 5815 */ 5816 do_flush = bi->bi_opf & REQ_PREFLUSH; 5817 } 5818 5819 if (!md_write_start(mddev, bi)) 5820 return false; 5821 /* 5822 * If array is degraded, better not do chunk aligned read because 5823 * later we might have to read it again in order to reconstruct 5824 * data on failed drives. 5825 */ 5826 if (rw == READ && mddev->degraded == 0 && 5827 mddev->reshape_position == MaxSector) { 5828 bi = chunk_aligned_read(mddev, bi); 5829 if (!bi) 5830 return true; 5831 } 5832 5833 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { 5834 make_discard_request(mddev, bi); 5835 md_write_end(mddev); 5836 return true; 5837 } 5838 5839 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); 5840 last_sector = bio_end_sector(bi); 5841 bi->bi_next = NULL; 5842 5843 /* Bail out if conflicts with reshape and REQ_NOWAIT is set */ 5844 if ((bi->bi_opf & REQ_NOWAIT) && 5845 (conf->reshape_progress != MaxSector) && 5846 (mddev->reshape_backwards 5847 ? (logical_sector > conf->reshape_progress && logical_sector <= conf->reshape_safe) 5848 : (logical_sector >= conf->reshape_safe && logical_sector < conf->reshape_progress))) { 5849 bio_wouldblock_error(bi); 5850 if (rw == WRITE) 5851 md_write_end(mddev); 5852 return true; 5853 } 5854 md_account_bio(mddev, &bi); 5855 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); 5856 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) { 5857 int previous; 5858 int seq; 5859 5860 do_prepare = false; 5861 retry: 5862 seq = read_seqcount_begin(&conf->gen_lock); 5863 previous = 0; 5864 if (do_prepare) 5865 prepare_to_wait(&conf->wait_for_overlap, &w, 5866 TASK_UNINTERRUPTIBLE); 5867 if (unlikely(conf->reshape_progress != MaxSector)) { 5868 /* spinlock is needed as reshape_progress may be 5869 * 64bit on a 32bit platform, and so it might be 5870 * possible to see a half-updated value 5871 * Of course reshape_progress could change after 5872 * the lock is dropped, so once we get a reference 5873 * to the stripe that we think it is, we will have 5874 * to check again. 5875 */ 5876 spin_lock_irq(&conf->device_lock); 5877 if (mddev->reshape_backwards 5878 ? logical_sector < conf->reshape_progress 5879 : logical_sector >= conf->reshape_progress) { 5880 previous = 1; 5881 } else { 5882 if (mddev->reshape_backwards 5883 ? logical_sector < conf->reshape_safe 5884 : logical_sector >= conf->reshape_safe) { 5885 spin_unlock_irq(&conf->device_lock); 5886 schedule(); 5887 do_prepare = true; 5888 goto retry; 5889 } 5890 } 5891 spin_unlock_irq(&conf->device_lock); 5892 } 5893 5894 new_sector = raid5_compute_sector(conf, logical_sector, 5895 previous, 5896 &dd_idx, NULL); 5897 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", 5898 (unsigned long long)new_sector, 5899 (unsigned long long)logical_sector); 5900 5901 sh = raid5_get_active_stripe(conf, new_sector, previous, 5902 (bi->bi_opf & REQ_RAHEAD), 0); 5903 if (sh) { 5904 if (unlikely(previous)) { 5905 /* expansion might have moved on while waiting for a 5906 * stripe, so we must do the range check again. 5907 * Expansion could still move past after this 5908 * test, but as we are holding a reference to 5909 * 'sh', we know that if that happens, 5910 * STRIPE_EXPANDING will get set and the expansion 5911 * won't proceed until we finish with the stripe. 5912 */ 5913 int must_retry = 0; 5914 spin_lock_irq(&conf->device_lock); 5915 if (mddev->reshape_backwards 5916 ? logical_sector >= conf->reshape_progress 5917 : logical_sector < conf->reshape_progress) 5918 /* mismatch, need to try again */ 5919 must_retry = 1; 5920 spin_unlock_irq(&conf->device_lock); 5921 if (must_retry) { 5922 raid5_release_stripe(sh); 5923 schedule(); 5924 do_prepare = true; 5925 goto retry; 5926 } 5927 } 5928 if (read_seqcount_retry(&conf->gen_lock, seq)) { 5929 /* Might have got the wrong stripe_head 5930 * by accident 5931 */ 5932 raid5_release_stripe(sh); 5933 goto retry; 5934 } 5935 5936 if (test_bit(STRIPE_EXPANDING, &sh->state) || 5937 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { 5938 /* Stripe is busy expanding or 5939 * add failed due to overlap. Flush everything 5940 * and wait a while 5941 */ 5942 md_wakeup_thread(mddev->thread); 5943 raid5_release_stripe(sh); 5944 schedule(); 5945 do_prepare = true; 5946 goto retry; 5947 } 5948 if (do_flush) { 5949 set_bit(STRIPE_R5C_PREFLUSH, &sh->state); 5950 /* we only need flush for one stripe */ 5951 do_flush = false; 5952 } 5953 5954 set_bit(STRIPE_HANDLE, &sh->state); 5955 clear_bit(STRIPE_DELAYED, &sh->state); 5956 if ((!sh->batch_head || sh == sh->batch_head) && 5957 (bi->bi_opf & REQ_SYNC) && 5958 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 5959 atomic_inc(&conf->preread_active_stripes); 5960 release_stripe_plug(mddev, sh); 5961 } else { 5962 /* cannot get stripe for read-ahead, just give-up */ 5963 bi->bi_status = BLK_STS_IOERR; 5964 break; 5965 } 5966 } 5967 finish_wait(&conf->wait_for_overlap, &w); 5968 5969 if (rw == WRITE) 5970 md_write_end(mddev); 5971 bio_endio(bi); 5972 return true; 5973 } 5974 5975 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); 5976 5977 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) 5978 { 5979 /* reshaping is quite different to recovery/resync so it is 5980 * handled quite separately ... here. 5981 * 5982 * On each call to sync_request, we gather one chunk worth of 5983 * destination stripes and flag them as expanding. 5984 * Then we find all the source stripes and request reads. 5985 * As the reads complete, handle_stripe will copy the data 5986 * into the destination stripe and release that stripe. 5987 */ 5988 struct r5conf *conf = mddev->private; 5989 struct stripe_head *sh; 5990 struct md_rdev *rdev; 5991 sector_t first_sector, last_sector; 5992 int raid_disks = conf->previous_raid_disks; 5993 int data_disks = raid_disks - conf->max_degraded; 5994 int new_data_disks = conf->raid_disks - conf->max_degraded; 5995 int i; 5996 int dd_idx; 5997 sector_t writepos, readpos, safepos; 5998 sector_t stripe_addr; 5999 int reshape_sectors; 6000 struct list_head stripes; 6001 sector_t retn; 6002 6003 if (sector_nr == 0) { 6004 /* If restarting in the middle, skip the initial sectors */ 6005 if (mddev->reshape_backwards && 6006 conf->reshape_progress < raid5_size(mddev, 0, 0)) { 6007 sector_nr = raid5_size(mddev, 0, 0) 6008 - conf->reshape_progress; 6009 } else if (mddev->reshape_backwards && 6010 conf->reshape_progress == MaxSector) { 6011 /* shouldn't happen, but just in case, finish up.*/ 6012 sector_nr = MaxSector; 6013 } else if (!mddev->reshape_backwards && 6014 conf->reshape_progress > 0) 6015 sector_nr = conf->reshape_progress; 6016 sector_div(sector_nr, new_data_disks); 6017 if (sector_nr) { 6018 mddev->curr_resync_completed = sector_nr; 6019 sysfs_notify_dirent_safe(mddev->sysfs_completed); 6020 *skipped = 1; 6021 retn = sector_nr; 6022 goto finish; 6023 } 6024 } 6025 6026 /* We need to process a full chunk at a time. 6027 * If old and new chunk sizes differ, we need to process the 6028 * largest of these 6029 */ 6030 6031 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); 6032 6033 /* We update the metadata at least every 10 seconds, or when 6034 * the data about to be copied would over-write the source of 6035 * the data at the front of the range. i.e. one new_stripe 6036 * along from reshape_progress new_maps to after where 6037 * reshape_safe old_maps to 6038 */ 6039 writepos = conf->reshape_progress; 6040 sector_div(writepos, new_data_disks); 6041 readpos = conf->reshape_progress; 6042 sector_div(readpos, data_disks); 6043 safepos = conf->reshape_safe; 6044 sector_div(safepos, data_disks); 6045 if (mddev->reshape_backwards) { 6046 BUG_ON(writepos < reshape_sectors); 6047 writepos -= reshape_sectors; 6048 readpos += reshape_sectors; 6049 safepos += reshape_sectors; 6050 } else { 6051 writepos += reshape_sectors; 6052 /* readpos and safepos are worst-case calculations. 6053 * A negative number is overly pessimistic, and causes 6054 * obvious problems for unsigned storage. So clip to 0. 6055 */ 6056 readpos -= min_t(sector_t, reshape_sectors, readpos); 6057 safepos -= min_t(sector_t, reshape_sectors, safepos); 6058 } 6059 6060 /* Having calculated the 'writepos' possibly use it 6061 * to set 'stripe_addr' which is where we will write to. 6062 */ 6063 if (mddev->reshape_backwards) { 6064 BUG_ON(conf->reshape_progress == 0); 6065 stripe_addr = writepos; 6066 BUG_ON((mddev->dev_sectors & 6067 ~((sector_t)reshape_sectors - 1)) 6068 - reshape_sectors - stripe_addr 6069 != sector_nr); 6070 } else { 6071 BUG_ON(writepos != sector_nr + reshape_sectors); 6072 stripe_addr = sector_nr; 6073 } 6074 6075 /* 'writepos' is the most advanced device address we might write. 6076 * 'readpos' is the least advanced device address we might read. 6077 * 'safepos' is the least address recorded in the metadata as having 6078 * been reshaped. 6079 * If there is a min_offset_diff, these are adjusted either by 6080 * increasing the safepos/readpos if diff is negative, or 6081 * increasing writepos if diff is positive. 6082 * If 'readpos' is then behind 'writepos', there is no way that we can 6083 * ensure safety in the face of a crash - that must be done by userspace 6084 * making a backup of the data. So in that case there is no particular 6085 * rush to update metadata. 6086 * Otherwise if 'safepos' is behind 'writepos', then we really need to 6087 * update the metadata to advance 'safepos' to match 'readpos' so that 6088 * we can be safe in the event of a crash. 6089 * So we insist on updating metadata if safepos is behind writepos and 6090 * readpos is beyond writepos. 6091 * In any case, update the metadata every 10 seconds. 6092 * Maybe that number should be configurable, but I'm not sure it is 6093 * worth it.... maybe it could be a multiple of safemode_delay??? 6094 */ 6095 if (conf->min_offset_diff < 0) { 6096 safepos += -conf->min_offset_diff; 6097 readpos += -conf->min_offset_diff; 6098 } else 6099 writepos += conf->min_offset_diff; 6100 6101 if ((mddev->reshape_backwards 6102 ? (safepos > writepos && readpos < writepos) 6103 : (safepos < writepos && readpos > writepos)) || 6104 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { 6105 /* Cannot proceed until we've updated the superblock... */ 6106 wait_event(conf->wait_for_overlap, 6107 atomic_read(&conf->reshape_stripes)==0 6108 || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); 6109 if (atomic_read(&conf->reshape_stripes) != 0) 6110 return 0; 6111 mddev->reshape_position = conf->reshape_progress; 6112 mddev->curr_resync_completed = sector_nr; 6113 if (!mddev->reshape_backwards) 6114 /* Can update recovery_offset */ 6115 rdev_for_each(rdev, mddev) 6116 if (rdev->raid_disk >= 0 && 6117 !test_bit(Journal, &rdev->flags) && 6118 !test_bit(In_sync, &rdev->flags) && 6119 rdev->recovery_offset < sector_nr) 6120 rdev->recovery_offset = sector_nr; 6121 6122 conf->reshape_checkpoint = jiffies; 6123 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 6124 md_wakeup_thread(mddev->thread); 6125 wait_event(mddev->sb_wait, mddev->sb_flags == 0 || 6126 test_bit(MD_RECOVERY_INTR, &mddev->recovery)); 6127 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 6128 return 0; 6129 spin_lock_irq(&conf->device_lock); 6130 conf->reshape_safe = mddev->reshape_position; 6131 spin_unlock_irq(&conf->device_lock); 6132 wake_up(&conf->wait_for_overlap); 6133 sysfs_notify_dirent_safe(mddev->sysfs_completed); 6134 } 6135 6136 INIT_LIST_HEAD(&stripes); 6137 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) { 6138 int j; 6139 int skipped_disk = 0; 6140 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); 6141 set_bit(STRIPE_EXPANDING, &sh->state); 6142 atomic_inc(&conf->reshape_stripes); 6143 /* If any of this stripe is beyond the end of the old 6144 * array, then we need to zero those blocks 6145 */ 6146 for (j=sh->disks; j--;) { 6147 sector_t s; 6148 if (j == sh->pd_idx) 6149 continue; 6150 if (conf->level == 6 && 6151 j == sh->qd_idx) 6152 continue; 6153 s = raid5_compute_blocknr(sh, j, 0); 6154 if (s < raid5_size(mddev, 0, 0)) { 6155 skipped_disk = 1; 6156 continue; 6157 } 6158 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf)); 6159 set_bit(R5_Expanded, &sh->dev[j].flags); 6160 set_bit(R5_UPTODATE, &sh->dev[j].flags); 6161 } 6162 if (!skipped_disk) { 6163 set_bit(STRIPE_EXPAND_READY, &sh->state); 6164 set_bit(STRIPE_HANDLE, &sh->state); 6165 } 6166 list_add(&sh->lru, &stripes); 6167 } 6168 spin_lock_irq(&conf->device_lock); 6169 if (mddev->reshape_backwards) 6170 conf->reshape_progress -= reshape_sectors * new_data_disks; 6171 else 6172 conf->reshape_progress += reshape_sectors * new_data_disks; 6173 spin_unlock_irq(&conf->device_lock); 6174 /* Ok, those stripe are ready. We can start scheduling 6175 * reads on the source stripes. 6176 * The source stripes are determined by mapping the first and last 6177 * block on the destination stripes. 6178 */ 6179 first_sector = 6180 raid5_compute_sector(conf, stripe_addr*(new_data_disks), 6181 1, &dd_idx, NULL); 6182 last_sector = 6183 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) 6184 * new_data_disks - 1), 6185 1, &dd_idx, NULL); 6186 if (last_sector >= mddev->dev_sectors) 6187 last_sector = mddev->dev_sectors - 1; 6188 while (first_sector <= last_sector) { 6189 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); 6190 set_bit(STRIPE_EXPAND_SOURCE, &sh->state); 6191 set_bit(STRIPE_HANDLE, &sh->state); 6192 raid5_release_stripe(sh); 6193 first_sector += RAID5_STRIPE_SECTORS(conf); 6194 } 6195 /* Now that the sources are clearly marked, we can release 6196 * the destination stripes 6197 */ 6198 while (!list_empty(&stripes)) { 6199 sh = list_entry(stripes.next, struct stripe_head, lru); 6200 list_del_init(&sh->lru); 6201 raid5_release_stripe(sh); 6202 } 6203 /* If this takes us to the resync_max point where we have to pause, 6204 * then we need to write out the superblock. 6205 */ 6206 sector_nr += reshape_sectors; 6207 retn = reshape_sectors; 6208 finish: 6209 if (mddev->curr_resync_completed > mddev->resync_max || 6210 (sector_nr - mddev->curr_resync_completed) * 2 6211 >= mddev->resync_max - mddev->curr_resync_completed) { 6212 /* Cannot proceed until we've updated the superblock... */ 6213 wait_event(conf->wait_for_overlap, 6214 atomic_read(&conf->reshape_stripes) == 0 6215 || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); 6216 if (atomic_read(&conf->reshape_stripes) != 0) 6217 goto ret; 6218 mddev->reshape_position = conf->reshape_progress; 6219 mddev->curr_resync_completed = sector_nr; 6220 if (!mddev->reshape_backwards) 6221 /* Can update recovery_offset */ 6222 rdev_for_each(rdev, mddev) 6223 if (rdev->raid_disk >= 0 && 6224 !test_bit(Journal, &rdev->flags) && 6225 !test_bit(In_sync, &rdev->flags) && 6226 rdev->recovery_offset < sector_nr) 6227 rdev->recovery_offset = sector_nr; 6228 conf->reshape_checkpoint = jiffies; 6229 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 6230 md_wakeup_thread(mddev->thread); 6231 wait_event(mddev->sb_wait, 6232 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) 6233 || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); 6234 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) 6235 goto ret; 6236 spin_lock_irq(&conf->device_lock); 6237 conf->reshape_safe = mddev->reshape_position; 6238 spin_unlock_irq(&conf->device_lock); 6239 wake_up(&conf->wait_for_overlap); 6240 sysfs_notify_dirent_safe(mddev->sysfs_completed); 6241 } 6242 ret: 6243 return retn; 6244 } 6245 6246 static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, 6247 int *skipped) 6248 { 6249 struct r5conf *conf = mddev->private; 6250 struct stripe_head *sh; 6251 sector_t max_sector = mddev->dev_sectors; 6252 sector_t sync_blocks; 6253 int still_degraded = 0; 6254 int i; 6255 6256 if (sector_nr >= max_sector) { 6257 /* just being told to finish up .. nothing much to do */ 6258 6259 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { 6260 end_reshape(conf); 6261 return 0; 6262 } 6263 6264 if (mddev->curr_resync < max_sector) /* aborted */ 6265 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync, 6266 &sync_blocks, 1); 6267 else /* completed sync */ 6268 conf->fullsync = 0; 6269 md_bitmap_close_sync(mddev->bitmap); 6270 6271 return 0; 6272 } 6273 6274 /* Allow raid5_quiesce to complete */ 6275 wait_event(conf->wait_for_overlap, conf->quiesce != 2); 6276 6277 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 6278 return reshape_request(mddev, sector_nr, skipped); 6279 6280 /* No need to check resync_max as we never do more than one 6281 * stripe, and as resync_max will always be on a chunk boundary, 6282 * if the check in md_do_sync didn't fire, there is no chance 6283 * of overstepping resync_max here 6284 */ 6285 6286 /* if there is too many failed drives and we are trying 6287 * to resync, then assert that we are finished, because there is 6288 * nothing we can do. 6289 */ 6290 if (mddev->degraded >= conf->max_degraded && 6291 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 6292 sector_t rv = mddev->dev_sectors - sector_nr; 6293 *skipped = 1; 6294 return rv; 6295 } 6296 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 6297 !conf->fullsync && 6298 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && 6299 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) { 6300 /* we can skip this block, and probably more */ 6301 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf)); 6302 *skipped = 1; 6303 /* keep things rounded to whole stripes */ 6304 return sync_blocks * RAID5_STRIPE_SECTORS(conf); 6305 } 6306 6307 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); 6308 6309 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); 6310 if (sh == NULL) { 6311 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); 6312 /* make sure we don't swamp the stripe cache if someone else 6313 * is trying to get access 6314 */ 6315 schedule_timeout_uninterruptible(1); 6316 } 6317 /* Need to check if array will still be degraded after recovery/resync 6318 * Note in case of > 1 drive failures it's possible we're rebuilding 6319 * one drive while leaving another faulty drive in array. 6320 */ 6321 rcu_read_lock(); 6322 for (i = 0; i < conf->raid_disks; i++) { 6323 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 6324 6325 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) 6326 still_degraded = 1; 6327 } 6328 rcu_read_unlock(); 6329 6330 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); 6331 6332 set_bit(STRIPE_SYNC_REQUESTED, &sh->state); 6333 set_bit(STRIPE_HANDLE, &sh->state); 6334 6335 raid5_release_stripe(sh); 6336 6337 return RAID5_STRIPE_SECTORS(conf); 6338 } 6339 6340 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio, 6341 unsigned int offset) 6342 { 6343 /* We may not be able to submit a whole bio at once as there 6344 * may not be enough stripe_heads available. 6345 * We cannot pre-allocate enough stripe_heads as we may need 6346 * more than exist in the cache (if we allow ever large chunks). 6347 * So we do one stripe head at a time and record in 6348 * ->bi_hw_segments how many have been done. 6349 * 6350 * We *know* that this entire raid_bio is in one chunk, so 6351 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. 6352 */ 6353 struct stripe_head *sh; 6354 int dd_idx; 6355 sector_t sector, logical_sector, last_sector; 6356 int scnt = 0; 6357 int handled = 0; 6358 6359 logical_sector = raid_bio->bi_iter.bi_sector & 6360 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); 6361 sector = raid5_compute_sector(conf, logical_sector, 6362 0, &dd_idx, NULL); 6363 last_sector = bio_end_sector(raid_bio); 6364 6365 for (; logical_sector < last_sector; 6366 logical_sector += RAID5_STRIPE_SECTORS(conf), 6367 sector += RAID5_STRIPE_SECTORS(conf), 6368 scnt++) { 6369 6370 if (scnt < offset) 6371 /* already done this stripe */ 6372 continue; 6373 6374 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); 6375 6376 if (!sh) { 6377 /* failed to get a stripe - must wait */ 6378 conf->retry_read_aligned = raid_bio; 6379 conf->retry_read_offset = scnt; 6380 return handled; 6381 } 6382 6383 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { 6384 raid5_release_stripe(sh); 6385 conf->retry_read_aligned = raid_bio; 6386 conf->retry_read_offset = scnt; 6387 return handled; 6388 } 6389 6390 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); 6391 handle_stripe(sh); 6392 raid5_release_stripe(sh); 6393 handled++; 6394 } 6395 6396 bio_endio(raid_bio); 6397 6398 if (atomic_dec_and_test(&conf->active_aligned_reads)) 6399 wake_up(&conf->wait_for_quiescent); 6400 return handled; 6401 } 6402 6403 static int handle_active_stripes(struct r5conf *conf, int group, 6404 struct r5worker *worker, 6405 struct list_head *temp_inactive_list) 6406 __must_hold(&conf->device_lock) 6407 { 6408 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; 6409 int i, batch_size = 0, hash; 6410 bool release_inactive = false; 6411 6412 while (batch_size < MAX_STRIPE_BATCH && 6413 (sh = __get_priority_stripe(conf, group)) != NULL) 6414 batch[batch_size++] = sh; 6415 6416 if (batch_size == 0) { 6417 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) 6418 if (!list_empty(temp_inactive_list + i)) 6419 break; 6420 if (i == NR_STRIPE_HASH_LOCKS) { 6421 spin_unlock_irq(&conf->device_lock); 6422 log_flush_stripe_to_raid(conf); 6423 spin_lock_irq(&conf->device_lock); 6424 return batch_size; 6425 } 6426 release_inactive = true; 6427 } 6428 spin_unlock_irq(&conf->device_lock); 6429 6430 release_inactive_stripe_list(conf, temp_inactive_list, 6431 NR_STRIPE_HASH_LOCKS); 6432 6433 r5l_flush_stripe_to_raid(conf->log); 6434 if (release_inactive) { 6435 spin_lock_irq(&conf->device_lock); 6436 return 0; 6437 } 6438 6439 for (i = 0; i < batch_size; i++) 6440 handle_stripe(batch[i]); 6441 log_write_stripe_run(conf); 6442 6443 cond_resched(); 6444 6445 spin_lock_irq(&conf->device_lock); 6446 for (i = 0; i < batch_size; i++) { 6447 hash = batch[i]->hash_lock_index; 6448 __release_stripe(conf, batch[i], &temp_inactive_list[hash]); 6449 } 6450 return batch_size; 6451 } 6452 6453 static void raid5_do_work(struct work_struct *work) 6454 { 6455 struct r5worker *worker = container_of(work, struct r5worker, work); 6456 struct r5worker_group *group = worker->group; 6457 struct r5conf *conf = group->conf; 6458 struct mddev *mddev = conf->mddev; 6459 int group_id = group - conf->worker_groups; 6460 int handled; 6461 struct blk_plug plug; 6462 6463 pr_debug("+++ raid5worker active\n"); 6464 6465 blk_start_plug(&plug); 6466 handled = 0; 6467 spin_lock_irq(&conf->device_lock); 6468 while (1) { 6469 int batch_size, released; 6470 6471 released = release_stripe_list(conf, worker->temp_inactive_list); 6472 6473 batch_size = handle_active_stripes(conf, group_id, worker, 6474 worker->temp_inactive_list); 6475 worker->working = false; 6476 if (!batch_size && !released) 6477 break; 6478 handled += batch_size; 6479 wait_event_lock_irq(mddev->sb_wait, 6480 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags), 6481 conf->device_lock); 6482 } 6483 pr_debug("%d stripes handled\n", handled); 6484 6485 spin_unlock_irq(&conf->device_lock); 6486 6487 flush_deferred_bios(conf); 6488 6489 r5l_flush_stripe_to_raid(conf->log); 6490 6491 async_tx_issue_pending_all(); 6492 blk_finish_plug(&plug); 6493 6494 pr_debug("--- raid5worker inactive\n"); 6495 } 6496 6497 /* 6498 * This is our raid5 kernel thread. 6499 * 6500 * We scan the hash table for stripes which can be handled now. 6501 * During the scan, completed stripes are saved for us by the interrupt 6502 * handler, so that they will not have to wait for our next wakeup. 6503 */ 6504 static void raid5d(struct md_thread *thread) 6505 { 6506 struct mddev *mddev = thread->mddev; 6507 struct r5conf *conf = mddev->private; 6508 int handled; 6509 struct blk_plug plug; 6510 6511 pr_debug("+++ raid5d active\n"); 6512 6513 md_check_recovery(mddev); 6514 6515 blk_start_plug(&plug); 6516 handled = 0; 6517 spin_lock_irq(&conf->device_lock); 6518 while (1) { 6519 struct bio *bio; 6520 int batch_size, released; 6521 unsigned int offset; 6522 6523 released = release_stripe_list(conf, conf->temp_inactive_list); 6524 if (released) 6525 clear_bit(R5_DID_ALLOC, &conf->cache_state); 6526 6527 if ( 6528 !list_empty(&conf->bitmap_list)) { 6529 /* Now is a good time to flush some bitmap updates */ 6530 conf->seq_flush++; 6531 spin_unlock_irq(&conf->device_lock); 6532 md_bitmap_unplug(mddev->bitmap); 6533 spin_lock_irq(&conf->device_lock); 6534 conf->seq_write = conf->seq_flush; 6535 activate_bit_delay(conf, conf->temp_inactive_list); 6536 } 6537 raid5_activate_delayed(conf); 6538 6539 while ((bio = remove_bio_from_retry(conf, &offset))) { 6540 int ok; 6541 spin_unlock_irq(&conf->device_lock); 6542 ok = retry_aligned_read(conf, bio, offset); 6543 spin_lock_irq(&conf->device_lock); 6544 if (!ok) 6545 break; 6546 handled++; 6547 } 6548 6549 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, 6550 conf->temp_inactive_list); 6551 if (!batch_size && !released) 6552 break; 6553 handled += batch_size; 6554 6555 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { 6556 spin_unlock_irq(&conf->device_lock); 6557 md_check_recovery(mddev); 6558 spin_lock_irq(&conf->device_lock); 6559 } 6560 } 6561 pr_debug("%d stripes handled\n", handled); 6562 6563 spin_unlock_irq(&conf->device_lock); 6564 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && 6565 mutex_trylock(&conf->cache_size_mutex)) { 6566 grow_one_stripe(conf, __GFP_NOWARN); 6567 /* Set flag even if allocation failed. This helps 6568 * slow down allocation requests when mem is short 6569 */ 6570 set_bit(R5_DID_ALLOC, &conf->cache_state); 6571 mutex_unlock(&conf->cache_size_mutex); 6572 } 6573 6574 flush_deferred_bios(conf); 6575 6576 r5l_flush_stripe_to_raid(conf->log); 6577 6578 async_tx_issue_pending_all(); 6579 blk_finish_plug(&plug); 6580 6581 pr_debug("--- raid5d inactive\n"); 6582 } 6583 6584 static ssize_t 6585 raid5_show_stripe_cache_size(struct mddev *mddev, char *page) 6586 { 6587 struct r5conf *conf; 6588 int ret = 0; 6589 spin_lock(&mddev->lock); 6590 conf = mddev->private; 6591 if (conf) 6592 ret = sprintf(page, "%d\n", conf->min_nr_stripes); 6593 spin_unlock(&mddev->lock); 6594 return ret; 6595 } 6596 6597 int 6598 raid5_set_cache_size(struct mddev *mddev, int size) 6599 { 6600 int result = 0; 6601 struct r5conf *conf = mddev->private; 6602 6603 if (size <= 16 || size > 32768) 6604 return -EINVAL; 6605 6606 conf->min_nr_stripes = size; 6607 mutex_lock(&conf->cache_size_mutex); 6608 while (size < conf->max_nr_stripes && 6609 drop_one_stripe(conf)) 6610 ; 6611 mutex_unlock(&conf->cache_size_mutex); 6612 6613 md_allow_write(mddev); 6614 6615 mutex_lock(&conf->cache_size_mutex); 6616 while (size > conf->max_nr_stripes) 6617 if (!grow_one_stripe(conf, GFP_KERNEL)) { 6618 conf->min_nr_stripes = conf->max_nr_stripes; 6619 result = -ENOMEM; 6620 break; 6621 } 6622 mutex_unlock(&conf->cache_size_mutex); 6623 6624 return result; 6625 } 6626 EXPORT_SYMBOL(raid5_set_cache_size); 6627 6628 static ssize_t 6629 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) 6630 { 6631 struct r5conf *conf; 6632 unsigned long new; 6633 int err; 6634 6635 if (len >= PAGE_SIZE) 6636 return -EINVAL; 6637 if (kstrtoul(page, 10, &new)) 6638 return -EINVAL; 6639 err = mddev_lock(mddev); 6640 if (err) 6641 return err; 6642 conf = mddev->private; 6643 if (!conf) 6644 err = -ENODEV; 6645 else 6646 err = raid5_set_cache_size(mddev, new); 6647 mddev_unlock(mddev); 6648 6649 return err ?: len; 6650 } 6651 6652 static struct md_sysfs_entry 6653 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, 6654 raid5_show_stripe_cache_size, 6655 raid5_store_stripe_cache_size); 6656 6657 static ssize_t 6658 raid5_show_rmw_level(struct mddev *mddev, char *page) 6659 { 6660 struct r5conf *conf = mddev->private; 6661 if (conf) 6662 return sprintf(page, "%d\n", conf->rmw_level); 6663 else 6664 return 0; 6665 } 6666 6667 static ssize_t 6668 raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) 6669 { 6670 struct r5conf *conf = mddev->private; 6671 unsigned long new; 6672 6673 if (!conf) 6674 return -ENODEV; 6675 6676 if (len >= PAGE_SIZE) 6677 return -EINVAL; 6678 6679 if (kstrtoul(page, 10, &new)) 6680 return -EINVAL; 6681 6682 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) 6683 return -EINVAL; 6684 6685 if (new != PARITY_DISABLE_RMW && 6686 new != PARITY_ENABLE_RMW && 6687 new != PARITY_PREFER_RMW) 6688 return -EINVAL; 6689 6690 conf->rmw_level = new; 6691 return len; 6692 } 6693 6694 static struct md_sysfs_entry 6695 raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, 6696 raid5_show_rmw_level, 6697 raid5_store_rmw_level); 6698 6699 static ssize_t 6700 raid5_show_stripe_size(struct mddev *mddev, char *page) 6701 { 6702 struct r5conf *conf; 6703 int ret = 0; 6704 6705 spin_lock(&mddev->lock); 6706 conf = mddev->private; 6707 if (conf) 6708 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf)); 6709 spin_unlock(&mddev->lock); 6710 return ret; 6711 } 6712 6713 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 6714 static ssize_t 6715 raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len) 6716 { 6717 struct r5conf *conf; 6718 unsigned long new; 6719 int err; 6720 int size; 6721 6722 if (len >= PAGE_SIZE) 6723 return -EINVAL; 6724 if (kstrtoul(page, 10, &new)) 6725 return -EINVAL; 6726 6727 /* 6728 * The value should not be bigger than PAGE_SIZE. It requires to 6729 * be multiple of DEFAULT_STRIPE_SIZE and the value should be power 6730 * of two. 6731 */ 6732 if (new % DEFAULT_STRIPE_SIZE != 0 || 6733 new > PAGE_SIZE || new == 0 || 6734 new != roundup_pow_of_two(new)) 6735 return -EINVAL; 6736 6737 err = mddev_lock(mddev); 6738 if (err) 6739 return err; 6740 6741 conf = mddev->private; 6742 if (!conf) { 6743 err = -ENODEV; 6744 goto out_unlock; 6745 } 6746 6747 if (new == conf->stripe_size) 6748 goto out_unlock; 6749 6750 pr_debug("md/raid: change stripe_size from %lu to %lu\n", 6751 conf->stripe_size, new); 6752 6753 if (mddev->sync_thread || 6754 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || 6755 mddev->reshape_position != MaxSector || 6756 mddev->sysfs_active) { 6757 err = -EBUSY; 6758 goto out_unlock; 6759 } 6760 6761 mddev_suspend(mddev); 6762 mutex_lock(&conf->cache_size_mutex); 6763 size = conf->max_nr_stripes; 6764 6765 shrink_stripes(conf); 6766 6767 conf->stripe_size = new; 6768 conf->stripe_shift = ilog2(new) - 9; 6769 conf->stripe_sectors = new >> 9; 6770 if (grow_stripes(conf, size)) { 6771 pr_warn("md/raid:%s: couldn't allocate buffers\n", 6772 mdname(mddev)); 6773 err = -ENOMEM; 6774 } 6775 mutex_unlock(&conf->cache_size_mutex); 6776 mddev_resume(mddev); 6777 6778 out_unlock: 6779 mddev_unlock(mddev); 6780 return err ?: len; 6781 } 6782 6783 static struct md_sysfs_entry 6784 raid5_stripe_size = __ATTR(stripe_size, 0644, 6785 raid5_show_stripe_size, 6786 raid5_store_stripe_size); 6787 #else 6788 static struct md_sysfs_entry 6789 raid5_stripe_size = __ATTR(stripe_size, 0444, 6790 raid5_show_stripe_size, 6791 NULL); 6792 #endif 6793 6794 static ssize_t 6795 raid5_show_preread_threshold(struct mddev *mddev, char *page) 6796 { 6797 struct r5conf *conf; 6798 int ret = 0; 6799 spin_lock(&mddev->lock); 6800 conf = mddev->private; 6801 if (conf) 6802 ret = sprintf(page, "%d\n", conf->bypass_threshold); 6803 spin_unlock(&mddev->lock); 6804 return ret; 6805 } 6806 6807 static ssize_t 6808 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) 6809 { 6810 struct r5conf *conf; 6811 unsigned long new; 6812 int err; 6813 6814 if (len >= PAGE_SIZE) 6815 return -EINVAL; 6816 if (kstrtoul(page, 10, &new)) 6817 return -EINVAL; 6818 6819 err = mddev_lock(mddev); 6820 if (err) 6821 return err; 6822 conf = mddev->private; 6823 if (!conf) 6824 err = -ENODEV; 6825 else if (new > conf->min_nr_stripes) 6826 err = -EINVAL; 6827 else 6828 conf->bypass_threshold = new; 6829 mddev_unlock(mddev); 6830 return err ?: len; 6831 } 6832 6833 static struct md_sysfs_entry 6834 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, 6835 S_IRUGO | S_IWUSR, 6836 raid5_show_preread_threshold, 6837 raid5_store_preread_threshold); 6838 6839 static ssize_t 6840 raid5_show_skip_copy(struct mddev *mddev, char *page) 6841 { 6842 struct r5conf *conf; 6843 int ret = 0; 6844 spin_lock(&mddev->lock); 6845 conf = mddev->private; 6846 if (conf) 6847 ret = sprintf(page, "%d\n", conf->skip_copy); 6848 spin_unlock(&mddev->lock); 6849 return ret; 6850 } 6851 6852 static ssize_t 6853 raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) 6854 { 6855 struct r5conf *conf; 6856 unsigned long new; 6857 int err; 6858 6859 if (len >= PAGE_SIZE) 6860 return -EINVAL; 6861 if (kstrtoul(page, 10, &new)) 6862 return -EINVAL; 6863 new = !!new; 6864 6865 err = mddev_lock(mddev); 6866 if (err) 6867 return err; 6868 conf = mddev->private; 6869 if (!conf) 6870 err = -ENODEV; 6871 else if (new != conf->skip_copy) { 6872 struct request_queue *q = mddev->queue; 6873 6874 mddev_suspend(mddev); 6875 conf->skip_copy = new; 6876 if (new) 6877 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q); 6878 else 6879 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q); 6880 mddev_resume(mddev); 6881 } 6882 mddev_unlock(mddev); 6883 return err ?: len; 6884 } 6885 6886 static struct md_sysfs_entry 6887 raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, 6888 raid5_show_skip_copy, 6889 raid5_store_skip_copy); 6890 6891 static ssize_t 6892 stripe_cache_active_show(struct mddev *mddev, char *page) 6893 { 6894 struct r5conf *conf = mddev->private; 6895 if (conf) 6896 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); 6897 else 6898 return 0; 6899 } 6900 6901 static struct md_sysfs_entry 6902 raid5_stripecache_active = __ATTR_RO(stripe_cache_active); 6903 6904 static ssize_t 6905 raid5_show_group_thread_cnt(struct mddev *mddev, char *page) 6906 { 6907 struct r5conf *conf; 6908 int ret = 0; 6909 spin_lock(&mddev->lock); 6910 conf = mddev->private; 6911 if (conf) 6912 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); 6913 spin_unlock(&mddev->lock); 6914 return ret; 6915 } 6916 6917 static int alloc_thread_groups(struct r5conf *conf, int cnt, 6918 int *group_cnt, 6919 struct r5worker_group **worker_groups); 6920 static ssize_t 6921 raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) 6922 { 6923 struct r5conf *conf; 6924 unsigned int new; 6925 int err; 6926 struct r5worker_group *new_groups, *old_groups; 6927 int group_cnt; 6928 6929 if (len >= PAGE_SIZE) 6930 return -EINVAL; 6931 if (kstrtouint(page, 10, &new)) 6932 return -EINVAL; 6933 /* 8192 should be big enough */ 6934 if (new > 8192) 6935 return -EINVAL; 6936 6937 err = mddev_lock(mddev); 6938 if (err) 6939 return err; 6940 conf = mddev->private; 6941 if (!conf) 6942 err = -ENODEV; 6943 else if (new != conf->worker_cnt_per_group) { 6944 mddev_suspend(mddev); 6945 6946 old_groups = conf->worker_groups; 6947 if (old_groups) 6948 flush_workqueue(raid5_wq); 6949 6950 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups); 6951 if (!err) { 6952 spin_lock_irq(&conf->device_lock); 6953 conf->group_cnt = group_cnt; 6954 conf->worker_cnt_per_group = new; 6955 conf->worker_groups = new_groups; 6956 spin_unlock_irq(&conf->device_lock); 6957 6958 if (old_groups) 6959 kfree(old_groups[0].workers); 6960 kfree(old_groups); 6961 } 6962 mddev_resume(mddev); 6963 } 6964 mddev_unlock(mddev); 6965 6966 return err ?: len; 6967 } 6968 6969 static struct md_sysfs_entry 6970 raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, 6971 raid5_show_group_thread_cnt, 6972 raid5_store_group_thread_cnt); 6973 6974 static struct attribute *raid5_attrs[] = { 6975 &raid5_stripecache_size.attr, 6976 &raid5_stripecache_active.attr, 6977 &raid5_preread_bypass_threshold.attr, 6978 &raid5_group_thread_cnt.attr, 6979 &raid5_skip_copy.attr, 6980 &raid5_rmw_level.attr, 6981 &raid5_stripe_size.attr, 6982 &r5c_journal_mode.attr, 6983 &ppl_write_hint.attr, 6984 NULL, 6985 }; 6986 static const struct attribute_group raid5_attrs_group = { 6987 .name = NULL, 6988 .attrs = raid5_attrs, 6989 }; 6990 6991 static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt, 6992 struct r5worker_group **worker_groups) 6993 { 6994 int i, j, k; 6995 ssize_t size; 6996 struct r5worker *workers; 6997 6998 if (cnt == 0) { 6999 *group_cnt = 0; 7000 *worker_groups = NULL; 7001 return 0; 7002 } 7003 *group_cnt = num_possible_nodes(); 7004 size = sizeof(struct r5worker) * cnt; 7005 workers = kcalloc(size, *group_cnt, GFP_NOIO); 7006 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group), 7007 GFP_NOIO); 7008 if (!*worker_groups || !workers) { 7009 kfree(workers); 7010 kfree(*worker_groups); 7011 return -ENOMEM; 7012 } 7013 7014 for (i = 0; i < *group_cnt; i++) { 7015 struct r5worker_group *group; 7016 7017 group = &(*worker_groups)[i]; 7018 INIT_LIST_HEAD(&group->handle_list); 7019 INIT_LIST_HEAD(&group->loprio_list); 7020 group->conf = conf; 7021 group->workers = workers + i * cnt; 7022 7023 for (j = 0; j < cnt; j++) { 7024 struct r5worker *worker = group->workers + j; 7025 worker->group = group; 7026 INIT_WORK(&worker->work, raid5_do_work); 7027 7028 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) 7029 INIT_LIST_HEAD(worker->temp_inactive_list + k); 7030 } 7031 } 7032 7033 return 0; 7034 } 7035 7036 static void free_thread_groups(struct r5conf *conf) 7037 { 7038 if (conf->worker_groups) 7039 kfree(conf->worker_groups[0].workers); 7040 kfree(conf->worker_groups); 7041 conf->worker_groups = NULL; 7042 } 7043 7044 static sector_t 7045 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) 7046 { 7047 struct r5conf *conf = mddev->private; 7048 7049 if (!sectors) 7050 sectors = mddev->dev_sectors; 7051 if (!raid_disks) 7052 /* size is defined by the smallest of previous and new size */ 7053 raid_disks = min(conf->raid_disks, conf->previous_raid_disks); 7054 7055 sectors &= ~((sector_t)conf->chunk_sectors - 1); 7056 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); 7057 return sectors * (raid_disks - conf->max_degraded); 7058 } 7059 7060 static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) 7061 { 7062 safe_put_page(percpu->spare_page); 7063 percpu->spare_page = NULL; 7064 kvfree(percpu->scribble); 7065 percpu->scribble = NULL; 7066 } 7067 7068 static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) 7069 { 7070 if (conf->level == 6 && !percpu->spare_page) { 7071 percpu->spare_page = alloc_page(GFP_KERNEL); 7072 if (!percpu->spare_page) 7073 return -ENOMEM; 7074 } 7075 7076 if (scribble_alloc(percpu, 7077 max(conf->raid_disks, 7078 conf->previous_raid_disks), 7079 max(conf->chunk_sectors, 7080 conf->prev_chunk_sectors) 7081 / RAID5_STRIPE_SECTORS(conf))) { 7082 free_scratch_buffer(conf, percpu); 7083 return -ENOMEM; 7084 } 7085 7086 local_lock_init(&percpu->lock); 7087 return 0; 7088 } 7089 7090 static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) 7091 { 7092 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); 7093 7094 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); 7095 return 0; 7096 } 7097 7098 static void raid5_free_percpu(struct r5conf *conf) 7099 { 7100 if (!conf->percpu) 7101 return; 7102 7103 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); 7104 free_percpu(conf->percpu); 7105 } 7106 7107 static void free_conf(struct r5conf *conf) 7108 { 7109 int i; 7110 7111 log_exit(conf); 7112 7113 unregister_shrinker(&conf->shrinker); 7114 free_thread_groups(conf); 7115 shrink_stripes(conf); 7116 raid5_free_percpu(conf); 7117 for (i = 0; i < conf->pool_size; i++) 7118 if (conf->disks[i].extra_page) 7119 put_page(conf->disks[i].extra_page); 7120 kfree(conf->disks); 7121 bioset_exit(&conf->bio_split); 7122 kfree(conf->stripe_hashtbl); 7123 kfree(conf->pending_data); 7124 kfree(conf); 7125 } 7126 7127 static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) 7128 { 7129 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); 7130 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); 7131 7132 if (alloc_scratch_buffer(conf, percpu)) { 7133 pr_warn("%s: failed memory allocation for cpu%u\n", 7134 __func__, cpu); 7135 return -ENOMEM; 7136 } 7137 return 0; 7138 } 7139 7140 static int raid5_alloc_percpu(struct r5conf *conf) 7141 { 7142 int err = 0; 7143 7144 conf->percpu = alloc_percpu(struct raid5_percpu); 7145 if (!conf->percpu) 7146 return -ENOMEM; 7147 7148 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); 7149 if (!err) { 7150 conf->scribble_disks = max(conf->raid_disks, 7151 conf->previous_raid_disks); 7152 conf->scribble_sectors = max(conf->chunk_sectors, 7153 conf->prev_chunk_sectors); 7154 } 7155 return err; 7156 } 7157 7158 static unsigned long raid5_cache_scan(struct shrinker *shrink, 7159 struct shrink_control *sc) 7160 { 7161 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); 7162 unsigned long ret = SHRINK_STOP; 7163 7164 if (mutex_trylock(&conf->cache_size_mutex)) { 7165 ret= 0; 7166 while (ret < sc->nr_to_scan && 7167 conf->max_nr_stripes > conf->min_nr_stripes) { 7168 if (drop_one_stripe(conf) == 0) { 7169 ret = SHRINK_STOP; 7170 break; 7171 } 7172 ret++; 7173 } 7174 mutex_unlock(&conf->cache_size_mutex); 7175 } 7176 return ret; 7177 } 7178 7179 static unsigned long raid5_cache_count(struct shrinker *shrink, 7180 struct shrink_control *sc) 7181 { 7182 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); 7183 7184 if (conf->max_nr_stripes < conf->min_nr_stripes) 7185 /* unlikely, but not impossible */ 7186 return 0; 7187 return conf->max_nr_stripes - conf->min_nr_stripes; 7188 } 7189 7190 static struct r5conf *setup_conf(struct mddev *mddev) 7191 { 7192 struct r5conf *conf; 7193 int raid_disk, memory, max_disks; 7194 struct md_rdev *rdev; 7195 struct disk_info *disk; 7196 char pers_name[6]; 7197 int i; 7198 int group_cnt; 7199 struct r5worker_group *new_group; 7200 int ret = -ENOMEM; 7201 7202 if (mddev->new_level != 5 7203 && mddev->new_level != 4 7204 && mddev->new_level != 6) { 7205 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", 7206 mdname(mddev), mddev->new_level); 7207 return ERR_PTR(-EIO); 7208 } 7209 if ((mddev->new_level == 5 7210 && !algorithm_valid_raid5(mddev->new_layout)) || 7211 (mddev->new_level == 6 7212 && !algorithm_valid_raid6(mddev->new_layout))) { 7213 pr_warn("md/raid:%s: layout %d not supported\n", 7214 mdname(mddev), mddev->new_layout); 7215 return ERR_PTR(-EIO); 7216 } 7217 if (mddev->new_level == 6 && mddev->raid_disks < 4) { 7218 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", 7219 mdname(mddev), mddev->raid_disks); 7220 return ERR_PTR(-EINVAL); 7221 } 7222 7223 if (!mddev->new_chunk_sectors || 7224 (mddev->new_chunk_sectors << 9) % PAGE_SIZE || 7225 !is_power_of_2(mddev->new_chunk_sectors)) { 7226 pr_warn("md/raid:%s: invalid chunk size %d\n", 7227 mdname(mddev), mddev->new_chunk_sectors << 9); 7228 return ERR_PTR(-EINVAL); 7229 } 7230 7231 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); 7232 if (conf == NULL) 7233 goto abort; 7234 7235 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE 7236 conf->stripe_size = DEFAULT_STRIPE_SIZE; 7237 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9; 7238 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9; 7239 #endif 7240 INIT_LIST_HEAD(&conf->free_list); 7241 INIT_LIST_HEAD(&conf->pending_list); 7242 conf->pending_data = kcalloc(PENDING_IO_MAX, 7243 sizeof(struct r5pending_data), 7244 GFP_KERNEL); 7245 if (!conf->pending_data) 7246 goto abort; 7247 for (i = 0; i < PENDING_IO_MAX; i++) 7248 list_add(&conf->pending_data[i].sibling, &conf->free_list); 7249 /* Don't enable multi-threading by default*/ 7250 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) { 7251 conf->group_cnt = group_cnt; 7252 conf->worker_cnt_per_group = 0; 7253 conf->worker_groups = new_group; 7254 } else 7255 goto abort; 7256 spin_lock_init(&conf->device_lock); 7257 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock); 7258 mutex_init(&conf->cache_size_mutex); 7259 7260 init_waitqueue_head(&conf->wait_for_quiescent); 7261 init_waitqueue_head(&conf->wait_for_stripe); 7262 init_waitqueue_head(&conf->wait_for_overlap); 7263 INIT_LIST_HEAD(&conf->handle_list); 7264 INIT_LIST_HEAD(&conf->loprio_list); 7265 INIT_LIST_HEAD(&conf->hold_list); 7266 INIT_LIST_HEAD(&conf->delayed_list); 7267 INIT_LIST_HEAD(&conf->bitmap_list); 7268 init_llist_head(&conf->released_stripes); 7269 atomic_set(&conf->active_stripes, 0); 7270 atomic_set(&conf->preread_active_stripes, 0); 7271 atomic_set(&conf->active_aligned_reads, 0); 7272 spin_lock_init(&conf->pending_bios_lock); 7273 conf->batch_bio_dispatch = true; 7274 rdev_for_each(rdev, mddev) { 7275 if (test_bit(Journal, &rdev->flags)) 7276 continue; 7277 if (bdev_nonrot(rdev->bdev)) { 7278 conf->batch_bio_dispatch = false; 7279 break; 7280 } 7281 } 7282 7283 conf->bypass_threshold = BYPASS_THRESHOLD; 7284 conf->recovery_disabled = mddev->recovery_disabled - 1; 7285 7286 conf->raid_disks = mddev->raid_disks; 7287 if (mddev->reshape_position == MaxSector) 7288 conf->previous_raid_disks = mddev->raid_disks; 7289 else 7290 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; 7291 max_disks = max(conf->raid_disks, conf->previous_raid_disks); 7292 7293 conf->disks = kcalloc(max_disks, sizeof(struct disk_info), 7294 GFP_KERNEL); 7295 7296 if (!conf->disks) 7297 goto abort; 7298 7299 for (i = 0; i < max_disks; i++) { 7300 conf->disks[i].extra_page = alloc_page(GFP_KERNEL); 7301 if (!conf->disks[i].extra_page) 7302 goto abort; 7303 } 7304 7305 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); 7306 if (ret) 7307 goto abort; 7308 conf->mddev = mddev; 7309 7310 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) 7311 goto abort; 7312 7313 /* We init hash_locks[0] separately to that it can be used 7314 * as the reference lock in the spin_lock_nest_lock() call 7315 * in lock_all_device_hash_locks_irq in order to convince 7316 * lockdep that we know what we are doing. 7317 */ 7318 spin_lock_init(conf->hash_locks); 7319 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) 7320 spin_lock_init(conf->hash_locks + i); 7321 7322 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) 7323 INIT_LIST_HEAD(conf->inactive_list + i); 7324 7325 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) 7326 INIT_LIST_HEAD(conf->temp_inactive_list + i); 7327 7328 atomic_set(&conf->r5c_cached_full_stripes, 0); 7329 INIT_LIST_HEAD(&conf->r5c_full_stripe_list); 7330 atomic_set(&conf->r5c_cached_partial_stripes, 0); 7331 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); 7332 atomic_set(&conf->r5c_flushing_full_stripes, 0); 7333 atomic_set(&conf->r5c_flushing_partial_stripes, 0); 7334 7335 conf->level = mddev->new_level; 7336 conf->chunk_sectors = mddev->new_chunk_sectors; 7337 ret = raid5_alloc_percpu(conf); 7338 if (ret) 7339 goto abort; 7340 7341 pr_debug("raid456: run(%s) called.\n", mdname(mddev)); 7342 7343 ret = -EIO; 7344 rdev_for_each(rdev, mddev) { 7345 raid_disk = rdev->raid_disk; 7346 if (raid_disk >= max_disks 7347 || raid_disk < 0 || test_bit(Journal, &rdev->flags)) 7348 continue; 7349 disk = conf->disks + raid_disk; 7350 7351 if (test_bit(Replacement, &rdev->flags)) { 7352 if (disk->replacement) 7353 goto abort; 7354 RCU_INIT_POINTER(disk->replacement, rdev); 7355 } else { 7356 if (disk->rdev) 7357 goto abort; 7358 RCU_INIT_POINTER(disk->rdev, rdev); 7359 } 7360 7361 if (test_bit(In_sync, &rdev->flags)) { 7362 char b[BDEVNAME_SIZE]; 7363 pr_info("md/raid:%s: device %s operational as raid disk %d\n", 7364 mdname(mddev), bdevname(rdev->bdev, b), raid_disk); 7365 } else if (rdev->saved_raid_disk != raid_disk) 7366 /* Cannot rely on bitmap to complete recovery */ 7367 conf->fullsync = 1; 7368 } 7369 7370 conf->level = mddev->new_level; 7371 if (conf->level == 6) { 7372 conf->max_degraded = 2; 7373 if (raid6_call.xor_syndrome) 7374 conf->rmw_level = PARITY_ENABLE_RMW; 7375 else 7376 conf->rmw_level = PARITY_DISABLE_RMW; 7377 } else { 7378 conf->max_degraded = 1; 7379 conf->rmw_level = PARITY_ENABLE_RMW; 7380 } 7381 conf->algorithm = mddev->new_layout; 7382 conf->reshape_progress = mddev->reshape_position; 7383 if (conf->reshape_progress != MaxSector) { 7384 conf->prev_chunk_sectors = mddev->chunk_sectors; 7385 conf->prev_algo = mddev->layout; 7386 } else { 7387 conf->prev_chunk_sectors = conf->chunk_sectors; 7388 conf->prev_algo = conf->algorithm; 7389 } 7390 7391 conf->min_nr_stripes = NR_STRIPES; 7392 if (mddev->reshape_position != MaxSector) { 7393 int stripes = max_t(int, 7394 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4, 7395 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4); 7396 conf->min_nr_stripes = max(NR_STRIPES, stripes); 7397 if (conf->min_nr_stripes != NR_STRIPES) 7398 pr_info("md/raid:%s: force stripe size %d for reshape\n", 7399 mdname(mddev), conf->min_nr_stripes); 7400 } 7401 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + 7402 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; 7403 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); 7404 if (grow_stripes(conf, conf->min_nr_stripes)) { 7405 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", 7406 mdname(mddev), memory); 7407 ret = -ENOMEM; 7408 goto abort; 7409 } else 7410 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); 7411 /* 7412 * Losing a stripe head costs more than the time to refill it, 7413 * it reduces the queue depth and so can hurt throughput. 7414 * So set it rather large, scaled by number of devices. 7415 */ 7416 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; 7417 conf->shrinker.scan_objects = raid5_cache_scan; 7418 conf->shrinker.count_objects = raid5_cache_count; 7419 conf->shrinker.batch = 128; 7420 conf->shrinker.flags = 0; 7421 ret = register_shrinker(&conf->shrinker); 7422 if (ret) { 7423 pr_warn("md/raid:%s: couldn't register shrinker.\n", 7424 mdname(mddev)); 7425 goto abort; 7426 } 7427 7428 sprintf(pers_name, "raid%d", mddev->new_level); 7429 conf->thread = md_register_thread(raid5d, mddev, pers_name); 7430 if (!conf->thread) { 7431 pr_warn("md/raid:%s: couldn't allocate thread.\n", 7432 mdname(mddev)); 7433 ret = -ENOMEM; 7434 goto abort; 7435 } 7436 7437 return conf; 7438 7439 abort: 7440 if (conf) 7441 free_conf(conf); 7442 return ERR_PTR(ret); 7443 } 7444 7445 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) 7446 { 7447 switch (algo) { 7448 case ALGORITHM_PARITY_0: 7449 if (raid_disk < max_degraded) 7450 return 1; 7451 break; 7452 case ALGORITHM_PARITY_N: 7453 if (raid_disk >= raid_disks - max_degraded) 7454 return 1; 7455 break; 7456 case ALGORITHM_PARITY_0_6: 7457 if (raid_disk == 0 || 7458 raid_disk == raid_disks - 1) 7459 return 1; 7460 break; 7461 case ALGORITHM_LEFT_ASYMMETRIC_6: 7462 case ALGORITHM_RIGHT_ASYMMETRIC_6: 7463 case ALGORITHM_LEFT_SYMMETRIC_6: 7464 case ALGORITHM_RIGHT_SYMMETRIC_6: 7465 if (raid_disk == raid_disks - 1) 7466 return 1; 7467 } 7468 return 0; 7469 } 7470 7471 static void raid5_set_io_opt(struct r5conf *conf) 7472 { 7473 blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) * 7474 (conf->raid_disks - conf->max_degraded)); 7475 } 7476 7477 static int raid5_run(struct mddev *mddev) 7478 { 7479 struct r5conf *conf; 7480 int working_disks = 0; 7481 int dirty_parity_disks = 0; 7482 struct md_rdev *rdev; 7483 struct md_rdev *journal_dev = NULL; 7484 sector_t reshape_offset = 0; 7485 int i, ret = 0; 7486 long long min_offset_diff = 0; 7487 int first = 1; 7488 7489 if (acct_bioset_init(mddev)) { 7490 pr_err("md/raid456:%s: alloc acct bioset failed.\n", mdname(mddev)); 7491 return -ENOMEM; 7492 } 7493 7494 if (mddev_init_writes_pending(mddev) < 0) { 7495 ret = -ENOMEM; 7496 goto exit_acct_set; 7497 } 7498 7499 if (mddev->recovery_cp != MaxSector) 7500 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", 7501 mdname(mddev)); 7502 7503 rdev_for_each(rdev, mddev) { 7504 long long diff; 7505 7506 if (test_bit(Journal, &rdev->flags)) { 7507 journal_dev = rdev; 7508 continue; 7509 } 7510 if (rdev->raid_disk < 0) 7511 continue; 7512 diff = (rdev->new_data_offset - rdev->data_offset); 7513 if (first) { 7514 min_offset_diff = diff; 7515 first = 0; 7516 } else if (mddev->reshape_backwards && 7517 diff < min_offset_diff) 7518 min_offset_diff = diff; 7519 else if (!mddev->reshape_backwards && 7520 diff > min_offset_diff) 7521 min_offset_diff = diff; 7522 } 7523 7524 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) && 7525 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) { 7526 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n", 7527 mdname(mddev)); 7528 ret = -EINVAL; 7529 goto exit_acct_set; 7530 } 7531 7532 if (mddev->reshape_position != MaxSector) { 7533 /* Check that we can continue the reshape. 7534 * Difficulties arise if the stripe we would write to 7535 * next is at or after the stripe we would read from next. 7536 * For a reshape that changes the number of devices, this 7537 * is only possible for a very short time, and mdadm makes 7538 * sure that time appears to have past before assembling 7539 * the array. So we fail if that time hasn't passed. 7540 * For a reshape that keeps the number of devices the same 7541 * mdadm must be monitoring the reshape can keeping the 7542 * critical areas read-only and backed up. It will start 7543 * the array in read-only mode, so we check for that. 7544 */ 7545 sector_t here_new, here_old; 7546 int old_disks; 7547 int max_degraded = (mddev->level == 6 ? 2 : 1); 7548 int chunk_sectors; 7549 int new_data_disks; 7550 7551 if (journal_dev) { 7552 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", 7553 mdname(mddev)); 7554 ret = -EINVAL; 7555 goto exit_acct_set; 7556 } 7557 7558 if (mddev->new_level != mddev->level) { 7559 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", 7560 mdname(mddev)); 7561 ret = -EINVAL; 7562 goto exit_acct_set; 7563 } 7564 old_disks = mddev->raid_disks - mddev->delta_disks; 7565 /* reshape_position must be on a new-stripe boundary, and one 7566 * further up in new geometry must map after here in old 7567 * geometry. 7568 * If the chunk sizes are different, then as we perform reshape 7569 * in units of the largest of the two, reshape_position needs 7570 * be a multiple of the largest chunk size times new data disks. 7571 */ 7572 here_new = mddev->reshape_position; 7573 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); 7574 new_data_disks = mddev->raid_disks - max_degraded; 7575 if (sector_div(here_new, chunk_sectors * new_data_disks)) { 7576 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", 7577 mdname(mddev)); 7578 ret = -EINVAL; 7579 goto exit_acct_set; 7580 } 7581 reshape_offset = here_new * chunk_sectors; 7582 /* here_new is the stripe we will write to */ 7583 here_old = mddev->reshape_position; 7584 sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); 7585 /* here_old is the first stripe that we might need to read 7586 * from */ 7587 if (mddev->delta_disks == 0) { 7588 /* We cannot be sure it is safe to start an in-place 7589 * reshape. It is only safe if user-space is monitoring 7590 * and taking constant backups. 7591 * mdadm always starts a situation like this in 7592 * readonly mode so it can take control before 7593 * allowing any writes. So just check for that. 7594 */ 7595 if (abs(min_offset_diff) >= mddev->chunk_sectors && 7596 abs(min_offset_diff) >= mddev->new_chunk_sectors) 7597 /* not really in-place - so OK */; 7598 else if (mddev->ro == 0) { 7599 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", 7600 mdname(mddev)); 7601 ret = -EINVAL; 7602 goto exit_acct_set; 7603 } 7604 } else if (mddev->reshape_backwards 7605 ? (here_new * chunk_sectors + min_offset_diff <= 7606 here_old * chunk_sectors) 7607 : (here_new * chunk_sectors >= 7608 here_old * chunk_sectors + (-min_offset_diff))) { 7609 /* Reading from the same stripe as writing to - bad */ 7610 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", 7611 mdname(mddev)); 7612 ret = -EINVAL; 7613 goto exit_acct_set; 7614 } 7615 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); 7616 /* OK, we should be able to continue; */ 7617 } else { 7618 BUG_ON(mddev->level != mddev->new_level); 7619 BUG_ON(mddev->layout != mddev->new_layout); 7620 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); 7621 BUG_ON(mddev->delta_disks != 0); 7622 } 7623 7624 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && 7625 test_bit(MD_HAS_PPL, &mddev->flags)) { 7626 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n", 7627 mdname(mddev)); 7628 clear_bit(MD_HAS_PPL, &mddev->flags); 7629 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags); 7630 } 7631 7632 if (mddev->private == NULL) 7633 conf = setup_conf(mddev); 7634 else 7635 conf = mddev->private; 7636 7637 if (IS_ERR(conf)) { 7638 ret = PTR_ERR(conf); 7639 goto exit_acct_set; 7640 } 7641 7642 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { 7643 if (!journal_dev) { 7644 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", 7645 mdname(mddev)); 7646 mddev->ro = 1; 7647 set_disk_ro(mddev->gendisk, 1); 7648 } else if (mddev->recovery_cp == MaxSector) 7649 set_bit(MD_JOURNAL_CLEAN, &mddev->flags); 7650 } 7651 7652 conf->min_offset_diff = min_offset_diff; 7653 mddev->thread = conf->thread; 7654 conf->thread = NULL; 7655 mddev->private = conf; 7656 7657 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; 7658 i++) { 7659 rdev = rdev_mdlock_deref(mddev, conf->disks[i].rdev); 7660 if (!rdev && conf->disks[i].replacement) { 7661 /* The replacement is all we have yet */ 7662 rdev = rdev_mdlock_deref(mddev, 7663 conf->disks[i].replacement); 7664 conf->disks[i].replacement = NULL; 7665 clear_bit(Replacement, &rdev->flags); 7666 rcu_assign_pointer(conf->disks[i].rdev, rdev); 7667 } 7668 if (!rdev) 7669 continue; 7670 if (rcu_access_pointer(conf->disks[i].replacement) && 7671 conf->reshape_progress != MaxSector) { 7672 /* replacements and reshape simply do not mix. */ 7673 pr_warn("md: cannot handle concurrent replacement and reshape.\n"); 7674 goto abort; 7675 } 7676 if (test_bit(In_sync, &rdev->flags)) { 7677 working_disks++; 7678 continue; 7679 } 7680 /* This disc is not fully in-sync. However if it 7681 * just stored parity (beyond the recovery_offset), 7682 * when we don't need to be concerned about the 7683 * array being dirty. 7684 * When reshape goes 'backwards', we never have 7685 * partially completed devices, so we only need 7686 * to worry about reshape going forwards. 7687 */ 7688 /* Hack because v0.91 doesn't store recovery_offset properly. */ 7689 if (mddev->major_version == 0 && 7690 mddev->minor_version > 90) 7691 rdev->recovery_offset = reshape_offset; 7692 7693 if (rdev->recovery_offset < reshape_offset) { 7694 /* We need to check old and new layout */ 7695 if (!only_parity(rdev->raid_disk, 7696 conf->algorithm, 7697 conf->raid_disks, 7698 conf->max_degraded)) 7699 continue; 7700 } 7701 if (!only_parity(rdev->raid_disk, 7702 conf->prev_algo, 7703 conf->previous_raid_disks, 7704 conf->max_degraded)) 7705 continue; 7706 dirty_parity_disks++; 7707 } 7708 7709 /* 7710 * 0 for a fully functional array, 1 or 2 for a degraded array. 7711 */ 7712 mddev->degraded = raid5_calc_degraded(conf); 7713 7714 if (has_failed(conf)) { 7715 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", 7716 mdname(mddev), mddev->degraded, conf->raid_disks); 7717 goto abort; 7718 } 7719 7720 /* device size must be a multiple of chunk size */ 7721 mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1); 7722 mddev->resync_max_sectors = mddev->dev_sectors; 7723 7724 if (mddev->degraded > dirty_parity_disks && 7725 mddev->recovery_cp != MaxSector) { 7726 if (test_bit(MD_HAS_PPL, &mddev->flags)) 7727 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n", 7728 mdname(mddev)); 7729 else if (mddev->ok_start_degraded) 7730 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", 7731 mdname(mddev)); 7732 else { 7733 pr_crit("md/raid:%s: cannot start dirty degraded array.\n", 7734 mdname(mddev)); 7735 goto abort; 7736 } 7737 } 7738 7739 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", 7740 mdname(mddev), conf->level, 7741 mddev->raid_disks-mddev->degraded, mddev->raid_disks, 7742 mddev->new_layout); 7743 7744 print_raid5_conf(conf); 7745 7746 if (conf->reshape_progress != MaxSector) { 7747 conf->reshape_safe = conf->reshape_progress; 7748 atomic_set(&conf->reshape_stripes, 0); 7749 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 7750 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 7751 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 7752 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 7753 mddev->sync_thread = md_register_thread(md_do_sync, mddev, 7754 "reshape"); 7755 if (!mddev->sync_thread) 7756 goto abort; 7757 } 7758 7759 /* Ok, everything is just fine now */ 7760 if (mddev->to_remove == &raid5_attrs_group) 7761 mddev->to_remove = NULL; 7762 else if (mddev->kobj.sd && 7763 sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) 7764 pr_warn("raid5: failed to create sysfs attributes for %s\n", 7765 mdname(mddev)); 7766 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); 7767 7768 if (mddev->queue) { 7769 int chunk_size; 7770 /* read-ahead size must cover two whole stripes, which 7771 * is 2 * (datadisks) * chunksize where 'n' is the 7772 * number of raid devices 7773 */ 7774 int data_disks = conf->previous_raid_disks - conf->max_degraded; 7775 int stripe = data_disks * 7776 ((mddev->chunk_sectors << 9) / PAGE_SIZE); 7777 7778 chunk_size = mddev->chunk_sectors << 9; 7779 blk_queue_io_min(mddev->queue, chunk_size); 7780 raid5_set_io_opt(conf); 7781 mddev->queue->limits.raid_partial_stripes_expensive = 1; 7782 /* 7783 * We can only discard a whole stripe. It doesn't make sense to 7784 * discard data disk but write parity disk 7785 */ 7786 stripe = stripe * PAGE_SIZE; 7787 stripe = roundup_pow_of_two(stripe); 7788 mddev->queue->limits.discard_granularity = stripe; 7789 7790 blk_queue_max_write_zeroes_sectors(mddev->queue, 0); 7791 7792 rdev_for_each(rdev, mddev) { 7793 disk_stack_limits(mddev->gendisk, rdev->bdev, 7794 rdev->data_offset << 9); 7795 disk_stack_limits(mddev->gendisk, rdev->bdev, 7796 rdev->new_data_offset << 9); 7797 } 7798 7799 /* 7800 * zeroing is required, otherwise data 7801 * could be lost. Consider a scenario: discard a stripe 7802 * (the stripe could be inconsistent if 7803 * discard_zeroes_data is 0); write one disk of the 7804 * stripe (the stripe could be inconsistent again 7805 * depending on which disks are used to calculate 7806 * parity); the disk is broken; The stripe data of this 7807 * disk is lost. 7808 * 7809 * We only allow DISCARD if the sysadmin has confirmed that 7810 * only safe devices are in use by setting a module parameter. 7811 * A better idea might be to turn DISCARD into WRITE_ZEROES 7812 * requests, as that is required to be safe. 7813 */ 7814 if (!devices_handle_discard_safely || 7815 mddev->queue->limits.max_discard_sectors < (stripe >> 9) || 7816 mddev->queue->limits.discard_granularity < stripe) 7817 blk_queue_max_discard_sectors(mddev->queue, 0); 7818 7819 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); 7820 } 7821 7822 if (log_init(conf, journal_dev, raid5_has_ppl(conf))) 7823 goto abort; 7824 7825 return 0; 7826 abort: 7827 md_unregister_thread(&mddev->thread); 7828 print_raid5_conf(conf); 7829 free_conf(conf); 7830 mddev->private = NULL; 7831 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev)); 7832 ret = -EIO; 7833 exit_acct_set: 7834 acct_bioset_exit(mddev); 7835 return ret; 7836 } 7837 7838 static void raid5_free(struct mddev *mddev, void *priv) 7839 { 7840 struct r5conf *conf = priv; 7841 7842 free_conf(conf); 7843 acct_bioset_exit(mddev); 7844 mddev->to_remove = &raid5_attrs_group; 7845 } 7846 7847 static void raid5_status(struct seq_file *seq, struct mddev *mddev) 7848 { 7849 struct r5conf *conf = mddev->private; 7850 int i; 7851 7852 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, 7853 conf->chunk_sectors / 2, mddev->layout); 7854 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); 7855 rcu_read_lock(); 7856 for (i = 0; i < conf->raid_disks; i++) { 7857 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); 7858 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 7859 } 7860 rcu_read_unlock(); 7861 seq_printf (seq, "]"); 7862 } 7863 7864 static void print_raid5_conf (struct r5conf *conf) 7865 { 7866 struct md_rdev *rdev; 7867 int i; 7868 7869 pr_debug("RAID conf printout:\n"); 7870 if (!conf) { 7871 pr_debug("(conf==NULL)\n"); 7872 return; 7873 } 7874 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, 7875 conf->raid_disks, 7876 conf->raid_disks - conf->mddev->degraded); 7877 7878 rcu_read_lock(); 7879 for (i = 0; i < conf->raid_disks; i++) { 7880 char b[BDEVNAME_SIZE]; 7881 rdev = rcu_dereference(conf->disks[i].rdev); 7882 if (rdev) 7883 pr_debug(" disk %d, o:%d, dev:%s\n", 7884 i, !test_bit(Faulty, &rdev->flags), 7885 bdevname(rdev->bdev, b)); 7886 } 7887 rcu_read_unlock(); 7888 } 7889 7890 static int raid5_spare_active(struct mddev *mddev) 7891 { 7892 int i; 7893 struct r5conf *conf = mddev->private; 7894 struct md_rdev *rdev, *replacement; 7895 int count = 0; 7896 unsigned long flags; 7897 7898 for (i = 0; i < conf->raid_disks; i++) { 7899 rdev = rdev_mdlock_deref(mddev, conf->disks[i].rdev); 7900 replacement = rdev_mdlock_deref(mddev, 7901 conf->disks[i].replacement); 7902 if (replacement 7903 && replacement->recovery_offset == MaxSector 7904 && !test_bit(Faulty, &replacement->flags) 7905 && !test_and_set_bit(In_sync, &replacement->flags)) { 7906 /* Replacement has just become active. */ 7907 if (!rdev 7908 || !test_and_clear_bit(In_sync, &rdev->flags)) 7909 count++; 7910 if (rdev) { 7911 /* Replaced device not technically faulty, 7912 * but we need to be sure it gets removed 7913 * and never re-added. 7914 */ 7915 set_bit(Faulty, &rdev->flags); 7916 sysfs_notify_dirent_safe( 7917 rdev->sysfs_state); 7918 } 7919 sysfs_notify_dirent_safe(replacement->sysfs_state); 7920 } else if (rdev 7921 && rdev->recovery_offset == MaxSector 7922 && !test_bit(Faulty, &rdev->flags) 7923 && !test_and_set_bit(In_sync, &rdev->flags)) { 7924 count++; 7925 sysfs_notify_dirent_safe(rdev->sysfs_state); 7926 } 7927 } 7928 spin_lock_irqsave(&conf->device_lock, flags); 7929 mddev->degraded = raid5_calc_degraded(conf); 7930 spin_unlock_irqrestore(&conf->device_lock, flags); 7931 print_raid5_conf(conf); 7932 return count; 7933 } 7934 7935 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 7936 { 7937 struct r5conf *conf = mddev->private; 7938 int err = 0; 7939 int number = rdev->raid_disk; 7940 struct md_rdev __rcu **rdevp; 7941 struct disk_info *p = conf->disks + number; 7942 struct md_rdev *tmp; 7943 7944 print_raid5_conf(conf); 7945 if (test_bit(Journal, &rdev->flags) && conf->log) { 7946 /* 7947 * we can't wait pending write here, as this is called in 7948 * raid5d, wait will deadlock. 7949 * neilb: there is no locking about new writes here, 7950 * so this cannot be safe. 7951 */ 7952 if (atomic_read(&conf->active_stripes) || 7953 atomic_read(&conf->r5c_cached_full_stripes) || 7954 atomic_read(&conf->r5c_cached_partial_stripes)) { 7955 return -EBUSY; 7956 } 7957 log_exit(conf); 7958 return 0; 7959 } 7960 if (rdev == rcu_access_pointer(p->rdev)) 7961 rdevp = &p->rdev; 7962 else if (rdev == rcu_access_pointer(p->replacement)) 7963 rdevp = &p->replacement; 7964 else 7965 return 0; 7966 7967 if (number >= conf->raid_disks && 7968 conf->reshape_progress == MaxSector) 7969 clear_bit(In_sync, &rdev->flags); 7970 7971 if (test_bit(In_sync, &rdev->flags) || 7972 atomic_read(&rdev->nr_pending)) { 7973 err = -EBUSY; 7974 goto abort; 7975 } 7976 /* Only remove non-faulty devices if recovery 7977 * isn't possible. 7978 */ 7979 if (!test_bit(Faulty, &rdev->flags) && 7980 mddev->recovery_disabled != conf->recovery_disabled && 7981 !has_failed(conf) && 7982 (!rcu_access_pointer(p->replacement) || 7983 rcu_access_pointer(p->replacement) == rdev) && 7984 number < conf->raid_disks) { 7985 err = -EBUSY; 7986 goto abort; 7987 } 7988 *rdevp = NULL; 7989 if (!test_bit(RemoveSynchronized, &rdev->flags)) { 7990 lockdep_assert_held(&mddev->reconfig_mutex); 7991 synchronize_rcu(); 7992 if (atomic_read(&rdev->nr_pending)) { 7993 /* lost the race, try later */ 7994 err = -EBUSY; 7995 rcu_assign_pointer(*rdevp, rdev); 7996 } 7997 } 7998 if (!err) { 7999 err = log_modify(conf, rdev, false); 8000 if (err) 8001 goto abort; 8002 } 8003 8004 tmp = rcu_access_pointer(p->replacement); 8005 if (tmp) { 8006 /* We must have just cleared 'rdev' */ 8007 rcu_assign_pointer(p->rdev, tmp); 8008 clear_bit(Replacement, &tmp->flags); 8009 smp_mb(); /* Make sure other CPUs may see both as identical 8010 * but will never see neither - if they are careful 8011 */ 8012 rcu_assign_pointer(p->replacement, NULL); 8013 8014 if (!err) 8015 err = log_modify(conf, tmp, true); 8016 } 8017 8018 clear_bit(WantReplacement, &rdev->flags); 8019 abort: 8020 8021 print_raid5_conf(conf); 8022 return err; 8023 } 8024 8025 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) 8026 { 8027 struct r5conf *conf = mddev->private; 8028 int ret, err = -EEXIST; 8029 int disk; 8030 struct disk_info *p; 8031 struct md_rdev *tmp; 8032 int first = 0; 8033 int last = conf->raid_disks - 1; 8034 8035 if (test_bit(Journal, &rdev->flags)) { 8036 if (conf->log) 8037 return -EBUSY; 8038 8039 rdev->raid_disk = 0; 8040 /* 8041 * The array is in readonly mode if journal is missing, so no 8042 * write requests running. We should be safe 8043 */ 8044 ret = log_init(conf, rdev, false); 8045 if (ret) 8046 return ret; 8047 8048 ret = r5l_start(conf->log); 8049 if (ret) 8050 return ret; 8051 8052 return 0; 8053 } 8054 if (mddev->recovery_disabled == conf->recovery_disabled) 8055 return -EBUSY; 8056 8057 if (rdev->saved_raid_disk < 0 && has_failed(conf)) 8058 /* no point adding a device */ 8059 return -EINVAL; 8060 8061 if (rdev->raid_disk >= 0) 8062 first = last = rdev->raid_disk; 8063 8064 /* 8065 * find the disk ... but prefer rdev->saved_raid_disk 8066 * if possible. 8067 */ 8068 if (rdev->saved_raid_disk >= 0 && 8069 rdev->saved_raid_disk >= first && 8070 conf->disks[rdev->saved_raid_disk].rdev == NULL) 8071 first = rdev->saved_raid_disk; 8072 8073 for (disk = first; disk <= last; disk++) { 8074 p = conf->disks + disk; 8075 if (p->rdev == NULL) { 8076 clear_bit(In_sync, &rdev->flags); 8077 rdev->raid_disk = disk; 8078 if (rdev->saved_raid_disk != disk) 8079 conf->fullsync = 1; 8080 rcu_assign_pointer(p->rdev, rdev); 8081 8082 err = log_modify(conf, rdev, true); 8083 8084 goto out; 8085 } 8086 } 8087 for (disk = first; disk <= last; disk++) { 8088 p = conf->disks + disk; 8089 tmp = rdev_mdlock_deref(mddev, p->rdev); 8090 if (test_bit(WantReplacement, &tmp->flags) && 8091 p->replacement == NULL) { 8092 clear_bit(In_sync, &rdev->flags); 8093 set_bit(Replacement, &rdev->flags); 8094 rdev->raid_disk = disk; 8095 err = 0; 8096 conf->fullsync = 1; 8097 rcu_assign_pointer(p->replacement, rdev); 8098 break; 8099 } 8100 } 8101 out: 8102 print_raid5_conf(conf); 8103 return err; 8104 } 8105 8106 static int raid5_resize(struct mddev *mddev, sector_t sectors) 8107 { 8108 /* no resync is happening, and there is enough space 8109 * on all devices, so we can resize. 8110 * We need to make sure resync covers any new space. 8111 * If the array is shrinking we should possibly wait until 8112 * any io in the removed space completes, but it hardly seems 8113 * worth it. 8114 */ 8115 sector_t newsize; 8116 struct r5conf *conf = mddev->private; 8117 8118 if (raid5_has_log(conf) || raid5_has_ppl(conf)) 8119 return -EINVAL; 8120 sectors &= ~((sector_t)conf->chunk_sectors - 1); 8121 newsize = raid5_size(mddev, sectors, mddev->raid_disks); 8122 if (mddev->external_size && 8123 mddev->array_sectors > newsize) 8124 return -EINVAL; 8125 if (mddev->bitmap) { 8126 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0); 8127 if (ret) 8128 return ret; 8129 } 8130 md_set_array_sectors(mddev, newsize); 8131 if (sectors > mddev->dev_sectors && 8132 mddev->recovery_cp > mddev->dev_sectors) { 8133 mddev->recovery_cp = mddev->dev_sectors; 8134 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 8135 } 8136 mddev->dev_sectors = sectors; 8137 mddev->resync_max_sectors = sectors; 8138 return 0; 8139 } 8140 8141 static int check_stripe_cache(struct mddev *mddev) 8142 { 8143 /* Can only proceed if there are plenty of stripe_heads. 8144 * We need a minimum of one full stripe,, and for sensible progress 8145 * it is best to have about 4 times that. 8146 * If we require 4 times, then the default 256 4K stripe_heads will 8147 * allow for chunk sizes up to 256K, which is probably OK. 8148 * If the chunk size is greater, user-space should request more 8149 * stripe_heads first. 8150 */ 8151 struct r5conf *conf = mddev->private; 8152 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 8153 > conf->min_nr_stripes || 8154 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 8155 > conf->min_nr_stripes) { 8156 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", 8157 mdname(mddev), 8158 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) 8159 / RAID5_STRIPE_SIZE(conf))*4); 8160 return 0; 8161 } 8162 return 1; 8163 } 8164 8165 static int check_reshape(struct mddev *mddev) 8166 { 8167 struct r5conf *conf = mddev->private; 8168 8169 if (raid5_has_log(conf) || raid5_has_ppl(conf)) 8170 return -EINVAL; 8171 if (mddev->delta_disks == 0 && 8172 mddev->new_layout == mddev->layout && 8173 mddev->new_chunk_sectors == mddev->chunk_sectors) 8174 return 0; /* nothing to do */ 8175 if (has_failed(conf)) 8176 return -EINVAL; 8177 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { 8178 /* We might be able to shrink, but the devices must 8179 * be made bigger first. 8180 * For raid6, 4 is the minimum size. 8181 * Otherwise 2 is the minimum 8182 */ 8183 int min = 2; 8184 if (mddev->level == 6) 8185 min = 4; 8186 if (mddev->raid_disks + mddev->delta_disks < min) 8187 return -EINVAL; 8188 } 8189 8190 if (!check_stripe_cache(mddev)) 8191 return -ENOSPC; 8192 8193 if (mddev->new_chunk_sectors > mddev->chunk_sectors || 8194 mddev->delta_disks > 0) 8195 if (resize_chunks(conf, 8196 conf->previous_raid_disks 8197 + max(0, mddev->delta_disks), 8198 max(mddev->new_chunk_sectors, 8199 mddev->chunk_sectors) 8200 ) < 0) 8201 return -ENOMEM; 8202 8203 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size) 8204 return 0; /* never bother to shrink */ 8205 return resize_stripes(conf, (conf->previous_raid_disks 8206 + mddev->delta_disks)); 8207 } 8208 8209 static int raid5_start_reshape(struct mddev *mddev) 8210 { 8211 struct r5conf *conf = mddev->private; 8212 struct md_rdev *rdev; 8213 int spares = 0; 8214 unsigned long flags; 8215 8216 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 8217 return -EBUSY; 8218 8219 if (!check_stripe_cache(mddev)) 8220 return -ENOSPC; 8221 8222 if (has_failed(conf)) 8223 return -EINVAL; 8224 8225 rdev_for_each(rdev, mddev) { 8226 if (!test_bit(In_sync, &rdev->flags) 8227 && !test_bit(Faulty, &rdev->flags)) 8228 spares++; 8229 } 8230 8231 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) 8232 /* Not enough devices even to make a degraded array 8233 * of that size 8234 */ 8235 return -EINVAL; 8236 8237 /* Refuse to reduce size of the array. Any reductions in 8238 * array size must be through explicit setting of array_size 8239 * attribute. 8240 */ 8241 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) 8242 < mddev->array_sectors) { 8243 pr_warn("md/raid:%s: array size must be reduced before number of disks\n", 8244 mdname(mddev)); 8245 return -EINVAL; 8246 } 8247 8248 atomic_set(&conf->reshape_stripes, 0); 8249 spin_lock_irq(&conf->device_lock); 8250 write_seqcount_begin(&conf->gen_lock); 8251 conf->previous_raid_disks = conf->raid_disks; 8252 conf->raid_disks += mddev->delta_disks; 8253 conf->prev_chunk_sectors = conf->chunk_sectors; 8254 conf->chunk_sectors = mddev->new_chunk_sectors; 8255 conf->prev_algo = conf->algorithm; 8256 conf->algorithm = mddev->new_layout; 8257 conf->generation++; 8258 /* Code that selects data_offset needs to see the generation update 8259 * if reshape_progress has been set - so a memory barrier needed. 8260 */ 8261 smp_mb(); 8262 if (mddev->reshape_backwards) 8263 conf->reshape_progress = raid5_size(mddev, 0, 0); 8264 else 8265 conf->reshape_progress = 0; 8266 conf->reshape_safe = conf->reshape_progress; 8267 write_seqcount_end(&conf->gen_lock); 8268 spin_unlock_irq(&conf->device_lock); 8269 8270 /* Now make sure any requests that proceeded on the assumption 8271 * the reshape wasn't running - like Discard or Read - have 8272 * completed. 8273 */ 8274 mddev_suspend(mddev); 8275 mddev_resume(mddev); 8276 8277 /* Add some new drives, as many as will fit. 8278 * We know there are enough to make the newly sized array work. 8279 * Don't add devices if we are reducing the number of 8280 * devices in the array. This is because it is not possible 8281 * to correctly record the "partially reconstructed" state of 8282 * such devices during the reshape and confusion could result. 8283 */ 8284 if (mddev->delta_disks >= 0) { 8285 rdev_for_each(rdev, mddev) 8286 if (rdev->raid_disk < 0 && 8287 !test_bit(Faulty, &rdev->flags)) { 8288 if (raid5_add_disk(mddev, rdev) == 0) { 8289 if (rdev->raid_disk 8290 >= conf->previous_raid_disks) 8291 set_bit(In_sync, &rdev->flags); 8292 else 8293 rdev->recovery_offset = 0; 8294 8295 /* Failure here is OK */ 8296 sysfs_link_rdev(mddev, rdev); 8297 } 8298 } else if (rdev->raid_disk >= conf->previous_raid_disks 8299 && !test_bit(Faulty, &rdev->flags)) { 8300 /* This is a spare that was manually added */ 8301 set_bit(In_sync, &rdev->flags); 8302 } 8303 8304 /* When a reshape changes the number of devices, 8305 * ->degraded is measured against the larger of the 8306 * pre and post number of devices. 8307 */ 8308 spin_lock_irqsave(&conf->device_lock, flags); 8309 mddev->degraded = raid5_calc_degraded(conf); 8310 spin_unlock_irqrestore(&conf->device_lock, flags); 8311 } 8312 mddev->raid_disks = conf->raid_disks; 8313 mddev->reshape_position = conf->reshape_progress; 8314 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 8315 8316 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); 8317 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); 8318 clear_bit(MD_RECOVERY_DONE, &mddev->recovery); 8319 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); 8320 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); 8321 mddev->sync_thread = md_register_thread(md_do_sync, mddev, 8322 "reshape"); 8323 if (!mddev->sync_thread) { 8324 mddev->recovery = 0; 8325 spin_lock_irq(&conf->device_lock); 8326 write_seqcount_begin(&conf->gen_lock); 8327 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; 8328 mddev->new_chunk_sectors = 8329 conf->chunk_sectors = conf->prev_chunk_sectors; 8330 mddev->new_layout = conf->algorithm = conf->prev_algo; 8331 rdev_for_each(rdev, mddev) 8332 rdev->new_data_offset = rdev->data_offset; 8333 smp_wmb(); 8334 conf->generation --; 8335 conf->reshape_progress = MaxSector; 8336 mddev->reshape_position = MaxSector; 8337 write_seqcount_end(&conf->gen_lock); 8338 spin_unlock_irq(&conf->device_lock); 8339 return -EAGAIN; 8340 } 8341 conf->reshape_checkpoint = jiffies; 8342 md_wakeup_thread(mddev->sync_thread); 8343 md_new_event(); 8344 return 0; 8345 } 8346 8347 /* This is called from the reshape thread and should make any 8348 * changes needed in 'conf' 8349 */ 8350 static void end_reshape(struct r5conf *conf) 8351 { 8352 8353 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { 8354 struct md_rdev *rdev; 8355 8356 spin_lock_irq(&conf->device_lock); 8357 conf->previous_raid_disks = conf->raid_disks; 8358 md_finish_reshape(conf->mddev); 8359 smp_wmb(); 8360 conf->reshape_progress = MaxSector; 8361 conf->mddev->reshape_position = MaxSector; 8362 rdev_for_each(rdev, conf->mddev) 8363 if (rdev->raid_disk >= 0 && 8364 !test_bit(Journal, &rdev->flags) && 8365 !test_bit(In_sync, &rdev->flags)) 8366 rdev->recovery_offset = MaxSector; 8367 spin_unlock_irq(&conf->device_lock); 8368 wake_up(&conf->wait_for_overlap); 8369 8370 if (conf->mddev->queue) 8371 raid5_set_io_opt(conf); 8372 } 8373 } 8374 8375 /* This is called from the raid5d thread with mddev_lock held. 8376 * It makes config changes to the device. 8377 */ 8378 static void raid5_finish_reshape(struct mddev *mddev) 8379 { 8380 struct r5conf *conf = mddev->private; 8381 struct md_rdev *rdev; 8382 8383 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { 8384 8385 if (mddev->delta_disks <= 0) { 8386 int d; 8387 spin_lock_irq(&conf->device_lock); 8388 mddev->degraded = raid5_calc_degraded(conf); 8389 spin_unlock_irq(&conf->device_lock); 8390 for (d = conf->raid_disks ; 8391 d < conf->raid_disks - mddev->delta_disks; 8392 d++) { 8393 rdev = rdev_mdlock_deref(mddev, 8394 conf->disks[d].rdev); 8395 if (rdev) 8396 clear_bit(In_sync, &rdev->flags); 8397 rdev = rdev_mdlock_deref(mddev, 8398 conf->disks[d].replacement); 8399 if (rdev) 8400 clear_bit(In_sync, &rdev->flags); 8401 } 8402 } 8403 mddev->layout = conf->algorithm; 8404 mddev->chunk_sectors = conf->chunk_sectors; 8405 mddev->reshape_position = MaxSector; 8406 mddev->delta_disks = 0; 8407 mddev->reshape_backwards = 0; 8408 } 8409 } 8410 8411 static void raid5_quiesce(struct mddev *mddev, int quiesce) 8412 { 8413 struct r5conf *conf = mddev->private; 8414 8415 if (quiesce) { 8416 /* stop all writes */ 8417 lock_all_device_hash_locks_irq(conf); 8418 /* '2' tells resync/reshape to pause so that all 8419 * active stripes can drain 8420 */ 8421 r5c_flush_cache(conf, INT_MAX); 8422 /* need a memory barrier to make sure read_one_chunk() sees 8423 * quiesce started and reverts to slow (locked) path. 8424 */ 8425 smp_store_release(&conf->quiesce, 2); 8426 wait_event_cmd(conf->wait_for_quiescent, 8427 atomic_read(&conf->active_stripes) == 0 && 8428 atomic_read(&conf->active_aligned_reads) == 0, 8429 unlock_all_device_hash_locks_irq(conf), 8430 lock_all_device_hash_locks_irq(conf)); 8431 conf->quiesce = 1; 8432 unlock_all_device_hash_locks_irq(conf); 8433 /* allow reshape to continue */ 8434 wake_up(&conf->wait_for_overlap); 8435 } else { 8436 /* re-enable writes */ 8437 lock_all_device_hash_locks_irq(conf); 8438 conf->quiesce = 0; 8439 wake_up(&conf->wait_for_quiescent); 8440 wake_up(&conf->wait_for_overlap); 8441 unlock_all_device_hash_locks_irq(conf); 8442 } 8443 log_quiesce(conf, quiesce); 8444 } 8445 8446 static void *raid45_takeover_raid0(struct mddev *mddev, int level) 8447 { 8448 struct r0conf *raid0_conf = mddev->private; 8449 sector_t sectors; 8450 8451 /* for raid0 takeover only one zone is supported */ 8452 if (raid0_conf->nr_strip_zones > 1) { 8453 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", 8454 mdname(mddev)); 8455 return ERR_PTR(-EINVAL); 8456 } 8457 8458 sectors = raid0_conf->strip_zone[0].zone_end; 8459 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); 8460 mddev->dev_sectors = sectors; 8461 mddev->new_level = level; 8462 mddev->new_layout = ALGORITHM_PARITY_N; 8463 mddev->new_chunk_sectors = mddev->chunk_sectors; 8464 mddev->raid_disks += 1; 8465 mddev->delta_disks = 1; 8466 /* make sure it will be not marked as dirty */ 8467 mddev->recovery_cp = MaxSector; 8468 8469 return setup_conf(mddev); 8470 } 8471 8472 static void *raid5_takeover_raid1(struct mddev *mddev) 8473 { 8474 int chunksect; 8475 void *ret; 8476 8477 if (mddev->raid_disks != 2 || 8478 mddev->degraded > 1) 8479 return ERR_PTR(-EINVAL); 8480 8481 /* Should check if there are write-behind devices? */ 8482 8483 chunksect = 64*2; /* 64K by default */ 8484 8485 /* The array must be an exact multiple of chunksize */ 8486 while (chunksect && (mddev->array_sectors & (chunksect-1))) 8487 chunksect >>= 1; 8488 8489 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private)) 8490 /* array size does not allow a suitable chunk size */ 8491 return ERR_PTR(-EINVAL); 8492 8493 mddev->new_level = 5; 8494 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; 8495 mddev->new_chunk_sectors = chunksect; 8496 8497 ret = setup_conf(mddev); 8498 if (!IS_ERR(ret)) 8499 mddev_clear_unsupported_flags(mddev, 8500 UNSUPPORTED_MDDEV_FLAGS); 8501 return ret; 8502 } 8503 8504 static void *raid5_takeover_raid6(struct mddev *mddev) 8505 { 8506 int new_layout; 8507 8508 switch (mddev->layout) { 8509 case ALGORITHM_LEFT_ASYMMETRIC_6: 8510 new_layout = ALGORITHM_LEFT_ASYMMETRIC; 8511 break; 8512 case ALGORITHM_RIGHT_ASYMMETRIC_6: 8513 new_layout = ALGORITHM_RIGHT_ASYMMETRIC; 8514 break; 8515 case ALGORITHM_LEFT_SYMMETRIC_6: 8516 new_layout = ALGORITHM_LEFT_SYMMETRIC; 8517 break; 8518 case ALGORITHM_RIGHT_SYMMETRIC_6: 8519 new_layout = ALGORITHM_RIGHT_SYMMETRIC; 8520 break; 8521 case ALGORITHM_PARITY_0_6: 8522 new_layout = ALGORITHM_PARITY_0; 8523 break; 8524 case ALGORITHM_PARITY_N: 8525 new_layout = ALGORITHM_PARITY_N; 8526 break; 8527 default: 8528 return ERR_PTR(-EINVAL); 8529 } 8530 mddev->new_level = 5; 8531 mddev->new_layout = new_layout; 8532 mddev->delta_disks = -1; 8533 mddev->raid_disks -= 1; 8534 return setup_conf(mddev); 8535 } 8536 8537 static int raid5_check_reshape(struct mddev *mddev) 8538 { 8539 /* For a 2-drive array, the layout and chunk size can be changed 8540 * immediately as not restriping is needed. 8541 * For larger arrays we record the new value - after validation 8542 * to be used by a reshape pass. 8543 */ 8544 struct r5conf *conf = mddev->private; 8545 int new_chunk = mddev->new_chunk_sectors; 8546 8547 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) 8548 return -EINVAL; 8549 if (new_chunk > 0) { 8550 if (!is_power_of_2(new_chunk)) 8551 return -EINVAL; 8552 if (new_chunk < (PAGE_SIZE>>9)) 8553 return -EINVAL; 8554 if (mddev->array_sectors & (new_chunk-1)) 8555 /* not factor of array size */ 8556 return -EINVAL; 8557 } 8558 8559 /* They look valid */ 8560 8561 if (mddev->raid_disks == 2) { 8562 /* can make the change immediately */ 8563 if (mddev->new_layout >= 0) { 8564 conf->algorithm = mddev->new_layout; 8565 mddev->layout = mddev->new_layout; 8566 } 8567 if (new_chunk > 0) { 8568 conf->chunk_sectors = new_chunk ; 8569 mddev->chunk_sectors = new_chunk; 8570 } 8571 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 8572 md_wakeup_thread(mddev->thread); 8573 } 8574 return check_reshape(mddev); 8575 } 8576 8577 static int raid6_check_reshape(struct mddev *mddev) 8578 { 8579 int new_chunk = mddev->new_chunk_sectors; 8580 8581 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) 8582 return -EINVAL; 8583 if (new_chunk > 0) { 8584 if (!is_power_of_2(new_chunk)) 8585 return -EINVAL; 8586 if (new_chunk < (PAGE_SIZE >> 9)) 8587 return -EINVAL; 8588 if (mddev->array_sectors & (new_chunk-1)) 8589 /* not factor of array size */ 8590 return -EINVAL; 8591 } 8592 8593 /* They look valid */ 8594 return check_reshape(mddev); 8595 } 8596 8597 static void *raid5_takeover(struct mddev *mddev) 8598 { 8599 /* raid5 can take over: 8600 * raid0 - if there is only one strip zone - make it a raid4 layout 8601 * raid1 - if there are two drives. We need to know the chunk size 8602 * raid4 - trivial - just use a raid4 layout. 8603 * raid6 - Providing it is a *_6 layout 8604 */ 8605 if (mddev->level == 0) 8606 return raid45_takeover_raid0(mddev, 5); 8607 if (mddev->level == 1) 8608 return raid5_takeover_raid1(mddev); 8609 if (mddev->level == 4) { 8610 mddev->new_layout = ALGORITHM_PARITY_N; 8611 mddev->new_level = 5; 8612 return setup_conf(mddev); 8613 } 8614 if (mddev->level == 6) 8615 return raid5_takeover_raid6(mddev); 8616 8617 return ERR_PTR(-EINVAL); 8618 } 8619 8620 static void *raid4_takeover(struct mddev *mddev) 8621 { 8622 /* raid4 can take over: 8623 * raid0 - if there is only one strip zone 8624 * raid5 - if layout is right 8625 */ 8626 if (mddev->level == 0) 8627 return raid45_takeover_raid0(mddev, 4); 8628 if (mddev->level == 5 && 8629 mddev->layout == ALGORITHM_PARITY_N) { 8630 mddev->new_layout = 0; 8631 mddev->new_level = 4; 8632 return setup_conf(mddev); 8633 } 8634 return ERR_PTR(-EINVAL); 8635 } 8636 8637 static struct md_personality raid5_personality; 8638 8639 static void *raid6_takeover(struct mddev *mddev) 8640 { 8641 /* Currently can only take over a raid5. We map the 8642 * personality to an equivalent raid6 personality 8643 * with the Q block at the end. 8644 */ 8645 int new_layout; 8646 8647 if (mddev->pers != &raid5_personality) 8648 return ERR_PTR(-EINVAL); 8649 if (mddev->degraded > 1) 8650 return ERR_PTR(-EINVAL); 8651 if (mddev->raid_disks > 253) 8652 return ERR_PTR(-EINVAL); 8653 if (mddev->raid_disks < 3) 8654 return ERR_PTR(-EINVAL); 8655 8656 switch (mddev->layout) { 8657 case ALGORITHM_LEFT_ASYMMETRIC: 8658 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; 8659 break; 8660 case ALGORITHM_RIGHT_ASYMMETRIC: 8661 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; 8662 break; 8663 case ALGORITHM_LEFT_SYMMETRIC: 8664 new_layout = ALGORITHM_LEFT_SYMMETRIC_6; 8665 break; 8666 case ALGORITHM_RIGHT_SYMMETRIC: 8667 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; 8668 break; 8669 case ALGORITHM_PARITY_0: 8670 new_layout = ALGORITHM_PARITY_0_6; 8671 break; 8672 case ALGORITHM_PARITY_N: 8673 new_layout = ALGORITHM_PARITY_N; 8674 break; 8675 default: 8676 return ERR_PTR(-EINVAL); 8677 } 8678 mddev->new_level = 6; 8679 mddev->new_layout = new_layout; 8680 mddev->delta_disks = 1; 8681 mddev->raid_disks += 1; 8682 return setup_conf(mddev); 8683 } 8684 8685 static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) 8686 { 8687 struct r5conf *conf; 8688 int err; 8689 8690 err = mddev_lock(mddev); 8691 if (err) 8692 return err; 8693 conf = mddev->private; 8694 if (!conf) { 8695 mddev_unlock(mddev); 8696 return -ENODEV; 8697 } 8698 8699 if (strncmp(buf, "ppl", 3) == 0) { 8700 /* ppl only works with RAID 5 */ 8701 if (!raid5_has_ppl(conf) && conf->level == 5) { 8702 err = log_init(conf, NULL, true); 8703 if (!err) { 8704 err = resize_stripes(conf, conf->pool_size); 8705 if (err) 8706 log_exit(conf); 8707 } 8708 } else 8709 err = -EINVAL; 8710 } else if (strncmp(buf, "resync", 6) == 0) { 8711 if (raid5_has_ppl(conf)) { 8712 mddev_suspend(mddev); 8713 log_exit(conf); 8714 mddev_resume(mddev); 8715 err = resize_stripes(conf, conf->pool_size); 8716 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) && 8717 r5l_log_disk_error(conf)) { 8718 bool journal_dev_exists = false; 8719 struct md_rdev *rdev; 8720 8721 rdev_for_each(rdev, mddev) 8722 if (test_bit(Journal, &rdev->flags)) { 8723 journal_dev_exists = true; 8724 break; 8725 } 8726 8727 if (!journal_dev_exists) { 8728 mddev_suspend(mddev); 8729 clear_bit(MD_HAS_JOURNAL, &mddev->flags); 8730 mddev_resume(mddev); 8731 } else /* need remove journal device first */ 8732 err = -EBUSY; 8733 } else 8734 err = -EINVAL; 8735 } else { 8736 err = -EINVAL; 8737 } 8738 8739 if (!err) 8740 md_update_sb(mddev, 1); 8741 8742 mddev_unlock(mddev); 8743 8744 return err; 8745 } 8746 8747 static int raid5_start(struct mddev *mddev) 8748 { 8749 struct r5conf *conf = mddev->private; 8750 8751 return r5l_start(conf->log); 8752 } 8753 8754 static struct md_personality raid6_personality = 8755 { 8756 .name = "raid6", 8757 .level = 6, 8758 .owner = THIS_MODULE, 8759 .make_request = raid5_make_request, 8760 .run = raid5_run, 8761 .start = raid5_start, 8762 .free = raid5_free, 8763 .status = raid5_status, 8764 .error_handler = raid5_error, 8765 .hot_add_disk = raid5_add_disk, 8766 .hot_remove_disk= raid5_remove_disk, 8767 .spare_active = raid5_spare_active, 8768 .sync_request = raid5_sync_request, 8769 .resize = raid5_resize, 8770 .size = raid5_size, 8771 .check_reshape = raid6_check_reshape, 8772 .start_reshape = raid5_start_reshape, 8773 .finish_reshape = raid5_finish_reshape, 8774 .quiesce = raid5_quiesce, 8775 .takeover = raid6_takeover, 8776 .change_consistency_policy = raid5_change_consistency_policy, 8777 }; 8778 static struct md_personality raid5_personality = 8779 { 8780 .name = "raid5", 8781 .level = 5, 8782 .owner = THIS_MODULE, 8783 .make_request = raid5_make_request, 8784 .run = raid5_run, 8785 .start = raid5_start, 8786 .free = raid5_free, 8787 .status = raid5_status, 8788 .error_handler = raid5_error, 8789 .hot_add_disk = raid5_add_disk, 8790 .hot_remove_disk= raid5_remove_disk, 8791 .spare_active = raid5_spare_active, 8792 .sync_request = raid5_sync_request, 8793 .resize = raid5_resize, 8794 .size = raid5_size, 8795 .check_reshape = raid5_check_reshape, 8796 .start_reshape = raid5_start_reshape, 8797 .finish_reshape = raid5_finish_reshape, 8798 .quiesce = raid5_quiesce, 8799 .takeover = raid5_takeover, 8800 .change_consistency_policy = raid5_change_consistency_policy, 8801 }; 8802 8803 static struct md_personality raid4_personality = 8804 { 8805 .name = "raid4", 8806 .level = 4, 8807 .owner = THIS_MODULE, 8808 .make_request = raid5_make_request, 8809 .run = raid5_run, 8810 .start = raid5_start, 8811 .free = raid5_free, 8812 .status = raid5_status, 8813 .error_handler = raid5_error, 8814 .hot_add_disk = raid5_add_disk, 8815 .hot_remove_disk= raid5_remove_disk, 8816 .spare_active = raid5_spare_active, 8817 .sync_request = raid5_sync_request, 8818 .resize = raid5_resize, 8819 .size = raid5_size, 8820 .check_reshape = raid5_check_reshape, 8821 .start_reshape = raid5_start_reshape, 8822 .finish_reshape = raid5_finish_reshape, 8823 .quiesce = raid5_quiesce, 8824 .takeover = raid4_takeover, 8825 .change_consistency_policy = raid5_change_consistency_policy, 8826 }; 8827 8828 static int __init raid5_init(void) 8829 { 8830 int ret; 8831 8832 raid5_wq = alloc_workqueue("raid5wq", 8833 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); 8834 if (!raid5_wq) 8835 return -ENOMEM; 8836 8837 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, 8838 "md/raid5:prepare", 8839 raid456_cpu_up_prepare, 8840 raid456_cpu_dead); 8841 if (ret) { 8842 destroy_workqueue(raid5_wq); 8843 return ret; 8844 } 8845 register_md_personality(&raid6_personality); 8846 register_md_personality(&raid5_personality); 8847 register_md_personality(&raid4_personality); 8848 return 0; 8849 } 8850 8851 static void raid5_exit(void) 8852 { 8853 unregister_md_personality(&raid6_personality); 8854 unregister_md_personality(&raid5_personality); 8855 unregister_md_personality(&raid4_personality); 8856 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); 8857 destroy_workqueue(raid5_wq); 8858 } 8859 8860 module_init(raid5_init); 8861 module_exit(raid5_exit); 8862 MODULE_LICENSE("GPL"); 8863 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); 8864 MODULE_ALIAS("md-personality-4"); /* RAID5 */ 8865 MODULE_ALIAS("md-raid5"); 8866 MODULE_ALIAS("md-raid4"); 8867 MODULE_ALIAS("md-level-5"); 8868 MODULE_ALIAS("md-level-4"); 8869 MODULE_ALIAS("md-personality-8"); /* RAID6 */ 8870 MODULE_ALIAS("md-raid6"); 8871 MODULE_ALIAS("md-level-6"); 8872 8873 /* This used to be two separate modules, they were: */ 8874 MODULE_ALIAS("raid5"); 8875 MODULE_ALIAS("raid6"); 8876