1 // SPDX-License-Identifier: GPL-2.0-or-later
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
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!
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
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
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
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
38 #include <linux/blkdev.h>
39 #include <linux/delay.h>
40 #include <linux/kthread.h>
41 #include <linux/raid/pq.h>
42 #include <linux/async_tx.h>
43 #include <linux/module.h>
44 #include <linux/async.h>
45 #include <linux/seq_file.h>
46 #include <linux/cpu.h>
47 #include <linux/slab.h>
48 #include <linux/ratelimit.h>
49 #include <linux/nodemask.h>
51 #include <trace/events/block.h>
52 #include <linux/list_sort.h>
57 #include "md-bitmap.h"
58 #include "raid5-log.h"
60 #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
62 #define cpu_to_group(cpu) cpu_to_node(cpu)
63 #define ANY_GROUP NUMA_NO_NODE
65 #define RAID5_MAX_REQ_STRIPES 256
67 static bool devices_handle_discard_safely = false;
68 module_param(devices_handle_discard_safely, bool, 0644);
69 MODULE_PARM_DESC(devices_handle_discard_safely,
70 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
71 static struct workqueue_struct *raid5_wq;
73 static void raid5_quiesce(struct mddev *mddev, int quiesce);
75 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
77 int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK;
78 return &conf->stripe_hashtbl[hash];
81 static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect)
83 return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK;
86 static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
87 __acquires(&conf->device_lock)
89 spin_lock_irq(conf->hash_locks + hash);
90 spin_lock(&conf->device_lock);
93 static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
94 __releases(&conf->device_lock)
96 spin_unlock(&conf->device_lock);
97 spin_unlock_irq(conf->hash_locks + hash);
100 static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
101 __acquires(&conf->device_lock)
104 spin_lock_irq(conf->hash_locks);
105 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
106 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
107 spin_lock(&conf->device_lock);
110 static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
111 __releases(&conf->device_lock)
114 spin_unlock(&conf->device_lock);
115 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
116 spin_unlock(conf->hash_locks + i);
117 spin_unlock_irq(conf->hash_locks);
120 /* Find first data disk in a raid6 stripe */
121 static inline int raid6_d0(struct stripe_head *sh)
124 /* ddf always start from first device */
126 /* md starts just after Q block */
127 if (sh->qd_idx == sh->disks - 1)
130 return sh->qd_idx + 1;
132 static inline int raid6_next_disk(int disk, int raid_disks)
135 return (disk < raid_disks) ? disk : 0;
138 /* When walking through the disks in a raid5, starting at raid6_d0,
139 * We need to map each disk to a 'slot', where the data disks are slot
140 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
141 * is raid_disks-1. This help does that mapping.
143 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
144 int *count, int syndrome_disks)
150 if (idx == sh->pd_idx)
151 return syndrome_disks;
152 if (idx == sh->qd_idx)
153 return syndrome_disks + 1;
159 static void print_raid5_conf (struct r5conf *conf);
161 static int stripe_operations_active(struct stripe_head *sh)
163 return sh->check_state || sh->reconstruct_state ||
164 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
165 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
168 static bool stripe_is_lowprio(struct stripe_head *sh)
170 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
171 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
172 !test_bit(STRIPE_R5C_CACHING, &sh->state);
175 static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
176 __must_hold(&sh->raid_conf->device_lock)
178 struct r5conf *conf = sh->raid_conf;
179 struct r5worker_group *group;
181 int i, cpu = sh->cpu;
183 if (!cpu_online(cpu)) {
184 cpu = cpumask_any(cpu_online_mask);
188 if (list_empty(&sh->lru)) {
189 struct r5worker_group *group;
190 group = conf->worker_groups + cpu_to_group(cpu);
191 if (stripe_is_lowprio(sh))
192 list_add_tail(&sh->lru, &group->loprio_list);
194 list_add_tail(&sh->lru, &group->handle_list);
195 group->stripes_cnt++;
199 if (conf->worker_cnt_per_group == 0) {
200 md_wakeup_thread(conf->mddev->thread);
204 group = conf->worker_groups + cpu_to_group(sh->cpu);
206 group->workers[0].working = true;
207 /* at least one worker should run to avoid race */
208 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
210 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
211 /* wakeup more workers */
212 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
213 if (group->workers[i].working == false) {
214 group->workers[i].working = true;
215 queue_work_on(sh->cpu, raid5_wq,
216 &group->workers[i].work);
222 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
223 struct list_head *temp_inactive_list)
224 __must_hold(&conf->device_lock)
227 int injournal = 0; /* number of date pages with R5_InJournal */
229 BUG_ON(!list_empty(&sh->lru));
230 BUG_ON(atomic_read(&conf->active_stripes)==0);
232 if (r5c_is_writeback(conf->log))
233 for (i = sh->disks; i--; )
234 if (test_bit(R5_InJournal, &sh->dev[i].flags))
237 * In the following cases, the stripe cannot be released to cached
238 * lists. Therefore, we make the stripe write out and set
240 * 1. when quiesce in r5c write back;
241 * 2. when resync is requested fot the stripe.
243 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
244 (conf->quiesce && r5c_is_writeback(conf->log) &&
245 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
246 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
247 r5c_make_stripe_write_out(sh);
248 set_bit(STRIPE_HANDLE, &sh->state);
251 if (test_bit(STRIPE_HANDLE, &sh->state)) {
252 if (test_bit(STRIPE_DELAYED, &sh->state) &&
253 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
254 list_add_tail(&sh->lru, &conf->delayed_list);
255 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
256 sh->bm_seq - conf->seq_write > 0)
257 list_add_tail(&sh->lru, &conf->bitmap_list);
259 clear_bit(STRIPE_DELAYED, &sh->state);
260 clear_bit(STRIPE_BIT_DELAY, &sh->state);
261 if (conf->worker_cnt_per_group == 0) {
262 if (stripe_is_lowprio(sh))
263 list_add_tail(&sh->lru,
266 list_add_tail(&sh->lru,
269 raid5_wakeup_stripe_thread(sh);
273 md_wakeup_thread(conf->mddev->thread);
275 BUG_ON(stripe_operations_active(sh));
276 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
277 if (atomic_dec_return(&conf->preread_active_stripes)
279 md_wakeup_thread(conf->mddev->thread);
280 atomic_dec(&conf->active_stripes);
281 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
282 if (!r5c_is_writeback(conf->log))
283 list_add_tail(&sh->lru, temp_inactive_list);
285 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
287 list_add_tail(&sh->lru, temp_inactive_list);
288 else if (injournal == conf->raid_disks - conf->max_degraded) {
290 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
291 atomic_inc(&conf->r5c_cached_full_stripes);
292 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
293 atomic_dec(&conf->r5c_cached_partial_stripes);
294 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
295 r5c_check_cached_full_stripe(conf);
298 * STRIPE_R5C_PARTIAL_STRIPE is set in
299 * r5c_try_caching_write(). No need to
302 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
308 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
309 struct list_head *temp_inactive_list)
310 __must_hold(&conf->device_lock)
312 if (atomic_dec_and_test(&sh->count))
313 do_release_stripe(conf, sh, temp_inactive_list);
317 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
319 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
320 * given time. Adding stripes only takes device lock, while deleting stripes
321 * only takes hash lock.
323 static void release_inactive_stripe_list(struct r5conf *conf,
324 struct list_head *temp_inactive_list,
328 bool do_wakeup = false;
331 if (hash == NR_STRIPE_HASH_LOCKS) {
332 size = NR_STRIPE_HASH_LOCKS;
333 hash = NR_STRIPE_HASH_LOCKS - 1;
337 struct list_head *list = &temp_inactive_list[size - 1];
340 * We don't hold any lock here yet, raid5_get_active_stripe() might
341 * remove stripes from the list
343 if (!list_empty_careful(list)) {
344 spin_lock_irqsave(conf->hash_locks + hash, flags);
345 if (list_empty(conf->inactive_list + hash) &&
347 atomic_dec(&conf->empty_inactive_list_nr);
348 list_splice_tail_init(list, conf->inactive_list + hash);
350 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
357 wake_up(&conf->wait_for_stripe);
358 if (atomic_read(&conf->active_stripes) == 0)
359 wake_up(&conf->wait_for_quiescent);
360 if (conf->retry_read_aligned)
361 md_wakeup_thread(conf->mddev->thread);
365 static int release_stripe_list(struct r5conf *conf,
366 struct list_head *temp_inactive_list)
367 __must_hold(&conf->device_lock)
369 struct stripe_head *sh, *t;
371 struct llist_node *head;
373 head = llist_del_all(&conf->released_stripes);
374 head = llist_reverse_order(head);
375 llist_for_each_entry_safe(sh, t, head, release_list) {
378 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
380 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
382 * Don't worry the bit is set here, because if the bit is set
383 * again, the count is always > 1. This is true for
384 * STRIPE_ON_UNPLUG_LIST bit too.
386 hash = sh->hash_lock_index;
387 __release_stripe(conf, sh, &temp_inactive_list[hash]);
394 void raid5_release_stripe(struct stripe_head *sh)
396 struct r5conf *conf = sh->raid_conf;
398 struct list_head list;
402 /* Avoid release_list until the last reference.
404 if (atomic_add_unless(&sh->count, -1, 1))
407 if (unlikely(!conf->mddev->thread) ||
408 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
410 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
412 md_wakeup_thread(conf->mddev->thread);
415 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
416 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
417 INIT_LIST_HEAD(&list);
418 hash = sh->hash_lock_index;
419 do_release_stripe(conf, sh, &list);
420 spin_unlock_irqrestore(&conf->device_lock, flags);
421 release_inactive_stripe_list(conf, &list, hash);
425 static inline void remove_hash(struct stripe_head *sh)
427 pr_debug("remove_hash(), stripe %llu\n",
428 (unsigned long long)sh->sector);
430 hlist_del_init(&sh->hash);
433 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
435 struct hlist_head *hp = stripe_hash(conf, sh->sector);
437 pr_debug("insert_hash(), stripe %llu\n",
438 (unsigned long long)sh->sector);
440 hlist_add_head(&sh->hash, hp);
443 /* find an idle stripe, make sure it is unhashed, and return it. */
444 static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
446 struct stripe_head *sh = NULL;
447 struct list_head *first;
449 if (list_empty(conf->inactive_list + hash))
451 first = (conf->inactive_list + hash)->next;
452 sh = list_entry(first, struct stripe_head, lru);
453 list_del_init(first);
455 atomic_inc(&conf->active_stripes);
456 BUG_ON(hash != sh->hash_lock_index);
457 if (list_empty(conf->inactive_list + hash))
458 atomic_inc(&conf->empty_inactive_list_nr);
463 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
464 static void free_stripe_pages(struct stripe_head *sh)
469 /* Have not allocate page pool */
473 for (i = 0; i < sh->nr_pages; i++) {
481 static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp)
486 for (i = 0; i < sh->nr_pages; i++) {
487 /* The page have allocated. */
493 free_stripe_pages(sh);
502 init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks)
509 /* Each of the sh->dev[i] need one conf->stripe_size */
510 cnt = PAGE_SIZE / conf->stripe_size;
511 nr_pages = (disks + cnt - 1) / cnt;
513 sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
516 sh->nr_pages = nr_pages;
517 sh->stripes_per_page = cnt;
522 static void shrink_buffers(struct stripe_head *sh)
525 int num = sh->raid_conf->pool_size;
527 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE
528 for (i = 0; i < num ; i++) {
531 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
535 sh->dev[i].page = NULL;
539 for (i = 0; i < num; i++)
540 sh->dev[i].page = NULL;
541 free_stripe_pages(sh); /* Free pages */
545 static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
548 int num = sh->raid_conf->pool_size;
550 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE
551 for (i = 0; i < num; i++) {
554 if (!(page = alloc_page(gfp))) {
557 sh->dev[i].page = page;
558 sh->dev[i].orig_page = page;
559 sh->dev[i].offset = 0;
562 if (alloc_stripe_pages(sh, gfp))
565 for (i = 0; i < num; i++) {
566 sh->dev[i].page = raid5_get_dev_page(sh, i);
567 sh->dev[i].orig_page = sh->dev[i].page;
568 sh->dev[i].offset = raid5_get_page_offset(sh, i);
574 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
575 struct stripe_head *sh);
577 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
579 struct r5conf *conf = sh->raid_conf;
582 BUG_ON(atomic_read(&sh->count) != 0);
583 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
584 BUG_ON(stripe_operations_active(sh));
585 BUG_ON(sh->batch_head);
587 pr_debug("init_stripe called, stripe %llu\n",
588 (unsigned long long)sector);
590 seq = read_seqcount_begin(&conf->gen_lock);
591 sh->generation = conf->generation - previous;
592 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
594 stripe_set_idx(sector, conf, previous, sh);
597 for (i = sh->disks; i--; ) {
598 struct r5dev *dev = &sh->dev[i];
600 if (dev->toread || dev->read || dev->towrite || dev->written ||
601 test_bit(R5_LOCKED, &dev->flags)) {
602 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
603 (unsigned long long)sh->sector, i, dev->toread,
604 dev->read, dev->towrite, dev->written,
605 test_bit(R5_LOCKED, &dev->flags));
609 dev->sector = raid5_compute_blocknr(sh, i, previous);
611 if (read_seqcount_retry(&conf->gen_lock, seq))
613 sh->overwrite_disks = 0;
614 insert_hash(conf, sh);
615 sh->cpu = smp_processor_id();
616 set_bit(STRIPE_BATCH_READY, &sh->state);
619 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
622 struct stripe_head *sh;
624 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
625 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
626 if (sh->sector == sector && sh->generation == generation)
628 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
632 static struct stripe_head *find_get_stripe(struct r5conf *conf,
633 sector_t sector, short generation, int hash)
635 int inc_empty_inactive_list_flag;
636 struct stripe_head *sh;
638 sh = __find_stripe(conf, sector, generation);
642 if (atomic_inc_not_zero(&sh->count))
646 * Slow path. The reference count is zero which means the stripe must
647 * be on a list (sh->lru). Must remove the stripe from the list that
648 * references it with the device_lock held.
651 spin_lock(&conf->device_lock);
652 if (!atomic_read(&sh->count)) {
653 if (!test_bit(STRIPE_HANDLE, &sh->state))
654 atomic_inc(&conf->active_stripes);
655 BUG_ON(list_empty(&sh->lru) &&
656 !test_bit(STRIPE_EXPANDING, &sh->state));
657 inc_empty_inactive_list_flag = 0;
658 if (!list_empty(conf->inactive_list + hash))
659 inc_empty_inactive_list_flag = 1;
660 list_del_init(&sh->lru);
661 if (list_empty(conf->inactive_list + hash) &&
662 inc_empty_inactive_list_flag)
663 atomic_inc(&conf->empty_inactive_list_nr);
665 sh->group->stripes_cnt--;
669 atomic_inc(&sh->count);
670 spin_unlock(&conf->device_lock);
676 * Need to check if array has failed when deciding whether to:
678 * - remove non-faulty devices
681 * This determination is simple when no reshape is happening.
682 * However if there is a reshape, we need to carefully check
683 * both the before and after sections.
684 * This is because some failed devices may only affect one
685 * of the two sections, and some non-in_sync devices may
686 * be insync in the section most affected by failed devices.
688 * Most calls to this function hold &conf->device_lock. Calls
689 * in raid5_run() do not require the lock as no other threads
690 * have been started yet.
692 int raid5_calc_degraded(struct r5conf *conf)
694 int degraded, degraded2;
699 for (i = 0; i < conf->previous_raid_disks; i++) {
700 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
701 if (rdev && test_bit(Faulty, &rdev->flags))
702 rdev = rcu_dereference(conf->disks[i].replacement);
703 if (!rdev || test_bit(Faulty, &rdev->flags))
705 else if (test_bit(In_sync, &rdev->flags))
708 /* not in-sync or faulty.
709 * If the reshape increases the number of devices,
710 * this is being recovered by the reshape, so
711 * this 'previous' section is not in_sync.
712 * If the number of devices is being reduced however,
713 * the device can only be part of the array if
714 * we are reverting a reshape, so this section will
717 if (conf->raid_disks >= conf->previous_raid_disks)
721 if (conf->raid_disks == conf->previous_raid_disks)
725 for (i = 0; i < conf->raid_disks; i++) {
726 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
727 if (rdev && test_bit(Faulty, &rdev->flags))
728 rdev = rcu_dereference(conf->disks[i].replacement);
729 if (!rdev || test_bit(Faulty, &rdev->flags))
731 else if (test_bit(In_sync, &rdev->flags))
734 /* not in-sync or faulty.
735 * If reshape increases the number of devices, this
736 * section has already been recovered, else it
737 * almost certainly hasn't.
739 if (conf->raid_disks <= conf->previous_raid_disks)
743 if (degraded2 > degraded)
748 static bool has_failed(struct r5conf *conf)
750 int degraded = conf->mddev->degraded;
752 if (test_bit(MD_BROKEN, &conf->mddev->flags))
755 if (conf->mddev->reshape_position != MaxSector)
756 degraded = raid5_calc_degraded(conf);
758 return degraded > conf->max_degraded;
764 STRIPE_SCHEDULE_AND_RETRY,
768 struct stripe_request_ctx {
769 /* a reference to the last stripe_head for batching */
770 struct stripe_head *batch_last;
772 /* first sector in the request */
773 sector_t first_sector;
775 /* last sector in the request */
776 sector_t last_sector;
779 * bitmap to track stripe sectors that have been added to stripes
780 * add one to account for unaligned requests
782 DECLARE_BITMAP(sectors_to_do, RAID5_MAX_REQ_STRIPES + 1);
784 /* the request had REQ_PREFLUSH, cleared after the first stripe_head */
789 * Block until another thread clears R5_INACTIVE_BLOCKED or
790 * there are fewer than 3/4 the maximum number of active stripes
791 * and there is an inactive stripe available.
793 static bool is_inactive_blocked(struct r5conf *conf, int hash)
795 if (list_empty(conf->inactive_list + hash))
798 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
801 return (atomic_read(&conf->active_stripes) <
802 (conf->max_nr_stripes * 3 / 4));
805 struct stripe_head *raid5_get_active_stripe(struct r5conf *conf,
806 struct stripe_request_ctx *ctx, sector_t sector,
809 struct stripe_head *sh;
810 int hash = stripe_hash_locks_hash(conf, sector);
811 int previous = !!(flags & R5_GAS_PREVIOUS);
813 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
815 spin_lock_irq(conf->hash_locks + hash);
818 if (!(flags & R5_GAS_NOQUIESCE) && conf->quiesce) {
820 * Must release the reference to batch_last before
821 * waiting, on quiesce, otherwise the batch_last will
822 * hold a reference to a stripe and raid5_quiesce()
823 * will deadlock waiting for active_stripes to go to
826 if (ctx && ctx->batch_last) {
827 raid5_release_stripe(ctx->batch_last);
828 ctx->batch_last = NULL;
831 wait_event_lock_irq(conf->wait_for_quiescent,
833 *(conf->hash_locks + hash));
836 sh = find_get_stripe(conf, sector, conf->generation - previous,
841 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
842 sh = get_free_stripe(conf, hash);
844 r5c_check_stripe_cache_usage(conf);
845 init_stripe(sh, sector, previous);
846 atomic_inc(&sh->count);
850 if (!test_bit(R5_DID_ALLOC, &conf->cache_state))
851 set_bit(R5_ALLOC_MORE, &conf->cache_state);
854 if (flags & R5_GAS_NOBLOCK)
857 set_bit(R5_INACTIVE_BLOCKED, &conf->cache_state);
858 r5l_wake_reclaim(conf->log, 0);
860 /* release batch_last before wait to avoid risk of deadlock */
861 if (ctx && ctx->batch_last) {
862 raid5_release_stripe(ctx->batch_last);
863 ctx->batch_last = NULL;
866 wait_event_lock_irq(conf->wait_for_stripe,
867 is_inactive_blocked(conf, hash),
868 *(conf->hash_locks + hash));
869 clear_bit(R5_INACTIVE_BLOCKED, &conf->cache_state);
872 spin_unlock_irq(conf->hash_locks + hash);
876 static bool is_full_stripe_write(struct stripe_head *sh)
878 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
879 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
882 static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
883 __acquires(&sh1->stripe_lock)
884 __acquires(&sh2->stripe_lock)
887 spin_lock_irq(&sh2->stripe_lock);
888 spin_lock_nested(&sh1->stripe_lock, 1);
890 spin_lock_irq(&sh1->stripe_lock);
891 spin_lock_nested(&sh2->stripe_lock, 1);
895 static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
896 __releases(&sh1->stripe_lock)
897 __releases(&sh2->stripe_lock)
899 spin_unlock(&sh1->stripe_lock);
900 spin_unlock_irq(&sh2->stripe_lock);
903 /* Only freshly new full stripe normal write stripe can be added to a batch list */
904 static bool stripe_can_batch(struct stripe_head *sh)
906 struct r5conf *conf = sh->raid_conf;
908 if (raid5_has_log(conf) || raid5_has_ppl(conf))
910 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
911 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
912 is_full_stripe_write(sh);
915 /* we only do back search */
916 static void stripe_add_to_batch_list(struct r5conf *conf,
917 struct stripe_head *sh, struct stripe_head *last_sh)
919 struct stripe_head *head;
920 sector_t head_sector, tmp_sec;
924 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
925 tmp_sec = sh->sector;
926 if (!sector_div(tmp_sec, conf->chunk_sectors))
928 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
930 if (last_sh && head_sector == last_sh->sector) {
932 atomic_inc(&head->count);
934 hash = stripe_hash_locks_hash(conf, head_sector);
935 spin_lock_irq(conf->hash_locks + hash);
936 head = find_get_stripe(conf, head_sector, conf->generation,
938 spin_unlock_irq(conf->hash_locks + hash);
941 if (!stripe_can_batch(head))
945 lock_two_stripes(head, sh);
946 /* clear_batch_ready clear the flag */
947 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
954 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
956 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
957 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
960 if (head->batch_head) {
961 spin_lock(&head->batch_head->batch_lock);
962 /* This batch list is already running */
963 if (!stripe_can_batch(head)) {
964 spin_unlock(&head->batch_head->batch_lock);
968 * We must assign batch_head of this stripe within the
969 * batch_lock, otherwise clear_batch_ready of batch head
970 * stripe could clear BATCH_READY bit of this stripe and
971 * this stripe->batch_head doesn't get assigned, which
972 * could confuse clear_batch_ready for this stripe
974 sh->batch_head = head->batch_head;
977 * at this point, head's BATCH_READY could be cleared, but we
978 * can still add the stripe to batch list
980 list_add(&sh->batch_list, &head->batch_list);
981 spin_unlock(&head->batch_head->batch_lock);
983 head->batch_head = head;
984 sh->batch_head = head->batch_head;
985 spin_lock(&head->batch_lock);
986 list_add_tail(&sh->batch_list, &head->batch_list);
987 spin_unlock(&head->batch_lock);
990 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
991 if (atomic_dec_return(&conf->preread_active_stripes)
993 md_wakeup_thread(conf->mddev->thread);
995 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
996 int seq = sh->bm_seq;
997 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
998 sh->batch_head->bm_seq > seq)
999 seq = sh->batch_head->bm_seq;
1000 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
1001 sh->batch_head->bm_seq = seq;
1004 atomic_inc(&sh->count);
1006 unlock_two_stripes(head, sh);
1008 raid5_release_stripe(head);
1011 /* Determine if 'data_offset' or 'new_data_offset' should be used
1012 * in this stripe_head.
1014 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
1016 sector_t progress = conf->reshape_progress;
1017 /* Need a memory barrier to make sure we see the value
1018 * of conf->generation, or ->data_offset that was set before
1019 * reshape_progress was updated.
1022 if (progress == MaxSector)
1024 if (sh->generation == conf->generation - 1)
1026 /* We are in a reshape, and this is a new-generation stripe,
1027 * so use new_data_offset.
1032 static void dispatch_bio_list(struct bio_list *tmp)
1036 while ((bio = bio_list_pop(tmp)))
1037 submit_bio_noacct(bio);
1040 static int cmp_stripe(void *priv, const struct list_head *a,
1041 const struct list_head *b)
1043 const struct r5pending_data *da = list_entry(a,
1044 struct r5pending_data, sibling);
1045 const struct r5pending_data *db = list_entry(b,
1046 struct r5pending_data, sibling);
1047 if (da->sector > db->sector)
1049 if (da->sector < db->sector)
1054 static void dispatch_defer_bios(struct r5conf *conf, int target,
1055 struct bio_list *list)
1057 struct r5pending_data *data;
1058 struct list_head *first, *next = NULL;
1061 if (conf->pending_data_cnt == 0)
1064 list_sort(NULL, &conf->pending_list, cmp_stripe);
1066 first = conf->pending_list.next;
1068 /* temporarily move the head */
1069 if (conf->next_pending_data)
1070 list_move_tail(&conf->pending_list,
1071 &conf->next_pending_data->sibling);
1073 while (!list_empty(&conf->pending_list)) {
1074 data = list_first_entry(&conf->pending_list,
1075 struct r5pending_data, sibling);
1076 if (&data->sibling == first)
1077 first = data->sibling.next;
1078 next = data->sibling.next;
1080 bio_list_merge(list, &data->bios);
1081 list_move(&data->sibling, &conf->free_list);
1086 conf->pending_data_cnt -= cnt;
1087 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
1089 if (next != &conf->pending_list)
1090 conf->next_pending_data = list_entry(next,
1091 struct r5pending_data, sibling);
1093 conf->next_pending_data = NULL;
1094 /* list isn't empty */
1095 if (first != &conf->pending_list)
1096 list_move_tail(&conf->pending_list, first);
1099 static void flush_deferred_bios(struct r5conf *conf)
1101 struct bio_list tmp = BIO_EMPTY_LIST;
1103 if (conf->pending_data_cnt == 0)
1106 spin_lock(&conf->pending_bios_lock);
1107 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
1108 BUG_ON(conf->pending_data_cnt != 0);
1109 spin_unlock(&conf->pending_bios_lock);
1111 dispatch_bio_list(&tmp);
1114 static void defer_issue_bios(struct r5conf *conf, sector_t sector,
1115 struct bio_list *bios)
1117 struct bio_list tmp = BIO_EMPTY_LIST;
1118 struct r5pending_data *ent;
1120 spin_lock(&conf->pending_bios_lock);
1121 ent = list_first_entry(&conf->free_list, struct r5pending_data,
1123 list_move_tail(&ent->sibling, &conf->pending_list);
1124 ent->sector = sector;
1125 bio_list_init(&ent->bios);
1126 bio_list_merge(&ent->bios, bios);
1127 conf->pending_data_cnt++;
1128 if (conf->pending_data_cnt >= PENDING_IO_MAX)
1129 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
1131 spin_unlock(&conf->pending_bios_lock);
1133 dispatch_bio_list(&tmp);
1137 raid5_end_read_request(struct bio *bi);
1139 raid5_end_write_request(struct bio *bi);
1141 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
1143 struct r5conf *conf = sh->raid_conf;
1144 int i, disks = sh->disks;
1145 struct stripe_head *head_sh = sh;
1146 struct bio_list pending_bios = BIO_EMPTY_LIST;
1152 if (log_stripe(sh, s) == 0)
1155 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
1157 for (i = disks; i--; ) {
1159 blk_opf_t op_flags = 0;
1160 int replace_only = 0;
1161 struct bio *bi, *rbi;
1162 struct md_rdev *rdev, *rrdev = NULL;
1165 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
1167 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
1169 if (test_bit(R5_Discard, &sh->dev[i].flags))
1170 op = REQ_OP_DISCARD;
1171 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1173 else if (test_and_clear_bit(R5_WantReplace,
1174 &sh->dev[i].flags)) {
1179 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
1180 op_flags |= REQ_SYNC;
1185 rbi = &dev->rreq; /* For writing to replacement */
1188 rrdev = rcu_dereference(conf->disks[i].replacement);
1189 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1190 rdev = rcu_dereference(conf->disks[i].rdev);
1195 if (op_is_write(op)) {
1199 /* We raced and saw duplicates */
1202 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
1207 if (rdev && test_bit(Faulty, &rdev->flags))
1210 atomic_inc(&rdev->nr_pending);
1211 if (rrdev && test_bit(Faulty, &rrdev->flags))
1214 atomic_inc(&rrdev->nr_pending);
1217 /* We have already checked bad blocks for reads. Now
1218 * need to check for writes. We never accept write errors
1219 * on the replacement, so we don't to check rrdev.
1221 while (op_is_write(op) && rdev &&
1222 test_bit(WriteErrorSeen, &rdev->flags)) {
1225 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
1226 &first_bad, &bad_sectors);
1231 set_bit(BlockedBadBlocks, &rdev->flags);
1232 if (!conf->mddev->external &&
1233 conf->mddev->sb_flags) {
1234 /* It is very unlikely, but we might
1235 * still need to write out the
1236 * bad block log - better give it
1238 md_check_recovery(conf->mddev);
1241 * Because md_wait_for_blocked_rdev
1242 * will dec nr_pending, we must
1243 * increment it first.
1245 atomic_inc(&rdev->nr_pending);
1246 md_wait_for_blocked_rdev(rdev, conf->mddev);
1248 /* Acknowledged bad block - skip the write */
1249 rdev_dec_pending(rdev, conf->mddev);
1255 if (s->syncing || s->expanding || s->expanded
1257 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
1259 set_bit(STRIPE_IO_STARTED, &sh->state);
1261 bio_init(bi, rdev->bdev, &dev->vec, 1, op | op_flags);
1262 bi->bi_end_io = op_is_write(op)
1263 ? raid5_end_write_request
1264 : raid5_end_read_request;
1265 bi->bi_private = sh;
1267 pr_debug("%s: for %llu schedule op %d on disc %d\n",
1268 __func__, (unsigned long long)sh->sector,
1270 atomic_inc(&sh->count);
1272 atomic_inc(&head_sh->count);
1273 if (use_new_offset(conf, sh))
1274 bi->bi_iter.bi_sector = (sh->sector
1275 + rdev->new_data_offset);
1277 bi->bi_iter.bi_sector = (sh->sector
1278 + rdev->data_offset);
1279 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
1280 bi->bi_opf |= REQ_NOMERGE;
1282 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1283 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1285 if (!op_is_write(op) &&
1286 test_bit(R5_InJournal, &sh->dev[i].flags))
1288 * issuing read for a page in journal, this
1289 * must be preparing for prexor in rmw; read
1290 * the data into orig_page
1292 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1294 sh->dev[i].vec.bv_page = sh->dev[i].page;
1296 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
1297 bi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
1298 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
1300 * If this is discard request, set bi_vcnt 0. We don't
1301 * want to confuse SCSI because SCSI will replace payload
1303 if (op == REQ_OP_DISCARD)
1306 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
1308 if (conf->mddev->gendisk)
1309 trace_block_bio_remap(bi,
1310 disk_devt(conf->mddev->gendisk),
1312 if (should_defer && op_is_write(op))
1313 bio_list_add(&pending_bios, bi);
1315 submit_bio_noacct(bi);
1318 if (s->syncing || s->expanding || s->expanded
1320 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
1322 set_bit(STRIPE_IO_STARTED, &sh->state);
1324 bio_init(rbi, rrdev->bdev, &dev->rvec, 1, op | op_flags);
1325 BUG_ON(!op_is_write(op));
1326 rbi->bi_end_io = raid5_end_write_request;
1327 rbi->bi_private = sh;
1329 pr_debug("%s: for %llu schedule op %d on "
1330 "replacement disc %d\n",
1331 __func__, (unsigned long long)sh->sector,
1333 atomic_inc(&sh->count);
1335 atomic_inc(&head_sh->count);
1336 if (use_new_offset(conf, sh))
1337 rbi->bi_iter.bi_sector = (sh->sector
1338 + rrdev->new_data_offset);
1340 rbi->bi_iter.bi_sector = (sh->sector
1341 + rrdev->data_offset);
1342 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1343 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1344 sh->dev[i].rvec.bv_page = sh->dev[i].page;
1346 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
1347 rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
1348 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
1350 * If this is discard request, set bi_vcnt 0. We don't
1351 * want to confuse SCSI because SCSI will replace payload
1353 if (op == REQ_OP_DISCARD)
1355 if (conf->mddev->gendisk)
1356 trace_block_bio_remap(rbi,
1357 disk_devt(conf->mddev->gendisk),
1359 if (should_defer && op_is_write(op))
1360 bio_list_add(&pending_bios, rbi);
1362 submit_bio_noacct(rbi);
1364 if (!rdev && !rrdev) {
1365 if (op_is_write(op))
1366 set_bit(STRIPE_DEGRADED, &sh->state);
1367 pr_debug("skip op %d on disc %d for sector %llu\n",
1368 bi->bi_opf, i, (unsigned long long)sh->sector);
1369 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1370 set_bit(STRIPE_HANDLE, &sh->state);
1373 if (!head_sh->batch_head)
1375 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1381 if (should_defer && !bio_list_empty(&pending_bios))
1382 defer_issue_bios(conf, head_sh->sector, &pending_bios);
1385 static struct dma_async_tx_descriptor *
1386 async_copy_data(int frombio, struct bio *bio, struct page **page,
1387 unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx,
1388 struct stripe_head *sh, int no_skipcopy)
1391 struct bvec_iter iter;
1392 struct page *bio_page;
1394 struct async_submit_ctl submit;
1395 enum async_tx_flags flags = 0;
1396 struct r5conf *conf = sh->raid_conf;
1398 if (bio->bi_iter.bi_sector >= sector)
1399 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
1401 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
1404 flags |= ASYNC_TX_FENCE;
1405 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1407 bio_for_each_segment(bvl, bio, iter) {
1408 int len = bvl.bv_len;
1412 if (page_offset < 0) {
1413 b_offset = -page_offset;
1414 page_offset += b_offset;
1418 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1419 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
1424 b_offset += bvl.bv_offset;
1425 bio_page = bvl.bv_page;
1427 if (conf->skip_copy &&
1428 b_offset == 0 && page_offset == 0 &&
1429 clen == RAID5_STRIPE_SIZE(conf) &&
1433 tx = async_memcpy(*page, bio_page, page_offset + poff,
1434 b_offset, clen, &submit);
1436 tx = async_memcpy(bio_page, *page, b_offset,
1437 page_offset + poff, clen, &submit);
1439 /* chain the operations */
1440 submit.depend_tx = tx;
1442 if (clen < len) /* hit end of page */
1450 static void ops_complete_biofill(void *stripe_head_ref)
1452 struct stripe_head *sh = stripe_head_ref;
1454 struct r5conf *conf = sh->raid_conf;
1456 pr_debug("%s: stripe %llu\n", __func__,
1457 (unsigned long long)sh->sector);
1459 /* clear completed biofills */
1460 for (i = sh->disks; i--; ) {
1461 struct r5dev *dev = &sh->dev[i];
1463 /* acknowledge completion of a biofill operation */
1464 /* and check if we need to reply to a read request,
1465 * new R5_Wantfill requests are held off until
1466 * !STRIPE_BIOFILL_RUN
1468 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
1469 struct bio *rbi, *rbi2;
1474 while (rbi && rbi->bi_iter.bi_sector <
1475 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1476 rbi2 = r5_next_bio(conf, rbi, dev->sector);
1482 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
1484 set_bit(STRIPE_HANDLE, &sh->state);
1485 raid5_release_stripe(sh);
1488 static void ops_run_biofill(struct stripe_head *sh)
1490 struct dma_async_tx_descriptor *tx = NULL;
1491 struct async_submit_ctl submit;
1493 struct r5conf *conf = sh->raid_conf;
1495 BUG_ON(sh->batch_head);
1496 pr_debug("%s: stripe %llu\n", __func__,
1497 (unsigned long long)sh->sector);
1499 for (i = sh->disks; i--; ) {
1500 struct r5dev *dev = &sh->dev[i];
1501 if (test_bit(R5_Wantfill, &dev->flags)) {
1503 spin_lock_irq(&sh->stripe_lock);
1504 dev->read = rbi = dev->toread;
1506 spin_unlock_irq(&sh->stripe_lock);
1507 while (rbi && rbi->bi_iter.bi_sector <
1508 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1509 tx = async_copy_data(0, rbi, &dev->page,
1511 dev->sector, tx, sh, 0);
1512 rbi = r5_next_bio(conf, rbi, dev->sector);
1517 atomic_inc(&sh->count);
1518 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1519 async_trigger_callback(&submit);
1522 static void mark_target_uptodate(struct stripe_head *sh, int target)
1529 tgt = &sh->dev[target];
1530 set_bit(R5_UPTODATE, &tgt->flags);
1531 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1532 clear_bit(R5_Wantcompute, &tgt->flags);
1535 static void ops_complete_compute(void *stripe_head_ref)
1537 struct stripe_head *sh = stripe_head_ref;
1539 pr_debug("%s: stripe %llu\n", __func__,
1540 (unsigned long long)sh->sector);
1542 /* mark the computed target(s) as uptodate */
1543 mark_target_uptodate(sh, sh->ops.target);
1544 mark_target_uptodate(sh, sh->ops.target2);
1546 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1547 if (sh->check_state == check_state_compute_run)
1548 sh->check_state = check_state_compute_result;
1549 set_bit(STRIPE_HANDLE, &sh->state);
1550 raid5_release_stripe(sh);
1553 /* return a pointer to the address conversion region of the scribble buffer */
1554 static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1556 return percpu->scribble + i * percpu->scribble_obj_size;
1559 /* return a pointer to the address conversion region of the scribble buffer */
1560 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1561 struct raid5_percpu *percpu, int i)
1563 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
1567 * Return a pointer to record offset address.
1569 static unsigned int *
1570 to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu)
1572 return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2);
1575 static struct dma_async_tx_descriptor *
1576 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1578 int disks = sh->disks;
1579 struct page **xor_srcs = to_addr_page(percpu, 0);
1580 unsigned int *off_srcs = to_addr_offs(sh, percpu);
1581 int target = sh->ops.target;
1582 struct r5dev *tgt = &sh->dev[target];
1583 struct page *xor_dest = tgt->page;
1584 unsigned int off_dest = tgt->offset;
1586 struct dma_async_tx_descriptor *tx;
1587 struct async_submit_ctl submit;
1590 BUG_ON(sh->batch_head);
1592 pr_debug("%s: stripe %llu block: %d\n",
1593 __func__, (unsigned long long)sh->sector, target);
1594 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1596 for (i = disks; i--; ) {
1598 off_srcs[count] = sh->dev[i].offset;
1599 xor_srcs[count++] = sh->dev[i].page;
1603 atomic_inc(&sh->count);
1605 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
1606 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
1607 if (unlikely(count == 1))
1608 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
1609 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1611 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
1612 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1617 /* set_syndrome_sources - populate source buffers for gen_syndrome
1618 * @srcs - (struct page *) array of size sh->disks
1619 * @offs - (unsigned int) array of offset for each page
1620 * @sh - stripe_head to parse
1622 * Populates srcs in proper layout order for the stripe and returns the
1623 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1624 * destination buffer is recorded in srcs[count] and the Q destination
1625 * is recorded in srcs[count+1]].
1627 static int set_syndrome_sources(struct page **srcs,
1629 struct stripe_head *sh,
1632 int disks = sh->disks;
1633 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1634 int d0_idx = raid6_d0(sh);
1638 for (i = 0; i < disks; i++)
1644 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1645 struct r5dev *dev = &sh->dev[i];
1647 if (i == sh->qd_idx || i == sh->pd_idx ||
1648 (srctype == SYNDROME_SRC_ALL) ||
1649 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1650 (test_bit(R5_Wantdrain, &dev->flags) ||
1651 test_bit(R5_InJournal, &dev->flags))) ||
1652 (srctype == SYNDROME_SRC_WRITTEN &&
1654 test_bit(R5_InJournal, &dev->flags)))) {
1655 if (test_bit(R5_InJournal, &dev->flags))
1656 srcs[slot] = sh->dev[i].orig_page;
1658 srcs[slot] = sh->dev[i].page;
1660 * For R5_InJournal, PAGE_SIZE must be 4KB and will
1661 * not shared page. In that case, dev[i].offset
1664 offs[slot] = sh->dev[i].offset;
1666 i = raid6_next_disk(i, disks);
1667 } while (i != d0_idx);
1669 return syndrome_disks;
1672 static struct dma_async_tx_descriptor *
1673 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1675 int disks = sh->disks;
1676 struct page **blocks = to_addr_page(percpu, 0);
1677 unsigned int *offs = to_addr_offs(sh, percpu);
1679 int qd_idx = sh->qd_idx;
1680 struct dma_async_tx_descriptor *tx;
1681 struct async_submit_ctl submit;
1684 unsigned int dest_off;
1688 BUG_ON(sh->batch_head);
1689 if (sh->ops.target < 0)
1690 target = sh->ops.target2;
1691 else if (sh->ops.target2 < 0)
1692 target = sh->ops.target;
1694 /* we should only have one valid target */
1697 pr_debug("%s: stripe %llu block: %d\n",
1698 __func__, (unsigned long long)sh->sector, target);
1700 tgt = &sh->dev[target];
1701 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1703 dest_off = tgt->offset;
1705 atomic_inc(&sh->count);
1707 if (target == qd_idx) {
1708 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
1709 blocks[count] = NULL; /* regenerating p is not necessary */
1710 BUG_ON(blocks[count+1] != dest); /* q should already be set */
1711 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1712 ops_complete_compute, sh,
1713 to_addr_conv(sh, percpu, 0));
1714 tx = async_gen_syndrome(blocks, offs, count+2,
1715 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1717 /* Compute any data- or p-drive using XOR */
1719 for (i = disks; i-- ; ) {
1720 if (i == target || i == qd_idx)
1722 offs[count] = sh->dev[i].offset;
1723 blocks[count++] = sh->dev[i].page;
1726 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1727 NULL, ops_complete_compute, sh,
1728 to_addr_conv(sh, percpu, 0));
1729 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
1730 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1736 static struct dma_async_tx_descriptor *
1737 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1739 int i, count, disks = sh->disks;
1740 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1741 int d0_idx = raid6_d0(sh);
1742 int faila = -1, failb = -1;
1743 int target = sh->ops.target;
1744 int target2 = sh->ops.target2;
1745 struct r5dev *tgt = &sh->dev[target];
1746 struct r5dev *tgt2 = &sh->dev[target2];
1747 struct dma_async_tx_descriptor *tx;
1748 struct page **blocks = to_addr_page(percpu, 0);
1749 unsigned int *offs = to_addr_offs(sh, percpu);
1750 struct async_submit_ctl submit;
1752 BUG_ON(sh->batch_head);
1753 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1754 __func__, (unsigned long long)sh->sector, target, target2);
1755 BUG_ON(target < 0 || target2 < 0);
1756 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1757 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1759 /* we need to open-code set_syndrome_sources to handle the
1760 * slot number conversion for 'faila' and 'failb'
1762 for (i = 0; i < disks ; i++) {
1769 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1771 offs[slot] = sh->dev[i].offset;
1772 blocks[slot] = sh->dev[i].page;
1778 i = raid6_next_disk(i, disks);
1779 } while (i != d0_idx);
1781 BUG_ON(faila == failb);
1784 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1785 __func__, (unsigned long long)sh->sector, faila, failb);
1787 atomic_inc(&sh->count);
1789 if (failb == syndrome_disks+1) {
1790 /* Q disk is one of the missing disks */
1791 if (faila == syndrome_disks) {
1792 /* Missing P+Q, just recompute */
1793 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1794 ops_complete_compute, sh,
1795 to_addr_conv(sh, percpu, 0));
1796 return async_gen_syndrome(blocks, offs, syndrome_disks+2,
1797 RAID5_STRIPE_SIZE(sh->raid_conf),
1801 unsigned int dest_off;
1803 int qd_idx = sh->qd_idx;
1805 /* Missing D+Q: recompute D from P, then recompute Q */
1806 if (target == qd_idx)
1807 data_target = target2;
1809 data_target = target;
1812 for (i = disks; i-- ; ) {
1813 if (i == data_target || i == qd_idx)
1815 offs[count] = sh->dev[i].offset;
1816 blocks[count++] = sh->dev[i].page;
1818 dest = sh->dev[data_target].page;
1819 dest_off = sh->dev[data_target].offset;
1820 init_async_submit(&submit,
1821 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1823 to_addr_conv(sh, percpu, 0));
1824 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
1825 RAID5_STRIPE_SIZE(sh->raid_conf),
1828 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
1829 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1830 ops_complete_compute, sh,
1831 to_addr_conv(sh, percpu, 0));
1832 return async_gen_syndrome(blocks, offs, count+2,
1833 RAID5_STRIPE_SIZE(sh->raid_conf),
1837 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1838 ops_complete_compute, sh,
1839 to_addr_conv(sh, percpu, 0));
1840 if (failb == syndrome_disks) {
1841 /* We're missing D+P. */
1842 return async_raid6_datap_recov(syndrome_disks+2,
1843 RAID5_STRIPE_SIZE(sh->raid_conf),
1845 blocks, offs, &submit);
1847 /* We're missing D+D. */
1848 return async_raid6_2data_recov(syndrome_disks+2,
1849 RAID5_STRIPE_SIZE(sh->raid_conf),
1851 blocks, offs, &submit);
1856 static void ops_complete_prexor(void *stripe_head_ref)
1858 struct stripe_head *sh = stripe_head_ref;
1860 pr_debug("%s: stripe %llu\n", __func__,
1861 (unsigned long long)sh->sector);
1863 if (r5c_is_writeback(sh->raid_conf->log))
1865 * raid5-cache write back uses orig_page during prexor.
1866 * After prexor, it is time to free orig_page
1868 r5c_release_extra_page(sh);
1871 static struct dma_async_tx_descriptor *
1872 ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1873 struct dma_async_tx_descriptor *tx)
1875 int disks = sh->disks;
1876 struct page **xor_srcs = to_addr_page(percpu, 0);
1877 unsigned int *off_srcs = to_addr_offs(sh, percpu);
1878 int count = 0, pd_idx = sh->pd_idx, i;
1879 struct async_submit_ctl submit;
1881 /* existing parity data subtracted */
1882 unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
1883 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1885 BUG_ON(sh->batch_head);
1886 pr_debug("%s: stripe %llu\n", __func__,
1887 (unsigned long long)sh->sector);
1889 for (i = disks; i--; ) {
1890 struct r5dev *dev = &sh->dev[i];
1891 /* Only process blocks that are known to be uptodate */
1892 if (test_bit(R5_InJournal, &dev->flags)) {
1894 * For this case, PAGE_SIZE must be equal to 4KB and
1895 * page offset is zero.
1897 off_srcs[count] = dev->offset;
1898 xor_srcs[count++] = dev->orig_page;
1899 } else if (test_bit(R5_Wantdrain, &dev->flags)) {
1900 off_srcs[count] = dev->offset;
1901 xor_srcs[count++] = dev->page;
1905 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1906 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1907 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
1908 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1913 static struct dma_async_tx_descriptor *
1914 ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1915 struct dma_async_tx_descriptor *tx)
1917 struct page **blocks = to_addr_page(percpu, 0);
1918 unsigned int *offs = to_addr_offs(sh, percpu);
1920 struct async_submit_ctl submit;
1922 pr_debug("%s: stripe %llu\n", __func__,
1923 (unsigned long long)sh->sector);
1925 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN);
1927 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1928 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1929 tx = async_gen_syndrome(blocks, offs, count+2,
1930 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1935 static struct dma_async_tx_descriptor *
1936 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1938 struct r5conf *conf = sh->raid_conf;
1939 int disks = sh->disks;
1941 struct stripe_head *head_sh = sh;
1943 pr_debug("%s: stripe %llu\n", __func__,
1944 (unsigned long long)sh->sector);
1946 for (i = disks; i--; ) {
1951 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
1957 * clear R5_InJournal, so when rewriting a page in
1958 * journal, it is not skipped by r5l_log_stripe()
1960 clear_bit(R5_InJournal, &dev->flags);
1961 spin_lock_irq(&sh->stripe_lock);
1962 chosen = dev->towrite;
1963 dev->towrite = NULL;
1964 sh->overwrite_disks = 0;
1965 BUG_ON(dev->written);
1966 wbi = dev->written = chosen;
1967 spin_unlock_irq(&sh->stripe_lock);
1968 WARN_ON(dev->page != dev->orig_page);
1970 while (wbi && wbi->bi_iter.bi_sector <
1971 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1972 if (wbi->bi_opf & REQ_FUA)
1973 set_bit(R5_WantFUA, &dev->flags);
1974 if (wbi->bi_opf & REQ_SYNC)
1975 set_bit(R5_SyncIO, &dev->flags);
1976 if (bio_op(wbi) == REQ_OP_DISCARD)
1977 set_bit(R5_Discard, &dev->flags);
1979 tx = async_copy_data(1, wbi, &dev->page,
1981 dev->sector, tx, sh,
1982 r5c_is_writeback(conf->log));
1983 if (dev->page != dev->orig_page &&
1984 !r5c_is_writeback(conf->log)) {
1985 set_bit(R5_SkipCopy, &dev->flags);
1986 clear_bit(R5_UPTODATE, &dev->flags);
1987 clear_bit(R5_OVERWRITE, &dev->flags);
1990 wbi = r5_next_bio(conf, wbi, dev->sector);
1993 if (head_sh->batch_head) {
1994 sh = list_first_entry(&sh->batch_list,
2007 static void ops_complete_reconstruct(void *stripe_head_ref)
2009 struct stripe_head *sh = stripe_head_ref;
2010 int disks = sh->disks;
2011 int pd_idx = sh->pd_idx;
2012 int qd_idx = sh->qd_idx;
2014 bool fua = false, sync = false, discard = false;
2016 pr_debug("%s: stripe %llu\n", __func__,
2017 (unsigned long long)sh->sector);
2019 for (i = disks; i--; ) {
2020 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
2021 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
2022 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
2025 for (i = disks; i--; ) {
2026 struct r5dev *dev = &sh->dev[i];
2028 if (dev->written || i == pd_idx || i == qd_idx) {
2029 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
2030 set_bit(R5_UPTODATE, &dev->flags);
2031 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
2032 set_bit(R5_Expanded, &dev->flags);
2035 set_bit(R5_WantFUA, &dev->flags);
2037 set_bit(R5_SyncIO, &dev->flags);
2041 if (sh->reconstruct_state == reconstruct_state_drain_run)
2042 sh->reconstruct_state = reconstruct_state_drain_result;
2043 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
2044 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
2046 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
2047 sh->reconstruct_state = reconstruct_state_result;
2050 set_bit(STRIPE_HANDLE, &sh->state);
2051 raid5_release_stripe(sh);
2055 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
2056 struct dma_async_tx_descriptor *tx)
2058 int disks = sh->disks;
2059 struct page **xor_srcs;
2060 unsigned int *off_srcs;
2061 struct async_submit_ctl submit;
2062 int count, pd_idx = sh->pd_idx, i;
2063 struct page *xor_dest;
2064 unsigned int off_dest;
2066 unsigned long flags;
2068 struct stripe_head *head_sh = sh;
2071 pr_debug("%s: stripe %llu\n", __func__,
2072 (unsigned long long)sh->sector);
2074 for (i = 0; i < sh->disks; i++) {
2077 if (!test_bit(R5_Discard, &sh->dev[i].flags))
2080 if (i >= sh->disks) {
2081 atomic_inc(&sh->count);
2082 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
2083 ops_complete_reconstruct(sh);
2088 xor_srcs = to_addr_page(percpu, j);
2089 off_srcs = to_addr_offs(sh, percpu);
2090 /* check if prexor is active which means only process blocks
2091 * that are part of a read-modify-write (written)
2093 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2095 off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
2096 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
2097 for (i = disks; i--; ) {
2098 struct r5dev *dev = &sh->dev[i];
2099 if (head_sh->dev[i].written ||
2100 test_bit(R5_InJournal, &head_sh->dev[i].flags)) {
2101 off_srcs[count] = dev->offset;
2102 xor_srcs[count++] = dev->page;
2106 xor_dest = sh->dev[pd_idx].page;
2107 off_dest = sh->dev[pd_idx].offset;
2108 for (i = disks; i--; ) {
2109 struct r5dev *dev = &sh->dev[i];
2111 off_srcs[count] = dev->offset;
2112 xor_srcs[count++] = dev->page;
2117 /* 1/ if we prexor'd then the dest is reused as a source
2118 * 2/ if we did not prexor then we are redoing the parity
2119 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
2120 * for the synchronous xor case
2122 last_stripe = !head_sh->batch_head ||
2123 list_first_entry(&sh->batch_list,
2124 struct stripe_head, batch_list) == head_sh;
2126 flags = ASYNC_TX_ACK |
2127 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
2129 atomic_inc(&head_sh->count);
2130 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
2131 to_addr_conv(sh, percpu, j));
2133 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
2134 init_async_submit(&submit, flags, tx, NULL, NULL,
2135 to_addr_conv(sh, percpu, j));
2138 if (unlikely(count == 1))
2139 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
2140 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2142 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
2143 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2146 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2153 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
2154 struct dma_async_tx_descriptor *tx)
2156 struct async_submit_ctl submit;
2157 struct page **blocks;
2159 int count, i, j = 0;
2160 struct stripe_head *head_sh = sh;
2163 unsigned long txflags;
2165 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
2167 for (i = 0; i < sh->disks; i++) {
2168 if (sh->pd_idx == i || sh->qd_idx == i)
2170 if (!test_bit(R5_Discard, &sh->dev[i].flags))
2173 if (i >= sh->disks) {
2174 atomic_inc(&sh->count);
2175 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2176 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2177 ops_complete_reconstruct(sh);
2182 blocks = to_addr_page(percpu, j);
2183 offs = to_addr_offs(sh, percpu);
2185 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2186 synflags = SYNDROME_SRC_WRITTEN;
2187 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
2189 synflags = SYNDROME_SRC_ALL;
2190 txflags = ASYNC_TX_ACK;
2193 count = set_syndrome_sources(blocks, offs, sh, synflags);
2194 last_stripe = !head_sh->batch_head ||
2195 list_first_entry(&sh->batch_list,
2196 struct stripe_head, batch_list) == head_sh;
2199 atomic_inc(&head_sh->count);
2200 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
2201 head_sh, to_addr_conv(sh, percpu, j));
2203 init_async_submit(&submit, 0, tx, NULL, NULL,
2204 to_addr_conv(sh, percpu, j));
2205 tx = async_gen_syndrome(blocks, offs, count+2,
2206 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2209 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2215 static void ops_complete_check(void *stripe_head_ref)
2217 struct stripe_head *sh = stripe_head_ref;
2219 pr_debug("%s: stripe %llu\n", __func__,
2220 (unsigned long long)sh->sector);
2222 sh->check_state = check_state_check_result;
2223 set_bit(STRIPE_HANDLE, &sh->state);
2224 raid5_release_stripe(sh);
2227 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
2229 int disks = sh->disks;
2230 int pd_idx = sh->pd_idx;
2231 int qd_idx = sh->qd_idx;
2232 struct page *xor_dest;
2233 unsigned int off_dest;
2234 struct page **xor_srcs = to_addr_page(percpu, 0);
2235 unsigned int *off_srcs = to_addr_offs(sh, percpu);
2236 struct dma_async_tx_descriptor *tx;
2237 struct async_submit_ctl submit;
2241 pr_debug("%s: stripe %llu\n", __func__,
2242 (unsigned long long)sh->sector);
2244 BUG_ON(sh->batch_head);
2246 xor_dest = sh->dev[pd_idx].page;
2247 off_dest = sh->dev[pd_idx].offset;
2248 off_srcs[count] = off_dest;
2249 xor_srcs[count++] = xor_dest;
2250 for (i = disks; i--; ) {
2251 if (i == pd_idx || i == qd_idx)
2253 off_srcs[count] = sh->dev[i].offset;
2254 xor_srcs[count++] = sh->dev[i].page;
2257 init_async_submit(&submit, 0, NULL, NULL, NULL,
2258 to_addr_conv(sh, percpu, 0));
2259 tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
2260 RAID5_STRIPE_SIZE(sh->raid_conf),
2261 &sh->ops.zero_sum_result, &submit);
2263 atomic_inc(&sh->count);
2264 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2265 tx = async_trigger_callback(&submit);
2268 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2270 struct page **srcs = to_addr_page(percpu, 0);
2271 unsigned int *offs = to_addr_offs(sh, percpu);
2272 struct async_submit_ctl submit;
2275 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2276 (unsigned long long)sh->sector, checkp);
2278 BUG_ON(sh->batch_head);
2279 count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL);
2283 atomic_inc(&sh->count);
2284 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
2285 sh, to_addr_conv(sh, percpu, 0));
2286 async_syndrome_val(srcs, offs, count+2,
2287 RAID5_STRIPE_SIZE(sh->raid_conf),
2288 &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit);
2291 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
2293 int overlap_clear = 0, i, disks = sh->disks;
2294 struct dma_async_tx_descriptor *tx = NULL;
2295 struct r5conf *conf = sh->raid_conf;
2296 int level = conf->level;
2297 struct raid5_percpu *percpu;
2299 local_lock(&conf->percpu->lock);
2300 percpu = this_cpu_ptr(conf->percpu);
2301 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
2302 ops_run_biofill(sh);
2306 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
2308 tx = ops_run_compute5(sh, percpu);
2310 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2311 tx = ops_run_compute6_1(sh, percpu);
2313 tx = ops_run_compute6_2(sh, percpu);
2315 /* terminate the chain if reconstruct is not set to be run */
2316 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
2320 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2322 tx = ops_run_prexor5(sh, percpu, tx);
2324 tx = ops_run_prexor6(sh, percpu, tx);
2327 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2328 tx = ops_run_partial_parity(sh, percpu, tx);
2330 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
2331 tx = ops_run_biodrain(sh, tx);
2335 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2337 ops_run_reconstruct5(sh, percpu, tx);
2339 ops_run_reconstruct6(sh, percpu, tx);
2342 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2343 if (sh->check_state == check_state_run)
2344 ops_run_check_p(sh, percpu);
2345 else if (sh->check_state == check_state_run_q)
2346 ops_run_check_pq(sh, percpu, 0);
2347 else if (sh->check_state == check_state_run_pq)
2348 ops_run_check_pq(sh, percpu, 1);
2353 if (overlap_clear && !sh->batch_head) {
2354 for (i = disks; i--; ) {
2355 struct r5dev *dev = &sh->dev[i];
2356 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2357 wake_up(&sh->raid_conf->wait_for_overlap);
2360 local_unlock(&conf->percpu->lock);
2363 static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2365 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2369 __free_page(sh->ppl_page);
2370 kmem_cache_free(sc, sh);
2373 static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2374 int disks, struct r5conf *conf)
2376 struct stripe_head *sh;
2378 sh = kmem_cache_zalloc(sc, gfp);
2380 spin_lock_init(&sh->stripe_lock);
2381 spin_lock_init(&sh->batch_lock);
2382 INIT_LIST_HEAD(&sh->batch_list);
2383 INIT_LIST_HEAD(&sh->lru);
2384 INIT_LIST_HEAD(&sh->r5c);
2385 INIT_LIST_HEAD(&sh->log_list);
2386 atomic_set(&sh->count, 1);
2387 sh->raid_conf = conf;
2388 sh->log_start = MaxSector;
2390 if (raid5_has_ppl(conf)) {
2391 sh->ppl_page = alloc_page(gfp);
2392 if (!sh->ppl_page) {
2393 free_stripe(sc, sh);
2397 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2398 if (init_stripe_shared_pages(sh, conf, disks)) {
2399 free_stripe(sc, sh);
2406 static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
2408 struct stripe_head *sh;
2410 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
2414 if (grow_buffers(sh, gfp)) {
2416 free_stripe(conf->slab_cache, sh);
2419 sh->hash_lock_index =
2420 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
2421 /* we just created an active stripe so... */
2422 atomic_inc(&conf->active_stripes);
2424 raid5_release_stripe(sh);
2425 conf->max_nr_stripes++;
2429 static int grow_stripes(struct r5conf *conf, int num)
2431 struct kmem_cache *sc;
2432 size_t namelen = sizeof(conf->cache_name[0]);
2433 int devs = max(conf->raid_disks, conf->previous_raid_disks);
2435 if (conf->mddev->gendisk)
2436 snprintf(conf->cache_name[0], namelen,
2437 "raid%d-%s", conf->level, mdname(conf->mddev));
2439 snprintf(conf->cache_name[0], namelen,
2440 "raid%d-%p", conf->level, conf->mddev);
2441 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
2443 conf->active_name = 0;
2444 sc = kmem_cache_create(conf->cache_name[conf->active_name],
2445 struct_size_t(struct stripe_head, dev, devs),
2449 conf->slab_cache = sc;
2450 conf->pool_size = devs;
2452 if (!grow_one_stripe(conf, GFP_KERNEL))
2459 * scribble_alloc - allocate percpu scribble buffer for required size
2460 * of the scribble region
2461 * @percpu: from for_each_present_cpu() of the caller
2462 * @num: total number of disks in the array
2463 * @cnt: scribble objs count for required size of the scribble region
2465 * The scribble buffer size must be enough to contain:
2466 * 1/ a struct page pointer for each device in the array +2
2467 * 2/ room to convert each entry in (1) to its corresponding dma
2468 * (dma_map_page()) or page (page_address()) address.
2470 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2471 * calculate over all devices (not just the data blocks), using zeros in place
2472 * of the P and Q blocks.
2474 static int scribble_alloc(struct raid5_percpu *percpu,
2478 sizeof(struct page *) * (num + 2) +
2479 sizeof(addr_conv_t) * (num + 2) +
2480 sizeof(unsigned int) * (num + 2);
2484 * If here is in raid array suspend context, it is in memalloc noio
2485 * context as well, there is no potential recursive memory reclaim
2486 * I/Os with the GFP_KERNEL flag.
2488 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
2492 kvfree(percpu->scribble);
2494 percpu->scribble = scribble;
2495 percpu->scribble_obj_size = obj_size;
2499 static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2505 if (conf->scribble_disks >= new_disks &&
2506 conf->scribble_sectors >= new_sectors)
2509 raid5_quiesce(conf->mddev, true);
2512 for_each_present_cpu(cpu) {
2513 struct raid5_percpu *percpu;
2515 percpu = per_cpu_ptr(conf->percpu, cpu);
2516 err = scribble_alloc(percpu, new_disks,
2517 new_sectors / RAID5_STRIPE_SECTORS(conf));
2523 raid5_quiesce(conf->mddev, false);
2526 conf->scribble_disks = new_disks;
2527 conf->scribble_sectors = new_sectors;
2532 static int resize_stripes(struct r5conf *conf, int newsize)
2534 /* Make all the stripes able to hold 'newsize' devices.
2535 * New slots in each stripe get 'page' set to a new page.
2537 * This happens in stages:
2538 * 1/ create a new kmem_cache and allocate the required number of
2540 * 2/ gather all the old stripe_heads and transfer the pages across
2541 * to the new stripe_heads. This will have the side effect of
2542 * freezing the array as once all stripe_heads have been collected,
2543 * no IO will be possible. Old stripe heads are freed once their
2544 * pages have been transferred over, and the old kmem_cache is
2545 * freed when all stripes are done.
2546 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2547 * we simple return a failure status - no need to clean anything up.
2548 * 4/ allocate new pages for the new slots in the new stripe_heads.
2549 * If this fails, we don't bother trying the shrink the
2550 * stripe_heads down again, we just leave them as they are.
2551 * As each stripe_head is processed the new one is released into
2554 * Once step2 is started, we cannot afford to wait for a write,
2555 * so we use GFP_NOIO allocations.
2557 struct stripe_head *osh, *nsh;
2558 LIST_HEAD(newstripes);
2559 struct disk_info *ndisks;
2561 struct kmem_cache *sc;
2565 md_allow_write(conf->mddev);
2568 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2569 struct_size_t(struct stripe_head, dev, newsize),
2574 /* Need to ensure auto-resizing doesn't interfere */
2575 mutex_lock(&conf->cache_size_mutex);
2577 for (i = conf->max_nr_stripes; i; i--) {
2578 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
2582 list_add(&nsh->lru, &newstripes);
2585 /* didn't get enough, give up */
2586 while (!list_empty(&newstripes)) {
2587 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2588 list_del(&nsh->lru);
2589 free_stripe(sc, nsh);
2591 kmem_cache_destroy(sc);
2592 mutex_unlock(&conf->cache_size_mutex);
2595 /* Step 2 - Must use GFP_NOIO now.
2596 * OK, we have enough stripes, start collecting inactive
2597 * stripes and copying them over
2601 list_for_each_entry(nsh, &newstripes, lru) {
2602 lock_device_hash_lock(conf, hash);
2603 wait_event_cmd(conf->wait_for_stripe,
2604 !list_empty(conf->inactive_list + hash),
2605 unlock_device_hash_lock(conf, hash),
2606 lock_device_hash_lock(conf, hash));
2607 osh = get_free_stripe(conf, hash);
2608 unlock_device_hash_lock(conf, hash);
2610 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2611 for (i = 0; i < osh->nr_pages; i++) {
2612 nsh->pages[i] = osh->pages[i];
2613 osh->pages[i] = NULL;
2616 for(i=0; i<conf->pool_size; i++) {
2617 nsh->dev[i].page = osh->dev[i].page;
2618 nsh->dev[i].orig_page = osh->dev[i].page;
2619 nsh->dev[i].offset = osh->dev[i].offset;
2621 nsh->hash_lock_index = hash;
2622 free_stripe(conf->slab_cache, osh);
2624 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2625 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2630 kmem_cache_destroy(conf->slab_cache);
2633 * At this point, we are holding all the stripes so the array
2634 * is completely stalled, so now is a good time to resize
2635 * conf->disks and the scribble region
2637 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
2639 for (i = 0; i < conf->pool_size; i++)
2640 ndisks[i] = conf->disks[i];
2642 for (i = conf->pool_size; i < newsize; i++) {
2643 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2644 if (!ndisks[i].extra_page)
2649 for (i = conf->pool_size; i < newsize; i++)
2650 if (ndisks[i].extra_page)
2651 put_page(ndisks[i].extra_page);
2655 conf->disks = ndisks;
2660 conf->slab_cache = sc;
2661 conf->active_name = 1-conf->active_name;
2663 /* Step 4, return new stripes to service */
2664 while(!list_empty(&newstripes)) {
2665 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2666 list_del_init(&nsh->lru);
2668 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2669 for (i = 0; i < nsh->nr_pages; i++) {
2672 nsh->pages[i] = alloc_page(GFP_NOIO);
2677 for (i = conf->raid_disks; i < newsize; i++) {
2678 if (nsh->dev[i].page)
2680 nsh->dev[i].page = raid5_get_dev_page(nsh, i);
2681 nsh->dev[i].orig_page = nsh->dev[i].page;
2682 nsh->dev[i].offset = raid5_get_page_offset(nsh, i);
2685 for (i=conf->raid_disks; i < newsize; i++)
2686 if (nsh->dev[i].page == NULL) {
2687 struct page *p = alloc_page(GFP_NOIO);
2688 nsh->dev[i].page = p;
2689 nsh->dev[i].orig_page = p;
2690 nsh->dev[i].offset = 0;
2695 raid5_release_stripe(nsh);
2697 /* critical section pass, GFP_NOIO no longer needed */
2700 conf->pool_size = newsize;
2701 mutex_unlock(&conf->cache_size_mutex);
2706 static int drop_one_stripe(struct r5conf *conf)
2708 struct stripe_head *sh;
2709 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
2711 spin_lock_irq(conf->hash_locks + hash);
2712 sh = get_free_stripe(conf, hash);
2713 spin_unlock_irq(conf->hash_locks + hash);
2716 BUG_ON(atomic_read(&sh->count));
2718 free_stripe(conf->slab_cache, sh);
2719 atomic_dec(&conf->active_stripes);
2720 conf->max_nr_stripes--;
2724 static void shrink_stripes(struct r5conf *conf)
2726 while (conf->max_nr_stripes &&
2727 drop_one_stripe(conf))
2730 kmem_cache_destroy(conf->slab_cache);
2731 conf->slab_cache = NULL;
2735 * This helper wraps rcu_dereference_protected() and can be used when
2736 * it is known that the nr_pending of the rdev is elevated.
2738 static struct md_rdev *rdev_pend_deref(struct md_rdev __rcu *rdev)
2740 return rcu_dereference_protected(rdev,
2741 atomic_read(&rcu_access_pointer(rdev)->nr_pending));
2745 * This helper wraps rcu_dereference_protected() and should be used
2746 * when it is known that the mddev_lock() is held. This is safe
2747 * seeing raid5_remove_disk() has the same lock held.
2749 static struct md_rdev *rdev_mdlock_deref(struct mddev *mddev,
2750 struct md_rdev __rcu *rdev)
2752 return rcu_dereference_protected(rdev,
2753 lockdep_is_held(&mddev->reconfig_mutex));
2756 static void raid5_end_read_request(struct bio * bi)
2758 struct stripe_head *sh = bi->bi_private;
2759 struct r5conf *conf = sh->raid_conf;
2760 int disks = sh->disks, i;
2761 struct md_rdev *rdev = NULL;
2764 for (i=0 ; i<disks; i++)
2765 if (bi == &sh->dev[i].req)
2768 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2769 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2775 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2776 /* If replacement finished while this request was outstanding,
2777 * 'replacement' might be NULL already.
2778 * In that case it moved down to 'rdev'.
2779 * rdev is not removed until all requests are finished.
2781 rdev = rdev_pend_deref(conf->disks[i].replacement);
2783 rdev = rdev_pend_deref(conf->disks[i].rdev);
2785 if (use_new_offset(conf, sh))
2786 s = sh->sector + rdev->new_data_offset;
2788 s = sh->sector + rdev->data_offset;
2789 if (!bi->bi_status) {
2790 set_bit(R5_UPTODATE, &sh->dev[i].flags);
2791 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2792 /* Note that this cannot happen on a
2793 * replacement device. We just fail those on
2796 pr_info_ratelimited(
2797 "md/raid:%s: read error corrected (%lu sectors at %llu on %pg)\n",
2798 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
2799 (unsigned long long)s,
2801 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
2802 clear_bit(R5_ReadError, &sh->dev[i].flags);
2803 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2804 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2805 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2807 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2809 * end read for a page in journal, this
2810 * must be preparing for prexor in rmw
2812 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2814 if (atomic_read(&rdev->read_errors))
2815 atomic_set(&rdev->read_errors, 0);
2820 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
2821 if (!(bi->bi_status == BLK_STS_PROTECTION))
2822 atomic_inc(&rdev->read_errors);
2823 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2824 pr_warn_ratelimited(
2825 "md/raid:%s: read error on replacement device (sector %llu on %pg).\n",
2826 mdname(conf->mddev),
2827 (unsigned long long)s,
2829 else if (conf->mddev->degraded >= conf->max_degraded) {
2831 pr_warn_ratelimited(
2832 "md/raid:%s: read error not correctable (sector %llu on %pg).\n",
2833 mdname(conf->mddev),
2834 (unsigned long long)s,
2836 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
2839 pr_warn_ratelimited(
2840 "md/raid:%s: read error NOT corrected!! (sector %llu on %pg).\n",
2841 mdname(conf->mddev),
2842 (unsigned long long)s,
2844 } else if (atomic_read(&rdev->read_errors)
2845 > conf->max_nr_stripes) {
2846 if (!test_bit(Faulty, &rdev->flags)) {
2847 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2848 mdname(conf->mddev),
2849 atomic_read(&rdev->read_errors),
2850 conf->max_nr_stripes);
2851 pr_warn("md/raid:%s: Too many read errors, failing device %pg.\n",
2852 mdname(conf->mddev), rdev->bdev);
2856 if (set_bad && test_bit(In_sync, &rdev->flags)
2857 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2860 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2861 set_bit(R5_ReadError, &sh->dev[i].flags);
2862 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2863 set_bit(R5_ReadError, &sh->dev[i].flags);
2864 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2866 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2868 clear_bit(R5_ReadError, &sh->dev[i].flags);
2869 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2871 && test_bit(In_sync, &rdev->flags)
2872 && rdev_set_badblocks(
2873 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
2874 md_error(conf->mddev, rdev);
2877 rdev_dec_pending(rdev, conf->mddev);
2879 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2880 set_bit(STRIPE_HANDLE, &sh->state);
2881 raid5_release_stripe(sh);
2884 static void raid5_end_write_request(struct bio *bi)
2886 struct stripe_head *sh = bi->bi_private;
2887 struct r5conf *conf = sh->raid_conf;
2888 int disks = sh->disks, i;
2889 struct md_rdev *rdev;
2892 int replacement = 0;
2894 for (i = 0 ; i < disks; i++) {
2895 if (bi == &sh->dev[i].req) {
2896 rdev = rdev_pend_deref(conf->disks[i].rdev);
2899 if (bi == &sh->dev[i].rreq) {
2900 rdev = rdev_pend_deref(conf->disks[i].replacement);
2904 /* rdev was removed and 'replacement'
2905 * replaced it. rdev is not removed
2906 * until all requests are finished.
2908 rdev = rdev_pend_deref(conf->disks[i].rdev);
2912 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2913 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2922 md_error(conf->mddev, rdev);
2923 else if (is_badblock(rdev, sh->sector,
2924 RAID5_STRIPE_SECTORS(conf),
2925 &first_bad, &bad_sectors))
2926 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2928 if (bi->bi_status) {
2929 set_bit(STRIPE_DEGRADED, &sh->state);
2930 set_bit(WriteErrorSeen, &rdev->flags);
2931 set_bit(R5_WriteError, &sh->dev[i].flags);
2932 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2933 set_bit(MD_RECOVERY_NEEDED,
2934 &rdev->mddev->recovery);
2935 } else if (is_badblock(rdev, sh->sector,
2936 RAID5_STRIPE_SECTORS(conf),
2937 &first_bad, &bad_sectors)) {
2938 set_bit(R5_MadeGood, &sh->dev[i].flags);
2939 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2940 /* That was a successful write so make
2941 * sure it looks like we already did
2944 set_bit(R5_ReWrite, &sh->dev[i].flags);
2947 rdev_dec_pending(rdev, conf->mddev);
2949 if (sh->batch_head && bi->bi_status && !replacement)
2950 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2953 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2954 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2955 set_bit(STRIPE_HANDLE, &sh->state);
2957 if (sh->batch_head && sh != sh->batch_head)
2958 raid5_release_stripe(sh->batch_head);
2959 raid5_release_stripe(sh);
2962 static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
2964 struct r5conf *conf = mddev->private;
2965 unsigned long flags;
2966 pr_debug("raid456: error called\n");
2968 pr_crit("md/raid:%s: Disk failure on %pg, disabling device.\n",
2969 mdname(mddev), rdev->bdev);
2971 spin_lock_irqsave(&conf->device_lock, flags);
2972 set_bit(Faulty, &rdev->flags);
2973 clear_bit(In_sync, &rdev->flags);
2974 mddev->degraded = raid5_calc_degraded(conf);
2976 if (has_failed(conf)) {
2977 set_bit(MD_BROKEN, &conf->mddev->flags);
2978 conf->recovery_disabled = mddev->recovery_disabled;
2980 pr_crit("md/raid:%s: Cannot continue operation (%d/%d failed).\n",
2981 mdname(mddev), mddev->degraded, conf->raid_disks);
2983 pr_crit("md/raid:%s: Operation continuing on %d devices.\n",
2984 mdname(mddev), conf->raid_disks - mddev->degraded);
2987 spin_unlock_irqrestore(&conf->device_lock, flags);
2988 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2990 set_bit(Blocked, &rdev->flags);
2991 set_mask_bits(&mddev->sb_flags, 0,
2992 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2993 r5c_update_on_rdev_error(mddev, rdev);
2997 * Input: a 'big' sector number,
2998 * Output: index of the data and parity disk, and the sector # in them.
3000 sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
3001 int previous, int *dd_idx,
3002 struct stripe_head *sh)
3004 sector_t stripe, stripe2;
3005 sector_t chunk_number;
3006 unsigned int chunk_offset;
3009 sector_t new_sector;
3010 int algorithm = previous ? conf->prev_algo
3012 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
3013 : conf->chunk_sectors;
3014 int raid_disks = previous ? conf->previous_raid_disks
3016 int data_disks = raid_disks - conf->max_degraded;
3018 /* First compute the information on this sector */
3021 * Compute the chunk number and the sector offset inside the chunk
3023 chunk_offset = sector_div(r_sector, sectors_per_chunk);
3024 chunk_number = r_sector;
3027 * Compute the stripe number
3029 stripe = chunk_number;
3030 *dd_idx = sector_div(stripe, data_disks);
3033 * Select the parity disk based on the user selected algorithm.
3035 pd_idx = qd_idx = -1;
3036 switch(conf->level) {
3038 pd_idx = data_disks;
3041 switch (algorithm) {
3042 case ALGORITHM_LEFT_ASYMMETRIC:
3043 pd_idx = data_disks - sector_div(stripe2, raid_disks);
3044 if (*dd_idx >= pd_idx)
3047 case ALGORITHM_RIGHT_ASYMMETRIC:
3048 pd_idx = sector_div(stripe2, raid_disks);
3049 if (*dd_idx >= pd_idx)
3052 case ALGORITHM_LEFT_SYMMETRIC:
3053 pd_idx = data_disks - sector_div(stripe2, raid_disks);
3054 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
3056 case ALGORITHM_RIGHT_SYMMETRIC:
3057 pd_idx = sector_div(stripe2, raid_disks);
3058 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
3060 case ALGORITHM_PARITY_0:
3064 case ALGORITHM_PARITY_N:
3065 pd_idx = data_disks;
3073 switch (algorithm) {
3074 case ALGORITHM_LEFT_ASYMMETRIC:
3075 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3076 qd_idx = pd_idx + 1;
3077 if (pd_idx == raid_disks-1) {
3078 (*dd_idx)++; /* Q D D D P */
3080 } else if (*dd_idx >= pd_idx)
3081 (*dd_idx) += 2; /* D D P Q D */
3083 case ALGORITHM_RIGHT_ASYMMETRIC:
3084 pd_idx = sector_div(stripe2, raid_disks);
3085 qd_idx = pd_idx + 1;
3086 if (pd_idx == raid_disks-1) {
3087 (*dd_idx)++; /* Q D D D P */
3089 } else if (*dd_idx >= pd_idx)
3090 (*dd_idx) += 2; /* D D P Q D */
3092 case ALGORITHM_LEFT_SYMMETRIC:
3093 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3094 qd_idx = (pd_idx + 1) % raid_disks;
3095 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
3097 case ALGORITHM_RIGHT_SYMMETRIC:
3098 pd_idx = sector_div(stripe2, raid_disks);
3099 qd_idx = (pd_idx + 1) % raid_disks;
3100 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
3103 case ALGORITHM_PARITY_0:
3108 case ALGORITHM_PARITY_N:
3109 pd_idx = data_disks;
3110 qd_idx = data_disks + 1;
3113 case ALGORITHM_ROTATING_ZERO_RESTART:
3114 /* Exactly the same as RIGHT_ASYMMETRIC, but or
3115 * of blocks for computing Q is different.
3117 pd_idx = sector_div(stripe2, raid_disks);
3118 qd_idx = pd_idx + 1;
3119 if (pd_idx == raid_disks-1) {
3120 (*dd_idx)++; /* Q D D D P */
3122 } else if (*dd_idx >= pd_idx)
3123 (*dd_idx) += 2; /* D D P Q D */
3127 case ALGORITHM_ROTATING_N_RESTART:
3128 /* Same a left_asymmetric, by first stripe is
3129 * D D D P Q rather than
3133 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3134 qd_idx = pd_idx + 1;
3135 if (pd_idx == raid_disks-1) {
3136 (*dd_idx)++; /* Q D D D P */
3138 } else if (*dd_idx >= pd_idx)
3139 (*dd_idx) += 2; /* D D P Q D */
3143 case ALGORITHM_ROTATING_N_CONTINUE:
3144 /* Same as left_symmetric but Q is before P */
3145 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3146 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
3147 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
3151 case ALGORITHM_LEFT_ASYMMETRIC_6:
3152 /* RAID5 left_asymmetric, with Q on last device */
3153 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
3154 if (*dd_idx >= pd_idx)
3156 qd_idx = raid_disks - 1;
3159 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3160 pd_idx = sector_div(stripe2, raid_disks-1);
3161 if (*dd_idx >= pd_idx)
3163 qd_idx = raid_disks - 1;
3166 case ALGORITHM_LEFT_SYMMETRIC_6:
3167 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
3168 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3169 qd_idx = raid_disks - 1;
3172 case ALGORITHM_RIGHT_SYMMETRIC_6:
3173 pd_idx = sector_div(stripe2, raid_disks-1);
3174 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3175 qd_idx = raid_disks - 1;
3178 case ALGORITHM_PARITY_0_6:
3181 qd_idx = raid_disks - 1;
3191 sh->pd_idx = pd_idx;
3192 sh->qd_idx = qd_idx;
3193 sh->ddf_layout = ddf_layout;
3196 * Finally, compute the new sector number
3198 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
3202 sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
3204 struct r5conf *conf = sh->raid_conf;
3205 int raid_disks = sh->disks;
3206 int data_disks = raid_disks - conf->max_degraded;
3207 sector_t new_sector = sh->sector, check;
3208 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
3209 : conf->chunk_sectors;
3210 int algorithm = previous ? conf->prev_algo
3214 sector_t chunk_number;
3215 int dummy1, dd_idx = i;
3217 struct stripe_head sh2;
3219 chunk_offset = sector_div(new_sector, sectors_per_chunk);
3220 stripe = new_sector;
3222 if (i == sh->pd_idx)
3224 switch(conf->level) {
3227 switch (algorithm) {
3228 case ALGORITHM_LEFT_ASYMMETRIC:
3229 case ALGORITHM_RIGHT_ASYMMETRIC:
3233 case ALGORITHM_LEFT_SYMMETRIC:
3234 case ALGORITHM_RIGHT_SYMMETRIC:
3237 i -= (sh->pd_idx + 1);
3239 case ALGORITHM_PARITY_0:
3242 case ALGORITHM_PARITY_N:
3249 if (i == sh->qd_idx)
3250 return 0; /* It is the Q disk */
3251 switch (algorithm) {
3252 case ALGORITHM_LEFT_ASYMMETRIC:
3253 case ALGORITHM_RIGHT_ASYMMETRIC:
3254 case ALGORITHM_ROTATING_ZERO_RESTART:
3255 case ALGORITHM_ROTATING_N_RESTART:
3256 if (sh->pd_idx == raid_disks-1)
3257 i--; /* Q D D D P */
3258 else if (i > sh->pd_idx)
3259 i -= 2; /* D D P Q D */
3261 case ALGORITHM_LEFT_SYMMETRIC:
3262 case ALGORITHM_RIGHT_SYMMETRIC:
3263 if (sh->pd_idx == raid_disks-1)
3264 i--; /* Q D D D P */
3269 i -= (sh->pd_idx + 2);
3272 case ALGORITHM_PARITY_0:
3275 case ALGORITHM_PARITY_N:
3277 case ALGORITHM_ROTATING_N_CONTINUE:
3278 /* Like left_symmetric, but P is before Q */
3279 if (sh->pd_idx == 0)
3280 i--; /* P D D D Q */
3285 i -= (sh->pd_idx + 1);
3288 case ALGORITHM_LEFT_ASYMMETRIC_6:
3289 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3293 case ALGORITHM_LEFT_SYMMETRIC_6:
3294 case ALGORITHM_RIGHT_SYMMETRIC_6:
3296 i += data_disks + 1;
3297 i -= (sh->pd_idx + 1);
3299 case ALGORITHM_PARITY_0_6:
3308 chunk_number = stripe * data_disks + i;
3309 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
3311 check = raid5_compute_sector(conf, r_sector,
3312 previous, &dummy1, &sh2);
3313 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3314 || sh2.qd_idx != sh->qd_idx) {
3315 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3316 mdname(conf->mddev));
3323 * There are cases where we want handle_stripe_dirtying() and
3324 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3326 * This function checks whether we want to delay the towrite. Specifically,
3327 * we delay the towrite when:
3329 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3330 * stripe has data in journal (for other devices).
3332 * In this case, when reading data for the non-overwrite dev, it is
3333 * necessary to handle complex rmw of write back cache (prexor with
3334 * orig_page, and xor with page). To keep read path simple, we would
3335 * like to flush data in journal to RAID disks first, so complex rmw
3336 * is handled in the write patch (handle_stripe_dirtying).
3338 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3340 * It is important to be able to flush all stripes in raid5-cache.
3341 * Therefore, we need reserve some space on the journal device for
3342 * these flushes. If flush operation includes pending writes to the
3343 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3344 * for the flush out. If we exclude these pending writes from flush
3345 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3346 * Therefore, excluding pending writes in these cases enables more
3347 * efficient use of the journal device.
3349 * Note: To make sure the stripe makes progress, we only delay
3350 * towrite for stripes with data already in journal (injournal > 0).
3351 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3352 * no_space_stripes list.
3354 * 3. during journal failure
3355 * In journal failure, we try to flush all cached data to raid disks
3356 * based on data in stripe cache. The array is read-only to upper
3357 * layers, so we would skip all pending writes.
3360 static inline bool delay_towrite(struct r5conf *conf,
3362 struct stripe_head_state *s)
3365 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3366 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3369 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3373 if (s->log_failed && s->injournal)
3379 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
3380 int rcw, int expand)
3382 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
3383 struct r5conf *conf = sh->raid_conf;
3384 int level = conf->level;
3388 * In some cases, handle_stripe_dirtying initially decided to
3389 * run rmw and allocates extra page for prexor. However, rcw is
3390 * cheaper later on. We need to free the extra page now,
3391 * because we won't be able to do that in ops_complete_prexor().
3393 r5c_release_extra_page(sh);
3395 for (i = disks; i--; ) {
3396 struct r5dev *dev = &sh->dev[i];
3398 if (dev->towrite && !delay_towrite(conf, dev, s)) {
3399 set_bit(R5_LOCKED, &dev->flags);
3400 set_bit(R5_Wantdrain, &dev->flags);
3402 clear_bit(R5_UPTODATE, &dev->flags);
3404 } else if (test_bit(R5_InJournal, &dev->flags)) {
3405 set_bit(R5_LOCKED, &dev->flags);
3409 /* if we are not expanding this is a proper write request, and
3410 * there will be bios with new data to be drained into the
3415 /* False alarm, nothing to do */
3417 sh->reconstruct_state = reconstruct_state_drain_run;
3418 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3420 sh->reconstruct_state = reconstruct_state_run;
3422 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3424 if (s->locked + conf->max_degraded == disks)
3425 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
3426 atomic_inc(&conf->pending_full_writes);
3428 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3429 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
3430 BUG_ON(level == 6 &&
3431 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3432 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
3434 for (i = disks; i--; ) {
3435 struct r5dev *dev = &sh->dev[i];
3436 if (i == pd_idx || i == qd_idx)
3440 (test_bit(R5_UPTODATE, &dev->flags) ||
3441 test_bit(R5_Wantcompute, &dev->flags))) {
3442 set_bit(R5_Wantdrain, &dev->flags);
3443 set_bit(R5_LOCKED, &dev->flags);
3444 clear_bit(R5_UPTODATE, &dev->flags);
3446 } else if (test_bit(R5_InJournal, &dev->flags)) {
3447 set_bit(R5_LOCKED, &dev->flags);
3452 /* False alarm - nothing to do */
3454 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3455 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3456 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3457 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3460 /* keep the parity disk(s) locked while asynchronous operations
3463 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3464 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3468 int qd_idx = sh->qd_idx;
3469 struct r5dev *dev = &sh->dev[qd_idx];
3471 set_bit(R5_LOCKED, &dev->flags);
3472 clear_bit(R5_UPTODATE, &dev->flags);
3476 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
3477 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3478 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3479 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3480 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3482 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
3483 __func__, (unsigned long long)sh->sector,
3484 s->locked, s->ops_request);
3487 static bool stripe_bio_overlaps(struct stripe_head *sh, struct bio *bi,
3488 int dd_idx, int forwrite)
3490 struct r5conf *conf = sh->raid_conf;
3493 pr_debug("checking bi b#%llu to stripe s#%llu\n",
3494 bi->bi_iter.bi_sector, sh->sector);
3496 /* Don't allow new IO added to stripes in batch list */
3501 bip = &sh->dev[dd_idx].towrite;
3503 bip = &sh->dev[dd_idx].toread;
3505 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3506 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
3508 bip = &(*bip)->bi_next;
3511 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
3514 if (forwrite && raid5_has_ppl(conf)) {
3516 * With PPL only writes to consecutive data chunks within a
3517 * stripe are allowed because for a single stripe_head we can
3518 * only have one PPL entry at a time, which describes one data
3519 * range. Not really an overlap, but wait_for_overlap can be
3520 * used to handle this.
3528 for (i = 0; i < sh->disks; i++) {
3529 if (i != sh->pd_idx &&
3530 (i == dd_idx || sh->dev[i].towrite)) {
3531 sector = sh->dev[i].sector;
3532 if (count == 0 || sector < first)
3540 if (first + conf->chunk_sectors * (count - 1) != last)
3547 static void __add_stripe_bio(struct stripe_head *sh, struct bio *bi,
3548 int dd_idx, int forwrite, int previous)
3550 struct r5conf *conf = sh->raid_conf;
3555 bip = &sh->dev[dd_idx].towrite;
3559 bip = &sh->dev[dd_idx].toread;
3562 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector)
3563 bip = &(*bip)->bi_next;
3565 if (!forwrite || previous)
3566 clear_bit(STRIPE_BATCH_READY, &sh->state);
3568 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
3572 bio_inc_remaining(bi);
3573 md_write_inc(conf->mddev, bi);
3576 /* check if page is covered */
3577 sector_t sector = sh->dev[dd_idx].sector;
3578 for (bi=sh->dev[dd_idx].towrite;
3579 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
3580 bi && bi->bi_iter.bi_sector <= sector;
3581 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
3582 if (bio_end_sector(bi) >= sector)
3583 sector = bio_end_sector(bi);
3585 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
3586 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3587 sh->overwrite_disks++;
3590 pr_debug("added bi b#%llu to stripe s#%llu, disk %d, logical %llu\n",
3591 (*bip)->bi_iter.bi_sector, sh->sector, dd_idx,
3592 sh->dev[dd_idx].sector);
3594 if (conf->mddev->bitmap && firstwrite) {
3595 /* Cannot hold spinlock over bitmap_startwrite,
3596 * but must ensure this isn't added to a batch until
3597 * we have added to the bitmap and set bm_seq.
3598 * So set STRIPE_BITMAP_PENDING to prevent
3600 * If multiple __add_stripe_bio() calls race here they
3601 * much all set STRIPE_BITMAP_PENDING. So only the first one
3602 * to complete "bitmap_startwrite" gets to set
3603 * STRIPE_BIT_DELAY. This is important as once a stripe
3604 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3607 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3608 spin_unlock_irq(&sh->stripe_lock);
3609 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3610 RAID5_STRIPE_SECTORS(conf), 0);
3611 spin_lock_irq(&sh->stripe_lock);
3612 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3613 if (!sh->batch_head) {
3614 sh->bm_seq = conf->seq_flush+1;
3615 set_bit(STRIPE_BIT_DELAY, &sh->state);
3621 * Each stripe/dev can have one or more bios attached.
3622 * toread/towrite point to the first in a chain.
3623 * The bi_next chain must be in order.
3625 static bool add_stripe_bio(struct stripe_head *sh, struct bio *bi,
3626 int dd_idx, int forwrite, int previous)
3628 spin_lock_irq(&sh->stripe_lock);
3630 if (stripe_bio_overlaps(sh, bi, dd_idx, forwrite)) {
3631 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
3632 spin_unlock_irq(&sh->stripe_lock);
3636 __add_stripe_bio(sh, bi, dd_idx, forwrite, previous);
3637 spin_unlock_irq(&sh->stripe_lock);
3641 static void end_reshape(struct r5conf *conf);
3643 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
3644 struct stripe_head *sh)
3646 int sectors_per_chunk =
3647 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
3649 int chunk_offset = sector_div(stripe, sectors_per_chunk);
3650 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
3652 raid5_compute_sector(conf,
3653 stripe * (disks - conf->max_degraded)
3654 *sectors_per_chunk + chunk_offset,
3660 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
3661 struct stripe_head_state *s, int disks)
3664 BUG_ON(sh->batch_head);
3665 for (i = disks; i--; ) {
3669 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
3670 struct md_rdev *rdev;
3672 rdev = rcu_dereference(conf->disks[i].rdev);
3673 if (rdev && test_bit(In_sync, &rdev->flags) &&
3674 !test_bit(Faulty, &rdev->flags))
3675 atomic_inc(&rdev->nr_pending);
3680 if (!rdev_set_badblocks(
3683 RAID5_STRIPE_SECTORS(conf), 0))
3684 md_error(conf->mddev, rdev);
3685 rdev_dec_pending(rdev, conf->mddev);
3688 spin_lock_irq(&sh->stripe_lock);
3689 /* fail all writes first */
3690 bi = sh->dev[i].towrite;
3691 sh->dev[i].towrite = NULL;
3692 sh->overwrite_disks = 0;
3693 spin_unlock_irq(&sh->stripe_lock);
3697 log_stripe_write_finished(sh);
3699 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3700 wake_up(&conf->wait_for_overlap);
3702 while (bi && bi->bi_iter.bi_sector <
3703 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3704 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
3706 md_write_end(conf->mddev);
3711 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3712 RAID5_STRIPE_SECTORS(conf), 0, 0);
3714 /* and fail all 'written' */
3715 bi = sh->dev[i].written;
3716 sh->dev[i].written = NULL;
3717 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3718 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3719 sh->dev[i].page = sh->dev[i].orig_page;
3722 if (bi) bitmap_end = 1;
3723 while (bi && bi->bi_iter.bi_sector <
3724 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3725 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
3727 md_write_end(conf->mddev);
3732 /* fail any reads if this device is non-operational and
3733 * the data has not reached the cache yet.
3735 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
3736 s->failed > conf->max_degraded &&
3737 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3738 test_bit(R5_ReadError, &sh->dev[i].flags))) {
3739 spin_lock_irq(&sh->stripe_lock);
3740 bi = sh->dev[i].toread;
3741 sh->dev[i].toread = NULL;
3742 spin_unlock_irq(&sh->stripe_lock);
3743 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3744 wake_up(&conf->wait_for_overlap);
3747 while (bi && bi->bi_iter.bi_sector <
3748 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3749 struct bio *nextbi =
3750 r5_next_bio(conf, bi, sh->dev[i].sector);
3757 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3758 RAID5_STRIPE_SECTORS(conf), 0, 0);
3759 /* If we were in the middle of a write the parity block might
3760 * still be locked - so just clear all R5_LOCKED flags
3762 clear_bit(R5_LOCKED, &sh->dev[i].flags);
3767 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3768 if (atomic_dec_and_test(&conf->pending_full_writes))
3769 md_wakeup_thread(conf->mddev->thread);
3773 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
3774 struct stripe_head_state *s)
3779 BUG_ON(sh->batch_head);
3780 clear_bit(STRIPE_SYNCING, &sh->state);
3781 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3782 wake_up(&conf->wait_for_overlap);
3785 /* There is nothing more to do for sync/check/repair.
3786 * Don't even need to abort as that is handled elsewhere
3787 * if needed, and not always wanted e.g. if there is a known
3789 * For recover/replace we need to record a bad block on all
3790 * non-sync devices, or abort the recovery
3792 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3793 /* During recovery devices cannot be removed, so
3794 * locking and refcounting of rdevs is not needed
3797 for (i = 0; i < conf->raid_disks; i++) {
3798 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
3800 && !test_bit(Faulty, &rdev->flags)
3801 && !test_bit(In_sync, &rdev->flags)
3802 && !rdev_set_badblocks(rdev, sh->sector,
3803 RAID5_STRIPE_SECTORS(conf), 0))
3805 rdev = rcu_dereference(conf->disks[i].replacement);
3807 && !test_bit(Faulty, &rdev->flags)
3808 && !test_bit(In_sync, &rdev->flags)
3809 && !rdev_set_badblocks(rdev, sh->sector,
3810 RAID5_STRIPE_SECTORS(conf), 0))
3815 conf->recovery_disabled =
3816 conf->mddev->recovery_disabled;
3818 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
3821 static int want_replace(struct stripe_head *sh, int disk_idx)
3823 struct md_rdev *rdev;
3827 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
3829 && !test_bit(Faulty, &rdev->flags)
3830 && !test_bit(In_sync, &rdev->flags)
3831 && (rdev->recovery_offset <= sh->sector
3832 || rdev->mddev->recovery_cp <= sh->sector))
3838 static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3839 int disk_idx, int disks)
3841 struct r5dev *dev = &sh->dev[disk_idx];
3842 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3843 &sh->dev[s->failed_num[1]] };
3845 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
3848 if (test_bit(R5_LOCKED, &dev->flags) ||
3849 test_bit(R5_UPTODATE, &dev->flags))
3850 /* No point reading this as we already have it or have
3851 * decided to get it.
3856 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3857 /* We need this block to directly satisfy a request */
3860 if (s->syncing || s->expanding ||
3861 (s->replacing && want_replace(sh, disk_idx)))
3862 /* When syncing, or expanding we read everything.
3863 * When replacing, we need the replaced block.
3867 if ((s->failed >= 1 && fdev[0]->toread) ||
3868 (s->failed >= 2 && fdev[1]->toread))
3869 /* If we want to read from a failed device, then
3870 * we need to actually read every other device.
3874 /* Sometimes neither read-modify-write nor reconstruct-write
3875 * cycles can work. In those cases we read every block we
3876 * can. Then the parity-update is certain to have enough to
3878 * This can only be a problem when we need to write something,
3879 * and some device has failed. If either of those tests
3880 * fail we need look no further.
3882 if (!s->failed || !s->to_write)
3885 if (test_bit(R5_Insync, &dev->flags) &&
3886 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3887 /* Pre-reads at not permitted until after short delay
3888 * to gather multiple requests. However if this
3889 * device is no Insync, the block could only be computed
3890 * and there is no need to delay that.
3894 for (i = 0; i < s->failed && i < 2; i++) {
3895 if (fdev[i]->towrite &&
3896 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3897 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3898 /* If we have a partial write to a failed
3899 * device, then we will need to reconstruct
3900 * the content of that device, so all other
3901 * devices must be read.
3905 if (s->failed >= 2 &&
3906 (fdev[i]->towrite ||
3907 s->failed_num[i] == sh->pd_idx ||
3908 s->failed_num[i] == sh->qd_idx) &&
3909 !test_bit(R5_UPTODATE, &fdev[i]->flags))
3910 /* In max degraded raid6, If the failed disk is P, Q,
3911 * or we want to read the failed disk, we need to do
3912 * reconstruct-write.
3917 /* If we are forced to do a reconstruct-write, because parity
3918 * cannot be trusted and we are currently recovering it, there
3919 * is extra need to be careful.
3920 * If one of the devices that we would need to read, because
3921 * it is not being overwritten (and maybe not written at all)
3922 * is missing/faulty, then we need to read everything we can.
3925 sh->sector < sh->raid_conf->mddev->recovery_cp)
3926 /* reconstruct-write isn't being forced */
3928 for (i = 0; i < s->failed && i < 2; i++) {
3929 if (s->failed_num[i] != sh->pd_idx &&
3930 s->failed_num[i] != sh->qd_idx &&
3931 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3932 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3939 /* fetch_block - checks the given member device to see if its data needs
3940 * to be read or computed to satisfy a request.
3942 * Returns 1 when no more member devices need to be checked, otherwise returns
3943 * 0 to tell the loop in handle_stripe_fill to continue
3945 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3946 int disk_idx, int disks)
3948 struct r5dev *dev = &sh->dev[disk_idx];
3950 /* is the data in this block needed, and can we get it? */
3951 if (need_this_block(sh, s, disk_idx, disks)) {
3952 /* we would like to get this block, possibly by computing it,
3953 * otherwise read it if the backing disk is insync
3955 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3956 BUG_ON(test_bit(R5_Wantread, &dev->flags));
3957 BUG_ON(sh->batch_head);
3960 * In the raid6 case if the only non-uptodate disk is P
3961 * then we already trusted P to compute the other failed
3962 * drives. It is safe to compute rather than re-read P.
3963 * In other cases we only compute blocks from failed
3964 * devices, otherwise check/repair might fail to detect
3965 * a real inconsistency.
3968 if ((s->uptodate == disks - 1) &&
3969 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
3970 (s->failed && (disk_idx == s->failed_num[0] ||
3971 disk_idx == s->failed_num[1])))) {
3972 /* have disk failed, and we're requested to fetch it;
3975 pr_debug("Computing stripe %llu block %d\n",
3976 (unsigned long long)sh->sector, disk_idx);
3977 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3978 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3979 set_bit(R5_Wantcompute, &dev->flags);
3980 sh->ops.target = disk_idx;
3981 sh->ops.target2 = -1; /* no 2nd target */
3983 /* Careful: from this point on 'uptodate' is in the eye
3984 * of raid_run_ops which services 'compute' operations
3985 * before writes. R5_Wantcompute flags a block that will
3986 * be R5_UPTODATE by the time it is needed for a
3987 * subsequent operation.
3991 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3992 /* Computing 2-failure is *very* expensive; only
3993 * do it if failed >= 2
3996 for (other = disks; other--; ) {
3997 if (other == disk_idx)
3999 if (!test_bit(R5_UPTODATE,
4000 &sh->dev[other].flags))
4004 pr_debug("Computing stripe %llu blocks %d,%d\n",
4005 (unsigned long long)sh->sector,
4007 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4008 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4009 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
4010 set_bit(R5_Wantcompute, &sh->dev[other].flags);
4011 sh->ops.target = disk_idx;
4012 sh->ops.target2 = other;
4016 } else if (test_bit(R5_Insync, &dev->flags)) {
4017 set_bit(R5_LOCKED, &dev->flags);
4018 set_bit(R5_Wantread, &dev->flags);
4020 pr_debug("Reading block %d (sync=%d)\n",
4021 disk_idx, s->syncing);
4029 * handle_stripe_fill - read or compute data to satisfy pending requests.
4031 static void handle_stripe_fill(struct stripe_head *sh,
4032 struct stripe_head_state *s,
4037 /* look for blocks to read/compute, skip this if a compute
4038 * is already in flight, or if the stripe contents are in the
4039 * midst of changing due to a write
4041 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
4042 !sh->reconstruct_state) {
4045 * For degraded stripe with data in journal, do not handle
4046 * read requests yet, instead, flush the stripe to raid
4047 * disks first, this avoids handling complex rmw of write
4048 * back cache (prexor with orig_page, and then xor with
4049 * page) in the read path
4051 if (s->to_read && s->injournal && s->failed) {
4052 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
4053 r5c_make_stripe_write_out(sh);
4057 for (i = disks; i--; )
4058 if (fetch_block(sh, s, i, disks))
4062 set_bit(STRIPE_HANDLE, &sh->state);
4065 static void break_stripe_batch_list(struct stripe_head *head_sh,
4066 unsigned long handle_flags);
4067 /* handle_stripe_clean_event
4068 * any written block on an uptodate or failed drive can be returned.
4069 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
4070 * never LOCKED, so we don't need to test 'failed' directly.
4072 static void handle_stripe_clean_event(struct r5conf *conf,
4073 struct stripe_head *sh, int disks)
4077 int discard_pending = 0;
4078 struct stripe_head *head_sh = sh;
4079 bool do_endio = false;
4081 for (i = disks; i--; )
4082 if (sh->dev[i].written) {
4084 if (!test_bit(R5_LOCKED, &dev->flags) &&
4085 (test_bit(R5_UPTODATE, &dev->flags) ||
4086 test_bit(R5_Discard, &dev->flags) ||
4087 test_bit(R5_SkipCopy, &dev->flags))) {
4088 /* We can return any write requests */
4089 struct bio *wbi, *wbi2;
4090 pr_debug("Return write for disc %d\n", i);
4091 if (test_and_clear_bit(R5_Discard, &dev->flags))
4092 clear_bit(R5_UPTODATE, &dev->flags);
4093 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
4094 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
4099 dev->page = dev->orig_page;
4101 dev->written = NULL;
4102 while (wbi && wbi->bi_iter.bi_sector <
4103 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
4104 wbi2 = r5_next_bio(conf, wbi, dev->sector);
4105 md_write_end(conf->mddev);
4109 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
4110 RAID5_STRIPE_SECTORS(conf),
4111 !test_bit(STRIPE_DEGRADED, &sh->state),
4113 if (head_sh->batch_head) {
4114 sh = list_first_entry(&sh->batch_list,
4117 if (sh != head_sh) {
4124 } else if (test_bit(R5_Discard, &dev->flags))
4125 discard_pending = 1;
4128 log_stripe_write_finished(sh);
4130 if (!discard_pending &&
4131 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
4133 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
4134 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4135 if (sh->qd_idx >= 0) {
4136 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
4137 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
4139 /* now that discard is done we can proceed with any sync */
4140 clear_bit(STRIPE_DISCARD, &sh->state);
4142 * SCSI discard will change some bio fields and the stripe has
4143 * no updated data, so remove it from hash list and the stripe
4144 * will be reinitialized
4147 hash = sh->hash_lock_index;
4148 spin_lock_irq(conf->hash_locks + hash);
4150 spin_unlock_irq(conf->hash_locks + hash);
4151 if (head_sh->batch_head) {
4152 sh = list_first_entry(&sh->batch_list,
4153 struct stripe_head, batch_list);
4159 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
4160 set_bit(STRIPE_HANDLE, &sh->state);
4164 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
4165 if (atomic_dec_and_test(&conf->pending_full_writes))
4166 md_wakeup_thread(conf->mddev->thread);
4168 if (head_sh->batch_head && do_endio)
4169 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
4173 * For RMW in write back cache, we need extra page in prexor to store the
4174 * old data. This page is stored in dev->orig_page.
4176 * This function checks whether we have data for prexor. The exact logic
4178 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
4180 static inline bool uptodate_for_rmw(struct r5dev *dev)
4182 return (test_bit(R5_UPTODATE, &dev->flags)) &&
4183 (!test_bit(R5_InJournal, &dev->flags) ||
4184 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
4187 static int handle_stripe_dirtying(struct r5conf *conf,
4188 struct stripe_head *sh,
4189 struct stripe_head_state *s,
4192 int rmw = 0, rcw = 0, i;
4193 sector_t recovery_cp = conf->mddev->recovery_cp;
4195 /* Check whether resync is now happening or should start.
4196 * If yes, then the array is dirty (after unclean shutdown or
4197 * initial creation), so parity in some stripes might be inconsistent.
4198 * In this case, we need to always do reconstruct-write, to ensure
4199 * that in case of drive failure or read-error correction, we
4200 * generate correct data from the parity.
4202 if (conf->rmw_level == PARITY_DISABLE_RMW ||
4203 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
4205 /* Calculate the real rcw later - for now make it
4206 * look like rcw is cheaper
4209 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
4210 conf->rmw_level, (unsigned long long)recovery_cp,
4211 (unsigned long long)sh->sector);
4212 } else for (i = disks; i--; ) {
4213 /* would I have to read this buffer for read_modify_write */
4214 struct r5dev *dev = &sh->dev[i];
4215 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
4216 i == sh->pd_idx || i == sh->qd_idx ||
4217 test_bit(R5_InJournal, &dev->flags)) &&
4218 !test_bit(R5_LOCKED, &dev->flags) &&
4219 !(uptodate_for_rmw(dev) ||
4220 test_bit(R5_Wantcompute, &dev->flags))) {
4221 if (test_bit(R5_Insync, &dev->flags))
4224 rmw += 2*disks; /* cannot read it */
4226 /* Would I have to read this buffer for reconstruct_write */
4227 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4228 i != sh->pd_idx && i != sh->qd_idx &&
4229 !test_bit(R5_LOCKED, &dev->flags) &&
4230 !(test_bit(R5_UPTODATE, &dev->flags) ||
4231 test_bit(R5_Wantcompute, &dev->flags))) {
4232 if (test_bit(R5_Insync, &dev->flags))
4239 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
4240 (unsigned long long)sh->sector, sh->state, rmw, rcw);
4241 set_bit(STRIPE_HANDLE, &sh->state);
4242 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
4243 /* prefer read-modify-write, but need to get some data */
4244 if (conf->mddev->queue)
4245 blk_add_trace_msg(conf->mddev->queue,
4246 "raid5 rmw %llu %d",
4247 (unsigned long long)sh->sector, rmw);
4248 for (i = disks; i--; ) {
4249 struct r5dev *dev = &sh->dev[i];
4250 if (test_bit(R5_InJournal, &dev->flags) &&
4251 dev->page == dev->orig_page &&
4252 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
4253 /* alloc page for prexor */
4254 struct page *p = alloc_page(GFP_NOIO);
4262 * alloc_page() failed, try use
4263 * disk_info->extra_page
4265 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
4266 &conf->cache_state)) {
4267 r5c_use_extra_page(sh);
4271 /* extra_page in use, add to delayed_list */
4272 set_bit(STRIPE_DELAYED, &sh->state);
4273 s->waiting_extra_page = 1;
4278 for (i = disks; i--; ) {
4279 struct r5dev *dev = &sh->dev[i];
4280 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
4281 i == sh->pd_idx || i == sh->qd_idx ||
4282 test_bit(R5_InJournal, &dev->flags)) &&
4283 !test_bit(R5_LOCKED, &dev->flags) &&
4284 !(uptodate_for_rmw(dev) ||
4285 test_bit(R5_Wantcompute, &dev->flags)) &&
4286 test_bit(R5_Insync, &dev->flags)) {
4287 if (test_bit(STRIPE_PREREAD_ACTIVE,
4289 pr_debug("Read_old block %d for r-m-w\n",
4291 set_bit(R5_LOCKED, &dev->flags);
4292 set_bit(R5_Wantread, &dev->flags);
4295 set_bit(STRIPE_DELAYED, &sh->state);
4299 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
4300 /* want reconstruct write, but need to get some data */
4303 for (i = disks; i--; ) {
4304 struct r5dev *dev = &sh->dev[i];
4305 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4306 i != sh->pd_idx && i != sh->qd_idx &&
4307 !test_bit(R5_LOCKED, &dev->flags) &&
4308 !(test_bit(R5_UPTODATE, &dev->flags) ||
4309 test_bit(R5_Wantcompute, &dev->flags))) {
4311 if (test_bit(R5_Insync, &dev->flags) &&
4312 test_bit(STRIPE_PREREAD_ACTIVE,
4314 pr_debug("Read_old block "
4315 "%d for Reconstruct\n", i);
4316 set_bit(R5_LOCKED, &dev->flags);
4317 set_bit(R5_Wantread, &dev->flags);
4321 set_bit(STRIPE_DELAYED, &sh->state);
4324 if (rcw && conf->mddev->queue)
4325 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4326 (unsigned long long)sh->sector,
4327 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
4330 if (rcw > disks && rmw > disks &&
4331 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4332 set_bit(STRIPE_DELAYED, &sh->state);
4334 /* now if nothing is locked, and if we have enough data,
4335 * we can start a write request
4337 /* since handle_stripe can be called at any time we need to handle the
4338 * case where a compute block operation has been submitted and then a
4339 * subsequent call wants to start a write request. raid_run_ops only
4340 * handles the case where compute block and reconstruct are requested
4341 * simultaneously. If this is not the case then new writes need to be
4342 * held off until the compute completes.
4344 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4345 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
4346 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
4347 schedule_reconstruction(sh, s, rcw == 0, 0);
4351 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
4352 struct stripe_head_state *s, int disks)
4354 struct r5dev *dev = NULL;
4356 BUG_ON(sh->batch_head);
4357 set_bit(STRIPE_HANDLE, &sh->state);
4359 switch (sh->check_state) {
4360 case check_state_idle:
4361 /* start a new check operation if there are no failures */
4362 if (s->failed == 0) {
4363 BUG_ON(s->uptodate != disks);
4364 sh->check_state = check_state_run;
4365 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4366 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4370 dev = &sh->dev[s->failed_num[0]];
4372 case check_state_compute_result:
4373 sh->check_state = check_state_idle;
4375 dev = &sh->dev[sh->pd_idx];
4377 /* check that a write has not made the stripe insync */
4378 if (test_bit(STRIPE_INSYNC, &sh->state))
4381 /* either failed parity check, or recovery is happening */
4382 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4383 BUG_ON(s->uptodate != disks);
4385 set_bit(R5_LOCKED, &dev->flags);
4387 set_bit(R5_Wantwrite, &dev->flags);
4389 clear_bit(STRIPE_DEGRADED, &sh->state);
4390 set_bit(STRIPE_INSYNC, &sh->state);
4392 case check_state_run:
4393 break; /* we will be called again upon completion */
4394 case check_state_check_result:
4395 sh->check_state = check_state_idle;
4397 /* if a failure occurred during the check operation, leave
4398 * STRIPE_INSYNC not set and let the stripe be handled again
4403 /* handle a successful check operation, if parity is correct
4404 * we are done. Otherwise update the mismatch count and repair
4405 * parity if !MD_RECOVERY_CHECK
4407 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
4408 /* parity is correct (on disc,
4409 * not in buffer any more)
4411 set_bit(STRIPE_INSYNC, &sh->state);
4413 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
4414 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
4415 /* don't try to repair!! */
4416 set_bit(STRIPE_INSYNC, &sh->state);
4417 pr_warn_ratelimited("%s: mismatch sector in range "
4418 "%llu-%llu\n", mdname(conf->mddev),
4419 (unsigned long long) sh->sector,
4420 (unsigned long long) sh->sector +
4421 RAID5_STRIPE_SECTORS(conf));
4423 sh->check_state = check_state_compute_run;
4424 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4425 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4426 set_bit(R5_Wantcompute,
4427 &sh->dev[sh->pd_idx].flags);
4428 sh->ops.target = sh->pd_idx;
4429 sh->ops.target2 = -1;
4434 case check_state_compute_run:
4437 pr_err("%s: unknown check_state: %d sector: %llu\n",
4438 __func__, sh->check_state,
4439 (unsigned long long) sh->sector);
4444 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
4445 struct stripe_head_state *s,
4448 int pd_idx = sh->pd_idx;
4449 int qd_idx = sh->qd_idx;
4452 BUG_ON(sh->batch_head);
4453 set_bit(STRIPE_HANDLE, &sh->state);
4455 BUG_ON(s->failed > 2);
4457 /* Want to check and possibly repair P and Q.
4458 * However there could be one 'failed' device, in which
4459 * case we can only check one of them, possibly using the
4460 * other to generate missing data
4463 switch (sh->check_state) {
4464 case check_state_idle:
4465 /* start a new check operation if there are < 2 failures */
4466 if (s->failed == s->q_failed) {
4467 /* The only possible failed device holds Q, so it
4468 * makes sense to check P (If anything else were failed,
4469 * we would have used P to recreate it).
4471 sh->check_state = check_state_run;
4473 if (!s->q_failed && s->failed < 2) {
4474 /* Q is not failed, and we didn't use it to generate
4475 * anything, so it makes sense to check it
4477 if (sh->check_state == check_state_run)
4478 sh->check_state = check_state_run_pq;
4480 sh->check_state = check_state_run_q;
4483 /* discard potentially stale zero_sum_result */
4484 sh->ops.zero_sum_result = 0;
4486 if (sh->check_state == check_state_run) {
4487 /* async_xor_zero_sum destroys the contents of P */
4488 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4491 if (sh->check_state >= check_state_run &&
4492 sh->check_state <= check_state_run_pq) {
4493 /* async_syndrome_zero_sum preserves P and Q, so
4494 * no need to mark them !uptodate here
4496 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4500 /* we have 2-disk failure */
4501 BUG_ON(s->failed != 2);
4503 case check_state_compute_result:
4504 sh->check_state = check_state_idle;
4506 /* check that a write has not made the stripe insync */
4507 if (test_bit(STRIPE_INSYNC, &sh->state))
4510 /* now write out any block on a failed drive,
4511 * or P or Q if they were recomputed
4514 if (s->failed == 2) {
4515 dev = &sh->dev[s->failed_num[1]];
4517 set_bit(R5_LOCKED, &dev->flags);
4518 set_bit(R5_Wantwrite, &dev->flags);
4520 if (s->failed >= 1) {
4521 dev = &sh->dev[s->failed_num[0]];
4523 set_bit(R5_LOCKED, &dev->flags);
4524 set_bit(R5_Wantwrite, &dev->flags);
4526 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4527 dev = &sh->dev[pd_idx];
4529 set_bit(R5_LOCKED, &dev->flags);
4530 set_bit(R5_Wantwrite, &dev->flags);
4532 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4533 dev = &sh->dev[qd_idx];
4535 set_bit(R5_LOCKED, &dev->flags);
4536 set_bit(R5_Wantwrite, &dev->flags);
4538 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4539 "%s: disk%td not up to date\n",
4540 mdname(conf->mddev),
4541 dev - (struct r5dev *) &sh->dev)) {
4542 clear_bit(R5_LOCKED, &dev->flags);
4543 clear_bit(R5_Wantwrite, &dev->flags);
4546 clear_bit(STRIPE_DEGRADED, &sh->state);
4548 set_bit(STRIPE_INSYNC, &sh->state);
4550 case check_state_run:
4551 case check_state_run_q:
4552 case check_state_run_pq:
4553 break; /* we will be called again upon completion */
4554 case check_state_check_result:
4555 sh->check_state = check_state_idle;
4557 /* handle a successful check operation, if parity is correct
4558 * we are done. Otherwise update the mismatch count and repair
4559 * parity if !MD_RECOVERY_CHECK
4561 if (sh->ops.zero_sum_result == 0) {
4562 /* both parities are correct */
4564 set_bit(STRIPE_INSYNC, &sh->state);
4566 /* in contrast to the raid5 case we can validate
4567 * parity, but still have a failure to write
4570 sh->check_state = check_state_compute_result;
4571 /* Returning at this point means that we may go
4572 * off and bring p and/or q uptodate again so
4573 * we make sure to check zero_sum_result again
4574 * to verify if p or q need writeback
4578 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
4579 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
4580 /* don't try to repair!! */
4581 set_bit(STRIPE_INSYNC, &sh->state);
4582 pr_warn_ratelimited("%s: mismatch sector in range "
4583 "%llu-%llu\n", mdname(conf->mddev),
4584 (unsigned long long) sh->sector,
4585 (unsigned long long) sh->sector +
4586 RAID5_STRIPE_SECTORS(conf));
4588 int *target = &sh->ops.target;
4590 sh->ops.target = -1;
4591 sh->ops.target2 = -1;
4592 sh->check_state = check_state_compute_run;
4593 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4594 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4595 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4596 set_bit(R5_Wantcompute,
4597 &sh->dev[pd_idx].flags);
4599 target = &sh->ops.target2;
4602 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4603 set_bit(R5_Wantcompute,
4604 &sh->dev[qd_idx].flags);
4611 case check_state_compute_run:
4614 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4615 __func__, sh->check_state,
4616 (unsigned long long) sh->sector);
4621 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
4625 /* We have read all the blocks in this stripe and now we need to
4626 * copy some of them into a target stripe for expand.
4628 struct dma_async_tx_descriptor *tx = NULL;
4629 BUG_ON(sh->batch_head);
4630 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4631 for (i = 0; i < sh->disks; i++)
4632 if (i != sh->pd_idx && i != sh->qd_idx) {
4634 struct stripe_head *sh2;
4635 struct async_submit_ctl submit;
4637 sector_t bn = raid5_compute_blocknr(sh, i, 1);
4638 sector_t s = raid5_compute_sector(conf, bn, 0,
4640 sh2 = raid5_get_active_stripe(conf, NULL, s,
4641 R5_GAS_NOBLOCK | R5_GAS_NOQUIESCE);
4643 /* so far only the early blocks of this stripe
4644 * have been requested. When later blocks
4645 * get requested, we will try again
4648 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4649 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4650 /* must have already done this block */
4651 raid5_release_stripe(sh2);
4655 /* place all the copies on one channel */
4656 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
4657 tx = async_memcpy(sh2->dev[dd_idx].page,
4658 sh->dev[i].page, sh2->dev[dd_idx].offset,
4659 sh->dev[i].offset, RAID5_STRIPE_SIZE(conf),
4662 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4663 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4664 for (j = 0; j < conf->raid_disks; j++)
4665 if (j != sh2->pd_idx &&
4667 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4669 if (j == conf->raid_disks) {
4670 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4671 set_bit(STRIPE_HANDLE, &sh2->state);
4673 raid5_release_stripe(sh2);
4676 /* done submitting copies, wait for them to complete */
4677 async_tx_quiesce(&tx);
4681 * handle_stripe - do things to a stripe.
4683 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4684 * state of various bits to see what needs to be done.
4686 * return some read requests which now have data
4687 * return some write requests which are safely on storage
4688 * schedule a read on some buffers
4689 * schedule a write of some buffers
4690 * return confirmation of parity correctness
4694 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
4696 struct r5conf *conf = sh->raid_conf;
4697 int disks = sh->disks;
4700 int do_recovery = 0;
4702 memset(s, 0, sizeof(*s));
4704 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4705 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
4706 s->failed_num[0] = -1;
4707 s->failed_num[1] = -1;
4708 s->log_failed = r5l_log_disk_error(conf);
4710 /* Now to look around and see what can be done */
4712 for (i=disks; i--; ) {
4713 struct md_rdev *rdev;
4720 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4722 dev->toread, dev->towrite, dev->written);
4723 /* maybe we can reply to a read
4725 * new wantfill requests are only permitted while
4726 * ops_complete_biofill is guaranteed to be inactive
4728 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4729 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4730 set_bit(R5_Wantfill, &dev->flags);
4732 /* now count some things */
4733 if (test_bit(R5_LOCKED, &dev->flags))
4735 if (test_bit(R5_UPTODATE, &dev->flags))
4737 if (test_bit(R5_Wantcompute, &dev->flags)) {
4739 BUG_ON(s->compute > 2);
4742 if (test_bit(R5_Wantfill, &dev->flags))
4744 else if (dev->toread)
4748 if (!test_bit(R5_OVERWRITE, &dev->flags))
4753 /* Prefer to use the replacement for reads, but only
4754 * if it is recovered enough and has no bad blocks.
4756 rdev = rcu_dereference(conf->disks[i].replacement);
4757 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4758 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4759 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
4760 &first_bad, &bad_sectors))
4761 set_bit(R5_ReadRepl, &dev->flags);
4763 if (rdev && !test_bit(Faulty, &rdev->flags))
4764 set_bit(R5_NeedReplace, &dev->flags);
4766 clear_bit(R5_NeedReplace, &dev->flags);
4767 rdev = rcu_dereference(conf->disks[i].rdev);
4768 clear_bit(R5_ReadRepl, &dev->flags);
4770 if (rdev && test_bit(Faulty, &rdev->flags))
4773 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
4774 &first_bad, &bad_sectors);
4775 if (s->blocked_rdev == NULL
4776 && (test_bit(Blocked, &rdev->flags)
4779 set_bit(BlockedBadBlocks,
4781 s->blocked_rdev = rdev;
4782 atomic_inc(&rdev->nr_pending);
4785 clear_bit(R5_Insync, &dev->flags);
4789 /* also not in-sync */
4790 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4791 test_bit(R5_UPTODATE, &dev->flags)) {
4792 /* treat as in-sync, but with a read error
4793 * which we can now try to correct
4795 set_bit(R5_Insync, &dev->flags);
4796 set_bit(R5_ReadError, &dev->flags);
4798 } else if (test_bit(In_sync, &rdev->flags))
4799 set_bit(R5_Insync, &dev->flags);
4800 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
4801 /* in sync if before recovery_offset */
4802 set_bit(R5_Insync, &dev->flags);
4803 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4804 test_bit(R5_Expanded, &dev->flags))
4805 /* If we've reshaped into here, we assume it is Insync.
4806 * We will shortly update recovery_offset to make
4809 set_bit(R5_Insync, &dev->flags);
4811 if (test_bit(R5_WriteError, &dev->flags)) {
4812 /* This flag does not apply to '.replacement'
4813 * only to .rdev, so make sure to check that*/
4814 struct md_rdev *rdev2 = rcu_dereference(
4815 conf->disks[i].rdev);
4817 clear_bit(R5_Insync, &dev->flags);
4818 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4819 s->handle_bad_blocks = 1;
4820 atomic_inc(&rdev2->nr_pending);
4822 clear_bit(R5_WriteError, &dev->flags);
4824 if (test_bit(R5_MadeGood, &dev->flags)) {
4825 /* This flag does not apply to '.replacement'
4826 * only to .rdev, so make sure to check that*/
4827 struct md_rdev *rdev2 = rcu_dereference(
4828 conf->disks[i].rdev);
4829 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4830 s->handle_bad_blocks = 1;
4831 atomic_inc(&rdev2->nr_pending);
4833 clear_bit(R5_MadeGood, &dev->flags);
4835 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4836 struct md_rdev *rdev2 = rcu_dereference(
4837 conf->disks[i].replacement);
4838 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4839 s->handle_bad_blocks = 1;
4840 atomic_inc(&rdev2->nr_pending);
4842 clear_bit(R5_MadeGoodRepl, &dev->flags);
4844 if (!test_bit(R5_Insync, &dev->flags)) {
4845 /* The ReadError flag will just be confusing now */
4846 clear_bit(R5_ReadError, &dev->flags);
4847 clear_bit(R5_ReWrite, &dev->flags);
4849 if (test_bit(R5_ReadError, &dev->flags))
4850 clear_bit(R5_Insync, &dev->flags);
4851 if (!test_bit(R5_Insync, &dev->flags)) {
4853 s->failed_num[s->failed] = i;
4855 if (rdev && !test_bit(Faulty, &rdev->flags))
4858 rdev = rcu_dereference(
4859 conf->disks[i].replacement);
4860 if (rdev && !test_bit(Faulty, &rdev->flags))
4865 if (test_bit(R5_InJournal, &dev->flags))
4867 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4870 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4871 /* If there is a failed device being replaced,
4872 * we must be recovering.
4873 * else if we are after recovery_cp, we must be syncing
4874 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4875 * else we can only be replacing
4876 * sync and recovery both need to read all devices, and so
4877 * use the same flag.
4880 sh->sector >= conf->mddev->recovery_cp ||
4881 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
4890 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4891 * a head which can now be handled.
4893 static int clear_batch_ready(struct stripe_head *sh)
4895 struct stripe_head *tmp;
4896 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
4897 return (sh->batch_head && sh->batch_head != sh);
4898 spin_lock(&sh->stripe_lock);
4899 if (!sh->batch_head) {
4900 spin_unlock(&sh->stripe_lock);
4905 * this stripe could be added to a batch list before we check
4906 * BATCH_READY, skips it
4908 if (sh->batch_head != sh) {
4909 spin_unlock(&sh->stripe_lock);
4912 spin_lock(&sh->batch_lock);
4913 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4914 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4915 spin_unlock(&sh->batch_lock);
4916 spin_unlock(&sh->stripe_lock);
4919 * BATCH_READY is cleared, no new stripes can be added.
4920 * batch_list can be accessed without lock
4925 static void break_stripe_batch_list(struct stripe_head *head_sh,
4926 unsigned long handle_flags)
4928 struct stripe_head *sh, *next;
4932 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4934 list_del_init(&sh->batch_list);
4936 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
4937 (1 << STRIPE_SYNCING) |
4938 (1 << STRIPE_REPLACED) |
4939 (1 << STRIPE_DELAYED) |
4940 (1 << STRIPE_BIT_DELAY) |
4941 (1 << STRIPE_FULL_WRITE) |
4942 (1 << STRIPE_BIOFILL_RUN) |
4943 (1 << STRIPE_COMPUTE_RUN) |
4944 (1 << STRIPE_DISCARD) |
4945 (1 << STRIPE_BATCH_READY) |
4946 (1 << STRIPE_BATCH_ERR) |
4947 (1 << STRIPE_BITMAP_PENDING)),
4948 "stripe state: %lx\n", sh->state);
4949 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4950 (1 << STRIPE_REPLACED)),
4951 "head stripe state: %lx\n", head_sh->state);
4953 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
4954 (1 << STRIPE_PREREAD_ACTIVE) |
4955 (1 << STRIPE_DEGRADED) |
4956 (1 << STRIPE_ON_UNPLUG_LIST)),
4957 head_sh->state & (1 << STRIPE_INSYNC));
4959 sh->check_state = head_sh->check_state;
4960 sh->reconstruct_state = head_sh->reconstruct_state;
4961 spin_lock_irq(&sh->stripe_lock);
4962 sh->batch_head = NULL;
4963 spin_unlock_irq(&sh->stripe_lock);
4964 for (i = 0; i < sh->disks; i++) {
4965 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4967 sh->dev[i].flags = head_sh->dev[i].flags &
4968 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
4970 if (handle_flags == 0 ||
4971 sh->state & handle_flags)
4972 set_bit(STRIPE_HANDLE, &sh->state);
4973 raid5_release_stripe(sh);
4975 spin_lock_irq(&head_sh->stripe_lock);
4976 head_sh->batch_head = NULL;
4977 spin_unlock_irq(&head_sh->stripe_lock);
4978 for (i = 0; i < head_sh->disks; i++)
4979 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4981 if (head_sh->state & handle_flags)
4982 set_bit(STRIPE_HANDLE, &head_sh->state);
4985 wake_up(&head_sh->raid_conf->wait_for_overlap);
4988 static void handle_stripe(struct stripe_head *sh)
4990 struct stripe_head_state s;
4991 struct r5conf *conf = sh->raid_conf;
4994 int disks = sh->disks;
4995 struct r5dev *pdev, *qdev;
4997 clear_bit(STRIPE_HANDLE, &sh->state);
5000 * handle_stripe should not continue handle the batched stripe, only
5001 * the head of batch list or lone stripe can continue. Otherwise we
5002 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
5003 * is set for the batched stripe.
5005 if (clear_batch_ready(sh))
5008 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
5009 /* already being handled, ensure it gets handled
5010 * again when current action finishes */
5011 set_bit(STRIPE_HANDLE, &sh->state);
5015 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
5016 break_stripe_batch_list(sh, 0);
5018 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
5019 spin_lock(&sh->stripe_lock);
5021 * Cannot process 'sync' concurrently with 'discard'.
5022 * Flush data in r5cache before 'sync'.
5024 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
5025 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
5026 !test_bit(STRIPE_DISCARD, &sh->state) &&
5027 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
5028 set_bit(STRIPE_SYNCING, &sh->state);
5029 clear_bit(STRIPE_INSYNC, &sh->state);
5030 clear_bit(STRIPE_REPLACED, &sh->state);
5032 spin_unlock(&sh->stripe_lock);
5034 clear_bit(STRIPE_DELAYED, &sh->state);
5036 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
5037 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
5038 (unsigned long long)sh->sector, sh->state,
5039 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
5040 sh->check_state, sh->reconstruct_state);
5042 analyse_stripe(sh, &s);
5044 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
5047 if (s.handle_bad_blocks ||
5048 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
5049 set_bit(STRIPE_HANDLE, &sh->state);
5053 if (unlikely(s.blocked_rdev)) {
5054 if (s.syncing || s.expanding || s.expanded ||
5055 s.replacing || s.to_write || s.written) {
5056 set_bit(STRIPE_HANDLE, &sh->state);
5059 /* There is nothing for the blocked_rdev to block */
5060 rdev_dec_pending(s.blocked_rdev, conf->mddev);
5061 s.blocked_rdev = NULL;
5064 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
5065 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
5066 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
5069 pr_debug("locked=%d uptodate=%d to_read=%d"
5070 " to_write=%d failed=%d failed_num=%d,%d\n",
5071 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
5072 s.failed_num[0], s.failed_num[1]);
5074 * check if the array has lost more than max_degraded devices and,
5075 * if so, some requests might need to be failed.
5077 * When journal device failed (log_failed), we will only process
5078 * the stripe if there is data need write to raid disks
5080 if (s.failed > conf->max_degraded ||
5081 (s.log_failed && s.injournal == 0)) {
5082 sh->check_state = 0;
5083 sh->reconstruct_state = 0;
5084 break_stripe_batch_list(sh, 0);
5085 if (s.to_read+s.to_write+s.written)
5086 handle_failed_stripe(conf, sh, &s, disks);
5087 if (s.syncing + s.replacing)
5088 handle_failed_sync(conf, sh, &s);
5091 /* Now we check to see if any write operations have recently
5095 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
5097 if (sh->reconstruct_state == reconstruct_state_drain_result ||
5098 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
5099 sh->reconstruct_state = reconstruct_state_idle;
5101 /* All the 'written' buffers and the parity block are ready to
5102 * be written back to disk
5104 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
5105 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
5106 BUG_ON(sh->qd_idx >= 0 &&
5107 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
5108 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
5109 for (i = disks; i--; ) {
5110 struct r5dev *dev = &sh->dev[i];
5111 if (test_bit(R5_LOCKED, &dev->flags) &&
5112 (i == sh->pd_idx || i == sh->qd_idx ||
5113 dev->written || test_bit(R5_InJournal,
5115 pr_debug("Writing block %d\n", i);
5116 set_bit(R5_Wantwrite, &dev->flags);
5121 if (!test_bit(R5_Insync, &dev->flags) ||
5122 ((i == sh->pd_idx || i == sh->qd_idx) &&
5124 set_bit(STRIPE_INSYNC, &sh->state);
5127 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5128 s.dec_preread_active = 1;
5132 * might be able to return some write requests if the parity blocks
5133 * are safe, or on a failed drive
5135 pdev = &sh->dev[sh->pd_idx];
5136 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
5137 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
5138 qdev = &sh->dev[sh->qd_idx];
5139 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
5140 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
5144 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
5145 && !test_bit(R5_LOCKED, &pdev->flags)
5146 && (test_bit(R5_UPTODATE, &pdev->flags) ||
5147 test_bit(R5_Discard, &pdev->flags))))) &&
5148 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
5149 && !test_bit(R5_LOCKED, &qdev->flags)
5150 && (test_bit(R5_UPTODATE, &qdev->flags) ||
5151 test_bit(R5_Discard, &qdev->flags))))))
5152 handle_stripe_clean_event(conf, sh, disks);
5155 r5c_handle_cached_data_endio(conf, sh, disks);
5156 log_stripe_write_finished(sh);
5158 /* Now we might consider reading some blocks, either to check/generate
5159 * parity, or to satisfy requests
5160 * or to load a block that is being partially written.
5162 if (s.to_read || s.non_overwrite
5163 || (s.to_write && s.failed)
5164 || (s.syncing && (s.uptodate + s.compute < disks))
5167 handle_stripe_fill(sh, &s, disks);
5170 * When the stripe finishes full journal write cycle (write to journal
5171 * and raid disk), this is the clean up procedure so it is ready for
5174 r5c_finish_stripe_write_out(conf, sh, &s);
5177 * Now to consider new write requests, cache write back and what else,
5178 * if anything should be read. We do not handle new writes when:
5179 * 1/ A 'write' operation (copy+xor) is already in flight.
5180 * 2/ A 'check' operation is in flight, as it may clobber the parity
5182 * 3/ A r5c cache log write is in flight.
5185 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
5186 if (!r5c_is_writeback(conf->log)) {
5188 handle_stripe_dirtying(conf, sh, &s, disks);
5189 } else { /* write back cache */
5192 /* First, try handle writes in caching phase */
5194 ret = r5c_try_caching_write(conf, sh, &s,
5197 * If caching phase failed: ret == -EAGAIN
5199 * stripe under reclaim: !caching && injournal
5201 * fall back to handle_stripe_dirtying()
5203 if (ret == -EAGAIN ||
5204 /* stripe under reclaim: !caching && injournal */
5205 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
5207 ret = handle_stripe_dirtying(conf, sh, &s,
5215 /* maybe we need to check and possibly fix the parity for this stripe
5216 * Any reads will already have been scheduled, so we just see if enough
5217 * data is available. The parity check is held off while parity
5218 * dependent operations are in flight.
5220 if (sh->check_state ||
5221 (s.syncing && s.locked == 0 &&
5222 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5223 !test_bit(STRIPE_INSYNC, &sh->state))) {
5224 if (conf->level == 6)
5225 handle_parity_checks6(conf, sh, &s, disks);
5227 handle_parity_checks5(conf, sh, &s, disks);
5230 if ((s.replacing || s.syncing) && s.locked == 0
5231 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
5232 && !test_bit(STRIPE_REPLACED, &sh->state)) {
5233 /* Write out to replacement devices where possible */
5234 for (i = 0; i < conf->raid_disks; i++)
5235 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
5236 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
5237 set_bit(R5_WantReplace, &sh->dev[i].flags);
5238 set_bit(R5_LOCKED, &sh->dev[i].flags);
5242 set_bit(STRIPE_INSYNC, &sh->state);
5243 set_bit(STRIPE_REPLACED, &sh->state);
5245 if ((s.syncing || s.replacing) && s.locked == 0 &&
5246 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5247 test_bit(STRIPE_INSYNC, &sh->state)) {
5248 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
5249 clear_bit(STRIPE_SYNCING, &sh->state);
5250 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
5251 wake_up(&conf->wait_for_overlap);
5254 /* If the failed drives are just a ReadError, then we might need
5255 * to progress the repair/check process
5257 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
5258 for (i = 0; i < s.failed; i++) {
5259 struct r5dev *dev = &sh->dev[s.failed_num[i]];
5260 if (test_bit(R5_ReadError, &dev->flags)
5261 && !test_bit(R5_LOCKED, &dev->flags)
5262 && test_bit(R5_UPTODATE, &dev->flags)
5264 if (!test_bit(R5_ReWrite, &dev->flags)) {
5265 set_bit(R5_Wantwrite, &dev->flags);
5266 set_bit(R5_ReWrite, &dev->flags);
5268 /* let's read it back */
5269 set_bit(R5_Wantread, &dev->flags);
5270 set_bit(R5_LOCKED, &dev->flags);
5275 /* Finish reconstruct operations initiated by the expansion process */
5276 if (sh->reconstruct_state == reconstruct_state_result) {
5277 struct stripe_head *sh_src
5278 = raid5_get_active_stripe(conf, NULL, sh->sector,
5279 R5_GAS_PREVIOUS | R5_GAS_NOBLOCK |
5281 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
5282 /* sh cannot be written until sh_src has been read.
5283 * so arrange for sh to be delayed a little
5285 set_bit(STRIPE_DELAYED, &sh->state);
5286 set_bit(STRIPE_HANDLE, &sh->state);
5287 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
5289 atomic_inc(&conf->preread_active_stripes);
5290 raid5_release_stripe(sh_src);
5294 raid5_release_stripe(sh_src);
5296 sh->reconstruct_state = reconstruct_state_idle;
5297 clear_bit(STRIPE_EXPANDING, &sh->state);
5298 for (i = conf->raid_disks; i--; ) {
5299 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5300 set_bit(R5_LOCKED, &sh->dev[i].flags);
5305 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5306 !sh->reconstruct_state) {
5307 /* Need to write out all blocks after computing parity */
5308 sh->disks = conf->raid_disks;
5309 stripe_set_idx(sh->sector, conf, 0, sh);
5310 schedule_reconstruction(sh, &s, 1, 1);
5311 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5312 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5313 atomic_dec(&conf->reshape_stripes);
5314 wake_up(&conf->wait_for_overlap);
5315 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
5318 if (s.expanding && s.locked == 0 &&
5319 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5320 handle_stripe_expansion(conf, sh);
5323 /* wait for this device to become unblocked */
5324 if (unlikely(s.blocked_rdev)) {
5325 if (conf->mddev->external)
5326 md_wait_for_blocked_rdev(s.blocked_rdev,
5329 /* Internal metadata will immediately
5330 * be written by raid5d, so we don't
5331 * need to wait here.
5333 rdev_dec_pending(s.blocked_rdev,
5337 if (s.handle_bad_blocks)
5338 for (i = disks; i--; ) {
5339 struct md_rdev *rdev;
5340 struct r5dev *dev = &sh->dev[i];
5341 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5342 /* We own a safe reference to the rdev */
5343 rdev = rdev_pend_deref(conf->disks[i].rdev);
5344 if (!rdev_set_badblocks(rdev, sh->sector,
5345 RAID5_STRIPE_SECTORS(conf), 0))
5346 md_error(conf->mddev, rdev);
5347 rdev_dec_pending(rdev, conf->mddev);
5349 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5350 rdev = rdev_pend_deref(conf->disks[i].rdev);
5351 rdev_clear_badblocks(rdev, sh->sector,
5352 RAID5_STRIPE_SECTORS(conf), 0);
5353 rdev_dec_pending(rdev, conf->mddev);
5355 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5356 rdev = rdev_pend_deref(conf->disks[i].replacement);
5358 /* rdev have been moved down */
5359 rdev = rdev_pend_deref(conf->disks[i].rdev);
5360 rdev_clear_badblocks(rdev, sh->sector,
5361 RAID5_STRIPE_SECTORS(conf), 0);
5362 rdev_dec_pending(rdev, conf->mddev);
5367 raid_run_ops(sh, s.ops_request);
5371 if (s.dec_preread_active) {
5372 /* We delay this until after ops_run_io so that if make_request
5373 * is waiting on a flush, it won't continue until the writes
5374 * have actually been submitted.
5376 atomic_dec(&conf->preread_active_stripes);
5377 if (atomic_read(&conf->preread_active_stripes) <
5379 md_wakeup_thread(conf->mddev->thread);
5382 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
5385 static void raid5_activate_delayed(struct r5conf *conf)
5386 __must_hold(&conf->device_lock)
5388 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5389 while (!list_empty(&conf->delayed_list)) {
5390 struct list_head *l = conf->delayed_list.next;
5391 struct stripe_head *sh;
5392 sh = list_entry(l, struct stripe_head, lru);
5394 clear_bit(STRIPE_DELAYED, &sh->state);
5395 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5396 atomic_inc(&conf->preread_active_stripes);
5397 list_add_tail(&sh->lru, &conf->hold_list);
5398 raid5_wakeup_stripe_thread(sh);
5403 static void activate_bit_delay(struct r5conf *conf,
5404 struct list_head *temp_inactive_list)
5405 __must_hold(&conf->device_lock)
5407 struct list_head head;
5408 list_add(&head, &conf->bitmap_list);
5409 list_del_init(&conf->bitmap_list);
5410 while (!list_empty(&head)) {
5411 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
5413 list_del_init(&sh->lru);
5414 atomic_inc(&sh->count);
5415 hash = sh->hash_lock_index;
5416 __release_stripe(conf, sh, &temp_inactive_list[hash]);
5420 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
5422 struct r5conf *conf = mddev->private;
5423 sector_t sector = bio->bi_iter.bi_sector;
5424 unsigned int chunk_sectors;
5425 unsigned int bio_sectors = bio_sectors(bio);
5427 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
5428 return chunk_sectors >=
5429 ((sector & (chunk_sectors - 1)) + bio_sectors);
5433 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5434 * later sampled by raid5d.
5436 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
5438 unsigned long flags;
5440 spin_lock_irqsave(&conf->device_lock, flags);
5442 bi->bi_next = conf->retry_read_aligned_list;
5443 conf->retry_read_aligned_list = bi;
5445 spin_unlock_irqrestore(&conf->device_lock, flags);
5446 md_wakeup_thread(conf->mddev->thread);
5449 static struct bio *remove_bio_from_retry(struct r5conf *conf,
5450 unsigned int *offset)
5454 bi = conf->retry_read_aligned;
5456 *offset = conf->retry_read_offset;
5457 conf->retry_read_aligned = NULL;
5460 bi = conf->retry_read_aligned_list;
5462 conf->retry_read_aligned_list = bi->bi_next;
5471 * The "raid5_align_endio" should check if the read succeeded and if it
5472 * did, call bio_endio on the original bio (having bio_put the new bio
5474 * If the read failed..
5476 static void raid5_align_endio(struct bio *bi)
5478 struct bio *raid_bi = bi->bi_private;
5479 struct md_rdev *rdev = (void *)raid_bi->bi_next;
5480 struct mddev *mddev = rdev->mddev;
5481 struct r5conf *conf = mddev->private;
5482 blk_status_t error = bi->bi_status;
5485 raid_bi->bi_next = NULL;
5486 rdev_dec_pending(rdev, conf->mddev);
5490 if (atomic_dec_and_test(&conf->active_aligned_reads))
5491 wake_up(&conf->wait_for_quiescent);
5495 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
5497 add_bio_to_retry(raid_bi, conf);
5500 static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
5502 struct r5conf *conf = mddev->private;
5503 struct bio *align_bio;
5504 struct md_rdev *rdev;
5505 sector_t sector, end_sector, first_bad;
5506 int bad_sectors, dd_idx;
5509 if (!in_chunk_boundary(mddev, raid_bio)) {
5510 pr_debug("%s: non aligned\n", __func__);
5514 sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0,
5516 end_sector = sector + bio_sectors(raid_bio);
5519 if (r5c_big_stripe_cached(conf, sector))
5520 goto out_rcu_unlock;
5522 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5523 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5524 rdev->recovery_offset < end_sector) {
5525 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5527 goto out_rcu_unlock;
5528 if (test_bit(Faulty, &rdev->flags) ||
5529 !(test_bit(In_sync, &rdev->flags) ||
5530 rdev->recovery_offset >= end_sector))
5531 goto out_rcu_unlock;
5534 atomic_inc(&rdev->nr_pending);
5537 if (is_badblock(rdev, sector, bio_sectors(raid_bio), &first_bad,
5539 rdev_dec_pending(rdev, mddev);
5543 md_account_bio(mddev, &raid_bio);
5544 raid_bio->bi_next = (void *)rdev;
5546 align_bio = bio_alloc_clone(rdev->bdev, raid_bio, GFP_NOIO,
5548 align_bio->bi_end_io = raid5_align_endio;
5549 align_bio->bi_private = raid_bio;
5550 align_bio->bi_iter.bi_sector = sector;
5552 /* No reshape active, so we can trust rdev->data_offset */
5553 align_bio->bi_iter.bi_sector += rdev->data_offset;
5556 if (conf->quiesce == 0) {
5557 atomic_inc(&conf->active_aligned_reads);
5560 /* need a memory barrier to detect the race with raid5_quiesce() */
5561 if (!did_inc || smp_load_acquire(&conf->quiesce) != 0) {
5562 /* quiesce is in progress, so we need to undo io activation and wait
5565 if (did_inc && atomic_dec_and_test(&conf->active_aligned_reads))
5566 wake_up(&conf->wait_for_quiescent);
5567 spin_lock_irq(&conf->device_lock);
5568 wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0,
5570 atomic_inc(&conf->active_aligned_reads);
5571 spin_unlock_irq(&conf->device_lock);
5575 trace_block_bio_remap(align_bio, disk_devt(mddev->gendisk),
5576 raid_bio->bi_iter.bi_sector);
5577 submit_bio_noacct(align_bio);
5585 static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5588 sector_t sector = raid_bio->bi_iter.bi_sector;
5589 unsigned chunk_sects = mddev->chunk_sectors;
5590 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5592 if (sectors < bio_sectors(raid_bio)) {
5593 struct r5conf *conf = mddev->private;
5594 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
5595 bio_chain(split, raid_bio);
5596 submit_bio_noacct(raid_bio);
5600 if (!raid5_read_one_chunk(mddev, raid_bio))
5606 /* __get_priority_stripe - get the next stripe to process
5608 * Full stripe writes are allowed to pass preread active stripes up until
5609 * the bypass_threshold is exceeded. In general the bypass_count
5610 * increments when the handle_list is handled before the hold_list; however, it
5611 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5612 * stripe with in flight i/o. The bypass_count will be reset when the
5613 * head of the hold_list has changed, i.e. the head was promoted to the
5616 static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
5617 __must_hold(&conf->device_lock)
5619 struct stripe_head *sh, *tmp;
5620 struct list_head *handle_list = NULL;
5621 struct r5worker_group *wg;
5622 bool second_try = !r5c_is_writeback(conf->log) &&
5623 !r5l_log_disk_error(conf);
5624 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5625 r5l_log_disk_error(conf);
5630 if (conf->worker_cnt_per_group == 0) {
5631 handle_list = try_loprio ? &conf->loprio_list :
5633 } else if (group != ANY_GROUP) {
5634 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5635 &conf->worker_groups[group].handle_list;
5636 wg = &conf->worker_groups[group];
5639 for (i = 0; i < conf->group_cnt; i++) {
5640 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5641 &conf->worker_groups[i].handle_list;
5642 wg = &conf->worker_groups[i];
5643 if (!list_empty(handle_list))
5648 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5650 list_empty(handle_list) ? "empty" : "busy",
5651 list_empty(&conf->hold_list) ? "empty" : "busy",
5652 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5654 if (!list_empty(handle_list)) {
5655 sh = list_entry(handle_list->next, typeof(*sh), lru);
5657 if (list_empty(&conf->hold_list))
5658 conf->bypass_count = 0;
5659 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5660 if (conf->hold_list.next == conf->last_hold)
5661 conf->bypass_count++;
5663 conf->last_hold = conf->hold_list.next;
5664 conf->bypass_count -= conf->bypass_threshold;
5665 if (conf->bypass_count < 0)
5666 conf->bypass_count = 0;
5669 } else if (!list_empty(&conf->hold_list) &&
5670 ((conf->bypass_threshold &&
5671 conf->bypass_count > conf->bypass_threshold) ||
5672 atomic_read(&conf->pending_full_writes) == 0)) {
5674 list_for_each_entry(tmp, &conf->hold_list, lru) {
5675 if (conf->worker_cnt_per_group == 0 ||
5676 group == ANY_GROUP ||
5677 !cpu_online(tmp->cpu) ||
5678 cpu_to_group(tmp->cpu) == group) {
5685 conf->bypass_count -= conf->bypass_threshold;
5686 if (conf->bypass_count < 0)
5687 conf->bypass_count = 0;
5696 try_loprio = !try_loprio;
5704 list_del_init(&sh->lru);
5705 BUG_ON(atomic_inc_return(&sh->count) != 1);
5709 struct raid5_plug_cb {
5710 struct blk_plug_cb cb;
5711 struct list_head list;
5712 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
5715 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5717 struct raid5_plug_cb *cb = container_of(
5718 blk_cb, struct raid5_plug_cb, cb);
5719 struct stripe_head *sh;
5720 struct mddev *mddev = cb->cb.data;
5721 struct r5conf *conf = mddev->private;
5725 if (cb->list.next && !list_empty(&cb->list)) {
5726 spin_lock_irq(&conf->device_lock);
5727 while (!list_empty(&cb->list)) {
5728 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5729 list_del_init(&sh->lru);
5731 * avoid race release_stripe_plug() sees
5732 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5733 * is still in our list
5735 smp_mb__before_atomic();
5736 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
5738 * STRIPE_ON_RELEASE_LIST could be set here. In that
5739 * case, the count is always > 1 here
5741 hash = sh->hash_lock_index;
5742 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
5745 spin_unlock_irq(&conf->device_lock);
5747 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5748 NR_STRIPE_HASH_LOCKS);
5750 trace_block_unplug(mddev->queue, cnt, !from_schedule);
5754 static void release_stripe_plug(struct mddev *mddev,
5755 struct stripe_head *sh)
5757 struct blk_plug_cb *blk_cb = blk_check_plugged(
5758 raid5_unplug, mddev,
5759 sizeof(struct raid5_plug_cb));
5760 struct raid5_plug_cb *cb;
5763 raid5_release_stripe(sh);
5767 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5769 if (cb->list.next == NULL) {
5771 INIT_LIST_HEAD(&cb->list);
5772 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5773 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5776 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5777 list_add_tail(&sh->lru, &cb->list);
5779 raid5_release_stripe(sh);
5782 static void make_discard_request(struct mddev *mddev, struct bio *bi)
5784 struct r5conf *conf = mddev->private;
5785 sector_t logical_sector, last_sector;
5786 struct stripe_head *sh;
5789 /* We need to handle this when io_uring supports discard/trim */
5790 if (WARN_ON_ONCE(bi->bi_opf & REQ_NOWAIT))
5793 if (mddev->reshape_position != MaxSector)
5794 /* Skip discard while reshape is happening */
5797 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
5798 last_sector = bio_end_sector(bi);
5802 stripe_sectors = conf->chunk_sectors *
5803 (conf->raid_disks - conf->max_degraded);
5804 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5806 sector_div(last_sector, stripe_sectors);
5808 logical_sector *= conf->chunk_sectors;
5809 last_sector *= conf->chunk_sectors;
5811 for (; logical_sector < last_sector;
5812 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
5816 sh = raid5_get_active_stripe(conf, NULL, logical_sector, 0);
5817 prepare_to_wait(&conf->wait_for_overlap, &w,
5818 TASK_UNINTERRUPTIBLE);
5819 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5820 if (test_bit(STRIPE_SYNCING, &sh->state)) {
5821 raid5_release_stripe(sh);
5825 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5826 spin_lock_irq(&sh->stripe_lock);
5827 for (d = 0; d < conf->raid_disks; d++) {
5828 if (d == sh->pd_idx || d == sh->qd_idx)
5830 if (sh->dev[d].towrite || sh->dev[d].toread) {
5831 set_bit(R5_Overlap, &sh->dev[d].flags);
5832 spin_unlock_irq(&sh->stripe_lock);
5833 raid5_release_stripe(sh);
5838 set_bit(STRIPE_DISCARD, &sh->state);
5839 finish_wait(&conf->wait_for_overlap, &w);
5840 sh->overwrite_disks = 0;
5841 for (d = 0; d < conf->raid_disks; d++) {
5842 if (d == sh->pd_idx || d == sh->qd_idx)
5844 sh->dev[d].towrite = bi;
5845 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5846 bio_inc_remaining(bi);
5847 md_write_inc(mddev, bi);
5848 sh->overwrite_disks++;
5850 spin_unlock_irq(&sh->stripe_lock);
5851 if (conf->mddev->bitmap) {
5853 d < conf->raid_disks - conf->max_degraded;
5855 md_bitmap_startwrite(mddev->bitmap,
5857 RAID5_STRIPE_SECTORS(conf),
5859 sh->bm_seq = conf->seq_flush + 1;
5860 set_bit(STRIPE_BIT_DELAY, &sh->state);
5863 set_bit(STRIPE_HANDLE, &sh->state);
5864 clear_bit(STRIPE_DELAYED, &sh->state);
5865 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5866 atomic_inc(&conf->preread_active_stripes);
5867 release_stripe_plug(mddev, sh);
5873 static bool ahead_of_reshape(struct mddev *mddev, sector_t sector,
5874 sector_t reshape_sector)
5876 return mddev->reshape_backwards ? sector < reshape_sector :
5877 sector >= reshape_sector;
5880 static bool range_ahead_of_reshape(struct mddev *mddev, sector_t min,
5881 sector_t max, sector_t reshape_sector)
5883 return mddev->reshape_backwards ? max < reshape_sector :
5884 min >= reshape_sector;
5887 static bool stripe_ahead_of_reshape(struct mddev *mddev, struct r5conf *conf,
5888 struct stripe_head *sh)
5890 sector_t max_sector = 0, min_sector = MaxSector;
5894 for (dd_idx = 0; dd_idx < sh->disks; dd_idx++) {
5895 if (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
5898 min_sector = min(min_sector, sh->dev[dd_idx].sector);
5899 max_sector = max(max_sector, sh->dev[dd_idx].sector);
5902 spin_lock_irq(&conf->device_lock);
5904 if (!range_ahead_of_reshape(mddev, min_sector, max_sector,
5905 conf->reshape_progress))
5906 /* mismatch, need to try again */
5909 spin_unlock_irq(&conf->device_lock);
5914 static int add_all_stripe_bios(struct r5conf *conf,
5915 struct stripe_request_ctx *ctx, struct stripe_head *sh,
5916 struct bio *bi, int forwrite, int previous)
5921 spin_lock_irq(&sh->stripe_lock);
5923 for (dd_idx = 0; dd_idx < sh->disks; dd_idx++) {
5924 struct r5dev *dev = &sh->dev[dd_idx];
5926 if (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
5929 if (dev->sector < ctx->first_sector ||
5930 dev->sector >= ctx->last_sector)
5933 if (stripe_bio_overlaps(sh, bi, dd_idx, forwrite)) {
5934 set_bit(R5_Overlap, &dev->flags);
5943 for (dd_idx = 0; dd_idx < sh->disks; dd_idx++) {
5944 struct r5dev *dev = &sh->dev[dd_idx];
5946 if (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
5949 if (dev->sector < ctx->first_sector ||
5950 dev->sector >= ctx->last_sector)
5953 __add_stripe_bio(sh, bi, dd_idx, forwrite, previous);
5954 clear_bit((dev->sector - ctx->first_sector) >>
5955 RAID5_STRIPE_SHIFT(conf), ctx->sectors_to_do);
5959 spin_unlock_irq(&sh->stripe_lock);
5963 static enum stripe_result make_stripe_request(struct mddev *mddev,
5964 struct r5conf *conf, struct stripe_request_ctx *ctx,
5965 sector_t logical_sector, struct bio *bi)
5967 const int rw = bio_data_dir(bi);
5968 enum stripe_result ret;
5969 struct stripe_head *sh;
5970 sector_t new_sector;
5971 int previous = 0, flags = 0;
5974 seq = read_seqcount_begin(&conf->gen_lock);
5976 if (unlikely(conf->reshape_progress != MaxSector)) {
5978 * Spinlock is needed as reshape_progress may be
5979 * 64bit on a 32bit platform, and so it might be
5980 * possible to see a half-updated value
5981 * Of course reshape_progress could change after
5982 * the lock is dropped, so once we get a reference
5983 * to the stripe that we think it is, we will have
5986 spin_lock_irq(&conf->device_lock);
5987 if (ahead_of_reshape(mddev, logical_sector,
5988 conf->reshape_progress)) {
5991 if (ahead_of_reshape(mddev, logical_sector,
5992 conf->reshape_safe)) {
5993 spin_unlock_irq(&conf->device_lock);
5994 return STRIPE_SCHEDULE_AND_RETRY;
5997 spin_unlock_irq(&conf->device_lock);
6000 new_sector = raid5_compute_sector(conf, logical_sector, previous,
6002 pr_debug("raid456: %s, sector %llu logical %llu\n", __func__,
6003 new_sector, logical_sector);
6006 flags |= R5_GAS_PREVIOUS;
6007 if (bi->bi_opf & REQ_RAHEAD)
6008 flags |= R5_GAS_NOBLOCK;
6009 sh = raid5_get_active_stripe(conf, ctx, new_sector, flags);
6010 if (unlikely(!sh)) {
6011 /* cannot get stripe, just give-up */
6012 bi->bi_status = BLK_STS_IOERR;
6016 if (unlikely(previous) &&
6017 stripe_ahead_of_reshape(mddev, conf, sh)) {
6019 * Expansion moved on while waiting for a stripe.
6020 * Expansion could still move past after this
6021 * test, but as we are holding a reference to
6022 * 'sh', we know that if that happens,
6023 * STRIPE_EXPANDING will get set and the expansion
6024 * won't proceed until we finish with the stripe.
6026 ret = STRIPE_SCHEDULE_AND_RETRY;
6030 if (read_seqcount_retry(&conf->gen_lock, seq)) {
6031 /* Might have got the wrong stripe_head by accident */
6036 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
6037 !add_all_stripe_bios(conf, ctx, sh, bi, rw, previous)) {
6039 * Stripe is busy expanding or add failed due to
6040 * overlap. Flush everything and wait a while.
6042 md_wakeup_thread(mddev->thread);
6043 ret = STRIPE_SCHEDULE_AND_RETRY;
6047 if (stripe_can_batch(sh)) {
6048 stripe_add_to_batch_list(conf, sh, ctx->batch_last);
6049 if (ctx->batch_last)
6050 raid5_release_stripe(ctx->batch_last);
6051 atomic_inc(&sh->count);
6052 ctx->batch_last = sh;
6055 if (ctx->do_flush) {
6056 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
6057 /* we only need flush for one stripe */
6058 ctx->do_flush = false;
6061 set_bit(STRIPE_HANDLE, &sh->state);
6062 clear_bit(STRIPE_DELAYED, &sh->state);
6063 if ((!sh->batch_head || sh == sh->batch_head) &&
6064 (bi->bi_opf & REQ_SYNC) &&
6065 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
6066 atomic_inc(&conf->preread_active_stripes);
6068 release_stripe_plug(mddev, sh);
6069 return STRIPE_SUCCESS;
6072 raid5_release_stripe(sh);
6077 * If the bio covers multiple data disks, find sector within the bio that has
6078 * the lowest chunk offset in the first chunk.
6080 static sector_t raid5_bio_lowest_chunk_sector(struct r5conf *conf,
6083 int sectors_per_chunk = conf->chunk_sectors;
6084 int raid_disks = conf->raid_disks;
6086 struct stripe_head sh;
6087 unsigned int chunk_offset;
6088 sector_t r_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
6091 /* We pass in fake stripe_head to get back parity disk numbers */
6092 sector = raid5_compute_sector(conf, r_sector, 0, &dd_idx, &sh);
6093 chunk_offset = sector_div(sector, sectors_per_chunk);
6094 if (sectors_per_chunk - chunk_offset >= bio_sectors(bi))
6097 * Bio crosses to the next data disk. Check whether it's in the same
6101 while (dd_idx == sh.pd_idx || dd_idx == sh.qd_idx)
6103 if (dd_idx >= raid_disks)
6105 return r_sector + sectors_per_chunk - chunk_offset;
6108 static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
6110 DEFINE_WAIT_FUNC(wait, woken_wake_function);
6111 struct r5conf *conf = mddev->private;
6112 sector_t logical_sector;
6113 struct stripe_request_ctx ctx = {};
6114 const int rw = bio_data_dir(bi);
6115 enum stripe_result res;
6118 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
6119 int ret = log_handle_flush_request(conf, bi);
6123 if (ret == -ENODEV) {
6124 if (md_flush_request(mddev, bi))
6127 /* ret == -EAGAIN, fallback */
6129 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
6130 * we need to flush journal device
6132 ctx.do_flush = bi->bi_opf & REQ_PREFLUSH;
6135 if (!md_write_start(mddev, bi))
6138 * If array is degraded, better not do chunk aligned read because
6139 * later we might have to read it again in order to reconstruct
6140 * data on failed drives.
6142 if (rw == READ && mddev->degraded == 0 &&
6143 mddev->reshape_position == MaxSector) {
6144 bi = chunk_aligned_read(mddev, bi);
6149 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
6150 make_discard_request(mddev, bi);
6151 md_write_end(mddev);
6155 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
6156 ctx.first_sector = logical_sector;
6157 ctx.last_sector = bio_end_sector(bi);
6160 stripe_cnt = DIV_ROUND_UP_SECTOR_T(ctx.last_sector - logical_sector,
6161 RAID5_STRIPE_SECTORS(conf));
6162 bitmap_set(ctx.sectors_to_do, 0, stripe_cnt);
6164 pr_debug("raid456: %s, logical %llu to %llu\n", __func__,
6165 bi->bi_iter.bi_sector, ctx.last_sector);
6167 /* Bail out if conflicts with reshape and REQ_NOWAIT is set */
6168 if ((bi->bi_opf & REQ_NOWAIT) &&
6169 (conf->reshape_progress != MaxSector) &&
6170 !ahead_of_reshape(mddev, logical_sector, conf->reshape_progress) &&
6171 ahead_of_reshape(mddev, logical_sector, conf->reshape_safe)) {
6172 bio_wouldblock_error(bi);
6174 md_write_end(mddev);
6177 md_account_bio(mddev, &bi);
6180 * Lets start with the stripe with the lowest chunk offset in the first
6181 * chunk. That has the best chances of creating IOs adjacent to
6182 * previous IOs in case of sequential IO and thus creates the most
6183 * sequential IO pattern. We don't bother with the optimization when
6184 * reshaping as the performance benefit is not worth the complexity.
6186 if (likely(conf->reshape_progress == MaxSector))
6187 logical_sector = raid5_bio_lowest_chunk_sector(conf, bi);
6188 s = (logical_sector - ctx.first_sector) >> RAID5_STRIPE_SHIFT(conf);
6190 add_wait_queue(&conf->wait_for_overlap, &wait);
6192 res = make_stripe_request(mddev, conf, &ctx, logical_sector,
6194 if (res == STRIPE_FAIL)
6197 if (res == STRIPE_RETRY)
6200 if (res == STRIPE_SCHEDULE_AND_RETRY) {
6202 * Must release the reference to batch_last before
6203 * scheduling and waiting for work to be done,
6204 * otherwise the batch_last stripe head could prevent
6205 * raid5_activate_delayed() from making progress
6206 * and thus deadlocking.
6208 if (ctx.batch_last) {
6209 raid5_release_stripe(ctx.batch_last);
6210 ctx.batch_last = NULL;
6213 wait_woken(&wait, TASK_UNINTERRUPTIBLE,
6214 MAX_SCHEDULE_TIMEOUT);
6218 s = find_next_bit_wrap(ctx.sectors_to_do, stripe_cnt, s);
6219 if (s == stripe_cnt)
6222 logical_sector = ctx.first_sector +
6223 (s << RAID5_STRIPE_SHIFT(conf));
6225 remove_wait_queue(&conf->wait_for_overlap, &wait);
6228 raid5_release_stripe(ctx.batch_last);
6231 md_write_end(mddev);
6236 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
6238 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
6240 /* reshaping is quite different to recovery/resync so it is
6241 * handled quite separately ... here.
6243 * On each call to sync_request, we gather one chunk worth of
6244 * destination stripes and flag them as expanding.
6245 * Then we find all the source stripes and request reads.
6246 * As the reads complete, handle_stripe will copy the data
6247 * into the destination stripe and release that stripe.
6249 struct r5conf *conf = mddev->private;
6250 struct stripe_head *sh;
6251 struct md_rdev *rdev;
6252 sector_t first_sector, last_sector;
6253 int raid_disks = conf->previous_raid_disks;
6254 int data_disks = raid_disks - conf->max_degraded;
6255 int new_data_disks = conf->raid_disks - conf->max_degraded;
6258 sector_t writepos, readpos, safepos;
6259 sector_t stripe_addr;
6260 int reshape_sectors;
6261 struct list_head stripes;
6264 if (sector_nr == 0) {
6265 /* If restarting in the middle, skip the initial sectors */
6266 if (mddev->reshape_backwards &&
6267 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
6268 sector_nr = raid5_size(mddev, 0, 0)
6269 - conf->reshape_progress;
6270 } else if (mddev->reshape_backwards &&
6271 conf->reshape_progress == MaxSector) {
6272 /* shouldn't happen, but just in case, finish up.*/
6273 sector_nr = MaxSector;
6274 } else if (!mddev->reshape_backwards &&
6275 conf->reshape_progress > 0)
6276 sector_nr = conf->reshape_progress;
6277 sector_div(sector_nr, new_data_disks);
6279 mddev->curr_resync_completed = sector_nr;
6280 sysfs_notify_dirent_safe(mddev->sysfs_completed);
6287 /* We need to process a full chunk at a time.
6288 * If old and new chunk sizes differ, we need to process the
6292 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
6294 /* We update the metadata at least every 10 seconds, or when
6295 * the data about to be copied would over-write the source of
6296 * the data at the front of the range. i.e. one new_stripe
6297 * along from reshape_progress new_maps to after where
6298 * reshape_safe old_maps to
6300 writepos = conf->reshape_progress;
6301 sector_div(writepos, new_data_disks);
6302 readpos = conf->reshape_progress;
6303 sector_div(readpos, data_disks);
6304 safepos = conf->reshape_safe;
6305 sector_div(safepos, data_disks);
6306 if (mddev->reshape_backwards) {
6307 BUG_ON(writepos < reshape_sectors);
6308 writepos -= reshape_sectors;
6309 readpos += reshape_sectors;
6310 safepos += reshape_sectors;
6312 writepos += reshape_sectors;
6313 /* readpos and safepos are worst-case calculations.
6314 * A negative number is overly pessimistic, and causes
6315 * obvious problems for unsigned storage. So clip to 0.
6317 readpos -= min_t(sector_t, reshape_sectors, readpos);
6318 safepos -= min_t(sector_t, reshape_sectors, safepos);
6321 /* Having calculated the 'writepos' possibly use it
6322 * to set 'stripe_addr' which is where we will write to.
6324 if (mddev->reshape_backwards) {
6325 BUG_ON(conf->reshape_progress == 0);
6326 stripe_addr = writepos;
6327 BUG_ON((mddev->dev_sectors &
6328 ~((sector_t)reshape_sectors - 1))
6329 - reshape_sectors - stripe_addr
6332 BUG_ON(writepos != sector_nr + reshape_sectors);
6333 stripe_addr = sector_nr;
6336 /* 'writepos' is the most advanced device address we might write.
6337 * 'readpos' is the least advanced device address we might read.
6338 * 'safepos' is the least address recorded in the metadata as having
6340 * If there is a min_offset_diff, these are adjusted either by
6341 * increasing the safepos/readpos if diff is negative, or
6342 * increasing writepos if diff is positive.
6343 * If 'readpos' is then behind 'writepos', there is no way that we can
6344 * ensure safety in the face of a crash - that must be done by userspace
6345 * making a backup of the data. So in that case there is no particular
6346 * rush to update metadata.
6347 * Otherwise if 'safepos' is behind 'writepos', then we really need to
6348 * update the metadata to advance 'safepos' to match 'readpos' so that
6349 * we can be safe in the event of a crash.
6350 * So we insist on updating metadata if safepos is behind writepos and
6351 * readpos is beyond writepos.
6352 * In any case, update the metadata every 10 seconds.
6353 * Maybe that number should be configurable, but I'm not sure it is
6354 * worth it.... maybe it could be a multiple of safemode_delay???
6356 if (conf->min_offset_diff < 0) {
6357 safepos += -conf->min_offset_diff;
6358 readpos += -conf->min_offset_diff;
6360 writepos += conf->min_offset_diff;
6362 if ((mddev->reshape_backwards
6363 ? (safepos > writepos && readpos < writepos)
6364 : (safepos < writepos && readpos > writepos)) ||
6365 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
6366 /* Cannot proceed until we've updated the superblock... */
6367 wait_event(conf->wait_for_overlap,
6368 atomic_read(&conf->reshape_stripes)==0
6369 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6370 if (atomic_read(&conf->reshape_stripes) != 0)
6372 mddev->reshape_position = conf->reshape_progress;
6373 mddev->curr_resync_completed = sector_nr;
6374 if (!mddev->reshape_backwards)
6375 /* Can update recovery_offset */
6376 rdev_for_each(rdev, mddev)
6377 if (rdev->raid_disk >= 0 &&
6378 !test_bit(Journal, &rdev->flags) &&
6379 !test_bit(In_sync, &rdev->flags) &&
6380 rdev->recovery_offset < sector_nr)
6381 rdev->recovery_offset = sector_nr;
6383 conf->reshape_checkpoint = jiffies;
6384 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6385 md_wakeup_thread(mddev->thread);
6386 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
6387 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6388 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6390 spin_lock_irq(&conf->device_lock);
6391 conf->reshape_safe = mddev->reshape_position;
6392 spin_unlock_irq(&conf->device_lock);
6393 wake_up(&conf->wait_for_overlap);
6394 sysfs_notify_dirent_safe(mddev->sysfs_completed);
6397 INIT_LIST_HEAD(&stripes);
6398 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
6400 int skipped_disk = 0;
6401 sh = raid5_get_active_stripe(conf, NULL, stripe_addr+i,
6403 set_bit(STRIPE_EXPANDING, &sh->state);
6404 atomic_inc(&conf->reshape_stripes);
6405 /* If any of this stripe is beyond the end of the old
6406 * array, then we need to zero those blocks
6408 for (j=sh->disks; j--;) {
6410 if (j == sh->pd_idx)
6412 if (conf->level == 6 &&
6415 s = raid5_compute_blocknr(sh, j, 0);
6416 if (s < raid5_size(mddev, 0, 0)) {
6420 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
6421 set_bit(R5_Expanded, &sh->dev[j].flags);
6422 set_bit(R5_UPTODATE, &sh->dev[j].flags);
6424 if (!skipped_disk) {
6425 set_bit(STRIPE_EXPAND_READY, &sh->state);
6426 set_bit(STRIPE_HANDLE, &sh->state);
6428 list_add(&sh->lru, &stripes);
6430 spin_lock_irq(&conf->device_lock);
6431 if (mddev->reshape_backwards)
6432 conf->reshape_progress -= reshape_sectors * new_data_disks;
6434 conf->reshape_progress += reshape_sectors * new_data_disks;
6435 spin_unlock_irq(&conf->device_lock);
6436 /* Ok, those stripe are ready. We can start scheduling
6437 * reads on the source stripes.
6438 * The source stripes are determined by mapping the first and last
6439 * block on the destination stripes.
6442 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
6445 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
6446 * new_data_disks - 1),
6448 if (last_sector >= mddev->dev_sectors)
6449 last_sector = mddev->dev_sectors - 1;
6450 while (first_sector <= last_sector) {
6451 sh = raid5_get_active_stripe(conf, NULL, first_sector,
6452 R5_GAS_PREVIOUS | R5_GAS_NOQUIESCE);
6453 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
6454 set_bit(STRIPE_HANDLE, &sh->state);
6455 raid5_release_stripe(sh);
6456 first_sector += RAID5_STRIPE_SECTORS(conf);
6458 /* Now that the sources are clearly marked, we can release
6459 * the destination stripes
6461 while (!list_empty(&stripes)) {
6462 sh = list_entry(stripes.next, struct stripe_head, lru);
6463 list_del_init(&sh->lru);
6464 raid5_release_stripe(sh);
6466 /* If this takes us to the resync_max point where we have to pause,
6467 * then we need to write out the superblock.
6469 sector_nr += reshape_sectors;
6470 retn = reshape_sectors;
6472 if (mddev->curr_resync_completed > mddev->resync_max ||
6473 (sector_nr - mddev->curr_resync_completed) * 2
6474 >= mddev->resync_max - mddev->curr_resync_completed) {
6475 /* Cannot proceed until we've updated the superblock... */
6476 wait_event(conf->wait_for_overlap,
6477 atomic_read(&conf->reshape_stripes) == 0
6478 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6479 if (atomic_read(&conf->reshape_stripes) != 0)
6481 mddev->reshape_position = conf->reshape_progress;
6482 mddev->curr_resync_completed = sector_nr;
6483 if (!mddev->reshape_backwards)
6484 /* Can update recovery_offset */
6485 rdev_for_each(rdev, mddev)
6486 if (rdev->raid_disk >= 0 &&
6487 !test_bit(Journal, &rdev->flags) &&
6488 !test_bit(In_sync, &rdev->flags) &&
6489 rdev->recovery_offset < sector_nr)
6490 rdev->recovery_offset = sector_nr;
6491 conf->reshape_checkpoint = jiffies;
6492 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6493 md_wakeup_thread(mddev->thread);
6494 wait_event(mddev->sb_wait,
6495 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
6496 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6497 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6499 spin_lock_irq(&conf->device_lock);
6500 conf->reshape_safe = mddev->reshape_position;
6501 spin_unlock_irq(&conf->device_lock);
6502 wake_up(&conf->wait_for_overlap);
6503 sysfs_notify_dirent_safe(mddev->sysfs_completed);
6509 static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6512 struct r5conf *conf = mddev->private;
6513 struct stripe_head *sh;
6514 sector_t max_sector = mddev->dev_sectors;
6515 sector_t sync_blocks;
6516 int still_degraded = 0;
6519 if (sector_nr >= max_sector) {
6520 /* just being told to finish up .. nothing much to do */
6522 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6527 if (mddev->curr_resync < max_sector) /* aborted */
6528 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6530 else /* completed sync */
6532 md_bitmap_close_sync(mddev->bitmap);
6537 /* Allow raid5_quiesce to complete */
6538 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6540 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6541 return reshape_request(mddev, sector_nr, skipped);
6543 /* No need to check resync_max as we never do more than one
6544 * stripe, and as resync_max will always be on a chunk boundary,
6545 * if the check in md_do_sync didn't fire, there is no chance
6546 * of overstepping resync_max here
6549 /* if there is too many failed drives and we are trying
6550 * to resync, then assert that we are finished, because there is
6551 * nothing we can do.
6553 if (mddev->degraded >= conf->max_degraded &&
6554 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6555 sector_t rv = mddev->dev_sectors - sector_nr;
6559 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6561 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
6562 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
6563 /* we can skip this block, and probably more */
6564 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf));
6566 /* keep things rounded to whole stripes */
6567 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
6570 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
6572 sh = raid5_get_active_stripe(conf, NULL, sector_nr,
6575 sh = raid5_get_active_stripe(conf, NULL, sector_nr, 0);
6576 /* make sure we don't swamp the stripe cache if someone else
6577 * is trying to get access
6579 schedule_timeout_uninterruptible(1);
6581 /* Need to check if array will still be degraded after recovery/resync
6582 * Note in case of > 1 drive failures it's possible we're rebuilding
6583 * one drive while leaving another faulty drive in array.
6586 for (i = 0; i < conf->raid_disks; i++) {
6587 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
6589 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
6594 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
6596 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
6597 set_bit(STRIPE_HANDLE, &sh->state);
6599 raid5_release_stripe(sh);
6601 return RAID5_STRIPE_SECTORS(conf);
6604 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6605 unsigned int offset)
6607 /* We may not be able to submit a whole bio at once as there
6608 * may not be enough stripe_heads available.
6609 * We cannot pre-allocate enough stripe_heads as we may need
6610 * more than exist in the cache (if we allow ever large chunks).
6611 * So we do one stripe head at a time and record in
6612 * ->bi_hw_segments how many have been done.
6614 * We *know* that this entire raid_bio is in one chunk, so
6615 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6617 struct stripe_head *sh;
6619 sector_t sector, logical_sector, last_sector;
6623 logical_sector = raid_bio->bi_iter.bi_sector &
6624 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
6625 sector = raid5_compute_sector(conf, logical_sector,
6627 last_sector = bio_end_sector(raid_bio);
6629 for (; logical_sector < last_sector;
6630 logical_sector += RAID5_STRIPE_SECTORS(conf),
6631 sector += RAID5_STRIPE_SECTORS(conf),
6635 /* already done this stripe */
6638 sh = raid5_get_active_stripe(conf, NULL, sector,
6639 R5_GAS_NOBLOCK | R5_GAS_NOQUIESCE);
6641 /* failed to get a stripe - must wait */
6642 conf->retry_read_aligned = raid_bio;
6643 conf->retry_read_offset = scnt;
6647 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
6648 raid5_release_stripe(sh);
6649 conf->retry_read_aligned = raid_bio;
6650 conf->retry_read_offset = scnt;
6654 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
6656 raid5_release_stripe(sh);
6660 bio_endio(raid_bio);
6662 if (atomic_dec_and_test(&conf->active_aligned_reads))
6663 wake_up(&conf->wait_for_quiescent);
6667 static int handle_active_stripes(struct r5conf *conf, int group,
6668 struct r5worker *worker,
6669 struct list_head *temp_inactive_list)
6670 __must_hold(&conf->device_lock)
6672 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
6673 int i, batch_size = 0, hash;
6674 bool release_inactive = false;
6676 while (batch_size < MAX_STRIPE_BATCH &&
6677 (sh = __get_priority_stripe(conf, group)) != NULL)
6678 batch[batch_size++] = sh;
6680 if (batch_size == 0) {
6681 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6682 if (!list_empty(temp_inactive_list + i))
6684 if (i == NR_STRIPE_HASH_LOCKS) {
6685 spin_unlock_irq(&conf->device_lock);
6686 log_flush_stripe_to_raid(conf);
6687 spin_lock_irq(&conf->device_lock);
6690 release_inactive = true;
6692 spin_unlock_irq(&conf->device_lock);
6694 release_inactive_stripe_list(conf, temp_inactive_list,
6695 NR_STRIPE_HASH_LOCKS);
6697 r5l_flush_stripe_to_raid(conf->log);
6698 if (release_inactive) {
6699 spin_lock_irq(&conf->device_lock);
6703 for (i = 0; i < batch_size; i++)
6704 handle_stripe(batch[i]);
6705 log_write_stripe_run(conf);
6709 spin_lock_irq(&conf->device_lock);
6710 for (i = 0; i < batch_size; i++) {
6711 hash = batch[i]->hash_lock_index;
6712 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6717 static void raid5_do_work(struct work_struct *work)
6719 struct r5worker *worker = container_of(work, struct r5worker, work);
6720 struct r5worker_group *group = worker->group;
6721 struct r5conf *conf = group->conf;
6722 struct mddev *mddev = conf->mddev;
6723 int group_id = group - conf->worker_groups;
6725 struct blk_plug plug;
6727 pr_debug("+++ raid5worker active\n");
6729 blk_start_plug(&plug);
6731 spin_lock_irq(&conf->device_lock);
6733 int batch_size, released;
6735 released = release_stripe_list(conf, worker->temp_inactive_list);
6737 batch_size = handle_active_stripes(conf, group_id, worker,
6738 worker->temp_inactive_list);
6739 worker->working = false;
6740 if (!batch_size && !released)
6742 handled += batch_size;
6743 wait_event_lock_irq(mddev->sb_wait,
6744 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6747 pr_debug("%d stripes handled\n", handled);
6749 spin_unlock_irq(&conf->device_lock);
6751 flush_deferred_bios(conf);
6753 r5l_flush_stripe_to_raid(conf->log);
6755 async_tx_issue_pending_all();
6756 blk_finish_plug(&plug);
6758 pr_debug("--- raid5worker inactive\n");
6762 * This is our raid5 kernel thread.
6764 * We scan the hash table for stripes which can be handled now.
6765 * During the scan, completed stripes are saved for us by the interrupt
6766 * handler, so that they will not have to wait for our next wakeup.
6768 static void raid5d(struct md_thread *thread)
6770 struct mddev *mddev = thread->mddev;
6771 struct r5conf *conf = mddev->private;
6773 struct blk_plug plug;
6775 pr_debug("+++ raid5d active\n");
6777 md_check_recovery(mddev);
6779 blk_start_plug(&plug);
6781 spin_lock_irq(&conf->device_lock);
6784 int batch_size, released;
6785 unsigned int offset;
6787 released = release_stripe_list(conf, conf->temp_inactive_list);
6789 clear_bit(R5_DID_ALLOC, &conf->cache_state);
6792 !list_empty(&conf->bitmap_list)) {
6793 /* Now is a good time to flush some bitmap updates */
6795 spin_unlock_irq(&conf->device_lock);
6796 md_bitmap_unplug(mddev->bitmap);
6797 spin_lock_irq(&conf->device_lock);
6798 conf->seq_write = conf->seq_flush;
6799 activate_bit_delay(conf, conf->temp_inactive_list);
6801 raid5_activate_delayed(conf);
6803 while ((bio = remove_bio_from_retry(conf, &offset))) {
6805 spin_unlock_irq(&conf->device_lock);
6806 ok = retry_aligned_read(conf, bio, offset);
6807 spin_lock_irq(&conf->device_lock);
6813 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6814 conf->temp_inactive_list);
6815 if (!batch_size && !released)
6817 handled += batch_size;
6819 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
6820 spin_unlock_irq(&conf->device_lock);
6821 md_check_recovery(mddev);
6822 spin_lock_irq(&conf->device_lock);
6825 * Waiting on MD_SB_CHANGE_PENDING below may deadlock
6826 * seeing md_check_recovery() is needed to clear
6827 * the flag when using mdmon.
6832 wait_event_lock_irq(mddev->sb_wait,
6833 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6836 pr_debug("%d stripes handled\n", handled);
6838 spin_unlock_irq(&conf->device_lock);
6839 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6840 mutex_trylock(&conf->cache_size_mutex)) {
6841 grow_one_stripe(conf, __GFP_NOWARN);
6842 /* Set flag even if allocation failed. This helps
6843 * slow down allocation requests when mem is short
6845 set_bit(R5_DID_ALLOC, &conf->cache_state);
6846 mutex_unlock(&conf->cache_size_mutex);
6849 flush_deferred_bios(conf);
6851 r5l_flush_stripe_to_raid(conf->log);
6853 async_tx_issue_pending_all();
6854 blk_finish_plug(&plug);
6856 pr_debug("--- raid5d inactive\n");
6860 raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
6862 struct r5conf *conf;
6864 spin_lock(&mddev->lock);
6865 conf = mddev->private;
6867 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
6868 spin_unlock(&mddev->lock);
6873 raid5_set_cache_size(struct mddev *mddev, int size)
6876 struct r5conf *conf = mddev->private;
6878 if (size <= 16 || size > 32768)
6881 conf->min_nr_stripes = size;
6882 mutex_lock(&conf->cache_size_mutex);
6883 while (size < conf->max_nr_stripes &&
6884 drop_one_stripe(conf))
6886 mutex_unlock(&conf->cache_size_mutex);
6888 md_allow_write(mddev);
6890 mutex_lock(&conf->cache_size_mutex);
6891 while (size > conf->max_nr_stripes)
6892 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6893 conf->min_nr_stripes = conf->max_nr_stripes;
6897 mutex_unlock(&conf->cache_size_mutex);
6901 EXPORT_SYMBOL(raid5_set_cache_size);
6904 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
6906 struct r5conf *conf;
6910 if (len >= PAGE_SIZE)
6912 if (kstrtoul(page, 10, &new))
6914 err = mddev_lock(mddev);
6917 conf = mddev->private;
6921 err = raid5_set_cache_size(mddev, new);
6922 mddev_unlock(mddev);
6927 static struct md_sysfs_entry
6928 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6929 raid5_show_stripe_cache_size,
6930 raid5_store_stripe_cache_size);
6933 raid5_show_rmw_level(struct mddev *mddev, char *page)
6935 struct r5conf *conf = mddev->private;
6937 return sprintf(page, "%d\n", conf->rmw_level);
6943 raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6945 struct r5conf *conf = mddev->private;
6951 if (len >= PAGE_SIZE)
6954 if (kstrtoul(page, 10, &new))
6957 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6960 if (new != PARITY_DISABLE_RMW &&
6961 new != PARITY_ENABLE_RMW &&
6962 new != PARITY_PREFER_RMW)
6965 conf->rmw_level = new;
6969 static struct md_sysfs_entry
6970 raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6971 raid5_show_rmw_level,
6972 raid5_store_rmw_level);
6975 raid5_show_stripe_size(struct mddev *mddev, char *page)
6977 struct r5conf *conf;
6980 spin_lock(&mddev->lock);
6981 conf = mddev->private;
6983 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf));
6984 spin_unlock(&mddev->lock);
6988 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
6990 raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len)
6992 struct r5conf *conf;
6997 if (len >= PAGE_SIZE)
6999 if (kstrtoul(page, 10, &new))
7003 * The value should not be bigger than PAGE_SIZE. It requires to
7004 * be multiple of DEFAULT_STRIPE_SIZE and the value should be power
7007 if (new % DEFAULT_STRIPE_SIZE != 0 ||
7008 new > PAGE_SIZE || new == 0 ||
7009 new != roundup_pow_of_two(new))
7012 err = mddev_suspend_and_lock(mddev);
7016 conf = mddev->private;
7022 if (new == conf->stripe_size)
7025 pr_debug("md/raid: change stripe_size from %lu to %lu\n",
7026 conf->stripe_size, new);
7028 if (mddev->sync_thread ||
7029 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7030 mddev->reshape_position != MaxSector ||
7031 mddev->sysfs_active) {
7036 mutex_lock(&conf->cache_size_mutex);
7037 size = conf->max_nr_stripes;
7039 shrink_stripes(conf);
7041 conf->stripe_size = new;
7042 conf->stripe_shift = ilog2(new) - 9;
7043 conf->stripe_sectors = new >> 9;
7044 if (grow_stripes(conf, size)) {
7045 pr_warn("md/raid:%s: couldn't allocate buffers\n",
7049 mutex_unlock(&conf->cache_size_mutex);
7052 mddev_unlock_and_resume(mddev);
7056 static struct md_sysfs_entry
7057 raid5_stripe_size = __ATTR(stripe_size, 0644,
7058 raid5_show_stripe_size,
7059 raid5_store_stripe_size);
7061 static struct md_sysfs_entry
7062 raid5_stripe_size = __ATTR(stripe_size, 0444,
7063 raid5_show_stripe_size,
7068 raid5_show_preread_threshold(struct mddev *mddev, char *page)
7070 struct r5conf *conf;
7072 spin_lock(&mddev->lock);
7073 conf = mddev->private;
7075 ret = sprintf(page, "%d\n", conf->bypass_threshold);
7076 spin_unlock(&mddev->lock);
7081 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
7083 struct r5conf *conf;
7087 if (len >= PAGE_SIZE)
7089 if (kstrtoul(page, 10, &new))
7092 err = mddev_lock(mddev);
7095 conf = mddev->private;
7098 else if (new > conf->min_nr_stripes)
7101 conf->bypass_threshold = new;
7102 mddev_unlock(mddev);
7106 static struct md_sysfs_entry
7107 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
7109 raid5_show_preread_threshold,
7110 raid5_store_preread_threshold);
7113 raid5_show_skip_copy(struct mddev *mddev, char *page)
7115 struct r5conf *conf;
7117 spin_lock(&mddev->lock);
7118 conf = mddev->private;
7120 ret = sprintf(page, "%d\n", conf->skip_copy);
7121 spin_unlock(&mddev->lock);
7126 raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
7128 struct r5conf *conf;
7132 if (len >= PAGE_SIZE)
7134 if (kstrtoul(page, 10, &new))
7138 err = mddev_suspend_and_lock(mddev);
7141 conf = mddev->private;
7144 else if (new != conf->skip_copy) {
7145 struct request_queue *q = mddev->queue;
7147 conf->skip_copy = new;
7149 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
7151 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
7153 mddev_unlock_and_resume(mddev);
7157 static struct md_sysfs_entry
7158 raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
7159 raid5_show_skip_copy,
7160 raid5_store_skip_copy);
7163 stripe_cache_active_show(struct mddev *mddev, char *page)
7165 struct r5conf *conf = mddev->private;
7167 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
7172 static struct md_sysfs_entry
7173 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
7176 raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
7178 struct r5conf *conf;
7180 spin_lock(&mddev->lock);
7181 conf = mddev->private;
7183 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
7184 spin_unlock(&mddev->lock);
7188 static int alloc_thread_groups(struct r5conf *conf, int cnt,
7190 struct r5worker_group **worker_groups);
7192 raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
7194 struct r5conf *conf;
7197 struct r5worker_group *new_groups, *old_groups;
7200 if (len >= PAGE_SIZE)
7202 if (kstrtouint(page, 10, &new))
7204 /* 8192 should be big enough */
7208 err = mddev_suspend_and_lock(mddev);
7211 conf = mddev->private;
7214 else if (new != conf->worker_cnt_per_group) {
7215 old_groups = conf->worker_groups;
7217 flush_workqueue(raid5_wq);
7219 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
7221 spin_lock_irq(&conf->device_lock);
7222 conf->group_cnt = group_cnt;
7223 conf->worker_cnt_per_group = new;
7224 conf->worker_groups = new_groups;
7225 spin_unlock_irq(&conf->device_lock);
7228 kfree(old_groups[0].workers);
7232 mddev_unlock_and_resume(mddev);
7237 static struct md_sysfs_entry
7238 raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
7239 raid5_show_group_thread_cnt,
7240 raid5_store_group_thread_cnt);
7242 static struct attribute *raid5_attrs[] = {
7243 &raid5_stripecache_size.attr,
7244 &raid5_stripecache_active.attr,
7245 &raid5_preread_bypass_threshold.attr,
7246 &raid5_group_thread_cnt.attr,
7247 &raid5_skip_copy.attr,
7248 &raid5_rmw_level.attr,
7249 &raid5_stripe_size.attr,
7250 &r5c_journal_mode.attr,
7251 &ppl_write_hint.attr,
7254 static const struct attribute_group raid5_attrs_group = {
7256 .attrs = raid5_attrs,
7259 static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
7260 struct r5worker_group **worker_groups)
7264 struct r5worker *workers;
7268 *worker_groups = NULL;
7271 *group_cnt = num_possible_nodes();
7272 size = sizeof(struct r5worker) * cnt;
7273 workers = kcalloc(size, *group_cnt, GFP_NOIO);
7274 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
7276 if (!*worker_groups || !workers) {
7278 kfree(*worker_groups);
7282 for (i = 0; i < *group_cnt; i++) {
7283 struct r5worker_group *group;
7285 group = &(*worker_groups)[i];
7286 INIT_LIST_HEAD(&group->handle_list);
7287 INIT_LIST_HEAD(&group->loprio_list);
7289 group->workers = workers + i * cnt;
7291 for (j = 0; j < cnt; j++) {
7292 struct r5worker *worker = group->workers + j;
7293 worker->group = group;
7294 INIT_WORK(&worker->work, raid5_do_work);
7296 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
7297 INIT_LIST_HEAD(worker->temp_inactive_list + k);
7304 static void free_thread_groups(struct r5conf *conf)
7306 if (conf->worker_groups)
7307 kfree(conf->worker_groups[0].workers);
7308 kfree(conf->worker_groups);
7309 conf->worker_groups = NULL;
7313 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
7315 struct r5conf *conf = mddev->private;
7318 sectors = mddev->dev_sectors;
7320 /* size is defined by the smallest of previous and new size */
7321 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
7323 sectors &= ~((sector_t)conf->chunk_sectors - 1);
7324 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
7325 return sectors * (raid_disks - conf->max_degraded);
7328 static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7330 safe_put_page(percpu->spare_page);
7331 percpu->spare_page = NULL;
7332 kvfree(percpu->scribble);
7333 percpu->scribble = NULL;
7336 static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7338 if (conf->level == 6 && !percpu->spare_page) {
7339 percpu->spare_page = alloc_page(GFP_KERNEL);
7340 if (!percpu->spare_page)
7344 if (scribble_alloc(percpu,
7345 max(conf->raid_disks,
7346 conf->previous_raid_disks),
7347 max(conf->chunk_sectors,
7348 conf->prev_chunk_sectors)
7349 / RAID5_STRIPE_SECTORS(conf))) {
7350 free_scratch_buffer(conf, percpu);
7354 local_lock_init(&percpu->lock);
7358 static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
7360 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
7362 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
7366 static void raid5_free_percpu(struct r5conf *conf)
7371 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
7372 free_percpu(conf->percpu);
7375 static void free_conf(struct r5conf *conf)
7381 shrinker_free(conf->shrinker);
7382 free_thread_groups(conf);
7383 shrink_stripes(conf);
7384 raid5_free_percpu(conf);
7385 for (i = 0; i < conf->pool_size; i++)
7386 if (conf->disks[i].extra_page)
7387 put_page(conf->disks[i].extra_page);
7389 bioset_exit(&conf->bio_split);
7390 kfree(conf->stripe_hashtbl);
7391 kfree(conf->pending_data);
7395 static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
7397 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
7398 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
7400 if (alloc_scratch_buffer(conf, percpu)) {
7401 pr_warn("%s: failed memory allocation for cpu%u\n",
7408 static int raid5_alloc_percpu(struct r5conf *conf)
7412 conf->percpu = alloc_percpu(struct raid5_percpu);
7416 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
7418 conf->scribble_disks = max(conf->raid_disks,
7419 conf->previous_raid_disks);
7420 conf->scribble_sectors = max(conf->chunk_sectors,
7421 conf->prev_chunk_sectors);
7426 static unsigned long raid5_cache_scan(struct shrinker *shrink,
7427 struct shrink_control *sc)
7429 struct r5conf *conf = shrink->private_data;
7430 unsigned long ret = SHRINK_STOP;
7432 if (mutex_trylock(&conf->cache_size_mutex)) {
7434 while (ret < sc->nr_to_scan &&
7435 conf->max_nr_stripes > conf->min_nr_stripes) {
7436 if (drop_one_stripe(conf) == 0) {
7442 mutex_unlock(&conf->cache_size_mutex);
7447 static unsigned long raid5_cache_count(struct shrinker *shrink,
7448 struct shrink_control *sc)
7450 struct r5conf *conf = shrink->private_data;
7452 if (conf->max_nr_stripes < conf->min_nr_stripes)
7453 /* unlikely, but not impossible */
7455 return conf->max_nr_stripes - conf->min_nr_stripes;
7458 static struct r5conf *setup_conf(struct mddev *mddev)
7460 struct r5conf *conf;
7461 int raid_disk, memory, max_disks;
7462 struct md_rdev *rdev;
7463 struct disk_info *disk;
7467 struct r5worker_group *new_group;
7470 if (mddev->new_level != 5
7471 && mddev->new_level != 4
7472 && mddev->new_level != 6) {
7473 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
7474 mdname(mddev), mddev->new_level);
7475 return ERR_PTR(-EIO);
7477 if ((mddev->new_level == 5
7478 && !algorithm_valid_raid5(mddev->new_layout)) ||
7479 (mddev->new_level == 6
7480 && !algorithm_valid_raid6(mddev->new_layout))) {
7481 pr_warn("md/raid:%s: layout %d not supported\n",
7482 mdname(mddev), mddev->new_layout);
7483 return ERR_PTR(-EIO);
7485 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
7486 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
7487 mdname(mddev), mddev->raid_disks);
7488 return ERR_PTR(-EINVAL);
7491 if (!mddev->new_chunk_sectors ||
7492 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
7493 !is_power_of_2(mddev->new_chunk_sectors)) {
7494 pr_warn("md/raid:%s: invalid chunk size %d\n",
7495 mdname(mddev), mddev->new_chunk_sectors << 9);
7496 return ERR_PTR(-EINVAL);
7499 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
7503 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
7504 conf->stripe_size = DEFAULT_STRIPE_SIZE;
7505 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
7506 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9;
7508 INIT_LIST_HEAD(&conf->free_list);
7509 INIT_LIST_HEAD(&conf->pending_list);
7510 conf->pending_data = kcalloc(PENDING_IO_MAX,
7511 sizeof(struct r5pending_data),
7513 if (!conf->pending_data)
7515 for (i = 0; i < PENDING_IO_MAX; i++)
7516 list_add(&conf->pending_data[i].sibling, &conf->free_list);
7517 /* Don't enable multi-threading by default*/
7518 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
7519 conf->group_cnt = group_cnt;
7520 conf->worker_cnt_per_group = 0;
7521 conf->worker_groups = new_group;
7524 spin_lock_init(&conf->device_lock);
7525 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock);
7526 mutex_init(&conf->cache_size_mutex);
7528 init_waitqueue_head(&conf->wait_for_quiescent);
7529 init_waitqueue_head(&conf->wait_for_stripe);
7530 init_waitqueue_head(&conf->wait_for_overlap);
7531 INIT_LIST_HEAD(&conf->handle_list);
7532 INIT_LIST_HEAD(&conf->loprio_list);
7533 INIT_LIST_HEAD(&conf->hold_list);
7534 INIT_LIST_HEAD(&conf->delayed_list);
7535 INIT_LIST_HEAD(&conf->bitmap_list);
7536 init_llist_head(&conf->released_stripes);
7537 atomic_set(&conf->active_stripes, 0);
7538 atomic_set(&conf->preread_active_stripes, 0);
7539 atomic_set(&conf->active_aligned_reads, 0);
7540 spin_lock_init(&conf->pending_bios_lock);
7541 conf->batch_bio_dispatch = true;
7542 rdev_for_each(rdev, mddev) {
7543 if (test_bit(Journal, &rdev->flags))
7545 if (bdev_nonrot(rdev->bdev)) {
7546 conf->batch_bio_dispatch = false;
7551 conf->bypass_threshold = BYPASS_THRESHOLD;
7552 conf->recovery_disabled = mddev->recovery_disabled - 1;
7554 conf->raid_disks = mddev->raid_disks;
7555 if (mddev->reshape_position == MaxSector)
7556 conf->previous_raid_disks = mddev->raid_disks;
7558 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
7559 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
7561 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
7567 for (i = 0; i < max_disks; i++) {
7568 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7569 if (!conf->disks[i].extra_page)
7573 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7576 conf->mddev = mddev;
7579 conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL);
7580 if (!conf->stripe_hashtbl)
7583 /* We init hash_locks[0] separately to that it can be used
7584 * as the reference lock in the spin_lock_nest_lock() call
7585 * in lock_all_device_hash_locks_irq in order to convince
7586 * lockdep that we know what we are doing.
7588 spin_lock_init(conf->hash_locks);
7589 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7590 spin_lock_init(conf->hash_locks + i);
7592 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7593 INIT_LIST_HEAD(conf->inactive_list + i);
7595 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7596 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7598 atomic_set(&conf->r5c_cached_full_stripes, 0);
7599 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7600 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7601 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
7602 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7603 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
7605 conf->level = mddev->new_level;
7606 conf->chunk_sectors = mddev->new_chunk_sectors;
7607 ret = raid5_alloc_percpu(conf);
7611 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
7614 rdev_for_each(rdev, mddev) {
7615 raid_disk = rdev->raid_disk;
7616 if (raid_disk >= max_disks
7617 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
7619 disk = conf->disks + raid_disk;
7621 if (test_bit(Replacement, &rdev->flags)) {
7622 if (disk->replacement)
7624 RCU_INIT_POINTER(disk->replacement, rdev);
7628 RCU_INIT_POINTER(disk->rdev, rdev);
7631 if (test_bit(In_sync, &rdev->flags)) {
7632 pr_info("md/raid:%s: device %pg operational as raid disk %d\n",
7633 mdname(mddev), rdev->bdev, raid_disk);
7634 } else if (rdev->saved_raid_disk != raid_disk)
7635 /* Cannot rely on bitmap to complete recovery */
7639 conf->level = mddev->new_level;
7640 if (conf->level == 6) {
7641 conf->max_degraded = 2;
7642 if (raid6_call.xor_syndrome)
7643 conf->rmw_level = PARITY_ENABLE_RMW;
7645 conf->rmw_level = PARITY_DISABLE_RMW;
7647 conf->max_degraded = 1;
7648 conf->rmw_level = PARITY_ENABLE_RMW;
7650 conf->algorithm = mddev->new_layout;
7651 conf->reshape_progress = mddev->reshape_position;
7652 if (conf->reshape_progress != MaxSector) {
7653 conf->prev_chunk_sectors = mddev->chunk_sectors;
7654 conf->prev_algo = mddev->layout;
7656 conf->prev_chunk_sectors = conf->chunk_sectors;
7657 conf->prev_algo = conf->algorithm;
7660 conf->min_nr_stripes = NR_STRIPES;
7661 if (mddev->reshape_position != MaxSector) {
7662 int stripes = max_t(int,
7663 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7664 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
7665 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7666 if (conf->min_nr_stripes != NR_STRIPES)
7667 pr_info("md/raid:%s: force stripe size %d for reshape\n",
7668 mdname(mddev), conf->min_nr_stripes);
7670 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
7671 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
7672 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
7673 if (grow_stripes(conf, conf->min_nr_stripes)) {
7674 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7675 mdname(mddev), memory);
7679 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
7681 * Losing a stripe head costs more than the time to refill it,
7682 * it reduces the queue depth and so can hurt throughput.
7683 * So set it rather large, scaled by number of devices.
7685 conf->shrinker = shrinker_alloc(0, "md-raid5:%s", mdname(mddev));
7686 if (!conf->shrinker) {
7688 pr_warn("md/raid:%s: couldn't allocate shrinker.\n",
7693 conf->shrinker->seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7694 conf->shrinker->scan_objects = raid5_cache_scan;
7695 conf->shrinker->count_objects = raid5_cache_count;
7696 conf->shrinker->batch = 128;
7697 conf->shrinker->private_data = conf;
7699 shrinker_register(conf->shrinker);
7701 sprintf(pers_name, "raid%d", mddev->new_level);
7702 rcu_assign_pointer(conf->thread,
7703 md_register_thread(raid5d, mddev, pers_name));
7704 if (!conf->thread) {
7705 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7716 return ERR_PTR(ret);
7719 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7722 case ALGORITHM_PARITY_0:
7723 if (raid_disk < max_degraded)
7726 case ALGORITHM_PARITY_N:
7727 if (raid_disk >= raid_disks - max_degraded)
7730 case ALGORITHM_PARITY_0_6:
7731 if (raid_disk == 0 ||
7732 raid_disk == raid_disks - 1)
7735 case ALGORITHM_LEFT_ASYMMETRIC_6:
7736 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7737 case ALGORITHM_LEFT_SYMMETRIC_6:
7738 case ALGORITHM_RIGHT_SYMMETRIC_6:
7739 if (raid_disk == raid_disks - 1)
7745 static void raid5_set_io_opt(struct r5conf *conf)
7747 blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) *
7748 (conf->raid_disks - conf->max_degraded));
7751 static int raid5_run(struct mddev *mddev)
7753 struct r5conf *conf;
7754 int dirty_parity_disks = 0;
7755 struct md_rdev *rdev;
7756 struct md_rdev *journal_dev = NULL;
7757 sector_t reshape_offset = 0;
7759 long long min_offset_diff = 0;
7762 if (mddev->recovery_cp != MaxSector)
7763 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7766 rdev_for_each(rdev, mddev) {
7769 if (test_bit(Journal, &rdev->flags)) {
7773 if (rdev->raid_disk < 0)
7775 diff = (rdev->new_data_offset - rdev->data_offset);
7777 min_offset_diff = diff;
7779 } else if (mddev->reshape_backwards &&
7780 diff < min_offset_diff)
7781 min_offset_diff = diff;
7782 else if (!mddev->reshape_backwards &&
7783 diff > min_offset_diff)
7784 min_offset_diff = diff;
7787 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7788 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7789 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7794 if (mddev->reshape_position != MaxSector) {
7795 /* Check that we can continue the reshape.
7796 * Difficulties arise if the stripe we would write to
7797 * next is at or after the stripe we would read from next.
7798 * For a reshape that changes the number of devices, this
7799 * is only possible for a very short time, and mdadm makes
7800 * sure that time appears to have past before assembling
7801 * the array. So we fail if that time hasn't passed.
7802 * For a reshape that keeps the number of devices the same
7803 * mdadm must be monitoring the reshape can keeping the
7804 * critical areas read-only and backed up. It will start
7805 * the array in read-only mode, so we check for that.
7807 sector_t here_new, here_old;
7809 int max_degraded = (mddev->level == 6 ? 2 : 1);
7814 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7819 if (mddev->new_level != mddev->level) {
7820 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7824 old_disks = mddev->raid_disks - mddev->delta_disks;
7825 /* reshape_position must be on a new-stripe boundary, and one
7826 * further up in new geometry must map after here in old
7828 * If the chunk sizes are different, then as we perform reshape
7829 * in units of the largest of the two, reshape_position needs
7830 * be a multiple of the largest chunk size times new data disks.
7832 here_new = mddev->reshape_position;
7833 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7834 new_data_disks = mddev->raid_disks - max_degraded;
7835 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
7836 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7840 reshape_offset = here_new * chunk_sectors;
7841 /* here_new is the stripe we will write to */
7842 here_old = mddev->reshape_position;
7843 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
7844 /* here_old is the first stripe that we might need to read
7846 if (mddev->delta_disks == 0) {
7847 /* We cannot be sure it is safe to start an in-place
7848 * reshape. It is only safe if user-space is monitoring
7849 * and taking constant backups.
7850 * mdadm always starts a situation like this in
7851 * readonly mode so it can take control before
7852 * allowing any writes. So just check for that.
7854 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7855 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7856 /* not really in-place - so OK */;
7857 else if (mddev->ro == 0) {
7858 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7862 } else if (mddev->reshape_backwards
7863 ? (here_new * chunk_sectors + min_offset_diff <=
7864 here_old * chunk_sectors)
7865 : (here_new * chunk_sectors >=
7866 here_old * chunk_sectors + (-min_offset_diff))) {
7867 /* Reading from the same stripe as writing to - bad */
7868 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7872 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
7873 /* OK, we should be able to continue; */
7875 BUG_ON(mddev->level != mddev->new_level);
7876 BUG_ON(mddev->layout != mddev->new_layout);
7877 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
7878 BUG_ON(mddev->delta_disks != 0);
7881 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7882 test_bit(MD_HAS_PPL, &mddev->flags)) {
7883 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7885 clear_bit(MD_HAS_PPL, &mddev->flags);
7886 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
7889 if (mddev->private == NULL)
7890 conf = setup_conf(mddev);
7892 conf = mddev->private;
7895 return PTR_ERR(conf);
7897 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7899 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7902 set_disk_ro(mddev->gendisk, 1);
7903 } else if (mddev->recovery_cp == MaxSector)
7904 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
7907 conf->min_offset_diff = min_offset_diff;
7908 rcu_assign_pointer(mddev->thread, conf->thread);
7909 rcu_assign_pointer(conf->thread, NULL);
7910 mddev->private = conf;
7912 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7914 rdev = rdev_mdlock_deref(mddev, conf->disks[i].rdev);
7915 if (!rdev && conf->disks[i].replacement) {
7916 /* The replacement is all we have yet */
7917 rdev = rdev_mdlock_deref(mddev,
7918 conf->disks[i].replacement);
7919 conf->disks[i].replacement = NULL;
7920 clear_bit(Replacement, &rdev->flags);
7921 rcu_assign_pointer(conf->disks[i].rdev, rdev);
7925 if (rcu_access_pointer(conf->disks[i].replacement) &&
7926 conf->reshape_progress != MaxSector) {
7927 /* replacements and reshape simply do not mix. */
7928 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
7931 if (test_bit(In_sync, &rdev->flags))
7933 /* This disc is not fully in-sync. However if it
7934 * just stored parity (beyond the recovery_offset),
7935 * when we don't need to be concerned about the
7936 * array being dirty.
7937 * When reshape goes 'backwards', we never have
7938 * partially completed devices, so we only need
7939 * to worry about reshape going forwards.
7941 /* Hack because v0.91 doesn't store recovery_offset properly. */
7942 if (mddev->major_version == 0 &&
7943 mddev->minor_version > 90)
7944 rdev->recovery_offset = reshape_offset;
7946 if (rdev->recovery_offset < reshape_offset) {
7947 /* We need to check old and new layout */
7948 if (!only_parity(rdev->raid_disk,
7951 conf->max_degraded))
7954 if (!only_parity(rdev->raid_disk,
7956 conf->previous_raid_disks,
7957 conf->max_degraded))
7959 dirty_parity_disks++;
7963 * 0 for a fully functional array, 1 or 2 for a degraded array.
7965 mddev->degraded = raid5_calc_degraded(conf);
7967 if (has_failed(conf)) {
7968 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
7969 mdname(mddev), mddev->degraded, conf->raid_disks);
7973 /* device size must be a multiple of chunk size */
7974 mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1);
7975 mddev->resync_max_sectors = mddev->dev_sectors;
7977 if (mddev->degraded > dirty_parity_disks &&
7978 mddev->recovery_cp != MaxSector) {
7979 if (test_bit(MD_HAS_PPL, &mddev->flags))
7980 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7982 else if (mddev->ok_start_degraded)
7983 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7986 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7992 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7993 mdname(mddev), conf->level,
7994 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7997 print_raid5_conf(conf);
7999 if (conf->reshape_progress != MaxSector) {
8000 conf->reshape_safe = conf->reshape_progress;
8001 atomic_set(&conf->reshape_stripes, 0);
8002 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8003 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8004 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8005 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8008 /* Ok, everything is just fine now */
8009 if (mddev->to_remove == &raid5_attrs_group)
8010 mddev->to_remove = NULL;
8011 else if (mddev->kobj.sd &&
8012 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
8013 pr_warn("raid5: failed to create sysfs attributes for %s\n",
8015 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
8019 /* read-ahead size must cover two whole stripes, which
8020 * is 2 * (datadisks) * chunksize where 'n' is the
8021 * number of raid devices
8023 int data_disks = conf->previous_raid_disks - conf->max_degraded;
8024 int stripe = data_disks *
8025 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
8027 chunk_size = mddev->chunk_sectors << 9;
8028 blk_queue_io_min(mddev->queue, chunk_size);
8029 raid5_set_io_opt(conf);
8030 mddev->queue->limits.raid_partial_stripes_expensive = 1;
8032 * We can only discard a whole stripe. It doesn't make sense to
8033 * discard data disk but write parity disk
8035 stripe = stripe * PAGE_SIZE;
8036 stripe = roundup_pow_of_two(stripe);
8037 mddev->queue->limits.discard_granularity = stripe;
8039 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
8041 rdev_for_each(rdev, mddev) {
8042 disk_stack_limits(mddev->gendisk, rdev->bdev,
8043 rdev->data_offset << 9);
8044 disk_stack_limits(mddev->gendisk, rdev->bdev,
8045 rdev->new_data_offset << 9);
8049 * zeroing is required, otherwise data
8050 * could be lost. Consider a scenario: discard a stripe
8051 * (the stripe could be inconsistent if
8052 * discard_zeroes_data is 0); write one disk of the
8053 * stripe (the stripe could be inconsistent again
8054 * depending on which disks are used to calculate
8055 * parity); the disk is broken; The stripe data of this
8058 * We only allow DISCARD if the sysadmin has confirmed that
8059 * only safe devices are in use by setting a module parameter.
8060 * A better idea might be to turn DISCARD into WRITE_ZEROES
8061 * requests, as that is required to be safe.
8063 if (!devices_handle_discard_safely ||
8064 mddev->queue->limits.max_discard_sectors < (stripe >> 9) ||
8065 mddev->queue->limits.discard_granularity < stripe)
8066 blk_queue_max_discard_sectors(mddev->queue, 0);
8069 * Requests require having a bitmap for each stripe.
8070 * Limit the max sectors based on this.
8072 blk_queue_max_hw_sectors(mddev->queue,
8073 RAID5_MAX_REQ_STRIPES << RAID5_STRIPE_SHIFT(conf));
8075 /* No restrictions on the number of segments in the request */
8076 blk_queue_max_segments(mddev->queue, USHRT_MAX);
8079 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
8084 md_unregister_thread(mddev, &mddev->thread);
8085 print_raid5_conf(conf);
8087 mddev->private = NULL;
8088 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
8092 static void raid5_free(struct mddev *mddev, void *priv)
8094 struct r5conf *conf = priv;
8097 mddev->to_remove = &raid5_attrs_group;
8100 static void raid5_status(struct seq_file *seq, struct mddev *mddev)
8102 struct r5conf *conf = mddev->private;
8105 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
8106 conf->chunk_sectors / 2, mddev->layout);
8107 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
8109 for (i = 0; i < conf->raid_disks; i++) {
8110 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
8111 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
8114 seq_printf (seq, "]");
8117 static void print_raid5_conf (struct r5conf *conf)
8119 struct md_rdev *rdev;
8122 pr_debug("RAID conf printout:\n");
8124 pr_debug("(conf==NULL)\n");
8127 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
8129 conf->raid_disks - conf->mddev->degraded);
8132 for (i = 0; i < conf->raid_disks; i++) {
8133 rdev = rcu_dereference(conf->disks[i].rdev);
8135 pr_debug(" disk %d, o:%d, dev:%pg\n",
8136 i, !test_bit(Faulty, &rdev->flags),
8142 static int raid5_spare_active(struct mddev *mddev)
8145 struct r5conf *conf = mddev->private;
8146 struct md_rdev *rdev, *replacement;
8148 unsigned long flags;
8150 for (i = 0; i < conf->raid_disks; i++) {
8151 rdev = rdev_mdlock_deref(mddev, conf->disks[i].rdev);
8152 replacement = rdev_mdlock_deref(mddev,
8153 conf->disks[i].replacement);
8155 && replacement->recovery_offset == MaxSector
8156 && !test_bit(Faulty, &replacement->flags)
8157 && !test_and_set_bit(In_sync, &replacement->flags)) {
8158 /* Replacement has just become active. */
8160 || !test_and_clear_bit(In_sync, &rdev->flags))
8163 /* Replaced device not technically faulty,
8164 * but we need to be sure it gets removed
8165 * and never re-added.
8167 set_bit(Faulty, &rdev->flags);
8168 sysfs_notify_dirent_safe(
8171 sysfs_notify_dirent_safe(replacement->sysfs_state);
8173 && rdev->recovery_offset == MaxSector
8174 && !test_bit(Faulty, &rdev->flags)
8175 && !test_and_set_bit(In_sync, &rdev->flags)) {
8177 sysfs_notify_dirent_safe(rdev->sysfs_state);
8180 spin_lock_irqsave(&conf->device_lock, flags);
8181 mddev->degraded = raid5_calc_degraded(conf);
8182 spin_unlock_irqrestore(&conf->device_lock, flags);
8183 print_raid5_conf(conf);
8187 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
8189 struct r5conf *conf = mddev->private;
8191 int number = rdev->raid_disk;
8192 struct md_rdev __rcu **rdevp;
8193 struct disk_info *p;
8194 struct md_rdev *tmp;
8196 print_raid5_conf(conf);
8197 if (test_bit(Journal, &rdev->flags) && conf->log) {
8199 * we can't wait pending write here, as this is called in
8200 * raid5d, wait will deadlock.
8201 * neilb: there is no locking about new writes here,
8202 * so this cannot be safe.
8204 if (atomic_read(&conf->active_stripes) ||
8205 atomic_read(&conf->r5c_cached_full_stripes) ||
8206 atomic_read(&conf->r5c_cached_partial_stripes)) {
8212 if (unlikely(number >= conf->pool_size))
8214 p = conf->disks + number;
8215 if (rdev == rcu_access_pointer(p->rdev))
8217 else if (rdev == rcu_access_pointer(p->replacement))
8218 rdevp = &p->replacement;
8222 if (number >= conf->raid_disks &&
8223 conf->reshape_progress == MaxSector)
8224 clear_bit(In_sync, &rdev->flags);
8226 if (test_bit(In_sync, &rdev->flags) ||
8227 atomic_read(&rdev->nr_pending)) {
8231 /* Only remove non-faulty devices if recovery
8234 if (!test_bit(Faulty, &rdev->flags) &&
8235 mddev->recovery_disabled != conf->recovery_disabled &&
8236 !has_failed(conf) &&
8237 (!rcu_access_pointer(p->replacement) ||
8238 rcu_access_pointer(p->replacement) == rdev) &&
8239 number < conf->raid_disks) {
8244 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
8245 lockdep_assert_held(&mddev->reconfig_mutex);
8247 if (atomic_read(&rdev->nr_pending)) {
8248 /* lost the race, try later */
8250 rcu_assign_pointer(*rdevp, rdev);
8254 err = log_modify(conf, rdev, false);
8259 tmp = rcu_access_pointer(p->replacement);
8261 /* We must have just cleared 'rdev' */
8262 rcu_assign_pointer(p->rdev, tmp);
8263 clear_bit(Replacement, &tmp->flags);
8264 smp_mb(); /* Make sure other CPUs may see both as identical
8265 * but will never see neither - if they are careful
8267 rcu_assign_pointer(p->replacement, NULL);
8270 err = log_modify(conf, tmp, true);
8273 clear_bit(WantReplacement, &rdev->flags);
8276 print_raid5_conf(conf);
8280 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
8282 struct r5conf *conf = mddev->private;
8283 int ret, err = -EEXIST;
8285 struct disk_info *p;
8286 struct md_rdev *tmp;
8288 int last = conf->raid_disks - 1;
8290 if (test_bit(Journal, &rdev->flags)) {
8294 rdev->raid_disk = 0;
8296 * The array is in readonly mode if journal is missing, so no
8297 * write requests running. We should be safe
8299 ret = log_init(conf, rdev, false);
8303 ret = r5l_start(conf->log);
8309 if (mddev->recovery_disabled == conf->recovery_disabled)
8312 if (rdev->saved_raid_disk < 0 && has_failed(conf))
8313 /* no point adding a device */
8316 if (rdev->raid_disk >= 0)
8317 first = last = rdev->raid_disk;
8320 * find the disk ... but prefer rdev->saved_raid_disk
8323 if (rdev->saved_raid_disk >= first &&
8324 rdev->saved_raid_disk <= last &&
8325 conf->disks[rdev->saved_raid_disk].rdev == NULL)
8326 first = rdev->saved_raid_disk;
8328 for (disk = first; disk <= last; disk++) {
8329 p = conf->disks + disk;
8330 if (p->rdev == NULL) {
8331 clear_bit(In_sync, &rdev->flags);
8332 rdev->raid_disk = disk;
8333 if (rdev->saved_raid_disk != disk)
8335 rcu_assign_pointer(p->rdev, rdev);
8337 err = log_modify(conf, rdev, true);
8342 for (disk = first; disk <= last; disk++) {
8343 p = conf->disks + disk;
8344 tmp = rdev_mdlock_deref(mddev, p->rdev);
8345 if (test_bit(WantReplacement, &tmp->flags) &&
8346 mddev->reshape_position == MaxSector &&
8347 p->replacement == NULL) {
8348 clear_bit(In_sync, &rdev->flags);
8349 set_bit(Replacement, &rdev->flags);
8350 rdev->raid_disk = disk;
8353 rcu_assign_pointer(p->replacement, rdev);
8358 print_raid5_conf(conf);
8362 static int raid5_resize(struct mddev *mddev, sector_t sectors)
8364 /* no resync is happening, and there is enough space
8365 * on all devices, so we can resize.
8366 * We need to make sure resync covers any new space.
8367 * If the array is shrinking we should possibly wait until
8368 * any io in the removed space completes, but it hardly seems
8372 struct r5conf *conf = mddev->private;
8374 if (raid5_has_log(conf) || raid5_has_ppl(conf))
8376 sectors &= ~((sector_t)conf->chunk_sectors - 1);
8377 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
8378 if (mddev->external_size &&
8379 mddev->array_sectors > newsize)
8381 if (mddev->bitmap) {
8382 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
8386 md_set_array_sectors(mddev, newsize);
8387 if (sectors > mddev->dev_sectors &&
8388 mddev->recovery_cp > mddev->dev_sectors) {
8389 mddev->recovery_cp = mddev->dev_sectors;
8390 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8392 mddev->dev_sectors = sectors;
8393 mddev->resync_max_sectors = sectors;
8397 static int check_stripe_cache(struct mddev *mddev)
8399 /* Can only proceed if there are plenty of stripe_heads.
8400 * We need a minimum of one full stripe,, and for sensible progress
8401 * it is best to have about 4 times that.
8402 * If we require 4 times, then the default 256 4K stripe_heads will
8403 * allow for chunk sizes up to 256K, which is probably OK.
8404 * If the chunk size is greater, user-space should request more
8405 * stripe_heads first.
8407 struct r5conf *conf = mddev->private;
8408 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
8409 > conf->min_nr_stripes ||
8410 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
8411 > conf->min_nr_stripes) {
8412 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
8414 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
8415 / RAID5_STRIPE_SIZE(conf))*4);
8421 static int check_reshape(struct mddev *mddev)
8423 struct r5conf *conf = mddev->private;
8425 if (raid5_has_log(conf) || raid5_has_ppl(conf))
8427 if (mddev->delta_disks == 0 &&
8428 mddev->new_layout == mddev->layout &&
8429 mddev->new_chunk_sectors == mddev->chunk_sectors)
8430 return 0; /* nothing to do */
8431 if (has_failed(conf))
8433 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
8434 /* We might be able to shrink, but the devices must
8435 * be made bigger first.
8436 * For raid6, 4 is the minimum size.
8437 * Otherwise 2 is the minimum
8440 if (mddev->level == 6)
8442 if (mddev->raid_disks + mddev->delta_disks < min)
8446 if (!check_stripe_cache(mddev))
8449 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
8450 mddev->delta_disks > 0)
8451 if (resize_chunks(conf,
8452 conf->previous_raid_disks
8453 + max(0, mddev->delta_disks),
8454 max(mddev->new_chunk_sectors,
8455 mddev->chunk_sectors)
8459 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
8460 return 0; /* never bother to shrink */
8461 return resize_stripes(conf, (conf->previous_raid_disks
8462 + mddev->delta_disks));
8465 static int raid5_start_reshape(struct mddev *mddev)
8467 struct r5conf *conf = mddev->private;
8468 struct md_rdev *rdev;
8471 unsigned long flags;
8473 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8476 if (!check_stripe_cache(mddev))
8479 if (has_failed(conf))
8482 /* raid5 can't handle concurrent reshape and recovery */
8483 if (mddev->recovery_cp < MaxSector)
8485 for (i = 0; i < conf->raid_disks; i++)
8486 if (rdev_mdlock_deref(mddev, conf->disks[i].replacement))
8489 rdev_for_each(rdev, mddev) {
8490 if (!test_bit(In_sync, &rdev->flags)
8491 && !test_bit(Faulty, &rdev->flags))
8495 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
8496 /* Not enough devices even to make a degraded array
8501 /* Refuse to reduce size of the array. Any reductions in
8502 * array size must be through explicit setting of array_size
8505 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
8506 < mddev->array_sectors) {
8507 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
8512 atomic_set(&conf->reshape_stripes, 0);
8513 spin_lock_irq(&conf->device_lock);
8514 write_seqcount_begin(&conf->gen_lock);
8515 conf->previous_raid_disks = conf->raid_disks;
8516 conf->raid_disks += mddev->delta_disks;
8517 conf->prev_chunk_sectors = conf->chunk_sectors;
8518 conf->chunk_sectors = mddev->new_chunk_sectors;
8519 conf->prev_algo = conf->algorithm;
8520 conf->algorithm = mddev->new_layout;
8522 /* Code that selects data_offset needs to see the generation update
8523 * if reshape_progress has been set - so a memory barrier needed.
8526 if (mddev->reshape_backwards)
8527 conf->reshape_progress = raid5_size(mddev, 0, 0);
8529 conf->reshape_progress = 0;
8530 conf->reshape_safe = conf->reshape_progress;
8531 write_seqcount_end(&conf->gen_lock);
8532 spin_unlock_irq(&conf->device_lock);
8534 /* Now make sure any requests that proceeded on the assumption
8535 * the reshape wasn't running - like Discard or Read - have
8538 raid5_quiesce(mddev, true);
8539 raid5_quiesce(mddev, false);
8541 /* Add some new drives, as many as will fit.
8542 * We know there are enough to make the newly sized array work.
8543 * Don't add devices if we are reducing the number of
8544 * devices in the array. This is because it is not possible
8545 * to correctly record the "partially reconstructed" state of
8546 * such devices during the reshape and confusion could result.
8548 if (mddev->delta_disks >= 0) {
8549 rdev_for_each(rdev, mddev)
8550 if (rdev->raid_disk < 0 &&
8551 !test_bit(Faulty, &rdev->flags)) {
8552 if (raid5_add_disk(mddev, rdev) == 0) {
8554 >= conf->previous_raid_disks)
8555 set_bit(In_sync, &rdev->flags);
8557 rdev->recovery_offset = 0;
8559 /* Failure here is OK */
8560 sysfs_link_rdev(mddev, rdev);
8562 } else if (rdev->raid_disk >= conf->previous_raid_disks
8563 && !test_bit(Faulty, &rdev->flags)) {
8564 /* This is a spare that was manually added */
8565 set_bit(In_sync, &rdev->flags);
8568 /* When a reshape changes the number of devices,
8569 * ->degraded is measured against the larger of the
8570 * pre and post number of devices.
8572 spin_lock_irqsave(&conf->device_lock, flags);
8573 mddev->degraded = raid5_calc_degraded(conf);
8574 spin_unlock_irqrestore(&conf->device_lock, flags);
8576 mddev->raid_disks = conf->raid_disks;
8577 mddev->reshape_position = conf->reshape_progress;
8578 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8580 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8581 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8582 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8583 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8584 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8585 conf->reshape_checkpoint = jiffies;
8590 /* This is called from the reshape thread and should make any
8591 * changes needed in 'conf'
8593 static void end_reshape(struct r5conf *conf)
8596 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
8597 struct md_rdev *rdev;
8599 spin_lock_irq(&conf->device_lock);
8600 conf->previous_raid_disks = conf->raid_disks;
8601 md_finish_reshape(conf->mddev);
8603 conf->reshape_progress = MaxSector;
8604 conf->mddev->reshape_position = MaxSector;
8605 rdev_for_each(rdev, conf->mddev)
8606 if (rdev->raid_disk >= 0 &&
8607 !test_bit(Journal, &rdev->flags) &&
8608 !test_bit(In_sync, &rdev->flags))
8609 rdev->recovery_offset = MaxSector;
8610 spin_unlock_irq(&conf->device_lock);
8611 wake_up(&conf->wait_for_overlap);
8613 if (conf->mddev->queue)
8614 raid5_set_io_opt(conf);
8618 /* This is called from the raid5d thread with mddev_lock held.
8619 * It makes config changes to the device.
8621 static void raid5_finish_reshape(struct mddev *mddev)
8623 struct r5conf *conf = mddev->private;
8624 struct md_rdev *rdev;
8626 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8628 if (mddev->delta_disks <= 0) {
8630 spin_lock_irq(&conf->device_lock);
8631 mddev->degraded = raid5_calc_degraded(conf);
8632 spin_unlock_irq(&conf->device_lock);
8633 for (d = conf->raid_disks ;
8634 d < conf->raid_disks - mddev->delta_disks;
8636 rdev = rdev_mdlock_deref(mddev,
8637 conf->disks[d].rdev);
8639 clear_bit(In_sync, &rdev->flags);
8640 rdev = rdev_mdlock_deref(mddev,
8641 conf->disks[d].replacement);
8643 clear_bit(In_sync, &rdev->flags);
8646 mddev->layout = conf->algorithm;
8647 mddev->chunk_sectors = conf->chunk_sectors;
8648 mddev->reshape_position = MaxSector;
8649 mddev->delta_disks = 0;
8650 mddev->reshape_backwards = 0;
8654 static void raid5_quiesce(struct mddev *mddev, int quiesce)
8656 struct r5conf *conf = mddev->private;
8659 /* stop all writes */
8660 lock_all_device_hash_locks_irq(conf);
8661 /* '2' tells resync/reshape to pause so that all
8662 * active stripes can drain
8664 r5c_flush_cache(conf, INT_MAX);
8665 /* need a memory barrier to make sure read_one_chunk() sees
8666 * quiesce started and reverts to slow (locked) path.
8668 smp_store_release(&conf->quiesce, 2);
8669 wait_event_cmd(conf->wait_for_quiescent,
8670 atomic_read(&conf->active_stripes) == 0 &&
8671 atomic_read(&conf->active_aligned_reads) == 0,
8672 unlock_all_device_hash_locks_irq(conf),
8673 lock_all_device_hash_locks_irq(conf));
8675 unlock_all_device_hash_locks_irq(conf);
8676 /* allow reshape to continue */
8677 wake_up(&conf->wait_for_overlap);
8679 /* re-enable writes */
8680 lock_all_device_hash_locks_irq(conf);
8682 wake_up(&conf->wait_for_quiescent);
8683 wake_up(&conf->wait_for_overlap);
8684 unlock_all_device_hash_locks_irq(conf);
8686 log_quiesce(conf, quiesce);
8689 static void *raid45_takeover_raid0(struct mddev *mddev, int level)
8691 struct r0conf *raid0_conf = mddev->private;
8694 /* for raid0 takeover only one zone is supported */
8695 if (raid0_conf->nr_strip_zones > 1) {
8696 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8698 return ERR_PTR(-EINVAL);
8701 sectors = raid0_conf->strip_zone[0].zone_end;
8702 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
8703 mddev->dev_sectors = sectors;
8704 mddev->new_level = level;
8705 mddev->new_layout = ALGORITHM_PARITY_N;
8706 mddev->new_chunk_sectors = mddev->chunk_sectors;
8707 mddev->raid_disks += 1;
8708 mddev->delta_disks = 1;
8709 /* make sure it will be not marked as dirty */
8710 mddev->recovery_cp = MaxSector;
8712 return setup_conf(mddev);
8715 static void *raid5_takeover_raid1(struct mddev *mddev)
8720 if (mddev->raid_disks != 2 ||
8721 mddev->degraded > 1)
8722 return ERR_PTR(-EINVAL);
8724 /* Should check if there are write-behind devices? */
8726 chunksect = 64*2; /* 64K by default */
8728 /* The array must be an exact multiple of chunksize */
8729 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8732 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
8733 /* array size does not allow a suitable chunk size */
8734 return ERR_PTR(-EINVAL);
8736 mddev->new_level = 5;
8737 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
8738 mddev->new_chunk_sectors = chunksect;
8740 ret = setup_conf(mddev);
8742 mddev_clear_unsupported_flags(mddev,
8743 UNSUPPORTED_MDDEV_FLAGS);
8747 static void *raid5_takeover_raid6(struct mddev *mddev)
8751 switch (mddev->layout) {
8752 case ALGORITHM_LEFT_ASYMMETRIC_6:
8753 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8755 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8756 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8758 case ALGORITHM_LEFT_SYMMETRIC_6:
8759 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8761 case ALGORITHM_RIGHT_SYMMETRIC_6:
8762 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8764 case ALGORITHM_PARITY_0_6:
8765 new_layout = ALGORITHM_PARITY_0;
8767 case ALGORITHM_PARITY_N:
8768 new_layout = ALGORITHM_PARITY_N;
8771 return ERR_PTR(-EINVAL);
8773 mddev->new_level = 5;
8774 mddev->new_layout = new_layout;
8775 mddev->delta_disks = -1;
8776 mddev->raid_disks -= 1;
8777 return setup_conf(mddev);
8780 static int raid5_check_reshape(struct mddev *mddev)
8782 /* For a 2-drive array, the layout and chunk size can be changed
8783 * immediately as not restriping is needed.
8784 * For larger arrays we record the new value - after validation
8785 * to be used by a reshape pass.
8787 struct r5conf *conf = mddev->private;
8788 int new_chunk = mddev->new_chunk_sectors;
8790 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
8792 if (new_chunk > 0) {
8793 if (!is_power_of_2(new_chunk))
8795 if (new_chunk < (PAGE_SIZE>>9))
8797 if (mddev->array_sectors & (new_chunk-1))
8798 /* not factor of array size */
8802 /* They look valid */
8804 if (mddev->raid_disks == 2) {
8805 /* can make the change immediately */
8806 if (mddev->new_layout >= 0) {
8807 conf->algorithm = mddev->new_layout;
8808 mddev->layout = mddev->new_layout;
8810 if (new_chunk > 0) {
8811 conf->chunk_sectors = new_chunk ;
8812 mddev->chunk_sectors = new_chunk;
8814 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8815 md_wakeup_thread(mddev->thread);
8817 return check_reshape(mddev);
8820 static int raid6_check_reshape(struct mddev *mddev)
8822 int new_chunk = mddev->new_chunk_sectors;
8824 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
8826 if (new_chunk > 0) {
8827 if (!is_power_of_2(new_chunk))
8829 if (new_chunk < (PAGE_SIZE >> 9))
8831 if (mddev->array_sectors & (new_chunk-1))
8832 /* not factor of array size */
8836 /* They look valid */
8837 return check_reshape(mddev);
8840 static void *raid5_takeover(struct mddev *mddev)
8842 /* raid5 can take over:
8843 * raid0 - if there is only one strip zone - make it a raid4 layout
8844 * raid1 - if there are two drives. We need to know the chunk size
8845 * raid4 - trivial - just use a raid4 layout.
8846 * raid6 - Providing it is a *_6 layout
8848 if (mddev->level == 0)
8849 return raid45_takeover_raid0(mddev, 5);
8850 if (mddev->level == 1)
8851 return raid5_takeover_raid1(mddev);
8852 if (mddev->level == 4) {
8853 mddev->new_layout = ALGORITHM_PARITY_N;
8854 mddev->new_level = 5;
8855 return setup_conf(mddev);
8857 if (mddev->level == 6)
8858 return raid5_takeover_raid6(mddev);
8860 return ERR_PTR(-EINVAL);
8863 static void *raid4_takeover(struct mddev *mddev)
8865 /* raid4 can take over:
8866 * raid0 - if there is only one strip zone
8867 * raid5 - if layout is right
8869 if (mddev->level == 0)
8870 return raid45_takeover_raid0(mddev, 4);
8871 if (mddev->level == 5 &&
8872 mddev->layout == ALGORITHM_PARITY_N) {
8873 mddev->new_layout = 0;
8874 mddev->new_level = 4;
8875 return setup_conf(mddev);
8877 return ERR_PTR(-EINVAL);
8880 static struct md_personality raid5_personality;
8882 static void *raid6_takeover(struct mddev *mddev)
8884 /* Currently can only take over a raid5. We map the
8885 * personality to an equivalent raid6 personality
8886 * with the Q block at the end.
8890 if (mddev->pers != &raid5_personality)
8891 return ERR_PTR(-EINVAL);
8892 if (mddev->degraded > 1)
8893 return ERR_PTR(-EINVAL);
8894 if (mddev->raid_disks > 253)
8895 return ERR_PTR(-EINVAL);
8896 if (mddev->raid_disks < 3)
8897 return ERR_PTR(-EINVAL);
8899 switch (mddev->layout) {
8900 case ALGORITHM_LEFT_ASYMMETRIC:
8901 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8903 case ALGORITHM_RIGHT_ASYMMETRIC:
8904 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8906 case ALGORITHM_LEFT_SYMMETRIC:
8907 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8909 case ALGORITHM_RIGHT_SYMMETRIC:
8910 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8912 case ALGORITHM_PARITY_0:
8913 new_layout = ALGORITHM_PARITY_0_6;
8915 case ALGORITHM_PARITY_N:
8916 new_layout = ALGORITHM_PARITY_N;
8919 return ERR_PTR(-EINVAL);
8921 mddev->new_level = 6;
8922 mddev->new_layout = new_layout;
8923 mddev->delta_disks = 1;
8924 mddev->raid_disks += 1;
8925 return setup_conf(mddev);
8928 static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8930 struct r5conf *conf;
8933 err = mddev_suspend_and_lock(mddev);
8936 conf = mddev->private;
8938 mddev_unlock_and_resume(mddev);
8942 if (strncmp(buf, "ppl", 3) == 0) {
8943 /* ppl only works with RAID 5 */
8944 if (!raid5_has_ppl(conf) && conf->level == 5) {
8945 err = log_init(conf, NULL, true);
8947 err = resize_stripes(conf, conf->pool_size);
8953 } else if (strncmp(buf, "resync", 6) == 0) {
8954 if (raid5_has_ppl(conf)) {
8956 err = resize_stripes(conf, conf->pool_size);
8957 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8958 r5l_log_disk_error(conf)) {
8959 bool journal_dev_exists = false;
8960 struct md_rdev *rdev;
8962 rdev_for_each(rdev, mddev)
8963 if (test_bit(Journal, &rdev->flags)) {
8964 journal_dev_exists = true;
8968 if (!journal_dev_exists)
8969 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8970 else /* need remove journal device first */
8979 md_update_sb(mddev, 1);
8981 mddev_unlock_and_resume(mddev);
8986 static int raid5_start(struct mddev *mddev)
8988 struct r5conf *conf = mddev->private;
8990 return r5l_start(conf->log);
8993 static struct md_personality raid6_personality =
8997 .owner = THIS_MODULE,
8998 .make_request = raid5_make_request,
9000 .start = raid5_start,
9002 .status = raid5_status,
9003 .error_handler = raid5_error,
9004 .hot_add_disk = raid5_add_disk,
9005 .hot_remove_disk= raid5_remove_disk,
9006 .spare_active = raid5_spare_active,
9007 .sync_request = raid5_sync_request,
9008 .resize = raid5_resize,
9010 .check_reshape = raid6_check_reshape,
9011 .start_reshape = raid5_start_reshape,
9012 .finish_reshape = raid5_finish_reshape,
9013 .quiesce = raid5_quiesce,
9014 .takeover = raid6_takeover,
9015 .change_consistency_policy = raid5_change_consistency_policy,
9017 static struct md_personality raid5_personality =
9021 .owner = THIS_MODULE,
9022 .make_request = raid5_make_request,
9024 .start = raid5_start,
9026 .status = raid5_status,
9027 .error_handler = raid5_error,
9028 .hot_add_disk = raid5_add_disk,
9029 .hot_remove_disk= raid5_remove_disk,
9030 .spare_active = raid5_spare_active,
9031 .sync_request = raid5_sync_request,
9032 .resize = raid5_resize,
9034 .check_reshape = raid5_check_reshape,
9035 .start_reshape = raid5_start_reshape,
9036 .finish_reshape = raid5_finish_reshape,
9037 .quiesce = raid5_quiesce,
9038 .takeover = raid5_takeover,
9039 .change_consistency_policy = raid5_change_consistency_policy,
9042 static struct md_personality raid4_personality =
9046 .owner = THIS_MODULE,
9047 .make_request = raid5_make_request,
9049 .start = raid5_start,
9051 .status = raid5_status,
9052 .error_handler = raid5_error,
9053 .hot_add_disk = raid5_add_disk,
9054 .hot_remove_disk= raid5_remove_disk,
9055 .spare_active = raid5_spare_active,
9056 .sync_request = raid5_sync_request,
9057 .resize = raid5_resize,
9059 .check_reshape = raid5_check_reshape,
9060 .start_reshape = raid5_start_reshape,
9061 .finish_reshape = raid5_finish_reshape,
9062 .quiesce = raid5_quiesce,
9063 .takeover = raid4_takeover,
9064 .change_consistency_policy = raid5_change_consistency_policy,
9067 static int __init raid5_init(void)
9071 raid5_wq = alloc_workqueue("raid5wq",
9072 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
9076 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
9078 raid456_cpu_up_prepare,
9081 destroy_workqueue(raid5_wq);
9084 register_md_personality(&raid6_personality);
9085 register_md_personality(&raid5_personality);
9086 register_md_personality(&raid4_personality);
9090 static void raid5_exit(void)
9092 unregister_md_personality(&raid6_personality);
9093 unregister_md_personality(&raid5_personality);
9094 unregister_md_personality(&raid4_personality);
9095 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
9096 destroy_workqueue(raid5_wq);
9099 module_init(raid5_init);
9100 module_exit(raid5_exit);
9101 MODULE_LICENSE("GPL");
9102 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
9103 MODULE_ALIAS("md-personality-4"); /* RAID5 */
9104 MODULE_ALIAS("md-raid5");
9105 MODULE_ALIAS("md-raid4");
9106 MODULE_ALIAS("md-level-5");
9107 MODULE_ALIAS("md-level-4");
9108 MODULE_ALIAS("md-personality-8"); /* RAID6 */
9109 MODULE_ALIAS("md-raid6");
9110 MODULE_ALIAS("md-level-6");
9112 /* This used to be two separate modules, they were: */
9113 MODULE_ALIAS("raid5");
9114 MODULE_ALIAS("raid6");