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_processed_stripes(struct bio *bio)
104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
108 static inline int raid5_dec_bi_active_stripes(struct bio *bio)
110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
114 static inline void raid5_inc_bi_active_stripes(struct bio *bio)
116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
120 static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
132 static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
138 /* Find first data disk in a raid6 stripe */
139 static inline int raid6_d0(struct stripe_head *sh)
142 /* ddf always start from first device */
144 /* md starts just after Q block */
145 if (sh->qd_idx == sh->disks - 1)
148 return sh->qd_idx + 1;
150 static inline int raid6_next_disk(int disk, int raid_disks)
153 return (disk < raid_disks) ? disk : 0;
156 /* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
161 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
168 if (idx == sh->pd_idx)
169 return syndrome_disks;
170 if (idx == sh->qd_idx)
171 return syndrome_disks + 1;
177 static void return_io(struct bio *return_bi)
179 struct bio *bi = return_bi;
182 return_bi = bi->bi_next;
190 static void print_raid5_conf (struct r5conf *conf);
192 static int stripe_operations_active(struct stripe_head *sh)
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
199 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
215 md_wakeup_thread(conf->mddev->thread);
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
232 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
238 static void release_stripe(struct stripe_head *sh)
240 struct r5conf *conf = sh->raid_conf;
243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
248 local_irq_restore(flags);
251 static inline void remove_hash(struct stripe_head *sh)
253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
256 hlist_del_init(&sh->hash);
259 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
266 hlist_add_head(&sh->hash, hp);
270 /* find an idle stripe, make sure it is unhashed, and return it. */
271 static struct stripe_head *get_free_stripe(struct r5conf *conf)
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
276 if (list_empty(&conf->inactive_list))
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
282 atomic_inc(&conf->active_stripes);
287 static void shrink_buffers(struct stripe_head *sh)
291 int num = sh->raid_conf->pool_size;
293 for (i = 0; i < num ; i++) {
297 sh->dev[i].page = NULL;
302 static int grow_buffers(struct stripe_head *sh)
305 int num = sh->raid_conf->pool_size;
307 for (i = 0; i < num; i++) {
310 if (!(page = alloc_page(GFP_KERNEL))) {
313 sh->dev[i].page = page;
318 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
319 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
320 struct stripe_head *sh);
322 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
324 struct r5conf *conf = sh->raid_conf;
327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
329 BUG_ON(stripe_operations_active(sh));
331 pr_debug("init_stripe called, stripe %llu\n",
332 (unsigned long long)sh->sector);
336 sh->generation = conf->generation - previous;
337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
339 stripe_set_idx(sector, conf, previous, sh);
343 for (i = sh->disks; i--; ) {
344 struct r5dev *dev = &sh->dev[i];
346 if (dev->toread || dev->read || dev->towrite || dev->written ||
347 test_bit(R5_LOCKED, &dev->flags)) {
348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
349 (unsigned long long)sh->sector, i, dev->toread,
350 dev->read, dev->towrite, dev->written,
351 test_bit(R5_LOCKED, &dev->flags));
355 raid5_build_block(sh, i, previous);
357 insert_hash(conf, sh);
360 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
363 struct stripe_head *sh;
364 struct hlist_node *hn;
366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
368 if (sh->sector == sector && sh->generation == generation)
370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
375 * Need to check if array has failed when deciding whether to:
377 * - remove non-faulty devices
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
387 static int calc_degraded(struct r5conf *conf)
389 int degraded, degraded2;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
396 if (!rdev || test_bit(Faulty, &rdev->flags))
398 else if (test_bit(In_sync, &rdev->flags))
401 /* not in-sync or faulty.
402 * If the reshape increases the number of devices,
403 * this is being recovered by the reshape, so
404 * this 'previous' section is not in_sync.
405 * If the number of devices is being reduced however,
406 * the device can only be part of the array if
407 * we are reverting a reshape, so this section will
410 if (conf->raid_disks >= conf->previous_raid_disks)
414 if (conf->raid_disks == conf->previous_raid_disks)
418 for (i = 0; i < conf->raid_disks; i++) {
419 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
420 if (!rdev || test_bit(Faulty, &rdev->flags))
422 else if (test_bit(In_sync, &rdev->flags))
425 /* not in-sync or faulty.
426 * If reshape increases the number of devices, this
427 * section has already been recovered, else it
428 * almost certainly hasn't.
430 if (conf->raid_disks <= conf->previous_raid_disks)
434 if (degraded2 > degraded)
439 static int has_failed(struct r5conf *conf)
443 if (conf->mddev->reshape_position == MaxSector)
444 return conf->mddev->degraded > conf->max_degraded;
446 degraded = calc_degraded(conf);
447 if (degraded > conf->max_degraded)
452 static struct stripe_head *
453 get_active_stripe(struct r5conf *conf, sector_t sector,
454 int previous, int noblock, int noquiesce)
456 struct stripe_head *sh;
458 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
460 spin_lock_irq(&conf->device_lock);
463 wait_event_lock_irq(conf->wait_for_stripe,
464 conf->quiesce == 0 || noquiesce,
465 conf->device_lock, /* nothing */);
466 sh = __find_stripe(conf, sector, conf->generation - previous);
468 if (!conf->inactive_blocked)
469 sh = get_free_stripe(conf);
470 if (noblock && sh == NULL)
473 conf->inactive_blocked = 1;
474 wait_event_lock_irq(conf->wait_for_stripe,
475 !list_empty(&conf->inactive_list) &&
476 (atomic_read(&conf->active_stripes)
477 < (conf->max_nr_stripes *3/4)
478 || !conf->inactive_blocked),
481 conf->inactive_blocked = 0;
483 init_stripe(sh, sector, previous);
485 if (atomic_read(&sh->count)) {
486 BUG_ON(!list_empty(&sh->lru)
487 && !test_bit(STRIPE_EXPANDING, &sh->state)
488 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
490 if (!test_bit(STRIPE_HANDLE, &sh->state))
491 atomic_inc(&conf->active_stripes);
492 if (list_empty(&sh->lru) &&
493 !test_bit(STRIPE_EXPANDING, &sh->state))
495 list_del_init(&sh->lru);
498 } while (sh == NULL);
501 atomic_inc(&sh->count);
503 spin_unlock_irq(&conf->device_lock);
507 /* Determine if 'data_offset' or 'new_data_offset' should be used
508 * in this stripe_head.
510 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
512 sector_t progress = conf->reshape_progress;
513 /* Need a memory barrier to make sure we see the value
514 * of conf->generation, or ->data_offset that was set before
515 * reshape_progress was updated.
518 if (progress == MaxSector)
520 if (sh->generation == conf->generation - 1)
522 /* We are in a reshape, and this is a new-generation stripe,
523 * so use new_data_offset.
529 raid5_end_read_request(struct bio *bi, int error);
531 raid5_end_write_request(struct bio *bi, int error);
533 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
535 struct r5conf *conf = sh->raid_conf;
536 int i, disks = sh->disks;
540 for (i = disks; i--; ) {
542 int replace_only = 0;
543 struct bio *bi, *rbi;
544 struct md_rdev *rdev, *rrdev = NULL;
545 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
546 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
550 if (test_bit(R5_Discard, &sh->dev[i].flags))
552 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
554 else if (test_and_clear_bit(R5_WantReplace,
555 &sh->dev[i].flags)) {
560 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
563 bi = &sh->dev[i].req;
564 rbi = &sh->dev[i].rreq; /* For writing to replacement */
569 bi->bi_end_io = raid5_end_write_request;
570 rbi->bi_end_io = raid5_end_write_request;
572 bi->bi_end_io = raid5_end_read_request;
575 rrdev = rcu_dereference(conf->disks[i].replacement);
576 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
577 rdev = rcu_dereference(conf->disks[i].rdev);
586 /* We raced and saw duplicates */
589 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
594 if (rdev && test_bit(Faulty, &rdev->flags))
597 atomic_inc(&rdev->nr_pending);
598 if (rrdev && test_bit(Faulty, &rrdev->flags))
601 atomic_inc(&rrdev->nr_pending);
604 /* We have already checked bad blocks for reads. Now
605 * need to check for writes. We never accept write errors
606 * on the replacement, so we don't to check rrdev.
608 while ((rw & WRITE) && rdev &&
609 test_bit(WriteErrorSeen, &rdev->flags)) {
612 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
613 &first_bad, &bad_sectors);
618 set_bit(BlockedBadBlocks, &rdev->flags);
619 if (!conf->mddev->external &&
620 conf->mddev->flags) {
621 /* It is very unlikely, but we might
622 * still need to write out the
623 * bad block log - better give it
625 md_check_recovery(conf->mddev);
628 * Because md_wait_for_blocked_rdev
629 * will dec nr_pending, we must
630 * increment it first.
632 atomic_inc(&rdev->nr_pending);
633 md_wait_for_blocked_rdev(rdev, conf->mddev);
635 /* Acknowledged bad block - skip the write */
636 rdev_dec_pending(rdev, conf->mddev);
642 if (s->syncing || s->expanding || s->expanded
644 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
646 set_bit(STRIPE_IO_STARTED, &sh->state);
648 bi->bi_bdev = rdev->bdev;
649 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
650 __func__, (unsigned long long)sh->sector,
652 atomic_inc(&sh->count);
653 if (use_new_offset(conf, sh))
654 bi->bi_sector = (sh->sector
655 + rdev->new_data_offset);
657 bi->bi_sector = (sh->sector
658 + rdev->data_offset);
659 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
660 bi->bi_rw |= REQ_FLUSH;
662 bi->bi_flags = 1 << BIO_UPTODATE;
664 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
665 bi->bi_io_vec[0].bv_offset = 0;
666 bi->bi_size = STRIPE_SIZE;
669 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
670 generic_make_request(bi);
673 if (s->syncing || s->expanding || s->expanded
675 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
677 set_bit(STRIPE_IO_STARTED, &sh->state);
679 rbi->bi_bdev = rrdev->bdev;
680 pr_debug("%s: for %llu schedule op %ld on "
681 "replacement disc %d\n",
682 __func__, (unsigned long long)sh->sector,
684 atomic_inc(&sh->count);
685 if (use_new_offset(conf, sh))
686 rbi->bi_sector = (sh->sector
687 + rrdev->new_data_offset);
689 rbi->bi_sector = (sh->sector
690 + rrdev->data_offset);
691 rbi->bi_flags = 1 << BIO_UPTODATE;
693 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
694 rbi->bi_io_vec[0].bv_offset = 0;
695 rbi->bi_size = STRIPE_SIZE;
697 generic_make_request(rbi);
699 if (!rdev && !rrdev) {
701 set_bit(STRIPE_DEGRADED, &sh->state);
702 pr_debug("skip op %ld on disc %d for sector %llu\n",
703 bi->bi_rw, i, (unsigned long long)sh->sector);
704 clear_bit(R5_LOCKED, &sh->dev[i].flags);
705 set_bit(STRIPE_HANDLE, &sh->state);
710 static struct dma_async_tx_descriptor *
711 async_copy_data(int frombio, struct bio *bio, struct page *page,
712 sector_t sector, struct dma_async_tx_descriptor *tx)
715 struct page *bio_page;
718 struct async_submit_ctl submit;
719 enum async_tx_flags flags = 0;
721 if (bio->bi_sector >= sector)
722 page_offset = (signed)(bio->bi_sector - sector) * 512;
724 page_offset = (signed)(sector - bio->bi_sector) * -512;
727 flags |= ASYNC_TX_FENCE;
728 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
730 bio_for_each_segment(bvl, bio, i) {
731 int len = bvl->bv_len;
735 if (page_offset < 0) {
736 b_offset = -page_offset;
737 page_offset += b_offset;
741 if (len > 0 && page_offset + len > STRIPE_SIZE)
742 clen = STRIPE_SIZE - page_offset;
747 b_offset += bvl->bv_offset;
748 bio_page = bvl->bv_page;
750 tx = async_memcpy(page, bio_page, page_offset,
751 b_offset, clen, &submit);
753 tx = async_memcpy(bio_page, page, b_offset,
754 page_offset, clen, &submit);
756 /* chain the operations */
757 submit.depend_tx = tx;
759 if (clen < len) /* hit end of page */
767 static void ops_complete_biofill(void *stripe_head_ref)
769 struct stripe_head *sh = stripe_head_ref;
770 struct bio *return_bi = NULL;
773 pr_debug("%s: stripe %llu\n", __func__,
774 (unsigned long long)sh->sector);
776 /* clear completed biofills */
777 for (i = sh->disks; i--; ) {
778 struct r5dev *dev = &sh->dev[i];
780 /* acknowledge completion of a biofill operation */
781 /* and check if we need to reply to a read request,
782 * new R5_Wantfill requests are held off until
783 * !STRIPE_BIOFILL_RUN
785 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
786 struct bio *rbi, *rbi2;
791 while (rbi && rbi->bi_sector <
792 dev->sector + STRIPE_SECTORS) {
793 rbi2 = r5_next_bio(rbi, dev->sector);
794 if (!raid5_dec_bi_active_stripes(rbi)) {
795 rbi->bi_next = return_bi;
802 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
804 return_io(return_bi);
806 set_bit(STRIPE_HANDLE, &sh->state);
810 static void ops_run_biofill(struct stripe_head *sh)
812 struct dma_async_tx_descriptor *tx = NULL;
813 struct async_submit_ctl submit;
816 pr_debug("%s: stripe %llu\n", __func__,
817 (unsigned long long)sh->sector);
819 for (i = sh->disks; i--; ) {
820 struct r5dev *dev = &sh->dev[i];
821 if (test_bit(R5_Wantfill, &dev->flags)) {
823 spin_lock_irq(&sh->stripe_lock);
824 dev->read = rbi = dev->toread;
826 spin_unlock_irq(&sh->stripe_lock);
827 while (rbi && rbi->bi_sector <
828 dev->sector + STRIPE_SECTORS) {
829 tx = async_copy_data(0, rbi, dev->page,
831 rbi = r5_next_bio(rbi, dev->sector);
836 atomic_inc(&sh->count);
837 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
838 async_trigger_callback(&submit);
841 static void mark_target_uptodate(struct stripe_head *sh, int target)
848 tgt = &sh->dev[target];
849 set_bit(R5_UPTODATE, &tgt->flags);
850 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
851 clear_bit(R5_Wantcompute, &tgt->flags);
854 static void ops_complete_compute(void *stripe_head_ref)
856 struct stripe_head *sh = stripe_head_ref;
858 pr_debug("%s: stripe %llu\n", __func__,
859 (unsigned long long)sh->sector);
861 /* mark the computed target(s) as uptodate */
862 mark_target_uptodate(sh, sh->ops.target);
863 mark_target_uptodate(sh, sh->ops.target2);
865 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
866 if (sh->check_state == check_state_compute_run)
867 sh->check_state = check_state_compute_result;
868 set_bit(STRIPE_HANDLE, &sh->state);
872 /* return a pointer to the address conversion region of the scribble buffer */
873 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
874 struct raid5_percpu *percpu)
876 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
879 static struct dma_async_tx_descriptor *
880 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
882 int disks = sh->disks;
883 struct page **xor_srcs = percpu->scribble;
884 int target = sh->ops.target;
885 struct r5dev *tgt = &sh->dev[target];
886 struct page *xor_dest = tgt->page;
888 struct dma_async_tx_descriptor *tx;
889 struct async_submit_ctl submit;
892 pr_debug("%s: stripe %llu block: %d\n",
893 __func__, (unsigned long long)sh->sector, target);
894 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
896 for (i = disks; i--; )
898 xor_srcs[count++] = sh->dev[i].page;
900 atomic_inc(&sh->count);
902 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
903 ops_complete_compute, sh, to_addr_conv(sh, percpu));
904 if (unlikely(count == 1))
905 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
907 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
912 /* set_syndrome_sources - populate source buffers for gen_syndrome
913 * @srcs - (struct page *) array of size sh->disks
914 * @sh - stripe_head to parse
916 * Populates srcs in proper layout order for the stripe and returns the
917 * 'count' of sources to be used in a call to async_gen_syndrome. The P
918 * destination buffer is recorded in srcs[count] and the Q destination
919 * is recorded in srcs[count+1]].
921 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
923 int disks = sh->disks;
924 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
925 int d0_idx = raid6_d0(sh);
929 for (i = 0; i < disks; i++)
935 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
937 srcs[slot] = sh->dev[i].page;
938 i = raid6_next_disk(i, disks);
939 } while (i != d0_idx);
941 return syndrome_disks;
944 static struct dma_async_tx_descriptor *
945 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
947 int disks = sh->disks;
948 struct page **blocks = percpu->scribble;
950 int qd_idx = sh->qd_idx;
951 struct dma_async_tx_descriptor *tx;
952 struct async_submit_ctl submit;
958 if (sh->ops.target < 0)
959 target = sh->ops.target2;
960 else if (sh->ops.target2 < 0)
961 target = sh->ops.target;
963 /* we should only have one valid target */
966 pr_debug("%s: stripe %llu block: %d\n",
967 __func__, (unsigned long long)sh->sector, target);
969 tgt = &sh->dev[target];
970 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
973 atomic_inc(&sh->count);
975 if (target == qd_idx) {
976 count = set_syndrome_sources(blocks, sh);
977 blocks[count] = NULL; /* regenerating p is not necessary */
978 BUG_ON(blocks[count+1] != dest); /* q should already be set */
979 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
980 ops_complete_compute, sh,
981 to_addr_conv(sh, percpu));
982 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
984 /* Compute any data- or p-drive using XOR */
986 for (i = disks; i-- ; ) {
987 if (i == target || i == qd_idx)
989 blocks[count++] = sh->dev[i].page;
992 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
993 NULL, ops_complete_compute, sh,
994 to_addr_conv(sh, percpu));
995 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1001 static struct dma_async_tx_descriptor *
1002 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1004 int i, count, disks = sh->disks;
1005 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1006 int d0_idx = raid6_d0(sh);
1007 int faila = -1, failb = -1;
1008 int target = sh->ops.target;
1009 int target2 = sh->ops.target2;
1010 struct r5dev *tgt = &sh->dev[target];
1011 struct r5dev *tgt2 = &sh->dev[target2];
1012 struct dma_async_tx_descriptor *tx;
1013 struct page **blocks = percpu->scribble;
1014 struct async_submit_ctl submit;
1016 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1017 __func__, (unsigned long long)sh->sector, target, target2);
1018 BUG_ON(target < 0 || target2 < 0);
1019 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1020 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1022 /* we need to open-code set_syndrome_sources to handle the
1023 * slot number conversion for 'faila' and 'failb'
1025 for (i = 0; i < disks ; i++)
1030 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1032 blocks[slot] = sh->dev[i].page;
1038 i = raid6_next_disk(i, disks);
1039 } while (i != d0_idx);
1041 BUG_ON(faila == failb);
1044 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1045 __func__, (unsigned long long)sh->sector, faila, failb);
1047 atomic_inc(&sh->count);
1049 if (failb == syndrome_disks+1) {
1050 /* Q disk is one of the missing disks */
1051 if (faila == syndrome_disks) {
1052 /* Missing P+Q, just recompute */
1053 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1054 ops_complete_compute, sh,
1055 to_addr_conv(sh, percpu));
1056 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
1057 STRIPE_SIZE, &submit);
1061 int qd_idx = sh->qd_idx;
1063 /* Missing D+Q: recompute D from P, then recompute Q */
1064 if (target == qd_idx)
1065 data_target = target2;
1067 data_target = target;
1070 for (i = disks; i-- ; ) {
1071 if (i == data_target || i == qd_idx)
1073 blocks[count++] = sh->dev[i].page;
1075 dest = sh->dev[data_target].page;
1076 init_async_submit(&submit,
1077 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1079 to_addr_conv(sh, percpu));
1080 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1083 count = set_syndrome_sources(blocks, sh);
1084 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1085 ops_complete_compute, sh,
1086 to_addr_conv(sh, percpu));
1087 return async_gen_syndrome(blocks, 0, count+2,
1088 STRIPE_SIZE, &submit);
1091 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1092 ops_complete_compute, sh,
1093 to_addr_conv(sh, percpu));
1094 if (failb == syndrome_disks) {
1095 /* We're missing D+P. */
1096 return async_raid6_datap_recov(syndrome_disks+2,
1100 /* We're missing D+D. */
1101 return async_raid6_2data_recov(syndrome_disks+2,
1102 STRIPE_SIZE, faila, failb,
1109 static void ops_complete_prexor(void *stripe_head_ref)
1111 struct stripe_head *sh = stripe_head_ref;
1113 pr_debug("%s: stripe %llu\n", __func__,
1114 (unsigned long long)sh->sector);
1117 static struct dma_async_tx_descriptor *
1118 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1119 struct dma_async_tx_descriptor *tx)
1121 int disks = sh->disks;
1122 struct page **xor_srcs = percpu->scribble;
1123 int count = 0, pd_idx = sh->pd_idx, i;
1124 struct async_submit_ctl submit;
1126 /* existing parity data subtracted */
1127 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
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];
1134 /* Only process blocks that are known to be uptodate */
1135 if (test_bit(R5_Wantdrain, &dev->flags))
1136 xor_srcs[count++] = dev->page;
1139 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1140 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
1141 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1146 static struct dma_async_tx_descriptor *
1147 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1149 int disks = sh->disks;
1152 pr_debug("%s: stripe %llu\n", __func__,
1153 (unsigned long long)sh->sector);
1155 for (i = disks; i--; ) {
1156 struct r5dev *dev = &sh->dev[i];
1159 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
1162 spin_lock_irq(&sh->stripe_lock);
1163 chosen = dev->towrite;
1164 dev->towrite = NULL;
1165 BUG_ON(dev->written);
1166 wbi = dev->written = chosen;
1167 spin_unlock_irq(&sh->stripe_lock);
1169 while (wbi && wbi->bi_sector <
1170 dev->sector + STRIPE_SECTORS) {
1171 if (wbi->bi_rw & REQ_FUA)
1172 set_bit(R5_WantFUA, &dev->flags);
1173 if (wbi->bi_rw & REQ_SYNC)
1174 set_bit(R5_SyncIO, &dev->flags);
1175 if (wbi->bi_rw & REQ_DISCARD)
1176 set_bit(R5_Discard, &dev->flags);
1178 tx = async_copy_data(1, wbi, dev->page,
1180 wbi = r5_next_bio(wbi, dev->sector);
1188 static void ops_complete_reconstruct(void *stripe_head_ref)
1190 struct stripe_head *sh = stripe_head_ref;
1191 int disks = sh->disks;
1192 int pd_idx = sh->pd_idx;
1193 int qd_idx = sh->qd_idx;
1195 bool fua = false, sync = false, discard = false;
1197 pr_debug("%s: stripe %llu\n", __func__,
1198 (unsigned long long)sh->sector);
1200 for (i = disks; i--; ) {
1201 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1202 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1203 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1206 for (i = disks; i--; ) {
1207 struct r5dev *dev = &sh->dev[i];
1209 if (dev->written || i == pd_idx || i == qd_idx) {
1211 set_bit(R5_UPTODATE, &dev->flags);
1213 set_bit(R5_WantFUA, &dev->flags);
1215 set_bit(R5_SyncIO, &dev->flags);
1219 if (sh->reconstruct_state == reconstruct_state_drain_run)
1220 sh->reconstruct_state = reconstruct_state_drain_result;
1221 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1222 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1224 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1225 sh->reconstruct_state = reconstruct_state_result;
1228 set_bit(STRIPE_HANDLE, &sh->state);
1233 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1234 struct dma_async_tx_descriptor *tx)
1236 int disks = sh->disks;
1237 struct page **xor_srcs = percpu->scribble;
1238 struct async_submit_ctl submit;
1239 int count = 0, pd_idx = sh->pd_idx, i;
1240 struct page *xor_dest;
1242 unsigned long flags;
1244 pr_debug("%s: stripe %llu\n", __func__,
1245 (unsigned long long)sh->sector);
1247 for (i = 0; i < sh->disks; i++) {
1250 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1253 if (i >= sh->disks) {
1254 atomic_inc(&sh->count);
1255 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1256 ops_complete_reconstruct(sh);
1259 /* check if prexor is active which means only process blocks
1260 * that are part of a read-modify-write (written)
1262 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1264 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1265 for (i = disks; i--; ) {
1266 struct r5dev *dev = &sh->dev[i];
1268 xor_srcs[count++] = dev->page;
1271 xor_dest = sh->dev[pd_idx].page;
1272 for (i = disks; i--; ) {
1273 struct r5dev *dev = &sh->dev[i];
1275 xor_srcs[count++] = dev->page;
1279 /* 1/ if we prexor'd then the dest is reused as a source
1280 * 2/ if we did not prexor then we are redoing the parity
1281 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1282 * for the synchronous xor case
1284 flags = ASYNC_TX_ACK |
1285 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1287 atomic_inc(&sh->count);
1289 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1290 to_addr_conv(sh, percpu));
1291 if (unlikely(count == 1))
1292 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1294 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1298 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1299 struct dma_async_tx_descriptor *tx)
1301 struct async_submit_ctl submit;
1302 struct page **blocks = percpu->scribble;
1305 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1307 for (i = 0; i < sh->disks; i++) {
1308 if (sh->pd_idx == i || sh->qd_idx == i)
1310 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1313 if (i >= sh->disks) {
1314 atomic_inc(&sh->count);
1315 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1316 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1317 ops_complete_reconstruct(sh);
1321 count = set_syndrome_sources(blocks, sh);
1323 atomic_inc(&sh->count);
1325 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1326 sh, to_addr_conv(sh, percpu));
1327 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1330 static void ops_complete_check(void *stripe_head_ref)
1332 struct stripe_head *sh = stripe_head_ref;
1334 pr_debug("%s: stripe %llu\n", __func__,
1335 (unsigned long long)sh->sector);
1337 sh->check_state = check_state_check_result;
1338 set_bit(STRIPE_HANDLE, &sh->state);
1342 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1344 int disks = sh->disks;
1345 int pd_idx = sh->pd_idx;
1346 int qd_idx = sh->qd_idx;
1347 struct page *xor_dest;
1348 struct page **xor_srcs = percpu->scribble;
1349 struct dma_async_tx_descriptor *tx;
1350 struct async_submit_ctl submit;
1354 pr_debug("%s: stripe %llu\n", __func__,
1355 (unsigned long long)sh->sector);
1358 xor_dest = sh->dev[pd_idx].page;
1359 xor_srcs[count++] = xor_dest;
1360 for (i = disks; i--; ) {
1361 if (i == pd_idx || i == qd_idx)
1363 xor_srcs[count++] = sh->dev[i].page;
1366 init_async_submit(&submit, 0, NULL, NULL, NULL,
1367 to_addr_conv(sh, percpu));
1368 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1369 &sh->ops.zero_sum_result, &submit);
1371 atomic_inc(&sh->count);
1372 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1373 tx = async_trigger_callback(&submit);
1376 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1378 struct page **srcs = percpu->scribble;
1379 struct async_submit_ctl submit;
1382 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1383 (unsigned long long)sh->sector, checkp);
1385 count = set_syndrome_sources(srcs, sh);
1389 atomic_inc(&sh->count);
1390 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1391 sh, to_addr_conv(sh, percpu));
1392 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1393 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1396 static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1398 int overlap_clear = 0, i, disks = sh->disks;
1399 struct dma_async_tx_descriptor *tx = NULL;
1400 struct r5conf *conf = sh->raid_conf;
1401 int level = conf->level;
1402 struct raid5_percpu *percpu;
1406 percpu = per_cpu_ptr(conf->percpu, cpu);
1407 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1408 ops_run_biofill(sh);
1412 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1414 tx = ops_run_compute5(sh, percpu);
1416 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1417 tx = ops_run_compute6_1(sh, percpu);
1419 tx = ops_run_compute6_2(sh, percpu);
1421 /* terminate the chain if reconstruct is not set to be run */
1422 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1426 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1427 tx = ops_run_prexor(sh, percpu, tx);
1429 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1430 tx = ops_run_biodrain(sh, tx);
1434 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1436 ops_run_reconstruct5(sh, percpu, tx);
1438 ops_run_reconstruct6(sh, percpu, tx);
1441 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1442 if (sh->check_state == check_state_run)
1443 ops_run_check_p(sh, percpu);
1444 else if (sh->check_state == check_state_run_q)
1445 ops_run_check_pq(sh, percpu, 0);
1446 else if (sh->check_state == check_state_run_pq)
1447 ops_run_check_pq(sh, percpu, 1);
1453 for (i = disks; i--; ) {
1454 struct r5dev *dev = &sh->dev[i];
1455 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1456 wake_up(&sh->raid_conf->wait_for_overlap);
1461 #ifdef CONFIG_MULTICORE_RAID456
1462 static void async_run_ops(void *param, async_cookie_t cookie)
1464 struct stripe_head *sh = param;
1465 unsigned long ops_request = sh->ops.request;
1467 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1468 wake_up(&sh->ops.wait_for_ops);
1470 __raid_run_ops(sh, ops_request);
1474 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1476 /* since handle_stripe can be called outside of raid5d context
1477 * we need to ensure sh->ops.request is de-staged before another
1480 wait_event(sh->ops.wait_for_ops,
1481 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1482 sh->ops.request = ops_request;
1484 atomic_inc(&sh->count);
1485 async_schedule(async_run_ops, sh);
1488 #define raid_run_ops __raid_run_ops
1491 static int grow_one_stripe(struct r5conf *conf)
1493 struct stripe_head *sh;
1494 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
1498 sh->raid_conf = conf;
1499 #ifdef CONFIG_MULTICORE_RAID456
1500 init_waitqueue_head(&sh->ops.wait_for_ops);
1503 spin_lock_init(&sh->stripe_lock);
1505 if (grow_buffers(sh)) {
1507 kmem_cache_free(conf->slab_cache, sh);
1510 /* we just created an active stripe so... */
1511 atomic_set(&sh->count, 1);
1512 atomic_inc(&conf->active_stripes);
1513 INIT_LIST_HEAD(&sh->lru);
1518 static int grow_stripes(struct r5conf *conf, int num)
1520 struct kmem_cache *sc;
1521 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1523 if (conf->mddev->gendisk)
1524 sprintf(conf->cache_name[0],
1525 "raid%d-%s", conf->level, mdname(conf->mddev));
1527 sprintf(conf->cache_name[0],
1528 "raid%d-%p", conf->level, conf->mddev);
1529 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1531 conf->active_name = 0;
1532 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1533 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1537 conf->slab_cache = sc;
1538 conf->pool_size = devs;
1540 if (!grow_one_stripe(conf))
1546 * scribble_len - return the required size of the scribble region
1547 * @num - total number of disks in the array
1549 * The size must be enough to contain:
1550 * 1/ a struct page pointer for each device in the array +2
1551 * 2/ room to convert each entry in (1) to its corresponding dma
1552 * (dma_map_page()) or page (page_address()) address.
1554 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1555 * calculate over all devices (not just the data blocks), using zeros in place
1556 * of the P and Q blocks.
1558 static size_t scribble_len(int num)
1562 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1567 static int resize_stripes(struct r5conf *conf, int newsize)
1569 /* Make all the stripes able to hold 'newsize' devices.
1570 * New slots in each stripe get 'page' set to a new page.
1572 * This happens in stages:
1573 * 1/ create a new kmem_cache and allocate the required number of
1575 * 2/ gather all the old stripe_heads and tranfer the pages across
1576 * to the new stripe_heads. This will have the side effect of
1577 * freezing the array as once all stripe_heads have been collected,
1578 * no IO will be possible. Old stripe heads are freed once their
1579 * pages have been transferred over, and the old kmem_cache is
1580 * freed when all stripes are done.
1581 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1582 * we simple return a failre status - no need to clean anything up.
1583 * 4/ allocate new pages for the new slots in the new stripe_heads.
1584 * If this fails, we don't bother trying the shrink the
1585 * stripe_heads down again, we just leave them as they are.
1586 * As each stripe_head is processed the new one is released into
1589 * Once step2 is started, we cannot afford to wait for a write,
1590 * so we use GFP_NOIO allocations.
1592 struct stripe_head *osh, *nsh;
1593 LIST_HEAD(newstripes);
1594 struct disk_info *ndisks;
1597 struct kmem_cache *sc;
1600 if (newsize <= conf->pool_size)
1601 return 0; /* never bother to shrink */
1603 err = md_allow_write(conf->mddev);
1608 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1609 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1614 for (i = conf->max_nr_stripes; i; i--) {
1615 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
1619 nsh->raid_conf = conf;
1620 #ifdef CONFIG_MULTICORE_RAID456
1621 init_waitqueue_head(&nsh->ops.wait_for_ops);
1624 list_add(&nsh->lru, &newstripes);
1627 /* didn't get enough, give up */
1628 while (!list_empty(&newstripes)) {
1629 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1630 list_del(&nsh->lru);
1631 kmem_cache_free(sc, nsh);
1633 kmem_cache_destroy(sc);
1636 /* Step 2 - Must use GFP_NOIO now.
1637 * OK, we have enough stripes, start collecting inactive
1638 * stripes and copying them over
1640 list_for_each_entry(nsh, &newstripes, lru) {
1641 spin_lock_irq(&conf->device_lock);
1642 wait_event_lock_irq(conf->wait_for_stripe,
1643 !list_empty(&conf->inactive_list),
1646 osh = get_free_stripe(conf);
1647 spin_unlock_irq(&conf->device_lock);
1648 atomic_set(&nsh->count, 1);
1649 for(i=0; i<conf->pool_size; i++)
1650 nsh->dev[i].page = osh->dev[i].page;
1651 for( ; i<newsize; i++)
1652 nsh->dev[i].page = NULL;
1653 kmem_cache_free(conf->slab_cache, osh);
1655 kmem_cache_destroy(conf->slab_cache);
1658 * At this point, we are holding all the stripes so the array
1659 * is completely stalled, so now is a good time to resize
1660 * conf->disks and the scribble region
1662 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1664 for (i=0; i<conf->raid_disks; i++)
1665 ndisks[i] = conf->disks[i];
1667 conf->disks = ndisks;
1672 conf->scribble_len = scribble_len(newsize);
1673 for_each_present_cpu(cpu) {
1674 struct raid5_percpu *percpu;
1677 percpu = per_cpu_ptr(conf->percpu, cpu);
1678 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1681 kfree(percpu->scribble);
1682 percpu->scribble = scribble;
1690 /* Step 4, return new stripes to service */
1691 while(!list_empty(&newstripes)) {
1692 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1693 list_del_init(&nsh->lru);
1695 for (i=conf->raid_disks; i < newsize; i++)
1696 if (nsh->dev[i].page == NULL) {
1697 struct page *p = alloc_page(GFP_NOIO);
1698 nsh->dev[i].page = p;
1702 release_stripe(nsh);
1704 /* critical section pass, GFP_NOIO no longer needed */
1706 conf->slab_cache = sc;
1707 conf->active_name = 1-conf->active_name;
1708 conf->pool_size = newsize;
1712 static int drop_one_stripe(struct r5conf *conf)
1714 struct stripe_head *sh;
1716 spin_lock_irq(&conf->device_lock);
1717 sh = get_free_stripe(conf);
1718 spin_unlock_irq(&conf->device_lock);
1721 BUG_ON(atomic_read(&sh->count));
1723 kmem_cache_free(conf->slab_cache, sh);
1724 atomic_dec(&conf->active_stripes);
1728 static void shrink_stripes(struct r5conf *conf)
1730 while (drop_one_stripe(conf))
1733 if (conf->slab_cache)
1734 kmem_cache_destroy(conf->slab_cache);
1735 conf->slab_cache = NULL;
1738 static void raid5_end_read_request(struct bio * bi, int error)
1740 struct stripe_head *sh = bi->bi_private;
1741 struct r5conf *conf = sh->raid_conf;
1742 int disks = sh->disks, i;
1743 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1744 char b[BDEVNAME_SIZE];
1745 struct md_rdev *rdev = NULL;
1748 for (i=0 ; i<disks; i++)
1749 if (bi == &sh->dev[i].req)
1752 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1753 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1759 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1760 /* If replacement finished while this request was outstanding,
1761 * 'replacement' might be NULL already.
1762 * In that case it moved down to 'rdev'.
1763 * rdev is not removed until all requests are finished.
1765 rdev = conf->disks[i].replacement;
1767 rdev = conf->disks[i].rdev;
1769 if (use_new_offset(conf, sh))
1770 s = sh->sector + rdev->new_data_offset;
1772 s = sh->sector + rdev->data_offset;
1774 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1775 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1776 /* Note that this cannot happen on a
1777 * replacement device. We just fail those on
1782 "md/raid:%s: read error corrected"
1783 " (%lu sectors at %llu on %s)\n",
1784 mdname(conf->mddev), STRIPE_SECTORS,
1785 (unsigned long long)s,
1786 bdevname(rdev->bdev, b));
1787 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1788 clear_bit(R5_ReadError, &sh->dev[i].flags);
1789 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1790 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1791 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1793 if (atomic_read(&rdev->read_errors))
1794 atomic_set(&rdev->read_errors, 0);
1796 const char *bdn = bdevname(rdev->bdev, b);
1800 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1801 atomic_inc(&rdev->read_errors);
1802 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1805 "md/raid:%s: read error on replacement device "
1806 "(sector %llu on %s).\n",
1807 mdname(conf->mddev),
1808 (unsigned long long)s,
1810 else if (conf->mddev->degraded >= conf->max_degraded) {
1814 "md/raid:%s: read error not correctable "
1815 "(sector %llu on %s).\n",
1816 mdname(conf->mddev),
1817 (unsigned long long)s,
1819 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
1824 "md/raid:%s: read error NOT corrected!! "
1825 "(sector %llu on %s).\n",
1826 mdname(conf->mddev),
1827 (unsigned long long)s,
1829 } else if (atomic_read(&rdev->read_errors)
1830 > conf->max_nr_stripes)
1832 "md/raid:%s: Too many read errors, failing device %s.\n",
1833 mdname(conf->mddev), bdn);
1837 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1838 set_bit(R5_ReadError, &sh->dev[i].flags);
1839 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1841 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1843 clear_bit(R5_ReadError, &sh->dev[i].flags);
1844 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1846 && test_bit(In_sync, &rdev->flags)
1847 && rdev_set_badblocks(
1848 rdev, sh->sector, STRIPE_SECTORS, 0)))
1849 md_error(conf->mddev, rdev);
1852 rdev_dec_pending(rdev, conf->mddev);
1853 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1854 set_bit(STRIPE_HANDLE, &sh->state);
1858 static void raid5_end_write_request(struct bio *bi, int error)
1860 struct stripe_head *sh = bi->bi_private;
1861 struct r5conf *conf = sh->raid_conf;
1862 int disks = sh->disks, i;
1863 struct md_rdev *uninitialized_var(rdev);
1864 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1867 int replacement = 0;
1869 for (i = 0 ; i < disks; i++) {
1870 if (bi == &sh->dev[i].req) {
1871 rdev = conf->disks[i].rdev;
1874 if (bi == &sh->dev[i].rreq) {
1875 rdev = conf->disks[i].replacement;
1879 /* rdev was removed and 'replacement'
1880 * replaced it. rdev is not removed
1881 * until all requests are finished.
1883 rdev = conf->disks[i].rdev;
1887 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1888 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1897 md_error(conf->mddev, rdev);
1898 else if (is_badblock(rdev, sh->sector,
1900 &first_bad, &bad_sectors))
1901 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1904 set_bit(WriteErrorSeen, &rdev->flags);
1905 set_bit(R5_WriteError, &sh->dev[i].flags);
1906 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1907 set_bit(MD_RECOVERY_NEEDED,
1908 &rdev->mddev->recovery);
1909 } else if (is_badblock(rdev, sh->sector,
1911 &first_bad, &bad_sectors))
1912 set_bit(R5_MadeGood, &sh->dev[i].flags);
1914 rdev_dec_pending(rdev, conf->mddev);
1916 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1917 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1918 set_bit(STRIPE_HANDLE, &sh->state);
1922 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1924 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1926 struct r5dev *dev = &sh->dev[i];
1928 bio_init(&dev->req);
1929 dev->req.bi_io_vec = &dev->vec;
1931 dev->req.bi_max_vecs++;
1932 dev->req.bi_private = sh;
1933 dev->vec.bv_page = dev->page;
1935 bio_init(&dev->rreq);
1936 dev->rreq.bi_io_vec = &dev->rvec;
1937 dev->rreq.bi_vcnt++;
1938 dev->rreq.bi_max_vecs++;
1939 dev->rreq.bi_private = sh;
1940 dev->rvec.bv_page = dev->page;
1943 dev->sector = compute_blocknr(sh, i, previous);
1946 static void error(struct mddev *mddev, struct md_rdev *rdev)
1948 char b[BDEVNAME_SIZE];
1949 struct r5conf *conf = mddev->private;
1950 unsigned long flags;
1951 pr_debug("raid456: error called\n");
1953 spin_lock_irqsave(&conf->device_lock, flags);
1954 clear_bit(In_sync, &rdev->flags);
1955 mddev->degraded = calc_degraded(conf);
1956 spin_unlock_irqrestore(&conf->device_lock, flags);
1957 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1959 set_bit(Blocked, &rdev->flags);
1960 set_bit(Faulty, &rdev->flags);
1961 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1963 "md/raid:%s: Disk failure on %s, disabling device.\n"
1964 "md/raid:%s: Operation continuing on %d devices.\n",
1966 bdevname(rdev->bdev, b),
1968 conf->raid_disks - mddev->degraded);
1972 * Input: a 'big' sector number,
1973 * Output: index of the data and parity disk, and the sector # in them.
1975 static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
1976 int previous, int *dd_idx,
1977 struct stripe_head *sh)
1979 sector_t stripe, stripe2;
1980 sector_t chunk_number;
1981 unsigned int chunk_offset;
1984 sector_t new_sector;
1985 int algorithm = previous ? conf->prev_algo
1987 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1988 : conf->chunk_sectors;
1989 int raid_disks = previous ? conf->previous_raid_disks
1991 int data_disks = raid_disks - conf->max_degraded;
1993 /* First compute the information on this sector */
1996 * Compute the chunk number and the sector offset inside the chunk
1998 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1999 chunk_number = r_sector;
2002 * Compute the stripe number
2004 stripe = chunk_number;
2005 *dd_idx = sector_div(stripe, data_disks);
2008 * Select the parity disk based on the user selected algorithm.
2010 pd_idx = qd_idx = -1;
2011 switch(conf->level) {
2013 pd_idx = data_disks;
2016 switch (algorithm) {
2017 case ALGORITHM_LEFT_ASYMMETRIC:
2018 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2019 if (*dd_idx >= pd_idx)
2022 case ALGORITHM_RIGHT_ASYMMETRIC:
2023 pd_idx = sector_div(stripe2, raid_disks);
2024 if (*dd_idx >= pd_idx)
2027 case ALGORITHM_LEFT_SYMMETRIC:
2028 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2029 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2031 case ALGORITHM_RIGHT_SYMMETRIC:
2032 pd_idx = sector_div(stripe2, raid_disks);
2033 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2035 case ALGORITHM_PARITY_0:
2039 case ALGORITHM_PARITY_N:
2040 pd_idx = data_disks;
2048 switch (algorithm) {
2049 case ALGORITHM_LEFT_ASYMMETRIC:
2050 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2051 qd_idx = pd_idx + 1;
2052 if (pd_idx == raid_disks-1) {
2053 (*dd_idx)++; /* Q D D D P */
2055 } else if (*dd_idx >= pd_idx)
2056 (*dd_idx) += 2; /* D D P Q D */
2058 case ALGORITHM_RIGHT_ASYMMETRIC:
2059 pd_idx = sector_div(stripe2, raid_disks);
2060 qd_idx = pd_idx + 1;
2061 if (pd_idx == raid_disks-1) {
2062 (*dd_idx)++; /* Q D D D P */
2064 } else if (*dd_idx >= pd_idx)
2065 (*dd_idx) += 2; /* D D P Q D */
2067 case ALGORITHM_LEFT_SYMMETRIC:
2068 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2069 qd_idx = (pd_idx + 1) % raid_disks;
2070 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2072 case ALGORITHM_RIGHT_SYMMETRIC:
2073 pd_idx = sector_div(stripe2, raid_disks);
2074 qd_idx = (pd_idx + 1) % raid_disks;
2075 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2078 case ALGORITHM_PARITY_0:
2083 case ALGORITHM_PARITY_N:
2084 pd_idx = data_disks;
2085 qd_idx = data_disks + 1;
2088 case ALGORITHM_ROTATING_ZERO_RESTART:
2089 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2090 * of blocks for computing Q is different.
2092 pd_idx = sector_div(stripe2, raid_disks);
2093 qd_idx = pd_idx + 1;
2094 if (pd_idx == raid_disks-1) {
2095 (*dd_idx)++; /* Q D D D P */
2097 } else if (*dd_idx >= pd_idx)
2098 (*dd_idx) += 2; /* D D P Q D */
2102 case ALGORITHM_ROTATING_N_RESTART:
2103 /* Same a left_asymmetric, by first stripe is
2104 * D D D P Q rather than
2108 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2109 qd_idx = pd_idx + 1;
2110 if (pd_idx == raid_disks-1) {
2111 (*dd_idx)++; /* Q D D D P */
2113 } else if (*dd_idx >= pd_idx)
2114 (*dd_idx) += 2; /* D D P Q D */
2118 case ALGORITHM_ROTATING_N_CONTINUE:
2119 /* Same as left_symmetric but Q is before P */
2120 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2121 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2122 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2126 case ALGORITHM_LEFT_ASYMMETRIC_6:
2127 /* RAID5 left_asymmetric, with Q on last device */
2128 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2129 if (*dd_idx >= pd_idx)
2131 qd_idx = raid_disks - 1;
2134 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2135 pd_idx = sector_div(stripe2, raid_disks-1);
2136 if (*dd_idx >= pd_idx)
2138 qd_idx = raid_disks - 1;
2141 case ALGORITHM_LEFT_SYMMETRIC_6:
2142 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2143 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2144 qd_idx = raid_disks - 1;
2147 case ALGORITHM_RIGHT_SYMMETRIC_6:
2148 pd_idx = sector_div(stripe2, raid_disks-1);
2149 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2150 qd_idx = raid_disks - 1;
2153 case ALGORITHM_PARITY_0_6:
2156 qd_idx = raid_disks - 1;
2166 sh->pd_idx = pd_idx;
2167 sh->qd_idx = qd_idx;
2168 sh->ddf_layout = ddf_layout;
2171 * Finally, compute the new sector number
2173 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2178 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
2180 struct r5conf *conf = sh->raid_conf;
2181 int raid_disks = sh->disks;
2182 int data_disks = raid_disks - conf->max_degraded;
2183 sector_t new_sector = sh->sector, check;
2184 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2185 : conf->chunk_sectors;
2186 int algorithm = previous ? conf->prev_algo
2190 sector_t chunk_number;
2191 int dummy1, dd_idx = i;
2193 struct stripe_head sh2;
2196 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2197 stripe = new_sector;
2199 if (i == sh->pd_idx)
2201 switch(conf->level) {
2204 switch (algorithm) {
2205 case ALGORITHM_LEFT_ASYMMETRIC:
2206 case ALGORITHM_RIGHT_ASYMMETRIC:
2210 case ALGORITHM_LEFT_SYMMETRIC:
2211 case ALGORITHM_RIGHT_SYMMETRIC:
2214 i -= (sh->pd_idx + 1);
2216 case ALGORITHM_PARITY_0:
2219 case ALGORITHM_PARITY_N:
2226 if (i == sh->qd_idx)
2227 return 0; /* It is the Q disk */
2228 switch (algorithm) {
2229 case ALGORITHM_LEFT_ASYMMETRIC:
2230 case ALGORITHM_RIGHT_ASYMMETRIC:
2231 case ALGORITHM_ROTATING_ZERO_RESTART:
2232 case ALGORITHM_ROTATING_N_RESTART:
2233 if (sh->pd_idx == raid_disks-1)
2234 i--; /* Q D D D P */
2235 else if (i > sh->pd_idx)
2236 i -= 2; /* D D P Q D */
2238 case ALGORITHM_LEFT_SYMMETRIC:
2239 case ALGORITHM_RIGHT_SYMMETRIC:
2240 if (sh->pd_idx == raid_disks-1)
2241 i--; /* Q D D D P */
2246 i -= (sh->pd_idx + 2);
2249 case ALGORITHM_PARITY_0:
2252 case ALGORITHM_PARITY_N:
2254 case ALGORITHM_ROTATING_N_CONTINUE:
2255 /* Like left_symmetric, but P is before Q */
2256 if (sh->pd_idx == 0)
2257 i--; /* P D D D Q */
2262 i -= (sh->pd_idx + 1);
2265 case ALGORITHM_LEFT_ASYMMETRIC_6:
2266 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2270 case ALGORITHM_LEFT_SYMMETRIC_6:
2271 case ALGORITHM_RIGHT_SYMMETRIC_6:
2273 i += data_disks + 1;
2274 i -= (sh->pd_idx + 1);
2276 case ALGORITHM_PARITY_0_6:
2285 chunk_number = stripe * data_disks + i;
2286 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
2288 check = raid5_compute_sector(conf, r_sector,
2289 previous, &dummy1, &sh2);
2290 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2291 || sh2.qd_idx != sh->qd_idx) {
2292 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2293 mdname(conf->mddev));
2301 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
2302 int rcw, int expand)
2304 int i, pd_idx = sh->pd_idx, disks = sh->disks;
2305 struct r5conf *conf = sh->raid_conf;
2306 int level = conf->level;
2309 /* if we are not expanding this is a proper write request, and
2310 * there will be bios with new data to be drained into the
2314 sh->reconstruct_state = reconstruct_state_drain_run;
2315 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2317 sh->reconstruct_state = reconstruct_state_run;
2319 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2321 for (i = disks; i--; ) {
2322 struct r5dev *dev = &sh->dev[i];
2325 set_bit(R5_LOCKED, &dev->flags);
2326 set_bit(R5_Wantdrain, &dev->flags);
2328 clear_bit(R5_UPTODATE, &dev->flags);
2332 if (s->locked + conf->max_degraded == disks)
2333 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2334 atomic_inc(&conf->pending_full_writes);
2337 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2338 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2340 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2341 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2342 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2343 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2345 for (i = disks; i--; ) {
2346 struct r5dev *dev = &sh->dev[i];
2351 (test_bit(R5_UPTODATE, &dev->flags) ||
2352 test_bit(R5_Wantcompute, &dev->flags))) {
2353 set_bit(R5_Wantdrain, &dev->flags);
2354 set_bit(R5_LOCKED, &dev->flags);
2355 clear_bit(R5_UPTODATE, &dev->flags);
2361 /* keep the parity disk(s) locked while asynchronous operations
2364 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2365 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2369 int qd_idx = sh->qd_idx;
2370 struct r5dev *dev = &sh->dev[qd_idx];
2372 set_bit(R5_LOCKED, &dev->flags);
2373 clear_bit(R5_UPTODATE, &dev->flags);
2377 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2378 __func__, (unsigned long long)sh->sector,
2379 s->locked, s->ops_request);
2383 * Each stripe/dev can have one or more bion attached.
2384 * toread/towrite point to the first in a chain.
2385 * The bi_next chain must be in order.
2387 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2390 struct r5conf *conf = sh->raid_conf;
2393 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2394 (unsigned long long)bi->bi_sector,
2395 (unsigned long long)sh->sector);
2398 * If several bio share a stripe. The bio bi_phys_segments acts as a
2399 * reference count to avoid race. The reference count should already be
2400 * increased before this function is called (for example, in
2401 * make_request()), so other bio sharing this stripe will not free the
2402 * stripe. If a stripe is owned by one stripe, the stripe lock will
2405 spin_lock_irq(&sh->stripe_lock);
2407 bip = &sh->dev[dd_idx].towrite;
2411 bip = &sh->dev[dd_idx].toread;
2412 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2413 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2415 bip = & (*bip)->bi_next;
2417 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2420 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2424 raid5_inc_bi_active_stripes(bi);
2427 /* check if page is covered */
2428 sector_t sector = sh->dev[dd_idx].sector;
2429 for (bi=sh->dev[dd_idx].towrite;
2430 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2431 bi && bi->bi_sector <= sector;
2432 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2433 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2434 sector = bi->bi_sector + (bi->bi_size>>9);
2436 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2437 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2440 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2441 (unsigned long long)(*bip)->bi_sector,
2442 (unsigned long long)sh->sector, dd_idx);
2443 spin_unlock_irq(&sh->stripe_lock);
2445 if (conf->mddev->bitmap && firstwrite) {
2446 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2448 sh->bm_seq = conf->seq_flush+1;
2449 set_bit(STRIPE_BIT_DELAY, &sh->state);
2454 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2455 spin_unlock_irq(&sh->stripe_lock);
2459 static void end_reshape(struct r5conf *conf);
2461 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
2462 struct stripe_head *sh)
2464 int sectors_per_chunk =
2465 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2467 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2468 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2470 raid5_compute_sector(conf,
2471 stripe * (disks - conf->max_degraded)
2472 *sectors_per_chunk + chunk_offset,
2478 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
2479 struct stripe_head_state *s, int disks,
2480 struct bio **return_bi)
2483 for (i = disks; i--; ) {
2487 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2488 struct md_rdev *rdev;
2490 rdev = rcu_dereference(conf->disks[i].rdev);
2491 if (rdev && test_bit(In_sync, &rdev->flags))
2492 atomic_inc(&rdev->nr_pending);
2497 if (!rdev_set_badblocks(
2501 md_error(conf->mddev, rdev);
2502 rdev_dec_pending(rdev, conf->mddev);
2505 spin_lock_irq(&sh->stripe_lock);
2506 /* fail all writes first */
2507 bi = sh->dev[i].towrite;
2508 sh->dev[i].towrite = NULL;
2509 spin_unlock_irq(&sh->stripe_lock);
2513 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2514 wake_up(&conf->wait_for_overlap);
2516 while (bi && bi->bi_sector <
2517 sh->dev[i].sector + STRIPE_SECTORS) {
2518 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2519 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2520 if (!raid5_dec_bi_active_stripes(bi)) {
2521 md_write_end(conf->mddev);
2522 bi->bi_next = *return_bi;
2528 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2529 STRIPE_SECTORS, 0, 0);
2531 /* and fail all 'written' */
2532 bi = sh->dev[i].written;
2533 sh->dev[i].written = NULL;
2534 if (bi) bitmap_end = 1;
2535 while (bi && bi->bi_sector <
2536 sh->dev[i].sector + STRIPE_SECTORS) {
2537 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2538 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2539 if (!raid5_dec_bi_active_stripes(bi)) {
2540 md_write_end(conf->mddev);
2541 bi->bi_next = *return_bi;
2547 /* fail any reads if this device is non-operational and
2548 * the data has not reached the cache yet.
2550 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2551 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2552 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2553 spin_lock_irq(&sh->stripe_lock);
2554 bi = sh->dev[i].toread;
2555 sh->dev[i].toread = NULL;
2556 spin_unlock_irq(&sh->stripe_lock);
2557 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2558 wake_up(&conf->wait_for_overlap);
2559 while (bi && bi->bi_sector <
2560 sh->dev[i].sector + STRIPE_SECTORS) {
2561 struct bio *nextbi =
2562 r5_next_bio(bi, sh->dev[i].sector);
2563 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2564 if (!raid5_dec_bi_active_stripes(bi)) {
2565 bi->bi_next = *return_bi;
2572 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2573 STRIPE_SECTORS, 0, 0);
2574 /* If we were in the middle of a write the parity block might
2575 * still be locked - so just clear all R5_LOCKED flags
2577 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2580 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2581 if (atomic_dec_and_test(&conf->pending_full_writes))
2582 md_wakeup_thread(conf->mddev->thread);
2586 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
2587 struct stripe_head_state *s)
2592 clear_bit(STRIPE_SYNCING, &sh->state);
2595 /* There is nothing more to do for sync/check/repair.
2596 * Don't even need to abort as that is handled elsewhere
2597 * if needed, and not always wanted e.g. if there is a known
2599 * For recover/replace we need to record a bad block on all
2600 * non-sync devices, or abort the recovery
2602 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2603 /* During recovery devices cannot be removed, so
2604 * locking and refcounting of rdevs is not needed
2606 for (i = 0; i < conf->raid_disks; i++) {
2607 struct md_rdev *rdev = conf->disks[i].rdev;
2609 && !test_bit(Faulty, &rdev->flags)
2610 && !test_bit(In_sync, &rdev->flags)
2611 && !rdev_set_badblocks(rdev, sh->sector,
2614 rdev = conf->disks[i].replacement;
2616 && !test_bit(Faulty, &rdev->flags)
2617 && !test_bit(In_sync, &rdev->flags)
2618 && !rdev_set_badblocks(rdev, sh->sector,
2623 conf->recovery_disabled =
2624 conf->mddev->recovery_disabled;
2626 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
2629 static int want_replace(struct stripe_head *sh, int disk_idx)
2631 struct md_rdev *rdev;
2633 /* Doing recovery so rcu locking not required */
2634 rdev = sh->raid_conf->disks[disk_idx].replacement;
2636 && !test_bit(Faulty, &rdev->flags)
2637 && !test_bit(In_sync, &rdev->flags)
2638 && (rdev->recovery_offset <= sh->sector
2639 || rdev->mddev->recovery_cp <= sh->sector))
2645 /* fetch_block - checks the given member device to see if its data needs
2646 * to be read or computed to satisfy a request.
2648 * Returns 1 when no more member devices need to be checked, otherwise returns
2649 * 0 to tell the loop in handle_stripe_fill to continue
2651 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2652 int disk_idx, int disks)
2654 struct r5dev *dev = &sh->dev[disk_idx];
2655 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2656 &sh->dev[s->failed_num[1]] };
2658 /* is the data in this block needed, and can we get it? */
2659 if (!test_bit(R5_LOCKED, &dev->flags) &&
2660 !test_bit(R5_UPTODATE, &dev->flags) &&
2662 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2663 s->syncing || s->expanding ||
2664 (s->replacing && want_replace(sh, disk_idx)) ||
2665 (s->failed >= 1 && fdev[0]->toread) ||
2666 (s->failed >= 2 && fdev[1]->toread) ||
2667 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2668 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2669 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
2670 /* we would like to get this block, possibly by computing it,
2671 * otherwise read it if the backing disk is insync
2673 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2674 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2675 if ((s->uptodate == disks - 1) &&
2676 (s->failed && (disk_idx == s->failed_num[0] ||
2677 disk_idx == s->failed_num[1]))) {
2678 /* have disk failed, and we're requested to fetch it;
2681 pr_debug("Computing stripe %llu block %d\n",
2682 (unsigned long long)sh->sector, disk_idx);
2683 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2684 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2685 set_bit(R5_Wantcompute, &dev->flags);
2686 sh->ops.target = disk_idx;
2687 sh->ops.target2 = -1; /* no 2nd target */
2689 /* Careful: from this point on 'uptodate' is in the eye
2690 * of raid_run_ops which services 'compute' operations
2691 * before writes. R5_Wantcompute flags a block that will
2692 * be R5_UPTODATE by the time it is needed for a
2693 * subsequent operation.
2697 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2698 /* Computing 2-failure is *very* expensive; only
2699 * do it if failed >= 2
2702 for (other = disks; other--; ) {
2703 if (other == disk_idx)
2705 if (!test_bit(R5_UPTODATE,
2706 &sh->dev[other].flags))
2710 pr_debug("Computing stripe %llu blocks %d,%d\n",
2711 (unsigned long long)sh->sector,
2713 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2714 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2715 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2716 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2717 sh->ops.target = disk_idx;
2718 sh->ops.target2 = other;
2722 } else if (test_bit(R5_Insync, &dev->flags)) {
2723 set_bit(R5_LOCKED, &dev->flags);
2724 set_bit(R5_Wantread, &dev->flags);
2726 pr_debug("Reading block %d (sync=%d)\n",
2727 disk_idx, s->syncing);
2735 * handle_stripe_fill - read or compute data to satisfy pending requests.
2737 static void handle_stripe_fill(struct stripe_head *sh,
2738 struct stripe_head_state *s,
2743 /* look for blocks to read/compute, skip this if a compute
2744 * is already in flight, or if the stripe contents are in the
2745 * midst of changing due to a write
2747 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2748 !sh->reconstruct_state)
2749 for (i = disks; i--; )
2750 if (fetch_block(sh, s, i, disks))
2752 set_bit(STRIPE_HANDLE, &sh->state);
2756 /* handle_stripe_clean_event
2757 * any written block on an uptodate or failed drive can be returned.
2758 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2759 * never LOCKED, so we don't need to test 'failed' directly.
2761 static void handle_stripe_clean_event(struct r5conf *conf,
2762 struct stripe_head *sh, int disks, struct bio **return_bi)
2767 for (i = disks; i--; )
2768 if (sh->dev[i].written) {
2770 if (!test_bit(R5_LOCKED, &dev->flags) &&
2771 (test_bit(R5_UPTODATE, &dev->flags) ||
2772 test_and_clear_bit(R5_Discard, &dev->flags))) {
2773 /* We can return any write requests */
2774 struct bio *wbi, *wbi2;
2775 pr_debug("Return write for disc %d\n", i);
2777 dev->written = NULL;
2778 while (wbi && wbi->bi_sector <
2779 dev->sector + STRIPE_SECTORS) {
2780 wbi2 = r5_next_bio(wbi, dev->sector);
2781 if (!raid5_dec_bi_active_stripes(wbi)) {
2782 md_write_end(conf->mddev);
2783 wbi->bi_next = *return_bi;
2788 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2790 !test_bit(STRIPE_DEGRADED, &sh->state),
2795 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2796 if (atomic_dec_and_test(&conf->pending_full_writes))
2797 md_wakeup_thread(conf->mddev->thread);
2800 static void handle_stripe_dirtying(struct r5conf *conf,
2801 struct stripe_head *sh,
2802 struct stripe_head_state *s,
2805 int rmw = 0, rcw = 0, i;
2806 sector_t recovery_cp = conf->mddev->recovery_cp;
2808 /* RAID6 requires 'rcw' in current implementation.
2809 * Otherwise, check whether resync is now happening or should start.
2810 * If yes, then the array is dirty (after unclean shutdown or
2811 * initial creation), so parity in some stripes might be inconsistent.
2812 * In this case, we need to always do reconstruct-write, to ensure
2813 * that in case of drive failure or read-error correction, we
2814 * generate correct data from the parity.
2816 if (conf->max_degraded == 2 ||
2817 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2818 /* Calculate the real rcw later - for now make it
2819 * look like rcw is cheaper
2822 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2823 conf->max_degraded, (unsigned long long)recovery_cp,
2824 (unsigned long long)sh->sector);
2825 } else for (i = disks; i--; ) {
2826 /* would I have to read this buffer for read_modify_write */
2827 struct r5dev *dev = &sh->dev[i];
2828 if ((dev->towrite || i == sh->pd_idx) &&
2829 !test_bit(R5_LOCKED, &dev->flags) &&
2830 !(test_bit(R5_UPTODATE, &dev->flags) ||
2831 test_bit(R5_Wantcompute, &dev->flags))) {
2832 if (test_bit(R5_Insync, &dev->flags))
2835 rmw += 2*disks; /* cannot read it */
2837 /* Would I have to read this buffer for reconstruct_write */
2838 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2839 !test_bit(R5_LOCKED, &dev->flags) &&
2840 !(test_bit(R5_UPTODATE, &dev->flags) ||
2841 test_bit(R5_Wantcompute, &dev->flags))) {
2842 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2847 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2848 (unsigned long long)sh->sector, rmw, rcw);
2849 set_bit(STRIPE_HANDLE, &sh->state);
2850 if (rmw < rcw && rmw > 0)
2851 /* prefer read-modify-write, but need to get some data */
2852 for (i = disks; i--; ) {
2853 struct r5dev *dev = &sh->dev[i];
2854 if ((dev->towrite || i == sh->pd_idx) &&
2855 !test_bit(R5_LOCKED, &dev->flags) &&
2856 !(test_bit(R5_UPTODATE, &dev->flags) ||
2857 test_bit(R5_Wantcompute, &dev->flags)) &&
2858 test_bit(R5_Insync, &dev->flags)) {
2860 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2861 pr_debug("Read_old block "
2862 "%d for r-m-w\n", i);
2863 set_bit(R5_LOCKED, &dev->flags);
2864 set_bit(R5_Wantread, &dev->flags);
2867 set_bit(STRIPE_DELAYED, &sh->state);
2868 set_bit(STRIPE_HANDLE, &sh->state);
2872 if (rcw <= rmw && rcw > 0) {
2873 /* want reconstruct write, but need to get some data */
2875 for (i = disks; i--; ) {
2876 struct r5dev *dev = &sh->dev[i];
2877 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2878 i != sh->pd_idx && i != sh->qd_idx &&
2879 !test_bit(R5_LOCKED, &dev->flags) &&
2880 !(test_bit(R5_UPTODATE, &dev->flags) ||
2881 test_bit(R5_Wantcompute, &dev->flags))) {
2883 if (!test_bit(R5_Insync, &dev->flags))
2884 continue; /* it's a failed drive */
2886 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2887 pr_debug("Read_old block "
2888 "%d for Reconstruct\n", i);
2889 set_bit(R5_LOCKED, &dev->flags);
2890 set_bit(R5_Wantread, &dev->flags);
2893 set_bit(STRIPE_DELAYED, &sh->state);
2894 set_bit(STRIPE_HANDLE, &sh->state);
2899 /* now if nothing is locked, and if we have enough data,
2900 * we can start a write request
2902 /* since handle_stripe can be called at any time we need to handle the
2903 * case where a compute block operation has been submitted and then a
2904 * subsequent call wants to start a write request. raid_run_ops only
2905 * handles the case where compute block and reconstruct are requested
2906 * simultaneously. If this is not the case then new writes need to be
2907 * held off until the compute completes.
2909 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2910 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2911 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2912 schedule_reconstruction(sh, s, rcw == 0, 0);
2915 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
2916 struct stripe_head_state *s, int disks)
2918 struct r5dev *dev = NULL;
2920 set_bit(STRIPE_HANDLE, &sh->state);
2922 switch (sh->check_state) {
2923 case check_state_idle:
2924 /* start a new check operation if there are no failures */
2925 if (s->failed == 0) {
2926 BUG_ON(s->uptodate != disks);
2927 sh->check_state = check_state_run;
2928 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2929 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2933 dev = &sh->dev[s->failed_num[0]];
2935 case check_state_compute_result:
2936 sh->check_state = check_state_idle;
2938 dev = &sh->dev[sh->pd_idx];
2940 /* check that a write has not made the stripe insync */
2941 if (test_bit(STRIPE_INSYNC, &sh->state))
2944 /* either failed parity check, or recovery is happening */
2945 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2946 BUG_ON(s->uptodate != disks);
2948 set_bit(R5_LOCKED, &dev->flags);
2950 set_bit(R5_Wantwrite, &dev->flags);
2952 clear_bit(STRIPE_DEGRADED, &sh->state);
2953 set_bit(STRIPE_INSYNC, &sh->state);
2955 case check_state_run:
2956 break; /* we will be called again upon completion */
2957 case check_state_check_result:
2958 sh->check_state = check_state_idle;
2960 /* if a failure occurred during the check operation, leave
2961 * STRIPE_INSYNC not set and let the stripe be handled again
2966 /* handle a successful check operation, if parity is correct
2967 * we are done. Otherwise update the mismatch count and repair
2968 * parity if !MD_RECOVERY_CHECK
2970 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
2971 /* parity is correct (on disc,
2972 * not in buffer any more)
2974 set_bit(STRIPE_INSYNC, &sh->state);
2976 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
2977 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2978 /* don't try to repair!! */
2979 set_bit(STRIPE_INSYNC, &sh->state);
2981 sh->check_state = check_state_compute_run;
2982 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2983 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2984 set_bit(R5_Wantcompute,
2985 &sh->dev[sh->pd_idx].flags);
2986 sh->ops.target = sh->pd_idx;
2987 sh->ops.target2 = -1;
2992 case check_state_compute_run:
2995 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2996 __func__, sh->check_state,
2997 (unsigned long long) sh->sector);
3003 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
3004 struct stripe_head_state *s,
3007 int pd_idx = sh->pd_idx;
3008 int qd_idx = sh->qd_idx;
3011 set_bit(STRIPE_HANDLE, &sh->state);
3013 BUG_ON(s->failed > 2);
3015 /* Want to check and possibly repair P and Q.
3016 * However there could be one 'failed' device, in which
3017 * case we can only check one of them, possibly using the
3018 * other to generate missing data
3021 switch (sh->check_state) {
3022 case check_state_idle:
3023 /* start a new check operation if there are < 2 failures */
3024 if (s->failed == s->q_failed) {
3025 /* The only possible failed device holds Q, so it
3026 * makes sense to check P (If anything else were failed,
3027 * we would have used P to recreate it).
3029 sh->check_state = check_state_run;
3031 if (!s->q_failed && s->failed < 2) {
3032 /* Q is not failed, and we didn't use it to generate
3033 * anything, so it makes sense to check it
3035 if (sh->check_state == check_state_run)
3036 sh->check_state = check_state_run_pq;
3038 sh->check_state = check_state_run_q;
3041 /* discard potentially stale zero_sum_result */
3042 sh->ops.zero_sum_result = 0;
3044 if (sh->check_state == check_state_run) {
3045 /* async_xor_zero_sum destroys the contents of P */
3046 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3049 if (sh->check_state >= check_state_run &&
3050 sh->check_state <= check_state_run_pq) {
3051 /* async_syndrome_zero_sum preserves P and Q, so
3052 * no need to mark them !uptodate here
3054 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3058 /* we have 2-disk failure */
3059 BUG_ON(s->failed != 2);
3061 case check_state_compute_result:
3062 sh->check_state = check_state_idle;
3064 /* check that a write has not made the stripe insync */
3065 if (test_bit(STRIPE_INSYNC, &sh->state))
3068 /* now write out any block on a failed drive,
3069 * or P or Q if they were recomputed
3071 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
3072 if (s->failed == 2) {
3073 dev = &sh->dev[s->failed_num[1]];
3075 set_bit(R5_LOCKED, &dev->flags);
3076 set_bit(R5_Wantwrite, &dev->flags);
3078 if (s->failed >= 1) {
3079 dev = &sh->dev[s->failed_num[0]];
3081 set_bit(R5_LOCKED, &dev->flags);
3082 set_bit(R5_Wantwrite, &dev->flags);
3084 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3085 dev = &sh->dev[pd_idx];
3087 set_bit(R5_LOCKED, &dev->flags);
3088 set_bit(R5_Wantwrite, &dev->flags);
3090 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3091 dev = &sh->dev[qd_idx];
3093 set_bit(R5_LOCKED, &dev->flags);
3094 set_bit(R5_Wantwrite, &dev->flags);
3096 clear_bit(STRIPE_DEGRADED, &sh->state);
3098 set_bit(STRIPE_INSYNC, &sh->state);
3100 case check_state_run:
3101 case check_state_run_q:
3102 case check_state_run_pq:
3103 break; /* we will be called again upon completion */
3104 case check_state_check_result:
3105 sh->check_state = check_state_idle;
3107 /* handle a successful check operation, if parity is correct
3108 * we are done. Otherwise update the mismatch count and repair
3109 * parity if !MD_RECOVERY_CHECK
3111 if (sh->ops.zero_sum_result == 0) {
3112 /* both parities are correct */
3114 set_bit(STRIPE_INSYNC, &sh->state);
3116 /* in contrast to the raid5 case we can validate
3117 * parity, but still have a failure to write
3120 sh->check_state = check_state_compute_result;
3121 /* Returning at this point means that we may go
3122 * off and bring p and/or q uptodate again so
3123 * we make sure to check zero_sum_result again
3124 * to verify if p or q need writeback
3128 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
3129 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3130 /* don't try to repair!! */
3131 set_bit(STRIPE_INSYNC, &sh->state);
3133 int *target = &sh->ops.target;
3135 sh->ops.target = -1;
3136 sh->ops.target2 = -1;
3137 sh->check_state = check_state_compute_run;
3138 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3139 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3140 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3141 set_bit(R5_Wantcompute,
3142 &sh->dev[pd_idx].flags);
3144 target = &sh->ops.target2;
3147 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3148 set_bit(R5_Wantcompute,
3149 &sh->dev[qd_idx].flags);
3156 case check_state_compute_run:
3159 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3160 __func__, sh->check_state,
3161 (unsigned long long) sh->sector);
3166 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
3170 /* We have read all the blocks in this stripe and now we need to
3171 * copy some of them into a target stripe for expand.
3173 struct dma_async_tx_descriptor *tx = NULL;
3174 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3175 for (i = 0; i < sh->disks; i++)
3176 if (i != sh->pd_idx && i != sh->qd_idx) {
3178 struct stripe_head *sh2;
3179 struct async_submit_ctl submit;
3181 sector_t bn = compute_blocknr(sh, i, 1);
3182 sector_t s = raid5_compute_sector(conf, bn, 0,
3184 sh2 = get_active_stripe(conf, s, 0, 1, 1);
3186 /* so far only the early blocks of this stripe
3187 * have been requested. When later blocks
3188 * get requested, we will try again
3191 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3192 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3193 /* must have already done this block */
3194 release_stripe(sh2);
3198 /* place all the copies on one channel */
3199 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
3200 tx = async_memcpy(sh2->dev[dd_idx].page,
3201 sh->dev[i].page, 0, 0, STRIPE_SIZE,
3204 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3205 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3206 for (j = 0; j < conf->raid_disks; j++)
3207 if (j != sh2->pd_idx &&
3209 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3211 if (j == conf->raid_disks) {
3212 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3213 set_bit(STRIPE_HANDLE, &sh2->state);
3215 release_stripe(sh2);
3218 /* done submitting copies, wait for them to complete */
3221 dma_wait_for_async_tx(tx);
3226 * handle_stripe - do things to a stripe.
3228 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3229 * state of various bits to see what needs to be done.
3231 * return some read requests which now have data
3232 * return some write requests which are safely on storage
3233 * schedule a read on some buffers
3234 * schedule a write of some buffers
3235 * return confirmation of parity correctness
3239 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
3241 struct r5conf *conf = sh->raid_conf;
3242 int disks = sh->disks;
3245 int do_recovery = 0;
3247 memset(s, 0, sizeof(*s));
3249 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3250 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3251 s->failed_num[0] = -1;
3252 s->failed_num[1] = -1;
3254 /* Now to look around and see what can be done */
3256 for (i=disks; i--; ) {
3257 struct md_rdev *rdev;
3264 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3266 dev->toread, dev->towrite, dev->written);
3267 /* maybe we can reply to a read
3269 * new wantfill requests are only permitted while
3270 * ops_complete_biofill is guaranteed to be inactive
3272 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3273 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3274 set_bit(R5_Wantfill, &dev->flags);
3276 /* now count some things */
3277 if (test_bit(R5_LOCKED, &dev->flags))
3279 if (test_bit(R5_UPTODATE, &dev->flags))
3281 if (test_bit(R5_Wantcompute, &dev->flags)) {
3283 BUG_ON(s->compute > 2);
3286 if (test_bit(R5_Wantfill, &dev->flags))
3288 else if (dev->toread)
3292 if (!test_bit(R5_OVERWRITE, &dev->flags))
3297 /* Prefer to use the replacement for reads, but only
3298 * if it is recovered enough and has no bad blocks.
3300 rdev = rcu_dereference(conf->disks[i].replacement);
3301 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3302 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3303 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3304 &first_bad, &bad_sectors))
3305 set_bit(R5_ReadRepl, &dev->flags);
3308 set_bit(R5_NeedReplace, &dev->flags);
3309 rdev = rcu_dereference(conf->disks[i].rdev);
3310 clear_bit(R5_ReadRepl, &dev->flags);
3312 if (rdev && test_bit(Faulty, &rdev->flags))
3315 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3316 &first_bad, &bad_sectors);
3317 if (s->blocked_rdev == NULL
3318 && (test_bit(Blocked, &rdev->flags)
3321 set_bit(BlockedBadBlocks,
3323 s->blocked_rdev = rdev;
3324 atomic_inc(&rdev->nr_pending);
3327 clear_bit(R5_Insync, &dev->flags);
3331 /* also not in-sync */
3332 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3333 test_bit(R5_UPTODATE, &dev->flags)) {
3334 /* treat as in-sync, but with a read error
3335 * which we can now try to correct
3337 set_bit(R5_Insync, &dev->flags);
3338 set_bit(R5_ReadError, &dev->flags);
3340 } else if (test_bit(In_sync, &rdev->flags))
3341 set_bit(R5_Insync, &dev->flags);
3342 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3343 /* in sync if before recovery_offset */
3344 set_bit(R5_Insync, &dev->flags);
3345 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3346 test_bit(R5_Expanded, &dev->flags))
3347 /* If we've reshaped into here, we assume it is Insync.
3348 * We will shortly update recovery_offset to make
3351 set_bit(R5_Insync, &dev->flags);
3353 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
3354 /* This flag does not apply to '.replacement'
3355 * only to .rdev, so make sure to check that*/
3356 struct md_rdev *rdev2 = rcu_dereference(
3357 conf->disks[i].rdev);
3359 clear_bit(R5_Insync, &dev->flags);
3360 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3361 s->handle_bad_blocks = 1;
3362 atomic_inc(&rdev2->nr_pending);
3364 clear_bit(R5_WriteError, &dev->flags);
3366 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
3367 /* This flag does not apply to '.replacement'
3368 * only to .rdev, so make sure to check that*/
3369 struct md_rdev *rdev2 = rcu_dereference(
3370 conf->disks[i].rdev);
3371 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3372 s->handle_bad_blocks = 1;
3373 atomic_inc(&rdev2->nr_pending);
3375 clear_bit(R5_MadeGood, &dev->flags);
3377 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3378 struct md_rdev *rdev2 = rcu_dereference(
3379 conf->disks[i].replacement);
3380 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3381 s->handle_bad_blocks = 1;
3382 atomic_inc(&rdev2->nr_pending);
3384 clear_bit(R5_MadeGoodRepl, &dev->flags);
3386 if (!test_bit(R5_Insync, &dev->flags)) {
3387 /* The ReadError flag will just be confusing now */
3388 clear_bit(R5_ReadError, &dev->flags);
3389 clear_bit(R5_ReWrite, &dev->flags);
3391 if (test_bit(R5_ReadError, &dev->flags))
3392 clear_bit(R5_Insync, &dev->flags);
3393 if (!test_bit(R5_Insync, &dev->flags)) {
3395 s->failed_num[s->failed] = i;
3397 if (rdev && !test_bit(Faulty, &rdev->flags))
3401 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3402 /* If there is a failed device being replaced,
3403 * we must be recovering.
3404 * else if we are after recovery_cp, we must be syncing
3405 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3406 * else we can only be replacing
3407 * sync and recovery both need to read all devices, and so
3408 * use the same flag.
3411 sh->sector >= conf->mddev->recovery_cp ||
3412 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
3420 static void handle_stripe(struct stripe_head *sh)
3422 struct stripe_head_state s;
3423 struct r5conf *conf = sh->raid_conf;
3426 int disks = sh->disks;
3427 struct r5dev *pdev, *qdev;
3429 clear_bit(STRIPE_HANDLE, &sh->state);
3430 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
3431 /* already being handled, ensure it gets handled
3432 * again when current action finishes */
3433 set_bit(STRIPE_HANDLE, &sh->state);
3437 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3438 set_bit(STRIPE_SYNCING, &sh->state);
3439 clear_bit(STRIPE_INSYNC, &sh->state);
3441 clear_bit(STRIPE_DELAYED, &sh->state);
3443 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3444 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3445 (unsigned long long)sh->sector, sh->state,
3446 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3447 sh->check_state, sh->reconstruct_state);
3449 analyse_stripe(sh, &s);
3451 if (s.handle_bad_blocks) {
3452 set_bit(STRIPE_HANDLE, &sh->state);
3456 if (unlikely(s.blocked_rdev)) {
3457 if (s.syncing || s.expanding || s.expanded ||
3458 s.replacing || s.to_write || s.written) {
3459 set_bit(STRIPE_HANDLE, &sh->state);
3462 /* There is nothing for the blocked_rdev to block */
3463 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3464 s.blocked_rdev = NULL;
3467 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3468 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3469 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3472 pr_debug("locked=%d uptodate=%d to_read=%d"
3473 " to_write=%d failed=%d failed_num=%d,%d\n",
3474 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3475 s.failed_num[0], s.failed_num[1]);
3476 /* check if the array has lost more than max_degraded devices and,
3477 * if so, some requests might need to be failed.
3479 if (s.failed > conf->max_degraded) {
3480 sh->check_state = 0;
3481 sh->reconstruct_state = 0;
3482 if (s.to_read+s.to_write+s.written)
3483 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3484 if (s.syncing + s.replacing)
3485 handle_failed_sync(conf, sh, &s);
3489 * might be able to return some write requests if the parity blocks
3490 * are safe, or on a failed drive
3492 pdev = &sh->dev[sh->pd_idx];
3493 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3494 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3495 qdev = &sh->dev[sh->qd_idx];
3496 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3497 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3501 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3502 && !test_bit(R5_LOCKED, &pdev->flags)
3503 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3504 test_bit(R5_Discard, &pdev->flags))))) &&
3505 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3506 && !test_bit(R5_LOCKED, &qdev->flags)
3507 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3508 test_bit(R5_Discard, &qdev->flags))))))
3509 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3511 /* Now we might consider reading some blocks, either to check/generate
3512 * parity, or to satisfy requests
3513 * or to load a block that is being partially written.
3515 if (s.to_read || s.non_overwrite
3516 || (conf->level == 6 && s.to_write && s.failed)
3517 || (s.syncing && (s.uptodate + s.compute < disks))
3520 handle_stripe_fill(sh, &s, disks);
3522 /* Now we check to see if any write operations have recently
3526 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3528 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3529 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3530 sh->reconstruct_state = reconstruct_state_idle;
3532 /* All the 'written' buffers and the parity block are ready to
3533 * be written back to disk
3535 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3536 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
3537 BUG_ON(sh->qd_idx >= 0 &&
3538 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3539 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
3540 for (i = disks; i--; ) {
3541 struct r5dev *dev = &sh->dev[i];
3542 if (test_bit(R5_LOCKED, &dev->flags) &&
3543 (i == sh->pd_idx || i == sh->qd_idx ||
3545 pr_debug("Writing block %d\n", i);
3546 set_bit(R5_Wantwrite, &dev->flags);
3549 if (!test_bit(R5_Insync, &dev->flags) ||
3550 ((i == sh->pd_idx || i == sh->qd_idx) &&
3552 set_bit(STRIPE_INSYNC, &sh->state);
3555 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3556 s.dec_preread_active = 1;
3559 /* Now to consider new write requests and what else, if anything
3560 * should be read. We do not handle new writes when:
3561 * 1/ A 'write' operation (copy+xor) is already in flight.
3562 * 2/ A 'check' operation is in flight, as it may clobber the parity
3565 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3566 handle_stripe_dirtying(conf, sh, &s, disks);
3568 /* maybe we need to check and possibly fix the parity for this stripe
3569 * Any reads will already have been scheduled, so we just see if enough
3570 * data is available. The parity check is held off while parity
3571 * dependent operations are in flight.
3573 if (sh->check_state ||
3574 (s.syncing && s.locked == 0 &&
3575 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3576 !test_bit(STRIPE_INSYNC, &sh->state))) {
3577 if (conf->level == 6)
3578 handle_parity_checks6(conf, sh, &s, disks);
3580 handle_parity_checks5(conf, sh, &s, disks);
3583 if (s.replacing && s.locked == 0
3584 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3585 /* Write out to replacement devices where possible */
3586 for (i = 0; i < conf->raid_disks; i++)
3587 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3588 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3589 set_bit(R5_WantReplace, &sh->dev[i].flags);
3590 set_bit(R5_LOCKED, &sh->dev[i].flags);
3593 set_bit(STRIPE_INSYNC, &sh->state);
3595 if ((s.syncing || s.replacing) && s.locked == 0 &&
3596 test_bit(STRIPE_INSYNC, &sh->state)) {
3597 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3598 clear_bit(STRIPE_SYNCING, &sh->state);
3601 /* If the failed drives are just a ReadError, then we might need
3602 * to progress the repair/check process
3604 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3605 for (i = 0; i < s.failed; i++) {
3606 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3607 if (test_bit(R5_ReadError, &dev->flags)
3608 && !test_bit(R5_LOCKED, &dev->flags)
3609 && test_bit(R5_UPTODATE, &dev->flags)
3611 if (!test_bit(R5_ReWrite, &dev->flags)) {
3612 set_bit(R5_Wantwrite, &dev->flags);
3613 set_bit(R5_ReWrite, &dev->flags);
3614 set_bit(R5_LOCKED, &dev->flags);
3617 /* let's read it back */
3618 set_bit(R5_Wantread, &dev->flags);
3619 set_bit(R5_LOCKED, &dev->flags);
3626 /* Finish reconstruct operations initiated by the expansion process */
3627 if (sh->reconstruct_state == reconstruct_state_result) {
3628 struct stripe_head *sh_src
3629 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3630 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3631 /* sh cannot be written until sh_src has been read.
3632 * so arrange for sh to be delayed a little
3634 set_bit(STRIPE_DELAYED, &sh->state);
3635 set_bit(STRIPE_HANDLE, &sh->state);
3636 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3638 atomic_inc(&conf->preread_active_stripes);
3639 release_stripe(sh_src);
3643 release_stripe(sh_src);
3645 sh->reconstruct_state = reconstruct_state_idle;
3646 clear_bit(STRIPE_EXPANDING, &sh->state);
3647 for (i = conf->raid_disks; i--; ) {
3648 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3649 set_bit(R5_LOCKED, &sh->dev[i].flags);
3654 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3655 !sh->reconstruct_state) {
3656 /* Need to write out all blocks after computing parity */
3657 sh->disks = conf->raid_disks;
3658 stripe_set_idx(sh->sector, conf, 0, sh);
3659 schedule_reconstruction(sh, &s, 1, 1);
3660 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3661 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3662 atomic_dec(&conf->reshape_stripes);
3663 wake_up(&conf->wait_for_overlap);
3664 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3667 if (s.expanding && s.locked == 0 &&
3668 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3669 handle_stripe_expansion(conf, sh);
3672 /* wait for this device to become unblocked */
3673 if (unlikely(s.blocked_rdev)) {
3674 if (conf->mddev->external)
3675 md_wait_for_blocked_rdev(s.blocked_rdev,
3678 /* Internal metadata will immediately
3679 * be written by raid5d, so we don't
3680 * need to wait here.
3682 rdev_dec_pending(s.blocked_rdev,
3686 if (s.handle_bad_blocks)
3687 for (i = disks; i--; ) {
3688 struct md_rdev *rdev;
3689 struct r5dev *dev = &sh->dev[i];
3690 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3691 /* We own a safe reference to the rdev */
3692 rdev = conf->disks[i].rdev;
3693 if (!rdev_set_badblocks(rdev, sh->sector,
3695 md_error(conf->mddev, rdev);
3696 rdev_dec_pending(rdev, conf->mddev);
3698 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3699 rdev = conf->disks[i].rdev;
3700 rdev_clear_badblocks(rdev, sh->sector,
3702 rdev_dec_pending(rdev, conf->mddev);
3704 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3705 rdev = conf->disks[i].replacement;
3707 /* rdev have been moved down */
3708 rdev = conf->disks[i].rdev;
3709 rdev_clear_badblocks(rdev, sh->sector,
3711 rdev_dec_pending(rdev, conf->mddev);
3716 raid_run_ops(sh, s.ops_request);
3720 if (s.dec_preread_active) {
3721 /* We delay this until after ops_run_io so that if make_request
3722 * is waiting on a flush, it won't continue until the writes
3723 * have actually been submitted.
3725 atomic_dec(&conf->preread_active_stripes);
3726 if (atomic_read(&conf->preread_active_stripes) <
3728 md_wakeup_thread(conf->mddev->thread);
3731 return_io(s.return_bi);
3733 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
3736 static void raid5_activate_delayed(struct r5conf *conf)
3738 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3739 while (!list_empty(&conf->delayed_list)) {
3740 struct list_head *l = conf->delayed_list.next;
3741 struct stripe_head *sh;
3742 sh = list_entry(l, struct stripe_head, lru);
3744 clear_bit(STRIPE_DELAYED, &sh->state);
3745 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3746 atomic_inc(&conf->preread_active_stripes);
3747 list_add_tail(&sh->lru, &conf->hold_list);
3752 static void activate_bit_delay(struct r5conf *conf)
3754 /* device_lock is held */
3755 struct list_head head;
3756 list_add(&head, &conf->bitmap_list);
3757 list_del_init(&conf->bitmap_list);
3758 while (!list_empty(&head)) {
3759 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3760 list_del_init(&sh->lru);
3761 atomic_inc(&sh->count);
3762 __release_stripe(conf, sh);
3766 int md_raid5_congested(struct mddev *mddev, int bits)
3768 struct r5conf *conf = mddev->private;
3770 /* No difference between reads and writes. Just check
3771 * how busy the stripe_cache is
3774 if (conf->inactive_blocked)
3778 if (list_empty_careful(&conf->inactive_list))
3783 EXPORT_SYMBOL_GPL(md_raid5_congested);
3785 static int raid5_congested(void *data, int bits)
3787 struct mddev *mddev = data;
3789 return mddev_congested(mddev, bits) ||
3790 md_raid5_congested(mddev, bits);
3793 /* We want read requests to align with chunks where possible,
3794 * but write requests don't need to.
3796 static int raid5_mergeable_bvec(struct request_queue *q,
3797 struct bvec_merge_data *bvm,
3798 struct bio_vec *biovec)
3800 struct mddev *mddev = q->queuedata;
3801 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
3803 unsigned int chunk_sectors = mddev->chunk_sectors;
3804 unsigned int bio_sectors = bvm->bi_size >> 9;
3806 if ((bvm->bi_rw & 1) == WRITE)
3807 return biovec->bv_len; /* always allow writes to be mergeable */
3809 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3810 chunk_sectors = mddev->new_chunk_sectors;
3811 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3812 if (max < 0) max = 0;
3813 if (max <= biovec->bv_len && bio_sectors == 0)
3814 return biovec->bv_len;
3820 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
3822 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3823 unsigned int chunk_sectors = mddev->chunk_sectors;
3824 unsigned int bio_sectors = bio->bi_size >> 9;
3826 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3827 chunk_sectors = mddev->new_chunk_sectors;
3828 return chunk_sectors >=
3829 ((sector & (chunk_sectors - 1)) + bio_sectors);
3833 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3834 * later sampled by raid5d.
3836 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
3838 unsigned long flags;
3840 spin_lock_irqsave(&conf->device_lock, flags);
3842 bi->bi_next = conf->retry_read_aligned_list;
3843 conf->retry_read_aligned_list = bi;
3845 spin_unlock_irqrestore(&conf->device_lock, flags);
3846 md_wakeup_thread(conf->mddev->thread);
3850 static struct bio *remove_bio_from_retry(struct r5conf *conf)
3854 bi = conf->retry_read_aligned;
3856 conf->retry_read_aligned = NULL;
3859 bi = conf->retry_read_aligned_list;
3861 conf->retry_read_aligned_list = bi->bi_next;
3864 * this sets the active strip count to 1 and the processed
3865 * strip count to zero (upper 8 bits)
3867 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
3875 * The "raid5_align_endio" should check if the read succeeded and if it
3876 * did, call bio_endio on the original bio (having bio_put the new bio
3878 * If the read failed..
3880 static void raid5_align_endio(struct bio *bi, int error)
3882 struct bio* raid_bi = bi->bi_private;
3883 struct mddev *mddev;
3884 struct r5conf *conf;
3885 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3886 struct md_rdev *rdev;
3890 rdev = (void*)raid_bi->bi_next;
3891 raid_bi->bi_next = NULL;
3892 mddev = rdev->mddev;
3893 conf = mddev->private;
3895 rdev_dec_pending(rdev, conf->mddev);
3897 if (!error && uptodate) {
3898 bio_endio(raid_bi, 0);
3899 if (atomic_dec_and_test(&conf->active_aligned_reads))
3900 wake_up(&conf->wait_for_stripe);
3905 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3907 add_bio_to_retry(raid_bi, conf);
3910 static int bio_fits_rdev(struct bio *bi)
3912 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
3914 if ((bi->bi_size>>9) > queue_max_sectors(q))
3916 blk_recount_segments(q, bi);
3917 if (bi->bi_phys_segments > queue_max_segments(q))
3920 if (q->merge_bvec_fn)
3921 /* it's too hard to apply the merge_bvec_fn at this stage,
3930 static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
3932 struct r5conf *conf = mddev->private;
3934 struct bio* align_bi;
3935 struct md_rdev *rdev;
3936 sector_t end_sector;
3938 if (!in_chunk_boundary(mddev, raid_bio)) {
3939 pr_debug("chunk_aligned_read : non aligned\n");
3943 * use bio_clone_mddev to make a copy of the bio
3945 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
3949 * set bi_end_io to a new function, and set bi_private to the
3952 align_bi->bi_end_io = raid5_align_endio;
3953 align_bi->bi_private = raid_bio;
3957 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3961 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
3963 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3964 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3965 rdev->recovery_offset < end_sector) {
3966 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3968 (test_bit(Faulty, &rdev->flags) ||
3969 !(test_bit(In_sync, &rdev->flags) ||
3970 rdev->recovery_offset >= end_sector)))
3977 atomic_inc(&rdev->nr_pending);
3979 raid_bio->bi_next = (void*)rdev;
3980 align_bi->bi_bdev = rdev->bdev;
3981 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3983 if (!bio_fits_rdev(align_bi) ||
3984 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3985 &first_bad, &bad_sectors)) {
3986 /* too big in some way, or has a known bad block */
3988 rdev_dec_pending(rdev, mddev);
3992 /* No reshape active, so we can trust rdev->data_offset */
3993 align_bi->bi_sector += rdev->data_offset;
3995 spin_lock_irq(&conf->device_lock);
3996 wait_event_lock_irq(conf->wait_for_stripe,
3998 conf->device_lock, /* nothing */);
3999 atomic_inc(&conf->active_aligned_reads);
4000 spin_unlock_irq(&conf->device_lock);
4002 generic_make_request(align_bi);
4011 /* __get_priority_stripe - get the next stripe to process
4013 * Full stripe writes are allowed to pass preread active stripes up until
4014 * the bypass_threshold is exceeded. In general the bypass_count
4015 * increments when the handle_list is handled before the hold_list; however, it
4016 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4017 * stripe with in flight i/o. The bypass_count will be reset when the
4018 * head of the hold_list has changed, i.e. the head was promoted to the
4021 static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
4023 struct stripe_head *sh;
4025 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4027 list_empty(&conf->handle_list) ? "empty" : "busy",
4028 list_empty(&conf->hold_list) ? "empty" : "busy",
4029 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4031 if (!list_empty(&conf->handle_list)) {
4032 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4034 if (list_empty(&conf->hold_list))
4035 conf->bypass_count = 0;
4036 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4037 if (conf->hold_list.next == conf->last_hold)
4038 conf->bypass_count++;
4040 conf->last_hold = conf->hold_list.next;
4041 conf->bypass_count -= conf->bypass_threshold;
4042 if (conf->bypass_count < 0)
4043 conf->bypass_count = 0;
4046 } else if (!list_empty(&conf->hold_list) &&
4047 ((conf->bypass_threshold &&
4048 conf->bypass_count > conf->bypass_threshold) ||
4049 atomic_read(&conf->pending_full_writes) == 0)) {
4050 sh = list_entry(conf->hold_list.next,
4052 conf->bypass_count -= conf->bypass_threshold;
4053 if (conf->bypass_count < 0)
4054 conf->bypass_count = 0;
4058 list_del_init(&sh->lru);
4059 atomic_inc(&sh->count);
4060 BUG_ON(atomic_read(&sh->count) != 1);
4064 struct raid5_plug_cb {
4065 struct blk_plug_cb cb;
4066 struct list_head list;
4069 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4071 struct raid5_plug_cb *cb = container_of(
4072 blk_cb, struct raid5_plug_cb, cb);
4073 struct stripe_head *sh;
4074 struct mddev *mddev = cb->cb.data;
4075 struct r5conf *conf = mddev->private;
4077 if (cb->list.next && !list_empty(&cb->list)) {
4078 spin_lock_irq(&conf->device_lock);
4079 while (!list_empty(&cb->list)) {
4080 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4081 list_del_init(&sh->lru);
4083 * avoid race release_stripe_plug() sees
4084 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4085 * is still in our list
4087 smp_mb__before_clear_bit();
4088 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4089 __release_stripe(conf, sh);
4091 spin_unlock_irq(&conf->device_lock);
4096 static void release_stripe_plug(struct mddev *mddev,
4097 struct stripe_head *sh)
4099 struct blk_plug_cb *blk_cb = blk_check_plugged(
4100 raid5_unplug, mddev,
4101 sizeof(struct raid5_plug_cb));
4102 struct raid5_plug_cb *cb;
4109 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4111 if (cb->list.next == NULL)
4112 INIT_LIST_HEAD(&cb->list);
4114 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4115 list_add_tail(&sh->lru, &cb->list);
4120 static void make_discard_request(struct mddev *mddev, struct bio *bi)
4122 struct r5conf *conf = mddev->private;
4123 sector_t logical_sector, last_sector;
4124 struct stripe_head *sh;
4128 if (mddev->reshape_position != MaxSector)
4129 /* Skip discard while reshape is happening */
4132 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4133 last_sector = bi->bi_sector + (bi->bi_size>>9);
4136 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4138 stripe_sectors = conf->chunk_sectors *
4139 (conf->raid_disks - conf->max_degraded);
4140 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4142 sector_div(last_sector, stripe_sectors);
4144 logical_sector *= conf->chunk_sectors;
4145 last_sector *= conf->chunk_sectors;
4147 for (; logical_sector < last_sector;
4148 logical_sector += STRIPE_SECTORS) {
4152 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4153 prepare_to_wait(&conf->wait_for_overlap, &w,
4154 TASK_UNINTERRUPTIBLE);
4155 spin_lock_irq(&sh->stripe_lock);
4156 for (d = 0; d < conf->raid_disks; d++) {
4157 if (d == sh->pd_idx || d == sh->qd_idx)
4159 if (sh->dev[d].towrite || sh->dev[d].toread) {
4160 set_bit(R5_Overlap, &sh->dev[d].flags);
4161 spin_unlock_irq(&sh->stripe_lock);
4167 finish_wait(&conf->wait_for_overlap, &w);
4168 for (d = 0; d < conf->raid_disks; d++) {
4169 if (d == sh->pd_idx || d == sh->qd_idx)
4171 sh->dev[d].towrite = bi;
4172 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4173 raid5_inc_bi_active_stripes(bi);
4175 spin_unlock_irq(&sh->stripe_lock);
4176 if (conf->mddev->bitmap) {
4178 d < conf->raid_disks - conf->max_degraded;
4180 bitmap_startwrite(mddev->bitmap,
4184 sh->bm_seq = conf->seq_flush + 1;
4185 set_bit(STRIPE_BIT_DELAY, &sh->state);
4188 set_bit(STRIPE_HANDLE, &sh->state);
4189 clear_bit(STRIPE_DELAYED, &sh->state);
4190 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4191 atomic_inc(&conf->preread_active_stripes);
4192 release_stripe_plug(mddev, sh);
4195 remaining = raid5_dec_bi_active_stripes(bi);
4196 if (remaining == 0) {
4197 md_write_end(mddev);
4202 static void make_request(struct mddev *mddev, struct bio * bi)
4204 struct r5conf *conf = mddev->private;
4206 sector_t new_sector;
4207 sector_t logical_sector, last_sector;
4208 struct stripe_head *sh;
4209 const int rw = bio_data_dir(bi);
4212 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4213 md_flush_request(mddev, bi);
4217 md_write_start(mddev, bi);
4220 mddev->reshape_position == MaxSector &&
4221 chunk_aligned_read(mddev,bi))
4224 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4225 make_discard_request(mddev, bi);
4229 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4230 last_sector = bi->bi_sector + (bi->bi_size>>9);
4232 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4234 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4240 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
4241 if (unlikely(conf->reshape_progress != MaxSector)) {
4242 /* spinlock is needed as reshape_progress may be
4243 * 64bit on a 32bit platform, and so it might be
4244 * possible to see a half-updated value
4245 * Of course reshape_progress could change after
4246 * the lock is dropped, so once we get a reference
4247 * to the stripe that we think it is, we will have
4250 spin_lock_irq(&conf->device_lock);
4251 if (mddev->reshape_backwards
4252 ? logical_sector < conf->reshape_progress
4253 : logical_sector >= conf->reshape_progress) {
4256 if (mddev->reshape_backwards
4257 ? logical_sector < conf->reshape_safe
4258 : logical_sector >= conf->reshape_safe) {
4259 spin_unlock_irq(&conf->device_lock);
4264 spin_unlock_irq(&conf->device_lock);
4267 new_sector = raid5_compute_sector(conf, logical_sector,
4270 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4271 (unsigned long long)new_sector,
4272 (unsigned long long)logical_sector);
4274 sh = get_active_stripe(conf, new_sector, previous,
4275 (bi->bi_rw&RWA_MASK), 0);
4277 if (unlikely(previous)) {
4278 /* expansion might have moved on while waiting for a
4279 * stripe, so we must do the range check again.
4280 * Expansion could still move past after this
4281 * test, but as we are holding a reference to
4282 * 'sh', we know that if that happens,
4283 * STRIPE_EXPANDING will get set and the expansion
4284 * won't proceed until we finish with the stripe.
4287 spin_lock_irq(&conf->device_lock);
4288 if (mddev->reshape_backwards
4289 ? logical_sector >= conf->reshape_progress
4290 : logical_sector < conf->reshape_progress)
4291 /* mismatch, need to try again */
4293 spin_unlock_irq(&conf->device_lock);
4302 logical_sector >= mddev->suspend_lo &&
4303 logical_sector < mddev->suspend_hi) {
4305 /* As the suspend_* range is controlled by
4306 * userspace, we want an interruptible
4309 flush_signals(current);
4310 prepare_to_wait(&conf->wait_for_overlap,
4311 &w, TASK_INTERRUPTIBLE);
4312 if (logical_sector >= mddev->suspend_lo &&
4313 logical_sector < mddev->suspend_hi)
4318 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
4319 !add_stripe_bio(sh, bi, dd_idx, rw)) {
4320 /* Stripe is busy expanding or
4321 * add failed due to overlap. Flush everything
4324 md_wakeup_thread(mddev->thread);
4329 finish_wait(&conf->wait_for_overlap, &w);
4330 set_bit(STRIPE_HANDLE, &sh->state);
4331 clear_bit(STRIPE_DELAYED, &sh->state);
4332 if ((bi->bi_rw & REQ_NOIDLE) &&
4333 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4334 atomic_inc(&conf->preread_active_stripes);
4335 release_stripe_plug(mddev, sh);
4337 /* cannot get stripe for read-ahead, just give-up */
4338 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4339 finish_wait(&conf->wait_for_overlap, &w);
4344 remaining = raid5_dec_bi_active_stripes(bi);
4345 if (remaining == 0) {
4348 md_write_end(mddev);
4354 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
4356 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
4358 /* reshaping is quite different to recovery/resync so it is
4359 * handled quite separately ... here.
4361 * On each call to sync_request, we gather one chunk worth of
4362 * destination stripes and flag them as expanding.
4363 * Then we find all the source stripes and request reads.
4364 * As the reads complete, handle_stripe will copy the data
4365 * into the destination stripe and release that stripe.
4367 struct r5conf *conf = mddev->private;
4368 struct stripe_head *sh;
4369 sector_t first_sector, last_sector;
4370 int raid_disks = conf->previous_raid_disks;
4371 int data_disks = raid_disks - conf->max_degraded;
4372 int new_data_disks = conf->raid_disks - conf->max_degraded;
4375 sector_t writepos, readpos, safepos;
4376 sector_t stripe_addr;
4377 int reshape_sectors;
4378 struct list_head stripes;
4380 if (sector_nr == 0) {
4381 /* If restarting in the middle, skip the initial sectors */
4382 if (mddev->reshape_backwards &&
4383 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4384 sector_nr = raid5_size(mddev, 0, 0)
4385 - conf->reshape_progress;
4386 } else if (!mddev->reshape_backwards &&
4387 conf->reshape_progress > 0)
4388 sector_nr = conf->reshape_progress;
4389 sector_div(sector_nr, new_data_disks);
4391 mddev->curr_resync_completed = sector_nr;
4392 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4398 /* We need to process a full chunk at a time.
4399 * If old and new chunk sizes differ, we need to process the
4402 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4403 reshape_sectors = mddev->new_chunk_sectors;
4405 reshape_sectors = mddev->chunk_sectors;
4407 /* We update the metadata at least every 10 seconds, or when
4408 * the data about to be copied would over-write the source of
4409 * the data at the front of the range. i.e. one new_stripe
4410 * along from reshape_progress new_maps to after where
4411 * reshape_safe old_maps to
4413 writepos = conf->reshape_progress;
4414 sector_div(writepos, new_data_disks);
4415 readpos = conf->reshape_progress;
4416 sector_div(readpos, data_disks);
4417 safepos = conf->reshape_safe;
4418 sector_div(safepos, data_disks);
4419 if (mddev->reshape_backwards) {
4420 writepos -= min_t(sector_t, reshape_sectors, writepos);
4421 readpos += reshape_sectors;
4422 safepos += reshape_sectors;
4424 writepos += reshape_sectors;
4425 readpos -= min_t(sector_t, reshape_sectors, readpos);
4426 safepos -= min_t(sector_t, reshape_sectors, safepos);
4429 /* Having calculated the 'writepos' possibly use it
4430 * to set 'stripe_addr' which is where we will write to.
4432 if (mddev->reshape_backwards) {
4433 BUG_ON(conf->reshape_progress == 0);
4434 stripe_addr = writepos;
4435 BUG_ON((mddev->dev_sectors &
4436 ~((sector_t)reshape_sectors - 1))
4437 - reshape_sectors - stripe_addr
4440 BUG_ON(writepos != sector_nr + reshape_sectors);
4441 stripe_addr = sector_nr;
4444 /* 'writepos' is the most advanced device address we might write.
4445 * 'readpos' is the least advanced device address we might read.
4446 * 'safepos' is the least address recorded in the metadata as having
4448 * If there is a min_offset_diff, these are adjusted either by
4449 * increasing the safepos/readpos if diff is negative, or
4450 * increasing writepos if diff is positive.
4451 * If 'readpos' is then behind 'writepos', there is no way that we can
4452 * ensure safety in the face of a crash - that must be done by userspace
4453 * making a backup of the data. So in that case there is no particular
4454 * rush to update metadata.
4455 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4456 * update the metadata to advance 'safepos' to match 'readpos' so that
4457 * we can be safe in the event of a crash.
4458 * So we insist on updating metadata if safepos is behind writepos and
4459 * readpos is beyond writepos.
4460 * In any case, update the metadata every 10 seconds.
4461 * Maybe that number should be configurable, but I'm not sure it is
4462 * worth it.... maybe it could be a multiple of safemode_delay???
4464 if (conf->min_offset_diff < 0) {
4465 safepos += -conf->min_offset_diff;
4466 readpos += -conf->min_offset_diff;
4468 writepos += conf->min_offset_diff;
4470 if ((mddev->reshape_backwards
4471 ? (safepos > writepos && readpos < writepos)
4472 : (safepos < writepos && readpos > writepos)) ||
4473 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4474 /* Cannot proceed until we've updated the superblock... */
4475 wait_event(conf->wait_for_overlap,
4476 atomic_read(&conf->reshape_stripes)==0);
4477 mddev->reshape_position = conf->reshape_progress;
4478 mddev->curr_resync_completed = sector_nr;
4479 conf->reshape_checkpoint = jiffies;
4480 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4481 md_wakeup_thread(mddev->thread);
4482 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4483 kthread_should_stop());
4484 spin_lock_irq(&conf->device_lock);
4485 conf->reshape_safe = mddev->reshape_position;
4486 spin_unlock_irq(&conf->device_lock);
4487 wake_up(&conf->wait_for_overlap);
4488 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4491 INIT_LIST_HEAD(&stripes);
4492 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
4494 int skipped_disk = 0;
4495 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
4496 set_bit(STRIPE_EXPANDING, &sh->state);
4497 atomic_inc(&conf->reshape_stripes);
4498 /* If any of this stripe is beyond the end of the old
4499 * array, then we need to zero those blocks
4501 for (j=sh->disks; j--;) {
4503 if (j == sh->pd_idx)
4505 if (conf->level == 6 &&
4508 s = compute_blocknr(sh, j, 0);
4509 if (s < raid5_size(mddev, 0, 0)) {
4513 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4514 set_bit(R5_Expanded, &sh->dev[j].flags);
4515 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4517 if (!skipped_disk) {
4518 set_bit(STRIPE_EXPAND_READY, &sh->state);
4519 set_bit(STRIPE_HANDLE, &sh->state);
4521 list_add(&sh->lru, &stripes);
4523 spin_lock_irq(&conf->device_lock);
4524 if (mddev->reshape_backwards)
4525 conf->reshape_progress -= reshape_sectors * new_data_disks;
4527 conf->reshape_progress += reshape_sectors * new_data_disks;
4528 spin_unlock_irq(&conf->device_lock);
4529 /* Ok, those stripe are ready. We can start scheduling
4530 * reads on the source stripes.
4531 * The source stripes are determined by mapping the first and last
4532 * block on the destination stripes.
4535 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
4538 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
4539 * new_data_disks - 1),
4541 if (last_sector >= mddev->dev_sectors)
4542 last_sector = mddev->dev_sectors - 1;
4543 while (first_sector <= last_sector) {
4544 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
4545 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4546 set_bit(STRIPE_HANDLE, &sh->state);
4548 first_sector += STRIPE_SECTORS;
4550 /* Now that the sources are clearly marked, we can release
4551 * the destination stripes
4553 while (!list_empty(&stripes)) {
4554 sh = list_entry(stripes.next, struct stripe_head, lru);
4555 list_del_init(&sh->lru);
4558 /* If this takes us to the resync_max point where we have to pause,
4559 * then we need to write out the superblock.
4561 sector_nr += reshape_sectors;
4562 if ((sector_nr - mddev->curr_resync_completed) * 2
4563 >= mddev->resync_max - mddev->curr_resync_completed) {
4564 /* Cannot proceed until we've updated the superblock... */
4565 wait_event(conf->wait_for_overlap,
4566 atomic_read(&conf->reshape_stripes) == 0);
4567 mddev->reshape_position = conf->reshape_progress;
4568 mddev->curr_resync_completed = sector_nr;
4569 conf->reshape_checkpoint = jiffies;
4570 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4571 md_wakeup_thread(mddev->thread);
4572 wait_event(mddev->sb_wait,
4573 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4574 || kthread_should_stop());
4575 spin_lock_irq(&conf->device_lock);
4576 conf->reshape_safe = mddev->reshape_position;
4577 spin_unlock_irq(&conf->device_lock);
4578 wake_up(&conf->wait_for_overlap);
4579 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4581 return reshape_sectors;
4584 /* FIXME go_faster isn't used */
4585 static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
4587 struct r5conf *conf = mddev->private;
4588 struct stripe_head *sh;
4589 sector_t max_sector = mddev->dev_sectors;
4590 sector_t sync_blocks;
4591 int still_degraded = 0;
4594 if (sector_nr >= max_sector) {
4595 /* just being told to finish up .. nothing much to do */
4597 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4602 if (mddev->curr_resync < max_sector) /* aborted */
4603 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4605 else /* completed sync */
4607 bitmap_close_sync(mddev->bitmap);
4612 /* Allow raid5_quiesce to complete */
4613 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4615 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4616 return reshape_request(mddev, sector_nr, skipped);
4618 /* No need to check resync_max as we never do more than one
4619 * stripe, and as resync_max will always be on a chunk boundary,
4620 * if the check in md_do_sync didn't fire, there is no chance
4621 * of overstepping resync_max here
4624 /* if there is too many failed drives and we are trying
4625 * to resync, then assert that we are finished, because there is
4626 * nothing we can do.
4628 if (mddev->degraded >= conf->max_degraded &&
4629 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4630 sector_t rv = mddev->dev_sectors - sector_nr;
4634 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4635 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4636 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4637 /* we can skip this block, and probably more */
4638 sync_blocks /= STRIPE_SECTORS;
4640 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4643 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4645 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
4647 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
4648 /* make sure we don't swamp the stripe cache if someone else
4649 * is trying to get access
4651 schedule_timeout_uninterruptible(1);
4653 /* Need to check if array will still be degraded after recovery/resync
4654 * We don't need to check the 'failed' flag as when that gets set,
4657 for (i = 0; i < conf->raid_disks; i++)
4658 if (conf->disks[i].rdev == NULL)
4661 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4663 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
4668 return STRIPE_SECTORS;
4671 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
4673 /* We may not be able to submit a whole bio at once as there
4674 * may not be enough stripe_heads available.
4675 * We cannot pre-allocate enough stripe_heads as we may need
4676 * more than exist in the cache (if we allow ever large chunks).
4677 * So we do one stripe head at a time and record in
4678 * ->bi_hw_segments how many have been done.
4680 * We *know* that this entire raid_bio is in one chunk, so
4681 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4683 struct stripe_head *sh;
4685 sector_t sector, logical_sector, last_sector;
4690 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4691 sector = raid5_compute_sector(conf, logical_sector,
4693 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4695 for (; logical_sector < last_sector;
4696 logical_sector += STRIPE_SECTORS,
4697 sector += STRIPE_SECTORS,
4700 if (scnt < raid5_bi_processed_stripes(raid_bio))
4701 /* already done this stripe */
4704 sh = get_active_stripe(conf, sector, 0, 1, 0);
4707 /* failed to get a stripe - must wait */
4708 raid5_set_bi_processed_stripes(raid_bio, scnt);
4709 conf->retry_read_aligned = raid_bio;
4713 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4715 raid5_set_bi_processed_stripes(raid_bio, scnt);
4716 conf->retry_read_aligned = raid_bio;
4720 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
4725 remaining = raid5_dec_bi_active_stripes(raid_bio);
4727 bio_endio(raid_bio, 0);
4728 if (atomic_dec_and_test(&conf->active_aligned_reads))
4729 wake_up(&conf->wait_for_stripe);
4733 #define MAX_STRIPE_BATCH 8
4734 static int handle_active_stripes(struct r5conf *conf)
4736 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4737 int i, batch_size = 0;
4739 while (batch_size < MAX_STRIPE_BATCH &&
4740 (sh = __get_priority_stripe(conf)) != NULL)
4741 batch[batch_size++] = sh;
4743 if (batch_size == 0)
4745 spin_unlock_irq(&conf->device_lock);
4747 for (i = 0; i < batch_size; i++)
4748 handle_stripe(batch[i]);
4752 spin_lock_irq(&conf->device_lock);
4753 for (i = 0; i < batch_size; i++)
4754 __release_stripe(conf, batch[i]);
4759 * This is our raid5 kernel thread.
4761 * We scan the hash table for stripes which can be handled now.
4762 * During the scan, completed stripes are saved for us by the interrupt
4763 * handler, so that they will not have to wait for our next wakeup.
4765 static void raid5d(struct md_thread *thread)
4767 struct mddev *mddev = thread->mddev;
4768 struct r5conf *conf = mddev->private;
4770 struct blk_plug plug;
4772 pr_debug("+++ raid5d active\n");
4774 md_check_recovery(mddev);
4776 blk_start_plug(&plug);
4778 spin_lock_irq(&conf->device_lock);
4784 !list_empty(&conf->bitmap_list)) {
4785 /* Now is a good time to flush some bitmap updates */
4787 spin_unlock_irq(&conf->device_lock);
4788 bitmap_unplug(mddev->bitmap);
4789 spin_lock_irq(&conf->device_lock);
4790 conf->seq_write = conf->seq_flush;
4791 activate_bit_delay(conf);
4793 raid5_activate_delayed(conf);
4795 while ((bio = remove_bio_from_retry(conf))) {
4797 spin_unlock_irq(&conf->device_lock);
4798 ok = retry_aligned_read(conf, bio);
4799 spin_lock_irq(&conf->device_lock);
4805 batch_size = handle_active_stripes(conf);
4808 handled += batch_size;
4810 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4811 spin_unlock_irq(&conf->device_lock);
4812 md_check_recovery(mddev);
4813 spin_lock_irq(&conf->device_lock);
4816 pr_debug("%d stripes handled\n", handled);
4818 spin_unlock_irq(&conf->device_lock);
4820 async_tx_issue_pending_all();
4821 blk_finish_plug(&plug);
4823 pr_debug("--- raid5d inactive\n");
4827 raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
4829 struct r5conf *conf = mddev->private;
4831 return sprintf(page, "%d\n", conf->max_nr_stripes);
4837 raid5_set_cache_size(struct mddev *mddev, int size)
4839 struct r5conf *conf = mddev->private;
4842 if (size <= 16 || size > 32768)
4844 while (size < conf->max_nr_stripes) {
4845 if (drop_one_stripe(conf))
4846 conf->max_nr_stripes--;
4850 err = md_allow_write(mddev);
4853 while (size > conf->max_nr_stripes) {
4854 if (grow_one_stripe(conf))
4855 conf->max_nr_stripes++;
4860 EXPORT_SYMBOL(raid5_set_cache_size);
4863 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
4865 struct r5conf *conf = mddev->private;
4869 if (len >= PAGE_SIZE)
4874 if (strict_strtoul(page, 10, &new))
4876 err = raid5_set_cache_size(mddev, new);
4882 static struct md_sysfs_entry
4883 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4884 raid5_show_stripe_cache_size,
4885 raid5_store_stripe_cache_size);
4888 raid5_show_preread_threshold(struct mddev *mddev, char *page)
4890 struct r5conf *conf = mddev->private;
4892 return sprintf(page, "%d\n", conf->bypass_threshold);
4898 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
4900 struct r5conf *conf = mddev->private;
4902 if (len >= PAGE_SIZE)
4907 if (strict_strtoul(page, 10, &new))
4909 if (new > conf->max_nr_stripes)
4911 conf->bypass_threshold = new;
4915 static struct md_sysfs_entry
4916 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4918 raid5_show_preread_threshold,
4919 raid5_store_preread_threshold);
4922 stripe_cache_active_show(struct mddev *mddev, char *page)
4924 struct r5conf *conf = mddev->private;
4926 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4931 static struct md_sysfs_entry
4932 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
4934 static struct attribute *raid5_attrs[] = {
4935 &raid5_stripecache_size.attr,
4936 &raid5_stripecache_active.attr,
4937 &raid5_preread_bypass_threshold.attr,
4940 static struct attribute_group raid5_attrs_group = {
4942 .attrs = raid5_attrs,
4946 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
4948 struct r5conf *conf = mddev->private;
4951 sectors = mddev->dev_sectors;
4953 /* size is defined by the smallest of previous and new size */
4954 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
4956 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
4957 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
4958 return sectors * (raid_disks - conf->max_degraded);
4961 static void raid5_free_percpu(struct r5conf *conf)
4963 struct raid5_percpu *percpu;
4970 for_each_possible_cpu(cpu) {
4971 percpu = per_cpu_ptr(conf->percpu, cpu);
4972 safe_put_page(percpu->spare_page);
4973 kfree(percpu->scribble);
4975 #ifdef CONFIG_HOTPLUG_CPU
4976 unregister_cpu_notifier(&conf->cpu_notify);
4980 free_percpu(conf->percpu);
4983 static void free_conf(struct r5conf *conf)
4985 shrink_stripes(conf);
4986 raid5_free_percpu(conf);
4988 kfree(conf->stripe_hashtbl);
4992 #ifdef CONFIG_HOTPLUG_CPU
4993 static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4996 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
4997 long cpu = (long)hcpu;
4998 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5001 case CPU_UP_PREPARE:
5002 case CPU_UP_PREPARE_FROZEN:
5003 if (conf->level == 6 && !percpu->spare_page)
5004 percpu->spare_page = alloc_page(GFP_KERNEL);
5005 if (!percpu->scribble)
5006 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5008 if (!percpu->scribble ||
5009 (conf->level == 6 && !percpu->spare_page)) {
5010 safe_put_page(percpu->spare_page);
5011 kfree(percpu->scribble);
5012 pr_err("%s: failed memory allocation for cpu%ld\n",
5014 return notifier_from_errno(-ENOMEM);
5018 case CPU_DEAD_FROZEN:
5019 safe_put_page(percpu->spare_page);
5020 kfree(percpu->scribble);
5021 percpu->spare_page = NULL;
5022 percpu->scribble = NULL;
5031 static int raid5_alloc_percpu(struct r5conf *conf)
5034 struct page *spare_page;
5035 struct raid5_percpu __percpu *allcpus;
5039 allcpus = alloc_percpu(struct raid5_percpu);
5042 conf->percpu = allcpus;
5046 for_each_present_cpu(cpu) {
5047 if (conf->level == 6) {
5048 spare_page = alloc_page(GFP_KERNEL);
5053 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5055 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5060 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
5062 #ifdef CONFIG_HOTPLUG_CPU
5063 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5064 conf->cpu_notify.priority = 0;
5066 err = register_cpu_notifier(&conf->cpu_notify);
5073 static struct r5conf *setup_conf(struct mddev *mddev)
5075 struct r5conf *conf;
5076 int raid_disk, memory, max_disks;
5077 struct md_rdev *rdev;
5078 struct disk_info *disk;
5081 if (mddev->new_level != 5
5082 && mddev->new_level != 4
5083 && mddev->new_level != 6) {
5084 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
5085 mdname(mddev), mddev->new_level);
5086 return ERR_PTR(-EIO);
5088 if ((mddev->new_level == 5
5089 && !algorithm_valid_raid5(mddev->new_layout)) ||
5090 (mddev->new_level == 6
5091 && !algorithm_valid_raid6(mddev->new_layout))) {
5092 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
5093 mdname(mddev), mddev->new_layout);
5094 return ERR_PTR(-EIO);
5096 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
5097 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
5098 mdname(mddev), mddev->raid_disks);
5099 return ERR_PTR(-EINVAL);
5102 if (!mddev->new_chunk_sectors ||
5103 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5104 !is_power_of_2(mddev->new_chunk_sectors)) {
5105 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5106 mdname(mddev), mddev->new_chunk_sectors << 9);
5107 return ERR_PTR(-EINVAL);
5110 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
5113 spin_lock_init(&conf->device_lock);
5114 init_waitqueue_head(&conf->wait_for_stripe);
5115 init_waitqueue_head(&conf->wait_for_overlap);
5116 INIT_LIST_HEAD(&conf->handle_list);
5117 INIT_LIST_HEAD(&conf->hold_list);
5118 INIT_LIST_HEAD(&conf->delayed_list);
5119 INIT_LIST_HEAD(&conf->bitmap_list);
5120 INIT_LIST_HEAD(&conf->inactive_list);
5121 atomic_set(&conf->active_stripes, 0);
5122 atomic_set(&conf->preread_active_stripes, 0);
5123 atomic_set(&conf->active_aligned_reads, 0);
5124 conf->bypass_threshold = BYPASS_THRESHOLD;
5125 conf->recovery_disabled = mddev->recovery_disabled - 1;
5127 conf->raid_disks = mddev->raid_disks;
5128 if (mddev->reshape_position == MaxSector)
5129 conf->previous_raid_disks = mddev->raid_disks;
5131 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5132 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5133 conf->scribble_len = scribble_len(max_disks);
5135 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
5140 conf->mddev = mddev;
5142 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5145 conf->level = mddev->new_level;
5146 if (raid5_alloc_percpu(conf) != 0)
5149 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
5151 rdev_for_each(rdev, mddev) {
5152 raid_disk = rdev->raid_disk;
5153 if (raid_disk >= max_disks
5156 disk = conf->disks + raid_disk;
5158 if (test_bit(Replacement, &rdev->flags)) {
5159 if (disk->replacement)
5161 disk->replacement = rdev;
5168 if (test_bit(In_sync, &rdev->flags)) {
5169 char b[BDEVNAME_SIZE];
5170 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5172 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
5173 } else if (rdev->saved_raid_disk != raid_disk)
5174 /* Cannot rely on bitmap to complete recovery */
5178 conf->chunk_sectors = mddev->new_chunk_sectors;
5179 conf->level = mddev->new_level;
5180 if (conf->level == 6)
5181 conf->max_degraded = 2;
5183 conf->max_degraded = 1;
5184 conf->algorithm = mddev->new_layout;
5185 conf->max_nr_stripes = NR_STRIPES;
5186 conf->reshape_progress = mddev->reshape_position;
5187 if (conf->reshape_progress != MaxSector) {
5188 conf->prev_chunk_sectors = mddev->chunk_sectors;
5189 conf->prev_algo = mddev->layout;
5192 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
5193 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
5194 if (grow_stripes(conf, conf->max_nr_stripes)) {
5196 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5197 mdname(mddev), memory);
5200 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5201 mdname(mddev), memory);
5203 sprintf(pers_name, "raid%d", mddev->new_level);
5204 conf->thread = md_register_thread(raid5d, mddev, pers_name);
5205 if (!conf->thread) {
5207 "md/raid:%s: couldn't allocate thread.\n",
5217 return ERR_PTR(-EIO);
5219 return ERR_PTR(-ENOMEM);
5223 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5226 case ALGORITHM_PARITY_0:
5227 if (raid_disk < max_degraded)
5230 case ALGORITHM_PARITY_N:
5231 if (raid_disk >= raid_disks - max_degraded)
5234 case ALGORITHM_PARITY_0_6:
5235 if (raid_disk == 0 ||
5236 raid_disk == raid_disks - 1)
5239 case ALGORITHM_LEFT_ASYMMETRIC_6:
5240 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5241 case ALGORITHM_LEFT_SYMMETRIC_6:
5242 case ALGORITHM_RIGHT_SYMMETRIC_6:
5243 if (raid_disk == raid_disks - 1)
5249 static int run(struct mddev *mddev)
5251 struct r5conf *conf;
5252 int working_disks = 0;
5253 int dirty_parity_disks = 0;
5254 struct md_rdev *rdev;
5255 sector_t reshape_offset = 0;
5257 long long min_offset_diff = 0;
5260 if (mddev->recovery_cp != MaxSector)
5261 printk(KERN_NOTICE "md/raid:%s: not clean"
5262 " -- starting background reconstruction\n",
5265 rdev_for_each(rdev, mddev) {
5267 if (rdev->raid_disk < 0)
5269 diff = (rdev->new_data_offset - rdev->data_offset);
5271 min_offset_diff = diff;
5273 } else if (mddev->reshape_backwards &&
5274 diff < min_offset_diff)
5275 min_offset_diff = diff;
5276 else if (!mddev->reshape_backwards &&
5277 diff > min_offset_diff)
5278 min_offset_diff = diff;
5281 if (mddev->reshape_position != MaxSector) {
5282 /* Check that we can continue the reshape.
5283 * Difficulties arise if the stripe we would write to
5284 * next is at or after the stripe we would read from next.
5285 * For a reshape that changes the number of devices, this
5286 * is only possible for a very short time, and mdadm makes
5287 * sure that time appears to have past before assembling
5288 * the array. So we fail if that time hasn't passed.
5289 * For a reshape that keeps the number of devices the same
5290 * mdadm must be monitoring the reshape can keeping the
5291 * critical areas read-only and backed up. It will start
5292 * the array in read-only mode, so we check for that.
5294 sector_t here_new, here_old;
5296 int max_degraded = (mddev->level == 6 ? 2 : 1);
5298 if (mddev->new_level != mddev->level) {
5299 printk(KERN_ERR "md/raid:%s: unsupported reshape "
5300 "required - aborting.\n",
5304 old_disks = mddev->raid_disks - mddev->delta_disks;
5305 /* reshape_position must be on a new-stripe boundary, and one
5306 * further up in new geometry must map after here in old
5309 here_new = mddev->reshape_position;
5310 if (sector_div(here_new, mddev->new_chunk_sectors *
5311 (mddev->raid_disks - max_degraded))) {
5312 printk(KERN_ERR "md/raid:%s: reshape_position not "
5313 "on a stripe boundary\n", mdname(mddev));
5316 reshape_offset = here_new * mddev->new_chunk_sectors;
5317 /* here_new is the stripe we will write to */
5318 here_old = mddev->reshape_position;
5319 sector_div(here_old, mddev->chunk_sectors *
5320 (old_disks-max_degraded));
5321 /* here_old is the first stripe that we might need to read
5323 if (mddev->delta_disks == 0) {
5324 if ((here_new * mddev->new_chunk_sectors !=
5325 here_old * mddev->chunk_sectors)) {
5326 printk(KERN_ERR "md/raid:%s: reshape position is"
5327 " confused - aborting\n", mdname(mddev));
5330 /* We cannot be sure it is safe to start an in-place
5331 * reshape. It is only safe if user-space is monitoring
5332 * and taking constant backups.
5333 * mdadm always starts a situation like this in
5334 * readonly mode so it can take control before
5335 * allowing any writes. So just check for that.
5337 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5338 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5339 /* not really in-place - so OK */;
5340 else if (mddev->ro == 0) {
5341 printk(KERN_ERR "md/raid:%s: in-place reshape "
5342 "must be started in read-only mode "
5347 } else if (mddev->reshape_backwards
5348 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
5349 here_old * mddev->chunk_sectors)
5350 : (here_new * mddev->new_chunk_sectors >=
5351 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
5352 /* Reading from the same stripe as writing to - bad */
5353 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5354 "auto-recovery - aborting.\n",
5358 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5360 /* OK, we should be able to continue; */
5362 BUG_ON(mddev->level != mddev->new_level);
5363 BUG_ON(mddev->layout != mddev->new_layout);
5364 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
5365 BUG_ON(mddev->delta_disks != 0);
5368 if (mddev->private == NULL)
5369 conf = setup_conf(mddev);
5371 conf = mddev->private;
5374 return PTR_ERR(conf);
5376 conf->min_offset_diff = min_offset_diff;
5377 mddev->thread = conf->thread;
5378 conf->thread = NULL;
5379 mddev->private = conf;
5381 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5383 rdev = conf->disks[i].rdev;
5384 if (!rdev && conf->disks[i].replacement) {
5385 /* The replacement is all we have yet */
5386 rdev = conf->disks[i].replacement;
5387 conf->disks[i].replacement = NULL;
5388 clear_bit(Replacement, &rdev->flags);
5389 conf->disks[i].rdev = rdev;
5393 if (conf->disks[i].replacement &&
5394 conf->reshape_progress != MaxSector) {
5395 /* replacements and reshape simply do not mix. */
5396 printk(KERN_ERR "md: cannot handle concurrent "
5397 "replacement and reshape.\n");
5400 if (test_bit(In_sync, &rdev->flags)) {
5404 /* This disc is not fully in-sync. However if it
5405 * just stored parity (beyond the recovery_offset),
5406 * when we don't need to be concerned about the
5407 * array being dirty.
5408 * When reshape goes 'backwards', we never have
5409 * partially completed devices, so we only need
5410 * to worry about reshape going forwards.
5412 /* Hack because v0.91 doesn't store recovery_offset properly. */
5413 if (mddev->major_version == 0 &&
5414 mddev->minor_version > 90)
5415 rdev->recovery_offset = reshape_offset;
5417 if (rdev->recovery_offset < reshape_offset) {
5418 /* We need to check old and new layout */
5419 if (!only_parity(rdev->raid_disk,
5422 conf->max_degraded))
5425 if (!only_parity(rdev->raid_disk,
5427 conf->previous_raid_disks,
5428 conf->max_degraded))
5430 dirty_parity_disks++;
5434 * 0 for a fully functional array, 1 or 2 for a degraded array.
5436 mddev->degraded = calc_degraded(conf);
5438 if (has_failed(conf)) {
5439 printk(KERN_ERR "md/raid:%s: not enough operational devices"
5440 " (%d/%d failed)\n",
5441 mdname(mddev), mddev->degraded, conf->raid_disks);
5445 /* device size must be a multiple of chunk size */
5446 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
5447 mddev->resync_max_sectors = mddev->dev_sectors;
5449 if (mddev->degraded > dirty_parity_disks &&
5450 mddev->recovery_cp != MaxSector) {
5451 if (mddev->ok_start_degraded)
5453 "md/raid:%s: starting dirty degraded array"
5454 " - data corruption possible.\n",
5458 "md/raid:%s: cannot start dirty degraded array.\n",
5464 if (mddev->degraded == 0)
5465 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5466 " devices, algorithm %d\n", mdname(mddev), conf->level,
5467 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5470 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5471 " out of %d devices, algorithm %d\n",
5472 mdname(mddev), conf->level,
5473 mddev->raid_disks - mddev->degraded,
5474 mddev->raid_disks, mddev->new_layout);
5476 print_raid5_conf(conf);
5478 if (conf->reshape_progress != MaxSector) {
5479 conf->reshape_safe = conf->reshape_progress;
5480 atomic_set(&conf->reshape_stripes, 0);
5481 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5482 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5483 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5484 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5485 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5490 /* Ok, everything is just fine now */
5491 if (mddev->to_remove == &raid5_attrs_group)
5492 mddev->to_remove = NULL;
5493 else if (mddev->kobj.sd &&
5494 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5496 "raid5: failed to create sysfs attributes for %s\n",
5498 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5502 bool discard_supported = true;
5503 /* read-ahead size must cover two whole stripes, which
5504 * is 2 * (datadisks) * chunksize where 'n' is the
5505 * number of raid devices
5507 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5508 int stripe = data_disks *
5509 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5510 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5511 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5513 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
5515 mddev->queue->backing_dev_info.congested_data = mddev;
5516 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
5518 chunk_size = mddev->chunk_sectors << 9;
5519 blk_queue_io_min(mddev->queue, chunk_size);
5520 blk_queue_io_opt(mddev->queue, chunk_size *
5521 (conf->raid_disks - conf->max_degraded));
5523 * We can only discard a whole stripe. It doesn't make sense to
5524 * discard data disk but write parity disk
5526 stripe = stripe * PAGE_SIZE;
5527 mddev->queue->limits.discard_alignment = stripe;
5528 mddev->queue->limits.discard_granularity = stripe;
5530 * unaligned part of discard request will be ignored, so can't
5531 * guarantee discard_zerors_data
5533 mddev->queue->limits.discard_zeroes_data = 0;
5535 rdev_for_each(rdev, mddev) {
5536 disk_stack_limits(mddev->gendisk, rdev->bdev,
5537 rdev->data_offset << 9);
5538 disk_stack_limits(mddev->gendisk, rdev->bdev,
5539 rdev->new_data_offset << 9);
5541 * discard_zeroes_data is required, otherwise data
5542 * could be lost. Consider a scenario: discard a stripe
5543 * (the stripe could be inconsistent if
5544 * discard_zeroes_data is 0); write one disk of the
5545 * stripe (the stripe could be inconsistent again
5546 * depending on which disks are used to calculate
5547 * parity); the disk is broken; The stripe data of this
5550 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5551 !bdev_get_queue(rdev->bdev)->
5552 limits.discard_zeroes_data)
5553 discard_supported = false;
5556 if (discard_supported &&
5557 mddev->queue->limits.max_discard_sectors >= stripe &&
5558 mddev->queue->limits.discard_granularity >= stripe)
5559 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5562 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5568 md_unregister_thread(&mddev->thread);
5569 print_raid5_conf(conf);
5571 mddev->private = NULL;
5572 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
5576 static int stop(struct mddev *mddev)
5578 struct r5conf *conf = mddev->private;
5580 md_unregister_thread(&mddev->thread);
5582 mddev->queue->backing_dev_info.congested_fn = NULL;
5584 mddev->private = NULL;
5585 mddev->to_remove = &raid5_attrs_group;
5589 static void status(struct seq_file *seq, struct mddev *mddev)
5591 struct r5conf *conf = mddev->private;
5594 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5595 mddev->chunk_sectors / 2, mddev->layout);
5596 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5597 for (i = 0; i < conf->raid_disks; i++)
5598 seq_printf (seq, "%s",
5599 conf->disks[i].rdev &&
5600 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
5601 seq_printf (seq, "]");
5604 static void print_raid5_conf (struct r5conf *conf)
5607 struct disk_info *tmp;
5609 printk(KERN_DEBUG "RAID conf printout:\n");
5611 printk("(conf==NULL)\n");
5614 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5616 conf->raid_disks - conf->mddev->degraded);
5618 for (i = 0; i < conf->raid_disks; i++) {
5619 char b[BDEVNAME_SIZE];
5620 tmp = conf->disks + i;
5622 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5623 i, !test_bit(Faulty, &tmp->rdev->flags),
5624 bdevname(tmp->rdev->bdev, b));
5628 static int raid5_spare_active(struct mddev *mddev)
5631 struct r5conf *conf = mddev->private;
5632 struct disk_info *tmp;
5634 unsigned long flags;
5636 for (i = 0; i < conf->raid_disks; i++) {
5637 tmp = conf->disks + i;
5638 if (tmp->replacement
5639 && tmp->replacement->recovery_offset == MaxSector
5640 && !test_bit(Faulty, &tmp->replacement->flags)
5641 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5642 /* Replacement has just become active. */
5644 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5647 /* Replaced device not technically faulty,
5648 * but we need to be sure it gets removed
5649 * and never re-added.
5651 set_bit(Faulty, &tmp->rdev->flags);
5652 sysfs_notify_dirent_safe(
5653 tmp->rdev->sysfs_state);
5655 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5656 } else if (tmp->rdev
5657 && tmp->rdev->recovery_offset == MaxSector
5658 && !test_bit(Faulty, &tmp->rdev->flags)
5659 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5661 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
5664 spin_lock_irqsave(&conf->device_lock, flags);
5665 mddev->degraded = calc_degraded(conf);
5666 spin_unlock_irqrestore(&conf->device_lock, flags);
5667 print_raid5_conf(conf);
5671 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
5673 struct r5conf *conf = mddev->private;
5675 int number = rdev->raid_disk;
5676 struct md_rdev **rdevp;
5677 struct disk_info *p = conf->disks + number;
5679 print_raid5_conf(conf);
5680 if (rdev == p->rdev)
5682 else if (rdev == p->replacement)
5683 rdevp = &p->replacement;
5687 if (number >= conf->raid_disks &&
5688 conf->reshape_progress == MaxSector)
5689 clear_bit(In_sync, &rdev->flags);
5691 if (test_bit(In_sync, &rdev->flags) ||
5692 atomic_read(&rdev->nr_pending)) {
5696 /* Only remove non-faulty devices if recovery
5699 if (!test_bit(Faulty, &rdev->flags) &&
5700 mddev->recovery_disabled != conf->recovery_disabled &&
5701 !has_failed(conf) &&
5702 (!p->replacement || p->replacement == rdev) &&
5703 number < conf->raid_disks) {
5709 if (atomic_read(&rdev->nr_pending)) {
5710 /* lost the race, try later */
5713 } else if (p->replacement) {
5714 /* We must have just cleared 'rdev' */
5715 p->rdev = p->replacement;
5716 clear_bit(Replacement, &p->replacement->flags);
5717 smp_mb(); /* Make sure other CPUs may see both as identical
5718 * but will never see neither - if they are careful
5720 p->replacement = NULL;
5721 clear_bit(WantReplacement, &rdev->flags);
5723 /* We might have just removed the Replacement as faulty-
5724 * clear the bit just in case
5726 clear_bit(WantReplacement, &rdev->flags);
5729 print_raid5_conf(conf);
5733 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
5735 struct r5conf *conf = mddev->private;
5738 struct disk_info *p;
5740 int last = conf->raid_disks - 1;
5742 if (mddev->recovery_disabled == conf->recovery_disabled)
5745 if (rdev->saved_raid_disk < 0 && has_failed(conf))
5746 /* no point adding a device */
5749 if (rdev->raid_disk >= 0)
5750 first = last = rdev->raid_disk;
5753 * find the disk ... but prefer rdev->saved_raid_disk
5756 if (rdev->saved_raid_disk >= 0 &&
5757 rdev->saved_raid_disk >= first &&
5758 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5759 first = rdev->saved_raid_disk;
5761 for (disk = first; disk <= last; disk++) {
5762 p = conf->disks + disk;
5763 if (p->rdev == NULL) {
5764 clear_bit(In_sync, &rdev->flags);
5765 rdev->raid_disk = disk;
5767 if (rdev->saved_raid_disk != disk)
5769 rcu_assign_pointer(p->rdev, rdev);
5773 for (disk = first; disk <= last; disk++) {
5774 p = conf->disks + disk;
5775 if (test_bit(WantReplacement, &p->rdev->flags) &&
5776 p->replacement == NULL) {
5777 clear_bit(In_sync, &rdev->flags);
5778 set_bit(Replacement, &rdev->flags);
5779 rdev->raid_disk = disk;
5782 rcu_assign_pointer(p->replacement, rdev);
5787 print_raid5_conf(conf);
5791 static int raid5_resize(struct mddev *mddev, sector_t sectors)
5793 /* no resync is happening, and there is enough space
5794 * on all devices, so we can resize.
5795 * We need to make sure resync covers any new space.
5796 * If the array is shrinking we should possibly wait until
5797 * any io in the removed space completes, but it hardly seems
5801 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
5802 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5803 if (mddev->external_size &&
5804 mddev->array_sectors > newsize)
5806 if (mddev->bitmap) {
5807 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5811 md_set_array_sectors(mddev, newsize);
5812 set_capacity(mddev->gendisk, mddev->array_sectors);
5813 revalidate_disk(mddev->gendisk);
5814 if (sectors > mddev->dev_sectors &&
5815 mddev->recovery_cp > mddev->dev_sectors) {
5816 mddev->recovery_cp = mddev->dev_sectors;
5817 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5819 mddev->dev_sectors = sectors;
5820 mddev->resync_max_sectors = sectors;
5824 static int check_stripe_cache(struct mddev *mddev)
5826 /* Can only proceed if there are plenty of stripe_heads.
5827 * We need a minimum of one full stripe,, and for sensible progress
5828 * it is best to have about 4 times that.
5829 * If we require 4 times, then the default 256 4K stripe_heads will
5830 * allow for chunk sizes up to 256K, which is probably OK.
5831 * If the chunk size is greater, user-space should request more
5832 * stripe_heads first.
5834 struct r5conf *conf = mddev->private;
5835 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5836 > conf->max_nr_stripes ||
5837 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5838 > conf->max_nr_stripes) {
5839 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5841 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5848 static int check_reshape(struct mddev *mddev)
5850 struct r5conf *conf = mddev->private;
5852 if (mddev->delta_disks == 0 &&
5853 mddev->new_layout == mddev->layout &&
5854 mddev->new_chunk_sectors == mddev->chunk_sectors)
5855 return 0; /* nothing to do */
5856 if (has_failed(conf))
5858 if (mddev->delta_disks < 0) {
5859 /* We might be able to shrink, but the devices must
5860 * be made bigger first.
5861 * For raid6, 4 is the minimum size.
5862 * Otherwise 2 is the minimum
5865 if (mddev->level == 6)
5867 if (mddev->raid_disks + mddev->delta_disks < min)
5871 if (!check_stripe_cache(mddev))
5874 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
5877 static int raid5_start_reshape(struct mddev *mddev)
5879 struct r5conf *conf = mddev->private;
5880 struct md_rdev *rdev;
5882 unsigned long flags;
5884 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5887 if (!check_stripe_cache(mddev))
5890 if (has_failed(conf))
5893 rdev_for_each(rdev, mddev) {
5894 if (!test_bit(In_sync, &rdev->flags)
5895 && !test_bit(Faulty, &rdev->flags))
5899 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
5900 /* Not enough devices even to make a degraded array
5905 /* Refuse to reduce size of the array. Any reductions in
5906 * array size must be through explicit setting of array_size
5909 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5910 < mddev->array_sectors) {
5911 printk(KERN_ERR "md/raid:%s: array size must be reduced "
5912 "before number of disks\n", mdname(mddev));
5916 atomic_set(&conf->reshape_stripes, 0);
5917 spin_lock_irq(&conf->device_lock);
5918 conf->previous_raid_disks = conf->raid_disks;
5919 conf->raid_disks += mddev->delta_disks;
5920 conf->prev_chunk_sectors = conf->chunk_sectors;
5921 conf->chunk_sectors = mddev->new_chunk_sectors;
5922 conf->prev_algo = conf->algorithm;
5923 conf->algorithm = mddev->new_layout;
5925 /* Code that selects data_offset needs to see the generation update
5926 * if reshape_progress has been set - so a memory barrier needed.
5929 if (mddev->reshape_backwards)
5930 conf->reshape_progress = raid5_size(mddev, 0, 0);
5932 conf->reshape_progress = 0;
5933 conf->reshape_safe = conf->reshape_progress;
5934 spin_unlock_irq(&conf->device_lock);
5936 /* Add some new drives, as many as will fit.
5937 * We know there are enough to make the newly sized array work.
5938 * Don't add devices if we are reducing the number of
5939 * devices in the array. This is because it is not possible
5940 * to correctly record the "partially reconstructed" state of
5941 * such devices during the reshape and confusion could result.
5943 if (mddev->delta_disks >= 0) {
5944 rdev_for_each(rdev, mddev)
5945 if (rdev->raid_disk < 0 &&
5946 !test_bit(Faulty, &rdev->flags)) {
5947 if (raid5_add_disk(mddev, rdev) == 0) {
5949 >= conf->previous_raid_disks)
5950 set_bit(In_sync, &rdev->flags);
5952 rdev->recovery_offset = 0;
5954 if (sysfs_link_rdev(mddev, rdev))
5955 /* Failure here is OK */;
5957 } else if (rdev->raid_disk >= conf->previous_raid_disks
5958 && !test_bit(Faulty, &rdev->flags)) {
5959 /* This is a spare that was manually added */
5960 set_bit(In_sync, &rdev->flags);
5963 /* When a reshape changes the number of devices,
5964 * ->degraded is measured against the larger of the
5965 * pre and post number of devices.
5967 spin_lock_irqsave(&conf->device_lock, flags);
5968 mddev->degraded = calc_degraded(conf);
5969 spin_unlock_irqrestore(&conf->device_lock, flags);
5971 mddev->raid_disks = conf->raid_disks;
5972 mddev->reshape_position = conf->reshape_progress;
5973 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5975 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5976 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5977 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5978 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5979 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5981 if (!mddev->sync_thread) {
5982 mddev->recovery = 0;
5983 spin_lock_irq(&conf->device_lock);
5984 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
5985 rdev_for_each(rdev, mddev)
5986 rdev->new_data_offset = rdev->data_offset;
5988 conf->reshape_progress = MaxSector;
5989 mddev->reshape_position = MaxSector;
5990 spin_unlock_irq(&conf->device_lock);
5993 conf->reshape_checkpoint = jiffies;
5994 md_wakeup_thread(mddev->sync_thread);
5995 md_new_event(mddev);
5999 /* This is called from the reshape thread and should make any
6000 * changes needed in 'conf'
6002 static void end_reshape(struct r5conf *conf)
6005 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
6006 struct md_rdev *rdev;
6008 spin_lock_irq(&conf->device_lock);
6009 conf->previous_raid_disks = conf->raid_disks;
6010 rdev_for_each(rdev, conf->mddev)
6011 rdev->data_offset = rdev->new_data_offset;
6013 conf->reshape_progress = MaxSector;
6014 spin_unlock_irq(&conf->device_lock);
6015 wake_up(&conf->wait_for_overlap);
6017 /* read-ahead size must cover two whole stripes, which is
6018 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6020 if (conf->mddev->queue) {
6021 int data_disks = conf->raid_disks - conf->max_degraded;
6022 int stripe = data_disks * ((conf->chunk_sectors << 9)
6024 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6025 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6030 /* This is called from the raid5d thread with mddev_lock held.
6031 * It makes config changes to the device.
6033 static void raid5_finish_reshape(struct mddev *mddev)
6035 struct r5conf *conf = mddev->private;
6037 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6039 if (mddev->delta_disks > 0) {
6040 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6041 set_capacity(mddev->gendisk, mddev->array_sectors);
6042 revalidate_disk(mddev->gendisk);
6045 spin_lock_irq(&conf->device_lock);
6046 mddev->degraded = calc_degraded(conf);
6047 spin_unlock_irq(&conf->device_lock);
6048 for (d = conf->raid_disks ;
6049 d < conf->raid_disks - mddev->delta_disks;
6051 struct md_rdev *rdev = conf->disks[d].rdev;
6053 clear_bit(In_sync, &rdev->flags);
6054 rdev = conf->disks[d].replacement;
6056 clear_bit(In_sync, &rdev->flags);
6059 mddev->layout = conf->algorithm;
6060 mddev->chunk_sectors = conf->chunk_sectors;
6061 mddev->reshape_position = MaxSector;
6062 mddev->delta_disks = 0;
6063 mddev->reshape_backwards = 0;
6067 static void raid5_quiesce(struct mddev *mddev, int state)
6069 struct r5conf *conf = mddev->private;
6072 case 2: /* resume for a suspend */
6073 wake_up(&conf->wait_for_overlap);
6076 case 1: /* stop all writes */
6077 spin_lock_irq(&conf->device_lock);
6078 /* '2' tells resync/reshape to pause so that all
6079 * active stripes can drain
6082 wait_event_lock_irq(conf->wait_for_stripe,
6083 atomic_read(&conf->active_stripes) == 0 &&
6084 atomic_read(&conf->active_aligned_reads) == 0,
6085 conf->device_lock, /* nothing */);
6087 spin_unlock_irq(&conf->device_lock);
6088 /* allow reshape to continue */
6089 wake_up(&conf->wait_for_overlap);
6092 case 0: /* re-enable writes */
6093 spin_lock_irq(&conf->device_lock);
6095 wake_up(&conf->wait_for_stripe);
6096 wake_up(&conf->wait_for_overlap);
6097 spin_unlock_irq(&conf->device_lock);
6103 static void *raid45_takeover_raid0(struct mddev *mddev, int level)
6105 struct r0conf *raid0_conf = mddev->private;
6108 /* for raid0 takeover only one zone is supported */
6109 if (raid0_conf->nr_strip_zones > 1) {
6110 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6112 return ERR_PTR(-EINVAL);
6115 sectors = raid0_conf->strip_zone[0].zone_end;
6116 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
6117 mddev->dev_sectors = sectors;
6118 mddev->new_level = level;
6119 mddev->new_layout = ALGORITHM_PARITY_N;
6120 mddev->new_chunk_sectors = mddev->chunk_sectors;
6121 mddev->raid_disks += 1;
6122 mddev->delta_disks = 1;
6123 /* make sure it will be not marked as dirty */
6124 mddev->recovery_cp = MaxSector;
6126 return setup_conf(mddev);
6130 static void *raid5_takeover_raid1(struct mddev *mddev)
6134 if (mddev->raid_disks != 2 ||
6135 mddev->degraded > 1)
6136 return ERR_PTR(-EINVAL);
6138 /* Should check if there are write-behind devices? */
6140 chunksect = 64*2; /* 64K by default */
6142 /* The array must be an exact multiple of chunksize */
6143 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6146 if ((chunksect<<9) < STRIPE_SIZE)
6147 /* array size does not allow a suitable chunk size */
6148 return ERR_PTR(-EINVAL);
6150 mddev->new_level = 5;
6151 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
6152 mddev->new_chunk_sectors = chunksect;
6154 return setup_conf(mddev);
6157 static void *raid5_takeover_raid6(struct mddev *mddev)
6161 switch (mddev->layout) {
6162 case ALGORITHM_LEFT_ASYMMETRIC_6:
6163 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6165 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6166 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6168 case ALGORITHM_LEFT_SYMMETRIC_6:
6169 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6171 case ALGORITHM_RIGHT_SYMMETRIC_6:
6172 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6174 case ALGORITHM_PARITY_0_6:
6175 new_layout = ALGORITHM_PARITY_0;
6177 case ALGORITHM_PARITY_N:
6178 new_layout = ALGORITHM_PARITY_N;
6181 return ERR_PTR(-EINVAL);
6183 mddev->new_level = 5;
6184 mddev->new_layout = new_layout;
6185 mddev->delta_disks = -1;
6186 mddev->raid_disks -= 1;
6187 return setup_conf(mddev);
6191 static int raid5_check_reshape(struct mddev *mddev)
6193 /* For a 2-drive array, the layout and chunk size can be changed
6194 * immediately as not restriping is needed.
6195 * For larger arrays we record the new value - after validation
6196 * to be used by a reshape pass.
6198 struct r5conf *conf = mddev->private;
6199 int new_chunk = mddev->new_chunk_sectors;
6201 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
6203 if (new_chunk > 0) {
6204 if (!is_power_of_2(new_chunk))
6206 if (new_chunk < (PAGE_SIZE>>9))
6208 if (mddev->array_sectors & (new_chunk-1))
6209 /* not factor of array size */
6213 /* They look valid */
6215 if (mddev->raid_disks == 2) {
6216 /* can make the change immediately */
6217 if (mddev->new_layout >= 0) {
6218 conf->algorithm = mddev->new_layout;
6219 mddev->layout = mddev->new_layout;
6221 if (new_chunk > 0) {
6222 conf->chunk_sectors = new_chunk ;
6223 mddev->chunk_sectors = new_chunk;
6225 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6226 md_wakeup_thread(mddev->thread);
6228 return check_reshape(mddev);
6231 static int raid6_check_reshape(struct mddev *mddev)
6233 int new_chunk = mddev->new_chunk_sectors;
6235 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
6237 if (new_chunk > 0) {
6238 if (!is_power_of_2(new_chunk))
6240 if (new_chunk < (PAGE_SIZE >> 9))
6242 if (mddev->array_sectors & (new_chunk-1))
6243 /* not factor of array size */
6247 /* They look valid */
6248 return check_reshape(mddev);
6251 static void *raid5_takeover(struct mddev *mddev)
6253 /* raid5 can take over:
6254 * raid0 - if there is only one strip zone - make it a raid4 layout
6255 * raid1 - if there are two drives. We need to know the chunk size
6256 * raid4 - trivial - just use a raid4 layout.
6257 * raid6 - Providing it is a *_6 layout
6259 if (mddev->level == 0)
6260 return raid45_takeover_raid0(mddev, 5);
6261 if (mddev->level == 1)
6262 return raid5_takeover_raid1(mddev);
6263 if (mddev->level == 4) {
6264 mddev->new_layout = ALGORITHM_PARITY_N;
6265 mddev->new_level = 5;
6266 return setup_conf(mddev);
6268 if (mddev->level == 6)
6269 return raid5_takeover_raid6(mddev);
6271 return ERR_PTR(-EINVAL);
6274 static void *raid4_takeover(struct mddev *mddev)
6276 /* raid4 can take over:
6277 * raid0 - if there is only one strip zone
6278 * raid5 - if layout is right
6280 if (mddev->level == 0)
6281 return raid45_takeover_raid0(mddev, 4);
6282 if (mddev->level == 5 &&
6283 mddev->layout == ALGORITHM_PARITY_N) {
6284 mddev->new_layout = 0;
6285 mddev->new_level = 4;
6286 return setup_conf(mddev);
6288 return ERR_PTR(-EINVAL);
6291 static struct md_personality raid5_personality;
6293 static void *raid6_takeover(struct mddev *mddev)
6295 /* Currently can only take over a raid5. We map the
6296 * personality to an equivalent raid6 personality
6297 * with the Q block at the end.
6301 if (mddev->pers != &raid5_personality)
6302 return ERR_PTR(-EINVAL);
6303 if (mddev->degraded > 1)
6304 return ERR_PTR(-EINVAL);
6305 if (mddev->raid_disks > 253)
6306 return ERR_PTR(-EINVAL);
6307 if (mddev->raid_disks < 3)
6308 return ERR_PTR(-EINVAL);
6310 switch (mddev->layout) {
6311 case ALGORITHM_LEFT_ASYMMETRIC:
6312 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6314 case ALGORITHM_RIGHT_ASYMMETRIC:
6315 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6317 case ALGORITHM_LEFT_SYMMETRIC:
6318 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6320 case ALGORITHM_RIGHT_SYMMETRIC:
6321 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6323 case ALGORITHM_PARITY_0:
6324 new_layout = ALGORITHM_PARITY_0_6;
6326 case ALGORITHM_PARITY_N:
6327 new_layout = ALGORITHM_PARITY_N;
6330 return ERR_PTR(-EINVAL);
6332 mddev->new_level = 6;
6333 mddev->new_layout = new_layout;
6334 mddev->delta_disks = 1;
6335 mddev->raid_disks += 1;
6336 return setup_conf(mddev);
6340 static struct md_personality raid6_personality =
6344 .owner = THIS_MODULE,
6345 .make_request = make_request,
6349 .error_handler = error,
6350 .hot_add_disk = raid5_add_disk,
6351 .hot_remove_disk= raid5_remove_disk,
6352 .spare_active = raid5_spare_active,
6353 .sync_request = sync_request,
6354 .resize = raid5_resize,
6356 .check_reshape = raid6_check_reshape,
6357 .start_reshape = raid5_start_reshape,
6358 .finish_reshape = raid5_finish_reshape,
6359 .quiesce = raid5_quiesce,
6360 .takeover = raid6_takeover,
6362 static struct md_personality raid5_personality =
6366 .owner = THIS_MODULE,
6367 .make_request = make_request,
6371 .error_handler = error,
6372 .hot_add_disk = raid5_add_disk,
6373 .hot_remove_disk= raid5_remove_disk,
6374 .spare_active = raid5_spare_active,
6375 .sync_request = sync_request,
6376 .resize = raid5_resize,
6378 .check_reshape = raid5_check_reshape,
6379 .start_reshape = raid5_start_reshape,
6380 .finish_reshape = raid5_finish_reshape,
6381 .quiesce = raid5_quiesce,
6382 .takeover = raid5_takeover,
6385 static struct md_personality raid4_personality =
6389 .owner = THIS_MODULE,
6390 .make_request = make_request,
6394 .error_handler = error,
6395 .hot_add_disk = raid5_add_disk,
6396 .hot_remove_disk= raid5_remove_disk,
6397 .spare_active = raid5_spare_active,
6398 .sync_request = sync_request,
6399 .resize = raid5_resize,
6401 .check_reshape = raid5_check_reshape,
6402 .start_reshape = raid5_start_reshape,
6403 .finish_reshape = raid5_finish_reshape,
6404 .quiesce = raid5_quiesce,
6405 .takeover = raid4_takeover,
6408 static int __init raid5_init(void)
6410 register_md_personality(&raid6_personality);
6411 register_md_personality(&raid5_personality);
6412 register_md_personality(&raid4_personality);
6416 static void raid5_exit(void)
6418 unregister_md_personality(&raid6_personality);
6419 unregister_md_personality(&raid5_personality);
6420 unregister_md_personality(&raid4_personality);
6423 module_init(raid5_init);
6424 module_exit(raid5_exit);
6425 MODULE_LICENSE("GPL");
6426 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6427 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6428 MODULE_ALIAS("md-raid5");
6429 MODULE_ALIAS("md-raid4");
6430 MODULE_ALIAS("md-level-5");
6431 MODULE_ALIAS("md-level-4");
6432 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6433 MODULE_ALIAS("md-raid6");
6434 MODULE_ALIAS("md-level-6");
6436 /* This used to be two separate modules, they were: */
6437 MODULE_ALIAS("raid5");
6438 MODULE_ALIAS("raid6");