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 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
201 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
202 sh
->bm_seq
- conf
->seq_write
> 0)
203 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
205 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
206 list_add_tail(&sh
->lru
, &conf
->handle_list
);
208 md_wakeup_thread(conf
->mddev
->thread
);
210 BUG_ON(stripe_operations_active(sh
));
211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
212 atomic_dec(&conf
->preread_active_stripes
);
213 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
214 md_wakeup_thread(conf
->mddev
->thread
);
216 atomic_dec(&conf
->active_stripes
);
217 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
218 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
219 wake_up(&conf
->wait_for_stripe
);
220 if (conf
->retry_read_aligned
)
221 md_wakeup_thread(conf
->mddev
->thread
);
227 static void release_stripe(struct stripe_head
*sh
)
229 struct r5conf
*conf
= sh
->raid_conf
;
232 spin_lock_irqsave(&conf
->device_lock
, flags
);
233 __release_stripe(conf
, sh
);
234 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
237 static inline void remove_hash(struct stripe_head
*sh
)
239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh
->sector
);
242 hlist_del_init(&sh
->hash
);
245 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
247 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh
->sector
);
252 hlist_add_head(&sh
->hash
, hp
);
256 /* find an idle stripe, make sure it is unhashed, and return it. */
257 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
)
259 struct stripe_head
*sh
= NULL
;
260 struct list_head
*first
;
262 if (list_empty(&conf
->inactive_list
))
264 first
= conf
->inactive_list
.next
;
265 sh
= list_entry(first
, struct stripe_head
, lru
);
266 list_del_init(first
);
268 atomic_inc(&conf
->active_stripes
);
273 static void shrink_buffers(struct stripe_head
*sh
)
277 int num
= sh
->raid_conf
->pool_size
;
279 for (i
= 0; i
< num
; i
++) {
283 sh
->dev
[i
].page
= NULL
;
288 static int grow_buffers(struct stripe_head
*sh
)
291 int num
= sh
->raid_conf
->pool_size
;
293 for (i
= 0; i
< num
; i
++) {
296 if (!(page
= alloc_page(GFP_KERNEL
))) {
299 sh
->dev
[i
].page
= page
;
304 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
305 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
306 struct stripe_head
*sh
);
308 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
310 struct r5conf
*conf
= sh
->raid_conf
;
313 BUG_ON(atomic_read(&sh
->count
) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
315 BUG_ON(stripe_operations_active(sh
));
317 pr_debug("init_stripe called, stripe %llu\n",
318 (unsigned long long)sh
->sector
);
322 sh
->generation
= conf
->generation
- previous
;
323 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
325 stripe_set_idx(sector
, conf
, previous
, sh
);
329 for (i
= sh
->disks
; i
--; ) {
330 struct r5dev
*dev
= &sh
->dev
[i
];
332 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
333 test_bit(R5_LOCKED
, &dev
->flags
)) {
334 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
335 (unsigned long long)sh
->sector
, i
, dev
->toread
,
336 dev
->read
, dev
->towrite
, dev
->written
,
337 test_bit(R5_LOCKED
, &dev
->flags
));
341 raid5_build_block(sh
, i
, previous
);
343 insert_hash(conf
, sh
);
346 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
349 struct stripe_head
*sh
;
350 struct hlist_node
*hn
;
352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
353 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
354 if (sh
->sector
== sector
&& sh
->generation
== generation
)
356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
361 * Need to check if array has failed when deciding whether to:
363 * - remove non-faulty devices
366 * This determination is simple when no reshape is happening.
367 * However if there is a reshape, we need to carefully check
368 * both the before and after sections.
369 * This is because some failed devices may only affect one
370 * of the two sections, and some non-in_sync devices may
371 * be insync in the section most affected by failed devices.
373 static int calc_degraded(struct r5conf
*conf
)
375 int degraded
, degraded2
;
380 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
381 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
382 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
384 else if (test_bit(In_sync
, &rdev
->flags
))
387 /* not in-sync or faulty.
388 * If the reshape increases the number of devices,
389 * this is being recovered by the reshape, so
390 * this 'previous' section is not in_sync.
391 * If the number of devices is being reduced however,
392 * the device can only be part of the array if
393 * we are reverting a reshape, so this section will
396 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
400 if (conf
->raid_disks
== conf
->previous_raid_disks
)
404 for (i
= 0; i
< conf
->raid_disks
; i
++) {
405 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
406 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
408 else if (test_bit(In_sync
, &rdev
->flags
))
411 /* not in-sync or faulty.
412 * If reshape increases the number of devices, this
413 * section has already been recovered, else it
414 * almost certainly hasn't.
416 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
420 if (degraded2
> degraded
)
425 static int has_failed(struct r5conf
*conf
)
429 if (conf
->mddev
->reshape_position
== MaxSector
)
430 return conf
->mddev
->degraded
> conf
->max_degraded
;
432 degraded
= calc_degraded(conf
);
433 if (degraded
> conf
->max_degraded
)
438 static struct stripe_head
*
439 get_active_stripe(struct r5conf
*conf
, sector_t sector
,
440 int previous
, int noblock
, int noquiesce
)
442 struct stripe_head
*sh
;
444 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
446 spin_lock_irq(&conf
->device_lock
);
449 wait_event_lock_irq(conf
->wait_for_stripe
,
450 conf
->quiesce
== 0 || noquiesce
,
451 conf
->device_lock
, /* nothing */);
452 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
454 if (!conf
->inactive_blocked
)
455 sh
= get_free_stripe(conf
);
456 if (noblock
&& sh
== NULL
)
459 conf
->inactive_blocked
= 1;
460 wait_event_lock_irq(conf
->wait_for_stripe
,
461 !list_empty(&conf
->inactive_list
) &&
462 (atomic_read(&conf
->active_stripes
)
463 < (conf
->max_nr_stripes
*3/4)
464 || !conf
->inactive_blocked
),
467 conf
->inactive_blocked
= 0;
469 init_stripe(sh
, sector
, previous
);
471 if (atomic_read(&sh
->count
)) {
472 BUG_ON(!list_empty(&sh
->lru
)
473 && !test_bit(STRIPE_EXPANDING
, &sh
->state
));
475 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
476 atomic_inc(&conf
->active_stripes
);
477 if (list_empty(&sh
->lru
) &&
478 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
480 list_del_init(&sh
->lru
);
483 } while (sh
== NULL
);
486 atomic_inc(&sh
->count
);
488 spin_unlock_irq(&conf
->device_lock
);
493 raid5_end_read_request(struct bio
*bi
, int error
);
495 raid5_end_write_request(struct bio
*bi
, int error
);
497 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
499 struct r5conf
*conf
= sh
->raid_conf
;
500 int i
, disks
= sh
->disks
;
504 for (i
= disks
; i
--; ) {
506 int replace_only
= 0;
507 struct bio
*bi
, *rbi
;
508 struct md_rdev
*rdev
, *rrdev
= NULL
;
509 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
510 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
514 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
516 else if (test_and_clear_bit(R5_WantReplace
,
517 &sh
->dev
[i
].flags
)) {
523 bi
= &sh
->dev
[i
].req
;
524 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
529 bi
->bi_end_io
= raid5_end_write_request
;
530 rbi
->bi_end_io
= raid5_end_write_request
;
532 bi
->bi_end_io
= raid5_end_read_request
;
535 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
536 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
537 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
546 /* We raced and saw duplicates */
549 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
) && rrdev
)
554 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
557 atomic_inc(&rdev
->nr_pending
);
558 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
561 atomic_inc(&rrdev
->nr_pending
);
564 /* We have already checked bad blocks for reads. Now
565 * need to check for writes. We never accept write errors
566 * on the replacement, so we don't to check rrdev.
568 while ((rw
& WRITE
) && rdev
&&
569 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
572 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
573 &first_bad
, &bad_sectors
);
578 set_bit(BlockedBadBlocks
, &rdev
->flags
);
579 if (!conf
->mddev
->external
&&
580 conf
->mddev
->flags
) {
581 /* It is very unlikely, but we might
582 * still need to write out the
583 * bad block log - better give it
585 md_check_recovery(conf
->mddev
);
587 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
589 /* Acknowledged bad block - skip the write */
590 rdev_dec_pending(rdev
, conf
->mddev
);
596 if (s
->syncing
|| s
->expanding
|| s
->expanded
598 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
600 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
602 bi
->bi_bdev
= rdev
->bdev
;
603 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
604 __func__
, (unsigned long long)sh
->sector
,
606 atomic_inc(&sh
->count
);
607 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
608 bi
->bi_flags
= 1 << BIO_UPTODATE
;
610 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
611 bi
->bi_io_vec
[0].bv_offset
= 0;
612 bi
->bi_size
= STRIPE_SIZE
;
615 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
616 generic_make_request(bi
);
619 if (s
->syncing
|| s
->expanding
|| s
->expanded
621 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
623 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
625 rbi
->bi_bdev
= rrdev
->bdev
;
626 pr_debug("%s: for %llu schedule op %ld on "
627 "replacement disc %d\n",
628 __func__
, (unsigned long long)sh
->sector
,
630 atomic_inc(&sh
->count
);
631 rbi
->bi_sector
= sh
->sector
+ rrdev
->data_offset
;
632 rbi
->bi_flags
= 1 << BIO_UPTODATE
;
634 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
635 rbi
->bi_io_vec
[0].bv_offset
= 0;
636 rbi
->bi_size
= STRIPE_SIZE
;
638 generic_make_request(rbi
);
640 if (!rdev
&& !rrdev
) {
642 set_bit(STRIPE_DEGRADED
, &sh
->state
);
643 pr_debug("skip op %ld on disc %d for sector %llu\n",
644 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
645 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
646 set_bit(STRIPE_HANDLE
, &sh
->state
);
651 static struct dma_async_tx_descriptor
*
652 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
653 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
656 struct page
*bio_page
;
659 struct async_submit_ctl submit
;
660 enum async_tx_flags flags
= 0;
662 if (bio
->bi_sector
>= sector
)
663 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
665 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
668 flags
|= ASYNC_TX_FENCE
;
669 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
671 bio_for_each_segment(bvl
, bio
, i
) {
672 int len
= bvl
->bv_len
;
676 if (page_offset
< 0) {
677 b_offset
= -page_offset
;
678 page_offset
+= b_offset
;
682 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
683 clen
= STRIPE_SIZE
- page_offset
;
688 b_offset
+= bvl
->bv_offset
;
689 bio_page
= bvl
->bv_page
;
691 tx
= async_memcpy(page
, bio_page
, page_offset
,
692 b_offset
, clen
, &submit
);
694 tx
= async_memcpy(bio_page
, page
, b_offset
,
695 page_offset
, clen
, &submit
);
697 /* chain the operations */
698 submit
.depend_tx
= tx
;
700 if (clen
< len
) /* hit end of page */
708 static void ops_complete_biofill(void *stripe_head_ref
)
710 struct stripe_head
*sh
= stripe_head_ref
;
711 struct bio
*return_bi
= NULL
;
712 struct r5conf
*conf
= sh
->raid_conf
;
715 pr_debug("%s: stripe %llu\n", __func__
,
716 (unsigned long long)sh
->sector
);
718 /* clear completed biofills */
719 spin_lock_irq(&conf
->device_lock
);
720 for (i
= sh
->disks
; i
--; ) {
721 struct r5dev
*dev
= &sh
->dev
[i
];
723 /* acknowledge completion of a biofill operation */
724 /* and check if we need to reply to a read request,
725 * new R5_Wantfill requests are held off until
726 * !STRIPE_BIOFILL_RUN
728 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
729 struct bio
*rbi
, *rbi2
;
734 while (rbi
&& rbi
->bi_sector
<
735 dev
->sector
+ STRIPE_SECTORS
) {
736 rbi2
= r5_next_bio(rbi
, dev
->sector
);
737 if (!raid5_dec_bi_phys_segments(rbi
)) {
738 rbi
->bi_next
= return_bi
;
745 spin_unlock_irq(&conf
->device_lock
);
746 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
748 return_io(return_bi
);
750 set_bit(STRIPE_HANDLE
, &sh
->state
);
754 static void ops_run_biofill(struct stripe_head
*sh
)
756 struct dma_async_tx_descriptor
*tx
= NULL
;
757 struct r5conf
*conf
= sh
->raid_conf
;
758 struct async_submit_ctl submit
;
761 pr_debug("%s: stripe %llu\n", __func__
,
762 (unsigned long long)sh
->sector
);
764 for (i
= sh
->disks
; i
--; ) {
765 struct r5dev
*dev
= &sh
->dev
[i
];
766 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
768 spin_lock_irq(&conf
->device_lock
);
769 dev
->read
= rbi
= dev
->toread
;
771 spin_unlock_irq(&conf
->device_lock
);
772 while (rbi
&& rbi
->bi_sector
<
773 dev
->sector
+ STRIPE_SECTORS
) {
774 tx
= async_copy_data(0, rbi
, dev
->page
,
776 rbi
= r5_next_bio(rbi
, dev
->sector
);
781 atomic_inc(&sh
->count
);
782 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
783 async_trigger_callback(&submit
);
786 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
793 tgt
= &sh
->dev
[target
];
794 set_bit(R5_UPTODATE
, &tgt
->flags
);
795 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
796 clear_bit(R5_Wantcompute
, &tgt
->flags
);
799 static void ops_complete_compute(void *stripe_head_ref
)
801 struct stripe_head
*sh
= stripe_head_ref
;
803 pr_debug("%s: stripe %llu\n", __func__
,
804 (unsigned long long)sh
->sector
);
806 /* mark the computed target(s) as uptodate */
807 mark_target_uptodate(sh
, sh
->ops
.target
);
808 mark_target_uptodate(sh
, sh
->ops
.target2
);
810 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
811 if (sh
->check_state
== check_state_compute_run
)
812 sh
->check_state
= check_state_compute_result
;
813 set_bit(STRIPE_HANDLE
, &sh
->state
);
817 /* return a pointer to the address conversion region of the scribble buffer */
818 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
819 struct raid5_percpu
*percpu
)
821 return percpu
->scribble
+ sizeof(struct page
*) * (sh
->disks
+ 2);
824 static struct dma_async_tx_descriptor
*
825 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
827 int disks
= sh
->disks
;
828 struct page
**xor_srcs
= percpu
->scribble
;
829 int target
= sh
->ops
.target
;
830 struct r5dev
*tgt
= &sh
->dev
[target
];
831 struct page
*xor_dest
= tgt
->page
;
833 struct dma_async_tx_descriptor
*tx
;
834 struct async_submit_ctl submit
;
837 pr_debug("%s: stripe %llu block: %d\n",
838 __func__
, (unsigned long long)sh
->sector
, target
);
839 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
841 for (i
= disks
; i
--; )
843 xor_srcs
[count
++] = sh
->dev
[i
].page
;
845 atomic_inc(&sh
->count
);
847 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
848 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
));
849 if (unlikely(count
== 1))
850 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
852 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
857 /* set_syndrome_sources - populate source buffers for gen_syndrome
858 * @srcs - (struct page *) array of size sh->disks
859 * @sh - stripe_head to parse
861 * Populates srcs in proper layout order for the stripe and returns the
862 * 'count' of sources to be used in a call to async_gen_syndrome. The P
863 * destination buffer is recorded in srcs[count] and the Q destination
864 * is recorded in srcs[count+1]].
866 static int set_syndrome_sources(struct page
**srcs
, struct stripe_head
*sh
)
868 int disks
= sh
->disks
;
869 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
870 int d0_idx
= raid6_d0(sh
);
874 for (i
= 0; i
< disks
; i
++)
880 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
882 srcs
[slot
] = sh
->dev
[i
].page
;
883 i
= raid6_next_disk(i
, disks
);
884 } while (i
!= d0_idx
);
886 return syndrome_disks
;
889 static struct dma_async_tx_descriptor
*
890 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
892 int disks
= sh
->disks
;
893 struct page
**blocks
= percpu
->scribble
;
895 int qd_idx
= sh
->qd_idx
;
896 struct dma_async_tx_descriptor
*tx
;
897 struct async_submit_ctl submit
;
903 if (sh
->ops
.target
< 0)
904 target
= sh
->ops
.target2
;
905 else if (sh
->ops
.target2
< 0)
906 target
= sh
->ops
.target
;
908 /* we should only have one valid target */
911 pr_debug("%s: stripe %llu block: %d\n",
912 __func__
, (unsigned long long)sh
->sector
, target
);
914 tgt
= &sh
->dev
[target
];
915 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
918 atomic_inc(&sh
->count
);
920 if (target
== qd_idx
) {
921 count
= set_syndrome_sources(blocks
, sh
);
922 blocks
[count
] = NULL
; /* regenerating p is not necessary */
923 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
924 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
925 ops_complete_compute
, sh
,
926 to_addr_conv(sh
, percpu
));
927 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
929 /* Compute any data- or p-drive using XOR */
931 for (i
= disks
; i
-- ; ) {
932 if (i
== target
|| i
== qd_idx
)
934 blocks
[count
++] = sh
->dev
[i
].page
;
937 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
938 NULL
, ops_complete_compute
, sh
,
939 to_addr_conv(sh
, percpu
));
940 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
946 static struct dma_async_tx_descriptor
*
947 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
949 int i
, count
, disks
= sh
->disks
;
950 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
951 int d0_idx
= raid6_d0(sh
);
952 int faila
= -1, failb
= -1;
953 int target
= sh
->ops
.target
;
954 int target2
= sh
->ops
.target2
;
955 struct r5dev
*tgt
= &sh
->dev
[target
];
956 struct r5dev
*tgt2
= &sh
->dev
[target2
];
957 struct dma_async_tx_descriptor
*tx
;
958 struct page
**blocks
= percpu
->scribble
;
959 struct async_submit_ctl submit
;
961 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
962 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
963 BUG_ON(target
< 0 || target2
< 0);
964 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
965 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
967 /* we need to open-code set_syndrome_sources to handle the
968 * slot number conversion for 'faila' and 'failb'
970 for (i
= 0; i
< disks
; i
++)
975 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
977 blocks
[slot
] = sh
->dev
[i
].page
;
983 i
= raid6_next_disk(i
, disks
);
984 } while (i
!= d0_idx
);
986 BUG_ON(faila
== failb
);
989 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
990 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
992 atomic_inc(&sh
->count
);
994 if (failb
== syndrome_disks
+1) {
995 /* Q disk is one of the missing disks */
996 if (faila
== syndrome_disks
) {
997 /* Missing P+Q, just recompute */
998 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
999 ops_complete_compute
, sh
,
1000 to_addr_conv(sh
, percpu
));
1001 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1002 STRIPE_SIZE
, &submit
);
1006 int qd_idx
= sh
->qd_idx
;
1008 /* Missing D+Q: recompute D from P, then recompute Q */
1009 if (target
== qd_idx
)
1010 data_target
= target2
;
1012 data_target
= target
;
1015 for (i
= disks
; i
-- ; ) {
1016 if (i
== data_target
|| i
== qd_idx
)
1018 blocks
[count
++] = sh
->dev
[i
].page
;
1020 dest
= sh
->dev
[data_target
].page
;
1021 init_async_submit(&submit
,
1022 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1024 to_addr_conv(sh
, percpu
));
1025 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1028 count
= set_syndrome_sources(blocks
, sh
);
1029 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1030 ops_complete_compute
, sh
,
1031 to_addr_conv(sh
, percpu
));
1032 return async_gen_syndrome(blocks
, 0, count
+2,
1033 STRIPE_SIZE
, &submit
);
1036 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1037 ops_complete_compute
, sh
,
1038 to_addr_conv(sh
, percpu
));
1039 if (failb
== syndrome_disks
) {
1040 /* We're missing D+P. */
1041 return async_raid6_datap_recov(syndrome_disks
+2,
1045 /* We're missing D+D. */
1046 return async_raid6_2data_recov(syndrome_disks
+2,
1047 STRIPE_SIZE
, faila
, failb
,
1054 static void ops_complete_prexor(void *stripe_head_ref
)
1056 struct stripe_head
*sh
= stripe_head_ref
;
1058 pr_debug("%s: stripe %llu\n", __func__
,
1059 (unsigned long long)sh
->sector
);
1062 static struct dma_async_tx_descriptor
*
1063 ops_run_prexor(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1064 struct dma_async_tx_descriptor
*tx
)
1066 int disks
= sh
->disks
;
1067 struct page
**xor_srcs
= percpu
->scribble
;
1068 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1069 struct async_submit_ctl submit
;
1071 /* existing parity data subtracted */
1072 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1074 pr_debug("%s: stripe %llu\n", __func__
,
1075 (unsigned long long)sh
->sector
);
1077 for (i
= disks
; i
--; ) {
1078 struct r5dev
*dev
= &sh
->dev
[i
];
1079 /* Only process blocks that are known to be uptodate */
1080 if (test_bit(R5_Wantdrain
, &dev
->flags
))
1081 xor_srcs
[count
++] = dev
->page
;
1084 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1085 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
));
1086 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1091 static struct dma_async_tx_descriptor
*
1092 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1094 int disks
= sh
->disks
;
1097 pr_debug("%s: stripe %llu\n", __func__
,
1098 (unsigned long long)sh
->sector
);
1100 for (i
= disks
; i
--; ) {
1101 struct r5dev
*dev
= &sh
->dev
[i
];
1104 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
1107 spin_lock_irq(&sh
->raid_conf
->device_lock
);
1108 chosen
= dev
->towrite
;
1109 dev
->towrite
= NULL
;
1110 BUG_ON(dev
->written
);
1111 wbi
= dev
->written
= chosen
;
1112 spin_unlock_irq(&sh
->raid_conf
->device_lock
);
1114 while (wbi
&& wbi
->bi_sector
<
1115 dev
->sector
+ STRIPE_SECTORS
) {
1116 if (wbi
->bi_rw
& REQ_FUA
)
1117 set_bit(R5_WantFUA
, &dev
->flags
);
1118 tx
= async_copy_data(1, wbi
, dev
->page
,
1120 wbi
= r5_next_bio(wbi
, dev
->sector
);
1128 static void ops_complete_reconstruct(void *stripe_head_ref
)
1130 struct stripe_head
*sh
= stripe_head_ref
;
1131 int disks
= sh
->disks
;
1132 int pd_idx
= sh
->pd_idx
;
1133 int qd_idx
= sh
->qd_idx
;
1137 pr_debug("%s: stripe %llu\n", __func__
,
1138 (unsigned long long)sh
->sector
);
1140 for (i
= disks
; i
--; )
1141 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1143 for (i
= disks
; i
--; ) {
1144 struct r5dev
*dev
= &sh
->dev
[i
];
1146 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1147 set_bit(R5_UPTODATE
, &dev
->flags
);
1149 set_bit(R5_WantFUA
, &dev
->flags
);
1153 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1154 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1155 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1156 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1158 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1159 sh
->reconstruct_state
= reconstruct_state_result
;
1162 set_bit(STRIPE_HANDLE
, &sh
->state
);
1167 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1168 struct dma_async_tx_descriptor
*tx
)
1170 int disks
= sh
->disks
;
1171 struct page
**xor_srcs
= percpu
->scribble
;
1172 struct async_submit_ctl submit
;
1173 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1174 struct page
*xor_dest
;
1176 unsigned long flags
;
1178 pr_debug("%s: stripe %llu\n", __func__
,
1179 (unsigned long long)sh
->sector
);
1181 /* check if prexor is active which means only process blocks
1182 * that are part of a read-modify-write (written)
1184 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1186 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1187 for (i
= disks
; i
--; ) {
1188 struct r5dev
*dev
= &sh
->dev
[i
];
1190 xor_srcs
[count
++] = dev
->page
;
1193 xor_dest
= sh
->dev
[pd_idx
].page
;
1194 for (i
= disks
; i
--; ) {
1195 struct r5dev
*dev
= &sh
->dev
[i
];
1197 xor_srcs
[count
++] = dev
->page
;
1201 /* 1/ if we prexor'd then the dest is reused as a source
1202 * 2/ if we did not prexor then we are redoing the parity
1203 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1204 * for the synchronous xor case
1206 flags
= ASYNC_TX_ACK
|
1207 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1209 atomic_inc(&sh
->count
);
1211 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, sh
,
1212 to_addr_conv(sh
, percpu
));
1213 if (unlikely(count
== 1))
1214 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1216 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1220 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1221 struct dma_async_tx_descriptor
*tx
)
1223 struct async_submit_ctl submit
;
1224 struct page
**blocks
= percpu
->scribble
;
1227 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1229 count
= set_syndrome_sources(blocks
, sh
);
1231 atomic_inc(&sh
->count
);
1233 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_reconstruct
,
1234 sh
, to_addr_conv(sh
, percpu
));
1235 async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1238 static void ops_complete_check(void *stripe_head_ref
)
1240 struct stripe_head
*sh
= stripe_head_ref
;
1242 pr_debug("%s: stripe %llu\n", __func__
,
1243 (unsigned long long)sh
->sector
);
1245 sh
->check_state
= check_state_check_result
;
1246 set_bit(STRIPE_HANDLE
, &sh
->state
);
1250 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1252 int disks
= sh
->disks
;
1253 int pd_idx
= sh
->pd_idx
;
1254 int qd_idx
= sh
->qd_idx
;
1255 struct page
*xor_dest
;
1256 struct page
**xor_srcs
= percpu
->scribble
;
1257 struct dma_async_tx_descriptor
*tx
;
1258 struct async_submit_ctl submit
;
1262 pr_debug("%s: stripe %llu\n", __func__
,
1263 (unsigned long long)sh
->sector
);
1266 xor_dest
= sh
->dev
[pd_idx
].page
;
1267 xor_srcs
[count
++] = xor_dest
;
1268 for (i
= disks
; i
--; ) {
1269 if (i
== pd_idx
|| i
== qd_idx
)
1271 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1274 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1275 to_addr_conv(sh
, percpu
));
1276 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1277 &sh
->ops
.zero_sum_result
, &submit
);
1279 atomic_inc(&sh
->count
);
1280 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1281 tx
= async_trigger_callback(&submit
);
1284 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1286 struct page
**srcs
= percpu
->scribble
;
1287 struct async_submit_ctl submit
;
1290 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1291 (unsigned long long)sh
->sector
, checkp
);
1293 count
= set_syndrome_sources(srcs
, sh
);
1297 atomic_inc(&sh
->count
);
1298 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1299 sh
, to_addr_conv(sh
, percpu
));
1300 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1301 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1304 static void __raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1306 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1307 struct dma_async_tx_descriptor
*tx
= NULL
;
1308 struct r5conf
*conf
= sh
->raid_conf
;
1309 int level
= conf
->level
;
1310 struct raid5_percpu
*percpu
;
1314 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1315 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1316 ops_run_biofill(sh
);
1320 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1322 tx
= ops_run_compute5(sh
, percpu
);
1324 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1325 tx
= ops_run_compute6_1(sh
, percpu
);
1327 tx
= ops_run_compute6_2(sh
, percpu
);
1329 /* terminate the chain if reconstruct is not set to be run */
1330 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1334 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
1335 tx
= ops_run_prexor(sh
, percpu
, tx
);
1337 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1338 tx
= ops_run_biodrain(sh
, tx
);
1342 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1344 ops_run_reconstruct5(sh
, percpu
, tx
);
1346 ops_run_reconstruct6(sh
, percpu
, tx
);
1349 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1350 if (sh
->check_state
== check_state_run
)
1351 ops_run_check_p(sh
, percpu
);
1352 else if (sh
->check_state
== check_state_run_q
)
1353 ops_run_check_pq(sh
, percpu
, 0);
1354 else if (sh
->check_state
== check_state_run_pq
)
1355 ops_run_check_pq(sh
, percpu
, 1);
1361 for (i
= disks
; i
--; ) {
1362 struct r5dev
*dev
= &sh
->dev
[i
];
1363 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1364 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1369 #ifdef CONFIG_MULTICORE_RAID456
1370 static void async_run_ops(void *param
, async_cookie_t cookie
)
1372 struct stripe_head
*sh
= param
;
1373 unsigned long ops_request
= sh
->ops
.request
;
1375 clear_bit_unlock(STRIPE_OPS_REQ_PENDING
, &sh
->state
);
1376 wake_up(&sh
->ops
.wait_for_ops
);
1378 __raid_run_ops(sh
, ops_request
);
1382 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1384 /* since handle_stripe can be called outside of raid5d context
1385 * we need to ensure sh->ops.request is de-staged before another
1388 wait_event(sh
->ops
.wait_for_ops
,
1389 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING
, &sh
->state
));
1390 sh
->ops
.request
= ops_request
;
1392 atomic_inc(&sh
->count
);
1393 async_schedule(async_run_ops
, sh
);
1396 #define raid_run_ops __raid_run_ops
1399 static int grow_one_stripe(struct r5conf
*conf
)
1401 struct stripe_head
*sh
;
1402 sh
= kmem_cache_zalloc(conf
->slab_cache
, GFP_KERNEL
);
1406 sh
->raid_conf
= conf
;
1407 #ifdef CONFIG_MULTICORE_RAID456
1408 init_waitqueue_head(&sh
->ops
.wait_for_ops
);
1411 if (grow_buffers(sh
)) {
1413 kmem_cache_free(conf
->slab_cache
, sh
);
1416 /* we just created an active stripe so... */
1417 atomic_set(&sh
->count
, 1);
1418 atomic_inc(&conf
->active_stripes
);
1419 INIT_LIST_HEAD(&sh
->lru
);
1424 static int grow_stripes(struct r5conf
*conf
, int num
)
1426 struct kmem_cache
*sc
;
1427 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
1429 if (conf
->mddev
->gendisk
)
1430 sprintf(conf
->cache_name
[0],
1431 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
1433 sprintf(conf
->cache_name
[0],
1434 "raid%d-%p", conf
->level
, conf
->mddev
);
1435 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
1437 conf
->active_name
= 0;
1438 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
1439 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
1443 conf
->slab_cache
= sc
;
1444 conf
->pool_size
= devs
;
1446 if (!grow_one_stripe(conf
))
1452 * scribble_len - return the required size of the scribble region
1453 * @num - total number of disks in the array
1455 * The size must be enough to contain:
1456 * 1/ a struct page pointer for each device in the array +2
1457 * 2/ room to convert each entry in (1) to its corresponding dma
1458 * (dma_map_page()) or page (page_address()) address.
1460 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1461 * calculate over all devices (not just the data blocks), using zeros in place
1462 * of the P and Q blocks.
1464 static size_t scribble_len(int num
)
1468 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
1473 static int resize_stripes(struct r5conf
*conf
, int newsize
)
1475 /* Make all the stripes able to hold 'newsize' devices.
1476 * New slots in each stripe get 'page' set to a new page.
1478 * This happens in stages:
1479 * 1/ create a new kmem_cache and allocate the required number of
1481 * 2/ gather all the old stripe_heads and tranfer the pages across
1482 * to the new stripe_heads. This will have the side effect of
1483 * freezing the array as once all stripe_heads have been collected,
1484 * no IO will be possible. Old stripe heads are freed once their
1485 * pages have been transferred over, and the old kmem_cache is
1486 * freed when all stripes are done.
1487 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1488 * we simple return a failre status - no need to clean anything up.
1489 * 4/ allocate new pages for the new slots in the new stripe_heads.
1490 * If this fails, we don't bother trying the shrink the
1491 * stripe_heads down again, we just leave them as they are.
1492 * As each stripe_head is processed the new one is released into
1495 * Once step2 is started, we cannot afford to wait for a write,
1496 * so we use GFP_NOIO allocations.
1498 struct stripe_head
*osh
, *nsh
;
1499 LIST_HEAD(newstripes
);
1500 struct disk_info
*ndisks
;
1503 struct kmem_cache
*sc
;
1506 if (newsize
<= conf
->pool_size
)
1507 return 0; /* never bother to shrink */
1509 err
= md_allow_write(conf
->mddev
);
1514 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
1515 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
1520 for (i
= conf
->max_nr_stripes
; i
; i
--) {
1521 nsh
= kmem_cache_zalloc(sc
, GFP_KERNEL
);
1525 nsh
->raid_conf
= conf
;
1526 #ifdef CONFIG_MULTICORE_RAID456
1527 init_waitqueue_head(&nsh
->ops
.wait_for_ops
);
1530 list_add(&nsh
->lru
, &newstripes
);
1533 /* didn't get enough, give up */
1534 while (!list_empty(&newstripes
)) {
1535 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1536 list_del(&nsh
->lru
);
1537 kmem_cache_free(sc
, nsh
);
1539 kmem_cache_destroy(sc
);
1542 /* Step 2 - Must use GFP_NOIO now.
1543 * OK, we have enough stripes, start collecting inactive
1544 * stripes and copying them over
1546 list_for_each_entry(nsh
, &newstripes
, lru
) {
1547 spin_lock_irq(&conf
->device_lock
);
1548 wait_event_lock_irq(conf
->wait_for_stripe
,
1549 !list_empty(&conf
->inactive_list
),
1552 osh
= get_free_stripe(conf
);
1553 spin_unlock_irq(&conf
->device_lock
);
1554 atomic_set(&nsh
->count
, 1);
1555 for(i
=0; i
<conf
->pool_size
; i
++)
1556 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1557 for( ; i
<newsize
; i
++)
1558 nsh
->dev
[i
].page
= NULL
;
1559 kmem_cache_free(conf
->slab_cache
, osh
);
1561 kmem_cache_destroy(conf
->slab_cache
);
1564 * At this point, we are holding all the stripes so the array
1565 * is completely stalled, so now is a good time to resize
1566 * conf->disks and the scribble region
1568 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1570 for (i
=0; i
<conf
->raid_disks
; i
++)
1571 ndisks
[i
] = conf
->disks
[i
];
1573 conf
->disks
= ndisks
;
1578 conf
->scribble_len
= scribble_len(newsize
);
1579 for_each_present_cpu(cpu
) {
1580 struct raid5_percpu
*percpu
;
1583 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1584 scribble
= kmalloc(conf
->scribble_len
, GFP_NOIO
);
1587 kfree(percpu
->scribble
);
1588 percpu
->scribble
= scribble
;
1596 /* Step 4, return new stripes to service */
1597 while(!list_empty(&newstripes
)) {
1598 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1599 list_del_init(&nsh
->lru
);
1601 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1602 if (nsh
->dev
[i
].page
== NULL
) {
1603 struct page
*p
= alloc_page(GFP_NOIO
);
1604 nsh
->dev
[i
].page
= p
;
1608 release_stripe(nsh
);
1610 /* critical section pass, GFP_NOIO no longer needed */
1612 conf
->slab_cache
= sc
;
1613 conf
->active_name
= 1-conf
->active_name
;
1614 conf
->pool_size
= newsize
;
1618 static int drop_one_stripe(struct r5conf
*conf
)
1620 struct stripe_head
*sh
;
1622 spin_lock_irq(&conf
->device_lock
);
1623 sh
= get_free_stripe(conf
);
1624 spin_unlock_irq(&conf
->device_lock
);
1627 BUG_ON(atomic_read(&sh
->count
));
1629 kmem_cache_free(conf
->slab_cache
, sh
);
1630 atomic_dec(&conf
->active_stripes
);
1634 static void shrink_stripes(struct r5conf
*conf
)
1636 while (drop_one_stripe(conf
))
1639 if (conf
->slab_cache
)
1640 kmem_cache_destroy(conf
->slab_cache
);
1641 conf
->slab_cache
= NULL
;
1644 static void raid5_end_read_request(struct bio
* bi
, int error
)
1646 struct stripe_head
*sh
= bi
->bi_private
;
1647 struct r5conf
*conf
= sh
->raid_conf
;
1648 int disks
= sh
->disks
, i
;
1649 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1650 char b
[BDEVNAME_SIZE
];
1651 struct md_rdev
*rdev
= NULL
;
1654 for (i
=0 ; i
<disks
; i
++)
1655 if (bi
== &sh
->dev
[i
].req
)
1658 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1659 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1665 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1666 /* If replacement finished while this request was outstanding,
1667 * 'replacement' might be NULL already.
1668 * In that case it moved down to 'rdev'.
1669 * rdev is not removed until all requests are finished.
1671 rdev
= conf
->disks
[i
].replacement
;
1673 rdev
= conf
->disks
[i
].rdev
;
1676 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1677 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1678 /* Note that this cannot happen on a
1679 * replacement device. We just fail those on
1684 "md/raid:%s: read error corrected"
1685 " (%lu sectors at %llu on %s)\n",
1686 mdname(conf
->mddev
), STRIPE_SECTORS
,
1687 (unsigned long long)(sh
->sector
1688 + rdev
->data_offset
),
1689 bdevname(rdev
->bdev
, b
));
1690 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1691 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1692 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1694 if (atomic_read(&rdev
->read_errors
))
1695 atomic_set(&rdev
->read_errors
, 0);
1697 const char *bdn
= bdevname(rdev
->bdev
, b
);
1700 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1701 atomic_inc(&rdev
->read_errors
);
1702 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1705 "md/raid:%s: read error on replacement device "
1706 "(sector %llu on %s).\n",
1707 mdname(conf
->mddev
),
1708 (unsigned long long)(sh
->sector
1709 + rdev
->data_offset
),
1711 else if (conf
->mddev
->degraded
>= conf
->max_degraded
)
1714 "md/raid:%s: read error not correctable "
1715 "(sector %llu on %s).\n",
1716 mdname(conf
->mddev
),
1717 (unsigned long long)(sh
->sector
1718 + rdev
->data_offset
),
1720 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1724 "md/raid:%s: read error NOT corrected!! "
1725 "(sector %llu on %s).\n",
1726 mdname(conf
->mddev
),
1727 (unsigned long long)(sh
->sector
1728 + rdev
->data_offset
),
1730 else if (atomic_read(&rdev
->read_errors
)
1731 > conf
->max_nr_stripes
)
1733 "md/raid:%s: Too many read errors, failing device %s.\n",
1734 mdname(conf
->mddev
), bdn
);
1738 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1740 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1741 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1742 md_error(conf
->mddev
, rdev
);
1745 rdev_dec_pending(rdev
, conf
->mddev
);
1746 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1747 set_bit(STRIPE_HANDLE
, &sh
->state
);
1751 static void raid5_end_write_request(struct bio
*bi
, int error
)
1753 struct stripe_head
*sh
= bi
->bi_private
;
1754 struct r5conf
*conf
= sh
->raid_conf
;
1755 int disks
= sh
->disks
, i
;
1756 struct md_rdev
*uninitialized_var(rdev
);
1757 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1760 int replacement
= 0;
1762 for (i
= 0 ; i
< disks
; i
++) {
1763 if (bi
== &sh
->dev
[i
].req
) {
1764 rdev
= conf
->disks
[i
].rdev
;
1767 if (bi
== &sh
->dev
[i
].rreq
) {
1768 rdev
= conf
->disks
[i
].replacement
;
1772 /* rdev was removed and 'replacement'
1773 * replaced it. rdev is not removed
1774 * until all requests are finished.
1776 rdev
= conf
->disks
[i
].rdev
;
1780 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1781 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1790 md_error(conf
->mddev
, rdev
);
1791 else if (is_badblock(rdev
, sh
->sector
,
1793 &first_bad
, &bad_sectors
))
1794 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
1797 set_bit(WriteErrorSeen
, &rdev
->flags
);
1798 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
1799 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1800 set_bit(MD_RECOVERY_NEEDED
,
1801 &rdev
->mddev
->recovery
);
1802 } else if (is_badblock(rdev
, sh
->sector
,
1804 &first_bad
, &bad_sectors
))
1805 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
1807 rdev_dec_pending(rdev
, conf
->mddev
);
1809 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
1810 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1811 set_bit(STRIPE_HANDLE
, &sh
->state
);
1815 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
);
1817 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
1819 struct r5dev
*dev
= &sh
->dev
[i
];
1821 bio_init(&dev
->req
);
1822 dev
->req
.bi_io_vec
= &dev
->vec
;
1824 dev
->req
.bi_max_vecs
++;
1825 dev
->req
.bi_private
= sh
;
1826 dev
->vec
.bv_page
= dev
->page
;
1828 bio_init(&dev
->rreq
);
1829 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
1830 dev
->rreq
.bi_vcnt
++;
1831 dev
->rreq
.bi_max_vecs
++;
1832 dev
->rreq
.bi_private
= sh
;
1833 dev
->rvec
.bv_page
= dev
->page
;
1836 dev
->sector
= compute_blocknr(sh
, i
, previous
);
1839 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1841 char b
[BDEVNAME_SIZE
];
1842 struct r5conf
*conf
= mddev
->private;
1843 unsigned long flags
;
1844 pr_debug("raid456: error called\n");
1846 spin_lock_irqsave(&conf
->device_lock
, flags
);
1847 clear_bit(In_sync
, &rdev
->flags
);
1848 mddev
->degraded
= calc_degraded(conf
);
1849 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1850 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1852 set_bit(Blocked
, &rdev
->flags
);
1853 set_bit(Faulty
, &rdev
->flags
);
1854 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1856 "md/raid:%s: Disk failure on %s, disabling device.\n"
1857 "md/raid:%s: Operation continuing on %d devices.\n",
1859 bdevname(rdev
->bdev
, b
),
1861 conf
->raid_disks
- mddev
->degraded
);
1865 * Input: a 'big' sector number,
1866 * Output: index of the data and parity disk, and the sector # in them.
1868 static sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
1869 int previous
, int *dd_idx
,
1870 struct stripe_head
*sh
)
1872 sector_t stripe
, stripe2
;
1873 sector_t chunk_number
;
1874 unsigned int chunk_offset
;
1877 sector_t new_sector
;
1878 int algorithm
= previous
? conf
->prev_algo
1880 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
1881 : conf
->chunk_sectors
;
1882 int raid_disks
= previous
? conf
->previous_raid_disks
1884 int data_disks
= raid_disks
- conf
->max_degraded
;
1886 /* First compute the information on this sector */
1889 * Compute the chunk number and the sector offset inside the chunk
1891 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
1892 chunk_number
= r_sector
;
1895 * Compute the stripe number
1897 stripe
= chunk_number
;
1898 *dd_idx
= sector_div(stripe
, data_disks
);
1901 * Select the parity disk based on the user selected algorithm.
1903 pd_idx
= qd_idx
= -1;
1904 switch(conf
->level
) {
1906 pd_idx
= data_disks
;
1909 switch (algorithm
) {
1910 case ALGORITHM_LEFT_ASYMMETRIC
:
1911 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1912 if (*dd_idx
>= pd_idx
)
1915 case ALGORITHM_RIGHT_ASYMMETRIC
:
1916 pd_idx
= sector_div(stripe2
, raid_disks
);
1917 if (*dd_idx
>= pd_idx
)
1920 case ALGORITHM_LEFT_SYMMETRIC
:
1921 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1922 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1924 case ALGORITHM_RIGHT_SYMMETRIC
:
1925 pd_idx
= sector_div(stripe2
, raid_disks
);
1926 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1928 case ALGORITHM_PARITY_0
:
1932 case ALGORITHM_PARITY_N
:
1933 pd_idx
= data_disks
;
1941 switch (algorithm
) {
1942 case ALGORITHM_LEFT_ASYMMETRIC
:
1943 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
1944 qd_idx
= pd_idx
+ 1;
1945 if (pd_idx
== raid_disks
-1) {
1946 (*dd_idx
)++; /* Q D D D P */
1948 } else if (*dd_idx
>= pd_idx
)
1949 (*dd_idx
) += 2; /* D D P Q D */
1951 case ALGORITHM_RIGHT_ASYMMETRIC
:
1952 pd_idx
= sector_div(stripe2
, raid_disks
);
1953 qd_idx
= pd_idx
+ 1;
1954 if (pd_idx
== raid_disks
-1) {
1955 (*dd_idx
)++; /* Q D D D P */
1957 } else if (*dd_idx
>= pd_idx
)
1958 (*dd_idx
) += 2; /* D D P Q D */
1960 case ALGORITHM_LEFT_SYMMETRIC
:
1961 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
1962 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1963 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1965 case ALGORITHM_RIGHT_SYMMETRIC
:
1966 pd_idx
= sector_div(stripe2
, raid_disks
);
1967 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1968 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1971 case ALGORITHM_PARITY_0
:
1976 case ALGORITHM_PARITY_N
:
1977 pd_idx
= data_disks
;
1978 qd_idx
= data_disks
+ 1;
1981 case ALGORITHM_ROTATING_ZERO_RESTART
:
1982 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1983 * of blocks for computing Q is different.
1985 pd_idx
= sector_div(stripe2
, raid_disks
);
1986 qd_idx
= pd_idx
+ 1;
1987 if (pd_idx
== raid_disks
-1) {
1988 (*dd_idx
)++; /* Q D D D P */
1990 } else if (*dd_idx
>= pd_idx
)
1991 (*dd_idx
) += 2; /* D D P Q D */
1995 case ALGORITHM_ROTATING_N_RESTART
:
1996 /* Same a left_asymmetric, by first stripe is
1997 * D D D P Q rather than
2001 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2002 qd_idx
= pd_idx
+ 1;
2003 if (pd_idx
== raid_disks
-1) {
2004 (*dd_idx
)++; /* Q D D D P */
2006 } else if (*dd_idx
>= pd_idx
)
2007 (*dd_idx
) += 2; /* D D P Q D */
2011 case ALGORITHM_ROTATING_N_CONTINUE
:
2012 /* Same as left_symmetric but Q is before P */
2013 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2014 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2015 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2019 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2020 /* RAID5 left_asymmetric, with Q on last device */
2021 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2022 if (*dd_idx
>= pd_idx
)
2024 qd_idx
= raid_disks
- 1;
2027 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2028 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2029 if (*dd_idx
>= pd_idx
)
2031 qd_idx
= raid_disks
- 1;
2034 case ALGORITHM_LEFT_SYMMETRIC_6
:
2035 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2036 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2037 qd_idx
= raid_disks
- 1;
2040 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2041 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2042 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2043 qd_idx
= raid_disks
- 1;
2046 case ALGORITHM_PARITY_0_6
:
2049 qd_idx
= raid_disks
- 1;
2059 sh
->pd_idx
= pd_idx
;
2060 sh
->qd_idx
= qd_idx
;
2061 sh
->ddf_layout
= ddf_layout
;
2064 * Finally, compute the new sector number
2066 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2071 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2073 struct r5conf
*conf
= sh
->raid_conf
;
2074 int raid_disks
= sh
->disks
;
2075 int data_disks
= raid_disks
- conf
->max_degraded
;
2076 sector_t new_sector
= sh
->sector
, check
;
2077 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2078 : conf
->chunk_sectors
;
2079 int algorithm
= previous
? conf
->prev_algo
2083 sector_t chunk_number
;
2084 int dummy1
, dd_idx
= i
;
2086 struct stripe_head sh2
;
2089 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2090 stripe
= new_sector
;
2092 if (i
== sh
->pd_idx
)
2094 switch(conf
->level
) {
2097 switch (algorithm
) {
2098 case ALGORITHM_LEFT_ASYMMETRIC
:
2099 case ALGORITHM_RIGHT_ASYMMETRIC
:
2103 case ALGORITHM_LEFT_SYMMETRIC
:
2104 case ALGORITHM_RIGHT_SYMMETRIC
:
2107 i
-= (sh
->pd_idx
+ 1);
2109 case ALGORITHM_PARITY_0
:
2112 case ALGORITHM_PARITY_N
:
2119 if (i
== sh
->qd_idx
)
2120 return 0; /* It is the Q disk */
2121 switch (algorithm
) {
2122 case ALGORITHM_LEFT_ASYMMETRIC
:
2123 case ALGORITHM_RIGHT_ASYMMETRIC
:
2124 case ALGORITHM_ROTATING_ZERO_RESTART
:
2125 case ALGORITHM_ROTATING_N_RESTART
:
2126 if (sh
->pd_idx
== raid_disks
-1)
2127 i
--; /* Q D D D P */
2128 else if (i
> sh
->pd_idx
)
2129 i
-= 2; /* D D P Q D */
2131 case ALGORITHM_LEFT_SYMMETRIC
:
2132 case ALGORITHM_RIGHT_SYMMETRIC
:
2133 if (sh
->pd_idx
== raid_disks
-1)
2134 i
--; /* Q D D D P */
2139 i
-= (sh
->pd_idx
+ 2);
2142 case ALGORITHM_PARITY_0
:
2145 case ALGORITHM_PARITY_N
:
2147 case ALGORITHM_ROTATING_N_CONTINUE
:
2148 /* Like left_symmetric, but P is before Q */
2149 if (sh
->pd_idx
== 0)
2150 i
--; /* P D D D Q */
2155 i
-= (sh
->pd_idx
+ 1);
2158 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2159 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2163 case ALGORITHM_LEFT_SYMMETRIC_6
:
2164 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2166 i
+= data_disks
+ 1;
2167 i
-= (sh
->pd_idx
+ 1);
2169 case ALGORITHM_PARITY_0_6
:
2178 chunk_number
= stripe
* data_disks
+ i
;
2179 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2181 check
= raid5_compute_sector(conf
, r_sector
,
2182 previous
, &dummy1
, &sh2
);
2183 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2184 || sh2
.qd_idx
!= sh
->qd_idx
) {
2185 printk(KERN_ERR
"md/raid:%s: compute_blocknr: map not correct\n",
2186 mdname(conf
->mddev
));
2194 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2195 int rcw
, int expand
)
2197 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
2198 struct r5conf
*conf
= sh
->raid_conf
;
2199 int level
= conf
->level
;
2202 /* if we are not expanding this is a proper write request, and
2203 * there will be bios with new data to be drained into the
2207 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2208 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2210 sh
->reconstruct_state
= reconstruct_state_run
;
2212 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2214 for (i
= disks
; i
--; ) {
2215 struct r5dev
*dev
= &sh
->dev
[i
];
2218 set_bit(R5_LOCKED
, &dev
->flags
);
2219 set_bit(R5_Wantdrain
, &dev
->flags
);
2221 clear_bit(R5_UPTODATE
, &dev
->flags
);
2225 if (s
->locked
+ conf
->max_degraded
== disks
)
2226 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2227 atomic_inc(&conf
->pending_full_writes
);
2230 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2231 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2233 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2234 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2235 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2236 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2238 for (i
= disks
; i
--; ) {
2239 struct r5dev
*dev
= &sh
->dev
[i
];
2244 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2245 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2246 set_bit(R5_Wantdrain
, &dev
->flags
);
2247 set_bit(R5_LOCKED
, &dev
->flags
);
2248 clear_bit(R5_UPTODATE
, &dev
->flags
);
2254 /* keep the parity disk(s) locked while asynchronous operations
2257 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2258 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2262 int qd_idx
= sh
->qd_idx
;
2263 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2265 set_bit(R5_LOCKED
, &dev
->flags
);
2266 clear_bit(R5_UPTODATE
, &dev
->flags
);
2270 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2271 __func__
, (unsigned long long)sh
->sector
,
2272 s
->locked
, s
->ops_request
);
2276 * Each stripe/dev can have one or more bion attached.
2277 * toread/towrite point to the first in a chain.
2278 * The bi_next chain must be in order.
2280 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
2283 struct r5conf
*conf
= sh
->raid_conf
;
2286 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2287 (unsigned long long)bi
->bi_sector
,
2288 (unsigned long long)sh
->sector
);
2291 spin_lock_irq(&conf
->device_lock
);
2293 bip
= &sh
->dev
[dd_idx
].towrite
;
2294 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
2297 bip
= &sh
->dev
[dd_idx
].toread
;
2298 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
2299 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
2301 bip
= & (*bip
)->bi_next
;
2303 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
2306 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
2310 bi
->bi_phys_segments
++;
2313 /* check if page is covered */
2314 sector_t sector
= sh
->dev
[dd_idx
].sector
;
2315 for (bi
=sh
->dev
[dd_idx
].towrite
;
2316 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
2317 bi
&& bi
->bi_sector
<= sector
;
2318 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
2319 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
2320 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
2322 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
2323 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
2325 spin_unlock_irq(&conf
->device_lock
);
2327 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2328 (unsigned long long)(*bip
)->bi_sector
,
2329 (unsigned long long)sh
->sector
, dd_idx
);
2331 if (conf
->mddev
->bitmap
&& firstwrite
) {
2332 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
2334 sh
->bm_seq
= conf
->seq_flush
+1;
2335 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
2340 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
2341 spin_unlock_irq(&conf
->device_lock
);
2345 static void end_reshape(struct r5conf
*conf
);
2347 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
2348 struct stripe_head
*sh
)
2350 int sectors_per_chunk
=
2351 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
2353 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
2354 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
2356 raid5_compute_sector(conf
,
2357 stripe
* (disks
- conf
->max_degraded
)
2358 *sectors_per_chunk
+ chunk_offset
,
2364 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
2365 struct stripe_head_state
*s
, int disks
,
2366 struct bio
**return_bi
)
2369 for (i
= disks
; i
--; ) {
2373 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2374 struct md_rdev
*rdev
;
2376 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2377 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2378 atomic_inc(&rdev
->nr_pending
);
2383 if (!rdev_set_badblocks(
2387 md_error(conf
->mddev
, rdev
);
2388 rdev_dec_pending(rdev
, conf
->mddev
);
2391 spin_lock_irq(&conf
->device_lock
);
2392 /* fail all writes first */
2393 bi
= sh
->dev
[i
].towrite
;
2394 sh
->dev
[i
].towrite
= NULL
;
2400 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2401 wake_up(&conf
->wait_for_overlap
);
2403 while (bi
&& bi
->bi_sector
<
2404 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2405 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2406 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2407 if (!raid5_dec_bi_phys_segments(bi
)) {
2408 md_write_end(conf
->mddev
);
2409 bi
->bi_next
= *return_bi
;
2414 /* and fail all 'written' */
2415 bi
= sh
->dev
[i
].written
;
2416 sh
->dev
[i
].written
= NULL
;
2417 if (bi
) bitmap_end
= 1;
2418 while (bi
&& bi
->bi_sector
<
2419 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2420 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2421 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2422 if (!raid5_dec_bi_phys_segments(bi
)) {
2423 md_write_end(conf
->mddev
);
2424 bi
->bi_next
= *return_bi
;
2430 /* fail any reads if this device is non-operational and
2431 * the data has not reached the cache yet.
2433 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2434 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2435 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2436 bi
= sh
->dev
[i
].toread
;
2437 sh
->dev
[i
].toread
= NULL
;
2438 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2439 wake_up(&conf
->wait_for_overlap
);
2440 if (bi
) s
->to_read
--;
2441 while (bi
&& bi
->bi_sector
<
2442 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2443 struct bio
*nextbi
=
2444 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2445 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2446 if (!raid5_dec_bi_phys_segments(bi
)) {
2447 bi
->bi_next
= *return_bi
;
2453 spin_unlock_irq(&conf
->device_lock
);
2455 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2456 STRIPE_SECTORS
, 0, 0);
2457 /* If we were in the middle of a write the parity block might
2458 * still be locked - so just clear all R5_LOCKED flags
2460 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2463 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2464 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2465 md_wakeup_thread(conf
->mddev
->thread
);
2469 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
2470 struct stripe_head_state
*s
)
2475 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 0);
2476 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2479 /* There is nothing more to do for sync/check/repair.
2480 * For recover/replace we need to record a bad block on all
2481 * non-sync devices, or abort the recovery
2483 if (!test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
))
2485 /* During recovery devices cannot be removed, so locking and
2486 * refcounting of rdevs is not needed
2488 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2489 struct md_rdev
*rdev
= conf
->disks
[i
].rdev
;
2491 && !test_bit(Faulty
, &rdev
->flags
)
2492 && !test_bit(In_sync
, &rdev
->flags
)
2493 && !rdev_set_badblocks(rdev
, sh
->sector
,
2496 rdev
= conf
->disks
[i
].replacement
;
2498 && !test_bit(Faulty
, &rdev
->flags
)
2499 && !test_bit(In_sync
, &rdev
->flags
)
2500 && !rdev_set_badblocks(rdev
, sh
->sector
,
2505 conf
->recovery_disabled
= conf
->mddev
->recovery_disabled
;
2506 set_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
);
2510 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
2512 struct md_rdev
*rdev
;
2514 /* Doing recovery so rcu locking not required */
2515 rdev
= sh
->raid_conf
->disks
[disk_idx
].replacement
;
2517 && !test_bit(Faulty
, &rdev
->flags
)
2518 && !test_bit(In_sync
, &rdev
->flags
)
2519 && (rdev
->recovery_offset
<= sh
->sector
2520 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
2526 /* fetch_block - checks the given member device to see if its data needs
2527 * to be read or computed to satisfy a request.
2529 * Returns 1 when no more member devices need to be checked, otherwise returns
2530 * 0 to tell the loop in handle_stripe_fill to continue
2532 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2533 int disk_idx
, int disks
)
2535 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2536 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
2537 &sh
->dev
[s
->failed_num
[1]] };
2539 /* is the data in this block needed, and can we get it? */
2540 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2541 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2543 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2544 s
->syncing
|| s
->expanding
||
2545 (s
->replacing
&& want_replace(sh
, disk_idx
)) ||
2546 (s
->failed
>= 1 && fdev
[0]->toread
) ||
2547 (s
->failed
>= 2 && fdev
[1]->toread
) ||
2548 (sh
->raid_conf
->level
<= 5 && s
->failed
&& fdev
[0]->towrite
&&
2549 !test_bit(R5_OVERWRITE
, &fdev
[0]->flags
)) ||
2550 (sh
->raid_conf
->level
== 6 && s
->failed
&& s
->to_write
))) {
2551 /* we would like to get this block, possibly by computing it,
2552 * otherwise read it if the backing disk is insync
2554 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
2555 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
2556 if ((s
->uptodate
== disks
- 1) &&
2557 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
2558 disk_idx
== s
->failed_num
[1]))) {
2559 /* have disk failed, and we're requested to fetch it;
2562 pr_debug("Computing stripe %llu block %d\n",
2563 (unsigned long long)sh
->sector
, disk_idx
);
2564 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2565 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2566 set_bit(R5_Wantcompute
, &dev
->flags
);
2567 sh
->ops
.target
= disk_idx
;
2568 sh
->ops
.target2
= -1; /* no 2nd target */
2570 /* Careful: from this point on 'uptodate' is in the eye
2571 * of raid_run_ops which services 'compute' operations
2572 * before writes. R5_Wantcompute flags a block that will
2573 * be R5_UPTODATE by the time it is needed for a
2574 * subsequent operation.
2578 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
2579 /* Computing 2-failure is *very* expensive; only
2580 * do it if failed >= 2
2583 for (other
= disks
; other
--; ) {
2584 if (other
== disk_idx
)
2586 if (!test_bit(R5_UPTODATE
,
2587 &sh
->dev
[other
].flags
))
2591 pr_debug("Computing stripe %llu blocks %d,%d\n",
2592 (unsigned long long)sh
->sector
,
2594 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2595 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2596 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
2597 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
2598 sh
->ops
.target
= disk_idx
;
2599 sh
->ops
.target2
= other
;
2603 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2604 set_bit(R5_LOCKED
, &dev
->flags
);
2605 set_bit(R5_Wantread
, &dev
->flags
);
2607 pr_debug("Reading block %d (sync=%d)\n",
2608 disk_idx
, s
->syncing
);
2616 * handle_stripe_fill - read or compute data to satisfy pending requests.
2618 static void handle_stripe_fill(struct stripe_head
*sh
,
2619 struct stripe_head_state
*s
,
2624 /* look for blocks to read/compute, skip this if a compute
2625 * is already in flight, or if the stripe contents are in the
2626 * midst of changing due to a write
2628 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2629 !sh
->reconstruct_state
)
2630 for (i
= disks
; i
--; )
2631 if (fetch_block(sh
, s
, i
, disks
))
2633 set_bit(STRIPE_HANDLE
, &sh
->state
);
2637 /* handle_stripe_clean_event
2638 * any written block on an uptodate or failed drive can be returned.
2639 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2640 * never LOCKED, so we don't need to test 'failed' directly.
2642 static void handle_stripe_clean_event(struct r5conf
*conf
,
2643 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2648 for (i
= disks
; i
--; )
2649 if (sh
->dev
[i
].written
) {
2651 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2652 test_bit(R5_UPTODATE
, &dev
->flags
)) {
2653 /* We can return any write requests */
2654 struct bio
*wbi
, *wbi2
;
2656 pr_debug("Return write for disc %d\n", i
);
2657 spin_lock_irq(&conf
->device_lock
);
2659 dev
->written
= NULL
;
2660 while (wbi
&& wbi
->bi_sector
<
2661 dev
->sector
+ STRIPE_SECTORS
) {
2662 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2663 if (!raid5_dec_bi_phys_segments(wbi
)) {
2664 md_write_end(conf
->mddev
);
2665 wbi
->bi_next
= *return_bi
;
2670 if (dev
->towrite
== NULL
)
2672 spin_unlock_irq(&conf
->device_lock
);
2674 bitmap_endwrite(conf
->mddev
->bitmap
,
2677 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
2682 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2683 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2684 md_wakeup_thread(conf
->mddev
->thread
);
2687 static void handle_stripe_dirtying(struct r5conf
*conf
,
2688 struct stripe_head
*sh
,
2689 struct stripe_head_state
*s
,
2692 int rmw
= 0, rcw
= 0, i
;
2693 if (conf
->max_degraded
== 2) {
2694 /* RAID6 requires 'rcw' in current implementation
2695 * Calculate the real rcw later - for now fake it
2696 * look like rcw is cheaper
2699 } else for (i
= disks
; i
--; ) {
2700 /* would I have to read this buffer for read_modify_write */
2701 struct r5dev
*dev
= &sh
->dev
[i
];
2702 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2703 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2704 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2705 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2706 if (test_bit(R5_Insync
, &dev
->flags
))
2709 rmw
+= 2*disks
; /* cannot read it */
2711 /* Would I have to read this buffer for reconstruct_write */
2712 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
2713 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2714 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2715 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2716 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2721 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2722 (unsigned long long)sh
->sector
, rmw
, rcw
);
2723 set_bit(STRIPE_HANDLE
, &sh
->state
);
2724 if (rmw
< rcw
&& rmw
> 0)
2725 /* prefer read-modify-write, but need to get some data */
2726 for (i
= disks
; i
--; ) {
2727 struct r5dev
*dev
= &sh
->dev
[i
];
2728 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2729 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2730 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2731 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2732 test_bit(R5_Insync
, &dev
->flags
)) {
2734 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2735 pr_debug("Read_old block "
2736 "%d for r-m-w\n", i
);
2737 set_bit(R5_LOCKED
, &dev
->flags
);
2738 set_bit(R5_Wantread
, &dev
->flags
);
2741 set_bit(STRIPE_DELAYED
, &sh
->state
);
2742 set_bit(STRIPE_HANDLE
, &sh
->state
);
2746 if (rcw
<= rmw
&& rcw
> 0) {
2747 /* want reconstruct write, but need to get some data */
2749 for (i
= disks
; i
--; ) {
2750 struct r5dev
*dev
= &sh
->dev
[i
];
2751 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2752 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
2753 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2754 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2755 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2757 if (!test_bit(R5_Insync
, &dev
->flags
))
2758 continue; /* it's a failed drive */
2760 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2761 pr_debug("Read_old block "
2762 "%d for Reconstruct\n", i
);
2763 set_bit(R5_LOCKED
, &dev
->flags
);
2764 set_bit(R5_Wantread
, &dev
->flags
);
2767 set_bit(STRIPE_DELAYED
, &sh
->state
);
2768 set_bit(STRIPE_HANDLE
, &sh
->state
);
2773 /* now if nothing is locked, and if we have enough data,
2774 * we can start a write request
2776 /* since handle_stripe can be called at any time we need to handle the
2777 * case where a compute block operation has been submitted and then a
2778 * subsequent call wants to start a write request. raid_run_ops only
2779 * handles the case where compute block and reconstruct are requested
2780 * simultaneously. If this is not the case then new writes need to be
2781 * held off until the compute completes.
2783 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
2784 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
2785 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
2786 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
2789 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
2790 struct stripe_head_state
*s
, int disks
)
2792 struct r5dev
*dev
= NULL
;
2794 set_bit(STRIPE_HANDLE
, &sh
->state
);
2796 switch (sh
->check_state
) {
2797 case check_state_idle
:
2798 /* start a new check operation if there are no failures */
2799 if (s
->failed
== 0) {
2800 BUG_ON(s
->uptodate
!= disks
);
2801 sh
->check_state
= check_state_run
;
2802 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2803 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
2807 dev
= &sh
->dev
[s
->failed_num
[0]];
2809 case check_state_compute_result
:
2810 sh
->check_state
= check_state_idle
;
2812 dev
= &sh
->dev
[sh
->pd_idx
];
2814 /* check that a write has not made the stripe insync */
2815 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2818 /* either failed parity check, or recovery is happening */
2819 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
2820 BUG_ON(s
->uptodate
!= disks
);
2822 set_bit(R5_LOCKED
, &dev
->flags
);
2824 set_bit(R5_Wantwrite
, &dev
->flags
);
2826 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2827 set_bit(STRIPE_INSYNC
, &sh
->state
);
2829 case check_state_run
:
2830 break; /* we will be called again upon completion */
2831 case check_state_check_result
:
2832 sh
->check_state
= check_state_idle
;
2834 /* if a failure occurred during the check operation, leave
2835 * STRIPE_INSYNC not set and let the stripe be handled again
2840 /* handle a successful check operation, if parity is correct
2841 * we are done. Otherwise update the mismatch count and repair
2842 * parity if !MD_RECOVERY_CHECK
2844 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
2845 /* parity is correct (on disc,
2846 * not in buffer any more)
2848 set_bit(STRIPE_INSYNC
, &sh
->state
);
2850 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2851 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2852 /* don't try to repair!! */
2853 set_bit(STRIPE_INSYNC
, &sh
->state
);
2855 sh
->check_state
= check_state_compute_run
;
2856 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2857 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2858 set_bit(R5_Wantcompute
,
2859 &sh
->dev
[sh
->pd_idx
].flags
);
2860 sh
->ops
.target
= sh
->pd_idx
;
2861 sh
->ops
.target2
= -1;
2866 case check_state_compute_run
:
2869 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
2870 __func__
, sh
->check_state
,
2871 (unsigned long long) sh
->sector
);
2877 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
2878 struct stripe_head_state
*s
,
2881 int pd_idx
= sh
->pd_idx
;
2882 int qd_idx
= sh
->qd_idx
;
2885 set_bit(STRIPE_HANDLE
, &sh
->state
);
2887 BUG_ON(s
->failed
> 2);
2889 /* Want to check and possibly repair P and Q.
2890 * However there could be one 'failed' device, in which
2891 * case we can only check one of them, possibly using the
2892 * other to generate missing data
2895 switch (sh
->check_state
) {
2896 case check_state_idle
:
2897 /* start a new check operation if there are < 2 failures */
2898 if (s
->failed
== s
->q_failed
) {
2899 /* The only possible failed device holds Q, so it
2900 * makes sense to check P (If anything else were failed,
2901 * we would have used P to recreate it).
2903 sh
->check_state
= check_state_run
;
2905 if (!s
->q_failed
&& s
->failed
< 2) {
2906 /* Q is not failed, and we didn't use it to generate
2907 * anything, so it makes sense to check it
2909 if (sh
->check_state
== check_state_run
)
2910 sh
->check_state
= check_state_run_pq
;
2912 sh
->check_state
= check_state_run_q
;
2915 /* discard potentially stale zero_sum_result */
2916 sh
->ops
.zero_sum_result
= 0;
2918 if (sh
->check_state
== check_state_run
) {
2919 /* async_xor_zero_sum destroys the contents of P */
2920 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2923 if (sh
->check_state
>= check_state_run
&&
2924 sh
->check_state
<= check_state_run_pq
) {
2925 /* async_syndrome_zero_sum preserves P and Q, so
2926 * no need to mark them !uptodate here
2928 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2932 /* we have 2-disk failure */
2933 BUG_ON(s
->failed
!= 2);
2935 case check_state_compute_result
:
2936 sh
->check_state
= check_state_idle
;
2938 /* check that a write has not made the stripe insync */
2939 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2942 /* now write out any block on a failed drive,
2943 * or P or Q if they were recomputed
2945 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
2946 if (s
->failed
== 2) {
2947 dev
= &sh
->dev
[s
->failed_num
[1]];
2949 set_bit(R5_LOCKED
, &dev
->flags
);
2950 set_bit(R5_Wantwrite
, &dev
->flags
);
2952 if (s
->failed
>= 1) {
2953 dev
= &sh
->dev
[s
->failed_num
[0]];
2955 set_bit(R5_LOCKED
, &dev
->flags
);
2956 set_bit(R5_Wantwrite
, &dev
->flags
);
2958 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
2959 dev
= &sh
->dev
[pd_idx
];
2961 set_bit(R5_LOCKED
, &dev
->flags
);
2962 set_bit(R5_Wantwrite
, &dev
->flags
);
2964 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
2965 dev
= &sh
->dev
[qd_idx
];
2967 set_bit(R5_LOCKED
, &dev
->flags
);
2968 set_bit(R5_Wantwrite
, &dev
->flags
);
2970 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2972 set_bit(STRIPE_INSYNC
, &sh
->state
);
2974 case check_state_run
:
2975 case check_state_run_q
:
2976 case check_state_run_pq
:
2977 break; /* we will be called again upon completion */
2978 case check_state_check_result
:
2979 sh
->check_state
= check_state_idle
;
2981 /* handle a successful check operation, if parity is correct
2982 * we are done. Otherwise update the mismatch count and repair
2983 * parity if !MD_RECOVERY_CHECK
2985 if (sh
->ops
.zero_sum_result
== 0) {
2986 /* both parities are correct */
2988 set_bit(STRIPE_INSYNC
, &sh
->state
);
2990 /* in contrast to the raid5 case we can validate
2991 * parity, but still have a failure to write
2994 sh
->check_state
= check_state_compute_result
;
2995 /* Returning at this point means that we may go
2996 * off and bring p and/or q uptodate again so
2997 * we make sure to check zero_sum_result again
2998 * to verify if p or q need writeback
3002 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
3003 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3004 /* don't try to repair!! */
3005 set_bit(STRIPE_INSYNC
, &sh
->state
);
3007 int *target
= &sh
->ops
.target
;
3009 sh
->ops
.target
= -1;
3010 sh
->ops
.target2
= -1;
3011 sh
->check_state
= check_state_compute_run
;
3012 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3013 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3014 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3015 set_bit(R5_Wantcompute
,
3016 &sh
->dev
[pd_idx
].flags
);
3018 target
= &sh
->ops
.target2
;
3021 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3022 set_bit(R5_Wantcompute
,
3023 &sh
->dev
[qd_idx
].flags
);
3030 case check_state_compute_run
:
3033 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3034 __func__
, sh
->check_state
,
3035 (unsigned long long) sh
->sector
);
3040 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3044 /* We have read all the blocks in this stripe and now we need to
3045 * copy some of them into a target stripe for expand.
3047 struct dma_async_tx_descriptor
*tx
= NULL
;
3048 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3049 for (i
= 0; i
< sh
->disks
; i
++)
3050 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3052 struct stripe_head
*sh2
;
3053 struct async_submit_ctl submit
;
3055 sector_t bn
= compute_blocknr(sh
, i
, 1);
3056 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3058 sh2
= get_active_stripe(conf
, s
, 0, 1, 1);
3060 /* so far only the early blocks of this stripe
3061 * have been requested. When later blocks
3062 * get requested, we will try again
3065 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3066 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3067 /* must have already done this block */
3068 release_stripe(sh2
);
3072 /* place all the copies on one channel */
3073 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
3074 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
3075 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
3078 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
3079 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
3080 for (j
= 0; j
< conf
->raid_disks
; j
++)
3081 if (j
!= sh2
->pd_idx
&&
3083 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
3085 if (j
== conf
->raid_disks
) {
3086 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
3087 set_bit(STRIPE_HANDLE
, &sh2
->state
);
3089 release_stripe(sh2
);
3092 /* done submitting copies, wait for them to complete */
3095 dma_wait_for_async_tx(tx
);
3100 * handle_stripe - do things to a stripe.
3102 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3103 * state of various bits to see what needs to be done.
3105 * return some read requests which now have data
3106 * return some write requests which are safely on storage
3107 * schedule a read on some buffers
3108 * schedule a write of some buffers
3109 * return confirmation of parity correctness
3113 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
3115 struct r5conf
*conf
= sh
->raid_conf
;
3116 int disks
= sh
->disks
;
3119 int do_recovery
= 0;
3121 memset(s
, 0, sizeof(*s
));
3123 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3124 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3125 s
->failed_num
[0] = -1;
3126 s
->failed_num
[1] = -1;
3128 /* Now to look around and see what can be done */
3130 spin_lock_irq(&conf
->device_lock
);
3131 for (i
=disks
; i
--; ) {
3132 struct md_rdev
*rdev
;
3139 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3141 dev
->toread
, dev
->towrite
, dev
->written
);
3142 /* maybe we can reply to a read
3144 * new wantfill requests are only permitted while
3145 * ops_complete_biofill is guaranteed to be inactive
3147 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
3148 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
3149 set_bit(R5_Wantfill
, &dev
->flags
);
3151 /* now count some things */
3152 if (test_bit(R5_LOCKED
, &dev
->flags
))
3154 if (test_bit(R5_UPTODATE
, &dev
->flags
))
3156 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
3158 BUG_ON(s
->compute
> 2);
3161 if (test_bit(R5_Wantfill
, &dev
->flags
))
3163 else if (dev
->toread
)
3167 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3172 /* Prefer to use the replacement for reads, but only
3173 * if it is recovered enough and has no bad blocks.
3175 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3176 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
3177 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
3178 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3179 &first_bad
, &bad_sectors
))
3180 set_bit(R5_ReadRepl
, &dev
->flags
);
3183 set_bit(R5_NeedReplace
, &dev
->flags
);
3184 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3185 clear_bit(R5_ReadRepl
, &dev
->flags
);
3187 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
3190 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3191 &first_bad
, &bad_sectors
);
3192 if (s
->blocked_rdev
== NULL
3193 && (test_bit(Blocked
, &rdev
->flags
)
3196 set_bit(BlockedBadBlocks
,
3198 s
->blocked_rdev
= rdev
;
3199 atomic_inc(&rdev
->nr_pending
);
3202 clear_bit(R5_Insync
, &dev
->flags
);
3206 /* also not in-sync */
3207 if (!test_bit(WriteErrorSeen
, &rdev
->flags
)) {
3208 /* treat as in-sync, but with a read error
3209 * which we can now try to correct
3211 set_bit(R5_Insync
, &dev
->flags
);
3212 set_bit(R5_ReadError
, &dev
->flags
);
3214 } else if (test_bit(In_sync
, &rdev
->flags
))
3215 set_bit(R5_Insync
, &dev
->flags
);
3216 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
3217 /* in sync if before recovery_offset */
3218 set_bit(R5_Insync
, &dev
->flags
);
3219 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
3220 test_bit(R5_Expanded
, &dev
->flags
))
3221 /* If we've reshaped into here, we assume it is Insync.
3222 * We will shortly update recovery_offset to make
3225 set_bit(R5_Insync
, &dev
->flags
);
3227 if (rdev
&& test_bit(R5_WriteError
, &dev
->flags
)) {
3228 /* This flag does not apply to '.replacement'
3229 * only to .rdev, so make sure to check that*/
3230 struct md_rdev
*rdev2
= rcu_dereference(
3231 conf
->disks
[i
].rdev
);
3233 clear_bit(R5_Insync
, &dev
->flags
);
3234 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3235 s
->handle_bad_blocks
= 1;
3236 atomic_inc(&rdev2
->nr_pending
);
3238 clear_bit(R5_WriteError
, &dev
->flags
);
3240 if (rdev
&& test_bit(R5_MadeGood
, &dev
->flags
)) {
3241 /* This flag does not apply to '.replacement'
3242 * only to .rdev, so make sure to check that*/
3243 struct md_rdev
*rdev2
= rcu_dereference(
3244 conf
->disks
[i
].rdev
);
3245 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3246 s
->handle_bad_blocks
= 1;
3247 atomic_inc(&rdev2
->nr_pending
);
3249 clear_bit(R5_MadeGood
, &dev
->flags
);
3251 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3252 struct md_rdev
*rdev2
= rcu_dereference(
3253 conf
->disks
[i
].replacement
);
3254 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3255 s
->handle_bad_blocks
= 1;
3256 atomic_inc(&rdev2
->nr_pending
);
3258 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
3260 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3261 /* The ReadError flag will just be confusing now */
3262 clear_bit(R5_ReadError
, &dev
->flags
);
3263 clear_bit(R5_ReWrite
, &dev
->flags
);
3265 if (test_bit(R5_ReadError
, &dev
->flags
))
3266 clear_bit(R5_Insync
, &dev
->flags
);
3267 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3269 s
->failed_num
[s
->failed
] = i
;
3271 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
3275 spin_unlock_irq(&conf
->device_lock
);
3276 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
3277 /* If there is a failed device being replaced,
3278 * we must be recovering.
3279 * else if we are after recovery_cp, we must be syncing
3280 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3281 * else we can only be replacing
3282 * sync and recovery both need to read all devices, and so
3283 * use the same flag.
3286 sh
->sector
>= conf
->mddev
->recovery_cp
||
3287 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
3295 static void handle_stripe(struct stripe_head
*sh
)
3297 struct stripe_head_state s
;
3298 struct r5conf
*conf
= sh
->raid_conf
;
3301 int disks
= sh
->disks
;
3302 struct r5dev
*pdev
, *qdev
;
3304 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3305 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
3306 /* already being handled, ensure it gets handled
3307 * again when current action finishes */
3308 set_bit(STRIPE_HANDLE
, &sh
->state
);
3312 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3313 set_bit(STRIPE_SYNCING
, &sh
->state
);
3314 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3316 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3318 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3319 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3320 (unsigned long long)sh
->sector
, sh
->state
,
3321 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
3322 sh
->check_state
, sh
->reconstruct_state
);
3324 analyse_stripe(sh
, &s
);
3326 if (s
.handle_bad_blocks
) {
3327 set_bit(STRIPE_HANDLE
, &sh
->state
);
3331 if (unlikely(s
.blocked_rdev
)) {
3332 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3333 s
.replacing
|| s
.to_write
|| s
.written
) {
3334 set_bit(STRIPE_HANDLE
, &sh
->state
);
3337 /* There is nothing for the blocked_rdev to block */
3338 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
3339 s
.blocked_rdev
= NULL
;
3342 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
3343 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
3344 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
3347 pr_debug("locked=%d uptodate=%d to_read=%d"
3348 " to_write=%d failed=%d failed_num=%d,%d\n",
3349 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3350 s
.failed_num
[0], s
.failed_num
[1]);
3351 /* check if the array has lost more than max_degraded devices and,
3352 * if so, some requests might need to be failed.
3354 if (s
.failed
> conf
->max_degraded
) {
3355 sh
->check_state
= 0;
3356 sh
->reconstruct_state
= 0;
3357 if (s
.to_read
+s
.to_write
+s
.written
)
3358 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
3359 if (s
.syncing
+ s
.replacing
)
3360 handle_failed_sync(conf
, sh
, &s
);
3364 * might be able to return some write requests if the parity blocks
3365 * are safe, or on a failed drive
3367 pdev
= &sh
->dev
[sh
->pd_idx
];
3368 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
3369 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
3370 qdev
= &sh
->dev
[sh
->qd_idx
];
3371 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
3372 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
3376 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3377 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3378 && test_bit(R5_UPTODATE
, &pdev
->flags
)))) &&
3379 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3380 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3381 && test_bit(R5_UPTODATE
, &qdev
->flags
)))))
3382 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
3384 /* Now we might consider reading some blocks, either to check/generate
3385 * parity, or to satisfy requests
3386 * or to load a block that is being partially written.
3388 if (s
.to_read
|| s
.non_overwrite
3389 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
3390 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
3393 handle_stripe_fill(sh
, &s
, disks
);
3395 /* Now we check to see if any write operations have recently
3399 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
3401 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
3402 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
3403 sh
->reconstruct_state
= reconstruct_state_idle
;
3405 /* All the 'written' buffers and the parity block are ready to
3406 * be written back to disk
3408 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
));
3409 BUG_ON(sh
->qd_idx
>= 0 &&
3410 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
));
3411 for (i
= disks
; i
--; ) {
3412 struct r5dev
*dev
= &sh
->dev
[i
];
3413 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
3414 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3416 pr_debug("Writing block %d\n", i
);
3417 set_bit(R5_Wantwrite
, &dev
->flags
);
3420 if (!test_bit(R5_Insync
, &dev
->flags
) ||
3421 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
3423 set_bit(STRIPE_INSYNC
, &sh
->state
);
3426 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3427 s
.dec_preread_active
= 1;
3430 /* Now to consider new write requests and what else, if anything
3431 * should be read. We do not handle new writes when:
3432 * 1/ A 'write' operation (copy+xor) is already in flight.
3433 * 2/ A 'check' operation is in flight, as it may clobber the parity
3436 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
3437 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
3439 /* maybe we need to check and possibly fix the parity for this stripe
3440 * Any reads will already have been scheduled, so we just see if enough
3441 * data is available. The parity check is held off while parity
3442 * dependent operations are in flight.
3444 if (sh
->check_state
||
3445 (s
.syncing
&& s
.locked
== 0 &&
3446 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3447 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
3448 if (conf
->level
== 6)
3449 handle_parity_checks6(conf
, sh
, &s
, disks
);
3451 handle_parity_checks5(conf
, sh
, &s
, disks
);
3454 if (s
.replacing
&& s
.locked
== 0
3455 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3456 /* Write out to replacement devices where possible */
3457 for (i
= 0; i
< conf
->raid_disks
; i
++)
3458 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
) &&
3459 test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
3460 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
3461 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3464 set_bit(STRIPE_INSYNC
, &sh
->state
);
3466 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
3467 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3468 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3469 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3472 /* If the failed drives are just a ReadError, then we might need
3473 * to progress the repair/check process
3475 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
3476 for (i
= 0; i
< s
.failed
; i
++) {
3477 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
3478 if (test_bit(R5_ReadError
, &dev
->flags
)
3479 && !test_bit(R5_LOCKED
, &dev
->flags
)
3480 && test_bit(R5_UPTODATE
, &dev
->flags
)
3482 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3483 set_bit(R5_Wantwrite
, &dev
->flags
);
3484 set_bit(R5_ReWrite
, &dev
->flags
);
3485 set_bit(R5_LOCKED
, &dev
->flags
);
3488 /* let's read it back */
3489 set_bit(R5_Wantread
, &dev
->flags
);
3490 set_bit(R5_LOCKED
, &dev
->flags
);
3497 /* Finish reconstruct operations initiated by the expansion process */
3498 if (sh
->reconstruct_state
== reconstruct_state_result
) {
3499 struct stripe_head
*sh_src
3500 = get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
3501 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
3502 /* sh cannot be written until sh_src has been read.
3503 * so arrange for sh to be delayed a little
3505 set_bit(STRIPE_DELAYED
, &sh
->state
);
3506 set_bit(STRIPE_HANDLE
, &sh
->state
);
3507 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
3509 atomic_inc(&conf
->preread_active_stripes
);
3510 release_stripe(sh_src
);
3514 release_stripe(sh_src
);
3516 sh
->reconstruct_state
= reconstruct_state_idle
;
3517 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3518 for (i
= conf
->raid_disks
; i
--; ) {
3519 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3520 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3525 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
3526 !sh
->reconstruct_state
) {
3527 /* Need to write out all blocks after computing parity */
3528 sh
->disks
= conf
->raid_disks
;
3529 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3530 schedule_reconstruction(sh
, &s
, 1, 1);
3531 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
3532 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3533 atomic_dec(&conf
->reshape_stripes
);
3534 wake_up(&conf
->wait_for_overlap
);
3535 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3538 if (s
.expanding
&& s
.locked
== 0 &&
3539 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3540 handle_stripe_expansion(conf
, sh
);
3543 /* wait for this device to become unblocked */
3544 if (conf
->mddev
->external
&& unlikely(s
.blocked_rdev
))
3545 md_wait_for_blocked_rdev(s
.blocked_rdev
, conf
->mddev
);
3547 if (s
.handle_bad_blocks
)
3548 for (i
= disks
; i
--; ) {
3549 struct md_rdev
*rdev
;
3550 struct r5dev
*dev
= &sh
->dev
[i
];
3551 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
3552 /* We own a safe reference to the rdev */
3553 rdev
= conf
->disks
[i
].rdev
;
3554 if (!rdev_set_badblocks(rdev
, sh
->sector
,
3556 md_error(conf
->mddev
, rdev
);
3557 rdev_dec_pending(rdev
, conf
->mddev
);
3559 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
3560 rdev
= conf
->disks
[i
].rdev
;
3561 rdev_clear_badblocks(rdev
, sh
->sector
,
3563 rdev_dec_pending(rdev
, conf
->mddev
);
3565 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3566 rdev
= conf
->disks
[i
].replacement
;
3568 /* rdev have been moved down */
3569 rdev
= conf
->disks
[i
].rdev
;
3570 rdev_clear_badblocks(rdev
, sh
->sector
,
3572 rdev_dec_pending(rdev
, conf
->mddev
);
3577 raid_run_ops(sh
, s
.ops_request
);
3581 if (s
.dec_preread_active
) {
3582 /* We delay this until after ops_run_io so that if make_request
3583 * is waiting on a flush, it won't continue until the writes
3584 * have actually been submitted.
3586 atomic_dec(&conf
->preread_active_stripes
);
3587 if (atomic_read(&conf
->preread_active_stripes
) <
3589 md_wakeup_thread(conf
->mddev
->thread
);
3592 return_io(s
.return_bi
);
3594 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
3597 static void raid5_activate_delayed(struct r5conf
*conf
)
3599 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
3600 while (!list_empty(&conf
->delayed_list
)) {
3601 struct list_head
*l
= conf
->delayed_list
.next
;
3602 struct stripe_head
*sh
;
3603 sh
= list_entry(l
, struct stripe_head
, lru
);
3605 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3606 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3607 atomic_inc(&conf
->preread_active_stripes
);
3608 list_add_tail(&sh
->lru
, &conf
->hold_list
);
3613 static void activate_bit_delay(struct r5conf
*conf
)
3615 /* device_lock is held */
3616 struct list_head head
;
3617 list_add(&head
, &conf
->bitmap_list
);
3618 list_del_init(&conf
->bitmap_list
);
3619 while (!list_empty(&head
)) {
3620 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
3621 list_del_init(&sh
->lru
);
3622 atomic_inc(&sh
->count
);
3623 __release_stripe(conf
, sh
);
3627 int md_raid5_congested(struct mddev
*mddev
, int bits
)
3629 struct r5conf
*conf
= mddev
->private;
3631 /* No difference between reads and writes. Just check
3632 * how busy the stripe_cache is
3635 if (conf
->inactive_blocked
)
3639 if (list_empty_careful(&conf
->inactive_list
))
3644 EXPORT_SYMBOL_GPL(md_raid5_congested
);
3646 static int raid5_congested(void *data
, int bits
)
3648 struct mddev
*mddev
= data
;
3650 return mddev_congested(mddev
, bits
) ||
3651 md_raid5_congested(mddev
, bits
);
3654 /* We want read requests to align with chunks where possible,
3655 * but write requests don't need to.
3657 static int raid5_mergeable_bvec(struct request_queue
*q
,
3658 struct bvec_merge_data
*bvm
,
3659 struct bio_vec
*biovec
)
3661 struct mddev
*mddev
= q
->queuedata
;
3662 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
3664 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3665 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
3667 if ((bvm
->bi_rw
& 1) == WRITE
)
3668 return biovec
->bv_len
; /* always allow writes to be mergeable */
3670 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3671 chunk_sectors
= mddev
->new_chunk_sectors
;
3672 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
3673 if (max
< 0) max
= 0;
3674 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
3675 return biovec
->bv_len
;
3681 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
3683 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
3684 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3685 unsigned int bio_sectors
= bio
->bi_size
>> 9;
3687 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3688 chunk_sectors
= mddev
->new_chunk_sectors
;
3689 return chunk_sectors
>=
3690 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
3694 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3695 * later sampled by raid5d.
3697 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
3699 unsigned long flags
;
3701 spin_lock_irqsave(&conf
->device_lock
, flags
);
3703 bi
->bi_next
= conf
->retry_read_aligned_list
;
3704 conf
->retry_read_aligned_list
= bi
;
3706 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3707 md_wakeup_thread(conf
->mddev
->thread
);
3711 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
3715 bi
= conf
->retry_read_aligned
;
3717 conf
->retry_read_aligned
= NULL
;
3720 bi
= conf
->retry_read_aligned_list
;
3722 conf
->retry_read_aligned_list
= bi
->bi_next
;
3725 * this sets the active strip count to 1 and the processed
3726 * strip count to zero (upper 8 bits)
3728 bi
->bi_phys_segments
= 1; /* biased count of active stripes */
3736 * The "raid5_align_endio" should check if the read succeeded and if it
3737 * did, call bio_endio on the original bio (having bio_put the new bio
3739 * If the read failed..
3741 static void raid5_align_endio(struct bio
*bi
, int error
)
3743 struct bio
* raid_bi
= bi
->bi_private
;
3744 struct mddev
*mddev
;
3745 struct r5conf
*conf
;
3746 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
3747 struct md_rdev
*rdev
;
3751 rdev
= (void*)raid_bi
->bi_next
;
3752 raid_bi
->bi_next
= NULL
;
3753 mddev
= rdev
->mddev
;
3754 conf
= mddev
->private;
3756 rdev_dec_pending(rdev
, conf
->mddev
);
3758 if (!error
&& uptodate
) {
3759 bio_endio(raid_bi
, 0);
3760 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
3761 wake_up(&conf
->wait_for_stripe
);
3766 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3768 add_bio_to_retry(raid_bi
, conf
);
3771 static int bio_fits_rdev(struct bio
*bi
)
3773 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
3775 if ((bi
->bi_size
>>9) > queue_max_sectors(q
))
3777 blk_recount_segments(q
, bi
);
3778 if (bi
->bi_phys_segments
> queue_max_segments(q
))
3781 if (q
->merge_bvec_fn
)
3782 /* it's too hard to apply the merge_bvec_fn at this stage,
3791 static int chunk_aligned_read(struct mddev
*mddev
, struct bio
* raid_bio
)
3793 struct r5conf
*conf
= mddev
->private;
3795 struct bio
* align_bi
;
3796 struct md_rdev
*rdev
;
3797 sector_t end_sector
;
3799 if (!in_chunk_boundary(mddev
, raid_bio
)) {
3800 pr_debug("chunk_aligned_read : non aligned\n");
3804 * use bio_clone_mddev to make a copy of the bio
3806 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
3810 * set bi_end_io to a new function, and set bi_private to the
3813 align_bi
->bi_end_io
= raid5_align_endio
;
3814 align_bi
->bi_private
= raid_bio
;
3818 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
3822 end_sector
= align_bi
->bi_sector
+ (align_bi
->bi_size
>> 9);
3824 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
3825 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
3826 rdev
->recovery_offset
< end_sector
) {
3827 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
3829 (test_bit(Faulty
, &rdev
->flags
) ||
3830 !(test_bit(In_sync
, &rdev
->flags
) ||
3831 rdev
->recovery_offset
>= end_sector
)))
3838 atomic_inc(&rdev
->nr_pending
);
3840 raid_bio
->bi_next
= (void*)rdev
;
3841 align_bi
->bi_bdev
= rdev
->bdev
;
3842 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
3843 align_bi
->bi_sector
+= rdev
->data_offset
;
3845 if (!bio_fits_rdev(align_bi
) ||
3846 is_badblock(rdev
, align_bi
->bi_sector
, align_bi
->bi_size
>>9,
3847 &first_bad
, &bad_sectors
)) {
3848 /* too big in some way, or has a known bad block */
3850 rdev_dec_pending(rdev
, mddev
);
3854 spin_lock_irq(&conf
->device_lock
);
3855 wait_event_lock_irq(conf
->wait_for_stripe
,
3857 conf
->device_lock
, /* nothing */);
3858 atomic_inc(&conf
->active_aligned_reads
);
3859 spin_unlock_irq(&conf
->device_lock
);
3861 generic_make_request(align_bi
);
3870 /* __get_priority_stripe - get the next stripe to process
3872 * Full stripe writes are allowed to pass preread active stripes up until
3873 * the bypass_threshold is exceeded. In general the bypass_count
3874 * increments when the handle_list is handled before the hold_list; however, it
3875 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3876 * stripe with in flight i/o. The bypass_count will be reset when the
3877 * head of the hold_list has changed, i.e. the head was promoted to the
3880 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
)
3882 struct stripe_head
*sh
;
3884 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3886 list_empty(&conf
->handle_list
) ? "empty" : "busy",
3887 list_empty(&conf
->hold_list
) ? "empty" : "busy",
3888 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
3890 if (!list_empty(&conf
->handle_list
)) {
3891 sh
= list_entry(conf
->handle_list
.next
, typeof(*sh
), lru
);
3893 if (list_empty(&conf
->hold_list
))
3894 conf
->bypass_count
= 0;
3895 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
3896 if (conf
->hold_list
.next
== conf
->last_hold
)
3897 conf
->bypass_count
++;
3899 conf
->last_hold
= conf
->hold_list
.next
;
3900 conf
->bypass_count
-= conf
->bypass_threshold
;
3901 if (conf
->bypass_count
< 0)
3902 conf
->bypass_count
= 0;
3905 } else if (!list_empty(&conf
->hold_list
) &&
3906 ((conf
->bypass_threshold
&&
3907 conf
->bypass_count
> conf
->bypass_threshold
) ||
3908 atomic_read(&conf
->pending_full_writes
) == 0)) {
3909 sh
= list_entry(conf
->hold_list
.next
,
3911 conf
->bypass_count
-= conf
->bypass_threshold
;
3912 if (conf
->bypass_count
< 0)
3913 conf
->bypass_count
= 0;
3917 list_del_init(&sh
->lru
);
3918 atomic_inc(&sh
->count
);
3919 BUG_ON(atomic_read(&sh
->count
) != 1);
3923 static void make_request(struct mddev
*mddev
, struct bio
* bi
)
3925 struct r5conf
*conf
= mddev
->private;
3927 sector_t new_sector
;
3928 sector_t logical_sector
, last_sector
;
3929 struct stripe_head
*sh
;
3930 const int rw
= bio_data_dir(bi
);
3934 if (unlikely(bi
->bi_rw
& REQ_FLUSH
)) {
3935 md_flush_request(mddev
, bi
);
3939 md_write_start(mddev
, bi
);
3942 mddev
->reshape_position
== MaxSector
&&
3943 chunk_aligned_read(mddev
,bi
))
3946 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
3947 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
3949 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
3951 plugged
= mddev_check_plugged(mddev
);
3952 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
3954 int disks
, data_disks
;
3959 disks
= conf
->raid_disks
;
3960 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
3961 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
3962 /* spinlock is needed as reshape_progress may be
3963 * 64bit on a 32bit platform, and so it might be
3964 * possible to see a half-updated value
3965 * Of course reshape_progress could change after
3966 * the lock is dropped, so once we get a reference
3967 * to the stripe that we think it is, we will have
3970 spin_lock_irq(&conf
->device_lock
);
3971 if (mddev
->delta_disks
< 0
3972 ? logical_sector
< conf
->reshape_progress
3973 : logical_sector
>= conf
->reshape_progress
) {
3974 disks
= conf
->previous_raid_disks
;
3977 if (mddev
->delta_disks
< 0
3978 ? logical_sector
< conf
->reshape_safe
3979 : logical_sector
>= conf
->reshape_safe
) {
3980 spin_unlock_irq(&conf
->device_lock
);
3985 spin_unlock_irq(&conf
->device_lock
);
3987 data_disks
= disks
- conf
->max_degraded
;
3989 new_sector
= raid5_compute_sector(conf
, logical_sector
,
3992 pr_debug("raid456: make_request, sector %llu logical %llu\n",
3993 (unsigned long long)new_sector
,
3994 (unsigned long long)logical_sector
);
3996 sh
= get_active_stripe(conf
, new_sector
, previous
,
3997 (bi
->bi_rw
&RWA_MASK
), 0);
3999 if (unlikely(previous
)) {
4000 /* expansion might have moved on while waiting for a
4001 * stripe, so we must do the range check again.
4002 * Expansion could still move past after this
4003 * test, but as we are holding a reference to
4004 * 'sh', we know that if that happens,
4005 * STRIPE_EXPANDING will get set and the expansion
4006 * won't proceed until we finish with the stripe.
4009 spin_lock_irq(&conf
->device_lock
);
4010 if (mddev
->delta_disks
< 0
4011 ? logical_sector
>= conf
->reshape_progress
4012 : logical_sector
< conf
->reshape_progress
)
4013 /* mismatch, need to try again */
4015 spin_unlock_irq(&conf
->device_lock
);
4024 logical_sector
>= mddev
->suspend_lo
&&
4025 logical_sector
< mddev
->suspend_hi
) {
4027 /* As the suspend_* range is controlled by
4028 * userspace, we want an interruptible
4031 flush_signals(current
);
4032 prepare_to_wait(&conf
->wait_for_overlap
,
4033 &w
, TASK_INTERRUPTIBLE
);
4034 if (logical_sector
>= mddev
->suspend_lo
&&
4035 logical_sector
< mddev
->suspend_hi
)
4040 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
4041 !add_stripe_bio(sh
, bi
, dd_idx
, rw
)) {
4042 /* Stripe is busy expanding or
4043 * add failed due to overlap. Flush everything
4046 md_wakeup_thread(mddev
->thread
);
4051 finish_wait(&conf
->wait_for_overlap
, &w
);
4052 set_bit(STRIPE_HANDLE
, &sh
->state
);
4053 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4054 if ((bi
->bi_rw
& REQ_SYNC
) &&
4055 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4056 atomic_inc(&conf
->preread_active_stripes
);
4059 /* cannot get stripe for read-ahead, just give-up */
4060 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4061 finish_wait(&conf
->wait_for_overlap
, &w
);
4067 md_wakeup_thread(mddev
->thread
);
4069 spin_lock_irq(&conf
->device_lock
);
4070 remaining
= raid5_dec_bi_phys_segments(bi
);
4071 spin_unlock_irq(&conf
->device_lock
);
4072 if (remaining
== 0) {
4075 md_write_end(mddev
);
4081 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
4083 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
4085 /* reshaping is quite different to recovery/resync so it is
4086 * handled quite separately ... here.
4088 * On each call to sync_request, we gather one chunk worth of
4089 * destination stripes and flag them as expanding.
4090 * Then we find all the source stripes and request reads.
4091 * As the reads complete, handle_stripe will copy the data
4092 * into the destination stripe and release that stripe.
4094 struct r5conf
*conf
= mddev
->private;
4095 struct stripe_head
*sh
;
4096 sector_t first_sector
, last_sector
;
4097 int raid_disks
= conf
->previous_raid_disks
;
4098 int data_disks
= raid_disks
- conf
->max_degraded
;
4099 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4102 sector_t writepos
, readpos
, safepos
;
4103 sector_t stripe_addr
;
4104 int reshape_sectors
;
4105 struct list_head stripes
;
4107 if (sector_nr
== 0) {
4108 /* If restarting in the middle, skip the initial sectors */
4109 if (mddev
->delta_disks
< 0 &&
4110 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
4111 sector_nr
= raid5_size(mddev
, 0, 0)
4112 - conf
->reshape_progress
;
4113 } else if (mddev
->delta_disks
>= 0 &&
4114 conf
->reshape_progress
> 0)
4115 sector_nr
= conf
->reshape_progress
;
4116 sector_div(sector_nr
, new_data_disks
);
4118 mddev
->curr_resync_completed
= sector_nr
;
4119 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4125 /* We need to process a full chunk at a time.
4126 * If old and new chunk sizes differ, we need to process the
4129 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
)
4130 reshape_sectors
= mddev
->new_chunk_sectors
;
4132 reshape_sectors
= mddev
->chunk_sectors
;
4134 /* we update the metadata when there is more than 3Meg
4135 * in the block range (that is rather arbitrary, should
4136 * probably be time based) or when the data about to be
4137 * copied would over-write the source of the data at
4138 * the front of the range.
4139 * i.e. one new_stripe along from reshape_progress new_maps
4140 * to after where reshape_safe old_maps to
4142 writepos
= conf
->reshape_progress
;
4143 sector_div(writepos
, new_data_disks
);
4144 readpos
= conf
->reshape_progress
;
4145 sector_div(readpos
, data_disks
);
4146 safepos
= conf
->reshape_safe
;
4147 sector_div(safepos
, data_disks
);
4148 if (mddev
->delta_disks
< 0) {
4149 writepos
-= min_t(sector_t
, reshape_sectors
, writepos
);
4150 readpos
+= reshape_sectors
;
4151 safepos
+= reshape_sectors
;
4153 writepos
+= reshape_sectors
;
4154 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
4155 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
4158 /* 'writepos' is the most advanced device address we might write.
4159 * 'readpos' is the least advanced device address we might read.
4160 * 'safepos' is the least address recorded in the metadata as having
4162 * If 'readpos' is behind 'writepos', then there is no way that we can
4163 * ensure safety in the face of a crash - that must be done by userspace
4164 * making a backup of the data. So in that case there is no particular
4165 * rush to update metadata.
4166 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4167 * update the metadata to advance 'safepos' to match 'readpos' so that
4168 * we can be safe in the event of a crash.
4169 * So we insist on updating metadata if safepos is behind writepos and
4170 * readpos is beyond writepos.
4171 * In any case, update the metadata every 10 seconds.
4172 * Maybe that number should be configurable, but I'm not sure it is
4173 * worth it.... maybe it could be a multiple of safemode_delay???
4175 if ((mddev
->delta_disks
< 0
4176 ? (safepos
> writepos
&& readpos
< writepos
)
4177 : (safepos
< writepos
&& readpos
> writepos
)) ||
4178 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4179 /* Cannot proceed until we've updated the superblock... */
4180 wait_event(conf
->wait_for_overlap
,
4181 atomic_read(&conf
->reshape_stripes
)==0);
4182 mddev
->reshape_position
= conf
->reshape_progress
;
4183 mddev
->curr_resync_completed
= sector_nr
;
4184 conf
->reshape_checkpoint
= jiffies
;
4185 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4186 md_wakeup_thread(mddev
->thread
);
4187 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4188 kthread_should_stop());
4189 spin_lock_irq(&conf
->device_lock
);
4190 conf
->reshape_safe
= mddev
->reshape_position
;
4191 spin_unlock_irq(&conf
->device_lock
);
4192 wake_up(&conf
->wait_for_overlap
);
4193 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4196 if (mddev
->delta_disks
< 0) {
4197 BUG_ON(conf
->reshape_progress
== 0);
4198 stripe_addr
= writepos
;
4199 BUG_ON((mddev
->dev_sectors
&
4200 ~((sector_t
)reshape_sectors
- 1))
4201 - reshape_sectors
- stripe_addr
4204 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
4205 stripe_addr
= sector_nr
;
4207 INIT_LIST_HEAD(&stripes
);
4208 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
4210 int skipped_disk
= 0;
4211 sh
= get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
4212 set_bit(STRIPE_EXPANDING
, &sh
->state
);
4213 atomic_inc(&conf
->reshape_stripes
);
4214 /* If any of this stripe is beyond the end of the old
4215 * array, then we need to zero those blocks
4217 for (j
=sh
->disks
; j
--;) {
4219 if (j
== sh
->pd_idx
)
4221 if (conf
->level
== 6 &&
4224 s
= compute_blocknr(sh
, j
, 0);
4225 if (s
< raid5_size(mddev
, 0, 0)) {
4229 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
4230 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
4231 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
4233 if (!skipped_disk
) {
4234 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4235 set_bit(STRIPE_HANDLE
, &sh
->state
);
4237 list_add(&sh
->lru
, &stripes
);
4239 spin_lock_irq(&conf
->device_lock
);
4240 if (mddev
->delta_disks
< 0)
4241 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
4243 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
4244 spin_unlock_irq(&conf
->device_lock
);
4245 /* Ok, those stripe are ready. We can start scheduling
4246 * reads on the source stripes.
4247 * The source stripes are determined by mapping the first and last
4248 * block on the destination stripes.
4251 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
4254 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
4255 * new_data_disks
- 1),
4257 if (last_sector
>= mddev
->dev_sectors
)
4258 last_sector
= mddev
->dev_sectors
- 1;
4259 while (first_sector
<= last_sector
) {
4260 sh
= get_active_stripe(conf
, first_sector
, 1, 0, 1);
4261 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4262 set_bit(STRIPE_HANDLE
, &sh
->state
);
4264 first_sector
+= STRIPE_SECTORS
;
4266 /* Now that the sources are clearly marked, we can release
4267 * the destination stripes
4269 while (!list_empty(&stripes
)) {
4270 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
4271 list_del_init(&sh
->lru
);
4274 /* If this takes us to the resync_max point where we have to pause,
4275 * then we need to write out the superblock.
4277 sector_nr
+= reshape_sectors
;
4278 if ((sector_nr
- mddev
->curr_resync_completed
) * 2
4279 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
4280 /* Cannot proceed until we've updated the superblock... */
4281 wait_event(conf
->wait_for_overlap
,
4282 atomic_read(&conf
->reshape_stripes
) == 0);
4283 mddev
->reshape_position
= conf
->reshape_progress
;
4284 mddev
->curr_resync_completed
= sector_nr
;
4285 conf
->reshape_checkpoint
= jiffies
;
4286 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4287 md_wakeup_thread(mddev
->thread
);
4288 wait_event(mddev
->sb_wait
,
4289 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
4290 || kthread_should_stop());
4291 spin_lock_irq(&conf
->device_lock
);
4292 conf
->reshape_safe
= mddev
->reshape_position
;
4293 spin_unlock_irq(&conf
->device_lock
);
4294 wake_up(&conf
->wait_for_overlap
);
4295 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4297 return reshape_sectors
;
4300 /* FIXME go_faster isn't used */
4301 static inline sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
4303 struct r5conf
*conf
= mddev
->private;
4304 struct stripe_head
*sh
;
4305 sector_t max_sector
= mddev
->dev_sectors
;
4306 sector_t sync_blocks
;
4307 int still_degraded
= 0;
4310 if (sector_nr
>= max_sector
) {
4311 /* just being told to finish up .. nothing much to do */
4313 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
4318 if (mddev
->curr_resync
< max_sector
) /* aborted */
4319 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
4321 else /* completed sync */
4323 bitmap_close_sync(mddev
->bitmap
);
4328 /* Allow raid5_quiesce to complete */
4329 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
4331 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4332 return reshape_request(mddev
, sector_nr
, skipped
);
4334 /* No need to check resync_max as we never do more than one
4335 * stripe, and as resync_max will always be on a chunk boundary,
4336 * if the check in md_do_sync didn't fire, there is no chance
4337 * of overstepping resync_max here
4340 /* if there is too many failed drives and we are trying
4341 * to resync, then assert that we are finished, because there is
4342 * nothing we can do.
4344 if (mddev
->degraded
>= conf
->max_degraded
&&
4345 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4346 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
4350 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
4351 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
4352 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
4353 /* we can skip this block, and probably more */
4354 sync_blocks
/= STRIPE_SECTORS
;
4356 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
4359 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
4361 sh
= get_active_stripe(conf
, sector_nr
, 0, 1, 0);
4363 sh
= get_active_stripe(conf
, sector_nr
, 0, 0, 0);
4364 /* make sure we don't swamp the stripe cache if someone else
4365 * is trying to get access
4367 schedule_timeout_uninterruptible(1);
4369 /* Need to check if array will still be degraded after recovery/resync
4370 * We don't need to check the 'failed' flag as when that gets set,
4373 for (i
= 0; i
< conf
->raid_disks
; i
++)
4374 if (conf
->disks
[i
].rdev
== NULL
)
4377 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
4379 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
4384 return STRIPE_SECTORS
;
4387 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
4389 /* We may not be able to submit a whole bio at once as there
4390 * may not be enough stripe_heads available.
4391 * We cannot pre-allocate enough stripe_heads as we may need
4392 * more than exist in the cache (if we allow ever large chunks).
4393 * So we do one stripe head at a time and record in
4394 * ->bi_hw_segments how many have been done.
4396 * We *know* that this entire raid_bio is in one chunk, so
4397 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4399 struct stripe_head
*sh
;
4401 sector_t sector
, logical_sector
, last_sector
;
4406 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4407 sector
= raid5_compute_sector(conf
, logical_sector
,
4409 last_sector
= raid_bio
->bi_sector
+ (raid_bio
->bi_size
>>9);
4411 for (; logical_sector
< last_sector
;
4412 logical_sector
+= STRIPE_SECTORS
,
4413 sector
+= STRIPE_SECTORS
,
4416 if (scnt
< raid5_bi_hw_segments(raid_bio
))
4417 /* already done this stripe */
4420 sh
= get_active_stripe(conf
, sector
, 0, 1, 0);
4423 /* failed to get a stripe - must wait */
4424 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4425 conf
->retry_read_aligned
= raid_bio
;
4429 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
4431 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4432 conf
->retry_read_aligned
= raid_bio
;
4440 spin_lock_irq(&conf
->device_lock
);
4441 remaining
= raid5_dec_bi_phys_segments(raid_bio
);
4442 spin_unlock_irq(&conf
->device_lock
);
4444 bio_endio(raid_bio
, 0);
4445 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4446 wake_up(&conf
->wait_for_stripe
);
4452 * This is our raid5 kernel thread.
4454 * We scan the hash table for stripes which can be handled now.
4455 * During the scan, completed stripes are saved for us by the interrupt
4456 * handler, so that they will not have to wait for our next wakeup.
4458 static void raid5d(struct mddev
*mddev
)
4460 struct stripe_head
*sh
;
4461 struct r5conf
*conf
= mddev
->private;
4463 struct blk_plug plug
;
4465 pr_debug("+++ raid5d active\n");
4467 md_check_recovery(mddev
);
4469 blk_start_plug(&plug
);
4471 spin_lock_irq(&conf
->device_lock
);
4475 if (atomic_read(&mddev
->plug_cnt
) == 0 &&
4476 !list_empty(&conf
->bitmap_list
)) {
4477 /* Now is a good time to flush some bitmap updates */
4479 spin_unlock_irq(&conf
->device_lock
);
4480 bitmap_unplug(mddev
->bitmap
);
4481 spin_lock_irq(&conf
->device_lock
);
4482 conf
->seq_write
= conf
->seq_flush
;
4483 activate_bit_delay(conf
);
4485 if (atomic_read(&mddev
->plug_cnt
) == 0)
4486 raid5_activate_delayed(conf
);
4488 while ((bio
= remove_bio_from_retry(conf
))) {
4490 spin_unlock_irq(&conf
->device_lock
);
4491 ok
= retry_aligned_read(conf
, bio
);
4492 spin_lock_irq(&conf
->device_lock
);
4498 sh
= __get_priority_stripe(conf
);
4502 spin_unlock_irq(&conf
->device_lock
);
4509 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
4510 md_check_recovery(mddev
);
4512 spin_lock_irq(&conf
->device_lock
);
4514 pr_debug("%d stripes handled\n", handled
);
4516 spin_unlock_irq(&conf
->device_lock
);
4518 async_tx_issue_pending_all();
4519 blk_finish_plug(&plug
);
4521 pr_debug("--- raid5d inactive\n");
4525 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
4527 struct r5conf
*conf
= mddev
->private;
4529 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
4535 raid5_set_cache_size(struct mddev
*mddev
, int size
)
4537 struct r5conf
*conf
= mddev
->private;
4540 if (size
<= 16 || size
> 32768)
4542 while (size
< conf
->max_nr_stripes
) {
4543 if (drop_one_stripe(conf
))
4544 conf
->max_nr_stripes
--;
4548 err
= md_allow_write(mddev
);
4551 while (size
> conf
->max_nr_stripes
) {
4552 if (grow_one_stripe(conf
))
4553 conf
->max_nr_stripes
++;
4558 EXPORT_SYMBOL(raid5_set_cache_size
);
4561 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
4563 struct r5conf
*conf
= mddev
->private;
4567 if (len
>= PAGE_SIZE
)
4572 if (strict_strtoul(page
, 10, &new))
4574 err
= raid5_set_cache_size(mddev
, new);
4580 static struct md_sysfs_entry
4581 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
4582 raid5_show_stripe_cache_size
,
4583 raid5_store_stripe_cache_size
);
4586 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
4588 struct r5conf
*conf
= mddev
->private;
4590 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
4596 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
4598 struct r5conf
*conf
= mddev
->private;
4600 if (len
>= PAGE_SIZE
)
4605 if (strict_strtoul(page
, 10, &new))
4607 if (new > conf
->max_nr_stripes
)
4609 conf
->bypass_threshold
= new;
4613 static struct md_sysfs_entry
4614 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
4616 raid5_show_preread_threshold
,
4617 raid5_store_preread_threshold
);
4620 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
4622 struct r5conf
*conf
= mddev
->private;
4624 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
4629 static struct md_sysfs_entry
4630 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
4632 static struct attribute
*raid5_attrs
[] = {
4633 &raid5_stripecache_size
.attr
,
4634 &raid5_stripecache_active
.attr
,
4635 &raid5_preread_bypass_threshold
.attr
,
4638 static struct attribute_group raid5_attrs_group
= {
4640 .attrs
= raid5_attrs
,
4644 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
4646 struct r5conf
*conf
= mddev
->private;
4649 sectors
= mddev
->dev_sectors
;
4651 /* size is defined by the smallest of previous and new size */
4652 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
4654 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
4655 sectors
&= ~((sector_t
)mddev
->new_chunk_sectors
- 1);
4656 return sectors
* (raid_disks
- conf
->max_degraded
);
4659 static void raid5_free_percpu(struct r5conf
*conf
)
4661 struct raid5_percpu
*percpu
;
4668 for_each_possible_cpu(cpu
) {
4669 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4670 safe_put_page(percpu
->spare_page
);
4671 kfree(percpu
->scribble
);
4673 #ifdef CONFIG_HOTPLUG_CPU
4674 unregister_cpu_notifier(&conf
->cpu_notify
);
4678 free_percpu(conf
->percpu
);
4681 static void free_conf(struct r5conf
*conf
)
4683 shrink_stripes(conf
);
4684 raid5_free_percpu(conf
);
4686 kfree(conf
->stripe_hashtbl
);
4690 #ifdef CONFIG_HOTPLUG_CPU
4691 static int raid456_cpu_notify(struct notifier_block
*nfb
, unsigned long action
,
4694 struct r5conf
*conf
= container_of(nfb
, struct r5conf
, cpu_notify
);
4695 long cpu
= (long)hcpu
;
4696 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4699 case CPU_UP_PREPARE
:
4700 case CPU_UP_PREPARE_FROZEN
:
4701 if (conf
->level
== 6 && !percpu
->spare_page
)
4702 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
4703 if (!percpu
->scribble
)
4704 percpu
->scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4706 if (!percpu
->scribble
||
4707 (conf
->level
== 6 && !percpu
->spare_page
)) {
4708 safe_put_page(percpu
->spare_page
);
4709 kfree(percpu
->scribble
);
4710 pr_err("%s: failed memory allocation for cpu%ld\n",
4712 return notifier_from_errno(-ENOMEM
);
4716 case CPU_DEAD_FROZEN
:
4717 safe_put_page(percpu
->spare_page
);
4718 kfree(percpu
->scribble
);
4719 percpu
->spare_page
= NULL
;
4720 percpu
->scribble
= NULL
;
4729 static int raid5_alloc_percpu(struct r5conf
*conf
)
4732 struct page
*spare_page
;
4733 struct raid5_percpu __percpu
*allcpus
;
4737 allcpus
= alloc_percpu(struct raid5_percpu
);
4740 conf
->percpu
= allcpus
;
4744 for_each_present_cpu(cpu
) {
4745 if (conf
->level
== 6) {
4746 spare_page
= alloc_page(GFP_KERNEL
);
4751 per_cpu_ptr(conf
->percpu
, cpu
)->spare_page
= spare_page
;
4753 scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4758 per_cpu_ptr(conf
->percpu
, cpu
)->scribble
= scribble
;
4760 #ifdef CONFIG_HOTPLUG_CPU
4761 conf
->cpu_notify
.notifier_call
= raid456_cpu_notify
;
4762 conf
->cpu_notify
.priority
= 0;
4764 err
= register_cpu_notifier(&conf
->cpu_notify
);
4771 static struct r5conf
*setup_conf(struct mddev
*mddev
)
4773 struct r5conf
*conf
;
4774 int raid_disk
, memory
, max_disks
;
4775 struct md_rdev
*rdev
;
4776 struct disk_info
*disk
;
4778 if (mddev
->new_level
!= 5
4779 && mddev
->new_level
!= 4
4780 && mddev
->new_level
!= 6) {
4781 printk(KERN_ERR
"md/raid:%s: raid level not set to 4/5/6 (%d)\n",
4782 mdname(mddev
), mddev
->new_level
);
4783 return ERR_PTR(-EIO
);
4785 if ((mddev
->new_level
== 5
4786 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
4787 (mddev
->new_level
== 6
4788 && !algorithm_valid_raid6(mddev
->new_layout
))) {
4789 printk(KERN_ERR
"md/raid:%s: layout %d not supported\n",
4790 mdname(mddev
), mddev
->new_layout
);
4791 return ERR_PTR(-EIO
);
4793 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
4794 printk(KERN_ERR
"md/raid:%s: not enough configured devices (%d, minimum 4)\n",
4795 mdname(mddev
), mddev
->raid_disks
);
4796 return ERR_PTR(-EINVAL
);
4799 if (!mddev
->new_chunk_sectors
||
4800 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
4801 !is_power_of_2(mddev
->new_chunk_sectors
)) {
4802 printk(KERN_ERR
"md/raid:%s: invalid chunk size %d\n",
4803 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
4804 return ERR_PTR(-EINVAL
);
4807 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
4810 spin_lock_init(&conf
->device_lock
);
4811 init_waitqueue_head(&conf
->wait_for_stripe
);
4812 init_waitqueue_head(&conf
->wait_for_overlap
);
4813 INIT_LIST_HEAD(&conf
->handle_list
);
4814 INIT_LIST_HEAD(&conf
->hold_list
);
4815 INIT_LIST_HEAD(&conf
->delayed_list
);
4816 INIT_LIST_HEAD(&conf
->bitmap_list
);
4817 INIT_LIST_HEAD(&conf
->inactive_list
);
4818 atomic_set(&conf
->active_stripes
, 0);
4819 atomic_set(&conf
->preread_active_stripes
, 0);
4820 atomic_set(&conf
->active_aligned_reads
, 0);
4821 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
4822 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
4824 conf
->raid_disks
= mddev
->raid_disks
;
4825 if (mddev
->reshape_position
== MaxSector
)
4826 conf
->previous_raid_disks
= mddev
->raid_disks
;
4828 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4829 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
4830 conf
->scribble_len
= scribble_len(max_disks
);
4832 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
4837 conf
->mddev
= mddev
;
4839 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
4842 conf
->level
= mddev
->new_level
;
4843 if (raid5_alloc_percpu(conf
) != 0)
4846 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
4848 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4849 raid_disk
= rdev
->raid_disk
;
4850 if (raid_disk
>= max_disks
4853 disk
= conf
->disks
+ raid_disk
;
4855 if (test_bit(Replacement
, &rdev
->flags
)) {
4856 if (disk
->replacement
)
4858 disk
->replacement
= rdev
;
4865 if (test_bit(In_sync
, &rdev
->flags
)) {
4866 char b
[BDEVNAME_SIZE
];
4867 printk(KERN_INFO
"md/raid:%s: device %s operational as raid"
4869 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
4870 } else if (rdev
->saved_raid_disk
!= raid_disk
)
4871 /* Cannot rely on bitmap to complete recovery */
4875 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
4876 conf
->level
= mddev
->new_level
;
4877 if (conf
->level
== 6)
4878 conf
->max_degraded
= 2;
4880 conf
->max_degraded
= 1;
4881 conf
->algorithm
= mddev
->new_layout
;
4882 conf
->max_nr_stripes
= NR_STRIPES
;
4883 conf
->reshape_progress
= mddev
->reshape_position
;
4884 if (conf
->reshape_progress
!= MaxSector
) {
4885 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
4886 conf
->prev_algo
= mddev
->layout
;
4889 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
4890 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
4891 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
4893 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4894 mdname(mddev
), memory
);
4897 printk(KERN_INFO
"md/raid:%s: allocated %dkB\n",
4898 mdname(mddev
), memory
);
4900 conf
->thread
= md_register_thread(raid5d
, mddev
, NULL
);
4901 if (!conf
->thread
) {
4903 "md/raid:%s: couldn't allocate thread.\n",
4913 return ERR_PTR(-EIO
);
4915 return ERR_PTR(-ENOMEM
);
4919 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
4922 case ALGORITHM_PARITY_0
:
4923 if (raid_disk
< max_degraded
)
4926 case ALGORITHM_PARITY_N
:
4927 if (raid_disk
>= raid_disks
- max_degraded
)
4930 case ALGORITHM_PARITY_0_6
:
4931 if (raid_disk
== 0 ||
4932 raid_disk
== raid_disks
- 1)
4935 case ALGORITHM_LEFT_ASYMMETRIC_6
:
4936 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
4937 case ALGORITHM_LEFT_SYMMETRIC_6
:
4938 case ALGORITHM_RIGHT_SYMMETRIC_6
:
4939 if (raid_disk
== raid_disks
- 1)
4945 static int run(struct mddev
*mddev
)
4947 struct r5conf
*conf
;
4948 int working_disks
= 0;
4949 int dirty_parity_disks
= 0;
4950 struct md_rdev
*rdev
;
4951 sector_t reshape_offset
= 0;
4954 if (mddev
->recovery_cp
!= MaxSector
)
4955 printk(KERN_NOTICE
"md/raid:%s: not clean"
4956 " -- starting background reconstruction\n",
4958 if (mddev
->reshape_position
!= MaxSector
) {
4959 /* Check that we can continue the reshape.
4960 * Currently only disks can change, it must
4961 * increase, and we must be past the point where
4962 * a stripe over-writes itself
4964 sector_t here_new
, here_old
;
4966 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
4968 if (mddev
->new_level
!= mddev
->level
) {
4969 printk(KERN_ERR
"md/raid:%s: unsupported reshape "
4970 "required - aborting.\n",
4974 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4975 /* reshape_position must be on a new-stripe boundary, and one
4976 * further up in new geometry must map after here in old
4979 here_new
= mddev
->reshape_position
;
4980 if (sector_div(here_new
, mddev
->new_chunk_sectors
*
4981 (mddev
->raid_disks
- max_degraded
))) {
4982 printk(KERN_ERR
"md/raid:%s: reshape_position not "
4983 "on a stripe boundary\n", mdname(mddev
));
4986 reshape_offset
= here_new
* mddev
->new_chunk_sectors
;
4987 /* here_new is the stripe we will write to */
4988 here_old
= mddev
->reshape_position
;
4989 sector_div(here_old
, mddev
->chunk_sectors
*
4990 (old_disks
-max_degraded
));
4991 /* here_old is the first stripe that we might need to read
4993 if (mddev
->delta_disks
== 0) {
4994 /* We cannot be sure it is safe to start an in-place
4995 * reshape. It is only safe if user-space if monitoring
4996 * and taking constant backups.
4997 * mdadm always starts a situation like this in
4998 * readonly mode so it can take control before
4999 * allowing any writes. So just check for that.
5001 if ((here_new
* mddev
->new_chunk_sectors
!=
5002 here_old
* mddev
->chunk_sectors
) ||
5004 printk(KERN_ERR
"md/raid:%s: in-place reshape must be started"
5005 " in read-only mode - aborting\n",
5009 } else if (mddev
->delta_disks
< 0
5010 ? (here_new
* mddev
->new_chunk_sectors
<=
5011 here_old
* mddev
->chunk_sectors
)
5012 : (here_new
* mddev
->new_chunk_sectors
>=
5013 here_old
* mddev
->chunk_sectors
)) {
5014 /* Reading from the same stripe as writing to - bad */
5015 printk(KERN_ERR
"md/raid:%s: reshape_position too early for "
5016 "auto-recovery - aborting.\n",
5020 printk(KERN_INFO
"md/raid:%s: reshape will continue\n",
5022 /* OK, we should be able to continue; */
5024 BUG_ON(mddev
->level
!= mddev
->new_level
);
5025 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
5026 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
5027 BUG_ON(mddev
->delta_disks
!= 0);
5030 if (mddev
->private == NULL
)
5031 conf
= setup_conf(mddev
);
5033 conf
= mddev
->private;
5036 return PTR_ERR(conf
);
5038 mddev
->thread
= conf
->thread
;
5039 conf
->thread
= NULL
;
5040 mddev
->private = conf
;
5042 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
5044 rdev
= conf
->disks
[i
].rdev
;
5045 if (!rdev
&& conf
->disks
[i
].replacement
) {
5046 /* The replacement is all we have yet */
5047 rdev
= conf
->disks
[i
].replacement
;
5048 conf
->disks
[i
].replacement
= NULL
;
5049 clear_bit(Replacement
, &rdev
->flags
);
5050 conf
->disks
[i
].rdev
= rdev
;
5054 if (conf
->disks
[i
].replacement
&&
5055 conf
->reshape_progress
!= MaxSector
) {
5056 /* replacements and reshape simply do not mix. */
5057 printk(KERN_ERR
"md: cannot handle concurrent "
5058 "replacement and reshape.\n");
5061 if (test_bit(In_sync
, &rdev
->flags
)) {
5065 /* This disc is not fully in-sync. However if it
5066 * just stored parity (beyond the recovery_offset),
5067 * when we don't need to be concerned about the
5068 * array being dirty.
5069 * When reshape goes 'backwards', we never have
5070 * partially completed devices, so we only need
5071 * to worry about reshape going forwards.
5073 /* Hack because v0.91 doesn't store recovery_offset properly. */
5074 if (mddev
->major_version
== 0 &&
5075 mddev
->minor_version
> 90)
5076 rdev
->recovery_offset
= reshape_offset
;
5078 if (rdev
->recovery_offset
< reshape_offset
) {
5079 /* We need to check old and new layout */
5080 if (!only_parity(rdev
->raid_disk
,
5083 conf
->max_degraded
))
5086 if (!only_parity(rdev
->raid_disk
,
5088 conf
->previous_raid_disks
,
5089 conf
->max_degraded
))
5091 dirty_parity_disks
++;
5095 * 0 for a fully functional array, 1 or 2 for a degraded array.
5097 mddev
->degraded
= calc_degraded(conf
);
5099 if (has_failed(conf
)) {
5100 printk(KERN_ERR
"md/raid:%s: not enough operational devices"
5101 " (%d/%d failed)\n",
5102 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
5106 /* device size must be a multiple of chunk size */
5107 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
5108 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5110 if (mddev
->degraded
> dirty_parity_disks
&&
5111 mddev
->recovery_cp
!= MaxSector
) {
5112 if (mddev
->ok_start_degraded
)
5114 "md/raid:%s: starting dirty degraded array"
5115 " - data corruption possible.\n",
5119 "md/raid:%s: cannot start dirty degraded array.\n",
5125 if (mddev
->degraded
== 0)
5126 printk(KERN_INFO
"md/raid:%s: raid level %d active with %d out of %d"
5127 " devices, algorithm %d\n", mdname(mddev
), conf
->level
,
5128 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
5131 printk(KERN_ALERT
"md/raid:%s: raid level %d active with %d"
5132 " out of %d devices, algorithm %d\n",
5133 mdname(mddev
), conf
->level
,
5134 mddev
->raid_disks
- mddev
->degraded
,
5135 mddev
->raid_disks
, mddev
->new_layout
);
5137 print_raid5_conf(conf
);
5139 if (conf
->reshape_progress
!= MaxSector
) {
5140 conf
->reshape_safe
= conf
->reshape_progress
;
5141 atomic_set(&conf
->reshape_stripes
, 0);
5142 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5143 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5144 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5145 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5146 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5151 /* Ok, everything is just fine now */
5152 if (mddev
->to_remove
== &raid5_attrs_group
)
5153 mddev
->to_remove
= NULL
;
5154 else if (mddev
->kobj
.sd
&&
5155 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
5157 "raid5: failed to create sysfs attributes for %s\n",
5159 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5163 /* read-ahead size must cover two whole stripes, which
5164 * is 2 * (datadisks) * chunksize where 'n' is the
5165 * number of raid devices
5167 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
5168 int stripe
= data_disks
*
5169 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
5170 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5171 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5173 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
5175 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5176 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
5178 chunk_size
= mddev
->chunk_sectors
<< 9;
5179 blk_queue_io_min(mddev
->queue
, chunk_size
);
5180 blk_queue_io_opt(mddev
->queue
, chunk_size
*
5181 (conf
->raid_disks
- conf
->max_degraded
));
5183 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5184 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5185 rdev
->data_offset
<< 9);
5190 md_unregister_thread(&mddev
->thread
);
5191 print_raid5_conf(conf
);
5193 mddev
->private = NULL
;
5194 printk(KERN_ALERT
"md/raid:%s: failed to run raid set.\n", mdname(mddev
));
5198 static int stop(struct mddev
*mddev
)
5200 struct r5conf
*conf
= mddev
->private;
5202 md_unregister_thread(&mddev
->thread
);
5204 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5206 mddev
->private = NULL
;
5207 mddev
->to_remove
= &raid5_attrs_group
;
5211 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
5213 struct r5conf
*conf
= mddev
->private;
5216 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
5217 mddev
->chunk_sectors
/ 2, mddev
->layout
);
5218 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
5219 for (i
= 0; i
< conf
->raid_disks
; i
++)
5220 seq_printf (seq
, "%s",
5221 conf
->disks
[i
].rdev
&&
5222 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
5223 seq_printf (seq
, "]");
5226 static void print_raid5_conf (struct r5conf
*conf
)
5229 struct disk_info
*tmp
;
5231 printk(KERN_DEBUG
"RAID conf printout:\n");
5233 printk("(conf==NULL)\n");
5236 printk(KERN_DEBUG
" --- level:%d rd:%d wd:%d\n", conf
->level
,
5238 conf
->raid_disks
- conf
->mddev
->degraded
);
5240 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5241 char b
[BDEVNAME_SIZE
];
5242 tmp
= conf
->disks
+ i
;
5244 printk(KERN_DEBUG
" disk %d, o:%d, dev:%s\n",
5245 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
5246 bdevname(tmp
->rdev
->bdev
, b
));
5250 static int raid5_spare_active(struct mddev
*mddev
)
5253 struct r5conf
*conf
= mddev
->private;
5254 struct disk_info
*tmp
;
5256 unsigned long flags
;
5258 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5259 tmp
= conf
->disks
+ i
;
5260 if (tmp
->replacement
5261 && tmp
->replacement
->recovery_offset
== MaxSector
5262 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
5263 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
5264 /* Replacement has just become active. */
5266 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
5269 /* Replaced device not technically faulty,
5270 * but we need to be sure it gets removed
5271 * and never re-added.
5273 set_bit(Faulty
, &tmp
->rdev
->flags
);
5274 sysfs_notify_dirent_safe(
5275 tmp
->rdev
->sysfs_state
);
5277 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
5278 } else if (tmp
->rdev
5279 && tmp
->rdev
->recovery_offset
== MaxSector
5280 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
5281 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
5283 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
5286 spin_lock_irqsave(&conf
->device_lock
, flags
);
5287 mddev
->degraded
= calc_degraded(conf
);
5288 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5289 print_raid5_conf(conf
);
5293 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5295 struct r5conf
*conf
= mddev
->private;
5297 int number
= rdev
->raid_disk
;
5298 struct md_rdev
**rdevp
;
5299 struct disk_info
*p
= conf
->disks
+ number
;
5301 print_raid5_conf(conf
);
5302 if (rdev
== p
->rdev
)
5304 else if (rdev
== p
->replacement
)
5305 rdevp
= &p
->replacement
;
5309 if (number
>= conf
->raid_disks
&&
5310 conf
->reshape_progress
== MaxSector
)
5311 clear_bit(In_sync
, &rdev
->flags
);
5313 if (test_bit(In_sync
, &rdev
->flags
) ||
5314 atomic_read(&rdev
->nr_pending
)) {
5318 /* Only remove non-faulty devices if recovery
5321 if (!test_bit(Faulty
, &rdev
->flags
) &&
5322 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
5323 !has_failed(conf
) &&
5324 (!p
->replacement
|| p
->replacement
== rdev
) &&
5325 number
< conf
->raid_disks
) {
5331 if (atomic_read(&rdev
->nr_pending
)) {
5332 /* lost the race, try later */
5335 } else if (p
->replacement
) {
5336 /* We must have just cleared 'rdev' */
5337 p
->rdev
= p
->replacement
;
5338 clear_bit(Replacement
, &p
->replacement
->flags
);
5339 smp_mb(); /* Make sure other CPUs may see both as identical
5340 * but will never see neither - if they are careful
5342 p
->replacement
= NULL
;
5343 clear_bit(WantReplacement
, &rdev
->flags
);
5345 /* We might have just removed the Replacement as faulty-
5346 * clear the bit just in case
5348 clear_bit(WantReplacement
, &rdev
->flags
);
5351 print_raid5_conf(conf
);
5355 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5357 struct r5conf
*conf
= mddev
->private;
5360 struct disk_info
*p
;
5362 int last
= conf
->raid_disks
- 1;
5364 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
5367 if (has_failed(conf
))
5368 /* no point adding a device */
5371 if (rdev
->raid_disk
>= 0)
5372 first
= last
= rdev
->raid_disk
;
5375 * find the disk ... but prefer rdev->saved_raid_disk
5378 if (rdev
->saved_raid_disk
>= 0 &&
5379 rdev
->saved_raid_disk
>= first
&&
5380 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
5381 disk
= rdev
->saved_raid_disk
;
5384 for ( ; disk
<= last
; disk
++) {
5385 p
= conf
->disks
+ disk
;
5386 if (p
->rdev
== NULL
) {
5387 clear_bit(In_sync
, &rdev
->flags
);
5388 rdev
->raid_disk
= disk
;
5390 if (rdev
->saved_raid_disk
!= disk
)
5392 rcu_assign_pointer(p
->rdev
, rdev
);
5395 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
5396 p
->replacement
== NULL
) {
5397 clear_bit(In_sync
, &rdev
->flags
);
5398 set_bit(Replacement
, &rdev
->flags
);
5399 rdev
->raid_disk
= disk
;
5402 rcu_assign_pointer(p
->replacement
, rdev
);
5406 print_raid5_conf(conf
);
5410 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
5412 /* no resync is happening, and there is enough space
5413 * on all devices, so we can resize.
5414 * We need to make sure resync covers any new space.
5415 * If the array is shrinking we should possibly wait until
5416 * any io in the removed space completes, but it hardly seems
5419 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
5420 md_set_array_sectors(mddev
, raid5_size(mddev
, sectors
,
5421 mddev
->raid_disks
));
5422 if (mddev
->array_sectors
>
5423 raid5_size(mddev
, sectors
, mddev
->raid_disks
))
5425 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5426 revalidate_disk(mddev
->gendisk
);
5427 if (sectors
> mddev
->dev_sectors
&&
5428 mddev
->recovery_cp
> mddev
->dev_sectors
) {
5429 mddev
->recovery_cp
= mddev
->dev_sectors
;
5430 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5432 mddev
->dev_sectors
= sectors
;
5433 mddev
->resync_max_sectors
= sectors
;
5437 static int check_stripe_cache(struct mddev
*mddev
)
5439 /* Can only proceed if there are plenty of stripe_heads.
5440 * We need a minimum of one full stripe,, and for sensible progress
5441 * it is best to have about 4 times that.
5442 * If we require 4 times, then the default 256 4K stripe_heads will
5443 * allow for chunk sizes up to 256K, which is probably OK.
5444 * If the chunk size is greater, user-space should request more
5445 * stripe_heads first.
5447 struct r5conf
*conf
= mddev
->private;
5448 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5449 > conf
->max_nr_stripes
||
5450 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5451 > conf
->max_nr_stripes
) {
5452 printk(KERN_WARNING
"md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5454 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
5461 static int check_reshape(struct mddev
*mddev
)
5463 struct r5conf
*conf
= mddev
->private;
5465 if (mddev
->delta_disks
== 0 &&
5466 mddev
->new_layout
== mddev
->layout
&&
5467 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
5468 return 0; /* nothing to do */
5470 /* Cannot grow a bitmap yet */
5472 if (has_failed(conf
))
5474 if (mddev
->delta_disks
< 0) {
5475 /* We might be able to shrink, but the devices must
5476 * be made bigger first.
5477 * For raid6, 4 is the minimum size.
5478 * Otherwise 2 is the minimum
5481 if (mddev
->level
== 6)
5483 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
5487 if (!check_stripe_cache(mddev
))
5490 return resize_stripes(conf
, conf
->raid_disks
+ mddev
->delta_disks
);
5493 static int raid5_start_reshape(struct mddev
*mddev
)
5495 struct r5conf
*conf
= mddev
->private;
5496 struct md_rdev
*rdev
;
5498 unsigned long flags
;
5500 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5503 if (!check_stripe_cache(mddev
))
5506 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5507 if (!test_bit(In_sync
, &rdev
->flags
)
5508 && !test_bit(Faulty
, &rdev
->flags
))
5511 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
5512 /* Not enough devices even to make a degraded array
5517 /* Refuse to reduce size of the array. Any reductions in
5518 * array size must be through explicit setting of array_size
5521 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
5522 < mddev
->array_sectors
) {
5523 printk(KERN_ERR
"md/raid:%s: array size must be reduced "
5524 "before number of disks\n", mdname(mddev
));
5528 atomic_set(&conf
->reshape_stripes
, 0);
5529 spin_lock_irq(&conf
->device_lock
);
5530 conf
->previous_raid_disks
= conf
->raid_disks
;
5531 conf
->raid_disks
+= mddev
->delta_disks
;
5532 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
5533 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5534 conf
->prev_algo
= conf
->algorithm
;
5535 conf
->algorithm
= mddev
->new_layout
;
5536 if (mddev
->delta_disks
< 0)
5537 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
5539 conf
->reshape_progress
= 0;
5540 conf
->reshape_safe
= conf
->reshape_progress
;
5542 spin_unlock_irq(&conf
->device_lock
);
5544 /* Add some new drives, as many as will fit.
5545 * We know there are enough to make the newly sized array work.
5546 * Don't add devices if we are reducing the number of
5547 * devices in the array. This is because it is not possible
5548 * to correctly record the "partially reconstructed" state of
5549 * such devices during the reshape and confusion could result.
5551 if (mddev
->delta_disks
>= 0) {
5552 int added_devices
= 0;
5553 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
5554 if (rdev
->raid_disk
< 0 &&
5555 !test_bit(Faulty
, &rdev
->flags
)) {
5556 if (raid5_add_disk(mddev
, rdev
) == 0) {
5558 >= conf
->previous_raid_disks
) {
5559 set_bit(In_sync
, &rdev
->flags
);
5562 rdev
->recovery_offset
= 0;
5564 if (sysfs_link_rdev(mddev
, rdev
))
5565 /* Failure here is OK */;
5567 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
5568 && !test_bit(Faulty
, &rdev
->flags
)) {
5569 /* This is a spare that was manually added */
5570 set_bit(In_sync
, &rdev
->flags
);
5574 /* When a reshape changes the number of devices,
5575 * ->degraded is measured against the larger of the
5576 * pre and post number of devices.
5578 spin_lock_irqsave(&conf
->device_lock
, flags
);
5579 mddev
->degraded
= calc_degraded(conf
);
5580 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5582 mddev
->raid_disks
= conf
->raid_disks
;
5583 mddev
->reshape_position
= conf
->reshape_progress
;
5584 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5586 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5587 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5588 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5589 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5590 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5592 if (!mddev
->sync_thread
) {
5593 mddev
->recovery
= 0;
5594 spin_lock_irq(&conf
->device_lock
);
5595 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
5596 conf
->reshape_progress
= MaxSector
;
5597 spin_unlock_irq(&conf
->device_lock
);
5600 conf
->reshape_checkpoint
= jiffies
;
5601 md_wakeup_thread(mddev
->sync_thread
);
5602 md_new_event(mddev
);
5606 /* This is called from the reshape thread and should make any
5607 * changes needed in 'conf'
5609 static void end_reshape(struct r5conf
*conf
)
5612 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
5614 spin_lock_irq(&conf
->device_lock
);
5615 conf
->previous_raid_disks
= conf
->raid_disks
;
5616 conf
->reshape_progress
= MaxSector
;
5617 spin_unlock_irq(&conf
->device_lock
);
5618 wake_up(&conf
->wait_for_overlap
);
5620 /* read-ahead size must cover two whole stripes, which is
5621 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5623 if (conf
->mddev
->queue
) {
5624 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5625 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
5627 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5628 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5633 /* This is called from the raid5d thread with mddev_lock held.
5634 * It makes config changes to the device.
5636 static void raid5_finish_reshape(struct mddev
*mddev
)
5638 struct r5conf
*conf
= mddev
->private;
5640 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5642 if (mddev
->delta_disks
> 0) {
5643 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5644 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5645 revalidate_disk(mddev
->gendisk
);
5648 spin_lock_irq(&conf
->device_lock
);
5649 mddev
->degraded
= calc_degraded(conf
);
5650 spin_unlock_irq(&conf
->device_lock
);
5651 for (d
= conf
->raid_disks
;
5652 d
< conf
->raid_disks
- mddev
->delta_disks
;
5654 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
5656 raid5_remove_disk(mddev
, rdev
) == 0) {
5657 sysfs_unlink_rdev(mddev
, rdev
);
5658 rdev
->raid_disk
= -1;
5662 mddev
->layout
= conf
->algorithm
;
5663 mddev
->chunk_sectors
= conf
->chunk_sectors
;
5664 mddev
->reshape_position
= MaxSector
;
5665 mddev
->delta_disks
= 0;
5669 static void raid5_quiesce(struct mddev
*mddev
, int state
)
5671 struct r5conf
*conf
= mddev
->private;
5674 case 2: /* resume for a suspend */
5675 wake_up(&conf
->wait_for_overlap
);
5678 case 1: /* stop all writes */
5679 spin_lock_irq(&conf
->device_lock
);
5680 /* '2' tells resync/reshape to pause so that all
5681 * active stripes can drain
5684 wait_event_lock_irq(conf
->wait_for_stripe
,
5685 atomic_read(&conf
->active_stripes
) == 0 &&
5686 atomic_read(&conf
->active_aligned_reads
) == 0,
5687 conf
->device_lock
, /* nothing */);
5689 spin_unlock_irq(&conf
->device_lock
);
5690 /* allow reshape to continue */
5691 wake_up(&conf
->wait_for_overlap
);
5694 case 0: /* re-enable writes */
5695 spin_lock_irq(&conf
->device_lock
);
5697 wake_up(&conf
->wait_for_stripe
);
5698 wake_up(&conf
->wait_for_overlap
);
5699 spin_unlock_irq(&conf
->device_lock
);
5705 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
5707 struct r0conf
*raid0_conf
= mddev
->private;
5710 /* for raid0 takeover only one zone is supported */
5711 if (raid0_conf
->nr_strip_zones
> 1) {
5712 printk(KERN_ERR
"md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5714 return ERR_PTR(-EINVAL
);
5717 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
5718 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
5719 mddev
->dev_sectors
= sectors
;
5720 mddev
->new_level
= level
;
5721 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5722 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5723 mddev
->raid_disks
+= 1;
5724 mddev
->delta_disks
= 1;
5725 /* make sure it will be not marked as dirty */
5726 mddev
->recovery_cp
= MaxSector
;
5728 return setup_conf(mddev
);
5732 static void *raid5_takeover_raid1(struct mddev
*mddev
)
5736 if (mddev
->raid_disks
!= 2 ||
5737 mddev
->degraded
> 1)
5738 return ERR_PTR(-EINVAL
);
5740 /* Should check if there are write-behind devices? */
5742 chunksect
= 64*2; /* 64K by default */
5744 /* The array must be an exact multiple of chunksize */
5745 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
5748 if ((chunksect
<<9) < STRIPE_SIZE
)
5749 /* array size does not allow a suitable chunk size */
5750 return ERR_PTR(-EINVAL
);
5752 mddev
->new_level
= 5;
5753 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
5754 mddev
->new_chunk_sectors
= chunksect
;
5756 return setup_conf(mddev
);
5759 static void *raid5_takeover_raid6(struct mddev
*mddev
)
5763 switch (mddev
->layout
) {
5764 case ALGORITHM_LEFT_ASYMMETRIC_6
:
5765 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
5767 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
5768 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
5770 case ALGORITHM_LEFT_SYMMETRIC_6
:
5771 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
5773 case ALGORITHM_RIGHT_SYMMETRIC_6
:
5774 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
5776 case ALGORITHM_PARITY_0_6
:
5777 new_layout
= ALGORITHM_PARITY_0
;
5779 case ALGORITHM_PARITY_N
:
5780 new_layout
= ALGORITHM_PARITY_N
;
5783 return ERR_PTR(-EINVAL
);
5785 mddev
->new_level
= 5;
5786 mddev
->new_layout
= new_layout
;
5787 mddev
->delta_disks
= -1;
5788 mddev
->raid_disks
-= 1;
5789 return setup_conf(mddev
);
5793 static int raid5_check_reshape(struct mddev
*mddev
)
5795 /* For a 2-drive array, the layout and chunk size can be changed
5796 * immediately as not restriping is needed.
5797 * For larger arrays we record the new value - after validation
5798 * to be used by a reshape pass.
5800 struct r5conf
*conf
= mddev
->private;
5801 int new_chunk
= mddev
->new_chunk_sectors
;
5803 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
5805 if (new_chunk
> 0) {
5806 if (!is_power_of_2(new_chunk
))
5808 if (new_chunk
< (PAGE_SIZE
>>9))
5810 if (mddev
->array_sectors
& (new_chunk
-1))
5811 /* not factor of array size */
5815 /* They look valid */
5817 if (mddev
->raid_disks
== 2) {
5818 /* can make the change immediately */
5819 if (mddev
->new_layout
>= 0) {
5820 conf
->algorithm
= mddev
->new_layout
;
5821 mddev
->layout
= mddev
->new_layout
;
5823 if (new_chunk
> 0) {
5824 conf
->chunk_sectors
= new_chunk
;
5825 mddev
->chunk_sectors
= new_chunk
;
5827 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5828 md_wakeup_thread(mddev
->thread
);
5830 return check_reshape(mddev
);
5833 static int raid6_check_reshape(struct mddev
*mddev
)
5835 int new_chunk
= mddev
->new_chunk_sectors
;
5837 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
5839 if (new_chunk
> 0) {
5840 if (!is_power_of_2(new_chunk
))
5842 if (new_chunk
< (PAGE_SIZE
>> 9))
5844 if (mddev
->array_sectors
& (new_chunk
-1))
5845 /* not factor of array size */
5849 /* They look valid */
5850 return check_reshape(mddev
);
5853 static void *raid5_takeover(struct mddev
*mddev
)
5855 /* raid5 can take over:
5856 * raid0 - if there is only one strip zone - make it a raid4 layout
5857 * raid1 - if there are two drives. We need to know the chunk size
5858 * raid4 - trivial - just use a raid4 layout.
5859 * raid6 - Providing it is a *_6 layout
5861 if (mddev
->level
== 0)
5862 return raid45_takeover_raid0(mddev
, 5);
5863 if (mddev
->level
== 1)
5864 return raid5_takeover_raid1(mddev
);
5865 if (mddev
->level
== 4) {
5866 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5867 mddev
->new_level
= 5;
5868 return setup_conf(mddev
);
5870 if (mddev
->level
== 6)
5871 return raid5_takeover_raid6(mddev
);
5873 return ERR_PTR(-EINVAL
);
5876 static void *raid4_takeover(struct mddev
*mddev
)
5878 /* raid4 can take over:
5879 * raid0 - if there is only one strip zone
5880 * raid5 - if layout is right
5882 if (mddev
->level
== 0)
5883 return raid45_takeover_raid0(mddev
, 4);
5884 if (mddev
->level
== 5 &&
5885 mddev
->layout
== ALGORITHM_PARITY_N
) {
5886 mddev
->new_layout
= 0;
5887 mddev
->new_level
= 4;
5888 return setup_conf(mddev
);
5890 return ERR_PTR(-EINVAL
);
5893 static struct md_personality raid5_personality
;
5895 static void *raid6_takeover(struct mddev
*mddev
)
5897 /* Currently can only take over a raid5. We map the
5898 * personality to an equivalent raid6 personality
5899 * with the Q block at the end.
5903 if (mddev
->pers
!= &raid5_personality
)
5904 return ERR_PTR(-EINVAL
);
5905 if (mddev
->degraded
> 1)
5906 return ERR_PTR(-EINVAL
);
5907 if (mddev
->raid_disks
> 253)
5908 return ERR_PTR(-EINVAL
);
5909 if (mddev
->raid_disks
< 3)
5910 return ERR_PTR(-EINVAL
);
5912 switch (mddev
->layout
) {
5913 case ALGORITHM_LEFT_ASYMMETRIC
:
5914 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
5916 case ALGORITHM_RIGHT_ASYMMETRIC
:
5917 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
5919 case ALGORITHM_LEFT_SYMMETRIC
:
5920 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
5922 case ALGORITHM_RIGHT_SYMMETRIC
:
5923 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
5925 case ALGORITHM_PARITY_0
:
5926 new_layout
= ALGORITHM_PARITY_0_6
;
5928 case ALGORITHM_PARITY_N
:
5929 new_layout
= ALGORITHM_PARITY_N
;
5932 return ERR_PTR(-EINVAL
);
5934 mddev
->new_level
= 6;
5935 mddev
->new_layout
= new_layout
;
5936 mddev
->delta_disks
= 1;
5937 mddev
->raid_disks
+= 1;
5938 return setup_conf(mddev
);
5942 static struct md_personality raid6_personality
=
5946 .owner
= THIS_MODULE
,
5947 .make_request
= make_request
,
5951 .error_handler
= error
,
5952 .hot_add_disk
= raid5_add_disk
,
5953 .hot_remove_disk
= raid5_remove_disk
,
5954 .spare_active
= raid5_spare_active
,
5955 .sync_request
= sync_request
,
5956 .resize
= raid5_resize
,
5958 .check_reshape
= raid6_check_reshape
,
5959 .start_reshape
= raid5_start_reshape
,
5960 .finish_reshape
= raid5_finish_reshape
,
5961 .quiesce
= raid5_quiesce
,
5962 .takeover
= raid6_takeover
,
5964 static struct md_personality raid5_personality
=
5968 .owner
= THIS_MODULE
,
5969 .make_request
= make_request
,
5973 .error_handler
= error
,
5974 .hot_add_disk
= raid5_add_disk
,
5975 .hot_remove_disk
= raid5_remove_disk
,
5976 .spare_active
= raid5_spare_active
,
5977 .sync_request
= sync_request
,
5978 .resize
= raid5_resize
,
5980 .check_reshape
= raid5_check_reshape
,
5981 .start_reshape
= raid5_start_reshape
,
5982 .finish_reshape
= raid5_finish_reshape
,
5983 .quiesce
= raid5_quiesce
,
5984 .takeover
= raid5_takeover
,
5987 static struct md_personality raid4_personality
=
5991 .owner
= THIS_MODULE
,
5992 .make_request
= make_request
,
5996 .error_handler
= error
,
5997 .hot_add_disk
= raid5_add_disk
,
5998 .hot_remove_disk
= raid5_remove_disk
,
5999 .spare_active
= raid5_spare_active
,
6000 .sync_request
= sync_request
,
6001 .resize
= raid5_resize
,
6003 .check_reshape
= raid5_check_reshape
,
6004 .start_reshape
= raid5_start_reshape
,
6005 .finish_reshape
= raid5_finish_reshape
,
6006 .quiesce
= raid5_quiesce
,
6007 .takeover
= raid4_takeover
,
6010 static int __init
raid5_init(void)
6012 register_md_personality(&raid6_personality
);
6013 register_md_personality(&raid5_personality
);
6014 register_md_personality(&raid4_personality
);
6018 static void raid5_exit(void)
6020 unregister_md_personality(&raid6_personality
);
6021 unregister_md_personality(&raid5_personality
);
6022 unregister_md_personality(&raid4_personality
);
6025 module_init(raid5_init
);
6026 module_exit(raid5_exit
);
6027 MODULE_LICENSE("GPL");
6028 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6029 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6030 MODULE_ALIAS("md-raid5");
6031 MODULE_ALIAS("md-raid4");
6032 MODULE_ALIAS("md-level-5");
6033 MODULE_ALIAS("md-level-4");
6034 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6035 MODULE_ALIAS("md-raid6");
6036 MODULE_ALIAS("md-level-6");
6038 /* This used to be two separate modules, they were: */
6039 MODULE_ALIAS("raid5");
6040 MODULE_ALIAS("raid6");