2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is seq_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
56 #include <linux/nodemask.h>
57 #include <linux/flex_array.h>
58 #include <trace/events/block.h>
65 #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
67 #define cpu_to_group(cpu) cpu_to_node(cpu)
68 #define ANY_GROUP NUMA_NO_NODE
70 static bool devices_handle_discard_safely
= false;
71 module_param(devices_handle_discard_safely
, bool, 0644);
72 MODULE_PARM_DESC(devices_handle_discard_safely
,
73 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
74 static struct workqueue_struct
*raid5_wq
;
76 static inline struct hlist_head
*stripe_hash(struct r5conf
*conf
, sector_t sect
)
78 int hash
= (sect
>> STRIPE_SHIFT
) & HASH_MASK
;
79 return &conf
->stripe_hashtbl
[hash
];
82 static inline int stripe_hash_locks_hash(sector_t sect
)
84 return (sect
>> STRIPE_SHIFT
) & STRIPE_HASH_LOCKS_MASK
;
87 static inline void lock_device_hash_lock(struct r5conf
*conf
, int hash
)
89 spin_lock_irq(conf
->hash_locks
+ hash
);
90 spin_lock(&conf
->device_lock
);
93 static inline void unlock_device_hash_lock(struct r5conf
*conf
, int hash
)
95 spin_unlock(&conf
->device_lock
);
96 spin_unlock_irq(conf
->hash_locks
+ hash
);
99 static inline void lock_all_device_hash_locks_irq(struct r5conf
*conf
)
103 spin_lock(conf
->hash_locks
);
104 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
105 spin_lock_nest_lock(conf
->hash_locks
+ i
, conf
->hash_locks
);
106 spin_lock(&conf
->device_lock
);
109 static inline void unlock_all_device_hash_locks_irq(struct r5conf
*conf
)
112 spin_unlock(&conf
->device_lock
);
113 for (i
= NR_STRIPE_HASH_LOCKS
; i
; i
--)
114 spin_unlock(conf
->hash_locks
+ i
- 1);
118 /* Find first data disk in a raid6 stripe */
119 static inline int raid6_d0(struct stripe_head
*sh
)
122 /* ddf always start from first device */
124 /* md starts just after Q block */
125 if (sh
->qd_idx
== sh
->disks
- 1)
128 return sh
->qd_idx
+ 1;
130 static inline int raid6_next_disk(int disk
, int raid_disks
)
133 return (disk
< raid_disks
) ? disk
: 0;
136 /* When walking through the disks in a raid5, starting at raid6_d0,
137 * We need to map each disk to a 'slot', where the data disks are slot
138 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
139 * is raid_disks-1. This help does that mapping.
141 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
142 int *count
, int syndrome_disks
)
148 if (idx
== sh
->pd_idx
)
149 return syndrome_disks
;
150 if (idx
== sh
->qd_idx
)
151 return syndrome_disks
+ 1;
157 static void return_io(struct bio_list
*return_bi
)
160 while ((bi
= bio_list_pop(return_bi
)) != NULL
) {
161 bi
->bi_iter
.bi_size
= 0;
162 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
168 static void print_raid5_conf (struct r5conf
*conf
);
170 static int stripe_operations_active(struct stripe_head
*sh
)
172 return sh
->check_state
|| sh
->reconstruct_state
||
173 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
174 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
177 static void raid5_wakeup_stripe_thread(struct stripe_head
*sh
)
179 struct r5conf
*conf
= sh
->raid_conf
;
180 struct r5worker_group
*group
;
182 int i
, cpu
= sh
->cpu
;
184 if (!cpu_online(cpu
)) {
185 cpu
= cpumask_any(cpu_online_mask
);
189 if (list_empty(&sh
->lru
)) {
190 struct r5worker_group
*group
;
191 group
= conf
->worker_groups
+ cpu_to_group(cpu
);
192 list_add_tail(&sh
->lru
, &group
->handle_list
);
193 group
->stripes_cnt
++;
197 if (conf
->worker_cnt_per_group
== 0) {
198 md_wakeup_thread(conf
->mddev
->thread
);
202 group
= conf
->worker_groups
+ cpu_to_group(sh
->cpu
);
204 group
->workers
[0].working
= true;
205 /* at least one worker should run to avoid race */
206 queue_work_on(sh
->cpu
, raid5_wq
, &group
->workers
[0].work
);
208 thread_cnt
= group
->stripes_cnt
/ MAX_STRIPE_BATCH
- 1;
209 /* wakeup more workers */
210 for (i
= 1; i
< conf
->worker_cnt_per_group
&& thread_cnt
> 0; i
++) {
211 if (group
->workers
[i
].working
== false) {
212 group
->workers
[i
].working
= true;
213 queue_work_on(sh
->cpu
, raid5_wq
,
214 &group
->workers
[i
].work
);
220 static void do_release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
221 struct list_head
*temp_inactive_list
)
224 int injournal
= 0; /* number of date pages with R5_InJournal */
226 BUG_ON(!list_empty(&sh
->lru
));
227 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
229 if (r5c_is_writeback(conf
->log
))
230 for (i
= sh
->disks
; i
--; )
231 if (test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
234 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
235 * data in journal, so they are not released to cached lists
237 if (conf
->quiesce
&& r5c_is_writeback(conf
->log
) &&
238 !test_bit(STRIPE_HANDLE
, &sh
->state
) && injournal
!= 0) {
239 if (test_bit(STRIPE_R5C_CACHING
, &sh
->state
))
240 r5c_make_stripe_write_out(sh
);
241 set_bit(STRIPE_HANDLE
, &sh
->state
);
244 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
245 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
246 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
247 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
248 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
249 sh
->bm_seq
- conf
->seq_write
> 0)
250 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
252 clear_bit(STRIPE_DELAYED
, &sh
->state
);
253 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
254 if (conf
->worker_cnt_per_group
== 0) {
255 list_add_tail(&sh
->lru
, &conf
->handle_list
);
257 raid5_wakeup_stripe_thread(sh
);
261 md_wakeup_thread(conf
->mddev
->thread
);
263 BUG_ON(stripe_operations_active(sh
));
264 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
265 if (atomic_dec_return(&conf
->preread_active_stripes
)
267 md_wakeup_thread(conf
->mddev
->thread
);
268 atomic_dec(&conf
->active_stripes
);
269 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
270 if (!r5c_is_writeback(conf
->log
))
271 list_add_tail(&sh
->lru
, temp_inactive_list
);
273 WARN_ON(test_bit(R5_InJournal
, &sh
->dev
[sh
->pd_idx
].flags
));
275 list_add_tail(&sh
->lru
, temp_inactive_list
);
276 else if (injournal
== conf
->raid_disks
- conf
->max_degraded
) {
278 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE
, &sh
->state
))
279 atomic_inc(&conf
->r5c_cached_full_stripes
);
280 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE
, &sh
->state
))
281 atomic_dec(&conf
->r5c_cached_partial_stripes
);
282 list_add_tail(&sh
->lru
, &conf
->r5c_full_stripe_list
);
283 r5c_check_cached_full_stripe(conf
);
286 if (!test_and_set_bit(STRIPE_R5C_PARTIAL_STRIPE
,
288 atomic_inc(&conf
->r5c_cached_partial_stripes
);
289 list_add_tail(&sh
->lru
, &conf
->r5c_partial_stripe_list
);
296 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
297 struct list_head
*temp_inactive_list
)
299 if (atomic_dec_and_test(&sh
->count
))
300 do_release_stripe(conf
, sh
, temp_inactive_list
);
304 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
306 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
307 * given time. Adding stripes only takes device lock, while deleting stripes
308 * only takes hash lock.
310 static void release_inactive_stripe_list(struct r5conf
*conf
,
311 struct list_head
*temp_inactive_list
,
315 bool do_wakeup
= false;
318 if (hash
== NR_STRIPE_HASH_LOCKS
) {
319 size
= NR_STRIPE_HASH_LOCKS
;
320 hash
= NR_STRIPE_HASH_LOCKS
- 1;
324 struct list_head
*list
= &temp_inactive_list
[size
- 1];
327 * We don't hold any lock here yet, raid5_get_active_stripe() might
328 * remove stripes from the list
330 if (!list_empty_careful(list
)) {
331 spin_lock_irqsave(conf
->hash_locks
+ hash
, flags
);
332 if (list_empty(conf
->inactive_list
+ hash
) &&
334 atomic_dec(&conf
->empty_inactive_list_nr
);
335 list_splice_tail_init(list
, conf
->inactive_list
+ hash
);
337 spin_unlock_irqrestore(conf
->hash_locks
+ hash
, flags
);
344 wake_up(&conf
->wait_for_stripe
);
345 if (atomic_read(&conf
->active_stripes
) == 0)
346 wake_up(&conf
->wait_for_quiescent
);
347 if (conf
->retry_read_aligned
)
348 md_wakeup_thread(conf
->mddev
->thread
);
352 /* should hold conf->device_lock already */
353 static int release_stripe_list(struct r5conf
*conf
,
354 struct list_head
*temp_inactive_list
)
356 struct stripe_head
*sh
;
358 struct llist_node
*head
;
360 head
= llist_del_all(&conf
->released_stripes
);
361 head
= llist_reverse_order(head
);
365 sh
= llist_entry(head
, struct stripe_head
, release_list
);
366 head
= llist_next(head
);
367 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
369 clear_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
);
371 * Don't worry the bit is set here, because if the bit is set
372 * again, the count is always > 1. This is true for
373 * STRIPE_ON_UNPLUG_LIST bit too.
375 hash
= sh
->hash_lock_index
;
376 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
383 void raid5_release_stripe(struct stripe_head
*sh
)
385 struct r5conf
*conf
= sh
->raid_conf
;
387 struct list_head list
;
391 /* Avoid release_list until the last reference.
393 if (atomic_add_unless(&sh
->count
, -1, 1))
396 if (unlikely(!conf
->mddev
->thread
) ||
397 test_and_set_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
))
399 wakeup
= llist_add(&sh
->release_list
, &conf
->released_stripes
);
401 md_wakeup_thread(conf
->mddev
->thread
);
404 local_irq_save(flags
);
405 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
406 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
407 INIT_LIST_HEAD(&list
);
408 hash
= sh
->hash_lock_index
;
409 do_release_stripe(conf
, sh
, &list
);
410 spin_unlock(&conf
->device_lock
);
411 release_inactive_stripe_list(conf
, &list
, hash
);
413 local_irq_restore(flags
);
416 static inline void remove_hash(struct stripe_head
*sh
)
418 pr_debug("remove_hash(), stripe %llu\n",
419 (unsigned long long)sh
->sector
);
421 hlist_del_init(&sh
->hash
);
424 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
426 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
428 pr_debug("insert_hash(), stripe %llu\n",
429 (unsigned long long)sh
->sector
);
431 hlist_add_head(&sh
->hash
, hp
);
434 /* find an idle stripe, make sure it is unhashed, and return it. */
435 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
, int hash
)
437 struct stripe_head
*sh
= NULL
;
438 struct list_head
*first
;
440 if (list_empty(conf
->inactive_list
+ hash
))
442 first
= (conf
->inactive_list
+ hash
)->next
;
443 sh
= list_entry(first
, struct stripe_head
, lru
);
444 list_del_init(first
);
446 atomic_inc(&conf
->active_stripes
);
447 BUG_ON(hash
!= sh
->hash_lock_index
);
448 if (list_empty(conf
->inactive_list
+ hash
))
449 atomic_inc(&conf
->empty_inactive_list_nr
);
454 static void shrink_buffers(struct stripe_head
*sh
)
458 int num
= sh
->raid_conf
->pool_size
;
460 for (i
= 0; i
< num
; i
++) {
461 WARN_ON(sh
->dev
[i
].page
!= sh
->dev
[i
].orig_page
);
465 sh
->dev
[i
].page
= NULL
;
470 static int grow_buffers(struct stripe_head
*sh
, gfp_t gfp
)
473 int num
= sh
->raid_conf
->pool_size
;
475 for (i
= 0; i
< num
; i
++) {
478 if (!(page
= alloc_page(gfp
))) {
481 sh
->dev
[i
].page
= page
;
482 sh
->dev
[i
].orig_page
= page
;
487 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
488 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
489 struct stripe_head
*sh
);
491 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
493 struct r5conf
*conf
= sh
->raid_conf
;
496 BUG_ON(atomic_read(&sh
->count
) != 0);
497 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
498 BUG_ON(stripe_operations_active(sh
));
499 BUG_ON(sh
->batch_head
);
501 pr_debug("init_stripe called, stripe %llu\n",
502 (unsigned long long)sector
);
504 seq
= read_seqcount_begin(&conf
->gen_lock
);
505 sh
->generation
= conf
->generation
- previous
;
506 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
508 stripe_set_idx(sector
, conf
, previous
, sh
);
511 for (i
= sh
->disks
; i
--; ) {
512 struct r5dev
*dev
= &sh
->dev
[i
];
514 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
515 test_bit(R5_LOCKED
, &dev
->flags
)) {
516 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
517 (unsigned long long)sh
->sector
, i
, dev
->toread
,
518 dev
->read
, dev
->towrite
, dev
->written
,
519 test_bit(R5_LOCKED
, &dev
->flags
));
523 raid5_build_block(sh
, i
, previous
);
525 if (read_seqcount_retry(&conf
->gen_lock
, seq
))
527 sh
->overwrite_disks
= 0;
528 insert_hash(conf
, sh
);
529 sh
->cpu
= smp_processor_id();
530 set_bit(STRIPE_BATCH_READY
, &sh
->state
);
533 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
536 struct stripe_head
*sh
;
538 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
539 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
540 if (sh
->sector
== sector
&& sh
->generation
== generation
)
542 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
547 * Need to check if array has failed when deciding whether to:
549 * - remove non-faulty devices
552 * This determination is simple when no reshape is happening.
553 * However if there is a reshape, we need to carefully check
554 * both the before and after sections.
555 * This is because some failed devices may only affect one
556 * of the two sections, and some non-in_sync devices may
557 * be insync in the section most affected by failed devices.
559 int raid5_calc_degraded(struct r5conf
*conf
)
561 int degraded
, degraded2
;
566 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
567 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
568 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
569 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
570 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
572 else if (test_bit(In_sync
, &rdev
->flags
))
575 /* not in-sync or faulty.
576 * If the reshape increases the number of devices,
577 * this is being recovered by the reshape, so
578 * this 'previous' section is not in_sync.
579 * If the number of devices is being reduced however,
580 * the device can only be part of the array if
581 * we are reverting a reshape, so this section will
584 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
588 if (conf
->raid_disks
== conf
->previous_raid_disks
)
592 for (i
= 0; i
< conf
->raid_disks
; i
++) {
593 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
594 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
595 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
596 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
598 else if (test_bit(In_sync
, &rdev
->flags
))
601 /* not in-sync or faulty.
602 * If reshape increases the number of devices, this
603 * section has already been recovered, else it
604 * almost certainly hasn't.
606 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
610 if (degraded2
> degraded
)
615 static int has_failed(struct r5conf
*conf
)
619 if (conf
->mddev
->reshape_position
== MaxSector
)
620 return conf
->mddev
->degraded
> conf
->max_degraded
;
622 degraded
= raid5_calc_degraded(conf
);
623 if (degraded
> conf
->max_degraded
)
629 raid5_get_active_stripe(struct r5conf
*conf
, sector_t sector
,
630 int previous
, int noblock
, int noquiesce
)
632 struct stripe_head
*sh
;
633 int hash
= stripe_hash_locks_hash(sector
);
634 int inc_empty_inactive_list_flag
;
636 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
638 spin_lock_irq(conf
->hash_locks
+ hash
);
641 wait_event_lock_irq(conf
->wait_for_quiescent
,
642 conf
->quiesce
== 0 || noquiesce
,
643 *(conf
->hash_locks
+ hash
));
644 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
646 if (!test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
)) {
647 sh
= get_free_stripe(conf
, hash
);
648 if (!sh
&& !test_bit(R5_DID_ALLOC
,
650 set_bit(R5_ALLOC_MORE
,
653 if (noblock
&& sh
== NULL
)
656 r5c_check_stripe_cache_usage(conf
);
658 set_bit(R5_INACTIVE_BLOCKED
,
660 r5l_wake_reclaim(conf
->log
, 0);
662 conf
->wait_for_stripe
,
663 !list_empty(conf
->inactive_list
+ hash
) &&
664 (atomic_read(&conf
->active_stripes
)
665 < (conf
->max_nr_stripes
* 3 / 4)
666 || !test_bit(R5_INACTIVE_BLOCKED
,
667 &conf
->cache_state
)),
668 *(conf
->hash_locks
+ hash
));
669 clear_bit(R5_INACTIVE_BLOCKED
,
672 init_stripe(sh
, sector
, previous
);
673 atomic_inc(&sh
->count
);
675 } else if (!atomic_inc_not_zero(&sh
->count
)) {
676 spin_lock(&conf
->device_lock
);
677 if (!atomic_read(&sh
->count
)) {
678 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
679 atomic_inc(&conf
->active_stripes
);
680 BUG_ON(list_empty(&sh
->lru
) &&
681 !test_bit(STRIPE_EXPANDING
, &sh
->state
));
682 inc_empty_inactive_list_flag
= 0;
683 if (!list_empty(conf
->inactive_list
+ hash
))
684 inc_empty_inactive_list_flag
= 1;
685 list_del_init(&sh
->lru
);
686 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
687 atomic_inc(&conf
->empty_inactive_list_nr
);
689 sh
->group
->stripes_cnt
--;
693 atomic_inc(&sh
->count
);
694 spin_unlock(&conf
->device_lock
);
696 } while (sh
== NULL
);
698 spin_unlock_irq(conf
->hash_locks
+ hash
);
702 static bool is_full_stripe_write(struct stripe_head
*sh
)
704 BUG_ON(sh
->overwrite_disks
> (sh
->disks
- sh
->raid_conf
->max_degraded
));
705 return sh
->overwrite_disks
== (sh
->disks
- sh
->raid_conf
->max_degraded
);
708 static void lock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
712 spin_lock(&sh2
->stripe_lock
);
713 spin_lock_nested(&sh1
->stripe_lock
, 1);
715 spin_lock(&sh1
->stripe_lock
);
716 spin_lock_nested(&sh2
->stripe_lock
, 1);
720 static void unlock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
722 spin_unlock(&sh1
->stripe_lock
);
723 spin_unlock(&sh2
->stripe_lock
);
727 /* Only freshly new full stripe normal write stripe can be added to a batch list */
728 static bool stripe_can_batch(struct stripe_head
*sh
)
730 struct r5conf
*conf
= sh
->raid_conf
;
734 return test_bit(STRIPE_BATCH_READY
, &sh
->state
) &&
735 !test_bit(STRIPE_BITMAP_PENDING
, &sh
->state
) &&
736 is_full_stripe_write(sh
);
739 /* we only do back search */
740 static void stripe_add_to_batch_list(struct r5conf
*conf
, struct stripe_head
*sh
)
742 struct stripe_head
*head
;
743 sector_t head_sector
, tmp_sec
;
746 int inc_empty_inactive_list_flag
;
748 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
749 tmp_sec
= sh
->sector
;
750 if (!sector_div(tmp_sec
, conf
->chunk_sectors
))
752 head_sector
= sh
->sector
- STRIPE_SECTORS
;
754 hash
= stripe_hash_locks_hash(head_sector
);
755 spin_lock_irq(conf
->hash_locks
+ hash
);
756 head
= __find_stripe(conf
, head_sector
, conf
->generation
);
757 if (head
&& !atomic_inc_not_zero(&head
->count
)) {
758 spin_lock(&conf
->device_lock
);
759 if (!atomic_read(&head
->count
)) {
760 if (!test_bit(STRIPE_HANDLE
, &head
->state
))
761 atomic_inc(&conf
->active_stripes
);
762 BUG_ON(list_empty(&head
->lru
) &&
763 !test_bit(STRIPE_EXPANDING
, &head
->state
));
764 inc_empty_inactive_list_flag
= 0;
765 if (!list_empty(conf
->inactive_list
+ hash
))
766 inc_empty_inactive_list_flag
= 1;
767 list_del_init(&head
->lru
);
768 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
769 atomic_inc(&conf
->empty_inactive_list_nr
);
771 head
->group
->stripes_cnt
--;
775 atomic_inc(&head
->count
);
776 spin_unlock(&conf
->device_lock
);
778 spin_unlock_irq(conf
->hash_locks
+ hash
);
782 if (!stripe_can_batch(head
))
785 lock_two_stripes(head
, sh
);
786 /* clear_batch_ready clear the flag */
787 if (!stripe_can_batch(head
) || !stripe_can_batch(sh
))
794 while (dd_idx
== sh
->pd_idx
|| dd_idx
== sh
->qd_idx
)
796 if (head
->dev
[dd_idx
].towrite
->bi_opf
!= sh
->dev
[dd_idx
].towrite
->bi_opf
||
797 bio_op(head
->dev
[dd_idx
].towrite
) != bio_op(sh
->dev
[dd_idx
].towrite
))
800 if (head
->batch_head
) {
801 spin_lock(&head
->batch_head
->batch_lock
);
802 /* This batch list is already running */
803 if (!stripe_can_batch(head
)) {
804 spin_unlock(&head
->batch_head
->batch_lock
);
809 * at this point, head's BATCH_READY could be cleared, but we
810 * can still add the stripe to batch list
812 list_add(&sh
->batch_list
, &head
->batch_list
);
813 spin_unlock(&head
->batch_head
->batch_lock
);
815 sh
->batch_head
= head
->batch_head
;
817 head
->batch_head
= head
;
818 sh
->batch_head
= head
->batch_head
;
819 spin_lock(&head
->batch_lock
);
820 list_add_tail(&sh
->batch_list
, &head
->batch_list
);
821 spin_unlock(&head
->batch_lock
);
824 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
825 if (atomic_dec_return(&conf
->preread_active_stripes
)
827 md_wakeup_thread(conf
->mddev
->thread
);
829 if (test_and_clear_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
830 int seq
= sh
->bm_seq
;
831 if (test_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
) &&
832 sh
->batch_head
->bm_seq
> seq
)
833 seq
= sh
->batch_head
->bm_seq
;
834 set_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
);
835 sh
->batch_head
->bm_seq
= seq
;
838 atomic_inc(&sh
->count
);
840 unlock_two_stripes(head
, sh
);
842 raid5_release_stripe(head
);
845 /* Determine if 'data_offset' or 'new_data_offset' should be used
846 * in this stripe_head.
848 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
850 sector_t progress
= conf
->reshape_progress
;
851 /* Need a memory barrier to make sure we see the value
852 * of conf->generation, or ->data_offset that was set before
853 * reshape_progress was updated.
856 if (progress
== MaxSector
)
858 if (sh
->generation
== conf
->generation
- 1)
860 /* We are in a reshape, and this is a new-generation stripe,
861 * so use new_data_offset.
867 raid5_end_read_request(struct bio
*bi
);
869 raid5_end_write_request(struct bio
*bi
);
871 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
873 struct r5conf
*conf
= sh
->raid_conf
;
874 int i
, disks
= sh
->disks
;
875 struct stripe_head
*head_sh
= sh
;
879 if (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
)) {
880 /* writing out phase */
881 if (s
->waiting_extra_page
)
883 if (r5l_write_stripe(conf
->log
, sh
) == 0)
885 } else { /* caching phase */
886 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
)) {
887 r5c_cache_data(conf
->log
, sh
, s
);
892 for (i
= disks
; i
--; ) {
893 int op
, op_flags
= 0;
894 int replace_only
= 0;
895 struct bio
*bi
, *rbi
;
896 struct md_rdev
*rdev
, *rrdev
= NULL
;
899 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
901 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
903 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
905 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
907 else if (test_and_clear_bit(R5_WantReplace
,
908 &sh
->dev
[i
].flags
)) {
913 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
914 op_flags
|= REQ_SYNC
;
917 bi
= &sh
->dev
[i
].req
;
918 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
921 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
922 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
923 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
928 if (op_is_write(op
)) {
932 /* We raced and saw duplicates */
935 if (test_bit(R5_ReadRepl
, &head_sh
->dev
[i
].flags
) && rrdev
)
940 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
943 atomic_inc(&rdev
->nr_pending
);
944 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
947 atomic_inc(&rrdev
->nr_pending
);
950 /* We have already checked bad blocks for reads. Now
951 * need to check for writes. We never accept write errors
952 * on the replacement, so we don't to check rrdev.
954 while (op_is_write(op
) && rdev
&&
955 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
958 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
959 &first_bad
, &bad_sectors
);
964 set_bit(BlockedBadBlocks
, &rdev
->flags
);
965 if (!conf
->mddev
->external
&&
966 conf
->mddev
->sb_flags
) {
967 /* It is very unlikely, but we might
968 * still need to write out the
969 * bad block log - better give it
971 md_check_recovery(conf
->mddev
);
974 * Because md_wait_for_blocked_rdev
975 * will dec nr_pending, we must
976 * increment it first.
978 atomic_inc(&rdev
->nr_pending
);
979 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
981 /* Acknowledged bad block - skip the write */
982 rdev_dec_pending(rdev
, conf
->mddev
);
988 if (s
->syncing
|| s
->expanding
|| s
->expanded
990 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
992 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
994 bi
->bi_bdev
= rdev
->bdev
;
995 bio_set_op_attrs(bi
, op
, op_flags
);
996 bi
->bi_end_io
= op_is_write(op
)
997 ? raid5_end_write_request
998 : raid5_end_read_request
;
1001 pr_debug("%s: for %llu schedule op %d on disc %d\n",
1002 __func__
, (unsigned long long)sh
->sector
,
1004 atomic_inc(&sh
->count
);
1006 atomic_inc(&head_sh
->count
);
1007 if (use_new_offset(conf
, sh
))
1008 bi
->bi_iter
.bi_sector
= (sh
->sector
1009 + rdev
->new_data_offset
);
1011 bi
->bi_iter
.bi_sector
= (sh
->sector
1012 + rdev
->data_offset
);
1013 if (test_bit(R5_ReadNoMerge
, &head_sh
->dev
[i
].flags
))
1014 bi
->bi_opf
|= REQ_NOMERGE
;
1016 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1017 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1019 if (!op_is_write(op
) &&
1020 test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
1022 * issuing read for a page in journal, this
1023 * must be preparing for prexor in rmw; read
1024 * the data into orig_page
1026 sh
->dev
[i
].vec
.bv_page
= sh
->dev
[i
].orig_page
;
1028 sh
->dev
[i
].vec
.bv_page
= sh
->dev
[i
].page
;
1030 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1031 bi
->bi_io_vec
[0].bv_offset
= 0;
1032 bi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1034 * If this is discard request, set bi_vcnt 0. We don't
1035 * want to confuse SCSI because SCSI will replace payload
1037 if (op
== REQ_OP_DISCARD
)
1040 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
1042 if (conf
->mddev
->gendisk
)
1043 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
1044 bi
, disk_devt(conf
->mddev
->gendisk
),
1046 generic_make_request(bi
);
1049 if (s
->syncing
|| s
->expanding
|| s
->expanded
1051 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
1053 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
1055 rbi
->bi_bdev
= rrdev
->bdev
;
1056 bio_set_op_attrs(rbi
, op
, op_flags
);
1057 BUG_ON(!op_is_write(op
));
1058 rbi
->bi_end_io
= raid5_end_write_request
;
1059 rbi
->bi_private
= sh
;
1061 pr_debug("%s: for %llu schedule op %d on "
1062 "replacement disc %d\n",
1063 __func__
, (unsigned long long)sh
->sector
,
1065 atomic_inc(&sh
->count
);
1067 atomic_inc(&head_sh
->count
);
1068 if (use_new_offset(conf
, sh
))
1069 rbi
->bi_iter
.bi_sector
= (sh
->sector
1070 + rrdev
->new_data_offset
);
1072 rbi
->bi_iter
.bi_sector
= (sh
->sector
1073 + rrdev
->data_offset
);
1074 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1075 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1076 sh
->dev
[i
].rvec
.bv_page
= sh
->dev
[i
].page
;
1078 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1079 rbi
->bi_io_vec
[0].bv_offset
= 0;
1080 rbi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1082 * If this is discard request, set bi_vcnt 0. We don't
1083 * want to confuse SCSI because SCSI will replace payload
1085 if (op
== REQ_OP_DISCARD
)
1087 if (conf
->mddev
->gendisk
)
1088 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
1089 rbi
, disk_devt(conf
->mddev
->gendisk
),
1091 generic_make_request(rbi
);
1093 if (!rdev
&& !rrdev
) {
1094 if (op_is_write(op
))
1095 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1096 pr_debug("skip op %d on disc %d for sector %llu\n",
1097 bi
->bi_opf
, i
, (unsigned long long)sh
->sector
);
1098 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1099 set_bit(STRIPE_HANDLE
, &sh
->state
);
1102 if (!head_sh
->batch_head
)
1104 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1111 static struct dma_async_tx_descriptor
*
1112 async_copy_data(int frombio
, struct bio
*bio
, struct page
**page
,
1113 sector_t sector
, struct dma_async_tx_descriptor
*tx
,
1114 struct stripe_head
*sh
, int no_skipcopy
)
1117 struct bvec_iter iter
;
1118 struct page
*bio_page
;
1120 struct async_submit_ctl submit
;
1121 enum async_tx_flags flags
= 0;
1123 if (bio
->bi_iter
.bi_sector
>= sector
)
1124 page_offset
= (signed)(bio
->bi_iter
.bi_sector
- sector
) * 512;
1126 page_offset
= (signed)(sector
- bio
->bi_iter
.bi_sector
) * -512;
1129 flags
|= ASYNC_TX_FENCE
;
1130 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
1132 bio_for_each_segment(bvl
, bio
, iter
) {
1133 int len
= bvl
.bv_len
;
1137 if (page_offset
< 0) {
1138 b_offset
= -page_offset
;
1139 page_offset
+= b_offset
;
1143 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
1144 clen
= STRIPE_SIZE
- page_offset
;
1149 b_offset
+= bvl
.bv_offset
;
1150 bio_page
= bvl
.bv_page
;
1152 if (sh
->raid_conf
->skip_copy
&&
1153 b_offset
== 0 && page_offset
== 0 &&
1154 clen
== STRIPE_SIZE
&&
1158 tx
= async_memcpy(*page
, bio_page
, page_offset
,
1159 b_offset
, clen
, &submit
);
1161 tx
= async_memcpy(bio_page
, *page
, b_offset
,
1162 page_offset
, clen
, &submit
);
1164 /* chain the operations */
1165 submit
.depend_tx
= tx
;
1167 if (clen
< len
) /* hit end of page */
1175 static void ops_complete_biofill(void *stripe_head_ref
)
1177 struct stripe_head
*sh
= stripe_head_ref
;
1178 struct bio_list return_bi
= BIO_EMPTY_LIST
;
1181 pr_debug("%s: stripe %llu\n", __func__
,
1182 (unsigned long long)sh
->sector
);
1184 /* clear completed biofills */
1185 for (i
= sh
->disks
; i
--; ) {
1186 struct r5dev
*dev
= &sh
->dev
[i
];
1188 /* acknowledge completion of a biofill operation */
1189 /* and check if we need to reply to a read request,
1190 * new R5_Wantfill requests are held off until
1191 * !STRIPE_BIOFILL_RUN
1193 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
1194 struct bio
*rbi
, *rbi2
;
1199 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1200 dev
->sector
+ STRIPE_SECTORS
) {
1201 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1202 if (!raid5_dec_bi_active_stripes(rbi
))
1203 bio_list_add(&return_bi
, rbi
);
1208 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
1210 return_io(&return_bi
);
1212 set_bit(STRIPE_HANDLE
, &sh
->state
);
1213 raid5_release_stripe(sh
);
1216 static void ops_run_biofill(struct stripe_head
*sh
)
1218 struct dma_async_tx_descriptor
*tx
= NULL
;
1219 struct async_submit_ctl submit
;
1222 BUG_ON(sh
->batch_head
);
1223 pr_debug("%s: stripe %llu\n", __func__
,
1224 (unsigned long long)sh
->sector
);
1226 for (i
= sh
->disks
; i
--; ) {
1227 struct r5dev
*dev
= &sh
->dev
[i
];
1228 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
1230 spin_lock_irq(&sh
->stripe_lock
);
1231 dev
->read
= rbi
= dev
->toread
;
1233 spin_unlock_irq(&sh
->stripe_lock
);
1234 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1235 dev
->sector
+ STRIPE_SECTORS
) {
1236 tx
= async_copy_data(0, rbi
, &dev
->page
,
1237 dev
->sector
, tx
, sh
, 0);
1238 rbi
= r5_next_bio(rbi
, dev
->sector
);
1243 atomic_inc(&sh
->count
);
1244 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
1245 async_trigger_callback(&submit
);
1248 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
1255 tgt
= &sh
->dev
[target
];
1256 set_bit(R5_UPTODATE
, &tgt
->flags
);
1257 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1258 clear_bit(R5_Wantcompute
, &tgt
->flags
);
1261 static void ops_complete_compute(void *stripe_head_ref
)
1263 struct stripe_head
*sh
= stripe_head_ref
;
1265 pr_debug("%s: stripe %llu\n", __func__
,
1266 (unsigned long long)sh
->sector
);
1268 /* mark the computed target(s) as uptodate */
1269 mark_target_uptodate(sh
, sh
->ops
.target
);
1270 mark_target_uptodate(sh
, sh
->ops
.target2
);
1272 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
1273 if (sh
->check_state
== check_state_compute_run
)
1274 sh
->check_state
= check_state_compute_result
;
1275 set_bit(STRIPE_HANDLE
, &sh
->state
);
1276 raid5_release_stripe(sh
);
1279 /* return a pointer to the address conversion region of the scribble buffer */
1280 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
1281 struct raid5_percpu
*percpu
, int i
)
1285 addr
= flex_array_get(percpu
->scribble
, i
);
1286 return addr
+ sizeof(struct page
*) * (sh
->disks
+ 2);
1289 /* return a pointer to the address conversion region of the scribble buffer */
1290 static struct page
**to_addr_page(struct raid5_percpu
*percpu
, int i
)
1294 addr
= flex_array_get(percpu
->scribble
, i
);
1298 static struct dma_async_tx_descriptor
*
1299 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1301 int disks
= sh
->disks
;
1302 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1303 int target
= sh
->ops
.target
;
1304 struct r5dev
*tgt
= &sh
->dev
[target
];
1305 struct page
*xor_dest
= tgt
->page
;
1307 struct dma_async_tx_descriptor
*tx
;
1308 struct async_submit_ctl submit
;
1311 BUG_ON(sh
->batch_head
);
1313 pr_debug("%s: stripe %llu block: %d\n",
1314 __func__
, (unsigned long long)sh
->sector
, target
);
1315 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1317 for (i
= disks
; i
--; )
1319 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1321 atomic_inc(&sh
->count
);
1323 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
1324 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
, 0));
1325 if (unlikely(count
== 1))
1326 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1328 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1333 /* set_syndrome_sources - populate source buffers for gen_syndrome
1334 * @srcs - (struct page *) array of size sh->disks
1335 * @sh - stripe_head to parse
1337 * Populates srcs in proper layout order for the stripe and returns the
1338 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1339 * destination buffer is recorded in srcs[count] and the Q destination
1340 * is recorded in srcs[count+1]].
1342 static int set_syndrome_sources(struct page
**srcs
,
1343 struct stripe_head
*sh
,
1346 int disks
= sh
->disks
;
1347 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1348 int d0_idx
= raid6_d0(sh
);
1352 for (i
= 0; i
< disks
; i
++)
1358 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1359 struct r5dev
*dev
= &sh
->dev
[i
];
1361 if (i
== sh
->qd_idx
|| i
== sh
->pd_idx
||
1362 (srctype
== SYNDROME_SRC_ALL
) ||
1363 (srctype
== SYNDROME_SRC_WANT_DRAIN
&&
1364 (test_bit(R5_Wantdrain
, &dev
->flags
) ||
1365 test_bit(R5_InJournal
, &dev
->flags
))) ||
1366 (srctype
== SYNDROME_SRC_WRITTEN
&&
1368 test_bit(R5_InJournal
, &dev
->flags
)))) {
1369 if (test_bit(R5_InJournal
, &dev
->flags
))
1370 srcs
[slot
] = sh
->dev
[i
].orig_page
;
1372 srcs
[slot
] = sh
->dev
[i
].page
;
1374 i
= raid6_next_disk(i
, disks
);
1375 } while (i
!= d0_idx
);
1377 return syndrome_disks
;
1380 static struct dma_async_tx_descriptor
*
1381 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1383 int disks
= sh
->disks
;
1384 struct page
**blocks
= to_addr_page(percpu
, 0);
1386 int qd_idx
= sh
->qd_idx
;
1387 struct dma_async_tx_descriptor
*tx
;
1388 struct async_submit_ctl submit
;
1394 BUG_ON(sh
->batch_head
);
1395 if (sh
->ops
.target
< 0)
1396 target
= sh
->ops
.target2
;
1397 else if (sh
->ops
.target2
< 0)
1398 target
= sh
->ops
.target
;
1400 /* we should only have one valid target */
1403 pr_debug("%s: stripe %llu block: %d\n",
1404 __func__
, (unsigned long long)sh
->sector
, target
);
1406 tgt
= &sh
->dev
[target
];
1407 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1410 atomic_inc(&sh
->count
);
1412 if (target
== qd_idx
) {
1413 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1414 blocks
[count
] = NULL
; /* regenerating p is not necessary */
1415 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
1416 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1417 ops_complete_compute
, sh
,
1418 to_addr_conv(sh
, percpu
, 0));
1419 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1421 /* Compute any data- or p-drive using XOR */
1423 for (i
= disks
; i
-- ; ) {
1424 if (i
== target
|| i
== qd_idx
)
1426 blocks
[count
++] = sh
->dev
[i
].page
;
1429 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1430 NULL
, ops_complete_compute
, sh
,
1431 to_addr_conv(sh
, percpu
, 0));
1432 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1438 static struct dma_async_tx_descriptor
*
1439 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1441 int i
, count
, disks
= sh
->disks
;
1442 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1443 int d0_idx
= raid6_d0(sh
);
1444 int faila
= -1, failb
= -1;
1445 int target
= sh
->ops
.target
;
1446 int target2
= sh
->ops
.target2
;
1447 struct r5dev
*tgt
= &sh
->dev
[target
];
1448 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1449 struct dma_async_tx_descriptor
*tx
;
1450 struct page
**blocks
= to_addr_page(percpu
, 0);
1451 struct async_submit_ctl submit
;
1453 BUG_ON(sh
->batch_head
);
1454 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1455 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1456 BUG_ON(target
< 0 || target2
< 0);
1457 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1458 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1460 /* we need to open-code set_syndrome_sources to handle the
1461 * slot number conversion for 'faila' and 'failb'
1463 for (i
= 0; i
< disks
; i
++)
1468 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1470 blocks
[slot
] = sh
->dev
[i
].page
;
1476 i
= raid6_next_disk(i
, disks
);
1477 } while (i
!= d0_idx
);
1479 BUG_ON(faila
== failb
);
1482 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1483 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1485 atomic_inc(&sh
->count
);
1487 if (failb
== syndrome_disks
+1) {
1488 /* Q disk is one of the missing disks */
1489 if (faila
== syndrome_disks
) {
1490 /* Missing P+Q, just recompute */
1491 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1492 ops_complete_compute
, sh
,
1493 to_addr_conv(sh
, percpu
, 0));
1494 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1495 STRIPE_SIZE
, &submit
);
1499 int qd_idx
= sh
->qd_idx
;
1501 /* Missing D+Q: recompute D from P, then recompute Q */
1502 if (target
== qd_idx
)
1503 data_target
= target2
;
1505 data_target
= target
;
1508 for (i
= disks
; i
-- ; ) {
1509 if (i
== data_target
|| i
== qd_idx
)
1511 blocks
[count
++] = sh
->dev
[i
].page
;
1513 dest
= sh
->dev
[data_target
].page
;
1514 init_async_submit(&submit
,
1515 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1517 to_addr_conv(sh
, percpu
, 0));
1518 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1521 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1522 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1523 ops_complete_compute
, sh
,
1524 to_addr_conv(sh
, percpu
, 0));
1525 return async_gen_syndrome(blocks
, 0, count
+2,
1526 STRIPE_SIZE
, &submit
);
1529 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1530 ops_complete_compute
, sh
,
1531 to_addr_conv(sh
, percpu
, 0));
1532 if (failb
== syndrome_disks
) {
1533 /* We're missing D+P. */
1534 return async_raid6_datap_recov(syndrome_disks
+2,
1538 /* We're missing D+D. */
1539 return async_raid6_2data_recov(syndrome_disks
+2,
1540 STRIPE_SIZE
, faila
, failb
,
1546 static void ops_complete_prexor(void *stripe_head_ref
)
1548 struct stripe_head
*sh
= stripe_head_ref
;
1550 pr_debug("%s: stripe %llu\n", __func__
,
1551 (unsigned long long)sh
->sector
);
1553 if (r5c_is_writeback(sh
->raid_conf
->log
))
1555 * raid5-cache write back uses orig_page during prexor.
1556 * After prexor, it is time to free orig_page
1558 r5c_release_extra_page(sh
);
1561 static struct dma_async_tx_descriptor
*
1562 ops_run_prexor5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1563 struct dma_async_tx_descriptor
*tx
)
1565 int disks
= sh
->disks
;
1566 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1567 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1568 struct async_submit_ctl submit
;
1570 /* existing parity data subtracted */
1571 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1573 BUG_ON(sh
->batch_head
);
1574 pr_debug("%s: stripe %llu\n", __func__
,
1575 (unsigned long long)sh
->sector
);
1577 for (i
= disks
; i
--; ) {
1578 struct r5dev
*dev
= &sh
->dev
[i
];
1579 /* Only process blocks that are known to be uptodate */
1580 if (test_bit(R5_InJournal
, &dev
->flags
))
1581 xor_srcs
[count
++] = dev
->orig_page
;
1582 else if (test_bit(R5_Wantdrain
, &dev
->flags
))
1583 xor_srcs
[count
++] = dev
->page
;
1586 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1587 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1588 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1593 static struct dma_async_tx_descriptor
*
1594 ops_run_prexor6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1595 struct dma_async_tx_descriptor
*tx
)
1597 struct page
**blocks
= to_addr_page(percpu
, 0);
1599 struct async_submit_ctl submit
;
1601 pr_debug("%s: stripe %llu\n", __func__
,
1602 (unsigned long long)sh
->sector
);
1604 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_WANT_DRAIN
);
1606 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_PQ_XOR_DST
, tx
,
1607 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1608 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1613 static struct dma_async_tx_descriptor
*
1614 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1616 struct r5conf
*conf
= sh
->raid_conf
;
1617 int disks
= sh
->disks
;
1619 struct stripe_head
*head_sh
= sh
;
1621 pr_debug("%s: stripe %llu\n", __func__
,
1622 (unsigned long long)sh
->sector
);
1624 for (i
= disks
; i
--; ) {
1629 if (test_and_clear_bit(R5_Wantdrain
, &head_sh
->dev
[i
].flags
)) {
1635 * clear R5_InJournal, so when rewriting a page in
1636 * journal, it is not skipped by r5l_log_stripe()
1638 clear_bit(R5_InJournal
, &dev
->flags
);
1639 spin_lock_irq(&sh
->stripe_lock
);
1640 chosen
= dev
->towrite
;
1641 dev
->towrite
= NULL
;
1642 sh
->overwrite_disks
= 0;
1643 BUG_ON(dev
->written
);
1644 wbi
= dev
->written
= chosen
;
1645 spin_unlock_irq(&sh
->stripe_lock
);
1646 WARN_ON(dev
->page
!= dev
->orig_page
);
1648 while (wbi
&& wbi
->bi_iter
.bi_sector
<
1649 dev
->sector
+ STRIPE_SECTORS
) {
1650 if (wbi
->bi_opf
& REQ_FUA
)
1651 set_bit(R5_WantFUA
, &dev
->flags
);
1652 if (wbi
->bi_opf
& REQ_SYNC
)
1653 set_bit(R5_SyncIO
, &dev
->flags
);
1654 if (bio_op(wbi
) == REQ_OP_DISCARD
)
1655 set_bit(R5_Discard
, &dev
->flags
);
1657 tx
= async_copy_data(1, wbi
, &dev
->page
,
1658 dev
->sector
, tx
, sh
,
1659 r5c_is_writeback(conf
->log
));
1660 if (dev
->page
!= dev
->orig_page
&&
1661 !r5c_is_writeback(conf
->log
)) {
1662 set_bit(R5_SkipCopy
, &dev
->flags
);
1663 clear_bit(R5_UPTODATE
, &dev
->flags
);
1664 clear_bit(R5_OVERWRITE
, &dev
->flags
);
1667 wbi
= r5_next_bio(wbi
, dev
->sector
);
1670 if (head_sh
->batch_head
) {
1671 sh
= list_first_entry(&sh
->batch_list
,
1684 static void ops_complete_reconstruct(void *stripe_head_ref
)
1686 struct stripe_head
*sh
= stripe_head_ref
;
1687 int disks
= sh
->disks
;
1688 int pd_idx
= sh
->pd_idx
;
1689 int qd_idx
= sh
->qd_idx
;
1691 bool fua
= false, sync
= false, discard
= false;
1693 pr_debug("%s: stripe %llu\n", __func__
,
1694 (unsigned long long)sh
->sector
);
1696 for (i
= disks
; i
--; ) {
1697 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1698 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1699 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1702 for (i
= disks
; i
--; ) {
1703 struct r5dev
*dev
= &sh
->dev
[i
];
1705 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1706 if (!discard
&& !test_bit(R5_SkipCopy
, &dev
->flags
))
1707 set_bit(R5_UPTODATE
, &dev
->flags
);
1709 set_bit(R5_WantFUA
, &dev
->flags
);
1711 set_bit(R5_SyncIO
, &dev
->flags
);
1715 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1716 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1717 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1718 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1720 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1721 sh
->reconstruct_state
= reconstruct_state_result
;
1724 set_bit(STRIPE_HANDLE
, &sh
->state
);
1725 raid5_release_stripe(sh
);
1729 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1730 struct dma_async_tx_descriptor
*tx
)
1732 int disks
= sh
->disks
;
1733 struct page
**xor_srcs
;
1734 struct async_submit_ctl submit
;
1735 int count
, pd_idx
= sh
->pd_idx
, i
;
1736 struct page
*xor_dest
;
1738 unsigned long flags
;
1740 struct stripe_head
*head_sh
= sh
;
1743 pr_debug("%s: stripe %llu\n", __func__
,
1744 (unsigned long long)sh
->sector
);
1746 for (i
= 0; i
< sh
->disks
; i
++) {
1749 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1752 if (i
>= sh
->disks
) {
1753 atomic_inc(&sh
->count
);
1754 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1755 ops_complete_reconstruct(sh
);
1760 xor_srcs
= to_addr_page(percpu
, j
);
1761 /* check if prexor is active which means only process blocks
1762 * that are part of a read-modify-write (written)
1764 if (head_sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1766 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1767 for (i
= disks
; i
--; ) {
1768 struct r5dev
*dev
= &sh
->dev
[i
];
1769 if (head_sh
->dev
[i
].written
||
1770 test_bit(R5_InJournal
, &head_sh
->dev
[i
].flags
))
1771 xor_srcs
[count
++] = dev
->page
;
1774 xor_dest
= sh
->dev
[pd_idx
].page
;
1775 for (i
= disks
; i
--; ) {
1776 struct r5dev
*dev
= &sh
->dev
[i
];
1778 xor_srcs
[count
++] = dev
->page
;
1782 /* 1/ if we prexor'd then the dest is reused as a source
1783 * 2/ if we did not prexor then we are redoing the parity
1784 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1785 * for the synchronous xor case
1787 last_stripe
= !head_sh
->batch_head
||
1788 list_first_entry(&sh
->batch_list
,
1789 struct stripe_head
, batch_list
) == head_sh
;
1791 flags
= ASYNC_TX_ACK
|
1792 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1794 atomic_inc(&head_sh
->count
);
1795 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, head_sh
,
1796 to_addr_conv(sh
, percpu
, j
));
1798 flags
= prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
;
1799 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
,
1800 to_addr_conv(sh
, percpu
, j
));
1803 if (unlikely(count
== 1))
1804 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1806 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1809 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1816 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1817 struct dma_async_tx_descriptor
*tx
)
1819 struct async_submit_ctl submit
;
1820 struct page
**blocks
;
1821 int count
, i
, j
= 0;
1822 struct stripe_head
*head_sh
= sh
;
1825 unsigned long txflags
;
1827 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1829 for (i
= 0; i
< sh
->disks
; i
++) {
1830 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1832 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1835 if (i
>= sh
->disks
) {
1836 atomic_inc(&sh
->count
);
1837 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1838 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1839 ops_complete_reconstruct(sh
);
1844 blocks
= to_addr_page(percpu
, j
);
1846 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1847 synflags
= SYNDROME_SRC_WRITTEN
;
1848 txflags
= ASYNC_TX_ACK
| ASYNC_TX_PQ_XOR_DST
;
1850 synflags
= SYNDROME_SRC_ALL
;
1851 txflags
= ASYNC_TX_ACK
;
1854 count
= set_syndrome_sources(blocks
, sh
, synflags
);
1855 last_stripe
= !head_sh
->batch_head
||
1856 list_first_entry(&sh
->batch_list
,
1857 struct stripe_head
, batch_list
) == head_sh
;
1860 atomic_inc(&head_sh
->count
);
1861 init_async_submit(&submit
, txflags
, tx
, ops_complete_reconstruct
,
1862 head_sh
, to_addr_conv(sh
, percpu
, j
));
1864 init_async_submit(&submit
, 0, tx
, NULL
, NULL
,
1865 to_addr_conv(sh
, percpu
, j
));
1866 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1869 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1875 static void ops_complete_check(void *stripe_head_ref
)
1877 struct stripe_head
*sh
= stripe_head_ref
;
1879 pr_debug("%s: stripe %llu\n", __func__
,
1880 (unsigned long long)sh
->sector
);
1882 sh
->check_state
= check_state_check_result
;
1883 set_bit(STRIPE_HANDLE
, &sh
->state
);
1884 raid5_release_stripe(sh
);
1887 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1889 int disks
= sh
->disks
;
1890 int pd_idx
= sh
->pd_idx
;
1891 int qd_idx
= sh
->qd_idx
;
1892 struct page
*xor_dest
;
1893 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1894 struct dma_async_tx_descriptor
*tx
;
1895 struct async_submit_ctl submit
;
1899 pr_debug("%s: stripe %llu\n", __func__
,
1900 (unsigned long long)sh
->sector
);
1902 BUG_ON(sh
->batch_head
);
1904 xor_dest
= sh
->dev
[pd_idx
].page
;
1905 xor_srcs
[count
++] = xor_dest
;
1906 for (i
= disks
; i
--; ) {
1907 if (i
== pd_idx
|| i
== qd_idx
)
1909 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1912 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1913 to_addr_conv(sh
, percpu
, 0));
1914 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1915 &sh
->ops
.zero_sum_result
, &submit
);
1917 atomic_inc(&sh
->count
);
1918 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1919 tx
= async_trigger_callback(&submit
);
1922 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1924 struct page
**srcs
= to_addr_page(percpu
, 0);
1925 struct async_submit_ctl submit
;
1928 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1929 (unsigned long long)sh
->sector
, checkp
);
1931 BUG_ON(sh
->batch_head
);
1932 count
= set_syndrome_sources(srcs
, sh
, SYNDROME_SRC_ALL
);
1936 atomic_inc(&sh
->count
);
1937 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1938 sh
, to_addr_conv(sh
, percpu
, 0));
1939 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1940 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1943 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1945 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1946 struct dma_async_tx_descriptor
*tx
= NULL
;
1947 struct r5conf
*conf
= sh
->raid_conf
;
1948 int level
= conf
->level
;
1949 struct raid5_percpu
*percpu
;
1953 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1954 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1955 ops_run_biofill(sh
);
1959 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1961 tx
= ops_run_compute5(sh
, percpu
);
1963 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1964 tx
= ops_run_compute6_1(sh
, percpu
);
1966 tx
= ops_run_compute6_2(sh
, percpu
);
1968 /* terminate the chain if reconstruct is not set to be run */
1969 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1973 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
)) {
1975 tx
= ops_run_prexor5(sh
, percpu
, tx
);
1977 tx
= ops_run_prexor6(sh
, percpu
, tx
);
1980 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1981 tx
= ops_run_biodrain(sh
, tx
);
1985 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1987 ops_run_reconstruct5(sh
, percpu
, tx
);
1989 ops_run_reconstruct6(sh
, percpu
, tx
);
1992 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1993 if (sh
->check_state
== check_state_run
)
1994 ops_run_check_p(sh
, percpu
);
1995 else if (sh
->check_state
== check_state_run_q
)
1996 ops_run_check_pq(sh
, percpu
, 0);
1997 else if (sh
->check_state
== check_state_run_pq
)
1998 ops_run_check_pq(sh
, percpu
, 1);
2003 if (overlap_clear
&& !sh
->batch_head
)
2004 for (i
= disks
; i
--; ) {
2005 struct r5dev
*dev
= &sh
->dev
[i
];
2006 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
2007 wake_up(&sh
->raid_conf
->wait_for_overlap
);
2012 static struct stripe_head
*alloc_stripe(struct kmem_cache
*sc
, gfp_t gfp
,
2015 struct stripe_head
*sh
;
2018 sh
= kmem_cache_zalloc(sc
, gfp
);
2020 spin_lock_init(&sh
->stripe_lock
);
2021 spin_lock_init(&sh
->batch_lock
);
2022 INIT_LIST_HEAD(&sh
->batch_list
);
2023 INIT_LIST_HEAD(&sh
->lru
);
2024 INIT_LIST_HEAD(&sh
->r5c
);
2025 INIT_LIST_HEAD(&sh
->log_list
);
2026 atomic_set(&sh
->count
, 1);
2027 sh
->log_start
= MaxSector
;
2028 for (i
= 0; i
< disks
; i
++) {
2029 struct r5dev
*dev
= &sh
->dev
[i
];
2031 bio_init(&dev
->req
, &dev
->vec
, 1);
2032 bio_init(&dev
->rreq
, &dev
->rvec
, 1);
2037 static int grow_one_stripe(struct r5conf
*conf
, gfp_t gfp
)
2039 struct stripe_head
*sh
;
2041 sh
= alloc_stripe(conf
->slab_cache
, gfp
, conf
->pool_size
);
2045 sh
->raid_conf
= conf
;
2047 if (grow_buffers(sh
, gfp
)) {
2049 kmem_cache_free(conf
->slab_cache
, sh
);
2052 sh
->hash_lock_index
=
2053 conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
;
2054 /* we just created an active stripe so... */
2055 atomic_inc(&conf
->active_stripes
);
2057 raid5_release_stripe(sh
);
2058 conf
->max_nr_stripes
++;
2062 static int grow_stripes(struct r5conf
*conf
, int num
)
2064 struct kmem_cache
*sc
;
2065 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
2067 if (conf
->mddev
->gendisk
)
2068 sprintf(conf
->cache_name
[0],
2069 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
2071 sprintf(conf
->cache_name
[0],
2072 "raid%d-%p", conf
->level
, conf
->mddev
);
2073 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
2075 conf
->active_name
= 0;
2076 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
2077 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
2081 conf
->slab_cache
= sc
;
2082 conf
->pool_size
= devs
;
2084 if (!grow_one_stripe(conf
, GFP_KERNEL
))
2091 * scribble_len - return the required size of the scribble region
2092 * @num - total number of disks in the array
2094 * The size must be enough to contain:
2095 * 1/ a struct page pointer for each device in the array +2
2096 * 2/ room to convert each entry in (1) to its corresponding dma
2097 * (dma_map_page()) or page (page_address()) address.
2099 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2100 * calculate over all devices (not just the data blocks), using zeros in place
2101 * of the P and Q blocks.
2103 static struct flex_array
*scribble_alloc(int num
, int cnt
, gfp_t flags
)
2105 struct flex_array
*ret
;
2108 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
2109 ret
= flex_array_alloc(len
, cnt
, flags
);
2112 /* always prealloc all elements, so no locking is required */
2113 if (flex_array_prealloc(ret
, 0, cnt
, flags
)) {
2114 flex_array_free(ret
);
2120 static int resize_chunks(struct r5conf
*conf
, int new_disks
, int new_sectors
)
2126 * Never shrink. And mddev_suspend() could deadlock if this is called
2127 * from raid5d. In that case, scribble_disks and scribble_sectors
2128 * should equal to new_disks and new_sectors
2130 if (conf
->scribble_disks
>= new_disks
&&
2131 conf
->scribble_sectors
>= new_sectors
)
2133 mddev_suspend(conf
->mddev
);
2135 for_each_present_cpu(cpu
) {
2136 struct raid5_percpu
*percpu
;
2137 struct flex_array
*scribble
;
2139 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
2140 scribble
= scribble_alloc(new_disks
,
2141 new_sectors
/ STRIPE_SECTORS
,
2145 flex_array_free(percpu
->scribble
);
2146 percpu
->scribble
= scribble
;
2153 mddev_resume(conf
->mddev
);
2155 conf
->scribble_disks
= new_disks
;
2156 conf
->scribble_sectors
= new_sectors
;
2161 static int resize_stripes(struct r5conf
*conf
, int newsize
)
2163 /* Make all the stripes able to hold 'newsize' devices.
2164 * New slots in each stripe get 'page' set to a new page.
2166 * This happens in stages:
2167 * 1/ create a new kmem_cache and allocate the required number of
2169 * 2/ gather all the old stripe_heads and transfer the pages across
2170 * to the new stripe_heads. This will have the side effect of
2171 * freezing the array as once all stripe_heads have been collected,
2172 * no IO will be possible. Old stripe heads are freed once their
2173 * pages have been transferred over, and the old kmem_cache is
2174 * freed when all stripes are done.
2175 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2176 * we simple return a failre status - no need to clean anything up.
2177 * 4/ allocate new pages for the new slots in the new stripe_heads.
2178 * If this fails, we don't bother trying the shrink the
2179 * stripe_heads down again, we just leave them as they are.
2180 * As each stripe_head is processed the new one is released into
2183 * Once step2 is started, we cannot afford to wait for a write,
2184 * so we use GFP_NOIO allocations.
2186 struct stripe_head
*osh
, *nsh
;
2187 LIST_HEAD(newstripes
);
2188 struct disk_info
*ndisks
;
2190 struct kmem_cache
*sc
;
2194 if (newsize
<= conf
->pool_size
)
2195 return 0; /* never bother to shrink */
2197 err
= md_allow_write(conf
->mddev
);
2202 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
2203 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
2208 /* Need to ensure auto-resizing doesn't interfere */
2209 mutex_lock(&conf
->cache_size_mutex
);
2211 for (i
= conf
->max_nr_stripes
; i
; i
--) {
2212 nsh
= alloc_stripe(sc
, GFP_KERNEL
, newsize
);
2216 nsh
->raid_conf
= conf
;
2217 list_add(&nsh
->lru
, &newstripes
);
2220 /* didn't get enough, give up */
2221 while (!list_empty(&newstripes
)) {
2222 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2223 list_del(&nsh
->lru
);
2224 kmem_cache_free(sc
, nsh
);
2226 kmem_cache_destroy(sc
);
2227 mutex_unlock(&conf
->cache_size_mutex
);
2230 /* Step 2 - Must use GFP_NOIO now.
2231 * OK, we have enough stripes, start collecting inactive
2232 * stripes and copying them over
2236 list_for_each_entry(nsh
, &newstripes
, lru
) {
2237 lock_device_hash_lock(conf
, hash
);
2238 wait_event_cmd(conf
->wait_for_stripe
,
2239 !list_empty(conf
->inactive_list
+ hash
),
2240 unlock_device_hash_lock(conf
, hash
),
2241 lock_device_hash_lock(conf
, hash
));
2242 osh
= get_free_stripe(conf
, hash
);
2243 unlock_device_hash_lock(conf
, hash
);
2245 for(i
=0; i
<conf
->pool_size
; i
++) {
2246 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
2247 nsh
->dev
[i
].orig_page
= osh
->dev
[i
].page
;
2249 nsh
->hash_lock_index
= hash
;
2250 kmem_cache_free(conf
->slab_cache
, osh
);
2252 if (cnt
>= conf
->max_nr_stripes
/ NR_STRIPE_HASH_LOCKS
+
2253 !!((conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
) > hash
)) {
2258 kmem_cache_destroy(conf
->slab_cache
);
2261 * At this point, we are holding all the stripes so the array
2262 * is completely stalled, so now is a good time to resize
2263 * conf->disks and the scribble region
2265 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
2267 for (i
= 0; i
< conf
->pool_size
; i
++)
2268 ndisks
[i
] = conf
->disks
[i
];
2270 for (i
= conf
->pool_size
; i
< newsize
; i
++) {
2271 ndisks
[i
].extra_page
= alloc_page(GFP_NOIO
);
2272 if (!ndisks
[i
].extra_page
)
2277 for (i
= conf
->pool_size
; i
< newsize
; i
++)
2278 if (ndisks
[i
].extra_page
)
2279 put_page(ndisks
[i
].extra_page
);
2283 conf
->disks
= ndisks
;
2288 mutex_unlock(&conf
->cache_size_mutex
);
2289 /* Step 4, return new stripes to service */
2290 while(!list_empty(&newstripes
)) {
2291 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2292 list_del_init(&nsh
->lru
);
2294 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
2295 if (nsh
->dev
[i
].page
== NULL
) {
2296 struct page
*p
= alloc_page(GFP_NOIO
);
2297 nsh
->dev
[i
].page
= p
;
2298 nsh
->dev
[i
].orig_page
= p
;
2302 raid5_release_stripe(nsh
);
2304 /* critical section pass, GFP_NOIO no longer needed */
2306 conf
->slab_cache
= sc
;
2307 conf
->active_name
= 1-conf
->active_name
;
2309 conf
->pool_size
= newsize
;
2313 static int drop_one_stripe(struct r5conf
*conf
)
2315 struct stripe_head
*sh
;
2316 int hash
= (conf
->max_nr_stripes
- 1) & STRIPE_HASH_LOCKS_MASK
;
2318 spin_lock_irq(conf
->hash_locks
+ hash
);
2319 sh
= get_free_stripe(conf
, hash
);
2320 spin_unlock_irq(conf
->hash_locks
+ hash
);
2323 BUG_ON(atomic_read(&sh
->count
));
2325 kmem_cache_free(conf
->slab_cache
, sh
);
2326 atomic_dec(&conf
->active_stripes
);
2327 conf
->max_nr_stripes
--;
2331 static void shrink_stripes(struct r5conf
*conf
)
2333 while (conf
->max_nr_stripes
&&
2334 drop_one_stripe(conf
))
2337 kmem_cache_destroy(conf
->slab_cache
);
2338 conf
->slab_cache
= NULL
;
2341 static void raid5_end_read_request(struct bio
* bi
)
2343 struct stripe_head
*sh
= bi
->bi_private
;
2344 struct r5conf
*conf
= sh
->raid_conf
;
2345 int disks
= sh
->disks
, i
;
2346 char b
[BDEVNAME_SIZE
];
2347 struct md_rdev
*rdev
= NULL
;
2350 for (i
=0 ; i
<disks
; i
++)
2351 if (bi
== &sh
->dev
[i
].req
)
2354 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2355 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2362 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2363 /* If replacement finished while this request was outstanding,
2364 * 'replacement' might be NULL already.
2365 * In that case it moved down to 'rdev'.
2366 * rdev is not removed until all requests are finished.
2368 rdev
= conf
->disks
[i
].replacement
;
2370 rdev
= conf
->disks
[i
].rdev
;
2372 if (use_new_offset(conf
, sh
))
2373 s
= sh
->sector
+ rdev
->new_data_offset
;
2375 s
= sh
->sector
+ rdev
->data_offset
;
2376 if (!bi
->bi_error
) {
2377 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2378 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2379 /* Note that this cannot happen on a
2380 * replacement device. We just fail those on
2383 pr_info_ratelimited(
2384 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
2385 mdname(conf
->mddev
), STRIPE_SECTORS
,
2386 (unsigned long long)s
,
2387 bdevname(rdev
->bdev
, b
));
2388 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
2389 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2390 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2391 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2392 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2394 if (test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
2396 * end read for a page in journal, this
2397 * must be preparing for prexor in rmw
2399 set_bit(R5_OrigPageUPTDODATE
, &sh
->dev
[i
].flags
);
2401 if (atomic_read(&rdev
->read_errors
))
2402 atomic_set(&rdev
->read_errors
, 0);
2404 const char *bdn
= bdevname(rdev
->bdev
, b
);
2408 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2409 atomic_inc(&rdev
->read_errors
);
2410 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2411 pr_warn_ratelimited(
2412 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
2413 mdname(conf
->mddev
),
2414 (unsigned long long)s
,
2416 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
2418 pr_warn_ratelimited(
2419 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
2420 mdname(conf
->mddev
),
2421 (unsigned long long)s
,
2423 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
2426 pr_warn_ratelimited(
2427 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
2428 mdname(conf
->mddev
),
2429 (unsigned long long)s
,
2431 } else if (atomic_read(&rdev
->read_errors
)
2432 > conf
->max_nr_stripes
)
2433 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2434 mdname(conf
->mddev
), bdn
);
2437 if (set_bad
&& test_bit(In_sync
, &rdev
->flags
)
2438 && !test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2441 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
2442 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2443 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2445 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2447 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2448 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2450 && test_bit(In_sync
, &rdev
->flags
)
2451 && rdev_set_badblocks(
2452 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
2453 md_error(conf
->mddev
, rdev
);
2456 rdev_dec_pending(rdev
, conf
->mddev
);
2458 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2459 set_bit(STRIPE_HANDLE
, &sh
->state
);
2460 raid5_release_stripe(sh
);
2463 static void raid5_end_write_request(struct bio
*bi
)
2465 struct stripe_head
*sh
= bi
->bi_private
;
2466 struct r5conf
*conf
= sh
->raid_conf
;
2467 int disks
= sh
->disks
, i
;
2468 struct md_rdev
*uninitialized_var(rdev
);
2471 int replacement
= 0;
2473 for (i
= 0 ; i
< disks
; i
++) {
2474 if (bi
== &sh
->dev
[i
].req
) {
2475 rdev
= conf
->disks
[i
].rdev
;
2478 if (bi
== &sh
->dev
[i
].rreq
) {
2479 rdev
= conf
->disks
[i
].replacement
;
2483 /* rdev was removed and 'replacement'
2484 * replaced it. rdev is not removed
2485 * until all requests are finished.
2487 rdev
= conf
->disks
[i
].rdev
;
2491 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2492 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2502 md_error(conf
->mddev
, rdev
);
2503 else if (is_badblock(rdev
, sh
->sector
,
2505 &first_bad
, &bad_sectors
))
2506 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
2509 set_bit(STRIPE_DEGRADED
, &sh
->state
);
2510 set_bit(WriteErrorSeen
, &rdev
->flags
);
2511 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
2512 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2513 set_bit(MD_RECOVERY_NEEDED
,
2514 &rdev
->mddev
->recovery
);
2515 } else if (is_badblock(rdev
, sh
->sector
,
2517 &first_bad
, &bad_sectors
)) {
2518 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
2519 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))
2520 /* That was a successful write so make
2521 * sure it looks like we already did
2524 set_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2527 rdev_dec_pending(rdev
, conf
->mddev
);
2529 if (sh
->batch_head
&& bi
->bi_error
&& !replacement
)
2530 set_bit(STRIPE_BATCH_ERR
, &sh
->batch_head
->state
);
2533 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
2534 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2535 set_bit(STRIPE_HANDLE
, &sh
->state
);
2536 raid5_release_stripe(sh
);
2538 if (sh
->batch_head
&& sh
!= sh
->batch_head
)
2539 raid5_release_stripe(sh
->batch_head
);
2542 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
2544 struct r5dev
*dev
= &sh
->dev
[i
];
2547 dev
->sector
= raid5_compute_blocknr(sh
, i
, previous
);
2550 static void raid5_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2552 char b
[BDEVNAME_SIZE
];
2553 struct r5conf
*conf
= mddev
->private;
2554 unsigned long flags
;
2555 pr_debug("raid456: error called\n");
2557 spin_lock_irqsave(&conf
->device_lock
, flags
);
2558 clear_bit(In_sync
, &rdev
->flags
);
2559 mddev
->degraded
= raid5_calc_degraded(conf
);
2560 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2561 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2563 set_bit(Blocked
, &rdev
->flags
);
2564 set_bit(Faulty
, &rdev
->flags
);
2565 set_mask_bits(&mddev
->sb_flags
, 0,
2566 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
2567 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2568 "md/raid:%s: Operation continuing on %d devices.\n",
2570 bdevname(rdev
->bdev
, b
),
2572 conf
->raid_disks
- mddev
->degraded
);
2573 r5c_update_on_rdev_error(mddev
);
2577 * Input: a 'big' sector number,
2578 * Output: index of the data and parity disk, and the sector # in them.
2580 sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
2581 int previous
, int *dd_idx
,
2582 struct stripe_head
*sh
)
2584 sector_t stripe
, stripe2
;
2585 sector_t chunk_number
;
2586 unsigned int chunk_offset
;
2589 sector_t new_sector
;
2590 int algorithm
= previous
? conf
->prev_algo
2592 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2593 : conf
->chunk_sectors
;
2594 int raid_disks
= previous
? conf
->previous_raid_disks
2596 int data_disks
= raid_disks
- conf
->max_degraded
;
2598 /* First compute the information on this sector */
2601 * Compute the chunk number and the sector offset inside the chunk
2603 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
2604 chunk_number
= r_sector
;
2607 * Compute the stripe number
2609 stripe
= chunk_number
;
2610 *dd_idx
= sector_div(stripe
, data_disks
);
2613 * Select the parity disk based on the user selected algorithm.
2615 pd_idx
= qd_idx
= -1;
2616 switch(conf
->level
) {
2618 pd_idx
= data_disks
;
2621 switch (algorithm
) {
2622 case ALGORITHM_LEFT_ASYMMETRIC
:
2623 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2624 if (*dd_idx
>= pd_idx
)
2627 case ALGORITHM_RIGHT_ASYMMETRIC
:
2628 pd_idx
= sector_div(stripe2
, raid_disks
);
2629 if (*dd_idx
>= pd_idx
)
2632 case ALGORITHM_LEFT_SYMMETRIC
:
2633 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2634 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2636 case ALGORITHM_RIGHT_SYMMETRIC
:
2637 pd_idx
= sector_div(stripe2
, raid_disks
);
2638 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2640 case ALGORITHM_PARITY_0
:
2644 case ALGORITHM_PARITY_N
:
2645 pd_idx
= data_disks
;
2653 switch (algorithm
) {
2654 case ALGORITHM_LEFT_ASYMMETRIC
:
2655 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2656 qd_idx
= pd_idx
+ 1;
2657 if (pd_idx
== raid_disks
-1) {
2658 (*dd_idx
)++; /* Q D D D P */
2660 } else if (*dd_idx
>= pd_idx
)
2661 (*dd_idx
) += 2; /* D D P Q D */
2663 case ALGORITHM_RIGHT_ASYMMETRIC
:
2664 pd_idx
= sector_div(stripe2
, raid_disks
);
2665 qd_idx
= pd_idx
+ 1;
2666 if (pd_idx
== raid_disks
-1) {
2667 (*dd_idx
)++; /* Q D D D P */
2669 } else if (*dd_idx
>= pd_idx
)
2670 (*dd_idx
) += 2; /* D D P Q D */
2672 case ALGORITHM_LEFT_SYMMETRIC
:
2673 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2674 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2675 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2677 case ALGORITHM_RIGHT_SYMMETRIC
:
2678 pd_idx
= sector_div(stripe2
, raid_disks
);
2679 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2680 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2683 case ALGORITHM_PARITY_0
:
2688 case ALGORITHM_PARITY_N
:
2689 pd_idx
= data_disks
;
2690 qd_idx
= data_disks
+ 1;
2693 case ALGORITHM_ROTATING_ZERO_RESTART
:
2694 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2695 * of blocks for computing Q is different.
2697 pd_idx
= sector_div(stripe2
, raid_disks
);
2698 qd_idx
= pd_idx
+ 1;
2699 if (pd_idx
== raid_disks
-1) {
2700 (*dd_idx
)++; /* Q D D D P */
2702 } else if (*dd_idx
>= pd_idx
)
2703 (*dd_idx
) += 2; /* D D P Q D */
2707 case ALGORITHM_ROTATING_N_RESTART
:
2708 /* Same a left_asymmetric, by first stripe is
2709 * D D D P Q rather than
2713 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2714 qd_idx
= pd_idx
+ 1;
2715 if (pd_idx
== raid_disks
-1) {
2716 (*dd_idx
)++; /* Q D D D P */
2718 } else if (*dd_idx
>= pd_idx
)
2719 (*dd_idx
) += 2; /* D D P Q D */
2723 case ALGORITHM_ROTATING_N_CONTINUE
:
2724 /* Same as left_symmetric but Q is before P */
2725 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2726 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2727 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2731 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2732 /* RAID5 left_asymmetric, with Q on last device */
2733 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2734 if (*dd_idx
>= pd_idx
)
2736 qd_idx
= raid_disks
- 1;
2739 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2740 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2741 if (*dd_idx
>= pd_idx
)
2743 qd_idx
= raid_disks
- 1;
2746 case ALGORITHM_LEFT_SYMMETRIC_6
:
2747 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2748 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2749 qd_idx
= raid_disks
- 1;
2752 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2753 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2754 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2755 qd_idx
= raid_disks
- 1;
2758 case ALGORITHM_PARITY_0_6
:
2761 qd_idx
= raid_disks
- 1;
2771 sh
->pd_idx
= pd_idx
;
2772 sh
->qd_idx
= qd_idx
;
2773 sh
->ddf_layout
= ddf_layout
;
2776 * Finally, compute the new sector number
2778 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2782 sector_t
raid5_compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2784 struct r5conf
*conf
= sh
->raid_conf
;
2785 int raid_disks
= sh
->disks
;
2786 int data_disks
= raid_disks
- conf
->max_degraded
;
2787 sector_t new_sector
= sh
->sector
, check
;
2788 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2789 : conf
->chunk_sectors
;
2790 int algorithm
= previous
? conf
->prev_algo
2794 sector_t chunk_number
;
2795 int dummy1
, dd_idx
= i
;
2797 struct stripe_head sh2
;
2799 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2800 stripe
= new_sector
;
2802 if (i
== sh
->pd_idx
)
2804 switch(conf
->level
) {
2807 switch (algorithm
) {
2808 case ALGORITHM_LEFT_ASYMMETRIC
:
2809 case ALGORITHM_RIGHT_ASYMMETRIC
:
2813 case ALGORITHM_LEFT_SYMMETRIC
:
2814 case ALGORITHM_RIGHT_SYMMETRIC
:
2817 i
-= (sh
->pd_idx
+ 1);
2819 case ALGORITHM_PARITY_0
:
2822 case ALGORITHM_PARITY_N
:
2829 if (i
== sh
->qd_idx
)
2830 return 0; /* It is the Q disk */
2831 switch (algorithm
) {
2832 case ALGORITHM_LEFT_ASYMMETRIC
:
2833 case ALGORITHM_RIGHT_ASYMMETRIC
:
2834 case ALGORITHM_ROTATING_ZERO_RESTART
:
2835 case ALGORITHM_ROTATING_N_RESTART
:
2836 if (sh
->pd_idx
== raid_disks
-1)
2837 i
--; /* Q D D D P */
2838 else if (i
> sh
->pd_idx
)
2839 i
-= 2; /* D D P Q D */
2841 case ALGORITHM_LEFT_SYMMETRIC
:
2842 case ALGORITHM_RIGHT_SYMMETRIC
:
2843 if (sh
->pd_idx
== raid_disks
-1)
2844 i
--; /* Q D D D P */
2849 i
-= (sh
->pd_idx
+ 2);
2852 case ALGORITHM_PARITY_0
:
2855 case ALGORITHM_PARITY_N
:
2857 case ALGORITHM_ROTATING_N_CONTINUE
:
2858 /* Like left_symmetric, but P is before Q */
2859 if (sh
->pd_idx
== 0)
2860 i
--; /* P D D D Q */
2865 i
-= (sh
->pd_idx
+ 1);
2868 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2869 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2873 case ALGORITHM_LEFT_SYMMETRIC_6
:
2874 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2876 i
+= data_disks
+ 1;
2877 i
-= (sh
->pd_idx
+ 1);
2879 case ALGORITHM_PARITY_0_6
:
2888 chunk_number
= stripe
* data_disks
+ i
;
2889 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2891 check
= raid5_compute_sector(conf
, r_sector
,
2892 previous
, &dummy1
, &sh2
);
2893 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2894 || sh2
.qd_idx
!= sh
->qd_idx
) {
2895 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2896 mdname(conf
->mddev
));
2903 * There are cases where we want handle_stripe_dirtying() and
2904 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2906 * This function checks whether we want to delay the towrite. Specifically,
2907 * we delay the towrite when:
2909 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2910 * stripe has data in journal (for other devices).
2912 * In this case, when reading data for the non-overwrite dev, it is
2913 * necessary to handle complex rmw of write back cache (prexor with
2914 * orig_page, and xor with page). To keep read path simple, we would
2915 * like to flush data in journal to RAID disks first, so complex rmw
2916 * is handled in the write patch (handle_stripe_dirtying).
2919 static inline bool delay_towrite(struct r5dev
*dev
,
2920 struct stripe_head_state
*s
)
2922 return !test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2923 !test_bit(R5_Insync
, &dev
->flags
) && s
->injournal
;
2927 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2928 int rcw
, int expand
)
2930 int i
, pd_idx
= sh
->pd_idx
, qd_idx
= sh
->qd_idx
, disks
= sh
->disks
;
2931 struct r5conf
*conf
= sh
->raid_conf
;
2932 int level
= conf
->level
;
2936 * In some cases, handle_stripe_dirtying initially decided to
2937 * run rmw and allocates extra page for prexor. However, rcw is
2938 * cheaper later on. We need to free the extra page now,
2939 * because we won't be able to do that in ops_complete_prexor().
2941 r5c_release_extra_page(sh
);
2943 for (i
= disks
; i
--; ) {
2944 struct r5dev
*dev
= &sh
->dev
[i
];
2946 if (dev
->towrite
&& !delay_towrite(dev
, s
)) {
2947 set_bit(R5_LOCKED
, &dev
->flags
);
2948 set_bit(R5_Wantdrain
, &dev
->flags
);
2950 clear_bit(R5_UPTODATE
, &dev
->flags
);
2952 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
2953 set_bit(R5_LOCKED
, &dev
->flags
);
2957 /* if we are not expanding this is a proper write request, and
2958 * there will be bios with new data to be drained into the
2963 /* False alarm, nothing to do */
2965 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2966 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2968 sh
->reconstruct_state
= reconstruct_state_run
;
2970 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2972 if (s
->locked
+ conf
->max_degraded
== disks
)
2973 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2974 atomic_inc(&conf
->pending_full_writes
);
2976 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2977 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2978 BUG_ON(level
== 6 &&
2979 (!(test_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
) ||
2980 test_bit(R5_Wantcompute
, &sh
->dev
[qd_idx
].flags
))));
2982 for (i
= disks
; i
--; ) {
2983 struct r5dev
*dev
= &sh
->dev
[i
];
2984 if (i
== pd_idx
|| i
== qd_idx
)
2988 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2989 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2990 set_bit(R5_Wantdrain
, &dev
->flags
);
2991 set_bit(R5_LOCKED
, &dev
->flags
);
2992 clear_bit(R5_UPTODATE
, &dev
->flags
);
2994 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
2995 set_bit(R5_LOCKED
, &dev
->flags
);
3000 /* False alarm - nothing to do */
3002 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
3003 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
3004 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
3005 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
3008 /* keep the parity disk(s) locked while asynchronous operations
3011 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
3012 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3016 int qd_idx
= sh
->qd_idx
;
3017 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
3019 set_bit(R5_LOCKED
, &dev
->flags
);
3020 clear_bit(R5_UPTODATE
, &dev
->flags
);
3024 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
3025 __func__
, (unsigned long long)sh
->sector
,
3026 s
->locked
, s
->ops_request
);
3030 * Each stripe/dev can have one or more bion attached.
3031 * toread/towrite point to the first in a chain.
3032 * The bi_next chain must be in order.
3034 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
,
3035 int forwrite
, int previous
)
3038 struct r5conf
*conf
= sh
->raid_conf
;
3041 pr_debug("adding bi b#%llu to stripe s#%llu\n",
3042 (unsigned long long)bi
->bi_iter
.bi_sector
,
3043 (unsigned long long)sh
->sector
);
3046 * If several bio share a stripe. The bio bi_phys_segments acts as a
3047 * reference count to avoid race. The reference count should already be
3048 * increased before this function is called (for example, in
3049 * raid5_make_request()), so other bio sharing this stripe will not free the
3050 * stripe. If a stripe is owned by one stripe, the stripe lock will
3053 spin_lock_irq(&sh
->stripe_lock
);
3054 /* Don't allow new IO added to stripes in batch list */
3058 bip
= &sh
->dev
[dd_idx
].towrite
;
3062 bip
= &sh
->dev
[dd_idx
].toread
;
3063 while (*bip
&& (*bip
)->bi_iter
.bi_sector
< bi
->bi_iter
.bi_sector
) {
3064 if (bio_end_sector(*bip
) > bi
->bi_iter
.bi_sector
)
3066 bip
= & (*bip
)->bi_next
;
3068 if (*bip
&& (*bip
)->bi_iter
.bi_sector
< bio_end_sector(bi
))
3071 if (!forwrite
|| previous
)
3072 clear_bit(STRIPE_BATCH_READY
, &sh
->state
);
3074 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
3078 raid5_inc_bi_active_stripes(bi
);
3081 /* check if page is covered */
3082 sector_t sector
= sh
->dev
[dd_idx
].sector
;
3083 for (bi
=sh
->dev
[dd_idx
].towrite
;
3084 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
3085 bi
&& bi
->bi_iter
.bi_sector
<= sector
;
3086 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
3087 if (bio_end_sector(bi
) >= sector
)
3088 sector
= bio_end_sector(bi
);
3090 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
3091 if (!test_and_set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
))
3092 sh
->overwrite_disks
++;
3095 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
3096 (unsigned long long)(*bip
)->bi_iter
.bi_sector
,
3097 (unsigned long long)sh
->sector
, dd_idx
);
3099 if (conf
->mddev
->bitmap
&& firstwrite
) {
3100 /* Cannot hold spinlock over bitmap_startwrite,
3101 * but must ensure this isn't added to a batch until
3102 * we have added to the bitmap and set bm_seq.
3103 * So set STRIPE_BITMAP_PENDING to prevent
3105 * If multiple add_stripe_bio() calls race here they
3106 * much all set STRIPE_BITMAP_PENDING. So only the first one
3107 * to complete "bitmap_startwrite" gets to set
3108 * STRIPE_BIT_DELAY. This is important as once a stripe
3109 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3112 set_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3113 spin_unlock_irq(&sh
->stripe_lock
);
3114 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
3116 spin_lock_irq(&sh
->stripe_lock
);
3117 clear_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3118 if (!sh
->batch_head
) {
3119 sh
->bm_seq
= conf
->seq_flush
+1;
3120 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
3123 spin_unlock_irq(&sh
->stripe_lock
);
3125 if (stripe_can_batch(sh
))
3126 stripe_add_to_batch_list(conf
, sh
);
3130 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
3131 spin_unlock_irq(&sh
->stripe_lock
);
3135 static void end_reshape(struct r5conf
*conf
);
3137 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
3138 struct stripe_head
*sh
)
3140 int sectors_per_chunk
=
3141 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
3143 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
3144 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
3146 raid5_compute_sector(conf
,
3147 stripe
* (disks
- conf
->max_degraded
)
3148 *sectors_per_chunk
+ chunk_offset
,
3154 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
3155 struct stripe_head_state
*s
, int disks
,
3156 struct bio_list
*return_bi
)
3159 BUG_ON(sh
->batch_head
);
3160 for (i
= disks
; i
--; ) {
3164 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
3165 struct md_rdev
*rdev
;
3167 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3168 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
3169 !test_bit(Faulty
, &rdev
->flags
))
3170 atomic_inc(&rdev
->nr_pending
);
3175 if (!rdev_set_badblocks(
3179 md_error(conf
->mddev
, rdev
);
3180 rdev_dec_pending(rdev
, conf
->mddev
);
3183 spin_lock_irq(&sh
->stripe_lock
);
3184 /* fail all writes first */
3185 bi
= sh
->dev
[i
].towrite
;
3186 sh
->dev
[i
].towrite
= NULL
;
3187 sh
->overwrite_disks
= 0;
3188 spin_unlock_irq(&sh
->stripe_lock
);
3192 r5l_stripe_write_finished(sh
);
3194 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3195 wake_up(&conf
->wait_for_overlap
);
3197 while (bi
&& bi
->bi_iter
.bi_sector
<
3198 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3199 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3201 bi
->bi_error
= -EIO
;
3202 if (!raid5_dec_bi_active_stripes(bi
)) {
3203 md_write_end(conf
->mddev
);
3204 bio_list_add(return_bi
, bi
);
3209 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3210 STRIPE_SECTORS
, 0, 0);
3212 /* and fail all 'written' */
3213 bi
= sh
->dev
[i
].written
;
3214 sh
->dev
[i
].written
= NULL
;
3215 if (test_and_clear_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
)) {
3216 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
3217 sh
->dev
[i
].page
= sh
->dev
[i
].orig_page
;
3220 if (bi
) bitmap_end
= 1;
3221 while (bi
&& bi
->bi_iter
.bi_sector
<
3222 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3223 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3225 bi
->bi_error
= -EIO
;
3226 if (!raid5_dec_bi_active_stripes(bi
)) {
3227 md_write_end(conf
->mddev
);
3228 bio_list_add(return_bi
, bi
);
3233 /* fail any reads if this device is non-operational and
3234 * the data has not reached the cache yet.
3236 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
3237 s
->failed
> conf
->max_degraded
&&
3238 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
3239 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
3240 spin_lock_irq(&sh
->stripe_lock
);
3241 bi
= sh
->dev
[i
].toread
;
3242 sh
->dev
[i
].toread
= NULL
;
3243 spin_unlock_irq(&sh
->stripe_lock
);
3244 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3245 wake_up(&conf
->wait_for_overlap
);
3248 while (bi
&& bi
->bi_iter
.bi_sector
<
3249 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3250 struct bio
*nextbi
=
3251 r5_next_bio(bi
, sh
->dev
[i
].sector
);
3253 bi
->bi_error
= -EIO
;
3254 if (!raid5_dec_bi_active_stripes(bi
))
3255 bio_list_add(return_bi
, bi
);
3260 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3261 STRIPE_SECTORS
, 0, 0);
3262 /* If we were in the middle of a write the parity block might
3263 * still be locked - so just clear all R5_LOCKED flags
3265 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3270 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3271 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3272 md_wakeup_thread(conf
->mddev
->thread
);
3276 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
3277 struct stripe_head_state
*s
)
3282 BUG_ON(sh
->batch_head
);
3283 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3284 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
3285 wake_up(&conf
->wait_for_overlap
);
3288 /* There is nothing more to do for sync/check/repair.
3289 * Don't even need to abort as that is handled elsewhere
3290 * if needed, and not always wanted e.g. if there is a known
3292 * For recover/replace we need to record a bad block on all
3293 * non-sync devices, or abort the recovery
3295 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
3296 /* During recovery devices cannot be removed, so
3297 * locking and refcounting of rdevs is not needed
3300 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3301 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3303 && !test_bit(Faulty
, &rdev
->flags
)
3304 && !test_bit(In_sync
, &rdev
->flags
)
3305 && !rdev_set_badblocks(rdev
, sh
->sector
,
3308 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3310 && !test_bit(Faulty
, &rdev
->flags
)
3311 && !test_bit(In_sync
, &rdev
->flags
)
3312 && !rdev_set_badblocks(rdev
, sh
->sector
,
3318 conf
->recovery_disabled
=
3319 conf
->mddev
->recovery_disabled
;
3321 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
3324 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
3326 struct md_rdev
*rdev
;
3330 rdev
= rcu_dereference(sh
->raid_conf
->disks
[disk_idx
].replacement
);
3332 && !test_bit(Faulty
, &rdev
->flags
)
3333 && !test_bit(In_sync
, &rdev
->flags
)
3334 && (rdev
->recovery_offset
<= sh
->sector
3335 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
3341 static int need_this_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3342 int disk_idx
, int disks
)
3344 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3345 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
3346 &sh
->dev
[s
->failed_num
[1]] };
3350 if (test_bit(R5_LOCKED
, &dev
->flags
) ||
3351 test_bit(R5_UPTODATE
, &dev
->flags
))
3352 /* No point reading this as we already have it or have
3353 * decided to get it.
3358 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)))
3359 /* We need this block to directly satisfy a request */
3362 if (s
->syncing
|| s
->expanding
||
3363 (s
->replacing
&& want_replace(sh
, disk_idx
)))
3364 /* When syncing, or expanding we read everything.
3365 * When replacing, we need the replaced block.
3369 if ((s
->failed
>= 1 && fdev
[0]->toread
) ||
3370 (s
->failed
>= 2 && fdev
[1]->toread
))
3371 /* If we want to read from a failed device, then
3372 * we need to actually read every other device.
3376 /* Sometimes neither read-modify-write nor reconstruct-write
3377 * cycles can work. In those cases we read every block we
3378 * can. Then the parity-update is certain to have enough to
3380 * This can only be a problem when we need to write something,
3381 * and some device has failed. If either of those tests
3382 * fail we need look no further.
3384 if (!s
->failed
|| !s
->to_write
)
3387 if (test_bit(R5_Insync
, &dev
->flags
) &&
3388 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3389 /* Pre-reads at not permitted until after short delay
3390 * to gather multiple requests. However if this
3391 * device is no Insync, the block could only be be computed
3392 * and there is no need to delay that.
3396 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3397 if (fdev
[i
]->towrite
&&
3398 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3399 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3400 /* If we have a partial write to a failed
3401 * device, then we will need to reconstruct
3402 * the content of that device, so all other
3403 * devices must be read.
3408 /* If we are forced to do a reconstruct-write, either because
3409 * the current RAID6 implementation only supports that, or
3410 * or because parity cannot be trusted and we are currently
3411 * recovering it, there is extra need to be careful.
3412 * If one of the devices that we would need to read, because
3413 * it is not being overwritten (and maybe not written at all)
3414 * is missing/faulty, then we need to read everything we can.
3416 if (sh
->raid_conf
->level
!= 6 &&
3417 sh
->sector
< sh
->raid_conf
->mddev
->recovery_cp
)
3418 /* reconstruct-write isn't being forced */
3420 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3421 if (s
->failed_num
[i
] != sh
->pd_idx
&&
3422 s
->failed_num
[i
] != sh
->qd_idx
&&
3423 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3424 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3431 /* fetch_block - checks the given member device to see if its data needs
3432 * to be read or computed to satisfy a request.
3434 * Returns 1 when no more member devices need to be checked, otherwise returns
3435 * 0 to tell the loop in handle_stripe_fill to continue
3437 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3438 int disk_idx
, int disks
)
3440 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3442 /* is the data in this block needed, and can we get it? */
3443 if (need_this_block(sh
, s
, disk_idx
, disks
)) {
3444 /* we would like to get this block, possibly by computing it,
3445 * otherwise read it if the backing disk is insync
3447 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
3448 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
3449 BUG_ON(sh
->batch_head
);
3450 if ((s
->uptodate
== disks
- 1) &&
3451 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
3452 disk_idx
== s
->failed_num
[1]))) {
3453 /* have disk failed, and we're requested to fetch it;
3456 pr_debug("Computing stripe %llu block %d\n",
3457 (unsigned long long)sh
->sector
, disk_idx
);
3458 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3459 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3460 set_bit(R5_Wantcompute
, &dev
->flags
);
3461 sh
->ops
.target
= disk_idx
;
3462 sh
->ops
.target2
= -1; /* no 2nd target */
3464 /* Careful: from this point on 'uptodate' is in the eye
3465 * of raid_run_ops which services 'compute' operations
3466 * before writes. R5_Wantcompute flags a block that will
3467 * be R5_UPTODATE by the time it is needed for a
3468 * subsequent operation.
3472 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
3473 /* Computing 2-failure is *very* expensive; only
3474 * do it if failed >= 2
3477 for (other
= disks
; other
--; ) {
3478 if (other
== disk_idx
)
3480 if (!test_bit(R5_UPTODATE
,
3481 &sh
->dev
[other
].flags
))
3485 pr_debug("Computing stripe %llu blocks %d,%d\n",
3486 (unsigned long long)sh
->sector
,
3488 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3489 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3490 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
3491 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
3492 sh
->ops
.target
= disk_idx
;
3493 sh
->ops
.target2
= other
;
3497 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
3498 set_bit(R5_LOCKED
, &dev
->flags
);
3499 set_bit(R5_Wantread
, &dev
->flags
);
3501 pr_debug("Reading block %d (sync=%d)\n",
3502 disk_idx
, s
->syncing
);
3510 * handle_stripe_fill - read or compute data to satisfy pending requests.
3512 static void handle_stripe_fill(struct stripe_head
*sh
,
3513 struct stripe_head_state
*s
,
3518 /* look for blocks to read/compute, skip this if a compute
3519 * is already in flight, or if the stripe contents are in the
3520 * midst of changing due to a write
3522 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
3523 !sh
->reconstruct_state
) {
3526 * For degraded stripe with data in journal, do not handle
3527 * read requests yet, instead, flush the stripe to raid
3528 * disks first, this avoids handling complex rmw of write
3529 * back cache (prexor with orig_page, and then xor with
3530 * page) in the read path
3532 if (s
->injournal
&& s
->failed
) {
3533 if (test_bit(STRIPE_R5C_CACHING
, &sh
->state
))
3534 r5c_make_stripe_write_out(sh
);
3538 for (i
= disks
; i
--; )
3539 if (fetch_block(sh
, s
, i
, disks
))
3543 set_bit(STRIPE_HANDLE
, &sh
->state
);
3546 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
3547 unsigned long handle_flags
);
3548 /* handle_stripe_clean_event
3549 * any written block on an uptodate or failed drive can be returned.
3550 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3551 * never LOCKED, so we don't need to test 'failed' directly.
3553 static void handle_stripe_clean_event(struct r5conf
*conf
,
3554 struct stripe_head
*sh
, int disks
, struct bio_list
*return_bi
)
3558 int discard_pending
= 0;
3559 struct stripe_head
*head_sh
= sh
;
3560 bool do_endio
= false;
3562 for (i
= disks
; i
--; )
3563 if (sh
->dev
[i
].written
) {
3565 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
3566 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
3567 test_bit(R5_Discard
, &dev
->flags
) ||
3568 test_bit(R5_SkipCopy
, &dev
->flags
))) {
3569 /* We can return any write requests */
3570 struct bio
*wbi
, *wbi2
;
3571 pr_debug("Return write for disc %d\n", i
);
3572 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
3573 clear_bit(R5_UPTODATE
, &dev
->flags
);
3574 if (test_and_clear_bit(R5_SkipCopy
, &dev
->flags
)) {
3575 WARN_ON(test_bit(R5_UPTODATE
, &dev
->flags
));
3580 dev
->page
= dev
->orig_page
;
3582 dev
->written
= NULL
;
3583 while (wbi
&& wbi
->bi_iter
.bi_sector
<
3584 dev
->sector
+ STRIPE_SECTORS
) {
3585 wbi2
= r5_next_bio(wbi
, dev
->sector
);
3586 if (!raid5_dec_bi_active_stripes(wbi
)) {
3587 md_write_end(conf
->mddev
);
3588 bio_list_add(return_bi
, wbi
);
3592 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3594 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
3596 if (head_sh
->batch_head
) {
3597 sh
= list_first_entry(&sh
->batch_list
,
3600 if (sh
!= head_sh
) {
3607 } else if (test_bit(R5_Discard
, &dev
->flags
))
3608 discard_pending
= 1;
3611 r5l_stripe_write_finished(sh
);
3613 if (!discard_pending
&&
3614 test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3616 clear_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
3617 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3618 if (sh
->qd_idx
>= 0) {
3619 clear_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
3620 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
);
3622 /* now that discard is done we can proceed with any sync */
3623 clear_bit(STRIPE_DISCARD
, &sh
->state
);
3625 * SCSI discard will change some bio fields and the stripe has
3626 * no updated data, so remove it from hash list and the stripe
3627 * will be reinitialized
3630 hash
= sh
->hash_lock_index
;
3631 spin_lock_irq(conf
->hash_locks
+ hash
);
3633 spin_unlock_irq(conf
->hash_locks
+ hash
);
3634 if (head_sh
->batch_head
) {
3635 sh
= list_first_entry(&sh
->batch_list
,
3636 struct stripe_head
, batch_list
);
3642 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
))
3643 set_bit(STRIPE_HANDLE
, &sh
->state
);
3647 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3648 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3649 md_wakeup_thread(conf
->mddev
->thread
);
3651 if (head_sh
->batch_head
&& do_endio
)
3652 break_stripe_batch_list(head_sh
, STRIPE_EXPAND_SYNC_FLAGS
);
3656 * For RMW in write back cache, we need extra page in prexor to store the
3657 * old data. This page is stored in dev->orig_page.
3659 * This function checks whether we have data for prexor. The exact logic
3661 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3663 static inline bool uptodate_for_rmw(struct r5dev
*dev
)
3665 return (test_bit(R5_UPTODATE
, &dev
->flags
)) &&
3666 (!test_bit(R5_InJournal
, &dev
->flags
) ||
3667 test_bit(R5_OrigPageUPTDODATE
, &dev
->flags
));
3670 static int handle_stripe_dirtying(struct r5conf
*conf
,
3671 struct stripe_head
*sh
,
3672 struct stripe_head_state
*s
,
3675 int rmw
= 0, rcw
= 0, i
;
3676 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
3678 /* Check whether resync is now happening or should start.
3679 * If yes, then the array is dirty (after unclean shutdown or
3680 * initial creation), so parity in some stripes might be inconsistent.
3681 * In this case, we need to always do reconstruct-write, to ensure
3682 * that in case of drive failure or read-error correction, we
3683 * generate correct data from the parity.
3685 if (conf
->rmw_level
== PARITY_DISABLE_RMW
||
3686 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
&&
3688 /* Calculate the real rcw later - for now make it
3689 * look like rcw is cheaper
3692 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3693 conf
->rmw_level
, (unsigned long long)recovery_cp
,
3694 (unsigned long long)sh
->sector
);
3695 } else for (i
= disks
; i
--; ) {
3696 /* would I have to read this buffer for read_modify_write */
3697 struct r5dev
*dev
= &sh
->dev
[i
];
3698 if (((dev
->towrite
&& !delay_towrite(dev
, s
)) ||
3699 i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3700 test_bit(R5_InJournal
, &dev
->flags
)) &&
3701 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3702 !(uptodate_for_rmw(dev
) ||
3703 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3704 if (test_bit(R5_Insync
, &dev
->flags
))
3707 rmw
+= 2*disks
; /* cannot read it */
3709 /* Would I have to read this buffer for reconstruct_write */
3710 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3711 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3712 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3713 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3714 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3715 if (test_bit(R5_Insync
, &dev
->flags
))
3722 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
3723 (unsigned long long)sh
->sector
, rmw
, rcw
);
3724 set_bit(STRIPE_HANDLE
, &sh
->state
);
3725 if ((rmw
< rcw
|| (rmw
== rcw
&& conf
->rmw_level
== PARITY_PREFER_RMW
)) && rmw
> 0) {
3726 /* prefer read-modify-write, but need to get some data */
3727 if (conf
->mddev
->queue
)
3728 blk_add_trace_msg(conf
->mddev
->queue
,
3729 "raid5 rmw %llu %d",
3730 (unsigned long long)sh
->sector
, rmw
);
3731 for (i
= disks
; i
--; ) {
3732 struct r5dev
*dev
= &sh
->dev
[i
];
3733 if (test_bit(R5_InJournal
, &dev
->flags
) &&
3734 dev
->page
== dev
->orig_page
&&
3735 !test_bit(R5_LOCKED
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3736 /* alloc page for prexor */
3737 struct page
*p
= alloc_page(GFP_NOIO
);
3745 * alloc_page() failed, try use
3746 * disk_info->extra_page
3748 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE
,
3749 &conf
->cache_state
)) {
3750 r5c_use_extra_page(sh
);
3754 /* extra_page in use, add to delayed_list */
3755 set_bit(STRIPE_DELAYED
, &sh
->state
);
3756 s
->waiting_extra_page
= 1;
3761 for (i
= disks
; i
--; ) {
3762 struct r5dev
*dev
= &sh
->dev
[i
];
3763 if (((dev
->towrite
&& !delay_towrite(dev
, s
)) ||
3764 i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3765 test_bit(R5_InJournal
, &dev
->flags
)) &&
3766 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3767 !(uptodate_for_rmw(dev
) ||
3768 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
3769 test_bit(R5_Insync
, &dev
->flags
)) {
3770 if (test_bit(STRIPE_PREREAD_ACTIVE
,
3772 pr_debug("Read_old block %d for r-m-w\n",
3774 set_bit(R5_LOCKED
, &dev
->flags
);
3775 set_bit(R5_Wantread
, &dev
->flags
);
3778 set_bit(STRIPE_DELAYED
, &sh
->state
);
3779 set_bit(STRIPE_HANDLE
, &sh
->state
);
3784 if ((rcw
< rmw
|| (rcw
== rmw
&& conf
->rmw_level
!= PARITY_PREFER_RMW
)) && rcw
> 0) {
3785 /* want reconstruct write, but need to get some data */
3788 for (i
= disks
; i
--; ) {
3789 struct r5dev
*dev
= &sh
->dev
[i
];
3790 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3791 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3792 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3793 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3794 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3796 if (test_bit(R5_Insync
, &dev
->flags
) &&
3797 test_bit(STRIPE_PREREAD_ACTIVE
,
3799 pr_debug("Read_old block "
3800 "%d for Reconstruct\n", i
);
3801 set_bit(R5_LOCKED
, &dev
->flags
);
3802 set_bit(R5_Wantread
, &dev
->flags
);
3806 set_bit(STRIPE_DELAYED
, &sh
->state
);
3807 set_bit(STRIPE_HANDLE
, &sh
->state
);
3811 if (rcw
&& conf
->mddev
->queue
)
3812 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
3813 (unsigned long long)sh
->sector
,
3814 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
3817 if (rcw
> disks
&& rmw
> disks
&&
3818 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3819 set_bit(STRIPE_DELAYED
, &sh
->state
);
3821 /* now if nothing is locked, and if we have enough data,
3822 * we can start a write request
3824 /* since handle_stripe can be called at any time we need to handle the
3825 * case where a compute block operation has been submitted and then a
3826 * subsequent call wants to start a write request. raid_run_ops only
3827 * handles the case where compute block and reconstruct are requested
3828 * simultaneously. If this is not the case then new writes need to be
3829 * held off until the compute completes.
3831 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
3832 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
3833 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
3834 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
3838 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
3839 struct stripe_head_state
*s
, int disks
)
3841 struct r5dev
*dev
= NULL
;
3843 BUG_ON(sh
->batch_head
);
3844 set_bit(STRIPE_HANDLE
, &sh
->state
);
3846 switch (sh
->check_state
) {
3847 case check_state_idle
:
3848 /* start a new check operation if there are no failures */
3849 if (s
->failed
== 0) {
3850 BUG_ON(s
->uptodate
!= disks
);
3851 sh
->check_state
= check_state_run
;
3852 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3853 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3857 dev
= &sh
->dev
[s
->failed_num
[0]];
3859 case check_state_compute_result
:
3860 sh
->check_state
= check_state_idle
;
3862 dev
= &sh
->dev
[sh
->pd_idx
];
3864 /* check that a write has not made the stripe insync */
3865 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3868 /* either failed parity check, or recovery is happening */
3869 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
3870 BUG_ON(s
->uptodate
!= disks
);
3872 set_bit(R5_LOCKED
, &dev
->flags
);
3874 set_bit(R5_Wantwrite
, &dev
->flags
);
3876 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3877 set_bit(STRIPE_INSYNC
, &sh
->state
);
3879 case check_state_run
:
3880 break; /* we will be called again upon completion */
3881 case check_state_check_result
:
3882 sh
->check_state
= check_state_idle
;
3884 /* if a failure occurred during the check operation, leave
3885 * STRIPE_INSYNC not set and let the stripe be handled again
3890 /* handle a successful check operation, if parity is correct
3891 * we are done. Otherwise update the mismatch count and repair
3892 * parity if !MD_RECOVERY_CHECK
3894 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
3895 /* parity is correct (on disc,
3896 * not in buffer any more)
3898 set_bit(STRIPE_INSYNC
, &sh
->state
);
3900 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3901 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3902 /* don't try to repair!! */
3903 set_bit(STRIPE_INSYNC
, &sh
->state
);
3905 sh
->check_state
= check_state_compute_run
;
3906 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3907 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3908 set_bit(R5_Wantcompute
,
3909 &sh
->dev
[sh
->pd_idx
].flags
);
3910 sh
->ops
.target
= sh
->pd_idx
;
3911 sh
->ops
.target2
= -1;
3916 case check_state_compute_run
:
3919 pr_err("%s: unknown check_state: %d sector: %llu\n",
3920 __func__
, sh
->check_state
,
3921 (unsigned long long) sh
->sector
);
3926 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
3927 struct stripe_head_state
*s
,
3930 int pd_idx
= sh
->pd_idx
;
3931 int qd_idx
= sh
->qd_idx
;
3934 BUG_ON(sh
->batch_head
);
3935 set_bit(STRIPE_HANDLE
, &sh
->state
);
3937 BUG_ON(s
->failed
> 2);
3939 /* Want to check and possibly repair P and Q.
3940 * However there could be one 'failed' device, in which
3941 * case we can only check one of them, possibly using the
3942 * other to generate missing data
3945 switch (sh
->check_state
) {
3946 case check_state_idle
:
3947 /* start a new check operation if there are < 2 failures */
3948 if (s
->failed
== s
->q_failed
) {
3949 /* The only possible failed device holds Q, so it
3950 * makes sense to check P (If anything else were failed,
3951 * we would have used P to recreate it).
3953 sh
->check_state
= check_state_run
;
3955 if (!s
->q_failed
&& s
->failed
< 2) {
3956 /* Q is not failed, and we didn't use it to generate
3957 * anything, so it makes sense to check it
3959 if (sh
->check_state
== check_state_run
)
3960 sh
->check_state
= check_state_run_pq
;
3962 sh
->check_state
= check_state_run_q
;
3965 /* discard potentially stale zero_sum_result */
3966 sh
->ops
.zero_sum_result
= 0;
3968 if (sh
->check_state
== check_state_run
) {
3969 /* async_xor_zero_sum destroys the contents of P */
3970 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3973 if (sh
->check_state
>= check_state_run
&&
3974 sh
->check_state
<= check_state_run_pq
) {
3975 /* async_syndrome_zero_sum preserves P and Q, so
3976 * no need to mark them !uptodate here
3978 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3982 /* we have 2-disk failure */
3983 BUG_ON(s
->failed
!= 2);
3985 case check_state_compute_result
:
3986 sh
->check_state
= check_state_idle
;
3988 /* check that a write has not made the stripe insync */
3989 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3992 /* now write out any block on a failed drive,
3993 * or P or Q if they were recomputed
3995 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
3996 if (s
->failed
== 2) {
3997 dev
= &sh
->dev
[s
->failed_num
[1]];
3999 set_bit(R5_LOCKED
, &dev
->flags
);
4000 set_bit(R5_Wantwrite
, &dev
->flags
);
4002 if (s
->failed
>= 1) {
4003 dev
= &sh
->dev
[s
->failed_num
[0]];
4005 set_bit(R5_LOCKED
, &dev
->flags
);
4006 set_bit(R5_Wantwrite
, &dev
->flags
);
4008 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
4009 dev
= &sh
->dev
[pd_idx
];
4011 set_bit(R5_LOCKED
, &dev
->flags
);
4012 set_bit(R5_Wantwrite
, &dev
->flags
);
4014 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
4015 dev
= &sh
->dev
[qd_idx
];
4017 set_bit(R5_LOCKED
, &dev
->flags
);
4018 set_bit(R5_Wantwrite
, &dev
->flags
);
4020 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
4022 set_bit(STRIPE_INSYNC
, &sh
->state
);
4024 case check_state_run
:
4025 case check_state_run_q
:
4026 case check_state_run_pq
:
4027 break; /* we will be called again upon completion */
4028 case check_state_check_result
:
4029 sh
->check_state
= check_state_idle
;
4031 /* handle a successful check operation, if parity is correct
4032 * we are done. Otherwise update the mismatch count and repair
4033 * parity if !MD_RECOVERY_CHECK
4035 if (sh
->ops
.zero_sum_result
== 0) {
4036 /* both parities are correct */
4038 set_bit(STRIPE_INSYNC
, &sh
->state
);
4040 /* in contrast to the raid5 case we can validate
4041 * parity, but still have a failure to write
4044 sh
->check_state
= check_state_compute_result
;
4045 /* Returning at this point means that we may go
4046 * off and bring p and/or q uptodate again so
4047 * we make sure to check zero_sum_result again
4048 * to verify if p or q need writeback
4052 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
4053 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
4054 /* don't try to repair!! */
4055 set_bit(STRIPE_INSYNC
, &sh
->state
);
4057 int *target
= &sh
->ops
.target
;
4059 sh
->ops
.target
= -1;
4060 sh
->ops
.target2
= -1;
4061 sh
->check_state
= check_state_compute_run
;
4062 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
4063 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
4064 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
4065 set_bit(R5_Wantcompute
,
4066 &sh
->dev
[pd_idx
].flags
);
4068 target
= &sh
->ops
.target2
;
4071 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
4072 set_bit(R5_Wantcompute
,
4073 &sh
->dev
[qd_idx
].flags
);
4080 case check_state_compute_run
:
4083 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4084 __func__
, sh
->check_state
,
4085 (unsigned long long) sh
->sector
);
4090 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
4094 /* We have read all the blocks in this stripe and now we need to
4095 * copy some of them into a target stripe for expand.
4097 struct dma_async_tx_descriptor
*tx
= NULL
;
4098 BUG_ON(sh
->batch_head
);
4099 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4100 for (i
= 0; i
< sh
->disks
; i
++)
4101 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
4103 struct stripe_head
*sh2
;
4104 struct async_submit_ctl submit
;
4106 sector_t bn
= raid5_compute_blocknr(sh
, i
, 1);
4107 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
4109 sh2
= raid5_get_active_stripe(conf
, s
, 0, 1, 1);
4111 /* so far only the early blocks of this stripe
4112 * have been requested. When later blocks
4113 * get requested, we will try again
4116 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
4117 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
4118 /* must have already done this block */
4119 raid5_release_stripe(sh2
);
4123 /* place all the copies on one channel */
4124 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
4125 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
4126 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
4129 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
4130 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
4131 for (j
= 0; j
< conf
->raid_disks
; j
++)
4132 if (j
!= sh2
->pd_idx
&&
4134 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
4136 if (j
== conf
->raid_disks
) {
4137 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
4138 set_bit(STRIPE_HANDLE
, &sh2
->state
);
4140 raid5_release_stripe(sh2
);
4143 /* done submitting copies, wait for them to complete */
4144 async_tx_quiesce(&tx
);
4148 * handle_stripe - do things to a stripe.
4150 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4151 * state of various bits to see what needs to be done.
4153 * return some read requests which now have data
4154 * return some write requests which are safely on storage
4155 * schedule a read on some buffers
4156 * schedule a write of some buffers
4157 * return confirmation of parity correctness
4161 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
4163 struct r5conf
*conf
= sh
->raid_conf
;
4164 int disks
= sh
->disks
;
4167 int do_recovery
= 0;
4169 memset(s
, 0, sizeof(*s
));
4171 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
) && !sh
->batch_head
;
4172 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
) && !sh
->batch_head
;
4173 s
->failed_num
[0] = -1;
4174 s
->failed_num
[1] = -1;
4175 s
->log_failed
= r5l_log_disk_error(conf
);
4177 /* Now to look around and see what can be done */
4179 for (i
=disks
; i
--; ) {
4180 struct md_rdev
*rdev
;
4187 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4189 dev
->toread
, dev
->towrite
, dev
->written
);
4190 /* maybe we can reply to a read
4192 * new wantfill requests are only permitted while
4193 * ops_complete_biofill is guaranteed to be inactive
4195 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
4196 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
4197 set_bit(R5_Wantfill
, &dev
->flags
);
4199 /* now count some things */
4200 if (test_bit(R5_LOCKED
, &dev
->flags
))
4202 if (test_bit(R5_UPTODATE
, &dev
->flags
))
4204 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
4206 BUG_ON(s
->compute
> 2);
4209 if (test_bit(R5_Wantfill
, &dev
->flags
))
4211 else if (dev
->toread
)
4215 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
4220 /* Prefer to use the replacement for reads, but only
4221 * if it is recovered enough and has no bad blocks.
4223 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
4224 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
4225 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
4226 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4227 &first_bad
, &bad_sectors
))
4228 set_bit(R5_ReadRepl
, &dev
->flags
);
4230 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4231 set_bit(R5_NeedReplace
, &dev
->flags
);
4233 clear_bit(R5_NeedReplace
, &dev
->flags
);
4234 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
4235 clear_bit(R5_ReadRepl
, &dev
->flags
);
4237 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
4240 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4241 &first_bad
, &bad_sectors
);
4242 if (s
->blocked_rdev
== NULL
4243 && (test_bit(Blocked
, &rdev
->flags
)
4246 set_bit(BlockedBadBlocks
,
4248 s
->blocked_rdev
= rdev
;
4249 atomic_inc(&rdev
->nr_pending
);
4252 clear_bit(R5_Insync
, &dev
->flags
);
4256 /* also not in-sync */
4257 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
4258 test_bit(R5_UPTODATE
, &dev
->flags
)) {
4259 /* treat as in-sync, but with a read error
4260 * which we can now try to correct
4262 set_bit(R5_Insync
, &dev
->flags
);
4263 set_bit(R5_ReadError
, &dev
->flags
);
4265 } else if (test_bit(In_sync
, &rdev
->flags
))
4266 set_bit(R5_Insync
, &dev
->flags
);
4267 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
4268 /* in sync if before recovery_offset */
4269 set_bit(R5_Insync
, &dev
->flags
);
4270 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
4271 test_bit(R5_Expanded
, &dev
->flags
))
4272 /* If we've reshaped into here, we assume it is Insync.
4273 * We will shortly update recovery_offset to make
4276 set_bit(R5_Insync
, &dev
->flags
);
4278 if (test_bit(R5_WriteError
, &dev
->flags
)) {
4279 /* This flag does not apply to '.replacement'
4280 * only to .rdev, so make sure to check that*/
4281 struct md_rdev
*rdev2
= rcu_dereference(
4282 conf
->disks
[i
].rdev
);
4284 clear_bit(R5_Insync
, &dev
->flags
);
4285 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4286 s
->handle_bad_blocks
= 1;
4287 atomic_inc(&rdev2
->nr_pending
);
4289 clear_bit(R5_WriteError
, &dev
->flags
);
4291 if (test_bit(R5_MadeGood
, &dev
->flags
)) {
4292 /* This flag does not apply to '.replacement'
4293 * only to .rdev, so make sure to check that*/
4294 struct md_rdev
*rdev2
= rcu_dereference(
4295 conf
->disks
[i
].rdev
);
4296 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4297 s
->handle_bad_blocks
= 1;
4298 atomic_inc(&rdev2
->nr_pending
);
4300 clear_bit(R5_MadeGood
, &dev
->flags
);
4302 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4303 struct md_rdev
*rdev2
= rcu_dereference(
4304 conf
->disks
[i
].replacement
);
4305 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4306 s
->handle_bad_blocks
= 1;
4307 atomic_inc(&rdev2
->nr_pending
);
4309 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
4311 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4312 /* The ReadError flag will just be confusing now */
4313 clear_bit(R5_ReadError
, &dev
->flags
);
4314 clear_bit(R5_ReWrite
, &dev
->flags
);
4316 if (test_bit(R5_ReadError
, &dev
->flags
))
4317 clear_bit(R5_Insync
, &dev
->flags
);
4318 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4320 s
->failed_num
[s
->failed
] = i
;
4322 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4326 if (test_bit(R5_InJournal
, &dev
->flags
))
4328 if (test_bit(R5_InJournal
, &dev
->flags
) && dev
->written
)
4331 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
4332 /* If there is a failed device being replaced,
4333 * we must be recovering.
4334 * else if we are after recovery_cp, we must be syncing
4335 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4336 * else we can only be replacing
4337 * sync and recovery both need to read all devices, and so
4338 * use the same flag.
4341 sh
->sector
>= conf
->mddev
->recovery_cp
||
4342 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
4350 static int clear_batch_ready(struct stripe_head
*sh
)
4352 /* Return '1' if this is a member of batch, or
4353 * '0' if it is a lone stripe or a head which can now be
4356 struct stripe_head
*tmp
;
4357 if (!test_and_clear_bit(STRIPE_BATCH_READY
, &sh
->state
))
4358 return (sh
->batch_head
&& sh
->batch_head
!= sh
);
4359 spin_lock(&sh
->stripe_lock
);
4360 if (!sh
->batch_head
) {
4361 spin_unlock(&sh
->stripe_lock
);
4366 * this stripe could be added to a batch list before we check
4367 * BATCH_READY, skips it
4369 if (sh
->batch_head
!= sh
) {
4370 spin_unlock(&sh
->stripe_lock
);
4373 spin_lock(&sh
->batch_lock
);
4374 list_for_each_entry(tmp
, &sh
->batch_list
, batch_list
)
4375 clear_bit(STRIPE_BATCH_READY
, &tmp
->state
);
4376 spin_unlock(&sh
->batch_lock
);
4377 spin_unlock(&sh
->stripe_lock
);
4380 * BATCH_READY is cleared, no new stripes can be added.
4381 * batch_list can be accessed without lock
4386 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
4387 unsigned long handle_flags
)
4389 struct stripe_head
*sh
, *next
;
4393 list_for_each_entry_safe(sh
, next
, &head_sh
->batch_list
, batch_list
) {
4395 list_del_init(&sh
->batch_list
);
4397 WARN_ONCE(sh
->state
& ((1 << STRIPE_ACTIVE
) |
4398 (1 << STRIPE_SYNCING
) |
4399 (1 << STRIPE_REPLACED
) |
4400 (1 << STRIPE_DELAYED
) |
4401 (1 << STRIPE_BIT_DELAY
) |
4402 (1 << STRIPE_FULL_WRITE
) |
4403 (1 << STRIPE_BIOFILL_RUN
) |
4404 (1 << STRIPE_COMPUTE_RUN
) |
4405 (1 << STRIPE_OPS_REQ_PENDING
) |
4406 (1 << STRIPE_DISCARD
) |
4407 (1 << STRIPE_BATCH_READY
) |
4408 (1 << STRIPE_BATCH_ERR
) |
4409 (1 << STRIPE_BITMAP_PENDING
)),
4410 "stripe state: %lx\n", sh
->state
);
4411 WARN_ONCE(head_sh
->state
& ((1 << STRIPE_DISCARD
) |
4412 (1 << STRIPE_REPLACED
)),
4413 "head stripe state: %lx\n", head_sh
->state
);
4415 set_mask_bits(&sh
->state
, ~(STRIPE_EXPAND_SYNC_FLAGS
|
4416 (1 << STRIPE_PREREAD_ACTIVE
) |
4417 (1 << STRIPE_DEGRADED
)),
4418 head_sh
->state
& (1 << STRIPE_INSYNC
));
4420 sh
->check_state
= head_sh
->check_state
;
4421 sh
->reconstruct_state
= head_sh
->reconstruct_state
;
4422 for (i
= 0; i
< sh
->disks
; i
++) {
4423 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
4425 sh
->dev
[i
].flags
= head_sh
->dev
[i
].flags
&
4426 (~((1 << R5_WriteError
) | (1 << R5_Overlap
)));
4428 spin_lock_irq(&sh
->stripe_lock
);
4429 sh
->batch_head
= NULL
;
4430 spin_unlock_irq(&sh
->stripe_lock
);
4431 if (handle_flags
== 0 ||
4432 sh
->state
& handle_flags
)
4433 set_bit(STRIPE_HANDLE
, &sh
->state
);
4434 raid5_release_stripe(sh
);
4436 spin_lock_irq(&head_sh
->stripe_lock
);
4437 head_sh
->batch_head
= NULL
;
4438 spin_unlock_irq(&head_sh
->stripe_lock
);
4439 for (i
= 0; i
< head_sh
->disks
; i
++)
4440 if (test_and_clear_bit(R5_Overlap
, &head_sh
->dev
[i
].flags
))
4442 if (head_sh
->state
& handle_flags
)
4443 set_bit(STRIPE_HANDLE
, &head_sh
->state
);
4446 wake_up(&head_sh
->raid_conf
->wait_for_overlap
);
4449 static void handle_stripe(struct stripe_head
*sh
)
4451 struct stripe_head_state s
;
4452 struct r5conf
*conf
= sh
->raid_conf
;
4455 int disks
= sh
->disks
;
4456 struct r5dev
*pdev
, *qdev
;
4458 clear_bit(STRIPE_HANDLE
, &sh
->state
);
4459 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
4460 /* already being handled, ensure it gets handled
4461 * again when current action finishes */
4462 set_bit(STRIPE_HANDLE
, &sh
->state
);
4466 if (clear_batch_ready(sh
) ) {
4467 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4471 if (test_and_clear_bit(STRIPE_BATCH_ERR
, &sh
->state
))
4472 break_stripe_batch_list(sh
, 0);
4474 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
) && !sh
->batch_head
) {
4475 spin_lock(&sh
->stripe_lock
);
4476 /* Cannot process 'sync' concurrently with 'discard' */
4477 if (!test_bit(STRIPE_DISCARD
, &sh
->state
) &&
4478 test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
4479 set_bit(STRIPE_SYNCING
, &sh
->state
);
4480 clear_bit(STRIPE_INSYNC
, &sh
->state
);
4481 clear_bit(STRIPE_REPLACED
, &sh
->state
);
4483 spin_unlock(&sh
->stripe_lock
);
4485 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4487 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4488 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4489 (unsigned long long)sh
->sector
, sh
->state
,
4490 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
4491 sh
->check_state
, sh
->reconstruct_state
);
4493 analyse_stripe(sh
, &s
);
4495 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
))
4498 if (s
.handle_bad_blocks
) {
4499 set_bit(STRIPE_HANDLE
, &sh
->state
);
4503 if (unlikely(s
.blocked_rdev
)) {
4504 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
4505 s
.replacing
|| s
.to_write
|| s
.written
) {
4506 set_bit(STRIPE_HANDLE
, &sh
->state
);
4509 /* There is nothing for the blocked_rdev to block */
4510 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
4511 s
.blocked_rdev
= NULL
;
4514 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
4515 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
4516 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
4519 pr_debug("locked=%d uptodate=%d to_read=%d"
4520 " to_write=%d failed=%d failed_num=%d,%d\n",
4521 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
4522 s
.failed_num
[0], s
.failed_num
[1]);
4523 /* check if the array has lost more than max_degraded devices and,
4524 * if so, some requests might need to be failed.
4526 if (s
.failed
> conf
->max_degraded
|| s
.log_failed
) {
4527 sh
->check_state
= 0;
4528 sh
->reconstruct_state
= 0;
4529 break_stripe_batch_list(sh
, 0);
4530 if (s
.to_read
+s
.to_write
+s
.written
)
4531 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
4532 if (s
.syncing
+ s
.replacing
)
4533 handle_failed_sync(conf
, sh
, &s
);
4536 /* Now we check to see if any write operations have recently
4540 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
4542 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
4543 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
4544 sh
->reconstruct_state
= reconstruct_state_idle
;
4546 /* All the 'written' buffers and the parity block are ready to
4547 * be written back to disk
4549 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
4550 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
4551 BUG_ON(sh
->qd_idx
>= 0 &&
4552 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
4553 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
4554 for (i
= disks
; i
--; ) {
4555 struct r5dev
*dev
= &sh
->dev
[i
];
4556 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
4557 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
4558 dev
->written
|| test_bit(R5_InJournal
,
4560 pr_debug("Writing block %d\n", i
);
4561 set_bit(R5_Wantwrite
, &dev
->flags
);
4566 if (!test_bit(R5_Insync
, &dev
->flags
) ||
4567 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
4569 set_bit(STRIPE_INSYNC
, &sh
->state
);
4572 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4573 s
.dec_preread_active
= 1;
4577 * might be able to return some write requests if the parity blocks
4578 * are safe, or on a failed drive
4580 pdev
= &sh
->dev
[sh
->pd_idx
];
4581 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
4582 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
4583 qdev
= &sh
->dev
[sh
->qd_idx
];
4584 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
4585 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
4589 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
4590 && !test_bit(R5_LOCKED
, &pdev
->flags
)
4591 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
4592 test_bit(R5_Discard
, &pdev
->flags
))))) &&
4593 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
4594 && !test_bit(R5_LOCKED
, &qdev
->flags
)
4595 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
4596 test_bit(R5_Discard
, &qdev
->flags
))))))
4597 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
4600 r5c_handle_cached_data_endio(conf
, sh
, disks
, &s
.return_bi
);
4601 r5l_stripe_write_finished(sh
);
4603 /* Now we might consider reading some blocks, either to check/generate
4604 * parity, or to satisfy requests
4605 * or to load a block that is being partially written.
4607 if (s
.to_read
|| s
.non_overwrite
4608 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
4609 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
4612 handle_stripe_fill(sh
, &s
, disks
);
4615 * When the stripe finishes full journal write cycle (write to journal
4616 * and raid disk), this is the clean up procedure so it is ready for
4619 r5c_finish_stripe_write_out(conf
, sh
, &s
);
4622 * Now to consider new write requests, cache write back and what else,
4623 * if anything should be read. We do not handle new writes when:
4624 * 1/ A 'write' operation (copy+xor) is already in flight.
4625 * 2/ A 'check' operation is in flight, as it may clobber the parity
4627 * 3/ A r5c cache log write is in flight.
4630 if (!sh
->reconstruct_state
&& !sh
->check_state
&& !sh
->log_io
) {
4631 if (!r5c_is_writeback(conf
->log
)) {
4633 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
4634 } else { /* write back cache */
4637 /* First, try handle writes in caching phase */
4639 ret
= r5c_try_caching_write(conf
, sh
, &s
,
4642 * If caching phase failed: ret == -EAGAIN
4644 * stripe under reclaim: !caching && injournal
4646 * fall back to handle_stripe_dirtying()
4648 if (ret
== -EAGAIN
||
4649 /* stripe under reclaim: !caching && injournal */
4650 (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
) &&
4652 ret
= handle_stripe_dirtying(conf
, sh
, &s
,
4660 /* maybe we need to check and possibly fix the parity for this stripe
4661 * Any reads will already have been scheduled, so we just see if enough
4662 * data is available. The parity check is held off while parity
4663 * dependent operations are in flight.
4665 if (sh
->check_state
||
4666 (s
.syncing
&& s
.locked
== 0 &&
4667 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4668 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
4669 if (conf
->level
== 6)
4670 handle_parity_checks6(conf
, sh
, &s
, disks
);
4672 handle_parity_checks5(conf
, sh
, &s
, disks
);
4675 if ((s
.replacing
|| s
.syncing
) && s
.locked
== 0
4676 && !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)
4677 && !test_bit(STRIPE_REPLACED
, &sh
->state
)) {
4678 /* Write out to replacement devices where possible */
4679 for (i
= 0; i
< conf
->raid_disks
; i
++)
4680 if (test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
4681 WARN_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
4682 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
4683 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4687 set_bit(STRIPE_INSYNC
, &sh
->state
);
4688 set_bit(STRIPE_REPLACED
, &sh
->state
);
4690 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
4691 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4692 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
4693 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4694 clear_bit(STRIPE_SYNCING
, &sh
->state
);
4695 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
4696 wake_up(&conf
->wait_for_overlap
);
4699 /* If the failed drives are just a ReadError, then we might need
4700 * to progress the repair/check process
4702 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
4703 for (i
= 0; i
< s
.failed
; i
++) {
4704 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
4705 if (test_bit(R5_ReadError
, &dev
->flags
)
4706 && !test_bit(R5_LOCKED
, &dev
->flags
)
4707 && test_bit(R5_UPTODATE
, &dev
->flags
)
4709 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
4710 set_bit(R5_Wantwrite
, &dev
->flags
);
4711 set_bit(R5_ReWrite
, &dev
->flags
);
4712 set_bit(R5_LOCKED
, &dev
->flags
);
4715 /* let's read it back */
4716 set_bit(R5_Wantread
, &dev
->flags
);
4717 set_bit(R5_LOCKED
, &dev
->flags
);
4723 /* Finish reconstruct operations initiated by the expansion process */
4724 if (sh
->reconstruct_state
== reconstruct_state_result
) {
4725 struct stripe_head
*sh_src
4726 = raid5_get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
4727 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
4728 /* sh cannot be written until sh_src has been read.
4729 * so arrange for sh to be delayed a little
4731 set_bit(STRIPE_DELAYED
, &sh
->state
);
4732 set_bit(STRIPE_HANDLE
, &sh
->state
);
4733 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
4735 atomic_inc(&conf
->preread_active_stripes
);
4736 raid5_release_stripe(sh_src
);
4740 raid5_release_stripe(sh_src
);
4742 sh
->reconstruct_state
= reconstruct_state_idle
;
4743 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
4744 for (i
= conf
->raid_disks
; i
--; ) {
4745 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
4746 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4751 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
4752 !sh
->reconstruct_state
) {
4753 /* Need to write out all blocks after computing parity */
4754 sh
->disks
= conf
->raid_disks
;
4755 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
4756 schedule_reconstruction(sh
, &s
, 1, 1);
4757 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
4758 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4759 atomic_dec(&conf
->reshape_stripes
);
4760 wake_up(&conf
->wait_for_overlap
);
4761 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4764 if (s
.expanding
&& s
.locked
== 0 &&
4765 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
4766 handle_stripe_expansion(conf
, sh
);
4769 /* wait for this device to become unblocked */
4770 if (unlikely(s
.blocked_rdev
)) {
4771 if (conf
->mddev
->external
)
4772 md_wait_for_blocked_rdev(s
.blocked_rdev
,
4775 /* Internal metadata will immediately
4776 * be written by raid5d, so we don't
4777 * need to wait here.
4779 rdev_dec_pending(s
.blocked_rdev
,
4783 if (s
.handle_bad_blocks
)
4784 for (i
= disks
; i
--; ) {
4785 struct md_rdev
*rdev
;
4786 struct r5dev
*dev
= &sh
->dev
[i
];
4787 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
4788 /* We own a safe reference to the rdev */
4789 rdev
= conf
->disks
[i
].rdev
;
4790 if (!rdev_set_badblocks(rdev
, sh
->sector
,
4792 md_error(conf
->mddev
, rdev
);
4793 rdev_dec_pending(rdev
, conf
->mddev
);
4795 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
4796 rdev
= conf
->disks
[i
].rdev
;
4797 rdev_clear_badblocks(rdev
, sh
->sector
,
4799 rdev_dec_pending(rdev
, conf
->mddev
);
4801 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4802 rdev
= conf
->disks
[i
].replacement
;
4804 /* rdev have been moved down */
4805 rdev
= conf
->disks
[i
].rdev
;
4806 rdev_clear_badblocks(rdev
, sh
->sector
,
4808 rdev_dec_pending(rdev
, conf
->mddev
);
4813 raid_run_ops(sh
, s
.ops_request
);
4817 if (s
.dec_preread_active
) {
4818 /* We delay this until after ops_run_io so that if make_request
4819 * is waiting on a flush, it won't continue until the writes
4820 * have actually been submitted.
4822 atomic_dec(&conf
->preread_active_stripes
);
4823 if (atomic_read(&conf
->preread_active_stripes
) <
4825 md_wakeup_thread(conf
->mddev
->thread
);
4828 if (!bio_list_empty(&s
.return_bi
)) {
4829 if (test_bit(MD_SB_CHANGE_PENDING
, &conf
->mddev
->sb_flags
)) {
4830 spin_lock_irq(&conf
->device_lock
);
4831 bio_list_merge(&conf
->return_bi
, &s
.return_bi
);
4832 spin_unlock_irq(&conf
->device_lock
);
4833 md_wakeup_thread(conf
->mddev
->thread
);
4835 return_io(&s
.return_bi
);
4838 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4841 static void raid5_activate_delayed(struct r5conf
*conf
)
4843 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
4844 while (!list_empty(&conf
->delayed_list
)) {
4845 struct list_head
*l
= conf
->delayed_list
.next
;
4846 struct stripe_head
*sh
;
4847 sh
= list_entry(l
, struct stripe_head
, lru
);
4849 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4850 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4851 atomic_inc(&conf
->preread_active_stripes
);
4852 list_add_tail(&sh
->lru
, &conf
->hold_list
);
4853 raid5_wakeup_stripe_thread(sh
);
4858 static void activate_bit_delay(struct r5conf
*conf
,
4859 struct list_head
*temp_inactive_list
)
4861 /* device_lock is held */
4862 struct list_head head
;
4863 list_add(&head
, &conf
->bitmap_list
);
4864 list_del_init(&conf
->bitmap_list
);
4865 while (!list_empty(&head
)) {
4866 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
4868 list_del_init(&sh
->lru
);
4869 atomic_inc(&sh
->count
);
4870 hash
= sh
->hash_lock_index
;
4871 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
4875 static int raid5_congested(struct mddev
*mddev
, int bits
)
4877 struct r5conf
*conf
= mddev
->private;
4879 /* No difference between reads and writes. Just check
4880 * how busy the stripe_cache is
4883 if (test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
))
4886 /* Also checks whether there is pressure on r5cache log space */
4887 if (test_bit(R5C_LOG_TIGHT
, &conf
->cache_state
))
4891 if (atomic_read(&conf
->empty_inactive_list_nr
))
4897 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
4899 struct r5conf
*conf
= mddev
->private;
4900 sector_t sector
= bio
->bi_iter
.bi_sector
+ get_start_sect(bio
->bi_bdev
);
4901 unsigned int chunk_sectors
;
4902 unsigned int bio_sectors
= bio_sectors(bio
);
4904 chunk_sectors
= min(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
4905 return chunk_sectors
>=
4906 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
4910 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4911 * later sampled by raid5d.
4913 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
4915 unsigned long flags
;
4917 spin_lock_irqsave(&conf
->device_lock
, flags
);
4919 bi
->bi_next
= conf
->retry_read_aligned_list
;
4920 conf
->retry_read_aligned_list
= bi
;
4922 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4923 md_wakeup_thread(conf
->mddev
->thread
);
4926 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
4930 bi
= conf
->retry_read_aligned
;
4932 conf
->retry_read_aligned
= NULL
;
4935 bi
= conf
->retry_read_aligned_list
;
4937 conf
->retry_read_aligned_list
= bi
->bi_next
;
4940 * this sets the active strip count to 1 and the processed
4941 * strip count to zero (upper 8 bits)
4943 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
4950 * The "raid5_align_endio" should check if the read succeeded and if it
4951 * did, call bio_endio on the original bio (having bio_put the new bio
4953 * If the read failed..
4955 static void raid5_align_endio(struct bio
*bi
)
4957 struct bio
* raid_bi
= bi
->bi_private
;
4958 struct mddev
*mddev
;
4959 struct r5conf
*conf
;
4960 struct md_rdev
*rdev
;
4961 int error
= bi
->bi_error
;
4965 rdev
= (void*)raid_bi
->bi_next
;
4966 raid_bi
->bi_next
= NULL
;
4967 mddev
= rdev
->mddev
;
4968 conf
= mddev
->private;
4970 rdev_dec_pending(rdev
, conf
->mddev
);
4973 trace_block_bio_complete(bdev_get_queue(raid_bi
->bi_bdev
),
4976 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4977 wake_up(&conf
->wait_for_quiescent
);
4981 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
4983 add_bio_to_retry(raid_bi
, conf
);
4986 static int raid5_read_one_chunk(struct mddev
*mddev
, struct bio
*raid_bio
)
4988 struct r5conf
*conf
= mddev
->private;
4990 struct bio
* align_bi
;
4991 struct md_rdev
*rdev
;
4992 sector_t end_sector
;
4994 if (!in_chunk_boundary(mddev
, raid_bio
)) {
4995 pr_debug("%s: non aligned\n", __func__
);
4999 * use bio_clone_mddev to make a copy of the bio
5001 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
5005 * set bi_end_io to a new function, and set bi_private to the
5008 align_bi
->bi_end_io
= raid5_align_endio
;
5009 align_bi
->bi_private
= raid_bio
;
5013 align_bi
->bi_iter
.bi_sector
=
5014 raid5_compute_sector(conf
, raid_bio
->bi_iter
.bi_sector
,
5017 end_sector
= bio_end_sector(align_bi
);
5019 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
5020 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
5021 rdev
->recovery_offset
< end_sector
) {
5022 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
5024 (test_bit(Faulty
, &rdev
->flags
) ||
5025 !(test_bit(In_sync
, &rdev
->flags
) ||
5026 rdev
->recovery_offset
>= end_sector
)))
5033 atomic_inc(&rdev
->nr_pending
);
5035 raid_bio
->bi_next
= (void*)rdev
;
5036 align_bi
->bi_bdev
= rdev
->bdev
;
5037 bio_clear_flag(align_bi
, BIO_SEG_VALID
);
5039 if (is_badblock(rdev
, align_bi
->bi_iter
.bi_sector
,
5040 bio_sectors(align_bi
),
5041 &first_bad
, &bad_sectors
)) {
5043 rdev_dec_pending(rdev
, mddev
);
5047 /* No reshape active, so we can trust rdev->data_offset */
5048 align_bi
->bi_iter
.bi_sector
+= rdev
->data_offset
;
5050 spin_lock_irq(&conf
->device_lock
);
5051 wait_event_lock_irq(conf
->wait_for_quiescent
,
5054 atomic_inc(&conf
->active_aligned_reads
);
5055 spin_unlock_irq(&conf
->device_lock
);
5058 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
5059 align_bi
, disk_devt(mddev
->gendisk
),
5060 raid_bio
->bi_iter
.bi_sector
);
5061 generic_make_request(align_bi
);
5070 static struct bio
*chunk_aligned_read(struct mddev
*mddev
, struct bio
*raid_bio
)
5075 sector_t sector
= raid_bio
->bi_iter
.bi_sector
;
5076 unsigned chunk_sects
= mddev
->chunk_sectors
;
5077 unsigned sectors
= chunk_sects
- (sector
& (chunk_sects
-1));
5079 if (sectors
< bio_sectors(raid_bio
)) {
5080 split
= bio_split(raid_bio
, sectors
, GFP_NOIO
, fs_bio_set
);
5081 bio_chain(split
, raid_bio
);
5085 if (!raid5_read_one_chunk(mddev
, split
)) {
5086 if (split
!= raid_bio
)
5087 generic_make_request(raid_bio
);
5090 } while (split
!= raid_bio
);
5095 /* __get_priority_stripe - get the next stripe to process
5097 * Full stripe writes are allowed to pass preread active stripes up until
5098 * the bypass_threshold is exceeded. In general the bypass_count
5099 * increments when the handle_list is handled before the hold_list; however, it
5100 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5101 * stripe with in flight i/o. The bypass_count will be reset when the
5102 * head of the hold_list has changed, i.e. the head was promoted to the
5105 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
, int group
)
5107 struct stripe_head
*sh
= NULL
, *tmp
;
5108 struct list_head
*handle_list
= NULL
;
5109 struct r5worker_group
*wg
= NULL
;
5111 if (conf
->worker_cnt_per_group
== 0) {
5112 handle_list
= &conf
->handle_list
;
5113 } else if (group
!= ANY_GROUP
) {
5114 handle_list
= &conf
->worker_groups
[group
].handle_list
;
5115 wg
= &conf
->worker_groups
[group
];
5118 for (i
= 0; i
< conf
->group_cnt
; i
++) {
5119 handle_list
= &conf
->worker_groups
[i
].handle_list
;
5120 wg
= &conf
->worker_groups
[i
];
5121 if (!list_empty(handle_list
))
5126 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5128 list_empty(handle_list
) ? "empty" : "busy",
5129 list_empty(&conf
->hold_list
) ? "empty" : "busy",
5130 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
5132 if (!list_empty(handle_list
)) {
5133 sh
= list_entry(handle_list
->next
, typeof(*sh
), lru
);
5135 if (list_empty(&conf
->hold_list
))
5136 conf
->bypass_count
= 0;
5137 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
5138 if (conf
->hold_list
.next
== conf
->last_hold
)
5139 conf
->bypass_count
++;
5141 conf
->last_hold
= conf
->hold_list
.next
;
5142 conf
->bypass_count
-= conf
->bypass_threshold
;
5143 if (conf
->bypass_count
< 0)
5144 conf
->bypass_count
= 0;
5147 } else if (!list_empty(&conf
->hold_list
) &&
5148 ((conf
->bypass_threshold
&&
5149 conf
->bypass_count
> conf
->bypass_threshold
) ||
5150 atomic_read(&conf
->pending_full_writes
) == 0)) {
5152 list_for_each_entry(tmp
, &conf
->hold_list
, lru
) {
5153 if (conf
->worker_cnt_per_group
== 0 ||
5154 group
== ANY_GROUP
||
5155 !cpu_online(tmp
->cpu
) ||
5156 cpu_to_group(tmp
->cpu
) == group
) {
5163 conf
->bypass_count
-= conf
->bypass_threshold
;
5164 if (conf
->bypass_count
< 0)
5165 conf
->bypass_count
= 0;
5177 list_del_init(&sh
->lru
);
5178 BUG_ON(atomic_inc_return(&sh
->count
) != 1);
5182 struct raid5_plug_cb
{
5183 struct blk_plug_cb cb
;
5184 struct list_head list
;
5185 struct list_head temp_inactive_list
[NR_STRIPE_HASH_LOCKS
];
5188 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
5190 struct raid5_plug_cb
*cb
= container_of(
5191 blk_cb
, struct raid5_plug_cb
, cb
);
5192 struct stripe_head
*sh
;
5193 struct mddev
*mddev
= cb
->cb
.data
;
5194 struct r5conf
*conf
= mddev
->private;
5198 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
5199 spin_lock_irq(&conf
->device_lock
);
5200 while (!list_empty(&cb
->list
)) {
5201 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
5202 list_del_init(&sh
->lru
);
5204 * avoid race release_stripe_plug() sees
5205 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5206 * is still in our list
5208 smp_mb__before_atomic();
5209 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
5211 * STRIPE_ON_RELEASE_LIST could be set here. In that
5212 * case, the count is always > 1 here
5214 hash
= sh
->hash_lock_index
;
5215 __release_stripe(conf
, sh
, &cb
->temp_inactive_list
[hash
]);
5218 spin_unlock_irq(&conf
->device_lock
);
5220 release_inactive_stripe_list(conf
, cb
->temp_inactive_list
,
5221 NR_STRIPE_HASH_LOCKS
);
5223 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
5227 static void release_stripe_plug(struct mddev
*mddev
,
5228 struct stripe_head
*sh
)
5230 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
5231 raid5_unplug
, mddev
,
5232 sizeof(struct raid5_plug_cb
));
5233 struct raid5_plug_cb
*cb
;
5236 raid5_release_stripe(sh
);
5240 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
5242 if (cb
->list
.next
== NULL
) {
5244 INIT_LIST_HEAD(&cb
->list
);
5245 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5246 INIT_LIST_HEAD(cb
->temp_inactive_list
+ i
);
5249 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
5250 list_add_tail(&sh
->lru
, &cb
->list
);
5252 raid5_release_stripe(sh
);
5255 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
5257 struct r5conf
*conf
= mddev
->private;
5258 sector_t logical_sector
, last_sector
;
5259 struct stripe_head
*sh
;
5263 if (mddev
->reshape_position
!= MaxSector
)
5264 /* Skip discard while reshape is happening */
5267 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5268 last_sector
= bi
->bi_iter
.bi_sector
+ (bi
->bi_iter
.bi_size
>>9);
5271 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5273 stripe_sectors
= conf
->chunk_sectors
*
5274 (conf
->raid_disks
- conf
->max_degraded
);
5275 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
5277 sector_div(last_sector
, stripe_sectors
);
5279 logical_sector
*= conf
->chunk_sectors
;
5280 last_sector
*= conf
->chunk_sectors
;
5282 for (; logical_sector
< last_sector
;
5283 logical_sector
+= STRIPE_SECTORS
) {
5287 sh
= raid5_get_active_stripe(conf
, logical_sector
, 0, 0, 0);
5288 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5289 TASK_UNINTERRUPTIBLE
);
5290 set_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5291 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
5292 raid5_release_stripe(sh
);
5296 clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5297 spin_lock_irq(&sh
->stripe_lock
);
5298 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5299 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5301 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
5302 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
5303 spin_unlock_irq(&sh
->stripe_lock
);
5304 raid5_release_stripe(sh
);
5309 set_bit(STRIPE_DISCARD
, &sh
->state
);
5310 finish_wait(&conf
->wait_for_overlap
, &w
);
5311 sh
->overwrite_disks
= 0;
5312 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5313 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5315 sh
->dev
[d
].towrite
= bi
;
5316 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
5317 raid5_inc_bi_active_stripes(bi
);
5318 sh
->overwrite_disks
++;
5320 spin_unlock_irq(&sh
->stripe_lock
);
5321 if (conf
->mddev
->bitmap
) {
5323 d
< conf
->raid_disks
- conf
->max_degraded
;
5325 bitmap_startwrite(mddev
->bitmap
,
5329 sh
->bm_seq
= conf
->seq_flush
+ 1;
5330 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
5333 set_bit(STRIPE_HANDLE
, &sh
->state
);
5334 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5335 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5336 atomic_inc(&conf
->preread_active_stripes
);
5337 release_stripe_plug(mddev
, sh
);
5340 remaining
= raid5_dec_bi_active_stripes(bi
);
5341 if (remaining
== 0) {
5342 md_write_end(mddev
);
5347 static void raid5_make_request(struct mddev
*mddev
, struct bio
* bi
)
5349 struct r5conf
*conf
= mddev
->private;
5351 sector_t new_sector
;
5352 sector_t logical_sector
, last_sector
;
5353 struct stripe_head
*sh
;
5354 const int rw
= bio_data_dir(bi
);
5358 bool do_flush
= false;
5360 if (unlikely(bi
->bi_opf
& REQ_PREFLUSH
)) {
5361 int ret
= r5l_handle_flush_request(conf
->log
, bi
);
5365 if (ret
== -ENODEV
) {
5366 md_flush_request(mddev
, bi
);
5369 /* ret == -EAGAIN, fallback */
5371 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5372 * we need to flush journal device
5374 do_flush
= bi
->bi_opf
& REQ_PREFLUSH
;
5377 md_write_start(mddev
, bi
);
5380 * If array is degraded, better not do chunk aligned read because
5381 * later we might have to read it again in order to reconstruct
5382 * data on failed drives.
5384 if (rw
== READ
&& mddev
->degraded
== 0 &&
5385 !r5c_is_writeback(conf
->log
) &&
5386 mddev
->reshape_position
== MaxSector
) {
5387 bi
= chunk_aligned_read(mddev
, bi
);
5392 if (unlikely(bio_op(bi
) == REQ_OP_DISCARD
)) {
5393 make_discard_request(mddev
, bi
);
5397 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5398 last_sector
= bio_end_sector(bi
);
5400 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5402 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
5403 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
5409 seq
= read_seqcount_begin(&conf
->gen_lock
);
5412 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5413 TASK_UNINTERRUPTIBLE
);
5414 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
5415 /* spinlock is needed as reshape_progress may be
5416 * 64bit on a 32bit platform, and so it might be
5417 * possible to see a half-updated value
5418 * Of course reshape_progress could change after
5419 * the lock is dropped, so once we get a reference
5420 * to the stripe that we think it is, we will have
5423 spin_lock_irq(&conf
->device_lock
);
5424 if (mddev
->reshape_backwards
5425 ? logical_sector
< conf
->reshape_progress
5426 : logical_sector
>= conf
->reshape_progress
) {
5429 if (mddev
->reshape_backwards
5430 ? logical_sector
< conf
->reshape_safe
5431 : logical_sector
>= conf
->reshape_safe
) {
5432 spin_unlock_irq(&conf
->device_lock
);
5438 spin_unlock_irq(&conf
->device_lock
);
5441 new_sector
= raid5_compute_sector(conf
, logical_sector
,
5444 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
5445 (unsigned long long)new_sector
,
5446 (unsigned long long)logical_sector
);
5448 sh
= raid5_get_active_stripe(conf
, new_sector
, previous
,
5449 (bi
->bi_opf
& REQ_RAHEAD
), 0);
5451 if (unlikely(previous
)) {
5452 /* expansion might have moved on while waiting for a
5453 * stripe, so we must do the range check again.
5454 * Expansion could still move past after this
5455 * test, but as we are holding a reference to
5456 * 'sh', we know that if that happens,
5457 * STRIPE_EXPANDING will get set and the expansion
5458 * won't proceed until we finish with the stripe.
5461 spin_lock_irq(&conf
->device_lock
);
5462 if (mddev
->reshape_backwards
5463 ? logical_sector
>= conf
->reshape_progress
5464 : logical_sector
< conf
->reshape_progress
)
5465 /* mismatch, need to try again */
5467 spin_unlock_irq(&conf
->device_lock
);
5469 raid5_release_stripe(sh
);
5475 if (read_seqcount_retry(&conf
->gen_lock
, seq
)) {
5476 /* Might have got the wrong stripe_head
5479 raid5_release_stripe(sh
);
5484 logical_sector
>= mddev
->suspend_lo
&&
5485 logical_sector
< mddev
->suspend_hi
) {
5486 raid5_release_stripe(sh
);
5487 /* As the suspend_* range is controlled by
5488 * userspace, we want an interruptible
5491 flush_signals(current
);
5492 prepare_to_wait(&conf
->wait_for_overlap
,
5493 &w
, TASK_INTERRUPTIBLE
);
5494 if (logical_sector
>= mddev
->suspend_lo
&&
5495 logical_sector
< mddev
->suspend_hi
) {
5502 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
5503 !add_stripe_bio(sh
, bi
, dd_idx
, rw
, previous
)) {
5504 /* Stripe is busy expanding or
5505 * add failed due to overlap. Flush everything
5508 md_wakeup_thread(mddev
->thread
);
5509 raid5_release_stripe(sh
);
5515 set_bit(STRIPE_R5C_PREFLUSH
, &sh
->state
);
5516 /* we only need flush for one stripe */
5520 set_bit(STRIPE_HANDLE
, &sh
->state
);
5521 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5522 if ((!sh
->batch_head
|| sh
== sh
->batch_head
) &&
5523 (bi
->bi_opf
& REQ_SYNC
) &&
5524 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5525 atomic_inc(&conf
->preread_active_stripes
);
5526 release_stripe_plug(mddev
, sh
);
5528 /* cannot get stripe for read-ahead, just give-up */
5529 bi
->bi_error
= -EIO
;
5533 finish_wait(&conf
->wait_for_overlap
, &w
);
5535 remaining
= raid5_dec_bi_active_stripes(bi
);
5536 if (remaining
== 0) {
5539 md_write_end(mddev
);
5541 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
5547 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
5549 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
5551 /* reshaping is quite different to recovery/resync so it is
5552 * handled quite separately ... here.
5554 * On each call to sync_request, we gather one chunk worth of
5555 * destination stripes and flag them as expanding.
5556 * Then we find all the source stripes and request reads.
5557 * As the reads complete, handle_stripe will copy the data
5558 * into the destination stripe and release that stripe.
5560 struct r5conf
*conf
= mddev
->private;
5561 struct stripe_head
*sh
;
5562 sector_t first_sector
, last_sector
;
5563 int raid_disks
= conf
->previous_raid_disks
;
5564 int data_disks
= raid_disks
- conf
->max_degraded
;
5565 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5568 sector_t writepos
, readpos
, safepos
;
5569 sector_t stripe_addr
;
5570 int reshape_sectors
;
5571 struct list_head stripes
;
5574 if (sector_nr
== 0) {
5575 /* If restarting in the middle, skip the initial sectors */
5576 if (mddev
->reshape_backwards
&&
5577 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
5578 sector_nr
= raid5_size(mddev
, 0, 0)
5579 - conf
->reshape_progress
;
5580 } else if (mddev
->reshape_backwards
&&
5581 conf
->reshape_progress
== MaxSector
) {
5582 /* shouldn't happen, but just in case, finish up.*/
5583 sector_nr
= MaxSector
;
5584 } else if (!mddev
->reshape_backwards
&&
5585 conf
->reshape_progress
> 0)
5586 sector_nr
= conf
->reshape_progress
;
5587 sector_div(sector_nr
, new_data_disks
);
5589 mddev
->curr_resync_completed
= sector_nr
;
5590 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5597 /* We need to process a full chunk at a time.
5598 * If old and new chunk sizes differ, we need to process the
5602 reshape_sectors
= max(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
5604 /* We update the metadata at least every 10 seconds, or when
5605 * the data about to be copied would over-write the source of
5606 * the data at the front of the range. i.e. one new_stripe
5607 * along from reshape_progress new_maps to after where
5608 * reshape_safe old_maps to
5610 writepos
= conf
->reshape_progress
;
5611 sector_div(writepos
, new_data_disks
);
5612 readpos
= conf
->reshape_progress
;
5613 sector_div(readpos
, data_disks
);
5614 safepos
= conf
->reshape_safe
;
5615 sector_div(safepos
, data_disks
);
5616 if (mddev
->reshape_backwards
) {
5617 BUG_ON(writepos
< reshape_sectors
);
5618 writepos
-= reshape_sectors
;
5619 readpos
+= reshape_sectors
;
5620 safepos
+= reshape_sectors
;
5622 writepos
+= reshape_sectors
;
5623 /* readpos and safepos are worst-case calculations.
5624 * A negative number is overly pessimistic, and causes
5625 * obvious problems for unsigned storage. So clip to 0.
5627 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
5628 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
5631 /* Having calculated the 'writepos' possibly use it
5632 * to set 'stripe_addr' which is where we will write to.
5634 if (mddev
->reshape_backwards
) {
5635 BUG_ON(conf
->reshape_progress
== 0);
5636 stripe_addr
= writepos
;
5637 BUG_ON((mddev
->dev_sectors
&
5638 ~((sector_t
)reshape_sectors
- 1))
5639 - reshape_sectors
- stripe_addr
5642 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
5643 stripe_addr
= sector_nr
;
5646 /* 'writepos' is the most advanced device address we might write.
5647 * 'readpos' is the least advanced device address we might read.
5648 * 'safepos' is the least address recorded in the metadata as having
5650 * If there is a min_offset_diff, these are adjusted either by
5651 * increasing the safepos/readpos if diff is negative, or
5652 * increasing writepos if diff is positive.
5653 * If 'readpos' is then behind 'writepos', there is no way that we can
5654 * ensure safety in the face of a crash - that must be done by userspace
5655 * making a backup of the data. So in that case there is no particular
5656 * rush to update metadata.
5657 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5658 * update the metadata to advance 'safepos' to match 'readpos' so that
5659 * we can be safe in the event of a crash.
5660 * So we insist on updating metadata if safepos is behind writepos and
5661 * readpos is beyond writepos.
5662 * In any case, update the metadata every 10 seconds.
5663 * Maybe that number should be configurable, but I'm not sure it is
5664 * worth it.... maybe it could be a multiple of safemode_delay???
5666 if (conf
->min_offset_diff
< 0) {
5667 safepos
+= -conf
->min_offset_diff
;
5668 readpos
+= -conf
->min_offset_diff
;
5670 writepos
+= conf
->min_offset_diff
;
5672 if ((mddev
->reshape_backwards
5673 ? (safepos
> writepos
&& readpos
< writepos
)
5674 : (safepos
< writepos
&& readpos
> writepos
)) ||
5675 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
5676 /* Cannot proceed until we've updated the superblock... */
5677 wait_event(conf
->wait_for_overlap
,
5678 atomic_read(&conf
->reshape_stripes
)==0
5679 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5680 if (atomic_read(&conf
->reshape_stripes
) != 0)
5682 mddev
->reshape_position
= conf
->reshape_progress
;
5683 mddev
->curr_resync_completed
= sector_nr
;
5684 conf
->reshape_checkpoint
= jiffies
;
5685 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
5686 md_wakeup_thread(mddev
->thread
);
5687 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
5688 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5689 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5691 spin_lock_irq(&conf
->device_lock
);
5692 conf
->reshape_safe
= mddev
->reshape_position
;
5693 spin_unlock_irq(&conf
->device_lock
);
5694 wake_up(&conf
->wait_for_overlap
);
5695 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5698 INIT_LIST_HEAD(&stripes
);
5699 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
5701 int skipped_disk
= 0;
5702 sh
= raid5_get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
5703 set_bit(STRIPE_EXPANDING
, &sh
->state
);
5704 atomic_inc(&conf
->reshape_stripes
);
5705 /* If any of this stripe is beyond the end of the old
5706 * array, then we need to zero those blocks
5708 for (j
=sh
->disks
; j
--;) {
5710 if (j
== sh
->pd_idx
)
5712 if (conf
->level
== 6 &&
5715 s
= raid5_compute_blocknr(sh
, j
, 0);
5716 if (s
< raid5_size(mddev
, 0, 0)) {
5720 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
5721 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
5722 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
5724 if (!skipped_disk
) {
5725 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
5726 set_bit(STRIPE_HANDLE
, &sh
->state
);
5728 list_add(&sh
->lru
, &stripes
);
5730 spin_lock_irq(&conf
->device_lock
);
5731 if (mddev
->reshape_backwards
)
5732 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
5734 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
5735 spin_unlock_irq(&conf
->device_lock
);
5736 /* Ok, those stripe are ready. We can start scheduling
5737 * reads on the source stripes.
5738 * The source stripes are determined by mapping the first and last
5739 * block on the destination stripes.
5742 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
5745 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
5746 * new_data_disks
- 1),
5748 if (last_sector
>= mddev
->dev_sectors
)
5749 last_sector
= mddev
->dev_sectors
- 1;
5750 while (first_sector
<= last_sector
) {
5751 sh
= raid5_get_active_stripe(conf
, first_sector
, 1, 0, 1);
5752 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
5753 set_bit(STRIPE_HANDLE
, &sh
->state
);
5754 raid5_release_stripe(sh
);
5755 first_sector
+= STRIPE_SECTORS
;
5757 /* Now that the sources are clearly marked, we can release
5758 * the destination stripes
5760 while (!list_empty(&stripes
)) {
5761 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
5762 list_del_init(&sh
->lru
);
5763 raid5_release_stripe(sh
);
5765 /* If this takes us to the resync_max point where we have to pause,
5766 * then we need to write out the superblock.
5768 sector_nr
+= reshape_sectors
;
5769 retn
= reshape_sectors
;
5771 if (mddev
->curr_resync_completed
> mddev
->resync_max
||
5772 (sector_nr
- mddev
->curr_resync_completed
) * 2
5773 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
5774 /* Cannot proceed until we've updated the superblock... */
5775 wait_event(conf
->wait_for_overlap
,
5776 atomic_read(&conf
->reshape_stripes
) == 0
5777 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5778 if (atomic_read(&conf
->reshape_stripes
) != 0)
5780 mddev
->reshape_position
= conf
->reshape_progress
;
5781 mddev
->curr_resync_completed
= sector_nr
;
5782 conf
->reshape_checkpoint
= jiffies
;
5783 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
5784 md_wakeup_thread(mddev
->thread
);
5785 wait_event(mddev
->sb_wait
,
5786 !test_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
)
5787 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5788 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5790 spin_lock_irq(&conf
->device_lock
);
5791 conf
->reshape_safe
= mddev
->reshape_position
;
5792 spin_unlock_irq(&conf
->device_lock
);
5793 wake_up(&conf
->wait_for_overlap
);
5794 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5800 static inline sector_t
raid5_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
5803 struct r5conf
*conf
= mddev
->private;
5804 struct stripe_head
*sh
;
5805 sector_t max_sector
= mddev
->dev_sectors
;
5806 sector_t sync_blocks
;
5807 int still_degraded
= 0;
5810 if (sector_nr
>= max_sector
) {
5811 /* just being told to finish up .. nothing much to do */
5813 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
5818 if (mddev
->curr_resync
< max_sector
) /* aborted */
5819 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
5821 else /* completed sync */
5823 bitmap_close_sync(mddev
->bitmap
);
5828 /* Allow raid5_quiesce to complete */
5829 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
5831 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5832 return reshape_request(mddev
, sector_nr
, skipped
);
5834 /* No need to check resync_max as we never do more than one
5835 * stripe, and as resync_max will always be on a chunk boundary,
5836 * if the check in md_do_sync didn't fire, there is no chance
5837 * of overstepping resync_max here
5840 /* if there is too many failed drives and we are trying
5841 * to resync, then assert that we are finished, because there is
5842 * nothing we can do.
5844 if (mddev
->degraded
>= conf
->max_degraded
&&
5845 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5846 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
5850 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
5852 !bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
5853 sync_blocks
>= STRIPE_SECTORS
) {
5854 /* we can skip this block, and probably more */
5855 sync_blocks
/= STRIPE_SECTORS
;
5857 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
5860 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
, false);
5862 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 1, 0);
5864 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 0, 0);
5865 /* make sure we don't swamp the stripe cache if someone else
5866 * is trying to get access
5868 schedule_timeout_uninterruptible(1);
5870 /* Need to check if array will still be degraded after recovery/resync
5871 * Note in case of > 1 drive failures it's possible we're rebuilding
5872 * one drive while leaving another faulty drive in array.
5875 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5876 struct md_rdev
*rdev
= ACCESS_ONCE(conf
->disks
[i
].rdev
);
5878 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
))
5883 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
5885 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
5886 set_bit(STRIPE_HANDLE
, &sh
->state
);
5888 raid5_release_stripe(sh
);
5890 return STRIPE_SECTORS
;
5893 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
5895 /* We may not be able to submit a whole bio at once as there
5896 * may not be enough stripe_heads available.
5897 * We cannot pre-allocate enough stripe_heads as we may need
5898 * more than exist in the cache (if we allow ever large chunks).
5899 * So we do one stripe head at a time and record in
5900 * ->bi_hw_segments how many have been done.
5902 * We *know* that this entire raid_bio is in one chunk, so
5903 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5905 struct stripe_head
*sh
;
5907 sector_t sector
, logical_sector
, last_sector
;
5912 logical_sector
= raid_bio
->bi_iter
.bi_sector
&
5913 ~((sector_t
)STRIPE_SECTORS
-1);
5914 sector
= raid5_compute_sector(conf
, logical_sector
,
5916 last_sector
= bio_end_sector(raid_bio
);
5918 for (; logical_sector
< last_sector
;
5919 logical_sector
+= STRIPE_SECTORS
,
5920 sector
+= STRIPE_SECTORS
,
5923 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
5924 /* already done this stripe */
5927 sh
= raid5_get_active_stripe(conf
, sector
, 0, 1, 1);
5930 /* failed to get a stripe - must wait */
5931 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5932 conf
->retry_read_aligned
= raid_bio
;
5936 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0, 0)) {
5937 raid5_release_stripe(sh
);
5938 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5939 conf
->retry_read_aligned
= raid_bio
;
5943 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
5945 raid5_release_stripe(sh
);
5948 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
5949 if (remaining
== 0) {
5950 trace_block_bio_complete(bdev_get_queue(raid_bio
->bi_bdev
),
5952 bio_endio(raid_bio
);
5954 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
5955 wake_up(&conf
->wait_for_quiescent
);
5959 static int handle_active_stripes(struct r5conf
*conf
, int group
,
5960 struct r5worker
*worker
,
5961 struct list_head
*temp_inactive_list
)
5963 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
5964 int i
, batch_size
= 0, hash
;
5965 bool release_inactive
= false;
5967 while (batch_size
< MAX_STRIPE_BATCH
&&
5968 (sh
= __get_priority_stripe(conf
, group
)) != NULL
)
5969 batch
[batch_size
++] = sh
;
5971 if (batch_size
== 0) {
5972 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5973 if (!list_empty(temp_inactive_list
+ i
))
5975 if (i
== NR_STRIPE_HASH_LOCKS
) {
5976 spin_unlock_irq(&conf
->device_lock
);
5977 r5l_flush_stripe_to_raid(conf
->log
);
5978 spin_lock_irq(&conf
->device_lock
);
5981 release_inactive
= true;
5983 spin_unlock_irq(&conf
->device_lock
);
5985 release_inactive_stripe_list(conf
, temp_inactive_list
,
5986 NR_STRIPE_HASH_LOCKS
);
5988 r5l_flush_stripe_to_raid(conf
->log
);
5989 if (release_inactive
) {
5990 spin_lock_irq(&conf
->device_lock
);
5994 for (i
= 0; i
< batch_size
; i
++)
5995 handle_stripe(batch
[i
]);
5996 r5l_write_stripe_run(conf
->log
);
6000 spin_lock_irq(&conf
->device_lock
);
6001 for (i
= 0; i
< batch_size
; i
++) {
6002 hash
= batch
[i
]->hash_lock_index
;
6003 __release_stripe(conf
, batch
[i
], &temp_inactive_list
[hash
]);
6008 static void raid5_do_work(struct work_struct
*work
)
6010 struct r5worker
*worker
= container_of(work
, struct r5worker
, work
);
6011 struct r5worker_group
*group
= worker
->group
;
6012 struct r5conf
*conf
= group
->conf
;
6013 int group_id
= group
- conf
->worker_groups
;
6015 struct blk_plug plug
;
6017 pr_debug("+++ raid5worker active\n");
6019 blk_start_plug(&plug
);
6021 spin_lock_irq(&conf
->device_lock
);
6023 int batch_size
, released
;
6025 released
= release_stripe_list(conf
, worker
->temp_inactive_list
);
6027 batch_size
= handle_active_stripes(conf
, group_id
, worker
,
6028 worker
->temp_inactive_list
);
6029 worker
->working
= false;
6030 if (!batch_size
&& !released
)
6032 handled
+= batch_size
;
6034 pr_debug("%d stripes handled\n", handled
);
6036 spin_unlock_irq(&conf
->device_lock
);
6037 blk_finish_plug(&plug
);
6039 pr_debug("--- raid5worker inactive\n");
6043 * This is our raid5 kernel thread.
6045 * We scan the hash table for stripes which can be handled now.
6046 * During the scan, completed stripes are saved for us by the interrupt
6047 * handler, so that they will not have to wait for our next wakeup.
6049 static void raid5d(struct md_thread
*thread
)
6051 struct mddev
*mddev
= thread
->mddev
;
6052 struct r5conf
*conf
= mddev
->private;
6054 struct blk_plug plug
;
6056 pr_debug("+++ raid5d active\n");
6058 md_check_recovery(mddev
);
6060 if (!bio_list_empty(&conf
->return_bi
) &&
6061 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
6062 struct bio_list tmp
= BIO_EMPTY_LIST
;
6063 spin_lock_irq(&conf
->device_lock
);
6064 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
6065 bio_list_merge(&tmp
, &conf
->return_bi
);
6066 bio_list_init(&conf
->return_bi
);
6068 spin_unlock_irq(&conf
->device_lock
);
6072 blk_start_plug(&plug
);
6074 spin_lock_irq(&conf
->device_lock
);
6077 int batch_size
, released
;
6079 released
= release_stripe_list(conf
, conf
->temp_inactive_list
);
6081 clear_bit(R5_DID_ALLOC
, &conf
->cache_state
);
6084 !list_empty(&conf
->bitmap_list
)) {
6085 /* Now is a good time to flush some bitmap updates */
6087 spin_unlock_irq(&conf
->device_lock
);
6088 bitmap_unplug(mddev
->bitmap
);
6089 spin_lock_irq(&conf
->device_lock
);
6090 conf
->seq_write
= conf
->seq_flush
;
6091 activate_bit_delay(conf
, conf
->temp_inactive_list
);
6093 raid5_activate_delayed(conf
);
6095 while ((bio
= remove_bio_from_retry(conf
))) {
6097 spin_unlock_irq(&conf
->device_lock
);
6098 ok
= retry_aligned_read(conf
, bio
);
6099 spin_lock_irq(&conf
->device_lock
);
6105 batch_size
= handle_active_stripes(conf
, ANY_GROUP
, NULL
,
6106 conf
->temp_inactive_list
);
6107 if (!batch_size
&& !released
)
6109 handled
+= batch_size
;
6111 if (mddev
->sb_flags
& ~(1 << MD_SB_CHANGE_PENDING
)) {
6112 spin_unlock_irq(&conf
->device_lock
);
6113 md_check_recovery(mddev
);
6114 spin_lock_irq(&conf
->device_lock
);
6117 pr_debug("%d stripes handled\n", handled
);
6119 spin_unlock_irq(&conf
->device_lock
);
6120 if (test_and_clear_bit(R5_ALLOC_MORE
, &conf
->cache_state
) &&
6121 mutex_trylock(&conf
->cache_size_mutex
)) {
6122 grow_one_stripe(conf
, __GFP_NOWARN
);
6123 /* Set flag even if allocation failed. This helps
6124 * slow down allocation requests when mem is short
6126 set_bit(R5_DID_ALLOC
, &conf
->cache_state
);
6127 mutex_unlock(&conf
->cache_size_mutex
);
6130 r5l_flush_stripe_to_raid(conf
->log
);
6132 async_tx_issue_pending_all();
6133 blk_finish_plug(&plug
);
6135 pr_debug("--- raid5d inactive\n");
6139 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
6141 struct r5conf
*conf
;
6143 spin_lock(&mddev
->lock
);
6144 conf
= mddev
->private;
6146 ret
= sprintf(page
, "%d\n", conf
->min_nr_stripes
);
6147 spin_unlock(&mddev
->lock
);
6152 raid5_set_cache_size(struct mddev
*mddev
, int size
)
6154 struct r5conf
*conf
= mddev
->private;
6157 if (size
<= 16 || size
> 32768)
6160 conf
->min_nr_stripes
= size
;
6161 mutex_lock(&conf
->cache_size_mutex
);
6162 while (size
< conf
->max_nr_stripes
&&
6163 drop_one_stripe(conf
))
6165 mutex_unlock(&conf
->cache_size_mutex
);
6168 err
= md_allow_write(mddev
);
6172 mutex_lock(&conf
->cache_size_mutex
);
6173 while (size
> conf
->max_nr_stripes
)
6174 if (!grow_one_stripe(conf
, GFP_KERNEL
))
6176 mutex_unlock(&conf
->cache_size_mutex
);
6180 EXPORT_SYMBOL(raid5_set_cache_size
);
6183 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
6185 struct r5conf
*conf
;
6189 if (len
>= PAGE_SIZE
)
6191 if (kstrtoul(page
, 10, &new))
6193 err
= mddev_lock(mddev
);
6196 conf
= mddev
->private;
6200 err
= raid5_set_cache_size(mddev
, new);
6201 mddev_unlock(mddev
);
6206 static struct md_sysfs_entry
6207 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
6208 raid5_show_stripe_cache_size
,
6209 raid5_store_stripe_cache_size
);
6212 raid5_show_rmw_level(struct mddev
*mddev
, char *page
)
6214 struct r5conf
*conf
= mddev
->private;
6216 return sprintf(page
, "%d\n", conf
->rmw_level
);
6222 raid5_store_rmw_level(struct mddev
*mddev
, const char *page
, size_t len
)
6224 struct r5conf
*conf
= mddev
->private;
6230 if (len
>= PAGE_SIZE
)
6233 if (kstrtoul(page
, 10, &new))
6236 if (new != PARITY_DISABLE_RMW
&& !raid6_call
.xor_syndrome
)
6239 if (new != PARITY_DISABLE_RMW
&&
6240 new != PARITY_ENABLE_RMW
&&
6241 new != PARITY_PREFER_RMW
)
6244 conf
->rmw_level
= new;
6248 static struct md_sysfs_entry
6249 raid5_rmw_level
= __ATTR(rmw_level
, S_IRUGO
| S_IWUSR
,
6250 raid5_show_rmw_level
,
6251 raid5_store_rmw_level
);
6255 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
6257 struct r5conf
*conf
;
6259 spin_lock(&mddev
->lock
);
6260 conf
= mddev
->private;
6262 ret
= sprintf(page
, "%d\n", conf
->bypass_threshold
);
6263 spin_unlock(&mddev
->lock
);
6268 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
6270 struct r5conf
*conf
;
6274 if (len
>= PAGE_SIZE
)
6276 if (kstrtoul(page
, 10, &new))
6279 err
= mddev_lock(mddev
);
6282 conf
= mddev
->private;
6285 else if (new > conf
->min_nr_stripes
)
6288 conf
->bypass_threshold
= new;
6289 mddev_unlock(mddev
);
6293 static struct md_sysfs_entry
6294 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
6296 raid5_show_preread_threshold
,
6297 raid5_store_preread_threshold
);
6300 raid5_show_skip_copy(struct mddev
*mddev
, char *page
)
6302 struct r5conf
*conf
;
6304 spin_lock(&mddev
->lock
);
6305 conf
= mddev
->private;
6307 ret
= sprintf(page
, "%d\n", conf
->skip_copy
);
6308 spin_unlock(&mddev
->lock
);
6313 raid5_store_skip_copy(struct mddev
*mddev
, const char *page
, size_t len
)
6315 struct r5conf
*conf
;
6319 if (len
>= PAGE_SIZE
)
6321 if (kstrtoul(page
, 10, &new))
6325 err
= mddev_lock(mddev
);
6328 conf
= mddev
->private;
6331 else if (new != conf
->skip_copy
) {
6332 mddev_suspend(mddev
);
6333 conf
->skip_copy
= new;
6335 mddev
->queue
->backing_dev_info
.capabilities
|=
6336 BDI_CAP_STABLE_WRITES
;
6338 mddev
->queue
->backing_dev_info
.capabilities
&=
6339 ~BDI_CAP_STABLE_WRITES
;
6340 mddev_resume(mddev
);
6342 mddev_unlock(mddev
);
6346 static struct md_sysfs_entry
6347 raid5_skip_copy
= __ATTR(skip_copy
, S_IRUGO
| S_IWUSR
,
6348 raid5_show_skip_copy
,
6349 raid5_store_skip_copy
);
6352 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
6354 struct r5conf
*conf
= mddev
->private;
6356 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
6361 static struct md_sysfs_entry
6362 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
6365 raid5_show_group_thread_cnt(struct mddev
*mddev
, char *page
)
6367 struct r5conf
*conf
;
6369 spin_lock(&mddev
->lock
);
6370 conf
= mddev
->private;
6372 ret
= sprintf(page
, "%d\n", conf
->worker_cnt_per_group
);
6373 spin_unlock(&mddev
->lock
);
6377 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6379 int *worker_cnt_per_group
,
6380 struct r5worker_group
**worker_groups
);
6382 raid5_store_group_thread_cnt(struct mddev
*mddev
, const char *page
, size_t len
)
6384 struct r5conf
*conf
;
6387 struct r5worker_group
*new_groups
, *old_groups
;
6388 int group_cnt
, worker_cnt_per_group
;
6390 if (len
>= PAGE_SIZE
)
6392 if (kstrtoul(page
, 10, &new))
6395 err
= mddev_lock(mddev
);
6398 conf
= mddev
->private;
6401 else if (new != conf
->worker_cnt_per_group
) {
6402 mddev_suspend(mddev
);
6404 old_groups
= conf
->worker_groups
;
6406 flush_workqueue(raid5_wq
);
6408 err
= alloc_thread_groups(conf
, new,
6409 &group_cnt
, &worker_cnt_per_group
,
6412 spin_lock_irq(&conf
->device_lock
);
6413 conf
->group_cnt
= group_cnt
;
6414 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6415 conf
->worker_groups
= new_groups
;
6416 spin_unlock_irq(&conf
->device_lock
);
6419 kfree(old_groups
[0].workers
);
6422 mddev_resume(mddev
);
6424 mddev_unlock(mddev
);
6429 static struct md_sysfs_entry
6430 raid5_group_thread_cnt
= __ATTR(group_thread_cnt
, S_IRUGO
| S_IWUSR
,
6431 raid5_show_group_thread_cnt
,
6432 raid5_store_group_thread_cnt
);
6434 static struct attribute
*raid5_attrs
[] = {
6435 &raid5_stripecache_size
.attr
,
6436 &raid5_stripecache_active
.attr
,
6437 &raid5_preread_bypass_threshold
.attr
,
6438 &raid5_group_thread_cnt
.attr
,
6439 &raid5_skip_copy
.attr
,
6440 &raid5_rmw_level
.attr
,
6441 &r5c_journal_mode
.attr
,
6444 static struct attribute_group raid5_attrs_group
= {
6446 .attrs
= raid5_attrs
,
6449 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6451 int *worker_cnt_per_group
,
6452 struct r5worker_group
**worker_groups
)
6456 struct r5worker
*workers
;
6458 *worker_cnt_per_group
= cnt
;
6461 *worker_groups
= NULL
;
6464 *group_cnt
= num_possible_nodes();
6465 size
= sizeof(struct r5worker
) * cnt
;
6466 workers
= kzalloc(size
* *group_cnt
, GFP_NOIO
);
6467 *worker_groups
= kzalloc(sizeof(struct r5worker_group
) *
6468 *group_cnt
, GFP_NOIO
);
6469 if (!*worker_groups
|| !workers
) {
6471 kfree(*worker_groups
);
6475 for (i
= 0; i
< *group_cnt
; i
++) {
6476 struct r5worker_group
*group
;
6478 group
= &(*worker_groups
)[i
];
6479 INIT_LIST_HEAD(&group
->handle_list
);
6481 group
->workers
= workers
+ i
* cnt
;
6483 for (j
= 0; j
< cnt
; j
++) {
6484 struct r5worker
*worker
= group
->workers
+ j
;
6485 worker
->group
= group
;
6486 INIT_WORK(&worker
->work
, raid5_do_work
);
6488 for (k
= 0; k
< NR_STRIPE_HASH_LOCKS
; k
++)
6489 INIT_LIST_HEAD(worker
->temp_inactive_list
+ k
);
6496 static void free_thread_groups(struct r5conf
*conf
)
6498 if (conf
->worker_groups
)
6499 kfree(conf
->worker_groups
[0].workers
);
6500 kfree(conf
->worker_groups
);
6501 conf
->worker_groups
= NULL
;
6505 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
6507 struct r5conf
*conf
= mddev
->private;
6510 sectors
= mddev
->dev_sectors
;
6512 /* size is defined by the smallest of previous and new size */
6513 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
6515 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
6516 sectors
&= ~((sector_t
)conf
->prev_chunk_sectors
- 1);
6517 return sectors
* (raid_disks
- conf
->max_degraded
);
6520 static void free_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6522 safe_put_page(percpu
->spare_page
);
6523 if (percpu
->scribble
)
6524 flex_array_free(percpu
->scribble
);
6525 percpu
->spare_page
= NULL
;
6526 percpu
->scribble
= NULL
;
6529 static int alloc_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6531 if (conf
->level
== 6 && !percpu
->spare_page
)
6532 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
6533 if (!percpu
->scribble
)
6534 percpu
->scribble
= scribble_alloc(max(conf
->raid_disks
,
6535 conf
->previous_raid_disks
),
6536 max(conf
->chunk_sectors
,
6537 conf
->prev_chunk_sectors
)
6541 if (!percpu
->scribble
|| (conf
->level
== 6 && !percpu
->spare_page
)) {
6542 free_scratch_buffer(conf
, percpu
);
6549 static int raid456_cpu_dead(unsigned int cpu
, struct hlist_node
*node
)
6551 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6553 free_scratch_buffer(conf
, per_cpu_ptr(conf
->percpu
, cpu
));
6557 static void raid5_free_percpu(struct r5conf
*conf
)
6562 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6563 free_percpu(conf
->percpu
);
6566 static void free_conf(struct r5conf
*conf
)
6571 r5l_exit_log(conf
->log
);
6572 if (conf
->shrinker
.nr_deferred
)
6573 unregister_shrinker(&conf
->shrinker
);
6575 free_thread_groups(conf
);
6576 shrink_stripes(conf
);
6577 raid5_free_percpu(conf
);
6578 for (i
= 0; i
< conf
->pool_size
; i
++)
6579 if (conf
->disks
[i
].extra_page
)
6580 put_page(conf
->disks
[i
].extra_page
);
6582 kfree(conf
->stripe_hashtbl
);
6586 static int raid456_cpu_up_prepare(unsigned int cpu
, struct hlist_node
*node
)
6588 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6589 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
6591 if (alloc_scratch_buffer(conf
, percpu
)) {
6592 pr_warn("%s: failed memory allocation for cpu%u\n",
6599 static int raid5_alloc_percpu(struct r5conf
*conf
)
6603 conf
->percpu
= alloc_percpu(struct raid5_percpu
);
6607 err
= cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6609 conf
->scribble_disks
= max(conf
->raid_disks
,
6610 conf
->previous_raid_disks
);
6611 conf
->scribble_sectors
= max(conf
->chunk_sectors
,
6612 conf
->prev_chunk_sectors
);
6617 static unsigned long raid5_cache_scan(struct shrinker
*shrink
,
6618 struct shrink_control
*sc
)
6620 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6621 unsigned long ret
= SHRINK_STOP
;
6623 if (mutex_trylock(&conf
->cache_size_mutex
)) {
6625 while (ret
< sc
->nr_to_scan
&&
6626 conf
->max_nr_stripes
> conf
->min_nr_stripes
) {
6627 if (drop_one_stripe(conf
) == 0) {
6633 mutex_unlock(&conf
->cache_size_mutex
);
6638 static unsigned long raid5_cache_count(struct shrinker
*shrink
,
6639 struct shrink_control
*sc
)
6641 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6643 if (conf
->max_nr_stripes
< conf
->min_nr_stripes
)
6644 /* unlikely, but not impossible */
6646 return conf
->max_nr_stripes
- conf
->min_nr_stripes
;
6649 static struct r5conf
*setup_conf(struct mddev
*mddev
)
6651 struct r5conf
*conf
;
6652 int raid_disk
, memory
, max_disks
;
6653 struct md_rdev
*rdev
;
6654 struct disk_info
*disk
;
6657 int group_cnt
, worker_cnt_per_group
;
6658 struct r5worker_group
*new_group
;
6660 if (mddev
->new_level
!= 5
6661 && mddev
->new_level
!= 4
6662 && mddev
->new_level
!= 6) {
6663 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6664 mdname(mddev
), mddev
->new_level
);
6665 return ERR_PTR(-EIO
);
6667 if ((mddev
->new_level
== 5
6668 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
6669 (mddev
->new_level
== 6
6670 && !algorithm_valid_raid6(mddev
->new_layout
))) {
6671 pr_warn("md/raid:%s: layout %d not supported\n",
6672 mdname(mddev
), mddev
->new_layout
);
6673 return ERR_PTR(-EIO
);
6675 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
6676 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6677 mdname(mddev
), mddev
->raid_disks
);
6678 return ERR_PTR(-EINVAL
);
6681 if (!mddev
->new_chunk_sectors
||
6682 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
6683 !is_power_of_2(mddev
->new_chunk_sectors
)) {
6684 pr_warn("md/raid:%s: invalid chunk size %d\n",
6685 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
6686 return ERR_PTR(-EINVAL
);
6689 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
6692 /* Don't enable multi-threading by default*/
6693 if (!alloc_thread_groups(conf
, 0, &group_cnt
, &worker_cnt_per_group
,
6695 conf
->group_cnt
= group_cnt
;
6696 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6697 conf
->worker_groups
= new_group
;
6700 spin_lock_init(&conf
->device_lock
);
6701 seqcount_init(&conf
->gen_lock
);
6702 mutex_init(&conf
->cache_size_mutex
);
6703 init_waitqueue_head(&conf
->wait_for_quiescent
);
6704 init_waitqueue_head(&conf
->wait_for_stripe
);
6705 init_waitqueue_head(&conf
->wait_for_overlap
);
6706 INIT_LIST_HEAD(&conf
->handle_list
);
6707 INIT_LIST_HEAD(&conf
->hold_list
);
6708 INIT_LIST_HEAD(&conf
->delayed_list
);
6709 INIT_LIST_HEAD(&conf
->bitmap_list
);
6710 bio_list_init(&conf
->return_bi
);
6711 init_llist_head(&conf
->released_stripes
);
6712 atomic_set(&conf
->active_stripes
, 0);
6713 atomic_set(&conf
->preread_active_stripes
, 0);
6714 atomic_set(&conf
->active_aligned_reads
, 0);
6715 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
6716 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
6718 conf
->raid_disks
= mddev
->raid_disks
;
6719 if (mddev
->reshape_position
== MaxSector
)
6720 conf
->previous_raid_disks
= mddev
->raid_disks
;
6722 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6723 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
6725 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
6731 for (i
= 0; i
< max_disks
; i
++) {
6732 conf
->disks
[i
].extra_page
= alloc_page(GFP_KERNEL
);
6733 if (!conf
->disks
[i
].extra_page
)
6737 conf
->mddev
= mddev
;
6739 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
6742 /* We init hash_locks[0] separately to that it can be used
6743 * as the reference lock in the spin_lock_nest_lock() call
6744 * in lock_all_device_hash_locks_irq in order to convince
6745 * lockdep that we know what we are doing.
6747 spin_lock_init(conf
->hash_locks
);
6748 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6749 spin_lock_init(conf
->hash_locks
+ i
);
6751 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6752 INIT_LIST_HEAD(conf
->inactive_list
+ i
);
6754 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6755 INIT_LIST_HEAD(conf
->temp_inactive_list
+ i
);
6757 atomic_set(&conf
->r5c_cached_full_stripes
, 0);
6758 INIT_LIST_HEAD(&conf
->r5c_full_stripe_list
);
6759 atomic_set(&conf
->r5c_cached_partial_stripes
, 0);
6760 INIT_LIST_HEAD(&conf
->r5c_partial_stripe_list
);
6762 conf
->level
= mddev
->new_level
;
6763 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
6764 if (raid5_alloc_percpu(conf
) != 0)
6767 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
6769 rdev_for_each(rdev
, mddev
) {
6770 raid_disk
= rdev
->raid_disk
;
6771 if (raid_disk
>= max_disks
6772 || raid_disk
< 0 || test_bit(Journal
, &rdev
->flags
))
6774 disk
= conf
->disks
+ raid_disk
;
6776 if (test_bit(Replacement
, &rdev
->flags
)) {
6777 if (disk
->replacement
)
6779 disk
->replacement
= rdev
;
6786 if (test_bit(In_sync
, &rdev
->flags
)) {
6787 char b
[BDEVNAME_SIZE
];
6788 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6789 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
6790 } else if (rdev
->saved_raid_disk
!= raid_disk
)
6791 /* Cannot rely on bitmap to complete recovery */
6795 conf
->level
= mddev
->new_level
;
6796 if (conf
->level
== 6) {
6797 conf
->max_degraded
= 2;
6798 if (raid6_call
.xor_syndrome
)
6799 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6801 conf
->rmw_level
= PARITY_DISABLE_RMW
;
6803 conf
->max_degraded
= 1;
6804 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6806 conf
->algorithm
= mddev
->new_layout
;
6807 conf
->reshape_progress
= mddev
->reshape_position
;
6808 if (conf
->reshape_progress
!= MaxSector
) {
6809 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
6810 conf
->prev_algo
= mddev
->layout
;
6812 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
6813 conf
->prev_algo
= conf
->algorithm
;
6816 conf
->min_nr_stripes
= NR_STRIPES
;
6817 if (mddev
->reshape_position
!= MaxSector
) {
6818 int stripes
= max_t(int,
6819 ((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4,
6820 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4);
6821 conf
->min_nr_stripes
= max(NR_STRIPES
, stripes
);
6822 if (conf
->min_nr_stripes
!= NR_STRIPES
)
6823 pr_info("md/raid:%s: force stripe size %d for reshape\n",
6824 mdname(mddev
), conf
->min_nr_stripes
);
6826 memory
= conf
->min_nr_stripes
* (sizeof(struct stripe_head
) +
6827 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
6828 atomic_set(&conf
->empty_inactive_list_nr
, NR_STRIPE_HASH_LOCKS
);
6829 if (grow_stripes(conf
, conf
->min_nr_stripes
)) {
6830 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6831 mdname(mddev
), memory
);
6834 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev
), memory
);
6836 * Losing a stripe head costs more than the time to refill it,
6837 * it reduces the queue depth and so can hurt throughput.
6838 * So set it rather large, scaled by number of devices.
6840 conf
->shrinker
.seeks
= DEFAULT_SEEKS
* conf
->raid_disks
* 4;
6841 conf
->shrinker
.scan_objects
= raid5_cache_scan
;
6842 conf
->shrinker
.count_objects
= raid5_cache_count
;
6843 conf
->shrinker
.batch
= 128;
6844 conf
->shrinker
.flags
= 0;
6845 if (register_shrinker(&conf
->shrinker
)) {
6846 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6851 sprintf(pers_name
, "raid%d", mddev
->new_level
);
6852 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
6853 if (!conf
->thread
) {
6854 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6864 return ERR_PTR(-EIO
);
6866 return ERR_PTR(-ENOMEM
);
6869 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
6872 case ALGORITHM_PARITY_0
:
6873 if (raid_disk
< max_degraded
)
6876 case ALGORITHM_PARITY_N
:
6877 if (raid_disk
>= raid_disks
- max_degraded
)
6880 case ALGORITHM_PARITY_0_6
:
6881 if (raid_disk
== 0 ||
6882 raid_disk
== raid_disks
- 1)
6885 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6886 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6887 case ALGORITHM_LEFT_SYMMETRIC_6
:
6888 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6889 if (raid_disk
== raid_disks
- 1)
6895 static int raid5_run(struct mddev
*mddev
)
6897 struct r5conf
*conf
;
6898 int working_disks
= 0;
6899 int dirty_parity_disks
= 0;
6900 struct md_rdev
*rdev
;
6901 struct md_rdev
*journal_dev
= NULL
;
6902 sector_t reshape_offset
= 0;
6904 long long min_offset_diff
= 0;
6907 if (mddev
->recovery_cp
!= MaxSector
)
6908 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6911 rdev_for_each(rdev
, mddev
) {
6914 if (test_bit(Journal
, &rdev
->flags
)) {
6918 if (rdev
->raid_disk
< 0)
6920 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
6922 min_offset_diff
= diff
;
6924 } else if (mddev
->reshape_backwards
&&
6925 diff
< min_offset_diff
)
6926 min_offset_diff
= diff
;
6927 else if (!mddev
->reshape_backwards
&&
6928 diff
> min_offset_diff
)
6929 min_offset_diff
= diff
;
6932 if (mddev
->reshape_position
!= MaxSector
) {
6933 /* Check that we can continue the reshape.
6934 * Difficulties arise if the stripe we would write to
6935 * next is at or after the stripe we would read from next.
6936 * For a reshape that changes the number of devices, this
6937 * is only possible for a very short time, and mdadm makes
6938 * sure that time appears to have past before assembling
6939 * the array. So we fail if that time hasn't passed.
6940 * For a reshape that keeps the number of devices the same
6941 * mdadm must be monitoring the reshape can keeping the
6942 * critical areas read-only and backed up. It will start
6943 * the array in read-only mode, so we check for that.
6945 sector_t here_new
, here_old
;
6947 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
6952 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
6957 if (mddev
->new_level
!= mddev
->level
) {
6958 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
6962 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6963 /* reshape_position must be on a new-stripe boundary, and one
6964 * further up in new geometry must map after here in old
6966 * If the chunk sizes are different, then as we perform reshape
6967 * in units of the largest of the two, reshape_position needs
6968 * be a multiple of the largest chunk size times new data disks.
6970 here_new
= mddev
->reshape_position
;
6971 chunk_sectors
= max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
);
6972 new_data_disks
= mddev
->raid_disks
- max_degraded
;
6973 if (sector_div(here_new
, chunk_sectors
* new_data_disks
)) {
6974 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
6978 reshape_offset
= here_new
* chunk_sectors
;
6979 /* here_new is the stripe we will write to */
6980 here_old
= mddev
->reshape_position
;
6981 sector_div(here_old
, chunk_sectors
* (old_disks
-max_degraded
));
6982 /* here_old is the first stripe that we might need to read
6984 if (mddev
->delta_disks
== 0) {
6985 /* We cannot be sure it is safe to start an in-place
6986 * reshape. It is only safe if user-space is monitoring
6987 * and taking constant backups.
6988 * mdadm always starts a situation like this in
6989 * readonly mode so it can take control before
6990 * allowing any writes. So just check for that.
6992 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
6993 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
6994 /* not really in-place - so OK */;
6995 else if (mddev
->ro
== 0) {
6996 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7000 } else if (mddev
->reshape_backwards
7001 ? (here_new
* chunk_sectors
+ min_offset_diff
<=
7002 here_old
* chunk_sectors
)
7003 : (here_new
* chunk_sectors
>=
7004 here_old
* chunk_sectors
+ (-min_offset_diff
))) {
7005 /* Reading from the same stripe as writing to - bad */
7006 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7010 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev
));
7011 /* OK, we should be able to continue; */
7013 BUG_ON(mddev
->level
!= mddev
->new_level
);
7014 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
7015 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
7016 BUG_ON(mddev
->delta_disks
!= 0);
7019 if (mddev
->private == NULL
)
7020 conf
= setup_conf(mddev
);
7022 conf
= mddev
->private;
7025 return PTR_ERR(conf
);
7027 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
7029 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7032 set_disk_ro(mddev
->gendisk
, 1);
7033 } else if (mddev
->recovery_cp
== MaxSector
)
7034 set_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
7037 conf
->min_offset_diff
= min_offset_diff
;
7038 mddev
->thread
= conf
->thread
;
7039 conf
->thread
= NULL
;
7040 mddev
->private = conf
;
7042 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
7044 rdev
= conf
->disks
[i
].rdev
;
7045 if (!rdev
&& conf
->disks
[i
].replacement
) {
7046 /* The replacement is all we have yet */
7047 rdev
= conf
->disks
[i
].replacement
;
7048 conf
->disks
[i
].replacement
= NULL
;
7049 clear_bit(Replacement
, &rdev
->flags
);
7050 conf
->disks
[i
].rdev
= rdev
;
7054 if (conf
->disks
[i
].replacement
&&
7055 conf
->reshape_progress
!= MaxSector
) {
7056 /* replacements and reshape simply do not mix. */
7057 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
7060 if (test_bit(In_sync
, &rdev
->flags
)) {
7064 /* This disc is not fully in-sync. However if it
7065 * just stored parity (beyond the recovery_offset),
7066 * when we don't need to be concerned about the
7067 * array being dirty.
7068 * When reshape goes 'backwards', we never have
7069 * partially completed devices, so we only need
7070 * to worry about reshape going forwards.
7072 /* Hack because v0.91 doesn't store recovery_offset properly. */
7073 if (mddev
->major_version
== 0 &&
7074 mddev
->minor_version
> 90)
7075 rdev
->recovery_offset
= reshape_offset
;
7077 if (rdev
->recovery_offset
< reshape_offset
) {
7078 /* We need to check old and new layout */
7079 if (!only_parity(rdev
->raid_disk
,
7082 conf
->max_degraded
))
7085 if (!only_parity(rdev
->raid_disk
,
7087 conf
->previous_raid_disks
,
7088 conf
->max_degraded
))
7090 dirty_parity_disks
++;
7094 * 0 for a fully functional array, 1 or 2 for a degraded array.
7096 mddev
->degraded
= raid5_calc_degraded(conf
);
7098 if (has_failed(conf
)) {
7099 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
7100 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
7104 /* device size must be a multiple of chunk size */
7105 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
7106 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
7108 if (mddev
->degraded
> dirty_parity_disks
&&
7109 mddev
->recovery_cp
!= MaxSector
) {
7110 if (mddev
->ok_start_degraded
)
7111 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7114 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7120 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7121 mdname(mddev
), conf
->level
,
7122 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
7125 print_raid5_conf(conf
);
7127 if (conf
->reshape_progress
!= MaxSector
) {
7128 conf
->reshape_safe
= conf
->reshape_progress
;
7129 atomic_set(&conf
->reshape_stripes
, 0);
7130 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7131 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7132 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7133 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7134 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7138 /* Ok, everything is just fine now */
7139 if (mddev
->to_remove
== &raid5_attrs_group
)
7140 mddev
->to_remove
= NULL
;
7141 else if (mddev
->kobj
.sd
&&
7142 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
7143 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7145 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7149 bool discard_supported
= true;
7150 /* read-ahead size must cover two whole stripes, which
7151 * is 2 * (datadisks) * chunksize where 'n' is the
7152 * number of raid devices
7154 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
7155 int stripe
= data_disks
*
7156 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
7157 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
7158 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
7160 chunk_size
= mddev
->chunk_sectors
<< 9;
7161 blk_queue_io_min(mddev
->queue
, chunk_size
);
7162 blk_queue_io_opt(mddev
->queue
, chunk_size
*
7163 (conf
->raid_disks
- conf
->max_degraded
));
7164 mddev
->queue
->limits
.raid_partial_stripes_expensive
= 1;
7166 * We can only discard a whole stripe. It doesn't make sense to
7167 * discard data disk but write parity disk
7169 stripe
= stripe
* PAGE_SIZE
;
7170 /* Round up to power of 2, as discard handling
7171 * currently assumes that */
7172 while ((stripe
-1) & stripe
)
7173 stripe
= (stripe
| (stripe
-1)) + 1;
7174 mddev
->queue
->limits
.discard_alignment
= stripe
;
7175 mddev
->queue
->limits
.discard_granularity
= stripe
;
7178 * We use 16-bit counter of active stripes in bi_phys_segments
7179 * (minus one for over-loaded initialization)
7181 blk_queue_max_hw_sectors(mddev
->queue
, 0xfffe * STRIPE_SECTORS
);
7182 blk_queue_max_discard_sectors(mddev
->queue
,
7183 0xfffe * STRIPE_SECTORS
);
7186 * unaligned part of discard request will be ignored, so can't
7187 * guarantee discard_zeroes_data
7189 mddev
->queue
->limits
.discard_zeroes_data
= 0;
7191 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
7193 rdev_for_each(rdev
, mddev
) {
7194 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7195 rdev
->data_offset
<< 9);
7196 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7197 rdev
->new_data_offset
<< 9);
7199 * discard_zeroes_data is required, otherwise data
7200 * could be lost. Consider a scenario: discard a stripe
7201 * (the stripe could be inconsistent if
7202 * discard_zeroes_data is 0); write one disk of the
7203 * stripe (the stripe could be inconsistent again
7204 * depending on which disks are used to calculate
7205 * parity); the disk is broken; The stripe data of this
7208 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
7209 !bdev_get_queue(rdev
->bdev
)->
7210 limits
.discard_zeroes_data
)
7211 discard_supported
= false;
7212 /* Unfortunately, discard_zeroes_data is not currently
7213 * a guarantee - just a hint. So we only allow DISCARD
7214 * if the sysadmin has confirmed that only safe devices
7215 * are in use by setting a module parameter.
7217 if (!devices_handle_discard_safely
) {
7218 if (discard_supported
) {
7219 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7220 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7222 discard_supported
= false;
7226 if (discard_supported
&&
7227 mddev
->queue
->limits
.max_discard_sectors
>= (stripe
>> 9) &&
7228 mddev
->queue
->limits
.discard_granularity
>= stripe
)
7229 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
7232 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
7235 blk_queue_max_hw_sectors(mddev
->queue
, UINT_MAX
);
7239 char b
[BDEVNAME_SIZE
];
7241 pr_debug("md/raid:%s: using device %s as journal\n",
7242 mdname(mddev
), bdevname(journal_dev
->bdev
, b
));
7243 if (r5l_init_log(conf
, journal_dev
))
7249 md_unregister_thread(&mddev
->thread
);
7250 print_raid5_conf(conf
);
7252 mddev
->private = NULL
;
7253 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev
));
7257 static void raid5_free(struct mddev
*mddev
, void *priv
)
7259 struct r5conf
*conf
= priv
;
7262 mddev
->to_remove
= &raid5_attrs_group
;
7265 static void raid5_status(struct seq_file
*seq
, struct mddev
*mddev
)
7267 struct r5conf
*conf
= mddev
->private;
7270 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
7271 conf
->chunk_sectors
/ 2, mddev
->layout
);
7272 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
7274 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7275 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
7276 seq_printf (seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
7279 seq_printf (seq
, "]");
7282 static void print_raid5_conf (struct r5conf
*conf
)
7285 struct disk_info
*tmp
;
7287 pr_debug("RAID conf printout:\n");
7289 pr_debug("(conf==NULL)\n");
7292 pr_debug(" --- level:%d rd:%d wd:%d\n", conf
->level
,
7294 conf
->raid_disks
- conf
->mddev
->degraded
);
7296 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7297 char b
[BDEVNAME_SIZE
];
7298 tmp
= conf
->disks
+ i
;
7300 pr_debug(" disk %d, o:%d, dev:%s\n",
7301 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
7302 bdevname(tmp
->rdev
->bdev
, b
));
7306 static int raid5_spare_active(struct mddev
*mddev
)
7309 struct r5conf
*conf
= mddev
->private;
7310 struct disk_info
*tmp
;
7312 unsigned long flags
;
7314 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7315 tmp
= conf
->disks
+ i
;
7316 if (tmp
->replacement
7317 && tmp
->replacement
->recovery_offset
== MaxSector
7318 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
7319 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
7320 /* Replacement has just become active. */
7322 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
7325 /* Replaced device not technically faulty,
7326 * but we need to be sure it gets removed
7327 * and never re-added.
7329 set_bit(Faulty
, &tmp
->rdev
->flags
);
7330 sysfs_notify_dirent_safe(
7331 tmp
->rdev
->sysfs_state
);
7333 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
7334 } else if (tmp
->rdev
7335 && tmp
->rdev
->recovery_offset
== MaxSector
7336 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
7337 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
7339 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
7342 spin_lock_irqsave(&conf
->device_lock
, flags
);
7343 mddev
->degraded
= raid5_calc_degraded(conf
);
7344 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7345 print_raid5_conf(conf
);
7349 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7351 struct r5conf
*conf
= mddev
->private;
7353 int number
= rdev
->raid_disk
;
7354 struct md_rdev
**rdevp
;
7355 struct disk_info
*p
= conf
->disks
+ number
;
7357 print_raid5_conf(conf
);
7358 if (test_bit(Journal
, &rdev
->flags
) && conf
->log
) {
7359 struct r5l_log
*log
;
7361 * we can't wait pending write here, as this is called in
7362 * raid5d, wait will deadlock.
7364 if (atomic_read(&mddev
->writes_pending
))
7372 if (rdev
== p
->rdev
)
7374 else if (rdev
== p
->replacement
)
7375 rdevp
= &p
->replacement
;
7379 if (number
>= conf
->raid_disks
&&
7380 conf
->reshape_progress
== MaxSector
)
7381 clear_bit(In_sync
, &rdev
->flags
);
7383 if (test_bit(In_sync
, &rdev
->flags
) ||
7384 atomic_read(&rdev
->nr_pending
)) {
7388 /* Only remove non-faulty devices if recovery
7391 if (!test_bit(Faulty
, &rdev
->flags
) &&
7392 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
7393 !has_failed(conf
) &&
7394 (!p
->replacement
|| p
->replacement
== rdev
) &&
7395 number
< conf
->raid_disks
) {
7400 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
7402 if (atomic_read(&rdev
->nr_pending
)) {
7403 /* lost the race, try later */
7408 if (p
->replacement
) {
7409 /* We must have just cleared 'rdev' */
7410 p
->rdev
= p
->replacement
;
7411 clear_bit(Replacement
, &p
->replacement
->flags
);
7412 smp_mb(); /* Make sure other CPUs may see both as identical
7413 * but will never see neither - if they are careful
7415 p
->replacement
= NULL
;
7416 clear_bit(WantReplacement
, &rdev
->flags
);
7418 /* We might have just removed the Replacement as faulty-
7419 * clear the bit just in case
7421 clear_bit(WantReplacement
, &rdev
->flags
);
7424 print_raid5_conf(conf
);
7428 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7430 struct r5conf
*conf
= mddev
->private;
7433 struct disk_info
*p
;
7435 int last
= conf
->raid_disks
- 1;
7437 if (test_bit(Journal
, &rdev
->flags
)) {
7438 char b
[BDEVNAME_SIZE
];
7442 rdev
->raid_disk
= 0;
7444 * The array is in readonly mode if journal is missing, so no
7445 * write requests running. We should be safe
7447 r5l_init_log(conf
, rdev
);
7448 pr_debug("md/raid:%s: using device %s as journal\n",
7449 mdname(mddev
), bdevname(rdev
->bdev
, b
));
7452 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
7455 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
7456 /* no point adding a device */
7459 if (rdev
->raid_disk
>= 0)
7460 first
= last
= rdev
->raid_disk
;
7463 * find the disk ... but prefer rdev->saved_raid_disk
7466 if (rdev
->saved_raid_disk
>= 0 &&
7467 rdev
->saved_raid_disk
>= first
&&
7468 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
7469 first
= rdev
->saved_raid_disk
;
7471 for (disk
= first
; disk
<= last
; disk
++) {
7472 p
= conf
->disks
+ disk
;
7473 if (p
->rdev
== NULL
) {
7474 clear_bit(In_sync
, &rdev
->flags
);
7475 rdev
->raid_disk
= disk
;
7477 if (rdev
->saved_raid_disk
!= disk
)
7479 rcu_assign_pointer(p
->rdev
, rdev
);
7483 for (disk
= first
; disk
<= last
; disk
++) {
7484 p
= conf
->disks
+ disk
;
7485 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
7486 p
->replacement
== NULL
) {
7487 clear_bit(In_sync
, &rdev
->flags
);
7488 set_bit(Replacement
, &rdev
->flags
);
7489 rdev
->raid_disk
= disk
;
7492 rcu_assign_pointer(p
->replacement
, rdev
);
7497 print_raid5_conf(conf
);
7501 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
7503 /* no resync is happening, and there is enough space
7504 * on all devices, so we can resize.
7505 * We need to make sure resync covers any new space.
7506 * If the array is shrinking we should possibly wait until
7507 * any io in the removed space completes, but it hardly seems
7511 struct r5conf
*conf
= mddev
->private;
7515 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
7516 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
7517 if (mddev
->external_size
&&
7518 mddev
->array_sectors
> newsize
)
7520 if (mddev
->bitmap
) {
7521 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
7525 md_set_array_sectors(mddev
, newsize
);
7526 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7527 revalidate_disk(mddev
->gendisk
);
7528 if (sectors
> mddev
->dev_sectors
&&
7529 mddev
->recovery_cp
> mddev
->dev_sectors
) {
7530 mddev
->recovery_cp
= mddev
->dev_sectors
;
7531 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7533 mddev
->dev_sectors
= sectors
;
7534 mddev
->resync_max_sectors
= sectors
;
7538 static int check_stripe_cache(struct mddev
*mddev
)
7540 /* Can only proceed if there are plenty of stripe_heads.
7541 * We need a minimum of one full stripe,, and for sensible progress
7542 * it is best to have about 4 times that.
7543 * If we require 4 times, then the default 256 4K stripe_heads will
7544 * allow for chunk sizes up to 256K, which is probably OK.
7545 * If the chunk size is greater, user-space should request more
7546 * stripe_heads first.
7548 struct r5conf
*conf
= mddev
->private;
7549 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7550 > conf
->min_nr_stripes
||
7551 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7552 > conf
->min_nr_stripes
) {
7553 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7555 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
7562 static int check_reshape(struct mddev
*mddev
)
7564 struct r5conf
*conf
= mddev
->private;
7568 if (mddev
->delta_disks
== 0 &&
7569 mddev
->new_layout
== mddev
->layout
&&
7570 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
7571 return 0; /* nothing to do */
7572 if (has_failed(conf
))
7574 if (mddev
->delta_disks
< 0 && mddev
->reshape_position
== MaxSector
) {
7575 /* We might be able to shrink, but the devices must
7576 * be made bigger first.
7577 * For raid6, 4 is the minimum size.
7578 * Otherwise 2 is the minimum
7581 if (mddev
->level
== 6)
7583 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
7587 if (!check_stripe_cache(mddev
))
7590 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
||
7591 mddev
->delta_disks
> 0)
7592 if (resize_chunks(conf
,
7593 conf
->previous_raid_disks
7594 + max(0, mddev
->delta_disks
),
7595 max(mddev
->new_chunk_sectors
,
7596 mddev
->chunk_sectors
)
7599 return resize_stripes(conf
, (conf
->previous_raid_disks
7600 + mddev
->delta_disks
));
7603 static int raid5_start_reshape(struct mddev
*mddev
)
7605 struct r5conf
*conf
= mddev
->private;
7606 struct md_rdev
*rdev
;
7608 unsigned long flags
;
7610 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
7613 if (!check_stripe_cache(mddev
))
7616 if (has_failed(conf
))
7619 rdev_for_each(rdev
, mddev
) {
7620 if (!test_bit(In_sync
, &rdev
->flags
)
7621 && !test_bit(Faulty
, &rdev
->flags
))
7625 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
7626 /* Not enough devices even to make a degraded array
7631 /* Refuse to reduce size of the array. Any reductions in
7632 * array size must be through explicit setting of array_size
7635 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
7636 < mddev
->array_sectors
) {
7637 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7642 atomic_set(&conf
->reshape_stripes
, 0);
7643 spin_lock_irq(&conf
->device_lock
);
7644 write_seqcount_begin(&conf
->gen_lock
);
7645 conf
->previous_raid_disks
= conf
->raid_disks
;
7646 conf
->raid_disks
+= mddev
->delta_disks
;
7647 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
7648 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
7649 conf
->prev_algo
= conf
->algorithm
;
7650 conf
->algorithm
= mddev
->new_layout
;
7652 /* Code that selects data_offset needs to see the generation update
7653 * if reshape_progress has been set - so a memory barrier needed.
7656 if (mddev
->reshape_backwards
)
7657 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
7659 conf
->reshape_progress
= 0;
7660 conf
->reshape_safe
= conf
->reshape_progress
;
7661 write_seqcount_end(&conf
->gen_lock
);
7662 spin_unlock_irq(&conf
->device_lock
);
7664 /* Now make sure any requests that proceeded on the assumption
7665 * the reshape wasn't running - like Discard or Read - have
7668 mddev_suspend(mddev
);
7669 mddev_resume(mddev
);
7671 /* Add some new drives, as many as will fit.
7672 * We know there are enough to make the newly sized array work.
7673 * Don't add devices if we are reducing the number of
7674 * devices in the array. This is because it is not possible
7675 * to correctly record the "partially reconstructed" state of
7676 * such devices during the reshape and confusion could result.
7678 if (mddev
->delta_disks
>= 0) {
7679 rdev_for_each(rdev
, mddev
)
7680 if (rdev
->raid_disk
< 0 &&
7681 !test_bit(Faulty
, &rdev
->flags
)) {
7682 if (raid5_add_disk(mddev
, rdev
) == 0) {
7684 >= conf
->previous_raid_disks
)
7685 set_bit(In_sync
, &rdev
->flags
);
7687 rdev
->recovery_offset
= 0;
7689 if (sysfs_link_rdev(mddev
, rdev
))
7690 /* Failure here is OK */;
7692 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
7693 && !test_bit(Faulty
, &rdev
->flags
)) {
7694 /* This is a spare that was manually added */
7695 set_bit(In_sync
, &rdev
->flags
);
7698 /* When a reshape changes the number of devices,
7699 * ->degraded is measured against the larger of the
7700 * pre and post number of devices.
7702 spin_lock_irqsave(&conf
->device_lock
, flags
);
7703 mddev
->degraded
= raid5_calc_degraded(conf
);
7704 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7706 mddev
->raid_disks
= conf
->raid_disks
;
7707 mddev
->reshape_position
= conf
->reshape_progress
;
7708 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7710 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7711 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7712 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7713 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7714 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7715 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7717 if (!mddev
->sync_thread
) {
7718 mddev
->recovery
= 0;
7719 spin_lock_irq(&conf
->device_lock
);
7720 write_seqcount_begin(&conf
->gen_lock
);
7721 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
7722 mddev
->new_chunk_sectors
=
7723 conf
->chunk_sectors
= conf
->prev_chunk_sectors
;
7724 mddev
->new_layout
= conf
->algorithm
= conf
->prev_algo
;
7725 rdev_for_each(rdev
, mddev
)
7726 rdev
->new_data_offset
= rdev
->data_offset
;
7728 conf
->generation
--;
7729 conf
->reshape_progress
= MaxSector
;
7730 mddev
->reshape_position
= MaxSector
;
7731 write_seqcount_end(&conf
->gen_lock
);
7732 spin_unlock_irq(&conf
->device_lock
);
7735 conf
->reshape_checkpoint
= jiffies
;
7736 md_wakeup_thread(mddev
->sync_thread
);
7737 md_new_event(mddev
);
7741 /* This is called from the reshape thread and should make any
7742 * changes needed in 'conf'
7744 static void end_reshape(struct r5conf
*conf
)
7747 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
7748 struct md_rdev
*rdev
;
7750 spin_lock_irq(&conf
->device_lock
);
7751 conf
->previous_raid_disks
= conf
->raid_disks
;
7752 rdev_for_each(rdev
, conf
->mddev
)
7753 rdev
->data_offset
= rdev
->new_data_offset
;
7755 conf
->reshape_progress
= MaxSector
;
7756 conf
->mddev
->reshape_position
= MaxSector
;
7757 spin_unlock_irq(&conf
->device_lock
);
7758 wake_up(&conf
->wait_for_overlap
);
7760 /* read-ahead size must cover two whole stripes, which is
7761 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7763 if (conf
->mddev
->queue
) {
7764 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
7765 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
7767 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
7768 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
7773 /* This is called from the raid5d thread with mddev_lock held.
7774 * It makes config changes to the device.
7776 static void raid5_finish_reshape(struct mddev
*mddev
)
7778 struct r5conf
*conf
= mddev
->private;
7780 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7782 if (mddev
->delta_disks
> 0) {
7783 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7785 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7786 revalidate_disk(mddev
->gendisk
);
7790 spin_lock_irq(&conf
->device_lock
);
7791 mddev
->degraded
= raid5_calc_degraded(conf
);
7792 spin_unlock_irq(&conf
->device_lock
);
7793 for (d
= conf
->raid_disks
;
7794 d
< conf
->raid_disks
- mddev
->delta_disks
;
7796 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
7798 clear_bit(In_sync
, &rdev
->flags
);
7799 rdev
= conf
->disks
[d
].replacement
;
7801 clear_bit(In_sync
, &rdev
->flags
);
7804 mddev
->layout
= conf
->algorithm
;
7805 mddev
->chunk_sectors
= conf
->chunk_sectors
;
7806 mddev
->reshape_position
= MaxSector
;
7807 mddev
->delta_disks
= 0;
7808 mddev
->reshape_backwards
= 0;
7812 static void raid5_quiesce(struct mddev
*mddev
, int state
)
7814 struct r5conf
*conf
= mddev
->private;
7817 case 2: /* resume for a suspend */
7818 wake_up(&conf
->wait_for_overlap
);
7821 case 1: /* stop all writes */
7822 lock_all_device_hash_locks_irq(conf
);
7823 /* '2' tells resync/reshape to pause so that all
7824 * active stripes can drain
7826 r5c_flush_cache(conf
, INT_MAX
);
7828 wait_event_cmd(conf
->wait_for_quiescent
,
7829 atomic_read(&conf
->active_stripes
) == 0 &&
7830 atomic_read(&conf
->active_aligned_reads
) == 0,
7831 unlock_all_device_hash_locks_irq(conf
),
7832 lock_all_device_hash_locks_irq(conf
));
7834 unlock_all_device_hash_locks_irq(conf
);
7835 /* allow reshape to continue */
7836 wake_up(&conf
->wait_for_overlap
);
7839 case 0: /* re-enable writes */
7840 lock_all_device_hash_locks_irq(conf
);
7842 wake_up(&conf
->wait_for_quiescent
);
7843 wake_up(&conf
->wait_for_overlap
);
7844 unlock_all_device_hash_locks_irq(conf
);
7847 r5l_quiesce(conf
->log
, state
);
7850 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
7852 struct r0conf
*raid0_conf
= mddev
->private;
7855 /* for raid0 takeover only one zone is supported */
7856 if (raid0_conf
->nr_strip_zones
> 1) {
7857 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7859 return ERR_PTR(-EINVAL
);
7862 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
7863 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
7864 mddev
->dev_sectors
= sectors
;
7865 mddev
->new_level
= level
;
7866 mddev
->new_layout
= ALGORITHM_PARITY_N
;
7867 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7868 mddev
->raid_disks
+= 1;
7869 mddev
->delta_disks
= 1;
7870 /* make sure it will be not marked as dirty */
7871 mddev
->recovery_cp
= MaxSector
;
7873 return setup_conf(mddev
);
7876 static void *raid5_takeover_raid1(struct mddev
*mddev
)
7881 if (mddev
->raid_disks
!= 2 ||
7882 mddev
->degraded
> 1)
7883 return ERR_PTR(-EINVAL
);
7885 /* Should check if there are write-behind devices? */
7887 chunksect
= 64*2; /* 64K by default */
7889 /* The array must be an exact multiple of chunksize */
7890 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
7893 if ((chunksect
<<9) < STRIPE_SIZE
)
7894 /* array size does not allow a suitable chunk size */
7895 return ERR_PTR(-EINVAL
);
7897 mddev
->new_level
= 5;
7898 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7899 mddev
->new_chunk_sectors
= chunksect
;
7901 ret
= setup_conf(mddev
);
7903 mddev_clear_unsupported_flags(mddev
,
7904 UNSUPPORTED_MDDEV_FLAGS
);
7908 static void *raid5_takeover_raid6(struct mddev
*mddev
)
7912 switch (mddev
->layout
) {
7913 case ALGORITHM_LEFT_ASYMMETRIC_6
:
7914 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
7916 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
7917 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
7919 case ALGORITHM_LEFT_SYMMETRIC_6
:
7920 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7922 case ALGORITHM_RIGHT_SYMMETRIC_6
:
7923 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
7925 case ALGORITHM_PARITY_0_6
:
7926 new_layout
= ALGORITHM_PARITY_0
;
7928 case ALGORITHM_PARITY_N
:
7929 new_layout
= ALGORITHM_PARITY_N
;
7932 return ERR_PTR(-EINVAL
);
7934 mddev
->new_level
= 5;
7935 mddev
->new_layout
= new_layout
;
7936 mddev
->delta_disks
= -1;
7937 mddev
->raid_disks
-= 1;
7938 return setup_conf(mddev
);
7941 static int raid5_check_reshape(struct mddev
*mddev
)
7943 /* For a 2-drive array, the layout and chunk size can be changed
7944 * immediately as not restriping is needed.
7945 * For larger arrays we record the new value - after validation
7946 * to be used by a reshape pass.
7948 struct r5conf
*conf
= mddev
->private;
7949 int new_chunk
= mddev
->new_chunk_sectors
;
7951 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
7953 if (new_chunk
> 0) {
7954 if (!is_power_of_2(new_chunk
))
7956 if (new_chunk
< (PAGE_SIZE
>>9))
7958 if (mddev
->array_sectors
& (new_chunk
-1))
7959 /* not factor of array size */
7963 /* They look valid */
7965 if (mddev
->raid_disks
== 2) {
7966 /* can make the change immediately */
7967 if (mddev
->new_layout
>= 0) {
7968 conf
->algorithm
= mddev
->new_layout
;
7969 mddev
->layout
= mddev
->new_layout
;
7971 if (new_chunk
> 0) {
7972 conf
->chunk_sectors
= new_chunk
;
7973 mddev
->chunk_sectors
= new_chunk
;
7975 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
7976 md_wakeup_thread(mddev
->thread
);
7978 return check_reshape(mddev
);
7981 static int raid6_check_reshape(struct mddev
*mddev
)
7983 int new_chunk
= mddev
->new_chunk_sectors
;
7985 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
7987 if (new_chunk
> 0) {
7988 if (!is_power_of_2(new_chunk
))
7990 if (new_chunk
< (PAGE_SIZE
>> 9))
7992 if (mddev
->array_sectors
& (new_chunk
-1))
7993 /* not factor of array size */
7997 /* They look valid */
7998 return check_reshape(mddev
);
8001 static void *raid5_takeover(struct mddev
*mddev
)
8003 /* raid5 can take over:
8004 * raid0 - if there is only one strip zone - make it a raid4 layout
8005 * raid1 - if there are two drives. We need to know the chunk size
8006 * raid4 - trivial - just use a raid4 layout.
8007 * raid6 - Providing it is a *_6 layout
8009 if (mddev
->level
== 0)
8010 return raid45_takeover_raid0(mddev
, 5);
8011 if (mddev
->level
== 1)
8012 return raid5_takeover_raid1(mddev
);
8013 if (mddev
->level
== 4) {
8014 mddev
->new_layout
= ALGORITHM_PARITY_N
;
8015 mddev
->new_level
= 5;
8016 return setup_conf(mddev
);
8018 if (mddev
->level
== 6)
8019 return raid5_takeover_raid6(mddev
);
8021 return ERR_PTR(-EINVAL
);
8024 static void *raid4_takeover(struct mddev
*mddev
)
8026 /* raid4 can take over:
8027 * raid0 - if there is only one strip zone
8028 * raid5 - if layout is right
8030 if (mddev
->level
== 0)
8031 return raid45_takeover_raid0(mddev
, 4);
8032 if (mddev
->level
== 5 &&
8033 mddev
->layout
== ALGORITHM_PARITY_N
) {
8034 mddev
->new_layout
= 0;
8035 mddev
->new_level
= 4;
8036 return setup_conf(mddev
);
8038 return ERR_PTR(-EINVAL
);
8041 static struct md_personality raid5_personality
;
8043 static void *raid6_takeover(struct mddev
*mddev
)
8045 /* Currently can only take over a raid5. We map the
8046 * personality to an equivalent raid6 personality
8047 * with the Q block at the end.
8051 if (mddev
->pers
!= &raid5_personality
)
8052 return ERR_PTR(-EINVAL
);
8053 if (mddev
->degraded
> 1)
8054 return ERR_PTR(-EINVAL
);
8055 if (mddev
->raid_disks
> 253)
8056 return ERR_PTR(-EINVAL
);
8057 if (mddev
->raid_disks
< 3)
8058 return ERR_PTR(-EINVAL
);
8060 switch (mddev
->layout
) {
8061 case ALGORITHM_LEFT_ASYMMETRIC
:
8062 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
8064 case ALGORITHM_RIGHT_ASYMMETRIC
:
8065 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
8067 case ALGORITHM_LEFT_SYMMETRIC
:
8068 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
8070 case ALGORITHM_RIGHT_SYMMETRIC
:
8071 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
8073 case ALGORITHM_PARITY_0
:
8074 new_layout
= ALGORITHM_PARITY_0_6
;
8076 case ALGORITHM_PARITY_N
:
8077 new_layout
= ALGORITHM_PARITY_N
;
8080 return ERR_PTR(-EINVAL
);
8082 mddev
->new_level
= 6;
8083 mddev
->new_layout
= new_layout
;
8084 mddev
->delta_disks
= 1;
8085 mddev
->raid_disks
+= 1;
8086 return setup_conf(mddev
);
8089 static struct md_personality raid6_personality
=
8093 .owner
= THIS_MODULE
,
8094 .make_request
= raid5_make_request
,
8097 .status
= raid5_status
,
8098 .error_handler
= raid5_error
,
8099 .hot_add_disk
= raid5_add_disk
,
8100 .hot_remove_disk
= raid5_remove_disk
,
8101 .spare_active
= raid5_spare_active
,
8102 .sync_request
= raid5_sync_request
,
8103 .resize
= raid5_resize
,
8105 .check_reshape
= raid6_check_reshape
,
8106 .start_reshape
= raid5_start_reshape
,
8107 .finish_reshape
= raid5_finish_reshape
,
8108 .quiesce
= raid5_quiesce
,
8109 .takeover
= raid6_takeover
,
8110 .congested
= raid5_congested
,
8112 static struct md_personality raid5_personality
=
8116 .owner
= THIS_MODULE
,
8117 .make_request
= raid5_make_request
,
8120 .status
= raid5_status
,
8121 .error_handler
= raid5_error
,
8122 .hot_add_disk
= raid5_add_disk
,
8123 .hot_remove_disk
= raid5_remove_disk
,
8124 .spare_active
= raid5_spare_active
,
8125 .sync_request
= raid5_sync_request
,
8126 .resize
= raid5_resize
,
8128 .check_reshape
= raid5_check_reshape
,
8129 .start_reshape
= raid5_start_reshape
,
8130 .finish_reshape
= raid5_finish_reshape
,
8131 .quiesce
= raid5_quiesce
,
8132 .takeover
= raid5_takeover
,
8133 .congested
= raid5_congested
,
8136 static struct md_personality raid4_personality
=
8140 .owner
= THIS_MODULE
,
8141 .make_request
= raid5_make_request
,
8144 .status
= raid5_status
,
8145 .error_handler
= raid5_error
,
8146 .hot_add_disk
= raid5_add_disk
,
8147 .hot_remove_disk
= raid5_remove_disk
,
8148 .spare_active
= raid5_spare_active
,
8149 .sync_request
= raid5_sync_request
,
8150 .resize
= raid5_resize
,
8152 .check_reshape
= raid5_check_reshape
,
8153 .start_reshape
= raid5_start_reshape
,
8154 .finish_reshape
= raid5_finish_reshape
,
8155 .quiesce
= raid5_quiesce
,
8156 .takeover
= raid4_takeover
,
8157 .congested
= raid5_congested
,
8160 static int __init
raid5_init(void)
8164 raid5_wq
= alloc_workqueue("raid5wq",
8165 WQ_UNBOUND
|WQ_MEM_RECLAIM
|WQ_CPU_INTENSIVE
|WQ_SYSFS
, 0);
8169 ret
= cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE
,
8171 raid456_cpu_up_prepare
,
8174 destroy_workqueue(raid5_wq
);
8177 register_md_personality(&raid6_personality
);
8178 register_md_personality(&raid5_personality
);
8179 register_md_personality(&raid4_personality
);
8183 static void raid5_exit(void)
8185 unregister_md_personality(&raid6_personality
);
8186 unregister_md_personality(&raid5_personality
);
8187 unregister_md_personality(&raid4_personality
);
8188 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE
);
8189 destroy_workqueue(raid5_wq
);
8192 module_init(raid5_init
);
8193 module_exit(raid5_exit
);
8194 MODULE_LICENSE("GPL");
8195 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
8196 MODULE_ALIAS("md-personality-4"); /* RAID5 */
8197 MODULE_ALIAS("md-raid5");
8198 MODULE_ALIAS("md-raid4");
8199 MODULE_ALIAS("md-level-5");
8200 MODULE_ALIAS("md-level-4");
8201 MODULE_ALIAS("md-personality-8"); /* RAID6 */
8202 MODULE_ALIAS("md-raid6");
8203 MODULE_ALIAS("md-level-6");
8205 /* This used to be two separate modules, they were: */
8206 MODULE_ALIAS("raid5");
8207 MODULE_ALIAS("raid6");