2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is seq_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
65 #define NR_STRIPES 256
66 #define STRIPE_SIZE PAGE_SIZE
67 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
68 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
69 #define IO_THRESHOLD 1
70 #define BYPASS_THRESHOLD 1
71 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
72 #define HASH_MASK (NR_HASH - 1)
74 static inline struct hlist_head
*stripe_hash(struct r5conf
*conf
, sector_t sect
)
76 int hash
= (sect
>> STRIPE_SHIFT
) & HASH_MASK
;
77 return &conf
->stripe_hashtbl
[hash
];
80 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
86 * This function is used to determine the 'next' bio in the list, given the sector
87 * of the current stripe+device
89 static inline struct bio
*r5_next_bio(struct bio
*bio
, sector_t sector
)
91 int sectors
= bio
->bi_size
>> 9;
92 if (bio
->bi_sector
+ sectors
< sector
+ STRIPE_SECTORS
)
99 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
102 static inline int raid5_bi_phys_segments(struct bio
*bio
)
104 return bio
->bi_phys_segments
& 0xffff;
107 static inline int raid5_bi_hw_segments(struct bio
*bio
)
109 return (bio
->bi_phys_segments
>> 16) & 0xffff;
112 static inline int raid5_dec_bi_phys_segments(struct bio
*bio
)
114 --bio
->bi_phys_segments
;
115 return raid5_bi_phys_segments(bio
);
118 static inline int raid5_dec_bi_hw_segments(struct bio
*bio
)
120 unsigned short val
= raid5_bi_hw_segments(bio
);
123 bio
->bi_phys_segments
= (val
<< 16) | raid5_bi_phys_segments(bio
);
127 static inline void raid5_set_bi_hw_segments(struct bio
*bio
, unsigned int cnt
)
129 bio
->bi_phys_segments
= raid5_bi_phys_segments(bio
) | (cnt
<< 16);
132 /* Find first data disk in a raid6 stripe */
133 static inline int raid6_d0(struct stripe_head
*sh
)
136 /* ddf always start from first device */
138 /* md starts just after Q block */
139 if (sh
->qd_idx
== sh
->disks
- 1)
142 return sh
->qd_idx
+ 1;
144 static inline int raid6_next_disk(int disk
, int raid_disks
)
147 return (disk
< raid_disks
) ? disk
: 0;
150 /* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
155 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
156 int *count
, int syndrome_disks
)
162 if (idx
== sh
->pd_idx
)
163 return syndrome_disks
;
164 if (idx
== sh
->qd_idx
)
165 return syndrome_disks
+ 1;
171 static void return_io(struct bio
*return_bi
)
173 struct bio
*bi
= return_bi
;
176 return_bi
= bi
->bi_next
;
184 static void print_raid5_conf (struct r5conf
*conf
);
186 static int stripe_operations_active(struct stripe_head
*sh
)
188 return sh
->check_state
|| sh
->reconstruct_state
||
189 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
190 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
193 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
)
195 if (atomic_dec_and_test(&sh
->count
)) {
196 BUG_ON(!list_empty(&sh
->lru
));
197 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
198 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
199 if (test_bit(STRIPE_DELAYED
, &sh
->state
))
200 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
201 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
202 sh
->bm_seq
- conf
->seq_write
> 0)
203 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
205 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
206 list_add_tail(&sh
->lru
, &conf
->handle_list
);
208 md_wakeup_thread(conf
->mddev
->thread
);
210 BUG_ON(stripe_operations_active(sh
));
211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
212 if (atomic_dec_return(&conf
->preread_active_stripes
)
214 md_wakeup_thread(conf
->mddev
->thread
);
215 atomic_dec(&conf
->active_stripes
);
216 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
217 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
218 wake_up(&conf
->wait_for_stripe
);
219 if (conf
->retry_read_aligned
)
220 md_wakeup_thread(conf
->mddev
->thread
);
226 static void release_stripe(struct stripe_head
*sh
)
228 struct r5conf
*conf
= sh
->raid_conf
;
231 spin_lock_irqsave(&conf
->device_lock
, flags
);
232 __release_stripe(conf
, sh
);
233 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
236 static inline void remove_hash(struct stripe_head
*sh
)
238 pr_debug("remove_hash(), stripe %llu\n",
239 (unsigned long long)sh
->sector
);
241 hlist_del_init(&sh
->hash
);
244 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
246 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
248 pr_debug("insert_hash(), stripe %llu\n",
249 (unsigned long long)sh
->sector
);
251 hlist_add_head(&sh
->hash
, hp
);
255 /* find an idle stripe, make sure it is unhashed, and return it. */
256 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
)
258 struct stripe_head
*sh
= NULL
;
259 struct list_head
*first
;
261 if (list_empty(&conf
->inactive_list
))
263 first
= conf
->inactive_list
.next
;
264 sh
= list_entry(first
, struct stripe_head
, lru
);
265 list_del_init(first
);
267 atomic_inc(&conf
->active_stripes
);
272 static void shrink_buffers(struct stripe_head
*sh
)
276 int num
= sh
->raid_conf
->pool_size
;
278 for (i
= 0; i
< num
; i
++) {
282 sh
->dev
[i
].page
= NULL
;
287 static int grow_buffers(struct stripe_head
*sh
)
290 int num
= sh
->raid_conf
->pool_size
;
292 for (i
= 0; i
< num
; i
++) {
295 if (!(page
= alloc_page(GFP_KERNEL
))) {
298 sh
->dev
[i
].page
= page
;
303 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
304 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
305 struct stripe_head
*sh
);
307 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
309 struct r5conf
*conf
= sh
->raid_conf
;
312 BUG_ON(atomic_read(&sh
->count
) != 0);
313 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
314 BUG_ON(stripe_operations_active(sh
));
316 pr_debug("init_stripe called, stripe %llu\n",
317 (unsigned long long)sh
->sector
);
321 sh
->generation
= conf
->generation
- previous
;
322 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
324 stripe_set_idx(sector
, conf
, previous
, sh
);
328 for (i
= sh
->disks
; i
--; ) {
329 struct r5dev
*dev
= &sh
->dev
[i
];
331 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
332 test_bit(R5_LOCKED
, &dev
->flags
)) {
333 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
334 (unsigned long long)sh
->sector
, i
, dev
->toread
,
335 dev
->read
, dev
->towrite
, dev
->written
,
336 test_bit(R5_LOCKED
, &dev
->flags
));
340 raid5_build_block(sh
, i
, previous
);
342 insert_hash(conf
, sh
);
345 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
348 struct stripe_head
*sh
;
349 struct hlist_node
*hn
;
351 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
352 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
353 if (sh
->sector
== sector
&& sh
->generation
== generation
)
355 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
360 * Need to check if array has failed when deciding whether to:
362 * - remove non-faulty devices
365 * This determination is simple when no reshape is happening.
366 * However if there is a reshape, we need to carefully check
367 * both the before and after sections.
368 * This is because some failed devices may only affect one
369 * of the two sections, and some non-in_sync devices may
370 * be insync in the section most affected by failed devices.
372 static int calc_degraded(struct r5conf
*conf
)
374 int degraded
, degraded2
;
379 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
380 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
381 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
383 else if (test_bit(In_sync
, &rdev
->flags
))
386 /* not in-sync or faulty.
387 * If the reshape increases the number of devices,
388 * this is being recovered by the reshape, so
389 * this 'previous' section is not in_sync.
390 * If the number of devices is being reduced however,
391 * the device can only be part of the array if
392 * we are reverting a reshape, so this section will
395 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
399 if (conf
->raid_disks
== conf
->previous_raid_disks
)
403 for (i
= 0; i
< conf
->raid_disks
; i
++) {
404 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
405 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
407 else if (test_bit(In_sync
, &rdev
->flags
))
410 /* not in-sync or faulty.
411 * If reshape increases the number of devices, this
412 * section has already been recovered, else it
413 * almost certainly hasn't.
415 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
419 if (degraded2
> degraded
)
424 static int has_failed(struct r5conf
*conf
)
428 if (conf
->mddev
->reshape_position
== MaxSector
)
429 return conf
->mddev
->degraded
> conf
->max_degraded
;
431 degraded
= calc_degraded(conf
);
432 if (degraded
> conf
->max_degraded
)
437 static struct stripe_head
*
438 get_active_stripe(struct r5conf
*conf
, sector_t sector
,
439 int previous
, int noblock
, int noquiesce
)
441 struct stripe_head
*sh
;
443 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
445 spin_lock_irq(&conf
->device_lock
);
448 wait_event_lock_irq(conf
->wait_for_stripe
,
449 conf
->quiesce
== 0 || noquiesce
,
450 conf
->device_lock
, /* nothing */);
451 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
453 if (!conf
->inactive_blocked
)
454 sh
= get_free_stripe(conf
);
455 if (noblock
&& sh
== NULL
)
458 conf
->inactive_blocked
= 1;
459 wait_event_lock_irq(conf
->wait_for_stripe
,
460 !list_empty(&conf
->inactive_list
) &&
461 (atomic_read(&conf
->active_stripes
)
462 < (conf
->max_nr_stripes
*3/4)
463 || !conf
->inactive_blocked
),
466 conf
->inactive_blocked
= 0;
468 init_stripe(sh
, sector
, previous
);
470 if (atomic_read(&sh
->count
)) {
471 BUG_ON(!list_empty(&sh
->lru
)
472 && !test_bit(STRIPE_EXPANDING
, &sh
->state
));
474 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
475 atomic_inc(&conf
->active_stripes
);
476 if (list_empty(&sh
->lru
) &&
477 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
479 list_del_init(&sh
->lru
);
482 } while (sh
== NULL
);
485 atomic_inc(&sh
->count
);
487 spin_unlock_irq(&conf
->device_lock
);
492 raid5_end_read_request(struct bio
*bi
, int error
);
494 raid5_end_write_request(struct bio
*bi
, int error
);
496 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
498 struct r5conf
*conf
= sh
->raid_conf
;
499 int i
, disks
= sh
->disks
;
503 for (i
= disks
; i
--; ) {
505 int replace_only
= 0;
506 struct bio
*bi
, *rbi
;
507 struct md_rdev
*rdev
, *rrdev
= NULL
;
508 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
509 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
513 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
515 else if (test_and_clear_bit(R5_WantReplace
,
516 &sh
->dev
[i
].flags
)) {
522 bi
= &sh
->dev
[i
].req
;
523 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
528 bi
->bi_end_io
= raid5_end_write_request
;
529 rbi
->bi_end_io
= raid5_end_write_request
;
531 bi
->bi_end_io
= raid5_end_read_request
;
534 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
535 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
536 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
545 /* We raced and saw duplicates */
548 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
) && rrdev
)
553 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
556 atomic_inc(&rdev
->nr_pending
);
557 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
560 atomic_inc(&rrdev
->nr_pending
);
563 /* We have already checked bad blocks for reads. Now
564 * need to check for writes. We never accept write errors
565 * on the replacement, so we don't to check rrdev.
567 while ((rw
& WRITE
) && rdev
&&
568 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
571 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
572 &first_bad
, &bad_sectors
);
577 set_bit(BlockedBadBlocks
, &rdev
->flags
);
578 if (!conf
->mddev
->external
&&
579 conf
->mddev
->flags
) {
580 /* It is very unlikely, but we might
581 * still need to write out the
582 * bad block log - better give it
584 md_check_recovery(conf
->mddev
);
586 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
588 /* Acknowledged bad block - skip the write */
589 rdev_dec_pending(rdev
, conf
->mddev
);
595 if (s
->syncing
|| s
->expanding
|| s
->expanded
597 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
599 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
601 bi
->bi_bdev
= rdev
->bdev
;
602 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
603 __func__
, (unsigned long long)sh
->sector
,
605 atomic_inc(&sh
->count
);
606 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
607 bi
->bi_flags
= 1 << BIO_UPTODATE
;
609 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
610 bi
->bi_io_vec
[0].bv_offset
= 0;
611 bi
->bi_size
= STRIPE_SIZE
;
614 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
615 generic_make_request(bi
);
618 if (s
->syncing
|| s
->expanding
|| s
->expanded
620 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
622 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
624 rbi
->bi_bdev
= rrdev
->bdev
;
625 pr_debug("%s: for %llu schedule op %ld on "
626 "replacement disc %d\n",
627 __func__
, (unsigned long long)sh
->sector
,
629 atomic_inc(&sh
->count
);
630 rbi
->bi_sector
= sh
->sector
+ rrdev
->data_offset
;
631 rbi
->bi_flags
= 1 << BIO_UPTODATE
;
633 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
634 rbi
->bi_io_vec
[0].bv_offset
= 0;
635 rbi
->bi_size
= STRIPE_SIZE
;
637 generic_make_request(rbi
);
639 if (!rdev
&& !rrdev
) {
641 set_bit(STRIPE_DEGRADED
, &sh
->state
);
642 pr_debug("skip op %ld on disc %d for sector %llu\n",
643 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
644 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
645 set_bit(STRIPE_HANDLE
, &sh
->state
);
650 static struct dma_async_tx_descriptor
*
651 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
652 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
655 struct page
*bio_page
;
658 struct async_submit_ctl submit
;
659 enum async_tx_flags flags
= 0;
661 if (bio
->bi_sector
>= sector
)
662 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
664 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
667 flags
|= ASYNC_TX_FENCE
;
668 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
670 bio_for_each_segment(bvl
, bio
, i
) {
671 int len
= bvl
->bv_len
;
675 if (page_offset
< 0) {
676 b_offset
= -page_offset
;
677 page_offset
+= b_offset
;
681 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
682 clen
= STRIPE_SIZE
- page_offset
;
687 b_offset
+= bvl
->bv_offset
;
688 bio_page
= bvl
->bv_page
;
690 tx
= async_memcpy(page
, bio_page
, page_offset
,
691 b_offset
, clen
, &submit
);
693 tx
= async_memcpy(bio_page
, page
, b_offset
,
694 page_offset
, clen
, &submit
);
696 /* chain the operations */
697 submit
.depend_tx
= tx
;
699 if (clen
< len
) /* hit end of page */
707 static void ops_complete_biofill(void *stripe_head_ref
)
709 struct stripe_head
*sh
= stripe_head_ref
;
710 struct bio
*return_bi
= NULL
;
711 struct r5conf
*conf
= sh
->raid_conf
;
714 pr_debug("%s: stripe %llu\n", __func__
,
715 (unsigned long long)sh
->sector
);
717 /* clear completed biofills */
718 spin_lock_irq(&conf
->device_lock
);
719 for (i
= sh
->disks
; i
--; ) {
720 struct r5dev
*dev
= &sh
->dev
[i
];
722 /* acknowledge completion of a biofill operation */
723 /* and check if we need to reply to a read request,
724 * new R5_Wantfill requests are held off until
725 * !STRIPE_BIOFILL_RUN
727 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
728 struct bio
*rbi
, *rbi2
;
733 while (rbi
&& rbi
->bi_sector
<
734 dev
->sector
+ STRIPE_SECTORS
) {
735 rbi2
= r5_next_bio(rbi
, dev
->sector
);
736 if (!raid5_dec_bi_phys_segments(rbi
)) {
737 rbi
->bi_next
= return_bi
;
744 spin_unlock_irq(&conf
->device_lock
);
745 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
747 return_io(return_bi
);
749 set_bit(STRIPE_HANDLE
, &sh
->state
);
753 static void ops_run_biofill(struct stripe_head
*sh
)
755 struct dma_async_tx_descriptor
*tx
= NULL
;
756 struct r5conf
*conf
= sh
->raid_conf
;
757 struct async_submit_ctl submit
;
760 pr_debug("%s: stripe %llu\n", __func__
,
761 (unsigned long long)sh
->sector
);
763 for (i
= sh
->disks
; i
--; ) {
764 struct r5dev
*dev
= &sh
->dev
[i
];
765 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
767 spin_lock_irq(&conf
->device_lock
);
768 dev
->read
= rbi
= dev
->toread
;
770 spin_unlock_irq(&conf
->device_lock
);
771 while (rbi
&& rbi
->bi_sector
<
772 dev
->sector
+ STRIPE_SECTORS
) {
773 tx
= async_copy_data(0, rbi
, dev
->page
,
775 rbi
= r5_next_bio(rbi
, dev
->sector
);
780 atomic_inc(&sh
->count
);
781 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
782 async_trigger_callback(&submit
);
785 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
792 tgt
= &sh
->dev
[target
];
793 set_bit(R5_UPTODATE
, &tgt
->flags
);
794 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
795 clear_bit(R5_Wantcompute
, &tgt
->flags
);
798 static void ops_complete_compute(void *stripe_head_ref
)
800 struct stripe_head
*sh
= stripe_head_ref
;
802 pr_debug("%s: stripe %llu\n", __func__
,
803 (unsigned long long)sh
->sector
);
805 /* mark the computed target(s) as uptodate */
806 mark_target_uptodate(sh
, sh
->ops
.target
);
807 mark_target_uptodate(sh
, sh
->ops
.target2
);
809 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
810 if (sh
->check_state
== check_state_compute_run
)
811 sh
->check_state
= check_state_compute_result
;
812 set_bit(STRIPE_HANDLE
, &sh
->state
);
816 /* return a pointer to the address conversion region of the scribble buffer */
817 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
818 struct raid5_percpu
*percpu
)
820 return percpu
->scribble
+ sizeof(struct page
*) * (sh
->disks
+ 2);
823 static struct dma_async_tx_descriptor
*
824 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
826 int disks
= sh
->disks
;
827 struct page
**xor_srcs
= percpu
->scribble
;
828 int target
= sh
->ops
.target
;
829 struct r5dev
*tgt
= &sh
->dev
[target
];
830 struct page
*xor_dest
= tgt
->page
;
832 struct dma_async_tx_descriptor
*tx
;
833 struct async_submit_ctl submit
;
836 pr_debug("%s: stripe %llu block: %d\n",
837 __func__
, (unsigned long long)sh
->sector
, target
);
838 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
840 for (i
= disks
; i
--; )
842 xor_srcs
[count
++] = sh
->dev
[i
].page
;
844 atomic_inc(&sh
->count
);
846 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
847 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
));
848 if (unlikely(count
== 1))
849 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
851 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
856 /* set_syndrome_sources - populate source buffers for gen_syndrome
857 * @srcs - (struct page *) array of size sh->disks
858 * @sh - stripe_head to parse
860 * Populates srcs in proper layout order for the stripe and returns the
861 * 'count' of sources to be used in a call to async_gen_syndrome. The P
862 * destination buffer is recorded in srcs[count] and the Q destination
863 * is recorded in srcs[count+1]].
865 static int set_syndrome_sources(struct page
**srcs
, struct stripe_head
*sh
)
867 int disks
= sh
->disks
;
868 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
869 int d0_idx
= raid6_d0(sh
);
873 for (i
= 0; i
< disks
; i
++)
879 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
881 srcs
[slot
] = sh
->dev
[i
].page
;
882 i
= raid6_next_disk(i
, disks
);
883 } while (i
!= d0_idx
);
885 return syndrome_disks
;
888 static struct dma_async_tx_descriptor
*
889 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
891 int disks
= sh
->disks
;
892 struct page
**blocks
= percpu
->scribble
;
894 int qd_idx
= sh
->qd_idx
;
895 struct dma_async_tx_descriptor
*tx
;
896 struct async_submit_ctl submit
;
902 if (sh
->ops
.target
< 0)
903 target
= sh
->ops
.target2
;
904 else if (sh
->ops
.target2
< 0)
905 target
= sh
->ops
.target
;
907 /* we should only have one valid target */
910 pr_debug("%s: stripe %llu block: %d\n",
911 __func__
, (unsigned long long)sh
->sector
, target
);
913 tgt
= &sh
->dev
[target
];
914 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
917 atomic_inc(&sh
->count
);
919 if (target
== qd_idx
) {
920 count
= set_syndrome_sources(blocks
, sh
);
921 blocks
[count
] = NULL
; /* regenerating p is not necessary */
922 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
923 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
924 ops_complete_compute
, sh
,
925 to_addr_conv(sh
, percpu
));
926 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
928 /* Compute any data- or p-drive using XOR */
930 for (i
= disks
; i
-- ; ) {
931 if (i
== target
|| i
== qd_idx
)
933 blocks
[count
++] = sh
->dev
[i
].page
;
936 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
937 NULL
, ops_complete_compute
, sh
,
938 to_addr_conv(sh
, percpu
));
939 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
945 static struct dma_async_tx_descriptor
*
946 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
948 int i
, count
, disks
= sh
->disks
;
949 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
950 int d0_idx
= raid6_d0(sh
);
951 int faila
= -1, failb
= -1;
952 int target
= sh
->ops
.target
;
953 int target2
= sh
->ops
.target2
;
954 struct r5dev
*tgt
= &sh
->dev
[target
];
955 struct r5dev
*tgt2
= &sh
->dev
[target2
];
956 struct dma_async_tx_descriptor
*tx
;
957 struct page
**blocks
= percpu
->scribble
;
958 struct async_submit_ctl submit
;
960 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
961 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
962 BUG_ON(target
< 0 || target2
< 0);
963 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
964 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
966 /* we need to open-code set_syndrome_sources to handle the
967 * slot number conversion for 'faila' and 'failb'
969 for (i
= 0; i
< disks
; i
++)
974 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
976 blocks
[slot
] = sh
->dev
[i
].page
;
982 i
= raid6_next_disk(i
, disks
);
983 } while (i
!= d0_idx
);
985 BUG_ON(faila
== failb
);
988 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
989 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
991 atomic_inc(&sh
->count
);
993 if (failb
== syndrome_disks
+1) {
994 /* Q disk is one of the missing disks */
995 if (faila
== syndrome_disks
) {
996 /* Missing P+Q, just recompute */
997 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
998 ops_complete_compute
, sh
,
999 to_addr_conv(sh
, percpu
));
1000 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1001 STRIPE_SIZE
, &submit
);
1005 int qd_idx
= sh
->qd_idx
;
1007 /* Missing D+Q: recompute D from P, then recompute Q */
1008 if (target
== qd_idx
)
1009 data_target
= target2
;
1011 data_target
= target
;
1014 for (i
= disks
; i
-- ; ) {
1015 if (i
== data_target
|| i
== qd_idx
)
1017 blocks
[count
++] = sh
->dev
[i
].page
;
1019 dest
= sh
->dev
[data_target
].page
;
1020 init_async_submit(&submit
,
1021 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1023 to_addr_conv(sh
, percpu
));
1024 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1027 count
= set_syndrome_sources(blocks
, sh
);
1028 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1029 ops_complete_compute
, sh
,
1030 to_addr_conv(sh
, percpu
));
1031 return async_gen_syndrome(blocks
, 0, count
+2,
1032 STRIPE_SIZE
, &submit
);
1035 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1036 ops_complete_compute
, sh
,
1037 to_addr_conv(sh
, percpu
));
1038 if (failb
== syndrome_disks
) {
1039 /* We're missing D+P. */
1040 return async_raid6_datap_recov(syndrome_disks
+2,
1044 /* We're missing D+D. */
1045 return async_raid6_2data_recov(syndrome_disks
+2,
1046 STRIPE_SIZE
, faila
, failb
,
1053 static void ops_complete_prexor(void *stripe_head_ref
)
1055 struct stripe_head
*sh
= stripe_head_ref
;
1057 pr_debug("%s: stripe %llu\n", __func__
,
1058 (unsigned long long)sh
->sector
);
1061 static struct dma_async_tx_descriptor
*
1062 ops_run_prexor(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1063 struct dma_async_tx_descriptor
*tx
)
1065 int disks
= sh
->disks
;
1066 struct page
**xor_srcs
= percpu
->scribble
;
1067 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1068 struct async_submit_ctl submit
;
1070 /* existing parity data subtracted */
1071 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1073 pr_debug("%s: stripe %llu\n", __func__
,
1074 (unsigned long long)sh
->sector
);
1076 for (i
= disks
; i
--; ) {
1077 struct r5dev
*dev
= &sh
->dev
[i
];
1078 /* Only process blocks that are known to be uptodate */
1079 if (test_bit(R5_Wantdrain
, &dev
->flags
))
1080 xor_srcs
[count
++] = dev
->page
;
1083 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1084 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
));
1085 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1090 static struct dma_async_tx_descriptor
*
1091 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1093 int disks
= sh
->disks
;
1096 pr_debug("%s: stripe %llu\n", __func__
,
1097 (unsigned long long)sh
->sector
);
1099 for (i
= disks
; i
--; ) {
1100 struct r5dev
*dev
= &sh
->dev
[i
];
1103 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
1106 spin_lock_irq(&sh
->raid_conf
->device_lock
);
1107 chosen
= dev
->towrite
;
1108 dev
->towrite
= NULL
;
1109 BUG_ON(dev
->written
);
1110 wbi
= dev
->written
= chosen
;
1111 spin_unlock_irq(&sh
->raid_conf
->device_lock
);
1113 while (wbi
&& wbi
->bi_sector
<
1114 dev
->sector
+ STRIPE_SECTORS
) {
1115 if (wbi
->bi_rw
& REQ_FUA
)
1116 set_bit(R5_WantFUA
, &dev
->flags
);
1117 tx
= async_copy_data(1, wbi
, dev
->page
,
1119 wbi
= r5_next_bio(wbi
, dev
->sector
);
1127 static void ops_complete_reconstruct(void *stripe_head_ref
)
1129 struct stripe_head
*sh
= stripe_head_ref
;
1130 int disks
= sh
->disks
;
1131 int pd_idx
= sh
->pd_idx
;
1132 int qd_idx
= sh
->qd_idx
;
1136 pr_debug("%s: stripe %llu\n", __func__
,
1137 (unsigned long long)sh
->sector
);
1139 for (i
= disks
; i
--; )
1140 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1142 for (i
= disks
; i
--; ) {
1143 struct r5dev
*dev
= &sh
->dev
[i
];
1145 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1146 set_bit(R5_UPTODATE
, &dev
->flags
);
1148 set_bit(R5_WantFUA
, &dev
->flags
);
1152 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1153 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1154 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1155 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1157 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1158 sh
->reconstruct_state
= reconstruct_state_result
;
1161 set_bit(STRIPE_HANDLE
, &sh
->state
);
1166 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1167 struct dma_async_tx_descriptor
*tx
)
1169 int disks
= sh
->disks
;
1170 struct page
**xor_srcs
= percpu
->scribble
;
1171 struct async_submit_ctl submit
;
1172 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1173 struct page
*xor_dest
;
1175 unsigned long flags
;
1177 pr_debug("%s: stripe %llu\n", __func__
,
1178 (unsigned long long)sh
->sector
);
1180 /* check if prexor is active which means only process blocks
1181 * that are part of a read-modify-write (written)
1183 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1185 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1186 for (i
= disks
; i
--; ) {
1187 struct r5dev
*dev
= &sh
->dev
[i
];
1189 xor_srcs
[count
++] = dev
->page
;
1192 xor_dest
= sh
->dev
[pd_idx
].page
;
1193 for (i
= disks
; i
--; ) {
1194 struct r5dev
*dev
= &sh
->dev
[i
];
1196 xor_srcs
[count
++] = dev
->page
;
1200 /* 1/ if we prexor'd then the dest is reused as a source
1201 * 2/ if we did not prexor then we are redoing the parity
1202 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1203 * for the synchronous xor case
1205 flags
= ASYNC_TX_ACK
|
1206 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1208 atomic_inc(&sh
->count
);
1210 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, sh
,
1211 to_addr_conv(sh
, percpu
));
1212 if (unlikely(count
== 1))
1213 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1215 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1219 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1220 struct dma_async_tx_descriptor
*tx
)
1222 struct async_submit_ctl submit
;
1223 struct page
**blocks
= percpu
->scribble
;
1226 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1228 count
= set_syndrome_sources(blocks
, sh
);
1230 atomic_inc(&sh
->count
);
1232 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_reconstruct
,
1233 sh
, to_addr_conv(sh
, percpu
));
1234 async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1237 static void ops_complete_check(void *stripe_head_ref
)
1239 struct stripe_head
*sh
= stripe_head_ref
;
1241 pr_debug("%s: stripe %llu\n", __func__
,
1242 (unsigned long long)sh
->sector
);
1244 sh
->check_state
= check_state_check_result
;
1245 set_bit(STRIPE_HANDLE
, &sh
->state
);
1249 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1251 int disks
= sh
->disks
;
1252 int pd_idx
= sh
->pd_idx
;
1253 int qd_idx
= sh
->qd_idx
;
1254 struct page
*xor_dest
;
1255 struct page
**xor_srcs
= percpu
->scribble
;
1256 struct dma_async_tx_descriptor
*tx
;
1257 struct async_submit_ctl submit
;
1261 pr_debug("%s: stripe %llu\n", __func__
,
1262 (unsigned long long)sh
->sector
);
1265 xor_dest
= sh
->dev
[pd_idx
].page
;
1266 xor_srcs
[count
++] = xor_dest
;
1267 for (i
= disks
; i
--; ) {
1268 if (i
== pd_idx
|| i
== qd_idx
)
1270 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1273 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1274 to_addr_conv(sh
, percpu
));
1275 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1276 &sh
->ops
.zero_sum_result
, &submit
);
1278 atomic_inc(&sh
->count
);
1279 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1280 tx
= async_trigger_callback(&submit
);
1283 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1285 struct page
**srcs
= percpu
->scribble
;
1286 struct async_submit_ctl submit
;
1289 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1290 (unsigned long long)sh
->sector
, checkp
);
1292 count
= set_syndrome_sources(srcs
, sh
);
1296 atomic_inc(&sh
->count
);
1297 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1298 sh
, to_addr_conv(sh
, percpu
));
1299 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1300 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1303 static void __raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1305 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1306 struct dma_async_tx_descriptor
*tx
= NULL
;
1307 struct r5conf
*conf
= sh
->raid_conf
;
1308 int level
= conf
->level
;
1309 struct raid5_percpu
*percpu
;
1313 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1314 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1315 ops_run_biofill(sh
);
1319 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1321 tx
= ops_run_compute5(sh
, percpu
);
1323 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1324 tx
= ops_run_compute6_1(sh
, percpu
);
1326 tx
= ops_run_compute6_2(sh
, percpu
);
1328 /* terminate the chain if reconstruct is not set to be run */
1329 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1333 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
1334 tx
= ops_run_prexor(sh
, percpu
, tx
);
1336 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1337 tx
= ops_run_biodrain(sh
, tx
);
1341 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1343 ops_run_reconstruct5(sh
, percpu
, tx
);
1345 ops_run_reconstruct6(sh
, percpu
, tx
);
1348 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1349 if (sh
->check_state
== check_state_run
)
1350 ops_run_check_p(sh
, percpu
);
1351 else if (sh
->check_state
== check_state_run_q
)
1352 ops_run_check_pq(sh
, percpu
, 0);
1353 else if (sh
->check_state
== check_state_run_pq
)
1354 ops_run_check_pq(sh
, percpu
, 1);
1360 for (i
= disks
; i
--; ) {
1361 struct r5dev
*dev
= &sh
->dev
[i
];
1362 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1363 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1368 #ifdef CONFIG_MULTICORE_RAID456
1369 static void async_run_ops(void *param
, async_cookie_t cookie
)
1371 struct stripe_head
*sh
= param
;
1372 unsigned long ops_request
= sh
->ops
.request
;
1374 clear_bit_unlock(STRIPE_OPS_REQ_PENDING
, &sh
->state
);
1375 wake_up(&sh
->ops
.wait_for_ops
);
1377 __raid_run_ops(sh
, ops_request
);
1381 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1383 /* since handle_stripe can be called outside of raid5d context
1384 * we need to ensure sh->ops.request is de-staged before another
1387 wait_event(sh
->ops
.wait_for_ops
,
1388 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING
, &sh
->state
));
1389 sh
->ops
.request
= ops_request
;
1391 atomic_inc(&sh
->count
);
1392 async_schedule(async_run_ops
, sh
);
1395 #define raid_run_ops __raid_run_ops
1398 static int grow_one_stripe(struct r5conf
*conf
)
1400 struct stripe_head
*sh
;
1401 sh
= kmem_cache_zalloc(conf
->slab_cache
, GFP_KERNEL
);
1405 sh
->raid_conf
= conf
;
1406 #ifdef CONFIG_MULTICORE_RAID456
1407 init_waitqueue_head(&sh
->ops
.wait_for_ops
);
1410 if (grow_buffers(sh
)) {
1412 kmem_cache_free(conf
->slab_cache
, sh
);
1415 /* we just created an active stripe so... */
1416 atomic_set(&sh
->count
, 1);
1417 atomic_inc(&conf
->active_stripes
);
1418 INIT_LIST_HEAD(&sh
->lru
);
1423 static int grow_stripes(struct r5conf
*conf
, int num
)
1425 struct kmem_cache
*sc
;
1426 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
1428 if (conf
->mddev
->gendisk
)
1429 sprintf(conf
->cache_name
[0],
1430 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
1432 sprintf(conf
->cache_name
[0],
1433 "raid%d-%p", conf
->level
, conf
->mddev
);
1434 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
1436 conf
->active_name
= 0;
1437 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
1438 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
1442 conf
->slab_cache
= sc
;
1443 conf
->pool_size
= devs
;
1445 if (!grow_one_stripe(conf
))
1451 * scribble_len - return the required size of the scribble region
1452 * @num - total number of disks in the array
1454 * The size must be enough to contain:
1455 * 1/ a struct page pointer for each device in the array +2
1456 * 2/ room to convert each entry in (1) to its corresponding dma
1457 * (dma_map_page()) or page (page_address()) address.
1459 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1460 * calculate over all devices (not just the data blocks), using zeros in place
1461 * of the P and Q blocks.
1463 static size_t scribble_len(int num
)
1467 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
1472 static int resize_stripes(struct r5conf
*conf
, int newsize
)
1474 /* Make all the stripes able to hold 'newsize' devices.
1475 * New slots in each stripe get 'page' set to a new page.
1477 * This happens in stages:
1478 * 1/ create a new kmem_cache and allocate the required number of
1480 * 2/ gather all the old stripe_heads and tranfer the pages across
1481 * to the new stripe_heads. This will have the side effect of
1482 * freezing the array as once all stripe_heads have been collected,
1483 * no IO will be possible. Old stripe heads are freed once their
1484 * pages have been transferred over, and the old kmem_cache is
1485 * freed when all stripes are done.
1486 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1487 * we simple return a failre status - no need to clean anything up.
1488 * 4/ allocate new pages for the new slots in the new stripe_heads.
1489 * If this fails, we don't bother trying the shrink the
1490 * stripe_heads down again, we just leave them as they are.
1491 * As each stripe_head is processed the new one is released into
1494 * Once step2 is started, we cannot afford to wait for a write,
1495 * so we use GFP_NOIO allocations.
1497 struct stripe_head
*osh
, *nsh
;
1498 LIST_HEAD(newstripes
);
1499 struct disk_info
*ndisks
;
1502 struct kmem_cache
*sc
;
1505 if (newsize
<= conf
->pool_size
)
1506 return 0; /* never bother to shrink */
1508 err
= md_allow_write(conf
->mddev
);
1513 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
1514 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
1519 for (i
= conf
->max_nr_stripes
; i
; i
--) {
1520 nsh
= kmem_cache_zalloc(sc
, GFP_KERNEL
);
1524 nsh
->raid_conf
= conf
;
1525 #ifdef CONFIG_MULTICORE_RAID456
1526 init_waitqueue_head(&nsh
->ops
.wait_for_ops
);
1529 list_add(&nsh
->lru
, &newstripes
);
1532 /* didn't get enough, give up */
1533 while (!list_empty(&newstripes
)) {
1534 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1535 list_del(&nsh
->lru
);
1536 kmem_cache_free(sc
, nsh
);
1538 kmem_cache_destroy(sc
);
1541 /* Step 2 - Must use GFP_NOIO now.
1542 * OK, we have enough stripes, start collecting inactive
1543 * stripes and copying them over
1545 list_for_each_entry(nsh
, &newstripes
, lru
) {
1546 spin_lock_irq(&conf
->device_lock
);
1547 wait_event_lock_irq(conf
->wait_for_stripe
,
1548 !list_empty(&conf
->inactive_list
),
1551 osh
= get_free_stripe(conf
);
1552 spin_unlock_irq(&conf
->device_lock
);
1553 atomic_set(&nsh
->count
, 1);
1554 for(i
=0; i
<conf
->pool_size
; i
++)
1555 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1556 for( ; i
<newsize
; i
++)
1557 nsh
->dev
[i
].page
= NULL
;
1558 kmem_cache_free(conf
->slab_cache
, osh
);
1560 kmem_cache_destroy(conf
->slab_cache
);
1563 * At this point, we are holding all the stripes so the array
1564 * is completely stalled, so now is a good time to resize
1565 * conf->disks and the scribble region
1567 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1569 for (i
=0; i
<conf
->raid_disks
; i
++)
1570 ndisks
[i
] = conf
->disks
[i
];
1572 conf
->disks
= ndisks
;
1577 conf
->scribble_len
= scribble_len(newsize
);
1578 for_each_present_cpu(cpu
) {
1579 struct raid5_percpu
*percpu
;
1582 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1583 scribble
= kmalloc(conf
->scribble_len
, GFP_NOIO
);
1586 kfree(percpu
->scribble
);
1587 percpu
->scribble
= scribble
;
1595 /* Step 4, return new stripes to service */
1596 while(!list_empty(&newstripes
)) {
1597 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1598 list_del_init(&nsh
->lru
);
1600 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1601 if (nsh
->dev
[i
].page
== NULL
) {
1602 struct page
*p
= alloc_page(GFP_NOIO
);
1603 nsh
->dev
[i
].page
= p
;
1607 release_stripe(nsh
);
1609 /* critical section pass, GFP_NOIO no longer needed */
1611 conf
->slab_cache
= sc
;
1612 conf
->active_name
= 1-conf
->active_name
;
1613 conf
->pool_size
= newsize
;
1617 static int drop_one_stripe(struct r5conf
*conf
)
1619 struct stripe_head
*sh
;
1621 spin_lock_irq(&conf
->device_lock
);
1622 sh
= get_free_stripe(conf
);
1623 spin_unlock_irq(&conf
->device_lock
);
1626 BUG_ON(atomic_read(&sh
->count
));
1628 kmem_cache_free(conf
->slab_cache
, sh
);
1629 atomic_dec(&conf
->active_stripes
);
1633 static void shrink_stripes(struct r5conf
*conf
)
1635 while (drop_one_stripe(conf
))
1638 if (conf
->slab_cache
)
1639 kmem_cache_destroy(conf
->slab_cache
);
1640 conf
->slab_cache
= NULL
;
1643 static void raid5_end_read_request(struct bio
* bi
, int error
)
1645 struct stripe_head
*sh
= bi
->bi_private
;
1646 struct r5conf
*conf
= sh
->raid_conf
;
1647 int disks
= sh
->disks
, i
;
1648 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1649 char b
[BDEVNAME_SIZE
];
1650 struct md_rdev
*rdev
= NULL
;
1653 for (i
=0 ; i
<disks
; i
++)
1654 if (bi
== &sh
->dev
[i
].req
)
1657 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1658 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1664 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1665 /* If replacement finished while this request was outstanding,
1666 * 'replacement' might be NULL already.
1667 * In that case it moved down to 'rdev'.
1668 * rdev is not removed until all requests are finished.
1670 rdev
= conf
->disks
[i
].replacement
;
1672 rdev
= conf
->disks
[i
].rdev
;
1675 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1676 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1677 /* Note that this cannot happen on a
1678 * replacement device. We just fail those on
1683 "md/raid:%s: read error corrected"
1684 " (%lu sectors at %llu on %s)\n",
1685 mdname(conf
->mddev
), STRIPE_SECTORS
,
1686 (unsigned long long)(sh
->sector
1687 + rdev
->data_offset
),
1688 bdevname(rdev
->bdev
, b
));
1689 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1690 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1691 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1693 if (atomic_read(&rdev
->read_errors
))
1694 atomic_set(&rdev
->read_errors
, 0);
1696 const char *bdn
= bdevname(rdev
->bdev
, b
);
1699 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1700 atomic_inc(&rdev
->read_errors
);
1701 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1704 "md/raid:%s: read error on replacement device "
1705 "(sector %llu on %s).\n",
1706 mdname(conf
->mddev
),
1707 (unsigned long long)(sh
->sector
1708 + rdev
->data_offset
),
1710 else if (conf
->mddev
->degraded
>= conf
->max_degraded
)
1713 "md/raid:%s: read error not correctable "
1714 "(sector %llu on %s).\n",
1715 mdname(conf
->mddev
),
1716 (unsigned long long)(sh
->sector
1717 + rdev
->data_offset
),
1719 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1723 "md/raid:%s: read error NOT corrected!! "
1724 "(sector %llu on %s).\n",
1725 mdname(conf
->mddev
),
1726 (unsigned long long)(sh
->sector
1727 + rdev
->data_offset
),
1729 else if (atomic_read(&rdev
->read_errors
)
1730 > conf
->max_nr_stripes
)
1732 "md/raid:%s: Too many read errors, failing device %s.\n",
1733 mdname(conf
->mddev
), bdn
);
1737 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1739 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1740 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1741 md_error(conf
->mddev
, rdev
);
1744 rdev_dec_pending(rdev
, conf
->mddev
);
1745 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1746 set_bit(STRIPE_HANDLE
, &sh
->state
);
1750 static void raid5_end_write_request(struct bio
*bi
, int error
)
1752 struct stripe_head
*sh
= bi
->bi_private
;
1753 struct r5conf
*conf
= sh
->raid_conf
;
1754 int disks
= sh
->disks
, i
;
1755 struct md_rdev
*uninitialized_var(rdev
);
1756 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1759 int replacement
= 0;
1761 for (i
= 0 ; i
< disks
; i
++) {
1762 if (bi
== &sh
->dev
[i
].req
) {
1763 rdev
= conf
->disks
[i
].rdev
;
1766 if (bi
== &sh
->dev
[i
].rreq
) {
1767 rdev
= conf
->disks
[i
].replacement
;
1771 /* rdev was removed and 'replacement'
1772 * replaced it. rdev is not removed
1773 * until all requests are finished.
1775 rdev
= conf
->disks
[i
].rdev
;
1779 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1780 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1789 md_error(conf
->mddev
, rdev
);
1790 else if (is_badblock(rdev
, sh
->sector
,
1792 &first_bad
, &bad_sectors
))
1793 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
1796 set_bit(WriteErrorSeen
, &rdev
->flags
);
1797 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
1798 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1799 set_bit(MD_RECOVERY_NEEDED
,
1800 &rdev
->mddev
->recovery
);
1801 } else if (is_badblock(rdev
, sh
->sector
,
1803 &first_bad
, &bad_sectors
))
1804 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
1806 rdev_dec_pending(rdev
, conf
->mddev
);
1808 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
1809 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1810 set_bit(STRIPE_HANDLE
, &sh
->state
);
1814 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
);
1816 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
1818 struct r5dev
*dev
= &sh
->dev
[i
];
1820 bio_init(&dev
->req
);
1821 dev
->req
.bi_io_vec
= &dev
->vec
;
1823 dev
->req
.bi_max_vecs
++;
1824 dev
->req
.bi_private
= sh
;
1825 dev
->vec
.bv_page
= dev
->page
;
1827 bio_init(&dev
->rreq
);
1828 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
1829 dev
->rreq
.bi_vcnt
++;
1830 dev
->rreq
.bi_max_vecs
++;
1831 dev
->rreq
.bi_private
= sh
;
1832 dev
->rvec
.bv_page
= dev
->page
;
1835 dev
->sector
= compute_blocknr(sh
, i
, previous
);
1838 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1840 char b
[BDEVNAME_SIZE
];
1841 struct r5conf
*conf
= mddev
->private;
1842 unsigned long flags
;
1843 pr_debug("raid456: error called\n");
1845 spin_lock_irqsave(&conf
->device_lock
, flags
);
1846 clear_bit(In_sync
, &rdev
->flags
);
1847 mddev
->degraded
= calc_degraded(conf
);
1848 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1849 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1851 set_bit(Blocked
, &rdev
->flags
);
1852 set_bit(Faulty
, &rdev
->flags
);
1853 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1855 "md/raid:%s: Disk failure on %s, disabling device.\n"
1856 "md/raid:%s: Operation continuing on %d devices.\n",
1858 bdevname(rdev
->bdev
, b
),
1860 conf
->raid_disks
- mddev
->degraded
);
1864 * Input: a 'big' sector number,
1865 * Output: index of the data and parity disk, and the sector # in them.
1867 static sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
1868 int previous
, int *dd_idx
,
1869 struct stripe_head
*sh
)
1871 sector_t stripe
, stripe2
;
1872 sector_t chunk_number
;
1873 unsigned int chunk_offset
;
1876 sector_t new_sector
;
1877 int algorithm
= previous
? conf
->prev_algo
1879 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
1880 : conf
->chunk_sectors
;
1881 int raid_disks
= previous
? conf
->previous_raid_disks
1883 int data_disks
= raid_disks
- conf
->max_degraded
;
1885 /* First compute the information on this sector */
1888 * Compute the chunk number and the sector offset inside the chunk
1890 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
1891 chunk_number
= r_sector
;
1894 * Compute the stripe number
1896 stripe
= chunk_number
;
1897 *dd_idx
= sector_div(stripe
, data_disks
);
1900 * Select the parity disk based on the user selected algorithm.
1902 pd_idx
= qd_idx
= -1;
1903 switch(conf
->level
) {
1905 pd_idx
= data_disks
;
1908 switch (algorithm
) {
1909 case ALGORITHM_LEFT_ASYMMETRIC
:
1910 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1911 if (*dd_idx
>= pd_idx
)
1914 case ALGORITHM_RIGHT_ASYMMETRIC
:
1915 pd_idx
= sector_div(stripe2
, raid_disks
);
1916 if (*dd_idx
>= pd_idx
)
1919 case ALGORITHM_LEFT_SYMMETRIC
:
1920 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
1921 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1923 case ALGORITHM_RIGHT_SYMMETRIC
:
1924 pd_idx
= sector_div(stripe2
, raid_disks
);
1925 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
1927 case ALGORITHM_PARITY_0
:
1931 case ALGORITHM_PARITY_N
:
1932 pd_idx
= data_disks
;
1940 switch (algorithm
) {
1941 case ALGORITHM_LEFT_ASYMMETRIC
:
1942 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
1943 qd_idx
= pd_idx
+ 1;
1944 if (pd_idx
== raid_disks
-1) {
1945 (*dd_idx
)++; /* Q D D D P */
1947 } else if (*dd_idx
>= pd_idx
)
1948 (*dd_idx
) += 2; /* D D P Q D */
1950 case ALGORITHM_RIGHT_ASYMMETRIC
:
1951 pd_idx
= sector_div(stripe2
, raid_disks
);
1952 qd_idx
= pd_idx
+ 1;
1953 if (pd_idx
== raid_disks
-1) {
1954 (*dd_idx
)++; /* Q D D D P */
1956 } else if (*dd_idx
>= pd_idx
)
1957 (*dd_idx
) += 2; /* D D P Q D */
1959 case ALGORITHM_LEFT_SYMMETRIC
:
1960 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
1961 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1962 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1964 case ALGORITHM_RIGHT_SYMMETRIC
:
1965 pd_idx
= sector_div(stripe2
, raid_disks
);
1966 qd_idx
= (pd_idx
+ 1) % raid_disks
;
1967 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
1970 case ALGORITHM_PARITY_0
:
1975 case ALGORITHM_PARITY_N
:
1976 pd_idx
= data_disks
;
1977 qd_idx
= data_disks
+ 1;
1980 case ALGORITHM_ROTATING_ZERO_RESTART
:
1981 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1982 * of blocks for computing Q is different.
1984 pd_idx
= sector_div(stripe2
, raid_disks
);
1985 qd_idx
= pd_idx
+ 1;
1986 if (pd_idx
== raid_disks
-1) {
1987 (*dd_idx
)++; /* Q D D D P */
1989 } else if (*dd_idx
>= pd_idx
)
1990 (*dd_idx
) += 2; /* D D P Q D */
1994 case ALGORITHM_ROTATING_N_RESTART
:
1995 /* Same a left_asymmetric, by first stripe is
1996 * D D D P Q rather than
2000 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2001 qd_idx
= pd_idx
+ 1;
2002 if (pd_idx
== raid_disks
-1) {
2003 (*dd_idx
)++; /* Q D D D P */
2005 } else if (*dd_idx
>= pd_idx
)
2006 (*dd_idx
) += 2; /* D D P Q D */
2010 case ALGORITHM_ROTATING_N_CONTINUE
:
2011 /* Same as left_symmetric but Q is before P */
2012 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2013 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2014 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2018 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2019 /* RAID5 left_asymmetric, with Q on last device */
2020 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2021 if (*dd_idx
>= pd_idx
)
2023 qd_idx
= raid_disks
- 1;
2026 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2027 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2028 if (*dd_idx
>= pd_idx
)
2030 qd_idx
= raid_disks
- 1;
2033 case ALGORITHM_LEFT_SYMMETRIC_6
:
2034 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2035 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2036 qd_idx
= raid_disks
- 1;
2039 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2040 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2041 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2042 qd_idx
= raid_disks
- 1;
2045 case ALGORITHM_PARITY_0_6
:
2048 qd_idx
= raid_disks
- 1;
2058 sh
->pd_idx
= pd_idx
;
2059 sh
->qd_idx
= qd_idx
;
2060 sh
->ddf_layout
= ddf_layout
;
2063 * Finally, compute the new sector number
2065 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2070 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2072 struct r5conf
*conf
= sh
->raid_conf
;
2073 int raid_disks
= sh
->disks
;
2074 int data_disks
= raid_disks
- conf
->max_degraded
;
2075 sector_t new_sector
= sh
->sector
, check
;
2076 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2077 : conf
->chunk_sectors
;
2078 int algorithm
= previous
? conf
->prev_algo
2082 sector_t chunk_number
;
2083 int dummy1
, dd_idx
= i
;
2085 struct stripe_head sh2
;
2088 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2089 stripe
= new_sector
;
2091 if (i
== sh
->pd_idx
)
2093 switch(conf
->level
) {
2096 switch (algorithm
) {
2097 case ALGORITHM_LEFT_ASYMMETRIC
:
2098 case ALGORITHM_RIGHT_ASYMMETRIC
:
2102 case ALGORITHM_LEFT_SYMMETRIC
:
2103 case ALGORITHM_RIGHT_SYMMETRIC
:
2106 i
-= (sh
->pd_idx
+ 1);
2108 case ALGORITHM_PARITY_0
:
2111 case ALGORITHM_PARITY_N
:
2118 if (i
== sh
->qd_idx
)
2119 return 0; /* It is the Q disk */
2120 switch (algorithm
) {
2121 case ALGORITHM_LEFT_ASYMMETRIC
:
2122 case ALGORITHM_RIGHT_ASYMMETRIC
:
2123 case ALGORITHM_ROTATING_ZERO_RESTART
:
2124 case ALGORITHM_ROTATING_N_RESTART
:
2125 if (sh
->pd_idx
== raid_disks
-1)
2126 i
--; /* Q D D D P */
2127 else if (i
> sh
->pd_idx
)
2128 i
-= 2; /* D D P Q D */
2130 case ALGORITHM_LEFT_SYMMETRIC
:
2131 case ALGORITHM_RIGHT_SYMMETRIC
:
2132 if (sh
->pd_idx
== raid_disks
-1)
2133 i
--; /* Q D D D P */
2138 i
-= (sh
->pd_idx
+ 2);
2141 case ALGORITHM_PARITY_0
:
2144 case ALGORITHM_PARITY_N
:
2146 case ALGORITHM_ROTATING_N_CONTINUE
:
2147 /* Like left_symmetric, but P is before Q */
2148 if (sh
->pd_idx
== 0)
2149 i
--; /* P D D D Q */
2154 i
-= (sh
->pd_idx
+ 1);
2157 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2158 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2162 case ALGORITHM_LEFT_SYMMETRIC_6
:
2163 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2165 i
+= data_disks
+ 1;
2166 i
-= (sh
->pd_idx
+ 1);
2168 case ALGORITHM_PARITY_0_6
:
2177 chunk_number
= stripe
* data_disks
+ i
;
2178 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2180 check
= raid5_compute_sector(conf
, r_sector
,
2181 previous
, &dummy1
, &sh2
);
2182 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2183 || sh2
.qd_idx
!= sh
->qd_idx
) {
2184 printk(KERN_ERR
"md/raid:%s: compute_blocknr: map not correct\n",
2185 mdname(conf
->mddev
));
2193 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2194 int rcw
, int expand
)
2196 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
2197 struct r5conf
*conf
= sh
->raid_conf
;
2198 int level
= conf
->level
;
2201 /* if we are not expanding this is a proper write request, and
2202 * there will be bios with new data to be drained into the
2206 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2207 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2209 sh
->reconstruct_state
= reconstruct_state_run
;
2211 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2213 for (i
= disks
; i
--; ) {
2214 struct r5dev
*dev
= &sh
->dev
[i
];
2217 set_bit(R5_LOCKED
, &dev
->flags
);
2218 set_bit(R5_Wantdrain
, &dev
->flags
);
2220 clear_bit(R5_UPTODATE
, &dev
->flags
);
2224 if (s
->locked
+ conf
->max_degraded
== disks
)
2225 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2226 atomic_inc(&conf
->pending_full_writes
);
2229 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2230 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2232 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2233 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2234 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2235 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2237 for (i
= disks
; i
--; ) {
2238 struct r5dev
*dev
= &sh
->dev
[i
];
2243 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2244 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2245 set_bit(R5_Wantdrain
, &dev
->flags
);
2246 set_bit(R5_LOCKED
, &dev
->flags
);
2247 clear_bit(R5_UPTODATE
, &dev
->flags
);
2253 /* keep the parity disk(s) locked while asynchronous operations
2256 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2257 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2261 int qd_idx
= sh
->qd_idx
;
2262 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2264 set_bit(R5_LOCKED
, &dev
->flags
);
2265 clear_bit(R5_UPTODATE
, &dev
->flags
);
2269 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2270 __func__
, (unsigned long long)sh
->sector
,
2271 s
->locked
, s
->ops_request
);
2275 * Each stripe/dev can have one or more bion attached.
2276 * toread/towrite point to the first in a chain.
2277 * The bi_next chain must be in order.
2279 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
2282 struct r5conf
*conf
= sh
->raid_conf
;
2285 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2286 (unsigned long long)bi
->bi_sector
,
2287 (unsigned long long)sh
->sector
);
2290 spin_lock_irq(&conf
->device_lock
);
2292 bip
= &sh
->dev
[dd_idx
].towrite
;
2293 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
2296 bip
= &sh
->dev
[dd_idx
].toread
;
2297 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
2298 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
2300 bip
= & (*bip
)->bi_next
;
2302 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
2305 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
2309 bi
->bi_phys_segments
++;
2312 /* check if page is covered */
2313 sector_t sector
= sh
->dev
[dd_idx
].sector
;
2314 for (bi
=sh
->dev
[dd_idx
].towrite
;
2315 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
2316 bi
&& bi
->bi_sector
<= sector
;
2317 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
2318 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
2319 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
2321 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
2322 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
2324 spin_unlock_irq(&conf
->device_lock
);
2326 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2327 (unsigned long long)(*bip
)->bi_sector
,
2328 (unsigned long long)sh
->sector
, dd_idx
);
2330 if (conf
->mddev
->bitmap
&& firstwrite
) {
2331 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
2333 sh
->bm_seq
= conf
->seq_flush
+1;
2334 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
2339 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
2340 spin_unlock_irq(&conf
->device_lock
);
2344 static void end_reshape(struct r5conf
*conf
);
2346 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
2347 struct stripe_head
*sh
)
2349 int sectors_per_chunk
=
2350 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
2352 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
2353 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
2355 raid5_compute_sector(conf
,
2356 stripe
* (disks
- conf
->max_degraded
)
2357 *sectors_per_chunk
+ chunk_offset
,
2363 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
2364 struct stripe_head_state
*s
, int disks
,
2365 struct bio
**return_bi
)
2368 for (i
= disks
; i
--; ) {
2372 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2373 struct md_rdev
*rdev
;
2375 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2376 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2377 atomic_inc(&rdev
->nr_pending
);
2382 if (!rdev_set_badblocks(
2386 md_error(conf
->mddev
, rdev
);
2387 rdev_dec_pending(rdev
, conf
->mddev
);
2390 spin_lock_irq(&conf
->device_lock
);
2391 /* fail all writes first */
2392 bi
= sh
->dev
[i
].towrite
;
2393 sh
->dev
[i
].towrite
= NULL
;
2399 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2400 wake_up(&conf
->wait_for_overlap
);
2402 while (bi
&& bi
->bi_sector
<
2403 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2404 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2405 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2406 if (!raid5_dec_bi_phys_segments(bi
)) {
2407 md_write_end(conf
->mddev
);
2408 bi
->bi_next
= *return_bi
;
2413 /* and fail all 'written' */
2414 bi
= sh
->dev
[i
].written
;
2415 sh
->dev
[i
].written
= NULL
;
2416 if (bi
) bitmap_end
= 1;
2417 while (bi
&& bi
->bi_sector
<
2418 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2419 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2420 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2421 if (!raid5_dec_bi_phys_segments(bi
)) {
2422 md_write_end(conf
->mddev
);
2423 bi
->bi_next
= *return_bi
;
2429 /* fail any reads if this device is non-operational and
2430 * the data has not reached the cache yet.
2432 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2433 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2434 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2435 bi
= sh
->dev
[i
].toread
;
2436 sh
->dev
[i
].toread
= NULL
;
2437 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2438 wake_up(&conf
->wait_for_overlap
);
2439 if (bi
) s
->to_read
--;
2440 while (bi
&& bi
->bi_sector
<
2441 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2442 struct bio
*nextbi
=
2443 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2444 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2445 if (!raid5_dec_bi_phys_segments(bi
)) {
2446 bi
->bi_next
= *return_bi
;
2452 spin_unlock_irq(&conf
->device_lock
);
2454 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2455 STRIPE_SECTORS
, 0, 0);
2456 /* If we were in the middle of a write the parity block might
2457 * still be locked - so just clear all R5_LOCKED flags
2459 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2462 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2463 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2464 md_wakeup_thread(conf
->mddev
->thread
);
2468 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
2469 struct stripe_head_state
*s
)
2474 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2477 /* There is nothing more to do for sync/check/repair.
2478 * Don't even need to abort as that is handled elsewhere
2479 * if needed, and not always wanted e.g. if there is a known
2481 * For recover/replace we need to record a bad block on all
2482 * non-sync devices, or abort the recovery
2484 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
2485 /* During recovery devices cannot be removed, so
2486 * locking and refcounting of rdevs is not needed
2488 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2489 struct md_rdev
*rdev
= conf
->disks
[i
].rdev
;
2491 && !test_bit(Faulty
, &rdev
->flags
)
2492 && !test_bit(In_sync
, &rdev
->flags
)
2493 && !rdev_set_badblocks(rdev
, sh
->sector
,
2496 rdev
= conf
->disks
[i
].replacement
;
2498 && !test_bit(Faulty
, &rdev
->flags
)
2499 && !test_bit(In_sync
, &rdev
->flags
)
2500 && !rdev_set_badblocks(rdev
, sh
->sector
,
2505 conf
->recovery_disabled
=
2506 conf
->mddev
->recovery_disabled
;
2508 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
2511 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
2513 struct md_rdev
*rdev
;
2515 /* Doing recovery so rcu locking not required */
2516 rdev
= sh
->raid_conf
->disks
[disk_idx
].replacement
;
2518 && !test_bit(Faulty
, &rdev
->flags
)
2519 && !test_bit(In_sync
, &rdev
->flags
)
2520 && (rdev
->recovery_offset
<= sh
->sector
2521 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
2527 /* fetch_block - checks the given member device to see if its data needs
2528 * to be read or computed to satisfy a request.
2530 * Returns 1 when no more member devices need to be checked, otherwise returns
2531 * 0 to tell the loop in handle_stripe_fill to continue
2533 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2534 int disk_idx
, int disks
)
2536 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2537 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
2538 &sh
->dev
[s
->failed_num
[1]] };
2540 /* is the data in this block needed, and can we get it? */
2541 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2542 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2544 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2545 s
->syncing
|| s
->expanding
||
2546 (s
->replacing
&& want_replace(sh
, disk_idx
)) ||
2547 (s
->failed
>= 1 && fdev
[0]->toread
) ||
2548 (s
->failed
>= 2 && fdev
[1]->toread
) ||
2549 (sh
->raid_conf
->level
<= 5 && s
->failed
&& fdev
[0]->towrite
&&
2550 !test_bit(R5_OVERWRITE
, &fdev
[0]->flags
)) ||
2551 (sh
->raid_conf
->level
== 6 && s
->failed
&& s
->to_write
))) {
2552 /* we would like to get this block, possibly by computing it,
2553 * otherwise read it if the backing disk is insync
2555 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
2556 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
2557 if ((s
->uptodate
== disks
- 1) &&
2558 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
2559 disk_idx
== s
->failed_num
[1]))) {
2560 /* have disk failed, and we're requested to fetch it;
2563 pr_debug("Computing stripe %llu block %d\n",
2564 (unsigned long long)sh
->sector
, disk_idx
);
2565 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2566 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2567 set_bit(R5_Wantcompute
, &dev
->flags
);
2568 sh
->ops
.target
= disk_idx
;
2569 sh
->ops
.target2
= -1; /* no 2nd target */
2571 /* Careful: from this point on 'uptodate' is in the eye
2572 * of raid_run_ops which services 'compute' operations
2573 * before writes. R5_Wantcompute flags a block that will
2574 * be R5_UPTODATE by the time it is needed for a
2575 * subsequent operation.
2579 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
2580 /* Computing 2-failure is *very* expensive; only
2581 * do it if failed >= 2
2584 for (other
= disks
; other
--; ) {
2585 if (other
== disk_idx
)
2587 if (!test_bit(R5_UPTODATE
,
2588 &sh
->dev
[other
].flags
))
2592 pr_debug("Computing stripe %llu blocks %d,%d\n",
2593 (unsigned long long)sh
->sector
,
2595 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2596 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2597 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
2598 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
2599 sh
->ops
.target
= disk_idx
;
2600 sh
->ops
.target2
= other
;
2604 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2605 set_bit(R5_LOCKED
, &dev
->flags
);
2606 set_bit(R5_Wantread
, &dev
->flags
);
2608 pr_debug("Reading block %d (sync=%d)\n",
2609 disk_idx
, s
->syncing
);
2617 * handle_stripe_fill - read or compute data to satisfy pending requests.
2619 static void handle_stripe_fill(struct stripe_head
*sh
,
2620 struct stripe_head_state
*s
,
2625 /* look for blocks to read/compute, skip this if a compute
2626 * is already in flight, or if the stripe contents are in the
2627 * midst of changing due to a write
2629 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2630 !sh
->reconstruct_state
)
2631 for (i
= disks
; i
--; )
2632 if (fetch_block(sh
, s
, i
, disks
))
2634 set_bit(STRIPE_HANDLE
, &sh
->state
);
2638 /* handle_stripe_clean_event
2639 * any written block on an uptodate or failed drive can be returned.
2640 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2641 * never LOCKED, so we don't need to test 'failed' directly.
2643 static void handle_stripe_clean_event(struct r5conf
*conf
,
2644 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2649 for (i
= disks
; i
--; )
2650 if (sh
->dev
[i
].written
) {
2652 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2653 test_bit(R5_UPTODATE
, &dev
->flags
)) {
2654 /* We can return any write requests */
2655 struct bio
*wbi
, *wbi2
;
2657 pr_debug("Return write for disc %d\n", i
);
2658 spin_lock_irq(&conf
->device_lock
);
2660 dev
->written
= NULL
;
2661 while (wbi
&& wbi
->bi_sector
<
2662 dev
->sector
+ STRIPE_SECTORS
) {
2663 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2664 if (!raid5_dec_bi_phys_segments(wbi
)) {
2665 md_write_end(conf
->mddev
);
2666 wbi
->bi_next
= *return_bi
;
2671 if (dev
->towrite
== NULL
)
2673 spin_unlock_irq(&conf
->device_lock
);
2675 bitmap_endwrite(conf
->mddev
->bitmap
,
2678 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
2683 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2684 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2685 md_wakeup_thread(conf
->mddev
->thread
);
2688 static void handle_stripe_dirtying(struct r5conf
*conf
,
2689 struct stripe_head
*sh
,
2690 struct stripe_head_state
*s
,
2693 int rmw
= 0, rcw
= 0, i
;
2694 if (conf
->max_degraded
== 2) {
2695 /* RAID6 requires 'rcw' in current implementation
2696 * Calculate the real rcw later - for now fake it
2697 * look like rcw is cheaper
2700 } else for (i
= disks
; i
--; ) {
2701 /* would I have to read this buffer for read_modify_write */
2702 struct r5dev
*dev
= &sh
->dev
[i
];
2703 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2704 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2705 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2706 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2707 if (test_bit(R5_Insync
, &dev
->flags
))
2710 rmw
+= 2*disks
; /* cannot read it */
2712 /* Would I have to read this buffer for reconstruct_write */
2713 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
2714 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2715 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2716 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2717 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2722 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2723 (unsigned long long)sh
->sector
, rmw
, rcw
);
2724 set_bit(STRIPE_HANDLE
, &sh
->state
);
2725 if (rmw
< rcw
&& rmw
> 0)
2726 /* prefer read-modify-write, but need to get some data */
2727 for (i
= disks
; i
--; ) {
2728 struct r5dev
*dev
= &sh
->dev
[i
];
2729 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2730 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2731 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2732 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2733 test_bit(R5_Insync
, &dev
->flags
)) {
2735 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2736 pr_debug("Read_old block "
2737 "%d for r-m-w\n", i
);
2738 set_bit(R5_LOCKED
, &dev
->flags
);
2739 set_bit(R5_Wantread
, &dev
->flags
);
2742 set_bit(STRIPE_DELAYED
, &sh
->state
);
2743 set_bit(STRIPE_HANDLE
, &sh
->state
);
2747 if (rcw
<= rmw
&& rcw
> 0) {
2748 /* want reconstruct write, but need to get some data */
2750 for (i
= disks
; i
--; ) {
2751 struct r5dev
*dev
= &sh
->dev
[i
];
2752 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
2753 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
2754 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2755 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2756 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2758 if (!test_bit(R5_Insync
, &dev
->flags
))
2759 continue; /* it's a failed drive */
2761 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2762 pr_debug("Read_old block "
2763 "%d for Reconstruct\n", i
);
2764 set_bit(R5_LOCKED
, &dev
->flags
);
2765 set_bit(R5_Wantread
, &dev
->flags
);
2768 set_bit(STRIPE_DELAYED
, &sh
->state
);
2769 set_bit(STRIPE_HANDLE
, &sh
->state
);
2774 /* now if nothing is locked, and if we have enough data,
2775 * we can start a write request
2777 /* since handle_stripe can be called at any time we need to handle the
2778 * case where a compute block operation has been submitted and then a
2779 * subsequent call wants to start a write request. raid_run_ops only
2780 * handles the case where compute block and reconstruct are requested
2781 * simultaneously. If this is not the case then new writes need to be
2782 * held off until the compute completes.
2784 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
2785 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
2786 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
2787 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
2790 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
2791 struct stripe_head_state
*s
, int disks
)
2793 struct r5dev
*dev
= NULL
;
2795 set_bit(STRIPE_HANDLE
, &sh
->state
);
2797 switch (sh
->check_state
) {
2798 case check_state_idle
:
2799 /* start a new check operation if there are no failures */
2800 if (s
->failed
== 0) {
2801 BUG_ON(s
->uptodate
!= disks
);
2802 sh
->check_state
= check_state_run
;
2803 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2804 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
2808 dev
= &sh
->dev
[s
->failed_num
[0]];
2810 case check_state_compute_result
:
2811 sh
->check_state
= check_state_idle
;
2813 dev
= &sh
->dev
[sh
->pd_idx
];
2815 /* check that a write has not made the stripe insync */
2816 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2819 /* either failed parity check, or recovery is happening */
2820 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
2821 BUG_ON(s
->uptodate
!= disks
);
2823 set_bit(R5_LOCKED
, &dev
->flags
);
2825 set_bit(R5_Wantwrite
, &dev
->flags
);
2827 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2828 set_bit(STRIPE_INSYNC
, &sh
->state
);
2830 case check_state_run
:
2831 break; /* we will be called again upon completion */
2832 case check_state_check_result
:
2833 sh
->check_state
= check_state_idle
;
2835 /* if a failure occurred during the check operation, leave
2836 * STRIPE_INSYNC not set and let the stripe be handled again
2841 /* handle a successful check operation, if parity is correct
2842 * we are done. Otherwise update the mismatch count and repair
2843 * parity if !MD_RECOVERY_CHECK
2845 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
2846 /* parity is correct (on disc,
2847 * not in buffer any more)
2849 set_bit(STRIPE_INSYNC
, &sh
->state
);
2851 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2852 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2853 /* don't try to repair!! */
2854 set_bit(STRIPE_INSYNC
, &sh
->state
);
2856 sh
->check_state
= check_state_compute_run
;
2857 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2858 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2859 set_bit(R5_Wantcompute
,
2860 &sh
->dev
[sh
->pd_idx
].flags
);
2861 sh
->ops
.target
= sh
->pd_idx
;
2862 sh
->ops
.target2
= -1;
2867 case check_state_compute_run
:
2870 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
2871 __func__
, sh
->check_state
,
2872 (unsigned long long) sh
->sector
);
2878 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
2879 struct stripe_head_state
*s
,
2882 int pd_idx
= sh
->pd_idx
;
2883 int qd_idx
= sh
->qd_idx
;
2886 set_bit(STRIPE_HANDLE
, &sh
->state
);
2888 BUG_ON(s
->failed
> 2);
2890 /* Want to check and possibly repair P and Q.
2891 * However there could be one 'failed' device, in which
2892 * case we can only check one of them, possibly using the
2893 * other to generate missing data
2896 switch (sh
->check_state
) {
2897 case check_state_idle
:
2898 /* start a new check operation if there are < 2 failures */
2899 if (s
->failed
== s
->q_failed
) {
2900 /* The only possible failed device holds Q, so it
2901 * makes sense to check P (If anything else were failed,
2902 * we would have used P to recreate it).
2904 sh
->check_state
= check_state_run
;
2906 if (!s
->q_failed
&& s
->failed
< 2) {
2907 /* Q is not failed, and we didn't use it to generate
2908 * anything, so it makes sense to check it
2910 if (sh
->check_state
== check_state_run
)
2911 sh
->check_state
= check_state_run_pq
;
2913 sh
->check_state
= check_state_run_q
;
2916 /* discard potentially stale zero_sum_result */
2917 sh
->ops
.zero_sum_result
= 0;
2919 if (sh
->check_state
== check_state_run
) {
2920 /* async_xor_zero_sum destroys the contents of P */
2921 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2924 if (sh
->check_state
>= check_state_run
&&
2925 sh
->check_state
<= check_state_run_pq
) {
2926 /* async_syndrome_zero_sum preserves P and Q, so
2927 * no need to mark them !uptodate here
2929 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
2933 /* we have 2-disk failure */
2934 BUG_ON(s
->failed
!= 2);
2936 case check_state_compute_result
:
2937 sh
->check_state
= check_state_idle
;
2939 /* check that a write has not made the stripe insync */
2940 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
2943 /* now write out any block on a failed drive,
2944 * or P or Q if they were recomputed
2946 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
2947 if (s
->failed
== 2) {
2948 dev
= &sh
->dev
[s
->failed_num
[1]];
2950 set_bit(R5_LOCKED
, &dev
->flags
);
2951 set_bit(R5_Wantwrite
, &dev
->flags
);
2953 if (s
->failed
>= 1) {
2954 dev
= &sh
->dev
[s
->failed_num
[0]];
2956 set_bit(R5_LOCKED
, &dev
->flags
);
2957 set_bit(R5_Wantwrite
, &dev
->flags
);
2959 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
2960 dev
= &sh
->dev
[pd_idx
];
2962 set_bit(R5_LOCKED
, &dev
->flags
);
2963 set_bit(R5_Wantwrite
, &dev
->flags
);
2965 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
2966 dev
= &sh
->dev
[qd_idx
];
2968 set_bit(R5_LOCKED
, &dev
->flags
);
2969 set_bit(R5_Wantwrite
, &dev
->flags
);
2971 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2973 set_bit(STRIPE_INSYNC
, &sh
->state
);
2975 case check_state_run
:
2976 case check_state_run_q
:
2977 case check_state_run_pq
:
2978 break; /* we will be called again upon completion */
2979 case check_state_check_result
:
2980 sh
->check_state
= check_state_idle
;
2982 /* handle a successful check operation, if parity is correct
2983 * we are done. Otherwise update the mismatch count and repair
2984 * parity if !MD_RECOVERY_CHECK
2986 if (sh
->ops
.zero_sum_result
== 0) {
2987 /* both parities are correct */
2989 set_bit(STRIPE_INSYNC
, &sh
->state
);
2991 /* in contrast to the raid5 case we can validate
2992 * parity, but still have a failure to write
2995 sh
->check_state
= check_state_compute_result
;
2996 /* Returning at this point means that we may go
2997 * off and bring p and/or q uptodate again so
2998 * we make sure to check zero_sum_result again
2999 * to verify if p or q need writeback
3003 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
3004 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3005 /* don't try to repair!! */
3006 set_bit(STRIPE_INSYNC
, &sh
->state
);
3008 int *target
= &sh
->ops
.target
;
3010 sh
->ops
.target
= -1;
3011 sh
->ops
.target2
= -1;
3012 sh
->check_state
= check_state_compute_run
;
3013 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3014 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3015 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3016 set_bit(R5_Wantcompute
,
3017 &sh
->dev
[pd_idx
].flags
);
3019 target
= &sh
->ops
.target2
;
3022 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3023 set_bit(R5_Wantcompute
,
3024 &sh
->dev
[qd_idx
].flags
);
3031 case check_state_compute_run
:
3034 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3035 __func__
, sh
->check_state
,
3036 (unsigned long long) sh
->sector
);
3041 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3045 /* We have read all the blocks in this stripe and now we need to
3046 * copy some of them into a target stripe for expand.
3048 struct dma_async_tx_descriptor
*tx
= NULL
;
3049 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3050 for (i
= 0; i
< sh
->disks
; i
++)
3051 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3053 struct stripe_head
*sh2
;
3054 struct async_submit_ctl submit
;
3056 sector_t bn
= compute_blocknr(sh
, i
, 1);
3057 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3059 sh2
= get_active_stripe(conf
, s
, 0, 1, 1);
3061 /* so far only the early blocks of this stripe
3062 * have been requested. When later blocks
3063 * get requested, we will try again
3066 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3067 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3068 /* must have already done this block */
3069 release_stripe(sh2
);
3073 /* place all the copies on one channel */
3074 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
3075 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
3076 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
3079 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
3080 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
3081 for (j
= 0; j
< conf
->raid_disks
; j
++)
3082 if (j
!= sh2
->pd_idx
&&
3084 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
3086 if (j
== conf
->raid_disks
) {
3087 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
3088 set_bit(STRIPE_HANDLE
, &sh2
->state
);
3090 release_stripe(sh2
);
3093 /* done submitting copies, wait for them to complete */
3096 dma_wait_for_async_tx(tx
);
3101 * handle_stripe - do things to a stripe.
3103 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3104 * state of various bits to see what needs to be done.
3106 * return some read requests which now have data
3107 * return some write requests which are safely on storage
3108 * schedule a read on some buffers
3109 * schedule a write of some buffers
3110 * return confirmation of parity correctness
3114 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
3116 struct r5conf
*conf
= sh
->raid_conf
;
3117 int disks
= sh
->disks
;
3120 int do_recovery
= 0;
3122 memset(s
, 0, sizeof(*s
));
3124 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3125 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3126 s
->failed_num
[0] = -1;
3127 s
->failed_num
[1] = -1;
3129 /* Now to look around and see what can be done */
3131 spin_lock_irq(&conf
->device_lock
);
3132 for (i
=disks
; i
--; ) {
3133 struct md_rdev
*rdev
;
3140 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3142 dev
->toread
, dev
->towrite
, dev
->written
);
3143 /* maybe we can reply to a read
3145 * new wantfill requests are only permitted while
3146 * ops_complete_biofill is guaranteed to be inactive
3148 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
3149 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
3150 set_bit(R5_Wantfill
, &dev
->flags
);
3152 /* now count some things */
3153 if (test_bit(R5_LOCKED
, &dev
->flags
))
3155 if (test_bit(R5_UPTODATE
, &dev
->flags
))
3157 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
3159 BUG_ON(s
->compute
> 2);
3162 if (test_bit(R5_Wantfill
, &dev
->flags
))
3164 else if (dev
->toread
)
3168 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3173 /* Prefer to use the replacement for reads, but only
3174 * if it is recovered enough and has no bad blocks.
3176 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3177 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
3178 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
3179 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3180 &first_bad
, &bad_sectors
))
3181 set_bit(R5_ReadRepl
, &dev
->flags
);
3184 set_bit(R5_NeedReplace
, &dev
->flags
);
3185 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3186 clear_bit(R5_ReadRepl
, &dev
->flags
);
3188 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
3191 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3192 &first_bad
, &bad_sectors
);
3193 if (s
->blocked_rdev
== NULL
3194 && (test_bit(Blocked
, &rdev
->flags
)
3197 set_bit(BlockedBadBlocks
,
3199 s
->blocked_rdev
= rdev
;
3200 atomic_inc(&rdev
->nr_pending
);
3203 clear_bit(R5_Insync
, &dev
->flags
);
3207 /* also not in-sync */
3208 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
3209 test_bit(R5_UPTODATE
, &dev
->flags
)) {
3210 /* treat as in-sync, but with a read error
3211 * which we can now try to correct
3213 set_bit(R5_Insync
, &dev
->flags
);
3214 set_bit(R5_ReadError
, &dev
->flags
);
3216 } else if (test_bit(In_sync
, &rdev
->flags
))
3217 set_bit(R5_Insync
, &dev
->flags
);
3218 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
3219 /* in sync if before recovery_offset */
3220 set_bit(R5_Insync
, &dev
->flags
);
3221 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
3222 test_bit(R5_Expanded
, &dev
->flags
))
3223 /* If we've reshaped into here, we assume it is Insync.
3224 * We will shortly update recovery_offset to make
3227 set_bit(R5_Insync
, &dev
->flags
);
3229 if (rdev
&& test_bit(R5_WriteError
, &dev
->flags
)) {
3230 /* This flag does not apply to '.replacement'
3231 * only to .rdev, so make sure to check that*/
3232 struct md_rdev
*rdev2
= rcu_dereference(
3233 conf
->disks
[i
].rdev
);
3235 clear_bit(R5_Insync
, &dev
->flags
);
3236 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3237 s
->handle_bad_blocks
= 1;
3238 atomic_inc(&rdev2
->nr_pending
);
3240 clear_bit(R5_WriteError
, &dev
->flags
);
3242 if (rdev
&& test_bit(R5_MadeGood
, &dev
->flags
)) {
3243 /* This flag does not apply to '.replacement'
3244 * only to .rdev, so make sure to check that*/
3245 struct md_rdev
*rdev2
= rcu_dereference(
3246 conf
->disks
[i
].rdev
);
3247 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3248 s
->handle_bad_blocks
= 1;
3249 atomic_inc(&rdev2
->nr_pending
);
3251 clear_bit(R5_MadeGood
, &dev
->flags
);
3253 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3254 struct md_rdev
*rdev2
= rcu_dereference(
3255 conf
->disks
[i
].replacement
);
3256 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3257 s
->handle_bad_blocks
= 1;
3258 atomic_inc(&rdev2
->nr_pending
);
3260 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
3262 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3263 /* The ReadError flag will just be confusing now */
3264 clear_bit(R5_ReadError
, &dev
->flags
);
3265 clear_bit(R5_ReWrite
, &dev
->flags
);
3267 if (test_bit(R5_ReadError
, &dev
->flags
))
3268 clear_bit(R5_Insync
, &dev
->flags
);
3269 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3271 s
->failed_num
[s
->failed
] = i
;
3273 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
3277 spin_unlock_irq(&conf
->device_lock
);
3278 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
3279 /* If there is a failed device being replaced,
3280 * we must be recovering.
3281 * else if we are after recovery_cp, we must be syncing
3282 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3283 * else we can only be replacing
3284 * sync and recovery both need to read all devices, and so
3285 * use the same flag.
3288 sh
->sector
>= conf
->mddev
->recovery_cp
||
3289 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
3297 static void handle_stripe(struct stripe_head
*sh
)
3299 struct stripe_head_state s
;
3300 struct r5conf
*conf
= sh
->raid_conf
;
3303 int disks
= sh
->disks
;
3304 struct r5dev
*pdev
, *qdev
;
3306 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3307 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
3308 /* already being handled, ensure it gets handled
3309 * again when current action finishes */
3310 set_bit(STRIPE_HANDLE
, &sh
->state
);
3314 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3315 set_bit(STRIPE_SYNCING
, &sh
->state
);
3316 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3318 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3320 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3321 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3322 (unsigned long long)sh
->sector
, sh
->state
,
3323 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
3324 sh
->check_state
, sh
->reconstruct_state
);
3326 analyse_stripe(sh
, &s
);
3328 if (s
.handle_bad_blocks
) {
3329 set_bit(STRIPE_HANDLE
, &sh
->state
);
3333 if (unlikely(s
.blocked_rdev
)) {
3334 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3335 s
.replacing
|| s
.to_write
|| s
.written
) {
3336 set_bit(STRIPE_HANDLE
, &sh
->state
);
3339 /* There is nothing for the blocked_rdev to block */
3340 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
3341 s
.blocked_rdev
= NULL
;
3344 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
3345 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
3346 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
3349 pr_debug("locked=%d uptodate=%d to_read=%d"
3350 " to_write=%d failed=%d failed_num=%d,%d\n",
3351 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3352 s
.failed_num
[0], s
.failed_num
[1]);
3353 /* check if the array has lost more than max_degraded devices and,
3354 * if so, some requests might need to be failed.
3356 if (s
.failed
> conf
->max_degraded
) {
3357 sh
->check_state
= 0;
3358 sh
->reconstruct_state
= 0;
3359 if (s
.to_read
+s
.to_write
+s
.written
)
3360 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
3361 if (s
.syncing
+ s
.replacing
)
3362 handle_failed_sync(conf
, sh
, &s
);
3366 * might be able to return some write requests if the parity blocks
3367 * are safe, or on a failed drive
3369 pdev
= &sh
->dev
[sh
->pd_idx
];
3370 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
3371 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
3372 qdev
= &sh
->dev
[sh
->qd_idx
];
3373 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
3374 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
3378 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3379 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3380 && test_bit(R5_UPTODATE
, &pdev
->flags
)))) &&
3381 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3382 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3383 && test_bit(R5_UPTODATE
, &qdev
->flags
)))))
3384 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
3386 /* Now we might consider reading some blocks, either to check/generate
3387 * parity, or to satisfy requests
3388 * or to load a block that is being partially written.
3390 if (s
.to_read
|| s
.non_overwrite
3391 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
3392 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
3395 handle_stripe_fill(sh
, &s
, disks
);
3397 /* Now we check to see if any write operations have recently
3401 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
3403 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
3404 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
3405 sh
->reconstruct_state
= reconstruct_state_idle
;
3407 /* All the 'written' buffers and the parity block are ready to
3408 * be written back to disk
3410 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
));
3411 BUG_ON(sh
->qd_idx
>= 0 &&
3412 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
));
3413 for (i
= disks
; i
--; ) {
3414 struct r5dev
*dev
= &sh
->dev
[i
];
3415 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
3416 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3418 pr_debug("Writing block %d\n", i
);
3419 set_bit(R5_Wantwrite
, &dev
->flags
);
3422 if (!test_bit(R5_Insync
, &dev
->flags
) ||
3423 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
3425 set_bit(STRIPE_INSYNC
, &sh
->state
);
3428 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3429 s
.dec_preread_active
= 1;
3432 /* Now to consider new write requests and what else, if anything
3433 * should be read. We do not handle new writes when:
3434 * 1/ A 'write' operation (copy+xor) is already in flight.
3435 * 2/ A 'check' operation is in flight, as it may clobber the parity
3438 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
3439 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
3441 /* maybe we need to check and possibly fix the parity for this stripe
3442 * Any reads will already have been scheduled, so we just see if enough
3443 * data is available. The parity check is held off while parity
3444 * dependent operations are in flight.
3446 if (sh
->check_state
||
3447 (s
.syncing
&& s
.locked
== 0 &&
3448 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3449 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
3450 if (conf
->level
== 6)
3451 handle_parity_checks6(conf
, sh
, &s
, disks
);
3453 handle_parity_checks5(conf
, sh
, &s
, disks
);
3456 if (s
.replacing
&& s
.locked
== 0
3457 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3458 /* Write out to replacement devices where possible */
3459 for (i
= 0; i
< conf
->raid_disks
; i
++)
3460 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
) &&
3461 test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
3462 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
3463 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3466 set_bit(STRIPE_INSYNC
, &sh
->state
);
3468 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
3469 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3470 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3471 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3474 /* If the failed drives are just a ReadError, then we might need
3475 * to progress the repair/check process
3477 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
3478 for (i
= 0; i
< s
.failed
; i
++) {
3479 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
3480 if (test_bit(R5_ReadError
, &dev
->flags
)
3481 && !test_bit(R5_LOCKED
, &dev
->flags
)
3482 && test_bit(R5_UPTODATE
, &dev
->flags
)
3484 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3485 set_bit(R5_Wantwrite
, &dev
->flags
);
3486 set_bit(R5_ReWrite
, &dev
->flags
);
3487 set_bit(R5_LOCKED
, &dev
->flags
);
3490 /* let's read it back */
3491 set_bit(R5_Wantread
, &dev
->flags
);
3492 set_bit(R5_LOCKED
, &dev
->flags
);
3499 /* Finish reconstruct operations initiated by the expansion process */
3500 if (sh
->reconstruct_state
== reconstruct_state_result
) {
3501 struct stripe_head
*sh_src
3502 = get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
3503 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
3504 /* sh cannot be written until sh_src has been read.
3505 * so arrange for sh to be delayed a little
3507 set_bit(STRIPE_DELAYED
, &sh
->state
);
3508 set_bit(STRIPE_HANDLE
, &sh
->state
);
3509 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
3511 atomic_inc(&conf
->preread_active_stripes
);
3512 release_stripe(sh_src
);
3516 release_stripe(sh_src
);
3518 sh
->reconstruct_state
= reconstruct_state_idle
;
3519 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3520 for (i
= conf
->raid_disks
; i
--; ) {
3521 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3522 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3527 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
3528 !sh
->reconstruct_state
) {
3529 /* Need to write out all blocks after computing parity */
3530 sh
->disks
= conf
->raid_disks
;
3531 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3532 schedule_reconstruction(sh
, &s
, 1, 1);
3533 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
3534 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3535 atomic_dec(&conf
->reshape_stripes
);
3536 wake_up(&conf
->wait_for_overlap
);
3537 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3540 if (s
.expanding
&& s
.locked
== 0 &&
3541 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3542 handle_stripe_expansion(conf
, sh
);
3545 /* wait for this device to become unblocked */
3546 if (conf
->mddev
->external
&& unlikely(s
.blocked_rdev
))
3547 md_wait_for_blocked_rdev(s
.blocked_rdev
, conf
->mddev
);
3549 if (s
.handle_bad_blocks
)
3550 for (i
= disks
; i
--; ) {
3551 struct md_rdev
*rdev
;
3552 struct r5dev
*dev
= &sh
->dev
[i
];
3553 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
3554 /* We own a safe reference to the rdev */
3555 rdev
= conf
->disks
[i
].rdev
;
3556 if (!rdev_set_badblocks(rdev
, sh
->sector
,
3558 md_error(conf
->mddev
, rdev
);
3559 rdev_dec_pending(rdev
, conf
->mddev
);
3561 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
3562 rdev
= conf
->disks
[i
].rdev
;
3563 rdev_clear_badblocks(rdev
, sh
->sector
,
3565 rdev_dec_pending(rdev
, conf
->mddev
);
3567 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3568 rdev
= conf
->disks
[i
].replacement
;
3570 /* rdev have been moved down */
3571 rdev
= conf
->disks
[i
].rdev
;
3572 rdev_clear_badblocks(rdev
, sh
->sector
,
3574 rdev_dec_pending(rdev
, conf
->mddev
);
3579 raid_run_ops(sh
, s
.ops_request
);
3583 if (s
.dec_preread_active
) {
3584 /* We delay this until after ops_run_io so that if make_request
3585 * is waiting on a flush, it won't continue until the writes
3586 * have actually been submitted.
3588 atomic_dec(&conf
->preread_active_stripes
);
3589 if (atomic_read(&conf
->preread_active_stripes
) <
3591 md_wakeup_thread(conf
->mddev
->thread
);
3594 return_io(s
.return_bi
);
3596 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
3599 static void raid5_activate_delayed(struct r5conf
*conf
)
3601 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
3602 while (!list_empty(&conf
->delayed_list
)) {
3603 struct list_head
*l
= conf
->delayed_list
.next
;
3604 struct stripe_head
*sh
;
3605 sh
= list_entry(l
, struct stripe_head
, lru
);
3607 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3608 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3609 atomic_inc(&conf
->preread_active_stripes
);
3610 list_add_tail(&sh
->lru
, &conf
->hold_list
);
3615 static void activate_bit_delay(struct r5conf
*conf
)
3617 /* device_lock is held */
3618 struct list_head head
;
3619 list_add(&head
, &conf
->bitmap_list
);
3620 list_del_init(&conf
->bitmap_list
);
3621 while (!list_empty(&head
)) {
3622 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
3623 list_del_init(&sh
->lru
);
3624 atomic_inc(&sh
->count
);
3625 __release_stripe(conf
, sh
);
3629 int md_raid5_congested(struct mddev
*mddev
, int bits
)
3631 struct r5conf
*conf
= mddev
->private;
3633 /* No difference between reads and writes. Just check
3634 * how busy the stripe_cache is
3637 if (conf
->inactive_blocked
)
3641 if (list_empty_careful(&conf
->inactive_list
))
3646 EXPORT_SYMBOL_GPL(md_raid5_congested
);
3648 static int raid5_congested(void *data
, int bits
)
3650 struct mddev
*mddev
= data
;
3652 return mddev_congested(mddev
, bits
) ||
3653 md_raid5_congested(mddev
, bits
);
3656 /* We want read requests to align with chunks where possible,
3657 * but write requests don't need to.
3659 static int raid5_mergeable_bvec(struct request_queue
*q
,
3660 struct bvec_merge_data
*bvm
,
3661 struct bio_vec
*biovec
)
3663 struct mddev
*mddev
= q
->queuedata
;
3664 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
3666 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3667 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
3669 if ((bvm
->bi_rw
& 1) == WRITE
)
3670 return biovec
->bv_len
; /* always allow writes to be mergeable */
3672 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3673 chunk_sectors
= mddev
->new_chunk_sectors
;
3674 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
3675 if (max
< 0) max
= 0;
3676 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
3677 return biovec
->bv_len
;
3683 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
3685 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
3686 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3687 unsigned int bio_sectors
= bio
->bi_size
>> 9;
3689 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3690 chunk_sectors
= mddev
->new_chunk_sectors
;
3691 return chunk_sectors
>=
3692 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
3696 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3697 * later sampled by raid5d.
3699 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
3701 unsigned long flags
;
3703 spin_lock_irqsave(&conf
->device_lock
, flags
);
3705 bi
->bi_next
= conf
->retry_read_aligned_list
;
3706 conf
->retry_read_aligned_list
= bi
;
3708 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3709 md_wakeup_thread(conf
->mddev
->thread
);
3713 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
3717 bi
= conf
->retry_read_aligned
;
3719 conf
->retry_read_aligned
= NULL
;
3722 bi
= conf
->retry_read_aligned_list
;
3724 conf
->retry_read_aligned_list
= bi
->bi_next
;
3727 * this sets the active strip count to 1 and the processed
3728 * strip count to zero (upper 8 bits)
3730 bi
->bi_phys_segments
= 1; /* biased count of active stripes */
3738 * The "raid5_align_endio" should check if the read succeeded and if it
3739 * did, call bio_endio on the original bio (having bio_put the new bio
3741 * If the read failed..
3743 static void raid5_align_endio(struct bio
*bi
, int error
)
3745 struct bio
* raid_bi
= bi
->bi_private
;
3746 struct mddev
*mddev
;
3747 struct r5conf
*conf
;
3748 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
3749 struct md_rdev
*rdev
;
3753 rdev
= (void*)raid_bi
->bi_next
;
3754 raid_bi
->bi_next
= NULL
;
3755 mddev
= rdev
->mddev
;
3756 conf
= mddev
->private;
3758 rdev_dec_pending(rdev
, conf
->mddev
);
3760 if (!error
&& uptodate
) {
3761 bio_endio(raid_bi
, 0);
3762 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
3763 wake_up(&conf
->wait_for_stripe
);
3768 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3770 add_bio_to_retry(raid_bi
, conf
);
3773 static int bio_fits_rdev(struct bio
*bi
)
3775 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
3777 if ((bi
->bi_size
>>9) > queue_max_sectors(q
))
3779 blk_recount_segments(q
, bi
);
3780 if (bi
->bi_phys_segments
> queue_max_segments(q
))
3783 if (q
->merge_bvec_fn
)
3784 /* it's too hard to apply the merge_bvec_fn at this stage,
3793 static int chunk_aligned_read(struct mddev
*mddev
, struct bio
* raid_bio
)
3795 struct r5conf
*conf
= mddev
->private;
3797 struct bio
* align_bi
;
3798 struct md_rdev
*rdev
;
3799 sector_t end_sector
;
3801 if (!in_chunk_boundary(mddev
, raid_bio
)) {
3802 pr_debug("chunk_aligned_read : non aligned\n");
3806 * use bio_clone_mddev to make a copy of the bio
3808 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
3812 * set bi_end_io to a new function, and set bi_private to the
3815 align_bi
->bi_end_io
= raid5_align_endio
;
3816 align_bi
->bi_private
= raid_bio
;
3820 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
3824 end_sector
= align_bi
->bi_sector
+ (align_bi
->bi_size
>> 9);
3826 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
3827 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
3828 rdev
->recovery_offset
< end_sector
) {
3829 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
3831 (test_bit(Faulty
, &rdev
->flags
) ||
3832 !(test_bit(In_sync
, &rdev
->flags
) ||
3833 rdev
->recovery_offset
>= end_sector
)))
3840 atomic_inc(&rdev
->nr_pending
);
3842 raid_bio
->bi_next
= (void*)rdev
;
3843 align_bi
->bi_bdev
= rdev
->bdev
;
3844 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
3845 align_bi
->bi_sector
+= rdev
->data_offset
;
3847 if (!bio_fits_rdev(align_bi
) ||
3848 is_badblock(rdev
, align_bi
->bi_sector
, align_bi
->bi_size
>>9,
3849 &first_bad
, &bad_sectors
)) {
3850 /* too big in some way, or has a known bad block */
3852 rdev_dec_pending(rdev
, mddev
);
3856 spin_lock_irq(&conf
->device_lock
);
3857 wait_event_lock_irq(conf
->wait_for_stripe
,
3859 conf
->device_lock
, /* nothing */);
3860 atomic_inc(&conf
->active_aligned_reads
);
3861 spin_unlock_irq(&conf
->device_lock
);
3863 generic_make_request(align_bi
);
3872 /* __get_priority_stripe - get the next stripe to process
3874 * Full stripe writes are allowed to pass preread active stripes up until
3875 * the bypass_threshold is exceeded. In general the bypass_count
3876 * increments when the handle_list is handled before the hold_list; however, it
3877 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3878 * stripe with in flight i/o. The bypass_count will be reset when the
3879 * head of the hold_list has changed, i.e. the head was promoted to the
3882 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
)
3884 struct stripe_head
*sh
;
3886 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3888 list_empty(&conf
->handle_list
) ? "empty" : "busy",
3889 list_empty(&conf
->hold_list
) ? "empty" : "busy",
3890 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
3892 if (!list_empty(&conf
->handle_list
)) {
3893 sh
= list_entry(conf
->handle_list
.next
, typeof(*sh
), lru
);
3895 if (list_empty(&conf
->hold_list
))
3896 conf
->bypass_count
= 0;
3897 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
3898 if (conf
->hold_list
.next
== conf
->last_hold
)
3899 conf
->bypass_count
++;
3901 conf
->last_hold
= conf
->hold_list
.next
;
3902 conf
->bypass_count
-= conf
->bypass_threshold
;
3903 if (conf
->bypass_count
< 0)
3904 conf
->bypass_count
= 0;
3907 } else if (!list_empty(&conf
->hold_list
) &&
3908 ((conf
->bypass_threshold
&&
3909 conf
->bypass_count
> conf
->bypass_threshold
) ||
3910 atomic_read(&conf
->pending_full_writes
) == 0)) {
3911 sh
= list_entry(conf
->hold_list
.next
,
3913 conf
->bypass_count
-= conf
->bypass_threshold
;
3914 if (conf
->bypass_count
< 0)
3915 conf
->bypass_count
= 0;
3919 list_del_init(&sh
->lru
);
3920 atomic_inc(&sh
->count
);
3921 BUG_ON(atomic_read(&sh
->count
) != 1);
3925 static void make_request(struct mddev
*mddev
, struct bio
* bi
)
3927 struct r5conf
*conf
= mddev
->private;
3929 sector_t new_sector
;
3930 sector_t logical_sector
, last_sector
;
3931 struct stripe_head
*sh
;
3932 const int rw
= bio_data_dir(bi
);
3936 if (unlikely(bi
->bi_rw
& REQ_FLUSH
)) {
3937 md_flush_request(mddev
, bi
);
3941 md_write_start(mddev
, bi
);
3944 mddev
->reshape_position
== MaxSector
&&
3945 chunk_aligned_read(mddev
,bi
))
3948 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
3949 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
3951 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
3953 plugged
= mddev_check_plugged(mddev
);
3954 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
3956 int disks
, data_disks
;
3961 disks
= conf
->raid_disks
;
3962 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
3963 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
3964 /* spinlock is needed as reshape_progress may be
3965 * 64bit on a 32bit platform, and so it might be
3966 * possible to see a half-updated value
3967 * Of course reshape_progress could change after
3968 * the lock is dropped, so once we get a reference
3969 * to the stripe that we think it is, we will have
3972 spin_lock_irq(&conf
->device_lock
);
3973 if (mddev
->delta_disks
< 0
3974 ? logical_sector
< conf
->reshape_progress
3975 : logical_sector
>= conf
->reshape_progress
) {
3976 disks
= conf
->previous_raid_disks
;
3979 if (mddev
->delta_disks
< 0
3980 ? logical_sector
< conf
->reshape_safe
3981 : logical_sector
>= conf
->reshape_safe
) {
3982 spin_unlock_irq(&conf
->device_lock
);
3987 spin_unlock_irq(&conf
->device_lock
);
3989 data_disks
= disks
- conf
->max_degraded
;
3991 new_sector
= raid5_compute_sector(conf
, logical_sector
,
3994 pr_debug("raid456: make_request, sector %llu logical %llu\n",
3995 (unsigned long long)new_sector
,
3996 (unsigned long long)logical_sector
);
3998 sh
= get_active_stripe(conf
, new_sector
, previous
,
3999 (bi
->bi_rw
&RWA_MASK
), 0);
4001 if (unlikely(previous
)) {
4002 /* expansion might have moved on while waiting for a
4003 * stripe, so we must do the range check again.
4004 * Expansion could still move past after this
4005 * test, but as we are holding a reference to
4006 * 'sh', we know that if that happens,
4007 * STRIPE_EXPANDING will get set and the expansion
4008 * won't proceed until we finish with the stripe.
4011 spin_lock_irq(&conf
->device_lock
);
4012 if (mddev
->delta_disks
< 0
4013 ? logical_sector
>= conf
->reshape_progress
4014 : logical_sector
< conf
->reshape_progress
)
4015 /* mismatch, need to try again */
4017 spin_unlock_irq(&conf
->device_lock
);
4026 logical_sector
>= mddev
->suspend_lo
&&
4027 logical_sector
< mddev
->suspend_hi
) {
4029 /* As the suspend_* range is controlled by
4030 * userspace, we want an interruptible
4033 flush_signals(current
);
4034 prepare_to_wait(&conf
->wait_for_overlap
,
4035 &w
, TASK_INTERRUPTIBLE
);
4036 if (logical_sector
>= mddev
->suspend_lo
&&
4037 logical_sector
< mddev
->suspend_hi
)
4042 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
4043 !add_stripe_bio(sh
, bi
, dd_idx
, rw
)) {
4044 /* Stripe is busy expanding or
4045 * add failed due to overlap. Flush everything
4048 md_wakeup_thread(mddev
->thread
);
4053 finish_wait(&conf
->wait_for_overlap
, &w
);
4054 set_bit(STRIPE_HANDLE
, &sh
->state
);
4055 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4056 if ((bi
->bi_rw
& REQ_SYNC
) &&
4057 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4058 atomic_inc(&conf
->preread_active_stripes
);
4061 /* cannot get stripe for read-ahead, just give-up */
4062 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4063 finish_wait(&conf
->wait_for_overlap
, &w
);
4069 md_wakeup_thread(mddev
->thread
);
4071 spin_lock_irq(&conf
->device_lock
);
4072 remaining
= raid5_dec_bi_phys_segments(bi
);
4073 spin_unlock_irq(&conf
->device_lock
);
4074 if (remaining
== 0) {
4077 md_write_end(mddev
);
4083 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
4085 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
4087 /* reshaping is quite different to recovery/resync so it is
4088 * handled quite separately ... here.
4090 * On each call to sync_request, we gather one chunk worth of
4091 * destination stripes and flag them as expanding.
4092 * Then we find all the source stripes and request reads.
4093 * As the reads complete, handle_stripe will copy the data
4094 * into the destination stripe and release that stripe.
4096 struct r5conf
*conf
= mddev
->private;
4097 struct stripe_head
*sh
;
4098 sector_t first_sector
, last_sector
;
4099 int raid_disks
= conf
->previous_raid_disks
;
4100 int data_disks
= raid_disks
- conf
->max_degraded
;
4101 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4104 sector_t writepos
, readpos
, safepos
;
4105 sector_t stripe_addr
;
4106 int reshape_sectors
;
4107 struct list_head stripes
;
4109 if (sector_nr
== 0) {
4110 /* If restarting in the middle, skip the initial sectors */
4111 if (mddev
->delta_disks
< 0 &&
4112 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
4113 sector_nr
= raid5_size(mddev
, 0, 0)
4114 - conf
->reshape_progress
;
4115 } else if (mddev
->delta_disks
>= 0 &&
4116 conf
->reshape_progress
> 0)
4117 sector_nr
= conf
->reshape_progress
;
4118 sector_div(sector_nr
, new_data_disks
);
4120 mddev
->curr_resync_completed
= sector_nr
;
4121 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4127 /* We need to process a full chunk at a time.
4128 * If old and new chunk sizes differ, we need to process the
4131 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
)
4132 reshape_sectors
= mddev
->new_chunk_sectors
;
4134 reshape_sectors
= mddev
->chunk_sectors
;
4136 /* we update the metadata when there is more than 3Meg
4137 * in the block range (that is rather arbitrary, should
4138 * probably be time based) or when the data about to be
4139 * copied would over-write the source of the data at
4140 * the front of the range.
4141 * i.e. one new_stripe along from reshape_progress new_maps
4142 * to after where reshape_safe old_maps to
4144 writepos
= conf
->reshape_progress
;
4145 sector_div(writepos
, new_data_disks
);
4146 readpos
= conf
->reshape_progress
;
4147 sector_div(readpos
, data_disks
);
4148 safepos
= conf
->reshape_safe
;
4149 sector_div(safepos
, data_disks
);
4150 if (mddev
->delta_disks
< 0) {
4151 writepos
-= min_t(sector_t
, reshape_sectors
, writepos
);
4152 readpos
+= reshape_sectors
;
4153 safepos
+= reshape_sectors
;
4155 writepos
+= reshape_sectors
;
4156 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
4157 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
4160 /* 'writepos' is the most advanced device address we might write.
4161 * 'readpos' is the least advanced device address we might read.
4162 * 'safepos' is the least address recorded in the metadata as having
4164 * If 'readpos' is behind 'writepos', then there is no way that we can
4165 * ensure safety in the face of a crash - that must be done by userspace
4166 * making a backup of the data. So in that case there is no particular
4167 * rush to update metadata.
4168 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4169 * update the metadata to advance 'safepos' to match 'readpos' so that
4170 * we can be safe in the event of a crash.
4171 * So we insist on updating metadata if safepos is behind writepos and
4172 * readpos is beyond writepos.
4173 * In any case, update the metadata every 10 seconds.
4174 * Maybe that number should be configurable, but I'm not sure it is
4175 * worth it.... maybe it could be a multiple of safemode_delay???
4177 if ((mddev
->delta_disks
< 0
4178 ? (safepos
> writepos
&& readpos
< writepos
)
4179 : (safepos
< writepos
&& readpos
> writepos
)) ||
4180 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4181 /* Cannot proceed until we've updated the superblock... */
4182 wait_event(conf
->wait_for_overlap
,
4183 atomic_read(&conf
->reshape_stripes
)==0);
4184 mddev
->reshape_position
= conf
->reshape_progress
;
4185 mddev
->curr_resync_completed
= sector_nr
;
4186 conf
->reshape_checkpoint
= jiffies
;
4187 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4188 md_wakeup_thread(mddev
->thread
);
4189 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4190 kthread_should_stop());
4191 spin_lock_irq(&conf
->device_lock
);
4192 conf
->reshape_safe
= mddev
->reshape_position
;
4193 spin_unlock_irq(&conf
->device_lock
);
4194 wake_up(&conf
->wait_for_overlap
);
4195 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4198 if (mddev
->delta_disks
< 0) {
4199 BUG_ON(conf
->reshape_progress
== 0);
4200 stripe_addr
= writepos
;
4201 BUG_ON((mddev
->dev_sectors
&
4202 ~((sector_t
)reshape_sectors
- 1))
4203 - reshape_sectors
- stripe_addr
4206 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
4207 stripe_addr
= sector_nr
;
4209 INIT_LIST_HEAD(&stripes
);
4210 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
4212 int skipped_disk
= 0;
4213 sh
= get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
4214 set_bit(STRIPE_EXPANDING
, &sh
->state
);
4215 atomic_inc(&conf
->reshape_stripes
);
4216 /* If any of this stripe is beyond the end of the old
4217 * array, then we need to zero those blocks
4219 for (j
=sh
->disks
; j
--;) {
4221 if (j
== sh
->pd_idx
)
4223 if (conf
->level
== 6 &&
4226 s
= compute_blocknr(sh
, j
, 0);
4227 if (s
< raid5_size(mddev
, 0, 0)) {
4231 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
4232 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
4233 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
4235 if (!skipped_disk
) {
4236 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4237 set_bit(STRIPE_HANDLE
, &sh
->state
);
4239 list_add(&sh
->lru
, &stripes
);
4241 spin_lock_irq(&conf
->device_lock
);
4242 if (mddev
->delta_disks
< 0)
4243 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
4245 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
4246 spin_unlock_irq(&conf
->device_lock
);
4247 /* Ok, those stripe are ready. We can start scheduling
4248 * reads on the source stripes.
4249 * The source stripes are determined by mapping the first and last
4250 * block on the destination stripes.
4253 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
4256 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
4257 * new_data_disks
- 1),
4259 if (last_sector
>= mddev
->dev_sectors
)
4260 last_sector
= mddev
->dev_sectors
- 1;
4261 while (first_sector
<= last_sector
) {
4262 sh
= get_active_stripe(conf
, first_sector
, 1, 0, 1);
4263 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4264 set_bit(STRIPE_HANDLE
, &sh
->state
);
4266 first_sector
+= STRIPE_SECTORS
;
4268 /* Now that the sources are clearly marked, we can release
4269 * the destination stripes
4271 while (!list_empty(&stripes
)) {
4272 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
4273 list_del_init(&sh
->lru
);
4276 /* If this takes us to the resync_max point where we have to pause,
4277 * then we need to write out the superblock.
4279 sector_nr
+= reshape_sectors
;
4280 if ((sector_nr
- mddev
->curr_resync_completed
) * 2
4281 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
4282 /* Cannot proceed until we've updated the superblock... */
4283 wait_event(conf
->wait_for_overlap
,
4284 atomic_read(&conf
->reshape_stripes
) == 0);
4285 mddev
->reshape_position
= conf
->reshape_progress
;
4286 mddev
->curr_resync_completed
= sector_nr
;
4287 conf
->reshape_checkpoint
= jiffies
;
4288 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4289 md_wakeup_thread(mddev
->thread
);
4290 wait_event(mddev
->sb_wait
,
4291 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
4292 || kthread_should_stop());
4293 spin_lock_irq(&conf
->device_lock
);
4294 conf
->reshape_safe
= mddev
->reshape_position
;
4295 spin_unlock_irq(&conf
->device_lock
);
4296 wake_up(&conf
->wait_for_overlap
);
4297 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4299 return reshape_sectors
;
4302 /* FIXME go_faster isn't used */
4303 static inline sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
4305 struct r5conf
*conf
= mddev
->private;
4306 struct stripe_head
*sh
;
4307 sector_t max_sector
= mddev
->dev_sectors
;
4308 sector_t sync_blocks
;
4309 int still_degraded
= 0;
4312 if (sector_nr
>= max_sector
) {
4313 /* just being told to finish up .. nothing much to do */
4315 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
4320 if (mddev
->curr_resync
< max_sector
) /* aborted */
4321 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
4323 else /* completed sync */
4325 bitmap_close_sync(mddev
->bitmap
);
4330 /* Allow raid5_quiesce to complete */
4331 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
4333 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4334 return reshape_request(mddev
, sector_nr
, skipped
);
4336 /* No need to check resync_max as we never do more than one
4337 * stripe, and as resync_max will always be on a chunk boundary,
4338 * if the check in md_do_sync didn't fire, there is no chance
4339 * of overstepping resync_max here
4342 /* if there is too many failed drives and we are trying
4343 * to resync, then assert that we are finished, because there is
4344 * nothing we can do.
4346 if (mddev
->degraded
>= conf
->max_degraded
&&
4347 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4348 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
4352 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
4353 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
4354 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
4355 /* we can skip this block, and probably more */
4356 sync_blocks
/= STRIPE_SECTORS
;
4358 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
4361 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
4363 sh
= get_active_stripe(conf
, sector_nr
, 0, 1, 0);
4365 sh
= get_active_stripe(conf
, sector_nr
, 0, 0, 0);
4366 /* make sure we don't swamp the stripe cache if someone else
4367 * is trying to get access
4369 schedule_timeout_uninterruptible(1);
4371 /* Need to check if array will still be degraded after recovery/resync
4372 * We don't need to check the 'failed' flag as when that gets set,
4375 for (i
= 0; i
< conf
->raid_disks
; i
++)
4376 if (conf
->disks
[i
].rdev
== NULL
)
4379 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
4381 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
4386 return STRIPE_SECTORS
;
4389 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
4391 /* We may not be able to submit a whole bio at once as there
4392 * may not be enough stripe_heads available.
4393 * We cannot pre-allocate enough stripe_heads as we may need
4394 * more than exist in the cache (if we allow ever large chunks).
4395 * So we do one stripe head at a time and record in
4396 * ->bi_hw_segments how many have been done.
4398 * We *know* that this entire raid_bio is in one chunk, so
4399 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4401 struct stripe_head
*sh
;
4403 sector_t sector
, logical_sector
, last_sector
;
4408 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4409 sector
= raid5_compute_sector(conf
, logical_sector
,
4411 last_sector
= raid_bio
->bi_sector
+ (raid_bio
->bi_size
>>9);
4413 for (; logical_sector
< last_sector
;
4414 logical_sector
+= STRIPE_SECTORS
,
4415 sector
+= STRIPE_SECTORS
,
4418 if (scnt
< raid5_bi_hw_segments(raid_bio
))
4419 /* already done this stripe */
4422 sh
= get_active_stripe(conf
, sector
, 0, 1, 0);
4425 /* failed to get a stripe - must wait */
4426 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4427 conf
->retry_read_aligned
= raid_bio
;
4431 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
4433 raid5_set_bi_hw_segments(raid_bio
, scnt
);
4434 conf
->retry_read_aligned
= raid_bio
;
4442 spin_lock_irq(&conf
->device_lock
);
4443 remaining
= raid5_dec_bi_phys_segments(raid_bio
);
4444 spin_unlock_irq(&conf
->device_lock
);
4446 bio_endio(raid_bio
, 0);
4447 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4448 wake_up(&conf
->wait_for_stripe
);
4454 * This is our raid5 kernel thread.
4456 * We scan the hash table for stripes which can be handled now.
4457 * During the scan, completed stripes are saved for us by the interrupt
4458 * handler, so that they will not have to wait for our next wakeup.
4460 static void raid5d(struct mddev
*mddev
)
4462 struct stripe_head
*sh
;
4463 struct r5conf
*conf
= mddev
->private;
4465 struct blk_plug plug
;
4467 pr_debug("+++ raid5d active\n");
4469 md_check_recovery(mddev
);
4471 blk_start_plug(&plug
);
4473 spin_lock_irq(&conf
->device_lock
);
4477 if (atomic_read(&mddev
->plug_cnt
) == 0 &&
4478 !list_empty(&conf
->bitmap_list
)) {
4479 /* Now is a good time to flush some bitmap updates */
4481 spin_unlock_irq(&conf
->device_lock
);
4482 bitmap_unplug(mddev
->bitmap
);
4483 spin_lock_irq(&conf
->device_lock
);
4484 conf
->seq_write
= conf
->seq_flush
;
4485 activate_bit_delay(conf
);
4487 if (atomic_read(&mddev
->plug_cnt
) == 0)
4488 raid5_activate_delayed(conf
);
4490 while ((bio
= remove_bio_from_retry(conf
))) {
4492 spin_unlock_irq(&conf
->device_lock
);
4493 ok
= retry_aligned_read(conf
, bio
);
4494 spin_lock_irq(&conf
->device_lock
);
4500 sh
= __get_priority_stripe(conf
);
4504 spin_unlock_irq(&conf
->device_lock
);
4511 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
4512 md_check_recovery(mddev
);
4514 spin_lock_irq(&conf
->device_lock
);
4516 pr_debug("%d stripes handled\n", handled
);
4518 spin_unlock_irq(&conf
->device_lock
);
4520 async_tx_issue_pending_all();
4521 blk_finish_plug(&plug
);
4523 pr_debug("--- raid5d inactive\n");
4527 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
4529 struct r5conf
*conf
= mddev
->private;
4531 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
4537 raid5_set_cache_size(struct mddev
*mddev
, int size
)
4539 struct r5conf
*conf
= mddev
->private;
4542 if (size
<= 16 || size
> 32768)
4544 while (size
< conf
->max_nr_stripes
) {
4545 if (drop_one_stripe(conf
))
4546 conf
->max_nr_stripes
--;
4550 err
= md_allow_write(mddev
);
4553 while (size
> conf
->max_nr_stripes
) {
4554 if (grow_one_stripe(conf
))
4555 conf
->max_nr_stripes
++;
4560 EXPORT_SYMBOL(raid5_set_cache_size
);
4563 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
4565 struct r5conf
*conf
= mddev
->private;
4569 if (len
>= PAGE_SIZE
)
4574 if (strict_strtoul(page
, 10, &new))
4576 err
= raid5_set_cache_size(mddev
, new);
4582 static struct md_sysfs_entry
4583 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
4584 raid5_show_stripe_cache_size
,
4585 raid5_store_stripe_cache_size
);
4588 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
4590 struct r5conf
*conf
= mddev
->private;
4592 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
4598 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
4600 struct r5conf
*conf
= mddev
->private;
4602 if (len
>= PAGE_SIZE
)
4607 if (strict_strtoul(page
, 10, &new))
4609 if (new > conf
->max_nr_stripes
)
4611 conf
->bypass_threshold
= new;
4615 static struct md_sysfs_entry
4616 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
4618 raid5_show_preread_threshold
,
4619 raid5_store_preread_threshold
);
4622 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
4624 struct r5conf
*conf
= mddev
->private;
4626 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
4631 static struct md_sysfs_entry
4632 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
4634 static struct attribute
*raid5_attrs
[] = {
4635 &raid5_stripecache_size
.attr
,
4636 &raid5_stripecache_active
.attr
,
4637 &raid5_preread_bypass_threshold
.attr
,
4640 static struct attribute_group raid5_attrs_group
= {
4642 .attrs
= raid5_attrs
,
4646 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
4648 struct r5conf
*conf
= mddev
->private;
4651 sectors
= mddev
->dev_sectors
;
4653 /* size is defined by the smallest of previous and new size */
4654 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
4656 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
4657 sectors
&= ~((sector_t
)mddev
->new_chunk_sectors
- 1);
4658 return sectors
* (raid_disks
- conf
->max_degraded
);
4661 static void raid5_free_percpu(struct r5conf
*conf
)
4663 struct raid5_percpu
*percpu
;
4670 for_each_possible_cpu(cpu
) {
4671 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4672 safe_put_page(percpu
->spare_page
);
4673 kfree(percpu
->scribble
);
4675 #ifdef CONFIG_HOTPLUG_CPU
4676 unregister_cpu_notifier(&conf
->cpu_notify
);
4680 free_percpu(conf
->percpu
);
4683 static void free_conf(struct r5conf
*conf
)
4685 shrink_stripes(conf
);
4686 raid5_free_percpu(conf
);
4688 kfree(conf
->stripe_hashtbl
);
4692 #ifdef CONFIG_HOTPLUG_CPU
4693 static int raid456_cpu_notify(struct notifier_block
*nfb
, unsigned long action
,
4696 struct r5conf
*conf
= container_of(nfb
, struct r5conf
, cpu_notify
);
4697 long cpu
= (long)hcpu
;
4698 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
4701 case CPU_UP_PREPARE
:
4702 case CPU_UP_PREPARE_FROZEN
:
4703 if (conf
->level
== 6 && !percpu
->spare_page
)
4704 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
4705 if (!percpu
->scribble
)
4706 percpu
->scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4708 if (!percpu
->scribble
||
4709 (conf
->level
== 6 && !percpu
->spare_page
)) {
4710 safe_put_page(percpu
->spare_page
);
4711 kfree(percpu
->scribble
);
4712 pr_err("%s: failed memory allocation for cpu%ld\n",
4714 return notifier_from_errno(-ENOMEM
);
4718 case CPU_DEAD_FROZEN
:
4719 safe_put_page(percpu
->spare_page
);
4720 kfree(percpu
->scribble
);
4721 percpu
->spare_page
= NULL
;
4722 percpu
->scribble
= NULL
;
4731 static int raid5_alloc_percpu(struct r5conf
*conf
)
4734 struct page
*spare_page
;
4735 struct raid5_percpu __percpu
*allcpus
;
4739 allcpus
= alloc_percpu(struct raid5_percpu
);
4742 conf
->percpu
= allcpus
;
4746 for_each_present_cpu(cpu
) {
4747 if (conf
->level
== 6) {
4748 spare_page
= alloc_page(GFP_KERNEL
);
4753 per_cpu_ptr(conf
->percpu
, cpu
)->spare_page
= spare_page
;
4755 scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
4760 per_cpu_ptr(conf
->percpu
, cpu
)->scribble
= scribble
;
4762 #ifdef CONFIG_HOTPLUG_CPU
4763 conf
->cpu_notify
.notifier_call
= raid456_cpu_notify
;
4764 conf
->cpu_notify
.priority
= 0;
4766 err
= register_cpu_notifier(&conf
->cpu_notify
);
4773 static struct r5conf
*setup_conf(struct mddev
*mddev
)
4775 struct r5conf
*conf
;
4776 int raid_disk
, memory
, max_disks
;
4777 struct md_rdev
*rdev
;
4778 struct disk_info
*disk
;
4780 if (mddev
->new_level
!= 5
4781 && mddev
->new_level
!= 4
4782 && mddev
->new_level
!= 6) {
4783 printk(KERN_ERR
"md/raid:%s: raid level not set to 4/5/6 (%d)\n",
4784 mdname(mddev
), mddev
->new_level
);
4785 return ERR_PTR(-EIO
);
4787 if ((mddev
->new_level
== 5
4788 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
4789 (mddev
->new_level
== 6
4790 && !algorithm_valid_raid6(mddev
->new_layout
))) {
4791 printk(KERN_ERR
"md/raid:%s: layout %d not supported\n",
4792 mdname(mddev
), mddev
->new_layout
);
4793 return ERR_PTR(-EIO
);
4795 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
4796 printk(KERN_ERR
"md/raid:%s: not enough configured devices (%d, minimum 4)\n",
4797 mdname(mddev
), mddev
->raid_disks
);
4798 return ERR_PTR(-EINVAL
);
4801 if (!mddev
->new_chunk_sectors
||
4802 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
4803 !is_power_of_2(mddev
->new_chunk_sectors
)) {
4804 printk(KERN_ERR
"md/raid:%s: invalid chunk size %d\n",
4805 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
4806 return ERR_PTR(-EINVAL
);
4809 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
4812 spin_lock_init(&conf
->device_lock
);
4813 init_waitqueue_head(&conf
->wait_for_stripe
);
4814 init_waitqueue_head(&conf
->wait_for_overlap
);
4815 INIT_LIST_HEAD(&conf
->handle_list
);
4816 INIT_LIST_HEAD(&conf
->hold_list
);
4817 INIT_LIST_HEAD(&conf
->delayed_list
);
4818 INIT_LIST_HEAD(&conf
->bitmap_list
);
4819 INIT_LIST_HEAD(&conf
->inactive_list
);
4820 atomic_set(&conf
->active_stripes
, 0);
4821 atomic_set(&conf
->preread_active_stripes
, 0);
4822 atomic_set(&conf
->active_aligned_reads
, 0);
4823 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
4824 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
4826 conf
->raid_disks
= mddev
->raid_disks
;
4827 if (mddev
->reshape_position
== MaxSector
)
4828 conf
->previous_raid_disks
= mddev
->raid_disks
;
4830 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4831 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
4832 conf
->scribble_len
= scribble_len(max_disks
);
4834 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
4839 conf
->mddev
= mddev
;
4841 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
4844 conf
->level
= mddev
->new_level
;
4845 if (raid5_alloc_percpu(conf
) != 0)
4848 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
4850 rdev_for_each(rdev
, mddev
) {
4851 raid_disk
= rdev
->raid_disk
;
4852 if (raid_disk
>= max_disks
4855 disk
= conf
->disks
+ raid_disk
;
4857 if (test_bit(Replacement
, &rdev
->flags
)) {
4858 if (disk
->replacement
)
4860 disk
->replacement
= rdev
;
4867 if (test_bit(In_sync
, &rdev
->flags
)) {
4868 char b
[BDEVNAME_SIZE
];
4869 printk(KERN_INFO
"md/raid:%s: device %s operational as raid"
4871 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
4872 } else if (rdev
->saved_raid_disk
!= raid_disk
)
4873 /* Cannot rely on bitmap to complete recovery */
4877 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
4878 conf
->level
= mddev
->new_level
;
4879 if (conf
->level
== 6)
4880 conf
->max_degraded
= 2;
4882 conf
->max_degraded
= 1;
4883 conf
->algorithm
= mddev
->new_layout
;
4884 conf
->max_nr_stripes
= NR_STRIPES
;
4885 conf
->reshape_progress
= mddev
->reshape_position
;
4886 if (conf
->reshape_progress
!= MaxSector
) {
4887 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
4888 conf
->prev_algo
= mddev
->layout
;
4891 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
4892 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
4893 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
4895 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4896 mdname(mddev
), memory
);
4899 printk(KERN_INFO
"md/raid:%s: allocated %dkB\n",
4900 mdname(mddev
), memory
);
4902 conf
->thread
= md_register_thread(raid5d
, mddev
, NULL
);
4903 if (!conf
->thread
) {
4905 "md/raid:%s: couldn't allocate thread.\n",
4915 return ERR_PTR(-EIO
);
4917 return ERR_PTR(-ENOMEM
);
4921 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
4924 case ALGORITHM_PARITY_0
:
4925 if (raid_disk
< max_degraded
)
4928 case ALGORITHM_PARITY_N
:
4929 if (raid_disk
>= raid_disks
- max_degraded
)
4932 case ALGORITHM_PARITY_0_6
:
4933 if (raid_disk
== 0 ||
4934 raid_disk
== raid_disks
- 1)
4937 case ALGORITHM_LEFT_ASYMMETRIC_6
:
4938 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
4939 case ALGORITHM_LEFT_SYMMETRIC_6
:
4940 case ALGORITHM_RIGHT_SYMMETRIC_6
:
4941 if (raid_disk
== raid_disks
- 1)
4947 static int run(struct mddev
*mddev
)
4949 struct r5conf
*conf
;
4950 int working_disks
= 0;
4951 int dirty_parity_disks
= 0;
4952 struct md_rdev
*rdev
;
4953 sector_t reshape_offset
= 0;
4956 if (mddev
->recovery_cp
!= MaxSector
)
4957 printk(KERN_NOTICE
"md/raid:%s: not clean"
4958 " -- starting background reconstruction\n",
4960 if (mddev
->reshape_position
!= MaxSector
) {
4961 /* Check that we can continue the reshape.
4962 * Currently only disks can change, it must
4963 * increase, and we must be past the point where
4964 * a stripe over-writes itself
4966 sector_t here_new
, here_old
;
4968 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
4970 if (mddev
->new_level
!= mddev
->level
) {
4971 printk(KERN_ERR
"md/raid:%s: unsupported reshape "
4972 "required - aborting.\n",
4976 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
4977 /* reshape_position must be on a new-stripe boundary, and one
4978 * further up in new geometry must map after here in old
4981 here_new
= mddev
->reshape_position
;
4982 if (sector_div(here_new
, mddev
->new_chunk_sectors
*
4983 (mddev
->raid_disks
- max_degraded
))) {
4984 printk(KERN_ERR
"md/raid:%s: reshape_position not "
4985 "on a stripe boundary\n", mdname(mddev
));
4988 reshape_offset
= here_new
* mddev
->new_chunk_sectors
;
4989 /* here_new is the stripe we will write to */
4990 here_old
= mddev
->reshape_position
;
4991 sector_div(here_old
, mddev
->chunk_sectors
*
4992 (old_disks
-max_degraded
));
4993 /* here_old is the first stripe that we might need to read
4995 if (mddev
->delta_disks
== 0) {
4996 /* We cannot be sure it is safe to start an in-place
4997 * reshape. It is only safe if user-space if monitoring
4998 * and taking constant backups.
4999 * mdadm always starts a situation like this in
5000 * readonly mode so it can take control before
5001 * allowing any writes. So just check for that.
5003 if ((here_new
* mddev
->new_chunk_sectors
!=
5004 here_old
* mddev
->chunk_sectors
) ||
5006 printk(KERN_ERR
"md/raid:%s: in-place reshape must be started"
5007 " in read-only mode - aborting\n",
5011 } else if (mddev
->delta_disks
< 0
5012 ? (here_new
* mddev
->new_chunk_sectors
<=
5013 here_old
* mddev
->chunk_sectors
)
5014 : (here_new
* mddev
->new_chunk_sectors
>=
5015 here_old
* mddev
->chunk_sectors
)) {
5016 /* Reading from the same stripe as writing to - bad */
5017 printk(KERN_ERR
"md/raid:%s: reshape_position too early for "
5018 "auto-recovery - aborting.\n",
5022 printk(KERN_INFO
"md/raid:%s: reshape will continue\n",
5024 /* OK, we should be able to continue; */
5026 BUG_ON(mddev
->level
!= mddev
->new_level
);
5027 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
5028 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
5029 BUG_ON(mddev
->delta_disks
!= 0);
5032 if (mddev
->private == NULL
)
5033 conf
= setup_conf(mddev
);
5035 conf
= mddev
->private;
5038 return PTR_ERR(conf
);
5040 mddev
->thread
= conf
->thread
;
5041 conf
->thread
= NULL
;
5042 mddev
->private = conf
;
5044 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
5046 rdev
= conf
->disks
[i
].rdev
;
5047 if (!rdev
&& conf
->disks
[i
].replacement
) {
5048 /* The replacement is all we have yet */
5049 rdev
= conf
->disks
[i
].replacement
;
5050 conf
->disks
[i
].replacement
= NULL
;
5051 clear_bit(Replacement
, &rdev
->flags
);
5052 conf
->disks
[i
].rdev
= rdev
;
5056 if (conf
->disks
[i
].replacement
&&
5057 conf
->reshape_progress
!= MaxSector
) {
5058 /* replacements and reshape simply do not mix. */
5059 printk(KERN_ERR
"md: cannot handle concurrent "
5060 "replacement and reshape.\n");
5063 if (test_bit(In_sync
, &rdev
->flags
)) {
5067 /* This disc is not fully in-sync. However if it
5068 * just stored parity (beyond the recovery_offset),
5069 * when we don't need to be concerned about the
5070 * array being dirty.
5071 * When reshape goes 'backwards', we never have
5072 * partially completed devices, so we only need
5073 * to worry about reshape going forwards.
5075 /* Hack because v0.91 doesn't store recovery_offset properly. */
5076 if (mddev
->major_version
== 0 &&
5077 mddev
->minor_version
> 90)
5078 rdev
->recovery_offset
= reshape_offset
;
5080 if (rdev
->recovery_offset
< reshape_offset
) {
5081 /* We need to check old and new layout */
5082 if (!only_parity(rdev
->raid_disk
,
5085 conf
->max_degraded
))
5088 if (!only_parity(rdev
->raid_disk
,
5090 conf
->previous_raid_disks
,
5091 conf
->max_degraded
))
5093 dirty_parity_disks
++;
5097 * 0 for a fully functional array, 1 or 2 for a degraded array.
5099 mddev
->degraded
= calc_degraded(conf
);
5101 if (has_failed(conf
)) {
5102 printk(KERN_ERR
"md/raid:%s: not enough operational devices"
5103 " (%d/%d failed)\n",
5104 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
5108 /* device size must be a multiple of chunk size */
5109 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
5110 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5112 if (mddev
->degraded
> dirty_parity_disks
&&
5113 mddev
->recovery_cp
!= MaxSector
) {
5114 if (mddev
->ok_start_degraded
)
5116 "md/raid:%s: starting dirty degraded array"
5117 " - data corruption possible.\n",
5121 "md/raid:%s: cannot start dirty degraded array.\n",
5127 if (mddev
->degraded
== 0)
5128 printk(KERN_INFO
"md/raid:%s: raid level %d active with %d out of %d"
5129 " devices, algorithm %d\n", mdname(mddev
), conf
->level
,
5130 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
5133 printk(KERN_ALERT
"md/raid:%s: raid level %d active with %d"
5134 " out of %d devices, algorithm %d\n",
5135 mdname(mddev
), conf
->level
,
5136 mddev
->raid_disks
- mddev
->degraded
,
5137 mddev
->raid_disks
, mddev
->new_layout
);
5139 print_raid5_conf(conf
);
5141 if (conf
->reshape_progress
!= MaxSector
) {
5142 conf
->reshape_safe
= conf
->reshape_progress
;
5143 atomic_set(&conf
->reshape_stripes
, 0);
5144 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5145 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5146 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5147 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5148 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5153 /* Ok, everything is just fine now */
5154 if (mddev
->to_remove
== &raid5_attrs_group
)
5155 mddev
->to_remove
= NULL
;
5156 else if (mddev
->kobj
.sd
&&
5157 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
5159 "raid5: failed to create sysfs attributes for %s\n",
5161 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5165 /* read-ahead size must cover two whole stripes, which
5166 * is 2 * (datadisks) * chunksize where 'n' is the
5167 * number of raid devices
5169 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
5170 int stripe
= data_disks
*
5171 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
5172 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5173 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5175 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
5177 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5178 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
5180 chunk_size
= mddev
->chunk_sectors
<< 9;
5181 blk_queue_io_min(mddev
->queue
, chunk_size
);
5182 blk_queue_io_opt(mddev
->queue
, chunk_size
*
5183 (conf
->raid_disks
- conf
->max_degraded
));
5185 rdev_for_each(rdev
, mddev
)
5186 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5187 rdev
->data_offset
<< 9);
5192 md_unregister_thread(&mddev
->thread
);
5193 print_raid5_conf(conf
);
5195 mddev
->private = NULL
;
5196 printk(KERN_ALERT
"md/raid:%s: failed to run raid set.\n", mdname(mddev
));
5200 static int stop(struct mddev
*mddev
)
5202 struct r5conf
*conf
= mddev
->private;
5204 md_unregister_thread(&mddev
->thread
);
5206 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5208 mddev
->private = NULL
;
5209 mddev
->to_remove
= &raid5_attrs_group
;
5213 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
5215 struct r5conf
*conf
= mddev
->private;
5218 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
5219 mddev
->chunk_sectors
/ 2, mddev
->layout
);
5220 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
5221 for (i
= 0; i
< conf
->raid_disks
; i
++)
5222 seq_printf (seq
, "%s",
5223 conf
->disks
[i
].rdev
&&
5224 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
5225 seq_printf (seq
, "]");
5228 static void print_raid5_conf (struct r5conf
*conf
)
5231 struct disk_info
*tmp
;
5233 printk(KERN_DEBUG
"RAID conf printout:\n");
5235 printk("(conf==NULL)\n");
5238 printk(KERN_DEBUG
" --- level:%d rd:%d wd:%d\n", conf
->level
,
5240 conf
->raid_disks
- conf
->mddev
->degraded
);
5242 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5243 char b
[BDEVNAME_SIZE
];
5244 tmp
= conf
->disks
+ i
;
5246 printk(KERN_DEBUG
" disk %d, o:%d, dev:%s\n",
5247 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
5248 bdevname(tmp
->rdev
->bdev
, b
));
5252 static int raid5_spare_active(struct mddev
*mddev
)
5255 struct r5conf
*conf
= mddev
->private;
5256 struct disk_info
*tmp
;
5258 unsigned long flags
;
5260 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5261 tmp
= conf
->disks
+ i
;
5262 if (tmp
->replacement
5263 && tmp
->replacement
->recovery_offset
== MaxSector
5264 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
5265 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
5266 /* Replacement has just become active. */
5268 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
5271 /* Replaced device not technically faulty,
5272 * but we need to be sure it gets removed
5273 * and never re-added.
5275 set_bit(Faulty
, &tmp
->rdev
->flags
);
5276 sysfs_notify_dirent_safe(
5277 tmp
->rdev
->sysfs_state
);
5279 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
5280 } else if (tmp
->rdev
5281 && tmp
->rdev
->recovery_offset
== MaxSector
5282 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
5283 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
5285 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
5288 spin_lock_irqsave(&conf
->device_lock
, flags
);
5289 mddev
->degraded
= calc_degraded(conf
);
5290 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5291 print_raid5_conf(conf
);
5295 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5297 struct r5conf
*conf
= mddev
->private;
5299 int number
= rdev
->raid_disk
;
5300 struct md_rdev
**rdevp
;
5301 struct disk_info
*p
= conf
->disks
+ number
;
5303 print_raid5_conf(conf
);
5304 if (rdev
== p
->rdev
)
5306 else if (rdev
== p
->replacement
)
5307 rdevp
= &p
->replacement
;
5311 if (number
>= conf
->raid_disks
&&
5312 conf
->reshape_progress
== MaxSector
)
5313 clear_bit(In_sync
, &rdev
->flags
);
5315 if (test_bit(In_sync
, &rdev
->flags
) ||
5316 atomic_read(&rdev
->nr_pending
)) {
5320 /* Only remove non-faulty devices if recovery
5323 if (!test_bit(Faulty
, &rdev
->flags
) &&
5324 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
5325 !has_failed(conf
) &&
5326 (!p
->replacement
|| p
->replacement
== rdev
) &&
5327 number
< conf
->raid_disks
) {
5333 if (atomic_read(&rdev
->nr_pending
)) {
5334 /* lost the race, try later */
5337 } else if (p
->replacement
) {
5338 /* We must have just cleared 'rdev' */
5339 p
->rdev
= p
->replacement
;
5340 clear_bit(Replacement
, &p
->replacement
->flags
);
5341 smp_mb(); /* Make sure other CPUs may see both as identical
5342 * but will never see neither - if they are careful
5344 p
->replacement
= NULL
;
5345 clear_bit(WantReplacement
, &rdev
->flags
);
5347 /* We might have just removed the Replacement as faulty-
5348 * clear the bit just in case
5350 clear_bit(WantReplacement
, &rdev
->flags
);
5353 print_raid5_conf(conf
);
5357 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
5359 struct r5conf
*conf
= mddev
->private;
5362 struct disk_info
*p
;
5364 int last
= conf
->raid_disks
- 1;
5366 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
5369 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
5370 /* no point adding a device */
5373 if (rdev
->raid_disk
>= 0)
5374 first
= last
= rdev
->raid_disk
;
5377 * find the disk ... but prefer rdev->saved_raid_disk
5380 if (rdev
->saved_raid_disk
>= 0 &&
5381 rdev
->saved_raid_disk
>= first
&&
5382 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
5383 disk
= rdev
->saved_raid_disk
;
5386 for ( ; disk
<= last
; disk
++) {
5387 p
= conf
->disks
+ disk
;
5388 if (p
->rdev
== NULL
) {
5389 clear_bit(In_sync
, &rdev
->flags
);
5390 rdev
->raid_disk
= disk
;
5392 if (rdev
->saved_raid_disk
!= disk
)
5394 rcu_assign_pointer(p
->rdev
, rdev
);
5397 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
5398 p
->replacement
== NULL
) {
5399 clear_bit(In_sync
, &rdev
->flags
);
5400 set_bit(Replacement
, &rdev
->flags
);
5401 rdev
->raid_disk
= disk
;
5404 rcu_assign_pointer(p
->replacement
, rdev
);
5408 print_raid5_conf(conf
);
5412 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
5414 /* no resync is happening, and there is enough space
5415 * on all devices, so we can resize.
5416 * We need to make sure resync covers any new space.
5417 * If the array is shrinking we should possibly wait until
5418 * any io in the removed space completes, but it hardly seems
5421 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
5422 md_set_array_sectors(mddev
, raid5_size(mddev
, sectors
,
5423 mddev
->raid_disks
));
5424 if (mddev
->array_sectors
>
5425 raid5_size(mddev
, sectors
, mddev
->raid_disks
))
5427 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5428 revalidate_disk(mddev
->gendisk
);
5429 if (sectors
> mddev
->dev_sectors
&&
5430 mddev
->recovery_cp
> mddev
->dev_sectors
) {
5431 mddev
->recovery_cp
= mddev
->dev_sectors
;
5432 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5434 mddev
->dev_sectors
= sectors
;
5435 mddev
->resync_max_sectors
= sectors
;
5439 static int check_stripe_cache(struct mddev
*mddev
)
5441 /* Can only proceed if there are plenty of stripe_heads.
5442 * We need a minimum of one full stripe,, and for sensible progress
5443 * it is best to have about 4 times that.
5444 * If we require 4 times, then the default 256 4K stripe_heads will
5445 * allow for chunk sizes up to 256K, which is probably OK.
5446 * If the chunk size is greater, user-space should request more
5447 * stripe_heads first.
5449 struct r5conf
*conf
= mddev
->private;
5450 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5451 > conf
->max_nr_stripes
||
5452 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
5453 > conf
->max_nr_stripes
) {
5454 printk(KERN_WARNING
"md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5456 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
5463 static int check_reshape(struct mddev
*mddev
)
5465 struct r5conf
*conf
= mddev
->private;
5467 if (mddev
->delta_disks
== 0 &&
5468 mddev
->new_layout
== mddev
->layout
&&
5469 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
5470 return 0; /* nothing to do */
5472 /* Cannot grow a bitmap yet */
5474 if (has_failed(conf
))
5476 if (mddev
->delta_disks
< 0) {
5477 /* We might be able to shrink, but the devices must
5478 * be made bigger first.
5479 * For raid6, 4 is the minimum size.
5480 * Otherwise 2 is the minimum
5483 if (mddev
->level
== 6)
5485 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
5489 if (!check_stripe_cache(mddev
))
5492 return resize_stripes(conf
, conf
->raid_disks
+ mddev
->delta_disks
);
5495 static int raid5_start_reshape(struct mddev
*mddev
)
5497 struct r5conf
*conf
= mddev
->private;
5498 struct md_rdev
*rdev
;
5500 unsigned long flags
;
5502 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
5505 if (!check_stripe_cache(mddev
))
5508 rdev_for_each(rdev
, mddev
)
5509 if (!test_bit(In_sync
, &rdev
->flags
)
5510 && !test_bit(Faulty
, &rdev
->flags
))
5513 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
5514 /* Not enough devices even to make a degraded array
5519 /* Refuse to reduce size of the array. Any reductions in
5520 * array size must be through explicit setting of array_size
5523 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
5524 < mddev
->array_sectors
) {
5525 printk(KERN_ERR
"md/raid:%s: array size must be reduced "
5526 "before number of disks\n", mdname(mddev
));
5530 atomic_set(&conf
->reshape_stripes
, 0);
5531 spin_lock_irq(&conf
->device_lock
);
5532 conf
->previous_raid_disks
= conf
->raid_disks
;
5533 conf
->raid_disks
+= mddev
->delta_disks
;
5534 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
5535 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5536 conf
->prev_algo
= conf
->algorithm
;
5537 conf
->algorithm
= mddev
->new_layout
;
5538 if (mddev
->delta_disks
< 0)
5539 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
5541 conf
->reshape_progress
= 0;
5542 conf
->reshape_safe
= conf
->reshape_progress
;
5544 spin_unlock_irq(&conf
->device_lock
);
5546 /* Add some new drives, as many as will fit.
5547 * We know there are enough to make the newly sized array work.
5548 * Don't add devices if we are reducing the number of
5549 * devices in the array. This is because it is not possible
5550 * to correctly record the "partially reconstructed" state of
5551 * such devices during the reshape and confusion could result.
5553 if (mddev
->delta_disks
>= 0) {
5554 rdev_for_each(rdev
, mddev
)
5555 if (rdev
->raid_disk
< 0 &&
5556 !test_bit(Faulty
, &rdev
->flags
)) {
5557 if (raid5_add_disk(mddev
, rdev
) == 0) {
5559 >= conf
->previous_raid_disks
)
5560 set_bit(In_sync
, &rdev
->flags
);
5562 rdev
->recovery_offset
= 0;
5564 if (sysfs_link_rdev(mddev
, rdev
))
5565 /* Failure here is OK */;
5567 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
5568 && !test_bit(Faulty
, &rdev
->flags
)) {
5569 /* This is a spare that was manually added */
5570 set_bit(In_sync
, &rdev
->flags
);
5573 /* When a reshape changes the number of devices,
5574 * ->degraded is measured against the larger of the
5575 * pre and post number of devices.
5577 spin_lock_irqsave(&conf
->device_lock
, flags
);
5578 mddev
->degraded
= calc_degraded(conf
);
5579 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
5581 mddev
->raid_disks
= conf
->raid_disks
;
5582 mddev
->reshape_position
= conf
->reshape_progress
;
5583 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5585 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5586 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5587 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5588 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5589 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5591 if (!mddev
->sync_thread
) {
5592 mddev
->recovery
= 0;
5593 spin_lock_irq(&conf
->device_lock
);
5594 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
5595 conf
->reshape_progress
= MaxSector
;
5596 mddev
->reshape_position
= MaxSector
;
5597 spin_unlock_irq(&conf
->device_lock
);
5600 conf
->reshape_checkpoint
= jiffies
;
5601 md_wakeup_thread(mddev
->sync_thread
);
5602 md_new_event(mddev
);
5606 /* This is called from the reshape thread and should make any
5607 * changes needed in 'conf'
5609 static void end_reshape(struct r5conf
*conf
)
5612 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
5614 spin_lock_irq(&conf
->device_lock
);
5615 conf
->previous_raid_disks
= conf
->raid_disks
;
5616 conf
->reshape_progress
= MaxSector
;
5617 spin_unlock_irq(&conf
->device_lock
);
5618 wake_up(&conf
->wait_for_overlap
);
5620 /* read-ahead size must cover two whole stripes, which is
5621 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5623 if (conf
->mddev
->queue
) {
5624 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
5625 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
5627 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5628 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5633 /* This is called from the raid5d thread with mddev_lock held.
5634 * It makes config changes to the device.
5636 static void raid5_finish_reshape(struct mddev
*mddev
)
5638 struct r5conf
*conf
= mddev
->private;
5640 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
5642 if (mddev
->delta_disks
> 0) {
5643 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5644 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
5645 revalidate_disk(mddev
->gendisk
);
5648 spin_lock_irq(&conf
->device_lock
);
5649 mddev
->degraded
= calc_degraded(conf
);
5650 spin_unlock_irq(&conf
->device_lock
);
5651 for (d
= conf
->raid_disks
;
5652 d
< conf
->raid_disks
- mddev
->delta_disks
;
5654 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
5656 raid5_remove_disk(mddev
, rdev
) == 0) {
5657 sysfs_unlink_rdev(mddev
, rdev
);
5658 rdev
->raid_disk
= -1;
5662 mddev
->layout
= conf
->algorithm
;
5663 mddev
->chunk_sectors
= conf
->chunk_sectors
;
5664 mddev
->reshape_position
= MaxSector
;
5665 mddev
->delta_disks
= 0;
5669 static void raid5_quiesce(struct mddev
*mddev
, int state
)
5671 struct r5conf
*conf
= mddev
->private;
5674 case 2: /* resume for a suspend */
5675 wake_up(&conf
->wait_for_overlap
);
5678 case 1: /* stop all writes */
5679 spin_lock_irq(&conf
->device_lock
);
5680 /* '2' tells resync/reshape to pause so that all
5681 * active stripes can drain
5684 wait_event_lock_irq(conf
->wait_for_stripe
,
5685 atomic_read(&conf
->active_stripes
) == 0 &&
5686 atomic_read(&conf
->active_aligned_reads
) == 0,
5687 conf
->device_lock
, /* nothing */);
5689 spin_unlock_irq(&conf
->device_lock
);
5690 /* allow reshape to continue */
5691 wake_up(&conf
->wait_for_overlap
);
5694 case 0: /* re-enable writes */
5695 spin_lock_irq(&conf
->device_lock
);
5697 wake_up(&conf
->wait_for_stripe
);
5698 wake_up(&conf
->wait_for_overlap
);
5699 spin_unlock_irq(&conf
->device_lock
);
5705 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
5707 struct r0conf
*raid0_conf
= mddev
->private;
5710 /* for raid0 takeover only one zone is supported */
5711 if (raid0_conf
->nr_strip_zones
> 1) {
5712 printk(KERN_ERR
"md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5714 return ERR_PTR(-EINVAL
);
5717 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
5718 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
5719 mddev
->dev_sectors
= sectors
;
5720 mddev
->new_level
= level
;
5721 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5722 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
5723 mddev
->raid_disks
+= 1;
5724 mddev
->delta_disks
= 1;
5725 /* make sure it will be not marked as dirty */
5726 mddev
->recovery_cp
= MaxSector
;
5728 return setup_conf(mddev
);
5732 static void *raid5_takeover_raid1(struct mddev
*mddev
)
5736 if (mddev
->raid_disks
!= 2 ||
5737 mddev
->degraded
> 1)
5738 return ERR_PTR(-EINVAL
);
5740 /* Should check if there are write-behind devices? */
5742 chunksect
= 64*2; /* 64K by default */
5744 /* The array must be an exact multiple of chunksize */
5745 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
5748 if ((chunksect
<<9) < STRIPE_SIZE
)
5749 /* array size does not allow a suitable chunk size */
5750 return ERR_PTR(-EINVAL
);
5752 mddev
->new_level
= 5;
5753 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
5754 mddev
->new_chunk_sectors
= chunksect
;
5756 return setup_conf(mddev
);
5759 static void *raid5_takeover_raid6(struct mddev
*mddev
)
5763 switch (mddev
->layout
) {
5764 case ALGORITHM_LEFT_ASYMMETRIC_6
:
5765 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
5767 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
5768 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
5770 case ALGORITHM_LEFT_SYMMETRIC_6
:
5771 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
5773 case ALGORITHM_RIGHT_SYMMETRIC_6
:
5774 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
5776 case ALGORITHM_PARITY_0_6
:
5777 new_layout
= ALGORITHM_PARITY_0
;
5779 case ALGORITHM_PARITY_N
:
5780 new_layout
= ALGORITHM_PARITY_N
;
5783 return ERR_PTR(-EINVAL
);
5785 mddev
->new_level
= 5;
5786 mddev
->new_layout
= new_layout
;
5787 mddev
->delta_disks
= -1;
5788 mddev
->raid_disks
-= 1;
5789 return setup_conf(mddev
);
5793 static int raid5_check_reshape(struct mddev
*mddev
)
5795 /* For a 2-drive array, the layout and chunk size can be changed
5796 * immediately as not restriping is needed.
5797 * For larger arrays we record the new value - after validation
5798 * to be used by a reshape pass.
5800 struct r5conf
*conf
= mddev
->private;
5801 int new_chunk
= mddev
->new_chunk_sectors
;
5803 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
5805 if (new_chunk
> 0) {
5806 if (!is_power_of_2(new_chunk
))
5808 if (new_chunk
< (PAGE_SIZE
>>9))
5810 if (mddev
->array_sectors
& (new_chunk
-1))
5811 /* not factor of array size */
5815 /* They look valid */
5817 if (mddev
->raid_disks
== 2) {
5818 /* can make the change immediately */
5819 if (mddev
->new_layout
>= 0) {
5820 conf
->algorithm
= mddev
->new_layout
;
5821 mddev
->layout
= mddev
->new_layout
;
5823 if (new_chunk
> 0) {
5824 conf
->chunk_sectors
= new_chunk
;
5825 mddev
->chunk_sectors
= new_chunk
;
5827 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
5828 md_wakeup_thread(mddev
->thread
);
5830 return check_reshape(mddev
);
5833 static int raid6_check_reshape(struct mddev
*mddev
)
5835 int new_chunk
= mddev
->new_chunk_sectors
;
5837 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
5839 if (new_chunk
> 0) {
5840 if (!is_power_of_2(new_chunk
))
5842 if (new_chunk
< (PAGE_SIZE
>> 9))
5844 if (mddev
->array_sectors
& (new_chunk
-1))
5845 /* not factor of array size */
5849 /* They look valid */
5850 return check_reshape(mddev
);
5853 static void *raid5_takeover(struct mddev
*mddev
)
5855 /* raid5 can take over:
5856 * raid0 - if there is only one strip zone - make it a raid4 layout
5857 * raid1 - if there are two drives. We need to know the chunk size
5858 * raid4 - trivial - just use a raid4 layout.
5859 * raid6 - Providing it is a *_6 layout
5861 if (mddev
->level
== 0)
5862 return raid45_takeover_raid0(mddev
, 5);
5863 if (mddev
->level
== 1)
5864 return raid5_takeover_raid1(mddev
);
5865 if (mddev
->level
== 4) {
5866 mddev
->new_layout
= ALGORITHM_PARITY_N
;
5867 mddev
->new_level
= 5;
5868 return setup_conf(mddev
);
5870 if (mddev
->level
== 6)
5871 return raid5_takeover_raid6(mddev
);
5873 return ERR_PTR(-EINVAL
);
5876 static void *raid4_takeover(struct mddev
*mddev
)
5878 /* raid4 can take over:
5879 * raid0 - if there is only one strip zone
5880 * raid5 - if layout is right
5882 if (mddev
->level
== 0)
5883 return raid45_takeover_raid0(mddev
, 4);
5884 if (mddev
->level
== 5 &&
5885 mddev
->layout
== ALGORITHM_PARITY_N
) {
5886 mddev
->new_layout
= 0;
5887 mddev
->new_level
= 4;
5888 return setup_conf(mddev
);
5890 return ERR_PTR(-EINVAL
);
5893 static struct md_personality raid5_personality
;
5895 static void *raid6_takeover(struct mddev
*mddev
)
5897 /* Currently can only take over a raid5. We map the
5898 * personality to an equivalent raid6 personality
5899 * with the Q block at the end.
5903 if (mddev
->pers
!= &raid5_personality
)
5904 return ERR_PTR(-EINVAL
);
5905 if (mddev
->degraded
> 1)
5906 return ERR_PTR(-EINVAL
);
5907 if (mddev
->raid_disks
> 253)
5908 return ERR_PTR(-EINVAL
);
5909 if (mddev
->raid_disks
< 3)
5910 return ERR_PTR(-EINVAL
);
5912 switch (mddev
->layout
) {
5913 case ALGORITHM_LEFT_ASYMMETRIC
:
5914 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
5916 case ALGORITHM_RIGHT_ASYMMETRIC
:
5917 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
5919 case ALGORITHM_LEFT_SYMMETRIC
:
5920 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
5922 case ALGORITHM_RIGHT_SYMMETRIC
:
5923 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
5925 case ALGORITHM_PARITY_0
:
5926 new_layout
= ALGORITHM_PARITY_0_6
;
5928 case ALGORITHM_PARITY_N
:
5929 new_layout
= ALGORITHM_PARITY_N
;
5932 return ERR_PTR(-EINVAL
);
5934 mddev
->new_level
= 6;
5935 mddev
->new_layout
= new_layout
;
5936 mddev
->delta_disks
= 1;
5937 mddev
->raid_disks
+= 1;
5938 return setup_conf(mddev
);
5942 static struct md_personality raid6_personality
=
5946 .owner
= THIS_MODULE
,
5947 .make_request
= make_request
,
5951 .error_handler
= error
,
5952 .hot_add_disk
= raid5_add_disk
,
5953 .hot_remove_disk
= raid5_remove_disk
,
5954 .spare_active
= raid5_spare_active
,
5955 .sync_request
= sync_request
,
5956 .resize
= raid5_resize
,
5958 .check_reshape
= raid6_check_reshape
,
5959 .start_reshape
= raid5_start_reshape
,
5960 .finish_reshape
= raid5_finish_reshape
,
5961 .quiesce
= raid5_quiesce
,
5962 .takeover
= raid6_takeover
,
5964 static struct md_personality raid5_personality
=
5968 .owner
= THIS_MODULE
,
5969 .make_request
= make_request
,
5973 .error_handler
= error
,
5974 .hot_add_disk
= raid5_add_disk
,
5975 .hot_remove_disk
= raid5_remove_disk
,
5976 .spare_active
= raid5_spare_active
,
5977 .sync_request
= sync_request
,
5978 .resize
= raid5_resize
,
5980 .check_reshape
= raid5_check_reshape
,
5981 .start_reshape
= raid5_start_reshape
,
5982 .finish_reshape
= raid5_finish_reshape
,
5983 .quiesce
= raid5_quiesce
,
5984 .takeover
= raid5_takeover
,
5987 static struct md_personality raid4_personality
=
5991 .owner
= THIS_MODULE
,
5992 .make_request
= make_request
,
5996 .error_handler
= error
,
5997 .hot_add_disk
= raid5_add_disk
,
5998 .hot_remove_disk
= raid5_remove_disk
,
5999 .spare_active
= raid5_spare_active
,
6000 .sync_request
= sync_request
,
6001 .resize
= raid5_resize
,
6003 .check_reshape
= raid5_check_reshape
,
6004 .start_reshape
= raid5_start_reshape
,
6005 .finish_reshape
= raid5_finish_reshape
,
6006 .quiesce
= raid5_quiesce
,
6007 .takeover
= raid4_takeover
,
6010 static int __init
raid5_init(void)
6012 register_md_personality(&raid6_personality
);
6013 register_md_personality(&raid5_personality
);
6014 register_md_personality(&raid4_personality
);
6018 static void raid5_exit(void)
6020 unregister_md_personality(&raid6_personality
);
6021 unregister_md_personality(&raid5_personality
);
6022 unregister_md_personality(&raid4_personality
);
6025 module_init(raid5_init
);
6026 module_exit(raid5_exit
);
6027 MODULE_LICENSE("GPL");
6028 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6029 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6030 MODULE_ALIAS("md-raid5");
6031 MODULE_ALIAS("md-raid4");
6032 MODULE_ALIAS("md-level-5");
6033 MODULE_ALIAS("md-level-4");
6034 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6035 MODULE_ALIAS("md-raid6");
6036 MODULE_ALIAS("md-level-6");
6038 /* This used to be two separate modules, they were: */
6039 MODULE_ALIAS("raid5");
6040 MODULE_ALIAS("raid6");