2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is seq_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
56 #include <trace/events/block.h>
67 #define NR_STRIPES 256
68 #define STRIPE_SIZE PAGE_SIZE
69 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
70 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
71 #define IO_THRESHOLD 1
72 #define BYPASS_THRESHOLD 1
73 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
74 #define HASH_MASK (NR_HASH - 1)
76 static inline struct hlist_head
*stripe_hash(struct r5conf
*conf
, sector_t sect
)
78 int hash
= (sect
>> STRIPE_SHIFT
) & HASH_MASK
;
79 return &conf
->stripe_hashtbl
[hash
];
82 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
88 * This function is used to determine the 'next' bio in the list, given the sector
89 * of the current stripe+device
91 static inline struct bio
*r5_next_bio(struct bio
*bio
, sector_t sector
)
93 int sectors
= bio
->bi_size
>> 9;
94 if (bio
->bi_sector
+ sectors
< sector
+ STRIPE_SECTORS
)
101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
104 static inline int raid5_bi_processed_stripes(struct bio
*bio
)
106 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
107 return (atomic_read(segments
) >> 16) & 0xffff;
110 static inline int raid5_dec_bi_active_stripes(struct bio
*bio
)
112 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
113 return atomic_sub_return(1, segments
) & 0xffff;
116 static inline void raid5_inc_bi_active_stripes(struct bio
*bio
)
118 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
119 atomic_inc(segments
);
122 static inline void raid5_set_bi_processed_stripes(struct bio
*bio
,
125 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
129 old
= atomic_read(segments
);
130 new = (old
& 0xffff) | (cnt
<< 16);
131 } while (atomic_cmpxchg(segments
, old
, new) != old
);
134 static inline void raid5_set_bi_stripes(struct bio
*bio
, unsigned int cnt
)
136 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
137 atomic_set(segments
, cnt
);
140 /* Find first data disk in a raid6 stripe */
141 static inline int raid6_d0(struct stripe_head
*sh
)
144 /* ddf always start from first device */
146 /* md starts just after Q block */
147 if (sh
->qd_idx
== sh
->disks
- 1)
150 return sh
->qd_idx
+ 1;
152 static inline int raid6_next_disk(int disk
, int raid_disks
)
155 return (disk
< raid_disks
) ? disk
: 0;
158 /* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
163 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
164 int *count
, int syndrome_disks
)
170 if (idx
== sh
->pd_idx
)
171 return syndrome_disks
;
172 if (idx
== sh
->qd_idx
)
173 return syndrome_disks
+ 1;
179 static void return_io(struct bio
*return_bi
)
181 struct bio
*bi
= return_bi
;
184 return_bi
= bi
->bi_next
;
192 static void print_raid5_conf (struct r5conf
*conf
);
194 static int stripe_operations_active(struct stripe_head
*sh
)
196 return sh
->check_state
|| sh
->reconstruct_state
||
197 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
198 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
201 static void do_release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
)
203 BUG_ON(!list_empty(&sh
->lru
));
204 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
205 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
206 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
207 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
208 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
209 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
210 sh
->bm_seq
- conf
->seq_write
> 0)
211 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
213 clear_bit(STRIPE_DELAYED
, &sh
->state
);
214 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
215 list_add_tail(&sh
->lru
, &conf
->handle_list
);
217 md_wakeup_thread(conf
->mddev
->thread
);
219 BUG_ON(stripe_operations_active(sh
));
220 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
221 if (atomic_dec_return(&conf
->preread_active_stripes
)
223 md_wakeup_thread(conf
->mddev
->thread
);
224 atomic_dec(&conf
->active_stripes
);
225 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
226 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
227 wake_up(&conf
->wait_for_stripe
);
228 if (conf
->retry_read_aligned
)
229 md_wakeup_thread(conf
->mddev
->thread
);
234 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
)
236 if (atomic_dec_and_test(&sh
->count
))
237 do_release_stripe(conf
, sh
);
240 static void release_stripe(struct stripe_head
*sh
)
242 struct r5conf
*conf
= sh
->raid_conf
;
245 local_irq_save(flags
);
246 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
247 do_release_stripe(conf
, sh
);
248 spin_unlock(&conf
->device_lock
);
250 local_irq_restore(flags
);
253 static inline void remove_hash(struct stripe_head
*sh
)
255 pr_debug("remove_hash(), stripe %llu\n",
256 (unsigned long long)sh
->sector
);
258 hlist_del_init(&sh
->hash
);
261 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
263 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
265 pr_debug("insert_hash(), stripe %llu\n",
266 (unsigned long long)sh
->sector
);
268 hlist_add_head(&sh
->hash
, hp
);
272 /* find an idle stripe, make sure it is unhashed, and return it. */
273 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
)
275 struct stripe_head
*sh
= NULL
;
276 struct list_head
*first
;
278 if (list_empty(&conf
->inactive_list
))
280 first
= conf
->inactive_list
.next
;
281 sh
= list_entry(first
, struct stripe_head
, lru
);
282 list_del_init(first
);
284 atomic_inc(&conf
->active_stripes
);
289 static void shrink_buffers(struct stripe_head
*sh
)
293 int num
= sh
->raid_conf
->pool_size
;
295 for (i
= 0; i
< num
; i
++) {
299 sh
->dev
[i
].page
= NULL
;
304 static int grow_buffers(struct stripe_head
*sh
)
307 int num
= sh
->raid_conf
->pool_size
;
309 for (i
= 0; i
< num
; i
++) {
312 if (!(page
= alloc_page(GFP_KERNEL
))) {
315 sh
->dev
[i
].page
= page
;
320 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
321 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
322 struct stripe_head
*sh
);
324 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
326 struct r5conf
*conf
= sh
->raid_conf
;
329 BUG_ON(atomic_read(&sh
->count
) != 0);
330 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
331 BUG_ON(stripe_operations_active(sh
));
333 pr_debug("init_stripe called, stripe %llu\n",
334 (unsigned long long)sh
->sector
);
338 sh
->generation
= conf
->generation
- previous
;
339 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
341 stripe_set_idx(sector
, conf
, previous
, sh
);
345 for (i
= sh
->disks
; i
--; ) {
346 struct r5dev
*dev
= &sh
->dev
[i
];
348 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
349 test_bit(R5_LOCKED
, &dev
->flags
)) {
350 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
351 (unsigned long long)sh
->sector
, i
, dev
->toread
,
352 dev
->read
, dev
->towrite
, dev
->written
,
353 test_bit(R5_LOCKED
, &dev
->flags
));
357 raid5_build_block(sh
, i
, previous
);
359 insert_hash(conf
, sh
);
362 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
365 struct stripe_head
*sh
;
367 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
368 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
369 if (sh
->sector
== sector
&& sh
->generation
== generation
)
371 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
376 * Need to check if array has failed when deciding whether to:
378 * - remove non-faulty devices
381 * This determination is simple when no reshape is happening.
382 * However if there is a reshape, we need to carefully check
383 * both the before and after sections.
384 * This is because some failed devices may only affect one
385 * of the two sections, and some non-in_sync devices may
386 * be insync in the section most affected by failed devices.
388 static int calc_degraded(struct r5conf
*conf
)
390 int degraded
, degraded2
;
395 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
396 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
397 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
398 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
399 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
401 else if (test_bit(In_sync
, &rdev
->flags
))
404 /* not in-sync or faulty.
405 * If the reshape increases the number of devices,
406 * this is being recovered by the reshape, so
407 * this 'previous' section is not in_sync.
408 * If the number of devices is being reduced however,
409 * the device can only be part of the array if
410 * we are reverting a reshape, so this section will
413 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
417 if (conf
->raid_disks
== conf
->previous_raid_disks
)
421 for (i
= 0; i
< conf
->raid_disks
; i
++) {
422 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
423 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
424 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
425 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
427 else if (test_bit(In_sync
, &rdev
->flags
))
430 /* not in-sync or faulty.
431 * If reshape increases the number of devices, this
432 * section has already been recovered, else it
433 * almost certainly hasn't.
435 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
439 if (degraded2
> degraded
)
444 static int has_failed(struct r5conf
*conf
)
448 if (conf
->mddev
->reshape_position
== MaxSector
)
449 return conf
->mddev
->degraded
> conf
->max_degraded
;
451 degraded
= calc_degraded(conf
);
452 if (degraded
> conf
->max_degraded
)
457 static struct stripe_head
*
458 get_active_stripe(struct r5conf
*conf
, sector_t sector
,
459 int previous
, int noblock
, int noquiesce
)
461 struct stripe_head
*sh
;
463 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
465 spin_lock_irq(&conf
->device_lock
);
468 wait_event_lock_irq(conf
->wait_for_stripe
,
469 conf
->quiesce
== 0 || noquiesce
,
471 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
473 if (!conf
->inactive_blocked
)
474 sh
= get_free_stripe(conf
);
475 if (noblock
&& sh
== NULL
)
478 conf
->inactive_blocked
= 1;
479 wait_event_lock_irq(conf
->wait_for_stripe
,
480 !list_empty(&conf
->inactive_list
) &&
481 (atomic_read(&conf
->active_stripes
)
482 < (conf
->max_nr_stripes
*3/4)
483 || !conf
->inactive_blocked
),
485 conf
->inactive_blocked
= 0;
487 init_stripe(sh
, sector
, previous
);
489 if (atomic_read(&sh
->count
)) {
490 BUG_ON(!list_empty(&sh
->lru
)
491 && !test_bit(STRIPE_EXPANDING
, &sh
->state
)
492 && !test_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
));
494 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
495 atomic_inc(&conf
->active_stripes
);
496 if (list_empty(&sh
->lru
) &&
497 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
499 list_del_init(&sh
->lru
);
502 } while (sh
== NULL
);
505 atomic_inc(&sh
->count
);
507 spin_unlock_irq(&conf
->device_lock
);
511 /* Determine if 'data_offset' or 'new_data_offset' should be used
512 * in this stripe_head.
514 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
516 sector_t progress
= conf
->reshape_progress
;
517 /* Need a memory barrier to make sure we see the value
518 * of conf->generation, or ->data_offset that was set before
519 * reshape_progress was updated.
522 if (progress
== MaxSector
)
524 if (sh
->generation
== conf
->generation
- 1)
526 /* We are in a reshape, and this is a new-generation stripe,
527 * so use new_data_offset.
533 raid5_end_read_request(struct bio
*bi
, int error
);
535 raid5_end_write_request(struct bio
*bi
, int error
);
537 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
539 struct r5conf
*conf
= sh
->raid_conf
;
540 int i
, disks
= sh
->disks
;
544 for (i
= disks
; i
--; ) {
546 int replace_only
= 0;
547 struct bio
*bi
, *rbi
;
548 struct md_rdev
*rdev
, *rrdev
= NULL
;
549 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
550 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
554 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
556 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
558 else if (test_and_clear_bit(R5_WantReplace
,
559 &sh
->dev
[i
].flags
)) {
564 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
567 bi
= &sh
->dev
[i
].req
;
568 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
573 bi
->bi_end_io
= raid5_end_write_request
;
574 rbi
->bi_end_io
= raid5_end_write_request
;
576 bi
->bi_end_io
= raid5_end_read_request
;
579 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
580 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
581 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
590 /* We raced and saw duplicates */
593 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
) && rrdev
)
598 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
601 atomic_inc(&rdev
->nr_pending
);
602 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
605 atomic_inc(&rrdev
->nr_pending
);
608 /* We have already checked bad blocks for reads. Now
609 * need to check for writes. We never accept write errors
610 * on the replacement, so we don't to check rrdev.
612 while ((rw
& WRITE
) && rdev
&&
613 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
616 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
617 &first_bad
, &bad_sectors
);
622 set_bit(BlockedBadBlocks
, &rdev
->flags
);
623 if (!conf
->mddev
->external
&&
624 conf
->mddev
->flags
) {
625 /* It is very unlikely, but we might
626 * still need to write out the
627 * bad block log - better give it
629 md_check_recovery(conf
->mddev
);
632 * Because md_wait_for_blocked_rdev
633 * will dec nr_pending, we must
634 * increment it first.
636 atomic_inc(&rdev
->nr_pending
);
637 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
639 /* Acknowledged bad block - skip the write */
640 rdev_dec_pending(rdev
, conf
->mddev
);
646 if (s
->syncing
|| s
->expanding
|| s
->expanded
648 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
650 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
652 bi
->bi_bdev
= rdev
->bdev
;
653 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
654 __func__
, (unsigned long long)sh
->sector
,
656 atomic_inc(&sh
->count
);
657 if (use_new_offset(conf
, sh
))
658 bi
->bi_sector
= (sh
->sector
659 + rdev
->new_data_offset
);
661 bi
->bi_sector
= (sh
->sector
662 + rdev
->data_offset
);
663 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
664 bi
->bi_rw
|= REQ_FLUSH
;
666 bi
->bi_flags
= 1 << BIO_UPTODATE
;
668 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
669 bi
->bi_io_vec
[0].bv_offset
= 0;
670 bi
->bi_size
= STRIPE_SIZE
;
673 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
674 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
675 bi
, disk_devt(conf
->mddev
->gendisk
),
677 generic_make_request(bi
);
680 if (s
->syncing
|| s
->expanding
|| s
->expanded
682 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
684 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
686 rbi
->bi_bdev
= rrdev
->bdev
;
687 pr_debug("%s: for %llu schedule op %ld on "
688 "replacement disc %d\n",
689 __func__
, (unsigned long long)sh
->sector
,
691 atomic_inc(&sh
->count
);
692 if (use_new_offset(conf
, sh
))
693 rbi
->bi_sector
= (sh
->sector
694 + rrdev
->new_data_offset
);
696 rbi
->bi_sector
= (sh
->sector
697 + rrdev
->data_offset
);
698 rbi
->bi_flags
= 1 << BIO_UPTODATE
;
700 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
701 rbi
->bi_io_vec
[0].bv_offset
= 0;
702 rbi
->bi_size
= STRIPE_SIZE
;
704 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
705 rbi
, disk_devt(conf
->mddev
->gendisk
),
707 generic_make_request(rbi
);
709 if (!rdev
&& !rrdev
) {
711 set_bit(STRIPE_DEGRADED
, &sh
->state
);
712 pr_debug("skip op %ld on disc %d for sector %llu\n",
713 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
714 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
715 set_bit(STRIPE_HANDLE
, &sh
->state
);
720 static struct dma_async_tx_descriptor
*
721 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
722 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
725 struct page
*bio_page
;
728 struct async_submit_ctl submit
;
729 enum async_tx_flags flags
= 0;
731 if (bio
->bi_sector
>= sector
)
732 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
734 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
737 flags
|= ASYNC_TX_FENCE
;
738 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
740 bio_for_each_segment(bvl
, bio
, i
) {
741 int len
= bvl
->bv_len
;
745 if (page_offset
< 0) {
746 b_offset
= -page_offset
;
747 page_offset
+= b_offset
;
751 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
752 clen
= STRIPE_SIZE
- page_offset
;
757 b_offset
+= bvl
->bv_offset
;
758 bio_page
= bvl
->bv_page
;
760 tx
= async_memcpy(page
, bio_page
, page_offset
,
761 b_offset
, clen
, &submit
);
763 tx
= async_memcpy(bio_page
, page
, b_offset
,
764 page_offset
, clen
, &submit
);
766 /* chain the operations */
767 submit
.depend_tx
= tx
;
769 if (clen
< len
) /* hit end of page */
777 static void ops_complete_biofill(void *stripe_head_ref
)
779 struct stripe_head
*sh
= stripe_head_ref
;
780 struct bio
*return_bi
= NULL
;
783 pr_debug("%s: stripe %llu\n", __func__
,
784 (unsigned long long)sh
->sector
);
786 /* clear completed biofills */
787 for (i
= sh
->disks
; i
--; ) {
788 struct r5dev
*dev
= &sh
->dev
[i
];
790 /* acknowledge completion of a biofill operation */
791 /* and check if we need to reply to a read request,
792 * new R5_Wantfill requests are held off until
793 * !STRIPE_BIOFILL_RUN
795 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
796 struct bio
*rbi
, *rbi2
;
801 while (rbi
&& rbi
->bi_sector
<
802 dev
->sector
+ STRIPE_SECTORS
) {
803 rbi2
= r5_next_bio(rbi
, dev
->sector
);
804 if (!raid5_dec_bi_active_stripes(rbi
)) {
805 rbi
->bi_next
= return_bi
;
812 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
814 return_io(return_bi
);
816 set_bit(STRIPE_HANDLE
, &sh
->state
);
820 static void ops_run_biofill(struct stripe_head
*sh
)
822 struct dma_async_tx_descriptor
*tx
= NULL
;
823 struct async_submit_ctl submit
;
826 pr_debug("%s: stripe %llu\n", __func__
,
827 (unsigned long long)sh
->sector
);
829 for (i
= sh
->disks
; i
--; ) {
830 struct r5dev
*dev
= &sh
->dev
[i
];
831 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
833 spin_lock_irq(&sh
->stripe_lock
);
834 dev
->read
= rbi
= dev
->toread
;
836 spin_unlock_irq(&sh
->stripe_lock
);
837 while (rbi
&& rbi
->bi_sector
<
838 dev
->sector
+ STRIPE_SECTORS
) {
839 tx
= async_copy_data(0, rbi
, dev
->page
,
841 rbi
= r5_next_bio(rbi
, dev
->sector
);
846 atomic_inc(&sh
->count
);
847 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
848 async_trigger_callback(&submit
);
851 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
858 tgt
= &sh
->dev
[target
];
859 set_bit(R5_UPTODATE
, &tgt
->flags
);
860 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
861 clear_bit(R5_Wantcompute
, &tgt
->flags
);
864 static void ops_complete_compute(void *stripe_head_ref
)
866 struct stripe_head
*sh
= stripe_head_ref
;
868 pr_debug("%s: stripe %llu\n", __func__
,
869 (unsigned long long)sh
->sector
);
871 /* mark the computed target(s) as uptodate */
872 mark_target_uptodate(sh
, sh
->ops
.target
);
873 mark_target_uptodate(sh
, sh
->ops
.target2
);
875 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
876 if (sh
->check_state
== check_state_compute_run
)
877 sh
->check_state
= check_state_compute_result
;
878 set_bit(STRIPE_HANDLE
, &sh
->state
);
882 /* return a pointer to the address conversion region of the scribble buffer */
883 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
884 struct raid5_percpu
*percpu
)
886 return percpu
->scribble
+ sizeof(struct page
*) * (sh
->disks
+ 2);
889 static struct dma_async_tx_descriptor
*
890 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
892 int disks
= sh
->disks
;
893 struct page
**xor_srcs
= percpu
->scribble
;
894 int target
= sh
->ops
.target
;
895 struct r5dev
*tgt
= &sh
->dev
[target
];
896 struct page
*xor_dest
= tgt
->page
;
898 struct dma_async_tx_descriptor
*tx
;
899 struct async_submit_ctl submit
;
902 pr_debug("%s: stripe %llu block: %d\n",
903 __func__
, (unsigned long long)sh
->sector
, target
);
904 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
906 for (i
= disks
; i
--; )
908 xor_srcs
[count
++] = sh
->dev
[i
].page
;
910 atomic_inc(&sh
->count
);
912 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
913 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
));
914 if (unlikely(count
== 1))
915 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
917 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
922 /* set_syndrome_sources - populate source buffers for gen_syndrome
923 * @srcs - (struct page *) array of size sh->disks
924 * @sh - stripe_head to parse
926 * Populates srcs in proper layout order for the stripe and returns the
927 * 'count' of sources to be used in a call to async_gen_syndrome. The P
928 * destination buffer is recorded in srcs[count] and the Q destination
929 * is recorded in srcs[count+1]].
931 static int set_syndrome_sources(struct page
**srcs
, struct stripe_head
*sh
)
933 int disks
= sh
->disks
;
934 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
935 int d0_idx
= raid6_d0(sh
);
939 for (i
= 0; i
< disks
; i
++)
945 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
947 srcs
[slot
] = sh
->dev
[i
].page
;
948 i
= raid6_next_disk(i
, disks
);
949 } while (i
!= d0_idx
);
951 return syndrome_disks
;
954 static struct dma_async_tx_descriptor
*
955 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
957 int disks
= sh
->disks
;
958 struct page
**blocks
= percpu
->scribble
;
960 int qd_idx
= sh
->qd_idx
;
961 struct dma_async_tx_descriptor
*tx
;
962 struct async_submit_ctl submit
;
968 if (sh
->ops
.target
< 0)
969 target
= sh
->ops
.target2
;
970 else if (sh
->ops
.target2
< 0)
971 target
= sh
->ops
.target
;
973 /* we should only have one valid target */
976 pr_debug("%s: stripe %llu block: %d\n",
977 __func__
, (unsigned long long)sh
->sector
, target
);
979 tgt
= &sh
->dev
[target
];
980 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
983 atomic_inc(&sh
->count
);
985 if (target
== qd_idx
) {
986 count
= set_syndrome_sources(blocks
, sh
);
987 blocks
[count
] = NULL
; /* regenerating p is not necessary */
988 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
989 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
990 ops_complete_compute
, sh
,
991 to_addr_conv(sh
, percpu
));
992 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
994 /* Compute any data- or p-drive using XOR */
996 for (i
= disks
; i
-- ; ) {
997 if (i
== target
|| i
== qd_idx
)
999 blocks
[count
++] = sh
->dev
[i
].page
;
1002 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1003 NULL
, ops_complete_compute
, sh
,
1004 to_addr_conv(sh
, percpu
));
1005 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1011 static struct dma_async_tx_descriptor
*
1012 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1014 int i
, count
, disks
= sh
->disks
;
1015 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1016 int d0_idx
= raid6_d0(sh
);
1017 int faila
= -1, failb
= -1;
1018 int target
= sh
->ops
.target
;
1019 int target2
= sh
->ops
.target2
;
1020 struct r5dev
*tgt
= &sh
->dev
[target
];
1021 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1022 struct dma_async_tx_descriptor
*tx
;
1023 struct page
**blocks
= percpu
->scribble
;
1024 struct async_submit_ctl submit
;
1026 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1027 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1028 BUG_ON(target
< 0 || target2
< 0);
1029 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1030 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1032 /* we need to open-code set_syndrome_sources to handle the
1033 * slot number conversion for 'faila' and 'failb'
1035 for (i
= 0; i
< disks
; i
++)
1040 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1042 blocks
[slot
] = sh
->dev
[i
].page
;
1048 i
= raid6_next_disk(i
, disks
);
1049 } while (i
!= d0_idx
);
1051 BUG_ON(faila
== failb
);
1054 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1055 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1057 atomic_inc(&sh
->count
);
1059 if (failb
== syndrome_disks
+1) {
1060 /* Q disk is one of the missing disks */
1061 if (faila
== syndrome_disks
) {
1062 /* Missing P+Q, just recompute */
1063 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1064 ops_complete_compute
, sh
,
1065 to_addr_conv(sh
, percpu
));
1066 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1067 STRIPE_SIZE
, &submit
);
1071 int qd_idx
= sh
->qd_idx
;
1073 /* Missing D+Q: recompute D from P, then recompute Q */
1074 if (target
== qd_idx
)
1075 data_target
= target2
;
1077 data_target
= target
;
1080 for (i
= disks
; i
-- ; ) {
1081 if (i
== data_target
|| i
== qd_idx
)
1083 blocks
[count
++] = sh
->dev
[i
].page
;
1085 dest
= sh
->dev
[data_target
].page
;
1086 init_async_submit(&submit
,
1087 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1089 to_addr_conv(sh
, percpu
));
1090 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1093 count
= set_syndrome_sources(blocks
, sh
);
1094 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1095 ops_complete_compute
, sh
,
1096 to_addr_conv(sh
, percpu
));
1097 return async_gen_syndrome(blocks
, 0, count
+2,
1098 STRIPE_SIZE
, &submit
);
1101 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1102 ops_complete_compute
, sh
,
1103 to_addr_conv(sh
, percpu
));
1104 if (failb
== syndrome_disks
) {
1105 /* We're missing D+P. */
1106 return async_raid6_datap_recov(syndrome_disks
+2,
1110 /* We're missing D+D. */
1111 return async_raid6_2data_recov(syndrome_disks
+2,
1112 STRIPE_SIZE
, faila
, failb
,
1119 static void ops_complete_prexor(void *stripe_head_ref
)
1121 struct stripe_head
*sh
= stripe_head_ref
;
1123 pr_debug("%s: stripe %llu\n", __func__
,
1124 (unsigned long long)sh
->sector
);
1127 static struct dma_async_tx_descriptor
*
1128 ops_run_prexor(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1129 struct dma_async_tx_descriptor
*tx
)
1131 int disks
= sh
->disks
;
1132 struct page
**xor_srcs
= percpu
->scribble
;
1133 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1134 struct async_submit_ctl submit
;
1136 /* existing parity data subtracted */
1137 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1139 pr_debug("%s: stripe %llu\n", __func__
,
1140 (unsigned long long)sh
->sector
);
1142 for (i
= disks
; i
--; ) {
1143 struct r5dev
*dev
= &sh
->dev
[i
];
1144 /* Only process blocks that are known to be uptodate */
1145 if (test_bit(R5_Wantdrain
, &dev
->flags
))
1146 xor_srcs
[count
++] = dev
->page
;
1149 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1150 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
));
1151 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1156 static struct dma_async_tx_descriptor
*
1157 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1159 int disks
= sh
->disks
;
1162 pr_debug("%s: stripe %llu\n", __func__
,
1163 (unsigned long long)sh
->sector
);
1165 for (i
= disks
; i
--; ) {
1166 struct r5dev
*dev
= &sh
->dev
[i
];
1169 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
1172 spin_lock_irq(&sh
->stripe_lock
);
1173 chosen
= dev
->towrite
;
1174 dev
->towrite
= NULL
;
1175 BUG_ON(dev
->written
);
1176 wbi
= dev
->written
= chosen
;
1177 spin_unlock_irq(&sh
->stripe_lock
);
1179 while (wbi
&& wbi
->bi_sector
<
1180 dev
->sector
+ STRIPE_SECTORS
) {
1181 if (wbi
->bi_rw
& REQ_FUA
)
1182 set_bit(R5_WantFUA
, &dev
->flags
);
1183 if (wbi
->bi_rw
& REQ_SYNC
)
1184 set_bit(R5_SyncIO
, &dev
->flags
);
1185 if (wbi
->bi_rw
& REQ_DISCARD
)
1186 set_bit(R5_Discard
, &dev
->flags
);
1188 tx
= async_copy_data(1, wbi
, dev
->page
,
1190 wbi
= r5_next_bio(wbi
, dev
->sector
);
1198 static void ops_complete_reconstruct(void *stripe_head_ref
)
1200 struct stripe_head
*sh
= stripe_head_ref
;
1201 int disks
= sh
->disks
;
1202 int pd_idx
= sh
->pd_idx
;
1203 int qd_idx
= sh
->qd_idx
;
1205 bool fua
= false, sync
= false, discard
= false;
1207 pr_debug("%s: stripe %llu\n", __func__
,
1208 (unsigned long long)sh
->sector
);
1210 for (i
= disks
; i
--; ) {
1211 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1212 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1213 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1216 for (i
= disks
; i
--; ) {
1217 struct r5dev
*dev
= &sh
->dev
[i
];
1219 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1221 set_bit(R5_UPTODATE
, &dev
->flags
);
1223 set_bit(R5_WantFUA
, &dev
->flags
);
1225 set_bit(R5_SyncIO
, &dev
->flags
);
1229 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1230 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1231 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1232 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1234 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1235 sh
->reconstruct_state
= reconstruct_state_result
;
1238 set_bit(STRIPE_HANDLE
, &sh
->state
);
1243 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1244 struct dma_async_tx_descriptor
*tx
)
1246 int disks
= sh
->disks
;
1247 struct page
**xor_srcs
= percpu
->scribble
;
1248 struct async_submit_ctl submit
;
1249 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1250 struct page
*xor_dest
;
1252 unsigned long flags
;
1254 pr_debug("%s: stripe %llu\n", __func__
,
1255 (unsigned long long)sh
->sector
);
1257 for (i
= 0; i
< sh
->disks
; i
++) {
1260 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1263 if (i
>= sh
->disks
) {
1264 atomic_inc(&sh
->count
);
1265 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1266 ops_complete_reconstruct(sh
);
1269 /* check if prexor is active which means only process blocks
1270 * that are part of a read-modify-write (written)
1272 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1274 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1275 for (i
= disks
; i
--; ) {
1276 struct r5dev
*dev
= &sh
->dev
[i
];
1278 xor_srcs
[count
++] = dev
->page
;
1281 xor_dest
= sh
->dev
[pd_idx
].page
;
1282 for (i
= disks
; i
--; ) {
1283 struct r5dev
*dev
= &sh
->dev
[i
];
1285 xor_srcs
[count
++] = dev
->page
;
1289 /* 1/ if we prexor'd then the dest is reused as a source
1290 * 2/ if we did not prexor then we are redoing the parity
1291 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1292 * for the synchronous xor case
1294 flags
= ASYNC_TX_ACK
|
1295 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1297 atomic_inc(&sh
->count
);
1299 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, sh
,
1300 to_addr_conv(sh
, percpu
));
1301 if (unlikely(count
== 1))
1302 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1304 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1308 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1309 struct dma_async_tx_descriptor
*tx
)
1311 struct async_submit_ctl submit
;
1312 struct page
**blocks
= percpu
->scribble
;
1315 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1317 for (i
= 0; i
< sh
->disks
; i
++) {
1318 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1320 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1323 if (i
>= sh
->disks
) {
1324 atomic_inc(&sh
->count
);
1325 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1326 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1327 ops_complete_reconstruct(sh
);
1331 count
= set_syndrome_sources(blocks
, sh
);
1333 atomic_inc(&sh
->count
);
1335 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_reconstruct
,
1336 sh
, to_addr_conv(sh
, percpu
));
1337 async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1340 static void ops_complete_check(void *stripe_head_ref
)
1342 struct stripe_head
*sh
= stripe_head_ref
;
1344 pr_debug("%s: stripe %llu\n", __func__
,
1345 (unsigned long long)sh
->sector
);
1347 sh
->check_state
= check_state_check_result
;
1348 set_bit(STRIPE_HANDLE
, &sh
->state
);
1352 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1354 int disks
= sh
->disks
;
1355 int pd_idx
= sh
->pd_idx
;
1356 int qd_idx
= sh
->qd_idx
;
1357 struct page
*xor_dest
;
1358 struct page
**xor_srcs
= percpu
->scribble
;
1359 struct dma_async_tx_descriptor
*tx
;
1360 struct async_submit_ctl submit
;
1364 pr_debug("%s: stripe %llu\n", __func__
,
1365 (unsigned long long)sh
->sector
);
1368 xor_dest
= sh
->dev
[pd_idx
].page
;
1369 xor_srcs
[count
++] = xor_dest
;
1370 for (i
= disks
; i
--; ) {
1371 if (i
== pd_idx
|| i
== qd_idx
)
1373 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1376 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1377 to_addr_conv(sh
, percpu
));
1378 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1379 &sh
->ops
.zero_sum_result
, &submit
);
1381 atomic_inc(&sh
->count
);
1382 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1383 tx
= async_trigger_callback(&submit
);
1386 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1388 struct page
**srcs
= percpu
->scribble
;
1389 struct async_submit_ctl submit
;
1392 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1393 (unsigned long long)sh
->sector
, checkp
);
1395 count
= set_syndrome_sources(srcs
, sh
);
1399 atomic_inc(&sh
->count
);
1400 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1401 sh
, to_addr_conv(sh
, percpu
));
1402 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1403 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1406 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1408 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1409 struct dma_async_tx_descriptor
*tx
= NULL
;
1410 struct r5conf
*conf
= sh
->raid_conf
;
1411 int level
= conf
->level
;
1412 struct raid5_percpu
*percpu
;
1416 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1417 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1418 ops_run_biofill(sh
);
1422 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1424 tx
= ops_run_compute5(sh
, percpu
);
1426 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1427 tx
= ops_run_compute6_1(sh
, percpu
);
1429 tx
= ops_run_compute6_2(sh
, percpu
);
1431 /* terminate the chain if reconstruct is not set to be run */
1432 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1436 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
1437 tx
= ops_run_prexor(sh
, percpu
, tx
);
1439 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1440 tx
= ops_run_biodrain(sh
, tx
);
1444 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1446 ops_run_reconstruct5(sh
, percpu
, tx
);
1448 ops_run_reconstruct6(sh
, percpu
, tx
);
1451 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1452 if (sh
->check_state
== check_state_run
)
1453 ops_run_check_p(sh
, percpu
);
1454 else if (sh
->check_state
== check_state_run_q
)
1455 ops_run_check_pq(sh
, percpu
, 0);
1456 else if (sh
->check_state
== check_state_run_pq
)
1457 ops_run_check_pq(sh
, percpu
, 1);
1463 for (i
= disks
; i
--; ) {
1464 struct r5dev
*dev
= &sh
->dev
[i
];
1465 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1466 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1471 static int grow_one_stripe(struct r5conf
*conf
)
1473 struct stripe_head
*sh
;
1474 sh
= kmem_cache_zalloc(conf
->slab_cache
, GFP_KERNEL
);
1478 sh
->raid_conf
= conf
;
1480 spin_lock_init(&sh
->stripe_lock
);
1482 if (grow_buffers(sh
)) {
1484 kmem_cache_free(conf
->slab_cache
, sh
);
1487 /* we just created an active stripe so... */
1488 atomic_set(&sh
->count
, 1);
1489 atomic_inc(&conf
->active_stripes
);
1490 INIT_LIST_HEAD(&sh
->lru
);
1495 static int grow_stripes(struct r5conf
*conf
, int num
)
1497 struct kmem_cache
*sc
;
1498 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
1500 if (conf
->mddev
->gendisk
)
1501 sprintf(conf
->cache_name
[0],
1502 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
1504 sprintf(conf
->cache_name
[0],
1505 "raid%d-%p", conf
->level
, conf
->mddev
);
1506 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
1508 conf
->active_name
= 0;
1509 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
1510 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
1514 conf
->slab_cache
= sc
;
1515 conf
->pool_size
= devs
;
1517 if (!grow_one_stripe(conf
))
1523 * scribble_len - return the required size of the scribble region
1524 * @num - total number of disks in the array
1526 * The size must be enough to contain:
1527 * 1/ a struct page pointer for each device in the array +2
1528 * 2/ room to convert each entry in (1) to its corresponding dma
1529 * (dma_map_page()) or page (page_address()) address.
1531 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1532 * calculate over all devices (not just the data blocks), using zeros in place
1533 * of the P and Q blocks.
1535 static size_t scribble_len(int num
)
1539 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
1544 static int resize_stripes(struct r5conf
*conf
, int newsize
)
1546 /* Make all the stripes able to hold 'newsize' devices.
1547 * New slots in each stripe get 'page' set to a new page.
1549 * This happens in stages:
1550 * 1/ create a new kmem_cache and allocate the required number of
1552 * 2/ gather all the old stripe_heads and transfer the pages across
1553 * to the new stripe_heads. This will have the side effect of
1554 * freezing the array as once all stripe_heads have been collected,
1555 * no IO will be possible. Old stripe heads are freed once their
1556 * pages have been transferred over, and the old kmem_cache is
1557 * freed when all stripes are done.
1558 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1559 * we simple return a failre status - no need to clean anything up.
1560 * 4/ allocate new pages for the new slots in the new stripe_heads.
1561 * If this fails, we don't bother trying the shrink the
1562 * stripe_heads down again, we just leave them as they are.
1563 * As each stripe_head is processed the new one is released into
1566 * Once step2 is started, we cannot afford to wait for a write,
1567 * so we use GFP_NOIO allocations.
1569 struct stripe_head
*osh
, *nsh
;
1570 LIST_HEAD(newstripes
);
1571 struct disk_info
*ndisks
;
1574 struct kmem_cache
*sc
;
1577 if (newsize
<= conf
->pool_size
)
1578 return 0; /* never bother to shrink */
1580 err
= md_allow_write(conf
->mddev
);
1585 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
1586 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
1591 for (i
= conf
->max_nr_stripes
; i
; i
--) {
1592 nsh
= kmem_cache_zalloc(sc
, GFP_KERNEL
);
1596 nsh
->raid_conf
= conf
;
1597 spin_lock_init(&nsh
->stripe_lock
);
1599 list_add(&nsh
->lru
, &newstripes
);
1602 /* didn't get enough, give up */
1603 while (!list_empty(&newstripes
)) {
1604 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1605 list_del(&nsh
->lru
);
1606 kmem_cache_free(sc
, nsh
);
1608 kmem_cache_destroy(sc
);
1611 /* Step 2 - Must use GFP_NOIO now.
1612 * OK, we have enough stripes, start collecting inactive
1613 * stripes and copying them over
1615 list_for_each_entry(nsh
, &newstripes
, lru
) {
1616 spin_lock_irq(&conf
->device_lock
);
1617 wait_event_lock_irq(conf
->wait_for_stripe
,
1618 !list_empty(&conf
->inactive_list
),
1620 osh
= get_free_stripe(conf
);
1621 spin_unlock_irq(&conf
->device_lock
);
1622 atomic_set(&nsh
->count
, 1);
1623 for(i
=0; i
<conf
->pool_size
; i
++)
1624 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1625 for( ; i
<newsize
; i
++)
1626 nsh
->dev
[i
].page
= NULL
;
1627 kmem_cache_free(conf
->slab_cache
, osh
);
1629 kmem_cache_destroy(conf
->slab_cache
);
1632 * At this point, we are holding all the stripes so the array
1633 * is completely stalled, so now is a good time to resize
1634 * conf->disks and the scribble region
1636 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1638 for (i
=0; i
<conf
->raid_disks
; i
++)
1639 ndisks
[i
] = conf
->disks
[i
];
1641 conf
->disks
= ndisks
;
1646 conf
->scribble_len
= scribble_len(newsize
);
1647 for_each_present_cpu(cpu
) {
1648 struct raid5_percpu
*percpu
;
1651 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1652 scribble
= kmalloc(conf
->scribble_len
, GFP_NOIO
);
1655 kfree(percpu
->scribble
);
1656 percpu
->scribble
= scribble
;
1664 /* Step 4, return new stripes to service */
1665 while(!list_empty(&newstripes
)) {
1666 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1667 list_del_init(&nsh
->lru
);
1669 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1670 if (nsh
->dev
[i
].page
== NULL
) {
1671 struct page
*p
= alloc_page(GFP_NOIO
);
1672 nsh
->dev
[i
].page
= p
;
1676 release_stripe(nsh
);
1678 /* critical section pass, GFP_NOIO no longer needed */
1680 conf
->slab_cache
= sc
;
1681 conf
->active_name
= 1-conf
->active_name
;
1682 conf
->pool_size
= newsize
;
1686 static int drop_one_stripe(struct r5conf
*conf
)
1688 struct stripe_head
*sh
;
1690 spin_lock_irq(&conf
->device_lock
);
1691 sh
= get_free_stripe(conf
);
1692 spin_unlock_irq(&conf
->device_lock
);
1695 BUG_ON(atomic_read(&sh
->count
));
1697 kmem_cache_free(conf
->slab_cache
, sh
);
1698 atomic_dec(&conf
->active_stripes
);
1702 static void shrink_stripes(struct r5conf
*conf
)
1704 while (drop_one_stripe(conf
))
1707 if (conf
->slab_cache
)
1708 kmem_cache_destroy(conf
->slab_cache
);
1709 conf
->slab_cache
= NULL
;
1712 static void raid5_end_read_request(struct bio
* bi
, int error
)
1714 struct stripe_head
*sh
= bi
->bi_private
;
1715 struct r5conf
*conf
= sh
->raid_conf
;
1716 int disks
= sh
->disks
, i
;
1717 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1718 char b
[BDEVNAME_SIZE
];
1719 struct md_rdev
*rdev
= NULL
;
1722 for (i
=0 ; i
<disks
; i
++)
1723 if (bi
== &sh
->dev
[i
].req
)
1726 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1727 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1733 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1734 /* If replacement finished while this request was outstanding,
1735 * 'replacement' might be NULL already.
1736 * In that case it moved down to 'rdev'.
1737 * rdev is not removed until all requests are finished.
1739 rdev
= conf
->disks
[i
].replacement
;
1741 rdev
= conf
->disks
[i
].rdev
;
1743 if (use_new_offset(conf
, sh
))
1744 s
= sh
->sector
+ rdev
->new_data_offset
;
1746 s
= sh
->sector
+ rdev
->data_offset
;
1748 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1749 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1750 /* Note that this cannot happen on a
1751 * replacement device. We just fail those on
1756 "md/raid:%s: read error corrected"
1757 " (%lu sectors at %llu on %s)\n",
1758 mdname(conf
->mddev
), STRIPE_SECTORS
,
1759 (unsigned long long)s
,
1760 bdevname(rdev
->bdev
, b
));
1761 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1762 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1763 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1764 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
1765 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1767 if (atomic_read(&rdev
->read_errors
))
1768 atomic_set(&rdev
->read_errors
, 0);
1770 const char *bdn
= bdevname(rdev
->bdev
, b
);
1774 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1775 atomic_inc(&rdev
->read_errors
);
1776 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1779 "md/raid:%s: read error on replacement device "
1780 "(sector %llu on %s).\n",
1781 mdname(conf
->mddev
),
1782 (unsigned long long)s
,
1784 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
1788 "md/raid:%s: read error not correctable "
1789 "(sector %llu on %s).\n",
1790 mdname(conf
->mddev
),
1791 (unsigned long long)s
,
1793 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
1798 "md/raid:%s: read error NOT corrected!! "
1799 "(sector %llu on %s).\n",
1800 mdname(conf
->mddev
),
1801 (unsigned long long)s
,
1803 } else if (atomic_read(&rdev
->read_errors
)
1804 > conf
->max_nr_stripes
)
1806 "md/raid:%s: Too many read errors, failing device %s.\n",
1807 mdname(conf
->mddev
), bdn
);
1811 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
1812 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1813 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1815 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1817 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1818 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1820 && test_bit(In_sync
, &rdev
->flags
)
1821 && rdev_set_badblocks(
1822 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
1823 md_error(conf
->mddev
, rdev
);
1826 rdev_dec_pending(rdev
, conf
->mddev
);
1827 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1828 set_bit(STRIPE_HANDLE
, &sh
->state
);
1832 static void raid5_end_write_request(struct bio
*bi
, int error
)
1834 struct stripe_head
*sh
= bi
->bi_private
;
1835 struct r5conf
*conf
= sh
->raid_conf
;
1836 int disks
= sh
->disks
, i
;
1837 struct md_rdev
*uninitialized_var(rdev
);
1838 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1841 int replacement
= 0;
1843 for (i
= 0 ; i
< disks
; i
++) {
1844 if (bi
== &sh
->dev
[i
].req
) {
1845 rdev
= conf
->disks
[i
].rdev
;
1848 if (bi
== &sh
->dev
[i
].rreq
) {
1849 rdev
= conf
->disks
[i
].replacement
;
1853 /* rdev was removed and 'replacement'
1854 * replaced it. rdev is not removed
1855 * until all requests are finished.
1857 rdev
= conf
->disks
[i
].rdev
;
1861 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1862 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1871 md_error(conf
->mddev
, rdev
);
1872 else if (is_badblock(rdev
, sh
->sector
,
1874 &first_bad
, &bad_sectors
))
1875 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
1878 set_bit(WriteErrorSeen
, &rdev
->flags
);
1879 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
1880 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1881 set_bit(MD_RECOVERY_NEEDED
,
1882 &rdev
->mddev
->recovery
);
1883 } else if (is_badblock(rdev
, sh
->sector
,
1885 &first_bad
, &bad_sectors
))
1886 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
1888 rdev_dec_pending(rdev
, conf
->mddev
);
1890 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
1891 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1892 set_bit(STRIPE_HANDLE
, &sh
->state
);
1896 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
);
1898 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
1900 struct r5dev
*dev
= &sh
->dev
[i
];
1902 bio_init(&dev
->req
);
1903 dev
->req
.bi_io_vec
= &dev
->vec
;
1905 dev
->req
.bi_max_vecs
++;
1906 dev
->req
.bi_private
= sh
;
1907 dev
->vec
.bv_page
= dev
->page
;
1909 bio_init(&dev
->rreq
);
1910 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
1911 dev
->rreq
.bi_vcnt
++;
1912 dev
->rreq
.bi_max_vecs
++;
1913 dev
->rreq
.bi_private
= sh
;
1914 dev
->rvec
.bv_page
= dev
->page
;
1917 dev
->sector
= compute_blocknr(sh
, i
, previous
);
1920 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1922 char b
[BDEVNAME_SIZE
];
1923 struct r5conf
*conf
= mddev
->private;
1924 unsigned long flags
;
1925 pr_debug("raid456: error called\n");
1927 spin_lock_irqsave(&conf
->device_lock
, flags
);
1928 clear_bit(In_sync
, &rdev
->flags
);
1929 mddev
->degraded
= calc_degraded(conf
);
1930 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1931 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1933 set_bit(Blocked
, &rdev
->flags
);
1934 set_bit(Faulty
, &rdev
->flags
);
1935 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1937 "md/raid:%s: Disk failure on %s, disabling device.\n"
1938 "md/raid:%s: Operation continuing on %d devices.\n",
1940 bdevname(rdev
->bdev
, b
),
1942 conf
->raid_disks
- mddev
->degraded
);
1946 * Input: a 'big' sector number,
1947 * Output: index of the data and parity disk, and the sector # in them.
1949 static sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
1950 int previous
, int *dd_idx
,
1951 struct stripe_head
*sh
)
1953 sector_t stripe
, stripe2
;
1954 sector_t chunk_number
;
1955 unsigned int chunk_offset
;
1958 sector_t new_sector
;
1959 int algorithm
= previous
? conf
->prev_algo
1961 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
1962 : conf
->chunk_sectors
;
1963 int raid_disks
= previous
? conf
->previous_raid_disks
1965 int data_disks
= raid_disks
- conf
->max_degraded
;
1967 /* First compute the information on this sector */
1970 * Compute the chunk number and the sector offset inside the chunk
1972 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
1973 chunk_number
= r_sector
;
1976 * Compute the stripe number
1978 stripe
= chunk_number
;
1979 *dd_idx
= sector_div(stripe
, data_disks
);
1982 * Select the parity disk based on the user selected algorithm.
1984 pd_idx
= qd_idx
= -1;
1985 switch(conf
->level
) {
1987 pd_idx
= data_disks
;
1990 switch (algorithm
) {
1991 case ALGORITHM_LEFT_ASYMMETRIC
:
1992 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1993 if (*dd_idx
>= pd_idx
)
1996 case ALGORITHM_RIGHT_ASYMMETRIC
:
1997 pd_idx
= sector_div(stripe2
, raid_disks
);
1998 if (*dd_idx
>= pd_idx
)
2001 case ALGORITHM_LEFT_SYMMETRIC
:
2002 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2003 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2005 case ALGORITHM_RIGHT_SYMMETRIC
:
2006 pd_idx
= sector_div(stripe2
, raid_disks
);
2007 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2009 case ALGORITHM_PARITY_0
:
2013 case ALGORITHM_PARITY_N
:
2014 pd_idx
= data_disks
;
2022 switch (algorithm
) {
2023 case ALGORITHM_LEFT_ASYMMETRIC
:
2024 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2025 qd_idx
= pd_idx
+ 1;
2026 if (pd_idx
== raid_disks
-1) {
2027 (*dd_idx
)++; /* Q D D D P */
2029 } else if (*dd_idx
>= pd_idx
)
2030 (*dd_idx
) += 2; /* D D P Q D */
2032 case ALGORITHM_RIGHT_ASYMMETRIC
:
2033 pd_idx
= sector_div(stripe2
, raid_disks
);
2034 qd_idx
= pd_idx
+ 1;
2035 if (pd_idx
== raid_disks
-1) {
2036 (*dd_idx
)++; /* Q D D D P */
2038 } else if (*dd_idx
>= pd_idx
)
2039 (*dd_idx
) += 2; /* D D P Q D */
2041 case ALGORITHM_LEFT_SYMMETRIC
:
2042 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2043 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2044 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2046 case ALGORITHM_RIGHT_SYMMETRIC
:
2047 pd_idx
= sector_div(stripe2
, raid_disks
);
2048 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2049 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2052 case ALGORITHM_PARITY_0
:
2057 case ALGORITHM_PARITY_N
:
2058 pd_idx
= data_disks
;
2059 qd_idx
= data_disks
+ 1;
2062 case ALGORITHM_ROTATING_ZERO_RESTART
:
2063 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2064 * of blocks for computing Q is different.
2066 pd_idx
= sector_div(stripe2
, raid_disks
);
2067 qd_idx
= pd_idx
+ 1;
2068 if (pd_idx
== raid_disks
-1) {
2069 (*dd_idx
)++; /* Q D D D P */
2071 } else if (*dd_idx
>= pd_idx
)
2072 (*dd_idx
) += 2; /* D D P Q D */
2076 case ALGORITHM_ROTATING_N_RESTART
:
2077 /* Same a left_asymmetric, by first stripe is
2078 * D D D P Q rather than
2082 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2083 qd_idx
= pd_idx
+ 1;
2084 if (pd_idx
== raid_disks
-1) {
2085 (*dd_idx
)++; /* Q D D D P */
2087 } else if (*dd_idx
>= pd_idx
)
2088 (*dd_idx
) += 2; /* D D P Q D */
2092 case ALGORITHM_ROTATING_N_CONTINUE
:
2093 /* Same as left_symmetric but Q is before P */
2094 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2095 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2096 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2100 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2101 /* RAID5 left_asymmetric, with Q on last device */
2102 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2103 if (*dd_idx
>= pd_idx
)
2105 qd_idx
= raid_disks
- 1;
2108 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2109 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2110 if (*dd_idx
>= pd_idx
)
2112 qd_idx
= raid_disks
- 1;
2115 case ALGORITHM_LEFT_SYMMETRIC_6
:
2116 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2117 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2118 qd_idx
= raid_disks
- 1;
2121 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2122 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2123 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2124 qd_idx
= raid_disks
- 1;
2127 case ALGORITHM_PARITY_0_6
:
2130 qd_idx
= raid_disks
- 1;
2140 sh
->pd_idx
= pd_idx
;
2141 sh
->qd_idx
= qd_idx
;
2142 sh
->ddf_layout
= ddf_layout
;
2145 * Finally, compute the new sector number
2147 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2152 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2154 struct r5conf
*conf
= sh
->raid_conf
;
2155 int raid_disks
= sh
->disks
;
2156 int data_disks
= raid_disks
- conf
->max_degraded
;
2157 sector_t new_sector
= sh
->sector
, check
;
2158 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2159 : conf
->chunk_sectors
;
2160 int algorithm
= previous
? conf
->prev_algo
2164 sector_t chunk_number
;
2165 int dummy1
, dd_idx
= i
;
2167 struct stripe_head sh2
;
2170 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2171 stripe
= new_sector
;
2173 if (i
== sh
->pd_idx
)
2175 switch(conf
->level
) {
2178 switch (algorithm
) {
2179 case ALGORITHM_LEFT_ASYMMETRIC
:
2180 case ALGORITHM_RIGHT_ASYMMETRIC
:
2184 case ALGORITHM_LEFT_SYMMETRIC
:
2185 case ALGORITHM_RIGHT_SYMMETRIC
:
2188 i
-= (sh
->pd_idx
+ 1);
2190 case ALGORITHM_PARITY_0
:
2193 case ALGORITHM_PARITY_N
:
2200 if (i
== sh
->qd_idx
)
2201 return 0; /* It is the Q disk */
2202 switch (algorithm
) {
2203 case ALGORITHM_LEFT_ASYMMETRIC
:
2204 case ALGORITHM_RIGHT_ASYMMETRIC
:
2205 case ALGORITHM_ROTATING_ZERO_RESTART
:
2206 case ALGORITHM_ROTATING_N_RESTART
:
2207 if (sh
->pd_idx
== raid_disks
-1)
2208 i
--; /* Q D D D P */
2209 else if (i
> sh
->pd_idx
)
2210 i
-= 2; /* D D P Q D */
2212 case ALGORITHM_LEFT_SYMMETRIC
:
2213 case ALGORITHM_RIGHT_SYMMETRIC
:
2214 if (sh
->pd_idx
== raid_disks
-1)
2215 i
--; /* Q D D D P */
2220 i
-= (sh
->pd_idx
+ 2);
2223 case ALGORITHM_PARITY_0
:
2226 case ALGORITHM_PARITY_N
:
2228 case ALGORITHM_ROTATING_N_CONTINUE
:
2229 /* Like left_symmetric, but P is before Q */
2230 if (sh
->pd_idx
== 0)
2231 i
--; /* P D D D Q */
2236 i
-= (sh
->pd_idx
+ 1);
2239 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2240 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2244 case ALGORITHM_LEFT_SYMMETRIC_6
:
2245 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2247 i
+= data_disks
+ 1;
2248 i
-= (sh
->pd_idx
+ 1);
2250 case ALGORITHM_PARITY_0_6
:
2259 chunk_number
= stripe
* data_disks
+ i
;
2260 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2262 check
= raid5_compute_sector(conf
, r_sector
,
2263 previous
, &dummy1
, &sh2
);
2264 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2265 || sh2
.qd_idx
!= sh
->qd_idx
) {
2266 printk(KERN_ERR
"md/raid:%s: compute_blocknr: map not correct\n",
2267 mdname(conf
->mddev
));
2275 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2276 int rcw
, int expand
)
2278 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
2279 struct r5conf
*conf
= sh
->raid_conf
;
2280 int level
= conf
->level
;
2283 /* if we are not expanding this is a proper write request, and
2284 * there will be bios with new data to be drained into the
2288 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2289 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2291 sh
->reconstruct_state
= reconstruct_state_run
;
2293 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2295 for (i
= disks
; i
--; ) {
2296 struct r5dev
*dev
= &sh
->dev
[i
];
2299 set_bit(R5_LOCKED
, &dev
->flags
);
2300 set_bit(R5_Wantdrain
, &dev
->flags
);
2302 clear_bit(R5_UPTODATE
, &dev
->flags
);
2306 if (s
->locked
+ conf
->max_degraded
== disks
)
2307 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2308 atomic_inc(&conf
->pending_full_writes
);
2311 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2312 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2314 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2315 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2316 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2317 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2319 for (i
= disks
; i
--; ) {
2320 struct r5dev
*dev
= &sh
->dev
[i
];
2325 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2326 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2327 set_bit(R5_Wantdrain
, &dev
->flags
);
2328 set_bit(R5_LOCKED
, &dev
->flags
);
2329 clear_bit(R5_UPTODATE
, &dev
->flags
);
2335 /* keep the parity disk(s) locked while asynchronous operations
2338 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2339 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2343 int qd_idx
= sh
->qd_idx
;
2344 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2346 set_bit(R5_LOCKED
, &dev
->flags
);
2347 clear_bit(R5_UPTODATE
, &dev
->flags
);
2351 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2352 __func__
, (unsigned long long)sh
->sector
,
2353 s
->locked
, s
->ops_request
);
2357 * Each stripe/dev can have one or more bion attached.
2358 * toread/towrite point to the first in a chain.
2359 * The bi_next chain must be in order.
2361 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
2364 struct r5conf
*conf
= sh
->raid_conf
;
2367 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2368 (unsigned long long)bi
->bi_sector
,
2369 (unsigned long long)sh
->sector
);
2372 * If several bio share a stripe. The bio bi_phys_segments acts as a
2373 * reference count to avoid race. The reference count should already be
2374 * increased before this function is called (for example, in
2375 * make_request()), so other bio sharing this stripe will not free the
2376 * stripe. If a stripe is owned by one stripe, the stripe lock will
2379 spin_lock_irq(&sh
->stripe_lock
);
2381 bip
= &sh
->dev
[dd_idx
].towrite
;
2385 bip
= &sh
->dev
[dd_idx
].toread
;
2386 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
2387 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
2389 bip
= & (*bip
)->bi_next
;
2391 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
2394 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
2398 raid5_inc_bi_active_stripes(bi
);
2401 /* check if page is covered */
2402 sector_t sector
= sh
->dev
[dd_idx
].sector
;
2403 for (bi
=sh
->dev
[dd_idx
].towrite
;
2404 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
2405 bi
&& bi
->bi_sector
<= sector
;
2406 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
2407 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
2408 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
2410 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
2411 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
2414 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2415 (unsigned long long)(*bip
)->bi_sector
,
2416 (unsigned long long)sh
->sector
, dd_idx
);
2417 spin_unlock_irq(&sh
->stripe_lock
);
2419 if (conf
->mddev
->bitmap
&& firstwrite
) {
2420 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
2422 sh
->bm_seq
= conf
->seq_flush
+1;
2423 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
2428 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
2429 spin_unlock_irq(&sh
->stripe_lock
);
2433 static void end_reshape(struct r5conf
*conf
);
2435 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
2436 struct stripe_head
*sh
)
2438 int sectors_per_chunk
=
2439 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
2441 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
2442 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
2444 raid5_compute_sector(conf
,
2445 stripe
* (disks
- conf
->max_degraded
)
2446 *sectors_per_chunk
+ chunk_offset
,
2452 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
2453 struct stripe_head_state
*s
, int disks
,
2454 struct bio
**return_bi
)
2457 for (i
= disks
; i
--; ) {
2461 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2462 struct md_rdev
*rdev
;
2464 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2465 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2466 atomic_inc(&rdev
->nr_pending
);
2471 if (!rdev_set_badblocks(
2475 md_error(conf
->mddev
, rdev
);
2476 rdev_dec_pending(rdev
, conf
->mddev
);
2479 spin_lock_irq(&sh
->stripe_lock
);
2480 /* fail all writes first */
2481 bi
= sh
->dev
[i
].towrite
;
2482 sh
->dev
[i
].towrite
= NULL
;
2483 spin_unlock_irq(&sh
->stripe_lock
);
2487 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2488 wake_up(&conf
->wait_for_overlap
);
2490 while (bi
&& bi
->bi_sector
<
2491 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2492 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2493 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2494 if (!raid5_dec_bi_active_stripes(bi
)) {
2495 md_write_end(conf
->mddev
);
2496 bi
->bi_next
= *return_bi
;
2502 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2503 STRIPE_SECTORS
, 0, 0);
2505 /* and fail all 'written' */
2506 bi
= sh
->dev
[i
].written
;
2507 sh
->dev
[i
].written
= NULL
;
2508 if (bi
) bitmap_end
= 1;
2509 while (bi
&& bi
->bi_sector
<
2510 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2511 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2512 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2513 if (!raid5_dec_bi_active_stripes(bi
)) {
2514 md_write_end(conf
->mddev
);
2515 bi
->bi_next
= *return_bi
;
2521 /* fail any reads if this device is non-operational and
2522 * the data has not reached the cache yet.
2524 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2525 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2526 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2527 spin_lock_irq(&sh
->stripe_lock
);
2528 bi
= sh
->dev
[i
].toread
;
2529 sh
->dev
[i
].toread
= NULL
;
2530 spin_unlock_irq(&sh
->stripe_lock
);
2531 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2532 wake_up(&conf
->wait_for_overlap
);
2533 while (bi
&& bi
->bi_sector
<
2534 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2535 struct bio
*nextbi
=
2536 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2537 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2538 if (!raid5_dec_bi_active_stripes(bi
)) {
2539 bi
->bi_next
= *return_bi
;
2546 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2547 STRIPE_SECTORS
, 0, 0);
2548 /* If we were in the middle of a write the parity block might
2549 * still be locked - so just clear all R5_LOCKED flags
2551 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2554 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2555 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2556 md_wakeup_thread(conf
->mddev
->thread
);
2560 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
2561 struct stripe_head_state
*s
)
2566 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2569 /* There is nothing more to do for sync/check/repair.
2570 * Don't even need to abort as that is handled elsewhere
2571 * if needed, and not always wanted e.g. if there is a known
2573 * For recover/replace we need to record a bad block on all
2574 * non-sync devices, or abort the recovery
2576 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
2577 /* During recovery devices cannot be removed, so
2578 * locking and refcounting of rdevs is not needed
2580 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2581 struct md_rdev
*rdev
= conf
->disks
[i
].rdev
;
2583 && !test_bit(Faulty
, &rdev
->flags
)
2584 && !test_bit(In_sync
, &rdev
->flags
)
2585 && !rdev_set_badblocks(rdev
, sh
->sector
,
2588 rdev
= conf
->disks
[i
].replacement
;
2590 && !test_bit(Faulty
, &rdev
->flags
)
2591 && !test_bit(In_sync
, &rdev
->flags
)
2592 && !rdev_set_badblocks(rdev
, sh
->sector
,
2597 conf
->recovery_disabled
=
2598 conf
->mddev
->recovery_disabled
;
2600 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
2603 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
2605 struct md_rdev
*rdev
;
2607 /* Doing recovery so rcu locking not required */
2608 rdev
= sh
->raid_conf
->disks
[disk_idx
].replacement
;
2610 && !test_bit(Faulty
, &rdev
->flags
)
2611 && !test_bit(In_sync
, &rdev
->flags
)
2612 && (rdev
->recovery_offset
<= sh
->sector
2613 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
2619 /* fetch_block - checks the given member device to see if its data needs
2620 * to be read or computed to satisfy a request.
2622 * Returns 1 when no more member devices need to be checked, otherwise returns
2623 * 0 to tell the loop in handle_stripe_fill to continue
2625 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2626 int disk_idx
, int disks
)
2628 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2629 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
2630 &sh
->dev
[s
->failed_num
[1]] };
2632 /* is the data in this block needed, and can we get it? */
2633 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2634 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2636 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2637 s
->syncing
|| s
->expanding
||
2638 (s
->replacing
&& want_replace(sh
, disk_idx
)) ||
2639 (s
->failed
>= 1 && fdev
[0]->toread
) ||
2640 (s
->failed
>= 2 && fdev
[1]->toread
) ||
2641 (sh
->raid_conf
->level
<= 5 && s
->failed
&& fdev
[0]->towrite
&&
2642 !test_bit(R5_OVERWRITE
, &fdev
[0]->flags
)) ||
2643 (sh
->raid_conf
->level
== 6 && s
->failed
&& s
->to_write
))) {
2644 /* we would like to get this block, possibly by computing it,
2645 * otherwise read it if the backing disk is insync
2647 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
2648 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
2649 if ((s
->uptodate
== disks
- 1) &&
2650 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
2651 disk_idx
== s
->failed_num
[1]))) {
2652 /* have disk failed, and we're requested to fetch it;
2655 pr_debug("Computing stripe %llu block %d\n",
2656 (unsigned long long)sh
->sector
, disk_idx
);
2657 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2658 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2659 set_bit(R5_Wantcompute
, &dev
->flags
);
2660 sh
->ops
.target
= disk_idx
;
2661 sh
->ops
.target2
= -1; /* no 2nd target */
2663 /* Careful: from this point on 'uptodate' is in the eye
2664 * of raid_run_ops which services 'compute' operations
2665 * before writes. R5_Wantcompute flags a block that will
2666 * be R5_UPTODATE by the time it is needed for a
2667 * subsequent operation.
2671 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
2672 /* Computing 2-failure is *very* expensive; only
2673 * do it if failed >= 2
2676 for (other
= disks
; other
--; ) {
2677 if (other
== disk_idx
)
2679 if (!test_bit(R5_UPTODATE
,
2680 &sh
->dev
[other
].flags
))
2684 pr_debug("Computing stripe %llu blocks %d,%d\n",
2685 (unsigned long long)sh
->sector
,
2687 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2688 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2689 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
2690 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
2691 sh
->ops
.target
= disk_idx
;
2692 sh
->ops
.target2
= other
;
2696 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2697 set_bit(R5_LOCKED
, &dev
->flags
);
2698 set_bit(R5_Wantread
, &dev
->flags
);
2700 pr_debug("Reading block %d (sync=%d)\n",
2701 disk_idx
, s
->syncing
);
2709 * handle_stripe_fill - read or compute data to satisfy pending requests.
2711 static void handle_stripe_fill(struct stripe_head
*sh
,
2712 struct stripe_head_state
*s
,
2717 /* look for blocks to read/compute, skip this if a compute
2718 * is already in flight, or if the stripe contents are in the
2719 * midst of changing due to a write
2721 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2722 !sh
->reconstruct_state
)
2723 for (i
= disks
; i
--; )
2724 if (fetch_block(sh
, s
, i
, disks
))
2726 set_bit(STRIPE_HANDLE
, &sh
->state
);
2730 /* handle_stripe_clean_event
2731 * any written block on an uptodate or failed drive can be returned.
2732 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2733 * never LOCKED, so we don't need to test 'failed' directly.
2735 static void handle_stripe_clean_event(struct r5conf
*conf
,
2736 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2741 for (i
= disks
; i
--; )
2742 if (sh
->dev
[i
].written
) {
2744 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2745 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2746 test_bit(R5_Discard
, &dev
->flags
))) {
2747 /* We can return any write requests */
2748 struct bio
*wbi
, *wbi2
;
2749 pr_debug("Return write for disc %d\n", i
);
2750 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
2751 clear_bit(R5_UPTODATE
, &dev
->flags
);
2753 dev
->written
= NULL
;
2754 while (wbi
&& wbi
->bi_sector
<
2755 dev
->sector
+ STRIPE_SECTORS
) {
2756 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2757 if (!raid5_dec_bi_active_stripes(wbi
)) {
2758 md_write_end(conf
->mddev
);
2759 wbi
->bi_next
= *return_bi
;
2764 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2766 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
2769 } else if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
2770 clear_bit(R5_Discard
, &sh
->dev
[i
].flags
);
2772 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2773 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2774 md_wakeup_thread(conf
->mddev
->thread
);
2777 static void handle_stripe_dirtying(struct r5conf
*conf
,
2778 struct stripe_head
*sh
,
2779 struct stripe_head_state
*s
,
2782 int rmw
= 0, rcw
= 0, i
;
2783 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
2785 /* RAID6 requires 'rcw' in current implementation.
2786 * Otherwise, check whether resync is now happening or should start.
2787 * If yes, then the array is dirty (after unclean shutdown or
2788 * initial creation), so parity in some stripes might be inconsistent.
2789 * In this case, we need to always do reconstruct-write, to ensure
2790 * that in case of drive failure or read-error correction, we
2791 * generate correct data from the parity.
2793 if (conf
->max_degraded
== 2 ||
2794 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
)) {
2795 /* Calculate the real rcw later - for now make it
2796 * look like rcw is cheaper
2799 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2800 conf
->max_degraded
, (unsigned long long)recovery_cp
,
2801 (unsigned long long)sh
->sector
);
2802 } else for (i
= disks
; i
--; ) {
2803 /* would I have to read this buffer for read_modify_write */
2804 struct r5dev
*dev
= &sh
->dev
[i
];
2805 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2806 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2807 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2808 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2809 if (test_bit(R5_Insync
, &dev
->flags
))
2812 rmw
+= 2*disks
; /* cannot read it */
2814 /* Would I have to read this buffer for reconstruct_write */
2815 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
2816 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2817 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2818 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2819 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2824 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2825 (unsigned long long)sh
->sector
, rmw
, rcw
);
2826 set_bit(STRIPE_HANDLE
, &sh
->state
);
2827 if (rmw
< rcw
&& rmw
> 0) {
2828 /* prefer read-modify-write, but need to get some data */
2829 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rmw %llu %d",
2830 (unsigned long long)sh
->sector
, rmw
);
2831 for (i
= disks
; i
--; ) {
2832 struct r5dev
*dev
= &sh
->dev
[i
];
2833 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2834 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2835 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2836 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2837 test_bit(R5_Insync
, &dev
->flags
)) {
2839 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2840 pr_debug("Read_old block "
2841 "%d for r-m-w\n", i
);
2842 set_bit(R5_LOCKED
, &dev
->flags
);
2843 set_bit(R5_Wantread
, &dev
->flags
);
2846 set_bit(STRIPE_DELAYED
, &sh
->state
);
2847 set_bit(STRIPE_HANDLE
, &sh
->state
);
2852 if (rcw
<= rmw
&& rcw
> 0) {
2853 /* want reconstruct write, but need to get some data */
2856 for (i
= disks
; i
--; ) {
2857 struct r5dev
*dev
= &sh
->dev
[i
];
2858 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2859 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
2860 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2861 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2862 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2864 if (!test_bit(R5_Insync
, &dev
->flags
))
2865 continue; /* it's a failed drive */
2867 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2868 pr_debug("Read_old block "
2869 "%d for Reconstruct\n", i
);
2870 set_bit(R5_LOCKED
, &dev
->flags
);
2871 set_bit(R5_Wantread
, &dev
->flags
);
2875 set_bit(STRIPE_DELAYED
, &sh
->state
);
2876 set_bit(STRIPE_HANDLE
, &sh
->state
);
2881 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
2882 (unsigned long long)sh
->sector
,
2883 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
2885 /* now if nothing is locked, and if we have enough data,
2886 * we can start a write request
2888 /* since handle_stripe can be called at any time we need to handle the
2889 * case where a compute block operation has been submitted and then a
2890 * subsequent call wants to start a write request. raid_run_ops only
2891 * handles the case where compute block and reconstruct are requested
2892 * simultaneously. If this is not the case then new writes need to be
2893 * held off until the compute completes.
2895 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
2896 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
2897 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
2898 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
2901 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
2902 struct stripe_head_state
*s
, int disks
)
2904 struct r5dev
*dev
= NULL
;
2906 set_bit(STRIPE_HANDLE
, &sh
->state
);
2908 switch (sh
->check_state
) {
2909 case check_state_idle
:
2910 /* start a new check operation if there are no failures */
2911 if (s
->failed
== 0) {
2912 BUG_ON(s
->uptodate
!= disks
);
2913 sh
->check_state
= check_state_run
;
2914 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2915 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
2919 dev
= &sh
->dev
[s
->failed_num
[0]];
2921 case check_state_compute_result
:
2922 sh
->check_state
= check_state_idle
;
2924 dev
= &sh
->dev
[sh
->pd_idx
];
2926 /* check that a write has not made the stripe insync */
2927 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2930 /* either failed parity check, or recovery is happening */
2931 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
2932 BUG_ON(s
->uptodate
!= disks
);
2934 set_bit(R5_LOCKED
, &dev
->flags
);
2936 set_bit(R5_Wantwrite
, &dev
->flags
);
2938 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2939 set_bit(STRIPE_INSYNC
, &sh
->state
);
2941 case check_state_run
:
2942 break; /* we will be called again upon completion */
2943 case check_state_check_result
:
2944 sh
->check_state
= check_state_idle
;
2946 /* if a failure occurred during the check operation, leave
2947 * STRIPE_INSYNC not set and let the stripe be handled again
2952 /* handle a successful check operation, if parity is correct
2953 * we are done. Otherwise update the mismatch count and repair
2954 * parity if !MD_RECOVERY_CHECK
2956 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
2957 /* parity is correct (on disc,
2958 * not in buffer any more)
2960 set_bit(STRIPE_INSYNC
, &sh
->state
);
2962 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
2963 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2964 /* don't try to repair!! */
2965 set_bit(STRIPE_INSYNC
, &sh
->state
);
2967 sh
->check_state
= check_state_compute_run
;
2968 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2969 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2970 set_bit(R5_Wantcompute
,
2971 &sh
->dev
[sh
->pd_idx
].flags
);
2972 sh
->ops
.target
= sh
->pd_idx
;
2973 sh
->ops
.target2
= -1;
2978 case check_state_compute_run
:
2981 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
2982 __func__
, sh
->check_state
,
2983 (unsigned long long) sh
->sector
);
2989 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
2990 struct stripe_head_state
*s
,
2993 int pd_idx
= sh
->pd_idx
;
2994 int qd_idx
= sh
->qd_idx
;
2997 set_bit(STRIPE_HANDLE
, &sh
->state
);
2999 BUG_ON(s
->failed
> 2);
3001 /* Want to check and possibly repair P and Q.
3002 * However there could be one 'failed' device, in which
3003 * case we can only check one of them, possibly using the
3004 * other to generate missing data
3007 switch (sh
->check_state
) {
3008 case check_state_idle
:
3009 /* start a new check operation if there are < 2 failures */
3010 if (s
->failed
== s
->q_failed
) {
3011 /* The only possible failed device holds Q, so it
3012 * makes sense to check P (If anything else were failed,
3013 * we would have used P to recreate it).
3015 sh
->check_state
= check_state_run
;
3017 if (!s
->q_failed
&& s
->failed
< 2) {
3018 /* Q is not failed, and we didn't use it to generate
3019 * anything, so it makes sense to check it
3021 if (sh
->check_state
== check_state_run
)
3022 sh
->check_state
= check_state_run_pq
;
3024 sh
->check_state
= check_state_run_q
;
3027 /* discard potentially stale zero_sum_result */
3028 sh
->ops
.zero_sum_result
= 0;
3030 if (sh
->check_state
== check_state_run
) {
3031 /* async_xor_zero_sum destroys the contents of P */
3032 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3035 if (sh
->check_state
>= check_state_run
&&
3036 sh
->check_state
<= check_state_run_pq
) {
3037 /* async_syndrome_zero_sum preserves P and Q, so
3038 * no need to mark them !uptodate here
3040 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3044 /* we have 2-disk failure */
3045 BUG_ON(s
->failed
!= 2);
3047 case check_state_compute_result
:
3048 sh
->check_state
= check_state_idle
;
3050 /* check that a write has not made the stripe insync */
3051 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3054 /* now write out any block on a failed drive,
3055 * or P or Q if they were recomputed
3057 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
3058 if (s
->failed
== 2) {
3059 dev
= &sh
->dev
[s
->failed_num
[1]];
3061 set_bit(R5_LOCKED
, &dev
->flags
);
3062 set_bit(R5_Wantwrite
, &dev
->flags
);
3064 if (s
->failed
>= 1) {
3065 dev
= &sh
->dev
[s
->failed_num
[0]];
3067 set_bit(R5_LOCKED
, &dev
->flags
);
3068 set_bit(R5_Wantwrite
, &dev
->flags
);
3070 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3071 dev
= &sh
->dev
[pd_idx
];
3073 set_bit(R5_LOCKED
, &dev
->flags
);
3074 set_bit(R5_Wantwrite
, &dev
->flags
);
3076 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3077 dev
= &sh
->dev
[qd_idx
];
3079 set_bit(R5_LOCKED
, &dev
->flags
);
3080 set_bit(R5_Wantwrite
, &dev
->flags
);
3082 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3084 set_bit(STRIPE_INSYNC
, &sh
->state
);
3086 case check_state_run
:
3087 case check_state_run_q
:
3088 case check_state_run_pq
:
3089 break; /* we will be called again upon completion */
3090 case check_state_check_result
:
3091 sh
->check_state
= check_state_idle
;
3093 /* handle a successful check operation, if parity is correct
3094 * we are done. Otherwise update the mismatch count and repair
3095 * parity if !MD_RECOVERY_CHECK
3097 if (sh
->ops
.zero_sum_result
== 0) {
3098 /* both parities are correct */
3100 set_bit(STRIPE_INSYNC
, &sh
->state
);
3102 /* in contrast to the raid5 case we can validate
3103 * parity, but still have a failure to write
3106 sh
->check_state
= check_state_compute_result
;
3107 /* Returning at this point means that we may go
3108 * off and bring p and/or q uptodate again so
3109 * we make sure to check zero_sum_result again
3110 * to verify if p or q need writeback
3114 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3115 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3116 /* don't try to repair!! */
3117 set_bit(STRIPE_INSYNC
, &sh
->state
);
3119 int *target
= &sh
->ops
.target
;
3121 sh
->ops
.target
= -1;
3122 sh
->ops
.target2
= -1;
3123 sh
->check_state
= check_state_compute_run
;
3124 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3125 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3126 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3127 set_bit(R5_Wantcompute
,
3128 &sh
->dev
[pd_idx
].flags
);
3130 target
= &sh
->ops
.target2
;
3133 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3134 set_bit(R5_Wantcompute
,
3135 &sh
->dev
[qd_idx
].flags
);
3142 case check_state_compute_run
:
3145 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3146 __func__
, sh
->check_state
,
3147 (unsigned long long) sh
->sector
);
3152 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3156 /* We have read all the blocks in this stripe and now we need to
3157 * copy some of them into a target stripe for expand.
3159 struct dma_async_tx_descriptor
*tx
= NULL
;
3160 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3161 for (i
= 0; i
< sh
->disks
; i
++)
3162 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3164 struct stripe_head
*sh2
;
3165 struct async_submit_ctl submit
;
3167 sector_t bn
= compute_blocknr(sh
, i
, 1);
3168 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3170 sh2
= get_active_stripe(conf
, s
, 0, 1, 1);
3172 /* so far only the early blocks of this stripe
3173 * have been requested. When later blocks
3174 * get requested, we will try again
3177 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3178 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3179 /* must have already done this block */
3180 release_stripe(sh2
);
3184 /* place all the copies on one channel */
3185 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
3186 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
3187 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
3190 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
3191 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
3192 for (j
= 0; j
< conf
->raid_disks
; j
++)
3193 if (j
!= sh2
->pd_idx
&&
3195 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
3197 if (j
== conf
->raid_disks
) {
3198 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
3199 set_bit(STRIPE_HANDLE
, &sh2
->state
);
3201 release_stripe(sh2
);
3204 /* done submitting copies, wait for them to complete */
3205 async_tx_quiesce(&tx
);
3209 * handle_stripe - do things to a stripe.
3211 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3212 * state of various bits to see what needs to be done.
3214 * return some read requests which now have data
3215 * return some write requests which are safely on storage
3216 * schedule a read on some buffers
3217 * schedule a write of some buffers
3218 * return confirmation of parity correctness
3222 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
3224 struct r5conf
*conf
= sh
->raid_conf
;
3225 int disks
= sh
->disks
;
3228 int do_recovery
= 0;
3230 memset(s
, 0, sizeof(*s
));
3232 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3233 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3234 s
->failed_num
[0] = -1;
3235 s
->failed_num
[1] = -1;
3237 /* Now to look around and see what can be done */
3239 for (i
=disks
; i
--; ) {
3240 struct md_rdev
*rdev
;
3247 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3249 dev
->toread
, dev
->towrite
, dev
->written
);
3250 /* maybe we can reply to a read
3252 * new wantfill requests are only permitted while
3253 * ops_complete_biofill is guaranteed to be inactive
3255 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
3256 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
3257 set_bit(R5_Wantfill
, &dev
->flags
);
3259 /* now count some things */
3260 if (test_bit(R5_LOCKED
, &dev
->flags
))
3262 if (test_bit(R5_UPTODATE
, &dev
->flags
))
3264 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
3266 BUG_ON(s
->compute
> 2);
3269 if (test_bit(R5_Wantfill
, &dev
->flags
))
3271 else if (dev
->toread
)
3275 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3280 /* Prefer to use the replacement for reads, but only
3281 * if it is recovered enough and has no bad blocks.
3283 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3284 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
3285 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
3286 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3287 &first_bad
, &bad_sectors
))
3288 set_bit(R5_ReadRepl
, &dev
->flags
);
3291 set_bit(R5_NeedReplace
, &dev
->flags
);
3292 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3293 clear_bit(R5_ReadRepl
, &dev
->flags
);
3295 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
3298 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3299 &first_bad
, &bad_sectors
);
3300 if (s
->blocked_rdev
== NULL
3301 && (test_bit(Blocked
, &rdev
->flags
)
3304 set_bit(BlockedBadBlocks
,
3306 s
->blocked_rdev
= rdev
;
3307 atomic_inc(&rdev
->nr_pending
);
3310 clear_bit(R5_Insync
, &dev
->flags
);
3314 /* also not in-sync */
3315 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
3316 test_bit(R5_UPTODATE
, &dev
->flags
)) {
3317 /* treat as in-sync, but with a read error
3318 * which we can now try to correct
3320 set_bit(R5_Insync
, &dev
->flags
);
3321 set_bit(R5_ReadError
, &dev
->flags
);
3323 } else if (test_bit(In_sync
, &rdev
->flags
))
3324 set_bit(R5_Insync
, &dev
->flags
);
3325 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
3326 /* in sync if before recovery_offset */
3327 set_bit(R5_Insync
, &dev
->flags
);
3328 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
3329 test_bit(R5_Expanded
, &dev
->flags
))
3330 /* If we've reshaped into here, we assume it is Insync.
3331 * We will shortly update recovery_offset to make
3334 set_bit(R5_Insync
, &dev
->flags
);
3336 if (rdev
&& test_bit(R5_WriteError
, &dev
->flags
)) {
3337 /* This flag does not apply to '.replacement'
3338 * only to .rdev, so make sure to check that*/
3339 struct md_rdev
*rdev2
= rcu_dereference(
3340 conf
->disks
[i
].rdev
);
3342 clear_bit(R5_Insync
, &dev
->flags
);
3343 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3344 s
->handle_bad_blocks
= 1;
3345 atomic_inc(&rdev2
->nr_pending
);
3347 clear_bit(R5_WriteError
, &dev
->flags
);
3349 if (rdev
&& test_bit(R5_MadeGood
, &dev
->flags
)) {
3350 /* This flag does not apply to '.replacement'
3351 * only to .rdev, so make sure to check that*/
3352 struct md_rdev
*rdev2
= rcu_dereference(
3353 conf
->disks
[i
].rdev
);
3354 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3355 s
->handle_bad_blocks
= 1;
3356 atomic_inc(&rdev2
->nr_pending
);
3358 clear_bit(R5_MadeGood
, &dev
->flags
);
3360 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3361 struct md_rdev
*rdev2
= rcu_dereference(
3362 conf
->disks
[i
].replacement
);
3363 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3364 s
->handle_bad_blocks
= 1;
3365 atomic_inc(&rdev2
->nr_pending
);
3367 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
3369 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3370 /* The ReadError flag will just be confusing now */
3371 clear_bit(R5_ReadError
, &dev
->flags
);
3372 clear_bit(R5_ReWrite
, &dev
->flags
);
3374 if (test_bit(R5_ReadError
, &dev
->flags
))
3375 clear_bit(R5_Insync
, &dev
->flags
);
3376 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3378 s
->failed_num
[s
->failed
] = i
;
3380 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
3384 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
3385 /* If there is a failed device being replaced,
3386 * we must be recovering.
3387 * else if we are after recovery_cp, we must be syncing
3388 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3389 * else we can only be replacing
3390 * sync and recovery both need to read all devices, and so
3391 * use the same flag.
3394 sh
->sector
>= conf
->mddev
->recovery_cp
||
3395 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
3403 static void handle_stripe(struct stripe_head
*sh
)
3405 struct stripe_head_state s
;
3406 struct r5conf
*conf
= sh
->raid_conf
;
3409 int disks
= sh
->disks
;
3410 struct r5dev
*pdev
, *qdev
;
3412 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3413 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
3414 /* already being handled, ensure it gets handled
3415 * again when current action finishes */
3416 set_bit(STRIPE_HANDLE
, &sh
->state
);
3420 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3421 set_bit(STRIPE_SYNCING
, &sh
->state
);
3422 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3424 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3426 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3427 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3428 (unsigned long long)sh
->sector
, sh
->state
,
3429 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
3430 sh
->check_state
, sh
->reconstruct_state
);
3432 analyse_stripe(sh
, &s
);
3434 if (s
.handle_bad_blocks
) {
3435 set_bit(STRIPE_HANDLE
, &sh
->state
);
3439 if (unlikely(s
.blocked_rdev
)) {
3440 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3441 s
.replacing
|| s
.to_write
|| s
.written
) {
3442 set_bit(STRIPE_HANDLE
, &sh
->state
);
3445 /* There is nothing for the blocked_rdev to block */
3446 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
3447 s
.blocked_rdev
= NULL
;
3450 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
3451 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
3452 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
3455 pr_debug("locked=%d uptodate=%d to_read=%d"
3456 " to_write=%d failed=%d failed_num=%d,%d\n",
3457 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3458 s
.failed_num
[0], s
.failed_num
[1]);
3459 /* check if the array has lost more than max_degraded devices and,
3460 * if so, some requests might need to be failed.
3462 if (s
.failed
> conf
->max_degraded
) {
3463 sh
->check_state
= 0;
3464 sh
->reconstruct_state
= 0;
3465 if (s
.to_read
+s
.to_write
+s
.written
)
3466 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
3467 if (s
.syncing
+ s
.replacing
)
3468 handle_failed_sync(conf
, sh
, &s
);
3471 /* Now we check to see if any write operations have recently
3475 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
3477 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
3478 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
3479 sh
->reconstruct_state
= reconstruct_state_idle
;
3481 /* All the 'written' buffers and the parity block are ready to
3482 * be written back to disk
3484 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
3485 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
3486 BUG_ON(sh
->qd_idx
>= 0 &&
3487 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
3488 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
3489 for (i
= disks
; i
--; ) {
3490 struct r5dev
*dev
= &sh
->dev
[i
];
3491 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
3492 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3494 pr_debug("Writing block %d\n", i
);
3495 set_bit(R5_Wantwrite
, &dev
->flags
);
3498 if (!test_bit(R5_Insync
, &dev
->flags
) ||
3499 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
3501 set_bit(STRIPE_INSYNC
, &sh
->state
);
3504 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3505 s
.dec_preread_active
= 1;
3509 * might be able to return some write requests if the parity blocks
3510 * are safe, or on a failed drive
3512 pdev
= &sh
->dev
[sh
->pd_idx
];
3513 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
3514 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
3515 qdev
= &sh
->dev
[sh
->qd_idx
];
3516 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
3517 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
3521 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3522 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3523 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
3524 test_bit(R5_Discard
, &pdev
->flags
))))) &&
3525 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3526 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3527 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
3528 test_bit(R5_Discard
, &qdev
->flags
))))))
3529 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
3531 /* Now we might consider reading some blocks, either to check/generate
3532 * parity, or to satisfy requests
3533 * or to load a block that is being partially written.
3535 if (s
.to_read
|| s
.non_overwrite
3536 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
3537 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
3540 handle_stripe_fill(sh
, &s
, disks
);
3542 /* Now to consider new write requests and what else, if anything
3543 * should be read. We do not handle new writes when:
3544 * 1/ A 'write' operation (copy+xor) is already in flight.
3545 * 2/ A 'check' operation is in flight, as it may clobber the parity
3548 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
3549 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
3551 /* maybe we need to check and possibly fix the parity for this stripe
3552 * Any reads will already have been scheduled, so we just see if enough
3553 * data is available. The parity check is held off while parity
3554 * dependent operations are in flight.
3556 if (sh
->check_state
||
3557 (s
.syncing
&& s
.locked
== 0 &&
3558 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3559 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
3560 if (conf
->level
== 6)
3561 handle_parity_checks6(conf
, sh
, &s
, disks
);
3563 handle_parity_checks5(conf
, sh
, &s
, disks
);
3566 if (s
.replacing
&& s
.locked
== 0
3567 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3568 /* Write out to replacement devices where possible */
3569 for (i
= 0; i
< conf
->raid_disks
; i
++)
3570 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
) &&
3571 test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
3572 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
3573 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3576 set_bit(STRIPE_INSYNC
, &sh
->state
);
3578 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
3579 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3580 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3581 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3584 /* If the failed drives are just a ReadError, then we might need
3585 * to progress the repair/check process
3587 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
3588 for (i
= 0; i
< s
.failed
; i
++) {
3589 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
3590 if (test_bit(R5_ReadError
, &dev
->flags
)
3591 && !test_bit(R5_LOCKED
, &dev
->flags
)
3592 && test_bit(R5_UPTODATE
, &dev
->flags
)
3594 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3595 set_bit(R5_Wantwrite
, &dev
->flags
);
3596 set_bit(R5_ReWrite
, &dev
->flags
);
3597 set_bit(R5_LOCKED
, &dev
->flags
);
3600 /* let's read it back */
3601 set_bit(R5_Wantread
, &dev
->flags
);
3602 set_bit(R5_LOCKED
, &dev
->flags
);
3609 /* Finish reconstruct operations initiated by the expansion process */
3610 if (sh
->reconstruct_state
== reconstruct_state_result
) {
3611 struct stripe_head
*sh_src
3612 = get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
3613 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
3614 /* sh cannot be written until sh_src has been read.
3615 * so arrange for sh to be delayed a little
3617 set_bit(STRIPE_DELAYED
, &sh
->state
);
3618 set_bit(STRIPE_HANDLE
, &sh
->state
);
3619 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
3621 atomic_inc(&conf
->preread_active_stripes
);
3622 release_stripe(sh_src
);
3626 release_stripe(sh_src
);
3628 sh
->reconstruct_state
= reconstruct_state_idle
;
3629 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3630 for (i
= conf
->raid_disks
; i
--; ) {
3631 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3632 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3637 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
3638 !sh
->reconstruct_state
) {
3639 /* Need to write out all blocks after computing parity */
3640 sh
->disks
= conf
->raid_disks
;
3641 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3642 schedule_reconstruction(sh
, &s
, 1, 1);
3643 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
3644 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3645 atomic_dec(&conf
->reshape_stripes
);
3646 wake_up(&conf
->wait_for_overlap
);
3647 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3650 if (s
.expanding
&& s
.locked
== 0 &&
3651 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3652 handle_stripe_expansion(conf
, sh
);
3655 /* wait for this device to become unblocked */
3656 if (unlikely(s
.blocked_rdev
)) {
3657 if (conf
->mddev
->external
)
3658 md_wait_for_blocked_rdev(s
.blocked_rdev
,
3661 /* Internal metadata will immediately
3662 * be written by raid5d, so we don't
3663 * need to wait here.
3665 rdev_dec_pending(s
.blocked_rdev
,
3669 if (s
.handle_bad_blocks
)
3670 for (i
= disks
; i
--; ) {
3671 struct md_rdev
*rdev
;
3672 struct r5dev
*dev
= &sh
->dev
[i
];
3673 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
3674 /* We own a safe reference to the rdev */
3675 rdev
= conf
->disks
[i
].rdev
;
3676 if (!rdev_set_badblocks(rdev
, sh
->sector
,
3678 md_error(conf
->mddev
, rdev
);
3679 rdev_dec_pending(rdev
, conf
->mddev
);
3681 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
3682 rdev
= conf
->disks
[i
].rdev
;
3683 rdev_clear_badblocks(rdev
, sh
->sector
,
3685 rdev_dec_pending(rdev
, conf
->mddev
);
3687 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3688 rdev
= conf
->disks
[i
].replacement
;
3690 /* rdev have been moved down */
3691 rdev
= conf
->disks
[i
].rdev
;
3692 rdev_clear_badblocks(rdev
, sh
->sector
,
3694 rdev_dec_pending(rdev
, conf
->mddev
);
3699 raid_run_ops(sh
, s
.ops_request
);
3703 if (s
.dec_preread_active
) {
3704 /* We delay this until after ops_run_io so that if make_request
3705 * is waiting on a flush, it won't continue until the writes
3706 * have actually been submitted.
3708 atomic_dec(&conf
->preread_active_stripes
);
3709 if (atomic_read(&conf
->preread_active_stripes
) <
3711 md_wakeup_thread(conf
->mddev
->thread
);
3714 return_io(s
.return_bi
);
3716 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
3719 static void raid5_activate_delayed(struct r5conf
*conf
)
3721 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
3722 while (!list_empty(&conf
->delayed_list
)) {
3723 struct list_head
*l
= conf
->delayed_list
.next
;
3724 struct stripe_head
*sh
;
3725 sh
= list_entry(l
, struct stripe_head
, lru
);
3727 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3728 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3729 atomic_inc(&conf
->preread_active_stripes
);
3730 list_add_tail(&sh
->lru
, &conf
->hold_list
);
3735 static void activate_bit_delay(struct r5conf
*conf
)
3737 /* device_lock is held */
3738 struct list_head head
;
3739 list_add(&head
, &conf
->bitmap_list
);
3740 list_del_init(&conf
->bitmap_list
);
3741 while (!list_empty(&head
)) {
3742 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
3743 list_del_init(&sh
->lru
);
3744 atomic_inc(&sh
->count
);
3745 __release_stripe(conf
, sh
);
3749 int md_raid5_congested(struct mddev
*mddev
, int bits
)
3751 struct r5conf
*conf
= mddev
->private;
3753 /* No difference between reads and writes. Just check
3754 * how busy the stripe_cache is
3757 if (conf
->inactive_blocked
)
3761 if (list_empty_careful(&conf
->inactive_list
))
3766 EXPORT_SYMBOL_GPL(md_raid5_congested
);
3768 static int raid5_congested(void *data
, int bits
)
3770 struct mddev
*mddev
= data
;
3772 return mddev_congested(mddev
, bits
) ||
3773 md_raid5_congested(mddev
, bits
);
3776 /* We want read requests to align with chunks where possible,
3777 * but write requests don't need to.
3779 static int raid5_mergeable_bvec(struct request_queue
*q
,
3780 struct bvec_merge_data
*bvm
,
3781 struct bio_vec
*biovec
)
3783 struct mddev
*mddev
= q
->queuedata
;
3784 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
3786 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3787 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
3789 if ((bvm
->bi_rw
& 1) == WRITE
)
3790 return biovec
->bv_len
; /* always allow writes to be mergeable */
3792 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3793 chunk_sectors
= mddev
->new_chunk_sectors
;
3794 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
3795 if (max
< 0) max
= 0;
3796 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
3797 return biovec
->bv_len
;
3803 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
3805 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
3806 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3807 unsigned int bio_sectors
= bio
->bi_size
>> 9;
3809 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3810 chunk_sectors
= mddev
->new_chunk_sectors
;
3811 return chunk_sectors
>=
3812 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
3816 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3817 * later sampled by raid5d.
3819 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
3821 unsigned long flags
;
3823 spin_lock_irqsave(&conf
->device_lock
, flags
);
3825 bi
->bi_next
= conf
->retry_read_aligned_list
;
3826 conf
->retry_read_aligned_list
= bi
;
3828 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3829 md_wakeup_thread(conf
->mddev
->thread
);
3833 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
3837 bi
= conf
->retry_read_aligned
;
3839 conf
->retry_read_aligned
= NULL
;
3842 bi
= conf
->retry_read_aligned_list
;
3844 conf
->retry_read_aligned_list
= bi
->bi_next
;
3847 * this sets the active strip count to 1 and the processed
3848 * strip count to zero (upper 8 bits)
3850 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
3858 * The "raid5_align_endio" should check if the read succeeded and if it
3859 * did, call bio_endio on the original bio (having bio_put the new bio
3861 * If the read failed..
3863 static void raid5_align_endio(struct bio
*bi
, int error
)
3865 struct bio
* raid_bi
= bi
->bi_private
;
3866 struct mddev
*mddev
;
3867 struct r5conf
*conf
;
3868 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
3869 struct md_rdev
*rdev
;
3873 rdev
= (void*)raid_bi
->bi_next
;
3874 raid_bi
->bi_next
= NULL
;
3875 mddev
= rdev
->mddev
;
3876 conf
= mddev
->private;
3878 rdev_dec_pending(rdev
, conf
->mddev
);
3880 if (!error
&& uptodate
) {
3881 bio_endio(raid_bi
, 0);
3882 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
3883 wake_up(&conf
->wait_for_stripe
);
3888 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3890 add_bio_to_retry(raid_bi
, conf
);
3893 static int bio_fits_rdev(struct bio
*bi
)
3895 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
3897 if ((bi
->bi_size
>>9) > queue_max_sectors(q
))
3899 blk_recount_segments(q
, bi
);
3900 if (bi
->bi_phys_segments
> queue_max_segments(q
))
3903 if (q
->merge_bvec_fn
)
3904 /* it's too hard to apply the merge_bvec_fn at this stage,
3913 static int chunk_aligned_read(struct mddev
*mddev
, struct bio
* raid_bio
)
3915 struct r5conf
*conf
= mddev
->private;
3917 struct bio
* align_bi
;
3918 struct md_rdev
*rdev
;
3919 sector_t end_sector
;
3921 if (!in_chunk_boundary(mddev
, raid_bio
)) {
3922 pr_debug("chunk_aligned_read : non aligned\n");
3926 * use bio_clone_mddev to make a copy of the bio
3928 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
3932 * set bi_end_io to a new function, and set bi_private to the
3935 align_bi
->bi_end_io
= raid5_align_endio
;
3936 align_bi
->bi_private
= raid_bio
;
3940 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
3944 end_sector
= align_bi
->bi_sector
+ (align_bi
->bi_size
>> 9);
3946 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
3947 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
3948 rdev
->recovery_offset
< end_sector
) {
3949 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
3951 (test_bit(Faulty
, &rdev
->flags
) ||
3952 !(test_bit(In_sync
, &rdev
->flags
) ||
3953 rdev
->recovery_offset
>= end_sector
)))
3960 atomic_inc(&rdev
->nr_pending
);
3962 raid_bio
->bi_next
= (void*)rdev
;
3963 align_bi
->bi_bdev
= rdev
->bdev
;
3964 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
3966 if (!bio_fits_rdev(align_bi
) ||
3967 is_badblock(rdev
, align_bi
->bi_sector
, align_bi
->bi_size
>>9,
3968 &first_bad
, &bad_sectors
)) {
3969 /* too big in some way, or has a known bad block */
3971 rdev_dec_pending(rdev
, mddev
);
3975 /* No reshape active, so we can trust rdev->data_offset */
3976 align_bi
->bi_sector
+= rdev
->data_offset
;
3978 spin_lock_irq(&conf
->device_lock
);
3979 wait_event_lock_irq(conf
->wait_for_stripe
,
3982 atomic_inc(&conf
->active_aligned_reads
);
3983 spin_unlock_irq(&conf
->device_lock
);
3985 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
3986 align_bi
, disk_devt(mddev
->gendisk
),
3987 raid_bio
->bi_sector
);
3988 generic_make_request(align_bi
);
3997 /* __get_priority_stripe - get the next stripe to process
3999 * Full stripe writes are allowed to pass preread active stripes up until
4000 * the bypass_threshold is exceeded. In general the bypass_count
4001 * increments when the handle_list is handled before the hold_list; however, it
4002 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4003 * stripe with in flight i/o. The bypass_count will be reset when the
4004 * head of the hold_list has changed, i.e. the head was promoted to the
4007 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
)
4009 struct stripe_head
*sh
;
4011 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4013 list_empty(&conf
->handle_list
) ? "empty" : "busy",
4014 list_empty(&conf
->hold_list
) ? "empty" : "busy",
4015 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
4017 if (!list_empty(&conf
->handle_list
)) {
4018 sh
= list_entry(conf
->handle_list
.next
, typeof(*sh
), lru
);
4020 if (list_empty(&conf
->hold_list
))
4021 conf
->bypass_count
= 0;
4022 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
4023 if (conf
->hold_list
.next
== conf
->last_hold
)
4024 conf
->bypass_count
++;
4026 conf
->last_hold
= conf
->hold_list
.next
;
4027 conf
->bypass_count
-= conf
->bypass_threshold
;
4028 if (conf
->bypass_count
< 0)
4029 conf
->bypass_count
= 0;
4032 } else if (!list_empty(&conf
->hold_list
) &&
4033 ((conf
->bypass_threshold
&&
4034 conf
->bypass_count
> conf
->bypass_threshold
) ||
4035 atomic_read(&conf
->pending_full_writes
) == 0)) {
4036 sh
= list_entry(conf
->hold_list
.next
,
4038 conf
->bypass_count
-= conf
->bypass_threshold
;
4039 if (conf
->bypass_count
< 0)
4040 conf
->bypass_count
= 0;
4044 list_del_init(&sh
->lru
);
4045 atomic_inc(&sh
->count
);
4046 BUG_ON(atomic_read(&sh
->count
) != 1);
4050 struct raid5_plug_cb
{
4051 struct blk_plug_cb cb
;
4052 struct list_head list
;
4055 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
4057 struct raid5_plug_cb
*cb
= container_of(
4058 blk_cb
, struct raid5_plug_cb
, cb
);
4059 struct stripe_head
*sh
;
4060 struct mddev
*mddev
= cb
->cb
.data
;
4061 struct r5conf
*conf
= mddev
->private;
4064 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
4065 spin_lock_irq(&conf
->device_lock
);
4066 while (!list_empty(&cb
->list
)) {
4067 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
4068 list_del_init(&sh
->lru
);
4070 * avoid race release_stripe_plug() sees
4071 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4072 * is still in our list
4074 smp_mb__before_clear_bit();
4075 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
4076 __release_stripe(conf
, sh
);
4079 spin_unlock_irq(&conf
->device_lock
);
4081 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
4085 static void release_stripe_plug(struct mddev
*mddev
,
4086 struct stripe_head
*sh
)
4088 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
4089 raid5_unplug
, mddev
,
4090 sizeof(struct raid5_plug_cb
));
4091 struct raid5_plug_cb
*cb
;
4098 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
4100 if (cb
->list
.next
== NULL
)
4101 INIT_LIST_HEAD(&cb
->list
);
4103 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
4104 list_add_tail(&sh
->lru
, &cb
->list
);
4109 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
4111 struct r5conf
*conf
= mddev
->private;
4112 sector_t logical_sector
, last_sector
;
4113 struct stripe_head
*sh
;
4117 if (mddev
->reshape_position
!= MaxSector
)
4118 /* Skip discard while reshape is happening */
4121 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4122 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
4125 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
4127 stripe_sectors
= conf
->chunk_sectors
*
4128 (conf
->raid_disks
- conf
->max_degraded
);
4129 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
4131 sector_div(last_sector
, stripe_sectors
);
4133 logical_sector
*= conf
->chunk_sectors
;
4134 last_sector
*= conf
->chunk_sectors
;
4136 for (; logical_sector
< last_sector
;
4137 logical_sector
+= STRIPE_SECTORS
) {
4141 sh
= get_active_stripe(conf
, logical_sector
, 0, 0, 0);
4142 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
4143 TASK_UNINTERRUPTIBLE
);
4144 spin_lock_irq(&sh
->stripe_lock
);
4145 for (d
= 0; d
< conf
->raid_disks
; d
++) {
4146 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
4148 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
4149 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
4150 spin_unlock_irq(&sh
->stripe_lock
);
4156 finish_wait(&conf
->wait_for_overlap
, &w
);
4157 for (d
= 0; d
< conf
->raid_disks
; d
++) {
4158 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
4160 sh
->dev
[d
].towrite
= bi
;
4161 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
4162 raid5_inc_bi_active_stripes(bi
);
4164 spin_unlock_irq(&sh
->stripe_lock
);
4165 if (conf
->mddev
->bitmap
) {
4167 d
< conf
->raid_disks
- conf
->max_degraded
;
4169 bitmap_startwrite(mddev
->bitmap
,
4173 sh
->bm_seq
= conf
->seq_flush
+ 1;
4174 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
4177 set_bit(STRIPE_HANDLE
, &sh
->state
);
4178 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4179 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4180 atomic_inc(&conf
->preread_active_stripes
);
4181 release_stripe_plug(mddev
, sh
);
4184 remaining
= raid5_dec_bi_active_stripes(bi
);
4185 if (remaining
== 0) {
4186 md_write_end(mddev
);
4191 static void make_request(struct mddev
*mddev
, struct bio
* bi
)
4193 struct r5conf
*conf
= mddev
->private;
4195 sector_t new_sector
;
4196 sector_t logical_sector
, last_sector
;
4197 struct stripe_head
*sh
;
4198 const int rw
= bio_data_dir(bi
);
4201 if (unlikely(bi
->bi_rw
& REQ_FLUSH
)) {
4202 md_flush_request(mddev
, bi
);
4206 md_write_start(mddev
, bi
);
4209 mddev
->reshape_position
== MaxSector
&&
4210 chunk_aligned_read(mddev
,bi
))
4213 if (unlikely(bi
->bi_rw
& REQ_DISCARD
)) {
4214 make_discard_request(mddev
, bi
);
4218 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4219 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
4221 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
4223 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
4229 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
4230 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
4231 /* spinlock is needed as reshape_progress may be
4232 * 64bit on a 32bit platform, and so it might be
4233 * possible to see a half-updated value
4234 * Of course reshape_progress could change after
4235 * the lock is dropped, so once we get a reference
4236 * to the stripe that we think it is, we will have
4239 spin_lock_irq(&conf
->device_lock
);
4240 if (mddev
->reshape_backwards
4241 ? logical_sector
< conf
->reshape_progress
4242 : logical_sector
>= conf
->reshape_progress
) {
4245 if (mddev
->reshape_backwards
4246 ? logical_sector
< conf
->reshape_safe
4247 : logical_sector
>= conf
->reshape_safe
) {
4248 spin_unlock_irq(&conf
->device_lock
);
4253 spin_unlock_irq(&conf
->device_lock
);
4256 new_sector
= raid5_compute_sector(conf
, logical_sector
,
4259 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4260 (unsigned long long)new_sector
,
4261 (unsigned long long)logical_sector
);
4263 sh
= get_active_stripe(conf
, new_sector
, previous
,
4264 (bi
->bi_rw
&RWA_MASK
), 0);
4266 if (unlikely(previous
)) {
4267 /* expansion might have moved on while waiting for a
4268 * stripe, so we must do the range check again.
4269 * Expansion could still move past after this
4270 * test, but as we are holding a reference to
4271 * 'sh', we know that if that happens,
4272 * STRIPE_EXPANDING will get set and the expansion
4273 * won't proceed until we finish with the stripe.
4276 spin_lock_irq(&conf
->device_lock
);
4277 if (mddev
->reshape_backwards
4278 ? logical_sector
>= conf
->reshape_progress
4279 : logical_sector
< conf
->reshape_progress
)
4280 /* mismatch, need to try again */
4282 spin_unlock_irq(&conf
->device_lock
);
4291 logical_sector
>= mddev
->suspend_lo
&&
4292 logical_sector
< mddev
->suspend_hi
) {
4294 /* As the suspend_* range is controlled by
4295 * userspace, we want an interruptible
4298 flush_signals(current
);
4299 prepare_to_wait(&conf
->wait_for_overlap
,
4300 &w
, TASK_INTERRUPTIBLE
);
4301 if (logical_sector
>= mddev
->suspend_lo
&&
4302 logical_sector
< mddev
->suspend_hi
)
4307 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
4308 !add_stripe_bio(sh
, bi
, dd_idx
, rw
)) {
4309 /* Stripe is busy expanding or
4310 * add failed due to overlap. Flush everything
4313 md_wakeup_thread(mddev
->thread
);
4318 finish_wait(&conf
->wait_for_overlap
, &w
);
4319 set_bit(STRIPE_HANDLE
, &sh
->state
);
4320 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4321 if ((bi
->bi_rw
& REQ_SYNC
) &&
4322 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4323 atomic_inc(&conf
->preread_active_stripes
);
4324 release_stripe_plug(mddev
, sh
);
4326 /* cannot get stripe for read-ahead, just give-up */
4327 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4328 finish_wait(&conf
->wait_for_overlap
, &w
);
4333 remaining
= raid5_dec_bi_active_stripes(bi
);
4334 if (remaining
== 0) {
4337 md_write_end(mddev
);
4343 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
4345 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
4347 /* reshaping is quite different to recovery/resync so it is
4348 * handled quite separately ... here.
4350 * On each call to sync_request, we gather one chunk worth of
4351 * destination stripes and flag them as expanding.
4352 * Then we find all the source stripes and request reads.
4353 * As the reads complete, handle_stripe will copy the data
4354 * into the destination stripe and release that stripe.
4356 struct r5conf
*conf
= mddev
->private;
4357 struct stripe_head
*sh
;
4358 sector_t first_sector
, last_sector
;
4359 int raid_disks
= conf
->previous_raid_disks
;
4360 int data_disks
= raid_disks
- conf
->max_degraded
;
4361 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4364 sector_t writepos
, readpos
, safepos
;
4365 sector_t stripe_addr
;
4366 int reshape_sectors
;
4367 struct list_head stripes
;
4369 if (sector_nr
== 0) {
4370 /* If restarting in the middle, skip the initial sectors */
4371 if (mddev
->reshape_backwards
&&
4372 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
4373 sector_nr
= raid5_size(mddev
, 0, 0)
4374 - conf
->reshape_progress
;
4375 } else if (!mddev
->reshape_backwards
&&
4376 conf
->reshape_progress
> 0)
4377 sector_nr
= conf
->reshape_progress
;
4378 sector_div(sector_nr
, new_data_disks
);
4380 mddev
->curr_resync_completed
= sector_nr
;
4381 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4387 /* We need to process a full chunk at a time.
4388 * If old and new chunk sizes differ, we need to process the
4391 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
)
4392 reshape_sectors
= mddev
->new_chunk_sectors
;
4394 reshape_sectors
= mddev
->chunk_sectors
;
4396 /* We update the metadata at least every 10 seconds, or when
4397 * the data about to be copied would over-write the source of
4398 * the data at the front of the range. i.e. one new_stripe
4399 * along from reshape_progress new_maps to after where
4400 * reshape_safe old_maps to
4402 writepos
= conf
->reshape_progress
;
4403 sector_div(writepos
, new_data_disks
);
4404 readpos
= conf
->reshape_progress
;
4405 sector_div(readpos
, data_disks
);
4406 safepos
= conf
->reshape_safe
;
4407 sector_div(safepos
, data_disks
);
4408 if (mddev
->reshape_backwards
) {
4409 writepos
-= min_t(sector_t
, reshape_sectors
, writepos
);
4410 readpos
+= reshape_sectors
;
4411 safepos
+= reshape_sectors
;
4413 writepos
+= reshape_sectors
;
4414 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
4415 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
4418 /* Having calculated the 'writepos' possibly use it
4419 * to set 'stripe_addr' which is where we will write to.
4421 if (mddev
->reshape_backwards
) {
4422 BUG_ON(conf
->reshape_progress
== 0);
4423 stripe_addr
= writepos
;
4424 BUG_ON((mddev
->dev_sectors
&
4425 ~((sector_t
)reshape_sectors
- 1))
4426 - reshape_sectors
- stripe_addr
4429 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
4430 stripe_addr
= sector_nr
;
4433 /* 'writepos' is the most advanced device address we might write.
4434 * 'readpos' is the least advanced device address we might read.
4435 * 'safepos' is the least address recorded in the metadata as having
4437 * If there is a min_offset_diff, these are adjusted either by
4438 * increasing the safepos/readpos if diff is negative, or
4439 * increasing writepos if diff is positive.
4440 * If 'readpos' is then behind 'writepos', there is no way that we can
4441 * ensure safety in the face of a crash - that must be done by userspace
4442 * making a backup of the data. So in that case there is no particular
4443 * rush to update metadata.
4444 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4445 * update the metadata to advance 'safepos' to match 'readpos' so that
4446 * we can be safe in the event of a crash.
4447 * So we insist on updating metadata if safepos is behind writepos and
4448 * readpos is beyond writepos.
4449 * In any case, update the metadata every 10 seconds.
4450 * Maybe that number should be configurable, but I'm not sure it is
4451 * worth it.... maybe it could be a multiple of safemode_delay???
4453 if (conf
->min_offset_diff
< 0) {
4454 safepos
+= -conf
->min_offset_diff
;
4455 readpos
+= -conf
->min_offset_diff
;
4457 writepos
+= conf
->min_offset_diff
;
4459 if ((mddev
->reshape_backwards
4460 ? (safepos
> writepos
&& readpos
< writepos
)
4461 : (safepos
< writepos
&& readpos
> writepos
)) ||
4462 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4463 /* Cannot proceed until we've updated the superblock... */
4464 wait_event(conf
->wait_for_overlap
,
4465 atomic_read(&conf
->reshape_stripes
)==0);
4466 mddev
->reshape_position
= conf
->reshape_progress
;
4467 mddev
->curr_resync_completed
= sector_nr
;
4468 conf
->reshape_checkpoint
= jiffies
;
4469 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4470 md_wakeup_thread(mddev
->thread
);
4471 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4472 kthread_should_stop());
4473 spin_lock_irq(&conf
->device_lock
);
4474 conf
->reshape_safe
= mddev
->reshape_position
;
4475 spin_unlock_irq(&conf
->device_lock
);
4476 wake_up(&conf
->wait_for_overlap
);
4477 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4480 INIT_LIST_HEAD(&stripes
);
4481 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
4483 int skipped_disk
= 0;
4484 sh
= get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
4485 set_bit(STRIPE_EXPANDING
, &sh
->state
);
4486 atomic_inc(&conf
->reshape_stripes
);
4487 /* If any of this stripe is beyond the end of the old
4488 * array, then we need to zero those blocks
4490 for (j
=sh
->disks
; j
--;) {
4492 if (j
== sh
->pd_idx
)
4494 if (conf
->level
== 6 &&
4497 s
= compute_blocknr(sh
, j
, 0);
4498 if (s
< raid5_size(mddev
, 0, 0)) {
4502 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
4503 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
4504 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
4506 if (!skipped_disk
) {
4507 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4508 set_bit(STRIPE_HANDLE
, &sh
->state
);
4510 list_add(&sh
->lru
, &stripes
);
4512 spin_lock_irq(&conf
->device_lock
);
4513 if (mddev
->reshape_backwards
)
4514 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
4516 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
4517 spin_unlock_irq(&conf
->device_lock
);
4518 /* Ok, those stripe are ready. We can start scheduling
4519 * reads on the source stripes.
4520 * The source stripes are determined by mapping the first and last
4521 * block on the destination stripes.
4524 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
4527 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
4528 * new_data_disks
- 1),
4530 if (last_sector
>= mddev
->dev_sectors
)
4531 last_sector
= mddev
->dev_sectors
- 1;
4532 while (first_sector
<= last_sector
) {
4533 sh
= get_active_stripe(conf
, first_sector
, 1, 0, 1);
4534 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4535 set_bit(STRIPE_HANDLE
, &sh
->state
);
4537 first_sector
+= STRIPE_SECTORS
;
4539 /* Now that the sources are clearly marked, we can release
4540 * the destination stripes
4542 while (!list_empty(&stripes
)) {
4543 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
4544 list_del_init(&sh
->lru
);
4547 /* If this takes us to the resync_max point where we have to pause,
4548 * then we need to write out the superblock.
4550 sector_nr
+= reshape_sectors
;
4551 if ((sector_nr
- mddev
->curr_resync_completed
) * 2
4552 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
4553 /* Cannot proceed until we've updated the superblock... */
4554 wait_event(conf
->wait_for_overlap
,
4555 atomic_read(&conf
->reshape_stripes
) == 0);
4556 mddev
->reshape_position
= conf
->reshape_progress
;
4557 mddev
->curr_resync_completed
= sector_nr
;
4558 conf
->reshape_checkpoint
= jiffies
;
4559 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4560 md_wakeup_thread(mddev
->thread
);
4561 wait_event(mddev
->sb_wait
,
4562 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
4563 || kthread_should_stop());
4564 spin_lock_irq(&conf
->device_lock
);
4565 conf
->reshape_safe
= mddev
->reshape_position
;
4566 spin_unlock_irq(&conf
->device_lock
);
4567 wake_up(&conf
->wait_for_overlap
);
4568 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4570 return reshape_sectors
;
4573 /* FIXME go_faster isn't used */
4574 static inline sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
4576 struct r5conf
*conf
= mddev
->private;
4577 struct stripe_head
*sh
;
4578 sector_t max_sector
= mddev
->dev_sectors
;
4579 sector_t sync_blocks
;
4580 int still_degraded
= 0;
4583 if (sector_nr
>= max_sector
) {
4584 /* just being told to finish up .. nothing much to do */
4586 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
4591 if (mddev
->curr_resync
< max_sector
) /* aborted */
4592 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
4594 else /* completed sync */
4596 bitmap_close_sync(mddev
->bitmap
);
4601 /* Allow raid5_quiesce to complete */
4602 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
4604 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4605 return reshape_request(mddev
, sector_nr
, skipped
);
4607 /* No need to check resync_max as we never do more than one
4608 * stripe, and as resync_max will always be on a chunk boundary,
4609 * if the check in md_do_sync didn't fire, there is no chance
4610 * of overstepping resync_max here
4613 /* if there is too many failed drives and we are trying
4614 * to resync, then assert that we are finished, because there is
4615 * nothing we can do.
4617 if (mddev
->degraded
>= conf
->max_degraded
&&
4618 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4619 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
4623 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
4624 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
4625 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
4626 /* we can skip this block, and probably more */
4627 sync_blocks
/= STRIPE_SECTORS
;
4629 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
4632 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
4634 sh
= get_active_stripe(conf
, sector_nr
, 0, 1, 0);
4636 sh
= get_active_stripe(conf
, sector_nr
, 0, 0, 0);
4637 /* make sure we don't swamp the stripe cache if someone else
4638 * is trying to get access
4640 schedule_timeout_uninterruptible(1);
4642 /* Need to check if array will still be degraded after recovery/resync
4643 * We don't need to check the 'failed' flag as when that gets set,
4646 for (i
= 0; i
< conf
->raid_disks
; i
++)
4647 if (conf
->disks
[i
].rdev
== NULL
)
4650 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
4652 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
4657 return STRIPE_SECTORS
;
4660 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
4662 /* We may not be able to submit a whole bio at once as there
4663 * may not be enough stripe_heads available.
4664 * We cannot pre-allocate enough stripe_heads as we may need
4665 * more than exist in the cache (if we allow ever large chunks).
4666 * So we do one stripe head at a time and record in
4667 * ->bi_hw_segments how many have been done.
4669 * We *know* that this entire raid_bio is in one chunk, so
4670 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4672 struct stripe_head
*sh
;
4674 sector_t sector
, logical_sector
, last_sector
;
4679 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4680 sector
= raid5_compute_sector(conf
, logical_sector
,
4682 last_sector
= raid_bio
->bi_sector
+ (raid_bio
->bi_size
>>9);
4684 for (; logical_sector
< last_sector
;
4685 logical_sector
+= STRIPE_SECTORS
,
4686 sector
+= STRIPE_SECTORS
,
4689 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
4690 /* already done this stripe */
4693 sh
= get_active_stripe(conf
, sector
, 0, 1, 0);
4696 /* failed to get a stripe - must wait */
4697 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
4698 conf
->retry_read_aligned
= raid_bio
;
4702 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
4704 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
4705 conf
->retry_read_aligned
= raid_bio
;
4709 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
4714 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
4716 bio_endio(raid_bio
, 0);
4717 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4718 wake_up(&conf
->wait_for_stripe
);
4722 #define MAX_STRIPE_BATCH 8
4723 static int handle_active_stripes(struct r5conf
*conf
)
4725 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
4726 int i
, batch_size
= 0;
4728 while (batch_size
< MAX_STRIPE_BATCH
&&
4729 (sh
= __get_priority_stripe(conf
)) != NULL
)
4730 batch
[batch_size
++] = sh
;
4732 if (batch_size
== 0)
4734 spin_unlock_irq(&conf
->device_lock
);
4736 for (i
= 0; i
< batch_size
; i
++)
4737 handle_stripe(batch
[i
]);
4741 spin_lock_irq(&conf
->device_lock
);
4742 for (i
= 0; i
< batch_size
; i
++)
4743 __release_stripe(conf
, batch
[i
]);
4748 * This is our raid5 kernel thread.
4750 * We scan the hash table for stripes which can be handled now.
4751 * During the scan, completed stripes are saved for us by the interrupt
4752 * handler, so that they will not have to wait for our next wakeup.
4754 static void raid5d(struct md_thread
*thread
)
4756 struct mddev
*mddev
= thread
->mddev
;
4757 struct r5conf
*conf
= mddev
->private;
4759 struct blk_plug plug
;
4761 pr_debug("+++ raid5d active\n");
4763 md_check_recovery(mddev
);
4765 blk_start_plug(&plug
);
4767 spin_lock_irq(&conf
->device_lock
);
4773 !list_empty(&conf
->bitmap_list
)) {
4774 /* Now is a good time to flush some bitmap updates */
4776 spin_unlock_irq(&conf
->device_lock
);
4777 bitmap_unplug(mddev
->bitmap
);
4778 spin_lock_irq(&conf
->device_lock
);
4779 conf
->seq_write
= conf
->seq_flush
;
4780 activate_bit_delay(conf
);
4782 raid5_activate_delayed(conf
);
4784 while ((bio
= remove_bio_from_retry(conf
))) {
4786 spin_unlock_irq(&conf
->device_lock
);
4787 ok
= retry_aligned_read(conf
, bio
);
4788 spin_lock_irq(&conf
->device_lock
);
4794 batch_size
= handle_active_stripes(conf
);
4797 handled
+= batch_size
;
4799 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
)) {
4800 spin_unlock_irq(&conf
->device_lock
);
4801 md_check_recovery(mddev
);
4802 spin_lock_irq(&conf
->device_lock
);
4805 pr_debug("%d stripes handled\n", handled
);
4807 spin_unlock_irq(&conf
->device_lock
);
4809 async_tx_issue_pending_all();
4810 blk_finish_plug(&plug
);
4812 pr_debug("--- raid5d inactive\n");
4816 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
4818 struct r5conf
*conf
= mddev
->private;
4820 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
4826 raid5_set_cache_size(struct mddev
*mddev
, int size
)
4828 struct r5conf
*conf
= mddev
->private;
4831 if (size
<= 16 || size
> 32768)
4833 while (size
< conf
->max_nr_stripes
) {
4834 if (drop_one_stripe(conf
))
4835 conf
->max_nr_stripes
--;
4839 err
= md_allow_write(mddev
);
4842 while (size
> conf
->max_nr_stripes
) {
4843 if (grow_one_stripe(conf
))
4844 conf
->max_nr_stripes
++;
4849 EXPORT_SYMBOL(raid5_set_cache_size
);
4852 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
4854 struct r5conf
*conf
= mddev
->private;
4858 if (len
>= PAGE_SIZE
)
4863 if (strict_strtoul(page
, 10, &new))
4865 err
= raid5_set_cache_size(mddev
, new);
4871 static struct md_sysfs_entry
4872 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
4873 raid5_show_stripe_cache_size
,
4874 raid5_store_stripe_cache_size
);
4877 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
4879 struct r5conf
*conf
= mddev
->private;
4881 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
4887 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
4889 struct r5conf
*conf
= mddev
->private;
4891 if (len
>= PAGE_SIZE
)
4896 if (strict_strtoul(page
, 10, &new))
4898 if (new > conf
->max_nr_stripes
)
4900 conf
->bypass_threshold
= new;
4904 static struct md_sysfs_entry
4905 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
4907 raid5_show_preread_threshold
,
4908 raid5_store_preread_threshold
);
4911 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
4913 struct r5conf
*conf
= mddev
->private;
4915 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
4920 static struct md_sysfs_entry
4921 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
4923 static struct attribute
*raid5_attrs
[] = {
4924 &raid5_stripecache_size
.attr
,
4925 &raid5_stripecache_active
.attr
,
4926 &raid5_preread_bypass_threshold
.attr
,
4929 static struct attribute_group raid5_attrs_group
= {
4931 .attrs
= raid5_attrs
,
4935 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
4937 struct r5conf
*conf
= mddev
->private;
4940 sectors
= mddev
->dev_sectors
;
4942 /* size is defined by the smallest of previous and new size */
4943 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
4945 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
4946 sectors
&= ~((sector_t
)mddev
->new_chunk_sectors
- 1);
4947 return sectors
* (raid_disks
- conf
->max_degraded
);
4950 static void raid5_free_percpu(struct r5conf
*conf
)
4952 struct raid5_percpu
*percpu
;
4959 for_each_possible_cpu(cpu
) {
4960 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4961 safe_put_page(percpu
->spare_page
);
4962 kfree(percpu
->scribble
);
4964 #ifdef CONFIG_HOTPLUG_CPU
4965 unregister_cpu_notifier(&conf
->cpu_notify
);
4969 free_percpu(conf
->percpu
);
4972 static void free_conf(struct r5conf
*conf
)
4974 shrink_stripes(conf
);
4975 raid5_free_percpu(conf
);
4977 kfree(conf
->stripe_hashtbl
);
4981 #ifdef CONFIG_HOTPLUG_CPU
4982 static int raid456_cpu_notify(struct notifier_block
*nfb
, unsigned long action
,
4985 struct r5conf
*conf
= container_of(nfb
, struct r5conf
, cpu_notify
);
4986 long cpu
= (long)hcpu
;
4987 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4990 case CPU_UP_PREPARE
:
4991 case CPU_UP_PREPARE_FROZEN
:
4992 if (conf
->level
== 6 && !percpu
->spare_page
)
4993 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
4994 if (!percpu
->scribble
)
4995 percpu
->scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4997 if (!percpu
->scribble
||
4998 (conf
->level
== 6 && !percpu
->spare_page
)) {
4999 safe_put_page(percpu
->spare_page
);
5000 kfree(percpu
->scribble
);
5001 pr_err("%s: failed memory allocation for cpu%ld\n",
5003 return notifier_from_errno(-ENOMEM
);
5007 case CPU_DEAD_FROZEN
:
5008 safe_put_page(percpu
->spare_page
);
5009 kfree(percpu
->scribble
);
5010 percpu
->spare_page
= NULL
;
5011 percpu
->scribble
= NULL
;
5020 static int raid5_alloc_percpu(struct r5conf
*conf
)
5023 struct page
*spare_page
;
5024 struct raid5_percpu __percpu
*allcpus
;
5028 allcpus
= alloc_percpu(struct raid5_percpu
);
5031 conf
->percpu
= allcpus
;
5035 for_each_present_cpu(cpu
) {
5036 if (conf
->level
== 6) {
5037 spare_page
= alloc_page(GFP_KERNEL
);
5042 per_cpu_ptr(conf
->percpu
, cpu
)->spare_page
= spare_page
;
5044 scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
5049 per_cpu_ptr(conf
->percpu
, cpu
)->scribble
= scribble
;
5051 #ifdef CONFIG_HOTPLUG_CPU
5052 conf
->cpu_notify
.notifier_call
= raid456_cpu_notify
;
5053 conf
->cpu_notify
.priority
= 0;
5055 err
= register_cpu_notifier(&conf
->cpu_notify
);
5062 static struct r5conf
*setup_conf(struct mddev
*mddev
)
5064 struct r5conf
*conf
;
5065 int raid_disk
, memory
, max_disks
;
5066 struct md_rdev
*rdev
;
5067 struct disk_info
*disk
;
5070 if (mddev
->new_level
!= 5
5071 && mddev
->new_level
!= 4
5072 && mddev
->new_level
!= 6) {
5073 printk(KERN_ERR
"md/raid:%s: raid level not set to 4/5/6 (%d)\n",
5074 mdname(mddev
), mddev
->new_level
);
5075 return ERR_PTR(-EIO
);
5077 if ((mddev
->new_level
== 5
5078 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
5079 (mddev
->new_level
== 6
5080 && !algorithm_valid_raid6(mddev
->new_layout
))) {
5081 printk(KERN_ERR
"md/raid:%s: layout %d not supported\n",
5082 mdname(mddev
), mddev
->new_layout
);
5083 return ERR_PTR(-EIO
);
5085 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
5086 printk(KERN_ERR
"md/raid:%s: not enough configured devices (%d, minimum 4)\n",
5087 mdname(mddev
), mddev
->raid_disks
);
5088 return ERR_PTR(-EINVAL
);
5091 if (!mddev
->new_chunk_sectors
||
5092 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
5093 !is_power_of_2(mddev
->new_chunk_sectors
)) {
5094 printk(KERN_ERR
"md/raid:%s: invalid chunk size %d\n",
5095 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
5096 return ERR_PTR(-EINVAL
);
5099 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
5102 spin_lock_init(&conf
->device_lock
);
5103 init_waitqueue_head(&conf
->wait_for_stripe
);
5104 init_waitqueue_head(&conf
->wait_for_overlap
);
5105 INIT_LIST_HEAD(&conf
->handle_list
);
5106 INIT_LIST_HEAD(&conf
->hold_list
);
5107 INIT_LIST_HEAD(&conf
->delayed_list
);
5108 INIT_LIST_HEAD(&conf
->bitmap_list
);
5109 INIT_LIST_HEAD(&conf
->inactive_list
);
5110 atomic_set(&conf
->active_stripes
, 0);
5111 atomic_set(&conf
->preread_active_stripes
, 0);
5112 atomic_set(&conf
->active_aligned_reads
, 0);
5113 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
5114 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
5116 conf
->raid_disks
= mddev
->raid_disks
;
5117 if (mddev
->reshape_position
== MaxSector
)
5118 conf
->previous_raid_disks
= mddev
->raid_disks
;
5120 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
5121 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
5122 conf
->scribble_len
= scribble_len(max_disks
);
5124 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
5129 conf
->mddev
= mddev
;
5131 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
5134 conf
->level
= mddev
->new_level
;
5135 if (raid5_alloc_percpu(conf
) != 0)
5138 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
5140 rdev_for_each(rdev
, mddev
) {
5141 raid_disk
= rdev
->raid_disk
;
5142 if (raid_disk
>= max_disks
5145 disk
= conf
->disks
+ raid_disk
;
5147 if (test_bit(Replacement
, &rdev
->flags
)) {
5148 if (disk
->replacement
)
5150 disk
->replacement
= rdev
;
5157 if (test_bit(In_sync
, &rdev
->flags
)) {
5158 char b
[BDEVNAME_SIZE
];
5159 printk(KERN_INFO
"md/raid:%s: device %s operational as raid"
5161 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
5162 } else if (rdev
->saved_raid_disk
!= raid_disk
)
5163 /* Cannot rely on bitmap to complete recovery */
5167 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5168 conf
->level
= mddev
->new_level
;
5169 if (conf
->level
== 6)
5170 conf
->max_degraded
= 2;
5172 conf
->max_degraded
= 1;
5173 conf
->algorithm
= mddev
->new_layout
;
5174 conf
->max_nr_stripes
= NR_STRIPES
;
5175 conf
->reshape_progress
= mddev
->reshape_position
;
5176 if (conf
->reshape_progress
!= MaxSector
) {
5177 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
5178 conf
->prev_algo
= mddev
->layout
;
5181 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
5182 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
5183 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
5185 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5186 mdname(mddev
), memory
);
5189 printk(KERN_INFO
"md/raid:%s: allocated %dkB\n",
5190 mdname(mddev
), memory
);
5192 sprintf(pers_name
, "raid%d", mddev
->new_level
);
5193 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
5194 if (!conf
->thread
) {
5196 "md/raid:%s: couldn't allocate thread.\n",
5206 return ERR_PTR(-EIO
);
5208 return ERR_PTR(-ENOMEM
);
5212 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
5215 case ALGORITHM_PARITY_0
:
5216 if (raid_disk
< max_degraded
)
5219 case ALGORITHM_PARITY_N
:
5220 if (raid_disk
>= raid_disks
- max_degraded
)
5223 case ALGORITHM_PARITY_0_6
:
5224 if (raid_disk
== 0 ||
5225 raid_disk
== raid_disks
- 1)
5228 case ALGORITHM_LEFT_ASYMMETRIC_6
:
5229 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
5230 case ALGORITHM_LEFT_SYMMETRIC_6
:
5231 case ALGORITHM_RIGHT_SYMMETRIC_6
:
5232 if (raid_disk
== raid_disks
- 1)
5238 static int run(struct mddev
*mddev
)
5240 struct r5conf
*conf
;
5241 int working_disks
= 0;
5242 int dirty_parity_disks
= 0;
5243 struct md_rdev
*rdev
;
5244 sector_t reshape_offset
= 0;
5246 long long min_offset_diff
= 0;
5249 if (mddev
->recovery_cp
!= MaxSector
)
5250 printk(KERN_NOTICE
"md/raid:%s: not clean"
5251 " -- starting background reconstruction\n",
5254 rdev_for_each(rdev
, mddev
) {
5256 if (rdev
->raid_disk
< 0)
5258 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
5260 min_offset_diff
= diff
;
5262 } else if (mddev
->reshape_backwards
&&
5263 diff
< min_offset_diff
)
5264 min_offset_diff
= diff
;
5265 else if (!mddev
->reshape_backwards
&&
5266 diff
> min_offset_diff
)
5267 min_offset_diff
= diff
;
5270 if (mddev
->reshape_position
!= MaxSector
) {
5271 /* Check that we can continue the reshape.
5272 * Difficulties arise if the stripe we would write to
5273 * next is at or after the stripe we would read from next.
5274 * For a reshape that changes the number of devices, this
5275 * is only possible for a very short time, and mdadm makes
5276 * sure that time appears to have past before assembling
5277 * the array. So we fail if that time hasn't passed.
5278 * For a reshape that keeps the number of devices the same
5279 * mdadm must be monitoring the reshape can keeping the
5280 * critical areas read-only and backed up. It will start
5281 * the array in read-only mode, so we check for that.
5283 sector_t here_new
, here_old
;
5285 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
5287 if (mddev
->new_level
!= mddev
->level
) {
5288 printk(KERN_ERR
"md/raid:%s: unsupported reshape "
5289 "required - aborting.\n",
5293 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
5294 /* reshape_position must be on a new-stripe boundary, and one
5295 * further up in new geometry must map after here in old
5298 here_new
= mddev
->reshape_position
;
5299 if (sector_div(here_new
, mddev
->new_chunk_sectors
*
5300 (mddev
->raid_disks
- max_degraded
))) {
5301 printk(KERN_ERR
"md/raid:%s: reshape_position not "
5302 "on a stripe boundary\n", mdname(mddev
));
5305 reshape_offset
= here_new
* mddev
->new_chunk_sectors
;
5306 /* here_new is the stripe we will write to */
5307 here_old
= mddev
->reshape_position
;
5308 sector_div(here_old
, mddev
->chunk_sectors
*
5309 (old_disks
-max_degraded
));
5310 /* here_old is the first stripe that we might need to read
5312 if (mddev
->delta_disks
== 0) {
5313 if ((here_new
* mddev
->new_chunk_sectors
!=
5314 here_old
* mddev
->chunk_sectors
)) {
5315 printk(KERN_ERR
"md/raid:%s: reshape position is"
5316 " confused - aborting\n", mdname(mddev
));
5319 /* We cannot be sure it is safe to start an in-place
5320 * reshape. It is only safe if user-space is monitoring
5321 * and taking constant backups.
5322 * mdadm always starts a situation like this in
5323 * readonly mode so it can take control before
5324 * allowing any writes. So just check for that.
5326 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
5327 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
5328 /* not really in-place - so OK */;
5329 else if (mddev
->ro
== 0) {
5330 printk(KERN_ERR
"md/raid:%s: in-place reshape "
5331 "must be started in read-only mode "
5336 } else if (mddev
->reshape_backwards
5337 ? (here_new
* mddev
->new_chunk_sectors
+ min_offset_diff
<=
5338 here_old
* mddev
->chunk_sectors
)
5339 : (here_new
* mddev
->new_chunk_sectors
>=
5340 here_old
* mddev
->chunk_sectors
+ (-min_offset_diff
))) {
5341 /* Reading from the same stripe as writing to - bad */
5342 printk(KERN_ERR
"md/raid:%s: reshape_position too early for "
5343 "auto-recovery - aborting.\n",
5347 printk(KERN_INFO
"md/raid:%s: reshape will continue\n",
5349 /* OK, we should be able to continue; */
5351 BUG_ON(mddev
->level
!= mddev
->new_level
);
5352 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
5353 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
5354 BUG_ON(mddev
->delta_disks
!= 0);
5357 if (mddev
->private == NULL
)
5358 conf
= setup_conf(mddev
);
5360 conf
= mddev
->private;
5363 return PTR_ERR(conf
);
5365 conf
->min_offset_diff
= min_offset_diff
;
5366 mddev
->thread
= conf
->thread
;
5367 conf
->thread
= NULL
;
5368 mddev
->private = conf
;
5370 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
5372 rdev
= conf
->disks
[i
].rdev
;
5373 if (!rdev
&& conf
->disks
[i
].replacement
) {
5374 /* The replacement is all we have yet */
5375 rdev
= conf
->disks
[i
].replacement
;
5376 conf
->disks
[i
].replacement
= NULL
;
5377 clear_bit(Replacement
, &rdev
->flags
);
5378 conf
->disks
[i
].rdev
= rdev
;
5382 if (conf
->disks
[i
].replacement
&&
5383 conf
->reshape_progress
!= MaxSector
) {
5384 /* replacements and reshape simply do not mix. */
5385 printk(KERN_ERR
"md: cannot handle concurrent "
5386 "replacement and reshape.\n");
5389 if (test_bit(In_sync
, &rdev
->flags
)) {
5393 /* This disc is not fully in-sync. However if it
5394 * just stored parity (beyond the recovery_offset),
5395 * when we don't need to be concerned about the
5396 * array being dirty.
5397 * When reshape goes 'backwards', we never have
5398 * partially completed devices, so we only need
5399 * to worry about reshape going forwards.
5401 /* Hack because v0.91 doesn't store recovery_offset properly. */
5402 if (mddev
->major_version
== 0 &&
5403 mddev
->minor_version
> 90)
5404 rdev
->recovery_offset
= reshape_offset
;
5406 if (rdev
->recovery_offset
< reshape_offset
) {
5407 /* We need to check old and new layout */
5408 if (!only_parity(rdev
->raid_disk
,
5411 conf
->max_degraded
))
5414 if (!only_parity(rdev
->raid_disk
,
5416 conf
->previous_raid_disks
,
5417 conf
->max_degraded
))
5419 dirty_parity_disks
++;
5423 * 0 for a fully functional array, 1 or 2 for a degraded array.
5425 mddev
->degraded
= calc_degraded(conf
);
5427 if (has_failed(conf
)) {
5428 printk(KERN_ERR
"md/raid:%s: not enough operational devices"
5429 " (%d/%d failed)\n",
5430 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
5434 /* device size must be a multiple of chunk size */
5435 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
5436 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5438 if (mddev
->degraded
> dirty_parity_disks
&&
5439 mddev
->recovery_cp
!= MaxSector
) {
5440 if (mddev
->ok_start_degraded
)
5442 "md/raid:%s: starting dirty degraded array"
5443 " - data corruption possible.\n",
5447 "md/raid:%s: cannot start dirty degraded array.\n",
5453 if (mddev
->degraded
== 0)
5454 printk(KERN_INFO
"md/raid:%s: raid level %d active with %d out of %d"
5455 " devices, algorithm %d\n", mdname(mddev
), conf
->level
,
5456 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
5459 printk(KERN_ALERT
"md/raid:%s: raid level %d active with %d"
5460 " out of %d devices, algorithm %d\n",
5461 mdname(mddev
), conf
->level
,
5462 mddev
->raid_disks
- mddev
->degraded
,
5463 mddev
->raid_disks
, mddev
->new_layout
);
5465 print_raid5_conf(conf
);
5467 if (conf
->reshape_progress
!= MaxSector
) {
5468 conf
->reshape_safe
= conf
->reshape_progress
;
5469 atomic_set(&conf
->reshape_stripes
, 0);
5470 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5471 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5472 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5473 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5474 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5479 /* Ok, everything is just fine now */
5480 if (mddev
->to_remove
== &raid5_attrs_group
)
5481 mddev
->to_remove
= NULL
;
5482 else if (mddev
->kobj
.sd
&&
5483 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
5485 "raid5: failed to create sysfs attributes for %s\n",
5487 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5491 bool discard_supported
= true;
5492 /* read-ahead size must cover two whole stripes, which
5493 * is 2 * (datadisks) * chunksize where 'n' is the
5494 * number of raid devices
5496 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
5497 int stripe
= data_disks
*
5498 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
5499 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5500 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5502 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
5504 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5505 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
5507 chunk_size
= mddev
->chunk_sectors
<< 9;
5508 blk_queue_io_min(mddev
->queue
, chunk_size
);
5509 blk_queue_io_opt(mddev
->queue
, chunk_size
*
5510 (conf
->raid_disks
- conf
->max_degraded
));
5512 * We can only discard a whole stripe. It doesn't make sense to
5513 * discard data disk but write parity disk
5515 stripe
= stripe
* PAGE_SIZE
;
5516 /* Round up to power of 2, as discard handling
5517 * currently assumes that */
5518 while ((stripe
-1) & stripe
)
5519 stripe
= (stripe
| (stripe
-1)) + 1;
5520 mddev
->queue
->limits
.discard_alignment
= stripe
;
5521 mddev
->queue
->limits
.discard_granularity
= stripe
;
5523 * unaligned part of discard request will be ignored, so can't
5524 * guarantee discard_zerors_data
5526 mddev
->queue
->limits
.discard_zeroes_data
= 0;
5528 rdev_for_each(rdev
, mddev
) {
5529 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5530 rdev
->data_offset
<< 9);
5531 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5532 rdev
->new_data_offset
<< 9);
5534 * discard_zeroes_data is required, otherwise data
5535 * could be lost. Consider a scenario: discard a stripe
5536 * (the stripe could be inconsistent if
5537 * discard_zeroes_data is 0); write one disk of the
5538 * stripe (the stripe could be inconsistent again
5539 * depending on which disks are used to calculate
5540 * parity); the disk is broken; The stripe data of this
5543 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
5544 !bdev_get_queue(rdev
->bdev
)->
5545 limits
.discard_zeroes_data
)
5546 discard_supported
= false;
5549 if (discard_supported
&&
5550 mddev
->queue
->limits
.max_discard_sectors
>= stripe
&&
5551 mddev
->queue
->limits
.discard_granularity
>= stripe
)
5552 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
5555 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
5561 md_unregister_thread(&mddev
->thread
);
5562 print_raid5_conf(conf
);
5564 mddev
->private = NULL
;
5565 printk(KERN_ALERT
"md/raid:%s: failed to run raid set.\n", mdname(mddev
));
5569 static int stop(struct mddev
*mddev
)
5571 struct r5conf
*conf
= mddev
->private;
5573 md_unregister_thread(&mddev
->thread
);
5575 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5577 mddev
->private = NULL
;
5578 mddev
->to_remove
= &raid5_attrs_group
;
5582 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
5584 struct r5conf
*conf
= mddev
->private;
5587 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
5588 mddev
->chunk_sectors
/ 2, mddev
->layout
);
5589 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
5590 for (i
= 0; i
< conf
->raid_disks
; i
++)
5591 seq_printf (seq
, "%s",
5592 conf
->disks
[i
].rdev
&&
5593 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
5594 seq_printf (seq
, "]");
5597 static void print_raid5_conf (struct r5conf
*conf
)
5600 struct disk_info
*tmp
;
5602 printk(KERN_DEBUG
"RAID conf printout:\n");
5604 printk("(conf==NULL)\n");
5607 printk(KERN_DEBUG
" --- level:%d rd:%d wd:%d\n", conf
->level
,
5609 conf
->raid_disks
- conf
->mddev
->degraded
);
5611 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5612 char b
[BDEVNAME_SIZE
];
5613 tmp
= conf
->disks
+ i
;
5615 printk(KERN_DEBUG
" disk %d, o:%d, dev:%s\n",
5616 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
5617 bdevname(tmp
->rdev
->bdev
, b
));
5621 static int raid5_spare_active(struct mddev
*mddev
)
5624 struct r5conf
*conf
= mddev
->private;
5625 struct disk_info
*tmp
;
5627 unsigned long flags
;
5629 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5630 tmp
= conf
->disks
+ i
;
5631 if (tmp
->replacement
5632 && tmp
->replacement
->recovery_offset
== MaxSector
5633 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
5634 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
5635 /* Replacement has just become active. */
5637 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
5640 /* Replaced device not technically faulty,
5641 * but we need to be sure it gets removed
5642 * and never re-added.
5644 set_bit(Faulty
, &tmp
->rdev
->flags
);
5645 sysfs_notify_dirent_safe(
5646 tmp
->rdev
->sysfs_state
);
5648 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
5649 } else if (tmp
->rdev
5650 && tmp
->rdev
->recovery_offset
== MaxSector
5651 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
5652 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
5654 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
5657 spin_lock_irqsave(&conf
->device_lock
, flags
);
5658 mddev
->degraded
= calc_degraded(conf
);
5659 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5660 print_raid5_conf(conf
);
5664 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5666 struct r5conf
*conf
= mddev
->private;
5668 int number
= rdev
->raid_disk
;
5669 struct md_rdev
**rdevp
;
5670 struct disk_info
*p
= conf
->disks
+ number
;
5672 print_raid5_conf(conf
);
5673 if (rdev
== p
->rdev
)
5675 else if (rdev
== p
->replacement
)
5676 rdevp
= &p
->replacement
;
5680 if (number
>= conf
->raid_disks
&&
5681 conf
->reshape_progress
== MaxSector
)
5682 clear_bit(In_sync
, &rdev
->flags
);
5684 if (test_bit(In_sync
, &rdev
->flags
) ||
5685 atomic_read(&rdev
->nr_pending
)) {
5689 /* Only remove non-faulty devices if recovery
5692 if (!test_bit(Faulty
, &rdev
->flags
) &&
5693 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
5694 !has_failed(conf
) &&
5695 (!p
->replacement
|| p
->replacement
== rdev
) &&
5696 number
< conf
->raid_disks
) {
5702 if (atomic_read(&rdev
->nr_pending
)) {
5703 /* lost the race, try later */
5706 } else if (p
->replacement
) {
5707 /* We must have just cleared 'rdev' */
5708 p
->rdev
= p
->replacement
;
5709 clear_bit(Replacement
, &p
->replacement
->flags
);
5710 smp_mb(); /* Make sure other CPUs may see both as identical
5711 * but will never see neither - if they are careful
5713 p
->replacement
= NULL
;
5714 clear_bit(WantReplacement
, &rdev
->flags
);
5716 /* We might have just removed the Replacement as faulty-
5717 * clear the bit just in case
5719 clear_bit(WantReplacement
, &rdev
->flags
);
5722 print_raid5_conf(conf
);
5726 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5728 struct r5conf
*conf
= mddev
->private;
5731 struct disk_info
*p
;
5733 int last
= conf
->raid_disks
- 1;
5735 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
5738 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
5739 /* no point adding a device */
5742 if (rdev
->raid_disk
>= 0)
5743 first
= last
= rdev
->raid_disk
;
5746 * find the disk ... but prefer rdev->saved_raid_disk
5749 if (rdev
->saved_raid_disk
>= 0 &&
5750 rdev
->saved_raid_disk
>= first
&&
5751 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
5752 first
= rdev
->saved_raid_disk
;
5754 for (disk
= first
; disk
<= last
; disk
++) {
5755 p
= conf
->disks
+ disk
;
5756 if (p
->rdev
== NULL
) {
5757 clear_bit(In_sync
, &rdev
->flags
);
5758 rdev
->raid_disk
= disk
;
5760 if (rdev
->saved_raid_disk
!= disk
)
5762 rcu_assign_pointer(p
->rdev
, rdev
);
5766 for (disk
= first
; disk
<= last
; disk
++) {
5767 p
= conf
->disks
+ disk
;
5768 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
5769 p
->replacement
== NULL
) {
5770 clear_bit(In_sync
, &rdev
->flags
);
5771 set_bit(Replacement
, &rdev
->flags
);
5772 rdev
->raid_disk
= disk
;
5775 rcu_assign_pointer(p
->replacement
, rdev
);
5780 print_raid5_conf(conf
);
5784 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
5786 /* no resync is happening, and there is enough space
5787 * on all devices, so we can resize.
5788 * We need to make sure resync covers any new space.
5789 * If the array is shrinking we should possibly wait until
5790 * any io in the removed space completes, but it hardly seems
5794 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
5795 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
5796 if (mddev
->external_size
&&
5797 mddev
->array_sectors
> newsize
)
5799 if (mddev
->bitmap
) {
5800 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
5804 md_set_array_sectors(mddev
, newsize
);
5805 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5806 revalidate_disk(mddev
->gendisk
);
5807 if (sectors
> mddev
->dev_sectors
&&
5808 mddev
->recovery_cp
> mddev
->dev_sectors
) {
5809 mddev
->recovery_cp
= mddev
->dev_sectors
;
5810 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5812 mddev
->dev_sectors
= sectors
;
5813 mddev
->resync_max_sectors
= sectors
;
5817 static int check_stripe_cache(struct mddev
*mddev
)
5819 /* Can only proceed if there are plenty of stripe_heads.
5820 * We need a minimum of one full stripe,, and for sensible progress
5821 * it is best to have about 4 times that.
5822 * If we require 4 times, then the default 256 4K stripe_heads will
5823 * allow for chunk sizes up to 256K, which is probably OK.
5824 * If the chunk size is greater, user-space should request more
5825 * stripe_heads first.
5827 struct r5conf
*conf
= mddev
->private;
5828 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5829 > conf
->max_nr_stripes
||
5830 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5831 > conf
->max_nr_stripes
) {
5832 printk(KERN_WARNING
"md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5834 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
5841 static int check_reshape(struct mddev
*mddev
)
5843 struct r5conf
*conf
= mddev
->private;
5845 if (mddev
->delta_disks
== 0 &&
5846 mddev
->new_layout
== mddev
->layout
&&
5847 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
5848 return 0; /* nothing to do */
5849 if (has_failed(conf
))
5851 if (mddev
->delta_disks
< 0) {
5852 /* We might be able to shrink, but the devices must
5853 * be made bigger first.
5854 * For raid6, 4 is the minimum size.
5855 * Otherwise 2 is the minimum
5858 if (mddev
->level
== 6)
5860 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
5864 if (!check_stripe_cache(mddev
))
5867 return resize_stripes(conf
, (conf
->previous_raid_disks
5868 + mddev
->delta_disks
));
5871 static int raid5_start_reshape(struct mddev
*mddev
)
5873 struct r5conf
*conf
= mddev
->private;
5874 struct md_rdev
*rdev
;
5876 unsigned long flags
;
5878 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5881 if (!check_stripe_cache(mddev
))
5884 if (has_failed(conf
))
5887 rdev_for_each(rdev
, mddev
) {
5888 if (!test_bit(In_sync
, &rdev
->flags
)
5889 && !test_bit(Faulty
, &rdev
->flags
))
5893 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
5894 /* Not enough devices even to make a degraded array
5899 /* Refuse to reduce size of the array. Any reductions in
5900 * array size must be through explicit setting of array_size
5903 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
5904 < mddev
->array_sectors
) {
5905 printk(KERN_ERR
"md/raid:%s: array size must be reduced "
5906 "before number of disks\n", mdname(mddev
));
5910 atomic_set(&conf
->reshape_stripes
, 0);
5911 spin_lock_irq(&conf
->device_lock
);
5912 conf
->previous_raid_disks
= conf
->raid_disks
;
5913 conf
->raid_disks
+= mddev
->delta_disks
;
5914 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
5915 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5916 conf
->prev_algo
= conf
->algorithm
;
5917 conf
->algorithm
= mddev
->new_layout
;
5919 /* Code that selects data_offset needs to see the generation update
5920 * if reshape_progress has been set - so a memory barrier needed.
5923 if (mddev
->reshape_backwards
)
5924 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
5926 conf
->reshape_progress
= 0;
5927 conf
->reshape_safe
= conf
->reshape_progress
;
5928 spin_unlock_irq(&conf
->device_lock
);
5930 /* Add some new drives, as many as will fit.
5931 * We know there are enough to make the newly sized array work.
5932 * Don't add devices if we are reducing the number of
5933 * devices in the array. This is because it is not possible
5934 * to correctly record the "partially reconstructed" state of
5935 * such devices during the reshape and confusion could result.
5937 if (mddev
->delta_disks
>= 0) {
5938 rdev_for_each(rdev
, mddev
)
5939 if (rdev
->raid_disk
< 0 &&
5940 !test_bit(Faulty
, &rdev
->flags
)) {
5941 if (raid5_add_disk(mddev
, rdev
) == 0) {
5943 >= conf
->previous_raid_disks
)
5944 set_bit(In_sync
, &rdev
->flags
);
5946 rdev
->recovery_offset
= 0;
5948 if (sysfs_link_rdev(mddev
, rdev
))
5949 /* Failure here is OK */;
5951 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
5952 && !test_bit(Faulty
, &rdev
->flags
)) {
5953 /* This is a spare that was manually added */
5954 set_bit(In_sync
, &rdev
->flags
);
5957 /* When a reshape changes the number of devices,
5958 * ->degraded is measured against the larger of the
5959 * pre and post number of devices.
5961 spin_lock_irqsave(&conf
->device_lock
, flags
);
5962 mddev
->degraded
= calc_degraded(conf
);
5963 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5965 mddev
->raid_disks
= conf
->raid_disks
;
5966 mddev
->reshape_position
= conf
->reshape_progress
;
5967 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5969 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5970 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5971 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5972 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5973 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5975 if (!mddev
->sync_thread
) {
5976 mddev
->recovery
= 0;
5977 spin_lock_irq(&conf
->device_lock
);
5978 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
5979 rdev_for_each(rdev
, mddev
)
5980 rdev
->new_data_offset
= rdev
->data_offset
;
5982 conf
->reshape_progress
= MaxSector
;
5983 mddev
->reshape_position
= MaxSector
;
5984 spin_unlock_irq(&conf
->device_lock
);
5987 conf
->reshape_checkpoint
= jiffies
;
5988 md_wakeup_thread(mddev
->sync_thread
);
5989 md_new_event(mddev
);
5993 /* This is called from the reshape thread and should make any
5994 * changes needed in 'conf'
5996 static void end_reshape(struct r5conf
*conf
)
5999 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
6000 struct md_rdev
*rdev
;
6002 spin_lock_irq(&conf
->device_lock
);
6003 conf
->previous_raid_disks
= conf
->raid_disks
;
6004 rdev_for_each(rdev
, conf
->mddev
)
6005 rdev
->data_offset
= rdev
->new_data_offset
;
6007 conf
->reshape_progress
= MaxSector
;
6008 spin_unlock_irq(&conf
->device_lock
);
6009 wake_up(&conf
->wait_for_overlap
);
6011 /* read-ahead size must cover two whole stripes, which is
6012 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6014 if (conf
->mddev
->queue
) {
6015 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
6016 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
6018 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
6019 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
6024 /* This is called from the raid5d thread with mddev_lock held.
6025 * It makes config changes to the device.
6027 static void raid5_finish_reshape(struct mddev
*mddev
)
6029 struct r5conf
*conf
= mddev
->private;
6031 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6033 if (mddev
->delta_disks
> 0) {
6034 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
6035 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6036 revalidate_disk(mddev
->gendisk
);
6039 spin_lock_irq(&conf
->device_lock
);
6040 mddev
->degraded
= calc_degraded(conf
);
6041 spin_unlock_irq(&conf
->device_lock
);
6042 for (d
= conf
->raid_disks
;
6043 d
< conf
->raid_disks
- mddev
->delta_disks
;
6045 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
6047 clear_bit(In_sync
, &rdev
->flags
);
6048 rdev
= conf
->disks
[d
].replacement
;
6050 clear_bit(In_sync
, &rdev
->flags
);
6053 mddev
->layout
= conf
->algorithm
;
6054 mddev
->chunk_sectors
= conf
->chunk_sectors
;
6055 mddev
->reshape_position
= MaxSector
;
6056 mddev
->delta_disks
= 0;
6057 mddev
->reshape_backwards
= 0;
6061 static void raid5_quiesce(struct mddev
*mddev
, int state
)
6063 struct r5conf
*conf
= mddev
->private;
6066 case 2: /* resume for a suspend */
6067 wake_up(&conf
->wait_for_overlap
);
6070 case 1: /* stop all writes */
6071 spin_lock_irq(&conf
->device_lock
);
6072 /* '2' tells resync/reshape to pause so that all
6073 * active stripes can drain
6076 wait_event_lock_irq(conf
->wait_for_stripe
,
6077 atomic_read(&conf
->active_stripes
) == 0 &&
6078 atomic_read(&conf
->active_aligned_reads
) == 0,
6081 spin_unlock_irq(&conf
->device_lock
);
6082 /* allow reshape to continue */
6083 wake_up(&conf
->wait_for_overlap
);
6086 case 0: /* re-enable writes */
6087 spin_lock_irq(&conf
->device_lock
);
6089 wake_up(&conf
->wait_for_stripe
);
6090 wake_up(&conf
->wait_for_overlap
);
6091 spin_unlock_irq(&conf
->device_lock
);
6097 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
6099 struct r0conf
*raid0_conf
= mddev
->private;
6102 /* for raid0 takeover only one zone is supported */
6103 if (raid0_conf
->nr_strip_zones
> 1) {
6104 printk(KERN_ERR
"md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6106 return ERR_PTR(-EINVAL
);
6109 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
6110 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
6111 mddev
->dev_sectors
= sectors
;
6112 mddev
->new_level
= level
;
6113 mddev
->new_layout
= ALGORITHM_PARITY_N
;
6114 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6115 mddev
->raid_disks
+= 1;
6116 mddev
->delta_disks
= 1;
6117 /* make sure it will be not marked as dirty */
6118 mddev
->recovery_cp
= MaxSector
;
6120 return setup_conf(mddev
);
6124 static void *raid5_takeover_raid1(struct mddev
*mddev
)
6128 if (mddev
->raid_disks
!= 2 ||
6129 mddev
->degraded
> 1)
6130 return ERR_PTR(-EINVAL
);
6132 /* Should check if there are write-behind devices? */
6134 chunksect
= 64*2; /* 64K by default */
6136 /* The array must be an exact multiple of chunksize */
6137 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
6140 if ((chunksect
<<9) < STRIPE_SIZE
)
6141 /* array size does not allow a suitable chunk size */
6142 return ERR_PTR(-EINVAL
);
6144 mddev
->new_level
= 5;
6145 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
6146 mddev
->new_chunk_sectors
= chunksect
;
6148 return setup_conf(mddev
);
6151 static void *raid5_takeover_raid6(struct mddev
*mddev
)
6155 switch (mddev
->layout
) {
6156 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6157 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
6159 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6160 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
6162 case ALGORITHM_LEFT_SYMMETRIC_6
:
6163 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
6165 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6166 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
6168 case ALGORITHM_PARITY_0_6
:
6169 new_layout
= ALGORITHM_PARITY_0
;
6171 case ALGORITHM_PARITY_N
:
6172 new_layout
= ALGORITHM_PARITY_N
;
6175 return ERR_PTR(-EINVAL
);
6177 mddev
->new_level
= 5;
6178 mddev
->new_layout
= new_layout
;
6179 mddev
->delta_disks
= -1;
6180 mddev
->raid_disks
-= 1;
6181 return setup_conf(mddev
);
6185 static int raid5_check_reshape(struct mddev
*mddev
)
6187 /* For a 2-drive array, the layout and chunk size can be changed
6188 * immediately as not restriping is needed.
6189 * For larger arrays we record the new value - after validation
6190 * to be used by a reshape pass.
6192 struct r5conf
*conf
= mddev
->private;
6193 int new_chunk
= mddev
->new_chunk_sectors
;
6195 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
6197 if (new_chunk
> 0) {
6198 if (!is_power_of_2(new_chunk
))
6200 if (new_chunk
< (PAGE_SIZE
>>9))
6202 if (mddev
->array_sectors
& (new_chunk
-1))
6203 /* not factor of array size */
6207 /* They look valid */
6209 if (mddev
->raid_disks
== 2) {
6210 /* can make the change immediately */
6211 if (mddev
->new_layout
>= 0) {
6212 conf
->algorithm
= mddev
->new_layout
;
6213 mddev
->layout
= mddev
->new_layout
;
6215 if (new_chunk
> 0) {
6216 conf
->chunk_sectors
= new_chunk
;
6217 mddev
->chunk_sectors
= new_chunk
;
6219 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6220 md_wakeup_thread(mddev
->thread
);
6222 return check_reshape(mddev
);
6225 static int raid6_check_reshape(struct mddev
*mddev
)
6227 int new_chunk
= mddev
->new_chunk_sectors
;
6229 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
6231 if (new_chunk
> 0) {
6232 if (!is_power_of_2(new_chunk
))
6234 if (new_chunk
< (PAGE_SIZE
>> 9))
6236 if (mddev
->array_sectors
& (new_chunk
-1))
6237 /* not factor of array size */
6241 /* They look valid */
6242 return check_reshape(mddev
);
6245 static void *raid5_takeover(struct mddev
*mddev
)
6247 /* raid5 can take over:
6248 * raid0 - if there is only one strip zone - make it a raid4 layout
6249 * raid1 - if there are two drives. We need to know the chunk size
6250 * raid4 - trivial - just use a raid4 layout.
6251 * raid6 - Providing it is a *_6 layout
6253 if (mddev
->level
== 0)
6254 return raid45_takeover_raid0(mddev
, 5);
6255 if (mddev
->level
== 1)
6256 return raid5_takeover_raid1(mddev
);
6257 if (mddev
->level
== 4) {
6258 mddev
->new_layout
= ALGORITHM_PARITY_N
;
6259 mddev
->new_level
= 5;
6260 return setup_conf(mddev
);
6262 if (mddev
->level
== 6)
6263 return raid5_takeover_raid6(mddev
);
6265 return ERR_PTR(-EINVAL
);
6268 static void *raid4_takeover(struct mddev
*mddev
)
6270 /* raid4 can take over:
6271 * raid0 - if there is only one strip zone
6272 * raid5 - if layout is right
6274 if (mddev
->level
== 0)
6275 return raid45_takeover_raid0(mddev
, 4);
6276 if (mddev
->level
== 5 &&
6277 mddev
->layout
== ALGORITHM_PARITY_N
) {
6278 mddev
->new_layout
= 0;
6279 mddev
->new_level
= 4;
6280 return setup_conf(mddev
);
6282 return ERR_PTR(-EINVAL
);
6285 static struct md_personality raid5_personality
;
6287 static void *raid6_takeover(struct mddev
*mddev
)
6289 /* Currently can only take over a raid5. We map the
6290 * personality to an equivalent raid6 personality
6291 * with the Q block at the end.
6295 if (mddev
->pers
!= &raid5_personality
)
6296 return ERR_PTR(-EINVAL
);
6297 if (mddev
->degraded
> 1)
6298 return ERR_PTR(-EINVAL
);
6299 if (mddev
->raid_disks
> 253)
6300 return ERR_PTR(-EINVAL
);
6301 if (mddev
->raid_disks
< 3)
6302 return ERR_PTR(-EINVAL
);
6304 switch (mddev
->layout
) {
6305 case ALGORITHM_LEFT_ASYMMETRIC
:
6306 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
6308 case ALGORITHM_RIGHT_ASYMMETRIC
:
6309 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
6311 case ALGORITHM_LEFT_SYMMETRIC
:
6312 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
6314 case ALGORITHM_RIGHT_SYMMETRIC
:
6315 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
6317 case ALGORITHM_PARITY_0
:
6318 new_layout
= ALGORITHM_PARITY_0_6
;
6320 case ALGORITHM_PARITY_N
:
6321 new_layout
= ALGORITHM_PARITY_N
;
6324 return ERR_PTR(-EINVAL
);
6326 mddev
->new_level
= 6;
6327 mddev
->new_layout
= new_layout
;
6328 mddev
->delta_disks
= 1;
6329 mddev
->raid_disks
+= 1;
6330 return setup_conf(mddev
);
6334 static struct md_personality raid6_personality
=
6338 .owner
= THIS_MODULE
,
6339 .make_request
= make_request
,
6343 .error_handler
= error
,
6344 .hot_add_disk
= raid5_add_disk
,
6345 .hot_remove_disk
= raid5_remove_disk
,
6346 .spare_active
= raid5_spare_active
,
6347 .sync_request
= sync_request
,
6348 .resize
= raid5_resize
,
6350 .check_reshape
= raid6_check_reshape
,
6351 .start_reshape
= raid5_start_reshape
,
6352 .finish_reshape
= raid5_finish_reshape
,
6353 .quiesce
= raid5_quiesce
,
6354 .takeover
= raid6_takeover
,
6356 static struct md_personality raid5_personality
=
6360 .owner
= THIS_MODULE
,
6361 .make_request
= make_request
,
6365 .error_handler
= error
,
6366 .hot_add_disk
= raid5_add_disk
,
6367 .hot_remove_disk
= raid5_remove_disk
,
6368 .spare_active
= raid5_spare_active
,
6369 .sync_request
= sync_request
,
6370 .resize
= raid5_resize
,
6372 .check_reshape
= raid5_check_reshape
,
6373 .start_reshape
= raid5_start_reshape
,
6374 .finish_reshape
= raid5_finish_reshape
,
6375 .quiesce
= raid5_quiesce
,
6376 .takeover
= raid5_takeover
,
6379 static struct md_personality raid4_personality
=
6383 .owner
= THIS_MODULE
,
6384 .make_request
= make_request
,
6388 .error_handler
= error
,
6389 .hot_add_disk
= raid5_add_disk
,
6390 .hot_remove_disk
= raid5_remove_disk
,
6391 .spare_active
= raid5_spare_active
,
6392 .sync_request
= sync_request
,
6393 .resize
= raid5_resize
,
6395 .check_reshape
= raid5_check_reshape
,
6396 .start_reshape
= raid5_start_reshape
,
6397 .finish_reshape
= raid5_finish_reshape
,
6398 .quiesce
= raid5_quiesce
,
6399 .takeover
= raid4_takeover
,
6402 static int __init
raid5_init(void)
6404 register_md_personality(&raid6_personality
);
6405 register_md_personality(&raid5_personality
);
6406 register_md_personality(&raid4_personality
);
6410 static void raid5_exit(void)
6412 unregister_md_personality(&raid6_personality
);
6413 unregister_md_personality(&raid5_personality
);
6414 unregister_md_personality(&raid4_personality
);
6417 module_init(raid5_init
);
6418 module_exit(raid5_exit
);
6419 MODULE_LICENSE("GPL");
6420 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6421 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6422 MODULE_ALIAS("md-raid5");
6423 MODULE_ALIAS("md-raid4");
6424 MODULE_ALIAS("md-level-5");
6425 MODULE_ALIAS("md-level-4");
6426 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6427 MODULE_ALIAS("md-raid6");
6428 MODULE_ALIAS("md-level-6");
6430 /* This used to be two separate modules, they were: */
6431 MODULE_ALIAS("raid5");
6432 MODULE_ALIAS("raid6");