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>
56 #include <trace/events/block.h>
63 static bool devices_handle_discard_safely = false;
64 module_param(devices_handle_discard_safely, bool, 0644);
65 MODULE_PARM_DESC(devices_handle_discard_safely,
66 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
71 #define NR_STRIPES 256
72 #define STRIPE_SIZE PAGE_SIZE
73 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
74 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
75 #define IO_THRESHOLD 1
76 #define BYPASS_THRESHOLD 1
77 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
78 #define HASH_MASK (NR_HASH - 1)
80 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
82 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
83 return &conf->stripe_hashtbl[hash];
86 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
87 * order without overlap. There may be several bio's per stripe+device, and
88 * a bio could span several devices.
89 * When walking this list for a particular stripe+device, we must never proceed
90 * beyond a bio that extends past this device, as the next bio might no longer
92 * This function is used to determine the 'next' bio in the list, given the sector
93 * of the current stripe+device
95 static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
97 int sectors = bio_sectors(bio);
98 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
105 * We maintain a biased count of active stripes in the bottom 16 bits of
106 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
108 static inline int raid5_bi_processed_stripes(struct bio *bio)
110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return (atomic_read(segments) >> 16) & 0xffff;
114 static inline int raid5_dec_bi_active_stripes(struct bio *bio)
116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 return atomic_sub_return(1, segments) & 0xffff;
120 static inline void raid5_inc_bi_active_stripes(struct bio *bio)
122 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
123 atomic_inc(segments);
126 static inline void raid5_set_bi_processed_stripes(struct bio *bio,
129 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
133 old = atomic_read(segments);
134 new = (old & 0xffff) | (cnt << 16);
135 } while (atomic_cmpxchg(segments, old, new) != old);
138 static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
140 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
141 atomic_set(segments, cnt);
144 /* Find first data disk in a raid6 stripe */
145 static inline int raid6_d0(struct stripe_head *sh)
148 /* ddf always start from first device */
150 /* md starts just after Q block */
151 if (sh->qd_idx == sh->disks - 1)
154 return sh->qd_idx + 1;
156 static inline int raid6_next_disk(int disk, int raid_disks)
159 return (disk < raid_disks) ? disk : 0;
162 /* When walking through the disks in a raid5, starting at raid6_d0,
163 * We need to map each disk to a 'slot', where the data disks are slot
164 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
165 * is raid_disks-1. This help does that mapping.
167 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
168 int *count, int syndrome_disks)
174 if (idx == sh->pd_idx)
175 return syndrome_disks;
176 if (idx == sh->qd_idx)
177 return syndrome_disks + 1;
183 static void return_io(struct bio *return_bi)
185 struct bio *bi = return_bi;
188 return_bi = bi->bi_next;
191 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
198 static void print_raid5_conf (struct r5conf *conf);
200 static int stripe_operations_active(struct stripe_head *sh)
202 return sh->check_state || sh->reconstruct_state ||
203 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
204 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
207 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
209 BUG_ON(!list_empty(&sh->lru));
210 BUG_ON(atomic_read(&conf->active_stripes)==0);
211 if (test_bit(STRIPE_HANDLE, &sh->state)) {
212 if (test_bit(STRIPE_DELAYED, &sh->state) &&
213 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
214 list_add_tail(&sh->lru, &conf->delayed_list);
215 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
216 sh->bm_seq - conf->seq_write > 0)
217 list_add_tail(&sh->lru, &conf->bitmap_list);
219 clear_bit(STRIPE_DELAYED, &sh->state);
220 clear_bit(STRIPE_BIT_DELAY, &sh->state);
221 list_add_tail(&sh->lru, &conf->handle_list);
223 md_wakeup_thread(conf->mddev->thread);
225 BUG_ON(stripe_operations_active(sh));
226 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
227 if (atomic_dec_return(&conf->preread_active_stripes)
229 md_wakeup_thread(conf->mddev->thread);
230 atomic_dec(&conf->active_stripes);
231 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
232 list_add_tail(&sh->lru, &conf->inactive_list);
233 wake_up(&conf->wait_for_stripe);
234 if (conf->retry_read_aligned)
235 md_wakeup_thread(conf->mddev->thread);
240 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
242 if (atomic_dec_and_test(&sh->count))
243 do_release_stripe(conf, sh);
246 static void release_stripe(struct stripe_head *sh)
248 struct r5conf *conf = sh->raid_conf;
251 local_irq_save(flags);
252 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
253 do_release_stripe(conf, sh);
254 spin_unlock(&conf->device_lock);
256 local_irq_restore(flags);
259 static inline void remove_hash(struct stripe_head *sh)
261 pr_debug("remove_hash(), stripe %llu\n",
262 (unsigned long long)sh->sector);
264 hlist_del_init(&sh->hash);
267 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
269 struct hlist_head *hp = stripe_hash(conf, sh->sector);
271 pr_debug("insert_hash(), stripe %llu\n",
272 (unsigned long long)sh->sector);
274 hlist_add_head(&sh->hash, hp);
278 /* find an idle stripe, make sure it is unhashed, and return it. */
279 static struct stripe_head *get_free_stripe(struct r5conf *conf)
281 struct stripe_head *sh = NULL;
282 struct list_head *first;
284 if (list_empty(&conf->inactive_list))
286 first = conf->inactive_list.next;
287 sh = list_entry(first, struct stripe_head, lru);
288 list_del_init(first);
290 atomic_inc(&conf->active_stripes);
295 static void shrink_buffers(struct stripe_head *sh)
299 int num = sh->raid_conf->pool_size;
301 for (i = 0; i < num ; i++) {
305 sh->dev[i].page = NULL;
310 static int grow_buffers(struct stripe_head *sh)
313 int num = sh->raid_conf->pool_size;
315 for (i = 0; i < num; i++) {
318 if (!(page = alloc_page(GFP_KERNEL))) {
321 sh->dev[i].page = page;
326 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
327 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
328 struct stripe_head *sh);
330 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
332 struct r5conf *conf = sh->raid_conf;
335 BUG_ON(atomic_read(&sh->count) != 0);
336 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
337 BUG_ON(stripe_operations_active(sh));
339 pr_debug("init_stripe called, stripe %llu\n",
340 (unsigned long long)sh->sector);
344 sh->generation = conf->generation - previous;
345 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
347 stripe_set_idx(sector, conf, previous, sh);
351 for (i = sh->disks; i--; ) {
352 struct r5dev *dev = &sh->dev[i];
354 if (dev->toread || dev->read || dev->towrite || dev->written ||
355 test_bit(R5_LOCKED, &dev->flags)) {
356 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
357 (unsigned long long)sh->sector, i, dev->toread,
358 dev->read, dev->towrite, dev->written,
359 test_bit(R5_LOCKED, &dev->flags));
363 raid5_build_block(sh, i, previous);
365 insert_hash(conf, sh);
368 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
371 struct stripe_head *sh;
373 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
374 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
375 if (sh->sector == sector && sh->generation == generation)
377 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
382 * Need to check if array has failed when deciding whether to:
384 * - remove non-faulty devices
387 * This determination is simple when no reshape is happening.
388 * However if there is a reshape, we need to carefully check
389 * both the before and after sections.
390 * This is because some failed devices may only affect one
391 * of the two sections, and some non-in_sync devices may
392 * be insync in the section most affected by failed devices.
394 static int calc_degraded(struct r5conf *conf)
396 int degraded, degraded2;
401 for (i = 0; i < conf->previous_raid_disks; i++) {
402 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
403 if (rdev && test_bit(Faulty, &rdev->flags))
404 rdev = rcu_dereference(conf->disks[i].replacement);
405 if (!rdev || test_bit(Faulty, &rdev->flags))
407 else if (test_bit(In_sync, &rdev->flags))
410 /* not in-sync or faulty.
411 * If the reshape increases the number of devices,
412 * this is being recovered by the reshape, so
413 * this 'previous' section is not in_sync.
414 * If the number of devices is being reduced however,
415 * the device can only be part of the array if
416 * we are reverting a reshape, so this section will
419 if (conf->raid_disks >= conf->previous_raid_disks)
423 if (conf->raid_disks == conf->previous_raid_disks)
427 for (i = 0; i < conf->raid_disks; i++) {
428 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
429 if (rdev && test_bit(Faulty, &rdev->flags))
430 rdev = rcu_dereference(conf->disks[i].replacement);
431 if (!rdev || test_bit(Faulty, &rdev->flags))
433 else if (test_bit(In_sync, &rdev->flags))
436 /* not in-sync or faulty.
437 * If reshape increases the number of devices, this
438 * section has already been recovered, else it
439 * almost certainly hasn't.
441 if (conf->raid_disks <= conf->previous_raid_disks)
445 if (degraded2 > degraded)
450 static int has_failed(struct r5conf *conf)
454 if (conf->mddev->reshape_position == MaxSector)
455 return conf->mddev->degraded > conf->max_degraded;
457 degraded = calc_degraded(conf);
458 if (degraded > conf->max_degraded)
463 static struct stripe_head *
464 get_active_stripe(struct r5conf *conf, sector_t sector,
465 int previous, int noblock, int noquiesce)
467 struct stripe_head *sh;
469 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
471 spin_lock_irq(&conf->device_lock);
474 wait_event_lock_irq(conf->wait_for_stripe,
475 conf->quiesce == 0 || noquiesce,
477 sh = __find_stripe(conf, sector, conf->generation - previous);
479 if (!conf->inactive_blocked)
480 sh = get_free_stripe(conf);
481 if (noblock && sh == NULL)
484 conf->inactive_blocked = 1;
485 wait_event_lock_irq(conf->wait_for_stripe,
486 !list_empty(&conf->inactive_list) &&
487 (atomic_read(&conf->active_stripes)
488 < (conf->max_nr_stripes *3/4)
489 || !conf->inactive_blocked),
491 conf->inactive_blocked = 0;
493 init_stripe(sh, sector, previous);
495 if (atomic_read(&sh->count)) {
496 BUG_ON(!list_empty(&sh->lru)
497 && !test_bit(STRIPE_EXPANDING, &sh->state)
498 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
500 if (!test_bit(STRIPE_HANDLE, &sh->state))
501 atomic_inc(&conf->active_stripes);
502 if (list_empty(&sh->lru) &&
503 !test_bit(STRIPE_EXPANDING, &sh->state))
505 list_del_init(&sh->lru);
508 } while (sh == NULL);
511 atomic_inc(&sh->count);
513 spin_unlock_irq(&conf->device_lock);
517 /* Determine if 'data_offset' or 'new_data_offset' should be used
518 * in this stripe_head.
520 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
522 sector_t progress = conf->reshape_progress;
523 /* Need a memory barrier to make sure we see the value
524 * of conf->generation, or ->data_offset that was set before
525 * reshape_progress was updated.
528 if (progress == MaxSector)
530 if (sh->generation == conf->generation - 1)
532 /* We are in a reshape, and this is a new-generation stripe,
533 * so use new_data_offset.
539 raid5_end_read_request(struct bio *bi, int error);
541 raid5_end_write_request(struct bio *bi, int error);
543 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
545 struct r5conf *conf = sh->raid_conf;
546 int i, disks = sh->disks;
550 for (i = disks; i--; ) {
552 int replace_only = 0;
553 struct bio *bi, *rbi;
554 struct md_rdev *rdev, *rrdev = NULL;
555 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
556 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
560 if (test_bit(R5_Discard, &sh->dev[i].flags))
562 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
564 else if (test_and_clear_bit(R5_WantReplace,
565 &sh->dev[i].flags)) {
570 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
573 bi = &sh->dev[i].req;
574 rbi = &sh->dev[i].rreq; /* For writing to replacement */
577 rrdev = rcu_dereference(conf->disks[i].replacement);
578 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
579 rdev = rcu_dereference(conf->disks[i].rdev);
588 /* We raced and saw duplicates */
591 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
596 if (rdev && test_bit(Faulty, &rdev->flags))
599 atomic_inc(&rdev->nr_pending);
600 if (rrdev && test_bit(Faulty, &rrdev->flags))
603 atomic_inc(&rrdev->nr_pending);
606 /* We have already checked bad blocks for reads. Now
607 * need to check for writes. We never accept write errors
608 * on the replacement, so we don't to check rrdev.
610 while ((rw & WRITE) && rdev &&
611 test_bit(WriteErrorSeen, &rdev->flags)) {
614 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
615 &first_bad, &bad_sectors);
620 set_bit(BlockedBadBlocks, &rdev->flags);
621 if (!conf->mddev->external &&
622 conf->mddev->flags) {
623 /* It is very unlikely, but we might
624 * still need to write out the
625 * bad block log - better give it
627 md_check_recovery(conf->mddev);
630 * Because md_wait_for_blocked_rdev
631 * will dec nr_pending, we must
632 * increment it first.
634 atomic_inc(&rdev->nr_pending);
635 md_wait_for_blocked_rdev(rdev, conf->mddev);
637 /* Acknowledged bad block - skip the write */
638 rdev_dec_pending(rdev, conf->mddev);
644 if (s->syncing || s->expanding || s->expanded
646 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
648 set_bit(STRIPE_IO_STARTED, &sh->state);
651 bi->bi_bdev = rdev->bdev;
653 bi->bi_end_io = (rw & WRITE)
654 ? raid5_end_write_request
655 : raid5_end_read_request;
658 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
659 __func__, (unsigned long long)sh->sector,
661 atomic_inc(&sh->count);
662 if (use_new_offset(conf, sh))
663 bi->bi_sector = (sh->sector
664 + rdev->new_data_offset);
666 bi->bi_sector = (sh->sector
667 + rdev->data_offset);
668 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
669 bi->bi_rw |= REQ_FLUSH;
672 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
673 bi->bi_io_vec[0].bv_offset = 0;
674 bi->bi_size = STRIPE_SIZE;
676 * If this is discard request, set bi_vcnt 0. We don't
677 * want to confuse SCSI because SCSI will replace payload
679 if (rw & REQ_DISCARD)
682 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
684 if (conf->mddev->gendisk)
685 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
686 bi, disk_devt(conf->mddev->gendisk),
688 generic_make_request(bi);
691 if (s->syncing || s->expanding || s->expanded
693 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
695 set_bit(STRIPE_IO_STARTED, &sh->state);
698 rbi->bi_bdev = rrdev->bdev;
700 BUG_ON(!(rw & WRITE));
701 rbi->bi_end_io = raid5_end_write_request;
702 rbi->bi_private = sh;
704 pr_debug("%s: for %llu schedule op %ld on "
705 "replacement disc %d\n",
706 __func__, (unsigned long long)sh->sector,
708 atomic_inc(&sh->count);
709 if (use_new_offset(conf, sh))
710 rbi->bi_sector = (sh->sector
711 + rrdev->new_data_offset);
713 rbi->bi_sector = (sh->sector
714 + rrdev->data_offset);
716 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
717 rbi->bi_io_vec[0].bv_offset = 0;
718 rbi->bi_size = STRIPE_SIZE;
720 * If this is discard request, set bi_vcnt 0. We don't
721 * want to confuse SCSI because SCSI will replace payload
723 if (rw & REQ_DISCARD)
725 if (conf->mddev->gendisk)
726 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
727 rbi, disk_devt(conf->mddev->gendisk),
729 generic_make_request(rbi);
731 if (!rdev && !rrdev) {
733 set_bit(STRIPE_DEGRADED, &sh->state);
734 pr_debug("skip op %ld on disc %d for sector %llu\n",
735 bi->bi_rw, i, (unsigned long long)sh->sector);
736 clear_bit(R5_LOCKED, &sh->dev[i].flags);
737 set_bit(STRIPE_HANDLE, &sh->state);
742 static struct dma_async_tx_descriptor *
743 async_copy_data(int frombio, struct bio *bio, struct page *page,
744 sector_t sector, struct dma_async_tx_descriptor *tx)
747 struct page *bio_page;
750 struct async_submit_ctl submit;
751 enum async_tx_flags flags = 0;
753 if (bio->bi_sector >= sector)
754 page_offset = (signed)(bio->bi_sector - sector) * 512;
756 page_offset = (signed)(sector - bio->bi_sector) * -512;
759 flags |= ASYNC_TX_FENCE;
760 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
762 bio_for_each_segment(bvl, bio, i) {
763 int len = bvl->bv_len;
767 if (page_offset < 0) {
768 b_offset = -page_offset;
769 page_offset += b_offset;
773 if (len > 0 && page_offset + len > STRIPE_SIZE)
774 clen = STRIPE_SIZE - page_offset;
779 b_offset += bvl->bv_offset;
780 bio_page = bvl->bv_page;
782 tx = async_memcpy(page, bio_page, page_offset,
783 b_offset, clen, &submit);
785 tx = async_memcpy(bio_page, page, b_offset,
786 page_offset, clen, &submit);
788 /* chain the operations */
789 submit.depend_tx = tx;
791 if (clen < len) /* hit end of page */
799 static void ops_complete_biofill(void *stripe_head_ref)
801 struct stripe_head *sh = stripe_head_ref;
802 struct bio *return_bi = NULL;
805 pr_debug("%s: stripe %llu\n", __func__,
806 (unsigned long long)sh->sector);
808 /* clear completed biofills */
809 for (i = sh->disks; i--; ) {
810 struct r5dev *dev = &sh->dev[i];
812 /* acknowledge completion of a biofill operation */
813 /* and check if we need to reply to a read request,
814 * new R5_Wantfill requests are held off until
815 * !STRIPE_BIOFILL_RUN
817 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
818 struct bio *rbi, *rbi2;
823 while (rbi && rbi->bi_sector <
824 dev->sector + STRIPE_SECTORS) {
825 rbi2 = r5_next_bio(rbi, dev->sector);
826 if (!raid5_dec_bi_active_stripes(rbi)) {
827 rbi->bi_next = return_bi;
834 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
836 return_io(return_bi);
838 set_bit(STRIPE_HANDLE, &sh->state);
842 static void ops_run_biofill(struct stripe_head *sh)
844 struct dma_async_tx_descriptor *tx = NULL;
845 struct async_submit_ctl submit;
848 pr_debug("%s: stripe %llu\n", __func__,
849 (unsigned long long)sh->sector);
851 for (i = sh->disks; i--; ) {
852 struct r5dev *dev = &sh->dev[i];
853 if (test_bit(R5_Wantfill, &dev->flags)) {
855 spin_lock_irq(&sh->stripe_lock);
856 dev->read = rbi = dev->toread;
858 spin_unlock_irq(&sh->stripe_lock);
859 while (rbi && rbi->bi_sector <
860 dev->sector + STRIPE_SECTORS) {
861 tx = async_copy_data(0, rbi, dev->page,
863 rbi = r5_next_bio(rbi, dev->sector);
868 atomic_inc(&sh->count);
869 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
870 async_trigger_callback(&submit);
873 static void mark_target_uptodate(struct stripe_head *sh, int target)
880 tgt = &sh->dev[target];
881 set_bit(R5_UPTODATE, &tgt->flags);
882 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
883 clear_bit(R5_Wantcompute, &tgt->flags);
886 static void ops_complete_compute(void *stripe_head_ref)
888 struct stripe_head *sh = stripe_head_ref;
890 pr_debug("%s: stripe %llu\n", __func__,
891 (unsigned long long)sh->sector);
893 /* mark the computed target(s) as uptodate */
894 mark_target_uptodate(sh, sh->ops.target);
895 mark_target_uptodate(sh, sh->ops.target2);
897 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
898 if (sh->check_state == check_state_compute_run)
899 sh->check_state = check_state_compute_result;
900 set_bit(STRIPE_HANDLE, &sh->state);
904 /* return a pointer to the address conversion region of the scribble buffer */
905 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
906 struct raid5_percpu *percpu)
908 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
911 static struct dma_async_tx_descriptor *
912 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
914 int disks = sh->disks;
915 struct page **xor_srcs = percpu->scribble;
916 int target = sh->ops.target;
917 struct r5dev *tgt = &sh->dev[target];
918 struct page *xor_dest = tgt->page;
920 struct dma_async_tx_descriptor *tx;
921 struct async_submit_ctl submit;
924 pr_debug("%s: stripe %llu block: %d\n",
925 __func__, (unsigned long long)sh->sector, target);
926 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
928 for (i = disks; i--; )
930 xor_srcs[count++] = sh->dev[i].page;
932 atomic_inc(&sh->count);
934 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
935 ops_complete_compute, sh, to_addr_conv(sh, percpu));
936 if (unlikely(count == 1))
937 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
939 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
944 /* set_syndrome_sources - populate source buffers for gen_syndrome
945 * @srcs - (struct page *) array of size sh->disks
946 * @sh - stripe_head to parse
948 * Populates srcs in proper layout order for the stripe and returns the
949 * 'count' of sources to be used in a call to async_gen_syndrome. The P
950 * destination buffer is recorded in srcs[count] and the Q destination
951 * is recorded in srcs[count+1]].
953 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
955 int disks = sh->disks;
956 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
957 int d0_idx = raid6_d0(sh);
961 for (i = 0; i < disks; i++)
967 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
969 srcs[slot] = sh->dev[i].page;
970 i = raid6_next_disk(i, disks);
971 } while (i != d0_idx);
973 return syndrome_disks;
976 static struct dma_async_tx_descriptor *
977 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
979 int disks = sh->disks;
980 struct page **blocks = percpu->scribble;
982 int qd_idx = sh->qd_idx;
983 struct dma_async_tx_descriptor *tx;
984 struct async_submit_ctl submit;
990 if (sh->ops.target < 0)
991 target = sh->ops.target2;
992 else if (sh->ops.target2 < 0)
993 target = sh->ops.target;
995 /* we should only have one valid target */
998 pr_debug("%s: stripe %llu block: %d\n",
999 __func__, (unsigned long long)sh->sector, target);
1001 tgt = &sh->dev[target];
1002 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1005 atomic_inc(&sh->count);
1007 if (target == qd_idx) {
1008 count = set_syndrome_sources(blocks, sh);
1009 blocks[count] = NULL; /* regenerating p is not necessary */
1010 BUG_ON(blocks[count+1] != dest); /* q should already be set */
1011 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1012 ops_complete_compute, sh,
1013 to_addr_conv(sh, percpu));
1014 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1016 /* Compute any data- or p-drive using XOR */
1018 for (i = disks; i-- ; ) {
1019 if (i == target || i == qd_idx)
1021 blocks[count++] = sh->dev[i].page;
1024 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1025 NULL, ops_complete_compute, sh,
1026 to_addr_conv(sh, percpu));
1027 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1033 static struct dma_async_tx_descriptor *
1034 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1036 int i, count, disks = sh->disks;
1037 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1038 int d0_idx = raid6_d0(sh);
1039 int faila = -1, failb = -1;
1040 int target = sh->ops.target;
1041 int target2 = sh->ops.target2;
1042 struct r5dev *tgt = &sh->dev[target];
1043 struct r5dev *tgt2 = &sh->dev[target2];
1044 struct dma_async_tx_descriptor *tx;
1045 struct page **blocks = percpu->scribble;
1046 struct async_submit_ctl submit;
1048 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1049 __func__, (unsigned long long)sh->sector, target, target2);
1050 BUG_ON(target < 0 || target2 < 0);
1051 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1052 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1054 /* we need to open-code set_syndrome_sources to handle the
1055 * slot number conversion for 'faila' and 'failb'
1057 for (i = 0; i < disks ; i++)
1062 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1064 blocks[slot] = sh->dev[i].page;
1070 i = raid6_next_disk(i, disks);
1071 } while (i != d0_idx);
1073 BUG_ON(faila == failb);
1076 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1077 __func__, (unsigned long long)sh->sector, faila, failb);
1079 atomic_inc(&sh->count);
1081 if (failb == syndrome_disks+1) {
1082 /* Q disk is one of the missing disks */
1083 if (faila == syndrome_disks) {
1084 /* Missing P+Q, just recompute */
1085 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1086 ops_complete_compute, sh,
1087 to_addr_conv(sh, percpu));
1088 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
1089 STRIPE_SIZE, &submit);
1093 int qd_idx = sh->qd_idx;
1095 /* Missing D+Q: recompute D from P, then recompute Q */
1096 if (target == qd_idx)
1097 data_target = target2;
1099 data_target = target;
1102 for (i = disks; i-- ; ) {
1103 if (i == data_target || i == qd_idx)
1105 blocks[count++] = sh->dev[i].page;
1107 dest = sh->dev[data_target].page;
1108 init_async_submit(&submit,
1109 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1111 to_addr_conv(sh, percpu));
1112 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1115 count = set_syndrome_sources(blocks, sh);
1116 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1117 ops_complete_compute, sh,
1118 to_addr_conv(sh, percpu));
1119 return async_gen_syndrome(blocks, 0, count+2,
1120 STRIPE_SIZE, &submit);
1123 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1124 ops_complete_compute, sh,
1125 to_addr_conv(sh, percpu));
1126 if (failb == syndrome_disks) {
1127 /* We're missing D+P. */
1128 return async_raid6_datap_recov(syndrome_disks+2,
1132 /* We're missing D+D. */
1133 return async_raid6_2data_recov(syndrome_disks+2,
1134 STRIPE_SIZE, faila, failb,
1141 static void ops_complete_prexor(void *stripe_head_ref)
1143 struct stripe_head *sh = stripe_head_ref;
1145 pr_debug("%s: stripe %llu\n", __func__,
1146 (unsigned long long)sh->sector);
1149 static struct dma_async_tx_descriptor *
1150 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1151 struct dma_async_tx_descriptor *tx)
1153 int disks = sh->disks;
1154 struct page **xor_srcs = percpu->scribble;
1155 int count = 0, pd_idx = sh->pd_idx, i;
1156 struct async_submit_ctl submit;
1158 /* existing parity data subtracted */
1159 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1161 pr_debug("%s: stripe %llu\n", __func__,
1162 (unsigned long long)sh->sector);
1164 for (i = disks; i--; ) {
1165 struct r5dev *dev = &sh->dev[i];
1166 /* Only process blocks that are known to be uptodate */
1167 if (test_bit(R5_Wantdrain, &dev->flags))
1168 xor_srcs[count++] = dev->page;
1171 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1172 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
1173 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1178 static struct dma_async_tx_descriptor *
1179 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1181 int disks = sh->disks;
1184 pr_debug("%s: stripe %llu\n", __func__,
1185 (unsigned long long)sh->sector);
1187 for (i = disks; i--; ) {
1188 struct r5dev *dev = &sh->dev[i];
1191 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
1194 spin_lock_irq(&sh->stripe_lock);
1195 chosen = dev->towrite;
1196 dev->towrite = NULL;
1197 BUG_ON(dev->written);
1198 wbi = dev->written = chosen;
1199 spin_unlock_irq(&sh->stripe_lock);
1201 while (wbi && wbi->bi_sector <
1202 dev->sector + STRIPE_SECTORS) {
1203 if (wbi->bi_rw & REQ_FUA)
1204 set_bit(R5_WantFUA, &dev->flags);
1205 if (wbi->bi_rw & REQ_SYNC)
1206 set_bit(R5_SyncIO, &dev->flags);
1207 if (wbi->bi_rw & REQ_DISCARD)
1208 set_bit(R5_Discard, &dev->flags);
1210 tx = async_copy_data(1, wbi, dev->page,
1212 wbi = r5_next_bio(wbi, dev->sector);
1220 static void ops_complete_reconstruct(void *stripe_head_ref)
1222 struct stripe_head *sh = stripe_head_ref;
1223 int disks = sh->disks;
1224 int pd_idx = sh->pd_idx;
1225 int qd_idx = sh->qd_idx;
1227 bool fua = false, sync = false, discard = false;
1229 pr_debug("%s: stripe %llu\n", __func__,
1230 (unsigned long long)sh->sector);
1232 for (i = disks; i--; ) {
1233 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1234 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1235 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1238 for (i = disks; i--; ) {
1239 struct r5dev *dev = &sh->dev[i];
1241 if (dev->written || i == pd_idx || i == qd_idx) {
1243 set_bit(R5_UPTODATE, &dev->flags);
1245 set_bit(R5_WantFUA, &dev->flags);
1247 set_bit(R5_SyncIO, &dev->flags);
1251 if (sh->reconstruct_state == reconstruct_state_drain_run)
1252 sh->reconstruct_state = reconstruct_state_drain_result;
1253 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1254 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1256 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1257 sh->reconstruct_state = reconstruct_state_result;
1260 set_bit(STRIPE_HANDLE, &sh->state);
1265 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1266 struct dma_async_tx_descriptor *tx)
1268 int disks = sh->disks;
1269 struct page **xor_srcs = percpu->scribble;
1270 struct async_submit_ctl submit;
1271 int count = 0, pd_idx = sh->pd_idx, i;
1272 struct page *xor_dest;
1274 unsigned long flags;
1276 pr_debug("%s: stripe %llu\n", __func__,
1277 (unsigned long long)sh->sector);
1279 for (i = 0; i < sh->disks; i++) {
1282 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1285 if (i >= sh->disks) {
1286 atomic_inc(&sh->count);
1287 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1288 ops_complete_reconstruct(sh);
1291 /* check if prexor is active which means only process blocks
1292 * that are part of a read-modify-write (written)
1294 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1296 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1297 for (i = disks; i--; ) {
1298 struct r5dev *dev = &sh->dev[i];
1300 xor_srcs[count++] = dev->page;
1303 xor_dest = sh->dev[pd_idx].page;
1304 for (i = disks; i--; ) {
1305 struct r5dev *dev = &sh->dev[i];
1307 xor_srcs[count++] = dev->page;
1311 /* 1/ if we prexor'd then the dest is reused as a source
1312 * 2/ if we did not prexor then we are redoing the parity
1313 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1314 * for the synchronous xor case
1316 flags = ASYNC_TX_ACK |
1317 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1319 atomic_inc(&sh->count);
1321 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1322 to_addr_conv(sh, percpu));
1323 if (unlikely(count == 1))
1324 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1326 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1330 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1331 struct dma_async_tx_descriptor *tx)
1333 struct async_submit_ctl submit;
1334 struct page **blocks = percpu->scribble;
1337 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1339 for (i = 0; i < sh->disks; i++) {
1340 if (sh->pd_idx == i || sh->qd_idx == i)
1342 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1345 if (i >= sh->disks) {
1346 atomic_inc(&sh->count);
1347 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1348 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1349 ops_complete_reconstruct(sh);
1353 count = set_syndrome_sources(blocks, sh);
1355 atomic_inc(&sh->count);
1357 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1358 sh, to_addr_conv(sh, percpu));
1359 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1362 static void ops_complete_check(void *stripe_head_ref)
1364 struct stripe_head *sh = stripe_head_ref;
1366 pr_debug("%s: stripe %llu\n", __func__,
1367 (unsigned long long)sh->sector);
1369 sh->check_state = check_state_check_result;
1370 set_bit(STRIPE_HANDLE, &sh->state);
1374 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1376 int disks = sh->disks;
1377 int pd_idx = sh->pd_idx;
1378 int qd_idx = sh->qd_idx;
1379 struct page *xor_dest;
1380 struct page **xor_srcs = percpu->scribble;
1381 struct dma_async_tx_descriptor *tx;
1382 struct async_submit_ctl submit;
1386 pr_debug("%s: stripe %llu\n", __func__,
1387 (unsigned long long)sh->sector);
1390 xor_dest = sh->dev[pd_idx].page;
1391 xor_srcs[count++] = xor_dest;
1392 for (i = disks; i--; ) {
1393 if (i == pd_idx || i == qd_idx)
1395 xor_srcs[count++] = sh->dev[i].page;
1398 init_async_submit(&submit, 0, NULL, NULL, NULL,
1399 to_addr_conv(sh, percpu));
1400 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1401 &sh->ops.zero_sum_result, &submit);
1403 atomic_inc(&sh->count);
1404 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1405 tx = async_trigger_callback(&submit);
1408 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1410 struct page **srcs = percpu->scribble;
1411 struct async_submit_ctl submit;
1414 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1415 (unsigned long long)sh->sector, checkp);
1417 count = set_syndrome_sources(srcs, sh);
1421 atomic_inc(&sh->count);
1422 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1423 sh, to_addr_conv(sh, percpu));
1424 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1425 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1428 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1430 int overlap_clear = 0, i, disks = sh->disks;
1431 struct dma_async_tx_descriptor *tx = NULL;
1432 struct r5conf *conf = sh->raid_conf;
1433 int level = conf->level;
1434 struct raid5_percpu *percpu;
1438 percpu = per_cpu_ptr(conf->percpu, cpu);
1439 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1440 ops_run_biofill(sh);
1444 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1446 tx = ops_run_compute5(sh, percpu);
1448 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1449 tx = ops_run_compute6_1(sh, percpu);
1451 tx = ops_run_compute6_2(sh, percpu);
1453 /* terminate the chain if reconstruct is not set to be run */
1454 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1458 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1459 tx = ops_run_prexor(sh, percpu, tx);
1461 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1462 tx = ops_run_biodrain(sh, tx);
1466 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1468 ops_run_reconstruct5(sh, percpu, tx);
1470 ops_run_reconstruct6(sh, percpu, tx);
1473 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1474 if (sh->check_state == check_state_run)
1475 ops_run_check_p(sh, percpu);
1476 else if (sh->check_state == check_state_run_q)
1477 ops_run_check_pq(sh, percpu, 0);
1478 else if (sh->check_state == check_state_run_pq)
1479 ops_run_check_pq(sh, percpu, 1);
1485 for (i = disks; i--; ) {
1486 struct r5dev *dev = &sh->dev[i];
1487 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1488 wake_up(&sh->raid_conf->wait_for_overlap);
1493 static int grow_one_stripe(struct r5conf *conf)
1495 struct stripe_head *sh;
1496 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
1500 sh->raid_conf = conf;
1502 spin_lock_init(&sh->stripe_lock);
1504 if (grow_buffers(sh)) {
1506 kmem_cache_free(conf->slab_cache, sh);
1509 /* we just created an active stripe so... */
1510 atomic_set(&sh->count, 1);
1511 atomic_inc(&conf->active_stripes);
1512 INIT_LIST_HEAD(&sh->lru);
1517 static int grow_stripes(struct r5conf *conf, int num)
1519 struct kmem_cache *sc;
1520 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1522 if (conf->mddev->gendisk)
1523 sprintf(conf->cache_name[0],
1524 "raid%d-%s", conf->level, mdname(conf->mddev));
1526 sprintf(conf->cache_name[0],
1527 "raid%d-%p", conf->level, conf->mddev);
1528 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1530 conf->active_name = 0;
1531 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1532 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1536 conf->slab_cache = sc;
1537 conf->pool_size = devs;
1539 if (!grow_one_stripe(conf))
1545 * scribble_len - return the required size of the scribble region
1546 * @num - total number of disks in the array
1548 * The size must be enough to contain:
1549 * 1/ a struct page pointer for each device in the array +2
1550 * 2/ room to convert each entry in (1) to its corresponding dma
1551 * (dma_map_page()) or page (page_address()) address.
1553 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1554 * calculate over all devices (not just the data blocks), using zeros in place
1555 * of the P and Q blocks.
1557 static size_t scribble_len(int num)
1561 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1566 static int resize_stripes(struct r5conf *conf, int newsize)
1568 /* Make all the stripes able to hold 'newsize' devices.
1569 * New slots in each stripe get 'page' set to a new page.
1571 * This happens in stages:
1572 * 1/ create a new kmem_cache and allocate the required number of
1574 * 2/ gather all the old stripe_heads and transfer the pages across
1575 * to the new stripe_heads. This will have the side effect of
1576 * freezing the array as once all stripe_heads have been collected,
1577 * no IO will be possible. Old stripe heads are freed once their
1578 * pages have been transferred over, and the old kmem_cache is
1579 * freed when all stripes are done.
1580 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1581 * we simple return a failre status - no need to clean anything up.
1582 * 4/ allocate new pages for the new slots in the new stripe_heads.
1583 * If this fails, we don't bother trying the shrink the
1584 * stripe_heads down again, we just leave them as they are.
1585 * As each stripe_head is processed the new one is released into
1588 * Once step2 is started, we cannot afford to wait for a write,
1589 * so we use GFP_NOIO allocations.
1591 struct stripe_head *osh, *nsh;
1592 LIST_HEAD(newstripes);
1593 struct disk_info *ndisks;
1596 struct kmem_cache *sc;
1599 if (newsize <= conf->pool_size)
1600 return 0; /* never bother to shrink */
1602 err = md_allow_write(conf->mddev);
1607 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1608 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1613 for (i = conf->max_nr_stripes; i; i--) {
1614 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
1618 nsh->raid_conf = conf;
1619 spin_lock_init(&nsh->stripe_lock);
1621 list_add(&nsh->lru, &newstripes);
1624 /* didn't get enough, give up */
1625 while (!list_empty(&newstripes)) {
1626 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1627 list_del(&nsh->lru);
1628 kmem_cache_free(sc, nsh);
1630 kmem_cache_destroy(sc);
1633 /* Step 2 - Must use GFP_NOIO now.
1634 * OK, we have enough stripes, start collecting inactive
1635 * stripes and copying them over
1637 list_for_each_entry(nsh, &newstripes, lru) {
1638 spin_lock_irq(&conf->device_lock);
1639 wait_event_lock_irq(conf->wait_for_stripe,
1640 !list_empty(&conf->inactive_list),
1642 osh = get_free_stripe(conf);
1643 spin_unlock_irq(&conf->device_lock);
1644 atomic_set(&nsh->count, 1);
1645 for(i=0; i<conf->pool_size; i++)
1646 nsh->dev[i].page = osh->dev[i].page;
1647 for( ; i<newsize; i++)
1648 nsh->dev[i].page = NULL;
1649 kmem_cache_free(conf->slab_cache, osh);
1651 kmem_cache_destroy(conf->slab_cache);
1654 * At this point, we are holding all the stripes so the array
1655 * is completely stalled, so now is a good time to resize
1656 * conf->disks and the scribble region
1658 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1660 for (i=0; i<conf->raid_disks; i++)
1661 ndisks[i] = conf->disks[i];
1663 conf->disks = ndisks;
1668 conf->scribble_len = scribble_len(newsize);
1669 for_each_present_cpu(cpu) {
1670 struct raid5_percpu *percpu;
1673 percpu = per_cpu_ptr(conf->percpu, cpu);
1674 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1677 kfree(percpu->scribble);
1678 percpu->scribble = scribble;
1686 /* Step 4, return new stripes to service */
1687 while(!list_empty(&newstripes)) {
1688 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1689 list_del_init(&nsh->lru);
1691 for (i=conf->raid_disks; i < newsize; i++)
1692 if (nsh->dev[i].page == NULL) {
1693 struct page *p = alloc_page(GFP_NOIO);
1694 nsh->dev[i].page = p;
1698 release_stripe(nsh);
1700 /* critical section pass, GFP_NOIO no longer needed */
1702 conf->slab_cache = sc;
1703 conf->active_name = 1-conf->active_name;
1704 conf->pool_size = newsize;
1708 static int drop_one_stripe(struct r5conf *conf)
1710 struct stripe_head *sh;
1712 spin_lock_irq(&conf->device_lock);
1713 sh = get_free_stripe(conf);
1714 spin_unlock_irq(&conf->device_lock);
1717 BUG_ON(atomic_read(&sh->count));
1719 kmem_cache_free(conf->slab_cache, sh);
1720 atomic_dec(&conf->active_stripes);
1724 static void shrink_stripes(struct r5conf *conf)
1726 while (drop_one_stripe(conf))
1729 if (conf->slab_cache)
1730 kmem_cache_destroy(conf->slab_cache);
1731 conf->slab_cache = NULL;
1734 static void raid5_end_read_request(struct bio * bi, int error)
1736 struct stripe_head *sh = bi->bi_private;
1737 struct r5conf *conf = sh->raid_conf;
1738 int disks = sh->disks, i;
1739 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1740 char b[BDEVNAME_SIZE];
1741 struct md_rdev *rdev = NULL;
1744 for (i=0 ; i<disks; i++)
1745 if (bi == &sh->dev[i].req)
1748 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1749 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1755 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1756 /* If replacement finished while this request was outstanding,
1757 * 'replacement' might be NULL already.
1758 * In that case it moved down to 'rdev'.
1759 * rdev is not removed until all requests are finished.
1761 rdev = conf->disks[i].replacement;
1763 rdev = conf->disks[i].rdev;
1765 if (use_new_offset(conf, sh))
1766 s = sh->sector + rdev->new_data_offset;
1768 s = sh->sector + rdev->data_offset;
1770 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1771 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1772 /* Note that this cannot happen on a
1773 * replacement device. We just fail those on
1778 "md/raid:%s: read error corrected"
1779 " (%lu sectors at %llu on %s)\n",
1780 mdname(conf->mddev), STRIPE_SECTORS,
1781 (unsigned long long)s,
1782 bdevname(rdev->bdev, b));
1783 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1784 clear_bit(R5_ReadError, &sh->dev[i].flags);
1785 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1786 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1787 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1789 if (atomic_read(&rdev->read_errors))
1790 atomic_set(&rdev->read_errors, 0);
1792 const char *bdn = bdevname(rdev->bdev, b);
1796 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1797 atomic_inc(&rdev->read_errors);
1798 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1801 "md/raid:%s: read error on replacement device "
1802 "(sector %llu on %s).\n",
1803 mdname(conf->mddev),
1804 (unsigned long long)s,
1806 else if (conf->mddev->degraded >= conf->max_degraded) {
1810 "md/raid:%s: read error not correctable "
1811 "(sector %llu on %s).\n",
1812 mdname(conf->mddev),
1813 (unsigned long long)s,
1815 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
1820 "md/raid:%s: read error NOT corrected!! "
1821 "(sector %llu on %s).\n",
1822 mdname(conf->mddev),
1823 (unsigned long long)s,
1825 } else if (atomic_read(&rdev->read_errors)
1826 > conf->max_nr_stripes)
1828 "md/raid:%s: Too many read errors, failing device %s.\n",
1829 mdname(conf->mddev), bdn);
1833 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1834 set_bit(R5_ReadError, &sh->dev[i].flags);
1835 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1837 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1839 clear_bit(R5_ReadError, &sh->dev[i].flags);
1840 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1842 && test_bit(In_sync, &rdev->flags)
1843 && rdev_set_badblocks(
1844 rdev, sh->sector, STRIPE_SECTORS, 0)))
1845 md_error(conf->mddev, rdev);
1848 rdev_dec_pending(rdev, conf->mddev);
1849 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1850 set_bit(STRIPE_HANDLE, &sh->state);
1854 static void raid5_end_write_request(struct bio *bi, int error)
1856 struct stripe_head *sh = bi->bi_private;
1857 struct r5conf *conf = sh->raid_conf;
1858 int disks = sh->disks, i;
1859 struct md_rdev *uninitialized_var(rdev);
1860 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1863 int replacement = 0;
1865 for (i = 0 ; i < disks; i++) {
1866 if (bi == &sh->dev[i].req) {
1867 rdev = conf->disks[i].rdev;
1870 if (bi == &sh->dev[i].rreq) {
1871 rdev = conf->disks[i].replacement;
1875 /* rdev was removed and 'replacement'
1876 * replaced it. rdev is not removed
1877 * until all requests are finished.
1879 rdev = conf->disks[i].rdev;
1883 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1884 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1893 md_error(conf->mddev, rdev);
1894 else if (is_badblock(rdev, sh->sector,
1896 &first_bad, &bad_sectors))
1897 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1900 set_bit(STRIPE_DEGRADED, &sh->state);
1901 set_bit(WriteErrorSeen, &rdev->flags);
1902 set_bit(R5_WriteError, &sh->dev[i].flags);
1903 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1904 set_bit(MD_RECOVERY_NEEDED,
1905 &rdev->mddev->recovery);
1906 } else if (is_badblock(rdev, sh->sector,
1908 &first_bad, &bad_sectors)) {
1909 set_bit(R5_MadeGood, &sh->dev[i].flags);
1910 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1911 /* That was a successful write so make
1912 * sure it looks like we already did
1915 set_bit(R5_ReWrite, &sh->dev[i].flags);
1918 rdev_dec_pending(rdev, conf->mddev);
1920 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1921 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1922 set_bit(STRIPE_HANDLE, &sh->state);
1926 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1928 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1930 struct r5dev *dev = &sh->dev[i];
1932 bio_init(&dev->req);
1933 dev->req.bi_io_vec = &dev->vec;
1935 dev->req.bi_max_vecs++;
1936 dev->req.bi_private = sh;
1937 dev->vec.bv_page = dev->page;
1939 bio_init(&dev->rreq);
1940 dev->rreq.bi_io_vec = &dev->rvec;
1941 dev->rreq.bi_vcnt++;
1942 dev->rreq.bi_max_vecs++;
1943 dev->rreq.bi_private = sh;
1944 dev->rvec.bv_page = dev->page;
1947 dev->sector = compute_blocknr(sh, i, previous);
1950 static void error(struct mddev *mddev, struct md_rdev *rdev)
1952 char b[BDEVNAME_SIZE];
1953 struct r5conf *conf = mddev->private;
1954 unsigned long flags;
1955 pr_debug("raid456: error called\n");
1957 spin_lock_irqsave(&conf->device_lock, flags);
1958 clear_bit(In_sync, &rdev->flags);
1959 mddev->degraded = calc_degraded(conf);
1960 spin_unlock_irqrestore(&conf->device_lock, flags);
1961 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1963 set_bit(Blocked, &rdev->flags);
1964 set_bit(Faulty, &rdev->flags);
1965 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1967 "md/raid:%s: Disk failure on %s, disabling device.\n"
1968 "md/raid:%s: Operation continuing on %d devices.\n",
1970 bdevname(rdev->bdev, b),
1972 conf->raid_disks - mddev->degraded);
1976 * Input: a 'big' sector number,
1977 * Output: index of the data and parity disk, and the sector # in them.
1979 static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
1980 int previous, int *dd_idx,
1981 struct stripe_head *sh)
1983 sector_t stripe, stripe2;
1984 sector_t chunk_number;
1985 unsigned int chunk_offset;
1988 sector_t new_sector;
1989 int algorithm = previous ? conf->prev_algo
1991 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1992 : conf->chunk_sectors;
1993 int raid_disks = previous ? conf->previous_raid_disks
1995 int data_disks = raid_disks - conf->max_degraded;
1997 /* First compute the information on this sector */
2000 * Compute the chunk number and the sector offset inside the chunk
2002 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2003 chunk_number = r_sector;
2006 * Compute the stripe number
2008 stripe = chunk_number;
2009 *dd_idx = sector_div(stripe, data_disks);
2012 * Select the parity disk based on the user selected algorithm.
2014 pd_idx = qd_idx = -1;
2015 switch(conf->level) {
2017 pd_idx = data_disks;
2020 switch (algorithm) {
2021 case ALGORITHM_LEFT_ASYMMETRIC:
2022 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2023 if (*dd_idx >= pd_idx)
2026 case ALGORITHM_RIGHT_ASYMMETRIC:
2027 pd_idx = sector_div(stripe2, raid_disks);
2028 if (*dd_idx >= pd_idx)
2031 case ALGORITHM_LEFT_SYMMETRIC:
2032 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2033 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2035 case ALGORITHM_RIGHT_SYMMETRIC:
2036 pd_idx = sector_div(stripe2, raid_disks);
2037 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2039 case ALGORITHM_PARITY_0:
2043 case ALGORITHM_PARITY_N:
2044 pd_idx = data_disks;
2052 switch (algorithm) {
2053 case ALGORITHM_LEFT_ASYMMETRIC:
2054 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2055 qd_idx = pd_idx + 1;
2056 if (pd_idx == raid_disks-1) {
2057 (*dd_idx)++; /* Q D D D P */
2059 } else if (*dd_idx >= pd_idx)
2060 (*dd_idx) += 2; /* D D P Q D */
2062 case ALGORITHM_RIGHT_ASYMMETRIC:
2063 pd_idx = sector_div(stripe2, raid_disks);
2064 qd_idx = pd_idx + 1;
2065 if (pd_idx == raid_disks-1) {
2066 (*dd_idx)++; /* Q D D D P */
2068 } else if (*dd_idx >= pd_idx)
2069 (*dd_idx) += 2; /* D D P Q D */
2071 case ALGORITHM_LEFT_SYMMETRIC:
2072 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2073 qd_idx = (pd_idx + 1) % raid_disks;
2074 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2076 case ALGORITHM_RIGHT_SYMMETRIC:
2077 pd_idx = sector_div(stripe2, raid_disks);
2078 qd_idx = (pd_idx + 1) % raid_disks;
2079 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2082 case ALGORITHM_PARITY_0:
2087 case ALGORITHM_PARITY_N:
2088 pd_idx = data_disks;
2089 qd_idx = data_disks + 1;
2092 case ALGORITHM_ROTATING_ZERO_RESTART:
2093 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2094 * of blocks for computing Q is different.
2096 pd_idx = sector_div(stripe2, raid_disks);
2097 qd_idx = pd_idx + 1;
2098 if (pd_idx == raid_disks-1) {
2099 (*dd_idx)++; /* Q D D D P */
2101 } else if (*dd_idx >= pd_idx)
2102 (*dd_idx) += 2; /* D D P Q D */
2106 case ALGORITHM_ROTATING_N_RESTART:
2107 /* Same a left_asymmetric, by first stripe is
2108 * D D D P Q rather than
2112 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2113 qd_idx = pd_idx + 1;
2114 if (pd_idx == raid_disks-1) {
2115 (*dd_idx)++; /* Q D D D P */
2117 } else if (*dd_idx >= pd_idx)
2118 (*dd_idx) += 2; /* D D P Q D */
2122 case ALGORITHM_ROTATING_N_CONTINUE:
2123 /* Same as left_symmetric but Q is before P */
2124 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2125 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2126 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2130 case ALGORITHM_LEFT_ASYMMETRIC_6:
2131 /* RAID5 left_asymmetric, with Q on last device */
2132 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2133 if (*dd_idx >= pd_idx)
2135 qd_idx = raid_disks - 1;
2138 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2139 pd_idx = sector_div(stripe2, raid_disks-1);
2140 if (*dd_idx >= pd_idx)
2142 qd_idx = raid_disks - 1;
2145 case ALGORITHM_LEFT_SYMMETRIC_6:
2146 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2147 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2148 qd_idx = raid_disks - 1;
2151 case ALGORITHM_RIGHT_SYMMETRIC_6:
2152 pd_idx = sector_div(stripe2, raid_disks-1);
2153 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2154 qd_idx = raid_disks - 1;
2157 case ALGORITHM_PARITY_0_6:
2160 qd_idx = raid_disks - 1;
2170 sh->pd_idx = pd_idx;
2171 sh->qd_idx = qd_idx;
2172 sh->ddf_layout = ddf_layout;
2175 * Finally, compute the new sector number
2177 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2182 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
2184 struct r5conf *conf = sh->raid_conf;
2185 int raid_disks = sh->disks;
2186 int data_disks = raid_disks - conf->max_degraded;
2187 sector_t new_sector = sh->sector, check;
2188 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2189 : conf->chunk_sectors;
2190 int algorithm = previous ? conf->prev_algo
2194 sector_t chunk_number;
2195 int dummy1, dd_idx = i;
2197 struct stripe_head sh2;
2200 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2201 stripe = new_sector;
2203 if (i == sh->pd_idx)
2205 switch(conf->level) {
2208 switch (algorithm) {
2209 case ALGORITHM_LEFT_ASYMMETRIC:
2210 case ALGORITHM_RIGHT_ASYMMETRIC:
2214 case ALGORITHM_LEFT_SYMMETRIC:
2215 case ALGORITHM_RIGHT_SYMMETRIC:
2218 i -= (sh->pd_idx + 1);
2220 case ALGORITHM_PARITY_0:
2223 case ALGORITHM_PARITY_N:
2230 if (i == sh->qd_idx)
2231 return 0; /* It is the Q disk */
2232 switch (algorithm) {
2233 case ALGORITHM_LEFT_ASYMMETRIC:
2234 case ALGORITHM_RIGHT_ASYMMETRIC:
2235 case ALGORITHM_ROTATING_ZERO_RESTART:
2236 case ALGORITHM_ROTATING_N_RESTART:
2237 if (sh->pd_idx == raid_disks-1)
2238 i--; /* Q D D D P */
2239 else if (i > sh->pd_idx)
2240 i -= 2; /* D D P Q D */
2242 case ALGORITHM_LEFT_SYMMETRIC:
2243 case ALGORITHM_RIGHT_SYMMETRIC:
2244 if (sh->pd_idx == raid_disks-1)
2245 i--; /* Q D D D P */
2250 i -= (sh->pd_idx + 2);
2253 case ALGORITHM_PARITY_0:
2256 case ALGORITHM_PARITY_N:
2258 case ALGORITHM_ROTATING_N_CONTINUE:
2259 /* Like left_symmetric, but P is before Q */
2260 if (sh->pd_idx == 0)
2261 i--; /* P D D D Q */
2266 i -= (sh->pd_idx + 1);
2269 case ALGORITHM_LEFT_ASYMMETRIC_6:
2270 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2274 case ALGORITHM_LEFT_SYMMETRIC_6:
2275 case ALGORITHM_RIGHT_SYMMETRIC_6:
2277 i += data_disks + 1;
2278 i -= (sh->pd_idx + 1);
2280 case ALGORITHM_PARITY_0_6:
2289 chunk_number = stripe * data_disks + i;
2290 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
2292 check = raid5_compute_sector(conf, r_sector,
2293 previous, &dummy1, &sh2);
2294 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2295 || sh2.qd_idx != sh->qd_idx) {
2296 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2297 mdname(conf->mddev));
2305 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
2306 int rcw, int expand)
2308 int i, pd_idx = sh->pd_idx, disks = sh->disks;
2309 struct r5conf *conf = sh->raid_conf;
2310 int level = conf->level;
2314 for (i = disks; i--; ) {
2315 struct r5dev *dev = &sh->dev[i];
2318 set_bit(R5_LOCKED, &dev->flags);
2319 set_bit(R5_Wantdrain, &dev->flags);
2321 clear_bit(R5_UPTODATE, &dev->flags);
2325 /* if we are not expanding this is a proper write request, and
2326 * there will be bios with new data to be drained into the
2331 /* False alarm, nothing to do */
2333 sh->reconstruct_state = reconstruct_state_drain_run;
2334 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2336 sh->reconstruct_state = reconstruct_state_run;
2338 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2340 if (s->locked + conf->max_degraded == disks)
2341 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2342 atomic_inc(&conf->pending_full_writes);
2345 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2346 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2348 for (i = disks; i--; ) {
2349 struct r5dev *dev = &sh->dev[i];
2354 (test_bit(R5_UPTODATE, &dev->flags) ||
2355 test_bit(R5_Wantcompute, &dev->flags))) {
2356 set_bit(R5_Wantdrain, &dev->flags);
2357 set_bit(R5_LOCKED, &dev->flags);
2358 clear_bit(R5_UPTODATE, &dev->flags);
2363 /* False alarm - nothing to do */
2365 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2366 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2367 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2368 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2371 /* keep the parity disk(s) locked while asynchronous operations
2374 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2375 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2379 int qd_idx = sh->qd_idx;
2380 struct r5dev *dev = &sh->dev[qd_idx];
2382 set_bit(R5_LOCKED, &dev->flags);
2383 clear_bit(R5_UPTODATE, &dev->flags);
2387 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2388 __func__, (unsigned long long)sh->sector,
2389 s->locked, s->ops_request);
2393 * Each stripe/dev can have one or more bion attached.
2394 * toread/towrite point to the first in a chain.
2395 * The bi_next chain must be in order.
2397 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2400 struct r5conf *conf = sh->raid_conf;
2403 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2404 (unsigned long long)bi->bi_sector,
2405 (unsigned long long)sh->sector);
2408 * If several bio share a stripe. The bio bi_phys_segments acts as a
2409 * reference count to avoid race. The reference count should already be
2410 * increased before this function is called (for example, in
2411 * make_request()), so other bio sharing this stripe will not free the
2412 * stripe. If a stripe is owned by one stripe, the stripe lock will
2415 spin_lock_irq(&sh->stripe_lock);
2417 bip = &sh->dev[dd_idx].towrite;
2421 bip = &sh->dev[dd_idx].toread;
2422 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2423 if (bio_end_sector(*bip) > bi->bi_sector)
2425 bip = & (*bip)->bi_next;
2427 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
2430 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2434 raid5_inc_bi_active_stripes(bi);
2437 /* check if page is covered */
2438 sector_t sector = sh->dev[dd_idx].sector;
2439 for (bi=sh->dev[dd_idx].towrite;
2440 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2441 bi && bi->bi_sector <= sector;
2442 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2443 if (bio_end_sector(bi) >= sector)
2444 sector = bio_end_sector(bi);
2446 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2447 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2450 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2451 (unsigned long long)(*bip)->bi_sector,
2452 (unsigned long long)sh->sector, dd_idx);
2453 spin_unlock_irq(&sh->stripe_lock);
2455 if (conf->mddev->bitmap && firstwrite) {
2456 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2458 sh->bm_seq = conf->seq_flush+1;
2459 set_bit(STRIPE_BIT_DELAY, &sh->state);
2464 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2465 spin_unlock_irq(&sh->stripe_lock);
2469 static void end_reshape(struct r5conf *conf);
2471 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
2472 struct stripe_head *sh)
2474 int sectors_per_chunk =
2475 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2477 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2478 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2480 raid5_compute_sector(conf,
2481 stripe * (disks - conf->max_degraded)
2482 *sectors_per_chunk + chunk_offset,
2488 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
2489 struct stripe_head_state *s, int disks,
2490 struct bio **return_bi)
2493 for (i = disks; i--; ) {
2497 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2498 struct md_rdev *rdev;
2500 rdev = rcu_dereference(conf->disks[i].rdev);
2501 if (rdev && test_bit(In_sync, &rdev->flags))
2502 atomic_inc(&rdev->nr_pending);
2507 if (!rdev_set_badblocks(
2511 md_error(conf->mddev, rdev);
2512 rdev_dec_pending(rdev, conf->mddev);
2515 spin_lock_irq(&sh->stripe_lock);
2516 /* fail all writes first */
2517 bi = sh->dev[i].towrite;
2518 sh->dev[i].towrite = NULL;
2519 spin_unlock_irq(&sh->stripe_lock);
2523 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2524 wake_up(&conf->wait_for_overlap);
2526 while (bi && bi->bi_sector <
2527 sh->dev[i].sector + STRIPE_SECTORS) {
2528 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2529 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2530 if (!raid5_dec_bi_active_stripes(bi)) {
2531 md_write_end(conf->mddev);
2532 bi->bi_next = *return_bi;
2538 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2539 STRIPE_SECTORS, 0, 0);
2541 /* and fail all 'written' */
2542 bi = sh->dev[i].written;
2543 sh->dev[i].written = NULL;
2544 if (bi) bitmap_end = 1;
2545 while (bi && bi->bi_sector <
2546 sh->dev[i].sector + STRIPE_SECTORS) {
2547 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2548 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2549 if (!raid5_dec_bi_active_stripes(bi)) {
2550 md_write_end(conf->mddev);
2551 bi->bi_next = *return_bi;
2557 /* fail any reads if this device is non-operational and
2558 * the data has not reached the cache yet.
2560 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2561 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2562 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2563 spin_lock_irq(&sh->stripe_lock);
2564 bi = sh->dev[i].toread;
2565 sh->dev[i].toread = NULL;
2566 spin_unlock_irq(&sh->stripe_lock);
2567 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2568 wake_up(&conf->wait_for_overlap);
2569 while (bi && bi->bi_sector <
2570 sh->dev[i].sector + STRIPE_SECTORS) {
2571 struct bio *nextbi =
2572 r5_next_bio(bi, sh->dev[i].sector);
2573 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2574 if (!raid5_dec_bi_active_stripes(bi)) {
2575 bi->bi_next = *return_bi;
2582 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2583 STRIPE_SECTORS, 0, 0);
2584 /* If we were in the middle of a write the parity block might
2585 * still be locked - so just clear all R5_LOCKED flags
2587 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2590 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2591 if (atomic_dec_and_test(&conf->pending_full_writes))
2592 md_wakeup_thread(conf->mddev->thread);
2596 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
2597 struct stripe_head_state *s)
2602 clear_bit(STRIPE_SYNCING, &sh->state);
2603 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2604 wake_up(&conf->wait_for_overlap);
2607 /* There is nothing more to do for sync/check/repair.
2608 * Don't even need to abort as that is handled elsewhere
2609 * if needed, and not always wanted e.g. if there is a known
2611 * For recover/replace we need to record a bad block on all
2612 * non-sync devices, or abort the recovery
2614 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2615 /* During recovery devices cannot be removed, so
2616 * locking and refcounting of rdevs is not needed
2618 for (i = 0; i < conf->raid_disks; i++) {
2619 struct md_rdev *rdev = conf->disks[i].rdev;
2621 && !test_bit(Faulty, &rdev->flags)
2622 && !test_bit(In_sync, &rdev->flags)
2623 && !rdev_set_badblocks(rdev, sh->sector,
2626 rdev = conf->disks[i].replacement;
2628 && !test_bit(Faulty, &rdev->flags)
2629 && !test_bit(In_sync, &rdev->flags)
2630 && !rdev_set_badblocks(rdev, sh->sector,
2635 conf->recovery_disabled =
2636 conf->mddev->recovery_disabled;
2638 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
2641 static int want_replace(struct stripe_head *sh, int disk_idx)
2643 struct md_rdev *rdev;
2645 /* Doing recovery so rcu locking not required */
2646 rdev = sh->raid_conf->disks[disk_idx].replacement;
2648 && !test_bit(Faulty, &rdev->flags)
2649 && !test_bit(In_sync, &rdev->flags)
2650 && (rdev->recovery_offset <= sh->sector
2651 || rdev->mddev->recovery_cp <= sh->sector))
2657 /* fetch_block - checks the given member device to see if its data needs
2658 * to be read or computed to satisfy a request.
2660 * Returns 1 when no more member devices need to be checked, otherwise returns
2661 * 0 to tell the loop in handle_stripe_fill to continue
2663 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2664 int disk_idx, int disks)
2666 struct r5dev *dev = &sh->dev[disk_idx];
2667 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2668 &sh->dev[s->failed_num[1]] };
2670 /* is the data in this block needed, and can we get it? */
2671 if (!test_bit(R5_LOCKED, &dev->flags) &&
2672 !test_bit(R5_UPTODATE, &dev->flags) &&
2674 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2675 s->syncing || s->expanding ||
2676 (s->replacing && want_replace(sh, disk_idx)) ||
2677 (s->failed >= 1 && fdev[0]->toread) ||
2678 (s->failed >= 2 && fdev[1]->toread) ||
2679 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2680 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2681 ((sh->raid_conf->level == 6 || sh->sector >= sh->raid_conf->mddev->recovery_cp)
2682 && s->failed && s->to_write))) {
2683 /* we would like to get this block, possibly by computing it,
2684 * otherwise read it if the backing disk is insync
2686 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2687 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2688 if ((s->uptodate == disks - 1) &&
2689 (s->failed && (disk_idx == s->failed_num[0] ||
2690 disk_idx == s->failed_num[1]))) {
2691 /* have disk failed, and we're requested to fetch it;
2694 pr_debug("Computing stripe %llu block %d\n",
2695 (unsigned long long)sh->sector, disk_idx);
2696 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2697 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2698 set_bit(R5_Wantcompute, &dev->flags);
2699 sh->ops.target = disk_idx;
2700 sh->ops.target2 = -1; /* no 2nd target */
2702 /* Careful: from this point on 'uptodate' is in the eye
2703 * of raid_run_ops which services 'compute' operations
2704 * before writes. R5_Wantcompute flags a block that will
2705 * be R5_UPTODATE by the time it is needed for a
2706 * subsequent operation.
2710 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2711 /* Computing 2-failure is *very* expensive; only
2712 * do it if failed >= 2
2715 for (other = disks; other--; ) {
2716 if (other == disk_idx)
2718 if (!test_bit(R5_UPTODATE,
2719 &sh->dev[other].flags))
2723 pr_debug("Computing stripe %llu blocks %d,%d\n",
2724 (unsigned long long)sh->sector,
2726 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2727 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2728 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2729 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2730 sh->ops.target = disk_idx;
2731 sh->ops.target2 = other;
2735 } else if (test_bit(R5_Insync, &dev->flags)) {
2736 set_bit(R5_LOCKED, &dev->flags);
2737 set_bit(R5_Wantread, &dev->flags);
2739 pr_debug("Reading block %d (sync=%d)\n",
2740 disk_idx, s->syncing);
2748 * handle_stripe_fill - read or compute data to satisfy pending requests.
2750 static void handle_stripe_fill(struct stripe_head *sh,
2751 struct stripe_head_state *s,
2756 /* look for blocks to read/compute, skip this if a compute
2757 * is already in flight, or if the stripe contents are in the
2758 * midst of changing due to a write
2760 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2761 !sh->reconstruct_state)
2762 for (i = disks; i--; )
2763 if (fetch_block(sh, s, i, disks))
2765 set_bit(STRIPE_HANDLE, &sh->state);
2769 /* handle_stripe_clean_event
2770 * any written block on an uptodate or failed drive can be returned.
2771 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2772 * never LOCKED, so we don't need to test 'failed' directly.
2774 static void handle_stripe_clean_event(struct r5conf *conf,
2775 struct stripe_head *sh, int disks, struct bio **return_bi)
2779 int discard_pending = 0;
2781 for (i = disks; i--; )
2782 if (sh->dev[i].written) {
2784 if (!test_bit(R5_LOCKED, &dev->flags) &&
2785 (test_bit(R5_UPTODATE, &dev->flags) ||
2786 test_bit(R5_Discard, &dev->flags))) {
2787 /* We can return any write requests */
2788 struct bio *wbi, *wbi2;
2789 pr_debug("Return write for disc %d\n", i);
2790 if (test_and_clear_bit(R5_Discard, &dev->flags))
2791 clear_bit(R5_UPTODATE, &dev->flags);
2793 dev->written = NULL;
2794 while (wbi && wbi->bi_sector <
2795 dev->sector + STRIPE_SECTORS) {
2796 wbi2 = r5_next_bio(wbi, dev->sector);
2797 if (!raid5_dec_bi_active_stripes(wbi)) {
2798 md_write_end(conf->mddev);
2799 wbi->bi_next = *return_bi;
2804 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2806 !test_bit(STRIPE_DEGRADED, &sh->state),
2808 } else if (test_bit(R5_Discard, &dev->flags))
2809 discard_pending = 1;
2811 if (!discard_pending &&
2812 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2813 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2814 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2815 if (sh->qd_idx >= 0) {
2816 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2817 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2819 /* now that discard is done we can proceed with any sync */
2820 clear_bit(STRIPE_DISCARD, &sh->state);
2822 * SCSI discard will change some bio fields and the stripe has
2823 * no updated data, so remove it from hash list and the stripe
2824 * will be reinitialized
2826 spin_lock_irq(&conf->device_lock);
2828 spin_unlock_irq(&conf->device_lock);
2829 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2830 set_bit(STRIPE_HANDLE, &sh->state);
2834 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2835 if (atomic_dec_and_test(&conf->pending_full_writes))
2836 md_wakeup_thread(conf->mddev->thread);
2839 static void handle_stripe_dirtying(struct r5conf *conf,
2840 struct stripe_head *sh,
2841 struct stripe_head_state *s,
2844 int rmw = 0, rcw = 0, i;
2845 sector_t recovery_cp = conf->mddev->recovery_cp;
2847 /* RAID6 requires 'rcw' in current implementation.
2848 * Otherwise, check whether resync is now happening or should start.
2849 * If yes, then the array is dirty (after unclean shutdown or
2850 * initial creation), so parity in some stripes might be inconsistent.
2851 * In this case, we need to always do reconstruct-write, to ensure
2852 * that in case of drive failure or read-error correction, we
2853 * generate correct data from the parity.
2855 if (conf->max_degraded == 2 ||
2856 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2857 /* Calculate the real rcw later - for now make it
2858 * look like rcw is cheaper
2861 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2862 conf->max_degraded, (unsigned long long)recovery_cp,
2863 (unsigned long long)sh->sector);
2864 } else for (i = disks; i--; ) {
2865 /* would I have to read this buffer for read_modify_write */
2866 struct r5dev *dev = &sh->dev[i];
2867 if ((dev->towrite || i == sh->pd_idx) &&
2868 !test_bit(R5_LOCKED, &dev->flags) &&
2869 !(test_bit(R5_UPTODATE, &dev->flags) ||
2870 test_bit(R5_Wantcompute, &dev->flags))) {
2871 if (test_bit(R5_Insync, &dev->flags))
2874 rmw += 2*disks; /* cannot read it */
2876 /* Would I have to read this buffer for reconstruct_write */
2877 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2878 !test_bit(R5_LOCKED, &dev->flags) &&
2879 !(test_bit(R5_UPTODATE, &dev->flags) ||
2880 test_bit(R5_Wantcompute, &dev->flags))) {
2881 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2886 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2887 (unsigned long long)sh->sector, rmw, rcw);
2888 set_bit(STRIPE_HANDLE, &sh->state);
2889 if (rmw < rcw && rmw > 0) {
2890 /* prefer read-modify-write, but need to get some data */
2891 if (conf->mddev->queue)
2892 blk_add_trace_msg(conf->mddev->queue,
2893 "raid5 rmw %llu %d",
2894 (unsigned long long)sh->sector, rmw);
2895 for (i = disks; i--; ) {
2896 struct r5dev *dev = &sh->dev[i];
2897 if ((dev->towrite || i == sh->pd_idx) &&
2898 !test_bit(R5_LOCKED, &dev->flags) &&
2899 !(test_bit(R5_UPTODATE, &dev->flags) ||
2900 test_bit(R5_Wantcompute, &dev->flags)) &&
2901 test_bit(R5_Insync, &dev->flags)) {
2903 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2904 pr_debug("Read_old block "
2905 "%d for r-m-w\n", i);
2906 set_bit(R5_LOCKED, &dev->flags);
2907 set_bit(R5_Wantread, &dev->flags);
2910 set_bit(STRIPE_DELAYED, &sh->state);
2911 set_bit(STRIPE_HANDLE, &sh->state);
2916 if (rcw <= rmw && rcw > 0) {
2917 /* want reconstruct write, but need to get some data */
2920 for (i = disks; i--; ) {
2921 struct r5dev *dev = &sh->dev[i];
2922 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2923 i != sh->pd_idx && i != sh->qd_idx &&
2924 !test_bit(R5_LOCKED, &dev->flags) &&
2925 !(test_bit(R5_UPTODATE, &dev->flags) ||
2926 test_bit(R5_Wantcompute, &dev->flags))) {
2928 if (!test_bit(R5_Insync, &dev->flags))
2929 continue; /* it's a failed drive */
2931 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2932 pr_debug("Read_old block "
2933 "%d for Reconstruct\n", i);
2934 set_bit(R5_LOCKED, &dev->flags);
2935 set_bit(R5_Wantread, &dev->flags);
2939 set_bit(STRIPE_DELAYED, &sh->state);
2940 set_bit(STRIPE_HANDLE, &sh->state);
2944 if (rcw && conf->mddev->queue)
2945 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2946 (unsigned long long)sh->sector,
2947 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
2949 /* now if nothing is locked, and if we have enough data,
2950 * we can start a write request
2952 /* since handle_stripe can be called at any time we need to handle the
2953 * case where a compute block operation has been submitted and then a
2954 * subsequent call wants to start a write request. raid_run_ops only
2955 * handles the case where compute block and reconstruct are requested
2956 * simultaneously. If this is not the case then new writes need to be
2957 * held off until the compute completes.
2959 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2960 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2961 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2962 schedule_reconstruction(sh, s, rcw == 0, 0);
2965 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
2966 struct stripe_head_state *s, int disks)
2968 struct r5dev *dev = NULL;
2970 set_bit(STRIPE_HANDLE, &sh->state);
2972 switch (sh->check_state) {
2973 case check_state_idle:
2974 /* start a new check operation if there are no failures */
2975 if (s->failed == 0) {
2976 BUG_ON(s->uptodate != disks);
2977 sh->check_state = check_state_run;
2978 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2979 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2983 dev = &sh->dev[s->failed_num[0]];
2985 case check_state_compute_result:
2986 sh->check_state = check_state_idle;
2988 dev = &sh->dev[sh->pd_idx];
2990 /* check that a write has not made the stripe insync */
2991 if (test_bit(STRIPE_INSYNC, &sh->state))
2994 /* either failed parity check, or recovery is happening */
2995 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2996 BUG_ON(s->uptodate != disks);
2998 set_bit(R5_LOCKED, &dev->flags);
3000 set_bit(R5_Wantwrite, &dev->flags);
3002 clear_bit(STRIPE_DEGRADED, &sh->state);
3003 set_bit(STRIPE_INSYNC, &sh->state);
3005 case check_state_run:
3006 break; /* we will be called again upon completion */
3007 case check_state_check_result:
3008 sh->check_state = check_state_idle;
3010 /* if a failure occurred during the check operation, leave
3011 * STRIPE_INSYNC not set and let the stripe be handled again
3016 /* handle a successful check operation, if parity is correct
3017 * we are done. Otherwise update the mismatch count and repair
3018 * parity if !MD_RECOVERY_CHECK
3020 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
3021 /* parity is correct (on disc,
3022 * not in buffer any more)
3024 set_bit(STRIPE_INSYNC, &sh->state);
3026 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
3027 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3028 /* don't try to repair!! */
3029 set_bit(STRIPE_INSYNC, &sh->state);
3031 sh->check_state = check_state_compute_run;
3032 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3033 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3034 set_bit(R5_Wantcompute,
3035 &sh->dev[sh->pd_idx].flags);
3036 sh->ops.target = sh->pd_idx;
3037 sh->ops.target2 = -1;
3042 case check_state_compute_run:
3045 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3046 __func__, sh->check_state,
3047 (unsigned long long) sh->sector);
3053 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
3054 struct stripe_head_state *s,
3057 int pd_idx = sh->pd_idx;
3058 int qd_idx = sh->qd_idx;
3061 set_bit(STRIPE_HANDLE, &sh->state);
3063 BUG_ON(s->failed > 2);
3065 /* Want to check and possibly repair P and Q.
3066 * However there could be one 'failed' device, in which
3067 * case we can only check one of them, possibly using the
3068 * other to generate missing data
3071 switch (sh->check_state) {
3072 case check_state_idle:
3073 /* start a new check operation if there are < 2 failures */
3074 if (s->failed == s->q_failed) {
3075 /* The only possible failed device holds Q, so it
3076 * makes sense to check P (If anything else were failed,
3077 * we would have used P to recreate it).
3079 sh->check_state = check_state_run;
3081 if (!s->q_failed && s->failed < 2) {
3082 /* Q is not failed, and we didn't use it to generate
3083 * anything, so it makes sense to check it
3085 if (sh->check_state == check_state_run)
3086 sh->check_state = check_state_run_pq;
3088 sh->check_state = check_state_run_q;
3091 /* discard potentially stale zero_sum_result */
3092 sh->ops.zero_sum_result = 0;
3094 if (sh->check_state == check_state_run) {
3095 /* async_xor_zero_sum destroys the contents of P */
3096 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3099 if (sh->check_state >= check_state_run &&
3100 sh->check_state <= check_state_run_pq) {
3101 /* async_syndrome_zero_sum preserves P and Q, so
3102 * no need to mark them !uptodate here
3104 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3108 /* we have 2-disk failure */
3109 BUG_ON(s->failed != 2);
3111 case check_state_compute_result:
3112 sh->check_state = check_state_idle;
3114 /* check that a write has not made the stripe insync */
3115 if (test_bit(STRIPE_INSYNC, &sh->state))
3118 /* now write out any block on a failed drive,
3119 * or P or Q if they were recomputed
3121 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
3122 if (s->failed == 2) {
3123 dev = &sh->dev[s->failed_num[1]];
3125 set_bit(R5_LOCKED, &dev->flags);
3126 set_bit(R5_Wantwrite, &dev->flags);
3128 if (s->failed >= 1) {
3129 dev = &sh->dev[s->failed_num[0]];
3131 set_bit(R5_LOCKED, &dev->flags);
3132 set_bit(R5_Wantwrite, &dev->flags);
3134 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3135 dev = &sh->dev[pd_idx];
3137 set_bit(R5_LOCKED, &dev->flags);
3138 set_bit(R5_Wantwrite, &dev->flags);
3140 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3141 dev = &sh->dev[qd_idx];
3143 set_bit(R5_LOCKED, &dev->flags);
3144 set_bit(R5_Wantwrite, &dev->flags);
3146 clear_bit(STRIPE_DEGRADED, &sh->state);
3148 set_bit(STRIPE_INSYNC, &sh->state);
3150 case check_state_run:
3151 case check_state_run_q:
3152 case check_state_run_pq:
3153 break; /* we will be called again upon completion */
3154 case check_state_check_result:
3155 sh->check_state = check_state_idle;
3157 /* handle a successful check operation, if parity is correct
3158 * we are done. Otherwise update the mismatch count and repair
3159 * parity if !MD_RECOVERY_CHECK
3161 if (sh->ops.zero_sum_result == 0) {
3162 /* both parities are correct */
3164 set_bit(STRIPE_INSYNC, &sh->state);
3166 /* in contrast to the raid5 case we can validate
3167 * parity, but still have a failure to write
3170 sh->check_state = check_state_compute_result;
3171 /* Returning at this point means that we may go
3172 * off and bring p and/or q uptodate again so
3173 * we make sure to check zero_sum_result again
3174 * to verify if p or q need writeback
3178 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
3179 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3180 /* don't try to repair!! */
3181 set_bit(STRIPE_INSYNC, &sh->state);
3183 int *target = &sh->ops.target;
3185 sh->ops.target = -1;
3186 sh->ops.target2 = -1;
3187 sh->check_state = check_state_compute_run;
3188 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3189 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3190 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3191 set_bit(R5_Wantcompute,
3192 &sh->dev[pd_idx].flags);
3194 target = &sh->ops.target2;
3197 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3198 set_bit(R5_Wantcompute,
3199 &sh->dev[qd_idx].flags);
3206 case check_state_compute_run:
3209 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3210 __func__, sh->check_state,
3211 (unsigned long long) sh->sector);
3216 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
3220 /* We have read all the blocks in this stripe and now we need to
3221 * copy some of them into a target stripe for expand.
3223 struct dma_async_tx_descriptor *tx = NULL;
3224 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3225 for (i = 0; i < sh->disks; i++)
3226 if (i != sh->pd_idx && i != sh->qd_idx) {
3228 struct stripe_head *sh2;
3229 struct async_submit_ctl submit;
3231 sector_t bn = compute_blocknr(sh, i, 1);
3232 sector_t s = raid5_compute_sector(conf, bn, 0,
3234 sh2 = get_active_stripe(conf, s, 0, 1, 1);
3236 /* so far only the early blocks of this stripe
3237 * have been requested. When later blocks
3238 * get requested, we will try again
3241 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3242 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3243 /* must have already done this block */
3244 release_stripe(sh2);
3248 /* place all the copies on one channel */
3249 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
3250 tx = async_memcpy(sh2->dev[dd_idx].page,
3251 sh->dev[i].page, 0, 0, STRIPE_SIZE,
3254 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3255 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3256 for (j = 0; j < conf->raid_disks; j++)
3257 if (j != sh2->pd_idx &&
3259 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3261 if (j == conf->raid_disks) {
3262 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3263 set_bit(STRIPE_HANDLE, &sh2->state);
3265 release_stripe(sh2);
3268 /* done submitting copies, wait for them to complete */
3269 async_tx_quiesce(&tx);
3273 * handle_stripe - do things to a stripe.
3275 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3276 * state of various bits to see what needs to be done.
3278 * return some read requests which now have data
3279 * return some write requests which are safely on storage
3280 * schedule a read on some buffers
3281 * schedule a write of some buffers
3282 * return confirmation of parity correctness
3286 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
3288 struct r5conf *conf = sh->raid_conf;
3289 int disks = sh->disks;
3292 int do_recovery = 0;
3294 memset(s, 0, sizeof(*s));
3296 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3297 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3298 s->failed_num[0] = -1;
3299 s->failed_num[1] = -1;
3301 /* Now to look around and see what can be done */
3303 for (i=disks; i--; ) {
3304 struct md_rdev *rdev;
3311 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3313 dev->toread, dev->towrite, dev->written);
3314 /* maybe we can reply to a read
3316 * new wantfill requests are only permitted while
3317 * ops_complete_biofill is guaranteed to be inactive
3319 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3320 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3321 set_bit(R5_Wantfill, &dev->flags);
3323 /* now count some things */
3324 if (test_bit(R5_LOCKED, &dev->flags))
3326 if (test_bit(R5_UPTODATE, &dev->flags))
3328 if (test_bit(R5_Wantcompute, &dev->flags)) {
3330 BUG_ON(s->compute > 2);
3333 if (test_bit(R5_Wantfill, &dev->flags))
3335 else if (dev->toread)
3339 if (!test_bit(R5_OVERWRITE, &dev->flags))
3344 /* Prefer to use the replacement for reads, but only
3345 * if it is recovered enough and has no bad blocks.
3347 rdev = rcu_dereference(conf->disks[i].replacement);
3348 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3349 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3350 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3351 &first_bad, &bad_sectors))
3352 set_bit(R5_ReadRepl, &dev->flags);
3355 set_bit(R5_NeedReplace, &dev->flags);
3356 rdev = rcu_dereference(conf->disks[i].rdev);
3357 clear_bit(R5_ReadRepl, &dev->flags);
3359 if (rdev && test_bit(Faulty, &rdev->flags))
3362 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3363 &first_bad, &bad_sectors);
3364 if (s->blocked_rdev == NULL
3365 && (test_bit(Blocked, &rdev->flags)
3368 set_bit(BlockedBadBlocks,
3370 s->blocked_rdev = rdev;
3371 atomic_inc(&rdev->nr_pending);
3374 clear_bit(R5_Insync, &dev->flags);
3378 /* also not in-sync */
3379 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3380 test_bit(R5_UPTODATE, &dev->flags)) {
3381 /* treat as in-sync, but with a read error
3382 * which we can now try to correct
3384 set_bit(R5_Insync, &dev->flags);
3385 set_bit(R5_ReadError, &dev->flags);
3387 } else if (test_bit(In_sync, &rdev->flags))
3388 set_bit(R5_Insync, &dev->flags);
3389 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3390 /* in sync if before recovery_offset */
3391 set_bit(R5_Insync, &dev->flags);
3392 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3393 test_bit(R5_Expanded, &dev->flags))
3394 /* If we've reshaped into here, we assume it is Insync.
3395 * We will shortly update recovery_offset to make
3398 set_bit(R5_Insync, &dev->flags);
3400 if (test_bit(R5_WriteError, &dev->flags)) {
3401 /* This flag does not apply to '.replacement'
3402 * only to .rdev, so make sure to check that*/
3403 struct md_rdev *rdev2 = rcu_dereference(
3404 conf->disks[i].rdev);
3406 clear_bit(R5_Insync, &dev->flags);
3407 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3408 s->handle_bad_blocks = 1;
3409 atomic_inc(&rdev2->nr_pending);
3411 clear_bit(R5_WriteError, &dev->flags);
3413 if (test_bit(R5_MadeGood, &dev->flags)) {
3414 /* This flag does not apply to '.replacement'
3415 * only to .rdev, so make sure to check that*/
3416 struct md_rdev *rdev2 = rcu_dereference(
3417 conf->disks[i].rdev);
3418 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3419 s->handle_bad_blocks = 1;
3420 atomic_inc(&rdev2->nr_pending);
3422 clear_bit(R5_MadeGood, &dev->flags);
3424 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3425 struct md_rdev *rdev2 = rcu_dereference(
3426 conf->disks[i].replacement);
3427 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3428 s->handle_bad_blocks = 1;
3429 atomic_inc(&rdev2->nr_pending);
3431 clear_bit(R5_MadeGoodRepl, &dev->flags);
3433 if (!test_bit(R5_Insync, &dev->flags)) {
3434 /* The ReadError flag will just be confusing now */
3435 clear_bit(R5_ReadError, &dev->flags);
3436 clear_bit(R5_ReWrite, &dev->flags);
3438 if (test_bit(R5_ReadError, &dev->flags))
3439 clear_bit(R5_Insync, &dev->flags);
3440 if (!test_bit(R5_Insync, &dev->flags)) {
3442 s->failed_num[s->failed] = i;
3444 if (rdev && !test_bit(Faulty, &rdev->flags))
3448 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3449 /* If there is a failed device being replaced,
3450 * we must be recovering.
3451 * else if we are after recovery_cp, we must be syncing
3452 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3453 * else we can only be replacing
3454 * sync and recovery both need to read all devices, and so
3455 * use the same flag.
3458 sh->sector >= conf->mddev->recovery_cp ||
3459 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
3467 static void handle_stripe(struct stripe_head *sh)
3469 struct stripe_head_state s;
3470 struct r5conf *conf = sh->raid_conf;
3473 int disks = sh->disks;
3474 struct r5dev *pdev, *qdev;
3476 clear_bit(STRIPE_HANDLE, &sh->state);
3477 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
3478 /* already being handled, ensure it gets handled
3479 * again when current action finishes */
3480 set_bit(STRIPE_HANDLE, &sh->state);
3484 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3485 spin_lock(&sh->stripe_lock);
3486 /* Cannot process 'sync' concurrently with 'discard' */
3487 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3488 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3489 set_bit(STRIPE_SYNCING, &sh->state);
3490 clear_bit(STRIPE_INSYNC, &sh->state);
3491 clear_bit(STRIPE_REPLACED, &sh->state);
3493 spin_unlock(&sh->stripe_lock);
3495 clear_bit(STRIPE_DELAYED, &sh->state);
3497 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3498 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3499 (unsigned long long)sh->sector, sh->state,
3500 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3501 sh->check_state, sh->reconstruct_state);
3503 analyse_stripe(sh, &s);
3505 if (s.handle_bad_blocks) {
3506 set_bit(STRIPE_HANDLE, &sh->state);
3510 if (unlikely(s.blocked_rdev)) {
3511 if (s.syncing || s.expanding || s.expanded ||
3512 s.replacing || s.to_write || s.written) {
3513 set_bit(STRIPE_HANDLE, &sh->state);
3516 /* There is nothing for the blocked_rdev to block */
3517 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3518 s.blocked_rdev = NULL;
3521 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3522 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3523 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3526 pr_debug("locked=%d uptodate=%d to_read=%d"
3527 " to_write=%d failed=%d failed_num=%d,%d\n",
3528 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3529 s.failed_num[0], s.failed_num[1]);
3530 /* check if the array has lost more than max_degraded devices and,
3531 * if so, some requests might need to be failed.
3533 if (s.failed > conf->max_degraded) {
3534 sh->check_state = 0;
3535 sh->reconstruct_state = 0;
3536 if (s.to_read+s.to_write+s.written)
3537 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3538 if (s.syncing + s.replacing)
3539 handle_failed_sync(conf, sh, &s);
3542 /* Now we check to see if any write operations have recently
3546 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3548 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3549 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3550 sh->reconstruct_state = reconstruct_state_idle;
3552 /* All the 'written' buffers and the parity block are ready to
3553 * be written back to disk
3555 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3556 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
3557 BUG_ON(sh->qd_idx >= 0 &&
3558 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3559 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
3560 for (i = disks; i--; ) {
3561 struct r5dev *dev = &sh->dev[i];
3562 if (test_bit(R5_LOCKED, &dev->flags) &&
3563 (i == sh->pd_idx || i == sh->qd_idx ||
3565 pr_debug("Writing block %d\n", i);
3566 set_bit(R5_Wantwrite, &dev->flags);
3571 if (!test_bit(R5_Insync, &dev->flags) ||
3572 ((i == sh->pd_idx || i == sh->qd_idx) &&
3574 set_bit(STRIPE_INSYNC, &sh->state);
3577 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3578 s.dec_preread_active = 1;
3582 * might be able to return some write requests if the parity blocks
3583 * are safe, or on a failed drive
3585 pdev = &sh->dev[sh->pd_idx];
3586 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3587 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3588 qdev = &sh->dev[sh->qd_idx];
3589 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3590 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3594 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3595 && !test_bit(R5_LOCKED, &pdev->flags)
3596 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3597 test_bit(R5_Discard, &pdev->flags))))) &&
3598 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3599 && !test_bit(R5_LOCKED, &qdev->flags)
3600 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3601 test_bit(R5_Discard, &qdev->flags))))))
3602 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3604 /* Now we might consider reading some blocks, either to check/generate
3605 * parity, or to satisfy requests
3606 * or to load a block that is being partially written.
3608 if (s.to_read || s.non_overwrite
3609 || (conf->level == 6 && s.to_write && s.failed)
3610 || (s.syncing && (s.uptodate + s.compute < disks))
3613 handle_stripe_fill(sh, &s, disks);
3615 /* Now to consider new write requests and what else, if anything
3616 * should be read. We do not handle new writes when:
3617 * 1/ A 'write' operation (copy+xor) is already in flight.
3618 * 2/ A 'check' operation is in flight, as it may clobber the parity
3621 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3622 handle_stripe_dirtying(conf, sh, &s, disks);
3624 /* maybe we need to check and possibly fix the parity for this stripe
3625 * Any reads will already have been scheduled, so we just see if enough
3626 * data is available. The parity check is held off while parity
3627 * dependent operations are in flight.
3629 if (sh->check_state ||
3630 (s.syncing && s.locked == 0 &&
3631 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3632 !test_bit(STRIPE_INSYNC, &sh->state))) {
3633 if (conf->level == 6)
3634 handle_parity_checks6(conf, sh, &s, disks);
3636 handle_parity_checks5(conf, sh, &s, disks);
3639 if ((s.replacing || s.syncing) && s.locked == 0
3640 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3641 && !test_bit(STRIPE_REPLACED, &sh->state)) {
3642 /* Write out to replacement devices where possible */
3643 for (i = 0; i < conf->raid_disks; i++)
3644 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3645 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
3646 set_bit(R5_WantReplace, &sh->dev[i].flags);
3647 set_bit(R5_LOCKED, &sh->dev[i].flags);
3651 set_bit(STRIPE_INSYNC, &sh->state);
3652 set_bit(STRIPE_REPLACED, &sh->state);
3654 if ((s.syncing || s.replacing) && s.locked == 0 &&
3655 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3656 test_bit(STRIPE_INSYNC, &sh->state)) {
3657 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3658 clear_bit(STRIPE_SYNCING, &sh->state);
3659 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3660 wake_up(&conf->wait_for_overlap);
3663 /* If the failed drives are just a ReadError, then we might need
3664 * to progress the repair/check process
3666 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3667 for (i = 0; i < s.failed; i++) {
3668 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3669 if (test_bit(R5_ReadError, &dev->flags)
3670 && !test_bit(R5_LOCKED, &dev->flags)
3671 && test_bit(R5_UPTODATE, &dev->flags)
3673 if (!test_bit(R5_ReWrite, &dev->flags)) {
3674 set_bit(R5_Wantwrite, &dev->flags);
3675 set_bit(R5_ReWrite, &dev->flags);
3676 set_bit(R5_LOCKED, &dev->flags);
3679 /* let's read it back */
3680 set_bit(R5_Wantread, &dev->flags);
3681 set_bit(R5_LOCKED, &dev->flags);
3688 /* Finish reconstruct operations initiated by the expansion process */
3689 if (sh->reconstruct_state == reconstruct_state_result) {
3690 struct stripe_head *sh_src
3691 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3692 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3693 /* sh cannot be written until sh_src has been read.
3694 * so arrange for sh to be delayed a little
3696 set_bit(STRIPE_DELAYED, &sh->state);
3697 set_bit(STRIPE_HANDLE, &sh->state);
3698 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3700 atomic_inc(&conf->preread_active_stripes);
3701 release_stripe(sh_src);
3705 release_stripe(sh_src);
3707 sh->reconstruct_state = reconstruct_state_idle;
3708 clear_bit(STRIPE_EXPANDING, &sh->state);
3709 for (i = conf->raid_disks; i--; ) {
3710 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3711 set_bit(R5_LOCKED, &sh->dev[i].flags);
3716 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3717 !sh->reconstruct_state) {
3718 /* Need to write out all blocks after computing parity */
3719 sh->disks = conf->raid_disks;
3720 stripe_set_idx(sh->sector, conf, 0, sh);
3721 schedule_reconstruction(sh, &s, 1, 1);
3722 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3723 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3724 atomic_dec(&conf->reshape_stripes);
3725 wake_up(&conf->wait_for_overlap);
3726 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3729 if (s.expanding && s.locked == 0 &&
3730 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3731 handle_stripe_expansion(conf, sh);
3734 /* wait for this device to become unblocked */
3735 if (unlikely(s.blocked_rdev)) {
3736 if (conf->mddev->external)
3737 md_wait_for_blocked_rdev(s.blocked_rdev,
3740 /* Internal metadata will immediately
3741 * be written by raid5d, so we don't
3742 * need to wait here.
3744 rdev_dec_pending(s.blocked_rdev,
3748 if (s.handle_bad_blocks)
3749 for (i = disks; i--; ) {
3750 struct md_rdev *rdev;
3751 struct r5dev *dev = &sh->dev[i];
3752 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3753 /* We own a safe reference to the rdev */
3754 rdev = conf->disks[i].rdev;
3755 if (!rdev_set_badblocks(rdev, sh->sector,
3757 md_error(conf->mddev, rdev);
3758 rdev_dec_pending(rdev, conf->mddev);
3760 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3761 rdev = conf->disks[i].rdev;
3762 rdev_clear_badblocks(rdev, sh->sector,
3764 rdev_dec_pending(rdev, conf->mddev);
3766 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3767 rdev = conf->disks[i].replacement;
3769 /* rdev have been moved down */
3770 rdev = conf->disks[i].rdev;
3771 rdev_clear_badblocks(rdev, sh->sector,
3773 rdev_dec_pending(rdev, conf->mddev);
3778 raid_run_ops(sh, s.ops_request);
3782 if (s.dec_preread_active) {
3783 /* We delay this until after ops_run_io so that if make_request
3784 * is waiting on a flush, it won't continue until the writes
3785 * have actually been submitted.
3787 atomic_dec(&conf->preread_active_stripes);
3788 if (atomic_read(&conf->preread_active_stripes) <
3790 md_wakeup_thread(conf->mddev->thread);
3793 return_io(s.return_bi);
3795 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
3798 static void raid5_activate_delayed(struct r5conf *conf)
3800 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3801 while (!list_empty(&conf->delayed_list)) {
3802 struct list_head *l = conf->delayed_list.next;
3803 struct stripe_head *sh;
3804 sh = list_entry(l, struct stripe_head, lru);
3806 clear_bit(STRIPE_DELAYED, &sh->state);
3807 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3808 atomic_inc(&conf->preread_active_stripes);
3809 list_add_tail(&sh->lru, &conf->hold_list);
3814 static void activate_bit_delay(struct r5conf *conf)
3816 /* device_lock is held */
3817 struct list_head head;
3818 list_add(&head, &conf->bitmap_list);
3819 list_del_init(&conf->bitmap_list);
3820 while (!list_empty(&head)) {
3821 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3822 list_del_init(&sh->lru);
3823 atomic_inc(&sh->count);
3824 __release_stripe(conf, sh);
3828 int md_raid5_congested(struct mddev *mddev, int bits)
3830 struct r5conf *conf = mddev->private;
3832 /* No difference between reads and writes. Just check
3833 * how busy the stripe_cache is
3836 if (conf->inactive_blocked)
3840 if (list_empty_careful(&conf->inactive_list))
3845 EXPORT_SYMBOL_GPL(md_raid5_congested);
3847 static int raid5_congested(void *data, int bits)
3849 struct mddev *mddev = data;
3851 return mddev_congested(mddev, bits) ||
3852 md_raid5_congested(mddev, bits);
3855 /* We want read requests to align with chunks where possible,
3856 * but write requests don't need to.
3858 static int raid5_mergeable_bvec(struct request_queue *q,
3859 struct bvec_merge_data *bvm,
3860 struct bio_vec *biovec)
3862 struct mddev *mddev = q->queuedata;
3863 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
3865 unsigned int chunk_sectors = mddev->chunk_sectors;
3866 unsigned int bio_sectors = bvm->bi_size >> 9;
3868 if ((bvm->bi_rw & 1) == WRITE)
3869 return biovec->bv_len; /* always allow writes to be mergeable */
3871 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3872 chunk_sectors = mddev->new_chunk_sectors;
3873 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3874 if (max < 0) max = 0;
3875 if (max <= biovec->bv_len && bio_sectors == 0)
3876 return biovec->bv_len;
3882 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
3884 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3885 unsigned int chunk_sectors = mddev->chunk_sectors;
3886 unsigned int bio_sectors = bio_sectors(bio);
3888 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3889 chunk_sectors = mddev->new_chunk_sectors;
3890 return chunk_sectors >=
3891 ((sector & (chunk_sectors - 1)) + bio_sectors);
3895 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3896 * later sampled by raid5d.
3898 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
3900 unsigned long flags;
3902 spin_lock_irqsave(&conf->device_lock, flags);
3904 bi->bi_next = conf->retry_read_aligned_list;
3905 conf->retry_read_aligned_list = bi;
3907 spin_unlock_irqrestore(&conf->device_lock, flags);
3908 md_wakeup_thread(conf->mddev->thread);
3912 static struct bio *remove_bio_from_retry(struct r5conf *conf)
3916 bi = conf->retry_read_aligned;
3918 conf->retry_read_aligned = NULL;
3921 bi = conf->retry_read_aligned_list;
3923 conf->retry_read_aligned_list = bi->bi_next;
3926 * this sets the active strip count to 1 and the processed
3927 * strip count to zero (upper 8 bits)
3929 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
3937 * The "raid5_align_endio" should check if the read succeeded and if it
3938 * did, call bio_endio on the original bio (having bio_put the new bio
3940 * If the read failed..
3942 static void raid5_align_endio(struct bio *bi, int error)
3944 struct bio* raid_bi = bi->bi_private;
3945 struct mddev *mddev;
3946 struct r5conf *conf;
3947 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3948 struct md_rdev *rdev;
3952 rdev = (void*)raid_bi->bi_next;
3953 raid_bi->bi_next = NULL;
3954 mddev = rdev->mddev;
3955 conf = mddev->private;
3957 rdev_dec_pending(rdev, conf->mddev);
3959 if (!error && uptodate) {
3960 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3962 bio_endio(raid_bi, 0);
3963 if (atomic_dec_and_test(&conf->active_aligned_reads))
3964 wake_up(&conf->wait_for_stripe);
3969 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3971 add_bio_to_retry(raid_bi, conf);
3974 static int bio_fits_rdev(struct bio *bi)
3976 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
3978 if (bio_sectors(bi) > queue_max_sectors(q))
3980 blk_recount_segments(q, bi);
3981 if (bi->bi_phys_segments > queue_max_segments(q))
3984 if (q->merge_bvec_fn)
3985 /* it's too hard to apply the merge_bvec_fn at this stage,
3994 static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
3996 struct r5conf *conf = mddev->private;
3998 struct bio* align_bi;
3999 struct md_rdev *rdev;
4000 sector_t end_sector;
4002 if (!in_chunk_boundary(mddev, raid_bio)) {
4003 pr_debug("chunk_aligned_read : non aligned\n");
4007 * use bio_clone_mddev to make a copy of the bio
4009 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
4013 * set bi_end_io to a new function, and set bi_private to the
4016 align_bi->bi_end_io = raid5_align_endio;
4017 align_bi->bi_private = raid_bio;
4021 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4025 end_sector = bio_end_sector(align_bi);
4027 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4028 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4029 rdev->recovery_offset < end_sector) {
4030 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4032 (test_bit(Faulty, &rdev->flags) ||
4033 !(test_bit(In_sync, &rdev->flags) ||
4034 rdev->recovery_offset >= end_sector)))
4041 atomic_inc(&rdev->nr_pending);
4043 raid_bio->bi_next = (void*)rdev;
4044 align_bi->bi_bdev = rdev->bdev;
4045 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
4047 if (!bio_fits_rdev(align_bi) ||
4048 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
4049 &first_bad, &bad_sectors)) {
4050 /* too big in some way, or has a known bad block */
4052 rdev_dec_pending(rdev, mddev);
4056 /* No reshape active, so we can trust rdev->data_offset */
4057 align_bi->bi_sector += rdev->data_offset;
4059 spin_lock_irq(&conf->device_lock);
4060 wait_event_lock_irq(conf->wait_for_stripe,
4063 atomic_inc(&conf->active_aligned_reads);
4064 spin_unlock_irq(&conf->device_lock);
4067 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4068 align_bi, disk_devt(mddev->gendisk),
4069 raid_bio->bi_sector);
4070 generic_make_request(align_bi);
4079 /* __get_priority_stripe - get the next stripe to process
4081 * Full stripe writes are allowed to pass preread active stripes up until
4082 * the bypass_threshold is exceeded. In general the bypass_count
4083 * increments when the handle_list is handled before the hold_list; however, it
4084 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4085 * stripe with in flight i/o. The bypass_count will be reset when the
4086 * head of the hold_list has changed, i.e. the head was promoted to the
4089 static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
4091 struct stripe_head *sh;
4093 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4095 list_empty(&conf->handle_list) ? "empty" : "busy",
4096 list_empty(&conf->hold_list) ? "empty" : "busy",
4097 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4099 if (!list_empty(&conf->handle_list)) {
4100 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4102 if (list_empty(&conf->hold_list))
4103 conf->bypass_count = 0;
4104 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4105 if (conf->hold_list.next == conf->last_hold)
4106 conf->bypass_count++;
4108 conf->last_hold = conf->hold_list.next;
4109 conf->bypass_count -= conf->bypass_threshold;
4110 if (conf->bypass_count < 0)
4111 conf->bypass_count = 0;
4114 } else if (!list_empty(&conf->hold_list) &&
4115 ((conf->bypass_threshold &&
4116 conf->bypass_count > conf->bypass_threshold) ||
4117 atomic_read(&conf->pending_full_writes) == 0)) {
4118 sh = list_entry(conf->hold_list.next,
4120 conf->bypass_count -= conf->bypass_threshold;
4121 if (conf->bypass_count < 0)
4122 conf->bypass_count = 0;
4126 list_del_init(&sh->lru);
4127 atomic_inc(&sh->count);
4128 BUG_ON(atomic_read(&sh->count) != 1);
4132 struct raid5_plug_cb {
4133 struct blk_plug_cb cb;
4134 struct list_head list;
4137 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4139 struct raid5_plug_cb *cb = container_of(
4140 blk_cb, struct raid5_plug_cb, cb);
4141 struct stripe_head *sh;
4142 struct mddev *mddev = cb->cb.data;
4143 struct r5conf *conf = mddev->private;
4146 if (cb->list.next && !list_empty(&cb->list)) {
4147 spin_lock_irq(&conf->device_lock);
4148 while (!list_empty(&cb->list)) {
4149 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4150 list_del_init(&sh->lru);
4152 * avoid race release_stripe_plug() sees
4153 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4154 * is still in our list
4156 smp_mb__before_clear_bit();
4157 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4158 __release_stripe(conf, sh);
4161 spin_unlock_irq(&conf->device_lock);
4164 trace_block_unplug(mddev->queue, cnt, !from_schedule);
4168 static void release_stripe_plug(struct mddev *mddev,
4169 struct stripe_head *sh)
4171 struct blk_plug_cb *blk_cb = blk_check_plugged(
4172 raid5_unplug, mddev,
4173 sizeof(struct raid5_plug_cb));
4174 struct raid5_plug_cb *cb;
4181 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4183 if (cb->list.next == NULL)
4184 INIT_LIST_HEAD(&cb->list);
4186 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4187 list_add_tail(&sh->lru, &cb->list);
4192 static void make_discard_request(struct mddev *mddev, struct bio *bi)
4194 struct r5conf *conf = mddev->private;
4195 sector_t logical_sector, last_sector;
4196 struct stripe_head *sh;
4200 if (mddev->reshape_position != MaxSector)
4201 /* Skip discard while reshape is happening */
4204 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4205 last_sector = bi->bi_sector + (bi->bi_size>>9);
4208 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4210 stripe_sectors = conf->chunk_sectors *
4211 (conf->raid_disks - conf->max_degraded);
4212 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4214 sector_div(last_sector, stripe_sectors);
4216 logical_sector *= conf->chunk_sectors;
4217 last_sector *= conf->chunk_sectors;
4219 for (; logical_sector < last_sector;
4220 logical_sector += STRIPE_SECTORS) {
4224 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4225 prepare_to_wait(&conf->wait_for_overlap, &w,
4226 TASK_UNINTERRUPTIBLE);
4227 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4228 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4233 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4234 spin_lock_irq(&sh->stripe_lock);
4235 for (d = 0; d < conf->raid_disks; d++) {
4236 if (d == sh->pd_idx || d == sh->qd_idx)
4238 if (sh->dev[d].towrite || sh->dev[d].toread) {
4239 set_bit(R5_Overlap, &sh->dev[d].flags);
4240 spin_unlock_irq(&sh->stripe_lock);
4246 set_bit(STRIPE_DISCARD, &sh->state);
4247 finish_wait(&conf->wait_for_overlap, &w);
4248 for (d = 0; d < conf->raid_disks; d++) {
4249 if (d == sh->pd_idx || d == sh->qd_idx)
4251 sh->dev[d].towrite = bi;
4252 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4253 raid5_inc_bi_active_stripes(bi);
4255 spin_unlock_irq(&sh->stripe_lock);
4256 if (conf->mddev->bitmap) {
4258 d < conf->raid_disks - conf->max_degraded;
4260 bitmap_startwrite(mddev->bitmap,
4264 sh->bm_seq = conf->seq_flush + 1;
4265 set_bit(STRIPE_BIT_DELAY, &sh->state);
4268 set_bit(STRIPE_HANDLE, &sh->state);
4269 clear_bit(STRIPE_DELAYED, &sh->state);
4270 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4271 atomic_inc(&conf->preread_active_stripes);
4272 release_stripe_plug(mddev, sh);
4275 remaining = raid5_dec_bi_active_stripes(bi);
4276 if (remaining == 0) {
4277 md_write_end(mddev);
4282 static void make_request(struct mddev *mddev, struct bio * bi)
4284 struct r5conf *conf = mddev->private;
4286 sector_t new_sector;
4287 sector_t logical_sector, last_sector;
4288 struct stripe_head *sh;
4289 const int rw = bio_data_dir(bi);
4292 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4293 md_flush_request(mddev, bi);
4297 md_write_start(mddev, bi);
4300 mddev->reshape_position == MaxSector &&
4301 chunk_aligned_read(mddev,bi))
4304 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4305 make_discard_request(mddev, bi);
4309 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4310 last_sector = bio_end_sector(bi);
4312 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4314 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4320 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
4321 if (unlikely(conf->reshape_progress != MaxSector)) {
4322 /* spinlock is needed as reshape_progress may be
4323 * 64bit on a 32bit platform, and so it might be
4324 * possible to see a half-updated value
4325 * Of course reshape_progress could change after
4326 * the lock is dropped, so once we get a reference
4327 * to the stripe that we think it is, we will have
4330 spin_lock_irq(&conf->device_lock);
4331 if (mddev->reshape_backwards
4332 ? logical_sector < conf->reshape_progress
4333 : logical_sector >= conf->reshape_progress) {
4336 if (mddev->reshape_backwards
4337 ? logical_sector < conf->reshape_safe
4338 : logical_sector >= conf->reshape_safe) {
4339 spin_unlock_irq(&conf->device_lock);
4344 spin_unlock_irq(&conf->device_lock);
4347 new_sector = raid5_compute_sector(conf, logical_sector,
4350 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4351 (unsigned long long)new_sector,
4352 (unsigned long long)logical_sector);
4354 sh = get_active_stripe(conf, new_sector, previous,
4355 (bi->bi_rw&RWA_MASK), 0);
4357 if (unlikely(previous)) {
4358 /* expansion might have moved on while waiting for a
4359 * stripe, so we must do the range check again.
4360 * Expansion could still move past after this
4361 * test, but as we are holding a reference to
4362 * 'sh', we know that if that happens,
4363 * STRIPE_EXPANDING will get set and the expansion
4364 * won't proceed until we finish with the stripe.
4367 spin_lock_irq(&conf->device_lock);
4368 if (mddev->reshape_backwards
4369 ? logical_sector >= conf->reshape_progress
4370 : logical_sector < conf->reshape_progress)
4371 /* mismatch, need to try again */
4373 spin_unlock_irq(&conf->device_lock);
4382 logical_sector >= mddev->suspend_lo &&
4383 logical_sector < mddev->suspend_hi) {
4385 /* As the suspend_* range is controlled by
4386 * userspace, we want an interruptible
4389 flush_signals(current);
4390 prepare_to_wait(&conf->wait_for_overlap,
4391 &w, TASK_INTERRUPTIBLE);
4392 if (logical_sector >= mddev->suspend_lo &&
4393 logical_sector < mddev->suspend_hi)
4398 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
4399 !add_stripe_bio(sh, bi, dd_idx, rw)) {
4400 /* Stripe is busy expanding or
4401 * add failed due to overlap. Flush everything
4404 md_wakeup_thread(mddev->thread);
4409 finish_wait(&conf->wait_for_overlap, &w);
4410 set_bit(STRIPE_HANDLE, &sh->state);
4411 clear_bit(STRIPE_DELAYED, &sh->state);
4412 if ((bi->bi_rw & REQ_SYNC) &&
4413 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4414 atomic_inc(&conf->preread_active_stripes);
4415 release_stripe_plug(mddev, sh);
4417 /* cannot get stripe for read-ahead, just give-up */
4418 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4419 finish_wait(&conf->wait_for_overlap, &w);
4424 remaining = raid5_dec_bi_active_stripes(bi);
4425 if (remaining == 0) {
4428 md_write_end(mddev);
4430 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4436 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
4438 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
4440 /* reshaping is quite different to recovery/resync so it is
4441 * handled quite separately ... here.
4443 * On each call to sync_request, we gather one chunk worth of
4444 * destination stripes and flag them as expanding.
4445 * Then we find all the source stripes and request reads.
4446 * As the reads complete, handle_stripe will copy the data
4447 * into the destination stripe and release that stripe.
4449 struct r5conf *conf = mddev->private;
4450 struct stripe_head *sh;
4451 sector_t first_sector, last_sector;
4452 int raid_disks = conf->previous_raid_disks;
4453 int data_disks = raid_disks - conf->max_degraded;
4454 int new_data_disks = conf->raid_disks - conf->max_degraded;
4457 sector_t writepos, readpos, safepos;
4458 sector_t stripe_addr;
4459 int reshape_sectors;
4460 struct list_head stripes;
4462 if (sector_nr == 0) {
4463 /* If restarting in the middle, skip the initial sectors */
4464 if (mddev->reshape_backwards &&
4465 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4466 sector_nr = raid5_size(mddev, 0, 0)
4467 - conf->reshape_progress;
4468 } else if (!mddev->reshape_backwards &&
4469 conf->reshape_progress > 0)
4470 sector_nr = conf->reshape_progress;
4471 sector_div(sector_nr, new_data_disks);
4473 mddev->curr_resync_completed = sector_nr;
4474 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4480 /* We need to process a full chunk at a time.
4481 * If old and new chunk sizes differ, we need to process the
4484 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4485 reshape_sectors = mddev->new_chunk_sectors;
4487 reshape_sectors = mddev->chunk_sectors;
4489 /* We update the metadata at least every 10 seconds, or when
4490 * the data about to be copied would over-write the source of
4491 * the data at the front of the range. i.e. one new_stripe
4492 * along from reshape_progress new_maps to after where
4493 * reshape_safe old_maps to
4495 writepos = conf->reshape_progress;
4496 sector_div(writepos, new_data_disks);
4497 readpos = conf->reshape_progress;
4498 sector_div(readpos, data_disks);
4499 safepos = conf->reshape_safe;
4500 sector_div(safepos, data_disks);
4501 if (mddev->reshape_backwards) {
4502 writepos -= min_t(sector_t, reshape_sectors, writepos);
4503 readpos += reshape_sectors;
4504 safepos += reshape_sectors;
4506 writepos += reshape_sectors;
4507 readpos -= min_t(sector_t, reshape_sectors, readpos);
4508 safepos -= min_t(sector_t, reshape_sectors, safepos);
4511 /* Having calculated the 'writepos' possibly use it
4512 * to set 'stripe_addr' which is where we will write to.
4514 if (mddev->reshape_backwards) {
4515 BUG_ON(conf->reshape_progress == 0);
4516 stripe_addr = writepos;
4517 BUG_ON((mddev->dev_sectors &
4518 ~((sector_t)reshape_sectors - 1))
4519 - reshape_sectors - stripe_addr
4522 BUG_ON(writepos != sector_nr + reshape_sectors);
4523 stripe_addr = sector_nr;
4526 /* 'writepos' is the most advanced device address we might write.
4527 * 'readpos' is the least advanced device address we might read.
4528 * 'safepos' is the least address recorded in the metadata as having
4530 * If there is a min_offset_diff, these are adjusted either by
4531 * increasing the safepos/readpos if diff is negative, or
4532 * increasing writepos if diff is positive.
4533 * If 'readpos' is then behind 'writepos', there is no way that we can
4534 * ensure safety in the face of a crash - that must be done by userspace
4535 * making a backup of the data. So in that case there is no particular
4536 * rush to update metadata.
4537 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4538 * update the metadata to advance 'safepos' to match 'readpos' so that
4539 * we can be safe in the event of a crash.
4540 * So we insist on updating metadata if safepos is behind writepos and
4541 * readpos is beyond writepos.
4542 * In any case, update the metadata every 10 seconds.
4543 * Maybe that number should be configurable, but I'm not sure it is
4544 * worth it.... maybe it could be a multiple of safemode_delay???
4546 if (conf->min_offset_diff < 0) {
4547 safepos += -conf->min_offset_diff;
4548 readpos += -conf->min_offset_diff;
4550 writepos += conf->min_offset_diff;
4552 if ((mddev->reshape_backwards
4553 ? (safepos > writepos && readpos < writepos)
4554 : (safepos < writepos && readpos > writepos)) ||
4555 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4556 /* Cannot proceed until we've updated the superblock... */
4557 wait_event(conf->wait_for_overlap,
4558 atomic_read(&conf->reshape_stripes)==0);
4559 mddev->reshape_position = conf->reshape_progress;
4560 mddev->curr_resync_completed = sector_nr;
4561 conf->reshape_checkpoint = jiffies;
4562 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4563 md_wakeup_thread(mddev->thread);
4564 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4565 kthread_should_stop());
4566 spin_lock_irq(&conf->device_lock);
4567 conf->reshape_safe = mddev->reshape_position;
4568 spin_unlock_irq(&conf->device_lock);
4569 wake_up(&conf->wait_for_overlap);
4570 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4573 INIT_LIST_HEAD(&stripes);
4574 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
4576 int skipped_disk = 0;
4577 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
4578 set_bit(STRIPE_EXPANDING, &sh->state);
4579 atomic_inc(&conf->reshape_stripes);
4580 /* If any of this stripe is beyond the end of the old
4581 * array, then we need to zero those blocks
4583 for (j=sh->disks; j--;) {
4585 if (j == sh->pd_idx)
4587 if (conf->level == 6 &&
4590 s = compute_blocknr(sh, j, 0);
4591 if (s < raid5_size(mddev, 0, 0)) {
4595 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4596 set_bit(R5_Expanded, &sh->dev[j].flags);
4597 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4599 if (!skipped_disk) {
4600 set_bit(STRIPE_EXPAND_READY, &sh->state);
4601 set_bit(STRIPE_HANDLE, &sh->state);
4603 list_add(&sh->lru, &stripes);
4605 spin_lock_irq(&conf->device_lock);
4606 if (mddev->reshape_backwards)
4607 conf->reshape_progress -= reshape_sectors * new_data_disks;
4609 conf->reshape_progress += reshape_sectors * new_data_disks;
4610 spin_unlock_irq(&conf->device_lock);
4611 /* Ok, those stripe are ready. We can start scheduling
4612 * reads on the source stripes.
4613 * The source stripes are determined by mapping the first and last
4614 * block on the destination stripes.
4617 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
4620 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
4621 * new_data_disks - 1),
4623 if (last_sector >= mddev->dev_sectors)
4624 last_sector = mddev->dev_sectors - 1;
4625 while (first_sector <= last_sector) {
4626 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
4627 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4628 set_bit(STRIPE_HANDLE, &sh->state);
4630 first_sector += STRIPE_SECTORS;
4632 /* Now that the sources are clearly marked, we can release
4633 * the destination stripes
4635 while (!list_empty(&stripes)) {
4636 sh = list_entry(stripes.next, struct stripe_head, lru);
4637 list_del_init(&sh->lru);
4640 /* If this takes us to the resync_max point where we have to pause,
4641 * then we need to write out the superblock.
4643 sector_nr += reshape_sectors;
4644 if ((sector_nr - mddev->curr_resync_completed) * 2
4645 >= mddev->resync_max - mddev->curr_resync_completed) {
4646 /* Cannot proceed until we've updated the superblock... */
4647 wait_event(conf->wait_for_overlap,
4648 atomic_read(&conf->reshape_stripes) == 0);
4649 mddev->reshape_position = conf->reshape_progress;
4650 mddev->curr_resync_completed = sector_nr;
4651 conf->reshape_checkpoint = jiffies;
4652 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4653 md_wakeup_thread(mddev->thread);
4654 wait_event(mddev->sb_wait,
4655 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4656 || kthread_should_stop());
4657 spin_lock_irq(&conf->device_lock);
4658 conf->reshape_safe = mddev->reshape_position;
4659 spin_unlock_irq(&conf->device_lock);
4660 wake_up(&conf->wait_for_overlap);
4661 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4663 return reshape_sectors;
4666 /* FIXME go_faster isn't used */
4667 static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
4669 struct r5conf *conf = mddev->private;
4670 struct stripe_head *sh;
4671 sector_t max_sector = mddev->dev_sectors;
4672 sector_t sync_blocks;
4673 int still_degraded = 0;
4676 if (sector_nr >= max_sector) {
4677 /* just being told to finish up .. nothing much to do */
4679 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4684 if (mddev->curr_resync < max_sector) /* aborted */
4685 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4687 else /* completed sync */
4689 bitmap_close_sync(mddev->bitmap);
4694 /* Allow raid5_quiesce to complete */
4695 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4697 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4698 return reshape_request(mddev, sector_nr, skipped);
4700 /* No need to check resync_max as we never do more than one
4701 * stripe, and as resync_max will always be on a chunk boundary,
4702 * if the check in md_do_sync didn't fire, there is no chance
4703 * of overstepping resync_max here
4706 /* if there is too many failed drives and we are trying
4707 * to resync, then assert that we are finished, because there is
4708 * nothing we can do.
4710 if (mddev->degraded >= conf->max_degraded &&
4711 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4712 sector_t rv = mddev->dev_sectors - sector_nr;
4716 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4718 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4719 sync_blocks >= STRIPE_SECTORS) {
4720 /* we can skip this block, and probably more */
4721 sync_blocks /= STRIPE_SECTORS;
4723 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4726 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4728 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
4730 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
4731 /* make sure we don't swamp the stripe cache if someone else
4732 * is trying to get access
4734 schedule_timeout_uninterruptible(1);
4736 /* Need to check if array will still be degraded after recovery/resync
4737 * We don't need to check the 'failed' flag as when that gets set,
4740 for (i = 0; i < conf->raid_disks; i++)
4741 if (conf->disks[i].rdev == NULL)
4744 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4746 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
4751 return STRIPE_SECTORS;
4754 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
4756 /* We may not be able to submit a whole bio at once as there
4757 * may not be enough stripe_heads available.
4758 * We cannot pre-allocate enough stripe_heads as we may need
4759 * more than exist in the cache (if we allow ever large chunks).
4760 * So we do one stripe head at a time and record in
4761 * ->bi_hw_segments how many have been done.
4763 * We *know* that this entire raid_bio is in one chunk, so
4764 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4766 struct stripe_head *sh;
4768 sector_t sector, logical_sector, last_sector;
4773 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4774 sector = raid5_compute_sector(conf, logical_sector,
4776 last_sector = bio_end_sector(raid_bio);
4778 for (; logical_sector < last_sector;
4779 logical_sector += STRIPE_SECTORS,
4780 sector += STRIPE_SECTORS,
4783 if (scnt < raid5_bi_processed_stripes(raid_bio))
4784 /* already done this stripe */
4787 sh = get_active_stripe(conf, sector, 0, 1, 0);
4790 /* failed to get a stripe - must wait */
4791 raid5_set_bi_processed_stripes(raid_bio, scnt);
4792 conf->retry_read_aligned = raid_bio;
4796 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4798 raid5_set_bi_processed_stripes(raid_bio, scnt);
4799 conf->retry_read_aligned = raid_bio;
4803 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
4808 remaining = raid5_dec_bi_active_stripes(raid_bio);
4809 if (remaining == 0) {
4810 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4812 bio_endio(raid_bio, 0);
4814 if (atomic_dec_and_test(&conf->active_aligned_reads))
4815 wake_up(&conf->wait_for_stripe);
4819 #define MAX_STRIPE_BATCH 8
4820 static int handle_active_stripes(struct r5conf *conf)
4822 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4823 int i, batch_size = 0;
4825 while (batch_size < MAX_STRIPE_BATCH &&
4826 (sh = __get_priority_stripe(conf)) != NULL)
4827 batch[batch_size++] = sh;
4829 if (batch_size == 0)
4831 spin_unlock_irq(&conf->device_lock);
4833 for (i = 0; i < batch_size; i++)
4834 handle_stripe(batch[i]);
4838 spin_lock_irq(&conf->device_lock);
4839 for (i = 0; i < batch_size; i++)
4840 __release_stripe(conf, batch[i]);
4845 * This is our raid5 kernel thread.
4847 * We scan the hash table for stripes which can be handled now.
4848 * During the scan, completed stripes are saved for us by the interrupt
4849 * handler, so that they will not have to wait for our next wakeup.
4851 static void raid5d(struct md_thread *thread)
4853 struct mddev *mddev = thread->mddev;
4854 struct r5conf *conf = mddev->private;
4856 struct blk_plug plug;
4858 pr_debug("+++ raid5d active\n");
4860 md_check_recovery(mddev);
4862 blk_start_plug(&plug);
4864 spin_lock_irq(&conf->device_lock);
4870 !list_empty(&conf->bitmap_list)) {
4871 /* Now is a good time to flush some bitmap updates */
4873 spin_unlock_irq(&conf->device_lock);
4874 bitmap_unplug(mddev->bitmap);
4875 spin_lock_irq(&conf->device_lock);
4876 conf->seq_write = conf->seq_flush;
4877 activate_bit_delay(conf);
4879 raid5_activate_delayed(conf);
4881 while ((bio = remove_bio_from_retry(conf))) {
4883 spin_unlock_irq(&conf->device_lock);
4884 ok = retry_aligned_read(conf, bio);
4885 spin_lock_irq(&conf->device_lock);
4891 batch_size = handle_active_stripes(conf);
4894 handled += batch_size;
4896 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4897 spin_unlock_irq(&conf->device_lock);
4898 md_check_recovery(mddev);
4899 spin_lock_irq(&conf->device_lock);
4902 pr_debug("%d stripes handled\n", handled);
4904 spin_unlock_irq(&conf->device_lock);
4906 async_tx_issue_pending_all();
4907 blk_finish_plug(&plug);
4909 pr_debug("--- raid5d inactive\n");
4913 raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
4915 struct r5conf *conf = mddev->private;
4917 return sprintf(page, "%d\n", conf->max_nr_stripes);
4923 raid5_set_cache_size(struct mddev *mddev, int size)
4925 struct r5conf *conf = mddev->private;
4928 if (size <= 16 || size > 32768)
4930 while (size < conf->max_nr_stripes) {
4931 if (drop_one_stripe(conf))
4932 conf->max_nr_stripes--;
4936 err = md_allow_write(mddev);
4939 while (size > conf->max_nr_stripes) {
4940 if (grow_one_stripe(conf))
4941 conf->max_nr_stripes++;
4946 EXPORT_SYMBOL(raid5_set_cache_size);
4949 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
4951 struct r5conf *conf = mddev->private;
4955 if (len >= PAGE_SIZE)
4960 if (strict_strtoul(page, 10, &new))
4962 err = raid5_set_cache_size(mddev, new);
4968 static struct md_sysfs_entry
4969 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4970 raid5_show_stripe_cache_size,
4971 raid5_store_stripe_cache_size);
4974 raid5_show_preread_threshold(struct mddev *mddev, char *page)
4976 struct r5conf *conf = mddev->private;
4978 return sprintf(page, "%d\n", conf->bypass_threshold);
4984 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
4986 struct r5conf *conf = mddev->private;
4988 if (len >= PAGE_SIZE)
4993 if (strict_strtoul(page, 10, &new))
4995 if (new > conf->max_nr_stripes)
4997 conf->bypass_threshold = new;
5001 static struct md_sysfs_entry
5002 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5004 raid5_show_preread_threshold,
5005 raid5_store_preread_threshold);
5008 stripe_cache_active_show(struct mddev *mddev, char *page)
5010 struct r5conf *conf = mddev->private;
5012 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5017 static struct md_sysfs_entry
5018 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
5020 static struct attribute *raid5_attrs[] = {
5021 &raid5_stripecache_size.attr,
5022 &raid5_stripecache_active.attr,
5023 &raid5_preread_bypass_threshold.attr,
5026 static struct attribute_group raid5_attrs_group = {
5028 .attrs = raid5_attrs,
5032 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
5034 struct r5conf *conf = mddev->private;
5037 sectors = mddev->dev_sectors;
5039 /* size is defined by the smallest of previous and new size */
5040 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
5042 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
5043 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
5044 return sectors * (raid_disks - conf->max_degraded);
5047 static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5049 safe_put_page(percpu->spare_page);
5050 kfree(percpu->scribble);
5051 percpu->spare_page = NULL;
5052 percpu->scribble = NULL;
5055 static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5057 if (conf->level == 6 && !percpu->spare_page)
5058 percpu->spare_page = alloc_page(GFP_KERNEL);
5059 if (!percpu->scribble)
5060 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5062 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5063 free_scratch_buffer(conf, percpu);
5070 static void raid5_free_percpu(struct r5conf *conf)
5077 #ifdef CONFIG_HOTPLUG_CPU
5078 unregister_cpu_notifier(&conf->cpu_notify);
5082 for_each_possible_cpu(cpu)
5083 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5086 free_percpu(conf->percpu);
5089 static void free_conf(struct r5conf *conf)
5091 shrink_stripes(conf);
5092 raid5_free_percpu(conf);
5094 kfree(conf->stripe_hashtbl);
5098 #ifdef CONFIG_HOTPLUG_CPU
5099 static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5102 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
5103 long cpu = (long)hcpu;
5104 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5107 case CPU_UP_PREPARE:
5108 case CPU_UP_PREPARE_FROZEN:
5109 if (alloc_scratch_buffer(conf, percpu)) {
5110 pr_err("%s: failed memory allocation for cpu%ld\n",
5112 return notifier_from_errno(-ENOMEM);
5116 case CPU_DEAD_FROZEN:
5117 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5126 static int raid5_alloc_percpu(struct r5conf *conf)
5131 conf->percpu = alloc_percpu(struct raid5_percpu);
5135 #ifdef CONFIG_HOTPLUG_CPU
5136 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5137 conf->cpu_notify.priority = 0;
5138 err = register_cpu_notifier(&conf->cpu_notify);
5144 for_each_present_cpu(cpu) {
5145 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5147 pr_err("%s: failed memory allocation for cpu%ld\n",
5157 static struct r5conf *setup_conf(struct mddev *mddev)
5159 struct r5conf *conf;
5160 int raid_disk, memory, max_disks;
5161 struct md_rdev *rdev;
5162 struct disk_info *disk;
5165 if (mddev->new_level != 5
5166 && mddev->new_level != 4
5167 && mddev->new_level != 6) {
5168 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
5169 mdname(mddev), mddev->new_level);
5170 return ERR_PTR(-EIO);
5172 if ((mddev->new_level == 5
5173 && !algorithm_valid_raid5(mddev->new_layout)) ||
5174 (mddev->new_level == 6
5175 && !algorithm_valid_raid6(mddev->new_layout))) {
5176 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
5177 mdname(mddev), mddev->new_layout);
5178 return ERR_PTR(-EIO);
5180 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
5181 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
5182 mdname(mddev), mddev->raid_disks);
5183 return ERR_PTR(-EINVAL);
5186 if (!mddev->new_chunk_sectors ||
5187 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5188 !is_power_of_2(mddev->new_chunk_sectors)) {
5189 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5190 mdname(mddev), mddev->new_chunk_sectors << 9);
5191 return ERR_PTR(-EINVAL);
5194 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
5197 spin_lock_init(&conf->device_lock);
5198 init_waitqueue_head(&conf->wait_for_stripe);
5199 init_waitqueue_head(&conf->wait_for_overlap);
5200 INIT_LIST_HEAD(&conf->handle_list);
5201 INIT_LIST_HEAD(&conf->hold_list);
5202 INIT_LIST_HEAD(&conf->delayed_list);
5203 INIT_LIST_HEAD(&conf->bitmap_list);
5204 INIT_LIST_HEAD(&conf->inactive_list);
5205 atomic_set(&conf->active_stripes, 0);
5206 atomic_set(&conf->preread_active_stripes, 0);
5207 atomic_set(&conf->active_aligned_reads, 0);
5208 conf->bypass_threshold = BYPASS_THRESHOLD;
5209 conf->recovery_disabled = mddev->recovery_disabled - 1;
5211 conf->raid_disks = mddev->raid_disks;
5212 if (mddev->reshape_position == MaxSector)
5213 conf->previous_raid_disks = mddev->raid_disks;
5215 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5216 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5217 conf->scribble_len = scribble_len(max_disks);
5219 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
5224 conf->mddev = mddev;
5226 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5229 conf->level = mddev->new_level;
5230 if (raid5_alloc_percpu(conf) != 0)
5233 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
5235 rdev_for_each(rdev, mddev) {
5236 raid_disk = rdev->raid_disk;
5237 if (raid_disk >= max_disks
5240 disk = conf->disks + raid_disk;
5242 if (test_bit(Replacement, &rdev->flags)) {
5243 if (disk->replacement)
5245 disk->replacement = rdev;
5252 if (test_bit(In_sync, &rdev->flags)) {
5253 char b[BDEVNAME_SIZE];
5254 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5256 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
5257 } else if (rdev->saved_raid_disk != raid_disk)
5258 /* Cannot rely on bitmap to complete recovery */
5262 conf->chunk_sectors = mddev->new_chunk_sectors;
5263 conf->level = mddev->new_level;
5264 if (conf->level == 6)
5265 conf->max_degraded = 2;
5267 conf->max_degraded = 1;
5268 conf->algorithm = mddev->new_layout;
5269 conf->max_nr_stripes = NR_STRIPES;
5270 conf->reshape_progress = mddev->reshape_position;
5271 if (conf->reshape_progress != MaxSector) {
5272 conf->prev_chunk_sectors = mddev->chunk_sectors;
5273 conf->prev_algo = mddev->layout;
5276 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
5277 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
5278 if (grow_stripes(conf, conf->max_nr_stripes)) {
5280 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5281 mdname(mddev), memory);
5284 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5285 mdname(mddev), memory);
5287 sprintf(pers_name, "raid%d", mddev->new_level);
5288 conf->thread = md_register_thread(raid5d, mddev, pers_name);
5289 if (!conf->thread) {
5291 "md/raid:%s: couldn't allocate thread.\n",
5301 return ERR_PTR(-EIO);
5303 return ERR_PTR(-ENOMEM);
5307 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5310 case ALGORITHM_PARITY_0:
5311 if (raid_disk < max_degraded)
5314 case ALGORITHM_PARITY_N:
5315 if (raid_disk >= raid_disks - max_degraded)
5318 case ALGORITHM_PARITY_0_6:
5319 if (raid_disk == 0 ||
5320 raid_disk == raid_disks - 1)
5323 case ALGORITHM_LEFT_ASYMMETRIC_6:
5324 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5325 case ALGORITHM_LEFT_SYMMETRIC_6:
5326 case ALGORITHM_RIGHT_SYMMETRIC_6:
5327 if (raid_disk == raid_disks - 1)
5333 static int run(struct mddev *mddev)
5335 struct r5conf *conf;
5336 int working_disks = 0;
5337 int dirty_parity_disks = 0;
5338 struct md_rdev *rdev;
5339 sector_t reshape_offset = 0;
5341 long long min_offset_diff = 0;
5344 if (mddev->recovery_cp != MaxSector)
5345 printk(KERN_NOTICE "md/raid:%s: not clean"
5346 " -- starting background reconstruction\n",
5349 rdev_for_each(rdev, mddev) {
5351 if (rdev->raid_disk < 0)
5353 diff = (rdev->new_data_offset - rdev->data_offset);
5355 min_offset_diff = diff;
5357 } else if (mddev->reshape_backwards &&
5358 diff < min_offset_diff)
5359 min_offset_diff = diff;
5360 else if (!mddev->reshape_backwards &&
5361 diff > min_offset_diff)
5362 min_offset_diff = diff;
5365 if (mddev->reshape_position != MaxSector) {
5366 /* Check that we can continue the reshape.
5367 * Difficulties arise if the stripe we would write to
5368 * next is at or after the stripe we would read from next.
5369 * For a reshape that changes the number of devices, this
5370 * is only possible for a very short time, and mdadm makes
5371 * sure that time appears to have past before assembling
5372 * the array. So we fail if that time hasn't passed.
5373 * For a reshape that keeps the number of devices the same
5374 * mdadm must be monitoring the reshape can keeping the
5375 * critical areas read-only and backed up. It will start
5376 * the array in read-only mode, so we check for that.
5378 sector_t here_new, here_old;
5380 int max_degraded = (mddev->level == 6 ? 2 : 1);
5382 if (mddev->new_level != mddev->level) {
5383 printk(KERN_ERR "md/raid:%s: unsupported reshape "
5384 "required - aborting.\n",
5388 old_disks = mddev->raid_disks - mddev->delta_disks;
5389 /* reshape_position must be on a new-stripe boundary, and one
5390 * further up in new geometry must map after here in old
5393 here_new = mddev->reshape_position;
5394 if (sector_div(here_new, mddev->new_chunk_sectors *
5395 (mddev->raid_disks - max_degraded))) {
5396 printk(KERN_ERR "md/raid:%s: reshape_position not "
5397 "on a stripe boundary\n", mdname(mddev));
5400 reshape_offset = here_new * mddev->new_chunk_sectors;
5401 /* here_new is the stripe we will write to */
5402 here_old = mddev->reshape_position;
5403 sector_div(here_old, mddev->chunk_sectors *
5404 (old_disks-max_degraded));
5405 /* here_old is the first stripe that we might need to read
5407 if (mddev->delta_disks == 0) {
5408 if ((here_new * mddev->new_chunk_sectors !=
5409 here_old * mddev->chunk_sectors)) {
5410 printk(KERN_ERR "md/raid:%s: reshape position is"
5411 " confused - aborting\n", mdname(mddev));
5414 /* We cannot be sure it is safe to start an in-place
5415 * reshape. It is only safe if user-space is monitoring
5416 * and taking constant backups.
5417 * mdadm always starts a situation like this in
5418 * readonly mode so it can take control before
5419 * allowing any writes. So just check for that.
5421 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5422 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5423 /* not really in-place - so OK */;
5424 else if (mddev->ro == 0) {
5425 printk(KERN_ERR "md/raid:%s: in-place reshape "
5426 "must be started in read-only mode "
5431 } else if (mddev->reshape_backwards
5432 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
5433 here_old * mddev->chunk_sectors)
5434 : (here_new * mddev->new_chunk_sectors >=
5435 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
5436 /* Reading from the same stripe as writing to - bad */
5437 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5438 "auto-recovery - aborting.\n",
5442 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5444 /* OK, we should be able to continue; */
5446 BUG_ON(mddev->level != mddev->new_level);
5447 BUG_ON(mddev->layout != mddev->new_layout);
5448 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
5449 BUG_ON(mddev->delta_disks != 0);
5452 if (mddev->private == NULL)
5453 conf = setup_conf(mddev);
5455 conf = mddev->private;
5458 return PTR_ERR(conf);
5460 conf->min_offset_diff = min_offset_diff;
5461 mddev->thread = conf->thread;
5462 conf->thread = NULL;
5463 mddev->private = conf;
5465 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5467 rdev = conf->disks[i].rdev;
5468 if (!rdev && conf->disks[i].replacement) {
5469 /* The replacement is all we have yet */
5470 rdev = conf->disks[i].replacement;
5471 conf->disks[i].replacement = NULL;
5472 clear_bit(Replacement, &rdev->flags);
5473 conf->disks[i].rdev = rdev;
5477 if (conf->disks[i].replacement &&
5478 conf->reshape_progress != MaxSector) {
5479 /* replacements and reshape simply do not mix. */
5480 printk(KERN_ERR "md: cannot handle concurrent "
5481 "replacement and reshape.\n");
5484 if (test_bit(In_sync, &rdev->flags)) {
5488 /* This disc is not fully in-sync. However if it
5489 * just stored parity (beyond the recovery_offset),
5490 * when we don't need to be concerned about the
5491 * array being dirty.
5492 * When reshape goes 'backwards', we never have
5493 * partially completed devices, so we only need
5494 * to worry about reshape going forwards.
5496 /* Hack because v0.91 doesn't store recovery_offset properly. */
5497 if (mddev->major_version == 0 &&
5498 mddev->minor_version > 90)
5499 rdev->recovery_offset = reshape_offset;
5501 if (rdev->recovery_offset < reshape_offset) {
5502 /* We need to check old and new layout */
5503 if (!only_parity(rdev->raid_disk,
5506 conf->max_degraded))
5509 if (!only_parity(rdev->raid_disk,
5511 conf->previous_raid_disks,
5512 conf->max_degraded))
5514 dirty_parity_disks++;
5518 * 0 for a fully functional array, 1 or 2 for a degraded array.
5520 mddev->degraded = calc_degraded(conf);
5522 if (has_failed(conf)) {
5523 printk(KERN_ERR "md/raid:%s: not enough operational devices"
5524 " (%d/%d failed)\n",
5525 mdname(mddev), mddev->degraded, conf->raid_disks);
5529 /* device size must be a multiple of chunk size */
5530 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
5531 mddev->resync_max_sectors = mddev->dev_sectors;
5533 if (mddev->degraded > dirty_parity_disks &&
5534 mddev->recovery_cp != MaxSector) {
5535 if (mddev->ok_start_degraded)
5537 "md/raid:%s: starting dirty degraded array"
5538 " - data corruption possible.\n",
5542 "md/raid:%s: cannot start dirty degraded array.\n",
5548 if (mddev->degraded == 0)
5549 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5550 " devices, algorithm %d\n", mdname(mddev), conf->level,
5551 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5554 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5555 " out of %d devices, algorithm %d\n",
5556 mdname(mddev), conf->level,
5557 mddev->raid_disks - mddev->degraded,
5558 mddev->raid_disks, mddev->new_layout);
5560 print_raid5_conf(conf);
5562 if (conf->reshape_progress != MaxSector) {
5563 conf->reshape_safe = conf->reshape_progress;
5564 atomic_set(&conf->reshape_stripes, 0);
5565 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5566 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5567 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5568 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5569 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5574 /* Ok, everything is just fine now */
5575 if (mddev->to_remove == &raid5_attrs_group)
5576 mddev->to_remove = NULL;
5577 else if (mddev->kobj.sd &&
5578 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5580 "raid5: failed to create sysfs attributes for %s\n",
5582 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5586 bool discard_supported = true;
5587 /* read-ahead size must cover two whole stripes, which
5588 * is 2 * (datadisks) * chunksize where 'n' is the
5589 * number of raid devices
5591 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5592 int stripe = data_disks *
5593 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5594 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5595 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5597 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
5599 mddev->queue->backing_dev_info.congested_data = mddev;
5600 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
5602 chunk_size = mddev->chunk_sectors << 9;
5603 blk_queue_io_min(mddev->queue, chunk_size);
5604 blk_queue_io_opt(mddev->queue, chunk_size *
5605 (conf->raid_disks - conf->max_degraded));
5607 * We can only discard a whole stripe. It doesn't make sense to
5608 * discard data disk but write parity disk
5610 stripe = stripe * PAGE_SIZE;
5611 /* Round up to power of 2, as discard handling
5612 * currently assumes that */
5613 while ((stripe-1) & stripe)
5614 stripe = (stripe | (stripe-1)) + 1;
5615 mddev->queue->limits.discard_alignment = stripe;
5616 mddev->queue->limits.discard_granularity = stripe;
5618 * unaligned part of discard request will be ignored, so can't
5619 * guarantee discard_zeroes_data
5621 mddev->queue->limits.discard_zeroes_data = 0;
5623 blk_queue_max_write_same_sectors(mddev->queue, 0);
5625 rdev_for_each(rdev, mddev) {
5626 disk_stack_limits(mddev->gendisk, rdev->bdev,
5627 rdev->data_offset << 9);
5628 disk_stack_limits(mddev->gendisk, rdev->bdev,
5629 rdev->new_data_offset << 9);
5631 * discard_zeroes_data is required, otherwise data
5632 * could be lost. Consider a scenario: discard a stripe
5633 * (the stripe could be inconsistent if
5634 * discard_zeroes_data is 0); write one disk of the
5635 * stripe (the stripe could be inconsistent again
5636 * depending on which disks are used to calculate
5637 * parity); the disk is broken; The stripe data of this
5640 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5641 !bdev_get_queue(rdev->bdev)->
5642 limits.discard_zeroes_data)
5643 discard_supported = false;
5644 /* Unfortunately, discard_zeroes_data is not currently
5645 * a guarantee - just a hint. So we only allow DISCARD
5646 * if the sysadmin has confirmed that only safe devices
5647 * are in use by setting a module parameter.
5649 if (!devices_handle_discard_safely) {
5650 if (discard_supported) {
5651 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
5652 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
5654 discard_supported = false;
5658 if (discard_supported &&
5659 mddev->queue->limits.max_discard_sectors >= stripe &&
5660 mddev->queue->limits.discard_granularity >= stripe)
5661 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5664 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5670 md_unregister_thread(&mddev->thread);
5671 print_raid5_conf(conf);
5673 mddev->private = NULL;
5674 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
5678 static int stop(struct mddev *mddev)
5680 struct r5conf *conf = mddev->private;
5682 md_unregister_thread(&mddev->thread);
5684 mddev->queue->backing_dev_info.congested_fn = NULL;
5686 mddev->private = NULL;
5687 mddev->to_remove = &raid5_attrs_group;
5691 static void status(struct seq_file *seq, struct mddev *mddev)
5693 struct r5conf *conf = mddev->private;
5696 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5697 mddev->chunk_sectors / 2, mddev->layout);
5698 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5699 for (i = 0; i < conf->raid_disks; i++)
5700 seq_printf (seq, "%s",
5701 conf->disks[i].rdev &&
5702 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
5703 seq_printf (seq, "]");
5706 static void print_raid5_conf (struct r5conf *conf)
5709 struct disk_info *tmp;
5711 printk(KERN_DEBUG "RAID conf printout:\n");
5713 printk("(conf==NULL)\n");
5716 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5718 conf->raid_disks - conf->mddev->degraded);
5720 for (i = 0; i < conf->raid_disks; i++) {
5721 char b[BDEVNAME_SIZE];
5722 tmp = conf->disks + i;
5724 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5725 i, !test_bit(Faulty, &tmp->rdev->flags),
5726 bdevname(tmp->rdev->bdev, b));
5730 static int raid5_spare_active(struct mddev *mddev)
5733 struct r5conf *conf = mddev->private;
5734 struct disk_info *tmp;
5736 unsigned long flags;
5738 for (i = 0; i < conf->raid_disks; i++) {
5739 tmp = conf->disks + i;
5740 if (tmp->replacement
5741 && tmp->replacement->recovery_offset == MaxSector
5742 && !test_bit(Faulty, &tmp->replacement->flags)
5743 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5744 /* Replacement has just become active. */
5746 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5749 /* Replaced device not technically faulty,
5750 * but we need to be sure it gets removed
5751 * and never re-added.
5753 set_bit(Faulty, &tmp->rdev->flags);
5754 sysfs_notify_dirent_safe(
5755 tmp->rdev->sysfs_state);
5757 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5758 } else if (tmp->rdev
5759 && tmp->rdev->recovery_offset == MaxSector
5760 && !test_bit(Faulty, &tmp->rdev->flags)
5761 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5763 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
5766 spin_lock_irqsave(&conf->device_lock, flags);
5767 mddev->degraded = calc_degraded(conf);
5768 spin_unlock_irqrestore(&conf->device_lock, flags);
5769 print_raid5_conf(conf);
5773 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
5775 struct r5conf *conf = mddev->private;
5777 int number = rdev->raid_disk;
5778 struct md_rdev **rdevp;
5779 struct disk_info *p = conf->disks + number;
5781 print_raid5_conf(conf);
5782 if (rdev == p->rdev)
5784 else if (rdev == p->replacement)
5785 rdevp = &p->replacement;
5789 if (number >= conf->raid_disks &&
5790 conf->reshape_progress == MaxSector)
5791 clear_bit(In_sync, &rdev->flags);
5793 if (test_bit(In_sync, &rdev->flags) ||
5794 atomic_read(&rdev->nr_pending)) {
5798 /* Only remove non-faulty devices if recovery
5801 if (!test_bit(Faulty, &rdev->flags) &&
5802 mddev->recovery_disabled != conf->recovery_disabled &&
5803 !has_failed(conf) &&
5804 (!p->replacement || p->replacement == rdev) &&
5805 number < conf->raid_disks) {
5811 if (atomic_read(&rdev->nr_pending)) {
5812 /* lost the race, try later */
5815 } else if (p->replacement) {
5816 /* We must have just cleared 'rdev' */
5817 p->rdev = p->replacement;
5818 clear_bit(Replacement, &p->replacement->flags);
5819 smp_mb(); /* Make sure other CPUs may see both as identical
5820 * but will never see neither - if they are careful
5822 p->replacement = NULL;
5823 clear_bit(WantReplacement, &rdev->flags);
5825 /* We might have just removed the Replacement as faulty-
5826 * clear the bit just in case
5828 clear_bit(WantReplacement, &rdev->flags);
5831 print_raid5_conf(conf);
5835 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
5837 struct r5conf *conf = mddev->private;
5840 struct disk_info *p;
5842 int last = conf->raid_disks - 1;
5844 if (mddev->recovery_disabled == conf->recovery_disabled)
5847 if (rdev->saved_raid_disk < 0 && has_failed(conf))
5848 /* no point adding a device */
5851 if (rdev->raid_disk >= 0)
5852 first = last = rdev->raid_disk;
5855 * find the disk ... but prefer rdev->saved_raid_disk
5858 if (rdev->saved_raid_disk >= 0 &&
5859 rdev->saved_raid_disk >= first &&
5860 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5861 first = rdev->saved_raid_disk;
5863 for (disk = first; disk <= last; disk++) {
5864 p = conf->disks + disk;
5865 if (p->rdev == NULL) {
5866 clear_bit(In_sync, &rdev->flags);
5867 rdev->raid_disk = disk;
5869 if (rdev->saved_raid_disk != disk)
5871 rcu_assign_pointer(p->rdev, rdev);
5875 for (disk = first; disk <= last; disk++) {
5876 p = conf->disks + disk;
5877 if (test_bit(WantReplacement, &p->rdev->flags) &&
5878 p->replacement == NULL) {
5879 clear_bit(In_sync, &rdev->flags);
5880 set_bit(Replacement, &rdev->flags);
5881 rdev->raid_disk = disk;
5884 rcu_assign_pointer(p->replacement, rdev);
5889 print_raid5_conf(conf);
5893 static int raid5_resize(struct mddev *mddev, sector_t sectors)
5895 /* no resync is happening, and there is enough space
5896 * on all devices, so we can resize.
5897 * We need to make sure resync covers any new space.
5898 * If the array is shrinking we should possibly wait until
5899 * any io in the removed space completes, but it hardly seems
5903 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
5904 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5905 if (mddev->external_size &&
5906 mddev->array_sectors > newsize)
5908 if (mddev->bitmap) {
5909 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5913 md_set_array_sectors(mddev, newsize);
5914 set_capacity(mddev->gendisk, mddev->array_sectors);
5915 revalidate_disk(mddev->gendisk);
5916 if (sectors > mddev->dev_sectors &&
5917 mddev->recovery_cp > mddev->dev_sectors) {
5918 mddev->recovery_cp = mddev->dev_sectors;
5919 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5921 mddev->dev_sectors = sectors;
5922 mddev->resync_max_sectors = sectors;
5926 static int check_stripe_cache(struct mddev *mddev)
5928 /* Can only proceed if there are plenty of stripe_heads.
5929 * We need a minimum of one full stripe,, and for sensible progress
5930 * it is best to have about 4 times that.
5931 * If we require 4 times, then the default 256 4K stripe_heads will
5932 * allow for chunk sizes up to 256K, which is probably OK.
5933 * If the chunk size is greater, user-space should request more
5934 * stripe_heads first.
5936 struct r5conf *conf = mddev->private;
5937 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5938 > conf->max_nr_stripes ||
5939 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5940 > conf->max_nr_stripes) {
5941 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5943 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5950 static int check_reshape(struct mddev *mddev)
5952 struct r5conf *conf = mddev->private;
5954 if (mddev->delta_disks == 0 &&
5955 mddev->new_layout == mddev->layout &&
5956 mddev->new_chunk_sectors == mddev->chunk_sectors)
5957 return 0; /* nothing to do */
5958 if (has_failed(conf))
5960 if (mddev->delta_disks < 0) {
5961 /* We might be able to shrink, but the devices must
5962 * be made bigger first.
5963 * For raid6, 4 is the minimum size.
5964 * Otherwise 2 is the minimum
5967 if (mddev->level == 6)
5969 if (mddev->raid_disks + mddev->delta_disks < min)
5973 if (!check_stripe_cache(mddev))
5976 return resize_stripes(conf, (conf->previous_raid_disks
5977 + mddev->delta_disks));
5980 static int raid5_start_reshape(struct mddev *mddev)
5982 struct r5conf *conf = mddev->private;
5983 struct md_rdev *rdev;
5985 unsigned long flags;
5987 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5990 if (!check_stripe_cache(mddev))
5993 if (has_failed(conf))
5996 rdev_for_each(rdev, mddev) {
5997 if (!test_bit(In_sync, &rdev->flags)
5998 && !test_bit(Faulty, &rdev->flags))
6002 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
6003 /* Not enough devices even to make a degraded array
6008 /* Refuse to reduce size of the array. Any reductions in
6009 * array size must be through explicit setting of array_size
6012 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6013 < mddev->array_sectors) {
6014 printk(KERN_ERR "md/raid:%s: array size must be reduced "
6015 "before number of disks\n", mdname(mddev));
6019 atomic_set(&conf->reshape_stripes, 0);
6020 spin_lock_irq(&conf->device_lock);
6021 conf->previous_raid_disks = conf->raid_disks;
6022 conf->raid_disks += mddev->delta_disks;
6023 conf->prev_chunk_sectors = conf->chunk_sectors;
6024 conf->chunk_sectors = mddev->new_chunk_sectors;
6025 conf->prev_algo = conf->algorithm;
6026 conf->algorithm = mddev->new_layout;
6028 /* Code that selects data_offset needs to see the generation update
6029 * if reshape_progress has been set - so a memory barrier needed.
6032 if (mddev->reshape_backwards)
6033 conf->reshape_progress = raid5_size(mddev, 0, 0);
6035 conf->reshape_progress = 0;
6036 conf->reshape_safe = conf->reshape_progress;
6037 spin_unlock_irq(&conf->device_lock);
6039 /* Add some new drives, as many as will fit.
6040 * We know there are enough to make the newly sized array work.
6041 * Don't add devices if we are reducing the number of
6042 * devices in the array. This is because it is not possible
6043 * to correctly record the "partially reconstructed" state of
6044 * such devices during the reshape and confusion could result.
6046 if (mddev->delta_disks >= 0) {
6047 rdev_for_each(rdev, mddev)
6048 if (rdev->raid_disk < 0 &&
6049 !test_bit(Faulty, &rdev->flags)) {
6050 if (raid5_add_disk(mddev, rdev) == 0) {
6052 >= conf->previous_raid_disks)
6053 set_bit(In_sync, &rdev->flags);
6055 rdev->recovery_offset = 0;
6057 if (sysfs_link_rdev(mddev, rdev))
6058 /* Failure here is OK */;
6060 } else if (rdev->raid_disk >= conf->previous_raid_disks
6061 && !test_bit(Faulty, &rdev->flags)) {
6062 /* This is a spare that was manually added */
6063 set_bit(In_sync, &rdev->flags);
6066 /* When a reshape changes the number of devices,
6067 * ->degraded is measured against the larger of the
6068 * pre and post number of devices.
6070 spin_lock_irqsave(&conf->device_lock, flags);
6071 mddev->degraded = calc_degraded(conf);
6072 spin_unlock_irqrestore(&conf->device_lock, flags);
6074 mddev->raid_disks = conf->raid_disks;
6075 mddev->reshape_position = conf->reshape_progress;
6076 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6078 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6079 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6080 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6081 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6082 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
6084 if (!mddev->sync_thread) {
6085 mddev->recovery = 0;
6086 spin_lock_irq(&conf->device_lock);
6087 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
6088 rdev_for_each(rdev, mddev)
6089 rdev->new_data_offset = rdev->data_offset;
6091 conf->reshape_progress = MaxSector;
6092 mddev->reshape_position = MaxSector;
6093 spin_unlock_irq(&conf->device_lock);
6096 conf->reshape_checkpoint = jiffies;
6097 md_wakeup_thread(mddev->sync_thread);
6098 md_new_event(mddev);
6102 /* This is called from the reshape thread and should make any
6103 * changes needed in 'conf'
6105 static void end_reshape(struct r5conf *conf)
6108 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
6109 struct md_rdev *rdev;
6111 spin_lock_irq(&conf->device_lock);
6112 conf->previous_raid_disks = conf->raid_disks;
6113 rdev_for_each(rdev, conf->mddev)
6114 rdev->data_offset = rdev->new_data_offset;
6116 conf->reshape_progress = MaxSector;
6117 spin_unlock_irq(&conf->device_lock);
6118 wake_up(&conf->wait_for_overlap);
6120 /* read-ahead size must cover two whole stripes, which is
6121 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6123 if (conf->mddev->queue) {
6124 int data_disks = conf->raid_disks - conf->max_degraded;
6125 int stripe = data_disks * ((conf->chunk_sectors << 9)
6127 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6128 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6133 /* This is called from the raid5d thread with mddev_lock held.
6134 * It makes config changes to the device.
6136 static void raid5_finish_reshape(struct mddev *mddev)
6138 struct r5conf *conf = mddev->private;
6140 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6142 if (mddev->delta_disks > 0) {
6143 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6144 set_capacity(mddev->gendisk, mddev->array_sectors);
6145 revalidate_disk(mddev->gendisk);
6148 spin_lock_irq(&conf->device_lock);
6149 mddev->degraded = calc_degraded(conf);
6150 spin_unlock_irq(&conf->device_lock);
6151 for (d = conf->raid_disks ;
6152 d < conf->raid_disks - mddev->delta_disks;
6154 struct md_rdev *rdev = conf->disks[d].rdev;
6156 clear_bit(In_sync, &rdev->flags);
6157 rdev = conf->disks[d].replacement;
6159 clear_bit(In_sync, &rdev->flags);
6162 mddev->layout = conf->algorithm;
6163 mddev->chunk_sectors = conf->chunk_sectors;
6164 mddev->reshape_position = MaxSector;
6165 mddev->delta_disks = 0;
6166 mddev->reshape_backwards = 0;
6170 static void raid5_quiesce(struct mddev *mddev, int state)
6172 struct r5conf *conf = mddev->private;
6175 case 2: /* resume for a suspend */
6176 wake_up(&conf->wait_for_overlap);
6179 case 1: /* stop all writes */
6180 spin_lock_irq(&conf->device_lock);
6181 /* '2' tells resync/reshape to pause so that all
6182 * active stripes can drain
6185 wait_event_lock_irq(conf->wait_for_stripe,
6186 atomic_read(&conf->active_stripes) == 0 &&
6187 atomic_read(&conf->active_aligned_reads) == 0,
6190 spin_unlock_irq(&conf->device_lock);
6191 /* allow reshape to continue */
6192 wake_up(&conf->wait_for_overlap);
6195 case 0: /* re-enable writes */
6196 spin_lock_irq(&conf->device_lock);
6198 wake_up(&conf->wait_for_stripe);
6199 wake_up(&conf->wait_for_overlap);
6200 spin_unlock_irq(&conf->device_lock);
6206 static void *raid45_takeover_raid0(struct mddev *mddev, int level)
6208 struct r0conf *raid0_conf = mddev->private;
6211 /* for raid0 takeover only one zone is supported */
6212 if (raid0_conf->nr_strip_zones > 1) {
6213 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6215 return ERR_PTR(-EINVAL);
6218 sectors = raid0_conf->strip_zone[0].zone_end;
6219 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
6220 mddev->dev_sectors = sectors;
6221 mddev->new_level = level;
6222 mddev->new_layout = ALGORITHM_PARITY_N;
6223 mddev->new_chunk_sectors = mddev->chunk_sectors;
6224 mddev->raid_disks += 1;
6225 mddev->delta_disks = 1;
6226 /* make sure it will be not marked as dirty */
6227 mddev->recovery_cp = MaxSector;
6229 return setup_conf(mddev);
6233 static void *raid5_takeover_raid1(struct mddev *mddev)
6237 if (mddev->raid_disks != 2 ||
6238 mddev->degraded > 1)
6239 return ERR_PTR(-EINVAL);
6241 /* Should check if there are write-behind devices? */
6243 chunksect = 64*2; /* 64K by default */
6245 /* The array must be an exact multiple of chunksize */
6246 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6249 if ((chunksect<<9) < STRIPE_SIZE)
6250 /* array size does not allow a suitable chunk size */
6251 return ERR_PTR(-EINVAL);
6253 mddev->new_level = 5;
6254 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
6255 mddev->new_chunk_sectors = chunksect;
6257 return setup_conf(mddev);
6260 static void *raid5_takeover_raid6(struct mddev *mddev)
6264 switch (mddev->layout) {
6265 case ALGORITHM_LEFT_ASYMMETRIC_6:
6266 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6268 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6269 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6271 case ALGORITHM_LEFT_SYMMETRIC_6:
6272 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6274 case ALGORITHM_RIGHT_SYMMETRIC_6:
6275 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6277 case ALGORITHM_PARITY_0_6:
6278 new_layout = ALGORITHM_PARITY_0;
6280 case ALGORITHM_PARITY_N:
6281 new_layout = ALGORITHM_PARITY_N;
6284 return ERR_PTR(-EINVAL);
6286 mddev->new_level = 5;
6287 mddev->new_layout = new_layout;
6288 mddev->delta_disks = -1;
6289 mddev->raid_disks -= 1;
6290 return setup_conf(mddev);
6294 static int raid5_check_reshape(struct mddev *mddev)
6296 /* For a 2-drive array, the layout and chunk size can be changed
6297 * immediately as not restriping is needed.
6298 * For larger arrays we record the new value - after validation
6299 * to be used by a reshape pass.
6301 struct r5conf *conf = mddev->private;
6302 int new_chunk = mddev->new_chunk_sectors;
6304 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
6306 if (new_chunk > 0) {
6307 if (!is_power_of_2(new_chunk))
6309 if (new_chunk < (PAGE_SIZE>>9))
6311 if (mddev->array_sectors & (new_chunk-1))
6312 /* not factor of array size */
6316 /* They look valid */
6318 if (mddev->raid_disks == 2) {
6319 /* can make the change immediately */
6320 if (mddev->new_layout >= 0) {
6321 conf->algorithm = mddev->new_layout;
6322 mddev->layout = mddev->new_layout;
6324 if (new_chunk > 0) {
6325 conf->chunk_sectors = new_chunk ;
6326 mddev->chunk_sectors = new_chunk;
6328 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6329 md_wakeup_thread(mddev->thread);
6331 return check_reshape(mddev);
6334 static int raid6_check_reshape(struct mddev *mddev)
6336 int new_chunk = mddev->new_chunk_sectors;
6338 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
6340 if (new_chunk > 0) {
6341 if (!is_power_of_2(new_chunk))
6343 if (new_chunk < (PAGE_SIZE >> 9))
6345 if (mddev->array_sectors & (new_chunk-1))
6346 /* not factor of array size */
6350 /* They look valid */
6351 return check_reshape(mddev);
6354 static void *raid5_takeover(struct mddev *mddev)
6356 /* raid5 can take over:
6357 * raid0 - if there is only one strip zone - make it a raid4 layout
6358 * raid1 - if there are two drives. We need to know the chunk size
6359 * raid4 - trivial - just use a raid4 layout.
6360 * raid6 - Providing it is a *_6 layout
6362 if (mddev->level == 0)
6363 return raid45_takeover_raid0(mddev, 5);
6364 if (mddev->level == 1)
6365 return raid5_takeover_raid1(mddev);
6366 if (mddev->level == 4) {
6367 mddev->new_layout = ALGORITHM_PARITY_N;
6368 mddev->new_level = 5;
6369 return setup_conf(mddev);
6371 if (mddev->level == 6)
6372 return raid5_takeover_raid6(mddev);
6374 return ERR_PTR(-EINVAL);
6377 static void *raid4_takeover(struct mddev *mddev)
6379 /* raid4 can take over:
6380 * raid0 - if there is only one strip zone
6381 * raid5 - if layout is right
6383 if (mddev->level == 0)
6384 return raid45_takeover_raid0(mddev, 4);
6385 if (mddev->level == 5 &&
6386 mddev->layout == ALGORITHM_PARITY_N) {
6387 mddev->new_layout = 0;
6388 mddev->new_level = 4;
6389 return setup_conf(mddev);
6391 return ERR_PTR(-EINVAL);
6394 static struct md_personality raid5_personality;
6396 static void *raid6_takeover(struct mddev *mddev)
6398 /* Currently can only take over a raid5. We map the
6399 * personality to an equivalent raid6 personality
6400 * with the Q block at the end.
6404 if (mddev->pers != &raid5_personality)
6405 return ERR_PTR(-EINVAL);
6406 if (mddev->degraded > 1)
6407 return ERR_PTR(-EINVAL);
6408 if (mddev->raid_disks > 253)
6409 return ERR_PTR(-EINVAL);
6410 if (mddev->raid_disks < 3)
6411 return ERR_PTR(-EINVAL);
6413 switch (mddev->layout) {
6414 case ALGORITHM_LEFT_ASYMMETRIC:
6415 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6417 case ALGORITHM_RIGHT_ASYMMETRIC:
6418 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6420 case ALGORITHM_LEFT_SYMMETRIC:
6421 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6423 case ALGORITHM_RIGHT_SYMMETRIC:
6424 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6426 case ALGORITHM_PARITY_0:
6427 new_layout = ALGORITHM_PARITY_0_6;
6429 case ALGORITHM_PARITY_N:
6430 new_layout = ALGORITHM_PARITY_N;
6433 return ERR_PTR(-EINVAL);
6435 mddev->new_level = 6;
6436 mddev->new_layout = new_layout;
6437 mddev->delta_disks = 1;
6438 mddev->raid_disks += 1;
6439 return setup_conf(mddev);
6443 static struct md_personality raid6_personality =
6447 .owner = THIS_MODULE,
6448 .make_request = make_request,
6452 .error_handler = error,
6453 .hot_add_disk = raid5_add_disk,
6454 .hot_remove_disk= raid5_remove_disk,
6455 .spare_active = raid5_spare_active,
6456 .sync_request = sync_request,
6457 .resize = raid5_resize,
6459 .check_reshape = raid6_check_reshape,
6460 .start_reshape = raid5_start_reshape,
6461 .finish_reshape = raid5_finish_reshape,
6462 .quiesce = raid5_quiesce,
6463 .takeover = raid6_takeover,
6465 static struct md_personality raid5_personality =
6469 .owner = THIS_MODULE,
6470 .make_request = make_request,
6474 .error_handler = error,
6475 .hot_add_disk = raid5_add_disk,
6476 .hot_remove_disk= raid5_remove_disk,
6477 .spare_active = raid5_spare_active,
6478 .sync_request = sync_request,
6479 .resize = raid5_resize,
6481 .check_reshape = raid5_check_reshape,
6482 .start_reshape = raid5_start_reshape,
6483 .finish_reshape = raid5_finish_reshape,
6484 .quiesce = raid5_quiesce,
6485 .takeover = raid5_takeover,
6488 static struct md_personality raid4_personality =
6492 .owner = THIS_MODULE,
6493 .make_request = make_request,
6497 .error_handler = error,
6498 .hot_add_disk = raid5_add_disk,
6499 .hot_remove_disk= raid5_remove_disk,
6500 .spare_active = raid5_spare_active,
6501 .sync_request = sync_request,
6502 .resize = raid5_resize,
6504 .check_reshape = raid5_check_reshape,
6505 .start_reshape = raid5_start_reshape,
6506 .finish_reshape = raid5_finish_reshape,
6507 .quiesce = raid5_quiesce,
6508 .takeover = raid4_takeover,
6511 static int __init raid5_init(void)
6513 register_md_personality(&raid6_personality);
6514 register_md_personality(&raid5_personality);
6515 register_md_personality(&raid4_personality);
6519 static void raid5_exit(void)
6521 unregister_md_personality(&raid6_personality);
6522 unregister_md_personality(&raid5_personality);
6523 unregister_md_personality(&raid4_personality);
6526 module_init(raid5_init);
6527 module_exit(raid5_exit);
6528 MODULE_LICENSE("GPL");
6529 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6530 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6531 MODULE_ALIAS("md-raid5");
6532 MODULE_ALIAS("md-raid4");
6533 MODULE_ALIAS("md-level-5");
6534 MODULE_ALIAS("md-level-4");
6535 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6536 MODULE_ALIAS("md-raid6");
6537 MODULE_ALIAS("md-level-6");
6539 /* This used to be two separate modules, they were: */
6540 MODULE_ALIAS("raid5");
6541 MODULE_ALIAS("raid6");