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