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 <linux/nodemask.h>
57 #include <trace/events/block.h>
64 #define cpu_to_group(cpu) cpu_to_node(cpu)
65 #define ANY_GROUP NUMA_NO_NODE
67 static struct workqueue_struct *raid5_wq;
72 #define NR_STRIPES 256
73 #define STRIPE_SIZE PAGE_SIZE
74 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
75 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
76 #define IO_THRESHOLD 1
77 #define BYPASS_THRESHOLD 1
78 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
79 #define HASH_MASK (NR_HASH - 1)
81 static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
83 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
84 return &conf->stripe_hashtbl[hash];
87 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
88 * order without overlap. There may be several bio's per stripe+device, and
89 * a bio could span several devices.
90 * When walking this list for a particular stripe+device, we must never proceed
91 * beyond a bio that extends past this device, as the next bio might no longer
93 * This function is used to determine the 'next' bio in the list, given the sector
94 * of the current stripe+device
96 static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
98 int sectors = bio_sectors(bio);
99 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
106 * We maintain a biased count of active stripes in the bottom 16 bits of
107 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
109 static inline int raid5_bi_processed_stripes(struct bio *bio)
111 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
112 return (atomic_read(segments) >> 16) & 0xffff;
115 static inline int raid5_dec_bi_active_stripes(struct bio *bio)
117 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
118 return atomic_sub_return(1, segments) & 0xffff;
121 static inline void raid5_inc_bi_active_stripes(struct bio *bio)
123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 atomic_inc(segments);
127 static inline void raid5_set_bi_processed_stripes(struct bio *bio,
130 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
134 old = atomic_read(segments);
135 new = (old & 0xffff) | (cnt << 16);
136 } while (atomic_cmpxchg(segments, old, new) != old);
139 static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
141 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
142 atomic_set(segments, cnt);
145 /* Find first data disk in a raid6 stripe */
146 static inline int raid6_d0(struct stripe_head *sh)
149 /* ddf always start from first device */
151 /* md starts just after Q block */
152 if (sh->qd_idx == sh->disks - 1)
155 return sh->qd_idx + 1;
157 static inline int raid6_next_disk(int disk, int raid_disks)
160 return (disk < raid_disks) ? disk : 0;
163 /* When walking through the disks in a raid5, starting at raid6_d0,
164 * We need to map each disk to a 'slot', where the data disks are slot
165 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
166 * is raid_disks-1. This help does that mapping.
168 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
169 int *count, int syndrome_disks)
175 if (idx == sh->pd_idx)
176 return syndrome_disks;
177 if (idx == sh->qd_idx)
178 return syndrome_disks + 1;
184 static void return_io(struct bio *return_bi)
186 struct bio *bi = return_bi;
189 return_bi = bi->bi_next;
192 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
199 static void print_raid5_conf (struct r5conf *conf);
201 static int stripe_operations_active(struct stripe_head *sh)
203 return sh->check_state || sh->reconstruct_state ||
204 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
205 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
208 static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
210 struct r5conf *conf = sh->raid_conf;
211 struct r5worker_group *group;
212 int i, cpu = sh->cpu;
214 if (!cpu_online(cpu)) {
215 cpu = cpumask_any(cpu_online_mask);
219 if (list_empty(&sh->lru)) {
220 struct r5worker_group *group;
221 group = conf->worker_groups + cpu_to_group(cpu);
222 list_add_tail(&sh->lru, &group->handle_list);
225 if (conf->worker_cnt_per_group == 0) {
226 md_wakeup_thread(conf->mddev->thread);
230 group = conf->worker_groups + cpu_to_group(sh->cpu);
232 for (i = 0; i < conf->worker_cnt_per_group; i++)
233 queue_work_on(sh->cpu, raid5_wq, &group->workers[i].work);
236 static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
238 BUG_ON(!list_empty(&sh->lru));
239 BUG_ON(atomic_read(&conf->active_stripes)==0);
240 if (test_bit(STRIPE_HANDLE, &sh->state)) {
241 if (test_bit(STRIPE_DELAYED, &sh->state) &&
242 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
243 list_add_tail(&sh->lru, &conf->delayed_list);
244 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
245 sh->bm_seq - conf->seq_write > 0)
246 list_add_tail(&sh->lru, &conf->bitmap_list);
248 clear_bit(STRIPE_DELAYED, &sh->state);
249 clear_bit(STRIPE_BIT_DELAY, &sh->state);
250 if (conf->worker_cnt_per_group == 0) {
251 list_add_tail(&sh->lru, &conf->handle_list);
253 raid5_wakeup_stripe_thread(sh);
257 md_wakeup_thread(conf->mddev->thread);
259 BUG_ON(stripe_operations_active(sh));
260 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
261 if (atomic_dec_return(&conf->preread_active_stripes)
263 md_wakeup_thread(conf->mddev->thread);
264 atomic_dec(&conf->active_stripes);
265 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
266 list_add_tail(&sh->lru, &conf->inactive_list);
267 wake_up(&conf->wait_for_stripe);
268 if (conf->retry_read_aligned)
269 md_wakeup_thread(conf->mddev->thread);
274 static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
276 if (atomic_dec_and_test(&sh->count))
277 do_release_stripe(conf, sh);
280 static struct llist_node *llist_reverse_order(struct llist_node *head)
282 struct llist_node *new_head = NULL;
285 struct llist_node *tmp = head;
287 tmp->next = new_head;
294 /* should hold conf->device_lock already */
295 static int release_stripe_list(struct r5conf *conf)
297 struct stripe_head *sh;
299 struct llist_node *head;
301 head = llist_del_all(&conf->released_stripes);
302 head = llist_reverse_order(head);
304 sh = llist_entry(head, struct stripe_head, release_list);
305 head = llist_next(head);
306 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
308 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
310 * Don't worry the bit is set here, because if the bit is set
311 * again, the count is always > 1. This is true for
312 * STRIPE_ON_UNPLUG_LIST bit too.
314 __release_stripe(conf, sh);
321 static void release_stripe(struct stripe_head *sh)
323 struct r5conf *conf = sh->raid_conf;
327 if (test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
329 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
331 md_wakeup_thread(conf->mddev->thread);
334 local_irq_save(flags);
335 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
336 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
337 do_release_stripe(conf, sh);
338 spin_unlock(&conf->device_lock);
340 local_irq_restore(flags);
343 static inline void remove_hash(struct stripe_head *sh)
345 pr_debug("remove_hash(), stripe %llu\n",
346 (unsigned long long)sh->sector);
348 hlist_del_init(&sh->hash);
351 static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
353 struct hlist_head *hp = stripe_hash(conf, sh->sector);
355 pr_debug("insert_hash(), stripe %llu\n",
356 (unsigned long long)sh->sector);
358 hlist_add_head(&sh->hash, hp);
362 /* find an idle stripe, make sure it is unhashed, and return it. */
363 static struct stripe_head *get_free_stripe(struct r5conf *conf)
365 struct stripe_head *sh = NULL;
366 struct list_head *first;
368 if (list_empty(&conf->inactive_list))
370 first = conf->inactive_list.next;
371 sh = list_entry(first, struct stripe_head, lru);
372 list_del_init(first);
374 atomic_inc(&conf->active_stripes);
379 static void shrink_buffers(struct stripe_head *sh)
383 int num = sh->raid_conf->pool_size;
385 for (i = 0; i < num ; i++) {
389 sh->dev[i].page = NULL;
394 static int grow_buffers(struct stripe_head *sh)
397 int num = sh->raid_conf->pool_size;
399 for (i = 0; i < num; i++) {
402 if (!(page = alloc_page(GFP_KERNEL))) {
405 sh->dev[i].page = page;
410 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
411 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
412 struct stripe_head *sh);
414 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
416 struct r5conf *conf = sh->raid_conf;
419 BUG_ON(atomic_read(&sh->count) != 0);
420 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
421 BUG_ON(stripe_operations_active(sh));
423 pr_debug("init_stripe called, stripe %llu\n",
424 (unsigned long long)sh->sector);
428 sh->generation = conf->generation - previous;
429 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
431 stripe_set_idx(sector, conf, previous, sh);
435 for (i = sh->disks; i--; ) {
436 struct r5dev *dev = &sh->dev[i];
438 if (dev->toread || dev->read || dev->towrite || dev->written ||
439 test_bit(R5_LOCKED, &dev->flags)) {
440 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
441 (unsigned long long)sh->sector, i, dev->toread,
442 dev->read, dev->towrite, dev->written,
443 test_bit(R5_LOCKED, &dev->flags));
447 raid5_build_block(sh, i, previous);
449 insert_hash(conf, sh);
450 sh->cpu = smp_processor_id();
453 static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
456 struct stripe_head *sh;
458 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
459 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
460 if (sh->sector == sector && sh->generation == generation)
462 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
467 * Need to check if array has failed when deciding whether to:
469 * - remove non-faulty devices
472 * This determination is simple when no reshape is happening.
473 * However if there is a reshape, we need to carefully check
474 * both the before and after sections.
475 * This is because some failed devices may only affect one
476 * of the two sections, and some non-in_sync devices may
477 * be insync in the section most affected by failed devices.
479 static int calc_degraded(struct r5conf *conf)
481 int degraded, degraded2;
486 for (i = 0; i < conf->previous_raid_disks; i++) {
487 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
488 if (rdev && test_bit(Faulty, &rdev->flags))
489 rdev = rcu_dereference(conf->disks[i].replacement);
490 if (!rdev || test_bit(Faulty, &rdev->flags))
492 else if (test_bit(In_sync, &rdev->flags))
495 /* not in-sync or faulty.
496 * If the reshape increases the number of devices,
497 * this is being recovered by the reshape, so
498 * this 'previous' section is not in_sync.
499 * If the number of devices is being reduced however,
500 * the device can only be part of the array if
501 * we are reverting a reshape, so this section will
504 if (conf->raid_disks >= conf->previous_raid_disks)
508 if (conf->raid_disks == conf->previous_raid_disks)
512 for (i = 0; i < conf->raid_disks; i++) {
513 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
514 if (rdev && test_bit(Faulty, &rdev->flags))
515 rdev = rcu_dereference(conf->disks[i].replacement);
516 if (!rdev || test_bit(Faulty, &rdev->flags))
518 else if (test_bit(In_sync, &rdev->flags))
521 /* not in-sync or faulty.
522 * If reshape increases the number of devices, this
523 * section has already been recovered, else it
524 * almost certainly hasn't.
526 if (conf->raid_disks <= conf->previous_raid_disks)
530 if (degraded2 > degraded)
535 static int has_failed(struct r5conf *conf)
539 if (conf->mddev->reshape_position == MaxSector)
540 return conf->mddev->degraded > conf->max_degraded;
542 degraded = calc_degraded(conf);
543 if (degraded > conf->max_degraded)
548 static struct stripe_head *
549 get_active_stripe(struct r5conf *conf, sector_t sector,
550 int previous, int noblock, int noquiesce)
552 struct stripe_head *sh;
554 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
556 spin_lock_irq(&conf->device_lock);
559 wait_event_lock_irq(conf->wait_for_stripe,
560 conf->quiesce == 0 || noquiesce,
562 sh = __find_stripe(conf, sector, conf->generation - previous);
564 if (!conf->inactive_blocked)
565 sh = get_free_stripe(conf);
566 if (noblock && sh == NULL)
569 conf->inactive_blocked = 1;
570 wait_event_lock_irq(conf->wait_for_stripe,
571 !list_empty(&conf->inactive_list) &&
572 (atomic_read(&conf->active_stripes)
573 < (conf->max_nr_stripes *3/4)
574 || !conf->inactive_blocked),
576 conf->inactive_blocked = 0;
578 init_stripe(sh, sector, previous);
580 if (atomic_read(&sh->count)) {
581 BUG_ON(!list_empty(&sh->lru)
582 && !test_bit(STRIPE_EXPANDING, &sh->state)
583 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
584 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
586 if (!test_bit(STRIPE_HANDLE, &sh->state))
587 atomic_inc(&conf->active_stripes);
588 if (list_empty(&sh->lru) &&
589 !test_bit(STRIPE_EXPANDING, &sh->state))
591 list_del_init(&sh->lru);
594 } while (sh == NULL);
597 atomic_inc(&sh->count);
599 spin_unlock_irq(&conf->device_lock);
603 /* Determine if 'data_offset' or 'new_data_offset' should be used
604 * in this stripe_head.
606 static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
608 sector_t progress = conf->reshape_progress;
609 /* Need a memory barrier to make sure we see the value
610 * of conf->generation, or ->data_offset that was set before
611 * reshape_progress was updated.
614 if (progress == MaxSector)
616 if (sh->generation == conf->generation - 1)
618 /* We are in a reshape, and this is a new-generation stripe,
619 * so use new_data_offset.
625 raid5_end_read_request(struct bio *bi, int error);
627 raid5_end_write_request(struct bio *bi, int error);
629 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
631 struct r5conf *conf = sh->raid_conf;
632 int i, disks = sh->disks;
636 for (i = disks; i--; ) {
638 int replace_only = 0;
639 struct bio *bi, *rbi;
640 struct md_rdev *rdev, *rrdev = NULL;
641 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
642 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
646 if (test_bit(R5_Discard, &sh->dev[i].flags))
648 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
650 else if (test_and_clear_bit(R5_WantReplace,
651 &sh->dev[i].flags)) {
656 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
659 bi = &sh->dev[i].req;
660 rbi = &sh->dev[i].rreq; /* For writing to replacement */
663 rrdev = rcu_dereference(conf->disks[i].replacement);
664 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
665 rdev = rcu_dereference(conf->disks[i].rdev);
674 /* We raced and saw duplicates */
677 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
682 if (rdev && test_bit(Faulty, &rdev->flags))
685 atomic_inc(&rdev->nr_pending);
686 if (rrdev && test_bit(Faulty, &rrdev->flags))
689 atomic_inc(&rrdev->nr_pending);
692 /* We have already checked bad blocks for reads. Now
693 * need to check for writes. We never accept write errors
694 * on the replacement, so we don't to check rrdev.
696 while ((rw & WRITE) && rdev &&
697 test_bit(WriteErrorSeen, &rdev->flags)) {
700 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
701 &first_bad, &bad_sectors);
706 set_bit(BlockedBadBlocks, &rdev->flags);
707 if (!conf->mddev->external &&
708 conf->mddev->flags) {
709 /* It is very unlikely, but we might
710 * still need to write out the
711 * bad block log - better give it
713 md_check_recovery(conf->mddev);
716 * Because md_wait_for_blocked_rdev
717 * will dec nr_pending, we must
718 * increment it first.
720 atomic_inc(&rdev->nr_pending);
721 md_wait_for_blocked_rdev(rdev, conf->mddev);
723 /* Acknowledged bad block - skip the write */
724 rdev_dec_pending(rdev, conf->mddev);
730 if (s->syncing || s->expanding || s->expanded
732 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
734 set_bit(STRIPE_IO_STARTED, &sh->state);
737 bi->bi_bdev = rdev->bdev;
739 bi->bi_end_io = (rw & WRITE)
740 ? raid5_end_write_request
741 : raid5_end_read_request;
744 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
745 __func__, (unsigned long long)sh->sector,
747 atomic_inc(&sh->count);
748 if (use_new_offset(conf, sh))
749 bi->bi_sector = (sh->sector
750 + rdev->new_data_offset);
752 bi->bi_sector = (sh->sector
753 + rdev->data_offset);
754 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
755 bi->bi_rw |= REQ_FLUSH;
758 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
759 bi->bi_io_vec[0].bv_offset = 0;
760 bi->bi_size = STRIPE_SIZE;
762 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
764 if (conf->mddev->gendisk)
765 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
766 bi, disk_devt(conf->mddev->gendisk),
768 generic_make_request(bi);
771 if (s->syncing || s->expanding || s->expanded
773 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
775 set_bit(STRIPE_IO_STARTED, &sh->state);
778 rbi->bi_bdev = rrdev->bdev;
780 BUG_ON(!(rw & WRITE));
781 rbi->bi_end_io = raid5_end_write_request;
782 rbi->bi_private = sh;
784 pr_debug("%s: for %llu schedule op %ld on "
785 "replacement disc %d\n",
786 __func__, (unsigned long long)sh->sector,
788 atomic_inc(&sh->count);
789 if (use_new_offset(conf, sh))
790 rbi->bi_sector = (sh->sector
791 + rrdev->new_data_offset);
793 rbi->bi_sector = (sh->sector
794 + rrdev->data_offset);
796 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
797 rbi->bi_io_vec[0].bv_offset = 0;
798 rbi->bi_size = STRIPE_SIZE;
799 if (conf->mddev->gendisk)
800 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
801 rbi, disk_devt(conf->mddev->gendisk),
803 generic_make_request(rbi);
805 if (!rdev && !rrdev) {
807 set_bit(STRIPE_DEGRADED, &sh->state);
808 pr_debug("skip op %ld on disc %d for sector %llu\n",
809 bi->bi_rw, i, (unsigned long long)sh->sector);
810 clear_bit(R5_LOCKED, &sh->dev[i].flags);
811 set_bit(STRIPE_HANDLE, &sh->state);
816 static struct dma_async_tx_descriptor *
817 async_copy_data(int frombio, struct bio *bio, struct page *page,
818 sector_t sector, struct dma_async_tx_descriptor *tx)
821 struct page *bio_page;
824 struct async_submit_ctl submit;
825 enum async_tx_flags flags = 0;
827 if (bio->bi_sector >= sector)
828 page_offset = (signed)(bio->bi_sector - sector) * 512;
830 page_offset = (signed)(sector - bio->bi_sector) * -512;
833 flags |= ASYNC_TX_FENCE;
834 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
836 bio_for_each_segment(bvl, bio, i) {
837 int len = bvl->bv_len;
841 if (page_offset < 0) {
842 b_offset = -page_offset;
843 page_offset += b_offset;
847 if (len > 0 && page_offset + len > STRIPE_SIZE)
848 clen = STRIPE_SIZE - page_offset;
853 b_offset += bvl->bv_offset;
854 bio_page = bvl->bv_page;
856 tx = async_memcpy(page, bio_page, page_offset,
857 b_offset, clen, &submit);
859 tx = async_memcpy(bio_page, page, b_offset,
860 page_offset, clen, &submit);
862 /* chain the operations */
863 submit.depend_tx = tx;
865 if (clen < len) /* hit end of page */
873 static void ops_complete_biofill(void *stripe_head_ref)
875 struct stripe_head *sh = stripe_head_ref;
876 struct bio *return_bi = NULL;
879 pr_debug("%s: stripe %llu\n", __func__,
880 (unsigned long long)sh->sector);
882 /* clear completed biofills */
883 for (i = sh->disks; i--; ) {
884 struct r5dev *dev = &sh->dev[i];
886 /* acknowledge completion of a biofill operation */
887 /* and check if we need to reply to a read request,
888 * new R5_Wantfill requests are held off until
889 * !STRIPE_BIOFILL_RUN
891 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
892 struct bio *rbi, *rbi2;
897 while (rbi && rbi->bi_sector <
898 dev->sector + STRIPE_SECTORS) {
899 rbi2 = r5_next_bio(rbi, dev->sector);
900 if (!raid5_dec_bi_active_stripes(rbi)) {
901 rbi->bi_next = return_bi;
908 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
910 return_io(return_bi);
912 set_bit(STRIPE_HANDLE, &sh->state);
916 static void ops_run_biofill(struct stripe_head *sh)
918 struct dma_async_tx_descriptor *tx = NULL;
919 struct async_submit_ctl submit;
922 pr_debug("%s: stripe %llu\n", __func__,
923 (unsigned long long)sh->sector);
925 for (i = sh->disks; i--; ) {
926 struct r5dev *dev = &sh->dev[i];
927 if (test_bit(R5_Wantfill, &dev->flags)) {
929 spin_lock_irq(&sh->stripe_lock);
930 dev->read = rbi = dev->toread;
932 spin_unlock_irq(&sh->stripe_lock);
933 while (rbi && rbi->bi_sector <
934 dev->sector + STRIPE_SECTORS) {
935 tx = async_copy_data(0, rbi, dev->page,
937 rbi = r5_next_bio(rbi, dev->sector);
942 atomic_inc(&sh->count);
943 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
944 async_trigger_callback(&submit);
947 static void mark_target_uptodate(struct stripe_head *sh, int target)
954 tgt = &sh->dev[target];
955 set_bit(R5_UPTODATE, &tgt->flags);
956 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
957 clear_bit(R5_Wantcompute, &tgt->flags);
960 static void ops_complete_compute(void *stripe_head_ref)
962 struct stripe_head *sh = stripe_head_ref;
964 pr_debug("%s: stripe %llu\n", __func__,
965 (unsigned long long)sh->sector);
967 /* mark the computed target(s) as uptodate */
968 mark_target_uptodate(sh, sh->ops.target);
969 mark_target_uptodate(sh, sh->ops.target2);
971 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
972 if (sh->check_state == check_state_compute_run)
973 sh->check_state = check_state_compute_result;
974 set_bit(STRIPE_HANDLE, &sh->state);
978 /* return a pointer to the address conversion region of the scribble buffer */
979 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
980 struct raid5_percpu *percpu)
982 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
985 static struct dma_async_tx_descriptor *
986 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
988 int disks = sh->disks;
989 struct page **xor_srcs = percpu->scribble;
990 int target = sh->ops.target;
991 struct r5dev *tgt = &sh->dev[target];
992 struct page *xor_dest = tgt->page;
994 struct dma_async_tx_descriptor *tx;
995 struct async_submit_ctl submit;
998 pr_debug("%s: stripe %llu block: %d\n",
999 __func__, (unsigned long long)sh->sector, target);
1000 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1002 for (i = disks; i--; )
1004 xor_srcs[count++] = sh->dev[i].page;
1006 atomic_inc(&sh->count);
1008 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
1009 ops_complete_compute, sh, to_addr_conv(sh, percpu));
1010 if (unlikely(count == 1))
1011 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1013 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1018 /* set_syndrome_sources - populate source buffers for gen_syndrome
1019 * @srcs - (struct page *) array of size sh->disks
1020 * @sh - stripe_head to parse
1022 * Populates srcs in proper layout order for the stripe and returns the
1023 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1024 * destination buffer is recorded in srcs[count] and the Q destination
1025 * is recorded in srcs[count+1]].
1027 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1029 int disks = sh->disks;
1030 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1031 int d0_idx = raid6_d0(sh);
1035 for (i = 0; i < disks; i++)
1041 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1043 srcs[slot] = sh->dev[i].page;
1044 i = raid6_next_disk(i, disks);
1045 } while (i != d0_idx);
1047 return syndrome_disks;
1050 static struct dma_async_tx_descriptor *
1051 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1053 int disks = sh->disks;
1054 struct page **blocks = percpu->scribble;
1056 int qd_idx = sh->qd_idx;
1057 struct dma_async_tx_descriptor *tx;
1058 struct async_submit_ctl submit;
1064 if (sh->ops.target < 0)
1065 target = sh->ops.target2;
1066 else if (sh->ops.target2 < 0)
1067 target = sh->ops.target;
1069 /* we should only have one valid target */
1072 pr_debug("%s: stripe %llu block: %d\n",
1073 __func__, (unsigned long long)sh->sector, target);
1075 tgt = &sh->dev[target];
1076 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1079 atomic_inc(&sh->count);
1081 if (target == qd_idx) {
1082 count = set_syndrome_sources(blocks, sh);
1083 blocks[count] = NULL; /* regenerating p is not necessary */
1084 BUG_ON(blocks[count+1] != dest); /* q should already be set */
1085 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1086 ops_complete_compute, sh,
1087 to_addr_conv(sh, percpu));
1088 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1090 /* Compute any data- or p-drive using XOR */
1092 for (i = disks; i-- ; ) {
1093 if (i == target || i == qd_idx)
1095 blocks[count++] = sh->dev[i].page;
1098 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1099 NULL, ops_complete_compute, sh,
1100 to_addr_conv(sh, percpu));
1101 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1107 static struct dma_async_tx_descriptor *
1108 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1110 int i, count, disks = sh->disks;
1111 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1112 int d0_idx = raid6_d0(sh);
1113 int faila = -1, failb = -1;
1114 int target = sh->ops.target;
1115 int target2 = sh->ops.target2;
1116 struct r5dev *tgt = &sh->dev[target];
1117 struct r5dev *tgt2 = &sh->dev[target2];
1118 struct dma_async_tx_descriptor *tx;
1119 struct page **blocks = percpu->scribble;
1120 struct async_submit_ctl submit;
1122 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1123 __func__, (unsigned long long)sh->sector, target, target2);
1124 BUG_ON(target < 0 || target2 < 0);
1125 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1126 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1128 /* we need to open-code set_syndrome_sources to handle the
1129 * slot number conversion for 'faila' and 'failb'
1131 for (i = 0; i < disks ; i++)
1136 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1138 blocks[slot] = sh->dev[i].page;
1144 i = raid6_next_disk(i, disks);
1145 } while (i != d0_idx);
1147 BUG_ON(faila == failb);
1150 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1151 __func__, (unsigned long long)sh->sector, faila, failb);
1153 atomic_inc(&sh->count);
1155 if (failb == syndrome_disks+1) {
1156 /* Q disk is one of the missing disks */
1157 if (faila == syndrome_disks) {
1158 /* Missing P+Q, just recompute */
1159 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1160 ops_complete_compute, sh,
1161 to_addr_conv(sh, percpu));
1162 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
1163 STRIPE_SIZE, &submit);
1167 int qd_idx = sh->qd_idx;
1169 /* Missing D+Q: recompute D from P, then recompute Q */
1170 if (target == qd_idx)
1171 data_target = target2;
1173 data_target = target;
1176 for (i = disks; i-- ; ) {
1177 if (i == data_target || i == qd_idx)
1179 blocks[count++] = sh->dev[i].page;
1181 dest = sh->dev[data_target].page;
1182 init_async_submit(&submit,
1183 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1185 to_addr_conv(sh, percpu));
1186 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1189 count = set_syndrome_sources(blocks, sh);
1190 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1191 ops_complete_compute, sh,
1192 to_addr_conv(sh, percpu));
1193 return async_gen_syndrome(blocks, 0, count+2,
1194 STRIPE_SIZE, &submit);
1197 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1198 ops_complete_compute, sh,
1199 to_addr_conv(sh, percpu));
1200 if (failb == syndrome_disks) {
1201 /* We're missing D+P. */
1202 return async_raid6_datap_recov(syndrome_disks+2,
1206 /* We're missing D+D. */
1207 return async_raid6_2data_recov(syndrome_disks+2,
1208 STRIPE_SIZE, faila, failb,
1215 static void ops_complete_prexor(void *stripe_head_ref)
1217 struct stripe_head *sh = stripe_head_ref;
1219 pr_debug("%s: stripe %llu\n", __func__,
1220 (unsigned long long)sh->sector);
1223 static struct dma_async_tx_descriptor *
1224 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1225 struct dma_async_tx_descriptor *tx)
1227 int disks = sh->disks;
1228 struct page **xor_srcs = percpu->scribble;
1229 int count = 0, pd_idx = sh->pd_idx, i;
1230 struct async_submit_ctl submit;
1232 /* existing parity data subtracted */
1233 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1235 pr_debug("%s: stripe %llu\n", __func__,
1236 (unsigned long long)sh->sector);
1238 for (i = disks; i--; ) {
1239 struct r5dev *dev = &sh->dev[i];
1240 /* Only process blocks that are known to be uptodate */
1241 if (test_bit(R5_Wantdrain, &dev->flags))
1242 xor_srcs[count++] = dev->page;
1245 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1246 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
1247 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1252 static struct dma_async_tx_descriptor *
1253 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1255 int disks = sh->disks;
1258 pr_debug("%s: stripe %llu\n", __func__,
1259 (unsigned long long)sh->sector);
1261 for (i = disks; i--; ) {
1262 struct r5dev *dev = &sh->dev[i];
1265 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
1268 spin_lock_irq(&sh->stripe_lock);
1269 chosen = dev->towrite;
1270 dev->towrite = NULL;
1271 BUG_ON(dev->written);
1272 wbi = dev->written = chosen;
1273 spin_unlock_irq(&sh->stripe_lock);
1275 while (wbi && wbi->bi_sector <
1276 dev->sector + STRIPE_SECTORS) {
1277 if (wbi->bi_rw & REQ_FUA)
1278 set_bit(R5_WantFUA, &dev->flags);
1279 if (wbi->bi_rw & REQ_SYNC)
1280 set_bit(R5_SyncIO, &dev->flags);
1281 if (wbi->bi_rw & REQ_DISCARD)
1282 set_bit(R5_Discard, &dev->flags);
1284 tx = async_copy_data(1, wbi, dev->page,
1286 wbi = r5_next_bio(wbi, dev->sector);
1294 static void ops_complete_reconstruct(void *stripe_head_ref)
1296 struct stripe_head *sh = stripe_head_ref;
1297 int disks = sh->disks;
1298 int pd_idx = sh->pd_idx;
1299 int qd_idx = sh->qd_idx;
1301 bool fua = false, sync = false, discard = false;
1303 pr_debug("%s: stripe %llu\n", __func__,
1304 (unsigned long long)sh->sector);
1306 for (i = disks; i--; ) {
1307 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1308 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1309 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1312 for (i = disks; i--; ) {
1313 struct r5dev *dev = &sh->dev[i];
1315 if (dev->written || i == pd_idx || i == qd_idx) {
1317 set_bit(R5_UPTODATE, &dev->flags);
1319 set_bit(R5_WantFUA, &dev->flags);
1321 set_bit(R5_SyncIO, &dev->flags);
1325 if (sh->reconstruct_state == reconstruct_state_drain_run)
1326 sh->reconstruct_state = reconstruct_state_drain_result;
1327 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1328 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1330 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1331 sh->reconstruct_state = reconstruct_state_result;
1334 set_bit(STRIPE_HANDLE, &sh->state);
1339 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1340 struct dma_async_tx_descriptor *tx)
1342 int disks = sh->disks;
1343 struct page **xor_srcs = percpu->scribble;
1344 struct async_submit_ctl submit;
1345 int count = 0, pd_idx = sh->pd_idx, i;
1346 struct page *xor_dest;
1348 unsigned long flags;
1350 pr_debug("%s: stripe %llu\n", __func__,
1351 (unsigned long long)sh->sector);
1353 for (i = 0; i < sh->disks; i++) {
1356 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1359 if (i >= sh->disks) {
1360 atomic_inc(&sh->count);
1361 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1362 ops_complete_reconstruct(sh);
1365 /* check if prexor is active which means only process blocks
1366 * that are part of a read-modify-write (written)
1368 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1370 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1371 for (i = disks; i--; ) {
1372 struct r5dev *dev = &sh->dev[i];
1374 xor_srcs[count++] = dev->page;
1377 xor_dest = sh->dev[pd_idx].page;
1378 for (i = disks; i--; ) {
1379 struct r5dev *dev = &sh->dev[i];
1381 xor_srcs[count++] = dev->page;
1385 /* 1/ if we prexor'd then the dest is reused as a source
1386 * 2/ if we did not prexor then we are redoing the parity
1387 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1388 * for the synchronous xor case
1390 flags = ASYNC_TX_ACK |
1391 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1393 atomic_inc(&sh->count);
1395 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1396 to_addr_conv(sh, percpu));
1397 if (unlikely(count == 1))
1398 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1400 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1404 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1405 struct dma_async_tx_descriptor *tx)
1407 struct async_submit_ctl submit;
1408 struct page **blocks = percpu->scribble;
1411 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1413 for (i = 0; i < sh->disks; i++) {
1414 if (sh->pd_idx == i || sh->qd_idx == i)
1416 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1419 if (i >= sh->disks) {
1420 atomic_inc(&sh->count);
1421 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1422 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1423 ops_complete_reconstruct(sh);
1427 count = set_syndrome_sources(blocks, sh);
1429 atomic_inc(&sh->count);
1431 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1432 sh, to_addr_conv(sh, percpu));
1433 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1436 static void ops_complete_check(void *stripe_head_ref)
1438 struct stripe_head *sh = stripe_head_ref;
1440 pr_debug("%s: stripe %llu\n", __func__,
1441 (unsigned long long)sh->sector);
1443 sh->check_state = check_state_check_result;
1444 set_bit(STRIPE_HANDLE, &sh->state);
1448 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1450 int disks = sh->disks;
1451 int pd_idx = sh->pd_idx;
1452 int qd_idx = sh->qd_idx;
1453 struct page *xor_dest;
1454 struct page **xor_srcs = percpu->scribble;
1455 struct dma_async_tx_descriptor *tx;
1456 struct async_submit_ctl submit;
1460 pr_debug("%s: stripe %llu\n", __func__,
1461 (unsigned long long)sh->sector);
1464 xor_dest = sh->dev[pd_idx].page;
1465 xor_srcs[count++] = xor_dest;
1466 for (i = disks; i--; ) {
1467 if (i == pd_idx || i == qd_idx)
1469 xor_srcs[count++] = sh->dev[i].page;
1472 init_async_submit(&submit, 0, NULL, NULL, NULL,
1473 to_addr_conv(sh, percpu));
1474 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1475 &sh->ops.zero_sum_result, &submit);
1477 atomic_inc(&sh->count);
1478 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1479 tx = async_trigger_callback(&submit);
1482 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1484 struct page **srcs = percpu->scribble;
1485 struct async_submit_ctl submit;
1488 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1489 (unsigned long long)sh->sector, checkp);
1491 count = set_syndrome_sources(srcs, sh);
1495 atomic_inc(&sh->count);
1496 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1497 sh, to_addr_conv(sh, percpu));
1498 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1499 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1502 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1504 int overlap_clear = 0, i, disks = sh->disks;
1505 struct dma_async_tx_descriptor *tx = NULL;
1506 struct r5conf *conf = sh->raid_conf;
1507 int level = conf->level;
1508 struct raid5_percpu *percpu;
1512 percpu = per_cpu_ptr(conf->percpu, cpu);
1513 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1514 ops_run_biofill(sh);
1518 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1520 tx = ops_run_compute5(sh, percpu);
1522 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1523 tx = ops_run_compute6_1(sh, percpu);
1525 tx = ops_run_compute6_2(sh, percpu);
1527 /* terminate the chain if reconstruct is not set to be run */
1528 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1532 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1533 tx = ops_run_prexor(sh, percpu, tx);
1535 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1536 tx = ops_run_biodrain(sh, tx);
1540 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1542 ops_run_reconstruct5(sh, percpu, tx);
1544 ops_run_reconstruct6(sh, percpu, tx);
1547 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1548 if (sh->check_state == check_state_run)
1549 ops_run_check_p(sh, percpu);
1550 else if (sh->check_state == check_state_run_q)
1551 ops_run_check_pq(sh, percpu, 0);
1552 else if (sh->check_state == check_state_run_pq)
1553 ops_run_check_pq(sh, percpu, 1);
1559 for (i = disks; i--; ) {
1560 struct r5dev *dev = &sh->dev[i];
1561 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1562 wake_up(&sh->raid_conf->wait_for_overlap);
1567 static int grow_one_stripe(struct r5conf *conf)
1569 struct stripe_head *sh;
1570 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
1574 sh->raid_conf = conf;
1576 spin_lock_init(&sh->stripe_lock);
1578 if (grow_buffers(sh)) {
1580 kmem_cache_free(conf->slab_cache, sh);
1583 /* we just created an active stripe so... */
1584 atomic_set(&sh->count, 1);
1585 atomic_inc(&conf->active_stripes);
1586 INIT_LIST_HEAD(&sh->lru);
1591 static int grow_stripes(struct r5conf *conf, int num)
1593 struct kmem_cache *sc;
1594 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1596 if (conf->mddev->gendisk)
1597 sprintf(conf->cache_name[0],
1598 "raid%d-%s", conf->level, mdname(conf->mddev));
1600 sprintf(conf->cache_name[0],
1601 "raid%d-%p", conf->level, conf->mddev);
1602 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1604 conf->active_name = 0;
1605 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1606 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1610 conf->slab_cache = sc;
1611 conf->pool_size = devs;
1613 if (!grow_one_stripe(conf))
1619 * scribble_len - return the required size of the scribble region
1620 * @num - total number of disks in the array
1622 * The size must be enough to contain:
1623 * 1/ a struct page pointer for each device in the array +2
1624 * 2/ room to convert each entry in (1) to its corresponding dma
1625 * (dma_map_page()) or page (page_address()) address.
1627 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1628 * calculate over all devices (not just the data blocks), using zeros in place
1629 * of the P and Q blocks.
1631 static size_t scribble_len(int num)
1635 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1640 static int resize_stripes(struct r5conf *conf, int newsize)
1642 /* Make all the stripes able to hold 'newsize' devices.
1643 * New slots in each stripe get 'page' set to a new page.
1645 * This happens in stages:
1646 * 1/ create a new kmem_cache and allocate the required number of
1648 * 2/ gather all the old stripe_heads and transfer the pages across
1649 * to the new stripe_heads. This will have the side effect of
1650 * freezing the array as once all stripe_heads have been collected,
1651 * no IO will be possible. Old stripe heads are freed once their
1652 * pages have been transferred over, and the old kmem_cache is
1653 * freed when all stripes are done.
1654 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1655 * we simple return a failre status - no need to clean anything up.
1656 * 4/ allocate new pages for the new slots in the new stripe_heads.
1657 * If this fails, we don't bother trying the shrink the
1658 * stripe_heads down again, we just leave them as they are.
1659 * As each stripe_head is processed the new one is released into
1662 * Once step2 is started, we cannot afford to wait for a write,
1663 * so we use GFP_NOIO allocations.
1665 struct stripe_head *osh, *nsh;
1666 LIST_HEAD(newstripes);
1667 struct disk_info *ndisks;
1670 struct kmem_cache *sc;
1673 if (newsize <= conf->pool_size)
1674 return 0; /* never bother to shrink */
1676 err = md_allow_write(conf->mddev);
1681 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1682 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1687 for (i = conf->max_nr_stripes; i; i--) {
1688 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
1692 nsh->raid_conf = conf;
1693 spin_lock_init(&nsh->stripe_lock);
1695 list_add(&nsh->lru, &newstripes);
1698 /* didn't get enough, give up */
1699 while (!list_empty(&newstripes)) {
1700 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1701 list_del(&nsh->lru);
1702 kmem_cache_free(sc, nsh);
1704 kmem_cache_destroy(sc);
1707 /* Step 2 - Must use GFP_NOIO now.
1708 * OK, we have enough stripes, start collecting inactive
1709 * stripes and copying them over
1711 list_for_each_entry(nsh, &newstripes, lru) {
1712 spin_lock_irq(&conf->device_lock);
1713 wait_event_lock_irq(conf->wait_for_stripe,
1714 !list_empty(&conf->inactive_list),
1716 osh = get_free_stripe(conf);
1717 spin_unlock_irq(&conf->device_lock);
1718 atomic_set(&nsh->count, 1);
1719 for(i=0; i<conf->pool_size; i++)
1720 nsh->dev[i].page = osh->dev[i].page;
1721 for( ; i<newsize; i++)
1722 nsh->dev[i].page = NULL;
1723 kmem_cache_free(conf->slab_cache, osh);
1725 kmem_cache_destroy(conf->slab_cache);
1728 * At this point, we are holding all the stripes so the array
1729 * is completely stalled, so now is a good time to resize
1730 * conf->disks and the scribble region
1732 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1734 for (i=0; i<conf->raid_disks; i++)
1735 ndisks[i] = conf->disks[i];
1737 conf->disks = ndisks;
1742 conf->scribble_len = scribble_len(newsize);
1743 for_each_present_cpu(cpu) {
1744 struct raid5_percpu *percpu;
1747 percpu = per_cpu_ptr(conf->percpu, cpu);
1748 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1751 kfree(percpu->scribble);
1752 percpu->scribble = scribble;
1760 /* Step 4, return new stripes to service */
1761 while(!list_empty(&newstripes)) {
1762 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1763 list_del_init(&nsh->lru);
1765 for (i=conf->raid_disks; i < newsize; i++)
1766 if (nsh->dev[i].page == NULL) {
1767 struct page *p = alloc_page(GFP_NOIO);
1768 nsh->dev[i].page = p;
1772 release_stripe(nsh);
1774 /* critical section pass, GFP_NOIO no longer needed */
1776 conf->slab_cache = sc;
1777 conf->active_name = 1-conf->active_name;
1778 conf->pool_size = newsize;
1782 static int drop_one_stripe(struct r5conf *conf)
1784 struct stripe_head *sh;
1786 spin_lock_irq(&conf->device_lock);
1787 sh = get_free_stripe(conf);
1788 spin_unlock_irq(&conf->device_lock);
1791 BUG_ON(atomic_read(&sh->count));
1793 kmem_cache_free(conf->slab_cache, sh);
1794 atomic_dec(&conf->active_stripes);
1798 static void shrink_stripes(struct r5conf *conf)
1800 while (drop_one_stripe(conf))
1803 if (conf->slab_cache)
1804 kmem_cache_destroy(conf->slab_cache);
1805 conf->slab_cache = NULL;
1808 static void raid5_end_read_request(struct bio * bi, int error)
1810 struct stripe_head *sh = bi->bi_private;
1811 struct r5conf *conf = sh->raid_conf;
1812 int disks = sh->disks, i;
1813 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1814 char b[BDEVNAME_SIZE];
1815 struct md_rdev *rdev = NULL;
1818 for (i=0 ; i<disks; i++)
1819 if (bi == &sh->dev[i].req)
1822 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1823 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1829 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1830 /* If replacement finished while this request was outstanding,
1831 * 'replacement' might be NULL already.
1832 * In that case it moved down to 'rdev'.
1833 * rdev is not removed until all requests are finished.
1835 rdev = conf->disks[i].replacement;
1837 rdev = conf->disks[i].rdev;
1839 if (use_new_offset(conf, sh))
1840 s = sh->sector + rdev->new_data_offset;
1842 s = sh->sector + rdev->data_offset;
1844 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1845 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1846 /* Note that this cannot happen on a
1847 * replacement device. We just fail those on
1852 "md/raid:%s: read error corrected"
1853 " (%lu sectors at %llu on %s)\n",
1854 mdname(conf->mddev), STRIPE_SECTORS,
1855 (unsigned long long)s,
1856 bdevname(rdev->bdev, b));
1857 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1858 clear_bit(R5_ReadError, &sh->dev[i].flags);
1859 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1860 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1861 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1863 if (atomic_read(&rdev->read_errors))
1864 atomic_set(&rdev->read_errors, 0);
1866 const char *bdn = bdevname(rdev->bdev, b);
1870 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1871 atomic_inc(&rdev->read_errors);
1872 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1875 "md/raid:%s: read error on replacement device "
1876 "(sector %llu on %s).\n",
1877 mdname(conf->mddev),
1878 (unsigned long long)s,
1880 else if (conf->mddev->degraded >= conf->max_degraded) {
1884 "md/raid:%s: read error not correctable "
1885 "(sector %llu on %s).\n",
1886 mdname(conf->mddev),
1887 (unsigned long long)s,
1889 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
1894 "md/raid:%s: read error NOT corrected!! "
1895 "(sector %llu on %s).\n",
1896 mdname(conf->mddev),
1897 (unsigned long long)s,
1899 } else if (atomic_read(&rdev->read_errors)
1900 > conf->max_nr_stripes)
1902 "md/raid:%s: Too many read errors, failing device %s.\n",
1903 mdname(conf->mddev), bdn);
1907 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1908 set_bit(R5_ReadError, &sh->dev[i].flags);
1909 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1911 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1913 clear_bit(R5_ReadError, &sh->dev[i].flags);
1914 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1916 && test_bit(In_sync, &rdev->flags)
1917 && rdev_set_badblocks(
1918 rdev, sh->sector, STRIPE_SECTORS, 0)))
1919 md_error(conf->mddev, rdev);
1922 rdev_dec_pending(rdev, conf->mddev);
1923 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1924 set_bit(STRIPE_HANDLE, &sh->state);
1928 static void raid5_end_write_request(struct bio *bi, int error)
1930 struct stripe_head *sh = bi->bi_private;
1931 struct r5conf *conf = sh->raid_conf;
1932 int disks = sh->disks, i;
1933 struct md_rdev *uninitialized_var(rdev);
1934 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1937 int replacement = 0;
1939 for (i = 0 ; i < disks; i++) {
1940 if (bi == &sh->dev[i].req) {
1941 rdev = conf->disks[i].rdev;
1944 if (bi == &sh->dev[i].rreq) {
1945 rdev = conf->disks[i].replacement;
1949 /* rdev was removed and 'replacement'
1950 * replaced it. rdev is not removed
1951 * until all requests are finished.
1953 rdev = conf->disks[i].rdev;
1957 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1958 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1967 md_error(conf->mddev, rdev);
1968 else if (is_badblock(rdev, sh->sector,
1970 &first_bad, &bad_sectors))
1971 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1974 set_bit(WriteErrorSeen, &rdev->flags);
1975 set_bit(R5_WriteError, &sh->dev[i].flags);
1976 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1977 set_bit(MD_RECOVERY_NEEDED,
1978 &rdev->mddev->recovery);
1979 } else if (is_badblock(rdev, sh->sector,
1981 &first_bad, &bad_sectors)) {
1982 set_bit(R5_MadeGood, &sh->dev[i].flags);
1983 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1984 /* That was a successful write so make
1985 * sure it looks like we already did
1988 set_bit(R5_ReWrite, &sh->dev[i].flags);
1991 rdev_dec_pending(rdev, conf->mddev);
1993 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1994 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1995 set_bit(STRIPE_HANDLE, &sh->state);
1999 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
2001 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
2003 struct r5dev *dev = &sh->dev[i];
2005 bio_init(&dev->req);
2006 dev->req.bi_io_vec = &dev->vec;
2008 dev->req.bi_max_vecs++;
2009 dev->req.bi_private = sh;
2010 dev->vec.bv_page = dev->page;
2012 bio_init(&dev->rreq);
2013 dev->rreq.bi_io_vec = &dev->rvec;
2014 dev->rreq.bi_vcnt++;
2015 dev->rreq.bi_max_vecs++;
2016 dev->rreq.bi_private = sh;
2017 dev->rvec.bv_page = dev->page;
2020 dev->sector = compute_blocknr(sh, i, previous);
2023 static void error(struct mddev *mddev, struct md_rdev *rdev)
2025 char b[BDEVNAME_SIZE];
2026 struct r5conf *conf = mddev->private;
2027 unsigned long flags;
2028 pr_debug("raid456: error called\n");
2030 spin_lock_irqsave(&conf->device_lock, flags);
2031 clear_bit(In_sync, &rdev->flags);
2032 mddev->degraded = calc_degraded(conf);
2033 spin_unlock_irqrestore(&conf->device_lock, flags);
2034 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2036 set_bit(Blocked, &rdev->flags);
2037 set_bit(Faulty, &rdev->flags);
2038 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2040 "md/raid:%s: Disk failure on %s, disabling device.\n"
2041 "md/raid:%s: Operation continuing on %d devices.\n",
2043 bdevname(rdev->bdev, b),
2045 conf->raid_disks - mddev->degraded);
2049 * Input: a 'big' sector number,
2050 * Output: index of the data and parity disk, and the sector # in them.
2052 static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2053 int previous, int *dd_idx,
2054 struct stripe_head *sh)
2056 sector_t stripe, stripe2;
2057 sector_t chunk_number;
2058 unsigned int chunk_offset;
2061 sector_t new_sector;
2062 int algorithm = previous ? conf->prev_algo
2064 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2065 : conf->chunk_sectors;
2066 int raid_disks = previous ? conf->previous_raid_disks
2068 int data_disks = raid_disks - conf->max_degraded;
2070 /* First compute the information on this sector */
2073 * Compute the chunk number and the sector offset inside the chunk
2075 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2076 chunk_number = r_sector;
2079 * Compute the stripe number
2081 stripe = chunk_number;
2082 *dd_idx = sector_div(stripe, data_disks);
2085 * Select the parity disk based on the user selected algorithm.
2087 pd_idx = qd_idx = -1;
2088 switch(conf->level) {
2090 pd_idx = data_disks;
2093 switch (algorithm) {
2094 case ALGORITHM_LEFT_ASYMMETRIC:
2095 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2096 if (*dd_idx >= pd_idx)
2099 case ALGORITHM_RIGHT_ASYMMETRIC:
2100 pd_idx = sector_div(stripe2, raid_disks);
2101 if (*dd_idx >= pd_idx)
2104 case ALGORITHM_LEFT_SYMMETRIC:
2105 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2106 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2108 case ALGORITHM_RIGHT_SYMMETRIC:
2109 pd_idx = sector_div(stripe2, raid_disks);
2110 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2112 case ALGORITHM_PARITY_0:
2116 case ALGORITHM_PARITY_N:
2117 pd_idx = data_disks;
2125 switch (algorithm) {
2126 case ALGORITHM_LEFT_ASYMMETRIC:
2127 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2128 qd_idx = pd_idx + 1;
2129 if (pd_idx == raid_disks-1) {
2130 (*dd_idx)++; /* Q D D D P */
2132 } else if (*dd_idx >= pd_idx)
2133 (*dd_idx) += 2; /* D D P Q D */
2135 case ALGORITHM_RIGHT_ASYMMETRIC:
2136 pd_idx = sector_div(stripe2, raid_disks);
2137 qd_idx = pd_idx + 1;
2138 if (pd_idx == raid_disks-1) {
2139 (*dd_idx)++; /* Q D D D P */
2141 } else if (*dd_idx >= pd_idx)
2142 (*dd_idx) += 2; /* D D P Q D */
2144 case ALGORITHM_LEFT_SYMMETRIC:
2145 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2146 qd_idx = (pd_idx + 1) % raid_disks;
2147 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2149 case ALGORITHM_RIGHT_SYMMETRIC:
2150 pd_idx = sector_div(stripe2, raid_disks);
2151 qd_idx = (pd_idx + 1) % raid_disks;
2152 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
2155 case ALGORITHM_PARITY_0:
2160 case ALGORITHM_PARITY_N:
2161 pd_idx = data_disks;
2162 qd_idx = data_disks + 1;
2165 case ALGORITHM_ROTATING_ZERO_RESTART:
2166 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2167 * of blocks for computing Q is different.
2169 pd_idx = sector_div(stripe2, raid_disks);
2170 qd_idx = pd_idx + 1;
2171 if (pd_idx == raid_disks-1) {
2172 (*dd_idx)++; /* Q D D D P */
2174 } else if (*dd_idx >= pd_idx)
2175 (*dd_idx) += 2; /* D D P Q D */
2179 case ALGORITHM_ROTATING_N_RESTART:
2180 /* Same a left_asymmetric, by first stripe is
2181 * D D D P Q rather than
2185 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2186 qd_idx = pd_idx + 1;
2187 if (pd_idx == raid_disks-1) {
2188 (*dd_idx)++; /* Q D D D P */
2190 } else if (*dd_idx >= pd_idx)
2191 (*dd_idx) += 2; /* D D P Q D */
2195 case ALGORITHM_ROTATING_N_CONTINUE:
2196 /* Same as left_symmetric but Q is before P */
2197 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2198 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2199 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2203 case ALGORITHM_LEFT_ASYMMETRIC_6:
2204 /* RAID5 left_asymmetric, with Q on last device */
2205 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2206 if (*dd_idx >= pd_idx)
2208 qd_idx = raid_disks - 1;
2211 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2212 pd_idx = sector_div(stripe2, raid_disks-1);
2213 if (*dd_idx >= pd_idx)
2215 qd_idx = raid_disks - 1;
2218 case ALGORITHM_LEFT_SYMMETRIC_6:
2219 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
2220 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2221 qd_idx = raid_disks - 1;
2224 case ALGORITHM_RIGHT_SYMMETRIC_6:
2225 pd_idx = sector_div(stripe2, raid_disks-1);
2226 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2227 qd_idx = raid_disks - 1;
2230 case ALGORITHM_PARITY_0_6:
2233 qd_idx = raid_disks - 1;
2243 sh->pd_idx = pd_idx;
2244 sh->qd_idx = qd_idx;
2245 sh->ddf_layout = ddf_layout;
2248 * Finally, compute the new sector number
2250 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2255 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
2257 struct r5conf *conf = sh->raid_conf;
2258 int raid_disks = sh->disks;
2259 int data_disks = raid_disks - conf->max_degraded;
2260 sector_t new_sector = sh->sector, check;
2261 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2262 : conf->chunk_sectors;
2263 int algorithm = previous ? conf->prev_algo
2267 sector_t chunk_number;
2268 int dummy1, dd_idx = i;
2270 struct stripe_head sh2;
2273 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2274 stripe = new_sector;
2276 if (i == sh->pd_idx)
2278 switch(conf->level) {
2281 switch (algorithm) {
2282 case ALGORITHM_LEFT_ASYMMETRIC:
2283 case ALGORITHM_RIGHT_ASYMMETRIC:
2287 case ALGORITHM_LEFT_SYMMETRIC:
2288 case ALGORITHM_RIGHT_SYMMETRIC:
2291 i -= (sh->pd_idx + 1);
2293 case ALGORITHM_PARITY_0:
2296 case ALGORITHM_PARITY_N:
2303 if (i == sh->qd_idx)
2304 return 0; /* It is the Q disk */
2305 switch (algorithm) {
2306 case ALGORITHM_LEFT_ASYMMETRIC:
2307 case ALGORITHM_RIGHT_ASYMMETRIC:
2308 case ALGORITHM_ROTATING_ZERO_RESTART:
2309 case ALGORITHM_ROTATING_N_RESTART:
2310 if (sh->pd_idx == raid_disks-1)
2311 i--; /* Q D D D P */
2312 else if (i > sh->pd_idx)
2313 i -= 2; /* D D P Q D */
2315 case ALGORITHM_LEFT_SYMMETRIC:
2316 case ALGORITHM_RIGHT_SYMMETRIC:
2317 if (sh->pd_idx == raid_disks-1)
2318 i--; /* Q D D D P */
2323 i -= (sh->pd_idx + 2);
2326 case ALGORITHM_PARITY_0:
2329 case ALGORITHM_PARITY_N:
2331 case ALGORITHM_ROTATING_N_CONTINUE:
2332 /* Like left_symmetric, but P is before Q */
2333 if (sh->pd_idx == 0)
2334 i--; /* P D D D Q */
2339 i -= (sh->pd_idx + 1);
2342 case ALGORITHM_LEFT_ASYMMETRIC_6:
2343 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2347 case ALGORITHM_LEFT_SYMMETRIC_6:
2348 case ALGORITHM_RIGHT_SYMMETRIC_6:
2350 i += data_disks + 1;
2351 i -= (sh->pd_idx + 1);
2353 case ALGORITHM_PARITY_0_6:
2362 chunk_number = stripe * data_disks + i;
2363 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
2365 check = raid5_compute_sector(conf, r_sector,
2366 previous, &dummy1, &sh2);
2367 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2368 || sh2.qd_idx != sh->qd_idx) {
2369 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2370 mdname(conf->mddev));
2378 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
2379 int rcw, int expand)
2381 int i, pd_idx = sh->pd_idx, disks = sh->disks;
2382 struct r5conf *conf = sh->raid_conf;
2383 int level = conf->level;
2387 for (i = disks; i--; ) {
2388 struct r5dev *dev = &sh->dev[i];
2391 set_bit(R5_LOCKED, &dev->flags);
2392 set_bit(R5_Wantdrain, &dev->flags);
2394 clear_bit(R5_UPTODATE, &dev->flags);
2398 /* if we are not expanding this is a proper write request, and
2399 * there will be bios with new data to be drained into the
2404 /* False alarm, nothing to do */
2406 sh->reconstruct_state = reconstruct_state_drain_run;
2407 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2409 sh->reconstruct_state = reconstruct_state_run;
2411 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2413 if (s->locked + conf->max_degraded == disks)
2414 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2415 atomic_inc(&conf->pending_full_writes);
2418 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2419 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2421 for (i = disks; i--; ) {
2422 struct r5dev *dev = &sh->dev[i];
2427 (test_bit(R5_UPTODATE, &dev->flags) ||
2428 test_bit(R5_Wantcompute, &dev->flags))) {
2429 set_bit(R5_Wantdrain, &dev->flags);
2430 set_bit(R5_LOCKED, &dev->flags);
2431 clear_bit(R5_UPTODATE, &dev->flags);
2436 /* False alarm - nothing to do */
2438 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2439 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2440 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2441 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2444 /* keep the parity disk(s) locked while asynchronous operations
2447 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2448 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2452 int qd_idx = sh->qd_idx;
2453 struct r5dev *dev = &sh->dev[qd_idx];
2455 set_bit(R5_LOCKED, &dev->flags);
2456 clear_bit(R5_UPTODATE, &dev->flags);
2460 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2461 __func__, (unsigned long long)sh->sector,
2462 s->locked, s->ops_request);
2466 * Each stripe/dev can have one or more bion attached.
2467 * toread/towrite point to the first in a chain.
2468 * The bi_next chain must be in order.
2470 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2473 struct r5conf *conf = sh->raid_conf;
2476 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2477 (unsigned long long)bi->bi_sector,
2478 (unsigned long long)sh->sector);
2481 * If several bio share a stripe. The bio bi_phys_segments acts as a
2482 * reference count to avoid race. The reference count should already be
2483 * increased before this function is called (for example, in
2484 * make_request()), so other bio sharing this stripe will not free the
2485 * stripe. If a stripe is owned by one stripe, the stripe lock will
2488 spin_lock_irq(&sh->stripe_lock);
2490 bip = &sh->dev[dd_idx].towrite;
2494 bip = &sh->dev[dd_idx].toread;
2495 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2496 if (bio_end_sector(*bip) > bi->bi_sector)
2498 bip = & (*bip)->bi_next;
2500 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
2503 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2507 raid5_inc_bi_active_stripes(bi);
2510 /* check if page is covered */
2511 sector_t sector = sh->dev[dd_idx].sector;
2512 for (bi=sh->dev[dd_idx].towrite;
2513 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2514 bi && bi->bi_sector <= sector;
2515 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2516 if (bio_end_sector(bi) >= sector)
2517 sector = bio_end_sector(bi);
2519 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2520 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2523 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2524 (unsigned long long)(*bip)->bi_sector,
2525 (unsigned long long)sh->sector, dd_idx);
2526 spin_unlock_irq(&sh->stripe_lock);
2528 if (conf->mddev->bitmap && firstwrite) {
2529 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2531 sh->bm_seq = conf->seq_flush+1;
2532 set_bit(STRIPE_BIT_DELAY, &sh->state);
2537 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2538 spin_unlock_irq(&sh->stripe_lock);
2542 static void end_reshape(struct r5conf *conf);
2544 static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
2545 struct stripe_head *sh)
2547 int sectors_per_chunk =
2548 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2550 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2551 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2553 raid5_compute_sector(conf,
2554 stripe * (disks - conf->max_degraded)
2555 *sectors_per_chunk + chunk_offset,
2561 handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
2562 struct stripe_head_state *s, int disks,
2563 struct bio **return_bi)
2566 for (i = disks; i--; ) {
2570 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2571 struct md_rdev *rdev;
2573 rdev = rcu_dereference(conf->disks[i].rdev);
2574 if (rdev && test_bit(In_sync, &rdev->flags))
2575 atomic_inc(&rdev->nr_pending);
2580 if (!rdev_set_badblocks(
2584 md_error(conf->mddev, rdev);
2585 rdev_dec_pending(rdev, conf->mddev);
2588 spin_lock_irq(&sh->stripe_lock);
2589 /* fail all writes first */
2590 bi = sh->dev[i].towrite;
2591 sh->dev[i].towrite = NULL;
2592 spin_unlock_irq(&sh->stripe_lock);
2596 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2597 wake_up(&conf->wait_for_overlap);
2599 while (bi && bi->bi_sector <
2600 sh->dev[i].sector + STRIPE_SECTORS) {
2601 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2602 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2603 if (!raid5_dec_bi_active_stripes(bi)) {
2604 md_write_end(conf->mddev);
2605 bi->bi_next = *return_bi;
2611 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2612 STRIPE_SECTORS, 0, 0);
2614 /* and fail all 'written' */
2615 bi = sh->dev[i].written;
2616 sh->dev[i].written = NULL;
2617 if (bi) bitmap_end = 1;
2618 while (bi && bi->bi_sector <
2619 sh->dev[i].sector + STRIPE_SECTORS) {
2620 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2621 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2622 if (!raid5_dec_bi_active_stripes(bi)) {
2623 md_write_end(conf->mddev);
2624 bi->bi_next = *return_bi;
2630 /* fail any reads if this device is non-operational and
2631 * the data has not reached the cache yet.
2633 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2634 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2635 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2636 spin_lock_irq(&sh->stripe_lock);
2637 bi = sh->dev[i].toread;
2638 sh->dev[i].toread = NULL;
2639 spin_unlock_irq(&sh->stripe_lock);
2640 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2641 wake_up(&conf->wait_for_overlap);
2642 while (bi && bi->bi_sector <
2643 sh->dev[i].sector + STRIPE_SECTORS) {
2644 struct bio *nextbi =
2645 r5_next_bio(bi, sh->dev[i].sector);
2646 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2647 if (!raid5_dec_bi_active_stripes(bi)) {
2648 bi->bi_next = *return_bi;
2655 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2656 STRIPE_SECTORS, 0, 0);
2657 /* If we were in the middle of a write the parity block might
2658 * still be locked - so just clear all R5_LOCKED flags
2660 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2663 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2664 if (atomic_dec_and_test(&conf->pending_full_writes))
2665 md_wakeup_thread(conf->mddev->thread);
2669 handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
2670 struct stripe_head_state *s)
2675 clear_bit(STRIPE_SYNCING, &sh->state);
2676 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2677 wake_up(&conf->wait_for_overlap);
2680 /* There is nothing more to do for sync/check/repair.
2681 * Don't even need to abort as that is handled elsewhere
2682 * if needed, and not always wanted e.g. if there is a known
2684 * For recover/replace we need to record a bad block on all
2685 * non-sync devices, or abort the recovery
2687 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2688 /* During recovery devices cannot be removed, so
2689 * locking and refcounting of rdevs is not needed
2691 for (i = 0; i < conf->raid_disks; i++) {
2692 struct md_rdev *rdev = conf->disks[i].rdev;
2694 && !test_bit(Faulty, &rdev->flags)
2695 && !test_bit(In_sync, &rdev->flags)
2696 && !rdev_set_badblocks(rdev, sh->sector,
2699 rdev = conf->disks[i].replacement;
2701 && !test_bit(Faulty, &rdev->flags)
2702 && !test_bit(In_sync, &rdev->flags)
2703 && !rdev_set_badblocks(rdev, sh->sector,
2708 conf->recovery_disabled =
2709 conf->mddev->recovery_disabled;
2711 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
2714 static int want_replace(struct stripe_head *sh, int disk_idx)
2716 struct md_rdev *rdev;
2718 /* Doing recovery so rcu locking not required */
2719 rdev = sh->raid_conf->disks[disk_idx].replacement;
2721 && !test_bit(Faulty, &rdev->flags)
2722 && !test_bit(In_sync, &rdev->flags)
2723 && (rdev->recovery_offset <= sh->sector
2724 || rdev->mddev->recovery_cp <= sh->sector))
2730 /* fetch_block - checks the given member device to see if its data needs
2731 * to be read or computed to satisfy a request.
2733 * Returns 1 when no more member devices need to be checked, otherwise returns
2734 * 0 to tell the loop in handle_stripe_fill to continue
2736 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2737 int disk_idx, int disks)
2739 struct r5dev *dev = &sh->dev[disk_idx];
2740 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2741 &sh->dev[s->failed_num[1]] };
2743 /* is the data in this block needed, and can we get it? */
2744 if (!test_bit(R5_LOCKED, &dev->flags) &&
2745 !test_bit(R5_UPTODATE, &dev->flags) &&
2747 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2748 s->syncing || s->expanding ||
2749 (s->replacing && want_replace(sh, disk_idx)) ||
2750 (s->failed >= 1 && fdev[0]->toread) ||
2751 (s->failed >= 2 && fdev[1]->toread) ||
2752 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2753 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2754 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
2755 /* we would like to get this block, possibly by computing it,
2756 * otherwise read it if the backing disk is insync
2758 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2759 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2760 if ((s->uptodate == disks - 1) &&
2761 (s->failed && (disk_idx == s->failed_num[0] ||
2762 disk_idx == s->failed_num[1]))) {
2763 /* have disk failed, and we're requested to fetch it;
2766 pr_debug("Computing stripe %llu block %d\n",
2767 (unsigned long long)sh->sector, disk_idx);
2768 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2769 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2770 set_bit(R5_Wantcompute, &dev->flags);
2771 sh->ops.target = disk_idx;
2772 sh->ops.target2 = -1; /* no 2nd target */
2774 /* Careful: from this point on 'uptodate' is in the eye
2775 * of raid_run_ops which services 'compute' operations
2776 * before writes. R5_Wantcompute flags a block that will
2777 * be R5_UPTODATE by the time it is needed for a
2778 * subsequent operation.
2782 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2783 /* Computing 2-failure is *very* expensive; only
2784 * do it if failed >= 2
2787 for (other = disks; other--; ) {
2788 if (other == disk_idx)
2790 if (!test_bit(R5_UPTODATE,
2791 &sh->dev[other].flags))
2795 pr_debug("Computing stripe %llu blocks %d,%d\n",
2796 (unsigned long long)sh->sector,
2798 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2799 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2800 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2801 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2802 sh->ops.target = disk_idx;
2803 sh->ops.target2 = other;
2807 } else if (test_bit(R5_Insync, &dev->flags)) {
2808 set_bit(R5_LOCKED, &dev->flags);
2809 set_bit(R5_Wantread, &dev->flags);
2811 pr_debug("Reading block %d (sync=%d)\n",
2812 disk_idx, s->syncing);
2820 * handle_stripe_fill - read or compute data to satisfy pending requests.
2822 static void handle_stripe_fill(struct stripe_head *sh,
2823 struct stripe_head_state *s,
2828 /* look for blocks to read/compute, skip this if a compute
2829 * is already in flight, or if the stripe contents are in the
2830 * midst of changing due to a write
2832 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2833 !sh->reconstruct_state)
2834 for (i = disks; i--; )
2835 if (fetch_block(sh, s, i, disks))
2837 set_bit(STRIPE_HANDLE, &sh->state);
2841 /* handle_stripe_clean_event
2842 * any written block on an uptodate or failed drive can be returned.
2843 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2844 * never LOCKED, so we don't need to test 'failed' directly.
2846 static void handle_stripe_clean_event(struct r5conf *conf,
2847 struct stripe_head *sh, int disks, struct bio **return_bi)
2851 int discard_pending = 0;
2853 for (i = disks; i--; )
2854 if (sh->dev[i].written) {
2856 if (!test_bit(R5_LOCKED, &dev->flags) &&
2857 (test_bit(R5_UPTODATE, &dev->flags) ||
2858 test_bit(R5_Discard, &dev->flags))) {
2859 /* We can return any write requests */
2860 struct bio *wbi, *wbi2;
2861 pr_debug("Return write for disc %d\n", i);
2862 if (test_and_clear_bit(R5_Discard, &dev->flags))
2863 clear_bit(R5_UPTODATE, &dev->flags);
2865 dev->written = NULL;
2866 while (wbi && wbi->bi_sector <
2867 dev->sector + STRIPE_SECTORS) {
2868 wbi2 = r5_next_bio(wbi, dev->sector);
2869 if (!raid5_dec_bi_active_stripes(wbi)) {
2870 md_write_end(conf->mddev);
2871 wbi->bi_next = *return_bi;
2876 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2878 !test_bit(STRIPE_DEGRADED, &sh->state),
2880 } else if (test_bit(R5_Discard, &dev->flags))
2881 discard_pending = 1;
2883 if (!discard_pending &&
2884 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2885 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2886 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2887 if (sh->qd_idx >= 0) {
2888 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2889 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2891 /* now that discard is done we can proceed with any sync */
2892 clear_bit(STRIPE_DISCARD, &sh->state);
2893 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2894 set_bit(STRIPE_HANDLE, &sh->state);
2898 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2899 if (atomic_dec_and_test(&conf->pending_full_writes))
2900 md_wakeup_thread(conf->mddev->thread);
2903 static void handle_stripe_dirtying(struct r5conf *conf,
2904 struct stripe_head *sh,
2905 struct stripe_head_state *s,
2908 int rmw = 0, rcw = 0, i;
2909 sector_t recovery_cp = conf->mddev->recovery_cp;
2911 /* RAID6 requires 'rcw' in current implementation.
2912 * Otherwise, check whether resync is now happening or should start.
2913 * If yes, then the array is dirty (after unclean shutdown or
2914 * initial creation), so parity in some stripes might be inconsistent.
2915 * In this case, we need to always do reconstruct-write, to ensure
2916 * that in case of drive failure or read-error correction, we
2917 * generate correct data from the parity.
2919 if (conf->max_degraded == 2 ||
2920 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2921 /* Calculate the real rcw later - for now make it
2922 * look like rcw is cheaper
2925 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2926 conf->max_degraded, (unsigned long long)recovery_cp,
2927 (unsigned long long)sh->sector);
2928 } else for (i = disks; i--; ) {
2929 /* would I have to read this buffer for read_modify_write */
2930 struct r5dev *dev = &sh->dev[i];
2931 if ((dev->towrite || i == sh->pd_idx) &&
2932 !test_bit(R5_LOCKED, &dev->flags) &&
2933 !(test_bit(R5_UPTODATE, &dev->flags) ||
2934 test_bit(R5_Wantcompute, &dev->flags))) {
2935 if (test_bit(R5_Insync, &dev->flags))
2938 rmw += 2*disks; /* cannot read it */
2940 /* Would I have to read this buffer for reconstruct_write */
2941 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2942 !test_bit(R5_LOCKED, &dev->flags) &&
2943 !(test_bit(R5_UPTODATE, &dev->flags) ||
2944 test_bit(R5_Wantcompute, &dev->flags))) {
2945 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2950 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2951 (unsigned long long)sh->sector, rmw, rcw);
2952 set_bit(STRIPE_HANDLE, &sh->state);
2953 if (rmw < rcw && rmw > 0) {
2954 /* prefer read-modify-write, but need to get some data */
2955 if (conf->mddev->queue)
2956 blk_add_trace_msg(conf->mddev->queue,
2957 "raid5 rmw %llu %d",
2958 (unsigned long long)sh->sector, rmw);
2959 for (i = disks; i--; ) {
2960 struct r5dev *dev = &sh->dev[i];
2961 if ((dev->towrite || i == sh->pd_idx) &&
2962 !test_bit(R5_LOCKED, &dev->flags) &&
2963 !(test_bit(R5_UPTODATE, &dev->flags) ||
2964 test_bit(R5_Wantcompute, &dev->flags)) &&
2965 test_bit(R5_Insync, &dev->flags)) {
2967 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2968 pr_debug("Read_old block "
2969 "%d for r-m-w\n", i);
2970 set_bit(R5_LOCKED, &dev->flags);
2971 set_bit(R5_Wantread, &dev->flags);
2974 set_bit(STRIPE_DELAYED, &sh->state);
2975 set_bit(STRIPE_HANDLE, &sh->state);
2980 if (rcw <= rmw && rcw > 0) {
2981 /* want reconstruct write, but need to get some data */
2984 for (i = disks; i--; ) {
2985 struct r5dev *dev = &sh->dev[i];
2986 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2987 i != sh->pd_idx && i != sh->qd_idx &&
2988 !test_bit(R5_LOCKED, &dev->flags) &&
2989 !(test_bit(R5_UPTODATE, &dev->flags) ||
2990 test_bit(R5_Wantcompute, &dev->flags))) {
2992 if (!test_bit(R5_Insync, &dev->flags))
2993 continue; /* it's a failed drive */
2995 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2996 pr_debug("Read_old block "
2997 "%d for Reconstruct\n", i);
2998 set_bit(R5_LOCKED, &dev->flags);
2999 set_bit(R5_Wantread, &dev->flags);
3003 set_bit(STRIPE_DELAYED, &sh->state);
3004 set_bit(STRIPE_HANDLE, &sh->state);
3008 if (rcw && conf->mddev->queue)
3009 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3010 (unsigned long long)sh->sector,
3011 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
3013 /* now if nothing is locked, and if we have enough data,
3014 * we can start a write request
3016 /* since handle_stripe can be called at any time we need to handle the
3017 * case where a compute block operation has been submitted and then a
3018 * subsequent call wants to start a write request. raid_run_ops only
3019 * handles the case where compute block and reconstruct are requested
3020 * simultaneously. If this is not the case then new writes need to be
3021 * held off until the compute completes.
3023 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3024 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3025 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
3026 schedule_reconstruction(sh, s, rcw == 0, 0);
3029 static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
3030 struct stripe_head_state *s, int disks)
3032 struct r5dev *dev = NULL;
3034 set_bit(STRIPE_HANDLE, &sh->state);
3036 switch (sh->check_state) {
3037 case check_state_idle:
3038 /* start a new check operation if there are no failures */
3039 if (s->failed == 0) {
3040 BUG_ON(s->uptodate != disks);
3041 sh->check_state = check_state_run;
3042 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3043 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3047 dev = &sh->dev[s->failed_num[0]];
3049 case check_state_compute_result:
3050 sh->check_state = check_state_idle;
3052 dev = &sh->dev[sh->pd_idx];
3054 /* check that a write has not made the stripe insync */
3055 if (test_bit(STRIPE_INSYNC, &sh->state))
3058 /* either failed parity check, or recovery is happening */
3059 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3060 BUG_ON(s->uptodate != disks);
3062 set_bit(R5_LOCKED, &dev->flags);
3064 set_bit(R5_Wantwrite, &dev->flags);
3066 clear_bit(STRIPE_DEGRADED, &sh->state);
3067 set_bit(STRIPE_INSYNC, &sh->state);
3069 case check_state_run:
3070 break; /* we will be called again upon completion */
3071 case check_state_check_result:
3072 sh->check_state = check_state_idle;
3074 /* if a failure occurred during the check operation, leave
3075 * STRIPE_INSYNC not set and let the stripe be handled again
3080 /* handle a successful check operation, if parity is correct
3081 * we are done. Otherwise update the mismatch count and repair
3082 * parity if !MD_RECOVERY_CHECK
3084 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
3085 /* parity is correct (on disc,
3086 * not in buffer any more)
3088 set_bit(STRIPE_INSYNC, &sh->state);
3090 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
3091 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3092 /* don't try to repair!! */
3093 set_bit(STRIPE_INSYNC, &sh->state);
3095 sh->check_state = check_state_compute_run;
3096 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3097 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3098 set_bit(R5_Wantcompute,
3099 &sh->dev[sh->pd_idx].flags);
3100 sh->ops.target = sh->pd_idx;
3101 sh->ops.target2 = -1;
3106 case check_state_compute_run:
3109 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3110 __func__, sh->check_state,
3111 (unsigned long long) sh->sector);
3117 static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
3118 struct stripe_head_state *s,
3121 int pd_idx = sh->pd_idx;
3122 int qd_idx = sh->qd_idx;
3125 set_bit(STRIPE_HANDLE, &sh->state);
3127 BUG_ON(s->failed > 2);
3129 /* Want to check and possibly repair P and Q.
3130 * However there could be one 'failed' device, in which
3131 * case we can only check one of them, possibly using the
3132 * other to generate missing data
3135 switch (sh->check_state) {
3136 case check_state_idle:
3137 /* start a new check operation if there are < 2 failures */
3138 if (s->failed == s->q_failed) {
3139 /* The only possible failed device holds Q, so it
3140 * makes sense to check P (If anything else were failed,
3141 * we would have used P to recreate it).
3143 sh->check_state = check_state_run;
3145 if (!s->q_failed && s->failed < 2) {
3146 /* Q is not failed, and we didn't use it to generate
3147 * anything, so it makes sense to check it
3149 if (sh->check_state == check_state_run)
3150 sh->check_state = check_state_run_pq;
3152 sh->check_state = check_state_run_q;
3155 /* discard potentially stale zero_sum_result */
3156 sh->ops.zero_sum_result = 0;
3158 if (sh->check_state == check_state_run) {
3159 /* async_xor_zero_sum destroys the contents of P */
3160 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3163 if (sh->check_state >= check_state_run &&
3164 sh->check_state <= check_state_run_pq) {
3165 /* async_syndrome_zero_sum preserves P and Q, so
3166 * no need to mark them !uptodate here
3168 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3172 /* we have 2-disk failure */
3173 BUG_ON(s->failed != 2);
3175 case check_state_compute_result:
3176 sh->check_state = check_state_idle;
3178 /* check that a write has not made the stripe insync */
3179 if (test_bit(STRIPE_INSYNC, &sh->state))
3182 /* now write out any block on a failed drive,
3183 * or P or Q if they were recomputed
3185 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
3186 if (s->failed == 2) {
3187 dev = &sh->dev[s->failed_num[1]];
3189 set_bit(R5_LOCKED, &dev->flags);
3190 set_bit(R5_Wantwrite, &dev->flags);
3192 if (s->failed >= 1) {
3193 dev = &sh->dev[s->failed_num[0]];
3195 set_bit(R5_LOCKED, &dev->flags);
3196 set_bit(R5_Wantwrite, &dev->flags);
3198 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3199 dev = &sh->dev[pd_idx];
3201 set_bit(R5_LOCKED, &dev->flags);
3202 set_bit(R5_Wantwrite, &dev->flags);
3204 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3205 dev = &sh->dev[qd_idx];
3207 set_bit(R5_LOCKED, &dev->flags);
3208 set_bit(R5_Wantwrite, &dev->flags);
3210 clear_bit(STRIPE_DEGRADED, &sh->state);
3212 set_bit(STRIPE_INSYNC, &sh->state);
3214 case check_state_run:
3215 case check_state_run_q:
3216 case check_state_run_pq:
3217 break; /* we will be called again upon completion */
3218 case check_state_check_result:
3219 sh->check_state = check_state_idle;
3221 /* handle a successful check operation, if parity is correct
3222 * we are done. Otherwise update the mismatch count and repair
3223 * parity if !MD_RECOVERY_CHECK
3225 if (sh->ops.zero_sum_result == 0) {
3226 /* both parities are correct */
3228 set_bit(STRIPE_INSYNC, &sh->state);
3230 /* in contrast to the raid5 case we can validate
3231 * parity, but still have a failure to write
3234 sh->check_state = check_state_compute_result;
3235 /* Returning at this point means that we may go
3236 * off and bring p and/or q uptodate again so
3237 * we make sure to check zero_sum_result again
3238 * to verify if p or q need writeback
3242 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
3243 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3244 /* don't try to repair!! */
3245 set_bit(STRIPE_INSYNC, &sh->state);
3247 int *target = &sh->ops.target;
3249 sh->ops.target = -1;
3250 sh->ops.target2 = -1;
3251 sh->check_state = check_state_compute_run;
3252 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3253 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3254 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3255 set_bit(R5_Wantcompute,
3256 &sh->dev[pd_idx].flags);
3258 target = &sh->ops.target2;
3261 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3262 set_bit(R5_Wantcompute,
3263 &sh->dev[qd_idx].flags);
3270 case check_state_compute_run:
3273 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3274 __func__, sh->check_state,
3275 (unsigned long long) sh->sector);
3280 static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
3284 /* We have read all the blocks in this stripe and now we need to
3285 * copy some of them into a target stripe for expand.
3287 struct dma_async_tx_descriptor *tx = NULL;
3288 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3289 for (i = 0; i < sh->disks; i++)
3290 if (i != sh->pd_idx && i != sh->qd_idx) {
3292 struct stripe_head *sh2;
3293 struct async_submit_ctl submit;
3295 sector_t bn = compute_blocknr(sh, i, 1);
3296 sector_t s = raid5_compute_sector(conf, bn, 0,
3298 sh2 = get_active_stripe(conf, s, 0, 1, 1);
3300 /* so far only the early blocks of this stripe
3301 * have been requested. When later blocks
3302 * get requested, we will try again
3305 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3306 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3307 /* must have already done this block */
3308 release_stripe(sh2);
3312 /* place all the copies on one channel */
3313 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
3314 tx = async_memcpy(sh2->dev[dd_idx].page,
3315 sh->dev[i].page, 0, 0, STRIPE_SIZE,
3318 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3319 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3320 for (j = 0; j < conf->raid_disks; j++)
3321 if (j != sh2->pd_idx &&
3323 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3325 if (j == conf->raid_disks) {
3326 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3327 set_bit(STRIPE_HANDLE, &sh2->state);
3329 release_stripe(sh2);
3332 /* done submitting copies, wait for them to complete */
3333 async_tx_quiesce(&tx);
3337 * handle_stripe - do things to a stripe.
3339 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3340 * state of various bits to see what needs to be done.
3342 * return some read requests which now have data
3343 * return some write requests which are safely on storage
3344 * schedule a read on some buffers
3345 * schedule a write of some buffers
3346 * return confirmation of parity correctness
3350 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
3352 struct r5conf *conf = sh->raid_conf;
3353 int disks = sh->disks;
3356 int do_recovery = 0;
3358 memset(s, 0, sizeof(*s));
3360 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3361 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3362 s->failed_num[0] = -1;
3363 s->failed_num[1] = -1;
3365 /* Now to look around and see what can be done */
3367 for (i=disks; i--; ) {
3368 struct md_rdev *rdev;
3375 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3377 dev->toread, dev->towrite, dev->written);
3378 /* maybe we can reply to a read
3380 * new wantfill requests are only permitted while
3381 * ops_complete_biofill is guaranteed to be inactive
3383 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3384 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3385 set_bit(R5_Wantfill, &dev->flags);
3387 /* now count some things */
3388 if (test_bit(R5_LOCKED, &dev->flags))
3390 if (test_bit(R5_UPTODATE, &dev->flags))
3392 if (test_bit(R5_Wantcompute, &dev->flags)) {
3394 BUG_ON(s->compute > 2);
3397 if (test_bit(R5_Wantfill, &dev->flags))
3399 else if (dev->toread)
3403 if (!test_bit(R5_OVERWRITE, &dev->flags))
3408 /* Prefer to use the replacement for reads, but only
3409 * if it is recovered enough and has no bad blocks.
3411 rdev = rcu_dereference(conf->disks[i].replacement);
3412 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3413 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3414 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3415 &first_bad, &bad_sectors))
3416 set_bit(R5_ReadRepl, &dev->flags);
3419 set_bit(R5_NeedReplace, &dev->flags);
3420 rdev = rcu_dereference(conf->disks[i].rdev);
3421 clear_bit(R5_ReadRepl, &dev->flags);
3423 if (rdev && test_bit(Faulty, &rdev->flags))
3426 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3427 &first_bad, &bad_sectors);
3428 if (s->blocked_rdev == NULL
3429 && (test_bit(Blocked, &rdev->flags)
3432 set_bit(BlockedBadBlocks,
3434 s->blocked_rdev = rdev;
3435 atomic_inc(&rdev->nr_pending);
3438 clear_bit(R5_Insync, &dev->flags);
3442 /* also not in-sync */
3443 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3444 test_bit(R5_UPTODATE, &dev->flags)) {
3445 /* treat as in-sync, but with a read error
3446 * which we can now try to correct
3448 set_bit(R5_Insync, &dev->flags);
3449 set_bit(R5_ReadError, &dev->flags);
3451 } else if (test_bit(In_sync, &rdev->flags))
3452 set_bit(R5_Insync, &dev->flags);
3453 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3454 /* in sync if before recovery_offset */
3455 set_bit(R5_Insync, &dev->flags);
3456 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3457 test_bit(R5_Expanded, &dev->flags))
3458 /* If we've reshaped into here, we assume it is Insync.
3459 * We will shortly update recovery_offset to make
3462 set_bit(R5_Insync, &dev->flags);
3464 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
3465 /* This flag does not apply to '.replacement'
3466 * only to .rdev, so make sure to check that*/
3467 struct md_rdev *rdev2 = rcu_dereference(
3468 conf->disks[i].rdev);
3470 clear_bit(R5_Insync, &dev->flags);
3471 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3472 s->handle_bad_blocks = 1;
3473 atomic_inc(&rdev2->nr_pending);
3475 clear_bit(R5_WriteError, &dev->flags);
3477 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
3478 /* This flag does not apply to '.replacement'
3479 * only to .rdev, so make sure to check that*/
3480 struct md_rdev *rdev2 = rcu_dereference(
3481 conf->disks[i].rdev);
3482 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3483 s->handle_bad_blocks = 1;
3484 atomic_inc(&rdev2->nr_pending);
3486 clear_bit(R5_MadeGood, &dev->flags);
3488 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3489 struct md_rdev *rdev2 = rcu_dereference(
3490 conf->disks[i].replacement);
3491 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3492 s->handle_bad_blocks = 1;
3493 atomic_inc(&rdev2->nr_pending);
3495 clear_bit(R5_MadeGoodRepl, &dev->flags);
3497 if (!test_bit(R5_Insync, &dev->flags)) {
3498 /* The ReadError flag will just be confusing now */
3499 clear_bit(R5_ReadError, &dev->flags);
3500 clear_bit(R5_ReWrite, &dev->flags);
3502 if (test_bit(R5_ReadError, &dev->flags))
3503 clear_bit(R5_Insync, &dev->flags);
3504 if (!test_bit(R5_Insync, &dev->flags)) {
3506 s->failed_num[s->failed] = i;
3508 if (rdev && !test_bit(Faulty, &rdev->flags))
3512 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3513 /* If there is a failed device being replaced,
3514 * we must be recovering.
3515 * else if we are after recovery_cp, we must be syncing
3516 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3517 * else we can only be replacing
3518 * sync and recovery both need to read all devices, and so
3519 * use the same flag.
3522 sh->sector >= conf->mddev->recovery_cp ||
3523 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
3531 static void handle_stripe(struct stripe_head *sh)
3533 struct stripe_head_state s;
3534 struct r5conf *conf = sh->raid_conf;
3537 int disks = sh->disks;
3538 struct r5dev *pdev, *qdev;
3540 clear_bit(STRIPE_HANDLE, &sh->state);
3541 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
3542 /* already being handled, ensure it gets handled
3543 * again when current action finishes */
3544 set_bit(STRIPE_HANDLE, &sh->state);
3548 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3549 spin_lock(&sh->stripe_lock);
3550 /* Cannot process 'sync' concurrently with 'discard' */
3551 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3552 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3553 set_bit(STRIPE_SYNCING, &sh->state);
3554 clear_bit(STRIPE_INSYNC, &sh->state);
3555 clear_bit(STRIPE_REPLACED, &sh->state);
3557 spin_unlock(&sh->stripe_lock);
3559 clear_bit(STRIPE_DELAYED, &sh->state);
3561 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3562 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3563 (unsigned long long)sh->sector, sh->state,
3564 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3565 sh->check_state, sh->reconstruct_state);
3567 analyse_stripe(sh, &s);
3569 if (s.handle_bad_blocks) {
3570 set_bit(STRIPE_HANDLE, &sh->state);
3574 if (unlikely(s.blocked_rdev)) {
3575 if (s.syncing || s.expanding || s.expanded ||
3576 s.replacing || s.to_write || s.written) {
3577 set_bit(STRIPE_HANDLE, &sh->state);
3580 /* There is nothing for the blocked_rdev to block */
3581 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3582 s.blocked_rdev = NULL;
3585 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3586 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3587 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3590 pr_debug("locked=%d uptodate=%d to_read=%d"
3591 " to_write=%d failed=%d failed_num=%d,%d\n",
3592 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3593 s.failed_num[0], s.failed_num[1]);
3594 /* check if the array has lost more than max_degraded devices and,
3595 * if so, some requests might need to be failed.
3597 if (s.failed > conf->max_degraded) {
3598 sh->check_state = 0;
3599 sh->reconstruct_state = 0;
3600 if (s.to_read+s.to_write+s.written)
3601 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3602 if (s.syncing + s.replacing)
3603 handle_failed_sync(conf, sh, &s);
3606 /* Now we check to see if any write operations have recently
3610 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3612 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3613 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3614 sh->reconstruct_state = reconstruct_state_idle;
3616 /* All the 'written' buffers and the parity block are ready to
3617 * be written back to disk
3619 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3620 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
3621 BUG_ON(sh->qd_idx >= 0 &&
3622 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3623 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
3624 for (i = disks; i--; ) {
3625 struct r5dev *dev = &sh->dev[i];
3626 if (test_bit(R5_LOCKED, &dev->flags) &&
3627 (i == sh->pd_idx || i == sh->qd_idx ||
3629 pr_debug("Writing block %d\n", i);
3630 set_bit(R5_Wantwrite, &dev->flags);
3633 if (!test_bit(R5_Insync, &dev->flags) ||
3634 ((i == sh->pd_idx || i == sh->qd_idx) &&
3636 set_bit(STRIPE_INSYNC, &sh->state);
3639 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3640 s.dec_preread_active = 1;
3644 * might be able to return some write requests if the parity blocks
3645 * are safe, or on a failed drive
3647 pdev = &sh->dev[sh->pd_idx];
3648 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3649 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3650 qdev = &sh->dev[sh->qd_idx];
3651 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3652 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3656 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3657 && !test_bit(R5_LOCKED, &pdev->flags)
3658 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3659 test_bit(R5_Discard, &pdev->flags))))) &&
3660 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3661 && !test_bit(R5_LOCKED, &qdev->flags)
3662 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3663 test_bit(R5_Discard, &qdev->flags))))))
3664 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3666 /* Now we might consider reading some blocks, either to check/generate
3667 * parity, or to satisfy requests
3668 * or to load a block that is being partially written.
3670 if (s.to_read || s.non_overwrite
3671 || (conf->level == 6 && s.to_write && s.failed)
3672 || (s.syncing && (s.uptodate + s.compute < disks))
3675 handle_stripe_fill(sh, &s, disks);
3677 /* Now to consider new write requests and what else, if anything
3678 * should be read. We do not handle new writes when:
3679 * 1/ A 'write' operation (copy+xor) is already in flight.
3680 * 2/ A 'check' operation is in flight, as it may clobber the parity
3683 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3684 handle_stripe_dirtying(conf, sh, &s, disks);
3686 /* maybe we need to check and possibly fix the parity for this stripe
3687 * Any reads will already have been scheduled, so we just see if enough
3688 * data is available. The parity check is held off while parity
3689 * dependent operations are in flight.
3691 if (sh->check_state ||
3692 (s.syncing && s.locked == 0 &&
3693 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3694 !test_bit(STRIPE_INSYNC, &sh->state))) {
3695 if (conf->level == 6)
3696 handle_parity_checks6(conf, sh, &s, disks);
3698 handle_parity_checks5(conf, sh, &s, disks);
3701 if ((s.replacing || s.syncing) && s.locked == 0
3702 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3703 && !test_bit(STRIPE_REPLACED, &sh->state)) {
3704 /* Write out to replacement devices where possible */
3705 for (i = 0; i < conf->raid_disks; i++)
3706 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3707 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
3708 set_bit(R5_WantReplace, &sh->dev[i].flags);
3709 set_bit(R5_LOCKED, &sh->dev[i].flags);
3713 set_bit(STRIPE_INSYNC, &sh->state);
3714 set_bit(STRIPE_REPLACED, &sh->state);
3716 if ((s.syncing || s.replacing) && s.locked == 0 &&
3717 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3718 test_bit(STRIPE_INSYNC, &sh->state)) {
3719 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3720 clear_bit(STRIPE_SYNCING, &sh->state);
3721 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3722 wake_up(&conf->wait_for_overlap);
3725 /* If the failed drives are just a ReadError, then we might need
3726 * to progress the repair/check process
3728 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3729 for (i = 0; i < s.failed; i++) {
3730 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3731 if (test_bit(R5_ReadError, &dev->flags)
3732 && !test_bit(R5_LOCKED, &dev->flags)
3733 && test_bit(R5_UPTODATE, &dev->flags)
3735 if (!test_bit(R5_ReWrite, &dev->flags)) {
3736 set_bit(R5_Wantwrite, &dev->flags);
3737 set_bit(R5_ReWrite, &dev->flags);
3738 set_bit(R5_LOCKED, &dev->flags);
3741 /* let's read it back */
3742 set_bit(R5_Wantread, &dev->flags);
3743 set_bit(R5_LOCKED, &dev->flags);
3750 /* Finish reconstruct operations initiated by the expansion process */
3751 if (sh->reconstruct_state == reconstruct_state_result) {
3752 struct stripe_head *sh_src
3753 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3754 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3755 /* sh cannot be written until sh_src has been read.
3756 * so arrange for sh to be delayed a little
3758 set_bit(STRIPE_DELAYED, &sh->state);
3759 set_bit(STRIPE_HANDLE, &sh->state);
3760 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3762 atomic_inc(&conf->preread_active_stripes);
3763 release_stripe(sh_src);
3767 release_stripe(sh_src);
3769 sh->reconstruct_state = reconstruct_state_idle;
3770 clear_bit(STRIPE_EXPANDING, &sh->state);
3771 for (i = conf->raid_disks; i--; ) {
3772 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3773 set_bit(R5_LOCKED, &sh->dev[i].flags);
3778 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3779 !sh->reconstruct_state) {
3780 /* Need to write out all blocks after computing parity */
3781 sh->disks = conf->raid_disks;
3782 stripe_set_idx(sh->sector, conf, 0, sh);
3783 schedule_reconstruction(sh, &s, 1, 1);
3784 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3785 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3786 atomic_dec(&conf->reshape_stripes);
3787 wake_up(&conf->wait_for_overlap);
3788 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3791 if (s.expanding && s.locked == 0 &&
3792 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3793 handle_stripe_expansion(conf, sh);
3796 /* wait for this device to become unblocked */
3797 if (unlikely(s.blocked_rdev)) {
3798 if (conf->mddev->external)
3799 md_wait_for_blocked_rdev(s.blocked_rdev,
3802 /* Internal metadata will immediately
3803 * be written by raid5d, so we don't
3804 * need to wait here.
3806 rdev_dec_pending(s.blocked_rdev,
3810 if (s.handle_bad_blocks)
3811 for (i = disks; i--; ) {
3812 struct md_rdev *rdev;
3813 struct r5dev *dev = &sh->dev[i];
3814 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3815 /* We own a safe reference to the rdev */
3816 rdev = conf->disks[i].rdev;
3817 if (!rdev_set_badblocks(rdev, sh->sector,
3819 md_error(conf->mddev, rdev);
3820 rdev_dec_pending(rdev, conf->mddev);
3822 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3823 rdev = conf->disks[i].rdev;
3824 rdev_clear_badblocks(rdev, sh->sector,
3826 rdev_dec_pending(rdev, conf->mddev);
3828 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3829 rdev = conf->disks[i].replacement;
3831 /* rdev have been moved down */
3832 rdev = conf->disks[i].rdev;
3833 rdev_clear_badblocks(rdev, sh->sector,
3835 rdev_dec_pending(rdev, conf->mddev);
3840 raid_run_ops(sh, s.ops_request);
3844 if (s.dec_preread_active) {
3845 /* We delay this until after ops_run_io so that if make_request
3846 * is waiting on a flush, it won't continue until the writes
3847 * have actually been submitted.
3849 atomic_dec(&conf->preread_active_stripes);
3850 if (atomic_read(&conf->preread_active_stripes) <
3852 md_wakeup_thread(conf->mddev->thread);
3855 return_io(s.return_bi);
3857 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
3860 static void raid5_activate_delayed(struct r5conf *conf)
3862 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3863 while (!list_empty(&conf->delayed_list)) {
3864 struct list_head *l = conf->delayed_list.next;
3865 struct stripe_head *sh;
3866 sh = list_entry(l, struct stripe_head, lru);
3868 clear_bit(STRIPE_DELAYED, &sh->state);
3869 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3870 atomic_inc(&conf->preread_active_stripes);
3871 list_add_tail(&sh->lru, &conf->hold_list);
3872 raid5_wakeup_stripe_thread(sh);
3877 static void activate_bit_delay(struct r5conf *conf)
3879 /* device_lock is held */
3880 struct list_head head;
3881 list_add(&head, &conf->bitmap_list);
3882 list_del_init(&conf->bitmap_list);
3883 while (!list_empty(&head)) {
3884 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3885 list_del_init(&sh->lru);
3886 atomic_inc(&sh->count);
3887 __release_stripe(conf, sh);
3891 int md_raid5_congested(struct mddev *mddev, int bits)
3893 struct r5conf *conf = mddev->private;
3895 /* No difference between reads and writes. Just check
3896 * how busy the stripe_cache is
3899 if (conf->inactive_blocked)
3903 if (list_empty_careful(&conf->inactive_list))
3908 EXPORT_SYMBOL_GPL(md_raid5_congested);
3910 static int raid5_congested(void *data, int bits)
3912 struct mddev *mddev = data;
3914 return mddev_congested(mddev, bits) ||
3915 md_raid5_congested(mddev, bits);
3918 /* We want read requests to align with chunks where possible,
3919 * but write requests don't need to.
3921 static int raid5_mergeable_bvec(struct request_queue *q,
3922 struct bvec_merge_data *bvm,
3923 struct bio_vec *biovec)
3925 struct mddev *mddev = q->queuedata;
3926 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
3928 unsigned int chunk_sectors = mddev->chunk_sectors;
3929 unsigned int bio_sectors = bvm->bi_size >> 9;
3931 if ((bvm->bi_rw & 1) == WRITE)
3932 return biovec->bv_len; /* always allow writes to be mergeable */
3934 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3935 chunk_sectors = mddev->new_chunk_sectors;
3936 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3937 if (max < 0) max = 0;
3938 if (max <= biovec->bv_len && bio_sectors == 0)
3939 return biovec->bv_len;
3945 static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
3947 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3948 unsigned int chunk_sectors = mddev->chunk_sectors;
3949 unsigned int bio_sectors = bio_sectors(bio);
3951 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3952 chunk_sectors = mddev->new_chunk_sectors;
3953 return chunk_sectors >=
3954 ((sector & (chunk_sectors - 1)) + bio_sectors);
3958 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3959 * later sampled by raid5d.
3961 static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
3963 unsigned long flags;
3965 spin_lock_irqsave(&conf->device_lock, flags);
3967 bi->bi_next = conf->retry_read_aligned_list;
3968 conf->retry_read_aligned_list = bi;
3970 spin_unlock_irqrestore(&conf->device_lock, flags);
3971 md_wakeup_thread(conf->mddev->thread);
3975 static struct bio *remove_bio_from_retry(struct r5conf *conf)
3979 bi = conf->retry_read_aligned;
3981 conf->retry_read_aligned = NULL;
3984 bi = conf->retry_read_aligned_list;
3986 conf->retry_read_aligned_list = bi->bi_next;
3989 * this sets the active strip count to 1 and the processed
3990 * strip count to zero (upper 8 bits)
3992 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
4000 * The "raid5_align_endio" should check if the read succeeded and if it
4001 * did, call bio_endio on the original bio (having bio_put the new bio
4003 * If the read failed..
4005 static void raid5_align_endio(struct bio *bi, int error)
4007 struct bio* raid_bi = bi->bi_private;
4008 struct mddev *mddev;
4009 struct r5conf *conf;
4010 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
4011 struct md_rdev *rdev;
4015 rdev = (void*)raid_bi->bi_next;
4016 raid_bi->bi_next = NULL;
4017 mddev = rdev->mddev;
4018 conf = mddev->private;
4020 rdev_dec_pending(rdev, conf->mddev);
4022 if (!error && uptodate) {
4023 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4025 bio_endio(raid_bi, 0);
4026 if (atomic_dec_and_test(&conf->active_aligned_reads))
4027 wake_up(&conf->wait_for_stripe);
4032 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
4034 add_bio_to_retry(raid_bi, conf);
4037 static int bio_fits_rdev(struct bio *bi)
4039 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
4041 if (bio_sectors(bi) > queue_max_sectors(q))
4043 blk_recount_segments(q, bi);
4044 if (bi->bi_phys_segments > queue_max_segments(q))
4047 if (q->merge_bvec_fn)
4048 /* it's too hard to apply the merge_bvec_fn at this stage,
4057 static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
4059 struct r5conf *conf = mddev->private;
4061 struct bio* align_bi;
4062 struct md_rdev *rdev;
4063 sector_t end_sector;
4065 if (!in_chunk_boundary(mddev, raid_bio)) {
4066 pr_debug("chunk_aligned_read : non aligned\n");
4070 * use bio_clone_mddev to make a copy of the bio
4072 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
4076 * set bi_end_io to a new function, and set bi_private to the
4079 align_bi->bi_end_io = raid5_align_endio;
4080 align_bi->bi_private = raid_bio;
4084 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4088 end_sector = bio_end_sector(align_bi);
4090 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4091 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4092 rdev->recovery_offset < end_sector) {
4093 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4095 (test_bit(Faulty, &rdev->flags) ||
4096 !(test_bit(In_sync, &rdev->flags) ||
4097 rdev->recovery_offset >= end_sector)))
4104 atomic_inc(&rdev->nr_pending);
4106 raid_bio->bi_next = (void*)rdev;
4107 align_bi->bi_bdev = rdev->bdev;
4108 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
4110 if (!bio_fits_rdev(align_bi) ||
4111 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
4112 &first_bad, &bad_sectors)) {
4113 /* too big in some way, or has a known bad block */
4115 rdev_dec_pending(rdev, mddev);
4119 /* No reshape active, so we can trust rdev->data_offset */
4120 align_bi->bi_sector += rdev->data_offset;
4122 spin_lock_irq(&conf->device_lock);
4123 wait_event_lock_irq(conf->wait_for_stripe,
4126 atomic_inc(&conf->active_aligned_reads);
4127 spin_unlock_irq(&conf->device_lock);
4130 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4131 align_bi, disk_devt(mddev->gendisk),
4132 raid_bio->bi_sector);
4133 generic_make_request(align_bi);
4142 /* __get_priority_stripe - get the next stripe to process
4144 * Full stripe writes are allowed to pass preread active stripes up until
4145 * the bypass_threshold is exceeded. In general the bypass_count
4146 * increments when the handle_list is handled before the hold_list; however, it
4147 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4148 * stripe with in flight i/o. The bypass_count will be reset when the
4149 * head of the hold_list has changed, i.e. the head was promoted to the
4152 static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
4154 struct stripe_head *sh = NULL, *tmp;
4155 struct list_head *handle_list = NULL;
4157 if (conf->worker_cnt_per_group == 0) {
4158 handle_list = &conf->handle_list;
4159 } else if (group != ANY_GROUP) {
4160 handle_list = &conf->worker_groups[group].handle_list;
4163 for (i = 0; i < conf->group_cnt; i++) {
4164 handle_list = &conf->worker_groups[i].handle_list;
4165 if (!list_empty(handle_list))
4170 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4172 list_empty(handle_list) ? "empty" : "busy",
4173 list_empty(&conf->hold_list) ? "empty" : "busy",
4174 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4176 if (!list_empty(handle_list)) {
4177 sh = list_entry(handle_list->next, typeof(*sh), lru);
4179 if (list_empty(&conf->hold_list))
4180 conf->bypass_count = 0;
4181 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4182 if (conf->hold_list.next == conf->last_hold)
4183 conf->bypass_count++;
4185 conf->last_hold = conf->hold_list.next;
4186 conf->bypass_count -= conf->bypass_threshold;
4187 if (conf->bypass_count < 0)
4188 conf->bypass_count = 0;
4191 } else if (!list_empty(&conf->hold_list) &&
4192 ((conf->bypass_threshold &&
4193 conf->bypass_count > conf->bypass_threshold) ||
4194 atomic_read(&conf->pending_full_writes) == 0)) {
4196 list_for_each_entry(tmp, &conf->hold_list, lru) {
4197 if (conf->worker_cnt_per_group == 0 ||
4198 group == ANY_GROUP ||
4199 !cpu_online(tmp->cpu) ||
4200 cpu_to_group(tmp->cpu) == group) {
4207 conf->bypass_count -= conf->bypass_threshold;
4208 if (conf->bypass_count < 0)
4209 conf->bypass_count = 0;
4216 list_del_init(&sh->lru);
4217 atomic_inc(&sh->count);
4218 BUG_ON(atomic_read(&sh->count) != 1);
4222 struct raid5_plug_cb {
4223 struct blk_plug_cb cb;
4224 struct list_head list;
4227 static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4229 struct raid5_plug_cb *cb = container_of(
4230 blk_cb, struct raid5_plug_cb, cb);
4231 struct stripe_head *sh;
4232 struct mddev *mddev = cb->cb.data;
4233 struct r5conf *conf = mddev->private;
4236 if (cb->list.next && !list_empty(&cb->list)) {
4237 spin_lock_irq(&conf->device_lock);
4238 while (!list_empty(&cb->list)) {
4239 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4240 list_del_init(&sh->lru);
4242 * avoid race release_stripe_plug() sees
4243 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4244 * is still in our list
4246 smp_mb__before_clear_bit();
4247 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4249 * STRIPE_ON_RELEASE_LIST could be set here. In that
4250 * case, the count is always > 1 here
4252 __release_stripe(conf, sh);
4255 spin_unlock_irq(&conf->device_lock);
4258 trace_block_unplug(mddev->queue, cnt, !from_schedule);
4262 static void release_stripe_plug(struct mddev *mddev,
4263 struct stripe_head *sh)
4265 struct blk_plug_cb *blk_cb = blk_check_plugged(
4266 raid5_unplug, mddev,
4267 sizeof(struct raid5_plug_cb));
4268 struct raid5_plug_cb *cb;
4275 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4277 if (cb->list.next == NULL)
4278 INIT_LIST_HEAD(&cb->list);
4280 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4281 list_add_tail(&sh->lru, &cb->list);
4286 static void make_discard_request(struct mddev *mddev, struct bio *bi)
4288 struct r5conf *conf = mddev->private;
4289 sector_t logical_sector, last_sector;
4290 struct stripe_head *sh;
4294 if (mddev->reshape_position != MaxSector)
4295 /* Skip discard while reshape is happening */
4298 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4299 last_sector = bi->bi_sector + (bi->bi_size>>9);
4302 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4304 stripe_sectors = conf->chunk_sectors *
4305 (conf->raid_disks - conf->max_degraded);
4306 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4308 sector_div(last_sector, stripe_sectors);
4310 logical_sector *= conf->chunk_sectors;
4311 last_sector *= conf->chunk_sectors;
4313 for (; logical_sector < last_sector;
4314 logical_sector += STRIPE_SECTORS) {
4318 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4319 prepare_to_wait(&conf->wait_for_overlap, &w,
4320 TASK_UNINTERRUPTIBLE);
4321 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4322 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4327 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4328 spin_lock_irq(&sh->stripe_lock);
4329 for (d = 0; d < conf->raid_disks; d++) {
4330 if (d == sh->pd_idx || d == sh->qd_idx)
4332 if (sh->dev[d].towrite || sh->dev[d].toread) {
4333 set_bit(R5_Overlap, &sh->dev[d].flags);
4334 spin_unlock_irq(&sh->stripe_lock);
4340 set_bit(STRIPE_DISCARD, &sh->state);
4341 finish_wait(&conf->wait_for_overlap, &w);
4342 for (d = 0; d < conf->raid_disks; d++) {
4343 if (d == sh->pd_idx || d == sh->qd_idx)
4345 sh->dev[d].towrite = bi;
4346 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4347 raid5_inc_bi_active_stripes(bi);
4349 spin_unlock_irq(&sh->stripe_lock);
4350 if (conf->mddev->bitmap) {
4352 d < conf->raid_disks - conf->max_degraded;
4354 bitmap_startwrite(mddev->bitmap,
4358 sh->bm_seq = conf->seq_flush + 1;
4359 set_bit(STRIPE_BIT_DELAY, &sh->state);
4362 set_bit(STRIPE_HANDLE, &sh->state);
4363 clear_bit(STRIPE_DELAYED, &sh->state);
4364 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4365 atomic_inc(&conf->preread_active_stripes);
4366 release_stripe_plug(mddev, sh);
4369 remaining = raid5_dec_bi_active_stripes(bi);
4370 if (remaining == 0) {
4371 md_write_end(mddev);
4376 static void make_request(struct mddev *mddev, struct bio * bi)
4378 struct r5conf *conf = mddev->private;
4380 sector_t new_sector;
4381 sector_t logical_sector, last_sector;
4382 struct stripe_head *sh;
4383 const int rw = bio_data_dir(bi);
4386 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4387 md_flush_request(mddev, bi);
4391 md_write_start(mddev, bi);
4394 mddev->reshape_position == MaxSector &&
4395 chunk_aligned_read(mddev,bi))
4398 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4399 make_discard_request(mddev, bi);
4403 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4404 last_sector = bio_end_sector(bi);
4406 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4408 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4414 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
4415 if (unlikely(conf->reshape_progress != MaxSector)) {
4416 /* spinlock is needed as reshape_progress may be
4417 * 64bit on a 32bit platform, and so it might be
4418 * possible to see a half-updated value
4419 * Of course reshape_progress could change after
4420 * the lock is dropped, so once we get a reference
4421 * to the stripe that we think it is, we will have
4424 spin_lock_irq(&conf->device_lock);
4425 if (mddev->reshape_backwards
4426 ? logical_sector < conf->reshape_progress
4427 : logical_sector >= conf->reshape_progress) {
4430 if (mddev->reshape_backwards
4431 ? logical_sector < conf->reshape_safe
4432 : logical_sector >= conf->reshape_safe) {
4433 spin_unlock_irq(&conf->device_lock);
4438 spin_unlock_irq(&conf->device_lock);
4441 new_sector = raid5_compute_sector(conf, logical_sector,
4444 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4445 (unsigned long long)new_sector,
4446 (unsigned long long)logical_sector);
4448 sh = get_active_stripe(conf, new_sector, previous,
4449 (bi->bi_rw&RWA_MASK), 0);
4451 if (unlikely(previous)) {
4452 /* expansion might have moved on while waiting for a
4453 * stripe, so we must do the range check again.
4454 * Expansion could still move past after this
4455 * test, but as we are holding a reference to
4456 * 'sh', we know that if that happens,
4457 * STRIPE_EXPANDING will get set and the expansion
4458 * won't proceed until we finish with the stripe.
4461 spin_lock_irq(&conf->device_lock);
4462 if (mddev->reshape_backwards
4463 ? logical_sector >= conf->reshape_progress
4464 : logical_sector < conf->reshape_progress)
4465 /* mismatch, need to try again */
4467 spin_unlock_irq(&conf->device_lock);
4476 logical_sector >= mddev->suspend_lo &&
4477 logical_sector < mddev->suspend_hi) {
4479 /* As the suspend_* range is controlled by
4480 * userspace, we want an interruptible
4483 flush_signals(current);
4484 prepare_to_wait(&conf->wait_for_overlap,
4485 &w, TASK_INTERRUPTIBLE);
4486 if (logical_sector >= mddev->suspend_lo &&
4487 logical_sector < mddev->suspend_hi)
4492 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
4493 !add_stripe_bio(sh, bi, dd_idx, rw)) {
4494 /* Stripe is busy expanding or
4495 * add failed due to overlap. Flush everything
4498 md_wakeup_thread(mddev->thread);
4503 finish_wait(&conf->wait_for_overlap, &w);
4504 set_bit(STRIPE_HANDLE, &sh->state);
4505 clear_bit(STRIPE_DELAYED, &sh->state);
4506 if ((bi->bi_rw & REQ_SYNC) &&
4507 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4508 atomic_inc(&conf->preread_active_stripes);
4509 release_stripe_plug(mddev, sh);
4511 /* cannot get stripe for read-ahead, just give-up */
4512 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4513 finish_wait(&conf->wait_for_overlap, &w);
4518 remaining = raid5_dec_bi_active_stripes(bi);
4519 if (remaining == 0) {
4522 md_write_end(mddev);
4524 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4530 static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
4532 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
4534 /* reshaping is quite different to recovery/resync so it is
4535 * handled quite separately ... here.
4537 * On each call to sync_request, we gather one chunk worth of
4538 * destination stripes and flag them as expanding.
4539 * Then we find all the source stripes and request reads.
4540 * As the reads complete, handle_stripe will copy the data
4541 * into the destination stripe and release that stripe.
4543 struct r5conf *conf = mddev->private;
4544 struct stripe_head *sh;
4545 sector_t first_sector, last_sector;
4546 int raid_disks = conf->previous_raid_disks;
4547 int data_disks = raid_disks - conf->max_degraded;
4548 int new_data_disks = conf->raid_disks - conf->max_degraded;
4551 sector_t writepos, readpos, safepos;
4552 sector_t stripe_addr;
4553 int reshape_sectors;
4554 struct list_head stripes;
4556 if (sector_nr == 0) {
4557 /* If restarting in the middle, skip the initial sectors */
4558 if (mddev->reshape_backwards &&
4559 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4560 sector_nr = raid5_size(mddev, 0, 0)
4561 - conf->reshape_progress;
4562 } else if (!mddev->reshape_backwards &&
4563 conf->reshape_progress > 0)
4564 sector_nr = conf->reshape_progress;
4565 sector_div(sector_nr, new_data_disks);
4567 mddev->curr_resync_completed = sector_nr;
4568 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4574 /* We need to process a full chunk at a time.
4575 * If old and new chunk sizes differ, we need to process the
4578 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4579 reshape_sectors = mddev->new_chunk_sectors;
4581 reshape_sectors = mddev->chunk_sectors;
4583 /* We update the metadata at least every 10 seconds, or when
4584 * the data about to be copied would over-write the source of
4585 * the data at the front of the range. i.e. one new_stripe
4586 * along from reshape_progress new_maps to after where
4587 * reshape_safe old_maps to
4589 writepos = conf->reshape_progress;
4590 sector_div(writepos, new_data_disks);
4591 readpos = conf->reshape_progress;
4592 sector_div(readpos, data_disks);
4593 safepos = conf->reshape_safe;
4594 sector_div(safepos, data_disks);
4595 if (mddev->reshape_backwards) {
4596 writepos -= min_t(sector_t, reshape_sectors, writepos);
4597 readpos += reshape_sectors;
4598 safepos += reshape_sectors;
4600 writepos += reshape_sectors;
4601 readpos -= min_t(sector_t, reshape_sectors, readpos);
4602 safepos -= min_t(sector_t, reshape_sectors, safepos);
4605 /* Having calculated the 'writepos' possibly use it
4606 * to set 'stripe_addr' which is where we will write to.
4608 if (mddev->reshape_backwards) {
4609 BUG_ON(conf->reshape_progress == 0);
4610 stripe_addr = writepos;
4611 BUG_ON((mddev->dev_sectors &
4612 ~((sector_t)reshape_sectors - 1))
4613 - reshape_sectors - stripe_addr
4616 BUG_ON(writepos != sector_nr + reshape_sectors);
4617 stripe_addr = sector_nr;
4620 /* 'writepos' is the most advanced device address we might write.
4621 * 'readpos' is the least advanced device address we might read.
4622 * 'safepos' is the least address recorded in the metadata as having
4624 * If there is a min_offset_diff, these are adjusted either by
4625 * increasing the safepos/readpos if diff is negative, or
4626 * increasing writepos if diff is positive.
4627 * If 'readpos' is then behind 'writepos', there is no way that we can
4628 * ensure safety in the face of a crash - that must be done by userspace
4629 * making a backup of the data. So in that case there is no particular
4630 * rush to update metadata.
4631 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4632 * update the metadata to advance 'safepos' to match 'readpos' so that
4633 * we can be safe in the event of a crash.
4634 * So we insist on updating metadata if safepos is behind writepos and
4635 * readpos is beyond writepos.
4636 * In any case, update the metadata every 10 seconds.
4637 * Maybe that number should be configurable, but I'm not sure it is
4638 * worth it.... maybe it could be a multiple of safemode_delay???
4640 if (conf->min_offset_diff < 0) {
4641 safepos += -conf->min_offset_diff;
4642 readpos += -conf->min_offset_diff;
4644 writepos += conf->min_offset_diff;
4646 if ((mddev->reshape_backwards
4647 ? (safepos > writepos && readpos < writepos)
4648 : (safepos < writepos && readpos > writepos)) ||
4649 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4650 /* Cannot proceed until we've updated the superblock... */
4651 wait_event(conf->wait_for_overlap,
4652 atomic_read(&conf->reshape_stripes)==0);
4653 mddev->reshape_position = conf->reshape_progress;
4654 mddev->curr_resync_completed = sector_nr;
4655 conf->reshape_checkpoint = jiffies;
4656 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4657 md_wakeup_thread(mddev->thread);
4658 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4659 kthread_should_stop());
4660 spin_lock_irq(&conf->device_lock);
4661 conf->reshape_safe = mddev->reshape_position;
4662 spin_unlock_irq(&conf->device_lock);
4663 wake_up(&conf->wait_for_overlap);
4664 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4667 INIT_LIST_HEAD(&stripes);
4668 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
4670 int skipped_disk = 0;
4671 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
4672 set_bit(STRIPE_EXPANDING, &sh->state);
4673 atomic_inc(&conf->reshape_stripes);
4674 /* If any of this stripe is beyond the end of the old
4675 * array, then we need to zero those blocks
4677 for (j=sh->disks; j--;) {
4679 if (j == sh->pd_idx)
4681 if (conf->level == 6 &&
4684 s = compute_blocknr(sh, j, 0);
4685 if (s < raid5_size(mddev, 0, 0)) {
4689 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4690 set_bit(R5_Expanded, &sh->dev[j].flags);
4691 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4693 if (!skipped_disk) {
4694 set_bit(STRIPE_EXPAND_READY, &sh->state);
4695 set_bit(STRIPE_HANDLE, &sh->state);
4697 list_add(&sh->lru, &stripes);
4699 spin_lock_irq(&conf->device_lock);
4700 if (mddev->reshape_backwards)
4701 conf->reshape_progress -= reshape_sectors * new_data_disks;
4703 conf->reshape_progress += reshape_sectors * new_data_disks;
4704 spin_unlock_irq(&conf->device_lock);
4705 /* Ok, those stripe are ready. We can start scheduling
4706 * reads on the source stripes.
4707 * The source stripes are determined by mapping the first and last
4708 * block on the destination stripes.
4711 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
4714 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
4715 * new_data_disks - 1),
4717 if (last_sector >= mddev->dev_sectors)
4718 last_sector = mddev->dev_sectors - 1;
4719 while (first_sector <= last_sector) {
4720 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
4721 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4722 set_bit(STRIPE_HANDLE, &sh->state);
4724 first_sector += STRIPE_SECTORS;
4726 /* Now that the sources are clearly marked, we can release
4727 * the destination stripes
4729 while (!list_empty(&stripes)) {
4730 sh = list_entry(stripes.next, struct stripe_head, lru);
4731 list_del_init(&sh->lru);
4734 /* If this takes us to the resync_max point where we have to pause,
4735 * then we need to write out the superblock.
4737 sector_nr += reshape_sectors;
4738 if ((sector_nr - mddev->curr_resync_completed) * 2
4739 >= mddev->resync_max - mddev->curr_resync_completed) {
4740 /* Cannot proceed until we've updated the superblock... */
4741 wait_event(conf->wait_for_overlap,
4742 atomic_read(&conf->reshape_stripes) == 0);
4743 mddev->reshape_position = conf->reshape_progress;
4744 mddev->curr_resync_completed = sector_nr;
4745 conf->reshape_checkpoint = jiffies;
4746 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4747 md_wakeup_thread(mddev->thread);
4748 wait_event(mddev->sb_wait,
4749 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4750 || kthread_should_stop());
4751 spin_lock_irq(&conf->device_lock);
4752 conf->reshape_safe = mddev->reshape_position;
4753 spin_unlock_irq(&conf->device_lock);
4754 wake_up(&conf->wait_for_overlap);
4755 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4757 return reshape_sectors;
4760 /* FIXME go_faster isn't used */
4761 static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
4763 struct r5conf *conf = mddev->private;
4764 struct stripe_head *sh;
4765 sector_t max_sector = mddev->dev_sectors;
4766 sector_t sync_blocks;
4767 int still_degraded = 0;
4770 if (sector_nr >= max_sector) {
4771 /* just being told to finish up .. nothing much to do */
4773 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4778 if (mddev->curr_resync < max_sector) /* aborted */
4779 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4781 else /* completed sync */
4783 bitmap_close_sync(mddev->bitmap);
4788 /* Allow raid5_quiesce to complete */
4789 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4791 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4792 return reshape_request(mddev, sector_nr, skipped);
4794 /* No need to check resync_max as we never do more than one
4795 * stripe, and as resync_max will always be on a chunk boundary,
4796 * if the check in md_do_sync didn't fire, there is no chance
4797 * of overstepping resync_max here
4800 /* if there is too many failed drives and we are trying
4801 * to resync, then assert that we are finished, because there is
4802 * nothing we can do.
4804 if (mddev->degraded >= conf->max_degraded &&
4805 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4806 sector_t rv = mddev->dev_sectors - sector_nr;
4810 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4812 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4813 sync_blocks >= STRIPE_SECTORS) {
4814 /* we can skip this block, and probably more */
4815 sync_blocks /= STRIPE_SECTORS;
4817 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4820 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4822 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
4824 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
4825 /* make sure we don't swamp the stripe cache if someone else
4826 * is trying to get access
4828 schedule_timeout_uninterruptible(1);
4830 /* Need to check if array will still be degraded after recovery/resync
4831 * We don't need to check the 'failed' flag as when that gets set,
4834 for (i = 0; i < conf->raid_disks; i++)
4835 if (conf->disks[i].rdev == NULL)
4838 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4840 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
4845 return STRIPE_SECTORS;
4848 static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
4850 /* We may not be able to submit a whole bio at once as there
4851 * may not be enough stripe_heads available.
4852 * We cannot pre-allocate enough stripe_heads as we may need
4853 * more than exist in the cache (if we allow ever large chunks).
4854 * So we do one stripe head at a time and record in
4855 * ->bi_hw_segments how many have been done.
4857 * We *know* that this entire raid_bio is in one chunk, so
4858 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4860 struct stripe_head *sh;
4862 sector_t sector, logical_sector, last_sector;
4867 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4868 sector = raid5_compute_sector(conf, logical_sector,
4870 last_sector = bio_end_sector(raid_bio);
4872 for (; logical_sector < last_sector;
4873 logical_sector += STRIPE_SECTORS,
4874 sector += STRIPE_SECTORS,
4877 if (scnt < raid5_bi_processed_stripes(raid_bio))
4878 /* already done this stripe */
4881 sh = get_active_stripe(conf, sector, 0, 1, 0);
4884 /* failed to get a stripe - must wait */
4885 raid5_set_bi_processed_stripes(raid_bio, scnt);
4886 conf->retry_read_aligned = raid_bio;
4890 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4892 raid5_set_bi_processed_stripes(raid_bio, scnt);
4893 conf->retry_read_aligned = raid_bio;
4897 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
4902 remaining = raid5_dec_bi_active_stripes(raid_bio);
4903 if (remaining == 0) {
4904 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4906 bio_endio(raid_bio, 0);
4908 if (atomic_dec_and_test(&conf->active_aligned_reads))
4909 wake_up(&conf->wait_for_stripe);
4913 #define MAX_STRIPE_BATCH 8
4914 static int handle_active_stripes(struct r5conf *conf, int group)
4916 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4917 int i, batch_size = 0;
4919 while (batch_size < MAX_STRIPE_BATCH &&
4920 (sh = __get_priority_stripe(conf, group)) != NULL)
4921 batch[batch_size++] = sh;
4923 if (batch_size == 0)
4925 spin_unlock_irq(&conf->device_lock);
4927 for (i = 0; i < batch_size; i++)
4928 handle_stripe(batch[i]);
4932 spin_lock_irq(&conf->device_lock);
4933 for (i = 0; i < batch_size; i++)
4934 __release_stripe(conf, batch[i]);
4938 static void raid5_do_work(struct work_struct *work)
4940 struct r5worker *worker = container_of(work, struct r5worker, work);
4941 struct r5worker_group *group = worker->group;
4942 struct r5conf *conf = group->conf;
4943 int group_id = group - conf->worker_groups;
4945 struct blk_plug plug;
4947 pr_debug("+++ raid5worker active\n");
4949 blk_start_plug(&plug);
4951 spin_lock_irq(&conf->device_lock);
4953 int batch_size, released;
4955 released = release_stripe_list(conf);
4957 batch_size = handle_active_stripes(conf, group_id);
4958 if (!batch_size && !released)
4960 handled += batch_size;
4962 pr_debug("%d stripes handled\n", handled);
4964 spin_unlock_irq(&conf->device_lock);
4965 blk_finish_plug(&plug);
4967 pr_debug("--- raid5worker inactive\n");
4971 * This is our raid5 kernel thread.
4973 * We scan the hash table for stripes which can be handled now.
4974 * During the scan, completed stripes are saved for us by the interrupt
4975 * handler, so that they will not have to wait for our next wakeup.
4977 static void raid5d(struct md_thread *thread)
4979 struct mddev *mddev = thread->mddev;
4980 struct r5conf *conf = mddev->private;
4982 struct blk_plug plug;
4984 pr_debug("+++ raid5d active\n");
4986 md_check_recovery(mddev);
4988 blk_start_plug(&plug);
4990 spin_lock_irq(&conf->device_lock);
4993 int batch_size, released;
4995 released = release_stripe_list(conf);
4998 !list_empty(&conf->bitmap_list)) {
4999 /* Now is a good time to flush some bitmap updates */
5001 spin_unlock_irq(&conf->device_lock);
5002 bitmap_unplug(mddev->bitmap);
5003 spin_lock_irq(&conf->device_lock);
5004 conf->seq_write = conf->seq_flush;
5005 activate_bit_delay(conf);
5007 raid5_activate_delayed(conf);
5009 while ((bio = remove_bio_from_retry(conf))) {
5011 spin_unlock_irq(&conf->device_lock);
5012 ok = retry_aligned_read(conf, bio);
5013 spin_lock_irq(&conf->device_lock);
5019 batch_size = handle_active_stripes(conf, ANY_GROUP);
5020 if (!batch_size && !released)
5022 handled += batch_size;
5024 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5025 spin_unlock_irq(&conf->device_lock);
5026 md_check_recovery(mddev);
5027 spin_lock_irq(&conf->device_lock);
5030 pr_debug("%d stripes handled\n", handled);
5032 spin_unlock_irq(&conf->device_lock);
5034 async_tx_issue_pending_all();
5035 blk_finish_plug(&plug);
5037 pr_debug("--- raid5d inactive\n");
5041 raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
5043 struct r5conf *conf = mddev->private;
5045 return sprintf(page, "%d\n", conf->max_nr_stripes);
5051 raid5_set_cache_size(struct mddev *mddev, int size)
5053 struct r5conf *conf = mddev->private;
5056 if (size <= 16 || size > 32768)
5058 while (size < conf->max_nr_stripes) {
5059 if (drop_one_stripe(conf))
5060 conf->max_nr_stripes--;
5064 err = md_allow_write(mddev);
5067 while (size > conf->max_nr_stripes) {
5068 if (grow_one_stripe(conf))
5069 conf->max_nr_stripes++;
5074 EXPORT_SYMBOL(raid5_set_cache_size);
5077 raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
5079 struct r5conf *conf = mddev->private;
5083 if (len >= PAGE_SIZE)
5088 if (kstrtoul(page, 10, &new))
5090 err = raid5_set_cache_size(mddev, new);
5096 static struct md_sysfs_entry
5097 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5098 raid5_show_stripe_cache_size,
5099 raid5_store_stripe_cache_size);
5102 raid5_show_preread_threshold(struct mddev *mddev, char *page)
5104 struct r5conf *conf = mddev->private;
5106 return sprintf(page, "%d\n", conf->bypass_threshold);
5112 raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
5114 struct r5conf *conf = mddev->private;
5116 if (len >= PAGE_SIZE)
5121 if (kstrtoul(page, 10, &new))
5123 if (new > conf->max_nr_stripes)
5125 conf->bypass_threshold = new;
5129 static struct md_sysfs_entry
5130 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5132 raid5_show_preread_threshold,
5133 raid5_store_preread_threshold);
5136 stripe_cache_active_show(struct mddev *mddev, char *page)
5138 struct r5conf *conf = mddev->private;
5140 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5145 static struct md_sysfs_entry
5146 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
5149 raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5151 struct r5conf *conf = mddev->private;
5153 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5158 static int alloc_thread_groups(struct r5conf *conf, int cnt);
5160 raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5162 struct r5conf *conf = mddev->private;
5165 struct r5worker_group *old_groups;
5168 if (len >= PAGE_SIZE)
5173 if (kstrtoul(page, 10, &new))
5176 if (new == conf->worker_cnt_per_group)
5179 mddev_suspend(mddev);
5181 old_groups = conf->worker_groups;
5182 old_group_cnt = conf->worker_cnt_per_group;
5184 conf->worker_groups = NULL;
5185 err = alloc_thread_groups(conf, new);
5187 conf->worker_groups = old_groups;
5188 conf->worker_cnt_per_group = old_group_cnt;
5191 kfree(old_groups[0].workers);
5195 mddev_resume(mddev);
5202 static struct md_sysfs_entry
5203 raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5204 raid5_show_group_thread_cnt,
5205 raid5_store_group_thread_cnt);
5207 static struct attribute *raid5_attrs[] = {
5208 &raid5_stripecache_size.attr,
5209 &raid5_stripecache_active.attr,
5210 &raid5_preread_bypass_threshold.attr,
5211 &raid5_group_thread_cnt.attr,
5214 static struct attribute_group raid5_attrs_group = {
5216 .attrs = raid5_attrs,
5219 static int alloc_thread_groups(struct r5conf *conf, int cnt)
5223 struct r5worker *workers;
5225 conf->worker_cnt_per_group = cnt;
5227 conf->worker_groups = NULL;
5230 conf->group_cnt = num_possible_nodes();
5231 size = sizeof(struct r5worker) * cnt;
5232 workers = kzalloc(size * conf->group_cnt, GFP_NOIO);
5233 conf->worker_groups = kzalloc(sizeof(struct r5worker_group) *
5234 conf->group_cnt, GFP_NOIO);
5235 if (!conf->worker_groups || !workers) {
5237 kfree(conf->worker_groups);
5238 conf->worker_groups = NULL;
5242 for (i = 0; i < conf->group_cnt; i++) {
5243 struct r5worker_group *group;
5245 group = &conf->worker_groups[i];
5246 INIT_LIST_HEAD(&group->handle_list);
5248 group->workers = workers + i * cnt;
5250 for (j = 0; j < cnt; j++) {
5251 group->workers[j].group = group;
5252 INIT_WORK(&group->workers[j].work, raid5_do_work);
5259 static void free_thread_groups(struct r5conf *conf)
5261 if (conf->worker_groups)
5262 kfree(conf->worker_groups[0].workers);
5263 kfree(conf->worker_groups);
5264 conf->worker_groups = NULL;
5268 raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
5270 struct r5conf *conf = mddev->private;
5273 sectors = mddev->dev_sectors;
5275 /* size is defined by the smallest of previous and new size */
5276 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
5278 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
5279 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
5280 return sectors * (raid_disks - conf->max_degraded);
5283 static void raid5_free_percpu(struct r5conf *conf)
5285 struct raid5_percpu *percpu;
5292 for_each_possible_cpu(cpu) {
5293 percpu = per_cpu_ptr(conf->percpu, cpu);
5294 safe_put_page(percpu->spare_page);
5295 kfree(percpu->scribble);
5297 #ifdef CONFIG_HOTPLUG_CPU
5298 unregister_cpu_notifier(&conf->cpu_notify);
5302 free_percpu(conf->percpu);
5305 static void free_conf(struct r5conf *conf)
5307 free_thread_groups(conf);
5308 shrink_stripes(conf);
5309 raid5_free_percpu(conf);
5311 kfree(conf->stripe_hashtbl);
5315 #ifdef CONFIG_HOTPLUG_CPU
5316 static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5319 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
5320 long cpu = (long)hcpu;
5321 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5324 case CPU_UP_PREPARE:
5325 case CPU_UP_PREPARE_FROZEN:
5326 if (conf->level == 6 && !percpu->spare_page)
5327 percpu->spare_page = alloc_page(GFP_KERNEL);
5328 if (!percpu->scribble)
5329 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5331 if (!percpu->scribble ||
5332 (conf->level == 6 && !percpu->spare_page)) {
5333 safe_put_page(percpu->spare_page);
5334 kfree(percpu->scribble);
5335 pr_err("%s: failed memory allocation for cpu%ld\n",
5337 return notifier_from_errno(-ENOMEM);
5341 case CPU_DEAD_FROZEN:
5342 safe_put_page(percpu->spare_page);
5343 kfree(percpu->scribble);
5344 percpu->spare_page = NULL;
5345 percpu->scribble = NULL;
5354 static int raid5_alloc_percpu(struct r5conf *conf)
5357 struct page *spare_page;
5358 struct raid5_percpu __percpu *allcpus;
5362 allcpus = alloc_percpu(struct raid5_percpu);
5365 conf->percpu = allcpus;
5369 for_each_present_cpu(cpu) {
5370 if (conf->level == 6) {
5371 spare_page = alloc_page(GFP_KERNEL);
5376 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5378 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5383 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
5385 #ifdef CONFIG_HOTPLUG_CPU
5386 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5387 conf->cpu_notify.priority = 0;
5389 err = register_cpu_notifier(&conf->cpu_notify);
5396 static struct r5conf *setup_conf(struct mddev *mddev)
5398 struct r5conf *conf;
5399 int raid_disk, memory, max_disks;
5400 struct md_rdev *rdev;
5401 struct disk_info *disk;
5404 if (mddev->new_level != 5
5405 && mddev->new_level != 4
5406 && mddev->new_level != 6) {
5407 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
5408 mdname(mddev), mddev->new_level);
5409 return ERR_PTR(-EIO);
5411 if ((mddev->new_level == 5
5412 && !algorithm_valid_raid5(mddev->new_layout)) ||
5413 (mddev->new_level == 6
5414 && !algorithm_valid_raid6(mddev->new_layout))) {
5415 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
5416 mdname(mddev), mddev->new_layout);
5417 return ERR_PTR(-EIO);
5419 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
5420 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
5421 mdname(mddev), mddev->raid_disks);
5422 return ERR_PTR(-EINVAL);
5425 if (!mddev->new_chunk_sectors ||
5426 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5427 !is_power_of_2(mddev->new_chunk_sectors)) {
5428 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5429 mdname(mddev), mddev->new_chunk_sectors << 9);
5430 return ERR_PTR(-EINVAL);
5433 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
5436 /* Don't enable multi-threading by default*/
5437 if (alloc_thread_groups(conf, 0))
5439 spin_lock_init(&conf->device_lock);
5440 init_waitqueue_head(&conf->wait_for_stripe);
5441 init_waitqueue_head(&conf->wait_for_overlap);
5442 INIT_LIST_HEAD(&conf->handle_list);
5443 INIT_LIST_HEAD(&conf->hold_list);
5444 INIT_LIST_HEAD(&conf->delayed_list);
5445 INIT_LIST_HEAD(&conf->bitmap_list);
5446 INIT_LIST_HEAD(&conf->inactive_list);
5447 init_llist_head(&conf->released_stripes);
5448 atomic_set(&conf->active_stripes, 0);
5449 atomic_set(&conf->preread_active_stripes, 0);
5450 atomic_set(&conf->active_aligned_reads, 0);
5451 conf->bypass_threshold = BYPASS_THRESHOLD;
5452 conf->recovery_disabled = mddev->recovery_disabled - 1;
5454 conf->raid_disks = mddev->raid_disks;
5455 if (mddev->reshape_position == MaxSector)
5456 conf->previous_raid_disks = mddev->raid_disks;
5458 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5459 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5460 conf->scribble_len = scribble_len(max_disks);
5462 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
5467 conf->mddev = mddev;
5469 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5472 conf->level = mddev->new_level;
5473 if (raid5_alloc_percpu(conf) != 0)
5476 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
5478 rdev_for_each(rdev, mddev) {
5479 raid_disk = rdev->raid_disk;
5480 if (raid_disk >= max_disks
5483 disk = conf->disks + raid_disk;
5485 if (test_bit(Replacement, &rdev->flags)) {
5486 if (disk->replacement)
5488 disk->replacement = rdev;
5495 if (test_bit(In_sync, &rdev->flags)) {
5496 char b[BDEVNAME_SIZE];
5497 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5499 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
5500 } else if (rdev->saved_raid_disk != raid_disk)
5501 /* Cannot rely on bitmap to complete recovery */
5505 conf->chunk_sectors = mddev->new_chunk_sectors;
5506 conf->level = mddev->new_level;
5507 if (conf->level == 6)
5508 conf->max_degraded = 2;
5510 conf->max_degraded = 1;
5511 conf->algorithm = mddev->new_layout;
5512 conf->max_nr_stripes = NR_STRIPES;
5513 conf->reshape_progress = mddev->reshape_position;
5514 if (conf->reshape_progress != MaxSector) {
5515 conf->prev_chunk_sectors = mddev->chunk_sectors;
5516 conf->prev_algo = mddev->layout;
5519 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
5520 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
5521 if (grow_stripes(conf, conf->max_nr_stripes)) {
5523 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5524 mdname(mddev), memory);
5527 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5528 mdname(mddev), memory);
5530 sprintf(pers_name, "raid%d", mddev->new_level);
5531 conf->thread = md_register_thread(raid5d, mddev, pers_name);
5532 if (!conf->thread) {
5534 "md/raid:%s: couldn't allocate thread.\n",
5544 return ERR_PTR(-EIO);
5546 return ERR_PTR(-ENOMEM);
5550 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5553 case ALGORITHM_PARITY_0:
5554 if (raid_disk < max_degraded)
5557 case ALGORITHM_PARITY_N:
5558 if (raid_disk >= raid_disks - max_degraded)
5561 case ALGORITHM_PARITY_0_6:
5562 if (raid_disk == 0 ||
5563 raid_disk == raid_disks - 1)
5566 case ALGORITHM_LEFT_ASYMMETRIC_6:
5567 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5568 case ALGORITHM_LEFT_SYMMETRIC_6:
5569 case ALGORITHM_RIGHT_SYMMETRIC_6:
5570 if (raid_disk == raid_disks - 1)
5576 static int run(struct mddev *mddev)
5578 struct r5conf *conf;
5579 int working_disks = 0;
5580 int dirty_parity_disks = 0;
5581 struct md_rdev *rdev;
5582 sector_t reshape_offset = 0;
5584 long long min_offset_diff = 0;
5587 if (mddev->recovery_cp != MaxSector)
5588 printk(KERN_NOTICE "md/raid:%s: not clean"
5589 " -- starting background reconstruction\n",
5592 rdev_for_each(rdev, mddev) {
5594 if (rdev->raid_disk < 0)
5596 diff = (rdev->new_data_offset - rdev->data_offset);
5598 min_offset_diff = diff;
5600 } else if (mddev->reshape_backwards &&
5601 diff < min_offset_diff)
5602 min_offset_diff = diff;
5603 else if (!mddev->reshape_backwards &&
5604 diff > min_offset_diff)
5605 min_offset_diff = diff;
5608 if (mddev->reshape_position != MaxSector) {
5609 /* Check that we can continue the reshape.
5610 * Difficulties arise if the stripe we would write to
5611 * next is at or after the stripe we would read from next.
5612 * For a reshape that changes the number of devices, this
5613 * is only possible for a very short time, and mdadm makes
5614 * sure that time appears to have past before assembling
5615 * the array. So we fail if that time hasn't passed.
5616 * For a reshape that keeps the number of devices the same
5617 * mdadm must be monitoring the reshape can keeping the
5618 * critical areas read-only and backed up. It will start
5619 * the array in read-only mode, so we check for that.
5621 sector_t here_new, here_old;
5623 int max_degraded = (mddev->level == 6 ? 2 : 1);
5625 if (mddev->new_level != mddev->level) {
5626 printk(KERN_ERR "md/raid:%s: unsupported reshape "
5627 "required - aborting.\n",
5631 old_disks = mddev->raid_disks - mddev->delta_disks;
5632 /* reshape_position must be on a new-stripe boundary, and one
5633 * further up in new geometry must map after here in old
5636 here_new = mddev->reshape_position;
5637 if (sector_div(here_new, mddev->new_chunk_sectors *
5638 (mddev->raid_disks - max_degraded))) {
5639 printk(KERN_ERR "md/raid:%s: reshape_position not "
5640 "on a stripe boundary\n", mdname(mddev));
5643 reshape_offset = here_new * mddev->new_chunk_sectors;
5644 /* here_new is the stripe we will write to */
5645 here_old = mddev->reshape_position;
5646 sector_div(here_old, mddev->chunk_sectors *
5647 (old_disks-max_degraded));
5648 /* here_old is the first stripe that we might need to read
5650 if (mddev->delta_disks == 0) {
5651 if ((here_new * mddev->new_chunk_sectors !=
5652 here_old * mddev->chunk_sectors)) {
5653 printk(KERN_ERR "md/raid:%s: reshape position is"
5654 " confused - aborting\n", mdname(mddev));
5657 /* We cannot be sure it is safe to start an in-place
5658 * reshape. It is only safe if user-space is monitoring
5659 * and taking constant backups.
5660 * mdadm always starts a situation like this in
5661 * readonly mode so it can take control before
5662 * allowing any writes. So just check for that.
5664 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5665 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5666 /* not really in-place - so OK */;
5667 else if (mddev->ro == 0) {
5668 printk(KERN_ERR "md/raid:%s: in-place reshape "
5669 "must be started in read-only mode "
5674 } else if (mddev->reshape_backwards
5675 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
5676 here_old * mddev->chunk_sectors)
5677 : (here_new * mddev->new_chunk_sectors >=
5678 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
5679 /* Reading from the same stripe as writing to - bad */
5680 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5681 "auto-recovery - aborting.\n",
5685 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5687 /* OK, we should be able to continue; */
5689 BUG_ON(mddev->level != mddev->new_level);
5690 BUG_ON(mddev->layout != mddev->new_layout);
5691 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
5692 BUG_ON(mddev->delta_disks != 0);
5695 if (mddev->private == NULL)
5696 conf = setup_conf(mddev);
5698 conf = mddev->private;
5701 return PTR_ERR(conf);
5703 conf->min_offset_diff = min_offset_diff;
5704 mddev->thread = conf->thread;
5705 conf->thread = NULL;
5706 mddev->private = conf;
5708 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5710 rdev = conf->disks[i].rdev;
5711 if (!rdev && conf->disks[i].replacement) {
5712 /* The replacement is all we have yet */
5713 rdev = conf->disks[i].replacement;
5714 conf->disks[i].replacement = NULL;
5715 clear_bit(Replacement, &rdev->flags);
5716 conf->disks[i].rdev = rdev;
5720 if (conf->disks[i].replacement &&
5721 conf->reshape_progress != MaxSector) {
5722 /* replacements and reshape simply do not mix. */
5723 printk(KERN_ERR "md: cannot handle concurrent "
5724 "replacement and reshape.\n");
5727 if (test_bit(In_sync, &rdev->flags)) {
5731 /* This disc is not fully in-sync. However if it
5732 * just stored parity (beyond the recovery_offset),
5733 * when we don't need to be concerned about the
5734 * array being dirty.
5735 * When reshape goes 'backwards', we never have
5736 * partially completed devices, so we only need
5737 * to worry about reshape going forwards.
5739 /* Hack because v0.91 doesn't store recovery_offset properly. */
5740 if (mddev->major_version == 0 &&
5741 mddev->minor_version > 90)
5742 rdev->recovery_offset = reshape_offset;
5744 if (rdev->recovery_offset < reshape_offset) {
5745 /* We need to check old and new layout */
5746 if (!only_parity(rdev->raid_disk,
5749 conf->max_degraded))
5752 if (!only_parity(rdev->raid_disk,
5754 conf->previous_raid_disks,
5755 conf->max_degraded))
5757 dirty_parity_disks++;
5761 * 0 for a fully functional array, 1 or 2 for a degraded array.
5763 mddev->degraded = calc_degraded(conf);
5765 if (has_failed(conf)) {
5766 printk(KERN_ERR "md/raid:%s: not enough operational devices"
5767 " (%d/%d failed)\n",
5768 mdname(mddev), mddev->degraded, conf->raid_disks);
5772 /* device size must be a multiple of chunk size */
5773 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
5774 mddev->resync_max_sectors = mddev->dev_sectors;
5776 if (mddev->degraded > dirty_parity_disks &&
5777 mddev->recovery_cp != MaxSector) {
5778 if (mddev->ok_start_degraded)
5780 "md/raid:%s: starting dirty degraded array"
5781 " - data corruption possible.\n",
5785 "md/raid:%s: cannot start dirty degraded array.\n",
5791 if (mddev->degraded == 0)
5792 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5793 " devices, algorithm %d\n", mdname(mddev), conf->level,
5794 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5797 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5798 " out of %d devices, algorithm %d\n",
5799 mdname(mddev), conf->level,
5800 mddev->raid_disks - mddev->degraded,
5801 mddev->raid_disks, mddev->new_layout);
5803 print_raid5_conf(conf);
5805 if (conf->reshape_progress != MaxSector) {
5806 conf->reshape_safe = conf->reshape_progress;
5807 atomic_set(&conf->reshape_stripes, 0);
5808 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5809 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5810 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5811 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5812 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5817 /* Ok, everything is just fine now */
5818 if (mddev->to_remove == &raid5_attrs_group)
5819 mddev->to_remove = NULL;
5820 else if (mddev->kobj.sd &&
5821 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5823 "raid5: failed to create sysfs attributes for %s\n",
5825 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5829 bool discard_supported = true;
5830 /* read-ahead size must cover two whole stripes, which
5831 * is 2 * (datadisks) * chunksize where 'n' is the
5832 * number of raid devices
5834 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5835 int stripe = data_disks *
5836 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5837 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5838 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5840 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
5842 mddev->queue->backing_dev_info.congested_data = mddev;
5843 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
5845 chunk_size = mddev->chunk_sectors << 9;
5846 blk_queue_io_min(mddev->queue, chunk_size);
5847 blk_queue_io_opt(mddev->queue, chunk_size *
5848 (conf->raid_disks - conf->max_degraded));
5850 * We can only discard a whole stripe. It doesn't make sense to
5851 * discard data disk but write parity disk
5853 stripe = stripe * PAGE_SIZE;
5854 /* Round up to power of 2, as discard handling
5855 * currently assumes that */
5856 while ((stripe-1) & stripe)
5857 stripe = (stripe | (stripe-1)) + 1;
5858 mddev->queue->limits.discard_alignment = stripe;
5859 mddev->queue->limits.discard_granularity = stripe;
5861 * unaligned part of discard request will be ignored, so can't
5862 * guarantee discard_zerors_data
5864 mddev->queue->limits.discard_zeroes_data = 0;
5866 blk_queue_max_write_same_sectors(mddev->queue, 0);
5868 rdev_for_each(rdev, mddev) {
5869 disk_stack_limits(mddev->gendisk, rdev->bdev,
5870 rdev->data_offset << 9);
5871 disk_stack_limits(mddev->gendisk, rdev->bdev,
5872 rdev->new_data_offset << 9);
5874 * discard_zeroes_data is required, otherwise data
5875 * could be lost. Consider a scenario: discard a stripe
5876 * (the stripe could be inconsistent if
5877 * discard_zeroes_data is 0); write one disk of the
5878 * stripe (the stripe could be inconsistent again
5879 * depending on which disks are used to calculate
5880 * parity); the disk is broken; The stripe data of this
5883 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5884 !bdev_get_queue(rdev->bdev)->
5885 limits.discard_zeroes_data)
5886 discard_supported = false;
5889 if (discard_supported &&
5890 mddev->queue->limits.max_discard_sectors >= stripe &&
5891 mddev->queue->limits.discard_granularity >= stripe)
5892 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5895 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5901 md_unregister_thread(&mddev->thread);
5902 print_raid5_conf(conf);
5904 mddev->private = NULL;
5905 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
5909 static int stop(struct mddev *mddev)
5911 struct r5conf *conf = mddev->private;
5913 md_unregister_thread(&mddev->thread);
5915 mddev->queue->backing_dev_info.congested_fn = NULL;
5917 mddev->private = NULL;
5918 mddev->to_remove = &raid5_attrs_group;
5922 static void status(struct seq_file *seq, struct mddev *mddev)
5924 struct r5conf *conf = mddev->private;
5927 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5928 mddev->chunk_sectors / 2, mddev->layout);
5929 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5930 for (i = 0; i < conf->raid_disks; i++)
5931 seq_printf (seq, "%s",
5932 conf->disks[i].rdev &&
5933 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
5934 seq_printf (seq, "]");
5937 static void print_raid5_conf (struct r5conf *conf)
5940 struct disk_info *tmp;
5942 printk(KERN_DEBUG "RAID conf printout:\n");
5944 printk("(conf==NULL)\n");
5947 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5949 conf->raid_disks - conf->mddev->degraded);
5951 for (i = 0; i < conf->raid_disks; i++) {
5952 char b[BDEVNAME_SIZE];
5953 tmp = conf->disks + i;
5955 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5956 i, !test_bit(Faulty, &tmp->rdev->flags),
5957 bdevname(tmp->rdev->bdev, b));
5961 static int raid5_spare_active(struct mddev *mddev)
5964 struct r5conf *conf = mddev->private;
5965 struct disk_info *tmp;
5967 unsigned long flags;
5969 for (i = 0; i < conf->raid_disks; i++) {
5970 tmp = conf->disks + i;
5971 if (tmp->replacement
5972 && tmp->replacement->recovery_offset == MaxSector
5973 && !test_bit(Faulty, &tmp->replacement->flags)
5974 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5975 /* Replacement has just become active. */
5977 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5980 /* Replaced device not technically faulty,
5981 * but we need to be sure it gets removed
5982 * and never re-added.
5984 set_bit(Faulty, &tmp->rdev->flags);
5985 sysfs_notify_dirent_safe(
5986 tmp->rdev->sysfs_state);
5988 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5989 } else if (tmp->rdev
5990 && tmp->rdev->recovery_offset == MaxSector
5991 && !test_bit(Faulty, &tmp->rdev->flags)
5992 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5994 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
5997 spin_lock_irqsave(&conf->device_lock, flags);
5998 mddev->degraded = calc_degraded(conf);
5999 spin_unlock_irqrestore(&conf->device_lock, flags);
6000 print_raid5_conf(conf);
6004 static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
6006 struct r5conf *conf = mddev->private;
6008 int number = rdev->raid_disk;
6009 struct md_rdev **rdevp;
6010 struct disk_info *p = conf->disks + number;
6012 print_raid5_conf(conf);
6013 if (rdev == p->rdev)
6015 else if (rdev == p->replacement)
6016 rdevp = &p->replacement;
6020 if (number >= conf->raid_disks &&
6021 conf->reshape_progress == MaxSector)
6022 clear_bit(In_sync, &rdev->flags);
6024 if (test_bit(In_sync, &rdev->flags) ||
6025 atomic_read(&rdev->nr_pending)) {
6029 /* Only remove non-faulty devices if recovery
6032 if (!test_bit(Faulty, &rdev->flags) &&
6033 mddev->recovery_disabled != conf->recovery_disabled &&
6034 !has_failed(conf) &&
6035 (!p->replacement || p->replacement == rdev) &&
6036 number < conf->raid_disks) {
6042 if (atomic_read(&rdev->nr_pending)) {
6043 /* lost the race, try later */
6046 } else if (p->replacement) {
6047 /* We must have just cleared 'rdev' */
6048 p->rdev = p->replacement;
6049 clear_bit(Replacement, &p->replacement->flags);
6050 smp_mb(); /* Make sure other CPUs may see both as identical
6051 * but will never see neither - if they are careful
6053 p->replacement = NULL;
6054 clear_bit(WantReplacement, &rdev->flags);
6056 /* We might have just removed the Replacement as faulty-
6057 * clear the bit just in case
6059 clear_bit(WantReplacement, &rdev->flags);
6062 print_raid5_conf(conf);
6066 static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
6068 struct r5conf *conf = mddev->private;
6071 struct disk_info *p;
6073 int last = conf->raid_disks - 1;
6075 if (mddev->recovery_disabled == conf->recovery_disabled)
6078 if (rdev->saved_raid_disk < 0 && has_failed(conf))
6079 /* no point adding a device */
6082 if (rdev->raid_disk >= 0)
6083 first = last = rdev->raid_disk;
6086 * find the disk ... but prefer rdev->saved_raid_disk
6089 if (rdev->saved_raid_disk >= 0 &&
6090 rdev->saved_raid_disk >= first &&
6091 conf->disks[rdev->saved_raid_disk].rdev == NULL)
6092 first = rdev->saved_raid_disk;
6094 for (disk = first; disk <= last; disk++) {
6095 p = conf->disks + disk;
6096 if (p->rdev == NULL) {
6097 clear_bit(In_sync, &rdev->flags);
6098 rdev->raid_disk = disk;
6100 if (rdev->saved_raid_disk != disk)
6102 rcu_assign_pointer(p->rdev, rdev);
6106 for (disk = first; disk <= last; disk++) {
6107 p = conf->disks + disk;
6108 if (test_bit(WantReplacement, &p->rdev->flags) &&
6109 p->replacement == NULL) {
6110 clear_bit(In_sync, &rdev->flags);
6111 set_bit(Replacement, &rdev->flags);
6112 rdev->raid_disk = disk;
6115 rcu_assign_pointer(p->replacement, rdev);
6120 print_raid5_conf(conf);
6124 static int raid5_resize(struct mddev *mddev, sector_t sectors)
6126 /* no resync is happening, and there is enough space
6127 * on all devices, so we can resize.
6128 * We need to make sure resync covers any new space.
6129 * If the array is shrinking we should possibly wait until
6130 * any io in the removed space completes, but it hardly seems
6134 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
6135 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6136 if (mddev->external_size &&
6137 mddev->array_sectors > newsize)
6139 if (mddev->bitmap) {
6140 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6144 md_set_array_sectors(mddev, newsize);
6145 set_capacity(mddev->gendisk, mddev->array_sectors);
6146 revalidate_disk(mddev->gendisk);
6147 if (sectors > mddev->dev_sectors &&
6148 mddev->recovery_cp > mddev->dev_sectors) {
6149 mddev->recovery_cp = mddev->dev_sectors;
6150 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6152 mddev->dev_sectors = sectors;
6153 mddev->resync_max_sectors = sectors;
6157 static int check_stripe_cache(struct mddev *mddev)
6159 /* Can only proceed if there are plenty of stripe_heads.
6160 * We need a minimum of one full stripe,, and for sensible progress
6161 * it is best to have about 4 times that.
6162 * If we require 4 times, then the default 256 4K stripe_heads will
6163 * allow for chunk sizes up to 256K, which is probably OK.
6164 * If the chunk size is greater, user-space should request more
6165 * stripe_heads first.
6167 struct r5conf *conf = mddev->private;
6168 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6169 > conf->max_nr_stripes ||
6170 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6171 > conf->max_nr_stripes) {
6172 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6174 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6181 static int check_reshape(struct mddev *mddev)
6183 struct r5conf *conf = mddev->private;
6185 if (mddev->delta_disks == 0 &&
6186 mddev->new_layout == mddev->layout &&
6187 mddev->new_chunk_sectors == mddev->chunk_sectors)
6188 return 0; /* nothing to do */
6189 if (has_failed(conf))
6191 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
6192 /* We might be able to shrink, but the devices must
6193 * be made bigger first.
6194 * For raid6, 4 is the minimum size.
6195 * Otherwise 2 is the minimum
6198 if (mddev->level == 6)
6200 if (mddev->raid_disks + mddev->delta_disks < min)
6204 if (!check_stripe_cache(mddev))
6207 return resize_stripes(conf, (conf->previous_raid_disks
6208 + mddev->delta_disks));
6211 static int raid5_start_reshape(struct mddev *mddev)
6213 struct r5conf *conf = mddev->private;
6214 struct md_rdev *rdev;
6216 unsigned long flags;
6218 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
6221 if (!check_stripe_cache(mddev))
6224 if (has_failed(conf))
6227 rdev_for_each(rdev, mddev) {
6228 if (!test_bit(In_sync, &rdev->flags)
6229 && !test_bit(Faulty, &rdev->flags))
6233 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
6234 /* Not enough devices even to make a degraded array
6239 /* Refuse to reduce size of the array. Any reductions in
6240 * array size must be through explicit setting of array_size
6243 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6244 < mddev->array_sectors) {
6245 printk(KERN_ERR "md/raid:%s: array size must be reduced "
6246 "before number of disks\n", mdname(mddev));
6250 atomic_set(&conf->reshape_stripes, 0);
6251 spin_lock_irq(&conf->device_lock);
6252 conf->previous_raid_disks = conf->raid_disks;
6253 conf->raid_disks += mddev->delta_disks;
6254 conf->prev_chunk_sectors = conf->chunk_sectors;
6255 conf->chunk_sectors = mddev->new_chunk_sectors;
6256 conf->prev_algo = conf->algorithm;
6257 conf->algorithm = mddev->new_layout;
6259 /* Code that selects data_offset needs to see the generation update
6260 * if reshape_progress has been set - so a memory barrier needed.
6263 if (mddev->reshape_backwards)
6264 conf->reshape_progress = raid5_size(mddev, 0, 0);
6266 conf->reshape_progress = 0;
6267 conf->reshape_safe = conf->reshape_progress;
6268 spin_unlock_irq(&conf->device_lock);
6270 /* Add some new drives, as many as will fit.
6271 * We know there are enough to make the newly sized array work.
6272 * Don't add devices if we are reducing the number of
6273 * devices in the array. This is because it is not possible
6274 * to correctly record the "partially reconstructed" state of
6275 * such devices during the reshape and confusion could result.
6277 if (mddev->delta_disks >= 0) {
6278 rdev_for_each(rdev, mddev)
6279 if (rdev->raid_disk < 0 &&
6280 !test_bit(Faulty, &rdev->flags)) {
6281 if (raid5_add_disk(mddev, rdev) == 0) {
6283 >= conf->previous_raid_disks)
6284 set_bit(In_sync, &rdev->flags);
6286 rdev->recovery_offset = 0;
6288 if (sysfs_link_rdev(mddev, rdev))
6289 /* Failure here is OK */;
6291 } else if (rdev->raid_disk >= conf->previous_raid_disks
6292 && !test_bit(Faulty, &rdev->flags)) {
6293 /* This is a spare that was manually added */
6294 set_bit(In_sync, &rdev->flags);
6297 /* When a reshape changes the number of devices,
6298 * ->degraded is measured against the larger of the
6299 * pre and post number of devices.
6301 spin_lock_irqsave(&conf->device_lock, flags);
6302 mddev->degraded = calc_degraded(conf);
6303 spin_unlock_irqrestore(&conf->device_lock, flags);
6305 mddev->raid_disks = conf->raid_disks;
6306 mddev->reshape_position = conf->reshape_progress;
6307 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6309 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6310 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6311 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6312 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6313 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
6315 if (!mddev->sync_thread) {
6316 mddev->recovery = 0;
6317 spin_lock_irq(&conf->device_lock);
6318 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
6319 rdev_for_each(rdev, mddev)
6320 rdev->new_data_offset = rdev->data_offset;
6322 conf->reshape_progress = MaxSector;
6323 mddev->reshape_position = MaxSector;
6324 spin_unlock_irq(&conf->device_lock);
6327 conf->reshape_checkpoint = jiffies;
6328 md_wakeup_thread(mddev->sync_thread);
6329 md_new_event(mddev);
6333 /* This is called from the reshape thread and should make any
6334 * changes needed in 'conf'
6336 static void end_reshape(struct r5conf *conf)
6339 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
6340 struct md_rdev *rdev;
6342 spin_lock_irq(&conf->device_lock);
6343 conf->previous_raid_disks = conf->raid_disks;
6344 rdev_for_each(rdev, conf->mddev)
6345 rdev->data_offset = rdev->new_data_offset;
6347 conf->reshape_progress = MaxSector;
6348 spin_unlock_irq(&conf->device_lock);
6349 wake_up(&conf->wait_for_overlap);
6351 /* read-ahead size must cover two whole stripes, which is
6352 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6354 if (conf->mddev->queue) {
6355 int data_disks = conf->raid_disks - conf->max_degraded;
6356 int stripe = data_disks * ((conf->chunk_sectors << 9)
6358 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6359 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6364 /* This is called from the raid5d thread with mddev_lock held.
6365 * It makes config changes to the device.
6367 static void raid5_finish_reshape(struct mddev *mddev)
6369 struct r5conf *conf = mddev->private;
6371 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6373 if (mddev->delta_disks > 0) {
6374 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6375 set_capacity(mddev->gendisk, mddev->array_sectors);
6376 revalidate_disk(mddev->gendisk);
6379 spin_lock_irq(&conf->device_lock);
6380 mddev->degraded = calc_degraded(conf);
6381 spin_unlock_irq(&conf->device_lock);
6382 for (d = conf->raid_disks ;
6383 d < conf->raid_disks - mddev->delta_disks;
6385 struct md_rdev *rdev = conf->disks[d].rdev;
6387 clear_bit(In_sync, &rdev->flags);
6388 rdev = conf->disks[d].replacement;
6390 clear_bit(In_sync, &rdev->flags);
6393 mddev->layout = conf->algorithm;
6394 mddev->chunk_sectors = conf->chunk_sectors;
6395 mddev->reshape_position = MaxSector;
6396 mddev->delta_disks = 0;
6397 mddev->reshape_backwards = 0;
6401 static void raid5_quiesce(struct mddev *mddev, int state)
6403 struct r5conf *conf = mddev->private;
6406 case 2: /* resume for a suspend */
6407 wake_up(&conf->wait_for_overlap);
6410 case 1: /* stop all writes */
6411 spin_lock_irq(&conf->device_lock);
6412 /* '2' tells resync/reshape to pause so that all
6413 * active stripes can drain
6416 wait_event_lock_irq(conf->wait_for_stripe,
6417 atomic_read(&conf->active_stripes) == 0 &&
6418 atomic_read(&conf->active_aligned_reads) == 0,
6421 spin_unlock_irq(&conf->device_lock);
6422 /* allow reshape to continue */
6423 wake_up(&conf->wait_for_overlap);
6426 case 0: /* re-enable writes */
6427 spin_lock_irq(&conf->device_lock);
6429 wake_up(&conf->wait_for_stripe);
6430 wake_up(&conf->wait_for_overlap);
6431 spin_unlock_irq(&conf->device_lock);
6437 static void *raid45_takeover_raid0(struct mddev *mddev, int level)
6439 struct r0conf *raid0_conf = mddev->private;
6442 /* for raid0 takeover only one zone is supported */
6443 if (raid0_conf->nr_strip_zones > 1) {
6444 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6446 return ERR_PTR(-EINVAL);
6449 sectors = raid0_conf->strip_zone[0].zone_end;
6450 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
6451 mddev->dev_sectors = sectors;
6452 mddev->new_level = level;
6453 mddev->new_layout = ALGORITHM_PARITY_N;
6454 mddev->new_chunk_sectors = mddev->chunk_sectors;
6455 mddev->raid_disks += 1;
6456 mddev->delta_disks = 1;
6457 /* make sure it will be not marked as dirty */
6458 mddev->recovery_cp = MaxSector;
6460 return setup_conf(mddev);
6464 static void *raid5_takeover_raid1(struct mddev *mddev)
6468 if (mddev->raid_disks != 2 ||
6469 mddev->degraded > 1)
6470 return ERR_PTR(-EINVAL);
6472 /* Should check if there are write-behind devices? */
6474 chunksect = 64*2; /* 64K by default */
6476 /* The array must be an exact multiple of chunksize */
6477 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6480 if ((chunksect<<9) < STRIPE_SIZE)
6481 /* array size does not allow a suitable chunk size */
6482 return ERR_PTR(-EINVAL);
6484 mddev->new_level = 5;
6485 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
6486 mddev->new_chunk_sectors = chunksect;
6488 return setup_conf(mddev);
6491 static void *raid5_takeover_raid6(struct mddev *mddev)
6495 switch (mddev->layout) {
6496 case ALGORITHM_LEFT_ASYMMETRIC_6:
6497 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6499 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6500 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6502 case ALGORITHM_LEFT_SYMMETRIC_6:
6503 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6505 case ALGORITHM_RIGHT_SYMMETRIC_6:
6506 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6508 case ALGORITHM_PARITY_0_6:
6509 new_layout = ALGORITHM_PARITY_0;
6511 case ALGORITHM_PARITY_N:
6512 new_layout = ALGORITHM_PARITY_N;
6515 return ERR_PTR(-EINVAL);
6517 mddev->new_level = 5;
6518 mddev->new_layout = new_layout;
6519 mddev->delta_disks = -1;
6520 mddev->raid_disks -= 1;
6521 return setup_conf(mddev);
6525 static int raid5_check_reshape(struct mddev *mddev)
6527 /* For a 2-drive array, the layout and chunk size can be changed
6528 * immediately as not restriping is needed.
6529 * For larger arrays we record the new value - after validation
6530 * to be used by a reshape pass.
6532 struct r5conf *conf = mddev->private;
6533 int new_chunk = mddev->new_chunk_sectors;
6535 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
6537 if (new_chunk > 0) {
6538 if (!is_power_of_2(new_chunk))
6540 if (new_chunk < (PAGE_SIZE>>9))
6542 if (mddev->array_sectors & (new_chunk-1))
6543 /* not factor of array size */
6547 /* They look valid */
6549 if (mddev->raid_disks == 2) {
6550 /* can make the change immediately */
6551 if (mddev->new_layout >= 0) {
6552 conf->algorithm = mddev->new_layout;
6553 mddev->layout = mddev->new_layout;
6555 if (new_chunk > 0) {
6556 conf->chunk_sectors = new_chunk ;
6557 mddev->chunk_sectors = new_chunk;
6559 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6560 md_wakeup_thread(mddev->thread);
6562 return check_reshape(mddev);
6565 static int raid6_check_reshape(struct mddev *mddev)
6567 int new_chunk = mddev->new_chunk_sectors;
6569 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
6571 if (new_chunk > 0) {
6572 if (!is_power_of_2(new_chunk))
6574 if (new_chunk < (PAGE_SIZE >> 9))
6576 if (mddev->array_sectors & (new_chunk-1))
6577 /* not factor of array size */
6581 /* They look valid */
6582 return check_reshape(mddev);
6585 static void *raid5_takeover(struct mddev *mddev)
6587 /* raid5 can take over:
6588 * raid0 - if there is only one strip zone - make it a raid4 layout
6589 * raid1 - if there are two drives. We need to know the chunk size
6590 * raid4 - trivial - just use a raid4 layout.
6591 * raid6 - Providing it is a *_6 layout
6593 if (mddev->level == 0)
6594 return raid45_takeover_raid0(mddev, 5);
6595 if (mddev->level == 1)
6596 return raid5_takeover_raid1(mddev);
6597 if (mddev->level == 4) {
6598 mddev->new_layout = ALGORITHM_PARITY_N;
6599 mddev->new_level = 5;
6600 return setup_conf(mddev);
6602 if (mddev->level == 6)
6603 return raid5_takeover_raid6(mddev);
6605 return ERR_PTR(-EINVAL);
6608 static void *raid4_takeover(struct mddev *mddev)
6610 /* raid4 can take over:
6611 * raid0 - if there is only one strip zone
6612 * raid5 - if layout is right
6614 if (mddev->level == 0)
6615 return raid45_takeover_raid0(mddev, 4);
6616 if (mddev->level == 5 &&
6617 mddev->layout == ALGORITHM_PARITY_N) {
6618 mddev->new_layout = 0;
6619 mddev->new_level = 4;
6620 return setup_conf(mddev);
6622 return ERR_PTR(-EINVAL);
6625 static struct md_personality raid5_personality;
6627 static void *raid6_takeover(struct mddev *mddev)
6629 /* Currently can only take over a raid5. We map the
6630 * personality to an equivalent raid6 personality
6631 * with the Q block at the end.
6635 if (mddev->pers != &raid5_personality)
6636 return ERR_PTR(-EINVAL);
6637 if (mddev->degraded > 1)
6638 return ERR_PTR(-EINVAL);
6639 if (mddev->raid_disks > 253)
6640 return ERR_PTR(-EINVAL);
6641 if (mddev->raid_disks < 3)
6642 return ERR_PTR(-EINVAL);
6644 switch (mddev->layout) {
6645 case ALGORITHM_LEFT_ASYMMETRIC:
6646 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6648 case ALGORITHM_RIGHT_ASYMMETRIC:
6649 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6651 case ALGORITHM_LEFT_SYMMETRIC:
6652 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6654 case ALGORITHM_RIGHT_SYMMETRIC:
6655 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6657 case ALGORITHM_PARITY_0:
6658 new_layout = ALGORITHM_PARITY_0_6;
6660 case ALGORITHM_PARITY_N:
6661 new_layout = ALGORITHM_PARITY_N;
6664 return ERR_PTR(-EINVAL);
6666 mddev->new_level = 6;
6667 mddev->new_layout = new_layout;
6668 mddev->delta_disks = 1;
6669 mddev->raid_disks += 1;
6670 return setup_conf(mddev);
6674 static struct md_personality raid6_personality =
6678 .owner = THIS_MODULE,
6679 .make_request = make_request,
6683 .error_handler = error,
6684 .hot_add_disk = raid5_add_disk,
6685 .hot_remove_disk= raid5_remove_disk,
6686 .spare_active = raid5_spare_active,
6687 .sync_request = sync_request,
6688 .resize = raid5_resize,
6690 .check_reshape = raid6_check_reshape,
6691 .start_reshape = raid5_start_reshape,
6692 .finish_reshape = raid5_finish_reshape,
6693 .quiesce = raid5_quiesce,
6694 .takeover = raid6_takeover,
6696 static struct md_personality raid5_personality =
6700 .owner = THIS_MODULE,
6701 .make_request = make_request,
6705 .error_handler = error,
6706 .hot_add_disk = raid5_add_disk,
6707 .hot_remove_disk= raid5_remove_disk,
6708 .spare_active = raid5_spare_active,
6709 .sync_request = sync_request,
6710 .resize = raid5_resize,
6712 .check_reshape = raid5_check_reshape,
6713 .start_reshape = raid5_start_reshape,
6714 .finish_reshape = raid5_finish_reshape,
6715 .quiesce = raid5_quiesce,
6716 .takeover = raid5_takeover,
6719 static struct md_personality raid4_personality =
6723 .owner = THIS_MODULE,
6724 .make_request = make_request,
6728 .error_handler = error,
6729 .hot_add_disk = raid5_add_disk,
6730 .hot_remove_disk= raid5_remove_disk,
6731 .spare_active = raid5_spare_active,
6732 .sync_request = sync_request,
6733 .resize = raid5_resize,
6735 .check_reshape = raid5_check_reshape,
6736 .start_reshape = raid5_start_reshape,
6737 .finish_reshape = raid5_finish_reshape,
6738 .quiesce = raid5_quiesce,
6739 .takeover = raid4_takeover,
6742 static int __init raid5_init(void)
6744 raid5_wq = alloc_workqueue("raid5wq",
6745 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
6748 register_md_personality(&raid6_personality);
6749 register_md_personality(&raid5_personality);
6750 register_md_personality(&raid4_personality);
6754 static void raid5_exit(void)
6756 unregister_md_personality(&raid6_personality);
6757 unregister_md_personality(&raid5_personality);
6758 unregister_md_personality(&raid4_personality);
6759 destroy_workqueue(raid5_wq);
6762 module_init(raid5_init);
6763 module_exit(raid5_exit);
6764 MODULE_LICENSE("GPL");
6765 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6766 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6767 MODULE_ALIAS("md-raid5");
6768 MODULE_ALIAS("md-raid4");
6769 MODULE_ALIAS("md-level-5");
6770 MODULE_ALIAS("md-level-4");
6771 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6772 MODULE_ALIAS("md-raid6");
6773 MODULE_ALIAS("md-level-6");
6775 /* This used to be two separate modules, they were: */
6776 MODULE_ALIAS("raid5");
6777 MODULE_ALIAS("raid6");