2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is seq_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
56 #include <linux/nodemask.h>
57 #include <linux/flex_array.h>
58 #include <trace/events/block.h>
65 #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
67 #define cpu_to_group(cpu) cpu_to_node(cpu)
68 #define ANY_GROUP NUMA_NO_NODE
70 static bool devices_handle_discard_safely
= false;
71 module_param(devices_handle_discard_safely
, bool, 0644);
72 MODULE_PARM_DESC(devices_handle_discard_safely
,
73 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
74 static struct workqueue_struct
*raid5_wq
;
76 static inline struct hlist_head
*stripe_hash(struct r5conf
*conf
, sector_t sect
)
78 int hash
= (sect
>> STRIPE_SHIFT
) & HASH_MASK
;
79 return &conf
->stripe_hashtbl
[hash
];
82 static inline int stripe_hash_locks_hash(sector_t sect
)
84 return (sect
>> STRIPE_SHIFT
) & STRIPE_HASH_LOCKS_MASK
;
87 static inline void lock_device_hash_lock(struct r5conf
*conf
, int hash
)
89 spin_lock_irq(conf
->hash_locks
+ hash
);
90 spin_lock(&conf
->device_lock
);
93 static inline void unlock_device_hash_lock(struct r5conf
*conf
, int hash
)
95 spin_unlock(&conf
->device_lock
);
96 spin_unlock_irq(conf
->hash_locks
+ hash
);
99 static inline void lock_all_device_hash_locks_irq(struct r5conf
*conf
)
103 spin_lock(conf
->hash_locks
);
104 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
105 spin_lock_nest_lock(conf
->hash_locks
+ i
, conf
->hash_locks
);
106 spin_lock(&conf
->device_lock
);
109 static inline void unlock_all_device_hash_locks_irq(struct r5conf
*conf
)
112 spin_unlock(&conf
->device_lock
);
113 for (i
= NR_STRIPE_HASH_LOCKS
; i
; i
--)
114 spin_unlock(conf
->hash_locks
+ i
- 1);
118 /* Find first data disk in a raid6 stripe */
119 static inline int raid6_d0(struct stripe_head
*sh
)
122 /* ddf always start from first device */
124 /* md starts just after Q block */
125 if (sh
->qd_idx
== sh
->disks
- 1)
128 return sh
->qd_idx
+ 1;
130 static inline int raid6_next_disk(int disk
, int raid_disks
)
133 return (disk
< raid_disks
) ? disk
: 0;
136 /* When walking through the disks in a raid5, starting at raid6_d0,
137 * We need to map each disk to a 'slot', where the data disks are slot
138 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
139 * is raid_disks-1. This help does that mapping.
141 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
142 int *count
, int syndrome_disks
)
148 if (idx
== sh
->pd_idx
)
149 return syndrome_disks
;
150 if (idx
== sh
->qd_idx
)
151 return syndrome_disks
+ 1;
157 static void return_io(struct bio_list
*return_bi
)
160 while ((bi
= bio_list_pop(return_bi
)) != NULL
) {
161 bi
->bi_iter
.bi_size
= 0;
162 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
168 static void print_raid5_conf (struct r5conf
*conf
);
170 static int stripe_operations_active(struct stripe_head
*sh
)
172 return sh
->check_state
|| sh
->reconstruct_state
||
173 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
174 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
177 static void raid5_wakeup_stripe_thread(struct stripe_head
*sh
)
179 struct r5conf
*conf
= sh
->raid_conf
;
180 struct r5worker_group
*group
;
182 int i
, cpu
= sh
->cpu
;
184 if (!cpu_online(cpu
)) {
185 cpu
= cpumask_any(cpu_online_mask
);
189 if (list_empty(&sh
->lru
)) {
190 struct r5worker_group
*group
;
191 group
= conf
->worker_groups
+ cpu_to_group(cpu
);
192 list_add_tail(&sh
->lru
, &group
->handle_list
);
193 group
->stripes_cnt
++;
197 if (conf
->worker_cnt_per_group
== 0) {
198 md_wakeup_thread(conf
->mddev
->thread
);
202 group
= conf
->worker_groups
+ cpu_to_group(sh
->cpu
);
204 group
->workers
[0].working
= true;
205 /* at least one worker should run to avoid race */
206 queue_work_on(sh
->cpu
, raid5_wq
, &group
->workers
[0].work
);
208 thread_cnt
= group
->stripes_cnt
/ MAX_STRIPE_BATCH
- 1;
209 /* wakeup more workers */
210 for (i
= 1; i
< conf
->worker_cnt_per_group
&& thread_cnt
> 0; i
++) {
211 if (group
->workers
[i
].working
== false) {
212 group
->workers
[i
].working
= true;
213 queue_work_on(sh
->cpu
, raid5_wq
,
214 &group
->workers
[i
].work
);
220 static void do_release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
221 struct list_head
*temp_inactive_list
)
224 int injournal
= 0; /* number of date pages with R5_InJournal */
226 BUG_ON(!list_empty(&sh
->lru
));
227 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
229 if (r5c_is_writeback(conf
->log
))
230 for (i
= sh
->disks
; i
--; )
231 if (test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
234 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
235 * data in journal, so they are not released to cached lists
237 if (conf
->quiesce
&& r5c_is_writeback(conf
->log
) &&
238 !test_bit(STRIPE_HANDLE
, &sh
->state
) && injournal
!= 0) {
239 if (test_bit(STRIPE_R5C_CACHING
, &sh
->state
))
240 r5c_make_stripe_write_out(sh
);
241 set_bit(STRIPE_HANDLE
, &sh
->state
);
244 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
245 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
246 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
247 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
248 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
249 sh
->bm_seq
- conf
->seq_write
> 0)
250 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
252 clear_bit(STRIPE_DELAYED
, &sh
->state
);
253 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
254 if (conf
->worker_cnt_per_group
== 0) {
255 list_add_tail(&sh
->lru
, &conf
->handle_list
);
257 raid5_wakeup_stripe_thread(sh
);
261 md_wakeup_thread(conf
->mddev
->thread
);
263 BUG_ON(stripe_operations_active(sh
));
264 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
265 if (atomic_dec_return(&conf
->preread_active_stripes
)
267 md_wakeup_thread(conf
->mddev
->thread
);
268 atomic_dec(&conf
->active_stripes
);
269 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
270 if (!r5c_is_writeback(conf
->log
))
271 list_add_tail(&sh
->lru
, temp_inactive_list
);
273 WARN_ON(test_bit(R5_InJournal
, &sh
->dev
[sh
->pd_idx
].flags
));
275 list_add_tail(&sh
->lru
, temp_inactive_list
);
276 else if (injournal
== conf
->raid_disks
- conf
->max_degraded
) {
278 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE
, &sh
->state
))
279 atomic_inc(&conf
->r5c_cached_full_stripes
);
280 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE
, &sh
->state
))
281 atomic_dec(&conf
->r5c_cached_partial_stripes
);
282 list_add_tail(&sh
->lru
, &conf
->r5c_full_stripe_list
);
283 r5c_check_cached_full_stripe(conf
);
286 if (!test_and_set_bit(STRIPE_R5C_PARTIAL_STRIPE
,
288 atomic_inc(&conf
->r5c_cached_partial_stripes
);
289 list_add_tail(&sh
->lru
, &conf
->r5c_partial_stripe_list
);
296 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
297 struct list_head
*temp_inactive_list
)
299 if (atomic_dec_and_test(&sh
->count
))
300 do_release_stripe(conf
, sh
, temp_inactive_list
);
304 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
306 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
307 * given time. Adding stripes only takes device lock, while deleting stripes
308 * only takes hash lock.
310 static void release_inactive_stripe_list(struct r5conf
*conf
,
311 struct list_head
*temp_inactive_list
,
315 bool do_wakeup
= false;
318 if (hash
== NR_STRIPE_HASH_LOCKS
) {
319 size
= NR_STRIPE_HASH_LOCKS
;
320 hash
= NR_STRIPE_HASH_LOCKS
- 1;
324 struct list_head
*list
= &temp_inactive_list
[size
- 1];
327 * We don't hold any lock here yet, raid5_get_active_stripe() might
328 * remove stripes from the list
330 if (!list_empty_careful(list
)) {
331 spin_lock_irqsave(conf
->hash_locks
+ hash
, flags
);
332 if (list_empty(conf
->inactive_list
+ hash
) &&
334 atomic_dec(&conf
->empty_inactive_list_nr
);
335 list_splice_tail_init(list
, conf
->inactive_list
+ hash
);
337 spin_unlock_irqrestore(conf
->hash_locks
+ hash
, flags
);
344 wake_up(&conf
->wait_for_stripe
);
345 if (atomic_read(&conf
->active_stripes
) == 0)
346 wake_up(&conf
->wait_for_quiescent
);
347 if (conf
->retry_read_aligned
)
348 md_wakeup_thread(conf
->mddev
->thread
);
352 /* should hold conf->device_lock already */
353 static int release_stripe_list(struct r5conf
*conf
,
354 struct list_head
*temp_inactive_list
)
356 struct stripe_head
*sh
;
358 struct llist_node
*head
;
360 head
= llist_del_all(&conf
->released_stripes
);
361 head
= llist_reverse_order(head
);
365 sh
= llist_entry(head
, struct stripe_head
, release_list
);
366 head
= llist_next(head
);
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 local_irq_save(flags
);
405 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
406 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
407 INIT_LIST_HEAD(&list
);
408 hash
= sh
->hash_lock_index
;
409 do_release_stripe(conf
, sh
, &list
);
410 spin_unlock(&conf
->device_lock
);
411 release_inactive_stripe_list(conf
, &list
, hash
);
413 local_irq_restore(flags
);
416 static inline void remove_hash(struct stripe_head
*sh
)
418 pr_debug("remove_hash(), stripe %llu\n",
419 (unsigned long long)sh
->sector
);
421 hlist_del_init(&sh
->hash
);
424 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
426 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
428 pr_debug("insert_hash(), stripe %llu\n",
429 (unsigned long long)sh
->sector
);
431 hlist_add_head(&sh
->hash
, hp
);
434 /* find an idle stripe, make sure it is unhashed, and return it. */
435 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
, int hash
)
437 struct stripe_head
*sh
= NULL
;
438 struct list_head
*first
;
440 if (list_empty(conf
->inactive_list
+ hash
))
442 first
= (conf
->inactive_list
+ hash
)->next
;
443 sh
= list_entry(first
, struct stripe_head
, lru
);
444 list_del_init(first
);
446 atomic_inc(&conf
->active_stripes
);
447 BUG_ON(hash
!= sh
->hash_lock_index
);
448 if (list_empty(conf
->inactive_list
+ hash
))
449 atomic_inc(&conf
->empty_inactive_list_nr
);
454 static void shrink_buffers(struct stripe_head
*sh
)
458 int num
= sh
->raid_conf
->pool_size
;
460 for (i
= 0; i
< num
; i
++) {
461 WARN_ON(sh
->dev
[i
].page
!= sh
->dev
[i
].orig_page
);
465 sh
->dev
[i
].page
= NULL
;
470 static int grow_buffers(struct stripe_head
*sh
, gfp_t gfp
)
473 int num
= sh
->raid_conf
->pool_size
;
475 for (i
= 0; i
< num
; i
++) {
478 if (!(page
= alloc_page(gfp
))) {
481 sh
->dev
[i
].page
= page
;
482 sh
->dev
[i
].orig_page
= page
;
487 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
488 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
489 struct stripe_head
*sh
);
491 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
493 struct r5conf
*conf
= sh
->raid_conf
;
496 BUG_ON(atomic_read(&sh
->count
) != 0);
497 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
498 BUG_ON(stripe_operations_active(sh
));
499 BUG_ON(sh
->batch_head
);
501 pr_debug("init_stripe called, stripe %llu\n",
502 (unsigned long long)sector
);
504 seq
= read_seqcount_begin(&conf
->gen_lock
);
505 sh
->generation
= conf
->generation
- previous
;
506 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
508 stripe_set_idx(sector
, conf
, previous
, sh
);
511 for (i
= sh
->disks
; i
--; ) {
512 struct r5dev
*dev
= &sh
->dev
[i
];
514 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
515 test_bit(R5_LOCKED
, &dev
->flags
)) {
516 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
517 (unsigned long long)sh
->sector
, i
, dev
->toread
,
518 dev
->read
, dev
->towrite
, dev
->written
,
519 test_bit(R5_LOCKED
, &dev
->flags
));
523 raid5_build_block(sh
, i
, previous
);
525 if (read_seqcount_retry(&conf
->gen_lock
, seq
))
527 sh
->overwrite_disks
= 0;
528 insert_hash(conf
, sh
);
529 sh
->cpu
= smp_processor_id();
530 set_bit(STRIPE_BATCH_READY
, &sh
->state
);
533 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
536 struct stripe_head
*sh
;
538 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
539 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
540 if (sh
->sector
== sector
&& sh
->generation
== generation
)
542 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
547 * Need to check if array has failed when deciding whether to:
549 * - remove non-faulty devices
552 * This determination is simple when no reshape is happening.
553 * However if there is a reshape, we need to carefully check
554 * both the before and after sections.
555 * This is because some failed devices may only affect one
556 * of the two sections, and some non-in_sync devices may
557 * be insync in the section most affected by failed devices.
559 static int calc_degraded(struct r5conf
*conf
)
561 int degraded
, degraded2
;
566 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
567 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
568 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
569 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
570 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
572 else if (test_bit(In_sync
, &rdev
->flags
))
575 /* not in-sync or faulty.
576 * If the reshape increases the number of devices,
577 * this is being recovered by the reshape, so
578 * this 'previous' section is not in_sync.
579 * If the number of devices is being reduced however,
580 * the device can only be part of the array if
581 * we are reverting a reshape, so this section will
584 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
588 if (conf
->raid_disks
== conf
->previous_raid_disks
)
592 for (i
= 0; i
< conf
->raid_disks
; i
++) {
593 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
594 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
595 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
596 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
598 else if (test_bit(In_sync
, &rdev
->flags
))
601 /* not in-sync or faulty.
602 * If reshape increases the number of devices, this
603 * section has already been recovered, else it
604 * almost certainly hasn't.
606 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
610 if (degraded2
> degraded
)
615 static int has_failed(struct r5conf
*conf
)
619 if (conf
->mddev
->reshape_position
== MaxSector
)
620 return conf
->mddev
->degraded
> conf
->max_degraded
;
622 degraded
= calc_degraded(conf
);
623 if (degraded
> conf
->max_degraded
)
629 raid5_get_active_stripe(struct r5conf
*conf
, sector_t sector
,
630 int previous
, int noblock
, int noquiesce
)
632 struct stripe_head
*sh
;
633 int hash
= stripe_hash_locks_hash(sector
);
634 int inc_empty_inactive_list_flag
;
636 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
638 spin_lock_irq(conf
->hash_locks
+ hash
);
641 wait_event_lock_irq(conf
->wait_for_quiescent
,
642 conf
->quiesce
== 0 || noquiesce
,
643 *(conf
->hash_locks
+ hash
));
644 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
646 if (!test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
)) {
647 sh
= get_free_stripe(conf
, hash
);
648 if (!sh
&& !test_bit(R5_DID_ALLOC
,
650 set_bit(R5_ALLOC_MORE
,
653 if (noblock
&& sh
== NULL
)
656 r5c_check_stripe_cache_usage(conf
);
658 set_bit(R5_INACTIVE_BLOCKED
,
660 r5l_wake_reclaim(conf
->log
, 0);
662 conf
->wait_for_stripe
,
663 !list_empty(conf
->inactive_list
+ hash
) &&
664 (atomic_read(&conf
->active_stripes
)
665 < (conf
->max_nr_stripes
* 3 / 4)
666 || !test_bit(R5_INACTIVE_BLOCKED
,
667 &conf
->cache_state
)),
668 *(conf
->hash_locks
+ hash
));
669 clear_bit(R5_INACTIVE_BLOCKED
,
672 init_stripe(sh
, sector
, previous
);
673 atomic_inc(&sh
->count
);
675 } else if (!atomic_inc_not_zero(&sh
->count
)) {
676 spin_lock(&conf
->device_lock
);
677 if (!atomic_read(&sh
->count
)) {
678 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
679 atomic_inc(&conf
->active_stripes
);
680 BUG_ON(list_empty(&sh
->lru
) &&
681 !test_bit(STRIPE_EXPANDING
, &sh
->state
));
682 inc_empty_inactive_list_flag
= 0;
683 if (!list_empty(conf
->inactive_list
+ hash
))
684 inc_empty_inactive_list_flag
= 1;
685 list_del_init(&sh
->lru
);
686 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
687 atomic_inc(&conf
->empty_inactive_list_nr
);
689 sh
->group
->stripes_cnt
--;
693 atomic_inc(&sh
->count
);
694 spin_unlock(&conf
->device_lock
);
696 } while (sh
== NULL
);
698 spin_unlock_irq(conf
->hash_locks
+ hash
);
702 static bool is_full_stripe_write(struct stripe_head
*sh
)
704 BUG_ON(sh
->overwrite_disks
> (sh
->disks
- sh
->raid_conf
->max_degraded
));
705 return sh
->overwrite_disks
== (sh
->disks
- sh
->raid_conf
->max_degraded
);
708 static void lock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
712 spin_lock(&sh2
->stripe_lock
);
713 spin_lock_nested(&sh1
->stripe_lock
, 1);
715 spin_lock(&sh1
->stripe_lock
);
716 spin_lock_nested(&sh2
->stripe_lock
, 1);
720 static void unlock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
722 spin_unlock(&sh1
->stripe_lock
);
723 spin_unlock(&sh2
->stripe_lock
);
727 /* Only freshly new full stripe normal write stripe can be added to a batch list */
728 static bool stripe_can_batch(struct stripe_head
*sh
)
730 struct r5conf
*conf
= sh
->raid_conf
;
734 return test_bit(STRIPE_BATCH_READY
, &sh
->state
) &&
735 !test_bit(STRIPE_BITMAP_PENDING
, &sh
->state
) &&
736 is_full_stripe_write(sh
);
739 /* we only do back search */
740 static void stripe_add_to_batch_list(struct r5conf
*conf
, struct stripe_head
*sh
)
742 struct stripe_head
*head
;
743 sector_t head_sector
, tmp_sec
;
746 int inc_empty_inactive_list_flag
;
748 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
749 tmp_sec
= sh
->sector
;
750 if (!sector_div(tmp_sec
, conf
->chunk_sectors
))
752 head_sector
= sh
->sector
- STRIPE_SECTORS
;
754 hash
= stripe_hash_locks_hash(head_sector
);
755 spin_lock_irq(conf
->hash_locks
+ hash
);
756 head
= __find_stripe(conf
, head_sector
, conf
->generation
);
757 if (head
&& !atomic_inc_not_zero(&head
->count
)) {
758 spin_lock(&conf
->device_lock
);
759 if (!atomic_read(&head
->count
)) {
760 if (!test_bit(STRIPE_HANDLE
, &head
->state
))
761 atomic_inc(&conf
->active_stripes
);
762 BUG_ON(list_empty(&head
->lru
) &&
763 !test_bit(STRIPE_EXPANDING
, &head
->state
));
764 inc_empty_inactive_list_flag
= 0;
765 if (!list_empty(conf
->inactive_list
+ hash
))
766 inc_empty_inactive_list_flag
= 1;
767 list_del_init(&head
->lru
);
768 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
769 atomic_inc(&conf
->empty_inactive_list_nr
);
771 head
->group
->stripes_cnt
--;
775 atomic_inc(&head
->count
);
776 spin_unlock(&conf
->device_lock
);
778 spin_unlock_irq(conf
->hash_locks
+ hash
);
782 if (!stripe_can_batch(head
))
785 lock_two_stripes(head
, sh
);
786 /* clear_batch_ready clear the flag */
787 if (!stripe_can_batch(head
) || !stripe_can_batch(sh
))
794 while (dd_idx
== sh
->pd_idx
|| dd_idx
== sh
->qd_idx
)
796 if (head
->dev
[dd_idx
].towrite
->bi_opf
!= sh
->dev
[dd_idx
].towrite
->bi_opf
||
797 bio_op(head
->dev
[dd_idx
].towrite
) != bio_op(sh
->dev
[dd_idx
].towrite
))
800 if (head
->batch_head
) {
801 spin_lock(&head
->batch_head
->batch_lock
);
802 /* This batch list is already running */
803 if (!stripe_can_batch(head
)) {
804 spin_unlock(&head
->batch_head
->batch_lock
);
809 * at this point, head's BATCH_READY could be cleared, but we
810 * can still add the stripe to batch list
812 list_add(&sh
->batch_list
, &head
->batch_list
);
813 spin_unlock(&head
->batch_head
->batch_lock
);
815 sh
->batch_head
= head
->batch_head
;
817 head
->batch_head
= head
;
818 sh
->batch_head
= head
->batch_head
;
819 spin_lock(&head
->batch_lock
);
820 list_add_tail(&sh
->batch_list
, &head
->batch_list
);
821 spin_unlock(&head
->batch_lock
);
824 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
825 if (atomic_dec_return(&conf
->preread_active_stripes
)
827 md_wakeup_thread(conf
->mddev
->thread
);
829 if (test_and_clear_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
830 int seq
= sh
->bm_seq
;
831 if (test_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
) &&
832 sh
->batch_head
->bm_seq
> seq
)
833 seq
= sh
->batch_head
->bm_seq
;
834 set_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
);
835 sh
->batch_head
->bm_seq
= seq
;
838 atomic_inc(&sh
->count
);
840 unlock_two_stripes(head
, sh
);
842 raid5_release_stripe(head
);
845 /* Determine if 'data_offset' or 'new_data_offset' should be used
846 * in this stripe_head.
848 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
850 sector_t progress
= conf
->reshape_progress
;
851 /* Need a memory barrier to make sure we see the value
852 * of conf->generation, or ->data_offset that was set before
853 * reshape_progress was updated.
856 if (progress
== MaxSector
)
858 if (sh
->generation
== conf
->generation
- 1)
860 /* We are in a reshape, and this is a new-generation stripe,
861 * so use new_data_offset.
867 raid5_end_read_request(struct bio
*bi
);
869 raid5_end_write_request(struct bio
*bi
);
871 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
873 struct r5conf
*conf
= sh
->raid_conf
;
874 int i
, disks
= sh
->disks
;
875 struct stripe_head
*head_sh
= sh
;
879 if (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
)) {
880 /* writing out phase */
881 if (s
->waiting_extra_page
)
883 if (r5l_write_stripe(conf
->log
, sh
) == 0)
885 } else { /* caching phase */
886 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
)) {
887 r5c_cache_data(conf
->log
, sh
, s
);
892 for (i
= disks
; i
--; ) {
893 int op
, op_flags
= 0;
894 int replace_only
= 0;
895 struct bio
*bi
, *rbi
;
896 struct md_rdev
*rdev
, *rrdev
= NULL
;
899 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
901 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
903 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
905 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
907 else if (test_and_clear_bit(R5_WantReplace
,
908 &sh
->dev
[i
].flags
)) {
913 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
914 op_flags
|= REQ_SYNC
;
917 bi
= &sh
->dev
[i
].req
;
918 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
921 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
922 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
923 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
928 if (op_is_write(op
)) {
932 /* We raced and saw duplicates */
935 if (test_bit(R5_ReadRepl
, &head_sh
->dev
[i
].flags
) && rrdev
)
940 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
943 atomic_inc(&rdev
->nr_pending
);
944 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
947 atomic_inc(&rrdev
->nr_pending
);
950 /* We have already checked bad blocks for reads. Now
951 * need to check for writes. We never accept write errors
952 * on the replacement, so we don't to check rrdev.
954 while (op_is_write(op
) && rdev
&&
955 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
958 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
959 &first_bad
, &bad_sectors
);
964 set_bit(BlockedBadBlocks
, &rdev
->flags
);
965 if (!conf
->mddev
->external
&&
966 conf
->mddev
->sb_flags
) {
967 /* It is very unlikely, but we might
968 * still need to write out the
969 * bad block log - better give it
971 md_check_recovery(conf
->mddev
);
974 * Because md_wait_for_blocked_rdev
975 * will dec nr_pending, we must
976 * increment it first.
978 atomic_inc(&rdev
->nr_pending
);
979 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
981 /* Acknowledged bad block - skip the write */
982 rdev_dec_pending(rdev
, conf
->mddev
);
988 if (s
->syncing
|| s
->expanding
|| s
->expanded
990 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
992 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
994 bi
->bi_bdev
= rdev
->bdev
;
995 bio_set_op_attrs(bi
, op
, op_flags
);
996 bi
->bi_end_io
= op_is_write(op
)
997 ? raid5_end_write_request
998 : raid5_end_read_request
;
1001 pr_debug("%s: for %llu schedule op %d on disc %d\n",
1002 __func__
, (unsigned long long)sh
->sector
,
1004 atomic_inc(&sh
->count
);
1006 atomic_inc(&head_sh
->count
);
1007 if (use_new_offset(conf
, sh
))
1008 bi
->bi_iter
.bi_sector
= (sh
->sector
1009 + rdev
->new_data_offset
);
1011 bi
->bi_iter
.bi_sector
= (sh
->sector
1012 + rdev
->data_offset
);
1013 if (test_bit(R5_ReadNoMerge
, &head_sh
->dev
[i
].flags
))
1014 bi
->bi_opf
|= REQ_NOMERGE
;
1016 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1017 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1018 sh
->dev
[i
].vec
.bv_page
= sh
->dev
[i
].page
;
1020 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1021 bi
->bi_io_vec
[0].bv_offset
= 0;
1022 bi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1024 * If this is discard request, set bi_vcnt 0. We don't
1025 * want to confuse SCSI because SCSI will replace payload
1027 if (op
== REQ_OP_DISCARD
)
1030 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
1032 if (conf
->mddev
->gendisk
)
1033 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
1034 bi
, disk_devt(conf
->mddev
->gendisk
),
1036 generic_make_request(bi
);
1039 if (s
->syncing
|| s
->expanding
|| s
->expanded
1041 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
1043 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
1045 rbi
->bi_bdev
= rrdev
->bdev
;
1046 bio_set_op_attrs(rbi
, op
, op_flags
);
1047 BUG_ON(!op_is_write(op
));
1048 rbi
->bi_end_io
= raid5_end_write_request
;
1049 rbi
->bi_private
= sh
;
1051 pr_debug("%s: for %llu schedule op %d on "
1052 "replacement disc %d\n",
1053 __func__
, (unsigned long long)sh
->sector
,
1055 atomic_inc(&sh
->count
);
1057 atomic_inc(&head_sh
->count
);
1058 if (use_new_offset(conf
, sh
))
1059 rbi
->bi_iter
.bi_sector
= (sh
->sector
1060 + rrdev
->new_data_offset
);
1062 rbi
->bi_iter
.bi_sector
= (sh
->sector
1063 + rrdev
->data_offset
);
1064 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1065 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1066 sh
->dev
[i
].rvec
.bv_page
= sh
->dev
[i
].page
;
1068 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1069 rbi
->bi_io_vec
[0].bv_offset
= 0;
1070 rbi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1072 * If this is discard request, set bi_vcnt 0. We don't
1073 * want to confuse SCSI because SCSI will replace payload
1075 if (op
== REQ_OP_DISCARD
)
1077 if (conf
->mddev
->gendisk
)
1078 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
1079 rbi
, disk_devt(conf
->mddev
->gendisk
),
1081 generic_make_request(rbi
);
1083 if (!rdev
&& !rrdev
) {
1084 if (op_is_write(op
))
1085 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1086 pr_debug("skip op %d on disc %d for sector %llu\n",
1087 bi
->bi_opf
, i
, (unsigned long long)sh
->sector
);
1088 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1089 set_bit(STRIPE_HANDLE
, &sh
->state
);
1092 if (!head_sh
->batch_head
)
1094 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1101 static struct dma_async_tx_descriptor
*
1102 async_copy_data(int frombio
, struct bio
*bio
, struct page
**page
,
1103 sector_t sector
, struct dma_async_tx_descriptor
*tx
,
1104 struct stripe_head
*sh
, int no_skipcopy
)
1107 struct bvec_iter iter
;
1108 struct page
*bio_page
;
1110 struct async_submit_ctl submit
;
1111 enum async_tx_flags flags
= 0;
1113 if (bio
->bi_iter
.bi_sector
>= sector
)
1114 page_offset
= (signed)(bio
->bi_iter
.bi_sector
- sector
) * 512;
1116 page_offset
= (signed)(sector
- bio
->bi_iter
.bi_sector
) * -512;
1119 flags
|= ASYNC_TX_FENCE
;
1120 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
1122 bio_for_each_segment(bvl
, bio
, iter
) {
1123 int len
= bvl
.bv_len
;
1127 if (page_offset
< 0) {
1128 b_offset
= -page_offset
;
1129 page_offset
+= b_offset
;
1133 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
1134 clen
= STRIPE_SIZE
- page_offset
;
1139 b_offset
+= bvl
.bv_offset
;
1140 bio_page
= bvl
.bv_page
;
1142 if (sh
->raid_conf
->skip_copy
&&
1143 b_offset
== 0 && page_offset
== 0 &&
1144 clen
== STRIPE_SIZE
&&
1148 tx
= async_memcpy(*page
, bio_page
, page_offset
,
1149 b_offset
, clen
, &submit
);
1151 tx
= async_memcpy(bio_page
, *page
, b_offset
,
1152 page_offset
, clen
, &submit
);
1154 /* chain the operations */
1155 submit
.depend_tx
= tx
;
1157 if (clen
< len
) /* hit end of page */
1165 static void ops_complete_biofill(void *stripe_head_ref
)
1167 struct stripe_head
*sh
= stripe_head_ref
;
1168 struct bio_list return_bi
= BIO_EMPTY_LIST
;
1171 pr_debug("%s: stripe %llu\n", __func__
,
1172 (unsigned long long)sh
->sector
);
1174 /* clear completed biofills */
1175 for (i
= sh
->disks
; i
--; ) {
1176 struct r5dev
*dev
= &sh
->dev
[i
];
1178 /* acknowledge completion of a biofill operation */
1179 /* and check if we need to reply to a read request,
1180 * new R5_Wantfill requests are held off until
1181 * !STRIPE_BIOFILL_RUN
1183 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
1184 struct bio
*rbi
, *rbi2
;
1189 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1190 dev
->sector
+ STRIPE_SECTORS
) {
1191 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1192 if (!raid5_dec_bi_active_stripes(rbi
))
1193 bio_list_add(&return_bi
, rbi
);
1198 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
1200 return_io(&return_bi
);
1202 set_bit(STRIPE_HANDLE
, &sh
->state
);
1203 raid5_release_stripe(sh
);
1206 static void ops_run_biofill(struct stripe_head
*sh
)
1208 struct dma_async_tx_descriptor
*tx
= NULL
;
1209 struct async_submit_ctl submit
;
1212 BUG_ON(sh
->batch_head
);
1213 pr_debug("%s: stripe %llu\n", __func__
,
1214 (unsigned long long)sh
->sector
);
1216 for (i
= sh
->disks
; i
--; ) {
1217 struct r5dev
*dev
= &sh
->dev
[i
];
1218 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
1220 spin_lock_irq(&sh
->stripe_lock
);
1221 dev
->read
= rbi
= dev
->toread
;
1223 spin_unlock_irq(&sh
->stripe_lock
);
1224 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1225 dev
->sector
+ STRIPE_SECTORS
) {
1226 tx
= async_copy_data(0, rbi
, &dev
->page
,
1227 dev
->sector
, tx
, sh
, 0);
1228 rbi
= r5_next_bio(rbi
, dev
->sector
);
1233 atomic_inc(&sh
->count
);
1234 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
1235 async_trigger_callback(&submit
);
1238 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
1245 tgt
= &sh
->dev
[target
];
1246 set_bit(R5_UPTODATE
, &tgt
->flags
);
1247 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1248 clear_bit(R5_Wantcompute
, &tgt
->flags
);
1251 static void ops_complete_compute(void *stripe_head_ref
)
1253 struct stripe_head
*sh
= stripe_head_ref
;
1255 pr_debug("%s: stripe %llu\n", __func__
,
1256 (unsigned long long)sh
->sector
);
1258 /* mark the computed target(s) as uptodate */
1259 mark_target_uptodate(sh
, sh
->ops
.target
);
1260 mark_target_uptodate(sh
, sh
->ops
.target2
);
1262 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
1263 if (sh
->check_state
== check_state_compute_run
)
1264 sh
->check_state
= check_state_compute_result
;
1265 set_bit(STRIPE_HANDLE
, &sh
->state
);
1266 raid5_release_stripe(sh
);
1269 /* return a pointer to the address conversion region of the scribble buffer */
1270 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
1271 struct raid5_percpu
*percpu
, int i
)
1275 addr
= flex_array_get(percpu
->scribble
, i
);
1276 return addr
+ sizeof(struct page
*) * (sh
->disks
+ 2);
1279 /* return a pointer to the address conversion region of the scribble buffer */
1280 static struct page
**to_addr_page(struct raid5_percpu
*percpu
, int i
)
1284 addr
= flex_array_get(percpu
->scribble
, i
);
1288 static struct dma_async_tx_descriptor
*
1289 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1291 int disks
= sh
->disks
;
1292 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1293 int target
= sh
->ops
.target
;
1294 struct r5dev
*tgt
= &sh
->dev
[target
];
1295 struct page
*xor_dest
= tgt
->page
;
1297 struct dma_async_tx_descriptor
*tx
;
1298 struct async_submit_ctl submit
;
1301 BUG_ON(sh
->batch_head
);
1303 pr_debug("%s: stripe %llu block: %d\n",
1304 __func__
, (unsigned long long)sh
->sector
, target
);
1305 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1307 for (i
= disks
; i
--; )
1309 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1311 atomic_inc(&sh
->count
);
1313 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
1314 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
, 0));
1315 if (unlikely(count
== 1))
1316 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1318 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1323 /* set_syndrome_sources - populate source buffers for gen_syndrome
1324 * @srcs - (struct page *) array of size sh->disks
1325 * @sh - stripe_head to parse
1327 * Populates srcs in proper layout order for the stripe and returns the
1328 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1329 * destination buffer is recorded in srcs[count] and the Q destination
1330 * is recorded in srcs[count+1]].
1332 static int set_syndrome_sources(struct page
**srcs
,
1333 struct stripe_head
*sh
,
1336 int disks
= sh
->disks
;
1337 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1338 int d0_idx
= raid6_d0(sh
);
1342 for (i
= 0; i
< disks
; i
++)
1348 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1349 struct r5dev
*dev
= &sh
->dev
[i
];
1351 if (i
== sh
->qd_idx
|| i
== sh
->pd_idx
||
1352 (srctype
== SYNDROME_SRC_ALL
) ||
1353 (srctype
== SYNDROME_SRC_WANT_DRAIN
&&
1354 (test_bit(R5_Wantdrain
, &dev
->flags
) ||
1355 test_bit(R5_InJournal
, &dev
->flags
))) ||
1356 (srctype
== SYNDROME_SRC_WRITTEN
&&
1358 if (test_bit(R5_InJournal
, &dev
->flags
))
1359 srcs
[slot
] = sh
->dev
[i
].orig_page
;
1361 srcs
[slot
] = sh
->dev
[i
].page
;
1363 i
= raid6_next_disk(i
, disks
);
1364 } while (i
!= d0_idx
);
1366 return syndrome_disks
;
1369 static struct dma_async_tx_descriptor
*
1370 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1372 int disks
= sh
->disks
;
1373 struct page
**blocks
= to_addr_page(percpu
, 0);
1375 int qd_idx
= sh
->qd_idx
;
1376 struct dma_async_tx_descriptor
*tx
;
1377 struct async_submit_ctl submit
;
1383 BUG_ON(sh
->batch_head
);
1384 if (sh
->ops
.target
< 0)
1385 target
= sh
->ops
.target2
;
1386 else if (sh
->ops
.target2
< 0)
1387 target
= sh
->ops
.target
;
1389 /* we should only have one valid target */
1392 pr_debug("%s: stripe %llu block: %d\n",
1393 __func__
, (unsigned long long)sh
->sector
, target
);
1395 tgt
= &sh
->dev
[target
];
1396 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1399 atomic_inc(&sh
->count
);
1401 if (target
== qd_idx
) {
1402 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1403 blocks
[count
] = NULL
; /* regenerating p is not necessary */
1404 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
1405 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1406 ops_complete_compute
, sh
,
1407 to_addr_conv(sh
, percpu
, 0));
1408 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1410 /* Compute any data- or p-drive using XOR */
1412 for (i
= disks
; i
-- ; ) {
1413 if (i
== target
|| i
== qd_idx
)
1415 blocks
[count
++] = sh
->dev
[i
].page
;
1418 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1419 NULL
, ops_complete_compute
, sh
,
1420 to_addr_conv(sh
, percpu
, 0));
1421 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1427 static struct dma_async_tx_descriptor
*
1428 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1430 int i
, count
, disks
= sh
->disks
;
1431 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1432 int d0_idx
= raid6_d0(sh
);
1433 int faila
= -1, failb
= -1;
1434 int target
= sh
->ops
.target
;
1435 int target2
= sh
->ops
.target2
;
1436 struct r5dev
*tgt
= &sh
->dev
[target
];
1437 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1438 struct dma_async_tx_descriptor
*tx
;
1439 struct page
**blocks
= to_addr_page(percpu
, 0);
1440 struct async_submit_ctl submit
;
1442 BUG_ON(sh
->batch_head
);
1443 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1444 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1445 BUG_ON(target
< 0 || target2
< 0);
1446 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1447 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1449 /* we need to open-code set_syndrome_sources to handle the
1450 * slot number conversion for 'faila' and 'failb'
1452 for (i
= 0; i
< disks
; i
++)
1457 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1459 blocks
[slot
] = sh
->dev
[i
].page
;
1465 i
= raid6_next_disk(i
, disks
);
1466 } while (i
!= d0_idx
);
1468 BUG_ON(faila
== failb
);
1471 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1472 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1474 atomic_inc(&sh
->count
);
1476 if (failb
== syndrome_disks
+1) {
1477 /* Q disk is one of the missing disks */
1478 if (faila
== syndrome_disks
) {
1479 /* Missing P+Q, just recompute */
1480 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1481 ops_complete_compute
, sh
,
1482 to_addr_conv(sh
, percpu
, 0));
1483 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1484 STRIPE_SIZE
, &submit
);
1488 int qd_idx
= sh
->qd_idx
;
1490 /* Missing D+Q: recompute D from P, then recompute Q */
1491 if (target
== qd_idx
)
1492 data_target
= target2
;
1494 data_target
= target
;
1497 for (i
= disks
; i
-- ; ) {
1498 if (i
== data_target
|| i
== qd_idx
)
1500 blocks
[count
++] = sh
->dev
[i
].page
;
1502 dest
= sh
->dev
[data_target
].page
;
1503 init_async_submit(&submit
,
1504 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1506 to_addr_conv(sh
, percpu
, 0));
1507 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1510 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1511 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1512 ops_complete_compute
, sh
,
1513 to_addr_conv(sh
, percpu
, 0));
1514 return async_gen_syndrome(blocks
, 0, count
+2,
1515 STRIPE_SIZE
, &submit
);
1518 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1519 ops_complete_compute
, sh
,
1520 to_addr_conv(sh
, percpu
, 0));
1521 if (failb
== syndrome_disks
) {
1522 /* We're missing D+P. */
1523 return async_raid6_datap_recov(syndrome_disks
+2,
1527 /* We're missing D+D. */
1528 return async_raid6_2data_recov(syndrome_disks
+2,
1529 STRIPE_SIZE
, faila
, failb
,
1535 static void ops_complete_prexor(void *stripe_head_ref
)
1537 struct stripe_head
*sh
= stripe_head_ref
;
1539 pr_debug("%s: stripe %llu\n", __func__
,
1540 (unsigned long long)sh
->sector
);
1542 if (r5c_is_writeback(sh
->raid_conf
->log
))
1544 * raid5-cache write back uses orig_page during prexor.
1545 * After prexor, it is time to free orig_page
1547 r5c_release_extra_page(sh
);
1550 static struct dma_async_tx_descriptor
*
1551 ops_run_prexor5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1552 struct dma_async_tx_descriptor
*tx
)
1554 int disks
= sh
->disks
;
1555 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1556 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1557 struct async_submit_ctl submit
;
1559 /* existing parity data subtracted */
1560 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1562 BUG_ON(sh
->batch_head
);
1563 pr_debug("%s: stripe %llu\n", __func__
,
1564 (unsigned long long)sh
->sector
);
1566 for (i
= disks
; i
--; ) {
1567 struct r5dev
*dev
= &sh
->dev
[i
];
1568 /* Only process blocks that are known to be uptodate */
1569 if (test_bit(R5_InJournal
, &dev
->flags
))
1570 xor_srcs
[count
++] = dev
->orig_page
;
1571 else if (test_bit(R5_Wantdrain
, &dev
->flags
))
1572 xor_srcs
[count
++] = dev
->page
;
1575 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1576 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1577 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1582 static struct dma_async_tx_descriptor
*
1583 ops_run_prexor6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1584 struct dma_async_tx_descriptor
*tx
)
1586 struct page
**blocks
= to_addr_page(percpu
, 0);
1588 struct async_submit_ctl submit
;
1590 pr_debug("%s: stripe %llu\n", __func__
,
1591 (unsigned long long)sh
->sector
);
1593 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_WANT_DRAIN
);
1595 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_PQ_XOR_DST
, tx
,
1596 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1597 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1602 static struct dma_async_tx_descriptor
*
1603 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1605 struct r5conf
*conf
= sh
->raid_conf
;
1606 int disks
= sh
->disks
;
1608 struct stripe_head
*head_sh
= sh
;
1610 pr_debug("%s: stripe %llu\n", __func__
,
1611 (unsigned long long)sh
->sector
);
1613 for (i
= disks
; i
--; ) {
1618 if (test_and_clear_bit(R5_Wantdrain
, &head_sh
->dev
[i
].flags
)) {
1624 * clear R5_InJournal, so when rewriting a page in
1625 * journal, it is not skipped by r5l_log_stripe()
1627 clear_bit(R5_InJournal
, &dev
->flags
);
1628 spin_lock_irq(&sh
->stripe_lock
);
1629 chosen
= dev
->towrite
;
1630 dev
->towrite
= NULL
;
1631 sh
->overwrite_disks
= 0;
1632 BUG_ON(dev
->written
);
1633 wbi
= dev
->written
= chosen
;
1634 spin_unlock_irq(&sh
->stripe_lock
);
1635 WARN_ON(dev
->page
!= dev
->orig_page
);
1637 while (wbi
&& wbi
->bi_iter
.bi_sector
<
1638 dev
->sector
+ STRIPE_SECTORS
) {
1639 if (wbi
->bi_opf
& REQ_FUA
)
1640 set_bit(R5_WantFUA
, &dev
->flags
);
1641 if (wbi
->bi_opf
& REQ_SYNC
)
1642 set_bit(R5_SyncIO
, &dev
->flags
);
1643 if (bio_op(wbi
) == REQ_OP_DISCARD
)
1644 set_bit(R5_Discard
, &dev
->flags
);
1646 tx
= async_copy_data(1, wbi
, &dev
->page
,
1647 dev
->sector
, tx
, sh
,
1648 r5c_is_writeback(conf
->log
));
1649 if (dev
->page
!= dev
->orig_page
&&
1650 !r5c_is_writeback(conf
->log
)) {
1651 set_bit(R5_SkipCopy
, &dev
->flags
);
1652 clear_bit(R5_UPTODATE
, &dev
->flags
);
1653 clear_bit(R5_OVERWRITE
, &dev
->flags
);
1656 wbi
= r5_next_bio(wbi
, dev
->sector
);
1659 if (head_sh
->batch_head
) {
1660 sh
= list_first_entry(&sh
->batch_list
,
1673 static void ops_complete_reconstruct(void *stripe_head_ref
)
1675 struct stripe_head
*sh
= stripe_head_ref
;
1676 int disks
= sh
->disks
;
1677 int pd_idx
= sh
->pd_idx
;
1678 int qd_idx
= sh
->qd_idx
;
1680 bool fua
= false, sync
= false, discard
= false;
1682 pr_debug("%s: stripe %llu\n", __func__
,
1683 (unsigned long long)sh
->sector
);
1685 for (i
= disks
; i
--; ) {
1686 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1687 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1688 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1691 for (i
= disks
; i
--; ) {
1692 struct r5dev
*dev
= &sh
->dev
[i
];
1694 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1695 if (!discard
&& !test_bit(R5_SkipCopy
, &dev
->flags
))
1696 set_bit(R5_UPTODATE
, &dev
->flags
);
1698 set_bit(R5_WantFUA
, &dev
->flags
);
1700 set_bit(R5_SyncIO
, &dev
->flags
);
1704 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1705 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1706 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1707 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1709 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1710 sh
->reconstruct_state
= reconstruct_state_result
;
1713 set_bit(STRIPE_HANDLE
, &sh
->state
);
1714 raid5_release_stripe(sh
);
1718 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1719 struct dma_async_tx_descriptor
*tx
)
1721 int disks
= sh
->disks
;
1722 struct page
**xor_srcs
;
1723 struct async_submit_ctl submit
;
1724 int count
, pd_idx
= sh
->pd_idx
, i
;
1725 struct page
*xor_dest
;
1727 unsigned long flags
;
1729 struct stripe_head
*head_sh
= sh
;
1732 pr_debug("%s: stripe %llu\n", __func__
,
1733 (unsigned long long)sh
->sector
);
1735 for (i
= 0; i
< sh
->disks
; i
++) {
1738 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1741 if (i
>= sh
->disks
) {
1742 atomic_inc(&sh
->count
);
1743 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1744 ops_complete_reconstruct(sh
);
1749 xor_srcs
= to_addr_page(percpu
, j
);
1750 /* check if prexor is active which means only process blocks
1751 * that are part of a read-modify-write (written)
1753 if (head_sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1755 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1756 for (i
= disks
; i
--; ) {
1757 struct r5dev
*dev
= &sh
->dev
[i
];
1758 if (head_sh
->dev
[i
].written
||
1759 test_bit(R5_InJournal
, &head_sh
->dev
[i
].flags
))
1760 xor_srcs
[count
++] = dev
->page
;
1763 xor_dest
= sh
->dev
[pd_idx
].page
;
1764 for (i
= disks
; i
--; ) {
1765 struct r5dev
*dev
= &sh
->dev
[i
];
1767 xor_srcs
[count
++] = dev
->page
;
1771 /* 1/ if we prexor'd then the dest is reused as a source
1772 * 2/ if we did not prexor then we are redoing the parity
1773 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1774 * for the synchronous xor case
1776 last_stripe
= !head_sh
->batch_head
||
1777 list_first_entry(&sh
->batch_list
,
1778 struct stripe_head
, batch_list
) == head_sh
;
1780 flags
= ASYNC_TX_ACK
|
1781 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1783 atomic_inc(&head_sh
->count
);
1784 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, head_sh
,
1785 to_addr_conv(sh
, percpu
, j
));
1787 flags
= prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
;
1788 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
,
1789 to_addr_conv(sh
, percpu
, j
));
1792 if (unlikely(count
== 1))
1793 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1795 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1798 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1805 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1806 struct dma_async_tx_descriptor
*tx
)
1808 struct async_submit_ctl submit
;
1809 struct page
**blocks
;
1810 int count
, i
, j
= 0;
1811 struct stripe_head
*head_sh
= sh
;
1814 unsigned long txflags
;
1816 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1818 for (i
= 0; i
< sh
->disks
; i
++) {
1819 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1821 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1824 if (i
>= sh
->disks
) {
1825 atomic_inc(&sh
->count
);
1826 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1827 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1828 ops_complete_reconstruct(sh
);
1833 blocks
= to_addr_page(percpu
, j
);
1835 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1836 synflags
= SYNDROME_SRC_WRITTEN
;
1837 txflags
= ASYNC_TX_ACK
| ASYNC_TX_PQ_XOR_DST
;
1839 synflags
= SYNDROME_SRC_ALL
;
1840 txflags
= ASYNC_TX_ACK
;
1843 count
= set_syndrome_sources(blocks
, sh
, synflags
);
1844 last_stripe
= !head_sh
->batch_head
||
1845 list_first_entry(&sh
->batch_list
,
1846 struct stripe_head
, batch_list
) == head_sh
;
1849 atomic_inc(&head_sh
->count
);
1850 init_async_submit(&submit
, txflags
, tx
, ops_complete_reconstruct
,
1851 head_sh
, to_addr_conv(sh
, percpu
, j
));
1853 init_async_submit(&submit
, 0, tx
, NULL
, NULL
,
1854 to_addr_conv(sh
, percpu
, j
));
1855 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1858 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1864 static void ops_complete_check(void *stripe_head_ref
)
1866 struct stripe_head
*sh
= stripe_head_ref
;
1868 pr_debug("%s: stripe %llu\n", __func__
,
1869 (unsigned long long)sh
->sector
);
1871 sh
->check_state
= check_state_check_result
;
1872 set_bit(STRIPE_HANDLE
, &sh
->state
);
1873 raid5_release_stripe(sh
);
1876 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1878 int disks
= sh
->disks
;
1879 int pd_idx
= sh
->pd_idx
;
1880 int qd_idx
= sh
->qd_idx
;
1881 struct page
*xor_dest
;
1882 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1883 struct dma_async_tx_descriptor
*tx
;
1884 struct async_submit_ctl submit
;
1888 pr_debug("%s: stripe %llu\n", __func__
,
1889 (unsigned long long)sh
->sector
);
1891 BUG_ON(sh
->batch_head
);
1893 xor_dest
= sh
->dev
[pd_idx
].page
;
1894 xor_srcs
[count
++] = xor_dest
;
1895 for (i
= disks
; i
--; ) {
1896 if (i
== pd_idx
|| i
== qd_idx
)
1898 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1901 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1902 to_addr_conv(sh
, percpu
, 0));
1903 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1904 &sh
->ops
.zero_sum_result
, &submit
);
1906 atomic_inc(&sh
->count
);
1907 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1908 tx
= async_trigger_callback(&submit
);
1911 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1913 struct page
**srcs
= to_addr_page(percpu
, 0);
1914 struct async_submit_ctl submit
;
1917 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1918 (unsigned long long)sh
->sector
, checkp
);
1920 BUG_ON(sh
->batch_head
);
1921 count
= set_syndrome_sources(srcs
, sh
, SYNDROME_SRC_ALL
);
1925 atomic_inc(&sh
->count
);
1926 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1927 sh
, to_addr_conv(sh
, percpu
, 0));
1928 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1929 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1932 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1934 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1935 struct dma_async_tx_descriptor
*tx
= NULL
;
1936 struct r5conf
*conf
= sh
->raid_conf
;
1937 int level
= conf
->level
;
1938 struct raid5_percpu
*percpu
;
1942 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1943 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1944 ops_run_biofill(sh
);
1948 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1950 tx
= ops_run_compute5(sh
, percpu
);
1952 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1953 tx
= ops_run_compute6_1(sh
, percpu
);
1955 tx
= ops_run_compute6_2(sh
, percpu
);
1957 /* terminate the chain if reconstruct is not set to be run */
1958 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1962 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
)) {
1964 tx
= ops_run_prexor5(sh
, percpu
, tx
);
1966 tx
= ops_run_prexor6(sh
, percpu
, tx
);
1969 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1970 tx
= ops_run_biodrain(sh
, tx
);
1974 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1976 ops_run_reconstruct5(sh
, percpu
, tx
);
1978 ops_run_reconstruct6(sh
, percpu
, tx
);
1981 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1982 if (sh
->check_state
== check_state_run
)
1983 ops_run_check_p(sh
, percpu
);
1984 else if (sh
->check_state
== check_state_run_q
)
1985 ops_run_check_pq(sh
, percpu
, 0);
1986 else if (sh
->check_state
== check_state_run_pq
)
1987 ops_run_check_pq(sh
, percpu
, 1);
1992 if (overlap_clear
&& !sh
->batch_head
)
1993 for (i
= disks
; i
--; ) {
1994 struct r5dev
*dev
= &sh
->dev
[i
];
1995 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1996 wake_up(&sh
->raid_conf
->wait_for_overlap
);
2001 static struct stripe_head
*alloc_stripe(struct kmem_cache
*sc
, gfp_t gfp
,
2004 struct stripe_head
*sh
;
2007 sh
= kmem_cache_zalloc(sc
, gfp
);
2009 spin_lock_init(&sh
->stripe_lock
);
2010 spin_lock_init(&sh
->batch_lock
);
2011 INIT_LIST_HEAD(&sh
->batch_list
);
2012 INIT_LIST_HEAD(&sh
->lru
);
2013 INIT_LIST_HEAD(&sh
->r5c
);
2014 INIT_LIST_HEAD(&sh
->log_list
);
2015 atomic_set(&sh
->count
, 1);
2016 sh
->log_start
= MaxSector
;
2017 for (i
= 0; i
< disks
; i
++) {
2018 struct r5dev
*dev
= &sh
->dev
[i
];
2020 bio_init(&dev
->req
, &dev
->vec
, 1);
2021 bio_init(&dev
->rreq
, &dev
->rvec
, 1);
2026 static int grow_one_stripe(struct r5conf
*conf
, gfp_t gfp
)
2028 struct stripe_head
*sh
;
2030 sh
= alloc_stripe(conf
->slab_cache
, gfp
, conf
->pool_size
);
2034 sh
->raid_conf
= conf
;
2036 if (grow_buffers(sh
, gfp
)) {
2038 kmem_cache_free(conf
->slab_cache
, sh
);
2041 sh
->hash_lock_index
=
2042 conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
;
2043 /* we just created an active stripe so... */
2044 atomic_inc(&conf
->active_stripes
);
2046 raid5_release_stripe(sh
);
2047 conf
->max_nr_stripes
++;
2051 static int grow_stripes(struct r5conf
*conf
, int num
)
2053 struct kmem_cache
*sc
;
2054 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
2056 if (conf
->mddev
->gendisk
)
2057 sprintf(conf
->cache_name
[0],
2058 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
2060 sprintf(conf
->cache_name
[0],
2061 "raid%d-%p", conf
->level
, conf
->mddev
);
2062 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
2064 conf
->active_name
= 0;
2065 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
2066 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
2070 conf
->slab_cache
= sc
;
2071 conf
->pool_size
= devs
;
2073 if (!grow_one_stripe(conf
, GFP_KERNEL
))
2080 * scribble_len - return the required size of the scribble region
2081 * @num - total number of disks in the array
2083 * The size must be enough to contain:
2084 * 1/ a struct page pointer for each device in the array +2
2085 * 2/ room to convert each entry in (1) to its corresponding dma
2086 * (dma_map_page()) or page (page_address()) address.
2088 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2089 * calculate over all devices (not just the data blocks), using zeros in place
2090 * of the P and Q blocks.
2092 static struct flex_array
*scribble_alloc(int num
, int cnt
, gfp_t flags
)
2094 struct flex_array
*ret
;
2097 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
2098 ret
= flex_array_alloc(len
, cnt
, flags
);
2101 /* always prealloc all elements, so no locking is required */
2102 if (flex_array_prealloc(ret
, 0, cnt
, flags
)) {
2103 flex_array_free(ret
);
2109 static int resize_chunks(struct r5conf
*conf
, int new_disks
, int new_sectors
)
2115 * Never shrink. And mddev_suspend() could deadlock if this is called
2116 * from raid5d. In that case, scribble_disks and scribble_sectors
2117 * should equal to new_disks and new_sectors
2119 if (conf
->scribble_disks
>= new_disks
&&
2120 conf
->scribble_sectors
>= new_sectors
)
2122 mddev_suspend(conf
->mddev
);
2124 for_each_present_cpu(cpu
) {
2125 struct raid5_percpu
*percpu
;
2126 struct flex_array
*scribble
;
2128 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
2129 scribble
= scribble_alloc(new_disks
,
2130 new_sectors
/ STRIPE_SECTORS
,
2134 flex_array_free(percpu
->scribble
);
2135 percpu
->scribble
= scribble
;
2142 mddev_resume(conf
->mddev
);
2144 conf
->scribble_disks
= new_disks
;
2145 conf
->scribble_sectors
= new_sectors
;
2150 static int resize_stripes(struct r5conf
*conf
, int newsize
)
2152 /* Make all the stripes able to hold 'newsize' devices.
2153 * New slots in each stripe get 'page' set to a new page.
2155 * This happens in stages:
2156 * 1/ create a new kmem_cache and allocate the required number of
2158 * 2/ gather all the old stripe_heads and transfer the pages across
2159 * to the new stripe_heads. This will have the side effect of
2160 * freezing the array as once all stripe_heads have been collected,
2161 * no IO will be possible. Old stripe heads are freed once their
2162 * pages have been transferred over, and the old kmem_cache is
2163 * freed when all stripes are done.
2164 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2165 * we simple return a failre status - no need to clean anything up.
2166 * 4/ allocate new pages for the new slots in the new stripe_heads.
2167 * If this fails, we don't bother trying the shrink the
2168 * stripe_heads down again, we just leave them as they are.
2169 * As each stripe_head is processed the new one is released into
2172 * Once step2 is started, we cannot afford to wait for a write,
2173 * so we use GFP_NOIO allocations.
2175 struct stripe_head
*osh
, *nsh
;
2176 LIST_HEAD(newstripes
);
2177 struct disk_info
*ndisks
;
2179 struct kmem_cache
*sc
;
2183 if (newsize
<= conf
->pool_size
)
2184 return 0; /* never bother to shrink */
2186 err
= md_allow_write(conf
->mddev
);
2191 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
2192 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
2197 /* Need to ensure auto-resizing doesn't interfere */
2198 mutex_lock(&conf
->cache_size_mutex
);
2200 for (i
= conf
->max_nr_stripes
; i
; i
--) {
2201 nsh
= alloc_stripe(sc
, GFP_KERNEL
, newsize
);
2205 nsh
->raid_conf
= conf
;
2206 list_add(&nsh
->lru
, &newstripes
);
2209 /* didn't get enough, give up */
2210 while (!list_empty(&newstripes
)) {
2211 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2212 list_del(&nsh
->lru
);
2213 kmem_cache_free(sc
, nsh
);
2215 kmem_cache_destroy(sc
);
2216 mutex_unlock(&conf
->cache_size_mutex
);
2219 /* Step 2 - Must use GFP_NOIO now.
2220 * OK, we have enough stripes, start collecting inactive
2221 * stripes and copying them over
2225 list_for_each_entry(nsh
, &newstripes
, lru
) {
2226 lock_device_hash_lock(conf
, hash
);
2227 wait_event_cmd(conf
->wait_for_stripe
,
2228 !list_empty(conf
->inactive_list
+ hash
),
2229 unlock_device_hash_lock(conf
, hash
),
2230 lock_device_hash_lock(conf
, hash
));
2231 osh
= get_free_stripe(conf
, hash
);
2232 unlock_device_hash_lock(conf
, hash
);
2234 for(i
=0; i
<conf
->pool_size
; i
++) {
2235 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
2236 nsh
->dev
[i
].orig_page
= osh
->dev
[i
].page
;
2238 nsh
->hash_lock_index
= hash
;
2239 kmem_cache_free(conf
->slab_cache
, osh
);
2241 if (cnt
>= conf
->max_nr_stripes
/ NR_STRIPE_HASH_LOCKS
+
2242 !!((conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
) > hash
)) {
2247 kmem_cache_destroy(conf
->slab_cache
);
2250 * At this point, we are holding all the stripes so the array
2251 * is completely stalled, so now is a good time to resize
2252 * conf->disks and the scribble region
2254 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
2256 for (i
= 0; i
< conf
->pool_size
; i
++)
2257 ndisks
[i
] = conf
->disks
[i
];
2259 for (i
= conf
->pool_size
; i
< newsize
; i
++) {
2260 ndisks
[i
].extra_page
= alloc_page(GFP_NOIO
);
2261 if (!ndisks
[i
].extra_page
)
2266 for (i
= conf
->pool_size
; i
< newsize
; i
++)
2267 if (ndisks
[i
].extra_page
)
2268 put_page(ndisks
[i
].extra_page
);
2272 conf
->disks
= ndisks
;
2277 mutex_unlock(&conf
->cache_size_mutex
);
2278 /* Step 4, return new stripes to service */
2279 while(!list_empty(&newstripes
)) {
2280 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2281 list_del_init(&nsh
->lru
);
2283 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
2284 if (nsh
->dev
[i
].page
== NULL
) {
2285 struct page
*p
= alloc_page(GFP_NOIO
);
2286 nsh
->dev
[i
].page
= p
;
2287 nsh
->dev
[i
].orig_page
= p
;
2291 raid5_release_stripe(nsh
);
2293 /* critical section pass, GFP_NOIO no longer needed */
2295 conf
->slab_cache
= sc
;
2296 conf
->active_name
= 1-conf
->active_name
;
2298 conf
->pool_size
= newsize
;
2302 static int drop_one_stripe(struct r5conf
*conf
)
2304 struct stripe_head
*sh
;
2305 int hash
= (conf
->max_nr_stripes
- 1) & STRIPE_HASH_LOCKS_MASK
;
2307 spin_lock_irq(conf
->hash_locks
+ hash
);
2308 sh
= get_free_stripe(conf
, hash
);
2309 spin_unlock_irq(conf
->hash_locks
+ hash
);
2312 BUG_ON(atomic_read(&sh
->count
));
2314 kmem_cache_free(conf
->slab_cache
, sh
);
2315 atomic_dec(&conf
->active_stripes
);
2316 conf
->max_nr_stripes
--;
2320 static void shrink_stripes(struct r5conf
*conf
)
2322 while (conf
->max_nr_stripes
&&
2323 drop_one_stripe(conf
))
2326 kmem_cache_destroy(conf
->slab_cache
);
2327 conf
->slab_cache
= NULL
;
2330 static void raid5_end_read_request(struct bio
* bi
)
2332 struct stripe_head
*sh
= bi
->bi_private
;
2333 struct r5conf
*conf
= sh
->raid_conf
;
2334 int disks
= sh
->disks
, i
;
2335 char b
[BDEVNAME_SIZE
];
2336 struct md_rdev
*rdev
= NULL
;
2339 for (i
=0 ; i
<disks
; i
++)
2340 if (bi
== &sh
->dev
[i
].req
)
2343 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2344 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2351 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2352 /* If replacement finished while this request was outstanding,
2353 * 'replacement' might be NULL already.
2354 * In that case it moved down to 'rdev'.
2355 * rdev is not removed until all requests are finished.
2357 rdev
= conf
->disks
[i
].replacement
;
2359 rdev
= conf
->disks
[i
].rdev
;
2361 if (use_new_offset(conf
, sh
))
2362 s
= sh
->sector
+ rdev
->new_data_offset
;
2364 s
= sh
->sector
+ rdev
->data_offset
;
2365 if (!bi
->bi_error
) {
2366 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2367 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2368 /* Note that this cannot happen on a
2369 * replacement device. We just fail those on
2372 pr_info_ratelimited(
2373 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
2374 mdname(conf
->mddev
), STRIPE_SECTORS
,
2375 (unsigned long long)s
,
2376 bdevname(rdev
->bdev
, b
));
2377 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
2378 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2379 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2380 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2381 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2383 if (atomic_read(&rdev
->read_errors
))
2384 atomic_set(&rdev
->read_errors
, 0);
2386 const char *bdn
= bdevname(rdev
->bdev
, b
);
2390 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2391 atomic_inc(&rdev
->read_errors
);
2392 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2393 pr_warn_ratelimited(
2394 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
2395 mdname(conf
->mddev
),
2396 (unsigned long long)s
,
2398 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
2400 pr_warn_ratelimited(
2401 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
2402 mdname(conf
->mddev
),
2403 (unsigned long long)s
,
2405 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
2408 pr_warn_ratelimited(
2409 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
2410 mdname(conf
->mddev
),
2411 (unsigned long long)s
,
2413 } else if (atomic_read(&rdev
->read_errors
)
2414 > conf
->max_nr_stripes
)
2415 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2416 mdname(conf
->mddev
), bdn
);
2419 if (set_bad
&& test_bit(In_sync
, &rdev
->flags
)
2420 && !test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2423 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
2424 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2425 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2427 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2429 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2430 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2432 && test_bit(In_sync
, &rdev
->flags
)
2433 && rdev_set_badblocks(
2434 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
2435 md_error(conf
->mddev
, rdev
);
2438 rdev_dec_pending(rdev
, conf
->mddev
);
2440 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2441 set_bit(STRIPE_HANDLE
, &sh
->state
);
2442 raid5_release_stripe(sh
);
2445 static void raid5_end_write_request(struct bio
*bi
)
2447 struct stripe_head
*sh
= bi
->bi_private
;
2448 struct r5conf
*conf
= sh
->raid_conf
;
2449 int disks
= sh
->disks
, i
;
2450 struct md_rdev
*uninitialized_var(rdev
);
2453 int replacement
= 0;
2455 for (i
= 0 ; i
< disks
; i
++) {
2456 if (bi
== &sh
->dev
[i
].req
) {
2457 rdev
= conf
->disks
[i
].rdev
;
2460 if (bi
== &sh
->dev
[i
].rreq
) {
2461 rdev
= conf
->disks
[i
].replacement
;
2465 /* rdev was removed and 'replacement'
2466 * replaced it. rdev is not removed
2467 * until all requests are finished.
2469 rdev
= conf
->disks
[i
].rdev
;
2473 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2474 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2484 md_error(conf
->mddev
, rdev
);
2485 else if (is_badblock(rdev
, sh
->sector
,
2487 &first_bad
, &bad_sectors
))
2488 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
2491 set_bit(STRIPE_DEGRADED
, &sh
->state
);
2492 set_bit(WriteErrorSeen
, &rdev
->flags
);
2493 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
2494 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2495 set_bit(MD_RECOVERY_NEEDED
,
2496 &rdev
->mddev
->recovery
);
2497 } else if (is_badblock(rdev
, sh
->sector
,
2499 &first_bad
, &bad_sectors
)) {
2500 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
2501 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))
2502 /* That was a successful write so make
2503 * sure it looks like we already did
2506 set_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2509 rdev_dec_pending(rdev
, conf
->mddev
);
2511 if (sh
->batch_head
&& bi
->bi_error
&& !replacement
)
2512 set_bit(STRIPE_BATCH_ERR
, &sh
->batch_head
->state
);
2515 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
2516 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2517 set_bit(STRIPE_HANDLE
, &sh
->state
);
2518 raid5_release_stripe(sh
);
2520 if (sh
->batch_head
&& sh
!= sh
->batch_head
)
2521 raid5_release_stripe(sh
->batch_head
);
2524 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
2526 struct r5dev
*dev
= &sh
->dev
[i
];
2529 dev
->sector
= raid5_compute_blocknr(sh
, i
, previous
);
2532 static void raid5_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2534 char b
[BDEVNAME_SIZE
];
2535 struct r5conf
*conf
= mddev
->private;
2536 unsigned long flags
;
2537 pr_debug("raid456: error called\n");
2539 spin_lock_irqsave(&conf
->device_lock
, flags
);
2540 clear_bit(In_sync
, &rdev
->flags
);
2541 mddev
->degraded
= calc_degraded(conf
);
2542 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2543 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2545 set_bit(Blocked
, &rdev
->flags
);
2546 set_bit(Faulty
, &rdev
->flags
);
2547 set_mask_bits(&mddev
->sb_flags
, 0,
2548 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
2549 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2550 "md/raid:%s: Operation continuing on %d devices.\n",
2552 bdevname(rdev
->bdev
, b
),
2554 conf
->raid_disks
- mddev
->degraded
);
2558 * Input: a 'big' sector number,
2559 * Output: index of the data and parity disk, and the sector # in them.
2561 sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
2562 int previous
, int *dd_idx
,
2563 struct stripe_head
*sh
)
2565 sector_t stripe
, stripe2
;
2566 sector_t chunk_number
;
2567 unsigned int chunk_offset
;
2570 sector_t new_sector
;
2571 int algorithm
= previous
? conf
->prev_algo
2573 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2574 : conf
->chunk_sectors
;
2575 int raid_disks
= previous
? conf
->previous_raid_disks
2577 int data_disks
= raid_disks
- conf
->max_degraded
;
2579 /* First compute the information on this sector */
2582 * Compute the chunk number and the sector offset inside the chunk
2584 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
2585 chunk_number
= r_sector
;
2588 * Compute the stripe number
2590 stripe
= chunk_number
;
2591 *dd_idx
= sector_div(stripe
, data_disks
);
2594 * Select the parity disk based on the user selected algorithm.
2596 pd_idx
= qd_idx
= -1;
2597 switch(conf
->level
) {
2599 pd_idx
= data_disks
;
2602 switch (algorithm
) {
2603 case ALGORITHM_LEFT_ASYMMETRIC
:
2604 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2605 if (*dd_idx
>= pd_idx
)
2608 case ALGORITHM_RIGHT_ASYMMETRIC
:
2609 pd_idx
= sector_div(stripe2
, raid_disks
);
2610 if (*dd_idx
>= pd_idx
)
2613 case ALGORITHM_LEFT_SYMMETRIC
:
2614 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2615 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2617 case ALGORITHM_RIGHT_SYMMETRIC
:
2618 pd_idx
= sector_div(stripe2
, raid_disks
);
2619 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2621 case ALGORITHM_PARITY_0
:
2625 case ALGORITHM_PARITY_N
:
2626 pd_idx
= data_disks
;
2634 switch (algorithm
) {
2635 case ALGORITHM_LEFT_ASYMMETRIC
:
2636 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2637 qd_idx
= pd_idx
+ 1;
2638 if (pd_idx
== raid_disks
-1) {
2639 (*dd_idx
)++; /* Q D D D P */
2641 } else if (*dd_idx
>= pd_idx
)
2642 (*dd_idx
) += 2; /* D D P Q D */
2644 case ALGORITHM_RIGHT_ASYMMETRIC
:
2645 pd_idx
= sector_div(stripe2
, raid_disks
);
2646 qd_idx
= pd_idx
+ 1;
2647 if (pd_idx
== raid_disks
-1) {
2648 (*dd_idx
)++; /* Q D D D P */
2650 } else if (*dd_idx
>= pd_idx
)
2651 (*dd_idx
) += 2; /* D D P Q D */
2653 case ALGORITHM_LEFT_SYMMETRIC
:
2654 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2655 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2656 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2658 case ALGORITHM_RIGHT_SYMMETRIC
:
2659 pd_idx
= sector_div(stripe2
, raid_disks
);
2660 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2661 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2664 case ALGORITHM_PARITY_0
:
2669 case ALGORITHM_PARITY_N
:
2670 pd_idx
= data_disks
;
2671 qd_idx
= data_disks
+ 1;
2674 case ALGORITHM_ROTATING_ZERO_RESTART
:
2675 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2676 * of blocks for computing Q is different.
2678 pd_idx
= sector_div(stripe2
, raid_disks
);
2679 qd_idx
= pd_idx
+ 1;
2680 if (pd_idx
== raid_disks
-1) {
2681 (*dd_idx
)++; /* Q D D D P */
2683 } else if (*dd_idx
>= pd_idx
)
2684 (*dd_idx
) += 2; /* D D P Q D */
2688 case ALGORITHM_ROTATING_N_RESTART
:
2689 /* Same a left_asymmetric, by first stripe is
2690 * D D D P Q rather than
2694 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2695 qd_idx
= pd_idx
+ 1;
2696 if (pd_idx
== raid_disks
-1) {
2697 (*dd_idx
)++; /* Q D D D P */
2699 } else if (*dd_idx
>= pd_idx
)
2700 (*dd_idx
) += 2; /* D D P Q D */
2704 case ALGORITHM_ROTATING_N_CONTINUE
:
2705 /* Same as left_symmetric but Q is before P */
2706 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2707 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2708 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2712 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2713 /* RAID5 left_asymmetric, with Q on last device */
2714 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2715 if (*dd_idx
>= pd_idx
)
2717 qd_idx
= raid_disks
- 1;
2720 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2721 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2722 if (*dd_idx
>= pd_idx
)
2724 qd_idx
= raid_disks
- 1;
2727 case ALGORITHM_LEFT_SYMMETRIC_6
:
2728 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2729 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2730 qd_idx
= raid_disks
- 1;
2733 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2734 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2735 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2736 qd_idx
= raid_disks
- 1;
2739 case ALGORITHM_PARITY_0_6
:
2742 qd_idx
= raid_disks
- 1;
2752 sh
->pd_idx
= pd_idx
;
2753 sh
->qd_idx
= qd_idx
;
2754 sh
->ddf_layout
= ddf_layout
;
2757 * Finally, compute the new sector number
2759 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2763 sector_t
raid5_compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2765 struct r5conf
*conf
= sh
->raid_conf
;
2766 int raid_disks
= sh
->disks
;
2767 int data_disks
= raid_disks
- conf
->max_degraded
;
2768 sector_t new_sector
= sh
->sector
, check
;
2769 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2770 : conf
->chunk_sectors
;
2771 int algorithm
= previous
? conf
->prev_algo
2775 sector_t chunk_number
;
2776 int dummy1
, dd_idx
= i
;
2778 struct stripe_head sh2
;
2780 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2781 stripe
= new_sector
;
2783 if (i
== sh
->pd_idx
)
2785 switch(conf
->level
) {
2788 switch (algorithm
) {
2789 case ALGORITHM_LEFT_ASYMMETRIC
:
2790 case ALGORITHM_RIGHT_ASYMMETRIC
:
2794 case ALGORITHM_LEFT_SYMMETRIC
:
2795 case ALGORITHM_RIGHT_SYMMETRIC
:
2798 i
-= (sh
->pd_idx
+ 1);
2800 case ALGORITHM_PARITY_0
:
2803 case ALGORITHM_PARITY_N
:
2810 if (i
== sh
->qd_idx
)
2811 return 0; /* It is the Q disk */
2812 switch (algorithm
) {
2813 case ALGORITHM_LEFT_ASYMMETRIC
:
2814 case ALGORITHM_RIGHT_ASYMMETRIC
:
2815 case ALGORITHM_ROTATING_ZERO_RESTART
:
2816 case ALGORITHM_ROTATING_N_RESTART
:
2817 if (sh
->pd_idx
== raid_disks
-1)
2818 i
--; /* Q D D D P */
2819 else if (i
> sh
->pd_idx
)
2820 i
-= 2; /* D D P Q D */
2822 case ALGORITHM_LEFT_SYMMETRIC
:
2823 case ALGORITHM_RIGHT_SYMMETRIC
:
2824 if (sh
->pd_idx
== raid_disks
-1)
2825 i
--; /* Q D D D P */
2830 i
-= (sh
->pd_idx
+ 2);
2833 case ALGORITHM_PARITY_0
:
2836 case ALGORITHM_PARITY_N
:
2838 case ALGORITHM_ROTATING_N_CONTINUE
:
2839 /* Like left_symmetric, but P is before Q */
2840 if (sh
->pd_idx
== 0)
2841 i
--; /* P D D D Q */
2846 i
-= (sh
->pd_idx
+ 1);
2849 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2850 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2854 case ALGORITHM_LEFT_SYMMETRIC_6
:
2855 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2857 i
+= data_disks
+ 1;
2858 i
-= (sh
->pd_idx
+ 1);
2860 case ALGORITHM_PARITY_0_6
:
2869 chunk_number
= stripe
* data_disks
+ i
;
2870 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2872 check
= raid5_compute_sector(conf
, r_sector
,
2873 previous
, &dummy1
, &sh2
);
2874 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2875 || sh2
.qd_idx
!= sh
->qd_idx
) {
2876 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2877 mdname(conf
->mddev
));
2884 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2885 int rcw
, int expand
)
2887 int i
, pd_idx
= sh
->pd_idx
, qd_idx
= sh
->qd_idx
, disks
= sh
->disks
;
2888 struct r5conf
*conf
= sh
->raid_conf
;
2889 int level
= conf
->level
;
2893 * In some cases, handle_stripe_dirtying initially decided to
2894 * run rmw and allocates extra page for prexor. However, rcw is
2895 * cheaper later on. We need to free the extra page now,
2896 * because we won't be able to do that in ops_complete_prexor().
2898 r5c_release_extra_page(sh
);
2900 for (i
= disks
; i
--; ) {
2901 struct r5dev
*dev
= &sh
->dev
[i
];
2904 set_bit(R5_LOCKED
, &dev
->flags
);
2905 set_bit(R5_Wantdrain
, &dev
->flags
);
2907 clear_bit(R5_UPTODATE
, &dev
->flags
);
2909 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
2910 set_bit(R5_LOCKED
, &dev
->flags
);
2914 /* if we are not expanding this is a proper write request, and
2915 * there will be bios with new data to be drained into the
2920 /* False alarm, nothing to do */
2922 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2923 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2925 sh
->reconstruct_state
= reconstruct_state_run
;
2927 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2929 if (s
->locked
+ conf
->max_degraded
== disks
)
2930 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2931 atomic_inc(&conf
->pending_full_writes
);
2933 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2934 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2935 BUG_ON(level
== 6 &&
2936 (!(test_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
) ||
2937 test_bit(R5_Wantcompute
, &sh
->dev
[qd_idx
].flags
))));
2939 for (i
= disks
; i
--; ) {
2940 struct r5dev
*dev
= &sh
->dev
[i
];
2941 if (i
== pd_idx
|| i
== qd_idx
)
2945 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2946 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2947 set_bit(R5_Wantdrain
, &dev
->flags
);
2948 set_bit(R5_LOCKED
, &dev
->flags
);
2949 clear_bit(R5_UPTODATE
, &dev
->flags
);
2951 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
2952 set_bit(R5_LOCKED
, &dev
->flags
);
2957 /* False alarm - nothing to do */
2959 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2960 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2961 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2962 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2965 /* keep the parity disk(s) locked while asynchronous operations
2968 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2969 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2973 int qd_idx
= sh
->qd_idx
;
2974 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2976 set_bit(R5_LOCKED
, &dev
->flags
);
2977 clear_bit(R5_UPTODATE
, &dev
->flags
);
2981 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2982 __func__
, (unsigned long long)sh
->sector
,
2983 s
->locked
, s
->ops_request
);
2987 * Each stripe/dev can have one or more bion attached.
2988 * toread/towrite point to the first in a chain.
2989 * The bi_next chain must be in order.
2991 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
,
2992 int forwrite
, int previous
)
2995 struct r5conf
*conf
= sh
->raid_conf
;
2998 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2999 (unsigned long long)bi
->bi_iter
.bi_sector
,
3000 (unsigned long long)sh
->sector
);
3003 * If several bio share a stripe. The bio bi_phys_segments acts as a
3004 * reference count to avoid race. The reference count should already be
3005 * increased before this function is called (for example, in
3006 * raid5_make_request()), so other bio sharing this stripe will not free the
3007 * stripe. If a stripe is owned by one stripe, the stripe lock will
3010 spin_lock_irq(&sh
->stripe_lock
);
3011 /* Don't allow new IO added to stripes in batch list */
3015 bip
= &sh
->dev
[dd_idx
].towrite
;
3019 bip
= &sh
->dev
[dd_idx
].toread
;
3020 while (*bip
&& (*bip
)->bi_iter
.bi_sector
< bi
->bi_iter
.bi_sector
) {
3021 if (bio_end_sector(*bip
) > bi
->bi_iter
.bi_sector
)
3023 bip
= & (*bip
)->bi_next
;
3025 if (*bip
&& (*bip
)->bi_iter
.bi_sector
< bio_end_sector(bi
))
3028 if (!forwrite
|| previous
)
3029 clear_bit(STRIPE_BATCH_READY
, &sh
->state
);
3031 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
3035 raid5_inc_bi_active_stripes(bi
);
3038 /* check if page is covered */
3039 sector_t sector
= sh
->dev
[dd_idx
].sector
;
3040 for (bi
=sh
->dev
[dd_idx
].towrite
;
3041 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
3042 bi
&& bi
->bi_iter
.bi_sector
<= sector
;
3043 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
3044 if (bio_end_sector(bi
) >= sector
)
3045 sector
= bio_end_sector(bi
);
3047 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
3048 if (!test_and_set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
))
3049 sh
->overwrite_disks
++;
3052 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
3053 (unsigned long long)(*bip
)->bi_iter
.bi_sector
,
3054 (unsigned long long)sh
->sector
, dd_idx
);
3056 if (conf
->mddev
->bitmap
&& firstwrite
) {
3057 /* Cannot hold spinlock over bitmap_startwrite,
3058 * but must ensure this isn't added to a batch until
3059 * we have added to the bitmap and set bm_seq.
3060 * So set STRIPE_BITMAP_PENDING to prevent
3062 * If multiple add_stripe_bio() calls race here they
3063 * much all set STRIPE_BITMAP_PENDING. So only the first one
3064 * to complete "bitmap_startwrite" gets to set
3065 * STRIPE_BIT_DELAY. This is important as once a stripe
3066 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3069 set_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3070 spin_unlock_irq(&sh
->stripe_lock
);
3071 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
3073 spin_lock_irq(&sh
->stripe_lock
);
3074 clear_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3075 if (!sh
->batch_head
) {
3076 sh
->bm_seq
= conf
->seq_flush
+1;
3077 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
3080 spin_unlock_irq(&sh
->stripe_lock
);
3082 if (stripe_can_batch(sh
))
3083 stripe_add_to_batch_list(conf
, sh
);
3087 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
3088 spin_unlock_irq(&sh
->stripe_lock
);
3092 static void end_reshape(struct r5conf
*conf
);
3094 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
3095 struct stripe_head
*sh
)
3097 int sectors_per_chunk
=
3098 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
3100 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
3101 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
3103 raid5_compute_sector(conf
,
3104 stripe
* (disks
- conf
->max_degraded
)
3105 *sectors_per_chunk
+ chunk_offset
,
3111 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
3112 struct stripe_head_state
*s
, int disks
,
3113 struct bio_list
*return_bi
)
3116 BUG_ON(sh
->batch_head
);
3117 for (i
= disks
; i
--; ) {
3121 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
3122 struct md_rdev
*rdev
;
3124 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3125 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
3126 !test_bit(Faulty
, &rdev
->flags
))
3127 atomic_inc(&rdev
->nr_pending
);
3132 if (!rdev_set_badblocks(
3136 md_error(conf
->mddev
, rdev
);
3137 rdev_dec_pending(rdev
, conf
->mddev
);
3140 spin_lock_irq(&sh
->stripe_lock
);
3141 /* fail all writes first */
3142 bi
= sh
->dev
[i
].towrite
;
3143 sh
->dev
[i
].towrite
= NULL
;
3144 sh
->overwrite_disks
= 0;
3145 spin_unlock_irq(&sh
->stripe_lock
);
3149 r5l_stripe_write_finished(sh
);
3151 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3152 wake_up(&conf
->wait_for_overlap
);
3154 while (bi
&& bi
->bi_iter
.bi_sector
<
3155 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3156 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3158 bi
->bi_error
= -EIO
;
3159 if (!raid5_dec_bi_active_stripes(bi
)) {
3160 md_write_end(conf
->mddev
);
3161 bio_list_add(return_bi
, bi
);
3166 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3167 STRIPE_SECTORS
, 0, 0);
3169 /* and fail all 'written' */
3170 bi
= sh
->dev
[i
].written
;
3171 sh
->dev
[i
].written
= NULL
;
3172 if (test_and_clear_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
)) {
3173 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
3174 sh
->dev
[i
].page
= sh
->dev
[i
].orig_page
;
3177 if (bi
) bitmap_end
= 1;
3178 while (bi
&& bi
->bi_iter
.bi_sector
<
3179 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3180 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3182 bi
->bi_error
= -EIO
;
3183 if (!raid5_dec_bi_active_stripes(bi
)) {
3184 md_write_end(conf
->mddev
);
3185 bio_list_add(return_bi
, bi
);
3190 /* fail any reads if this device is non-operational and
3191 * the data has not reached the cache yet.
3193 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
3194 s
->failed
> conf
->max_degraded
&&
3195 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
3196 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
3197 spin_lock_irq(&sh
->stripe_lock
);
3198 bi
= sh
->dev
[i
].toread
;
3199 sh
->dev
[i
].toread
= NULL
;
3200 spin_unlock_irq(&sh
->stripe_lock
);
3201 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3202 wake_up(&conf
->wait_for_overlap
);
3205 while (bi
&& bi
->bi_iter
.bi_sector
<
3206 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3207 struct bio
*nextbi
=
3208 r5_next_bio(bi
, sh
->dev
[i
].sector
);
3210 bi
->bi_error
= -EIO
;
3211 if (!raid5_dec_bi_active_stripes(bi
))
3212 bio_list_add(return_bi
, bi
);
3217 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3218 STRIPE_SECTORS
, 0, 0);
3219 /* If we were in the middle of a write the parity block might
3220 * still be locked - so just clear all R5_LOCKED flags
3222 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3227 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3228 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3229 md_wakeup_thread(conf
->mddev
->thread
);
3233 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
3234 struct stripe_head_state
*s
)
3239 BUG_ON(sh
->batch_head
);
3240 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3241 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
3242 wake_up(&conf
->wait_for_overlap
);
3245 /* There is nothing more to do for sync/check/repair.
3246 * Don't even need to abort as that is handled elsewhere
3247 * if needed, and not always wanted e.g. if there is a known
3249 * For recover/replace we need to record a bad block on all
3250 * non-sync devices, or abort the recovery
3252 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
3253 /* During recovery devices cannot be removed, so
3254 * locking and refcounting of rdevs is not needed
3257 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3258 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3260 && !test_bit(Faulty
, &rdev
->flags
)
3261 && !test_bit(In_sync
, &rdev
->flags
)
3262 && !rdev_set_badblocks(rdev
, sh
->sector
,
3265 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3267 && !test_bit(Faulty
, &rdev
->flags
)
3268 && !test_bit(In_sync
, &rdev
->flags
)
3269 && !rdev_set_badblocks(rdev
, sh
->sector
,
3275 conf
->recovery_disabled
=
3276 conf
->mddev
->recovery_disabled
;
3278 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
3281 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
3283 struct md_rdev
*rdev
;
3287 rdev
= rcu_dereference(sh
->raid_conf
->disks
[disk_idx
].replacement
);
3289 && !test_bit(Faulty
, &rdev
->flags
)
3290 && !test_bit(In_sync
, &rdev
->flags
)
3291 && (rdev
->recovery_offset
<= sh
->sector
3292 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
3298 /* fetch_block - checks the given member device to see if its data needs
3299 * to be read or computed to satisfy a request.
3301 * Returns 1 when no more member devices need to be checked, otherwise returns
3302 * 0 to tell the loop in handle_stripe_fill to continue
3305 static int need_this_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3306 int disk_idx
, int disks
)
3308 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3309 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
3310 &sh
->dev
[s
->failed_num
[1]] };
3314 if (test_bit(R5_LOCKED
, &dev
->flags
) ||
3315 test_bit(R5_UPTODATE
, &dev
->flags
))
3316 /* No point reading this as we already have it or have
3317 * decided to get it.
3322 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)))
3323 /* We need this block to directly satisfy a request */
3326 if (s
->syncing
|| s
->expanding
||
3327 (s
->replacing
&& want_replace(sh
, disk_idx
)))
3328 /* When syncing, or expanding we read everything.
3329 * When replacing, we need the replaced block.
3333 if ((s
->failed
>= 1 && fdev
[0]->toread
) ||
3334 (s
->failed
>= 2 && fdev
[1]->toread
))
3335 /* If we want to read from a failed device, then
3336 * we need to actually read every other device.
3340 /* Sometimes neither read-modify-write nor reconstruct-write
3341 * cycles can work. In those cases we read every block we
3342 * can. Then the parity-update is certain to have enough to
3344 * This can only be a problem when we need to write something,
3345 * and some device has failed. If either of those tests
3346 * fail we need look no further.
3348 if (!s
->failed
|| !s
->to_write
)
3351 if (test_bit(R5_Insync
, &dev
->flags
) &&
3352 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3353 /* Pre-reads at not permitted until after short delay
3354 * to gather multiple requests. However if this
3355 * device is no Insync, the block could only be be computed
3356 * and there is no need to delay that.
3360 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3361 if (fdev
[i
]->towrite
&&
3362 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3363 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3364 /* If we have a partial write to a failed
3365 * device, then we will need to reconstruct
3366 * the content of that device, so all other
3367 * devices must be read.
3372 /* If we are forced to do a reconstruct-write, either because
3373 * the current RAID6 implementation only supports that, or
3374 * or because parity cannot be trusted and we are currently
3375 * recovering it, there is extra need to be careful.
3376 * If one of the devices that we would need to read, because
3377 * it is not being overwritten (and maybe not written at all)
3378 * is missing/faulty, then we need to read everything we can.
3380 if (sh
->raid_conf
->level
!= 6 &&
3381 sh
->sector
< sh
->raid_conf
->mddev
->recovery_cp
)
3382 /* reconstruct-write isn't being forced */
3384 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3385 if (s
->failed_num
[i
] != sh
->pd_idx
&&
3386 s
->failed_num
[i
] != sh
->qd_idx
&&
3387 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3388 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3395 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3396 int disk_idx
, int disks
)
3398 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3400 /* is the data in this block needed, and can we get it? */
3401 if (need_this_block(sh
, s
, disk_idx
, disks
)) {
3402 /* we would like to get this block, possibly by computing it,
3403 * otherwise read it if the backing disk is insync
3405 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
3406 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
3407 BUG_ON(sh
->batch_head
);
3408 if ((s
->uptodate
== disks
- 1) &&
3409 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
3410 disk_idx
== s
->failed_num
[1]))) {
3411 /* have disk failed, and we're requested to fetch it;
3414 pr_debug("Computing stripe %llu block %d\n",
3415 (unsigned long long)sh
->sector
, disk_idx
);
3416 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3417 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3418 set_bit(R5_Wantcompute
, &dev
->flags
);
3419 sh
->ops
.target
= disk_idx
;
3420 sh
->ops
.target2
= -1; /* no 2nd target */
3422 /* Careful: from this point on 'uptodate' is in the eye
3423 * of raid_run_ops which services 'compute' operations
3424 * before writes. R5_Wantcompute flags a block that will
3425 * be R5_UPTODATE by the time it is needed for a
3426 * subsequent operation.
3430 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
3431 /* Computing 2-failure is *very* expensive; only
3432 * do it if failed >= 2
3435 for (other
= disks
; other
--; ) {
3436 if (other
== disk_idx
)
3438 if (!test_bit(R5_UPTODATE
,
3439 &sh
->dev
[other
].flags
))
3443 pr_debug("Computing stripe %llu blocks %d,%d\n",
3444 (unsigned long long)sh
->sector
,
3446 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3447 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3448 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
3449 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
3450 sh
->ops
.target
= disk_idx
;
3451 sh
->ops
.target2
= other
;
3455 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
3456 set_bit(R5_LOCKED
, &dev
->flags
);
3457 set_bit(R5_Wantread
, &dev
->flags
);
3459 pr_debug("Reading block %d (sync=%d)\n",
3460 disk_idx
, s
->syncing
);
3468 * handle_stripe_fill - read or compute data to satisfy pending requests.
3470 static void handle_stripe_fill(struct stripe_head
*sh
,
3471 struct stripe_head_state
*s
,
3476 /* look for blocks to read/compute, skip this if a compute
3477 * is already in flight, or if the stripe contents are in the
3478 * midst of changing due to a write
3480 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
3481 !sh
->reconstruct_state
)
3482 for (i
= disks
; i
--; )
3483 if (fetch_block(sh
, s
, i
, disks
))
3485 set_bit(STRIPE_HANDLE
, &sh
->state
);
3488 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
3489 unsigned long handle_flags
);
3490 /* handle_stripe_clean_event
3491 * any written block on an uptodate or failed drive can be returned.
3492 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3493 * never LOCKED, so we don't need to test 'failed' directly.
3495 static void handle_stripe_clean_event(struct r5conf
*conf
,
3496 struct stripe_head
*sh
, int disks
, struct bio_list
*return_bi
)
3500 int discard_pending
= 0;
3501 struct stripe_head
*head_sh
= sh
;
3502 bool do_endio
= false;
3504 for (i
= disks
; i
--; )
3505 if (sh
->dev
[i
].written
) {
3507 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
3508 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
3509 test_bit(R5_Discard
, &dev
->flags
) ||
3510 test_bit(R5_SkipCopy
, &dev
->flags
))) {
3511 /* We can return any write requests */
3512 struct bio
*wbi
, *wbi2
;
3513 pr_debug("Return write for disc %d\n", i
);
3514 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
3515 clear_bit(R5_UPTODATE
, &dev
->flags
);
3516 if (test_and_clear_bit(R5_SkipCopy
, &dev
->flags
)) {
3517 WARN_ON(test_bit(R5_UPTODATE
, &dev
->flags
));
3522 dev
->page
= dev
->orig_page
;
3524 dev
->written
= NULL
;
3525 while (wbi
&& wbi
->bi_iter
.bi_sector
<
3526 dev
->sector
+ STRIPE_SECTORS
) {
3527 wbi2
= r5_next_bio(wbi
, dev
->sector
);
3528 if (!raid5_dec_bi_active_stripes(wbi
)) {
3529 md_write_end(conf
->mddev
);
3530 bio_list_add(return_bi
, wbi
);
3534 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3536 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
3538 if (head_sh
->batch_head
) {
3539 sh
= list_first_entry(&sh
->batch_list
,
3542 if (sh
!= head_sh
) {
3549 } else if (test_bit(R5_Discard
, &dev
->flags
))
3550 discard_pending
= 1;
3553 r5l_stripe_write_finished(sh
);
3555 if (!discard_pending
&&
3556 test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3558 clear_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
3559 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3560 if (sh
->qd_idx
>= 0) {
3561 clear_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
3562 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
);
3564 /* now that discard is done we can proceed with any sync */
3565 clear_bit(STRIPE_DISCARD
, &sh
->state
);
3567 * SCSI discard will change some bio fields and the stripe has
3568 * no updated data, so remove it from hash list and the stripe
3569 * will be reinitialized
3572 hash
= sh
->hash_lock_index
;
3573 spin_lock_irq(conf
->hash_locks
+ hash
);
3575 spin_unlock_irq(conf
->hash_locks
+ hash
);
3576 if (head_sh
->batch_head
) {
3577 sh
= list_first_entry(&sh
->batch_list
,
3578 struct stripe_head
, batch_list
);
3584 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
))
3585 set_bit(STRIPE_HANDLE
, &sh
->state
);
3589 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3590 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3591 md_wakeup_thread(conf
->mddev
->thread
);
3593 if (head_sh
->batch_head
&& do_endio
)
3594 break_stripe_batch_list(head_sh
, STRIPE_EXPAND_SYNC_FLAGS
);
3597 static int handle_stripe_dirtying(struct r5conf
*conf
,
3598 struct stripe_head
*sh
,
3599 struct stripe_head_state
*s
,
3602 int rmw
= 0, rcw
= 0, i
;
3603 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
3605 /* Check whether resync is now happening or should start.
3606 * If yes, then the array is dirty (after unclean shutdown or
3607 * initial creation), so parity in some stripes might be inconsistent.
3608 * In this case, we need to always do reconstruct-write, to ensure
3609 * that in case of drive failure or read-error correction, we
3610 * generate correct data from the parity.
3612 if (conf
->rmw_level
== PARITY_DISABLE_RMW
||
3613 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
&&
3615 /* Calculate the real rcw later - for now make it
3616 * look like rcw is cheaper
3619 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3620 conf
->rmw_level
, (unsigned long long)recovery_cp
,
3621 (unsigned long long)sh
->sector
);
3622 } else for (i
= disks
; i
--; ) {
3623 /* would I have to read this buffer for read_modify_write */
3624 struct r5dev
*dev
= &sh
->dev
[i
];
3625 if ((dev
->towrite
|| i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3626 test_bit(R5_InJournal
, &dev
->flags
)) &&
3627 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3628 !((test_bit(R5_UPTODATE
, &dev
->flags
) &&
3629 (!test_bit(R5_InJournal
, &dev
->flags
) ||
3630 dev
->page
!= dev
->orig_page
)) ||
3631 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3632 if (test_bit(R5_Insync
, &dev
->flags
))
3635 rmw
+= 2*disks
; /* cannot read it */
3637 /* Would I have to read this buffer for reconstruct_write */
3638 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3639 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3640 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3641 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3642 test_bit(R5_InJournal
, &dev
->flags
) ||
3643 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3644 if (test_bit(R5_Insync
, &dev
->flags
))
3651 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
3652 (unsigned long long)sh
->sector
, rmw
, rcw
);
3653 set_bit(STRIPE_HANDLE
, &sh
->state
);
3654 if ((rmw
< rcw
|| (rmw
== rcw
&& conf
->rmw_level
== PARITY_PREFER_RMW
)) && rmw
> 0) {
3655 /* prefer read-modify-write, but need to get some data */
3656 if (conf
->mddev
->queue
)
3657 blk_add_trace_msg(conf
->mddev
->queue
,
3658 "raid5 rmw %llu %d",
3659 (unsigned long long)sh
->sector
, rmw
);
3660 for (i
= disks
; i
--; ) {
3661 struct r5dev
*dev
= &sh
->dev
[i
];
3662 if (test_bit(R5_InJournal
, &dev
->flags
) &&
3663 dev
->page
== dev
->orig_page
&&
3664 !test_bit(R5_LOCKED
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3665 /* alloc page for prexor */
3666 struct page
*p
= alloc_page(GFP_NOIO
);
3674 * alloc_page() failed, try use
3675 * disk_info->extra_page
3677 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE
,
3678 &conf
->cache_state
)) {
3679 r5c_use_extra_page(sh
);
3683 /* extra_page in use, add to delayed_list */
3684 set_bit(STRIPE_DELAYED
, &sh
->state
);
3685 s
->waiting_extra_page
= 1;
3690 for (i
= disks
; i
--; ) {
3691 struct r5dev
*dev
= &sh
->dev
[i
];
3692 if ((dev
->towrite
||
3693 i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3694 test_bit(R5_InJournal
, &dev
->flags
)) &&
3695 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3696 !((test_bit(R5_UPTODATE
, &dev
->flags
) &&
3697 (!test_bit(R5_InJournal
, &dev
->flags
) ||
3698 dev
->page
!= dev
->orig_page
)) ||
3699 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
3700 test_bit(R5_Insync
, &dev
->flags
)) {
3701 if (test_bit(STRIPE_PREREAD_ACTIVE
,
3703 pr_debug("Read_old block %d for r-m-w\n",
3705 set_bit(R5_LOCKED
, &dev
->flags
);
3706 set_bit(R5_Wantread
, &dev
->flags
);
3709 set_bit(STRIPE_DELAYED
, &sh
->state
);
3710 set_bit(STRIPE_HANDLE
, &sh
->state
);
3715 if ((rcw
< rmw
|| (rcw
== rmw
&& conf
->rmw_level
!= PARITY_PREFER_RMW
)) && rcw
> 0) {
3716 /* want reconstruct write, but need to get some data */
3719 for (i
= disks
; i
--; ) {
3720 struct r5dev
*dev
= &sh
->dev
[i
];
3721 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3722 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3723 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3724 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3725 test_bit(R5_InJournal
, &dev
->flags
) ||
3726 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3728 if (test_bit(R5_Insync
, &dev
->flags
) &&
3729 test_bit(STRIPE_PREREAD_ACTIVE
,
3731 pr_debug("Read_old block "
3732 "%d for Reconstruct\n", i
);
3733 set_bit(R5_LOCKED
, &dev
->flags
);
3734 set_bit(R5_Wantread
, &dev
->flags
);
3738 set_bit(STRIPE_DELAYED
, &sh
->state
);
3739 set_bit(STRIPE_HANDLE
, &sh
->state
);
3743 if (rcw
&& conf
->mddev
->queue
)
3744 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
3745 (unsigned long long)sh
->sector
,
3746 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
3749 if (rcw
> disks
&& rmw
> disks
&&
3750 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3751 set_bit(STRIPE_DELAYED
, &sh
->state
);
3753 /* now if nothing is locked, and if we have enough data,
3754 * we can start a write request
3756 /* since handle_stripe can be called at any time we need to handle the
3757 * case where a compute block operation has been submitted and then a
3758 * subsequent call wants to start a write request. raid_run_ops only
3759 * handles the case where compute block and reconstruct are requested
3760 * simultaneously. If this is not the case then new writes need to be
3761 * held off until the compute completes.
3763 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
3764 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
3765 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
3766 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
3770 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
3771 struct stripe_head_state
*s
, int disks
)
3773 struct r5dev
*dev
= NULL
;
3775 BUG_ON(sh
->batch_head
);
3776 set_bit(STRIPE_HANDLE
, &sh
->state
);
3778 switch (sh
->check_state
) {
3779 case check_state_idle
:
3780 /* start a new check operation if there are no failures */
3781 if (s
->failed
== 0) {
3782 BUG_ON(s
->uptodate
!= disks
);
3783 sh
->check_state
= check_state_run
;
3784 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3785 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3789 dev
= &sh
->dev
[s
->failed_num
[0]];
3791 case check_state_compute_result
:
3792 sh
->check_state
= check_state_idle
;
3794 dev
= &sh
->dev
[sh
->pd_idx
];
3796 /* check that a write has not made the stripe insync */
3797 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3800 /* either failed parity check, or recovery is happening */
3801 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
3802 BUG_ON(s
->uptodate
!= disks
);
3804 set_bit(R5_LOCKED
, &dev
->flags
);
3806 set_bit(R5_Wantwrite
, &dev
->flags
);
3808 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3809 set_bit(STRIPE_INSYNC
, &sh
->state
);
3811 case check_state_run
:
3812 break; /* we will be called again upon completion */
3813 case check_state_check_result
:
3814 sh
->check_state
= check_state_idle
;
3816 /* if a failure occurred during the check operation, leave
3817 * STRIPE_INSYNC not set and let the stripe be handled again
3822 /* handle a successful check operation, if parity is correct
3823 * we are done. Otherwise update the mismatch count and repair
3824 * parity if !MD_RECOVERY_CHECK
3826 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
3827 /* parity is correct (on disc,
3828 * not in buffer any more)
3830 set_bit(STRIPE_INSYNC
, &sh
->state
);
3832 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3833 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3834 /* don't try to repair!! */
3835 set_bit(STRIPE_INSYNC
, &sh
->state
);
3837 sh
->check_state
= check_state_compute_run
;
3838 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3839 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3840 set_bit(R5_Wantcompute
,
3841 &sh
->dev
[sh
->pd_idx
].flags
);
3842 sh
->ops
.target
= sh
->pd_idx
;
3843 sh
->ops
.target2
= -1;
3848 case check_state_compute_run
:
3851 pr_err("%s: unknown check_state: %d sector: %llu\n",
3852 __func__
, sh
->check_state
,
3853 (unsigned long long) sh
->sector
);
3858 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
3859 struct stripe_head_state
*s
,
3862 int pd_idx
= sh
->pd_idx
;
3863 int qd_idx
= sh
->qd_idx
;
3866 BUG_ON(sh
->batch_head
);
3867 set_bit(STRIPE_HANDLE
, &sh
->state
);
3869 BUG_ON(s
->failed
> 2);
3871 /* Want to check and possibly repair P and Q.
3872 * However there could be one 'failed' device, in which
3873 * case we can only check one of them, possibly using the
3874 * other to generate missing data
3877 switch (sh
->check_state
) {
3878 case check_state_idle
:
3879 /* start a new check operation if there are < 2 failures */
3880 if (s
->failed
== s
->q_failed
) {
3881 /* The only possible failed device holds Q, so it
3882 * makes sense to check P (If anything else were failed,
3883 * we would have used P to recreate it).
3885 sh
->check_state
= check_state_run
;
3887 if (!s
->q_failed
&& s
->failed
< 2) {
3888 /* Q is not failed, and we didn't use it to generate
3889 * anything, so it makes sense to check it
3891 if (sh
->check_state
== check_state_run
)
3892 sh
->check_state
= check_state_run_pq
;
3894 sh
->check_state
= check_state_run_q
;
3897 /* discard potentially stale zero_sum_result */
3898 sh
->ops
.zero_sum_result
= 0;
3900 if (sh
->check_state
== check_state_run
) {
3901 /* async_xor_zero_sum destroys the contents of P */
3902 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3905 if (sh
->check_state
>= check_state_run
&&
3906 sh
->check_state
<= check_state_run_pq
) {
3907 /* async_syndrome_zero_sum preserves P and Q, so
3908 * no need to mark them !uptodate here
3910 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3914 /* we have 2-disk failure */
3915 BUG_ON(s
->failed
!= 2);
3917 case check_state_compute_result
:
3918 sh
->check_state
= check_state_idle
;
3920 /* check that a write has not made the stripe insync */
3921 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3924 /* now write out any block on a failed drive,
3925 * or P or Q if they were recomputed
3927 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
3928 if (s
->failed
== 2) {
3929 dev
= &sh
->dev
[s
->failed_num
[1]];
3931 set_bit(R5_LOCKED
, &dev
->flags
);
3932 set_bit(R5_Wantwrite
, &dev
->flags
);
3934 if (s
->failed
>= 1) {
3935 dev
= &sh
->dev
[s
->failed_num
[0]];
3937 set_bit(R5_LOCKED
, &dev
->flags
);
3938 set_bit(R5_Wantwrite
, &dev
->flags
);
3940 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3941 dev
= &sh
->dev
[pd_idx
];
3943 set_bit(R5_LOCKED
, &dev
->flags
);
3944 set_bit(R5_Wantwrite
, &dev
->flags
);
3946 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3947 dev
= &sh
->dev
[qd_idx
];
3949 set_bit(R5_LOCKED
, &dev
->flags
);
3950 set_bit(R5_Wantwrite
, &dev
->flags
);
3952 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3954 set_bit(STRIPE_INSYNC
, &sh
->state
);
3956 case check_state_run
:
3957 case check_state_run_q
:
3958 case check_state_run_pq
:
3959 break; /* we will be called again upon completion */
3960 case check_state_check_result
:
3961 sh
->check_state
= check_state_idle
;
3963 /* handle a successful check operation, if parity is correct
3964 * we are done. Otherwise update the mismatch count and repair
3965 * parity if !MD_RECOVERY_CHECK
3967 if (sh
->ops
.zero_sum_result
== 0) {
3968 /* both parities are correct */
3970 set_bit(STRIPE_INSYNC
, &sh
->state
);
3972 /* in contrast to the raid5 case we can validate
3973 * parity, but still have a failure to write
3976 sh
->check_state
= check_state_compute_result
;
3977 /* Returning at this point means that we may go
3978 * off and bring p and/or q uptodate again so
3979 * we make sure to check zero_sum_result again
3980 * to verify if p or q need writeback
3984 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3985 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3986 /* don't try to repair!! */
3987 set_bit(STRIPE_INSYNC
, &sh
->state
);
3989 int *target
= &sh
->ops
.target
;
3991 sh
->ops
.target
= -1;
3992 sh
->ops
.target2
= -1;
3993 sh
->check_state
= check_state_compute_run
;
3994 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3995 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3996 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3997 set_bit(R5_Wantcompute
,
3998 &sh
->dev
[pd_idx
].flags
);
4000 target
= &sh
->ops
.target2
;
4003 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
4004 set_bit(R5_Wantcompute
,
4005 &sh
->dev
[qd_idx
].flags
);
4012 case check_state_compute_run
:
4015 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4016 __func__
, sh
->check_state
,
4017 (unsigned long long) sh
->sector
);
4022 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
4026 /* We have read all the blocks in this stripe and now we need to
4027 * copy some of them into a target stripe for expand.
4029 struct dma_async_tx_descriptor
*tx
= NULL
;
4030 BUG_ON(sh
->batch_head
);
4031 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4032 for (i
= 0; i
< sh
->disks
; i
++)
4033 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
4035 struct stripe_head
*sh2
;
4036 struct async_submit_ctl submit
;
4038 sector_t bn
= raid5_compute_blocknr(sh
, i
, 1);
4039 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
4041 sh2
= raid5_get_active_stripe(conf
, s
, 0, 1, 1);
4043 /* so far only the early blocks of this stripe
4044 * have been requested. When later blocks
4045 * get requested, we will try again
4048 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
4049 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
4050 /* must have already done this block */
4051 raid5_release_stripe(sh2
);
4055 /* place all the copies on one channel */
4056 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
4057 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
4058 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
4061 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
4062 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
4063 for (j
= 0; j
< conf
->raid_disks
; j
++)
4064 if (j
!= sh2
->pd_idx
&&
4066 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
4068 if (j
== conf
->raid_disks
) {
4069 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
4070 set_bit(STRIPE_HANDLE
, &sh2
->state
);
4072 raid5_release_stripe(sh2
);
4075 /* done submitting copies, wait for them to complete */
4076 async_tx_quiesce(&tx
);
4080 * handle_stripe - do things to a stripe.
4082 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4083 * state of various bits to see what needs to be done.
4085 * return some read requests which now have data
4086 * return some write requests which are safely on storage
4087 * schedule a read on some buffers
4088 * schedule a write of some buffers
4089 * return confirmation of parity correctness
4093 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
4095 struct r5conf
*conf
= sh
->raid_conf
;
4096 int disks
= sh
->disks
;
4099 int do_recovery
= 0;
4101 memset(s
, 0, sizeof(*s
));
4103 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
) && !sh
->batch_head
;
4104 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
) && !sh
->batch_head
;
4105 s
->failed_num
[0] = -1;
4106 s
->failed_num
[1] = -1;
4107 s
->log_failed
= r5l_log_disk_error(conf
);
4109 /* Now to look around and see what can be done */
4111 for (i
=disks
; i
--; ) {
4112 struct md_rdev
*rdev
;
4119 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4121 dev
->toread
, dev
->towrite
, dev
->written
);
4122 /* maybe we can reply to a read
4124 * new wantfill requests are only permitted while
4125 * ops_complete_biofill is guaranteed to be inactive
4127 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
4128 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
4129 set_bit(R5_Wantfill
, &dev
->flags
);
4131 /* now count some things */
4132 if (test_bit(R5_LOCKED
, &dev
->flags
))
4134 if (test_bit(R5_UPTODATE
, &dev
->flags
))
4136 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
4138 BUG_ON(s
->compute
> 2);
4141 if (test_bit(R5_Wantfill
, &dev
->flags
))
4143 else if (dev
->toread
)
4147 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
4152 /* Prefer to use the replacement for reads, but only
4153 * if it is recovered enough and has no bad blocks.
4155 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
4156 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
4157 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
4158 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4159 &first_bad
, &bad_sectors
))
4160 set_bit(R5_ReadRepl
, &dev
->flags
);
4162 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4163 set_bit(R5_NeedReplace
, &dev
->flags
);
4165 clear_bit(R5_NeedReplace
, &dev
->flags
);
4166 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
4167 clear_bit(R5_ReadRepl
, &dev
->flags
);
4169 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
4172 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4173 &first_bad
, &bad_sectors
);
4174 if (s
->blocked_rdev
== NULL
4175 && (test_bit(Blocked
, &rdev
->flags
)
4178 set_bit(BlockedBadBlocks
,
4180 s
->blocked_rdev
= rdev
;
4181 atomic_inc(&rdev
->nr_pending
);
4184 clear_bit(R5_Insync
, &dev
->flags
);
4188 /* also not in-sync */
4189 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
4190 test_bit(R5_UPTODATE
, &dev
->flags
)) {
4191 /* treat as in-sync, but with a read error
4192 * which we can now try to correct
4194 set_bit(R5_Insync
, &dev
->flags
);
4195 set_bit(R5_ReadError
, &dev
->flags
);
4197 } else if (test_bit(In_sync
, &rdev
->flags
))
4198 set_bit(R5_Insync
, &dev
->flags
);
4199 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
4200 /* in sync if before recovery_offset */
4201 set_bit(R5_Insync
, &dev
->flags
);
4202 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
4203 test_bit(R5_Expanded
, &dev
->flags
))
4204 /* If we've reshaped into here, we assume it is Insync.
4205 * We will shortly update recovery_offset to make
4208 set_bit(R5_Insync
, &dev
->flags
);
4210 if (test_bit(R5_WriteError
, &dev
->flags
)) {
4211 /* This flag does not apply to '.replacement'
4212 * only to .rdev, so make sure to check that*/
4213 struct md_rdev
*rdev2
= rcu_dereference(
4214 conf
->disks
[i
].rdev
);
4216 clear_bit(R5_Insync
, &dev
->flags
);
4217 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4218 s
->handle_bad_blocks
= 1;
4219 atomic_inc(&rdev2
->nr_pending
);
4221 clear_bit(R5_WriteError
, &dev
->flags
);
4223 if (test_bit(R5_MadeGood
, &dev
->flags
)) {
4224 /* This flag does not apply to '.replacement'
4225 * only to .rdev, so make sure to check that*/
4226 struct md_rdev
*rdev2
= rcu_dereference(
4227 conf
->disks
[i
].rdev
);
4228 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4229 s
->handle_bad_blocks
= 1;
4230 atomic_inc(&rdev2
->nr_pending
);
4232 clear_bit(R5_MadeGood
, &dev
->flags
);
4234 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4235 struct md_rdev
*rdev2
= rcu_dereference(
4236 conf
->disks
[i
].replacement
);
4237 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4238 s
->handle_bad_blocks
= 1;
4239 atomic_inc(&rdev2
->nr_pending
);
4241 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
4243 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4244 /* The ReadError flag will just be confusing now */
4245 clear_bit(R5_ReadError
, &dev
->flags
);
4246 clear_bit(R5_ReWrite
, &dev
->flags
);
4248 if (test_bit(R5_ReadError
, &dev
->flags
))
4249 clear_bit(R5_Insync
, &dev
->flags
);
4250 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4252 s
->failed_num
[s
->failed
] = i
;
4254 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4258 if (test_bit(R5_InJournal
, &dev
->flags
))
4260 if (test_bit(R5_InJournal
, &dev
->flags
) && dev
->written
)
4263 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
4264 /* If there is a failed device being replaced,
4265 * we must be recovering.
4266 * else if we are after recovery_cp, we must be syncing
4267 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4268 * else we can only be replacing
4269 * sync and recovery both need to read all devices, and so
4270 * use the same flag.
4273 sh
->sector
>= conf
->mddev
->recovery_cp
||
4274 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
4282 static int clear_batch_ready(struct stripe_head
*sh
)
4284 /* Return '1' if this is a member of batch, or
4285 * '0' if it is a lone stripe or a head which can now be
4288 struct stripe_head
*tmp
;
4289 if (!test_and_clear_bit(STRIPE_BATCH_READY
, &sh
->state
))
4290 return (sh
->batch_head
&& sh
->batch_head
!= sh
);
4291 spin_lock(&sh
->stripe_lock
);
4292 if (!sh
->batch_head
) {
4293 spin_unlock(&sh
->stripe_lock
);
4298 * this stripe could be added to a batch list before we check
4299 * BATCH_READY, skips it
4301 if (sh
->batch_head
!= sh
) {
4302 spin_unlock(&sh
->stripe_lock
);
4305 spin_lock(&sh
->batch_lock
);
4306 list_for_each_entry(tmp
, &sh
->batch_list
, batch_list
)
4307 clear_bit(STRIPE_BATCH_READY
, &tmp
->state
);
4308 spin_unlock(&sh
->batch_lock
);
4309 spin_unlock(&sh
->stripe_lock
);
4312 * BATCH_READY is cleared, no new stripes can be added.
4313 * batch_list can be accessed without lock
4318 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
4319 unsigned long handle_flags
)
4321 struct stripe_head
*sh
, *next
;
4325 list_for_each_entry_safe(sh
, next
, &head_sh
->batch_list
, batch_list
) {
4327 list_del_init(&sh
->batch_list
);
4329 WARN_ONCE(sh
->state
& ((1 << STRIPE_ACTIVE
) |
4330 (1 << STRIPE_SYNCING
) |
4331 (1 << STRIPE_REPLACED
) |
4332 (1 << STRIPE_DELAYED
) |
4333 (1 << STRIPE_BIT_DELAY
) |
4334 (1 << STRIPE_FULL_WRITE
) |
4335 (1 << STRIPE_BIOFILL_RUN
) |
4336 (1 << STRIPE_COMPUTE_RUN
) |
4337 (1 << STRIPE_OPS_REQ_PENDING
) |
4338 (1 << STRIPE_DISCARD
) |
4339 (1 << STRIPE_BATCH_READY
) |
4340 (1 << STRIPE_BATCH_ERR
) |
4341 (1 << STRIPE_BITMAP_PENDING
)),
4342 "stripe state: %lx\n", sh
->state
);
4343 WARN_ONCE(head_sh
->state
& ((1 << STRIPE_DISCARD
) |
4344 (1 << STRIPE_REPLACED
)),
4345 "head stripe state: %lx\n", head_sh
->state
);
4347 set_mask_bits(&sh
->state
, ~(STRIPE_EXPAND_SYNC_FLAGS
|
4348 (1 << STRIPE_PREREAD_ACTIVE
) |
4349 (1 << STRIPE_DEGRADED
)),
4350 head_sh
->state
& (1 << STRIPE_INSYNC
));
4352 sh
->check_state
= head_sh
->check_state
;
4353 sh
->reconstruct_state
= head_sh
->reconstruct_state
;
4354 for (i
= 0; i
< sh
->disks
; i
++) {
4355 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
4357 sh
->dev
[i
].flags
= head_sh
->dev
[i
].flags
&
4358 (~((1 << R5_WriteError
) | (1 << R5_Overlap
)));
4360 spin_lock_irq(&sh
->stripe_lock
);
4361 sh
->batch_head
= NULL
;
4362 spin_unlock_irq(&sh
->stripe_lock
);
4363 if (handle_flags
== 0 ||
4364 sh
->state
& handle_flags
)
4365 set_bit(STRIPE_HANDLE
, &sh
->state
);
4366 raid5_release_stripe(sh
);
4368 spin_lock_irq(&head_sh
->stripe_lock
);
4369 head_sh
->batch_head
= NULL
;
4370 spin_unlock_irq(&head_sh
->stripe_lock
);
4371 for (i
= 0; i
< head_sh
->disks
; i
++)
4372 if (test_and_clear_bit(R5_Overlap
, &head_sh
->dev
[i
].flags
))
4374 if (head_sh
->state
& handle_flags
)
4375 set_bit(STRIPE_HANDLE
, &head_sh
->state
);
4378 wake_up(&head_sh
->raid_conf
->wait_for_overlap
);
4381 static void handle_stripe(struct stripe_head
*sh
)
4383 struct stripe_head_state s
;
4384 struct r5conf
*conf
= sh
->raid_conf
;
4387 int disks
= sh
->disks
;
4388 struct r5dev
*pdev
, *qdev
;
4390 clear_bit(STRIPE_HANDLE
, &sh
->state
);
4391 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
4392 /* already being handled, ensure it gets handled
4393 * again when current action finishes */
4394 set_bit(STRIPE_HANDLE
, &sh
->state
);
4398 if (clear_batch_ready(sh
) ) {
4399 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4403 if (test_and_clear_bit(STRIPE_BATCH_ERR
, &sh
->state
))
4404 break_stripe_batch_list(sh
, 0);
4406 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
) && !sh
->batch_head
) {
4407 spin_lock(&sh
->stripe_lock
);
4408 /* Cannot process 'sync' concurrently with 'discard' */
4409 if (!test_bit(STRIPE_DISCARD
, &sh
->state
) &&
4410 test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
4411 set_bit(STRIPE_SYNCING
, &sh
->state
);
4412 clear_bit(STRIPE_INSYNC
, &sh
->state
);
4413 clear_bit(STRIPE_REPLACED
, &sh
->state
);
4415 spin_unlock(&sh
->stripe_lock
);
4417 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4419 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4420 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4421 (unsigned long long)sh
->sector
, sh
->state
,
4422 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
4423 sh
->check_state
, sh
->reconstruct_state
);
4425 analyse_stripe(sh
, &s
);
4427 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
))
4430 if (s
.handle_bad_blocks
) {
4431 set_bit(STRIPE_HANDLE
, &sh
->state
);
4435 if (unlikely(s
.blocked_rdev
)) {
4436 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
4437 s
.replacing
|| s
.to_write
|| s
.written
) {
4438 set_bit(STRIPE_HANDLE
, &sh
->state
);
4441 /* There is nothing for the blocked_rdev to block */
4442 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
4443 s
.blocked_rdev
= NULL
;
4446 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
4447 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
4448 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
4451 pr_debug("locked=%d uptodate=%d to_read=%d"
4452 " to_write=%d failed=%d failed_num=%d,%d\n",
4453 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
4454 s
.failed_num
[0], s
.failed_num
[1]);
4455 /* check if the array has lost more than max_degraded devices and,
4456 * if so, some requests might need to be failed.
4458 if (s
.failed
> conf
->max_degraded
|| s
.log_failed
) {
4459 sh
->check_state
= 0;
4460 sh
->reconstruct_state
= 0;
4461 break_stripe_batch_list(sh
, 0);
4462 if (s
.to_read
+s
.to_write
+s
.written
)
4463 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
4464 if (s
.syncing
+ s
.replacing
)
4465 handle_failed_sync(conf
, sh
, &s
);
4468 /* Now we check to see if any write operations have recently
4472 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
4474 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
4475 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
4476 sh
->reconstruct_state
= reconstruct_state_idle
;
4478 /* All the 'written' buffers and the parity block are ready to
4479 * be written back to disk
4481 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
4482 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
4483 BUG_ON(sh
->qd_idx
>= 0 &&
4484 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
4485 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
4486 for (i
= disks
; i
--; ) {
4487 struct r5dev
*dev
= &sh
->dev
[i
];
4488 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
4489 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
4490 dev
->written
|| test_bit(R5_InJournal
,
4492 pr_debug("Writing block %d\n", i
);
4493 set_bit(R5_Wantwrite
, &dev
->flags
);
4498 if (!test_bit(R5_Insync
, &dev
->flags
) ||
4499 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
4501 set_bit(STRIPE_INSYNC
, &sh
->state
);
4504 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4505 s
.dec_preread_active
= 1;
4509 * might be able to return some write requests if the parity blocks
4510 * are safe, or on a failed drive
4512 pdev
= &sh
->dev
[sh
->pd_idx
];
4513 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
4514 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
4515 qdev
= &sh
->dev
[sh
->qd_idx
];
4516 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
4517 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
4521 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
4522 && !test_bit(R5_LOCKED
, &pdev
->flags
)
4523 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
4524 test_bit(R5_Discard
, &pdev
->flags
))))) &&
4525 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
4526 && !test_bit(R5_LOCKED
, &qdev
->flags
)
4527 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
4528 test_bit(R5_Discard
, &qdev
->flags
))))))
4529 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
4532 r5c_handle_cached_data_endio(conf
, sh
, disks
, &s
.return_bi
);
4533 r5l_stripe_write_finished(sh
);
4535 /* Now we might consider reading some blocks, either to check/generate
4536 * parity, or to satisfy requests
4537 * or to load a block that is being partially written.
4539 if (s
.to_read
|| s
.non_overwrite
4540 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
4541 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
4544 handle_stripe_fill(sh
, &s
, disks
);
4547 * When the stripe finishes full journal write cycle (write to journal
4548 * and raid disk), this is the clean up procedure so it is ready for
4551 r5c_finish_stripe_write_out(conf
, sh
, &s
);
4554 * Now to consider new write requests, cache write back and what else,
4555 * if anything should be read. We do not handle new writes when:
4556 * 1/ A 'write' operation (copy+xor) is already in flight.
4557 * 2/ A 'check' operation is in flight, as it may clobber the parity
4559 * 3/ A r5c cache log write is in flight.
4562 if (!sh
->reconstruct_state
&& !sh
->check_state
&& !sh
->log_io
) {
4563 if (!r5c_is_writeback(conf
->log
)) {
4565 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
4566 } else { /* write back cache */
4569 /* First, try handle writes in caching phase */
4571 ret
= r5c_try_caching_write(conf
, sh
, &s
,
4574 * If caching phase failed: ret == -EAGAIN
4576 * stripe under reclaim: !caching && injournal
4578 * fall back to handle_stripe_dirtying()
4580 if (ret
== -EAGAIN
||
4581 /* stripe under reclaim: !caching && injournal */
4582 (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
) &&
4584 ret
= handle_stripe_dirtying(conf
, sh
, &s
,
4592 /* maybe we need to check and possibly fix the parity for this stripe
4593 * Any reads will already have been scheduled, so we just see if enough
4594 * data is available. The parity check is held off while parity
4595 * dependent operations are in flight.
4597 if (sh
->check_state
||
4598 (s
.syncing
&& s
.locked
== 0 &&
4599 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4600 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
4601 if (conf
->level
== 6)
4602 handle_parity_checks6(conf
, sh
, &s
, disks
);
4604 handle_parity_checks5(conf
, sh
, &s
, disks
);
4607 if ((s
.replacing
|| s
.syncing
) && s
.locked
== 0
4608 && !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)
4609 && !test_bit(STRIPE_REPLACED
, &sh
->state
)) {
4610 /* Write out to replacement devices where possible */
4611 for (i
= 0; i
< conf
->raid_disks
; i
++)
4612 if (test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
4613 WARN_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
4614 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
4615 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4619 set_bit(STRIPE_INSYNC
, &sh
->state
);
4620 set_bit(STRIPE_REPLACED
, &sh
->state
);
4622 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
4623 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4624 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
4625 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4626 clear_bit(STRIPE_SYNCING
, &sh
->state
);
4627 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
4628 wake_up(&conf
->wait_for_overlap
);
4631 /* If the failed drives are just a ReadError, then we might need
4632 * to progress the repair/check process
4634 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
4635 for (i
= 0; i
< s
.failed
; i
++) {
4636 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
4637 if (test_bit(R5_ReadError
, &dev
->flags
)
4638 && !test_bit(R5_LOCKED
, &dev
->flags
)
4639 && test_bit(R5_UPTODATE
, &dev
->flags
)
4641 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
4642 set_bit(R5_Wantwrite
, &dev
->flags
);
4643 set_bit(R5_ReWrite
, &dev
->flags
);
4644 set_bit(R5_LOCKED
, &dev
->flags
);
4647 /* let's read it back */
4648 set_bit(R5_Wantread
, &dev
->flags
);
4649 set_bit(R5_LOCKED
, &dev
->flags
);
4655 /* Finish reconstruct operations initiated by the expansion process */
4656 if (sh
->reconstruct_state
== reconstruct_state_result
) {
4657 struct stripe_head
*sh_src
4658 = raid5_get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
4659 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
4660 /* sh cannot be written until sh_src has been read.
4661 * so arrange for sh to be delayed a little
4663 set_bit(STRIPE_DELAYED
, &sh
->state
);
4664 set_bit(STRIPE_HANDLE
, &sh
->state
);
4665 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
4667 atomic_inc(&conf
->preread_active_stripes
);
4668 raid5_release_stripe(sh_src
);
4672 raid5_release_stripe(sh_src
);
4674 sh
->reconstruct_state
= reconstruct_state_idle
;
4675 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
4676 for (i
= conf
->raid_disks
; i
--; ) {
4677 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
4678 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4683 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
4684 !sh
->reconstruct_state
) {
4685 /* Need to write out all blocks after computing parity */
4686 sh
->disks
= conf
->raid_disks
;
4687 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
4688 schedule_reconstruction(sh
, &s
, 1, 1);
4689 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
4690 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4691 atomic_dec(&conf
->reshape_stripes
);
4692 wake_up(&conf
->wait_for_overlap
);
4693 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4696 if (s
.expanding
&& s
.locked
== 0 &&
4697 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
4698 handle_stripe_expansion(conf
, sh
);
4701 /* wait for this device to become unblocked */
4702 if (unlikely(s
.blocked_rdev
)) {
4703 if (conf
->mddev
->external
)
4704 md_wait_for_blocked_rdev(s
.blocked_rdev
,
4707 /* Internal metadata will immediately
4708 * be written by raid5d, so we don't
4709 * need to wait here.
4711 rdev_dec_pending(s
.blocked_rdev
,
4715 if (s
.handle_bad_blocks
)
4716 for (i
= disks
; i
--; ) {
4717 struct md_rdev
*rdev
;
4718 struct r5dev
*dev
= &sh
->dev
[i
];
4719 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
4720 /* We own a safe reference to the rdev */
4721 rdev
= conf
->disks
[i
].rdev
;
4722 if (!rdev_set_badblocks(rdev
, sh
->sector
,
4724 md_error(conf
->mddev
, rdev
);
4725 rdev_dec_pending(rdev
, conf
->mddev
);
4727 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
4728 rdev
= conf
->disks
[i
].rdev
;
4729 rdev_clear_badblocks(rdev
, sh
->sector
,
4731 rdev_dec_pending(rdev
, conf
->mddev
);
4733 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4734 rdev
= conf
->disks
[i
].replacement
;
4736 /* rdev have been moved down */
4737 rdev
= conf
->disks
[i
].rdev
;
4738 rdev_clear_badblocks(rdev
, sh
->sector
,
4740 rdev_dec_pending(rdev
, conf
->mddev
);
4745 raid_run_ops(sh
, s
.ops_request
);
4749 if (s
.dec_preread_active
) {
4750 /* We delay this until after ops_run_io so that if make_request
4751 * is waiting on a flush, it won't continue until the writes
4752 * have actually been submitted.
4754 atomic_dec(&conf
->preread_active_stripes
);
4755 if (atomic_read(&conf
->preread_active_stripes
) <
4757 md_wakeup_thread(conf
->mddev
->thread
);
4760 if (!bio_list_empty(&s
.return_bi
)) {
4761 if (test_bit(MD_SB_CHANGE_PENDING
, &conf
->mddev
->sb_flags
)) {
4762 spin_lock_irq(&conf
->device_lock
);
4763 bio_list_merge(&conf
->return_bi
, &s
.return_bi
);
4764 spin_unlock_irq(&conf
->device_lock
);
4765 md_wakeup_thread(conf
->mddev
->thread
);
4767 return_io(&s
.return_bi
);
4770 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4773 static void raid5_activate_delayed(struct r5conf
*conf
)
4775 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
4776 while (!list_empty(&conf
->delayed_list
)) {
4777 struct list_head
*l
= conf
->delayed_list
.next
;
4778 struct stripe_head
*sh
;
4779 sh
= list_entry(l
, struct stripe_head
, lru
);
4781 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4782 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4783 atomic_inc(&conf
->preread_active_stripes
);
4784 list_add_tail(&sh
->lru
, &conf
->hold_list
);
4785 raid5_wakeup_stripe_thread(sh
);
4790 static void activate_bit_delay(struct r5conf
*conf
,
4791 struct list_head
*temp_inactive_list
)
4793 /* device_lock is held */
4794 struct list_head head
;
4795 list_add(&head
, &conf
->bitmap_list
);
4796 list_del_init(&conf
->bitmap_list
);
4797 while (!list_empty(&head
)) {
4798 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
4800 list_del_init(&sh
->lru
);
4801 atomic_inc(&sh
->count
);
4802 hash
= sh
->hash_lock_index
;
4803 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
4807 static int raid5_congested(struct mddev
*mddev
, int bits
)
4809 struct r5conf
*conf
= mddev
->private;
4811 /* No difference between reads and writes. Just check
4812 * how busy the stripe_cache is
4815 if (test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
))
4818 /* Also checks whether there is pressure on r5cache log space */
4819 if (test_bit(R5C_LOG_TIGHT
, &conf
->cache_state
))
4823 if (atomic_read(&conf
->empty_inactive_list_nr
))
4829 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
4831 struct r5conf
*conf
= mddev
->private;
4832 sector_t sector
= bio
->bi_iter
.bi_sector
+ get_start_sect(bio
->bi_bdev
);
4833 unsigned int chunk_sectors
;
4834 unsigned int bio_sectors
= bio_sectors(bio
);
4836 chunk_sectors
= min(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
4837 return chunk_sectors
>=
4838 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
4842 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4843 * later sampled by raid5d.
4845 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
4847 unsigned long flags
;
4849 spin_lock_irqsave(&conf
->device_lock
, flags
);
4851 bi
->bi_next
= conf
->retry_read_aligned_list
;
4852 conf
->retry_read_aligned_list
= bi
;
4854 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4855 md_wakeup_thread(conf
->mddev
->thread
);
4858 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
4862 bi
= conf
->retry_read_aligned
;
4864 conf
->retry_read_aligned
= NULL
;
4867 bi
= conf
->retry_read_aligned_list
;
4869 conf
->retry_read_aligned_list
= bi
->bi_next
;
4872 * this sets the active strip count to 1 and the processed
4873 * strip count to zero (upper 8 bits)
4875 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
4882 * The "raid5_align_endio" should check if the read succeeded and if it
4883 * did, call bio_endio on the original bio (having bio_put the new bio
4885 * If the read failed..
4887 static void raid5_align_endio(struct bio
*bi
)
4889 struct bio
* raid_bi
= bi
->bi_private
;
4890 struct mddev
*mddev
;
4891 struct r5conf
*conf
;
4892 struct md_rdev
*rdev
;
4893 int error
= bi
->bi_error
;
4897 rdev
= (void*)raid_bi
->bi_next
;
4898 raid_bi
->bi_next
= NULL
;
4899 mddev
= rdev
->mddev
;
4900 conf
= mddev
->private;
4902 rdev_dec_pending(rdev
, conf
->mddev
);
4905 trace_block_bio_complete(bdev_get_queue(raid_bi
->bi_bdev
),
4908 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4909 wake_up(&conf
->wait_for_quiescent
);
4913 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
4915 add_bio_to_retry(raid_bi
, conf
);
4918 static int raid5_read_one_chunk(struct mddev
*mddev
, struct bio
*raid_bio
)
4920 struct r5conf
*conf
= mddev
->private;
4922 struct bio
* align_bi
;
4923 struct md_rdev
*rdev
;
4924 sector_t end_sector
;
4926 if (!in_chunk_boundary(mddev
, raid_bio
)) {
4927 pr_debug("%s: non aligned\n", __func__
);
4931 * use bio_clone_mddev to make a copy of the bio
4933 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
4937 * set bi_end_io to a new function, and set bi_private to the
4940 align_bi
->bi_end_io
= raid5_align_endio
;
4941 align_bi
->bi_private
= raid_bio
;
4945 align_bi
->bi_iter
.bi_sector
=
4946 raid5_compute_sector(conf
, raid_bio
->bi_iter
.bi_sector
,
4949 end_sector
= bio_end_sector(align_bi
);
4951 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
4952 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
4953 rdev
->recovery_offset
< end_sector
) {
4954 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
4956 (test_bit(Faulty
, &rdev
->flags
) ||
4957 !(test_bit(In_sync
, &rdev
->flags
) ||
4958 rdev
->recovery_offset
>= end_sector
)))
4965 atomic_inc(&rdev
->nr_pending
);
4967 raid_bio
->bi_next
= (void*)rdev
;
4968 align_bi
->bi_bdev
= rdev
->bdev
;
4969 bio_clear_flag(align_bi
, BIO_SEG_VALID
);
4971 if (is_badblock(rdev
, align_bi
->bi_iter
.bi_sector
,
4972 bio_sectors(align_bi
),
4973 &first_bad
, &bad_sectors
)) {
4975 rdev_dec_pending(rdev
, mddev
);
4979 /* No reshape active, so we can trust rdev->data_offset */
4980 align_bi
->bi_iter
.bi_sector
+= rdev
->data_offset
;
4982 spin_lock_irq(&conf
->device_lock
);
4983 wait_event_lock_irq(conf
->wait_for_quiescent
,
4986 atomic_inc(&conf
->active_aligned_reads
);
4987 spin_unlock_irq(&conf
->device_lock
);
4990 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
4991 align_bi
, disk_devt(mddev
->gendisk
),
4992 raid_bio
->bi_iter
.bi_sector
);
4993 generic_make_request(align_bi
);
5002 static struct bio
*chunk_aligned_read(struct mddev
*mddev
, struct bio
*raid_bio
)
5007 sector_t sector
= raid_bio
->bi_iter
.bi_sector
;
5008 unsigned chunk_sects
= mddev
->chunk_sectors
;
5009 unsigned sectors
= chunk_sects
- (sector
& (chunk_sects
-1));
5011 if (sectors
< bio_sectors(raid_bio
)) {
5012 split
= bio_split(raid_bio
, sectors
, GFP_NOIO
, fs_bio_set
);
5013 bio_chain(split
, raid_bio
);
5017 if (!raid5_read_one_chunk(mddev
, split
)) {
5018 if (split
!= raid_bio
)
5019 generic_make_request(raid_bio
);
5022 } while (split
!= raid_bio
);
5027 /* __get_priority_stripe - get the next stripe to process
5029 * Full stripe writes are allowed to pass preread active stripes up until
5030 * the bypass_threshold is exceeded. In general the bypass_count
5031 * increments when the handle_list is handled before the hold_list; however, it
5032 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5033 * stripe with in flight i/o. The bypass_count will be reset when the
5034 * head of the hold_list has changed, i.e. the head was promoted to the
5037 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
, int group
)
5039 struct stripe_head
*sh
= NULL
, *tmp
;
5040 struct list_head
*handle_list
= NULL
;
5041 struct r5worker_group
*wg
= NULL
;
5043 if (conf
->worker_cnt_per_group
== 0) {
5044 handle_list
= &conf
->handle_list
;
5045 } else if (group
!= ANY_GROUP
) {
5046 handle_list
= &conf
->worker_groups
[group
].handle_list
;
5047 wg
= &conf
->worker_groups
[group
];
5050 for (i
= 0; i
< conf
->group_cnt
; i
++) {
5051 handle_list
= &conf
->worker_groups
[i
].handle_list
;
5052 wg
= &conf
->worker_groups
[i
];
5053 if (!list_empty(handle_list
))
5058 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5060 list_empty(handle_list
) ? "empty" : "busy",
5061 list_empty(&conf
->hold_list
) ? "empty" : "busy",
5062 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
5064 if (!list_empty(handle_list
)) {
5065 sh
= list_entry(handle_list
->next
, typeof(*sh
), lru
);
5067 if (list_empty(&conf
->hold_list
))
5068 conf
->bypass_count
= 0;
5069 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
5070 if (conf
->hold_list
.next
== conf
->last_hold
)
5071 conf
->bypass_count
++;
5073 conf
->last_hold
= conf
->hold_list
.next
;
5074 conf
->bypass_count
-= conf
->bypass_threshold
;
5075 if (conf
->bypass_count
< 0)
5076 conf
->bypass_count
= 0;
5079 } else if (!list_empty(&conf
->hold_list
) &&
5080 ((conf
->bypass_threshold
&&
5081 conf
->bypass_count
> conf
->bypass_threshold
) ||
5082 atomic_read(&conf
->pending_full_writes
) == 0)) {
5084 list_for_each_entry(tmp
, &conf
->hold_list
, lru
) {
5085 if (conf
->worker_cnt_per_group
== 0 ||
5086 group
== ANY_GROUP
||
5087 !cpu_online(tmp
->cpu
) ||
5088 cpu_to_group(tmp
->cpu
) == group
) {
5095 conf
->bypass_count
-= conf
->bypass_threshold
;
5096 if (conf
->bypass_count
< 0)
5097 conf
->bypass_count
= 0;
5109 list_del_init(&sh
->lru
);
5110 BUG_ON(atomic_inc_return(&sh
->count
) != 1);
5114 struct raid5_plug_cb
{
5115 struct blk_plug_cb cb
;
5116 struct list_head list
;
5117 struct list_head temp_inactive_list
[NR_STRIPE_HASH_LOCKS
];
5120 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
5122 struct raid5_plug_cb
*cb
= container_of(
5123 blk_cb
, struct raid5_plug_cb
, cb
);
5124 struct stripe_head
*sh
;
5125 struct mddev
*mddev
= cb
->cb
.data
;
5126 struct r5conf
*conf
= mddev
->private;
5130 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
5131 spin_lock_irq(&conf
->device_lock
);
5132 while (!list_empty(&cb
->list
)) {
5133 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
5134 list_del_init(&sh
->lru
);
5136 * avoid race release_stripe_plug() sees
5137 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5138 * is still in our list
5140 smp_mb__before_atomic();
5141 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
5143 * STRIPE_ON_RELEASE_LIST could be set here. In that
5144 * case, the count is always > 1 here
5146 hash
= sh
->hash_lock_index
;
5147 __release_stripe(conf
, sh
, &cb
->temp_inactive_list
[hash
]);
5150 spin_unlock_irq(&conf
->device_lock
);
5152 release_inactive_stripe_list(conf
, cb
->temp_inactive_list
,
5153 NR_STRIPE_HASH_LOCKS
);
5155 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
5159 static void release_stripe_plug(struct mddev
*mddev
,
5160 struct stripe_head
*sh
)
5162 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
5163 raid5_unplug
, mddev
,
5164 sizeof(struct raid5_plug_cb
));
5165 struct raid5_plug_cb
*cb
;
5168 raid5_release_stripe(sh
);
5172 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
5174 if (cb
->list
.next
== NULL
) {
5176 INIT_LIST_HEAD(&cb
->list
);
5177 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5178 INIT_LIST_HEAD(cb
->temp_inactive_list
+ i
);
5181 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
5182 list_add_tail(&sh
->lru
, &cb
->list
);
5184 raid5_release_stripe(sh
);
5187 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
5189 struct r5conf
*conf
= mddev
->private;
5190 sector_t logical_sector
, last_sector
;
5191 struct stripe_head
*sh
;
5195 if (mddev
->reshape_position
!= MaxSector
)
5196 /* Skip discard while reshape is happening */
5199 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5200 last_sector
= bi
->bi_iter
.bi_sector
+ (bi
->bi_iter
.bi_size
>>9);
5203 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5205 stripe_sectors
= conf
->chunk_sectors
*
5206 (conf
->raid_disks
- conf
->max_degraded
);
5207 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
5209 sector_div(last_sector
, stripe_sectors
);
5211 logical_sector
*= conf
->chunk_sectors
;
5212 last_sector
*= conf
->chunk_sectors
;
5214 for (; logical_sector
< last_sector
;
5215 logical_sector
+= STRIPE_SECTORS
) {
5219 sh
= raid5_get_active_stripe(conf
, logical_sector
, 0, 0, 0);
5220 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5221 TASK_UNINTERRUPTIBLE
);
5222 set_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5223 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
5224 raid5_release_stripe(sh
);
5228 clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5229 spin_lock_irq(&sh
->stripe_lock
);
5230 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5231 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5233 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
5234 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
5235 spin_unlock_irq(&sh
->stripe_lock
);
5236 raid5_release_stripe(sh
);
5241 set_bit(STRIPE_DISCARD
, &sh
->state
);
5242 finish_wait(&conf
->wait_for_overlap
, &w
);
5243 sh
->overwrite_disks
= 0;
5244 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5245 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5247 sh
->dev
[d
].towrite
= bi
;
5248 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
5249 raid5_inc_bi_active_stripes(bi
);
5250 sh
->overwrite_disks
++;
5252 spin_unlock_irq(&sh
->stripe_lock
);
5253 if (conf
->mddev
->bitmap
) {
5255 d
< conf
->raid_disks
- conf
->max_degraded
;
5257 bitmap_startwrite(mddev
->bitmap
,
5261 sh
->bm_seq
= conf
->seq_flush
+ 1;
5262 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
5265 set_bit(STRIPE_HANDLE
, &sh
->state
);
5266 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5267 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5268 atomic_inc(&conf
->preread_active_stripes
);
5269 release_stripe_plug(mddev
, sh
);
5272 remaining
= raid5_dec_bi_active_stripes(bi
);
5273 if (remaining
== 0) {
5274 md_write_end(mddev
);
5279 static void raid5_make_request(struct mddev
*mddev
, struct bio
* bi
)
5281 struct r5conf
*conf
= mddev
->private;
5283 sector_t new_sector
;
5284 sector_t logical_sector
, last_sector
;
5285 struct stripe_head
*sh
;
5286 const int rw
= bio_data_dir(bi
);
5290 bool do_flush
= false;
5292 if (unlikely(bi
->bi_opf
& REQ_PREFLUSH
)) {
5293 int ret
= r5l_handle_flush_request(conf
->log
, bi
);
5297 if (ret
== -ENODEV
) {
5298 md_flush_request(mddev
, bi
);
5301 /* ret == -EAGAIN, fallback */
5303 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5304 * we need to flush journal device
5306 do_flush
= bi
->bi_opf
& REQ_PREFLUSH
;
5309 md_write_start(mddev
, bi
);
5312 * If array is degraded, better not do chunk aligned read because
5313 * later we might have to read it again in order to reconstruct
5314 * data on failed drives.
5316 if (rw
== READ
&& mddev
->degraded
== 0 &&
5317 !r5c_is_writeback(conf
->log
) &&
5318 mddev
->reshape_position
== MaxSector
) {
5319 bi
= chunk_aligned_read(mddev
, bi
);
5324 if (unlikely(bio_op(bi
) == REQ_OP_DISCARD
)) {
5325 make_discard_request(mddev
, bi
);
5329 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5330 last_sector
= bio_end_sector(bi
);
5332 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5334 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
5335 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
5341 seq
= read_seqcount_begin(&conf
->gen_lock
);
5344 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5345 TASK_UNINTERRUPTIBLE
);
5346 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
5347 /* spinlock is needed as reshape_progress may be
5348 * 64bit on a 32bit platform, and so it might be
5349 * possible to see a half-updated value
5350 * Of course reshape_progress could change after
5351 * the lock is dropped, so once we get a reference
5352 * to the stripe that we think it is, we will have
5355 spin_lock_irq(&conf
->device_lock
);
5356 if (mddev
->reshape_backwards
5357 ? logical_sector
< conf
->reshape_progress
5358 : logical_sector
>= conf
->reshape_progress
) {
5361 if (mddev
->reshape_backwards
5362 ? logical_sector
< conf
->reshape_safe
5363 : logical_sector
>= conf
->reshape_safe
) {
5364 spin_unlock_irq(&conf
->device_lock
);
5370 spin_unlock_irq(&conf
->device_lock
);
5373 new_sector
= raid5_compute_sector(conf
, logical_sector
,
5376 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
5377 (unsigned long long)new_sector
,
5378 (unsigned long long)logical_sector
);
5380 sh
= raid5_get_active_stripe(conf
, new_sector
, previous
,
5381 (bi
->bi_opf
& REQ_RAHEAD
), 0);
5383 if (unlikely(previous
)) {
5384 /* expansion might have moved on while waiting for a
5385 * stripe, so we must do the range check again.
5386 * Expansion could still move past after this
5387 * test, but as we are holding a reference to
5388 * 'sh', we know that if that happens,
5389 * STRIPE_EXPANDING will get set and the expansion
5390 * won't proceed until we finish with the stripe.
5393 spin_lock_irq(&conf
->device_lock
);
5394 if (mddev
->reshape_backwards
5395 ? logical_sector
>= conf
->reshape_progress
5396 : logical_sector
< conf
->reshape_progress
)
5397 /* mismatch, need to try again */
5399 spin_unlock_irq(&conf
->device_lock
);
5401 raid5_release_stripe(sh
);
5407 if (read_seqcount_retry(&conf
->gen_lock
, seq
)) {
5408 /* Might have got the wrong stripe_head
5411 raid5_release_stripe(sh
);
5416 logical_sector
>= mddev
->suspend_lo
&&
5417 logical_sector
< mddev
->suspend_hi
) {
5418 raid5_release_stripe(sh
);
5419 /* As the suspend_* range is controlled by
5420 * userspace, we want an interruptible
5423 flush_signals(current
);
5424 prepare_to_wait(&conf
->wait_for_overlap
,
5425 &w
, TASK_INTERRUPTIBLE
);
5426 if (logical_sector
>= mddev
->suspend_lo
&&
5427 logical_sector
< mddev
->suspend_hi
) {
5434 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
5435 !add_stripe_bio(sh
, bi
, dd_idx
, rw
, previous
)) {
5436 /* Stripe is busy expanding or
5437 * add failed due to overlap. Flush everything
5440 md_wakeup_thread(mddev
->thread
);
5441 raid5_release_stripe(sh
);
5447 set_bit(STRIPE_R5C_PREFLUSH
, &sh
->state
);
5448 /* we only need flush for one stripe */
5452 set_bit(STRIPE_HANDLE
, &sh
->state
);
5453 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5454 if ((!sh
->batch_head
|| sh
== sh
->batch_head
) &&
5455 (bi
->bi_opf
& REQ_SYNC
) &&
5456 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5457 atomic_inc(&conf
->preread_active_stripes
);
5458 release_stripe_plug(mddev
, sh
);
5460 /* cannot get stripe for read-ahead, just give-up */
5461 bi
->bi_error
= -EIO
;
5465 finish_wait(&conf
->wait_for_overlap
, &w
);
5467 remaining
= raid5_dec_bi_active_stripes(bi
);
5468 if (remaining
== 0) {
5471 md_write_end(mddev
);
5473 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
5479 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
5481 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
5483 /* reshaping is quite different to recovery/resync so it is
5484 * handled quite separately ... here.
5486 * On each call to sync_request, we gather one chunk worth of
5487 * destination stripes and flag them as expanding.
5488 * Then we find all the source stripes and request reads.
5489 * As the reads complete, handle_stripe will copy the data
5490 * into the destination stripe and release that stripe.
5492 struct r5conf
*conf
= mddev
->private;
5493 struct stripe_head
*sh
;
5494 sector_t first_sector
, last_sector
;
5495 int raid_disks
= conf
->previous_raid_disks
;
5496 int data_disks
= raid_disks
- conf
->max_degraded
;
5497 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5500 sector_t writepos
, readpos
, safepos
;
5501 sector_t stripe_addr
;
5502 int reshape_sectors
;
5503 struct list_head stripes
;
5506 if (sector_nr
== 0) {
5507 /* If restarting in the middle, skip the initial sectors */
5508 if (mddev
->reshape_backwards
&&
5509 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
5510 sector_nr
= raid5_size(mddev
, 0, 0)
5511 - conf
->reshape_progress
;
5512 } else if (mddev
->reshape_backwards
&&
5513 conf
->reshape_progress
== MaxSector
) {
5514 /* shouldn't happen, but just in case, finish up.*/
5515 sector_nr
= MaxSector
;
5516 } else if (!mddev
->reshape_backwards
&&
5517 conf
->reshape_progress
> 0)
5518 sector_nr
= conf
->reshape_progress
;
5519 sector_div(sector_nr
, new_data_disks
);
5521 mddev
->curr_resync_completed
= sector_nr
;
5522 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5529 /* We need to process a full chunk at a time.
5530 * If old and new chunk sizes differ, we need to process the
5534 reshape_sectors
= max(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
5536 /* We update the metadata at least every 10 seconds, or when
5537 * the data about to be copied would over-write the source of
5538 * the data at the front of the range. i.e. one new_stripe
5539 * along from reshape_progress new_maps to after where
5540 * reshape_safe old_maps to
5542 writepos
= conf
->reshape_progress
;
5543 sector_div(writepos
, new_data_disks
);
5544 readpos
= conf
->reshape_progress
;
5545 sector_div(readpos
, data_disks
);
5546 safepos
= conf
->reshape_safe
;
5547 sector_div(safepos
, data_disks
);
5548 if (mddev
->reshape_backwards
) {
5549 BUG_ON(writepos
< reshape_sectors
);
5550 writepos
-= reshape_sectors
;
5551 readpos
+= reshape_sectors
;
5552 safepos
+= reshape_sectors
;
5554 writepos
+= reshape_sectors
;
5555 /* readpos and safepos are worst-case calculations.
5556 * A negative number is overly pessimistic, and causes
5557 * obvious problems for unsigned storage. So clip to 0.
5559 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
5560 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
5563 /* Having calculated the 'writepos' possibly use it
5564 * to set 'stripe_addr' which is where we will write to.
5566 if (mddev
->reshape_backwards
) {
5567 BUG_ON(conf
->reshape_progress
== 0);
5568 stripe_addr
= writepos
;
5569 BUG_ON((mddev
->dev_sectors
&
5570 ~((sector_t
)reshape_sectors
- 1))
5571 - reshape_sectors
- stripe_addr
5574 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
5575 stripe_addr
= sector_nr
;
5578 /* 'writepos' is the most advanced device address we might write.
5579 * 'readpos' is the least advanced device address we might read.
5580 * 'safepos' is the least address recorded in the metadata as having
5582 * If there is a min_offset_diff, these are adjusted either by
5583 * increasing the safepos/readpos if diff is negative, or
5584 * increasing writepos if diff is positive.
5585 * If 'readpos' is then behind 'writepos', there is no way that we can
5586 * ensure safety in the face of a crash - that must be done by userspace
5587 * making a backup of the data. So in that case there is no particular
5588 * rush to update metadata.
5589 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5590 * update the metadata to advance 'safepos' to match 'readpos' so that
5591 * we can be safe in the event of a crash.
5592 * So we insist on updating metadata if safepos is behind writepos and
5593 * readpos is beyond writepos.
5594 * In any case, update the metadata every 10 seconds.
5595 * Maybe that number should be configurable, but I'm not sure it is
5596 * worth it.... maybe it could be a multiple of safemode_delay???
5598 if (conf
->min_offset_diff
< 0) {
5599 safepos
+= -conf
->min_offset_diff
;
5600 readpos
+= -conf
->min_offset_diff
;
5602 writepos
+= conf
->min_offset_diff
;
5604 if ((mddev
->reshape_backwards
5605 ? (safepos
> writepos
&& readpos
< writepos
)
5606 : (safepos
< writepos
&& readpos
> writepos
)) ||
5607 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
5608 /* Cannot proceed until we've updated the superblock... */
5609 wait_event(conf
->wait_for_overlap
,
5610 atomic_read(&conf
->reshape_stripes
)==0
5611 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5612 if (atomic_read(&conf
->reshape_stripes
) != 0)
5614 mddev
->reshape_position
= conf
->reshape_progress
;
5615 mddev
->curr_resync_completed
= sector_nr
;
5616 conf
->reshape_checkpoint
= jiffies
;
5617 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
5618 md_wakeup_thread(mddev
->thread
);
5619 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
5620 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5621 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5623 spin_lock_irq(&conf
->device_lock
);
5624 conf
->reshape_safe
= mddev
->reshape_position
;
5625 spin_unlock_irq(&conf
->device_lock
);
5626 wake_up(&conf
->wait_for_overlap
);
5627 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5630 INIT_LIST_HEAD(&stripes
);
5631 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
5633 int skipped_disk
= 0;
5634 sh
= raid5_get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
5635 set_bit(STRIPE_EXPANDING
, &sh
->state
);
5636 atomic_inc(&conf
->reshape_stripes
);
5637 /* If any of this stripe is beyond the end of the old
5638 * array, then we need to zero those blocks
5640 for (j
=sh
->disks
; j
--;) {
5642 if (j
== sh
->pd_idx
)
5644 if (conf
->level
== 6 &&
5647 s
= raid5_compute_blocknr(sh
, j
, 0);
5648 if (s
< raid5_size(mddev
, 0, 0)) {
5652 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
5653 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
5654 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
5656 if (!skipped_disk
) {
5657 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
5658 set_bit(STRIPE_HANDLE
, &sh
->state
);
5660 list_add(&sh
->lru
, &stripes
);
5662 spin_lock_irq(&conf
->device_lock
);
5663 if (mddev
->reshape_backwards
)
5664 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
5666 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
5667 spin_unlock_irq(&conf
->device_lock
);
5668 /* Ok, those stripe are ready. We can start scheduling
5669 * reads on the source stripes.
5670 * The source stripes are determined by mapping the first and last
5671 * block on the destination stripes.
5674 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
5677 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
5678 * new_data_disks
- 1),
5680 if (last_sector
>= mddev
->dev_sectors
)
5681 last_sector
= mddev
->dev_sectors
- 1;
5682 while (first_sector
<= last_sector
) {
5683 sh
= raid5_get_active_stripe(conf
, first_sector
, 1, 0, 1);
5684 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
5685 set_bit(STRIPE_HANDLE
, &sh
->state
);
5686 raid5_release_stripe(sh
);
5687 first_sector
+= STRIPE_SECTORS
;
5689 /* Now that the sources are clearly marked, we can release
5690 * the destination stripes
5692 while (!list_empty(&stripes
)) {
5693 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
5694 list_del_init(&sh
->lru
);
5695 raid5_release_stripe(sh
);
5697 /* If this takes us to the resync_max point where we have to pause,
5698 * then we need to write out the superblock.
5700 sector_nr
+= reshape_sectors
;
5701 retn
= reshape_sectors
;
5703 if (mddev
->curr_resync_completed
> mddev
->resync_max
||
5704 (sector_nr
- mddev
->curr_resync_completed
) * 2
5705 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
5706 /* Cannot proceed until we've updated the superblock... */
5707 wait_event(conf
->wait_for_overlap
,
5708 atomic_read(&conf
->reshape_stripes
) == 0
5709 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5710 if (atomic_read(&conf
->reshape_stripes
) != 0)
5712 mddev
->reshape_position
= conf
->reshape_progress
;
5713 mddev
->curr_resync_completed
= sector_nr
;
5714 conf
->reshape_checkpoint
= jiffies
;
5715 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
5716 md_wakeup_thread(mddev
->thread
);
5717 wait_event(mddev
->sb_wait
,
5718 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)
5719 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5720 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5722 spin_lock_irq(&conf
->device_lock
);
5723 conf
->reshape_safe
= mddev
->reshape_position
;
5724 spin_unlock_irq(&conf
->device_lock
);
5725 wake_up(&conf
->wait_for_overlap
);
5726 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5732 static inline sector_t
raid5_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
5735 struct r5conf
*conf
= mddev
->private;
5736 struct stripe_head
*sh
;
5737 sector_t max_sector
= mddev
->dev_sectors
;
5738 sector_t sync_blocks
;
5739 int still_degraded
= 0;
5742 if (sector_nr
>= max_sector
) {
5743 /* just being told to finish up .. nothing much to do */
5745 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
5750 if (mddev
->curr_resync
< max_sector
) /* aborted */
5751 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
5753 else /* completed sync */
5755 bitmap_close_sync(mddev
->bitmap
);
5760 /* Allow raid5_quiesce to complete */
5761 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
5763 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5764 return reshape_request(mddev
, sector_nr
, skipped
);
5766 /* No need to check resync_max as we never do more than one
5767 * stripe, and as resync_max will always be on a chunk boundary,
5768 * if the check in md_do_sync didn't fire, there is no chance
5769 * of overstepping resync_max here
5772 /* if there is too many failed drives and we are trying
5773 * to resync, then assert that we are finished, because there is
5774 * nothing we can do.
5776 if (mddev
->degraded
>= conf
->max_degraded
&&
5777 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5778 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
5782 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
5784 !bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
5785 sync_blocks
>= STRIPE_SECTORS
) {
5786 /* we can skip this block, and probably more */
5787 sync_blocks
/= STRIPE_SECTORS
;
5789 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
5792 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
, false);
5794 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 1, 0);
5796 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 0, 0);
5797 /* make sure we don't swamp the stripe cache if someone else
5798 * is trying to get access
5800 schedule_timeout_uninterruptible(1);
5802 /* Need to check if array will still be degraded after recovery/resync
5803 * Note in case of > 1 drive failures it's possible we're rebuilding
5804 * one drive while leaving another faulty drive in array.
5807 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5808 struct md_rdev
*rdev
= ACCESS_ONCE(conf
->disks
[i
].rdev
);
5810 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
))
5815 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
5817 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
5818 set_bit(STRIPE_HANDLE
, &sh
->state
);
5820 raid5_release_stripe(sh
);
5822 return STRIPE_SECTORS
;
5825 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
5827 /* We may not be able to submit a whole bio at once as there
5828 * may not be enough stripe_heads available.
5829 * We cannot pre-allocate enough stripe_heads as we may need
5830 * more than exist in the cache (if we allow ever large chunks).
5831 * So we do one stripe head at a time and record in
5832 * ->bi_hw_segments how many have been done.
5834 * We *know* that this entire raid_bio is in one chunk, so
5835 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5837 struct stripe_head
*sh
;
5839 sector_t sector
, logical_sector
, last_sector
;
5844 logical_sector
= raid_bio
->bi_iter
.bi_sector
&
5845 ~((sector_t
)STRIPE_SECTORS
-1);
5846 sector
= raid5_compute_sector(conf
, logical_sector
,
5848 last_sector
= bio_end_sector(raid_bio
);
5850 for (; logical_sector
< last_sector
;
5851 logical_sector
+= STRIPE_SECTORS
,
5852 sector
+= STRIPE_SECTORS
,
5855 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
5856 /* already done this stripe */
5859 sh
= raid5_get_active_stripe(conf
, sector
, 0, 1, 1);
5862 /* failed to get a stripe - must wait */
5863 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5864 conf
->retry_read_aligned
= raid_bio
;
5868 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0, 0)) {
5869 raid5_release_stripe(sh
);
5870 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5871 conf
->retry_read_aligned
= raid_bio
;
5875 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
5877 raid5_release_stripe(sh
);
5880 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
5881 if (remaining
== 0) {
5882 trace_block_bio_complete(bdev_get_queue(raid_bio
->bi_bdev
),
5884 bio_endio(raid_bio
);
5886 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
5887 wake_up(&conf
->wait_for_quiescent
);
5891 static int handle_active_stripes(struct r5conf
*conf
, int group
,
5892 struct r5worker
*worker
,
5893 struct list_head
*temp_inactive_list
)
5895 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
5896 int i
, batch_size
= 0, hash
;
5897 bool release_inactive
= false;
5899 while (batch_size
< MAX_STRIPE_BATCH
&&
5900 (sh
= __get_priority_stripe(conf
, group
)) != NULL
)
5901 batch
[batch_size
++] = sh
;
5903 if (batch_size
== 0) {
5904 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5905 if (!list_empty(temp_inactive_list
+ i
))
5907 if (i
== NR_STRIPE_HASH_LOCKS
) {
5908 spin_unlock_irq(&conf
->device_lock
);
5909 r5l_flush_stripe_to_raid(conf
->log
);
5910 spin_lock_irq(&conf
->device_lock
);
5913 release_inactive
= true;
5915 spin_unlock_irq(&conf
->device_lock
);
5917 release_inactive_stripe_list(conf
, temp_inactive_list
,
5918 NR_STRIPE_HASH_LOCKS
);
5920 r5l_flush_stripe_to_raid(conf
->log
);
5921 if (release_inactive
) {
5922 spin_lock_irq(&conf
->device_lock
);
5926 for (i
= 0; i
< batch_size
; i
++)
5927 handle_stripe(batch
[i
]);
5928 r5l_write_stripe_run(conf
->log
);
5932 spin_lock_irq(&conf
->device_lock
);
5933 for (i
= 0; i
< batch_size
; i
++) {
5934 hash
= batch
[i
]->hash_lock_index
;
5935 __release_stripe(conf
, batch
[i
], &temp_inactive_list
[hash
]);
5940 static void raid5_do_work(struct work_struct
*work
)
5942 struct r5worker
*worker
= container_of(work
, struct r5worker
, work
);
5943 struct r5worker_group
*group
= worker
->group
;
5944 struct r5conf
*conf
= group
->conf
;
5945 int group_id
= group
- conf
->worker_groups
;
5947 struct blk_plug plug
;
5949 pr_debug("+++ raid5worker active\n");
5951 blk_start_plug(&plug
);
5953 spin_lock_irq(&conf
->device_lock
);
5955 int batch_size
, released
;
5957 released
= release_stripe_list(conf
, worker
->temp_inactive_list
);
5959 batch_size
= handle_active_stripes(conf
, group_id
, worker
,
5960 worker
->temp_inactive_list
);
5961 worker
->working
= false;
5962 if (!batch_size
&& !released
)
5964 handled
+= batch_size
;
5966 pr_debug("%d stripes handled\n", handled
);
5968 spin_unlock_irq(&conf
->device_lock
);
5969 blk_finish_plug(&plug
);
5971 pr_debug("--- raid5worker inactive\n");
5975 * This is our raid5 kernel thread.
5977 * We scan the hash table for stripes which can be handled now.
5978 * During the scan, completed stripes are saved for us by the interrupt
5979 * handler, so that they will not have to wait for our next wakeup.
5981 static void raid5d(struct md_thread
*thread
)
5983 struct mddev
*mddev
= thread
->mddev
;
5984 struct r5conf
*conf
= mddev
->private;
5986 struct blk_plug plug
;
5988 pr_debug("+++ raid5d active\n");
5990 md_check_recovery(mddev
);
5992 if (!bio_list_empty(&conf
->return_bi
) &&
5993 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
5994 struct bio_list tmp
= BIO_EMPTY_LIST
;
5995 spin_lock_irq(&conf
->device_lock
);
5996 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
5997 bio_list_merge(&tmp
, &conf
->return_bi
);
5998 bio_list_init(&conf
->return_bi
);
6000 spin_unlock_irq(&conf
->device_lock
);
6004 blk_start_plug(&plug
);
6006 spin_lock_irq(&conf
->device_lock
);
6009 int batch_size
, released
;
6011 released
= release_stripe_list(conf
, conf
->temp_inactive_list
);
6013 clear_bit(R5_DID_ALLOC
, &conf
->cache_state
);
6016 !list_empty(&conf
->bitmap_list
)) {
6017 /* Now is a good time to flush some bitmap updates */
6019 spin_unlock_irq(&conf
->device_lock
);
6020 bitmap_unplug(mddev
->bitmap
);
6021 spin_lock_irq(&conf
->device_lock
);
6022 conf
->seq_write
= conf
->seq_flush
;
6023 activate_bit_delay(conf
, conf
->temp_inactive_list
);
6025 raid5_activate_delayed(conf
);
6027 while ((bio
= remove_bio_from_retry(conf
))) {
6029 spin_unlock_irq(&conf
->device_lock
);
6030 ok
= retry_aligned_read(conf
, bio
);
6031 spin_lock_irq(&conf
->device_lock
);
6037 batch_size
= handle_active_stripes(conf
, ANY_GROUP
, NULL
,
6038 conf
->temp_inactive_list
);
6039 if (!batch_size
&& !released
)
6041 handled
+= batch_size
;
6043 if (mddev
->sb_flags
& ~(1 << MD_SB_CHANGE_PENDING
)) {
6044 spin_unlock_irq(&conf
->device_lock
);
6045 md_check_recovery(mddev
);
6046 spin_lock_irq(&conf
->device_lock
);
6049 pr_debug("%d stripes handled\n", handled
);
6051 spin_unlock_irq(&conf
->device_lock
);
6052 if (test_and_clear_bit(R5_ALLOC_MORE
, &conf
->cache_state
) &&
6053 mutex_trylock(&conf
->cache_size_mutex
)) {
6054 grow_one_stripe(conf
, __GFP_NOWARN
);
6055 /* Set flag even if allocation failed. This helps
6056 * slow down allocation requests when mem is short
6058 set_bit(R5_DID_ALLOC
, &conf
->cache_state
);
6059 mutex_unlock(&conf
->cache_size_mutex
);
6062 r5l_flush_stripe_to_raid(conf
->log
);
6064 async_tx_issue_pending_all();
6065 blk_finish_plug(&plug
);
6067 pr_debug("--- raid5d inactive\n");
6071 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
6073 struct r5conf
*conf
;
6075 spin_lock(&mddev
->lock
);
6076 conf
= mddev
->private;
6078 ret
= sprintf(page
, "%d\n", conf
->min_nr_stripes
);
6079 spin_unlock(&mddev
->lock
);
6084 raid5_set_cache_size(struct mddev
*mddev
, int size
)
6086 struct r5conf
*conf
= mddev
->private;
6089 if (size
<= 16 || size
> 32768)
6092 conf
->min_nr_stripes
= size
;
6093 mutex_lock(&conf
->cache_size_mutex
);
6094 while (size
< conf
->max_nr_stripes
&&
6095 drop_one_stripe(conf
))
6097 mutex_unlock(&conf
->cache_size_mutex
);
6100 err
= md_allow_write(mddev
);
6104 mutex_lock(&conf
->cache_size_mutex
);
6105 while (size
> conf
->max_nr_stripes
)
6106 if (!grow_one_stripe(conf
, GFP_KERNEL
))
6108 mutex_unlock(&conf
->cache_size_mutex
);
6112 EXPORT_SYMBOL(raid5_set_cache_size
);
6115 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
6117 struct r5conf
*conf
;
6121 if (len
>= PAGE_SIZE
)
6123 if (kstrtoul(page
, 10, &new))
6125 err
= mddev_lock(mddev
);
6128 conf
= mddev
->private;
6132 err
= raid5_set_cache_size(mddev
, new);
6133 mddev_unlock(mddev
);
6138 static struct md_sysfs_entry
6139 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
6140 raid5_show_stripe_cache_size
,
6141 raid5_store_stripe_cache_size
);
6144 raid5_show_rmw_level(struct mddev
*mddev
, char *page
)
6146 struct r5conf
*conf
= mddev
->private;
6148 return sprintf(page
, "%d\n", conf
->rmw_level
);
6154 raid5_store_rmw_level(struct mddev
*mddev
, const char *page
, size_t len
)
6156 struct r5conf
*conf
= mddev
->private;
6162 if (len
>= PAGE_SIZE
)
6165 if (kstrtoul(page
, 10, &new))
6168 if (new != PARITY_DISABLE_RMW
&& !raid6_call
.xor_syndrome
)
6171 if (new != PARITY_DISABLE_RMW
&&
6172 new != PARITY_ENABLE_RMW
&&
6173 new != PARITY_PREFER_RMW
)
6176 conf
->rmw_level
= new;
6180 static struct md_sysfs_entry
6181 raid5_rmw_level
= __ATTR(rmw_level
, S_IRUGO
| S_IWUSR
,
6182 raid5_show_rmw_level
,
6183 raid5_store_rmw_level
);
6187 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
6189 struct r5conf
*conf
;
6191 spin_lock(&mddev
->lock
);
6192 conf
= mddev
->private;
6194 ret
= sprintf(page
, "%d\n", conf
->bypass_threshold
);
6195 spin_unlock(&mddev
->lock
);
6200 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
6202 struct r5conf
*conf
;
6206 if (len
>= PAGE_SIZE
)
6208 if (kstrtoul(page
, 10, &new))
6211 err
= mddev_lock(mddev
);
6214 conf
= mddev
->private;
6217 else if (new > conf
->min_nr_stripes
)
6220 conf
->bypass_threshold
= new;
6221 mddev_unlock(mddev
);
6225 static struct md_sysfs_entry
6226 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
6228 raid5_show_preread_threshold
,
6229 raid5_store_preread_threshold
);
6232 raid5_show_skip_copy(struct mddev
*mddev
, char *page
)
6234 struct r5conf
*conf
;
6236 spin_lock(&mddev
->lock
);
6237 conf
= mddev
->private;
6239 ret
= sprintf(page
, "%d\n", conf
->skip_copy
);
6240 spin_unlock(&mddev
->lock
);
6245 raid5_store_skip_copy(struct mddev
*mddev
, const char *page
, size_t len
)
6247 struct r5conf
*conf
;
6251 if (len
>= PAGE_SIZE
)
6253 if (kstrtoul(page
, 10, &new))
6257 err
= mddev_lock(mddev
);
6260 conf
= mddev
->private;
6263 else if (new != conf
->skip_copy
) {
6264 mddev_suspend(mddev
);
6265 conf
->skip_copy
= new;
6267 mddev
->queue
->backing_dev_info
.capabilities
|=
6268 BDI_CAP_STABLE_WRITES
;
6270 mddev
->queue
->backing_dev_info
.capabilities
&=
6271 ~BDI_CAP_STABLE_WRITES
;
6272 mddev_resume(mddev
);
6274 mddev_unlock(mddev
);
6278 static struct md_sysfs_entry
6279 raid5_skip_copy
= __ATTR(skip_copy
, S_IRUGO
| S_IWUSR
,
6280 raid5_show_skip_copy
,
6281 raid5_store_skip_copy
);
6284 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
6286 struct r5conf
*conf
= mddev
->private;
6288 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
6293 static struct md_sysfs_entry
6294 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
6297 raid5_show_group_thread_cnt(struct mddev
*mddev
, char *page
)
6299 struct r5conf
*conf
;
6301 spin_lock(&mddev
->lock
);
6302 conf
= mddev
->private;
6304 ret
= sprintf(page
, "%d\n", conf
->worker_cnt_per_group
);
6305 spin_unlock(&mddev
->lock
);
6309 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6311 int *worker_cnt_per_group
,
6312 struct r5worker_group
**worker_groups
);
6314 raid5_store_group_thread_cnt(struct mddev
*mddev
, const char *page
, size_t len
)
6316 struct r5conf
*conf
;
6319 struct r5worker_group
*new_groups
, *old_groups
;
6320 int group_cnt
, worker_cnt_per_group
;
6322 if (len
>= PAGE_SIZE
)
6324 if (kstrtoul(page
, 10, &new))
6327 err
= mddev_lock(mddev
);
6330 conf
= mddev
->private;
6333 else if (new != conf
->worker_cnt_per_group
) {
6334 mddev_suspend(mddev
);
6336 old_groups
= conf
->worker_groups
;
6338 flush_workqueue(raid5_wq
);
6340 err
= alloc_thread_groups(conf
, new,
6341 &group_cnt
, &worker_cnt_per_group
,
6344 spin_lock_irq(&conf
->device_lock
);
6345 conf
->group_cnt
= group_cnt
;
6346 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6347 conf
->worker_groups
= new_groups
;
6348 spin_unlock_irq(&conf
->device_lock
);
6351 kfree(old_groups
[0].workers
);
6354 mddev_resume(mddev
);
6356 mddev_unlock(mddev
);
6361 static struct md_sysfs_entry
6362 raid5_group_thread_cnt
= __ATTR(group_thread_cnt
, S_IRUGO
| S_IWUSR
,
6363 raid5_show_group_thread_cnt
,
6364 raid5_store_group_thread_cnt
);
6366 static struct attribute
*raid5_attrs
[] = {
6367 &raid5_stripecache_size
.attr
,
6368 &raid5_stripecache_active
.attr
,
6369 &raid5_preread_bypass_threshold
.attr
,
6370 &raid5_group_thread_cnt
.attr
,
6371 &raid5_skip_copy
.attr
,
6372 &raid5_rmw_level
.attr
,
6373 &r5c_journal_mode
.attr
,
6376 static struct attribute_group raid5_attrs_group
= {
6378 .attrs
= raid5_attrs
,
6381 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6383 int *worker_cnt_per_group
,
6384 struct r5worker_group
**worker_groups
)
6388 struct r5worker
*workers
;
6390 *worker_cnt_per_group
= cnt
;
6393 *worker_groups
= NULL
;
6396 *group_cnt
= num_possible_nodes();
6397 size
= sizeof(struct r5worker
) * cnt
;
6398 workers
= kzalloc(size
* *group_cnt
, GFP_NOIO
);
6399 *worker_groups
= kzalloc(sizeof(struct r5worker_group
) *
6400 *group_cnt
, GFP_NOIO
);
6401 if (!*worker_groups
|| !workers
) {
6403 kfree(*worker_groups
);
6407 for (i
= 0; i
< *group_cnt
; i
++) {
6408 struct r5worker_group
*group
;
6410 group
= &(*worker_groups
)[i
];
6411 INIT_LIST_HEAD(&group
->handle_list
);
6413 group
->workers
= workers
+ i
* cnt
;
6415 for (j
= 0; j
< cnt
; j
++) {
6416 struct r5worker
*worker
= group
->workers
+ j
;
6417 worker
->group
= group
;
6418 INIT_WORK(&worker
->work
, raid5_do_work
);
6420 for (k
= 0; k
< NR_STRIPE_HASH_LOCKS
; k
++)
6421 INIT_LIST_HEAD(worker
->temp_inactive_list
+ k
);
6428 static void free_thread_groups(struct r5conf
*conf
)
6430 if (conf
->worker_groups
)
6431 kfree(conf
->worker_groups
[0].workers
);
6432 kfree(conf
->worker_groups
);
6433 conf
->worker_groups
= NULL
;
6437 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
6439 struct r5conf
*conf
= mddev
->private;
6442 sectors
= mddev
->dev_sectors
;
6444 /* size is defined by the smallest of previous and new size */
6445 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
6447 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
6448 sectors
&= ~((sector_t
)conf
->prev_chunk_sectors
- 1);
6449 return sectors
* (raid_disks
- conf
->max_degraded
);
6452 static void free_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6454 safe_put_page(percpu
->spare_page
);
6455 if (percpu
->scribble
)
6456 flex_array_free(percpu
->scribble
);
6457 percpu
->spare_page
= NULL
;
6458 percpu
->scribble
= NULL
;
6461 static int alloc_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6463 if (conf
->level
== 6 && !percpu
->spare_page
)
6464 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
6465 if (!percpu
->scribble
)
6466 percpu
->scribble
= scribble_alloc(max(conf
->raid_disks
,
6467 conf
->previous_raid_disks
),
6468 max(conf
->chunk_sectors
,
6469 conf
->prev_chunk_sectors
)
6473 if (!percpu
->scribble
|| (conf
->level
== 6 && !percpu
->spare_page
)) {
6474 free_scratch_buffer(conf
, percpu
);
6481 static int raid456_cpu_dead(unsigned int cpu
, struct hlist_node
*node
)
6483 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6485 free_scratch_buffer(conf
, per_cpu_ptr(conf
->percpu
, cpu
));
6489 static void raid5_free_percpu(struct r5conf
*conf
)
6494 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6495 free_percpu(conf
->percpu
);
6498 static void free_conf(struct r5conf
*conf
)
6503 r5l_exit_log(conf
->log
);
6504 if (conf
->shrinker
.nr_deferred
)
6505 unregister_shrinker(&conf
->shrinker
);
6507 free_thread_groups(conf
);
6508 shrink_stripes(conf
);
6509 raid5_free_percpu(conf
);
6510 for (i
= 0; i
< conf
->pool_size
; i
++)
6511 if (conf
->disks
[i
].extra_page
)
6512 put_page(conf
->disks
[i
].extra_page
);
6514 kfree(conf
->stripe_hashtbl
);
6518 static int raid456_cpu_up_prepare(unsigned int cpu
, struct hlist_node
*node
)
6520 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6521 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
6523 if (alloc_scratch_buffer(conf
, percpu
)) {
6524 pr_warn("%s: failed memory allocation for cpu%u\n",
6531 static int raid5_alloc_percpu(struct r5conf
*conf
)
6535 conf
->percpu
= alloc_percpu(struct raid5_percpu
);
6539 err
= cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6541 conf
->scribble_disks
= max(conf
->raid_disks
,
6542 conf
->previous_raid_disks
);
6543 conf
->scribble_sectors
= max(conf
->chunk_sectors
,
6544 conf
->prev_chunk_sectors
);
6549 static unsigned long raid5_cache_scan(struct shrinker
*shrink
,
6550 struct shrink_control
*sc
)
6552 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6553 unsigned long ret
= SHRINK_STOP
;
6555 if (mutex_trylock(&conf
->cache_size_mutex
)) {
6557 while (ret
< sc
->nr_to_scan
&&
6558 conf
->max_nr_stripes
> conf
->min_nr_stripes
) {
6559 if (drop_one_stripe(conf
) == 0) {
6565 mutex_unlock(&conf
->cache_size_mutex
);
6570 static unsigned long raid5_cache_count(struct shrinker
*shrink
,
6571 struct shrink_control
*sc
)
6573 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6575 if (conf
->max_nr_stripes
< conf
->min_nr_stripes
)
6576 /* unlikely, but not impossible */
6578 return conf
->max_nr_stripes
- conf
->min_nr_stripes
;
6581 static struct r5conf
*setup_conf(struct mddev
*mddev
)
6583 struct r5conf
*conf
;
6584 int raid_disk
, memory
, max_disks
;
6585 struct md_rdev
*rdev
;
6586 struct disk_info
*disk
;
6589 int group_cnt
, worker_cnt_per_group
;
6590 struct r5worker_group
*new_group
;
6592 if (mddev
->new_level
!= 5
6593 && mddev
->new_level
!= 4
6594 && mddev
->new_level
!= 6) {
6595 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6596 mdname(mddev
), mddev
->new_level
);
6597 return ERR_PTR(-EIO
);
6599 if ((mddev
->new_level
== 5
6600 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
6601 (mddev
->new_level
== 6
6602 && !algorithm_valid_raid6(mddev
->new_layout
))) {
6603 pr_warn("md/raid:%s: layout %d not supported\n",
6604 mdname(mddev
), mddev
->new_layout
);
6605 return ERR_PTR(-EIO
);
6607 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
6608 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6609 mdname(mddev
), mddev
->raid_disks
);
6610 return ERR_PTR(-EINVAL
);
6613 if (!mddev
->new_chunk_sectors
||
6614 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
6615 !is_power_of_2(mddev
->new_chunk_sectors
)) {
6616 pr_warn("md/raid:%s: invalid chunk size %d\n",
6617 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
6618 return ERR_PTR(-EINVAL
);
6621 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
6624 /* Don't enable multi-threading by default*/
6625 if (!alloc_thread_groups(conf
, 0, &group_cnt
, &worker_cnt_per_group
,
6627 conf
->group_cnt
= group_cnt
;
6628 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6629 conf
->worker_groups
= new_group
;
6632 spin_lock_init(&conf
->device_lock
);
6633 seqcount_init(&conf
->gen_lock
);
6634 mutex_init(&conf
->cache_size_mutex
);
6635 init_waitqueue_head(&conf
->wait_for_quiescent
);
6636 init_waitqueue_head(&conf
->wait_for_stripe
);
6637 init_waitqueue_head(&conf
->wait_for_overlap
);
6638 INIT_LIST_HEAD(&conf
->handle_list
);
6639 INIT_LIST_HEAD(&conf
->hold_list
);
6640 INIT_LIST_HEAD(&conf
->delayed_list
);
6641 INIT_LIST_HEAD(&conf
->bitmap_list
);
6642 bio_list_init(&conf
->return_bi
);
6643 init_llist_head(&conf
->released_stripes
);
6644 atomic_set(&conf
->active_stripes
, 0);
6645 atomic_set(&conf
->preread_active_stripes
, 0);
6646 atomic_set(&conf
->active_aligned_reads
, 0);
6647 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
6648 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
6650 conf
->raid_disks
= mddev
->raid_disks
;
6651 if (mddev
->reshape_position
== MaxSector
)
6652 conf
->previous_raid_disks
= mddev
->raid_disks
;
6654 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6655 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
6657 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
6663 for (i
= 0; i
< max_disks
; i
++) {
6664 conf
->disks
[i
].extra_page
= alloc_page(GFP_KERNEL
);
6665 if (!conf
->disks
[i
].extra_page
)
6669 conf
->mddev
= mddev
;
6671 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
6674 /* We init hash_locks[0] separately to that it can be used
6675 * as the reference lock in the spin_lock_nest_lock() call
6676 * in lock_all_device_hash_locks_irq in order to convince
6677 * lockdep that we know what we are doing.
6679 spin_lock_init(conf
->hash_locks
);
6680 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6681 spin_lock_init(conf
->hash_locks
+ i
);
6683 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6684 INIT_LIST_HEAD(conf
->inactive_list
+ i
);
6686 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6687 INIT_LIST_HEAD(conf
->temp_inactive_list
+ i
);
6689 atomic_set(&conf
->r5c_cached_full_stripes
, 0);
6690 INIT_LIST_HEAD(&conf
->r5c_full_stripe_list
);
6691 atomic_set(&conf
->r5c_cached_partial_stripes
, 0);
6692 INIT_LIST_HEAD(&conf
->r5c_partial_stripe_list
);
6694 conf
->level
= mddev
->new_level
;
6695 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
6696 if (raid5_alloc_percpu(conf
) != 0)
6699 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
6701 rdev_for_each(rdev
, mddev
) {
6702 raid_disk
= rdev
->raid_disk
;
6703 if (raid_disk
>= max_disks
6704 || raid_disk
< 0 || test_bit(Journal
, &rdev
->flags
))
6706 disk
= conf
->disks
+ raid_disk
;
6708 if (test_bit(Replacement
, &rdev
->flags
)) {
6709 if (disk
->replacement
)
6711 disk
->replacement
= rdev
;
6718 if (test_bit(In_sync
, &rdev
->flags
)) {
6719 char b
[BDEVNAME_SIZE
];
6720 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6721 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
6722 } else if (rdev
->saved_raid_disk
!= raid_disk
)
6723 /* Cannot rely on bitmap to complete recovery */
6727 conf
->level
= mddev
->new_level
;
6728 if (conf
->level
== 6) {
6729 conf
->max_degraded
= 2;
6730 if (raid6_call
.xor_syndrome
)
6731 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6733 conf
->rmw_level
= PARITY_DISABLE_RMW
;
6735 conf
->max_degraded
= 1;
6736 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6738 conf
->algorithm
= mddev
->new_layout
;
6739 conf
->reshape_progress
= mddev
->reshape_position
;
6740 if (conf
->reshape_progress
!= MaxSector
) {
6741 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
6742 conf
->prev_algo
= mddev
->layout
;
6744 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
6745 conf
->prev_algo
= conf
->algorithm
;
6748 conf
->min_nr_stripes
= NR_STRIPES
;
6749 if (mddev
->reshape_position
!= MaxSector
) {
6750 int stripes
= max_t(int,
6751 ((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4,
6752 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4);
6753 conf
->min_nr_stripes
= max(NR_STRIPES
, stripes
);
6754 if (conf
->min_nr_stripes
!= NR_STRIPES
)
6755 pr_info("md/raid:%s: force stripe size %d for reshape\n",
6756 mdname(mddev
), conf
->min_nr_stripes
);
6758 memory
= conf
->min_nr_stripes
* (sizeof(struct stripe_head
) +
6759 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
6760 atomic_set(&conf
->empty_inactive_list_nr
, NR_STRIPE_HASH_LOCKS
);
6761 if (grow_stripes(conf
, conf
->min_nr_stripes
)) {
6762 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6763 mdname(mddev
), memory
);
6766 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev
), memory
);
6768 * Losing a stripe head costs more than the time to refill it,
6769 * it reduces the queue depth and so can hurt throughput.
6770 * So set it rather large, scaled by number of devices.
6772 conf
->shrinker
.seeks
= DEFAULT_SEEKS
* conf
->raid_disks
* 4;
6773 conf
->shrinker
.scan_objects
= raid5_cache_scan
;
6774 conf
->shrinker
.count_objects
= raid5_cache_count
;
6775 conf
->shrinker
.batch
= 128;
6776 conf
->shrinker
.flags
= 0;
6777 if (register_shrinker(&conf
->shrinker
)) {
6778 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6783 sprintf(pers_name
, "raid%d", mddev
->new_level
);
6784 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
6785 if (!conf
->thread
) {
6786 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6796 return ERR_PTR(-EIO
);
6798 return ERR_PTR(-ENOMEM
);
6801 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
6804 case ALGORITHM_PARITY_0
:
6805 if (raid_disk
< max_degraded
)
6808 case ALGORITHM_PARITY_N
:
6809 if (raid_disk
>= raid_disks
- max_degraded
)
6812 case ALGORITHM_PARITY_0_6
:
6813 if (raid_disk
== 0 ||
6814 raid_disk
== raid_disks
- 1)
6817 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6818 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6819 case ALGORITHM_LEFT_SYMMETRIC_6
:
6820 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6821 if (raid_disk
== raid_disks
- 1)
6827 static int raid5_run(struct mddev
*mddev
)
6829 struct r5conf
*conf
;
6830 int working_disks
= 0;
6831 int dirty_parity_disks
= 0;
6832 struct md_rdev
*rdev
;
6833 struct md_rdev
*journal_dev
= NULL
;
6834 sector_t reshape_offset
= 0;
6836 long long min_offset_diff
= 0;
6839 if (mddev
->recovery_cp
!= MaxSector
)
6840 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6843 rdev_for_each(rdev
, mddev
) {
6846 if (test_bit(Journal
, &rdev
->flags
)) {
6850 if (rdev
->raid_disk
< 0)
6852 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
6854 min_offset_diff
= diff
;
6856 } else if (mddev
->reshape_backwards
&&
6857 diff
< min_offset_diff
)
6858 min_offset_diff
= diff
;
6859 else if (!mddev
->reshape_backwards
&&
6860 diff
> min_offset_diff
)
6861 min_offset_diff
= diff
;
6864 if (mddev
->reshape_position
!= MaxSector
) {
6865 /* Check that we can continue the reshape.
6866 * Difficulties arise if the stripe we would write to
6867 * next is at or after the stripe we would read from next.
6868 * For a reshape that changes the number of devices, this
6869 * is only possible for a very short time, and mdadm makes
6870 * sure that time appears to have past before assembling
6871 * the array. So we fail if that time hasn't passed.
6872 * For a reshape that keeps the number of devices the same
6873 * mdadm must be monitoring the reshape can keeping the
6874 * critical areas read-only and backed up. It will start
6875 * the array in read-only mode, so we check for that.
6877 sector_t here_new
, here_old
;
6879 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
6884 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
6889 if (mddev
->new_level
!= mddev
->level
) {
6890 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
6894 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6895 /* reshape_position must be on a new-stripe boundary, and one
6896 * further up in new geometry must map after here in old
6898 * If the chunk sizes are different, then as we perform reshape
6899 * in units of the largest of the two, reshape_position needs
6900 * be a multiple of the largest chunk size times new data disks.
6902 here_new
= mddev
->reshape_position
;
6903 chunk_sectors
= max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
);
6904 new_data_disks
= mddev
->raid_disks
- max_degraded
;
6905 if (sector_div(here_new
, chunk_sectors
* new_data_disks
)) {
6906 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
6910 reshape_offset
= here_new
* chunk_sectors
;
6911 /* here_new is the stripe we will write to */
6912 here_old
= mddev
->reshape_position
;
6913 sector_div(here_old
, chunk_sectors
* (old_disks
-max_degraded
));
6914 /* here_old is the first stripe that we might need to read
6916 if (mddev
->delta_disks
== 0) {
6917 /* We cannot be sure it is safe to start an in-place
6918 * reshape. It is only safe if user-space is monitoring
6919 * and taking constant backups.
6920 * mdadm always starts a situation like this in
6921 * readonly mode so it can take control before
6922 * allowing any writes. So just check for that.
6924 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
6925 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
6926 /* not really in-place - so OK */;
6927 else if (mddev
->ro
== 0) {
6928 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
6932 } else if (mddev
->reshape_backwards
6933 ? (here_new
* chunk_sectors
+ min_offset_diff
<=
6934 here_old
* chunk_sectors
)
6935 : (here_new
* chunk_sectors
>=
6936 here_old
* chunk_sectors
+ (-min_offset_diff
))) {
6937 /* Reading from the same stripe as writing to - bad */
6938 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
6942 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev
));
6943 /* OK, we should be able to continue; */
6945 BUG_ON(mddev
->level
!= mddev
->new_level
);
6946 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
6947 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
6948 BUG_ON(mddev
->delta_disks
!= 0);
6951 if (mddev
->private == NULL
)
6952 conf
= setup_conf(mddev
);
6954 conf
= mddev
->private;
6957 return PTR_ERR(conf
);
6959 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
6961 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
6964 set_disk_ro(mddev
->gendisk
, 1);
6965 } else if (mddev
->recovery_cp
== MaxSector
)
6966 set_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
6969 conf
->min_offset_diff
= min_offset_diff
;
6970 mddev
->thread
= conf
->thread
;
6971 conf
->thread
= NULL
;
6972 mddev
->private = conf
;
6974 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
6976 rdev
= conf
->disks
[i
].rdev
;
6977 if (!rdev
&& conf
->disks
[i
].replacement
) {
6978 /* The replacement is all we have yet */
6979 rdev
= conf
->disks
[i
].replacement
;
6980 conf
->disks
[i
].replacement
= NULL
;
6981 clear_bit(Replacement
, &rdev
->flags
);
6982 conf
->disks
[i
].rdev
= rdev
;
6986 if (conf
->disks
[i
].replacement
&&
6987 conf
->reshape_progress
!= MaxSector
) {
6988 /* replacements and reshape simply do not mix. */
6989 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
6992 if (test_bit(In_sync
, &rdev
->flags
)) {
6996 /* This disc is not fully in-sync. However if it
6997 * just stored parity (beyond the recovery_offset),
6998 * when we don't need to be concerned about the
6999 * array being dirty.
7000 * When reshape goes 'backwards', we never have
7001 * partially completed devices, so we only need
7002 * to worry about reshape going forwards.
7004 /* Hack because v0.91 doesn't store recovery_offset properly. */
7005 if (mddev
->major_version
== 0 &&
7006 mddev
->minor_version
> 90)
7007 rdev
->recovery_offset
= reshape_offset
;
7009 if (rdev
->recovery_offset
< reshape_offset
) {
7010 /* We need to check old and new layout */
7011 if (!only_parity(rdev
->raid_disk
,
7014 conf
->max_degraded
))
7017 if (!only_parity(rdev
->raid_disk
,
7019 conf
->previous_raid_disks
,
7020 conf
->max_degraded
))
7022 dirty_parity_disks
++;
7026 * 0 for a fully functional array, 1 or 2 for a degraded array.
7028 mddev
->degraded
= calc_degraded(conf
);
7030 if (has_failed(conf
)) {
7031 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
7032 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
7036 /* device size must be a multiple of chunk size */
7037 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
7038 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
7040 if (mddev
->degraded
> dirty_parity_disks
&&
7041 mddev
->recovery_cp
!= MaxSector
) {
7042 if (mddev
->ok_start_degraded
)
7043 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7046 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7052 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7053 mdname(mddev
), conf
->level
,
7054 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
7057 print_raid5_conf(conf
);
7059 if (conf
->reshape_progress
!= MaxSector
) {
7060 conf
->reshape_safe
= conf
->reshape_progress
;
7061 atomic_set(&conf
->reshape_stripes
, 0);
7062 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7063 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7064 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7065 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7066 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7070 /* Ok, everything is just fine now */
7071 if (mddev
->to_remove
== &raid5_attrs_group
)
7072 mddev
->to_remove
= NULL
;
7073 else if (mddev
->kobj
.sd
&&
7074 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
7075 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7077 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7081 bool discard_supported
= true;
7082 /* read-ahead size must cover two whole stripes, which
7083 * is 2 * (datadisks) * chunksize where 'n' is the
7084 * number of raid devices
7086 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
7087 int stripe
= data_disks
*
7088 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
7089 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
7090 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
7092 chunk_size
= mddev
->chunk_sectors
<< 9;
7093 blk_queue_io_min(mddev
->queue
, chunk_size
);
7094 blk_queue_io_opt(mddev
->queue
, chunk_size
*
7095 (conf
->raid_disks
- conf
->max_degraded
));
7096 mddev
->queue
->limits
.raid_partial_stripes_expensive
= 1;
7098 * We can only discard a whole stripe. It doesn't make sense to
7099 * discard data disk but write parity disk
7101 stripe
= stripe
* PAGE_SIZE
;
7102 /* Round up to power of 2, as discard handling
7103 * currently assumes that */
7104 while ((stripe
-1) & stripe
)
7105 stripe
= (stripe
| (stripe
-1)) + 1;
7106 mddev
->queue
->limits
.discard_alignment
= stripe
;
7107 mddev
->queue
->limits
.discard_granularity
= stripe
;
7110 * We use 16-bit counter of active stripes in bi_phys_segments
7111 * (minus one for over-loaded initialization)
7113 blk_queue_max_hw_sectors(mddev
->queue
, 0xfffe * STRIPE_SECTORS
);
7114 blk_queue_max_discard_sectors(mddev
->queue
,
7115 0xfffe * STRIPE_SECTORS
);
7118 * unaligned part of discard request will be ignored, so can't
7119 * guarantee discard_zeroes_data
7121 mddev
->queue
->limits
.discard_zeroes_data
= 0;
7123 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
7125 rdev_for_each(rdev
, mddev
) {
7126 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7127 rdev
->data_offset
<< 9);
7128 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7129 rdev
->new_data_offset
<< 9);
7131 * discard_zeroes_data is required, otherwise data
7132 * could be lost. Consider a scenario: discard a stripe
7133 * (the stripe could be inconsistent if
7134 * discard_zeroes_data is 0); write one disk of the
7135 * stripe (the stripe could be inconsistent again
7136 * depending on which disks are used to calculate
7137 * parity); the disk is broken; The stripe data of this
7140 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
7141 !bdev_get_queue(rdev
->bdev
)->
7142 limits
.discard_zeroes_data
)
7143 discard_supported
= false;
7144 /* Unfortunately, discard_zeroes_data is not currently
7145 * a guarantee - just a hint. So we only allow DISCARD
7146 * if the sysadmin has confirmed that only safe devices
7147 * are in use by setting a module parameter.
7149 if (!devices_handle_discard_safely
) {
7150 if (discard_supported
) {
7151 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7152 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7154 discard_supported
= false;
7158 if (discard_supported
&&
7159 mddev
->queue
->limits
.max_discard_sectors
>= (stripe
>> 9) &&
7160 mddev
->queue
->limits
.discard_granularity
>= stripe
)
7161 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
7164 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
7167 blk_queue_max_hw_sectors(mddev
->queue
, UINT_MAX
);
7171 char b
[BDEVNAME_SIZE
];
7173 pr_debug("md/raid:%s: using device %s as journal\n",
7174 mdname(mddev
), bdevname(journal_dev
->bdev
, b
));
7175 if (r5l_init_log(conf
, journal_dev
))
7181 md_unregister_thread(&mddev
->thread
);
7182 print_raid5_conf(conf
);
7184 mddev
->private = NULL
;
7185 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev
));
7189 static void raid5_free(struct mddev
*mddev
, void *priv
)
7191 struct r5conf
*conf
= priv
;
7194 mddev
->to_remove
= &raid5_attrs_group
;
7197 static void raid5_status(struct seq_file
*seq
, struct mddev
*mddev
)
7199 struct r5conf
*conf
= mddev
->private;
7202 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
7203 conf
->chunk_sectors
/ 2, mddev
->layout
);
7204 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
7206 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7207 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
7208 seq_printf (seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
7211 seq_printf (seq
, "]");
7214 static void print_raid5_conf (struct r5conf
*conf
)
7217 struct disk_info
*tmp
;
7219 pr_debug("RAID conf printout:\n");
7221 pr_debug("(conf==NULL)\n");
7224 pr_debug(" --- level:%d rd:%d wd:%d\n", conf
->level
,
7226 conf
->raid_disks
- conf
->mddev
->degraded
);
7228 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7229 char b
[BDEVNAME_SIZE
];
7230 tmp
= conf
->disks
+ i
;
7232 pr_debug(" disk %d, o:%d, dev:%s\n",
7233 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
7234 bdevname(tmp
->rdev
->bdev
, b
));
7238 static int raid5_spare_active(struct mddev
*mddev
)
7241 struct r5conf
*conf
= mddev
->private;
7242 struct disk_info
*tmp
;
7244 unsigned long flags
;
7246 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7247 tmp
= conf
->disks
+ i
;
7248 if (tmp
->replacement
7249 && tmp
->replacement
->recovery_offset
== MaxSector
7250 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
7251 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
7252 /* Replacement has just become active. */
7254 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
7257 /* Replaced device not technically faulty,
7258 * but we need to be sure it gets removed
7259 * and never re-added.
7261 set_bit(Faulty
, &tmp
->rdev
->flags
);
7262 sysfs_notify_dirent_safe(
7263 tmp
->rdev
->sysfs_state
);
7265 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
7266 } else if (tmp
->rdev
7267 && tmp
->rdev
->recovery_offset
== MaxSector
7268 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
7269 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
7271 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
7274 spin_lock_irqsave(&conf
->device_lock
, flags
);
7275 mddev
->degraded
= calc_degraded(conf
);
7276 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7277 print_raid5_conf(conf
);
7281 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7283 struct r5conf
*conf
= mddev
->private;
7285 int number
= rdev
->raid_disk
;
7286 struct md_rdev
**rdevp
;
7287 struct disk_info
*p
= conf
->disks
+ number
;
7289 print_raid5_conf(conf
);
7290 if (test_bit(Journal
, &rdev
->flags
) && conf
->log
) {
7291 struct r5l_log
*log
;
7293 * we can't wait pending write here, as this is called in
7294 * raid5d, wait will deadlock.
7296 if (atomic_read(&mddev
->writes_pending
))
7304 if (rdev
== p
->rdev
)
7306 else if (rdev
== p
->replacement
)
7307 rdevp
= &p
->replacement
;
7311 if (number
>= conf
->raid_disks
&&
7312 conf
->reshape_progress
== MaxSector
)
7313 clear_bit(In_sync
, &rdev
->flags
);
7315 if (test_bit(In_sync
, &rdev
->flags
) ||
7316 atomic_read(&rdev
->nr_pending
)) {
7320 /* Only remove non-faulty devices if recovery
7323 if (!test_bit(Faulty
, &rdev
->flags
) &&
7324 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
7325 !has_failed(conf
) &&
7326 (!p
->replacement
|| p
->replacement
== rdev
) &&
7327 number
< conf
->raid_disks
) {
7332 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
7334 if (atomic_read(&rdev
->nr_pending
)) {
7335 /* lost the race, try later */
7340 if (p
->replacement
) {
7341 /* We must have just cleared 'rdev' */
7342 p
->rdev
= p
->replacement
;
7343 clear_bit(Replacement
, &p
->replacement
->flags
);
7344 smp_mb(); /* Make sure other CPUs may see both as identical
7345 * but will never see neither - if they are careful
7347 p
->replacement
= NULL
;
7348 clear_bit(WantReplacement
, &rdev
->flags
);
7350 /* We might have just removed the Replacement as faulty-
7351 * clear the bit just in case
7353 clear_bit(WantReplacement
, &rdev
->flags
);
7356 print_raid5_conf(conf
);
7360 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7362 struct r5conf
*conf
= mddev
->private;
7365 struct disk_info
*p
;
7367 int last
= conf
->raid_disks
- 1;
7369 if (test_bit(Journal
, &rdev
->flags
)) {
7370 char b
[BDEVNAME_SIZE
];
7374 rdev
->raid_disk
= 0;
7376 * The array is in readonly mode if journal is missing, so no
7377 * write requests running. We should be safe
7379 r5l_init_log(conf
, rdev
);
7380 pr_debug("md/raid:%s: using device %s as journal\n",
7381 mdname(mddev
), bdevname(rdev
->bdev
, b
));
7384 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
7387 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
7388 /* no point adding a device */
7391 if (rdev
->raid_disk
>= 0)
7392 first
= last
= rdev
->raid_disk
;
7395 * find the disk ... but prefer rdev->saved_raid_disk
7398 if (rdev
->saved_raid_disk
>= 0 &&
7399 rdev
->saved_raid_disk
>= first
&&
7400 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
7401 first
= rdev
->saved_raid_disk
;
7403 for (disk
= first
; disk
<= last
; disk
++) {
7404 p
= conf
->disks
+ disk
;
7405 if (p
->rdev
== NULL
) {
7406 clear_bit(In_sync
, &rdev
->flags
);
7407 rdev
->raid_disk
= disk
;
7409 if (rdev
->saved_raid_disk
!= disk
)
7411 rcu_assign_pointer(p
->rdev
, rdev
);
7415 for (disk
= first
; disk
<= last
; disk
++) {
7416 p
= conf
->disks
+ disk
;
7417 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
7418 p
->replacement
== NULL
) {
7419 clear_bit(In_sync
, &rdev
->flags
);
7420 set_bit(Replacement
, &rdev
->flags
);
7421 rdev
->raid_disk
= disk
;
7424 rcu_assign_pointer(p
->replacement
, rdev
);
7429 print_raid5_conf(conf
);
7433 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
7435 /* no resync is happening, and there is enough space
7436 * on all devices, so we can resize.
7437 * We need to make sure resync covers any new space.
7438 * If the array is shrinking we should possibly wait until
7439 * any io in the removed space completes, but it hardly seems
7443 struct r5conf
*conf
= mddev
->private;
7447 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
7448 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
7449 if (mddev
->external_size
&&
7450 mddev
->array_sectors
> newsize
)
7452 if (mddev
->bitmap
) {
7453 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
7457 md_set_array_sectors(mddev
, newsize
);
7458 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7459 revalidate_disk(mddev
->gendisk
);
7460 if (sectors
> mddev
->dev_sectors
&&
7461 mddev
->recovery_cp
> mddev
->dev_sectors
) {
7462 mddev
->recovery_cp
= mddev
->dev_sectors
;
7463 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7465 mddev
->dev_sectors
= sectors
;
7466 mddev
->resync_max_sectors
= sectors
;
7470 static int check_stripe_cache(struct mddev
*mddev
)
7472 /* Can only proceed if there are plenty of stripe_heads.
7473 * We need a minimum of one full stripe,, and for sensible progress
7474 * it is best to have about 4 times that.
7475 * If we require 4 times, then the default 256 4K stripe_heads will
7476 * allow for chunk sizes up to 256K, which is probably OK.
7477 * If the chunk size is greater, user-space should request more
7478 * stripe_heads first.
7480 struct r5conf
*conf
= mddev
->private;
7481 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7482 > conf
->min_nr_stripes
||
7483 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7484 > conf
->min_nr_stripes
) {
7485 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7487 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
7494 static int check_reshape(struct mddev
*mddev
)
7496 struct r5conf
*conf
= mddev
->private;
7500 if (mddev
->delta_disks
== 0 &&
7501 mddev
->new_layout
== mddev
->layout
&&
7502 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
7503 return 0; /* nothing to do */
7504 if (has_failed(conf
))
7506 if (mddev
->delta_disks
< 0 && mddev
->reshape_position
== MaxSector
) {
7507 /* We might be able to shrink, but the devices must
7508 * be made bigger first.
7509 * For raid6, 4 is the minimum size.
7510 * Otherwise 2 is the minimum
7513 if (mddev
->level
== 6)
7515 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
7519 if (!check_stripe_cache(mddev
))
7522 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
||
7523 mddev
->delta_disks
> 0)
7524 if (resize_chunks(conf
,
7525 conf
->previous_raid_disks
7526 + max(0, mddev
->delta_disks
),
7527 max(mddev
->new_chunk_sectors
,
7528 mddev
->chunk_sectors
)
7531 return resize_stripes(conf
, (conf
->previous_raid_disks
7532 + mddev
->delta_disks
));
7535 static int raid5_start_reshape(struct mddev
*mddev
)
7537 struct r5conf
*conf
= mddev
->private;
7538 struct md_rdev
*rdev
;
7540 unsigned long flags
;
7542 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
7545 if (!check_stripe_cache(mddev
))
7548 if (has_failed(conf
))
7551 rdev_for_each(rdev
, mddev
) {
7552 if (!test_bit(In_sync
, &rdev
->flags
)
7553 && !test_bit(Faulty
, &rdev
->flags
))
7557 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
7558 /* Not enough devices even to make a degraded array
7563 /* Refuse to reduce size of the array. Any reductions in
7564 * array size must be through explicit setting of array_size
7567 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
7568 < mddev
->array_sectors
) {
7569 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7574 atomic_set(&conf
->reshape_stripes
, 0);
7575 spin_lock_irq(&conf
->device_lock
);
7576 write_seqcount_begin(&conf
->gen_lock
);
7577 conf
->previous_raid_disks
= conf
->raid_disks
;
7578 conf
->raid_disks
+= mddev
->delta_disks
;
7579 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
7580 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
7581 conf
->prev_algo
= conf
->algorithm
;
7582 conf
->algorithm
= mddev
->new_layout
;
7584 /* Code that selects data_offset needs to see the generation update
7585 * if reshape_progress has been set - so a memory barrier needed.
7588 if (mddev
->reshape_backwards
)
7589 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
7591 conf
->reshape_progress
= 0;
7592 conf
->reshape_safe
= conf
->reshape_progress
;
7593 write_seqcount_end(&conf
->gen_lock
);
7594 spin_unlock_irq(&conf
->device_lock
);
7596 /* Now make sure any requests that proceeded on the assumption
7597 * the reshape wasn't running - like Discard or Read - have
7600 mddev_suspend(mddev
);
7601 mddev_resume(mddev
);
7603 /* Add some new drives, as many as will fit.
7604 * We know there are enough to make the newly sized array work.
7605 * Don't add devices if we are reducing the number of
7606 * devices in the array. This is because it is not possible
7607 * to correctly record the "partially reconstructed" state of
7608 * such devices during the reshape and confusion could result.
7610 if (mddev
->delta_disks
>= 0) {
7611 rdev_for_each(rdev
, mddev
)
7612 if (rdev
->raid_disk
< 0 &&
7613 !test_bit(Faulty
, &rdev
->flags
)) {
7614 if (raid5_add_disk(mddev
, rdev
) == 0) {
7616 >= conf
->previous_raid_disks
)
7617 set_bit(In_sync
, &rdev
->flags
);
7619 rdev
->recovery_offset
= 0;
7621 if (sysfs_link_rdev(mddev
, rdev
))
7622 /* Failure here is OK */;
7624 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
7625 && !test_bit(Faulty
, &rdev
->flags
)) {
7626 /* This is a spare that was manually added */
7627 set_bit(In_sync
, &rdev
->flags
);
7630 /* When a reshape changes the number of devices,
7631 * ->degraded is measured against the larger of the
7632 * pre and post number of devices.
7634 spin_lock_irqsave(&conf
->device_lock
, flags
);
7635 mddev
->degraded
= calc_degraded(conf
);
7636 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7638 mddev
->raid_disks
= conf
->raid_disks
;
7639 mddev
->reshape_position
= conf
->reshape_progress
;
7640 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7642 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7643 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7644 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7645 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7646 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7647 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7649 if (!mddev
->sync_thread
) {
7650 mddev
->recovery
= 0;
7651 spin_lock_irq(&conf
->device_lock
);
7652 write_seqcount_begin(&conf
->gen_lock
);
7653 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
7654 mddev
->new_chunk_sectors
=
7655 conf
->chunk_sectors
= conf
->prev_chunk_sectors
;
7656 mddev
->new_layout
= conf
->algorithm
= conf
->prev_algo
;
7657 rdev_for_each(rdev
, mddev
)
7658 rdev
->new_data_offset
= rdev
->data_offset
;
7660 conf
->generation
--;
7661 conf
->reshape_progress
= MaxSector
;
7662 mddev
->reshape_position
= MaxSector
;
7663 write_seqcount_end(&conf
->gen_lock
);
7664 spin_unlock_irq(&conf
->device_lock
);
7667 conf
->reshape_checkpoint
= jiffies
;
7668 md_wakeup_thread(mddev
->sync_thread
);
7669 md_new_event(mddev
);
7673 /* This is called from the reshape thread and should make any
7674 * changes needed in 'conf'
7676 static void end_reshape(struct r5conf
*conf
)
7679 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
7680 struct md_rdev
*rdev
;
7682 spin_lock_irq(&conf
->device_lock
);
7683 conf
->previous_raid_disks
= conf
->raid_disks
;
7684 rdev_for_each(rdev
, conf
->mddev
)
7685 rdev
->data_offset
= rdev
->new_data_offset
;
7687 conf
->reshape_progress
= MaxSector
;
7688 conf
->mddev
->reshape_position
= MaxSector
;
7689 spin_unlock_irq(&conf
->device_lock
);
7690 wake_up(&conf
->wait_for_overlap
);
7692 /* read-ahead size must cover two whole stripes, which is
7693 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7695 if (conf
->mddev
->queue
) {
7696 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
7697 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
7699 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
7700 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
7705 /* This is called from the raid5d thread with mddev_lock held.
7706 * It makes config changes to the device.
7708 static void raid5_finish_reshape(struct mddev
*mddev
)
7710 struct r5conf
*conf
= mddev
->private;
7712 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7714 if (mddev
->delta_disks
> 0) {
7715 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7717 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7718 revalidate_disk(mddev
->gendisk
);
7722 spin_lock_irq(&conf
->device_lock
);
7723 mddev
->degraded
= calc_degraded(conf
);
7724 spin_unlock_irq(&conf
->device_lock
);
7725 for (d
= conf
->raid_disks
;
7726 d
< conf
->raid_disks
- mddev
->delta_disks
;
7728 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
7730 clear_bit(In_sync
, &rdev
->flags
);
7731 rdev
= conf
->disks
[d
].replacement
;
7733 clear_bit(In_sync
, &rdev
->flags
);
7736 mddev
->layout
= conf
->algorithm
;
7737 mddev
->chunk_sectors
= conf
->chunk_sectors
;
7738 mddev
->reshape_position
= MaxSector
;
7739 mddev
->delta_disks
= 0;
7740 mddev
->reshape_backwards
= 0;
7744 static void raid5_quiesce(struct mddev
*mddev
, int state
)
7746 struct r5conf
*conf
= mddev
->private;
7749 case 2: /* resume for a suspend */
7750 wake_up(&conf
->wait_for_overlap
);
7753 case 1: /* stop all writes */
7754 lock_all_device_hash_locks_irq(conf
);
7755 /* '2' tells resync/reshape to pause so that all
7756 * active stripes can drain
7758 r5c_flush_cache(conf
, INT_MAX
);
7760 wait_event_cmd(conf
->wait_for_quiescent
,
7761 atomic_read(&conf
->active_stripes
) == 0 &&
7762 atomic_read(&conf
->active_aligned_reads
) == 0,
7763 unlock_all_device_hash_locks_irq(conf
),
7764 lock_all_device_hash_locks_irq(conf
));
7766 unlock_all_device_hash_locks_irq(conf
);
7767 /* allow reshape to continue */
7768 wake_up(&conf
->wait_for_overlap
);
7771 case 0: /* re-enable writes */
7772 lock_all_device_hash_locks_irq(conf
);
7774 wake_up(&conf
->wait_for_quiescent
);
7775 wake_up(&conf
->wait_for_overlap
);
7776 unlock_all_device_hash_locks_irq(conf
);
7779 r5l_quiesce(conf
->log
, state
);
7782 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
7784 struct r0conf
*raid0_conf
= mddev
->private;
7787 /* for raid0 takeover only one zone is supported */
7788 if (raid0_conf
->nr_strip_zones
> 1) {
7789 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7791 return ERR_PTR(-EINVAL
);
7794 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
7795 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
7796 mddev
->dev_sectors
= sectors
;
7797 mddev
->new_level
= level
;
7798 mddev
->new_layout
= ALGORITHM_PARITY_N
;
7799 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7800 mddev
->raid_disks
+= 1;
7801 mddev
->delta_disks
= 1;
7802 /* make sure it will be not marked as dirty */
7803 mddev
->recovery_cp
= MaxSector
;
7805 return setup_conf(mddev
);
7808 static void *raid5_takeover_raid1(struct mddev
*mddev
)
7813 if (mddev
->raid_disks
!= 2 ||
7814 mddev
->degraded
> 1)
7815 return ERR_PTR(-EINVAL
);
7817 /* Should check if there are write-behind devices? */
7819 chunksect
= 64*2; /* 64K by default */
7821 /* The array must be an exact multiple of chunksize */
7822 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
7825 if ((chunksect
<<9) < STRIPE_SIZE
)
7826 /* array size does not allow a suitable chunk size */
7827 return ERR_PTR(-EINVAL
);
7829 mddev
->new_level
= 5;
7830 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7831 mddev
->new_chunk_sectors
= chunksect
;
7833 ret
= setup_conf(mddev
);
7835 mddev_clear_unsupported_flags(mddev
,
7836 UNSUPPORTED_MDDEV_FLAGS
);
7840 static void *raid5_takeover_raid6(struct mddev
*mddev
)
7844 switch (mddev
->layout
) {
7845 case ALGORITHM_LEFT_ASYMMETRIC_6
:
7846 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
7848 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
7849 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
7851 case ALGORITHM_LEFT_SYMMETRIC_6
:
7852 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7854 case ALGORITHM_RIGHT_SYMMETRIC_6
:
7855 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
7857 case ALGORITHM_PARITY_0_6
:
7858 new_layout
= ALGORITHM_PARITY_0
;
7860 case ALGORITHM_PARITY_N
:
7861 new_layout
= ALGORITHM_PARITY_N
;
7864 return ERR_PTR(-EINVAL
);
7866 mddev
->new_level
= 5;
7867 mddev
->new_layout
= new_layout
;
7868 mddev
->delta_disks
= -1;
7869 mddev
->raid_disks
-= 1;
7870 return setup_conf(mddev
);
7873 static int raid5_check_reshape(struct mddev
*mddev
)
7875 /* For a 2-drive array, the layout and chunk size can be changed
7876 * immediately as not restriping is needed.
7877 * For larger arrays we record the new value - after validation
7878 * to be used by a reshape pass.
7880 struct r5conf
*conf
= mddev
->private;
7881 int new_chunk
= mddev
->new_chunk_sectors
;
7883 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
7885 if (new_chunk
> 0) {
7886 if (!is_power_of_2(new_chunk
))
7888 if (new_chunk
< (PAGE_SIZE
>>9))
7890 if (mddev
->array_sectors
& (new_chunk
-1))
7891 /* not factor of array size */
7895 /* They look valid */
7897 if (mddev
->raid_disks
== 2) {
7898 /* can make the change immediately */
7899 if (mddev
->new_layout
>= 0) {
7900 conf
->algorithm
= mddev
->new_layout
;
7901 mddev
->layout
= mddev
->new_layout
;
7903 if (new_chunk
> 0) {
7904 conf
->chunk_sectors
= new_chunk
;
7905 mddev
->chunk_sectors
= new_chunk
;
7907 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7908 md_wakeup_thread(mddev
->thread
);
7910 return check_reshape(mddev
);
7913 static int raid6_check_reshape(struct mddev
*mddev
)
7915 int new_chunk
= mddev
->new_chunk_sectors
;
7917 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
7919 if (new_chunk
> 0) {
7920 if (!is_power_of_2(new_chunk
))
7922 if (new_chunk
< (PAGE_SIZE
>> 9))
7924 if (mddev
->array_sectors
& (new_chunk
-1))
7925 /* not factor of array size */
7929 /* They look valid */
7930 return check_reshape(mddev
);
7933 static void *raid5_takeover(struct mddev
*mddev
)
7935 /* raid5 can take over:
7936 * raid0 - if there is only one strip zone - make it a raid4 layout
7937 * raid1 - if there are two drives. We need to know the chunk size
7938 * raid4 - trivial - just use a raid4 layout.
7939 * raid6 - Providing it is a *_6 layout
7941 if (mddev
->level
== 0)
7942 return raid45_takeover_raid0(mddev
, 5);
7943 if (mddev
->level
== 1)
7944 return raid5_takeover_raid1(mddev
);
7945 if (mddev
->level
== 4) {
7946 mddev
->new_layout
= ALGORITHM_PARITY_N
;
7947 mddev
->new_level
= 5;
7948 return setup_conf(mddev
);
7950 if (mddev
->level
== 6)
7951 return raid5_takeover_raid6(mddev
);
7953 return ERR_PTR(-EINVAL
);
7956 static void *raid4_takeover(struct mddev
*mddev
)
7958 /* raid4 can take over:
7959 * raid0 - if there is only one strip zone
7960 * raid5 - if layout is right
7962 if (mddev
->level
== 0)
7963 return raid45_takeover_raid0(mddev
, 4);
7964 if (mddev
->level
== 5 &&
7965 mddev
->layout
== ALGORITHM_PARITY_N
) {
7966 mddev
->new_layout
= 0;
7967 mddev
->new_level
= 4;
7968 return setup_conf(mddev
);
7970 return ERR_PTR(-EINVAL
);
7973 static struct md_personality raid5_personality
;
7975 static void *raid6_takeover(struct mddev
*mddev
)
7977 /* Currently can only take over a raid5. We map the
7978 * personality to an equivalent raid6 personality
7979 * with the Q block at the end.
7983 if (mddev
->pers
!= &raid5_personality
)
7984 return ERR_PTR(-EINVAL
);
7985 if (mddev
->degraded
> 1)
7986 return ERR_PTR(-EINVAL
);
7987 if (mddev
->raid_disks
> 253)
7988 return ERR_PTR(-EINVAL
);
7989 if (mddev
->raid_disks
< 3)
7990 return ERR_PTR(-EINVAL
);
7992 switch (mddev
->layout
) {
7993 case ALGORITHM_LEFT_ASYMMETRIC
:
7994 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
7996 case ALGORITHM_RIGHT_ASYMMETRIC
:
7997 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
7999 case ALGORITHM_LEFT_SYMMETRIC
:
8000 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
8002 case ALGORITHM_RIGHT_SYMMETRIC
:
8003 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
8005 case ALGORITHM_PARITY_0
:
8006 new_layout
= ALGORITHM_PARITY_0_6
;
8008 case ALGORITHM_PARITY_N
:
8009 new_layout
= ALGORITHM_PARITY_N
;
8012 return ERR_PTR(-EINVAL
);
8014 mddev
->new_level
= 6;
8015 mddev
->new_layout
= new_layout
;
8016 mddev
->delta_disks
= 1;
8017 mddev
->raid_disks
+= 1;
8018 return setup_conf(mddev
);
8021 static struct md_personality raid6_personality
=
8025 .owner
= THIS_MODULE
,
8026 .make_request
= raid5_make_request
,
8029 .status
= raid5_status
,
8030 .error_handler
= raid5_error
,
8031 .hot_add_disk
= raid5_add_disk
,
8032 .hot_remove_disk
= raid5_remove_disk
,
8033 .spare_active
= raid5_spare_active
,
8034 .sync_request
= raid5_sync_request
,
8035 .resize
= raid5_resize
,
8037 .check_reshape
= raid6_check_reshape
,
8038 .start_reshape
= raid5_start_reshape
,
8039 .finish_reshape
= raid5_finish_reshape
,
8040 .quiesce
= raid5_quiesce
,
8041 .takeover
= raid6_takeover
,
8042 .congested
= raid5_congested
,
8044 static struct md_personality raid5_personality
=
8048 .owner
= THIS_MODULE
,
8049 .make_request
= raid5_make_request
,
8052 .status
= raid5_status
,
8053 .error_handler
= raid5_error
,
8054 .hot_add_disk
= raid5_add_disk
,
8055 .hot_remove_disk
= raid5_remove_disk
,
8056 .spare_active
= raid5_spare_active
,
8057 .sync_request
= raid5_sync_request
,
8058 .resize
= raid5_resize
,
8060 .check_reshape
= raid5_check_reshape
,
8061 .start_reshape
= raid5_start_reshape
,
8062 .finish_reshape
= raid5_finish_reshape
,
8063 .quiesce
= raid5_quiesce
,
8064 .takeover
= raid5_takeover
,
8065 .congested
= raid5_congested
,
8068 static struct md_personality raid4_personality
=
8072 .owner
= THIS_MODULE
,
8073 .make_request
= raid5_make_request
,
8076 .status
= raid5_status
,
8077 .error_handler
= raid5_error
,
8078 .hot_add_disk
= raid5_add_disk
,
8079 .hot_remove_disk
= raid5_remove_disk
,
8080 .spare_active
= raid5_spare_active
,
8081 .sync_request
= raid5_sync_request
,
8082 .resize
= raid5_resize
,
8084 .check_reshape
= raid5_check_reshape
,
8085 .start_reshape
= raid5_start_reshape
,
8086 .finish_reshape
= raid5_finish_reshape
,
8087 .quiesce
= raid5_quiesce
,
8088 .takeover
= raid4_takeover
,
8089 .congested
= raid5_congested
,
8092 static int __init
raid5_init(void)
8096 raid5_wq
= alloc_workqueue("raid5wq",
8097 WQ_UNBOUND
|WQ_MEM_RECLAIM
|WQ_CPU_INTENSIVE
|WQ_SYSFS
, 0);
8101 ret
= cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE
,
8103 raid456_cpu_up_prepare
,
8106 destroy_workqueue(raid5_wq
);
8109 register_md_personality(&raid6_personality
);
8110 register_md_personality(&raid5_personality
);
8111 register_md_personality(&raid4_personality
);
8115 static void raid5_exit(void)
8117 unregister_md_personality(&raid6_personality
);
8118 unregister_md_personality(&raid5_personality
);
8119 unregister_md_personality(&raid4_personality
);
8120 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE
);
8121 destroy_workqueue(raid5_wq
);
8124 module_init(raid5_init
);
8125 module_exit(raid5_exit
);
8126 MODULE_LICENSE("GPL");
8127 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
8128 MODULE_ALIAS("md-personality-4"); /* RAID5 */
8129 MODULE_ALIAS("md-raid5");
8130 MODULE_ALIAS("md-raid4");
8131 MODULE_ALIAS("md-level-5");
8132 MODULE_ALIAS("md-level-4");
8133 MODULE_ALIAS("md-personality-8"); /* RAID6 */
8134 MODULE_ALIAS("md-raid6");
8135 MODULE_ALIAS("md-level-6");
8137 /* This used to be two separate modules, they were: */
8138 MODULE_ALIAS("raid5");
8139 MODULE_ALIAS("raid6");