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/async_tx.h>
49 #include <linux/seq_file.h>
59 #define NR_STRIPES 256
60 #define STRIPE_SIZE PAGE_SIZE
61 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
62 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
63 #define IO_THRESHOLD 1
64 #define BYPASS_THRESHOLD 1
65 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
66 #define HASH_MASK (NR_HASH - 1)
68 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
70 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
79 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
81 * The following can be used to debug the driver
83 #define RAID5_PARANOIA 1
84 #if RAID5_PARANOIA && defined(CONFIG_SMP)
85 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
87 # define CHECK_DEVLOCK()
95 #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
97 #if !RAID6_USE_EMPTY_ZERO_PAGE
98 /* In .bss so it's zeroed */
99 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
103 * We maintain a biased count of active stripes in the bottom 16 bits of
104 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
106 static inline int raid5_bi_phys_segments(struct bio *bio)
108 return bio->bi_phys_segments & 0xffff;
111 static inline int raid5_bi_hw_segments(struct bio *bio)
113 return (bio->bi_phys_segments >> 16) & 0xffff;
116 static inline int raid5_dec_bi_phys_segments(struct bio *bio)
118 --bio->bi_phys_segments;
119 return raid5_bi_phys_segments(bio);
122 static inline int raid5_dec_bi_hw_segments(struct bio *bio)
124 unsigned short val = raid5_bi_hw_segments(bio);
127 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
131 static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
133 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
136 static inline int raid6_next_disk(int disk, int raid_disks)
139 return (disk < raid_disks) ? disk : 0;
142 static void return_io(struct bio *return_bi)
144 struct bio *bi = return_bi;
147 return_bi = bi->bi_next;
155 static void print_raid5_conf (raid5_conf_t *conf);
157 static int stripe_operations_active(struct stripe_head *sh)
159 return sh->check_state || sh->reconstruct_state ||
160 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
161 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
164 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
166 if (atomic_dec_and_test(&sh->count)) {
167 BUG_ON(!list_empty(&sh->lru));
168 BUG_ON(atomic_read(&conf->active_stripes)==0);
169 if (test_bit(STRIPE_HANDLE, &sh->state)) {
170 if (test_bit(STRIPE_DELAYED, &sh->state)) {
171 list_add_tail(&sh->lru, &conf->delayed_list);
172 blk_plug_device(conf->mddev->queue);
173 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
174 sh->bm_seq - conf->seq_write > 0) {
175 list_add_tail(&sh->lru, &conf->bitmap_list);
176 blk_plug_device(conf->mddev->queue);
178 clear_bit(STRIPE_BIT_DELAY, &sh->state);
179 list_add_tail(&sh->lru, &conf->handle_list);
181 md_wakeup_thread(conf->mddev->thread);
183 BUG_ON(stripe_operations_active(sh));
184 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
185 atomic_dec(&conf->preread_active_stripes);
186 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
187 md_wakeup_thread(conf->mddev->thread);
189 atomic_dec(&conf->active_stripes);
190 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
191 list_add_tail(&sh->lru, &conf->inactive_list);
192 wake_up(&conf->wait_for_stripe);
193 if (conf->retry_read_aligned)
194 md_wakeup_thread(conf->mddev->thread);
199 static void release_stripe(struct stripe_head *sh)
201 raid5_conf_t *conf = sh->raid_conf;
204 spin_lock_irqsave(&conf->device_lock, flags);
205 __release_stripe(conf, sh);
206 spin_unlock_irqrestore(&conf->device_lock, flags);
209 static inline void remove_hash(struct stripe_head *sh)
211 pr_debug("remove_hash(), stripe %llu\n",
212 (unsigned long long)sh->sector);
214 hlist_del_init(&sh->hash);
217 static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
219 struct hlist_head *hp = stripe_hash(conf, sh->sector);
221 pr_debug("insert_hash(), stripe %llu\n",
222 (unsigned long long)sh->sector);
225 hlist_add_head(&sh->hash, hp);
229 /* find an idle stripe, make sure it is unhashed, and return it. */
230 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
232 struct stripe_head *sh = NULL;
233 struct list_head *first;
236 if (list_empty(&conf->inactive_list))
238 first = conf->inactive_list.next;
239 sh = list_entry(first, struct stripe_head, lru);
240 list_del_init(first);
242 atomic_inc(&conf->active_stripes);
247 static void shrink_buffers(struct stripe_head *sh, int num)
252 for (i=0; i<num ; i++) {
256 sh->dev[i].page = NULL;
261 static int grow_buffers(struct stripe_head *sh, int num)
265 for (i=0; i<num; i++) {
268 if (!(page = alloc_page(GFP_KERNEL))) {
271 sh->dev[i].page = page;
276 static void raid5_build_block(struct stripe_head *sh, int i);
277 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int previous);
279 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
281 raid5_conf_t *conf = sh->raid_conf;
284 BUG_ON(atomic_read(&sh->count) != 0);
285 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
286 BUG_ON(stripe_operations_active(sh));
289 pr_debug("init_stripe called, stripe %llu\n",
290 (unsigned long long)sh->sector);
294 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
296 sh->pd_idx = stripe_to_pdidx(sector, conf, previous);
300 for (i = sh->disks; i--; ) {
301 struct r5dev *dev = &sh->dev[i];
303 if (dev->toread || dev->read || dev->towrite || dev->written ||
304 test_bit(R5_LOCKED, &dev->flags)) {
305 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
306 (unsigned long long)sh->sector, i, dev->toread,
307 dev->read, dev->towrite, dev->written,
308 test_bit(R5_LOCKED, &dev->flags));
312 raid5_build_block(sh, i);
314 insert_hash(conf, sh);
317 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
319 struct stripe_head *sh;
320 struct hlist_node *hn;
323 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
324 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
325 if (sh->sector == sector && sh->disks == disks)
327 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
331 static void unplug_slaves(mddev_t *mddev);
332 static void raid5_unplug_device(struct request_queue *q);
334 static struct stripe_head *
335 get_active_stripe(raid5_conf_t *conf, sector_t sector,
336 int previous, int noblock)
338 struct stripe_head *sh;
339 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
341 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
343 spin_lock_irq(&conf->device_lock);
346 wait_event_lock_irq(conf->wait_for_stripe,
348 conf->device_lock, /* nothing */);
349 sh = __find_stripe(conf, sector, disks);
351 if (!conf->inactive_blocked)
352 sh = get_free_stripe(conf);
353 if (noblock && sh == NULL)
356 conf->inactive_blocked = 1;
357 wait_event_lock_irq(conf->wait_for_stripe,
358 !list_empty(&conf->inactive_list) &&
359 (atomic_read(&conf->active_stripes)
360 < (conf->max_nr_stripes *3/4)
361 || !conf->inactive_blocked),
363 raid5_unplug_device(conf->mddev->queue)
365 conf->inactive_blocked = 0;
367 init_stripe(sh, sector, previous);
369 if (atomic_read(&sh->count)) {
370 BUG_ON(!list_empty(&sh->lru));
372 if (!test_bit(STRIPE_HANDLE, &sh->state))
373 atomic_inc(&conf->active_stripes);
374 if (list_empty(&sh->lru) &&
375 !test_bit(STRIPE_EXPANDING, &sh->state))
377 list_del_init(&sh->lru);
380 } while (sh == NULL);
383 atomic_inc(&sh->count);
385 spin_unlock_irq(&conf->device_lock);
390 raid5_end_read_request(struct bio *bi, int error);
392 raid5_end_write_request(struct bio *bi, int error);
394 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
396 raid5_conf_t *conf = sh->raid_conf;
397 int i, disks = sh->disks;
401 for (i = disks; i--; ) {
405 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
407 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
412 bi = &sh->dev[i].req;
416 bi->bi_end_io = raid5_end_write_request;
418 bi->bi_end_io = raid5_end_read_request;
421 rdev = rcu_dereference(conf->disks[i].rdev);
422 if (rdev && test_bit(Faulty, &rdev->flags))
425 atomic_inc(&rdev->nr_pending);
429 if (s->syncing || s->expanding || s->expanded)
430 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
432 set_bit(STRIPE_IO_STARTED, &sh->state);
434 bi->bi_bdev = rdev->bdev;
435 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
436 __func__, (unsigned long long)sh->sector,
438 atomic_inc(&sh->count);
439 bi->bi_sector = sh->sector + rdev->data_offset;
440 bi->bi_flags = 1 << BIO_UPTODATE;
444 bi->bi_io_vec = &sh->dev[i].vec;
445 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
446 bi->bi_io_vec[0].bv_offset = 0;
447 bi->bi_size = STRIPE_SIZE;
450 test_bit(R5_ReWrite, &sh->dev[i].flags))
451 atomic_add(STRIPE_SECTORS,
452 &rdev->corrected_errors);
453 generic_make_request(bi);
456 set_bit(STRIPE_DEGRADED, &sh->state);
457 pr_debug("skip op %ld on disc %d for sector %llu\n",
458 bi->bi_rw, i, (unsigned long long)sh->sector);
459 clear_bit(R5_LOCKED, &sh->dev[i].flags);
460 set_bit(STRIPE_HANDLE, &sh->state);
465 static struct dma_async_tx_descriptor *
466 async_copy_data(int frombio, struct bio *bio, struct page *page,
467 sector_t sector, struct dma_async_tx_descriptor *tx)
470 struct page *bio_page;
474 if (bio->bi_sector >= sector)
475 page_offset = (signed)(bio->bi_sector - sector) * 512;
477 page_offset = (signed)(sector - bio->bi_sector) * -512;
478 bio_for_each_segment(bvl, bio, i) {
479 int len = bio_iovec_idx(bio, i)->bv_len;
483 if (page_offset < 0) {
484 b_offset = -page_offset;
485 page_offset += b_offset;
489 if (len > 0 && page_offset + len > STRIPE_SIZE)
490 clen = STRIPE_SIZE - page_offset;
495 b_offset += bio_iovec_idx(bio, i)->bv_offset;
496 bio_page = bio_iovec_idx(bio, i)->bv_page;
498 tx = async_memcpy(page, bio_page, page_offset,
503 tx = async_memcpy(bio_page, page, b_offset,
508 if (clen < len) /* hit end of page */
516 static void ops_complete_biofill(void *stripe_head_ref)
518 struct stripe_head *sh = stripe_head_ref;
519 struct bio *return_bi = NULL;
520 raid5_conf_t *conf = sh->raid_conf;
523 pr_debug("%s: stripe %llu\n", __func__,
524 (unsigned long long)sh->sector);
526 /* clear completed biofills */
527 spin_lock_irq(&conf->device_lock);
528 for (i = sh->disks; i--; ) {
529 struct r5dev *dev = &sh->dev[i];
531 /* acknowledge completion of a biofill operation */
532 /* and check if we need to reply to a read request,
533 * new R5_Wantfill requests are held off until
534 * !STRIPE_BIOFILL_RUN
536 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
537 struct bio *rbi, *rbi2;
542 while (rbi && rbi->bi_sector <
543 dev->sector + STRIPE_SECTORS) {
544 rbi2 = r5_next_bio(rbi, dev->sector);
545 if (!raid5_dec_bi_phys_segments(rbi)) {
546 rbi->bi_next = return_bi;
553 spin_unlock_irq(&conf->device_lock);
554 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
556 return_io(return_bi);
558 set_bit(STRIPE_HANDLE, &sh->state);
562 static void ops_run_biofill(struct stripe_head *sh)
564 struct dma_async_tx_descriptor *tx = NULL;
565 raid5_conf_t *conf = sh->raid_conf;
568 pr_debug("%s: stripe %llu\n", __func__,
569 (unsigned long long)sh->sector);
571 for (i = sh->disks; i--; ) {
572 struct r5dev *dev = &sh->dev[i];
573 if (test_bit(R5_Wantfill, &dev->flags)) {
575 spin_lock_irq(&conf->device_lock);
576 dev->read = rbi = dev->toread;
578 spin_unlock_irq(&conf->device_lock);
579 while (rbi && rbi->bi_sector <
580 dev->sector + STRIPE_SECTORS) {
581 tx = async_copy_data(0, rbi, dev->page,
583 rbi = r5_next_bio(rbi, dev->sector);
588 atomic_inc(&sh->count);
589 async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
590 ops_complete_biofill, sh);
593 static void ops_complete_compute5(void *stripe_head_ref)
595 struct stripe_head *sh = stripe_head_ref;
596 int target = sh->ops.target;
597 struct r5dev *tgt = &sh->dev[target];
599 pr_debug("%s: stripe %llu\n", __func__,
600 (unsigned long long)sh->sector);
602 set_bit(R5_UPTODATE, &tgt->flags);
603 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
604 clear_bit(R5_Wantcompute, &tgt->flags);
605 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
606 if (sh->check_state == check_state_compute_run)
607 sh->check_state = check_state_compute_result;
608 set_bit(STRIPE_HANDLE, &sh->state);
612 static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
614 /* kernel stack size limits the total number of disks */
615 int disks = sh->disks;
616 struct page *xor_srcs[disks];
617 int target = sh->ops.target;
618 struct r5dev *tgt = &sh->dev[target];
619 struct page *xor_dest = tgt->page;
621 struct dma_async_tx_descriptor *tx;
624 pr_debug("%s: stripe %llu block: %d\n",
625 __func__, (unsigned long long)sh->sector, target);
626 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
628 for (i = disks; i--; )
630 xor_srcs[count++] = sh->dev[i].page;
632 atomic_inc(&sh->count);
634 if (unlikely(count == 1))
635 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
636 0, NULL, ops_complete_compute5, sh);
638 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
639 ASYNC_TX_XOR_ZERO_DST, NULL,
640 ops_complete_compute5, sh);
645 static void ops_complete_prexor(void *stripe_head_ref)
647 struct stripe_head *sh = stripe_head_ref;
649 pr_debug("%s: stripe %llu\n", __func__,
650 (unsigned long long)sh->sector);
653 static struct dma_async_tx_descriptor *
654 ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
656 /* kernel stack size limits the total number of disks */
657 int disks = sh->disks;
658 struct page *xor_srcs[disks];
659 int count = 0, pd_idx = sh->pd_idx, i;
661 /* existing parity data subtracted */
662 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
664 pr_debug("%s: stripe %llu\n", __func__,
665 (unsigned long long)sh->sector);
667 for (i = disks; i--; ) {
668 struct r5dev *dev = &sh->dev[i];
669 /* Only process blocks that are known to be uptodate */
670 if (test_bit(R5_Wantdrain, &dev->flags))
671 xor_srcs[count++] = dev->page;
674 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
675 ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
676 ops_complete_prexor, sh);
681 static struct dma_async_tx_descriptor *
682 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
684 int disks = sh->disks;
687 pr_debug("%s: stripe %llu\n", __func__,
688 (unsigned long long)sh->sector);
690 for (i = disks; i--; ) {
691 struct r5dev *dev = &sh->dev[i];
694 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
697 spin_lock(&sh->lock);
698 chosen = dev->towrite;
700 BUG_ON(dev->written);
701 wbi = dev->written = chosen;
702 spin_unlock(&sh->lock);
704 while (wbi && wbi->bi_sector <
705 dev->sector + STRIPE_SECTORS) {
706 tx = async_copy_data(1, wbi, dev->page,
708 wbi = r5_next_bio(wbi, dev->sector);
716 static void ops_complete_postxor(void *stripe_head_ref)
718 struct stripe_head *sh = stripe_head_ref;
719 int disks = sh->disks, i, pd_idx = sh->pd_idx;
721 pr_debug("%s: stripe %llu\n", __func__,
722 (unsigned long long)sh->sector);
724 for (i = disks; i--; ) {
725 struct r5dev *dev = &sh->dev[i];
726 if (dev->written || i == pd_idx)
727 set_bit(R5_UPTODATE, &dev->flags);
730 if (sh->reconstruct_state == reconstruct_state_drain_run)
731 sh->reconstruct_state = reconstruct_state_drain_result;
732 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
733 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
735 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
736 sh->reconstruct_state = reconstruct_state_result;
739 set_bit(STRIPE_HANDLE, &sh->state);
744 ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
746 /* kernel stack size limits the total number of disks */
747 int disks = sh->disks;
748 struct page *xor_srcs[disks];
750 int count = 0, pd_idx = sh->pd_idx, i;
751 struct page *xor_dest;
755 pr_debug("%s: stripe %llu\n", __func__,
756 (unsigned long long)sh->sector);
758 /* check if prexor is active which means only process blocks
759 * that are part of a read-modify-write (written)
761 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
763 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
764 for (i = disks; i--; ) {
765 struct r5dev *dev = &sh->dev[i];
767 xor_srcs[count++] = dev->page;
770 xor_dest = sh->dev[pd_idx].page;
771 for (i = disks; i--; ) {
772 struct r5dev *dev = &sh->dev[i];
774 xor_srcs[count++] = dev->page;
778 /* 1/ if we prexor'd then the dest is reused as a source
779 * 2/ if we did not prexor then we are redoing the parity
780 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
781 * for the synchronous xor case
783 flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
784 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
786 atomic_inc(&sh->count);
788 if (unlikely(count == 1)) {
789 flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
790 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
791 flags, tx, ops_complete_postxor, sh);
793 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
794 flags, tx, ops_complete_postxor, sh);
797 static void ops_complete_check(void *stripe_head_ref)
799 struct stripe_head *sh = stripe_head_ref;
801 pr_debug("%s: stripe %llu\n", __func__,
802 (unsigned long long)sh->sector);
804 sh->check_state = check_state_check_result;
805 set_bit(STRIPE_HANDLE, &sh->state);
809 static void ops_run_check(struct stripe_head *sh)
811 /* kernel stack size limits the total number of disks */
812 int disks = sh->disks;
813 struct page *xor_srcs[disks];
814 struct dma_async_tx_descriptor *tx;
816 int count = 0, pd_idx = sh->pd_idx, i;
817 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
819 pr_debug("%s: stripe %llu\n", __func__,
820 (unsigned long long)sh->sector);
822 for (i = disks; i--; ) {
823 struct r5dev *dev = &sh->dev[i];
825 xor_srcs[count++] = dev->page;
828 tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
829 &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
831 atomic_inc(&sh->count);
832 tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
833 ops_complete_check, sh);
836 static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
838 int overlap_clear = 0, i, disks = sh->disks;
839 struct dma_async_tx_descriptor *tx = NULL;
841 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
846 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
847 tx = ops_run_compute5(sh);
848 /* terminate the chain if postxor is not set to be run */
849 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
853 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
854 tx = ops_run_prexor(sh, tx);
856 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
857 tx = ops_run_biodrain(sh, tx);
861 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
862 ops_run_postxor(sh, tx);
864 if (test_bit(STRIPE_OP_CHECK, &ops_request))
868 for (i = disks; i--; ) {
869 struct r5dev *dev = &sh->dev[i];
870 if (test_and_clear_bit(R5_Overlap, &dev->flags))
871 wake_up(&sh->raid_conf->wait_for_overlap);
875 static int grow_one_stripe(raid5_conf_t *conf)
877 struct stripe_head *sh;
878 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
881 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
882 sh->raid_conf = conf;
883 spin_lock_init(&sh->lock);
885 if (grow_buffers(sh, conf->raid_disks)) {
886 shrink_buffers(sh, conf->raid_disks);
887 kmem_cache_free(conf->slab_cache, sh);
890 sh->disks = conf->raid_disks;
891 /* we just created an active stripe so... */
892 atomic_set(&sh->count, 1);
893 atomic_inc(&conf->active_stripes);
894 INIT_LIST_HEAD(&sh->lru);
899 static int grow_stripes(raid5_conf_t *conf, int num)
901 struct kmem_cache *sc;
902 int devs = conf->raid_disks;
904 sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
905 sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
906 conf->active_name = 0;
907 sc = kmem_cache_create(conf->cache_name[conf->active_name],
908 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
912 conf->slab_cache = sc;
913 conf->pool_size = devs;
915 if (!grow_one_stripe(conf))
920 #ifdef CONFIG_MD_RAID5_RESHAPE
921 static int resize_stripes(raid5_conf_t *conf, int newsize)
923 /* Make all the stripes able to hold 'newsize' devices.
924 * New slots in each stripe get 'page' set to a new page.
926 * This happens in stages:
927 * 1/ create a new kmem_cache and allocate the required number of
929 * 2/ gather all the old stripe_heads and tranfer the pages across
930 * to the new stripe_heads. This will have the side effect of
931 * freezing the array as once all stripe_heads have been collected,
932 * no IO will be possible. Old stripe heads are freed once their
933 * pages have been transferred over, and the old kmem_cache is
934 * freed when all stripes are done.
935 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
936 * we simple return a failre status - no need to clean anything up.
937 * 4/ allocate new pages for the new slots in the new stripe_heads.
938 * If this fails, we don't bother trying the shrink the
939 * stripe_heads down again, we just leave them as they are.
940 * As each stripe_head is processed the new one is released into
943 * Once step2 is started, we cannot afford to wait for a write,
944 * so we use GFP_NOIO allocations.
946 struct stripe_head *osh, *nsh;
947 LIST_HEAD(newstripes);
948 struct disk_info *ndisks;
950 struct kmem_cache *sc;
953 if (newsize <= conf->pool_size)
954 return 0; /* never bother to shrink */
956 err = md_allow_write(conf->mddev);
961 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
962 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
967 for (i = conf->max_nr_stripes; i; i--) {
968 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
972 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
974 nsh->raid_conf = conf;
975 spin_lock_init(&nsh->lock);
977 list_add(&nsh->lru, &newstripes);
980 /* didn't get enough, give up */
981 while (!list_empty(&newstripes)) {
982 nsh = list_entry(newstripes.next, struct stripe_head, lru);
984 kmem_cache_free(sc, nsh);
986 kmem_cache_destroy(sc);
989 /* Step 2 - Must use GFP_NOIO now.
990 * OK, we have enough stripes, start collecting inactive
991 * stripes and copying them over
993 list_for_each_entry(nsh, &newstripes, lru) {
994 spin_lock_irq(&conf->device_lock);
995 wait_event_lock_irq(conf->wait_for_stripe,
996 !list_empty(&conf->inactive_list),
998 unplug_slaves(conf->mddev)
1000 osh = get_free_stripe(conf);
1001 spin_unlock_irq(&conf->device_lock);
1002 atomic_set(&nsh->count, 1);
1003 for(i=0; i<conf->pool_size; i++)
1004 nsh->dev[i].page = osh->dev[i].page;
1005 for( ; i<newsize; i++)
1006 nsh->dev[i].page = NULL;
1007 kmem_cache_free(conf->slab_cache, osh);
1009 kmem_cache_destroy(conf->slab_cache);
1012 * At this point, we are holding all the stripes so the array
1013 * is completely stalled, so now is a good time to resize
1016 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1018 for (i=0; i<conf->raid_disks; i++)
1019 ndisks[i] = conf->disks[i];
1021 conf->disks = ndisks;
1025 /* Step 4, return new stripes to service */
1026 while(!list_empty(&newstripes)) {
1027 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1028 list_del_init(&nsh->lru);
1029 for (i=conf->raid_disks; i < newsize; i++)
1030 if (nsh->dev[i].page == NULL) {
1031 struct page *p = alloc_page(GFP_NOIO);
1032 nsh->dev[i].page = p;
1036 release_stripe(nsh);
1038 /* critical section pass, GFP_NOIO no longer needed */
1040 conf->slab_cache = sc;
1041 conf->active_name = 1-conf->active_name;
1042 conf->pool_size = newsize;
1047 static int drop_one_stripe(raid5_conf_t *conf)
1049 struct stripe_head *sh;
1051 spin_lock_irq(&conf->device_lock);
1052 sh = get_free_stripe(conf);
1053 spin_unlock_irq(&conf->device_lock);
1056 BUG_ON(atomic_read(&sh->count));
1057 shrink_buffers(sh, conf->pool_size);
1058 kmem_cache_free(conf->slab_cache, sh);
1059 atomic_dec(&conf->active_stripes);
1063 static void shrink_stripes(raid5_conf_t *conf)
1065 while (drop_one_stripe(conf))
1068 if (conf->slab_cache)
1069 kmem_cache_destroy(conf->slab_cache);
1070 conf->slab_cache = NULL;
1073 static void raid5_end_read_request(struct bio * bi, int error)
1075 struct stripe_head *sh = bi->bi_private;
1076 raid5_conf_t *conf = sh->raid_conf;
1077 int disks = sh->disks, i;
1078 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1079 char b[BDEVNAME_SIZE];
1083 for (i=0 ; i<disks; i++)
1084 if (bi == &sh->dev[i].req)
1087 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1088 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1096 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1097 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1098 rdev = conf->disks[i].rdev;
1099 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1100 " (%lu sectors at %llu on %s)\n",
1101 mdname(conf->mddev), STRIPE_SECTORS,
1102 (unsigned long long)(sh->sector
1103 + rdev->data_offset),
1104 bdevname(rdev->bdev, b));
1105 clear_bit(R5_ReadError, &sh->dev[i].flags);
1106 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1108 if (atomic_read(&conf->disks[i].rdev->read_errors))
1109 atomic_set(&conf->disks[i].rdev->read_errors, 0);
1111 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
1113 rdev = conf->disks[i].rdev;
1115 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1116 atomic_inc(&rdev->read_errors);
1117 if (conf->mddev->degraded)
1118 printk_rl(KERN_WARNING
1119 "raid5:%s: read error not correctable "
1120 "(sector %llu on %s).\n",
1121 mdname(conf->mddev),
1122 (unsigned long long)(sh->sector
1123 + rdev->data_offset),
1125 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
1127 printk_rl(KERN_WARNING
1128 "raid5:%s: read error NOT corrected!! "
1129 "(sector %llu on %s).\n",
1130 mdname(conf->mddev),
1131 (unsigned long long)(sh->sector
1132 + rdev->data_offset),
1134 else if (atomic_read(&rdev->read_errors)
1135 > conf->max_nr_stripes)
1137 "raid5:%s: Too many read errors, failing device %s.\n",
1138 mdname(conf->mddev), bdn);
1142 set_bit(R5_ReadError, &sh->dev[i].flags);
1144 clear_bit(R5_ReadError, &sh->dev[i].flags);
1145 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1146 md_error(conf->mddev, rdev);
1149 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1150 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1151 set_bit(STRIPE_HANDLE, &sh->state);
1155 static void raid5_end_write_request(struct bio *bi, int error)
1157 struct stripe_head *sh = bi->bi_private;
1158 raid5_conf_t *conf = sh->raid_conf;
1159 int disks = sh->disks, i;
1160 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1162 for (i=0 ; i<disks; i++)
1163 if (bi == &sh->dev[i].req)
1166 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1167 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1175 md_error(conf->mddev, conf->disks[i].rdev);
1177 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1179 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1180 set_bit(STRIPE_HANDLE, &sh->state);
1185 static sector_t compute_blocknr(struct stripe_head *sh, int i);
1187 static void raid5_build_block(struct stripe_head *sh, int i)
1189 struct r5dev *dev = &sh->dev[i];
1191 bio_init(&dev->req);
1192 dev->req.bi_io_vec = &dev->vec;
1194 dev->req.bi_max_vecs++;
1195 dev->vec.bv_page = dev->page;
1196 dev->vec.bv_len = STRIPE_SIZE;
1197 dev->vec.bv_offset = 0;
1199 dev->req.bi_sector = sh->sector;
1200 dev->req.bi_private = sh;
1203 dev->sector = compute_blocknr(sh, i);
1206 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1208 char b[BDEVNAME_SIZE];
1209 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1210 pr_debug("raid5: error called\n");
1212 if (!test_bit(Faulty, &rdev->flags)) {
1213 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1214 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1215 unsigned long flags;
1216 spin_lock_irqsave(&conf->device_lock, flags);
1218 spin_unlock_irqrestore(&conf->device_lock, flags);
1220 * if recovery was running, make sure it aborts.
1222 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1224 set_bit(Faulty, &rdev->flags);
1226 "raid5: Disk failure on %s, disabling device.\n"
1227 "raid5: Operation continuing on %d devices.\n",
1228 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1233 * Input: a 'big' sector number,
1234 * Output: index of the data and parity disk, and the sector # in them.
1236 static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
1238 int *dd_idx, int *pd_idx)
1241 unsigned long chunk_number;
1242 unsigned int chunk_offset;
1243 sector_t new_sector;
1244 int sectors_per_chunk = conf->chunk_size >> 9;
1245 int raid_disks = previous ? conf->previous_raid_disks
1247 int data_disks = raid_disks - conf->max_degraded;
1249 /* First compute the information on this sector */
1252 * Compute the chunk number and the sector offset inside the chunk
1254 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1255 chunk_number = r_sector;
1256 BUG_ON(r_sector != chunk_number);
1259 * Compute the stripe number
1261 stripe = chunk_number / data_disks;
1264 * Compute the data disk and parity disk indexes inside the stripe
1266 *dd_idx = chunk_number % data_disks;
1269 * Select the parity disk based on the user selected algorithm.
1271 switch(conf->level) {
1273 *pd_idx = data_disks;
1276 switch (conf->algorithm) {
1277 case ALGORITHM_LEFT_ASYMMETRIC:
1278 *pd_idx = data_disks - stripe % raid_disks;
1279 if (*dd_idx >= *pd_idx)
1282 case ALGORITHM_RIGHT_ASYMMETRIC:
1283 *pd_idx = stripe % raid_disks;
1284 if (*dd_idx >= *pd_idx)
1287 case ALGORITHM_LEFT_SYMMETRIC:
1288 *pd_idx = data_disks - stripe % raid_disks;
1289 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
1291 case ALGORITHM_RIGHT_SYMMETRIC:
1292 *pd_idx = stripe % raid_disks;
1293 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
1296 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1302 /**** FIX THIS ****/
1303 switch (conf->algorithm) {
1304 case ALGORITHM_LEFT_ASYMMETRIC:
1305 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
1306 if (*pd_idx == raid_disks-1)
1307 (*dd_idx)++; /* Q D D D P */
1308 else if (*dd_idx >= *pd_idx)
1309 (*dd_idx) += 2; /* D D P Q D */
1311 case ALGORITHM_RIGHT_ASYMMETRIC:
1312 *pd_idx = stripe % raid_disks;
1313 if (*pd_idx == raid_disks-1)
1314 (*dd_idx)++; /* Q D D D P */
1315 else if (*dd_idx >= *pd_idx)
1316 (*dd_idx) += 2; /* D D P Q D */
1318 case ALGORITHM_LEFT_SYMMETRIC:
1319 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
1320 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
1322 case ALGORITHM_RIGHT_SYMMETRIC:
1323 *pd_idx = stripe % raid_disks;
1324 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
1327 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1334 * Finally, compute the new sector number
1336 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1341 static sector_t compute_blocknr(struct stripe_head *sh, int i)
1343 raid5_conf_t *conf = sh->raid_conf;
1344 int raid_disks = sh->disks;
1345 int data_disks = raid_disks - conf->max_degraded;
1346 sector_t new_sector = sh->sector, check;
1347 int sectors_per_chunk = conf->chunk_size >> 9;
1350 int chunk_number, dummy1, dummy2, dd_idx = i;
1354 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1355 stripe = new_sector;
1356 BUG_ON(new_sector != stripe);
1358 if (i == sh->pd_idx)
1360 switch(conf->level) {
1363 switch (conf->algorithm) {
1364 case ALGORITHM_LEFT_ASYMMETRIC:
1365 case ALGORITHM_RIGHT_ASYMMETRIC:
1369 case ALGORITHM_LEFT_SYMMETRIC:
1370 case ALGORITHM_RIGHT_SYMMETRIC:
1373 i -= (sh->pd_idx + 1);
1376 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1381 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
1382 return 0; /* It is the Q disk */
1383 switch (conf->algorithm) {
1384 case ALGORITHM_LEFT_ASYMMETRIC:
1385 case ALGORITHM_RIGHT_ASYMMETRIC:
1386 if (sh->pd_idx == raid_disks-1)
1387 i--; /* Q D D D P */
1388 else if (i > sh->pd_idx)
1389 i -= 2; /* D D P Q D */
1391 case ALGORITHM_LEFT_SYMMETRIC:
1392 case ALGORITHM_RIGHT_SYMMETRIC:
1393 if (sh->pd_idx == raid_disks-1)
1394 i--; /* Q D D D P */
1399 i -= (sh->pd_idx + 2);
1403 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1409 chunk_number = stripe * data_disks + i;
1410 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1412 check = raid5_compute_sector(conf, r_sector,
1413 (raid_disks != conf->raid_disks),
1415 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
1416 printk(KERN_ERR "compute_blocknr: map not correct\n");
1425 * Copy data between a page in the stripe cache, and one or more bion
1426 * The page could align with the middle of the bio, or there could be
1427 * several bion, each with several bio_vecs, which cover part of the page
1428 * Multiple bion are linked together on bi_next. There may be extras
1429 * at the end of this list. We ignore them.
1431 static void copy_data(int frombio, struct bio *bio,
1435 char *pa = page_address(page);
1436 struct bio_vec *bvl;
1440 if (bio->bi_sector >= sector)
1441 page_offset = (signed)(bio->bi_sector - sector) * 512;
1443 page_offset = (signed)(sector - bio->bi_sector) * -512;
1444 bio_for_each_segment(bvl, bio, i) {
1445 int len = bio_iovec_idx(bio,i)->bv_len;
1449 if (page_offset < 0) {
1450 b_offset = -page_offset;
1451 page_offset += b_offset;
1455 if (len > 0 && page_offset + len > STRIPE_SIZE)
1456 clen = STRIPE_SIZE - page_offset;
1460 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1462 memcpy(pa+page_offset, ba+b_offset, clen);
1464 memcpy(ba+b_offset, pa+page_offset, clen);
1465 __bio_kunmap_atomic(ba, KM_USER0);
1467 if (clen < len) /* hit end of page */
1473 #define check_xor() do { \
1474 if (count == MAX_XOR_BLOCKS) { \
1475 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1480 static void compute_parity6(struct stripe_head *sh, int method)
1482 raid5_conf_t *conf = sh->raid_conf;
1483 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
1485 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1488 qd_idx = raid6_next_disk(pd_idx, disks);
1489 d0_idx = raid6_next_disk(qd_idx, disks);
1491 pr_debug("compute_parity, stripe %llu, method %d\n",
1492 (unsigned long long)sh->sector, method);
1495 case READ_MODIFY_WRITE:
1496 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1497 case RECONSTRUCT_WRITE:
1498 for (i= disks; i-- ;)
1499 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1500 chosen = sh->dev[i].towrite;
1501 sh->dev[i].towrite = NULL;
1503 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1504 wake_up(&conf->wait_for_overlap);
1506 BUG_ON(sh->dev[i].written);
1507 sh->dev[i].written = chosen;
1511 BUG(); /* Not implemented yet */
1514 for (i = disks; i--;)
1515 if (sh->dev[i].written) {
1516 sector_t sector = sh->dev[i].sector;
1517 struct bio *wbi = sh->dev[i].written;
1518 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1519 copy_data(1, wbi, sh->dev[i].page, sector);
1520 wbi = r5_next_bio(wbi, sector);
1523 set_bit(R5_LOCKED, &sh->dev[i].flags);
1524 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1528 // case RECONSTRUCT_WRITE:
1529 // case CHECK_PARITY:
1530 // case UPDATE_PARITY:
1531 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1532 /* FIX: Is this ordering of drives even remotely optimal? */
1536 ptrs[count++] = page_address(sh->dev[i].page);
1537 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1538 printk("block %d/%d not uptodate on parity calc\n", i,count);
1539 i = raid6_next_disk(i, disks);
1540 } while ( i != d0_idx );
1544 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1547 case RECONSTRUCT_WRITE:
1548 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1549 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1550 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1551 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1554 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1555 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1561 /* Compute one missing block */
1562 static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1564 int i, count, disks = sh->disks;
1565 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
1566 int pd_idx = sh->pd_idx;
1567 int qd_idx = raid6_next_disk(pd_idx, disks);
1569 pr_debug("compute_block_1, stripe %llu, idx %d\n",
1570 (unsigned long long)sh->sector, dd_idx);
1572 if ( dd_idx == qd_idx ) {
1573 /* We're actually computing the Q drive */
1574 compute_parity6(sh, UPDATE_PARITY);
1576 dest = page_address(sh->dev[dd_idx].page);
1577 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1579 for (i = disks ; i--; ) {
1580 if (i == dd_idx || i == qd_idx)
1582 p = page_address(sh->dev[i].page);
1583 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1586 printk("compute_block() %d, stripe %llu, %d"
1587 " not present\n", dd_idx,
1588 (unsigned long long)sh->sector, i);
1593 xor_blocks(count, STRIPE_SIZE, dest, ptr);
1594 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1595 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1599 /* Compute two missing blocks */
1600 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1602 int i, count, disks = sh->disks;
1603 int pd_idx = sh->pd_idx;
1604 int qd_idx = raid6_next_disk(pd_idx, disks);
1605 int d0_idx = raid6_next_disk(qd_idx, disks);
1608 /* faila and failb are disk numbers relative to d0_idx */
1609 /* pd_idx become disks-2 and qd_idx become disks-1 */
1610 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1611 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1613 BUG_ON(faila == failb);
1614 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1616 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1617 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1619 if ( failb == disks-1 ) {
1620 /* Q disk is one of the missing disks */
1621 if ( faila == disks-2 ) {
1622 /* Missing P+Q, just recompute */
1623 compute_parity6(sh, UPDATE_PARITY);
1626 /* We're missing D+Q; recompute D from P */
1627 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1628 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1633 /* We're missing D+P or D+D; build pointer table */
1635 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1641 ptrs[count++] = page_address(sh->dev[i].page);
1642 i = raid6_next_disk(i, disks);
1643 if (i != dd_idx1 && i != dd_idx2 &&
1644 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1645 printk("compute_2 with missing block %d/%d\n", count, i);
1646 } while ( i != d0_idx );
1648 if ( failb == disks-2 ) {
1649 /* We're missing D+P. */
1650 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1652 /* We're missing D+D. */
1653 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1656 /* Both the above update both missing blocks */
1657 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1658 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1663 schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
1664 int rcw, int expand)
1666 int i, pd_idx = sh->pd_idx, disks = sh->disks;
1669 /* if we are not expanding this is a proper write request, and
1670 * there will be bios with new data to be drained into the
1674 sh->reconstruct_state = reconstruct_state_drain_run;
1675 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1677 sh->reconstruct_state = reconstruct_state_run;
1679 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
1681 for (i = disks; i--; ) {
1682 struct r5dev *dev = &sh->dev[i];
1685 set_bit(R5_LOCKED, &dev->flags);
1686 set_bit(R5_Wantdrain, &dev->flags);
1688 clear_bit(R5_UPTODATE, &dev->flags);
1692 if (s->locked + 1 == disks)
1693 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1694 atomic_inc(&sh->raid_conf->pending_full_writes);
1696 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1697 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1699 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
1700 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1701 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1702 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
1704 for (i = disks; i--; ) {
1705 struct r5dev *dev = &sh->dev[i];
1710 (test_bit(R5_UPTODATE, &dev->flags) ||
1711 test_bit(R5_Wantcompute, &dev->flags))) {
1712 set_bit(R5_Wantdrain, &dev->flags);
1713 set_bit(R5_LOCKED, &dev->flags);
1714 clear_bit(R5_UPTODATE, &dev->flags);
1720 /* keep the parity disk locked while asynchronous operations
1723 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1724 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1727 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
1728 __func__, (unsigned long long)sh->sector,
1729 s->locked, s->ops_request);
1733 * Each stripe/dev can have one or more bion attached.
1734 * toread/towrite point to the first in a chain.
1735 * The bi_next chain must be in order.
1737 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1740 raid5_conf_t *conf = sh->raid_conf;
1743 pr_debug("adding bh b#%llu to stripe s#%llu\n",
1744 (unsigned long long)bi->bi_sector,
1745 (unsigned long long)sh->sector);
1748 spin_lock(&sh->lock);
1749 spin_lock_irq(&conf->device_lock);
1751 bip = &sh->dev[dd_idx].towrite;
1752 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1755 bip = &sh->dev[dd_idx].toread;
1756 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1757 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1759 bip = & (*bip)->bi_next;
1761 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1764 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1768 bi->bi_phys_segments++;
1769 spin_unlock_irq(&conf->device_lock);
1770 spin_unlock(&sh->lock);
1772 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
1773 (unsigned long long)bi->bi_sector,
1774 (unsigned long long)sh->sector, dd_idx);
1776 if (conf->mddev->bitmap && firstwrite) {
1777 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1779 sh->bm_seq = conf->seq_flush+1;
1780 set_bit(STRIPE_BIT_DELAY, &sh->state);
1784 /* check if page is covered */
1785 sector_t sector = sh->dev[dd_idx].sector;
1786 for (bi=sh->dev[dd_idx].towrite;
1787 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1788 bi && bi->bi_sector <= sector;
1789 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1790 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1791 sector = bi->bi_sector + (bi->bi_size>>9);
1793 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1794 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1799 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1800 spin_unlock_irq(&conf->device_lock);
1801 spin_unlock(&sh->lock);
1805 static void end_reshape(raid5_conf_t *conf);
1807 static int page_is_zero(struct page *p)
1809 char *a = page_address(p);
1810 return ((*(u32*)a) == 0 &&
1811 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1814 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int previous)
1816 int sectors_per_chunk = conf->chunk_size >> 9;
1818 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1819 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
1821 raid5_compute_sector(conf,
1822 stripe * (disks - conf->max_degraded)
1823 *sectors_per_chunk + chunk_offset,
1830 handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
1831 struct stripe_head_state *s, int disks,
1832 struct bio **return_bi)
1835 for (i = disks; i--; ) {
1839 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1842 rdev = rcu_dereference(conf->disks[i].rdev);
1843 if (rdev && test_bit(In_sync, &rdev->flags))
1844 /* multiple read failures in one stripe */
1845 md_error(conf->mddev, rdev);
1848 spin_lock_irq(&conf->device_lock);
1849 /* fail all writes first */
1850 bi = sh->dev[i].towrite;
1851 sh->dev[i].towrite = NULL;
1857 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1858 wake_up(&conf->wait_for_overlap);
1860 while (bi && bi->bi_sector <
1861 sh->dev[i].sector + STRIPE_SECTORS) {
1862 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1863 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1864 if (!raid5_dec_bi_phys_segments(bi)) {
1865 md_write_end(conf->mddev);
1866 bi->bi_next = *return_bi;
1871 /* and fail all 'written' */
1872 bi = sh->dev[i].written;
1873 sh->dev[i].written = NULL;
1874 if (bi) bitmap_end = 1;
1875 while (bi && bi->bi_sector <
1876 sh->dev[i].sector + STRIPE_SECTORS) {
1877 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1878 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1879 if (!raid5_dec_bi_phys_segments(bi)) {
1880 md_write_end(conf->mddev);
1881 bi->bi_next = *return_bi;
1887 /* fail any reads if this device is non-operational and
1888 * the data has not reached the cache yet.
1890 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
1891 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1892 test_bit(R5_ReadError, &sh->dev[i].flags))) {
1893 bi = sh->dev[i].toread;
1894 sh->dev[i].toread = NULL;
1895 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1896 wake_up(&conf->wait_for_overlap);
1897 if (bi) s->to_read--;
1898 while (bi && bi->bi_sector <
1899 sh->dev[i].sector + STRIPE_SECTORS) {
1900 struct bio *nextbi =
1901 r5_next_bio(bi, sh->dev[i].sector);
1902 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1903 if (!raid5_dec_bi_phys_segments(bi)) {
1904 bi->bi_next = *return_bi;
1910 spin_unlock_irq(&conf->device_lock);
1912 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1913 STRIPE_SECTORS, 0, 0);
1916 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
1917 if (atomic_dec_and_test(&conf->pending_full_writes))
1918 md_wakeup_thread(conf->mddev->thread);
1921 /* fetch_block5 - checks the given member device to see if its data needs
1922 * to be read or computed to satisfy a request.
1924 * Returns 1 when no more member devices need to be checked, otherwise returns
1925 * 0 to tell the loop in handle_stripe_fill5 to continue
1927 static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
1928 int disk_idx, int disks)
1930 struct r5dev *dev = &sh->dev[disk_idx];
1931 struct r5dev *failed_dev = &sh->dev[s->failed_num];
1933 /* is the data in this block needed, and can we get it? */
1934 if (!test_bit(R5_LOCKED, &dev->flags) &&
1935 !test_bit(R5_UPTODATE, &dev->flags) &&
1937 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1938 s->syncing || s->expanding ||
1940 (failed_dev->toread ||
1941 (failed_dev->towrite &&
1942 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
1943 /* We would like to get this block, possibly by computing it,
1944 * otherwise read it if the backing disk is insync
1946 if ((s->uptodate == disks - 1) &&
1947 (s->failed && disk_idx == s->failed_num)) {
1948 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
1949 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
1950 set_bit(R5_Wantcompute, &dev->flags);
1951 sh->ops.target = disk_idx;
1953 /* Careful: from this point on 'uptodate' is in the eye
1954 * of raid5_run_ops which services 'compute' operations
1955 * before writes. R5_Wantcompute flags a block that will
1956 * be R5_UPTODATE by the time it is needed for a
1957 * subsequent operation.
1960 return 1; /* uptodate + compute == disks */
1961 } else if (test_bit(R5_Insync, &dev->flags)) {
1962 set_bit(R5_LOCKED, &dev->flags);
1963 set_bit(R5_Wantread, &dev->flags);
1965 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
1974 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
1976 static void handle_stripe_fill5(struct stripe_head *sh,
1977 struct stripe_head_state *s, int disks)
1981 /* look for blocks to read/compute, skip this if a compute
1982 * is already in flight, or if the stripe contents are in the
1983 * midst of changing due to a write
1985 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
1986 !sh->reconstruct_state)
1987 for (i = disks; i--; )
1988 if (fetch_block5(sh, s, i, disks))
1990 set_bit(STRIPE_HANDLE, &sh->state);
1993 static void handle_stripe_fill6(struct stripe_head *sh,
1994 struct stripe_head_state *s, struct r6_state *r6s,
1998 for (i = disks; i--; ) {
1999 struct r5dev *dev = &sh->dev[i];
2000 if (!test_bit(R5_LOCKED, &dev->flags) &&
2001 !test_bit(R5_UPTODATE, &dev->flags) &&
2002 (dev->toread || (dev->towrite &&
2003 !test_bit(R5_OVERWRITE, &dev->flags)) ||
2004 s->syncing || s->expanding ||
2006 (sh->dev[r6s->failed_num[0]].toread ||
2009 (sh->dev[r6s->failed_num[1]].toread ||
2011 /* we would like to get this block, possibly
2012 * by computing it, but we might not be able to
2014 if ((s->uptodate == disks - 1) &&
2015 (s->failed && (i == r6s->failed_num[0] ||
2016 i == r6s->failed_num[1]))) {
2017 pr_debug("Computing stripe %llu block %d\n",
2018 (unsigned long long)sh->sector, i);
2019 compute_block_1(sh, i, 0);
2021 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
2022 /* Computing 2-failure is *very* expensive; only
2023 * do it if failed >= 2
2026 for (other = disks; other--; ) {
2029 if (!test_bit(R5_UPTODATE,
2030 &sh->dev[other].flags))
2034 pr_debug("Computing stripe %llu blocks %d,%d\n",
2035 (unsigned long long)sh->sector,
2037 compute_block_2(sh, i, other);
2039 } else if (test_bit(R5_Insync, &dev->flags)) {
2040 set_bit(R5_LOCKED, &dev->flags);
2041 set_bit(R5_Wantread, &dev->flags);
2043 pr_debug("Reading block %d (sync=%d)\n",
2048 set_bit(STRIPE_HANDLE, &sh->state);
2052 /* handle_stripe_clean_event
2053 * any written block on an uptodate or failed drive can be returned.
2054 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2055 * never LOCKED, so we don't need to test 'failed' directly.
2057 static void handle_stripe_clean_event(raid5_conf_t *conf,
2058 struct stripe_head *sh, int disks, struct bio **return_bi)
2063 for (i = disks; i--; )
2064 if (sh->dev[i].written) {
2066 if (!test_bit(R5_LOCKED, &dev->flags) &&
2067 test_bit(R5_UPTODATE, &dev->flags)) {
2068 /* We can return any write requests */
2069 struct bio *wbi, *wbi2;
2071 pr_debug("Return write for disc %d\n", i);
2072 spin_lock_irq(&conf->device_lock);
2074 dev->written = NULL;
2075 while (wbi && wbi->bi_sector <
2076 dev->sector + STRIPE_SECTORS) {
2077 wbi2 = r5_next_bio(wbi, dev->sector);
2078 if (!raid5_dec_bi_phys_segments(wbi)) {
2079 md_write_end(conf->mddev);
2080 wbi->bi_next = *return_bi;
2085 if (dev->towrite == NULL)
2087 spin_unlock_irq(&conf->device_lock);
2089 bitmap_endwrite(conf->mddev->bitmap,
2092 !test_bit(STRIPE_DEGRADED, &sh->state),
2097 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2098 if (atomic_dec_and_test(&conf->pending_full_writes))
2099 md_wakeup_thread(conf->mddev->thread);
2102 static void handle_stripe_dirtying5(raid5_conf_t *conf,
2103 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2105 int rmw = 0, rcw = 0, i;
2106 for (i = disks; i--; ) {
2107 /* would I have to read this buffer for read_modify_write */
2108 struct r5dev *dev = &sh->dev[i];
2109 if ((dev->towrite || i == sh->pd_idx) &&
2110 !test_bit(R5_LOCKED, &dev->flags) &&
2111 !(test_bit(R5_UPTODATE, &dev->flags) ||
2112 test_bit(R5_Wantcompute, &dev->flags))) {
2113 if (test_bit(R5_Insync, &dev->flags))
2116 rmw += 2*disks; /* cannot read it */
2118 /* Would I have to read this buffer for reconstruct_write */
2119 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2120 !test_bit(R5_LOCKED, &dev->flags) &&
2121 !(test_bit(R5_UPTODATE, &dev->flags) ||
2122 test_bit(R5_Wantcompute, &dev->flags))) {
2123 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2128 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2129 (unsigned long long)sh->sector, rmw, rcw);
2130 set_bit(STRIPE_HANDLE, &sh->state);
2131 if (rmw < rcw && rmw > 0)
2132 /* prefer read-modify-write, but need to get some data */
2133 for (i = disks; i--; ) {
2134 struct r5dev *dev = &sh->dev[i];
2135 if ((dev->towrite || i == sh->pd_idx) &&
2136 !test_bit(R5_LOCKED, &dev->flags) &&
2137 !(test_bit(R5_UPTODATE, &dev->flags) ||
2138 test_bit(R5_Wantcompute, &dev->flags)) &&
2139 test_bit(R5_Insync, &dev->flags)) {
2141 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2142 pr_debug("Read_old block "
2143 "%d for r-m-w\n", i);
2144 set_bit(R5_LOCKED, &dev->flags);
2145 set_bit(R5_Wantread, &dev->flags);
2148 set_bit(STRIPE_DELAYED, &sh->state);
2149 set_bit(STRIPE_HANDLE, &sh->state);
2153 if (rcw <= rmw && rcw > 0)
2154 /* want reconstruct write, but need to get some data */
2155 for (i = disks; i--; ) {
2156 struct r5dev *dev = &sh->dev[i];
2157 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2159 !test_bit(R5_LOCKED, &dev->flags) &&
2160 !(test_bit(R5_UPTODATE, &dev->flags) ||
2161 test_bit(R5_Wantcompute, &dev->flags)) &&
2162 test_bit(R5_Insync, &dev->flags)) {
2164 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2165 pr_debug("Read_old block "
2166 "%d for Reconstruct\n", i);
2167 set_bit(R5_LOCKED, &dev->flags);
2168 set_bit(R5_Wantread, &dev->flags);
2171 set_bit(STRIPE_DELAYED, &sh->state);
2172 set_bit(STRIPE_HANDLE, &sh->state);
2176 /* now if nothing is locked, and if we have enough data,
2177 * we can start a write request
2179 /* since handle_stripe can be called at any time we need to handle the
2180 * case where a compute block operation has been submitted and then a
2181 * subsequent call wants to start a write request. raid5_run_ops only
2182 * handles the case where compute block and postxor are requested
2183 * simultaneously. If this is not the case then new writes need to be
2184 * held off until the compute completes.
2186 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2187 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2188 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2189 schedule_reconstruction5(sh, s, rcw == 0, 0);
2192 static void handle_stripe_dirtying6(raid5_conf_t *conf,
2193 struct stripe_head *sh, struct stripe_head_state *s,
2194 struct r6_state *r6s, int disks)
2196 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
2197 int qd_idx = r6s->qd_idx;
2198 for (i = disks; i--; ) {
2199 struct r5dev *dev = &sh->dev[i];
2200 /* Would I have to read this buffer for reconstruct_write */
2201 if (!test_bit(R5_OVERWRITE, &dev->flags)
2202 && i != pd_idx && i != qd_idx
2203 && (!test_bit(R5_LOCKED, &dev->flags)
2205 !test_bit(R5_UPTODATE, &dev->flags)) {
2206 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2208 pr_debug("raid6: must_compute: "
2209 "disk %d flags=%#lx\n", i, dev->flags);
2214 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
2215 (unsigned long long)sh->sector, rcw, must_compute);
2216 set_bit(STRIPE_HANDLE, &sh->state);
2219 /* want reconstruct write, but need to get some data */
2220 for (i = disks; i--; ) {
2221 struct r5dev *dev = &sh->dev[i];
2222 if (!test_bit(R5_OVERWRITE, &dev->flags)
2223 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2224 && !test_bit(R5_LOCKED, &dev->flags) &&
2225 !test_bit(R5_UPTODATE, &dev->flags) &&
2226 test_bit(R5_Insync, &dev->flags)) {
2228 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2229 pr_debug("Read_old stripe %llu "
2230 "block %d for Reconstruct\n",
2231 (unsigned long long)sh->sector, i);
2232 set_bit(R5_LOCKED, &dev->flags);
2233 set_bit(R5_Wantread, &dev->flags);
2236 pr_debug("Request delayed stripe %llu "
2237 "block %d for Reconstruct\n",
2238 (unsigned long long)sh->sector, i);
2239 set_bit(STRIPE_DELAYED, &sh->state);
2240 set_bit(STRIPE_HANDLE, &sh->state);
2244 /* now if nothing is locked, and if we have enough data, we can start a
2247 if (s->locked == 0 && rcw == 0 &&
2248 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2249 if (must_compute > 0) {
2250 /* We have failed blocks and need to compute them */
2251 switch (s->failed) {
2255 compute_block_1(sh, r6s->failed_num[0], 0);
2258 compute_block_2(sh, r6s->failed_num[0],
2259 r6s->failed_num[1]);
2261 default: /* This request should have been failed? */
2266 pr_debug("Computing parity for stripe %llu\n",
2267 (unsigned long long)sh->sector);
2268 compute_parity6(sh, RECONSTRUCT_WRITE);
2269 /* now every locked buffer is ready to be written */
2270 for (i = disks; i--; )
2271 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2272 pr_debug("Writing stripe %llu block %d\n",
2273 (unsigned long long)sh->sector, i);
2275 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2277 if (s->locked == disks)
2278 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2279 atomic_inc(&conf->pending_full_writes);
2280 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2281 set_bit(STRIPE_INSYNC, &sh->state);
2283 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2284 atomic_dec(&conf->preread_active_stripes);
2285 if (atomic_read(&conf->preread_active_stripes) <
2287 md_wakeup_thread(conf->mddev->thread);
2292 static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2293 struct stripe_head_state *s, int disks)
2295 struct r5dev *dev = NULL;
2297 set_bit(STRIPE_HANDLE, &sh->state);
2299 switch (sh->check_state) {
2300 case check_state_idle:
2301 /* start a new check operation if there are no failures */
2302 if (s->failed == 0) {
2303 BUG_ON(s->uptodate != disks);
2304 sh->check_state = check_state_run;
2305 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2306 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2310 dev = &sh->dev[s->failed_num];
2312 case check_state_compute_result:
2313 sh->check_state = check_state_idle;
2315 dev = &sh->dev[sh->pd_idx];
2317 /* check that a write has not made the stripe insync */
2318 if (test_bit(STRIPE_INSYNC, &sh->state))
2321 /* either failed parity check, or recovery is happening */
2322 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2323 BUG_ON(s->uptodate != disks);
2325 set_bit(R5_LOCKED, &dev->flags);
2327 set_bit(R5_Wantwrite, &dev->flags);
2329 clear_bit(STRIPE_DEGRADED, &sh->state);
2330 set_bit(STRIPE_INSYNC, &sh->state);
2332 case check_state_run:
2333 break; /* we will be called again upon completion */
2334 case check_state_check_result:
2335 sh->check_state = check_state_idle;
2337 /* if a failure occurred during the check operation, leave
2338 * STRIPE_INSYNC not set and let the stripe be handled again
2343 /* handle a successful check operation, if parity is correct
2344 * we are done. Otherwise update the mismatch count and repair
2345 * parity if !MD_RECOVERY_CHECK
2347 if (sh->ops.zero_sum_result == 0)
2348 /* parity is correct (on disc,
2349 * not in buffer any more)
2351 set_bit(STRIPE_INSYNC, &sh->state);
2353 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2354 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2355 /* don't try to repair!! */
2356 set_bit(STRIPE_INSYNC, &sh->state);
2358 sh->check_state = check_state_compute_run;
2359 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2360 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2361 set_bit(R5_Wantcompute,
2362 &sh->dev[sh->pd_idx].flags);
2363 sh->ops.target = sh->pd_idx;
2368 case check_state_compute_run:
2371 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2372 __func__, sh->check_state,
2373 (unsigned long long) sh->sector);
2379 static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2380 struct stripe_head_state *s,
2381 struct r6_state *r6s, struct page *tmp_page,
2384 int update_p = 0, update_q = 0;
2386 int pd_idx = sh->pd_idx;
2387 int qd_idx = r6s->qd_idx;
2389 set_bit(STRIPE_HANDLE, &sh->state);
2391 BUG_ON(s->failed > 2);
2392 BUG_ON(s->uptodate < disks);
2393 /* Want to check and possibly repair P and Q.
2394 * However there could be one 'failed' device, in which
2395 * case we can only check one of them, possibly using the
2396 * other to generate missing data
2399 /* If !tmp_page, we cannot do the calculations,
2400 * but as we have set STRIPE_HANDLE, we will soon be called
2401 * by stripe_handle with a tmp_page - just wait until then.
2404 if (s->failed == r6s->q_failed) {
2405 /* The only possible failed device holds 'Q', so it
2406 * makes sense to check P (If anything else were failed,
2407 * we would have used P to recreate it).
2409 compute_block_1(sh, pd_idx, 1);
2410 if (!page_is_zero(sh->dev[pd_idx].page)) {
2411 compute_block_1(sh, pd_idx, 0);
2415 if (!r6s->q_failed && s->failed < 2) {
2416 /* q is not failed, and we didn't use it to generate
2417 * anything, so it makes sense to check it
2419 memcpy(page_address(tmp_page),
2420 page_address(sh->dev[qd_idx].page),
2422 compute_parity6(sh, UPDATE_PARITY);
2423 if (memcmp(page_address(tmp_page),
2424 page_address(sh->dev[qd_idx].page),
2425 STRIPE_SIZE) != 0) {
2426 clear_bit(STRIPE_INSYNC, &sh->state);
2430 if (update_p || update_q) {
2431 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2432 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2433 /* don't try to repair!! */
2434 update_p = update_q = 0;
2437 /* now write out any block on a failed drive,
2438 * or P or Q if they need it
2441 if (s->failed == 2) {
2442 dev = &sh->dev[r6s->failed_num[1]];
2444 set_bit(R5_LOCKED, &dev->flags);
2445 set_bit(R5_Wantwrite, &dev->flags);
2447 if (s->failed >= 1) {
2448 dev = &sh->dev[r6s->failed_num[0]];
2450 set_bit(R5_LOCKED, &dev->flags);
2451 set_bit(R5_Wantwrite, &dev->flags);
2455 dev = &sh->dev[pd_idx];
2457 set_bit(R5_LOCKED, &dev->flags);
2458 set_bit(R5_Wantwrite, &dev->flags);
2461 dev = &sh->dev[qd_idx];
2463 set_bit(R5_LOCKED, &dev->flags);
2464 set_bit(R5_Wantwrite, &dev->flags);
2466 clear_bit(STRIPE_DEGRADED, &sh->state);
2468 set_bit(STRIPE_INSYNC, &sh->state);
2472 static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2473 struct r6_state *r6s)
2477 /* We have read all the blocks in this stripe and now we need to
2478 * copy some of them into a target stripe for expand.
2480 struct dma_async_tx_descriptor *tx = NULL;
2481 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2482 for (i = 0; i < sh->disks; i++)
2483 if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
2484 int dd_idx, pd_idx, j;
2485 struct stripe_head *sh2;
2487 sector_t bn = compute_blocknr(sh, i);
2488 sector_t s = raid5_compute_sector(conf, bn, 0,
2490 sh2 = get_active_stripe(conf, s, 0, 1);
2492 /* so far only the early blocks of this stripe
2493 * have been requested. When later blocks
2494 * get requested, we will try again
2497 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2498 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2499 /* must have already done this block */
2500 release_stripe(sh2);
2504 /* place all the copies on one channel */
2505 tx = async_memcpy(sh2->dev[dd_idx].page,
2506 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2507 ASYNC_TX_DEP_ACK, tx, NULL, NULL);
2509 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2510 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2511 for (j = 0; j < conf->raid_disks; j++)
2512 if (j != sh2->pd_idx &&
2513 (!r6s || j != raid6_next_disk(sh2->pd_idx,
2515 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2517 if (j == conf->raid_disks) {
2518 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2519 set_bit(STRIPE_HANDLE, &sh2->state);
2521 release_stripe(sh2);
2524 /* done submitting copies, wait for them to complete */
2527 dma_wait_for_async_tx(tx);
2533 * handle_stripe - do things to a stripe.
2535 * We lock the stripe and then examine the state of various bits
2536 * to see what needs to be done.
2538 * return some read request which now have data
2539 * return some write requests which are safely on disc
2540 * schedule a read on some buffers
2541 * schedule a write of some buffers
2542 * return confirmation of parity correctness
2544 * buffers are taken off read_list or write_list, and bh_cache buffers
2545 * get BH_Lock set before the stripe lock is released.
2549 static bool handle_stripe5(struct stripe_head *sh)
2551 raid5_conf_t *conf = sh->raid_conf;
2552 int disks = sh->disks, i;
2553 struct bio *return_bi = NULL;
2554 struct stripe_head_state s;
2556 mdk_rdev_t *blocked_rdev = NULL;
2559 memset(&s, 0, sizeof(s));
2560 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2561 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2562 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2563 sh->reconstruct_state);
2565 spin_lock(&sh->lock);
2566 clear_bit(STRIPE_HANDLE, &sh->state);
2567 clear_bit(STRIPE_DELAYED, &sh->state);
2569 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2570 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2571 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2573 /* Now to look around and see what can be done */
2575 for (i=disks; i--; ) {
2577 struct r5dev *dev = &sh->dev[i];
2578 clear_bit(R5_Insync, &dev->flags);
2580 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2581 "written %p\n", i, dev->flags, dev->toread, dev->read,
2582 dev->towrite, dev->written);
2584 /* maybe we can request a biofill operation
2586 * new wantfill requests are only permitted while
2587 * ops_complete_biofill is guaranteed to be inactive
2589 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
2590 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
2591 set_bit(R5_Wantfill, &dev->flags);
2593 /* now count some things */
2594 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2595 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
2596 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
2598 if (test_bit(R5_Wantfill, &dev->flags))
2600 else if (dev->toread)
2604 if (!test_bit(R5_OVERWRITE, &dev->flags))
2609 rdev = rcu_dereference(conf->disks[i].rdev);
2610 if (blocked_rdev == NULL &&
2611 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2612 blocked_rdev = rdev;
2613 atomic_inc(&rdev->nr_pending);
2615 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2616 /* The ReadError flag will just be confusing now */
2617 clear_bit(R5_ReadError, &dev->flags);
2618 clear_bit(R5_ReWrite, &dev->flags);
2620 if (!rdev || !test_bit(In_sync, &rdev->flags)
2621 || test_bit(R5_ReadError, &dev->flags)) {
2625 set_bit(R5_Insync, &dev->flags);
2629 if (unlikely(blocked_rdev)) {
2630 if (s.syncing || s.expanding || s.expanded ||
2631 s.to_write || s.written) {
2632 set_bit(STRIPE_HANDLE, &sh->state);
2635 /* There is nothing for the blocked_rdev to block */
2636 rdev_dec_pending(blocked_rdev, conf->mddev);
2637 blocked_rdev = NULL;
2640 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2641 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2642 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2645 pr_debug("locked=%d uptodate=%d to_read=%d"
2646 " to_write=%d failed=%d failed_num=%d\n",
2647 s.locked, s.uptodate, s.to_read, s.to_write,
2648 s.failed, s.failed_num);
2649 /* check if the array has lost two devices and, if so, some requests might
2652 if (s.failed > 1 && s.to_read+s.to_write+s.written)
2653 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
2654 if (s.failed > 1 && s.syncing) {
2655 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2656 clear_bit(STRIPE_SYNCING, &sh->state);
2660 /* might be able to return some write requests if the parity block
2661 * is safe, or on a failed drive
2663 dev = &sh->dev[sh->pd_idx];
2665 ((test_bit(R5_Insync, &dev->flags) &&
2666 !test_bit(R5_LOCKED, &dev->flags) &&
2667 test_bit(R5_UPTODATE, &dev->flags)) ||
2668 (s.failed == 1 && s.failed_num == sh->pd_idx)))
2669 handle_stripe_clean_event(conf, sh, disks, &return_bi);
2671 /* Now we might consider reading some blocks, either to check/generate
2672 * parity, or to satisfy requests
2673 * or to load a block that is being partially written.
2675 if (s.to_read || s.non_overwrite ||
2676 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
2677 handle_stripe_fill5(sh, &s, disks);
2679 /* Now we check to see if any write operations have recently
2683 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
2685 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2686 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
2687 sh->reconstruct_state = reconstruct_state_idle;
2689 /* All the 'written' buffers and the parity block are ready to
2690 * be written back to disk
2692 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2693 for (i = disks; i--; ) {
2695 if (test_bit(R5_LOCKED, &dev->flags) &&
2696 (i == sh->pd_idx || dev->written)) {
2697 pr_debug("Writing block %d\n", i);
2698 set_bit(R5_Wantwrite, &dev->flags);
2701 if (!test_bit(R5_Insync, &dev->flags) ||
2702 (i == sh->pd_idx && s.failed == 0))
2703 set_bit(STRIPE_INSYNC, &sh->state);
2706 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2707 atomic_dec(&conf->preread_active_stripes);
2708 if (atomic_read(&conf->preread_active_stripes) <
2710 md_wakeup_thread(conf->mddev->thread);
2714 /* Now to consider new write requests and what else, if anything
2715 * should be read. We do not handle new writes when:
2716 * 1/ A 'write' operation (copy+xor) is already in flight.
2717 * 2/ A 'check' operation is in flight, as it may clobber the parity
2720 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
2721 handle_stripe_dirtying5(conf, sh, &s, disks);
2723 /* maybe we need to check and possibly fix the parity for this stripe
2724 * Any reads will already have been scheduled, so we just see if enough
2725 * data is available. The parity check is held off while parity
2726 * dependent operations are in flight.
2728 if (sh->check_state ||
2729 (s.syncing && s.locked == 0 &&
2730 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
2731 !test_bit(STRIPE_INSYNC, &sh->state)))
2732 handle_parity_checks5(conf, sh, &s, disks);
2734 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2735 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2736 clear_bit(STRIPE_SYNCING, &sh->state);
2739 /* If the failed drive is just a ReadError, then we might need to progress
2740 * the repair/check process
2742 if (s.failed == 1 && !conf->mddev->ro &&
2743 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2744 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2745 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
2747 dev = &sh->dev[s.failed_num];
2748 if (!test_bit(R5_ReWrite, &dev->flags)) {
2749 set_bit(R5_Wantwrite, &dev->flags);
2750 set_bit(R5_ReWrite, &dev->flags);
2751 set_bit(R5_LOCKED, &dev->flags);
2754 /* let's read it back */
2755 set_bit(R5_Wantread, &dev->flags);
2756 set_bit(R5_LOCKED, &dev->flags);
2761 /* Finish reconstruct operations initiated by the expansion process */
2762 if (sh->reconstruct_state == reconstruct_state_result) {
2763 sh->reconstruct_state = reconstruct_state_idle;
2764 clear_bit(STRIPE_EXPANDING, &sh->state);
2765 for (i = conf->raid_disks; i--; ) {
2766 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2767 set_bit(R5_LOCKED, &sh->dev[i].flags);
2772 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
2773 !sh->reconstruct_state) {
2774 /* Need to write out all blocks after computing parity */
2775 sh->disks = conf->raid_disks;
2776 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, 0);
2777 schedule_reconstruction5(sh, &s, 1, 1);
2778 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
2779 clear_bit(STRIPE_EXPAND_READY, &sh->state);
2780 atomic_dec(&conf->reshape_stripes);
2781 wake_up(&conf->wait_for_overlap);
2782 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2785 if (s.expanding && s.locked == 0 &&
2786 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
2787 handle_stripe_expansion(conf, sh, NULL);
2790 spin_unlock(&sh->lock);
2792 /* wait for this device to become unblocked */
2793 if (unlikely(blocked_rdev))
2794 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2797 raid5_run_ops(sh, s.ops_request);
2801 return_io(return_bi);
2803 return blocked_rdev == NULL;
2806 static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
2808 raid5_conf_t *conf = sh->raid_conf;
2809 int disks = sh->disks;
2810 struct bio *return_bi = NULL;
2811 int i, pd_idx = sh->pd_idx;
2812 struct stripe_head_state s;
2813 struct r6_state r6s;
2814 struct r5dev *dev, *pdev, *qdev;
2815 mdk_rdev_t *blocked_rdev = NULL;
2817 r6s.qd_idx = raid6_next_disk(pd_idx, disks);
2818 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
2819 "pd_idx=%d, qd_idx=%d\n",
2820 (unsigned long long)sh->sector, sh->state,
2821 atomic_read(&sh->count), pd_idx, r6s.qd_idx);
2822 memset(&s, 0, sizeof(s));
2824 spin_lock(&sh->lock);
2825 clear_bit(STRIPE_HANDLE, &sh->state);
2826 clear_bit(STRIPE_DELAYED, &sh->state);
2828 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2829 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2830 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2831 /* Now to look around and see what can be done */
2834 for (i=disks; i--; ) {
2837 clear_bit(R5_Insync, &dev->flags);
2839 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
2840 i, dev->flags, dev->toread, dev->towrite, dev->written);
2841 /* maybe we can reply to a read */
2842 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
2843 struct bio *rbi, *rbi2;
2844 pr_debug("Return read for disc %d\n", i);
2845 spin_lock_irq(&conf->device_lock);
2848 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2849 wake_up(&conf->wait_for_overlap);
2850 spin_unlock_irq(&conf->device_lock);
2851 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2852 copy_data(0, rbi, dev->page, dev->sector);
2853 rbi2 = r5_next_bio(rbi, dev->sector);
2854 spin_lock_irq(&conf->device_lock);
2855 if (!raid5_dec_bi_phys_segments(rbi)) {
2856 rbi->bi_next = return_bi;
2859 spin_unlock_irq(&conf->device_lock);
2864 /* now count some things */
2865 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2866 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
2873 if (!test_bit(R5_OVERWRITE, &dev->flags))
2878 rdev = rcu_dereference(conf->disks[i].rdev);
2879 if (blocked_rdev == NULL &&
2880 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2881 blocked_rdev = rdev;
2882 atomic_inc(&rdev->nr_pending);
2884 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2885 /* The ReadError flag will just be confusing now */
2886 clear_bit(R5_ReadError, &dev->flags);
2887 clear_bit(R5_ReWrite, &dev->flags);
2889 if (!rdev || !test_bit(In_sync, &rdev->flags)
2890 || test_bit(R5_ReadError, &dev->flags)) {
2892 r6s.failed_num[s.failed] = i;
2895 set_bit(R5_Insync, &dev->flags);
2899 if (unlikely(blocked_rdev)) {
2900 if (s.syncing || s.expanding || s.expanded ||
2901 s.to_write || s.written) {
2902 set_bit(STRIPE_HANDLE, &sh->state);
2905 /* There is nothing for the blocked_rdev to block */
2906 rdev_dec_pending(blocked_rdev, conf->mddev);
2907 blocked_rdev = NULL;
2910 pr_debug("locked=%d uptodate=%d to_read=%d"
2911 " to_write=%d failed=%d failed_num=%d,%d\n",
2912 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
2913 r6s.failed_num[0], r6s.failed_num[1]);
2914 /* check if the array has lost >2 devices and, if so, some requests
2915 * might need to be failed
2917 if (s.failed > 2 && s.to_read+s.to_write+s.written)
2918 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
2919 if (s.failed > 2 && s.syncing) {
2920 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2921 clear_bit(STRIPE_SYNCING, &sh->state);
2926 * might be able to return some write requests if the parity blocks
2927 * are safe, or on a failed drive
2929 pdev = &sh->dev[pd_idx];
2930 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
2931 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
2932 qdev = &sh->dev[r6s.qd_idx];
2933 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
2934 || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
2937 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
2938 && !test_bit(R5_LOCKED, &pdev->flags)
2939 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
2940 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
2941 && !test_bit(R5_LOCKED, &qdev->flags)
2942 && test_bit(R5_UPTODATE, &qdev->flags)))))
2943 handle_stripe_clean_event(conf, sh, disks, &return_bi);
2945 /* Now we might consider reading some blocks, either to check/generate
2946 * parity, or to satisfy requests
2947 * or to load a block that is being partially written.
2949 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
2950 (s.syncing && (s.uptodate < disks)) || s.expanding)
2951 handle_stripe_fill6(sh, &s, &r6s, disks);
2953 /* now to consider writing and what else, if anything should be read */
2955 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
2957 /* maybe we need to check and possibly fix the parity for this stripe
2958 * Any reads will already have been scheduled, so we just see if enough
2961 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
2962 handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
2964 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2965 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2966 clear_bit(STRIPE_SYNCING, &sh->state);
2969 /* If the failed drives are just a ReadError, then we might need
2970 * to progress the repair/check process
2972 if (s.failed <= 2 && !conf->mddev->ro)
2973 for (i = 0; i < s.failed; i++) {
2974 dev = &sh->dev[r6s.failed_num[i]];
2975 if (test_bit(R5_ReadError, &dev->flags)
2976 && !test_bit(R5_LOCKED, &dev->flags)
2977 && test_bit(R5_UPTODATE, &dev->flags)
2979 if (!test_bit(R5_ReWrite, &dev->flags)) {
2980 set_bit(R5_Wantwrite, &dev->flags);
2981 set_bit(R5_ReWrite, &dev->flags);
2982 set_bit(R5_LOCKED, &dev->flags);
2984 /* let's read it back */
2985 set_bit(R5_Wantread, &dev->flags);
2986 set_bit(R5_LOCKED, &dev->flags);
2991 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
2992 /* Need to write out all blocks after computing P&Q */
2993 sh->disks = conf->raid_disks;
2994 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, 0);
2995 compute_parity6(sh, RECONSTRUCT_WRITE);
2996 for (i = conf->raid_disks ; i-- ; ) {
2997 set_bit(R5_LOCKED, &sh->dev[i].flags);
2999 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3001 clear_bit(STRIPE_EXPANDING, &sh->state);
3002 } else if (s.expanded) {
3003 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3004 atomic_dec(&conf->reshape_stripes);
3005 wake_up(&conf->wait_for_overlap);
3006 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3009 if (s.expanding && s.locked == 0 &&
3010 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3011 handle_stripe_expansion(conf, sh, &r6s);
3014 spin_unlock(&sh->lock);
3016 /* wait for this device to become unblocked */
3017 if (unlikely(blocked_rdev))
3018 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3022 return_io(return_bi);
3024 return blocked_rdev == NULL;
3027 /* returns true if the stripe was handled */
3028 static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
3030 if (sh->raid_conf->level == 6)
3031 return handle_stripe6(sh, tmp_page);
3033 return handle_stripe5(sh);
3038 static void raid5_activate_delayed(raid5_conf_t *conf)
3040 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3041 while (!list_empty(&conf->delayed_list)) {
3042 struct list_head *l = conf->delayed_list.next;
3043 struct stripe_head *sh;
3044 sh = list_entry(l, struct stripe_head, lru);
3046 clear_bit(STRIPE_DELAYED, &sh->state);
3047 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3048 atomic_inc(&conf->preread_active_stripes);
3049 list_add_tail(&sh->lru, &conf->hold_list);
3052 blk_plug_device(conf->mddev->queue);
3055 static void activate_bit_delay(raid5_conf_t *conf)
3057 /* device_lock is held */
3058 struct list_head head;
3059 list_add(&head, &conf->bitmap_list);
3060 list_del_init(&conf->bitmap_list);
3061 while (!list_empty(&head)) {
3062 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3063 list_del_init(&sh->lru);
3064 atomic_inc(&sh->count);
3065 __release_stripe(conf, sh);
3069 static void unplug_slaves(mddev_t *mddev)
3071 raid5_conf_t *conf = mddev_to_conf(mddev);
3075 for (i=0; i<mddev->raid_disks; i++) {
3076 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
3077 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
3078 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
3080 atomic_inc(&rdev->nr_pending);
3083 blk_unplug(r_queue);
3085 rdev_dec_pending(rdev, mddev);
3092 static void raid5_unplug_device(struct request_queue *q)
3094 mddev_t *mddev = q->queuedata;
3095 raid5_conf_t *conf = mddev_to_conf(mddev);
3096 unsigned long flags;
3098 spin_lock_irqsave(&conf->device_lock, flags);
3100 if (blk_remove_plug(q)) {
3102 raid5_activate_delayed(conf);
3104 md_wakeup_thread(mddev->thread);
3106 spin_unlock_irqrestore(&conf->device_lock, flags);
3108 unplug_slaves(mddev);
3111 static int raid5_congested(void *data, int bits)
3113 mddev_t *mddev = data;
3114 raid5_conf_t *conf = mddev_to_conf(mddev);
3116 /* No difference between reads and writes. Just check
3117 * how busy the stripe_cache is
3119 if (conf->inactive_blocked)
3123 if (list_empty_careful(&conf->inactive_list))
3129 /* We want read requests to align with chunks where possible,
3130 * but write requests don't need to.
3132 static int raid5_mergeable_bvec(struct request_queue *q,
3133 struct bvec_merge_data *bvm,
3134 struct bio_vec *biovec)
3136 mddev_t *mddev = q->queuedata;
3137 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
3139 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3140 unsigned int bio_sectors = bvm->bi_size >> 9;
3142 if ((bvm->bi_rw & 1) == WRITE)
3143 return biovec->bv_len; /* always allow writes to be mergeable */
3145 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3146 if (max < 0) max = 0;
3147 if (max <= biovec->bv_len && bio_sectors == 0)
3148 return biovec->bv_len;
3154 static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3156 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3157 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3158 unsigned int bio_sectors = bio->bi_size >> 9;
3160 return chunk_sectors >=
3161 ((sector & (chunk_sectors - 1)) + bio_sectors);
3165 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3166 * later sampled by raid5d.
3168 static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3170 unsigned long flags;
3172 spin_lock_irqsave(&conf->device_lock, flags);
3174 bi->bi_next = conf->retry_read_aligned_list;
3175 conf->retry_read_aligned_list = bi;
3177 spin_unlock_irqrestore(&conf->device_lock, flags);
3178 md_wakeup_thread(conf->mddev->thread);
3182 static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3186 bi = conf->retry_read_aligned;
3188 conf->retry_read_aligned = NULL;
3191 bi = conf->retry_read_aligned_list;
3193 conf->retry_read_aligned_list = bi->bi_next;
3196 * this sets the active strip count to 1 and the processed
3197 * strip count to zero (upper 8 bits)
3199 bi->bi_phys_segments = 1; /* biased count of active stripes */
3207 * The "raid5_align_endio" should check if the read succeeded and if it
3208 * did, call bio_endio on the original bio (having bio_put the new bio
3210 * If the read failed..
3212 static void raid5_align_endio(struct bio *bi, int error)
3214 struct bio* raid_bi = bi->bi_private;
3217 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3222 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3223 conf = mddev_to_conf(mddev);
3224 rdev = (void*)raid_bi->bi_next;
3225 raid_bi->bi_next = NULL;
3227 rdev_dec_pending(rdev, conf->mddev);
3229 if (!error && uptodate) {
3230 bio_endio(raid_bi, 0);
3231 if (atomic_dec_and_test(&conf->active_aligned_reads))
3232 wake_up(&conf->wait_for_stripe);
3237 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3239 add_bio_to_retry(raid_bi, conf);
3242 static int bio_fits_rdev(struct bio *bi)
3244 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
3246 if ((bi->bi_size>>9) > q->max_sectors)
3248 blk_recount_segments(q, bi);
3249 if (bi->bi_phys_segments > q->max_phys_segments)
3252 if (q->merge_bvec_fn)
3253 /* it's too hard to apply the merge_bvec_fn at this stage,
3262 static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
3264 mddev_t *mddev = q->queuedata;
3265 raid5_conf_t *conf = mddev_to_conf(mddev);
3266 unsigned int dd_idx, pd_idx;
3267 struct bio* align_bi;
3270 if (!in_chunk_boundary(mddev, raid_bio)) {
3271 pr_debug("chunk_aligned_read : non aligned\n");
3275 * use bio_clone to make a copy of the bio
3277 align_bi = bio_clone(raid_bio, GFP_NOIO);
3281 * set bi_end_io to a new function, and set bi_private to the
3284 align_bi->bi_end_io = raid5_align_endio;
3285 align_bi->bi_private = raid_bio;
3289 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3294 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3295 if (rdev && test_bit(In_sync, &rdev->flags)) {
3296 atomic_inc(&rdev->nr_pending);
3298 raid_bio->bi_next = (void*)rdev;
3299 align_bi->bi_bdev = rdev->bdev;
3300 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3301 align_bi->bi_sector += rdev->data_offset;
3303 if (!bio_fits_rdev(align_bi)) {
3304 /* too big in some way */
3306 rdev_dec_pending(rdev, mddev);
3310 spin_lock_irq(&conf->device_lock);
3311 wait_event_lock_irq(conf->wait_for_stripe,
3313 conf->device_lock, /* nothing */);
3314 atomic_inc(&conf->active_aligned_reads);
3315 spin_unlock_irq(&conf->device_lock);
3317 generic_make_request(align_bi);
3326 /* __get_priority_stripe - get the next stripe to process
3328 * Full stripe writes are allowed to pass preread active stripes up until
3329 * the bypass_threshold is exceeded. In general the bypass_count
3330 * increments when the handle_list is handled before the hold_list; however, it
3331 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3332 * stripe with in flight i/o. The bypass_count will be reset when the
3333 * head of the hold_list has changed, i.e. the head was promoted to the
3336 static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3338 struct stripe_head *sh;
3340 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3342 list_empty(&conf->handle_list) ? "empty" : "busy",
3343 list_empty(&conf->hold_list) ? "empty" : "busy",
3344 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3346 if (!list_empty(&conf->handle_list)) {
3347 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3349 if (list_empty(&conf->hold_list))
3350 conf->bypass_count = 0;
3351 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3352 if (conf->hold_list.next == conf->last_hold)
3353 conf->bypass_count++;
3355 conf->last_hold = conf->hold_list.next;
3356 conf->bypass_count -= conf->bypass_threshold;
3357 if (conf->bypass_count < 0)
3358 conf->bypass_count = 0;
3361 } else if (!list_empty(&conf->hold_list) &&
3362 ((conf->bypass_threshold &&
3363 conf->bypass_count > conf->bypass_threshold) ||
3364 atomic_read(&conf->pending_full_writes) == 0)) {
3365 sh = list_entry(conf->hold_list.next,
3367 conf->bypass_count -= conf->bypass_threshold;
3368 if (conf->bypass_count < 0)
3369 conf->bypass_count = 0;
3373 list_del_init(&sh->lru);
3374 atomic_inc(&sh->count);
3375 BUG_ON(atomic_read(&sh->count) != 1);
3379 static int make_request(struct request_queue *q, struct bio * bi)
3381 mddev_t *mddev = q->queuedata;
3382 raid5_conf_t *conf = mddev_to_conf(mddev);
3383 unsigned int dd_idx, pd_idx;
3384 sector_t new_sector;
3385 sector_t logical_sector, last_sector;
3386 struct stripe_head *sh;
3387 const int rw = bio_data_dir(bi);
3390 if (unlikely(bio_barrier(bi))) {
3391 bio_endio(bi, -EOPNOTSUPP);
3395 md_write_start(mddev, bi);
3397 cpu = part_stat_lock();
3398 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3399 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3404 mddev->reshape_position == MaxSector &&
3405 chunk_aligned_read(q,bi))
3408 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3409 last_sector = bi->bi_sector + (bi->bi_size>>9);
3411 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
3413 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3415 int disks, data_disks;
3420 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
3421 if (likely(conf->expand_progress == MaxSector))
3422 disks = conf->raid_disks;
3424 /* spinlock is needed as expand_progress may be
3425 * 64bit on a 32bit platform, and so it might be
3426 * possible to see a half-updated value
3427 * Ofcourse expand_progress could change after
3428 * the lock is dropped, so once we get a reference
3429 * to the stripe that we think it is, we will have
3432 spin_lock_irq(&conf->device_lock);
3433 disks = conf->raid_disks;
3434 if (logical_sector >= conf->expand_progress) {
3435 disks = conf->previous_raid_disks;
3438 if (logical_sector >= conf->expand_lo) {
3439 spin_unlock_irq(&conf->device_lock);
3444 spin_unlock_irq(&conf->device_lock);
3446 data_disks = disks - conf->max_degraded;
3448 new_sector = raid5_compute_sector(conf, logical_sector,
3451 pr_debug("raid5: make_request, sector %llu logical %llu\n",
3452 (unsigned long long)new_sector,
3453 (unsigned long long)logical_sector);
3455 sh = get_active_stripe(conf, new_sector, previous,
3456 (bi->bi_rw&RWA_MASK));
3458 if (unlikely(conf->expand_progress != MaxSector)) {
3459 /* expansion might have moved on while waiting for a
3460 * stripe, so we must do the range check again.
3461 * Expansion could still move past after this
3462 * test, but as we are holding a reference to
3463 * 'sh', we know that if that happens,
3464 * STRIPE_EXPANDING will get set and the expansion
3465 * won't proceed until we finish with the stripe.
3468 spin_lock_irq(&conf->device_lock);
3469 if (logical_sector < conf->expand_progress &&
3470 disks == conf->previous_raid_disks)
3471 /* mismatch, need to try again */
3473 spin_unlock_irq(&conf->device_lock);
3479 /* FIXME what if we get a false positive because these
3480 * are being updated.
3482 if (logical_sector >= mddev->suspend_lo &&
3483 logical_sector < mddev->suspend_hi) {
3489 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3490 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3491 /* Stripe is busy expanding or
3492 * add failed due to overlap. Flush everything
3495 raid5_unplug_device(mddev->queue);
3500 finish_wait(&conf->wait_for_overlap, &w);
3501 set_bit(STRIPE_HANDLE, &sh->state);
3502 clear_bit(STRIPE_DELAYED, &sh->state);
3505 /* cannot get stripe for read-ahead, just give-up */
3506 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3507 finish_wait(&conf->wait_for_overlap, &w);
3512 spin_lock_irq(&conf->device_lock);
3513 remaining = raid5_dec_bi_phys_segments(bi);
3514 spin_unlock_irq(&conf->device_lock);
3515 if (remaining == 0) {
3518 md_write_end(mddev);
3525 static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
3527 /* reshaping is quite different to recovery/resync so it is
3528 * handled quite separately ... here.
3530 * On each call to sync_request, we gather one chunk worth of
3531 * destination stripes and flag them as expanding.
3532 * Then we find all the source stripes and request reads.
3533 * As the reads complete, handle_stripe will copy the data
3534 * into the destination stripe and release that stripe.
3536 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3537 struct stripe_head *sh;
3539 sector_t first_sector, last_sector;
3540 int raid_disks = conf->previous_raid_disks;
3541 int data_disks = raid_disks - conf->max_degraded;
3542 int new_data_disks = conf->raid_disks - conf->max_degraded;
3545 sector_t writepos, safepos, gap;
3547 if (sector_nr == 0 &&
3548 conf->expand_progress != 0) {
3549 /* restarting in the middle, skip the initial sectors */
3550 sector_nr = conf->expand_progress;
3551 sector_div(sector_nr, new_data_disks);
3556 /* we update the metadata when there is more than 3Meg
3557 * in the block range (that is rather arbitrary, should
3558 * probably be time based) or when the data about to be
3559 * copied would over-write the source of the data at
3560 * the front of the range.
3561 * i.e. one new_stripe forward from expand_progress new_maps
3562 * to after where expand_lo old_maps to
3564 writepos = conf->expand_progress +
3565 conf->chunk_size/512*(new_data_disks);
3566 sector_div(writepos, new_data_disks);
3567 safepos = conf->expand_lo;
3568 sector_div(safepos, data_disks);
3569 gap = conf->expand_progress - conf->expand_lo;
3571 if (writepos >= safepos ||
3572 gap > (new_data_disks)*3000*2 /*3Meg*/) {
3573 /* Cannot proceed until we've updated the superblock... */
3574 wait_event(conf->wait_for_overlap,
3575 atomic_read(&conf->reshape_stripes)==0);
3576 mddev->reshape_position = conf->expand_progress;
3577 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3578 md_wakeup_thread(mddev->thread);
3579 wait_event(mddev->sb_wait, mddev->flags == 0 ||
3580 kthread_should_stop());
3581 spin_lock_irq(&conf->device_lock);
3582 conf->expand_lo = mddev->reshape_position;
3583 spin_unlock_irq(&conf->device_lock);
3584 wake_up(&conf->wait_for_overlap);
3587 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
3590 sh = get_active_stripe(conf, sector_nr+i, 0, 0);
3591 set_bit(STRIPE_EXPANDING, &sh->state);
3592 atomic_inc(&conf->reshape_stripes);
3593 /* If any of this stripe is beyond the end of the old
3594 * array, then we need to zero those blocks
3596 for (j=sh->disks; j--;) {
3598 if (j == sh->pd_idx)
3600 if (conf->level == 6 &&
3601 j == raid6_next_disk(sh->pd_idx, sh->disks))
3603 s = compute_blocknr(sh, j);
3604 if (s < mddev->array_sectors) {
3608 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3609 set_bit(R5_Expanded, &sh->dev[j].flags);
3610 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3613 set_bit(STRIPE_EXPAND_READY, &sh->state);
3614 set_bit(STRIPE_HANDLE, &sh->state);
3618 spin_lock_irq(&conf->device_lock);
3619 conf->expand_progress = (sector_nr + i) * new_data_disks;
3620 spin_unlock_irq(&conf->device_lock);
3621 /* Ok, those stripe are ready. We can start scheduling
3622 * reads on the source stripes.
3623 * The source stripes are determined by mapping the first and last
3624 * block on the destination stripes.
3627 raid5_compute_sector(conf, sector_nr*(new_data_disks),
3628 1, &dd_idx, &pd_idx);
3630 raid5_compute_sector(conf, ((sector_nr+conf->chunk_size/512)
3631 *(new_data_disks) - 1),
3632 1, &dd_idx, &pd_idx);
3633 if (last_sector >= mddev->dev_sectors)
3634 last_sector = mddev->dev_sectors - 1;
3635 while (first_sector <= last_sector) {
3636 sh = get_active_stripe(conf, first_sector, 1, 0);
3637 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3638 set_bit(STRIPE_HANDLE, &sh->state);
3640 first_sector += STRIPE_SECTORS;
3642 /* If this takes us to the resync_max point where we have to pause,
3643 * then we need to write out the superblock.
3645 sector_nr += conf->chunk_size>>9;
3646 if (sector_nr >= mddev->resync_max) {
3647 /* Cannot proceed until we've updated the superblock... */
3648 wait_event(conf->wait_for_overlap,
3649 atomic_read(&conf->reshape_stripes) == 0);
3650 mddev->reshape_position = conf->expand_progress;
3651 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3652 md_wakeup_thread(mddev->thread);
3653 wait_event(mddev->sb_wait,
3654 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
3655 || kthread_should_stop());
3656 spin_lock_irq(&conf->device_lock);
3657 conf->expand_lo = mddev->reshape_position;
3658 spin_unlock_irq(&conf->device_lock);
3659 wake_up(&conf->wait_for_overlap);
3661 return conf->chunk_size>>9;
3664 /* FIXME go_faster isn't used */
3665 static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3667 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3668 struct stripe_head *sh;
3669 sector_t max_sector = mddev->dev_sectors;
3671 int still_degraded = 0;
3674 if (sector_nr >= max_sector) {
3675 /* just being told to finish up .. nothing much to do */
3676 unplug_slaves(mddev);
3677 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3682 if (mddev->curr_resync < max_sector) /* aborted */
3683 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3685 else /* completed sync */
3687 bitmap_close_sync(mddev->bitmap);
3692 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3693 return reshape_request(mddev, sector_nr, skipped);
3695 /* No need to check resync_max as we never do more than one
3696 * stripe, and as resync_max will always be on a chunk boundary,
3697 * if the check in md_do_sync didn't fire, there is no chance
3698 * of overstepping resync_max here
3701 /* if there is too many failed drives and we are trying
3702 * to resync, then assert that we are finished, because there is
3703 * nothing we can do.
3705 if (mddev->degraded >= conf->max_degraded &&
3706 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3707 sector_t rv = mddev->dev_sectors - sector_nr;
3711 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
3712 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
3713 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3714 /* we can skip this block, and probably more */
3715 sync_blocks /= STRIPE_SECTORS;
3717 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3721 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3723 sh = get_active_stripe(conf, sector_nr, 0, 1);
3725 sh = get_active_stripe(conf, sector_nr, 0, 0);
3726 /* make sure we don't swamp the stripe cache if someone else
3727 * is trying to get access
3729 schedule_timeout_uninterruptible(1);
3731 /* Need to check if array will still be degraded after recovery/resync
3732 * We don't need to check the 'failed' flag as when that gets set,
3735 for (i=0; i<mddev->raid_disks; i++)
3736 if (conf->disks[i].rdev == NULL)
3739 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3741 spin_lock(&sh->lock);
3742 set_bit(STRIPE_SYNCING, &sh->state);
3743 clear_bit(STRIPE_INSYNC, &sh->state);
3744 spin_unlock(&sh->lock);
3746 /* wait for any blocked device to be handled */
3747 while(unlikely(!handle_stripe(sh, NULL)))
3751 return STRIPE_SECTORS;
3754 static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3756 /* We may not be able to submit a whole bio at once as there
3757 * may not be enough stripe_heads available.
3758 * We cannot pre-allocate enough stripe_heads as we may need
3759 * more than exist in the cache (if we allow ever large chunks).
3760 * So we do one stripe head at a time and record in
3761 * ->bi_hw_segments how many have been done.
3763 * We *know* that this entire raid_bio is in one chunk, so
3764 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3766 struct stripe_head *sh;
3768 sector_t sector, logical_sector, last_sector;
3773 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3774 sector = raid5_compute_sector(conf, logical_sector,
3775 0, &dd_idx, &pd_idx);
3776 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3778 for (; logical_sector < last_sector;
3779 logical_sector += STRIPE_SECTORS,
3780 sector += STRIPE_SECTORS,
3783 if (scnt < raid5_bi_hw_segments(raid_bio))
3784 /* already done this stripe */
3787 sh = get_active_stripe(conf, sector, 0, 1);
3790 /* failed to get a stripe - must wait */
3791 raid5_set_bi_hw_segments(raid_bio, scnt);
3792 conf->retry_read_aligned = raid_bio;
3796 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
3797 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
3799 raid5_set_bi_hw_segments(raid_bio, scnt);
3800 conf->retry_read_aligned = raid_bio;
3804 handle_stripe(sh, NULL);
3808 spin_lock_irq(&conf->device_lock);
3809 remaining = raid5_dec_bi_phys_segments(raid_bio);
3810 spin_unlock_irq(&conf->device_lock);
3812 bio_endio(raid_bio, 0);
3813 if (atomic_dec_and_test(&conf->active_aligned_reads))
3814 wake_up(&conf->wait_for_stripe);
3821 * This is our raid5 kernel thread.
3823 * We scan the hash table for stripes which can be handled now.
3824 * During the scan, completed stripes are saved for us by the interrupt
3825 * handler, so that they will not have to wait for our next wakeup.
3827 static void raid5d(mddev_t *mddev)
3829 struct stripe_head *sh;
3830 raid5_conf_t *conf = mddev_to_conf(mddev);
3833 pr_debug("+++ raid5d active\n");
3835 md_check_recovery(mddev);
3838 spin_lock_irq(&conf->device_lock);
3842 if (conf->seq_flush != conf->seq_write) {
3843 int seq = conf->seq_flush;
3844 spin_unlock_irq(&conf->device_lock);
3845 bitmap_unplug(mddev->bitmap);
3846 spin_lock_irq(&conf->device_lock);
3847 conf->seq_write = seq;
3848 activate_bit_delay(conf);
3851 while ((bio = remove_bio_from_retry(conf))) {
3853 spin_unlock_irq(&conf->device_lock);
3854 ok = retry_aligned_read(conf, bio);
3855 spin_lock_irq(&conf->device_lock);
3861 sh = __get_priority_stripe(conf);
3865 spin_unlock_irq(&conf->device_lock);
3868 handle_stripe(sh, conf->spare_page);
3871 spin_lock_irq(&conf->device_lock);
3873 pr_debug("%d stripes handled\n", handled);
3875 spin_unlock_irq(&conf->device_lock);
3877 async_tx_issue_pending_all();
3878 unplug_slaves(mddev);
3880 pr_debug("--- raid5d inactive\n");
3884 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
3886 raid5_conf_t *conf = mddev_to_conf(mddev);
3888 return sprintf(page, "%d\n", conf->max_nr_stripes);
3894 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
3896 raid5_conf_t *conf = mddev_to_conf(mddev);
3900 if (len >= PAGE_SIZE)
3905 if (strict_strtoul(page, 10, &new))
3907 if (new <= 16 || new > 32768)
3909 while (new < conf->max_nr_stripes) {
3910 if (drop_one_stripe(conf))
3911 conf->max_nr_stripes--;
3915 err = md_allow_write(mddev);
3918 while (new > conf->max_nr_stripes) {
3919 if (grow_one_stripe(conf))
3920 conf->max_nr_stripes++;
3926 static struct md_sysfs_entry
3927 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3928 raid5_show_stripe_cache_size,
3929 raid5_store_stripe_cache_size);
3932 raid5_show_preread_threshold(mddev_t *mddev, char *page)
3934 raid5_conf_t *conf = mddev_to_conf(mddev);
3936 return sprintf(page, "%d\n", conf->bypass_threshold);
3942 raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
3944 raid5_conf_t *conf = mddev_to_conf(mddev);
3946 if (len >= PAGE_SIZE)
3951 if (strict_strtoul(page, 10, &new))
3953 if (new > conf->max_nr_stripes)
3955 conf->bypass_threshold = new;
3959 static struct md_sysfs_entry
3960 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
3962 raid5_show_preread_threshold,
3963 raid5_store_preread_threshold);
3966 stripe_cache_active_show(mddev_t *mddev, char *page)
3968 raid5_conf_t *conf = mddev_to_conf(mddev);
3970 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3975 static struct md_sysfs_entry
3976 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3978 static struct attribute *raid5_attrs[] = {
3979 &raid5_stripecache_size.attr,
3980 &raid5_stripecache_active.attr,
3981 &raid5_preread_bypass_threshold.attr,
3984 static struct attribute_group raid5_attrs_group = {
3986 .attrs = raid5_attrs,
3989 static int run(mddev_t *mddev)
3992 int raid_disk, memory;
3994 struct disk_info *disk;
3995 int working_disks = 0;
3997 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3998 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
3999 mdname(mddev), mddev->level);
4003 if (mddev->chunk_size < PAGE_SIZE) {
4004 printk(KERN_ERR "md/raid5: chunk_size must be at least "
4005 "PAGE_SIZE but %d < %ld\n",
4006 mddev->chunk_size, PAGE_SIZE);
4010 if (mddev->reshape_position != MaxSector) {
4011 /* Check that we can continue the reshape.
4012 * Currently only disks can change, it must
4013 * increase, and we must be past the point where
4014 * a stripe over-writes itself
4016 sector_t here_new, here_old;
4018 int max_degraded = (mddev->level == 5 ? 1 : 2);
4020 if (mddev->new_level != mddev->level ||
4021 mddev->new_layout != mddev->layout ||
4022 mddev->new_chunk != mddev->chunk_size) {
4023 printk(KERN_ERR "raid5: %s: unsupported reshape "
4024 "required - aborting.\n",
4028 if (mddev->delta_disks <= 0) {
4029 printk(KERN_ERR "raid5: %s: unsupported reshape "
4030 "(reduce disks) required - aborting.\n",
4034 old_disks = mddev->raid_disks - mddev->delta_disks;
4035 /* reshape_position must be on a new-stripe boundary, and one
4036 * further up in new geometry must map after here in old
4039 here_new = mddev->reshape_position;
4040 if (sector_div(here_new, (mddev->chunk_size>>9)*
4041 (mddev->raid_disks - max_degraded))) {
4042 printk(KERN_ERR "raid5: reshape_position not "
4043 "on a stripe boundary\n");
4046 /* here_new is the stripe we will write to */
4047 here_old = mddev->reshape_position;
4048 sector_div(here_old, (mddev->chunk_size>>9)*
4049 (old_disks-max_degraded));
4050 /* here_old is the first stripe that we might need to read
4052 if (here_new >= here_old) {
4053 /* Reading from the same stripe as writing to - bad */
4054 printk(KERN_ERR "raid5: reshape_position too early for "
4055 "auto-recovery - aborting.\n");
4058 printk(KERN_INFO "raid5: reshape will continue\n");
4059 /* OK, we should be able to continue; */
4063 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
4064 if ((conf = mddev->private) == NULL)
4066 if (mddev->reshape_position == MaxSector) {
4067 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
4069 conf->raid_disks = mddev->raid_disks;
4070 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4073 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4078 conf->mddev = mddev;
4080 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4083 if (mddev->level == 6) {
4084 conf->spare_page = alloc_page(GFP_KERNEL);
4085 if (!conf->spare_page)
4088 spin_lock_init(&conf->device_lock);
4089 mddev->queue->queue_lock = &conf->device_lock;
4090 init_waitqueue_head(&conf->wait_for_stripe);
4091 init_waitqueue_head(&conf->wait_for_overlap);
4092 INIT_LIST_HEAD(&conf->handle_list);
4093 INIT_LIST_HEAD(&conf->hold_list);
4094 INIT_LIST_HEAD(&conf->delayed_list);
4095 INIT_LIST_HEAD(&conf->bitmap_list);
4096 INIT_LIST_HEAD(&conf->inactive_list);
4097 atomic_set(&conf->active_stripes, 0);
4098 atomic_set(&conf->preread_active_stripes, 0);
4099 atomic_set(&conf->active_aligned_reads, 0);
4100 conf->bypass_threshold = BYPASS_THRESHOLD;
4102 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4104 list_for_each_entry(rdev, &mddev->disks, same_set) {
4105 raid_disk = rdev->raid_disk;
4106 if (raid_disk >= conf->raid_disks
4109 disk = conf->disks + raid_disk;
4113 if (test_bit(In_sync, &rdev->flags)) {
4114 char b[BDEVNAME_SIZE];
4115 printk(KERN_INFO "raid5: device %s operational as raid"
4116 " disk %d\n", bdevname(rdev->bdev,b),
4120 /* Cannot rely on bitmap to complete recovery */
4125 * 0 for a fully functional array, 1 or 2 for a degraded array.
4127 mddev->degraded = conf->raid_disks - working_disks;
4128 conf->mddev = mddev;
4129 conf->chunk_size = mddev->chunk_size;
4130 conf->level = mddev->level;
4131 if (conf->level == 6)
4132 conf->max_degraded = 2;
4134 conf->max_degraded = 1;
4135 conf->algorithm = mddev->layout;
4136 conf->max_nr_stripes = NR_STRIPES;
4137 conf->expand_progress = mddev->reshape_position;
4139 /* device size must be a multiple of chunk size */
4140 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4141 mddev->resync_max_sectors = mddev->dev_sectors;
4143 if (conf->level == 6 && conf->raid_disks < 4) {
4144 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4145 mdname(mddev), conf->raid_disks);
4148 if (!conf->chunk_size || conf->chunk_size % 4) {
4149 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4150 conf->chunk_size, mdname(mddev));
4153 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
4155 "raid5: unsupported parity algorithm %d for %s\n",
4156 conf->algorithm, mdname(mddev));
4159 if (mddev->degraded > conf->max_degraded) {
4160 printk(KERN_ERR "raid5: not enough operational devices for %s"
4161 " (%d/%d failed)\n",
4162 mdname(mddev), mddev->degraded, conf->raid_disks);
4166 if (mddev->degraded > 0 &&
4167 mddev->recovery_cp != MaxSector) {
4168 if (mddev->ok_start_degraded)
4170 "raid5: starting dirty degraded array: %s"
4171 "- data corruption possible.\n",
4175 "raid5: cannot start dirty degraded array for %s\n",
4182 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4183 if (!mddev->thread) {
4185 "raid5: couldn't allocate thread for %s\n",
4190 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4191 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4192 if (grow_stripes(conf, conf->max_nr_stripes)) {
4194 "raid5: couldn't allocate %dkB for buffers\n", memory);
4195 shrink_stripes(conf);
4196 md_unregister_thread(mddev->thread);
4199 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4200 memory, mdname(mddev));
4202 if (mddev->degraded == 0)
4203 printk("raid5: raid level %d set %s active with %d out of %d"
4204 " devices, algorithm %d\n", conf->level, mdname(mddev),
4205 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4208 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4209 " out of %d devices, algorithm %d\n", conf->level,
4210 mdname(mddev), mddev->raid_disks - mddev->degraded,
4211 mddev->raid_disks, conf->algorithm);
4213 print_raid5_conf(conf);
4215 if (conf->expand_progress != MaxSector) {
4216 printk("...ok start reshape thread\n");
4217 conf->expand_lo = conf->expand_progress;
4218 atomic_set(&conf->reshape_stripes, 0);
4219 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4220 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4221 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4222 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4223 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4227 /* read-ahead size must cover two whole stripes, which is
4228 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4231 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4232 int stripe = data_disks *
4233 (mddev->chunk_size / PAGE_SIZE);
4234 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4235 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4238 /* Ok, everything is just fine now */
4239 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4241 "raid5: failed to create sysfs attributes for %s\n",
4244 mddev->queue->unplug_fn = raid5_unplug_device;
4245 mddev->queue->backing_dev_info.congested_data = mddev;
4246 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
4248 mddev->array_sectors = mddev->dev_sectors *
4249 (conf->previous_raid_disks - conf->max_degraded);
4251 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4256 print_raid5_conf(conf);
4257 safe_put_page(conf->spare_page);
4259 kfree(conf->stripe_hashtbl);
4262 mddev->private = NULL;
4263 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4269 static int stop(mddev_t *mddev)
4271 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4273 md_unregister_thread(mddev->thread);
4274 mddev->thread = NULL;
4275 shrink_stripes(conf);
4276 kfree(conf->stripe_hashtbl);
4277 mddev->queue->backing_dev_info.congested_fn = NULL;
4278 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
4279 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
4282 mddev->private = NULL;
4287 static void print_sh(struct seq_file *seq, struct stripe_head *sh)
4291 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4292 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4293 seq_printf(seq, "sh %llu, count %d.\n",
4294 (unsigned long long)sh->sector, atomic_read(&sh->count));
4295 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
4296 for (i = 0; i < sh->disks; i++) {
4297 seq_printf(seq, "(cache%d: %p %ld) ",
4298 i, sh->dev[i].page, sh->dev[i].flags);
4300 seq_printf(seq, "\n");
4303 static void printall(struct seq_file *seq, raid5_conf_t *conf)
4305 struct stripe_head *sh;
4306 struct hlist_node *hn;
4309 spin_lock_irq(&conf->device_lock);
4310 for (i = 0; i < NR_HASH; i++) {
4311 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
4312 if (sh->raid_conf != conf)
4317 spin_unlock_irq(&conf->device_lock);
4321 static void status(struct seq_file *seq, mddev_t *mddev)
4323 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4326 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
4327 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
4328 for (i = 0; i < conf->raid_disks; i++)
4329 seq_printf (seq, "%s",
4330 conf->disks[i].rdev &&
4331 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
4332 seq_printf (seq, "]");
4334 seq_printf (seq, "\n");
4335 printall(seq, conf);
4339 static void print_raid5_conf (raid5_conf_t *conf)
4342 struct disk_info *tmp;
4344 printk("RAID5 conf printout:\n");
4346 printk("(conf==NULL)\n");
4349 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4350 conf->raid_disks - conf->mddev->degraded);
4352 for (i = 0; i < conf->raid_disks; i++) {
4353 char b[BDEVNAME_SIZE];
4354 tmp = conf->disks + i;
4356 printk(" disk %d, o:%d, dev:%s\n",
4357 i, !test_bit(Faulty, &tmp->rdev->flags),
4358 bdevname(tmp->rdev->bdev,b));
4362 static int raid5_spare_active(mddev_t *mddev)
4365 raid5_conf_t *conf = mddev->private;
4366 struct disk_info *tmp;
4368 for (i = 0; i < conf->raid_disks; i++) {
4369 tmp = conf->disks + i;
4371 && !test_bit(Faulty, &tmp->rdev->flags)
4372 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4373 unsigned long flags;
4374 spin_lock_irqsave(&conf->device_lock, flags);
4376 spin_unlock_irqrestore(&conf->device_lock, flags);
4379 print_raid5_conf(conf);
4383 static int raid5_remove_disk(mddev_t *mddev, int number)
4385 raid5_conf_t *conf = mddev->private;
4388 struct disk_info *p = conf->disks + number;
4390 print_raid5_conf(conf);
4393 if (test_bit(In_sync, &rdev->flags) ||
4394 atomic_read(&rdev->nr_pending)) {
4398 /* Only remove non-faulty devices if recovery
4401 if (!test_bit(Faulty, &rdev->flags) &&
4402 mddev->degraded <= conf->max_degraded) {
4408 if (atomic_read(&rdev->nr_pending)) {
4409 /* lost the race, try later */
4416 print_raid5_conf(conf);
4420 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4422 raid5_conf_t *conf = mddev->private;
4425 struct disk_info *p;
4427 int last = conf->raid_disks - 1;
4429 if (mddev->degraded > conf->max_degraded)
4430 /* no point adding a device */
4433 if (rdev->raid_disk >= 0)
4434 first = last = rdev->raid_disk;
4437 * find the disk ... but prefer rdev->saved_raid_disk
4440 if (rdev->saved_raid_disk >= 0 &&
4441 rdev->saved_raid_disk >= first &&
4442 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4443 disk = rdev->saved_raid_disk;
4446 for ( ; disk <= last ; disk++)
4447 if ((p=conf->disks + disk)->rdev == NULL) {
4448 clear_bit(In_sync, &rdev->flags);
4449 rdev->raid_disk = disk;
4451 if (rdev->saved_raid_disk != disk)
4453 rcu_assign_pointer(p->rdev, rdev);
4456 print_raid5_conf(conf);
4460 static int raid5_resize(mddev_t *mddev, sector_t sectors)
4462 /* no resync is happening, and there is enough space
4463 * on all devices, so we can resize.
4464 * We need to make sure resync covers any new space.
4465 * If the array is shrinking we should possibly wait until
4466 * any io in the removed space completes, but it hardly seems
4469 raid5_conf_t *conf = mddev_to_conf(mddev);
4471 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
4472 mddev->array_sectors = sectors * (mddev->raid_disks
4473 - conf->max_degraded);
4474 set_capacity(mddev->gendisk, mddev->array_sectors);
4476 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
4477 mddev->recovery_cp = mddev->dev_sectors;
4478 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4480 mddev->dev_sectors = sectors;
4481 mddev->resync_max_sectors = sectors;
4485 #ifdef CONFIG_MD_RAID5_RESHAPE
4486 static int raid5_check_reshape(mddev_t *mddev)
4488 raid5_conf_t *conf = mddev_to_conf(mddev);
4491 if (mddev->delta_disks < 0 ||
4492 mddev->new_level != mddev->level)
4493 return -EINVAL; /* Cannot shrink array or change level yet */
4494 if (mddev->delta_disks == 0)
4495 return 0; /* nothing to do */
4497 /* Cannot grow a bitmap yet */
4500 /* Can only proceed if there are plenty of stripe_heads.
4501 * We need a minimum of one full stripe,, and for sensible progress
4502 * it is best to have about 4 times that.
4503 * If we require 4 times, then the default 256 4K stripe_heads will
4504 * allow for chunk sizes up to 256K, which is probably OK.
4505 * If the chunk size is greater, user-space should request more
4506 * stripe_heads first.
4508 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
4509 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
4510 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
4511 (mddev->chunk_size / STRIPE_SIZE)*4);
4515 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
4519 if (mddev->degraded > conf->max_degraded)
4521 /* looks like we might be able to manage this */
4525 static int raid5_start_reshape(mddev_t *mddev)
4527 raid5_conf_t *conf = mddev_to_conf(mddev);
4530 int added_devices = 0;
4531 unsigned long flags;
4533 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4536 list_for_each_entry(rdev, &mddev->disks, same_set)
4537 if (rdev->raid_disk < 0 &&
4538 !test_bit(Faulty, &rdev->flags))
4541 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
4542 /* Not enough devices even to make a degraded array
4547 atomic_set(&conf->reshape_stripes, 0);
4548 spin_lock_irq(&conf->device_lock);
4549 conf->previous_raid_disks = conf->raid_disks;
4550 conf->raid_disks += mddev->delta_disks;
4551 conf->expand_progress = 0;
4552 conf->expand_lo = 0;
4553 spin_unlock_irq(&conf->device_lock);
4555 /* Add some new drives, as many as will fit.
4556 * We know there are enough to make the newly sized array work.
4558 list_for_each_entry(rdev, &mddev->disks, same_set)
4559 if (rdev->raid_disk < 0 &&
4560 !test_bit(Faulty, &rdev->flags)) {
4561 if (raid5_add_disk(mddev, rdev) == 0) {
4563 set_bit(In_sync, &rdev->flags);
4565 rdev->recovery_offset = 0;
4566 sprintf(nm, "rd%d", rdev->raid_disk);
4567 if (sysfs_create_link(&mddev->kobj,
4570 "raid5: failed to create "
4571 " link %s for %s\n",
4577 spin_lock_irqsave(&conf->device_lock, flags);
4578 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
4579 spin_unlock_irqrestore(&conf->device_lock, flags);
4580 mddev->raid_disks = conf->raid_disks;
4581 mddev->reshape_position = 0;
4582 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4584 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4585 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4586 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4587 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4588 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4590 if (!mddev->sync_thread) {
4591 mddev->recovery = 0;
4592 spin_lock_irq(&conf->device_lock);
4593 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
4594 conf->expand_progress = MaxSector;
4595 spin_unlock_irq(&conf->device_lock);
4598 md_wakeup_thread(mddev->sync_thread);
4599 md_new_event(mddev);
4604 static void end_reshape(raid5_conf_t *conf)
4606 struct block_device *bdev;
4608 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
4609 conf->mddev->array_sectors = conf->mddev->dev_sectors *
4610 (conf->raid_disks - conf->max_degraded);
4611 set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
4612 conf->mddev->changed = 1;
4614 bdev = bdget_disk(conf->mddev->gendisk, 0);
4616 mutex_lock(&bdev->bd_inode->i_mutex);
4617 i_size_write(bdev->bd_inode,
4618 (loff_t)conf->mddev->array_sectors << 9);
4619 mutex_unlock(&bdev->bd_inode->i_mutex);
4622 spin_lock_irq(&conf->device_lock);
4623 conf->expand_progress = MaxSector;
4624 spin_unlock_irq(&conf->device_lock);
4625 conf->mddev->reshape_position = MaxSector;
4627 /* read-ahead size must cover two whole stripes, which is
4628 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4631 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4632 int stripe = data_disks *
4633 (conf->mddev->chunk_size / PAGE_SIZE);
4634 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4635 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4640 static void raid5_quiesce(mddev_t *mddev, int state)
4642 raid5_conf_t *conf = mddev_to_conf(mddev);
4645 case 2: /* resume for a suspend */
4646 wake_up(&conf->wait_for_overlap);
4649 case 1: /* stop all writes */
4650 spin_lock_irq(&conf->device_lock);
4652 wait_event_lock_irq(conf->wait_for_stripe,
4653 atomic_read(&conf->active_stripes) == 0 &&
4654 atomic_read(&conf->active_aligned_reads) == 0,
4655 conf->device_lock, /* nothing */);
4656 spin_unlock_irq(&conf->device_lock);
4659 case 0: /* re-enable writes */
4660 spin_lock_irq(&conf->device_lock);
4662 wake_up(&conf->wait_for_stripe);
4663 wake_up(&conf->wait_for_overlap);
4664 spin_unlock_irq(&conf->device_lock);
4669 static struct mdk_personality raid6_personality =
4673 .owner = THIS_MODULE,
4674 .make_request = make_request,
4678 .error_handler = error,
4679 .hot_add_disk = raid5_add_disk,
4680 .hot_remove_disk= raid5_remove_disk,
4681 .spare_active = raid5_spare_active,
4682 .sync_request = sync_request,
4683 .resize = raid5_resize,
4684 #ifdef CONFIG_MD_RAID5_RESHAPE
4685 .check_reshape = raid5_check_reshape,
4686 .start_reshape = raid5_start_reshape,
4688 .quiesce = raid5_quiesce,
4690 static struct mdk_personality raid5_personality =
4694 .owner = THIS_MODULE,
4695 .make_request = make_request,
4699 .error_handler = error,
4700 .hot_add_disk = raid5_add_disk,
4701 .hot_remove_disk= raid5_remove_disk,
4702 .spare_active = raid5_spare_active,
4703 .sync_request = sync_request,
4704 .resize = raid5_resize,
4705 #ifdef CONFIG_MD_RAID5_RESHAPE
4706 .check_reshape = raid5_check_reshape,
4707 .start_reshape = raid5_start_reshape,
4709 .quiesce = raid5_quiesce,
4712 static struct mdk_personality raid4_personality =
4716 .owner = THIS_MODULE,
4717 .make_request = make_request,
4721 .error_handler = error,
4722 .hot_add_disk = raid5_add_disk,
4723 .hot_remove_disk= raid5_remove_disk,
4724 .spare_active = raid5_spare_active,
4725 .sync_request = sync_request,
4726 .resize = raid5_resize,
4727 #ifdef CONFIG_MD_RAID5_RESHAPE
4728 .check_reshape = raid5_check_reshape,
4729 .start_reshape = raid5_start_reshape,
4731 .quiesce = raid5_quiesce,
4734 static int __init raid5_init(void)
4738 e = raid6_select_algo();
4741 register_md_personality(&raid6_personality);
4742 register_md_personality(&raid5_personality);
4743 register_md_personality(&raid4_personality);
4747 static void raid5_exit(void)
4749 unregister_md_personality(&raid6_personality);
4750 unregister_md_personality(&raid5_personality);
4751 unregister_md_personality(&raid4_personality);
4754 module_init(raid5_init);
4755 module_exit(raid5_exit);
4756 MODULE_LICENSE("GPL");
4757 MODULE_ALIAS("md-personality-4"); /* RAID5 */
4758 MODULE_ALIAS("md-raid5");
4759 MODULE_ALIAS("md-raid4");
4760 MODULE_ALIAS("md-level-5");
4761 MODULE_ALIAS("md-level-4");
4762 MODULE_ALIAS("md-personality-8"); /* RAID6 */
4763 MODULE_ALIAS("md-raid6");
4764 MODULE_ALIAS("md-level-6");
4766 /* This used to be two separate modules, they were: */
4767 MODULE_ALIAS("raid5");
4768 MODULE_ALIAS("raid6");