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 * STRIPE_R5C_PARTIAL_STRIPE is set in
287 * r5c_try_caching_write(). No need to
290 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
, *t
;
358 struct llist_node
*head
;
360 head
= llist_del_all(&conf
->released_stripes
);
361 head
= llist_reverse_order(head
);
362 llist_for_each_entry_safe(sh
, t
, head
, release_list
) {
365 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
367 clear_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
);
369 * Don't worry the bit is set here, because if the bit is set
370 * again, the count is always > 1. This is true for
371 * STRIPE_ON_UNPLUG_LIST bit too.
373 hash
= sh
->hash_lock_index
;
374 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
381 void raid5_release_stripe(struct stripe_head
*sh
)
383 struct r5conf
*conf
= sh
->raid_conf
;
385 struct list_head list
;
389 /* Avoid release_list until the last reference.
391 if (atomic_add_unless(&sh
->count
, -1, 1))
394 if (unlikely(!conf
->mddev
->thread
) ||
395 test_and_set_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
))
397 wakeup
= llist_add(&sh
->release_list
, &conf
->released_stripes
);
399 md_wakeup_thread(conf
->mddev
->thread
);
402 local_irq_save(flags
);
403 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
404 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
405 INIT_LIST_HEAD(&list
);
406 hash
= sh
->hash_lock_index
;
407 do_release_stripe(conf
, sh
, &list
);
408 spin_unlock(&conf
->device_lock
);
409 release_inactive_stripe_list(conf
, &list
, hash
);
411 local_irq_restore(flags
);
414 static inline void remove_hash(struct stripe_head
*sh
)
416 pr_debug("remove_hash(), stripe %llu\n",
417 (unsigned long long)sh
->sector
);
419 hlist_del_init(&sh
->hash
);
422 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
424 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
426 pr_debug("insert_hash(), stripe %llu\n",
427 (unsigned long long)sh
->sector
);
429 hlist_add_head(&sh
->hash
, hp
);
432 /* find an idle stripe, make sure it is unhashed, and return it. */
433 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
, int hash
)
435 struct stripe_head
*sh
= NULL
;
436 struct list_head
*first
;
438 if (list_empty(conf
->inactive_list
+ hash
))
440 first
= (conf
->inactive_list
+ hash
)->next
;
441 sh
= list_entry(first
, struct stripe_head
, lru
);
442 list_del_init(first
);
444 atomic_inc(&conf
->active_stripes
);
445 BUG_ON(hash
!= sh
->hash_lock_index
);
446 if (list_empty(conf
->inactive_list
+ hash
))
447 atomic_inc(&conf
->empty_inactive_list_nr
);
452 static void shrink_buffers(struct stripe_head
*sh
)
456 int num
= sh
->raid_conf
->pool_size
;
458 for (i
= 0; i
< num
; i
++) {
459 WARN_ON(sh
->dev
[i
].page
!= sh
->dev
[i
].orig_page
);
463 sh
->dev
[i
].page
= NULL
;
468 static int grow_buffers(struct stripe_head
*sh
, gfp_t gfp
)
471 int num
= sh
->raid_conf
->pool_size
;
473 for (i
= 0; i
< num
; i
++) {
476 if (!(page
= alloc_page(gfp
))) {
479 sh
->dev
[i
].page
= page
;
480 sh
->dev
[i
].orig_page
= page
;
485 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
486 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
487 struct stripe_head
*sh
);
489 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
491 struct r5conf
*conf
= sh
->raid_conf
;
494 BUG_ON(atomic_read(&sh
->count
) != 0);
495 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
496 BUG_ON(stripe_operations_active(sh
));
497 BUG_ON(sh
->batch_head
);
499 pr_debug("init_stripe called, stripe %llu\n",
500 (unsigned long long)sector
);
502 seq
= read_seqcount_begin(&conf
->gen_lock
);
503 sh
->generation
= conf
->generation
- previous
;
504 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
506 stripe_set_idx(sector
, conf
, previous
, sh
);
509 for (i
= sh
->disks
; i
--; ) {
510 struct r5dev
*dev
= &sh
->dev
[i
];
512 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
513 test_bit(R5_LOCKED
, &dev
->flags
)) {
514 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
515 (unsigned long long)sh
->sector
, i
, dev
->toread
,
516 dev
->read
, dev
->towrite
, dev
->written
,
517 test_bit(R5_LOCKED
, &dev
->flags
));
521 raid5_build_block(sh
, i
, previous
);
523 if (read_seqcount_retry(&conf
->gen_lock
, seq
))
525 sh
->overwrite_disks
= 0;
526 insert_hash(conf
, sh
);
527 sh
->cpu
= smp_processor_id();
528 set_bit(STRIPE_BATCH_READY
, &sh
->state
);
531 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
534 struct stripe_head
*sh
;
536 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
537 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
538 if (sh
->sector
== sector
&& sh
->generation
== generation
)
540 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
545 * Need to check if array has failed when deciding whether to:
547 * - remove non-faulty devices
550 * This determination is simple when no reshape is happening.
551 * However if there is a reshape, we need to carefully check
552 * both the before and after sections.
553 * This is because some failed devices may only affect one
554 * of the two sections, and some non-in_sync devices may
555 * be insync in the section most affected by failed devices.
557 int raid5_calc_degraded(struct r5conf
*conf
)
559 int degraded
, degraded2
;
564 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
565 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
566 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
567 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
568 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
570 else if (test_bit(In_sync
, &rdev
->flags
))
573 /* not in-sync or faulty.
574 * If the reshape increases the number of devices,
575 * this is being recovered by the reshape, so
576 * this 'previous' section is not in_sync.
577 * If the number of devices is being reduced however,
578 * the device can only be part of the array if
579 * we are reverting a reshape, so this section will
582 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
586 if (conf
->raid_disks
== conf
->previous_raid_disks
)
590 for (i
= 0; i
< conf
->raid_disks
; i
++) {
591 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
592 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
593 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
594 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
596 else if (test_bit(In_sync
, &rdev
->flags
))
599 /* not in-sync or faulty.
600 * If reshape increases the number of devices, this
601 * section has already been recovered, else it
602 * almost certainly hasn't.
604 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
608 if (degraded2
> degraded
)
613 static int has_failed(struct r5conf
*conf
)
617 if (conf
->mddev
->reshape_position
== MaxSector
)
618 return conf
->mddev
->degraded
> conf
->max_degraded
;
620 degraded
= raid5_calc_degraded(conf
);
621 if (degraded
> conf
->max_degraded
)
627 raid5_get_active_stripe(struct r5conf
*conf
, sector_t sector
,
628 int previous
, int noblock
, int noquiesce
)
630 struct stripe_head
*sh
;
631 int hash
= stripe_hash_locks_hash(sector
);
632 int inc_empty_inactive_list_flag
;
634 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
636 spin_lock_irq(conf
->hash_locks
+ hash
);
639 wait_event_lock_irq(conf
->wait_for_quiescent
,
640 conf
->quiesce
== 0 || noquiesce
,
641 *(conf
->hash_locks
+ hash
));
642 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
644 if (!test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
)) {
645 sh
= get_free_stripe(conf
, hash
);
646 if (!sh
&& !test_bit(R5_DID_ALLOC
,
648 set_bit(R5_ALLOC_MORE
,
651 if (noblock
&& sh
== NULL
)
654 r5c_check_stripe_cache_usage(conf
);
656 set_bit(R5_INACTIVE_BLOCKED
,
658 r5l_wake_reclaim(conf
->log
, 0);
660 conf
->wait_for_stripe
,
661 !list_empty(conf
->inactive_list
+ hash
) &&
662 (atomic_read(&conf
->active_stripes
)
663 < (conf
->max_nr_stripes
* 3 / 4)
664 || !test_bit(R5_INACTIVE_BLOCKED
,
665 &conf
->cache_state
)),
666 *(conf
->hash_locks
+ hash
));
667 clear_bit(R5_INACTIVE_BLOCKED
,
670 init_stripe(sh
, sector
, previous
);
671 atomic_inc(&sh
->count
);
673 } else if (!atomic_inc_not_zero(&sh
->count
)) {
674 spin_lock(&conf
->device_lock
);
675 if (!atomic_read(&sh
->count
)) {
676 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
677 atomic_inc(&conf
->active_stripes
);
678 BUG_ON(list_empty(&sh
->lru
) &&
679 !test_bit(STRIPE_EXPANDING
, &sh
->state
));
680 inc_empty_inactive_list_flag
= 0;
681 if (!list_empty(conf
->inactive_list
+ hash
))
682 inc_empty_inactive_list_flag
= 1;
683 list_del_init(&sh
->lru
);
684 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
685 atomic_inc(&conf
->empty_inactive_list_nr
);
687 sh
->group
->stripes_cnt
--;
691 atomic_inc(&sh
->count
);
692 spin_unlock(&conf
->device_lock
);
694 } while (sh
== NULL
);
696 spin_unlock_irq(conf
->hash_locks
+ hash
);
700 static bool is_full_stripe_write(struct stripe_head
*sh
)
702 BUG_ON(sh
->overwrite_disks
> (sh
->disks
- sh
->raid_conf
->max_degraded
));
703 return sh
->overwrite_disks
== (sh
->disks
- sh
->raid_conf
->max_degraded
);
706 static void lock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
710 spin_lock(&sh2
->stripe_lock
);
711 spin_lock_nested(&sh1
->stripe_lock
, 1);
713 spin_lock(&sh1
->stripe_lock
);
714 spin_lock_nested(&sh2
->stripe_lock
, 1);
718 static void unlock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
720 spin_unlock(&sh1
->stripe_lock
);
721 spin_unlock(&sh2
->stripe_lock
);
725 /* Only freshly new full stripe normal write stripe can be added to a batch list */
726 static bool stripe_can_batch(struct stripe_head
*sh
)
728 struct r5conf
*conf
= sh
->raid_conf
;
732 return test_bit(STRIPE_BATCH_READY
, &sh
->state
) &&
733 !test_bit(STRIPE_BITMAP_PENDING
, &sh
->state
) &&
734 is_full_stripe_write(sh
);
737 /* we only do back search */
738 static void stripe_add_to_batch_list(struct r5conf
*conf
, struct stripe_head
*sh
)
740 struct stripe_head
*head
;
741 sector_t head_sector
, tmp_sec
;
744 int inc_empty_inactive_list_flag
;
746 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
747 tmp_sec
= sh
->sector
;
748 if (!sector_div(tmp_sec
, conf
->chunk_sectors
))
750 head_sector
= sh
->sector
- STRIPE_SECTORS
;
752 hash
= stripe_hash_locks_hash(head_sector
);
753 spin_lock_irq(conf
->hash_locks
+ hash
);
754 head
= __find_stripe(conf
, head_sector
, conf
->generation
);
755 if (head
&& !atomic_inc_not_zero(&head
->count
)) {
756 spin_lock(&conf
->device_lock
);
757 if (!atomic_read(&head
->count
)) {
758 if (!test_bit(STRIPE_HANDLE
, &head
->state
))
759 atomic_inc(&conf
->active_stripes
);
760 BUG_ON(list_empty(&head
->lru
) &&
761 !test_bit(STRIPE_EXPANDING
, &head
->state
));
762 inc_empty_inactive_list_flag
= 0;
763 if (!list_empty(conf
->inactive_list
+ hash
))
764 inc_empty_inactive_list_flag
= 1;
765 list_del_init(&head
->lru
);
766 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
767 atomic_inc(&conf
->empty_inactive_list_nr
);
769 head
->group
->stripes_cnt
--;
773 atomic_inc(&head
->count
);
774 spin_unlock(&conf
->device_lock
);
776 spin_unlock_irq(conf
->hash_locks
+ hash
);
780 if (!stripe_can_batch(head
))
783 lock_two_stripes(head
, sh
);
784 /* clear_batch_ready clear the flag */
785 if (!stripe_can_batch(head
) || !stripe_can_batch(sh
))
792 while (dd_idx
== sh
->pd_idx
|| dd_idx
== sh
->qd_idx
)
794 if (head
->dev
[dd_idx
].towrite
->bi_opf
!= sh
->dev
[dd_idx
].towrite
->bi_opf
||
795 bio_op(head
->dev
[dd_idx
].towrite
) != bio_op(sh
->dev
[dd_idx
].towrite
))
798 if (head
->batch_head
) {
799 spin_lock(&head
->batch_head
->batch_lock
);
800 /* This batch list is already running */
801 if (!stripe_can_batch(head
)) {
802 spin_unlock(&head
->batch_head
->batch_lock
);
807 * at this point, head's BATCH_READY could be cleared, but we
808 * can still add the stripe to batch list
810 list_add(&sh
->batch_list
, &head
->batch_list
);
811 spin_unlock(&head
->batch_head
->batch_lock
);
813 sh
->batch_head
= head
->batch_head
;
815 head
->batch_head
= head
;
816 sh
->batch_head
= head
->batch_head
;
817 spin_lock(&head
->batch_lock
);
818 list_add_tail(&sh
->batch_list
, &head
->batch_list
);
819 spin_unlock(&head
->batch_lock
);
822 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
823 if (atomic_dec_return(&conf
->preread_active_stripes
)
825 md_wakeup_thread(conf
->mddev
->thread
);
827 if (test_and_clear_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
828 int seq
= sh
->bm_seq
;
829 if (test_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
) &&
830 sh
->batch_head
->bm_seq
> seq
)
831 seq
= sh
->batch_head
->bm_seq
;
832 set_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
);
833 sh
->batch_head
->bm_seq
= seq
;
836 atomic_inc(&sh
->count
);
838 unlock_two_stripes(head
, sh
);
840 raid5_release_stripe(head
);
843 /* Determine if 'data_offset' or 'new_data_offset' should be used
844 * in this stripe_head.
846 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
848 sector_t progress
= conf
->reshape_progress
;
849 /* Need a memory barrier to make sure we see the value
850 * of conf->generation, or ->data_offset that was set before
851 * reshape_progress was updated.
854 if (progress
== MaxSector
)
856 if (sh
->generation
== conf
->generation
- 1)
858 /* We are in a reshape, and this is a new-generation stripe,
859 * so use new_data_offset.
864 static void flush_deferred_bios(struct r5conf
*conf
)
869 if (!conf
->batch_bio_dispatch
|| !conf
->group_cnt
)
873 spin_lock(&conf
->pending_bios_lock
);
874 bio_list_merge(&tmp
, &conf
->pending_bios
);
875 bio_list_init(&conf
->pending_bios
);
876 spin_unlock(&conf
->pending_bios_lock
);
878 while ((bio
= bio_list_pop(&tmp
)))
879 generic_make_request(bio
);
882 static void defer_bio_issue(struct r5conf
*conf
, struct bio
*bio
)
885 * change group_cnt will drain all bios, so this is safe
887 * A read generally means a read-modify-write, which usually means a
888 * randwrite, so we don't delay it
890 if (!conf
->batch_bio_dispatch
|| !conf
->group_cnt
||
891 bio_op(bio
) == REQ_OP_READ
) {
892 generic_make_request(bio
);
895 spin_lock(&conf
->pending_bios_lock
);
896 bio_list_add(&conf
->pending_bios
, bio
);
897 spin_unlock(&conf
->pending_bios_lock
);
898 md_wakeup_thread(conf
->mddev
->thread
);
902 raid5_end_read_request(struct bio
*bi
);
904 raid5_end_write_request(struct bio
*bi
);
906 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
908 struct r5conf
*conf
= sh
->raid_conf
;
909 int i
, disks
= sh
->disks
;
910 struct stripe_head
*head_sh
= sh
;
914 if (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
)) {
915 /* writing out phase */
916 if (s
->waiting_extra_page
)
918 if (r5l_write_stripe(conf
->log
, sh
) == 0)
920 } else { /* caching phase */
921 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
)) {
922 r5c_cache_data(conf
->log
, sh
, s
);
927 for (i
= disks
; i
--; ) {
928 int op
, op_flags
= 0;
929 int replace_only
= 0;
930 struct bio
*bi
, *rbi
;
931 struct md_rdev
*rdev
, *rrdev
= NULL
;
934 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
936 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
938 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
940 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
942 else if (test_and_clear_bit(R5_WantReplace
,
943 &sh
->dev
[i
].flags
)) {
948 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
949 op_flags
|= REQ_SYNC
;
952 bi
= &sh
->dev
[i
].req
;
953 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
956 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
957 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
958 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
963 if (op_is_write(op
)) {
967 /* We raced and saw duplicates */
970 if (test_bit(R5_ReadRepl
, &head_sh
->dev
[i
].flags
) && rrdev
)
975 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
978 atomic_inc(&rdev
->nr_pending
);
979 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
982 atomic_inc(&rrdev
->nr_pending
);
985 /* We have already checked bad blocks for reads. Now
986 * need to check for writes. We never accept write errors
987 * on the replacement, so we don't to check rrdev.
989 while (op_is_write(op
) && rdev
&&
990 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
993 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
994 &first_bad
, &bad_sectors
);
999 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1000 if (!conf
->mddev
->external
&&
1001 conf
->mddev
->sb_flags
) {
1002 /* It is very unlikely, but we might
1003 * still need to write out the
1004 * bad block log - better give it
1006 md_check_recovery(conf
->mddev
);
1009 * Because md_wait_for_blocked_rdev
1010 * will dec nr_pending, we must
1011 * increment it first.
1013 atomic_inc(&rdev
->nr_pending
);
1014 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
1016 /* Acknowledged bad block - skip the write */
1017 rdev_dec_pending(rdev
, conf
->mddev
);
1023 if (s
->syncing
|| s
->expanding
|| s
->expanded
1025 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1027 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
1029 bi
->bi_bdev
= rdev
->bdev
;
1030 bio_set_op_attrs(bi
, op
, op_flags
);
1031 bi
->bi_end_io
= op_is_write(op
)
1032 ? raid5_end_write_request
1033 : raid5_end_read_request
;
1034 bi
->bi_private
= sh
;
1036 pr_debug("%s: for %llu schedule op %d on disc %d\n",
1037 __func__
, (unsigned long long)sh
->sector
,
1039 atomic_inc(&sh
->count
);
1041 atomic_inc(&head_sh
->count
);
1042 if (use_new_offset(conf
, sh
))
1043 bi
->bi_iter
.bi_sector
= (sh
->sector
1044 + rdev
->new_data_offset
);
1046 bi
->bi_iter
.bi_sector
= (sh
->sector
1047 + rdev
->data_offset
);
1048 if (test_bit(R5_ReadNoMerge
, &head_sh
->dev
[i
].flags
))
1049 bi
->bi_opf
|= REQ_NOMERGE
;
1051 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1052 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1054 if (!op_is_write(op
) &&
1055 test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
1057 * issuing read for a page in journal, this
1058 * must be preparing for prexor in rmw; read
1059 * the data into orig_page
1061 sh
->dev
[i
].vec
.bv_page
= sh
->dev
[i
].orig_page
;
1063 sh
->dev
[i
].vec
.bv_page
= sh
->dev
[i
].page
;
1065 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1066 bi
->bi_io_vec
[0].bv_offset
= 0;
1067 bi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1069 * If this is discard request, set bi_vcnt 0. We don't
1070 * want to confuse SCSI because SCSI will replace payload
1072 if (op
== REQ_OP_DISCARD
)
1075 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
1077 if (conf
->mddev
->gendisk
)
1078 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
1079 bi
, disk_devt(conf
->mddev
->gendisk
),
1081 defer_bio_issue(conf
, bi
);
1084 if (s
->syncing
|| s
->expanding
|| s
->expanded
1086 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
1088 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
1090 rbi
->bi_bdev
= rrdev
->bdev
;
1091 bio_set_op_attrs(rbi
, op
, op_flags
);
1092 BUG_ON(!op_is_write(op
));
1093 rbi
->bi_end_io
= raid5_end_write_request
;
1094 rbi
->bi_private
= sh
;
1096 pr_debug("%s: for %llu schedule op %d on "
1097 "replacement disc %d\n",
1098 __func__
, (unsigned long long)sh
->sector
,
1100 atomic_inc(&sh
->count
);
1102 atomic_inc(&head_sh
->count
);
1103 if (use_new_offset(conf
, sh
))
1104 rbi
->bi_iter
.bi_sector
= (sh
->sector
1105 + rrdev
->new_data_offset
);
1107 rbi
->bi_iter
.bi_sector
= (sh
->sector
1108 + rrdev
->data_offset
);
1109 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1110 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1111 sh
->dev
[i
].rvec
.bv_page
= sh
->dev
[i
].page
;
1113 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1114 rbi
->bi_io_vec
[0].bv_offset
= 0;
1115 rbi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1117 * If this is discard request, set bi_vcnt 0. We don't
1118 * want to confuse SCSI because SCSI will replace payload
1120 if (op
== REQ_OP_DISCARD
)
1122 if (conf
->mddev
->gendisk
)
1123 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
1124 rbi
, disk_devt(conf
->mddev
->gendisk
),
1126 defer_bio_issue(conf
, rbi
);
1128 if (!rdev
&& !rrdev
) {
1129 if (op_is_write(op
))
1130 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1131 pr_debug("skip op %d on disc %d for sector %llu\n",
1132 bi
->bi_opf
, i
, (unsigned long long)sh
->sector
);
1133 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1134 set_bit(STRIPE_HANDLE
, &sh
->state
);
1137 if (!head_sh
->batch_head
)
1139 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1146 static struct dma_async_tx_descriptor
*
1147 async_copy_data(int frombio
, struct bio
*bio
, struct page
**page
,
1148 sector_t sector
, struct dma_async_tx_descriptor
*tx
,
1149 struct stripe_head
*sh
, int no_skipcopy
)
1152 struct bvec_iter iter
;
1153 struct page
*bio_page
;
1155 struct async_submit_ctl submit
;
1156 enum async_tx_flags flags
= 0;
1158 if (bio
->bi_iter
.bi_sector
>= sector
)
1159 page_offset
= (signed)(bio
->bi_iter
.bi_sector
- sector
) * 512;
1161 page_offset
= (signed)(sector
- bio
->bi_iter
.bi_sector
) * -512;
1164 flags
|= ASYNC_TX_FENCE
;
1165 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
1167 bio_for_each_segment(bvl
, bio
, iter
) {
1168 int len
= bvl
.bv_len
;
1172 if (page_offset
< 0) {
1173 b_offset
= -page_offset
;
1174 page_offset
+= b_offset
;
1178 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
1179 clen
= STRIPE_SIZE
- page_offset
;
1184 b_offset
+= bvl
.bv_offset
;
1185 bio_page
= bvl
.bv_page
;
1187 if (sh
->raid_conf
->skip_copy
&&
1188 b_offset
== 0 && page_offset
== 0 &&
1189 clen
== STRIPE_SIZE
&&
1193 tx
= async_memcpy(*page
, bio_page
, page_offset
,
1194 b_offset
, clen
, &submit
);
1196 tx
= async_memcpy(bio_page
, *page
, b_offset
,
1197 page_offset
, clen
, &submit
);
1199 /* chain the operations */
1200 submit
.depend_tx
= tx
;
1202 if (clen
< len
) /* hit end of page */
1210 static void ops_complete_biofill(void *stripe_head_ref
)
1212 struct stripe_head
*sh
= stripe_head_ref
;
1213 struct bio_list return_bi
= BIO_EMPTY_LIST
;
1216 pr_debug("%s: stripe %llu\n", __func__
,
1217 (unsigned long long)sh
->sector
);
1219 /* clear completed biofills */
1220 for (i
= sh
->disks
; i
--; ) {
1221 struct r5dev
*dev
= &sh
->dev
[i
];
1223 /* acknowledge completion of a biofill operation */
1224 /* and check if we need to reply to a read request,
1225 * new R5_Wantfill requests are held off until
1226 * !STRIPE_BIOFILL_RUN
1228 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
1229 struct bio
*rbi
, *rbi2
;
1234 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1235 dev
->sector
+ STRIPE_SECTORS
) {
1236 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1237 if (!raid5_dec_bi_active_stripes(rbi
))
1238 bio_list_add(&return_bi
, rbi
);
1243 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
1245 return_io(&return_bi
);
1247 set_bit(STRIPE_HANDLE
, &sh
->state
);
1248 raid5_release_stripe(sh
);
1251 static void ops_run_biofill(struct stripe_head
*sh
)
1253 struct dma_async_tx_descriptor
*tx
= NULL
;
1254 struct async_submit_ctl submit
;
1257 BUG_ON(sh
->batch_head
);
1258 pr_debug("%s: stripe %llu\n", __func__
,
1259 (unsigned long long)sh
->sector
);
1261 for (i
= sh
->disks
; i
--; ) {
1262 struct r5dev
*dev
= &sh
->dev
[i
];
1263 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
1265 spin_lock_irq(&sh
->stripe_lock
);
1266 dev
->read
= rbi
= dev
->toread
;
1268 spin_unlock_irq(&sh
->stripe_lock
);
1269 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1270 dev
->sector
+ STRIPE_SECTORS
) {
1271 tx
= async_copy_data(0, rbi
, &dev
->page
,
1272 dev
->sector
, tx
, sh
, 0);
1273 rbi
= r5_next_bio(rbi
, dev
->sector
);
1278 atomic_inc(&sh
->count
);
1279 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
1280 async_trigger_callback(&submit
);
1283 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
1290 tgt
= &sh
->dev
[target
];
1291 set_bit(R5_UPTODATE
, &tgt
->flags
);
1292 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1293 clear_bit(R5_Wantcompute
, &tgt
->flags
);
1296 static void ops_complete_compute(void *stripe_head_ref
)
1298 struct stripe_head
*sh
= stripe_head_ref
;
1300 pr_debug("%s: stripe %llu\n", __func__
,
1301 (unsigned long long)sh
->sector
);
1303 /* mark the computed target(s) as uptodate */
1304 mark_target_uptodate(sh
, sh
->ops
.target
);
1305 mark_target_uptodate(sh
, sh
->ops
.target2
);
1307 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
1308 if (sh
->check_state
== check_state_compute_run
)
1309 sh
->check_state
= check_state_compute_result
;
1310 set_bit(STRIPE_HANDLE
, &sh
->state
);
1311 raid5_release_stripe(sh
);
1314 /* return a pointer to the address conversion region of the scribble buffer */
1315 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
1316 struct raid5_percpu
*percpu
, int i
)
1320 addr
= flex_array_get(percpu
->scribble
, i
);
1321 return addr
+ sizeof(struct page
*) * (sh
->disks
+ 2);
1324 /* return a pointer to the address conversion region of the scribble buffer */
1325 static struct page
**to_addr_page(struct raid5_percpu
*percpu
, int i
)
1329 addr
= flex_array_get(percpu
->scribble
, i
);
1333 static struct dma_async_tx_descriptor
*
1334 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1336 int disks
= sh
->disks
;
1337 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1338 int target
= sh
->ops
.target
;
1339 struct r5dev
*tgt
= &sh
->dev
[target
];
1340 struct page
*xor_dest
= tgt
->page
;
1342 struct dma_async_tx_descriptor
*tx
;
1343 struct async_submit_ctl submit
;
1346 BUG_ON(sh
->batch_head
);
1348 pr_debug("%s: stripe %llu block: %d\n",
1349 __func__
, (unsigned long long)sh
->sector
, target
);
1350 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1352 for (i
= disks
; i
--; )
1354 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1356 atomic_inc(&sh
->count
);
1358 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
1359 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
, 0));
1360 if (unlikely(count
== 1))
1361 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1363 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1368 /* set_syndrome_sources - populate source buffers for gen_syndrome
1369 * @srcs - (struct page *) array of size sh->disks
1370 * @sh - stripe_head to parse
1372 * Populates srcs in proper layout order for the stripe and returns the
1373 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1374 * destination buffer is recorded in srcs[count] and the Q destination
1375 * is recorded in srcs[count+1]].
1377 static int set_syndrome_sources(struct page
**srcs
,
1378 struct stripe_head
*sh
,
1381 int disks
= sh
->disks
;
1382 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1383 int d0_idx
= raid6_d0(sh
);
1387 for (i
= 0; i
< disks
; i
++)
1393 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1394 struct r5dev
*dev
= &sh
->dev
[i
];
1396 if (i
== sh
->qd_idx
|| i
== sh
->pd_idx
||
1397 (srctype
== SYNDROME_SRC_ALL
) ||
1398 (srctype
== SYNDROME_SRC_WANT_DRAIN
&&
1399 (test_bit(R5_Wantdrain
, &dev
->flags
) ||
1400 test_bit(R5_InJournal
, &dev
->flags
))) ||
1401 (srctype
== SYNDROME_SRC_WRITTEN
&&
1403 if (test_bit(R5_InJournal
, &dev
->flags
))
1404 srcs
[slot
] = sh
->dev
[i
].orig_page
;
1406 srcs
[slot
] = sh
->dev
[i
].page
;
1408 i
= raid6_next_disk(i
, disks
);
1409 } while (i
!= d0_idx
);
1411 return syndrome_disks
;
1414 static struct dma_async_tx_descriptor
*
1415 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1417 int disks
= sh
->disks
;
1418 struct page
**blocks
= to_addr_page(percpu
, 0);
1420 int qd_idx
= sh
->qd_idx
;
1421 struct dma_async_tx_descriptor
*tx
;
1422 struct async_submit_ctl submit
;
1428 BUG_ON(sh
->batch_head
);
1429 if (sh
->ops
.target
< 0)
1430 target
= sh
->ops
.target2
;
1431 else if (sh
->ops
.target2
< 0)
1432 target
= sh
->ops
.target
;
1434 /* we should only have one valid target */
1437 pr_debug("%s: stripe %llu block: %d\n",
1438 __func__
, (unsigned long long)sh
->sector
, target
);
1440 tgt
= &sh
->dev
[target
];
1441 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1444 atomic_inc(&sh
->count
);
1446 if (target
== qd_idx
) {
1447 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1448 blocks
[count
] = NULL
; /* regenerating p is not necessary */
1449 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
1450 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1451 ops_complete_compute
, sh
,
1452 to_addr_conv(sh
, percpu
, 0));
1453 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1455 /* Compute any data- or p-drive using XOR */
1457 for (i
= disks
; i
-- ; ) {
1458 if (i
== target
|| i
== qd_idx
)
1460 blocks
[count
++] = sh
->dev
[i
].page
;
1463 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1464 NULL
, ops_complete_compute
, sh
,
1465 to_addr_conv(sh
, percpu
, 0));
1466 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1472 static struct dma_async_tx_descriptor
*
1473 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1475 int i
, count
, disks
= sh
->disks
;
1476 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1477 int d0_idx
= raid6_d0(sh
);
1478 int faila
= -1, failb
= -1;
1479 int target
= sh
->ops
.target
;
1480 int target2
= sh
->ops
.target2
;
1481 struct r5dev
*tgt
= &sh
->dev
[target
];
1482 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1483 struct dma_async_tx_descriptor
*tx
;
1484 struct page
**blocks
= to_addr_page(percpu
, 0);
1485 struct async_submit_ctl submit
;
1487 BUG_ON(sh
->batch_head
);
1488 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1489 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1490 BUG_ON(target
< 0 || target2
< 0);
1491 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1492 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1494 /* we need to open-code set_syndrome_sources to handle the
1495 * slot number conversion for 'faila' and 'failb'
1497 for (i
= 0; i
< disks
; i
++)
1502 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1504 blocks
[slot
] = sh
->dev
[i
].page
;
1510 i
= raid6_next_disk(i
, disks
);
1511 } while (i
!= d0_idx
);
1513 BUG_ON(faila
== failb
);
1516 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1517 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1519 atomic_inc(&sh
->count
);
1521 if (failb
== syndrome_disks
+1) {
1522 /* Q disk is one of the missing disks */
1523 if (faila
== syndrome_disks
) {
1524 /* Missing P+Q, just recompute */
1525 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1526 ops_complete_compute
, sh
,
1527 to_addr_conv(sh
, percpu
, 0));
1528 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1529 STRIPE_SIZE
, &submit
);
1533 int qd_idx
= sh
->qd_idx
;
1535 /* Missing D+Q: recompute D from P, then recompute Q */
1536 if (target
== qd_idx
)
1537 data_target
= target2
;
1539 data_target
= target
;
1542 for (i
= disks
; i
-- ; ) {
1543 if (i
== data_target
|| i
== qd_idx
)
1545 blocks
[count
++] = sh
->dev
[i
].page
;
1547 dest
= sh
->dev
[data_target
].page
;
1548 init_async_submit(&submit
,
1549 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1551 to_addr_conv(sh
, percpu
, 0));
1552 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1555 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1556 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1557 ops_complete_compute
, sh
,
1558 to_addr_conv(sh
, percpu
, 0));
1559 return async_gen_syndrome(blocks
, 0, count
+2,
1560 STRIPE_SIZE
, &submit
);
1563 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1564 ops_complete_compute
, sh
,
1565 to_addr_conv(sh
, percpu
, 0));
1566 if (failb
== syndrome_disks
) {
1567 /* We're missing D+P. */
1568 return async_raid6_datap_recov(syndrome_disks
+2,
1572 /* We're missing D+D. */
1573 return async_raid6_2data_recov(syndrome_disks
+2,
1574 STRIPE_SIZE
, faila
, failb
,
1580 static void ops_complete_prexor(void *stripe_head_ref
)
1582 struct stripe_head
*sh
= stripe_head_ref
;
1584 pr_debug("%s: stripe %llu\n", __func__
,
1585 (unsigned long long)sh
->sector
);
1587 if (r5c_is_writeback(sh
->raid_conf
->log
))
1589 * raid5-cache write back uses orig_page during prexor.
1590 * After prexor, it is time to free orig_page
1592 r5c_release_extra_page(sh
);
1595 static struct dma_async_tx_descriptor
*
1596 ops_run_prexor5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1597 struct dma_async_tx_descriptor
*tx
)
1599 int disks
= sh
->disks
;
1600 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1601 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1602 struct async_submit_ctl submit
;
1604 /* existing parity data subtracted */
1605 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1607 BUG_ON(sh
->batch_head
);
1608 pr_debug("%s: stripe %llu\n", __func__
,
1609 (unsigned long long)sh
->sector
);
1611 for (i
= disks
; i
--; ) {
1612 struct r5dev
*dev
= &sh
->dev
[i
];
1613 /* Only process blocks that are known to be uptodate */
1614 if (test_bit(R5_InJournal
, &dev
->flags
))
1615 xor_srcs
[count
++] = dev
->orig_page
;
1616 else if (test_bit(R5_Wantdrain
, &dev
->flags
))
1617 xor_srcs
[count
++] = dev
->page
;
1620 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1621 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1622 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1627 static struct dma_async_tx_descriptor
*
1628 ops_run_prexor6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1629 struct dma_async_tx_descriptor
*tx
)
1631 struct page
**blocks
= to_addr_page(percpu
, 0);
1633 struct async_submit_ctl submit
;
1635 pr_debug("%s: stripe %llu\n", __func__
,
1636 (unsigned long long)sh
->sector
);
1638 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_WANT_DRAIN
);
1640 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_PQ_XOR_DST
, tx
,
1641 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1642 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1647 static struct dma_async_tx_descriptor
*
1648 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1650 struct r5conf
*conf
= sh
->raid_conf
;
1651 int disks
= sh
->disks
;
1653 struct stripe_head
*head_sh
= sh
;
1655 pr_debug("%s: stripe %llu\n", __func__
,
1656 (unsigned long long)sh
->sector
);
1658 for (i
= disks
; i
--; ) {
1663 if (test_and_clear_bit(R5_Wantdrain
, &head_sh
->dev
[i
].flags
)) {
1669 * clear R5_InJournal, so when rewriting a page in
1670 * journal, it is not skipped by r5l_log_stripe()
1672 clear_bit(R5_InJournal
, &dev
->flags
);
1673 spin_lock_irq(&sh
->stripe_lock
);
1674 chosen
= dev
->towrite
;
1675 dev
->towrite
= NULL
;
1676 sh
->overwrite_disks
= 0;
1677 BUG_ON(dev
->written
);
1678 wbi
= dev
->written
= chosen
;
1679 spin_unlock_irq(&sh
->stripe_lock
);
1680 WARN_ON(dev
->page
!= dev
->orig_page
);
1682 while (wbi
&& wbi
->bi_iter
.bi_sector
<
1683 dev
->sector
+ STRIPE_SECTORS
) {
1684 if (wbi
->bi_opf
& REQ_FUA
)
1685 set_bit(R5_WantFUA
, &dev
->flags
);
1686 if (wbi
->bi_opf
& REQ_SYNC
)
1687 set_bit(R5_SyncIO
, &dev
->flags
);
1688 if (bio_op(wbi
) == REQ_OP_DISCARD
)
1689 set_bit(R5_Discard
, &dev
->flags
);
1691 tx
= async_copy_data(1, wbi
, &dev
->page
,
1692 dev
->sector
, tx
, sh
,
1693 r5c_is_writeback(conf
->log
));
1694 if (dev
->page
!= dev
->orig_page
&&
1695 !r5c_is_writeback(conf
->log
)) {
1696 set_bit(R5_SkipCopy
, &dev
->flags
);
1697 clear_bit(R5_UPTODATE
, &dev
->flags
);
1698 clear_bit(R5_OVERWRITE
, &dev
->flags
);
1701 wbi
= r5_next_bio(wbi
, dev
->sector
);
1704 if (head_sh
->batch_head
) {
1705 sh
= list_first_entry(&sh
->batch_list
,
1718 static void ops_complete_reconstruct(void *stripe_head_ref
)
1720 struct stripe_head
*sh
= stripe_head_ref
;
1721 int disks
= sh
->disks
;
1722 int pd_idx
= sh
->pd_idx
;
1723 int qd_idx
= sh
->qd_idx
;
1725 bool fua
= false, sync
= false, discard
= false;
1727 pr_debug("%s: stripe %llu\n", __func__
,
1728 (unsigned long long)sh
->sector
);
1730 for (i
= disks
; i
--; ) {
1731 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1732 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1733 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1736 for (i
= disks
; i
--; ) {
1737 struct r5dev
*dev
= &sh
->dev
[i
];
1739 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1740 if (!discard
&& !test_bit(R5_SkipCopy
, &dev
->flags
))
1741 set_bit(R5_UPTODATE
, &dev
->flags
);
1743 set_bit(R5_WantFUA
, &dev
->flags
);
1745 set_bit(R5_SyncIO
, &dev
->flags
);
1749 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1750 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1751 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1752 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1754 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1755 sh
->reconstruct_state
= reconstruct_state_result
;
1758 set_bit(STRIPE_HANDLE
, &sh
->state
);
1759 raid5_release_stripe(sh
);
1763 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1764 struct dma_async_tx_descriptor
*tx
)
1766 int disks
= sh
->disks
;
1767 struct page
**xor_srcs
;
1768 struct async_submit_ctl submit
;
1769 int count
, pd_idx
= sh
->pd_idx
, i
;
1770 struct page
*xor_dest
;
1772 unsigned long flags
;
1774 struct stripe_head
*head_sh
= sh
;
1777 pr_debug("%s: stripe %llu\n", __func__
,
1778 (unsigned long long)sh
->sector
);
1780 for (i
= 0; i
< sh
->disks
; i
++) {
1783 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1786 if (i
>= sh
->disks
) {
1787 atomic_inc(&sh
->count
);
1788 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1789 ops_complete_reconstruct(sh
);
1794 xor_srcs
= to_addr_page(percpu
, j
);
1795 /* check if prexor is active which means only process blocks
1796 * that are part of a read-modify-write (written)
1798 if (head_sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1800 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1801 for (i
= disks
; i
--; ) {
1802 struct r5dev
*dev
= &sh
->dev
[i
];
1803 if (head_sh
->dev
[i
].written
||
1804 test_bit(R5_InJournal
, &head_sh
->dev
[i
].flags
))
1805 xor_srcs
[count
++] = dev
->page
;
1808 xor_dest
= sh
->dev
[pd_idx
].page
;
1809 for (i
= disks
; i
--; ) {
1810 struct r5dev
*dev
= &sh
->dev
[i
];
1812 xor_srcs
[count
++] = dev
->page
;
1816 /* 1/ if we prexor'd then the dest is reused as a source
1817 * 2/ if we did not prexor then we are redoing the parity
1818 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1819 * for the synchronous xor case
1821 last_stripe
= !head_sh
->batch_head
||
1822 list_first_entry(&sh
->batch_list
,
1823 struct stripe_head
, batch_list
) == head_sh
;
1825 flags
= ASYNC_TX_ACK
|
1826 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1828 atomic_inc(&head_sh
->count
);
1829 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, head_sh
,
1830 to_addr_conv(sh
, percpu
, j
));
1832 flags
= prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
;
1833 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
,
1834 to_addr_conv(sh
, percpu
, j
));
1837 if (unlikely(count
== 1))
1838 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1840 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1843 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1850 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1851 struct dma_async_tx_descriptor
*tx
)
1853 struct async_submit_ctl submit
;
1854 struct page
**blocks
;
1855 int count
, i
, j
= 0;
1856 struct stripe_head
*head_sh
= sh
;
1859 unsigned long txflags
;
1861 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1863 for (i
= 0; i
< sh
->disks
; i
++) {
1864 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1866 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1869 if (i
>= sh
->disks
) {
1870 atomic_inc(&sh
->count
);
1871 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1872 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1873 ops_complete_reconstruct(sh
);
1878 blocks
= to_addr_page(percpu
, j
);
1880 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1881 synflags
= SYNDROME_SRC_WRITTEN
;
1882 txflags
= ASYNC_TX_ACK
| ASYNC_TX_PQ_XOR_DST
;
1884 synflags
= SYNDROME_SRC_ALL
;
1885 txflags
= ASYNC_TX_ACK
;
1888 count
= set_syndrome_sources(blocks
, sh
, synflags
);
1889 last_stripe
= !head_sh
->batch_head
||
1890 list_first_entry(&sh
->batch_list
,
1891 struct stripe_head
, batch_list
) == head_sh
;
1894 atomic_inc(&head_sh
->count
);
1895 init_async_submit(&submit
, txflags
, tx
, ops_complete_reconstruct
,
1896 head_sh
, to_addr_conv(sh
, percpu
, j
));
1898 init_async_submit(&submit
, 0, tx
, NULL
, NULL
,
1899 to_addr_conv(sh
, percpu
, j
));
1900 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1903 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1909 static void ops_complete_check(void *stripe_head_ref
)
1911 struct stripe_head
*sh
= stripe_head_ref
;
1913 pr_debug("%s: stripe %llu\n", __func__
,
1914 (unsigned long long)sh
->sector
);
1916 sh
->check_state
= check_state_check_result
;
1917 set_bit(STRIPE_HANDLE
, &sh
->state
);
1918 raid5_release_stripe(sh
);
1921 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1923 int disks
= sh
->disks
;
1924 int pd_idx
= sh
->pd_idx
;
1925 int qd_idx
= sh
->qd_idx
;
1926 struct page
*xor_dest
;
1927 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1928 struct dma_async_tx_descriptor
*tx
;
1929 struct async_submit_ctl submit
;
1933 pr_debug("%s: stripe %llu\n", __func__
,
1934 (unsigned long long)sh
->sector
);
1936 BUG_ON(sh
->batch_head
);
1938 xor_dest
= sh
->dev
[pd_idx
].page
;
1939 xor_srcs
[count
++] = xor_dest
;
1940 for (i
= disks
; i
--; ) {
1941 if (i
== pd_idx
|| i
== qd_idx
)
1943 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1946 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1947 to_addr_conv(sh
, percpu
, 0));
1948 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1949 &sh
->ops
.zero_sum_result
, &submit
);
1951 atomic_inc(&sh
->count
);
1952 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1953 tx
= async_trigger_callback(&submit
);
1956 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1958 struct page
**srcs
= to_addr_page(percpu
, 0);
1959 struct async_submit_ctl submit
;
1962 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1963 (unsigned long long)sh
->sector
, checkp
);
1965 BUG_ON(sh
->batch_head
);
1966 count
= set_syndrome_sources(srcs
, sh
, SYNDROME_SRC_ALL
);
1970 atomic_inc(&sh
->count
);
1971 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1972 sh
, to_addr_conv(sh
, percpu
, 0));
1973 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1974 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1977 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1979 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1980 struct dma_async_tx_descriptor
*tx
= NULL
;
1981 struct r5conf
*conf
= sh
->raid_conf
;
1982 int level
= conf
->level
;
1983 struct raid5_percpu
*percpu
;
1987 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1988 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1989 ops_run_biofill(sh
);
1993 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1995 tx
= ops_run_compute5(sh
, percpu
);
1997 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1998 tx
= ops_run_compute6_1(sh
, percpu
);
2000 tx
= ops_run_compute6_2(sh
, percpu
);
2002 /* terminate the chain if reconstruct is not set to be run */
2003 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
2007 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
)) {
2009 tx
= ops_run_prexor5(sh
, percpu
, tx
);
2011 tx
= ops_run_prexor6(sh
, percpu
, tx
);
2014 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
2015 tx
= ops_run_biodrain(sh
, tx
);
2019 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
2021 ops_run_reconstruct5(sh
, percpu
, tx
);
2023 ops_run_reconstruct6(sh
, percpu
, tx
);
2026 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
2027 if (sh
->check_state
== check_state_run
)
2028 ops_run_check_p(sh
, percpu
);
2029 else if (sh
->check_state
== check_state_run_q
)
2030 ops_run_check_pq(sh
, percpu
, 0);
2031 else if (sh
->check_state
== check_state_run_pq
)
2032 ops_run_check_pq(sh
, percpu
, 1);
2037 if (overlap_clear
&& !sh
->batch_head
)
2038 for (i
= disks
; i
--; ) {
2039 struct r5dev
*dev
= &sh
->dev
[i
];
2040 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
2041 wake_up(&sh
->raid_conf
->wait_for_overlap
);
2046 static struct stripe_head
*alloc_stripe(struct kmem_cache
*sc
, gfp_t gfp
,
2049 struct stripe_head
*sh
;
2052 sh
= kmem_cache_zalloc(sc
, gfp
);
2054 spin_lock_init(&sh
->stripe_lock
);
2055 spin_lock_init(&sh
->batch_lock
);
2056 INIT_LIST_HEAD(&sh
->batch_list
);
2057 INIT_LIST_HEAD(&sh
->lru
);
2058 INIT_LIST_HEAD(&sh
->r5c
);
2059 INIT_LIST_HEAD(&sh
->log_list
);
2060 atomic_set(&sh
->count
, 1);
2061 sh
->log_start
= MaxSector
;
2062 for (i
= 0; i
< disks
; i
++) {
2063 struct r5dev
*dev
= &sh
->dev
[i
];
2065 bio_init(&dev
->req
, &dev
->vec
, 1);
2066 bio_init(&dev
->rreq
, &dev
->rvec
, 1);
2071 static int grow_one_stripe(struct r5conf
*conf
, gfp_t gfp
)
2073 struct stripe_head
*sh
;
2075 sh
= alloc_stripe(conf
->slab_cache
, gfp
, conf
->pool_size
);
2079 sh
->raid_conf
= conf
;
2081 if (grow_buffers(sh
, gfp
)) {
2083 kmem_cache_free(conf
->slab_cache
, sh
);
2086 sh
->hash_lock_index
=
2087 conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
;
2088 /* we just created an active stripe so... */
2089 atomic_inc(&conf
->active_stripes
);
2091 raid5_release_stripe(sh
);
2092 conf
->max_nr_stripes
++;
2096 static int grow_stripes(struct r5conf
*conf
, int num
)
2098 struct kmem_cache
*sc
;
2099 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
2101 if (conf
->mddev
->gendisk
)
2102 sprintf(conf
->cache_name
[0],
2103 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
2105 sprintf(conf
->cache_name
[0],
2106 "raid%d-%p", conf
->level
, conf
->mddev
);
2107 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
2109 conf
->active_name
= 0;
2110 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
2111 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
2115 conf
->slab_cache
= sc
;
2116 conf
->pool_size
= devs
;
2118 if (!grow_one_stripe(conf
, GFP_KERNEL
))
2125 * scribble_len - return the required size of the scribble region
2126 * @num - total number of disks in the array
2128 * The size must be enough to contain:
2129 * 1/ a struct page pointer for each device in the array +2
2130 * 2/ room to convert each entry in (1) to its corresponding dma
2131 * (dma_map_page()) or page (page_address()) address.
2133 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2134 * calculate over all devices (not just the data blocks), using zeros in place
2135 * of the P and Q blocks.
2137 static struct flex_array
*scribble_alloc(int num
, int cnt
, gfp_t flags
)
2139 struct flex_array
*ret
;
2142 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
2143 ret
= flex_array_alloc(len
, cnt
, flags
);
2146 /* always prealloc all elements, so no locking is required */
2147 if (flex_array_prealloc(ret
, 0, cnt
, flags
)) {
2148 flex_array_free(ret
);
2154 static int resize_chunks(struct r5conf
*conf
, int new_disks
, int new_sectors
)
2160 * Never shrink. And mddev_suspend() could deadlock if this is called
2161 * from raid5d. In that case, scribble_disks and scribble_sectors
2162 * should equal to new_disks and new_sectors
2164 if (conf
->scribble_disks
>= new_disks
&&
2165 conf
->scribble_sectors
>= new_sectors
)
2167 mddev_suspend(conf
->mddev
);
2169 for_each_present_cpu(cpu
) {
2170 struct raid5_percpu
*percpu
;
2171 struct flex_array
*scribble
;
2173 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
2174 scribble
= scribble_alloc(new_disks
,
2175 new_sectors
/ STRIPE_SECTORS
,
2179 flex_array_free(percpu
->scribble
);
2180 percpu
->scribble
= scribble
;
2187 mddev_resume(conf
->mddev
);
2189 conf
->scribble_disks
= new_disks
;
2190 conf
->scribble_sectors
= new_sectors
;
2195 static int resize_stripes(struct r5conf
*conf
, int newsize
)
2197 /* Make all the stripes able to hold 'newsize' devices.
2198 * New slots in each stripe get 'page' set to a new page.
2200 * This happens in stages:
2201 * 1/ create a new kmem_cache and allocate the required number of
2203 * 2/ gather all the old stripe_heads and transfer the pages across
2204 * to the new stripe_heads. This will have the side effect of
2205 * freezing the array as once all stripe_heads have been collected,
2206 * no IO will be possible. Old stripe heads are freed once their
2207 * pages have been transferred over, and the old kmem_cache is
2208 * freed when all stripes are done.
2209 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2210 * we simple return a failre status - no need to clean anything up.
2211 * 4/ allocate new pages for the new slots in the new stripe_heads.
2212 * If this fails, we don't bother trying the shrink the
2213 * stripe_heads down again, we just leave them as they are.
2214 * As each stripe_head is processed the new one is released into
2217 * Once step2 is started, we cannot afford to wait for a write,
2218 * so we use GFP_NOIO allocations.
2220 struct stripe_head
*osh
, *nsh
;
2221 LIST_HEAD(newstripes
);
2222 struct disk_info
*ndisks
;
2224 struct kmem_cache
*sc
;
2228 if (newsize
<= conf
->pool_size
)
2229 return 0; /* never bother to shrink */
2231 err
= md_allow_write(conf
->mddev
);
2236 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
2237 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
2242 /* Need to ensure auto-resizing doesn't interfere */
2243 mutex_lock(&conf
->cache_size_mutex
);
2245 for (i
= conf
->max_nr_stripes
; i
; i
--) {
2246 nsh
= alloc_stripe(sc
, GFP_KERNEL
, newsize
);
2250 nsh
->raid_conf
= conf
;
2251 list_add(&nsh
->lru
, &newstripes
);
2254 /* didn't get enough, give up */
2255 while (!list_empty(&newstripes
)) {
2256 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2257 list_del(&nsh
->lru
);
2258 kmem_cache_free(sc
, nsh
);
2260 kmem_cache_destroy(sc
);
2261 mutex_unlock(&conf
->cache_size_mutex
);
2264 /* Step 2 - Must use GFP_NOIO now.
2265 * OK, we have enough stripes, start collecting inactive
2266 * stripes and copying them over
2270 list_for_each_entry(nsh
, &newstripes
, lru
) {
2271 lock_device_hash_lock(conf
, hash
);
2272 wait_event_cmd(conf
->wait_for_stripe
,
2273 !list_empty(conf
->inactive_list
+ hash
),
2274 unlock_device_hash_lock(conf
, hash
),
2275 lock_device_hash_lock(conf
, hash
));
2276 osh
= get_free_stripe(conf
, hash
);
2277 unlock_device_hash_lock(conf
, hash
);
2279 for(i
=0; i
<conf
->pool_size
; i
++) {
2280 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
2281 nsh
->dev
[i
].orig_page
= osh
->dev
[i
].page
;
2283 nsh
->hash_lock_index
= hash
;
2284 kmem_cache_free(conf
->slab_cache
, osh
);
2286 if (cnt
>= conf
->max_nr_stripes
/ NR_STRIPE_HASH_LOCKS
+
2287 !!((conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
) > hash
)) {
2292 kmem_cache_destroy(conf
->slab_cache
);
2295 * At this point, we are holding all the stripes so the array
2296 * is completely stalled, so now is a good time to resize
2297 * conf->disks and the scribble region
2299 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
2301 for (i
= 0; i
< conf
->pool_size
; i
++)
2302 ndisks
[i
] = conf
->disks
[i
];
2304 for (i
= conf
->pool_size
; i
< newsize
; i
++) {
2305 ndisks
[i
].extra_page
= alloc_page(GFP_NOIO
);
2306 if (!ndisks
[i
].extra_page
)
2311 for (i
= conf
->pool_size
; i
< newsize
; i
++)
2312 if (ndisks
[i
].extra_page
)
2313 put_page(ndisks
[i
].extra_page
);
2317 conf
->disks
= ndisks
;
2322 mutex_unlock(&conf
->cache_size_mutex
);
2323 /* Step 4, return new stripes to service */
2324 while(!list_empty(&newstripes
)) {
2325 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2326 list_del_init(&nsh
->lru
);
2328 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
2329 if (nsh
->dev
[i
].page
== NULL
) {
2330 struct page
*p
= alloc_page(GFP_NOIO
);
2331 nsh
->dev
[i
].page
= p
;
2332 nsh
->dev
[i
].orig_page
= p
;
2336 raid5_release_stripe(nsh
);
2338 /* critical section pass, GFP_NOIO no longer needed */
2340 conf
->slab_cache
= sc
;
2341 conf
->active_name
= 1-conf
->active_name
;
2343 conf
->pool_size
= newsize
;
2347 static int drop_one_stripe(struct r5conf
*conf
)
2349 struct stripe_head
*sh
;
2350 int hash
= (conf
->max_nr_stripes
- 1) & STRIPE_HASH_LOCKS_MASK
;
2352 spin_lock_irq(conf
->hash_locks
+ hash
);
2353 sh
= get_free_stripe(conf
, hash
);
2354 spin_unlock_irq(conf
->hash_locks
+ hash
);
2357 BUG_ON(atomic_read(&sh
->count
));
2359 kmem_cache_free(conf
->slab_cache
, sh
);
2360 atomic_dec(&conf
->active_stripes
);
2361 conf
->max_nr_stripes
--;
2365 static void shrink_stripes(struct r5conf
*conf
)
2367 while (conf
->max_nr_stripes
&&
2368 drop_one_stripe(conf
))
2371 kmem_cache_destroy(conf
->slab_cache
);
2372 conf
->slab_cache
= NULL
;
2375 static void raid5_end_read_request(struct bio
* bi
)
2377 struct stripe_head
*sh
= bi
->bi_private
;
2378 struct r5conf
*conf
= sh
->raid_conf
;
2379 int disks
= sh
->disks
, i
;
2380 char b
[BDEVNAME_SIZE
];
2381 struct md_rdev
*rdev
= NULL
;
2384 for (i
=0 ; i
<disks
; i
++)
2385 if (bi
== &sh
->dev
[i
].req
)
2388 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2389 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2396 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2397 /* If replacement finished while this request was outstanding,
2398 * 'replacement' might be NULL already.
2399 * In that case it moved down to 'rdev'.
2400 * rdev is not removed until all requests are finished.
2402 rdev
= conf
->disks
[i
].replacement
;
2404 rdev
= conf
->disks
[i
].rdev
;
2406 if (use_new_offset(conf
, sh
))
2407 s
= sh
->sector
+ rdev
->new_data_offset
;
2409 s
= sh
->sector
+ rdev
->data_offset
;
2410 if (!bi
->bi_error
) {
2411 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2412 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2413 /* Note that this cannot happen on a
2414 * replacement device. We just fail those on
2417 pr_info_ratelimited(
2418 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
2419 mdname(conf
->mddev
), STRIPE_SECTORS
,
2420 (unsigned long long)s
,
2421 bdevname(rdev
->bdev
, b
));
2422 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
2423 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2424 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2425 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2426 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2428 if (test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
2430 * end read for a page in journal, this
2431 * must be preparing for prexor in rmw
2433 set_bit(R5_OrigPageUPTDODATE
, &sh
->dev
[i
].flags
);
2435 if (atomic_read(&rdev
->read_errors
))
2436 atomic_set(&rdev
->read_errors
, 0);
2438 const char *bdn
= bdevname(rdev
->bdev
, b
);
2442 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2443 atomic_inc(&rdev
->read_errors
);
2444 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2445 pr_warn_ratelimited(
2446 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
2447 mdname(conf
->mddev
),
2448 (unsigned long long)s
,
2450 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
2452 pr_warn_ratelimited(
2453 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
2454 mdname(conf
->mddev
),
2455 (unsigned long long)s
,
2457 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
2460 pr_warn_ratelimited(
2461 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
2462 mdname(conf
->mddev
),
2463 (unsigned long long)s
,
2465 } else if (atomic_read(&rdev
->read_errors
)
2466 > conf
->max_nr_stripes
)
2467 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2468 mdname(conf
->mddev
), bdn
);
2471 if (set_bad
&& test_bit(In_sync
, &rdev
->flags
)
2472 && !test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2475 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
2476 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2477 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2479 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2481 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2482 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2484 && test_bit(In_sync
, &rdev
->flags
)
2485 && rdev_set_badblocks(
2486 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
2487 md_error(conf
->mddev
, rdev
);
2490 rdev_dec_pending(rdev
, conf
->mddev
);
2492 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2493 set_bit(STRIPE_HANDLE
, &sh
->state
);
2494 raid5_release_stripe(sh
);
2497 static void raid5_end_write_request(struct bio
*bi
)
2499 struct stripe_head
*sh
= bi
->bi_private
;
2500 struct r5conf
*conf
= sh
->raid_conf
;
2501 int disks
= sh
->disks
, i
;
2502 struct md_rdev
*uninitialized_var(rdev
);
2505 int replacement
= 0;
2507 for (i
= 0 ; i
< disks
; i
++) {
2508 if (bi
== &sh
->dev
[i
].req
) {
2509 rdev
= conf
->disks
[i
].rdev
;
2512 if (bi
== &sh
->dev
[i
].rreq
) {
2513 rdev
= conf
->disks
[i
].replacement
;
2517 /* rdev was removed and 'replacement'
2518 * replaced it. rdev is not removed
2519 * until all requests are finished.
2521 rdev
= conf
->disks
[i
].rdev
;
2525 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2526 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2536 md_error(conf
->mddev
, rdev
);
2537 else if (is_badblock(rdev
, sh
->sector
,
2539 &first_bad
, &bad_sectors
))
2540 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
2543 set_bit(STRIPE_DEGRADED
, &sh
->state
);
2544 set_bit(WriteErrorSeen
, &rdev
->flags
);
2545 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
2546 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2547 set_bit(MD_RECOVERY_NEEDED
,
2548 &rdev
->mddev
->recovery
);
2549 } else if (is_badblock(rdev
, sh
->sector
,
2551 &first_bad
, &bad_sectors
)) {
2552 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
2553 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))
2554 /* That was a successful write so make
2555 * sure it looks like we already did
2558 set_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2561 rdev_dec_pending(rdev
, conf
->mddev
);
2563 if (sh
->batch_head
&& bi
->bi_error
&& !replacement
)
2564 set_bit(STRIPE_BATCH_ERR
, &sh
->batch_head
->state
);
2567 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
2568 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2569 set_bit(STRIPE_HANDLE
, &sh
->state
);
2570 raid5_release_stripe(sh
);
2572 if (sh
->batch_head
&& sh
!= sh
->batch_head
)
2573 raid5_release_stripe(sh
->batch_head
);
2576 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
2578 struct r5dev
*dev
= &sh
->dev
[i
];
2581 dev
->sector
= raid5_compute_blocknr(sh
, i
, previous
);
2584 static void raid5_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2586 char b
[BDEVNAME_SIZE
];
2587 struct r5conf
*conf
= mddev
->private;
2588 unsigned long flags
;
2589 pr_debug("raid456: error called\n");
2591 spin_lock_irqsave(&conf
->device_lock
, flags
);
2592 clear_bit(In_sync
, &rdev
->flags
);
2593 mddev
->degraded
= raid5_calc_degraded(conf
);
2594 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2595 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2597 set_bit(Blocked
, &rdev
->flags
);
2598 set_bit(Faulty
, &rdev
->flags
);
2599 set_mask_bits(&mddev
->sb_flags
, 0,
2600 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
2601 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2602 "md/raid:%s: Operation continuing on %d devices.\n",
2604 bdevname(rdev
->bdev
, b
),
2606 conf
->raid_disks
- mddev
->degraded
);
2607 r5c_update_on_rdev_error(mddev
);
2611 * Input: a 'big' sector number,
2612 * Output: index of the data and parity disk, and the sector # in them.
2614 sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
2615 int previous
, int *dd_idx
,
2616 struct stripe_head
*sh
)
2618 sector_t stripe
, stripe2
;
2619 sector_t chunk_number
;
2620 unsigned int chunk_offset
;
2623 sector_t new_sector
;
2624 int algorithm
= previous
? conf
->prev_algo
2626 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2627 : conf
->chunk_sectors
;
2628 int raid_disks
= previous
? conf
->previous_raid_disks
2630 int data_disks
= raid_disks
- conf
->max_degraded
;
2632 /* First compute the information on this sector */
2635 * Compute the chunk number and the sector offset inside the chunk
2637 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
2638 chunk_number
= r_sector
;
2641 * Compute the stripe number
2643 stripe
= chunk_number
;
2644 *dd_idx
= sector_div(stripe
, data_disks
);
2647 * Select the parity disk based on the user selected algorithm.
2649 pd_idx
= qd_idx
= -1;
2650 switch(conf
->level
) {
2652 pd_idx
= data_disks
;
2655 switch (algorithm
) {
2656 case ALGORITHM_LEFT_ASYMMETRIC
:
2657 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2658 if (*dd_idx
>= pd_idx
)
2661 case ALGORITHM_RIGHT_ASYMMETRIC
:
2662 pd_idx
= sector_div(stripe2
, raid_disks
);
2663 if (*dd_idx
>= pd_idx
)
2666 case ALGORITHM_LEFT_SYMMETRIC
:
2667 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2668 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2670 case ALGORITHM_RIGHT_SYMMETRIC
:
2671 pd_idx
= sector_div(stripe2
, raid_disks
);
2672 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2674 case ALGORITHM_PARITY_0
:
2678 case ALGORITHM_PARITY_N
:
2679 pd_idx
= data_disks
;
2687 switch (algorithm
) {
2688 case ALGORITHM_LEFT_ASYMMETRIC
:
2689 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2690 qd_idx
= pd_idx
+ 1;
2691 if (pd_idx
== raid_disks
-1) {
2692 (*dd_idx
)++; /* Q D D D P */
2694 } else if (*dd_idx
>= pd_idx
)
2695 (*dd_idx
) += 2; /* D D P Q D */
2697 case ALGORITHM_RIGHT_ASYMMETRIC
:
2698 pd_idx
= sector_div(stripe2
, raid_disks
);
2699 qd_idx
= pd_idx
+ 1;
2700 if (pd_idx
== raid_disks
-1) {
2701 (*dd_idx
)++; /* Q D D D P */
2703 } else if (*dd_idx
>= pd_idx
)
2704 (*dd_idx
) += 2; /* D D P Q D */
2706 case ALGORITHM_LEFT_SYMMETRIC
:
2707 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2708 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2709 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2711 case ALGORITHM_RIGHT_SYMMETRIC
:
2712 pd_idx
= sector_div(stripe2
, raid_disks
);
2713 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2714 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2717 case ALGORITHM_PARITY_0
:
2722 case ALGORITHM_PARITY_N
:
2723 pd_idx
= data_disks
;
2724 qd_idx
= data_disks
+ 1;
2727 case ALGORITHM_ROTATING_ZERO_RESTART
:
2728 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2729 * of blocks for computing Q is different.
2731 pd_idx
= sector_div(stripe2
, raid_disks
);
2732 qd_idx
= pd_idx
+ 1;
2733 if (pd_idx
== raid_disks
-1) {
2734 (*dd_idx
)++; /* Q D D D P */
2736 } else if (*dd_idx
>= pd_idx
)
2737 (*dd_idx
) += 2; /* D D P Q D */
2741 case ALGORITHM_ROTATING_N_RESTART
:
2742 /* Same a left_asymmetric, by first stripe is
2743 * D D D P Q rather than
2747 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2748 qd_idx
= pd_idx
+ 1;
2749 if (pd_idx
== raid_disks
-1) {
2750 (*dd_idx
)++; /* Q D D D P */
2752 } else if (*dd_idx
>= pd_idx
)
2753 (*dd_idx
) += 2; /* D D P Q D */
2757 case ALGORITHM_ROTATING_N_CONTINUE
:
2758 /* Same as left_symmetric but Q is before P */
2759 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2760 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2761 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2765 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2766 /* RAID5 left_asymmetric, with Q on last device */
2767 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2768 if (*dd_idx
>= pd_idx
)
2770 qd_idx
= raid_disks
- 1;
2773 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2774 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2775 if (*dd_idx
>= pd_idx
)
2777 qd_idx
= raid_disks
- 1;
2780 case ALGORITHM_LEFT_SYMMETRIC_6
:
2781 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2782 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2783 qd_idx
= raid_disks
- 1;
2786 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2787 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2788 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2789 qd_idx
= raid_disks
- 1;
2792 case ALGORITHM_PARITY_0_6
:
2795 qd_idx
= raid_disks
- 1;
2805 sh
->pd_idx
= pd_idx
;
2806 sh
->qd_idx
= qd_idx
;
2807 sh
->ddf_layout
= ddf_layout
;
2810 * Finally, compute the new sector number
2812 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2816 sector_t
raid5_compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2818 struct r5conf
*conf
= sh
->raid_conf
;
2819 int raid_disks
= sh
->disks
;
2820 int data_disks
= raid_disks
- conf
->max_degraded
;
2821 sector_t new_sector
= sh
->sector
, check
;
2822 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2823 : conf
->chunk_sectors
;
2824 int algorithm
= previous
? conf
->prev_algo
2828 sector_t chunk_number
;
2829 int dummy1
, dd_idx
= i
;
2831 struct stripe_head sh2
;
2833 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2834 stripe
= new_sector
;
2836 if (i
== sh
->pd_idx
)
2838 switch(conf
->level
) {
2841 switch (algorithm
) {
2842 case ALGORITHM_LEFT_ASYMMETRIC
:
2843 case ALGORITHM_RIGHT_ASYMMETRIC
:
2847 case ALGORITHM_LEFT_SYMMETRIC
:
2848 case ALGORITHM_RIGHT_SYMMETRIC
:
2851 i
-= (sh
->pd_idx
+ 1);
2853 case ALGORITHM_PARITY_0
:
2856 case ALGORITHM_PARITY_N
:
2863 if (i
== sh
->qd_idx
)
2864 return 0; /* It is the Q disk */
2865 switch (algorithm
) {
2866 case ALGORITHM_LEFT_ASYMMETRIC
:
2867 case ALGORITHM_RIGHT_ASYMMETRIC
:
2868 case ALGORITHM_ROTATING_ZERO_RESTART
:
2869 case ALGORITHM_ROTATING_N_RESTART
:
2870 if (sh
->pd_idx
== raid_disks
-1)
2871 i
--; /* Q D D D P */
2872 else if (i
> sh
->pd_idx
)
2873 i
-= 2; /* D D P Q D */
2875 case ALGORITHM_LEFT_SYMMETRIC
:
2876 case ALGORITHM_RIGHT_SYMMETRIC
:
2877 if (sh
->pd_idx
== raid_disks
-1)
2878 i
--; /* Q D D D P */
2883 i
-= (sh
->pd_idx
+ 2);
2886 case ALGORITHM_PARITY_0
:
2889 case ALGORITHM_PARITY_N
:
2891 case ALGORITHM_ROTATING_N_CONTINUE
:
2892 /* Like left_symmetric, but P is before Q */
2893 if (sh
->pd_idx
== 0)
2894 i
--; /* P D D D Q */
2899 i
-= (sh
->pd_idx
+ 1);
2902 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2903 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2907 case ALGORITHM_LEFT_SYMMETRIC_6
:
2908 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2910 i
+= data_disks
+ 1;
2911 i
-= (sh
->pd_idx
+ 1);
2913 case ALGORITHM_PARITY_0_6
:
2922 chunk_number
= stripe
* data_disks
+ i
;
2923 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2925 check
= raid5_compute_sector(conf
, r_sector
,
2926 previous
, &dummy1
, &sh2
);
2927 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2928 || sh2
.qd_idx
!= sh
->qd_idx
) {
2929 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2930 mdname(conf
->mddev
));
2937 * There are cases where we want handle_stripe_dirtying() and
2938 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2940 * This function checks whether we want to delay the towrite. Specifically,
2941 * we delay the towrite when:
2943 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2944 * stripe has data in journal (for other devices).
2946 * In this case, when reading data for the non-overwrite dev, it is
2947 * necessary to handle complex rmw of write back cache (prexor with
2948 * orig_page, and xor with page). To keep read path simple, we would
2949 * like to flush data in journal to RAID disks first, so complex rmw
2950 * is handled in the write patch (handle_stripe_dirtying).
2952 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
2954 * It is important to be able to flush all stripes in raid5-cache.
2955 * Therefore, we need reserve some space on the journal device for
2956 * these flushes. If flush operation includes pending writes to the
2957 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
2958 * for the flush out. If we exclude these pending writes from flush
2959 * operation, we only need (conf->max_degraded + 1) pages per stripe.
2960 * Therefore, excluding pending writes in these cases enables more
2961 * efficient use of the journal device.
2963 * Note: To make sure the stripe makes progress, we only delay
2964 * towrite for stripes with data already in journal (injournal > 0).
2965 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
2966 * no_space_stripes list.
2969 static inline bool delay_towrite(struct r5conf
*conf
,
2971 struct stripe_head_state
*s
)
2974 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2975 !test_bit(R5_Insync
, &dev
->flags
) && s
->injournal
)
2978 if (test_bit(R5C_LOG_CRITICAL
, &conf
->cache_state
) &&
2985 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2986 int rcw
, int expand
)
2988 int i
, pd_idx
= sh
->pd_idx
, qd_idx
= sh
->qd_idx
, disks
= sh
->disks
;
2989 struct r5conf
*conf
= sh
->raid_conf
;
2990 int level
= conf
->level
;
2994 * In some cases, handle_stripe_dirtying initially decided to
2995 * run rmw and allocates extra page for prexor. However, rcw is
2996 * cheaper later on. We need to free the extra page now,
2997 * because we won't be able to do that in ops_complete_prexor().
2999 r5c_release_extra_page(sh
);
3001 for (i
= disks
; i
--; ) {
3002 struct r5dev
*dev
= &sh
->dev
[i
];
3004 if (dev
->towrite
&& !delay_towrite(conf
, dev
, s
)) {
3005 set_bit(R5_LOCKED
, &dev
->flags
);
3006 set_bit(R5_Wantdrain
, &dev
->flags
);
3008 clear_bit(R5_UPTODATE
, &dev
->flags
);
3010 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
3011 set_bit(R5_LOCKED
, &dev
->flags
);
3015 /* if we are not expanding this is a proper write request, and
3016 * there will be bios with new data to be drained into the
3021 /* False alarm, nothing to do */
3023 sh
->reconstruct_state
= reconstruct_state_drain_run
;
3024 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
3026 sh
->reconstruct_state
= reconstruct_state_run
;
3028 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
3030 if (s
->locked
+ conf
->max_degraded
== disks
)
3031 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3032 atomic_inc(&conf
->pending_full_writes
);
3034 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
3035 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
3036 BUG_ON(level
== 6 &&
3037 (!(test_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
) ||
3038 test_bit(R5_Wantcompute
, &sh
->dev
[qd_idx
].flags
))));
3040 for (i
= disks
; i
--; ) {
3041 struct r5dev
*dev
= &sh
->dev
[i
];
3042 if (i
== pd_idx
|| i
== qd_idx
)
3046 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
3047 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3048 set_bit(R5_Wantdrain
, &dev
->flags
);
3049 set_bit(R5_LOCKED
, &dev
->flags
);
3050 clear_bit(R5_UPTODATE
, &dev
->flags
);
3052 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
3053 set_bit(R5_LOCKED
, &dev
->flags
);
3058 /* False alarm - nothing to do */
3060 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
3061 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
3062 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
3063 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
3066 /* keep the parity disk(s) locked while asynchronous operations
3069 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
3070 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3074 int qd_idx
= sh
->qd_idx
;
3075 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
3077 set_bit(R5_LOCKED
, &dev
->flags
);
3078 clear_bit(R5_UPTODATE
, &dev
->flags
);
3082 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
3083 __func__
, (unsigned long long)sh
->sector
,
3084 s
->locked
, s
->ops_request
);
3088 * Each stripe/dev can have one or more bion attached.
3089 * toread/towrite point to the first in a chain.
3090 * The bi_next chain must be in order.
3092 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
,
3093 int forwrite
, int previous
)
3096 struct r5conf
*conf
= sh
->raid_conf
;
3099 pr_debug("adding bi b#%llu to stripe s#%llu\n",
3100 (unsigned long long)bi
->bi_iter
.bi_sector
,
3101 (unsigned long long)sh
->sector
);
3104 * If several bio share a stripe. The bio bi_phys_segments acts as a
3105 * reference count to avoid race. The reference count should already be
3106 * increased before this function is called (for example, in
3107 * raid5_make_request()), so other bio sharing this stripe will not free the
3108 * stripe. If a stripe is owned by one stripe, the stripe lock will
3111 spin_lock_irq(&sh
->stripe_lock
);
3112 /* Don't allow new IO added to stripes in batch list */
3116 bip
= &sh
->dev
[dd_idx
].towrite
;
3120 bip
= &sh
->dev
[dd_idx
].toread
;
3121 while (*bip
&& (*bip
)->bi_iter
.bi_sector
< bi
->bi_iter
.bi_sector
) {
3122 if (bio_end_sector(*bip
) > bi
->bi_iter
.bi_sector
)
3124 bip
= & (*bip
)->bi_next
;
3126 if (*bip
&& (*bip
)->bi_iter
.bi_sector
< bio_end_sector(bi
))
3129 if (!forwrite
|| previous
)
3130 clear_bit(STRIPE_BATCH_READY
, &sh
->state
);
3132 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
3136 raid5_inc_bi_active_stripes(bi
);
3139 /* check if page is covered */
3140 sector_t sector
= sh
->dev
[dd_idx
].sector
;
3141 for (bi
=sh
->dev
[dd_idx
].towrite
;
3142 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
3143 bi
&& bi
->bi_iter
.bi_sector
<= sector
;
3144 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
3145 if (bio_end_sector(bi
) >= sector
)
3146 sector
= bio_end_sector(bi
);
3148 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
3149 if (!test_and_set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
))
3150 sh
->overwrite_disks
++;
3153 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
3154 (unsigned long long)(*bip
)->bi_iter
.bi_sector
,
3155 (unsigned long long)sh
->sector
, dd_idx
);
3157 if (conf
->mddev
->bitmap
&& firstwrite
) {
3158 /* Cannot hold spinlock over bitmap_startwrite,
3159 * but must ensure this isn't added to a batch until
3160 * we have added to the bitmap and set bm_seq.
3161 * So set STRIPE_BITMAP_PENDING to prevent
3163 * If multiple add_stripe_bio() calls race here they
3164 * much all set STRIPE_BITMAP_PENDING. So only the first one
3165 * to complete "bitmap_startwrite" gets to set
3166 * STRIPE_BIT_DELAY. This is important as once a stripe
3167 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3170 set_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3171 spin_unlock_irq(&sh
->stripe_lock
);
3172 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
3174 spin_lock_irq(&sh
->stripe_lock
);
3175 clear_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3176 if (!sh
->batch_head
) {
3177 sh
->bm_seq
= conf
->seq_flush
+1;
3178 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
3181 spin_unlock_irq(&sh
->stripe_lock
);
3183 if (stripe_can_batch(sh
))
3184 stripe_add_to_batch_list(conf
, sh
);
3188 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
3189 spin_unlock_irq(&sh
->stripe_lock
);
3193 static void end_reshape(struct r5conf
*conf
);
3195 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
3196 struct stripe_head
*sh
)
3198 int sectors_per_chunk
=
3199 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
3201 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
3202 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
3204 raid5_compute_sector(conf
,
3205 stripe
* (disks
- conf
->max_degraded
)
3206 *sectors_per_chunk
+ chunk_offset
,
3212 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
3213 struct stripe_head_state
*s
, int disks
,
3214 struct bio_list
*return_bi
)
3217 BUG_ON(sh
->batch_head
);
3218 for (i
= disks
; i
--; ) {
3222 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
3223 struct md_rdev
*rdev
;
3225 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3226 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
3227 !test_bit(Faulty
, &rdev
->flags
))
3228 atomic_inc(&rdev
->nr_pending
);
3233 if (!rdev_set_badblocks(
3237 md_error(conf
->mddev
, rdev
);
3238 rdev_dec_pending(rdev
, conf
->mddev
);
3241 spin_lock_irq(&sh
->stripe_lock
);
3242 /* fail all writes first */
3243 bi
= sh
->dev
[i
].towrite
;
3244 sh
->dev
[i
].towrite
= NULL
;
3245 sh
->overwrite_disks
= 0;
3246 spin_unlock_irq(&sh
->stripe_lock
);
3250 r5l_stripe_write_finished(sh
);
3252 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3253 wake_up(&conf
->wait_for_overlap
);
3255 while (bi
&& bi
->bi_iter
.bi_sector
<
3256 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3257 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3259 bi
->bi_error
= -EIO
;
3260 if (!raid5_dec_bi_active_stripes(bi
)) {
3261 md_write_end(conf
->mddev
);
3262 bio_list_add(return_bi
, bi
);
3267 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3268 STRIPE_SECTORS
, 0, 0);
3270 /* and fail all 'written' */
3271 bi
= sh
->dev
[i
].written
;
3272 sh
->dev
[i
].written
= NULL
;
3273 if (test_and_clear_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
)) {
3274 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
3275 sh
->dev
[i
].page
= sh
->dev
[i
].orig_page
;
3278 if (bi
) bitmap_end
= 1;
3279 while (bi
&& bi
->bi_iter
.bi_sector
<
3280 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3281 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3283 bi
->bi_error
= -EIO
;
3284 if (!raid5_dec_bi_active_stripes(bi
)) {
3285 md_write_end(conf
->mddev
);
3286 bio_list_add(return_bi
, bi
);
3291 /* fail any reads if this device is non-operational and
3292 * the data has not reached the cache yet.
3294 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
3295 s
->failed
> conf
->max_degraded
&&
3296 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
3297 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
3298 spin_lock_irq(&sh
->stripe_lock
);
3299 bi
= sh
->dev
[i
].toread
;
3300 sh
->dev
[i
].toread
= NULL
;
3301 spin_unlock_irq(&sh
->stripe_lock
);
3302 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3303 wake_up(&conf
->wait_for_overlap
);
3306 while (bi
&& bi
->bi_iter
.bi_sector
<
3307 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3308 struct bio
*nextbi
=
3309 r5_next_bio(bi
, sh
->dev
[i
].sector
);
3311 bi
->bi_error
= -EIO
;
3312 if (!raid5_dec_bi_active_stripes(bi
))
3313 bio_list_add(return_bi
, bi
);
3318 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3319 STRIPE_SECTORS
, 0, 0);
3320 /* If we were in the middle of a write the parity block might
3321 * still be locked - so just clear all R5_LOCKED flags
3323 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3328 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3329 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3330 md_wakeup_thread(conf
->mddev
->thread
);
3334 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
3335 struct stripe_head_state
*s
)
3340 BUG_ON(sh
->batch_head
);
3341 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3342 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
3343 wake_up(&conf
->wait_for_overlap
);
3346 /* There is nothing more to do for sync/check/repair.
3347 * Don't even need to abort as that is handled elsewhere
3348 * if needed, and not always wanted e.g. if there is a known
3350 * For recover/replace we need to record a bad block on all
3351 * non-sync devices, or abort the recovery
3353 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
3354 /* During recovery devices cannot be removed, so
3355 * locking and refcounting of rdevs is not needed
3358 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3359 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3361 && !test_bit(Faulty
, &rdev
->flags
)
3362 && !test_bit(In_sync
, &rdev
->flags
)
3363 && !rdev_set_badblocks(rdev
, sh
->sector
,
3366 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3368 && !test_bit(Faulty
, &rdev
->flags
)
3369 && !test_bit(In_sync
, &rdev
->flags
)
3370 && !rdev_set_badblocks(rdev
, sh
->sector
,
3376 conf
->recovery_disabled
=
3377 conf
->mddev
->recovery_disabled
;
3379 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
3382 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
3384 struct md_rdev
*rdev
;
3388 rdev
= rcu_dereference(sh
->raid_conf
->disks
[disk_idx
].replacement
);
3390 && !test_bit(Faulty
, &rdev
->flags
)
3391 && !test_bit(In_sync
, &rdev
->flags
)
3392 && (rdev
->recovery_offset
<= sh
->sector
3393 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
3399 static int need_this_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3400 int disk_idx
, int disks
)
3402 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3403 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
3404 &sh
->dev
[s
->failed_num
[1]] };
3408 if (test_bit(R5_LOCKED
, &dev
->flags
) ||
3409 test_bit(R5_UPTODATE
, &dev
->flags
))
3410 /* No point reading this as we already have it or have
3411 * decided to get it.
3416 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)))
3417 /* We need this block to directly satisfy a request */
3420 if (s
->syncing
|| s
->expanding
||
3421 (s
->replacing
&& want_replace(sh
, disk_idx
)))
3422 /* When syncing, or expanding we read everything.
3423 * When replacing, we need the replaced block.
3427 if ((s
->failed
>= 1 && fdev
[0]->toread
) ||
3428 (s
->failed
>= 2 && fdev
[1]->toread
))
3429 /* If we want to read from a failed device, then
3430 * we need to actually read every other device.
3434 /* Sometimes neither read-modify-write nor reconstruct-write
3435 * cycles can work. In those cases we read every block we
3436 * can. Then the parity-update is certain to have enough to
3438 * This can only be a problem when we need to write something,
3439 * and some device has failed. If either of those tests
3440 * fail we need look no further.
3442 if (!s
->failed
|| !s
->to_write
)
3445 if (test_bit(R5_Insync
, &dev
->flags
) &&
3446 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3447 /* Pre-reads at not permitted until after short delay
3448 * to gather multiple requests. However if this
3449 * device is no Insync, the block could only be be computed
3450 * and there is no need to delay that.
3454 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3455 if (fdev
[i
]->towrite
&&
3456 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3457 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3458 /* If we have a partial write to a failed
3459 * device, then we will need to reconstruct
3460 * the content of that device, so all other
3461 * devices must be read.
3466 /* If we are forced to do a reconstruct-write, either because
3467 * the current RAID6 implementation only supports that, or
3468 * or because parity cannot be trusted and we are currently
3469 * recovering it, there is extra need to be careful.
3470 * If one of the devices that we would need to read, because
3471 * it is not being overwritten (and maybe not written at all)
3472 * is missing/faulty, then we need to read everything we can.
3474 if (sh
->raid_conf
->level
!= 6 &&
3475 sh
->sector
< sh
->raid_conf
->mddev
->recovery_cp
)
3476 /* reconstruct-write isn't being forced */
3478 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3479 if (s
->failed_num
[i
] != sh
->pd_idx
&&
3480 s
->failed_num
[i
] != sh
->qd_idx
&&
3481 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3482 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3489 /* fetch_block - checks the given member device to see if its data needs
3490 * to be read or computed to satisfy a request.
3492 * Returns 1 when no more member devices need to be checked, otherwise returns
3493 * 0 to tell the loop in handle_stripe_fill to continue
3495 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3496 int disk_idx
, int disks
)
3498 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3500 /* is the data in this block needed, and can we get it? */
3501 if (need_this_block(sh
, s
, disk_idx
, disks
)) {
3502 /* we would like to get this block, possibly by computing it,
3503 * otherwise read it if the backing disk is insync
3505 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
3506 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
3507 BUG_ON(sh
->batch_head
);
3508 if ((s
->uptodate
== disks
- 1) &&
3509 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
3510 disk_idx
== s
->failed_num
[1]))) {
3511 /* have disk failed, and we're requested to fetch it;
3514 pr_debug("Computing stripe %llu block %d\n",
3515 (unsigned long long)sh
->sector
, disk_idx
);
3516 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3517 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3518 set_bit(R5_Wantcompute
, &dev
->flags
);
3519 sh
->ops
.target
= disk_idx
;
3520 sh
->ops
.target2
= -1; /* no 2nd target */
3522 /* Careful: from this point on 'uptodate' is in the eye
3523 * of raid_run_ops which services 'compute' operations
3524 * before writes. R5_Wantcompute flags a block that will
3525 * be R5_UPTODATE by the time it is needed for a
3526 * subsequent operation.
3530 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
3531 /* Computing 2-failure is *very* expensive; only
3532 * do it if failed >= 2
3535 for (other
= disks
; other
--; ) {
3536 if (other
== disk_idx
)
3538 if (!test_bit(R5_UPTODATE
,
3539 &sh
->dev
[other
].flags
))
3543 pr_debug("Computing stripe %llu blocks %d,%d\n",
3544 (unsigned long long)sh
->sector
,
3546 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3547 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3548 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
3549 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
3550 sh
->ops
.target
= disk_idx
;
3551 sh
->ops
.target2
= other
;
3555 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
3556 set_bit(R5_LOCKED
, &dev
->flags
);
3557 set_bit(R5_Wantread
, &dev
->flags
);
3559 pr_debug("Reading block %d (sync=%d)\n",
3560 disk_idx
, s
->syncing
);
3568 * handle_stripe_fill - read or compute data to satisfy pending requests.
3570 static void handle_stripe_fill(struct stripe_head
*sh
,
3571 struct stripe_head_state
*s
,
3576 /* look for blocks to read/compute, skip this if a compute
3577 * is already in flight, or if the stripe contents are in the
3578 * midst of changing due to a write
3580 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
3581 !sh
->reconstruct_state
) {
3584 * For degraded stripe with data in journal, do not handle
3585 * read requests yet, instead, flush the stripe to raid
3586 * disks first, this avoids handling complex rmw of write
3587 * back cache (prexor with orig_page, and then xor with
3588 * page) in the read path
3590 if (s
->injournal
&& s
->failed
) {
3591 if (test_bit(STRIPE_R5C_CACHING
, &sh
->state
))
3592 r5c_make_stripe_write_out(sh
);
3596 for (i
= disks
; i
--; )
3597 if (fetch_block(sh
, s
, i
, disks
))
3601 set_bit(STRIPE_HANDLE
, &sh
->state
);
3604 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
3605 unsigned long handle_flags
);
3606 /* handle_stripe_clean_event
3607 * any written block on an uptodate or failed drive can be returned.
3608 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3609 * never LOCKED, so we don't need to test 'failed' directly.
3611 static void handle_stripe_clean_event(struct r5conf
*conf
,
3612 struct stripe_head
*sh
, int disks
, struct bio_list
*return_bi
)
3616 int discard_pending
= 0;
3617 struct stripe_head
*head_sh
= sh
;
3618 bool do_endio
= false;
3620 for (i
= disks
; i
--; )
3621 if (sh
->dev
[i
].written
) {
3623 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
3624 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
3625 test_bit(R5_Discard
, &dev
->flags
) ||
3626 test_bit(R5_SkipCopy
, &dev
->flags
))) {
3627 /* We can return any write requests */
3628 struct bio
*wbi
, *wbi2
;
3629 pr_debug("Return write for disc %d\n", i
);
3630 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
3631 clear_bit(R5_UPTODATE
, &dev
->flags
);
3632 if (test_and_clear_bit(R5_SkipCopy
, &dev
->flags
)) {
3633 WARN_ON(test_bit(R5_UPTODATE
, &dev
->flags
));
3638 dev
->page
= dev
->orig_page
;
3640 dev
->written
= NULL
;
3641 while (wbi
&& wbi
->bi_iter
.bi_sector
<
3642 dev
->sector
+ STRIPE_SECTORS
) {
3643 wbi2
= r5_next_bio(wbi
, dev
->sector
);
3644 if (!raid5_dec_bi_active_stripes(wbi
)) {
3645 md_write_end(conf
->mddev
);
3646 bio_list_add(return_bi
, wbi
);
3650 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3652 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
3654 if (head_sh
->batch_head
) {
3655 sh
= list_first_entry(&sh
->batch_list
,
3658 if (sh
!= head_sh
) {
3665 } else if (test_bit(R5_Discard
, &dev
->flags
))
3666 discard_pending
= 1;
3669 r5l_stripe_write_finished(sh
);
3671 if (!discard_pending
&&
3672 test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3674 clear_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
3675 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3676 if (sh
->qd_idx
>= 0) {
3677 clear_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
3678 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
);
3680 /* now that discard is done we can proceed with any sync */
3681 clear_bit(STRIPE_DISCARD
, &sh
->state
);
3683 * SCSI discard will change some bio fields and the stripe has
3684 * no updated data, so remove it from hash list and the stripe
3685 * will be reinitialized
3688 hash
= sh
->hash_lock_index
;
3689 spin_lock_irq(conf
->hash_locks
+ hash
);
3691 spin_unlock_irq(conf
->hash_locks
+ hash
);
3692 if (head_sh
->batch_head
) {
3693 sh
= list_first_entry(&sh
->batch_list
,
3694 struct stripe_head
, batch_list
);
3700 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
))
3701 set_bit(STRIPE_HANDLE
, &sh
->state
);
3705 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3706 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3707 md_wakeup_thread(conf
->mddev
->thread
);
3709 if (head_sh
->batch_head
&& do_endio
)
3710 break_stripe_batch_list(head_sh
, STRIPE_EXPAND_SYNC_FLAGS
);
3714 * For RMW in write back cache, we need extra page in prexor to store the
3715 * old data. This page is stored in dev->orig_page.
3717 * This function checks whether we have data for prexor. The exact logic
3719 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3721 static inline bool uptodate_for_rmw(struct r5dev
*dev
)
3723 return (test_bit(R5_UPTODATE
, &dev
->flags
)) &&
3724 (!test_bit(R5_InJournal
, &dev
->flags
) ||
3725 test_bit(R5_OrigPageUPTDODATE
, &dev
->flags
));
3728 static int handle_stripe_dirtying(struct r5conf
*conf
,
3729 struct stripe_head
*sh
,
3730 struct stripe_head_state
*s
,
3733 int rmw
= 0, rcw
= 0, i
;
3734 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
3736 /* Check whether resync is now happening or should start.
3737 * If yes, then the array is dirty (after unclean shutdown or
3738 * initial creation), so parity in some stripes might be inconsistent.
3739 * In this case, we need to always do reconstruct-write, to ensure
3740 * that in case of drive failure or read-error correction, we
3741 * generate correct data from the parity.
3743 if (conf
->rmw_level
== PARITY_DISABLE_RMW
||
3744 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
&&
3746 /* Calculate the real rcw later - for now make it
3747 * look like rcw is cheaper
3750 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3751 conf
->rmw_level
, (unsigned long long)recovery_cp
,
3752 (unsigned long long)sh
->sector
);
3753 } else for (i
= disks
; i
--; ) {
3754 /* would I have to read this buffer for read_modify_write */
3755 struct r5dev
*dev
= &sh
->dev
[i
];
3756 if (((dev
->towrite
&& !delay_towrite(conf
, dev
, s
)) ||
3757 i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3758 test_bit(R5_InJournal
, &dev
->flags
)) &&
3759 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3760 !(uptodate_for_rmw(dev
) ||
3761 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3762 if (test_bit(R5_Insync
, &dev
->flags
))
3765 rmw
+= 2*disks
; /* cannot read it */
3767 /* Would I have to read this buffer for reconstruct_write */
3768 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3769 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3770 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3771 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3772 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3773 if (test_bit(R5_Insync
, &dev
->flags
))
3780 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3781 (unsigned long long)sh
->sector
, sh
->state
, rmw
, rcw
);
3782 set_bit(STRIPE_HANDLE
, &sh
->state
);
3783 if ((rmw
< rcw
|| (rmw
== rcw
&& conf
->rmw_level
== PARITY_PREFER_RMW
)) && rmw
> 0) {
3784 /* prefer read-modify-write, but need to get some data */
3785 if (conf
->mddev
->queue
)
3786 blk_add_trace_msg(conf
->mddev
->queue
,
3787 "raid5 rmw %llu %d",
3788 (unsigned long long)sh
->sector
, rmw
);
3789 for (i
= disks
; i
--; ) {
3790 struct r5dev
*dev
= &sh
->dev
[i
];
3791 if (test_bit(R5_InJournal
, &dev
->flags
) &&
3792 dev
->page
== dev
->orig_page
&&
3793 !test_bit(R5_LOCKED
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3794 /* alloc page for prexor */
3795 struct page
*p
= alloc_page(GFP_NOIO
);
3803 * alloc_page() failed, try use
3804 * disk_info->extra_page
3806 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE
,
3807 &conf
->cache_state
)) {
3808 r5c_use_extra_page(sh
);
3812 /* extra_page in use, add to delayed_list */
3813 set_bit(STRIPE_DELAYED
, &sh
->state
);
3814 s
->waiting_extra_page
= 1;
3819 for (i
= disks
; i
--; ) {
3820 struct r5dev
*dev
= &sh
->dev
[i
];
3821 if (((dev
->towrite
&& !delay_towrite(conf
, dev
, s
)) ||
3822 i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3823 test_bit(R5_InJournal
, &dev
->flags
)) &&
3824 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3825 !(uptodate_for_rmw(dev
) ||
3826 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
3827 test_bit(R5_Insync
, &dev
->flags
)) {
3828 if (test_bit(STRIPE_PREREAD_ACTIVE
,
3830 pr_debug("Read_old block %d for r-m-w\n",
3832 set_bit(R5_LOCKED
, &dev
->flags
);
3833 set_bit(R5_Wantread
, &dev
->flags
);
3836 set_bit(STRIPE_DELAYED
, &sh
->state
);
3837 set_bit(STRIPE_HANDLE
, &sh
->state
);
3842 if ((rcw
< rmw
|| (rcw
== rmw
&& conf
->rmw_level
!= PARITY_PREFER_RMW
)) && rcw
> 0) {
3843 /* want reconstruct write, but need to get some data */
3846 for (i
= disks
; i
--; ) {
3847 struct r5dev
*dev
= &sh
->dev
[i
];
3848 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3849 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3850 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3851 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3852 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3854 if (test_bit(R5_Insync
, &dev
->flags
) &&
3855 test_bit(STRIPE_PREREAD_ACTIVE
,
3857 pr_debug("Read_old block "
3858 "%d for Reconstruct\n", i
);
3859 set_bit(R5_LOCKED
, &dev
->flags
);
3860 set_bit(R5_Wantread
, &dev
->flags
);
3864 set_bit(STRIPE_DELAYED
, &sh
->state
);
3865 set_bit(STRIPE_HANDLE
, &sh
->state
);
3869 if (rcw
&& conf
->mddev
->queue
)
3870 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
3871 (unsigned long long)sh
->sector
,
3872 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
3875 if (rcw
> disks
&& rmw
> disks
&&
3876 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3877 set_bit(STRIPE_DELAYED
, &sh
->state
);
3879 /* now if nothing is locked, and if we have enough data,
3880 * we can start a write request
3882 /* since handle_stripe can be called at any time we need to handle the
3883 * case where a compute block operation has been submitted and then a
3884 * subsequent call wants to start a write request. raid_run_ops only
3885 * handles the case where compute block and reconstruct are requested
3886 * simultaneously. If this is not the case then new writes need to be
3887 * held off until the compute completes.
3889 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
3890 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
3891 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
3892 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
3896 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
3897 struct stripe_head_state
*s
, int disks
)
3899 struct r5dev
*dev
= NULL
;
3901 BUG_ON(sh
->batch_head
);
3902 set_bit(STRIPE_HANDLE
, &sh
->state
);
3904 switch (sh
->check_state
) {
3905 case check_state_idle
:
3906 /* start a new check operation if there are no failures */
3907 if (s
->failed
== 0) {
3908 BUG_ON(s
->uptodate
!= disks
);
3909 sh
->check_state
= check_state_run
;
3910 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3911 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3915 dev
= &sh
->dev
[s
->failed_num
[0]];
3917 case check_state_compute_result
:
3918 sh
->check_state
= check_state_idle
;
3920 dev
= &sh
->dev
[sh
->pd_idx
];
3922 /* check that a write has not made the stripe insync */
3923 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3926 /* either failed parity check, or recovery is happening */
3927 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
3928 BUG_ON(s
->uptodate
!= disks
);
3930 set_bit(R5_LOCKED
, &dev
->flags
);
3932 set_bit(R5_Wantwrite
, &dev
->flags
);
3934 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3935 set_bit(STRIPE_INSYNC
, &sh
->state
);
3937 case check_state_run
:
3938 break; /* we will be called again upon completion */
3939 case check_state_check_result
:
3940 sh
->check_state
= check_state_idle
;
3942 /* if a failure occurred during the check operation, leave
3943 * STRIPE_INSYNC not set and let the stripe be handled again
3948 /* handle a successful check operation, if parity is correct
3949 * we are done. Otherwise update the mismatch count and repair
3950 * parity if !MD_RECOVERY_CHECK
3952 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
3953 /* parity is correct (on disc,
3954 * not in buffer any more)
3956 set_bit(STRIPE_INSYNC
, &sh
->state
);
3958 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3959 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3960 /* don't try to repair!! */
3961 set_bit(STRIPE_INSYNC
, &sh
->state
);
3963 sh
->check_state
= check_state_compute_run
;
3964 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3965 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3966 set_bit(R5_Wantcompute
,
3967 &sh
->dev
[sh
->pd_idx
].flags
);
3968 sh
->ops
.target
= sh
->pd_idx
;
3969 sh
->ops
.target2
= -1;
3974 case check_state_compute_run
:
3977 pr_err("%s: unknown check_state: %d sector: %llu\n",
3978 __func__
, sh
->check_state
,
3979 (unsigned long long) sh
->sector
);
3984 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
3985 struct stripe_head_state
*s
,
3988 int pd_idx
= sh
->pd_idx
;
3989 int qd_idx
= sh
->qd_idx
;
3992 BUG_ON(sh
->batch_head
);
3993 set_bit(STRIPE_HANDLE
, &sh
->state
);
3995 BUG_ON(s
->failed
> 2);
3997 /* Want to check and possibly repair P and Q.
3998 * However there could be one 'failed' device, in which
3999 * case we can only check one of them, possibly using the
4000 * other to generate missing data
4003 switch (sh
->check_state
) {
4004 case check_state_idle
:
4005 /* start a new check operation if there are < 2 failures */
4006 if (s
->failed
== s
->q_failed
) {
4007 /* The only possible failed device holds Q, so it
4008 * makes sense to check P (If anything else were failed,
4009 * we would have used P to recreate it).
4011 sh
->check_state
= check_state_run
;
4013 if (!s
->q_failed
&& s
->failed
< 2) {
4014 /* Q is not failed, and we didn't use it to generate
4015 * anything, so it makes sense to check it
4017 if (sh
->check_state
== check_state_run
)
4018 sh
->check_state
= check_state_run_pq
;
4020 sh
->check_state
= check_state_run_q
;
4023 /* discard potentially stale zero_sum_result */
4024 sh
->ops
.zero_sum_result
= 0;
4026 if (sh
->check_state
== check_state_run
) {
4027 /* async_xor_zero_sum destroys the contents of P */
4028 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
4031 if (sh
->check_state
>= check_state_run
&&
4032 sh
->check_state
<= check_state_run_pq
) {
4033 /* async_syndrome_zero_sum preserves P and Q, so
4034 * no need to mark them !uptodate here
4036 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
4040 /* we have 2-disk failure */
4041 BUG_ON(s
->failed
!= 2);
4043 case check_state_compute_result
:
4044 sh
->check_state
= check_state_idle
;
4046 /* check that a write has not made the stripe insync */
4047 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
4050 /* now write out any block on a failed drive,
4051 * or P or Q if they were recomputed
4053 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
4054 if (s
->failed
== 2) {
4055 dev
= &sh
->dev
[s
->failed_num
[1]];
4057 set_bit(R5_LOCKED
, &dev
->flags
);
4058 set_bit(R5_Wantwrite
, &dev
->flags
);
4060 if (s
->failed
>= 1) {
4061 dev
= &sh
->dev
[s
->failed_num
[0]];
4063 set_bit(R5_LOCKED
, &dev
->flags
);
4064 set_bit(R5_Wantwrite
, &dev
->flags
);
4066 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
4067 dev
= &sh
->dev
[pd_idx
];
4069 set_bit(R5_LOCKED
, &dev
->flags
);
4070 set_bit(R5_Wantwrite
, &dev
->flags
);
4072 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
4073 dev
= &sh
->dev
[qd_idx
];
4075 set_bit(R5_LOCKED
, &dev
->flags
);
4076 set_bit(R5_Wantwrite
, &dev
->flags
);
4078 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
4080 set_bit(STRIPE_INSYNC
, &sh
->state
);
4082 case check_state_run
:
4083 case check_state_run_q
:
4084 case check_state_run_pq
:
4085 break; /* we will be called again upon completion */
4086 case check_state_check_result
:
4087 sh
->check_state
= check_state_idle
;
4089 /* handle a successful check operation, if parity is correct
4090 * we are done. Otherwise update the mismatch count and repair
4091 * parity if !MD_RECOVERY_CHECK
4093 if (sh
->ops
.zero_sum_result
== 0) {
4094 /* both parities are correct */
4096 set_bit(STRIPE_INSYNC
, &sh
->state
);
4098 /* in contrast to the raid5 case we can validate
4099 * parity, but still have a failure to write
4102 sh
->check_state
= check_state_compute_result
;
4103 /* Returning at this point means that we may go
4104 * off and bring p and/or q uptodate again so
4105 * we make sure to check zero_sum_result again
4106 * to verify if p or q need writeback
4110 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
4111 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
4112 /* don't try to repair!! */
4113 set_bit(STRIPE_INSYNC
, &sh
->state
);
4115 int *target
= &sh
->ops
.target
;
4117 sh
->ops
.target
= -1;
4118 sh
->ops
.target2
= -1;
4119 sh
->check_state
= check_state_compute_run
;
4120 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
4121 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
4122 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
4123 set_bit(R5_Wantcompute
,
4124 &sh
->dev
[pd_idx
].flags
);
4126 target
= &sh
->ops
.target2
;
4129 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
4130 set_bit(R5_Wantcompute
,
4131 &sh
->dev
[qd_idx
].flags
);
4138 case check_state_compute_run
:
4141 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4142 __func__
, sh
->check_state
,
4143 (unsigned long long) sh
->sector
);
4148 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
4152 /* We have read all the blocks in this stripe and now we need to
4153 * copy some of them into a target stripe for expand.
4155 struct dma_async_tx_descriptor
*tx
= NULL
;
4156 BUG_ON(sh
->batch_head
);
4157 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4158 for (i
= 0; i
< sh
->disks
; i
++)
4159 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
4161 struct stripe_head
*sh2
;
4162 struct async_submit_ctl submit
;
4164 sector_t bn
= raid5_compute_blocknr(sh
, i
, 1);
4165 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
4167 sh2
= raid5_get_active_stripe(conf
, s
, 0, 1, 1);
4169 /* so far only the early blocks of this stripe
4170 * have been requested. When later blocks
4171 * get requested, we will try again
4174 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
4175 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
4176 /* must have already done this block */
4177 raid5_release_stripe(sh2
);
4181 /* place all the copies on one channel */
4182 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
4183 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
4184 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
4187 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
4188 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
4189 for (j
= 0; j
< conf
->raid_disks
; j
++)
4190 if (j
!= sh2
->pd_idx
&&
4192 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
4194 if (j
== conf
->raid_disks
) {
4195 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
4196 set_bit(STRIPE_HANDLE
, &sh2
->state
);
4198 raid5_release_stripe(sh2
);
4201 /* done submitting copies, wait for them to complete */
4202 async_tx_quiesce(&tx
);
4206 * handle_stripe - do things to a stripe.
4208 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4209 * state of various bits to see what needs to be done.
4211 * return some read requests which now have data
4212 * return some write requests which are safely on storage
4213 * schedule a read on some buffers
4214 * schedule a write of some buffers
4215 * return confirmation of parity correctness
4219 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
4221 struct r5conf
*conf
= sh
->raid_conf
;
4222 int disks
= sh
->disks
;
4225 int do_recovery
= 0;
4227 memset(s
, 0, sizeof(*s
));
4229 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
) && !sh
->batch_head
;
4230 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
) && !sh
->batch_head
;
4231 s
->failed_num
[0] = -1;
4232 s
->failed_num
[1] = -1;
4233 s
->log_failed
= r5l_log_disk_error(conf
);
4235 /* Now to look around and see what can be done */
4237 for (i
=disks
; i
--; ) {
4238 struct md_rdev
*rdev
;
4245 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4247 dev
->toread
, dev
->towrite
, dev
->written
);
4248 /* maybe we can reply to a read
4250 * new wantfill requests are only permitted while
4251 * ops_complete_biofill is guaranteed to be inactive
4253 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
4254 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
4255 set_bit(R5_Wantfill
, &dev
->flags
);
4257 /* now count some things */
4258 if (test_bit(R5_LOCKED
, &dev
->flags
))
4260 if (test_bit(R5_UPTODATE
, &dev
->flags
))
4262 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
4264 BUG_ON(s
->compute
> 2);
4267 if (test_bit(R5_Wantfill
, &dev
->flags
))
4269 else if (dev
->toread
)
4273 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
4278 /* Prefer to use the replacement for reads, but only
4279 * if it is recovered enough and has no bad blocks.
4281 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
4282 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
4283 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
4284 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4285 &first_bad
, &bad_sectors
))
4286 set_bit(R5_ReadRepl
, &dev
->flags
);
4288 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4289 set_bit(R5_NeedReplace
, &dev
->flags
);
4291 clear_bit(R5_NeedReplace
, &dev
->flags
);
4292 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
4293 clear_bit(R5_ReadRepl
, &dev
->flags
);
4295 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
4298 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4299 &first_bad
, &bad_sectors
);
4300 if (s
->blocked_rdev
== NULL
4301 && (test_bit(Blocked
, &rdev
->flags
)
4304 set_bit(BlockedBadBlocks
,
4306 s
->blocked_rdev
= rdev
;
4307 atomic_inc(&rdev
->nr_pending
);
4310 clear_bit(R5_Insync
, &dev
->flags
);
4314 /* also not in-sync */
4315 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
4316 test_bit(R5_UPTODATE
, &dev
->flags
)) {
4317 /* treat as in-sync, but with a read error
4318 * which we can now try to correct
4320 set_bit(R5_Insync
, &dev
->flags
);
4321 set_bit(R5_ReadError
, &dev
->flags
);
4323 } else if (test_bit(In_sync
, &rdev
->flags
))
4324 set_bit(R5_Insync
, &dev
->flags
);
4325 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
4326 /* in sync if before recovery_offset */
4327 set_bit(R5_Insync
, &dev
->flags
);
4328 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
4329 test_bit(R5_Expanded
, &dev
->flags
))
4330 /* If we've reshaped into here, we assume it is Insync.
4331 * We will shortly update recovery_offset to make
4334 set_bit(R5_Insync
, &dev
->flags
);
4336 if (test_bit(R5_WriteError
, &dev
->flags
)) {
4337 /* This flag does not apply to '.replacement'
4338 * only to .rdev, so make sure to check that*/
4339 struct md_rdev
*rdev2
= rcu_dereference(
4340 conf
->disks
[i
].rdev
);
4342 clear_bit(R5_Insync
, &dev
->flags
);
4343 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4344 s
->handle_bad_blocks
= 1;
4345 atomic_inc(&rdev2
->nr_pending
);
4347 clear_bit(R5_WriteError
, &dev
->flags
);
4349 if (test_bit(R5_MadeGood
, &dev
->flags
)) {
4350 /* This flag does not apply to '.replacement'
4351 * only to .rdev, so make sure to check that*/
4352 struct md_rdev
*rdev2
= rcu_dereference(
4353 conf
->disks
[i
].rdev
);
4354 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4355 s
->handle_bad_blocks
= 1;
4356 atomic_inc(&rdev2
->nr_pending
);
4358 clear_bit(R5_MadeGood
, &dev
->flags
);
4360 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4361 struct md_rdev
*rdev2
= rcu_dereference(
4362 conf
->disks
[i
].replacement
);
4363 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4364 s
->handle_bad_blocks
= 1;
4365 atomic_inc(&rdev2
->nr_pending
);
4367 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
4369 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4370 /* The ReadError flag will just be confusing now */
4371 clear_bit(R5_ReadError
, &dev
->flags
);
4372 clear_bit(R5_ReWrite
, &dev
->flags
);
4374 if (test_bit(R5_ReadError
, &dev
->flags
))
4375 clear_bit(R5_Insync
, &dev
->flags
);
4376 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4378 s
->failed_num
[s
->failed
] = i
;
4380 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4384 if (test_bit(R5_InJournal
, &dev
->flags
))
4386 if (test_bit(R5_InJournal
, &dev
->flags
) && dev
->written
)
4389 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
4390 /* If there is a failed device being replaced,
4391 * we must be recovering.
4392 * else if we are after recovery_cp, we must be syncing
4393 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4394 * else we can only be replacing
4395 * sync and recovery both need to read all devices, and so
4396 * use the same flag.
4399 sh
->sector
>= conf
->mddev
->recovery_cp
||
4400 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
4408 static int clear_batch_ready(struct stripe_head
*sh
)
4410 /* Return '1' if this is a member of batch, or
4411 * '0' if it is a lone stripe or a head which can now be
4414 struct stripe_head
*tmp
;
4415 if (!test_and_clear_bit(STRIPE_BATCH_READY
, &sh
->state
))
4416 return (sh
->batch_head
&& sh
->batch_head
!= sh
);
4417 spin_lock(&sh
->stripe_lock
);
4418 if (!sh
->batch_head
) {
4419 spin_unlock(&sh
->stripe_lock
);
4424 * this stripe could be added to a batch list before we check
4425 * BATCH_READY, skips it
4427 if (sh
->batch_head
!= sh
) {
4428 spin_unlock(&sh
->stripe_lock
);
4431 spin_lock(&sh
->batch_lock
);
4432 list_for_each_entry(tmp
, &sh
->batch_list
, batch_list
)
4433 clear_bit(STRIPE_BATCH_READY
, &tmp
->state
);
4434 spin_unlock(&sh
->batch_lock
);
4435 spin_unlock(&sh
->stripe_lock
);
4438 * BATCH_READY is cleared, no new stripes can be added.
4439 * batch_list can be accessed without lock
4444 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
4445 unsigned long handle_flags
)
4447 struct stripe_head
*sh
, *next
;
4451 list_for_each_entry_safe(sh
, next
, &head_sh
->batch_list
, batch_list
) {
4453 list_del_init(&sh
->batch_list
);
4455 WARN_ONCE(sh
->state
& ((1 << STRIPE_ACTIVE
) |
4456 (1 << STRIPE_SYNCING
) |
4457 (1 << STRIPE_REPLACED
) |
4458 (1 << STRIPE_DELAYED
) |
4459 (1 << STRIPE_BIT_DELAY
) |
4460 (1 << STRIPE_FULL_WRITE
) |
4461 (1 << STRIPE_BIOFILL_RUN
) |
4462 (1 << STRIPE_COMPUTE_RUN
) |
4463 (1 << STRIPE_OPS_REQ_PENDING
) |
4464 (1 << STRIPE_DISCARD
) |
4465 (1 << STRIPE_BATCH_READY
) |
4466 (1 << STRIPE_BATCH_ERR
) |
4467 (1 << STRIPE_BITMAP_PENDING
)),
4468 "stripe state: %lx\n", sh
->state
);
4469 WARN_ONCE(head_sh
->state
& ((1 << STRIPE_DISCARD
) |
4470 (1 << STRIPE_REPLACED
)),
4471 "head stripe state: %lx\n", head_sh
->state
);
4473 set_mask_bits(&sh
->state
, ~(STRIPE_EXPAND_SYNC_FLAGS
|
4474 (1 << STRIPE_PREREAD_ACTIVE
) |
4475 (1 << STRIPE_DEGRADED
)),
4476 head_sh
->state
& (1 << STRIPE_INSYNC
));
4478 sh
->check_state
= head_sh
->check_state
;
4479 sh
->reconstruct_state
= head_sh
->reconstruct_state
;
4480 for (i
= 0; i
< sh
->disks
; i
++) {
4481 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
4483 sh
->dev
[i
].flags
= head_sh
->dev
[i
].flags
&
4484 (~((1 << R5_WriteError
) | (1 << R5_Overlap
)));
4486 spin_lock_irq(&sh
->stripe_lock
);
4487 sh
->batch_head
= NULL
;
4488 spin_unlock_irq(&sh
->stripe_lock
);
4489 if (handle_flags
== 0 ||
4490 sh
->state
& handle_flags
)
4491 set_bit(STRIPE_HANDLE
, &sh
->state
);
4492 raid5_release_stripe(sh
);
4494 spin_lock_irq(&head_sh
->stripe_lock
);
4495 head_sh
->batch_head
= NULL
;
4496 spin_unlock_irq(&head_sh
->stripe_lock
);
4497 for (i
= 0; i
< head_sh
->disks
; i
++)
4498 if (test_and_clear_bit(R5_Overlap
, &head_sh
->dev
[i
].flags
))
4500 if (head_sh
->state
& handle_flags
)
4501 set_bit(STRIPE_HANDLE
, &head_sh
->state
);
4504 wake_up(&head_sh
->raid_conf
->wait_for_overlap
);
4507 static void handle_stripe(struct stripe_head
*sh
)
4509 struct stripe_head_state s
;
4510 struct r5conf
*conf
= sh
->raid_conf
;
4513 int disks
= sh
->disks
;
4514 struct r5dev
*pdev
, *qdev
;
4516 clear_bit(STRIPE_HANDLE
, &sh
->state
);
4517 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
4518 /* already being handled, ensure it gets handled
4519 * again when current action finishes */
4520 set_bit(STRIPE_HANDLE
, &sh
->state
);
4524 if (clear_batch_ready(sh
) ) {
4525 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4529 if (test_and_clear_bit(STRIPE_BATCH_ERR
, &sh
->state
))
4530 break_stripe_batch_list(sh
, 0);
4532 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
) && !sh
->batch_head
) {
4533 spin_lock(&sh
->stripe_lock
);
4534 /* Cannot process 'sync' concurrently with 'discard' */
4535 if (!test_bit(STRIPE_DISCARD
, &sh
->state
) &&
4536 test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
4537 set_bit(STRIPE_SYNCING
, &sh
->state
);
4538 clear_bit(STRIPE_INSYNC
, &sh
->state
);
4539 clear_bit(STRIPE_REPLACED
, &sh
->state
);
4541 spin_unlock(&sh
->stripe_lock
);
4543 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4545 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4546 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4547 (unsigned long long)sh
->sector
, sh
->state
,
4548 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
4549 sh
->check_state
, sh
->reconstruct_state
);
4551 analyse_stripe(sh
, &s
);
4553 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
))
4556 if (s
.handle_bad_blocks
) {
4557 set_bit(STRIPE_HANDLE
, &sh
->state
);
4561 if (unlikely(s
.blocked_rdev
)) {
4562 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
4563 s
.replacing
|| s
.to_write
|| s
.written
) {
4564 set_bit(STRIPE_HANDLE
, &sh
->state
);
4567 /* There is nothing for the blocked_rdev to block */
4568 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
4569 s
.blocked_rdev
= NULL
;
4572 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
4573 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
4574 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
4577 pr_debug("locked=%d uptodate=%d to_read=%d"
4578 " to_write=%d failed=%d failed_num=%d,%d\n",
4579 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
4580 s
.failed_num
[0], s
.failed_num
[1]);
4581 /* check if the array has lost more than max_degraded devices and,
4582 * if so, some requests might need to be failed.
4584 if (s
.failed
> conf
->max_degraded
|| s
.log_failed
) {
4585 sh
->check_state
= 0;
4586 sh
->reconstruct_state
= 0;
4587 break_stripe_batch_list(sh
, 0);
4588 if (s
.to_read
+s
.to_write
+s
.written
)
4589 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
4590 if (s
.syncing
+ s
.replacing
)
4591 handle_failed_sync(conf
, sh
, &s
);
4594 /* Now we check to see if any write operations have recently
4598 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
4600 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
4601 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
4602 sh
->reconstruct_state
= reconstruct_state_idle
;
4604 /* All the 'written' buffers and the parity block are ready to
4605 * be written back to disk
4607 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
4608 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
4609 BUG_ON(sh
->qd_idx
>= 0 &&
4610 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
4611 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
4612 for (i
= disks
; i
--; ) {
4613 struct r5dev
*dev
= &sh
->dev
[i
];
4614 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
4615 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
4616 dev
->written
|| test_bit(R5_InJournal
,
4618 pr_debug("Writing block %d\n", i
);
4619 set_bit(R5_Wantwrite
, &dev
->flags
);
4624 if (!test_bit(R5_Insync
, &dev
->flags
) ||
4625 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
4627 set_bit(STRIPE_INSYNC
, &sh
->state
);
4630 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4631 s
.dec_preread_active
= 1;
4635 * might be able to return some write requests if the parity blocks
4636 * are safe, or on a failed drive
4638 pdev
= &sh
->dev
[sh
->pd_idx
];
4639 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
4640 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
4641 qdev
= &sh
->dev
[sh
->qd_idx
];
4642 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
4643 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
4647 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
4648 && !test_bit(R5_LOCKED
, &pdev
->flags
)
4649 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
4650 test_bit(R5_Discard
, &pdev
->flags
))))) &&
4651 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
4652 && !test_bit(R5_LOCKED
, &qdev
->flags
)
4653 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
4654 test_bit(R5_Discard
, &qdev
->flags
))))))
4655 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
4658 r5c_handle_cached_data_endio(conf
, sh
, disks
, &s
.return_bi
);
4659 r5l_stripe_write_finished(sh
);
4661 /* Now we might consider reading some blocks, either to check/generate
4662 * parity, or to satisfy requests
4663 * or to load a block that is being partially written.
4665 if (s
.to_read
|| s
.non_overwrite
4666 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
4667 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
4670 handle_stripe_fill(sh
, &s
, disks
);
4673 * When the stripe finishes full journal write cycle (write to journal
4674 * and raid disk), this is the clean up procedure so it is ready for
4677 r5c_finish_stripe_write_out(conf
, sh
, &s
);
4680 * Now to consider new write requests, cache write back and what else,
4681 * if anything should be read. We do not handle new writes when:
4682 * 1/ A 'write' operation (copy+xor) is already in flight.
4683 * 2/ A 'check' operation is in flight, as it may clobber the parity
4685 * 3/ A r5c cache log write is in flight.
4688 if (!sh
->reconstruct_state
&& !sh
->check_state
&& !sh
->log_io
) {
4689 if (!r5c_is_writeback(conf
->log
)) {
4691 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
4692 } else { /* write back cache */
4695 /* First, try handle writes in caching phase */
4697 ret
= r5c_try_caching_write(conf
, sh
, &s
,
4700 * If caching phase failed: ret == -EAGAIN
4702 * stripe under reclaim: !caching && injournal
4704 * fall back to handle_stripe_dirtying()
4706 if (ret
== -EAGAIN
||
4707 /* stripe under reclaim: !caching && injournal */
4708 (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
) &&
4710 ret
= handle_stripe_dirtying(conf
, sh
, &s
,
4718 /* maybe we need to check and possibly fix the parity for this stripe
4719 * Any reads will already have been scheduled, so we just see if enough
4720 * data is available. The parity check is held off while parity
4721 * dependent operations are in flight.
4723 if (sh
->check_state
||
4724 (s
.syncing
&& s
.locked
== 0 &&
4725 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4726 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
4727 if (conf
->level
== 6)
4728 handle_parity_checks6(conf
, sh
, &s
, disks
);
4730 handle_parity_checks5(conf
, sh
, &s
, disks
);
4733 if ((s
.replacing
|| s
.syncing
) && s
.locked
== 0
4734 && !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)
4735 && !test_bit(STRIPE_REPLACED
, &sh
->state
)) {
4736 /* Write out to replacement devices where possible */
4737 for (i
= 0; i
< conf
->raid_disks
; i
++)
4738 if (test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
4739 WARN_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
4740 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
4741 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4745 set_bit(STRIPE_INSYNC
, &sh
->state
);
4746 set_bit(STRIPE_REPLACED
, &sh
->state
);
4748 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
4749 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4750 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
4751 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4752 clear_bit(STRIPE_SYNCING
, &sh
->state
);
4753 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
4754 wake_up(&conf
->wait_for_overlap
);
4757 /* If the failed drives are just a ReadError, then we might need
4758 * to progress the repair/check process
4760 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
4761 for (i
= 0; i
< s
.failed
; i
++) {
4762 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
4763 if (test_bit(R5_ReadError
, &dev
->flags
)
4764 && !test_bit(R5_LOCKED
, &dev
->flags
)
4765 && test_bit(R5_UPTODATE
, &dev
->flags
)
4767 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
4768 set_bit(R5_Wantwrite
, &dev
->flags
);
4769 set_bit(R5_ReWrite
, &dev
->flags
);
4770 set_bit(R5_LOCKED
, &dev
->flags
);
4773 /* let's read it back */
4774 set_bit(R5_Wantread
, &dev
->flags
);
4775 set_bit(R5_LOCKED
, &dev
->flags
);
4781 /* Finish reconstruct operations initiated by the expansion process */
4782 if (sh
->reconstruct_state
== reconstruct_state_result
) {
4783 struct stripe_head
*sh_src
4784 = raid5_get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
4785 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
4786 /* sh cannot be written until sh_src has been read.
4787 * so arrange for sh to be delayed a little
4789 set_bit(STRIPE_DELAYED
, &sh
->state
);
4790 set_bit(STRIPE_HANDLE
, &sh
->state
);
4791 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
4793 atomic_inc(&conf
->preread_active_stripes
);
4794 raid5_release_stripe(sh_src
);
4798 raid5_release_stripe(sh_src
);
4800 sh
->reconstruct_state
= reconstruct_state_idle
;
4801 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
4802 for (i
= conf
->raid_disks
; i
--; ) {
4803 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
4804 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4809 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
4810 !sh
->reconstruct_state
) {
4811 /* Need to write out all blocks after computing parity */
4812 sh
->disks
= conf
->raid_disks
;
4813 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
4814 schedule_reconstruction(sh
, &s
, 1, 1);
4815 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
4816 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4817 atomic_dec(&conf
->reshape_stripes
);
4818 wake_up(&conf
->wait_for_overlap
);
4819 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4822 if (s
.expanding
&& s
.locked
== 0 &&
4823 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
4824 handle_stripe_expansion(conf
, sh
);
4827 /* wait for this device to become unblocked */
4828 if (unlikely(s
.blocked_rdev
)) {
4829 if (conf
->mddev
->external
)
4830 md_wait_for_blocked_rdev(s
.blocked_rdev
,
4833 /* Internal metadata will immediately
4834 * be written by raid5d, so we don't
4835 * need to wait here.
4837 rdev_dec_pending(s
.blocked_rdev
,
4841 if (s
.handle_bad_blocks
)
4842 for (i
= disks
; i
--; ) {
4843 struct md_rdev
*rdev
;
4844 struct r5dev
*dev
= &sh
->dev
[i
];
4845 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
4846 /* We own a safe reference to the rdev */
4847 rdev
= conf
->disks
[i
].rdev
;
4848 if (!rdev_set_badblocks(rdev
, sh
->sector
,
4850 md_error(conf
->mddev
, rdev
);
4851 rdev_dec_pending(rdev
, conf
->mddev
);
4853 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
4854 rdev
= conf
->disks
[i
].rdev
;
4855 rdev_clear_badblocks(rdev
, sh
->sector
,
4857 rdev_dec_pending(rdev
, conf
->mddev
);
4859 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4860 rdev
= conf
->disks
[i
].replacement
;
4862 /* rdev have been moved down */
4863 rdev
= conf
->disks
[i
].rdev
;
4864 rdev_clear_badblocks(rdev
, sh
->sector
,
4866 rdev_dec_pending(rdev
, conf
->mddev
);
4871 raid_run_ops(sh
, s
.ops_request
);
4875 if (s
.dec_preread_active
) {
4876 /* We delay this until after ops_run_io so that if make_request
4877 * is waiting on a flush, it won't continue until the writes
4878 * have actually been submitted.
4880 atomic_dec(&conf
->preread_active_stripes
);
4881 if (atomic_read(&conf
->preread_active_stripes
) <
4883 md_wakeup_thread(conf
->mddev
->thread
);
4886 if (!bio_list_empty(&s
.return_bi
)) {
4887 if (test_bit(MD_SB_CHANGE_PENDING
, &conf
->mddev
->sb_flags
)) {
4888 spin_lock_irq(&conf
->device_lock
);
4889 bio_list_merge(&conf
->return_bi
, &s
.return_bi
);
4890 spin_unlock_irq(&conf
->device_lock
);
4891 md_wakeup_thread(conf
->mddev
->thread
);
4893 return_io(&s
.return_bi
);
4896 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4899 static void raid5_activate_delayed(struct r5conf
*conf
)
4901 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
4902 while (!list_empty(&conf
->delayed_list
)) {
4903 struct list_head
*l
= conf
->delayed_list
.next
;
4904 struct stripe_head
*sh
;
4905 sh
= list_entry(l
, struct stripe_head
, lru
);
4907 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4908 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4909 atomic_inc(&conf
->preread_active_stripes
);
4910 list_add_tail(&sh
->lru
, &conf
->hold_list
);
4911 raid5_wakeup_stripe_thread(sh
);
4916 static void activate_bit_delay(struct r5conf
*conf
,
4917 struct list_head
*temp_inactive_list
)
4919 /* device_lock is held */
4920 struct list_head head
;
4921 list_add(&head
, &conf
->bitmap_list
);
4922 list_del_init(&conf
->bitmap_list
);
4923 while (!list_empty(&head
)) {
4924 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
4926 list_del_init(&sh
->lru
);
4927 atomic_inc(&sh
->count
);
4928 hash
= sh
->hash_lock_index
;
4929 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
4933 static int raid5_congested(struct mddev
*mddev
, int bits
)
4935 struct r5conf
*conf
= mddev
->private;
4937 /* No difference between reads and writes. Just check
4938 * how busy the stripe_cache is
4941 if (test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
))
4944 /* Also checks whether there is pressure on r5cache log space */
4945 if (test_bit(R5C_LOG_TIGHT
, &conf
->cache_state
))
4949 if (atomic_read(&conf
->empty_inactive_list_nr
))
4955 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
4957 struct r5conf
*conf
= mddev
->private;
4958 sector_t sector
= bio
->bi_iter
.bi_sector
+ get_start_sect(bio
->bi_bdev
);
4959 unsigned int chunk_sectors
;
4960 unsigned int bio_sectors
= bio_sectors(bio
);
4962 chunk_sectors
= min(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
4963 return chunk_sectors
>=
4964 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
4968 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4969 * later sampled by raid5d.
4971 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
4973 unsigned long flags
;
4975 spin_lock_irqsave(&conf
->device_lock
, flags
);
4977 bi
->bi_next
= conf
->retry_read_aligned_list
;
4978 conf
->retry_read_aligned_list
= bi
;
4980 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4981 md_wakeup_thread(conf
->mddev
->thread
);
4984 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
4988 bi
= conf
->retry_read_aligned
;
4990 conf
->retry_read_aligned
= NULL
;
4993 bi
= conf
->retry_read_aligned_list
;
4995 conf
->retry_read_aligned_list
= bi
->bi_next
;
4998 * this sets the active strip count to 1 and the processed
4999 * strip count to zero (upper 8 bits)
5001 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
5008 * The "raid5_align_endio" should check if the read succeeded and if it
5009 * did, call bio_endio on the original bio (having bio_put the new bio
5011 * If the read failed..
5013 static void raid5_align_endio(struct bio
*bi
)
5015 struct bio
* raid_bi
= bi
->bi_private
;
5016 struct mddev
*mddev
;
5017 struct r5conf
*conf
;
5018 struct md_rdev
*rdev
;
5019 int error
= bi
->bi_error
;
5023 rdev
= (void*)raid_bi
->bi_next
;
5024 raid_bi
->bi_next
= NULL
;
5025 mddev
= rdev
->mddev
;
5026 conf
= mddev
->private;
5028 rdev_dec_pending(rdev
, conf
->mddev
);
5031 trace_block_bio_complete(bdev_get_queue(raid_bi
->bi_bdev
),
5034 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
5035 wake_up(&conf
->wait_for_quiescent
);
5039 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
5041 add_bio_to_retry(raid_bi
, conf
);
5044 static int raid5_read_one_chunk(struct mddev
*mddev
, struct bio
*raid_bio
)
5046 struct r5conf
*conf
= mddev
->private;
5048 struct bio
* align_bi
;
5049 struct md_rdev
*rdev
;
5050 sector_t end_sector
;
5052 if (!in_chunk_boundary(mddev
, raid_bio
)) {
5053 pr_debug("%s: non aligned\n", __func__
);
5057 * use bio_clone_fast to make a copy of the bio
5059 align_bi
= bio_clone_fast(raid_bio
, GFP_NOIO
, mddev
->bio_set
);
5063 * set bi_end_io to a new function, and set bi_private to the
5066 align_bi
->bi_end_io
= raid5_align_endio
;
5067 align_bi
->bi_private
= raid_bio
;
5071 align_bi
->bi_iter
.bi_sector
=
5072 raid5_compute_sector(conf
, raid_bio
->bi_iter
.bi_sector
,
5075 end_sector
= bio_end_sector(align_bi
);
5077 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
5078 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
5079 rdev
->recovery_offset
< end_sector
) {
5080 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
5082 (test_bit(Faulty
, &rdev
->flags
) ||
5083 !(test_bit(In_sync
, &rdev
->flags
) ||
5084 rdev
->recovery_offset
>= end_sector
)))
5088 if (r5c_big_stripe_cached(conf
, align_bi
->bi_iter
.bi_sector
)) {
5098 atomic_inc(&rdev
->nr_pending
);
5100 raid_bio
->bi_next
= (void*)rdev
;
5101 align_bi
->bi_bdev
= rdev
->bdev
;
5102 bio_clear_flag(align_bi
, BIO_SEG_VALID
);
5104 if (is_badblock(rdev
, align_bi
->bi_iter
.bi_sector
,
5105 bio_sectors(align_bi
),
5106 &first_bad
, &bad_sectors
)) {
5108 rdev_dec_pending(rdev
, mddev
);
5112 /* No reshape active, so we can trust rdev->data_offset */
5113 align_bi
->bi_iter
.bi_sector
+= rdev
->data_offset
;
5115 spin_lock_irq(&conf
->device_lock
);
5116 wait_event_lock_irq(conf
->wait_for_quiescent
,
5119 atomic_inc(&conf
->active_aligned_reads
);
5120 spin_unlock_irq(&conf
->device_lock
);
5123 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
5124 align_bi
, disk_devt(mddev
->gendisk
),
5125 raid_bio
->bi_iter
.bi_sector
);
5126 generic_make_request(align_bi
);
5135 static struct bio
*chunk_aligned_read(struct mddev
*mddev
, struct bio
*raid_bio
)
5140 sector_t sector
= raid_bio
->bi_iter
.bi_sector
;
5141 unsigned chunk_sects
= mddev
->chunk_sectors
;
5142 unsigned sectors
= chunk_sects
- (sector
& (chunk_sects
-1));
5144 if (sectors
< bio_sectors(raid_bio
)) {
5145 split
= bio_split(raid_bio
, sectors
, GFP_NOIO
, fs_bio_set
);
5146 bio_chain(split
, raid_bio
);
5150 if (!raid5_read_one_chunk(mddev
, split
)) {
5151 if (split
!= raid_bio
)
5152 generic_make_request(raid_bio
);
5155 } while (split
!= raid_bio
);
5160 /* __get_priority_stripe - get the next stripe to process
5162 * Full stripe writes are allowed to pass preread active stripes up until
5163 * the bypass_threshold is exceeded. In general the bypass_count
5164 * increments when the handle_list is handled before the hold_list; however, it
5165 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5166 * stripe with in flight i/o. The bypass_count will be reset when the
5167 * head of the hold_list has changed, i.e. the head was promoted to the
5170 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
, int group
)
5172 struct stripe_head
*sh
= NULL
, *tmp
;
5173 struct list_head
*handle_list
= NULL
;
5174 struct r5worker_group
*wg
= NULL
;
5176 if (conf
->worker_cnt_per_group
== 0) {
5177 handle_list
= &conf
->handle_list
;
5178 } else if (group
!= ANY_GROUP
) {
5179 handle_list
= &conf
->worker_groups
[group
].handle_list
;
5180 wg
= &conf
->worker_groups
[group
];
5183 for (i
= 0; i
< conf
->group_cnt
; i
++) {
5184 handle_list
= &conf
->worker_groups
[i
].handle_list
;
5185 wg
= &conf
->worker_groups
[i
];
5186 if (!list_empty(handle_list
))
5191 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5193 list_empty(handle_list
) ? "empty" : "busy",
5194 list_empty(&conf
->hold_list
) ? "empty" : "busy",
5195 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
5197 if (!list_empty(handle_list
)) {
5198 sh
= list_entry(handle_list
->next
, typeof(*sh
), lru
);
5200 if (list_empty(&conf
->hold_list
))
5201 conf
->bypass_count
= 0;
5202 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
5203 if (conf
->hold_list
.next
== conf
->last_hold
)
5204 conf
->bypass_count
++;
5206 conf
->last_hold
= conf
->hold_list
.next
;
5207 conf
->bypass_count
-= conf
->bypass_threshold
;
5208 if (conf
->bypass_count
< 0)
5209 conf
->bypass_count
= 0;
5212 } else if (!list_empty(&conf
->hold_list
) &&
5213 ((conf
->bypass_threshold
&&
5214 conf
->bypass_count
> conf
->bypass_threshold
) ||
5215 atomic_read(&conf
->pending_full_writes
) == 0)) {
5217 list_for_each_entry(tmp
, &conf
->hold_list
, lru
) {
5218 if (conf
->worker_cnt_per_group
== 0 ||
5219 group
== ANY_GROUP
||
5220 !cpu_online(tmp
->cpu
) ||
5221 cpu_to_group(tmp
->cpu
) == group
) {
5228 conf
->bypass_count
-= conf
->bypass_threshold
;
5229 if (conf
->bypass_count
< 0)
5230 conf
->bypass_count
= 0;
5242 list_del_init(&sh
->lru
);
5243 BUG_ON(atomic_inc_return(&sh
->count
) != 1);
5247 struct raid5_plug_cb
{
5248 struct blk_plug_cb cb
;
5249 struct list_head list
;
5250 struct list_head temp_inactive_list
[NR_STRIPE_HASH_LOCKS
];
5253 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
5255 struct raid5_plug_cb
*cb
= container_of(
5256 blk_cb
, struct raid5_plug_cb
, cb
);
5257 struct stripe_head
*sh
;
5258 struct mddev
*mddev
= cb
->cb
.data
;
5259 struct r5conf
*conf
= mddev
->private;
5263 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
5264 spin_lock_irq(&conf
->device_lock
);
5265 while (!list_empty(&cb
->list
)) {
5266 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
5267 list_del_init(&sh
->lru
);
5269 * avoid race release_stripe_plug() sees
5270 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5271 * is still in our list
5273 smp_mb__before_atomic();
5274 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
5276 * STRIPE_ON_RELEASE_LIST could be set here. In that
5277 * case, the count is always > 1 here
5279 hash
= sh
->hash_lock_index
;
5280 __release_stripe(conf
, sh
, &cb
->temp_inactive_list
[hash
]);
5283 spin_unlock_irq(&conf
->device_lock
);
5285 release_inactive_stripe_list(conf
, cb
->temp_inactive_list
,
5286 NR_STRIPE_HASH_LOCKS
);
5288 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
5292 static void release_stripe_plug(struct mddev
*mddev
,
5293 struct stripe_head
*sh
)
5295 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
5296 raid5_unplug
, mddev
,
5297 sizeof(struct raid5_plug_cb
));
5298 struct raid5_plug_cb
*cb
;
5301 raid5_release_stripe(sh
);
5305 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
5307 if (cb
->list
.next
== NULL
) {
5309 INIT_LIST_HEAD(&cb
->list
);
5310 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5311 INIT_LIST_HEAD(cb
->temp_inactive_list
+ i
);
5314 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
5315 list_add_tail(&sh
->lru
, &cb
->list
);
5317 raid5_release_stripe(sh
);
5320 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
5322 struct r5conf
*conf
= mddev
->private;
5323 sector_t logical_sector
, last_sector
;
5324 struct stripe_head
*sh
;
5328 if (mddev
->reshape_position
!= MaxSector
)
5329 /* Skip discard while reshape is happening */
5332 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5333 last_sector
= bi
->bi_iter
.bi_sector
+ (bi
->bi_iter
.bi_size
>>9);
5336 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5338 stripe_sectors
= conf
->chunk_sectors
*
5339 (conf
->raid_disks
- conf
->max_degraded
);
5340 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
5342 sector_div(last_sector
, stripe_sectors
);
5344 logical_sector
*= conf
->chunk_sectors
;
5345 last_sector
*= conf
->chunk_sectors
;
5347 for (; logical_sector
< last_sector
;
5348 logical_sector
+= STRIPE_SECTORS
) {
5352 sh
= raid5_get_active_stripe(conf
, logical_sector
, 0, 0, 0);
5353 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5354 TASK_UNINTERRUPTIBLE
);
5355 set_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5356 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
5357 raid5_release_stripe(sh
);
5361 clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5362 spin_lock_irq(&sh
->stripe_lock
);
5363 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5364 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5366 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
5367 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
5368 spin_unlock_irq(&sh
->stripe_lock
);
5369 raid5_release_stripe(sh
);
5374 set_bit(STRIPE_DISCARD
, &sh
->state
);
5375 finish_wait(&conf
->wait_for_overlap
, &w
);
5376 sh
->overwrite_disks
= 0;
5377 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5378 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5380 sh
->dev
[d
].towrite
= bi
;
5381 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
5382 raid5_inc_bi_active_stripes(bi
);
5383 sh
->overwrite_disks
++;
5385 spin_unlock_irq(&sh
->stripe_lock
);
5386 if (conf
->mddev
->bitmap
) {
5388 d
< conf
->raid_disks
- conf
->max_degraded
;
5390 bitmap_startwrite(mddev
->bitmap
,
5394 sh
->bm_seq
= conf
->seq_flush
+ 1;
5395 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
5398 set_bit(STRIPE_HANDLE
, &sh
->state
);
5399 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5400 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5401 atomic_inc(&conf
->preread_active_stripes
);
5402 release_stripe_plug(mddev
, sh
);
5405 remaining
= raid5_dec_bi_active_stripes(bi
);
5406 if (remaining
== 0) {
5407 md_write_end(mddev
);
5412 static void raid5_make_request(struct mddev
*mddev
, struct bio
* bi
)
5414 struct r5conf
*conf
= mddev
->private;
5416 sector_t new_sector
;
5417 sector_t logical_sector
, last_sector
;
5418 struct stripe_head
*sh
;
5419 const int rw
= bio_data_dir(bi
);
5423 bool do_flush
= false;
5425 if (unlikely(bi
->bi_opf
& REQ_PREFLUSH
)) {
5426 int ret
= r5l_handle_flush_request(conf
->log
, bi
);
5430 if (ret
== -ENODEV
) {
5431 md_flush_request(mddev
, bi
);
5434 /* ret == -EAGAIN, fallback */
5436 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5437 * we need to flush journal device
5439 do_flush
= bi
->bi_opf
& REQ_PREFLUSH
;
5442 md_write_start(mddev
, bi
);
5445 * If array is degraded, better not do chunk aligned read because
5446 * later we might have to read it again in order to reconstruct
5447 * data on failed drives.
5449 if (rw
== READ
&& mddev
->degraded
== 0 &&
5450 mddev
->reshape_position
== MaxSector
) {
5451 bi
= chunk_aligned_read(mddev
, bi
);
5456 if (unlikely(bio_op(bi
) == REQ_OP_DISCARD
)) {
5457 make_discard_request(mddev
, bi
);
5461 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5462 last_sector
= bio_end_sector(bi
);
5464 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5466 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
5467 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
5473 seq
= read_seqcount_begin(&conf
->gen_lock
);
5476 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5477 TASK_UNINTERRUPTIBLE
);
5478 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
5479 /* spinlock is needed as reshape_progress may be
5480 * 64bit on a 32bit platform, and so it might be
5481 * possible to see a half-updated value
5482 * Of course reshape_progress could change after
5483 * the lock is dropped, so once we get a reference
5484 * to the stripe that we think it is, we will have
5487 spin_lock_irq(&conf
->device_lock
);
5488 if (mddev
->reshape_backwards
5489 ? logical_sector
< conf
->reshape_progress
5490 : logical_sector
>= conf
->reshape_progress
) {
5493 if (mddev
->reshape_backwards
5494 ? logical_sector
< conf
->reshape_safe
5495 : logical_sector
>= conf
->reshape_safe
) {
5496 spin_unlock_irq(&conf
->device_lock
);
5502 spin_unlock_irq(&conf
->device_lock
);
5505 new_sector
= raid5_compute_sector(conf
, logical_sector
,
5508 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
5509 (unsigned long long)new_sector
,
5510 (unsigned long long)logical_sector
);
5512 sh
= raid5_get_active_stripe(conf
, new_sector
, previous
,
5513 (bi
->bi_opf
& REQ_RAHEAD
), 0);
5515 if (unlikely(previous
)) {
5516 /* expansion might have moved on while waiting for a
5517 * stripe, so we must do the range check again.
5518 * Expansion could still move past after this
5519 * test, but as we are holding a reference to
5520 * 'sh', we know that if that happens,
5521 * STRIPE_EXPANDING will get set and the expansion
5522 * won't proceed until we finish with the stripe.
5525 spin_lock_irq(&conf
->device_lock
);
5526 if (mddev
->reshape_backwards
5527 ? logical_sector
>= conf
->reshape_progress
5528 : logical_sector
< conf
->reshape_progress
)
5529 /* mismatch, need to try again */
5531 spin_unlock_irq(&conf
->device_lock
);
5533 raid5_release_stripe(sh
);
5539 if (read_seqcount_retry(&conf
->gen_lock
, seq
)) {
5540 /* Might have got the wrong stripe_head
5543 raid5_release_stripe(sh
);
5548 logical_sector
>= mddev
->suspend_lo
&&
5549 logical_sector
< mddev
->suspend_hi
) {
5550 raid5_release_stripe(sh
);
5551 /* As the suspend_* range is controlled by
5552 * userspace, we want an interruptible
5555 flush_signals(current
);
5556 prepare_to_wait(&conf
->wait_for_overlap
,
5557 &w
, TASK_INTERRUPTIBLE
);
5558 if (logical_sector
>= mddev
->suspend_lo
&&
5559 logical_sector
< mddev
->suspend_hi
) {
5566 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
5567 !add_stripe_bio(sh
, bi
, dd_idx
, rw
, previous
)) {
5568 /* Stripe is busy expanding or
5569 * add failed due to overlap. Flush everything
5572 md_wakeup_thread(mddev
->thread
);
5573 raid5_release_stripe(sh
);
5579 set_bit(STRIPE_R5C_PREFLUSH
, &sh
->state
);
5580 /* we only need flush for one stripe */
5584 set_bit(STRIPE_HANDLE
, &sh
->state
);
5585 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5586 if ((!sh
->batch_head
|| sh
== sh
->batch_head
) &&
5587 (bi
->bi_opf
& REQ_SYNC
) &&
5588 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5589 atomic_inc(&conf
->preread_active_stripes
);
5590 release_stripe_plug(mddev
, sh
);
5592 /* cannot get stripe for read-ahead, just give-up */
5593 bi
->bi_error
= -EIO
;
5597 finish_wait(&conf
->wait_for_overlap
, &w
);
5599 remaining
= raid5_dec_bi_active_stripes(bi
);
5600 if (remaining
== 0) {
5603 md_write_end(mddev
);
5605 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
5611 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
5613 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
5615 /* reshaping is quite different to recovery/resync so it is
5616 * handled quite separately ... here.
5618 * On each call to sync_request, we gather one chunk worth of
5619 * destination stripes and flag them as expanding.
5620 * Then we find all the source stripes and request reads.
5621 * As the reads complete, handle_stripe will copy the data
5622 * into the destination stripe and release that stripe.
5624 struct r5conf
*conf
= mddev
->private;
5625 struct stripe_head
*sh
;
5626 sector_t first_sector
, last_sector
;
5627 int raid_disks
= conf
->previous_raid_disks
;
5628 int data_disks
= raid_disks
- conf
->max_degraded
;
5629 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5632 sector_t writepos
, readpos
, safepos
;
5633 sector_t stripe_addr
;
5634 int reshape_sectors
;
5635 struct list_head stripes
;
5638 if (sector_nr
== 0) {
5639 /* If restarting in the middle, skip the initial sectors */
5640 if (mddev
->reshape_backwards
&&
5641 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
5642 sector_nr
= raid5_size(mddev
, 0, 0)
5643 - conf
->reshape_progress
;
5644 } else if (mddev
->reshape_backwards
&&
5645 conf
->reshape_progress
== MaxSector
) {
5646 /* shouldn't happen, but just in case, finish up.*/
5647 sector_nr
= MaxSector
;
5648 } else if (!mddev
->reshape_backwards
&&
5649 conf
->reshape_progress
> 0)
5650 sector_nr
= conf
->reshape_progress
;
5651 sector_div(sector_nr
, new_data_disks
);
5653 mddev
->curr_resync_completed
= sector_nr
;
5654 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5661 /* We need to process a full chunk at a time.
5662 * If old and new chunk sizes differ, we need to process the
5666 reshape_sectors
= max(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
5668 /* We update the metadata at least every 10 seconds, or when
5669 * the data about to be copied would over-write the source of
5670 * the data at the front of the range. i.e. one new_stripe
5671 * along from reshape_progress new_maps to after where
5672 * reshape_safe old_maps to
5674 writepos
= conf
->reshape_progress
;
5675 sector_div(writepos
, new_data_disks
);
5676 readpos
= conf
->reshape_progress
;
5677 sector_div(readpos
, data_disks
);
5678 safepos
= conf
->reshape_safe
;
5679 sector_div(safepos
, data_disks
);
5680 if (mddev
->reshape_backwards
) {
5681 BUG_ON(writepos
< reshape_sectors
);
5682 writepos
-= reshape_sectors
;
5683 readpos
+= reshape_sectors
;
5684 safepos
+= reshape_sectors
;
5686 writepos
+= reshape_sectors
;
5687 /* readpos and safepos are worst-case calculations.
5688 * A negative number is overly pessimistic, and causes
5689 * obvious problems for unsigned storage. So clip to 0.
5691 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
5692 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
5695 /* Having calculated the 'writepos' possibly use it
5696 * to set 'stripe_addr' which is where we will write to.
5698 if (mddev
->reshape_backwards
) {
5699 BUG_ON(conf
->reshape_progress
== 0);
5700 stripe_addr
= writepos
;
5701 BUG_ON((mddev
->dev_sectors
&
5702 ~((sector_t
)reshape_sectors
- 1))
5703 - reshape_sectors
- stripe_addr
5706 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
5707 stripe_addr
= sector_nr
;
5710 /* 'writepos' is the most advanced device address we might write.
5711 * 'readpos' is the least advanced device address we might read.
5712 * 'safepos' is the least address recorded in the metadata as having
5714 * If there is a min_offset_diff, these are adjusted either by
5715 * increasing the safepos/readpos if diff is negative, or
5716 * increasing writepos if diff is positive.
5717 * If 'readpos' is then behind 'writepos', there is no way that we can
5718 * ensure safety in the face of a crash - that must be done by userspace
5719 * making a backup of the data. So in that case there is no particular
5720 * rush to update metadata.
5721 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5722 * update the metadata to advance 'safepos' to match 'readpos' so that
5723 * we can be safe in the event of a crash.
5724 * So we insist on updating metadata if safepos is behind writepos and
5725 * readpos is beyond writepos.
5726 * In any case, update the metadata every 10 seconds.
5727 * Maybe that number should be configurable, but I'm not sure it is
5728 * worth it.... maybe it could be a multiple of safemode_delay???
5730 if (conf
->min_offset_diff
< 0) {
5731 safepos
+= -conf
->min_offset_diff
;
5732 readpos
+= -conf
->min_offset_diff
;
5734 writepos
+= conf
->min_offset_diff
;
5736 if ((mddev
->reshape_backwards
5737 ? (safepos
> writepos
&& readpos
< writepos
)
5738 : (safepos
< writepos
&& readpos
> writepos
)) ||
5739 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
5740 /* Cannot proceed until we've updated the superblock... */
5741 wait_event(conf
->wait_for_overlap
,
5742 atomic_read(&conf
->reshape_stripes
)==0
5743 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5744 if (atomic_read(&conf
->reshape_stripes
) != 0)
5746 mddev
->reshape_position
= conf
->reshape_progress
;
5747 mddev
->curr_resync_completed
= sector_nr
;
5748 conf
->reshape_checkpoint
= jiffies
;
5749 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
5750 md_wakeup_thread(mddev
->thread
);
5751 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
5752 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5753 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5755 spin_lock_irq(&conf
->device_lock
);
5756 conf
->reshape_safe
= mddev
->reshape_position
;
5757 spin_unlock_irq(&conf
->device_lock
);
5758 wake_up(&conf
->wait_for_overlap
);
5759 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5762 INIT_LIST_HEAD(&stripes
);
5763 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
5765 int skipped_disk
= 0;
5766 sh
= raid5_get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
5767 set_bit(STRIPE_EXPANDING
, &sh
->state
);
5768 atomic_inc(&conf
->reshape_stripes
);
5769 /* If any of this stripe is beyond the end of the old
5770 * array, then we need to zero those blocks
5772 for (j
=sh
->disks
; j
--;) {
5774 if (j
== sh
->pd_idx
)
5776 if (conf
->level
== 6 &&
5779 s
= raid5_compute_blocknr(sh
, j
, 0);
5780 if (s
< raid5_size(mddev
, 0, 0)) {
5784 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
5785 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
5786 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
5788 if (!skipped_disk
) {
5789 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
5790 set_bit(STRIPE_HANDLE
, &sh
->state
);
5792 list_add(&sh
->lru
, &stripes
);
5794 spin_lock_irq(&conf
->device_lock
);
5795 if (mddev
->reshape_backwards
)
5796 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
5798 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
5799 spin_unlock_irq(&conf
->device_lock
);
5800 /* Ok, those stripe are ready. We can start scheduling
5801 * reads on the source stripes.
5802 * The source stripes are determined by mapping the first and last
5803 * block on the destination stripes.
5806 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
5809 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
5810 * new_data_disks
- 1),
5812 if (last_sector
>= mddev
->dev_sectors
)
5813 last_sector
= mddev
->dev_sectors
- 1;
5814 while (first_sector
<= last_sector
) {
5815 sh
= raid5_get_active_stripe(conf
, first_sector
, 1, 0, 1);
5816 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
5817 set_bit(STRIPE_HANDLE
, &sh
->state
);
5818 raid5_release_stripe(sh
);
5819 first_sector
+= STRIPE_SECTORS
;
5821 /* Now that the sources are clearly marked, we can release
5822 * the destination stripes
5824 while (!list_empty(&stripes
)) {
5825 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
5826 list_del_init(&sh
->lru
);
5827 raid5_release_stripe(sh
);
5829 /* If this takes us to the resync_max point where we have to pause,
5830 * then we need to write out the superblock.
5832 sector_nr
+= reshape_sectors
;
5833 retn
= reshape_sectors
;
5835 if (mddev
->curr_resync_completed
> mddev
->resync_max
||
5836 (sector_nr
- mddev
->curr_resync_completed
) * 2
5837 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
5838 /* Cannot proceed until we've updated the superblock... */
5839 wait_event(conf
->wait_for_overlap
,
5840 atomic_read(&conf
->reshape_stripes
) == 0
5841 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5842 if (atomic_read(&conf
->reshape_stripes
) != 0)
5844 mddev
->reshape_position
= conf
->reshape_progress
;
5845 mddev
->curr_resync_completed
= sector_nr
;
5846 conf
->reshape_checkpoint
= jiffies
;
5847 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
5848 md_wakeup_thread(mddev
->thread
);
5849 wait_event(mddev
->sb_wait
,
5850 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)
5851 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5852 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5854 spin_lock_irq(&conf
->device_lock
);
5855 conf
->reshape_safe
= mddev
->reshape_position
;
5856 spin_unlock_irq(&conf
->device_lock
);
5857 wake_up(&conf
->wait_for_overlap
);
5858 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5864 static inline sector_t
raid5_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
5867 struct r5conf
*conf
= mddev
->private;
5868 struct stripe_head
*sh
;
5869 sector_t max_sector
= mddev
->dev_sectors
;
5870 sector_t sync_blocks
;
5871 int still_degraded
= 0;
5874 if (sector_nr
>= max_sector
) {
5875 /* just being told to finish up .. nothing much to do */
5877 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
5882 if (mddev
->curr_resync
< max_sector
) /* aborted */
5883 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
5885 else /* completed sync */
5887 bitmap_close_sync(mddev
->bitmap
);
5892 /* Allow raid5_quiesce to complete */
5893 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
5895 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5896 return reshape_request(mddev
, sector_nr
, skipped
);
5898 /* No need to check resync_max as we never do more than one
5899 * stripe, and as resync_max will always be on a chunk boundary,
5900 * if the check in md_do_sync didn't fire, there is no chance
5901 * of overstepping resync_max here
5904 /* if there is too many failed drives and we are trying
5905 * to resync, then assert that we are finished, because there is
5906 * nothing we can do.
5908 if (mddev
->degraded
>= conf
->max_degraded
&&
5909 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5910 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
5914 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
5916 !bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
5917 sync_blocks
>= STRIPE_SECTORS
) {
5918 /* we can skip this block, and probably more */
5919 sync_blocks
/= STRIPE_SECTORS
;
5921 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
5924 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
, false);
5926 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 1, 0);
5928 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 0, 0);
5929 /* make sure we don't swamp the stripe cache if someone else
5930 * is trying to get access
5932 schedule_timeout_uninterruptible(1);
5934 /* Need to check if array will still be degraded after recovery/resync
5935 * Note in case of > 1 drive failures it's possible we're rebuilding
5936 * one drive while leaving another faulty drive in array.
5939 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5940 struct md_rdev
*rdev
= ACCESS_ONCE(conf
->disks
[i
].rdev
);
5942 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
))
5947 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
5949 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
5950 set_bit(STRIPE_HANDLE
, &sh
->state
);
5952 raid5_release_stripe(sh
);
5954 return STRIPE_SECTORS
;
5957 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
5959 /* We may not be able to submit a whole bio at once as there
5960 * may not be enough stripe_heads available.
5961 * We cannot pre-allocate enough stripe_heads as we may need
5962 * more than exist in the cache (if we allow ever large chunks).
5963 * So we do one stripe head at a time and record in
5964 * ->bi_hw_segments how many have been done.
5966 * We *know* that this entire raid_bio is in one chunk, so
5967 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5969 struct stripe_head
*sh
;
5971 sector_t sector
, logical_sector
, last_sector
;
5976 logical_sector
= raid_bio
->bi_iter
.bi_sector
&
5977 ~((sector_t
)STRIPE_SECTORS
-1);
5978 sector
= raid5_compute_sector(conf
, logical_sector
,
5980 last_sector
= bio_end_sector(raid_bio
);
5982 for (; logical_sector
< last_sector
;
5983 logical_sector
+= STRIPE_SECTORS
,
5984 sector
+= STRIPE_SECTORS
,
5987 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
5988 /* already done this stripe */
5991 sh
= raid5_get_active_stripe(conf
, sector
, 0, 1, 1);
5994 /* failed to get a stripe - must wait */
5995 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5996 conf
->retry_read_aligned
= raid_bio
;
6000 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0, 0)) {
6001 raid5_release_stripe(sh
);
6002 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
6003 conf
->retry_read_aligned
= raid_bio
;
6007 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
6009 raid5_release_stripe(sh
);
6012 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
6013 if (remaining
== 0) {
6014 trace_block_bio_complete(bdev_get_queue(raid_bio
->bi_bdev
),
6016 bio_endio(raid_bio
);
6018 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
6019 wake_up(&conf
->wait_for_quiescent
);
6023 static int handle_active_stripes(struct r5conf
*conf
, int group
,
6024 struct r5worker
*worker
,
6025 struct list_head
*temp_inactive_list
)
6027 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
6028 int i
, batch_size
= 0, hash
;
6029 bool release_inactive
= false;
6031 while (batch_size
< MAX_STRIPE_BATCH
&&
6032 (sh
= __get_priority_stripe(conf
, group
)) != NULL
)
6033 batch
[batch_size
++] = sh
;
6035 if (batch_size
== 0) {
6036 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6037 if (!list_empty(temp_inactive_list
+ i
))
6039 if (i
== NR_STRIPE_HASH_LOCKS
) {
6040 spin_unlock_irq(&conf
->device_lock
);
6041 r5l_flush_stripe_to_raid(conf
->log
);
6042 spin_lock_irq(&conf
->device_lock
);
6045 release_inactive
= true;
6047 spin_unlock_irq(&conf
->device_lock
);
6049 release_inactive_stripe_list(conf
, temp_inactive_list
,
6050 NR_STRIPE_HASH_LOCKS
);
6052 r5l_flush_stripe_to_raid(conf
->log
);
6053 if (release_inactive
) {
6054 spin_lock_irq(&conf
->device_lock
);
6058 for (i
= 0; i
< batch_size
; i
++)
6059 handle_stripe(batch
[i
]);
6060 r5l_write_stripe_run(conf
->log
);
6064 spin_lock_irq(&conf
->device_lock
);
6065 for (i
= 0; i
< batch_size
; i
++) {
6066 hash
= batch
[i
]->hash_lock_index
;
6067 __release_stripe(conf
, batch
[i
], &temp_inactive_list
[hash
]);
6072 static void raid5_do_work(struct work_struct
*work
)
6074 struct r5worker
*worker
= container_of(work
, struct r5worker
, work
);
6075 struct r5worker_group
*group
= worker
->group
;
6076 struct r5conf
*conf
= group
->conf
;
6077 int group_id
= group
- conf
->worker_groups
;
6079 struct blk_plug plug
;
6081 pr_debug("+++ raid5worker active\n");
6083 blk_start_plug(&plug
);
6085 spin_lock_irq(&conf
->device_lock
);
6087 int batch_size
, released
;
6089 released
= release_stripe_list(conf
, worker
->temp_inactive_list
);
6091 batch_size
= handle_active_stripes(conf
, group_id
, worker
,
6092 worker
->temp_inactive_list
);
6093 worker
->working
= false;
6094 if (!batch_size
&& !released
)
6096 handled
+= batch_size
;
6098 pr_debug("%d stripes handled\n", handled
);
6100 spin_unlock_irq(&conf
->device_lock
);
6101 blk_finish_plug(&plug
);
6103 pr_debug("--- raid5worker inactive\n");
6107 * This is our raid5 kernel thread.
6109 * We scan the hash table for stripes which can be handled now.
6110 * During the scan, completed stripes are saved for us by the interrupt
6111 * handler, so that they will not have to wait for our next wakeup.
6113 static void raid5d(struct md_thread
*thread
)
6115 struct mddev
*mddev
= thread
->mddev
;
6116 struct r5conf
*conf
= mddev
->private;
6118 struct blk_plug plug
;
6120 pr_debug("+++ raid5d active\n");
6122 md_check_recovery(mddev
);
6124 if (!bio_list_empty(&conf
->return_bi
) &&
6125 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
6126 struct bio_list tmp
= BIO_EMPTY_LIST
;
6127 spin_lock_irq(&conf
->device_lock
);
6128 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
6129 bio_list_merge(&tmp
, &conf
->return_bi
);
6130 bio_list_init(&conf
->return_bi
);
6132 spin_unlock_irq(&conf
->device_lock
);
6136 blk_start_plug(&plug
);
6138 spin_lock_irq(&conf
->device_lock
);
6141 int batch_size
, released
;
6143 released
= release_stripe_list(conf
, conf
->temp_inactive_list
);
6145 clear_bit(R5_DID_ALLOC
, &conf
->cache_state
);
6148 !list_empty(&conf
->bitmap_list
)) {
6149 /* Now is a good time to flush some bitmap updates */
6151 spin_unlock_irq(&conf
->device_lock
);
6152 bitmap_unplug(mddev
->bitmap
);
6153 spin_lock_irq(&conf
->device_lock
);
6154 conf
->seq_write
= conf
->seq_flush
;
6155 activate_bit_delay(conf
, conf
->temp_inactive_list
);
6157 raid5_activate_delayed(conf
);
6159 while ((bio
= remove_bio_from_retry(conf
))) {
6161 spin_unlock_irq(&conf
->device_lock
);
6162 ok
= retry_aligned_read(conf
, bio
);
6163 spin_lock_irq(&conf
->device_lock
);
6169 batch_size
= handle_active_stripes(conf
, ANY_GROUP
, NULL
,
6170 conf
->temp_inactive_list
);
6171 if (!batch_size
&& !released
)
6173 handled
+= batch_size
;
6175 if (mddev
->sb_flags
& ~(1 << MD_SB_CHANGE_PENDING
)) {
6176 spin_unlock_irq(&conf
->device_lock
);
6177 md_check_recovery(mddev
);
6178 spin_lock_irq(&conf
->device_lock
);
6181 pr_debug("%d stripes handled\n", handled
);
6183 spin_unlock_irq(&conf
->device_lock
);
6184 if (test_and_clear_bit(R5_ALLOC_MORE
, &conf
->cache_state
) &&
6185 mutex_trylock(&conf
->cache_size_mutex
)) {
6186 grow_one_stripe(conf
, __GFP_NOWARN
);
6187 /* Set flag even if allocation failed. This helps
6188 * slow down allocation requests when mem is short
6190 set_bit(R5_DID_ALLOC
, &conf
->cache_state
);
6191 mutex_unlock(&conf
->cache_size_mutex
);
6194 flush_deferred_bios(conf
);
6196 r5l_flush_stripe_to_raid(conf
->log
);
6198 async_tx_issue_pending_all();
6199 blk_finish_plug(&plug
);
6201 pr_debug("--- raid5d inactive\n");
6205 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
6207 struct r5conf
*conf
;
6209 spin_lock(&mddev
->lock
);
6210 conf
= mddev
->private;
6212 ret
= sprintf(page
, "%d\n", conf
->min_nr_stripes
);
6213 spin_unlock(&mddev
->lock
);
6218 raid5_set_cache_size(struct mddev
*mddev
, int size
)
6220 struct r5conf
*conf
= mddev
->private;
6223 if (size
<= 16 || size
> 32768)
6226 conf
->min_nr_stripes
= size
;
6227 mutex_lock(&conf
->cache_size_mutex
);
6228 while (size
< conf
->max_nr_stripes
&&
6229 drop_one_stripe(conf
))
6231 mutex_unlock(&conf
->cache_size_mutex
);
6234 err
= md_allow_write(mddev
);
6238 mutex_lock(&conf
->cache_size_mutex
);
6239 while (size
> conf
->max_nr_stripes
)
6240 if (!grow_one_stripe(conf
, GFP_KERNEL
))
6242 mutex_unlock(&conf
->cache_size_mutex
);
6246 EXPORT_SYMBOL(raid5_set_cache_size
);
6249 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
6251 struct r5conf
*conf
;
6255 if (len
>= PAGE_SIZE
)
6257 if (kstrtoul(page
, 10, &new))
6259 err
= mddev_lock(mddev
);
6262 conf
= mddev
->private;
6266 err
= raid5_set_cache_size(mddev
, new);
6267 mddev_unlock(mddev
);
6272 static struct md_sysfs_entry
6273 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
6274 raid5_show_stripe_cache_size
,
6275 raid5_store_stripe_cache_size
);
6278 raid5_show_rmw_level(struct mddev
*mddev
, char *page
)
6280 struct r5conf
*conf
= mddev
->private;
6282 return sprintf(page
, "%d\n", conf
->rmw_level
);
6288 raid5_store_rmw_level(struct mddev
*mddev
, const char *page
, size_t len
)
6290 struct r5conf
*conf
= mddev
->private;
6296 if (len
>= PAGE_SIZE
)
6299 if (kstrtoul(page
, 10, &new))
6302 if (new != PARITY_DISABLE_RMW
&& !raid6_call
.xor_syndrome
)
6305 if (new != PARITY_DISABLE_RMW
&&
6306 new != PARITY_ENABLE_RMW
&&
6307 new != PARITY_PREFER_RMW
)
6310 conf
->rmw_level
= new;
6314 static struct md_sysfs_entry
6315 raid5_rmw_level
= __ATTR(rmw_level
, S_IRUGO
| S_IWUSR
,
6316 raid5_show_rmw_level
,
6317 raid5_store_rmw_level
);
6321 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
6323 struct r5conf
*conf
;
6325 spin_lock(&mddev
->lock
);
6326 conf
= mddev
->private;
6328 ret
= sprintf(page
, "%d\n", conf
->bypass_threshold
);
6329 spin_unlock(&mddev
->lock
);
6334 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
6336 struct r5conf
*conf
;
6340 if (len
>= PAGE_SIZE
)
6342 if (kstrtoul(page
, 10, &new))
6345 err
= mddev_lock(mddev
);
6348 conf
= mddev
->private;
6351 else if (new > conf
->min_nr_stripes
)
6354 conf
->bypass_threshold
= new;
6355 mddev_unlock(mddev
);
6359 static struct md_sysfs_entry
6360 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
6362 raid5_show_preread_threshold
,
6363 raid5_store_preread_threshold
);
6366 raid5_show_skip_copy(struct mddev
*mddev
, char *page
)
6368 struct r5conf
*conf
;
6370 spin_lock(&mddev
->lock
);
6371 conf
= mddev
->private;
6373 ret
= sprintf(page
, "%d\n", conf
->skip_copy
);
6374 spin_unlock(&mddev
->lock
);
6379 raid5_store_skip_copy(struct mddev
*mddev
, const char *page
, size_t len
)
6381 struct r5conf
*conf
;
6385 if (len
>= PAGE_SIZE
)
6387 if (kstrtoul(page
, 10, &new))
6391 err
= mddev_lock(mddev
);
6394 conf
= mddev
->private;
6397 else if (new != conf
->skip_copy
) {
6398 mddev_suspend(mddev
);
6399 conf
->skip_copy
= new;
6401 mddev
->queue
->backing_dev_info
->capabilities
|=
6402 BDI_CAP_STABLE_WRITES
;
6404 mddev
->queue
->backing_dev_info
->capabilities
&=
6405 ~BDI_CAP_STABLE_WRITES
;
6406 mddev_resume(mddev
);
6408 mddev_unlock(mddev
);
6412 static struct md_sysfs_entry
6413 raid5_skip_copy
= __ATTR(skip_copy
, S_IRUGO
| S_IWUSR
,
6414 raid5_show_skip_copy
,
6415 raid5_store_skip_copy
);
6418 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
6420 struct r5conf
*conf
= mddev
->private;
6422 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
6427 static struct md_sysfs_entry
6428 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
6431 raid5_show_group_thread_cnt(struct mddev
*mddev
, char *page
)
6433 struct r5conf
*conf
;
6435 spin_lock(&mddev
->lock
);
6436 conf
= mddev
->private;
6438 ret
= sprintf(page
, "%d\n", conf
->worker_cnt_per_group
);
6439 spin_unlock(&mddev
->lock
);
6443 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6445 int *worker_cnt_per_group
,
6446 struct r5worker_group
**worker_groups
);
6448 raid5_store_group_thread_cnt(struct mddev
*mddev
, const char *page
, size_t len
)
6450 struct r5conf
*conf
;
6453 struct r5worker_group
*new_groups
, *old_groups
;
6454 int group_cnt
, worker_cnt_per_group
;
6456 if (len
>= PAGE_SIZE
)
6458 if (kstrtoul(page
, 10, &new))
6461 err
= mddev_lock(mddev
);
6464 conf
= mddev
->private;
6467 else if (new != conf
->worker_cnt_per_group
) {
6468 mddev_suspend(mddev
);
6470 old_groups
= conf
->worker_groups
;
6472 flush_workqueue(raid5_wq
);
6474 err
= alloc_thread_groups(conf
, new,
6475 &group_cnt
, &worker_cnt_per_group
,
6478 spin_lock_irq(&conf
->device_lock
);
6479 conf
->group_cnt
= group_cnt
;
6480 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6481 conf
->worker_groups
= new_groups
;
6482 spin_unlock_irq(&conf
->device_lock
);
6485 kfree(old_groups
[0].workers
);
6488 mddev_resume(mddev
);
6490 mddev_unlock(mddev
);
6495 static struct md_sysfs_entry
6496 raid5_group_thread_cnt
= __ATTR(group_thread_cnt
, S_IRUGO
| S_IWUSR
,
6497 raid5_show_group_thread_cnt
,
6498 raid5_store_group_thread_cnt
);
6500 static struct attribute
*raid5_attrs
[] = {
6501 &raid5_stripecache_size
.attr
,
6502 &raid5_stripecache_active
.attr
,
6503 &raid5_preread_bypass_threshold
.attr
,
6504 &raid5_group_thread_cnt
.attr
,
6505 &raid5_skip_copy
.attr
,
6506 &raid5_rmw_level
.attr
,
6507 &r5c_journal_mode
.attr
,
6510 static struct attribute_group raid5_attrs_group
= {
6512 .attrs
= raid5_attrs
,
6515 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6517 int *worker_cnt_per_group
,
6518 struct r5worker_group
**worker_groups
)
6522 struct r5worker
*workers
;
6524 *worker_cnt_per_group
= cnt
;
6527 *worker_groups
= NULL
;
6530 *group_cnt
= num_possible_nodes();
6531 size
= sizeof(struct r5worker
) * cnt
;
6532 workers
= kzalloc(size
* *group_cnt
, GFP_NOIO
);
6533 *worker_groups
= kzalloc(sizeof(struct r5worker_group
) *
6534 *group_cnt
, GFP_NOIO
);
6535 if (!*worker_groups
|| !workers
) {
6537 kfree(*worker_groups
);
6541 for (i
= 0; i
< *group_cnt
; i
++) {
6542 struct r5worker_group
*group
;
6544 group
= &(*worker_groups
)[i
];
6545 INIT_LIST_HEAD(&group
->handle_list
);
6547 group
->workers
= workers
+ i
* cnt
;
6549 for (j
= 0; j
< cnt
; j
++) {
6550 struct r5worker
*worker
= group
->workers
+ j
;
6551 worker
->group
= group
;
6552 INIT_WORK(&worker
->work
, raid5_do_work
);
6554 for (k
= 0; k
< NR_STRIPE_HASH_LOCKS
; k
++)
6555 INIT_LIST_HEAD(worker
->temp_inactive_list
+ k
);
6562 static void free_thread_groups(struct r5conf
*conf
)
6564 if (conf
->worker_groups
)
6565 kfree(conf
->worker_groups
[0].workers
);
6566 kfree(conf
->worker_groups
);
6567 conf
->worker_groups
= NULL
;
6571 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
6573 struct r5conf
*conf
= mddev
->private;
6576 sectors
= mddev
->dev_sectors
;
6578 /* size is defined by the smallest of previous and new size */
6579 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
6581 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
6582 sectors
&= ~((sector_t
)conf
->prev_chunk_sectors
- 1);
6583 return sectors
* (raid_disks
- conf
->max_degraded
);
6586 static void free_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6588 safe_put_page(percpu
->spare_page
);
6589 if (percpu
->scribble
)
6590 flex_array_free(percpu
->scribble
);
6591 percpu
->spare_page
= NULL
;
6592 percpu
->scribble
= NULL
;
6595 static int alloc_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6597 if (conf
->level
== 6 && !percpu
->spare_page
)
6598 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
6599 if (!percpu
->scribble
)
6600 percpu
->scribble
= scribble_alloc(max(conf
->raid_disks
,
6601 conf
->previous_raid_disks
),
6602 max(conf
->chunk_sectors
,
6603 conf
->prev_chunk_sectors
)
6607 if (!percpu
->scribble
|| (conf
->level
== 6 && !percpu
->spare_page
)) {
6608 free_scratch_buffer(conf
, percpu
);
6615 static int raid456_cpu_dead(unsigned int cpu
, struct hlist_node
*node
)
6617 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6619 free_scratch_buffer(conf
, per_cpu_ptr(conf
->percpu
, cpu
));
6623 static void raid5_free_percpu(struct r5conf
*conf
)
6628 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6629 free_percpu(conf
->percpu
);
6632 static void free_conf(struct r5conf
*conf
)
6637 r5l_exit_log(conf
->log
);
6638 if (conf
->shrinker
.nr_deferred
)
6639 unregister_shrinker(&conf
->shrinker
);
6641 free_thread_groups(conf
);
6642 shrink_stripes(conf
);
6643 raid5_free_percpu(conf
);
6644 for (i
= 0; i
< conf
->pool_size
; i
++)
6645 if (conf
->disks
[i
].extra_page
)
6646 put_page(conf
->disks
[i
].extra_page
);
6648 kfree(conf
->stripe_hashtbl
);
6652 static int raid456_cpu_up_prepare(unsigned int cpu
, struct hlist_node
*node
)
6654 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6655 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
6657 if (alloc_scratch_buffer(conf
, percpu
)) {
6658 pr_warn("%s: failed memory allocation for cpu%u\n",
6665 static int raid5_alloc_percpu(struct r5conf
*conf
)
6669 conf
->percpu
= alloc_percpu(struct raid5_percpu
);
6673 err
= cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6675 conf
->scribble_disks
= max(conf
->raid_disks
,
6676 conf
->previous_raid_disks
);
6677 conf
->scribble_sectors
= max(conf
->chunk_sectors
,
6678 conf
->prev_chunk_sectors
);
6683 static unsigned long raid5_cache_scan(struct shrinker
*shrink
,
6684 struct shrink_control
*sc
)
6686 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6687 unsigned long ret
= SHRINK_STOP
;
6689 if (mutex_trylock(&conf
->cache_size_mutex
)) {
6691 while (ret
< sc
->nr_to_scan
&&
6692 conf
->max_nr_stripes
> conf
->min_nr_stripes
) {
6693 if (drop_one_stripe(conf
) == 0) {
6699 mutex_unlock(&conf
->cache_size_mutex
);
6704 static unsigned long raid5_cache_count(struct shrinker
*shrink
,
6705 struct shrink_control
*sc
)
6707 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6709 if (conf
->max_nr_stripes
< conf
->min_nr_stripes
)
6710 /* unlikely, but not impossible */
6712 return conf
->max_nr_stripes
- conf
->min_nr_stripes
;
6715 static struct r5conf
*setup_conf(struct mddev
*mddev
)
6717 struct r5conf
*conf
;
6718 int raid_disk
, memory
, max_disks
;
6719 struct md_rdev
*rdev
;
6720 struct disk_info
*disk
;
6723 int group_cnt
, worker_cnt_per_group
;
6724 struct r5worker_group
*new_group
;
6726 if (mddev
->new_level
!= 5
6727 && mddev
->new_level
!= 4
6728 && mddev
->new_level
!= 6) {
6729 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6730 mdname(mddev
), mddev
->new_level
);
6731 return ERR_PTR(-EIO
);
6733 if ((mddev
->new_level
== 5
6734 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
6735 (mddev
->new_level
== 6
6736 && !algorithm_valid_raid6(mddev
->new_layout
))) {
6737 pr_warn("md/raid:%s: layout %d not supported\n",
6738 mdname(mddev
), mddev
->new_layout
);
6739 return ERR_PTR(-EIO
);
6741 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
6742 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6743 mdname(mddev
), mddev
->raid_disks
);
6744 return ERR_PTR(-EINVAL
);
6747 if (!mddev
->new_chunk_sectors
||
6748 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
6749 !is_power_of_2(mddev
->new_chunk_sectors
)) {
6750 pr_warn("md/raid:%s: invalid chunk size %d\n",
6751 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
6752 return ERR_PTR(-EINVAL
);
6755 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
6758 /* Don't enable multi-threading by default*/
6759 if (!alloc_thread_groups(conf
, 0, &group_cnt
, &worker_cnt_per_group
,
6761 conf
->group_cnt
= group_cnt
;
6762 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6763 conf
->worker_groups
= new_group
;
6766 spin_lock_init(&conf
->device_lock
);
6767 seqcount_init(&conf
->gen_lock
);
6768 mutex_init(&conf
->cache_size_mutex
);
6769 init_waitqueue_head(&conf
->wait_for_quiescent
);
6770 init_waitqueue_head(&conf
->wait_for_stripe
);
6771 init_waitqueue_head(&conf
->wait_for_overlap
);
6772 INIT_LIST_HEAD(&conf
->handle_list
);
6773 INIT_LIST_HEAD(&conf
->hold_list
);
6774 INIT_LIST_HEAD(&conf
->delayed_list
);
6775 INIT_LIST_HEAD(&conf
->bitmap_list
);
6776 bio_list_init(&conf
->return_bi
);
6777 init_llist_head(&conf
->released_stripes
);
6778 atomic_set(&conf
->active_stripes
, 0);
6779 atomic_set(&conf
->preread_active_stripes
, 0);
6780 atomic_set(&conf
->active_aligned_reads
, 0);
6781 bio_list_init(&conf
->pending_bios
);
6782 spin_lock_init(&conf
->pending_bios_lock
);
6783 conf
->batch_bio_dispatch
= true;
6784 rdev_for_each(rdev
, mddev
) {
6785 if (test_bit(Journal
, &rdev
->flags
))
6787 if (blk_queue_nonrot(bdev_get_queue(rdev
->bdev
))) {
6788 conf
->batch_bio_dispatch
= false;
6793 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
6794 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
6796 conf
->raid_disks
= mddev
->raid_disks
;
6797 if (mddev
->reshape_position
== MaxSector
)
6798 conf
->previous_raid_disks
= mddev
->raid_disks
;
6800 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6801 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
6803 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
6809 for (i
= 0; i
< max_disks
; i
++) {
6810 conf
->disks
[i
].extra_page
= alloc_page(GFP_KERNEL
);
6811 if (!conf
->disks
[i
].extra_page
)
6815 conf
->mddev
= mddev
;
6817 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
6820 /* We init hash_locks[0] separately to that it can be used
6821 * as the reference lock in the spin_lock_nest_lock() call
6822 * in lock_all_device_hash_locks_irq in order to convince
6823 * lockdep that we know what we are doing.
6825 spin_lock_init(conf
->hash_locks
);
6826 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6827 spin_lock_init(conf
->hash_locks
+ i
);
6829 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6830 INIT_LIST_HEAD(conf
->inactive_list
+ i
);
6832 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6833 INIT_LIST_HEAD(conf
->temp_inactive_list
+ i
);
6835 atomic_set(&conf
->r5c_cached_full_stripes
, 0);
6836 INIT_LIST_HEAD(&conf
->r5c_full_stripe_list
);
6837 atomic_set(&conf
->r5c_cached_partial_stripes
, 0);
6838 INIT_LIST_HEAD(&conf
->r5c_partial_stripe_list
);
6839 atomic_set(&conf
->r5c_flushing_full_stripes
, 0);
6840 atomic_set(&conf
->r5c_flushing_partial_stripes
, 0);
6842 conf
->level
= mddev
->new_level
;
6843 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
6844 if (raid5_alloc_percpu(conf
) != 0)
6847 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
6849 rdev_for_each(rdev
, mddev
) {
6850 raid_disk
= rdev
->raid_disk
;
6851 if (raid_disk
>= max_disks
6852 || raid_disk
< 0 || test_bit(Journal
, &rdev
->flags
))
6854 disk
= conf
->disks
+ raid_disk
;
6856 if (test_bit(Replacement
, &rdev
->flags
)) {
6857 if (disk
->replacement
)
6859 disk
->replacement
= rdev
;
6866 if (test_bit(In_sync
, &rdev
->flags
)) {
6867 char b
[BDEVNAME_SIZE
];
6868 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6869 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
6870 } else if (rdev
->saved_raid_disk
!= raid_disk
)
6871 /* Cannot rely on bitmap to complete recovery */
6875 conf
->level
= mddev
->new_level
;
6876 if (conf
->level
== 6) {
6877 conf
->max_degraded
= 2;
6878 if (raid6_call
.xor_syndrome
)
6879 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6881 conf
->rmw_level
= PARITY_DISABLE_RMW
;
6883 conf
->max_degraded
= 1;
6884 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6886 conf
->algorithm
= mddev
->new_layout
;
6887 conf
->reshape_progress
= mddev
->reshape_position
;
6888 if (conf
->reshape_progress
!= MaxSector
) {
6889 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
6890 conf
->prev_algo
= mddev
->layout
;
6892 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
6893 conf
->prev_algo
= conf
->algorithm
;
6896 conf
->min_nr_stripes
= NR_STRIPES
;
6897 if (mddev
->reshape_position
!= MaxSector
) {
6898 int stripes
= max_t(int,
6899 ((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4,
6900 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4);
6901 conf
->min_nr_stripes
= max(NR_STRIPES
, stripes
);
6902 if (conf
->min_nr_stripes
!= NR_STRIPES
)
6903 pr_info("md/raid:%s: force stripe size %d for reshape\n",
6904 mdname(mddev
), conf
->min_nr_stripes
);
6906 memory
= conf
->min_nr_stripes
* (sizeof(struct stripe_head
) +
6907 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
6908 atomic_set(&conf
->empty_inactive_list_nr
, NR_STRIPE_HASH_LOCKS
);
6909 if (grow_stripes(conf
, conf
->min_nr_stripes
)) {
6910 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6911 mdname(mddev
), memory
);
6914 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev
), memory
);
6916 * Losing a stripe head costs more than the time to refill it,
6917 * it reduces the queue depth and so can hurt throughput.
6918 * So set it rather large, scaled by number of devices.
6920 conf
->shrinker
.seeks
= DEFAULT_SEEKS
* conf
->raid_disks
* 4;
6921 conf
->shrinker
.scan_objects
= raid5_cache_scan
;
6922 conf
->shrinker
.count_objects
= raid5_cache_count
;
6923 conf
->shrinker
.batch
= 128;
6924 conf
->shrinker
.flags
= 0;
6925 if (register_shrinker(&conf
->shrinker
)) {
6926 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6931 sprintf(pers_name
, "raid%d", mddev
->new_level
);
6932 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
6933 if (!conf
->thread
) {
6934 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6944 return ERR_PTR(-EIO
);
6946 return ERR_PTR(-ENOMEM
);
6949 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
6952 case ALGORITHM_PARITY_0
:
6953 if (raid_disk
< max_degraded
)
6956 case ALGORITHM_PARITY_N
:
6957 if (raid_disk
>= raid_disks
- max_degraded
)
6960 case ALGORITHM_PARITY_0_6
:
6961 if (raid_disk
== 0 ||
6962 raid_disk
== raid_disks
- 1)
6965 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6966 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6967 case ALGORITHM_LEFT_SYMMETRIC_6
:
6968 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6969 if (raid_disk
== raid_disks
- 1)
6975 static int raid5_run(struct mddev
*mddev
)
6977 struct r5conf
*conf
;
6978 int working_disks
= 0;
6979 int dirty_parity_disks
= 0;
6980 struct md_rdev
*rdev
;
6981 struct md_rdev
*journal_dev
= NULL
;
6982 sector_t reshape_offset
= 0;
6984 long long min_offset_diff
= 0;
6987 if (mddev
->recovery_cp
!= MaxSector
)
6988 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6991 rdev_for_each(rdev
, mddev
) {
6994 if (test_bit(Journal
, &rdev
->flags
)) {
6998 if (rdev
->raid_disk
< 0)
7000 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
7002 min_offset_diff
= diff
;
7004 } else if (mddev
->reshape_backwards
&&
7005 diff
< min_offset_diff
)
7006 min_offset_diff
= diff
;
7007 else if (!mddev
->reshape_backwards
&&
7008 diff
> min_offset_diff
)
7009 min_offset_diff
= diff
;
7012 if (mddev
->reshape_position
!= MaxSector
) {
7013 /* Check that we can continue the reshape.
7014 * Difficulties arise if the stripe we would write to
7015 * next is at or after the stripe we would read from next.
7016 * For a reshape that changes the number of devices, this
7017 * is only possible for a very short time, and mdadm makes
7018 * sure that time appears to have past before assembling
7019 * the array. So we fail if that time hasn't passed.
7020 * For a reshape that keeps the number of devices the same
7021 * mdadm must be monitoring the reshape can keeping the
7022 * critical areas read-only and backed up. It will start
7023 * the array in read-only mode, so we check for that.
7025 sector_t here_new
, here_old
;
7027 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
7032 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7037 if (mddev
->new_level
!= mddev
->level
) {
7038 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7042 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
7043 /* reshape_position must be on a new-stripe boundary, and one
7044 * further up in new geometry must map after here in old
7046 * If the chunk sizes are different, then as we perform reshape
7047 * in units of the largest of the two, reshape_position needs
7048 * be a multiple of the largest chunk size times new data disks.
7050 here_new
= mddev
->reshape_position
;
7051 chunk_sectors
= max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
);
7052 new_data_disks
= mddev
->raid_disks
- max_degraded
;
7053 if (sector_div(here_new
, chunk_sectors
* new_data_disks
)) {
7054 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7058 reshape_offset
= here_new
* chunk_sectors
;
7059 /* here_new is the stripe we will write to */
7060 here_old
= mddev
->reshape_position
;
7061 sector_div(here_old
, chunk_sectors
* (old_disks
-max_degraded
));
7062 /* here_old is the first stripe that we might need to read
7064 if (mddev
->delta_disks
== 0) {
7065 /* We cannot be sure it is safe to start an in-place
7066 * reshape. It is only safe if user-space is monitoring
7067 * and taking constant backups.
7068 * mdadm always starts a situation like this in
7069 * readonly mode so it can take control before
7070 * allowing any writes. So just check for that.
7072 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
7073 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
7074 /* not really in-place - so OK */;
7075 else if (mddev
->ro
== 0) {
7076 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7080 } else if (mddev
->reshape_backwards
7081 ? (here_new
* chunk_sectors
+ min_offset_diff
<=
7082 here_old
* chunk_sectors
)
7083 : (here_new
* chunk_sectors
>=
7084 here_old
* chunk_sectors
+ (-min_offset_diff
))) {
7085 /* Reading from the same stripe as writing to - bad */
7086 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7090 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev
));
7091 /* OK, we should be able to continue; */
7093 BUG_ON(mddev
->level
!= mddev
->new_level
);
7094 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
7095 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
7096 BUG_ON(mddev
->delta_disks
!= 0);
7099 if (mddev
->private == NULL
)
7100 conf
= setup_conf(mddev
);
7102 conf
= mddev
->private;
7105 return PTR_ERR(conf
);
7107 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
7109 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7112 set_disk_ro(mddev
->gendisk
, 1);
7113 } else if (mddev
->recovery_cp
== MaxSector
)
7114 set_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
7117 conf
->min_offset_diff
= min_offset_diff
;
7118 mddev
->thread
= conf
->thread
;
7119 conf
->thread
= NULL
;
7120 mddev
->private = conf
;
7122 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
7124 rdev
= conf
->disks
[i
].rdev
;
7125 if (!rdev
&& conf
->disks
[i
].replacement
) {
7126 /* The replacement is all we have yet */
7127 rdev
= conf
->disks
[i
].replacement
;
7128 conf
->disks
[i
].replacement
= NULL
;
7129 clear_bit(Replacement
, &rdev
->flags
);
7130 conf
->disks
[i
].rdev
= rdev
;
7134 if (conf
->disks
[i
].replacement
&&
7135 conf
->reshape_progress
!= MaxSector
) {
7136 /* replacements and reshape simply do not mix. */
7137 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
7140 if (test_bit(In_sync
, &rdev
->flags
)) {
7144 /* This disc is not fully in-sync. However if it
7145 * just stored parity (beyond the recovery_offset),
7146 * when we don't need to be concerned about the
7147 * array being dirty.
7148 * When reshape goes 'backwards', we never have
7149 * partially completed devices, so we only need
7150 * to worry about reshape going forwards.
7152 /* Hack because v0.91 doesn't store recovery_offset properly. */
7153 if (mddev
->major_version
== 0 &&
7154 mddev
->minor_version
> 90)
7155 rdev
->recovery_offset
= reshape_offset
;
7157 if (rdev
->recovery_offset
< reshape_offset
) {
7158 /* We need to check old and new layout */
7159 if (!only_parity(rdev
->raid_disk
,
7162 conf
->max_degraded
))
7165 if (!only_parity(rdev
->raid_disk
,
7167 conf
->previous_raid_disks
,
7168 conf
->max_degraded
))
7170 dirty_parity_disks
++;
7174 * 0 for a fully functional array, 1 or 2 for a degraded array.
7176 mddev
->degraded
= raid5_calc_degraded(conf
);
7178 if (has_failed(conf
)) {
7179 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
7180 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
7184 /* device size must be a multiple of chunk size */
7185 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
7186 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
7188 if (mddev
->degraded
> dirty_parity_disks
&&
7189 mddev
->recovery_cp
!= MaxSector
) {
7190 if (mddev
->ok_start_degraded
)
7191 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7194 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7200 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7201 mdname(mddev
), conf
->level
,
7202 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
7205 print_raid5_conf(conf
);
7207 if (conf
->reshape_progress
!= MaxSector
) {
7208 conf
->reshape_safe
= conf
->reshape_progress
;
7209 atomic_set(&conf
->reshape_stripes
, 0);
7210 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7211 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7212 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7213 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7214 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7218 /* Ok, everything is just fine now */
7219 if (mddev
->to_remove
== &raid5_attrs_group
)
7220 mddev
->to_remove
= NULL
;
7221 else if (mddev
->kobj
.sd
&&
7222 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
7223 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7225 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7229 bool discard_supported
= true;
7230 /* read-ahead size must cover two whole stripes, which
7231 * is 2 * (datadisks) * chunksize where 'n' is the
7232 * number of raid devices
7234 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
7235 int stripe
= data_disks
*
7236 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
7237 if (mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
7238 mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
7240 chunk_size
= mddev
->chunk_sectors
<< 9;
7241 blk_queue_io_min(mddev
->queue
, chunk_size
);
7242 blk_queue_io_opt(mddev
->queue
, chunk_size
*
7243 (conf
->raid_disks
- conf
->max_degraded
));
7244 mddev
->queue
->limits
.raid_partial_stripes_expensive
= 1;
7246 * We can only discard a whole stripe. It doesn't make sense to
7247 * discard data disk but write parity disk
7249 stripe
= stripe
* PAGE_SIZE
;
7250 /* Round up to power of 2, as discard handling
7251 * currently assumes that */
7252 while ((stripe
-1) & stripe
)
7253 stripe
= (stripe
| (stripe
-1)) + 1;
7254 mddev
->queue
->limits
.discard_alignment
= stripe
;
7255 mddev
->queue
->limits
.discard_granularity
= stripe
;
7258 * We use 16-bit counter of active stripes in bi_phys_segments
7259 * (minus one for over-loaded initialization)
7261 blk_queue_max_hw_sectors(mddev
->queue
, 0xfffe * STRIPE_SECTORS
);
7262 blk_queue_max_discard_sectors(mddev
->queue
,
7263 0xfffe * STRIPE_SECTORS
);
7266 * unaligned part of discard request will be ignored, so can't
7267 * guarantee discard_zeroes_data
7269 mddev
->queue
->limits
.discard_zeroes_data
= 0;
7271 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
7273 rdev_for_each(rdev
, mddev
) {
7274 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7275 rdev
->data_offset
<< 9);
7276 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7277 rdev
->new_data_offset
<< 9);
7279 * discard_zeroes_data is required, otherwise data
7280 * could be lost. Consider a scenario: discard a stripe
7281 * (the stripe could be inconsistent if
7282 * discard_zeroes_data is 0); write one disk of the
7283 * stripe (the stripe could be inconsistent again
7284 * depending on which disks are used to calculate
7285 * parity); the disk is broken; The stripe data of this
7288 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
7289 !bdev_get_queue(rdev
->bdev
)->
7290 limits
.discard_zeroes_data
)
7291 discard_supported
= false;
7292 /* Unfortunately, discard_zeroes_data is not currently
7293 * a guarantee - just a hint. So we only allow DISCARD
7294 * if the sysadmin has confirmed that only safe devices
7295 * are in use by setting a module parameter.
7297 if (!devices_handle_discard_safely
) {
7298 if (discard_supported
) {
7299 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7300 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7302 discard_supported
= false;
7306 if (discard_supported
&&
7307 mddev
->queue
->limits
.max_discard_sectors
>= (stripe
>> 9) &&
7308 mddev
->queue
->limits
.discard_granularity
>= stripe
)
7309 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
7312 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
7315 blk_queue_max_hw_sectors(mddev
->queue
, UINT_MAX
);
7319 char b
[BDEVNAME_SIZE
];
7321 pr_debug("md/raid:%s: using device %s as journal\n",
7322 mdname(mddev
), bdevname(journal_dev
->bdev
, b
));
7323 if (r5l_init_log(conf
, journal_dev
))
7329 md_unregister_thread(&mddev
->thread
);
7330 print_raid5_conf(conf
);
7332 mddev
->private = NULL
;
7333 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev
));
7337 static void raid5_free(struct mddev
*mddev
, void *priv
)
7339 struct r5conf
*conf
= priv
;
7342 mddev
->to_remove
= &raid5_attrs_group
;
7345 static void raid5_status(struct seq_file
*seq
, struct mddev
*mddev
)
7347 struct r5conf
*conf
= mddev
->private;
7350 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
7351 conf
->chunk_sectors
/ 2, mddev
->layout
);
7352 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
7354 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7355 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
7356 seq_printf (seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
7359 seq_printf (seq
, "]");
7362 static void print_raid5_conf (struct r5conf
*conf
)
7365 struct disk_info
*tmp
;
7367 pr_debug("RAID conf printout:\n");
7369 pr_debug("(conf==NULL)\n");
7372 pr_debug(" --- level:%d rd:%d wd:%d\n", conf
->level
,
7374 conf
->raid_disks
- conf
->mddev
->degraded
);
7376 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7377 char b
[BDEVNAME_SIZE
];
7378 tmp
= conf
->disks
+ i
;
7380 pr_debug(" disk %d, o:%d, dev:%s\n",
7381 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
7382 bdevname(tmp
->rdev
->bdev
, b
));
7386 static int raid5_spare_active(struct mddev
*mddev
)
7389 struct r5conf
*conf
= mddev
->private;
7390 struct disk_info
*tmp
;
7392 unsigned long flags
;
7394 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7395 tmp
= conf
->disks
+ i
;
7396 if (tmp
->replacement
7397 && tmp
->replacement
->recovery_offset
== MaxSector
7398 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
7399 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
7400 /* Replacement has just become active. */
7402 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
7405 /* Replaced device not technically faulty,
7406 * but we need to be sure it gets removed
7407 * and never re-added.
7409 set_bit(Faulty
, &tmp
->rdev
->flags
);
7410 sysfs_notify_dirent_safe(
7411 tmp
->rdev
->sysfs_state
);
7413 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
7414 } else if (tmp
->rdev
7415 && tmp
->rdev
->recovery_offset
== MaxSector
7416 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
7417 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
7419 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
7422 spin_lock_irqsave(&conf
->device_lock
, flags
);
7423 mddev
->degraded
= raid5_calc_degraded(conf
);
7424 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7425 print_raid5_conf(conf
);
7429 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7431 struct r5conf
*conf
= mddev
->private;
7433 int number
= rdev
->raid_disk
;
7434 struct md_rdev
**rdevp
;
7435 struct disk_info
*p
= conf
->disks
+ number
;
7437 print_raid5_conf(conf
);
7438 if (test_bit(Journal
, &rdev
->flags
) && conf
->log
) {
7439 struct r5l_log
*log
;
7441 * we can't wait pending write here, as this is called in
7442 * raid5d, wait will deadlock.
7444 if (atomic_read(&mddev
->writes_pending
))
7452 if (rdev
== p
->rdev
)
7454 else if (rdev
== p
->replacement
)
7455 rdevp
= &p
->replacement
;
7459 if (number
>= conf
->raid_disks
&&
7460 conf
->reshape_progress
== MaxSector
)
7461 clear_bit(In_sync
, &rdev
->flags
);
7463 if (test_bit(In_sync
, &rdev
->flags
) ||
7464 atomic_read(&rdev
->nr_pending
)) {
7468 /* Only remove non-faulty devices if recovery
7471 if (!test_bit(Faulty
, &rdev
->flags
) &&
7472 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
7473 !has_failed(conf
) &&
7474 (!p
->replacement
|| p
->replacement
== rdev
) &&
7475 number
< conf
->raid_disks
) {
7480 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
7482 if (atomic_read(&rdev
->nr_pending
)) {
7483 /* lost the race, try later */
7488 if (p
->replacement
) {
7489 /* We must have just cleared 'rdev' */
7490 p
->rdev
= p
->replacement
;
7491 clear_bit(Replacement
, &p
->replacement
->flags
);
7492 smp_mb(); /* Make sure other CPUs may see both as identical
7493 * but will never see neither - if they are careful
7495 p
->replacement
= NULL
;
7496 clear_bit(WantReplacement
, &rdev
->flags
);
7498 /* We might have just removed the Replacement as faulty-
7499 * clear the bit just in case
7501 clear_bit(WantReplacement
, &rdev
->flags
);
7504 print_raid5_conf(conf
);
7508 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7510 struct r5conf
*conf
= mddev
->private;
7513 struct disk_info
*p
;
7515 int last
= conf
->raid_disks
- 1;
7517 if (test_bit(Journal
, &rdev
->flags
)) {
7518 char b
[BDEVNAME_SIZE
];
7522 rdev
->raid_disk
= 0;
7524 * The array is in readonly mode if journal is missing, so no
7525 * write requests running. We should be safe
7527 r5l_init_log(conf
, rdev
);
7528 pr_debug("md/raid:%s: using device %s as journal\n",
7529 mdname(mddev
), bdevname(rdev
->bdev
, b
));
7532 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
7535 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
7536 /* no point adding a device */
7539 if (rdev
->raid_disk
>= 0)
7540 first
= last
= rdev
->raid_disk
;
7543 * find the disk ... but prefer rdev->saved_raid_disk
7546 if (rdev
->saved_raid_disk
>= 0 &&
7547 rdev
->saved_raid_disk
>= first
&&
7548 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
7549 first
= rdev
->saved_raid_disk
;
7551 for (disk
= first
; disk
<= last
; disk
++) {
7552 p
= conf
->disks
+ disk
;
7553 if (p
->rdev
== NULL
) {
7554 clear_bit(In_sync
, &rdev
->flags
);
7555 rdev
->raid_disk
= disk
;
7557 if (rdev
->saved_raid_disk
!= disk
)
7559 rcu_assign_pointer(p
->rdev
, rdev
);
7563 for (disk
= first
; disk
<= last
; disk
++) {
7564 p
= conf
->disks
+ disk
;
7565 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
7566 p
->replacement
== NULL
) {
7567 clear_bit(In_sync
, &rdev
->flags
);
7568 set_bit(Replacement
, &rdev
->flags
);
7569 rdev
->raid_disk
= disk
;
7572 rcu_assign_pointer(p
->replacement
, rdev
);
7577 print_raid5_conf(conf
);
7581 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
7583 /* no resync is happening, and there is enough space
7584 * on all devices, so we can resize.
7585 * We need to make sure resync covers any new space.
7586 * If the array is shrinking we should possibly wait until
7587 * any io in the removed space completes, but it hardly seems
7591 struct r5conf
*conf
= mddev
->private;
7595 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
7596 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
7597 if (mddev
->external_size
&&
7598 mddev
->array_sectors
> newsize
)
7600 if (mddev
->bitmap
) {
7601 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
7605 md_set_array_sectors(mddev
, newsize
);
7606 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7607 revalidate_disk(mddev
->gendisk
);
7608 if (sectors
> mddev
->dev_sectors
&&
7609 mddev
->recovery_cp
> mddev
->dev_sectors
) {
7610 mddev
->recovery_cp
= mddev
->dev_sectors
;
7611 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7613 mddev
->dev_sectors
= sectors
;
7614 mddev
->resync_max_sectors
= sectors
;
7618 static int check_stripe_cache(struct mddev
*mddev
)
7620 /* Can only proceed if there are plenty of stripe_heads.
7621 * We need a minimum of one full stripe,, and for sensible progress
7622 * it is best to have about 4 times that.
7623 * If we require 4 times, then the default 256 4K stripe_heads will
7624 * allow for chunk sizes up to 256K, which is probably OK.
7625 * If the chunk size is greater, user-space should request more
7626 * stripe_heads first.
7628 struct r5conf
*conf
= mddev
->private;
7629 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7630 > conf
->min_nr_stripes
||
7631 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7632 > conf
->min_nr_stripes
) {
7633 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7635 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
7642 static int check_reshape(struct mddev
*mddev
)
7644 struct r5conf
*conf
= mddev
->private;
7648 if (mddev
->delta_disks
== 0 &&
7649 mddev
->new_layout
== mddev
->layout
&&
7650 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
7651 return 0; /* nothing to do */
7652 if (has_failed(conf
))
7654 if (mddev
->delta_disks
< 0 && mddev
->reshape_position
== MaxSector
) {
7655 /* We might be able to shrink, but the devices must
7656 * be made bigger first.
7657 * For raid6, 4 is the minimum size.
7658 * Otherwise 2 is the minimum
7661 if (mddev
->level
== 6)
7663 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
7667 if (!check_stripe_cache(mddev
))
7670 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
||
7671 mddev
->delta_disks
> 0)
7672 if (resize_chunks(conf
,
7673 conf
->previous_raid_disks
7674 + max(0, mddev
->delta_disks
),
7675 max(mddev
->new_chunk_sectors
,
7676 mddev
->chunk_sectors
)
7679 return resize_stripes(conf
, (conf
->previous_raid_disks
7680 + mddev
->delta_disks
));
7683 static int raid5_start_reshape(struct mddev
*mddev
)
7685 struct r5conf
*conf
= mddev
->private;
7686 struct md_rdev
*rdev
;
7688 unsigned long flags
;
7690 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
7693 if (!check_stripe_cache(mddev
))
7696 if (has_failed(conf
))
7699 rdev_for_each(rdev
, mddev
) {
7700 if (!test_bit(In_sync
, &rdev
->flags
)
7701 && !test_bit(Faulty
, &rdev
->flags
))
7705 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
7706 /* Not enough devices even to make a degraded array
7711 /* Refuse to reduce size of the array. Any reductions in
7712 * array size must be through explicit setting of array_size
7715 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
7716 < mddev
->array_sectors
) {
7717 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7722 atomic_set(&conf
->reshape_stripes
, 0);
7723 spin_lock_irq(&conf
->device_lock
);
7724 write_seqcount_begin(&conf
->gen_lock
);
7725 conf
->previous_raid_disks
= conf
->raid_disks
;
7726 conf
->raid_disks
+= mddev
->delta_disks
;
7727 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
7728 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
7729 conf
->prev_algo
= conf
->algorithm
;
7730 conf
->algorithm
= mddev
->new_layout
;
7732 /* Code that selects data_offset needs to see the generation update
7733 * if reshape_progress has been set - so a memory barrier needed.
7736 if (mddev
->reshape_backwards
)
7737 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
7739 conf
->reshape_progress
= 0;
7740 conf
->reshape_safe
= conf
->reshape_progress
;
7741 write_seqcount_end(&conf
->gen_lock
);
7742 spin_unlock_irq(&conf
->device_lock
);
7744 /* Now make sure any requests that proceeded on the assumption
7745 * the reshape wasn't running - like Discard or Read - have
7748 mddev_suspend(mddev
);
7749 mddev_resume(mddev
);
7751 /* Add some new drives, as many as will fit.
7752 * We know there are enough to make the newly sized array work.
7753 * Don't add devices if we are reducing the number of
7754 * devices in the array. This is because it is not possible
7755 * to correctly record the "partially reconstructed" state of
7756 * such devices during the reshape and confusion could result.
7758 if (mddev
->delta_disks
>= 0) {
7759 rdev_for_each(rdev
, mddev
)
7760 if (rdev
->raid_disk
< 0 &&
7761 !test_bit(Faulty
, &rdev
->flags
)) {
7762 if (raid5_add_disk(mddev
, rdev
) == 0) {
7764 >= conf
->previous_raid_disks
)
7765 set_bit(In_sync
, &rdev
->flags
);
7767 rdev
->recovery_offset
= 0;
7769 if (sysfs_link_rdev(mddev
, rdev
))
7770 /* Failure here is OK */;
7772 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
7773 && !test_bit(Faulty
, &rdev
->flags
)) {
7774 /* This is a spare that was manually added */
7775 set_bit(In_sync
, &rdev
->flags
);
7778 /* When a reshape changes the number of devices,
7779 * ->degraded is measured against the larger of the
7780 * pre and post number of devices.
7782 spin_lock_irqsave(&conf
->device_lock
, flags
);
7783 mddev
->degraded
= raid5_calc_degraded(conf
);
7784 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7786 mddev
->raid_disks
= conf
->raid_disks
;
7787 mddev
->reshape_position
= conf
->reshape_progress
;
7788 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7790 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7791 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7792 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7793 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7794 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7795 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7797 if (!mddev
->sync_thread
) {
7798 mddev
->recovery
= 0;
7799 spin_lock_irq(&conf
->device_lock
);
7800 write_seqcount_begin(&conf
->gen_lock
);
7801 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
7802 mddev
->new_chunk_sectors
=
7803 conf
->chunk_sectors
= conf
->prev_chunk_sectors
;
7804 mddev
->new_layout
= conf
->algorithm
= conf
->prev_algo
;
7805 rdev_for_each(rdev
, mddev
)
7806 rdev
->new_data_offset
= rdev
->data_offset
;
7808 conf
->generation
--;
7809 conf
->reshape_progress
= MaxSector
;
7810 mddev
->reshape_position
= MaxSector
;
7811 write_seqcount_end(&conf
->gen_lock
);
7812 spin_unlock_irq(&conf
->device_lock
);
7815 conf
->reshape_checkpoint
= jiffies
;
7816 md_wakeup_thread(mddev
->sync_thread
);
7817 md_new_event(mddev
);
7821 /* This is called from the reshape thread and should make any
7822 * changes needed in 'conf'
7824 static void end_reshape(struct r5conf
*conf
)
7827 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
7828 struct md_rdev
*rdev
;
7830 spin_lock_irq(&conf
->device_lock
);
7831 conf
->previous_raid_disks
= conf
->raid_disks
;
7832 rdev_for_each(rdev
, conf
->mddev
)
7833 rdev
->data_offset
= rdev
->new_data_offset
;
7835 conf
->reshape_progress
= MaxSector
;
7836 conf
->mddev
->reshape_position
= MaxSector
;
7837 spin_unlock_irq(&conf
->device_lock
);
7838 wake_up(&conf
->wait_for_overlap
);
7840 /* read-ahead size must cover two whole stripes, which is
7841 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7843 if (conf
->mddev
->queue
) {
7844 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
7845 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
7847 if (conf
->mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
7848 conf
->mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
7853 /* This is called from the raid5d thread with mddev_lock held.
7854 * It makes config changes to the device.
7856 static void raid5_finish_reshape(struct mddev
*mddev
)
7858 struct r5conf
*conf
= mddev
->private;
7860 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7862 if (mddev
->delta_disks
> 0) {
7863 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7865 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7866 revalidate_disk(mddev
->gendisk
);
7870 spin_lock_irq(&conf
->device_lock
);
7871 mddev
->degraded
= raid5_calc_degraded(conf
);
7872 spin_unlock_irq(&conf
->device_lock
);
7873 for (d
= conf
->raid_disks
;
7874 d
< conf
->raid_disks
- mddev
->delta_disks
;
7876 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
7878 clear_bit(In_sync
, &rdev
->flags
);
7879 rdev
= conf
->disks
[d
].replacement
;
7881 clear_bit(In_sync
, &rdev
->flags
);
7884 mddev
->layout
= conf
->algorithm
;
7885 mddev
->chunk_sectors
= conf
->chunk_sectors
;
7886 mddev
->reshape_position
= MaxSector
;
7887 mddev
->delta_disks
= 0;
7888 mddev
->reshape_backwards
= 0;
7892 static void raid5_quiesce(struct mddev
*mddev
, int state
)
7894 struct r5conf
*conf
= mddev
->private;
7897 case 2: /* resume for a suspend */
7898 wake_up(&conf
->wait_for_overlap
);
7901 case 1: /* stop all writes */
7902 lock_all_device_hash_locks_irq(conf
);
7903 /* '2' tells resync/reshape to pause so that all
7904 * active stripes can drain
7906 r5c_flush_cache(conf
, INT_MAX
);
7908 wait_event_cmd(conf
->wait_for_quiescent
,
7909 atomic_read(&conf
->active_stripes
) == 0 &&
7910 atomic_read(&conf
->active_aligned_reads
) == 0,
7911 unlock_all_device_hash_locks_irq(conf
),
7912 lock_all_device_hash_locks_irq(conf
));
7914 unlock_all_device_hash_locks_irq(conf
);
7915 /* allow reshape to continue */
7916 wake_up(&conf
->wait_for_overlap
);
7919 case 0: /* re-enable writes */
7920 lock_all_device_hash_locks_irq(conf
);
7922 wake_up(&conf
->wait_for_quiescent
);
7923 wake_up(&conf
->wait_for_overlap
);
7924 unlock_all_device_hash_locks_irq(conf
);
7927 r5l_quiesce(conf
->log
, state
);
7930 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
7932 struct r0conf
*raid0_conf
= mddev
->private;
7935 /* for raid0 takeover only one zone is supported */
7936 if (raid0_conf
->nr_strip_zones
> 1) {
7937 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7939 return ERR_PTR(-EINVAL
);
7942 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
7943 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
7944 mddev
->dev_sectors
= sectors
;
7945 mddev
->new_level
= level
;
7946 mddev
->new_layout
= ALGORITHM_PARITY_N
;
7947 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7948 mddev
->raid_disks
+= 1;
7949 mddev
->delta_disks
= 1;
7950 /* make sure it will be not marked as dirty */
7951 mddev
->recovery_cp
= MaxSector
;
7953 return setup_conf(mddev
);
7956 static void *raid5_takeover_raid1(struct mddev
*mddev
)
7961 if (mddev
->raid_disks
!= 2 ||
7962 mddev
->degraded
> 1)
7963 return ERR_PTR(-EINVAL
);
7965 /* Should check if there are write-behind devices? */
7967 chunksect
= 64*2; /* 64K by default */
7969 /* The array must be an exact multiple of chunksize */
7970 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
7973 if ((chunksect
<<9) < STRIPE_SIZE
)
7974 /* array size does not allow a suitable chunk size */
7975 return ERR_PTR(-EINVAL
);
7977 mddev
->new_level
= 5;
7978 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7979 mddev
->new_chunk_sectors
= chunksect
;
7981 ret
= setup_conf(mddev
);
7983 mddev_clear_unsupported_flags(mddev
,
7984 UNSUPPORTED_MDDEV_FLAGS
);
7988 static void *raid5_takeover_raid6(struct mddev
*mddev
)
7992 switch (mddev
->layout
) {
7993 case ALGORITHM_LEFT_ASYMMETRIC_6
:
7994 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
7996 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
7997 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
7999 case ALGORITHM_LEFT_SYMMETRIC_6
:
8000 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
8002 case ALGORITHM_RIGHT_SYMMETRIC_6
:
8003 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
8005 case ALGORITHM_PARITY_0_6
:
8006 new_layout
= ALGORITHM_PARITY_0
;
8008 case ALGORITHM_PARITY_N
:
8009 new_layout
= ALGORITHM_PARITY_N
;
8012 return ERR_PTR(-EINVAL
);
8014 mddev
->new_level
= 5;
8015 mddev
->new_layout
= new_layout
;
8016 mddev
->delta_disks
= -1;
8017 mddev
->raid_disks
-= 1;
8018 return setup_conf(mddev
);
8021 static int raid5_check_reshape(struct mddev
*mddev
)
8023 /* For a 2-drive array, the layout and chunk size can be changed
8024 * immediately as not restriping is needed.
8025 * For larger arrays we record the new value - after validation
8026 * to be used by a reshape pass.
8028 struct r5conf
*conf
= mddev
->private;
8029 int new_chunk
= mddev
->new_chunk_sectors
;
8031 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
8033 if (new_chunk
> 0) {
8034 if (!is_power_of_2(new_chunk
))
8036 if (new_chunk
< (PAGE_SIZE
>>9))
8038 if (mddev
->array_sectors
& (new_chunk
-1))
8039 /* not factor of array size */
8043 /* They look valid */
8045 if (mddev
->raid_disks
== 2) {
8046 /* can make the change immediately */
8047 if (mddev
->new_layout
>= 0) {
8048 conf
->algorithm
= mddev
->new_layout
;
8049 mddev
->layout
= mddev
->new_layout
;
8051 if (new_chunk
> 0) {
8052 conf
->chunk_sectors
= new_chunk
;
8053 mddev
->chunk_sectors
= new_chunk
;
8055 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
8056 md_wakeup_thread(mddev
->thread
);
8058 return check_reshape(mddev
);
8061 static int raid6_check_reshape(struct mddev
*mddev
)
8063 int new_chunk
= mddev
->new_chunk_sectors
;
8065 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
8067 if (new_chunk
> 0) {
8068 if (!is_power_of_2(new_chunk
))
8070 if (new_chunk
< (PAGE_SIZE
>> 9))
8072 if (mddev
->array_sectors
& (new_chunk
-1))
8073 /* not factor of array size */
8077 /* They look valid */
8078 return check_reshape(mddev
);
8081 static void *raid5_takeover(struct mddev
*mddev
)
8083 /* raid5 can take over:
8084 * raid0 - if there is only one strip zone - make it a raid4 layout
8085 * raid1 - if there are two drives. We need to know the chunk size
8086 * raid4 - trivial - just use a raid4 layout.
8087 * raid6 - Providing it is a *_6 layout
8089 if (mddev
->level
== 0)
8090 return raid45_takeover_raid0(mddev
, 5);
8091 if (mddev
->level
== 1)
8092 return raid5_takeover_raid1(mddev
);
8093 if (mddev
->level
== 4) {
8094 mddev
->new_layout
= ALGORITHM_PARITY_N
;
8095 mddev
->new_level
= 5;
8096 return setup_conf(mddev
);
8098 if (mddev
->level
== 6)
8099 return raid5_takeover_raid6(mddev
);
8101 return ERR_PTR(-EINVAL
);
8104 static void *raid4_takeover(struct mddev
*mddev
)
8106 /* raid4 can take over:
8107 * raid0 - if there is only one strip zone
8108 * raid5 - if layout is right
8110 if (mddev
->level
== 0)
8111 return raid45_takeover_raid0(mddev
, 4);
8112 if (mddev
->level
== 5 &&
8113 mddev
->layout
== ALGORITHM_PARITY_N
) {
8114 mddev
->new_layout
= 0;
8115 mddev
->new_level
= 4;
8116 return setup_conf(mddev
);
8118 return ERR_PTR(-EINVAL
);
8121 static struct md_personality raid5_personality
;
8123 static void *raid6_takeover(struct mddev
*mddev
)
8125 /* Currently can only take over a raid5. We map the
8126 * personality to an equivalent raid6 personality
8127 * with the Q block at the end.
8131 if (mddev
->pers
!= &raid5_personality
)
8132 return ERR_PTR(-EINVAL
);
8133 if (mddev
->degraded
> 1)
8134 return ERR_PTR(-EINVAL
);
8135 if (mddev
->raid_disks
> 253)
8136 return ERR_PTR(-EINVAL
);
8137 if (mddev
->raid_disks
< 3)
8138 return ERR_PTR(-EINVAL
);
8140 switch (mddev
->layout
) {
8141 case ALGORITHM_LEFT_ASYMMETRIC
:
8142 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
8144 case ALGORITHM_RIGHT_ASYMMETRIC
:
8145 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
8147 case ALGORITHM_LEFT_SYMMETRIC
:
8148 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
8150 case ALGORITHM_RIGHT_SYMMETRIC
:
8151 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
8153 case ALGORITHM_PARITY_0
:
8154 new_layout
= ALGORITHM_PARITY_0_6
;
8156 case ALGORITHM_PARITY_N
:
8157 new_layout
= ALGORITHM_PARITY_N
;
8160 return ERR_PTR(-EINVAL
);
8162 mddev
->new_level
= 6;
8163 mddev
->new_layout
= new_layout
;
8164 mddev
->delta_disks
= 1;
8165 mddev
->raid_disks
+= 1;
8166 return setup_conf(mddev
);
8169 static struct md_personality raid6_personality
=
8173 .owner
= THIS_MODULE
,
8174 .make_request
= raid5_make_request
,
8177 .status
= raid5_status
,
8178 .error_handler
= raid5_error
,
8179 .hot_add_disk
= raid5_add_disk
,
8180 .hot_remove_disk
= raid5_remove_disk
,
8181 .spare_active
= raid5_spare_active
,
8182 .sync_request
= raid5_sync_request
,
8183 .resize
= raid5_resize
,
8185 .check_reshape
= raid6_check_reshape
,
8186 .start_reshape
= raid5_start_reshape
,
8187 .finish_reshape
= raid5_finish_reshape
,
8188 .quiesce
= raid5_quiesce
,
8189 .takeover
= raid6_takeover
,
8190 .congested
= raid5_congested
,
8192 static struct md_personality raid5_personality
=
8196 .owner
= THIS_MODULE
,
8197 .make_request
= raid5_make_request
,
8200 .status
= raid5_status
,
8201 .error_handler
= raid5_error
,
8202 .hot_add_disk
= raid5_add_disk
,
8203 .hot_remove_disk
= raid5_remove_disk
,
8204 .spare_active
= raid5_spare_active
,
8205 .sync_request
= raid5_sync_request
,
8206 .resize
= raid5_resize
,
8208 .check_reshape
= raid5_check_reshape
,
8209 .start_reshape
= raid5_start_reshape
,
8210 .finish_reshape
= raid5_finish_reshape
,
8211 .quiesce
= raid5_quiesce
,
8212 .takeover
= raid5_takeover
,
8213 .congested
= raid5_congested
,
8216 static struct md_personality raid4_personality
=
8220 .owner
= THIS_MODULE
,
8221 .make_request
= raid5_make_request
,
8224 .status
= raid5_status
,
8225 .error_handler
= raid5_error
,
8226 .hot_add_disk
= raid5_add_disk
,
8227 .hot_remove_disk
= raid5_remove_disk
,
8228 .spare_active
= raid5_spare_active
,
8229 .sync_request
= raid5_sync_request
,
8230 .resize
= raid5_resize
,
8232 .check_reshape
= raid5_check_reshape
,
8233 .start_reshape
= raid5_start_reshape
,
8234 .finish_reshape
= raid5_finish_reshape
,
8235 .quiesce
= raid5_quiesce
,
8236 .takeover
= raid4_takeover
,
8237 .congested
= raid5_congested
,
8240 static int __init
raid5_init(void)
8244 raid5_wq
= alloc_workqueue("raid5wq",
8245 WQ_UNBOUND
|WQ_MEM_RECLAIM
|WQ_CPU_INTENSIVE
|WQ_SYSFS
, 0);
8249 ret
= cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE
,
8251 raid456_cpu_up_prepare
,
8254 destroy_workqueue(raid5_wq
);
8257 register_md_personality(&raid6_personality
);
8258 register_md_personality(&raid5_personality
);
8259 register_md_personality(&raid4_personality
);
8263 static void raid5_exit(void)
8265 unregister_md_personality(&raid6_personality
);
8266 unregister_md_personality(&raid5_personality
);
8267 unregister_md_personality(&raid4_personality
);
8268 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE
);
8269 destroy_workqueue(raid5_wq
);
8272 module_init(raid5_init
);
8273 module_exit(raid5_exit
);
8274 MODULE_LICENSE("GPL");
8275 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
8276 MODULE_ALIAS("md-personality-4"); /* RAID5 */
8277 MODULE_ALIAS("md-raid5");
8278 MODULE_ALIAS("md-raid4");
8279 MODULE_ALIAS("md-level-5");
8280 MODULE_ALIAS("md-level-4");
8281 MODULE_ALIAS("md-personality-8"); /* RAID6 */
8282 MODULE_ALIAS("md-raid6");
8283 MODULE_ALIAS("md-level-6");
8285 /* This used to be two separate modules, they were: */
8286 MODULE_ALIAS("raid5");
8287 MODULE_ALIAS("raid6");