2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is seq_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
65 #define NR_STRIPES 256
66 #define STRIPE_SIZE PAGE_SIZE
67 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
68 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
69 #define IO_THRESHOLD 1
70 #define BYPASS_THRESHOLD 1
71 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
72 #define HASH_MASK (NR_HASH - 1)
74 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
80 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
86 * This function is used to determine the 'next' bio in the list, given the sector
87 * of the current stripe+device
89 static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
99 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
102 static inline int raid5_bi_phys_segments(struct bio *bio)
104 return bio->bi_phys_segments & 0xffff;
107 static inline int raid5_bi_hw_segments(struct bio *bio)
109 return (bio->bi_phys_segments >> 16) & 0xffff;
112 static inline int raid5_dec_bi_phys_segments(struct bio *bio)
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
118 static inline int raid5_dec_bi_hw_segments(struct bio *bio)
120 unsigned short val = raid5_bi_hw_segments(bio);
123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
127 static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
132 /* Find first data disk in a raid6 stripe */
133 static inline int raid6_d0(struct stripe_head *sh)
136 /* ddf always start from first device */
138 /* md starts just after Q block */
139 if (sh->qd_idx == sh->disks - 1)
142 return sh->qd_idx + 1;
144 static inline int raid6_next_disk(int disk, int raid_disks)
147 return (disk < raid_disks) ? disk : 0;
150 /* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
155 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
162 if (idx == sh->pd_idx)
163 return syndrome_disks;
164 if (idx == sh->qd_idx)
165 return syndrome_disks + 1;
171 static void return_io(struct bio *return_bi)
173 struct bio *bi = return_bi;
176 return_bi = bi->bi_next;
184 static void print_raid5_conf (struct r5conf *conf);
186 static int stripe_operations_active(struct stripe_head *sh)
188 return sh->check_state || sh->reconstruct_state ||
189 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
190 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
193 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
195 if (atomic_dec_and_test(&sh->count)) {
196 BUG_ON(!list_empty(&sh->lru));
197 BUG_ON(atomic_read(&conf->active_stripes)==0);
198 if (test_bit(STRIPE_HANDLE, &sh->state)) {
199 if (test_bit(STRIPE_DELAYED, &sh->state))
200 list_add_tail(&sh->lru, &conf->delayed_list);
201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
203 list_add_tail(&sh->lru, &conf->bitmap_list);
205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
206 list_add_tail(&sh->lru, &conf->handle_list);
208 md_wakeup_thread(conf->mddev->thread);
210 BUG_ON(stripe_operations_active(sh));
211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
212 if (atomic_dec_return(&conf->preread_active_stripes)
214 md_wakeup_thread(conf->mddev->thread);
215 atomic_dec(&conf->active_stripes);
216 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
217 list_add_tail(&sh->lru, &conf->inactive_list);
218 wake_up(&conf->wait_for_stripe);
219 if (conf->retry_read_aligned)
220 md_wakeup_thread(conf->mddev->thread);
226 static void release_stripe(struct stripe_head *sh)
228 struct r5conf *conf = sh->raid_conf;
231 spin_lock_irqsave(&conf->device_lock, flags);
232 __release_stripe(conf, sh);
233 spin_unlock_irqrestore(&conf->device_lock, flags);
236 static inline void remove_hash(struct stripe_head *sh)
238 pr_debug("remove_hash(), stripe %llu\n",
239 (unsigned long long)sh->sector);
241 hlist_del_init(&sh->hash);
244 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
246 struct hlist_head *hp = stripe_hash(conf, sh->sector);
248 pr_debug("insert_hash(), stripe %llu\n",
249 (unsigned long long)sh->sector);
251 hlist_add_head(&sh->hash, hp);
255 /* find an idle stripe, make sure it is unhashed, and return it. */
256 static struct stripe_head *get_free_stripe(struct r5conf *conf)
258 struct stripe_head *sh = NULL;
259 struct list_head *first;
261 if (list_empty(&conf->inactive_list))
263 first = conf->inactive_list.next;
264 sh = list_entry(first, struct stripe_head, lru);
265 list_del_init(first);
267 atomic_inc(&conf->active_stripes);
272 static void shrink_buffers(struct stripe_head *sh)
276 int num = sh->raid_conf->pool_size;
278 for (i = 0; i < num ; i++) {
282 sh->dev[i].page = NULL;
287 static int grow_buffers(struct stripe_head *sh)
290 int num = sh->raid_conf->pool_size;
292 for (i = 0; i < num; i++) {
295 if (!(page = alloc_page(GFP_KERNEL))) {
298 sh->dev[i].page = page;
303 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
304 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
305 struct stripe_head *sh);
307 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
309 struct r5conf *conf = sh->raid_conf;
312 BUG_ON(atomic_read(&sh->count) != 0);
313 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
314 BUG_ON(stripe_operations_active(sh));
316 pr_debug("init_stripe called, stripe %llu\n",
317 (unsigned long long)sh->sector);
321 sh->generation = conf->generation - previous;
322 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
324 stripe_set_idx(sector, conf, previous, sh);
328 for (i = sh->disks; i--; ) {
329 struct r5dev *dev = &sh->dev[i];
331 if (dev->toread || dev->read || dev->towrite || dev->written ||
332 test_bit(R5_LOCKED, &dev->flags)) {
333 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
334 (unsigned long long)sh->sector, i, dev->toread,
335 dev->read, dev->towrite, dev->written,
336 test_bit(R5_LOCKED, &dev->flags));
340 raid5_build_block(sh, i, previous);
342 insert_hash(conf, sh);
345 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
348 struct stripe_head *sh;
349 struct hlist_node *hn;
351 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
352 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
353 if (sh->sector == sector && sh->generation == generation)
355 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
360 * Need to check if array has failed when deciding whether to:
362 * - remove non-faulty devices
365 * This determination is simple when no reshape is happening.
366 * However if there is a reshape, we need to carefully check
367 * both the before and after sections.
368 * This is because some failed devices may only affect one
369 * of the two sections, and some non-in_sync devices may
370 * be insync in the section most affected by failed devices.
372 static int calc_degraded(struct r5conf *conf)
374 int degraded, degraded2;
379 for (i = 0; i < conf->previous_raid_disks; i++) {
380 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
381 if (!rdev || test_bit(Faulty, &rdev->flags))
383 else if (test_bit(In_sync, &rdev->flags))
386 /* not in-sync or faulty.
387 * If the reshape increases the number of devices,
388 * this is being recovered by the reshape, so
389 * this 'previous' section is not in_sync.
390 * If the number of devices is being reduced however,
391 * the device can only be part of the array if
392 * we are reverting a reshape, so this section will
395 if (conf->raid_disks >= conf->previous_raid_disks)
399 if (conf->raid_disks == conf->previous_raid_disks)
403 for (i = 0; i < conf->raid_disks; i++) {
404 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
405 if (!rdev || test_bit(Faulty, &rdev->flags))
407 else if (test_bit(In_sync, &rdev->flags))
410 /* not in-sync or faulty.
411 * If reshape increases the number of devices, this
412 * section has already been recovered, else it
413 * almost certainly hasn't.
415 if (conf->raid_disks <= conf->previous_raid_disks)
419 if (degraded2 > degraded)
424 static int has_failed(struct r5conf *conf)
428 if (conf->mddev->reshape_position == MaxSector)
429 return conf->mddev->degraded > conf->max_degraded;
431 degraded = calc_degraded(conf);
432 if (degraded > conf->max_degraded)
437 static struct stripe_head *
438 get_active_stripe(struct r5conf *conf, sector_t sector,
439 int previous, int noblock, int noquiesce)
441 struct stripe_head *sh;
443 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
445 spin_lock_irq(&conf->device_lock);
448 wait_event_lock_irq(conf->wait_for_stripe,
449 conf->quiesce == 0 || noquiesce,
450 conf->device_lock, /* nothing */);
451 sh = __find_stripe(conf, sector, conf->generation - previous);
453 if (!conf->inactive_blocked)
454 sh = get_free_stripe(conf);
455 if (noblock && sh == NULL)
458 conf->inactive_blocked = 1;
459 wait_event_lock_irq(conf->wait_for_stripe,
460 !list_empty(&conf->inactive_list) &&
461 (atomic_read(&conf->active_stripes)
462 < (conf->max_nr_stripes *3/4)
463 || !conf->inactive_blocked),
466 conf->inactive_blocked = 0;
468 init_stripe(sh, sector, previous);
470 if (atomic_read(&sh->count)) {
471 BUG_ON(!list_empty(&sh->lru)
472 && !test_bit(STRIPE_EXPANDING, &sh->state));
474 if (!test_bit(STRIPE_HANDLE, &sh->state))
475 atomic_inc(&conf->active_stripes);
476 if (list_empty(&sh->lru) &&
477 !test_bit(STRIPE_EXPANDING, &sh->state))
479 list_del_init(&sh->lru);
482 } while (sh == NULL);
485 atomic_inc(&sh->count);
487 spin_unlock_irq(&conf->device_lock);
491 /* Determine if 'data_offset' or 'new_data_offset' should be used
492 * in this stripe_head.
494 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
496 sector_t progress = conf->reshape_progress;
497 /* Need a memory barrier to make sure we see the value
498 * of conf->generation, or ->data_offset that was set before
499 * reshape_progress was updated.
502 if (progress == MaxSector)
504 if (sh->generation == conf->generation - 1)
506 /* We are in a reshape, and this is a new-generation stripe,
507 * so use new_data_offset.
513 raid5_end_read_request(struct bio *bi, int error);
515 raid5_end_write_request(struct bio *bi, int error);
517 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
519 struct r5conf *conf = sh->raid_conf;
520 int i, disks = sh->disks;
524 for (i = disks; i--; ) {
526 int replace_only = 0;
527 struct bio *bi, *rbi;
528 struct md_rdev *rdev, *rrdev = NULL;
529 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
530 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
534 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
536 else if (test_and_clear_bit(R5_WantReplace,
537 &sh->dev[i].flags)) {
542 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
545 bi = &sh->dev[i].req;
546 rbi = &sh->dev[i].rreq; /* For writing to replacement */
551 bi->bi_end_io = raid5_end_write_request;
552 rbi->bi_end_io = raid5_end_write_request;
554 bi->bi_end_io = raid5_end_read_request;
557 rrdev = rcu_dereference(conf->disks[i].replacement);
558 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
559 rdev = rcu_dereference(conf->disks[i].rdev);
568 /* We raced and saw duplicates */
571 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
576 if (rdev && test_bit(Faulty, &rdev->flags))
579 atomic_inc(&rdev->nr_pending);
580 if (rrdev && test_bit(Faulty, &rrdev->flags))
583 atomic_inc(&rrdev->nr_pending);
586 /* We have already checked bad blocks for reads. Now
587 * need to check for writes. We never accept write errors
588 * on the replacement, so we don't to check rrdev.
590 while ((rw & WRITE) && rdev &&
591 test_bit(WriteErrorSeen, &rdev->flags)) {
594 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
595 &first_bad, &bad_sectors);
600 set_bit(BlockedBadBlocks, &rdev->flags);
601 if (!conf->mddev->external &&
602 conf->mddev->flags) {
603 /* It is very unlikely, but we might
604 * still need to write out the
605 * bad block log - better give it
607 md_check_recovery(conf->mddev);
609 md_wait_for_blocked_rdev(rdev, conf->mddev);
611 /* Acknowledged bad block - skip the write */
612 rdev_dec_pending(rdev, conf->mddev);
618 if (s->syncing || s->expanding || s->expanded
620 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
622 set_bit(STRIPE_IO_STARTED, &sh->state);
624 bi->bi_bdev = rdev->bdev;
625 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
626 __func__, (unsigned long long)sh->sector,
628 atomic_inc(&sh->count);
629 if (use_new_offset(conf, sh))
630 bi->bi_sector = (sh->sector
631 + rdev->new_data_offset);
633 bi->bi_sector = (sh->sector
634 + rdev->data_offset);
635 bi->bi_flags = 1 << BIO_UPTODATE;
637 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
638 bi->bi_io_vec[0].bv_offset = 0;
639 bi->bi_size = STRIPE_SIZE;
642 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
643 generic_make_request(bi);
646 if (s->syncing || s->expanding || s->expanded
648 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
650 set_bit(STRIPE_IO_STARTED, &sh->state);
652 rbi->bi_bdev = rrdev->bdev;
653 pr_debug("%s: for %llu schedule op %ld on "
654 "replacement disc %d\n",
655 __func__, (unsigned long long)sh->sector,
657 atomic_inc(&sh->count);
658 if (use_new_offset(conf, sh))
659 rbi->bi_sector = (sh->sector
660 + rrdev->new_data_offset);
662 rbi->bi_sector = (sh->sector
663 + rrdev->data_offset);
664 rbi->bi_flags = 1 << BIO_UPTODATE;
666 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
667 rbi->bi_io_vec[0].bv_offset = 0;
668 rbi->bi_size = STRIPE_SIZE;
670 generic_make_request(rbi);
672 if (!rdev && !rrdev) {
674 set_bit(STRIPE_DEGRADED, &sh->state);
675 pr_debug("skip op %ld on disc %d for sector %llu\n",
676 bi->bi_rw, i, (unsigned long long)sh->sector);
677 clear_bit(R5_LOCKED, &sh->dev[i].flags);
678 set_bit(STRIPE_HANDLE, &sh->state);
683 static struct dma_async_tx_descriptor *
684 async_copy_data(int frombio, struct bio *bio, struct page *page,
685 sector_t sector, struct dma_async_tx_descriptor *tx)
688 struct page *bio_page;
691 struct async_submit_ctl submit;
692 enum async_tx_flags flags = 0;
694 if (bio->bi_sector >= sector)
695 page_offset = (signed)(bio->bi_sector - sector) * 512;
697 page_offset = (signed)(sector - bio->bi_sector) * -512;
700 flags |= ASYNC_TX_FENCE;
701 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
703 bio_for_each_segment(bvl, bio, i) {
704 int len = bvl->bv_len;
708 if (page_offset < 0) {
709 b_offset = -page_offset;
710 page_offset += b_offset;
714 if (len > 0 && page_offset + len > STRIPE_SIZE)
715 clen = STRIPE_SIZE - page_offset;
720 b_offset += bvl->bv_offset;
721 bio_page = bvl->bv_page;
723 tx = async_memcpy(page, bio_page, page_offset,
724 b_offset, clen, &submit);
726 tx = async_memcpy(bio_page, page, b_offset,
727 page_offset, clen, &submit);
729 /* chain the operations */
730 submit.depend_tx = tx;
732 if (clen < len) /* hit end of page */
740 static void ops_complete_biofill(void *stripe_head_ref)
742 struct stripe_head *sh = stripe_head_ref;
743 struct bio *return_bi = NULL;
744 struct r5conf *conf = sh->raid_conf;
747 pr_debug("%s: stripe %llu\n", __func__,
748 (unsigned long long)sh->sector);
750 /* clear completed biofills */
751 spin_lock_irq(&conf->device_lock);
752 for (i = sh->disks; i--; ) {
753 struct r5dev *dev = &sh->dev[i];
755 /* acknowledge completion of a biofill operation */
756 /* and check if we need to reply to a read request,
757 * new R5_Wantfill requests are held off until
758 * !STRIPE_BIOFILL_RUN
760 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
761 struct bio *rbi, *rbi2;
766 while (rbi && rbi->bi_sector <
767 dev->sector + STRIPE_SECTORS) {
768 rbi2 = r5_next_bio(rbi, dev->sector);
769 if (!raid5_dec_bi_phys_segments(rbi)) {
770 rbi->bi_next = return_bi;
777 spin_unlock_irq(&conf->device_lock);
778 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
780 return_io(return_bi);
782 set_bit(STRIPE_HANDLE, &sh->state);
786 static void ops_run_biofill(struct stripe_head *sh)
788 struct dma_async_tx_descriptor *tx = NULL;
789 struct r5conf *conf = sh->raid_conf;
790 struct async_submit_ctl submit;
793 pr_debug("%s: stripe %llu\n", __func__,
794 (unsigned long long)sh->sector);
796 for (i = sh->disks; i--; ) {
797 struct r5dev *dev = &sh->dev[i];
798 if (test_bit(R5_Wantfill, &dev->flags)) {
800 spin_lock_irq(&conf->device_lock);
801 dev->read = rbi = dev->toread;
803 spin_unlock_irq(&conf->device_lock);
804 while (rbi && rbi->bi_sector <
805 dev->sector + STRIPE_SECTORS) {
806 tx = async_copy_data(0, rbi, dev->page,
808 rbi = r5_next_bio(rbi, dev->sector);
813 atomic_inc(&sh->count);
814 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
815 async_trigger_callback(&submit);
818 static void mark_target_uptodate(struct stripe_head *sh, int target)
825 tgt = &sh->dev[target];
826 set_bit(R5_UPTODATE, &tgt->flags);
827 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
828 clear_bit(R5_Wantcompute, &tgt->flags);
831 static void ops_complete_compute(void *stripe_head_ref)
833 struct stripe_head *sh = stripe_head_ref;
835 pr_debug("%s: stripe %llu\n", __func__,
836 (unsigned long long)sh->sector);
838 /* mark the computed target(s) as uptodate */
839 mark_target_uptodate(sh, sh->ops.target);
840 mark_target_uptodate(sh, sh->ops.target2);
842 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
843 if (sh->check_state == check_state_compute_run)
844 sh->check_state = check_state_compute_result;
845 set_bit(STRIPE_HANDLE, &sh->state);
849 /* return a pointer to the address conversion region of the scribble buffer */
850 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
851 struct raid5_percpu *percpu)
853 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
856 static struct dma_async_tx_descriptor *
857 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
859 int disks = sh->disks;
860 struct page **xor_srcs = percpu->scribble;
861 int target = sh->ops.target;
862 struct r5dev *tgt = &sh->dev[target];
863 struct page *xor_dest = tgt->page;
865 struct dma_async_tx_descriptor *tx;
866 struct async_submit_ctl submit;
869 pr_debug("%s: stripe %llu block: %d\n",
870 __func__, (unsigned long long)sh->sector, target);
871 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
873 for (i = disks; i--; )
875 xor_srcs[count++] = sh->dev[i].page;
877 atomic_inc(&sh->count);
879 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
880 ops_complete_compute, sh, to_addr_conv(sh, percpu));
881 if (unlikely(count == 1))
882 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
884 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
889 /* set_syndrome_sources - populate source buffers for gen_syndrome
890 * @srcs - (struct page *) array of size sh->disks
891 * @sh - stripe_head to parse
893 * Populates srcs in proper layout order for the stripe and returns the
894 * 'count' of sources to be used in a call to async_gen_syndrome. The P
895 * destination buffer is recorded in srcs[count] and the Q destination
896 * is recorded in srcs[count+1]].
898 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
900 int disks = sh->disks;
901 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
902 int d0_idx = raid6_d0(sh);
906 for (i = 0; i < disks; i++)
912 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
914 srcs[slot] = sh->dev[i].page;
915 i = raid6_next_disk(i, disks);
916 } while (i != d0_idx);
918 return syndrome_disks;
921 static struct dma_async_tx_descriptor *
922 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
924 int disks = sh->disks;
925 struct page **blocks = percpu->scribble;
927 int qd_idx = sh->qd_idx;
928 struct dma_async_tx_descriptor *tx;
929 struct async_submit_ctl submit;
935 if (sh->ops.target < 0)
936 target = sh->ops.target2;
937 else if (sh->ops.target2 < 0)
938 target = sh->ops.target;
940 /* we should only have one valid target */
943 pr_debug("%s: stripe %llu block: %d\n",
944 __func__, (unsigned long long)sh->sector, target);
946 tgt = &sh->dev[target];
947 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
950 atomic_inc(&sh->count);
952 if (target == qd_idx) {
953 count = set_syndrome_sources(blocks, sh);
954 blocks[count] = NULL; /* regenerating p is not necessary */
955 BUG_ON(blocks[count+1] != dest); /* q should already be set */
956 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
957 ops_complete_compute, sh,
958 to_addr_conv(sh, percpu));
959 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
961 /* Compute any data- or p-drive using XOR */
963 for (i = disks; i-- ; ) {
964 if (i == target || i == qd_idx)
966 blocks[count++] = sh->dev[i].page;
969 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
970 NULL, ops_complete_compute, sh,
971 to_addr_conv(sh, percpu));
972 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
978 static struct dma_async_tx_descriptor *
979 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
981 int i, count, disks = sh->disks;
982 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
983 int d0_idx = raid6_d0(sh);
984 int faila = -1, failb = -1;
985 int target = sh->ops.target;
986 int target2 = sh->ops.target2;
987 struct r5dev *tgt = &sh->dev[target];
988 struct r5dev *tgt2 = &sh->dev[target2];
989 struct dma_async_tx_descriptor *tx;
990 struct page **blocks = percpu->scribble;
991 struct async_submit_ctl submit;
993 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
994 __func__, (unsigned long long)sh->sector, target, target2);
995 BUG_ON(target < 0 || target2 < 0);
996 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
997 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
999 /* we need to open-code set_syndrome_sources to handle the
1000 * slot number conversion for 'faila' and 'failb'
1002 for (i = 0; i < disks ; i++)
1007 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1009 blocks[slot] = sh->dev[i].page;
1015 i = raid6_next_disk(i, disks);
1016 } while (i != d0_idx);
1018 BUG_ON(faila == failb);
1021 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1022 __func__, (unsigned long long)sh->sector, faila, failb);
1024 atomic_inc(&sh->count);
1026 if (failb == syndrome_disks+1) {
1027 /* Q disk is one of the missing disks */
1028 if (faila == syndrome_disks) {
1029 /* Missing P+Q, just recompute */
1030 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1031 ops_complete_compute, sh,
1032 to_addr_conv(sh, percpu));
1033 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
1034 STRIPE_SIZE, &submit);
1038 int qd_idx = sh->qd_idx;
1040 /* Missing D+Q: recompute D from P, then recompute Q */
1041 if (target == qd_idx)
1042 data_target = target2;
1044 data_target = target;
1047 for (i = disks; i-- ; ) {
1048 if (i == data_target || i == qd_idx)
1050 blocks[count++] = sh->dev[i].page;
1052 dest = sh->dev[data_target].page;
1053 init_async_submit(&submit,
1054 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1056 to_addr_conv(sh, percpu));
1057 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1060 count = set_syndrome_sources(blocks, sh);
1061 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1062 ops_complete_compute, sh,
1063 to_addr_conv(sh, percpu));
1064 return async_gen_syndrome(blocks, 0, count+2,
1065 STRIPE_SIZE, &submit);
1068 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1069 ops_complete_compute, sh,
1070 to_addr_conv(sh, percpu));
1071 if (failb == syndrome_disks) {
1072 /* We're missing D+P. */
1073 return async_raid6_datap_recov(syndrome_disks+2,
1077 /* We're missing D+D. */
1078 return async_raid6_2data_recov(syndrome_disks+2,
1079 STRIPE_SIZE, faila, failb,
1086 static void ops_complete_prexor(void *stripe_head_ref)
1088 struct stripe_head *sh = stripe_head_ref;
1090 pr_debug("%s: stripe %llu\n", __func__,
1091 (unsigned long long)sh->sector);
1094 static struct dma_async_tx_descriptor *
1095 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1096 struct dma_async_tx_descriptor *tx)
1098 int disks = sh->disks;
1099 struct page **xor_srcs = percpu->scribble;
1100 int count = 0, pd_idx = sh->pd_idx, i;
1101 struct async_submit_ctl submit;
1103 /* existing parity data subtracted */
1104 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1106 pr_debug("%s: stripe %llu\n", __func__,
1107 (unsigned long long)sh->sector);
1109 for (i = disks; i--; ) {
1110 struct r5dev *dev = &sh->dev[i];
1111 /* Only process blocks that are known to be uptodate */
1112 if (test_bit(R5_Wantdrain, &dev->flags))
1113 xor_srcs[count++] = dev->page;
1116 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1117 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
1118 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1123 static struct dma_async_tx_descriptor *
1124 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1126 int disks = sh->disks;
1129 pr_debug("%s: stripe %llu\n", __func__,
1130 (unsigned long long)sh->sector);
1132 for (i = disks; i--; ) {
1133 struct r5dev *dev = &sh->dev[i];
1136 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
1139 spin_lock_irq(&sh->raid_conf->device_lock);
1140 chosen = dev->towrite;
1141 dev->towrite = NULL;
1142 BUG_ON(dev->written);
1143 wbi = dev->written = chosen;
1144 spin_unlock_irq(&sh->raid_conf->device_lock);
1146 while (wbi && wbi->bi_sector <
1147 dev->sector + STRIPE_SECTORS) {
1148 if (wbi->bi_rw & REQ_FUA)
1149 set_bit(R5_WantFUA, &dev->flags);
1150 if (wbi->bi_rw & REQ_SYNC)
1151 set_bit(R5_SyncIO, &dev->flags);
1152 tx = async_copy_data(1, wbi, dev->page,
1154 wbi = r5_next_bio(wbi, dev->sector);
1162 static void ops_complete_reconstruct(void *stripe_head_ref)
1164 struct stripe_head *sh = stripe_head_ref;
1165 int disks = sh->disks;
1166 int pd_idx = sh->pd_idx;
1167 int qd_idx = sh->qd_idx;
1169 bool fua = false, sync = false;
1171 pr_debug("%s: stripe %llu\n", __func__,
1172 (unsigned long long)sh->sector);
1174 for (i = disks; i--; ) {
1175 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1176 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1179 for (i = disks; i--; ) {
1180 struct r5dev *dev = &sh->dev[i];
1182 if (dev->written || i == pd_idx || i == qd_idx) {
1183 set_bit(R5_UPTODATE, &dev->flags);
1185 set_bit(R5_WantFUA, &dev->flags);
1187 set_bit(R5_SyncIO, &dev->flags);
1191 if (sh->reconstruct_state == reconstruct_state_drain_run)
1192 sh->reconstruct_state = reconstruct_state_drain_result;
1193 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1194 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1196 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1197 sh->reconstruct_state = reconstruct_state_result;
1200 set_bit(STRIPE_HANDLE, &sh->state);
1205 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1206 struct dma_async_tx_descriptor *tx)
1208 int disks = sh->disks;
1209 struct page **xor_srcs = percpu->scribble;
1210 struct async_submit_ctl submit;
1211 int count = 0, pd_idx = sh->pd_idx, i;
1212 struct page *xor_dest;
1214 unsigned long flags;
1216 pr_debug("%s: stripe %llu\n", __func__,
1217 (unsigned long long)sh->sector);
1219 /* check if prexor is active which means only process blocks
1220 * that are part of a read-modify-write (written)
1222 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1224 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1225 for (i = disks; i--; ) {
1226 struct r5dev *dev = &sh->dev[i];
1228 xor_srcs[count++] = dev->page;
1231 xor_dest = sh->dev[pd_idx].page;
1232 for (i = disks; i--; ) {
1233 struct r5dev *dev = &sh->dev[i];
1235 xor_srcs[count++] = dev->page;
1239 /* 1/ if we prexor'd then the dest is reused as a source
1240 * 2/ if we did not prexor then we are redoing the parity
1241 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1242 * for the synchronous xor case
1244 flags = ASYNC_TX_ACK |
1245 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1247 atomic_inc(&sh->count);
1249 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1250 to_addr_conv(sh, percpu));
1251 if (unlikely(count == 1))
1252 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1254 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1258 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1259 struct dma_async_tx_descriptor *tx)
1261 struct async_submit_ctl submit;
1262 struct page **blocks = percpu->scribble;
1265 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1267 count = set_syndrome_sources(blocks, sh);
1269 atomic_inc(&sh->count);
1271 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1272 sh, to_addr_conv(sh, percpu));
1273 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1276 static void ops_complete_check(void *stripe_head_ref)
1278 struct stripe_head *sh = stripe_head_ref;
1280 pr_debug("%s: stripe %llu\n", __func__,
1281 (unsigned long long)sh->sector);
1283 sh->check_state = check_state_check_result;
1284 set_bit(STRIPE_HANDLE, &sh->state);
1288 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1290 int disks = sh->disks;
1291 int pd_idx = sh->pd_idx;
1292 int qd_idx = sh->qd_idx;
1293 struct page *xor_dest;
1294 struct page **xor_srcs = percpu->scribble;
1295 struct dma_async_tx_descriptor *tx;
1296 struct async_submit_ctl submit;
1300 pr_debug("%s: stripe %llu\n", __func__,
1301 (unsigned long long)sh->sector);
1304 xor_dest = sh->dev[pd_idx].page;
1305 xor_srcs[count++] = xor_dest;
1306 for (i = disks; i--; ) {
1307 if (i == pd_idx || i == qd_idx)
1309 xor_srcs[count++] = sh->dev[i].page;
1312 init_async_submit(&submit, 0, NULL, NULL, NULL,
1313 to_addr_conv(sh, percpu));
1314 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1315 &sh->ops.zero_sum_result, &submit);
1317 atomic_inc(&sh->count);
1318 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1319 tx = async_trigger_callback(&submit);
1322 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1324 struct page **srcs = percpu->scribble;
1325 struct async_submit_ctl submit;
1328 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1329 (unsigned long long)sh->sector, checkp);
1331 count = set_syndrome_sources(srcs, sh);
1335 atomic_inc(&sh->count);
1336 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1337 sh, to_addr_conv(sh, percpu));
1338 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1339 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1342 static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1344 int overlap_clear = 0, i, disks = sh->disks;
1345 struct dma_async_tx_descriptor *tx = NULL;
1346 struct r5conf *conf = sh->raid_conf;
1347 int level = conf->level;
1348 struct raid5_percpu *percpu;
1352 percpu = per_cpu_ptr(conf->percpu, cpu);
1353 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1354 ops_run_biofill(sh);
1358 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1360 tx = ops_run_compute5(sh, percpu);
1362 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1363 tx = ops_run_compute6_1(sh, percpu);
1365 tx = ops_run_compute6_2(sh, percpu);
1367 /* terminate the chain if reconstruct is not set to be run */
1368 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1372 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1373 tx = ops_run_prexor(sh, percpu, tx);
1375 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1376 tx = ops_run_biodrain(sh, tx);
1380 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1382 ops_run_reconstruct5(sh, percpu, tx);
1384 ops_run_reconstruct6(sh, percpu, tx);
1387 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1388 if (sh->check_state == check_state_run)
1389 ops_run_check_p(sh, percpu);
1390 else if (sh->check_state == check_state_run_q)
1391 ops_run_check_pq(sh, percpu, 0);
1392 else if (sh->check_state == check_state_run_pq)
1393 ops_run_check_pq(sh, percpu, 1);
1399 for (i = disks; i--; ) {
1400 struct r5dev *dev = &sh->dev[i];
1401 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1402 wake_up(&sh->raid_conf->wait_for_overlap);
1407 #ifdef CONFIG_MULTICORE_RAID456
1408 static void async_run_ops(void *param, async_cookie_t cookie)
1410 struct stripe_head *sh = param;
1411 unsigned long ops_request = sh->ops.request;
1413 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1414 wake_up(&sh->ops.wait_for_ops);
1416 __raid_run_ops(sh, ops_request);
1420 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1422 /* since handle_stripe can be called outside of raid5d context
1423 * we need to ensure sh->ops.request is de-staged before another
1426 wait_event(sh->ops.wait_for_ops,
1427 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1428 sh->ops.request = ops_request;
1430 atomic_inc(&sh->count);
1431 async_schedule(async_run_ops, sh);
1434 #define raid_run_ops __raid_run_ops
1437 static int grow_one_stripe(struct r5conf *conf)
1439 struct stripe_head *sh;
1440 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
1444 sh->raid_conf = conf;
1445 #ifdef CONFIG_MULTICORE_RAID456
1446 init_waitqueue_head(&sh->ops.wait_for_ops);
1449 if (grow_buffers(sh)) {
1451 kmem_cache_free(conf->slab_cache, sh);
1454 /* we just created an active stripe so... */
1455 atomic_set(&sh->count, 1);
1456 atomic_inc(&conf->active_stripes);
1457 INIT_LIST_HEAD(&sh->lru);
1462 static int grow_stripes(struct r5conf *conf, int num)
1464 struct kmem_cache *sc;
1465 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1467 if (conf->mddev->gendisk)
1468 sprintf(conf->cache_name[0],
1469 "raid%d-%s", conf->level, mdname(conf->mddev));
1471 sprintf(conf->cache_name[0],
1472 "raid%d-%p", conf->level, conf->mddev);
1473 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1475 conf->active_name = 0;
1476 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1477 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1481 conf->slab_cache = sc;
1482 conf->pool_size = devs;
1484 if (!grow_one_stripe(conf))
1490 * scribble_len - return the required size of the scribble region
1491 * @num - total number of disks in the array
1493 * The size must be enough to contain:
1494 * 1/ a struct page pointer for each device in the array +2
1495 * 2/ room to convert each entry in (1) to its corresponding dma
1496 * (dma_map_page()) or page (page_address()) address.
1498 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1499 * calculate over all devices (not just the data blocks), using zeros in place
1500 * of the P and Q blocks.
1502 static size_t scribble_len(int num)
1506 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1511 static int resize_stripes(struct r5conf *conf, int newsize)
1513 /* Make all the stripes able to hold 'newsize' devices.
1514 * New slots in each stripe get 'page' set to a new page.
1516 * This happens in stages:
1517 * 1/ create a new kmem_cache and allocate the required number of
1519 * 2/ gather all the old stripe_heads and tranfer the pages across
1520 * to the new stripe_heads. This will have the side effect of
1521 * freezing the array as once all stripe_heads have been collected,
1522 * no IO will be possible. Old stripe heads are freed once their
1523 * pages have been transferred over, and the old kmem_cache is
1524 * freed when all stripes are done.
1525 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1526 * we simple return a failre status - no need to clean anything up.
1527 * 4/ allocate new pages for the new slots in the new stripe_heads.
1528 * If this fails, we don't bother trying the shrink the
1529 * stripe_heads down again, we just leave them as they are.
1530 * As each stripe_head is processed the new one is released into
1533 * Once step2 is started, we cannot afford to wait for a write,
1534 * so we use GFP_NOIO allocations.
1536 struct stripe_head *osh, *nsh;
1537 LIST_HEAD(newstripes);
1538 struct disk_info *ndisks;
1541 struct kmem_cache *sc;
1544 if (newsize <= conf->pool_size)
1545 return 0; /* never bother to shrink */
1547 err = md_allow_write(conf->mddev);
1552 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1553 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1558 for (i = conf->max_nr_stripes; i; i--) {
1559 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
1563 nsh->raid_conf = conf;
1564 #ifdef CONFIG_MULTICORE_RAID456
1565 init_waitqueue_head(&nsh->ops.wait_for_ops);
1568 list_add(&nsh->lru, &newstripes);
1571 /* didn't get enough, give up */
1572 while (!list_empty(&newstripes)) {
1573 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1574 list_del(&nsh->lru);
1575 kmem_cache_free(sc, nsh);
1577 kmem_cache_destroy(sc);
1580 /* Step 2 - Must use GFP_NOIO now.
1581 * OK, we have enough stripes, start collecting inactive
1582 * stripes and copying them over
1584 list_for_each_entry(nsh, &newstripes, lru) {
1585 spin_lock_irq(&conf->device_lock);
1586 wait_event_lock_irq(conf->wait_for_stripe,
1587 !list_empty(&conf->inactive_list),
1590 osh = get_free_stripe(conf);
1591 spin_unlock_irq(&conf->device_lock);
1592 atomic_set(&nsh->count, 1);
1593 for(i=0; i<conf->pool_size; i++)
1594 nsh->dev[i].page = osh->dev[i].page;
1595 for( ; i<newsize; i++)
1596 nsh->dev[i].page = NULL;
1597 kmem_cache_free(conf->slab_cache, osh);
1599 kmem_cache_destroy(conf->slab_cache);
1602 * At this point, we are holding all the stripes so the array
1603 * is completely stalled, so now is a good time to resize
1604 * conf->disks and the scribble region
1606 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1608 for (i=0; i<conf->raid_disks; i++)
1609 ndisks[i] = conf->disks[i];
1611 conf->disks = ndisks;
1616 conf->scribble_len = scribble_len(newsize);
1617 for_each_present_cpu(cpu) {
1618 struct raid5_percpu *percpu;
1621 percpu = per_cpu_ptr(conf->percpu, cpu);
1622 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1625 kfree(percpu->scribble);
1626 percpu->scribble = scribble;
1634 /* Step 4, return new stripes to service */
1635 while(!list_empty(&newstripes)) {
1636 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1637 list_del_init(&nsh->lru);
1639 for (i=conf->raid_disks; i < newsize; i++)
1640 if (nsh->dev[i].page == NULL) {
1641 struct page *p = alloc_page(GFP_NOIO);
1642 nsh->dev[i].page = p;
1646 release_stripe(nsh);
1648 /* critical section pass, GFP_NOIO no longer needed */
1650 conf->slab_cache = sc;
1651 conf->active_name = 1-conf->active_name;
1652 conf->pool_size = newsize;
1656 static int drop_one_stripe(struct r5conf *conf)
1658 struct stripe_head *sh;
1660 spin_lock_irq(&conf->device_lock);
1661 sh = get_free_stripe(conf);
1662 spin_unlock_irq(&conf->device_lock);
1665 BUG_ON(atomic_read(&sh->count));
1667 kmem_cache_free(conf->slab_cache, sh);
1668 atomic_dec(&conf->active_stripes);
1672 static void shrink_stripes(struct r5conf *conf)
1674 while (drop_one_stripe(conf))
1677 if (conf->slab_cache)
1678 kmem_cache_destroy(conf->slab_cache);
1679 conf->slab_cache = NULL;
1682 static void raid5_end_read_request(struct bio * bi, int error)
1684 struct stripe_head *sh = bi->bi_private;
1685 struct r5conf *conf = sh->raid_conf;
1686 int disks = sh->disks, i;
1687 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1688 char b[BDEVNAME_SIZE];
1689 struct md_rdev *rdev = NULL;
1692 for (i=0 ; i<disks; i++)
1693 if (bi == &sh->dev[i].req)
1696 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1697 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1703 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1704 /* If replacement finished while this request was outstanding,
1705 * 'replacement' might be NULL already.
1706 * In that case it moved down to 'rdev'.
1707 * rdev is not removed until all requests are finished.
1709 rdev = conf->disks[i].replacement;
1711 rdev = conf->disks[i].rdev;
1713 if (use_new_offset(conf, sh))
1714 s = sh->sector + rdev->new_data_offset;
1716 s = sh->sector + rdev->data_offset;
1718 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1719 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1720 /* Note that this cannot happen on a
1721 * replacement device. We just fail those on
1726 "md/raid:%s: read error corrected"
1727 " (%lu sectors at %llu on %s)\n",
1728 mdname(conf->mddev), STRIPE_SECTORS,
1729 (unsigned long long)s,
1730 bdevname(rdev->bdev, b));
1731 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1732 clear_bit(R5_ReadError, &sh->dev[i].flags);
1733 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1735 if (atomic_read(&rdev->read_errors))
1736 atomic_set(&rdev->read_errors, 0);
1738 const char *bdn = bdevname(rdev->bdev, b);
1741 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1742 atomic_inc(&rdev->read_errors);
1743 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1746 "md/raid:%s: read error on replacement device "
1747 "(sector %llu on %s).\n",
1748 mdname(conf->mddev),
1749 (unsigned long long)s,
1751 else if (conf->mddev->degraded >= conf->max_degraded)
1754 "md/raid:%s: read error not correctable "
1755 "(sector %llu on %s).\n",
1756 mdname(conf->mddev),
1757 (unsigned long long)s,
1759 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
1763 "md/raid:%s: read error NOT corrected!! "
1764 "(sector %llu on %s).\n",
1765 mdname(conf->mddev),
1766 (unsigned long long)s,
1768 else if (atomic_read(&rdev->read_errors)
1769 > conf->max_nr_stripes)
1771 "md/raid:%s: Too many read errors, failing device %s.\n",
1772 mdname(conf->mddev), bdn);
1776 set_bit(R5_ReadError, &sh->dev[i].flags);
1778 clear_bit(R5_ReadError, &sh->dev[i].flags);
1779 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1780 md_error(conf->mddev, rdev);
1783 rdev_dec_pending(rdev, conf->mddev);
1784 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1785 set_bit(STRIPE_HANDLE, &sh->state);
1789 static void raid5_end_write_request(struct bio *bi, int error)
1791 struct stripe_head *sh = bi->bi_private;
1792 struct r5conf *conf = sh->raid_conf;
1793 int disks = sh->disks, i;
1794 struct md_rdev *uninitialized_var(rdev);
1795 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1798 int replacement = 0;
1800 for (i = 0 ; i < disks; i++) {
1801 if (bi == &sh->dev[i].req) {
1802 rdev = conf->disks[i].rdev;
1805 if (bi == &sh->dev[i].rreq) {
1806 rdev = conf->disks[i].replacement;
1810 /* rdev was removed and 'replacement'
1811 * replaced it. rdev is not removed
1812 * until all requests are finished.
1814 rdev = conf->disks[i].rdev;
1818 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1819 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1828 md_error(conf->mddev, rdev);
1829 else if (is_badblock(rdev, sh->sector,
1831 &first_bad, &bad_sectors))
1832 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1835 set_bit(WriteErrorSeen, &rdev->flags);
1836 set_bit(R5_WriteError, &sh->dev[i].flags);
1837 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1838 set_bit(MD_RECOVERY_NEEDED,
1839 &rdev->mddev->recovery);
1840 } else if (is_badblock(rdev, sh->sector,
1842 &first_bad, &bad_sectors))
1843 set_bit(R5_MadeGood, &sh->dev[i].flags);
1845 rdev_dec_pending(rdev, conf->mddev);
1847 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1848 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1849 set_bit(STRIPE_HANDLE, &sh->state);
1853 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1855 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1857 struct r5dev *dev = &sh->dev[i];
1859 bio_init(&dev->req);
1860 dev->req.bi_io_vec = &dev->vec;
1862 dev->req.bi_max_vecs++;
1863 dev->req.bi_private = sh;
1864 dev->vec.bv_page = dev->page;
1866 bio_init(&dev->rreq);
1867 dev->rreq.bi_io_vec = &dev->rvec;
1868 dev->rreq.bi_vcnt++;
1869 dev->rreq.bi_max_vecs++;
1870 dev->rreq.bi_private = sh;
1871 dev->rvec.bv_page = dev->page;
1874 dev->sector = compute_blocknr(sh, i, previous);
1877 static void error(struct mddev *mddev, struct md_rdev *rdev)
1879 char b[BDEVNAME_SIZE];
1880 struct r5conf *conf = mddev->private;
1881 unsigned long flags;
1882 pr_debug("raid456: error called\n");
1884 spin_lock_irqsave(&conf->device_lock, flags);
1885 clear_bit(In_sync, &rdev->flags);
1886 mddev->degraded = calc_degraded(conf);
1887 spin_unlock_irqrestore(&conf->device_lock, flags);
1888 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1890 set_bit(Blocked, &rdev->flags);
1891 set_bit(Faulty, &rdev->flags);
1892 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1894 "md/raid:%s: Disk failure on %s, disabling device.\n"
1895 "md/raid:%s: Operation continuing on %d devices.\n",
1897 bdevname(rdev->bdev, b),
1899 conf->raid_disks - mddev->degraded);
1903 * Input: a 'big' sector number,
1904 * Output: index of the data and parity disk, and the sector # in them.
1906 static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
1907 int previous, int *dd_idx,
1908 struct stripe_head *sh)
1910 sector_t stripe, stripe2;
1911 sector_t chunk_number;
1912 unsigned int chunk_offset;
1915 sector_t new_sector;
1916 int algorithm = previous ? conf->prev_algo
1918 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1919 : conf->chunk_sectors;
1920 int raid_disks = previous ? conf->previous_raid_disks
1922 int data_disks = raid_disks - conf->max_degraded;
1924 /* First compute the information on this sector */
1927 * Compute the chunk number and the sector offset inside the chunk
1929 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1930 chunk_number = r_sector;
1933 * Compute the stripe number
1935 stripe = chunk_number;
1936 *dd_idx = sector_div(stripe, data_disks);
1939 * Select the parity disk based on the user selected algorithm.
1941 pd_idx = qd_idx = -1;
1942 switch(conf->level) {
1944 pd_idx = data_disks;
1947 switch (algorithm) {
1948 case ALGORITHM_LEFT_ASYMMETRIC:
1949 pd_idx = data_disks - sector_div(stripe2, raid_disks);
1950 if (*dd_idx >= pd_idx)
1953 case ALGORITHM_RIGHT_ASYMMETRIC:
1954 pd_idx = sector_div(stripe2, raid_disks);
1955 if (*dd_idx >= pd_idx)
1958 case ALGORITHM_LEFT_SYMMETRIC:
1959 pd_idx = data_disks - sector_div(stripe2, raid_disks);
1960 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1962 case ALGORITHM_RIGHT_SYMMETRIC:
1963 pd_idx = sector_div(stripe2, raid_disks);
1964 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1966 case ALGORITHM_PARITY_0:
1970 case ALGORITHM_PARITY_N:
1971 pd_idx = data_disks;
1979 switch (algorithm) {
1980 case ALGORITHM_LEFT_ASYMMETRIC:
1981 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
1982 qd_idx = pd_idx + 1;
1983 if (pd_idx == raid_disks-1) {
1984 (*dd_idx)++; /* Q D D D P */
1986 } else if (*dd_idx >= pd_idx)
1987 (*dd_idx) += 2; /* D D P Q D */
1989 case ALGORITHM_RIGHT_ASYMMETRIC:
1990 pd_idx = sector_div(stripe2, raid_disks);
1991 qd_idx = pd_idx + 1;
1992 if (pd_idx == raid_disks-1) {
1993 (*dd_idx)++; /* Q D D D P */
1995 } else if (*dd_idx >= pd_idx)
1996 (*dd_idx) += 2; /* D D P Q D */
1998 case ALGORITHM_LEFT_SYMMETRIC:
1999 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2000 qd_idx = (pd_idx + 1) % raid_disks;
2001 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2003 case ALGORITHM_RIGHT_SYMMETRIC:
2004 pd_idx = sector_div(stripe2, raid_disks);
2005 qd_idx = (pd_idx + 1) % raid_disks;
2006 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2009 case ALGORITHM_PARITY_0:
2014 case ALGORITHM_PARITY_N:
2015 pd_idx = data_disks;
2016 qd_idx = data_disks + 1;
2019 case ALGORITHM_ROTATING_ZERO_RESTART:
2020 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2021 * of blocks for computing Q is different.
2023 pd_idx = sector_div(stripe2, raid_disks);
2024 qd_idx = pd_idx + 1;
2025 if (pd_idx == raid_disks-1) {
2026 (*dd_idx)++; /* Q D D D P */
2028 } else if (*dd_idx >= pd_idx)
2029 (*dd_idx) += 2; /* D D P Q D */
2033 case ALGORITHM_ROTATING_N_RESTART:
2034 /* Same a left_asymmetric, by first stripe is
2035 * D D D P Q rather than
2039 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2040 qd_idx = pd_idx + 1;
2041 if (pd_idx == raid_disks-1) {
2042 (*dd_idx)++; /* Q D D D P */
2044 } else if (*dd_idx >= pd_idx)
2045 (*dd_idx) += 2; /* D D P Q D */
2049 case ALGORITHM_ROTATING_N_CONTINUE:
2050 /* Same as left_symmetric but Q is before P */
2051 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2052 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2053 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2057 case ALGORITHM_LEFT_ASYMMETRIC_6:
2058 /* RAID5 left_asymmetric, with Q on last device */
2059 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2060 if (*dd_idx >= pd_idx)
2062 qd_idx = raid_disks - 1;
2065 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2066 pd_idx = sector_div(stripe2, raid_disks-1);
2067 if (*dd_idx >= pd_idx)
2069 qd_idx = raid_disks - 1;
2072 case ALGORITHM_LEFT_SYMMETRIC_6:
2073 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2074 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2075 qd_idx = raid_disks - 1;
2078 case ALGORITHM_RIGHT_SYMMETRIC_6:
2079 pd_idx = sector_div(stripe2, raid_disks-1);
2080 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2081 qd_idx = raid_disks - 1;
2084 case ALGORITHM_PARITY_0_6:
2087 qd_idx = raid_disks - 1;
2097 sh->pd_idx = pd_idx;
2098 sh->qd_idx = qd_idx;
2099 sh->ddf_layout = ddf_layout;
2102 * Finally, compute the new sector number
2104 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2109 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
2111 struct r5conf *conf = sh->raid_conf;
2112 int raid_disks = sh->disks;
2113 int data_disks = raid_disks - conf->max_degraded;
2114 sector_t new_sector = sh->sector, check;
2115 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2116 : conf->chunk_sectors;
2117 int algorithm = previous ? conf->prev_algo
2121 sector_t chunk_number;
2122 int dummy1, dd_idx = i;
2124 struct stripe_head sh2;
2127 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2128 stripe = new_sector;
2130 if (i == sh->pd_idx)
2132 switch(conf->level) {
2135 switch (algorithm) {
2136 case ALGORITHM_LEFT_ASYMMETRIC:
2137 case ALGORITHM_RIGHT_ASYMMETRIC:
2141 case ALGORITHM_LEFT_SYMMETRIC:
2142 case ALGORITHM_RIGHT_SYMMETRIC:
2145 i -= (sh->pd_idx + 1);
2147 case ALGORITHM_PARITY_0:
2150 case ALGORITHM_PARITY_N:
2157 if (i == sh->qd_idx)
2158 return 0; /* It is the Q disk */
2159 switch (algorithm) {
2160 case ALGORITHM_LEFT_ASYMMETRIC:
2161 case ALGORITHM_RIGHT_ASYMMETRIC:
2162 case ALGORITHM_ROTATING_ZERO_RESTART:
2163 case ALGORITHM_ROTATING_N_RESTART:
2164 if (sh->pd_idx == raid_disks-1)
2165 i--; /* Q D D D P */
2166 else if (i > sh->pd_idx)
2167 i -= 2; /* D D P Q D */
2169 case ALGORITHM_LEFT_SYMMETRIC:
2170 case ALGORITHM_RIGHT_SYMMETRIC:
2171 if (sh->pd_idx == raid_disks-1)
2172 i--; /* Q D D D P */
2177 i -= (sh->pd_idx + 2);
2180 case ALGORITHM_PARITY_0:
2183 case ALGORITHM_PARITY_N:
2185 case ALGORITHM_ROTATING_N_CONTINUE:
2186 /* Like left_symmetric, but P is before Q */
2187 if (sh->pd_idx == 0)
2188 i--; /* P D D D Q */
2193 i -= (sh->pd_idx + 1);
2196 case ALGORITHM_LEFT_ASYMMETRIC_6:
2197 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2201 case ALGORITHM_LEFT_SYMMETRIC_6:
2202 case ALGORITHM_RIGHT_SYMMETRIC_6:
2204 i += data_disks + 1;
2205 i -= (sh->pd_idx + 1);
2207 case ALGORITHM_PARITY_0_6:
2216 chunk_number = stripe * data_disks + i;
2217 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
2219 check = raid5_compute_sector(conf, r_sector,
2220 previous, &dummy1, &sh2);
2221 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2222 || sh2.qd_idx != sh->qd_idx) {
2223 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2224 mdname(conf->mddev));
2232 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
2233 int rcw, int expand)
2235 int i, pd_idx = sh->pd_idx, disks = sh->disks;
2236 struct r5conf *conf = sh->raid_conf;
2237 int level = conf->level;
2240 /* if we are not expanding this is a proper write request, and
2241 * there will be bios with new data to be drained into the
2245 sh->reconstruct_state = reconstruct_state_drain_run;
2246 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2248 sh->reconstruct_state = reconstruct_state_run;
2250 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2252 for (i = disks; i--; ) {
2253 struct r5dev *dev = &sh->dev[i];
2256 set_bit(R5_LOCKED, &dev->flags);
2257 set_bit(R5_Wantdrain, &dev->flags);
2259 clear_bit(R5_UPTODATE, &dev->flags);
2263 if (s->locked + conf->max_degraded == disks)
2264 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2265 atomic_inc(&conf->pending_full_writes);
2268 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2269 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2271 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2272 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2273 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2274 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2276 for (i = disks; i--; ) {
2277 struct r5dev *dev = &sh->dev[i];
2282 (test_bit(R5_UPTODATE, &dev->flags) ||
2283 test_bit(R5_Wantcompute, &dev->flags))) {
2284 set_bit(R5_Wantdrain, &dev->flags);
2285 set_bit(R5_LOCKED, &dev->flags);
2286 clear_bit(R5_UPTODATE, &dev->flags);
2292 /* keep the parity disk(s) locked while asynchronous operations
2295 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2296 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2300 int qd_idx = sh->qd_idx;
2301 struct r5dev *dev = &sh->dev[qd_idx];
2303 set_bit(R5_LOCKED, &dev->flags);
2304 clear_bit(R5_UPTODATE, &dev->flags);
2308 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2309 __func__, (unsigned long long)sh->sector,
2310 s->locked, s->ops_request);
2314 * Each stripe/dev can have one or more bion attached.
2315 * toread/towrite point to the first in a chain.
2316 * The bi_next chain must be in order.
2318 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2321 struct r5conf *conf = sh->raid_conf;
2324 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2325 (unsigned long long)bi->bi_sector,
2326 (unsigned long long)sh->sector);
2329 spin_lock_irq(&conf->device_lock);
2331 bip = &sh->dev[dd_idx].towrite;
2332 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2335 bip = &sh->dev[dd_idx].toread;
2336 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2337 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2339 bip = & (*bip)->bi_next;
2341 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2344 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2348 bi->bi_phys_segments++;
2351 /* check if page is covered */
2352 sector_t sector = sh->dev[dd_idx].sector;
2353 for (bi=sh->dev[dd_idx].towrite;
2354 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2355 bi && bi->bi_sector <= sector;
2356 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2357 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2358 sector = bi->bi_sector + (bi->bi_size>>9);
2360 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2361 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2363 spin_unlock_irq(&conf->device_lock);
2365 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2366 (unsigned long long)(*bip)->bi_sector,
2367 (unsigned long long)sh->sector, dd_idx);
2369 if (conf->mddev->bitmap && firstwrite) {
2370 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2372 sh->bm_seq = conf->seq_flush+1;
2373 set_bit(STRIPE_BIT_DELAY, &sh->state);
2378 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2379 spin_unlock_irq(&conf->device_lock);
2383 static void end_reshape(struct r5conf *conf);
2385 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
2386 struct stripe_head *sh)
2388 int sectors_per_chunk =
2389 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2391 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2392 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2394 raid5_compute_sector(conf,
2395 stripe * (disks - conf->max_degraded)
2396 *sectors_per_chunk + chunk_offset,
2402 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
2403 struct stripe_head_state *s, int disks,
2404 struct bio **return_bi)
2407 for (i = disks; i--; ) {
2411 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2412 struct md_rdev *rdev;
2414 rdev = rcu_dereference(conf->disks[i].rdev);
2415 if (rdev && test_bit(In_sync, &rdev->flags))
2416 atomic_inc(&rdev->nr_pending);
2421 if (!rdev_set_badblocks(
2425 md_error(conf->mddev, rdev);
2426 rdev_dec_pending(rdev, conf->mddev);
2429 spin_lock_irq(&conf->device_lock);
2430 /* fail all writes first */
2431 bi = sh->dev[i].towrite;
2432 sh->dev[i].towrite = NULL;
2438 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2439 wake_up(&conf->wait_for_overlap);
2441 while (bi && bi->bi_sector <
2442 sh->dev[i].sector + STRIPE_SECTORS) {
2443 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2444 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2445 if (!raid5_dec_bi_phys_segments(bi)) {
2446 md_write_end(conf->mddev);
2447 bi->bi_next = *return_bi;
2452 /* and fail all 'written' */
2453 bi = sh->dev[i].written;
2454 sh->dev[i].written = NULL;
2455 if (bi) bitmap_end = 1;
2456 while (bi && bi->bi_sector <
2457 sh->dev[i].sector + STRIPE_SECTORS) {
2458 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2459 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2460 if (!raid5_dec_bi_phys_segments(bi)) {
2461 md_write_end(conf->mddev);
2462 bi->bi_next = *return_bi;
2468 /* fail any reads if this device is non-operational and
2469 * the data has not reached the cache yet.
2471 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2472 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2473 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2474 bi = sh->dev[i].toread;
2475 sh->dev[i].toread = NULL;
2476 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2477 wake_up(&conf->wait_for_overlap);
2478 if (bi) s->to_read--;
2479 while (bi && bi->bi_sector <
2480 sh->dev[i].sector + STRIPE_SECTORS) {
2481 struct bio *nextbi =
2482 r5_next_bio(bi, sh->dev[i].sector);
2483 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2484 if (!raid5_dec_bi_phys_segments(bi)) {
2485 bi->bi_next = *return_bi;
2491 spin_unlock_irq(&conf->device_lock);
2493 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2494 STRIPE_SECTORS, 0, 0);
2495 /* If we were in the middle of a write the parity block might
2496 * still be locked - so just clear all R5_LOCKED flags
2498 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2501 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2502 if (atomic_dec_and_test(&conf->pending_full_writes))
2503 md_wakeup_thread(conf->mddev->thread);
2507 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
2508 struct stripe_head_state *s)
2513 clear_bit(STRIPE_SYNCING, &sh->state);
2516 /* There is nothing more to do for sync/check/repair.
2517 * Don't even need to abort as that is handled elsewhere
2518 * if needed, and not always wanted e.g. if there is a known
2520 * For recover/replace we need to record a bad block on all
2521 * non-sync devices, or abort the recovery
2523 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2524 /* During recovery devices cannot be removed, so
2525 * locking and refcounting of rdevs is not needed
2527 for (i = 0; i < conf->raid_disks; i++) {
2528 struct md_rdev *rdev = conf->disks[i].rdev;
2530 && !test_bit(Faulty, &rdev->flags)
2531 && !test_bit(In_sync, &rdev->flags)
2532 && !rdev_set_badblocks(rdev, sh->sector,
2535 rdev = conf->disks[i].replacement;
2537 && !test_bit(Faulty, &rdev->flags)
2538 && !test_bit(In_sync, &rdev->flags)
2539 && !rdev_set_badblocks(rdev, sh->sector,
2544 conf->recovery_disabled =
2545 conf->mddev->recovery_disabled;
2547 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
2550 static int want_replace(struct stripe_head *sh, int disk_idx)
2552 struct md_rdev *rdev;
2554 /* Doing recovery so rcu locking not required */
2555 rdev = sh->raid_conf->disks[disk_idx].replacement;
2557 && !test_bit(Faulty, &rdev->flags)
2558 && !test_bit(In_sync, &rdev->flags)
2559 && (rdev->recovery_offset <= sh->sector
2560 || rdev->mddev->recovery_cp <= sh->sector))
2566 /* fetch_block - checks the given member device to see if its data needs
2567 * to be read or computed to satisfy a request.
2569 * Returns 1 when no more member devices need to be checked, otherwise returns
2570 * 0 to tell the loop in handle_stripe_fill to continue
2572 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2573 int disk_idx, int disks)
2575 struct r5dev *dev = &sh->dev[disk_idx];
2576 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2577 &sh->dev[s->failed_num[1]] };
2579 /* is the data in this block needed, and can we get it? */
2580 if (!test_bit(R5_LOCKED, &dev->flags) &&
2581 !test_bit(R5_UPTODATE, &dev->flags) &&
2583 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2584 s->syncing || s->expanding ||
2585 (s->replacing && want_replace(sh, disk_idx)) ||
2586 (s->failed >= 1 && fdev[0]->toread) ||
2587 (s->failed >= 2 && fdev[1]->toread) ||
2588 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2589 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2590 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
2591 /* we would like to get this block, possibly by computing it,
2592 * otherwise read it if the backing disk is insync
2594 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2595 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2596 if ((s->uptodate == disks - 1) &&
2597 (s->failed && (disk_idx == s->failed_num[0] ||
2598 disk_idx == s->failed_num[1]))) {
2599 /* have disk failed, and we're requested to fetch it;
2602 pr_debug("Computing stripe %llu block %d\n",
2603 (unsigned long long)sh->sector, disk_idx);
2604 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2605 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2606 set_bit(R5_Wantcompute, &dev->flags);
2607 sh->ops.target = disk_idx;
2608 sh->ops.target2 = -1; /* no 2nd target */
2610 /* Careful: from this point on 'uptodate' is in the eye
2611 * of raid_run_ops which services 'compute' operations
2612 * before writes. R5_Wantcompute flags a block that will
2613 * be R5_UPTODATE by the time it is needed for a
2614 * subsequent operation.
2618 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2619 /* Computing 2-failure is *very* expensive; only
2620 * do it if failed >= 2
2623 for (other = disks; other--; ) {
2624 if (other == disk_idx)
2626 if (!test_bit(R5_UPTODATE,
2627 &sh->dev[other].flags))
2631 pr_debug("Computing stripe %llu blocks %d,%d\n",
2632 (unsigned long long)sh->sector,
2634 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2635 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2636 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2637 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2638 sh->ops.target = disk_idx;
2639 sh->ops.target2 = other;
2643 } else if (test_bit(R5_Insync, &dev->flags)) {
2644 set_bit(R5_LOCKED, &dev->flags);
2645 set_bit(R5_Wantread, &dev->flags);
2647 pr_debug("Reading block %d (sync=%d)\n",
2648 disk_idx, s->syncing);
2656 * handle_stripe_fill - read or compute data to satisfy pending requests.
2658 static void handle_stripe_fill(struct stripe_head *sh,
2659 struct stripe_head_state *s,
2664 /* look for blocks to read/compute, skip this if a compute
2665 * is already in flight, or if the stripe contents are in the
2666 * midst of changing due to a write
2668 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2669 !sh->reconstruct_state)
2670 for (i = disks; i--; )
2671 if (fetch_block(sh, s, i, disks))
2673 set_bit(STRIPE_HANDLE, &sh->state);
2677 /* handle_stripe_clean_event
2678 * any written block on an uptodate or failed drive can be returned.
2679 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2680 * never LOCKED, so we don't need to test 'failed' directly.
2682 static void handle_stripe_clean_event(struct r5conf *conf,
2683 struct stripe_head *sh, int disks, struct bio **return_bi)
2688 for (i = disks; i--; )
2689 if (sh->dev[i].written) {
2691 if (!test_bit(R5_LOCKED, &dev->flags) &&
2692 test_bit(R5_UPTODATE, &dev->flags)) {
2693 /* We can return any write requests */
2694 struct bio *wbi, *wbi2;
2696 pr_debug("Return write for disc %d\n", i);
2697 spin_lock_irq(&conf->device_lock);
2699 dev->written = NULL;
2700 while (wbi && wbi->bi_sector <
2701 dev->sector + STRIPE_SECTORS) {
2702 wbi2 = r5_next_bio(wbi, dev->sector);
2703 if (!raid5_dec_bi_phys_segments(wbi)) {
2704 md_write_end(conf->mddev);
2705 wbi->bi_next = *return_bi;
2710 if (dev->towrite == NULL)
2712 spin_unlock_irq(&conf->device_lock);
2714 bitmap_endwrite(conf->mddev->bitmap,
2717 !test_bit(STRIPE_DEGRADED, &sh->state),
2722 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2723 if (atomic_dec_and_test(&conf->pending_full_writes))
2724 md_wakeup_thread(conf->mddev->thread);
2727 static void handle_stripe_dirtying(struct r5conf *conf,
2728 struct stripe_head *sh,
2729 struct stripe_head_state *s,
2732 int rmw = 0, rcw = 0, i;
2733 if (conf->max_degraded == 2) {
2734 /* RAID6 requires 'rcw' in current implementation
2735 * Calculate the real rcw later - for now fake it
2736 * look like rcw is cheaper
2739 } else for (i = disks; i--; ) {
2740 /* would I have to read this buffer for read_modify_write */
2741 struct r5dev *dev = &sh->dev[i];
2742 if ((dev->towrite || i == sh->pd_idx) &&
2743 !test_bit(R5_LOCKED, &dev->flags) &&
2744 !(test_bit(R5_UPTODATE, &dev->flags) ||
2745 test_bit(R5_Wantcompute, &dev->flags))) {
2746 if (test_bit(R5_Insync, &dev->flags))
2749 rmw += 2*disks; /* cannot read it */
2751 /* Would I have to read this buffer for reconstruct_write */
2752 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2753 !test_bit(R5_LOCKED, &dev->flags) &&
2754 !(test_bit(R5_UPTODATE, &dev->flags) ||
2755 test_bit(R5_Wantcompute, &dev->flags))) {
2756 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2761 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2762 (unsigned long long)sh->sector, rmw, rcw);
2763 set_bit(STRIPE_HANDLE, &sh->state);
2764 if (rmw < rcw && rmw > 0)
2765 /* prefer read-modify-write, but need to get some data */
2766 for (i = disks; i--; ) {
2767 struct r5dev *dev = &sh->dev[i];
2768 if ((dev->towrite || i == sh->pd_idx) &&
2769 !test_bit(R5_LOCKED, &dev->flags) &&
2770 !(test_bit(R5_UPTODATE, &dev->flags) ||
2771 test_bit(R5_Wantcompute, &dev->flags)) &&
2772 test_bit(R5_Insync, &dev->flags)) {
2774 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2775 pr_debug("Read_old block "
2776 "%d for r-m-w\n", i);
2777 set_bit(R5_LOCKED, &dev->flags);
2778 set_bit(R5_Wantread, &dev->flags);
2781 set_bit(STRIPE_DELAYED, &sh->state);
2782 set_bit(STRIPE_HANDLE, &sh->state);
2786 if (rcw <= rmw && rcw > 0) {
2787 /* want reconstruct write, but need to get some data */
2789 for (i = disks; i--; ) {
2790 struct r5dev *dev = &sh->dev[i];
2791 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2792 i != sh->pd_idx && i != sh->qd_idx &&
2793 !test_bit(R5_LOCKED, &dev->flags) &&
2794 !(test_bit(R5_UPTODATE, &dev->flags) ||
2795 test_bit(R5_Wantcompute, &dev->flags))) {
2797 if (!test_bit(R5_Insync, &dev->flags))
2798 continue; /* it's a failed drive */
2800 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2801 pr_debug("Read_old block "
2802 "%d for Reconstruct\n", i);
2803 set_bit(R5_LOCKED, &dev->flags);
2804 set_bit(R5_Wantread, &dev->flags);
2807 set_bit(STRIPE_DELAYED, &sh->state);
2808 set_bit(STRIPE_HANDLE, &sh->state);
2813 /* now if nothing is locked, and if we have enough data,
2814 * we can start a write request
2816 /* since handle_stripe can be called at any time we need to handle the
2817 * case where a compute block operation has been submitted and then a
2818 * subsequent call wants to start a write request. raid_run_ops only
2819 * handles the case where compute block and reconstruct are requested
2820 * simultaneously. If this is not the case then new writes need to be
2821 * held off until the compute completes.
2823 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2824 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2825 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2826 schedule_reconstruction(sh, s, rcw == 0, 0);
2829 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
2830 struct stripe_head_state *s, int disks)
2832 struct r5dev *dev = NULL;
2834 set_bit(STRIPE_HANDLE, &sh->state);
2836 switch (sh->check_state) {
2837 case check_state_idle:
2838 /* start a new check operation if there are no failures */
2839 if (s->failed == 0) {
2840 BUG_ON(s->uptodate != disks);
2841 sh->check_state = check_state_run;
2842 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2843 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2847 dev = &sh->dev[s->failed_num[0]];
2849 case check_state_compute_result:
2850 sh->check_state = check_state_idle;
2852 dev = &sh->dev[sh->pd_idx];
2854 /* check that a write has not made the stripe insync */
2855 if (test_bit(STRIPE_INSYNC, &sh->state))
2858 /* either failed parity check, or recovery is happening */
2859 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2860 BUG_ON(s->uptodate != disks);
2862 set_bit(R5_LOCKED, &dev->flags);
2864 set_bit(R5_Wantwrite, &dev->flags);
2866 clear_bit(STRIPE_DEGRADED, &sh->state);
2867 set_bit(STRIPE_INSYNC, &sh->state);
2869 case check_state_run:
2870 break; /* we will be called again upon completion */
2871 case check_state_check_result:
2872 sh->check_state = check_state_idle;
2874 /* if a failure occurred during the check operation, leave
2875 * STRIPE_INSYNC not set and let the stripe be handled again
2880 /* handle a successful check operation, if parity is correct
2881 * we are done. Otherwise update the mismatch count and repair
2882 * parity if !MD_RECOVERY_CHECK
2884 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
2885 /* parity is correct (on disc,
2886 * not in buffer any more)
2888 set_bit(STRIPE_INSYNC, &sh->state);
2890 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2891 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2892 /* don't try to repair!! */
2893 set_bit(STRIPE_INSYNC, &sh->state);
2895 sh->check_state = check_state_compute_run;
2896 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2897 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2898 set_bit(R5_Wantcompute,
2899 &sh->dev[sh->pd_idx].flags);
2900 sh->ops.target = sh->pd_idx;
2901 sh->ops.target2 = -1;
2906 case check_state_compute_run:
2909 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2910 __func__, sh->check_state,
2911 (unsigned long long) sh->sector);
2917 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
2918 struct stripe_head_state *s,
2921 int pd_idx = sh->pd_idx;
2922 int qd_idx = sh->qd_idx;
2925 set_bit(STRIPE_HANDLE, &sh->state);
2927 BUG_ON(s->failed > 2);
2929 /* Want to check and possibly repair P and Q.
2930 * However there could be one 'failed' device, in which
2931 * case we can only check one of them, possibly using the
2932 * other to generate missing data
2935 switch (sh->check_state) {
2936 case check_state_idle:
2937 /* start a new check operation if there are < 2 failures */
2938 if (s->failed == s->q_failed) {
2939 /* The only possible failed device holds Q, so it
2940 * makes sense to check P (If anything else were failed,
2941 * we would have used P to recreate it).
2943 sh->check_state = check_state_run;
2945 if (!s->q_failed && s->failed < 2) {
2946 /* Q is not failed, and we didn't use it to generate
2947 * anything, so it makes sense to check it
2949 if (sh->check_state == check_state_run)
2950 sh->check_state = check_state_run_pq;
2952 sh->check_state = check_state_run_q;
2955 /* discard potentially stale zero_sum_result */
2956 sh->ops.zero_sum_result = 0;
2958 if (sh->check_state == check_state_run) {
2959 /* async_xor_zero_sum destroys the contents of P */
2960 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2963 if (sh->check_state >= check_state_run &&
2964 sh->check_state <= check_state_run_pq) {
2965 /* async_syndrome_zero_sum preserves P and Q, so
2966 * no need to mark them !uptodate here
2968 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2972 /* we have 2-disk failure */
2973 BUG_ON(s->failed != 2);
2975 case check_state_compute_result:
2976 sh->check_state = check_state_idle;
2978 /* check that a write has not made the stripe insync */
2979 if (test_bit(STRIPE_INSYNC, &sh->state))
2982 /* now write out any block on a failed drive,
2983 * or P or Q if they were recomputed
2985 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
2986 if (s->failed == 2) {
2987 dev = &sh->dev[s->failed_num[1]];
2989 set_bit(R5_LOCKED, &dev->flags);
2990 set_bit(R5_Wantwrite, &dev->flags);
2992 if (s->failed >= 1) {
2993 dev = &sh->dev[s->failed_num[0]];
2995 set_bit(R5_LOCKED, &dev->flags);
2996 set_bit(R5_Wantwrite, &dev->flags);
2998 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2999 dev = &sh->dev[pd_idx];
3001 set_bit(R5_LOCKED, &dev->flags);
3002 set_bit(R5_Wantwrite, &dev->flags);
3004 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3005 dev = &sh->dev[qd_idx];
3007 set_bit(R5_LOCKED, &dev->flags);
3008 set_bit(R5_Wantwrite, &dev->flags);
3010 clear_bit(STRIPE_DEGRADED, &sh->state);
3012 set_bit(STRIPE_INSYNC, &sh->state);
3014 case check_state_run:
3015 case check_state_run_q:
3016 case check_state_run_pq:
3017 break; /* we will be called again upon completion */
3018 case check_state_check_result:
3019 sh->check_state = check_state_idle;
3021 /* handle a successful check operation, if parity is correct
3022 * we are done. Otherwise update the mismatch count and repair
3023 * parity if !MD_RECOVERY_CHECK
3025 if (sh->ops.zero_sum_result == 0) {
3026 /* both parities are correct */
3028 set_bit(STRIPE_INSYNC, &sh->state);
3030 /* in contrast to the raid5 case we can validate
3031 * parity, but still have a failure to write
3034 sh->check_state = check_state_compute_result;
3035 /* Returning at this point means that we may go
3036 * off and bring p and/or q uptodate again so
3037 * we make sure to check zero_sum_result again
3038 * to verify if p or q need writeback
3042 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3043 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3044 /* don't try to repair!! */
3045 set_bit(STRIPE_INSYNC, &sh->state);
3047 int *target = &sh->ops.target;
3049 sh->ops.target = -1;
3050 sh->ops.target2 = -1;
3051 sh->check_state = check_state_compute_run;
3052 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3053 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3054 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3055 set_bit(R5_Wantcompute,
3056 &sh->dev[pd_idx].flags);
3058 target = &sh->ops.target2;
3061 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3062 set_bit(R5_Wantcompute,
3063 &sh->dev[qd_idx].flags);
3070 case check_state_compute_run:
3073 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3074 __func__, sh->check_state,
3075 (unsigned long long) sh->sector);
3080 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
3084 /* We have read all the blocks in this stripe and now we need to
3085 * copy some of them into a target stripe for expand.
3087 struct dma_async_tx_descriptor *tx = NULL;
3088 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3089 for (i = 0; i < sh->disks; i++)
3090 if (i != sh->pd_idx && i != sh->qd_idx) {
3092 struct stripe_head *sh2;
3093 struct async_submit_ctl submit;
3095 sector_t bn = compute_blocknr(sh, i, 1);
3096 sector_t s = raid5_compute_sector(conf, bn, 0,
3098 sh2 = get_active_stripe(conf, s, 0, 1, 1);
3100 /* so far only the early blocks of this stripe
3101 * have been requested. When later blocks
3102 * get requested, we will try again
3105 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3106 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3107 /* must have already done this block */
3108 release_stripe(sh2);
3112 /* place all the copies on one channel */
3113 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
3114 tx = async_memcpy(sh2->dev[dd_idx].page,
3115 sh->dev[i].page, 0, 0, STRIPE_SIZE,
3118 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3119 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3120 for (j = 0; j < conf->raid_disks; j++)
3121 if (j != sh2->pd_idx &&
3123 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3125 if (j == conf->raid_disks) {
3126 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3127 set_bit(STRIPE_HANDLE, &sh2->state);
3129 release_stripe(sh2);
3132 /* done submitting copies, wait for them to complete */
3135 dma_wait_for_async_tx(tx);
3140 * handle_stripe - do things to a stripe.
3142 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3143 * state of various bits to see what needs to be done.
3145 * return some read requests which now have data
3146 * return some write requests which are safely on storage
3147 * schedule a read on some buffers
3148 * schedule a write of some buffers
3149 * return confirmation of parity correctness
3153 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
3155 struct r5conf *conf = sh->raid_conf;
3156 int disks = sh->disks;
3159 int do_recovery = 0;
3161 memset(s, 0, sizeof(*s));
3163 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3164 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3165 s->failed_num[0] = -1;
3166 s->failed_num[1] = -1;
3168 /* Now to look around and see what can be done */
3170 spin_lock_irq(&conf->device_lock);
3171 for (i=disks; i--; ) {
3172 struct md_rdev *rdev;
3179 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3181 dev->toread, dev->towrite, dev->written);
3182 /* maybe we can reply to a read
3184 * new wantfill requests are only permitted while
3185 * ops_complete_biofill is guaranteed to be inactive
3187 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3188 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3189 set_bit(R5_Wantfill, &dev->flags);
3191 /* now count some things */
3192 if (test_bit(R5_LOCKED, &dev->flags))
3194 if (test_bit(R5_UPTODATE, &dev->flags))
3196 if (test_bit(R5_Wantcompute, &dev->flags)) {
3198 BUG_ON(s->compute > 2);
3201 if (test_bit(R5_Wantfill, &dev->flags))
3203 else if (dev->toread)
3207 if (!test_bit(R5_OVERWRITE, &dev->flags))
3212 /* Prefer to use the replacement for reads, but only
3213 * if it is recovered enough and has no bad blocks.
3215 rdev = rcu_dereference(conf->disks[i].replacement);
3216 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3217 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3218 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3219 &first_bad, &bad_sectors))
3220 set_bit(R5_ReadRepl, &dev->flags);
3223 set_bit(R5_NeedReplace, &dev->flags);
3224 rdev = rcu_dereference(conf->disks[i].rdev);
3225 clear_bit(R5_ReadRepl, &dev->flags);
3227 if (rdev && test_bit(Faulty, &rdev->flags))
3230 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3231 &first_bad, &bad_sectors);
3232 if (s->blocked_rdev == NULL
3233 && (test_bit(Blocked, &rdev->flags)
3236 set_bit(BlockedBadBlocks,
3238 s->blocked_rdev = rdev;
3239 atomic_inc(&rdev->nr_pending);
3242 clear_bit(R5_Insync, &dev->flags);
3246 /* also not in-sync */
3247 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3248 test_bit(R5_UPTODATE, &dev->flags)) {
3249 /* treat as in-sync, but with a read error
3250 * which we can now try to correct
3252 set_bit(R5_Insync, &dev->flags);
3253 set_bit(R5_ReadError, &dev->flags);
3255 } else if (test_bit(In_sync, &rdev->flags))
3256 set_bit(R5_Insync, &dev->flags);
3257 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3258 /* in sync if before recovery_offset */
3259 set_bit(R5_Insync, &dev->flags);
3260 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3261 test_bit(R5_Expanded, &dev->flags))
3262 /* If we've reshaped into here, we assume it is Insync.
3263 * We will shortly update recovery_offset to make
3266 set_bit(R5_Insync, &dev->flags);
3268 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
3269 /* This flag does not apply to '.replacement'
3270 * only to .rdev, so make sure to check that*/
3271 struct md_rdev *rdev2 = rcu_dereference(
3272 conf->disks[i].rdev);
3274 clear_bit(R5_Insync, &dev->flags);
3275 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3276 s->handle_bad_blocks = 1;
3277 atomic_inc(&rdev2->nr_pending);
3279 clear_bit(R5_WriteError, &dev->flags);
3281 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
3282 /* This flag does not apply to '.replacement'
3283 * only to .rdev, so make sure to check that*/
3284 struct md_rdev *rdev2 = rcu_dereference(
3285 conf->disks[i].rdev);
3286 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3287 s->handle_bad_blocks = 1;
3288 atomic_inc(&rdev2->nr_pending);
3290 clear_bit(R5_MadeGood, &dev->flags);
3292 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3293 struct md_rdev *rdev2 = rcu_dereference(
3294 conf->disks[i].replacement);
3295 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3296 s->handle_bad_blocks = 1;
3297 atomic_inc(&rdev2->nr_pending);
3299 clear_bit(R5_MadeGoodRepl, &dev->flags);
3301 if (!test_bit(R5_Insync, &dev->flags)) {
3302 /* The ReadError flag will just be confusing now */
3303 clear_bit(R5_ReadError, &dev->flags);
3304 clear_bit(R5_ReWrite, &dev->flags);
3306 if (test_bit(R5_ReadError, &dev->flags))
3307 clear_bit(R5_Insync, &dev->flags);
3308 if (!test_bit(R5_Insync, &dev->flags)) {
3310 s->failed_num[s->failed] = i;
3312 if (rdev && !test_bit(Faulty, &rdev->flags))
3316 spin_unlock_irq(&conf->device_lock);
3317 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3318 /* If there is a failed device being replaced,
3319 * we must be recovering.
3320 * else if we are after recovery_cp, we must be syncing
3321 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3322 * else we can only be replacing
3323 * sync and recovery both need to read all devices, and so
3324 * use the same flag.
3327 sh->sector >= conf->mddev->recovery_cp ||
3328 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
3336 static void handle_stripe(struct stripe_head *sh)
3338 struct stripe_head_state s;
3339 struct r5conf *conf = sh->raid_conf;
3342 int disks = sh->disks;
3343 struct r5dev *pdev, *qdev;
3345 clear_bit(STRIPE_HANDLE, &sh->state);
3346 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
3347 /* already being handled, ensure it gets handled
3348 * again when current action finishes */
3349 set_bit(STRIPE_HANDLE, &sh->state);
3353 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3354 set_bit(STRIPE_SYNCING, &sh->state);
3355 clear_bit(STRIPE_INSYNC, &sh->state);
3357 clear_bit(STRIPE_DELAYED, &sh->state);
3359 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3360 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3361 (unsigned long long)sh->sector, sh->state,
3362 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3363 sh->check_state, sh->reconstruct_state);
3365 analyse_stripe(sh, &s);
3367 if (s.handle_bad_blocks) {
3368 set_bit(STRIPE_HANDLE, &sh->state);
3372 if (unlikely(s.blocked_rdev)) {
3373 if (s.syncing || s.expanding || s.expanded ||
3374 s.replacing || s.to_write || s.written) {
3375 set_bit(STRIPE_HANDLE, &sh->state);
3378 /* There is nothing for the blocked_rdev to block */
3379 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3380 s.blocked_rdev = NULL;
3383 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3384 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3385 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3388 pr_debug("locked=%d uptodate=%d to_read=%d"
3389 " to_write=%d failed=%d failed_num=%d,%d\n",
3390 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3391 s.failed_num[0], s.failed_num[1]);
3392 /* check if the array has lost more than max_degraded devices and,
3393 * if so, some requests might need to be failed.
3395 if (s.failed > conf->max_degraded) {
3396 sh->check_state = 0;
3397 sh->reconstruct_state = 0;
3398 if (s.to_read+s.to_write+s.written)
3399 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3400 if (s.syncing + s.replacing)
3401 handle_failed_sync(conf, sh, &s);
3405 * might be able to return some write requests if the parity blocks
3406 * are safe, or on a failed drive
3408 pdev = &sh->dev[sh->pd_idx];
3409 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3410 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3411 qdev = &sh->dev[sh->qd_idx];
3412 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3413 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3417 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3418 && !test_bit(R5_LOCKED, &pdev->flags)
3419 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3420 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3421 && !test_bit(R5_LOCKED, &qdev->flags)
3422 && test_bit(R5_UPTODATE, &qdev->flags)))))
3423 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3425 /* Now we might consider reading some blocks, either to check/generate
3426 * parity, or to satisfy requests
3427 * or to load a block that is being partially written.
3429 if (s.to_read || s.non_overwrite
3430 || (conf->level == 6 && s.to_write && s.failed)
3431 || (s.syncing && (s.uptodate + s.compute < disks))
3434 handle_stripe_fill(sh, &s, disks);
3436 /* Now we check to see if any write operations have recently
3440 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3442 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3443 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3444 sh->reconstruct_state = reconstruct_state_idle;
3446 /* All the 'written' buffers and the parity block are ready to
3447 * be written back to disk
3449 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3450 BUG_ON(sh->qd_idx >= 0 &&
3451 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3452 for (i = disks; i--; ) {
3453 struct r5dev *dev = &sh->dev[i];
3454 if (test_bit(R5_LOCKED, &dev->flags) &&
3455 (i == sh->pd_idx || i == sh->qd_idx ||
3457 pr_debug("Writing block %d\n", i);
3458 set_bit(R5_Wantwrite, &dev->flags);
3461 if (!test_bit(R5_Insync, &dev->flags) ||
3462 ((i == sh->pd_idx || i == sh->qd_idx) &&
3464 set_bit(STRIPE_INSYNC, &sh->state);
3467 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3468 s.dec_preread_active = 1;
3471 /* Now to consider new write requests and what else, if anything
3472 * should be read. We do not handle new writes when:
3473 * 1/ A 'write' operation (copy+xor) is already in flight.
3474 * 2/ A 'check' operation is in flight, as it may clobber the parity
3477 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3478 handle_stripe_dirtying(conf, sh, &s, disks);
3480 /* maybe we need to check and possibly fix the parity for this stripe
3481 * Any reads will already have been scheduled, so we just see if enough
3482 * data is available. The parity check is held off while parity
3483 * dependent operations are in flight.
3485 if (sh->check_state ||
3486 (s.syncing && s.locked == 0 &&
3487 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3488 !test_bit(STRIPE_INSYNC, &sh->state))) {
3489 if (conf->level == 6)
3490 handle_parity_checks6(conf, sh, &s, disks);
3492 handle_parity_checks5(conf, sh, &s, disks);
3495 if (s.replacing && s.locked == 0
3496 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3497 /* Write out to replacement devices where possible */
3498 for (i = 0; i < conf->raid_disks; i++)
3499 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3500 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3501 set_bit(R5_WantReplace, &sh->dev[i].flags);
3502 set_bit(R5_LOCKED, &sh->dev[i].flags);
3505 set_bit(STRIPE_INSYNC, &sh->state);
3507 if ((s.syncing || s.replacing) && s.locked == 0 &&
3508 test_bit(STRIPE_INSYNC, &sh->state)) {
3509 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3510 clear_bit(STRIPE_SYNCING, &sh->state);
3513 /* If the failed drives are just a ReadError, then we might need
3514 * to progress the repair/check process
3516 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3517 for (i = 0; i < s.failed; i++) {
3518 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3519 if (test_bit(R5_ReadError, &dev->flags)
3520 && !test_bit(R5_LOCKED, &dev->flags)
3521 && test_bit(R5_UPTODATE, &dev->flags)
3523 if (!test_bit(R5_ReWrite, &dev->flags)) {
3524 set_bit(R5_Wantwrite, &dev->flags);
3525 set_bit(R5_ReWrite, &dev->flags);
3526 set_bit(R5_LOCKED, &dev->flags);
3529 /* let's read it back */
3530 set_bit(R5_Wantread, &dev->flags);
3531 set_bit(R5_LOCKED, &dev->flags);
3538 /* Finish reconstruct operations initiated by the expansion process */
3539 if (sh->reconstruct_state == reconstruct_state_result) {
3540 struct stripe_head *sh_src
3541 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3542 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3543 /* sh cannot be written until sh_src has been read.
3544 * so arrange for sh to be delayed a little
3546 set_bit(STRIPE_DELAYED, &sh->state);
3547 set_bit(STRIPE_HANDLE, &sh->state);
3548 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3550 atomic_inc(&conf->preread_active_stripes);
3551 release_stripe(sh_src);
3555 release_stripe(sh_src);
3557 sh->reconstruct_state = reconstruct_state_idle;
3558 clear_bit(STRIPE_EXPANDING, &sh->state);
3559 for (i = conf->raid_disks; i--; ) {
3560 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3561 set_bit(R5_LOCKED, &sh->dev[i].flags);
3566 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3567 !sh->reconstruct_state) {
3568 /* Need to write out all blocks after computing parity */
3569 sh->disks = conf->raid_disks;
3570 stripe_set_idx(sh->sector, conf, 0, sh);
3571 schedule_reconstruction(sh, &s, 1, 1);
3572 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3573 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3574 atomic_dec(&conf->reshape_stripes);
3575 wake_up(&conf->wait_for_overlap);
3576 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3579 if (s.expanding && s.locked == 0 &&
3580 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3581 handle_stripe_expansion(conf, sh);
3584 /* wait for this device to become unblocked */
3585 if (conf->mddev->external && unlikely(s.blocked_rdev))
3586 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
3588 if (s.handle_bad_blocks)
3589 for (i = disks; i--; ) {
3590 struct md_rdev *rdev;
3591 struct r5dev *dev = &sh->dev[i];
3592 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3593 /* We own a safe reference to the rdev */
3594 rdev = conf->disks[i].rdev;
3595 if (!rdev_set_badblocks(rdev, sh->sector,
3597 md_error(conf->mddev, rdev);
3598 rdev_dec_pending(rdev, conf->mddev);
3600 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3601 rdev = conf->disks[i].rdev;
3602 rdev_clear_badblocks(rdev, sh->sector,
3604 rdev_dec_pending(rdev, conf->mddev);
3606 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3607 rdev = conf->disks[i].replacement;
3609 /* rdev have been moved down */
3610 rdev = conf->disks[i].rdev;
3611 rdev_clear_badblocks(rdev, sh->sector,
3613 rdev_dec_pending(rdev, conf->mddev);
3618 raid_run_ops(sh, s.ops_request);
3622 if (s.dec_preread_active) {
3623 /* We delay this until after ops_run_io so that if make_request
3624 * is waiting on a flush, it won't continue until the writes
3625 * have actually been submitted.
3627 atomic_dec(&conf->preread_active_stripes);
3628 if (atomic_read(&conf->preread_active_stripes) <
3630 md_wakeup_thread(conf->mddev->thread);
3633 return_io(s.return_bi);
3635 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
3638 static void raid5_activate_delayed(struct r5conf *conf)
3640 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3641 while (!list_empty(&conf->delayed_list)) {
3642 struct list_head *l = conf->delayed_list.next;
3643 struct stripe_head *sh;
3644 sh = list_entry(l, struct stripe_head, lru);
3646 clear_bit(STRIPE_DELAYED, &sh->state);
3647 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3648 atomic_inc(&conf->preread_active_stripes);
3649 list_add_tail(&sh->lru, &conf->hold_list);
3654 static void activate_bit_delay(struct r5conf *conf)
3656 /* device_lock is held */
3657 struct list_head head;
3658 list_add(&head, &conf->bitmap_list);
3659 list_del_init(&conf->bitmap_list);
3660 while (!list_empty(&head)) {
3661 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3662 list_del_init(&sh->lru);
3663 atomic_inc(&sh->count);
3664 __release_stripe(conf, sh);
3668 int md_raid5_congested(struct mddev *mddev, int bits)
3670 struct r5conf *conf = mddev->private;
3672 /* No difference between reads and writes. Just check
3673 * how busy the stripe_cache is
3676 if (conf->inactive_blocked)
3680 if (list_empty_careful(&conf->inactive_list))
3685 EXPORT_SYMBOL_GPL(md_raid5_congested);
3687 static int raid5_congested(void *data, int bits)
3689 struct mddev *mddev = data;
3691 return mddev_congested(mddev, bits) ||
3692 md_raid5_congested(mddev, bits);
3695 /* We want read requests to align with chunks where possible,
3696 * but write requests don't need to.
3698 static int raid5_mergeable_bvec(struct request_queue *q,
3699 struct bvec_merge_data *bvm,
3700 struct bio_vec *biovec)
3702 struct mddev *mddev = q->queuedata;
3703 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
3705 unsigned int chunk_sectors = mddev->chunk_sectors;
3706 unsigned int bio_sectors = bvm->bi_size >> 9;
3708 if ((bvm->bi_rw & 1) == WRITE)
3709 return biovec->bv_len; /* always allow writes to be mergeable */
3711 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3712 chunk_sectors = mddev->new_chunk_sectors;
3713 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3714 if (max < 0) max = 0;
3715 if (max <= biovec->bv_len && bio_sectors == 0)
3716 return biovec->bv_len;
3722 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
3724 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3725 unsigned int chunk_sectors = mddev->chunk_sectors;
3726 unsigned int bio_sectors = bio->bi_size >> 9;
3728 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3729 chunk_sectors = mddev->new_chunk_sectors;
3730 return chunk_sectors >=
3731 ((sector & (chunk_sectors - 1)) + bio_sectors);
3735 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3736 * later sampled by raid5d.
3738 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
3740 unsigned long flags;
3742 spin_lock_irqsave(&conf->device_lock, flags);
3744 bi->bi_next = conf->retry_read_aligned_list;
3745 conf->retry_read_aligned_list = bi;
3747 spin_unlock_irqrestore(&conf->device_lock, flags);
3748 md_wakeup_thread(conf->mddev->thread);
3752 static struct bio *remove_bio_from_retry(struct r5conf *conf)
3756 bi = conf->retry_read_aligned;
3758 conf->retry_read_aligned = NULL;
3761 bi = conf->retry_read_aligned_list;
3763 conf->retry_read_aligned_list = bi->bi_next;
3766 * this sets the active strip count to 1 and the processed
3767 * strip count to zero (upper 8 bits)
3769 bi->bi_phys_segments = 1; /* biased count of active stripes */
3777 * The "raid5_align_endio" should check if the read succeeded and if it
3778 * did, call bio_endio on the original bio (having bio_put the new bio
3780 * If the read failed..
3782 static void raid5_align_endio(struct bio *bi, int error)
3784 struct bio* raid_bi = bi->bi_private;
3785 struct mddev *mddev;
3786 struct r5conf *conf;
3787 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3788 struct md_rdev *rdev;
3792 rdev = (void*)raid_bi->bi_next;
3793 raid_bi->bi_next = NULL;
3794 mddev = rdev->mddev;
3795 conf = mddev->private;
3797 rdev_dec_pending(rdev, conf->mddev);
3799 if (!error && uptodate) {
3800 bio_endio(raid_bi, 0);
3801 if (atomic_dec_and_test(&conf->active_aligned_reads))
3802 wake_up(&conf->wait_for_stripe);
3807 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3809 add_bio_to_retry(raid_bi, conf);
3812 static int bio_fits_rdev(struct bio *bi)
3814 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
3816 if ((bi->bi_size>>9) > queue_max_sectors(q))
3818 blk_recount_segments(q, bi);
3819 if (bi->bi_phys_segments > queue_max_segments(q))
3822 if (q->merge_bvec_fn)
3823 /* it's too hard to apply the merge_bvec_fn at this stage,
3832 static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
3834 struct r5conf *conf = mddev->private;
3836 struct bio* align_bi;
3837 struct md_rdev *rdev;
3838 sector_t end_sector;
3840 if (!in_chunk_boundary(mddev, raid_bio)) {
3841 pr_debug("chunk_aligned_read : non aligned\n");
3845 * use bio_clone_mddev to make a copy of the bio
3847 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
3851 * set bi_end_io to a new function, and set bi_private to the
3854 align_bi->bi_end_io = raid5_align_endio;
3855 align_bi->bi_private = raid_bio;
3859 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3863 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
3865 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3866 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3867 rdev->recovery_offset < end_sector) {
3868 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3870 (test_bit(Faulty, &rdev->flags) ||
3871 !(test_bit(In_sync, &rdev->flags) ||
3872 rdev->recovery_offset >= end_sector)))
3879 atomic_inc(&rdev->nr_pending);
3881 raid_bio->bi_next = (void*)rdev;
3882 align_bi->bi_bdev = rdev->bdev;
3883 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3884 /* No reshape active, so we can trust rdev->data_offset */
3885 align_bi->bi_sector += rdev->data_offset;
3887 if (!bio_fits_rdev(align_bi) ||
3888 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3889 &first_bad, &bad_sectors)) {
3890 /* too big in some way, or has a known bad block */
3892 rdev_dec_pending(rdev, mddev);
3896 spin_lock_irq(&conf->device_lock);
3897 wait_event_lock_irq(conf->wait_for_stripe,
3899 conf->device_lock, /* nothing */);
3900 atomic_inc(&conf->active_aligned_reads);
3901 spin_unlock_irq(&conf->device_lock);
3903 generic_make_request(align_bi);
3912 /* __get_priority_stripe - get the next stripe to process
3914 * Full stripe writes are allowed to pass preread active stripes up until
3915 * the bypass_threshold is exceeded. In general the bypass_count
3916 * increments when the handle_list is handled before the hold_list; however, it
3917 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3918 * stripe with in flight i/o. The bypass_count will be reset when the
3919 * head of the hold_list has changed, i.e. the head was promoted to the
3922 static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
3924 struct stripe_head *sh;
3926 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3928 list_empty(&conf->handle_list) ? "empty" : "busy",
3929 list_empty(&conf->hold_list) ? "empty" : "busy",
3930 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3932 if (!list_empty(&conf->handle_list)) {
3933 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3935 if (list_empty(&conf->hold_list))
3936 conf->bypass_count = 0;
3937 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3938 if (conf->hold_list.next == conf->last_hold)
3939 conf->bypass_count++;
3941 conf->last_hold = conf->hold_list.next;
3942 conf->bypass_count -= conf->bypass_threshold;
3943 if (conf->bypass_count < 0)
3944 conf->bypass_count = 0;
3947 } else if (!list_empty(&conf->hold_list) &&
3948 ((conf->bypass_threshold &&
3949 conf->bypass_count > conf->bypass_threshold) ||
3950 atomic_read(&conf->pending_full_writes) == 0)) {
3951 sh = list_entry(conf->hold_list.next,
3953 conf->bypass_count -= conf->bypass_threshold;
3954 if (conf->bypass_count < 0)
3955 conf->bypass_count = 0;
3959 list_del_init(&sh->lru);
3960 atomic_inc(&sh->count);
3961 BUG_ON(atomic_read(&sh->count) != 1);
3965 static void make_request(struct mddev *mddev, struct bio * bi)
3967 struct r5conf *conf = mddev->private;
3969 sector_t new_sector;
3970 sector_t logical_sector, last_sector;
3971 struct stripe_head *sh;
3972 const int rw = bio_data_dir(bi);
3976 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3977 md_flush_request(mddev, bi);
3981 md_write_start(mddev, bi);
3984 mddev->reshape_position == MaxSector &&
3985 chunk_aligned_read(mddev,bi))
3988 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3989 last_sector = bi->bi_sector + (bi->bi_size>>9);
3991 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
3993 plugged = mddev_check_plugged(mddev);
3994 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4000 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
4001 if (unlikely(conf->reshape_progress != MaxSector)) {
4002 /* spinlock is needed as reshape_progress may be
4003 * 64bit on a 32bit platform, and so it might be
4004 * possible to see a half-updated value
4005 * Of course reshape_progress could change after
4006 * the lock is dropped, so once we get a reference
4007 * to the stripe that we think it is, we will have
4010 spin_lock_irq(&conf->device_lock);
4011 if (mddev->reshape_backwards
4012 ? logical_sector < conf->reshape_progress
4013 : logical_sector >= conf->reshape_progress) {
4016 if (mddev->reshape_backwards
4017 ? logical_sector < conf->reshape_safe
4018 : logical_sector >= conf->reshape_safe) {
4019 spin_unlock_irq(&conf->device_lock);
4024 spin_unlock_irq(&conf->device_lock);
4027 new_sector = raid5_compute_sector(conf, logical_sector,
4030 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4031 (unsigned long long)new_sector,
4032 (unsigned long long)logical_sector);
4034 sh = get_active_stripe(conf, new_sector, previous,
4035 (bi->bi_rw&RWA_MASK), 0);
4037 if (unlikely(previous)) {
4038 /* expansion might have moved on while waiting for a
4039 * stripe, so we must do the range check again.
4040 * Expansion could still move past after this
4041 * test, but as we are holding a reference to
4042 * 'sh', we know that if that happens,
4043 * STRIPE_EXPANDING will get set and the expansion
4044 * won't proceed until we finish with the stripe.
4047 spin_lock_irq(&conf->device_lock);
4048 if (mddev->reshape_backwards
4049 ? logical_sector >= conf->reshape_progress
4050 : logical_sector < conf->reshape_progress)
4051 /* mismatch, need to try again */
4053 spin_unlock_irq(&conf->device_lock);
4062 logical_sector >= mddev->suspend_lo &&
4063 logical_sector < mddev->suspend_hi) {
4065 /* As the suspend_* range is controlled by
4066 * userspace, we want an interruptible
4069 flush_signals(current);
4070 prepare_to_wait(&conf->wait_for_overlap,
4071 &w, TASK_INTERRUPTIBLE);
4072 if (logical_sector >= mddev->suspend_lo &&
4073 logical_sector < mddev->suspend_hi)
4078 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
4079 !add_stripe_bio(sh, bi, dd_idx, rw)) {
4080 /* Stripe is busy expanding or
4081 * add failed due to overlap. Flush everything
4084 md_wakeup_thread(mddev->thread);
4089 finish_wait(&conf->wait_for_overlap, &w);
4090 set_bit(STRIPE_HANDLE, &sh->state);
4091 clear_bit(STRIPE_DELAYED, &sh->state);
4092 if ((bi->bi_rw & REQ_SYNC) &&
4093 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4094 atomic_inc(&conf->preread_active_stripes);
4097 /* cannot get stripe for read-ahead, just give-up */
4098 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4099 finish_wait(&conf->wait_for_overlap, &w);
4105 md_wakeup_thread(mddev->thread);
4107 spin_lock_irq(&conf->device_lock);
4108 remaining = raid5_dec_bi_phys_segments(bi);
4109 spin_unlock_irq(&conf->device_lock);
4110 if (remaining == 0) {
4113 md_write_end(mddev);
4119 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
4121 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
4123 /* reshaping is quite different to recovery/resync so it is
4124 * handled quite separately ... here.
4126 * On each call to sync_request, we gather one chunk worth of
4127 * destination stripes and flag them as expanding.
4128 * Then we find all the source stripes and request reads.
4129 * As the reads complete, handle_stripe will copy the data
4130 * into the destination stripe and release that stripe.
4132 struct r5conf *conf = mddev->private;
4133 struct stripe_head *sh;
4134 sector_t first_sector, last_sector;
4135 int raid_disks = conf->previous_raid_disks;
4136 int data_disks = raid_disks - conf->max_degraded;
4137 int new_data_disks = conf->raid_disks - conf->max_degraded;
4140 sector_t writepos, readpos, safepos;
4141 sector_t stripe_addr;
4142 int reshape_sectors;
4143 struct list_head stripes;
4145 if (sector_nr == 0) {
4146 /* If restarting in the middle, skip the initial sectors */
4147 if (mddev->reshape_backwards &&
4148 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4149 sector_nr = raid5_size(mddev, 0, 0)
4150 - conf->reshape_progress;
4151 } else if (!mddev->reshape_backwards &&
4152 conf->reshape_progress > 0)
4153 sector_nr = conf->reshape_progress;
4154 sector_div(sector_nr, new_data_disks);
4156 mddev->curr_resync_completed = sector_nr;
4157 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4163 /* We need to process a full chunk at a time.
4164 * If old and new chunk sizes differ, we need to process the
4167 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4168 reshape_sectors = mddev->new_chunk_sectors;
4170 reshape_sectors = mddev->chunk_sectors;
4172 /* We update the metadata at least every 10 seconds, or when
4173 * the data about to be copied would over-write the source of
4174 * the data at the front of the range. i.e. one new_stripe
4175 * along from reshape_progress new_maps to after where
4176 * reshape_safe old_maps to
4178 writepos = conf->reshape_progress;
4179 sector_div(writepos, new_data_disks);
4180 readpos = conf->reshape_progress;
4181 sector_div(readpos, data_disks);
4182 safepos = conf->reshape_safe;
4183 sector_div(safepos, data_disks);
4184 if (mddev->reshape_backwards) {
4185 writepos -= min_t(sector_t, reshape_sectors, writepos);
4186 readpos += reshape_sectors;
4187 safepos += reshape_sectors;
4189 writepos += reshape_sectors;
4190 readpos -= min_t(sector_t, reshape_sectors, readpos);
4191 safepos -= min_t(sector_t, reshape_sectors, safepos);
4194 /* Having calculated the 'writepos' possibly use it
4195 * to set 'stripe_addr' which is where we will write to.
4197 if (mddev->reshape_backwards) {
4198 BUG_ON(conf->reshape_progress == 0);
4199 stripe_addr = writepos;
4200 BUG_ON((mddev->dev_sectors &
4201 ~((sector_t)reshape_sectors - 1))
4202 - reshape_sectors - stripe_addr
4205 BUG_ON(writepos != sector_nr + reshape_sectors);
4206 stripe_addr = sector_nr;
4209 /* 'writepos' is the most advanced device address we might write.
4210 * 'readpos' is the least advanced device address we might read.
4211 * 'safepos' is the least address recorded in the metadata as having
4213 * If there is a min_offset_diff, these are adjusted either by
4214 * increasing the safepos/readpos if diff is negative, or
4215 * increasing writepos if diff is positive.
4216 * If 'readpos' is then behind 'writepos', there is no way that we can
4217 * ensure safety in the face of a crash - that must be done by userspace
4218 * making a backup of the data. So in that case there is no particular
4219 * rush to update metadata.
4220 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4221 * update the metadata to advance 'safepos' to match 'readpos' so that
4222 * we can be safe in the event of a crash.
4223 * So we insist on updating metadata if safepos is behind writepos and
4224 * readpos is beyond writepos.
4225 * In any case, update the metadata every 10 seconds.
4226 * Maybe that number should be configurable, but I'm not sure it is
4227 * worth it.... maybe it could be a multiple of safemode_delay???
4229 if (conf->min_offset_diff < 0) {
4230 safepos += -conf->min_offset_diff;
4231 readpos += -conf->min_offset_diff;
4233 writepos += conf->min_offset_diff;
4235 if ((mddev->reshape_backwards
4236 ? (safepos > writepos && readpos < writepos)
4237 : (safepos < writepos && readpos > writepos)) ||
4238 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4239 /* Cannot proceed until we've updated the superblock... */
4240 wait_event(conf->wait_for_overlap,
4241 atomic_read(&conf->reshape_stripes)==0);
4242 mddev->reshape_position = conf->reshape_progress;
4243 mddev->curr_resync_completed = sector_nr;
4244 conf->reshape_checkpoint = jiffies;
4245 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4246 md_wakeup_thread(mddev->thread);
4247 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4248 kthread_should_stop());
4249 spin_lock_irq(&conf->device_lock);
4250 conf->reshape_safe = mddev->reshape_position;
4251 spin_unlock_irq(&conf->device_lock);
4252 wake_up(&conf->wait_for_overlap);
4253 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4256 INIT_LIST_HEAD(&stripes);
4257 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
4259 int skipped_disk = 0;
4260 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
4261 set_bit(STRIPE_EXPANDING, &sh->state);
4262 atomic_inc(&conf->reshape_stripes);
4263 /* If any of this stripe is beyond the end of the old
4264 * array, then we need to zero those blocks
4266 for (j=sh->disks; j--;) {
4268 if (j == sh->pd_idx)
4270 if (conf->level == 6 &&
4273 s = compute_blocknr(sh, j, 0);
4274 if (s < raid5_size(mddev, 0, 0)) {
4278 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4279 set_bit(R5_Expanded, &sh->dev[j].flags);
4280 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4282 if (!skipped_disk) {
4283 set_bit(STRIPE_EXPAND_READY, &sh->state);
4284 set_bit(STRIPE_HANDLE, &sh->state);
4286 list_add(&sh->lru, &stripes);
4288 spin_lock_irq(&conf->device_lock);
4289 if (mddev->reshape_backwards)
4290 conf->reshape_progress -= reshape_sectors * new_data_disks;
4292 conf->reshape_progress += reshape_sectors * new_data_disks;
4293 spin_unlock_irq(&conf->device_lock);
4294 /* Ok, those stripe are ready. We can start scheduling
4295 * reads on the source stripes.
4296 * The source stripes are determined by mapping the first and last
4297 * block on the destination stripes.
4300 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
4303 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
4304 * new_data_disks - 1),
4306 if (last_sector >= mddev->dev_sectors)
4307 last_sector = mddev->dev_sectors - 1;
4308 while (first_sector <= last_sector) {
4309 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
4310 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4311 set_bit(STRIPE_HANDLE, &sh->state);
4313 first_sector += STRIPE_SECTORS;
4315 /* Now that the sources are clearly marked, we can release
4316 * the destination stripes
4318 while (!list_empty(&stripes)) {
4319 sh = list_entry(stripes.next, struct stripe_head, lru);
4320 list_del_init(&sh->lru);
4323 /* If this takes us to the resync_max point where we have to pause,
4324 * then we need to write out the superblock.
4326 sector_nr += reshape_sectors;
4327 if ((sector_nr - mddev->curr_resync_completed) * 2
4328 >= mddev->resync_max - mddev->curr_resync_completed) {
4329 /* Cannot proceed until we've updated the superblock... */
4330 wait_event(conf->wait_for_overlap,
4331 atomic_read(&conf->reshape_stripes) == 0);
4332 mddev->reshape_position = conf->reshape_progress;
4333 mddev->curr_resync_completed = sector_nr;
4334 conf->reshape_checkpoint = jiffies;
4335 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4336 md_wakeup_thread(mddev->thread);
4337 wait_event(mddev->sb_wait,
4338 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4339 || kthread_should_stop());
4340 spin_lock_irq(&conf->device_lock);
4341 conf->reshape_safe = mddev->reshape_position;
4342 spin_unlock_irq(&conf->device_lock);
4343 wake_up(&conf->wait_for_overlap);
4344 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4346 return reshape_sectors;
4349 /* FIXME go_faster isn't used */
4350 static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
4352 struct r5conf *conf = mddev->private;
4353 struct stripe_head *sh;
4354 sector_t max_sector = mddev->dev_sectors;
4355 sector_t sync_blocks;
4356 int still_degraded = 0;
4359 if (sector_nr >= max_sector) {
4360 /* just being told to finish up .. nothing much to do */
4362 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4367 if (mddev->curr_resync < max_sector) /* aborted */
4368 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4370 else /* completed sync */
4372 bitmap_close_sync(mddev->bitmap);
4377 /* Allow raid5_quiesce to complete */
4378 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4380 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4381 return reshape_request(mddev, sector_nr, skipped);
4383 /* No need to check resync_max as we never do more than one
4384 * stripe, and as resync_max will always be on a chunk boundary,
4385 * if the check in md_do_sync didn't fire, there is no chance
4386 * of overstepping resync_max here
4389 /* if there is too many failed drives and we are trying
4390 * to resync, then assert that we are finished, because there is
4391 * nothing we can do.
4393 if (mddev->degraded >= conf->max_degraded &&
4394 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4395 sector_t rv = mddev->dev_sectors - sector_nr;
4399 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4400 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4401 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4402 /* we can skip this block, and probably more */
4403 sync_blocks /= STRIPE_SECTORS;
4405 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4408 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4410 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
4412 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
4413 /* make sure we don't swamp the stripe cache if someone else
4414 * is trying to get access
4416 schedule_timeout_uninterruptible(1);
4418 /* Need to check if array will still be degraded after recovery/resync
4419 * We don't need to check the 'failed' flag as when that gets set,
4422 for (i = 0; i < conf->raid_disks; i++)
4423 if (conf->disks[i].rdev == NULL)
4426 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4428 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
4433 return STRIPE_SECTORS;
4436 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
4438 /* We may not be able to submit a whole bio at once as there
4439 * may not be enough stripe_heads available.
4440 * We cannot pre-allocate enough stripe_heads as we may need
4441 * more than exist in the cache (if we allow ever large chunks).
4442 * So we do one stripe head at a time and record in
4443 * ->bi_hw_segments how many have been done.
4445 * We *know* that this entire raid_bio is in one chunk, so
4446 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4448 struct stripe_head *sh;
4450 sector_t sector, logical_sector, last_sector;
4455 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4456 sector = raid5_compute_sector(conf, logical_sector,
4458 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4460 for (; logical_sector < last_sector;
4461 logical_sector += STRIPE_SECTORS,
4462 sector += STRIPE_SECTORS,
4465 if (scnt < raid5_bi_hw_segments(raid_bio))
4466 /* already done this stripe */
4469 sh = get_active_stripe(conf, sector, 0, 1, 0);
4472 /* failed to get a stripe - must wait */
4473 raid5_set_bi_hw_segments(raid_bio, scnt);
4474 conf->retry_read_aligned = raid_bio;
4478 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4480 raid5_set_bi_hw_segments(raid_bio, scnt);
4481 conf->retry_read_aligned = raid_bio;
4489 spin_lock_irq(&conf->device_lock);
4490 remaining = raid5_dec_bi_phys_segments(raid_bio);
4491 spin_unlock_irq(&conf->device_lock);
4493 bio_endio(raid_bio, 0);
4494 if (atomic_dec_and_test(&conf->active_aligned_reads))
4495 wake_up(&conf->wait_for_stripe);
4501 * This is our raid5 kernel thread.
4503 * We scan the hash table for stripes which can be handled now.
4504 * During the scan, completed stripes are saved for us by the interrupt
4505 * handler, so that they will not have to wait for our next wakeup.
4507 static void raid5d(struct mddev *mddev)
4509 struct stripe_head *sh;
4510 struct r5conf *conf = mddev->private;
4512 struct blk_plug plug;
4514 pr_debug("+++ raid5d active\n");
4516 md_check_recovery(mddev);
4518 blk_start_plug(&plug);
4520 spin_lock_irq(&conf->device_lock);
4524 if (atomic_read(&mddev->plug_cnt) == 0 &&
4525 !list_empty(&conf->bitmap_list)) {
4526 /* Now is a good time to flush some bitmap updates */
4528 spin_unlock_irq(&conf->device_lock);
4529 bitmap_unplug(mddev->bitmap);
4530 spin_lock_irq(&conf->device_lock);
4531 conf->seq_write = conf->seq_flush;
4532 activate_bit_delay(conf);
4534 if (atomic_read(&mddev->plug_cnt) == 0)
4535 raid5_activate_delayed(conf);
4537 while ((bio = remove_bio_from_retry(conf))) {
4539 spin_unlock_irq(&conf->device_lock);
4540 ok = retry_aligned_read(conf, bio);
4541 spin_lock_irq(&conf->device_lock);
4547 sh = __get_priority_stripe(conf);
4551 spin_unlock_irq(&conf->device_lock);
4558 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4559 md_check_recovery(mddev);
4561 spin_lock_irq(&conf->device_lock);
4563 pr_debug("%d stripes handled\n", handled);
4565 spin_unlock_irq(&conf->device_lock);
4567 async_tx_issue_pending_all();
4568 blk_finish_plug(&plug);
4570 pr_debug("--- raid5d inactive\n");
4574 raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
4576 struct r5conf *conf = mddev->private;
4578 return sprintf(page, "%d\n", conf->max_nr_stripes);
4584 raid5_set_cache_size(struct mddev *mddev, int size)
4586 struct r5conf *conf = mddev->private;
4589 if (size <= 16 || size > 32768)
4591 while (size < conf->max_nr_stripes) {
4592 if (drop_one_stripe(conf))
4593 conf->max_nr_stripes--;
4597 err = md_allow_write(mddev);
4600 while (size > conf->max_nr_stripes) {
4601 if (grow_one_stripe(conf))
4602 conf->max_nr_stripes++;
4607 EXPORT_SYMBOL(raid5_set_cache_size);
4610 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
4612 struct r5conf *conf = mddev->private;
4616 if (len >= PAGE_SIZE)
4621 if (strict_strtoul(page, 10, &new))
4623 err = raid5_set_cache_size(mddev, new);
4629 static struct md_sysfs_entry
4630 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4631 raid5_show_stripe_cache_size,
4632 raid5_store_stripe_cache_size);
4635 raid5_show_preread_threshold(struct mddev *mddev, char *page)
4637 struct r5conf *conf = mddev->private;
4639 return sprintf(page, "%d\n", conf->bypass_threshold);
4645 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
4647 struct r5conf *conf = mddev->private;
4649 if (len >= PAGE_SIZE)
4654 if (strict_strtoul(page, 10, &new))
4656 if (new > conf->max_nr_stripes)
4658 conf->bypass_threshold = new;
4662 static struct md_sysfs_entry
4663 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4665 raid5_show_preread_threshold,
4666 raid5_store_preread_threshold);
4669 stripe_cache_active_show(struct mddev *mddev, char *page)
4671 struct r5conf *conf = mddev->private;
4673 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4678 static struct md_sysfs_entry
4679 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
4681 static struct attribute *raid5_attrs[] = {
4682 &raid5_stripecache_size.attr,
4683 &raid5_stripecache_active.attr,
4684 &raid5_preread_bypass_threshold.attr,
4687 static struct attribute_group raid5_attrs_group = {
4689 .attrs = raid5_attrs,
4693 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
4695 struct r5conf *conf = mddev->private;
4698 sectors = mddev->dev_sectors;
4700 /* size is defined by the smallest of previous and new size */
4701 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
4703 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
4704 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
4705 return sectors * (raid_disks - conf->max_degraded);
4708 static void raid5_free_percpu(struct r5conf *conf)
4710 struct raid5_percpu *percpu;
4717 for_each_possible_cpu(cpu) {
4718 percpu = per_cpu_ptr(conf->percpu, cpu);
4719 safe_put_page(percpu->spare_page);
4720 kfree(percpu->scribble);
4722 #ifdef CONFIG_HOTPLUG_CPU
4723 unregister_cpu_notifier(&conf->cpu_notify);
4727 free_percpu(conf->percpu);
4730 static void free_conf(struct r5conf *conf)
4732 shrink_stripes(conf);
4733 raid5_free_percpu(conf);
4735 kfree(conf->stripe_hashtbl);
4739 #ifdef CONFIG_HOTPLUG_CPU
4740 static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4743 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
4744 long cpu = (long)hcpu;
4745 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4748 case CPU_UP_PREPARE:
4749 case CPU_UP_PREPARE_FROZEN:
4750 if (conf->level == 6 && !percpu->spare_page)
4751 percpu->spare_page = alloc_page(GFP_KERNEL);
4752 if (!percpu->scribble)
4753 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4755 if (!percpu->scribble ||
4756 (conf->level == 6 && !percpu->spare_page)) {
4757 safe_put_page(percpu->spare_page);
4758 kfree(percpu->scribble);
4759 pr_err("%s: failed memory allocation for cpu%ld\n",
4761 return notifier_from_errno(-ENOMEM);
4765 case CPU_DEAD_FROZEN:
4766 safe_put_page(percpu->spare_page);
4767 kfree(percpu->scribble);
4768 percpu->spare_page = NULL;
4769 percpu->scribble = NULL;
4778 static int raid5_alloc_percpu(struct r5conf *conf)
4781 struct page *spare_page;
4782 struct raid5_percpu __percpu *allcpus;
4786 allcpus = alloc_percpu(struct raid5_percpu);
4789 conf->percpu = allcpus;
4793 for_each_present_cpu(cpu) {
4794 if (conf->level == 6) {
4795 spare_page = alloc_page(GFP_KERNEL);
4800 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4802 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4807 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
4809 #ifdef CONFIG_HOTPLUG_CPU
4810 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4811 conf->cpu_notify.priority = 0;
4813 err = register_cpu_notifier(&conf->cpu_notify);
4820 static struct r5conf *setup_conf(struct mddev *mddev)
4822 struct r5conf *conf;
4823 int raid_disk, memory, max_disks;
4824 struct md_rdev *rdev;
4825 struct disk_info *disk;
4827 if (mddev->new_level != 5
4828 && mddev->new_level != 4
4829 && mddev->new_level != 6) {
4830 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
4831 mdname(mddev), mddev->new_level);
4832 return ERR_PTR(-EIO);
4834 if ((mddev->new_level == 5
4835 && !algorithm_valid_raid5(mddev->new_layout)) ||
4836 (mddev->new_level == 6
4837 && !algorithm_valid_raid6(mddev->new_layout))) {
4838 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
4839 mdname(mddev), mddev->new_layout);
4840 return ERR_PTR(-EIO);
4842 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4843 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
4844 mdname(mddev), mddev->raid_disks);
4845 return ERR_PTR(-EINVAL);
4848 if (!mddev->new_chunk_sectors ||
4849 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4850 !is_power_of_2(mddev->new_chunk_sectors)) {
4851 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4852 mdname(mddev), mddev->new_chunk_sectors << 9);
4853 return ERR_PTR(-EINVAL);
4856 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
4859 spin_lock_init(&conf->device_lock);
4860 init_waitqueue_head(&conf->wait_for_stripe);
4861 init_waitqueue_head(&conf->wait_for_overlap);
4862 INIT_LIST_HEAD(&conf->handle_list);
4863 INIT_LIST_HEAD(&conf->hold_list);
4864 INIT_LIST_HEAD(&conf->delayed_list);
4865 INIT_LIST_HEAD(&conf->bitmap_list);
4866 INIT_LIST_HEAD(&conf->inactive_list);
4867 atomic_set(&conf->active_stripes, 0);
4868 atomic_set(&conf->preread_active_stripes, 0);
4869 atomic_set(&conf->active_aligned_reads, 0);
4870 conf->bypass_threshold = BYPASS_THRESHOLD;
4871 conf->recovery_disabled = mddev->recovery_disabled - 1;
4873 conf->raid_disks = mddev->raid_disks;
4874 if (mddev->reshape_position == MaxSector)
4875 conf->previous_raid_disks = mddev->raid_disks;
4877 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4878 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4879 conf->scribble_len = scribble_len(max_disks);
4881 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
4886 conf->mddev = mddev;
4888 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4891 conf->level = mddev->new_level;
4892 if (raid5_alloc_percpu(conf) != 0)
4895 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
4897 rdev_for_each(rdev, mddev) {
4898 raid_disk = rdev->raid_disk;
4899 if (raid_disk >= max_disks
4902 disk = conf->disks + raid_disk;
4904 if (test_bit(Replacement, &rdev->flags)) {
4905 if (disk->replacement)
4907 disk->replacement = rdev;
4914 if (test_bit(In_sync, &rdev->flags)) {
4915 char b[BDEVNAME_SIZE];
4916 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4918 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
4919 } else if (rdev->saved_raid_disk != raid_disk)
4920 /* Cannot rely on bitmap to complete recovery */
4924 conf->chunk_sectors = mddev->new_chunk_sectors;
4925 conf->level = mddev->new_level;
4926 if (conf->level == 6)
4927 conf->max_degraded = 2;
4929 conf->max_degraded = 1;
4930 conf->algorithm = mddev->new_layout;
4931 conf->max_nr_stripes = NR_STRIPES;
4932 conf->reshape_progress = mddev->reshape_position;
4933 if (conf->reshape_progress != MaxSector) {
4934 conf->prev_chunk_sectors = mddev->chunk_sectors;
4935 conf->prev_algo = mddev->layout;
4938 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4939 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4940 if (grow_stripes(conf, conf->max_nr_stripes)) {
4942 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4943 mdname(mddev), memory);
4946 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4947 mdname(mddev), memory);
4949 conf->thread = md_register_thread(raid5d, mddev, NULL);
4950 if (!conf->thread) {
4952 "md/raid:%s: couldn't allocate thread.\n",
4962 return ERR_PTR(-EIO);
4964 return ERR_PTR(-ENOMEM);
4968 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4971 case ALGORITHM_PARITY_0:
4972 if (raid_disk < max_degraded)
4975 case ALGORITHM_PARITY_N:
4976 if (raid_disk >= raid_disks - max_degraded)
4979 case ALGORITHM_PARITY_0_6:
4980 if (raid_disk == 0 ||
4981 raid_disk == raid_disks - 1)
4984 case ALGORITHM_LEFT_ASYMMETRIC_6:
4985 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4986 case ALGORITHM_LEFT_SYMMETRIC_6:
4987 case ALGORITHM_RIGHT_SYMMETRIC_6:
4988 if (raid_disk == raid_disks - 1)
4994 static int run(struct mddev *mddev)
4996 struct r5conf *conf;
4997 int working_disks = 0;
4998 int dirty_parity_disks = 0;
4999 struct md_rdev *rdev;
5000 sector_t reshape_offset = 0;
5002 long long min_offset_diff = 0;
5005 if (mddev->recovery_cp != MaxSector)
5006 printk(KERN_NOTICE "md/raid:%s: not clean"
5007 " -- starting background reconstruction\n",
5010 rdev_for_each(rdev, mddev) {
5012 if (rdev->raid_disk < 0)
5014 diff = (rdev->new_data_offset - rdev->data_offset);
5016 min_offset_diff = diff;
5018 } else if (mddev->reshape_backwards &&
5019 diff < min_offset_diff)
5020 min_offset_diff = diff;
5021 else if (!mddev->reshape_backwards &&
5022 diff > min_offset_diff)
5023 min_offset_diff = diff;
5026 if (mddev->reshape_position != MaxSector) {
5027 /* Check that we can continue the reshape.
5028 * Difficulties arise if the stripe we would write to
5029 * next is at or after the stripe we would read from next.
5030 * For a reshape that changes the number of devices, this
5031 * is only possible for a very short time, and mdadm makes
5032 * sure that time appears to have past before assembling
5033 * the array. So we fail if that time hasn't passed.
5034 * For a reshape that keeps the number of devices the same
5035 * mdadm must be monitoring the reshape can keeping the
5036 * critical areas read-only and backed up. It will start
5037 * the array in read-only mode, so we check for that.
5039 sector_t here_new, here_old;
5041 int max_degraded = (mddev->level == 6 ? 2 : 1);
5043 if (mddev->new_level != mddev->level) {
5044 printk(KERN_ERR "md/raid:%s: unsupported reshape "
5045 "required - aborting.\n",
5049 old_disks = mddev->raid_disks - mddev->delta_disks;
5050 /* reshape_position must be on a new-stripe boundary, and one
5051 * further up in new geometry must map after here in old
5054 here_new = mddev->reshape_position;
5055 if (sector_div(here_new, mddev->new_chunk_sectors *
5056 (mddev->raid_disks - max_degraded))) {
5057 printk(KERN_ERR "md/raid:%s: reshape_position not "
5058 "on a stripe boundary\n", mdname(mddev));
5061 reshape_offset = here_new * mddev->new_chunk_sectors;
5062 /* here_new is the stripe we will write to */
5063 here_old = mddev->reshape_position;
5064 sector_div(here_old, mddev->chunk_sectors *
5065 (old_disks-max_degraded));
5066 /* here_old is the first stripe that we might need to read
5068 if (mddev->delta_disks == 0) {
5069 if ((here_new * mddev->new_chunk_sectors !=
5070 here_old * mddev->chunk_sectors)) {
5071 printk(KERN_ERR "md/raid:%s: reshape position is"
5072 " confused - aborting\n", mdname(mddev));
5075 /* We cannot be sure it is safe to start an in-place
5076 * reshape. It is only safe if user-space is monitoring
5077 * and taking constant backups.
5078 * mdadm always starts a situation like this in
5079 * readonly mode so it can take control before
5080 * allowing any writes. So just check for that.
5082 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5083 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5084 /* not really in-place - so OK */;
5085 else if (mddev->ro == 0) {
5086 printk(KERN_ERR "md/raid:%s: in-place reshape "
5087 "must be started in read-only mode "
5092 } else if (mddev->reshape_backwards
5093 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
5094 here_old * mddev->chunk_sectors)
5095 : (here_new * mddev->new_chunk_sectors >=
5096 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
5097 /* Reading from the same stripe as writing to - bad */
5098 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5099 "auto-recovery - aborting.\n",
5103 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5105 /* OK, we should be able to continue; */
5107 BUG_ON(mddev->level != mddev->new_level);
5108 BUG_ON(mddev->layout != mddev->new_layout);
5109 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
5110 BUG_ON(mddev->delta_disks != 0);
5113 if (mddev->private == NULL)
5114 conf = setup_conf(mddev);
5116 conf = mddev->private;
5119 return PTR_ERR(conf);
5121 conf->min_offset_diff = min_offset_diff;
5122 mddev->thread = conf->thread;
5123 conf->thread = NULL;
5124 mddev->private = conf;
5126 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5128 rdev = conf->disks[i].rdev;
5129 if (!rdev && conf->disks[i].replacement) {
5130 /* The replacement is all we have yet */
5131 rdev = conf->disks[i].replacement;
5132 conf->disks[i].replacement = NULL;
5133 clear_bit(Replacement, &rdev->flags);
5134 conf->disks[i].rdev = rdev;
5138 if (conf->disks[i].replacement &&
5139 conf->reshape_progress != MaxSector) {
5140 /* replacements and reshape simply do not mix. */
5141 printk(KERN_ERR "md: cannot handle concurrent "
5142 "replacement and reshape.\n");
5145 if (test_bit(In_sync, &rdev->flags)) {
5149 /* This disc is not fully in-sync. However if it
5150 * just stored parity (beyond the recovery_offset),
5151 * when we don't need to be concerned about the
5152 * array being dirty.
5153 * When reshape goes 'backwards', we never have
5154 * partially completed devices, so we only need
5155 * to worry about reshape going forwards.
5157 /* Hack because v0.91 doesn't store recovery_offset properly. */
5158 if (mddev->major_version == 0 &&
5159 mddev->minor_version > 90)
5160 rdev->recovery_offset = reshape_offset;
5162 if (rdev->recovery_offset < reshape_offset) {
5163 /* We need to check old and new layout */
5164 if (!only_parity(rdev->raid_disk,
5167 conf->max_degraded))
5170 if (!only_parity(rdev->raid_disk,
5172 conf->previous_raid_disks,
5173 conf->max_degraded))
5175 dirty_parity_disks++;
5179 * 0 for a fully functional array, 1 or 2 for a degraded array.
5181 mddev->degraded = calc_degraded(conf);
5183 if (has_failed(conf)) {
5184 printk(KERN_ERR "md/raid:%s: not enough operational devices"
5185 " (%d/%d failed)\n",
5186 mdname(mddev), mddev->degraded, conf->raid_disks);
5190 /* device size must be a multiple of chunk size */
5191 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
5192 mddev->resync_max_sectors = mddev->dev_sectors;
5194 if (mddev->degraded > dirty_parity_disks &&
5195 mddev->recovery_cp != MaxSector) {
5196 if (mddev->ok_start_degraded)
5198 "md/raid:%s: starting dirty degraded array"
5199 " - data corruption possible.\n",
5203 "md/raid:%s: cannot start dirty degraded array.\n",
5209 if (mddev->degraded == 0)
5210 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5211 " devices, algorithm %d\n", mdname(mddev), conf->level,
5212 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5215 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5216 " out of %d devices, algorithm %d\n",
5217 mdname(mddev), conf->level,
5218 mddev->raid_disks - mddev->degraded,
5219 mddev->raid_disks, mddev->new_layout);
5221 print_raid5_conf(conf);
5223 if (conf->reshape_progress != MaxSector) {
5224 conf->reshape_safe = conf->reshape_progress;
5225 atomic_set(&conf->reshape_stripes, 0);
5226 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5227 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5228 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5229 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5230 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5235 /* Ok, everything is just fine now */
5236 if (mddev->to_remove == &raid5_attrs_group)
5237 mddev->to_remove = NULL;
5238 else if (mddev->kobj.sd &&
5239 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5241 "raid5: failed to create sysfs attributes for %s\n",
5243 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5247 /* read-ahead size must cover two whole stripes, which
5248 * is 2 * (datadisks) * chunksize where 'n' is the
5249 * number of raid devices
5251 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5252 int stripe = data_disks *
5253 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5254 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5255 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5257 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
5259 mddev->queue->backing_dev_info.congested_data = mddev;
5260 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
5262 chunk_size = mddev->chunk_sectors << 9;
5263 blk_queue_io_min(mddev->queue, chunk_size);
5264 blk_queue_io_opt(mddev->queue, chunk_size *
5265 (conf->raid_disks - conf->max_degraded));
5267 rdev_for_each(rdev, mddev) {
5268 disk_stack_limits(mddev->gendisk, rdev->bdev,
5269 rdev->data_offset << 9);
5270 disk_stack_limits(mddev->gendisk, rdev->bdev,
5271 rdev->new_data_offset << 9);
5277 md_unregister_thread(&mddev->thread);
5278 print_raid5_conf(conf);
5280 mddev->private = NULL;
5281 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
5285 static int stop(struct mddev *mddev)
5287 struct r5conf *conf = mddev->private;
5289 md_unregister_thread(&mddev->thread);
5291 mddev->queue->backing_dev_info.congested_fn = NULL;
5293 mddev->private = NULL;
5294 mddev->to_remove = &raid5_attrs_group;
5298 static void status(struct seq_file *seq, struct mddev *mddev)
5300 struct r5conf *conf = mddev->private;
5303 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5304 mddev->chunk_sectors / 2, mddev->layout);
5305 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5306 for (i = 0; i < conf->raid_disks; i++)
5307 seq_printf (seq, "%s",
5308 conf->disks[i].rdev &&
5309 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
5310 seq_printf (seq, "]");
5313 static void print_raid5_conf (struct r5conf *conf)
5316 struct disk_info *tmp;
5318 printk(KERN_DEBUG "RAID conf printout:\n");
5320 printk("(conf==NULL)\n");
5323 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5325 conf->raid_disks - conf->mddev->degraded);
5327 for (i = 0; i < conf->raid_disks; i++) {
5328 char b[BDEVNAME_SIZE];
5329 tmp = conf->disks + i;
5331 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5332 i, !test_bit(Faulty, &tmp->rdev->flags),
5333 bdevname(tmp->rdev->bdev, b));
5337 static int raid5_spare_active(struct mddev *mddev)
5340 struct r5conf *conf = mddev->private;
5341 struct disk_info *tmp;
5343 unsigned long flags;
5345 for (i = 0; i < conf->raid_disks; i++) {
5346 tmp = conf->disks + i;
5347 if (tmp->replacement
5348 && tmp->replacement->recovery_offset == MaxSector
5349 && !test_bit(Faulty, &tmp->replacement->flags)
5350 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5351 /* Replacement has just become active. */
5353 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5356 /* Replaced device not technically faulty,
5357 * but we need to be sure it gets removed
5358 * and never re-added.
5360 set_bit(Faulty, &tmp->rdev->flags);
5361 sysfs_notify_dirent_safe(
5362 tmp->rdev->sysfs_state);
5364 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5365 } else if (tmp->rdev
5366 && tmp->rdev->recovery_offset == MaxSector
5367 && !test_bit(Faulty, &tmp->rdev->flags)
5368 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5370 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
5373 spin_lock_irqsave(&conf->device_lock, flags);
5374 mddev->degraded = calc_degraded(conf);
5375 spin_unlock_irqrestore(&conf->device_lock, flags);
5376 print_raid5_conf(conf);
5380 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
5382 struct r5conf *conf = mddev->private;
5384 int number = rdev->raid_disk;
5385 struct md_rdev **rdevp;
5386 struct disk_info *p = conf->disks + number;
5388 print_raid5_conf(conf);
5389 if (rdev == p->rdev)
5391 else if (rdev == p->replacement)
5392 rdevp = &p->replacement;
5396 if (number >= conf->raid_disks &&
5397 conf->reshape_progress == MaxSector)
5398 clear_bit(In_sync, &rdev->flags);
5400 if (test_bit(In_sync, &rdev->flags) ||
5401 atomic_read(&rdev->nr_pending)) {
5405 /* Only remove non-faulty devices if recovery
5408 if (!test_bit(Faulty, &rdev->flags) &&
5409 mddev->recovery_disabled != conf->recovery_disabled &&
5410 !has_failed(conf) &&
5411 (!p->replacement || p->replacement == rdev) &&
5412 number < conf->raid_disks) {
5418 if (atomic_read(&rdev->nr_pending)) {
5419 /* lost the race, try later */
5422 } else if (p->replacement) {
5423 /* We must have just cleared 'rdev' */
5424 p->rdev = p->replacement;
5425 clear_bit(Replacement, &p->replacement->flags);
5426 smp_mb(); /* Make sure other CPUs may see both as identical
5427 * but will never see neither - if they are careful
5429 p->replacement = NULL;
5430 clear_bit(WantReplacement, &rdev->flags);
5432 /* We might have just removed the Replacement as faulty-
5433 * clear the bit just in case
5435 clear_bit(WantReplacement, &rdev->flags);
5438 print_raid5_conf(conf);
5442 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
5444 struct r5conf *conf = mddev->private;
5447 struct disk_info *p;
5449 int last = conf->raid_disks - 1;
5451 if (mddev->recovery_disabled == conf->recovery_disabled)
5454 if (rdev->saved_raid_disk < 0 && has_failed(conf))
5455 /* no point adding a device */
5458 if (rdev->raid_disk >= 0)
5459 first = last = rdev->raid_disk;
5462 * find the disk ... but prefer rdev->saved_raid_disk
5465 if (rdev->saved_raid_disk >= 0 &&
5466 rdev->saved_raid_disk >= first &&
5467 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5468 disk = rdev->saved_raid_disk;
5471 for ( ; disk <= last ; disk++) {
5472 p = conf->disks + disk;
5473 if (p->rdev == NULL) {
5474 clear_bit(In_sync, &rdev->flags);
5475 rdev->raid_disk = disk;
5477 if (rdev->saved_raid_disk != disk)
5479 rcu_assign_pointer(p->rdev, rdev);
5482 if (test_bit(WantReplacement, &p->rdev->flags) &&
5483 p->replacement == NULL) {
5484 clear_bit(In_sync, &rdev->flags);
5485 set_bit(Replacement, &rdev->flags);
5486 rdev->raid_disk = disk;
5489 rcu_assign_pointer(p->replacement, rdev);
5493 print_raid5_conf(conf);
5497 static int raid5_resize(struct mddev *mddev, sector_t sectors)
5499 /* no resync is happening, and there is enough space
5500 * on all devices, so we can resize.
5501 * We need to make sure resync covers any new space.
5502 * If the array is shrinking we should possibly wait until
5503 * any io in the removed space completes, but it hardly seems
5507 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
5508 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5509 if (mddev->external_size &&
5510 mddev->array_sectors > newsize)
5512 if (mddev->bitmap) {
5513 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5517 md_set_array_sectors(mddev, newsize);
5518 set_capacity(mddev->gendisk, mddev->array_sectors);
5519 revalidate_disk(mddev->gendisk);
5520 if (sectors > mddev->dev_sectors &&
5521 mddev->recovery_cp > mddev->dev_sectors) {
5522 mddev->recovery_cp = mddev->dev_sectors;
5523 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5525 mddev->dev_sectors = sectors;
5526 mddev->resync_max_sectors = sectors;
5530 static int check_stripe_cache(struct mddev *mddev)
5532 /* Can only proceed if there are plenty of stripe_heads.
5533 * We need a minimum of one full stripe,, and for sensible progress
5534 * it is best to have about 4 times that.
5535 * If we require 4 times, then the default 256 4K stripe_heads will
5536 * allow for chunk sizes up to 256K, which is probably OK.
5537 * If the chunk size is greater, user-space should request more
5538 * stripe_heads first.
5540 struct r5conf *conf = mddev->private;
5541 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5542 > conf->max_nr_stripes ||
5543 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5544 > conf->max_nr_stripes) {
5545 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5547 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5554 static int check_reshape(struct mddev *mddev)
5556 struct r5conf *conf = mddev->private;
5558 if (mddev->delta_disks == 0 &&
5559 mddev->new_layout == mddev->layout &&
5560 mddev->new_chunk_sectors == mddev->chunk_sectors)
5561 return 0; /* nothing to do */
5562 if (has_failed(conf))
5564 if (mddev->delta_disks < 0) {
5565 /* We might be able to shrink, but the devices must
5566 * be made bigger first.
5567 * For raid6, 4 is the minimum size.
5568 * Otherwise 2 is the minimum
5571 if (mddev->level == 6)
5573 if (mddev->raid_disks + mddev->delta_disks < min)
5577 if (!check_stripe_cache(mddev))
5580 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
5583 static int raid5_start_reshape(struct mddev *mddev)
5585 struct r5conf *conf = mddev->private;
5586 struct md_rdev *rdev;
5588 unsigned long flags;
5590 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5593 if (!check_stripe_cache(mddev))
5596 if (has_failed(conf))
5599 rdev_for_each(rdev, mddev) {
5600 if (!test_bit(In_sync, &rdev->flags)
5601 && !test_bit(Faulty, &rdev->flags))
5605 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
5606 /* Not enough devices even to make a degraded array
5611 /* Refuse to reduce size of the array. Any reductions in
5612 * array size must be through explicit setting of array_size
5615 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5616 < mddev->array_sectors) {
5617 printk(KERN_ERR "md/raid:%s: array size must be reduced "
5618 "before number of disks\n", mdname(mddev));
5622 atomic_set(&conf->reshape_stripes, 0);
5623 spin_lock_irq(&conf->device_lock);
5624 conf->previous_raid_disks = conf->raid_disks;
5625 conf->raid_disks += mddev->delta_disks;
5626 conf->prev_chunk_sectors = conf->chunk_sectors;
5627 conf->chunk_sectors = mddev->new_chunk_sectors;
5628 conf->prev_algo = conf->algorithm;
5629 conf->algorithm = mddev->new_layout;
5631 /* Code that selects data_offset needs to see the generation update
5632 * if reshape_progress has been set - so a memory barrier needed.
5635 if (mddev->reshape_backwards)
5636 conf->reshape_progress = raid5_size(mddev, 0, 0);
5638 conf->reshape_progress = 0;
5639 conf->reshape_safe = conf->reshape_progress;
5640 spin_unlock_irq(&conf->device_lock);
5642 /* Add some new drives, as many as will fit.
5643 * We know there are enough to make the newly sized array work.
5644 * Don't add devices if we are reducing the number of
5645 * devices in the array. This is because it is not possible
5646 * to correctly record the "partially reconstructed" state of
5647 * such devices during the reshape and confusion could result.
5649 if (mddev->delta_disks >= 0) {
5650 rdev_for_each(rdev, mddev)
5651 if (rdev->raid_disk < 0 &&
5652 !test_bit(Faulty, &rdev->flags)) {
5653 if (raid5_add_disk(mddev, rdev) == 0) {
5655 >= conf->previous_raid_disks)
5656 set_bit(In_sync, &rdev->flags);
5658 rdev->recovery_offset = 0;
5660 if (sysfs_link_rdev(mddev, rdev))
5661 /* Failure here is OK */;
5663 } else if (rdev->raid_disk >= conf->previous_raid_disks
5664 && !test_bit(Faulty, &rdev->flags)) {
5665 /* This is a spare that was manually added */
5666 set_bit(In_sync, &rdev->flags);
5669 /* When a reshape changes the number of devices,
5670 * ->degraded is measured against the larger of the
5671 * pre and post number of devices.
5673 spin_lock_irqsave(&conf->device_lock, flags);
5674 mddev->degraded = calc_degraded(conf);
5675 spin_unlock_irqrestore(&conf->device_lock, flags);
5677 mddev->raid_disks = conf->raid_disks;
5678 mddev->reshape_position = conf->reshape_progress;
5679 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5681 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5682 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5683 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5684 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5685 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5687 if (!mddev->sync_thread) {
5688 mddev->recovery = 0;
5689 spin_lock_irq(&conf->device_lock);
5690 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
5691 rdev_for_each(rdev, mddev)
5692 rdev->new_data_offset = rdev->data_offset;
5694 conf->reshape_progress = MaxSector;
5695 mddev->reshape_position = MaxSector;
5696 spin_unlock_irq(&conf->device_lock);
5699 conf->reshape_checkpoint = jiffies;
5700 md_wakeup_thread(mddev->sync_thread);
5701 md_new_event(mddev);
5705 /* This is called from the reshape thread and should make any
5706 * changes needed in 'conf'
5708 static void end_reshape(struct r5conf *conf)
5711 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
5712 struct md_rdev *rdev;
5714 spin_lock_irq(&conf->device_lock);
5715 conf->previous_raid_disks = conf->raid_disks;
5716 rdev_for_each(rdev, conf->mddev)
5717 rdev->data_offset = rdev->new_data_offset;
5719 conf->reshape_progress = MaxSector;
5720 spin_unlock_irq(&conf->device_lock);
5721 wake_up(&conf->wait_for_overlap);
5723 /* read-ahead size must cover two whole stripes, which is
5724 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5726 if (conf->mddev->queue) {
5727 int data_disks = conf->raid_disks - conf->max_degraded;
5728 int stripe = data_disks * ((conf->chunk_sectors << 9)
5730 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5731 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5736 /* This is called from the raid5d thread with mddev_lock held.
5737 * It makes config changes to the device.
5739 static void raid5_finish_reshape(struct mddev *mddev)
5741 struct r5conf *conf = mddev->private;
5743 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5745 if (mddev->delta_disks > 0) {
5746 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5747 set_capacity(mddev->gendisk, mddev->array_sectors);
5748 revalidate_disk(mddev->gendisk);
5751 spin_lock_irq(&conf->device_lock);
5752 mddev->degraded = calc_degraded(conf);
5753 spin_unlock_irq(&conf->device_lock);
5754 for (d = conf->raid_disks ;
5755 d < conf->raid_disks - mddev->delta_disks;
5757 struct md_rdev *rdev = conf->disks[d].rdev;
5759 clear_bit(In_sync, &rdev->flags);
5760 rdev = conf->disks[d].replacement;
5762 clear_bit(In_sync, &rdev->flags);
5765 mddev->layout = conf->algorithm;
5766 mddev->chunk_sectors = conf->chunk_sectors;
5767 mddev->reshape_position = MaxSector;
5768 mddev->delta_disks = 0;
5769 mddev->reshape_backwards = 0;
5773 static void raid5_quiesce(struct mddev *mddev, int state)
5775 struct r5conf *conf = mddev->private;
5778 case 2: /* resume for a suspend */
5779 wake_up(&conf->wait_for_overlap);
5782 case 1: /* stop all writes */
5783 spin_lock_irq(&conf->device_lock);
5784 /* '2' tells resync/reshape to pause so that all
5785 * active stripes can drain
5788 wait_event_lock_irq(conf->wait_for_stripe,
5789 atomic_read(&conf->active_stripes) == 0 &&
5790 atomic_read(&conf->active_aligned_reads) == 0,
5791 conf->device_lock, /* nothing */);
5793 spin_unlock_irq(&conf->device_lock);
5794 /* allow reshape to continue */
5795 wake_up(&conf->wait_for_overlap);
5798 case 0: /* re-enable writes */
5799 spin_lock_irq(&conf->device_lock);
5801 wake_up(&conf->wait_for_stripe);
5802 wake_up(&conf->wait_for_overlap);
5803 spin_unlock_irq(&conf->device_lock);
5809 static void *raid45_takeover_raid0(struct mddev *mddev, int level)
5811 struct r0conf *raid0_conf = mddev->private;
5814 /* for raid0 takeover only one zone is supported */
5815 if (raid0_conf->nr_strip_zones > 1) {
5816 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5818 return ERR_PTR(-EINVAL);
5821 sectors = raid0_conf->strip_zone[0].zone_end;
5822 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
5823 mddev->dev_sectors = sectors;
5824 mddev->new_level = level;
5825 mddev->new_layout = ALGORITHM_PARITY_N;
5826 mddev->new_chunk_sectors = mddev->chunk_sectors;
5827 mddev->raid_disks += 1;
5828 mddev->delta_disks = 1;
5829 /* make sure it will be not marked as dirty */
5830 mddev->recovery_cp = MaxSector;
5832 return setup_conf(mddev);
5836 static void *raid5_takeover_raid1(struct mddev *mddev)
5840 if (mddev->raid_disks != 2 ||
5841 mddev->degraded > 1)
5842 return ERR_PTR(-EINVAL);
5844 /* Should check if there are write-behind devices? */
5846 chunksect = 64*2; /* 64K by default */
5848 /* The array must be an exact multiple of chunksize */
5849 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5852 if ((chunksect<<9) < STRIPE_SIZE)
5853 /* array size does not allow a suitable chunk size */
5854 return ERR_PTR(-EINVAL);
5856 mddev->new_level = 5;
5857 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
5858 mddev->new_chunk_sectors = chunksect;
5860 return setup_conf(mddev);
5863 static void *raid5_takeover_raid6(struct mddev *mddev)
5867 switch (mddev->layout) {
5868 case ALGORITHM_LEFT_ASYMMETRIC_6:
5869 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5871 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5872 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5874 case ALGORITHM_LEFT_SYMMETRIC_6:
5875 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5877 case ALGORITHM_RIGHT_SYMMETRIC_6:
5878 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5880 case ALGORITHM_PARITY_0_6:
5881 new_layout = ALGORITHM_PARITY_0;
5883 case ALGORITHM_PARITY_N:
5884 new_layout = ALGORITHM_PARITY_N;
5887 return ERR_PTR(-EINVAL);
5889 mddev->new_level = 5;
5890 mddev->new_layout = new_layout;
5891 mddev->delta_disks = -1;
5892 mddev->raid_disks -= 1;
5893 return setup_conf(mddev);
5897 static int raid5_check_reshape(struct mddev *mddev)
5899 /* For a 2-drive array, the layout and chunk size can be changed
5900 * immediately as not restriping is needed.
5901 * For larger arrays we record the new value - after validation
5902 * to be used by a reshape pass.
5904 struct r5conf *conf = mddev->private;
5905 int new_chunk = mddev->new_chunk_sectors;
5907 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
5909 if (new_chunk > 0) {
5910 if (!is_power_of_2(new_chunk))
5912 if (new_chunk < (PAGE_SIZE>>9))
5914 if (mddev->array_sectors & (new_chunk-1))
5915 /* not factor of array size */
5919 /* They look valid */
5921 if (mddev->raid_disks == 2) {
5922 /* can make the change immediately */
5923 if (mddev->new_layout >= 0) {
5924 conf->algorithm = mddev->new_layout;
5925 mddev->layout = mddev->new_layout;
5927 if (new_chunk > 0) {
5928 conf->chunk_sectors = new_chunk ;
5929 mddev->chunk_sectors = new_chunk;
5931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5932 md_wakeup_thread(mddev->thread);
5934 return check_reshape(mddev);
5937 static int raid6_check_reshape(struct mddev *mddev)
5939 int new_chunk = mddev->new_chunk_sectors;
5941 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
5943 if (new_chunk > 0) {
5944 if (!is_power_of_2(new_chunk))
5946 if (new_chunk < (PAGE_SIZE >> 9))
5948 if (mddev->array_sectors & (new_chunk-1))
5949 /* not factor of array size */
5953 /* They look valid */
5954 return check_reshape(mddev);
5957 static void *raid5_takeover(struct mddev *mddev)
5959 /* raid5 can take over:
5960 * raid0 - if there is only one strip zone - make it a raid4 layout
5961 * raid1 - if there are two drives. We need to know the chunk size
5962 * raid4 - trivial - just use a raid4 layout.
5963 * raid6 - Providing it is a *_6 layout
5965 if (mddev->level == 0)
5966 return raid45_takeover_raid0(mddev, 5);
5967 if (mddev->level == 1)
5968 return raid5_takeover_raid1(mddev);
5969 if (mddev->level == 4) {
5970 mddev->new_layout = ALGORITHM_PARITY_N;
5971 mddev->new_level = 5;
5972 return setup_conf(mddev);
5974 if (mddev->level == 6)
5975 return raid5_takeover_raid6(mddev);
5977 return ERR_PTR(-EINVAL);
5980 static void *raid4_takeover(struct mddev *mddev)
5982 /* raid4 can take over:
5983 * raid0 - if there is only one strip zone
5984 * raid5 - if layout is right
5986 if (mddev->level == 0)
5987 return raid45_takeover_raid0(mddev, 4);
5988 if (mddev->level == 5 &&
5989 mddev->layout == ALGORITHM_PARITY_N) {
5990 mddev->new_layout = 0;
5991 mddev->new_level = 4;
5992 return setup_conf(mddev);
5994 return ERR_PTR(-EINVAL);
5997 static struct md_personality raid5_personality;
5999 static void *raid6_takeover(struct mddev *mddev)
6001 /* Currently can only take over a raid5. We map the
6002 * personality to an equivalent raid6 personality
6003 * with the Q block at the end.
6007 if (mddev->pers != &raid5_personality)
6008 return ERR_PTR(-EINVAL);
6009 if (mddev->degraded > 1)
6010 return ERR_PTR(-EINVAL);
6011 if (mddev->raid_disks > 253)
6012 return ERR_PTR(-EINVAL);
6013 if (mddev->raid_disks < 3)
6014 return ERR_PTR(-EINVAL);
6016 switch (mddev->layout) {
6017 case ALGORITHM_LEFT_ASYMMETRIC:
6018 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6020 case ALGORITHM_RIGHT_ASYMMETRIC:
6021 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6023 case ALGORITHM_LEFT_SYMMETRIC:
6024 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6026 case ALGORITHM_RIGHT_SYMMETRIC:
6027 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6029 case ALGORITHM_PARITY_0:
6030 new_layout = ALGORITHM_PARITY_0_6;
6032 case ALGORITHM_PARITY_N:
6033 new_layout = ALGORITHM_PARITY_N;
6036 return ERR_PTR(-EINVAL);
6038 mddev->new_level = 6;
6039 mddev->new_layout = new_layout;
6040 mddev->delta_disks = 1;
6041 mddev->raid_disks += 1;
6042 return setup_conf(mddev);
6046 static struct md_personality raid6_personality =
6050 .owner = THIS_MODULE,
6051 .make_request = make_request,
6055 .error_handler = error,
6056 .hot_add_disk = raid5_add_disk,
6057 .hot_remove_disk= raid5_remove_disk,
6058 .spare_active = raid5_spare_active,
6059 .sync_request = sync_request,
6060 .resize = raid5_resize,
6062 .check_reshape = raid6_check_reshape,
6063 .start_reshape = raid5_start_reshape,
6064 .finish_reshape = raid5_finish_reshape,
6065 .quiesce = raid5_quiesce,
6066 .takeover = raid6_takeover,
6068 static struct md_personality raid5_personality =
6072 .owner = THIS_MODULE,
6073 .make_request = make_request,
6077 .error_handler = error,
6078 .hot_add_disk = raid5_add_disk,
6079 .hot_remove_disk= raid5_remove_disk,
6080 .spare_active = raid5_spare_active,
6081 .sync_request = sync_request,
6082 .resize = raid5_resize,
6084 .check_reshape = raid5_check_reshape,
6085 .start_reshape = raid5_start_reshape,
6086 .finish_reshape = raid5_finish_reshape,
6087 .quiesce = raid5_quiesce,
6088 .takeover = raid5_takeover,
6091 static struct md_personality raid4_personality =
6095 .owner = THIS_MODULE,
6096 .make_request = make_request,
6100 .error_handler = error,
6101 .hot_add_disk = raid5_add_disk,
6102 .hot_remove_disk= raid5_remove_disk,
6103 .spare_active = raid5_spare_active,
6104 .sync_request = sync_request,
6105 .resize = raid5_resize,
6107 .check_reshape = raid5_check_reshape,
6108 .start_reshape = raid5_start_reshape,
6109 .finish_reshape = raid5_finish_reshape,
6110 .quiesce = raid5_quiesce,
6111 .takeover = raid4_takeover,
6114 static int __init raid5_init(void)
6116 register_md_personality(&raid6_personality);
6117 register_md_personality(&raid5_personality);
6118 register_md_personality(&raid4_personality);
6122 static void raid5_exit(void)
6124 unregister_md_personality(&raid6_personality);
6125 unregister_md_personality(&raid5_personality);
6126 unregister_md_personality(&raid4_personality);
6129 module_init(raid5_init);
6130 module_exit(raid5_exit);
6131 MODULE_LICENSE("GPL");
6132 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6133 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6134 MODULE_ALIAS("md-raid5");
6135 MODULE_ALIAS("md-raid4");
6136 MODULE_ALIAS("md-level-5");
6137 MODULE_ALIAS("md-level-4");
6138 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6139 MODULE_ALIAS("md-raid6");
6140 MODULE_ALIAS("md-level-6");
6142 /* This used to be two separate modules, they were: */
6143 MODULE_ALIAS("raid5");
6144 MODULE_ALIAS("raid6");