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->bm_write is the number of the last batch successfully written.
31 * conf->bm_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 bm_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/seq_file.h>
59 #define NR_STRIPES 256
60 #define STRIPE_SIZE PAGE_SIZE
61 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
62 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
63 #define IO_THRESHOLD 1
64 #define BYPASS_THRESHOLD 1
65 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
66 #define HASH_MASK (NR_HASH - 1)
68 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
70 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
79 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
81 * The following can be used to debug the driver
83 #define RAID5_PARANOIA 1
84 #if RAID5_PARANOIA && defined(CONFIG_SMP)
85 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
87 # define CHECK_DEVLOCK()
95 #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
98 * We maintain a biased count of active stripes in the bottom 16 bits of
99 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
101 static inline int raid5_bi_phys_segments(struct bio
*bio
)
103 return bio
->bi_phys_segments
& 0xffff;
106 static inline int raid5_bi_hw_segments(struct bio
*bio
)
108 return (bio
->bi_phys_segments
>> 16) & 0xffff;
111 static inline int raid5_dec_bi_phys_segments(struct bio
*bio
)
113 --bio
->bi_phys_segments
;
114 return raid5_bi_phys_segments(bio
);
117 static inline int raid5_dec_bi_hw_segments(struct bio
*bio
)
119 unsigned short val
= raid5_bi_hw_segments(bio
);
122 bio
->bi_phys_segments
= (val
<< 16) | raid5_bi_phys_segments(bio
);
126 static inline void raid5_set_bi_hw_segments(struct bio
*bio
, unsigned int cnt
)
128 bio
->bi_phys_segments
= raid5_bi_phys_segments(bio
) || (cnt
<< 16);
131 /* Find first data disk in a raid6 stripe */
132 static inline int raid6_d0(struct stripe_head
*sh
)
135 /* ddf always start from first device */
137 /* md starts just after Q block */
138 if (sh
->qd_idx
== sh
->disks
- 1)
141 return sh
->qd_idx
+ 1;
143 static inline int raid6_next_disk(int disk
, int raid_disks
)
146 return (disk
< raid_disks
) ? disk
: 0;
149 /* When walking through the disks in a raid5, starting at raid6_d0,
150 * We need to map each disk to a 'slot', where the data disks are slot
151 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
152 * is raid_disks-1. This help does that mapping.
154 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
155 int *count
, int syndrome_disks
)
159 if (idx
== sh
->pd_idx
)
160 return syndrome_disks
;
161 if (idx
== sh
->qd_idx
)
162 return syndrome_disks
+ 1;
167 static void return_io(struct bio
*return_bi
)
169 struct bio
*bi
= return_bi
;
172 return_bi
= bi
->bi_next
;
180 static void print_raid5_conf (raid5_conf_t
*conf
);
182 static int stripe_operations_active(struct stripe_head
*sh
)
184 return sh
->check_state
|| sh
->reconstruct_state
||
185 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
186 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
189 static void __release_stripe(raid5_conf_t
*conf
, struct stripe_head
*sh
)
191 if (atomic_dec_and_test(&sh
->count
)) {
192 BUG_ON(!list_empty(&sh
->lru
));
193 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
194 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
195 if (test_bit(STRIPE_DELAYED
, &sh
->state
)) {
196 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
197 blk_plug_device(conf
->mddev
->queue
);
198 } else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
199 sh
->bm_seq
- conf
->seq_write
> 0) {
200 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
201 blk_plug_device(conf
->mddev
->queue
);
203 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
204 list_add_tail(&sh
->lru
, &conf
->handle_list
);
206 md_wakeup_thread(conf
->mddev
->thread
);
208 BUG_ON(stripe_operations_active(sh
));
209 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
210 atomic_dec(&conf
->preread_active_stripes
);
211 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
212 md_wakeup_thread(conf
->mddev
->thread
);
214 atomic_dec(&conf
->active_stripes
);
215 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
216 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
217 wake_up(&conf
->wait_for_stripe
);
218 if (conf
->retry_read_aligned
)
219 md_wakeup_thread(conf
->mddev
->thread
);
225 static void release_stripe(struct stripe_head
*sh
)
227 raid5_conf_t
*conf
= sh
->raid_conf
;
230 spin_lock_irqsave(&conf
->device_lock
, flags
);
231 __release_stripe(conf
, sh
);
232 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
235 static inline void remove_hash(struct stripe_head
*sh
)
237 pr_debug("remove_hash(), stripe %llu\n",
238 (unsigned long long)sh
->sector
);
240 hlist_del_init(&sh
->hash
);
243 static inline void insert_hash(raid5_conf_t
*conf
, struct stripe_head
*sh
)
245 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
247 pr_debug("insert_hash(), stripe %llu\n",
248 (unsigned long long)sh
->sector
);
251 hlist_add_head(&sh
->hash
, hp
);
255 /* find an idle stripe, make sure it is unhashed, and return it. */
256 static struct stripe_head
*get_free_stripe(raid5_conf_t
*conf
)
258 struct stripe_head
*sh
= NULL
;
259 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
, int num
)
278 for (i
=0; i
<num
; i
++) {
282 sh
->dev
[i
].page
= NULL
;
287 static int grow_buffers(struct stripe_head
*sh
, int num
)
291 for (i
=0; i
<num
; i
++) {
294 if (!(page
= alloc_page(GFP_KERNEL
))) {
297 sh
->dev
[i
].page
= page
;
302 static void raid5_build_block(struct stripe_head
*sh
, int i
);
303 static void stripe_set_idx(sector_t stripe
, raid5_conf_t
*conf
, int previous
,
304 struct stripe_head
*sh
);
306 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
308 raid5_conf_t
*conf
= sh
->raid_conf
;
311 BUG_ON(atomic_read(&sh
->count
) != 0);
312 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
313 BUG_ON(stripe_operations_active(sh
));
316 pr_debug("init_stripe called, stripe %llu\n",
317 (unsigned long long)sh
->sector
);
321 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
323 stripe_set_idx(sector
, conf
, previous
, sh
);
327 for (i
= sh
->disks
; i
--; ) {
328 struct r5dev
*dev
= &sh
->dev
[i
];
330 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
331 test_bit(R5_LOCKED
, &dev
->flags
)) {
332 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
333 (unsigned long long)sh
->sector
, i
, dev
->toread
,
334 dev
->read
, dev
->towrite
, dev
->written
,
335 test_bit(R5_LOCKED
, &dev
->flags
));
339 raid5_build_block(sh
, i
);
341 insert_hash(conf
, sh
);
344 static struct stripe_head
*__find_stripe(raid5_conf_t
*conf
, sector_t sector
, int disks
)
346 struct stripe_head
*sh
;
347 struct hlist_node
*hn
;
350 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
351 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
352 if (sh
->sector
== sector
&& sh
->disks
== disks
)
354 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
358 static void unplug_slaves(mddev_t
*mddev
);
359 static void raid5_unplug_device(struct request_queue
*q
);
361 static struct stripe_head
*
362 get_active_stripe(raid5_conf_t
*conf
, sector_t sector
,
363 int previous
, int noblock
)
365 struct stripe_head
*sh
;
366 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
368 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
370 spin_lock_irq(&conf
->device_lock
);
373 wait_event_lock_irq(conf
->wait_for_stripe
,
375 conf
->device_lock
, /* nothing */);
376 sh
= __find_stripe(conf
, sector
, disks
);
378 if (!conf
->inactive_blocked
)
379 sh
= get_free_stripe(conf
);
380 if (noblock
&& sh
== NULL
)
383 conf
->inactive_blocked
= 1;
384 wait_event_lock_irq(conf
->wait_for_stripe
,
385 !list_empty(&conf
->inactive_list
) &&
386 (atomic_read(&conf
->active_stripes
)
387 < (conf
->max_nr_stripes
*3/4)
388 || !conf
->inactive_blocked
),
390 raid5_unplug_device(conf
->mddev
->queue
)
392 conf
->inactive_blocked
= 0;
394 init_stripe(sh
, sector
, previous
);
396 if (atomic_read(&sh
->count
)) {
397 BUG_ON(!list_empty(&sh
->lru
));
399 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
400 atomic_inc(&conf
->active_stripes
);
401 if (list_empty(&sh
->lru
) &&
402 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
404 list_del_init(&sh
->lru
);
407 } while (sh
== NULL
);
410 atomic_inc(&sh
->count
);
412 spin_unlock_irq(&conf
->device_lock
);
417 raid5_end_read_request(struct bio
*bi
, int error
);
419 raid5_end_write_request(struct bio
*bi
, int error
);
421 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
423 raid5_conf_t
*conf
= sh
->raid_conf
;
424 int i
, disks
= sh
->disks
;
428 for (i
= disks
; i
--; ) {
432 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
434 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
439 bi
= &sh
->dev
[i
].req
;
443 bi
->bi_end_io
= raid5_end_write_request
;
445 bi
->bi_end_io
= raid5_end_read_request
;
448 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
449 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
452 atomic_inc(&rdev
->nr_pending
);
456 if (s
->syncing
|| s
->expanding
|| s
->expanded
)
457 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
459 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
461 bi
->bi_bdev
= rdev
->bdev
;
462 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
463 __func__
, (unsigned long long)sh
->sector
,
465 atomic_inc(&sh
->count
);
466 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
467 bi
->bi_flags
= 1 << BIO_UPTODATE
;
471 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
472 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
473 bi
->bi_io_vec
[0].bv_offset
= 0;
474 bi
->bi_size
= STRIPE_SIZE
;
477 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
478 atomic_add(STRIPE_SECTORS
,
479 &rdev
->corrected_errors
);
480 generic_make_request(bi
);
483 set_bit(STRIPE_DEGRADED
, &sh
->state
);
484 pr_debug("skip op %ld on disc %d for sector %llu\n",
485 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
486 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
487 set_bit(STRIPE_HANDLE
, &sh
->state
);
492 static struct dma_async_tx_descriptor
*
493 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
494 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
497 struct page
*bio_page
;
501 if (bio
->bi_sector
>= sector
)
502 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
504 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
505 bio_for_each_segment(bvl
, bio
, i
) {
506 int len
= bio_iovec_idx(bio
, i
)->bv_len
;
510 if (page_offset
< 0) {
511 b_offset
= -page_offset
;
512 page_offset
+= b_offset
;
516 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
517 clen
= STRIPE_SIZE
- page_offset
;
522 b_offset
+= bio_iovec_idx(bio
, i
)->bv_offset
;
523 bio_page
= bio_iovec_idx(bio
, i
)->bv_page
;
525 tx
= async_memcpy(page
, bio_page
, page_offset
,
530 tx
= async_memcpy(bio_page
, page
, b_offset
,
535 if (clen
< len
) /* hit end of page */
543 static void ops_complete_biofill(void *stripe_head_ref
)
545 struct stripe_head
*sh
= stripe_head_ref
;
546 struct bio
*return_bi
= NULL
;
547 raid5_conf_t
*conf
= sh
->raid_conf
;
550 pr_debug("%s: stripe %llu\n", __func__
,
551 (unsigned long long)sh
->sector
);
553 /* clear completed biofills */
554 spin_lock_irq(&conf
->device_lock
);
555 for (i
= sh
->disks
; i
--; ) {
556 struct r5dev
*dev
= &sh
->dev
[i
];
558 /* acknowledge completion of a biofill operation */
559 /* and check if we need to reply to a read request,
560 * new R5_Wantfill requests are held off until
561 * !STRIPE_BIOFILL_RUN
563 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
564 struct bio
*rbi
, *rbi2
;
569 while (rbi
&& rbi
->bi_sector
<
570 dev
->sector
+ STRIPE_SECTORS
) {
571 rbi2
= r5_next_bio(rbi
, dev
->sector
);
572 if (!raid5_dec_bi_phys_segments(rbi
)) {
573 rbi
->bi_next
= return_bi
;
580 spin_unlock_irq(&conf
->device_lock
);
581 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
583 return_io(return_bi
);
585 set_bit(STRIPE_HANDLE
, &sh
->state
);
589 static void ops_run_biofill(struct stripe_head
*sh
)
591 struct dma_async_tx_descriptor
*tx
= NULL
;
592 raid5_conf_t
*conf
= sh
->raid_conf
;
595 pr_debug("%s: stripe %llu\n", __func__
,
596 (unsigned long long)sh
->sector
);
598 for (i
= sh
->disks
; i
--; ) {
599 struct r5dev
*dev
= &sh
->dev
[i
];
600 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
602 spin_lock_irq(&conf
->device_lock
);
603 dev
->read
= rbi
= dev
->toread
;
605 spin_unlock_irq(&conf
->device_lock
);
606 while (rbi
&& rbi
->bi_sector
<
607 dev
->sector
+ STRIPE_SECTORS
) {
608 tx
= async_copy_data(0, rbi
, dev
->page
,
610 rbi
= r5_next_bio(rbi
, dev
->sector
);
615 atomic_inc(&sh
->count
);
616 async_trigger_callback(ASYNC_TX_DEP_ACK
| ASYNC_TX_ACK
, tx
,
617 ops_complete_biofill
, sh
);
620 static void ops_complete_compute5(void *stripe_head_ref
)
622 struct stripe_head
*sh
= stripe_head_ref
;
623 int target
= sh
->ops
.target
;
624 struct r5dev
*tgt
= &sh
->dev
[target
];
626 pr_debug("%s: stripe %llu\n", __func__
,
627 (unsigned long long)sh
->sector
);
629 set_bit(R5_UPTODATE
, &tgt
->flags
);
630 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
631 clear_bit(R5_Wantcompute
, &tgt
->flags
);
632 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
633 if (sh
->check_state
== check_state_compute_run
)
634 sh
->check_state
= check_state_compute_result
;
635 set_bit(STRIPE_HANDLE
, &sh
->state
);
639 static struct dma_async_tx_descriptor
*ops_run_compute5(struct stripe_head
*sh
)
641 /* kernel stack size limits the total number of disks */
642 int disks
= sh
->disks
;
643 struct page
*xor_srcs
[disks
];
644 int target
= sh
->ops
.target
;
645 struct r5dev
*tgt
= &sh
->dev
[target
];
646 struct page
*xor_dest
= tgt
->page
;
648 struct dma_async_tx_descriptor
*tx
;
651 pr_debug("%s: stripe %llu block: %d\n",
652 __func__
, (unsigned long long)sh
->sector
, target
);
653 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
655 for (i
= disks
; i
--; )
657 xor_srcs
[count
++] = sh
->dev
[i
].page
;
659 atomic_inc(&sh
->count
);
661 if (unlikely(count
== 1))
662 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
,
663 0, NULL
, ops_complete_compute5
, sh
);
665 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
666 ASYNC_TX_XOR_ZERO_DST
, NULL
,
667 ops_complete_compute5
, sh
);
672 static void ops_complete_prexor(void *stripe_head_ref
)
674 struct stripe_head
*sh
= stripe_head_ref
;
676 pr_debug("%s: stripe %llu\n", __func__
,
677 (unsigned long long)sh
->sector
);
680 static struct dma_async_tx_descriptor
*
681 ops_run_prexor(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
683 /* kernel stack size limits the total number of disks */
684 int disks
= sh
->disks
;
685 struct page
*xor_srcs
[disks
];
686 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
688 /* existing parity data subtracted */
689 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
691 pr_debug("%s: stripe %llu\n", __func__
,
692 (unsigned long long)sh
->sector
);
694 for (i
= disks
; i
--; ) {
695 struct r5dev
*dev
= &sh
->dev
[i
];
696 /* Only process blocks that are known to be uptodate */
697 if (test_bit(R5_Wantdrain
, &dev
->flags
))
698 xor_srcs
[count
++] = dev
->page
;
701 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
702 ASYNC_TX_DEP_ACK
| ASYNC_TX_XOR_DROP_DST
, tx
,
703 ops_complete_prexor
, sh
);
708 static struct dma_async_tx_descriptor
*
709 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
711 int disks
= sh
->disks
;
714 pr_debug("%s: stripe %llu\n", __func__
,
715 (unsigned long long)sh
->sector
);
717 for (i
= disks
; i
--; ) {
718 struct r5dev
*dev
= &sh
->dev
[i
];
721 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
724 spin_lock(&sh
->lock
);
725 chosen
= dev
->towrite
;
727 BUG_ON(dev
->written
);
728 wbi
= dev
->written
= chosen
;
729 spin_unlock(&sh
->lock
);
731 while (wbi
&& wbi
->bi_sector
<
732 dev
->sector
+ STRIPE_SECTORS
) {
733 tx
= async_copy_data(1, wbi
, dev
->page
,
735 wbi
= r5_next_bio(wbi
, dev
->sector
);
743 static void ops_complete_postxor(void *stripe_head_ref
)
745 struct stripe_head
*sh
= stripe_head_ref
;
746 int disks
= sh
->disks
, i
, pd_idx
= sh
->pd_idx
;
748 pr_debug("%s: stripe %llu\n", __func__
,
749 (unsigned long long)sh
->sector
);
751 for (i
= disks
; i
--; ) {
752 struct r5dev
*dev
= &sh
->dev
[i
];
753 if (dev
->written
|| i
== pd_idx
)
754 set_bit(R5_UPTODATE
, &dev
->flags
);
757 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
758 sh
->reconstruct_state
= reconstruct_state_drain_result
;
759 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
760 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
762 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
763 sh
->reconstruct_state
= reconstruct_state_result
;
766 set_bit(STRIPE_HANDLE
, &sh
->state
);
771 ops_run_postxor(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
773 /* kernel stack size limits the total number of disks */
774 int disks
= sh
->disks
;
775 struct page
*xor_srcs
[disks
];
777 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
778 struct page
*xor_dest
;
782 pr_debug("%s: stripe %llu\n", __func__
,
783 (unsigned long long)sh
->sector
);
785 /* check if prexor is active which means only process blocks
786 * that are part of a read-modify-write (written)
788 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
790 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
791 for (i
= disks
; i
--; ) {
792 struct r5dev
*dev
= &sh
->dev
[i
];
794 xor_srcs
[count
++] = dev
->page
;
797 xor_dest
= sh
->dev
[pd_idx
].page
;
798 for (i
= disks
; i
--; ) {
799 struct r5dev
*dev
= &sh
->dev
[i
];
801 xor_srcs
[count
++] = dev
->page
;
805 /* 1/ if we prexor'd then the dest is reused as a source
806 * 2/ if we did not prexor then we are redoing the parity
807 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
808 * for the synchronous xor case
810 flags
= ASYNC_TX_DEP_ACK
| ASYNC_TX_ACK
|
811 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
813 atomic_inc(&sh
->count
);
815 if (unlikely(count
== 1)) {
816 flags
&= ~(ASYNC_TX_XOR_DROP_DST
| ASYNC_TX_XOR_ZERO_DST
);
817 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
,
818 flags
, tx
, ops_complete_postxor
, sh
);
820 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
821 flags
, tx
, ops_complete_postxor
, sh
);
824 static void ops_complete_check(void *stripe_head_ref
)
826 struct stripe_head
*sh
= stripe_head_ref
;
828 pr_debug("%s: stripe %llu\n", __func__
,
829 (unsigned long long)sh
->sector
);
831 sh
->check_state
= check_state_check_result
;
832 set_bit(STRIPE_HANDLE
, &sh
->state
);
836 static void ops_run_check(struct stripe_head
*sh
)
838 /* kernel stack size limits the total number of disks */
839 int disks
= sh
->disks
;
840 struct page
*xor_srcs
[disks
];
841 struct dma_async_tx_descriptor
*tx
;
843 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
844 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
846 pr_debug("%s: stripe %llu\n", __func__
,
847 (unsigned long long)sh
->sector
);
849 for (i
= disks
; i
--; ) {
850 struct r5dev
*dev
= &sh
->dev
[i
];
852 xor_srcs
[count
++] = dev
->page
;
855 tx
= async_xor_zero_sum(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
856 &sh
->ops
.zero_sum_result
, 0, NULL
, NULL
, NULL
);
858 atomic_inc(&sh
->count
);
859 tx
= async_trigger_callback(ASYNC_TX_DEP_ACK
| ASYNC_TX_ACK
, tx
,
860 ops_complete_check
, sh
);
863 static void raid5_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
865 int overlap_clear
= 0, i
, disks
= sh
->disks
;
866 struct dma_async_tx_descriptor
*tx
= NULL
;
868 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
873 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
874 tx
= ops_run_compute5(sh
);
875 /* terminate the chain if postxor is not set to be run */
876 if (tx
&& !test_bit(STRIPE_OP_POSTXOR
, &ops_request
))
880 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
881 tx
= ops_run_prexor(sh
, tx
);
883 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
884 tx
= ops_run_biodrain(sh
, tx
);
888 if (test_bit(STRIPE_OP_POSTXOR
, &ops_request
))
889 ops_run_postxor(sh
, tx
);
891 if (test_bit(STRIPE_OP_CHECK
, &ops_request
))
895 for (i
= disks
; i
--; ) {
896 struct r5dev
*dev
= &sh
->dev
[i
];
897 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
898 wake_up(&sh
->raid_conf
->wait_for_overlap
);
902 static int grow_one_stripe(raid5_conf_t
*conf
)
904 struct stripe_head
*sh
;
905 sh
= kmem_cache_alloc(conf
->slab_cache
, GFP_KERNEL
);
908 memset(sh
, 0, sizeof(*sh
) + (conf
->raid_disks
-1)*sizeof(struct r5dev
));
909 sh
->raid_conf
= conf
;
910 spin_lock_init(&sh
->lock
);
912 if (grow_buffers(sh
, conf
->raid_disks
)) {
913 shrink_buffers(sh
, conf
->raid_disks
);
914 kmem_cache_free(conf
->slab_cache
, sh
);
917 sh
->disks
= conf
->raid_disks
;
918 /* we just created an active stripe so... */
919 atomic_set(&sh
->count
, 1);
920 atomic_inc(&conf
->active_stripes
);
921 INIT_LIST_HEAD(&sh
->lru
);
926 static int grow_stripes(raid5_conf_t
*conf
, int num
)
928 struct kmem_cache
*sc
;
929 int devs
= conf
->raid_disks
;
931 sprintf(conf
->cache_name
[0],
932 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
933 sprintf(conf
->cache_name
[1],
934 "raid%d-%s-alt", conf
->level
, mdname(conf
->mddev
));
935 conf
->active_name
= 0;
936 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
937 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
941 conf
->slab_cache
= sc
;
942 conf
->pool_size
= devs
;
944 if (!grow_one_stripe(conf
))
949 #ifdef CONFIG_MD_RAID5_RESHAPE
950 static int resize_stripes(raid5_conf_t
*conf
, int newsize
)
952 /* Make all the stripes able to hold 'newsize' devices.
953 * New slots in each stripe get 'page' set to a new page.
955 * This happens in stages:
956 * 1/ create a new kmem_cache and allocate the required number of
958 * 2/ gather all the old stripe_heads and tranfer the pages across
959 * to the new stripe_heads. This will have the side effect of
960 * freezing the array as once all stripe_heads have been collected,
961 * no IO will be possible. Old stripe heads are freed once their
962 * pages have been transferred over, and the old kmem_cache is
963 * freed when all stripes are done.
964 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
965 * we simple return a failre status - no need to clean anything up.
966 * 4/ allocate new pages for the new slots in the new stripe_heads.
967 * If this fails, we don't bother trying the shrink the
968 * stripe_heads down again, we just leave them as they are.
969 * As each stripe_head is processed the new one is released into
972 * Once step2 is started, we cannot afford to wait for a write,
973 * so we use GFP_NOIO allocations.
975 struct stripe_head
*osh
, *nsh
;
976 LIST_HEAD(newstripes
);
977 struct disk_info
*ndisks
;
979 struct kmem_cache
*sc
;
982 if (newsize
<= conf
->pool_size
)
983 return 0; /* never bother to shrink */
985 err
= md_allow_write(conf
->mddev
);
990 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
991 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
996 for (i
= conf
->max_nr_stripes
; i
; i
--) {
997 nsh
= kmem_cache_alloc(sc
, GFP_KERNEL
);
1001 memset(nsh
, 0, sizeof(*nsh
) + (newsize
-1)*sizeof(struct r5dev
));
1003 nsh
->raid_conf
= conf
;
1004 spin_lock_init(&nsh
->lock
);
1006 list_add(&nsh
->lru
, &newstripes
);
1009 /* didn't get enough, give up */
1010 while (!list_empty(&newstripes
)) {
1011 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1012 list_del(&nsh
->lru
);
1013 kmem_cache_free(sc
, nsh
);
1015 kmem_cache_destroy(sc
);
1018 /* Step 2 - Must use GFP_NOIO now.
1019 * OK, we have enough stripes, start collecting inactive
1020 * stripes and copying them over
1022 list_for_each_entry(nsh
, &newstripes
, lru
) {
1023 spin_lock_irq(&conf
->device_lock
);
1024 wait_event_lock_irq(conf
->wait_for_stripe
,
1025 !list_empty(&conf
->inactive_list
),
1027 unplug_slaves(conf
->mddev
)
1029 osh
= get_free_stripe(conf
);
1030 spin_unlock_irq(&conf
->device_lock
);
1031 atomic_set(&nsh
->count
, 1);
1032 for(i
=0; i
<conf
->pool_size
; i
++)
1033 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1034 for( ; i
<newsize
; i
++)
1035 nsh
->dev
[i
].page
= NULL
;
1036 kmem_cache_free(conf
->slab_cache
, osh
);
1038 kmem_cache_destroy(conf
->slab_cache
);
1041 * At this point, we are holding all the stripes so the array
1042 * is completely stalled, so now is a good time to resize
1045 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1047 for (i
=0; i
<conf
->raid_disks
; i
++)
1048 ndisks
[i
] = conf
->disks
[i
];
1050 conf
->disks
= ndisks
;
1054 /* Step 4, return new stripes to service */
1055 while(!list_empty(&newstripes
)) {
1056 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1057 list_del_init(&nsh
->lru
);
1058 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1059 if (nsh
->dev
[i
].page
== NULL
) {
1060 struct page
*p
= alloc_page(GFP_NOIO
);
1061 nsh
->dev
[i
].page
= p
;
1065 release_stripe(nsh
);
1067 /* critical section pass, GFP_NOIO no longer needed */
1069 conf
->slab_cache
= sc
;
1070 conf
->active_name
= 1-conf
->active_name
;
1071 conf
->pool_size
= newsize
;
1076 static int drop_one_stripe(raid5_conf_t
*conf
)
1078 struct stripe_head
*sh
;
1080 spin_lock_irq(&conf
->device_lock
);
1081 sh
= get_free_stripe(conf
);
1082 spin_unlock_irq(&conf
->device_lock
);
1085 BUG_ON(atomic_read(&sh
->count
));
1086 shrink_buffers(sh
, conf
->pool_size
);
1087 kmem_cache_free(conf
->slab_cache
, sh
);
1088 atomic_dec(&conf
->active_stripes
);
1092 static void shrink_stripes(raid5_conf_t
*conf
)
1094 while (drop_one_stripe(conf
))
1097 if (conf
->slab_cache
)
1098 kmem_cache_destroy(conf
->slab_cache
);
1099 conf
->slab_cache
= NULL
;
1102 static void raid5_end_read_request(struct bio
* bi
, int error
)
1104 struct stripe_head
*sh
= bi
->bi_private
;
1105 raid5_conf_t
*conf
= sh
->raid_conf
;
1106 int disks
= sh
->disks
, i
;
1107 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1108 char b
[BDEVNAME_SIZE
];
1112 for (i
=0 ; i
<disks
; i
++)
1113 if (bi
== &sh
->dev
[i
].req
)
1116 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1117 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1125 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1126 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1127 rdev
= conf
->disks
[i
].rdev
;
1128 printk_rl(KERN_INFO
"raid5:%s: read error corrected"
1129 " (%lu sectors at %llu on %s)\n",
1130 mdname(conf
->mddev
), STRIPE_SECTORS
,
1131 (unsigned long long)(sh
->sector
1132 + rdev
->data_offset
),
1133 bdevname(rdev
->bdev
, b
));
1134 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1135 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1137 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
1138 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
1140 const char *bdn
= bdevname(conf
->disks
[i
].rdev
->bdev
, b
);
1142 rdev
= conf
->disks
[i
].rdev
;
1144 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1145 atomic_inc(&rdev
->read_errors
);
1146 if (conf
->mddev
->degraded
)
1147 printk_rl(KERN_WARNING
1148 "raid5:%s: read error not correctable "
1149 "(sector %llu on %s).\n",
1150 mdname(conf
->mddev
),
1151 (unsigned long long)(sh
->sector
1152 + rdev
->data_offset
),
1154 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1156 printk_rl(KERN_WARNING
1157 "raid5:%s: read error NOT corrected!! "
1158 "(sector %llu on %s).\n",
1159 mdname(conf
->mddev
),
1160 (unsigned long long)(sh
->sector
1161 + rdev
->data_offset
),
1163 else if (atomic_read(&rdev
->read_errors
)
1164 > conf
->max_nr_stripes
)
1166 "raid5:%s: Too many read errors, failing device %s.\n",
1167 mdname(conf
->mddev
), bdn
);
1171 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1173 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1174 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1175 md_error(conf
->mddev
, rdev
);
1178 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
1179 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1180 set_bit(STRIPE_HANDLE
, &sh
->state
);
1184 static void raid5_end_write_request(struct bio
*bi
, int error
)
1186 struct stripe_head
*sh
= bi
->bi_private
;
1187 raid5_conf_t
*conf
= sh
->raid_conf
;
1188 int disks
= sh
->disks
, i
;
1189 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1191 for (i
=0 ; i
<disks
; i
++)
1192 if (bi
== &sh
->dev
[i
].req
)
1195 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1196 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1204 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
1206 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
1208 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1209 set_bit(STRIPE_HANDLE
, &sh
->state
);
1214 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
1216 static void raid5_build_block(struct stripe_head
*sh
, int i
)
1218 struct r5dev
*dev
= &sh
->dev
[i
];
1220 bio_init(&dev
->req
);
1221 dev
->req
.bi_io_vec
= &dev
->vec
;
1223 dev
->req
.bi_max_vecs
++;
1224 dev
->vec
.bv_page
= dev
->page
;
1225 dev
->vec
.bv_len
= STRIPE_SIZE
;
1226 dev
->vec
.bv_offset
= 0;
1228 dev
->req
.bi_sector
= sh
->sector
;
1229 dev
->req
.bi_private
= sh
;
1232 dev
->sector
= compute_blocknr(sh
, i
);
1235 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1237 char b
[BDEVNAME_SIZE
];
1238 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
1239 pr_debug("raid5: error called\n");
1241 if (!test_bit(Faulty
, &rdev
->flags
)) {
1242 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1243 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1244 unsigned long flags
;
1245 spin_lock_irqsave(&conf
->device_lock
, flags
);
1247 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1249 * if recovery was running, make sure it aborts.
1251 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1253 set_bit(Faulty
, &rdev
->flags
);
1255 "raid5: Disk failure on %s, disabling device.\n"
1256 "raid5: Operation continuing on %d devices.\n",
1257 bdevname(rdev
->bdev
,b
), conf
->raid_disks
- mddev
->degraded
);
1262 * Input: a 'big' sector number,
1263 * Output: index of the data and parity disk, and the sector # in them.
1265 static sector_t
raid5_compute_sector(raid5_conf_t
*conf
, sector_t r_sector
,
1266 int previous
, int *dd_idx
,
1267 struct stripe_head
*sh
)
1270 unsigned long chunk_number
;
1271 unsigned int chunk_offset
;
1274 sector_t new_sector
;
1275 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1276 int raid_disks
= previous
? conf
->previous_raid_disks
1278 int data_disks
= raid_disks
- conf
->max_degraded
;
1280 /* First compute the information on this sector */
1283 * Compute the chunk number and the sector offset inside the chunk
1285 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
1286 chunk_number
= r_sector
;
1287 BUG_ON(r_sector
!= chunk_number
);
1290 * Compute the stripe number
1292 stripe
= chunk_number
/ data_disks
;
1295 * Compute the data disk and parity disk indexes inside the stripe
1297 *dd_idx
= chunk_number
% data_disks
;
1300 * Select the parity disk based on the user selected algorithm.
1302 pd_idx
= qd_idx
= ~0;
1303 switch(conf
->level
) {
1305 pd_idx
= data_disks
;
1308 switch (conf
->algorithm
) {
1309 case ALGORITHM_LEFT_ASYMMETRIC
:
1310 pd_idx
= data_disks
- stripe
% raid_disks
;
1311 if (*dd_idx
>= pd_idx
)
1314 case ALGORITHM_RIGHT_ASYMMETRIC
:
1315 pd_idx
= stripe
% raid_disks
;
1316 if (*dd_idx
>= pd_idx
)
1319 case ALGORITHM_LEFT_SYMMETRIC
:
1320 pd_idx
= data_disks
- stripe
% raid_disks
;
1321 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1323 case ALGORITHM_RIGHT_SYMMETRIC
:
1324 pd_idx
= stripe
% raid_disks
;
1325 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1327 case ALGORITHM_PARITY_0
:
1331 case ALGORITHM_PARITY_N
:
1332 pd_idx
= data_disks
;
1335 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
1342 switch (conf
->algorithm
) {
1343 case ALGORITHM_LEFT_ASYMMETRIC
:
1344 pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
1345 qd_idx
= pd_idx
+ 1;
1346 if (pd_idx
== raid_disks
-1) {
1347 (*dd_idx
)++; /* Q D D D P */
1349 } else if (*dd_idx
>= pd_idx
)
1350 (*dd_idx
) += 2; /* D D P Q D */
1352 case ALGORITHM_RIGHT_ASYMMETRIC
:
1353 pd_idx
= stripe
% raid_disks
;
1354 qd_idx
= pd_idx
+ 1;
1355 if (pd_idx
== raid_disks
-1) {
1356 (*dd_idx
)++; /* Q D D D P */
1358 } else if (*dd_idx
>= pd_idx
)
1359 (*dd_idx
) += 2; /* D D P Q D */
1361 case ALGORITHM_LEFT_SYMMETRIC
:
1362 pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
1363 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1364 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1366 case ALGORITHM_RIGHT_SYMMETRIC
:
1367 pd_idx
= stripe
% raid_disks
;
1368 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1369 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1372 case ALGORITHM_PARITY_0
:
1377 case ALGORITHM_PARITY_N
:
1378 pd_idx
= data_disks
;
1379 qd_idx
= data_disks
+ 1;
1382 case ALGORITHM_ROTATING_ZERO_RESTART
:
1383 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1384 * of blocks for computing Q is different.
1386 pd_idx
= stripe
% raid_disks
;
1387 qd_idx
= pd_idx
+ 1;
1388 if (pd_idx
== raid_disks
-1) {
1389 (*dd_idx
)++; /* Q D D D P */
1391 } else if (*dd_idx
>= pd_idx
)
1392 (*dd_idx
) += 2; /* D D P Q D */
1396 case ALGORITHM_ROTATING_N_RESTART
:
1397 /* Same a left_asymmetric, by first stripe is
1398 * D D D P Q rather than
1401 pd_idx
= raid_disks
- 1 - ((stripe
+ 1) % raid_disks
);
1402 qd_idx
= pd_idx
+ 1;
1403 if (pd_idx
== raid_disks
-1) {
1404 (*dd_idx
)++; /* Q D D D P */
1406 } else if (*dd_idx
>= pd_idx
)
1407 (*dd_idx
) += 2; /* D D P Q D */
1411 case ALGORITHM_ROTATING_N_CONTINUE
:
1412 /* Same as left_symmetric but Q is before P */
1413 pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
1414 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
1415 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1419 case ALGORITHM_LEFT_ASYMMETRIC_6
:
1420 /* RAID5 left_asymmetric, with Q on last device */
1421 pd_idx
= data_disks
- stripe
% (raid_disks
-1);
1422 if (*dd_idx
>= pd_idx
)
1424 qd_idx
= raid_disks
- 1;
1427 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
1428 pd_idx
= stripe
% (raid_disks
-1);
1429 if (*dd_idx
>= pd_idx
)
1431 qd_idx
= raid_disks
- 1;
1434 case ALGORITHM_LEFT_SYMMETRIC_6
:
1435 pd_idx
= data_disks
- stripe
% (raid_disks
-1);
1436 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
1437 qd_idx
= raid_disks
- 1;
1440 case ALGORITHM_RIGHT_SYMMETRIC_6
:
1441 pd_idx
= stripe
% (raid_disks
-1);
1442 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
1443 qd_idx
= raid_disks
- 1;
1446 case ALGORITHM_PARITY_0_6
:
1449 qd_idx
= raid_disks
- 1;
1454 printk(KERN_CRIT
"raid6: unsupported algorithm %d\n",
1462 sh
->pd_idx
= pd_idx
;
1463 sh
->qd_idx
= qd_idx
;
1464 sh
->ddf_layout
= ddf_layout
;
1467 * Finally, compute the new sector number
1469 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
1474 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
1476 raid5_conf_t
*conf
= sh
->raid_conf
;
1477 int raid_disks
= sh
->disks
;
1478 int data_disks
= raid_disks
- conf
->max_degraded
;
1479 sector_t new_sector
= sh
->sector
, check
;
1480 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1483 int chunk_number
, dummy1
, dd_idx
= i
;
1485 struct stripe_head sh2
;
1488 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
1489 stripe
= new_sector
;
1490 BUG_ON(new_sector
!= stripe
);
1492 if (i
== sh
->pd_idx
)
1494 switch(conf
->level
) {
1497 switch (conf
->algorithm
) {
1498 case ALGORITHM_LEFT_ASYMMETRIC
:
1499 case ALGORITHM_RIGHT_ASYMMETRIC
:
1503 case ALGORITHM_LEFT_SYMMETRIC
:
1504 case ALGORITHM_RIGHT_SYMMETRIC
:
1507 i
-= (sh
->pd_idx
+ 1);
1509 case ALGORITHM_PARITY_0
:
1512 case ALGORITHM_PARITY_N
:
1515 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
1521 if (i
== sh
->qd_idx
)
1522 return 0; /* It is the Q disk */
1523 switch (conf
->algorithm
) {
1524 case ALGORITHM_LEFT_ASYMMETRIC
:
1525 case ALGORITHM_RIGHT_ASYMMETRIC
:
1526 case ALGORITHM_ROTATING_ZERO_RESTART
:
1527 case ALGORITHM_ROTATING_N_RESTART
:
1528 if (sh
->pd_idx
== raid_disks
-1)
1529 i
--; /* Q D D D P */
1530 else if (i
> sh
->pd_idx
)
1531 i
-= 2; /* D D P Q D */
1533 case ALGORITHM_LEFT_SYMMETRIC
:
1534 case ALGORITHM_RIGHT_SYMMETRIC
:
1535 if (sh
->pd_idx
== raid_disks
-1)
1536 i
--; /* Q D D D P */
1541 i
-= (sh
->pd_idx
+ 2);
1544 case ALGORITHM_PARITY_0
:
1547 case ALGORITHM_PARITY_N
:
1549 case ALGORITHM_ROTATING_N_CONTINUE
:
1550 if (sh
->pd_idx
== 0)
1551 i
--; /* P D D D Q */
1552 else if (i
> sh
->pd_idx
)
1553 i
-= 2; /* D D Q P D */
1555 case ALGORITHM_LEFT_ASYMMETRIC_6
:
1556 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
1560 case ALGORITHM_LEFT_SYMMETRIC_6
:
1561 case ALGORITHM_RIGHT_SYMMETRIC_6
:
1563 i
+= data_disks
+ 1;
1564 i
-= (sh
->pd_idx
+ 1);
1566 case ALGORITHM_PARITY_0_6
:
1570 printk(KERN_CRIT
"raid6: unsupported algorithm %d\n",
1577 chunk_number
= stripe
* data_disks
+ i
;
1578 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
1580 check
= raid5_compute_sector(conf
, r_sector
,
1581 (raid_disks
!= conf
->raid_disks
),
1583 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
1584 || sh2
.qd_idx
!= sh
->qd_idx
) {
1585 printk(KERN_ERR
"compute_blocknr: map not correct\n");
1594 * Copy data between a page in the stripe cache, and one or more bion
1595 * The page could align with the middle of the bio, or there could be
1596 * several bion, each with several bio_vecs, which cover part of the page
1597 * Multiple bion are linked together on bi_next. There may be extras
1598 * at the end of this list. We ignore them.
1600 static void copy_data(int frombio
, struct bio
*bio
,
1604 char *pa
= page_address(page
);
1605 struct bio_vec
*bvl
;
1609 if (bio
->bi_sector
>= sector
)
1610 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
1612 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
1613 bio_for_each_segment(bvl
, bio
, i
) {
1614 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
1618 if (page_offset
< 0) {
1619 b_offset
= -page_offset
;
1620 page_offset
+= b_offset
;
1624 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
1625 clen
= STRIPE_SIZE
- page_offset
;
1629 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
1631 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
1633 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
1634 __bio_kunmap_atomic(ba
, KM_USER0
);
1636 if (clen
< len
) /* hit end of page */
1642 #define check_xor() do { \
1643 if (count == MAX_XOR_BLOCKS) { \
1644 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1649 static void compute_parity6(struct stripe_head
*sh
, int method
)
1651 raid5_conf_t
*conf
= sh
->raid_conf
;
1652 int i
, pd_idx
, qd_idx
, d0_idx
, disks
= sh
->disks
, count
;
1653 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1655 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1656 void *ptrs
[syndrome_disks
+2];
1658 pd_idx
= sh
->pd_idx
;
1659 qd_idx
= sh
->qd_idx
;
1660 d0_idx
= raid6_d0(sh
);
1662 pr_debug("compute_parity, stripe %llu, method %d\n",
1663 (unsigned long long)sh
->sector
, method
);
1666 case READ_MODIFY_WRITE
:
1667 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1668 case RECONSTRUCT_WRITE
:
1669 for (i
= disks
; i
-- ;)
1670 if ( i
!= pd_idx
&& i
!= qd_idx
&& sh
->dev
[i
].towrite
) {
1671 chosen
= sh
->dev
[i
].towrite
;
1672 sh
->dev
[i
].towrite
= NULL
;
1674 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1675 wake_up(&conf
->wait_for_overlap
);
1677 BUG_ON(sh
->dev
[i
].written
);
1678 sh
->dev
[i
].written
= chosen
;
1682 BUG(); /* Not implemented yet */
1685 for (i
= disks
; i
--;)
1686 if (sh
->dev
[i
].written
) {
1687 sector_t sector
= sh
->dev
[i
].sector
;
1688 struct bio
*wbi
= sh
->dev
[i
].written
;
1689 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
1690 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
1691 wbi
= r5_next_bio(wbi
, sector
);
1694 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1695 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1698 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
1700 for (i
= 0; i
< disks
; i
++)
1701 ptrs
[i
] = (void *)raid6_empty_zero_page
;
1706 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1708 ptrs
[slot
] = page_address(sh
->dev
[i
].page
);
1709 if (slot
< syndrome_disks
&&
1710 !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
)) {
1711 printk(KERN_ERR
"block %d/%d not uptodate "
1712 "on parity calc\n", i
, count
);
1716 i
= raid6_next_disk(i
, disks
);
1717 } while (i
!= d0_idx
);
1718 BUG_ON(count
!= syndrome_disks
);
1720 raid6_call
.gen_syndrome(syndrome_disks
+2, STRIPE_SIZE
, ptrs
);
1723 case RECONSTRUCT_WRITE
:
1724 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1725 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
1726 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
1727 set_bit(R5_LOCKED
, &sh
->dev
[qd_idx
].flags
);
1730 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1731 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
1737 /* Compute one missing block */
1738 static void compute_block_1(struct stripe_head
*sh
, int dd_idx
, int nozero
)
1740 int i
, count
, disks
= sh
->disks
;
1741 void *ptr
[MAX_XOR_BLOCKS
], *dest
, *p
;
1742 int qd_idx
= sh
->qd_idx
;
1744 pr_debug("compute_block_1, stripe %llu, idx %d\n",
1745 (unsigned long long)sh
->sector
, dd_idx
);
1747 if ( dd_idx
== qd_idx
) {
1748 /* We're actually computing the Q drive */
1749 compute_parity6(sh
, UPDATE_PARITY
);
1751 dest
= page_address(sh
->dev
[dd_idx
].page
);
1752 if (!nozero
) memset(dest
, 0, STRIPE_SIZE
);
1754 for (i
= disks
; i
--; ) {
1755 if (i
== dd_idx
|| i
== qd_idx
)
1757 p
= page_address(sh
->dev
[i
].page
);
1758 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1761 printk("compute_block() %d, stripe %llu, %d"
1762 " not present\n", dd_idx
,
1763 (unsigned long long)sh
->sector
, i
);
1768 xor_blocks(count
, STRIPE_SIZE
, dest
, ptr
);
1769 if (!nozero
) set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
1770 else clear_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
1774 /* Compute two missing blocks */
1775 static void compute_block_2(struct stripe_head
*sh
, int dd_idx1
, int dd_idx2
)
1777 int i
, count
, disks
= sh
->disks
;
1778 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1779 int d0_idx
= raid6_d0(sh
);
1780 int faila
= -1, failb
= -1;
1781 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1782 void *ptrs
[syndrome_disks
+2];
1784 for (i
= 0; i
< disks
; i
++)
1785 ptrs
[i
] = (void *)raid6_empty_zero_page
;
1789 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1791 ptrs
[slot
] = page_address(sh
->dev
[i
].page
);
1797 i
= raid6_next_disk(i
, disks
);
1798 } while (i
!= d0_idx
);
1799 BUG_ON(count
!= syndrome_disks
);
1801 BUG_ON(faila
== failb
);
1802 if ( failb
< faila
) { int tmp
= faila
; faila
= failb
; failb
= tmp
; }
1804 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1805 (unsigned long long)sh
->sector
, dd_idx1
, dd_idx2
,
1808 if (failb
== syndrome_disks
+1) {
1809 /* Q disk is one of the missing disks */
1810 if (faila
== syndrome_disks
) {
1811 /* Missing P+Q, just recompute */
1812 compute_parity6(sh
, UPDATE_PARITY
);
1815 /* We're missing D+Q; recompute D from P */
1816 compute_block_1(sh
, ((dd_idx1
== sh
->qd_idx
) ?
1819 compute_parity6(sh
, UPDATE_PARITY
); /* Is this necessary? */
1824 /* We're missing D+P or D+D; */
1825 if (failb
== syndrome_disks
) {
1826 /* We're missing D+P. */
1827 raid6_datap_recov(syndrome_disks
+2, STRIPE_SIZE
, faila
, ptrs
);
1829 /* We're missing D+D. */
1830 raid6_2data_recov(syndrome_disks
+2, STRIPE_SIZE
, faila
, failb
,
1834 /* Both the above update both missing blocks */
1835 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx1
].flags
);
1836 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx2
].flags
);
1840 schedule_reconstruction5(struct stripe_head
*sh
, struct stripe_head_state
*s
,
1841 int rcw
, int expand
)
1843 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
1846 /* if we are not expanding this is a proper write request, and
1847 * there will be bios with new data to be drained into the
1851 sh
->reconstruct_state
= reconstruct_state_drain_run
;
1852 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
1854 sh
->reconstruct_state
= reconstruct_state_run
;
1856 set_bit(STRIPE_OP_POSTXOR
, &s
->ops_request
);
1858 for (i
= disks
; i
--; ) {
1859 struct r5dev
*dev
= &sh
->dev
[i
];
1862 set_bit(R5_LOCKED
, &dev
->flags
);
1863 set_bit(R5_Wantdrain
, &dev
->flags
);
1865 clear_bit(R5_UPTODATE
, &dev
->flags
);
1869 if (s
->locked
+ 1 == disks
)
1870 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
1871 atomic_inc(&sh
->raid_conf
->pending_full_writes
);
1873 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
1874 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
1876 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
1877 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
1878 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
1879 set_bit(STRIPE_OP_POSTXOR
, &s
->ops_request
);
1881 for (i
= disks
; i
--; ) {
1882 struct r5dev
*dev
= &sh
->dev
[i
];
1887 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
1888 test_bit(R5_Wantcompute
, &dev
->flags
))) {
1889 set_bit(R5_Wantdrain
, &dev
->flags
);
1890 set_bit(R5_LOCKED
, &dev
->flags
);
1891 clear_bit(R5_UPTODATE
, &dev
->flags
);
1897 /* keep the parity disk locked while asynchronous operations
1900 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
1901 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1904 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
1905 __func__
, (unsigned long long)sh
->sector
,
1906 s
->locked
, s
->ops_request
);
1910 * Each stripe/dev can have one or more bion attached.
1911 * toread/towrite point to the first in a chain.
1912 * The bi_next chain must be in order.
1914 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
1917 raid5_conf_t
*conf
= sh
->raid_conf
;
1920 pr_debug("adding bh b#%llu to stripe s#%llu\n",
1921 (unsigned long long)bi
->bi_sector
,
1922 (unsigned long long)sh
->sector
);
1925 spin_lock(&sh
->lock
);
1926 spin_lock_irq(&conf
->device_lock
);
1928 bip
= &sh
->dev
[dd_idx
].towrite
;
1929 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
1932 bip
= &sh
->dev
[dd_idx
].toread
;
1933 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
1934 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
1936 bip
= & (*bip
)->bi_next
;
1938 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
1941 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
1945 bi
->bi_phys_segments
++;
1946 spin_unlock_irq(&conf
->device_lock
);
1947 spin_unlock(&sh
->lock
);
1949 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
1950 (unsigned long long)bi
->bi_sector
,
1951 (unsigned long long)sh
->sector
, dd_idx
);
1953 if (conf
->mddev
->bitmap
&& firstwrite
) {
1954 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
1956 sh
->bm_seq
= conf
->seq_flush
+1;
1957 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
1961 /* check if page is covered */
1962 sector_t sector
= sh
->dev
[dd_idx
].sector
;
1963 for (bi
=sh
->dev
[dd_idx
].towrite
;
1964 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
1965 bi
&& bi
->bi_sector
<= sector
;
1966 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
1967 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
1968 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1970 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
1971 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
1976 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
1977 spin_unlock_irq(&conf
->device_lock
);
1978 spin_unlock(&sh
->lock
);
1982 static void end_reshape(raid5_conf_t
*conf
);
1984 static int page_is_zero(struct page
*p
)
1986 char *a
= page_address(p
);
1987 return ((*(u32
*)a
) == 0 &&
1988 memcmp(a
, a
+4, STRIPE_SIZE
-4)==0);
1991 static void stripe_set_idx(sector_t stripe
, raid5_conf_t
*conf
, int previous
,
1992 struct stripe_head
*sh
)
1994 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1996 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
1997 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
1999 raid5_compute_sector(conf
,
2000 stripe
* (disks
- conf
->max_degraded
)
2001 *sectors_per_chunk
+ chunk_offset
,
2007 handle_failed_stripe(raid5_conf_t
*conf
, struct stripe_head
*sh
,
2008 struct stripe_head_state
*s
, int disks
,
2009 struct bio
**return_bi
)
2012 for (i
= disks
; i
--; ) {
2016 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2019 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2020 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2021 /* multiple read failures in one stripe */
2022 md_error(conf
->mddev
, rdev
);
2025 spin_lock_irq(&conf
->device_lock
);
2026 /* fail all writes first */
2027 bi
= sh
->dev
[i
].towrite
;
2028 sh
->dev
[i
].towrite
= NULL
;
2034 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2035 wake_up(&conf
->wait_for_overlap
);
2037 while (bi
&& bi
->bi_sector
<
2038 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2039 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2040 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2041 if (!raid5_dec_bi_phys_segments(bi
)) {
2042 md_write_end(conf
->mddev
);
2043 bi
->bi_next
= *return_bi
;
2048 /* and fail all 'written' */
2049 bi
= sh
->dev
[i
].written
;
2050 sh
->dev
[i
].written
= NULL
;
2051 if (bi
) bitmap_end
= 1;
2052 while (bi
&& bi
->bi_sector
<
2053 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2054 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2055 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2056 if (!raid5_dec_bi_phys_segments(bi
)) {
2057 md_write_end(conf
->mddev
);
2058 bi
->bi_next
= *return_bi
;
2064 /* fail any reads if this device is non-operational and
2065 * the data has not reached the cache yet.
2067 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2068 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2069 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2070 bi
= sh
->dev
[i
].toread
;
2071 sh
->dev
[i
].toread
= NULL
;
2072 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2073 wake_up(&conf
->wait_for_overlap
);
2074 if (bi
) s
->to_read
--;
2075 while (bi
&& bi
->bi_sector
<
2076 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2077 struct bio
*nextbi
=
2078 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2079 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2080 if (!raid5_dec_bi_phys_segments(bi
)) {
2081 bi
->bi_next
= *return_bi
;
2087 spin_unlock_irq(&conf
->device_lock
);
2089 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2090 STRIPE_SECTORS
, 0, 0);
2093 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2094 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2095 md_wakeup_thread(conf
->mddev
->thread
);
2098 /* fetch_block5 - checks the given member device to see if its data needs
2099 * to be read or computed to satisfy a request.
2101 * Returns 1 when no more member devices need to be checked, otherwise returns
2102 * 0 to tell the loop in handle_stripe_fill5 to continue
2104 static int fetch_block5(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2105 int disk_idx
, int disks
)
2107 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2108 struct r5dev
*failed_dev
= &sh
->dev
[s
->failed_num
];
2110 /* is the data in this block needed, and can we get it? */
2111 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2112 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2114 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2115 s
->syncing
|| s
->expanding
||
2117 (failed_dev
->toread
||
2118 (failed_dev
->towrite
&&
2119 !test_bit(R5_OVERWRITE
, &failed_dev
->flags
)))))) {
2120 /* We would like to get this block, possibly by computing it,
2121 * otherwise read it if the backing disk is insync
2123 if ((s
->uptodate
== disks
- 1) &&
2124 (s
->failed
&& disk_idx
== s
->failed_num
)) {
2125 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2126 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2127 set_bit(R5_Wantcompute
, &dev
->flags
);
2128 sh
->ops
.target
= disk_idx
;
2130 /* Careful: from this point on 'uptodate' is in the eye
2131 * of raid5_run_ops which services 'compute' operations
2132 * before writes. R5_Wantcompute flags a block that will
2133 * be R5_UPTODATE by the time it is needed for a
2134 * subsequent operation.
2137 return 1; /* uptodate + compute == disks */
2138 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2139 set_bit(R5_LOCKED
, &dev
->flags
);
2140 set_bit(R5_Wantread
, &dev
->flags
);
2142 pr_debug("Reading block %d (sync=%d)\n", disk_idx
,
2151 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2153 static void handle_stripe_fill5(struct stripe_head
*sh
,
2154 struct stripe_head_state
*s
, int disks
)
2158 /* look for blocks to read/compute, skip this if a compute
2159 * is already in flight, or if the stripe contents are in the
2160 * midst of changing due to a write
2162 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2163 !sh
->reconstruct_state
)
2164 for (i
= disks
; i
--; )
2165 if (fetch_block5(sh
, s
, i
, disks
))
2167 set_bit(STRIPE_HANDLE
, &sh
->state
);
2170 static void handle_stripe_fill6(struct stripe_head
*sh
,
2171 struct stripe_head_state
*s
, struct r6_state
*r6s
,
2175 for (i
= disks
; i
--; ) {
2176 struct r5dev
*dev
= &sh
->dev
[i
];
2177 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2178 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2179 (dev
->toread
|| (dev
->towrite
&&
2180 !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2181 s
->syncing
|| s
->expanding
||
2183 (sh
->dev
[r6s
->failed_num
[0]].toread
||
2186 (sh
->dev
[r6s
->failed_num
[1]].toread
||
2188 /* we would like to get this block, possibly
2189 * by computing it, but we might not be able to
2191 if ((s
->uptodate
== disks
- 1) &&
2192 (s
->failed
&& (i
== r6s
->failed_num
[0] ||
2193 i
== r6s
->failed_num
[1]))) {
2194 pr_debug("Computing stripe %llu block %d\n",
2195 (unsigned long long)sh
->sector
, i
);
2196 compute_block_1(sh
, i
, 0);
2198 } else if ( s
->uptodate
== disks
-2 && s
->failed
>= 2 ) {
2199 /* Computing 2-failure is *very* expensive; only
2200 * do it if failed >= 2
2203 for (other
= disks
; other
--; ) {
2206 if (!test_bit(R5_UPTODATE
,
2207 &sh
->dev
[other
].flags
))
2211 pr_debug("Computing stripe %llu blocks %d,%d\n",
2212 (unsigned long long)sh
->sector
,
2214 compute_block_2(sh
, i
, other
);
2216 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2217 set_bit(R5_LOCKED
, &dev
->flags
);
2218 set_bit(R5_Wantread
, &dev
->flags
);
2220 pr_debug("Reading block %d (sync=%d)\n",
2225 set_bit(STRIPE_HANDLE
, &sh
->state
);
2229 /* handle_stripe_clean_event
2230 * any written block on an uptodate or failed drive can be returned.
2231 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2232 * never LOCKED, so we don't need to test 'failed' directly.
2234 static void handle_stripe_clean_event(raid5_conf_t
*conf
,
2235 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2240 for (i
= disks
; i
--; )
2241 if (sh
->dev
[i
].written
) {
2243 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2244 test_bit(R5_UPTODATE
, &dev
->flags
)) {
2245 /* We can return any write requests */
2246 struct bio
*wbi
, *wbi2
;
2248 pr_debug("Return write for disc %d\n", i
);
2249 spin_lock_irq(&conf
->device_lock
);
2251 dev
->written
= NULL
;
2252 while (wbi
&& wbi
->bi_sector
<
2253 dev
->sector
+ STRIPE_SECTORS
) {
2254 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2255 if (!raid5_dec_bi_phys_segments(wbi
)) {
2256 md_write_end(conf
->mddev
);
2257 wbi
->bi_next
= *return_bi
;
2262 if (dev
->towrite
== NULL
)
2264 spin_unlock_irq(&conf
->device_lock
);
2266 bitmap_endwrite(conf
->mddev
->bitmap
,
2269 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
2274 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2275 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2276 md_wakeup_thread(conf
->mddev
->thread
);
2279 static void handle_stripe_dirtying5(raid5_conf_t
*conf
,
2280 struct stripe_head
*sh
, struct stripe_head_state
*s
, int disks
)
2282 int rmw
= 0, rcw
= 0, i
;
2283 for (i
= disks
; i
--; ) {
2284 /* would I have to read this buffer for read_modify_write */
2285 struct r5dev
*dev
= &sh
->dev
[i
];
2286 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2287 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2288 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2289 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2290 if (test_bit(R5_Insync
, &dev
->flags
))
2293 rmw
+= 2*disks
; /* cannot read it */
2295 /* Would I have to read this buffer for reconstruct_write */
2296 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
2297 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2298 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2299 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2300 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2305 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2306 (unsigned long long)sh
->sector
, rmw
, rcw
);
2307 set_bit(STRIPE_HANDLE
, &sh
->state
);
2308 if (rmw
< rcw
&& rmw
> 0)
2309 /* prefer read-modify-write, but need to get some data */
2310 for (i
= disks
; i
--; ) {
2311 struct r5dev
*dev
= &sh
->dev
[i
];
2312 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2313 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2314 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2315 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2316 test_bit(R5_Insync
, &dev
->flags
)) {
2318 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2319 pr_debug("Read_old block "
2320 "%d for r-m-w\n", i
);
2321 set_bit(R5_LOCKED
, &dev
->flags
);
2322 set_bit(R5_Wantread
, &dev
->flags
);
2325 set_bit(STRIPE_DELAYED
, &sh
->state
);
2326 set_bit(STRIPE_HANDLE
, &sh
->state
);
2330 if (rcw
<= rmw
&& rcw
> 0)
2331 /* want reconstruct write, but need to get some data */
2332 for (i
= disks
; i
--; ) {
2333 struct r5dev
*dev
= &sh
->dev
[i
];
2334 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2336 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2337 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2338 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2339 test_bit(R5_Insync
, &dev
->flags
)) {
2341 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2342 pr_debug("Read_old block "
2343 "%d for Reconstruct\n", i
);
2344 set_bit(R5_LOCKED
, &dev
->flags
);
2345 set_bit(R5_Wantread
, &dev
->flags
);
2348 set_bit(STRIPE_DELAYED
, &sh
->state
);
2349 set_bit(STRIPE_HANDLE
, &sh
->state
);
2353 /* now if nothing is locked, and if we have enough data,
2354 * we can start a write request
2356 /* since handle_stripe can be called at any time we need to handle the
2357 * case where a compute block operation has been submitted and then a
2358 * subsequent call wants to start a write request. raid5_run_ops only
2359 * handles the case where compute block and postxor are requested
2360 * simultaneously. If this is not the case then new writes need to be
2361 * held off until the compute completes.
2363 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
2364 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
2365 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
2366 schedule_reconstruction5(sh
, s
, rcw
== 0, 0);
2369 static void handle_stripe_dirtying6(raid5_conf_t
*conf
,
2370 struct stripe_head
*sh
, struct stripe_head_state
*s
,
2371 struct r6_state
*r6s
, int disks
)
2373 int rcw
= 0, must_compute
= 0, pd_idx
= sh
->pd_idx
, i
;
2374 int qd_idx
= sh
->qd_idx
;
2375 for (i
= disks
; i
--; ) {
2376 struct r5dev
*dev
= &sh
->dev
[i
];
2377 /* Would I have to read this buffer for reconstruct_write */
2378 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
2379 && i
!= pd_idx
&& i
!= qd_idx
2380 && (!test_bit(R5_LOCKED
, &dev
->flags
)
2382 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
2383 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2385 pr_debug("raid6: must_compute: "
2386 "disk %d flags=%#lx\n", i
, dev
->flags
);
2391 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
2392 (unsigned long long)sh
->sector
, rcw
, must_compute
);
2393 set_bit(STRIPE_HANDLE
, &sh
->state
);
2396 /* want reconstruct write, but need to get some data */
2397 for (i
= disks
; i
--; ) {
2398 struct r5dev
*dev
= &sh
->dev
[i
];
2399 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
2400 && !(s
->failed
== 0 && (i
== pd_idx
|| i
== qd_idx
))
2401 && !test_bit(R5_LOCKED
, &dev
->flags
) &&
2402 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2403 test_bit(R5_Insync
, &dev
->flags
)) {
2405 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2406 pr_debug("Read_old stripe %llu "
2407 "block %d for Reconstruct\n",
2408 (unsigned long long)sh
->sector
, i
);
2409 set_bit(R5_LOCKED
, &dev
->flags
);
2410 set_bit(R5_Wantread
, &dev
->flags
);
2413 pr_debug("Request delayed stripe %llu "
2414 "block %d for Reconstruct\n",
2415 (unsigned long long)sh
->sector
, i
);
2416 set_bit(STRIPE_DELAYED
, &sh
->state
);
2417 set_bit(STRIPE_HANDLE
, &sh
->state
);
2421 /* now if nothing is locked, and if we have enough data, we can start a
2424 if (s
->locked
== 0 && rcw
== 0 &&
2425 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
2426 if (must_compute
> 0) {
2427 /* We have failed blocks and need to compute them */
2428 switch (s
->failed
) {
2432 compute_block_1(sh
, r6s
->failed_num
[0], 0);
2435 compute_block_2(sh
, r6s
->failed_num
[0],
2436 r6s
->failed_num
[1]);
2438 default: /* This request should have been failed? */
2443 pr_debug("Computing parity for stripe %llu\n",
2444 (unsigned long long)sh
->sector
);
2445 compute_parity6(sh
, RECONSTRUCT_WRITE
);
2446 /* now every locked buffer is ready to be written */
2447 for (i
= disks
; i
--; )
2448 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
2449 pr_debug("Writing stripe %llu block %d\n",
2450 (unsigned long long)sh
->sector
, i
);
2452 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
2454 if (s
->locked
== disks
)
2455 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2456 atomic_inc(&conf
->pending_full_writes
);
2457 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2458 set_bit(STRIPE_INSYNC
, &sh
->state
);
2460 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2461 atomic_dec(&conf
->preread_active_stripes
);
2462 if (atomic_read(&conf
->preread_active_stripes
) <
2464 md_wakeup_thread(conf
->mddev
->thread
);
2469 static void handle_parity_checks5(raid5_conf_t
*conf
, struct stripe_head
*sh
,
2470 struct stripe_head_state
*s
, int disks
)
2472 struct r5dev
*dev
= NULL
;
2474 set_bit(STRIPE_HANDLE
, &sh
->state
);
2476 switch (sh
->check_state
) {
2477 case check_state_idle
:
2478 /* start a new check operation if there are no failures */
2479 if (s
->failed
== 0) {
2480 BUG_ON(s
->uptodate
!= disks
);
2481 sh
->check_state
= check_state_run
;
2482 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2483 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
2487 dev
= &sh
->dev
[s
->failed_num
];
2489 case check_state_compute_result
:
2490 sh
->check_state
= check_state_idle
;
2492 dev
= &sh
->dev
[sh
->pd_idx
];
2494 /* check that a write has not made the stripe insync */
2495 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2498 /* either failed parity check, or recovery is happening */
2499 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
2500 BUG_ON(s
->uptodate
!= disks
);
2502 set_bit(R5_LOCKED
, &dev
->flags
);
2504 set_bit(R5_Wantwrite
, &dev
->flags
);
2506 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2507 set_bit(STRIPE_INSYNC
, &sh
->state
);
2509 case check_state_run
:
2510 break; /* we will be called again upon completion */
2511 case check_state_check_result
:
2512 sh
->check_state
= check_state_idle
;
2514 /* if a failure occurred during the check operation, leave
2515 * STRIPE_INSYNC not set and let the stripe be handled again
2520 /* handle a successful check operation, if parity is correct
2521 * we are done. Otherwise update the mismatch count and repair
2522 * parity if !MD_RECOVERY_CHECK
2524 if (sh
->ops
.zero_sum_result
== 0)
2525 /* parity is correct (on disc,
2526 * not in buffer any more)
2528 set_bit(STRIPE_INSYNC
, &sh
->state
);
2530 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2531 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2532 /* don't try to repair!! */
2533 set_bit(STRIPE_INSYNC
, &sh
->state
);
2535 sh
->check_state
= check_state_compute_run
;
2536 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2537 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2538 set_bit(R5_Wantcompute
,
2539 &sh
->dev
[sh
->pd_idx
].flags
);
2540 sh
->ops
.target
= sh
->pd_idx
;
2545 case check_state_compute_run
:
2548 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
2549 __func__
, sh
->check_state
,
2550 (unsigned long long) sh
->sector
);
2556 static void handle_parity_checks6(raid5_conf_t
*conf
, struct stripe_head
*sh
,
2557 struct stripe_head_state
*s
,
2558 struct r6_state
*r6s
, struct page
*tmp_page
,
2561 int update_p
= 0, update_q
= 0;
2563 int pd_idx
= sh
->pd_idx
;
2564 int qd_idx
= sh
->qd_idx
;
2566 set_bit(STRIPE_HANDLE
, &sh
->state
);
2568 BUG_ON(s
->failed
> 2);
2569 BUG_ON(s
->uptodate
< disks
);
2570 /* Want to check and possibly repair P and Q.
2571 * However there could be one 'failed' device, in which
2572 * case we can only check one of them, possibly using the
2573 * other to generate missing data
2576 /* If !tmp_page, we cannot do the calculations,
2577 * but as we have set STRIPE_HANDLE, we will soon be called
2578 * by stripe_handle with a tmp_page - just wait until then.
2581 if (s
->failed
== r6s
->q_failed
) {
2582 /* The only possible failed device holds 'Q', so it
2583 * makes sense to check P (If anything else were failed,
2584 * we would have used P to recreate it).
2586 compute_block_1(sh
, pd_idx
, 1);
2587 if (!page_is_zero(sh
->dev
[pd_idx
].page
)) {
2588 compute_block_1(sh
, pd_idx
, 0);
2592 if (!r6s
->q_failed
&& s
->failed
< 2) {
2593 /* q is not failed, and we didn't use it to generate
2594 * anything, so it makes sense to check it
2596 memcpy(page_address(tmp_page
),
2597 page_address(sh
->dev
[qd_idx
].page
),
2599 compute_parity6(sh
, UPDATE_PARITY
);
2600 if (memcmp(page_address(tmp_page
),
2601 page_address(sh
->dev
[qd_idx
].page
),
2602 STRIPE_SIZE
) != 0) {
2603 clear_bit(STRIPE_INSYNC
, &sh
->state
);
2607 if (update_p
|| update_q
) {
2608 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2609 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2610 /* don't try to repair!! */
2611 update_p
= update_q
= 0;
2614 /* now write out any block on a failed drive,
2615 * or P or Q if they need it
2618 if (s
->failed
== 2) {
2619 dev
= &sh
->dev
[r6s
->failed_num
[1]];
2621 set_bit(R5_LOCKED
, &dev
->flags
);
2622 set_bit(R5_Wantwrite
, &dev
->flags
);
2624 if (s
->failed
>= 1) {
2625 dev
= &sh
->dev
[r6s
->failed_num
[0]];
2627 set_bit(R5_LOCKED
, &dev
->flags
);
2628 set_bit(R5_Wantwrite
, &dev
->flags
);
2632 dev
= &sh
->dev
[pd_idx
];
2634 set_bit(R5_LOCKED
, &dev
->flags
);
2635 set_bit(R5_Wantwrite
, &dev
->flags
);
2638 dev
= &sh
->dev
[qd_idx
];
2640 set_bit(R5_LOCKED
, &dev
->flags
);
2641 set_bit(R5_Wantwrite
, &dev
->flags
);
2643 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2645 set_bit(STRIPE_INSYNC
, &sh
->state
);
2649 static void handle_stripe_expansion(raid5_conf_t
*conf
, struct stripe_head
*sh
,
2650 struct r6_state
*r6s
)
2654 /* We have read all the blocks in this stripe and now we need to
2655 * copy some of them into a target stripe for expand.
2657 struct dma_async_tx_descriptor
*tx
= NULL
;
2658 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
2659 for (i
= 0; i
< sh
->disks
; i
++)
2660 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
2662 struct stripe_head
*sh2
;
2664 sector_t bn
= compute_blocknr(sh
, i
);
2665 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
2667 sh2
= get_active_stripe(conf
, s
, 0, 1);
2669 /* so far only the early blocks of this stripe
2670 * have been requested. When later blocks
2671 * get requested, we will try again
2674 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
2675 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
2676 /* must have already done this block */
2677 release_stripe(sh2
);
2681 /* place all the copies on one channel */
2682 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
2683 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
2684 ASYNC_TX_DEP_ACK
, tx
, NULL
, NULL
);
2686 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
2687 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
2688 for (j
= 0; j
< conf
->raid_disks
; j
++)
2689 if (j
!= sh2
->pd_idx
&&
2690 (!r6s
|| j
!= sh2
->qd_idx
) &&
2691 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
2693 if (j
== conf
->raid_disks
) {
2694 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
2695 set_bit(STRIPE_HANDLE
, &sh2
->state
);
2697 release_stripe(sh2
);
2700 /* done submitting copies, wait for them to complete */
2703 dma_wait_for_async_tx(tx
);
2709 * handle_stripe - do things to a stripe.
2711 * We lock the stripe and then examine the state of various bits
2712 * to see what needs to be done.
2714 * return some read request which now have data
2715 * return some write requests which are safely on disc
2716 * schedule a read on some buffers
2717 * schedule a write of some buffers
2718 * return confirmation of parity correctness
2720 * buffers are taken off read_list or write_list, and bh_cache buffers
2721 * get BH_Lock set before the stripe lock is released.
2725 static bool handle_stripe5(struct stripe_head
*sh
)
2727 raid5_conf_t
*conf
= sh
->raid_conf
;
2728 int disks
= sh
->disks
, i
;
2729 struct bio
*return_bi
= NULL
;
2730 struct stripe_head_state s
;
2732 mdk_rdev_t
*blocked_rdev
= NULL
;
2735 memset(&s
, 0, sizeof(s
));
2736 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2737 "reconstruct:%d\n", (unsigned long long)sh
->sector
, sh
->state
,
2738 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->check_state
,
2739 sh
->reconstruct_state
);
2741 spin_lock(&sh
->lock
);
2742 clear_bit(STRIPE_HANDLE
, &sh
->state
);
2743 clear_bit(STRIPE_DELAYED
, &sh
->state
);
2745 s
.syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
2746 s
.expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
2747 s
.expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
2749 /* Now to look around and see what can be done */
2751 for (i
=disks
; i
--; ) {
2753 struct r5dev
*dev
= &sh
->dev
[i
];
2754 clear_bit(R5_Insync
, &dev
->flags
);
2756 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2757 "written %p\n", i
, dev
->flags
, dev
->toread
, dev
->read
,
2758 dev
->towrite
, dev
->written
);
2760 /* maybe we can request a biofill operation
2762 * new wantfill requests are only permitted while
2763 * ops_complete_biofill is guaranteed to be inactive
2765 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
2766 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
2767 set_bit(R5_Wantfill
, &dev
->flags
);
2769 /* now count some things */
2770 if (test_bit(R5_LOCKED
, &dev
->flags
)) s
.locked
++;
2771 if (test_bit(R5_UPTODATE
, &dev
->flags
)) s
.uptodate
++;
2772 if (test_bit(R5_Wantcompute
, &dev
->flags
)) s
.compute
++;
2774 if (test_bit(R5_Wantfill
, &dev
->flags
))
2776 else if (dev
->toread
)
2780 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
2785 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2786 if (blocked_rdev
== NULL
&&
2787 rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
2788 blocked_rdev
= rdev
;
2789 atomic_inc(&rdev
->nr_pending
);
2791 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
2792 /* The ReadError flag will just be confusing now */
2793 clear_bit(R5_ReadError
, &dev
->flags
);
2794 clear_bit(R5_ReWrite
, &dev
->flags
);
2796 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
2797 || test_bit(R5_ReadError
, &dev
->flags
)) {
2801 set_bit(R5_Insync
, &dev
->flags
);
2805 if (unlikely(blocked_rdev
)) {
2806 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
2807 s
.to_write
|| s
.written
) {
2808 set_bit(STRIPE_HANDLE
, &sh
->state
);
2811 /* There is nothing for the blocked_rdev to block */
2812 rdev_dec_pending(blocked_rdev
, conf
->mddev
);
2813 blocked_rdev
= NULL
;
2816 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
2817 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
2818 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
2821 pr_debug("locked=%d uptodate=%d to_read=%d"
2822 " to_write=%d failed=%d failed_num=%d\n",
2823 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
,
2824 s
.failed
, s
.failed_num
);
2825 /* check if the array has lost two devices and, if so, some requests might
2828 if (s
.failed
> 1 && s
.to_read
+s
.to_write
+s
.written
)
2829 handle_failed_stripe(conf
, sh
, &s
, disks
, &return_bi
);
2830 if (s
.failed
> 1 && s
.syncing
) {
2831 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
2832 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2836 /* might be able to return some write requests if the parity block
2837 * is safe, or on a failed drive
2839 dev
= &sh
->dev
[sh
->pd_idx
];
2841 ((test_bit(R5_Insync
, &dev
->flags
) &&
2842 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2843 test_bit(R5_UPTODATE
, &dev
->flags
)) ||
2844 (s
.failed
== 1 && s
.failed_num
== sh
->pd_idx
)))
2845 handle_stripe_clean_event(conf
, sh
, disks
, &return_bi
);
2847 /* Now we might consider reading some blocks, either to check/generate
2848 * parity, or to satisfy requests
2849 * or to load a block that is being partially written.
2851 if (s
.to_read
|| s
.non_overwrite
||
2852 (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
)) || s
.expanding
)
2853 handle_stripe_fill5(sh
, &s
, disks
);
2855 /* Now we check to see if any write operations have recently
2859 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
2861 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
2862 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
2863 sh
->reconstruct_state
= reconstruct_state_idle
;
2865 /* All the 'written' buffers and the parity block are ready to
2866 * be written back to disk
2868 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
));
2869 for (i
= disks
; i
--; ) {
2871 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
2872 (i
== sh
->pd_idx
|| dev
->written
)) {
2873 pr_debug("Writing block %d\n", i
);
2874 set_bit(R5_Wantwrite
, &dev
->flags
);
2877 if (!test_bit(R5_Insync
, &dev
->flags
) ||
2878 (i
== sh
->pd_idx
&& s
.failed
== 0))
2879 set_bit(STRIPE_INSYNC
, &sh
->state
);
2882 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2883 atomic_dec(&conf
->preread_active_stripes
);
2884 if (atomic_read(&conf
->preread_active_stripes
) <
2886 md_wakeup_thread(conf
->mddev
->thread
);
2890 /* Now to consider new write requests and what else, if anything
2891 * should be read. We do not handle new writes when:
2892 * 1/ A 'write' operation (copy+xor) is already in flight.
2893 * 2/ A 'check' operation is in flight, as it may clobber the parity
2896 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
2897 handle_stripe_dirtying5(conf
, sh
, &s
, disks
);
2899 /* maybe we need to check and possibly fix the parity for this stripe
2900 * Any reads will already have been scheduled, so we just see if enough
2901 * data is available. The parity check is held off while parity
2902 * dependent operations are in flight.
2904 if (sh
->check_state
||
2905 (s
.syncing
&& s
.locked
== 0 &&
2906 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
2907 !test_bit(STRIPE_INSYNC
, &sh
->state
)))
2908 handle_parity_checks5(conf
, sh
, &s
, disks
);
2910 if (s
.syncing
&& s
.locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
2911 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
2912 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2915 /* If the failed drive is just a ReadError, then we might need to progress
2916 * the repair/check process
2918 if (s
.failed
== 1 && !conf
->mddev
->ro
&&
2919 test_bit(R5_ReadError
, &sh
->dev
[s
.failed_num
].flags
)
2920 && !test_bit(R5_LOCKED
, &sh
->dev
[s
.failed_num
].flags
)
2921 && test_bit(R5_UPTODATE
, &sh
->dev
[s
.failed_num
].flags
)
2923 dev
= &sh
->dev
[s
.failed_num
];
2924 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
2925 set_bit(R5_Wantwrite
, &dev
->flags
);
2926 set_bit(R5_ReWrite
, &dev
->flags
);
2927 set_bit(R5_LOCKED
, &dev
->flags
);
2930 /* let's read it back */
2931 set_bit(R5_Wantread
, &dev
->flags
);
2932 set_bit(R5_LOCKED
, &dev
->flags
);
2937 /* Finish reconstruct operations initiated by the expansion process */
2938 if (sh
->reconstruct_state
== reconstruct_state_result
) {
2939 sh
->reconstruct_state
= reconstruct_state_idle
;
2940 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
2941 for (i
= conf
->raid_disks
; i
--; ) {
2942 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
2943 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2948 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
2949 !sh
->reconstruct_state
) {
2950 /* Need to write out all blocks after computing parity */
2951 sh
->disks
= conf
->raid_disks
;
2952 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
2953 schedule_reconstruction5(sh
, &s
, 1, 1);
2954 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
2955 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
2956 atomic_dec(&conf
->reshape_stripes
);
2957 wake_up(&conf
->wait_for_overlap
);
2958 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
2961 if (s
.expanding
&& s
.locked
== 0 &&
2962 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
2963 handle_stripe_expansion(conf
, sh
, NULL
);
2966 spin_unlock(&sh
->lock
);
2968 /* wait for this device to become unblocked */
2969 if (unlikely(blocked_rdev
))
2970 md_wait_for_blocked_rdev(blocked_rdev
, conf
->mddev
);
2973 raid5_run_ops(sh
, s
.ops_request
);
2977 return_io(return_bi
);
2979 return blocked_rdev
== NULL
;
2982 static bool handle_stripe6(struct stripe_head
*sh
, struct page
*tmp_page
)
2984 raid5_conf_t
*conf
= sh
->raid_conf
;
2985 int disks
= sh
->disks
;
2986 struct bio
*return_bi
= NULL
;
2987 int i
, pd_idx
= sh
->pd_idx
, qd_idx
= sh
->qd_idx
;
2988 struct stripe_head_state s
;
2989 struct r6_state r6s
;
2990 struct r5dev
*dev
, *pdev
, *qdev
;
2991 mdk_rdev_t
*blocked_rdev
= NULL
;
2993 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
2994 "pd_idx=%d, qd_idx=%d\n",
2995 (unsigned long long)sh
->sector
, sh
->state
,
2996 atomic_read(&sh
->count
), pd_idx
, qd_idx
);
2997 memset(&s
, 0, sizeof(s
));
2999 spin_lock(&sh
->lock
);
3000 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3001 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3003 s
.syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
3004 s
.expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3005 s
.expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3006 /* Now to look around and see what can be done */
3009 for (i
=disks
; i
--; ) {
3012 clear_bit(R5_Insync
, &dev
->flags
);
3014 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3015 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
3016 /* maybe we can reply to a read */
3017 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
3018 struct bio
*rbi
, *rbi2
;
3019 pr_debug("Return read for disc %d\n", i
);
3020 spin_lock_irq(&conf
->device_lock
);
3023 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
3024 wake_up(&conf
->wait_for_overlap
);
3025 spin_unlock_irq(&conf
->device_lock
);
3026 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
3027 copy_data(0, rbi
, dev
->page
, dev
->sector
);
3028 rbi2
= r5_next_bio(rbi
, dev
->sector
);
3029 spin_lock_irq(&conf
->device_lock
);
3030 if (!raid5_dec_bi_phys_segments(rbi
)) {
3031 rbi
->bi_next
= return_bi
;
3034 spin_unlock_irq(&conf
->device_lock
);
3039 /* now count some things */
3040 if (test_bit(R5_LOCKED
, &dev
->flags
)) s
.locked
++;
3041 if (test_bit(R5_UPTODATE
, &dev
->flags
)) s
.uptodate
++;
3048 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3053 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3054 if (blocked_rdev
== NULL
&&
3055 rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
3056 blocked_rdev
= rdev
;
3057 atomic_inc(&rdev
->nr_pending
);
3059 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
3060 /* The ReadError flag will just be confusing now */
3061 clear_bit(R5_ReadError
, &dev
->flags
);
3062 clear_bit(R5_ReWrite
, &dev
->flags
);
3064 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
3065 || test_bit(R5_ReadError
, &dev
->flags
)) {
3067 r6s
.failed_num
[s
.failed
] = i
;
3070 set_bit(R5_Insync
, &dev
->flags
);
3074 if (unlikely(blocked_rdev
)) {
3075 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3076 s
.to_write
|| s
.written
) {
3077 set_bit(STRIPE_HANDLE
, &sh
->state
);
3080 /* There is nothing for the blocked_rdev to block */
3081 rdev_dec_pending(blocked_rdev
, conf
->mddev
);
3082 blocked_rdev
= NULL
;
3085 pr_debug("locked=%d uptodate=%d to_read=%d"
3086 " to_write=%d failed=%d failed_num=%d,%d\n",
3087 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3088 r6s
.failed_num
[0], r6s
.failed_num
[1]);
3089 /* check if the array has lost >2 devices and, if so, some requests
3090 * might need to be failed
3092 if (s
.failed
> 2 && s
.to_read
+s
.to_write
+s
.written
)
3093 handle_failed_stripe(conf
, sh
, &s
, disks
, &return_bi
);
3094 if (s
.failed
> 2 && s
.syncing
) {
3095 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
3096 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3101 * might be able to return some write requests if the parity blocks
3102 * are safe, or on a failed drive
3104 pdev
= &sh
->dev
[pd_idx
];
3105 r6s
.p_failed
= (s
.failed
>= 1 && r6s
.failed_num
[0] == pd_idx
)
3106 || (s
.failed
>= 2 && r6s
.failed_num
[1] == pd_idx
);
3107 qdev
= &sh
->dev
[qd_idx
];
3108 r6s
.q_failed
= (s
.failed
>= 1 && r6s
.failed_num
[0] == qd_idx
)
3109 || (s
.failed
>= 2 && r6s
.failed_num
[1] == qd_idx
);
3112 ( r6s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3113 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3114 && test_bit(R5_UPTODATE
, &pdev
->flags
)))) &&
3115 ( r6s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3116 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3117 && test_bit(R5_UPTODATE
, &qdev
->flags
)))))
3118 handle_stripe_clean_event(conf
, sh
, disks
, &return_bi
);
3120 /* Now we might consider reading some blocks, either to check/generate
3121 * parity, or to satisfy requests
3122 * or to load a block that is being partially written.
3124 if (s
.to_read
|| s
.non_overwrite
|| (s
.to_write
&& s
.failed
) ||
3125 (s
.syncing
&& (s
.uptodate
< disks
)) || s
.expanding
)
3126 handle_stripe_fill6(sh
, &s
, &r6s
, disks
);
3128 /* now to consider writing and what else, if anything should be read */
3130 handle_stripe_dirtying6(conf
, sh
, &s
, &r6s
, disks
);
3132 /* maybe we need to check and possibly fix the parity for this stripe
3133 * Any reads will already have been scheduled, so we just see if enough
3136 if (s
.syncing
&& s
.locked
== 0 && !test_bit(STRIPE_INSYNC
, &sh
->state
))
3137 handle_parity_checks6(conf
, sh
, &s
, &r6s
, tmp_page
, disks
);
3139 if (s
.syncing
&& s
.locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3140 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
3141 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3144 /* If the failed drives are just a ReadError, then we might need
3145 * to progress the repair/check process
3147 if (s
.failed
<= 2 && !conf
->mddev
->ro
)
3148 for (i
= 0; i
< s
.failed
; i
++) {
3149 dev
= &sh
->dev
[r6s
.failed_num
[i
]];
3150 if (test_bit(R5_ReadError
, &dev
->flags
)
3151 && !test_bit(R5_LOCKED
, &dev
->flags
)
3152 && test_bit(R5_UPTODATE
, &dev
->flags
)
3154 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3155 set_bit(R5_Wantwrite
, &dev
->flags
);
3156 set_bit(R5_ReWrite
, &dev
->flags
);
3157 set_bit(R5_LOCKED
, &dev
->flags
);
3159 /* let's read it back */
3160 set_bit(R5_Wantread
, &dev
->flags
);
3161 set_bit(R5_LOCKED
, &dev
->flags
);
3166 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
3167 /* Need to write out all blocks after computing P&Q */
3168 sh
->disks
= conf
->raid_disks
;
3169 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3170 compute_parity6(sh
, RECONSTRUCT_WRITE
);
3171 for (i
= conf
->raid_disks
; i
-- ; ) {
3172 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3174 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3176 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3177 } else if (s
.expanded
) {
3178 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3179 atomic_dec(&conf
->reshape_stripes
);
3180 wake_up(&conf
->wait_for_overlap
);
3181 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3184 if (s
.expanding
&& s
.locked
== 0 &&
3185 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3186 handle_stripe_expansion(conf
, sh
, &r6s
);
3189 spin_unlock(&sh
->lock
);
3191 /* wait for this device to become unblocked */
3192 if (unlikely(blocked_rdev
))
3193 md_wait_for_blocked_rdev(blocked_rdev
, conf
->mddev
);
3197 return_io(return_bi
);
3199 return blocked_rdev
== NULL
;
3202 /* returns true if the stripe was handled */
3203 static bool handle_stripe(struct stripe_head
*sh
, struct page
*tmp_page
)
3205 if (sh
->raid_conf
->level
== 6)
3206 return handle_stripe6(sh
, tmp_page
);
3208 return handle_stripe5(sh
);
3213 static void raid5_activate_delayed(raid5_conf_t
*conf
)
3215 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
3216 while (!list_empty(&conf
->delayed_list
)) {
3217 struct list_head
*l
= conf
->delayed_list
.next
;
3218 struct stripe_head
*sh
;
3219 sh
= list_entry(l
, struct stripe_head
, lru
);
3221 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3222 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3223 atomic_inc(&conf
->preread_active_stripes
);
3224 list_add_tail(&sh
->lru
, &conf
->hold_list
);
3227 blk_plug_device(conf
->mddev
->queue
);
3230 static void activate_bit_delay(raid5_conf_t
*conf
)
3232 /* device_lock is held */
3233 struct list_head head
;
3234 list_add(&head
, &conf
->bitmap_list
);
3235 list_del_init(&conf
->bitmap_list
);
3236 while (!list_empty(&head
)) {
3237 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
3238 list_del_init(&sh
->lru
);
3239 atomic_inc(&sh
->count
);
3240 __release_stripe(conf
, sh
);
3244 static void unplug_slaves(mddev_t
*mddev
)
3246 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3250 for (i
=0; i
<mddev
->raid_disks
; i
++) {
3251 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3252 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
3253 struct request_queue
*r_queue
= bdev_get_queue(rdev
->bdev
);
3255 atomic_inc(&rdev
->nr_pending
);
3258 blk_unplug(r_queue
);
3260 rdev_dec_pending(rdev
, mddev
);
3267 static void raid5_unplug_device(struct request_queue
*q
)
3269 mddev_t
*mddev
= q
->queuedata
;
3270 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3271 unsigned long flags
;
3273 spin_lock_irqsave(&conf
->device_lock
, flags
);
3275 if (blk_remove_plug(q
)) {
3277 raid5_activate_delayed(conf
);
3279 md_wakeup_thread(mddev
->thread
);
3281 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3283 unplug_slaves(mddev
);
3286 static int raid5_congested(void *data
, int bits
)
3288 mddev_t
*mddev
= data
;
3289 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3291 /* No difference between reads and writes. Just check
3292 * how busy the stripe_cache is
3294 if (conf
->inactive_blocked
)
3298 if (list_empty_careful(&conf
->inactive_list
))
3304 /* We want read requests to align with chunks where possible,
3305 * but write requests don't need to.
3307 static int raid5_mergeable_bvec(struct request_queue
*q
,
3308 struct bvec_merge_data
*bvm
,
3309 struct bio_vec
*biovec
)
3311 mddev_t
*mddev
= q
->queuedata
;
3312 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
3314 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
3315 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
3317 if ((bvm
->bi_rw
& 1) == WRITE
)
3318 return biovec
->bv_len
; /* always allow writes to be mergeable */
3320 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
3321 if (max
< 0) max
= 0;
3322 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
3323 return biovec
->bv_len
;
3329 static int in_chunk_boundary(mddev_t
*mddev
, struct bio
*bio
)
3331 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
3332 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
3333 unsigned int bio_sectors
= bio
->bi_size
>> 9;
3335 return chunk_sectors
>=
3336 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
3340 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3341 * later sampled by raid5d.
3343 static void add_bio_to_retry(struct bio
*bi
,raid5_conf_t
*conf
)
3345 unsigned long flags
;
3347 spin_lock_irqsave(&conf
->device_lock
, flags
);
3349 bi
->bi_next
= conf
->retry_read_aligned_list
;
3350 conf
->retry_read_aligned_list
= bi
;
3352 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3353 md_wakeup_thread(conf
->mddev
->thread
);
3357 static struct bio
*remove_bio_from_retry(raid5_conf_t
*conf
)
3361 bi
= conf
->retry_read_aligned
;
3363 conf
->retry_read_aligned
= NULL
;
3366 bi
= conf
->retry_read_aligned_list
;
3368 conf
->retry_read_aligned_list
= bi
->bi_next
;
3371 * this sets the active strip count to 1 and the processed
3372 * strip count to zero (upper 8 bits)
3374 bi
->bi_phys_segments
= 1; /* biased count of active stripes */
3382 * The "raid5_align_endio" should check if the read succeeded and if it
3383 * did, call bio_endio on the original bio (having bio_put the new bio
3385 * If the read failed..
3387 static void raid5_align_endio(struct bio
*bi
, int error
)
3389 struct bio
* raid_bi
= bi
->bi_private
;
3392 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
3397 mddev
= raid_bi
->bi_bdev
->bd_disk
->queue
->queuedata
;
3398 conf
= mddev_to_conf(mddev
);
3399 rdev
= (void*)raid_bi
->bi_next
;
3400 raid_bi
->bi_next
= NULL
;
3402 rdev_dec_pending(rdev
, conf
->mddev
);
3404 if (!error
&& uptodate
) {
3405 bio_endio(raid_bi
, 0);
3406 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
3407 wake_up(&conf
->wait_for_stripe
);
3412 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3414 add_bio_to_retry(raid_bi
, conf
);
3417 static int bio_fits_rdev(struct bio
*bi
)
3419 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
3421 if ((bi
->bi_size
>>9) > q
->max_sectors
)
3423 blk_recount_segments(q
, bi
);
3424 if (bi
->bi_phys_segments
> q
->max_phys_segments
)
3427 if (q
->merge_bvec_fn
)
3428 /* it's too hard to apply the merge_bvec_fn at this stage,
3437 static int chunk_aligned_read(struct request_queue
*q
, struct bio
* raid_bio
)
3439 mddev_t
*mddev
= q
->queuedata
;
3440 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3441 unsigned int dd_idx
;
3442 struct bio
* align_bi
;
3445 if (!in_chunk_boundary(mddev
, raid_bio
)) {
3446 pr_debug("chunk_aligned_read : non aligned\n");
3450 * use bio_clone to make a copy of the bio
3452 align_bi
= bio_clone(raid_bio
, GFP_NOIO
);
3456 * set bi_end_io to a new function, and set bi_private to the
3459 align_bi
->bi_end_io
= raid5_align_endio
;
3460 align_bi
->bi_private
= raid_bio
;
3464 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
3469 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
3470 if (rdev
&& test_bit(In_sync
, &rdev
->flags
)) {
3471 atomic_inc(&rdev
->nr_pending
);
3473 raid_bio
->bi_next
= (void*)rdev
;
3474 align_bi
->bi_bdev
= rdev
->bdev
;
3475 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
3476 align_bi
->bi_sector
+= rdev
->data_offset
;
3478 if (!bio_fits_rdev(align_bi
)) {
3479 /* too big in some way */
3481 rdev_dec_pending(rdev
, mddev
);
3485 spin_lock_irq(&conf
->device_lock
);
3486 wait_event_lock_irq(conf
->wait_for_stripe
,
3488 conf
->device_lock
, /* nothing */);
3489 atomic_inc(&conf
->active_aligned_reads
);
3490 spin_unlock_irq(&conf
->device_lock
);
3492 generic_make_request(align_bi
);
3501 /* __get_priority_stripe - get the next stripe to process
3503 * Full stripe writes are allowed to pass preread active stripes up until
3504 * the bypass_threshold is exceeded. In general the bypass_count
3505 * increments when the handle_list is handled before the hold_list; however, it
3506 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3507 * stripe with in flight i/o. The bypass_count will be reset when the
3508 * head of the hold_list has changed, i.e. the head was promoted to the
3511 static struct stripe_head
*__get_priority_stripe(raid5_conf_t
*conf
)
3513 struct stripe_head
*sh
;
3515 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3517 list_empty(&conf
->handle_list
) ? "empty" : "busy",
3518 list_empty(&conf
->hold_list
) ? "empty" : "busy",
3519 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
3521 if (!list_empty(&conf
->handle_list
)) {
3522 sh
= list_entry(conf
->handle_list
.next
, typeof(*sh
), lru
);
3524 if (list_empty(&conf
->hold_list
))
3525 conf
->bypass_count
= 0;
3526 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
3527 if (conf
->hold_list
.next
== conf
->last_hold
)
3528 conf
->bypass_count
++;
3530 conf
->last_hold
= conf
->hold_list
.next
;
3531 conf
->bypass_count
-= conf
->bypass_threshold
;
3532 if (conf
->bypass_count
< 0)
3533 conf
->bypass_count
= 0;
3536 } else if (!list_empty(&conf
->hold_list
) &&
3537 ((conf
->bypass_threshold
&&
3538 conf
->bypass_count
> conf
->bypass_threshold
) ||
3539 atomic_read(&conf
->pending_full_writes
) == 0)) {
3540 sh
= list_entry(conf
->hold_list
.next
,
3542 conf
->bypass_count
-= conf
->bypass_threshold
;
3543 if (conf
->bypass_count
< 0)
3544 conf
->bypass_count
= 0;
3548 list_del_init(&sh
->lru
);
3549 atomic_inc(&sh
->count
);
3550 BUG_ON(atomic_read(&sh
->count
) != 1);
3554 static int make_request(struct request_queue
*q
, struct bio
* bi
)
3556 mddev_t
*mddev
= q
->queuedata
;
3557 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3559 sector_t new_sector
;
3560 sector_t logical_sector
, last_sector
;
3561 struct stripe_head
*sh
;
3562 const int rw
= bio_data_dir(bi
);
3565 if (unlikely(bio_barrier(bi
))) {
3566 bio_endio(bi
, -EOPNOTSUPP
);
3570 md_write_start(mddev
, bi
);
3572 cpu
= part_stat_lock();
3573 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
3574 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
3579 mddev
->reshape_position
== MaxSector
&&
3580 chunk_aligned_read(q
,bi
))
3583 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
3584 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
3586 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
3588 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
3590 int disks
, data_disks
;
3595 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
3596 if (likely(conf
->reshape_progress
== MaxSector
))
3597 disks
= conf
->raid_disks
;
3599 /* spinlock is needed as reshape_progress may be
3600 * 64bit on a 32bit platform, and so it might be
3601 * possible to see a half-updated value
3602 * Ofcourse reshape_progress could change after
3603 * the lock is dropped, so once we get a reference
3604 * to the stripe that we think it is, we will have
3607 spin_lock_irq(&conf
->device_lock
);
3608 disks
= conf
->raid_disks
;
3609 if (mddev
->delta_disks
< 0
3610 ? logical_sector
< conf
->reshape_progress
3611 : logical_sector
>= conf
->reshape_progress
) {
3612 disks
= conf
->previous_raid_disks
;
3615 if (mddev
->delta_disks
< 0
3616 ? logical_sector
< conf
->reshape_safe
3617 : logical_sector
>= conf
->reshape_safe
) {
3618 spin_unlock_irq(&conf
->device_lock
);
3623 spin_unlock_irq(&conf
->device_lock
);
3625 data_disks
= disks
- conf
->max_degraded
;
3627 new_sector
= raid5_compute_sector(conf
, logical_sector
,
3630 pr_debug("raid5: make_request, sector %llu logical %llu\n",
3631 (unsigned long long)new_sector
,
3632 (unsigned long long)logical_sector
);
3634 sh
= get_active_stripe(conf
, new_sector
, previous
,
3635 (bi
->bi_rw
&RWA_MASK
));
3637 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
3638 /* expansion might have moved on while waiting for a
3639 * stripe, so we must do the range check again.
3640 * Expansion could still move past after this
3641 * test, but as we are holding a reference to
3642 * 'sh', we know that if that happens,
3643 * STRIPE_EXPANDING will get set and the expansion
3644 * won't proceed until we finish with the stripe.
3647 spin_lock_irq(&conf
->device_lock
);
3648 if ((mddev
->delta_disks
< 0
3649 ? logical_sector
>= conf
->reshape_progress
3650 : logical_sector
< conf
->reshape_progress
)
3651 && disks
== conf
->previous_raid_disks
)
3652 /* mismatch, need to try again */
3654 spin_unlock_irq(&conf
->device_lock
);
3660 /* FIXME what if we get a false positive because these
3661 * are being updated.
3663 if (logical_sector
>= mddev
->suspend_lo
&&
3664 logical_sector
< mddev
->suspend_hi
) {
3670 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
3671 !add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
3672 /* Stripe is busy expanding or
3673 * add failed due to overlap. Flush everything
3676 raid5_unplug_device(mddev
->queue
);
3681 finish_wait(&conf
->wait_for_overlap
, &w
);
3682 set_bit(STRIPE_HANDLE
, &sh
->state
);
3683 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3686 /* cannot get stripe for read-ahead, just give-up */
3687 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
3688 finish_wait(&conf
->wait_for_overlap
, &w
);
3693 spin_lock_irq(&conf
->device_lock
);
3694 remaining
= raid5_dec_bi_phys_segments(bi
);
3695 spin_unlock_irq(&conf
->device_lock
);
3696 if (remaining
== 0) {
3699 md_write_end(mddev
);
3706 static sector_t
raid5_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
);
3708 static sector_t
reshape_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
)
3710 /* reshaping is quite different to recovery/resync so it is
3711 * handled quite separately ... here.
3713 * On each call to sync_request, we gather one chunk worth of
3714 * destination stripes and flag them as expanding.
3715 * Then we find all the source stripes and request reads.
3716 * As the reads complete, handle_stripe will copy the data
3717 * into the destination stripe and release that stripe.
3719 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3720 struct stripe_head
*sh
;
3721 sector_t first_sector
, last_sector
;
3722 int raid_disks
= conf
->previous_raid_disks
;
3723 int data_disks
= raid_disks
- conf
->max_degraded
;
3724 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
3727 sector_t writepos
, safepos
, gap
;
3729 if (sector_nr
== 0) {
3730 /* If restarting in the middle, skip the initial sectors */
3731 if (mddev
->delta_disks
< 0 &&
3732 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
3733 sector_nr
= raid5_size(mddev
, 0, 0)
3734 - conf
->reshape_progress
;
3735 } else if (mddev
->delta_disks
> 0 &&
3736 conf
->reshape_progress
> 0)
3737 sector_nr
= conf
->reshape_progress
;
3738 sector_div(sector_nr
, new_data_disks
);
3745 /* we update the metadata when there is more than 3Meg
3746 * in the block range (that is rather arbitrary, should
3747 * probably be time based) or when the data about to be
3748 * copied would over-write the source of the data at
3749 * the front of the range.
3750 * i.e. one new_stripe along from reshape_progress new_maps
3751 * to after where reshape_safe old_maps to
3753 writepos
= conf
->reshape_progress
;
3754 sector_div(writepos
, new_data_disks
);
3755 safepos
= conf
->reshape_safe
;
3756 sector_div(safepos
, data_disks
);
3757 if (mddev
->delta_disks
< 0) {
3758 writepos
-= conf
->chunk_size
/512;
3759 safepos
+= conf
->chunk_size
/512;
3760 gap
= conf
->reshape_safe
- conf
->reshape_progress
;
3762 writepos
+= conf
->chunk_size
/512;
3763 safepos
-= conf
->chunk_size
/512;
3764 gap
= conf
->reshape_progress
- conf
->reshape_safe
;
3767 if ((mddev
->delta_disks
< 0
3768 ? writepos
< safepos
3769 : writepos
> safepos
) ||
3770 gap
> (new_data_disks
)*3000*2 /*3Meg*/) {
3771 /* Cannot proceed until we've updated the superblock... */
3772 wait_event(conf
->wait_for_overlap
,
3773 atomic_read(&conf
->reshape_stripes
)==0);
3774 mddev
->reshape_position
= conf
->reshape_progress
;
3775 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3776 md_wakeup_thread(mddev
->thread
);
3777 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
3778 kthread_should_stop());
3779 spin_lock_irq(&conf
->device_lock
);
3780 conf
->reshape_safe
= mddev
->reshape_position
;
3781 spin_unlock_irq(&conf
->device_lock
);
3782 wake_up(&conf
->wait_for_overlap
);
3785 for (i
=0; i
< conf
->chunk_size
/512; i
+= STRIPE_SECTORS
) {
3788 sh
= get_active_stripe(conf
, sector_nr
+i
, 0, 0);
3789 set_bit(STRIPE_EXPANDING
, &sh
->state
);
3790 atomic_inc(&conf
->reshape_stripes
);
3791 /* If any of this stripe is beyond the end of the old
3792 * array, then we need to zero those blocks
3794 for (j
=sh
->disks
; j
--;) {
3796 if (j
== sh
->pd_idx
)
3798 if (conf
->level
== 6 &&
3801 s
= compute_blocknr(sh
, j
);
3802 if (s
< raid5_size(mddev
, 0, 0)) {
3806 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
3807 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
3808 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
3811 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3812 set_bit(STRIPE_HANDLE
, &sh
->state
);
3816 spin_lock_irq(&conf
->device_lock
);
3817 if (mddev
->delta_disks
< 0)
3818 conf
->reshape_progress
-= i
* new_data_disks
;
3820 conf
->reshape_progress
+= i
* new_data_disks
;
3821 spin_unlock_irq(&conf
->device_lock
);
3822 /* Ok, those stripe are ready. We can start scheduling
3823 * reads on the source stripes.
3824 * The source stripes are determined by mapping the first and last
3825 * block on the destination stripes.
3828 raid5_compute_sector(conf
, sector_nr
*(new_data_disks
),
3831 raid5_compute_sector(conf
, ((sector_nr
+conf
->chunk_size
/512)
3832 *(new_data_disks
) - 1),
3834 if (last_sector
>= mddev
->dev_sectors
)
3835 last_sector
= mddev
->dev_sectors
- 1;
3836 while (first_sector
<= last_sector
) {
3837 sh
= get_active_stripe(conf
, first_sector
, 1, 0);
3838 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3839 set_bit(STRIPE_HANDLE
, &sh
->state
);
3841 first_sector
+= STRIPE_SECTORS
;
3843 /* If this takes us to the resync_max point where we have to pause,
3844 * then we need to write out the superblock.
3846 sector_nr
+= conf
->chunk_size
>>9;
3847 if (sector_nr
>= mddev
->resync_max
) {
3848 /* Cannot proceed until we've updated the superblock... */
3849 wait_event(conf
->wait_for_overlap
,
3850 atomic_read(&conf
->reshape_stripes
) == 0);
3851 mddev
->reshape_position
= conf
->reshape_progress
;
3852 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3853 md_wakeup_thread(mddev
->thread
);
3854 wait_event(mddev
->sb_wait
,
3855 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
3856 || kthread_should_stop());
3857 spin_lock_irq(&conf
->device_lock
);
3858 conf
->reshape_safe
= mddev
->reshape_position
;
3859 spin_unlock_irq(&conf
->device_lock
);
3860 wake_up(&conf
->wait_for_overlap
);
3862 return conf
->chunk_size
>>9;
3865 /* FIXME go_faster isn't used */
3866 static inline sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
3868 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3869 struct stripe_head
*sh
;
3870 sector_t max_sector
= mddev
->dev_sectors
;
3872 int still_degraded
= 0;
3875 if (sector_nr
>= max_sector
) {
3876 /* just being told to finish up .. nothing much to do */
3877 unplug_slaves(mddev
);
3879 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
3884 if (mddev
->curr_resync
< max_sector
) /* aborted */
3885 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
3887 else /* completed sync */
3889 bitmap_close_sync(mddev
->bitmap
);
3894 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3895 return reshape_request(mddev
, sector_nr
, skipped
);
3897 /* No need to check resync_max as we never do more than one
3898 * stripe, and as resync_max will always be on a chunk boundary,
3899 * if the check in md_do_sync didn't fire, there is no chance
3900 * of overstepping resync_max here
3903 /* if there is too many failed drives and we are trying
3904 * to resync, then assert that we are finished, because there is
3905 * nothing we can do.
3907 if (mddev
->degraded
>= conf
->max_degraded
&&
3908 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3909 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
3913 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
3914 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
3915 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
3916 /* we can skip this block, and probably more */
3917 sync_blocks
/= STRIPE_SECTORS
;
3919 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
3923 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
3925 sh
= get_active_stripe(conf
, sector_nr
, 0, 1);
3927 sh
= get_active_stripe(conf
, sector_nr
, 0, 0);
3928 /* make sure we don't swamp the stripe cache if someone else
3929 * is trying to get access
3931 schedule_timeout_uninterruptible(1);
3933 /* Need to check if array will still be degraded after recovery/resync
3934 * We don't need to check the 'failed' flag as when that gets set,
3937 for (i
=0; i
<mddev
->raid_disks
; i
++)
3938 if (conf
->disks
[i
].rdev
== NULL
)
3941 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
3943 spin_lock(&sh
->lock
);
3944 set_bit(STRIPE_SYNCING
, &sh
->state
);
3945 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3946 spin_unlock(&sh
->lock
);
3948 /* wait for any blocked device to be handled */
3949 while(unlikely(!handle_stripe(sh
, NULL
)))
3953 return STRIPE_SECTORS
;
3956 static int retry_aligned_read(raid5_conf_t
*conf
, struct bio
*raid_bio
)
3958 /* We may not be able to submit a whole bio at once as there
3959 * may not be enough stripe_heads available.
3960 * We cannot pre-allocate enough stripe_heads as we may need
3961 * more than exist in the cache (if we allow ever large chunks).
3962 * So we do one stripe head at a time and record in
3963 * ->bi_hw_segments how many have been done.
3965 * We *know* that this entire raid_bio is in one chunk, so
3966 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3968 struct stripe_head
*sh
;
3970 sector_t sector
, logical_sector
, last_sector
;
3975 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
3976 sector
= raid5_compute_sector(conf
, logical_sector
,
3978 last_sector
= raid_bio
->bi_sector
+ (raid_bio
->bi_size
>>9);
3980 for (; logical_sector
< last_sector
;
3981 logical_sector
+= STRIPE_SECTORS
,
3982 sector
+= STRIPE_SECTORS
,
3985 if (scnt
< raid5_bi_hw_segments(raid_bio
))
3986 /* already done this stripe */
3989 sh
= get_active_stripe(conf
, sector
, 0, 1);
3992 /* failed to get a stripe - must wait */
3993 raid5_set_bi_hw_segments(raid_bio
, scnt
);
3994 conf
->retry_read_aligned
= raid_bio
;
3998 set_bit(R5_ReadError
, &sh
->dev
[dd_idx
].flags
);
3999 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
4001 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4002 conf
->retry_read_aligned
= raid_bio
;
4006 handle_stripe(sh
, NULL
);
4010 spin_lock_irq(&conf
->device_lock
);
4011 remaining
= raid5_dec_bi_phys_segments(raid_bio
);
4012 spin_unlock_irq(&conf
->device_lock
);
4014 bio_endio(raid_bio
, 0);
4015 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4016 wake_up(&conf
->wait_for_stripe
);
4023 * This is our raid5 kernel thread.
4025 * We scan the hash table for stripes which can be handled now.
4026 * During the scan, completed stripes are saved for us by the interrupt
4027 * handler, so that they will not have to wait for our next wakeup.
4029 static void raid5d(mddev_t
*mddev
)
4031 struct stripe_head
*sh
;
4032 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4035 pr_debug("+++ raid5d active\n");
4037 md_check_recovery(mddev
);
4040 spin_lock_irq(&conf
->device_lock
);
4044 if (conf
->seq_flush
!= conf
->seq_write
) {
4045 int seq
= conf
->seq_flush
;
4046 spin_unlock_irq(&conf
->device_lock
);
4047 bitmap_unplug(mddev
->bitmap
);
4048 spin_lock_irq(&conf
->device_lock
);
4049 conf
->seq_write
= seq
;
4050 activate_bit_delay(conf
);
4053 while ((bio
= remove_bio_from_retry(conf
))) {
4055 spin_unlock_irq(&conf
->device_lock
);
4056 ok
= retry_aligned_read(conf
, bio
);
4057 spin_lock_irq(&conf
->device_lock
);
4063 sh
= __get_priority_stripe(conf
);
4067 spin_unlock_irq(&conf
->device_lock
);
4070 handle_stripe(sh
, conf
->spare_page
);
4073 spin_lock_irq(&conf
->device_lock
);
4075 pr_debug("%d stripes handled\n", handled
);
4077 spin_unlock_irq(&conf
->device_lock
);
4079 async_tx_issue_pending_all();
4080 unplug_slaves(mddev
);
4082 pr_debug("--- raid5d inactive\n");
4086 raid5_show_stripe_cache_size(mddev_t
*mddev
, char *page
)
4088 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4090 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
4096 raid5_store_stripe_cache_size(mddev_t
*mddev
, const char *page
, size_t len
)
4098 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4102 if (len
>= PAGE_SIZE
)
4107 if (strict_strtoul(page
, 10, &new))
4109 if (new <= 16 || new > 32768)
4111 while (new < conf
->max_nr_stripes
) {
4112 if (drop_one_stripe(conf
))
4113 conf
->max_nr_stripes
--;
4117 err
= md_allow_write(mddev
);
4120 while (new > conf
->max_nr_stripes
) {
4121 if (grow_one_stripe(conf
))
4122 conf
->max_nr_stripes
++;
4128 static struct md_sysfs_entry
4129 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
4130 raid5_show_stripe_cache_size
,
4131 raid5_store_stripe_cache_size
);
4134 raid5_show_preread_threshold(mddev_t
*mddev
, char *page
)
4136 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4138 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
4144 raid5_store_preread_threshold(mddev_t
*mddev
, const char *page
, size_t len
)
4146 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4148 if (len
>= PAGE_SIZE
)
4153 if (strict_strtoul(page
, 10, &new))
4155 if (new > conf
->max_nr_stripes
)
4157 conf
->bypass_threshold
= new;
4161 static struct md_sysfs_entry
4162 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
4164 raid5_show_preread_threshold
,
4165 raid5_store_preread_threshold
);
4168 stripe_cache_active_show(mddev_t
*mddev
, char *page
)
4170 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4172 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
4177 static struct md_sysfs_entry
4178 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
4180 static struct attribute
*raid5_attrs
[] = {
4181 &raid5_stripecache_size
.attr
,
4182 &raid5_stripecache_active
.attr
,
4183 &raid5_preread_bypass_threshold
.attr
,
4186 static struct attribute_group raid5_attrs_group
= {
4188 .attrs
= raid5_attrs
,
4192 raid5_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
4194 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4197 sectors
= mddev
->dev_sectors
;
4199 /* size is defined by the smallest of previous and new size */
4200 if (conf
->raid_disks
< conf
->previous_raid_disks
)
4201 raid_disks
= conf
->raid_disks
;
4203 raid_disks
= conf
->previous_raid_disks
;
4206 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
4207 return sectors
* (raid_disks
- conf
->max_degraded
);
4210 static raid5_conf_t
*setup_conf(mddev_t
*mddev
)
4213 int raid_disk
, memory
;
4215 struct disk_info
*disk
;
4217 if (mddev
->new_level
!= 5
4218 && mddev
->new_level
!= 4
4219 && mddev
->new_level
!= 6) {
4220 printk(KERN_ERR
"raid5: %s: raid level not set to 4/5/6 (%d)\n",
4221 mdname(mddev
), mddev
->new_level
);
4222 return ERR_PTR(-EIO
);
4224 if ((mddev
->new_level
== 5
4225 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
4226 (mddev
->new_level
== 6
4227 && !algorithm_valid_raid6(mddev
->new_layout
))) {
4228 printk(KERN_ERR
"raid5: %s: layout %d not supported\n",
4229 mdname(mddev
), mddev
->new_layout
);
4230 return ERR_PTR(-EIO
);
4232 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
4233 printk(KERN_ERR
"raid6: not enough configured devices for %s (%d, minimum 4)\n",
4234 mdname(mddev
), mddev
->raid_disks
);
4235 return ERR_PTR(-EINVAL
);
4238 if (!mddev
->new_chunk
|| mddev
->new_chunk
% PAGE_SIZE
) {
4239 printk(KERN_ERR
"raid5: invalid chunk size %d for %s\n",
4240 mddev
->new_chunk
, mdname(mddev
));
4241 return ERR_PTR(-EINVAL
);
4244 conf
= kzalloc(sizeof(raid5_conf_t
), GFP_KERNEL
);
4248 conf
->raid_disks
= mddev
->raid_disks
;
4249 if (mddev
->reshape_position
== MaxSector
)
4250 conf
->previous_raid_disks
= mddev
->raid_disks
;
4252 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4254 conf
->disks
= kzalloc(conf
->raid_disks
* sizeof(struct disk_info
),
4259 conf
->mddev
= mddev
;
4261 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
4264 if (mddev
->new_level
== 6) {
4265 conf
->spare_page
= alloc_page(GFP_KERNEL
);
4266 if (!conf
->spare_page
)
4269 spin_lock_init(&conf
->device_lock
);
4270 init_waitqueue_head(&conf
->wait_for_stripe
);
4271 init_waitqueue_head(&conf
->wait_for_overlap
);
4272 INIT_LIST_HEAD(&conf
->handle_list
);
4273 INIT_LIST_HEAD(&conf
->hold_list
);
4274 INIT_LIST_HEAD(&conf
->delayed_list
);
4275 INIT_LIST_HEAD(&conf
->bitmap_list
);
4276 INIT_LIST_HEAD(&conf
->inactive_list
);
4277 atomic_set(&conf
->active_stripes
, 0);
4278 atomic_set(&conf
->preread_active_stripes
, 0);
4279 atomic_set(&conf
->active_aligned_reads
, 0);
4280 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
4282 pr_debug("raid5: run(%s) called.\n", mdname(mddev
));
4284 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
4285 raid_disk
= rdev
->raid_disk
;
4286 if (raid_disk
>= conf
->raid_disks
4289 disk
= conf
->disks
+ raid_disk
;
4293 if (test_bit(In_sync
, &rdev
->flags
)) {
4294 char b
[BDEVNAME_SIZE
];
4295 printk(KERN_INFO
"raid5: device %s operational as raid"
4296 " disk %d\n", bdevname(rdev
->bdev
,b
),
4299 /* Cannot rely on bitmap to complete recovery */
4303 conf
->chunk_size
= mddev
->new_chunk
;
4304 conf
->level
= mddev
->new_level
;
4305 if (conf
->level
== 6)
4306 conf
->max_degraded
= 2;
4308 conf
->max_degraded
= 1;
4309 conf
->algorithm
= mddev
->new_layout
;
4310 conf
->max_nr_stripes
= NR_STRIPES
;
4311 conf
->reshape_progress
= mddev
->reshape_position
;
4313 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
4314 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
4315 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
4317 "raid5: couldn't allocate %dkB for buffers\n", memory
);
4320 printk(KERN_INFO
"raid5: allocated %dkB for %s\n",
4321 memory
, mdname(mddev
));
4323 conf
->thread
= md_register_thread(raid5d
, mddev
, "%s_raid5");
4324 if (!conf
->thread
) {
4326 "raid5: couldn't allocate thread for %s\n",
4335 shrink_stripes(conf
);
4336 safe_put_page(conf
->spare_page
);
4338 kfree(conf
->stripe_hashtbl
);
4340 return ERR_PTR(-EIO
);
4342 return ERR_PTR(-ENOMEM
);
4345 static int run(mddev_t
*mddev
)
4348 int working_disks
= 0;
4351 if (mddev
->reshape_position
!= MaxSector
) {
4352 /* Check that we can continue the reshape.
4353 * Currently only disks can change, it must
4354 * increase, and we must be past the point where
4355 * a stripe over-writes itself
4357 sector_t here_new
, here_old
;
4359 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
4361 if (mddev
->new_level
!= mddev
->level
||
4362 mddev
->new_layout
!= mddev
->layout
||
4363 mddev
->new_chunk
!= mddev
->chunk_size
) {
4364 printk(KERN_ERR
"raid5: %s: unsupported reshape "
4365 "required - aborting.\n",
4369 if (mddev
->delta_disks
<= 0) {
4370 printk(KERN_ERR
"raid5: %s: unsupported reshape "
4371 "(reduce disks) required - aborting.\n",
4375 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4376 /* reshape_position must be on a new-stripe boundary, and one
4377 * further up in new geometry must map after here in old
4380 here_new
= mddev
->reshape_position
;
4381 if (sector_div(here_new
, (mddev
->chunk_size
>>9)*
4382 (mddev
->raid_disks
- max_degraded
))) {
4383 printk(KERN_ERR
"raid5: reshape_position not "
4384 "on a stripe boundary\n");
4387 /* here_new is the stripe we will write to */
4388 here_old
= mddev
->reshape_position
;
4389 sector_div(here_old
, (mddev
->chunk_size
>>9)*
4390 (old_disks
-max_degraded
));
4391 /* here_old is the first stripe that we might need to read
4393 if (here_new
>= here_old
) {
4394 /* Reading from the same stripe as writing to - bad */
4395 printk(KERN_ERR
"raid5: reshape_position too early for "
4396 "auto-recovery - aborting.\n");
4399 printk(KERN_INFO
"raid5: reshape will continue\n");
4400 /* OK, we should be able to continue; */
4402 BUG_ON(mddev
->level
!= mddev
->new_level
);
4403 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
4404 BUG_ON(mddev
->chunk_size
!= mddev
->new_chunk
);
4405 BUG_ON(mddev
->delta_disks
!= 0);
4408 if (mddev
->private == NULL
)
4409 conf
= setup_conf(mddev
);
4411 conf
= mddev
->private;
4414 return PTR_ERR(conf
);
4416 mddev
->thread
= conf
->thread
;
4417 conf
->thread
= NULL
;
4418 mddev
->private = conf
;
4421 * 0 for a fully functional array, 1 or 2 for a degraded array.
4423 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4424 if (rdev
->raid_disk
>= 0 &&
4425 test_bit(In_sync
, &rdev
->flags
))
4428 mddev
->degraded
= conf
->raid_disks
- working_disks
;
4430 if (mddev
->degraded
> conf
->max_degraded
) {
4431 printk(KERN_ERR
"raid5: not enough operational devices for %s"
4432 " (%d/%d failed)\n",
4433 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
4437 /* device size must be a multiple of chunk size */
4438 mddev
->dev_sectors
&= ~(mddev
->chunk_size
/ 512 - 1);
4439 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
4441 if (mddev
->degraded
> 0 &&
4442 mddev
->recovery_cp
!= MaxSector
) {
4443 if (mddev
->ok_start_degraded
)
4445 "raid5: starting dirty degraded array: %s"
4446 "- data corruption possible.\n",
4450 "raid5: cannot start dirty degraded array for %s\n",
4456 if (mddev
->degraded
== 0)
4457 printk("raid5: raid level %d set %s active with %d out of %d"
4458 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
4459 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
4462 printk(KERN_ALERT
"raid5: raid level %d set %s active with %d"
4463 " out of %d devices, algorithm %d\n", conf
->level
,
4464 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
4465 mddev
->raid_disks
, conf
->algorithm
);
4467 print_raid5_conf(conf
);
4469 if (conf
->reshape_progress
!= MaxSector
) {
4470 printk("...ok start reshape thread\n");
4471 conf
->reshape_safe
= conf
->reshape_progress
;
4472 atomic_set(&conf
->reshape_stripes
, 0);
4473 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4474 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4475 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4476 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4477 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
4481 /* read-ahead size must cover two whole stripes, which is
4482 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4485 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
4486 int stripe
= data_disks
*
4487 (mddev
->chunk_size
/ PAGE_SIZE
);
4488 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
4489 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
4492 /* Ok, everything is just fine now */
4493 if (sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
4495 "raid5: failed to create sysfs attributes for %s\n",
4498 mddev
->queue
->queue_lock
= &conf
->device_lock
;
4500 mddev
->queue
->unplug_fn
= raid5_unplug_device
;
4501 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
4502 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
4504 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
4506 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
4510 md_unregister_thread(mddev
->thread
);
4511 mddev
->thread
= NULL
;
4513 shrink_stripes(conf
);
4514 print_raid5_conf(conf
);
4515 safe_put_page(conf
->spare_page
);
4517 kfree(conf
->stripe_hashtbl
);
4520 mddev
->private = NULL
;
4521 printk(KERN_ALERT
"raid5: failed to run raid set %s\n", mdname(mddev
));
4527 static int stop(mddev_t
*mddev
)
4529 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
4531 md_unregister_thread(mddev
->thread
);
4532 mddev
->thread
= NULL
;
4533 shrink_stripes(conf
);
4534 kfree(conf
->stripe_hashtbl
);
4535 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
4536 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
4537 sysfs_remove_group(&mddev
->kobj
, &raid5_attrs_group
);
4540 mddev
->private = NULL
;
4545 static void print_sh(struct seq_file
*seq
, struct stripe_head
*sh
)
4549 seq_printf(seq
, "sh %llu, pd_idx %d, state %ld.\n",
4550 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
4551 seq_printf(seq
, "sh %llu, count %d.\n",
4552 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
4553 seq_printf(seq
, "sh %llu, ", (unsigned long long)sh
->sector
);
4554 for (i
= 0; i
< sh
->disks
; i
++) {
4555 seq_printf(seq
, "(cache%d: %p %ld) ",
4556 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
4558 seq_printf(seq
, "\n");
4561 static void printall(struct seq_file
*seq
, raid5_conf_t
*conf
)
4563 struct stripe_head
*sh
;
4564 struct hlist_node
*hn
;
4567 spin_lock_irq(&conf
->device_lock
);
4568 for (i
= 0; i
< NR_HASH
; i
++) {
4569 hlist_for_each_entry(sh
, hn
, &conf
->stripe_hashtbl
[i
], hash
) {
4570 if (sh
->raid_conf
!= conf
)
4575 spin_unlock_irq(&conf
->device_lock
);
4579 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
4581 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
4584 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
4585 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
4586 for (i
= 0; i
< conf
->raid_disks
; i
++)
4587 seq_printf (seq
, "%s",
4588 conf
->disks
[i
].rdev
&&
4589 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
4590 seq_printf (seq
, "]");
4592 seq_printf (seq
, "\n");
4593 printall(seq
, conf
);
4597 static void print_raid5_conf (raid5_conf_t
*conf
)
4600 struct disk_info
*tmp
;
4602 printk("RAID5 conf printout:\n");
4604 printk("(conf==NULL)\n");
4607 printk(" --- rd:%d wd:%d\n", conf
->raid_disks
,
4608 conf
->raid_disks
- conf
->mddev
->degraded
);
4610 for (i
= 0; i
< conf
->raid_disks
; i
++) {
4611 char b
[BDEVNAME_SIZE
];
4612 tmp
= conf
->disks
+ i
;
4614 printk(" disk %d, o:%d, dev:%s\n",
4615 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
4616 bdevname(tmp
->rdev
->bdev
,b
));
4620 static int raid5_spare_active(mddev_t
*mddev
)
4623 raid5_conf_t
*conf
= mddev
->private;
4624 struct disk_info
*tmp
;
4626 for (i
= 0; i
< conf
->raid_disks
; i
++) {
4627 tmp
= conf
->disks
+ i
;
4629 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
4630 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
4631 unsigned long flags
;
4632 spin_lock_irqsave(&conf
->device_lock
, flags
);
4634 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4637 print_raid5_conf(conf
);
4641 static int raid5_remove_disk(mddev_t
*mddev
, int number
)
4643 raid5_conf_t
*conf
= mddev
->private;
4646 struct disk_info
*p
= conf
->disks
+ number
;
4648 print_raid5_conf(conf
);
4651 if (test_bit(In_sync
, &rdev
->flags
) ||
4652 atomic_read(&rdev
->nr_pending
)) {
4656 /* Only remove non-faulty devices if recovery
4659 if (!test_bit(Faulty
, &rdev
->flags
) &&
4660 mddev
->degraded
<= conf
->max_degraded
) {
4666 if (atomic_read(&rdev
->nr_pending
)) {
4667 /* lost the race, try later */
4674 print_raid5_conf(conf
);
4678 static int raid5_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
4680 raid5_conf_t
*conf
= mddev
->private;
4683 struct disk_info
*p
;
4685 int last
= conf
->raid_disks
- 1;
4687 if (mddev
->degraded
> conf
->max_degraded
)
4688 /* no point adding a device */
4691 if (rdev
->raid_disk
>= 0)
4692 first
= last
= rdev
->raid_disk
;
4695 * find the disk ... but prefer rdev->saved_raid_disk
4698 if (rdev
->saved_raid_disk
>= 0 &&
4699 rdev
->saved_raid_disk
>= first
&&
4700 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
4701 disk
= rdev
->saved_raid_disk
;
4704 for ( ; disk
<= last
; disk
++)
4705 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
4706 clear_bit(In_sync
, &rdev
->flags
);
4707 rdev
->raid_disk
= disk
;
4709 if (rdev
->saved_raid_disk
!= disk
)
4711 rcu_assign_pointer(p
->rdev
, rdev
);
4714 print_raid5_conf(conf
);
4718 static int raid5_resize(mddev_t
*mddev
, sector_t sectors
)
4720 /* no resync is happening, and there is enough space
4721 * on all devices, so we can resize.
4722 * We need to make sure resync covers any new space.
4723 * If the array is shrinking we should possibly wait until
4724 * any io in the removed space completes, but it hardly seems
4727 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
4728 md_set_array_sectors(mddev
, raid5_size(mddev
, sectors
,
4729 mddev
->raid_disks
));
4730 if (mddev
->array_sectors
>
4731 raid5_size(mddev
, sectors
, mddev
->raid_disks
))
4733 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4735 if (sectors
> mddev
->dev_sectors
&& mddev
->recovery_cp
== MaxSector
) {
4736 mddev
->recovery_cp
= mddev
->dev_sectors
;
4737 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4739 mddev
->dev_sectors
= sectors
;
4740 mddev
->resync_max_sectors
= sectors
;
4744 #ifdef CONFIG_MD_RAID5_RESHAPE
4745 static int raid5_check_reshape(mddev_t
*mddev
)
4747 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4750 if (mddev
->delta_disks
< 0 ||
4751 mddev
->new_level
!= mddev
->level
)
4752 return -EINVAL
; /* Cannot shrink array or change level yet */
4753 if (mddev
->delta_disks
== 0)
4754 return 0; /* nothing to do */
4756 /* Cannot grow a bitmap yet */
4759 /* Can only proceed if there are plenty of stripe_heads.
4760 * We need a minimum of one full stripe,, and for sensible progress
4761 * it is best to have about 4 times that.
4762 * If we require 4 times, then the default 256 4K stripe_heads will
4763 * allow for chunk sizes up to 256K, which is probably OK.
4764 * If the chunk size is greater, user-space should request more
4765 * stripe_heads first.
4767 if ((mddev
->chunk_size
/ STRIPE_SIZE
) * 4 > conf
->max_nr_stripes
||
4768 (mddev
->new_chunk
/ STRIPE_SIZE
) * 4 > conf
->max_nr_stripes
) {
4769 printk(KERN_WARNING
"raid5: reshape: not enough stripes. Needed %lu\n",
4770 (mddev
->chunk_size
/ STRIPE_SIZE
)*4);
4774 err
= resize_stripes(conf
, conf
->raid_disks
+ mddev
->delta_disks
);
4778 if (mddev
->degraded
> conf
->max_degraded
)
4780 /* looks like we might be able to manage this */
4784 static int raid5_start_reshape(mddev_t
*mddev
)
4786 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4789 int added_devices
= 0;
4790 unsigned long flags
;
4792 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4795 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4796 if (rdev
->raid_disk
< 0 &&
4797 !test_bit(Faulty
, &rdev
->flags
))
4800 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
4801 /* Not enough devices even to make a degraded array
4806 atomic_set(&conf
->reshape_stripes
, 0);
4807 spin_lock_irq(&conf
->device_lock
);
4808 conf
->previous_raid_disks
= conf
->raid_disks
;
4809 conf
->raid_disks
+= mddev
->delta_disks
;
4810 if (mddev
->delta_disks
< 0)
4811 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
4813 conf
->reshape_progress
= 0;
4814 conf
->reshape_safe
= conf
->reshape_progress
;
4815 spin_unlock_irq(&conf
->device_lock
);
4817 /* Add some new drives, as many as will fit.
4818 * We know there are enough to make the newly sized array work.
4820 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
4821 if (rdev
->raid_disk
< 0 &&
4822 !test_bit(Faulty
, &rdev
->flags
)) {
4823 if (raid5_add_disk(mddev
, rdev
) == 0) {
4825 set_bit(In_sync
, &rdev
->flags
);
4827 rdev
->recovery_offset
= 0;
4828 sprintf(nm
, "rd%d", rdev
->raid_disk
);
4829 if (sysfs_create_link(&mddev
->kobj
,
4832 "raid5: failed to create "
4833 " link %s for %s\n",
4839 spin_lock_irqsave(&conf
->device_lock
, flags
);
4840 mddev
->degraded
= (conf
->raid_disks
- conf
->previous_raid_disks
) - added_devices
;
4841 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4842 mddev
->raid_disks
= conf
->raid_disks
;
4843 mddev
->reshape_position
= 0;
4844 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4846 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4847 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4848 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4849 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4850 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
4852 if (!mddev
->sync_thread
) {
4853 mddev
->recovery
= 0;
4854 spin_lock_irq(&conf
->device_lock
);
4855 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
4856 conf
->reshape_progress
= MaxSector
;
4857 spin_unlock_irq(&conf
->device_lock
);
4860 md_wakeup_thread(mddev
->sync_thread
);
4861 md_new_event(mddev
);
4866 static void end_reshape(raid5_conf_t
*conf
)
4869 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
4871 spin_lock_irq(&conf
->device_lock
);
4872 conf
->previous_raid_disks
= conf
->raid_disks
;
4873 conf
->reshape_progress
= MaxSector
;
4874 spin_unlock_irq(&conf
->device_lock
);
4876 /* read-ahead size must cover two whole stripes, which is
4877 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4880 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4881 int stripe
= data_disks
* (conf
->chunk_size
4883 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
4884 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
4889 static void raid5_finish_reshape(mddev_t
*mddev
)
4891 struct block_device
*bdev
;
4893 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4895 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
4896 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4898 mddev
->reshape_position
= MaxSector
;
4900 bdev
= bdget_disk(mddev
->gendisk
, 0);
4902 mutex_lock(&bdev
->bd_inode
->i_mutex
);
4903 i_size_write(bdev
->bd_inode
,
4904 (loff_t
)mddev
->array_sectors
<< 9);
4905 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
4911 static void raid5_quiesce(mddev_t
*mddev
, int state
)
4913 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
4916 case 2: /* resume for a suspend */
4917 wake_up(&conf
->wait_for_overlap
);
4920 case 1: /* stop all writes */
4921 spin_lock_irq(&conf
->device_lock
);
4923 wait_event_lock_irq(conf
->wait_for_stripe
,
4924 atomic_read(&conf
->active_stripes
) == 0 &&
4925 atomic_read(&conf
->active_aligned_reads
) == 0,
4926 conf
->device_lock
, /* nothing */);
4927 spin_unlock_irq(&conf
->device_lock
);
4930 case 0: /* re-enable writes */
4931 spin_lock_irq(&conf
->device_lock
);
4933 wake_up(&conf
->wait_for_stripe
);
4934 wake_up(&conf
->wait_for_overlap
);
4935 spin_unlock_irq(&conf
->device_lock
);
4941 static void *raid5_takeover_raid1(mddev_t
*mddev
)
4945 if (mddev
->raid_disks
!= 2 ||
4946 mddev
->degraded
> 1)
4947 return ERR_PTR(-EINVAL
);
4949 /* Should check if there are write-behind devices? */
4951 chunksect
= 64*2; /* 64K by default */
4953 /* The array must be an exact multiple of chunksize */
4954 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
4957 if ((chunksect
<<9) < STRIPE_SIZE
)
4958 /* array size does not allow a suitable chunk size */
4959 return ERR_PTR(-EINVAL
);
4961 mddev
->new_level
= 5;
4962 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
4963 mddev
->new_chunk
= chunksect
<< 9;
4965 return setup_conf(mddev
);
4968 static void *raid5_takeover_raid6(mddev_t
*mddev
)
4972 switch (mddev
->layout
) {
4973 case ALGORITHM_LEFT_ASYMMETRIC_6
:
4974 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
4976 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
4977 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
4979 case ALGORITHM_LEFT_SYMMETRIC_6
:
4980 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
4982 case ALGORITHM_RIGHT_SYMMETRIC_6
:
4983 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
4985 case ALGORITHM_PARITY_0_6
:
4986 new_layout
= ALGORITHM_PARITY_0
;
4988 case ALGORITHM_PARITY_N
:
4989 new_layout
= ALGORITHM_PARITY_N
;
4992 return ERR_PTR(-EINVAL
);
4994 mddev
->new_level
= 5;
4995 mddev
->new_layout
= new_layout
;
4996 mddev
->delta_disks
= -1;
4997 mddev
->raid_disks
-= 1;
4998 return setup_conf(mddev
);
5002 static int raid5_reconfig(mddev_t
*mddev
, int new_layout
, int new_chunk
)
5004 /* Currently the layout and chunk size can only be changed
5005 * for a 2-drive raid array, as in that case no data shuffling
5007 * Later we might validate these and set new_* so a reshape
5008 * can complete the change.
5010 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
5012 if (new_layout
>= 0 && !algorithm_valid_raid5(new_layout
))
5014 if (new_chunk
> 0) {
5015 if (new_chunk
& (new_chunk
-1))
5016 /* not a power of 2 */
5018 if (new_chunk
< PAGE_SIZE
)
5020 if (mddev
->array_sectors
& ((new_chunk
>>9)-1))
5021 /* not factor of array size */
5025 /* They look valid */
5027 if (mddev
->raid_disks
!= 2)
5030 if (new_layout
>= 0) {
5031 conf
->algorithm
= new_layout
;
5032 mddev
->layout
= mddev
->new_layout
= new_layout
;
5034 if (new_chunk
> 0) {
5035 conf
->chunk_size
= new_chunk
;
5036 mddev
->chunk_size
= mddev
->new_chunk
= new_chunk
;
5038 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5039 md_wakeup_thread(mddev
->thread
);
5043 static void *raid5_takeover(mddev_t
*mddev
)
5045 /* raid5 can take over:
5046 * raid0 - if all devices are the same - make it a raid4 layout
5047 * raid1 - if there are two drives. We need to know the chunk size
5048 * raid4 - trivial - just use a raid4 layout.
5049 * raid6 - Providing it is a *_6 layout
5051 * For now, just do raid1
5054 if (mddev
->level
== 1)
5055 return raid5_takeover_raid1(mddev
);
5056 if (mddev
->level
== 4) {
5057 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5058 mddev
->new_level
= 5;
5059 return setup_conf(mddev
);
5061 if (mddev
->level
== 6)
5062 return raid5_takeover_raid6(mddev
);
5064 return ERR_PTR(-EINVAL
);
5068 static struct mdk_personality raid5_personality
;
5070 static void *raid6_takeover(mddev_t
*mddev
)
5072 /* Currently can only take over a raid5. We map the
5073 * personality to an equivalent raid6 personality
5074 * with the Q block at the end.
5078 if (mddev
->pers
!= &raid5_personality
)
5079 return ERR_PTR(-EINVAL
);
5080 if (mddev
->degraded
> 1)
5081 return ERR_PTR(-EINVAL
);
5082 if (mddev
->raid_disks
> 253)
5083 return ERR_PTR(-EINVAL
);
5084 if (mddev
->raid_disks
< 3)
5085 return ERR_PTR(-EINVAL
);
5087 switch (mddev
->layout
) {
5088 case ALGORITHM_LEFT_ASYMMETRIC
:
5089 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
5091 case ALGORITHM_RIGHT_ASYMMETRIC
:
5092 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
5094 case ALGORITHM_LEFT_SYMMETRIC
:
5095 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
5097 case ALGORITHM_RIGHT_SYMMETRIC
:
5098 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
5100 case ALGORITHM_PARITY_0
:
5101 new_layout
= ALGORITHM_PARITY_0_6
;
5103 case ALGORITHM_PARITY_N
:
5104 new_layout
= ALGORITHM_PARITY_N
;
5107 return ERR_PTR(-EINVAL
);
5109 mddev
->new_level
= 6;
5110 mddev
->new_layout
= new_layout
;
5111 mddev
->delta_disks
= 1;
5112 mddev
->raid_disks
+= 1;
5113 return setup_conf(mddev
);
5117 static struct mdk_personality raid6_personality
=
5121 .owner
= THIS_MODULE
,
5122 .make_request
= make_request
,
5126 .error_handler
= error
,
5127 .hot_add_disk
= raid5_add_disk
,
5128 .hot_remove_disk
= raid5_remove_disk
,
5129 .spare_active
= raid5_spare_active
,
5130 .sync_request
= sync_request
,
5131 .resize
= raid5_resize
,
5133 #ifdef CONFIG_MD_RAID5_RESHAPE
5134 .check_reshape
= raid5_check_reshape
,
5135 .start_reshape
= raid5_start_reshape
,
5136 .finish_reshape
= raid5_finish_reshape
,
5138 .quiesce
= raid5_quiesce
,
5139 .takeover
= raid6_takeover
,
5141 static struct mdk_personality raid5_personality
=
5145 .owner
= THIS_MODULE
,
5146 .make_request
= make_request
,
5150 .error_handler
= error
,
5151 .hot_add_disk
= raid5_add_disk
,
5152 .hot_remove_disk
= raid5_remove_disk
,
5153 .spare_active
= raid5_spare_active
,
5154 .sync_request
= sync_request
,
5155 .resize
= raid5_resize
,
5157 #ifdef CONFIG_MD_RAID5_RESHAPE
5158 .check_reshape
= raid5_check_reshape
,
5159 .start_reshape
= raid5_start_reshape
,
5160 .finish_reshape
= raid5_finish_reshape
,
5162 .quiesce
= raid5_quiesce
,
5163 .takeover
= raid5_takeover
,
5164 .reconfig
= raid5_reconfig
,
5167 static struct mdk_personality raid4_personality
=
5171 .owner
= THIS_MODULE
,
5172 .make_request
= make_request
,
5176 .error_handler
= error
,
5177 .hot_add_disk
= raid5_add_disk
,
5178 .hot_remove_disk
= raid5_remove_disk
,
5179 .spare_active
= raid5_spare_active
,
5180 .sync_request
= sync_request
,
5181 .resize
= raid5_resize
,
5183 #ifdef CONFIG_MD_RAID5_RESHAPE
5184 .check_reshape
= raid5_check_reshape
,
5185 .start_reshape
= raid5_start_reshape
,
5186 .finish_reshape
= raid5_finish_reshape
,
5188 .quiesce
= raid5_quiesce
,
5191 static int __init
raid5_init(void)
5193 register_md_personality(&raid6_personality
);
5194 register_md_personality(&raid5_personality
);
5195 register_md_personality(&raid4_personality
);
5199 static void raid5_exit(void)
5201 unregister_md_personality(&raid6_personality
);
5202 unregister_md_personality(&raid5_personality
);
5203 unregister_md_personality(&raid4_personality
);
5206 module_init(raid5_init
);
5207 module_exit(raid5_exit
);
5208 MODULE_LICENSE("GPL");
5209 MODULE_ALIAS("md-personality-4"); /* RAID5 */
5210 MODULE_ALIAS("md-raid5");
5211 MODULE_ALIAS("md-raid4");
5212 MODULE_ALIAS("md-level-5");
5213 MODULE_ALIAS("md-level-4");
5214 MODULE_ALIAS("md-personality-8"); /* RAID6 */
5215 MODULE_ALIAS("md-raid6");
5216 MODULE_ALIAS("md-level-6");
5218 /* This used to be two separate modules, they were: */
5219 MODULE_ALIAS("raid5");
5220 MODULE_ALIAS("raid6");