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/module.h>
47 #include <linux/slab.h>
48 #include <linux/highmem.h>
49 #include <linux/bitops.h>
50 #include <linux/kthread.h>
51 #include <asm/atomic.h>
54 #include <linux/raid/bitmap.h>
55 #include <linux/async_tx.h>
61 #define NR_STRIPES 256
62 #define STRIPE_SIZE PAGE_SIZE
63 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
64 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
65 #define IO_THRESHOLD 1
66 #define BYPASS_THRESHOLD 1
67 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
68 #define HASH_MASK (NR_HASH - 1)
70 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
72 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
73 * order without overlap. There may be several bio's per stripe+device, and
74 * a bio could span several devices.
75 * When walking this list for a particular stripe+device, we must never proceed
76 * beyond a bio that extends past this device, as the next bio might no longer
78 * This macro is used to determine the 'next' bio in the list, given the sector
79 * of the current stripe+device
81 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
83 * The following can be used to debug the driver
85 #define RAID5_PARANOIA 1
86 #if RAID5_PARANOIA && defined(CONFIG_SMP)
87 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
89 # define CHECK_DEVLOCK()
97 #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
99 #if !RAID6_USE_EMPTY_ZERO_PAGE
100 /* In .bss so it's zeroed */
101 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
104 static inline int raid6_next_disk(int disk, int raid_disks)
107 return (disk < raid_disks) ? disk : 0;
110 static void return_io(struct bio *return_bi)
112 struct bio *bi = return_bi;
115 return_bi = bi->bi_next;
123 static void print_raid5_conf (raid5_conf_t *conf);
125 static int stripe_operations_active(struct stripe_head *sh)
127 return sh->check_state || sh->reconstruct_state ||
128 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
129 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
132 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
134 if (atomic_dec_and_test(&sh->count)) {
135 BUG_ON(!list_empty(&sh->lru));
136 BUG_ON(atomic_read(&conf->active_stripes)==0);
137 if (test_bit(STRIPE_HANDLE, &sh->state)) {
138 if (test_bit(STRIPE_DELAYED, &sh->state)) {
139 list_add_tail(&sh->lru, &conf->delayed_list);
140 blk_plug_device(conf->mddev->queue);
141 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
142 sh->bm_seq - conf->seq_write > 0) {
143 list_add_tail(&sh->lru, &conf->bitmap_list);
144 blk_plug_device(conf->mddev->queue);
146 clear_bit(STRIPE_BIT_DELAY, &sh->state);
147 list_add_tail(&sh->lru, &conf->handle_list);
149 md_wakeup_thread(conf->mddev->thread);
151 BUG_ON(stripe_operations_active(sh));
152 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
153 atomic_dec(&conf->preread_active_stripes);
154 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
155 md_wakeup_thread(conf->mddev->thread);
157 atomic_dec(&conf->active_stripes);
158 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
159 list_add_tail(&sh->lru, &conf->inactive_list);
160 wake_up(&conf->wait_for_stripe);
161 if (conf->retry_read_aligned)
162 md_wakeup_thread(conf->mddev->thread);
167 static void release_stripe(struct stripe_head *sh)
169 raid5_conf_t *conf = sh->raid_conf;
172 spin_lock_irqsave(&conf->device_lock, flags);
173 __release_stripe(conf, sh);
174 spin_unlock_irqrestore(&conf->device_lock, flags);
177 static inline void remove_hash(struct stripe_head *sh)
179 pr_debug("remove_hash(), stripe %llu\n",
180 (unsigned long long)sh->sector);
182 hlist_del_init(&sh->hash);
185 static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
187 struct hlist_head *hp = stripe_hash(conf, sh->sector);
189 pr_debug("insert_hash(), stripe %llu\n",
190 (unsigned long long)sh->sector);
193 hlist_add_head(&sh->hash, hp);
197 /* find an idle stripe, make sure it is unhashed, and return it. */
198 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
200 struct stripe_head *sh = NULL;
201 struct list_head *first;
204 if (list_empty(&conf->inactive_list))
206 first = conf->inactive_list.next;
207 sh = list_entry(first, struct stripe_head, lru);
208 list_del_init(first);
210 atomic_inc(&conf->active_stripes);
215 static void shrink_buffers(struct stripe_head *sh, int num)
220 for (i=0; i<num ; i++) {
224 sh->dev[i].page = NULL;
229 static int grow_buffers(struct stripe_head *sh, int num)
233 for (i=0; i<num; i++) {
236 if (!(page = alloc_page(GFP_KERNEL))) {
239 sh->dev[i].page = page;
244 static void raid5_build_block (struct stripe_head *sh, int i);
246 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
248 raid5_conf_t *conf = sh->raid_conf;
251 BUG_ON(atomic_read(&sh->count) != 0);
252 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
253 BUG_ON(stripe_operations_active(sh));
256 pr_debug("init_stripe called, stripe %llu\n",
257 (unsigned long long)sh->sector);
267 for (i = sh->disks; i--; ) {
268 struct r5dev *dev = &sh->dev[i];
270 if (dev->toread || dev->read || dev->towrite || dev->written ||
271 test_bit(R5_LOCKED, &dev->flags)) {
272 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
273 (unsigned long long)sh->sector, i, dev->toread,
274 dev->read, dev->towrite, dev->written,
275 test_bit(R5_LOCKED, &dev->flags));
279 raid5_build_block(sh, i);
281 insert_hash(conf, sh);
284 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
286 struct stripe_head *sh;
287 struct hlist_node *hn;
290 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
291 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
292 if (sh->sector == sector && sh->disks == disks)
294 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
298 static void unplug_slaves(mddev_t *mddev);
299 static void raid5_unplug_device(struct request_queue *q);
301 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
302 int pd_idx, int noblock)
304 struct stripe_head *sh;
306 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
308 spin_lock_irq(&conf->device_lock);
311 wait_event_lock_irq(conf->wait_for_stripe,
313 conf->device_lock, /* nothing */);
314 sh = __find_stripe(conf, sector, disks);
316 if (!conf->inactive_blocked)
317 sh = get_free_stripe(conf);
318 if (noblock && sh == NULL)
321 conf->inactive_blocked = 1;
322 wait_event_lock_irq(conf->wait_for_stripe,
323 !list_empty(&conf->inactive_list) &&
324 (atomic_read(&conf->active_stripes)
325 < (conf->max_nr_stripes *3/4)
326 || !conf->inactive_blocked),
328 raid5_unplug_device(conf->mddev->queue)
330 conf->inactive_blocked = 0;
332 init_stripe(sh, sector, pd_idx, disks);
334 if (atomic_read(&sh->count)) {
335 BUG_ON(!list_empty(&sh->lru));
337 if (!test_bit(STRIPE_HANDLE, &sh->state))
338 atomic_inc(&conf->active_stripes);
339 if (list_empty(&sh->lru) &&
340 !test_bit(STRIPE_EXPANDING, &sh->state))
342 list_del_init(&sh->lru);
345 } while (sh == NULL);
348 atomic_inc(&sh->count);
350 spin_unlock_irq(&conf->device_lock);
355 raid5_end_read_request(struct bio *bi, int error);
357 raid5_end_write_request(struct bio *bi, int error);
359 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
361 raid5_conf_t *conf = sh->raid_conf;
362 int i, disks = sh->disks;
366 for (i = disks; i--; ) {
370 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
372 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
377 bi = &sh->dev[i].req;
381 bi->bi_end_io = raid5_end_write_request;
383 bi->bi_end_io = raid5_end_read_request;
386 rdev = rcu_dereference(conf->disks[i].rdev);
387 if (rdev && test_bit(Faulty, &rdev->flags))
390 atomic_inc(&rdev->nr_pending);
394 if (s->syncing || s->expanding || s->expanded)
395 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
397 set_bit(STRIPE_IO_STARTED, &sh->state);
399 bi->bi_bdev = rdev->bdev;
400 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
401 __func__, (unsigned long long)sh->sector,
403 atomic_inc(&sh->count);
404 bi->bi_sector = sh->sector + rdev->data_offset;
405 bi->bi_flags = 1 << BIO_UPTODATE;
409 bi->bi_io_vec = &sh->dev[i].vec;
410 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
411 bi->bi_io_vec[0].bv_offset = 0;
412 bi->bi_size = STRIPE_SIZE;
415 test_bit(R5_ReWrite, &sh->dev[i].flags))
416 atomic_add(STRIPE_SECTORS,
417 &rdev->corrected_errors);
418 generic_make_request(bi);
421 set_bit(STRIPE_DEGRADED, &sh->state);
422 pr_debug("skip op %ld on disc %d for sector %llu\n",
423 bi->bi_rw, i, (unsigned long long)sh->sector);
424 clear_bit(R5_LOCKED, &sh->dev[i].flags);
425 set_bit(STRIPE_HANDLE, &sh->state);
430 static struct dma_async_tx_descriptor *
431 async_copy_data(int frombio, struct bio *bio, struct page *page,
432 sector_t sector, struct dma_async_tx_descriptor *tx)
435 struct page *bio_page;
439 if (bio->bi_sector >= sector)
440 page_offset = (signed)(bio->bi_sector - sector) * 512;
442 page_offset = (signed)(sector - bio->bi_sector) * -512;
443 bio_for_each_segment(bvl, bio, i) {
444 int len = bio_iovec_idx(bio, i)->bv_len;
448 if (page_offset < 0) {
449 b_offset = -page_offset;
450 page_offset += b_offset;
454 if (len > 0 && page_offset + len > STRIPE_SIZE)
455 clen = STRIPE_SIZE - page_offset;
460 b_offset += bio_iovec_idx(bio, i)->bv_offset;
461 bio_page = bio_iovec_idx(bio, i)->bv_page;
463 tx = async_memcpy(page, bio_page, page_offset,
468 tx = async_memcpy(bio_page, page, b_offset,
473 if (clen < len) /* hit end of page */
481 static void ops_complete_biofill(void *stripe_head_ref)
483 struct stripe_head *sh = stripe_head_ref;
484 struct bio *return_bi = NULL;
485 raid5_conf_t *conf = sh->raid_conf;
488 pr_debug("%s: stripe %llu\n", __func__,
489 (unsigned long long)sh->sector);
491 /* clear completed biofills */
492 spin_lock_irq(&conf->device_lock);
493 for (i = sh->disks; i--; ) {
494 struct r5dev *dev = &sh->dev[i];
496 /* acknowledge completion of a biofill operation */
497 /* and check if we need to reply to a read request,
498 * new R5_Wantfill requests are held off until
499 * !STRIPE_BIOFILL_RUN
501 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
502 struct bio *rbi, *rbi2;
507 while (rbi && rbi->bi_sector <
508 dev->sector + STRIPE_SECTORS) {
509 rbi2 = r5_next_bio(rbi, dev->sector);
510 if (--rbi->bi_phys_segments == 0) {
511 rbi->bi_next = return_bi;
518 spin_unlock_irq(&conf->device_lock);
519 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
521 return_io(return_bi);
523 set_bit(STRIPE_HANDLE, &sh->state);
527 static void ops_run_biofill(struct stripe_head *sh)
529 struct dma_async_tx_descriptor *tx = NULL;
530 raid5_conf_t *conf = sh->raid_conf;
533 pr_debug("%s: stripe %llu\n", __func__,
534 (unsigned long long)sh->sector);
536 for (i = sh->disks; i--; ) {
537 struct r5dev *dev = &sh->dev[i];
538 if (test_bit(R5_Wantfill, &dev->flags)) {
540 spin_lock_irq(&conf->device_lock);
541 dev->read = rbi = dev->toread;
543 spin_unlock_irq(&conf->device_lock);
544 while (rbi && rbi->bi_sector <
545 dev->sector + STRIPE_SECTORS) {
546 tx = async_copy_data(0, rbi, dev->page,
548 rbi = r5_next_bio(rbi, dev->sector);
553 atomic_inc(&sh->count);
554 async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
555 ops_complete_biofill, sh);
558 static void ops_complete_compute5(void *stripe_head_ref)
560 struct stripe_head *sh = stripe_head_ref;
561 int target = sh->ops.target;
562 struct r5dev *tgt = &sh->dev[target];
564 pr_debug("%s: stripe %llu\n", __func__,
565 (unsigned long long)sh->sector);
567 set_bit(R5_UPTODATE, &tgt->flags);
568 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
569 clear_bit(R5_Wantcompute, &tgt->flags);
570 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
571 if (sh->check_state == check_state_compute_run)
572 sh->check_state = check_state_compute_result;
573 set_bit(STRIPE_HANDLE, &sh->state);
577 static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
579 /* kernel stack size limits the total number of disks */
580 int disks = sh->disks;
581 struct page *xor_srcs[disks];
582 int target = sh->ops.target;
583 struct r5dev *tgt = &sh->dev[target];
584 struct page *xor_dest = tgt->page;
586 struct dma_async_tx_descriptor *tx;
589 pr_debug("%s: stripe %llu block: %d\n",
590 __func__, (unsigned long long)sh->sector, target);
591 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
593 for (i = disks; i--; )
595 xor_srcs[count++] = sh->dev[i].page;
597 atomic_inc(&sh->count);
599 if (unlikely(count == 1))
600 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
601 0, NULL, ops_complete_compute5, sh);
603 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
604 ASYNC_TX_XOR_ZERO_DST, NULL,
605 ops_complete_compute5, sh);
610 static void ops_complete_prexor(void *stripe_head_ref)
612 struct stripe_head *sh = stripe_head_ref;
614 pr_debug("%s: stripe %llu\n", __func__,
615 (unsigned long long)sh->sector);
618 static struct dma_async_tx_descriptor *
619 ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
621 /* kernel stack size limits the total number of disks */
622 int disks = sh->disks;
623 struct page *xor_srcs[disks];
624 int count = 0, pd_idx = sh->pd_idx, i;
626 /* existing parity data subtracted */
627 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
629 pr_debug("%s: stripe %llu\n", __func__,
630 (unsigned long long)sh->sector);
632 for (i = disks; i--; ) {
633 struct r5dev *dev = &sh->dev[i];
634 /* Only process blocks that are known to be uptodate */
635 if (test_bit(R5_Wantdrain, &dev->flags))
636 xor_srcs[count++] = dev->page;
639 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
640 ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
641 ops_complete_prexor, sh);
646 static struct dma_async_tx_descriptor *
647 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
649 int disks = sh->disks;
652 pr_debug("%s: stripe %llu\n", __func__,
653 (unsigned long long)sh->sector);
655 for (i = disks; i--; ) {
656 struct r5dev *dev = &sh->dev[i];
659 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
662 spin_lock(&sh->lock);
663 chosen = dev->towrite;
665 BUG_ON(dev->written);
666 wbi = dev->written = chosen;
667 spin_unlock(&sh->lock);
669 while (wbi && wbi->bi_sector <
670 dev->sector + STRIPE_SECTORS) {
671 tx = async_copy_data(1, wbi, dev->page,
673 wbi = r5_next_bio(wbi, dev->sector);
681 static void ops_complete_postxor(void *stripe_head_ref)
683 struct stripe_head *sh = stripe_head_ref;
684 int disks = sh->disks, i, pd_idx = sh->pd_idx;
686 pr_debug("%s: stripe %llu\n", __func__,
687 (unsigned long long)sh->sector);
689 for (i = disks; i--; ) {
690 struct r5dev *dev = &sh->dev[i];
691 if (dev->written || i == pd_idx)
692 set_bit(R5_UPTODATE, &dev->flags);
695 if (sh->reconstruct_state == reconstruct_state_drain_run)
696 sh->reconstruct_state = reconstruct_state_drain_result;
697 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
698 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
700 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
701 sh->reconstruct_state = reconstruct_state_result;
704 set_bit(STRIPE_HANDLE, &sh->state);
709 ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
711 /* kernel stack size limits the total number of disks */
712 int disks = sh->disks;
713 struct page *xor_srcs[disks];
715 int count = 0, pd_idx = sh->pd_idx, i;
716 struct page *xor_dest;
720 pr_debug("%s: stripe %llu\n", __func__,
721 (unsigned long long)sh->sector);
723 /* check if prexor is active which means only process blocks
724 * that are part of a read-modify-write (written)
726 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
728 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
729 for (i = disks; i--; ) {
730 struct r5dev *dev = &sh->dev[i];
732 xor_srcs[count++] = dev->page;
735 xor_dest = sh->dev[pd_idx].page;
736 for (i = disks; i--; ) {
737 struct r5dev *dev = &sh->dev[i];
739 xor_srcs[count++] = dev->page;
743 /* 1/ if we prexor'd then the dest is reused as a source
744 * 2/ if we did not prexor then we are redoing the parity
745 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
746 * for the synchronous xor case
748 flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
749 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
751 atomic_inc(&sh->count);
753 if (unlikely(count == 1)) {
754 flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
755 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
756 flags, tx, ops_complete_postxor, sh);
758 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
759 flags, tx, ops_complete_postxor, sh);
762 static void ops_complete_check(void *stripe_head_ref)
764 struct stripe_head *sh = stripe_head_ref;
766 pr_debug("%s: stripe %llu\n", __func__,
767 (unsigned long long)sh->sector);
769 sh->check_state = check_state_check_result;
770 set_bit(STRIPE_HANDLE, &sh->state);
774 static void ops_run_check(struct stripe_head *sh)
776 /* kernel stack size limits the total number of disks */
777 int disks = sh->disks;
778 struct page *xor_srcs[disks];
779 struct dma_async_tx_descriptor *tx;
781 int count = 0, pd_idx = sh->pd_idx, i;
782 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
784 pr_debug("%s: stripe %llu\n", __func__,
785 (unsigned long long)sh->sector);
787 for (i = disks; i--; ) {
788 struct r5dev *dev = &sh->dev[i];
790 xor_srcs[count++] = dev->page;
793 tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
794 &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
796 atomic_inc(&sh->count);
797 tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
798 ops_complete_check, sh);
801 static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
803 int overlap_clear = 0, i, disks = sh->disks;
804 struct dma_async_tx_descriptor *tx = NULL;
806 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
811 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
812 tx = ops_run_compute5(sh);
813 /* terminate the chain if postxor is not set to be run */
814 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
818 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
819 tx = ops_run_prexor(sh, tx);
821 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
822 tx = ops_run_biodrain(sh, tx);
826 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
827 ops_run_postxor(sh, tx);
829 if (test_bit(STRIPE_OP_CHECK, &ops_request))
833 for (i = disks; i--; ) {
834 struct r5dev *dev = &sh->dev[i];
835 if (test_and_clear_bit(R5_Overlap, &dev->flags))
836 wake_up(&sh->raid_conf->wait_for_overlap);
840 static int grow_one_stripe(raid5_conf_t *conf)
842 struct stripe_head *sh;
843 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
846 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
847 sh->raid_conf = conf;
848 spin_lock_init(&sh->lock);
850 if (grow_buffers(sh, conf->raid_disks)) {
851 shrink_buffers(sh, conf->raid_disks);
852 kmem_cache_free(conf->slab_cache, sh);
855 sh->disks = conf->raid_disks;
856 /* we just created an active stripe so... */
857 atomic_set(&sh->count, 1);
858 atomic_inc(&conf->active_stripes);
859 INIT_LIST_HEAD(&sh->lru);
864 static int grow_stripes(raid5_conf_t *conf, int num)
866 struct kmem_cache *sc;
867 int devs = conf->raid_disks;
869 sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
870 sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
871 conf->active_name = 0;
872 sc = kmem_cache_create(conf->cache_name[conf->active_name],
873 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
877 conf->slab_cache = sc;
878 conf->pool_size = devs;
880 if (!grow_one_stripe(conf))
885 #ifdef CONFIG_MD_RAID5_RESHAPE
886 static int resize_stripes(raid5_conf_t *conf, int newsize)
888 /* Make all the stripes able to hold 'newsize' devices.
889 * New slots in each stripe get 'page' set to a new page.
891 * This happens in stages:
892 * 1/ create a new kmem_cache and allocate the required number of
894 * 2/ gather all the old stripe_heads and tranfer the pages across
895 * to the new stripe_heads. This will have the side effect of
896 * freezing the array as once all stripe_heads have been collected,
897 * no IO will be possible. Old stripe heads are freed once their
898 * pages have been transferred over, and the old kmem_cache is
899 * freed when all stripes are done.
900 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
901 * we simple return a failre status - no need to clean anything up.
902 * 4/ allocate new pages for the new slots in the new stripe_heads.
903 * If this fails, we don't bother trying the shrink the
904 * stripe_heads down again, we just leave them as they are.
905 * As each stripe_head is processed the new one is released into
908 * Once step2 is started, we cannot afford to wait for a write,
909 * so we use GFP_NOIO allocations.
911 struct stripe_head *osh, *nsh;
912 LIST_HEAD(newstripes);
913 struct disk_info *ndisks;
915 struct kmem_cache *sc;
918 if (newsize <= conf->pool_size)
919 return 0; /* never bother to shrink */
921 err = md_allow_write(conf->mddev);
926 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
927 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
932 for (i = conf->max_nr_stripes; i; i--) {
933 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
937 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
939 nsh->raid_conf = conf;
940 spin_lock_init(&nsh->lock);
942 list_add(&nsh->lru, &newstripes);
945 /* didn't get enough, give up */
946 while (!list_empty(&newstripes)) {
947 nsh = list_entry(newstripes.next, struct stripe_head, lru);
949 kmem_cache_free(sc, nsh);
951 kmem_cache_destroy(sc);
954 /* Step 2 - Must use GFP_NOIO now.
955 * OK, we have enough stripes, start collecting inactive
956 * stripes and copying them over
958 list_for_each_entry(nsh, &newstripes, lru) {
959 spin_lock_irq(&conf->device_lock);
960 wait_event_lock_irq(conf->wait_for_stripe,
961 !list_empty(&conf->inactive_list),
963 unplug_slaves(conf->mddev)
965 osh = get_free_stripe(conf);
966 spin_unlock_irq(&conf->device_lock);
967 atomic_set(&nsh->count, 1);
968 for(i=0; i<conf->pool_size; i++)
969 nsh->dev[i].page = osh->dev[i].page;
970 for( ; i<newsize; i++)
971 nsh->dev[i].page = NULL;
972 kmem_cache_free(conf->slab_cache, osh);
974 kmem_cache_destroy(conf->slab_cache);
977 * At this point, we are holding all the stripes so the array
978 * is completely stalled, so now is a good time to resize
981 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
983 for (i=0; i<conf->raid_disks; i++)
984 ndisks[i] = conf->disks[i];
986 conf->disks = ndisks;
990 /* Step 4, return new stripes to service */
991 while(!list_empty(&newstripes)) {
992 nsh = list_entry(newstripes.next, struct stripe_head, lru);
993 list_del_init(&nsh->lru);
994 for (i=conf->raid_disks; i < newsize; i++)
995 if (nsh->dev[i].page == NULL) {
996 struct page *p = alloc_page(GFP_NOIO);
997 nsh->dev[i].page = p;
1001 release_stripe(nsh);
1003 /* critical section pass, GFP_NOIO no longer needed */
1005 conf->slab_cache = sc;
1006 conf->active_name = 1-conf->active_name;
1007 conf->pool_size = newsize;
1012 static int drop_one_stripe(raid5_conf_t *conf)
1014 struct stripe_head *sh;
1016 spin_lock_irq(&conf->device_lock);
1017 sh = get_free_stripe(conf);
1018 spin_unlock_irq(&conf->device_lock);
1021 BUG_ON(atomic_read(&sh->count));
1022 shrink_buffers(sh, conf->pool_size);
1023 kmem_cache_free(conf->slab_cache, sh);
1024 atomic_dec(&conf->active_stripes);
1028 static void shrink_stripes(raid5_conf_t *conf)
1030 while (drop_one_stripe(conf))
1033 if (conf->slab_cache)
1034 kmem_cache_destroy(conf->slab_cache);
1035 conf->slab_cache = NULL;
1038 static void raid5_end_read_request(struct bio * bi, int error)
1040 struct stripe_head *sh = bi->bi_private;
1041 raid5_conf_t *conf = sh->raid_conf;
1042 int disks = sh->disks, i;
1043 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1044 char b[BDEVNAME_SIZE];
1048 for (i=0 ; i<disks; i++)
1049 if (bi == &sh->dev[i].req)
1052 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1053 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1061 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1062 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1063 rdev = conf->disks[i].rdev;
1064 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1065 " (%lu sectors at %llu on %s)\n",
1066 mdname(conf->mddev), STRIPE_SECTORS,
1067 (unsigned long long)(sh->sector
1068 + rdev->data_offset),
1069 bdevname(rdev->bdev, b));
1070 clear_bit(R5_ReadError, &sh->dev[i].flags);
1071 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1073 if (atomic_read(&conf->disks[i].rdev->read_errors))
1074 atomic_set(&conf->disks[i].rdev->read_errors, 0);
1076 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
1078 rdev = conf->disks[i].rdev;
1080 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1081 atomic_inc(&rdev->read_errors);
1082 if (conf->mddev->degraded)
1083 printk_rl(KERN_WARNING
1084 "raid5:%s: read error not correctable "
1085 "(sector %llu on %s).\n",
1086 mdname(conf->mddev),
1087 (unsigned long long)(sh->sector
1088 + rdev->data_offset),
1090 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
1092 printk_rl(KERN_WARNING
1093 "raid5:%s: read error NOT corrected!! "
1094 "(sector %llu on %s).\n",
1095 mdname(conf->mddev),
1096 (unsigned long long)(sh->sector
1097 + rdev->data_offset),
1099 else if (atomic_read(&rdev->read_errors)
1100 > conf->max_nr_stripes)
1102 "raid5:%s: Too many read errors, failing device %s.\n",
1103 mdname(conf->mddev), bdn);
1107 set_bit(R5_ReadError, &sh->dev[i].flags);
1109 clear_bit(R5_ReadError, &sh->dev[i].flags);
1110 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1111 md_error(conf->mddev, rdev);
1114 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1115 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1116 set_bit(STRIPE_HANDLE, &sh->state);
1120 static void raid5_end_write_request (struct bio *bi, int error)
1122 struct stripe_head *sh = bi->bi_private;
1123 raid5_conf_t *conf = sh->raid_conf;
1124 int disks = sh->disks, i;
1125 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1127 for (i=0 ; i<disks; i++)
1128 if (bi == &sh->dev[i].req)
1131 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1132 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1140 md_error(conf->mddev, conf->disks[i].rdev);
1142 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1144 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1145 set_bit(STRIPE_HANDLE, &sh->state);
1150 static sector_t compute_blocknr(struct stripe_head *sh, int i);
1152 static void raid5_build_block (struct stripe_head *sh, int i)
1154 struct r5dev *dev = &sh->dev[i];
1156 bio_init(&dev->req);
1157 dev->req.bi_io_vec = &dev->vec;
1159 dev->req.bi_max_vecs++;
1160 dev->vec.bv_page = dev->page;
1161 dev->vec.bv_len = STRIPE_SIZE;
1162 dev->vec.bv_offset = 0;
1164 dev->req.bi_sector = sh->sector;
1165 dev->req.bi_private = sh;
1168 dev->sector = compute_blocknr(sh, i);
1171 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1173 char b[BDEVNAME_SIZE];
1174 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1175 pr_debug("raid5: error called\n");
1177 if (!test_bit(Faulty, &rdev->flags)) {
1178 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1179 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1180 unsigned long flags;
1181 spin_lock_irqsave(&conf->device_lock, flags);
1183 spin_unlock_irqrestore(&conf->device_lock, flags);
1185 * if recovery was running, make sure it aborts.
1187 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1189 set_bit(Faulty, &rdev->flags);
1191 "raid5: Disk failure on %s, disabling device.\n"
1192 "raid5: Operation continuing on %d devices.\n",
1193 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1198 * Input: a 'big' sector number,
1199 * Output: index of the data and parity disk, and the sector # in them.
1201 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
1202 unsigned int data_disks, unsigned int * dd_idx,
1203 unsigned int * pd_idx, raid5_conf_t *conf)
1206 unsigned long chunk_number;
1207 unsigned int chunk_offset;
1208 sector_t new_sector;
1209 int sectors_per_chunk = conf->chunk_size >> 9;
1211 /* First compute the information on this sector */
1214 * Compute the chunk number and the sector offset inside the chunk
1216 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1217 chunk_number = r_sector;
1218 BUG_ON(r_sector != chunk_number);
1221 * Compute the stripe number
1223 stripe = chunk_number / data_disks;
1226 * Compute the data disk and parity disk indexes inside the stripe
1228 *dd_idx = chunk_number % data_disks;
1231 * Select the parity disk based on the user selected algorithm.
1233 switch(conf->level) {
1235 *pd_idx = data_disks;
1238 switch (conf->algorithm) {
1239 case ALGORITHM_LEFT_ASYMMETRIC:
1240 *pd_idx = data_disks - stripe % raid_disks;
1241 if (*dd_idx >= *pd_idx)
1244 case ALGORITHM_RIGHT_ASYMMETRIC:
1245 *pd_idx = stripe % raid_disks;
1246 if (*dd_idx >= *pd_idx)
1249 case ALGORITHM_LEFT_SYMMETRIC:
1250 *pd_idx = data_disks - stripe % raid_disks;
1251 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
1253 case ALGORITHM_RIGHT_SYMMETRIC:
1254 *pd_idx = stripe % raid_disks;
1255 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
1258 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1264 /**** FIX THIS ****/
1265 switch (conf->algorithm) {
1266 case ALGORITHM_LEFT_ASYMMETRIC:
1267 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
1268 if (*pd_idx == raid_disks-1)
1269 (*dd_idx)++; /* Q D D D P */
1270 else if (*dd_idx >= *pd_idx)
1271 (*dd_idx) += 2; /* D D P Q D */
1273 case ALGORITHM_RIGHT_ASYMMETRIC:
1274 *pd_idx = stripe % raid_disks;
1275 if (*pd_idx == raid_disks-1)
1276 (*dd_idx)++; /* Q D D D P */
1277 else if (*dd_idx >= *pd_idx)
1278 (*dd_idx) += 2; /* D D P Q D */
1280 case ALGORITHM_LEFT_SYMMETRIC:
1281 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
1282 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
1284 case ALGORITHM_RIGHT_SYMMETRIC:
1285 *pd_idx = stripe % raid_disks;
1286 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
1289 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
1296 * Finally, compute the new sector number
1298 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1303 static sector_t compute_blocknr(struct stripe_head *sh, int i)
1305 raid5_conf_t *conf = sh->raid_conf;
1306 int raid_disks = sh->disks;
1307 int data_disks = raid_disks - conf->max_degraded;
1308 sector_t new_sector = sh->sector, check;
1309 int sectors_per_chunk = conf->chunk_size >> 9;
1312 int chunk_number, dummy1, dummy2, dd_idx = i;
1316 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1317 stripe = new_sector;
1318 BUG_ON(new_sector != stripe);
1320 if (i == sh->pd_idx)
1322 switch(conf->level) {
1325 switch (conf->algorithm) {
1326 case ALGORITHM_LEFT_ASYMMETRIC:
1327 case ALGORITHM_RIGHT_ASYMMETRIC:
1331 case ALGORITHM_LEFT_SYMMETRIC:
1332 case ALGORITHM_RIGHT_SYMMETRIC:
1335 i -= (sh->pd_idx + 1);
1338 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1343 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
1344 return 0; /* It is the Q disk */
1345 switch (conf->algorithm) {
1346 case ALGORITHM_LEFT_ASYMMETRIC:
1347 case ALGORITHM_RIGHT_ASYMMETRIC:
1348 if (sh->pd_idx == raid_disks-1)
1349 i--; /* Q D D D P */
1350 else if (i > sh->pd_idx)
1351 i -= 2; /* D D P Q D */
1353 case ALGORITHM_LEFT_SYMMETRIC:
1354 case ALGORITHM_RIGHT_SYMMETRIC:
1355 if (sh->pd_idx == raid_disks-1)
1356 i--; /* Q D D D P */
1361 i -= (sh->pd_idx + 2);
1365 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
1371 chunk_number = stripe * data_disks + i;
1372 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1374 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
1375 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
1376 printk(KERN_ERR "compute_blocknr: map not correct\n");
1385 * Copy data between a page in the stripe cache, and one or more bion
1386 * The page could align with the middle of the bio, or there could be
1387 * several bion, each with several bio_vecs, which cover part of the page
1388 * Multiple bion are linked together on bi_next. There may be extras
1389 * at the end of this list. We ignore them.
1391 static void copy_data(int frombio, struct bio *bio,
1395 char *pa = page_address(page);
1396 struct bio_vec *bvl;
1400 if (bio->bi_sector >= sector)
1401 page_offset = (signed)(bio->bi_sector - sector) * 512;
1403 page_offset = (signed)(sector - bio->bi_sector) * -512;
1404 bio_for_each_segment(bvl, bio, i) {
1405 int len = bio_iovec_idx(bio,i)->bv_len;
1409 if (page_offset < 0) {
1410 b_offset = -page_offset;
1411 page_offset += b_offset;
1415 if (len > 0 && page_offset + len > STRIPE_SIZE)
1416 clen = STRIPE_SIZE - page_offset;
1420 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1422 memcpy(pa+page_offset, ba+b_offset, clen);
1424 memcpy(ba+b_offset, pa+page_offset, clen);
1425 __bio_kunmap_atomic(ba, KM_USER0);
1427 if (clen < len) /* hit end of page */
1433 #define check_xor() do { \
1434 if (count == MAX_XOR_BLOCKS) { \
1435 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1440 static void compute_parity6(struct stripe_head *sh, int method)
1442 raid6_conf_t *conf = sh->raid_conf;
1443 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
1445 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1448 qd_idx = raid6_next_disk(pd_idx, disks);
1449 d0_idx = raid6_next_disk(qd_idx, disks);
1451 pr_debug("compute_parity, stripe %llu, method %d\n",
1452 (unsigned long long)sh->sector, method);
1455 case READ_MODIFY_WRITE:
1456 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1457 case RECONSTRUCT_WRITE:
1458 for (i= disks; i-- ;)
1459 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1460 chosen = sh->dev[i].towrite;
1461 sh->dev[i].towrite = NULL;
1463 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1464 wake_up(&conf->wait_for_overlap);
1466 BUG_ON(sh->dev[i].written);
1467 sh->dev[i].written = chosen;
1471 BUG(); /* Not implemented yet */
1474 for (i = disks; i--;)
1475 if (sh->dev[i].written) {
1476 sector_t sector = sh->dev[i].sector;
1477 struct bio *wbi = sh->dev[i].written;
1478 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1479 copy_data(1, wbi, sh->dev[i].page, sector);
1480 wbi = r5_next_bio(wbi, sector);
1483 set_bit(R5_LOCKED, &sh->dev[i].flags);
1484 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1488 // case RECONSTRUCT_WRITE:
1489 // case CHECK_PARITY:
1490 // case UPDATE_PARITY:
1491 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1492 /* FIX: Is this ordering of drives even remotely optimal? */
1496 ptrs[count++] = page_address(sh->dev[i].page);
1497 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1498 printk("block %d/%d not uptodate on parity calc\n", i,count);
1499 i = raid6_next_disk(i, disks);
1500 } while ( i != d0_idx );
1504 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1507 case RECONSTRUCT_WRITE:
1508 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1509 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1510 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1511 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1514 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1515 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1521 /* Compute one missing block */
1522 static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1524 int i, count, disks = sh->disks;
1525 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
1526 int pd_idx = sh->pd_idx;
1527 int qd_idx = raid6_next_disk(pd_idx, disks);
1529 pr_debug("compute_block_1, stripe %llu, idx %d\n",
1530 (unsigned long long)sh->sector, dd_idx);
1532 if ( dd_idx == qd_idx ) {
1533 /* We're actually computing the Q drive */
1534 compute_parity6(sh, UPDATE_PARITY);
1536 dest = page_address(sh->dev[dd_idx].page);
1537 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1539 for (i = disks ; i--; ) {
1540 if (i == dd_idx || i == qd_idx)
1542 p = page_address(sh->dev[i].page);
1543 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1546 printk("compute_block() %d, stripe %llu, %d"
1547 " not present\n", dd_idx,
1548 (unsigned long long)sh->sector, i);
1553 xor_blocks(count, STRIPE_SIZE, dest, ptr);
1554 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1555 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1559 /* Compute two missing blocks */
1560 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1562 int i, count, disks = sh->disks;
1563 int pd_idx = sh->pd_idx;
1564 int qd_idx = raid6_next_disk(pd_idx, disks);
1565 int d0_idx = raid6_next_disk(qd_idx, disks);
1568 /* faila and failb are disk numbers relative to d0_idx */
1569 /* pd_idx become disks-2 and qd_idx become disks-1 */
1570 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1571 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1573 BUG_ON(faila == failb);
1574 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1576 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1577 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1579 if ( failb == disks-1 ) {
1580 /* Q disk is one of the missing disks */
1581 if ( faila == disks-2 ) {
1582 /* Missing P+Q, just recompute */
1583 compute_parity6(sh, UPDATE_PARITY);
1586 /* We're missing D+Q; recompute D from P */
1587 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1588 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1593 /* We're missing D+P or D+D; build pointer table */
1595 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1601 ptrs[count++] = page_address(sh->dev[i].page);
1602 i = raid6_next_disk(i, disks);
1603 if (i != dd_idx1 && i != dd_idx2 &&
1604 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1605 printk("compute_2 with missing block %d/%d\n", count, i);
1606 } while ( i != d0_idx );
1608 if ( failb == disks-2 ) {
1609 /* We're missing D+P. */
1610 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1612 /* We're missing D+D. */
1613 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1616 /* Both the above update both missing blocks */
1617 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1618 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1623 schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
1624 int rcw, int expand)
1626 int i, pd_idx = sh->pd_idx, disks = sh->disks;
1629 /* if we are not expanding this is a proper write request, and
1630 * there will be bios with new data to be drained into the
1634 sh->reconstruct_state = reconstruct_state_drain_run;
1635 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1637 sh->reconstruct_state = reconstruct_state_run;
1639 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
1641 for (i = disks; i--; ) {
1642 struct r5dev *dev = &sh->dev[i];
1645 set_bit(R5_LOCKED, &dev->flags);
1646 set_bit(R5_Wantdrain, &dev->flags);
1648 clear_bit(R5_UPTODATE, &dev->flags);
1652 if (s->locked + 1 == disks)
1653 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1654 atomic_inc(&sh->raid_conf->pending_full_writes);
1656 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1657 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1659 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
1660 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1661 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1662 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
1664 for (i = disks; i--; ) {
1665 struct r5dev *dev = &sh->dev[i];
1670 (test_bit(R5_UPTODATE, &dev->flags) ||
1671 test_bit(R5_Wantcompute, &dev->flags))) {
1672 set_bit(R5_Wantdrain, &dev->flags);
1673 set_bit(R5_LOCKED, &dev->flags);
1674 clear_bit(R5_UPTODATE, &dev->flags);
1680 /* keep the parity disk locked while asynchronous operations
1683 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1684 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1687 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
1688 __func__, (unsigned long long)sh->sector,
1689 s->locked, s->ops_request);
1693 * Each stripe/dev can have one or more bion attached.
1694 * toread/towrite point to the first in a chain.
1695 * The bi_next chain must be in order.
1697 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1700 raid5_conf_t *conf = sh->raid_conf;
1703 pr_debug("adding bh b#%llu to stripe s#%llu\n",
1704 (unsigned long long)bi->bi_sector,
1705 (unsigned long long)sh->sector);
1708 spin_lock(&sh->lock);
1709 spin_lock_irq(&conf->device_lock);
1711 bip = &sh->dev[dd_idx].towrite;
1712 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1715 bip = &sh->dev[dd_idx].toread;
1716 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1717 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1719 bip = & (*bip)->bi_next;
1721 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1724 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1728 bi->bi_phys_segments ++;
1729 spin_unlock_irq(&conf->device_lock);
1730 spin_unlock(&sh->lock);
1732 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
1733 (unsigned long long)bi->bi_sector,
1734 (unsigned long long)sh->sector, dd_idx);
1736 if (conf->mddev->bitmap && firstwrite) {
1737 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1739 sh->bm_seq = conf->seq_flush+1;
1740 set_bit(STRIPE_BIT_DELAY, &sh->state);
1744 /* check if page is covered */
1745 sector_t sector = sh->dev[dd_idx].sector;
1746 for (bi=sh->dev[dd_idx].towrite;
1747 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1748 bi && bi->bi_sector <= sector;
1749 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1750 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1751 sector = bi->bi_sector + (bi->bi_size>>9);
1753 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1754 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1759 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1760 spin_unlock_irq(&conf->device_lock);
1761 spin_unlock(&sh->lock);
1765 static void end_reshape(raid5_conf_t *conf);
1767 static int page_is_zero(struct page *p)
1769 char *a = page_address(p);
1770 return ((*(u32*)a) == 0 &&
1771 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1774 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1776 int sectors_per_chunk = conf->chunk_size >> 9;
1778 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1780 raid5_compute_sector(stripe * (disks - conf->max_degraded)
1781 *sectors_per_chunk + chunk_offset,
1782 disks, disks - conf->max_degraded,
1783 &dd_idx, &pd_idx, conf);
1788 handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
1789 struct stripe_head_state *s, int disks,
1790 struct bio **return_bi)
1793 for (i = disks; i--; ) {
1797 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1800 rdev = rcu_dereference(conf->disks[i].rdev);
1801 if (rdev && test_bit(In_sync, &rdev->flags))
1802 /* multiple read failures in one stripe */
1803 md_error(conf->mddev, rdev);
1806 spin_lock_irq(&conf->device_lock);
1807 /* fail all writes first */
1808 bi = sh->dev[i].towrite;
1809 sh->dev[i].towrite = NULL;
1815 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1816 wake_up(&conf->wait_for_overlap);
1818 while (bi && bi->bi_sector <
1819 sh->dev[i].sector + STRIPE_SECTORS) {
1820 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1821 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1822 if (--bi->bi_phys_segments == 0) {
1823 md_write_end(conf->mddev);
1824 bi->bi_next = *return_bi;
1829 /* and fail all 'written' */
1830 bi = sh->dev[i].written;
1831 sh->dev[i].written = NULL;
1832 if (bi) bitmap_end = 1;
1833 while (bi && bi->bi_sector <
1834 sh->dev[i].sector + STRIPE_SECTORS) {
1835 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1836 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1837 if (--bi->bi_phys_segments == 0) {
1838 md_write_end(conf->mddev);
1839 bi->bi_next = *return_bi;
1845 /* fail any reads if this device is non-operational and
1846 * the data has not reached the cache yet.
1848 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
1849 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1850 test_bit(R5_ReadError, &sh->dev[i].flags))) {
1851 bi = sh->dev[i].toread;
1852 sh->dev[i].toread = NULL;
1853 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1854 wake_up(&conf->wait_for_overlap);
1855 if (bi) s->to_read--;
1856 while (bi && bi->bi_sector <
1857 sh->dev[i].sector + STRIPE_SECTORS) {
1858 struct bio *nextbi =
1859 r5_next_bio(bi, sh->dev[i].sector);
1860 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1861 if (--bi->bi_phys_segments == 0) {
1862 bi->bi_next = *return_bi;
1868 spin_unlock_irq(&conf->device_lock);
1870 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1871 STRIPE_SECTORS, 0, 0);
1874 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
1875 if (atomic_dec_and_test(&conf->pending_full_writes))
1876 md_wakeup_thread(conf->mddev->thread);
1879 /* fetch_block5 - checks the given member device to see if its data needs
1880 * to be read or computed to satisfy a request.
1882 * Returns 1 when no more member devices need to be checked, otherwise returns
1883 * 0 to tell the loop in handle_stripe_fill5 to continue
1885 static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
1886 int disk_idx, int disks)
1888 struct r5dev *dev = &sh->dev[disk_idx];
1889 struct r5dev *failed_dev = &sh->dev[s->failed_num];
1891 /* is the data in this block needed, and can we get it? */
1892 if (!test_bit(R5_LOCKED, &dev->flags) &&
1893 !test_bit(R5_UPTODATE, &dev->flags) &&
1895 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1896 s->syncing || s->expanding ||
1898 (failed_dev->toread ||
1899 (failed_dev->towrite &&
1900 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
1901 /* We would like to get this block, possibly by computing it,
1902 * otherwise read it if the backing disk is insync
1904 if ((s->uptodate == disks - 1) &&
1905 (s->failed && disk_idx == s->failed_num)) {
1906 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
1907 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
1908 set_bit(R5_Wantcompute, &dev->flags);
1909 sh->ops.target = disk_idx;
1911 /* Careful: from this point on 'uptodate' is in the eye
1912 * of raid5_run_ops which services 'compute' operations
1913 * before writes. R5_Wantcompute flags a block that will
1914 * be R5_UPTODATE by the time it is needed for a
1915 * subsequent operation.
1918 return 1; /* uptodate + compute == disks */
1919 } else if (test_bit(R5_Insync, &dev->flags)) {
1920 set_bit(R5_LOCKED, &dev->flags);
1921 set_bit(R5_Wantread, &dev->flags);
1923 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
1932 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
1934 static void handle_stripe_fill5(struct stripe_head *sh,
1935 struct stripe_head_state *s, int disks)
1939 /* look for blocks to read/compute, skip this if a compute
1940 * is already in flight, or if the stripe contents are in the
1941 * midst of changing due to a write
1943 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
1944 !sh->reconstruct_state)
1945 for (i = disks; i--; )
1946 if (fetch_block5(sh, s, i, disks))
1948 set_bit(STRIPE_HANDLE, &sh->state);
1951 static void handle_stripe_fill6(struct stripe_head *sh,
1952 struct stripe_head_state *s, struct r6_state *r6s,
1956 for (i = disks; i--; ) {
1957 struct r5dev *dev = &sh->dev[i];
1958 if (!test_bit(R5_LOCKED, &dev->flags) &&
1959 !test_bit(R5_UPTODATE, &dev->flags) &&
1960 (dev->toread || (dev->towrite &&
1961 !test_bit(R5_OVERWRITE, &dev->flags)) ||
1962 s->syncing || s->expanding ||
1964 (sh->dev[r6s->failed_num[0]].toread ||
1967 (sh->dev[r6s->failed_num[1]].toread ||
1969 /* we would like to get this block, possibly
1970 * by computing it, but we might not be able to
1972 if ((s->uptodate == disks - 1) &&
1973 (s->failed && (i == r6s->failed_num[0] ||
1974 i == r6s->failed_num[1]))) {
1975 pr_debug("Computing stripe %llu block %d\n",
1976 (unsigned long long)sh->sector, i);
1977 compute_block_1(sh, i, 0);
1979 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
1980 /* Computing 2-failure is *very* expensive; only
1981 * do it if failed >= 2
1984 for (other = disks; other--; ) {
1987 if (!test_bit(R5_UPTODATE,
1988 &sh->dev[other].flags))
1992 pr_debug("Computing stripe %llu blocks %d,%d\n",
1993 (unsigned long long)sh->sector,
1995 compute_block_2(sh, i, other);
1997 } else if (test_bit(R5_Insync, &dev->flags)) {
1998 set_bit(R5_LOCKED, &dev->flags);
1999 set_bit(R5_Wantread, &dev->flags);
2001 pr_debug("Reading block %d (sync=%d)\n",
2006 set_bit(STRIPE_HANDLE, &sh->state);
2010 /* handle_stripe_clean_event
2011 * any written block on an uptodate or failed drive can be returned.
2012 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2013 * never LOCKED, so we don't need to test 'failed' directly.
2015 static void handle_stripe_clean_event(raid5_conf_t *conf,
2016 struct stripe_head *sh, int disks, struct bio **return_bi)
2021 for (i = disks; i--; )
2022 if (sh->dev[i].written) {
2024 if (!test_bit(R5_LOCKED, &dev->flags) &&
2025 test_bit(R5_UPTODATE, &dev->flags)) {
2026 /* We can return any write requests */
2027 struct bio *wbi, *wbi2;
2029 pr_debug("Return write for disc %d\n", i);
2030 spin_lock_irq(&conf->device_lock);
2032 dev->written = NULL;
2033 while (wbi && wbi->bi_sector <
2034 dev->sector + STRIPE_SECTORS) {
2035 wbi2 = r5_next_bio(wbi, dev->sector);
2036 if (--wbi->bi_phys_segments == 0) {
2037 md_write_end(conf->mddev);
2038 wbi->bi_next = *return_bi;
2043 if (dev->towrite == NULL)
2045 spin_unlock_irq(&conf->device_lock);
2047 bitmap_endwrite(conf->mddev->bitmap,
2050 !test_bit(STRIPE_DEGRADED, &sh->state),
2055 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2056 if (atomic_dec_and_test(&conf->pending_full_writes))
2057 md_wakeup_thread(conf->mddev->thread);
2060 static void handle_stripe_dirtying5(raid5_conf_t *conf,
2061 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2063 int rmw = 0, rcw = 0, i;
2064 for (i = disks; i--; ) {
2065 /* would I have to read this buffer for read_modify_write */
2066 struct r5dev *dev = &sh->dev[i];
2067 if ((dev->towrite || i == sh->pd_idx) &&
2068 !test_bit(R5_LOCKED, &dev->flags) &&
2069 !(test_bit(R5_UPTODATE, &dev->flags) ||
2070 test_bit(R5_Wantcompute, &dev->flags))) {
2071 if (test_bit(R5_Insync, &dev->flags))
2074 rmw += 2*disks; /* cannot read it */
2076 /* Would I have to read this buffer for reconstruct_write */
2077 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2078 !test_bit(R5_LOCKED, &dev->flags) &&
2079 !(test_bit(R5_UPTODATE, &dev->flags) ||
2080 test_bit(R5_Wantcompute, &dev->flags))) {
2081 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2086 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2087 (unsigned long long)sh->sector, rmw, rcw);
2088 set_bit(STRIPE_HANDLE, &sh->state);
2089 if (rmw < rcw && rmw > 0)
2090 /* prefer read-modify-write, but need to get some data */
2091 for (i = disks; i--; ) {
2092 struct r5dev *dev = &sh->dev[i];
2093 if ((dev->towrite || i == sh->pd_idx) &&
2094 !test_bit(R5_LOCKED, &dev->flags) &&
2095 !(test_bit(R5_UPTODATE, &dev->flags) ||
2096 test_bit(R5_Wantcompute, &dev->flags)) &&
2097 test_bit(R5_Insync, &dev->flags)) {
2099 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2100 pr_debug("Read_old block "
2101 "%d for r-m-w\n", i);
2102 set_bit(R5_LOCKED, &dev->flags);
2103 set_bit(R5_Wantread, &dev->flags);
2106 set_bit(STRIPE_DELAYED, &sh->state);
2107 set_bit(STRIPE_HANDLE, &sh->state);
2111 if (rcw <= rmw && rcw > 0)
2112 /* want reconstruct write, but need to get some data */
2113 for (i = disks; i--; ) {
2114 struct r5dev *dev = &sh->dev[i];
2115 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2117 !test_bit(R5_LOCKED, &dev->flags) &&
2118 !(test_bit(R5_UPTODATE, &dev->flags) ||
2119 test_bit(R5_Wantcompute, &dev->flags)) &&
2120 test_bit(R5_Insync, &dev->flags)) {
2122 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2123 pr_debug("Read_old block "
2124 "%d for Reconstruct\n", i);
2125 set_bit(R5_LOCKED, &dev->flags);
2126 set_bit(R5_Wantread, &dev->flags);
2129 set_bit(STRIPE_DELAYED, &sh->state);
2130 set_bit(STRIPE_HANDLE, &sh->state);
2134 /* now if nothing is locked, and if we have enough data,
2135 * we can start a write request
2137 /* since handle_stripe can be called at any time we need to handle the
2138 * case where a compute block operation has been submitted and then a
2139 * subsequent call wants to start a write request. raid5_run_ops only
2140 * handles the case where compute block and postxor are requested
2141 * simultaneously. If this is not the case then new writes need to be
2142 * held off until the compute completes.
2144 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2145 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2146 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2147 schedule_reconstruction5(sh, s, rcw == 0, 0);
2150 static void handle_stripe_dirtying6(raid5_conf_t *conf,
2151 struct stripe_head *sh, struct stripe_head_state *s,
2152 struct r6_state *r6s, int disks)
2154 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
2155 int qd_idx = r6s->qd_idx;
2156 for (i = disks; i--; ) {
2157 struct r5dev *dev = &sh->dev[i];
2158 /* Would I have to read this buffer for reconstruct_write */
2159 if (!test_bit(R5_OVERWRITE, &dev->flags)
2160 && i != pd_idx && i != qd_idx
2161 && (!test_bit(R5_LOCKED, &dev->flags)
2163 !test_bit(R5_UPTODATE, &dev->flags)) {
2164 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2166 pr_debug("raid6: must_compute: "
2167 "disk %d flags=%#lx\n", i, dev->flags);
2172 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
2173 (unsigned long long)sh->sector, rcw, must_compute);
2174 set_bit(STRIPE_HANDLE, &sh->state);
2177 /* want reconstruct write, but need to get some data */
2178 for (i = disks; i--; ) {
2179 struct r5dev *dev = &sh->dev[i];
2180 if (!test_bit(R5_OVERWRITE, &dev->flags)
2181 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2182 && !test_bit(R5_LOCKED, &dev->flags) &&
2183 !test_bit(R5_UPTODATE, &dev->flags) &&
2184 test_bit(R5_Insync, &dev->flags)) {
2186 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2187 pr_debug("Read_old stripe %llu "
2188 "block %d for Reconstruct\n",
2189 (unsigned long long)sh->sector, i);
2190 set_bit(R5_LOCKED, &dev->flags);
2191 set_bit(R5_Wantread, &dev->flags);
2194 pr_debug("Request delayed stripe %llu "
2195 "block %d for Reconstruct\n",
2196 (unsigned long long)sh->sector, i);
2197 set_bit(STRIPE_DELAYED, &sh->state);
2198 set_bit(STRIPE_HANDLE, &sh->state);
2202 /* now if nothing is locked, and if we have enough data, we can start a
2205 if (s->locked == 0 && rcw == 0 &&
2206 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2207 if (must_compute > 0) {
2208 /* We have failed blocks and need to compute them */
2209 switch (s->failed) {
2213 compute_block_1(sh, r6s->failed_num[0], 0);
2216 compute_block_2(sh, r6s->failed_num[0],
2217 r6s->failed_num[1]);
2219 default: /* This request should have been failed? */
2224 pr_debug("Computing parity for stripe %llu\n",
2225 (unsigned long long)sh->sector);
2226 compute_parity6(sh, RECONSTRUCT_WRITE);
2227 /* now every locked buffer is ready to be written */
2228 for (i = disks; i--; )
2229 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2230 pr_debug("Writing stripe %llu block %d\n",
2231 (unsigned long long)sh->sector, i);
2233 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2235 if (s->locked == disks)
2236 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2237 atomic_inc(&conf->pending_full_writes);
2238 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2239 set_bit(STRIPE_INSYNC, &sh->state);
2241 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2242 atomic_dec(&conf->preread_active_stripes);
2243 if (atomic_read(&conf->preread_active_stripes) <
2245 md_wakeup_thread(conf->mddev->thread);
2250 static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2251 struct stripe_head_state *s, int disks)
2253 struct r5dev *dev = NULL;
2255 set_bit(STRIPE_HANDLE, &sh->state);
2257 switch (sh->check_state) {
2258 case check_state_idle:
2259 /* start a new check operation if there are no failures */
2260 if (s->failed == 0) {
2261 BUG_ON(s->uptodate != disks);
2262 sh->check_state = check_state_run;
2263 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2264 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2268 dev = &sh->dev[s->failed_num];
2270 case check_state_compute_result:
2271 sh->check_state = check_state_idle;
2273 dev = &sh->dev[sh->pd_idx];
2275 /* check that a write has not made the stripe insync */
2276 if (test_bit(STRIPE_INSYNC, &sh->state))
2279 /* either failed parity check, or recovery is happening */
2280 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2281 BUG_ON(s->uptodate != disks);
2283 set_bit(R5_LOCKED, &dev->flags);
2285 set_bit(R5_Wantwrite, &dev->flags);
2287 clear_bit(STRIPE_DEGRADED, &sh->state);
2288 set_bit(STRIPE_INSYNC, &sh->state);
2290 case check_state_run:
2291 break; /* we will be called again upon completion */
2292 case check_state_check_result:
2293 sh->check_state = check_state_idle;
2295 /* if a failure occurred during the check operation, leave
2296 * STRIPE_INSYNC not set and let the stripe be handled again
2301 /* handle a successful check operation, if parity is correct
2302 * we are done. Otherwise update the mismatch count and repair
2303 * parity if !MD_RECOVERY_CHECK
2305 if (sh->ops.zero_sum_result == 0)
2306 /* parity is correct (on disc,
2307 * not in buffer any more)
2309 set_bit(STRIPE_INSYNC, &sh->state);
2311 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2312 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2313 /* don't try to repair!! */
2314 set_bit(STRIPE_INSYNC, &sh->state);
2316 sh->check_state = check_state_compute_run;
2317 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2318 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2319 set_bit(R5_Wantcompute,
2320 &sh->dev[sh->pd_idx].flags);
2321 sh->ops.target = sh->pd_idx;
2326 case check_state_compute_run:
2329 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2330 __func__, sh->check_state,
2331 (unsigned long long) sh->sector);
2337 static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2338 struct stripe_head_state *s,
2339 struct r6_state *r6s, struct page *tmp_page,
2342 int update_p = 0, update_q = 0;
2344 int pd_idx = sh->pd_idx;
2345 int qd_idx = r6s->qd_idx;
2347 set_bit(STRIPE_HANDLE, &sh->state);
2349 BUG_ON(s->failed > 2);
2350 BUG_ON(s->uptodate < disks);
2351 /* Want to check and possibly repair P and Q.
2352 * However there could be one 'failed' device, in which
2353 * case we can only check one of them, possibly using the
2354 * other to generate missing data
2357 /* If !tmp_page, we cannot do the calculations,
2358 * but as we have set STRIPE_HANDLE, we will soon be called
2359 * by stripe_handle with a tmp_page - just wait until then.
2362 if (s->failed == r6s->q_failed) {
2363 /* The only possible failed device holds 'Q', so it
2364 * makes sense to check P (If anything else were failed,
2365 * we would have used P to recreate it).
2367 compute_block_1(sh, pd_idx, 1);
2368 if (!page_is_zero(sh->dev[pd_idx].page)) {
2369 compute_block_1(sh, pd_idx, 0);
2373 if (!r6s->q_failed && s->failed < 2) {
2374 /* q is not failed, and we didn't use it to generate
2375 * anything, so it makes sense to check it
2377 memcpy(page_address(tmp_page),
2378 page_address(sh->dev[qd_idx].page),
2380 compute_parity6(sh, UPDATE_PARITY);
2381 if (memcmp(page_address(tmp_page),
2382 page_address(sh->dev[qd_idx].page),
2383 STRIPE_SIZE) != 0) {
2384 clear_bit(STRIPE_INSYNC, &sh->state);
2388 if (update_p || update_q) {
2389 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2390 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2391 /* don't try to repair!! */
2392 update_p = update_q = 0;
2395 /* now write out any block on a failed drive,
2396 * or P or Q if they need it
2399 if (s->failed == 2) {
2400 dev = &sh->dev[r6s->failed_num[1]];
2402 set_bit(R5_LOCKED, &dev->flags);
2403 set_bit(R5_Wantwrite, &dev->flags);
2405 if (s->failed >= 1) {
2406 dev = &sh->dev[r6s->failed_num[0]];
2408 set_bit(R5_LOCKED, &dev->flags);
2409 set_bit(R5_Wantwrite, &dev->flags);
2413 dev = &sh->dev[pd_idx];
2415 set_bit(R5_LOCKED, &dev->flags);
2416 set_bit(R5_Wantwrite, &dev->flags);
2419 dev = &sh->dev[qd_idx];
2421 set_bit(R5_LOCKED, &dev->flags);
2422 set_bit(R5_Wantwrite, &dev->flags);
2424 clear_bit(STRIPE_DEGRADED, &sh->state);
2426 set_bit(STRIPE_INSYNC, &sh->state);
2430 static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2431 struct r6_state *r6s)
2435 /* We have read all the blocks in this stripe and now we need to
2436 * copy some of them into a target stripe for expand.
2438 struct dma_async_tx_descriptor *tx = NULL;
2439 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2440 for (i = 0; i < sh->disks; i++)
2441 if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
2442 int dd_idx, pd_idx, j;
2443 struct stripe_head *sh2;
2445 sector_t bn = compute_blocknr(sh, i);
2446 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
2448 conf->max_degraded, &dd_idx,
2450 sh2 = get_active_stripe(conf, s, conf->raid_disks,
2453 /* so far only the early blocks of this stripe
2454 * have been requested. When later blocks
2455 * get requested, we will try again
2458 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2459 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2460 /* must have already done this block */
2461 release_stripe(sh2);
2465 /* place all the copies on one channel */
2466 tx = async_memcpy(sh2->dev[dd_idx].page,
2467 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2468 ASYNC_TX_DEP_ACK, tx, NULL, NULL);
2470 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2471 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2472 for (j = 0; j < conf->raid_disks; j++)
2473 if (j != sh2->pd_idx &&
2474 (!r6s || j != raid6_next_disk(sh2->pd_idx,
2476 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2478 if (j == conf->raid_disks) {
2479 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2480 set_bit(STRIPE_HANDLE, &sh2->state);
2482 release_stripe(sh2);
2485 /* done submitting copies, wait for them to complete */
2488 dma_wait_for_async_tx(tx);
2494 * handle_stripe - do things to a stripe.
2496 * We lock the stripe and then examine the state of various bits
2497 * to see what needs to be done.
2499 * return some read request which now have data
2500 * return some write requests which are safely on disc
2501 * schedule a read on some buffers
2502 * schedule a write of some buffers
2503 * return confirmation of parity correctness
2505 * buffers are taken off read_list or write_list, and bh_cache buffers
2506 * get BH_Lock set before the stripe lock is released.
2510 static bool handle_stripe5(struct stripe_head *sh)
2512 raid5_conf_t *conf = sh->raid_conf;
2513 int disks = sh->disks, i;
2514 struct bio *return_bi = NULL;
2515 struct stripe_head_state s;
2517 mdk_rdev_t *blocked_rdev = NULL;
2520 memset(&s, 0, sizeof(s));
2521 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2522 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2523 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2524 sh->reconstruct_state);
2526 spin_lock(&sh->lock);
2527 clear_bit(STRIPE_HANDLE, &sh->state);
2528 clear_bit(STRIPE_DELAYED, &sh->state);
2530 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2531 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2532 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2534 /* Now to look around and see what can be done */
2536 for (i=disks; i--; ) {
2538 struct r5dev *dev = &sh->dev[i];
2539 clear_bit(R5_Insync, &dev->flags);
2541 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2542 "written %p\n", i, dev->flags, dev->toread, dev->read,
2543 dev->towrite, dev->written);
2545 /* maybe we can request a biofill operation
2547 * new wantfill requests are only permitted while
2548 * ops_complete_biofill is guaranteed to be inactive
2550 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
2551 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
2552 set_bit(R5_Wantfill, &dev->flags);
2554 /* now count some things */
2555 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2556 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
2557 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
2559 if (test_bit(R5_Wantfill, &dev->flags))
2561 else if (dev->toread)
2565 if (!test_bit(R5_OVERWRITE, &dev->flags))
2570 rdev = rcu_dereference(conf->disks[i].rdev);
2571 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2572 blocked_rdev = rdev;
2573 atomic_inc(&rdev->nr_pending);
2576 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2577 /* The ReadError flag will just be confusing now */
2578 clear_bit(R5_ReadError, &dev->flags);
2579 clear_bit(R5_ReWrite, &dev->flags);
2581 if (!rdev || !test_bit(In_sync, &rdev->flags)
2582 || test_bit(R5_ReadError, &dev->flags)) {
2586 set_bit(R5_Insync, &dev->flags);
2590 if (unlikely(blocked_rdev)) {
2591 set_bit(STRIPE_HANDLE, &sh->state);
2595 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2596 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2597 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2600 pr_debug("locked=%d uptodate=%d to_read=%d"
2601 " to_write=%d failed=%d failed_num=%d\n",
2602 s.locked, s.uptodate, s.to_read, s.to_write,
2603 s.failed, s.failed_num);
2604 /* check if the array has lost two devices and, if so, some requests might
2607 if (s.failed > 1 && s.to_read+s.to_write+s.written)
2608 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
2609 if (s.failed > 1 && s.syncing) {
2610 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2611 clear_bit(STRIPE_SYNCING, &sh->state);
2615 /* might be able to return some write requests if the parity block
2616 * is safe, or on a failed drive
2618 dev = &sh->dev[sh->pd_idx];
2620 ((test_bit(R5_Insync, &dev->flags) &&
2621 !test_bit(R5_LOCKED, &dev->flags) &&
2622 test_bit(R5_UPTODATE, &dev->flags)) ||
2623 (s.failed == 1 && s.failed_num == sh->pd_idx)))
2624 handle_stripe_clean_event(conf, sh, disks, &return_bi);
2626 /* Now we might consider reading some blocks, either to check/generate
2627 * parity, or to satisfy requests
2628 * or to load a block that is being partially written.
2630 if (s.to_read || s.non_overwrite ||
2631 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
2632 handle_stripe_fill5(sh, &s, disks);
2634 /* Now we check to see if any write operations have recently
2638 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
2640 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2641 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
2642 sh->reconstruct_state = reconstruct_state_idle;
2644 /* All the 'written' buffers and the parity block are ready to
2645 * be written back to disk
2647 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2648 for (i = disks; i--; ) {
2650 if (test_bit(R5_LOCKED, &dev->flags) &&
2651 (i == sh->pd_idx || dev->written)) {
2652 pr_debug("Writing block %d\n", i);
2653 set_bit(R5_Wantwrite, &dev->flags);
2656 if (!test_bit(R5_Insync, &dev->flags) ||
2657 (i == sh->pd_idx && s.failed == 0))
2658 set_bit(STRIPE_INSYNC, &sh->state);
2661 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2662 atomic_dec(&conf->preread_active_stripes);
2663 if (atomic_read(&conf->preread_active_stripes) <
2665 md_wakeup_thread(conf->mddev->thread);
2669 /* Now to consider new write requests and what else, if anything
2670 * should be read. We do not handle new writes when:
2671 * 1/ A 'write' operation (copy+xor) is already in flight.
2672 * 2/ A 'check' operation is in flight, as it may clobber the parity
2675 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
2676 handle_stripe_dirtying5(conf, sh, &s, disks);
2678 /* maybe we need to check and possibly fix the parity for this stripe
2679 * Any reads will already have been scheduled, so we just see if enough
2680 * data is available. The parity check is held off while parity
2681 * dependent operations are in flight.
2683 if (sh->check_state ||
2684 (s.syncing && s.locked == 0 &&
2685 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
2686 !test_bit(STRIPE_INSYNC, &sh->state)))
2687 handle_parity_checks5(conf, sh, &s, disks);
2689 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2690 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2691 clear_bit(STRIPE_SYNCING, &sh->state);
2694 /* If the failed drive is just a ReadError, then we might need to progress
2695 * the repair/check process
2697 if (s.failed == 1 && !conf->mddev->ro &&
2698 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2699 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2700 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
2702 dev = &sh->dev[s.failed_num];
2703 if (!test_bit(R5_ReWrite, &dev->flags)) {
2704 set_bit(R5_Wantwrite, &dev->flags);
2705 set_bit(R5_ReWrite, &dev->flags);
2706 set_bit(R5_LOCKED, &dev->flags);
2709 /* let's read it back */
2710 set_bit(R5_Wantread, &dev->flags);
2711 set_bit(R5_LOCKED, &dev->flags);
2716 /* Finish reconstruct operations initiated by the expansion process */
2717 if (sh->reconstruct_state == reconstruct_state_result) {
2718 sh->reconstruct_state = reconstruct_state_idle;
2719 clear_bit(STRIPE_EXPANDING, &sh->state);
2720 for (i = conf->raid_disks; i--; ) {
2721 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2722 set_bit(R5_LOCKED, &sh->dev[i].flags);
2727 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
2728 !sh->reconstruct_state) {
2729 /* Need to write out all blocks after computing parity */
2730 sh->disks = conf->raid_disks;
2731 sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
2733 schedule_reconstruction5(sh, &s, 1, 1);
2734 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
2735 clear_bit(STRIPE_EXPAND_READY, &sh->state);
2736 atomic_dec(&conf->reshape_stripes);
2737 wake_up(&conf->wait_for_overlap);
2738 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2741 if (s.expanding && s.locked == 0 &&
2742 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
2743 handle_stripe_expansion(conf, sh, NULL);
2746 spin_unlock(&sh->lock);
2748 /* wait for this device to become unblocked */
2749 if (unlikely(blocked_rdev))
2750 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2753 raid5_run_ops(sh, s.ops_request);
2757 return_io(return_bi);
2759 return blocked_rdev == NULL;
2762 static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
2764 raid6_conf_t *conf = sh->raid_conf;
2765 int disks = sh->disks;
2766 struct bio *return_bi = NULL;
2767 int i, pd_idx = sh->pd_idx;
2768 struct stripe_head_state s;
2769 struct r6_state r6s;
2770 struct r5dev *dev, *pdev, *qdev;
2771 mdk_rdev_t *blocked_rdev = NULL;
2773 r6s.qd_idx = raid6_next_disk(pd_idx, disks);
2774 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
2775 "pd_idx=%d, qd_idx=%d\n",
2776 (unsigned long long)sh->sector, sh->state,
2777 atomic_read(&sh->count), pd_idx, r6s.qd_idx);
2778 memset(&s, 0, sizeof(s));
2780 spin_lock(&sh->lock);
2781 clear_bit(STRIPE_HANDLE, &sh->state);
2782 clear_bit(STRIPE_DELAYED, &sh->state);
2784 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2785 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2786 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2787 /* Now to look around and see what can be done */
2790 for (i=disks; i--; ) {
2793 clear_bit(R5_Insync, &dev->flags);
2795 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
2796 i, dev->flags, dev->toread, dev->towrite, dev->written);
2797 /* maybe we can reply to a read */
2798 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
2799 struct bio *rbi, *rbi2;
2800 pr_debug("Return read for disc %d\n", i);
2801 spin_lock_irq(&conf->device_lock);
2804 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2805 wake_up(&conf->wait_for_overlap);
2806 spin_unlock_irq(&conf->device_lock);
2807 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2808 copy_data(0, rbi, dev->page, dev->sector);
2809 rbi2 = r5_next_bio(rbi, dev->sector);
2810 spin_lock_irq(&conf->device_lock);
2811 if (--rbi->bi_phys_segments == 0) {
2812 rbi->bi_next = return_bi;
2815 spin_unlock_irq(&conf->device_lock);
2820 /* now count some things */
2821 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2822 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
2829 if (!test_bit(R5_OVERWRITE, &dev->flags))
2834 rdev = rcu_dereference(conf->disks[i].rdev);
2835 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2836 blocked_rdev = rdev;
2837 atomic_inc(&rdev->nr_pending);
2840 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2841 /* The ReadError flag will just be confusing now */
2842 clear_bit(R5_ReadError, &dev->flags);
2843 clear_bit(R5_ReWrite, &dev->flags);
2845 if (!rdev || !test_bit(In_sync, &rdev->flags)
2846 || test_bit(R5_ReadError, &dev->flags)) {
2848 r6s.failed_num[s.failed] = i;
2851 set_bit(R5_Insync, &dev->flags);
2855 if (unlikely(blocked_rdev)) {
2856 set_bit(STRIPE_HANDLE, &sh->state);
2859 pr_debug("locked=%d uptodate=%d to_read=%d"
2860 " to_write=%d failed=%d failed_num=%d,%d\n",
2861 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
2862 r6s.failed_num[0], r6s.failed_num[1]);
2863 /* check if the array has lost >2 devices and, if so, some requests
2864 * might need to be failed
2866 if (s.failed > 2 && s.to_read+s.to_write+s.written)
2867 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
2868 if (s.failed > 2 && s.syncing) {
2869 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2870 clear_bit(STRIPE_SYNCING, &sh->state);
2875 * might be able to return some write requests if the parity blocks
2876 * are safe, or on a failed drive
2878 pdev = &sh->dev[pd_idx];
2879 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
2880 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
2881 qdev = &sh->dev[r6s.qd_idx];
2882 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
2883 || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
2886 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
2887 && !test_bit(R5_LOCKED, &pdev->flags)
2888 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
2889 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
2890 && !test_bit(R5_LOCKED, &qdev->flags)
2891 && test_bit(R5_UPTODATE, &qdev->flags)))))
2892 handle_stripe_clean_event(conf, sh, disks, &return_bi);
2894 /* Now we might consider reading some blocks, either to check/generate
2895 * parity, or to satisfy requests
2896 * or to load a block that is being partially written.
2898 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
2899 (s.syncing && (s.uptodate < disks)) || s.expanding)
2900 handle_stripe_fill6(sh, &s, &r6s, disks);
2902 /* now to consider writing and what else, if anything should be read */
2904 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
2906 /* maybe we need to check and possibly fix the parity for this stripe
2907 * Any reads will already have been scheduled, so we just see if enough
2910 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
2911 handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
2913 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2914 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2915 clear_bit(STRIPE_SYNCING, &sh->state);
2918 /* If the failed drives are just a ReadError, then we might need
2919 * to progress the repair/check process
2921 if (s.failed <= 2 && !conf->mddev->ro)
2922 for (i = 0; i < s.failed; i++) {
2923 dev = &sh->dev[r6s.failed_num[i]];
2924 if (test_bit(R5_ReadError, &dev->flags)
2925 && !test_bit(R5_LOCKED, &dev->flags)
2926 && test_bit(R5_UPTODATE, &dev->flags)
2928 if (!test_bit(R5_ReWrite, &dev->flags)) {
2929 set_bit(R5_Wantwrite, &dev->flags);
2930 set_bit(R5_ReWrite, &dev->flags);