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