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 static bool devices_handle_discard_safely = false;
66 module_param(devices_handle_discard_safely, bool, 0644);
67 MODULE_PARM_DESC(devices_handle_discard_safely,
68 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
69 static struct workqueue_struct *raid5_wq;
71 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
73 int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK;
74 return &conf->stripe_hashtbl[hash];
77 static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect)
79 return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK;
82 static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
84 spin_lock_irq(conf->hash_locks + hash);
85 spin_lock(&conf->device_lock);
88 static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
90 spin_unlock(&conf->device_lock);
91 spin_unlock_irq(conf->hash_locks + hash);
94 static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
97 spin_lock_irq(conf->hash_locks);
98 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
99 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
100 spin_lock(&conf->device_lock);
103 static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
106 spin_unlock(&conf->device_lock);
107 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
108 spin_unlock(conf->hash_locks + i);
109 spin_unlock_irq(conf->hash_locks);
112 /* Find first data disk in a raid6 stripe */
113 static inline int raid6_d0(struct stripe_head *sh)
116 /* ddf always start from first device */
118 /* md starts just after Q block */
119 if (sh->qd_idx == sh->disks - 1)
122 return sh->qd_idx + 1;
124 static inline int raid6_next_disk(int disk, int raid_disks)
127 return (disk < raid_disks) ? disk : 0;
130 /* When walking through the disks in a raid5, starting at raid6_d0,
131 * We need to map each disk to a 'slot', where the data disks are slot
132 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
133 * is raid_disks-1. This help does that mapping.
135 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
136 int *count, int syndrome_disks)
142 if (idx == sh->pd_idx)
143 return syndrome_disks;
144 if (idx == sh->qd_idx)
145 return syndrome_disks + 1;
151 static void print_raid5_conf (struct r5conf *conf);
153 static int stripe_operations_active(struct stripe_head *sh)
155 return sh->check_state || sh->reconstruct_state ||
156 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
157 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
160 static bool stripe_is_lowprio(struct stripe_head *sh)
162 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
163 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
164 !test_bit(STRIPE_R5C_CACHING, &sh->state);
167 static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
169 struct r5conf *conf = sh->raid_conf;
170 struct r5worker_group *group;
172 int i, cpu = sh->cpu;
174 if (!cpu_online(cpu)) {
175 cpu = cpumask_any(cpu_online_mask);
179 if (list_empty(&sh->lru)) {
180 struct r5worker_group *group;
181 group = conf->worker_groups + cpu_to_group(cpu);
182 if (stripe_is_lowprio(sh))
183 list_add_tail(&sh->lru, &group->loprio_list);
185 list_add_tail(&sh->lru, &group->handle_list);
186 group->stripes_cnt++;
190 if (conf->worker_cnt_per_group == 0) {
191 md_wakeup_thread(conf->mddev->thread);
195 group = conf->worker_groups + cpu_to_group(sh->cpu);
197 group->workers[0].working = true;
198 /* at least one worker should run to avoid race */
199 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
201 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
202 /* wakeup more workers */
203 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
204 if (group->workers[i].working == false) {
205 group->workers[i].working = true;
206 queue_work_on(sh->cpu, raid5_wq,
207 &group->workers[i].work);
213 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
214 struct list_head *temp_inactive_list)
217 int injournal = 0; /* number of date pages with R5_InJournal */
219 BUG_ON(!list_empty(&sh->lru));
220 BUG_ON(atomic_read(&conf->active_stripes)==0);
222 if (r5c_is_writeback(conf->log))
223 for (i = sh->disks; i--; )
224 if (test_bit(R5_InJournal, &sh->dev[i].flags))
227 * In the following cases, the stripe cannot be released to cached
228 * lists. Therefore, we make the stripe write out and set
230 * 1. when quiesce in r5c write back;
231 * 2. when resync is requested fot the stripe.
233 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
234 (conf->quiesce && r5c_is_writeback(conf->log) &&
235 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
236 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
237 r5c_make_stripe_write_out(sh);
238 set_bit(STRIPE_HANDLE, &sh->state);
241 if (test_bit(STRIPE_HANDLE, &sh->state)) {
242 if (test_bit(STRIPE_DELAYED, &sh->state) &&
243 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
244 list_add_tail(&sh->lru, &conf->delayed_list);
245 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
246 sh->bm_seq - conf->seq_write > 0)
247 list_add_tail(&sh->lru, &conf->bitmap_list);
249 clear_bit(STRIPE_DELAYED, &sh->state);
250 clear_bit(STRIPE_BIT_DELAY, &sh->state);
251 if (conf->worker_cnt_per_group == 0) {
252 if (stripe_is_lowprio(sh))
253 list_add_tail(&sh->lru,
256 list_add_tail(&sh->lru,
259 raid5_wakeup_stripe_thread(sh);
263 md_wakeup_thread(conf->mddev->thread);
265 BUG_ON(stripe_operations_active(sh));
266 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
267 if (atomic_dec_return(&conf->preread_active_stripes)
269 md_wakeup_thread(conf->mddev->thread);
270 atomic_dec(&conf->active_stripes);
271 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
272 if (!r5c_is_writeback(conf->log))
273 list_add_tail(&sh->lru, temp_inactive_list);
275 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
277 list_add_tail(&sh->lru, temp_inactive_list);
278 else if (injournal == conf->raid_disks - conf->max_degraded) {
280 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
281 atomic_inc(&conf->r5c_cached_full_stripes);
282 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
283 atomic_dec(&conf->r5c_cached_partial_stripes);
284 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
285 r5c_check_cached_full_stripe(conf);
288 * STRIPE_R5C_PARTIAL_STRIPE is set in
289 * r5c_try_caching_write(). No need to
292 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
298 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
299 struct list_head *temp_inactive_list)
301 if (atomic_dec_and_test(&sh->count))
302 do_release_stripe(conf, sh, temp_inactive_list);
306 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
308 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
309 * given time. Adding stripes only takes device lock, while deleting stripes
310 * only takes hash lock.
312 static void release_inactive_stripe_list(struct r5conf *conf,
313 struct list_head *temp_inactive_list,
317 bool do_wakeup = false;
320 if (hash == NR_STRIPE_HASH_LOCKS) {
321 size = NR_STRIPE_HASH_LOCKS;
322 hash = NR_STRIPE_HASH_LOCKS - 1;
326 struct list_head *list = &temp_inactive_list[size - 1];
329 * We don't hold any lock here yet, raid5_get_active_stripe() might
330 * remove stripes from the list
332 if (!list_empty_careful(list)) {
333 spin_lock_irqsave(conf->hash_locks + hash, flags);
334 if (list_empty(conf->inactive_list + hash) &&
336 atomic_dec(&conf->empty_inactive_list_nr);
337 list_splice_tail_init(list, conf->inactive_list + hash);
339 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
346 wake_up(&conf->wait_for_stripe);
347 if (atomic_read(&conf->active_stripes) == 0)
348 wake_up(&conf->wait_for_quiescent);
349 if (conf->retry_read_aligned)
350 md_wakeup_thread(conf->mddev->thread);
354 /* should hold conf->device_lock already */
355 static int release_stripe_list(struct r5conf *conf,
356 struct list_head *temp_inactive_list)
358 struct stripe_head *sh, *t;
360 struct llist_node *head;
362 head = llist_del_all(&conf->released_stripes);
363 head = llist_reverse_order(head);
364 llist_for_each_entry_safe(sh, t, head, release_list) {
367 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
369 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
371 * Don't worry the bit is set here, because if the bit is set
372 * again, the count is always > 1. This is true for
373 * STRIPE_ON_UNPLUG_LIST bit too.
375 hash = sh->hash_lock_index;
376 __release_stripe(conf, sh, &temp_inactive_list[hash]);
383 void raid5_release_stripe(struct stripe_head *sh)
385 struct r5conf *conf = sh->raid_conf;
387 struct list_head list;
391 /* Avoid release_list until the last reference.
393 if (atomic_add_unless(&sh->count, -1, 1))
396 if (unlikely(!conf->mddev->thread) ||
397 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
399 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
401 md_wakeup_thread(conf->mddev->thread);
404 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
405 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
406 INIT_LIST_HEAD(&list);
407 hash = sh->hash_lock_index;
408 do_release_stripe(conf, sh, &list);
409 spin_unlock_irqrestore(&conf->device_lock, flags);
410 release_inactive_stripe_list(conf, &list, hash);
414 static inline void remove_hash(struct stripe_head *sh)
416 pr_debug("remove_hash(), stripe %llu\n",
417 (unsigned long long)sh->sector);
419 hlist_del_init(&sh->hash);
422 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
424 struct hlist_head *hp = stripe_hash(conf, sh->sector);
426 pr_debug("insert_hash(), stripe %llu\n",
427 (unsigned long long)sh->sector);
429 hlist_add_head(&sh->hash, hp);
432 /* find an idle stripe, make sure it is unhashed, and return it. */
433 static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
435 struct stripe_head *sh = NULL;
436 struct list_head *first;
438 if (list_empty(conf->inactive_list + hash))
440 first = (conf->inactive_list + hash)->next;
441 sh = list_entry(first, struct stripe_head, lru);
442 list_del_init(first);
444 atomic_inc(&conf->active_stripes);
445 BUG_ON(hash != sh->hash_lock_index);
446 if (list_empty(conf->inactive_list + hash))
447 atomic_inc(&conf->empty_inactive_list_nr);
452 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
453 static void free_stripe_pages(struct stripe_head *sh)
458 /* Have not allocate page pool */
462 for (i = 0; i < sh->nr_pages; i++) {
470 static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp)
475 for (i = 0; i < sh->nr_pages; i++) {
476 /* The page have allocated. */
482 free_stripe_pages(sh);
491 init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks)
498 /* Each of the sh->dev[i] need one conf->stripe_size */
499 cnt = PAGE_SIZE / conf->stripe_size;
500 nr_pages = (disks + cnt - 1) / cnt;
502 sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
505 sh->nr_pages = nr_pages;
506 sh->stripes_per_page = cnt;
511 static void shrink_buffers(struct stripe_head *sh)
514 int num = sh->raid_conf->pool_size;
516 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE
517 for (i = 0; i < num ; i++) {
520 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
524 sh->dev[i].page = NULL;
528 for (i = 0; i < num; i++)
529 sh->dev[i].page = NULL;
530 free_stripe_pages(sh); /* Free pages */
534 static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
537 int num = sh->raid_conf->pool_size;
539 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE
540 for (i = 0; i < num; i++) {
543 if (!(page = alloc_page(gfp))) {
546 sh->dev[i].page = page;
547 sh->dev[i].orig_page = page;
548 sh->dev[i].offset = 0;
551 if (alloc_stripe_pages(sh, gfp))
554 for (i = 0; i < num; i++) {
555 sh->dev[i].page = raid5_get_dev_page(sh, i);
556 sh->dev[i].orig_page = sh->dev[i].page;
557 sh->dev[i].offset = raid5_get_page_offset(sh, i);
563 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
564 struct stripe_head *sh);
566 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
568 struct r5conf *conf = sh->raid_conf;
571 BUG_ON(atomic_read(&sh->count) != 0);
572 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
573 BUG_ON(stripe_operations_active(sh));
574 BUG_ON(sh->batch_head);
576 pr_debug("init_stripe called, stripe %llu\n",
577 (unsigned long long)sector);
579 seq = read_seqcount_begin(&conf->gen_lock);
580 sh->generation = conf->generation - previous;
581 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
583 stripe_set_idx(sector, conf, previous, sh);
586 for (i = sh->disks; i--; ) {
587 struct r5dev *dev = &sh->dev[i];
589 if (dev->toread || dev->read || dev->towrite || dev->written ||
590 test_bit(R5_LOCKED, &dev->flags)) {
591 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
592 (unsigned long long)sh->sector, i, dev->toread,
593 dev->read, dev->towrite, dev->written,
594 test_bit(R5_LOCKED, &dev->flags));
598 dev->sector = raid5_compute_blocknr(sh, i, previous);
600 if (read_seqcount_retry(&conf->gen_lock, seq))
602 sh->overwrite_disks = 0;
603 insert_hash(conf, sh);
604 sh->cpu = smp_processor_id();
605 set_bit(STRIPE_BATCH_READY, &sh->state);
608 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
611 struct stripe_head *sh;
613 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
614 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
615 if (sh->sector == sector && sh->generation == generation)
617 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
622 * Need to check if array has failed when deciding whether to:
624 * - remove non-faulty devices
627 * This determination is simple when no reshape is happening.
628 * However if there is a reshape, we need to carefully check
629 * both the before and after sections.
630 * This is because some failed devices may only affect one
631 * of the two sections, and some non-in_sync devices may
632 * be insync in the section most affected by failed devices.
634 int raid5_calc_degraded(struct r5conf *conf)
636 int degraded, degraded2;
641 for (i = 0; i < conf->previous_raid_disks; i++) {
642 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
643 if (rdev && test_bit(Faulty, &rdev->flags))
644 rdev = rcu_dereference(conf->disks[i].replacement);
645 if (!rdev || test_bit(Faulty, &rdev->flags))
647 else if (test_bit(In_sync, &rdev->flags))
650 /* not in-sync or faulty.
651 * If the reshape increases the number of devices,
652 * this is being recovered by the reshape, so
653 * this 'previous' section is not in_sync.
654 * If the number of devices is being reduced however,
655 * the device can only be part of the array if
656 * we are reverting a reshape, so this section will
659 if (conf->raid_disks >= conf->previous_raid_disks)
663 if (conf->raid_disks == conf->previous_raid_disks)
667 for (i = 0; i < conf->raid_disks; i++) {
668 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
669 if (rdev && test_bit(Faulty, &rdev->flags))
670 rdev = rcu_dereference(conf->disks[i].replacement);
671 if (!rdev || test_bit(Faulty, &rdev->flags))
673 else if (test_bit(In_sync, &rdev->flags))
676 /* not in-sync or faulty.
677 * If reshape increases the number of devices, this
678 * section has already been recovered, else it
679 * almost certainly hasn't.
681 if (conf->raid_disks <= conf->previous_raid_disks)
685 if (degraded2 > degraded)
690 static bool has_failed(struct r5conf *conf)
692 int degraded = conf->mddev->degraded;
694 if (test_bit(MD_BROKEN, &conf->mddev->flags))
697 if (conf->mddev->reshape_position != MaxSector)
698 degraded = raid5_calc_degraded(conf);
700 return degraded > conf->max_degraded;
704 raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
705 int previous, int noblock, int noquiesce)
707 struct stripe_head *sh;
708 int hash = stripe_hash_locks_hash(conf, sector);
709 int inc_empty_inactive_list_flag;
711 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
713 spin_lock_irq(conf->hash_locks + hash);
716 wait_event_lock_irq(conf->wait_for_quiescent,
717 conf->quiesce == 0 || noquiesce,
718 *(conf->hash_locks + hash));
719 sh = __find_stripe(conf, sector, conf->generation - previous);
721 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
722 sh = get_free_stripe(conf, hash);
723 if (!sh && !test_bit(R5_DID_ALLOC,
725 set_bit(R5_ALLOC_MORE,
728 if (noblock && sh == NULL)
731 r5c_check_stripe_cache_usage(conf);
733 set_bit(R5_INACTIVE_BLOCKED,
735 r5l_wake_reclaim(conf->log, 0);
737 conf->wait_for_stripe,
738 !list_empty(conf->inactive_list + hash) &&
739 (atomic_read(&conf->active_stripes)
740 < (conf->max_nr_stripes * 3 / 4)
741 || !test_bit(R5_INACTIVE_BLOCKED,
742 &conf->cache_state)),
743 *(conf->hash_locks + hash));
744 clear_bit(R5_INACTIVE_BLOCKED,
747 init_stripe(sh, sector, previous);
748 atomic_inc(&sh->count);
750 } else if (!atomic_inc_not_zero(&sh->count)) {
751 spin_lock(&conf->device_lock);
752 if (!atomic_read(&sh->count)) {
753 if (!test_bit(STRIPE_HANDLE, &sh->state))
754 atomic_inc(&conf->active_stripes);
755 BUG_ON(list_empty(&sh->lru) &&
756 !test_bit(STRIPE_EXPANDING, &sh->state));
757 inc_empty_inactive_list_flag = 0;
758 if (!list_empty(conf->inactive_list + hash))
759 inc_empty_inactive_list_flag = 1;
760 list_del_init(&sh->lru);
761 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
762 atomic_inc(&conf->empty_inactive_list_nr);
764 sh->group->stripes_cnt--;
768 atomic_inc(&sh->count);
769 spin_unlock(&conf->device_lock);
771 } while (sh == NULL);
773 spin_unlock_irq(conf->hash_locks + hash);
777 static bool is_full_stripe_write(struct stripe_head *sh)
779 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
780 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
783 static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
784 __acquires(&sh1->stripe_lock)
785 __acquires(&sh2->stripe_lock)
788 spin_lock_irq(&sh2->stripe_lock);
789 spin_lock_nested(&sh1->stripe_lock, 1);
791 spin_lock_irq(&sh1->stripe_lock);
792 spin_lock_nested(&sh2->stripe_lock, 1);
796 static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
797 __releases(&sh1->stripe_lock)
798 __releases(&sh2->stripe_lock)
800 spin_unlock(&sh1->stripe_lock);
801 spin_unlock_irq(&sh2->stripe_lock);
804 /* Only freshly new full stripe normal write stripe can be added to a batch list */
805 static bool stripe_can_batch(struct stripe_head *sh)
807 struct r5conf *conf = sh->raid_conf;
809 if (raid5_has_log(conf) || raid5_has_ppl(conf))
811 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
812 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
813 is_full_stripe_write(sh);
816 /* we only do back search */
817 static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
819 struct stripe_head *head;
820 sector_t head_sector, tmp_sec;
823 int inc_empty_inactive_list_flag;
825 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
826 tmp_sec = sh->sector;
827 if (!sector_div(tmp_sec, conf->chunk_sectors))
829 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
831 hash = stripe_hash_locks_hash(conf, head_sector);
832 spin_lock_irq(conf->hash_locks + hash);
833 head = __find_stripe(conf, head_sector, conf->generation);
834 if (head && !atomic_inc_not_zero(&head->count)) {
835 spin_lock(&conf->device_lock);
836 if (!atomic_read(&head->count)) {
837 if (!test_bit(STRIPE_HANDLE, &head->state))
838 atomic_inc(&conf->active_stripes);
839 BUG_ON(list_empty(&head->lru) &&
840 !test_bit(STRIPE_EXPANDING, &head->state));
841 inc_empty_inactive_list_flag = 0;
842 if (!list_empty(conf->inactive_list + hash))
843 inc_empty_inactive_list_flag = 1;
844 list_del_init(&head->lru);
845 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
846 atomic_inc(&conf->empty_inactive_list_nr);
848 head->group->stripes_cnt--;
852 atomic_inc(&head->count);
853 spin_unlock(&conf->device_lock);
855 spin_unlock_irq(conf->hash_locks + hash);
859 if (!stripe_can_batch(head))
862 lock_two_stripes(head, sh);
863 /* clear_batch_ready clear the flag */
864 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
871 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
873 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
874 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
877 if (head->batch_head) {
878 spin_lock(&head->batch_head->batch_lock);
879 /* This batch list is already running */
880 if (!stripe_can_batch(head)) {
881 spin_unlock(&head->batch_head->batch_lock);
885 * We must assign batch_head of this stripe within the
886 * batch_lock, otherwise clear_batch_ready of batch head
887 * stripe could clear BATCH_READY bit of this stripe and
888 * this stripe->batch_head doesn't get assigned, which
889 * could confuse clear_batch_ready for this stripe
891 sh->batch_head = head->batch_head;
894 * at this point, head's BATCH_READY could be cleared, but we
895 * can still add the stripe to batch list
897 list_add(&sh->batch_list, &head->batch_list);
898 spin_unlock(&head->batch_head->batch_lock);
900 head->batch_head = head;
901 sh->batch_head = head->batch_head;
902 spin_lock(&head->batch_lock);
903 list_add_tail(&sh->batch_list, &head->batch_list);
904 spin_unlock(&head->batch_lock);
907 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
908 if (atomic_dec_return(&conf->preread_active_stripes)
910 md_wakeup_thread(conf->mddev->thread);
912 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
913 int seq = sh->bm_seq;
914 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
915 sh->batch_head->bm_seq > seq)
916 seq = sh->batch_head->bm_seq;
917 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
918 sh->batch_head->bm_seq = seq;
921 atomic_inc(&sh->count);
923 unlock_two_stripes(head, sh);
925 raid5_release_stripe(head);
928 /* Determine if 'data_offset' or 'new_data_offset' should be used
929 * in this stripe_head.
931 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
933 sector_t progress = conf->reshape_progress;
934 /* Need a memory barrier to make sure we see the value
935 * of conf->generation, or ->data_offset that was set before
936 * reshape_progress was updated.
939 if (progress == MaxSector)
941 if (sh->generation == conf->generation - 1)
943 /* We are in a reshape, and this is a new-generation stripe,
944 * so use new_data_offset.
949 static void dispatch_bio_list(struct bio_list *tmp)
953 while ((bio = bio_list_pop(tmp)))
954 submit_bio_noacct(bio);
957 static int cmp_stripe(void *priv, const struct list_head *a,
958 const struct list_head *b)
960 const struct r5pending_data *da = list_entry(a,
961 struct r5pending_data, sibling);
962 const struct r5pending_data *db = list_entry(b,
963 struct r5pending_data, sibling);
964 if (da->sector > db->sector)
966 if (da->sector < db->sector)
971 static void dispatch_defer_bios(struct r5conf *conf, int target,
972 struct bio_list *list)
974 struct r5pending_data *data;
975 struct list_head *first, *next = NULL;
978 if (conf->pending_data_cnt == 0)
981 list_sort(NULL, &conf->pending_list, cmp_stripe);
983 first = conf->pending_list.next;
985 /* temporarily move the head */
986 if (conf->next_pending_data)
987 list_move_tail(&conf->pending_list,
988 &conf->next_pending_data->sibling);
990 while (!list_empty(&conf->pending_list)) {
991 data = list_first_entry(&conf->pending_list,
992 struct r5pending_data, sibling);
993 if (&data->sibling == first)
994 first = data->sibling.next;
995 next = data->sibling.next;
997 bio_list_merge(list, &data->bios);
998 list_move(&data->sibling, &conf->free_list);
1003 conf->pending_data_cnt -= cnt;
1004 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
1006 if (next != &conf->pending_list)
1007 conf->next_pending_data = list_entry(next,
1008 struct r5pending_data, sibling);
1010 conf->next_pending_data = NULL;
1011 /* list isn't empty */
1012 if (first != &conf->pending_list)
1013 list_move_tail(&conf->pending_list, first);
1016 static void flush_deferred_bios(struct r5conf *conf)
1018 struct bio_list tmp = BIO_EMPTY_LIST;
1020 if (conf->pending_data_cnt == 0)
1023 spin_lock(&conf->pending_bios_lock);
1024 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
1025 BUG_ON(conf->pending_data_cnt != 0);
1026 spin_unlock(&conf->pending_bios_lock);
1028 dispatch_bio_list(&tmp);
1031 static void defer_issue_bios(struct r5conf *conf, sector_t sector,
1032 struct bio_list *bios)
1034 struct bio_list tmp = BIO_EMPTY_LIST;
1035 struct r5pending_data *ent;
1037 spin_lock(&conf->pending_bios_lock);
1038 ent = list_first_entry(&conf->free_list, struct r5pending_data,
1040 list_move_tail(&ent->sibling, &conf->pending_list);
1041 ent->sector = sector;
1042 bio_list_init(&ent->bios);
1043 bio_list_merge(&ent->bios, bios);
1044 conf->pending_data_cnt++;
1045 if (conf->pending_data_cnt >= PENDING_IO_MAX)
1046 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
1048 spin_unlock(&conf->pending_bios_lock);
1050 dispatch_bio_list(&tmp);
1054 raid5_end_read_request(struct bio *bi);
1056 raid5_end_write_request(struct bio *bi);
1058 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
1060 struct r5conf *conf = sh->raid_conf;
1061 int i, disks = sh->disks;
1062 struct stripe_head *head_sh = sh;
1063 struct bio_list pending_bios = BIO_EMPTY_LIST;
1068 if (log_stripe(sh, s) == 0)
1071 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
1073 for (i = disks; i--; ) {
1074 int op, op_flags = 0;
1075 int replace_only = 0;
1076 struct bio *bi, *rbi;
1077 struct md_rdev *rdev, *rrdev = NULL;
1080 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
1082 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
1084 if (test_bit(R5_Discard, &sh->dev[i].flags))
1085 op = REQ_OP_DISCARD;
1086 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1088 else if (test_and_clear_bit(R5_WantReplace,
1089 &sh->dev[i].flags)) {
1094 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
1095 op_flags |= REQ_SYNC;
1098 bi = &sh->dev[i].req;
1099 rbi = &sh->dev[i].rreq; /* For writing to replacement */
1102 rrdev = rcu_dereference(conf->disks[i].replacement);
1103 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1104 rdev = rcu_dereference(conf->disks[i].rdev);
1109 if (op_is_write(op)) {
1113 /* We raced and saw duplicates */
1116 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
1121 if (rdev && test_bit(Faulty, &rdev->flags))
1124 atomic_inc(&rdev->nr_pending);
1125 if (rrdev && test_bit(Faulty, &rrdev->flags))
1128 atomic_inc(&rrdev->nr_pending);
1131 /* We have already checked bad blocks for reads. Now
1132 * need to check for writes. We never accept write errors
1133 * on the replacement, so we don't to check rrdev.
1135 while (op_is_write(op) && rdev &&
1136 test_bit(WriteErrorSeen, &rdev->flags)) {
1139 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
1140 &first_bad, &bad_sectors);
1145 set_bit(BlockedBadBlocks, &rdev->flags);
1146 if (!conf->mddev->external &&
1147 conf->mddev->sb_flags) {
1148 /* It is very unlikely, but we might
1149 * still need to write out the
1150 * bad block log - better give it
1152 md_check_recovery(conf->mddev);
1155 * Because md_wait_for_blocked_rdev
1156 * will dec nr_pending, we must
1157 * increment it first.
1159 atomic_inc(&rdev->nr_pending);
1160 md_wait_for_blocked_rdev(rdev, conf->mddev);
1162 /* Acknowledged bad block - skip the write */
1163 rdev_dec_pending(rdev, conf->mddev);
1169 if (s->syncing || s->expanding || s->expanded
1171 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
1173 set_bit(STRIPE_IO_STARTED, &sh->state);
1175 bio_set_dev(bi, rdev->bdev);
1176 bio_set_op_attrs(bi, op, op_flags);
1177 bi->bi_end_io = op_is_write(op)
1178 ? raid5_end_write_request
1179 : raid5_end_read_request;
1180 bi->bi_private = sh;
1182 pr_debug("%s: for %llu schedule op %d on disc %d\n",
1183 __func__, (unsigned long long)sh->sector,
1185 atomic_inc(&sh->count);
1187 atomic_inc(&head_sh->count);
1188 if (use_new_offset(conf, sh))
1189 bi->bi_iter.bi_sector = (sh->sector
1190 + rdev->new_data_offset);
1192 bi->bi_iter.bi_sector = (sh->sector
1193 + rdev->data_offset);
1194 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
1195 bi->bi_opf |= REQ_NOMERGE;
1197 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1198 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1200 if (!op_is_write(op) &&
1201 test_bit(R5_InJournal, &sh->dev[i].flags))
1203 * issuing read for a page in journal, this
1204 * must be preparing for prexor in rmw; read
1205 * the data into orig_page
1207 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1209 sh->dev[i].vec.bv_page = sh->dev[i].page;
1211 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
1212 bi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
1213 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
1214 bi->bi_write_hint = sh->dev[i].write_hint;
1216 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
1218 * If this is discard request, set bi_vcnt 0. We don't
1219 * want to confuse SCSI because SCSI will replace payload
1221 if (op == REQ_OP_DISCARD)
1224 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
1226 if (conf->mddev->gendisk)
1227 trace_block_bio_remap(bi,
1228 disk_devt(conf->mddev->gendisk),
1230 if (should_defer && op_is_write(op))
1231 bio_list_add(&pending_bios, bi);
1233 submit_bio_noacct(bi);
1236 if (s->syncing || s->expanding || s->expanded
1238 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
1240 set_bit(STRIPE_IO_STARTED, &sh->state);
1242 bio_set_dev(rbi, rrdev->bdev);
1243 bio_set_op_attrs(rbi, op, op_flags);
1244 BUG_ON(!op_is_write(op));
1245 rbi->bi_end_io = raid5_end_write_request;
1246 rbi->bi_private = sh;
1248 pr_debug("%s: for %llu schedule op %d on "
1249 "replacement disc %d\n",
1250 __func__, (unsigned long long)sh->sector,
1252 atomic_inc(&sh->count);
1254 atomic_inc(&head_sh->count);
1255 if (use_new_offset(conf, sh))
1256 rbi->bi_iter.bi_sector = (sh->sector
1257 + rrdev->new_data_offset);
1259 rbi->bi_iter.bi_sector = (sh->sector
1260 + rrdev->data_offset);
1261 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1262 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1263 sh->dev[i].rvec.bv_page = sh->dev[i].page;
1265 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
1266 rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
1267 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
1268 rbi->bi_write_hint = sh->dev[i].write_hint;
1269 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
1271 * If this is discard request, set bi_vcnt 0. We don't
1272 * want to confuse SCSI because SCSI will replace payload
1274 if (op == REQ_OP_DISCARD)
1276 if (conf->mddev->gendisk)
1277 trace_block_bio_remap(rbi,
1278 disk_devt(conf->mddev->gendisk),
1280 if (should_defer && op_is_write(op))
1281 bio_list_add(&pending_bios, rbi);
1283 submit_bio_noacct(rbi);
1285 if (!rdev && !rrdev) {
1286 if (op_is_write(op))
1287 set_bit(STRIPE_DEGRADED, &sh->state);
1288 pr_debug("skip op %d on disc %d for sector %llu\n",
1289 bi->bi_opf, i, (unsigned long long)sh->sector);
1290 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1291 set_bit(STRIPE_HANDLE, &sh->state);
1294 if (!head_sh->batch_head)
1296 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1302 if (should_defer && !bio_list_empty(&pending_bios))
1303 defer_issue_bios(conf, head_sh->sector, &pending_bios);
1306 static struct dma_async_tx_descriptor *
1307 async_copy_data(int frombio, struct bio *bio, struct page **page,
1308 unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx,
1309 struct stripe_head *sh, int no_skipcopy)
1312 struct bvec_iter iter;
1313 struct page *bio_page;
1315 struct async_submit_ctl submit;
1316 enum async_tx_flags flags = 0;
1317 struct r5conf *conf = sh->raid_conf;
1319 if (bio->bi_iter.bi_sector >= sector)
1320 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
1322 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
1325 flags |= ASYNC_TX_FENCE;
1326 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1328 bio_for_each_segment(bvl, bio, iter) {
1329 int len = bvl.bv_len;
1333 if (page_offset < 0) {
1334 b_offset = -page_offset;
1335 page_offset += b_offset;
1339 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1340 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
1345 b_offset += bvl.bv_offset;
1346 bio_page = bvl.bv_page;
1348 if (conf->skip_copy &&
1349 b_offset == 0 && page_offset == 0 &&
1350 clen == RAID5_STRIPE_SIZE(conf) &&
1354 tx = async_memcpy(*page, bio_page, page_offset + poff,
1355 b_offset, clen, &submit);
1357 tx = async_memcpy(bio_page, *page, b_offset,
1358 page_offset + poff, clen, &submit);
1360 /* chain the operations */
1361 submit.depend_tx = tx;
1363 if (clen < len) /* hit end of page */
1371 static void ops_complete_biofill(void *stripe_head_ref)
1373 struct stripe_head *sh = stripe_head_ref;
1375 struct r5conf *conf = sh->raid_conf;
1377 pr_debug("%s: stripe %llu\n", __func__,
1378 (unsigned long long)sh->sector);
1380 /* clear completed biofills */
1381 for (i = sh->disks; i--; ) {
1382 struct r5dev *dev = &sh->dev[i];
1384 /* acknowledge completion of a biofill operation */
1385 /* and check if we need to reply to a read request,
1386 * new R5_Wantfill requests are held off until
1387 * !STRIPE_BIOFILL_RUN
1389 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
1390 struct bio *rbi, *rbi2;
1395 while (rbi && rbi->bi_iter.bi_sector <
1396 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1397 rbi2 = r5_next_bio(conf, rbi, dev->sector);
1403 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
1405 set_bit(STRIPE_HANDLE, &sh->state);
1406 raid5_release_stripe(sh);
1409 static void ops_run_biofill(struct stripe_head *sh)
1411 struct dma_async_tx_descriptor *tx = NULL;
1412 struct async_submit_ctl submit;
1414 struct r5conf *conf = sh->raid_conf;
1416 BUG_ON(sh->batch_head);
1417 pr_debug("%s: stripe %llu\n", __func__,
1418 (unsigned long long)sh->sector);
1420 for (i = sh->disks; i--; ) {
1421 struct r5dev *dev = &sh->dev[i];
1422 if (test_bit(R5_Wantfill, &dev->flags)) {
1424 spin_lock_irq(&sh->stripe_lock);
1425 dev->read = rbi = dev->toread;
1427 spin_unlock_irq(&sh->stripe_lock);
1428 while (rbi && rbi->bi_iter.bi_sector <
1429 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1430 tx = async_copy_data(0, rbi, &dev->page,
1432 dev->sector, tx, sh, 0);
1433 rbi = r5_next_bio(conf, rbi, dev->sector);
1438 atomic_inc(&sh->count);
1439 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1440 async_trigger_callback(&submit);
1443 static void mark_target_uptodate(struct stripe_head *sh, int target)
1450 tgt = &sh->dev[target];
1451 set_bit(R5_UPTODATE, &tgt->flags);
1452 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1453 clear_bit(R5_Wantcompute, &tgt->flags);
1456 static void ops_complete_compute(void *stripe_head_ref)
1458 struct stripe_head *sh = stripe_head_ref;
1460 pr_debug("%s: stripe %llu\n", __func__,
1461 (unsigned long long)sh->sector);
1463 /* mark the computed target(s) as uptodate */
1464 mark_target_uptodate(sh, sh->ops.target);
1465 mark_target_uptodate(sh, sh->ops.target2);
1467 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1468 if (sh->check_state == check_state_compute_run)
1469 sh->check_state = check_state_compute_result;
1470 set_bit(STRIPE_HANDLE, &sh->state);
1471 raid5_release_stripe(sh);
1474 /* return a pointer to the address conversion region of the scribble buffer */
1475 static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1477 return percpu->scribble + i * percpu->scribble_obj_size;
1480 /* return a pointer to the address conversion region of the scribble buffer */
1481 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1482 struct raid5_percpu *percpu, int i)
1484 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
1488 * Return a pointer to record offset address.
1490 static unsigned int *
1491 to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu)
1493 return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2);
1496 static struct dma_async_tx_descriptor *
1497 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1499 int disks = sh->disks;
1500 struct page **xor_srcs = to_addr_page(percpu, 0);
1501 unsigned int *off_srcs = to_addr_offs(sh, percpu);
1502 int target = sh->ops.target;
1503 struct r5dev *tgt = &sh->dev[target];
1504 struct page *xor_dest = tgt->page;
1505 unsigned int off_dest = tgt->offset;
1507 struct dma_async_tx_descriptor *tx;
1508 struct async_submit_ctl submit;
1511 BUG_ON(sh->batch_head);
1513 pr_debug("%s: stripe %llu block: %d\n",
1514 __func__, (unsigned long long)sh->sector, target);
1515 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1517 for (i = disks; i--; ) {
1519 off_srcs[count] = sh->dev[i].offset;
1520 xor_srcs[count++] = sh->dev[i].page;
1524 atomic_inc(&sh->count);
1526 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
1527 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
1528 if (unlikely(count == 1))
1529 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
1530 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1532 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
1533 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1538 /* set_syndrome_sources - populate source buffers for gen_syndrome
1539 * @srcs - (struct page *) array of size sh->disks
1540 * @offs - (unsigned int) array of offset for each page
1541 * @sh - stripe_head to parse
1543 * Populates srcs in proper layout order for the stripe and returns the
1544 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1545 * destination buffer is recorded in srcs[count] and the Q destination
1546 * is recorded in srcs[count+1]].
1548 static int set_syndrome_sources(struct page **srcs,
1550 struct stripe_head *sh,
1553 int disks = sh->disks;
1554 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1555 int d0_idx = raid6_d0(sh);
1559 for (i = 0; i < disks; i++)
1565 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1566 struct r5dev *dev = &sh->dev[i];
1568 if (i == sh->qd_idx || i == sh->pd_idx ||
1569 (srctype == SYNDROME_SRC_ALL) ||
1570 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1571 (test_bit(R5_Wantdrain, &dev->flags) ||
1572 test_bit(R5_InJournal, &dev->flags))) ||
1573 (srctype == SYNDROME_SRC_WRITTEN &&
1575 test_bit(R5_InJournal, &dev->flags)))) {
1576 if (test_bit(R5_InJournal, &dev->flags))
1577 srcs[slot] = sh->dev[i].orig_page;
1579 srcs[slot] = sh->dev[i].page;
1581 * For R5_InJournal, PAGE_SIZE must be 4KB and will
1582 * not shared page. In that case, dev[i].offset
1585 offs[slot] = sh->dev[i].offset;
1587 i = raid6_next_disk(i, disks);
1588 } while (i != d0_idx);
1590 return syndrome_disks;
1593 static struct dma_async_tx_descriptor *
1594 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1596 int disks = sh->disks;
1597 struct page **blocks = to_addr_page(percpu, 0);
1598 unsigned int *offs = to_addr_offs(sh, percpu);
1600 int qd_idx = sh->qd_idx;
1601 struct dma_async_tx_descriptor *tx;
1602 struct async_submit_ctl submit;
1605 unsigned int dest_off;
1609 BUG_ON(sh->batch_head);
1610 if (sh->ops.target < 0)
1611 target = sh->ops.target2;
1612 else if (sh->ops.target2 < 0)
1613 target = sh->ops.target;
1615 /* we should only have one valid target */
1618 pr_debug("%s: stripe %llu block: %d\n",
1619 __func__, (unsigned long long)sh->sector, target);
1621 tgt = &sh->dev[target];
1622 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1624 dest_off = tgt->offset;
1626 atomic_inc(&sh->count);
1628 if (target == qd_idx) {
1629 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
1630 blocks[count] = NULL; /* regenerating p is not necessary */
1631 BUG_ON(blocks[count+1] != dest); /* q should already be set */
1632 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1633 ops_complete_compute, sh,
1634 to_addr_conv(sh, percpu, 0));
1635 tx = async_gen_syndrome(blocks, offs, count+2,
1636 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1638 /* Compute any data- or p-drive using XOR */
1640 for (i = disks; i-- ; ) {
1641 if (i == target || i == qd_idx)
1643 offs[count] = sh->dev[i].offset;
1644 blocks[count++] = sh->dev[i].page;
1647 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1648 NULL, ops_complete_compute, sh,
1649 to_addr_conv(sh, percpu, 0));
1650 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
1651 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1657 static struct dma_async_tx_descriptor *
1658 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1660 int i, count, disks = sh->disks;
1661 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1662 int d0_idx = raid6_d0(sh);
1663 int faila = -1, failb = -1;
1664 int target = sh->ops.target;
1665 int target2 = sh->ops.target2;
1666 struct r5dev *tgt = &sh->dev[target];
1667 struct r5dev *tgt2 = &sh->dev[target2];
1668 struct dma_async_tx_descriptor *tx;
1669 struct page **blocks = to_addr_page(percpu, 0);
1670 unsigned int *offs = to_addr_offs(sh, percpu);
1671 struct async_submit_ctl submit;
1673 BUG_ON(sh->batch_head);
1674 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1675 __func__, (unsigned long long)sh->sector, target, target2);
1676 BUG_ON(target < 0 || target2 < 0);
1677 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1678 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1680 /* we need to open-code set_syndrome_sources to handle the
1681 * slot number conversion for 'faila' and 'failb'
1683 for (i = 0; i < disks ; i++) {
1690 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1692 offs[slot] = sh->dev[i].offset;
1693 blocks[slot] = sh->dev[i].page;
1699 i = raid6_next_disk(i, disks);
1700 } while (i != d0_idx);
1702 BUG_ON(faila == failb);
1705 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1706 __func__, (unsigned long long)sh->sector, faila, failb);
1708 atomic_inc(&sh->count);
1710 if (failb == syndrome_disks+1) {
1711 /* Q disk is one of the missing disks */
1712 if (faila == syndrome_disks) {
1713 /* Missing P+Q, just recompute */
1714 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1715 ops_complete_compute, sh,
1716 to_addr_conv(sh, percpu, 0));
1717 return async_gen_syndrome(blocks, offs, syndrome_disks+2,
1718 RAID5_STRIPE_SIZE(sh->raid_conf),
1722 unsigned int dest_off;
1724 int qd_idx = sh->qd_idx;
1726 /* Missing D+Q: recompute D from P, then recompute Q */
1727 if (target == qd_idx)
1728 data_target = target2;
1730 data_target = target;
1733 for (i = disks; i-- ; ) {
1734 if (i == data_target || i == qd_idx)
1736 offs[count] = sh->dev[i].offset;
1737 blocks[count++] = sh->dev[i].page;
1739 dest = sh->dev[data_target].page;
1740 dest_off = sh->dev[data_target].offset;
1741 init_async_submit(&submit,
1742 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1744 to_addr_conv(sh, percpu, 0));
1745 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
1746 RAID5_STRIPE_SIZE(sh->raid_conf),
1749 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
1750 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1751 ops_complete_compute, sh,
1752 to_addr_conv(sh, percpu, 0));
1753 return async_gen_syndrome(blocks, offs, count+2,
1754 RAID5_STRIPE_SIZE(sh->raid_conf),
1758 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1759 ops_complete_compute, sh,
1760 to_addr_conv(sh, percpu, 0));
1761 if (failb == syndrome_disks) {
1762 /* We're missing D+P. */
1763 return async_raid6_datap_recov(syndrome_disks+2,
1764 RAID5_STRIPE_SIZE(sh->raid_conf),
1766 blocks, offs, &submit);
1768 /* We're missing D+D. */
1769 return async_raid6_2data_recov(syndrome_disks+2,
1770 RAID5_STRIPE_SIZE(sh->raid_conf),
1772 blocks, offs, &submit);
1777 static void ops_complete_prexor(void *stripe_head_ref)
1779 struct stripe_head *sh = stripe_head_ref;
1781 pr_debug("%s: stripe %llu\n", __func__,
1782 (unsigned long long)sh->sector);
1784 if (r5c_is_writeback(sh->raid_conf->log))
1786 * raid5-cache write back uses orig_page during prexor.
1787 * After prexor, it is time to free orig_page
1789 r5c_release_extra_page(sh);
1792 static struct dma_async_tx_descriptor *
1793 ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1794 struct dma_async_tx_descriptor *tx)
1796 int disks = sh->disks;
1797 struct page **xor_srcs = to_addr_page(percpu, 0);
1798 unsigned int *off_srcs = to_addr_offs(sh, percpu);
1799 int count = 0, pd_idx = sh->pd_idx, i;
1800 struct async_submit_ctl submit;
1802 /* existing parity data subtracted */
1803 unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
1804 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1806 BUG_ON(sh->batch_head);
1807 pr_debug("%s: stripe %llu\n", __func__,
1808 (unsigned long long)sh->sector);
1810 for (i = disks; i--; ) {
1811 struct r5dev *dev = &sh->dev[i];
1812 /* Only process blocks that are known to be uptodate */
1813 if (test_bit(R5_InJournal, &dev->flags)) {
1815 * For this case, PAGE_SIZE must be equal to 4KB and
1816 * page offset is zero.
1818 off_srcs[count] = dev->offset;
1819 xor_srcs[count++] = dev->orig_page;
1820 } else if (test_bit(R5_Wantdrain, &dev->flags)) {
1821 off_srcs[count] = dev->offset;
1822 xor_srcs[count++] = dev->page;
1826 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1827 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1828 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
1829 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1834 static struct dma_async_tx_descriptor *
1835 ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1836 struct dma_async_tx_descriptor *tx)
1838 struct page **blocks = to_addr_page(percpu, 0);
1839 unsigned int *offs = to_addr_offs(sh, percpu);
1841 struct async_submit_ctl submit;
1843 pr_debug("%s: stripe %llu\n", __func__,
1844 (unsigned long long)sh->sector);
1846 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN);
1848 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1849 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1850 tx = async_gen_syndrome(blocks, offs, count+2,
1851 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1856 static struct dma_async_tx_descriptor *
1857 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1859 struct r5conf *conf = sh->raid_conf;
1860 int disks = sh->disks;
1862 struct stripe_head *head_sh = sh;
1864 pr_debug("%s: stripe %llu\n", __func__,
1865 (unsigned long long)sh->sector);
1867 for (i = disks; i--; ) {
1872 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
1878 * clear R5_InJournal, so when rewriting a page in
1879 * journal, it is not skipped by r5l_log_stripe()
1881 clear_bit(R5_InJournal, &dev->flags);
1882 spin_lock_irq(&sh->stripe_lock);
1883 chosen = dev->towrite;
1884 dev->towrite = NULL;
1885 sh->overwrite_disks = 0;
1886 BUG_ON(dev->written);
1887 wbi = dev->written = chosen;
1888 spin_unlock_irq(&sh->stripe_lock);
1889 WARN_ON(dev->page != dev->orig_page);
1891 while (wbi && wbi->bi_iter.bi_sector <
1892 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1893 if (wbi->bi_opf & REQ_FUA)
1894 set_bit(R5_WantFUA, &dev->flags);
1895 if (wbi->bi_opf & REQ_SYNC)
1896 set_bit(R5_SyncIO, &dev->flags);
1897 if (bio_op(wbi) == REQ_OP_DISCARD)
1898 set_bit(R5_Discard, &dev->flags);
1900 tx = async_copy_data(1, wbi, &dev->page,
1902 dev->sector, tx, sh,
1903 r5c_is_writeback(conf->log));
1904 if (dev->page != dev->orig_page &&
1905 !r5c_is_writeback(conf->log)) {
1906 set_bit(R5_SkipCopy, &dev->flags);
1907 clear_bit(R5_UPTODATE, &dev->flags);
1908 clear_bit(R5_OVERWRITE, &dev->flags);
1911 wbi = r5_next_bio(conf, wbi, dev->sector);
1914 if (head_sh->batch_head) {
1915 sh = list_first_entry(&sh->batch_list,
1928 static void ops_complete_reconstruct(void *stripe_head_ref)
1930 struct stripe_head *sh = stripe_head_ref;
1931 int disks = sh->disks;
1932 int pd_idx = sh->pd_idx;
1933 int qd_idx = sh->qd_idx;
1935 bool fua = false, sync = false, discard = false;
1937 pr_debug("%s: stripe %llu\n", __func__,
1938 (unsigned long long)sh->sector);
1940 for (i = disks; i--; ) {
1941 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1942 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1943 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1946 for (i = disks; i--; ) {
1947 struct r5dev *dev = &sh->dev[i];
1949 if (dev->written || i == pd_idx || i == qd_idx) {
1950 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
1951 set_bit(R5_UPTODATE, &dev->flags);
1952 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1953 set_bit(R5_Expanded, &dev->flags);
1956 set_bit(R5_WantFUA, &dev->flags);
1958 set_bit(R5_SyncIO, &dev->flags);
1962 if (sh->reconstruct_state == reconstruct_state_drain_run)
1963 sh->reconstruct_state = reconstruct_state_drain_result;
1964 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1965 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1967 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1968 sh->reconstruct_state = reconstruct_state_result;
1971 set_bit(STRIPE_HANDLE, &sh->state);
1972 raid5_release_stripe(sh);
1976 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1977 struct dma_async_tx_descriptor *tx)
1979 int disks = sh->disks;
1980 struct page **xor_srcs;
1981 unsigned int *off_srcs;
1982 struct async_submit_ctl submit;
1983 int count, pd_idx = sh->pd_idx, i;
1984 struct page *xor_dest;
1985 unsigned int off_dest;
1987 unsigned long flags;
1989 struct stripe_head *head_sh = sh;
1992 pr_debug("%s: stripe %llu\n", __func__,
1993 (unsigned long long)sh->sector);
1995 for (i = 0; i < sh->disks; i++) {
1998 if (!test_bit(R5_Discard, &sh->dev[i].flags))
2001 if (i >= sh->disks) {
2002 atomic_inc(&sh->count);
2003 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
2004 ops_complete_reconstruct(sh);
2009 xor_srcs = to_addr_page(percpu, j);
2010 off_srcs = to_addr_offs(sh, percpu);
2011 /* check if prexor is active which means only process blocks
2012 * that are part of a read-modify-write (written)
2014 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2016 off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
2017 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
2018 for (i = disks; i--; ) {
2019 struct r5dev *dev = &sh->dev[i];
2020 if (head_sh->dev[i].written ||
2021 test_bit(R5_InJournal, &head_sh->dev[i].flags)) {
2022 off_srcs[count] = dev->offset;
2023 xor_srcs[count++] = dev->page;
2027 xor_dest = sh->dev[pd_idx].page;
2028 off_dest = sh->dev[pd_idx].offset;
2029 for (i = disks; i--; ) {
2030 struct r5dev *dev = &sh->dev[i];
2032 off_srcs[count] = dev->offset;
2033 xor_srcs[count++] = dev->page;
2038 /* 1/ if we prexor'd then the dest is reused as a source
2039 * 2/ if we did not prexor then we are redoing the parity
2040 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
2041 * for the synchronous xor case
2043 last_stripe = !head_sh->batch_head ||
2044 list_first_entry(&sh->batch_list,
2045 struct stripe_head, batch_list) == head_sh;
2047 flags = ASYNC_TX_ACK |
2048 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
2050 atomic_inc(&head_sh->count);
2051 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
2052 to_addr_conv(sh, percpu, j));
2054 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
2055 init_async_submit(&submit, flags, tx, NULL, NULL,
2056 to_addr_conv(sh, percpu, j));
2059 if (unlikely(count == 1))
2060 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
2061 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2063 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
2064 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2067 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2074 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
2075 struct dma_async_tx_descriptor *tx)
2077 struct async_submit_ctl submit;
2078 struct page **blocks;
2080 int count, i, j = 0;
2081 struct stripe_head *head_sh = sh;
2084 unsigned long txflags;
2086 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
2088 for (i = 0; i < sh->disks; i++) {
2089 if (sh->pd_idx == i || sh->qd_idx == i)
2091 if (!test_bit(R5_Discard, &sh->dev[i].flags))
2094 if (i >= sh->disks) {
2095 atomic_inc(&sh->count);
2096 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2097 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2098 ops_complete_reconstruct(sh);
2103 blocks = to_addr_page(percpu, j);
2104 offs = to_addr_offs(sh, percpu);
2106 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2107 synflags = SYNDROME_SRC_WRITTEN;
2108 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
2110 synflags = SYNDROME_SRC_ALL;
2111 txflags = ASYNC_TX_ACK;
2114 count = set_syndrome_sources(blocks, offs, sh, synflags);
2115 last_stripe = !head_sh->batch_head ||
2116 list_first_entry(&sh->batch_list,
2117 struct stripe_head, batch_list) == head_sh;
2120 atomic_inc(&head_sh->count);
2121 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
2122 head_sh, to_addr_conv(sh, percpu, j));
2124 init_async_submit(&submit, 0, tx, NULL, NULL,
2125 to_addr_conv(sh, percpu, j));
2126 tx = async_gen_syndrome(blocks, offs, count+2,
2127 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2130 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2136 static void ops_complete_check(void *stripe_head_ref)
2138 struct stripe_head *sh = stripe_head_ref;
2140 pr_debug("%s: stripe %llu\n", __func__,
2141 (unsigned long long)sh->sector);
2143 sh->check_state = check_state_check_result;
2144 set_bit(STRIPE_HANDLE, &sh->state);
2145 raid5_release_stripe(sh);
2148 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
2150 int disks = sh->disks;
2151 int pd_idx = sh->pd_idx;
2152 int qd_idx = sh->qd_idx;
2153 struct page *xor_dest;
2154 unsigned int off_dest;
2155 struct page **xor_srcs = to_addr_page(percpu, 0);
2156 unsigned int *off_srcs = to_addr_offs(sh, percpu);
2157 struct dma_async_tx_descriptor *tx;
2158 struct async_submit_ctl submit;
2162 pr_debug("%s: stripe %llu\n", __func__,
2163 (unsigned long long)sh->sector);
2165 BUG_ON(sh->batch_head);
2167 xor_dest = sh->dev[pd_idx].page;
2168 off_dest = sh->dev[pd_idx].offset;
2169 off_srcs[count] = off_dest;
2170 xor_srcs[count++] = xor_dest;
2171 for (i = disks; i--; ) {
2172 if (i == pd_idx || i == qd_idx)
2174 off_srcs[count] = sh->dev[i].offset;
2175 xor_srcs[count++] = sh->dev[i].page;
2178 init_async_submit(&submit, 0, NULL, NULL, NULL,
2179 to_addr_conv(sh, percpu, 0));
2180 tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
2181 RAID5_STRIPE_SIZE(sh->raid_conf),
2182 &sh->ops.zero_sum_result, &submit);
2184 atomic_inc(&sh->count);
2185 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2186 tx = async_trigger_callback(&submit);
2189 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2191 struct page **srcs = to_addr_page(percpu, 0);
2192 unsigned int *offs = to_addr_offs(sh, percpu);
2193 struct async_submit_ctl submit;
2196 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2197 (unsigned long long)sh->sector, checkp);
2199 BUG_ON(sh->batch_head);
2200 count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL);
2204 atomic_inc(&sh->count);
2205 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
2206 sh, to_addr_conv(sh, percpu, 0));
2207 async_syndrome_val(srcs, offs, count+2,
2208 RAID5_STRIPE_SIZE(sh->raid_conf),
2209 &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit);
2212 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
2214 int overlap_clear = 0, i, disks = sh->disks;
2215 struct dma_async_tx_descriptor *tx = NULL;
2216 struct r5conf *conf = sh->raid_conf;
2217 int level = conf->level;
2218 struct raid5_percpu *percpu;
2222 percpu = per_cpu_ptr(conf->percpu, cpu);
2223 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
2224 ops_run_biofill(sh);
2228 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
2230 tx = ops_run_compute5(sh, percpu);
2232 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2233 tx = ops_run_compute6_1(sh, percpu);
2235 tx = ops_run_compute6_2(sh, percpu);
2237 /* terminate the chain if reconstruct is not set to be run */
2238 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
2242 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2244 tx = ops_run_prexor5(sh, percpu, tx);
2246 tx = ops_run_prexor6(sh, percpu, tx);
2249 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2250 tx = ops_run_partial_parity(sh, percpu, tx);
2252 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
2253 tx = ops_run_biodrain(sh, tx);
2257 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2259 ops_run_reconstruct5(sh, percpu, tx);
2261 ops_run_reconstruct6(sh, percpu, tx);
2264 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2265 if (sh->check_state == check_state_run)
2266 ops_run_check_p(sh, percpu);
2267 else if (sh->check_state == check_state_run_q)
2268 ops_run_check_pq(sh, percpu, 0);
2269 else if (sh->check_state == check_state_run_pq)
2270 ops_run_check_pq(sh, percpu, 1);
2275 if (overlap_clear && !sh->batch_head)
2276 for (i = disks; i--; ) {
2277 struct r5dev *dev = &sh->dev[i];
2278 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2279 wake_up(&sh->raid_conf->wait_for_overlap);
2284 static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2286 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2290 __free_page(sh->ppl_page);
2291 kmem_cache_free(sc, sh);
2294 static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2295 int disks, struct r5conf *conf)
2297 struct stripe_head *sh;
2300 sh = kmem_cache_zalloc(sc, gfp);
2302 spin_lock_init(&sh->stripe_lock);
2303 spin_lock_init(&sh->batch_lock);
2304 INIT_LIST_HEAD(&sh->batch_list);
2305 INIT_LIST_HEAD(&sh->lru);
2306 INIT_LIST_HEAD(&sh->r5c);
2307 INIT_LIST_HEAD(&sh->log_list);
2308 atomic_set(&sh->count, 1);
2309 sh->raid_conf = conf;
2310 sh->log_start = MaxSector;
2311 for (i = 0; i < disks; i++) {
2312 struct r5dev *dev = &sh->dev[i];
2314 bio_init(&dev->req, &dev->vec, 1);
2315 bio_init(&dev->rreq, &dev->rvec, 1);
2318 if (raid5_has_ppl(conf)) {
2319 sh->ppl_page = alloc_page(gfp);
2320 if (!sh->ppl_page) {
2321 free_stripe(sc, sh);
2325 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2326 if (init_stripe_shared_pages(sh, conf, disks)) {
2327 free_stripe(sc, sh);
2334 static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
2336 struct stripe_head *sh;
2338 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
2342 if (grow_buffers(sh, gfp)) {
2344 free_stripe(conf->slab_cache, sh);
2347 sh->hash_lock_index =
2348 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
2349 /* we just created an active stripe so... */
2350 atomic_inc(&conf->active_stripes);
2352 raid5_release_stripe(sh);
2353 conf->max_nr_stripes++;
2357 static int grow_stripes(struct r5conf *conf, int num)
2359 struct kmem_cache *sc;
2360 size_t namelen = sizeof(conf->cache_name[0]);
2361 int devs = max(conf->raid_disks, conf->previous_raid_disks);
2363 if (conf->mddev->gendisk)
2364 snprintf(conf->cache_name[0], namelen,
2365 "raid%d-%s", conf->level, mdname(conf->mddev));
2367 snprintf(conf->cache_name[0], namelen,
2368 "raid%d-%p", conf->level, conf->mddev);
2369 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
2371 conf->active_name = 0;
2372 sc = kmem_cache_create(conf->cache_name[conf->active_name],
2373 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
2377 conf->slab_cache = sc;
2378 conf->pool_size = devs;
2380 if (!grow_one_stripe(conf, GFP_KERNEL))
2387 * scribble_alloc - allocate percpu scribble buffer for required size
2388 * of the scribble region
2389 * @percpu: from for_each_present_cpu() of the caller
2390 * @num: total number of disks in the array
2391 * @cnt: scribble objs count for required size of the scribble region
2393 * The scribble buffer size must be enough to contain:
2394 * 1/ a struct page pointer for each device in the array +2
2395 * 2/ room to convert each entry in (1) to its corresponding dma
2396 * (dma_map_page()) or page (page_address()) address.
2398 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2399 * calculate over all devices (not just the data blocks), using zeros in place
2400 * of the P and Q blocks.
2402 static int scribble_alloc(struct raid5_percpu *percpu,
2406 sizeof(struct page *) * (num + 2) +
2407 sizeof(addr_conv_t) * (num + 2) +
2408 sizeof(unsigned int) * (num + 2);
2412 * If here is in raid array suspend context, it is in memalloc noio
2413 * context as well, there is no potential recursive memory reclaim
2414 * I/Os with the GFP_KERNEL flag.
2416 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
2420 kvfree(percpu->scribble);
2422 percpu->scribble = scribble;
2423 percpu->scribble_obj_size = obj_size;
2427 static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2433 * Never shrink. And mddev_suspend() could deadlock if this is called
2434 * from raid5d. In that case, scribble_disks and scribble_sectors
2435 * should equal to new_disks and new_sectors
2437 if (conf->scribble_disks >= new_disks &&
2438 conf->scribble_sectors >= new_sectors)
2440 mddev_suspend(conf->mddev);
2443 for_each_present_cpu(cpu) {
2444 struct raid5_percpu *percpu;
2446 percpu = per_cpu_ptr(conf->percpu, cpu);
2447 err = scribble_alloc(percpu, new_disks,
2448 new_sectors / RAID5_STRIPE_SECTORS(conf));
2454 mddev_resume(conf->mddev);
2456 conf->scribble_disks = new_disks;
2457 conf->scribble_sectors = new_sectors;
2462 static int resize_stripes(struct r5conf *conf, int newsize)
2464 /* Make all the stripes able to hold 'newsize' devices.
2465 * New slots in each stripe get 'page' set to a new page.
2467 * This happens in stages:
2468 * 1/ create a new kmem_cache and allocate the required number of
2470 * 2/ gather all the old stripe_heads and transfer the pages across
2471 * to the new stripe_heads. This will have the side effect of
2472 * freezing the array as once all stripe_heads have been collected,
2473 * no IO will be possible. Old stripe heads are freed once their
2474 * pages have been transferred over, and the old kmem_cache is
2475 * freed when all stripes are done.
2476 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2477 * we simple return a failure status - no need to clean anything up.
2478 * 4/ allocate new pages for the new slots in the new stripe_heads.
2479 * If this fails, we don't bother trying the shrink the
2480 * stripe_heads down again, we just leave them as they are.
2481 * As each stripe_head is processed the new one is released into
2484 * Once step2 is started, we cannot afford to wait for a write,
2485 * so we use GFP_NOIO allocations.
2487 struct stripe_head *osh, *nsh;
2488 LIST_HEAD(newstripes);
2489 struct disk_info *ndisks;
2491 struct kmem_cache *sc;
2495 md_allow_write(conf->mddev);
2498 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2499 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
2504 /* Need to ensure auto-resizing doesn't interfere */
2505 mutex_lock(&conf->cache_size_mutex);
2507 for (i = conf->max_nr_stripes; i; i--) {
2508 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
2512 list_add(&nsh->lru, &newstripes);
2515 /* didn't get enough, give up */
2516 while (!list_empty(&newstripes)) {
2517 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2518 list_del(&nsh->lru);
2519 free_stripe(sc, nsh);
2521 kmem_cache_destroy(sc);
2522 mutex_unlock(&conf->cache_size_mutex);
2525 /* Step 2 - Must use GFP_NOIO now.
2526 * OK, we have enough stripes, start collecting inactive
2527 * stripes and copying them over
2531 list_for_each_entry(nsh, &newstripes, lru) {
2532 lock_device_hash_lock(conf, hash);
2533 wait_event_cmd(conf->wait_for_stripe,
2534 !list_empty(conf->inactive_list + hash),
2535 unlock_device_hash_lock(conf, hash),
2536 lock_device_hash_lock(conf, hash));
2537 osh = get_free_stripe(conf, hash);
2538 unlock_device_hash_lock(conf, hash);
2540 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2541 for (i = 0; i < osh->nr_pages; i++) {
2542 nsh->pages[i] = osh->pages[i];
2543 osh->pages[i] = NULL;
2546 for(i=0; i<conf->pool_size; i++) {
2547 nsh->dev[i].page = osh->dev[i].page;
2548 nsh->dev[i].orig_page = osh->dev[i].page;
2549 nsh->dev[i].offset = osh->dev[i].offset;
2551 nsh->hash_lock_index = hash;
2552 free_stripe(conf->slab_cache, osh);
2554 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2555 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2560 kmem_cache_destroy(conf->slab_cache);
2563 * At this point, we are holding all the stripes so the array
2564 * is completely stalled, so now is a good time to resize
2565 * conf->disks and the scribble region
2567 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
2569 for (i = 0; i < conf->pool_size; i++)
2570 ndisks[i] = conf->disks[i];
2572 for (i = conf->pool_size; i < newsize; i++) {
2573 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2574 if (!ndisks[i].extra_page)
2579 for (i = conf->pool_size; i < newsize; i++)
2580 if (ndisks[i].extra_page)
2581 put_page(ndisks[i].extra_page);
2585 conf->disks = ndisks;
2590 conf->slab_cache = sc;
2591 conf->active_name = 1-conf->active_name;
2593 /* Step 4, return new stripes to service */
2594 while(!list_empty(&newstripes)) {
2595 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2596 list_del_init(&nsh->lru);
2598 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2599 for (i = 0; i < nsh->nr_pages; i++) {
2602 nsh->pages[i] = alloc_page(GFP_NOIO);
2607 for (i = conf->raid_disks; i < newsize; i++) {
2608 if (nsh->dev[i].page)
2610 nsh->dev[i].page = raid5_get_dev_page(nsh, i);
2611 nsh->dev[i].orig_page = nsh->dev[i].page;
2612 nsh->dev[i].offset = raid5_get_page_offset(nsh, i);
2615 for (i=conf->raid_disks; i < newsize; i++)
2616 if (nsh->dev[i].page == NULL) {
2617 struct page *p = alloc_page(GFP_NOIO);
2618 nsh->dev[i].page = p;
2619 nsh->dev[i].orig_page = p;
2620 nsh->dev[i].offset = 0;
2625 raid5_release_stripe(nsh);
2627 /* critical section pass, GFP_NOIO no longer needed */
2630 conf->pool_size = newsize;
2631 mutex_unlock(&conf->cache_size_mutex);
2636 static int drop_one_stripe(struct r5conf *conf)
2638 struct stripe_head *sh;
2639 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
2641 spin_lock_irq(conf->hash_locks + hash);
2642 sh = get_free_stripe(conf, hash);
2643 spin_unlock_irq(conf->hash_locks + hash);
2646 BUG_ON(atomic_read(&sh->count));
2648 free_stripe(conf->slab_cache, sh);
2649 atomic_dec(&conf->active_stripes);
2650 conf->max_nr_stripes--;
2654 static void shrink_stripes(struct r5conf *conf)
2656 while (conf->max_nr_stripes &&
2657 drop_one_stripe(conf))
2660 kmem_cache_destroy(conf->slab_cache);
2661 conf->slab_cache = NULL;
2664 static void raid5_end_read_request(struct bio * bi)
2666 struct stripe_head *sh = bi->bi_private;
2667 struct r5conf *conf = sh->raid_conf;
2668 int disks = sh->disks, i;
2669 char b[BDEVNAME_SIZE];
2670 struct md_rdev *rdev = NULL;
2673 for (i=0 ; i<disks; i++)
2674 if (bi == &sh->dev[i].req)
2677 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2678 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2685 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2686 /* If replacement finished while this request was outstanding,
2687 * 'replacement' might be NULL already.
2688 * In that case it moved down to 'rdev'.
2689 * rdev is not removed until all requests are finished.
2691 rdev = conf->disks[i].replacement;
2693 rdev = conf->disks[i].rdev;
2695 if (use_new_offset(conf, sh))
2696 s = sh->sector + rdev->new_data_offset;
2698 s = sh->sector + rdev->data_offset;
2699 if (!bi->bi_status) {
2700 set_bit(R5_UPTODATE, &sh->dev[i].flags);
2701 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2702 /* Note that this cannot happen on a
2703 * replacement device. We just fail those on
2706 pr_info_ratelimited(
2707 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
2708 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
2709 (unsigned long long)s,
2710 bdevname(rdev->bdev, b));
2711 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
2712 clear_bit(R5_ReadError, &sh->dev[i].flags);
2713 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2714 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2715 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2717 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2719 * end read for a page in journal, this
2720 * must be preparing for prexor in rmw
2722 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2724 if (atomic_read(&rdev->read_errors))
2725 atomic_set(&rdev->read_errors, 0);
2727 const char *bdn = bdevname(rdev->bdev, b);
2731 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
2732 if (!(bi->bi_status == BLK_STS_PROTECTION))
2733 atomic_inc(&rdev->read_errors);
2734 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2735 pr_warn_ratelimited(
2736 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
2737 mdname(conf->mddev),
2738 (unsigned long long)s,
2740 else if (conf->mddev->degraded >= conf->max_degraded) {
2742 pr_warn_ratelimited(
2743 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
2744 mdname(conf->mddev),
2745 (unsigned long long)s,
2747 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
2750 pr_warn_ratelimited(
2751 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
2752 mdname(conf->mddev),
2753 (unsigned long long)s,
2755 } else if (atomic_read(&rdev->read_errors)
2756 > conf->max_nr_stripes) {
2757 if (!test_bit(Faulty, &rdev->flags)) {
2758 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2759 mdname(conf->mddev),
2760 atomic_read(&rdev->read_errors),
2761 conf->max_nr_stripes);
2762 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2763 mdname(conf->mddev), bdn);
2767 if (set_bad && test_bit(In_sync, &rdev->flags)
2768 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2771 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2772 set_bit(R5_ReadError, &sh->dev[i].flags);
2773 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2774 set_bit(R5_ReadError, &sh->dev[i].flags);
2775 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2777 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2779 clear_bit(R5_ReadError, &sh->dev[i].flags);
2780 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2782 && test_bit(In_sync, &rdev->flags)
2783 && rdev_set_badblocks(
2784 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
2785 md_error(conf->mddev, rdev);
2788 rdev_dec_pending(rdev, conf->mddev);
2790 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2791 set_bit(STRIPE_HANDLE, &sh->state);
2792 raid5_release_stripe(sh);
2795 static void raid5_end_write_request(struct bio *bi)
2797 struct stripe_head *sh = bi->bi_private;
2798 struct r5conf *conf = sh->raid_conf;
2799 int disks = sh->disks, i;
2800 struct md_rdev *rdev;
2803 int replacement = 0;
2805 for (i = 0 ; i < disks; i++) {
2806 if (bi == &sh->dev[i].req) {
2807 rdev = conf->disks[i].rdev;
2810 if (bi == &sh->dev[i].rreq) {
2811 rdev = conf->disks[i].replacement;
2815 /* rdev was removed and 'replacement'
2816 * replaced it. rdev is not removed
2817 * until all requests are finished.
2819 rdev = conf->disks[i].rdev;
2823 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2824 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2834 md_error(conf->mddev, rdev);
2835 else if (is_badblock(rdev, sh->sector,
2836 RAID5_STRIPE_SECTORS(conf),
2837 &first_bad, &bad_sectors))
2838 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2840 if (bi->bi_status) {
2841 set_bit(STRIPE_DEGRADED, &sh->state);
2842 set_bit(WriteErrorSeen, &rdev->flags);
2843 set_bit(R5_WriteError, &sh->dev[i].flags);
2844 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2845 set_bit(MD_RECOVERY_NEEDED,
2846 &rdev->mddev->recovery);
2847 } else if (is_badblock(rdev, sh->sector,
2848 RAID5_STRIPE_SECTORS(conf),
2849 &first_bad, &bad_sectors)) {
2850 set_bit(R5_MadeGood, &sh->dev[i].flags);
2851 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2852 /* That was a successful write so make
2853 * sure it looks like we already did
2856 set_bit(R5_ReWrite, &sh->dev[i].flags);
2859 rdev_dec_pending(rdev, conf->mddev);
2861 if (sh->batch_head && bi->bi_status && !replacement)
2862 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2865 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2866 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2867 set_bit(STRIPE_HANDLE, &sh->state);
2869 if (sh->batch_head && sh != sh->batch_head)
2870 raid5_release_stripe(sh->batch_head);
2871 raid5_release_stripe(sh);
2874 static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
2876 char b[BDEVNAME_SIZE];
2877 struct r5conf *conf = mddev->private;
2878 unsigned long flags;
2879 pr_debug("raid456: error called\n");
2881 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n",
2882 mdname(mddev), bdevname(rdev->bdev, b));
2884 spin_lock_irqsave(&conf->device_lock, flags);
2885 set_bit(Faulty, &rdev->flags);
2886 clear_bit(In_sync, &rdev->flags);
2887 mddev->degraded = raid5_calc_degraded(conf);
2889 if (has_failed(conf)) {
2890 set_bit(MD_BROKEN, &conf->mddev->flags);
2891 conf->recovery_disabled = mddev->recovery_disabled;
2893 pr_crit("md/raid:%s: Cannot continue operation (%d/%d failed).\n",
2894 mdname(mddev), mddev->degraded, conf->raid_disks);
2896 pr_crit("md/raid:%s: Operation continuing on %d devices.\n",
2897 mdname(mddev), conf->raid_disks - mddev->degraded);
2900 spin_unlock_irqrestore(&conf->device_lock, flags);
2901 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2903 set_bit(Blocked, &rdev->flags);
2904 set_mask_bits(&mddev->sb_flags, 0,
2905 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2906 r5c_update_on_rdev_error(mddev, rdev);
2910 * Input: a 'big' sector number,
2911 * Output: index of the data and parity disk, and the sector # in them.
2913 sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2914 int previous, int *dd_idx,
2915 struct stripe_head *sh)
2917 sector_t stripe, stripe2;
2918 sector_t chunk_number;
2919 unsigned int chunk_offset;
2922 sector_t new_sector;
2923 int algorithm = previous ? conf->prev_algo
2925 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2926 : conf->chunk_sectors;
2927 int raid_disks = previous ? conf->previous_raid_disks
2929 int data_disks = raid_disks - conf->max_degraded;
2931 /* First compute the information on this sector */
2934 * Compute the chunk number and the sector offset inside the chunk
2936 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2937 chunk_number = r_sector;
2940 * Compute the stripe number
2942 stripe = chunk_number;
2943 *dd_idx = sector_div(stripe, data_disks);
2946 * Select the parity disk based on the user selected algorithm.
2948 pd_idx = qd_idx = -1;
2949 switch(conf->level) {
2951 pd_idx = data_disks;
2954 switch (algorithm) {
2955 case ALGORITHM_LEFT_ASYMMETRIC:
2956 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2957 if (*dd_idx >= pd_idx)
2960 case ALGORITHM_RIGHT_ASYMMETRIC:
2961 pd_idx = sector_div(stripe2, raid_disks);
2962 if (*dd_idx >= pd_idx)
2965 case ALGORITHM_LEFT_SYMMETRIC:
2966 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2967 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2969 case ALGORITHM_RIGHT_SYMMETRIC:
2970 pd_idx = sector_div(stripe2, raid_disks);
2971 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2973 case ALGORITHM_PARITY_0:
2977 case ALGORITHM_PARITY_N:
2978 pd_idx = data_disks;
2986 switch (algorithm) {
2987 case ALGORITHM_LEFT_ASYMMETRIC:
2988 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2989 qd_idx = pd_idx + 1;
2990 if (pd_idx == raid_disks-1) {
2991 (*dd_idx)++; /* Q D D D P */
2993 } else if (*dd_idx >= pd_idx)
2994 (*dd_idx) += 2; /* D D P Q D */
2996 case ALGORITHM_RIGHT_ASYMMETRIC:
2997 pd_idx = sector_div(stripe2, raid_disks);
2998 qd_idx = pd_idx + 1;
2999 if (pd_idx == raid_disks-1) {
3000 (*dd_idx)++; /* Q D D D P */
3002 } else if (*dd_idx >= pd_idx)
3003 (*dd_idx) += 2; /* D D P Q D */
3005 case ALGORITHM_LEFT_SYMMETRIC:
3006 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3007 qd_idx = (pd_idx + 1) % raid_disks;
3008 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
3010 case ALGORITHM_RIGHT_SYMMETRIC:
3011 pd_idx = sector_div(stripe2, raid_disks);
3012 qd_idx = (pd_idx + 1) % raid_disks;
3013 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
3016 case ALGORITHM_PARITY_0:
3021 case ALGORITHM_PARITY_N:
3022 pd_idx = data_disks;
3023 qd_idx = data_disks + 1;
3026 case ALGORITHM_ROTATING_ZERO_RESTART:
3027 /* Exactly the same as RIGHT_ASYMMETRIC, but or
3028 * of blocks for computing Q is different.
3030 pd_idx = sector_div(stripe2, raid_disks);
3031 qd_idx = pd_idx + 1;
3032 if (pd_idx == raid_disks-1) {
3033 (*dd_idx)++; /* Q D D D P */
3035 } else if (*dd_idx >= pd_idx)
3036 (*dd_idx) += 2; /* D D P Q D */
3040 case ALGORITHM_ROTATING_N_RESTART:
3041 /* Same a left_asymmetric, by first stripe is
3042 * D D D P Q rather than
3046 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3047 qd_idx = pd_idx + 1;
3048 if (pd_idx == raid_disks-1) {
3049 (*dd_idx)++; /* Q D D D P */
3051 } else if (*dd_idx >= pd_idx)
3052 (*dd_idx) += 2; /* D D P Q D */
3056 case ALGORITHM_ROTATING_N_CONTINUE:
3057 /* Same as left_symmetric but Q is before P */
3058 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3059 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
3060 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
3064 case ALGORITHM_LEFT_ASYMMETRIC_6:
3065 /* RAID5 left_asymmetric, with Q on last device */
3066 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
3067 if (*dd_idx >= pd_idx)
3069 qd_idx = raid_disks - 1;
3072 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3073 pd_idx = sector_div(stripe2, raid_disks-1);
3074 if (*dd_idx >= pd_idx)
3076 qd_idx = raid_disks - 1;
3079 case ALGORITHM_LEFT_SYMMETRIC_6:
3080 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
3081 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3082 qd_idx = raid_disks - 1;
3085 case ALGORITHM_RIGHT_SYMMETRIC_6:
3086 pd_idx = sector_div(stripe2, raid_disks-1);
3087 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3088 qd_idx = raid_disks - 1;
3091 case ALGORITHM_PARITY_0_6:
3094 qd_idx = raid_disks - 1;
3104 sh->pd_idx = pd_idx;
3105 sh->qd_idx = qd_idx;
3106 sh->ddf_layout = ddf_layout;
3109 * Finally, compute the new sector number
3111 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
3115 sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
3117 struct r5conf *conf = sh->raid_conf;
3118 int raid_disks = sh->disks;
3119 int data_disks = raid_disks - conf->max_degraded;
3120 sector_t new_sector = sh->sector, check;
3121 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
3122 : conf->chunk_sectors;
3123 int algorithm = previous ? conf->prev_algo
3127 sector_t chunk_number;
3128 int dummy1, dd_idx = i;
3130 struct stripe_head sh2;
3132 chunk_offset = sector_div(new_sector, sectors_per_chunk);
3133 stripe = new_sector;
3135 if (i == sh->pd_idx)
3137 switch(conf->level) {
3140 switch (algorithm) {
3141 case ALGORITHM_LEFT_ASYMMETRIC:
3142 case ALGORITHM_RIGHT_ASYMMETRIC:
3146 case ALGORITHM_LEFT_SYMMETRIC:
3147 case ALGORITHM_RIGHT_SYMMETRIC:
3150 i -= (sh->pd_idx + 1);
3152 case ALGORITHM_PARITY_0:
3155 case ALGORITHM_PARITY_N:
3162 if (i == sh->qd_idx)
3163 return 0; /* It is the Q disk */
3164 switch (algorithm) {
3165 case ALGORITHM_LEFT_ASYMMETRIC:
3166 case ALGORITHM_RIGHT_ASYMMETRIC:
3167 case ALGORITHM_ROTATING_ZERO_RESTART:
3168 case ALGORITHM_ROTATING_N_RESTART:
3169 if (sh->pd_idx == raid_disks-1)
3170 i--; /* Q D D D P */
3171 else if (i > sh->pd_idx)
3172 i -= 2; /* D D P Q D */
3174 case ALGORITHM_LEFT_SYMMETRIC:
3175 case ALGORITHM_RIGHT_SYMMETRIC:
3176 if (sh->pd_idx == raid_disks-1)
3177 i--; /* Q D D D P */
3182 i -= (sh->pd_idx + 2);
3185 case ALGORITHM_PARITY_0:
3188 case ALGORITHM_PARITY_N:
3190 case ALGORITHM_ROTATING_N_CONTINUE:
3191 /* Like left_symmetric, but P is before Q */
3192 if (sh->pd_idx == 0)
3193 i--; /* P D D D Q */
3198 i -= (sh->pd_idx + 1);
3201 case ALGORITHM_LEFT_ASYMMETRIC_6:
3202 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3206 case ALGORITHM_LEFT_SYMMETRIC_6:
3207 case ALGORITHM_RIGHT_SYMMETRIC_6:
3209 i += data_disks + 1;
3210 i -= (sh->pd_idx + 1);
3212 case ALGORITHM_PARITY_0_6:
3221 chunk_number = stripe * data_disks + i;
3222 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
3224 check = raid5_compute_sector(conf, r_sector,
3225 previous, &dummy1, &sh2);
3226 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3227 || sh2.qd_idx != sh->qd_idx) {
3228 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3229 mdname(conf->mddev));
3236 * There are cases where we want handle_stripe_dirtying() and
3237 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3239 * This function checks whether we want to delay the towrite. Specifically,
3240 * we delay the towrite when:
3242 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3243 * stripe has data in journal (for other devices).
3245 * In this case, when reading data for the non-overwrite dev, it is
3246 * necessary to handle complex rmw of write back cache (prexor with
3247 * orig_page, and xor with page). To keep read path simple, we would
3248 * like to flush data in journal to RAID disks first, so complex rmw
3249 * is handled in the write patch (handle_stripe_dirtying).
3251 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3253 * It is important to be able to flush all stripes in raid5-cache.
3254 * Therefore, we need reserve some space on the journal device for
3255 * these flushes. If flush operation includes pending writes to the
3256 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3257 * for the flush out. If we exclude these pending writes from flush
3258 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3259 * Therefore, excluding pending writes in these cases enables more
3260 * efficient use of the journal device.
3262 * Note: To make sure the stripe makes progress, we only delay
3263 * towrite for stripes with data already in journal (injournal > 0).
3264 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3265 * no_space_stripes list.
3267 * 3. during journal failure
3268 * In journal failure, we try to flush all cached data to raid disks
3269 * based on data in stripe cache. The array is read-only to upper
3270 * layers, so we would skip all pending writes.
3273 static inline bool delay_towrite(struct r5conf *conf,
3275 struct stripe_head_state *s)
3278 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3279 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3282 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3286 if (s->log_failed && s->injournal)
3292 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
3293 int rcw, int expand)
3295 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
3296 struct r5conf *conf = sh->raid_conf;
3297 int level = conf->level;
3301 * In some cases, handle_stripe_dirtying initially decided to
3302 * run rmw and allocates extra page for prexor. However, rcw is
3303 * cheaper later on. We need to free the extra page now,
3304 * because we won't be able to do that in ops_complete_prexor().
3306 r5c_release_extra_page(sh);
3308 for (i = disks; i--; ) {
3309 struct r5dev *dev = &sh->dev[i];
3311 if (dev->towrite && !delay_towrite(conf, dev, s)) {
3312 set_bit(R5_LOCKED, &dev->flags);
3313 set_bit(R5_Wantdrain, &dev->flags);
3315 clear_bit(R5_UPTODATE, &dev->flags);
3317 } else if (test_bit(R5_InJournal, &dev->flags)) {
3318 set_bit(R5_LOCKED, &dev->flags);
3322 /* if we are not expanding this is a proper write request, and
3323 * there will be bios with new data to be drained into the
3328 /* False alarm, nothing to do */
3330 sh->reconstruct_state = reconstruct_state_drain_run;
3331 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3333 sh->reconstruct_state = reconstruct_state_run;
3335 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3337 if (s->locked + conf->max_degraded == disks)
3338 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
3339 atomic_inc(&conf->pending_full_writes);
3341 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3342 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
3343 BUG_ON(level == 6 &&
3344 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3345 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
3347 for (i = disks; i--; ) {
3348 struct r5dev *dev = &sh->dev[i];
3349 if (i == pd_idx || i == qd_idx)
3353 (test_bit(R5_UPTODATE, &dev->flags) ||
3354 test_bit(R5_Wantcompute, &dev->flags))) {
3355 set_bit(R5_Wantdrain, &dev->flags);
3356 set_bit(R5_LOCKED, &dev->flags);
3357 clear_bit(R5_UPTODATE, &dev->flags);
3359 } else if (test_bit(R5_InJournal, &dev->flags)) {
3360 set_bit(R5_LOCKED, &dev->flags);
3365 /* False alarm - nothing to do */
3367 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3368 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3369 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3370 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3373 /* keep the parity disk(s) locked while asynchronous operations
3376 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3377 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3381 int qd_idx = sh->qd_idx;
3382 struct r5dev *dev = &sh->dev[qd_idx];
3384 set_bit(R5_LOCKED, &dev->flags);
3385 clear_bit(R5_UPTODATE, &dev->flags);
3389 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
3390 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3391 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3392 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3393 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3395 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
3396 __func__, (unsigned long long)sh->sector,
3397 s->locked, s->ops_request);
3401 * Each stripe/dev can have one or more bion attached.
3402 * toread/towrite point to the first in a chain.
3403 * The bi_next chain must be in order.
3405 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3406 int forwrite, int previous)
3409 struct r5conf *conf = sh->raid_conf;
3412 pr_debug("adding bi b#%llu to stripe s#%llu\n",
3413 (unsigned long long)bi->bi_iter.bi_sector,
3414 (unsigned long long)sh->sector);
3416 spin_lock_irq(&sh->stripe_lock);
3417 sh->dev[dd_idx].write_hint = bi->bi_write_hint;
3418 /* Don't allow new IO added to stripes in batch list */
3422 bip = &sh->dev[dd_idx].towrite;
3426 bip = &sh->dev[dd_idx].toread;
3427 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3428 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
3430 bip = & (*bip)->bi_next;
3432 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
3435 if (forwrite && raid5_has_ppl(conf)) {
3437 * With PPL only writes to consecutive data chunks within a
3438 * stripe are allowed because for a single stripe_head we can
3439 * only have one PPL entry at a time, which describes one data
3440 * range. Not really an overlap, but wait_for_overlap can be
3441 * used to handle this.
3449 for (i = 0; i < sh->disks; i++) {
3450 if (i != sh->pd_idx &&
3451 (i == dd_idx || sh->dev[i].towrite)) {
3452 sector = sh->dev[i].sector;
3453 if (count == 0 || sector < first)
3461 if (first + conf->chunk_sectors * (count - 1) != last)
3465 if (!forwrite || previous)
3466 clear_bit(STRIPE_BATCH_READY, &sh->state);
3468 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
3472 bio_inc_remaining(bi);
3473 md_write_inc(conf->mddev, bi);
3476 /* check if page is covered */
3477 sector_t sector = sh->dev[dd_idx].sector;
3478 for (bi=sh->dev[dd_idx].towrite;
3479 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
3480 bi && bi->bi_iter.bi_sector <= sector;
3481 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
3482 if (bio_end_sector(bi) >= sector)
3483 sector = bio_end_sector(bi);
3485 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
3486 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3487 sh->overwrite_disks++;
3490 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
3491 (unsigned long long)(*bip)->bi_iter.bi_sector,
3492 (unsigned long long)sh->sector, dd_idx);
3494 if (conf->mddev->bitmap && firstwrite) {
3495 /* Cannot hold spinlock over bitmap_startwrite,
3496 * but must ensure this isn't added to a batch until
3497 * we have added to the bitmap and set bm_seq.
3498 * So set STRIPE_BITMAP_PENDING to prevent
3500 * If multiple add_stripe_bio() calls race here they
3501 * much all set STRIPE_BITMAP_PENDING. So only the first one
3502 * to complete "bitmap_startwrite" gets to set
3503 * STRIPE_BIT_DELAY. This is important as once a stripe
3504 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3507 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3508 spin_unlock_irq(&sh->stripe_lock);
3509 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3510 RAID5_STRIPE_SECTORS(conf), 0);
3511 spin_lock_irq(&sh->stripe_lock);
3512 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3513 if (!sh->batch_head) {
3514 sh->bm_seq = conf->seq_flush+1;
3515 set_bit(STRIPE_BIT_DELAY, &sh->state);
3518 spin_unlock_irq(&sh->stripe_lock);
3520 if (stripe_can_batch(sh))
3521 stripe_add_to_batch_list(conf, sh);
3525 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
3526 spin_unlock_irq(&sh->stripe_lock);
3530 static void end_reshape(struct r5conf *conf);
3532 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
3533 struct stripe_head *sh)
3535 int sectors_per_chunk =
3536 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
3538 int chunk_offset = sector_div(stripe, sectors_per_chunk);
3539 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
3541 raid5_compute_sector(conf,
3542 stripe * (disks - conf->max_degraded)
3543 *sectors_per_chunk + chunk_offset,
3549 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
3550 struct stripe_head_state *s, int disks)
3553 BUG_ON(sh->batch_head);
3554 for (i = disks; i--; ) {
3558 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
3559 struct md_rdev *rdev;
3561 rdev = rcu_dereference(conf->disks[i].rdev);
3562 if (rdev && test_bit(In_sync, &rdev->flags) &&
3563 !test_bit(Faulty, &rdev->flags))
3564 atomic_inc(&rdev->nr_pending);
3569 if (!rdev_set_badblocks(
3572 RAID5_STRIPE_SECTORS(conf), 0))
3573 md_error(conf->mddev, rdev);
3574 rdev_dec_pending(rdev, conf->mddev);
3577 spin_lock_irq(&sh->stripe_lock);
3578 /* fail all writes first */
3579 bi = sh->dev[i].towrite;
3580 sh->dev[i].towrite = NULL;
3581 sh->overwrite_disks = 0;
3582 spin_unlock_irq(&sh->stripe_lock);
3586 log_stripe_write_finished(sh);
3588 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3589 wake_up(&conf->wait_for_overlap);
3591 while (bi && bi->bi_iter.bi_sector <
3592 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3593 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
3595 md_write_end(conf->mddev);
3600 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3601 RAID5_STRIPE_SECTORS(conf), 0, 0);
3603 /* and fail all 'written' */
3604 bi = sh->dev[i].written;
3605 sh->dev[i].written = NULL;
3606 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3607 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3608 sh->dev[i].page = sh->dev[i].orig_page;
3611 if (bi) bitmap_end = 1;
3612 while (bi && bi->bi_iter.bi_sector <
3613 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3614 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
3616 md_write_end(conf->mddev);
3621 /* fail any reads if this device is non-operational and
3622 * the data has not reached the cache yet.
3624 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
3625 s->failed > conf->max_degraded &&
3626 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3627 test_bit(R5_ReadError, &sh->dev[i].flags))) {
3628 spin_lock_irq(&sh->stripe_lock);
3629 bi = sh->dev[i].toread;
3630 sh->dev[i].toread = NULL;
3631 spin_unlock_irq(&sh->stripe_lock);
3632 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3633 wake_up(&conf->wait_for_overlap);
3636 while (bi && bi->bi_iter.bi_sector <
3637 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3638 struct bio *nextbi =
3639 r5_next_bio(conf, bi, sh->dev[i].sector);
3646 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3647 RAID5_STRIPE_SECTORS(conf), 0, 0);
3648 /* If we were in the middle of a write the parity block might
3649 * still be locked - so just clear all R5_LOCKED flags
3651 clear_bit(R5_LOCKED, &sh->dev[i].flags);
3656 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3657 if (atomic_dec_and_test(&conf->pending_full_writes))
3658 md_wakeup_thread(conf->mddev->thread);
3662 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
3663 struct stripe_head_state *s)
3668 BUG_ON(sh->batch_head);
3669 clear_bit(STRIPE_SYNCING, &sh->state);
3670 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3671 wake_up(&conf->wait_for_overlap);
3674 /* There is nothing more to do for sync/check/repair.
3675 * Don't even need to abort as that is handled elsewhere
3676 * if needed, and not always wanted e.g. if there is a known
3678 * For recover/replace we need to record a bad block on all
3679 * non-sync devices, or abort the recovery
3681 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3682 /* During recovery devices cannot be removed, so
3683 * locking and refcounting of rdevs is not needed
3686 for (i = 0; i < conf->raid_disks; i++) {
3687 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
3689 && !test_bit(Faulty, &rdev->flags)
3690 && !test_bit(In_sync, &rdev->flags)
3691 && !rdev_set_badblocks(rdev, sh->sector,
3692 RAID5_STRIPE_SECTORS(conf), 0))
3694 rdev = rcu_dereference(conf->disks[i].replacement);
3696 && !test_bit(Faulty, &rdev->flags)
3697 && !test_bit(In_sync, &rdev->flags)
3698 && !rdev_set_badblocks(rdev, sh->sector,
3699 RAID5_STRIPE_SECTORS(conf), 0))
3704 conf->recovery_disabled =
3705 conf->mddev->recovery_disabled;
3707 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
3710 static int want_replace(struct stripe_head *sh, int disk_idx)
3712 struct md_rdev *rdev;
3716 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
3718 && !test_bit(Faulty, &rdev->flags)
3719 && !test_bit(In_sync, &rdev->flags)
3720 && (rdev->recovery_offset <= sh->sector
3721 || rdev->mddev->recovery_cp <= sh->sector))
3727 static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3728 int disk_idx, int disks)
3730 struct r5dev *dev = &sh->dev[disk_idx];
3731 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3732 &sh->dev[s->failed_num[1]] };
3734 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
3737 if (test_bit(R5_LOCKED, &dev->flags) ||
3738 test_bit(R5_UPTODATE, &dev->flags))
3739 /* No point reading this as we already have it or have
3740 * decided to get it.
3745 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3746 /* We need this block to directly satisfy a request */
3749 if (s->syncing || s->expanding ||
3750 (s->replacing && want_replace(sh, disk_idx)))
3751 /* When syncing, or expanding we read everything.
3752 * When replacing, we need the replaced block.
3756 if ((s->failed >= 1 && fdev[0]->toread) ||
3757 (s->failed >= 2 && fdev[1]->toread))
3758 /* If we want to read from a failed device, then
3759 * we need to actually read every other device.
3763 /* Sometimes neither read-modify-write nor reconstruct-write
3764 * cycles can work. In those cases we read every block we
3765 * can. Then the parity-update is certain to have enough to
3767 * This can only be a problem when we need to write something,
3768 * and some device has failed. If either of those tests
3769 * fail we need look no further.
3771 if (!s->failed || !s->to_write)
3774 if (test_bit(R5_Insync, &dev->flags) &&
3775 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3776 /* Pre-reads at not permitted until after short delay
3777 * to gather multiple requests. However if this
3778 * device is no Insync, the block could only be computed
3779 * and there is no need to delay that.
3783 for (i = 0; i < s->failed && i < 2; i++) {
3784 if (fdev[i]->towrite &&
3785 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3786 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3787 /* If we have a partial write to a failed
3788 * device, then we will need to reconstruct
3789 * the content of that device, so all other
3790 * devices must be read.
3794 if (s->failed >= 2 &&
3795 (fdev[i]->towrite ||
3796 s->failed_num[i] == sh->pd_idx ||
3797 s->failed_num[i] == sh->qd_idx) &&
3798 !test_bit(R5_UPTODATE, &fdev[i]->flags))
3799 /* In max degraded raid6, If the failed disk is P, Q,
3800 * or we want to read the failed disk, we need to do
3801 * reconstruct-write.
3806 /* If we are forced to do a reconstruct-write, because parity
3807 * cannot be trusted and we are currently recovering it, there
3808 * is extra need to be careful.
3809 * If one of the devices that we would need to read, because
3810 * it is not being overwritten (and maybe not written at all)
3811 * is missing/faulty, then we need to read everything we can.
3814 sh->sector < sh->raid_conf->mddev->recovery_cp)
3815 /* reconstruct-write isn't being forced */
3817 for (i = 0; i < s->failed && i < 2; i++) {
3818 if (s->failed_num[i] != sh->pd_idx &&
3819 s->failed_num[i] != sh->qd_idx &&
3820 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3821 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3828 /* fetch_block - checks the given member device to see if its data needs
3829 * to be read or computed to satisfy a request.
3831 * Returns 1 when no more member devices need to be checked, otherwise returns
3832 * 0 to tell the loop in handle_stripe_fill to continue
3834 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3835 int disk_idx, int disks)
3837 struct r5dev *dev = &sh->dev[disk_idx];
3839 /* is the data in this block needed, and can we get it? */
3840 if (need_this_block(sh, s, disk_idx, disks)) {
3841 /* we would like to get this block, possibly by computing it,
3842 * otherwise read it if the backing disk is insync
3844 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3845 BUG_ON(test_bit(R5_Wantread, &dev->flags));
3846 BUG_ON(sh->batch_head);
3849 * In the raid6 case if the only non-uptodate disk is P
3850 * then we already trusted P to compute the other failed
3851 * drives. It is safe to compute rather than re-read P.
3852 * In other cases we only compute blocks from failed
3853 * devices, otherwise check/repair might fail to detect
3854 * a real inconsistency.
3857 if ((s->uptodate == disks - 1) &&
3858 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
3859 (s->failed && (disk_idx == s->failed_num[0] ||
3860 disk_idx == s->failed_num[1])))) {
3861 /* have disk failed, and we're requested to fetch it;
3864 pr_debug("Computing stripe %llu block %d\n",
3865 (unsigned long long)sh->sector, disk_idx);
3866 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3867 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3868 set_bit(R5_Wantcompute, &dev->flags);
3869 sh->ops.target = disk_idx;
3870 sh->ops.target2 = -1; /* no 2nd target */
3872 /* Careful: from this point on 'uptodate' is in the eye
3873 * of raid_run_ops which services 'compute' operations
3874 * before writes. R5_Wantcompute flags a block that will
3875 * be R5_UPTODATE by the time it is needed for a
3876 * subsequent operation.
3880 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3881 /* Computing 2-failure is *very* expensive; only
3882 * do it if failed >= 2
3885 for (other = disks; other--; ) {
3886 if (other == disk_idx)
3888 if (!test_bit(R5_UPTODATE,
3889 &sh->dev[other].flags))
3893 pr_debug("Computing stripe %llu blocks %d,%d\n",
3894 (unsigned long long)sh->sector,
3896 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3897 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3898 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3899 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3900 sh->ops.target = disk_idx;
3901 sh->ops.target2 = other;
3905 } else if (test_bit(R5_Insync, &dev->flags)) {
3906 set_bit(R5_LOCKED, &dev->flags);
3907 set_bit(R5_Wantread, &dev->flags);
3909 pr_debug("Reading block %d (sync=%d)\n",
3910 disk_idx, s->syncing);
3918 * handle_stripe_fill - read or compute data to satisfy pending requests.
3920 static void handle_stripe_fill(struct stripe_head *sh,
3921 struct stripe_head_state *s,
3926 /* look for blocks to read/compute, skip this if a compute
3927 * is already in flight, or if the stripe contents are in the
3928 * midst of changing due to a write
3930 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3931 !sh->reconstruct_state) {
3934 * For degraded stripe with data in journal, do not handle
3935 * read requests yet, instead, flush the stripe to raid
3936 * disks first, this avoids handling complex rmw of write
3937 * back cache (prexor with orig_page, and then xor with
3938 * page) in the read path
3940 if (s->to_read && s->injournal && s->failed) {
3941 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3942 r5c_make_stripe_write_out(sh);
3946 for (i = disks; i--; )
3947 if (fetch_block(sh, s, i, disks))
3951 set_bit(STRIPE_HANDLE, &sh->state);
3954 static void break_stripe_batch_list(struct stripe_head *head_sh,
3955 unsigned long handle_flags);
3956 /* handle_stripe_clean_event
3957 * any written block on an uptodate or failed drive can be returned.
3958 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3959 * never LOCKED, so we don't need to test 'failed' directly.
3961 static void handle_stripe_clean_event(struct r5conf *conf,
3962 struct stripe_head *sh, int disks)
3966 int discard_pending = 0;
3967 struct stripe_head *head_sh = sh;
3968 bool do_endio = false;
3970 for (i = disks; i--; )
3971 if (sh->dev[i].written) {
3973 if (!test_bit(R5_LOCKED, &dev->flags) &&
3974 (test_bit(R5_UPTODATE, &dev->flags) ||
3975 test_bit(R5_Discard, &dev->flags) ||
3976 test_bit(R5_SkipCopy, &dev->flags))) {
3977 /* We can return any write requests */
3978 struct bio *wbi, *wbi2;
3979 pr_debug("Return write for disc %d\n", i);
3980 if (test_and_clear_bit(R5_Discard, &dev->flags))
3981 clear_bit(R5_UPTODATE, &dev->flags);
3982 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3983 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
3988 dev->page = dev->orig_page;
3990 dev->written = NULL;
3991 while (wbi && wbi->bi_iter.bi_sector <
3992 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
3993 wbi2 = r5_next_bio(conf, wbi, dev->sector);
3994 md_write_end(conf->mddev);
3998 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3999 RAID5_STRIPE_SECTORS(conf),
4000 !test_bit(STRIPE_DEGRADED, &sh->state),
4002 if (head_sh->batch_head) {
4003 sh = list_first_entry(&sh->batch_list,
4006 if (sh != head_sh) {
4013 } else if (test_bit(R5_Discard, &dev->flags))
4014 discard_pending = 1;
4017 log_stripe_write_finished(sh);
4019 if (!discard_pending &&
4020 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
4022 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
4023 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4024 if (sh->qd_idx >= 0) {
4025 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
4026 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
4028 /* now that discard is done we can proceed with any sync */
4029 clear_bit(STRIPE_DISCARD, &sh->state);
4031 * SCSI discard will change some bio fields and the stripe has
4032 * no updated data, so remove it from hash list and the stripe
4033 * will be reinitialized
4036 hash = sh->hash_lock_index;
4037 spin_lock_irq(conf->hash_locks + hash);
4039 spin_unlock_irq(conf->hash_locks + hash);
4040 if (head_sh->batch_head) {
4041 sh = list_first_entry(&sh->batch_list,
4042 struct stripe_head, batch_list);
4048 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
4049 set_bit(STRIPE_HANDLE, &sh->state);
4053 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
4054 if (atomic_dec_and_test(&conf->pending_full_writes))
4055 md_wakeup_thread(conf->mddev->thread);
4057 if (head_sh->batch_head && do_endio)
4058 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
4062 * For RMW in write back cache, we need extra page in prexor to store the
4063 * old data. This page is stored in dev->orig_page.
4065 * This function checks whether we have data for prexor. The exact logic
4067 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
4069 static inline bool uptodate_for_rmw(struct r5dev *dev)
4071 return (test_bit(R5_UPTODATE, &dev->flags)) &&
4072 (!test_bit(R5_InJournal, &dev->flags) ||
4073 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
4076 static int handle_stripe_dirtying(struct r5conf *conf,
4077 struct stripe_head *sh,
4078 struct stripe_head_state *s,
4081 int rmw = 0, rcw = 0, i;
4082 sector_t recovery_cp = conf->mddev->recovery_cp;
4084 /* Check whether resync is now happening or should start.
4085 * If yes, then the array is dirty (after unclean shutdown or
4086 * initial creation), so parity in some stripes might be inconsistent.
4087 * In this case, we need to always do reconstruct-write, to ensure
4088 * that in case of drive failure or read-error correction, we
4089 * generate correct data from the parity.
4091 if (conf->rmw_level == PARITY_DISABLE_RMW ||
4092 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
4094 /* Calculate the real rcw later - for now make it
4095 * look like rcw is cheaper
4098 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
4099 conf->rmw_level, (unsigned long long)recovery_cp,
4100 (unsigned long long)sh->sector);
4101 } else for (i = disks; i--; ) {
4102 /* would I have to read this buffer for read_modify_write */
4103 struct r5dev *dev = &sh->dev[i];
4104 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
4105 i == sh->pd_idx || i == sh->qd_idx ||
4106 test_bit(R5_InJournal, &dev->flags)) &&
4107 !test_bit(R5_LOCKED, &dev->flags) &&
4108 !(uptodate_for_rmw(dev) ||
4109 test_bit(R5_Wantcompute, &dev->flags))) {
4110 if (test_bit(R5_Insync, &dev->flags))
4113 rmw += 2*disks; /* cannot read it */
4115 /* Would I have to read this buffer for reconstruct_write */
4116 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4117 i != sh->pd_idx && i != sh->qd_idx &&
4118 !test_bit(R5_LOCKED, &dev->flags) &&
4119 !(test_bit(R5_UPTODATE, &dev->flags) ||
4120 test_bit(R5_Wantcompute, &dev->flags))) {
4121 if (test_bit(R5_Insync, &dev->flags))
4128 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
4129 (unsigned long long)sh->sector, sh->state, rmw, rcw);
4130 set_bit(STRIPE_HANDLE, &sh->state);
4131 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
4132 /* prefer read-modify-write, but need to get some data */
4133 if (conf->mddev->queue)
4134 blk_add_trace_msg(conf->mddev->queue,
4135 "raid5 rmw %llu %d",
4136 (unsigned long long)sh->sector, rmw);
4137 for (i = disks; i--; ) {
4138 struct r5dev *dev = &sh->dev[i];
4139 if (test_bit(R5_InJournal, &dev->flags) &&
4140 dev->page == dev->orig_page &&
4141 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
4142 /* alloc page for prexor */
4143 struct page *p = alloc_page(GFP_NOIO);
4151 * alloc_page() failed, try use
4152 * disk_info->extra_page
4154 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
4155 &conf->cache_state)) {
4156 r5c_use_extra_page(sh);
4160 /* extra_page in use, add to delayed_list */
4161 set_bit(STRIPE_DELAYED, &sh->state);
4162 s->waiting_extra_page = 1;
4167 for (i = disks; i--; ) {
4168 struct r5dev *dev = &sh->dev[i];
4169 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
4170 i == sh->pd_idx || i == sh->qd_idx ||
4171 test_bit(R5_InJournal, &dev->flags)) &&
4172 !test_bit(R5_LOCKED, &dev->flags) &&
4173 !(uptodate_for_rmw(dev) ||
4174 test_bit(R5_Wantcompute, &dev->flags)) &&
4175 test_bit(R5_Insync, &dev->flags)) {
4176 if (test_bit(STRIPE_PREREAD_ACTIVE,
4178 pr_debug("Read_old block %d for r-m-w\n",
4180 set_bit(R5_LOCKED, &dev->flags);
4181 set_bit(R5_Wantread, &dev->flags);
4184 set_bit(STRIPE_DELAYED, &sh->state);
4188 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
4189 /* want reconstruct write, but need to get some data */
4192 for (i = disks; i--; ) {
4193 struct r5dev *dev = &sh->dev[i];
4194 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4195 i != sh->pd_idx && i != sh->qd_idx &&
4196 !test_bit(R5_LOCKED, &dev->flags) &&
4197 !(test_bit(R5_UPTODATE, &dev->flags) ||
4198 test_bit(R5_Wantcompute, &dev->flags))) {
4200 if (test_bit(R5_Insync, &dev->flags) &&
4201 test_bit(STRIPE_PREREAD_ACTIVE,
4203 pr_debug("Read_old block "
4204 "%d for Reconstruct\n", i);
4205 set_bit(R5_LOCKED, &dev->flags);
4206 set_bit(R5_Wantread, &dev->flags);
4210 set_bit(STRIPE_DELAYED, &sh->state);
4213 if (rcw && conf->mddev->queue)
4214 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4215 (unsigned long long)sh->sector,
4216 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
4219 if (rcw > disks && rmw > disks &&
4220 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4221 set_bit(STRIPE_DELAYED, &sh->state);
4223 /* now if nothing is locked, and if we have enough data,
4224 * we can start a write request
4226 /* since handle_stripe can be called at any time we need to handle the
4227 * case where a compute block operation has been submitted and then a
4228 * subsequent call wants to start a write request. raid_run_ops only
4229 * handles the case where compute block and reconstruct are requested
4230 * simultaneously. If this is not the case then new writes need to be
4231 * held off until the compute completes.
4233 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4234 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
4235 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
4236 schedule_reconstruction(sh, s, rcw == 0, 0);
4240 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
4241 struct stripe_head_state *s, int disks)
4243 struct r5dev *dev = NULL;
4245 BUG_ON(sh->batch_head);
4246 set_bit(STRIPE_HANDLE, &sh->state);
4248 switch (sh->check_state) {
4249 case check_state_idle:
4250 /* start a new check operation if there are no failures */
4251 if (s->failed == 0) {
4252 BUG_ON(s->uptodate != disks);
4253 sh->check_state = check_state_run;
4254 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4255 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4259 dev = &sh->dev[s->failed_num[0]];
4261 case check_state_compute_result:
4262 sh->check_state = check_state_idle;
4264 dev = &sh->dev[sh->pd_idx];
4266 /* check that a write has not made the stripe insync */
4267 if (test_bit(STRIPE_INSYNC, &sh->state))
4270 /* either failed parity check, or recovery is happening */
4271 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4272 BUG_ON(s->uptodate != disks);
4274 set_bit(R5_LOCKED, &dev->flags);
4276 set_bit(R5_Wantwrite, &dev->flags);
4278 clear_bit(STRIPE_DEGRADED, &sh->state);
4279 set_bit(STRIPE_INSYNC, &sh->state);
4281 case check_state_run:
4282 break; /* we will be called again upon completion */
4283 case check_state_check_result:
4284 sh->check_state = check_state_idle;
4286 /* if a failure occurred during the check operation, leave
4287 * STRIPE_INSYNC not set and let the stripe be handled again
4292 /* handle a successful check operation, if parity is correct
4293 * we are done. Otherwise update the mismatch count and repair
4294 * parity if !MD_RECOVERY_CHECK
4296 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
4297 /* parity is correct (on disc,
4298 * not in buffer any more)
4300 set_bit(STRIPE_INSYNC, &sh->state);
4302 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
4303 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
4304 /* don't try to repair!! */
4305 set_bit(STRIPE_INSYNC, &sh->state);
4306 pr_warn_ratelimited("%s: mismatch sector in range "
4307 "%llu-%llu\n", mdname(conf->mddev),
4308 (unsigned long long) sh->sector,
4309 (unsigned long long) sh->sector +
4310 RAID5_STRIPE_SECTORS(conf));
4312 sh->check_state = check_state_compute_run;
4313 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4314 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4315 set_bit(R5_Wantcompute,
4316 &sh->dev[sh->pd_idx].flags);
4317 sh->ops.target = sh->pd_idx;
4318 sh->ops.target2 = -1;
4323 case check_state_compute_run:
4326 pr_err("%s: unknown check_state: %d sector: %llu\n",
4327 __func__, sh->check_state,
4328 (unsigned long long) sh->sector);
4333 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
4334 struct stripe_head_state *s,
4337 int pd_idx = sh->pd_idx;
4338 int qd_idx = sh->qd_idx;
4341 BUG_ON(sh->batch_head);
4342 set_bit(STRIPE_HANDLE, &sh->state);
4344 BUG_ON(s->failed > 2);
4346 /* Want to check and possibly repair P and Q.
4347 * However there could be one 'failed' device, in which
4348 * case we can only check one of them, possibly using the
4349 * other to generate missing data
4352 switch (sh->check_state) {
4353 case check_state_idle:
4354 /* start a new check operation if there are < 2 failures */
4355 if (s->failed == s->q_failed) {
4356 /* The only possible failed device holds Q, so it
4357 * makes sense to check P (If anything else were failed,
4358 * we would have used P to recreate it).
4360 sh->check_state = check_state_run;
4362 if (!s->q_failed && s->failed < 2) {
4363 /* Q is not failed, and we didn't use it to generate
4364 * anything, so it makes sense to check it
4366 if (sh->check_state == check_state_run)
4367 sh->check_state = check_state_run_pq;
4369 sh->check_state = check_state_run_q;
4372 /* discard potentially stale zero_sum_result */
4373 sh->ops.zero_sum_result = 0;
4375 if (sh->check_state == check_state_run) {
4376 /* async_xor_zero_sum destroys the contents of P */
4377 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4380 if (sh->check_state >= check_state_run &&
4381 sh->check_state <= check_state_run_pq) {
4382 /* async_syndrome_zero_sum preserves P and Q, so
4383 * no need to mark them !uptodate here
4385 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4389 /* we have 2-disk failure */
4390 BUG_ON(s->failed != 2);
4392 case check_state_compute_result:
4393 sh->check_state = check_state_idle;
4395 /* check that a write has not made the stripe insync */
4396 if (test_bit(STRIPE_INSYNC, &sh->state))
4399 /* now write out any block on a failed drive,
4400 * or P or Q if they were recomputed
4403 if (s->failed == 2) {
4404 dev = &sh->dev[s->failed_num[1]];
4406 set_bit(R5_LOCKED, &dev->flags);
4407 set_bit(R5_Wantwrite, &dev->flags);
4409 if (s->failed >= 1) {
4410 dev = &sh->dev[s->failed_num[0]];
4412 set_bit(R5_LOCKED, &dev->flags);
4413 set_bit(R5_Wantwrite, &dev->flags);
4415 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4416 dev = &sh->dev[pd_idx];
4418 set_bit(R5_LOCKED, &dev->flags);
4419 set_bit(R5_Wantwrite, &dev->flags);
4421 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4422 dev = &sh->dev[qd_idx];
4424 set_bit(R5_LOCKED, &dev->flags);
4425 set_bit(R5_Wantwrite, &dev->flags);
4427 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4428 "%s: disk%td not up to date\n",
4429 mdname(conf->mddev),
4430 dev - (struct r5dev *) &sh->dev)) {
4431 clear_bit(R5_LOCKED, &dev->flags);
4432 clear_bit(R5_Wantwrite, &dev->flags);
4435 clear_bit(STRIPE_DEGRADED, &sh->state);
4437 set_bit(STRIPE_INSYNC, &sh->state);
4439 case check_state_run:
4440 case check_state_run_q:
4441 case check_state_run_pq:
4442 break; /* we will be called again upon completion */
4443 case check_state_check_result:
4444 sh->check_state = check_state_idle;
4446 /* handle a successful check operation, if parity is correct
4447 * we are done. Otherwise update the mismatch count and repair
4448 * parity if !MD_RECOVERY_CHECK
4450 if (sh->ops.zero_sum_result == 0) {
4451 /* both parities are correct */
4453 set_bit(STRIPE_INSYNC, &sh->state);
4455 /* in contrast to the raid5 case we can validate
4456 * parity, but still have a failure to write
4459 sh->check_state = check_state_compute_result;
4460 /* Returning at this point means that we may go
4461 * off and bring p and/or q uptodate again so
4462 * we make sure to check zero_sum_result again
4463 * to verify if p or q need writeback
4467 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
4468 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
4469 /* don't try to repair!! */
4470 set_bit(STRIPE_INSYNC, &sh->state);
4471 pr_warn_ratelimited("%s: mismatch sector in range "
4472 "%llu-%llu\n", mdname(conf->mddev),
4473 (unsigned long long) sh->sector,
4474 (unsigned long long) sh->sector +
4475 RAID5_STRIPE_SECTORS(conf));
4477 int *target = &sh->ops.target;
4479 sh->ops.target = -1;
4480 sh->ops.target2 = -1;
4481 sh->check_state = check_state_compute_run;
4482 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4483 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4484 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4485 set_bit(R5_Wantcompute,
4486 &sh->dev[pd_idx].flags);
4488 target = &sh->ops.target2;
4491 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4492 set_bit(R5_Wantcompute,
4493 &sh->dev[qd_idx].flags);
4500 case check_state_compute_run:
4503 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4504 __func__, sh->check_state,
4505 (unsigned long long) sh->sector);
4510 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
4514 /* We have read all the blocks in this stripe and now we need to
4515 * copy some of them into a target stripe for expand.
4517 struct dma_async_tx_descriptor *tx = NULL;
4518 BUG_ON(sh->batch_head);
4519 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4520 for (i = 0; i < sh->disks; i++)
4521 if (i != sh->pd_idx && i != sh->qd_idx) {
4523 struct stripe_head *sh2;
4524 struct async_submit_ctl submit;
4526 sector_t bn = raid5_compute_blocknr(sh, i, 1);
4527 sector_t s = raid5_compute_sector(conf, bn, 0,
4529 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
4531 /* so far only the early blocks of this stripe
4532 * have been requested. When later blocks
4533 * get requested, we will try again
4536 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4537 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4538 /* must have already done this block */
4539 raid5_release_stripe(sh2);
4543 /* place all the copies on one channel */
4544 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
4545 tx = async_memcpy(sh2->dev[dd_idx].page,
4546 sh->dev[i].page, sh2->dev[dd_idx].offset,
4547 sh->dev[i].offset, RAID5_STRIPE_SIZE(conf),
4550 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4551 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4552 for (j = 0; j < conf->raid_disks; j++)
4553 if (j != sh2->pd_idx &&
4555 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4557 if (j == conf->raid_disks) {
4558 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4559 set_bit(STRIPE_HANDLE, &sh2->state);
4561 raid5_release_stripe(sh2);
4564 /* done submitting copies, wait for them to complete */
4565 async_tx_quiesce(&tx);
4569 * handle_stripe - do things to a stripe.
4571 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4572 * state of various bits to see what needs to be done.
4574 * return some read requests which now have data
4575 * return some write requests which are safely on storage
4576 * schedule a read on some buffers
4577 * schedule a write of some buffers
4578 * return confirmation of parity correctness
4582 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
4584 struct r5conf *conf = sh->raid_conf;
4585 int disks = sh->disks;
4588 int do_recovery = 0;
4590 memset(s, 0, sizeof(*s));
4592 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4593 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
4594 s->failed_num[0] = -1;
4595 s->failed_num[1] = -1;
4596 s->log_failed = r5l_log_disk_error(conf);
4598 /* Now to look around and see what can be done */
4600 for (i=disks; i--; ) {
4601 struct md_rdev *rdev;
4608 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4610 dev->toread, dev->towrite, dev->written);
4611 /* maybe we can reply to a read
4613 * new wantfill requests are only permitted while
4614 * ops_complete_biofill is guaranteed to be inactive
4616 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4617 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4618 set_bit(R5_Wantfill, &dev->flags);
4620 /* now count some things */
4621 if (test_bit(R5_LOCKED, &dev->flags))
4623 if (test_bit(R5_UPTODATE, &dev->flags))
4625 if (test_bit(R5_Wantcompute, &dev->flags)) {
4627 BUG_ON(s->compute > 2);
4630 if (test_bit(R5_Wantfill, &dev->flags))
4632 else if (dev->toread)
4636 if (!test_bit(R5_OVERWRITE, &dev->flags))
4641 /* Prefer to use the replacement for reads, but only
4642 * if it is recovered enough and has no bad blocks.
4644 rdev = rcu_dereference(conf->disks[i].replacement);
4645 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4646 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4647 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
4648 &first_bad, &bad_sectors))
4649 set_bit(R5_ReadRepl, &dev->flags);
4651 if (rdev && !test_bit(Faulty, &rdev->flags))
4652 set_bit(R5_NeedReplace, &dev->flags);
4654 clear_bit(R5_NeedReplace, &dev->flags);
4655 rdev = rcu_dereference(conf->disks[i].rdev);
4656 clear_bit(R5_ReadRepl, &dev->flags);
4658 if (rdev && test_bit(Faulty, &rdev->flags))
4661 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
4662 &first_bad, &bad_sectors);
4663 if (s->blocked_rdev == NULL
4664 && (test_bit(Blocked, &rdev->flags)
4667 set_bit(BlockedBadBlocks,
4669 s->blocked_rdev = rdev;
4670 atomic_inc(&rdev->nr_pending);
4673 clear_bit(R5_Insync, &dev->flags);
4677 /* also not in-sync */
4678 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4679 test_bit(R5_UPTODATE, &dev->flags)) {
4680 /* treat as in-sync, but with a read error
4681 * which we can now try to correct
4683 set_bit(R5_Insync, &dev->flags);
4684 set_bit(R5_ReadError, &dev->flags);
4686 } else if (test_bit(In_sync, &rdev->flags))
4687 set_bit(R5_Insync, &dev->flags);
4688 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
4689 /* in sync if before recovery_offset */
4690 set_bit(R5_Insync, &dev->flags);
4691 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4692 test_bit(R5_Expanded, &dev->flags))
4693 /* If we've reshaped into here, we assume it is Insync.
4694 * We will shortly update recovery_offset to make
4697 set_bit(R5_Insync, &dev->flags);
4699 if (test_bit(R5_WriteError, &dev->flags)) {
4700 /* This flag does not apply to '.replacement'
4701 * only to .rdev, so make sure to check that*/
4702 struct md_rdev *rdev2 = rcu_dereference(
4703 conf->disks[i].rdev);
4705 clear_bit(R5_Insync, &dev->flags);
4706 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4707 s->handle_bad_blocks = 1;
4708 atomic_inc(&rdev2->nr_pending);
4710 clear_bit(R5_WriteError, &dev->flags);
4712 if (test_bit(R5_MadeGood, &dev->flags)) {
4713 /* This flag does not apply to '.replacement'
4714 * only to .rdev, so make sure to check that*/
4715 struct md_rdev *rdev2 = rcu_dereference(
4716 conf->disks[i].rdev);
4717 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4718 s->handle_bad_blocks = 1;
4719 atomic_inc(&rdev2->nr_pending);
4721 clear_bit(R5_MadeGood, &dev->flags);
4723 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4724 struct md_rdev *rdev2 = rcu_dereference(
4725 conf->disks[i].replacement);
4726 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4727 s->handle_bad_blocks = 1;
4728 atomic_inc(&rdev2->nr_pending);
4730 clear_bit(R5_MadeGoodRepl, &dev->flags);
4732 if (!test_bit(R5_Insync, &dev->flags)) {
4733 /* The ReadError flag will just be confusing now */
4734 clear_bit(R5_ReadError, &dev->flags);
4735 clear_bit(R5_ReWrite, &dev->flags);
4737 if (test_bit(R5_ReadError, &dev->flags))
4738 clear_bit(R5_Insync, &dev->flags);
4739 if (!test_bit(R5_Insync, &dev->flags)) {
4741 s->failed_num[s->failed] = i;
4743 if (rdev && !test_bit(Faulty, &rdev->flags))
4746 rdev = rcu_dereference(
4747 conf->disks[i].replacement);
4748 if (rdev && !test_bit(Faulty, &rdev->flags))
4753 if (test_bit(R5_InJournal, &dev->flags))
4755 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4758 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4759 /* If there is a failed device being replaced,
4760 * we must be recovering.
4761 * else if we are after recovery_cp, we must be syncing
4762 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4763 * else we can only be replacing
4764 * sync and recovery both need to read all devices, and so
4765 * use the same flag.
4768 sh->sector >= conf->mddev->recovery_cp ||
4769 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
4778 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4779 * a head which can now be handled.
4781 static int clear_batch_ready(struct stripe_head *sh)
4783 struct stripe_head *tmp;
4784 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
4785 return (sh->batch_head && sh->batch_head != sh);
4786 spin_lock(&sh->stripe_lock);
4787 if (!sh->batch_head) {
4788 spin_unlock(&sh->stripe_lock);
4793 * this stripe could be added to a batch list before we check
4794 * BATCH_READY, skips it
4796 if (sh->batch_head != sh) {
4797 spin_unlock(&sh->stripe_lock);
4800 spin_lock(&sh->batch_lock);
4801 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4802 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4803 spin_unlock(&sh->batch_lock);
4804 spin_unlock(&sh->stripe_lock);
4807 * BATCH_READY is cleared, no new stripes can be added.
4808 * batch_list can be accessed without lock
4813 static void break_stripe_batch_list(struct stripe_head *head_sh,
4814 unsigned long handle_flags)
4816 struct stripe_head *sh, *next;
4820 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4822 list_del_init(&sh->batch_list);
4824 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
4825 (1 << STRIPE_SYNCING) |
4826 (1 << STRIPE_REPLACED) |
4827 (1 << STRIPE_DELAYED) |
4828 (1 << STRIPE_BIT_DELAY) |
4829 (1 << STRIPE_FULL_WRITE) |
4830 (1 << STRIPE_BIOFILL_RUN) |
4831 (1 << STRIPE_COMPUTE_RUN) |
4832 (1 << STRIPE_DISCARD) |
4833 (1 << STRIPE_BATCH_READY) |
4834 (1 << STRIPE_BATCH_ERR) |
4835 (1 << STRIPE_BITMAP_PENDING)),
4836 "stripe state: %lx\n", sh->state);
4837 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4838 (1 << STRIPE_REPLACED)),
4839 "head stripe state: %lx\n", head_sh->state);
4841 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
4842 (1 << STRIPE_PREREAD_ACTIVE) |
4843 (1 << STRIPE_DEGRADED) |
4844 (1 << STRIPE_ON_UNPLUG_LIST)),
4845 head_sh->state & (1 << STRIPE_INSYNC));
4847 sh->check_state = head_sh->check_state;
4848 sh->reconstruct_state = head_sh->reconstruct_state;
4849 spin_lock_irq(&sh->stripe_lock);
4850 sh->batch_head = NULL;
4851 spin_unlock_irq(&sh->stripe_lock);
4852 for (i = 0; i < sh->disks; i++) {
4853 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4855 sh->dev[i].flags = head_sh->dev[i].flags &
4856 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
4858 if (handle_flags == 0 ||
4859 sh->state & handle_flags)
4860 set_bit(STRIPE_HANDLE, &sh->state);
4861 raid5_release_stripe(sh);
4863 spin_lock_irq(&head_sh->stripe_lock);
4864 head_sh->batch_head = NULL;
4865 spin_unlock_irq(&head_sh->stripe_lock);
4866 for (i = 0; i < head_sh->disks; i++)
4867 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4869 if (head_sh->state & handle_flags)
4870 set_bit(STRIPE_HANDLE, &head_sh->state);
4873 wake_up(&head_sh->raid_conf->wait_for_overlap);
4876 static void handle_stripe(struct stripe_head *sh)
4878 struct stripe_head_state s;
4879 struct r5conf *conf = sh->raid_conf;
4882 int disks = sh->disks;
4883 struct r5dev *pdev, *qdev;
4885 clear_bit(STRIPE_HANDLE, &sh->state);
4888 * handle_stripe should not continue handle the batched stripe, only
4889 * the head of batch list or lone stripe can continue. Otherwise we
4890 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4891 * is set for the batched stripe.
4893 if (clear_batch_ready(sh))
4896 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
4897 /* already being handled, ensure it gets handled
4898 * again when current action finishes */
4899 set_bit(STRIPE_HANDLE, &sh->state);
4903 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
4904 break_stripe_batch_list(sh, 0);
4906 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
4907 spin_lock(&sh->stripe_lock);
4909 * Cannot process 'sync' concurrently with 'discard'.
4910 * Flush data in r5cache before 'sync'.
4912 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4913 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4914 !test_bit(STRIPE_DISCARD, &sh->state) &&
4915 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4916 set_bit(STRIPE_SYNCING, &sh->state);
4917 clear_bit(STRIPE_INSYNC, &sh->state);
4918 clear_bit(STRIPE_REPLACED, &sh->state);
4920 spin_unlock(&sh->stripe_lock);
4922 clear_bit(STRIPE_DELAYED, &sh->state);
4924 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4925 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4926 (unsigned long long)sh->sector, sh->state,
4927 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4928 sh->check_state, sh->reconstruct_state);
4930 analyse_stripe(sh, &s);
4932 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4935 if (s.handle_bad_blocks ||
4936 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
4937 set_bit(STRIPE_HANDLE, &sh->state);
4941 if (unlikely(s.blocked_rdev)) {
4942 if (s.syncing || s.expanding || s.expanded ||
4943 s.replacing || s.to_write || s.written) {
4944 set_bit(STRIPE_HANDLE, &sh->state);
4947 /* There is nothing for the blocked_rdev to block */
4948 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4949 s.blocked_rdev = NULL;
4952 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4953 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4954 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4957 pr_debug("locked=%d uptodate=%d to_read=%d"
4958 " to_write=%d failed=%d failed_num=%d,%d\n",
4959 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4960 s.failed_num[0], s.failed_num[1]);
4962 * check if the array has lost more than max_degraded devices and,
4963 * if so, some requests might need to be failed.
4965 * When journal device failed (log_failed), we will only process
4966 * the stripe if there is data need write to raid disks
4968 if (s.failed > conf->max_degraded ||
4969 (s.log_failed && s.injournal == 0)) {
4970 sh->check_state = 0;
4971 sh->reconstruct_state = 0;
4972 break_stripe_batch_list(sh, 0);
4973 if (s.to_read+s.to_write+s.written)
4974 handle_failed_stripe(conf, sh, &s, disks);
4975 if (s.syncing + s.replacing)
4976 handle_failed_sync(conf, sh, &s);
4979 /* Now we check to see if any write operations have recently
4983 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4985 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4986 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4987 sh->reconstruct_state = reconstruct_state_idle;
4989 /* All the 'written' buffers and the parity block are ready to
4990 * be written back to disk
4992 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4993 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
4994 BUG_ON(sh->qd_idx >= 0 &&
4995 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4996 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
4997 for (i = disks; i--; ) {
4998 struct r5dev *dev = &sh->dev[i];
4999 if (test_bit(R5_LOCKED, &dev->flags) &&
5000 (i == sh->pd_idx || i == sh->qd_idx ||
5001 dev->written || test_bit(R5_InJournal,
5003 pr_debug("Writing block %d\n", i);
5004 set_bit(R5_Wantwrite, &dev->flags);
5009 if (!test_bit(R5_Insync, &dev->flags) ||
5010 ((i == sh->pd_idx || i == sh->qd_idx) &&
5012 set_bit(STRIPE_INSYNC, &sh->state);
5015 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5016 s.dec_preread_active = 1;
5020 * might be able to return some write requests if the parity blocks
5021 * are safe, or on a failed drive
5023 pdev = &sh->dev[sh->pd_idx];
5024 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
5025 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
5026 qdev = &sh->dev[sh->qd_idx];
5027 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
5028 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
5032 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
5033 && !test_bit(R5_LOCKED, &pdev->flags)
5034 && (test_bit(R5_UPTODATE, &pdev->flags) ||
5035 test_bit(R5_Discard, &pdev->flags))))) &&
5036 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
5037 && !test_bit(R5_LOCKED, &qdev->flags)
5038 && (test_bit(R5_UPTODATE, &qdev->flags) ||
5039 test_bit(R5_Discard, &qdev->flags))))))
5040 handle_stripe_clean_event(conf, sh, disks);
5043 r5c_handle_cached_data_endio(conf, sh, disks);
5044 log_stripe_write_finished(sh);
5046 /* Now we might consider reading some blocks, either to check/generate
5047 * parity, or to satisfy requests
5048 * or to load a block that is being partially written.
5050 if (s.to_read || s.non_overwrite
5051 || (s.to_write && s.failed)
5052 || (s.syncing && (s.uptodate + s.compute < disks))
5055 handle_stripe_fill(sh, &s, disks);
5058 * When the stripe finishes full journal write cycle (write to journal
5059 * and raid disk), this is the clean up procedure so it is ready for
5062 r5c_finish_stripe_write_out(conf, sh, &s);
5065 * Now to consider new write requests, cache write back and what else,
5066 * if anything should be read. We do not handle new writes when:
5067 * 1/ A 'write' operation (copy+xor) is already in flight.
5068 * 2/ A 'check' operation is in flight, as it may clobber the parity
5070 * 3/ A r5c cache log write is in flight.
5073 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
5074 if (!r5c_is_writeback(conf->log)) {
5076 handle_stripe_dirtying(conf, sh, &s, disks);
5077 } else { /* write back cache */
5080 /* First, try handle writes in caching phase */
5082 ret = r5c_try_caching_write(conf, sh, &s,
5085 * If caching phase failed: ret == -EAGAIN
5087 * stripe under reclaim: !caching && injournal
5089 * fall back to handle_stripe_dirtying()
5091 if (ret == -EAGAIN ||
5092 /* stripe under reclaim: !caching && injournal */
5093 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
5095 ret = handle_stripe_dirtying(conf, sh, &s,
5103 /* maybe we need to check and possibly fix the parity for this stripe
5104 * Any reads will already have been scheduled, so we just see if enough
5105 * data is available. The parity check is held off while parity
5106 * dependent operations are in flight.
5108 if (sh->check_state ||
5109 (s.syncing && s.locked == 0 &&
5110 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5111 !test_bit(STRIPE_INSYNC, &sh->state))) {
5112 if (conf->level == 6)
5113 handle_parity_checks6(conf, sh, &s, disks);
5115 handle_parity_checks5(conf, sh, &s, disks);
5118 if ((s.replacing || s.syncing) && s.locked == 0
5119 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
5120 && !test_bit(STRIPE_REPLACED, &sh->state)) {
5121 /* Write out to replacement devices where possible */
5122 for (i = 0; i < conf->raid_disks; i++)
5123 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
5124 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
5125 set_bit(R5_WantReplace, &sh->dev[i].flags);
5126 set_bit(R5_LOCKED, &sh->dev[i].flags);
5130 set_bit(STRIPE_INSYNC, &sh->state);
5131 set_bit(STRIPE_REPLACED, &sh->state);
5133 if ((s.syncing || s.replacing) && s.locked == 0 &&
5134 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5135 test_bit(STRIPE_INSYNC, &sh->state)) {
5136 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
5137 clear_bit(STRIPE_SYNCING, &sh->state);
5138 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
5139 wake_up(&conf->wait_for_overlap);
5142 /* If the failed drives are just a ReadError, then we might need
5143 * to progress the repair/check process
5145 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
5146 for (i = 0; i < s.failed; i++) {
5147 struct r5dev *dev = &sh->dev[s.failed_num[i]];
5148 if (test_bit(R5_ReadError, &dev->flags)
5149 && !test_bit(R5_LOCKED, &dev->flags)
5150 && test_bit(R5_UPTODATE, &dev->flags)
5152 if (!test_bit(R5_ReWrite, &dev->flags)) {
5153 set_bit(R5_Wantwrite, &dev->flags);
5154 set_bit(R5_ReWrite, &dev->flags);
5156 /* let's read it back */
5157 set_bit(R5_Wantread, &dev->flags);
5158 set_bit(R5_LOCKED, &dev->flags);
5163 /* Finish reconstruct operations initiated by the expansion process */
5164 if (sh->reconstruct_state == reconstruct_state_result) {
5165 struct stripe_head *sh_src
5166 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
5167 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
5168 /* sh cannot be written until sh_src has been read.
5169 * so arrange for sh to be delayed a little
5171 set_bit(STRIPE_DELAYED, &sh->state);
5172 set_bit(STRIPE_HANDLE, &sh->state);
5173 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
5175 atomic_inc(&conf->preread_active_stripes);
5176 raid5_release_stripe(sh_src);
5180 raid5_release_stripe(sh_src);
5182 sh->reconstruct_state = reconstruct_state_idle;
5183 clear_bit(STRIPE_EXPANDING, &sh->state);
5184 for (i = conf->raid_disks; i--; ) {
5185 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5186 set_bit(R5_LOCKED, &sh->dev[i].flags);
5191 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5192 !sh->reconstruct_state) {
5193 /* Need to write out all blocks after computing parity */
5194 sh->disks = conf->raid_disks;
5195 stripe_set_idx(sh->sector, conf, 0, sh);
5196 schedule_reconstruction(sh, &s, 1, 1);
5197 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5198 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5199 atomic_dec(&conf->reshape_stripes);
5200 wake_up(&conf->wait_for_overlap);
5201 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
5204 if (s.expanding && s.locked == 0 &&
5205 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5206 handle_stripe_expansion(conf, sh);
5209 /* wait for this device to become unblocked */
5210 if (unlikely(s.blocked_rdev)) {
5211 if (conf->mddev->external)
5212 md_wait_for_blocked_rdev(s.blocked_rdev,
5215 /* Internal metadata will immediately
5216 * be written by raid5d, so we don't
5217 * need to wait here.
5219 rdev_dec_pending(s.blocked_rdev,
5223 if (s.handle_bad_blocks)
5224 for (i = disks; i--; ) {
5225 struct md_rdev *rdev;
5226 struct r5dev *dev = &sh->dev[i];
5227 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5228 /* We own a safe reference to the rdev */
5229 rdev = conf->disks[i].rdev;
5230 if (!rdev_set_badblocks(rdev, sh->sector,
5231 RAID5_STRIPE_SECTORS(conf), 0))
5232 md_error(conf->mddev, rdev);
5233 rdev_dec_pending(rdev, conf->mddev);
5235 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5236 rdev = conf->disks[i].rdev;
5237 rdev_clear_badblocks(rdev, sh->sector,
5238 RAID5_STRIPE_SECTORS(conf), 0);
5239 rdev_dec_pending(rdev, conf->mddev);
5241 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5242 rdev = conf->disks[i].replacement;
5244 /* rdev have been moved down */
5245 rdev = conf->disks[i].rdev;
5246 rdev_clear_badblocks(rdev, sh->sector,
5247 RAID5_STRIPE_SECTORS(conf), 0);
5248 rdev_dec_pending(rdev, conf->mddev);
5253 raid_run_ops(sh, s.ops_request);
5257 if (s.dec_preread_active) {
5258 /* We delay this until after ops_run_io so that if make_request
5259 * is waiting on a flush, it won't continue until the writes
5260 * have actually been submitted.
5262 atomic_dec(&conf->preread_active_stripes);
5263 if (atomic_read(&conf->preread_active_stripes) <
5265 md_wakeup_thread(conf->mddev->thread);
5268 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
5271 static void raid5_activate_delayed(struct r5conf *conf)
5273 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5274 while (!list_empty(&conf->delayed_list)) {
5275 struct list_head *l = conf->delayed_list.next;
5276 struct stripe_head *sh;
5277 sh = list_entry(l, struct stripe_head, lru);
5279 clear_bit(STRIPE_DELAYED, &sh->state);
5280 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5281 atomic_inc(&conf->preread_active_stripes);
5282 list_add_tail(&sh->lru, &conf->hold_list);
5283 raid5_wakeup_stripe_thread(sh);
5288 static void activate_bit_delay(struct r5conf *conf,
5289 struct list_head *temp_inactive_list)
5291 /* device_lock is held */
5292 struct list_head head;
5293 list_add(&head, &conf->bitmap_list);
5294 list_del_init(&conf->bitmap_list);
5295 while (!list_empty(&head)) {
5296 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
5298 list_del_init(&sh->lru);
5299 atomic_inc(&sh->count);
5300 hash = sh->hash_lock_index;
5301 __release_stripe(conf, sh, &temp_inactive_list[hash]);
5305 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
5307 struct r5conf *conf = mddev->private;
5308 sector_t sector = bio->bi_iter.bi_sector;
5309 unsigned int chunk_sectors;
5310 unsigned int bio_sectors = bio_sectors(bio);
5312 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
5313 return chunk_sectors >=
5314 ((sector & (chunk_sectors - 1)) + bio_sectors);
5318 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5319 * later sampled by raid5d.
5321 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
5323 unsigned long flags;
5325 spin_lock_irqsave(&conf->device_lock, flags);
5327 bi->bi_next = conf->retry_read_aligned_list;
5328 conf->retry_read_aligned_list = bi;
5330 spin_unlock_irqrestore(&conf->device_lock, flags);
5331 md_wakeup_thread(conf->mddev->thread);
5334 static struct bio *remove_bio_from_retry(struct r5conf *conf,
5335 unsigned int *offset)
5339 bi = conf->retry_read_aligned;
5341 *offset = conf->retry_read_offset;
5342 conf->retry_read_aligned = NULL;
5345 bi = conf->retry_read_aligned_list;
5347 conf->retry_read_aligned_list = bi->bi_next;
5356 * The "raid5_align_endio" should check if the read succeeded and if it
5357 * did, call bio_endio on the original bio (having bio_put the new bio
5359 * If the read failed..
5361 static void raid5_align_endio(struct bio *bi)
5363 struct md_io_acct *md_io_acct = bi->bi_private;
5364 struct bio *raid_bi = md_io_acct->orig_bio;
5365 struct mddev *mddev;
5366 struct r5conf *conf;
5367 struct md_rdev *rdev;
5368 blk_status_t error = bi->bi_status;
5369 unsigned long start_time = md_io_acct->start_time;
5373 rdev = (void*)raid_bi->bi_next;
5374 raid_bi->bi_next = NULL;
5375 mddev = rdev->mddev;
5376 conf = mddev->private;
5378 rdev_dec_pending(rdev, conf->mddev);
5381 if (blk_queue_io_stat(raid_bi->bi_bdev->bd_disk->queue))
5382 bio_end_io_acct(raid_bi, start_time);
5384 if (atomic_dec_and_test(&conf->active_aligned_reads))
5385 wake_up(&conf->wait_for_quiescent);
5389 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
5391 add_bio_to_retry(raid_bi, conf);
5394 static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
5396 struct r5conf *conf = mddev->private;
5397 struct bio *align_bio;
5398 struct md_rdev *rdev;
5399 sector_t sector, end_sector, first_bad;
5400 int bad_sectors, dd_idx;
5401 struct md_io_acct *md_io_acct;
5404 if (!in_chunk_boundary(mddev, raid_bio)) {
5405 pr_debug("%s: non aligned\n", __func__);
5409 sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0,
5411 end_sector = bio_end_sector(raid_bio);
5414 if (r5c_big_stripe_cached(conf, sector))
5415 goto out_rcu_unlock;
5417 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5418 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5419 rdev->recovery_offset < end_sector) {
5420 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5422 goto out_rcu_unlock;
5423 if (test_bit(Faulty, &rdev->flags) ||
5424 !(test_bit(In_sync, &rdev->flags) ||
5425 rdev->recovery_offset >= end_sector))
5426 goto out_rcu_unlock;
5429 atomic_inc(&rdev->nr_pending);
5432 if (is_badblock(rdev, sector, bio_sectors(raid_bio), &first_bad,
5434 rdev_dec_pending(rdev, mddev);
5438 align_bio = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->io_acct_set);
5439 md_io_acct = container_of(align_bio, struct md_io_acct, bio_clone);
5440 raid_bio->bi_next = (void *)rdev;
5441 if (blk_queue_io_stat(raid_bio->bi_bdev->bd_disk->queue))
5442 md_io_acct->start_time = bio_start_io_acct(raid_bio);
5443 md_io_acct->orig_bio = raid_bio;
5445 bio_set_dev(align_bio, rdev->bdev);
5446 align_bio->bi_end_io = raid5_align_endio;
5447 align_bio->bi_private = md_io_acct;
5448 align_bio->bi_iter.bi_sector = sector;
5450 /* No reshape active, so we can trust rdev->data_offset */
5451 align_bio->bi_iter.bi_sector += rdev->data_offset;
5454 if (conf->quiesce == 0) {
5455 atomic_inc(&conf->active_aligned_reads);
5458 /* need a memory barrier to detect the race with raid5_quiesce() */
5459 if (!did_inc || smp_load_acquire(&conf->quiesce) != 0) {
5460 /* quiesce is in progress, so we need to undo io activation and wait
5463 if (did_inc && atomic_dec_and_test(&conf->active_aligned_reads))
5464 wake_up(&conf->wait_for_quiescent);
5465 spin_lock_irq(&conf->device_lock);
5466 wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0,
5468 atomic_inc(&conf->active_aligned_reads);
5469 spin_unlock_irq(&conf->device_lock);
5473 trace_block_bio_remap(align_bio, disk_devt(mddev->gendisk),
5474 raid_bio->bi_iter.bi_sector);
5475 submit_bio_noacct(align_bio);
5483 static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5486 sector_t sector = raid_bio->bi_iter.bi_sector;
5487 unsigned chunk_sects = mddev->chunk_sectors;
5488 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5490 if (sectors < bio_sectors(raid_bio)) {
5491 struct r5conf *conf = mddev->private;
5492 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
5493 bio_chain(split, raid_bio);
5494 submit_bio_noacct(raid_bio);
5498 if (!raid5_read_one_chunk(mddev, raid_bio))
5504 /* __get_priority_stripe - get the next stripe to process
5506 * Full stripe writes are allowed to pass preread active stripes up until
5507 * the bypass_threshold is exceeded. In general the bypass_count
5508 * increments when the handle_list is handled before the hold_list; however, it
5509 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5510 * stripe with in flight i/o. The bypass_count will be reset when the
5511 * head of the hold_list has changed, i.e. the head was promoted to the
5514 static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
5516 struct stripe_head *sh, *tmp;
5517 struct list_head *handle_list = NULL;
5518 struct r5worker_group *wg;
5519 bool second_try = !r5c_is_writeback(conf->log) &&
5520 !r5l_log_disk_error(conf);
5521 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5522 r5l_log_disk_error(conf);
5527 if (conf->worker_cnt_per_group == 0) {
5528 handle_list = try_loprio ? &conf->loprio_list :
5530 } else if (group != ANY_GROUP) {
5531 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5532 &conf->worker_groups[group].handle_list;
5533 wg = &conf->worker_groups[group];
5536 for (i = 0; i < conf->group_cnt; i++) {
5537 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5538 &conf->worker_groups[i].handle_list;
5539 wg = &conf->worker_groups[i];
5540 if (!list_empty(handle_list))
5545 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5547 list_empty(handle_list) ? "empty" : "busy",
5548 list_empty(&conf->hold_list) ? "empty" : "busy",
5549 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5551 if (!list_empty(handle_list)) {
5552 sh = list_entry(handle_list->next, typeof(*sh), lru);
5554 if (list_empty(&conf->hold_list))
5555 conf->bypass_count = 0;
5556 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5557 if (conf->hold_list.next == conf->last_hold)
5558 conf->bypass_count++;
5560 conf->last_hold = conf->hold_list.next;
5561 conf->bypass_count -= conf->bypass_threshold;
5562 if (conf->bypass_count < 0)
5563 conf->bypass_count = 0;
5566 } else if (!list_empty(&conf->hold_list) &&
5567 ((conf->bypass_threshold &&
5568 conf->bypass_count > conf->bypass_threshold) ||
5569 atomic_read(&conf->pending_full_writes) == 0)) {
5571 list_for_each_entry(tmp, &conf->hold_list, lru) {
5572 if (conf->worker_cnt_per_group == 0 ||
5573 group == ANY_GROUP ||
5574 !cpu_online(tmp->cpu) ||
5575 cpu_to_group(tmp->cpu) == group) {
5582 conf->bypass_count -= conf->bypass_threshold;
5583 if (conf->bypass_count < 0)
5584 conf->bypass_count = 0;
5593 try_loprio = !try_loprio;
5601 list_del_init(&sh->lru);
5602 BUG_ON(atomic_inc_return(&sh->count) != 1);
5606 struct raid5_plug_cb {
5607 struct blk_plug_cb cb;
5608 struct list_head list;
5609 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
5612 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5614 struct raid5_plug_cb *cb = container_of(
5615 blk_cb, struct raid5_plug_cb, cb);
5616 struct stripe_head *sh;
5617 struct mddev *mddev = cb->cb.data;
5618 struct r5conf *conf = mddev->private;
5622 if (cb->list.next && !list_empty(&cb->list)) {
5623 spin_lock_irq(&conf->device_lock);
5624 while (!list_empty(&cb->list)) {
5625 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5626 list_del_init(&sh->lru);
5628 * avoid race release_stripe_plug() sees
5629 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5630 * is still in our list
5632 smp_mb__before_atomic();
5633 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
5635 * STRIPE_ON_RELEASE_LIST could be set here. In that
5636 * case, the count is always > 1 here
5638 hash = sh->hash_lock_index;
5639 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
5642 spin_unlock_irq(&conf->device_lock);
5644 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5645 NR_STRIPE_HASH_LOCKS);
5647 trace_block_unplug(mddev->queue, cnt, !from_schedule);
5651 static void release_stripe_plug(struct mddev *mddev,
5652 struct stripe_head *sh)
5654 struct blk_plug_cb *blk_cb = blk_check_plugged(
5655 raid5_unplug, mddev,
5656 sizeof(struct raid5_plug_cb));
5657 struct raid5_plug_cb *cb;
5660 raid5_release_stripe(sh);
5664 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5666 if (cb->list.next == NULL) {
5668 INIT_LIST_HEAD(&cb->list);
5669 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5670 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5673 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5674 list_add_tail(&sh->lru, &cb->list);
5676 raid5_release_stripe(sh);
5679 static void make_discard_request(struct mddev *mddev, struct bio *bi)
5681 struct r5conf *conf = mddev->private;
5682 sector_t logical_sector, last_sector;
5683 struct stripe_head *sh;
5686 if (mddev->reshape_position != MaxSector)
5687 /* Skip discard while reshape is happening */
5690 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
5691 last_sector = bio_end_sector(bi);
5695 stripe_sectors = conf->chunk_sectors *
5696 (conf->raid_disks - conf->max_degraded);
5697 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5699 sector_div(last_sector, stripe_sectors);
5701 logical_sector *= conf->chunk_sectors;
5702 last_sector *= conf->chunk_sectors;
5704 for (; logical_sector < last_sector;
5705 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
5709 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
5710 prepare_to_wait(&conf->wait_for_overlap, &w,
5711 TASK_UNINTERRUPTIBLE);
5712 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5713 if (test_bit(STRIPE_SYNCING, &sh->state)) {
5714 raid5_release_stripe(sh);
5718 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5719 spin_lock_irq(&sh->stripe_lock);
5720 for (d = 0; d < conf->raid_disks; d++) {
5721 if (d == sh->pd_idx || d == sh->qd_idx)
5723 if (sh->dev[d].towrite || sh->dev[d].toread) {
5724 set_bit(R5_Overlap, &sh->dev[d].flags);
5725 spin_unlock_irq(&sh->stripe_lock);
5726 raid5_release_stripe(sh);
5731 set_bit(STRIPE_DISCARD, &sh->state);
5732 finish_wait(&conf->wait_for_overlap, &w);
5733 sh->overwrite_disks = 0;
5734 for (d = 0; d < conf->raid_disks; d++) {
5735 if (d == sh->pd_idx || d == sh->qd_idx)
5737 sh->dev[d].towrite = bi;
5738 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5739 bio_inc_remaining(bi);
5740 md_write_inc(mddev, bi);
5741 sh->overwrite_disks++;
5743 spin_unlock_irq(&sh->stripe_lock);
5744 if (conf->mddev->bitmap) {
5746 d < conf->raid_disks - conf->max_degraded;
5748 md_bitmap_startwrite(mddev->bitmap,
5750 RAID5_STRIPE_SECTORS(conf),
5752 sh->bm_seq = conf->seq_flush + 1;
5753 set_bit(STRIPE_BIT_DELAY, &sh->state);
5756 set_bit(STRIPE_HANDLE, &sh->state);
5757 clear_bit(STRIPE_DELAYED, &sh->state);
5758 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5759 atomic_inc(&conf->preread_active_stripes);
5760 release_stripe_plug(mddev, sh);
5766 static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
5768 struct r5conf *conf = mddev->private;
5770 sector_t new_sector;
5771 sector_t logical_sector, last_sector;
5772 struct stripe_head *sh;
5773 const int rw = bio_data_dir(bi);
5776 bool do_flush = false;
5778 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
5779 int ret = log_handle_flush_request(conf, bi);
5783 if (ret == -ENODEV) {
5784 if (md_flush_request(mddev, bi))
5787 /* ret == -EAGAIN, fallback */
5789 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5790 * we need to flush journal device
5792 do_flush = bi->bi_opf & REQ_PREFLUSH;
5795 if (!md_write_start(mddev, bi))
5798 * If array is degraded, better not do chunk aligned read because
5799 * later we might have to read it again in order to reconstruct
5800 * data on failed drives.
5802 if (rw == READ && mddev->degraded == 0 &&
5803 mddev->reshape_position == MaxSector) {
5804 bi = chunk_aligned_read(mddev, bi);
5809 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
5810 make_discard_request(mddev, bi);
5811 md_write_end(mddev);
5815 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
5816 last_sector = bio_end_sector(bi);
5819 md_account_bio(mddev, &bi);
5820 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
5821 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) {
5827 seq = read_seqcount_begin(&conf->gen_lock);
5830 prepare_to_wait(&conf->wait_for_overlap, &w,
5831 TASK_UNINTERRUPTIBLE);
5832 if (unlikely(conf->reshape_progress != MaxSector)) {
5833 /* spinlock is needed as reshape_progress may be
5834 * 64bit on a 32bit platform, and so it might be
5835 * possible to see a half-updated value
5836 * Of course reshape_progress could change after
5837 * the lock is dropped, so once we get a reference
5838 * to the stripe that we think it is, we will have
5841 spin_lock_irq(&conf->device_lock);
5842 if (mddev->reshape_backwards
5843 ? logical_sector < conf->reshape_progress
5844 : logical_sector >= conf->reshape_progress) {
5847 if (mddev->reshape_backwards
5848 ? logical_sector < conf->reshape_safe
5849 : logical_sector >= conf->reshape_safe) {
5850 spin_unlock_irq(&conf->device_lock);
5856 spin_unlock_irq(&conf->device_lock);
5859 new_sector = raid5_compute_sector(conf, logical_sector,
5862 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
5863 (unsigned long long)new_sector,
5864 (unsigned long long)logical_sector);
5866 sh = raid5_get_active_stripe(conf, new_sector, previous,
5867 (bi->bi_opf & REQ_RAHEAD), 0);
5869 if (unlikely(previous)) {
5870 /* expansion might have moved on while waiting for a
5871 * stripe, so we must do the range check again.
5872 * Expansion could still move past after this
5873 * test, but as we are holding a reference to
5874 * 'sh', we know that if that happens,
5875 * STRIPE_EXPANDING will get set and the expansion
5876 * won't proceed until we finish with the stripe.
5879 spin_lock_irq(&conf->device_lock);
5880 if (mddev->reshape_backwards
5881 ? logical_sector >= conf->reshape_progress
5882 : logical_sector < conf->reshape_progress)
5883 /* mismatch, need to try again */
5885 spin_unlock_irq(&conf->device_lock);
5887 raid5_release_stripe(sh);
5893 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5894 /* Might have got the wrong stripe_head
5897 raid5_release_stripe(sh);
5901 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
5902 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
5903 /* Stripe is busy expanding or
5904 * add failed due to overlap. Flush everything
5907 md_wakeup_thread(mddev->thread);
5908 raid5_release_stripe(sh);
5914 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5915 /* we only need flush for one stripe */
5919 set_bit(STRIPE_HANDLE, &sh->state);
5920 clear_bit(STRIPE_DELAYED, &sh->state);
5921 if ((!sh->batch_head || sh == sh->batch_head) &&
5922 (bi->bi_opf & REQ_SYNC) &&
5923 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5924 atomic_inc(&conf->preread_active_stripes);
5925 release_stripe_plug(mddev, sh);
5927 /* cannot get stripe for read-ahead, just give-up */
5928 bi->bi_status = BLK_STS_IOERR;
5932 finish_wait(&conf->wait_for_overlap, &w);
5935 md_write_end(mddev);
5940 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
5942 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
5944 /* reshaping is quite different to recovery/resync so it is
5945 * handled quite separately ... here.
5947 * On each call to sync_request, we gather one chunk worth of
5948 * destination stripes and flag them as expanding.
5949 * Then we find all the source stripes and request reads.
5950 * As the reads complete, handle_stripe will copy the data
5951 * into the destination stripe and release that stripe.
5953 struct r5conf *conf = mddev->private;
5954 struct stripe_head *sh;
5955 struct md_rdev *rdev;
5956 sector_t first_sector, last_sector;
5957 int raid_disks = conf->previous_raid_disks;
5958 int data_disks = raid_disks - conf->max_degraded;
5959 int new_data_disks = conf->raid_disks - conf->max_degraded;
5962 sector_t writepos, readpos, safepos;
5963 sector_t stripe_addr;
5964 int reshape_sectors;
5965 struct list_head stripes;
5968 if (sector_nr == 0) {
5969 /* If restarting in the middle, skip the initial sectors */
5970 if (mddev->reshape_backwards &&
5971 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5972 sector_nr = raid5_size(mddev, 0, 0)
5973 - conf->reshape_progress;
5974 } else if (mddev->reshape_backwards &&
5975 conf->reshape_progress == MaxSector) {
5976 /* shouldn't happen, but just in case, finish up.*/
5977 sector_nr = MaxSector;
5978 } else if (!mddev->reshape_backwards &&
5979 conf->reshape_progress > 0)
5980 sector_nr = conf->reshape_progress;
5981 sector_div(sector_nr, new_data_disks);
5983 mddev->curr_resync_completed = sector_nr;
5984 sysfs_notify_dirent_safe(mddev->sysfs_completed);
5991 /* We need to process a full chunk at a time.
5992 * If old and new chunk sizes differ, we need to process the
5996 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
5998 /* We update the metadata at least every 10 seconds, or when
5999 * the data about to be copied would over-write the source of
6000 * the data at the front of the range. i.e. one new_stripe
6001 * along from reshape_progress new_maps to after where
6002 * reshape_safe old_maps to
6004 writepos = conf->reshape_progress;
6005 sector_div(writepos, new_data_disks);
6006 readpos = conf->reshape_progress;
6007 sector_div(readpos, data_disks);
6008 safepos = conf->reshape_safe;
6009 sector_div(safepos, data_disks);
6010 if (mddev->reshape_backwards) {
6011 BUG_ON(writepos < reshape_sectors);
6012 writepos -= reshape_sectors;
6013 readpos += reshape_sectors;
6014 safepos += reshape_sectors;
6016 writepos += reshape_sectors;
6017 /* readpos and safepos are worst-case calculations.
6018 * A negative number is overly pessimistic, and causes
6019 * obvious problems for unsigned storage. So clip to 0.
6021 readpos -= min_t(sector_t, reshape_sectors, readpos);
6022 safepos -= min_t(sector_t, reshape_sectors, safepos);
6025 /* Having calculated the 'writepos' possibly use it
6026 * to set 'stripe_addr' which is where we will write to.
6028 if (mddev->reshape_backwards) {
6029 BUG_ON(conf->reshape_progress == 0);
6030 stripe_addr = writepos;
6031 BUG_ON((mddev->dev_sectors &
6032 ~((sector_t)reshape_sectors - 1))
6033 - reshape_sectors - stripe_addr
6036 BUG_ON(writepos != sector_nr + reshape_sectors);
6037 stripe_addr = sector_nr;
6040 /* 'writepos' is the most advanced device address we might write.
6041 * 'readpos' is the least advanced device address we might read.
6042 * 'safepos' is the least address recorded in the metadata as having
6044 * If there is a min_offset_diff, these are adjusted either by
6045 * increasing the safepos/readpos if diff is negative, or
6046 * increasing writepos if diff is positive.
6047 * If 'readpos' is then behind 'writepos', there is no way that we can
6048 * ensure safety in the face of a crash - that must be done by userspace
6049 * making a backup of the data. So in that case there is no particular
6050 * rush to update metadata.
6051 * Otherwise if 'safepos' is behind 'writepos', then we really need to
6052 * update the metadata to advance 'safepos' to match 'readpos' so that
6053 * we can be safe in the event of a crash.
6054 * So we insist on updating metadata if safepos is behind writepos and
6055 * readpos is beyond writepos.
6056 * In any case, update the metadata every 10 seconds.
6057 * Maybe that number should be configurable, but I'm not sure it is
6058 * worth it.... maybe it could be a multiple of safemode_delay???
6060 if (conf->min_offset_diff < 0) {
6061 safepos += -conf->min_offset_diff;
6062 readpos += -conf->min_offset_diff;
6064 writepos += conf->min_offset_diff;
6066 if ((mddev->reshape_backwards
6067 ? (safepos > writepos && readpos < writepos)
6068 : (safepos < writepos && readpos > writepos)) ||
6069 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
6070 /* Cannot proceed until we've updated the superblock... */
6071 wait_event(conf->wait_for_overlap,
6072 atomic_read(&conf->reshape_stripes)==0
6073 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6074 if (atomic_read(&conf->reshape_stripes) != 0)
6076 mddev->reshape_position = conf->reshape_progress;
6077 mddev->curr_resync_completed = sector_nr;
6078 if (!mddev->reshape_backwards)
6079 /* Can update recovery_offset */
6080 rdev_for_each(rdev, mddev)
6081 if (rdev->raid_disk >= 0 &&
6082 !test_bit(Journal, &rdev->flags) &&
6083 !test_bit(In_sync, &rdev->flags) &&
6084 rdev->recovery_offset < sector_nr)
6085 rdev->recovery_offset = sector_nr;
6087 conf->reshape_checkpoint = jiffies;
6088 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6089 md_wakeup_thread(mddev->thread);
6090 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
6091 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6092 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6094 spin_lock_irq(&conf->device_lock);
6095 conf->reshape_safe = mddev->reshape_position;
6096 spin_unlock_irq(&conf->device_lock);
6097 wake_up(&conf->wait_for_overlap);
6098 sysfs_notify_dirent_safe(mddev->sysfs_completed);
6101 INIT_LIST_HEAD(&stripes);
6102 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
6104 int skipped_disk = 0;
6105 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
6106 set_bit(STRIPE_EXPANDING, &sh->state);
6107 atomic_inc(&conf->reshape_stripes);
6108 /* If any of this stripe is beyond the end of the old
6109 * array, then we need to zero those blocks
6111 for (j=sh->disks; j--;) {
6113 if (j == sh->pd_idx)
6115 if (conf->level == 6 &&
6118 s = raid5_compute_blocknr(sh, j, 0);
6119 if (s < raid5_size(mddev, 0, 0)) {
6123 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
6124 set_bit(R5_Expanded, &sh->dev[j].flags);
6125 set_bit(R5_UPTODATE, &sh->dev[j].flags);
6127 if (!skipped_disk) {
6128 set_bit(STRIPE_EXPAND_READY, &sh->state);
6129 set_bit(STRIPE_HANDLE, &sh->state);
6131 list_add(&sh->lru, &stripes);
6133 spin_lock_irq(&conf->device_lock);
6134 if (mddev->reshape_backwards)
6135 conf->reshape_progress -= reshape_sectors * new_data_disks;
6137 conf->reshape_progress += reshape_sectors * new_data_disks;
6138 spin_unlock_irq(&conf->device_lock);
6139 /* Ok, those stripe are ready. We can start scheduling
6140 * reads on the source stripes.
6141 * The source stripes are determined by mapping the first and last
6142 * block on the destination stripes.
6145 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
6148 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
6149 * new_data_disks - 1),
6151 if (last_sector >= mddev->dev_sectors)
6152 last_sector = mddev->dev_sectors - 1;
6153 while (first_sector <= last_sector) {
6154 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
6155 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
6156 set_bit(STRIPE_HANDLE, &sh->state);
6157 raid5_release_stripe(sh);
6158 first_sector += RAID5_STRIPE_SECTORS(conf);
6160 /* Now that the sources are clearly marked, we can release
6161 * the destination stripes
6163 while (!list_empty(&stripes)) {
6164 sh = list_entry(stripes.next, struct stripe_head, lru);
6165 list_del_init(&sh->lru);
6166 raid5_release_stripe(sh);
6168 /* If this takes us to the resync_max point where we have to pause,
6169 * then we need to write out the superblock.
6171 sector_nr += reshape_sectors;
6172 retn = reshape_sectors;
6174 if (mddev->curr_resync_completed > mddev->resync_max ||
6175 (sector_nr - mddev->curr_resync_completed) * 2
6176 >= mddev->resync_max - mddev->curr_resync_completed) {
6177 /* Cannot proceed until we've updated the superblock... */
6178 wait_event(conf->wait_for_overlap,
6179 atomic_read(&conf->reshape_stripes) == 0
6180 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6181 if (atomic_read(&conf->reshape_stripes) != 0)
6183 mddev->reshape_position = conf->reshape_progress;
6184 mddev->curr_resync_completed = sector_nr;
6185 if (!mddev->reshape_backwards)
6186 /* Can update recovery_offset */
6187 rdev_for_each(rdev, mddev)
6188 if (rdev->raid_disk >= 0 &&
6189 !test_bit(Journal, &rdev->flags) &&
6190 !test_bit(In_sync, &rdev->flags) &&
6191 rdev->recovery_offset < sector_nr)
6192 rdev->recovery_offset = sector_nr;
6193 conf->reshape_checkpoint = jiffies;
6194 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
6195 md_wakeup_thread(mddev->thread);
6196 wait_event(mddev->sb_wait,
6197 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
6198 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6199 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6201 spin_lock_irq(&conf->device_lock);
6202 conf->reshape_safe = mddev->reshape_position;
6203 spin_unlock_irq(&conf->device_lock);
6204 wake_up(&conf->wait_for_overlap);
6205 sysfs_notify_dirent_safe(mddev->sysfs_completed);
6211 static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6214 struct r5conf *conf = mddev->private;
6215 struct stripe_head *sh;
6216 sector_t max_sector = mddev->dev_sectors;
6217 sector_t sync_blocks;
6218 int still_degraded = 0;
6221 if (sector_nr >= max_sector) {
6222 /* just being told to finish up .. nothing much to do */
6224 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6229 if (mddev->curr_resync < max_sector) /* aborted */
6230 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6232 else /* completed sync */
6234 md_bitmap_close_sync(mddev->bitmap);
6239 /* Allow raid5_quiesce to complete */
6240 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6242 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6243 return reshape_request(mddev, sector_nr, skipped);
6245 /* No need to check resync_max as we never do more than one
6246 * stripe, and as resync_max will always be on a chunk boundary,
6247 * if the check in md_do_sync didn't fire, there is no chance
6248 * of overstepping resync_max here
6251 /* if there is too many failed drives and we are trying
6252 * to resync, then assert that we are finished, because there is
6253 * nothing we can do.
6255 if (mddev->degraded >= conf->max_degraded &&
6256 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6257 sector_t rv = mddev->dev_sectors - sector_nr;
6261 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6263 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
6264 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
6265 /* we can skip this block, and probably more */
6266 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf));
6268 /* keep things rounded to whole stripes */
6269 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
6272 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
6274 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
6276 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
6277 /* make sure we don't swamp the stripe cache if someone else
6278 * is trying to get access
6280 schedule_timeout_uninterruptible(1);
6282 /* Need to check if array will still be degraded after recovery/resync
6283 * Note in case of > 1 drive failures it's possible we're rebuilding
6284 * one drive while leaving another faulty drive in array.
6287 for (i = 0; i < conf->raid_disks; i++) {
6288 struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
6290 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
6295 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
6297 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
6298 set_bit(STRIPE_HANDLE, &sh->state);
6300 raid5_release_stripe(sh);
6302 return RAID5_STRIPE_SECTORS(conf);
6305 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6306 unsigned int offset)
6308 /* We may not be able to submit a whole bio at once as there
6309 * may not be enough stripe_heads available.
6310 * We cannot pre-allocate enough stripe_heads as we may need
6311 * more than exist in the cache (if we allow ever large chunks).
6312 * So we do one stripe head at a time and record in
6313 * ->bi_hw_segments how many have been done.
6315 * We *know* that this entire raid_bio is in one chunk, so
6316 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6318 struct stripe_head *sh;
6320 sector_t sector, logical_sector, last_sector;
6324 logical_sector = raid_bio->bi_iter.bi_sector &
6325 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
6326 sector = raid5_compute_sector(conf, logical_sector,
6328 last_sector = bio_end_sector(raid_bio);
6330 for (; logical_sector < last_sector;
6331 logical_sector += RAID5_STRIPE_SECTORS(conf),
6332 sector += RAID5_STRIPE_SECTORS(conf),
6336 /* already done this stripe */
6339 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
6342 /* failed to get a stripe - must wait */
6343 conf->retry_read_aligned = raid_bio;
6344 conf->retry_read_offset = scnt;
6348 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
6349 raid5_release_stripe(sh);
6350 conf->retry_read_aligned = raid_bio;
6351 conf->retry_read_offset = scnt;
6355 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
6357 raid5_release_stripe(sh);
6361 bio_endio(raid_bio);
6363 if (atomic_dec_and_test(&conf->active_aligned_reads))
6364 wake_up(&conf->wait_for_quiescent);
6368 static int handle_active_stripes(struct r5conf *conf, int group,
6369 struct r5worker *worker,
6370 struct list_head *temp_inactive_list)
6371 __releases(&conf->device_lock)
6372 __acquires(&conf->device_lock)
6374 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
6375 int i, batch_size = 0, hash;
6376 bool release_inactive = false;
6378 while (batch_size < MAX_STRIPE_BATCH &&
6379 (sh = __get_priority_stripe(conf, group)) != NULL)
6380 batch[batch_size++] = sh;
6382 if (batch_size == 0) {
6383 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6384 if (!list_empty(temp_inactive_list + i))
6386 if (i == NR_STRIPE_HASH_LOCKS) {
6387 spin_unlock_irq(&conf->device_lock);
6388 log_flush_stripe_to_raid(conf);
6389 spin_lock_irq(&conf->device_lock);
6392 release_inactive = true;
6394 spin_unlock_irq(&conf->device_lock);
6396 release_inactive_stripe_list(conf, temp_inactive_list,
6397 NR_STRIPE_HASH_LOCKS);
6399 r5l_flush_stripe_to_raid(conf->log);
6400 if (release_inactive) {
6401 spin_lock_irq(&conf->device_lock);
6405 for (i = 0; i < batch_size; i++)
6406 handle_stripe(batch[i]);
6407 log_write_stripe_run(conf);
6411 spin_lock_irq(&conf->device_lock);
6412 for (i = 0; i < batch_size; i++) {
6413 hash = batch[i]->hash_lock_index;
6414 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6419 static void raid5_do_work(struct work_struct *work)
6421 struct r5worker *worker = container_of(work, struct r5worker, work);
6422 struct r5worker_group *group = worker->group;
6423 struct r5conf *conf = group->conf;
6424 struct mddev *mddev = conf->mddev;
6425 int group_id = group - conf->worker_groups;
6427 struct blk_plug plug;
6429 pr_debug("+++ raid5worker active\n");
6431 blk_start_plug(&plug);
6433 spin_lock_irq(&conf->device_lock);
6435 int batch_size, released;
6437 released = release_stripe_list(conf, worker->temp_inactive_list);
6439 batch_size = handle_active_stripes(conf, group_id, worker,
6440 worker->temp_inactive_list);
6441 worker->working = false;
6442 if (!batch_size && !released)
6444 handled += batch_size;
6445 wait_event_lock_irq(mddev->sb_wait,
6446 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6449 pr_debug("%d stripes handled\n", handled);
6451 spin_unlock_irq(&conf->device_lock);
6453 flush_deferred_bios(conf);
6455 r5l_flush_stripe_to_raid(conf->log);
6457 async_tx_issue_pending_all();
6458 blk_finish_plug(&plug);
6460 pr_debug("--- raid5worker inactive\n");
6464 * This is our raid5 kernel thread.
6466 * We scan the hash table for stripes which can be handled now.
6467 * During the scan, completed stripes are saved for us by the interrupt
6468 * handler, so that they will not have to wait for our next wakeup.
6470 static void raid5d(struct md_thread *thread)
6472 struct mddev *mddev = thread->mddev;
6473 struct r5conf *conf = mddev->private;
6475 struct blk_plug plug;
6477 pr_debug("+++ raid5d active\n");
6479 md_check_recovery(mddev);
6481 blk_start_plug(&plug);
6483 spin_lock_irq(&conf->device_lock);
6486 int batch_size, released;
6487 unsigned int offset;
6489 released = release_stripe_list(conf, conf->temp_inactive_list);
6491 clear_bit(R5_DID_ALLOC, &conf->cache_state);
6494 !list_empty(&conf->bitmap_list)) {
6495 /* Now is a good time to flush some bitmap updates */
6497 spin_unlock_irq(&conf->device_lock);
6498 md_bitmap_unplug(mddev->bitmap);
6499 spin_lock_irq(&conf->device_lock);
6500 conf->seq_write = conf->seq_flush;
6501 activate_bit_delay(conf, conf->temp_inactive_list);
6503 raid5_activate_delayed(conf);
6505 while ((bio = remove_bio_from_retry(conf, &offset))) {
6507 spin_unlock_irq(&conf->device_lock);
6508 ok = retry_aligned_read(conf, bio, offset);
6509 spin_lock_irq(&conf->device_lock);
6515 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6516 conf->temp_inactive_list);
6517 if (!batch_size && !released)
6519 handled += batch_size;
6521 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
6522 spin_unlock_irq(&conf->device_lock);
6523 md_check_recovery(mddev);
6524 spin_lock_irq(&conf->device_lock);
6527 * Waiting on MD_SB_CHANGE_PENDING below may deadlock
6528 * seeing md_check_recovery() is needed to clear
6529 * the flag when using mdmon.
6534 wait_event_lock_irq(mddev->sb_wait,
6535 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6538 pr_debug("%d stripes handled\n", handled);
6540 spin_unlock_irq(&conf->device_lock);
6541 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6542 mutex_trylock(&conf->cache_size_mutex)) {
6543 grow_one_stripe(conf, __GFP_NOWARN);
6544 /* Set flag even if allocation failed. This helps
6545 * slow down allocation requests when mem is short
6547 set_bit(R5_DID_ALLOC, &conf->cache_state);
6548 mutex_unlock(&conf->cache_size_mutex);
6551 flush_deferred_bios(conf);
6553 r5l_flush_stripe_to_raid(conf->log);
6555 async_tx_issue_pending_all();
6556 blk_finish_plug(&plug);
6558 pr_debug("--- raid5d inactive\n");
6562 raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
6564 struct r5conf *conf;
6566 spin_lock(&mddev->lock);
6567 conf = mddev->private;
6569 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
6570 spin_unlock(&mddev->lock);
6575 raid5_set_cache_size(struct mddev *mddev, int size)
6578 struct r5conf *conf = mddev->private;
6580 if (size <= 16 || size > 32768)
6583 conf->min_nr_stripes = size;
6584 mutex_lock(&conf->cache_size_mutex);
6585 while (size < conf->max_nr_stripes &&
6586 drop_one_stripe(conf))
6588 mutex_unlock(&conf->cache_size_mutex);
6590 md_allow_write(mddev);
6592 mutex_lock(&conf->cache_size_mutex);
6593 while (size > conf->max_nr_stripes)
6594 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6595 conf->min_nr_stripes = conf->max_nr_stripes;
6599 mutex_unlock(&conf->cache_size_mutex);
6603 EXPORT_SYMBOL(raid5_set_cache_size);
6606 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
6608 struct r5conf *conf;
6612 if (len >= PAGE_SIZE)
6614 if (kstrtoul(page, 10, &new))
6616 err = mddev_lock(mddev);
6619 conf = mddev->private;
6623 err = raid5_set_cache_size(mddev, new);
6624 mddev_unlock(mddev);
6629 static struct md_sysfs_entry
6630 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6631 raid5_show_stripe_cache_size,
6632 raid5_store_stripe_cache_size);
6635 raid5_show_rmw_level(struct mddev *mddev, char *page)
6637 struct r5conf *conf = mddev->private;
6639 return sprintf(page, "%d\n", conf->rmw_level);
6645 raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6647 struct r5conf *conf = mddev->private;
6653 if (len >= PAGE_SIZE)
6656 if (kstrtoul(page, 10, &new))
6659 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6662 if (new != PARITY_DISABLE_RMW &&
6663 new != PARITY_ENABLE_RMW &&
6664 new != PARITY_PREFER_RMW)
6667 conf->rmw_level = new;
6671 static struct md_sysfs_entry
6672 raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6673 raid5_show_rmw_level,
6674 raid5_store_rmw_level);
6677 raid5_show_stripe_size(struct mddev *mddev, char *page)
6679 struct r5conf *conf;
6682 spin_lock(&mddev->lock);
6683 conf = mddev->private;
6685 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf));
6686 spin_unlock(&mddev->lock);
6690 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
6692 raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len)
6694 struct r5conf *conf;
6699 if (len >= PAGE_SIZE)
6701 if (kstrtoul(page, 10, &new))
6705 * The value should not be bigger than PAGE_SIZE. It requires to
6706 * be multiple of DEFAULT_STRIPE_SIZE and the value should be power
6709 if (new % DEFAULT_STRIPE_SIZE != 0 ||
6710 new > PAGE_SIZE || new == 0 ||
6711 new != roundup_pow_of_two(new))
6714 err = mddev_lock(mddev);
6718 conf = mddev->private;
6724 if (new == conf->stripe_size)
6727 pr_debug("md/raid: change stripe_size from %lu to %lu\n",
6728 conf->stripe_size, new);
6730 if (mddev->sync_thread ||
6731 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6732 mddev->reshape_position != MaxSector ||
6733 mddev->sysfs_active) {
6738 mddev_suspend(mddev);
6739 mutex_lock(&conf->cache_size_mutex);
6740 size = conf->max_nr_stripes;
6742 shrink_stripes(conf);
6744 conf->stripe_size = new;
6745 conf->stripe_shift = ilog2(new) - 9;
6746 conf->stripe_sectors = new >> 9;
6747 if (grow_stripes(conf, size)) {
6748 pr_warn("md/raid:%s: couldn't allocate buffers\n",
6752 mutex_unlock(&conf->cache_size_mutex);
6753 mddev_resume(mddev);
6756 mddev_unlock(mddev);
6760 static struct md_sysfs_entry
6761 raid5_stripe_size = __ATTR(stripe_size, 0644,
6762 raid5_show_stripe_size,
6763 raid5_store_stripe_size);
6765 static struct md_sysfs_entry
6766 raid5_stripe_size = __ATTR(stripe_size, 0444,
6767 raid5_show_stripe_size,
6772 raid5_show_preread_threshold(struct mddev *mddev, char *page)
6774 struct r5conf *conf;
6776 spin_lock(&mddev->lock);
6777 conf = mddev->private;
6779 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6780 spin_unlock(&mddev->lock);
6785 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
6787 struct r5conf *conf;
6791 if (len >= PAGE_SIZE)
6793 if (kstrtoul(page, 10, &new))
6796 err = mddev_lock(mddev);
6799 conf = mddev->private;
6802 else if (new > conf->min_nr_stripes)
6805 conf->bypass_threshold = new;
6806 mddev_unlock(mddev);
6810 static struct md_sysfs_entry
6811 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6813 raid5_show_preread_threshold,
6814 raid5_store_preread_threshold);
6817 raid5_show_skip_copy(struct mddev *mddev, char *page)
6819 struct r5conf *conf;
6821 spin_lock(&mddev->lock);
6822 conf = mddev->private;
6824 ret = sprintf(page, "%d\n", conf->skip_copy);
6825 spin_unlock(&mddev->lock);
6830 raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6832 struct r5conf *conf;
6836 if (len >= PAGE_SIZE)
6838 if (kstrtoul(page, 10, &new))
6842 err = mddev_lock(mddev);
6845 conf = mddev->private;
6848 else if (new != conf->skip_copy) {
6849 struct request_queue *q = mddev->queue;
6851 mddev_suspend(mddev);
6852 conf->skip_copy = new;
6854 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
6856 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
6857 mddev_resume(mddev);
6859 mddev_unlock(mddev);
6863 static struct md_sysfs_entry
6864 raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6865 raid5_show_skip_copy,
6866 raid5_store_skip_copy);
6869 stripe_cache_active_show(struct mddev *mddev, char *page)
6871 struct r5conf *conf = mddev->private;
6873 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6878 static struct md_sysfs_entry
6879 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
6882 raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6884 struct r5conf *conf;
6886 spin_lock(&mddev->lock);
6887 conf = mddev->private;
6889 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6890 spin_unlock(&mddev->lock);
6894 static int alloc_thread_groups(struct r5conf *conf, int cnt,
6896 struct r5worker_group **worker_groups);
6898 raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6900 struct r5conf *conf;
6903 struct r5worker_group *new_groups, *old_groups;
6906 if (len >= PAGE_SIZE)
6908 if (kstrtouint(page, 10, &new))
6910 /* 8192 should be big enough */
6914 err = mddev_lock(mddev);
6917 conf = mddev->private;
6920 else if (new != conf->worker_cnt_per_group) {
6921 mddev_suspend(mddev);
6923 old_groups = conf->worker_groups;
6925 flush_workqueue(raid5_wq);
6927 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
6929 spin_lock_irq(&conf->device_lock);
6930 conf->group_cnt = group_cnt;
6931 conf->worker_cnt_per_group = new;
6932 conf->worker_groups = new_groups;
6933 spin_unlock_irq(&conf->device_lock);
6936 kfree(old_groups[0].workers);
6939 mddev_resume(mddev);
6941 mddev_unlock(mddev);
6946 static struct md_sysfs_entry
6947 raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6948 raid5_show_group_thread_cnt,
6949 raid5_store_group_thread_cnt);
6951 static struct attribute *raid5_attrs[] = {
6952 &raid5_stripecache_size.attr,
6953 &raid5_stripecache_active.attr,
6954 &raid5_preread_bypass_threshold.attr,
6955 &raid5_group_thread_cnt.attr,
6956 &raid5_skip_copy.attr,
6957 &raid5_rmw_level.attr,
6958 &raid5_stripe_size.attr,
6959 &r5c_journal_mode.attr,
6960 &ppl_write_hint.attr,
6963 static const struct attribute_group raid5_attrs_group = {
6965 .attrs = raid5_attrs,
6968 static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
6969 struct r5worker_group **worker_groups)
6973 struct r5worker *workers;
6977 *worker_groups = NULL;
6980 *group_cnt = num_possible_nodes();
6981 size = sizeof(struct r5worker) * cnt;
6982 workers = kcalloc(size, *group_cnt, GFP_NOIO);
6983 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
6985 if (!*worker_groups || !workers) {
6987 kfree(*worker_groups);
6991 for (i = 0; i < *group_cnt; i++) {
6992 struct r5worker_group *group;
6994 group = &(*worker_groups)[i];
6995 INIT_LIST_HEAD(&group->handle_list);
6996 INIT_LIST_HEAD(&group->loprio_list);
6998 group->workers = workers + i * cnt;
7000 for (j = 0; j < cnt; j++) {
7001 struct r5worker *worker = group->workers + j;
7002 worker->group = group;
7003 INIT_WORK(&worker->work, raid5_do_work);
7005 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
7006 INIT_LIST_HEAD(worker->temp_inactive_list + k);
7013 static void free_thread_groups(struct r5conf *conf)
7015 if (conf->worker_groups)
7016 kfree(conf->worker_groups[0].workers);
7017 kfree(conf->worker_groups);
7018 conf->worker_groups = NULL;
7022 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
7024 struct r5conf *conf = mddev->private;
7027 sectors = mddev->dev_sectors;
7029 /* size is defined by the smallest of previous and new size */
7030 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
7032 sectors &= ~((sector_t)conf->chunk_sectors - 1);
7033 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
7034 return sectors * (raid_disks - conf->max_degraded);
7037 static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7039 safe_put_page(percpu->spare_page);
7040 percpu->spare_page = NULL;
7041 kvfree(percpu->scribble);
7042 percpu->scribble = NULL;
7045 static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7047 if (conf->level == 6 && !percpu->spare_page) {
7048 percpu->spare_page = alloc_page(GFP_KERNEL);
7049 if (!percpu->spare_page)
7053 if (scribble_alloc(percpu,
7054 max(conf->raid_disks,
7055 conf->previous_raid_disks),
7056 max(conf->chunk_sectors,
7057 conf->prev_chunk_sectors)
7058 / RAID5_STRIPE_SECTORS(conf))) {
7059 free_scratch_buffer(conf, percpu);
7066 static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
7068 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
7070 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
7074 static void raid5_free_percpu(struct r5conf *conf)
7079 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
7080 free_percpu(conf->percpu);
7083 static void free_conf(struct r5conf *conf)
7089 unregister_shrinker(&conf->shrinker);
7090 free_thread_groups(conf);
7091 shrink_stripes(conf);
7092 raid5_free_percpu(conf);
7093 for (i = 0; i < conf->pool_size; i++)
7094 if (conf->disks[i].extra_page)
7095 put_page(conf->disks[i].extra_page);
7097 bioset_exit(&conf->bio_split);
7098 kfree(conf->stripe_hashtbl);
7099 kfree(conf->pending_data);
7103 static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
7105 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
7106 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
7108 if (alloc_scratch_buffer(conf, percpu)) {
7109 pr_warn("%s: failed memory allocation for cpu%u\n",
7116 static int raid5_alloc_percpu(struct r5conf *conf)
7120 conf->percpu = alloc_percpu(struct raid5_percpu);
7124 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
7126 conf->scribble_disks = max(conf->raid_disks,
7127 conf->previous_raid_disks);
7128 conf->scribble_sectors = max(conf->chunk_sectors,
7129 conf->prev_chunk_sectors);
7134 static unsigned long raid5_cache_scan(struct shrinker *shrink,
7135 struct shrink_control *sc)
7137 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
7138 unsigned long ret = SHRINK_STOP;
7140 if (mutex_trylock(&conf->cache_size_mutex)) {
7142 while (ret < sc->nr_to_scan &&
7143 conf->max_nr_stripes > conf->min_nr_stripes) {
7144 if (drop_one_stripe(conf) == 0) {
7150 mutex_unlock(&conf->cache_size_mutex);
7155 static unsigned long raid5_cache_count(struct shrinker *shrink,
7156 struct shrink_control *sc)
7158 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
7160 if (conf->max_nr_stripes < conf->min_nr_stripes)
7161 /* unlikely, but not impossible */
7163 return conf->max_nr_stripes - conf->min_nr_stripes;
7166 static struct r5conf *setup_conf(struct mddev *mddev)
7168 struct r5conf *conf;
7169 int raid_disk, memory, max_disks;
7170 struct md_rdev *rdev;
7171 struct disk_info *disk;
7175 struct r5worker_group *new_group;
7178 if (mddev->new_level != 5
7179 && mddev->new_level != 4
7180 && mddev->new_level != 6) {
7181 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
7182 mdname(mddev), mddev->new_level);
7183 return ERR_PTR(-EIO);
7185 if ((mddev->new_level == 5
7186 && !algorithm_valid_raid5(mddev->new_layout)) ||
7187 (mddev->new_level == 6
7188 && !algorithm_valid_raid6(mddev->new_layout))) {
7189 pr_warn("md/raid:%s: layout %d not supported\n",
7190 mdname(mddev), mddev->new_layout);
7191 return ERR_PTR(-EIO);
7193 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
7194 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
7195 mdname(mddev), mddev->raid_disks);
7196 return ERR_PTR(-EINVAL);
7199 if (!mddev->new_chunk_sectors ||
7200 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
7201 !is_power_of_2(mddev->new_chunk_sectors)) {
7202 pr_warn("md/raid:%s: invalid chunk size %d\n",
7203 mdname(mddev), mddev->new_chunk_sectors << 9);
7204 return ERR_PTR(-EINVAL);
7207 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
7211 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
7212 conf->stripe_size = DEFAULT_STRIPE_SIZE;
7213 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
7214 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9;
7216 INIT_LIST_HEAD(&conf->free_list);
7217 INIT_LIST_HEAD(&conf->pending_list);
7218 conf->pending_data = kcalloc(PENDING_IO_MAX,
7219 sizeof(struct r5pending_data),
7221 if (!conf->pending_data)
7223 for (i = 0; i < PENDING_IO_MAX; i++)
7224 list_add(&conf->pending_data[i].sibling, &conf->free_list);
7225 /* Don't enable multi-threading by default*/
7226 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
7227 conf->group_cnt = group_cnt;
7228 conf->worker_cnt_per_group = 0;
7229 conf->worker_groups = new_group;
7232 spin_lock_init(&conf->device_lock);
7233 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock);
7234 mutex_init(&conf->cache_size_mutex);
7235 init_waitqueue_head(&conf->wait_for_quiescent);
7236 init_waitqueue_head(&conf->wait_for_stripe);
7237 init_waitqueue_head(&conf->wait_for_overlap);
7238 INIT_LIST_HEAD(&conf->handle_list);
7239 INIT_LIST_HEAD(&conf->loprio_list);
7240 INIT_LIST_HEAD(&conf->hold_list);
7241 INIT_LIST_HEAD(&conf->delayed_list);
7242 INIT_LIST_HEAD(&conf->bitmap_list);
7243 init_llist_head(&conf->released_stripes);
7244 atomic_set(&conf->active_stripes, 0);
7245 atomic_set(&conf->preread_active_stripes, 0);
7246 atomic_set(&conf->active_aligned_reads, 0);
7247 spin_lock_init(&conf->pending_bios_lock);
7248 conf->batch_bio_dispatch = true;
7249 rdev_for_each(rdev, mddev) {
7250 if (test_bit(Journal, &rdev->flags))
7252 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
7253 conf->batch_bio_dispatch = false;
7258 conf->bypass_threshold = BYPASS_THRESHOLD;
7259 conf->recovery_disabled = mddev->recovery_disabled - 1;
7261 conf->raid_disks = mddev->raid_disks;
7262 if (mddev->reshape_position == MaxSector)
7263 conf->previous_raid_disks = mddev->raid_disks;
7265 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
7266 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
7268 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
7274 for (i = 0; i < max_disks; i++) {
7275 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7276 if (!conf->disks[i].extra_page)
7280 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7283 conf->mddev = mddev;
7285 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
7288 /* We init hash_locks[0] separately to that it can be used
7289 * as the reference lock in the spin_lock_nest_lock() call
7290 * in lock_all_device_hash_locks_irq in order to convince
7291 * lockdep that we know what we are doing.
7293 spin_lock_init(conf->hash_locks);
7294 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7295 spin_lock_init(conf->hash_locks + i);
7297 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7298 INIT_LIST_HEAD(conf->inactive_list + i);
7300 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7301 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7303 atomic_set(&conf->r5c_cached_full_stripes, 0);
7304 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7305 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7306 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
7307 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7308 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
7310 conf->level = mddev->new_level;
7311 conf->chunk_sectors = mddev->new_chunk_sectors;
7312 if (raid5_alloc_percpu(conf) != 0)
7315 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
7317 rdev_for_each(rdev, mddev) {
7318 raid_disk = rdev->raid_disk;
7319 if (raid_disk >= max_disks
7320 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
7322 disk = conf->disks + raid_disk;
7324 if (test_bit(Replacement, &rdev->flags)) {
7325 if (disk->replacement)
7327 disk->replacement = rdev;
7334 if (test_bit(In_sync, &rdev->flags)) {
7335 char b[BDEVNAME_SIZE];
7336 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
7337 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
7338 } else if (rdev->saved_raid_disk != raid_disk)
7339 /* Cannot rely on bitmap to complete recovery */
7343 conf->level = mddev->new_level;
7344 if (conf->level == 6) {
7345 conf->max_degraded = 2;
7346 if (raid6_call.xor_syndrome)
7347 conf->rmw_level = PARITY_ENABLE_RMW;
7349 conf->rmw_level = PARITY_DISABLE_RMW;
7351 conf->max_degraded = 1;
7352 conf->rmw_level = PARITY_ENABLE_RMW;
7354 conf->algorithm = mddev->new_layout;
7355 conf->reshape_progress = mddev->reshape_position;
7356 if (conf->reshape_progress != MaxSector) {
7357 conf->prev_chunk_sectors = mddev->chunk_sectors;
7358 conf->prev_algo = mddev->layout;
7360 conf->prev_chunk_sectors = conf->chunk_sectors;
7361 conf->prev_algo = conf->algorithm;
7364 conf->min_nr_stripes = NR_STRIPES;
7365 if (mddev->reshape_position != MaxSector) {
7366 int stripes = max_t(int,
7367 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7368 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
7369 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7370 if (conf->min_nr_stripes != NR_STRIPES)
7371 pr_info("md/raid:%s: force stripe size %d for reshape\n",
7372 mdname(mddev), conf->min_nr_stripes);
7374 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
7375 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
7376 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
7377 if (grow_stripes(conf, conf->min_nr_stripes)) {
7378 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7379 mdname(mddev), memory);
7382 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
7384 * Losing a stripe head costs more than the time to refill it,
7385 * it reduces the queue depth and so can hurt throughput.
7386 * So set it rather large, scaled by number of devices.
7388 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7389 conf->shrinker.scan_objects = raid5_cache_scan;
7390 conf->shrinker.count_objects = raid5_cache_count;
7391 conf->shrinker.batch = 128;
7392 conf->shrinker.flags = 0;
7393 if (register_shrinker(&conf->shrinker)) {
7394 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7399 sprintf(pers_name, "raid%d", mddev->new_level);
7400 conf->thread = md_register_thread(raid5d, mddev, pers_name);
7401 if (!conf->thread) {
7402 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7412 return ERR_PTR(-EIO);
7414 return ERR_PTR(-ENOMEM);
7417 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7420 case ALGORITHM_PARITY_0:
7421 if (raid_disk < max_degraded)
7424 case ALGORITHM_PARITY_N:
7425 if (raid_disk >= raid_disks - max_degraded)
7428 case ALGORITHM_PARITY_0_6:
7429 if (raid_disk == 0 ||
7430 raid_disk == raid_disks - 1)
7433 case ALGORITHM_LEFT_ASYMMETRIC_6:
7434 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7435 case ALGORITHM_LEFT_SYMMETRIC_6:
7436 case ALGORITHM_RIGHT_SYMMETRIC_6:
7437 if (raid_disk == raid_disks - 1)
7443 static void raid5_set_io_opt(struct r5conf *conf)
7445 blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) *
7446 (conf->raid_disks - conf->max_degraded));
7449 static int raid5_run(struct mddev *mddev)
7451 struct r5conf *conf;
7452 int working_disks = 0;
7453 int dirty_parity_disks = 0;
7454 struct md_rdev *rdev;
7455 struct md_rdev *journal_dev = NULL;
7456 sector_t reshape_offset = 0;
7458 long long min_offset_diff = 0;
7461 if (acct_bioset_init(mddev)) {
7462 pr_err("md/raid456:%s: alloc acct bioset failed.\n", mdname(mddev));
7466 if (mddev_init_writes_pending(mddev) < 0) {
7471 if (mddev->recovery_cp != MaxSector)
7472 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7475 rdev_for_each(rdev, mddev) {
7478 if (test_bit(Journal, &rdev->flags)) {
7482 if (rdev->raid_disk < 0)
7484 diff = (rdev->new_data_offset - rdev->data_offset);
7486 min_offset_diff = diff;
7488 } else if (mddev->reshape_backwards &&
7489 diff < min_offset_diff)
7490 min_offset_diff = diff;
7491 else if (!mddev->reshape_backwards &&
7492 diff > min_offset_diff)
7493 min_offset_diff = diff;
7496 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7497 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7498 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7504 if (mddev->reshape_position != MaxSector) {
7505 /* Check that we can continue the reshape.
7506 * Difficulties arise if the stripe we would write to
7507 * next is at or after the stripe we would read from next.
7508 * For a reshape that changes the number of devices, this
7509 * is only possible for a very short time, and mdadm makes
7510 * sure that time appears to have past before assembling
7511 * the array. So we fail if that time hasn't passed.
7512 * For a reshape that keeps the number of devices the same
7513 * mdadm must be monitoring the reshape can keeping the
7514 * critical areas read-only and backed up. It will start
7515 * the array in read-only mode, so we check for that.
7517 sector_t here_new, here_old;
7519 int max_degraded = (mddev->level == 6 ? 2 : 1);
7524 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7530 if (mddev->new_level != mddev->level) {
7531 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7536 old_disks = mddev->raid_disks - mddev->delta_disks;
7537 /* reshape_position must be on a new-stripe boundary, and one
7538 * further up in new geometry must map after here in old
7540 * If the chunk sizes are different, then as we perform reshape
7541 * in units of the largest of the two, reshape_position needs
7542 * be a multiple of the largest chunk size times new data disks.
7544 here_new = mddev->reshape_position;
7545 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7546 new_data_disks = mddev->raid_disks - max_degraded;
7547 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
7548 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7553 reshape_offset = here_new * chunk_sectors;
7554 /* here_new is the stripe we will write to */
7555 here_old = mddev->reshape_position;
7556 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
7557 /* here_old is the first stripe that we might need to read
7559 if (mddev->delta_disks == 0) {
7560 /* We cannot be sure it is safe to start an in-place
7561 * reshape. It is only safe if user-space is monitoring
7562 * and taking constant backups.
7563 * mdadm always starts a situation like this in
7564 * readonly mode so it can take control before
7565 * allowing any writes. So just check for that.
7567 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7568 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7569 /* not really in-place - so OK */;
7570 else if (mddev->ro == 0) {
7571 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7576 } else if (mddev->reshape_backwards
7577 ? (here_new * chunk_sectors + min_offset_diff <=
7578 here_old * chunk_sectors)
7579 : (here_new * chunk_sectors >=
7580 here_old * chunk_sectors + (-min_offset_diff))) {
7581 /* Reading from the same stripe as writing to - bad */
7582 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7587 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
7588 /* OK, we should be able to continue; */
7590 BUG_ON(mddev->level != mddev->new_level);
7591 BUG_ON(mddev->layout != mddev->new_layout);
7592 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
7593 BUG_ON(mddev->delta_disks != 0);
7596 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7597 test_bit(MD_HAS_PPL, &mddev->flags)) {
7598 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7600 clear_bit(MD_HAS_PPL, &mddev->flags);
7601 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
7604 if (mddev->private == NULL)
7605 conf = setup_conf(mddev);
7607 conf = mddev->private;
7610 ret = PTR_ERR(conf);
7614 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7616 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7619 set_disk_ro(mddev->gendisk, 1);
7620 } else if (mddev->recovery_cp == MaxSector)
7621 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
7624 conf->min_offset_diff = min_offset_diff;
7625 mddev->thread = conf->thread;
7626 conf->thread = NULL;
7627 mddev->private = conf;
7629 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7631 rdev = conf->disks[i].rdev;
7632 if (!rdev && conf->disks[i].replacement) {
7633 /* The replacement is all we have yet */
7634 rdev = conf->disks[i].replacement;
7635 conf->disks[i].replacement = NULL;
7636 clear_bit(Replacement, &rdev->flags);
7637 conf->disks[i].rdev = rdev;
7641 if (conf->disks[i].replacement &&
7642 conf->reshape_progress != MaxSector) {
7643 /* replacements and reshape simply do not mix. */
7644 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
7647 if (test_bit(In_sync, &rdev->flags)) {
7651 /* This disc is not fully in-sync. However if it
7652 * just stored parity (beyond the recovery_offset),
7653 * when we don't need to be concerned about the
7654 * array being dirty.
7655 * When reshape goes 'backwards', we never have
7656 * partially completed devices, so we only need
7657 * to worry about reshape going forwards.
7659 /* Hack because v0.91 doesn't store recovery_offset properly. */
7660 if (mddev->major_version == 0 &&
7661 mddev->minor_version > 90)
7662 rdev->recovery_offset = reshape_offset;
7664 if (rdev->recovery_offset < reshape_offset) {
7665 /* We need to check old and new layout */
7666 if (!only_parity(rdev->raid_disk,
7669 conf->max_degraded))
7672 if (!only_parity(rdev->raid_disk,
7674 conf->previous_raid_disks,
7675 conf->max_degraded))
7677 dirty_parity_disks++;
7681 * 0 for a fully functional array, 1 or 2 for a degraded array.
7683 mddev->degraded = raid5_calc_degraded(conf);
7685 if (has_failed(conf)) {
7686 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
7687 mdname(mddev), mddev->degraded, conf->raid_disks);
7691 /* device size must be a multiple of chunk size */
7692 mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1);
7693 mddev->resync_max_sectors = mddev->dev_sectors;
7695 if (mddev->degraded > dirty_parity_disks &&
7696 mddev->recovery_cp != MaxSector) {
7697 if (test_bit(MD_HAS_PPL, &mddev->flags))
7698 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7700 else if (mddev->ok_start_degraded)
7701 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7704 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7710 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7711 mdname(mddev), conf->level,
7712 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7715 print_raid5_conf(conf);
7717 if (conf->reshape_progress != MaxSector) {
7718 conf->reshape_safe = conf->reshape_progress;
7719 atomic_set(&conf->reshape_stripes, 0);
7720 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7721 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7722 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7723 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7724 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
7726 if (!mddev->sync_thread)
7730 /* Ok, everything is just fine now */
7731 if (mddev->to_remove == &raid5_attrs_group)
7732 mddev->to_remove = NULL;
7733 else if (mddev->kobj.sd &&
7734 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
7735 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7737 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7741 /* read-ahead size must cover two whole stripes, which
7742 * is 2 * (datadisks) * chunksize where 'n' is the
7743 * number of raid devices
7745 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7746 int stripe = data_disks *
7747 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
7749 chunk_size = mddev->chunk_sectors << 9;
7750 blk_queue_io_min(mddev->queue, chunk_size);
7751 raid5_set_io_opt(conf);
7752 mddev->queue->limits.raid_partial_stripes_expensive = 1;
7754 * We can only discard a whole stripe. It doesn't make sense to
7755 * discard data disk but write parity disk
7757 stripe = stripe * PAGE_SIZE;
7758 /* Round up to power of 2, as discard handling
7759 * currently assumes that */
7760 while ((stripe-1) & stripe)
7761 stripe = (stripe | (stripe-1)) + 1;
7762 mddev->queue->limits.discard_alignment = stripe;
7763 mddev->queue->limits.discard_granularity = stripe;
7765 blk_queue_max_write_same_sectors(mddev->queue, 0);
7766 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
7768 rdev_for_each(rdev, mddev) {
7769 disk_stack_limits(mddev->gendisk, rdev->bdev,
7770 rdev->data_offset << 9);
7771 disk_stack_limits(mddev->gendisk, rdev->bdev,
7772 rdev->new_data_offset << 9);
7776 * zeroing is required, otherwise data
7777 * could be lost. Consider a scenario: discard a stripe
7778 * (the stripe could be inconsistent if
7779 * discard_zeroes_data is 0); write one disk of the
7780 * stripe (the stripe could be inconsistent again
7781 * depending on which disks are used to calculate
7782 * parity); the disk is broken; The stripe data of this
7785 * We only allow DISCARD if the sysadmin has confirmed that
7786 * only safe devices are in use by setting a module parameter.
7787 * A better idea might be to turn DISCARD into WRITE_ZEROES
7788 * requests, as that is required to be safe.
7790 if (devices_handle_discard_safely &&
7791 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7792 mddev->queue->limits.discard_granularity >= stripe)
7793 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
7796 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
7799 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
7802 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
7807 md_unregister_thread(&mddev->thread);
7808 print_raid5_conf(conf);
7810 mddev->private = NULL;
7811 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
7814 acct_bioset_exit(mddev);
7818 static void raid5_free(struct mddev *mddev, void *priv)
7820 struct r5conf *conf = priv;
7823 acct_bioset_exit(mddev);
7824 mddev->to_remove = &raid5_attrs_group;
7827 static void raid5_status(struct seq_file *seq, struct mddev *mddev)
7829 struct r5conf *conf = mddev->private;
7832 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
7833 conf->chunk_sectors / 2, mddev->layout);
7834 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
7836 for (i = 0; i < conf->raid_disks; i++) {
7837 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7838 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7841 seq_printf (seq, "]");
7844 static void print_raid5_conf (struct r5conf *conf)
7847 struct disk_info *tmp;
7849 pr_debug("RAID conf printout:\n");
7851 pr_debug("(conf==NULL)\n");
7854 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
7856 conf->raid_disks - conf->mddev->degraded);
7858 for (i = 0; i < conf->raid_disks; i++) {
7859 char b[BDEVNAME_SIZE];
7860 tmp = conf->disks + i;
7862 pr_debug(" disk %d, o:%d, dev:%s\n",
7863 i, !test_bit(Faulty, &tmp->rdev->flags),
7864 bdevname(tmp->rdev->bdev, b));
7868 static int raid5_spare_active(struct mddev *mddev)
7871 struct r5conf *conf = mddev->private;
7872 struct disk_info *tmp;
7874 unsigned long flags;
7876 for (i = 0; i < conf->raid_disks; i++) {
7877 tmp = conf->disks + i;
7878 if (tmp->replacement
7879 && tmp->replacement->recovery_offset == MaxSector
7880 && !test_bit(Faulty, &tmp->replacement->flags)
7881 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7882 /* Replacement has just become active. */
7884 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7887 /* Replaced device not technically faulty,
7888 * but we need to be sure it gets removed
7889 * and never re-added.
7891 set_bit(Faulty, &tmp->rdev->flags);
7892 sysfs_notify_dirent_safe(
7893 tmp->rdev->sysfs_state);
7895 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7896 } else if (tmp->rdev
7897 && tmp->rdev->recovery_offset == MaxSector
7898 && !test_bit(Faulty, &tmp->rdev->flags)
7899 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
7901 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
7904 spin_lock_irqsave(&conf->device_lock, flags);
7905 mddev->degraded = raid5_calc_degraded(conf);
7906 spin_unlock_irqrestore(&conf->device_lock, flags);
7907 print_raid5_conf(conf);
7911 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
7913 struct r5conf *conf = mddev->private;
7915 int number = rdev->raid_disk;
7916 struct md_rdev **rdevp;
7917 struct disk_info *p = conf->disks + number;
7919 print_raid5_conf(conf);
7920 if (test_bit(Journal, &rdev->flags) && conf->log) {
7922 * we can't wait pending write here, as this is called in
7923 * raid5d, wait will deadlock.
7924 * neilb: there is no locking about new writes here,
7925 * so this cannot be safe.
7927 if (atomic_read(&conf->active_stripes) ||
7928 atomic_read(&conf->r5c_cached_full_stripes) ||
7929 atomic_read(&conf->r5c_cached_partial_stripes)) {
7935 if (rdev == p->rdev)
7937 else if (rdev == p->replacement)
7938 rdevp = &p->replacement;
7942 if (number >= conf->raid_disks &&
7943 conf->reshape_progress == MaxSector)
7944 clear_bit(In_sync, &rdev->flags);
7946 if (test_bit(In_sync, &rdev->flags) ||
7947 atomic_read(&rdev->nr_pending)) {
7951 /* Only remove non-faulty devices if recovery
7954 if (!test_bit(Faulty, &rdev->flags) &&
7955 mddev->recovery_disabled != conf->recovery_disabled &&
7956 !has_failed(conf) &&
7957 (!p->replacement || p->replacement == rdev) &&
7958 number < conf->raid_disks) {
7963 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7965 if (atomic_read(&rdev->nr_pending)) {
7966 /* lost the race, try later */
7972 err = log_modify(conf, rdev, false);
7976 if (p->replacement) {
7977 /* We must have just cleared 'rdev' */
7978 p->rdev = p->replacement;
7979 clear_bit(Replacement, &p->replacement->flags);
7980 smp_mb(); /* Make sure other CPUs may see both as identical
7981 * but will never see neither - if they are careful
7983 p->replacement = NULL;
7986 err = log_modify(conf, p->rdev, true);
7989 clear_bit(WantReplacement, &rdev->flags);
7992 print_raid5_conf(conf);
7996 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
7998 struct r5conf *conf = mddev->private;
7999 int ret, err = -EEXIST;
8001 struct disk_info *p;
8003 int last = conf->raid_disks - 1;
8005 if (test_bit(Journal, &rdev->flags)) {
8009 rdev->raid_disk = 0;
8011 * The array is in readonly mode if journal is missing, so no
8012 * write requests running. We should be safe
8014 ret = log_init(conf, rdev, false);
8018 ret = r5l_start(conf->log);
8024 if (mddev->recovery_disabled == conf->recovery_disabled)
8027 if (rdev->saved_raid_disk < 0 && has_failed(conf))
8028 /* no point adding a device */
8031 if (rdev->raid_disk >= 0)
8032 first = last = rdev->raid_disk;
8035 * find the disk ... but prefer rdev->saved_raid_disk
8038 if (rdev->saved_raid_disk >= 0 &&
8039 rdev->saved_raid_disk >= first &&
8040 rdev->saved_raid_disk <= last &&
8041 conf->disks[rdev->saved_raid_disk].rdev == NULL)
8042 first = rdev->saved_raid_disk;
8044 for (disk = first; disk <= last; disk++) {
8045 p = conf->disks + disk;
8046 if (p->rdev == NULL) {
8047 clear_bit(In_sync, &rdev->flags);
8048 rdev->raid_disk = disk;
8049 if (rdev->saved_raid_disk != disk)
8051 rcu_assign_pointer(p->rdev, rdev);
8053 err = log_modify(conf, rdev, true);
8058 for (disk = first; disk <= last; disk++) {
8059 p = conf->disks + disk;
8060 if (test_bit(WantReplacement, &p->rdev->flags) &&
8061 p->replacement == NULL) {
8062 clear_bit(In_sync, &rdev->flags);
8063 set_bit(Replacement, &rdev->flags);
8064 rdev->raid_disk = disk;
8067 rcu_assign_pointer(p->replacement, rdev);
8072 print_raid5_conf(conf);
8076 static int raid5_resize(struct mddev *mddev, sector_t sectors)
8078 /* no resync is happening, and there is enough space
8079 * on all devices, so we can resize.
8080 * We need to make sure resync covers any new space.
8081 * If the array is shrinking we should possibly wait until
8082 * any io in the removed space completes, but it hardly seems
8086 struct r5conf *conf = mddev->private;
8088 if (raid5_has_log(conf) || raid5_has_ppl(conf))
8090 sectors &= ~((sector_t)conf->chunk_sectors - 1);
8091 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
8092 if (mddev->external_size &&
8093 mddev->array_sectors > newsize)
8095 if (mddev->bitmap) {
8096 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
8100 md_set_array_sectors(mddev, newsize);
8101 if (sectors > mddev->dev_sectors &&
8102 mddev->recovery_cp > mddev->dev_sectors) {
8103 mddev->recovery_cp = mddev->dev_sectors;
8104 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8106 mddev->dev_sectors = sectors;
8107 mddev->resync_max_sectors = sectors;
8111 static int check_stripe_cache(struct mddev *mddev)
8113 /* Can only proceed if there are plenty of stripe_heads.
8114 * We need a minimum of one full stripe,, and for sensible progress
8115 * it is best to have about 4 times that.
8116 * If we require 4 times, then the default 256 4K stripe_heads will
8117 * allow for chunk sizes up to 256K, which is probably OK.
8118 * If the chunk size is greater, user-space should request more
8119 * stripe_heads first.
8121 struct r5conf *conf = mddev->private;
8122 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
8123 > conf->min_nr_stripes ||
8124 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
8125 > conf->min_nr_stripes) {
8126 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
8128 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
8129 / RAID5_STRIPE_SIZE(conf))*4);
8135 static int check_reshape(struct mddev *mddev)
8137 struct r5conf *conf = mddev->private;
8139 if (raid5_has_log(conf) || raid5_has_ppl(conf))
8141 if (mddev->delta_disks == 0 &&
8142 mddev->new_layout == mddev->layout &&
8143 mddev->new_chunk_sectors == mddev->chunk_sectors)
8144 return 0; /* nothing to do */
8145 if (has_failed(conf))
8147 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
8148 /* We might be able to shrink, but the devices must
8149 * be made bigger first.
8150 * For raid6, 4 is the minimum size.
8151 * Otherwise 2 is the minimum
8154 if (mddev->level == 6)
8156 if (mddev->raid_disks + mddev->delta_disks < min)
8160 if (!check_stripe_cache(mddev))
8163 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
8164 mddev->delta_disks > 0)
8165 if (resize_chunks(conf,
8166 conf->previous_raid_disks
8167 + max(0, mddev->delta_disks),
8168 max(mddev->new_chunk_sectors,
8169 mddev->chunk_sectors)
8173 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
8174 return 0; /* never bother to shrink */
8175 return resize_stripes(conf, (conf->previous_raid_disks
8176 + mddev->delta_disks));
8179 static int raid5_start_reshape(struct mddev *mddev)
8181 struct r5conf *conf = mddev->private;
8182 struct md_rdev *rdev;
8184 unsigned long flags;
8186 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8189 if (!check_stripe_cache(mddev))
8192 if (has_failed(conf))
8195 rdev_for_each(rdev, mddev) {
8196 if (!test_bit(In_sync, &rdev->flags)
8197 && !test_bit(Faulty, &rdev->flags))
8201 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
8202 /* Not enough devices even to make a degraded array
8207 /* Refuse to reduce size of the array. Any reductions in
8208 * array size must be through explicit setting of array_size
8211 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
8212 < mddev->array_sectors) {
8213 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
8218 atomic_set(&conf->reshape_stripes, 0);
8219 spin_lock_irq(&conf->device_lock);
8220 write_seqcount_begin(&conf->gen_lock);
8221 conf->previous_raid_disks = conf->raid_disks;
8222 conf->raid_disks += mddev->delta_disks;
8223 conf->prev_chunk_sectors = conf->chunk_sectors;
8224 conf->chunk_sectors = mddev->new_chunk_sectors;
8225 conf->prev_algo = conf->algorithm;
8226 conf->algorithm = mddev->new_layout;
8228 /* Code that selects data_offset needs to see the generation update
8229 * if reshape_progress has been set - so a memory barrier needed.
8232 if (mddev->reshape_backwards)
8233 conf->reshape_progress = raid5_size(mddev, 0, 0);
8235 conf->reshape_progress = 0;
8236 conf->reshape_safe = conf->reshape_progress;
8237 write_seqcount_end(&conf->gen_lock);
8238 spin_unlock_irq(&conf->device_lock);
8240 /* Now make sure any requests that proceeded on the assumption
8241 * the reshape wasn't running - like Discard or Read - have
8244 mddev_suspend(mddev);
8245 mddev_resume(mddev);
8247 /* Add some new drives, as many as will fit.
8248 * We know there are enough to make the newly sized array work.
8249 * Don't add devices if we are reducing the number of
8250 * devices in the array. This is because it is not possible
8251 * to correctly record the "partially reconstructed" state of
8252 * such devices during the reshape and confusion could result.
8254 if (mddev->delta_disks >= 0) {
8255 rdev_for_each(rdev, mddev)
8256 if (rdev->raid_disk < 0 &&
8257 !test_bit(Faulty, &rdev->flags)) {
8258 if (raid5_add_disk(mddev, rdev) == 0) {
8260 >= conf->previous_raid_disks)
8261 set_bit(In_sync, &rdev->flags);
8263 rdev->recovery_offset = 0;
8265 /* Failure here is OK */
8266 sysfs_link_rdev(mddev, rdev);
8268 } else if (rdev->raid_disk >= conf->previous_raid_disks
8269 && !test_bit(Faulty, &rdev->flags)) {
8270 /* This is a spare that was manually added */
8271 set_bit(In_sync, &rdev->flags);
8274 /* When a reshape changes the number of devices,
8275 * ->degraded is measured against the larger of the
8276 * pre and post number of devices.
8278 spin_lock_irqsave(&conf->device_lock, flags);
8279 mddev->degraded = raid5_calc_degraded(conf);
8280 spin_unlock_irqrestore(&conf->device_lock, flags);
8282 mddev->raid_disks = conf->raid_disks;
8283 mddev->reshape_position = conf->reshape_progress;
8284 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8286 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8287 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8288 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
8289 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8290 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8291 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
8293 if (!mddev->sync_thread) {
8294 mddev->recovery = 0;
8295 spin_lock_irq(&conf->device_lock);
8296 write_seqcount_begin(&conf->gen_lock);
8297 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
8298 mddev->new_chunk_sectors =
8299 conf->chunk_sectors = conf->prev_chunk_sectors;
8300 mddev->new_layout = conf->algorithm = conf->prev_algo;
8301 rdev_for_each(rdev, mddev)
8302 rdev->new_data_offset = rdev->data_offset;
8304 conf->generation --;
8305 conf->reshape_progress = MaxSector;
8306 mddev->reshape_position = MaxSector;
8307 write_seqcount_end(&conf->gen_lock);
8308 spin_unlock_irq(&conf->device_lock);
8311 conf->reshape_checkpoint = jiffies;
8312 md_wakeup_thread(mddev->sync_thread);
8313 md_new_event(mddev);
8317 /* This is called from the reshape thread and should make any
8318 * changes needed in 'conf'
8320 static void end_reshape(struct r5conf *conf)
8323 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
8324 struct md_rdev *rdev;
8326 spin_lock_irq(&conf->device_lock);
8327 conf->previous_raid_disks = conf->raid_disks;
8328 md_finish_reshape(conf->mddev);
8330 conf->reshape_progress = MaxSector;
8331 conf->mddev->reshape_position = MaxSector;
8332 rdev_for_each(rdev, conf->mddev)
8333 if (rdev->raid_disk >= 0 &&
8334 !test_bit(Journal, &rdev->flags) &&
8335 !test_bit(In_sync, &rdev->flags))
8336 rdev->recovery_offset = MaxSector;
8337 spin_unlock_irq(&conf->device_lock);
8338 wake_up(&conf->wait_for_overlap);
8340 if (conf->mddev->queue)
8341 raid5_set_io_opt(conf);
8345 /* This is called from the raid5d thread with mddev_lock held.
8346 * It makes config changes to the device.
8348 static void raid5_finish_reshape(struct mddev *mddev)
8350 struct r5conf *conf = mddev->private;
8352 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8354 if (mddev->delta_disks <= 0) {
8356 spin_lock_irq(&conf->device_lock);
8357 mddev->degraded = raid5_calc_degraded(conf);
8358 spin_unlock_irq(&conf->device_lock);
8359 for (d = conf->raid_disks ;
8360 d < conf->raid_disks - mddev->delta_disks;
8362 struct md_rdev *rdev = conf->disks[d].rdev;
8364 clear_bit(In_sync, &rdev->flags);
8365 rdev = conf->disks[d].replacement;
8367 clear_bit(In_sync, &rdev->flags);
8370 mddev->layout = conf->algorithm;
8371 mddev->chunk_sectors = conf->chunk_sectors;
8372 mddev->reshape_position = MaxSector;
8373 mddev->delta_disks = 0;
8374 mddev->reshape_backwards = 0;
8378 static void raid5_quiesce(struct mddev *mddev, int quiesce)
8380 struct r5conf *conf = mddev->private;
8383 /* stop all writes */
8384 lock_all_device_hash_locks_irq(conf);
8385 /* '2' tells resync/reshape to pause so that all
8386 * active stripes can drain
8388 r5c_flush_cache(conf, INT_MAX);
8389 /* need a memory barrier to make sure read_one_chunk() sees
8390 * quiesce started and reverts to slow (locked) path.
8392 smp_store_release(&conf->quiesce, 2);
8393 wait_event_cmd(conf->wait_for_quiescent,
8394 atomic_read(&conf->active_stripes) == 0 &&
8395 atomic_read(&conf->active_aligned_reads) == 0,
8396 unlock_all_device_hash_locks_irq(conf),
8397 lock_all_device_hash_locks_irq(conf));
8399 unlock_all_device_hash_locks_irq(conf);
8400 /* allow reshape to continue */
8401 wake_up(&conf->wait_for_overlap);
8403 /* re-enable writes */
8404 lock_all_device_hash_locks_irq(conf);
8406 wake_up(&conf->wait_for_quiescent);
8407 wake_up(&conf->wait_for_overlap);
8408 unlock_all_device_hash_locks_irq(conf);
8410 log_quiesce(conf, quiesce);
8413 static void *raid45_takeover_raid0(struct mddev *mddev, int level)
8415 struct r0conf *raid0_conf = mddev->private;
8418 /* for raid0 takeover only one zone is supported */
8419 if (raid0_conf->nr_strip_zones > 1) {
8420 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8422 return ERR_PTR(-EINVAL);
8425 sectors = raid0_conf->strip_zone[0].zone_end;
8426 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
8427 mddev->dev_sectors = sectors;
8428 mddev->new_level = level;
8429 mddev->new_layout = ALGORITHM_PARITY_N;
8430 mddev->new_chunk_sectors = mddev->chunk_sectors;
8431 mddev->raid_disks += 1;
8432 mddev->delta_disks = 1;
8433 /* make sure it will be not marked as dirty */
8434 mddev->recovery_cp = MaxSector;
8436 return setup_conf(mddev);
8439 static void *raid5_takeover_raid1(struct mddev *mddev)
8444 if (mddev->raid_disks != 2 ||
8445 mddev->degraded > 1)
8446 return ERR_PTR(-EINVAL);
8448 /* Should check if there are write-behind devices? */
8450 chunksect = 64*2; /* 64K by default */
8452 /* The array must be an exact multiple of chunksize */
8453 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8456 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
8457 /* array size does not allow a suitable chunk size */
8458 return ERR_PTR(-EINVAL);
8460 mddev->new_level = 5;
8461 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
8462 mddev->new_chunk_sectors = chunksect;
8464 ret = setup_conf(mddev);
8466 mddev_clear_unsupported_flags(mddev,
8467 UNSUPPORTED_MDDEV_FLAGS);
8471 static void *raid5_takeover_raid6(struct mddev *mddev)
8475 switch (mddev->layout) {
8476 case ALGORITHM_LEFT_ASYMMETRIC_6:
8477 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8479 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8480 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8482 case ALGORITHM_LEFT_SYMMETRIC_6:
8483 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8485 case ALGORITHM_RIGHT_SYMMETRIC_6:
8486 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8488 case ALGORITHM_PARITY_0_6:
8489 new_layout = ALGORITHM_PARITY_0;
8491 case ALGORITHM_PARITY_N:
8492 new_layout = ALGORITHM_PARITY_N;
8495 return ERR_PTR(-EINVAL);
8497 mddev->new_level = 5;
8498 mddev->new_layout = new_layout;
8499 mddev->delta_disks = -1;
8500 mddev->raid_disks -= 1;
8501 return setup_conf(mddev);
8504 static int raid5_check_reshape(struct mddev *mddev)
8506 /* For a 2-drive array, the layout and chunk size can be changed
8507 * immediately as not restriping is needed.
8508 * For larger arrays we record the new value - after validation
8509 * to be used by a reshape pass.
8511 struct r5conf *conf = mddev->private;
8512 int new_chunk = mddev->new_chunk_sectors;
8514 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
8516 if (new_chunk > 0) {
8517 if (!is_power_of_2(new_chunk))
8519 if (new_chunk < (PAGE_SIZE>>9))
8521 if (mddev->array_sectors & (new_chunk-1))
8522 /* not factor of array size */
8526 /* They look valid */
8528 if (mddev->raid_disks == 2) {
8529 /* can make the change immediately */
8530 if (mddev->new_layout >= 0) {
8531 conf->algorithm = mddev->new_layout;
8532 mddev->layout = mddev->new_layout;
8534 if (new_chunk > 0) {
8535 conf->chunk_sectors = new_chunk ;
8536 mddev->chunk_sectors = new_chunk;
8538 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
8539 md_wakeup_thread(mddev->thread);
8541 return check_reshape(mddev);
8544 static int raid6_check_reshape(struct mddev *mddev)
8546 int new_chunk = mddev->new_chunk_sectors;
8548 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
8550 if (new_chunk > 0) {
8551 if (!is_power_of_2(new_chunk))
8553 if (new_chunk < (PAGE_SIZE >> 9))
8555 if (mddev->array_sectors & (new_chunk-1))
8556 /* not factor of array size */
8560 /* They look valid */
8561 return check_reshape(mddev);
8564 static void *raid5_takeover(struct mddev *mddev)
8566 /* raid5 can take over:
8567 * raid0 - if there is only one strip zone - make it a raid4 layout
8568 * raid1 - if there are two drives. We need to know the chunk size
8569 * raid4 - trivial - just use a raid4 layout.
8570 * raid6 - Providing it is a *_6 layout
8572 if (mddev->level == 0)
8573 return raid45_takeover_raid0(mddev, 5);
8574 if (mddev->level == 1)
8575 return raid5_takeover_raid1(mddev);
8576 if (mddev->level == 4) {
8577 mddev->new_layout = ALGORITHM_PARITY_N;
8578 mddev->new_level = 5;
8579 return setup_conf(mddev);
8581 if (mddev->level == 6)
8582 return raid5_takeover_raid6(mddev);
8584 return ERR_PTR(-EINVAL);
8587 static void *raid4_takeover(struct mddev *mddev)
8589 /* raid4 can take over:
8590 * raid0 - if there is only one strip zone
8591 * raid5 - if layout is right
8593 if (mddev->level == 0)
8594 return raid45_takeover_raid0(mddev, 4);
8595 if (mddev->level == 5 &&
8596 mddev->layout == ALGORITHM_PARITY_N) {
8597 mddev->new_layout = 0;
8598 mddev->new_level = 4;
8599 return setup_conf(mddev);
8601 return ERR_PTR(-EINVAL);
8604 static struct md_personality raid5_personality;
8606 static void *raid6_takeover(struct mddev *mddev)
8608 /* Currently can only take over a raid5. We map the
8609 * personality to an equivalent raid6 personality
8610 * with the Q block at the end.
8614 if (mddev->pers != &raid5_personality)
8615 return ERR_PTR(-EINVAL);
8616 if (mddev->degraded > 1)
8617 return ERR_PTR(-EINVAL);
8618 if (mddev->raid_disks > 253)
8619 return ERR_PTR(-EINVAL);
8620 if (mddev->raid_disks < 3)
8621 return ERR_PTR(-EINVAL);
8623 switch (mddev->layout) {
8624 case ALGORITHM_LEFT_ASYMMETRIC:
8625 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8627 case ALGORITHM_RIGHT_ASYMMETRIC:
8628 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8630 case ALGORITHM_LEFT_SYMMETRIC:
8631 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8633 case ALGORITHM_RIGHT_SYMMETRIC:
8634 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8636 case ALGORITHM_PARITY_0:
8637 new_layout = ALGORITHM_PARITY_0_6;
8639 case ALGORITHM_PARITY_N:
8640 new_layout = ALGORITHM_PARITY_N;
8643 return ERR_PTR(-EINVAL);
8645 mddev->new_level = 6;
8646 mddev->new_layout = new_layout;
8647 mddev->delta_disks = 1;
8648 mddev->raid_disks += 1;
8649 return setup_conf(mddev);
8652 static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8654 struct r5conf *conf;
8657 err = mddev_lock(mddev);
8660 conf = mddev->private;
8662 mddev_unlock(mddev);
8666 if (strncmp(buf, "ppl", 3) == 0) {
8667 /* ppl only works with RAID 5 */
8668 if (!raid5_has_ppl(conf) && conf->level == 5) {
8669 err = log_init(conf, NULL, true);
8671 err = resize_stripes(conf, conf->pool_size);
8677 } else if (strncmp(buf, "resync", 6) == 0) {
8678 if (raid5_has_ppl(conf)) {
8679 mddev_suspend(mddev);
8681 mddev_resume(mddev);
8682 err = resize_stripes(conf, conf->pool_size);
8683 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8684 r5l_log_disk_error(conf)) {
8685 bool journal_dev_exists = false;
8686 struct md_rdev *rdev;
8688 rdev_for_each(rdev, mddev)
8689 if (test_bit(Journal, &rdev->flags)) {
8690 journal_dev_exists = true;
8694 if (!journal_dev_exists) {
8695 mddev_suspend(mddev);
8696 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8697 mddev_resume(mddev);
8698 } else /* need remove journal device first */
8707 md_update_sb(mddev, 1);
8709 mddev_unlock(mddev);
8714 static int raid5_start(struct mddev *mddev)
8716 struct r5conf *conf = mddev->private;
8718 return r5l_start(conf->log);
8721 static struct md_personality raid6_personality =
8725 .owner = THIS_MODULE,
8726 .make_request = raid5_make_request,
8728 .start = raid5_start,
8730 .status = raid5_status,
8731 .error_handler = raid5_error,
8732 .hot_add_disk = raid5_add_disk,
8733 .hot_remove_disk= raid5_remove_disk,
8734 .spare_active = raid5_spare_active,
8735 .sync_request = raid5_sync_request,
8736 .resize = raid5_resize,
8738 .check_reshape = raid6_check_reshape,
8739 .start_reshape = raid5_start_reshape,
8740 .finish_reshape = raid5_finish_reshape,
8741 .quiesce = raid5_quiesce,
8742 .takeover = raid6_takeover,
8743 .change_consistency_policy = raid5_change_consistency_policy,
8745 static struct md_personality raid5_personality =
8749 .owner = THIS_MODULE,
8750 .make_request = raid5_make_request,
8752 .start = raid5_start,
8754 .status = raid5_status,
8755 .error_handler = raid5_error,
8756 .hot_add_disk = raid5_add_disk,
8757 .hot_remove_disk= raid5_remove_disk,
8758 .spare_active = raid5_spare_active,
8759 .sync_request = raid5_sync_request,
8760 .resize = raid5_resize,
8762 .check_reshape = raid5_check_reshape,
8763 .start_reshape = raid5_start_reshape,
8764 .finish_reshape = raid5_finish_reshape,
8765 .quiesce = raid5_quiesce,
8766 .takeover = raid5_takeover,
8767 .change_consistency_policy = raid5_change_consistency_policy,
8770 static struct md_personality raid4_personality =
8774 .owner = THIS_MODULE,
8775 .make_request = raid5_make_request,
8777 .start = raid5_start,
8779 .status = raid5_status,
8780 .error_handler = raid5_error,
8781 .hot_add_disk = raid5_add_disk,
8782 .hot_remove_disk= raid5_remove_disk,
8783 .spare_active = raid5_spare_active,
8784 .sync_request = raid5_sync_request,
8785 .resize = raid5_resize,
8787 .check_reshape = raid5_check_reshape,
8788 .start_reshape = raid5_start_reshape,
8789 .finish_reshape = raid5_finish_reshape,
8790 .quiesce = raid5_quiesce,
8791 .takeover = raid4_takeover,
8792 .change_consistency_policy = raid5_change_consistency_policy,
8795 static int __init raid5_init(void)
8799 raid5_wq = alloc_workqueue("raid5wq",
8800 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8804 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8806 raid456_cpu_up_prepare,
8809 destroy_workqueue(raid5_wq);
8812 register_md_personality(&raid6_personality);
8813 register_md_personality(&raid5_personality);
8814 register_md_personality(&raid4_personality);
8818 static void raid5_exit(void)
8820 unregister_md_personality(&raid6_personality);
8821 unregister_md_personality(&raid5_personality);
8822 unregister_md_personality(&raid4_personality);
8823 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
8824 destroy_workqueue(raid5_wq);
8827 module_init(raid5_init);
8828 module_exit(raid5_exit);
8829 MODULE_LICENSE("GPL");
8830 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
8831 MODULE_ALIAS("md-personality-4"); /* RAID5 */
8832 MODULE_ALIAS("md-raid5");
8833 MODULE_ALIAS("md-raid4");
8834 MODULE_ALIAS("md-level-5");
8835 MODULE_ALIAS("md-level-4");
8836 MODULE_ALIAS("md-personality-8"); /* RAID6 */
8837 MODULE_ALIAS("md-raid6");
8838 MODULE_ALIAS("md-level-6");
8840 /* This used to be two separate modules, they were: */
8841 MODULE_ALIAS("raid5");
8842 MODULE_ALIAS("raid6");