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 cpu_to_group(cpu) cpu_to_node(cpu)
66 #define ANY_GROUP NUMA_NO_NODE
68 static bool devices_handle_discard_safely
= false;
69 module_param(devices_handle_discard_safely
, bool, 0644);
70 MODULE_PARM_DESC(devices_handle_discard_safely
,
71 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
72 static struct workqueue_struct
*raid5_wq
;
74 static inline struct hlist_head
*stripe_hash(struct r5conf
*conf
, sector_t sect
)
76 int hash
= (sect
>> STRIPE_SHIFT
) & HASH_MASK
;
77 return &conf
->stripe_hashtbl
[hash
];
80 static inline int stripe_hash_locks_hash(sector_t sect
)
82 return (sect
>> STRIPE_SHIFT
) & STRIPE_HASH_LOCKS_MASK
;
85 static inline void lock_device_hash_lock(struct r5conf
*conf
, int hash
)
87 spin_lock_irq(conf
->hash_locks
+ hash
);
88 spin_lock(&conf
->device_lock
);
91 static inline void unlock_device_hash_lock(struct r5conf
*conf
, int hash
)
93 spin_unlock(&conf
->device_lock
);
94 spin_unlock_irq(conf
->hash_locks
+ hash
);
97 static inline void lock_all_device_hash_locks_irq(struct r5conf
*conf
)
101 spin_lock(conf
->hash_locks
);
102 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
103 spin_lock_nest_lock(conf
->hash_locks
+ i
, conf
->hash_locks
);
104 spin_lock(&conf
->device_lock
);
107 static inline void unlock_all_device_hash_locks_irq(struct r5conf
*conf
)
110 spin_unlock(&conf
->device_lock
);
111 for (i
= NR_STRIPE_HASH_LOCKS
; i
; i
--)
112 spin_unlock(conf
->hash_locks
+ i
- 1);
116 /* Find first data disk in a raid6 stripe */
117 static inline int raid6_d0(struct stripe_head
*sh
)
120 /* ddf always start from first device */
122 /* md starts just after Q block */
123 if (sh
->qd_idx
== sh
->disks
- 1)
126 return sh
->qd_idx
+ 1;
128 static inline int raid6_next_disk(int disk
, int raid_disks
)
131 return (disk
< raid_disks
) ? disk
: 0;
134 /* When walking through the disks in a raid5, starting at raid6_d0,
135 * We need to map each disk to a 'slot', where the data disks are slot
136 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
137 * is raid_disks-1. This help does that mapping.
139 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
140 int *count
, int syndrome_disks
)
146 if (idx
== sh
->pd_idx
)
147 return syndrome_disks
;
148 if (idx
== sh
->qd_idx
)
149 return syndrome_disks
+ 1;
155 static void return_io(struct bio_list
*return_bi
)
158 while ((bi
= bio_list_pop(return_bi
)) != NULL
) {
159 bi
->bi_iter
.bi_size
= 0;
160 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
166 static void print_raid5_conf (struct r5conf
*conf
);
168 static int stripe_operations_active(struct stripe_head
*sh
)
170 return sh
->check_state
|| sh
->reconstruct_state
||
171 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
172 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
175 static void raid5_wakeup_stripe_thread(struct stripe_head
*sh
)
177 struct r5conf
*conf
= sh
->raid_conf
;
178 struct r5worker_group
*group
;
180 int i
, cpu
= sh
->cpu
;
182 if (!cpu_online(cpu
)) {
183 cpu
= cpumask_any(cpu_online_mask
);
187 if (list_empty(&sh
->lru
)) {
188 struct r5worker_group
*group
;
189 group
= conf
->worker_groups
+ cpu_to_group(cpu
);
190 list_add_tail(&sh
->lru
, &group
->handle_list
);
191 group
->stripes_cnt
++;
195 if (conf
->worker_cnt_per_group
== 0) {
196 md_wakeup_thread(conf
->mddev
->thread
);
200 group
= conf
->worker_groups
+ cpu_to_group(sh
->cpu
);
202 group
->workers
[0].working
= true;
203 /* at least one worker should run to avoid race */
204 queue_work_on(sh
->cpu
, raid5_wq
, &group
->workers
[0].work
);
206 thread_cnt
= group
->stripes_cnt
/ MAX_STRIPE_BATCH
- 1;
207 /* wakeup more workers */
208 for (i
= 1; i
< conf
->worker_cnt_per_group
&& thread_cnt
> 0; i
++) {
209 if (group
->workers
[i
].working
== false) {
210 group
->workers
[i
].working
= true;
211 queue_work_on(sh
->cpu
, raid5_wq
,
212 &group
->workers
[i
].work
);
218 static void do_release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
219 struct list_head
*temp_inactive_list
)
222 int injournal
= 0; /* number of date pages with R5_InJournal */
224 BUG_ON(!list_empty(&sh
->lru
));
225 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
227 if (r5c_is_writeback(conf
->log
))
228 for (i
= sh
->disks
; i
--; )
229 if (test_bit(R5_InJournal
, &sh
->dev
[i
].flags
))
232 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
233 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
234 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
235 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
236 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
237 sh
->bm_seq
- conf
->seq_write
> 0)
238 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
240 clear_bit(STRIPE_DELAYED
, &sh
->state
);
241 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
242 if (conf
->worker_cnt_per_group
== 0) {
243 list_add_tail(&sh
->lru
, &conf
->handle_list
);
245 raid5_wakeup_stripe_thread(sh
);
249 md_wakeup_thread(conf
->mddev
->thread
);
251 BUG_ON(stripe_operations_active(sh
));
252 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
253 if (atomic_dec_return(&conf
->preread_active_stripes
)
255 md_wakeup_thread(conf
->mddev
->thread
);
256 atomic_dec(&conf
->active_stripes
);
257 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
258 if (!r5c_is_writeback(conf
->log
))
259 list_add_tail(&sh
->lru
, temp_inactive_list
);
261 WARN_ON(test_bit(R5_InJournal
, &sh
->dev
[sh
->pd_idx
].flags
));
263 list_add_tail(&sh
->lru
, temp_inactive_list
);
264 else if (injournal
== conf
->raid_disks
- conf
->max_degraded
) {
266 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE
, &sh
->state
))
267 atomic_inc(&conf
->r5c_cached_full_stripes
);
268 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE
, &sh
->state
))
269 atomic_dec(&conf
->r5c_cached_partial_stripes
);
270 list_add_tail(&sh
->lru
, &conf
->r5c_full_stripe_list
);
273 if (!test_and_set_bit(STRIPE_R5C_PARTIAL_STRIPE
,
275 atomic_inc(&conf
->r5c_cached_partial_stripes
);
276 list_add_tail(&sh
->lru
, &conf
->r5c_partial_stripe_list
);
283 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
284 struct list_head
*temp_inactive_list
)
286 if (atomic_dec_and_test(&sh
->count
))
287 do_release_stripe(conf
, sh
, temp_inactive_list
);
291 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
293 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
294 * given time. Adding stripes only takes device lock, while deleting stripes
295 * only takes hash lock.
297 static void release_inactive_stripe_list(struct r5conf
*conf
,
298 struct list_head
*temp_inactive_list
,
302 bool do_wakeup
= false;
305 if (hash
== NR_STRIPE_HASH_LOCKS
) {
306 size
= NR_STRIPE_HASH_LOCKS
;
307 hash
= NR_STRIPE_HASH_LOCKS
- 1;
311 struct list_head
*list
= &temp_inactive_list
[size
- 1];
314 * We don't hold any lock here yet, raid5_get_active_stripe() might
315 * remove stripes from the list
317 if (!list_empty_careful(list
)) {
318 spin_lock_irqsave(conf
->hash_locks
+ hash
, flags
);
319 if (list_empty(conf
->inactive_list
+ hash
) &&
321 atomic_dec(&conf
->empty_inactive_list_nr
);
322 list_splice_tail_init(list
, conf
->inactive_list
+ hash
);
324 spin_unlock_irqrestore(conf
->hash_locks
+ hash
, flags
);
331 wake_up(&conf
->wait_for_stripe
);
332 if (atomic_read(&conf
->active_stripes
) == 0)
333 wake_up(&conf
->wait_for_quiescent
);
334 if (conf
->retry_read_aligned
)
335 md_wakeup_thread(conf
->mddev
->thread
);
339 /* should hold conf->device_lock already */
340 static int release_stripe_list(struct r5conf
*conf
,
341 struct list_head
*temp_inactive_list
)
343 struct stripe_head
*sh
;
345 struct llist_node
*head
;
347 head
= llist_del_all(&conf
->released_stripes
);
348 head
= llist_reverse_order(head
);
352 sh
= llist_entry(head
, struct stripe_head
, release_list
);
353 head
= llist_next(head
);
354 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
356 clear_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
);
358 * Don't worry the bit is set here, because if the bit is set
359 * again, the count is always > 1. This is true for
360 * STRIPE_ON_UNPLUG_LIST bit too.
362 hash
= sh
->hash_lock_index
;
363 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
370 void raid5_release_stripe(struct stripe_head
*sh
)
372 struct r5conf
*conf
= sh
->raid_conf
;
374 struct list_head list
;
378 /* Avoid release_list until the last reference.
380 if (atomic_add_unless(&sh
->count
, -1, 1))
383 if (unlikely(!conf
->mddev
->thread
) ||
384 test_and_set_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
))
386 wakeup
= llist_add(&sh
->release_list
, &conf
->released_stripes
);
388 md_wakeup_thread(conf
->mddev
->thread
);
391 local_irq_save(flags
);
392 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
393 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
394 INIT_LIST_HEAD(&list
);
395 hash
= sh
->hash_lock_index
;
396 do_release_stripe(conf
, sh
, &list
);
397 spin_unlock(&conf
->device_lock
);
398 release_inactive_stripe_list(conf
, &list
, hash
);
400 local_irq_restore(flags
);
403 static inline void remove_hash(struct stripe_head
*sh
)
405 pr_debug("remove_hash(), stripe %llu\n",
406 (unsigned long long)sh
->sector
);
408 hlist_del_init(&sh
->hash
);
411 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
413 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
415 pr_debug("insert_hash(), stripe %llu\n",
416 (unsigned long long)sh
->sector
);
418 hlist_add_head(&sh
->hash
, hp
);
421 /* find an idle stripe, make sure it is unhashed, and return it. */
422 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
, int hash
)
424 struct stripe_head
*sh
= NULL
;
425 struct list_head
*first
;
427 if (list_empty(conf
->inactive_list
+ hash
))
429 first
= (conf
->inactive_list
+ hash
)->next
;
430 sh
= list_entry(first
, struct stripe_head
, lru
);
431 list_del_init(first
);
433 atomic_inc(&conf
->active_stripes
);
434 BUG_ON(hash
!= sh
->hash_lock_index
);
435 if (list_empty(conf
->inactive_list
+ hash
))
436 atomic_inc(&conf
->empty_inactive_list_nr
);
441 static void shrink_buffers(struct stripe_head
*sh
)
445 int num
= sh
->raid_conf
->pool_size
;
447 for (i
= 0; i
< num
; i
++) {
448 WARN_ON(sh
->dev
[i
].page
!= sh
->dev
[i
].orig_page
);
452 sh
->dev
[i
].page
= NULL
;
457 static int grow_buffers(struct stripe_head
*sh
, gfp_t gfp
)
460 int num
= sh
->raid_conf
->pool_size
;
462 for (i
= 0; i
< num
; i
++) {
465 if (!(page
= alloc_page(gfp
))) {
468 sh
->dev
[i
].page
= page
;
469 sh
->dev
[i
].orig_page
= page
;
474 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
475 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
476 struct stripe_head
*sh
);
478 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
480 struct r5conf
*conf
= sh
->raid_conf
;
483 BUG_ON(atomic_read(&sh
->count
) != 0);
484 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
485 BUG_ON(stripe_operations_active(sh
));
486 BUG_ON(sh
->batch_head
);
488 pr_debug("init_stripe called, stripe %llu\n",
489 (unsigned long long)sector
);
491 seq
= read_seqcount_begin(&conf
->gen_lock
);
492 sh
->generation
= conf
->generation
- previous
;
493 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
495 stripe_set_idx(sector
, conf
, previous
, sh
);
498 for (i
= sh
->disks
; i
--; ) {
499 struct r5dev
*dev
= &sh
->dev
[i
];
501 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
502 test_bit(R5_LOCKED
, &dev
->flags
)) {
503 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
504 (unsigned long long)sh
->sector
, i
, dev
->toread
,
505 dev
->read
, dev
->towrite
, dev
->written
,
506 test_bit(R5_LOCKED
, &dev
->flags
));
510 raid5_build_block(sh
, i
, previous
);
512 if (read_seqcount_retry(&conf
->gen_lock
, seq
))
514 sh
->overwrite_disks
= 0;
515 insert_hash(conf
, sh
);
516 sh
->cpu
= smp_processor_id();
517 set_bit(STRIPE_BATCH_READY
, &sh
->state
);
520 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
523 struct stripe_head
*sh
;
525 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
526 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
527 if (sh
->sector
== sector
&& sh
->generation
== generation
)
529 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
534 * Need to check if array has failed when deciding whether to:
536 * - remove non-faulty devices
539 * This determination is simple when no reshape is happening.
540 * However if there is a reshape, we need to carefully check
541 * both the before and after sections.
542 * This is because some failed devices may only affect one
543 * of the two sections, and some non-in_sync devices may
544 * be insync in the section most affected by failed devices.
546 static int calc_degraded(struct r5conf
*conf
)
548 int degraded
, degraded2
;
553 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
554 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
555 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
556 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
557 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
559 else if (test_bit(In_sync
, &rdev
->flags
))
562 /* not in-sync or faulty.
563 * If the reshape increases the number of devices,
564 * this is being recovered by the reshape, so
565 * this 'previous' section is not in_sync.
566 * If the number of devices is being reduced however,
567 * the device can only be part of the array if
568 * we are reverting a reshape, so this section will
571 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
575 if (conf
->raid_disks
== conf
->previous_raid_disks
)
579 for (i
= 0; i
< conf
->raid_disks
; i
++) {
580 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
581 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
582 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
583 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
585 else if (test_bit(In_sync
, &rdev
->flags
))
588 /* not in-sync or faulty.
589 * If reshape increases the number of devices, this
590 * section has already been recovered, else it
591 * almost certainly hasn't.
593 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
597 if (degraded2
> degraded
)
602 static int has_failed(struct r5conf
*conf
)
606 if (conf
->mddev
->reshape_position
== MaxSector
)
607 return conf
->mddev
->degraded
> conf
->max_degraded
;
609 degraded
= calc_degraded(conf
);
610 if (degraded
> conf
->max_degraded
)
616 raid5_get_active_stripe(struct r5conf
*conf
, sector_t sector
,
617 int previous
, int noblock
, int noquiesce
)
619 struct stripe_head
*sh
;
620 int hash
= stripe_hash_locks_hash(sector
);
621 int inc_empty_inactive_list_flag
;
623 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
625 spin_lock_irq(conf
->hash_locks
+ hash
);
628 wait_event_lock_irq(conf
->wait_for_quiescent
,
629 conf
->quiesce
== 0 || noquiesce
,
630 *(conf
->hash_locks
+ hash
));
631 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
633 if (!test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
)) {
634 sh
= get_free_stripe(conf
, hash
);
635 if (!sh
&& !test_bit(R5_DID_ALLOC
,
637 set_bit(R5_ALLOC_MORE
,
640 if (noblock
&& sh
== NULL
)
643 set_bit(R5_INACTIVE_BLOCKED
,
646 conf
->wait_for_stripe
,
647 !list_empty(conf
->inactive_list
+ hash
) &&
648 (atomic_read(&conf
->active_stripes
)
649 < (conf
->max_nr_stripes
* 3 / 4)
650 || !test_bit(R5_INACTIVE_BLOCKED
,
651 &conf
->cache_state
)),
652 *(conf
->hash_locks
+ hash
));
653 clear_bit(R5_INACTIVE_BLOCKED
,
656 init_stripe(sh
, sector
, previous
);
657 atomic_inc(&sh
->count
);
659 } else if (!atomic_inc_not_zero(&sh
->count
)) {
660 spin_lock(&conf
->device_lock
);
661 if (!atomic_read(&sh
->count
)) {
662 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
663 atomic_inc(&conf
->active_stripes
);
664 BUG_ON(list_empty(&sh
->lru
) &&
665 !test_bit(STRIPE_EXPANDING
, &sh
->state
));
666 inc_empty_inactive_list_flag
= 0;
667 if (!list_empty(conf
->inactive_list
+ hash
))
668 inc_empty_inactive_list_flag
= 1;
669 list_del_init(&sh
->lru
);
670 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
671 atomic_inc(&conf
->empty_inactive_list_nr
);
673 sh
->group
->stripes_cnt
--;
677 atomic_inc(&sh
->count
);
678 spin_unlock(&conf
->device_lock
);
680 } while (sh
== NULL
);
682 spin_unlock_irq(conf
->hash_locks
+ hash
);
686 static bool is_full_stripe_write(struct stripe_head
*sh
)
688 BUG_ON(sh
->overwrite_disks
> (sh
->disks
- sh
->raid_conf
->max_degraded
));
689 return sh
->overwrite_disks
== (sh
->disks
- sh
->raid_conf
->max_degraded
);
692 static void lock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
696 spin_lock(&sh2
->stripe_lock
);
697 spin_lock_nested(&sh1
->stripe_lock
, 1);
699 spin_lock(&sh1
->stripe_lock
);
700 spin_lock_nested(&sh2
->stripe_lock
, 1);
704 static void unlock_two_stripes(struct stripe_head
*sh1
, struct stripe_head
*sh2
)
706 spin_unlock(&sh1
->stripe_lock
);
707 spin_unlock(&sh2
->stripe_lock
);
711 /* Only freshly new full stripe normal write stripe can be added to a batch list */
712 static bool stripe_can_batch(struct stripe_head
*sh
)
714 struct r5conf
*conf
= sh
->raid_conf
;
718 return test_bit(STRIPE_BATCH_READY
, &sh
->state
) &&
719 !test_bit(STRIPE_BITMAP_PENDING
, &sh
->state
) &&
720 is_full_stripe_write(sh
);
723 /* we only do back search */
724 static void stripe_add_to_batch_list(struct r5conf
*conf
, struct stripe_head
*sh
)
726 struct stripe_head
*head
;
727 sector_t head_sector
, tmp_sec
;
730 int inc_empty_inactive_list_flag
;
732 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
733 tmp_sec
= sh
->sector
;
734 if (!sector_div(tmp_sec
, conf
->chunk_sectors
))
736 head_sector
= sh
->sector
- STRIPE_SECTORS
;
738 hash
= stripe_hash_locks_hash(head_sector
);
739 spin_lock_irq(conf
->hash_locks
+ hash
);
740 head
= __find_stripe(conf
, head_sector
, conf
->generation
);
741 if (head
&& !atomic_inc_not_zero(&head
->count
)) {
742 spin_lock(&conf
->device_lock
);
743 if (!atomic_read(&head
->count
)) {
744 if (!test_bit(STRIPE_HANDLE
, &head
->state
))
745 atomic_inc(&conf
->active_stripes
);
746 BUG_ON(list_empty(&head
->lru
) &&
747 !test_bit(STRIPE_EXPANDING
, &head
->state
));
748 inc_empty_inactive_list_flag
= 0;
749 if (!list_empty(conf
->inactive_list
+ hash
))
750 inc_empty_inactive_list_flag
= 1;
751 list_del_init(&head
->lru
);
752 if (list_empty(conf
->inactive_list
+ hash
) && inc_empty_inactive_list_flag
)
753 atomic_inc(&conf
->empty_inactive_list_nr
);
755 head
->group
->stripes_cnt
--;
759 atomic_inc(&head
->count
);
760 spin_unlock(&conf
->device_lock
);
762 spin_unlock_irq(conf
->hash_locks
+ hash
);
766 if (!stripe_can_batch(head
))
769 lock_two_stripes(head
, sh
);
770 /* clear_batch_ready clear the flag */
771 if (!stripe_can_batch(head
) || !stripe_can_batch(sh
))
778 while (dd_idx
== sh
->pd_idx
|| dd_idx
== sh
->qd_idx
)
780 if (head
->dev
[dd_idx
].towrite
->bi_opf
!= sh
->dev
[dd_idx
].towrite
->bi_opf
||
781 bio_op(head
->dev
[dd_idx
].towrite
) != bio_op(sh
->dev
[dd_idx
].towrite
))
784 if (head
->batch_head
) {
785 spin_lock(&head
->batch_head
->batch_lock
);
786 /* This batch list is already running */
787 if (!stripe_can_batch(head
)) {
788 spin_unlock(&head
->batch_head
->batch_lock
);
793 * at this point, head's BATCH_READY could be cleared, but we
794 * can still add the stripe to batch list
796 list_add(&sh
->batch_list
, &head
->batch_list
);
797 spin_unlock(&head
->batch_head
->batch_lock
);
799 sh
->batch_head
= head
->batch_head
;
801 head
->batch_head
= head
;
802 sh
->batch_head
= head
->batch_head
;
803 spin_lock(&head
->batch_lock
);
804 list_add_tail(&sh
->batch_list
, &head
->batch_list
);
805 spin_unlock(&head
->batch_lock
);
808 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
809 if (atomic_dec_return(&conf
->preread_active_stripes
)
811 md_wakeup_thread(conf
->mddev
->thread
);
813 if (test_and_clear_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
814 int seq
= sh
->bm_seq
;
815 if (test_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
) &&
816 sh
->batch_head
->bm_seq
> seq
)
817 seq
= sh
->batch_head
->bm_seq
;
818 set_bit(STRIPE_BIT_DELAY
, &sh
->batch_head
->state
);
819 sh
->batch_head
->bm_seq
= seq
;
822 atomic_inc(&sh
->count
);
824 unlock_two_stripes(head
, sh
);
826 raid5_release_stripe(head
);
829 /* Determine if 'data_offset' or 'new_data_offset' should be used
830 * in this stripe_head.
832 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
834 sector_t progress
= conf
->reshape_progress
;
835 /* Need a memory barrier to make sure we see the value
836 * of conf->generation, or ->data_offset that was set before
837 * reshape_progress was updated.
840 if (progress
== MaxSector
)
842 if (sh
->generation
== conf
->generation
- 1)
844 /* We are in a reshape, and this is a new-generation stripe,
845 * so use new_data_offset.
851 raid5_end_read_request(struct bio
*bi
);
853 raid5_end_write_request(struct bio
*bi
);
855 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
857 struct r5conf
*conf
= sh
->raid_conf
;
858 int i
, disks
= sh
->disks
;
859 struct stripe_head
*head_sh
= sh
;
863 if (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
)) {
864 /* writing out phase */
865 if (r5l_write_stripe(conf
->log
, sh
) == 0)
867 } else { /* caching phase */
868 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
)) {
869 r5c_cache_data(conf
->log
, sh
, s
);
874 for (i
= disks
; i
--; ) {
875 int op
, op_flags
= 0;
876 int replace_only
= 0;
877 struct bio
*bi
, *rbi
;
878 struct md_rdev
*rdev
, *rrdev
= NULL
;
881 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
883 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
884 op_flags
= WRITE_FUA
;
885 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
887 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
889 else if (test_and_clear_bit(R5_WantReplace
,
890 &sh
->dev
[i
].flags
)) {
895 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
896 op_flags
|= REQ_SYNC
;
899 bi
= &sh
->dev
[i
].req
;
900 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
903 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
904 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
905 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
910 if (op_is_write(op
)) {
914 /* We raced and saw duplicates */
917 if (test_bit(R5_ReadRepl
, &head_sh
->dev
[i
].flags
) && rrdev
)
922 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
925 atomic_inc(&rdev
->nr_pending
);
926 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
929 atomic_inc(&rrdev
->nr_pending
);
932 /* We have already checked bad blocks for reads. Now
933 * need to check for writes. We never accept write errors
934 * on the replacement, so we don't to check rrdev.
936 while (op_is_write(op
) && rdev
&&
937 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
940 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
941 &first_bad
, &bad_sectors
);
946 set_bit(BlockedBadBlocks
, &rdev
->flags
);
947 if (!conf
->mddev
->external
&&
948 conf
->mddev
->flags
) {
949 /* It is very unlikely, but we might
950 * still need to write out the
951 * bad block log - better give it
953 md_check_recovery(conf
->mddev
);
956 * Because md_wait_for_blocked_rdev
957 * will dec nr_pending, we must
958 * increment it first.
960 atomic_inc(&rdev
->nr_pending
);
961 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
963 /* Acknowledged bad block - skip the write */
964 rdev_dec_pending(rdev
, conf
->mddev
);
970 if (s
->syncing
|| s
->expanding
|| s
->expanded
972 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
974 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
976 bi
->bi_bdev
= rdev
->bdev
;
977 bio_set_op_attrs(bi
, op
, op_flags
);
978 bi
->bi_end_io
= op_is_write(op
)
979 ? raid5_end_write_request
980 : raid5_end_read_request
;
983 pr_debug("%s: for %llu schedule op %d on disc %d\n",
984 __func__
, (unsigned long long)sh
->sector
,
986 atomic_inc(&sh
->count
);
988 atomic_inc(&head_sh
->count
);
989 if (use_new_offset(conf
, sh
))
990 bi
->bi_iter
.bi_sector
= (sh
->sector
991 + rdev
->new_data_offset
);
993 bi
->bi_iter
.bi_sector
= (sh
->sector
994 + rdev
->data_offset
);
995 if (test_bit(R5_ReadNoMerge
, &head_sh
->dev
[i
].flags
))
996 bi
->bi_opf
|= REQ_NOMERGE
;
998 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
999 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1000 sh
->dev
[i
].vec
.bv_page
= sh
->dev
[i
].page
;
1002 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1003 bi
->bi_io_vec
[0].bv_offset
= 0;
1004 bi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1006 * If this is discard request, set bi_vcnt 0. We don't
1007 * want to confuse SCSI because SCSI will replace payload
1009 if (op
== REQ_OP_DISCARD
)
1012 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
1014 if (conf
->mddev
->gendisk
)
1015 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
1016 bi
, disk_devt(conf
->mddev
->gendisk
),
1018 generic_make_request(bi
);
1021 if (s
->syncing
|| s
->expanding
|| s
->expanded
1023 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
1025 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
1027 rbi
->bi_bdev
= rrdev
->bdev
;
1028 bio_set_op_attrs(rbi
, op
, op_flags
);
1029 BUG_ON(!op_is_write(op
));
1030 rbi
->bi_end_io
= raid5_end_write_request
;
1031 rbi
->bi_private
= sh
;
1033 pr_debug("%s: for %llu schedule op %d on "
1034 "replacement disc %d\n",
1035 __func__
, (unsigned long long)sh
->sector
,
1037 atomic_inc(&sh
->count
);
1039 atomic_inc(&head_sh
->count
);
1040 if (use_new_offset(conf
, sh
))
1041 rbi
->bi_iter
.bi_sector
= (sh
->sector
1042 + rrdev
->new_data_offset
);
1044 rbi
->bi_iter
.bi_sector
= (sh
->sector
1045 + rrdev
->data_offset
);
1046 if (test_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
))
1047 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
1048 sh
->dev
[i
].rvec
.bv_page
= sh
->dev
[i
].page
;
1050 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1051 rbi
->bi_io_vec
[0].bv_offset
= 0;
1052 rbi
->bi_iter
.bi_size
= STRIPE_SIZE
;
1054 * If this is discard request, set bi_vcnt 0. We don't
1055 * want to confuse SCSI because SCSI will replace payload
1057 if (op
== REQ_OP_DISCARD
)
1059 if (conf
->mddev
->gendisk
)
1060 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
1061 rbi
, disk_devt(conf
->mddev
->gendisk
),
1063 generic_make_request(rbi
);
1065 if (!rdev
&& !rrdev
) {
1066 if (op_is_write(op
))
1067 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1068 pr_debug("skip op %d on disc %d for sector %llu\n",
1069 bi
->bi_opf
, i
, (unsigned long long)sh
->sector
);
1070 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1071 set_bit(STRIPE_HANDLE
, &sh
->state
);
1074 if (!head_sh
->batch_head
)
1076 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1083 static struct dma_async_tx_descriptor
*
1084 async_copy_data(int frombio
, struct bio
*bio
, struct page
**page
,
1085 sector_t sector
, struct dma_async_tx_descriptor
*tx
,
1086 struct stripe_head
*sh
, int no_skipcopy
)
1089 struct bvec_iter iter
;
1090 struct page
*bio_page
;
1092 struct async_submit_ctl submit
;
1093 enum async_tx_flags flags
= 0;
1095 if (bio
->bi_iter
.bi_sector
>= sector
)
1096 page_offset
= (signed)(bio
->bi_iter
.bi_sector
- sector
) * 512;
1098 page_offset
= (signed)(sector
- bio
->bi_iter
.bi_sector
) * -512;
1101 flags
|= ASYNC_TX_FENCE
;
1102 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
1104 bio_for_each_segment(bvl
, bio
, iter
) {
1105 int len
= bvl
.bv_len
;
1109 if (page_offset
< 0) {
1110 b_offset
= -page_offset
;
1111 page_offset
+= b_offset
;
1115 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
1116 clen
= STRIPE_SIZE
- page_offset
;
1121 b_offset
+= bvl
.bv_offset
;
1122 bio_page
= bvl
.bv_page
;
1124 if (sh
->raid_conf
->skip_copy
&&
1125 b_offset
== 0 && page_offset
== 0 &&
1126 clen
== STRIPE_SIZE
&&
1130 tx
= async_memcpy(*page
, bio_page
, page_offset
,
1131 b_offset
, clen
, &submit
);
1133 tx
= async_memcpy(bio_page
, *page
, b_offset
,
1134 page_offset
, clen
, &submit
);
1136 /* chain the operations */
1137 submit
.depend_tx
= tx
;
1139 if (clen
< len
) /* hit end of page */
1147 static void ops_complete_biofill(void *stripe_head_ref
)
1149 struct stripe_head
*sh
= stripe_head_ref
;
1150 struct bio_list return_bi
= BIO_EMPTY_LIST
;
1153 pr_debug("%s: stripe %llu\n", __func__
,
1154 (unsigned long long)sh
->sector
);
1156 /* clear completed biofills */
1157 for (i
= sh
->disks
; i
--; ) {
1158 struct r5dev
*dev
= &sh
->dev
[i
];
1160 /* acknowledge completion of a biofill operation */
1161 /* and check if we need to reply to a read request,
1162 * new R5_Wantfill requests are held off until
1163 * !STRIPE_BIOFILL_RUN
1165 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
1166 struct bio
*rbi
, *rbi2
;
1171 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1172 dev
->sector
+ STRIPE_SECTORS
) {
1173 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1174 if (!raid5_dec_bi_active_stripes(rbi
))
1175 bio_list_add(&return_bi
, rbi
);
1180 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
1182 return_io(&return_bi
);
1184 set_bit(STRIPE_HANDLE
, &sh
->state
);
1185 raid5_release_stripe(sh
);
1188 static void ops_run_biofill(struct stripe_head
*sh
)
1190 struct dma_async_tx_descriptor
*tx
= NULL
;
1191 struct async_submit_ctl submit
;
1194 BUG_ON(sh
->batch_head
);
1195 pr_debug("%s: stripe %llu\n", __func__
,
1196 (unsigned long long)sh
->sector
);
1198 for (i
= sh
->disks
; i
--; ) {
1199 struct r5dev
*dev
= &sh
->dev
[i
];
1200 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
1202 spin_lock_irq(&sh
->stripe_lock
);
1203 dev
->read
= rbi
= dev
->toread
;
1205 spin_unlock_irq(&sh
->stripe_lock
);
1206 while (rbi
&& rbi
->bi_iter
.bi_sector
<
1207 dev
->sector
+ STRIPE_SECTORS
) {
1208 tx
= async_copy_data(0, rbi
, &dev
->page
,
1209 dev
->sector
, tx
, sh
, 0);
1210 rbi
= r5_next_bio(rbi
, dev
->sector
);
1215 atomic_inc(&sh
->count
);
1216 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
1217 async_trigger_callback(&submit
);
1220 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
1227 tgt
= &sh
->dev
[target
];
1228 set_bit(R5_UPTODATE
, &tgt
->flags
);
1229 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1230 clear_bit(R5_Wantcompute
, &tgt
->flags
);
1233 static void ops_complete_compute(void *stripe_head_ref
)
1235 struct stripe_head
*sh
= stripe_head_ref
;
1237 pr_debug("%s: stripe %llu\n", __func__
,
1238 (unsigned long long)sh
->sector
);
1240 /* mark the computed target(s) as uptodate */
1241 mark_target_uptodate(sh
, sh
->ops
.target
);
1242 mark_target_uptodate(sh
, sh
->ops
.target2
);
1244 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
1245 if (sh
->check_state
== check_state_compute_run
)
1246 sh
->check_state
= check_state_compute_result
;
1247 set_bit(STRIPE_HANDLE
, &sh
->state
);
1248 raid5_release_stripe(sh
);
1251 /* return a pointer to the address conversion region of the scribble buffer */
1252 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
1253 struct raid5_percpu
*percpu
, int i
)
1257 addr
= flex_array_get(percpu
->scribble
, i
);
1258 return addr
+ sizeof(struct page
*) * (sh
->disks
+ 2);
1261 /* return a pointer to the address conversion region of the scribble buffer */
1262 static struct page
**to_addr_page(struct raid5_percpu
*percpu
, int i
)
1266 addr
= flex_array_get(percpu
->scribble
, i
);
1270 static struct dma_async_tx_descriptor
*
1271 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1273 int disks
= sh
->disks
;
1274 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1275 int target
= sh
->ops
.target
;
1276 struct r5dev
*tgt
= &sh
->dev
[target
];
1277 struct page
*xor_dest
= tgt
->page
;
1279 struct dma_async_tx_descriptor
*tx
;
1280 struct async_submit_ctl submit
;
1283 BUG_ON(sh
->batch_head
);
1285 pr_debug("%s: stripe %llu block: %d\n",
1286 __func__
, (unsigned long long)sh
->sector
, target
);
1287 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1289 for (i
= disks
; i
--; )
1291 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1293 atomic_inc(&sh
->count
);
1295 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
1296 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
, 0));
1297 if (unlikely(count
== 1))
1298 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1300 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1305 /* set_syndrome_sources - populate source buffers for gen_syndrome
1306 * @srcs - (struct page *) array of size sh->disks
1307 * @sh - stripe_head to parse
1309 * Populates srcs in proper layout order for the stripe and returns the
1310 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1311 * destination buffer is recorded in srcs[count] and the Q destination
1312 * is recorded in srcs[count+1]].
1314 static int set_syndrome_sources(struct page
**srcs
,
1315 struct stripe_head
*sh
,
1318 int disks
= sh
->disks
;
1319 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1320 int d0_idx
= raid6_d0(sh
);
1324 for (i
= 0; i
< disks
; i
++)
1330 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1331 struct r5dev
*dev
= &sh
->dev
[i
];
1333 if (i
== sh
->qd_idx
|| i
== sh
->pd_idx
||
1334 (srctype
== SYNDROME_SRC_ALL
) ||
1335 (srctype
== SYNDROME_SRC_WANT_DRAIN
&&
1336 (test_bit(R5_Wantdrain
, &dev
->flags
) ||
1337 test_bit(R5_InJournal
, &dev
->flags
))) ||
1338 (srctype
== SYNDROME_SRC_WRITTEN
&&
1340 if (test_bit(R5_InJournal
, &dev
->flags
))
1341 srcs
[slot
] = sh
->dev
[i
].orig_page
;
1343 srcs
[slot
] = sh
->dev
[i
].page
;
1345 i
= raid6_next_disk(i
, disks
);
1346 } while (i
!= d0_idx
);
1348 return syndrome_disks
;
1351 static struct dma_async_tx_descriptor
*
1352 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1354 int disks
= sh
->disks
;
1355 struct page
**blocks
= to_addr_page(percpu
, 0);
1357 int qd_idx
= sh
->qd_idx
;
1358 struct dma_async_tx_descriptor
*tx
;
1359 struct async_submit_ctl submit
;
1365 BUG_ON(sh
->batch_head
);
1366 if (sh
->ops
.target
< 0)
1367 target
= sh
->ops
.target2
;
1368 else if (sh
->ops
.target2
< 0)
1369 target
= sh
->ops
.target
;
1371 /* we should only have one valid target */
1374 pr_debug("%s: stripe %llu block: %d\n",
1375 __func__
, (unsigned long long)sh
->sector
, target
);
1377 tgt
= &sh
->dev
[target
];
1378 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1381 atomic_inc(&sh
->count
);
1383 if (target
== qd_idx
) {
1384 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1385 blocks
[count
] = NULL
; /* regenerating p is not necessary */
1386 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
1387 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1388 ops_complete_compute
, sh
,
1389 to_addr_conv(sh
, percpu
, 0));
1390 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1392 /* Compute any data- or p-drive using XOR */
1394 for (i
= disks
; i
-- ; ) {
1395 if (i
== target
|| i
== qd_idx
)
1397 blocks
[count
++] = sh
->dev
[i
].page
;
1400 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1401 NULL
, ops_complete_compute
, sh
,
1402 to_addr_conv(sh
, percpu
, 0));
1403 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1409 static struct dma_async_tx_descriptor
*
1410 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1412 int i
, count
, disks
= sh
->disks
;
1413 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1414 int d0_idx
= raid6_d0(sh
);
1415 int faila
= -1, failb
= -1;
1416 int target
= sh
->ops
.target
;
1417 int target2
= sh
->ops
.target2
;
1418 struct r5dev
*tgt
= &sh
->dev
[target
];
1419 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1420 struct dma_async_tx_descriptor
*tx
;
1421 struct page
**blocks
= to_addr_page(percpu
, 0);
1422 struct async_submit_ctl submit
;
1424 BUG_ON(sh
->batch_head
);
1425 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1426 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1427 BUG_ON(target
< 0 || target2
< 0);
1428 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1429 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1431 /* we need to open-code set_syndrome_sources to handle the
1432 * slot number conversion for 'faila' and 'failb'
1434 for (i
= 0; i
< disks
; i
++)
1439 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1441 blocks
[slot
] = sh
->dev
[i
].page
;
1447 i
= raid6_next_disk(i
, disks
);
1448 } while (i
!= d0_idx
);
1450 BUG_ON(faila
== failb
);
1453 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1454 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1456 atomic_inc(&sh
->count
);
1458 if (failb
== syndrome_disks
+1) {
1459 /* Q disk is one of the missing disks */
1460 if (faila
== syndrome_disks
) {
1461 /* Missing P+Q, just recompute */
1462 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1463 ops_complete_compute
, sh
,
1464 to_addr_conv(sh
, percpu
, 0));
1465 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1466 STRIPE_SIZE
, &submit
);
1470 int qd_idx
= sh
->qd_idx
;
1472 /* Missing D+Q: recompute D from P, then recompute Q */
1473 if (target
== qd_idx
)
1474 data_target
= target2
;
1476 data_target
= target
;
1479 for (i
= disks
; i
-- ; ) {
1480 if (i
== data_target
|| i
== qd_idx
)
1482 blocks
[count
++] = sh
->dev
[i
].page
;
1484 dest
= sh
->dev
[data_target
].page
;
1485 init_async_submit(&submit
,
1486 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1488 to_addr_conv(sh
, percpu
, 0));
1489 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1492 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_ALL
);
1493 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1494 ops_complete_compute
, sh
,
1495 to_addr_conv(sh
, percpu
, 0));
1496 return async_gen_syndrome(blocks
, 0, count
+2,
1497 STRIPE_SIZE
, &submit
);
1500 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1501 ops_complete_compute
, sh
,
1502 to_addr_conv(sh
, percpu
, 0));
1503 if (failb
== syndrome_disks
) {
1504 /* We're missing D+P. */
1505 return async_raid6_datap_recov(syndrome_disks
+2,
1509 /* We're missing D+D. */
1510 return async_raid6_2data_recov(syndrome_disks
+2,
1511 STRIPE_SIZE
, faila
, failb
,
1517 static void ops_complete_prexor(void *stripe_head_ref
)
1519 struct stripe_head
*sh
= stripe_head_ref
;
1521 pr_debug("%s: stripe %llu\n", __func__
,
1522 (unsigned long long)sh
->sector
);
1524 if (r5c_is_writeback(sh
->raid_conf
->log
))
1526 * raid5-cache write back uses orig_page during prexor.
1527 * After prexor, it is time to free orig_page
1529 r5c_release_extra_page(sh
);
1532 static struct dma_async_tx_descriptor
*
1533 ops_run_prexor5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1534 struct dma_async_tx_descriptor
*tx
)
1536 int disks
= sh
->disks
;
1537 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1538 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1539 struct async_submit_ctl submit
;
1541 /* existing parity data subtracted */
1542 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1544 BUG_ON(sh
->batch_head
);
1545 pr_debug("%s: stripe %llu\n", __func__
,
1546 (unsigned long long)sh
->sector
);
1548 for (i
= disks
; i
--; ) {
1549 struct r5dev
*dev
= &sh
->dev
[i
];
1550 /* Only process blocks that are known to be uptodate */
1551 if (test_bit(R5_InJournal
, &dev
->flags
))
1552 xor_srcs
[count
++] = dev
->orig_page
;
1553 else if (test_bit(R5_Wantdrain
, &dev
->flags
))
1554 xor_srcs
[count
++] = dev
->page
;
1557 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1558 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1559 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1564 static struct dma_async_tx_descriptor
*
1565 ops_run_prexor6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1566 struct dma_async_tx_descriptor
*tx
)
1568 struct page
**blocks
= to_addr_page(percpu
, 0);
1570 struct async_submit_ctl submit
;
1572 pr_debug("%s: stripe %llu\n", __func__
,
1573 (unsigned long long)sh
->sector
);
1575 count
= set_syndrome_sources(blocks
, sh
, SYNDROME_SRC_WANT_DRAIN
);
1577 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_PQ_XOR_DST
, tx
,
1578 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
, 0));
1579 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1584 static struct dma_async_tx_descriptor
*
1585 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1587 struct r5conf
*conf
= sh
->raid_conf
;
1588 int disks
= sh
->disks
;
1590 struct stripe_head
*head_sh
= sh
;
1592 pr_debug("%s: stripe %llu\n", __func__
,
1593 (unsigned long long)sh
->sector
);
1595 for (i
= disks
; i
--; ) {
1600 if (test_and_clear_bit(R5_Wantdrain
, &head_sh
->dev
[i
].flags
)) {
1606 * clear R5_InJournal, so when rewriting a page in
1607 * journal, it is not skipped by r5l_log_stripe()
1609 clear_bit(R5_InJournal
, &dev
->flags
);
1610 spin_lock_irq(&sh
->stripe_lock
);
1611 chosen
= dev
->towrite
;
1612 dev
->towrite
= NULL
;
1613 sh
->overwrite_disks
= 0;
1614 BUG_ON(dev
->written
);
1615 wbi
= dev
->written
= chosen
;
1616 spin_unlock_irq(&sh
->stripe_lock
);
1617 WARN_ON(dev
->page
!= dev
->orig_page
);
1619 while (wbi
&& wbi
->bi_iter
.bi_sector
<
1620 dev
->sector
+ STRIPE_SECTORS
) {
1621 if (wbi
->bi_opf
& REQ_FUA
)
1622 set_bit(R5_WantFUA
, &dev
->flags
);
1623 if (wbi
->bi_opf
& REQ_SYNC
)
1624 set_bit(R5_SyncIO
, &dev
->flags
);
1625 if (bio_op(wbi
) == REQ_OP_DISCARD
)
1626 set_bit(R5_Discard
, &dev
->flags
);
1628 tx
= async_copy_data(1, wbi
, &dev
->page
,
1629 dev
->sector
, tx
, sh
,
1630 r5c_is_writeback(conf
->log
));
1631 if (dev
->page
!= dev
->orig_page
&&
1632 !r5c_is_writeback(conf
->log
)) {
1633 set_bit(R5_SkipCopy
, &dev
->flags
);
1634 clear_bit(R5_UPTODATE
, &dev
->flags
);
1635 clear_bit(R5_OVERWRITE
, &dev
->flags
);
1638 wbi
= r5_next_bio(wbi
, dev
->sector
);
1641 if (head_sh
->batch_head
) {
1642 sh
= list_first_entry(&sh
->batch_list
,
1655 static void ops_complete_reconstruct(void *stripe_head_ref
)
1657 struct stripe_head
*sh
= stripe_head_ref
;
1658 int disks
= sh
->disks
;
1659 int pd_idx
= sh
->pd_idx
;
1660 int qd_idx
= sh
->qd_idx
;
1662 bool fua
= false, sync
= false, discard
= false;
1664 pr_debug("%s: stripe %llu\n", __func__
,
1665 (unsigned long long)sh
->sector
);
1667 for (i
= disks
; i
--; ) {
1668 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1669 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1670 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1673 for (i
= disks
; i
--; ) {
1674 struct r5dev
*dev
= &sh
->dev
[i
];
1676 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1677 if (!discard
&& !test_bit(R5_SkipCopy
, &dev
->flags
))
1678 set_bit(R5_UPTODATE
, &dev
->flags
);
1680 set_bit(R5_WantFUA
, &dev
->flags
);
1682 set_bit(R5_SyncIO
, &dev
->flags
);
1686 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1687 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1688 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1689 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1691 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1692 sh
->reconstruct_state
= reconstruct_state_result
;
1695 set_bit(STRIPE_HANDLE
, &sh
->state
);
1696 raid5_release_stripe(sh
);
1700 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1701 struct dma_async_tx_descriptor
*tx
)
1703 int disks
= sh
->disks
;
1704 struct page
**xor_srcs
;
1705 struct async_submit_ctl submit
;
1706 int count
, pd_idx
= sh
->pd_idx
, i
;
1707 struct page
*xor_dest
;
1709 unsigned long flags
;
1711 struct stripe_head
*head_sh
= sh
;
1714 pr_debug("%s: stripe %llu\n", __func__
,
1715 (unsigned long long)sh
->sector
);
1717 for (i
= 0; i
< sh
->disks
; i
++) {
1720 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1723 if (i
>= sh
->disks
) {
1724 atomic_inc(&sh
->count
);
1725 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1726 ops_complete_reconstruct(sh
);
1731 xor_srcs
= to_addr_page(percpu
, j
);
1732 /* check if prexor is active which means only process blocks
1733 * that are part of a read-modify-write (written)
1735 if (head_sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1737 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1738 for (i
= disks
; i
--; ) {
1739 struct r5dev
*dev
= &sh
->dev
[i
];
1740 if (head_sh
->dev
[i
].written
||
1741 test_bit(R5_InJournal
, &head_sh
->dev
[i
].flags
))
1742 xor_srcs
[count
++] = dev
->page
;
1745 xor_dest
= sh
->dev
[pd_idx
].page
;
1746 for (i
= disks
; i
--; ) {
1747 struct r5dev
*dev
= &sh
->dev
[i
];
1749 xor_srcs
[count
++] = dev
->page
;
1753 /* 1/ if we prexor'd then the dest is reused as a source
1754 * 2/ if we did not prexor then we are redoing the parity
1755 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1756 * for the synchronous xor case
1758 last_stripe
= !head_sh
->batch_head
||
1759 list_first_entry(&sh
->batch_list
,
1760 struct stripe_head
, batch_list
) == head_sh
;
1762 flags
= ASYNC_TX_ACK
|
1763 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1765 atomic_inc(&head_sh
->count
);
1766 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, head_sh
,
1767 to_addr_conv(sh
, percpu
, j
));
1769 flags
= prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
;
1770 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
,
1771 to_addr_conv(sh
, percpu
, j
));
1774 if (unlikely(count
== 1))
1775 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1777 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1780 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1787 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1788 struct dma_async_tx_descriptor
*tx
)
1790 struct async_submit_ctl submit
;
1791 struct page
**blocks
;
1792 int count
, i
, j
= 0;
1793 struct stripe_head
*head_sh
= sh
;
1796 unsigned long txflags
;
1798 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1800 for (i
= 0; i
< sh
->disks
; i
++) {
1801 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1803 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1806 if (i
>= sh
->disks
) {
1807 atomic_inc(&sh
->count
);
1808 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1809 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1810 ops_complete_reconstruct(sh
);
1815 blocks
= to_addr_page(percpu
, j
);
1817 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1818 synflags
= SYNDROME_SRC_WRITTEN
;
1819 txflags
= ASYNC_TX_ACK
| ASYNC_TX_PQ_XOR_DST
;
1821 synflags
= SYNDROME_SRC_ALL
;
1822 txflags
= ASYNC_TX_ACK
;
1825 count
= set_syndrome_sources(blocks
, sh
, synflags
);
1826 last_stripe
= !head_sh
->batch_head
||
1827 list_first_entry(&sh
->batch_list
,
1828 struct stripe_head
, batch_list
) == head_sh
;
1831 atomic_inc(&head_sh
->count
);
1832 init_async_submit(&submit
, txflags
, tx
, ops_complete_reconstruct
,
1833 head_sh
, to_addr_conv(sh
, percpu
, j
));
1835 init_async_submit(&submit
, 0, tx
, NULL
, NULL
,
1836 to_addr_conv(sh
, percpu
, j
));
1837 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1840 sh
= list_first_entry(&sh
->batch_list
, struct stripe_head
,
1846 static void ops_complete_check(void *stripe_head_ref
)
1848 struct stripe_head
*sh
= stripe_head_ref
;
1850 pr_debug("%s: stripe %llu\n", __func__
,
1851 (unsigned long long)sh
->sector
);
1853 sh
->check_state
= check_state_check_result
;
1854 set_bit(STRIPE_HANDLE
, &sh
->state
);
1855 raid5_release_stripe(sh
);
1858 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1860 int disks
= sh
->disks
;
1861 int pd_idx
= sh
->pd_idx
;
1862 int qd_idx
= sh
->qd_idx
;
1863 struct page
*xor_dest
;
1864 struct page
**xor_srcs
= to_addr_page(percpu
, 0);
1865 struct dma_async_tx_descriptor
*tx
;
1866 struct async_submit_ctl submit
;
1870 pr_debug("%s: stripe %llu\n", __func__
,
1871 (unsigned long long)sh
->sector
);
1873 BUG_ON(sh
->batch_head
);
1875 xor_dest
= sh
->dev
[pd_idx
].page
;
1876 xor_srcs
[count
++] = xor_dest
;
1877 for (i
= disks
; i
--; ) {
1878 if (i
== pd_idx
|| i
== qd_idx
)
1880 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1883 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1884 to_addr_conv(sh
, percpu
, 0));
1885 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1886 &sh
->ops
.zero_sum_result
, &submit
);
1888 atomic_inc(&sh
->count
);
1889 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1890 tx
= async_trigger_callback(&submit
);
1893 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1895 struct page
**srcs
= to_addr_page(percpu
, 0);
1896 struct async_submit_ctl submit
;
1899 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1900 (unsigned long long)sh
->sector
, checkp
);
1902 BUG_ON(sh
->batch_head
);
1903 count
= set_syndrome_sources(srcs
, sh
, SYNDROME_SRC_ALL
);
1907 atomic_inc(&sh
->count
);
1908 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1909 sh
, to_addr_conv(sh
, percpu
, 0));
1910 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1911 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1914 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1916 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1917 struct dma_async_tx_descriptor
*tx
= NULL
;
1918 struct r5conf
*conf
= sh
->raid_conf
;
1919 int level
= conf
->level
;
1920 struct raid5_percpu
*percpu
;
1924 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1925 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1926 ops_run_biofill(sh
);
1930 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1932 tx
= ops_run_compute5(sh
, percpu
);
1934 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1935 tx
= ops_run_compute6_1(sh
, percpu
);
1937 tx
= ops_run_compute6_2(sh
, percpu
);
1939 /* terminate the chain if reconstruct is not set to be run */
1940 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1944 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
)) {
1946 tx
= ops_run_prexor5(sh
, percpu
, tx
);
1948 tx
= ops_run_prexor6(sh
, percpu
, tx
);
1951 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1952 tx
= ops_run_biodrain(sh
, tx
);
1956 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1958 ops_run_reconstruct5(sh
, percpu
, tx
);
1960 ops_run_reconstruct6(sh
, percpu
, tx
);
1963 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1964 if (sh
->check_state
== check_state_run
)
1965 ops_run_check_p(sh
, percpu
);
1966 else if (sh
->check_state
== check_state_run_q
)
1967 ops_run_check_pq(sh
, percpu
, 0);
1968 else if (sh
->check_state
== check_state_run_pq
)
1969 ops_run_check_pq(sh
, percpu
, 1);
1974 if (overlap_clear
&& !sh
->batch_head
)
1975 for (i
= disks
; i
--; ) {
1976 struct r5dev
*dev
= &sh
->dev
[i
];
1977 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1978 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1983 static struct stripe_head
*alloc_stripe(struct kmem_cache
*sc
, gfp_t gfp
,
1986 struct stripe_head
*sh
;
1989 sh
= kmem_cache_zalloc(sc
, gfp
);
1991 spin_lock_init(&sh
->stripe_lock
);
1992 spin_lock_init(&sh
->batch_lock
);
1993 INIT_LIST_HEAD(&sh
->batch_list
);
1994 INIT_LIST_HEAD(&sh
->lru
);
1995 atomic_set(&sh
->count
, 1);
1996 for (i
= 0; i
< disks
; i
++) {
1997 struct r5dev
*dev
= &sh
->dev
[i
];
1999 bio_init(&dev
->req
);
2000 dev
->req
.bi_io_vec
= &dev
->vec
;
2001 dev
->req
.bi_max_vecs
= 1;
2003 bio_init(&dev
->rreq
);
2004 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
2005 dev
->rreq
.bi_max_vecs
= 1;
2010 static int grow_one_stripe(struct r5conf
*conf
, gfp_t gfp
)
2012 struct stripe_head
*sh
;
2014 sh
= alloc_stripe(conf
->slab_cache
, gfp
, conf
->pool_size
);
2018 sh
->raid_conf
= conf
;
2020 if (grow_buffers(sh
, gfp
)) {
2022 kmem_cache_free(conf
->slab_cache
, sh
);
2025 sh
->hash_lock_index
=
2026 conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
;
2027 /* we just created an active stripe so... */
2028 atomic_inc(&conf
->active_stripes
);
2030 raid5_release_stripe(sh
);
2031 conf
->max_nr_stripes
++;
2035 static int grow_stripes(struct r5conf
*conf
, int num
)
2037 struct kmem_cache
*sc
;
2038 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
2040 if (conf
->mddev
->gendisk
)
2041 sprintf(conf
->cache_name
[0],
2042 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
2044 sprintf(conf
->cache_name
[0],
2045 "raid%d-%p", conf
->level
, conf
->mddev
);
2046 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
2048 conf
->active_name
= 0;
2049 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
2050 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
2054 conf
->slab_cache
= sc
;
2055 conf
->pool_size
= devs
;
2057 if (!grow_one_stripe(conf
, GFP_KERNEL
))
2064 * scribble_len - return the required size of the scribble region
2065 * @num - total number of disks in the array
2067 * The size must be enough to contain:
2068 * 1/ a struct page pointer for each device in the array +2
2069 * 2/ room to convert each entry in (1) to its corresponding dma
2070 * (dma_map_page()) or page (page_address()) address.
2072 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2073 * calculate over all devices (not just the data blocks), using zeros in place
2074 * of the P and Q blocks.
2076 static struct flex_array
*scribble_alloc(int num
, int cnt
, gfp_t flags
)
2078 struct flex_array
*ret
;
2081 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
2082 ret
= flex_array_alloc(len
, cnt
, flags
);
2085 /* always prealloc all elements, so no locking is required */
2086 if (flex_array_prealloc(ret
, 0, cnt
, flags
)) {
2087 flex_array_free(ret
);
2093 static int resize_chunks(struct r5conf
*conf
, int new_disks
, int new_sectors
)
2099 * Never shrink. And mddev_suspend() could deadlock if this is called
2100 * from raid5d. In that case, scribble_disks and scribble_sectors
2101 * should equal to new_disks and new_sectors
2103 if (conf
->scribble_disks
>= new_disks
&&
2104 conf
->scribble_sectors
>= new_sectors
)
2106 mddev_suspend(conf
->mddev
);
2108 for_each_present_cpu(cpu
) {
2109 struct raid5_percpu
*percpu
;
2110 struct flex_array
*scribble
;
2112 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
2113 scribble
= scribble_alloc(new_disks
,
2114 new_sectors
/ STRIPE_SECTORS
,
2118 flex_array_free(percpu
->scribble
);
2119 percpu
->scribble
= scribble
;
2126 mddev_resume(conf
->mddev
);
2128 conf
->scribble_disks
= new_disks
;
2129 conf
->scribble_sectors
= new_sectors
;
2134 static int resize_stripes(struct r5conf
*conf
, int newsize
)
2136 /* Make all the stripes able to hold 'newsize' devices.
2137 * New slots in each stripe get 'page' set to a new page.
2139 * This happens in stages:
2140 * 1/ create a new kmem_cache and allocate the required number of
2142 * 2/ gather all the old stripe_heads and transfer the pages across
2143 * to the new stripe_heads. This will have the side effect of
2144 * freezing the array as once all stripe_heads have been collected,
2145 * no IO will be possible. Old stripe heads are freed once their
2146 * pages have been transferred over, and the old kmem_cache is
2147 * freed when all stripes are done.
2148 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2149 * we simple return a failre status - no need to clean anything up.
2150 * 4/ allocate new pages for the new slots in the new stripe_heads.
2151 * If this fails, we don't bother trying the shrink the
2152 * stripe_heads down again, we just leave them as they are.
2153 * As each stripe_head is processed the new one is released into
2156 * Once step2 is started, we cannot afford to wait for a write,
2157 * so we use GFP_NOIO allocations.
2159 struct stripe_head
*osh
, *nsh
;
2160 LIST_HEAD(newstripes
);
2161 struct disk_info
*ndisks
;
2163 struct kmem_cache
*sc
;
2167 if (newsize
<= conf
->pool_size
)
2168 return 0; /* never bother to shrink */
2170 err
= md_allow_write(conf
->mddev
);
2175 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
2176 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
2181 /* Need to ensure auto-resizing doesn't interfere */
2182 mutex_lock(&conf
->cache_size_mutex
);
2184 for (i
= conf
->max_nr_stripes
; i
; i
--) {
2185 nsh
= alloc_stripe(sc
, GFP_KERNEL
, newsize
);
2189 nsh
->raid_conf
= conf
;
2190 list_add(&nsh
->lru
, &newstripes
);
2193 /* didn't get enough, give up */
2194 while (!list_empty(&newstripes
)) {
2195 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2196 list_del(&nsh
->lru
);
2197 kmem_cache_free(sc
, nsh
);
2199 kmem_cache_destroy(sc
);
2200 mutex_unlock(&conf
->cache_size_mutex
);
2203 /* Step 2 - Must use GFP_NOIO now.
2204 * OK, we have enough stripes, start collecting inactive
2205 * stripes and copying them over
2209 list_for_each_entry(nsh
, &newstripes
, lru
) {
2210 lock_device_hash_lock(conf
, hash
);
2211 wait_event_cmd(conf
->wait_for_stripe
,
2212 !list_empty(conf
->inactive_list
+ hash
),
2213 unlock_device_hash_lock(conf
, hash
),
2214 lock_device_hash_lock(conf
, hash
));
2215 osh
= get_free_stripe(conf
, hash
);
2216 unlock_device_hash_lock(conf
, hash
);
2218 for(i
=0; i
<conf
->pool_size
; i
++) {
2219 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
2220 nsh
->dev
[i
].orig_page
= osh
->dev
[i
].page
;
2222 nsh
->hash_lock_index
= hash
;
2223 kmem_cache_free(conf
->slab_cache
, osh
);
2225 if (cnt
>= conf
->max_nr_stripes
/ NR_STRIPE_HASH_LOCKS
+
2226 !!((conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
) > hash
)) {
2231 kmem_cache_destroy(conf
->slab_cache
);
2234 * At this point, we are holding all the stripes so the array
2235 * is completely stalled, so now is a good time to resize
2236 * conf->disks and the scribble region
2238 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
2240 for (i
=0; i
<conf
->raid_disks
; i
++)
2241 ndisks
[i
] = conf
->disks
[i
];
2243 conf
->disks
= ndisks
;
2247 mutex_unlock(&conf
->cache_size_mutex
);
2248 /* Step 4, return new stripes to service */
2249 while(!list_empty(&newstripes
)) {
2250 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
2251 list_del_init(&nsh
->lru
);
2253 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
2254 if (nsh
->dev
[i
].page
== NULL
) {
2255 struct page
*p
= alloc_page(GFP_NOIO
);
2256 nsh
->dev
[i
].page
= p
;
2257 nsh
->dev
[i
].orig_page
= p
;
2261 raid5_release_stripe(nsh
);
2263 /* critical section pass, GFP_NOIO no longer needed */
2265 conf
->slab_cache
= sc
;
2266 conf
->active_name
= 1-conf
->active_name
;
2268 conf
->pool_size
= newsize
;
2272 static int drop_one_stripe(struct r5conf
*conf
)
2274 struct stripe_head
*sh
;
2275 int hash
= (conf
->max_nr_stripes
- 1) & STRIPE_HASH_LOCKS_MASK
;
2277 spin_lock_irq(conf
->hash_locks
+ hash
);
2278 sh
= get_free_stripe(conf
, hash
);
2279 spin_unlock_irq(conf
->hash_locks
+ hash
);
2282 BUG_ON(atomic_read(&sh
->count
));
2284 kmem_cache_free(conf
->slab_cache
, sh
);
2285 atomic_dec(&conf
->active_stripes
);
2286 conf
->max_nr_stripes
--;
2290 static void shrink_stripes(struct r5conf
*conf
)
2292 while (conf
->max_nr_stripes
&&
2293 drop_one_stripe(conf
))
2296 kmem_cache_destroy(conf
->slab_cache
);
2297 conf
->slab_cache
= NULL
;
2300 static void raid5_end_read_request(struct bio
* bi
)
2302 struct stripe_head
*sh
= bi
->bi_private
;
2303 struct r5conf
*conf
= sh
->raid_conf
;
2304 int disks
= sh
->disks
, i
;
2305 char b
[BDEVNAME_SIZE
];
2306 struct md_rdev
*rdev
= NULL
;
2309 for (i
=0 ; i
<disks
; i
++)
2310 if (bi
== &sh
->dev
[i
].req
)
2313 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2314 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2321 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2322 /* If replacement finished while this request was outstanding,
2323 * 'replacement' might be NULL already.
2324 * In that case it moved down to 'rdev'.
2325 * rdev is not removed until all requests are finished.
2327 rdev
= conf
->disks
[i
].replacement
;
2329 rdev
= conf
->disks
[i
].rdev
;
2331 if (use_new_offset(conf
, sh
))
2332 s
= sh
->sector
+ rdev
->new_data_offset
;
2334 s
= sh
->sector
+ rdev
->data_offset
;
2335 if (!bi
->bi_error
) {
2336 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2337 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2338 /* Note that this cannot happen on a
2339 * replacement device. We just fail those on
2342 pr_info_ratelimited(
2343 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
2344 mdname(conf
->mddev
), STRIPE_SECTORS
,
2345 (unsigned long long)s
,
2346 bdevname(rdev
->bdev
, b
));
2347 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
2348 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2349 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2350 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2351 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2353 if (atomic_read(&rdev
->read_errors
))
2354 atomic_set(&rdev
->read_errors
, 0);
2356 const char *bdn
= bdevname(rdev
->bdev
, b
);
2360 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2361 atomic_inc(&rdev
->read_errors
);
2362 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2363 pr_warn_ratelimited(
2364 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
2365 mdname(conf
->mddev
),
2366 (unsigned long long)s
,
2368 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
2370 pr_warn_ratelimited(
2371 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
2372 mdname(conf
->mddev
),
2373 (unsigned long long)s
,
2375 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
2378 pr_warn_ratelimited(
2379 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
2380 mdname(conf
->mddev
),
2381 (unsigned long long)s
,
2383 } else if (atomic_read(&rdev
->read_errors
)
2384 > conf
->max_nr_stripes
)
2385 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2386 mdname(conf
->mddev
), bdn
);
2389 if (set_bad
&& test_bit(In_sync
, &rdev
->flags
)
2390 && !test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2393 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
2394 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2395 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2397 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2399 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2400 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2402 && test_bit(In_sync
, &rdev
->flags
)
2403 && rdev_set_badblocks(
2404 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
2405 md_error(conf
->mddev
, rdev
);
2408 rdev_dec_pending(rdev
, conf
->mddev
);
2410 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2411 set_bit(STRIPE_HANDLE
, &sh
->state
);
2412 raid5_release_stripe(sh
);
2415 static void raid5_end_write_request(struct bio
*bi
)
2417 struct stripe_head
*sh
= bi
->bi_private
;
2418 struct r5conf
*conf
= sh
->raid_conf
;
2419 int disks
= sh
->disks
, i
;
2420 struct md_rdev
*uninitialized_var(rdev
);
2423 int replacement
= 0;
2425 for (i
= 0 ; i
< disks
; i
++) {
2426 if (bi
== &sh
->dev
[i
].req
) {
2427 rdev
= conf
->disks
[i
].rdev
;
2430 if (bi
== &sh
->dev
[i
].rreq
) {
2431 rdev
= conf
->disks
[i
].replacement
;
2435 /* rdev was removed and 'replacement'
2436 * replaced it. rdev is not removed
2437 * until all requests are finished.
2439 rdev
= conf
->disks
[i
].rdev
;
2443 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2444 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2454 md_error(conf
->mddev
, rdev
);
2455 else if (is_badblock(rdev
, sh
->sector
,
2457 &first_bad
, &bad_sectors
))
2458 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
2461 set_bit(STRIPE_DEGRADED
, &sh
->state
);
2462 set_bit(WriteErrorSeen
, &rdev
->flags
);
2463 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
2464 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2465 set_bit(MD_RECOVERY_NEEDED
,
2466 &rdev
->mddev
->recovery
);
2467 } else if (is_badblock(rdev
, sh
->sector
,
2469 &first_bad
, &bad_sectors
)) {
2470 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
2471 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))
2472 /* That was a successful write so make
2473 * sure it looks like we already did
2476 set_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2479 rdev_dec_pending(rdev
, conf
->mddev
);
2481 if (sh
->batch_head
&& bi
->bi_error
&& !replacement
)
2482 set_bit(STRIPE_BATCH_ERR
, &sh
->batch_head
->state
);
2485 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
2486 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2487 set_bit(STRIPE_HANDLE
, &sh
->state
);
2488 raid5_release_stripe(sh
);
2490 if (sh
->batch_head
&& sh
!= sh
->batch_head
)
2491 raid5_release_stripe(sh
->batch_head
);
2494 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
2496 struct r5dev
*dev
= &sh
->dev
[i
];
2499 dev
->sector
= raid5_compute_blocknr(sh
, i
, previous
);
2502 static void raid5_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2504 char b
[BDEVNAME_SIZE
];
2505 struct r5conf
*conf
= mddev
->private;
2506 unsigned long flags
;
2507 pr_debug("raid456: error called\n");
2509 spin_lock_irqsave(&conf
->device_lock
, flags
);
2510 clear_bit(In_sync
, &rdev
->flags
);
2511 mddev
->degraded
= calc_degraded(conf
);
2512 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2513 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2515 set_bit(Blocked
, &rdev
->flags
);
2516 set_bit(Faulty
, &rdev
->flags
);
2517 set_mask_bits(&mddev
->flags
, 0,
2518 BIT(MD_CHANGE_DEVS
) | BIT(MD_CHANGE_PENDING
));
2519 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2520 "md/raid:%s: Operation continuing on %d devices.\n",
2522 bdevname(rdev
->bdev
, b
),
2524 conf
->raid_disks
- mddev
->degraded
);
2528 * Input: a 'big' sector number,
2529 * Output: index of the data and parity disk, and the sector # in them.
2531 sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
2532 int previous
, int *dd_idx
,
2533 struct stripe_head
*sh
)
2535 sector_t stripe
, stripe2
;
2536 sector_t chunk_number
;
2537 unsigned int chunk_offset
;
2540 sector_t new_sector
;
2541 int algorithm
= previous
? conf
->prev_algo
2543 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2544 : conf
->chunk_sectors
;
2545 int raid_disks
= previous
? conf
->previous_raid_disks
2547 int data_disks
= raid_disks
- conf
->max_degraded
;
2549 /* First compute the information on this sector */
2552 * Compute the chunk number and the sector offset inside the chunk
2554 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
2555 chunk_number
= r_sector
;
2558 * Compute the stripe number
2560 stripe
= chunk_number
;
2561 *dd_idx
= sector_div(stripe
, data_disks
);
2564 * Select the parity disk based on the user selected algorithm.
2566 pd_idx
= qd_idx
= -1;
2567 switch(conf
->level
) {
2569 pd_idx
= data_disks
;
2572 switch (algorithm
) {
2573 case ALGORITHM_LEFT_ASYMMETRIC
:
2574 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2575 if (*dd_idx
>= pd_idx
)
2578 case ALGORITHM_RIGHT_ASYMMETRIC
:
2579 pd_idx
= sector_div(stripe2
, raid_disks
);
2580 if (*dd_idx
>= pd_idx
)
2583 case ALGORITHM_LEFT_SYMMETRIC
:
2584 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2585 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2587 case ALGORITHM_RIGHT_SYMMETRIC
:
2588 pd_idx
= sector_div(stripe2
, raid_disks
);
2589 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2591 case ALGORITHM_PARITY_0
:
2595 case ALGORITHM_PARITY_N
:
2596 pd_idx
= data_disks
;
2604 switch (algorithm
) {
2605 case ALGORITHM_LEFT_ASYMMETRIC
:
2606 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2607 qd_idx
= pd_idx
+ 1;
2608 if (pd_idx
== raid_disks
-1) {
2609 (*dd_idx
)++; /* Q D D D P */
2611 } else if (*dd_idx
>= pd_idx
)
2612 (*dd_idx
) += 2; /* D D P Q D */
2614 case ALGORITHM_RIGHT_ASYMMETRIC
:
2615 pd_idx
= sector_div(stripe2
, raid_disks
);
2616 qd_idx
= pd_idx
+ 1;
2617 if (pd_idx
== raid_disks
-1) {
2618 (*dd_idx
)++; /* Q D D D P */
2620 } else if (*dd_idx
>= pd_idx
)
2621 (*dd_idx
) += 2; /* D D P Q D */
2623 case ALGORITHM_LEFT_SYMMETRIC
:
2624 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2625 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2626 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2628 case ALGORITHM_RIGHT_SYMMETRIC
:
2629 pd_idx
= sector_div(stripe2
, raid_disks
);
2630 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2631 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2634 case ALGORITHM_PARITY_0
:
2639 case ALGORITHM_PARITY_N
:
2640 pd_idx
= data_disks
;
2641 qd_idx
= data_disks
+ 1;
2644 case ALGORITHM_ROTATING_ZERO_RESTART
:
2645 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2646 * of blocks for computing Q is different.
2648 pd_idx
= sector_div(stripe2
, raid_disks
);
2649 qd_idx
= pd_idx
+ 1;
2650 if (pd_idx
== raid_disks
-1) {
2651 (*dd_idx
)++; /* Q D D D P */
2653 } else if (*dd_idx
>= pd_idx
)
2654 (*dd_idx
) += 2; /* D D P Q D */
2658 case ALGORITHM_ROTATING_N_RESTART
:
2659 /* Same a left_asymmetric, by first stripe is
2660 * D D D P Q rather than
2664 pd_idx
= raid_disks
- 1 - 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 */
2674 case ALGORITHM_ROTATING_N_CONTINUE
:
2675 /* Same as left_symmetric but Q is before P */
2676 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2677 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2678 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2682 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2683 /* RAID5 left_asymmetric, with Q on last device */
2684 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2685 if (*dd_idx
>= pd_idx
)
2687 qd_idx
= raid_disks
- 1;
2690 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2691 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2692 if (*dd_idx
>= pd_idx
)
2694 qd_idx
= raid_disks
- 1;
2697 case ALGORITHM_LEFT_SYMMETRIC_6
:
2698 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2699 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2700 qd_idx
= raid_disks
- 1;
2703 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2704 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2705 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2706 qd_idx
= raid_disks
- 1;
2709 case ALGORITHM_PARITY_0_6
:
2712 qd_idx
= raid_disks
- 1;
2722 sh
->pd_idx
= pd_idx
;
2723 sh
->qd_idx
= qd_idx
;
2724 sh
->ddf_layout
= ddf_layout
;
2727 * Finally, compute the new sector number
2729 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2733 sector_t
raid5_compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2735 struct r5conf
*conf
= sh
->raid_conf
;
2736 int raid_disks
= sh
->disks
;
2737 int data_disks
= raid_disks
- conf
->max_degraded
;
2738 sector_t new_sector
= sh
->sector
, check
;
2739 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2740 : conf
->chunk_sectors
;
2741 int algorithm
= previous
? conf
->prev_algo
2745 sector_t chunk_number
;
2746 int dummy1
, dd_idx
= i
;
2748 struct stripe_head sh2
;
2750 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2751 stripe
= new_sector
;
2753 if (i
== sh
->pd_idx
)
2755 switch(conf
->level
) {
2758 switch (algorithm
) {
2759 case ALGORITHM_LEFT_ASYMMETRIC
:
2760 case ALGORITHM_RIGHT_ASYMMETRIC
:
2764 case ALGORITHM_LEFT_SYMMETRIC
:
2765 case ALGORITHM_RIGHT_SYMMETRIC
:
2768 i
-= (sh
->pd_idx
+ 1);
2770 case ALGORITHM_PARITY_0
:
2773 case ALGORITHM_PARITY_N
:
2780 if (i
== sh
->qd_idx
)
2781 return 0; /* It is the Q disk */
2782 switch (algorithm
) {
2783 case ALGORITHM_LEFT_ASYMMETRIC
:
2784 case ALGORITHM_RIGHT_ASYMMETRIC
:
2785 case ALGORITHM_ROTATING_ZERO_RESTART
:
2786 case ALGORITHM_ROTATING_N_RESTART
:
2787 if (sh
->pd_idx
== raid_disks
-1)
2788 i
--; /* Q D D D P */
2789 else if (i
> sh
->pd_idx
)
2790 i
-= 2; /* D D P Q D */
2792 case ALGORITHM_LEFT_SYMMETRIC
:
2793 case ALGORITHM_RIGHT_SYMMETRIC
:
2794 if (sh
->pd_idx
== raid_disks
-1)
2795 i
--; /* Q D D D P */
2800 i
-= (sh
->pd_idx
+ 2);
2803 case ALGORITHM_PARITY_0
:
2806 case ALGORITHM_PARITY_N
:
2808 case ALGORITHM_ROTATING_N_CONTINUE
:
2809 /* Like left_symmetric, but P is before Q */
2810 if (sh
->pd_idx
== 0)
2811 i
--; /* P D D D Q */
2816 i
-= (sh
->pd_idx
+ 1);
2819 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2820 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2824 case ALGORITHM_LEFT_SYMMETRIC_6
:
2825 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2827 i
+= data_disks
+ 1;
2828 i
-= (sh
->pd_idx
+ 1);
2830 case ALGORITHM_PARITY_0_6
:
2839 chunk_number
= stripe
* data_disks
+ i
;
2840 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2842 check
= raid5_compute_sector(conf
, r_sector
,
2843 previous
, &dummy1
, &sh2
);
2844 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2845 || sh2
.qd_idx
!= sh
->qd_idx
) {
2846 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2847 mdname(conf
->mddev
));
2854 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2855 int rcw
, int expand
)
2857 int i
, pd_idx
= sh
->pd_idx
, qd_idx
= sh
->qd_idx
, disks
= sh
->disks
;
2858 struct r5conf
*conf
= sh
->raid_conf
;
2859 int level
= conf
->level
;
2863 * In some cases, handle_stripe_dirtying initially decided to
2864 * run rmw and allocates extra page for prexor. However, rcw is
2865 * cheaper later on. We need to free the extra page now,
2866 * because we won't be able to do that in ops_complete_prexor().
2868 r5c_release_extra_page(sh
);
2870 for (i
= disks
; i
--; ) {
2871 struct r5dev
*dev
= &sh
->dev
[i
];
2874 set_bit(R5_LOCKED
, &dev
->flags
);
2875 set_bit(R5_Wantdrain
, &dev
->flags
);
2877 clear_bit(R5_UPTODATE
, &dev
->flags
);
2879 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
2880 set_bit(R5_LOCKED
, &dev
->flags
);
2884 /* if we are not expanding this is a proper write request, and
2885 * there will be bios with new data to be drained into the
2890 /* False alarm, nothing to do */
2892 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2893 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2895 sh
->reconstruct_state
= reconstruct_state_run
;
2897 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2899 if (s
->locked
+ conf
->max_degraded
== disks
)
2900 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2901 atomic_inc(&conf
->pending_full_writes
);
2903 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2904 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2905 BUG_ON(level
== 6 &&
2906 (!(test_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
) ||
2907 test_bit(R5_Wantcompute
, &sh
->dev
[qd_idx
].flags
))));
2909 for (i
= disks
; i
--; ) {
2910 struct r5dev
*dev
= &sh
->dev
[i
];
2911 if (i
== pd_idx
|| i
== qd_idx
)
2915 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2916 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2917 set_bit(R5_Wantdrain
, &dev
->flags
);
2918 set_bit(R5_LOCKED
, &dev
->flags
);
2919 clear_bit(R5_UPTODATE
, &dev
->flags
);
2921 } else if (test_bit(R5_InJournal
, &dev
->flags
)) {
2922 set_bit(R5_LOCKED
, &dev
->flags
);
2927 /* False alarm - nothing to do */
2929 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2930 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2931 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2932 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2935 /* keep the parity disk(s) locked while asynchronous operations
2938 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2939 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2943 int qd_idx
= sh
->qd_idx
;
2944 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2946 set_bit(R5_LOCKED
, &dev
->flags
);
2947 clear_bit(R5_UPTODATE
, &dev
->flags
);
2951 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2952 __func__
, (unsigned long long)sh
->sector
,
2953 s
->locked
, s
->ops_request
);
2957 * Each stripe/dev can have one or more bion attached.
2958 * toread/towrite point to the first in a chain.
2959 * The bi_next chain must be in order.
2961 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
,
2962 int forwrite
, int previous
)
2965 struct r5conf
*conf
= sh
->raid_conf
;
2968 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2969 (unsigned long long)bi
->bi_iter
.bi_sector
,
2970 (unsigned long long)sh
->sector
);
2973 * If several bio share a stripe. The bio bi_phys_segments acts as a
2974 * reference count to avoid race. The reference count should already be
2975 * increased before this function is called (for example, in
2976 * raid5_make_request()), so other bio sharing this stripe will not free the
2977 * stripe. If a stripe is owned by one stripe, the stripe lock will
2980 spin_lock_irq(&sh
->stripe_lock
);
2981 /* Don't allow new IO added to stripes in batch list */
2985 bip
= &sh
->dev
[dd_idx
].towrite
;
2989 bip
= &sh
->dev
[dd_idx
].toread
;
2990 while (*bip
&& (*bip
)->bi_iter
.bi_sector
< bi
->bi_iter
.bi_sector
) {
2991 if (bio_end_sector(*bip
) > bi
->bi_iter
.bi_sector
)
2993 bip
= & (*bip
)->bi_next
;
2995 if (*bip
&& (*bip
)->bi_iter
.bi_sector
< bio_end_sector(bi
))
2998 if (!forwrite
|| previous
)
2999 clear_bit(STRIPE_BATCH_READY
, &sh
->state
);
3001 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
3005 raid5_inc_bi_active_stripes(bi
);
3008 /* check if page is covered */
3009 sector_t sector
= sh
->dev
[dd_idx
].sector
;
3010 for (bi
=sh
->dev
[dd_idx
].towrite
;
3011 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
3012 bi
&& bi
->bi_iter
.bi_sector
<= sector
;
3013 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
3014 if (bio_end_sector(bi
) >= sector
)
3015 sector
= bio_end_sector(bi
);
3017 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
3018 if (!test_and_set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
))
3019 sh
->overwrite_disks
++;
3022 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
3023 (unsigned long long)(*bip
)->bi_iter
.bi_sector
,
3024 (unsigned long long)sh
->sector
, dd_idx
);
3026 if (conf
->mddev
->bitmap
&& firstwrite
) {
3027 /* Cannot hold spinlock over bitmap_startwrite,
3028 * but must ensure this isn't added to a batch until
3029 * we have added to the bitmap and set bm_seq.
3030 * So set STRIPE_BITMAP_PENDING to prevent
3032 * If multiple add_stripe_bio() calls race here they
3033 * much all set STRIPE_BITMAP_PENDING. So only the first one
3034 * to complete "bitmap_startwrite" gets to set
3035 * STRIPE_BIT_DELAY. This is important as once a stripe
3036 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3039 set_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3040 spin_unlock_irq(&sh
->stripe_lock
);
3041 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
3043 spin_lock_irq(&sh
->stripe_lock
);
3044 clear_bit(STRIPE_BITMAP_PENDING
, &sh
->state
);
3045 if (!sh
->batch_head
) {
3046 sh
->bm_seq
= conf
->seq_flush
+1;
3047 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
3050 spin_unlock_irq(&sh
->stripe_lock
);
3052 if (stripe_can_batch(sh
))
3053 stripe_add_to_batch_list(conf
, sh
);
3057 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
3058 spin_unlock_irq(&sh
->stripe_lock
);
3062 static void end_reshape(struct r5conf
*conf
);
3064 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
3065 struct stripe_head
*sh
)
3067 int sectors_per_chunk
=
3068 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
3070 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
3071 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
3073 raid5_compute_sector(conf
,
3074 stripe
* (disks
- conf
->max_degraded
)
3075 *sectors_per_chunk
+ chunk_offset
,
3081 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
3082 struct stripe_head_state
*s
, int disks
,
3083 struct bio_list
*return_bi
)
3086 BUG_ON(sh
->batch_head
);
3087 for (i
= disks
; i
--; ) {
3091 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
3092 struct md_rdev
*rdev
;
3094 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3095 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
3096 !test_bit(Faulty
, &rdev
->flags
))
3097 atomic_inc(&rdev
->nr_pending
);
3102 if (!rdev_set_badblocks(
3106 md_error(conf
->mddev
, rdev
);
3107 rdev_dec_pending(rdev
, conf
->mddev
);
3110 spin_lock_irq(&sh
->stripe_lock
);
3111 /* fail all writes first */
3112 bi
= sh
->dev
[i
].towrite
;
3113 sh
->dev
[i
].towrite
= NULL
;
3114 sh
->overwrite_disks
= 0;
3115 spin_unlock_irq(&sh
->stripe_lock
);
3119 r5l_stripe_write_finished(sh
);
3121 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3122 wake_up(&conf
->wait_for_overlap
);
3124 while (bi
&& bi
->bi_iter
.bi_sector
<
3125 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3126 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3128 bi
->bi_error
= -EIO
;
3129 if (!raid5_dec_bi_active_stripes(bi
)) {
3130 md_write_end(conf
->mddev
);
3131 bio_list_add(return_bi
, bi
);
3136 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3137 STRIPE_SECTORS
, 0, 0);
3139 /* and fail all 'written' */
3140 bi
= sh
->dev
[i
].written
;
3141 sh
->dev
[i
].written
= NULL
;
3142 if (test_and_clear_bit(R5_SkipCopy
, &sh
->dev
[i
].flags
)) {
3143 WARN_ON(test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
3144 sh
->dev
[i
].page
= sh
->dev
[i
].orig_page
;
3147 if (bi
) bitmap_end
= 1;
3148 while (bi
&& bi
->bi_iter
.bi_sector
<
3149 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3150 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
3152 bi
->bi_error
= -EIO
;
3153 if (!raid5_dec_bi_active_stripes(bi
)) {
3154 md_write_end(conf
->mddev
);
3155 bio_list_add(return_bi
, bi
);
3160 /* fail any reads if this device is non-operational and
3161 * the data has not reached the cache yet.
3163 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
3164 s
->failed
> conf
->max_degraded
&&
3165 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
3166 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
3167 spin_lock_irq(&sh
->stripe_lock
);
3168 bi
= sh
->dev
[i
].toread
;
3169 sh
->dev
[i
].toread
= NULL
;
3170 spin_unlock_irq(&sh
->stripe_lock
);
3171 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
3172 wake_up(&conf
->wait_for_overlap
);
3175 while (bi
&& bi
->bi_iter
.bi_sector
<
3176 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
3177 struct bio
*nextbi
=
3178 r5_next_bio(bi
, sh
->dev
[i
].sector
);
3180 bi
->bi_error
= -EIO
;
3181 if (!raid5_dec_bi_active_stripes(bi
))
3182 bio_list_add(return_bi
, bi
);
3187 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3188 STRIPE_SECTORS
, 0, 0);
3189 /* If we were in the middle of a write the parity block might
3190 * still be locked - so just clear all R5_LOCKED flags
3192 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3197 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3198 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3199 md_wakeup_thread(conf
->mddev
->thread
);
3203 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
3204 struct stripe_head_state
*s
)
3209 BUG_ON(sh
->batch_head
);
3210 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3211 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
3212 wake_up(&conf
->wait_for_overlap
);
3215 /* There is nothing more to do for sync/check/repair.
3216 * Don't even need to abort as that is handled elsewhere
3217 * if needed, and not always wanted e.g. if there is a known
3219 * For recover/replace we need to record a bad block on all
3220 * non-sync devices, or abort the recovery
3222 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
3223 /* During recovery devices cannot be removed, so
3224 * locking and refcounting of rdevs is not needed
3227 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3228 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3230 && !test_bit(Faulty
, &rdev
->flags
)
3231 && !test_bit(In_sync
, &rdev
->flags
)
3232 && !rdev_set_badblocks(rdev
, sh
->sector
,
3235 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3237 && !test_bit(Faulty
, &rdev
->flags
)
3238 && !test_bit(In_sync
, &rdev
->flags
)
3239 && !rdev_set_badblocks(rdev
, sh
->sector
,
3245 conf
->recovery_disabled
=
3246 conf
->mddev
->recovery_disabled
;
3248 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
3251 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
3253 struct md_rdev
*rdev
;
3257 rdev
= rcu_dereference(sh
->raid_conf
->disks
[disk_idx
].replacement
);
3259 && !test_bit(Faulty
, &rdev
->flags
)
3260 && !test_bit(In_sync
, &rdev
->flags
)
3261 && (rdev
->recovery_offset
<= sh
->sector
3262 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
3268 /* fetch_block - checks the given member device to see if its data needs
3269 * to be read or computed to satisfy a request.
3271 * Returns 1 when no more member devices need to be checked, otherwise returns
3272 * 0 to tell the loop in handle_stripe_fill to continue
3275 static int need_this_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3276 int disk_idx
, int disks
)
3278 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3279 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
3280 &sh
->dev
[s
->failed_num
[1]] };
3284 if (test_bit(R5_LOCKED
, &dev
->flags
) ||
3285 test_bit(R5_UPTODATE
, &dev
->flags
))
3286 /* No point reading this as we already have it or have
3287 * decided to get it.
3292 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)))
3293 /* We need this block to directly satisfy a request */
3296 if (s
->syncing
|| s
->expanding
||
3297 (s
->replacing
&& want_replace(sh
, disk_idx
)))
3298 /* When syncing, or expanding we read everything.
3299 * When replacing, we need the replaced block.
3303 if ((s
->failed
>= 1 && fdev
[0]->toread
) ||
3304 (s
->failed
>= 2 && fdev
[1]->toread
))
3305 /* If we want to read from a failed device, then
3306 * we need to actually read every other device.
3310 /* Sometimes neither read-modify-write nor reconstruct-write
3311 * cycles can work. In those cases we read every block we
3312 * can. Then the parity-update is certain to have enough to
3314 * This can only be a problem when we need to write something,
3315 * and some device has failed. If either of those tests
3316 * fail we need look no further.
3318 if (!s
->failed
|| !s
->to_write
)
3321 if (test_bit(R5_Insync
, &dev
->flags
) &&
3322 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3323 /* Pre-reads at not permitted until after short delay
3324 * to gather multiple requests. However if this
3325 * device is no Insync, the block could only be be computed
3326 * and there is no need to delay that.
3330 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3331 if (fdev
[i
]->towrite
&&
3332 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3333 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3334 /* If we have a partial write to a failed
3335 * device, then we will need to reconstruct
3336 * the content of that device, so all other
3337 * devices must be read.
3342 /* If we are forced to do a reconstruct-write, either because
3343 * the current RAID6 implementation only supports that, or
3344 * or because parity cannot be trusted and we are currently
3345 * recovering it, there is extra need to be careful.
3346 * If one of the devices that we would need to read, because
3347 * it is not being overwritten (and maybe not written at all)
3348 * is missing/faulty, then we need to read everything we can.
3350 if (sh
->raid_conf
->level
!= 6 &&
3351 sh
->sector
< sh
->raid_conf
->mddev
->recovery_cp
)
3352 /* reconstruct-write isn't being forced */
3354 for (i
= 0; i
< s
->failed
&& i
< 2; i
++) {
3355 if (s
->failed_num
[i
] != sh
->pd_idx
&&
3356 s
->failed_num
[i
] != sh
->qd_idx
&&
3357 !test_bit(R5_UPTODATE
, &fdev
[i
]->flags
) &&
3358 !test_bit(R5_OVERWRITE
, &fdev
[i
]->flags
))
3365 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
3366 int disk_idx
, int disks
)
3368 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
3370 /* is the data in this block needed, and can we get it? */
3371 if (need_this_block(sh
, s
, disk_idx
, disks
)) {
3372 /* we would like to get this block, possibly by computing it,
3373 * otherwise read it if the backing disk is insync
3375 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
3376 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
3377 BUG_ON(sh
->batch_head
);
3378 if ((s
->uptodate
== disks
- 1) &&
3379 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
3380 disk_idx
== s
->failed_num
[1]))) {
3381 /* have disk failed, and we're requested to fetch it;
3384 pr_debug("Computing stripe %llu block %d\n",
3385 (unsigned long long)sh
->sector
, disk_idx
);
3386 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3387 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3388 set_bit(R5_Wantcompute
, &dev
->flags
);
3389 sh
->ops
.target
= disk_idx
;
3390 sh
->ops
.target2
= -1; /* no 2nd target */
3392 /* Careful: from this point on 'uptodate' is in the eye
3393 * of raid_run_ops which services 'compute' operations
3394 * before writes. R5_Wantcompute flags a block that will
3395 * be R5_UPTODATE by the time it is needed for a
3396 * subsequent operation.
3400 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
3401 /* Computing 2-failure is *very* expensive; only
3402 * do it if failed >= 2
3405 for (other
= disks
; other
--; ) {
3406 if (other
== disk_idx
)
3408 if (!test_bit(R5_UPTODATE
,
3409 &sh
->dev
[other
].flags
))
3413 pr_debug("Computing stripe %llu blocks %d,%d\n",
3414 (unsigned long long)sh
->sector
,
3416 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3417 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3418 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
3419 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
3420 sh
->ops
.target
= disk_idx
;
3421 sh
->ops
.target2
= other
;
3425 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
3426 set_bit(R5_LOCKED
, &dev
->flags
);
3427 set_bit(R5_Wantread
, &dev
->flags
);
3429 pr_debug("Reading block %d (sync=%d)\n",
3430 disk_idx
, s
->syncing
);
3438 * handle_stripe_fill - read or compute data to satisfy pending requests.
3440 static void handle_stripe_fill(struct stripe_head
*sh
,
3441 struct stripe_head_state
*s
,
3446 /* look for blocks to read/compute, skip this if a compute
3447 * is already in flight, or if the stripe contents are in the
3448 * midst of changing due to a write
3450 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
3451 !sh
->reconstruct_state
)
3452 for (i
= disks
; i
--; )
3453 if (fetch_block(sh
, s
, i
, disks
))
3455 set_bit(STRIPE_HANDLE
, &sh
->state
);
3458 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
3459 unsigned long handle_flags
);
3460 /* handle_stripe_clean_event
3461 * any written block on an uptodate or failed drive can be returned.
3462 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3463 * never LOCKED, so we don't need to test 'failed' directly.
3465 static void handle_stripe_clean_event(struct r5conf
*conf
,
3466 struct stripe_head
*sh
, int disks
, struct bio_list
*return_bi
)
3470 int discard_pending
= 0;
3471 struct stripe_head
*head_sh
= sh
;
3472 bool do_endio
= false;
3474 for (i
= disks
; i
--; )
3475 if (sh
->dev
[i
].written
) {
3477 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
3478 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
3479 test_bit(R5_Discard
, &dev
->flags
) ||
3480 test_bit(R5_SkipCopy
, &dev
->flags
))) {
3481 /* We can return any write requests */
3482 struct bio
*wbi
, *wbi2
;
3483 pr_debug("Return write for disc %d\n", i
);
3484 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
3485 clear_bit(R5_UPTODATE
, &dev
->flags
);
3486 if (test_and_clear_bit(R5_SkipCopy
, &dev
->flags
)) {
3487 WARN_ON(test_bit(R5_UPTODATE
, &dev
->flags
));
3492 dev
->page
= dev
->orig_page
;
3494 dev
->written
= NULL
;
3495 while (wbi
&& wbi
->bi_iter
.bi_sector
<
3496 dev
->sector
+ STRIPE_SECTORS
) {
3497 wbi2
= r5_next_bio(wbi
, dev
->sector
);
3498 if (!raid5_dec_bi_active_stripes(wbi
)) {
3499 md_write_end(conf
->mddev
);
3500 bio_list_add(return_bi
, wbi
);
3504 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3506 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
3508 if (head_sh
->batch_head
) {
3509 sh
= list_first_entry(&sh
->batch_list
,
3512 if (sh
!= head_sh
) {
3519 } else if (test_bit(R5_Discard
, &dev
->flags
))
3520 discard_pending
= 1;
3523 r5l_stripe_write_finished(sh
);
3525 if (!discard_pending
&&
3526 test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3528 clear_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
3529 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3530 if (sh
->qd_idx
>= 0) {
3531 clear_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
3532 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
);
3534 /* now that discard is done we can proceed with any sync */
3535 clear_bit(STRIPE_DISCARD
, &sh
->state
);
3537 * SCSI discard will change some bio fields and the stripe has
3538 * no updated data, so remove it from hash list and the stripe
3539 * will be reinitialized
3542 hash
= sh
->hash_lock_index
;
3543 spin_lock_irq(conf
->hash_locks
+ hash
);
3545 spin_unlock_irq(conf
->hash_locks
+ hash
);
3546 if (head_sh
->batch_head
) {
3547 sh
= list_first_entry(&sh
->batch_list
,
3548 struct stripe_head
, batch_list
);
3554 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
))
3555 set_bit(STRIPE_HANDLE
, &sh
->state
);
3559 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3560 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3561 md_wakeup_thread(conf
->mddev
->thread
);
3563 if (head_sh
->batch_head
&& do_endio
)
3564 break_stripe_batch_list(head_sh
, STRIPE_EXPAND_SYNC_FLAGS
);
3567 static void handle_stripe_dirtying(struct r5conf
*conf
,
3568 struct stripe_head
*sh
,
3569 struct stripe_head_state
*s
,
3572 int rmw
= 0, rcw
= 0, i
;
3573 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
3575 /* Check whether resync is now happening or should start.
3576 * If yes, then the array is dirty (after unclean shutdown or
3577 * initial creation), so parity in some stripes might be inconsistent.
3578 * In this case, we need to always do reconstruct-write, to ensure
3579 * that in case of drive failure or read-error correction, we
3580 * generate correct data from the parity.
3582 if (conf
->rmw_level
== PARITY_DISABLE_RMW
||
3583 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
&&
3585 /* Calculate the real rcw later - for now make it
3586 * look like rcw is cheaper
3589 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3590 conf
->rmw_level
, (unsigned long long)recovery_cp
,
3591 (unsigned long long)sh
->sector
);
3592 } else for (i
= disks
; i
--; ) {
3593 /* would I have to read this buffer for read_modify_write */
3594 struct r5dev
*dev
= &sh
->dev
[i
];
3595 if ((dev
->towrite
|| i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3596 test_bit(R5_InJournal
, &dev
->flags
)) &&
3597 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3598 !((test_bit(R5_UPTODATE
, &dev
->flags
) &&
3599 (!test_bit(R5_InJournal
, &dev
->flags
) ||
3600 dev
->page
!= dev
->orig_page
)) ||
3601 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3602 if (test_bit(R5_Insync
, &dev
->flags
))
3605 rmw
+= 2*disks
; /* cannot read it */
3607 /* Would I have to read this buffer for reconstruct_write */
3608 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3609 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3610 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3611 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3612 test_bit(R5_InJournal
, &dev
->flags
) ||
3613 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3614 if (test_bit(R5_Insync
, &dev
->flags
))
3621 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
3622 (unsigned long long)sh
->sector
, rmw
, rcw
);
3623 set_bit(STRIPE_HANDLE
, &sh
->state
);
3624 if ((rmw
< rcw
|| (rmw
== rcw
&& conf
->rmw_level
== PARITY_PREFER_RMW
)) && rmw
> 0) {
3625 /* prefer read-modify-write, but need to get some data */
3626 if (conf
->mddev
->queue
)
3627 blk_add_trace_msg(conf
->mddev
->queue
,
3628 "raid5 rmw %llu %d",
3629 (unsigned long long)sh
->sector
, rmw
);
3630 for (i
= disks
; i
--; ) {
3631 struct r5dev
*dev
= &sh
->dev
[i
];
3632 if (test_bit(R5_InJournal
, &dev
->flags
) &&
3633 dev
->page
== dev
->orig_page
&&
3634 !test_bit(R5_LOCKED
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3635 /* alloc page for prexor */
3636 dev
->orig_page
= alloc_page(GFP_NOIO
);
3638 /* will handle failure in a later patch*/
3639 BUG_ON(!dev
->orig_page
);
3642 if ((dev
->towrite
||
3643 i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3644 test_bit(R5_InJournal
, &dev
->flags
)) &&
3645 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3646 !((test_bit(R5_UPTODATE
, &dev
->flags
) &&
3647 (!test_bit(R5_InJournal
, &dev
->flags
) ||
3648 dev
->page
!= dev
->orig_page
)) ||
3649 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
3650 test_bit(R5_Insync
, &dev
->flags
)) {
3651 if (test_bit(STRIPE_PREREAD_ACTIVE
,
3653 pr_debug("Read_old block %d for r-m-w\n",
3655 set_bit(R5_LOCKED
, &dev
->flags
);
3656 set_bit(R5_Wantread
, &dev
->flags
);
3659 set_bit(STRIPE_DELAYED
, &sh
->state
);
3660 set_bit(STRIPE_HANDLE
, &sh
->state
);
3665 if ((rcw
< rmw
|| (rcw
== rmw
&& conf
->rmw_level
!= PARITY_PREFER_RMW
)) && rcw
> 0) {
3666 /* want reconstruct write, but need to get some data */
3669 for (i
= disks
; i
--; ) {
3670 struct r5dev
*dev
= &sh
->dev
[i
];
3671 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3672 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3673 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3674 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3675 test_bit(R5_InJournal
, &dev
->flags
) ||
3676 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3678 if (test_bit(R5_Insync
, &dev
->flags
) &&
3679 test_bit(STRIPE_PREREAD_ACTIVE
,
3681 pr_debug("Read_old block "
3682 "%d for Reconstruct\n", i
);
3683 set_bit(R5_LOCKED
, &dev
->flags
);
3684 set_bit(R5_Wantread
, &dev
->flags
);
3688 set_bit(STRIPE_DELAYED
, &sh
->state
);
3689 set_bit(STRIPE_HANDLE
, &sh
->state
);
3693 if (rcw
&& conf
->mddev
->queue
)
3694 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
3695 (unsigned long long)sh
->sector
,
3696 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
3699 if (rcw
> disks
&& rmw
> disks
&&
3700 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3701 set_bit(STRIPE_DELAYED
, &sh
->state
);
3703 /* now if nothing is locked, and if we have enough data,
3704 * we can start a write request
3706 /* since handle_stripe can be called at any time we need to handle the
3707 * case where a compute block operation has been submitted and then a
3708 * subsequent call wants to start a write request. raid_run_ops only
3709 * handles the case where compute block and reconstruct are requested
3710 * simultaneously. If this is not the case then new writes need to be
3711 * held off until the compute completes.
3713 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
3714 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
3715 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
3716 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
3719 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
3720 struct stripe_head_state
*s
, int disks
)
3722 struct r5dev
*dev
= NULL
;
3724 BUG_ON(sh
->batch_head
);
3725 set_bit(STRIPE_HANDLE
, &sh
->state
);
3727 switch (sh
->check_state
) {
3728 case check_state_idle
:
3729 /* start a new check operation if there are no failures */
3730 if (s
->failed
== 0) {
3731 BUG_ON(s
->uptodate
!= disks
);
3732 sh
->check_state
= check_state_run
;
3733 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3734 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3738 dev
= &sh
->dev
[s
->failed_num
[0]];
3740 case check_state_compute_result
:
3741 sh
->check_state
= check_state_idle
;
3743 dev
= &sh
->dev
[sh
->pd_idx
];
3745 /* check that a write has not made the stripe insync */
3746 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3749 /* either failed parity check, or recovery is happening */
3750 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
3751 BUG_ON(s
->uptodate
!= disks
);
3753 set_bit(R5_LOCKED
, &dev
->flags
);
3755 set_bit(R5_Wantwrite
, &dev
->flags
);
3757 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3758 set_bit(STRIPE_INSYNC
, &sh
->state
);
3760 case check_state_run
:
3761 break; /* we will be called again upon completion */
3762 case check_state_check_result
:
3763 sh
->check_state
= check_state_idle
;
3765 /* if a failure occurred during the check operation, leave
3766 * STRIPE_INSYNC not set and let the stripe be handled again
3771 /* handle a successful check operation, if parity is correct
3772 * we are done. Otherwise update the mismatch count and repair
3773 * parity if !MD_RECOVERY_CHECK
3775 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
3776 /* parity is correct (on disc,
3777 * not in buffer any more)
3779 set_bit(STRIPE_INSYNC
, &sh
->state
);
3781 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3782 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3783 /* don't try to repair!! */
3784 set_bit(STRIPE_INSYNC
, &sh
->state
);
3786 sh
->check_state
= check_state_compute_run
;
3787 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3788 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3789 set_bit(R5_Wantcompute
,
3790 &sh
->dev
[sh
->pd_idx
].flags
);
3791 sh
->ops
.target
= sh
->pd_idx
;
3792 sh
->ops
.target2
= -1;
3797 case check_state_compute_run
:
3800 pr_err("%s: unknown check_state: %d sector: %llu\n",
3801 __func__
, sh
->check_state
,
3802 (unsigned long long) sh
->sector
);
3807 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
3808 struct stripe_head_state
*s
,
3811 int pd_idx
= sh
->pd_idx
;
3812 int qd_idx
= sh
->qd_idx
;
3815 BUG_ON(sh
->batch_head
);
3816 set_bit(STRIPE_HANDLE
, &sh
->state
);
3818 BUG_ON(s
->failed
> 2);
3820 /* Want to check and possibly repair P and Q.
3821 * However there could be one 'failed' device, in which
3822 * case we can only check one of them, possibly using the
3823 * other to generate missing data
3826 switch (sh
->check_state
) {
3827 case check_state_idle
:
3828 /* start a new check operation if there are < 2 failures */
3829 if (s
->failed
== s
->q_failed
) {
3830 /* The only possible failed device holds Q, so it
3831 * makes sense to check P (If anything else were failed,
3832 * we would have used P to recreate it).
3834 sh
->check_state
= check_state_run
;
3836 if (!s
->q_failed
&& s
->failed
< 2) {
3837 /* Q is not failed, and we didn't use it to generate
3838 * anything, so it makes sense to check it
3840 if (sh
->check_state
== check_state_run
)
3841 sh
->check_state
= check_state_run_pq
;
3843 sh
->check_state
= check_state_run_q
;
3846 /* discard potentially stale zero_sum_result */
3847 sh
->ops
.zero_sum_result
= 0;
3849 if (sh
->check_state
== check_state_run
) {
3850 /* async_xor_zero_sum destroys the contents of P */
3851 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3854 if (sh
->check_state
>= check_state_run
&&
3855 sh
->check_state
<= check_state_run_pq
) {
3856 /* async_syndrome_zero_sum preserves P and Q, so
3857 * no need to mark them !uptodate here
3859 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3863 /* we have 2-disk failure */
3864 BUG_ON(s
->failed
!= 2);
3866 case check_state_compute_result
:
3867 sh
->check_state
= check_state_idle
;
3869 /* check that a write has not made the stripe insync */
3870 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3873 /* now write out any block on a failed drive,
3874 * or P or Q if they were recomputed
3876 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
3877 if (s
->failed
== 2) {
3878 dev
= &sh
->dev
[s
->failed_num
[1]];
3880 set_bit(R5_LOCKED
, &dev
->flags
);
3881 set_bit(R5_Wantwrite
, &dev
->flags
);
3883 if (s
->failed
>= 1) {
3884 dev
= &sh
->dev
[s
->failed_num
[0]];
3886 set_bit(R5_LOCKED
, &dev
->flags
);
3887 set_bit(R5_Wantwrite
, &dev
->flags
);
3889 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3890 dev
= &sh
->dev
[pd_idx
];
3892 set_bit(R5_LOCKED
, &dev
->flags
);
3893 set_bit(R5_Wantwrite
, &dev
->flags
);
3895 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3896 dev
= &sh
->dev
[qd_idx
];
3898 set_bit(R5_LOCKED
, &dev
->flags
);
3899 set_bit(R5_Wantwrite
, &dev
->flags
);
3901 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3903 set_bit(STRIPE_INSYNC
, &sh
->state
);
3905 case check_state_run
:
3906 case check_state_run_q
:
3907 case check_state_run_pq
:
3908 break; /* we will be called again upon completion */
3909 case check_state_check_result
:
3910 sh
->check_state
= check_state_idle
;
3912 /* handle a successful check operation, if parity is correct
3913 * we are done. Otherwise update the mismatch count and repair
3914 * parity if !MD_RECOVERY_CHECK
3916 if (sh
->ops
.zero_sum_result
== 0) {
3917 /* both parities are correct */
3919 set_bit(STRIPE_INSYNC
, &sh
->state
);
3921 /* in contrast to the raid5 case we can validate
3922 * parity, but still have a failure to write
3925 sh
->check_state
= check_state_compute_result
;
3926 /* Returning at this point means that we may go
3927 * off and bring p and/or q uptodate again so
3928 * we make sure to check zero_sum_result again
3929 * to verify if p or q need writeback
3933 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3934 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3935 /* don't try to repair!! */
3936 set_bit(STRIPE_INSYNC
, &sh
->state
);
3938 int *target
= &sh
->ops
.target
;
3940 sh
->ops
.target
= -1;
3941 sh
->ops
.target2
= -1;
3942 sh
->check_state
= check_state_compute_run
;
3943 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3944 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3945 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3946 set_bit(R5_Wantcompute
,
3947 &sh
->dev
[pd_idx
].flags
);
3949 target
= &sh
->ops
.target2
;
3952 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3953 set_bit(R5_Wantcompute
,
3954 &sh
->dev
[qd_idx
].flags
);
3961 case check_state_compute_run
:
3964 pr_warn("%s: unknown check_state: %d sector: %llu\n",
3965 __func__
, sh
->check_state
,
3966 (unsigned long long) sh
->sector
);
3971 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3975 /* We have read all the blocks in this stripe and now we need to
3976 * copy some of them into a target stripe for expand.
3978 struct dma_async_tx_descriptor
*tx
= NULL
;
3979 BUG_ON(sh
->batch_head
);
3980 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3981 for (i
= 0; i
< sh
->disks
; i
++)
3982 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3984 struct stripe_head
*sh2
;
3985 struct async_submit_ctl submit
;
3987 sector_t bn
= raid5_compute_blocknr(sh
, i
, 1);
3988 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3990 sh2
= raid5_get_active_stripe(conf
, s
, 0, 1, 1);
3992 /* so far only the early blocks of this stripe
3993 * have been requested. When later blocks
3994 * get requested, we will try again
3997 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3998 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3999 /* must have already done this block */
4000 raid5_release_stripe(sh2
);
4004 /* place all the copies on one channel */
4005 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
4006 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
4007 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
4010 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
4011 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
4012 for (j
= 0; j
< conf
->raid_disks
; j
++)
4013 if (j
!= sh2
->pd_idx
&&
4015 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
4017 if (j
== conf
->raid_disks
) {
4018 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
4019 set_bit(STRIPE_HANDLE
, &sh2
->state
);
4021 raid5_release_stripe(sh2
);
4024 /* done submitting copies, wait for them to complete */
4025 async_tx_quiesce(&tx
);
4029 * handle_stripe - do things to a stripe.
4031 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4032 * state of various bits to see what needs to be done.
4034 * return some read requests which now have data
4035 * return some write requests which are safely on storage
4036 * schedule a read on some buffers
4037 * schedule a write of some buffers
4038 * return confirmation of parity correctness
4042 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
4044 struct r5conf
*conf
= sh
->raid_conf
;
4045 int disks
= sh
->disks
;
4048 int do_recovery
= 0;
4050 memset(s
, 0, sizeof(*s
));
4052 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
) && !sh
->batch_head
;
4053 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
) && !sh
->batch_head
;
4054 s
->failed_num
[0] = -1;
4055 s
->failed_num
[1] = -1;
4056 s
->log_failed
= r5l_log_disk_error(conf
);
4058 /* Now to look around and see what can be done */
4060 for (i
=disks
; i
--; ) {
4061 struct md_rdev
*rdev
;
4068 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4070 dev
->toread
, dev
->towrite
, dev
->written
);
4071 /* maybe we can reply to a read
4073 * new wantfill requests are only permitted while
4074 * ops_complete_biofill is guaranteed to be inactive
4076 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
4077 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
4078 set_bit(R5_Wantfill
, &dev
->flags
);
4080 /* now count some things */
4081 if (test_bit(R5_LOCKED
, &dev
->flags
))
4083 if (test_bit(R5_UPTODATE
, &dev
->flags
))
4085 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
4087 BUG_ON(s
->compute
> 2);
4090 if (test_bit(R5_Wantfill
, &dev
->flags
))
4092 else if (dev
->toread
)
4096 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
4101 /* Prefer to use the replacement for reads, but only
4102 * if it is recovered enough and has no bad blocks.
4104 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
4105 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
4106 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
4107 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4108 &first_bad
, &bad_sectors
))
4109 set_bit(R5_ReadRepl
, &dev
->flags
);
4111 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4112 set_bit(R5_NeedReplace
, &dev
->flags
);
4114 clear_bit(R5_NeedReplace
, &dev
->flags
);
4115 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
4116 clear_bit(R5_ReadRepl
, &dev
->flags
);
4118 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
4121 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
4122 &first_bad
, &bad_sectors
);
4123 if (s
->blocked_rdev
== NULL
4124 && (test_bit(Blocked
, &rdev
->flags
)
4127 set_bit(BlockedBadBlocks
,
4129 s
->blocked_rdev
= rdev
;
4130 atomic_inc(&rdev
->nr_pending
);
4133 clear_bit(R5_Insync
, &dev
->flags
);
4137 /* also not in-sync */
4138 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
4139 test_bit(R5_UPTODATE
, &dev
->flags
)) {
4140 /* treat as in-sync, but with a read error
4141 * which we can now try to correct
4143 set_bit(R5_Insync
, &dev
->flags
);
4144 set_bit(R5_ReadError
, &dev
->flags
);
4146 } else if (test_bit(In_sync
, &rdev
->flags
))
4147 set_bit(R5_Insync
, &dev
->flags
);
4148 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
4149 /* in sync if before recovery_offset */
4150 set_bit(R5_Insync
, &dev
->flags
);
4151 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
4152 test_bit(R5_Expanded
, &dev
->flags
))
4153 /* If we've reshaped into here, we assume it is Insync.
4154 * We will shortly update recovery_offset to make
4157 set_bit(R5_Insync
, &dev
->flags
);
4159 if (test_bit(R5_WriteError
, &dev
->flags
)) {
4160 /* This flag does not apply to '.replacement'
4161 * only to .rdev, so make sure to check that*/
4162 struct md_rdev
*rdev2
= rcu_dereference(
4163 conf
->disks
[i
].rdev
);
4165 clear_bit(R5_Insync
, &dev
->flags
);
4166 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4167 s
->handle_bad_blocks
= 1;
4168 atomic_inc(&rdev2
->nr_pending
);
4170 clear_bit(R5_WriteError
, &dev
->flags
);
4172 if (test_bit(R5_MadeGood
, &dev
->flags
)) {
4173 /* This flag does not apply to '.replacement'
4174 * only to .rdev, so make sure to check that*/
4175 struct md_rdev
*rdev2
= rcu_dereference(
4176 conf
->disks
[i
].rdev
);
4177 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4178 s
->handle_bad_blocks
= 1;
4179 atomic_inc(&rdev2
->nr_pending
);
4181 clear_bit(R5_MadeGood
, &dev
->flags
);
4183 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4184 struct md_rdev
*rdev2
= rcu_dereference(
4185 conf
->disks
[i
].replacement
);
4186 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
4187 s
->handle_bad_blocks
= 1;
4188 atomic_inc(&rdev2
->nr_pending
);
4190 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
4192 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4193 /* The ReadError flag will just be confusing now */
4194 clear_bit(R5_ReadError
, &dev
->flags
);
4195 clear_bit(R5_ReWrite
, &dev
->flags
);
4197 if (test_bit(R5_ReadError
, &dev
->flags
))
4198 clear_bit(R5_Insync
, &dev
->flags
);
4199 if (!test_bit(R5_Insync
, &dev
->flags
)) {
4201 s
->failed_num
[s
->failed
] = i
;
4203 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
4207 if (test_bit(R5_InJournal
, &dev
->flags
))
4209 if (test_bit(R5_InJournal
, &dev
->flags
) && dev
->written
)
4212 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
4213 /* If there is a failed device being replaced,
4214 * we must be recovering.
4215 * else if we are after recovery_cp, we must be syncing
4216 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4217 * else we can only be replacing
4218 * sync and recovery both need to read all devices, and so
4219 * use the same flag.
4222 sh
->sector
>= conf
->mddev
->recovery_cp
||
4223 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
4231 static int clear_batch_ready(struct stripe_head
*sh
)
4233 /* Return '1' if this is a member of batch, or
4234 * '0' if it is a lone stripe or a head which can now be
4237 struct stripe_head
*tmp
;
4238 if (!test_and_clear_bit(STRIPE_BATCH_READY
, &sh
->state
))
4239 return (sh
->batch_head
&& sh
->batch_head
!= sh
);
4240 spin_lock(&sh
->stripe_lock
);
4241 if (!sh
->batch_head
) {
4242 spin_unlock(&sh
->stripe_lock
);
4247 * this stripe could be added to a batch list before we check
4248 * BATCH_READY, skips it
4250 if (sh
->batch_head
!= sh
) {
4251 spin_unlock(&sh
->stripe_lock
);
4254 spin_lock(&sh
->batch_lock
);
4255 list_for_each_entry(tmp
, &sh
->batch_list
, batch_list
)
4256 clear_bit(STRIPE_BATCH_READY
, &tmp
->state
);
4257 spin_unlock(&sh
->batch_lock
);
4258 spin_unlock(&sh
->stripe_lock
);
4261 * BATCH_READY is cleared, no new stripes can be added.
4262 * batch_list can be accessed without lock
4267 static void break_stripe_batch_list(struct stripe_head
*head_sh
,
4268 unsigned long handle_flags
)
4270 struct stripe_head
*sh
, *next
;
4274 list_for_each_entry_safe(sh
, next
, &head_sh
->batch_list
, batch_list
) {
4276 list_del_init(&sh
->batch_list
);
4278 WARN_ONCE(sh
->state
& ((1 << STRIPE_ACTIVE
) |
4279 (1 << STRIPE_SYNCING
) |
4280 (1 << STRIPE_REPLACED
) |
4281 (1 << STRIPE_DELAYED
) |
4282 (1 << STRIPE_BIT_DELAY
) |
4283 (1 << STRIPE_FULL_WRITE
) |
4284 (1 << STRIPE_BIOFILL_RUN
) |
4285 (1 << STRIPE_COMPUTE_RUN
) |
4286 (1 << STRIPE_OPS_REQ_PENDING
) |
4287 (1 << STRIPE_DISCARD
) |
4288 (1 << STRIPE_BATCH_READY
) |
4289 (1 << STRIPE_BATCH_ERR
) |
4290 (1 << STRIPE_BITMAP_PENDING
)),
4291 "stripe state: %lx\n", sh
->state
);
4292 WARN_ONCE(head_sh
->state
& ((1 << STRIPE_DISCARD
) |
4293 (1 << STRIPE_REPLACED
)),
4294 "head stripe state: %lx\n", head_sh
->state
);
4296 set_mask_bits(&sh
->state
, ~(STRIPE_EXPAND_SYNC_FLAGS
|
4297 (1 << STRIPE_PREREAD_ACTIVE
) |
4298 (1 << STRIPE_DEGRADED
)),
4299 head_sh
->state
& (1 << STRIPE_INSYNC
));
4301 sh
->check_state
= head_sh
->check_state
;
4302 sh
->reconstruct_state
= head_sh
->reconstruct_state
;
4303 for (i
= 0; i
< sh
->disks
; i
++) {
4304 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
4306 sh
->dev
[i
].flags
= head_sh
->dev
[i
].flags
&
4307 (~((1 << R5_WriteError
) | (1 << R5_Overlap
)));
4309 spin_lock_irq(&sh
->stripe_lock
);
4310 sh
->batch_head
= NULL
;
4311 spin_unlock_irq(&sh
->stripe_lock
);
4312 if (handle_flags
== 0 ||
4313 sh
->state
& handle_flags
)
4314 set_bit(STRIPE_HANDLE
, &sh
->state
);
4315 raid5_release_stripe(sh
);
4317 spin_lock_irq(&head_sh
->stripe_lock
);
4318 head_sh
->batch_head
= NULL
;
4319 spin_unlock_irq(&head_sh
->stripe_lock
);
4320 for (i
= 0; i
< head_sh
->disks
; i
++)
4321 if (test_and_clear_bit(R5_Overlap
, &head_sh
->dev
[i
].flags
))
4323 if (head_sh
->state
& handle_flags
)
4324 set_bit(STRIPE_HANDLE
, &head_sh
->state
);
4327 wake_up(&head_sh
->raid_conf
->wait_for_overlap
);
4330 static void handle_stripe(struct stripe_head
*sh
)
4332 struct stripe_head_state s
;
4333 struct r5conf
*conf
= sh
->raid_conf
;
4336 int disks
= sh
->disks
;
4337 struct r5dev
*pdev
, *qdev
;
4339 clear_bit(STRIPE_HANDLE
, &sh
->state
);
4340 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
4341 /* already being handled, ensure it gets handled
4342 * again when current action finishes */
4343 set_bit(STRIPE_HANDLE
, &sh
->state
);
4347 if (clear_batch_ready(sh
) ) {
4348 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4352 if (test_and_clear_bit(STRIPE_BATCH_ERR
, &sh
->state
))
4353 break_stripe_batch_list(sh
, 0);
4355 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
) && !sh
->batch_head
) {
4356 spin_lock(&sh
->stripe_lock
);
4357 /* Cannot process 'sync' concurrently with 'discard' */
4358 if (!test_bit(STRIPE_DISCARD
, &sh
->state
) &&
4359 test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
4360 set_bit(STRIPE_SYNCING
, &sh
->state
);
4361 clear_bit(STRIPE_INSYNC
, &sh
->state
);
4362 clear_bit(STRIPE_REPLACED
, &sh
->state
);
4364 spin_unlock(&sh
->stripe_lock
);
4366 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4368 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4369 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4370 (unsigned long long)sh
->sector
, sh
->state
,
4371 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
4372 sh
->check_state
, sh
->reconstruct_state
);
4374 analyse_stripe(sh
, &s
);
4376 if (test_bit(STRIPE_LOG_TRAPPED
, &sh
->state
))
4379 if (s
.handle_bad_blocks
) {
4380 set_bit(STRIPE_HANDLE
, &sh
->state
);
4384 if (unlikely(s
.blocked_rdev
)) {
4385 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
4386 s
.replacing
|| s
.to_write
|| s
.written
) {
4387 set_bit(STRIPE_HANDLE
, &sh
->state
);
4390 /* There is nothing for the blocked_rdev to block */
4391 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
4392 s
.blocked_rdev
= NULL
;
4395 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
4396 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
4397 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
4400 pr_debug("locked=%d uptodate=%d to_read=%d"
4401 " to_write=%d failed=%d failed_num=%d,%d\n",
4402 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
4403 s
.failed_num
[0], s
.failed_num
[1]);
4404 /* check if the array has lost more than max_degraded devices and,
4405 * if so, some requests might need to be failed.
4407 if (s
.failed
> conf
->max_degraded
|| s
.log_failed
) {
4408 sh
->check_state
= 0;
4409 sh
->reconstruct_state
= 0;
4410 break_stripe_batch_list(sh
, 0);
4411 if (s
.to_read
+s
.to_write
+s
.written
)
4412 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
4413 if (s
.syncing
+ s
.replacing
)
4414 handle_failed_sync(conf
, sh
, &s
);
4417 /* Now we check to see if any write operations have recently
4421 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
4423 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
4424 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
4425 sh
->reconstruct_state
= reconstruct_state_idle
;
4427 /* All the 'written' buffers and the parity block are ready to
4428 * be written back to disk
4430 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
4431 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
4432 BUG_ON(sh
->qd_idx
>= 0 &&
4433 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
4434 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
4435 for (i
= disks
; i
--; ) {
4436 struct r5dev
*dev
= &sh
->dev
[i
];
4437 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
4438 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
4439 dev
->written
|| test_bit(R5_InJournal
,
4441 pr_debug("Writing block %d\n", i
);
4442 set_bit(R5_Wantwrite
, &dev
->flags
);
4447 if (!test_bit(R5_Insync
, &dev
->flags
) ||
4448 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
4450 set_bit(STRIPE_INSYNC
, &sh
->state
);
4453 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4454 s
.dec_preread_active
= 1;
4458 * might be able to return some write requests if the parity blocks
4459 * are safe, or on a failed drive
4461 pdev
= &sh
->dev
[sh
->pd_idx
];
4462 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
4463 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
4464 qdev
= &sh
->dev
[sh
->qd_idx
];
4465 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
4466 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
4470 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
4471 && !test_bit(R5_LOCKED
, &pdev
->flags
)
4472 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
4473 test_bit(R5_Discard
, &pdev
->flags
))))) &&
4474 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
4475 && !test_bit(R5_LOCKED
, &qdev
->flags
)
4476 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
4477 test_bit(R5_Discard
, &qdev
->flags
))))))
4478 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
4481 r5c_handle_cached_data_endio(conf
, sh
, disks
, &s
.return_bi
);
4482 r5l_stripe_write_finished(sh
);
4484 /* Now we might consider reading some blocks, either to check/generate
4485 * parity, or to satisfy requests
4486 * or to load a block that is being partially written.
4488 if (s
.to_read
|| s
.non_overwrite
4489 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
4490 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
4493 handle_stripe_fill(sh
, &s
, disks
);
4496 * When the stripe finishes full journal write cycle (write to journal
4497 * and raid disk), this is the clean up procedure so it is ready for
4500 r5c_finish_stripe_write_out(conf
, sh
, &s
);
4503 * Now to consider new write requests, cache write back and what else,
4504 * if anything should be read. We do not handle new writes when:
4505 * 1/ A 'write' operation (copy+xor) is already in flight.
4506 * 2/ A 'check' operation is in flight, as it may clobber the parity
4508 * 3/ A r5c cache log write is in flight.
4511 if (!sh
->reconstruct_state
&& !sh
->check_state
&& !sh
->log_io
) {
4512 if (!r5c_is_writeback(conf
->log
)) {
4514 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
4515 } else { /* write back cache */
4518 /* First, try handle writes in caching phase */
4520 ret
= r5c_try_caching_write(conf
, sh
, &s
,
4523 * If caching phase failed: ret == -EAGAIN
4525 * stripe under reclaim: !caching && injournal
4527 * fall back to handle_stripe_dirtying()
4529 if (ret
== -EAGAIN
||
4530 /* stripe under reclaim: !caching && injournal */
4531 (!test_bit(STRIPE_R5C_CACHING
, &sh
->state
) &&
4533 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
4537 /* maybe we need to check and possibly fix the parity for this stripe
4538 * Any reads will already have been scheduled, so we just see if enough
4539 * data is available. The parity check is held off while parity
4540 * dependent operations are in flight.
4542 if (sh
->check_state
||
4543 (s
.syncing
&& s
.locked
== 0 &&
4544 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4545 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
4546 if (conf
->level
== 6)
4547 handle_parity_checks6(conf
, sh
, &s
, disks
);
4549 handle_parity_checks5(conf
, sh
, &s
, disks
);
4552 if ((s
.replacing
|| s
.syncing
) && s
.locked
== 0
4553 && !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)
4554 && !test_bit(STRIPE_REPLACED
, &sh
->state
)) {
4555 /* Write out to replacement devices where possible */
4556 for (i
= 0; i
< conf
->raid_disks
; i
++)
4557 if (test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
4558 WARN_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
4559 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
4560 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4564 set_bit(STRIPE_INSYNC
, &sh
->state
);
4565 set_bit(STRIPE_REPLACED
, &sh
->state
);
4567 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
4568 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
4569 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
4570 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4571 clear_bit(STRIPE_SYNCING
, &sh
->state
);
4572 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
4573 wake_up(&conf
->wait_for_overlap
);
4576 /* If the failed drives are just a ReadError, then we might need
4577 * to progress the repair/check process
4579 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
4580 for (i
= 0; i
< s
.failed
; i
++) {
4581 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
4582 if (test_bit(R5_ReadError
, &dev
->flags
)
4583 && !test_bit(R5_LOCKED
, &dev
->flags
)
4584 && test_bit(R5_UPTODATE
, &dev
->flags
)
4586 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
4587 set_bit(R5_Wantwrite
, &dev
->flags
);
4588 set_bit(R5_ReWrite
, &dev
->flags
);
4589 set_bit(R5_LOCKED
, &dev
->flags
);
4592 /* let's read it back */
4593 set_bit(R5_Wantread
, &dev
->flags
);
4594 set_bit(R5_LOCKED
, &dev
->flags
);
4600 /* Finish reconstruct operations initiated by the expansion process */
4601 if (sh
->reconstruct_state
== reconstruct_state_result
) {
4602 struct stripe_head
*sh_src
4603 = raid5_get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
4604 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
4605 /* sh cannot be written until sh_src has been read.
4606 * so arrange for sh to be delayed a little
4608 set_bit(STRIPE_DELAYED
, &sh
->state
);
4609 set_bit(STRIPE_HANDLE
, &sh
->state
);
4610 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
4612 atomic_inc(&conf
->preread_active_stripes
);
4613 raid5_release_stripe(sh_src
);
4617 raid5_release_stripe(sh_src
);
4619 sh
->reconstruct_state
= reconstruct_state_idle
;
4620 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
4621 for (i
= conf
->raid_disks
; i
--; ) {
4622 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
4623 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
4628 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
4629 !sh
->reconstruct_state
) {
4630 /* Need to write out all blocks after computing parity */
4631 sh
->disks
= conf
->raid_disks
;
4632 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
4633 schedule_reconstruction(sh
, &s
, 1, 1);
4634 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
4635 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4636 atomic_dec(&conf
->reshape_stripes
);
4637 wake_up(&conf
->wait_for_overlap
);
4638 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
4641 if (s
.expanding
&& s
.locked
== 0 &&
4642 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
4643 handle_stripe_expansion(conf
, sh
);
4646 /* wait for this device to become unblocked */
4647 if (unlikely(s
.blocked_rdev
)) {
4648 if (conf
->mddev
->external
)
4649 md_wait_for_blocked_rdev(s
.blocked_rdev
,
4652 /* Internal metadata will immediately
4653 * be written by raid5d, so we don't
4654 * need to wait here.
4656 rdev_dec_pending(s
.blocked_rdev
,
4660 if (s
.handle_bad_blocks
)
4661 for (i
= disks
; i
--; ) {
4662 struct md_rdev
*rdev
;
4663 struct r5dev
*dev
= &sh
->dev
[i
];
4664 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
4665 /* We own a safe reference to the rdev */
4666 rdev
= conf
->disks
[i
].rdev
;
4667 if (!rdev_set_badblocks(rdev
, sh
->sector
,
4669 md_error(conf
->mddev
, rdev
);
4670 rdev_dec_pending(rdev
, conf
->mddev
);
4672 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
4673 rdev
= conf
->disks
[i
].rdev
;
4674 rdev_clear_badblocks(rdev
, sh
->sector
,
4676 rdev_dec_pending(rdev
, conf
->mddev
);
4678 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
4679 rdev
= conf
->disks
[i
].replacement
;
4681 /* rdev have been moved down */
4682 rdev
= conf
->disks
[i
].rdev
;
4683 rdev_clear_badblocks(rdev
, sh
->sector
,
4685 rdev_dec_pending(rdev
, conf
->mddev
);
4690 raid_run_ops(sh
, s
.ops_request
);
4694 if (s
.dec_preread_active
) {
4695 /* We delay this until after ops_run_io so that if make_request
4696 * is waiting on a flush, it won't continue until the writes
4697 * have actually been submitted.
4699 atomic_dec(&conf
->preread_active_stripes
);
4700 if (atomic_read(&conf
->preread_active_stripes
) <
4702 md_wakeup_thread(conf
->mddev
->thread
);
4705 if (!bio_list_empty(&s
.return_bi
)) {
4706 if (test_bit(MD_CHANGE_PENDING
, &conf
->mddev
->flags
)) {
4707 spin_lock_irq(&conf
->device_lock
);
4708 bio_list_merge(&conf
->return_bi
, &s
.return_bi
);
4709 spin_unlock_irq(&conf
->device_lock
);
4710 md_wakeup_thread(conf
->mddev
->thread
);
4712 return_io(&s
.return_bi
);
4715 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4718 static void raid5_activate_delayed(struct r5conf
*conf
)
4720 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
4721 while (!list_empty(&conf
->delayed_list
)) {
4722 struct list_head
*l
= conf
->delayed_list
.next
;
4723 struct stripe_head
*sh
;
4724 sh
= list_entry(l
, struct stripe_head
, lru
);
4726 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4727 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4728 atomic_inc(&conf
->preread_active_stripes
);
4729 list_add_tail(&sh
->lru
, &conf
->hold_list
);
4730 raid5_wakeup_stripe_thread(sh
);
4735 static void activate_bit_delay(struct r5conf
*conf
,
4736 struct list_head
*temp_inactive_list
)
4738 /* device_lock is held */
4739 struct list_head head
;
4740 list_add(&head
, &conf
->bitmap_list
);
4741 list_del_init(&conf
->bitmap_list
);
4742 while (!list_empty(&head
)) {
4743 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
4745 list_del_init(&sh
->lru
);
4746 atomic_inc(&sh
->count
);
4747 hash
= sh
->hash_lock_index
;
4748 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
4752 static int raid5_congested(struct mddev
*mddev
, int bits
)
4754 struct r5conf
*conf
= mddev
->private;
4756 /* No difference between reads and writes. Just check
4757 * how busy the stripe_cache is
4760 if (test_bit(R5_INACTIVE_BLOCKED
, &conf
->cache_state
))
4764 if (atomic_read(&conf
->empty_inactive_list_nr
))
4770 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
4772 struct r5conf
*conf
= mddev
->private;
4773 sector_t sector
= bio
->bi_iter
.bi_sector
+ get_start_sect(bio
->bi_bdev
);
4774 unsigned int chunk_sectors
;
4775 unsigned int bio_sectors
= bio_sectors(bio
);
4777 chunk_sectors
= min(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
4778 return chunk_sectors
>=
4779 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
4783 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4784 * later sampled by raid5d.
4786 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
4788 unsigned long flags
;
4790 spin_lock_irqsave(&conf
->device_lock
, flags
);
4792 bi
->bi_next
= conf
->retry_read_aligned_list
;
4793 conf
->retry_read_aligned_list
= bi
;
4795 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4796 md_wakeup_thread(conf
->mddev
->thread
);
4799 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
4803 bi
= conf
->retry_read_aligned
;
4805 conf
->retry_read_aligned
= NULL
;
4808 bi
= conf
->retry_read_aligned_list
;
4810 conf
->retry_read_aligned_list
= bi
->bi_next
;
4813 * this sets the active strip count to 1 and the processed
4814 * strip count to zero (upper 8 bits)
4816 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
4823 * The "raid5_align_endio" should check if the read succeeded and if it
4824 * did, call bio_endio on the original bio (having bio_put the new bio
4826 * If the read failed..
4828 static void raid5_align_endio(struct bio
*bi
)
4830 struct bio
* raid_bi
= bi
->bi_private
;
4831 struct mddev
*mddev
;
4832 struct r5conf
*conf
;
4833 struct md_rdev
*rdev
;
4834 int error
= bi
->bi_error
;
4838 rdev
= (void*)raid_bi
->bi_next
;
4839 raid_bi
->bi_next
= NULL
;
4840 mddev
= rdev
->mddev
;
4841 conf
= mddev
->private;
4843 rdev_dec_pending(rdev
, conf
->mddev
);
4846 trace_block_bio_complete(bdev_get_queue(raid_bi
->bi_bdev
),
4849 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4850 wake_up(&conf
->wait_for_quiescent
);
4854 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
4856 add_bio_to_retry(raid_bi
, conf
);
4859 static int raid5_read_one_chunk(struct mddev
*mddev
, struct bio
*raid_bio
)
4861 struct r5conf
*conf
= mddev
->private;
4863 struct bio
* align_bi
;
4864 struct md_rdev
*rdev
;
4865 sector_t end_sector
;
4867 if (!in_chunk_boundary(mddev
, raid_bio
)) {
4868 pr_debug("%s: non aligned\n", __func__
);
4872 * use bio_clone_mddev to make a copy of the bio
4874 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
4878 * set bi_end_io to a new function, and set bi_private to the
4881 align_bi
->bi_end_io
= raid5_align_endio
;
4882 align_bi
->bi_private
= raid_bio
;
4886 align_bi
->bi_iter
.bi_sector
=
4887 raid5_compute_sector(conf
, raid_bio
->bi_iter
.bi_sector
,
4890 end_sector
= bio_end_sector(align_bi
);
4892 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
4893 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
4894 rdev
->recovery_offset
< end_sector
) {
4895 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
4897 (test_bit(Faulty
, &rdev
->flags
) ||
4898 !(test_bit(In_sync
, &rdev
->flags
) ||
4899 rdev
->recovery_offset
>= end_sector
)))
4906 atomic_inc(&rdev
->nr_pending
);
4908 raid_bio
->bi_next
= (void*)rdev
;
4909 align_bi
->bi_bdev
= rdev
->bdev
;
4910 bio_clear_flag(align_bi
, BIO_SEG_VALID
);
4912 if (is_badblock(rdev
, align_bi
->bi_iter
.bi_sector
,
4913 bio_sectors(align_bi
),
4914 &first_bad
, &bad_sectors
)) {
4916 rdev_dec_pending(rdev
, mddev
);
4920 /* No reshape active, so we can trust rdev->data_offset */
4921 align_bi
->bi_iter
.bi_sector
+= rdev
->data_offset
;
4923 spin_lock_irq(&conf
->device_lock
);
4924 wait_event_lock_irq(conf
->wait_for_quiescent
,
4927 atomic_inc(&conf
->active_aligned_reads
);
4928 spin_unlock_irq(&conf
->device_lock
);
4931 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
4932 align_bi
, disk_devt(mddev
->gendisk
),
4933 raid_bio
->bi_iter
.bi_sector
);
4934 generic_make_request(align_bi
);
4943 static struct bio
*chunk_aligned_read(struct mddev
*mddev
, struct bio
*raid_bio
)
4948 sector_t sector
= raid_bio
->bi_iter
.bi_sector
;
4949 unsigned chunk_sects
= mddev
->chunk_sectors
;
4950 unsigned sectors
= chunk_sects
- (sector
& (chunk_sects
-1));
4952 if (sectors
< bio_sectors(raid_bio
)) {
4953 split
= bio_split(raid_bio
, sectors
, GFP_NOIO
, fs_bio_set
);
4954 bio_chain(split
, raid_bio
);
4958 if (!raid5_read_one_chunk(mddev
, split
)) {
4959 if (split
!= raid_bio
)
4960 generic_make_request(raid_bio
);
4963 } while (split
!= raid_bio
);
4968 /* __get_priority_stripe - get the next stripe to process
4970 * Full stripe writes are allowed to pass preread active stripes up until
4971 * the bypass_threshold is exceeded. In general the bypass_count
4972 * increments when the handle_list is handled before the hold_list; however, it
4973 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4974 * stripe with in flight i/o. The bypass_count will be reset when the
4975 * head of the hold_list has changed, i.e. the head was promoted to the
4978 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
, int group
)
4980 struct stripe_head
*sh
= NULL
, *tmp
;
4981 struct list_head
*handle_list
= NULL
;
4982 struct r5worker_group
*wg
= NULL
;
4984 if (conf
->worker_cnt_per_group
== 0) {
4985 handle_list
= &conf
->handle_list
;
4986 } else if (group
!= ANY_GROUP
) {
4987 handle_list
= &conf
->worker_groups
[group
].handle_list
;
4988 wg
= &conf
->worker_groups
[group
];
4991 for (i
= 0; i
< conf
->group_cnt
; i
++) {
4992 handle_list
= &conf
->worker_groups
[i
].handle_list
;
4993 wg
= &conf
->worker_groups
[i
];
4994 if (!list_empty(handle_list
))
4999 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5001 list_empty(handle_list
) ? "empty" : "busy",
5002 list_empty(&conf
->hold_list
) ? "empty" : "busy",
5003 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
5005 if (!list_empty(handle_list
)) {
5006 sh
= list_entry(handle_list
->next
, typeof(*sh
), lru
);
5008 if (list_empty(&conf
->hold_list
))
5009 conf
->bypass_count
= 0;
5010 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
5011 if (conf
->hold_list
.next
== conf
->last_hold
)
5012 conf
->bypass_count
++;
5014 conf
->last_hold
= conf
->hold_list
.next
;
5015 conf
->bypass_count
-= conf
->bypass_threshold
;
5016 if (conf
->bypass_count
< 0)
5017 conf
->bypass_count
= 0;
5020 } else if (!list_empty(&conf
->hold_list
) &&
5021 ((conf
->bypass_threshold
&&
5022 conf
->bypass_count
> conf
->bypass_threshold
) ||
5023 atomic_read(&conf
->pending_full_writes
) == 0)) {
5025 list_for_each_entry(tmp
, &conf
->hold_list
, lru
) {
5026 if (conf
->worker_cnt_per_group
== 0 ||
5027 group
== ANY_GROUP
||
5028 !cpu_online(tmp
->cpu
) ||
5029 cpu_to_group(tmp
->cpu
) == group
) {
5036 conf
->bypass_count
-= conf
->bypass_threshold
;
5037 if (conf
->bypass_count
< 0)
5038 conf
->bypass_count
= 0;
5050 list_del_init(&sh
->lru
);
5051 BUG_ON(atomic_inc_return(&sh
->count
) != 1);
5055 struct raid5_plug_cb
{
5056 struct blk_plug_cb cb
;
5057 struct list_head list
;
5058 struct list_head temp_inactive_list
[NR_STRIPE_HASH_LOCKS
];
5061 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
5063 struct raid5_plug_cb
*cb
= container_of(
5064 blk_cb
, struct raid5_plug_cb
, cb
);
5065 struct stripe_head
*sh
;
5066 struct mddev
*mddev
= cb
->cb
.data
;
5067 struct r5conf
*conf
= mddev
->private;
5071 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
5072 spin_lock_irq(&conf
->device_lock
);
5073 while (!list_empty(&cb
->list
)) {
5074 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
5075 list_del_init(&sh
->lru
);
5077 * avoid race release_stripe_plug() sees
5078 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5079 * is still in our list
5081 smp_mb__before_atomic();
5082 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
5084 * STRIPE_ON_RELEASE_LIST could be set here. In that
5085 * case, the count is always > 1 here
5087 hash
= sh
->hash_lock_index
;
5088 __release_stripe(conf
, sh
, &cb
->temp_inactive_list
[hash
]);
5091 spin_unlock_irq(&conf
->device_lock
);
5093 release_inactive_stripe_list(conf
, cb
->temp_inactive_list
,
5094 NR_STRIPE_HASH_LOCKS
);
5096 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
5100 static void release_stripe_plug(struct mddev
*mddev
,
5101 struct stripe_head
*sh
)
5103 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
5104 raid5_unplug
, mddev
,
5105 sizeof(struct raid5_plug_cb
));
5106 struct raid5_plug_cb
*cb
;
5109 raid5_release_stripe(sh
);
5113 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
5115 if (cb
->list
.next
== NULL
) {
5117 INIT_LIST_HEAD(&cb
->list
);
5118 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5119 INIT_LIST_HEAD(cb
->temp_inactive_list
+ i
);
5122 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
5123 list_add_tail(&sh
->lru
, &cb
->list
);
5125 raid5_release_stripe(sh
);
5128 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
5130 struct r5conf
*conf
= mddev
->private;
5131 sector_t logical_sector
, last_sector
;
5132 struct stripe_head
*sh
;
5136 if (mddev
->reshape_position
!= MaxSector
)
5137 /* Skip discard while reshape is happening */
5140 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5141 last_sector
= bi
->bi_iter
.bi_sector
+ (bi
->bi_iter
.bi_size
>>9);
5144 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5146 stripe_sectors
= conf
->chunk_sectors
*
5147 (conf
->raid_disks
- conf
->max_degraded
);
5148 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
5150 sector_div(last_sector
, stripe_sectors
);
5152 logical_sector
*= conf
->chunk_sectors
;
5153 last_sector
*= conf
->chunk_sectors
;
5155 for (; logical_sector
< last_sector
;
5156 logical_sector
+= STRIPE_SECTORS
) {
5160 sh
= raid5_get_active_stripe(conf
, logical_sector
, 0, 0, 0);
5161 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5162 TASK_UNINTERRUPTIBLE
);
5163 set_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5164 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
5165 raid5_release_stripe(sh
);
5169 clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
5170 spin_lock_irq(&sh
->stripe_lock
);
5171 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5172 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5174 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
5175 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
5176 spin_unlock_irq(&sh
->stripe_lock
);
5177 raid5_release_stripe(sh
);
5182 set_bit(STRIPE_DISCARD
, &sh
->state
);
5183 finish_wait(&conf
->wait_for_overlap
, &w
);
5184 sh
->overwrite_disks
= 0;
5185 for (d
= 0; d
< conf
->raid_disks
; d
++) {
5186 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
5188 sh
->dev
[d
].towrite
= bi
;
5189 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
5190 raid5_inc_bi_active_stripes(bi
);
5191 sh
->overwrite_disks
++;
5193 spin_unlock_irq(&sh
->stripe_lock
);
5194 if (conf
->mddev
->bitmap
) {
5196 d
< conf
->raid_disks
- conf
->max_degraded
;
5198 bitmap_startwrite(mddev
->bitmap
,
5202 sh
->bm_seq
= conf
->seq_flush
+ 1;
5203 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
5206 set_bit(STRIPE_HANDLE
, &sh
->state
);
5207 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5208 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5209 atomic_inc(&conf
->preread_active_stripes
);
5210 release_stripe_plug(mddev
, sh
);
5213 remaining
= raid5_dec_bi_active_stripes(bi
);
5214 if (remaining
== 0) {
5215 md_write_end(mddev
);
5220 static void raid5_make_request(struct mddev
*mddev
, struct bio
* bi
)
5222 struct r5conf
*conf
= mddev
->private;
5224 sector_t new_sector
;
5225 sector_t logical_sector
, last_sector
;
5226 struct stripe_head
*sh
;
5227 const int rw
= bio_data_dir(bi
);
5232 if (unlikely(bi
->bi_opf
& REQ_PREFLUSH
)) {
5233 int ret
= r5l_handle_flush_request(conf
->log
, bi
);
5237 if (ret
== -ENODEV
) {
5238 md_flush_request(mddev
, bi
);
5241 /* ret == -EAGAIN, fallback */
5244 md_write_start(mddev
, bi
);
5247 * If array is degraded, better not do chunk aligned read because
5248 * later we might have to read it again in order to reconstruct
5249 * data on failed drives.
5251 if (rw
== READ
&& mddev
->degraded
== 0 &&
5252 !r5c_is_writeback(conf
->log
) &&
5253 mddev
->reshape_position
== MaxSector
) {
5254 bi
= chunk_aligned_read(mddev
, bi
);
5259 if (unlikely(bio_op(bi
) == REQ_OP_DISCARD
)) {
5260 make_discard_request(mddev
, bi
);
5264 logical_sector
= bi
->bi_iter
.bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5265 last_sector
= bio_end_sector(bi
);
5267 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
5269 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
5270 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
5276 seq
= read_seqcount_begin(&conf
->gen_lock
);
5279 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
5280 TASK_UNINTERRUPTIBLE
);
5281 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
5282 /* spinlock is needed as reshape_progress may be
5283 * 64bit on a 32bit platform, and so it might be
5284 * possible to see a half-updated value
5285 * Of course reshape_progress could change after
5286 * the lock is dropped, so once we get a reference
5287 * to the stripe that we think it is, we will have
5290 spin_lock_irq(&conf
->device_lock
);
5291 if (mddev
->reshape_backwards
5292 ? logical_sector
< conf
->reshape_progress
5293 : logical_sector
>= conf
->reshape_progress
) {
5296 if (mddev
->reshape_backwards
5297 ? logical_sector
< conf
->reshape_safe
5298 : logical_sector
>= conf
->reshape_safe
) {
5299 spin_unlock_irq(&conf
->device_lock
);
5305 spin_unlock_irq(&conf
->device_lock
);
5308 new_sector
= raid5_compute_sector(conf
, logical_sector
,
5311 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
5312 (unsigned long long)new_sector
,
5313 (unsigned long long)logical_sector
);
5315 sh
= raid5_get_active_stripe(conf
, new_sector
, previous
,
5316 (bi
->bi_opf
& REQ_RAHEAD
), 0);
5318 if (unlikely(previous
)) {
5319 /* expansion might have moved on while waiting for a
5320 * stripe, so we must do the range check again.
5321 * Expansion could still move past after this
5322 * test, but as we are holding a reference to
5323 * 'sh', we know that if that happens,
5324 * STRIPE_EXPANDING will get set and the expansion
5325 * won't proceed until we finish with the stripe.
5328 spin_lock_irq(&conf
->device_lock
);
5329 if (mddev
->reshape_backwards
5330 ? logical_sector
>= conf
->reshape_progress
5331 : logical_sector
< conf
->reshape_progress
)
5332 /* mismatch, need to try again */
5334 spin_unlock_irq(&conf
->device_lock
);
5336 raid5_release_stripe(sh
);
5342 if (read_seqcount_retry(&conf
->gen_lock
, seq
)) {
5343 /* Might have got the wrong stripe_head
5346 raid5_release_stripe(sh
);
5351 logical_sector
>= mddev
->suspend_lo
&&
5352 logical_sector
< mddev
->suspend_hi
) {
5353 raid5_release_stripe(sh
);
5354 /* As the suspend_* range is controlled by
5355 * userspace, we want an interruptible
5358 flush_signals(current
);
5359 prepare_to_wait(&conf
->wait_for_overlap
,
5360 &w
, TASK_INTERRUPTIBLE
);
5361 if (logical_sector
>= mddev
->suspend_lo
&&
5362 logical_sector
< mddev
->suspend_hi
) {
5369 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
5370 !add_stripe_bio(sh
, bi
, dd_idx
, rw
, previous
)) {
5371 /* Stripe is busy expanding or
5372 * add failed due to overlap. Flush everything
5375 md_wakeup_thread(mddev
->thread
);
5376 raid5_release_stripe(sh
);
5381 set_bit(STRIPE_HANDLE
, &sh
->state
);
5382 clear_bit(STRIPE_DELAYED
, &sh
->state
);
5383 if ((!sh
->batch_head
|| sh
== sh
->batch_head
) &&
5384 (bi
->bi_opf
& REQ_SYNC
) &&
5385 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
5386 atomic_inc(&conf
->preread_active_stripes
);
5387 release_stripe_plug(mddev
, sh
);
5389 /* cannot get stripe for read-ahead, just give-up */
5390 bi
->bi_error
= -EIO
;
5394 finish_wait(&conf
->wait_for_overlap
, &w
);
5396 remaining
= raid5_dec_bi_active_stripes(bi
);
5397 if (remaining
== 0) {
5400 md_write_end(mddev
);
5402 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
5408 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
5410 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
5412 /* reshaping is quite different to recovery/resync so it is
5413 * handled quite separately ... here.
5415 * On each call to sync_request, we gather one chunk worth of
5416 * destination stripes and flag them as expanding.
5417 * Then we find all the source stripes and request reads.
5418 * As the reads complete, handle_stripe will copy the data
5419 * into the destination stripe and release that stripe.
5421 struct r5conf
*conf
= mddev
->private;
5422 struct stripe_head
*sh
;
5423 sector_t first_sector
, last_sector
;
5424 int raid_disks
= conf
->previous_raid_disks
;
5425 int data_disks
= raid_disks
- conf
->max_degraded
;
5426 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5429 sector_t writepos
, readpos
, safepos
;
5430 sector_t stripe_addr
;
5431 int reshape_sectors
;
5432 struct list_head stripes
;
5435 if (sector_nr
== 0) {
5436 /* If restarting in the middle, skip the initial sectors */
5437 if (mddev
->reshape_backwards
&&
5438 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
5439 sector_nr
= raid5_size(mddev
, 0, 0)
5440 - conf
->reshape_progress
;
5441 } else if (mddev
->reshape_backwards
&&
5442 conf
->reshape_progress
== MaxSector
) {
5443 /* shouldn't happen, but just in case, finish up.*/
5444 sector_nr
= MaxSector
;
5445 } else if (!mddev
->reshape_backwards
&&
5446 conf
->reshape_progress
> 0)
5447 sector_nr
= conf
->reshape_progress
;
5448 sector_div(sector_nr
, new_data_disks
);
5450 mddev
->curr_resync_completed
= sector_nr
;
5451 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5458 /* We need to process a full chunk at a time.
5459 * If old and new chunk sizes differ, we need to process the
5463 reshape_sectors
= max(conf
->chunk_sectors
, conf
->prev_chunk_sectors
);
5465 /* We update the metadata at least every 10 seconds, or when
5466 * the data about to be copied would over-write the source of
5467 * the data at the front of the range. i.e. one new_stripe
5468 * along from reshape_progress new_maps to after where
5469 * reshape_safe old_maps to
5471 writepos
= conf
->reshape_progress
;
5472 sector_div(writepos
, new_data_disks
);
5473 readpos
= conf
->reshape_progress
;
5474 sector_div(readpos
, data_disks
);
5475 safepos
= conf
->reshape_safe
;
5476 sector_div(safepos
, data_disks
);
5477 if (mddev
->reshape_backwards
) {
5478 BUG_ON(writepos
< reshape_sectors
);
5479 writepos
-= reshape_sectors
;
5480 readpos
+= reshape_sectors
;
5481 safepos
+= reshape_sectors
;
5483 writepos
+= reshape_sectors
;
5484 /* readpos and safepos are worst-case calculations.
5485 * A negative number is overly pessimistic, and causes
5486 * obvious problems for unsigned storage. So clip to 0.
5488 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
5489 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
5492 /* Having calculated the 'writepos' possibly use it
5493 * to set 'stripe_addr' which is where we will write to.
5495 if (mddev
->reshape_backwards
) {
5496 BUG_ON(conf
->reshape_progress
== 0);
5497 stripe_addr
= writepos
;
5498 BUG_ON((mddev
->dev_sectors
&
5499 ~((sector_t
)reshape_sectors
- 1))
5500 - reshape_sectors
- stripe_addr
5503 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
5504 stripe_addr
= sector_nr
;
5507 /* 'writepos' is the most advanced device address we might write.
5508 * 'readpos' is the least advanced device address we might read.
5509 * 'safepos' is the least address recorded in the metadata as having
5511 * If there is a min_offset_diff, these are adjusted either by
5512 * increasing the safepos/readpos if diff is negative, or
5513 * increasing writepos if diff is positive.
5514 * If 'readpos' is then behind 'writepos', there is no way that we can
5515 * ensure safety in the face of a crash - that must be done by userspace
5516 * making a backup of the data. So in that case there is no particular
5517 * rush to update metadata.
5518 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5519 * update the metadata to advance 'safepos' to match 'readpos' so that
5520 * we can be safe in the event of a crash.
5521 * So we insist on updating metadata if safepos is behind writepos and
5522 * readpos is beyond writepos.
5523 * In any case, update the metadata every 10 seconds.
5524 * Maybe that number should be configurable, but I'm not sure it is
5525 * worth it.... maybe it could be a multiple of safemode_delay???
5527 if (conf
->min_offset_diff
< 0) {
5528 safepos
+= -conf
->min_offset_diff
;
5529 readpos
+= -conf
->min_offset_diff
;
5531 writepos
+= conf
->min_offset_diff
;
5533 if ((mddev
->reshape_backwards
5534 ? (safepos
> writepos
&& readpos
< writepos
)
5535 : (safepos
< writepos
&& readpos
> writepos
)) ||
5536 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
5537 /* Cannot proceed until we've updated the superblock... */
5538 wait_event(conf
->wait_for_overlap
,
5539 atomic_read(&conf
->reshape_stripes
)==0
5540 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5541 if (atomic_read(&conf
->reshape_stripes
) != 0)
5543 mddev
->reshape_position
= conf
->reshape_progress
;
5544 mddev
->curr_resync_completed
= sector_nr
;
5545 conf
->reshape_checkpoint
= jiffies
;
5546 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5547 md_wakeup_thread(mddev
->thread
);
5548 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
5549 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5550 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5552 spin_lock_irq(&conf
->device_lock
);
5553 conf
->reshape_safe
= mddev
->reshape_position
;
5554 spin_unlock_irq(&conf
->device_lock
);
5555 wake_up(&conf
->wait_for_overlap
);
5556 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5559 INIT_LIST_HEAD(&stripes
);
5560 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
5562 int skipped_disk
= 0;
5563 sh
= raid5_get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
5564 set_bit(STRIPE_EXPANDING
, &sh
->state
);
5565 atomic_inc(&conf
->reshape_stripes
);
5566 /* If any of this stripe is beyond the end of the old
5567 * array, then we need to zero those blocks
5569 for (j
=sh
->disks
; j
--;) {
5571 if (j
== sh
->pd_idx
)
5573 if (conf
->level
== 6 &&
5576 s
= raid5_compute_blocknr(sh
, j
, 0);
5577 if (s
< raid5_size(mddev
, 0, 0)) {
5581 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
5582 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
5583 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
5585 if (!skipped_disk
) {
5586 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
5587 set_bit(STRIPE_HANDLE
, &sh
->state
);
5589 list_add(&sh
->lru
, &stripes
);
5591 spin_lock_irq(&conf
->device_lock
);
5592 if (mddev
->reshape_backwards
)
5593 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
5595 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
5596 spin_unlock_irq(&conf
->device_lock
);
5597 /* Ok, those stripe are ready. We can start scheduling
5598 * reads on the source stripes.
5599 * The source stripes are determined by mapping the first and last
5600 * block on the destination stripes.
5603 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
5606 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
5607 * new_data_disks
- 1),
5609 if (last_sector
>= mddev
->dev_sectors
)
5610 last_sector
= mddev
->dev_sectors
- 1;
5611 while (first_sector
<= last_sector
) {
5612 sh
= raid5_get_active_stripe(conf
, first_sector
, 1, 0, 1);
5613 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
5614 set_bit(STRIPE_HANDLE
, &sh
->state
);
5615 raid5_release_stripe(sh
);
5616 first_sector
+= STRIPE_SECTORS
;
5618 /* Now that the sources are clearly marked, we can release
5619 * the destination stripes
5621 while (!list_empty(&stripes
)) {
5622 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
5623 list_del_init(&sh
->lru
);
5624 raid5_release_stripe(sh
);
5626 /* If this takes us to the resync_max point where we have to pause,
5627 * then we need to write out the superblock.
5629 sector_nr
+= reshape_sectors
;
5630 retn
= reshape_sectors
;
5632 if (mddev
->curr_resync_completed
> mddev
->resync_max
||
5633 (sector_nr
- mddev
->curr_resync_completed
) * 2
5634 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
5635 /* Cannot proceed until we've updated the superblock... */
5636 wait_event(conf
->wait_for_overlap
,
5637 atomic_read(&conf
->reshape_stripes
) == 0
5638 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5639 if (atomic_read(&conf
->reshape_stripes
) != 0)
5641 mddev
->reshape_position
= conf
->reshape_progress
;
5642 mddev
->curr_resync_completed
= sector_nr
;
5643 conf
->reshape_checkpoint
= jiffies
;
5644 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5645 md_wakeup_thread(mddev
->thread
);
5646 wait_event(mddev
->sb_wait
,
5647 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
5648 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
5649 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5651 spin_lock_irq(&conf
->device_lock
);
5652 conf
->reshape_safe
= mddev
->reshape_position
;
5653 spin_unlock_irq(&conf
->device_lock
);
5654 wake_up(&conf
->wait_for_overlap
);
5655 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
5661 static inline sector_t
raid5_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
5664 struct r5conf
*conf
= mddev
->private;
5665 struct stripe_head
*sh
;
5666 sector_t max_sector
= mddev
->dev_sectors
;
5667 sector_t sync_blocks
;
5668 int still_degraded
= 0;
5671 if (sector_nr
>= max_sector
) {
5672 /* just being told to finish up .. nothing much to do */
5674 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
5679 if (mddev
->curr_resync
< max_sector
) /* aborted */
5680 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
5682 else /* completed sync */
5684 bitmap_close_sync(mddev
->bitmap
);
5689 /* Allow raid5_quiesce to complete */
5690 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
5692 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
5693 return reshape_request(mddev
, sector_nr
, skipped
);
5695 /* No need to check resync_max as we never do more than one
5696 * stripe, and as resync_max will always be on a chunk boundary,
5697 * if the check in md_do_sync didn't fire, there is no chance
5698 * of overstepping resync_max here
5701 /* if there is too many failed drives and we are trying
5702 * to resync, then assert that we are finished, because there is
5703 * nothing we can do.
5705 if (mddev
->degraded
>= conf
->max_degraded
&&
5706 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
5707 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
5711 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
5713 !bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
5714 sync_blocks
>= STRIPE_SECTORS
) {
5715 /* we can skip this block, and probably more */
5716 sync_blocks
/= STRIPE_SECTORS
;
5718 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
5721 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
, false);
5723 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 1, 0);
5725 sh
= raid5_get_active_stripe(conf
, sector_nr
, 0, 0, 0);
5726 /* make sure we don't swamp the stripe cache if someone else
5727 * is trying to get access
5729 schedule_timeout_uninterruptible(1);
5731 /* Need to check if array will still be degraded after recovery/resync
5732 * Note in case of > 1 drive failures it's possible we're rebuilding
5733 * one drive while leaving another faulty drive in array.
5736 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5737 struct md_rdev
*rdev
= ACCESS_ONCE(conf
->disks
[i
].rdev
);
5739 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
))
5744 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
5746 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
5747 set_bit(STRIPE_HANDLE
, &sh
->state
);
5749 raid5_release_stripe(sh
);
5751 return STRIPE_SECTORS
;
5754 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
5756 /* We may not be able to submit a whole bio at once as there
5757 * may not be enough stripe_heads available.
5758 * We cannot pre-allocate enough stripe_heads as we may need
5759 * more than exist in the cache (if we allow ever large chunks).
5760 * So we do one stripe head at a time and record in
5761 * ->bi_hw_segments how many have been done.
5763 * We *know* that this entire raid_bio is in one chunk, so
5764 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5766 struct stripe_head
*sh
;
5768 sector_t sector
, logical_sector
, last_sector
;
5773 logical_sector
= raid_bio
->bi_iter
.bi_sector
&
5774 ~((sector_t
)STRIPE_SECTORS
-1);
5775 sector
= raid5_compute_sector(conf
, logical_sector
,
5777 last_sector
= bio_end_sector(raid_bio
);
5779 for (; logical_sector
< last_sector
;
5780 logical_sector
+= STRIPE_SECTORS
,
5781 sector
+= STRIPE_SECTORS
,
5784 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
5785 /* already done this stripe */
5788 sh
= raid5_get_active_stripe(conf
, sector
, 0, 1, 1);
5791 /* failed to get a stripe - must wait */
5792 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5793 conf
->retry_read_aligned
= raid_bio
;
5797 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0, 0)) {
5798 raid5_release_stripe(sh
);
5799 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5800 conf
->retry_read_aligned
= raid_bio
;
5804 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
5806 raid5_release_stripe(sh
);
5809 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
5810 if (remaining
== 0) {
5811 trace_block_bio_complete(bdev_get_queue(raid_bio
->bi_bdev
),
5813 bio_endio(raid_bio
);
5815 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
5816 wake_up(&conf
->wait_for_quiescent
);
5820 static int handle_active_stripes(struct r5conf
*conf
, int group
,
5821 struct r5worker
*worker
,
5822 struct list_head
*temp_inactive_list
)
5824 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
5825 int i
, batch_size
= 0, hash
;
5826 bool release_inactive
= false;
5828 while (batch_size
< MAX_STRIPE_BATCH
&&
5829 (sh
= __get_priority_stripe(conf
, group
)) != NULL
)
5830 batch
[batch_size
++] = sh
;
5832 if (batch_size
== 0) {
5833 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5834 if (!list_empty(temp_inactive_list
+ i
))
5836 if (i
== NR_STRIPE_HASH_LOCKS
) {
5837 spin_unlock_irq(&conf
->device_lock
);
5838 r5l_flush_stripe_to_raid(conf
->log
);
5839 spin_lock_irq(&conf
->device_lock
);
5842 release_inactive
= true;
5844 spin_unlock_irq(&conf
->device_lock
);
5846 release_inactive_stripe_list(conf
, temp_inactive_list
,
5847 NR_STRIPE_HASH_LOCKS
);
5849 r5l_flush_stripe_to_raid(conf
->log
);
5850 if (release_inactive
) {
5851 spin_lock_irq(&conf
->device_lock
);
5855 for (i
= 0; i
< batch_size
; i
++)
5856 handle_stripe(batch
[i
]);
5857 r5l_write_stripe_run(conf
->log
);
5861 spin_lock_irq(&conf
->device_lock
);
5862 for (i
= 0; i
< batch_size
; i
++) {
5863 hash
= batch
[i
]->hash_lock_index
;
5864 __release_stripe(conf
, batch
[i
], &temp_inactive_list
[hash
]);
5869 static void raid5_do_work(struct work_struct
*work
)
5871 struct r5worker
*worker
= container_of(work
, struct r5worker
, work
);
5872 struct r5worker_group
*group
= worker
->group
;
5873 struct r5conf
*conf
= group
->conf
;
5874 int group_id
= group
- conf
->worker_groups
;
5876 struct blk_plug plug
;
5878 pr_debug("+++ raid5worker active\n");
5880 blk_start_plug(&plug
);
5882 spin_lock_irq(&conf
->device_lock
);
5884 int batch_size
, released
;
5886 released
= release_stripe_list(conf
, worker
->temp_inactive_list
);
5888 batch_size
= handle_active_stripes(conf
, group_id
, worker
,
5889 worker
->temp_inactive_list
);
5890 worker
->working
= false;
5891 if (!batch_size
&& !released
)
5893 handled
+= batch_size
;
5895 pr_debug("%d stripes handled\n", handled
);
5897 spin_unlock_irq(&conf
->device_lock
);
5898 blk_finish_plug(&plug
);
5900 pr_debug("--- raid5worker inactive\n");
5904 * This is our raid5 kernel thread.
5906 * We scan the hash table for stripes which can be handled now.
5907 * During the scan, completed stripes are saved for us by the interrupt
5908 * handler, so that they will not have to wait for our next wakeup.
5910 static void raid5d(struct md_thread
*thread
)
5912 struct mddev
*mddev
= thread
->mddev
;
5913 struct r5conf
*conf
= mddev
->private;
5915 struct blk_plug plug
;
5917 pr_debug("+++ raid5d active\n");
5919 md_check_recovery(mddev
);
5921 if (!bio_list_empty(&conf
->return_bi
) &&
5922 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
)) {
5923 struct bio_list tmp
= BIO_EMPTY_LIST
;
5924 spin_lock_irq(&conf
->device_lock
);
5925 if (!test_bit(MD_CHANGE_PENDING
, &mddev
->flags
)) {
5926 bio_list_merge(&tmp
, &conf
->return_bi
);
5927 bio_list_init(&conf
->return_bi
);
5929 spin_unlock_irq(&conf
->device_lock
);
5933 blk_start_plug(&plug
);
5935 spin_lock_irq(&conf
->device_lock
);
5938 int batch_size
, released
;
5940 released
= release_stripe_list(conf
, conf
->temp_inactive_list
);
5942 clear_bit(R5_DID_ALLOC
, &conf
->cache_state
);
5945 !list_empty(&conf
->bitmap_list
)) {
5946 /* Now is a good time to flush some bitmap updates */
5948 spin_unlock_irq(&conf
->device_lock
);
5949 bitmap_unplug(mddev
->bitmap
);
5950 spin_lock_irq(&conf
->device_lock
);
5951 conf
->seq_write
= conf
->seq_flush
;
5952 activate_bit_delay(conf
, conf
->temp_inactive_list
);
5954 raid5_activate_delayed(conf
);
5956 while ((bio
= remove_bio_from_retry(conf
))) {
5958 spin_unlock_irq(&conf
->device_lock
);
5959 ok
= retry_aligned_read(conf
, bio
);
5960 spin_lock_irq(&conf
->device_lock
);
5966 batch_size
= handle_active_stripes(conf
, ANY_GROUP
, NULL
,
5967 conf
->temp_inactive_list
);
5968 if (!batch_size
&& !released
)
5970 handled
+= batch_size
;
5972 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
)) {
5973 spin_unlock_irq(&conf
->device_lock
);
5974 md_check_recovery(mddev
);
5975 spin_lock_irq(&conf
->device_lock
);
5978 pr_debug("%d stripes handled\n", handled
);
5980 spin_unlock_irq(&conf
->device_lock
);
5981 if (test_and_clear_bit(R5_ALLOC_MORE
, &conf
->cache_state
) &&
5982 mutex_trylock(&conf
->cache_size_mutex
)) {
5983 grow_one_stripe(conf
, __GFP_NOWARN
);
5984 /* Set flag even if allocation failed. This helps
5985 * slow down allocation requests when mem is short
5987 set_bit(R5_DID_ALLOC
, &conf
->cache_state
);
5988 mutex_unlock(&conf
->cache_size_mutex
);
5991 r5l_flush_stripe_to_raid(conf
->log
);
5993 async_tx_issue_pending_all();
5994 blk_finish_plug(&plug
);
5996 pr_debug("--- raid5d inactive\n");
6000 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
6002 struct r5conf
*conf
;
6004 spin_lock(&mddev
->lock
);
6005 conf
= mddev
->private;
6007 ret
= sprintf(page
, "%d\n", conf
->min_nr_stripes
);
6008 spin_unlock(&mddev
->lock
);
6013 raid5_set_cache_size(struct mddev
*mddev
, int size
)
6015 struct r5conf
*conf
= mddev
->private;
6018 if (size
<= 16 || size
> 32768)
6021 conf
->min_nr_stripes
= size
;
6022 mutex_lock(&conf
->cache_size_mutex
);
6023 while (size
< conf
->max_nr_stripes
&&
6024 drop_one_stripe(conf
))
6026 mutex_unlock(&conf
->cache_size_mutex
);
6029 err
= md_allow_write(mddev
);
6033 mutex_lock(&conf
->cache_size_mutex
);
6034 while (size
> conf
->max_nr_stripes
)
6035 if (!grow_one_stripe(conf
, GFP_KERNEL
))
6037 mutex_unlock(&conf
->cache_size_mutex
);
6041 EXPORT_SYMBOL(raid5_set_cache_size
);
6044 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
6046 struct r5conf
*conf
;
6050 if (len
>= PAGE_SIZE
)
6052 if (kstrtoul(page
, 10, &new))
6054 err
= mddev_lock(mddev
);
6057 conf
= mddev
->private;
6061 err
= raid5_set_cache_size(mddev
, new);
6062 mddev_unlock(mddev
);
6067 static struct md_sysfs_entry
6068 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
6069 raid5_show_stripe_cache_size
,
6070 raid5_store_stripe_cache_size
);
6073 raid5_show_rmw_level(struct mddev
*mddev
, char *page
)
6075 struct r5conf
*conf
= mddev
->private;
6077 return sprintf(page
, "%d\n", conf
->rmw_level
);
6083 raid5_store_rmw_level(struct mddev
*mddev
, const char *page
, size_t len
)
6085 struct r5conf
*conf
= mddev
->private;
6091 if (len
>= PAGE_SIZE
)
6094 if (kstrtoul(page
, 10, &new))
6097 if (new != PARITY_DISABLE_RMW
&& !raid6_call
.xor_syndrome
)
6100 if (new != PARITY_DISABLE_RMW
&&
6101 new != PARITY_ENABLE_RMW
&&
6102 new != PARITY_PREFER_RMW
)
6105 conf
->rmw_level
= new;
6109 static struct md_sysfs_entry
6110 raid5_rmw_level
= __ATTR(rmw_level
, S_IRUGO
| S_IWUSR
,
6111 raid5_show_rmw_level
,
6112 raid5_store_rmw_level
);
6116 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
6118 struct r5conf
*conf
;
6120 spin_lock(&mddev
->lock
);
6121 conf
= mddev
->private;
6123 ret
= sprintf(page
, "%d\n", conf
->bypass_threshold
);
6124 spin_unlock(&mddev
->lock
);
6129 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
6131 struct r5conf
*conf
;
6135 if (len
>= PAGE_SIZE
)
6137 if (kstrtoul(page
, 10, &new))
6140 err
= mddev_lock(mddev
);
6143 conf
= mddev
->private;
6146 else if (new > conf
->min_nr_stripes
)
6149 conf
->bypass_threshold
= new;
6150 mddev_unlock(mddev
);
6154 static struct md_sysfs_entry
6155 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
6157 raid5_show_preread_threshold
,
6158 raid5_store_preread_threshold
);
6161 raid5_show_skip_copy(struct mddev
*mddev
, char *page
)
6163 struct r5conf
*conf
;
6165 spin_lock(&mddev
->lock
);
6166 conf
= mddev
->private;
6168 ret
= sprintf(page
, "%d\n", conf
->skip_copy
);
6169 spin_unlock(&mddev
->lock
);
6174 raid5_store_skip_copy(struct mddev
*mddev
, const char *page
, size_t len
)
6176 struct r5conf
*conf
;
6180 if (len
>= PAGE_SIZE
)
6182 if (kstrtoul(page
, 10, &new))
6186 err
= mddev_lock(mddev
);
6189 conf
= mddev
->private;
6192 else if (new != conf
->skip_copy
) {
6193 mddev_suspend(mddev
);
6194 conf
->skip_copy
= new;
6196 mddev
->queue
->backing_dev_info
.capabilities
|=
6197 BDI_CAP_STABLE_WRITES
;
6199 mddev
->queue
->backing_dev_info
.capabilities
&=
6200 ~BDI_CAP_STABLE_WRITES
;
6201 mddev_resume(mddev
);
6203 mddev_unlock(mddev
);
6207 static struct md_sysfs_entry
6208 raid5_skip_copy
= __ATTR(skip_copy
, S_IRUGO
| S_IWUSR
,
6209 raid5_show_skip_copy
,
6210 raid5_store_skip_copy
);
6213 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
6215 struct r5conf
*conf
= mddev
->private;
6217 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
6222 static struct md_sysfs_entry
6223 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
6226 raid5_show_group_thread_cnt(struct mddev
*mddev
, char *page
)
6228 struct r5conf
*conf
;
6230 spin_lock(&mddev
->lock
);
6231 conf
= mddev
->private;
6233 ret
= sprintf(page
, "%d\n", conf
->worker_cnt_per_group
);
6234 spin_unlock(&mddev
->lock
);
6238 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6240 int *worker_cnt_per_group
,
6241 struct r5worker_group
**worker_groups
);
6243 raid5_store_group_thread_cnt(struct mddev
*mddev
, const char *page
, size_t len
)
6245 struct r5conf
*conf
;
6248 struct r5worker_group
*new_groups
, *old_groups
;
6249 int group_cnt
, worker_cnt_per_group
;
6251 if (len
>= PAGE_SIZE
)
6253 if (kstrtoul(page
, 10, &new))
6256 err
= mddev_lock(mddev
);
6259 conf
= mddev
->private;
6262 else if (new != conf
->worker_cnt_per_group
) {
6263 mddev_suspend(mddev
);
6265 old_groups
= conf
->worker_groups
;
6267 flush_workqueue(raid5_wq
);
6269 err
= alloc_thread_groups(conf
, new,
6270 &group_cnt
, &worker_cnt_per_group
,
6273 spin_lock_irq(&conf
->device_lock
);
6274 conf
->group_cnt
= group_cnt
;
6275 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6276 conf
->worker_groups
= new_groups
;
6277 spin_unlock_irq(&conf
->device_lock
);
6280 kfree(old_groups
[0].workers
);
6283 mddev_resume(mddev
);
6285 mddev_unlock(mddev
);
6290 static struct md_sysfs_entry
6291 raid5_group_thread_cnt
= __ATTR(group_thread_cnt
, S_IRUGO
| S_IWUSR
,
6292 raid5_show_group_thread_cnt
,
6293 raid5_store_group_thread_cnt
);
6295 static struct attribute
*raid5_attrs
[] = {
6296 &raid5_stripecache_size
.attr
,
6297 &raid5_stripecache_active
.attr
,
6298 &raid5_preread_bypass_threshold
.attr
,
6299 &raid5_group_thread_cnt
.attr
,
6300 &raid5_skip_copy
.attr
,
6301 &raid5_rmw_level
.attr
,
6304 static struct attribute_group raid5_attrs_group
= {
6306 .attrs
= raid5_attrs
,
6309 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
6311 int *worker_cnt_per_group
,
6312 struct r5worker_group
**worker_groups
)
6316 struct r5worker
*workers
;
6318 *worker_cnt_per_group
= cnt
;
6321 *worker_groups
= NULL
;
6324 *group_cnt
= num_possible_nodes();
6325 size
= sizeof(struct r5worker
) * cnt
;
6326 workers
= kzalloc(size
* *group_cnt
, GFP_NOIO
);
6327 *worker_groups
= kzalloc(sizeof(struct r5worker_group
) *
6328 *group_cnt
, GFP_NOIO
);
6329 if (!*worker_groups
|| !workers
) {
6331 kfree(*worker_groups
);
6335 for (i
= 0; i
< *group_cnt
; i
++) {
6336 struct r5worker_group
*group
;
6338 group
= &(*worker_groups
)[i
];
6339 INIT_LIST_HEAD(&group
->handle_list
);
6341 group
->workers
= workers
+ i
* cnt
;
6343 for (j
= 0; j
< cnt
; j
++) {
6344 struct r5worker
*worker
= group
->workers
+ j
;
6345 worker
->group
= group
;
6346 INIT_WORK(&worker
->work
, raid5_do_work
);
6348 for (k
= 0; k
< NR_STRIPE_HASH_LOCKS
; k
++)
6349 INIT_LIST_HEAD(worker
->temp_inactive_list
+ k
);
6356 static void free_thread_groups(struct r5conf
*conf
)
6358 if (conf
->worker_groups
)
6359 kfree(conf
->worker_groups
[0].workers
);
6360 kfree(conf
->worker_groups
);
6361 conf
->worker_groups
= NULL
;
6365 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
6367 struct r5conf
*conf
= mddev
->private;
6370 sectors
= mddev
->dev_sectors
;
6372 /* size is defined by the smallest of previous and new size */
6373 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
6375 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
6376 sectors
&= ~((sector_t
)conf
->prev_chunk_sectors
- 1);
6377 return sectors
* (raid_disks
- conf
->max_degraded
);
6380 static void free_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6382 safe_put_page(percpu
->spare_page
);
6383 if (percpu
->scribble
)
6384 flex_array_free(percpu
->scribble
);
6385 percpu
->spare_page
= NULL
;
6386 percpu
->scribble
= NULL
;
6389 static int alloc_scratch_buffer(struct r5conf
*conf
, struct raid5_percpu
*percpu
)
6391 if (conf
->level
== 6 && !percpu
->spare_page
)
6392 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
6393 if (!percpu
->scribble
)
6394 percpu
->scribble
= scribble_alloc(max(conf
->raid_disks
,
6395 conf
->previous_raid_disks
),
6396 max(conf
->chunk_sectors
,
6397 conf
->prev_chunk_sectors
)
6401 if (!percpu
->scribble
|| (conf
->level
== 6 && !percpu
->spare_page
)) {
6402 free_scratch_buffer(conf
, percpu
);
6409 static int raid456_cpu_dead(unsigned int cpu
, struct hlist_node
*node
)
6411 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6413 free_scratch_buffer(conf
, per_cpu_ptr(conf
->percpu
, cpu
));
6417 static void raid5_free_percpu(struct r5conf
*conf
)
6422 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6423 free_percpu(conf
->percpu
);
6426 static void free_conf(struct r5conf
*conf
)
6429 r5l_exit_log(conf
->log
);
6430 if (conf
->shrinker
.nr_deferred
)
6431 unregister_shrinker(&conf
->shrinker
);
6433 free_thread_groups(conf
);
6434 shrink_stripes(conf
);
6435 raid5_free_percpu(conf
);
6437 kfree(conf
->stripe_hashtbl
);
6441 static int raid456_cpu_up_prepare(unsigned int cpu
, struct hlist_node
*node
)
6443 struct r5conf
*conf
= hlist_entry_safe(node
, struct r5conf
, node
);
6444 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
6446 if (alloc_scratch_buffer(conf
, percpu
)) {
6447 pr_warn("%s: failed memory allocation for cpu%u\n",
6454 static int raid5_alloc_percpu(struct r5conf
*conf
)
6458 conf
->percpu
= alloc_percpu(struct raid5_percpu
);
6462 err
= cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE
, &conf
->node
);
6464 conf
->scribble_disks
= max(conf
->raid_disks
,
6465 conf
->previous_raid_disks
);
6466 conf
->scribble_sectors
= max(conf
->chunk_sectors
,
6467 conf
->prev_chunk_sectors
);
6472 static unsigned long raid5_cache_scan(struct shrinker
*shrink
,
6473 struct shrink_control
*sc
)
6475 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6476 unsigned long ret
= SHRINK_STOP
;
6478 if (mutex_trylock(&conf
->cache_size_mutex
)) {
6480 while (ret
< sc
->nr_to_scan
&&
6481 conf
->max_nr_stripes
> conf
->min_nr_stripes
) {
6482 if (drop_one_stripe(conf
) == 0) {
6488 mutex_unlock(&conf
->cache_size_mutex
);
6493 static unsigned long raid5_cache_count(struct shrinker
*shrink
,
6494 struct shrink_control
*sc
)
6496 struct r5conf
*conf
= container_of(shrink
, struct r5conf
, shrinker
);
6498 if (conf
->max_nr_stripes
< conf
->min_nr_stripes
)
6499 /* unlikely, but not impossible */
6501 return conf
->max_nr_stripes
- conf
->min_nr_stripes
;
6504 static struct r5conf
*setup_conf(struct mddev
*mddev
)
6506 struct r5conf
*conf
;
6507 int raid_disk
, memory
, max_disks
;
6508 struct md_rdev
*rdev
;
6509 struct disk_info
*disk
;
6512 int group_cnt
, worker_cnt_per_group
;
6513 struct r5worker_group
*new_group
;
6515 if (mddev
->new_level
!= 5
6516 && mddev
->new_level
!= 4
6517 && mddev
->new_level
!= 6) {
6518 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6519 mdname(mddev
), mddev
->new_level
);
6520 return ERR_PTR(-EIO
);
6522 if ((mddev
->new_level
== 5
6523 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
6524 (mddev
->new_level
== 6
6525 && !algorithm_valid_raid6(mddev
->new_layout
))) {
6526 pr_warn("md/raid:%s: layout %d not supported\n",
6527 mdname(mddev
), mddev
->new_layout
);
6528 return ERR_PTR(-EIO
);
6530 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
6531 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6532 mdname(mddev
), mddev
->raid_disks
);
6533 return ERR_PTR(-EINVAL
);
6536 if (!mddev
->new_chunk_sectors
||
6537 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
6538 !is_power_of_2(mddev
->new_chunk_sectors
)) {
6539 pr_warn("md/raid:%s: invalid chunk size %d\n",
6540 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
6541 return ERR_PTR(-EINVAL
);
6544 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
6547 /* Don't enable multi-threading by default*/
6548 if (!alloc_thread_groups(conf
, 0, &group_cnt
, &worker_cnt_per_group
,
6550 conf
->group_cnt
= group_cnt
;
6551 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
6552 conf
->worker_groups
= new_group
;
6555 spin_lock_init(&conf
->device_lock
);
6556 seqcount_init(&conf
->gen_lock
);
6557 mutex_init(&conf
->cache_size_mutex
);
6558 init_waitqueue_head(&conf
->wait_for_quiescent
);
6559 init_waitqueue_head(&conf
->wait_for_stripe
);
6560 init_waitqueue_head(&conf
->wait_for_overlap
);
6561 INIT_LIST_HEAD(&conf
->handle_list
);
6562 INIT_LIST_HEAD(&conf
->hold_list
);
6563 INIT_LIST_HEAD(&conf
->delayed_list
);
6564 INIT_LIST_HEAD(&conf
->bitmap_list
);
6565 bio_list_init(&conf
->return_bi
);
6566 init_llist_head(&conf
->released_stripes
);
6567 atomic_set(&conf
->active_stripes
, 0);
6568 atomic_set(&conf
->preread_active_stripes
, 0);
6569 atomic_set(&conf
->active_aligned_reads
, 0);
6570 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
6571 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
6573 conf
->raid_disks
= mddev
->raid_disks
;
6574 if (mddev
->reshape_position
== MaxSector
)
6575 conf
->previous_raid_disks
= mddev
->raid_disks
;
6577 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6578 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
6580 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
6585 conf
->mddev
= mddev
;
6587 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
6590 /* We init hash_locks[0] separately to that it can be used
6591 * as the reference lock in the spin_lock_nest_lock() call
6592 * in lock_all_device_hash_locks_irq in order to convince
6593 * lockdep that we know what we are doing.
6595 spin_lock_init(conf
->hash_locks
);
6596 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6597 spin_lock_init(conf
->hash_locks
+ i
);
6599 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6600 INIT_LIST_HEAD(conf
->inactive_list
+ i
);
6602 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
6603 INIT_LIST_HEAD(conf
->temp_inactive_list
+ i
);
6605 atomic_set(&conf
->r5c_cached_full_stripes
, 0);
6606 INIT_LIST_HEAD(&conf
->r5c_full_stripe_list
);
6607 atomic_set(&conf
->r5c_cached_partial_stripes
, 0);
6608 INIT_LIST_HEAD(&conf
->r5c_partial_stripe_list
);
6610 conf
->level
= mddev
->new_level
;
6611 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
6612 if (raid5_alloc_percpu(conf
) != 0)
6615 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
6617 rdev_for_each(rdev
, mddev
) {
6618 raid_disk
= rdev
->raid_disk
;
6619 if (raid_disk
>= max_disks
6620 || raid_disk
< 0 || test_bit(Journal
, &rdev
->flags
))
6622 disk
= conf
->disks
+ raid_disk
;
6624 if (test_bit(Replacement
, &rdev
->flags
)) {
6625 if (disk
->replacement
)
6627 disk
->replacement
= rdev
;
6634 if (test_bit(In_sync
, &rdev
->flags
)) {
6635 char b
[BDEVNAME_SIZE
];
6636 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6637 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
6638 } else if (rdev
->saved_raid_disk
!= raid_disk
)
6639 /* Cannot rely on bitmap to complete recovery */
6643 conf
->level
= mddev
->new_level
;
6644 if (conf
->level
== 6) {
6645 conf
->max_degraded
= 2;
6646 if (raid6_call
.xor_syndrome
)
6647 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6649 conf
->rmw_level
= PARITY_DISABLE_RMW
;
6651 conf
->max_degraded
= 1;
6652 conf
->rmw_level
= PARITY_ENABLE_RMW
;
6654 conf
->algorithm
= mddev
->new_layout
;
6655 conf
->reshape_progress
= mddev
->reshape_position
;
6656 if (conf
->reshape_progress
!= MaxSector
) {
6657 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
6658 conf
->prev_algo
= mddev
->layout
;
6660 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
6661 conf
->prev_algo
= conf
->algorithm
;
6664 conf
->min_nr_stripes
= NR_STRIPES
;
6665 if (mddev
->reshape_position
!= MaxSector
) {
6666 int stripes
= max_t(int,
6667 ((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4,
6668 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4);
6669 conf
->min_nr_stripes
= max(NR_STRIPES
, stripes
);
6670 if (conf
->min_nr_stripes
!= NR_STRIPES
)
6671 pr_info("md/raid:%s: force stripe size %d for reshape\n",
6672 mdname(mddev
), conf
->min_nr_stripes
);
6674 memory
= conf
->min_nr_stripes
* (sizeof(struct stripe_head
) +
6675 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
6676 atomic_set(&conf
->empty_inactive_list_nr
, NR_STRIPE_HASH_LOCKS
);
6677 if (grow_stripes(conf
, conf
->min_nr_stripes
)) {
6678 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6679 mdname(mddev
), memory
);
6682 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev
), memory
);
6684 * Losing a stripe head costs more than the time to refill it,
6685 * it reduces the queue depth and so can hurt throughput.
6686 * So set it rather large, scaled by number of devices.
6688 conf
->shrinker
.seeks
= DEFAULT_SEEKS
* conf
->raid_disks
* 4;
6689 conf
->shrinker
.scan_objects
= raid5_cache_scan
;
6690 conf
->shrinker
.count_objects
= raid5_cache_count
;
6691 conf
->shrinker
.batch
= 128;
6692 conf
->shrinker
.flags
= 0;
6693 if (register_shrinker(&conf
->shrinker
)) {
6694 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6699 sprintf(pers_name
, "raid%d", mddev
->new_level
);
6700 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
6701 if (!conf
->thread
) {
6702 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6712 return ERR_PTR(-EIO
);
6714 return ERR_PTR(-ENOMEM
);
6717 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
6720 case ALGORITHM_PARITY_0
:
6721 if (raid_disk
< max_degraded
)
6724 case ALGORITHM_PARITY_N
:
6725 if (raid_disk
>= raid_disks
- max_degraded
)
6728 case ALGORITHM_PARITY_0_6
:
6729 if (raid_disk
== 0 ||
6730 raid_disk
== raid_disks
- 1)
6733 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6734 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6735 case ALGORITHM_LEFT_SYMMETRIC_6
:
6736 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6737 if (raid_disk
== raid_disks
- 1)
6743 static int raid5_run(struct mddev
*mddev
)
6745 struct r5conf
*conf
;
6746 int working_disks
= 0;
6747 int dirty_parity_disks
= 0;
6748 struct md_rdev
*rdev
;
6749 struct md_rdev
*journal_dev
= NULL
;
6750 sector_t reshape_offset
= 0;
6752 long long min_offset_diff
= 0;
6755 if (mddev
->recovery_cp
!= MaxSector
)
6756 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6759 rdev_for_each(rdev
, mddev
) {
6762 if (test_bit(Journal
, &rdev
->flags
)) {
6766 if (rdev
->raid_disk
< 0)
6768 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
6770 min_offset_diff
= diff
;
6772 } else if (mddev
->reshape_backwards
&&
6773 diff
< min_offset_diff
)
6774 min_offset_diff
= diff
;
6775 else if (!mddev
->reshape_backwards
&&
6776 diff
> min_offset_diff
)
6777 min_offset_diff
= diff
;
6780 if (mddev
->reshape_position
!= MaxSector
) {
6781 /* Check that we can continue the reshape.
6782 * Difficulties arise if the stripe we would write to
6783 * next is at or after the stripe we would read from next.
6784 * For a reshape that changes the number of devices, this
6785 * is only possible for a very short time, and mdadm makes
6786 * sure that time appears to have past before assembling
6787 * the array. So we fail if that time hasn't passed.
6788 * For a reshape that keeps the number of devices the same
6789 * mdadm must be monitoring the reshape can keeping the
6790 * critical areas read-only and backed up. It will start
6791 * the array in read-only mode, so we check for that.
6793 sector_t here_new
, here_old
;
6795 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
6800 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
6805 if (mddev
->new_level
!= mddev
->level
) {
6806 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
6810 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
6811 /* reshape_position must be on a new-stripe boundary, and one
6812 * further up in new geometry must map after here in old
6814 * If the chunk sizes are different, then as we perform reshape
6815 * in units of the largest of the two, reshape_position needs
6816 * be a multiple of the largest chunk size times new data disks.
6818 here_new
= mddev
->reshape_position
;
6819 chunk_sectors
= max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
);
6820 new_data_disks
= mddev
->raid_disks
- max_degraded
;
6821 if (sector_div(here_new
, chunk_sectors
* new_data_disks
)) {
6822 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
6826 reshape_offset
= here_new
* chunk_sectors
;
6827 /* here_new is the stripe we will write to */
6828 here_old
= mddev
->reshape_position
;
6829 sector_div(here_old
, chunk_sectors
* (old_disks
-max_degraded
));
6830 /* here_old is the first stripe that we might need to read
6832 if (mddev
->delta_disks
== 0) {
6833 /* We cannot be sure it is safe to start an in-place
6834 * reshape. It is only safe if user-space is monitoring
6835 * and taking constant backups.
6836 * mdadm always starts a situation like this in
6837 * readonly mode so it can take control before
6838 * allowing any writes. So just check for that.
6840 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
6841 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
6842 /* not really in-place - so OK */;
6843 else if (mddev
->ro
== 0) {
6844 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
6848 } else if (mddev
->reshape_backwards
6849 ? (here_new
* chunk_sectors
+ min_offset_diff
<=
6850 here_old
* chunk_sectors
)
6851 : (here_new
* chunk_sectors
>=
6852 here_old
* chunk_sectors
+ (-min_offset_diff
))) {
6853 /* Reading from the same stripe as writing to - bad */
6854 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
6858 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev
));
6859 /* OK, we should be able to continue; */
6861 BUG_ON(mddev
->level
!= mddev
->new_level
);
6862 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
6863 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
6864 BUG_ON(mddev
->delta_disks
!= 0);
6867 if (mddev
->private == NULL
)
6868 conf
= setup_conf(mddev
);
6870 conf
= mddev
->private;
6873 return PTR_ERR(conf
);
6875 if (test_bit(MD_HAS_JOURNAL
, &mddev
->flags
)) {
6877 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
6880 set_disk_ro(mddev
->gendisk
, 1);
6881 } else if (mddev
->recovery_cp
== MaxSector
)
6882 set_bit(MD_JOURNAL_CLEAN
, &mddev
->flags
);
6885 conf
->min_offset_diff
= min_offset_diff
;
6886 mddev
->thread
= conf
->thread
;
6887 conf
->thread
= NULL
;
6888 mddev
->private = conf
;
6890 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
6892 rdev
= conf
->disks
[i
].rdev
;
6893 if (!rdev
&& conf
->disks
[i
].replacement
) {
6894 /* The replacement is all we have yet */
6895 rdev
= conf
->disks
[i
].replacement
;
6896 conf
->disks
[i
].replacement
= NULL
;
6897 clear_bit(Replacement
, &rdev
->flags
);
6898 conf
->disks
[i
].rdev
= rdev
;
6902 if (conf
->disks
[i
].replacement
&&
6903 conf
->reshape_progress
!= MaxSector
) {
6904 /* replacements and reshape simply do not mix. */
6905 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
6908 if (test_bit(In_sync
, &rdev
->flags
)) {
6912 /* This disc is not fully in-sync. However if it
6913 * just stored parity (beyond the recovery_offset),
6914 * when we don't need to be concerned about the
6915 * array being dirty.
6916 * When reshape goes 'backwards', we never have
6917 * partially completed devices, so we only need
6918 * to worry about reshape going forwards.
6920 /* Hack because v0.91 doesn't store recovery_offset properly. */
6921 if (mddev
->major_version
== 0 &&
6922 mddev
->minor_version
> 90)
6923 rdev
->recovery_offset
= reshape_offset
;
6925 if (rdev
->recovery_offset
< reshape_offset
) {
6926 /* We need to check old and new layout */
6927 if (!only_parity(rdev
->raid_disk
,
6930 conf
->max_degraded
))
6933 if (!only_parity(rdev
->raid_disk
,
6935 conf
->previous_raid_disks
,
6936 conf
->max_degraded
))
6938 dirty_parity_disks
++;
6942 * 0 for a fully functional array, 1 or 2 for a degraded array.
6944 mddev
->degraded
= calc_degraded(conf
);
6946 if (has_failed(conf
)) {
6947 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
6948 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
6952 /* device size must be a multiple of chunk size */
6953 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
6954 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
6956 if (mddev
->degraded
> dirty_parity_disks
&&
6957 mddev
->recovery_cp
!= MaxSector
) {
6958 if (mddev
->ok_start_degraded
)
6959 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
6962 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
6968 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
6969 mdname(mddev
), conf
->level
,
6970 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
6973 print_raid5_conf(conf
);
6975 if (conf
->reshape_progress
!= MaxSector
) {
6976 conf
->reshape_safe
= conf
->reshape_progress
;
6977 atomic_set(&conf
->reshape_stripes
, 0);
6978 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6979 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6980 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6981 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6982 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
6986 /* Ok, everything is just fine now */
6987 if (mddev
->to_remove
== &raid5_attrs_group
)
6988 mddev
->to_remove
= NULL
;
6989 else if (mddev
->kobj
.sd
&&
6990 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
6991 pr_warn("raid5: failed to create sysfs attributes for %s\n",
6993 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
6997 bool discard_supported
= true;
6998 /* read-ahead size must cover two whole stripes, which
6999 * is 2 * (datadisks) * chunksize where 'n' is the
7000 * number of raid devices
7002 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
7003 int stripe
= data_disks
*
7004 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
7005 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
7006 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
7008 chunk_size
= mddev
->chunk_sectors
<< 9;
7009 blk_queue_io_min(mddev
->queue
, chunk_size
);
7010 blk_queue_io_opt(mddev
->queue
, chunk_size
*
7011 (conf
->raid_disks
- conf
->max_degraded
));
7012 mddev
->queue
->limits
.raid_partial_stripes_expensive
= 1;
7014 * We can only discard a whole stripe. It doesn't make sense to
7015 * discard data disk but write parity disk
7017 stripe
= stripe
* PAGE_SIZE
;
7018 /* Round up to power of 2, as discard handling
7019 * currently assumes that */
7020 while ((stripe
-1) & stripe
)
7021 stripe
= (stripe
| (stripe
-1)) + 1;
7022 mddev
->queue
->limits
.discard_alignment
= stripe
;
7023 mddev
->queue
->limits
.discard_granularity
= stripe
;
7025 * unaligned part of discard request will be ignored, so can't
7026 * guarantee discard_zeroes_data
7028 mddev
->queue
->limits
.discard_zeroes_data
= 0;
7030 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
7032 rdev_for_each(rdev
, mddev
) {
7033 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7034 rdev
->data_offset
<< 9);
7035 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
7036 rdev
->new_data_offset
<< 9);
7038 * discard_zeroes_data is required, otherwise data
7039 * could be lost. Consider a scenario: discard a stripe
7040 * (the stripe could be inconsistent if
7041 * discard_zeroes_data is 0); write one disk of the
7042 * stripe (the stripe could be inconsistent again
7043 * depending on which disks are used to calculate
7044 * parity); the disk is broken; The stripe data of this
7047 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
7048 !bdev_get_queue(rdev
->bdev
)->
7049 limits
.discard_zeroes_data
)
7050 discard_supported
= false;
7051 /* Unfortunately, discard_zeroes_data is not currently
7052 * a guarantee - just a hint. So we only allow DISCARD
7053 * if the sysadmin has confirmed that only safe devices
7054 * are in use by setting a module parameter.
7056 if (!devices_handle_discard_safely
) {
7057 if (discard_supported
) {
7058 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7059 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7061 discard_supported
= false;
7065 if (discard_supported
&&
7066 mddev
->queue
->limits
.max_discard_sectors
>= (stripe
>> 9) &&
7067 mddev
->queue
->limits
.discard_granularity
>= stripe
)
7068 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
7071 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
7074 blk_queue_max_hw_sectors(mddev
->queue
, UINT_MAX
);
7078 char b
[BDEVNAME_SIZE
];
7080 pr_debug("md/raid:%s: using device %s as journal\n",
7081 mdname(mddev
), bdevname(journal_dev
->bdev
, b
));
7082 r5l_init_log(conf
, journal_dev
);
7087 md_unregister_thread(&mddev
->thread
);
7088 print_raid5_conf(conf
);
7090 mddev
->private = NULL
;
7091 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev
));
7095 static void raid5_free(struct mddev
*mddev
, void *priv
)
7097 struct r5conf
*conf
= priv
;
7100 mddev
->to_remove
= &raid5_attrs_group
;
7103 static void raid5_status(struct seq_file
*seq
, struct mddev
*mddev
)
7105 struct r5conf
*conf
= mddev
->private;
7108 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
7109 conf
->chunk_sectors
/ 2, mddev
->layout
);
7110 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
7112 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7113 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
7114 seq_printf (seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
7117 seq_printf (seq
, "]");
7120 static void print_raid5_conf (struct r5conf
*conf
)
7123 struct disk_info
*tmp
;
7125 pr_debug("RAID conf printout:\n");
7127 pr_debug("(conf==NULL)\n");
7130 pr_debug(" --- level:%d rd:%d wd:%d\n", conf
->level
,
7132 conf
->raid_disks
- conf
->mddev
->degraded
);
7134 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7135 char b
[BDEVNAME_SIZE
];
7136 tmp
= conf
->disks
+ i
;
7138 pr_debug(" disk %d, o:%d, dev:%s\n",
7139 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
7140 bdevname(tmp
->rdev
->bdev
, b
));
7144 static int raid5_spare_active(struct mddev
*mddev
)
7147 struct r5conf
*conf
= mddev
->private;
7148 struct disk_info
*tmp
;
7150 unsigned long flags
;
7152 for (i
= 0; i
< conf
->raid_disks
; i
++) {
7153 tmp
= conf
->disks
+ i
;
7154 if (tmp
->replacement
7155 && tmp
->replacement
->recovery_offset
== MaxSector
7156 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
7157 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
7158 /* Replacement has just become active. */
7160 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
7163 /* Replaced device not technically faulty,
7164 * but we need to be sure it gets removed
7165 * and never re-added.
7167 set_bit(Faulty
, &tmp
->rdev
->flags
);
7168 sysfs_notify_dirent_safe(
7169 tmp
->rdev
->sysfs_state
);
7171 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
7172 } else if (tmp
->rdev
7173 && tmp
->rdev
->recovery_offset
== MaxSector
7174 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
7175 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
7177 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
7180 spin_lock_irqsave(&conf
->device_lock
, flags
);
7181 mddev
->degraded
= calc_degraded(conf
);
7182 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7183 print_raid5_conf(conf
);
7187 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7189 struct r5conf
*conf
= mddev
->private;
7191 int number
= rdev
->raid_disk
;
7192 struct md_rdev
**rdevp
;
7193 struct disk_info
*p
= conf
->disks
+ number
;
7195 print_raid5_conf(conf
);
7196 if (test_bit(Journal
, &rdev
->flags
) && conf
->log
) {
7197 struct r5l_log
*log
;
7199 * we can't wait pending write here, as this is called in
7200 * raid5d, wait will deadlock.
7202 if (atomic_read(&mddev
->writes_pending
))
7210 if (rdev
== p
->rdev
)
7212 else if (rdev
== p
->replacement
)
7213 rdevp
= &p
->replacement
;
7217 if (number
>= conf
->raid_disks
&&
7218 conf
->reshape_progress
== MaxSector
)
7219 clear_bit(In_sync
, &rdev
->flags
);
7221 if (test_bit(In_sync
, &rdev
->flags
) ||
7222 atomic_read(&rdev
->nr_pending
)) {
7226 /* Only remove non-faulty devices if recovery
7229 if (!test_bit(Faulty
, &rdev
->flags
) &&
7230 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
7231 !has_failed(conf
) &&
7232 (!p
->replacement
|| p
->replacement
== rdev
) &&
7233 number
< conf
->raid_disks
) {
7238 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
7240 if (atomic_read(&rdev
->nr_pending
)) {
7241 /* lost the race, try later */
7246 if (p
->replacement
) {
7247 /* We must have just cleared 'rdev' */
7248 p
->rdev
= p
->replacement
;
7249 clear_bit(Replacement
, &p
->replacement
->flags
);
7250 smp_mb(); /* Make sure other CPUs may see both as identical
7251 * but will never see neither - if they are careful
7253 p
->replacement
= NULL
;
7254 clear_bit(WantReplacement
, &rdev
->flags
);
7256 /* We might have just removed the Replacement as faulty-
7257 * clear the bit just in case
7259 clear_bit(WantReplacement
, &rdev
->flags
);
7262 print_raid5_conf(conf
);
7266 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
7268 struct r5conf
*conf
= mddev
->private;
7271 struct disk_info
*p
;
7273 int last
= conf
->raid_disks
- 1;
7275 if (test_bit(Journal
, &rdev
->flags
)) {
7276 char b
[BDEVNAME_SIZE
];
7280 rdev
->raid_disk
= 0;
7282 * The array is in readonly mode if journal is missing, so no
7283 * write requests running. We should be safe
7285 r5l_init_log(conf
, rdev
);
7286 pr_debug("md/raid:%s: using device %s as journal\n",
7287 mdname(mddev
), bdevname(rdev
->bdev
, b
));
7290 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
7293 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
7294 /* no point adding a device */
7297 if (rdev
->raid_disk
>= 0)
7298 first
= last
= rdev
->raid_disk
;
7301 * find the disk ... but prefer rdev->saved_raid_disk
7304 if (rdev
->saved_raid_disk
>= 0 &&
7305 rdev
->saved_raid_disk
>= first
&&
7306 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
7307 first
= rdev
->saved_raid_disk
;
7309 for (disk
= first
; disk
<= last
; disk
++) {
7310 p
= conf
->disks
+ disk
;
7311 if (p
->rdev
== NULL
) {
7312 clear_bit(In_sync
, &rdev
->flags
);
7313 rdev
->raid_disk
= disk
;
7315 if (rdev
->saved_raid_disk
!= disk
)
7317 rcu_assign_pointer(p
->rdev
, rdev
);
7321 for (disk
= first
; disk
<= last
; disk
++) {
7322 p
= conf
->disks
+ disk
;
7323 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
7324 p
->replacement
== NULL
) {
7325 clear_bit(In_sync
, &rdev
->flags
);
7326 set_bit(Replacement
, &rdev
->flags
);
7327 rdev
->raid_disk
= disk
;
7330 rcu_assign_pointer(p
->replacement
, rdev
);
7335 print_raid5_conf(conf
);
7339 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
7341 /* no resync is happening, and there is enough space
7342 * on all devices, so we can resize.
7343 * We need to make sure resync covers any new space.
7344 * If the array is shrinking we should possibly wait until
7345 * any io in the removed space completes, but it hardly seems
7349 struct r5conf
*conf
= mddev
->private;
7353 sectors
&= ~((sector_t
)conf
->chunk_sectors
- 1);
7354 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
7355 if (mddev
->external_size
&&
7356 mddev
->array_sectors
> newsize
)
7358 if (mddev
->bitmap
) {
7359 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
7363 md_set_array_sectors(mddev
, newsize
);
7364 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7365 revalidate_disk(mddev
->gendisk
);
7366 if (sectors
> mddev
->dev_sectors
&&
7367 mddev
->recovery_cp
> mddev
->dev_sectors
) {
7368 mddev
->recovery_cp
= mddev
->dev_sectors
;
7369 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
7371 mddev
->dev_sectors
= sectors
;
7372 mddev
->resync_max_sectors
= sectors
;
7376 static int check_stripe_cache(struct mddev
*mddev
)
7378 /* Can only proceed if there are plenty of stripe_heads.
7379 * We need a minimum of one full stripe,, and for sensible progress
7380 * it is best to have about 4 times that.
7381 * If we require 4 times, then the default 256 4K stripe_heads will
7382 * allow for chunk sizes up to 256K, which is probably OK.
7383 * If the chunk size is greater, user-space should request more
7384 * stripe_heads first.
7386 struct r5conf
*conf
= mddev
->private;
7387 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7388 > conf
->min_nr_stripes
||
7389 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
7390 > conf
->min_nr_stripes
) {
7391 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7393 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
7400 static int check_reshape(struct mddev
*mddev
)
7402 struct r5conf
*conf
= mddev
->private;
7406 if (mddev
->delta_disks
== 0 &&
7407 mddev
->new_layout
== mddev
->layout
&&
7408 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
7409 return 0; /* nothing to do */
7410 if (has_failed(conf
))
7412 if (mddev
->delta_disks
< 0 && mddev
->reshape_position
== MaxSector
) {
7413 /* We might be able to shrink, but the devices must
7414 * be made bigger first.
7415 * For raid6, 4 is the minimum size.
7416 * Otherwise 2 is the minimum
7419 if (mddev
->level
== 6)
7421 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
7425 if (!check_stripe_cache(mddev
))
7428 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
||
7429 mddev
->delta_disks
> 0)
7430 if (resize_chunks(conf
,
7431 conf
->previous_raid_disks
7432 + max(0, mddev
->delta_disks
),
7433 max(mddev
->new_chunk_sectors
,
7434 mddev
->chunk_sectors
)
7437 return resize_stripes(conf
, (conf
->previous_raid_disks
7438 + mddev
->delta_disks
));
7441 static int raid5_start_reshape(struct mddev
*mddev
)
7443 struct r5conf
*conf
= mddev
->private;
7444 struct md_rdev
*rdev
;
7446 unsigned long flags
;
7448 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
7451 if (!check_stripe_cache(mddev
))
7454 if (has_failed(conf
))
7457 rdev_for_each(rdev
, mddev
) {
7458 if (!test_bit(In_sync
, &rdev
->flags
)
7459 && !test_bit(Faulty
, &rdev
->flags
))
7463 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
7464 /* Not enough devices even to make a degraded array
7469 /* Refuse to reduce size of the array. Any reductions in
7470 * array size must be through explicit setting of array_size
7473 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
7474 < mddev
->array_sectors
) {
7475 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7480 atomic_set(&conf
->reshape_stripes
, 0);
7481 spin_lock_irq(&conf
->device_lock
);
7482 write_seqcount_begin(&conf
->gen_lock
);
7483 conf
->previous_raid_disks
= conf
->raid_disks
;
7484 conf
->raid_disks
+= mddev
->delta_disks
;
7485 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
7486 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
7487 conf
->prev_algo
= conf
->algorithm
;
7488 conf
->algorithm
= mddev
->new_layout
;
7490 /* Code that selects data_offset needs to see the generation update
7491 * if reshape_progress has been set - so a memory barrier needed.
7494 if (mddev
->reshape_backwards
)
7495 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
7497 conf
->reshape_progress
= 0;
7498 conf
->reshape_safe
= conf
->reshape_progress
;
7499 write_seqcount_end(&conf
->gen_lock
);
7500 spin_unlock_irq(&conf
->device_lock
);
7502 /* Now make sure any requests that proceeded on the assumption
7503 * the reshape wasn't running - like Discard or Read - have
7506 mddev_suspend(mddev
);
7507 mddev_resume(mddev
);
7509 /* Add some new drives, as many as will fit.
7510 * We know there are enough to make the newly sized array work.
7511 * Don't add devices if we are reducing the number of
7512 * devices in the array. This is because it is not possible
7513 * to correctly record the "partially reconstructed" state of
7514 * such devices during the reshape and confusion could result.
7516 if (mddev
->delta_disks
>= 0) {
7517 rdev_for_each(rdev
, mddev
)
7518 if (rdev
->raid_disk
< 0 &&
7519 !test_bit(Faulty
, &rdev
->flags
)) {
7520 if (raid5_add_disk(mddev
, rdev
) == 0) {
7522 >= conf
->previous_raid_disks
)
7523 set_bit(In_sync
, &rdev
->flags
);
7525 rdev
->recovery_offset
= 0;
7527 if (sysfs_link_rdev(mddev
, rdev
))
7528 /* Failure here is OK */;
7530 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
7531 && !test_bit(Faulty
, &rdev
->flags
)) {
7532 /* This is a spare that was manually added */
7533 set_bit(In_sync
, &rdev
->flags
);
7536 /* When a reshape changes the number of devices,
7537 * ->degraded is measured against the larger of the
7538 * pre and post number of devices.
7540 spin_lock_irqsave(&conf
->device_lock
, flags
);
7541 mddev
->degraded
= calc_degraded(conf
);
7542 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
7544 mddev
->raid_disks
= conf
->raid_disks
;
7545 mddev
->reshape_position
= conf
->reshape_progress
;
7546 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7548 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
7549 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
7550 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
7551 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
7552 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
7553 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
7555 if (!mddev
->sync_thread
) {
7556 mddev
->recovery
= 0;
7557 spin_lock_irq(&conf
->device_lock
);
7558 write_seqcount_begin(&conf
->gen_lock
);
7559 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
7560 mddev
->new_chunk_sectors
=
7561 conf
->chunk_sectors
= conf
->prev_chunk_sectors
;
7562 mddev
->new_layout
= conf
->algorithm
= conf
->prev_algo
;
7563 rdev_for_each(rdev
, mddev
)
7564 rdev
->new_data_offset
= rdev
->data_offset
;
7566 conf
->generation
--;
7567 conf
->reshape_progress
= MaxSector
;
7568 mddev
->reshape_position
= MaxSector
;
7569 write_seqcount_end(&conf
->gen_lock
);
7570 spin_unlock_irq(&conf
->device_lock
);
7573 conf
->reshape_checkpoint
= jiffies
;
7574 md_wakeup_thread(mddev
->sync_thread
);
7575 md_new_event(mddev
);
7579 /* This is called from the reshape thread and should make any
7580 * changes needed in 'conf'
7582 static void end_reshape(struct r5conf
*conf
)
7585 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
7586 struct md_rdev
*rdev
;
7588 spin_lock_irq(&conf
->device_lock
);
7589 conf
->previous_raid_disks
= conf
->raid_disks
;
7590 rdev_for_each(rdev
, conf
->mddev
)
7591 rdev
->data_offset
= rdev
->new_data_offset
;
7593 conf
->reshape_progress
= MaxSector
;
7594 conf
->mddev
->reshape_position
= MaxSector
;
7595 spin_unlock_irq(&conf
->device_lock
);
7596 wake_up(&conf
->wait_for_overlap
);
7598 /* read-ahead size must cover two whole stripes, which is
7599 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7601 if (conf
->mddev
->queue
) {
7602 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
7603 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
7605 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
7606 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
7611 /* This is called from the raid5d thread with mddev_lock held.
7612 * It makes config changes to the device.
7614 static void raid5_finish_reshape(struct mddev
*mddev
)
7616 struct r5conf
*conf
= mddev
->private;
7618 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
7620 if (mddev
->delta_disks
> 0) {
7621 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
7623 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
7624 revalidate_disk(mddev
->gendisk
);
7628 spin_lock_irq(&conf
->device_lock
);
7629 mddev
->degraded
= calc_degraded(conf
);
7630 spin_unlock_irq(&conf
->device_lock
);
7631 for (d
= conf
->raid_disks
;
7632 d
< conf
->raid_disks
- mddev
->delta_disks
;
7634 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
7636 clear_bit(In_sync
, &rdev
->flags
);
7637 rdev
= conf
->disks
[d
].replacement
;
7639 clear_bit(In_sync
, &rdev
->flags
);
7642 mddev
->layout
= conf
->algorithm
;
7643 mddev
->chunk_sectors
= conf
->chunk_sectors
;
7644 mddev
->reshape_position
= MaxSector
;
7645 mddev
->delta_disks
= 0;
7646 mddev
->reshape_backwards
= 0;
7650 static void raid5_quiesce(struct mddev
*mddev
, int state
)
7652 struct r5conf
*conf
= mddev
->private;
7655 case 2: /* resume for a suspend */
7656 wake_up(&conf
->wait_for_overlap
);
7659 case 1: /* stop all writes */
7660 lock_all_device_hash_locks_irq(conf
);
7661 /* '2' tells resync/reshape to pause so that all
7662 * active stripes can drain
7665 wait_event_cmd(conf
->wait_for_quiescent
,
7666 atomic_read(&conf
->active_stripes
) == 0 &&
7667 atomic_read(&conf
->active_aligned_reads
) == 0,
7668 unlock_all_device_hash_locks_irq(conf
),
7669 lock_all_device_hash_locks_irq(conf
));
7671 unlock_all_device_hash_locks_irq(conf
);
7672 /* allow reshape to continue */
7673 wake_up(&conf
->wait_for_overlap
);
7676 case 0: /* re-enable writes */
7677 lock_all_device_hash_locks_irq(conf
);
7679 wake_up(&conf
->wait_for_quiescent
);
7680 wake_up(&conf
->wait_for_overlap
);
7681 unlock_all_device_hash_locks_irq(conf
);
7684 r5l_quiesce(conf
->log
, state
);
7687 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
7689 struct r0conf
*raid0_conf
= mddev
->private;
7692 /* for raid0 takeover only one zone is supported */
7693 if (raid0_conf
->nr_strip_zones
> 1) {
7694 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7696 return ERR_PTR(-EINVAL
);
7699 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
7700 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
7701 mddev
->dev_sectors
= sectors
;
7702 mddev
->new_level
= level
;
7703 mddev
->new_layout
= ALGORITHM_PARITY_N
;
7704 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
7705 mddev
->raid_disks
+= 1;
7706 mddev
->delta_disks
= 1;
7707 /* make sure it will be not marked as dirty */
7708 mddev
->recovery_cp
= MaxSector
;
7710 return setup_conf(mddev
);
7713 static void *raid5_takeover_raid1(struct mddev
*mddev
)
7717 if (mddev
->raid_disks
!= 2 ||
7718 mddev
->degraded
> 1)
7719 return ERR_PTR(-EINVAL
);
7721 /* Should check if there are write-behind devices? */
7723 chunksect
= 64*2; /* 64K by default */
7725 /* The array must be an exact multiple of chunksize */
7726 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
7729 if ((chunksect
<<9) < STRIPE_SIZE
)
7730 /* array size does not allow a suitable chunk size */
7731 return ERR_PTR(-EINVAL
);
7733 mddev
->new_level
= 5;
7734 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7735 mddev
->new_chunk_sectors
= chunksect
;
7737 return setup_conf(mddev
);
7740 static void *raid5_takeover_raid6(struct mddev
*mddev
)
7744 switch (mddev
->layout
) {
7745 case ALGORITHM_LEFT_ASYMMETRIC_6
:
7746 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
7748 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
7749 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
7751 case ALGORITHM_LEFT_SYMMETRIC_6
:
7752 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
7754 case ALGORITHM_RIGHT_SYMMETRIC_6
:
7755 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
7757 case ALGORITHM_PARITY_0_6
:
7758 new_layout
= ALGORITHM_PARITY_0
;
7760 case ALGORITHM_PARITY_N
:
7761 new_layout
= ALGORITHM_PARITY_N
;
7764 return ERR_PTR(-EINVAL
);
7766 mddev
->new_level
= 5;
7767 mddev
->new_layout
= new_layout
;
7768 mddev
->delta_disks
= -1;
7769 mddev
->raid_disks
-= 1;
7770 return setup_conf(mddev
);
7773 static int raid5_check_reshape(struct mddev
*mddev
)
7775 /* For a 2-drive array, the layout and chunk size can be changed
7776 * immediately as not restriping is needed.
7777 * For larger arrays we record the new value - after validation
7778 * to be used by a reshape pass.
7780 struct r5conf
*conf
= mddev
->private;
7781 int new_chunk
= mddev
->new_chunk_sectors
;
7783 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
7785 if (new_chunk
> 0) {
7786 if (!is_power_of_2(new_chunk
))
7788 if (new_chunk
< (PAGE_SIZE
>>9))
7790 if (mddev
->array_sectors
& (new_chunk
-1))
7791 /* not factor of array size */
7795 /* They look valid */
7797 if (mddev
->raid_disks
== 2) {
7798 /* can make the change immediately */
7799 if (mddev
->new_layout
>= 0) {
7800 conf
->algorithm
= mddev
->new_layout
;
7801 mddev
->layout
= mddev
->new_layout
;
7803 if (new_chunk
> 0) {
7804 conf
->chunk_sectors
= new_chunk
;
7805 mddev
->chunk_sectors
= new_chunk
;
7807 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
7808 md_wakeup_thread(mddev
->thread
);
7810 return check_reshape(mddev
);
7813 static int raid6_check_reshape(struct mddev
*mddev
)
7815 int new_chunk
= mddev
->new_chunk_sectors
;
7817 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
7819 if (new_chunk
> 0) {
7820 if (!is_power_of_2(new_chunk
))
7822 if (new_chunk
< (PAGE_SIZE
>> 9))
7824 if (mddev
->array_sectors
& (new_chunk
-1))
7825 /* not factor of array size */
7829 /* They look valid */
7830 return check_reshape(mddev
);
7833 static void *raid5_takeover(struct mddev
*mddev
)
7835 /* raid5 can take over:
7836 * raid0 - if there is only one strip zone - make it a raid4 layout
7837 * raid1 - if there are two drives. We need to know the chunk size
7838 * raid4 - trivial - just use a raid4 layout.
7839 * raid6 - Providing it is a *_6 layout
7841 if (mddev
->level
== 0)
7842 return raid45_takeover_raid0(mddev
, 5);
7843 if (mddev
->level
== 1)
7844 return raid5_takeover_raid1(mddev
);
7845 if (mddev
->level
== 4) {
7846 mddev
->new_layout
= ALGORITHM_PARITY_N
;
7847 mddev
->new_level
= 5;
7848 return setup_conf(mddev
);
7850 if (mddev
->level
== 6)
7851 return raid5_takeover_raid6(mddev
);
7853 return ERR_PTR(-EINVAL
);
7856 static void *raid4_takeover(struct mddev
*mddev
)
7858 /* raid4 can take over:
7859 * raid0 - if there is only one strip zone
7860 * raid5 - if layout is right
7862 if (mddev
->level
== 0)
7863 return raid45_takeover_raid0(mddev
, 4);
7864 if (mddev
->level
== 5 &&
7865 mddev
->layout
== ALGORITHM_PARITY_N
) {
7866 mddev
->new_layout
= 0;
7867 mddev
->new_level
= 4;
7868 return setup_conf(mddev
);
7870 return ERR_PTR(-EINVAL
);
7873 static struct md_personality raid5_personality
;
7875 static void *raid6_takeover(struct mddev
*mddev
)
7877 /* Currently can only take over a raid5. We map the
7878 * personality to an equivalent raid6 personality
7879 * with the Q block at the end.
7883 if (mddev
->pers
!= &raid5_personality
)
7884 return ERR_PTR(-EINVAL
);
7885 if (mddev
->degraded
> 1)
7886 return ERR_PTR(-EINVAL
);
7887 if (mddev
->raid_disks
> 253)
7888 return ERR_PTR(-EINVAL
);
7889 if (mddev
->raid_disks
< 3)
7890 return ERR_PTR(-EINVAL
);
7892 switch (mddev
->layout
) {
7893 case ALGORITHM_LEFT_ASYMMETRIC
:
7894 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
7896 case ALGORITHM_RIGHT_ASYMMETRIC
:
7897 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
7899 case ALGORITHM_LEFT_SYMMETRIC
:
7900 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
7902 case ALGORITHM_RIGHT_SYMMETRIC
:
7903 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
7905 case ALGORITHM_PARITY_0
:
7906 new_layout
= ALGORITHM_PARITY_0_6
;
7908 case ALGORITHM_PARITY_N
:
7909 new_layout
= ALGORITHM_PARITY_N
;
7912 return ERR_PTR(-EINVAL
);
7914 mddev
->new_level
= 6;
7915 mddev
->new_layout
= new_layout
;
7916 mddev
->delta_disks
= 1;
7917 mddev
->raid_disks
+= 1;
7918 return setup_conf(mddev
);
7921 static struct md_personality raid6_personality
=
7925 .owner
= THIS_MODULE
,
7926 .make_request
= raid5_make_request
,
7929 .status
= raid5_status
,
7930 .error_handler
= raid5_error
,
7931 .hot_add_disk
= raid5_add_disk
,
7932 .hot_remove_disk
= raid5_remove_disk
,
7933 .spare_active
= raid5_spare_active
,
7934 .sync_request
= raid5_sync_request
,
7935 .resize
= raid5_resize
,
7937 .check_reshape
= raid6_check_reshape
,
7938 .start_reshape
= raid5_start_reshape
,
7939 .finish_reshape
= raid5_finish_reshape
,
7940 .quiesce
= raid5_quiesce
,
7941 .takeover
= raid6_takeover
,
7942 .congested
= raid5_congested
,
7944 static struct md_personality raid5_personality
=
7948 .owner
= THIS_MODULE
,
7949 .make_request
= raid5_make_request
,
7952 .status
= raid5_status
,
7953 .error_handler
= raid5_error
,
7954 .hot_add_disk
= raid5_add_disk
,
7955 .hot_remove_disk
= raid5_remove_disk
,
7956 .spare_active
= raid5_spare_active
,
7957 .sync_request
= raid5_sync_request
,
7958 .resize
= raid5_resize
,
7960 .check_reshape
= raid5_check_reshape
,
7961 .start_reshape
= raid5_start_reshape
,
7962 .finish_reshape
= raid5_finish_reshape
,
7963 .quiesce
= raid5_quiesce
,
7964 .takeover
= raid5_takeover
,
7965 .congested
= raid5_congested
,
7968 static struct md_personality raid4_personality
=
7972 .owner
= THIS_MODULE
,
7973 .make_request
= raid5_make_request
,
7976 .status
= raid5_status
,
7977 .error_handler
= raid5_error
,
7978 .hot_add_disk
= raid5_add_disk
,
7979 .hot_remove_disk
= raid5_remove_disk
,
7980 .spare_active
= raid5_spare_active
,
7981 .sync_request
= raid5_sync_request
,
7982 .resize
= raid5_resize
,
7984 .check_reshape
= raid5_check_reshape
,
7985 .start_reshape
= raid5_start_reshape
,
7986 .finish_reshape
= raid5_finish_reshape
,
7987 .quiesce
= raid5_quiesce
,
7988 .takeover
= raid4_takeover
,
7989 .congested
= raid5_congested
,
7992 static int __init
raid5_init(void)
7996 raid5_wq
= alloc_workqueue("raid5wq",
7997 WQ_UNBOUND
|WQ_MEM_RECLAIM
|WQ_CPU_INTENSIVE
|WQ_SYSFS
, 0);
8001 ret
= cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE
,
8003 raid456_cpu_up_prepare
,
8006 destroy_workqueue(raid5_wq
);
8009 register_md_personality(&raid6_personality
);
8010 register_md_personality(&raid5_personality
);
8011 register_md_personality(&raid4_personality
);
8015 static void raid5_exit(void)
8017 unregister_md_personality(&raid6_personality
);
8018 unregister_md_personality(&raid5_personality
);
8019 unregister_md_personality(&raid4_personality
);
8020 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE
);
8021 destroy_workqueue(raid5_wq
);
8024 module_init(raid5_init
);
8025 module_exit(raid5_exit
);
8026 MODULE_LICENSE("GPL");
8027 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
8028 MODULE_ALIAS("md-personality-4"); /* RAID5 */
8029 MODULE_ALIAS("md-raid5");
8030 MODULE_ALIAS("md-raid4");
8031 MODULE_ALIAS("md-level-5");
8032 MODULE_ALIAS("md-level-4");
8033 MODULE_ALIAS("md-personality-8"); /* RAID6 */
8034 MODULE_ALIAS("md-raid6");
8035 MODULE_ALIAS("md-level-6");
8037 /* This used to be two separate modules, they were: */
8038 MODULE_ALIAS("raid5");
8039 MODULE_ALIAS("raid6");