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>
65 #define NR_STRIPES 256
66 #define STRIPE_SIZE PAGE_SIZE
67 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
68 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
69 #define IO_THRESHOLD 1
70 #define BYPASS_THRESHOLD 1
71 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
72 #define HASH_MASK (NR_HASH - 1)
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 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
86 * This function is used to determine the 'next' bio in the list, given the sector
87 * of the current stripe+device
89 static inline struct bio
*r5_next_bio(struct bio
*bio
, sector_t sector
)
91 int sectors
= bio
->bi_size
>> 9;
92 if (bio
->bi_sector
+ sectors
< sector
+ STRIPE_SECTORS
)
99 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
102 static inline int raid5_bi_phys_segments(struct bio
*bio
)
104 return bio
->bi_phys_segments
& 0xffff;
107 static inline int raid5_bi_hw_segments(struct bio
*bio
)
109 return (bio
->bi_phys_segments
>> 16) & 0xffff;
112 static inline int raid5_dec_bi_phys_segments(struct bio
*bio
)
114 --bio
->bi_phys_segments
;
115 return raid5_bi_phys_segments(bio
);
118 static inline int raid5_dec_bi_hw_segments(struct bio
*bio
)
120 unsigned short val
= raid5_bi_hw_segments(bio
);
123 bio
->bi_phys_segments
= (val
<< 16) | raid5_bi_phys_segments(bio
);
127 static inline void raid5_set_bi_hw_segments(struct bio
*bio
, unsigned int cnt
)
129 bio
->bi_phys_segments
= raid5_bi_phys_segments(bio
) | (cnt
<< 16);
132 /* Find first data disk in a raid6 stripe */
133 static inline int raid6_d0(struct stripe_head
*sh
)
136 /* ddf always start from first device */
138 /* md starts just after Q block */
139 if (sh
->qd_idx
== sh
->disks
- 1)
142 return sh
->qd_idx
+ 1;
144 static inline int raid6_next_disk(int disk
, int raid_disks
)
147 return (disk
< raid_disks
) ? disk
: 0;
150 /* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
155 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
156 int *count
, int syndrome_disks
)
162 if (idx
== sh
->pd_idx
)
163 return syndrome_disks
;
164 if (idx
== sh
->qd_idx
)
165 return syndrome_disks
+ 1;
171 static void return_io(struct bio
*return_bi
)
173 struct bio
*bi
= return_bi
;
176 return_bi
= bi
->bi_next
;
184 static void print_raid5_conf (struct r5conf
*conf
);
186 static int stripe_operations_active(struct stripe_head
*sh
)
188 return sh
->check_state
|| sh
->reconstruct_state
||
189 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
190 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
193 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
)
195 if (atomic_dec_and_test(&sh
->count
)) {
196 BUG_ON(!list_empty(&sh
->lru
));
197 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
198 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
199 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
200 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
201 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
202 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
203 sh
->bm_seq
- conf
->seq_write
> 0)
204 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
206 clear_bit(STRIPE_DELAYED
, &sh
->state
);
207 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
208 list_add_tail(&sh
->lru
, &conf
->handle_list
);
210 md_wakeup_thread(conf
->mddev
->thread
);
212 BUG_ON(stripe_operations_active(sh
));
213 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
214 if (atomic_dec_return(&conf
->preread_active_stripes
)
216 md_wakeup_thread(conf
->mddev
->thread
);
217 atomic_dec(&conf
->active_stripes
);
218 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
219 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
220 wake_up(&conf
->wait_for_stripe
);
221 if (conf
->retry_read_aligned
)
222 md_wakeup_thread(conf
->mddev
->thread
);
228 static void release_stripe(struct stripe_head
*sh
)
230 struct r5conf
*conf
= sh
->raid_conf
;
233 spin_lock_irqsave(&conf
->device_lock
, flags
);
234 __release_stripe(conf
, sh
);
235 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
238 static inline void remove_hash(struct stripe_head
*sh
)
240 pr_debug("remove_hash(), stripe %llu\n",
241 (unsigned long long)sh
->sector
);
243 hlist_del_init(&sh
->hash
);
246 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
248 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
250 pr_debug("insert_hash(), stripe %llu\n",
251 (unsigned long long)sh
->sector
);
253 hlist_add_head(&sh
->hash
, hp
);
257 /* find an idle stripe, make sure it is unhashed, and return it. */
258 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
)
260 struct stripe_head
*sh
= NULL
;
261 struct list_head
*first
;
263 if (list_empty(&conf
->inactive_list
))
265 first
= conf
->inactive_list
.next
;
266 sh
= list_entry(first
, struct stripe_head
, lru
);
267 list_del_init(first
);
269 atomic_inc(&conf
->active_stripes
);
274 static void shrink_buffers(struct stripe_head
*sh
)
278 int num
= sh
->raid_conf
->pool_size
;
280 for (i
= 0; i
< num
; i
++) {
284 sh
->dev
[i
].page
= NULL
;
289 static int grow_buffers(struct stripe_head
*sh
)
292 int num
= sh
->raid_conf
->pool_size
;
294 for (i
= 0; i
< num
; i
++) {
297 if (!(page
= alloc_page(GFP_KERNEL
))) {
300 sh
->dev
[i
].page
= page
;
305 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
306 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
307 struct stripe_head
*sh
);
309 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
311 struct r5conf
*conf
= sh
->raid_conf
;
314 BUG_ON(atomic_read(&sh
->count
) != 0);
315 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
316 BUG_ON(stripe_operations_active(sh
));
318 pr_debug("init_stripe called, stripe %llu\n",
319 (unsigned long long)sh
->sector
);
323 sh
->generation
= conf
->generation
- previous
;
324 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
326 stripe_set_idx(sector
, conf
, previous
, sh
);
330 for (i
= sh
->disks
; i
--; ) {
331 struct r5dev
*dev
= &sh
->dev
[i
];
333 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
334 test_bit(R5_LOCKED
, &dev
->flags
)) {
335 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
336 (unsigned long long)sh
->sector
, i
, dev
->toread
,
337 dev
->read
, dev
->towrite
, dev
->written
,
338 test_bit(R5_LOCKED
, &dev
->flags
));
342 raid5_build_block(sh
, i
, previous
);
344 insert_hash(conf
, sh
);
347 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
350 struct stripe_head
*sh
;
351 struct hlist_node
*hn
;
353 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
354 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
355 if (sh
->sector
== sector
&& sh
->generation
== generation
)
357 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
362 * Need to check if array has failed when deciding whether to:
364 * - remove non-faulty devices
367 * This determination is simple when no reshape is happening.
368 * However if there is a reshape, we need to carefully check
369 * both the before and after sections.
370 * This is because some failed devices may only affect one
371 * of the two sections, and some non-in_sync devices may
372 * be insync in the section most affected by failed devices.
374 static int calc_degraded(struct r5conf
*conf
)
376 int degraded
, degraded2
;
381 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
382 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
383 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
384 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
385 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
387 else if (test_bit(In_sync
, &rdev
->flags
))
390 /* not in-sync or faulty.
391 * If the reshape increases the number of devices,
392 * this is being recovered by the reshape, so
393 * this 'previous' section is not in_sync.
394 * If the number of devices is being reduced however,
395 * the device can only be part of the array if
396 * we are reverting a reshape, so this section will
399 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
403 if (conf
->raid_disks
== conf
->previous_raid_disks
)
407 for (i
= 0; i
< conf
->raid_disks
; i
++) {
408 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
409 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
410 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
411 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
413 else if (test_bit(In_sync
, &rdev
->flags
))
416 /* not in-sync or faulty.
417 * If reshape increases the number of devices, this
418 * section has already been recovered, else it
419 * almost certainly hasn't.
421 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
425 if (degraded2
> degraded
)
430 static int has_failed(struct r5conf
*conf
)
434 if (conf
->mddev
->reshape_position
== MaxSector
)
435 return conf
->mddev
->degraded
> conf
->max_degraded
;
437 degraded
= calc_degraded(conf
);
438 if (degraded
> conf
->max_degraded
)
443 static struct stripe_head
*
444 get_active_stripe(struct r5conf
*conf
, sector_t sector
,
445 int previous
, int noblock
, int noquiesce
)
447 struct stripe_head
*sh
;
449 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
451 spin_lock_irq(&conf
->device_lock
);
454 wait_event_lock_irq(conf
->wait_for_stripe
,
455 conf
->quiesce
== 0 || noquiesce
,
456 conf
->device_lock
, /* nothing */);
457 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
459 if (!conf
->inactive_blocked
)
460 sh
= get_free_stripe(conf
);
461 if (noblock
&& sh
== NULL
)
464 conf
->inactive_blocked
= 1;
465 wait_event_lock_irq(conf
->wait_for_stripe
,
466 !list_empty(&conf
->inactive_list
) &&
467 (atomic_read(&conf
->active_stripes
)
468 < (conf
->max_nr_stripes
*3/4)
469 || !conf
->inactive_blocked
),
472 conf
->inactive_blocked
= 0;
474 init_stripe(sh
, sector
, previous
);
476 if (atomic_read(&sh
->count
)) {
477 BUG_ON(!list_empty(&sh
->lru
)
478 && !test_bit(STRIPE_EXPANDING
, &sh
->state
));
480 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
481 atomic_inc(&conf
->active_stripes
);
482 if (list_empty(&sh
->lru
) &&
483 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
485 list_del_init(&sh
->lru
);
488 } while (sh
== NULL
);
491 atomic_inc(&sh
->count
);
493 spin_unlock_irq(&conf
->device_lock
);
498 raid5_end_read_request(struct bio
*bi
, int error
);
500 raid5_end_write_request(struct bio
*bi
, int error
);
502 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
504 struct r5conf
*conf
= sh
->raid_conf
;
505 int i
, disks
= sh
->disks
;
509 for (i
= disks
; i
--; ) {
511 int replace_only
= 0;
512 struct bio
*bi
, *rbi
;
513 struct md_rdev
*rdev
, *rrdev
= NULL
;
514 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
515 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
519 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
521 else if (test_and_clear_bit(R5_WantReplace
,
522 &sh
->dev
[i
].flags
)) {
528 bi
= &sh
->dev
[i
].req
;
529 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
534 bi
->bi_end_io
= raid5_end_write_request
;
535 rbi
->bi_end_io
= raid5_end_write_request
;
537 bi
->bi_end_io
= raid5_end_read_request
;
540 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
541 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
542 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
551 /* We raced and saw duplicates */
554 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
) && rrdev
)
559 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
562 atomic_inc(&rdev
->nr_pending
);
563 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
566 atomic_inc(&rrdev
->nr_pending
);
569 /* We have already checked bad blocks for reads. Now
570 * need to check for writes. We never accept write errors
571 * on the replacement, so we don't to check rrdev.
573 while ((rw
& WRITE
) && rdev
&&
574 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
577 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
578 &first_bad
, &bad_sectors
);
583 set_bit(BlockedBadBlocks
, &rdev
->flags
);
584 if (!conf
->mddev
->external
&&
585 conf
->mddev
->flags
) {
586 /* It is very unlikely, but we might
587 * still need to write out the
588 * bad block log - better give it
590 md_check_recovery(conf
->mddev
);
593 * Because md_wait_for_blocked_rdev
594 * will dec nr_pending, we must
595 * increment it first.
597 atomic_inc(&rdev
->nr_pending
);
598 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
600 /* Acknowledged bad block - skip the write */
601 rdev_dec_pending(rdev
, conf
->mddev
);
607 if (s
->syncing
|| s
->expanding
|| s
->expanded
609 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
611 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
613 bi
->bi_bdev
= rdev
->bdev
;
614 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
615 __func__
, (unsigned long long)sh
->sector
,
617 atomic_inc(&sh
->count
);
618 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
619 bi
->bi_flags
= 1 << BIO_UPTODATE
;
621 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
622 bi
->bi_io_vec
[0].bv_offset
= 0;
623 bi
->bi_size
= STRIPE_SIZE
;
626 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
627 generic_make_request(bi
);
630 if (s
->syncing
|| s
->expanding
|| s
->expanded
632 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
634 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
636 rbi
->bi_bdev
= rrdev
->bdev
;
637 pr_debug("%s: for %llu schedule op %ld on "
638 "replacement disc %d\n",
639 __func__
, (unsigned long long)sh
->sector
,
641 atomic_inc(&sh
->count
);
642 rbi
->bi_sector
= sh
->sector
+ rrdev
->data_offset
;
643 rbi
->bi_flags
= 1 << BIO_UPTODATE
;
645 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
646 rbi
->bi_io_vec
[0].bv_offset
= 0;
647 rbi
->bi_size
= STRIPE_SIZE
;
649 generic_make_request(rbi
);
651 if (!rdev
&& !rrdev
) {
653 set_bit(STRIPE_DEGRADED
, &sh
->state
);
654 pr_debug("skip op %ld on disc %d for sector %llu\n",
655 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
656 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
657 set_bit(STRIPE_HANDLE
, &sh
->state
);
662 static struct dma_async_tx_descriptor
*
663 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
664 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
667 struct page
*bio_page
;
670 struct async_submit_ctl submit
;
671 enum async_tx_flags flags
= 0;
673 if (bio
->bi_sector
>= sector
)
674 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
676 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
679 flags
|= ASYNC_TX_FENCE
;
680 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
682 bio_for_each_segment(bvl
, bio
, i
) {
683 int len
= bvl
->bv_len
;
687 if (page_offset
< 0) {
688 b_offset
= -page_offset
;
689 page_offset
+= b_offset
;
693 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
694 clen
= STRIPE_SIZE
- page_offset
;
699 b_offset
+= bvl
->bv_offset
;
700 bio_page
= bvl
->bv_page
;
702 tx
= async_memcpy(page
, bio_page
, page_offset
,
703 b_offset
, clen
, &submit
);
705 tx
= async_memcpy(bio_page
, page
, b_offset
,
706 page_offset
, clen
, &submit
);
708 /* chain the operations */
709 submit
.depend_tx
= tx
;
711 if (clen
< len
) /* hit end of page */
719 static void ops_complete_biofill(void *stripe_head_ref
)
721 struct stripe_head
*sh
= stripe_head_ref
;
722 struct bio
*return_bi
= NULL
;
723 struct r5conf
*conf
= sh
->raid_conf
;
726 pr_debug("%s: stripe %llu\n", __func__
,
727 (unsigned long long)sh
->sector
);
729 /* clear completed biofills */
730 spin_lock_irq(&conf
->device_lock
);
731 for (i
= sh
->disks
; i
--; ) {
732 struct r5dev
*dev
= &sh
->dev
[i
];
734 /* acknowledge completion of a biofill operation */
735 /* and check if we need to reply to a read request,
736 * new R5_Wantfill requests are held off until
737 * !STRIPE_BIOFILL_RUN
739 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
740 struct bio
*rbi
, *rbi2
;
745 while (rbi
&& rbi
->bi_sector
<
746 dev
->sector
+ STRIPE_SECTORS
) {
747 rbi2
= r5_next_bio(rbi
, dev
->sector
);
748 if (!raid5_dec_bi_phys_segments(rbi
)) {
749 rbi
->bi_next
= return_bi
;
756 spin_unlock_irq(&conf
->device_lock
);
757 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
759 return_io(return_bi
);
761 set_bit(STRIPE_HANDLE
, &sh
->state
);
765 static void ops_run_biofill(struct stripe_head
*sh
)
767 struct dma_async_tx_descriptor
*tx
= NULL
;
768 struct r5conf
*conf
= sh
->raid_conf
;
769 struct async_submit_ctl submit
;
772 pr_debug("%s: stripe %llu\n", __func__
,
773 (unsigned long long)sh
->sector
);
775 for (i
= sh
->disks
; i
--; ) {
776 struct r5dev
*dev
= &sh
->dev
[i
];
777 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
779 spin_lock_irq(&conf
->device_lock
);
780 dev
->read
= rbi
= dev
->toread
;
782 spin_unlock_irq(&conf
->device_lock
);
783 while (rbi
&& rbi
->bi_sector
<
784 dev
->sector
+ STRIPE_SECTORS
) {
785 tx
= async_copy_data(0, rbi
, dev
->page
,
787 rbi
= r5_next_bio(rbi
, dev
->sector
);
792 atomic_inc(&sh
->count
);
793 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
794 async_trigger_callback(&submit
);
797 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
804 tgt
= &sh
->dev
[target
];
805 set_bit(R5_UPTODATE
, &tgt
->flags
);
806 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
807 clear_bit(R5_Wantcompute
, &tgt
->flags
);
810 static void ops_complete_compute(void *stripe_head_ref
)
812 struct stripe_head
*sh
= stripe_head_ref
;
814 pr_debug("%s: stripe %llu\n", __func__
,
815 (unsigned long long)sh
->sector
);
817 /* mark the computed target(s) as uptodate */
818 mark_target_uptodate(sh
, sh
->ops
.target
);
819 mark_target_uptodate(sh
, sh
->ops
.target2
);
821 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
822 if (sh
->check_state
== check_state_compute_run
)
823 sh
->check_state
= check_state_compute_result
;
824 set_bit(STRIPE_HANDLE
, &sh
->state
);
828 /* return a pointer to the address conversion region of the scribble buffer */
829 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
830 struct raid5_percpu
*percpu
)
832 return percpu
->scribble
+ sizeof(struct page
*) * (sh
->disks
+ 2);
835 static struct dma_async_tx_descriptor
*
836 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
838 int disks
= sh
->disks
;
839 struct page
**xor_srcs
= percpu
->scribble
;
840 int target
= sh
->ops
.target
;
841 struct r5dev
*tgt
= &sh
->dev
[target
];
842 struct page
*xor_dest
= tgt
->page
;
844 struct dma_async_tx_descriptor
*tx
;
845 struct async_submit_ctl submit
;
848 pr_debug("%s: stripe %llu block: %d\n",
849 __func__
, (unsigned long long)sh
->sector
, target
);
850 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
852 for (i
= disks
; i
--; )
854 xor_srcs
[count
++] = sh
->dev
[i
].page
;
856 atomic_inc(&sh
->count
);
858 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
859 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
));
860 if (unlikely(count
== 1))
861 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
863 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
868 /* set_syndrome_sources - populate source buffers for gen_syndrome
869 * @srcs - (struct page *) array of size sh->disks
870 * @sh - stripe_head to parse
872 * Populates srcs in proper layout order for the stripe and returns the
873 * 'count' of sources to be used in a call to async_gen_syndrome. The P
874 * destination buffer is recorded in srcs[count] and the Q destination
875 * is recorded in srcs[count+1]].
877 static int set_syndrome_sources(struct page
**srcs
, struct stripe_head
*sh
)
879 int disks
= sh
->disks
;
880 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
881 int d0_idx
= raid6_d0(sh
);
885 for (i
= 0; i
< disks
; i
++)
891 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
893 srcs
[slot
] = sh
->dev
[i
].page
;
894 i
= raid6_next_disk(i
, disks
);
895 } while (i
!= d0_idx
);
897 return syndrome_disks
;
900 static struct dma_async_tx_descriptor
*
901 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
903 int disks
= sh
->disks
;
904 struct page
**blocks
= percpu
->scribble
;
906 int qd_idx
= sh
->qd_idx
;
907 struct dma_async_tx_descriptor
*tx
;
908 struct async_submit_ctl submit
;
914 if (sh
->ops
.target
< 0)
915 target
= sh
->ops
.target2
;
916 else if (sh
->ops
.target2
< 0)
917 target
= sh
->ops
.target
;
919 /* we should only have one valid target */
922 pr_debug("%s: stripe %llu block: %d\n",
923 __func__
, (unsigned long long)sh
->sector
, target
);
925 tgt
= &sh
->dev
[target
];
926 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
929 atomic_inc(&sh
->count
);
931 if (target
== qd_idx
) {
932 count
= set_syndrome_sources(blocks
, sh
);
933 blocks
[count
] = NULL
; /* regenerating p is not necessary */
934 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
935 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
936 ops_complete_compute
, sh
,
937 to_addr_conv(sh
, percpu
));
938 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
940 /* Compute any data- or p-drive using XOR */
942 for (i
= disks
; i
-- ; ) {
943 if (i
== target
|| i
== qd_idx
)
945 blocks
[count
++] = sh
->dev
[i
].page
;
948 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
949 NULL
, ops_complete_compute
, sh
,
950 to_addr_conv(sh
, percpu
));
951 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
957 static struct dma_async_tx_descriptor
*
958 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
960 int i
, count
, disks
= sh
->disks
;
961 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
962 int d0_idx
= raid6_d0(sh
);
963 int faila
= -1, failb
= -1;
964 int target
= sh
->ops
.target
;
965 int target2
= sh
->ops
.target2
;
966 struct r5dev
*tgt
= &sh
->dev
[target
];
967 struct r5dev
*tgt2
= &sh
->dev
[target2
];
968 struct dma_async_tx_descriptor
*tx
;
969 struct page
**blocks
= percpu
->scribble
;
970 struct async_submit_ctl submit
;
972 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
973 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
974 BUG_ON(target
< 0 || target2
< 0);
975 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
976 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
978 /* we need to open-code set_syndrome_sources to handle the
979 * slot number conversion for 'faila' and 'failb'
981 for (i
= 0; i
< disks
; i
++)
986 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
988 blocks
[slot
] = sh
->dev
[i
].page
;
994 i
= raid6_next_disk(i
, disks
);
995 } while (i
!= d0_idx
);
997 BUG_ON(faila
== failb
);
1000 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1001 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1003 atomic_inc(&sh
->count
);
1005 if (failb
== syndrome_disks
+1) {
1006 /* Q disk is one of the missing disks */
1007 if (faila
== syndrome_disks
) {
1008 /* Missing P+Q, just recompute */
1009 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1010 ops_complete_compute
, sh
,
1011 to_addr_conv(sh
, percpu
));
1012 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1013 STRIPE_SIZE
, &submit
);
1017 int qd_idx
= sh
->qd_idx
;
1019 /* Missing D+Q: recompute D from P, then recompute Q */
1020 if (target
== qd_idx
)
1021 data_target
= target2
;
1023 data_target
= target
;
1026 for (i
= disks
; i
-- ; ) {
1027 if (i
== data_target
|| i
== qd_idx
)
1029 blocks
[count
++] = sh
->dev
[i
].page
;
1031 dest
= sh
->dev
[data_target
].page
;
1032 init_async_submit(&submit
,
1033 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1035 to_addr_conv(sh
, percpu
));
1036 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1039 count
= set_syndrome_sources(blocks
, sh
);
1040 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1041 ops_complete_compute
, sh
,
1042 to_addr_conv(sh
, percpu
));
1043 return async_gen_syndrome(blocks
, 0, count
+2,
1044 STRIPE_SIZE
, &submit
);
1047 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1048 ops_complete_compute
, sh
,
1049 to_addr_conv(sh
, percpu
));
1050 if (failb
== syndrome_disks
) {
1051 /* We're missing D+P. */
1052 return async_raid6_datap_recov(syndrome_disks
+2,
1056 /* We're missing D+D. */
1057 return async_raid6_2data_recov(syndrome_disks
+2,
1058 STRIPE_SIZE
, faila
, failb
,
1065 static void ops_complete_prexor(void *stripe_head_ref
)
1067 struct stripe_head
*sh
= stripe_head_ref
;
1069 pr_debug("%s: stripe %llu\n", __func__
,
1070 (unsigned long long)sh
->sector
);
1073 static struct dma_async_tx_descriptor
*
1074 ops_run_prexor(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1075 struct dma_async_tx_descriptor
*tx
)
1077 int disks
= sh
->disks
;
1078 struct page
**xor_srcs
= percpu
->scribble
;
1079 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1080 struct async_submit_ctl submit
;
1082 /* existing parity data subtracted */
1083 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1085 pr_debug("%s: stripe %llu\n", __func__
,
1086 (unsigned long long)sh
->sector
);
1088 for (i
= disks
; i
--; ) {
1089 struct r5dev
*dev
= &sh
->dev
[i
];
1090 /* Only process blocks that are known to be uptodate */
1091 if (test_bit(R5_Wantdrain
, &dev
->flags
))
1092 xor_srcs
[count
++] = dev
->page
;
1095 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1096 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
));
1097 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1102 static struct dma_async_tx_descriptor
*
1103 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1105 int disks
= sh
->disks
;
1108 pr_debug("%s: stripe %llu\n", __func__
,
1109 (unsigned long long)sh
->sector
);
1111 for (i
= disks
; i
--; ) {
1112 struct r5dev
*dev
= &sh
->dev
[i
];
1115 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
1118 spin_lock_irq(&sh
->raid_conf
->device_lock
);
1119 chosen
= dev
->towrite
;
1120 dev
->towrite
= NULL
;
1121 BUG_ON(dev
->written
);
1122 wbi
= dev
->written
= chosen
;
1123 spin_unlock_irq(&sh
->raid_conf
->device_lock
);
1125 while (wbi
&& wbi
->bi_sector
<
1126 dev
->sector
+ STRIPE_SECTORS
) {
1127 if (wbi
->bi_rw
& REQ_FUA
)
1128 set_bit(R5_WantFUA
, &dev
->flags
);
1129 tx
= async_copy_data(1, wbi
, dev
->page
,
1131 wbi
= r5_next_bio(wbi
, dev
->sector
);
1139 static void ops_complete_reconstruct(void *stripe_head_ref
)
1141 struct stripe_head
*sh
= stripe_head_ref
;
1142 int disks
= sh
->disks
;
1143 int pd_idx
= sh
->pd_idx
;
1144 int qd_idx
= sh
->qd_idx
;
1148 pr_debug("%s: stripe %llu\n", __func__
,
1149 (unsigned long long)sh
->sector
);
1151 for (i
= disks
; i
--; )
1152 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1154 for (i
= disks
; i
--; ) {
1155 struct r5dev
*dev
= &sh
->dev
[i
];
1157 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1158 set_bit(R5_UPTODATE
, &dev
->flags
);
1160 set_bit(R5_WantFUA
, &dev
->flags
);
1164 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1165 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1166 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1167 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1169 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1170 sh
->reconstruct_state
= reconstruct_state_result
;
1173 set_bit(STRIPE_HANDLE
, &sh
->state
);
1178 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1179 struct dma_async_tx_descriptor
*tx
)
1181 int disks
= sh
->disks
;
1182 struct page
**xor_srcs
= percpu
->scribble
;
1183 struct async_submit_ctl submit
;
1184 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1185 struct page
*xor_dest
;
1187 unsigned long flags
;
1189 pr_debug("%s: stripe %llu\n", __func__
,
1190 (unsigned long long)sh
->sector
);
1192 /* check if prexor is active which means only process blocks
1193 * that are part of a read-modify-write (written)
1195 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1197 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1198 for (i
= disks
; i
--; ) {
1199 struct r5dev
*dev
= &sh
->dev
[i
];
1201 xor_srcs
[count
++] = dev
->page
;
1204 xor_dest
= sh
->dev
[pd_idx
].page
;
1205 for (i
= disks
; i
--; ) {
1206 struct r5dev
*dev
= &sh
->dev
[i
];
1208 xor_srcs
[count
++] = dev
->page
;
1212 /* 1/ if we prexor'd then the dest is reused as a source
1213 * 2/ if we did not prexor then we are redoing the parity
1214 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1215 * for the synchronous xor case
1217 flags
= ASYNC_TX_ACK
|
1218 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1220 atomic_inc(&sh
->count
);
1222 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, sh
,
1223 to_addr_conv(sh
, percpu
));
1224 if (unlikely(count
== 1))
1225 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1227 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1231 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1232 struct dma_async_tx_descriptor
*tx
)
1234 struct async_submit_ctl submit
;
1235 struct page
**blocks
= percpu
->scribble
;
1238 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1240 count
= set_syndrome_sources(blocks
, sh
);
1242 atomic_inc(&sh
->count
);
1244 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_reconstruct
,
1245 sh
, to_addr_conv(sh
, percpu
));
1246 async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1249 static void ops_complete_check(void *stripe_head_ref
)
1251 struct stripe_head
*sh
= stripe_head_ref
;
1253 pr_debug("%s: stripe %llu\n", __func__
,
1254 (unsigned long long)sh
->sector
);
1256 sh
->check_state
= check_state_check_result
;
1257 set_bit(STRIPE_HANDLE
, &sh
->state
);
1261 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1263 int disks
= sh
->disks
;
1264 int pd_idx
= sh
->pd_idx
;
1265 int qd_idx
= sh
->qd_idx
;
1266 struct page
*xor_dest
;
1267 struct page
**xor_srcs
= percpu
->scribble
;
1268 struct dma_async_tx_descriptor
*tx
;
1269 struct async_submit_ctl submit
;
1273 pr_debug("%s: stripe %llu\n", __func__
,
1274 (unsigned long long)sh
->sector
);
1277 xor_dest
= sh
->dev
[pd_idx
].page
;
1278 xor_srcs
[count
++] = xor_dest
;
1279 for (i
= disks
; i
--; ) {
1280 if (i
== pd_idx
|| i
== qd_idx
)
1282 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1285 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1286 to_addr_conv(sh
, percpu
));
1287 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1288 &sh
->ops
.zero_sum_result
, &submit
);
1290 atomic_inc(&sh
->count
);
1291 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1292 tx
= async_trigger_callback(&submit
);
1295 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1297 struct page
**srcs
= percpu
->scribble
;
1298 struct async_submit_ctl submit
;
1301 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1302 (unsigned long long)sh
->sector
, checkp
);
1304 count
= set_syndrome_sources(srcs
, sh
);
1308 atomic_inc(&sh
->count
);
1309 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1310 sh
, to_addr_conv(sh
, percpu
));
1311 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1312 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1315 static void __raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1317 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1318 struct dma_async_tx_descriptor
*tx
= NULL
;
1319 struct r5conf
*conf
= sh
->raid_conf
;
1320 int level
= conf
->level
;
1321 struct raid5_percpu
*percpu
;
1325 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1326 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1327 ops_run_biofill(sh
);
1331 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1333 tx
= ops_run_compute5(sh
, percpu
);
1335 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1336 tx
= ops_run_compute6_1(sh
, percpu
);
1338 tx
= ops_run_compute6_2(sh
, percpu
);
1340 /* terminate the chain if reconstruct is not set to be run */
1341 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1345 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
1346 tx
= ops_run_prexor(sh
, percpu
, tx
);
1348 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1349 tx
= ops_run_biodrain(sh
, tx
);
1353 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1355 ops_run_reconstruct5(sh
, percpu
, tx
);
1357 ops_run_reconstruct6(sh
, percpu
, tx
);
1360 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1361 if (sh
->check_state
== check_state_run
)
1362 ops_run_check_p(sh
, percpu
);
1363 else if (sh
->check_state
== check_state_run_q
)
1364 ops_run_check_pq(sh
, percpu
, 0);
1365 else if (sh
->check_state
== check_state_run_pq
)
1366 ops_run_check_pq(sh
, percpu
, 1);
1372 for (i
= disks
; i
--; ) {
1373 struct r5dev
*dev
= &sh
->dev
[i
];
1374 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1375 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1380 #ifdef CONFIG_MULTICORE_RAID456
1381 static void async_run_ops(void *param
, async_cookie_t cookie
)
1383 struct stripe_head
*sh
= param
;
1384 unsigned long ops_request
= sh
->ops
.request
;
1386 clear_bit_unlock(STRIPE_OPS_REQ_PENDING
, &sh
->state
);
1387 wake_up(&sh
->ops
.wait_for_ops
);
1389 __raid_run_ops(sh
, ops_request
);
1393 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1395 /* since handle_stripe can be called outside of raid5d context
1396 * we need to ensure sh->ops.request is de-staged before another
1399 wait_event(sh
->ops
.wait_for_ops
,
1400 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING
, &sh
->state
));
1401 sh
->ops
.request
= ops_request
;
1403 atomic_inc(&sh
->count
);
1404 async_schedule(async_run_ops
, sh
);
1407 #define raid_run_ops __raid_run_ops
1410 static int grow_one_stripe(struct r5conf
*conf
)
1412 struct stripe_head
*sh
;
1413 sh
= kmem_cache_zalloc(conf
->slab_cache
, GFP_KERNEL
);
1417 sh
->raid_conf
= conf
;
1418 #ifdef CONFIG_MULTICORE_RAID456
1419 init_waitqueue_head(&sh
->ops
.wait_for_ops
);
1422 if (grow_buffers(sh
)) {
1424 kmem_cache_free(conf
->slab_cache
, sh
);
1427 /* we just created an active stripe so... */
1428 atomic_set(&sh
->count
, 1);
1429 atomic_inc(&conf
->active_stripes
);
1430 INIT_LIST_HEAD(&sh
->lru
);
1435 static int grow_stripes(struct r5conf
*conf
, int num
)
1437 struct kmem_cache
*sc
;
1438 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
1440 if (conf
->mddev
->gendisk
)
1441 sprintf(conf
->cache_name
[0],
1442 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
1444 sprintf(conf
->cache_name
[0],
1445 "raid%d-%p", conf
->level
, conf
->mddev
);
1446 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
1448 conf
->active_name
= 0;
1449 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
1450 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
1454 conf
->slab_cache
= sc
;
1455 conf
->pool_size
= devs
;
1457 if (!grow_one_stripe(conf
))
1463 * scribble_len - return the required size of the scribble region
1464 * @num - total number of disks in the array
1466 * The size must be enough to contain:
1467 * 1/ a struct page pointer for each device in the array +2
1468 * 2/ room to convert each entry in (1) to its corresponding dma
1469 * (dma_map_page()) or page (page_address()) address.
1471 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1472 * calculate over all devices (not just the data blocks), using zeros in place
1473 * of the P and Q blocks.
1475 static size_t scribble_len(int num
)
1479 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
1484 static int resize_stripes(struct r5conf
*conf
, int newsize
)
1486 /* Make all the stripes able to hold 'newsize' devices.
1487 * New slots in each stripe get 'page' set to a new page.
1489 * This happens in stages:
1490 * 1/ create a new kmem_cache and allocate the required number of
1492 * 2/ gather all the old stripe_heads and tranfer the pages across
1493 * to the new stripe_heads. This will have the side effect of
1494 * freezing the array as once all stripe_heads have been collected,
1495 * no IO will be possible. Old stripe heads are freed once their
1496 * pages have been transferred over, and the old kmem_cache is
1497 * freed when all stripes are done.
1498 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1499 * we simple return a failre status - no need to clean anything up.
1500 * 4/ allocate new pages for the new slots in the new stripe_heads.
1501 * If this fails, we don't bother trying the shrink the
1502 * stripe_heads down again, we just leave them as they are.
1503 * As each stripe_head is processed the new one is released into
1506 * Once step2 is started, we cannot afford to wait for a write,
1507 * so we use GFP_NOIO allocations.
1509 struct stripe_head
*osh
, *nsh
;
1510 LIST_HEAD(newstripes
);
1511 struct disk_info
*ndisks
;
1514 struct kmem_cache
*sc
;
1517 if (newsize
<= conf
->pool_size
)
1518 return 0; /* never bother to shrink */
1520 err
= md_allow_write(conf
->mddev
);
1525 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
1526 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
1531 for (i
= conf
->max_nr_stripes
; i
; i
--) {
1532 nsh
= kmem_cache_zalloc(sc
, GFP_KERNEL
);
1536 nsh
->raid_conf
= conf
;
1537 #ifdef CONFIG_MULTICORE_RAID456
1538 init_waitqueue_head(&nsh
->ops
.wait_for_ops
);
1541 list_add(&nsh
->lru
, &newstripes
);
1544 /* didn't get enough, give up */
1545 while (!list_empty(&newstripes
)) {
1546 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1547 list_del(&nsh
->lru
);
1548 kmem_cache_free(sc
, nsh
);
1550 kmem_cache_destroy(sc
);
1553 /* Step 2 - Must use GFP_NOIO now.
1554 * OK, we have enough stripes, start collecting inactive
1555 * stripes and copying them over
1557 list_for_each_entry(nsh
, &newstripes
, lru
) {
1558 spin_lock_irq(&conf
->device_lock
);
1559 wait_event_lock_irq(conf
->wait_for_stripe
,
1560 !list_empty(&conf
->inactive_list
),
1563 osh
= get_free_stripe(conf
);
1564 spin_unlock_irq(&conf
->device_lock
);
1565 atomic_set(&nsh
->count
, 1);
1566 for(i
=0; i
<conf
->pool_size
; i
++)
1567 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1568 for( ; i
<newsize
; i
++)
1569 nsh
->dev
[i
].page
= NULL
;
1570 kmem_cache_free(conf
->slab_cache
, osh
);
1572 kmem_cache_destroy(conf
->slab_cache
);
1575 * At this point, we are holding all the stripes so the array
1576 * is completely stalled, so now is a good time to resize
1577 * conf->disks and the scribble region
1579 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1581 for (i
=0; i
<conf
->raid_disks
; i
++)
1582 ndisks
[i
] = conf
->disks
[i
];
1584 conf
->disks
= ndisks
;
1589 conf
->scribble_len
= scribble_len(newsize
);
1590 for_each_present_cpu(cpu
) {
1591 struct raid5_percpu
*percpu
;
1594 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1595 scribble
= kmalloc(conf
->scribble_len
, GFP_NOIO
);
1598 kfree(percpu
->scribble
);
1599 percpu
->scribble
= scribble
;
1607 /* Step 4, return new stripes to service */
1608 while(!list_empty(&newstripes
)) {
1609 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1610 list_del_init(&nsh
->lru
);
1612 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1613 if (nsh
->dev
[i
].page
== NULL
) {
1614 struct page
*p
= alloc_page(GFP_NOIO
);
1615 nsh
->dev
[i
].page
= p
;
1619 release_stripe(nsh
);
1621 /* critical section pass, GFP_NOIO no longer needed */
1623 conf
->slab_cache
= sc
;
1624 conf
->active_name
= 1-conf
->active_name
;
1625 conf
->pool_size
= newsize
;
1629 static int drop_one_stripe(struct r5conf
*conf
)
1631 struct stripe_head
*sh
;
1633 spin_lock_irq(&conf
->device_lock
);
1634 sh
= get_free_stripe(conf
);
1635 spin_unlock_irq(&conf
->device_lock
);
1638 BUG_ON(atomic_read(&sh
->count
));
1640 kmem_cache_free(conf
->slab_cache
, sh
);
1641 atomic_dec(&conf
->active_stripes
);
1645 static void shrink_stripes(struct r5conf
*conf
)
1647 while (drop_one_stripe(conf
))
1650 if (conf
->slab_cache
)
1651 kmem_cache_destroy(conf
->slab_cache
);
1652 conf
->slab_cache
= NULL
;
1655 static void raid5_end_read_request(struct bio
* bi
, int error
)
1657 struct stripe_head
*sh
= bi
->bi_private
;
1658 struct r5conf
*conf
= sh
->raid_conf
;
1659 int disks
= sh
->disks
, i
;
1660 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1661 char b
[BDEVNAME_SIZE
];
1662 struct md_rdev
*rdev
= NULL
;
1665 for (i
=0 ; i
<disks
; i
++)
1666 if (bi
== &sh
->dev
[i
].req
)
1669 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1670 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1676 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1677 /* If replacement finished while this request was outstanding,
1678 * 'replacement' might be NULL already.
1679 * In that case it moved down to 'rdev'.
1680 * rdev is not removed until all requests are finished.
1682 rdev
= conf
->disks
[i
].replacement
;
1684 rdev
= conf
->disks
[i
].rdev
;
1687 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1688 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1689 /* Note that this cannot happen on a
1690 * replacement device. We just fail those on
1695 "md/raid:%s: read error corrected"
1696 " (%lu sectors at %llu on %s)\n",
1697 mdname(conf
->mddev
), STRIPE_SECTORS
,
1698 (unsigned long long)(sh
->sector
1699 + rdev
->data_offset
),
1700 bdevname(rdev
->bdev
, b
));
1701 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1702 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1703 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1705 if (atomic_read(&rdev
->read_errors
))
1706 atomic_set(&rdev
->read_errors
, 0);
1708 const char *bdn
= bdevname(rdev
->bdev
, b
);
1711 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1712 atomic_inc(&rdev
->read_errors
);
1713 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1716 "md/raid:%s: read error on replacement device "
1717 "(sector %llu on %s).\n",
1718 mdname(conf
->mddev
),
1719 (unsigned long long)(sh
->sector
1720 + rdev
->data_offset
),
1722 else if (conf
->mddev
->degraded
>= conf
->max_degraded
)
1725 "md/raid:%s: read error not correctable "
1726 "(sector %llu on %s).\n",
1727 mdname(conf
->mddev
),
1728 (unsigned long long)(sh
->sector
1729 + rdev
->data_offset
),
1731 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1735 "md/raid:%s: read error NOT corrected!! "
1736 "(sector %llu on %s).\n",
1737 mdname(conf
->mddev
),
1738 (unsigned long long)(sh
->sector
1739 + rdev
->data_offset
),
1741 else if (atomic_read(&rdev
->read_errors
)
1742 > conf
->max_nr_stripes
)
1744 "md/raid:%s: Too many read errors, failing device %s.\n",
1745 mdname(conf
->mddev
), bdn
);
1749 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1751 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1752 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1753 md_error(conf
->mddev
, rdev
);
1756 rdev_dec_pending(rdev
, conf
->mddev
);
1757 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1758 set_bit(STRIPE_HANDLE
, &sh
->state
);
1762 static void raid5_end_write_request(struct bio
*bi
, int error
)
1764 struct stripe_head
*sh
= bi
->bi_private
;
1765 struct r5conf
*conf
= sh
->raid_conf
;
1766 int disks
= sh
->disks
, i
;
1767 struct md_rdev
*uninitialized_var(rdev
);
1768 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1771 int replacement
= 0;
1773 for (i
= 0 ; i
< disks
; i
++) {
1774 if (bi
== &sh
->dev
[i
].req
) {
1775 rdev
= conf
->disks
[i
].rdev
;
1778 if (bi
== &sh
->dev
[i
].rreq
) {
1779 rdev
= conf
->disks
[i
].replacement
;
1783 /* rdev was removed and 'replacement'
1784 * replaced it. rdev is not removed
1785 * until all requests are finished.
1787 rdev
= conf
->disks
[i
].rdev
;
1791 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1792 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1801 md_error(conf
->mddev
, rdev
);
1802 else if (is_badblock(rdev
, sh
->sector
,
1804 &first_bad
, &bad_sectors
))
1805 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
1808 set_bit(WriteErrorSeen
, &rdev
->flags
);
1809 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
1810 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1811 set_bit(MD_RECOVERY_NEEDED
,
1812 &rdev
->mddev
->recovery
);
1813 } else if (is_badblock(rdev
, sh
->sector
,
1815 &first_bad
, &bad_sectors
))
1816 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
1818 rdev_dec_pending(rdev
, conf
->mddev
);
1820 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
1821 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1822 set_bit(STRIPE_HANDLE
, &sh
->state
);
1826 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
);
1828 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
1830 struct r5dev
*dev
= &sh
->dev
[i
];
1832 bio_init(&dev
->req
);
1833 dev
->req
.bi_io_vec
= &dev
->vec
;
1835 dev
->req
.bi_max_vecs
++;
1836 dev
->req
.bi_private
= sh
;
1837 dev
->vec
.bv_page
= dev
->page
;
1839 bio_init(&dev
->rreq
);
1840 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
1841 dev
->rreq
.bi_vcnt
++;
1842 dev
->rreq
.bi_max_vecs
++;
1843 dev
->rreq
.bi_private
= sh
;
1844 dev
->rvec
.bv_page
= dev
->page
;
1847 dev
->sector
= compute_blocknr(sh
, i
, previous
);
1850 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1852 char b
[BDEVNAME_SIZE
];
1853 struct r5conf
*conf
= mddev
->private;
1854 unsigned long flags
;
1855 pr_debug("raid456: error called\n");
1857 spin_lock_irqsave(&conf
->device_lock
, flags
);
1858 clear_bit(In_sync
, &rdev
->flags
);
1859 mddev
->degraded
= calc_degraded(conf
);
1860 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1861 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1863 set_bit(Blocked
, &rdev
->flags
);
1864 set_bit(Faulty
, &rdev
->flags
);
1865 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1867 "md/raid:%s: Disk failure on %s, disabling device.\n"
1868 "md/raid:%s: Operation continuing on %d devices.\n",
1870 bdevname(rdev
->bdev
, b
),
1872 conf
->raid_disks
- mddev
->degraded
);
1876 * Input: a 'big' sector number,
1877 * Output: index of the data and parity disk, and the sector # in them.
1879 static sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
1880 int previous
, int *dd_idx
,
1881 struct stripe_head
*sh
)
1883 sector_t stripe
, stripe2
;
1884 sector_t chunk_number
;
1885 unsigned int chunk_offset
;
1888 sector_t new_sector
;
1889 int algorithm
= previous
? conf
->prev_algo
1891 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
1892 : conf
->chunk_sectors
;
1893 int raid_disks
= previous
? conf
->previous_raid_disks
1895 int data_disks
= raid_disks
- conf
->max_degraded
;
1897 /* First compute the information on this sector */
1900 * Compute the chunk number and the sector offset inside the chunk
1902 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
1903 chunk_number
= r_sector
;
1906 * Compute the stripe number
1908 stripe
= chunk_number
;
1909 *dd_idx
= sector_div(stripe
, data_disks
);
1912 * Select the parity disk based on the user selected algorithm.
1914 pd_idx
= qd_idx
= -1;
1915 switch(conf
->level
) {
1917 pd_idx
= data_disks
;
1920 switch (algorithm
) {
1921 case ALGORITHM_LEFT_ASYMMETRIC
:
1922 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1923 if (*dd_idx
>= pd_idx
)
1926 case ALGORITHM_RIGHT_ASYMMETRIC
:
1927 pd_idx
= sector_div(stripe2
, raid_disks
);
1928 if (*dd_idx
>= pd_idx
)
1931 case ALGORITHM_LEFT_SYMMETRIC
:
1932 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1933 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1935 case ALGORITHM_RIGHT_SYMMETRIC
:
1936 pd_idx
= sector_div(stripe2
, raid_disks
);
1937 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1939 case ALGORITHM_PARITY_0
:
1943 case ALGORITHM_PARITY_N
:
1944 pd_idx
= data_disks
;
1952 switch (algorithm
) {
1953 case ALGORITHM_LEFT_ASYMMETRIC
:
1954 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
1955 qd_idx
= pd_idx
+ 1;
1956 if (pd_idx
== raid_disks
-1) {
1957 (*dd_idx
)++; /* Q D D D P */
1959 } else if (*dd_idx
>= pd_idx
)
1960 (*dd_idx
) += 2; /* D D P Q D */
1962 case ALGORITHM_RIGHT_ASYMMETRIC
:
1963 pd_idx
= sector_div(stripe2
, raid_disks
);
1964 qd_idx
= pd_idx
+ 1;
1965 if (pd_idx
== raid_disks
-1) {
1966 (*dd_idx
)++; /* Q D D D P */
1968 } else if (*dd_idx
>= pd_idx
)
1969 (*dd_idx
) += 2; /* D D P Q D */
1971 case ALGORITHM_LEFT_SYMMETRIC
:
1972 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
1973 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1974 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1976 case ALGORITHM_RIGHT_SYMMETRIC
:
1977 pd_idx
= sector_div(stripe2
, raid_disks
);
1978 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1979 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1982 case ALGORITHM_PARITY_0
:
1987 case ALGORITHM_PARITY_N
:
1988 pd_idx
= data_disks
;
1989 qd_idx
= data_disks
+ 1;
1992 case ALGORITHM_ROTATING_ZERO_RESTART
:
1993 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1994 * of blocks for computing Q is different.
1996 pd_idx
= sector_div(stripe2
, raid_disks
);
1997 qd_idx
= pd_idx
+ 1;
1998 if (pd_idx
== raid_disks
-1) {
1999 (*dd_idx
)++; /* Q D D D P */
2001 } else if (*dd_idx
>= pd_idx
)
2002 (*dd_idx
) += 2; /* D D P Q D */
2006 case ALGORITHM_ROTATING_N_RESTART
:
2007 /* Same a left_asymmetric, by first stripe is
2008 * D D D P Q rather than
2012 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2013 qd_idx
= pd_idx
+ 1;
2014 if (pd_idx
== raid_disks
-1) {
2015 (*dd_idx
)++; /* Q D D D P */
2017 } else if (*dd_idx
>= pd_idx
)
2018 (*dd_idx
) += 2; /* D D P Q D */
2022 case ALGORITHM_ROTATING_N_CONTINUE
:
2023 /* Same as left_symmetric but Q is before P */
2024 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2025 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2026 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2030 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2031 /* RAID5 left_asymmetric, with Q on last device */
2032 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2033 if (*dd_idx
>= pd_idx
)
2035 qd_idx
= raid_disks
- 1;
2038 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2039 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2040 if (*dd_idx
>= pd_idx
)
2042 qd_idx
= raid_disks
- 1;
2045 case ALGORITHM_LEFT_SYMMETRIC_6
:
2046 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2047 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2048 qd_idx
= raid_disks
- 1;
2051 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2052 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2053 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2054 qd_idx
= raid_disks
- 1;
2057 case ALGORITHM_PARITY_0_6
:
2060 qd_idx
= raid_disks
- 1;
2070 sh
->pd_idx
= pd_idx
;
2071 sh
->qd_idx
= qd_idx
;
2072 sh
->ddf_layout
= ddf_layout
;
2075 * Finally, compute the new sector number
2077 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2082 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2084 struct r5conf
*conf
= sh
->raid_conf
;
2085 int raid_disks
= sh
->disks
;
2086 int data_disks
= raid_disks
- conf
->max_degraded
;
2087 sector_t new_sector
= sh
->sector
, check
;
2088 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2089 : conf
->chunk_sectors
;
2090 int algorithm
= previous
? conf
->prev_algo
2094 sector_t chunk_number
;
2095 int dummy1
, dd_idx
= i
;
2097 struct stripe_head sh2
;
2100 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2101 stripe
= new_sector
;
2103 if (i
== sh
->pd_idx
)
2105 switch(conf
->level
) {
2108 switch (algorithm
) {
2109 case ALGORITHM_LEFT_ASYMMETRIC
:
2110 case ALGORITHM_RIGHT_ASYMMETRIC
:
2114 case ALGORITHM_LEFT_SYMMETRIC
:
2115 case ALGORITHM_RIGHT_SYMMETRIC
:
2118 i
-= (sh
->pd_idx
+ 1);
2120 case ALGORITHM_PARITY_0
:
2123 case ALGORITHM_PARITY_N
:
2130 if (i
== sh
->qd_idx
)
2131 return 0; /* It is the Q disk */
2132 switch (algorithm
) {
2133 case ALGORITHM_LEFT_ASYMMETRIC
:
2134 case ALGORITHM_RIGHT_ASYMMETRIC
:
2135 case ALGORITHM_ROTATING_ZERO_RESTART
:
2136 case ALGORITHM_ROTATING_N_RESTART
:
2137 if (sh
->pd_idx
== raid_disks
-1)
2138 i
--; /* Q D D D P */
2139 else if (i
> sh
->pd_idx
)
2140 i
-= 2; /* D D P Q D */
2142 case ALGORITHM_LEFT_SYMMETRIC
:
2143 case ALGORITHM_RIGHT_SYMMETRIC
:
2144 if (sh
->pd_idx
== raid_disks
-1)
2145 i
--; /* Q D D D P */
2150 i
-= (sh
->pd_idx
+ 2);
2153 case ALGORITHM_PARITY_0
:
2156 case ALGORITHM_PARITY_N
:
2158 case ALGORITHM_ROTATING_N_CONTINUE
:
2159 /* Like left_symmetric, but P is before Q */
2160 if (sh
->pd_idx
== 0)
2161 i
--; /* P D D D Q */
2166 i
-= (sh
->pd_idx
+ 1);
2169 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2170 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2174 case ALGORITHM_LEFT_SYMMETRIC_6
:
2175 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2177 i
+= data_disks
+ 1;
2178 i
-= (sh
->pd_idx
+ 1);
2180 case ALGORITHM_PARITY_0_6
:
2189 chunk_number
= stripe
* data_disks
+ i
;
2190 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2192 check
= raid5_compute_sector(conf
, r_sector
,
2193 previous
, &dummy1
, &sh2
);
2194 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2195 || sh2
.qd_idx
!= sh
->qd_idx
) {
2196 printk(KERN_ERR
"md/raid:%s: compute_blocknr: map not correct\n",
2197 mdname(conf
->mddev
));
2205 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2206 int rcw
, int expand
)
2208 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
2209 struct r5conf
*conf
= sh
->raid_conf
;
2210 int level
= conf
->level
;
2213 /* if we are not expanding this is a proper write request, and
2214 * there will be bios with new data to be drained into the
2218 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2219 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2221 sh
->reconstruct_state
= reconstruct_state_run
;
2223 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2225 for (i
= disks
; i
--; ) {
2226 struct r5dev
*dev
= &sh
->dev
[i
];
2229 set_bit(R5_LOCKED
, &dev
->flags
);
2230 set_bit(R5_Wantdrain
, &dev
->flags
);
2232 clear_bit(R5_UPTODATE
, &dev
->flags
);
2236 if (s
->locked
+ conf
->max_degraded
== disks
)
2237 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2238 atomic_inc(&conf
->pending_full_writes
);
2241 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2242 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2244 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2245 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2246 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2247 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2249 for (i
= disks
; i
--; ) {
2250 struct r5dev
*dev
= &sh
->dev
[i
];
2255 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2256 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2257 set_bit(R5_Wantdrain
, &dev
->flags
);
2258 set_bit(R5_LOCKED
, &dev
->flags
);
2259 clear_bit(R5_UPTODATE
, &dev
->flags
);
2265 /* keep the parity disk(s) locked while asynchronous operations
2268 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2269 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2273 int qd_idx
= sh
->qd_idx
;
2274 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2276 set_bit(R5_LOCKED
, &dev
->flags
);
2277 clear_bit(R5_UPTODATE
, &dev
->flags
);
2281 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2282 __func__
, (unsigned long long)sh
->sector
,
2283 s
->locked
, s
->ops_request
);
2287 * Each stripe/dev can have one or more bion attached.
2288 * toread/towrite point to the first in a chain.
2289 * The bi_next chain must be in order.
2291 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
2294 struct r5conf
*conf
= sh
->raid_conf
;
2297 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2298 (unsigned long long)bi
->bi_sector
,
2299 (unsigned long long)sh
->sector
);
2302 spin_lock_irq(&conf
->device_lock
);
2304 bip
= &sh
->dev
[dd_idx
].towrite
;
2305 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
2308 bip
= &sh
->dev
[dd_idx
].toread
;
2309 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
2310 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
2312 bip
= & (*bip
)->bi_next
;
2314 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
2317 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
2321 bi
->bi_phys_segments
++;
2324 /* check if page is covered */
2325 sector_t sector
= sh
->dev
[dd_idx
].sector
;
2326 for (bi
=sh
->dev
[dd_idx
].towrite
;
2327 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
2328 bi
&& bi
->bi_sector
<= sector
;
2329 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
2330 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
2331 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
2333 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
2334 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
2336 spin_unlock_irq(&conf
->device_lock
);
2338 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2339 (unsigned long long)(*bip
)->bi_sector
,
2340 (unsigned long long)sh
->sector
, dd_idx
);
2342 if (conf
->mddev
->bitmap
&& firstwrite
) {
2343 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
2345 sh
->bm_seq
= conf
->seq_flush
+1;
2346 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
2351 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
2352 spin_unlock_irq(&conf
->device_lock
);
2356 static void end_reshape(struct r5conf
*conf
);
2358 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
2359 struct stripe_head
*sh
)
2361 int sectors_per_chunk
=
2362 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
2364 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
2365 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
2367 raid5_compute_sector(conf
,
2368 stripe
* (disks
- conf
->max_degraded
)
2369 *sectors_per_chunk
+ chunk_offset
,
2375 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
2376 struct stripe_head_state
*s
, int disks
,
2377 struct bio
**return_bi
)
2380 for (i
= disks
; i
--; ) {
2384 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2385 struct md_rdev
*rdev
;
2387 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2388 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2389 atomic_inc(&rdev
->nr_pending
);
2394 if (!rdev_set_badblocks(
2398 md_error(conf
->mddev
, rdev
);
2399 rdev_dec_pending(rdev
, conf
->mddev
);
2402 spin_lock_irq(&conf
->device_lock
);
2403 /* fail all writes first */
2404 bi
= sh
->dev
[i
].towrite
;
2405 sh
->dev
[i
].towrite
= NULL
;
2411 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2412 wake_up(&conf
->wait_for_overlap
);
2414 while (bi
&& bi
->bi_sector
<
2415 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2416 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2417 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2418 if (!raid5_dec_bi_phys_segments(bi
)) {
2419 md_write_end(conf
->mddev
);
2420 bi
->bi_next
= *return_bi
;
2425 /* and fail all 'written' */
2426 bi
= sh
->dev
[i
].written
;
2427 sh
->dev
[i
].written
= NULL
;
2428 if (bi
) bitmap_end
= 1;
2429 while (bi
&& bi
->bi_sector
<
2430 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2431 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2432 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2433 if (!raid5_dec_bi_phys_segments(bi
)) {
2434 md_write_end(conf
->mddev
);
2435 bi
->bi_next
= *return_bi
;
2441 /* fail any reads if this device is non-operational and
2442 * the data has not reached the cache yet.
2444 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2445 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2446 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2447 bi
= sh
->dev
[i
].toread
;
2448 sh
->dev
[i
].toread
= NULL
;
2449 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2450 wake_up(&conf
->wait_for_overlap
);
2451 if (bi
) s
->to_read
--;
2452 while (bi
&& bi
->bi_sector
<
2453 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2454 struct bio
*nextbi
=
2455 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2456 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2457 if (!raid5_dec_bi_phys_segments(bi
)) {
2458 bi
->bi_next
= *return_bi
;
2464 spin_unlock_irq(&conf
->device_lock
);
2466 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2467 STRIPE_SECTORS
, 0, 0);
2468 /* If we were in the middle of a write the parity block might
2469 * still be locked - so just clear all R5_LOCKED flags
2471 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2474 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2475 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2476 md_wakeup_thread(conf
->mddev
->thread
);
2480 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
2481 struct stripe_head_state
*s
)
2486 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2489 /* There is nothing more to do for sync/check/repair.
2490 * Don't even need to abort as that is handled elsewhere
2491 * if needed, and not always wanted e.g. if there is a known
2493 * For recover/replace we need to record a bad block on all
2494 * non-sync devices, or abort the recovery
2496 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
2497 /* During recovery devices cannot be removed, so
2498 * locking and refcounting of rdevs is not needed
2500 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2501 struct md_rdev
*rdev
= conf
->disks
[i
].rdev
;
2503 && !test_bit(Faulty
, &rdev
->flags
)
2504 && !test_bit(In_sync
, &rdev
->flags
)
2505 && !rdev_set_badblocks(rdev
, sh
->sector
,
2508 rdev
= conf
->disks
[i
].replacement
;
2510 && !test_bit(Faulty
, &rdev
->flags
)
2511 && !test_bit(In_sync
, &rdev
->flags
)
2512 && !rdev_set_badblocks(rdev
, sh
->sector
,
2517 conf
->recovery_disabled
=
2518 conf
->mddev
->recovery_disabled
;
2520 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
2523 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
2525 struct md_rdev
*rdev
;
2527 /* Doing recovery so rcu locking not required */
2528 rdev
= sh
->raid_conf
->disks
[disk_idx
].replacement
;
2530 && !test_bit(Faulty
, &rdev
->flags
)
2531 && !test_bit(In_sync
, &rdev
->flags
)
2532 && (rdev
->recovery_offset
<= sh
->sector
2533 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
2539 /* fetch_block - checks the given member device to see if its data needs
2540 * to be read or computed to satisfy a request.
2542 * Returns 1 when no more member devices need to be checked, otherwise returns
2543 * 0 to tell the loop in handle_stripe_fill to continue
2545 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2546 int disk_idx
, int disks
)
2548 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2549 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
2550 &sh
->dev
[s
->failed_num
[1]] };
2552 /* is the data in this block needed, and can we get it? */
2553 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2554 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2556 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2557 s
->syncing
|| s
->expanding
||
2558 (s
->replacing
&& want_replace(sh
, disk_idx
)) ||
2559 (s
->failed
>= 1 && fdev
[0]->toread
) ||
2560 (s
->failed
>= 2 && fdev
[1]->toread
) ||
2561 (sh
->raid_conf
->level
<= 5 && s
->failed
&& fdev
[0]->towrite
&&
2562 !test_bit(R5_OVERWRITE
, &fdev
[0]->flags
)) ||
2563 (sh
->raid_conf
->level
== 6 && s
->failed
&& s
->to_write
))) {
2564 /* we would like to get this block, possibly by computing it,
2565 * otherwise read it if the backing disk is insync
2567 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
2568 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
2569 if ((s
->uptodate
== disks
- 1) &&
2570 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
2571 disk_idx
== s
->failed_num
[1]))) {
2572 /* have disk failed, and we're requested to fetch it;
2575 pr_debug("Computing stripe %llu block %d\n",
2576 (unsigned long long)sh
->sector
, disk_idx
);
2577 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2578 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2579 set_bit(R5_Wantcompute
, &dev
->flags
);
2580 sh
->ops
.target
= disk_idx
;
2581 sh
->ops
.target2
= -1; /* no 2nd target */
2583 /* Careful: from this point on 'uptodate' is in the eye
2584 * of raid_run_ops which services 'compute' operations
2585 * before writes. R5_Wantcompute flags a block that will
2586 * be R5_UPTODATE by the time it is needed for a
2587 * subsequent operation.
2591 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
2592 /* Computing 2-failure is *very* expensive; only
2593 * do it if failed >= 2
2596 for (other
= disks
; other
--; ) {
2597 if (other
== disk_idx
)
2599 if (!test_bit(R5_UPTODATE
,
2600 &sh
->dev
[other
].flags
))
2604 pr_debug("Computing stripe %llu blocks %d,%d\n",
2605 (unsigned long long)sh
->sector
,
2607 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2608 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2609 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
2610 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
2611 sh
->ops
.target
= disk_idx
;
2612 sh
->ops
.target2
= other
;
2616 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2617 set_bit(R5_LOCKED
, &dev
->flags
);
2618 set_bit(R5_Wantread
, &dev
->flags
);
2620 pr_debug("Reading block %d (sync=%d)\n",
2621 disk_idx
, s
->syncing
);
2629 * handle_stripe_fill - read or compute data to satisfy pending requests.
2631 static void handle_stripe_fill(struct stripe_head
*sh
,
2632 struct stripe_head_state
*s
,
2637 /* look for blocks to read/compute, skip this if a compute
2638 * is already in flight, or if the stripe contents are in the
2639 * midst of changing due to a write
2641 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2642 !sh
->reconstruct_state
)
2643 for (i
= disks
; i
--; )
2644 if (fetch_block(sh
, s
, i
, disks
))
2646 set_bit(STRIPE_HANDLE
, &sh
->state
);
2650 /* handle_stripe_clean_event
2651 * any written block on an uptodate or failed drive can be returned.
2652 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2653 * never LOCKED, so we don't need to test 'failed' directly.
2655 static void handle_stripe_clean_event(struct r5conf
*conf
,
2656 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2661 for (i
= disks
; i
--; )
2662 if (sh
->dev
[i
].written
) {
2664 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2665 test_bit(R5_UPTODATE
, &dev
->flags
)) {
2666 /* We can return any write requests */
2667 struct bio
*wbi
, *wbi2
;
2669 pr_debug("Return write for disc %d\n", i
);
2670 spin_lock_irq(&conf
->device_lock
);
2672 dev
->written
= NULL
;
2673 while (wbi
&& wbi
->bi_sector
<
2674 dev
->sector
+ STRIPE_SECTORS
) {
2675 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2676 if (!raid5_dec_bi_phys_segments(wbi
)) {
2677 md_write_end(conf
->mddev
);
2678 wbi
->bi_next
= *return_bi
;
2683 if (dev
->towrite
== NULL
)
2685 spin_unlock_irq(&conf
->device_lock
);
2687 bitmap_endwrite(conf
->mddev
->bitmap
,
2690 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
2695 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2696 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2697 md_wakeup_thread(conf
->mddev
->thread
);
2700 static void handle_stripe_dirtying(struct r5conf
*conf
,
2701 struct stripe_head
*sh
,
2702 struct stripe_head_state
*s
,
2705 int rmw
= 0, rcw
= 0, i
;
2706 if (conf
->max_degraded
== 2) {
2707 /* RAID6 requires 'rcw' in current implementation
2708 * Calculate the real rcw later - for now fake it
2709 * look like rcw is cheaper
2712 } else for (i
= disks
; i
--; ) {
2713 /* would I have to read this buffer for read_modify_write */
2714 struct r5dev
*dev
= &sh
->dev
[i
];
2715 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2716 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2717 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2718 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2719 if (test_bit(R5_Insync
, &dev
->flags
))
2722 rmw
+= 2*disks
; /* cannot read it */
2724 /* Would I have to read this buffer for reconstruct_write */
2725 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
2726 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2727 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2728 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2729 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2734 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2735 (unsigned long long)sh
->sector
, rmw
, rcw
);
2736 set_bit(STRIPE_HANDLE
, &sh
->state
);
2737 if (rmw
< rcw
&& rmw
> 0)
2738 /* prefer read-modify-write, but need to get some data */
2739 for (i
= disks
; i
--; ) {
2740 struct r5dev
*dev
= &sh
->dev
[i
];
2741 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2742 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2743 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2744 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2745 test_bit(R5_Insync
, &dev
->flags
)) {
2747 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2748 pr_debug("Read_old block "
2749 "%d for r-m-w\n", i
);
2750 set_bit(R5_LOCKED
, &dev
->flags
);
2751 set_bit(R5_Wantread
, &dev
->flags
);
2754 set_bit(STRIPE_DELAYED
, &sh
->state
);
2755 set_bit(STRIPE_HANDLE
, &sh
->state
);
2759 if (rcw
<= rmw
&& rcw
> 0) {
2760 /* want reconstruct write, but need to get some data */
2762 for (i
= disks
; i
--; ) {
2763 struct r5dev
*dev
= &sh
->dev
[i
];
2764 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2765 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
2766 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2767 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2768 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2770 if (!test_bit(R5_Insync
, &dev
->flags
))
2771 continue; /* it's a failed drive */
2773 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2774 pr_debug("Read_old block "
2775 "%d for Reconstruct\n", i
);
2776 set_bit(R5_LOCKED
, &dev
->flags
);
2777 set_bit(R5_Wantread
, &dev
->flags
);
2780 set_bit(STRIPE_DELAYED
, &sh
->state
);
2781 set_bit(STRIPE_HANDLE
, &sh
->state
);
2786 /* now if nothing is locked, and if we have enough data,
2787 * we can start a write request
2789 /* since handle_stripe can be called at any time we need to handle the
2790 * case where a compute block operation has been submitted and then a
2791 * subsequent call wants to start a write request. raid_run_ops only
2792 * handles the case where compute block and reconstruct are requested
2793 * simultaneously. If this is not the case then new writes need to be
2794 * held off until the compute completes.
2796 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
2797 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
2798 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
2799 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
2802 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
2803 struct stripe_head_state
*s
, int disks
)
2805 struct r5dev
*dev
= NULL
;
2807 set_bit(STRIPE_HANDLE
, &sh
->state
);
2809 switch (sh
->check_state
) {
2810 case check_state_idle
:
2811 /* start a new check operation if there are no failures */
2812 if (s
->failed
== 0) {
2813 BUG_ON(s
->uptodate
!= disks
);
2814 sh
->check_state
= check_state_run
;
2815 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2816 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
2820 dev
= &sh
->dev
[s
->failed_num
[0]];
2822 case check_state_compute_result
:
2823 sh
->check_state
= check_state_idle
;
2825 dev
= &sh
->dev
[sh
->pd_idx
];
2827 /* check that a write has not made the stripe insync */
2828 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2831 /* either failed parity check, or recovery is happening */
2832 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
2833 BUG_ON(s
->uptodate
!= disks
);
2835 set_bit(R5_LOCKED
, &dev
->flags
);
2837 set_bit(R5_Wantwrite
, &dev
->flags
);
2839 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2840 set_bit(STRIPE_INSYNC
, &sh
->state
);
2842 case check_state_run
:
2843 break; /* we will be called again upon completion */
2844 case check_state_check_result
:
2845 sh
->check_state
= check_state_idle
;
2847 /* if a failure occurred during the check operation, leave
2848 * STRIPE_INSYNC not set and let the stripe be handled again
2853 /* handle a successful check operation, if parity is correct
2854 * we are done. Otherwise update the mismatch count and repair
2855 * parity if !MD_RECOVERY_CHECK
2857 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
2858 /* parity is correct (on disc,
2859 * not in buffer any more)
2861 set_bit(STRIPE_INSYNC
, &sh
->state
);
2863 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2864 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2865 /* don't try to repair!! */
2866 set_bit(STRIPE_INSYNC
, &sh
->state
);
2868 sh
->check_state
= check_state_compute_run
;
2869 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2870 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2871 set_bit(R5_Wantcompute
,
2872 &sh
->dev
[sh
->pd_idx
].flags
);
2873 sh
->ops
.target
= sh
->pd_idx
;
2874 sh
->ops
.target2
= -1;
2879 case check_state_compute_run
:
2882 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
2883 __func__
, sh
->check_state
,
2884 (unsigned long long) sh
->sector
);
2890 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
2891 struct stripe_head_state
*s
,
2894 int pd_idx
= sh
->pd_idx
;
2895 int qd_idx
= sh
->qd_idx
;
2898 set_bit(STRIPE_HANDLE
, &sh
->state
);
2900 BUG_ON(s
->failed
> 2);
2902 /* Want to check and possibly repair P and Q.
2903 * However there could be one 'failed' device, in which
2904 * case we can only check one of them, possibly using the
2905 * other to generate missing data
2908 switch (sh
->check_state
) {
2909 case check_state_idle
:
2910 /* start a new check operation if there are < 2 failures */
2911 if (s
->failed
== s
->q_failed
) {
2912 /* The only possible failed device holds Q, so it
2913 * makes sense to check P (If anything else were failed,
2914 * we would have used P to recreate it).
2916 sh
->check_state
= check_state_run
;
2918 if (!s
->q_failed
&& s
->failed
< 2) {
2919 /* Q is not failed, and we didn't use it to generate
2920 * anything, so it makes sense to check it
2922 if (sh
->check_state
== check_state_run
)
2923 sh
->check_state
= check_state_run_pq
;
2925 sh
->check_state
= check_state_run_q
;
2928 /* discard potentially stale zero_sum_result */
2929 sh
->ops
.zero_sum_result
= 0;
2931 if (sh
->check_state
== check_state_run
) {
2932 /* async_xor_zero_sum destroys the contents of P */
2933 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2936 if (sh
->check_state
>= check_state_run
&&
2937 sh
->check_state
<= check_state_run_pq
) {
2938 /* async_syndrome_zero_sum preserves P and Q, so
2939 * no need to mark them !uptodate here
2941 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2945 /* we have 2-disk failure */
2946 BUG_ON(s
->failed
!= 2);
2948 case check_state_compute_result
:
2949 sh
->check_state
= check_state_idle
;
2951 /* check that a write has not made the stripe insync */
2952 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2955 /* now write out any block on a failed drive,
2956 * or P or Q if they were recomputed
2958 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
2959 if (s
->failed
== 2) {
2960 dev
= &sh
->dev
[s
->failed_num
[1]];
2962 set_bit(R5_LOCKED
, &dev
->flags
);
2963 set_bit(R5_Wantwrite
, &dev
->flags
);
2965 if (s
->failed
>= 1) {
2966 dev
= &sh
->dev
[s
->failed_num
[0]];
2968 set_bit(R5_LOCKED
, &dev
->flags
);
2969 set_bit(R5_Wantwrite
, &dev
->flags
);
2971 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
2972 dev
= &sh
->dev
[pd_idx
];
2974 set_bit(R5_LOCKED
, &dev
->flags
);
2975 set_bit(R5_Wantwrite
, &dev
->flags
);
2977 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
2978 dev
= &sh
->dev
[qd_idx
];
2980 set_bit(R5_LOCKED
, &dev
->flags
);
2981 set_bit(R5_Wantwrite
, &dev
->flags
);
2983 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2985 set_bit(STRIPE_INSYNC
, &sh
->state
);
2987 case check_state_run
:
2988 case check_state_run_q
:
2989 case check_state_run_pq
:
2990 break; /* we will be called again upon completion */
2991 case check_state_check_result
:
2992 sh
->check_state
= check_state_idle
;
2994 /* handle a successful check operation, if parity is correct
2995 * we are done. Otherwise update the mismatch count and repair
2996 * parity if !MD_RECOVERY_CHECK
2998 if (sh
->ops
.zero_sum_result
== 0) {
2999 /* both parities are correct */
3001 set_bit(STRIPE_INSYNC
, &sh
->state
);
3003 /* in contrast to the raid5 case we can validate
3004 * parity, but still have a failure to write
3007 sh
->check_state
= check_state_compute_result
;
3008 /* Returning at this point means that we may go
3009 * off and bring p and/or q uptodate again so
3010 * we make sure to check zero_sum_result again
3011 * to verify if p or q need writeback
3015 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
3016 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3017 /* don't try to repair!! */
3018 set_bit(STRIPE_INSYNC
, &sh
->state
);
3020 int *target
= &sh
->ops
.target
;
3022 sh
->ops
.target
= -1;
3023 sh
->ops
.target2
= -1;
3024 sh
->check_state
= check_state_compute_run
;
3025 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3026 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3027 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3028 set_bit(R5_Wantcompute
,
3029 &sh
->dev
[pd_idx
].flags
);
3031 target
= &sh
->ops
.target2
;
3034 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3035 set_bit(R5_Wantcompute
,
3036 &sh
->dev
[qd_idx
].flags
);
3043 case check_state_compute_run
:
3046 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3047 __func__
, sh
->check_state
,
3048 (unsigned long long) sh
->sector
);
3053 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3057 /* We have read all the blocks in this stripe and now we need to
3058 * copy some of them into a target stripe for expand.
3060 struct dma_async_tx_descriptor
*tx
= NULL
;
3061 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3062 for (i
= 0; i
< sh
->disks
; i
++)
3063 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3065 struct stripe_head
*sh2
;
3066 struct async_submit_ctl submit
;
3068 sector_t bn
= compute_blocknr(sh
, i
, 1);
3069 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3071 sh2
= get_active_stripe(conf
, s
, 0, 1, 1);
3073 /* so far only the early blocks of this stripe
3074 * have been requested. When later blocks
3075 * get requested, we will try again
3078 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3079 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3080 /* must have already done this block */
3081 release_stripe(sh2
);
3085 /* place all the copies on one channel */
3086 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
3087 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
3088 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
3091 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
3092 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
3093 for (j
= 0; j
< conf
->raid_disks
; j
++)
3094 if (j
!= sh2
->pd_idx
&&
3096 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
3098 if (j
== conf
->raid_disks
) {
3099 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
3100 set_bit(STRIPE_HANDLE
, &sh2
->state
);
3102 release_stripe(sh2
);
3105 /* done submitting copies, wait for them to complete */
3108 dma_wait_for_async_tx(tx
);
3113 * handle_stripe - do things to a stripe.
3115 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3116 * state of various bits to see what needs to be done.
3118 * return some read requests which now have data
3119 * return some write requests which are safely on storage
3120 * schedule a read on some buffers
3121 * schedule a write of some buffers
3122 * return confirmation of parity correctness
3126 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
3128 struct r5conf
*conf
= sh
->raid_conf
;
3129 int disks
= sh
->disks
;
3132 int do_recovery
= 0;
3134 memset(s
, 0, sizeof(*s
));
3136 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3137 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3138 s
->failed_num
[0] = -1;
3139 s
->failed_num
[1] = -1;
3141 /* Now to look around and see what can be done */
3143 spin_lock_irq(&conf
->device_lock
);
3144 for (i
=disks
; i
--; ) {
3145 struct md_rdev
*rdev
;
3152 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3154 dev
->toread
, dev
->towrite
, dev
->written
);
3155 /* maybe we can reply to a read
3157 * new wantfill requests are only permitted while
3158 * ops_complete_biofill is guaranteed to be inactive
3160 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
3161 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
3162 set_bit(R5_Wantfill
, &dev
->flags
);
3164 /* now count some things */
3165 if (test_bit(R5_LOCKED
, &dev
->flags
))
3167 if (test_bit(R5_UPTODATE
, &dev
->flags
))
3169 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
3171 BUG_ON(s
->compute
> 2);
3174 if (test_bit(R5_Wantfill
, &dev
->flags
))
3176 else if (dev
->toread
)
3180 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3185 /* Prefer to use the replacement for reads, but only
3186 * if it is recovered enough and has no bad blocks.
3188 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3189 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
3190 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
3191 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3192 &first_bad
, &bad_sectors
))
3193 set_bit(R5_ReadRepl
, &dev
->flags
);
3196 set_bit(R5_NeedReplace
, &dev
->flags
);
3197 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3198 clear_bit(R5_ReadRepl
, &dev
->flags
);
3200 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
3203 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3204 &first_bad
, &bad_sectors
);
3205 if (s
->blocked_rdev
== NULL
3206 && (test_bit(Blocked
, &rdev
->flags
)
3209 set_bit(BlockedBadBlocks
,
3211 s
->blocked_rdev
= rdev
;
3212 atomic_inc(&rdev
->nr_pending
);
3215 clear_bit(R5_Insync
, &dev
->flags
);
3219 /* also not in-sync */
3220 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
3221 test_bit(R5_UPTODATE
, &dev
->flags
)) {
3222 /* treat as in-sync, but with a read error
3223 * which we can now try to correct
3225 set_bit(R5_Insync
, &dev
->flags
);
3226 set_bit(R5_ReadError
, &dev
->flags
);
3228 } else if (test_bit(In_sync
, &rdev
->flags
))
3229 set_bit(R5_Insync
, &dev
->flags
);
3230 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
3231 /* in sync if before recovery_offset */
3232 set_bit(R5_Insync
, &dev
->flags
);
3233 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
3234 test_bit(R5_Expanded
, &dev
->flags
))
3235 /* If we've reshaped into here, we assume it is Insync.
3236 * We will shortly update recovery_offset to make
3239 set_bit(R5_Insync
, &dev
->flags
);
3241 if (rdev
&& test_bit(R5_WriteError
, &dev
->flags
)) {
3242 /* This flag does not apply to '.replacement'
3243 * only to .rdev, so make sure to check that*/
3244 struct md_rdev
*rdev2
= rcu_dereference(
3245 conf
->disks
[i
].rdev
);
3247 clear_bit(R5_Insync
, &dev
->flags
);
3248 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3249 s
->handle_bad_blocks
= 1;
3250 atomic_inc(&rdev2
->nr_pending
);
3252 clear_bit(R5_WriteError
, &dev
->flags
);
3254 if (rdev
&& test_bit(R5_MadeGood
, &dev
->flags
)) {
3255 /* This flag does not apply to '.replacement'
3256 * only to .rdev, so make sure to check that*/
3257 struct md_rdev
*rdev2
= rcu_dereference(
3258 conf
->disks
[i
].rdev
);
3259 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3260 s
->handle_bad_blocks
= 1;
3261 atomic_inc(&rdev2
->nr_pending
);
3263 clear_bit(R5_MadeGood
, &dev
->flags
);
3265 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3266 struct md_rdev
*rdev2
= rcu_dereference(
3267 conf
->disks
[i
].replacement
);
3268 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3269 s
->handle_bad_blocks
= 1;
3270 atomic_inc(&rdev2
->nr_pending
);
3272 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
3274 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3275 /* The ReadError flag will just be confusing now */
3276 clear_bit(R5_ReadError
, &dev
->flags
);
3277 clear_bit(R5_ReWrite
, &dev
->flags
);
3279 if (test_bit(R5_ReadError
, &dev
->flags
))
3280 clear_bit(R5_Insync
, &dev
->flags
);
3281 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3283 s
->failed_num
[s
->failed
] = i
;
3285 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
3289 spin_unlock_irq(&conf
->device_lock
);
3290 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
3291 /* If there is a failed device being replaced,
3292 * we must be recovering.
3293 * else if we are after recovery_cp, we must be syncing
3294 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3295 * else we can only be replacing
3296 * sync and recovery both need to read all devices, and so
3297 * use the same flag.
3300 sh
->sector
>= conf
->mddev
->recovery_cp
||
3301 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
3309 static void handle_stripe(struct stripe_head
*sh
)
3311 struct stripe_head_state s
;
3312 struct r5conf
*conf
= sh
->raid_conf
;
3315 int disks
= sh
->disks
;
3316 struct r5dev
*pdev
, *qdev
;
3318 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3319 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
3320 /* already being handled, ensure it gets handled
3321 * again when current action finishes */
3322 set_bit(STRIPE_HANDLE
, &sh
->state
);
3326 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3327 set_bit(STRIPE_SYNCING
, &sh
->state
);
3328 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3330 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3332 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3333 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3334 (unsigned long long)sh
->sector
, sh
->state
,
3335 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
3336 sh
->check_state
, sh
->reconstruct_state
);
3338 analyse_stripe(sh
, &s
);
3340 if (s
.handle_bad_blocks
) {
3341 set_bit(STRIPE_HANDLE
, &sh
->state
);
3345 if (unlikely(s
.blocked_rdev
)) {
3346 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3347 s
.replacing
|| s
.to_write
|| s
.written
) {
3348 set_bit(STRIPE_HANDLE
, &sh
->state
);
3351 /* There is nothing for the blocked_rdev to block */
3352 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
3353 s
.blocked_rdev
= NULL
;
3356 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
3357 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
3358 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
3361 pr_debug("locked=%d uptodate=%d to_read=%d"
3362 " to_write=%d failed=%d failed_num=%d,%d\n",
3363 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3364 s
.failed_num
[0], s
.failed_num
[1]);
3365 /* check if the array has lost more than max_degraded devices and,
3366 * if so, some requests might need to be failed.
3368 if (s
.failed
> conf
->max_degraded
) {
3369 sh
->check_state
= 0;
3370 sh
->reconstruct_state
= 0;
3371 if (s
.to_read
+s
.to_write
+s
.written
)
3372 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
3373 if (s
.syncing
+ s
.replacing
)
3374 handle_failed_sync(conf
, sh
, &s
);
3378 * might be able to return some write requests if the parity blocks
3379 * are safe, or on a failed drive
3381 pdev
= &sh
->dev
[sh
->pd_idx
];
3382 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
3383 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
3384 qdev
= &sh
->dev
[sh
->qd_idx
];
3385 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
3386 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
3390 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3391 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3392 && test_bit(R5_UPTODATE
, &pdev
->flags
)))) &&
3393 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3394 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3395 && test_bit(R5_UPTODATE
, &qdev
->flags
)))))
3396 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
3398 /* Now we might consider reading some blocks, either to check/generate
3399 * parity, or to satisfy requests
3400 * or to load a block that is being partially written.
3402 if (s
.to_read
|| s
.non_overwrite
3403 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
3404 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
3407 handle_stripe_fill(sh
, &s
, disks
);
3409 /* Now we check to see if any write operations have recently
3413 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
3415 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
3416 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
3417 sh
->reconstruct_state
= reconstruct_state_idle
;
3419 /* All the 'written' buffers and the parity block are ready to
3420 * be written back to disk
3422 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
));
3423 BUG_ON(sh
->qd_idx
>= 0 &&
3424 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
));
3425 for (i
= disks
; i
--; ) {
3426 struct r5dev
*dev
= &sh
->dev
[i
];
3427 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
3428 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3430 pr_debug("Writing block %d\n", i
);
3431 set_bit(R5_Wantwrite
, &dev
->flags
);
3434 if (!test_bit(R5_Insync
, &dev
->flags
) ||
3435 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
3437 set_bit(STRIPE_INSYNC
, &sh
->state
);
3440 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3441 s
.dec_preread_active
= 1;
3444 /* Now to consider new write requests and what else, if anything
3445 * should be read. We do not handle new writes when:
3446 * 1/ A 'write' operation (copy+xor) is already in flight.
3447 * 2/ A 'check' operation is in flight, as it may clobber the parity
3450 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
3451 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
3453 /* maybe we need to check and possibly fix the parity for this stripe
3454 * Any reads will already have been scheduled, so we just see if enough
3455 * data is available. The parity check is held off while parity
3456 * dependent operations are in flight.
3458 if (sh
->check_state
||
3459 (s
.syncing
&& s
.locked
== 0 &&
3460 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3461 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
3462 if (conf
->level
== 6)
3463 handle_parity_checks6(conf
, sh
, &s
, disks
);
3465 handle_parity_checks5(conf
, sh
, &s
, disks
);
3468 if (s
.replacing
&& s
.locked
== 0
3469 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3470 /* Write out to replacement devices where possible */
3471 for (i
= 0; i
< conf
->raid_disks
; i
++)
3472 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
) &&
3473 test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
3474 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
3475 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3478 set_bit(STRIPE_INSYNC
, &sh
->state
);
3480 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
3481 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3482 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3483 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3486 /* If the failed drives are just a ReadError, then we might need
3487 * to progress the repair/check process
3489 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
3490 for (i
= 0; i
< s
.failed
; i
++) {
3491 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
3492 if (test_bit(R5_ReadError
, &dev
->flags
)
3493 && !test_bit(R5_LOCKED
, &dev
->flags
)
3494 && test_bit(R5_UPTODATE
, &dev
->flags
)
3496 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3497 set_bit(R5_Wantwrite
, &dev
->flags
);
3498 set_bit(R5_ReWrite
, &dev
->flags
);
3499 set_bit(R5_LOCKED
, &dev
->flags
);
3502 /* let's read it back */
3503 set_bit(R5_Wantread
, &dev
->flags
);
3504 set_bit(R5_LOCKED
, &dev
->flags
);
3511 /* Finish reconstruct operations initiated by the expansion process */
3512 if (sh
->reconstruct_state
== reconstruct_state_result
) {
3513 struct stripe_head
*sh_src
3514 = get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
3515 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
3516 /* sh cannot be written until sh_src has been read.
3517 * so arrange for sh to be delayed a little
3519 set_bit(STRIPE_DELAYED
, &sh
->state
);
3520 set_bit(STRIPE_HANDLE
, &sh
->state
);
3521 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
3523 atomic_inc(&conf
->preread_active_stripes
);
3524 release_stripe(sh_src
);
3528 release_stripe(sh_src
);
3530 sh
->reconstruct_state
= reconstruct_state_idle
;
3531 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3532 for (i
= conf
->raid_disks
; i
--; ) {
3533 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3534 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3539 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
3540 !sh
->reconstruct_state
) {
3541 /* Need to write out all blocks after computing parity */
3542 sh
->disks
= conf
->raid_disks
;
3543 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3544 schedule_reconstruction(sh
, &s
, 1, 1);
3545 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
3546 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3547 atomic_dec(&conf
->reshape_stripes
);
3548 wake_up(&conf
->wait_for_overlap
);
3549 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3552 if (s
.expanding
&& s
.locked
== 0 &&
3553 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3554 handle_stripe_expansion(conf
, sh
);
3557 /* wait for this device to become unblocked */
3558 if (conf
->mddev
->external
&& unlikely(s
.blocked_rdev
))
3559 md_wait_for_blocked_rdev(s
.blocked_rdev
, conf
->mddev
);
3561 if (s
.handle_bad_blocks
)
3562 for (i
= disks
; i
--; ) {
3563 struct md_rdev
*rdev
;
3564 struct r5dev
*dev
= &sh
->dev
[i
];
3565 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
3566 /* We own a safe reference to the rdev */
3567 rdev
= conf
->disks
[i
].rdev
;
3568 if (!rdev_set_badblocks(rdev
, sh
->sector
,
3570 md_error(conf
->mddev
, rdev
);
3571 rdev_dec_pending(rdev
, conf
->mddev
);
3573 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
3574 rdev
= conf
->disks
[i
].rdev
;
3575 rdev_clear_badblocks(rdev
, sh
->sector
,
3577 rdev_dec_pending(rdev
, conf
->mddev
);
3579 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3580 rdev
= conf
->disks
[i
].replacement
;
3582 /* rdev have been moved down */
3583 rdev
= conf
->disks
[i
].rdev
;
3584 rdev_clear_badblocks(rdev
, sh
->sector
,
3586 rdev_dec_pending(rdev
, conf
->mddev
);
3591 raid_run_ops(sh
, s
.ops_request
);
3595 if (s
.dec_preread_active
) {
3596 /* We delay this until after ops_run_io so that if make_request
3597 * is waiting on a flush, it won't continue until the writes
3598 * have actually been submitted.
3600 atomic_dec(&conf
->preread_active_stripes
);
3601 if (atomic_read(&conf
->preread_active_stripes
) <
3603 md_wakeup_thread(conf
->mddev
->thread
);
3606 return_io(s
.return_bi
);
3608 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
3611 static void raid5_activate_delayed(struct r5conf
*conf
)
3613 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
3614 while (!list_empty(&conf
->delayed_list
)) {
3615 struct list_head
*l
= conf
->delayed_list
.next
;
3616 struct stripe_head
*sh
;
3617 sh
= list_entry(l
, struct stripe_head
, lru
);
3619 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3620 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3621 atomic_inc(&conf
->preread_active_stripes
);
3622 list_add_tail(&sh
->lru
, &conf
->hold_list
);
3627 static void activate_bit_delay(struct r5conf
*conf
)
3629 /* device_lock is held */
3630 struct list_head head
;
3631 list_add(&head
, &conf
->bitmap_list
);
3632 list_del_init(&conf
->bitmap_list
);
3633 while (!list_empty(&head
)) {
3634 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
3635 list_del_init(&sh
->lru
);
3636 atomic_inc(&sh
->count
);
3637 __release_stripe(conf
, sh
);
3641 int md_raid5_congested(struct mddev
*mddev
, int bits
)
3643 struct r5conf
*conf
= mddev
->private;
3645 /* No difference between reads and writes. Just check
3646 * how busy the stripe_cache is
3649 if (conf
->inactive_blocked
)
3653 if (list_empty_careful(&conf
->inactive_list
))
3658 EXPORT_SYMBOL_GPL(md_raid5_congested
);
3660 static int raid5_congested(void *data
, int bits
)
3662 struct mddev
*mddev
= data
;
3664 return mddev_congested(mddev
, bits
) ||
3665 md_raid5_congested(mddev
, bits
);
3668 /* We want read requests to align with chunks where possible,
3669 * but write requests don't need to.
3671 static int raid5_mergeable_bvec(struct request_queue
*q
,
3672 struct bvec_merge_data
*bvm
,
3673 struct bio_vec
*biovec
)
3675 struct mddev
*mddev
= q
->queuedata
;
3676 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
3678 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3679 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
3681 if ((bvm
->bi_rw
& 1) == WRITE
)
3682 return biovec
->bv_len
; /* always allow writes to be mergeable */
3684 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3685 chunk_sectors
= mddev
->new_chunk_sectors
;
3686 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
3687 if (max
< 0) max
= 0;
3688 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
3689 return biovec
->bv_len
;
3695 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
3697 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
3698 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3699 unsigned int bio_sectors
= bio
->bi_size
>> 9;
3701 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3702 chunk_sectors
= mddev
->new_chunk_sectors
;
3703 return chunk_sectors
>=
3704 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
3708 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3709 * later sampled by raid5d.
3711 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
3713 unsigned long flags
;
3715 spin_lock_irqsave(&conf
->device_lock
, flags
);
3717 bi
->bi_next
= conf
->retry_read_aligned_list
;
3718 conf
->retry_read_aligned_list
= bi
;
3720 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3721 md_wakeup_thread(conf
->mddev
->thread
);
3725 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
3729 bi
= conf
->retry_read_aligned
;
3731 conf
->retry_read_aligned
= NULL
;
3734 bi
= conf
->retry_read_aligned_list
;
3736 conf
->retry_read_aligned_list
= bi
->bi_next
;
3739 * this sets the active strip count to 1 and the processed
3740 * strip count to zero (upper 8 bits)
3742 bi
->bi_phys_segments
= 1; /* biased count of active stripes */
3750 * The "raid5_align_endio" should check if the read succeeded and if it
3751 * did, call bio_endio on the original bio (having bio_put the new bio
3753 * If the read failed..
3755 static void raid5_align_endio(struct bio
*bi
, int error
)
3757 struct bio
* raid_bi
= bi
->bi_private
;
3758 struct mddev
*mddev
;
3759 struct r5conf
*conf
;
3760 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
3761 struct md_rdev
*rdev
;
3765 rdev
= (void*)raid_bi
->bi_next
;
3766 raid_bi
->bi_next
= NULL
;
3767 mddev
= rdev
->mddev
;
3768 conf
= mddev
->private;
3770 rdev_dec_pending(rdev
, conf
->mddev
);
3772 if (!error
&& uptodate
) {
3773 bio_endio(raid_bi
, 0);
3774 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
3775 wake_up(&conf
->wait_for_stripe
);
3780 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3782 add_bio_to_retry(raid_bi
, conf
);
3785 static int bio_fits_rdev(struct bio
*bi
)
3787 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
3789 if ((bi
->bi_size
>>9) > queue_max_sectors(q
))
3791 blk_recount_segments(q
, bi
);
3792 if (bi
->bi_phys_segments
> queue_max_segments(q
))
3795 if (q
->merge_bvec_fn
)
3796 /* it's too hard to apply the merge_bvec_fn at this stage,
3805 static int chunk_aligned_read(struct mddev
*mddev
, struct bio
* raid_bio
)
3807 struct r5conf
*conf
= mddev
->private;
3809 struct bio
* align_bi
;
3810 struct md_rdev
*rdev
;
3811 sector_t end_sector
;
3813 if (!in_chunk_boundary(mddev
, raid_bio
)) {
3814 pr_debug("chunk_aligned_read : non aligned\n");
3818 * use bio_clone_mddev to make a copy of the bio
3820 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
3824 * set bi_end_io to a new function, and set bi_private to the
3827 align_bi
->bi_end_io
= raid5_align_endio
;
3828 align_bi
->bi_private
= raid_bio
;
3832 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
3836 end_sector
= align_bi
->bi_sector
+ (align_bi
->bi_size
>> 9);
3838 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
3839 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
3840 rdev
->recovery_offset
< end_sector
) {
3841 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
3843 (test_bit(Faulty
, &rdev
->flags
) ||
3844 !(test_bit(In_sync
, &rdev
->flags
) ||
3845 rdev
->recovery_offset
>= end_sector
)))
3852 atomic_inc(&rdev
->nr_pending
);
3854 raid_bio
->bi_next
= (void*)rdev
;
3855 align_bi
->bi_bdev
= rdev
->bdev
;
3856 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
3858 if (!bio_fits_rdev(align_bi
) ||
3859 is_badblock(rdev
, align_bi
->bi_sector
, align_bi
->bi_size
>>9,
3860 &first_bad
, &bad_sectors
)) {
3861 /* too big in some way, or has a known bad block */
3863 rdev_dec_pending(rdev
, mddev
);
3867 /* No reshape active, so we can trust rdev->data_offset */
3868 align_bi
->bi_sector
+= rdev
->data_offset
;
3870 spin_lock_irq(&conf
->device_lock
);
3871 wait_event_lock_irq(conf
->wait_for_stripe
,
3873 conf
->device_lock
, /* nothing */);
3874 atomic_inc(&conf
->active_aligned_reads
);
3875 spin_unlock_irq(&conf
->device_lock
);
3877 generic_make_request(align_bi
);
3886 /* __get_priority_stripe - get the next stripe to process
3888 * Full stripe writes are allowed to pass preread active stripes up until
3889 * the bypass_threshold is exceeded. In general the bypass_count
3890 * increments when the handle_list is handled before the hold_list; however, it
3891 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3892 * stripe with in flight i/o. The bypass_count will be reset when the
3893 * head of the hold_list has changed, i.e. the head was promoted to the
3896 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
)
3898 struct stripe_head
*sh
;
3900 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3902 list_empty(&conf
->handle_list
) ? "empty" : "busy",
3903 list_empty(&conf
->hold_list
) ? "empty" : "busy",
3904 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
3906 if (!list_empty(&conf
->handle_list
)) {
3907 sh
= list_entry(conf
->handle_list
.next
, typeof(*sh
), lru
);
3909 if (list_empty(&conf
->hold_list
))
3910 conf
->bypass_count
= 0;
3911 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
3912 if (conf
->hold_list
.next
== conf
->last_hold
)
3913 conf
->bypass_count
++;
3915 conf
->last_hold
= conf
->hold_list
.next
;
3916 conf
->bypass_count
-= conf
->bypass_threshold
;
3917 if (conf
->bypass_count
< 0)
3918 conf
->bypass_count
= 0;
3921 } else if (!list_empty(&conf
->hold_list
) &&
3922 ((conf
->bypass_threshold
&&
3923 conf
->bypass_count
> conf
->bypass_threshold
) ||
3924 atomic_read(&conf
->pending_full_writes
) == 0)) {
3925 sh
= list_entry(conf
->hold_list
.next
,
3927 conf
->bypass_count
-= conf
->bypass_threshold
;
3928 if (conf
->bypass_count
< 0)
3929 conf
->bypass_count
= 0;
3933 list_del_init(&sh
->lru
);
3934 atomic_inc(&sh
->count
);
3935 BUG_ON(atomic_read(&sh
->count
) != 1);
3939 static void make_request(struct mddev
*mddev
, struct bio
* bi
)
3941 struct r5conf
*conf
= mddev
->private;
3943 sector_t new_sector
;
3944 sector_t logical_sector
, last_sector
;
3945 struct stripe_head
*sh
;
3946 const int rw
= bio_data_dir(bi
);
3950 if (unlikely(bi
->bi_rw
& REQ_FLUSH
)) {
3951 md_flush_request(mddev
, bi
);
3955 md_write_start(mddev
, bi
);
3958 mddev
->reshape_position
== MaxSector
&&
3959 chunk_aligned_read(mddev
,bi
))
3962 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
3963 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
3965 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
3967 plugged
= mddev_check_plugged(mddev
);
3968 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
3970 int disks
, data_disks
;
3975 disks
= conf
->raid_disks
;
3976 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
3977 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
3978 /* spinlock is needed as reshape_progress may be
3979 * 64bit on a 32bit platform, and so it might be
3980 * possible to see a half-updated value
3981 * Of course reshape_progress could change after
3982 * the lock is dropped, so once we get a reference
3983 * to the stripe that we think it is, we will have
3986 spin_lock_irq(&conf
->device_lock
);
3987 if (mddev
->delta_disks
< 0
3988 ? logical_sector
< conf
->reshape_progress
3989 : logical_sector
>= conf
->reshape_progress
) {
3990 disks
= conf
->previous_raid_disks
;
3993 if (mddev
->delta_disks
< 0
3994 ? logical_sector
< conf
->reshape_safe
3995 : logical_sector
>= conf
->reshape_safe
) {
3996 spin_unlock_irq(&conf
->device_lock
);
4001 spin_unlock_irq(&conf
->device_lock
);
4003 data_disks
= disks
- conf
->max_degraded
;
4005 new_sector
= raid5_compute_sector(conf
, logical_sector
,
4008 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4009 (unsigned long long)new_sector
,
4010 (unsigned long long)logical_sector
);
4012 sh
= get_active_stripe(conf
, new_sector
, previous
,
4013 (bi
->bi_rw
&RWA_MASK
), 0);
4015 if (unlikely(previous
)) {
4016 /* expansion might have moved on while waiting for a
4017 * stripe, so we must do the range check again.
4018 * Expansion could still move past after this
4019 * test, but as we are holding a reference to
4020 * 'sh', we know that if that happens,
4021 * STRIPE_EXPANDING will get set and the expansion
4022 * won't proceed until we finish with the stripe.
4025 spin_lock_irq(&conf
->device_lock
);
4026 if (mddev
->delta_disks
< 0
4027 ? logical_sector
>= conf
->reshape_progress
4028 : logical_sector
< conf
->reshape_progress
)
4029 /* mismatch, need to try again */
4031 spin_unlock_irq(&conf
->device_lock
);
4040 logical_sector
>= mddev
->suspend_lo
&&
4041 logical_sector
< mddev
->suspend_hi
) {
4043 /* As the suspend_* range is controlled by
4044 * userspace, we want an interruptible
4047 flush_signals(current
);
4048 prepare_to_wait(&conf
->wait_for_overlap
,
4049 &w
, TASK_INTERRUPTIBLE
);
4050 if (logical_sector
>= mddev
->suspend_lo
&&
4051 logical_sector
< mddev
->suspend_hi
)
4056 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
4057 !add_stripe_bio(sh
, bi
, dd_idx
, rw
)) {
4058 /* Stripe is busy expanding or
4059 * add failed due to overlap. Flush everything
4062 md_wakeup_thread(mddev
->thread
);
4067 finish_wait(&conf
->wait_for_overlap
, &w
);
4068 set_bit(STRIPE_HANDLE
, &sh
->state
);
4069 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4070 if ((bi
->bi_rw
& REQ_SYNC
) &&
4071 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4072 atomic_inc(&conf
->preread_active_stripes
);
4075 /* cannot get stripe for read-ahead, just give-up */
4076 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4077 finish_wait(&conf
->wait_for_overlap
, &w
);
4083 md_wakeup_thread(mddev
->thread
);
4085 spin_lock_irq(&conf
->device_lock
);
4086 remaining
= raid5_dec_bi_phys_segments(bi
);
4087 spin_unlock_irq(&conf
->device_lock
);
4088 if (remaining
== 0) {
4091 md_write_end(mddev
);
4097 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
4099 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
4101 /* reshaping is quite different to recovery/resync so it is
4102 * handled quite separately ... here.
4104 * On each call to sync_request, we gather one chunk worth of
4105 * destination stripes and flag them as expanding.
4106 * Then we find all the source stripes and request reads.
4107 * As the reads complete, handle_stripe will copy the data
4108 * into the destination stripe and release that stripe.
4110 struct r5conf
*conf
= mddev
->private;
4111 struct stripe_head
*sh
;
4112 sector_t first_sector
, last_sector
;
4113 int raid_disks
= conf
->previous_raid_disks
;
4114 int data_disks
= raid_disks
- conf
->max_degraded
;
4115 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4118 sector_t writepos
, readpos
, safepos
;
4119 sector_t stripe_addr
;
4120 int reshape_sectors
;
4121 struct list_head stripes
;
4123 if (sector_nr
== 0) {
4124 /* If restarting in the middle, skip the initial sectors */
4125 if (mddev
->delta_disks
< 0 &&
4126 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
4127 sector_nr
= raid5_size(mddev
, 0, 0)
4128 - conf
->reshape_progress
;
4129 } else if (mddev
->delta_disks
>= 0 &&
4130 conf
->reshape_progress
> 0)
4131 sector_nr
= conf
->reshape_progress
;
4132 sector_div(sector_nr
, new_data_disks
);
4134 mddev
->curr_resync_completed
= sector_nr
;
4135 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4141 /* We need to process a full chunk at a time.
4142 * If old and new chunk sizes differ, we need to process the
4145 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
)
4146 reshape_sectors
= mddev
->new_chunk_sectors
;
4148 reshape_sectors
= mddev
->chunk_sectors
;
4150 /* we update the metadata when there is more than 3Meg
4151 * in the block range (that is rather arbitrary, should
4152 * probably be time based) or when the data about to be
4153 * copied would over-write the source of the data at
4154 * the front of the range.
4155 * i.e. one new_stripe along from reshape_progress new_maps
4156 * to after where reshape_safe old_maps to
4158 writepos
= conf
->reshape_progress
;
4159 sector_div(writepos
, new_data_disks
);
4160 readpos
= conf
->reshape_progress
;
4161 sector_div(readpos
, data_disks
);
4162 safepos
= conf
->reshape_safe
;
4163 sector_div(safepos
, data_disks
);
4164 if (mddev
->delta_disks
< 0) {
4165 writepos
-= min_t(sector_t
, reshape_sectors
, writepos
);
4166 readpos
+= reshape_sectors
;
4167 safepos
+= reshape_sectors
;
4169 writepos
+= reshape_sectors
;
4170 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
4171 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
4174 /* 'writepos' is the most advanced device address we might write.
4175 * 'readpos' is the least advanced device address we might read.
4176 * 'safepos' is the least address recorded in the metadata as having
4178 * If 'readpos' is behind 'writepos', then there is no way that we can
4179 * ensure safety in the face of a crash - that must be done by userspace
4180 * making a backup of the data. So in that case there is no particular
4181 * rush to update metadata.
4182 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4183 * update the metadata to advance 'safepos' to match 'readpos' so that
4184 * we can be safe in the event of a crash.
4185 * So we insist on updating metadata if safepos is behind writepos and
4186 * readpos is beyond writepos.
4187 * In any case, update the metadata every 10 seconds.
4188 * Maybe that number should be configurable, but I'm not sure it is
4189 * worth it.... maybe it could be a multiple of safemode_delay???
4191 if ((mddev
->delta_disks
< 0
4192 ? (safepos
> writepos
&& readpos
< writepos
)
4193 : (safepos
< writepos
&& readpos
> writepos
)) ||
4194 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4195 /* Cannot proceed until we've updated the superblock... */
4196 wait_event(conf
->wait_for_overlap
,
4197 atomic_read(&conf
->reshape_stripes
)==0);
4198 mddev
->reshape_position
= conf
->reshape_progress
;
4199 mddev
->curr_resync_completed
= sector_nr
;
4200 conf
->reshape_checkpoint
= jiffies
;
4201 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4202 md_wakeup_thread(mddev
->thread
);
4203 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4204 kthread_should_stop());
4205 spin_lock_irq(&conf
->device_lock
);
4206 conf
->reshape_safe
= mddev
->reshape_position
;
4207 spin_unlock_irq(&conf
->device_lock
);
4208 wake_up(&conf
->wait_for_overlap
);
4209 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4212 if (mddev
->delta_disks
< 0) {
4213 BUG_ON(conf
->reshape_progress
== 0);
4214 stripe_addr
= writepos
;
4215 BUG_ON((mddev
->dev_sectors
&
4216 ~((sector_t
)reshape_sectors
- 1))
4217 - reshape_sectors
- stripe_addr
4220 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
4221 stripe_addr
= sector_nr
;
4223 INIT_LIST_HEAD(&stripes
);
4224 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
4226 int skipped_disk
= 0;
4227 sh
= get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
4228 set_bit(STRIPE_EXPANDING
, &sh
->state
);
4229 atomic_inc(&conf
->reshape_stripes
);
4230 /* If any of this stripe is beyond the end of the old
4231 * array, then we need to zero those blocks
4233 for (j
=sh
->disks
; j
--;) {
4235 if (j
== sh
->pd_idx
)
4237 if (conf
->level
== 6 &&
4240 s
= compute_blocknr(sh
, j
, 0);
4241 if (s
< raid5_size(mddev
, 0, 0)) {
4245 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
4246 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
4247 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
4249 if (!skipped_disk
) {
4250 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4251 set_bit(STRIPE_HANDLE
, &sh
->state
);
4253 list_add(&sh
->lru
, &stripes
);
4255 spin_lock_irq(&conf
->device_lock
);
4256 if (mddev
->delta_disks
< 0)
4257 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
4259 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
4260 spin_unlock_irq(&conf
->device_lock
);
4261 /* Ok, those stripe are ready. We can start scheduling
4262 * reads on the source stripes.
4263 * The source stripes are determined by mapping the first and last
4264 * block on the destination stripes.
4267 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
4270 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
4271 * new_data_disks
- 1),
4273 if (last_sector
>= mddev
->dev_sectors
)
4274 last_sector
= mddev
->dev_sectors
- 1;
4275 while (first_sector
<= last_sector
) {
4276 sh
= get_active_stripe(conf
, first_sector
, 1, 0, 1);
4277 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4278 set_bit(STRIPE_HANDLE
, &sh
->state
);
4280 first_sector
+= STRIPE_SECTORS
;
4282 /* Now that the sources are clearly marked, we can release
4283 * the destination stripes
4285 while (!list_empty(&stripes
)) {
4286 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
4287 list_del_init(&sh
->lru
);
4290 /* If this takes us to the resync_max point where we have to pause,
4291 * then we need to write out the superblock.
4293 sector_nr
+= reshape_sectors
;
4294 if ((sector_nr
- mddev
->curr_resync_completed
) * 2
4295 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
4296 /* Cannot proceed until we've updated the superblock... */
4297 wait_event(conf
->wait_for_overlap
,
4298 atomic_read(&conf
->reshape_stripes
) == 0);
4299 mddev
->reshape_position
= conf
->reshape_progress
;
4300 mddev
->curr_resync_completed
= sector_nr
;
4301 conf
->reshape_checkpoint
= jiffies
;
4302 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4303 md_wakeup_thread(mddev
->thread
);
4304 wait_event(mddev
->sb_wait
,
4305 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
4306 || kthread_should_stop());
4307 spin_lock_irq(&conf
->device_lock
);
4308 conf
->reshape_safe
= mddev
->reshape_position
;
4309 spin_unlock_irq(&conf
->device_lock
);
4310 wake_up(&conf
->wait_for_overlap
);
4311 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4313 return reshape_sectors
;
4316 /* FIXME go_faster isn't used */
4317 static inline sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
4319 struct r5conf
*conf
= mddev
->private;
4320 struct stripe_head
*sh
;
4321 sector_t max_sector
= mddev
->dev_sectors
;
4322 sector_t sync_blocks
;
4323 int still_degraded
= 0;
4326 if (sector_nr
>= max_sector
) {
4327 /* just being told to finish up .. nothing much to do */
4329 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
4334 if (mddev
->curr_resync
< max_sector
) /* aborted */
4335 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
4337 else /* completed sync */
4339 bitmap_close_sync(mddev
->bitmap
);
4344 /* Allow raid5_quiesce to complete */
4345 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
4347 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4348 return reshape_request(mddev
, sector_nr
, skipped
);
4350 /* No need to check resync_max as we never do more than one
4351 * stripe, and as resync_max will always be on a chunk boundary,
4352 * if the check in md_do_sync didn't fire, there is no chance
4353 * of overstepping resync_max here
4356 /* if there is too many failed drives and we are trying
4357 * to resync, then assert that we are finished, because there is
4358 * nothing we can do.
4360 if (mddev
->degraded
>= conf
->max_degraded
&&
4361 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4362 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
4366 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
4367 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
4368 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
4369 /* we can skip this block, and probably more */
4370 sync_blocks
/= STRIPE_SECTORS
;
4372 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
4375 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
4377 sh
= get_active_stripe(conf
, sector_nr
, 0, 1, 0);
4379 sh
= get_active_stripe(conf
, sector_nr
, 0, 0, 0);
4380 /* make sure we don't swamp the stripe cache if someone else
4381 * is trying to get access
4383 schedule_timeout_uninterruptible(1);
4385 /* Need to check if array will still be degraded after recovery/resync
4386 * We don't need to check the 'failed' flag as when that gets set,
4389 for (i
= 0; i
< conf
->raid_disks
; i
++)
4390 if (conf
->disks
[i
].rdev
== NULL
)
4393 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
4395 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
4400 return STRIPE_SECTORS
;
4403 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
4405 /* We may not be able to submit a whole bio at once as there
4406 * may not be enough stripe_heads available.
4407 * We cannot pre-allocate enough stripe_heads as we may need
4408 * more than exist in the cache (if we allow ever large chunks).
4409 * So we do one stripe head at a time and record in
4410 * ->bi_hw_segments how many have been done.
4412 * We *know* that this entire raid_bio is in one chunk, so
4413 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4415 struct stripe_head
*sh
;
4417 sector_t sector
, logical_sector
, last_sector
;
4422 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4423 sector
= raid5_compute_sector(conf
, logical_sector
,
4425 last_sector
= raid_bio
->bi_sector
+ (raid_bio
->bi_size
>>9);
4427 for (; logical_sector
< last_sector
;
4428 logical_sector
+= STRIPE_SECTORS
,
4429 sector
+= STRIPE_SECTORS
,
4432 if (scnt
< raid5_bi_hw_segments(raid_bio
))
4433 /* already done this stripe */
4436 sh
= get_active_stripe(conf
, sector
, 0, 1, 0);
4439 /* failed to get a stripe - must wait */
4440 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4441 conf
->retry_read_aligned
= raid_bio
;
4445 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
4447 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4448 conf
->retry_read_aligned
= raid_bio
;
4456 spin_lock_irq(&conf
->device_lock
);
4457 remaining
= raid5_dec_bi_phys_segments(raid_bio
);
4458 spin_unlock_irq(&conf
->device_lock
);
4460 bio_endio(raid_bio
, 0);
4461 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4462 wake_up(&conf
->wait_for_stripe
);
4468 * This is our raid5 kernel thread.
4470 * We scan the hash table for stripes which can be handled now.
4471 * During the scan, completed stripes are saved for us by the interrupt
4472 * handler, so that they will not have to wait for our next wakeup.
4474 static void raid5d(struct mddev
*mddev
)
4476 struct stripe_head
*sh
;
4477 struct r5conf
*conf
= mddev
->private;
4479 struct blk_plug plug
;
4481 pr_debug("+++ raid5d active\n");
4483 md_check_recovery(mddev
);
4485 blk_start_plug(&plug
);
4487 spin_lock_irq(&conf
->device_lock
);
4491 if (atomic_read(&mddev
->plug_cnt
) == 0 &&
4492 !list_empty(&conf
->bitmap_list
)) {
4493 /* Now is a good time to flush some bitmap updates */
4495 spin_unlock_irq(&conf
->device_lock
);
4496 bitmap_unplug(mddev
->bitmap
);
4497 spin_lock_irq(&conf
->device_lock
);
4498 conf
->seq_write
= conf
->seq_flush
;
4499 activate_bit_delay(conf
);
4501 if (atomic_read(&mddev
->plug_cnt
) == 0)
4502 raid5_activate_delayed(conf
);
4504 while ((bio
= remove_bio_from_retry(conf
))) {
4506 spin_unlock_irq(&conf
->device_lock
);
4507 ok
= retry_aligned_read(conf
, bio
);
4508 spin_lock_irq(&conf
->device_lock
);
4514 sh
= __get_priority_stripe(conf
);
4518 spin_unlock_irq(&conf
->device_lock
);
4525 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
4526 md_check_recovery(mddev
);
4528 spin_lock_irq(&conf
->device_lock
);
4530 pr_debug("%d stripes handled\n", handled
);
4532 spin_unlock_irq(&conf
->device_lock
);
4534 async_tx_issue_pending_all();
4535 blk_finish_plug(&plug
);
4537 pr_debug("--- raid5d inactive\n");
4541 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
4543 struct r5conf
*conf
= mddev
->private;
4545 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
4551 raid5_set_cache_size(struct mddev
*mddev
, int size
)
4553 struct r5conf
*conf
= mddev
->private;
4556 if (size
<= 16 || size
> 32768)
4558 while (size
< conf
->max_nr_stripes
) {
4559 if (drop_one_stripe(conf
))
4560 conf
->max_nr_stripes
--;
4564 err
= md_allow_write(mddev
);
4567 while (size
> conf
->max_nr_stripes
) {
4568 if (grow_one_stripe(conf
))
4569 conf
->max_nr_stripes
++;
4574 EXPORT_SYMBOL(raid5_set_cache_size
);
4577 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
4579 struct r5conf
*conf
= mddev
->private;
4583 if (len
>= PAGE_SIZE
)
4588 if (strict_strtoul(page
, 10, &new))
4590 err
= raid5_set_cache_size(mddev
, new);
4596 static struct md_sysfs_entry
4597 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
4598 raid5_show_stripe_cache_size
,
4599 raid5_store_stripe_cache_size
);
4602 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
4604 struct r5conf
*conf
= mddev
->private;
4606 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
4612 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
4614 struct r5conf
*conf
= mddev
->private;
4616 if (len
>= PAGE_SIZE
)
4621 if (strict_strtoul(page
, 10, &new))
4623 if (new > conf
->max_nr_stripes
)
4625 conf
->bypass_threshold
= new;
4629 static struct md_sysfs_entry
4630 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
4632 raid5_show_preread_threshold
,
4633 raid5_store_preread_threshold
);
4636 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
4638 struct r5conf
*conf
= mddev
->private;
4640 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
4645 static struct md_sysfs_entry
4646 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
4648 static struct attribute
*raid5_attrs
[] = {
4649 &raid5_stripecache_size
.attr
,
4650 &raid5_stripecache_active
.attr
,
4651 &raid5_preread_bypass_threshold
.attr
,
4654 static struct attribute_group raid5_attrs_group
= {
4656 .attrs
= raid5_attrs
,
4660 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
4662 struct r5conf
*conf
= mddev
->private;
4665 sectors
= mddev
->dev_sectors
;
4667 /* size is defined by the smallest of previous and new size */
4668 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
4670 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
4671 sectors
&= ~((sector_t
)mddev
->new_chunk_sectors
- 1);
4672 return sectors
* (raid_disks
- conf
->max_degraded
);
4675 static void raid5_free_percpu(struct r5conf
*conf
)
4677 struct raid5_percpu
*percpu
;
4684 for_each_possible_cpu(cpu
) {
4685 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4686 safe_put_page(percpu
->spare_page
);
4687 kfree(percpu
->scribble
);
4689 #ifdef CONFIG_HOTPLUG_CPU
4690 unregister_cpu_notifier(&conf
->cpu_notify
);
4694 free_percpu(conf
->percpu
);
4697 static void free_conf(struct r5conf
*conf
)
4699 shrink_stripes(conf
);
4700 raid5_free_percpu(conf
);
4702 kfree(conf
->stripe_hashtbl
);
4706 #ifdef CONFIG_HOTPLUG_CPU
4707 static int raid456_cpu_notify(struct notifier_block
*nfb
, unsigned long action
,
4710 struct r5conf
*conf
= container_of(nfb
, struct r5conf
, cpu_notify
);
4711 long cpu
= (long)hcpu
;
4712 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4715 case CPU_UP_PREPARE
:
4716 case CPU_UP_PREPARE_FROZEN
:
4717 if (conf
->level
== 6 && !percpu
->spare_page
)
4718 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
4719 if (!percpu
->scribble
)
4720 percpu
->scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4722 if (!percpu
->scribble
||
4723 (conf
->level
== 6 && !percpu
->spare_page
)) {
4724 safe_put_page(percpu
->spare_page
);
4725 kfree(percpu
->scribble
);
4726 pr_err("%s: failed memory allocation for cpu%ld\n",
4728 return notifier_from_errno(-ENOMEM
);
4732 case CPU_DEAD_FROZEN
:
4733 safe_put_page(percpu
->spare_page
);
4734 kfree(percpu
->scribble
);
4735 percpu
->spare_page
= NULL
;
4736 percpu
->scribble
= NULL
;
4745 static int raid5_alloc_percpu(struct r5conf
*conf
)
4748 struct page
*spare_page
;
4749 struct raid5_percpu __percpu
*allcpus
;
4753 allcpus
= alloc_percpu(struct raid5_percpu
);
4756 conf
->percpu
= allcpus
;
4760 for_each_present_cpu(cpu
) {
4761 if (conf
->level
== 6) {
4762 spare_page
= alloc_page(GFP_KERNEL
);
4767 per_cpu_ptr(conf
->percpu
, cpu
)->spare_page
= spare_page
;
4769 scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4774 per_cpu_ptr(conf
->percpu
, cpu
)->scribble
= scribble
;
4776 #ifdef CONFIG_HOTPLUG_CPU
4777 conf
->cpu_notify
.notifier_call
= raid456_cpu_notify
;
4778 conf
->cpu_notify
.priority
= 0;
4780 err
= register_cpu_notifier(&conf
->cpu_notify
);
4787 static struct r5conf
*setup_conf(struct mddev
*mddev
)
4789 struct r5conf
*conf
;
4790 int raid_disk
, memory
, max_disks
;
4791 struct md_rdev
*rdev
;
4792 struct disk_info
*disk
;
4794 if (mddev
->new_level
!= 5
4795 && mddev
->new_level
!= 4
4796 && mddev
->new_level
!= 6) {
4797 printk(KERN_ERR
"md/raid:%s: raid level not set to 4/5/6 (%d)\n",
4798 mdname(mddev
), mddev
->new_level
);
4799 return ERR_PTR(-EIO
);
4801 if ((mddev
->new_level
== 5
4802 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
4803 (mddev
->new_level
== 6
4804 && !algorithm_valid_raid6(mddev
->new_layout
))) {
4805 printk(KERN_ERR
"md/raid:%s: layout %d not supported\n",
4806 mdname(mddev
), mddev
->new_layout
);
4807 return ERR_PTR(-EIO
);
4809 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
4810 printk(KERN_ERR
"md/raid:%s: not enough configured devices (%d, minimum 4)\n",
4811 mdname(mddev
), mddev
->raid_disks
);
4812 return ERR_PTR(-EINVAL
);
4815 if (!mddev
->new_chunk_sectors
||
4816 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
4817 !is_power_of_2(mddev
->new_chunk_sectors
)) {
4818 printk(KERN_ERR
"md/raid:%s: invalid chunk size %d\n",
4819 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
4820 return ERR_PTR(-EINVAL
);
4823 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
4826 spin_lock_init(&conf
->device_lock
);
4827 init_waitqueue_head(&conf
->wait_for_stripe
);
4828 init_waitqueue_head(&conf
->wait_for_overlap
);
4829 INIT_LIST_HEAD(&conf
->handle_list
);
4830 INIT_LIST_HEAD(&conf
->hold_list
);
4831 INIT_LIST_HEAD(&conf
->delayed_list
);
4832 INIT_LIST_HEAD(&conf
->bitmap_list
);
4833 INIT_LIST_HEAD(&conf
->inactive_list
);
4834 atomic_set(&conf
->active_stripes
, 0);
4835 atomic_set(&conf
->preread_active_stripes
, 0);
4836 atomic_set(&conf
->active_aligned_reads
, 0);
4837 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
4838 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
4840 conf
->raid_disks
= mddev
->raid_disks
;
4841 if (mddev
->reshape_position
== MaxSector
)
4842 conf
->previous_raid_disks
= mddev
->raid_disks
;
4844 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4845 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
4846 conf
->scribble_len
= scribble_len(max_disks
);
4848 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
4853 conf
->mddev
= mddev
;
4855 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
4858 conf
->level
= mddev
->new_level
;
4859 if (raid5_alloc_percpu(conf
) != 0)
4862 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
4864 rdev_for_each(rdev
, mddev
) {
4865 raid_disk
= rdev
->raid_disk
;
4866 if (raid_disk
>= max_disks
4869 disk
= conf
->disks
+ raid_disk
;
4871 if (test_bit(Replacement
, &rdev
->flags
)) {
4872 if (disk
->replacement
)
4874 disk
->replacement
= rdev
;
4881 if (test_bit(In_sync
, &rdev
->flags
)) {
4882 char b
[BDEVNAME_SIZE
];
4883 printk(KERN_INFO
"md/raid:%s: device %s operational as raid"
4885 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
4886 } else if (rdev
->saved_raid_disk
!= raid_disk
)
4887 /* Cannot rely on bitmap to complete recovery */
4891 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
4892 conf
->level
= mddev
->new_level
;
4893 if (conf
->level
== 6)
4894 conf
->max_degraded
= 2;
4896 conf
->max_degraded
= 1;
4897 conf
->algorithm
= mddev
->new_layout
;
4898 conf
->max_nr_stripes
= NR_STRIPES
;
4899 conf
->reshape_progress
= mddev
->reshape_position
;
4900 if (conf
->reshape_progress
!= MaxSector
) {
4901 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
4902 conf
->prev_algo
= mddev
->layout
;
4905 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
4906 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
4907 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
4909 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4910 mdname(mddev
), memory
);
4913 printk(KERN_INFO
"md/raid:%s: allocated %dkB\n",
4914 mdname(mddev
), memory
);
4916 conf
->thread
= md_register_thread(raid5d
, mddev
, NULL
);
4917 if (!conf
->thread
) {
4919 "md/raid:%s: couldn't allocate thread.\n",
4929 return ERR_PTR(-EIO
);
4931 return ERR_PTR(-ENOMEM
);
4935 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
4938 case ALGORITHM_PARITY_0
:
4939 if (raid_disk
< max_degraded
)
4942 case ALGORITHM_PARITY_N
:
4943 if (raid_disk
>= raid_disks
- max_degraded
)
4946 case ALGORITHM_PARITY_0_6
:
4947 if (raid_disk
== 0 ||
4948 raid_disk
== raid_disks
- 1)
4951 case ALGORITHM_LEFT_ASYMMETRIC_6
:
4952 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
4953 case ALGORITHM_LEFT_SYMMETRIC_6
:
4954 case ALGORITHM_RIGHT_SYMMETRIC_6
:
4955 if (raid_disk
== raid_disks
- 1)
4961 static int run(struct mddev
*mddev
)
4963 struct r5conf
*conf
;
4964 int working_disks
= 0;
4965 int dirty_parity_disks
= 0;
4966 struct md_rdev
*rdev
;
4967 sector_t reshape_offset
= 0;
4970 if (mddev
->recovery_cp
!= MaxSector
)
4971 printk(KERN_NOTICE
"md/raid:%s: not clean"
4972 " -- starting background reconstruction\n",
4974 if (mddev
->reshape_position
!= MaxSector
) {
4975 /* Check that we can continue the reshape.
4976 * Currently only disks can change, it must
4977 * increase, and we must be past the point where
4978 * a stripe over-writes itself
4980 sector_t here_new
, here_old
;
4982 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
4984 if (mddev
->new_level
!= mddev
->level
) {
4985 printk(KERN_ERR
"md/raid:%s: unsupported reshape "
4986 "required - aborting.\n",
4990 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4991 /* reshape_position must be on a new-stripe boundary, and one
4992 * further up in new geometry must map after here in old
4995 here_new
= mddev
->reshape_position
;
4996 if (sector_div(here_new
, mddev
->new_chunk_sectors
*
4997 (mddev
->raid_disks
- max_degraded
))) {
4998 printk(KERN_ERR
"md/raid:%s: reshape_position not "
4999 "on a stripe boundary\n", mdname(mddev
));
5002 reshape_offset
= here_new
* mddev
->new_chunk_sectors
;
5003 /* here_new is the stripe we will write to */
5004 here_old
= mddev
->reshape_position
;
5005 sector_div(here_old
, mddev
->chunk_sectors
*
5006 (old_disks
-max_degraded
));
5007 /* here_old is the first stripe that we might need to read
5009 if (mddev
->delta_disks
== 0) {
5010 /* We cannot be sure it is safe to start an in-place
5011 * reshape. It is only safe if user-space if monitoring
5012 * and taking constant backups.
5013 * mdadm always starts a situation like this in
5014 * readonly mode so it can take control before
5015 * allowing any writes. So just check for that.
5017 if ((here_new
* mddev
->new_chunk_sectors
!=
5018 here_old
* mddev
->chunk_sectors
) ||
5020 printk(KERN_ERR
"md/raid:%s: in-place reshape must be started"
5021 " in read-only mode - aborting\n",
5025 } else if (mddev
->delta_disks
< 0
5026 ? (here_new
* mddev
->new_chunk_sectors
<=
5027 here_old
* mddev
->chunk_sectors
)
5028 : (here_new
* mddev
->new_chunk_sectors
>=
5029 here_old
* mddev
->chunk_sectors
)) {
5030 /* Reading from the same stripe as writing to - bad */
5031 printk(KERN_ERR
"md/raid:%s: reshape_position too early for "
5032 "auto-recovery - aborting.\n",
5036 printk(KERN_INFO
"md/raid:%s: reshape will continue\n",
5038 /* OK, we should be able to continue; */
5040 BUG_ON(mddev
->level
!= mddev
->new_level
);
5041 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
5042 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
5043 BUG_ON(mddev
->delta_disks
!= 0);
5046 if (mddev
->private == NULL
)
5047 conf
= setup_conf(mddev
);
5049 conf
= mddev
->private;
5052 return PTR_ERR(conf
);
5054 mddev
->thread
= conf
->thread
;
5055 conf
->thread
= NULL
;
5056 mddev
->private = conf
;
5058 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
5060 rdev
= conf
->disks
[i
].rdev
;
5061 if (!rdev
&& conf
->disks
[i
].replacement
) {
5062 /* The replacement is all we have yet */
5063 rdev
= conf
->disks
[i
].replacement
;
5064 conf
->disks
[i
].replacement
= NULL
;
5065 clear_bit(Replacement
, &rdev
->flags
);
5066 conf
->disks
[i
].rdev
= rdev
;
5070 if (conf
->disks
[i
].replacement
&&
5071 conf
->reshape_progress
!= MaxSector
) {
5072 /* replacements and reshape simply do not mix. */
5073 printk(KERN_ERR
"md: cannot handle concurrent "
5074 "replacement and reshape.\n");
5077 if (test_bit(In_sync
, &rdev
->flags
)) {
5081 /* This disc is not fully in-sync. However if it
5082 * just stored parity (beyond the recovery_offset),
5083 * when we don't need to be concerned about the
5084 * array being dirty.
5085 * When reshape goes 'backwards', we never have
5086 * partially completed devices, so we only need
5087 * to worry about reshape going forwards.
5089 /* Hack because v0.91 doesn't store recovery_offset properly. */
5090 if (mddev
->major_version
== 0 &&
5091 mddev
->minor_version
> 90)
5092 rdev
->recovery_offset
= reshape_offset
;
5094 if (rdev
->recovery_offset
< reshape_offset
) {
5095 /* We need to check old and new layout */
5096 if (!only_parity(rdev
->raid_disk
,
5099 conf
->max_degraded
))
5102 if (!only_parity(rdev
->raid_disk
,
5104 conf
->previous_raid_disks
,
5105 conf
->max_degraded
))
5107 dirty_parity_disks
++;
5111 * 0 for a fully functional array, 1 or 2 for a degraded array.
5113 mddev
->degraded
= calc_degraded(conf
);
5115 if (has_failed(conf
)) {
5116 printk(KERN_ERR
"md/raid:%s: not enough operational devices"
5117 " (%d/%d failed)\n",
5118 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
5122 /* device size must be a multiple of chunk size */
5123 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
5124 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5126 if (mddev
->degraded
> dirty_parity_disks
&&
5127 mddev
->recovery_cp
!= MaxSector
) {
5128 if (mddev
->ok_start_degraded
)
5130 "md/raid:%s: starting dirty degraded array"
5131 " - data corruption possible.\n",
5135 "md/raid:%s: cannot start dirty degraded array.\n",
5141 if (mddev
->degraded
== 0)
5142 printk(KERN_INFO
"md/raid:%s: raid level %d active with %d out of %d"
5143 " devices, algorithm %d\n", mdname(mddev
), conf
->level
,
5144 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
5147 printk(KERN_ALERT
"md/raid:%s: raid level %d active with %d"
5148 " out of %d devices, algorithm %d\n",
5149 mdname(mddev
), conf
->level
,
5150 mddev
->raid_disks
- mddev
->degraded
,
5151 mddev
->raid_disks
, mddev
->new_layout
);
5153 print_raid5_conf(conf
);
5155 if (conf
->reshape_progress
!= MaxSector
) {
5156 conf
->reshape_safe
= conf
->reshape_progress
;
5157 atomic_set(&conf
->reshape_stripes
, 0);
5158 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5159 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5160 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5161 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5162 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5167 /* Ok, everything is just fine now */
5168 if (mddev
->to_remove
== &raid5_attrs_group
)
5169 mddev
->to_remove
= NULL
;
5170 else if (mddev
->kobj
.sd
&&
5171 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
5173 "raid5: failed to create sysfs attributes for %s\n",
5175 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5179 /* read-ahead size must cover two whole stripes, which
5180 * is 2 * (datadisks) * chunksize where 'n' is the
5181 * number of raid devices
5183 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
5184 int stripe
= data_disks
*
5185 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
5186 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5187 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5189 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
5191 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5192 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
5194 chunk_size
= mddev
->chunk_sectors
<< 9;
5195 blk_queue_io_min(mddev
->queue
, chunk_size
);
5196 blk_queue_io_opt(mddev
->queue
, chunk_size
*
5197 (conf
->raid_disks
- conf
->max_degraded
));
5199 rdev_for_each(rdev
, mddev
)
5200 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5201 rdev
->data_offset
<< 9);
5206 md_unregister_thread(&mddev
->thread
);
5207 print_raid5_conf(conf
);
5209 mddev
->private = NULL
;
5210 printk(KERN_ALERT
"md/raid:%s: failed to run raid set.\n", mdname(mddev
));
5214 static int stop(struct mddev
*mddev
)
5216 struct r5conf
*conf
= mddev
->private;
5218 md_unregister_thread(&mddev
->thread
);
5220 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5222 mddev
->private = NULL
;
5223 mddev
->to_remove
= &raid5_attrs_group
;
5227 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
5229 struct r5conf
*conf
= mddev
->private;
5232 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
5233 mddev
->chunk_sectors
/ 2, mddev
->layout
);
5234 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
5235 for (i
= 0; i
< conf
->raid_disks
; i
++)
5236 seq_printf (seq
, "%s",
5237 conf
->disks
[i
].rdev
&&
5238 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
5239 seq_printf (seq
, "]");
5242 static void print_raid5_conf (struct r5conf
*conf
)
5245 struct disk_info
*tmp
;
5247 printk(KERN_DEBUG
"RAID conf printout:\n");
5249 printk("(conf==NULL)\n");
5252 printk(KERN_DEBUG
" --- level:%d rd:%d wd:%d\n", conf
->level
,
5254 conf
->raid_disks
- conf
->mddev
->degraded
);
5256 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5257 char b
[BDEVNAME_SIZE
];
5258 tmp
= conf
->disks
+ i
;
5260 printk(KERN_DEBUG
" disk %d, o:%d, dev:%s\n",
5261 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
5262 bdevname(tmp
->rdev
->bdev
, b
));
5266 static int raid5_spare_active(struct mddev
*mddev
)
5269 struct r5conf
*conf
= mddev
->private;
5270 struct disk_info
*tmp
;
5272 unsigned long flags
;
5274 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5275 tmp
= conf
->disks
+ i
;
5276 if (tmp
->replacement
5277 && tmp
->replacement
->recovery_offset
== MaxSector
5278 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
5279 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
5280 /* Replacement has just become active. */
5282 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
5285 /* Replaced device not technically faulty,
5286 * but we need to be sure it gets removed
5287 * and never re-added.
5289 set_bit(Faulty
, &tmp
->rdev
->flags
);
5290 sysfs_notify_dirent_safe(
5291 tmp
->rdev
->sysfs_state
);
5293 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
5294 } else if (tmp
->rdev
5295 && tmp
->rdev
->recovery_offset
== MaxSector
5296 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
5297 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
5299 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
5302 spin_lock_irqsave(&conf
->device_lock
, flags
);
5303 mddev
->degraded
= calc_degraded(conf
);
5304 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5305 print_raid5_conf(conf
);
5309 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5311 struct r5conf
*conf
= mddev
->private;
5313 int number
= rdev
->raid_disk
;
5314 struct md_rdev
**rdevp
;
5315 struct disk_info
*p
= conf
->disks
+ number
;
5317 print_raid5_conf(conf
);
5318 if (rdev
== p
->rdev
)
5320 else if (rdev
== p
->replacement
)
5321 rdevp
= &p
->replacement
;
5325 if (number
>= conf
->raid_disks
&&
5326 conf
->reshape_progress
== MaxSector
)
5327 clear_bit(In_sync
, &rdev
->flags
);
5329 if (test_bit(In_sync
, &rdev
->flags
) ||
5330 atomic_read(&rdev
->nr_pending
)) {
5334 /* Only remove non-faulty devices if recovery
5337 if (!test_bit(Faulty
, &rdev
->flags
) &&
5338 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
5339 !has_failed(conf
) &&
5340 (!p
->replacement
|| p
->replacement
== rdev
) &&
5341 number
< conf
->raid_disks
) {
5347 if (atomic_read(&rdev
->nr_pending
)) {
5348 /* lost the race, try later */
5351 } else if (p
->replacement
) {
5352 /* We must have just cleared 'rdev' */
5353 p
->rdev
= p
->replacement
;
5354 clear_bit(Replacement
, &p
->replacement
->flags
);
5355 smp_mb(); /* Make sure other CPUs may see both as identical
5356 * but will never see neither - if they are careful
5358 p
->replacement
= NULL
;
5359 clear_bit(WantReplacement
, &rdev
->flags
);
5361 /* We might have just removed the Replacement as faulty-
5362 * clear the bit just in case
5364 clear_bit(WantReplacement
, &rdev
->flags
);
5367 print_raid5_conf(conf
);
5371 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5373 struct r5conf
*conf
= mddev
->private;
5376 struct disk_info
*p
;
5378 int last
= conf
->raid_disks
- 1;
5380 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
5383 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
5384 /* no point adding a device */
5387 if (rdev
->raid_disk
>= 0)
5388 first
= last
= rdev
->raid_disk
;
5391 * find the disk ... but prefer rdev->saved_raid_disk
5394 if (rdev
->saved_raid_disk
>= 0 &&
5395 rdev
->saved_raid_disk
>= first
&&
5396 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
5397 disk
= rdev
->saved_raid_disk
;
5400 for ( ; disk
<= last
; disk
++) {
5401 p
= conf
->disks
+ disk
;
5402 if (p
->rdev
== NULL
) {
5403 clear_bit(In_sync
, &rdev
->flags
);
5404 rdev
->raid_disk
= disk
;
5406 if (rdev
->saved_raid_disk
!= disk
)
5408 rcu_assign_pointer(p
->rdev
, rdev
);
5411 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
5412 p
->replacement
== NULL
) {
5413 clear_bit(In_sync
, &rdev
->flags
);
5414 set_bit(Replacement
, &rdev
->flags
);
5415 rdev
->raid_disk
= disk
;
5418 rcu_assign_pointer(p
->replacement
, rdev
);
5422 print_raid5_conf(conf
);
5426 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
5428 /* no resync is happening, and there is enough space
5429 * on all devices, so we can resize.
5430 * We need to make sure resync covers any new space.
5431 * If the array is shrinking we should possibly wait until
5432 * any io in the removed space completes, but it hardly seems
5435 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
5436 md_set_array_sectors(mddev
, raid5_size(mddev
, sectors
,
5437 mddev
->raid_disks
));
5438 if (mddev
->array_sectors
>
5439 raid5_size(mddev
, sectors
, mddev
->raid_disks
))
5441 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5442 revalidate_disk(mddev
->gendisk
);
5443 if (sectors
> mddev
->dev_sectors
&&
5444 mddev
->recovery_cp
> mddev
->dev_sectors
) {
5445 mddev
->recovery_cp
= mddev
->dev_sectors
;
5446 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5448 mddev
->dev_sectors
= sectors
;
5449 mddev
->resync_max_sectors
= sectors
;
5453 static int check_stripe_cache(struct mddev
*mddev
)
5455 /* Can only proceed if there are plenty of stripe_heads.
5456 * We need a minimum of one full stripe,, and for sensible progress
5457 * it is best to have about 4 times that.
5458 * If we require 4 times, then the default 256 4K stripe_heads will
5459 * allow for chunk sizes up to 256K, which is probably OK.
5460 * If the chunk size is greater, user-space should request more
5461 * stripe_heads first.
5463 struct r5conf
*conf
= mddev
->private;
5464 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5465 > conf
->max_nr_stripes
||
5466 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5467 > conf
->max_nr_stripes
) {
5468 printk(KERN_WARNING
"md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5470 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
5477 static int check_reshape(struct mddev
*mddev
)
5479 struct r5conf
*conf
= mddev
->private;
5481 if (mddev
->delta_disks
== 0 &&
5482 mddev
->new_layout
== mddev
->layout
&&
5483 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
5484 return 0; /* nothing to do */
5486 /* Cannot grow a bitmap yet */
5488 if (has_failed(conf
))
5490 if (mddev
->delta_disks
< 0) {
5491 /* We might be able to shrink, but the devices must
5492 * be made bigger first.
5493 * For raid6, 4 is the minimum size.
5494 * Otherwise 2 is the minimum
5497 if (mddev
->level
== 6)
5499 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
5503 if (!check_stripe_cache(mddev
))
5506 return resize_stripes(conf
, conf
->raid_disks
+ mddev
->delta_disks
);
5509 static int raid5_start_reshape(struct mddev
*mddev
)
5511 struct r5conf
*conf
= mddev
->private;
5512 struct md_rdev
*rdev
;
5514 unsigned long flags
;
5516 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5519 if (!check_stripe_cache(mddev
))
5522 rdev_for_each(rdev
, mddev
)
5523 if (!test_bit(In_sync
, &rdev
->flags
)
5524 && !test_bit(Faulty
, &rdev
->flags
))
5527 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
5528 /* Not enough devices even to make a degraded array
5533 /* Refuse to reduce size of the array. Any reductions in
5534 * array size must be through explicit setting of array_size
5537 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
5538 < mddev
->array_sectors
) {
5539 printk(KERN_ERR
"md/raid:%s: array size must be reduced "
5540 "before number of disks\n", mdname(mddev
));
5544 atomic_set(&conf
->reshape_stripes
, 0);
5545 spin_lock_irq(&conf
->device_lock
);
5546 conf
->previous_raid_disks
= conf
->raid_disks
;
5547 conf
->raid_disks
+= mddev
->delta_disks
;
5548 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
5549 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5550 conf
->prev_algo
= conf
->algorithm
;
5551 conf
->algorithm
= mddev
->new_layout
;
5552 if (mddev
->delta_disks
< 0)
5553 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
5555 conf
->reshape_progress
= 0;
5556 conf
->reshape_safe
= conf
->reshape_progress
;
5558 spin_unlock_irq(&conf
->device_lock
);
5560 /* Add some new drives, as many as will fit.
5561 * We know there are enough to make the newly sized array work.
5562 * Don't add devices if we are reducing the number of
5563 * devices in the array. This is because it is not possible
5564 * to correctly record the "partially reconstructed" state of
5565 * such devices during the reshape and confusion could result.
5567 if (mddev
->delta_disks
>= 0) {
5568 rdev_for_each(rdev
, mddev
)
5569 if (rdev
->raid_disk
< 0 &&
5570 !test_bit(Faulty
, &rdev
->flags
)) {
5571 if (raid5_add_disk(mddev
, rdev
) == 0) {
5573 >= conf
->previous_raid_disks
)
5574 set_bit(In_sync
, &rdev
->flags
);
5576 rdev
->recovery_offset
= 0;
5578 if (sysfs_link_rdev(mddev
, rdev
))
5579 /* Failure here is OK */;
5581 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
5582 && !test_bit(Faulty
, &rdev
->flags
)) {
5583 /* This is a spare that was manually added */
5584 set_bit(In_sync
, &rdev
->flags
);
5587 /* When a reshape changes the number of devices,
5588 * ->degraded is measured against the larger of the
5589 * pre and post number of devices.
5591 spin_lock_irqsave(&conf
->device_lock
, flags
);
5592 mddev
->degraded
= calc_degraded(conf
);
5593 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5595 mddev
->raid_disks
= conf
->raid_disks
;
5596 mddev
->reshape_position
= conf
->reshape_progress
;
5597 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5599 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5600 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5601 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5602 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5603 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5605 if (!mddev
->sync_thread
) {
5606 mddev
->recovery
= 0;
5607 spin_lock_irq(&conf
->device_lock
);
5608 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
5609 conf
->reshape_progress
= MaxSector
;
5610 mddev
->reshape_position
= MaxSector
;
5611 spin_unlock_irq(&conf
->device_lock
);
5614 conf
->reshape_checkpoint
= jiffies
;
5615 md_wakeup_thread(mddev
->sync_thread
);
5616 md_new_event(mddev
);
5620 /* This is called from the reshape thread and should make any
5621 * changes needed in 'conf'
5623 static void end_reshape(struct r5conf
*conf
)
5626 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
5628 spin_lock_irq(&conf
->device_lock
);
5629 conf
->previous_raid_disks
= conf
->raid_disks
;
5630 conf
->reshape_progress
= MaxSector
;
5631 spin_unlock_irq(&conf
->device_lock
);
5632 wake_up(&conf
->wait_for_overlap
);
5634 /* read-ahead size must cover two whole stripes, which is
5635 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5637 if (conf
->mddev
->queue
) {
5638 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5639 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
5641 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5642 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5647 /* This is called from the raid5d thread with mddev_lock held.
5648 * It makes config changes to the device.
5650 static void raid5_finish_reshape(struct mddev
*mddev
)
5652 struct r5conf
*conf
= mddev
->private;
5654 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5656 if (mddev
->delta_disks
> 0) {
5657 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5658 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5659 revalidate_disk(mddev
->gendisk
);
5662 spin_lock_irq(&conf
->device_lock
);
5663 mddev
->degraded
= calc_degraded(conf
);
5664 spin_unlock_irq(&conf
->device_lock
);
5665 for (d
= conf
->raid_disks
;
5666 d
< conf
->raid_disks
- mddev
->delta_disks
;
5668 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
5670 raid5_remove_disk(mddev
, rdev
) == 0) {
5671 sysfs_unlink_rdev(mddev
, rdev
);
5672 rdev
->raid_disk
= -1;
5676 mddev
->layout
= conf
->algorithm
;
5677 mddev
->chunk_sectors
= conf
->chunk_sectors
;
5678 mddev
->reshape_position
= MaxSector
;
5679 mddev
->delta_disks
= 0;
5683 static void raid5_quiesce(struct mddev
*mddev
, int state
)
5685 struct r5conf
*conf
= mddev
->private;
5688 case 2: /* resume for a suspend */
5689 wake_up(&conf
->wait_for_overlap
);
5692 case 1: /* stop all writes */
5693 spin_lock_irq(&conf
->device_lock
);
5694 /* '2' tells resync/reshape to pause so that all
5695 * active stripes can drain
5698 wait_event_lock_irq(conf
->wait_for_stripe
,
5699 atomic_read(&conf
->active_stripes
) == 0 &&
5700 atomic_read(&conf
->active_aligned_reads
) == 0,
5701 conf
->device_lock
, /* nothing */);
5703 spin_unlock_irq(&conf
->device_lock
);
5704 /* allow reshape to continue */
5705 wake_up(&conf
->wait_for_overlap
);
5708 case 0: /* re-enable writes */
5709 spin_lock_irq(&conf
->device_lock
);
5711 wake_up(&conf
->wait_for_stripe
);
5712 wake_up(&conf
->wait_for_overlap
);
5713 spin_unlock_irq(&conf
->device_lock
);
5719 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
5721 struct r0conf
*raid0_conf
= mddev
->private;
5724 /* for raid0 takeover only one zone is supported */
5725 if (raid0_conf
->nr_strip_zones
> 1) {
5726 printk(KERN_ERR
"md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5728 return ERR_PTR(-EINVAL
);
5731 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
5732 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
5733 mddev
->dev_sectors
= sectors
;
5734 mddev
->new_level
= level
;
5735 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5736 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5737 mddev
->raid_disks
+= 1;
5738 mddev
->delta_disks
= 1;
5739 /* make sure it will be not marked as dirty */
5740 mddev
->recovery_cp
= MaxSector
;
5742 return setup_conf(mddev
);
5746 static void *raid5_takeover_raid1(struct mddev
*mddev
)
5750 if (mddev
->raid_disks
!= 2 ||
5751 mddev
->degraded
> 1)
5752 return ERR_PTR(-EINVAL
);
5754 /* Should check if there are write-behind devices? */
5756 chunksect
= 64*2; /* 64K by default */
5758 /* The array must be an exact multiple of chunksize */
5759 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
5762 if ((chunksect
<<9) < STRIPE_SIZE
)
5763 /* array size does not allow a suitable chunk size */
5764 return ERR_PTR(-EINVAL
);
5766 mddev
->new_level
= 5;
5767 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
5768 mddev
->new_chunk_sectors
= chunksect
;
5770 return setup_conf(mddev
);
5773 static void *raid5_takeover_raid6(struct mddev
*mddev
)
5777 switch (mddev
->layout
) {
5778 case ALGORITHM_LEFT_ASYMMETRIC_6
:
5779 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
5781 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
5782 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
5784 case ALGORITHM_LEFT_SYMMETRIC_6
:
5785 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
5787 case ALGORITHM_RIGHT_SYMMETRIC_6
:
5788 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
5790 case ALGORITHM_PARITY_0_6
:
5791 new_layout
= ALGORITHM_PARITY_0
;
5793 case ALGORITHM_PARITY_N
:
5794 new_layout
= ALGORITHM_PARITY_N
;
5797 return ERR_PTR(-EINVAL
);
5799 mddev
->new_level
= 5;
5800 mddev
->new_layout
= new_layout
;
5801 mddev
->delta_disks
= -1;
5802 mddev
->raid_disks
-= 1;
5803 return setup_conf(mddev
);
5807 static int raid5_check_reshape(struct mddev
*mddev
)
5809 /* For a 2-drive array, the layout and chunk size can be changed
5810 * immediately as not restriping is needed.
5811 * For larger arrays we record the new value - after validation
5812 * to be used by a reshape pass.
5814 struct r5conf
*conf
= mddev
->private;
5815 int new_chunk
= mddev
->new_chunk_sectors
;
5817 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
5819 if (new_chunk
> 0) {
5820 if (!is_power_of_2(new_chunk
))
5822 if (new_chunk
< (PAGE_SIZE
>>9))
5824 if (mddev
->array_sectors
& (new_chunk
-1))
5825 /* not factor of array size */
5829 /* They look valid */
5831 if (mddev
->raid_disks
== 2) {
5832 /* can make the change immediately */
5833 if (mddev
->new_layout
>= 0) {
5834 conf
->algorithm
= mddev
->new_layout
;
5835 mddev
->layout
= mddev
->new_layout
;
5837 if (new_chunk
> 0) {
5838 conf
->chunk_sectors
= new_chunk
;
5839 mddev
->chunk_sectors
= new_chunk
;
5841 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5842 md_wakeup_thread(mddev
->thread
);
5844 return check_reshape(mddev
);
5847 static int raid6_check_reshape(struct mddev
*mddev
)
5849 int new_chunk
= mddev
->new_chunk_sectors
;
5851 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
5853 if (new_chunk
> 0) {
5854 if (!is_power_of_2(new_chunk
))
5856 if (new_chunk
< (PAGE_SIZE
>> 9))
5858 if (mddev
->array_sectors
& (new_chunk
-1))
5859 /* not factor of array size */
5863 /* They look valid */
5864 return check_reshape(mddev
);
5867 static void *raid5_takeover(struct mddev
*mddev
)
5869 /* raid5 can take over:
5870 * raid0 - if there is only one strip zone - make it a raid4 layout
5871 * raid1 - if there are two drives. We need to know the chunk size
5872 * raid4 - trivial - just use a raid4 layout.
5873 * raid6 - Providing it is a *_6 layout
5875 if (mddev
->level
== 0)
5876 return raid45_takeover_raid0(mddev
, 5);
5877 if (mddev
->level
== 1)
5878 return raid5_takeover_raid1(mddev
);
5879 if (mddev
->level
== 4) {
5880 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5881 mddev
->new_level
= 5;
5882 return setup_conf(mddev
);
5884 if (mddev
->level
== 6)
5885 return raid5_takeover_raid6(mddev
);
5887 return ERR_PTR(-EINVAL
);
5890 static void *raid4_takeover(struct mddev
*mddev
)
5892 /* raid4 can take over:
5893 * raid0 - if there is only one strip zone
5894 * raid5 - if layout is right
5896 if (mddev
->level
== 0)
5897 return raid45_takeover_raid0(mddev
, 4);
5898 if (mddev
->level
== 5 &&
5899 mddev
->layout
== ALGORITHM_PARITY_N
) {
5900 mddev
->new_layout
= 0;
5901 mddev
->new_level
= 4;
5902 return setup_conf(mddev
);
5904 return ERR_PTR(-EINVAL
);
5907 static struct md_personality raid5_personality
;
5909 static void *raid6_takeover(struct mddev
*mddev
)
5911 /* Currently can only take over a raid5. We map the
5912 * personality to an equivalent raid6 personality
5913 * with the Q block at the end.
5917 if (mddev
->pers
!= &raid5_personality
)
5918 return ERR_PTR(-EINVAL
);
5919 if (mddev
->degraded
> 1)
5920 return ERR_PTR(-EINVAL
);
5921 if (mddev
->raid_disks
> 253)
5922 return ERR_PTR(-EINVAL
);
5923 if (mddev
->raid_disks
< 3)
5924 return ERR_PTR(-EINVAL
);
5926 switch (mddev
->layout
) {
5927 case ALGORITHM_LEFT_ASYMMETRIC
:
5928 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
5930 case ALGORITHM_RIGHT_ASYMMETRIC
:
5931 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
5933 case ALGORITHM_LEFT_SYMMETRIC
:
5934 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
5936 case ALGORITHM_RIGHT_SYMMETRIC
:
5937 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
5939 case ALGORITHM_PARITY_0
:
5940 new_layout
= ALGORITHM_PARITY_0_6
;
5942 case ALGORITHM_PARITY_N
:
5943 new_layout
= ALGORITHM_PARITY_N
;
5946 return ERR_PTR(-EINVAL
);
5948 mddev
->new_level
= 6;
5949 mddev
->new_layout
= new_layout
;
5950 mddev
->delta_disks
= 1;
5951 mddev
->raid_disks
+= 1;
5952 return setup_conf(mddev
);
5956 static struct md_personality raid6_personality
=
5960 .owner
= THIS_MODULE
,
5961 .make_request
= make_request
,
5965 .error_handler
= error
,
5966 .hot_add_disk
= raid5_add_disk
,
5967 .hot_remove_disk
= raid5_remove_disk
,
5968 .spare_active
= raid5_spare_active
,
5969 .sync_request
= sync_request
,
5970 .resize
= raid5_resize
,
5972 .check_reshape
= raid6_check_reshape
,
5973 .start_reshape
= raid5_start_reshape
,
5974 .finish_reshape
= raid5_finish_reshape
,
5975 .quiesce
= raid5_quiesce
,
5976 .takeover
= raid6_takeover
,
5978 static struct md_personality raid5_personality
=
5982 .owner
= THIS_MODULE
,
5983 .make_request
= make_request
,
5987 .error_handler
= error
,
5988 .hot_add_disk
= raid5_add_disk
,
5989 .hot_remove_disk
= raid5_remove_disk
,
5990 .spare_active
= raid5_spare_active
,
5991 .sync_request
= sync_request
,
5992 .resize
= raid5_resize
,
5994 .check_reshape
= raid5_check_reshape
,
5995 .start_reshape
= raid5_start_reshape
,
5996 .finish_reshape
= raid5_finish_reshape
,
5997 .quiesce
= raid5_quiesce
,
5998 .takeover
= raid5_takeover
,
6001 static struct md_personality raid4_personality
=
6005 .owner
= THIS_MODULE
,
6006 .make_request
= make_request
,
6010 .error_handler
= error
,
6011 .hot_add_disk
= raid5_add_disk
,
6012 .hot_remove_disk
= raid5_remove_disk
,
6013 .spare_active
= raid5_spare_active
,
6014 .sync_request
= sync_request
,
6015 .resize
= raid5_resize
,
6017 .check_reshape
= raid5_check_reshape
,
6018 .start_reshape
= raid5_start_reshape
,
6019 .finish_reshape
= raid5_finish_reshape
,
6020 .quiesce
= raid5_quiesce
,
6021 .takeover
= raid4_takeover
,
6024 static int __init
raid5_init(void)
6026 register_md_personality(&raid6_personality
);
6027 register_md_personality(&raid5_personality
);
6028 register_md_personality(&raid4_personality
);
6032 static void raid5_exit(void)
6034 unregister_md_personality(&raid6_personality
);
6035 unregister_md_personality(&raid5_personality
);
6036 unregister_md_personality(&raid4_personality
);
6039 module_init(raid5_init
);
6040 module_exit(raid5_exit
);
6041 MODULE_LICENSE("GPL");
6042 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6043 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6044 MODULE_ALIAS("md-raid5");
6045 MODULE_ALIAS("md-raid4");
6046 MODULE_ALIAS("md-level-5");
6047 MODULE_ALIAS("md-level-4");
6048 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6049 MODULE_ALIAS("md-raid6");
6050 MODULE_ALIAS("md-level-6");
6052 /* This used to be two separate modules, they were: */
6053 MODULE_ALIAS("raid5");
6054 MODULE_ALIAS("raid6");