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