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/async.h>
51 #include <linux/seq_file.h>
52 #include <linux/cpu.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)))
100 * We maintain a biased count of active stripes in the bottom 16 bits of
101 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
103 static inline int raid5_bi_phys_segments(struct bio *bio)
105 return bio->bi_phys_segments & 0xffff;
108 static inline int raid5_bi_hw_segments(struct bio *bio)
110 return (bio->bi_phys_segments >> 16) & 0xffff;
113 static inline int raid5_dec_bi_phys_segments(struct bio *bio)
115 --bio->bi_phys_segments;
116 return raid5_bi_phys_segments(bio);
119 static inline int raid5_dec_bi_hw_segments(struct bio *bio)
121 unsigned short val = raid5_bi_hw_segments(bio);
124 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
128 static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
130 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
133 /* Find first data disk in a raid6 stripe */
134 static inline int raid6_d0(struct stripe_head *sh)
137 /* ddf always start from first device */
139 /* md starts just after Q block */
140 if (sh->qd_idx == sh->disks - 1)
143 return sh->qd_idx + 1;
145 static inline int raid6_next_disk(int disk, int raid_disks)
148 return (disk < raid_disks) ? disk : 0;
151 /* When walking through the disks in a raid5, starting at raid6_d0,
152 * We need to map each disk to a 'slot', where the data disks are slot
153 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
154 * is raid_disks-1. This help does that mapping.
156 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
157 int *count, int syndrome_disks)
161 if (idx == sh->pd_idx)
162 return syndrome_disks;
163 if (idx == sh->qd_idx)
164 return syndrome_disks + 1;
169 static void return_io(struct bio *return_bi)
171 struct bio *bi = return_bi;
174 return_bi = bi->bi_next;
182 static void print_raid5_conf (raid5_conf_t *conf);
184 static int stripe_operations_active(struct stripe_head *sh)
186 return sh->check_state || sh->reconstruct_state ||
187 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
188 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
191 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
193 if (atomic_dec_and_test(&sh->count)) {
194 BUG_ON(!list_empty(&sh->lru));
195 BUG_ON(atomic_read(&conf->active_stripes)==0);
196 if (test_bit(STRIPE_HANDLE, &sh->state)) {
197 if (test_bit(STRIPE_DELAYED, &sh->state)) {
198 list_add_tail(&sh->lru, &conf->delayed_list);
199 blk_plug_device(conf->mddev->queue);
200 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
201 sh->bm_seq - conf->seq_write > 0) {
202 list_add_tail(&sh->lru, &conf->bitmap_list);
203 blk_plug_device(conf->mddev->queue);
205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
206 list_add_tail(&sh->lru, &conf->handle_list);
208 md_wakeup_thread(conf->mddev->thread);
210 BUG_ON(stripe_operations_active(sh));
211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
216 atomic_dec(&conf->active_stripes);
217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
219 wake_up(&conf->wait_for_stripe);
220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
227 static void release_stripe(struct stripe_head *sh)
229 raid5_conf_t *conf = sh->raid_conf;
232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
237 static inline void remove_hash(struct stripe_head *sh)
239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
242 hlist_del_init(&sh->hash);
245 static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
253 hlist_add_head(&sh->hash, hp);
257 /* find an idle stripe, make sure it is unhashed, and return it. */
258 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
260 struct stripe_head *sh = NULL;
261 struct list_head *first;
264 if (list_empty(&conf->inactive_list))
266 first = conf->inactive_list.next;
267 sh = list_entry(first, struct stripe_head, lru);
268 list_del_init(first);
270 atomic_inc(&conf->active_stripes);
275 static void shrink_buffers(struct stripe_head *sh, int num)
280 for (i=0; i<num ; i++) {
284 sh->dev[i].page = NULL;
289 static int grow_buffers(struct stripe_head *sh, int num)
293 for (i=0; i<num; i++) {
296 if (!(page = alloc_page(GFP_KERNEL))) {
299 sh->dev[i].page = page;
304 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
305 static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
306 struct stripe_head *sh);
308 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
310 raid5_conf_t *conf = sh->raid_conf;
313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
315 BUG_ON(stripe_operations_active(sh));
318 pr_debug("init_stripe called, stripe %llu\n",
319 (unsigned long long)sh->sector);
323 sh->generation = conf->generation - previous;
324 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
326 stripe_set_idx(sector, conf, previous, sh);
330 for (i = sh->disks; i--; ) {
331 struct r5dev *dev = &sh->dev[i];
333 if (dev->toread || dev->read || dev->towrite || dev->written ||
334 test_bit(R5_LOCKED, &dev->flags)) {
335 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
336 (unsigned long long)sh->sector, i, dev->toread,
337 dev->read, dev->towrite, dev->written,
338 test_bit(R5_LOCKED, &dev->flags));
342 raid5_build_block(sh, i, previous);
344 insert_hash(conf, sh);
347 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
350 struct stripe_head *sh;
351 struct hlist_node *hn;
354 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
355 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
356 if (sh->sector == sector && sh->generation == generation)
358 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
362 static void unplug_slaves(mddev_t *mddev);
363 static void raid5_unplug_device(struct request_queue *q);
365 static struct stripe_head *
366 get_active_stripe(raid5_conf_t *conf, sector_t sector,
367 int previous, int noblock, int noquiesce)
369 struct stripe_head *sh;
371 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
373 spin_lock_irq(&conf->device_lock);
376 wait_event_lock_irq(conf->wait_for_stripe,
377 conf->quiesce == 0 || noquiesce,
378 conf->device_lock, /* nothing */);
379 sh = __find_stripe(conf, sector, conf->generation - previous);
381 if (!conf->inactive_blocked)
382 sh = get_free_stripe(conf);
383 if (noblock && sh == NULL)
386 conf->inactive_blocked = 1;
387 wait_event_lock_irq(conf->wait_for_stripe,
388 !list_empty(&conf->inactive_list) &&
389 (atomic_read(&conf->active_stripes)
390 < (conf->max_nr_stripes *3/4)
391 || !conf->inactive_blocked),
393 raid5_unplug_device(conf->mddev->queue)
395 conf->inactive_blocked = 0;
397 init_stripe(sh, sector, previous);
399 if (atomic_read(&sh->count)) {
400 BUG_ON(!list_empty(&sh->lru)
401 && !test_bit(STRIPE_EXPANDING, &sh->state));
403 if (!test_bit(STRIPE_HANDLE, &sh->state))
404 atomic_inc(&conf->active_stripes);
405 if (list_empty(&sh->lru) &&
406 !test_bit(STRIPE_EXPANDING, &sh->state))
408 list_del_init(&sh->lru);
411 } while (sh == NULL);
414 atomic_inc(&sh->count);
416 spin_unlock_irq(&conf->device_lock);
421 raid5_end_read_request(struct bio *bi, int error);
423 raid5_end_write_request(struct bio *bi, int error);
425 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
427 raid5_conf_t *conf = sh->raid_conf;
428 int i, disks = sh->disks;
432 for (i = disks; i--; ) {
436 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
438 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
443 bi = &sh->dev[i].req;
447 bi->bi_end_io = raid5_end_write_request;
449 bi->bi_end_io = raid5_end_read_request;
452 rdev = rcu_dereference(conf->disks[i].rdev);
453 if (rdev && test_bit(Faulty, &rdev->flags))
456 atomic_inc(&rdev->nr_pending);
460 if (s->syncing || s->expanding || s->expanded)
461 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
463 set_bit(STRIPE_IO_STARTED, &sh->state);
465 bi->bi_bdev = rdev->bdev;
466 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
467 __func__, (unsigned long long)sh->sector,
469 atomic_inc(&sh->count);
470 bi->bi_sector = sh->sector + rdev->data_offset;
471 bi->bi_flags = 1 << BIO_UPTODATE;
475 bi->bi_io_vec = &sh->dev[i].vec;
476 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
477 bi->bi_io_vec[0].bv_offset = 0;
478 bi->bi_size = STRIPE_SIZE;
481 test_bit(R5_ReWrite, &sh->dev[i].flags))
482 atomic_add(STRIPE_SECTORS,
483 &rdev->corrected_errors);
484 generic_make_request(bi);
487 set_bit(STRIPE_DEGRADED, &sh->state);
488 pr_debug("skip op %ld on disc %d for sector %llu\n",
489 bi->bi_rw, i, (unsigned long long)sh->sector);
490 clear_bit(R5_LOCKED, &sh->dev[i].flags);
491 set_bit(STRIPE_HANDLE, &sh->state);
496 static struct dma_async_tx_descriptor *
497 async_copy_data(int frombio, struct bio *bio, struct page *page,
498 sector_t sector, struct dma_async_tx_descriptor *tx)
501 struct page *bio_page;
504 struct async_submit_ctl submit;
505 enum async_tx_flags flags = 0;
507 if (bio->bi_sector >= sector)
508 page_offset = (signed)(bio->bi_sector - sector) * 512;
510 page_offset = (signed)(sector - bio->bi_sector) * -512;
513 flags |= ASYNC_TX_FENCE;
514 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
516 bio_for_each_segment(bvl, bio, i) {
517 int len = bio_iovec_idx(bio, i)->bv_len;
521 if (page_offset < 0) {
522 b_offset = -page_offset;
523 page_offset += b_offset;
527 if (len > 0 && page_offset + len > STRIPE_SIZE)
528 clen = STRIPE_SIZE - page_offset;
533 b_offset += bio_iovec_idx(bio, i)->bv_offset;
534 bio_page = bio_iovec_idx(bio, i)->bv_page;
536 tx = async_memcpy(page, bio_page, page_offset,
537 b_offset, clen, &submit);
539 tx = async_memcpy(bio_page, page, b_offset,
540 page_offset, clen, &submit);
542 /* chain the operations */
543 submit.depend_tx = tx;
545 if (clen < len) /* hit end of page */
553 static void ops_complete_biofill(void *stripe_head_ref)
555 struct stripe_head *sh = stripe_head_ref;
556 struct bio *return_bi = NULL;
557 raid5_conf_t *conf = sh->raid_conf;
560 pr_debug("%s: stripe %llu\n", __func__,
561 (unsigned long long)sh->sector);
563 /* clear completed biofills */
564 spin_lock_irq(&conf->device_lock);
565 for (i = sh->disks; i--; ) {
566 struct r5dev *dev = &sh->dev[i];
568 /* acknowledge completion of a biofill operation */
569 /* and check if we need to reply to a read request,
570 * new R5_Wantfill requests are held off until
571 * !STRIPE_BIOFILL_RUN
573 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
574 struct bio *rbi, *rbi2;
579 while (rbi && rbi->bi_sector <
580 dev->sector + STRIPE_SECTORS) {
581 rbi2 = r5_next_bio(rbi, dev->sector);
582 if (!raid5_dec_bi_phys_segments(rbi)) {
583 rbi->bi_next = return_bi;
590 spin_unlock_irq(&conf->device_lock);
591 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
593 return_io(return_bi);
595 set_bit(STRIPE_HANDLE, &sh->state);
599 static void ops_run_biofill(struct stripe_head *sh)
601 struct dma_async_tx_descriptor *tx = NULL;
602 raid5_conf_t *conf = sh->raid_conf;
603 struct async_submit_ctl submit;
606 pr_debug("%s: stripe %llu\n", __func__,
607 (unsigned long long)sh->sector);
609 for (i = sh->disks; i--; ) {
610 struct r5dev *dev = &sh->dev[i];
611 if (test_bit(R5_Wantfill, &dev->flags)) {
613 spin_lock_irq(&conf->device_lock);
614 dev->read = rbi = dev->toread;
616 spin_unlock_irq(&conf->device_lock);
617 while (rbi && rbi->bi_sector <
618 dev->sector + STRIPE_SECTORS) {
619 tx = async_copy_data(0, rbi, dev->page,
621 rbi = r5_next_bio(rbi, dev->sector);
626 atomic_inc(&sh->count);
627 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
628 async_trigger_callback(&submit);
631 static void mark_target_uptodate(struct stripe_head *sh, int target)
638 tgt = &sh->dev[target];
639 set_bit(R5_UPTODATE, &tgt->flags);
640 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
641 clear_bit(R5_Wantcompute, &tgt->flags);
644 static void ops_complete_compute(void *stripe_head_ref)
646 struct stripe_head *sh = stripe_head_ref;
648 pr_debug("%s: stripe %llu\n", __func__,
649 (unsigned long long)sh->sector);
651 /* mark the computed target(s) as uptodate */
652 mark_target_uptodate(sh, sh->ops.target);
653 mark_target_uptodate(sh, sh->ops.target2);
655 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
656 if (sh->check_state == check_state_compute_run)
657 sh->check_state = check_state_compute_result;
658 set_bit(STRIPE_HANDLE, &sh->state);
662 /* return a pointer to the address conversion region of the scribble buffer */
663 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
664 struct raid5_percpu *percpu)
666 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
669 static struct dma_async_tx_descriptor *
670 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
672 int disks = sh->disks;
673 struct page **xor_srcs = percpu->scribble;
674 int target = sh->ops.target;
675 struct r5dev *tgt = &sh->dev[target];
676 struct page *xor_dest = tgt->page;
678 struct dma_async_tx_descriptor *tx;
679 struct async_submit_ctl submit;
682 pr_debug("%s: stripe %llu block: %d\n",
683 __func__, (unsigned long long)sh->sector, target);
684 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
686 for (i = disks; i--; )
688 xor_srcs[count++] = sh->dev[i].page;
690 atomic_inc(&sh->count);
692 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
693 ops_complete_compute, sh, to_addr_conv(sh, percpu));
694 if (unlikely(count == 1))
695 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
697 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
702 /* set_syndrome_sources - populate source buffers for gen_syndrome
703 * @srcs - (struct page *) array of size sh->disks
704 * @sh - stripe_head to parse
706 * Populates srcs in proper layout order for the stripe and returns the
707 * 'count' of sources to be used in a call to async_gen_syndrome. The P
708 * destination buffer is recorded in srcs[count] and the Q destination
709 * is recorded in srcs[count+1]].
711 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
713 int disks = sh->disks;
714 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
715 int d0_idx = raid6_d0(sh);
719 for (i = 0; i < disks; i++)
720 srcs[i] = (void *)raid6_empty_zero_page;
725 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
727 srcs[slot] = sh->dev[i].page;
728 i = raid6_next_disk(i, disks);
729 } while (i != d0_idx);
730 BUG_ON(count != syndrome_disks);
735 static struct dma_async_tx_descriptor *
736 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
738 int disks = sh->disks;
739 struct page **blocks = percpu->scribble;
741 int qd_idx = sh->qd_idx;
742 struct dma_async_tx_descriptor *tx;
743 struct async_submit_ctl submit;
749 if (sh->ops.target < 0)
750 target = sh->ops.target2;
751 else if (sh->ops.target2 < 0)
752 target = sh->ops.target;
754 /* we should only have one valid target */
757 pr_debug("%s: stripe %llu block: %d\n",
758 __func__, (unsigned long long)sh->sector, target);
760 tgt = &sh->dev[target];
761 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
764 atomic_inc(&sh->count);
766 if (target == qd_idx) {
767 count = set_syndrome_sources(blocks, sh);
768 blocks[count] = NULL; /* regenerating p is not necessary */
769 BUG_ON(blocks[count+1] != dest); /* q should already be set */
770 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
771 ops_complete_compute, sh,
772 to_addr_conv(sh, percpu));
773 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
775 /* Compute any data- or p-drive using XOR */
777 for (i = disks; i-- ; ) {
778 if (i == target || i == qd_idx)
780 blocks[count++] = sh->dev[i].page;
783 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
784 NULL, ops_complete_compute, sh,
785 to_addr_conv(sh, percpu));
786 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
792 static struct dma_async_tx_descriptor *
793 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
795 int i, count, disks = sh->disks;
796 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
797 int d0_idx = raid6_d0(sh);
798 int faila = -1, failb = -1;
799 int target = sh->ops.target;
800 int target2 = sh->ops.target2;
801 struct r5dev *tgt = &sh->dev[target];
802 struct r5dev *tgt2 = &sh->dev[target2];
803 struct dma_async_tx_descriptor *tx;
804 struct page **blocks = percpu->scribble;
805 struct async_submit_ctl submit;
807 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
808 __func__, (unsigned long long)sh->sector, target, target2);
809 BUG_ON(target < 0 || target2 < 0);
810 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
811 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
813 /* we need to open-code set_syndrome_sources to handle the
814 * slot number conversion for 'faila' and 'failb'
816 for (i = 0; i < disks ; i++)
817 blocks[i] = (void *)raid6_empty_zero_page;
821 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
823 blocks[slot] = sh->dev[i].page;
829 i = raid6_next_disk(i, disks);
830 } while (i != d0_idx);
831 BUG_ON(count != syndrome_disks);
833 BUG_ON(faila == failb);
836 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
837 __func__, (unsigned long long)sh->sector, faila, failb);
839 atomic_inc(&sh->count);
841 if (failb == syndrome_disks+1) {
842 /* Q disk is one of the missing disks */
843 if (faila == syndrome_disks) {
844 /* Missing P+Q, just recompute */
845 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
846 ops_complete_compute, sh,
847 to_addr_conv(sh, percpu));
848 return async_gen_syndrome(blocks, 0, count+2,
849 STRIPE_SIZE, &submit);
853 int qd_idx = sh->qd_idx;
855 /* Missing D+Q: recompute D from P, then recompute Q */
856 if (target == qd_idx)
857 data_target = target2;
859 data_target = target;
862 for (i = disks; i-- ; ) {
863 if (i == data_target || i == qd_idx)
865 blocks[count++] = sh->dev[i].page;
867 dest = sh->dev[data_target].page;
868 init_async_submit(&submit,
869 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
871 to_addr_conv(sh, percpu));
872 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
875 count = set_syndrome_sources(blocks, sh);
876 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
877 ops_complete_compute, sh,
878 to_addr_conv(sh, percpu));
879 return async_gen_syndrome(blocks, 0, count+2,
880 STRIPE_SIZE, &submit);
883 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
884 ops_complete_compute, sh,
885 to_addr_conv(sh, percpu));
886 if (failb == syndrome_disks) {
887 /* We're missing D+P. */
888 return async_raid6_datap_recov(syndrome_disks+2,
892 /* We're missing D+D. */
893 return async_raid6_2data_recov(syndrome_disks+2,
894 STRIPE_SIZE, faila, failb,
901 static void ops_complete_prexor(void *stripe_head_ref)
903 struct stripe_head *sh = stripe_head_ref;
905 pr_debug("%s: stripe %llu\n", __func__,
906 (unsigned long long)sh->sector);
909 static struct dma_async_tx_descriptor *
910 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
911 struct dma_async_tx_descriptor *tx)
913 int disks = sh->disks;
914 struct page **xor_srcs = percpu->scribble;
915 int count = 0, pd_idx = sh->pd_idx, i;
916 struct async_submit_ctl submit;
918 /* existing parity data subtracted */
919 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
921 pr_debug("%s: stripe %llu\n", __func__,
922 (unsigned long long)sh->sector);
924 for (i = disks; i--; ) {
925 struct r5dev *dev = &sh->dev[i];
926 /* Only process blocks that are known to be uptodate */
927 if (test_bit(R5_Wantdrain, &dev->flags))
928 xor_srcs[count++] = dev->page;
931 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
932 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
933 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
938 static struct dma_async_tx_descriptor *
939 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
941 int disks = sh->disks;
944 pr_debug("%s: stripe %llu\n", __func__,
945 (unsigned long long)sh->sector);
947 for (i = disks; i--; ) {
948 struct r5dev *dev = &sh->dev[i];
951 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
954 spin_lock(&sh->lock);
955 chosen = dev->towrite;
957 BUG_ON(dev->written);
958 wbi = dev->written = chosen;
959 spin_unlock(&sh->lock);
961 while (wbi && wbi->bi_sector <
962 dev->sector + STRIPE_SECTORS) {
963 tx = async_copy_data(1, wbi, dev->page,
965 wbi = r5_next_bio(wbi, dev->sector);
973 static void ops_complete_reconstruct(void *stripe_head_ref)
975 struct stripe_head *sh = stripe_head_ref;
976 int disks = sh->disks;
977 int pd_idx = sh->pd_idx;
978 int qd_idx = sh->qd_idx;
981 pr_debug("%s: stripe %llu\n", __func__,
982 (unsigned long long)sh->sector);
984 for (i = disks; i--; ) {
985 struct r5dev *dev = &sh->dev[i];
987 if (dev->written || i == pd_idx || i == qd_idx)
988 set_bit(R5_UPTODATE, &dev->flags);
991 if (sh->reconstruct_state == reconstruct_state_drain_run)
992 sh->reconstruct_state = reconstruct_state_drain_result;
993 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
994 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
996 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
997 sh->reconstruct_state = reconstruct_state_result;
1000 set_bit(STRIPE_HANDLE, &sh->state);
1005 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1006 struct dma_async_tx_descriptor *tx)
1008 int disks = sh->disks;
1009 struct page **xor_srcs = percpu->scribble;
1010 struct async_submit_ctl submit;
1011 int count = 0, pd_idx = sh->pd_idx, i;
1012 struct page *xor_dest;
1014 unsigned long flags;
1016 pr_debug("%s: stripe %llu\n", __func__,
1017 (unsigned long long)sh->sector);
1019 /* check if prexor is active which means only process blocks
1020 * that are part of a read-modify-write (written)
1022 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1024 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1025 for (i = disks; i--; ) {
1026 struct r5dev *dev = &sh->dev[i];
1028 xor_srcs[count++] = dev->page;
1031 xor_dest = sh->dev[pd_idx].page;
1032 for (i = disks; i--; ) {
1033 struct r5dev *dev = &sh->dev[i];
1035 xor_srcs[count++] = dev->page;
1039 /* 1/ if we prexor'd then the dest is reused as a source
1040 * 2/ if we did not prexor then we are redoing the parity
1041 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1042 * for the synchronous xor case
1044 flags = ASYNC_TX_ACK |
1045 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1047 atomic_inc(&sh->count);
1049 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1050 to_addr_conv(sh, percpu));
1051 if (unlikely(count == 1))
1052 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1054 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1058 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1059 struct dma_async_tx_descriptor *tx)
1061 struct async_submit_ctl submit;
1062 struct page **blocks = percpu->scribble;
1065 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1067 count = set_syndrome_sources(blocks, sh);
1069 atomic_inc(&sh->count);
1071 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1072 sh, to_addr_conv(sh, percpu));
1073 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1076 static void ops_complete_check(void *stripe_head_ref)
1078 struct stripe_head *sh = stripe_head_ref;
1080 pr_debug("%s: stripe %llu\n", __func__,
1081 (unsigned long long)sh->sector);
1083 sh->check_state = check_state_check_result;
1084 set_bit(STRIPE_HANDLE, &sh->state);
1088 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1090 int disks = sh->disks;
1091 int pd_idx = sh->pd_idx;
1092 int qd_idx = sh->qd_idx;
1093 struct page *xor_dest;
1094 struct page **xor_srcs = percpu->scribble;
1095 struct dma_async_tx_descriptor *tx;
1096 struct async_submit_ctl submit;
1100 pr_debug("%s: stripe %llu\n", __func__,
1101 (unsigned long long)sh->sector);
1104 xor_dest = sh->dev[pd_idx].page;
1105 xor_srcs[count++] = xor_dest;
1106 for (i = disks; i--; ) {
1107 if (i == pd_idx || i == qd_idx)
1109 xor_srcs[count++] = sh->dev[i].page;
1112 init_async_submit(&submit, 0, NULL, NULL, NULL,
1113 to_addr_conv(sh, percpu));
1114 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1115 &sh->ops.zero_sum_result, &submit);
1117 atomic_inc(&sh->count);
1118 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1119 tx = async_trigger_callback(&submit);
1122 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1124 struct page **srcs = percpu->scribble;
1125 struct async_submit_ctl submit;
1128 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1129 (unsigned long long)sh->sector, checkp);
1131 count = set_syndrome_sources(srcs, sh);
1135 atomic_inc(&sh->count);
1136 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1137 sh, to_addr_conv(sh, percpu));
1138 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1139 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1142 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1144 int overlap_clear = 0, i, disks = sh->disks;
1145 struct dma_async_tx_descriptor *tx = NULL;
1146 raid5_conf_t *conf = sh->raid_conf;
1147 int level = conf->level;
1148 struct raid5_percpu *percpu;
1152 percpu = per_cpu_ptr(conf->percpu, cpu);
1153 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1154 ops_run_biofill(sh);
1158 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1160 tx = ops_run_compute5(sh, percpu);
1162 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1163 tx = ops_run_compute6_1(sh, percpu);
1165 tx = ops_run_compute6_2(sh, percpu);
1167 /* terminate the chain if reconstruct is not set to be run */
1168 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1172 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1173 tx = ops_run_prexor(sh, percpu, tx);
1175 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1176 tx = ops_run_biodrain(sh, tx);
1180 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1182 ops_run_reconstruct5(sh, percpu, tx);
1184 ops_run_reconstruct6(sh, percpu, tx);
1187 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1188 if (sh->check_state == check_state_run)
1189 ops_run_check_p(sh, percpu);
1190 else if (sh->check_state == check_state_run_q)
1191 ops_run_check_pq(sh, percpu, 0);
1192 else if (sh->check_state == check_state_run_pq)
1193 ops_run_check_pq(sh, percpu, 1);
1199 for (i = disks; i--; ) {
1200 struct r5dev *dev = &sh->dev[i];
1201 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1202 wake_up(&sh->raid_conf->wait_for_overlap);
1207 static int grow_one_stripe(raid5_conf_t *conf)
1209 struct stripe_head *sh;
1210 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1213 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
1214 sh->raid_conf = conf;
1215 spin_lock_init(&sh->lock);
1217 if (grow_buffers(sh, conf->raid_disks)) {
1218 shrink_buffers(sh, conf->raid_disks);
1219 kmem_cache_free(conf->slab_cache, sh);
1222 sh->disks = conf->raid_disks;
1223 /* we just created an active stripe so... */
1224 atomic_set(&sh->count, 1);
1225 atomic_inc(&conf->active_stripes);
1226 INIT_LIST_HEAD(&sh->lru);
1231 static int grow_stripes(raid5_conf_t *conf, int num)
1233 struct kmem_cache *sc;
1234 int devs = conf->raid_disks;
1236 sprintf(conf->cache_name[0],
1237 "raid%d-%s", conf->level, mdname(conf->mddev));
1238 sprintf(conf->cache_name[1],
1239 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
1240 conf->active_name = 0;
1241 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1242 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1246 conf->slab_cache = sc;
1247 conf->pool_size = devs;
1249 if (!grow_one_stripe(conf))
1255 * scribble_len - return the required size of the scribble region
1256 * @num - total number of disks in the array
1258 * The size must be enough to contain:
1259 * 1/ a struct page pointer for each device in the array +2
1260 * 2/ room to convert each entry in (1) to its corresponding dma
1261 * (dma_map_page()) or page (page_address()) address.
1263 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1264 * calculate over all devices (not just the data blocks), using zeros in place
1265 * of the P and Q blocks.
1267 static size_t scribble_len(int num)
1271 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1276 static int resize_stripes(raid5_conf_t *conf, int newsize)
1278 /* Make all the stripes able to hold 'newsize' devices.
1279 * New slots in each stripe get 'page' set to a new page.
1281 * This happens in stages:
1282 * 1/ create a new kmem_cache and allocate the required number of
1284 * 2/ gather all the old stripe_heads and tranfer the pages across
1285 * to the new stripe_heads. This will have the side effect of
1286 * freezing the array as once all stripe_heads have been collected,
1287 * no IO will be possible. Old stripe heads are freed once their
1288 * pages have been transferred over, and the old kmem_cache is
1289 * freed when all stripes are done.
1290 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1291 * we simple return a failre status - no need to clean anything up.
1292 * 4/ allocate new pages for the new slots in the new stripe_heads.
1293 * If this fails, we don't bother trying the shrink the
1294 * stripe_heads down again, we just leave them as they are.
1295 * As each stripe_head is processed the new one is released into
1298 * Once step2 is started, we cannot afford to wait for a write,
1299 * so we use GFP_NOIO allocations.
1301 struct stripe_head *osh, *nsh;
1302 LIST_HEAD(newstripes);
1303 struct disk_info *ndisks;
1306 struct kmem_cache *sc;
1309 if (newsize <= conf->pool_size)
1310 return 0; /* never bother to shrink */
1312 err = md_allow_write(conf->mddev);
1317 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1318 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1323 for (i = conf->max_nr_stripes; i; i--) {
1324 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1328 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1330 nsh->raid_conf = conf;
1331 spin_lock_init(&nsh->lock);
1333 list_add(&nsh->lru, &newstripes);
1336 /* didn't get enough, give up */
1337 while (!list_empty(&newstripes)) {
1338 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1339 list_del(&nsh->lru);
1340 kmem_cache_free(sc, nsh);
1342 kmem_cache_destroy(sc);
1345 /* Step 2 - Must use GFP_NOIO now.
1346 * OK, we have enough stripes, start collecting inactive
1347 * stripes and copying them over
1349 list_for_each_entry(nsh, &newstripes, lru) {
1350 spin_lock_irq(&conf->device_lock);
1351 wait_event_lock_irq(conf->wait_for_stripe,
1352 !list_empty(&conf->inactive_list),
1354 unplug_slaves(conf->mddev)
1356 osh = get_free_stripe(conf);
1357 spin_unlock_irq(&conf->device_lock);
1358 atomic_set(&nsh->count, 1);
1359 for(i=0; i<conf->pool_size; i++)
1360 nsh->dev[i].page = osh->dev[i].page;
1361 for( ; i<newsize; i++)
1362 nsh->dev[i].page = NULL;
1363 kmem_cache_free(conf->slab_cache, osh);
1365 kmem_cache_destroy(conf->slab_cache);
1368 * At this point, we are holding all the stripes so the array
1369 * is completely stalled, so now is a good time to resize
1370 * conf->disks and the scribble region
1372 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1374 for (i=0; i<conf->raid_disks; i++)
1375 ndisks[i] = conf->disks[i];
1377 conf->disks = ndisks;
1382 conf->scribble_len = scribble_len(newsize);
1383 for_each_present_cpu(cpu) {
1384 struct raid5_percpu *percpu;
1387 percpu = per_cpu_ptr(conf->percpu, cpu);
1388 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1391 kfree(percpu->scribble);
1392 percpu->scribble = scribble;
1400 /* Step 4, return new stripes to service */
1401 while(!list_empty(&newstripes)) {
1402 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1403 list_del_init(&nsh->lru);
1405 for (i=conf->raid_disks; i < newsize; i++)
1406 if (nsh->dev[i].page == NULL) {
1407 struct page *p = alloc_page(GFP_NOIO);
1408 nsh->dev[i].page = p;
1412 release_stripe(nsh);
1414 /* critical section pass, GFP_NOIO no longer needed */
1416 conf->slab_cache = sc;
1417 conf->active_name = 1-conf->active_name;
1418 conf->pool_size = newsize;
1422 static int drop_one_stripe(raid5_conf_t *conf)
1424 struct stripe_head *sh;
1426 spin_lock_irq(&conf->device_lock);
1427 sh = get_free_stripe(conf);
1428 spin_unlock_irq(&conf->device_lock);
1431 BUG_ON(atomic_read(&sh->count));
1432 shrink_buffers(sh, conf->pool_size);
1433 kmem_cache_free(conf->slab_cache, sh);
1434 atomic_dec(&conf->active_stripes);
1438 static void shrink_stripes(raid5_conf_t *conf)
1440 while (drop_one_stripe(conf))
1443 if (conf->slab_cache)
1444 kmem_cache_destroy(conf->slab_cache);
1445 conf->slab_cache = NULL;
1448 static void raid5_end_read_request(struct bio * bi, int error)
1450 struct stripe_head *sh = bi->bi_private;
1451 raid5_conf_t *conf = sh->raid_conf;
1452 int disks = sh->disks, i;
1453 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1454 char b[BDEVNAME_SIZE];
1458 for (i=0 ; i<disks; i++)
1459 if (bi == &sh->dev[i].req)
1462 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1463 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1471 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1472 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1473 rdev = conf->disks[i].rdev;
1474 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1475 " (%lu sectors at %llu on %s)\n",
1476 mdname(conf->mddev), STRIPE_SECTORS,
1477 (unsigned long long)(sh->sector
1478 + rdev->data_offset),
1479 bdevname(rdev->bdev, b));
1480 clear_bit(R5_ReadError, &sh->dev[i].flags);
1481 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1483 if (atomic_read(&conf->disks[i].rdev->read_errors))
1484 atomic_set(&conf->disks[i].rdev->read_errors, 0);
1486 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
1488 rdev = conf->disks[i].rdev;
1490 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1491 atomic_inc(&rdev->read_errors);
1492 if (conf->mddev->degraded)
1493 printk_rl(KERN_WARNING
1494 "raid5:%s: read error not correctable "
1495 "(sector %llu on %s).\n",
1496 mdname(conf->mddev),
1497 (unsigned long long)(sh->sector
1498 + rdev->data_offset),
1500 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
1502 printk_rl(KERN_WARNING
1503 "raid5:%s: read error NOT corrected!! "
1504 "(sector %llu on %s).\n",
1505 mdname(conf->mddev),
1506 (unsigned long long)(sh->sector
1507 + rdev->data_offset),
1509 else if (atomic_read(&rdev->read_errors)
1510 > conf->max_nr_stripes)
1512 "raid5:%s: Too many read errors, failing device %s.\n",
1513 mdname(conf->mddev), bdn);
1517 set_bit(R5_ReadError, &sh->dev[i].flags);
1519 clear_bit(R5_ReadError, &sh->dev[i].flags);
1520 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1521 md_error(conf->mddev, rdev);
1524 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1525 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1526 set_bit(STRIPE_HANDLE, &sh->state);
1530 static void raid5_end_write_request(struct bio *bi, int error)
1532 struct stripe_head *sh = bi->bi_private;
1533 raid5_conf_t *conf = sh->raid_conf;
1534 int disks = sh->disks, i;
1535 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1537 for (i=0 ; i<disks; i++)
1538 if (bi == &sh->dev[i].req)
1541 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1542 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1550 md_error(conf->mddev, conf->disks[i].rdev);
1552 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1554 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1555 set_bit(STRIPE_HANDLE, &sh->state);
1560 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1562 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1564 struct r5dev *dev = &sh->dev[i];
1566 bio_init(&dev->req);
1567 dev->req.bi_io_vec = &dev->vec;
1569 dev->req.bi_max_vecs++;
1570 dev->vec.bv_page = dev->page;
1571 dev->vec.bv_len = STRIPE_SIZE;
1572 dev->vec.bv_offset = 0;
1574 dev->req.bi_sector = sh->sector;
1575 dev->req.bi_private = sh;
1578 dev->sector = compute_blocknr(sh, i, previous);
1581 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1583 char b[BDEVNAME_SIZE];
1584 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1585 pr_debug("raid5: error called\n");
1587 if (!test_bit(Faulty, &rdev->flags)) {
1588 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1589 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1590 unsigned long flags;
1591 spin_lock_irqsave(&conf->device_lock, flags);
1593 spin_unlock_irqrestore(&conf->device_lock, flags);
1595 * if recovery was running, make sure it aborts.
1597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1599 set_bit(Faulty, &rdev->flags);
1601 "raid5: Disk failure on %s, disabling device.\n"
1602 "raid5: Operation continuing on %d devices.\n",
1603 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1608 * Input: a 'big' sector number,
1609 * Output: index of the data and parity disk, and the sector # in them.
1611 static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
1612 int previous, int *dd_idx,
1613 struct stripe_head *sh)
1616 unsigned long chunk_number;
1617 unsigned int chunk_offset;
1620 sector_t new_sector;
1621 int algorithm = previous ? conf->prev_algo
1623 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1624 : conf->chunk_sectors;
1625 int raid_disks = previous ? conf->previous_raid_disks
1627 int data_disks = raid_disks - conf->max_degraded;
1629 /* First compute the information on this sector */
1632 * Compute the chunk number and the sector offset inside the chunk
1634 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1635 chunk_number = r_sector;
1636 BUG_ON(r_sector != chunk_number);
1639 * Compute the stripe number
1641 stripe = chunk_number / data_disks;
1644 * Compute the data disk and parity disk indexes inside the stripe
1646 *dd_idx = chunk_number % data_disks;
1649 * Select the parity disk based on the user selected algorithm.
1651 pd_idx = qd_idx = ~0;
1652 switch(conf->level) {
1654 pd_idx = data_disks;
1657 switch (algorithm) {
1658 case ALGORITHM_LEFT_ASYMMETRIC:
1659 pd_idx = data_disks - stripe % raid_disks;
1660 if (*dd_idx >= pd_idx)
1663 case ALGORITHM_RIGHT_ASYMMETRIC:
1664 pd_idx = stripe % raid_disks;
1665 if (*dd_idx >= pd_idx)
1668 case ALGORITHM_LEFT_SYMMETRIC:
1669 pd_idx = data_disks - stripe % raid_disks;
1670 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1672 case ALGORITHM_RIGHT_SYMMETRIC:
1673 pd_idx = stripe % raid_disks;
1674 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1676 case ALGORITHM_PARITY_0:
1680 case ALGORITHM_PARITY_N:
1681 pd_idx = data_disks;
1684 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1691 switch (algorithm) {
1692 case ALGORITHM_LEFT_ASYMMETRIC:
1693 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1694 qd_idx = pd_idx + 1;
1695 if (pd_idx == raid_disks-1) {
1696 (*dd_idx)++; /* Q D D D P */
1698 } else if (*dd_idx >= pd_idx)
1699 (*dd_idx) += 2; /* D D P Q D */
1701 case ALGORITHM_RIGHT_ASYMMETRIC:
1702 pd_idx = stripe % raid_disks;
1703 qd_idx = pd_idx + 1;
1704 if (pd_idx == raid_disks-1) {
1705 (*dd_idx)++; /* Q D D D P */
1707 } else if (*dd_idx >= pd_idx)
1708 (*dd_idx) += 2; /* D D P Q D */
1710 case ALGORITHM_LEFT_SYMMETRIC:
1711 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1712 qd_idx = (pd_idx + 1) % raid_disks;
1713 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
1715 case ALGORITHM_RIGHT_SYMMETRIC:
1716 pd_idx = stripe % raid_disks;
1717 qd_idx = (pd_idx + 1) % raid_disks;
1718 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
1721 case ALGORITHM_PARITY_0:
1726 case ALGORITHM_PARITY_N:
1727 pd_idx = data_disks;
1728 qd_idx = data_disks + 1;
1731 case ALGORITHM_ROTATING_ZERO_RESTART:
1732 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1733 * of blocks for computing Q is different.
1735 pd_idx = stripe % raid_disks;
1736 qd_idx = pd_idx + 1;
1737 if (pd_idx == raid_disks-1) {
1738 (*dd_idx)++; /* Q D D D P */
1740 } else if (*dd_idx >= pd_idx)
1741 (*dd_idx) += 2; /* D D P Q D */
1745 case ALGORITHM_ROTATING_N_RESTART:
1746 /* Same a left_asymmetric, by first stripe is
1747 * D D D P Q rather than
1750 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1751 qd_idx = pd_idx + 1;
1752 if (pd_idx == raid_disks-1) {
1753 (*dd_idx)++; /* Q D D D P */
1755 } else if (*dd_idx >= pd_idx)
1756 (*dd_idx) += 2; /* D D P Q D */
1760 case ALGORITHM_ROTATING_N_CONTINUE:
1761 /* Same as left_symmetric but Q is before P */
1762 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1763 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1764 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1768 case ALGORITHM_LEFT_ASYMMETRIC_6:
1769 /* RAID5 left_asymmetric, with Q on last device */
1770 pd_idx = data_disks - stripe % (raid_disks-1);
1771 if (*dd_idx >= pd_idx)
1773 qd_idx = raid_disks - 1;
1776 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1777 pd_idx = stripe % (raid_disks-1);
1778 if (*dd_idx >= pd_idx)
1780 qd_idx = raid_disks - 1;
1783 case ALGORITHM_LEFT_SYMMETRIC_6:
1784 pd_idx = data_disks - stripe % (raid_disks-1);
1785 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1786 qd_idx = raid_disks - 1;
1789 case ALGORITHM_RIGHT_SYMMETRIC_6:
1790 pd_idx = stripe % (raid_disks-1);
1791 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1792 qd_idx = raid_disks - 1;
1795 case ALGORITHM_PARITY_0_6:
1798 qd_idx = raid_disks - 1;
1803 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1811 sh->pd_idx = pd_idx;
1812 sh->qd_idx = qd_idx;
1813 sh->ddf_layout = ddf_layout;
1816 * Finally, compute the new sector number
1818 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1823 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
1825 raid5_conf_t *conf = sh->raid_conf;
1826 int raid_disks = sh->disks;
1827 int data_disks = raid_disks - conf->max_degraded;
1828 sector_t new_sector = sh->sector, check;
1829 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1830 : conf->chunk_sectors;
1831 int algorithm = previous ? conf->prev_algo
1835 int chunk_number, dummy1, dd_idx = i;
1837 struct stripe_head sh2;
1840 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1841 stripe = new_sector;
1842 BUG_ON(new_sector != stripe);
1844 if (i == sh->pd_idx)
1846 switch(conf->level) {
1849 switch (algorithm) {
1850 case ALGORITHM_LEFT_ASYMMETRIC:
1851 case ALGORITHM_RIGHT_ASYMMETRIC:
1855 case ALGORITHM_LEFT_SYMMETRIC:
1856 case ALGORITHM_RIGHT_SYMMETRIC:
1859 i -= (sh->pd_idx + 1);
1861 case ALGORITHM_PARITY_0:
1864 case ALGORITHM_PARITY_N:
1867 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1873 if (i == sh->qd_idx)
1874 return 0; /* It is the Q disk */
1875 switch (algorithm) {
1876 case ALGORITHM_LEFT_ASYMMETRIC:
1877 case ALGORITHM_RIGHT_ASYMMETRIC:
1878 case ALGORITHM_ROTATING_ZERO_RESTART:
1879 case ALGORITHM_ROTATING_N_RESTART:
1880 if (sh->pd_idx == raid_disks-1)
1881 i--; /* Q D D D P */
1882 else if (i > sh->pd_idx)
1883 i -= 2; /* D D P Q D */
1885 case ALGORITHM_LEFT_SYMMETRIC:
1886 case ALGORITHM_RIGHT_SYMMETRIC:
1887 if (sh->pd_idx == raid_disks-1)
1888 i--; /* Q D D D P */
1893 i -= (sh->pd_idx + 2);
1896 case ALGORITHM_PARITY_0:
1899 case ALGORITHM_PARITY_N:
1901 case ALGORITHM_ROTATING_N_CONTINUE:
1902 if (sh->pd_idx == 0)
1903 i--; /* P D D D Q */
1904 else if (i > sh->pd_idx)
1905 i -= 2; /* D D Q P D */
1907 case ALGORITHM_LEFT_ASYMMETRIC_6:
1908 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1912 case ALGORITHM_LEFT_SYMMETRIC_6:
1913 case ALGORITHM_RIGHT_SYMMETRIC_6:
1915 i += data_disks + 1;
1916 i -= (sh->pd_idx + 1);
1918 case ALGORITHM_PARITY_0_6:
1922 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1929 chunk_number = stripe * data_disks + i;
1930 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1932 check = raid5_compute_sector(conf, r_sector,
1933 previous, &dummy1, &sh2);
1934 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1935 || sh2.qd_idx != sh->qd_idx) {
1936 printk(KERN_ERR "compute_blocknr: map not correct\n");
1944 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
1945 int rcw, int expand)
1947 int i, pd_idx = sh->pd_idx, disks = sh->disks;
1948 raid5_conf_t *conf = sh->raid_conf;
1949 int level = conf->level;
1952 /* if we are not expanding this is a proper write request, and
1953 * there will be bios with new data to be drained into the
1957 sh->reconstruct_state = reconstruct_state_drain_run;
1958 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1960 sh->reconstruct_state = reconstruct_state_run;
1962 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
1964 for (i = disks; i--; ) {
1965 struct r5dev *dev = &sh->dev[i];
1968 set_bit(R5_LOCKED, &dev->flags);
1969 set_bit(R5_Wantdrain, &dev->flags);
1971 clear_bit(R5_UPTODATE, &dev->flags);
1975 if (s->locked + conf->max_degraded == disks)
1976 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1977 atomic_inc(&conf->pending_full_writes);
1980 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1981 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1983 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
1984 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1985 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1986 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
1988 for (i = disks; i--; ) {
1989 struct r5dev *dev = &sh->dev[i];
1994 (test_bit(R5_UPTODATE, &dev->flags) ||
1995 test_bit(R5_Wantcompute, &dev->flags))) {
1996 set_bit(R5_Wantdrain, &dev->flags);
1997 set_bit(R5_LOCKED, &dev->flags);
1998 clear_bit(R5_UPTODATE, &dev->flags);
2004 /* keep the parity disk(s) locked while asynchronous operations
2007 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2008 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2012 int qd_idx = sh->qd_idx;
2013 struct r5dev *dev = &sh->dev[qd_idx];
2015 set_bit(R5_LOCKED, &dev->flags);
2016 clear_bit(R5_UPTODATE, &dev->flags);
2020 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2021 __func__, (unsigned long long)sh->sector,
2022 s->locked, s->ops_request);
2026 * Each stripe/dev can have one or more bion attached.
2027 * toread/towrite point to the first in a chain.
2028 * The bi_next chain must be in order.
2030 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2033 raid5_conf_t *conf = sh->raid_conf;
2036 pr_debug("adding bh b#%llu to stripe s#%llu\n",
2037 (unsigned long long)bi->bi_sector,
2038 (unsigned long long)sh->sector);
2041 spin_lock(&sh->lock);
2042 spin_lock_irq(&conf->device_lock);
2044 bip = &sh->dev[dd_idx].towrite;
2045 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2048 bip = &sh->dev[dd_idx].toread;
2049 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2050 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2052 bip = & (*bip)->bi_next;
2054 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2057 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2061 bi->bi_phys_segments++;
2062 spin_unlock_irq(&conf->device_lock);
2063 spin_unlock(&sh->lock);
2065 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2066 (unsigned long long)bi->bi_sector,
2067 (unsigned long long)sh->sector, dd_idx);
2069 if (conf->mddev->bitmap && firstwrite) {
2070 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2072 sh->bm_seq = conf->seq_flush+1;
2073 set_bit(STRIPE_BIT_DELAY, &sh->state);
2077 /* check if page is covered */
2078 sector_t sector = sh->dev[dd_idx].sector;
2079 for (bi=sh->dev[dd_idx].towrite;
2080 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2081 bi && bi->bi_sector <= sector;
2082 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2083 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2084 sector = bi->bi_sector + (bi->bi_size>>9);
2086 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2087 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2092 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2093 spin_unlock_irq(&conf->device_lock);
2094 spin_unlock(&sh->lock);
2098 static void end_reshape(raid5_conf_t *conf);
2100 static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2101 struct stripe_head *sh)
2103 int sectors_per_chunk =
2104 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2106 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2107 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2109 raid5_compute_sector(conf,
2110 stripe * (disks - conf->max_degraded)
2111 *sectors_per_chunk + chunk_offset,
2117 handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
2118 struct stripe_head_state *s, int disks,
2119 struct bio **return_bi)
2122 for (i = disks; i--; ) {
2126 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2129 rdev = rcu_dereference(conf->disks[i].rdev);
2130 if (rdev && test_bit(In_sync, &rdev->flags))
2131 /* multiple read failures in one stripe */
2132 md_error(conf->mddev, rdev);
2135 spin_lock_irq(&conf->device_lock);
2136 /* fail all writes first */
2137 bi = sh->dev[i].towrite;
2138 sh->dev[i].towrite = NULL;
2144 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2145 wake_up(&conf->wait_for_overlap);
2147 while (bi && bi->bi_sector <
2148 sh->dev[i].sector + STRIPE_SECTORS) {
2149 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2150 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2151 if (!raid5_dec_bi_phys_segments(bi)) {
2152 md_write_end(conf->mddev);
2153 bi->bi_next = *return_bi;
2158 /* and fail all 'written' */
2159 bi = sh->dev[i].written;
2160 sh->dev[i].written = NULL;
2161 if (bi) bitmap_end = 1;
2162 while (bi && bi->bi_sector <
2163 sh->dev[i].sector + STRIPE_SECTORS) {
2164 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2165 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2166 if (!raid5_dec_bi_phys_segments(bi)) {
2167 md_write_end(conf->mddev);
2168 bi->bi_next = *return_bi;
2174 /* fail any reads if this device is non-operational and
2175 * the data has not reached the cache yet.
2177 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2178 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2179 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2180 bi = sh->dev[i].toread;
2181 sh->dev[i].toread = NULL;
2182 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2183 wake_up(&conf->wait_for_overlap);
2184 if (bi) s->to_read--;
2185 while (bi && bi->bi_sector <
2186 sh->dev[i].sector + STRIPE_SECTORS) {
2187 struct bio *nextbi =
2188 r5_next_bio(bi, sh->dev[i].sector);
2189 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2190 if (!raid5_dec_bi_phys_segments(bi)) {
2191 bi->bi_next = *return_bi;
2197 spin_unlock_irq(&conf->device_lock);
2199 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2200 STRIPE_SECTORS, 0, 0);
2203 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2204 if (atomic_dec_and_test(&conf->pending_full_writes))
2205 md_wakeup_thread(conf->mddev->thread);
2208 /* fetch_block5 - checks the given member device to see if its data needs
2209 * to be read or computed to satisfy a request.
2211 * Returns 1 when no more member devices need to be checked, otherwise returns
2212 * 0 to tell the loop in handle_stripe_fill5 to continue
2214 static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2215 int disk_idx, int disks)
2217 struct r5dev *dev = &sh->dev[disk_idx];
2218 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2220 /* is the data in this block needed, and can we get it? */
2221 if (!test_bit(R5_LOCKED, &dev->flags) &&
2222 !test_bit(R5_UPTODATE, &dev->flags) &&
2224 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2225 s->syncing || s->expanding ||
2227 (failed_dev->toread ||
2228 (failed_dev->towrite &&
2229 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
2230 /* We would like to get this block, possibly by computing it,
2231 * otherwise read it if the backing disk is insync
2233 if ((s->uptodate == disks - 1) &&
2234 (s->failed && disk_idx == s->failed_num)) {
2235 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2236 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2237 set_bit(R5_Wantcompute, &dev->flags);
2238 sh->ops.target = disk_idx;
2239 sh->ops.target2 = -1;
2241 /* Careful: from this point on 'uptodate' is in the eye
2242 * of raid_run_ops which services 'compute' operations
2243 * before writes. R5_Wantcompute flags a block that will
2244 * be R5_UPTODATE by the time it is needed for a
2245 * subsequent operation.
2248 return 1; /* uptodate + compute == disks */
2249 } else if (test_bit(R5_Insync, &dev->flags)) {
2250 set_bit(R5_LOCKED, &dev->flags);
2251 set_bit(R5_Wantread, &dev->flags);
2253 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2262 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2264 static void handle_stripe_fill5(struct stripe_head *sh,
2265 struct stripe_head_state *s, int disks)
2269 /* look for blocks to read/compute, skip this if a compute
2270 * is already in flight, or if the stripe contents are in the
2271 * midst of changing due to a write
2273 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2274 !sh->reconstruct_state)
2275 for (i = disks; i--; )
2276 if (fetch_block5(sh, s, i, disks))
2278 set_bit(STRIPE_HANDLE, &sh->state);
2281 /* fetch_block6 - checks the given member device to see if its data needs
2282 * to be read or computed to satisfy a request.
2284 * Returns 1 when no more member devices need to be checked, otherwise returns
2285 * 0 to tell the loop in handle_stripe_fill6 to continue
2287 static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2288 struct r6_state *r6s, int disk_idx, int disks)
2290 struct r5dev *dev = &sh->dev[disk_idx];
2291 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2292 &sh->dev[r6s->failed_num[1]] };
2294 if (!test_bit(R5_LOCKED, &dev->flags) &&
2295 !test_bit(R5_UPTODATE, &dev->flags) &&
2297 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2298 s->syncing || s->expanding ||
2300 (fdev[0]->toread || s->to_write)) ||
2302 (fdev[1]->toread || s->to_write)))) {
2303 /* we would like to get this block, possibly by computing it,
2304 * otherwise read it if the backing disk is insync
2306 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2307 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2308 if ((s->uptodate == disks - 1) &&
2309 (s->failed && (disk_idx == r6s->failed_num[0] ||
2310 disk_idx == r6s->failed_num[1]))) {
2311 /* have disk failed, and we're requested to fetch it;
2314 pr_debug("Computing stripe %llu block %d\n",
2315 (unsigned long long)sh->sector, disk_idx);
2316 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2317 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2318 set_bit(R5_Wantcompute, &dev->flags);
2319 sh->ops.target = disk_idx;
2320 sh->ops.target2 = -1; /* no 2nd target */
2324 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2325 /* Computing 2-failure is *very* expensive; only
2326 * do it if failed >= 2
2329 for (other = disks; other--; ) {
2330 if (other == disk_idx)
2332 if (!test_bit(R5_UPTODATE,
2333 &sh->dev[other].flags))
2337 pr_debug("Computing stripe %llu blocks %d,%d\n",
2338 (unsigned long long)sh->sector,
2340 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2341 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2342 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2343 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2344 sh->ops.target = disk_idx;
2345 sh->ops.target2 = other;
2349 } else if (test_bit(R5_Insync, &dev->flags)) {
2350 set_bit(R5_LOCKED, &dev->flags);
2351 set_bit(R5_Wantread, &dev->flags);
2353 pr_debug("Reading block %d (sync=%d)\n",
2354 disk_idx, s->syncing);
2362 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2364 static void handle_stripe_fill6(struct stripe_head *sh,
2365 struct stripe_head_state *s, struct r6_state *r6s,
2370 /* look for blocks to read/compute, skip this if a compute
2371 * is already in flight, or if the stripe contents are in the
2372 * midst of changing due to a write
2374 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2375 !sh->reconstruct_state)
2376 for (i = disks; i--; )
2377 if (fetch_block6(sh, s, r6s, i, disks))
2379 set_bit(STRIPE_HANDLE, &sh->state);
2383 /* handle_stripe_clean_event
2384 * any written block on an uptodate or failed drive can be returned.
2385 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2386 * never LOCKED, so we don't need to test 'failed' directly.
2388 static void handle_stripe_clean_event(raid5_conf_t *conf,
2389 struct stripe_head *sh, int disks, struct bio **return_bi)
2394 for (i = disks; i--; )
2395 if (sh->dev[i].written) {
2397 if (!test_bit(R5_LOCKED, &dev->flags) &&
2398 test_bit(R5_UPTODATE, &dev->flags)) {
2399 /* We can return any write requests */
2400 struct bio *wbi, *wbi2;
2402 pr_debug("Return write for disc %d\n", i);
2403 spin_lock_irq(&conf->device_lock);
2405 dev->written = NULL;
2406 while (wbi && wbi->bi_sector <
2407 dev->sector + STRIPE_SECTORS) {
2408 wbi2 = r5_next_bio(wbi, dev->sector);
2409 if (!raid5_dec_bi_phys_segments(wbi)) {
2410 md_write_end(conf->mddev);
2411 wbi->bi_next = *return_bi;
2416 if (dev->towrite == NULL)
2418 spin_unlock_irq(&conf->device_lock);
2420 bitmap_endwrite(conf->mddev->bitmap,
2423 !test_bit(STRIPE_DEGRADED, &sh->state),
2428 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2429 if (atomic_dec_and_test(&conf->pending_full_writes))
2430 md_wakeup_thread(conf->mddev->thread);
2433 static void handle_stripe_dirtying5(raid5_conf_t *conf,
2434 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2436 int rmw = 0, rcw = 0, i;
2437 for (i = disks; i--; ) {
2438 /* would I have to read this buffer for read_modify_write */
2439 struct r5dev *dev = &sh->dev[i];
2440 if ((dev->towrite || i == sh->pd_idx) &&
2441 !test_bit(R5_LOCKED, &dev->flags) &&
2442 !(test_bit(R5_UPTODATE, &dev->flags) ||
2443 test_bit(R5_Wantcompute, &dev->flags))) {
2444 if (test_bit(R5_Insync, &dev->flags))
2447 rmw += 2*disks; /* cannot read it */
2449 /* Would I have to read this buffer for reconstruct_write */
2450 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2451 !test_bit(R5_LOCKED, &dev->flags) &&
2452 !(test_bit(R5_UPTODATE, &dev->flags) ||
2453 test_bit(R5_Wantcompute, &dev->flags))) {
2454 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2459 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2460 (unsigned long long)sh->sector, rmw, rcw);
2461 set_bit(STRIPE_HANDLE, &sh->state);
2462 if (rmw < rcw && rmw > 0)
2463 /* prefer read-modify-write, but need to get some data */
2464 for (i = disks; i--; ) {
2465 struct r5dev *dev = &sh->dev[i];
2466 if ((dev->towrite || i == sh->pd_idx) &&
2467 !test_bit(R5_LOCKED, &dev->flags) &&
2468 !(test_bit(R5_UPTODATE, &dev->flags) ||
2469 test_bit(R5_Wantcompute, &dev->flags)) &&
2470 test_bit(R5_Insync, &dev->flags)) {
2472 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2473 pr_debug("Read_old block "
2474 "%d for r-m-w\n", i);
2475 set_bit(R5_LOCKED, &dev->flags);
2476 set_bit(R5_Wantread, &dev->flags);
2479 set_bit(STRIPE_DELAYED, &sh->state);
2480 set_bit(STRIPE_HANDLE, &sh->state);
2484 if (rcw <= rmw && rcw > 0)
2485 /* want reconstruct write, but need to get some data */
2486 for (i = disks; i--; ) {
2487 struct r5dev *dev = &sh->dev[i];
2488 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2490 !test_bit(R5_LOCKED, &dev->flags) &&
2491 !(test_bit(R5_UPTODATE, &dev->flags) ||
2492 test_bit(R5_Wantcompute, &dev->flags)) &&
2493 test_bit(R5_Insync, &dev->flags)) {
2495 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2496 pr_debug("Read_old block "
2497 "%d for Reconstruct\n", i);
2498 set_bit(R5_LOCKED, &dev->flags);
2499 set_bit(R5_Wantread, &dev->flags);
2502 set_bit(STRIPE_DELAYED, &sh->state);
2503 set_bit(STRIPE_HANDLE, &sh->state);
2507 /* now if nothing is locked, and if we have enough data,
2508 * we can start a write request
2510 /* since handle_stripe can be called at any time we need to handle the
2511 * case where a compute block operation has been submitted and then a
2512 * subsequent call wants to start a write request. raid_run_ops only
2513 * handles the case where compute block and reconstruct are requested
2514 * simultaneously. If this is not the case then new writes need to be
2515 * held off until the compute completes.
2517 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2518 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2519 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2520 schedule_reconstruction(sh, s, rcw == 0, 0);
2523 static void handle_stripe_dirtying6(raid5_conf_t *conf,
2524 struct stripe_head *sh, struct stripe_head_state *s,
2525 struct r6_state *r6s, int disks)
2527 int rcw = 0, pd_idx = sh->pd_idx, i;
2528 int qd_idx = sh->qd_idx;
2530 set_bit(STRIPE_HANDLE, &sh->state);
2531 for (i = disks; i--; ) {
2532 struct r5dev *dev = &sh->dev[i];
2533 /* check if we haven't enough data */
2534 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2535 i != pd_idx && i != qd_idx &&
2536 !test_bit(R5_LOCKED, &dev->flags) &&
2537 !(test_bit(R5_UPTODATE, &dev->flags) ||
2538 test_bit(R5_Wantcompute, &dev->flags))) {
2540 if (!test_bit(R5_Insync, &dev->flags))
2541 continue; /* it's a failed drive */
2544 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2545 pr_debug("Read_old stripe %llu "
2546 "block %d for Reconstruct\n",
2547 (unsigned long long)sh->sector, i);
2548 set_bit(R5_LOCKED, &dev->flags);
2549 set_bit(R5_Wantread, &dev->flags);
2552 pr_debug("Request delayed stripe %llu "
2553 "block %d for Reconstruct\n",
2554 (unsigned long long)sh->sector, i);
2555 set_bit(STRIPE_DELAYED, &sh->state);
2556 set_bit(STRIPE_HANDLE, &sh->state);
2560 /* now if nothing is locked, and if we have enough data, we can start a
2563 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2564 s->locked == 0 && rcw == 0 &&
2565 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2566 schedule_reconstruction(sh, s, 1, 0);
2570 static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2571 struct stripe_head_state *s, int disks)
2573 struct r5dev *dev = NULL;
2575 set_bit(STRIPE_HANDLE, &sh->state);
2577 switch (sh->check_state) {
2578 case check_state_idle:
2579 /* start a new check operation if there are no failures */
2580 if (s->failed == 0) {
2581 BUG_ON(s->uptodate != disks);
2582 sh->check_state = check_state_run;
2583 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2584 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2588 dev = &sh->dev[s->failed_num];
2590 case check_state_compute_result:
2591 sh->check_state = check_state_idle;
2593 dev = &sh->dev[sh->pd_idx];
2595 /* check that a write has not made the stripe insync */
2596 if (test_bit(STRIPE_INSYNC, &sh->state))
2599 /* either failed parity check, or recovery is happening */
2600 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2601 BUG_ON(s->uptodate != disks);
2603 set_bit(R5_LOCKED, &dev->flags);
2605 set_bit(R5_Wantwrite, &dev->flags);
2607 clear_bit(STRIPE_DEGRADED, &sh->state);
2608 set_bit(STRIPE_INSYNC, &sh->state);
2610 case check_state_run:
2611 break; /* we will be called again upon completion */
2612 case check_state_check_result:
2613 sh->check_state = check_state_idle;
2615 /* if a failure occurred during the check operation, leave
2616 * STRIPE_INSYNC not set and let the stripe be handled again
2621 /* handle a successful check operation, if parity is correct
2622 * we are done. Otherwise update the mismatch count and repair
2623 * parity if !MD_RECOVERY_CHECK
2625 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
2626 /* parity is correct (on disc,
2627 * not in buffer any more)
2629 set_bit(STRIPE_INSYNC, &sh->state);
2631 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2632 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2633 /* don't try to repair!! */
2634 set_bit(STRIPE_INSYNC, &sh->state);
2636 sh->check_state = check_state_compute_run;
2637 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2638 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2639 set_bit(R5_Wantcompute,
2640 &sh->dev[sh->pd_idx].flags);
2641 sh->ops.target = sh->pd_idx;
2642 sh->ops.target2 = -1;
2647 case check_state_compute_run:
2650 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2651 __func__, sh->check_state,
2652 (unsigned long long) sh->sector);
2658 static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2659 struct stripe_head_state *s,
2660 struct r6_state *r6s, int disks)
2662 int pd_idx = sh->pd_idx;
2663 int qd_idx = sh->qd_idx;
2666 set_bit(STRIPE_HANDLE, &sh->state);
2668 BUG_ON(s->failed > 2);
2670 /* Want to check and possibly repair P and Q.
2671 * However there could be one 'failed' device, in which
2672 * case we can only check one of them, possibly using the
2673 * other to generate missing data
2676 switch (sh->check_state) {
2677 case check_state_idle:
2678 /* start a new check operation if there are < 2 failures */
2679 if (s->failed == r6s->q_failed) {
2680 /* The only possible failed device holds Q, so it
2681 * makes sense to check P (If anything else were failed,
2682 * we would have used P to recreate it).
2684 sh->check_state = check_state_run;
2686 if (!r6s->q_failed && s->failed < 2) {
2687 /* Q is not failed, and we didn't use it to generate
2688 * anything, so it makes sense to check it
2690 if (sh->check_state == check_state_run)
2691 sh->check_state = check_state_run_pq;
2693 sh->check_state = check_state_run_q;
2696 /* discard potentially stale zero_sum_result */
2697 sh->ops.zero_sum_result = 0;
2699 if (sh->check_state == check_state_run) {
2700 /* async_xor_zero_sum destroys the contents of P */
2701 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2704 if (sh->check_state >= check_state_run &&
2705 sh->check_state <= check_state_run_pq) {
2706 /* async_syndrome_zero_sum preserves P and Q, so
2707 * no need to mark them !uptodate here
2709 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2713 /* we have 2-disk failure */
2714 BUG_ON(s->failed != 2);
2716 case check_state_compute_result:
2717 sh->check_state = check_state_idle;
2719 /* check that a write has not made the stripe insync */
2720 if (test_bit(STRIPE_INSYNC, &sh->state))
2723 /* now write out any block on a failed drive,
2724 * or P or Q if they were recomputed
2726 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
2727 if (s->failed == 2) {
2728 dev = &sh->dev[r6s->failed_num[1]];
2730 set_bit(R5_LOCKED, &dev->flags);
2731 set_bit(R5_Wantwrite, &dev->flags);
2733 if (s->failed >= 1) {
2734 dev = &sh->dev[r6s->failed_num[0]];
2736 set_bit(R5_LOCKED, &dev->flags);
2737 set_bit(R5_Wantwrite, &dev->flags);
2739 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2740 dev = &sh->dev[pd_idx];
2742 set_bit(R5_LOCKED, &dev->flags);
2743 set_bit(R5_Wantwrite, &dev->flags);
2745 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2746 dev = &sh->dev[qd_idx];
2748 set_bit(R5_LOCKED, &dev->flags);
2749 set_bit(R5_Wantwrite, &dev->flags);
2751 clear_bit(STRIPE_DEGRADED, &sh->state);
2753 set_bit(STRIPE_INSYNC, &sh->state);
2755 case check_state_run:
2756 case check_state_run_q:
2757 case check_state_run_pq:
2758 break; /* we will be called again upon completion */
2759 case check_state_check_result:
2760 sh->check_state = check_state_idle;
2762 /* handle a successful check operation, if parity is correct
2763 * we are done. Otherwise update the mismatch count and repair
2764 * parity if !MD_RECOVERY_CHECK
2766 if (sh->ops.zero_sum_result == 0) {
2767 /* both parities are correct */
2769 set_bit(STRIPE_INSYNC, &sh->state);
2771 /* in contrast to the raid5 case we can validate
2772 * parity, but still have a failure to write
2775 sh->check_state = check_state_compute_result;
2776 /* Returning at this point means that we may go
2777 * off and bring p and/or q uptodate again so
2778 * we make sure to check zero_sum_result again
2779 * to verify if p or q need writeback
2783 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2784 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2785 /* don't try to repair!! */
2786 set_bit(STRIPE_INSYNC, &sh->state);
2788 int *target = &sh->ops.target;
2790 sh->ops.target = -1;
2791 sh->ops.target2 = -1;
2792 sh->check_state = check_state_compute_run;
2793 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2794 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2795 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2796 set_bit(R5_Wantcompute,
2797 &sh->dev[pd_idx].flags);
2799 target = &sh->ops.target2;
2802 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2803 set_bit(R5_Wantcompute,
2804 &sh->dev[qd_idx].flags);
2811 case check_state_compute_run:
2814 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2815 __func__, sh->check_state,
2816 (unsigned long long) sh->sector);
2821 static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2822 struct r6_state *r6s)
2826 /* We have read all the blocks in this stripe and now we need to
2827 * copy some of them into a target stripe for expand.
2829 struct dma_async_tx_descriptor *tx = NULL;
2830 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2831 for (i = 0; i < sh->disks; i++)
2832 if (i != sh->pd_idx && i != sh->qd_idx) {
2834 struct stripe_head *sh2;
2835 struct async_submit_ctl submit;
2837 sector_t bn = compute_blocknr(sh, i, 1);
2838 sector_t s = raid5_compute_sector(conf, bn, 0,
2840 sh2 = get_active_stripe(conf, s, 0, 1, 1);
2842 /* so far only the early blocks of this stripe
2843 * have been requested. When later blocks
2844 * get requested, we will try again
2847 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2848 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2849 /* must have already done this block */
2850 release_stripe(sh2);
2854 /* place all the copies on one channel */
2855 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
2856 tx = async_memcpy(sh2->dev[dd_idx].page,
2857 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2860 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2861 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2862 for (j = 0; j < conf->raid_disks; j++)
2863 if (j != sh2->pd_idx &&
2864 (!r6s || j != sh2->qd_idx) &&
2865 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2867 if (j == conf->raid_disks) {
2868 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2869 set_bit(STRIPE_HANDLE, &sh2->state);
2871 release_stripe(sh2);
2874 /* done submitting copies, wait for them to complete */
2877 dma_wait_for_async_tx(tx);
2883 * handle_stripe - do things to a stripe.
2885 * We lock the stripe and then examine the state of various bits
2886 * to see what needs to be done.
2888 * return some read request which now have data
2889 * return some write requests which are safely on disc
2890 * schedule a read on some buffers
2891 * schedule a write of some buffers
2892 * return confirmation of parity correctness
2894 * buffers are taken off read_list or write_list, and bh_cache buffers
2895 * get BH_Lock set before the stripe lock is released.
2899 static bool handle_stripe5(struct stripe_head *sh)
2901 raid5_conf_t *conf = sh->raid_conf;
2902 int disks = sh->disks, i;
2903 struct bio *return_bi = NULL;
2904 struct stripe_head_state s;
2906 mdk_rdev_t *blocked_rdev = NULL;
2909 memset(&s, 0, sizeof(s));
2910 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2911 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2912 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2913 sh->reconstruct_state);
2915 spin_lock(&sh->lock);
2916 clear_bit(STRIPE_HANDLE, &sh->state);
2917 clear_bit(STRIPE_DELAYED, &sh->state);
2919 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2920 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2921 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2923 /* Now to look around and see what can be done */
2925 for (i=disks; i--; ) {
2929 clear_bit(R5_Insync, &dev->flags);
2931 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2932 "written %p\n", i, dev->flags, dev->toread, dev->read,
2933 dev->towrite, dev->written);
2935 /* maybe we can request a biofill operation
2937 * new wantfill requests are only permitted while
2938 * ops_complete_biofill is guaranteed to be inactive
2940 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
2941 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
2942 set_bit(R5_Wantfill, &dev->flags);
2944 /* now count some things */
2945 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2946 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
2947 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
2949 if (test_bit(R5_Wantfill, &dev->flags))
2951 else if (dev->toread)
2955 if (!test_bit(R5_OVERWRITE, &dev->flags))
2960 rdev = rcu_dereference(conf->disks[i].rdev);
2961 if (blocked_rdev == NULL &&
2962 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2963 blocked_rdev = rdev;
2964 atomic_inc(&rdev->nr_pending);
2966 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2967 /* The ReadError flag will just be confusing now */
2968 clear_bit(R5_ReadError, &dev->flags);
2969 clear_bit(R5_ReWrite, &dev->flags);
2971 if (!rdev || !test_bit(In_sync, &rdev->flags)
2972 || test_bit(R5_ReadError, &dev->flags)) {
2976 set_bit(R5_Insync, &dev->flags);
2980 if (unlikely(blocked_rdev)) {
2981 if (s.syncing || s.expanding || s.expanded ||
2982 s.to_write || s.written) {
2983 set_bit(STRIPE_HANDLE, &sh->state);
2986 /* There is nothing for the blocked_rdev to block */
2987 rdev_dec_pending(blocked_rdev, conf->mddev);
2988 blocked_rdev = NULL;
2991 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2992 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2993 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2996 pr_debug("locked=%d uptodate=%d to_read=%d"
2997 " to_write=%d failed=%d failed_num=%d\n",
2998 s.locked, s.uptodate, s.to_read, s.to_write,
2999 s.failed, s.failed_num);
3000 /* check if the array has lost two devices and, if so, some requests might
3003 if (s.failed > 1 && s.to_read+s.to_write+s.written)
3004 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
3005 if (s.failed > 1 && s.syncing) {
3006 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3007 clear_bit(STRIPE_SYNCING, &sh->state);
3011 /* might be able to return some write requests if the parity block
3012 * is safe, or on a failed drive
3014 dev = &sh->dev[sh->pd_idx];
3016 ((test_bit(R5_Insync, &dev->flags) &&
3017 !test_bit(R5_LOCKED, &dev->flags) &&
3018 test_bit(R5_UPTODATE, &dev->flags)) ||
3019 (s.failed == 1 && s.failed_num == sh->pd_idx)))
3020 handle_stripe_clean_event(conf, sh, disks, &return_bi);
3022 /* Now we might consider reading some blocks, either to check/generate
3023 * parity, or to satisfy requests
3024 * or to load a block that is being partially written.
3026 if (s.to_read || s.non_overwrite ||
3027 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3028 handle_stripe_fill5(sh, &s, disks);
3030 /* Now we check to see if any write operations have recently
3034 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3036 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3037 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3038 sh->reconstruct_state = reconstruct_state_idle;
3040 /* All the 'written' buffers and the parity block are ready to
3041 * be written back to disk
3043 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3044 for (i = disks; i--; ) {
3046 if (test_bit(R5_LOCKED, &dev->flags) &&
3047 (i == sh->pd_idx || dev->written)) {
3048 pr_debug("Writing block %d\n", i);
3049 set_bit(R5_Wantwrite, &dev->flags);
3052 if (!test_bit(R5_Insync, &dev->flags) ||
3053 (i == sh->pd_idx && s.failed == 0))
3054 set_bit(STRIPE_INSYNC, &sh->state);
3057 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3058 atomic_dec(&conf->preread_active_stripes);
3059 if (atomic_read(&conf->preread_active_stripes) <
3061 md_wakeup_thread(conf->mddev->thread);
3065 /* Now to consider new write requests and what else, if anything
3066 * should be read. We do not handle new writes when:
3067 * 1/ A 'write' operation (copy+xor) is already in flight.
3068 * 2/ A 'check' operation is in flight, as it may clobber the parity
3071 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3072 handle_stripe_dirtying5(conf, sh, &s, disks);
3074 /* maybe we need to check and possibly fix the parity for this stripe
3075 * Any reads will already have been scheduled, so we just see if enough
3076 * data is available. The parity check is held off while parity
3077 * dependent operations are in flight.
3079 if (sh->check_state ||
3080 (s.syncing && s.locked == 0 &&