2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is seq_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
56 #include <linux/nodemask.h>
57 #include <trace/events/block.h>
64 #define cpu_to_group(cpu) cpu_to_node(cpu)
65 #define ANY_GROUP NUMA_NO_NODE
67 static struct workqueue_struct
*raid5_wq
;
72 #define NR_STRIPES 256
73 #define STRIPE_SIZE PAGE_SIZE
74 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
75 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
76 #define IO_THRESHOLD 1
77 #define BYPASS_THRESHOLD 1
78 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
79 #define HASH_MASK (NR_HASH - 1)
80 #define MAX_STRIPE_BATCH 8
82 static inline struct hlist_head
*stripe_hash(struct r5conf
*conf
, sector_t sect
)
84 int hash
= (sect
>> STRIPE_SHIFT
) & HASH_MASK
;
85 return &conf
->stripe_hashtbl
[hash
];
88 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
89 * order without overlap. There may be several bio's per stripe+device, and
90 * a bio could span several devices.
91 * When walking this list for a particular stripe+device, we must never proceed
92 * beyond a bio that extends past this device, as the next bio might no longer
94 * This function is used to determine the 'next' bio in the list, given the sector
95 * of the current stripe+device
97 static inline struct bio
*r5_next_bio(struct bio
*bio
, sector_t sector
)
99 int sectors
= bio_sectors(bio
);
100 if (bio
->bi_sector
+ sectors
< sector
+ STRIPE_SECTORS
)
107 * We maintain a biased count of active stripes in the bottom 16 bits of
108 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
110 static inline int raid5_bi_processed_stripes(struct bio
*bio
)
112 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
113 return (atomic_read(segments
) >> 16) & 0xffff;
116 static inline int raid5_dec_bi_active_stripes(struct bio
*bio
)
118 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
119 return atomic_sub_return(1, segments
) & 0xffff;
122 static inline void raid5_inc_bi_active_stripes(struct bio
*bio
)
124 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
125 atomic_inc(segments
);
128 static inline void raid5_set_bi_processed_stripes(struct bio
*bio
,
131 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
135 old
= atomic_read(segments
);
136 new = (old
& 0xffff) | (cnt
<< 16);
137 } while (atomic_cmpxchg(segments
, old
, new) != old
);
140 static inline void raid5_set_bi_stripes(struct bio
*bio
, unsigned int cnt
)
142 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
143 atomic_set(segments
, cnt
);
146 /* Find first data disk in a raid6 stripe */
147 static inline int raid6_d0(struct stripe_head
*sh
)
150 /* ddf always start from first device */
152 /* md starts just after Q block */
153 if (sh
->qd_idx
== sh
->disks
- 1)
156 return sh
->qd_idx
+ 1;
158 static inline int raid6_next_disk(int disk
, int raid_disks
)
161 return (disk
< raid_disks
) ? disk
: 0;
164 /* When walking through the disks in a raid5, starting at raid6_d0,
165 * We need to map each disk to a 'slot', where the data disks are slot
166 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
167 * is raid_disks-1. This help does that mapping.
169 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
170 int *count
, int syndrome_disks
)
176 if (idx
== sh
->pd_idx
)
177 return syndrome_disks
;
178 if (idx
== sh
->qd_idx
)
179 return syndrome_disks
+ 1;
185 static void return_io(struct bio
*return_bi
)
187 struct bio
*bi
= return_bi
;
190 return_bi
= bi
->bi_next
;
193 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
200 static void print_raid5_conf (struct r5conf
*conf
);
202 static int stripe_operations_active(struct stripe_head
*sh
)
204 return sh
->check_state
|| sh
->reconstruct_state
||
205 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
206 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
209 static void raid5_wakeup_stripe_thread(struct stripe_head
*sh
)
211 struct r5conf
*conf
= sh
->raid_conf
;
212 struct r5worker_group
*group
;
214 int i
, cpu
= sh
->cpu
;
216 if (!cpu_online(cpu
)) {
217 cpu
= cpumask_any(cpu_online_mask
);
221 if (list_empty(&sh
->lru
)) {
222 struct r5worker_group
*group
;
223 group
= conf
->worker_groups
+ cpu_to_group(cpu
);
224 list_add_tail(&sh
->lru
, &group
->handle_list
);
225 group
->stripes_cnt
++;
229 if (conf
->worker_cnt_per_group
== 0) {
230 md_wakeup_thread(conf
->mddev
->thread
);
234 group
= conf
->worker_groups
+ cpu_to_group(sh
->cpu
);
236 group
->workers
[0].working
= true;
237 /* at least one worker should run to avoid race */
238 queue_work_on(sh
->cpu
, raid5_wq
, &group
->workers
[0].work
);
240 thread_cnt
= group
->stripes_cnt
/ MAX_STRIPE_BATCH
- 1;
241 /* wakeup more workers */
242 for (i
= 1; i
< conf
->worker_cnt_per_group
&& thread_cnt
> 0; i
++) {
243 if (group
->workers
[i
].working
== false) {
244 group
->workers
[i
].working
= true;
245 queue_work_on(sh
->cpu
, raid5_wq
,
246 &group
->workers
[i
].work
);
252 static void do_release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
)
254 BUG_ON(!list_empty(&sh
->lru
));
255 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
256 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
257 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
258 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
259 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
260 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
261 sh
->bm_seq
- conf
->seq_write
> 0)
262 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
264 clear_bit(STRIPE_DELAYED
, &sh
->state
);
265 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
266 if (conf
->worker_cnt_per_group
== 0) {
267 list_add_tail(&sh
->lru
, &conf
->handle_list
);
269 raid5_wakeup_stripe_thread(sh
);
273 md_wakeup_thread(conf
->mddev
->thread
);
275 BUG_ON(stripe_operations_active(sh
));
276 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
277 if (atomic_dec_return(&conf
->preread_active_stripes
)
279 md_wakeup_thread(conf
->mddev
->thread
);
280 atomic_dec(&conf
->active_stripes
);
281 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
282 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
283 wake_up(&conf
->wait_for_stripe
);
284 if (conf
->retry_read_aligned
)
285 md_wakeup_thread(conf
->mddev
->thread
);
290 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
)
292 if (atomic_dec_and_test(&sh
->count
))
293 do_release_stripe(conf
, sh
);
296 static struct llist_node
*llist_reverse_order(struct llist_node
*head
)
298 struct llist_node
*new_head
= NULL
;
301 struct llist_node
*tmp
= head
;
303 tmp
->next
= new_head
;
310 /* should hold conf->device_lock already */
311 static int release_stripe_list(struct r5conf
*conf
)
313 struct stripe_head
*sh
;
315 struct llist_node
*head
;
317 head
= llist_del_all(&conf
->released_stripes
);
318 head
= llist_reverse_order(head
);
320 sh
= llist_entry(head
, struct stripe_head
, release_list
);
321 head
= llist_next(head
);
322 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
324 clear_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
);
326 * Don't worry the bit is set here, because if the bit is set
327 * again, the count is always > 1. This is true for
328 * STRIPE_ON_UNPLUG_LIST bit too.
330 __release_stripe(conf
, sh
);
337 static void release_stripe(struct stripe_head
*sh
)
339 struct r5conf
*conf
= sh
->raid_conf
;
343 if (test_and_set_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
))
345 wakeup
= llist_add(&sh
->release_list
, &conf
->released_stripes
);
347 md_wakeup_thread(conf
->mddev
->thread
);
350 local_irq_save(flags
);
351 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
352 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
353 do_release_stripe(conf
, sh
);
354 spin_unlock(&conf
->device_lock
);
356 local_irq_restore(flags
);
359 static inline void remove_hash(struct stripe_head
*sh
)
361 pr_debug("remove_hash(), stripe %llu\n",
362 (unsigned long long)sh
->sector
);
364 hlist_del_init(&sh
->hash
);
367 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
369 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
371 pr_debug("insert_hash(), stripe %llu\n",
372 (unsigned long long)sh
->sector
);
374 hlist_add_head(&sh
->hash
, hp
);
378 /* find an idle stripe, make sure it is unhashed, and return it. */
379 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
)
381 struct stripe_head
*sh
= NULL
;
382 struct list_head
*first
;
384 if (list_empty(&conf
->inactive_list
))
386 first
= conf
->inactive_list
.next
;
387 sh
= list_entry(first
, struct stripe_head
, lru
);
388 list_del_init(first
);
390 atomic_inc(&conf
->active_stripes
);
395 static void shrink_buffers(struct stripe_head
*sh
)
399 int num
= sh
->raid_conf
->pool_size
;
401 for (i
= 0; i
< num
; i
++) {
405 sh
->dev
[i
].page
= NULL
;
410 static int grow_buffers(struct stripe_head
*sh
)
413 int num
= sh
->raid_conf
->pool_size
;
415 for (i
= 0; i
< num
; i
++) {
418 if (!(page
= alloc_page(GFP_KERNEL
))) {
421 sh
->dev
[i
].page
= page
;
426 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
427 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
428 struct stripe_head
*sh
);
430 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
432 struct r5conf
*conf
= sh
->raid_conf
;
435 BUG_ON(atomic_read(&sh
->count
) != 0);
436 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
437 BUG_ON(stripe_operations_active(sh
));
439 pr_debug("init_stripe called, stripe %llu\n",
440 (unsigned long long)sh
->sector
);
444 sh
->generation
= conf
->generation
- previous
;
445 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
447 stripe_set_idx(sector
, conf
, previous
, sh
);
451 for (i
= sh
->disks
; i
--; ) {
452 struct r5dev
*dev
= &sh
->dev
[i
];
454 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
455 test_bit(R5_LOCKED
, &dev
->flags
)) {
456 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
457 (unsigned long long)sh
->sector
, i
, dev
->toread
,
458 dev
->read
, dev
->towrite
, dev
->written
,
459 test_bit(R5_LOCKED
, &dev
->flags
));
463 raid5_build_block(sh
, i
, previous
);
465 insert_hash(conf
, sh
);
466 sh
->cpu
= smp_processor_id();
469 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
472 struct stripe_head
*sh
;
474 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
475 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
476 if (sh
->sector
== sector
&& sh
->generation
== generation
)
478 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
483 * Need to check if array has failed when deciding whether to:
485 * - remove non-faulty devices
488 * This determination is simple when no reshape is happening.
489 * However if there is a reshape, we need to carefully check
490 * both the before and after sections.
491 * This is because some failed devices may only affect one
492 * of the two sections, and some non-in_sync devices may
493 * be insync in the section most affected by failed devices.
495 static int calc_degraded(struct r5conf
*conf
)
497 int degraded
, degraded2
;
502 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
503 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
504 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
505 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
506 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
508 else if (test_bit(In_sync
, &rdev
->flags
))
511 /* not in-sync or faulty.
512 * If the reshape increases the number of devices,
513 * this is being recovered by the reshape, so
514 * this 'previous' section is not in_sync.
515 * If the number of devices is being reduced however,
516 * the device can only be part of the array if
517 * we are reverting a reshape, so this section will
520 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
524 if (conf
->raid_disks
== conf
->previous_raid_disks
)
528 for (i
= 0; i
< conf
->raid_disks
; i
++) {
529 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
530 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
531 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
532 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
534 else if (test_bit(In_sync
, &rdev
->flags
))
537 /* not in-sync or faulty.
538 * If reshape increases the number of devices, this
539 * section has already been recovered, else it
540 * almost certainly hasn't.
542 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
546 if (degraded2
> degraded
)
551 static int has_failed(struct r5conf
*conf
)
555 if (conf
->mddev
->reshape_position
== MaxSector
)
556 return conf
->mddev
->degraded
> conf
->max_degraded
;
558 degraded
= calc_degraded(conf
);
559 if (degraded
> conf
->max_degraded
)
564 static struct stripe_head
*
565 get_active_stripe(struct r5conf
*conf
, sector_t sector
,
566 int previous
, int noblock
, int noquiesce
)
568 struct stripe_head
*sh
;
570 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
572 spin_lock_irq(&conf
->device_lock
);
575 wait_event_lock_irq(conf
->wait_for_stripe
,
576 conf
->quiesce
== 0 || noquiesce
,
578 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
580 if (!conf
->inactive_blocked
)
581 sh
= get_free_stripe(conf
);
582 if (noblock
&& sh
== NULL
)
585 conf
->inactive_blocked
= 1;
586 wait_event_lock_irq(conf
->wait_for_stripe
,
587 !list_empty(&conf
->inactive_list
) &&
588 (atomic_read(&conf
->active_stripes
)
589 < (conf
->max_nr_stripes
*3/4)
590 || !conf
->inactive_blocked
),
592 conf
->inactive_blocked
= 0;
594 init_stripe(sh
, sector
, previous
);
596 if (atomic_read(&sh
->count
)) {
597 BUG_ON(!list_empty(&sh
->lru
)
598 && !test_bit(STRIPE_EXPANDING
, &sh
->state
)
599 && !test_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
)
600 && !test_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
));
602 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
603 atomic_inc(&conf
->active_stripes
);
604 if (list_empty(&sh
->lru
) &&
605 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
607 list_del_init(&sh
->lru
);
609 sh
->group
->stripes_cnt
--;
614 } while (sh
== NULL
);
617 atomic_inc(&sh
->count
);
619 spin_unlock_irq(&conf
->device_lock
);
623 /* Determine if 'data_offset' or 'new_data_offset' should be used
624 * in this stripe_head.
626 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
628 sector_t progress
= conf
->reshape_progress
;
629 /* Need a memory barrier to make sure we see the value
630 * of conf->generation, or ->data_offset that was set before
631 * reshape_progress was updated.
634 if (progress
== MaxSector
)
636 if (sh
->generation
== conf
->generation
- 1)
638 /* We are in a reshape, and this is a new-generation stripe,
639 * so use new_data_offset.
645 raid5_end_read_request(struct bio
*bi
, int error
);
647 raid5_end_write_request(struct bio
*bi
, int error
);
649 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
651 struct r5conf
*conf
= sh
->raid_conf
;
652 int i
, disks
= sh
->disks
;
656 for (i
= disks
; i
--; ) {
658 int replace_only
= 0;
659 struct bio
*bi
, *rbi
;
660 struct md_rdev
*rdev
, *rrdev
= NULL
;
661 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
662 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
666 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
668 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
670 else if (test_and_clear_bit(R5_WantReplace
,
671 &sh
->dev
[i
].flags
)) {
676 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
679 bi
= &sh
->dev
[i
].req
;
680 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
683 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
684 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
685 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
694 /* We raced and saw duplicates */
697 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
) && rrdev
)
702 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
705 atomic_inc(&rdev
->nr_pending
);
706 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
709 atomic_inc(&rrdev
->nr_pending
);
712 /* We have already checked bad blocks for reads. Now
713 * need to check for writes. We never accept write errors
714 * on the replacement, so we don't to check rrdev.
716 while ((rw
& WRITE
) && rdev
&&
717 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
720 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
721 &first_bad
, &bad_sectors
);
726 set_bit(BlockedBadBlocks
, &rdev
->flags
);
727 if (!conf
->mddev
->external
&&
728 conf
->mddev
->flags
) {
729 /* It is very unlikely, but we might
730 * still need to write out the
731 * bad block log - better give it
733 md_check_recovery(conf
->mddev
);
736 * Because md_wait_for_blocked_rdev
737 * will dec nr_pending, we must
738 * increment it first.
740 atomic_inc(&rdev
->nr_pending
);
741 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
743 /* Acknowledged bad block - skip the write */
744 rdev_dec_pending(rdev
, conf
->mddev
);
750 if (s
->syncing
|| s
->expanding
|| s
->expanded
752 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
754 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
757 bi
->bi_bdev
= rdev
->bdev
;
759 bi
->bi_end_io
= (rw
& WRITE
)
760 ? raid5_end_write_request
761 : raid5_end_read_request
;
764 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
765 __func__
, (unsigned long long)sh
->sector
,
767 atomic_inc(&sh
->count
);
768 if (use_new_offset(conf
, sh
))
769 bi
->bi_sector
= (sh
->sector
770 + rdev
->new_data_offset
);
772 bi
->bi_sector
= (sh
->sector
773 + rdev
->data_offset
);
774 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
775 bi
->bi_rw
|= REQ_FLUSH
;
778 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
779 bi
->bi_io_vec
[0].bv_offset
= 0;
780 bi
->bi_size
= STRIPE_SIZE
;
782 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
784 if (conf
->mddev
->gendisk
)
785 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
786 bi
, disk_devt(conf
->mddev
->gendisk
),
788 generic_make_request(bi
);
791 if (s
->syncing
|| s
->expanding
|| s
->expanded
793 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
795 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
798 rbi
->bi_bdev
= rrdev
->bdev
;
800 BUG_ON(!(rw
& WRITE
));
801 rbi
->bi_end_io
= raid5_end_write_request
;
802 rbi
->bi_private
= sh
;
804 pr_debug("%s: for %llu schedule op %ld on "
805 "replacement disc %d\n",
806 __func__
, (unsigned long long)sh
->sector
,
808 atomic_inc(&sh
->count
);
809 if (use_new_offset(conf
, sh
))
810 rbi
->bi_sector
= (sh
->sector
811 + rrdev
->new_data_offset
);
813 rbi
->bi_sector
= (sh
->sector
814 + rrdev
->data_offset
);
816 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
817 rbi
->bi_io_vec
[0].bv_offset
= 0;
818 rbi
->bi_size
= STRIPE_SIZE
;
819 if (conf
->mddev
->gendisk
)
820 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
821 rbi
, disk_devt(conf
->mddev
->gendisk
),
823 generic_make_request(rbi
);
825 if (!rdev
&& !rrdev
) {
827 set_bit(STRIPE_DEGRADED
, &sh
->state
);
828 pr_debug("skip op %ld on disc %d for sector %llu\n",
829 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
830 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
831 set_bit(STRIPE_HANDLE
, &sh
->state
);
836 static struct dma_async_tx_descriptor
*
837 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
838 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
841 struct page
*bio_page
;
844 struct async_submit_ctl submit
;
845 enum async_tx_flags flags
= 0;
847 if (bio
->bi_sector
>= sector
)
848 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
850 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
853 flags
|= ASYNC_TX_FENCE
;
854 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
856 bio_for_each_segment(bvl
, bio
, i
) {
857 int len
= bvl
->bv_len
;
861 if (page_offset
< 0) {
862 b_offset
= -page_offset
;
863 page_offset
+= b_offset
;
867 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
868 clen
= STRIPE_SIZE
- page_offset
;
873 b_offset
+= bvl
->bv_offset
;
874 bio_page
= bvl
->bv_page
;
876 tx
= async_memcpy(page
, bio_page
, page_offset
,
877 b_offset
, clen
, &submit
);
879 tx
= async_memcpy(bio_page
, page
, b_offset
,
880 page_offset
, clen
, &submit
);
882 /* chain the operations */
883 submit
.depend_tx
= tx
;
885 if (clen
< len
) /* hit end of page */
893 static void ops_complete_biofill(void *stripe_head_ref
)
895 struct stripe_head
*sh
= stripe_head_ref
;
896 struct bio
*return_bi
= NULL
;
899 pr_debug("%s: stripe %llu\n", __func__
,
900 (unsigned long long)sh
->sector
);
902 /* clear completed biofills */
903 for (i
= sh
->disks
; i
--; ) {
904 struct r5dev
*dev
= &sh
->dev
[i
];
906 /* acknowledge completion of a biofill operation */
907 /* and check if we need to reply to a read request,
908 * new R5_Wantfill requests are held off until
909 * !STRIPE_BIOFILL_RUN
911 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
912 struct bio
*rbi
, *rbi2
;
917 while (rbi
&& rbi
->bi_sector
<
918 dev
->sector
+ STRIPE_SECTORS
) {
919 rbi2
= r5_next_bio(rbi
, dev
->sector
);
920 if (!raid5_dec_bi_active_stripes(rbi
)) {
921 rbi
->bi_next
= return_bi
;
928 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
930 return_io(return_bi
);
932 set_bit(STRIPE_HANDLE
, &sh
->state
);
936 static void ops_run_biofill(struct stripe_head
*sh
)
938 struct dma_async_tx_descriptor
*tx
= NULL
;
939 struct async_submit_ctl submit
;
942 pr_debug("%s: stripe %llu\n", __func__
,
943 (unsigned long long)sh
->sector
);
945 for (i
= sh
->disks
; i
--; ) {
946 struct r5dev
*dev
= &sh
->dev
[i
];
947 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
949 spin_lock_irq(&sh
->stripe_lock
);
950 dev
->read
= rbi
= dev
->toread
;
952 spin_unlock_irq(&sh
->stripe_lock
);
953 while (rbi
&& rbi
->bi_sector
<
954 dev
->sector
+ STRIPE_SECTORS
) {
955 tx
= async_copy_data(0, rbi
, dev
->page
,
957 rbi
= r5_next_bio(rbi
, dev
->sector
);
962 atomic_inc(&sh
->count
);
963 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
964 async_trigger_callback(&submit
);
967 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
974 tgt
= &sh
->dev
[target
];
975 set_bit(R5_UPTODATE
, &tgt
->flags
);
976 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
977 clear_bit(R5_Wantcompute
, &tgt
->flags
);
980 static void ops_complete_compute(void *stripe_head_ref
)
982 struct stripe_head
*sh
= stripe_head_ref
;
984 pr_debug("%s: stripe %llu\n", __func__
,
985 (unsigned long long)sh
->sector
);
987 /* mark the computed target(s) as uptodate */
988 mark_target_uptodate(sh
, sh
->ops
.target
);
989 mark_target_uptodate(sh
, sh
->ops
.target2
);
991 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
992 if (sh
->check_state
== check_state_compute_run
)
993 sh
->check_state
= check_state_compute_result
;
994 set_bit(STRIPE_HANDLE
, &sh
->state
);
998 /* return a pointer to the address conversion region of the scribble buffer */
999 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
1000 struct raid5_percpu
*percpu
)
1002 return percpu
->scribble
+ sizeof(struct page
*) * (sh
->disks
+ 2);
1005 static struct dma_async_tx_descriptor
*
1006 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1008 int disks
= sh
->disks
;
1009 struct page
**xor_srcs
= percpu
->scribble
;
1010 int target
= sh
->ops
.target
;
1011 struct r5dev
*tgt
= &sh
->dev
[target
];
1012 struct page
*xor_dest
= tgt
->page
;
1014 struct dma_async_tx_descriptor
*tx
;
1015 struct async_submit_ctl submit
;
1018 pr_debug("%s: stripe %llu block: %d\n",
1019 __func__
, (unsigned long long)sh
->sector
, target
);
1020 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1022 for (i
= disks
; i
--; )
1024 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1026 atomic_inc(&sh
->count
);
1028 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
1029 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
));
1030 if (unlikely(count
== 1))
1031 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1033 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1038 /* set_syndrome_sources - populate source buffers for gen_syndrome
1039 * @srcs - (struct page *) array of size sh->disks
1040 * @sh - stripe_head to parse
1042 * Populates srcs in proper layout order for the stripe and returns the
1043 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1044 * destination buffer is recorded in srcs[count] and the Q destination
1045 * is recorded in srcs[count+1]].
1047 static int set_syndrome_sources(struct page
**srcs
, struct stripe_head
*sh
)
1049 int disks
= sh
->disks
;
1050 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1051 int d0_idx
= raid6_d0(sh
);
1055 for (i
= 0; i
< disks
; i
++)
1061 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1063 srcs
[slot
] = sh
->dev
[i
].page
;
1064 i
= raid6_next_disk(i
, disks
);
1065 } while (i
!= d0_idx
);
1067 return syndrome_disks
;
1070 static struct dma_async_tx_descriptor
*
1071 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1073 int disks
= sh
->disks
;
1074 struct page
**blocks
= percpu
->scribble
;
1076 int qd_idx
= sh
->qd_idx
;
1077 struct dma_async_tx_descriptor
*tx
;
1078 struct async_submit_ctl submit
;
1084 if (sh
->ops
.target
< 0)
1085 target
= sh
->ops
.target2
;
1086 else if (sh
->ops
.target2
< 0)
1087 target
= sh
->ops
.target
;
1089 /* we should only have one valid target */
1092 pr_debug("%s: stripe %llu block: %d\n",
1093 __func__
, (unsigned long long)sh
->sector
, target
);
1095 tgt
= &sh
->dev
[target
];
1096 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1099 atomic_inc(&sh
->count
);
1101 if (target
== qd_idx
) {
1102 count
= set_syndrome_sources(blocks
, sh
);
1103 blocks
[count
] = NULL
; /* regenerating p is not necessary */
1104 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
1105 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1106 ops_complete_compute
, sh
,
1107 to_addr_conv(sh
, percpu
));
1108 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1110 /* Compute any data- or p-drive using XOR */
1112 for (i
= disks
; i
-- ; ) {
1113 if (i
== target
|| i
== qd_idx
)
1115 blocks
[count
++] = sh
->dev
[i
].page
;
1118 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1119 NULL
, ops_complete_compute
, sh
,
1120 to_addr_conv(sh
, percpu
));
1121 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1127 static struct dma_async_tx_descriptor
*
1128 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1130 int i
, count
, disks
= sh
->disks
;
1131 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1132 int d0_idx
= raid6_d0(sh
);
1133 int faila
= -1, failb
= -1;
1134 int target
= sh
->ops
.target
;
1135 int target2
= sh
->ops
.target2
;
1136 struct r5dev
*tgt
= &sh
->dev
[target
];
1137 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1138 struct dma_async_tx_descriptor
*tx
;
1139 struct page
**blocks
= percpu
->scribble
;
1140 struct async_submit_ctl submit
;
1142 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1143 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1144 BUG_ON(target
< 0 || target2
< 0);
1145 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1146 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1148 /* we need to open-code set_syndrome_sources to handle the
1149 * slot number conversion for 'faila' and 'failb'
1151 for (i
= 0; i
< disks
; i
++)
1156 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1158 blocks
[slot
] = sh
->dev
[i
].page
;
1164 i
= raid6_next_disk(i
, disks
);
1165 } while (i
!= d0_idx
);
1167 BUG_ON(faila
== failb
);
1170 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1171 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1173 atomic_inc(&sh
->count
);
1175 if (failb
== syndrome_disks
+1) {
1176 /* Q disk is one of the missing disks */
1177 if (faila
== syndrome_disks
) {
1178 /* Missing P+Q, just recompute */
1179 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1180 ops_complete_compute
, sh
,
1181 to_addr_conv(sh
, percpu
));
1182 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1183 STRIPE_SIZE
, &submit
);
1187 int qd_idx
= sh
->qd_idx
;
1189 /* Missing D+Q: recompute D from P, then recompute Q */
1190 if (target
== qd_idx
)
1191 data_target
= target2
;
1193 data_target
= target
;
1196 for (i
= disks
; i
-- ; ) {
1197 if (i
== data_target
|| i
== qd_idx
)
1199 blocks
[count
++] = sh
->dev
[i
].page
;
1201 dest
= sh
->dev
[data_target
].page
;
1202 init_async_submit(&submit
,
1203 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1205 to_addr_conv(sh
, percpu
));
1206 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1209 count
= set_syndrome_sources(blocks
, sh
);
1210 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1211 ops_complete_compute
, sh
,
1212 to_addr_conv(sh
, percpu
));
1213 return async_gen_syndrome(blocks
, 0, count
+2,
1214 STRIPE_SIZE
, &submit
);
1217 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1218 ops_complete_compute
, sh
,
1219 to_addr_conv(sh
, percpu
));
1220 if (failb
== syndrome_disks
) {
1221 /* We're missing D+P. */
1222 return async_raid6_datap_recov(syndrome_disks
+2,
1226 /* We're missing D+D. */
1227 return async_raid6_2data_recov(syndrome_disks
+2,
1228 STRIPE_SIZE
, faila
, failb
,
1235 static void ops_complete_prexor(void *stripe_head_ref
)
1237 struct stripe_head
*sh
= stripe_head_ref
;
1239 pr_debug("%s: stripe %llu\n", __func__
,
1240 (unsigned long long)sh
->sector
);
1243 static struct dma_async_tx_descriptor
*
1244 ops_run_prexor(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1245 struct dma_async_tx_descriptor
*tx
)
1247 int disks
= sh
->disks
;
1248 struct page
**xor_srcs
= percpu
->scribble
;
1249 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1250 struct async_submit_ctl submit
;
1252 /* existing parity data subtracted */
1253 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1255 pr_debug("%s: stripe %llu\n", __func__
,
1256 (unsigned long long)sh
->sector
);
1258 for (i
= disks
; i
--; ) {
1259 struct r5dev
*dev
= &sh
->dev
[i
];
1260 /* Only process blocks that are known to be uptodate */
1261 if (test_bit(R5_Wantdrain
, &dev
->flags
))
1262 xor_srcs
[count
++] = dev
->page
;
1265 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1266 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
));
1267 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1272 static struct dma_async_tx_descriptor
*
1273 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1275 int disks
= sh
->disks
;
1278 pr_debug("%s: stripe %llu\n", __func__
,
1279 (unsigned long long)sh
->sector
);
1281 for (i
= disks
; i
--; ) {
1282 struct r5dev
*dev
= &sh
->dev
[i
];
1285 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
1288 spin_lock_irq(&sh
->stripe_lock
);
1289 chosen
= dev
->towrite
;
1290 dev
->towrite
= NULL
;
1291 BUG_ON(dev
->written
);
1292 wbi
= dev
->written
= chosen
;
1293 spin_unlock_irq(&sh
->stripe_lock
);
1295 while (wbi
&& wbi
->bi_sector
<
1296 dev
->sector
+ STRIPE_SECTORS
) {
1297 if (wbi
->bi_rw
& REQ_FUA
)
1298 set_bit(R5_WantFUA
, &dev
->flags
);
1299 if (wbi
->bi_rw
& REQ_SYNC
)
1300 set_bit(R5_SyncIO
, &dev
->flags
);
1301 if (wbi
->bi_rw
& REQ_DISCARD
)
1302 set_bit(R5_Discard
, &dev
->flags
);
1304 tx
= async_copy_data(1, wbi
, dev
->page
,
1306 wbi
= r5_next_bio(wbi
, dev
->sector
);
1314 static void ops_complete_reconstruct(void *stripe_head_ref
)
1316 struct stripe_head
*sh
= stripe_head_ref
;
1317 int disks
= sh
->disks
;
1318 int pd_idx
= sh
->pd_idx
;
1319 int qd_idx
= sh
->qd_idx
;
1321 bool fua
= false, sync
= false, discard
= false;
1323 pr_debug("%s: stripe %llu\n", __func__
,
1324 (unsigned long long)sh
->sector
);
1326 for (i
= disks
; i
--; ) {
1327 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1328 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1329 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1332 for (i
= disks
; i
--; ) {
1333 struct r5dev
*dev
= &sh
->dev
[i
];
1335 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1337 set_bit(R5_UPTODATE
, &dev
->flags
);
1339 set_bit(R5_WantFUA
, &dev
->flags
);
1341 set_bit(R5_SyncIO
, &dev
->flags
);
1345 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1346 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1347 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1348 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1350 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1351 sh
->reconstruct_state
= reconstruct_state_result
;
1354 set_bit(STRIPE_HANDLE
, &sh
->state
);
1359 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1360 struct dma_async_tx_descriptor
*tx
)
1362 int disks
= sh
->disks
;
1363 struct page
**xor_srcs
= percpu
->scribble
;
1364 struct async_submit_ctl submit
;
1365 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1366 struct page
*xor_dest
;
1368 unsigned long flags
;
1370 pr_debug("%s: stripe %llu\n", __func__
,
1371 (unsigned long long)sh
->sector
);
1373 for (i
= 0; i
< sh
->disks
; i
++) {
1376 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1379 if (i
>= sh
->disks
) {
1380 atomic_inc(&sh
->count
);
1381 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1382 ops_complete_reconstruct(sh
);
1385 /* check if prexor is active which means only process blocks
1386 * that are part of a read-modify-write (written)
1388 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1390 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1391 for (i
= disks
; i
--; ) {
1392 struct r5dev
*dev
= &sh
->dev
[i
];
1394 xor_srcs
[count
++] = dev
->page
;
1397 xor_dest
= sh
->dev
[pd_idx
].page
;
1398 for (i
= disks
; i
--; ) {
1399 struct r5dev
*dev
= &sh
->dev
[i
];
1401 xor_srcs
[count
++] = dev
->page
;
1405 /* 1/ if we prexor'd then the dest is reused as a source
1406 * 2/ if we did not prexor then we are redoing the parity
1407 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1408 * for the synchronous xor case
1410 flags
= ASYNC_TX_ACK
|
1411 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1413 atomic_inc(&sh
->count
);
1415 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, sh
,
1416 to_addr_conv(sh
, percpu
));
1417 if (unlikely(count
== 1))
1418 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1420 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1424 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1425 struct dma_async_tx_descriptor
*tx
)
1427 struct async_submit_ctl submit
;
1428 struct page
**blocks
= percpu
->scribble
;
1431 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1433 for (i
= 0; i
< sh
->disks
; i
++) {
1434 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1436 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1439 if (i
>= sh
->disks
) {
1440 atomic_inc(&sh
->count
);
1441 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1442 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1443 ops_complete_reconstruct(sh
);
1447 count
= set_syndrome_sources(blocks
, sh
);
1449 atomic_inc(&sh
->count
);
1451 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_reconstruct
,
1452 sh
, to_addr_conv(sh
, percpu
));
1453 async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1456 static void ops_complete_check(void *stripe_head_ref
)
1458 struct stripe_head
*sh
= stripe_head_ref
;
1460 pr_debug("%s: stripe %llu\n", __func__
,
1461 (unsigned long long)sh
->sector
);
1463 sh
->check_state
= check_state_check_result
;
1464 set_bit(STRIPE_HANDLE
, &sh
->state
);
1468 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1470 int disks
= sh
->disks
;
1471 int pd_idx
= sh
->pd_idx
;
1472 int qd_idx
= sh
->qd_idx
;
1473 struct page
*xor_dest
;
1474 struct page
**xor_srcs
= percpu
->scribble
;
1475 struct dma_async_tx_descriptor
*tx
;
1476 struct async_submit_ctl submit
;
1480 pr_debug("%s: stripe %llu\n", __func__
,
1481 (unsigned long long)sh
->sector
);
1484 xor_dest
= sh
->dev
[pd_idx
].page
;
1485 xor_srcs
[count
++] = xor_dest
;
1486 for (i
= disks
; i
--; ) {
1487 if (i
== pd_idx
|| i
== qd_idx
)
1489 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1492 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1493 to_addr_conv(sh
, percpu
));
1494 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1495 &sh
->ops
.zero_sum_result
, &submit
);
1497 atomic_inc(&sh
->count
);
1498 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1499 tx
= async_trigger_callback(&submit
);
1502 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1504 struct page
**srcs
= percpu
->scribble
;
1505 struct async_submit_ctl submit
;
1508 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1509 (unsigned long long)sh
->sector
, checkp
);
1511 count
= set_syndrome_sources(srcs
, sh
);
1515 atomic_inc(&sh
->count
);
1516 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1517 sh
, to_addr_conv(sh
, percpu
));
1518 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1519 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1522 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1524 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1525 struct dma_async_tx_descriptor
*tx
= NULL
;
1526 struct r5conf
*conf
= sh
->raid_conf
;
1527 int level
= conf
->level
;
1528 struct raid5_percpu
*percpu
;
1532 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1533 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1534 ops_run_biofill(sh
);
1538 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1540 tx
= ops_run_compute5(sh
, percpu
);
1542 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1543 tx
= ops_run_compute6_1(sh
, percpu
);
1545 tx
= ops_run_compute6_2(sh
, percpu
);
1547 /* terminate the chain if reconstruct is not set to be run */
1548 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1552 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
1553 tx
= ops_run_prexor(sh
, percpu
, tx
);
1555 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1556 tx
= ops_run_biodrain(sh
, tx
);
1560 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1562 ops_run_reconstruct5(sh
, percpu
, tx
);
1564 ops_run_reconstruct6(sh
, percpu
, tx
);
1567 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1568 if (sh
->check_state
== check_state_run
)
1569 ops_run_check_p(sh
, percpu
);
1570 else if (sh
->check_state
== check_state_run_q
)
1571 ops_run_check_pq(sh
, percpu
, 0);
1572 else if (sh
->check_state
== check_state_run_pq
)
1573 ops_run_check_pq(sh
, percpu
, 1);
1579 for (i
= disks
; i
--; ) {
1580 struct r5dev
*dev
= &sh
->dev
[i
];
1581 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1582 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1587 static int grow_one_stripe(struct r5conf
*conf
)
1589 struct stripe_head
*sh
;
1590 sh
= kmem_cache_zalloc(conf
->slab_cache
, GFP_KERNEL
);
1594 sh
->raid_conf
= conf
;
1596 spin_lock_init(&sh
->stripe_lock
);
1598 if (grow_buffers(sh
)) {
1600 kmem_cache_free(conf
->slab_cache
, sh
);
1603 /* we just created an active stripe so... */
1604 atomic_set(&sh
->count
, 1);
1605 atomic_inc(&conf
->active_stripes
);
1606 INIT_LIST_HEAD(&sh
->lru
);
1611 static int grow_stripes(struct r5conf
*conf
, int num
)
1613 struct kmem_cache
*sc
;
1614 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
1616 if (conf
->mddev
->gendisk
)
1617 sprintf(conf
->cache_name
[0],
1618 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
1620 sprintf(conf
->cache_name
[0],
1621 "raid%d-%p", conf
->level
, conf
->mddev
);
1622 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
1624 conf
->active_name
= 0;
1625 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
1626 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
1630 conf
->slab_cache
= sc
;
1631 conf
->pool_size
= devs
;
1633 if (!grow_one_stripe(conf
))
1639 * scribble_len - return the required size of the scribble region
1640 * @num - total number of disks in the array
1642 * The size must be enough to contain:
1643 * 1/ a struct page pointer for each device in the array +2
1644 * 2/ room to convert each entry in (1) to its corresponding dma
1645 * (dma_map_page()) or page (page_address()) address.
1647 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1648 * calculate over all devices (not just the data blocks), using zeros in place
1649 * of the P and Q blocks.
1651 static size_t scribble_len(int num
)
1655 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
1660 static int resize_stripes(struct r5conf
*conf
, int newsize
)
1662 /* Make all the stripes able to hold 'newsize' devices.
1663 * New slots in each stripe get 'page' set to a new page.
1665 * This happens in stages:
1666 * 1/ create a new kmem_cache and allocate the required number of
1668 * 2/ gather all the old stripe_heads and transfer the pages across
1669 * to the new stripe_heads. This will have the side effect of
1670 * freezing the array as once all stripe_heads have been collected,
1671 * no IO will be possible. Old stripe heads are freed once their
1672 * pages have been transferred over, and the old kmem_cache is
1673 * freed when all stripes are done.
1674 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1675 * we simple return a failre status - no need to clean anything up.
1676 * 4/ allocate new pages for the new slots in the new stripe_heads.
1677 * If this fails, we don't bother trying the shrink the
1678 * stripe_heads down again, we just leave them as they are.
1679 * As each stripe_head is processed the new one is released into
1682 * Once step2 is started, we cannot afford to wait for a write,
1683 * so we use GFP_NOIO allocations.
1685 struct stripe_head
*osh
, *nsh
;
1686 LIST_HEAD(newstripes
);
1687 struct disk_info
*ndisks
;
1690 struct kmem_cache
*sc
;
1693 if (newsize
<= conf
->pool_size
)
1694 return 0; /* never bother to shrink */
1696 err
= md_allow_write(conf
->mddev
);
1701 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
1702 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
1707 for (i
= conf
->max_nr_stripes
; i
; i
--) {
1708 nsh
= kmem_cache_zalloc(sc
, GFP_KERNEL
);
1712 nsh
->raid_conf
= conf
;
1713 spin_lock_init(&nsh
->stripe_lock
);
1715 list_add(&nsh
->lru
, &newstripes
);
1718 /* didn't get enough, give up */
1719 while (!list_empty(&newstripes
)) {
1720 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1721 list_del(&nsh
->lru
);
1722 kmem_cache_free(sc
, nsh
);
1724 kmem_cache_destroy(sc
);
1727 /* Step 2 - Must use GFP_NOIO now.
1728 * OK, we have enough stripes, start collecting inactive
1729 * stripes and copying them over
1731 list_for_each_entry(nsh
, &newstripes
, lru
) {
1732 spin_lock_irq(&conf
->device_lock
);
1733 wait_event_lock_irq(conf
->wait_for_stripe
,
1734 !list_empty(&conf
->inactive_list
),
1736 osh
= get_free_stripe(conf
);
1737 spin_unlock_irq(&conf
->device_lock
);
1738 atomic_set(&nsh
->count
, 1);
1739 for(i
=0; i
<conf
->pool_size
; i
++)
1740 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1741 for( ; i
<newsize
; i
++)
1742 nsh
->dev
[i
].page
= NULL
;
1743 kmem_cache_free(conf
->slab_cache
, osh
);
1745 kmem_cache_destroy(conf
->slab_cache
);
1748 * At this point, we are holding all the stripes so the array
1749 * is completely stalled, so now is a good time to resize
1750 * conf->disks and the scribble region
1752 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1754 for (i
=0; i
<conf
->raid_disks
; i
++)
1755 ndisks
[i
] = conf
->disks
[i
];
1757 conf
->disks
= ndisks
;
1762 conf
->scribble_len
= scribble_len(newsize
);
1763 for_each_present_cpu(cpu
) {
1764 struct raid5_percpu
*percpu
;
1767 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1768 scribble
= kmalloc(conf
->scribble_len
, GFP_NOIO
);
1771 kfree(percpu
->scribble
);
1772 percpu
->scribble
= scribble
;
1780 /* Step 4, return new stripes to service */
1781 while(!list_empty(&newstripes
)) {
1782 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1783 list_del_init(&nsh
->lru
);
1785 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1786 if (nsh
->dev
[i
].page
== NULL
) {
1787 struct page
*p
= alloc_page(GFP_NOIO
);
1788 nsh
->dev
[i
].page
= p
;
1792 release_stripe(nsh
);
1794 /* critical section pass, GFP_NOIO no longer needed */
1796 conf
->slab_cache
= sc
;
1797 conf
->active_name
= 1-conf
->active_name
;
1798 conf
->pool_size
= newsize
;
1802 static int drop_one_stripe(struct r5conf
*conf
)
1804 struct stripe_head
*sh
;
1806 spin_lock_irq(&conf
->device_lock
);
1807 sh
= get_free_stripe(conf
);
1808 spin_unlock_irq(&conf
->device_lock
);
1811 BUG_ON(atomic_read(&sh
->count
));
1813 kmem_cache_free(conf
->slab_cache
, sh
);
1814 atomic_dec(&conf
->active_stripes
);
1818 static void shrink_stripes(struct r5conf
*conf
)
1820 while (drop_one_stripe(conf
))
1823 if (conf
->slab_cache
)
1824 kmem_cache_destroy(conf
->slab_cache
);
1825 conf
->slab_cache
= NULL
;
1828 static void raid5_end_read_request(struct bio
* bi
, int error
)
1830 struct stripe_head
*sh
= bi
->bi_private
;
1831 struct r5conf
*conf
= sh
->raid_conf
;
1832 int disks
= sh
->disks
, i
;
1833 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1834 char b
[BDEVNAME_SIZE
];
1835 struct md_rdev
*rdev
= NULL
;
1838 for (i
=0 ; i
<disks
; i
++)
1839 if (bi
== &sh
->dev
[i
].req
)
1842 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1843 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1849 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1850 /* If replacement finished while this request was outstanding,
1851 * 'replacement' might be NULL already.
1852 * In that case it moved down to 'rdev'.
1853 * rdev is not removed until all requests are finished.
1855 rdev
= conf
->disks
[i
].replacement
;
1857 rdev
= conf
->disks
[i
].rdev
;
1859 if (use_new_offset(conf
, sh
))
1860 s
= sh
->sector
+ rdev
->new_data_offset
;
1862 s
= sh
->sector
+ rdev
->data_offset
;
1864 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1865 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1866 /* Note that this cannot happen on a
1867 * replacement device. We just fail those on
1872 "md/raid:%s: read error corrected"
1873 " (%lu sectors at %llu on %s)\n",
1874 mdname(conf
->mddev
), STRIPE_SECTORS
,
1875 (unsigned long long)s
,
1876 bdevname(rdev
->bdev
, b
));
1877 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1878 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1879 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1880 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
1881 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1883 if (atomic_read(&rdev
->read_errors
))
1884 atomic_set(&rdev
->read_errors
, 0);
1886 const char *bdn
= bdevname(rdev
->bdev
, b
);
1890 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1891 atomic_inc(&rdev
->read_errors
);
1892 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1895 "md/raid:%s: read error on replacement device "
1896 "(sector %llu on %s).\n",
1897 mdname(conf
->mddev
),
1898 (unsigned long long)s
,
1900 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
1904 "md/raid:%s: read error not correctable "
1905 "(sector %llu on %s).\n",
1906 mdname(conf
->mddev
),
1907 (unsigned long long)s
,
1909 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
1914 "md/raid:%s: read error NOT corrected!! "
1915 "(sector %llu on %s).\n",
1916 mdname(conf
->mddev
),
1917 (unsigned long long)s
,
1919 } else if (atomic_read(&rdev
->read_errors
)
1920 > conf
->max_nr_stripes
)
1922 "md/raid:%s: Too many read errors, failing device %s.\n",
1923 mdname(conf
->mddev
), bdn
);
1927 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
1928 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1929 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1931 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1933 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1934 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1936 && test_bit(In_sync
, &rdev
->flags
)
1937 && rdev_set_badblocks(
1938 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
1939 md_error(conf
->mddev
, rdev
);
1942 rdev_dec_pending(rdev
, conf
->mddev
);
1943 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1944 set_bit(STRIPE_HANDLE
, &sh
->state
);
1948 static void raid5_end_write_request(struct bio
*bi
, int error
)
1950 struct stripe_head
*sh
= bi
->bi_private
;
1951 struct r5conf
*conf
= sh
->raid_conf
;
1952 int disks
= sh
->disks
, i
;
1953 struct md_rdev
*uninitialized_var(rdev
);
1954 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1957 int replacement
= 0;
1959 for (i
= 0 ; i
< disks
; i
++) {
1960 if (bi
== &sh
->dev
[i
].req
) {
1961 rdev
= conf
->disks
[i
].rdev
;
1964 if (bi
== &sh
->dev
[i
].rreq
) {
1965 rdev
= conf
->disks
[i
].replacement
;
1969 /* rdev was removed and 'replacement'
1970 * replaced it. rdev is not removed
1971 * until all requests are finished.
1973 rdev
= conf
->disks
[i
].rdev
;
1977 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1978 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1987 md_error(conf
->mddev
, rdev
);
1988 else if (is_badblock(rdev
, sh
->sector
,
1990 &first_bad
, &bad_sectors
))
1991 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
1994 set_bit(WriteErrorSeen
, &rdev
->flags
);
1995 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
1996 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1997 set_bit(MD_RECOVERY_NEEDED
,
1998 &rdev
->mddev
->recovery
);
1999 } else if (is_badblock(rdev
, sh
->sector
,
2001 &first_bad
, &bad_sectors
)) {
2002 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
2003 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))
2004 /* That was a successful write so make
2005 * sure it looks like we already did
2008 set_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2011 rdev_dec_pending(rdev
, conf
->mddev
);
2013 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
2014 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2015 set_bit(STRIPE_HANDLE
, &sh
->state
);
2019 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
);
2021 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
2023 struct r5dev
*dev
= &sh
->dev
[i
];
2025 bio_init(&dev
->req
);
2026 dev
->req
.bi_io_vec
= &dev
->vec
;
2028 dev
->req
.bi_max_vecs
++;
2029 dev
->req
.bi_private
= sh
;
2030 dev
->vec
.bv_page
= dev
->page
;
2032 bio_init(&dev
->rreq
);
2033 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
2034 dev
->rreq
.bi_vcnt
++;
2035 dev
->rreq
.bi_max_vecs
++;
2036 dev
->rreq
.bi_private
= sh
;
2037 dev
->rvec
.bv_page
= dev
->page
;
2040 dev
->sector
= compute_blocknr(sh
, i
, previous
);
2043 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2045 char b
[BDEVNAME_SIZE
];
2046 struct r5conf
*conf
= mddev
->private;
2047 unsigned long flags
;
2048 pr_debug("raid456: error called\n");
2050 spin_lock_irqsave(&conf
->device_lock
, flags
);
2051 clear_bit(In_sync
, &rdev
->flags
);
2052 mddev
->degraded
= calc_degraded(conf
);
2053 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2054 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2056 set_bit(Blocked
, &rdev
->flags
);
2057 set_bit(Faulty
, &rdev
->flags
);
2058 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2060 "md/raid:%s: Disk failure on %s, disabling device.\n"
2061 "md/raid:%s: Operation continuing on %d devices.\n",
2063 bdevname(rdev
->bdev
, b
),
2065 conf
->raid_disks
- mddev
->degraded
);
2069 * Input: a 'big' sector number,
2070 * Output: index of the data and parity disk, and the sector # in them.
2072 static sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
2073 int previous
, int *dd_idx
,
2074 struct stripe_head
*sh
)
2076 sector_t stripe
, stripe2
;
2077 sector_t chunk_number
;
2078 unsigned int chunk_offset
;
2081 sector_t new_sector
;
2082 int algorithm
= previous
? conf
->prev_algo
2084 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2085 : conf
->chunk_sectors
;
2086 int raid_disks
= previous
? conf
->previous_raid_disks
2088 int data_disks
= raid_disks
- conf
->max_degraded
;
2090 /* First compute the information on this sector */
2093 * Compute the chunk number and the sector offset inside the chunk
2095 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
2096 chunk_number
= r_sector
;
2099 * Compute the stripe number
2101 stripe
= chunk_number
;
2102 *dd_idx
= sector_div(stripe
, data_disks
);
2105 * Select the parity disk based on the user selected algorithm.
2107 pd_idx
= qd_idx
= -1;
2108 switch(conf
->level
) {
2110 pd_idx
= data_disks
;
2113 switch (algorithm
) {
2114 case ALGORITHM_LEFT_ASYMMETRIC
:
2115 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2116 if (*dd_idx
>= pd_idx
)
2119 case ALGORITHM_RIGHT_ASYMMETRIC
:
2120 pd_idx
= sector_div(stripe2
, raid_disks
);
2121 if (*dd_idx
>= pd_idx
)
2124 case ALGORITHM_LEFT_SYMMETRIC
:
2125 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2126 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2128 case ALGORITHM_RIGHT_SYMMETRIC
:
2129 pd_idx
= sector_div(stripe2
, raid_disks
);
2130 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2132 case ALGORITHM_PARITY_0
:
2136 case ALGORITHM_PARITY_N
:
2137 pd_idx
= data_disks
;
2145 switch (algorithm
) {
2146 case ALGORITHM_LEFT_ASYMMETRIC
:
2147 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2148 qd_idx
= pd_idx
+ 1;
2149 if (pd_idx
== raid_disks
-1) {
2150 (*dd_idx
)++; /* Q D D D P */
2152 } else if (*dd_idx
>= pd_idx
)
2153 (*dd_idx
) += 2; /* D D P Q D */
2155 case ALGORITHM_RIGHT_ASYMMETRIC
:
2156 pd_idx
= sector_div(stripe2
, raid_disks
);
2157 qd_idx
= pd_idx
+ 1;
2158 if (pd_idx
== raid_disks
-1) {
2159 (*dd_idx
)++; /* Q D D D P */
2161 } else if (*dd_idx
>= pd_idx
)
2162 (*dd_idx
) += 2; /* D D P Q D */
2164 case ALGORITHM_LEFT_SYMMETRIC
:
2165 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2166 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2167 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2169 case ALGORITHM_RIGHT_SYMMETRIC
:
2170 pd_idx
= sector_div(stripe2
, raid_disks
);
2171 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2172 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2175 case ALGORITHM_PARITY_0
:
2180 case ALGORITHM_PARITY_N
:
2181 pd_idx
= data_disks
;
2182 qd_idx
= data_disks
+ 1;
2185 case ALGORITHM_ROTATING_ZERO_RESTART
:
2186 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2187 * of blocks for computing Q is different.
2189 pd_idx
= sector_div(stripe2
, raid_disks
);
2190 qd_idx
= pd_idx
+ 1;
2191 if (pd_idx
== raid_disks
-1) {
2192 (*dd_idx
)++; /* Q D D D P */
2194 } else if (*dd_idx
>= pd_idx
)
2195 (*dd_idx
) += 2; /* D D P Q D */
2199 case ALGORITHM_ROTATING_N_RESTART
:
2200 /* Same a left_asymmetric, by first stripe is
2201 * D D D P Q rather than
2205 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2206 qd_idx
= pd_idx
+ 1;
2207 if (pd_idx
== raid_disks
-1) {
2208 (*dd_idx
)++; /* Q D D D P */
2210 } else if (*dd_idx
>= pd_idx
)
2211 (*dd_idx
) += 2; /* D D P Q D */
2215 case ALGORITHM_ROTATING_N_CONTINUE
:
2216 /* Same as left_symmetric but Q is before P */
2217 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2218 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2219 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2223 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2224 /* RAID5 left_asymmetric, with Q on last device */
2225 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2226 if (*dd_idx
>= pd_idx
)
2228 qd_idx
= raid_disks
- 1;
2231 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2232 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2233 if (*dd_idx
>= pd_idx
)
2235 qd_idx
= raid_disks
- 1;
2238 case ALGORITHM_LEFT_SYMMETRIC_6
:
2239 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2240 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2241 qd_idx
= raid_disks
- 1;
2244 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2245 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2246 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2247 qd_idx
= raid_disks
- 1;
2250 case ALGORITHM_PARITY_0_6
:
2253 qd_idx
= raid_disks
- 1;
2263 sh
->pd_idx
= pd_idx
;
2264 sh
->qd_idx
= qd_idx
;
2265 sh
->ddf_layout
= ddf_layout
;
2268 * Finally, compute the new sector number
2270 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2275 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2277 struct r5conf
*conf
= sh
->raid_conf
;
2278 int raid_disks
= sh
->disks
;
2279 int data_disks
= raid_disks
- conf
->max_degraded
;
2280 sector_t new_sector
= sh
->sector
, check
;
2281 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2282 : conf
->chunk_sectors
;
2283 int algorithm
= previous
? conf
->prev_algo
2287 sector_t chunk_number
;
2288 int dummy1
, dd_idx
= i
;
2290 struct stripe_head sh2
;
2293 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2294 stripe
= new_sector
;
2296 if (i
== sh
->pd_idx
)
2298 switch(conf
->level
) {
2301 switch (algorithm
) {
2302 case ALGORITHM_LEFT_ASYMMETRIC
:
2303 case ALGORITHM_RIGHT_ASYMMETRIC
:
2307 case ALGORITHM_LEFT_SYMMETRIC
:
2308 case ALGORITHM_RIGHT_SYMMETRIC
:
2311 i
-= (sh
->pd_idx
+ 1);
2313 case ALGORITHM_PARITY_0
:
2316 case ALGORITHM_PARITY_N
:
2323 if (i
== sh
->qd_idx
)
2324 return 0; /* It is the Q disk */
2325 switch (algorithm
) {
2326 case ALGORITHM_LEFT_ASYMMETRIC
:
2327 case ALGORITHM_RIGHT_ASYMMETRIC
:
2328 case ALGORITHM_ROTATING_ZERO_RESTART
:
2329 case ALGORITHM_ROTATING_N_RESTART
:
2330 if (sh
->pd_idx
== raid_disks
-1)
2331 i
--; /* Q D D D P */
2332 else if (i
> sh
->pd_idx
)
2333 i
-= 2; /* D D P Q D */
2335 case ALGORITHM_LEFT_SYMMETRIC
:
2336 case ALGORITHM_RIGHT_SYMMETRIC
:
2337 if (sh
->pd_idx
== raid_disks
-1)
2338 i
--; /* Q D D D P */
2343 i
-= (sh
->pd_idx
+ 2);
2346 case ALGORITHM_PARITY_0
:
2349 case ALGORITHM_PARITY_N
:
2351 case ALGORITHM_ROTATING_N_CONTINUE
:
2352 /* Like left_symmetric, but P is before Q */
2353 if (sh
->pd_idx
== 0)
2354 i
--; /* P D D D Q */
2359 i
-= (sh
->pd_idx
+ 1);
2362 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2363 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2367 case ALGORITHM_LEFT_SYMMETRIC_6
:
2368 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2370 i
+= data_disks
+ 1;
2371 i
-= (sh
->pd_idx
+ 1);
2373 case ALGORITHM_PARITY_0_6
:
2382 chunk_number
= stripe
* data_disks
+ i
;
2383 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2385 check
= raid5_compute_sector(conf
, r_sector
,
2386 previous
, &dummy1
, &sh2
);
2387 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2388 || sh2
.qd_idx
!= sh
->qd_idx
) {
2389 printk(KERN_ERR
"md/raid:%s: compute_blocknr: map not correct\n",
2390 mdname(conf
->mddev
));
2398 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2399 int rcw
, int expand
)
2401 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
2402 struct r5conf
*conf
= sh
->raid_conf
;
2403 int level
= conf
->level
;
2407 for (i
= disks
; i
--; ) {
2408 struct r5dev
*dev
= &sh
->dev
[i
];
2411 set_bit(R5_LOCKED
, &dev
->flags
);
2412 set_bit(R5_Wantdrain
, &dev
->flags
);
2414 clear_bit(R5_UPTODATE
, &dev
->flags
);
2418 /* if we are not expanding this is a proper write request, and
2419 * there will be bios with new data to be drained into the
2424 /* False alarm, nothing to do */
2426 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2427 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2429 sh
->reconstruct_state
= reconstruct_state_run
;
2431 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2433 if (s
->locked
+ conf
->max_degraded
== disks
)
2434 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2435 atomic_inc(&conf
->pending_full_writes
);
2438 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2439 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2441 for (i
= disks
; i
--; ) {
2442 struct r5dev
*dev
= &sh
->dev
[i
];
2447 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2448 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2449 set_bit(R5_Wantdrain
, &dev
->flags
);
2450 set_bit(R5_LOCKED
, &dev
->flags
);
2451 clear_bit(R5_UPTODATE
, &dev
->flags
);
2456 /* False alarm - nothing to do */
2458 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2459 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2460 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2461 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2464 /* keep the parity disk(s) locked while asynchronous operations
2467 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2468 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2472 int qd_idx
= sh
->qd_idx
;
2473 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2475 set_bit(R5_LOCKED
, &dev
->flags
);
2476 clear_bit(R5_UPTODATE
, &dev
->flags
);
2480 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2481 __func__
, (unsigned long long)sh
->sector
,
2482 s
->locked
, s
->ops_request
);
2486 * Each stripe/dev can have one or more bion attached.
2487 * toread/towrite point to the first in a chain.
2488 * The bi_next chain must be in order.
2490 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
2493 struct r5conf
*conf
= sh
->raid_conf
;
2496 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2497 (unsigned long long)bi
->bi_sector
,
2498 (unsigned long long)sh
->sector
);
2501 * If several bio share a stripe. The bio bi_phys_segments acts as a
2502 * reference count to avoid race. The reference count should already be
2503 * increased before this function is called (for example, in
2504 * make_request()), so other bio sharing this stripe will not free the
2505 * stripe. If a stripe is owned by one stripe, the stripe lock will
2508 spin_lock_irq(&sh
->stripe_lock
);
2510 bip
= &sh
->dev
[dd_idx
].towrite
;
2514 bip
= &sh
->dev
[dd_idx
].toread
;
2515 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
2516 if (bio_end_sector(*bip
) > bi
->bi_sector
)
2518 bip
= & (*bip
)->bi_next
;
2520 if (*bip
&& (*bip
)->bi_sector
< bio_end_sector(bi
))
2523 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
2527 raid5_inc_bi_active_stripes(bi
);
2530 /* check if page is covered */
2531 sector_t sector
= sh
->dev
[dd_idx
].sector
;
2532 for (bi
=sh
->dev
[dd_idx
].towrite
;
2533 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
2534 bi
&& bi
->bi_sector
<= sector
;
2535 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
2536 if (bio_end_sector(bi
) >= sector
)
2537 sector
= bio_end_sector(bi
);
2539 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
2540 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
2543 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2544 (unsigned long long)(*bip
)->bi_sector
,
2545 (unsigned long long)sh
->sector
, dd_idx
);
2546 spin_unlock_irq(&sh
->stripe_lock
);
2548 if (conf
->mddev
->bitmap
&& firstwrite
) {
2549 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
2551 sh
->bm_seq
= conf
->seq_flush
+1;
2552 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
2557 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
2558 spin_unlock_irq(&sh
->stripe_lock
);
2562 static void end_reshape(struct r5conf
*conf
);
2564 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
2565 struct stripe_head
*sh
)
2567 int sectors_per_chunk
=
2568 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
2570 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
2571 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
2573 raid5_compute_sector(conf
,
2574 stripe
* (disks
- conf
->max_degraded
)
2575 *sectors_per_chunk
+ chunk_offset
,
2581 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
2582 struct stripe_head_state
*s
, int disks
,
2583 struct bio
**return_bi
)
2586 for (i
= disks
; i
--; ) {
2590 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2591 struct md_rdev
*rdev
;
2593 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2594 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2595 atomic_inc(&rdev
->nr_pending
);
2600 if (!rdev_set_badblocks(
2604 md_error(conf
->mddev
, rdev
);
2605 rdev_dec_pending(rdev
, conf
->mddev
);
2608 spin_lock_irq(&sh
->stripe_lock
);
2609 /* fail all writes first */
2610 bi
= sh
->dev
[i
].towrite
;
2611 sh
->dev
[i
].towrite
= NULL
;
2612 spin_unlock_irq(&sh
->stripe_lock
);
2616 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2617 wake_up(&conf
->wait_for_overlap
);
2619 while (bi
&& bi
->bi_sector
<
2620 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2621 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2622 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2623 if (!raid5_dec_bi_active_stripes(bi
)) {
2624 md_write_end(conf
->mddev
);
2625 bi
->bi_next
= *return_bi
;
2631 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2632 STRIPE_SECTORS
, 0, 0);
2634 /* and fail all 'written' */
2635 bi
= sh
->dev
[i
].written
;
2636 sh
->dev
[i
].written
= NULL
;
2637 if (bi
) bitmap_end
= 1;
2638 while (bi
&& bi
->bi_sector
<
2639 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2640 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2641 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2642 if (!raid5_dec_bi_active_stripes(bi
)) {
2643 md_write_end(conf
->mddev
);
2644 bi
->bi_next
= *return_bi
;
2650 /* fail any reads if this device is non-operational and
2651 * the data has not reached the cache yet.
2653 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2654 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2655 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2656 spin_lock_irq(&sh
->stripe_lock
);
2657 bi
= sh
->dev
[i
].toread
;
2658 sh
->dev
[i
].toread
= NULL
;
2659 spin_unlock_irq(&sh
->stripe_lock
);
2660 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2661 wake_up(&conf
->wait_for_overlap
);
2662 while (bi
&& bi
->bi_sector
<
2663 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2664 struct bio
*nextbi
=
2665 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2666 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2667 if (!raid5_dec_bi_active_stripes(bi
)) {
2668 bi
->bi_next
= *return_bi
;
2675 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2676 STRIPE_SECTORS
, 0, 0);
2677 /* If we were in the middle of a write the parity block might
2678 * still be locked - so just clear all R5_LOCKED flags
2680 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
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
);
2689 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
2690 struct stripe_head_state
*s
)
2695 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2696 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
2697 wake_up(&conf
->wait_for_overlap
);
2700 /* There is nothing more to do for sync/check/repair.
2701 * Don't even need to abort as that is handled elsewhere
2702 * if needed, and not always wanted e.g. if there is a known
2704 * For recover/replace we need to record a bad block on all
2705 * non-sync devices, or abort the recovery
2707 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
2708 /* During recovery devices cannot be removed, so
2709 * locking and refcounting of rdevs is not needed
2711 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2712 struct md_rdev
*rdev
= conf
->disks
[i
].rdev
;
2714 && !test_bit(Faulty
, &rdev
->flags
)
2715 && !test_bit(In_sync
, &rdev
->flags
)
2716 && !rdev_set_badblocks(rdev
, sh
->sector
,
2719 rdev
= conf
->disks
[i
].replacement
;
2721 && !test_bit(Faulty
, &rdev
->flags
)
2722 && !test_bit(In_sync
, &rdev
->flags
)
2723 && !rdev_set_badblocks(rdev
, sh
->sector
,
2728 conf
->recovery_disabled
=
2729 conf
->mddev
->recovery_disabled
;
2731 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
2734 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
2736 struct md_rdev
*rdev
;
2738 /* Doing recovery so rcu locking not required */
2739 rdev
= sh
->raid_conf
->disks
[disk_idx
].replacement
;
2741 && !test_bit(Faulty
, &rdev
->flags
)
2742 && !test_bit(In_sync
, &rdev
->flags
)
2743 && (rdev
->recovery_offset
<= sh
->sector
2744 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
2750 /* fetch_block - checks the given member device to see if its data needs
2751 * to be read or computed to satisfy a request.
2753 * Returns 1 when no more member devices need to be checked, otherwise returns
2754 * 0 to tell the loop in handle_stripe_fill to continue
2756 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2757 int disk_idx
, int disks
)
2759 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2760 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
2761 &sh
->dev
[s
->failed_num
[1]] };
2763 /* is the data in this block needed, and can we get it? */
2764 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2765 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2767 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2768 s
->syncing
|| s
->expanding
||
2769 (s
->replacing
&& want_replace(sh
, disk_idx
)) ||
2770 (s
->failed
>= 1 && fdev
[0]->toread
) ||
2771 (s
->failed
>= 2 && fdev
[1]->toread
) ||
2772 (sh
->raid_conf
->level
<= 5 && s
->failed
&& fdev
[0]->towrite
&&
2773 !test_bit(R5_OVERWRITE
, &fdev
[0]->flags
)) ||
2774 (sh
->raid_conf
->level
== 6 && s
->failed
&& s
->to_write
))) {
2775 /* we would like to get this block, possibly by computing it,
2776 * otherwise read it if the backing disk is insync
2778 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
2779 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
2780 if ((s
->uptodate
== disks
- 1) &&
2781 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
2782 disk_idx
== s
->failed_num
[1]))) {
2783 /* have disk failed, and we're requested to fetch it;
2786 pr_debug("Computing stripe %llu block %d\n",
2787 (unsigned long long)sh
->sector
, disk_idx
);
2788 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2789 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2790 set_bit(R5_Wantcompute
, &dev
->flags
);
2791 sh
->ops
.target
= disk_idx
;
2792 sh
->ops
.target2
= -1; /* no 2nd target */
2794 /* Careful: from this point on 'uptodate' is in the eye
2795 * of raid_run_ops which services 'compute' operations
2796 * before writes. R5_Wantcompute flags a block that will
2797 * be R5_UPTODATE by the time it is needed for a
2798 * subsequent operation.
2802 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
2803 /* Computing 2-failure is *very* expensive; only
2804 * do it if failed >= 2
2807 for (other
= disks
; other
--; ) {
2808 if (other
== disk_idx
)
2810 if (!test_bit(R5_UPTODATE
,
2811 &sh
->dev
[other
].flags
))
2815 pr_debug("Computing stripe %llu blocks %d,%d\n",
2816 (unsigned long long)sh
->sector
,
2818 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2819 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2820 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
2821 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
2822 sh
->ops
.target
= disk_idx
;
2823 sh
->ops
.target2
= other
;
2827 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2828 set_bit(R5_LOCKED
, &dev
->flags
);
2829 set_bit(R5_Wantread
, &dev
->flags
);
2831 pr_debug("Reading block %d (sync=%d)\n",
2832 disk_idx
, s
->syncing
);
2840 * handle_stripe_fill - read or compute data to satisfy pending requests.
2842 static void handle_stripe_fill(struct stripe_head
*sh
,
2843 struct stripe_head_state
*s
,
2848 /* look for blocks to read/compute, skip this if a compute
2849 * is already in flight, or if the stripe contents are in the
2850 * midst of changing due to a write
2852 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2853 !sh
->reconstruct_state
)
2854 for (i
= disks
; i
--; )
2855 if (fetch_block(sh
, s
, i
, disks
))
2857 set_bit(STRIPE_HANDLE
, &sh
->state
);
2861 /* handle_stripe_clean_event
2862 * any written block on an uptodate or failed drive can be returned.
2863 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2864 * never LOCKED, so we don't need to test 'failed' directly.
2866 static void handle_stripe_clean_event(struct r5conf
*conf
,
2867 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2871 int discard_pending
= 0;
2873 for (i
= disks
; i
--; )
2874 if (sh
->dev
[i
].written
) {
2876 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2877 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2878 test_bit(R5_Discard
, &dev
->flags
))) {
2879 /* We can return any write requests */
2880 struct bio
*wbi
, *wbi2
;
2881 pr_debug("Return write for disc %d\n", i
);
2882 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
2883 clear_bit(R5_UPTODATE
, &dev
->flags
);
2885 dev
->written
= NULL
;
2886 while (wbi
&& wbi
->bi_sector
<
2887 dev
->sector
+ STRIPE_SECTORS
) {
2888 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2889 if (!raid5_dec_bi_active_stripes(wbi
)) {
2890 md_write_end(conf
->mddev
);
2891 wbi
->bi_next
= *return_bi
;
2896 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2898 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
2900 } else if (test_bit(R5_Discard
, &dev
->flags
))
2901 discard_pending
= 1;
2903 if (!discard_pending
&&
2904 test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
)) {
2905 clear_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
2906 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
2907 if (sh
->qd_idx
>= 0) {
2908 clear_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
2909 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
);
2911 /* now that discard is done we can proceed with any sync */
2912 clear_bit(STRIPE_DISCARD
, &sh
->state
);
2913 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
))
2914 set_bit(STRIPE_HANDLE
, &sh
->state
);
2918 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2919 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2920 md_wakeup_thread(conf
->mddev
->thread
);
2923 static void handle_stripe_dirtying(struct r5conf
*conf
,
2924 struct stripe_head
*sh
,
2925 struct stripe_head_state
*s
,
2928 int rmw
= 0, rcw
= 0, i
;
2929 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
2931 /* RAID6 requires 'rcw' in current implementation.
2932 * Otherwise, check whether resync is now happening or should start.
2933 * If yes, then the array is dirty (after unclean shutdown or
2934 * initial creation), so parity in some stripes might be inconsistent.
2935 * In this case, we need to always do reconstruct-write, to ensure
2936 * that in case of drive failure or read-error correction, we
2937 * generate correct data from the parity.
2939 if (conf
->max_degraded
== 2 ||
2940 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
)) {
2941 /* Calculate the real rcw later - for now make it
2942 * look like rcw is cheaper
2945 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2946 conf
->max_degraded
, (unsigned long long)recovery_cp
,
2947 (unsigned long long)sh
->sector
);
2948 } else for (i
= disks
; i
--; ) {
2949 /* would I have to read this buffer for read_modify_write */
2950 struct r5dev
*dev
= &sh
->dev
[i
];
2951 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2952 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2953 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2954 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2955 if (test_bit(R5_Insync
, &dev
->flags
))
2958 rmw
+= 2*disks
; /* cannot read it */
2960 /* Would I have to read this buffer for reconstruct_write */
2961 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
2962 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2963 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2964 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2965 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2970 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2971 (unsigned long long)sh
->sector
, rmw
, rcw
);
2972 set_bit(STRIPE_HANDLE
, &sh
->state
);
2973 if (rmw
< rcw
&& rmw
> 0) {
2974 /* prefer read-modify-write, but need to get some data */
2975 if (conf
->mddev
->queue
)
2976 blk_add_trace_msg(conf
->mddev
->queue
,
2977 "raid5 rmw %llu %d",
2978 (unsigned long long)sh
->sector
, rmw
);
2979 for (i
= disks
; i
--; ) {
2980 struct r5dev
*dev
= &sh
->dev
[i
];
2981 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
2982 !test_bit(R5_LOCKED
, &dev
->flags
) &&
2983 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
2984 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
2985 test_bit(R5_Insync
, &dev
->flags
)) {
2987 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2988 pr_debug("Read_old block "
2989 "%d for r-m-w\n", i
);
2990 set_bit(R5_LOCKED
, &dev
->flags
);
2991 set_bit(R5_Wantread
, &dev
->flags
);
2994 set_bit(STRIPE_DELAYED
, &sh
->state
);
2995 set_bit(STRIPE_HANDLE
, &sh
->state
);
3000 if (rcw
<= rmw
&& rcw
> 0) {
3001 /* want reconstruct write, but need to get some data */
3004 for (i
= disks
; i
--; ) {
3005 struct r5dev
*dev
= &sh
->dev
[i
];
3006 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3007 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3008 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3009 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3010 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3012 if (!test_bit(R5_Insync
, &dev
->flags
))
3013 continue; /* it's a failed drive */
3015 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
3016 pr_debug("Read_old block "
3017 "%d for Reconstruct\n", i
);
3018 set_bit(R5_LOCKED
, &dev
->flags
);
3019 set_bit(R5_Wantread
, &dev
->flags
);
3023 set_bit(STRIPE_DELAYED
, &sh
->state
);
3024 set_bit(STRIPE_HANDLE
, &sh
->state
);
3028 if (rcw
&& conf
->mddev
->queue
)
3029 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
3030 (unsigned long long)sh
->sector
,
3031 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
3033 /* now if nothing is locked, and if we have enough data,
3034 * we can start a write request
3036 /* since handle_stripe can be called at any time we need to handle the
3037 * case where a compute block operation has been submitted and then a
3038 * subsequent call wants to start a write request. raid_run_ops only
3039 * handles the case where compute block and reconstruct are requested
3040 * simultaneously. If this is not the case then new writes need to be
3041 * held off until the compute completes.
3043 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
3044 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
3045 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
3046 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
3049 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
3050 struct stripe_head_state
*s
, int disks
)
3052 struct r5dev
*dev
= NULL
;
3054 set_bit(STRIPE_HANDLE
, &sh
->state
);
3056 switch (sh
->check_state
) {
3057 case check_state_idle
:
3058 /* start a new check operation if there are no failures */
3059 if (s
->failed
== 0) {
3060 BUG_ON(s
->uptodate
!= disks
);
3061 sh
->check_state
= check_state_run
;
3062 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3063 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3067 dev
= &sh
->dev
[s
->failed_num
[0]];
3069 case check_state_compute_result
:
3070 sh
->check_state
= check_state_idle
;
3072 dev
= &sh
->dev
[sh
->pd_idx
];
3074 /* check that a write has not made the stripe insync */
3075 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3078 /* either failed parity check, or recovery is happening */
3079 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
3080 BUG_ON(s
->uptodate
!= disks
);
3082 set_bit(R5_LOCKED
, &dev
->flags
);
3084 set_bit(R5_Wantwrite
, &dev
->flags
);
3086 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3087 set_bit(STRIPE_INSYNC
, &sh
->state
);
3089 case check_state_run
:
3090 break; /* we will be called again upon completion */
3091 case check_state_check_result
:
3092 sh
->check_state
= check_state_idle
;
3094 /* if a failure occurred during the check operation, leave
3095 * STRIPE_INSYNC not set and let the stripe be handled again
3100 /* handle a successful check operation, if parity is correct
3101 * we are done. Otherwise update the mismatch count and repair
3102 * parity if !MD_RECOVERY_CHECK
3104 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
3105 /* parity is correct (on disc,
3106 * not in buffer any more)
3108 set_bit(STRIPE_INSYNC
, &sh
->state
);
3110 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3111 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3112 /* don't try to repair!! */
3113 set_bit(STRIPE_INSYNC
, &sh
->state
);
3115 sh
->check_state
= check_state_compute_run
;
3116 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3117 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3118 set_bit(R5_Wantcompute
,
3119 &sh
->dev
[sh
->pd_idx
].flags
);
3120 sh
->ops
.target
= sh
->pd_idx
;
3121 sh
->ops
.target2
= -1;
3126 case check_state_compute_run
:
3129 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3130 __func__
, sh
->check_state
,
3131 (unsigned long long) sh
->sector
);
3137 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
3138 struct stripe_head_state
*s
,
3141 int pd_idx
= sh
->pd_idx
;
3142 int qd_idx
= sh
->qd_idx
;
3145 set_bit(STRIPE_HANDLE
, &sh
->state
);
3147 BUG_ON(s
->failed
> 2);
3149 /* Want to check and possibly repair P and Q.
3150 * However there could be one 'failed' device, in which
3151 * case we can only check one of them, possibly using the
3152 * other to generate missing data
3155 switch (sh
->check_state
) {
3156 case check_state_idle
:
3157 /* start a new check operation if there are < 2 failures */
3158 if (s
->failed
== s
->q_failed
) {
3159 /* The only possible failed device holds Q, so it
3160 * makes sense to check P (If anything else were failed,
3161 * we would have used P to recreate it).
3163 sh
->check_state
= check_state_run
;
3165 if (!s
->q_failed
&& s
->failed
< 2) {
3166 /* Q is not failed, and we didn't use it to generate
3167 * anything, so it makes sense to check it
3169 if (sh
->check_state
== check_state_run
)
3170 sh
->check_state
= check_state_run_pq
;
3172 sh
->check_state
= check_state_run_q
;
3175 /* discard potentially stale zero_sum_result */
3176 sh
->ops
.zero_sum_result
= 0;
3178 if (sh
->check_state
== check_state_run
) {
3179 /* async_xor_zero_sum destroys the contents of P */
3180 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3183 if (sh
->check_state
>= check_state_run
&&
3184 sh
->check_state
<= check_state_run_pq
) {
3185 /* async_syndrome_zero_sum preserves P and Q, so
3186 * no need to mark them !uptodate here
3188 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3192 /* we have 2-disk failure */
3193 BUG_ON(s
->failed
!= 2);
3195 case check_state_compute_result
:
3196 sh
->check_state
= check_state_idle
;
3198 /* check that a write has not made the stripe insync */
3199 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3202 /* now write out any block on a failed drive,
3203 * or P or Q if they were recomputed
3205 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
3206 if (s
->failed
== 2) {
3207 dev
= &sh
->dev
[s
->failed_num
[1]];
3209 set_bit(R5_LOCKED
, &dev
->flags
);
3210 set_bit(R5_Wantwrite
, &dev
->flags
);
3212 if (s
->failed
>= 1) {
3213 dev
= &sh
->dev
[s
->failed_num
[0]];
3215 set_bit(R5_LOCKED
, &dev
->flags
);
3216 set_bit(R5_Wantwrite
, &dev
->flags
);
3218 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3219 dev
= &sh
->dev
[pd_idx
];
3221 set_bit(R5_LOCKED
, &dev
->flags
);
3222 set_bit(R5_Wantwrite
, &dev
->flags
);
3224 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3225 dev
= &sh
->dev
[qd_idx
];
3227 set_bit(R5_LOCKED
, &dev
->flags
);
3228 set_bit(R5_Wantwrite
, &dev
->flags
);
3230 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3232 set_bit(STRIPE_INSYNC
, &sh
->state
);
3234 case check_state_run
:
3235 case check_state_run_q
:
3236 case check_state_run_pq
:
3237 break; /* we will be called again upon completion */
3238 case check_state_check_result
:
3239 sh
->check_state
= check_state_idle
;
3241 /* handle a successful check operation, if parity is correct
3242 * we are done. Otherwise update the mismatch count and repair
3243 * parity if !MD_RECOVERY_CHECK
3245 if (sh
->ops
.zero_sum_result
== 0) {
3246 /* both parities are correct */
3248 set_bit(STRIPE_INSYNC
, &sh
->state
);
3250 /* in contrast to the raid5 case we can validate
3251 * parity, but still have a failure to write
3254 sh
->check_state
= check_state_compute_result
;
3255 /* Returning at this point means that we may go
3256 * off and bring p and/or q uptodate again so
3257 * we make sure to check zero_sum_result again
3258 * to verify if p or q need writeback
3262 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3263 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3264 /* don't try to repair!! */
3265 set_bit(STRIPE_INSYNC
, &sh
->state
);
3267 int *target
= &sh
->ops
.target
;
3269 sh
->ops
.target
= -1;
3270 sh
->ops
.target2
= -1;
3271 sh
->check_state
= check_state_compute_run
;
3272 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3273 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3274 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3275 set_bit(R5_Wantcompute
,
3276 &sh
->dev
[pd_idx
].flags
);
3278 target
= &sh
->ops
.target2
;
3281 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3282 set_bit(R5_Wantcompute
,
3283 &sh
->dev
[qd_idx
].flags
);
3290 case check_state_compute_run
:
3293 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3294 __func__
, sh
->check_state
,
3295 (unsigned long long) sh
->sector
);
3300 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3304 /* We have read all the blocks in this stripe and now we need to
3305 * copy some of them into a target stripe for expand.
3307 struct dma_async_tx_descriptor
*tx
= NULL
;
3308 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3309 for (i
= 0; i
< sh
->disks
; i
++)
3310 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3312 struct stripe_head
*sh2
;
3313 struct async_submit_ctl submit
;
3315 sector_t bn
= compute_blocknr(sh
, i
, 1);
3316 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3318 sh2
= get_active_stripe(conf
, s
, 0, 1, 1);
3320 /* so far only the early blocks of this stripe
3321 * have been requested. When later blocks
3322 * get requested, we will try again
3325 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3326 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3327 /* must have already done this block */
3328 release_stripe(sh2
);
3332 /* place all the copies on one channel */
3333 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
3334 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
3335 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
3338 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
3339 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
3340 for (j
= 0; j
< conf
->raid_disks
; j
++)
3341 if (j
!= sh2
->pd_idx
&&
3343 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
3345 if (j
== conf
->raid_disks
) {
3346 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
3347 set_bit(STRIPE_HANDLE
, &sh2
->state
);
3349 release_stripe(sh2
);
3352 /* done submitting copies, wait for them to complete */
3353 async_tx_quiesce(&tx
);
3357 * handle_stripe - do things to a stripe.
3359 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3360 * state of various bits to see what needs to be done.
3362 * return some read requests which now have data
3363 * return some write requests which are safely on storage
3364 * schedule a read on some buffers
3365 * schedule a write of some buffers
3366 * return confirmation of parity correctness
3370 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
3372 struct r5conf
*conf
= sh
->raid_conf
;
3373 int disks
= sh
->disks
;
3376 int do_recovery
= 0;
3378 memset(s
, 0, sizeof(*s
));
3380 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3381 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3382 s
->failed_num
[0] = -1;
3383 s
->failed_num
[1] = -1;
3385 /* Now to look around and see what can be done */
3387 for (i
=disks
; i
--; ) {
3388 struct md_rdev
*rdev
;
3395 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3397 dev
->toread
, dev
->towrite
, dev
->written
);
3398 /* maybe we can reply to a read
3400 * new wantfill requests are only permitted while
3401 * ops_complete_biofill is guaranteed to be inactive
3403 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
3404 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
3405 set_bit(R5_Wantfill
, &dev
->flags
);
3407 /* now count some things */
3408 if (test_bit(R5_LOCKED
, &dev
->flags
))
3410 if (test_bit(R5_UPTODATE
, &dev
->flags
))
3412 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
3414 BUG_ON(s
->compute
> 2);
3417 if (test_bit(R5_Wantfill
, &dev
->flags
))
3419 else if (dev
->toread
)
3423 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3428 /* Prefer to use the replacement for reads, but only
3429 * if it is recovered enough and has no bad blocks.
3431 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3432 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
3433 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
3434 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3435 &first_bad
, &bad_sectors
))
3436 set_bit(R5_ReadRepl
, &dev
->flags
);
3439 set_bit(R5_NeedReplace
, &dev
->flags
);
3440 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3441 clear_bit(R5_ReadRepl
, &dev
->flags
);
3443 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
3446 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3447 &first_bad
, &bad_sectors
);
3448 if (s
->blocked_rdev
== NULL
3449 && (test_bit(Blocked
, &rdev
->flags
)
3452 set_bit(BlockedBadBlocks
,
3454 s
->blocked_rdev
= rdev
;
3455 atomic_inc(&rdev
->nr_pending
);
3458 clear_bit(R5_Insync
, &dev
->flags
);
3462 /* also not in-sync */
3463 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
3464 test_bit(R5_UPTODATE
, &dev
->flags
)) {
3465 /* treat as in-sync, but with a read error
3466 * which we can now try to correct
3468 set_bit(R5_Insync
, &dev
->flags
);
3469 set_bit(R5_ReadError
, &dev
->flags
);
3471 } else if (test_bit(In_sync
, &rdev
->flags
))
3472 set_bit(R5_Insync
, &dev
->flags
);
3473 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
3474 /* in sync if before recovery_offset */
3475 set_bit(R5_Insync
, &dev
->flags
);
3476 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
3477 test_bit(R5_Expanded
, &dev
->flags
))
3478 /* If we've reshaped into here, we assume it is Insync.
3479 * We will shortly update recovery_offset to make
3482 set_bit(R5_Insync
, &dev
->flags
);
3484 if (rdev
&& test_bit(R5_WriteError
, &dev
->flags
)) {
3485 /* This flag does not apply to '.replacement'
3486 * only to .rdev, so make sure to check that*/
3487 struct md_rdev
*rdev2
= rcu_dereference(
3488 conf
->disks
[i
].rdev
);
3490 clear_bit(R5_Insync
, &dev
->flags
);
3491 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3492 s
->handle_bad_blocks
= 1;
3493 atomic_inc(&rdev2
->nr_pending
);
3495 clear_bit(R5_WriteError
, &dev
->flags
);
3497 if (rdev
&& test_bit(R5_MadeGood
, &dev
->flags
)) {
3498 /* This flag does not apply to '.replacement'
3499 * only to .rdev, so make sure to check that*/
3500 struct md_rdev
*rdev2
= rcu_dereference(
3501 conf
->disks
[i
].rdev
);
3502 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3503 s
->handle_bad_blocks
= 1;
3504 atomic_inc(&rdev2
->nr_pending
);
3506 clear_bit(R5_MadeGood
, &dev
->flags
);
3508 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3509 struct md_rdev
*rdev2
= rcu_dereference(
3510 conf
->disks
[i
].replacement
);
3511 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3512 s
->handle_bad_blocks
= 1;
3513 atomic_inc(&rdev2
->nr_pending
);
3515 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
3517 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3518 /* The ReadError flag will just be confusing now */
3519 clear_bit(R5_ReadError
, &dev
->flags
);
3520 clear_bit(R5_ReWrite
, &dev
->flags
);
3522 if (test_bit(R5_ReadError
, &dev
->flags
))
3523 clear_bit(R5_Insync
, &dev
->flags
);
3524 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3526 s
->failed_num
[s
->failed
] = i
;
3528 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
3532 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
3533 /* If there is a failed device being replaced,
3534 * we must be recovering.
3535 * else if we are after recovery_cp, we must be syncing
3536 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3537 * else we can only be replacing
3538 * sync and recovery both need to read all devices, and so
3539 * use the same flag.
3542 sh
->sector
>= conf
->mddev
->recovery_cp
||
3543 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
3551 static void handle_stripe(struct stripe_head
*sh
)
3553 struct stripe_head_state s
;
3554 struct r5conf
*conf
= sh
->raid_conf
;
3557 int disks
= sh
->disks
;
3558 struct r5dev
*pdev
, *qdev
;
3560 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3561 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
3562 /* already being handled, ensure it gets handled
3563 * again when current action finishes */
3564 set_bit(STRIPE_HANDLE
, &sh
->state
);
3568 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3569 spin_lock(&sh
->stripe_lock
);
3570 /* Cannot process 'sync' concurrently with 'discard' */
3571 if (!test_bit(STRIPE_DISCARD
, &sh
->state
) &&
3572 test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3573 set_bit(STRIPE_SYNCING
, &sh
->state
);
3574 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3575 clear_bit(STRIPE_REPLACED
, &sh
->state
);
3577 spin_unlock(&sh
->stripe_lock
);
3579 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3581 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3582 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3583 (unsigned long long)sh
->sector
, sh
->state
,
3584 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
3585 sh
->check_state
, sh
->reconstruct_state
);
3587 analyse_stripe(sh
, &s
);
3589 if (s
.handle_bad_blocks
) {
3590 set_bit(STRIPE_HANDLE
, &sh
->state
);
3594 if (unlikely(s
.blocked_rdev
)) {
3595 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3596 s
.replacing
|| s
.to_write
|| s
.written
) {
3597 set_bit(STRIPE_HANDLE
, &sh
->state
);
3600 /* There is nothing for the blocked_rdev to block */
3601 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
3602 s
.blocked_rdev
= NULL
;
3605 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
3606 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
3607 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
3610 pr_debug("locked=%d uptodate=%d to_read=%d"
3611 " to_write=%d failed=%d failed_num=%d,%d\n",
3612 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3613 s
.failed_num
[0], s
.failed_num
[1]);
3614 /* check if the array has lost more than max_degraded devices and,
3615 * if so, some requests might need to be failed.
3617 if (s
.failed
> conf
->max_degraded
) {
3618 sh
->check_state
= 0;
3619 sh
->reconstruct_state
= 0;
3620 if (s
.to_read
+s
.to_write
+s
.written
)
3621 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
3622 if (s
.syncing
+ s
.replacing
)
3623 handle_failed_sync(conf
, sh
, &s
);
3626 /* Now we check to see if any write operations have recently
3630 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
3632 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
3633 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
3634 sh
->reconstruct_state
= reconstruct_state_idle
;
3636 /* All the 'written' buffers and the parity block are ready to
3637 * be written back to disk
3639 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
3640 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
3641 BUG_ON(sh
->qd_idx
>= 0 &&
3642 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
3643 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
3644 for (i
= disks
; i
--; ) {
3645 struct r5dev
*dev
= &sh
->dev
[i
];
3646 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
3647 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3649 pr_debug("Writing block %d\n", i
);
3650 set_bit(R5_Wantwrite
, &dev
->flags
);
3653 if (!test_bit(R5_Insync
, &dev
->flags
) ||
3654 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
3656 set_bit(STRIPE_INSYNC
, &sh
->state
);
3659 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3660 s
.dec_preread_active
= 1;
3664 * might be able to return some write requests if the parity blocks
3665 * are safe, or on a failed drive
3667 pdev
= &sh
->dev
[sh
->pd_idx
];
3668 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
3669 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
3670 qdev
= &sh
->dev
[sh
->qd_idx
];
3671 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
3672 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
3676 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3677 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3678 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
3679 test_bit(R5_Discard
, &pdev
->flags
))))) &&
3680 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3681 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3682 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
3683 test_bit(R5_Discard
, &qdev
->flags
))))))
3684 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
3686 /* Now we might consider reading some blocks, either to check/generate
3687 * parity, or to satisfy requests
3688 * or to load a block that is being partially written.
3690 if (s
.to_read
|| s
.non_overwrite
3691 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
3692 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
3695 handle_stripe_fill(sh
, &s
, disks
);
3697 /* Now to consider new write requests and what else, if anything
3698 * should be read. We do not handle new writes when:
3699 * 1/ A 'write' operation (copy+xor) is already in flight.
3700 * 2/ A 'check' operation is in flight, as it may clobber the parity
3703 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
3704 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
3706 /* maybe we need to check and possibly fix the parity for this stripe
3707 * Any reads will already have been scheduled, so we just see if enough
3708 * data is available. The parity check is held off while parity
3709 * dependent operations are in flight.
3711 if (sh
->check_state
||
3712 (s
.syncing
&& s
.locked
== 0 &&
3713 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3714 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
3715 if (conf
->level
== 6)
3716 handle_parity_checks6(conf
, sh
, &s
, disks
);
3718 handle_parity_checks5(conf
, sh
, &s
, disks
);
3721 if ((s
.replacing
|| s
.syncing
) && s
.locked
== 0
3722 && !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)
3723 && !test_bit(STRIPE_REPLACED
, &sh
->state
)) {
3724 /* Write out to replacement devices where possible */
3725 for (i
= 0; i
< conf
->raid_disks
; i
++)
3726 if (test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
3727 WARN_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
3728 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
3729 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3733 set_bit(STRIPE_INSYNC
, &sh
->state
);
3734 set_bit(STRIPE_REPLACED
, &sh
->state
);
3736 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
3737 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3738 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3739 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3740 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3741 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
3742 wake_up(&conf
->wait_for_overlap
);
3745 /* If the failed drives are just a ReadError, then we might need
3746 * to progress the repair/check process
3748 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
3749 for (i
= 0; i
< s
.failed
; i
++) {
3750 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
3751 if (test_bit(R5_ReadError
, &dev
->flags
)
3752 && !test_bit(R5_LOCKED
, &dev
->flags
)
3753 && test_bit(R5_UPTODATE
, &dev
->flags
)
3755 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3756 set_bit(R5_Wantwrite
, &dev
->flags
);
3757 set_bit(R5_ReWrite
, &dev
->flags
);
3758 set_bit(R5_LOCKED
, &dev
->flags
);
3761 /* let's read it back */
3762 set_bit(R5_Wantread
, &dev
->flags
);
3763 set_bit(R5_LOCKED
, &dev
->flags
);
3770 /* Finish reconstruct operations initiated by the expansion process */
3771 if (sh
->reconstruct_state
== reconstruct_state_result
) {
3772 struct stripe_head
*sh_src
3773 = get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
3774 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
3775 /* sh cannot be written until sh_src has been read.
3776 * so arrange for sh to be delayed a little
3778 set_bit(STRIPE_DELAYED
, &sh
->state
);
3779 set_bit(STRIPE_HANDLE
, &sh
->state
);
3780 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
3782 atomic_inc(&conf
->preread_active_stripes
);
3783 release_stripe(sh_src
);
3787 release_stripe(sh_src
);
3789 sh
->reconstruct_state
= reconstruct_state_idle
;
3790 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3791 for (i
= conf
->raid_disks
; i
--; ) {
3792 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3793 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3798 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
3799 !sh
->reconstruct_state
) {
3800 /* Need to write out all blocks after computing parity */
3801 sh
->disks
= conf
->raid_disks
;
3802 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3803 schedule_reconstruction(sh
, &s
, 1, 1);
3804 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
3805 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3806 atomic_dec(&conf
->reshape_stripes
);
3807 wake_up(&conf
->wait_for_overlap
);
3808 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3811 if (s
.expanding
&& s
.locked
== 0 &&
3812 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3813 handle_stripe_expansion(conf
, sh
);
3816 /* wait for this device to become unblocked */
3817 if (unlikely(s
.blocked_rdev
)) {
3818 if (conf
->mddev
->external
)
3819 md_wait_for_blocked_rdev(s
.blocked_rdev
,
3822 /* Internal metadata will immediately
3823 * be written by raid5d, so we don't
3824 * need to wait here.
3826 rdev_dec_pending(s
.blocked_rdev
,
3830 if (s
.handle_bad_blocks
)
3831 for (i
= disks
; i
--; ) {
3832 struct md_rdev
*rdev
;
3833 struct r5dev
*dev
= &sh
->dev
[i
];
3834 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
3835 /* We own a safe reference to the rdev */
3836 rdev
= conf
->disks
[i
].rdev
;
3837 if (!rdev_set_badblocks(rdev
, sh
->sector
,
3839 md_error(conf
->mddev
, rdev
);
3840 rdev_dec_pending(rdev
, conf
->mddev
);
3842 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
3843 rdev
= conf
->disks
[i
].rdev
;
3844 rdev_clear_badblocks(rdev
, sh
->sector
,
3846 rdev_dec_pending(rdev
, conf
->mddev
);
3848 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3849 rdev
= conf
->disks
[i
].replacement
;
3851 /* rdev have been moved down */
3852 rdev
= conf
->disks
[i
].rdev
;
3853 rdev_clear_badblocks(rdev
, sh
->sector
,
3855 rdev_dec_pending(rdev
, conf
->mddev
);
3860 raid_run_ops(sh
, s
.ops_request
);
3864 if (s
.dec_preread_active
) {
3865 /* We delay this until after ops_run_io so that if make_request
3866 * is waiting on a flush, it won't continue until the writes
3867 * have actually been submitted.
3869 atomic_dec(&conf
->preread_active_stripes
);
3870 if (atomic_read(&conf
->preread_active_stripes
) <
3872 md_wakeup_thread(conf
->mddev
->thread
);
3875 return_io(s
.return_bi
);
3877 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
3880 static void raid5_activate_delayed(struct r5conf
*conf
)
3882 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
3883 while (!list_empty(&conf
->delayed_list
)) {
3884 struct list_head
*l
= conf
->delayed_list
.next
;
3885 struct stripe_head
*sh
;
3886 sh
= list_entry(l
, struct stripe_head
, lru
);
3888 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3889 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3890 atomic_inc(&conf
->preread_active_stripes
);
3891 list_add_tail(&sh
->lru
, &conf
->hold_list
);
3892 raid5_wakeup_stripe_thread(sh
);
3897 static void activate_bit_delay(struct r5conf
*conf
)
3899 /* device_lock is held */
3900 struct list_head head
;
3901 list_add(&head
, &conf
->bitmap_list
);
3902 list_del_init(&conf
->bitmap_list
);
3903 while (!list_empty(&head
)) {
3904 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
3905 list_del_init(&sh
->lru
);
3906 atomic_inc(&sh
->count
);
3907 __release_stripe(conf
, sh
);
3911 int md_raid5_congested(struct mddev
*mddev
, int bits
)
3913 struct r5conf
*conf
= mddev
->private;
3915 /* No difference between reads and writes. Just check
3916 * how busy the stripe_cache is
3919 if (conf
->inactive_blocked
)
3923 if (list_empty_careful(&conf
->inactive_list
))
3928 EXPORT_SYMBOL_GPL(md_raid5_congested
);
3930 static int raid5_congested(void *data
, int bits
)
3932 struct mddev
*mddev
= data
;
3934 return mddev_congested(mddev
, bits
) ||
3935 md_raid5_congested(mddev
, bits
);
3938 /* We want read requests to align with chunks where possible,
3939 * but write requests don't need to.
3941 static int raid5_mergeable_bvec(struct request_queue
*q
,
3942 struct bvec_merge_data
*bvm
,
3943 struct bio_vec
*biovec
)
3945 struct mddev
*mddev
= q
->queuedata
;
3946 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
3948 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3949 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
3951 if ((bvm
->bi_rw
& 1) == WRITE
)
3952 return biovec
->bv_len
; /* always allow writes to be mergeable */
3954 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3955 chunk_sectors
= mddev
->new_chunk_sectors
;
3956 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
3957 if (max
< 0) max
= 0;
3958 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
3959 return biovec
->bv_len
;
3965 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
3967 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
3968 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
3969 unsigned int bio_sectors
= bio_sectors(bio
);
3971 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
3972 chunk_sectors
= mddev
->new_chunk_sectors
;
3973 return chunk_sectors
>=
3974 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
3978 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3979 * later sampled by raid5d.
3981 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
3983 unsigned long flags
;
3985 spin_lock_irqsave(&conf
->device_lock
, flags
);
3987 bi
->bi_next
= conf
->retry_read_aligned_list
;
3988 conf
->retry_read_aligned_list
= bi
;
3990 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3991 md_wakeup_thread(conf
->mddev
->thread
);
3995 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
3999 bi
= conf
->retry_read_aligned
;
4001 conf
->retry_read_aligned
= NULL
;
4004 bi
= conf
->retry_read_aligned_list
;
4006 conf
->retry_read_aligned_list
= bi
->bi_next
;
4009 * this sets the active strip count to 1 and the processed
4010 * strip count to zero (upper 8 bits)
4012 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
4020 * The "raid5_align_endio" should check if the read succeeded and if it
4021 * did, call bio_endio on the original bio (having bio_put the new bio
4023 * If the read failed..
4025 static void raid5_align_endio(struct bio
*bi
, int error
)
4027 struct bio
* raid_bi
= bi
->bi_private
;
4028 struct mddev
*mddev
;
4029 struct r5conf
*conf
;
4030 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4031 struct md_rdev
*rdev
;
4035 rdev
= (void*)raid_bi
->bi_next
;
4036 raid_bi
->bi_next
= NULL
;
4037 mddev
= rdev
->mddev
;
4038 conf
= mddev
->private;
4040 rdev_dec_pending(rdev
, conf
->mddev
);
4042 if (!error
&& uptodate
) {
4043 trace_block_bio_complete(bdev_get_queue(raid_bi
->bi_bdev
),
4045 bio_endio(raid_bi
, 0);
4046 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4047 wake_up(&conf
->wait_for_stripe
);
4052 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
4054 add_bio_to_retry(raid_bi
, conf
);
4057 static int bio_fits_rdev(struct bio
*bi
)
4059 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
4061 if (bio_sectors(bi
) > queue_max_sectors(q
))
4063 blk_recount_segments(q
, bi
);
4064 if (bi
->bi_phys_segments
> queue_max_segments(q
))
4067 if (q
->merge_bvec_fn
)
4068 /* it's too hard to apply the merge_bvec_fn at this stage,
4077 static int chunk_aligned_read(struct mddev
*mddev
, struct bio
* raid_bio
)
4079 struct r5conf
*conf
= mddev
->private;
4081 struct bio
* align_bi
;
4082 struct md_rdev
*rdev
;
4083 sector_t end_sector
;
4085 if (!in_chunk_boundary(mddev
, raid_bio
)) {
4086 pr_debug("chunk_aligned_read : non aligned\n");
4090 * use bio_clone_mddev to make a copy of the bio
4092 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
4096 * set bi_end_io to a new function, and set bi_private to the
4099 align_bi
->bi_end_io
= raid5_align_endio
;
4100 align_bi
->bi_private
= raid_bio
;
4104 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
4108 end_sector
= bio_end_sector(align_bi
);
4110 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
4111 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
4112 rdev
->recovery_offset
< end_sector
) {
4113 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
4115 (test_bit(Faulty
, &rdev
->flags
) ||
4116 !(test_bit(In_sync
, &rdev
->flags
) ||
4117 rdev
->recovery_offset
>= end_sector
)))
4124 atomic_inc(&rdev
->nr_pending
);
4126 raid_bio
->bi_next
= (void*)rdev
;
4127 align_bi
->bi_bdev
= rdev
->bdev
;
4128 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
4130 if (!bio_fits_rdev(align_bi
) ||
4131 is_badblock(rdev
, align_bi
->bi_sector
, bio_sectors(align_bi
),
4132 &first_bad
, &bad_sectors
)) {
4133 /* too big in some way, or has a known bad block */
4135 rdev_dec_pending(rdev
, mddev
);
4139 /* No reshape active, so we can trust rdev->data_offset */
4140 align_bi
->bi_sector
+= rdev
->data_offset
;
4142 spin_lock_irq(&conf
->device_lock
);
4143 wait_event_lock_irq(conf
->wait_for_stripe
,
4146 atomic_inc(&conf
->active_aligned_reads
);
4147 spin_unlock_irq(&conf
->device_lock
);
4150 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
4151 align_bi
, disk_devt(mddev
->gendisk
),
4152 raid_bio
->bi_sector
);
4153 generic_make_request(align_bi
);
4162 /* __get_priority_stripe - get the next stripe to process
4164 * Full stripe writes are allowed to pass preread active stripes up until
4165 * the bypass_threshold is exceeded. In general the bypass_count
4166 * increments when the handle_list is handled before the hold_list; however, it
4167 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4168 * stripe with in flight i/o. The bypass_count will be reset when the
4169 * head of the hold_list has changed, i.e. the head was promoted to the
4172 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
, int group
)
4174 struct stripe_head
*sh
= NULL
, *tmp
;
4175 struct list_head
*handle_list
= NULL
;
4176 struct r5worker_group
*wg
= NULL
;
4178 if (conf
->worker_cnt_per_group
== 0) {
4179 handle_list
= &conf
->handle_list
;
4180 } else if (group
!= ANY_GROUP
) {
4181 handle_list
= &conf
->worker_groups
[group
].handle_list
;
4182 wg
= &conf
->worker_groups
[group
];
4185 for (i
= 0; i
< conf
->group_cnt
; i
++) {
4186 handle_list
= &conf
->worker_groups
[i
].handle_list
;
4187 wg
= &conf
->worker_groups
[i
];
4188 if (!list_empty(handle_list
))
4193 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4195 list_empty(handle_list
) ? "empty" : "busy",
4196 list_empty(&conf
->hold_list
) ? "empty" : "busy",
4197 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
4199 if (!list_empty(handle_list
)) {
4200 sh
= list_entry(handle_list
->next
, typeof(*sh
), lru
);
4202 if (list_empty(&conf
->hold_list
))
4203 conf
->bypass_count
= 0;
4204 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
4205 if (conf
->hold_list
.next
== conf
->last_hold
)
4206 conf
->bypass_count
++;
4208 conf
->last_hold
= conf
->hold_list
.next
;
4209 conf
->bypass_count
-= conf
->bypass_threshold
;
4210 if (conf
->bypass_count
< 0)
4211 conf
->bypass_count
= 0;
4214 } else if (!list_empty(&conf
->hold_list
) &&
4215 ((conf
->bypass_threshold
&&
4216 conf
->bypass_count
> conf
->bypass_threshold
) ||
4217 atomic_read(&conf
->pending_full_writes
) == 0)) {
4219 list_for_each_entry(tmp
, &conf
->hold_list
, lru
) {
4220 if (conf
->worker_cnt_per_group
== 0 ||
4221 group
== ANY_GROUP
||
4222 !cpu_online(tmp
->cpu
) ||
4223 cpu_to_group(tmp
->cpu
) == group
) {
4230 conf
->bypass_count
-= conf
->bypass_threshold
;
4231 if (conf
->bypass_count
< 0)
4232 conf
->bypass_count
= 0;
4244 list_del_init(&sh
->lru
);
4245 atomic_inc(&sh
->count
);
4246 BUG_ON(atomic_read(&sh
->count
) != 1);
4250 struct raid5_plug_cb
{
4251 struct blk_plug_cb cb
;
4252 struct list_head list
;
4255 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
4257 struct raid5_plug_cb
*cb
= container_of(
4258 blk_cb
, struct raid5_plug_cb
, cb
);
4259 struct stripe_head
*sh
;
4260 struct mddev
*mddev
= cb
->cb
.data
;
4261 struct r5conf
*conf
= mddev
->private;
4264 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
4265 spin_lock_irq(&conf
->device_lock
);
4266 while (!list_empty(&cb
->list
)) {
4267 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
4268 list_del_init(&sh
->lru
);
4270 * avoid race release_stripe_plug() sees
4271 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4272 * is still in our list
4274 smp_mb__before_clear_bit();
4275 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
4277 * STRIPE_ON_RELEASE_LIST could be set here. In that
4278 * case, the count is always > 1 here
4280 __release_stripe(conf
, sh
);
4283 spin_unlock_irq(&conf
->device_lock
);
4286 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
4290 static void release_stripe_plug(struct mddev
*mddev
,
4291 struct stripe_head
*sh
)
4293 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
4294 raid5_unplug
, mddev
,
4295 sizeof(struct raid5_plug_cb
));
4296 struct raid5_plug_cb
*cb
;
4303 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
4305 if (cb
->list
.next
== NULL
)
4306 INIT_LIST_HEAD(&cb
->list
);
4308 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
4309 list_add_tail(&sh
->lru
, &cb
->list
);
4314 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
4316 struct r5conf
*conf
= mddev
->private;
4317 sector_t logical_sector
, last_sector
;
4318 struct stripe_head
*sh
;
4322 if (mddev
->reshape_position
!= MaxSector
)
4323 /* Skip discard while reshape is happening */
4326 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4327 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
4330 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
4332 stripe_sectors
= conf
->chunk_sectors
*
4333 (conf
->raid_disks
- conf
->max_degraded
);
4334 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
4336 sector_div(last_sector
, stripe_sectors
);
4338 logical_sector
*= conf
->chunk_sectors
;
4339 last_sector
*= conf
->chunk_sectors
;
4341 for (; logical_sector
< last_sector
;
4342 logical_sector
+= STRIPE_SECTORS
) {
4346 sh
= get_active_stripe(conf
, logical_sector
, 0, 0, 0);
4347 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
4348 TASK_UNINTERRUPTIBLE
);
4349 set_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
4350 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
4355 clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
4356 spin_lock_irq(&sh
->stripe_lock
);
4357 for (d
= 0; d
< conf
->raid_disks
; d
++) {
4358 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
4360 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
4361 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
4362 spin_unlock_irq(&sh
->stripe_lock
);
4368 set_bit(STRIPE_DISCARD
, &sh
->state
);
4369 finish_wait(&conf
->wait_for_overlap
, &w
);
4370 for (d
= 0; d
< conf
->raid_disks
; d
++) {
4371 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
4373 sh
->dev
[d
].towrite
= bi
;
4374 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
4375 raid5_inc_bi_active_stripes(bi
);
4377 spin_unlock_irq(&sh
->stripe_lock
);
4378 if (conf
->mddev
->bitmap
) {
4380 d
< conf
->raid_disks
- conf
->max_degraded
;
4382 bitmap_startwrite(mddev
->bitmap
,
4386 sh
->bm_seq
= conf
->seq_flush
+ 1;
4387 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
4390 set_bit(STRIPE_HANDLE
, &sh
->state
);
4391 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4392 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4393 atomic_inc(&conf
->preread_active_stripes
);
4394 release_stripe_plug(mddev
, sh
);
4397 remaining
= raid5_dec_bi_active_stripes(bi
);
4398 if (remaining
== 0) {
4399 md_write_end(mddev
);
4404 static void make_request(struct mddev
*mddev
, struct bio
* bi
)
4406 struct r5conf
*conf
= mddev
->private;
4408 sector_t new_sector
;
4409 sector_t logical_sector
, last_sector
;
4410 struct stripe_head
*sh
;
4411 const int rw
= bio_data_dir(bi
);
4414 if (unlikely(bi
->bi_rw
& REQ_FLUSH
)) {
4415 md_flush_request(mddev
, bi
);
4419 md_write_start(mddev
, bi
);
4422 mddev
->reshape_position
== MaxSector
&&
4423 chunk_aligned_read(mddev
,bi
))
4426 if (unlikely(bi
->bi_rw
& REQ_DISCARD
)) {
4427 make_discard_request(mddev
, bi
);
4431 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4432 last_sector
= bio_end_sector(bi
);
4434 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
4436 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
4442 seq
= read_seqcount_begin(&conf
->gen_lock
);
4444 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
4445 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
4446 /* spinlock is needed as reshape_progress may be
4447 * 64bit on a 32bit platform, and so it might be
4448 * possible to see a half-updated value
4449 * Of course reshape_progress could change after
4450 * the lock is dropped, so once we get a reference
4451 * to the stripe that we think it is, we will have
4454 spin_lock_irq(&conf
->device_lock
);
4455 if (mddev
->reshape_backwards
4456 ? logical_sector
< conf
->reshape_progress
4457 : logical_sector
>= conf
->reshape_progress
) {
4460 if (mddev
->reshape_backwards
4461 ? logical_sector
< conf
->reshape_safe
4462 : logical_sector
>= conf
->reshape_safe
) {
4463 spin_unlock_irq(&conf
->device_lock
);
4468 spin_unlock_irq(&conf
->device_lock
);
4471 new_sector
= raid5_compute_sector(conf
, logical_sector
,
4474 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4475 (unsigned long long)new_sector
,
4476 (unsigned long long)logical_sector
);
4478 sh
= get_active_stripe(conf
, new_sector
, previous
,
4479 (bi
->bi_rw
&RWA_MASK
), 0);
4481 if (unlikely(previous
)) {
4482 /* expansion might have moved on while waiting for a
4483 * stripe, so we must do the range check again.
4484 * Expansion could still move past after this
4485 * test, but as we are holding a reference to
4486 * 'sh', we know that if that happens,
4487 * STRIPE_EXPANDING will get set and the expansion
4488 * won't proceed until we finish with the stripe.
4491 spin_lock_irq(&conf
->device_lock
);
4492 if (mddev
->reshape_backwards
4493 ? logical_sector
>= conf
->reshape_progress
4494 : logical_sector
< conf
->reshape_progress
)
4495 /* mismatch, need to try again */
4497 spin_unlock_irq(&conf
->device_lock
);
4504 if (read_seqcount_retry(&conf
->gen_lock
, seq
)) {
4505 /* Might have got the wrong stripe_head
4513 logical_sector
>= mddev
->suspend_lo
&&
4514 logical_sector
< mddev
->suspend_hi
) {
4516 /* As the suspend_* range is controlled by
4517 * userspace, we want an interruptible
4520 flush_signals(current
);
4521 prepare_to_wait(&conf
->wait_for_overlap
,
4522 &w
, TASK_INTERRUPTIBLE
);
4523 if (logical_sector
>= mddev
->suspend_lo
&&
4524 logical_sector
< mddev
->suspend_hi
)
4529 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
4530 !add_stripe_bio(sh
, bi
, dd_idx
, rw
)) {
4531 /* Stripe is busy expanding or
4532 * add failed due to overlap. Flush everything
4535 md_wakeup_thread(mddev
->thread
);
4540 finish_wait(&conf
->wait_for_overlap
, &w
);
4541 set_bit(STRIPE_HANDLE
, &sh
->state
);
4542 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4543 if ((bi
->bi_rw
& REQ_SYNC
) &&
4544 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4545 atomic_inc(&conf
->preread_active_stripes
);
4546 release_stripe_plug(mddev
, sh
);
4548 /* cannot get stripe for read-ahead, just give-up */
4549 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4550 finish_wait(&conf
->wait_for_overlap
, &w
);
4555 remaining
= raid5_dec_bi_active_stripes(bi
);
4556 if (remaining
== 0) {
4559 md_write_end(mddev
);
4561 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
4567 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
4569 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
4571 /* reshaping is quite different to recovery/resync so it is
4572 * handled quite separately ... here.
4574 * On each call to sync_request, we gather one chunk worth of
4575 * destination stripes and flag them as expanding.
4576 * Then we find all the source stripes and request reads.
4577 * As the reads complete, handle_stripe will copy the data
4578 * into the destination stripe and release that stripe.
4580 struct r5conf
*conf
= mddev
->private;
4581 struct stripe_head
*sh
;
4582 sector_t first_sector
, last_sector
;
4583 int raid_disks
= conf
->previous_raid_disks
;
4584 int data_disks
= raid_disks
- conf
->max_degraded
;
4585 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4588 sector_t writepos
, readpos
, safepos
;
4589 sector_t stripe_addr
;
4590 int reshape_sectors
;
4591 struct list_head stripes
;
4593 if (sector_nr
== 0) {
4594 /* If restarting in the middle, skip the initial sectors */
4595 if (mddev
->reshape_backwards
&&
4596 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
4597 sector_nr
= raid5_size(mddev
, 0, 0)
4598 - conf
->reshape_progress
;
4599 } else if (!mddev
->reshape_backwards
&&
4600 conf
->reshape_progress
> 0)
4601 sector_nr
= conf
->reshape_progress
;
4602 sector_div(sector_nr
, new_data_disks
);
4604 mddev
->curr_resync_completed
= sector_nr
;
4605 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4611 /* We need to process a full chunk at a time.
4612 * If old and new chunk sizes differ, we need to process the
4615 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
)
4616 reshape_sectors
= mddev
->new_chunk_sectors
;
4618 reshape_sectors
= mddev
->chunk_sectors
;
4620 /* We update the metadata at least every 10 seconds, or when
4621 * the data about to be copied would over-write the source of
4622 * the data at the front of the range. i.e. one new_stripe
4623 * along from reshape_progress new_maps to after where
4624 * reshape_safe old_maps to
4626 writepos
= conf
->reshape_progress
;
4627 sector_div(writepos
, new_data_disks
);
4628 readpos
= conf
->reshape_progress
;
4629 sector_div(readpos
, data_disks
);
4630 safepos
= conf
->reshape_safe
;
4631 sector_div(safepos
, data_disks
);
4632 if (mddev
->reshape_backwards
) {
4633 writepos
-= min_t(sector_t
, reshape_sectors
, writepos
);
4634 readpos
+= reshape_sectors
;
4635 safepos
+= reshape_sectors
;
4637 writepos
+= reshape_sectors
;
4638 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
4639 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
4642 /* Having calculated the 'writepos' possibly use it
4643 * to set 'stripe_addr' which is where we will write to.
4645 if (mddev
->reshape_backwards
) {
4646 BUG_ON(conf
->reshape_progress
== 0);
4647 stripe_addr
= writepos
;
4648 BUG_ON((mddev
->dev_sectors
&
4649 ~((sector_t
)reshape_sectors
- 1))
4650 - reshape_sectors
- stripe_addr
4653 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
4654 stripe_addr
= sector_nr
;
4657 /* 'writepos' is the most advanced device address we might write.
4658 * 'readpos' is the least advanced device address we might read.
4659 * 'safepos' is the least address recorded in the metadata as having
4661 * If there is a min_offset_diff, these are adjusted either by
4662 * increasing the safepos/readpos if diff is negative, or
4663 * increasing writepos if diff is positive.
4664 * If 'readpos' is then behind 'writepos', there is no way that we can
4665 * ensure safety in the face of a crash - that must be done by userspace
4666 * making a backup of the data. So in that case there is no particular
4667 * rush to update metadata.
4668 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4669 * update the metadata to advance 'safepos' to match 'readpos' so that
4670 * we can be safe in the event of a crash.
4671 * So we insist on updating metadata if safepos is behind writepos and
4672 * readpos is beyond writepos.
4673 * In any case, update the metadata every 10 seconds.
4674 * Maybe that number should be configurable, but I'm not sure it is
4675 * worth it.... maybe it could be a multiple of safemode_delay???
4677 if (conf
->min_offset_diff
< 0) {
4678 safepos
+= -conf
->min_offset_diff
;
4679 readpos
+= -conf
->min_offset_diff
;
4681 writepos
+= conf
->min_offset_diff
;
4683 if ((mddev
->reshape_backwards
4684 ? (safepos
> writepos
&& readpos
< writepos
)
4685 : (safepos
< writepos
&& readpos
> writepos
)) ||
4686 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4687 /* Cannot proceed until we've updated the superblock... */
4688 wait_event(conf
->wait_for_overlap
,
4689 atomic_read(&conf
->reshape_stripes
)==0);
4690 mddev
->reshape_position
= conf
->reshape_progress
;
4691 mddev
->curr_resync_completed
= sector_nr
;
4692 conf
->reshape_checkpoint
= jiffies
;
4693 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4694 md_wakeup_thread(mddev
->thread
);
4695 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4696 kthread_should_stop());
4697 spin_lock_irq(&conf
->device_lock
);
4698 conf
->reshape_safe
= mddev
->reshape_position
;
4699 spin_unlock_irq(&conf
->device_lock
);
4700 wake_up(&conf
->wait_for_overlap
);
4701 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4704 INIT_LIST_HEAD(&stripes
);
4705 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
4707 int skipped_disk
= 0;
4708 sh
= get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
4709 set_bit(STRIPE_EXPANDING
, &sh
->state
);
4710 atomic_inc(&conf
->reshape_stripes
);
4711 /* If any of this stripe is beyond the end of the old
4712 * array, then we need to zero those blocks
4714 for (j
=sh
->disks
; j
--;) {
4716 if (j
== sh
->pd_idx
)
4718 if (conf
->level
== 6 &&
4721 s
= compute_blocknr(sh
, j
, 0);
4722 if (s
< raid5_size(mddev
, 0, 0)) {
4726 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
4727 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
4728 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
4730 if (!skipped_disk
) {
4731 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4732 set_bit(STRIPE_HANDLE
, &sh
->state
);
4734 list_add(&sh
->lru
, &stripes
);
4736 spin_lock_irq(&conf
->device_lock
);
4737 if (mddev
->reshape_backwards
)
4738 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
4740 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
4741 spin_unlock_irq(&conf
->device_lock
);
4742 /* Ok, those stripe are ready. We can start scheduling
4743 * reads on the source stripes.
4744 * The source stripes are determined by mapping the first and last
4745 * block on the destination stripes.
4748 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
4751 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
4752 * new_data_disks
- 1),
4754 if (last_sector
>= mddev
->dev_sectors
)
4755 last_sector
= mddev
->dev_sectors
- 1;
4756 while (first_sector
<= last_sector
) {
4757 sh
= get_active_stripe(conf
, first_sector
, 1, 0, 1);
4758 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4759 set_bit(STRIPE_HANDLE
, &sh
->state
);
4761 first_sector
+= STRIPE_SECTORS
;
4763 /* Now that the sources are clearly marked, we can release
4764 * the destination stripes
4766 while (!list_empty(&stripes
)) {
4767 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
4768 list_del_init(&sh
->lru
);
4771 /* If this takes us to the resync_max point where we have to pause,
4772 * then we need to write out the superblock.
4774 sector_nr
+= reshape_sectors
;
4775 if ((sector_nr
- mddev
->curr_resync_completed
) * 2
4776 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
4777 /* Cannot proceed until we've updated the superblock... */
4778 wait_event(conf
->wait_for_overlap
,
4779 atomic_read(&conf
->reshape_stripes
) == 0);
4780 mddev
->reshape_position
= conf
->reshape_progress
;
4781 mddev
->curr_resync_completed
= sector_nr
;
4782 conf
->reshape_checkpoint
= jiffies
;
4783 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4784 md_wakeup_thread(mddev
->thread
);
4785 wait_event(mddev
->sb_wait
,
4786 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
4787 || kthread_should_stop());
4788 spin_lock_irq(&conf
->device_lock
);
4789 conf
->reshape_safe
= mddev
->reshape_position
;
4790 spin_unlock_irq(&conf
->device_lock
);
4791 wake_up(&conf
->wait_for_overlap
);
4792 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4794 return reshape_sectors
;
4797 /* FIXME go_faster isn't used */
4798 static inline sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
4800 struct r5conf
*conf
= mddev
->private;
4801 struct stripe_head
*sh
;
4802 sector_t max_sector
= mddev
->dev_sectors
;
4803 sector_t sync_blocks
;
4804 int still_degraded
= 0;
4807 if (sector_nr
>= max_sector
) {
4808 /* just being told to finish up .. nothing much to do */
4810 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
4815 if (mddev
->curr_resync
< max_sector
) /* aborted */
4816 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
4818 else /* completed sync */
4820 bitmap_close_sync(mddev
->bitmap
);
4825 /* Allow raid5_quiesce to complete */
4826 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
4828 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4829 return reshape_request(mddev
, sector_nr
, skipped
);
4831 /* No need to check resync_max as we never do more than one
4832 * stripe, and as resync_max will always be on a chunk boundary,
4833 * if the check in md_do_sync didn't fire, there is no chance
4834 * of overstepping resync_max here
4837 /* if there is too many failed drives and we are trying
4838 * to resync, then assert that we are finished, because there is
4839 * nothing we can do.
4841 if (mddev
->degraded
>= conf
->max_degraded
&&
4842 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4843 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
4847 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
4849 !bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
4850 sync_blocks
>= STRIPE_SECTORS
) {
4851 /* we can skip this block, and probably more */
4852 sync_blocks
/= STRIPE_SECTORS
;
4854 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
4857 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
4859 sh
= get_active_stripe(conf
, sector_nr
, 0, 1, 0);
4861 sh
= get_active_stripe(conf
, sector_nr
, 0, 0, 0);
4862 /* make sure we don't swamp the stripe cache if someone else
4863 * is trying to get access
4865 schedule_timeout_uninterruptible(1);
4867 /* Need to check if array will still be degraded after recovery/resync
4868 * We don't need to check the 'failed' flag as when that gets set,
4871 for (i
= 0; i
< conf
->raid_disks
; i
++)
4872 if (conf
->disks
[i
].rdev
== NULL
)
4875 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
4877 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
4882 return STRIPE_SECTORS
;
4885 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
4887 /* We may not be able to submit a whole bio at once as there
4888 * may not be enough stripe_heads available.
4889 * We cannot pre-allocate enough stripe_heads as we may need
4890 * more than exist in the cache (if we allow ever large chunks).
4891 * So we do one stripe head at a time and record in
4892 * ->bi_hw_segments how many have been done.
4894 * We *know* that this entire raid_bio is in one chunk, so
4895 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4897 struct stripe_head
*sh
;
4899 sector_t sector
, logical_sector
, last_sector
;
4904 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4905 sector
= raid5_compute_sector(conf
, logical_sector
,
4907 last_sector
= bio_end_sector(raid_bio
);
4909 for (; logical_sector
< last_sector
;
4910 logical_sector
+= STRIPE_SECTORS
,
4911 sector
+= STRIPE_SECTORS
,
4914 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
4915 /* already done this stripe */
4918 sh
= get_active_stripe(conf
, sector
, 0, 1, 0);
4921 /* failed to get a stripe - must wait */
4922 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
4923 conf
->retry_read_aligned
= raid_bio
;
4927 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
4929 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
4930 conf
->retry_read_aligned
= raid_bio
;
4934 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
4939 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
4940 if (remaining
== 0) {
4941 trace_block_bio_complete(bdev_get_queue(raid_bio
->bi_bdev
),
4943 bio_endio(raid_bio
, 0);
4945 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4946 wake_up(&conf
->wait_for_stripe
);
4950 static int handle_active_stripes(struct r5conf
*conf
, int group
,
4951 struct r5worker
*worker
)
4953 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
4954 int i
, batch_size
= 0;
4956 while (batch_size
< MAX_STRIPE_BATCH
&&
4957 (sh
= __get_priority_stripe(conf
, group
)) != NULL
)
4958 batch
[batch_size
++] = sh
;
4960 if (batch_size
== 0)
4962 spin_unlock_irq(&conf
->device_lock
);
4964 for (i
= 0; i
< batch_size
; i
++)
4965 handle_stripe(batch
[i
]);
4969 spin_lock_irq(&conf
->device_lock
);
4970 for (i
= 0; i
< batch_size
; i
++)
4971 __release_stripe(conf
, batch
[i
]);
4975 static void raid5_do_work(struct work_struct
*work
)
4977 struct r5worker
*worker
= container_of(work
, struct r5worker
, work
);
4978 struct r5worker_group
*group
= worker
->group
;
4979 struct r5conf
*conf
= group
->conf
;
4980 int group_id
= group
- conf
->worker_groups
;
4982 struct blk_plug plug
;
4984 pr_debug("+++ raid5worker active\n");
4986 blk_start_plug(&plug
);
4988 spin_lock_irq(&conf
->device_lock
);
4990 int batch_size
, released
;
4992 released
= release_stripe_list(conf
);
4994 batch_size
= handle_active_stripes(conf
, group_id
, worker
);
4995 worker
->working
= false;
4996 if (!batch_size
&& !released
)
4998 handled
+= batch_size
;
5000 pr_debug("%d stripes handled\n", handled
);
5002 spin_unlock_irq(&conf
->device_lock
);
5003 blk_finish_plug(&plug
);
5005 pr_debug("--- raid5worker inactive\n");
5009 * This is our raid5 kernel thread.
5011 * We scan the hash table for stripes which can be handled now.
5012 * During the scan, completed stripes are saved for us by the interrupt
5013 * handler, so that they will not have to wait for our next wakeup.
5015 static void raid5d(struct md_thread
*thread
)
5017 struct mddev
*mddev
= thread
->mddev
;
5018 struct r5conf
*conf
= mddev
->private;
5020 struct blk_plug plug
;
5022 pr_debug("+++ raid5d active\n");
5024 md_check_recovery(mddev
);
5026 blk_start_plug(&plug
);
5028 spin_lock_irq(&conf
->device_lock
);
5031 int batch_size
, released
;
5033 released
= release_stripe_list(conf
);
5036 !list_empty(&conf
->bitmap_list
)) {
5037 /* Now is a good time to flush some bitmap updates */
5039 spin_unlock_irq(&conf
->device_lock
);
5040 bitmap_unplug(mddev
->bitmap
);
5041 spin_lock_irq(&conf
->device_lock
);
5042 conf
->seq_write
= conf
->seq_flush
;
5043 activate_bit_delay(conf
);
5045 raid5_activate_delayed(conf
);
5047 while ((bio
= remove_bio_from_retry(conf
))) {
5049 spin_unlock_irq(&conf
->device_lock
);
5050 ok
= retry_aligned_read(conf
, bio
);
5051 spin_lock_irq(&conf
->device_lock
);
5057 batch_size
= handle_active_stripes(conf
, ANY_GROUP
, NULL
);
5058 if (!batch_size
&& !released
)
5060 handled
+= batch_size
;
5062 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
)) {
5063 spin_unlock_irq(&conf
->device_lock
);
5064 md_check_recovery(mddev
);
5065 spin_lock_irq(&conf
->device_lock
);
5068 pr_debug("%d stripes handled\n", handled
);
5070 spin_unlock_irq(&conf
->device_lock
);
5072 async_tx_issue_pending_all();
5073 blk_finish_plug(&plug
);
5075 pr_debug("--- raid5d inactive\n");
5079 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
5081 struct r5conf
*conf
= mddev
->private;
5083 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
5089 raid5_set_cache_size(struct mddev
*mddev
, int size
)
5091 struct r5conf
*conf
= mddev
->private;
5094 if (size
<= 16 || size
> 32768)
5096 while (size
< conf
->max_nr_stripes
) {
5097 if (drop_one_stripe(conf
))
5098 conf
->max_nr_stripes
--;
5102 err
= md_allow_write(mddev
);
5105 while (size
> conf
->max_nr_stripes
) {
5106 if (grow_one_stripe(conf
))
5107 conf
->max_nr_stripes
++;
5112 EXPORT_SYMBOL(raid5_set_cache_size
);
5115 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
5117 struct r5conf
*conf
= mddev
->private;
5121 if (len
>= PAGE_SIZE
)
5126 if (kstrtoul(page
, 10, &new))
5128 err
= raid5_set_cache_size(mddev
, new);
5134 static struct md_sysfs_entry
5135 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
5136 raid5_show_stripe_cache_size
,
5137 raid5_store_stripe_cache_size
);
5140 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
5142 struct r5conf
*conf
= mddev
->private;
5144 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
5150 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
5152 struct r5conf
*conf
= mddev
->private;
5154 if (len
>= PAGE_SIZE
)
5159 if (kstrtoul(page
, 10, &new))
5161 if (new > conf
->max_nr_stripes
)
5163 conf
->bypass_threshold
= new;
5167 static struct md_sysfs_entry
5168 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
5170 raid5_show_preread_threshold
,
5171 raid5_store_preread_threshold
);
5174 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
5176 struct r5conf
*conf
= mddev
->private;
5178 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
5183 static struct md_sysfs_entry
5184 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
5187 raid5_show_group_thread_cnt(struct mddev
*mddev
, char *page
)
5189 struct r5conf
*conf
= mddev
->private;
5191 return sprintf(page
, "%d\n", conf
->worker_cnt_per_group
);
5196 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
);
5198 raid5_store_group_thread_cnt(struct mddev
*mddev
, const char *page
, size_t len
)
5200 struct r5conf
*conf
= mddev
->private;
5203 struct r5worker_group
*old_groups
;
5206 if (len
>= PAGE_SIZE
)
5211 if (kstrtoul(page
, 10, &new))
5214 if (new == conf
->worker_cnt_per_group
)
5217 mddev_suspend(mddev
);
5219 old_groups
= conf
->worker_groups
;
5220 old_group_cnt
= conf
->worker_cnt_per_group
;
5222 conf
->worker_groups
= NULL
;
5223 err
= alloc_thread_groups(conf
, new);
5225 conf
->worker_groups
= old_groups
;
5226 conf
->worker_cnt_per_group
= old_group_cnt
;
5229 kfree(old_groups
[0].workers
);
5233 mddev_resume(mddev
);
5240 static struct md_sysfs_entry
5241 raid5_group_thread_cnt
= __ATTR(group_thread_cnt
, S_IRUGO
| S_IWUSR
,
5242 raid5_show_group_thread_cnt
,
5243 raid5_store_group_thread_cnt
);
5245 static struct attribute
*raid5_attrs
[] = {
5246 &raid5_stripecache_size
.attr
,
5247 &raid5_stripecache_active
.attr
,
5248 &raid5_preread_bypass_threshold
.attr
,
5249 &raid5_group_thread_cnt
.attr
,
5252 static struct attribute_group raid5_attrs_group
= {
5254 .attrs
= raid5_attrs
,
5257 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
)
5261 struct r5worker
*workers
;
5263 conf
->worker_cnt_per_group
= cnt
;
5265 conf
->worker_groups
= NULL
;
5268 conf
->group_cnt
= num_possible_nodes();
5269 size
= sizeof(struct r5worker
) * cnt
;
5270 workers
= kzalloc(size
* conf
->group_cnt
, GFP_NOIO
);
5271 conf
->worker_groups
= kzalloc(sizeof(struct r5worker_group
) *
5272 conf
->group_cnt
, GFP_NOIO
);
5273 if (!conf
->worker_groups
|| !workers
) {
5275 kfree(conf
->worker_groups
);
5276 conf
->worker_groups
= NULL
;
5280 for (i
= 0; i
< conf
->group_cnt
; i
++) {
5281 struct r5worker_group
*group
;
5283 group
= &conf
->worker_groups
[i
];
5284 INIT_LIST_HEAD(&group
->handle_list
);
5286 group
->workers
= workers
+ i
* cnt
;
5288 for (j
= 0; j
< cnt
; j
++) {
5289 group
->workers
[j
].group
= group
;
5290 INIT_WORK(&group
->workers
[j
].work
, raid5_do_work
);
5297 static void free_thread_groups(struct r5conf
*conf
)
5299 if (conf
->worker_groups
)
5300 kfree(conf
->worker_groups
[0].workers
);
5301 kfree(conf
->worker_groups
);
5302 conf
->worker_groups
= NULL
;
5306 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
5308 struct r5conf
*conf
= mddev
->private;
5311 sectors
= mddev
->dev_sectors
;
5313 /* size is defined by the smallest of previous and new size */
5314 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
5316 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
5317 sectors
&= ~((sector_t
)mddev
->new_chunk_sectors
- 1);
5318 return sectors
* (raid_disks
- conf
->max_degraded
);
5321 static void raid5_free_percpu(struct r5conf
*conf
)
5323 struct raid5_percpu
*percpu
;
5330 for_each_possible_cpu(cpu
) {
5331 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
5332 safe_put_page(percpu
->spare_page
);
5333 kfree(percpu
->scribble
);
5335 #ifdef CONFIG_HOTPLUG_CPU
5336 unregister_cpu_notifier(&conf
->cpu_notify
);
5340 free_percpu(conf
->percpu
);
5343 static void free_conf(struct r5conf
*conf
)
5345 free_thread_groups(conf
);
5346 shrink_stripes(conf
);
5347 raid5_free_percpu(conf
);
5349 kfree(conf
->stripe_hashtbl
);
5353 #ifdef CONFIG_HOTPLUG_CPU
5354 static int raid456_cpu_notify(struct notifier_block
*nfb
, unsigned long action
,
5357 struct r5conf
*conf
= container_of(nfb
, struct r5conf
, cpu_notify
);
5358 long cpu
= (long)hcpu
;
5359 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
5362 case CPU_UP_PREPARE
:
5363 case CPU_UP_PREPARE_FROZEN
:
5364 if (conf
->level
== 6 && !percpu
->spare_page
)
5365 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
5366 if (!percpu
->scribble
)
5367 percpu
->scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
5369 if (!percpu
->scribble
||
5370 (conf
->level
== 6 && !percpu
->spare_page
)) {
5371 safe_put_page(percpu
->spare_page
);
5372 kfree(percpu
->scribble
);
5373 pr_err("%s: failed memory allocation for cpu%ld\n",
5375 return notifier_from_errno(-ENOMEM
);
5379 case CPU_DEAD_FROZEN
:
5380 safe_put_page(percpu
->spare_page
);
5381 kfree(percpu
->scribble
);
5382 percpu
->spare_page
= NULL
;
5383 percpu
->scribble
= NULL
;
5392 static int raid5_alloc_percpu(struct r5conf
*conf
)
5395 struct page
*spare_page
;
5396 struct raid5_percpu __percpu
*allcpus
;
5400 allcpus
= alloc_percpu(struct raid5_percpu
);
5403 conf
->percpu
= allcpus
;
5407 for_each_present_cpu(cpu
) {
5408 if (conf
->level
== 6) {
5409 spare_page
= alloc_page(GFP_KERNEL
);
5414 per_cpu_ptr(conf
->percpu
, cpu
)->spare_page
= spare_page
;
5416 scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
5421 per_cpu_ptr(conf
->percpu
, cpu
)->scribble
= scribble
;
5423 #ifdef CONFIG_HOTPLUG_CPU
5424 conf
->cpu_notify
.notifier_call
= raid456_cpu_notify
;
5425 conf
->cpu_notify
.priority
= 0;
5427 err
= register_cpu_notifier(&conf
->cpu_notify
);
5434 static struct r5conf
*setup_conf(struct mddev
*mddev
)
5436 struct r5conf
*conf
;
5437 int raid_disk
, memory
, max_disks
;
5438 struct md_rdev
*rdev
;
5439 struct disk_info
*disk
;
5442 if (mddev
->new_level
!= 5
5443 && mddev
->new_level
!= 4
5444 && mddev
->new_level
!= 6) {
5445 printk(KERN_ERR
"md/raid:%s: raid level not set to 4/5/6 (%d)\n",
5446 mdname(mddev
), mddev
->new_level
);
5447 return ERR_PTR(-EIO
);
5449 if ((mddev
->new_level
== 5
5450 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
5451 (mddev
->new_level
== 6
5452 && !algorithm_valid_raid6(mddev
->new_layout
))) {
5453 printk(KERN_ERR
"md/raid:%s: layout %d not supported\n",
5454 mdname(mddev
), mddev
->new_layout
);
5455 return ERR_PTR(-EIO
);
5457 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
5458 printk(KERN_ERR
"md/raid:%s: not enough configured devices (%d, minimum 4)\n",
5459 mdname(mddev
), mddev
->raid_disks
);
5460 return ERR_PTR(-EINVAL
);
5463 if (!mddev
->new_chunk_sectors
||
5464 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
5465 !is_power_of_2(mddev
->new_chunk_sectors
)) {
5466 printk(KERN_ERR
"md/raid:%s: invalid chunk size %d\n",
5467 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
5468 return ERR_PTR(-EINVAL
);
5471 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
5474 /* Don't enable multi-threading by default*/
5475 if (alloc_thread_groups(conf
, 0))
5477 spin_lock_init(&conf
->device_lock
);
5478 seqcount_init(&conf
->gen_lock
);
5479 init_waitqueue_head(&conf
->wait_for_stripe
);
5480 init_waitqueue_head(&conf
->wait_for_overlap
);
5481 INIT_LIST_HEAD(&conf
->handle_list
);
5482 INIT_LIST_HEAD(&conf
->hold_list
);
5483 INIT_LIST_HEAD(&conf
->delayed_list
);
5484 INIT_LIST_HEAD(&conf
->bitmap_list
);
5485 INIT_LIST_HEAD(&conf
->inactive_list
);
5486 init_llist_head(&conf
->released_stripes
);
5487 atomic_set(&conf
->active_stripes
, 0);
5488 atomic_set(&conf
->preread_active_stripes
, 0);
5489 atomic_set(&conf
->active_aligned_reads
, 0);
5490 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
5491 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
5493 conf
->raid_disks
= mddev
->raid_disks
;
5494 if (mddev
->reshape_position
== MaxSector
)
5495 conf
->previous_raid_disks
= mddev
->raid_disks
;
5497 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
5498 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
5499 conf
->scribble_len
= scribble_len(max_disks
);
5501 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
5506 conf
->mddev
= mddev
;
5508 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
5511 conf
->level
= mddev
->new_level
;
5512 if (raid5_alloc_percpu(conf
) != 0)
5515 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
5517 rdev_for_each(rdev
, mddev
) {
5518 raid_disk
= rdev
->raid_disk
;
5519 if (raid_disk
>= max_disks
5522 disk
= conf
->disks
+ raid_disk
;
5524 if (test_bit(Replacement
, &rdev
->flags
)) {
5525 if (disk
->replacement
)
5527 disk
->replacement
= rdev
;
5534 if (test_bit(In_sync
, &rdev
->flags
)) {
5535 char b
[BDEVNAME_SIZE
];
5536 printk(KERN_INFO
"md/raid:%s: device %s operational as raid"
5538 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
5539 } else if (rdev
->saved_raid_disk
!= raid_disk
)
5540 /* Cannot rely on bitmap to complete recovery */
5544 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5545 conf
->level
= mddev
->new_level
;
5546 if (conf
->level
== 6)
5547 conf
->max_degraded
= 2;
5549 conf
->max_degraded
= 1;
5550 conf
->algorithm
= mddev
->new_layout
;
5551 conf
->max_nr_stripes
= NR_STRIPES
;
5552 conf
->reshape_progress
= mddev
->reshape_position
;
5553 if (conf
->reshape_progress
!= MaxSector
) {
5554 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
5555 conf
->prev_algo
= mddev
->layout
;
5558 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
5559 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
5560 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
5562 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5563 mdname(mddev
), memory
);
5566 printk(KERN_INFO
"md/raid:%s: allocated %dkB\n",
5567 mdname(mddev
), memory
);
5569 sprintf(pers_name
, "raid%d", mddev
->new_level
);
5570 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
5571 if (!conf
->thread
) {
5573 "md/raid:%s: couldn't allocate thread.\n",
5583 return ERR_PTR(-EIO
);
5585 return ERR_PTR(-ENOMEM
);
5589 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
5592 case ALGORITHM_PARITY_0
:
5593 if (raid_disk
< max_degraded
)
5596 case ALGORITHM_PARITY_N
:
5597 if (raid_disk
>= raid_disks
- max_degraded
)
5600 case ALGORITHM_PARITY_0_6
:
5601 if (raid_disk
== 0 ||
5602 raid_disk
== raid_disks
- 1)
5605 case ALGORITHM_LEFT_ASYMMETRIC_6
:
5606 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
5607 case ALGORITHM_LEFT_SYMMETRIC_6
:
5608 case ALGORITHM_RIGHT_SYMMETRIC_6
:
5609 if (raid_disk
== raid_disks
- 1)
5615 static int run(struct mddev
*mddev
)
5617 struct r5conf
*conf
;
5618 int working_disks
= 0;
5619 int dirty_parity_disks
= 0;
5620 struct md_rdev
*rdev
;
5621 sector_t reshape_offset
= 0;
5623 long long min_offset_diff
= 0;
5626 if (mddev
->recovery_cp
!= MaxSector
)
5627 printk(KERN_NOTICE
"md/raid:%s: not clean"
5628 " -- starting background reconstruction\n",
5631 rdev_for_each(rdev
, mddev
) {
5633 if (rdev
->raid_disk
< 0)
5635 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
5637 min_offset_diff
= diff
;
5639 } else if (mddev
->reshape_backwards
&&
5640 diff
< min_offset_diff
)
5641 min_offset_diff
= diff
;
5642 else if (!mddev
->reshape_backwards
&&
5643 diff
> min_offset_diff
)
5644 min_offset_diff
= diff
;
5647 if (mddev
->reshape_position
!= MaxSector
) {
5648 /* Check that we can continue the reshape.
5649 * Difficulties arise if the stripe we would write to
5650 * next is at or after the stripe we would read from next.
5651 * For a reshape that changes the number of devices, this
5652 * is only possible for a very short time, and mdadm makes
5653 * sure that time appears to have past before assembling
5654 * the array. So we fail if that time hasn't passed.
5655 * For a reshape that keeps the number of devices the same
5656 * mdadm must be monitoring the reshape can keeping the
5657 * critical areas read-only and backed up. It will start
5658 * the array in read-only mode, so we check for that.
5660 sector_t here_new
, here_old
;
5662 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
5664 if (mddev
->new_level
!= mddev
->level
) {
5665 printk(KERN_ERR
"md/raid:%s: unsupported reshape "
5666 "required - aborting.\n",
5670 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
5671 /* reshape_position must be on a new-stripe boundary, and one
5672 * further up in new geometry must map after here in old
5675 here_new
= mddev
->reshape_position
;
5676 if (sector_div(here_new
, mddev
->new_chunk_sectors
*
5677 (mddev
->raid_disks
- max_degraded
))) {
5678 printk(KERN_ERR
"md/raid:%s: reshape_position not "
5679 "on a stripe boundary\n", mdname(mddev
));
5682 reshape_offset
= here_new
* mddev
->new_chunk_sectors
;
5683 /* here_new is the stripe we will write to */
5684 here_old
= mddev
->reshape_position
;
5685 sector_div(here_old
, mddev
->chunk_sectors
*
5686 (old_disks
-max_degraded
));
5687 /* here_old is the first stripe that we might need to read
5689 if (mddev
->delta_disks
== 0) {
5690 if ((here_new
* mddev
->new_chunk_sectors
!=
5691 here_old
* mddev
->chunk_sectors
)) {
5692 printk(KERN_ERR
"md/raid:%s: reshape position is"
5693 " confused - aborting\n", mdname(mddev
));
5696 /* We cannot be sure it is safe to start an in-place
5697 * reshape. It is only safe if user-space is monitoring
5698 * and taking constant backups.
5699 * mdadm always starts a situation like this in
5700 * readonly mode so it can take control before
5701 * allowing any writes. So just check for that.
5703 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
5704 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
5705 /* not really in-place - so OK */;
5706 else if (mddev
->ro
== 0) {
5707 printk(KERN_ERR
"md/raid:%s: in-place reshape "
5708 "must be started in read-only mode "
5713 } else if (mddev
->reshape_backwards
5714 ? (here_new
* mddev
->new_chunk_sectors
+ min_offset_diff
<=
5715 here_old
* mddev
->chunk_sectors
)
5716 : (here_new
* mddev
->new_chunk_sectors
>=
5717 here_old
* mddev
->chunk_sectors
+ (-min_offset_diff
))) {
5718 /* Reading from the same stripe as writing to - bad */
5719 printk(KERN_ERR
"md/raid:%s: reshape_position too early for "
5720 "auto-recovery - aborting.\n",
5724 printk(KERN_INFO
"md/raid:%s: reshape will continue\n",
5726 /* OK, we should be able to continue; */
5728 BUG_ON(mddev
->level
!= mddev
->new_level
);
5729 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
5730 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
5731 BUG_ON(mddev
->delta_disks
!= 0);
5734 if (mddev
->private == NULL
)
5735 conf
= setup_conf(mddev
);
5737 conf
= mddev
->private;
5740 return PTR_ERR(conf
);
5742 conf
->min_offset_diff
= min_offset_diff
;
5743 mddev
->thread
= conf
->thread
;
5744 conf
->thread
= NULL
;
5745 mddev
->private = conf
;
5747 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
5749 rdev
= conf
->disks
[i
].rdev
;
5750 if (!rdev
&& conf
->disks
[i
].replacement
) {
5751 /* The replacement is all we have yet */
5752 rdev
= conf
->disks
[i
].replacement
;
5753 conf
->disks
[i
].replacement
= NULL
;
5754 clear_bit(Replacement
, &rdev
->flags
);
5755 conf
->disks
[i
].rdev
= rdev
;
5759 if (conf
->disks
[i
].replacement
&&
5760 conf
->reshape_progress
!= MaxSector
) {
5761 /* replacements and reshape simply do not mix. */
5762 printk(KERN_ERR
"md: cannot handle concurrent "
5763 "replacement and reshape.\n");
5766 if (test_bit(In_sync
, &rdev
->flags
)) {
5770 /* This disc is not fully in-sync. However if it
5771 * just stored parity (beyond the recovery_offset),
5772 * when we don't need to be concerned about the
5773 * array being dirty.
5774 * When reshape goes 'backwards', we never have
5775 * partially completed devices, so we only need
5776 * to worry about reshape going forwards.
5778 /* Hack because v0.91 doesn't store recovery_offset properly. */
5779 if (mddev
->major_version
== 0 &&
5780 mddev
->minor_version
> 90)
5781 rdev
->recovery_offset
= reshape_offset
;
5783 if (rdev
->recovery_offset
< reshape_offset
) {
5784 /* We need to check old and new layout */
5785 if (!only_parity(rdev
->raid_disk
,
5788 conf
->max_degraded
))
5791 if (!only_parity(rdev
->raid_disk
,
5793 conf
->previous_raid_disks
,
5794 conf
->max_degraded
))
5796 dirty_parity_disks
++;
5800 * 0 for a fully functional array, 1 or 2 for a degraded array.
5802 mddev
->degraded
= calc_degraded(conf
);
5804 if (has_failed(conf
)) {
5805 printk(KERN_ERR
"md/raid:%s: not enough operational devices"
5806 " (%d/%d failed)\n",
5807 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
5811 /* device size must be a multiple of chunk size */
5812 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
5813 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
5815 if (mddev
->degraded
> dirty_parity_disks
&&
5816 mddev
->recovery_cp
!= MaxSector
) {
5817 if (mddev
->ok_start_degraded
)
5819 "md/raid:%s: starting dirty degraded array"
5820 " - data corruption possible.\n",
5824 "md/raid:%s: cannot start dirty degraded array.\n",
5830 if (mddev
->degraded
== 0)
5831 printk(KERN_INFO
"md/raid:%s: raid level %d active with %d out of %d"
5832 " devices, algorithm %d\n", mdname(mddev
), conf
->level
,
5833 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
5836 printk(KERN_ALERT
"md/raid:%s: raid level %d active with %d"
5837 " out of %d devices, algorithm %d\n",
5838 mdname(mddev
), conf
->level
,
5839 mddev
->raid_disks
- mddev
->degraded
,
5840 mddev
->raid_disks
, mddev
->new_layout
);
5842 print_raid5_conf(conf
);
5844 if (conf
->reshape_progress
!= MaxSector
) {
5845 conf
->reshape_safe
= conf
->reshape_progress
;
5846 atomic_set(&conf
->reshape_stripes
, 0);
5847 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
5848 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
5849 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
5850 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
5851 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
5856 /* Ok, everything is just fine now */
5857 if (mddev
->to_remove
== &raid5_attrs_group
)
5858 mddev
->to_remove
= NULL
;
5859 else if (mddev
->kobj
.sd
&&
5860 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
5862 "raid5: failed to create sysfs attributes for %s\n",
5864 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
5868 bool discard_supported
= true;
5869 /* read-ahead size must cover two whole stripes, which
5870 * is 2 * (datadisks) * chunksize where 'n' is the
5871 * number of raid devices
5873 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
5874 int stripe
= data_disks
*
5875 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
5876 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
5877 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
5879 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
5881 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
5882 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
5884 chunk_size
= mddev
->chunk_sectors
<< 9;
5885 blk_queue_io_min(mddev
->queue
, chunk_size
);
5886 blk_queue_io_opt(mddev
->queue
, chunk_size
*
5887 (conf
->raid_disks
- conf
->max_degraded
));
5889 * We can only discard a whole stripe. It doesn't make sense to
5890 * discard data disk but write parity disk
5892 stripe
= stripe
* PAGE_SIZE
;
5893 /* Round up to power of 2, as discard handling
5894 * currently assumes that */
5895 while ((stripe
-1) & stripe
)
5896 stripe
= (stripe
| (stripe
-1)) + 1;
5897 mddev
->queue
->limits
.discard_alignment
= stripe
;
5898 mddev
->queue
->limits
.discard_granularity
= stripe
;
5900 * unaligned part of discard request will be ignored, so can't
5901 * guarantee discard_zerors_data
5903 mddev
->queue
->limits
.discard_zeroes_data
= 0;
5905 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
5907 rdev_for_each(rdev
, mddev
) {
5908 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5909 rdev
->data_offset
<< 9);
5910 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
5911 rdev
->new_data_offset
<< 9);
5913 * discard_zeroes_data is required, otherwise data
5914 * could be lost. Consider a scenario: discard a stripe
5915 * (the stripe could be inconsistent if
5916 * discard_zeroes_data is 0); write one disk of the
5917 * stripe (the stripe could be inconsistent again
5918 * depending on which disks are used to calculate
5919 * parity); the disk is broken; The stripe data of this
5922 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
5923 !bdev_get_queue(rdev
->bdev
)->
5924 limits
.discard_zeroes_data
)
5925 discard_supported
= false;
5928 if (discard_supported
&&
5929 mddev
->queue
->limits
.max_discard_sectors
>= stripe
&&
5930 mddev
->queue
->limits
.discard_granularity
>= stripe
)
5931 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
5934 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
5940 md_unregister_thread(&mddev
->thread
);
5941 print_raid5_conf(conf
);
5943 mddev
->private = NULL
;
5944 printk(KERN_ALERT
"md/raid:%s: failed to run raid set.\n", mdname(mddev
));
5948 static int stop(struct mddev
*mddev
)
5950 struct r5conf
*conf
= mddev
->private;
5952 md_unregister_thread(&mddev
->thread
);
5954 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
5956 mddev
->private = NULL
;
5957 mddev
->to_remove
= &raid5_attrs_group
;
5961 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
5963 struct r5conf
*conf
= mddev
->private;
5966 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
5967 mddev
->chunk_sectors
/ 2, mddev
->layout
);
5968 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
5969 for (i
= 0; i
< conf
->raid_disks
; i
++)
5970 seq_printf (seq
, "%s",
5971 conf
->disks
[i
].rdev
&&
5972 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
5973 seq_printf (seq
, "]");
5976 static void print_raid5_conf (struct r5conf
*conf
)
5979 struct disk_info
*tmp
;
5981 printk(KERN_DEBUG
"RAID conf printout:\n");
5983 printk("(conf==NULL)\n");
5986 printk(KERN_DEBUG
" --- level:%d rd:%d wd:%d\n", conf
->level
,
5988 conf
->raid_disks
- conf
->mddev
->degraded
);
5990 for (i
= 0; i
< conf
->raid_disks
; i
++) {
5991 char b
[BDEVNAME_SIZE
];
5992 tmp
= conf
->disks
+ i
;
5994 printk(KERN_DEBUG
" disk %d, o:%d, dev:%s\n",
5995 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
5996 bdevname(tmp
->rdev
->bdev
, b
));
6000 static int raid5_spare_active(struct mddev
*mddev
)
6003 struct r5conf
*conf
= mddev
->private;
6004 struct disk_info
*tmp
;
6006 unsigned long flags
;
6008 for (i
= 0; i
< conf
->raid_disks
; i
++) {
6009 tmp
= conf
->disks
+ i
;
6010 if (tmp
->replacement
6011 && tmp
->replacement
->recovery_offset
== MaxSector
6012 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
6013 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
6014 /* Replacement has just become active. */
6016 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
6019 /* Replaced device not technically faulty,
6020 * but we need to be sure it gets removed
6021 * and never re-added.
6023 set_bit(Faulty
, &tmp
->rdev
->flags
);
6024 sysfs_notify_dirent_safe(
6025 tmp
->rdev
->sysfs_state
);
6027 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
6028 } else if (tmp
->rdev
6029 && tmp
->rdev
->recovery_offset
== MaxSector
6030 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
6031 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
6033 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
6036 spin_lock_irqsave(&conf
->device_lock
, flags
);
6037 mddev
->degraded
= calc_degraded(conf
);
6038 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
6039 print_raid5_conf(conf
);
6043 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
6045 struct r5conf
*conf
= mddev
->private;
6047 int number
= rdev
->raid_disk
;
6048 struct md_rdev
**rdevp
;
6049 struct disk_info
*p
= conf
->disks
+ number
;
6051 print_raid5_conf(conf
);
6052 if (rdev
== p
->rdev
)
6054 else if (rdev
== p
->replacement
)
6055 rdevp
= &p
->replacement
;
6059 if (number
>= conf
->raid_disks
&&
6060 conf
->reshape_progress
== MaxSector
)
6061 clear_bit(In_sync
, &rdev
->flags
);
6063 if (test_bit(In_sync
, &rdev
->flags
) ||
6064 atomic_read(&rdev
->nr_pending
)) {
6068 /* Only remove non-faulty devices if recovery
6071 if (!test_bit(Faulty
, &rdev
->flags
) &&
6072 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
6073 !has_failed(conf
) &&
6074 (!p
->replacement
|| p
->replacement
== rdev
) &&
6075 number
< conf
->raid_disks
) {
6081 if (atomic_read(&rdev
->nr_pending
)) {
6082 /* lost the race, try later */
6085 } else if (p
->replacement
) {
6086 /* We must have just cleared 'rdev' */
6087 p
->rdev
= p
->replacement
;
6088 clear_bit(Replacement
, &p
->replacement
->flags
);
6089 smp_mb(); /* Make sure other CPUs may see both as identical
6090 * but will never see neither - if they are careful
6092 p
->replacement
= NULL
;
6093 clear_bit(WantReplacement
, &rdev
->flags
);
6095 /* We might have just removed the Replacement as faulty-
6096 * clear the bit just in case
6098 clear_bit(WantReplacement
, &rdev
->flags
);
6101 print_raid5_conf(conf
);
6105 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
6107 struct r5conf
*conf
= mddev
->private;
6110 struct disk_info
*p
;
6112 int last
= conf
->raid_disks
- 1;
6114 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
6117 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
6118 /* no point adding a device */
6121 if (rdev
->raid_disk
>= 0)
6122 first
= last
= rdev
->raid_disk
;
6125 * find the disk ... but prefer rdev->saved_raid_disk
6128 if (rdev
->saved_raid_disk
>= 0 &&
6129 rdev
->saved_raid_disk
>= first
&&
6130 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
6131 first
= rdev
->saved_raid_disk
;
6133 for (disk
= first
; disk
<= last
; disk
++) {
6134 p
= conf
->disks
+ disk
;
6135 if (p
->rdev
== NULL
) {
6136 clear_bit(In_sync
, &rdev
->flags
);
6137 rdev
->raid_disk
= disk
;
6139 if (rdev
->saved_raid_disk
!= disk
)
6141 rcu_assign_pointer(p
->rdev
, rdev
);
6145 for (disk
= first
; disk
<= last
; disk
++) {
6146 p
= conf
->disks
+ disk
;
6147 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
6148 p
->replacement
== NULL
) {
6149 clear_bit(In_sync
, &rdev
->flags
);
6150 set_bit(Replacement
, &rdev
->flags
);
6151 rdev
->raid_disk
= disk
;
6154 rcu_assign_pointer(p
->replacement
, rdev
);
6159 print_raid5_conf(conf
);
6163 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
6165 /* no resync is happening, and there is enough space
6166 * on all devices, so we can resize.
6167 * We need to make sure resync covers any new space.
6168 * If the array is shrinking we should possibly wait until
6169 * any io in the removed space completes, but it hardly seems
6173 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
6174 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
6175 if (mddev
->external_size
&&
6176 mddev
->array_sectors
> newsize
)
6178 if (mddev
->bitmap
) {
6179 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
6183 md_set_array_sectors(mddev
, newsize
);
6184 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6185 revalidate_disk(mddev
->gendisk
);
6186 if (sectors
> mddev
->dev_sectors
&&
6187 mddev
->recovery_cp
> mddev
->dev_sectors
) {
6188 mddev
->recovery_cp
= mddev
->dev_sectors
;
6189 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6191 mddev
->dev_sectors
= sectors
;
6192 mddev
->resync_max_sectors
= sectors
;
6196 static int check_stripe_cache(struct mddev
*mddev
)
6198 /* Can only proceed if there are plenty of stripe_heads.
6199 * We need a minimum of one full stripe,, and for sensible progress
6200 * it is best to have about 4 times that.
6201 * If we require 4 times, then the default 256 4K stripe_heads will
6202 * allow for chunk sizes up to 256K, which is probably OK.
6203 * If the chunk size is greater, user-space should request more
6204 * stripe_heads first.
6206 struct r5conf
*conf
= mddev
->private;
6207 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
6208 > conf
->max_nr_stripes
||
6209 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
6210 > conf
->max_nr_stripes
) {
6211 printk(KERN_WARNING
"md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6213 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
6220 static int check_reshape(struct mddev
*mddev
)
6222 struct r5conf
*conf
= mddev
->private;
6224 if (mddev
->delta_disks
== 0 &&
6225 mddev
->new_layout
== mddev
->layout
&&
6226 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
6227 return 0; /* nothing to do */
6228 if (has_failed(conf
))
6230 if (mddev
->delta_disks
< 0 && mddev
->reshape_position
== MaxSector
) {
6231 /* We might be able to shrink, but the devices must
6232 * be made bigger first.
6233 * For raid6, 4 is the minimum size.
6234 * Otherwise 2 is the minimum
6237 if (mddev
->level
== 6)
6239 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
6243 if (!check_stripe_cache(mddev
))
6246 return resize_stripes(conf
, (conf
->previous_raid_disks
6247 + mddev
->delta_disks
));
6250 static int raid5_start_reshape(struct mddev
*mddev
)
6252 struct r5conf
*conf
= mddev
->private;
6253 struct md_rdev
*rdev
;
6255 unsigned long flags
;
6257 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6260 if (!check_stripe_cache(mddev
))
6263 if (has_failed(conf
))
6266 rdev_for_each(rdev
, mddev
) {
6267 if (!test_bit(In_sync
, &rdev
->flags
)
6268 && !test_bit(Faulty
, &rdev
->flags
))
6272 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
6273 /* Not enough devices even to make a degraded array
6278 /* Refuse to reduce size of the array. Any reductions in
6279 * array size must be through explicit setting of array_size
6282 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
6283 < mddev
->array_sectors
) {
6284 printk(KERN_ERR
"md/raid:%s: array size must be reduced "
6285 "before number of disks\n", mdname(mddev
));
6289 atomic_set(&conf
->reshape_stripes
, 0);
6290 spin_lock_irq(&conf
->device_lock
);
6291 write_seqcount_begin(&conf
->gen_lock
);
6292 conf
->previous_raid_disks
= conf
->raid_disks
;
6293 conf
->raid_disks
+= mddev
->delta_disks
;
6294 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
6295 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
6296 conf
->prev_algo
= conf
->algorithm
;
6297 conf
->algorithm
= mddev
->new_layout
;
6299 /* Code that selects data_offset needs to see the generation update
6300 * if reshape_progress has been set - so a memory barrier needed.
6303 if (mddev
->reshape_backwards
)
6304 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
6306 conf
->reshape_progress
= 0;
6307 conf
->reshape_safe
= conf
->reshape_progress
;
6308 write_seqcount_end(&conf
->gen_lock
);
6309 spin_unlock_irq(&conf
->device_lock
);
6311 /* Now make sure any requests that proceeded on the assumption
6312 * the reshape wasn't running - like Discard or Read - have
6315 mddev_suspend(mddev
);
6316 mddev_resume(mddev
);
6318 /* Add some new drives, as many as will fit.
6319 * We know there are enough to make the newly sized array work.
6320 * Don't add devices if we are reducing the number of
6321 * devices in the array. This is because it is not possible
6322 * to correctly record the "partially reconstructed" state of
6323 * such devices during the reshape and confusion could result.
6325 if (mddev
->delta_disks
>= 0) {
6326 rdev_for_each(rdev
, mddev
)
6327 if (rdev
->raid_disk
< 0 &&
6328 !test_bit(Faulty
, &rdev
->flags
)) {
6329 if (raid5_add_disk(mddev
, rdev
) == 0) {
6331 >= conf
->previous_raid_disks
)
6332 set_bit(In_sync
, &rdev
->flags
);
6334 rdev
->recovery_offset
= 0;
6336 if (sysfs_link_rdev(mddev
, rdev
))
6337 /* Failure here is OK */;
6339 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
6340 && !test_bit(Faulty
, &rdev
->flags
)) {
6341 /* This is a spare that was manually added */
6342 set_bit(In_sync
, &rdev
->flags
);
6345 /* When a reshape changes the number of devices,
6346 * ->degraded is measured against the larger of the
6347 * pre and post number of devices.
6349 spin_lock_irqsave(&conf
->device_lock
, flags
);
6350 mddev
->degraded
= calc_degraded(conf
);
6351 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
6353 mddev
->raid_disks
= conf
->raid_disks
;
6354 mddev
->reshape_position
= conf
->reshape_progress
;
6355 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6357 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6358 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6359 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6360 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6361 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
6363 if (!mddev
->sync_thread
) {
6364 mddev
->recovery
= 0;
6365 spin_lock_irq(&conf
->device_lock
);
6366 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
6367 rdev_for_each(rdev
, mddev
)
6368 rdev
->new_data_offset
= rdev
->data_offset
;
6370 conf
->reshape_progress
= MaxSector
;
6371 mddev
->reshape_position
= MaxSector
;
6372 spin_unlock_irq(&conf
->device_lock
);
6375 conf
->reshape_checkpoint
= jiffies
;
6376 md_wakeup_thread(mddev
->sync_thread
);
6377 md_new_event(mddev
);
6381 /* This is called from the reshape thread and should make any
6382 * changes needed in 'conf'
6384 static void end_reshape(struct r5conf
*conf
)
6387 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
6388 struct md_rdev
*rdev
;
6390 spin_lock_irq(&conf
->device_lock
);
6391 conf
->previous_raid_disks
= conf
->raid_disks
;
6392 rdev_for_each(rdev
, conf
->mddev
)
6393 rdev
->data_offset
= rdev
->new_data_offset
;
6395 conf
->reshape_progress
= MaxSector
;
6396 spin_unlock_irq(&conf
->device_lock
);
6397 wake_up(&conf
->wait_for_overlap
);
6399 /* read-ahead size must cover two whole stripes, which is
6400 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6402 if (conf
->mddev
->queue
) {
6403 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
6404 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
6406 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
6407 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
6412 /* This is called from the raid5d thread with mddev_lock held.
6413 * It makes config changes to the device.
6415 static void raid5_finish_reshape(struct mddev
*mddev
)
6417 struct r5conf
*conf
= mddev
->private;
6419 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6421 if (mddev
->delta_disks
> 0) {
6422 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
6423 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6424 revalidate_disk(mddev
->gendisk
);
6427 spin_lock_irq(&conf
->device_lock
);
6428 mddev
->degraded
= calc_degraded(conf
);
6429 spin_unlock_irq(&conf
->device_lock
);
6430 for (d
= conf
->raid_disks
;
6431 d
< conf
->raid_disks
- mddev
->delta_disks
;
6433 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
6435 clear_bit(In_sync
, &rdev
->flags
);
6436 rdev
= conf
->disks
[d
].replacement
;
6438 clear_bit(In_sync
, &rdev
->flags
);
6441 mddev
->layout
= conf
->algorithm
;
6442 mddev
->chunk_sectors
= conf
->chunk_sectors
;
6443 mddev
->reshape_position
= MaxSector
;
6444 mddev
->delta_disks
= 0;
6445 mddev
->reshape_backwards
= 0;
6449 static void raid5_quiesce(struct mddev
*mddev
, int state
)
6451 struct r5conf
*conf
= mddev
->private;
6454 case 2: /* resume for a suspend */
6455 wake_up(&conf
->wait_for_overlap
);
6458 case 1: /* stop all writes */
6459 spin_lock_irq(&conf
->device_lock
);
6460 /* '2' tells resync/reshape to pause so that all
6461 * active stripes can drain
6464 wait_event_lock_irq(conf
->wait_for_stripe
,
6465 atomic_read(&conf
->active_stripes
) == 0 &&
6466 atomic_read(&conf
->active_aligned_reads
) == 0,
6469 spin_unlock_irq(&conf
->device_lock
);
6470 /* allow reshape to continue */
6471 wake_up(&conf
->wait_for_overlap
);
6474 case 0: /* re-enable writes */
6475 spin_lock_irq(&conf
->device_lock
);
6477 wake_up(&conf
->wait_for_stripe
);
6478 wake_up(&conf
->wait_for_overlap
);
6479 spin_unlock_irq(&conf
->device_lock
);
6485 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
6487 struct r0conf
*raid0_conf
= mddev
->private;
6490 /* for raid0 takeover only one zone is supported */
6491 if (raid0_conf
->nr_strip_zones
> 1) {
6492 printk(KERN_ERR
"md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6494 return ERR_PTR(-EINVAL
);
6497 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
6498 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
6499 mddev
->dev_sectors
= sectors
;
6500 mddev
->new_level
= level
;
6501 mddev
->new_layout
= ALGORITHM_PARITY_N
;
6502 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6503 mddev
->raid_disks
+= 1;
6504 mddev
->delta_disks
= 1;
6505 /* make sure it will be not marked as dirty */
6506 mddev
->recovery_cp
= MaxSector
;
6508 return setup_conf(mddev
);
6512 static void *raid5_takeover_raid1(struct mddev
*mddev
)
6516 if (mddev
->raid_disks
!= 2 ||
6517 mddev
->degraded
> 1)
6518 return ERR_PTR(-EINVAL
);
6520 /* Should check if there are write-behind devices? */
6522 chunksect
= 64*2; /* 64K by default */
6524 /* The array must be an exact multiple of chunksize */
6525 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
6528 if ((chunksect
<<9) < STRIPE_SIZE
)
6529 /* array size does not allow a suitable chunk size */
6530 return ERR_PTR(-EINVAL
);
6532 mddev
->new_level
= 5;
6533 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
6534 mddev
->new_chunk_sectors
= chunksect
;
6536 return setup_conf(mddev
);
6539 static void *raid5_takeover_raid6(struct mddev
*mddev
)
6543 switch (mddev
->layout
) {
6544 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6545 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
6547 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6548 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
6550 case ALGORITHM_LEFT_SYMMETRIC_6
:
6551 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
6553 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6554 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
6556 case ALGORITHM_PARITY_0_6
:
6557 new_layout
= ALGORITHM_PARITY_0
;
6559 case ALGORITHM_PARITY_N
:
6560 new_layout
= ALGORITHM_PARITY_N
;
6563 return ERR_PTR(-EINVAL
);
6565 mddev
->new_level
= 5;
6566 mddev
->new_layout
= new_layout
;
6567 mddev
->delta_disks
= -1;
6568 mddev
->raid_disks
-= 1;
6569 return setup_conf(mddev
);
6573 static int raid5_check_reshape(struct mddev
*mddev
)
6575 /* For a 2-drive array, the layout and chunk size can be changed
6576 * immediately as not restriping is needed.
6577 * For larger arrays we record the new value - after validation
6578 * to be used by a reshape pass.
6580 struct r5conf
*conf
= mddev
->private;
6581 int new_chunk
= mddev
->new_chunk_sectors
;
6583 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
6585 if (new_chunk
> 0) {
6586 if (!is_power_of_2(new_chunk
))
6588 if (new_chunk
< (PAGE_SIZE
>>9))
6590 if (mddev
->array_sectors
& (new_chunk
-1))
6591 /* not factor of array size */
6595 /* They look valid */
6597 if (mddev
->raid_disks
== 2) {
6598 /* can make the change immediately */
6599 if (mddev
->new_layout
>= 0) {
6600 conf
->algorithm
= mddev
->new_layout
;
6601 mddev
->layout
= mddev
->new_layout
;
6603 if (new_chunk
> 0) {
6604 conf
->chunk_sectors
= new_chunk
;
6605 mddev
->chunk_sectors
= new_chunk
;
6607 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6608 md_wakeup_thread(mddev
->thread
);
6610 return check_reshape(mddev
);
6613 static int raid6_check_reshape(struct mddev
*mddev
)
6615 int new_chunk
= mddev
->new_chunk_sectors
;
6617 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
6619 if (new_chunk
> 0) {
6620 if (!is_power_of_2(new_chunk
))
6622 if (new_chunk
< (PAGE_SIZE
>> 9))
6624 if (mddev
->array_sectors
& (new_chunk
-1))
6625 /* not factor of array size */
6629 /* They look valid */
6630 return check_reshape(mddev
);
6633 static void *raid5_takeover(struct mddev
*mddev
)
6635 /* raid5 can take over:
6636 * raid0 - if there is only one strip zone - make it a raid4 layout
6637 * raid1 - if there are two drives. We need to know the chunk size
6638 * raid4 - trivial - just use a raid4 layout.
6639 * raid6 - Providing it is a *_6 layout
6641 if (mddev
->level
== 0)
6642 return raid45_takeover_raid0(mddev
, 5);
6643 if (mddev
->level
== 1)
6644 return raid5_takeover_raid1(mddev
);
6645 if (mddev
->level
== 4) {
6646 mddev
->new_layout
= ALGORITHM_PARITY_N
;
6647 mddev
->new_level
= 5;
6648 return setup_conf(mddev
);
6650 if (mddev
->level
== 6)
6651 return raid5_takeover_raid6(mddev
);
6653 return ERR_PTR(-EINVAL
);
6656 static void *raid4_takeover(struct mddev
*mddev
)
6658 /* raid4 can take over:
6659 * raid0 - if there is only one strip zone
6660 * raid5 - if layout is right
6662 if (mddev
->level
== 0)
6663 return raid45_takeover_raid0(mddev
, 4);
6664 if (mddev
->level
== 5 &&
6665 mddev
->layout
== ALGORITHM_PARITY_N
) {
6666 mddev
->new_layout
= 0;
6667 mddev
->new_level
= 4;
6668 return setup_conf(mddev
);
6670 return ERR_PTR(-EINVAL
);
6673 static struct md_personality raid5_personality
;
6675 static void *raid6_takeover(struct mddev
*mddev
)
6677 /* Currently can only take over a raid5. We map the
6678 * personality to an equivalent raid6 personality
6679 * with the Q block at the end.
6683 if (mddev
->pers
!= &raid5_personality
)
6684 return ERR_PTR(-EINVAL
);
6685 if (mddev
->degraded
> 1)
6686 return ERR_PTR(-EINVAL
);
6687 if (mddev
->raid_disks
> 253)
6688 return ERR_PTR(-EINVAL
);
6689 if (mddev
->raid_disks
< 3)
6690 return ERR_PTR(-EINVAL
);
6692 switch (mddev
->layout
) {
6693 case ALGORITHM_LEFT_ASYMMETRIC
:
6694 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
6696 case ALGORITHM_RIGHT_ASYMMETRIC
:
6697 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
6699 case ALGORITHM_LEFT_SYMMETRIC
:
6700 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
6702 case ALGORITHM_RIGHT_SYMMETRIC
:
6703 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
6705 case ALGORITHM_PARITY_0
:
6706 new_layout
= ALGORITHM_PARITY_0_6
;
6708 case ALGORITHM_PARITY_N
:
6709 new_layout
= ALGORITHM_PARITY_N
;
6712 return ERR_PTR(-EINVAL
);
6714 mddev
->new_level
= 6;
6715 mddev
->new_layout
= new_layout
;
6716 mddev
->delta_disks
= 1;
6717 mddev
->raid_disks
+= 1;
6718 return setup_conf(mddev
);
6722 static struct md_personality raid6_personality
=
6726 .owner
= THIS_MODULE
,
6727 .make_request
= make_request
,
6731 .error_handler
= error
,
6732 .hot_add_disk
= raid5_add_disk
,
6733 .hot_remove_disk
= raid5_remove_disk
,
6734 .spare_active
= raid5_spare_active
,
6735 .sync_request
= sync_request
,
6736 .resize
= raid5_resize
,
6738 .check_reshape
= raid6_check_reshape
,
6739 .start_reshape
= raid5_start_reshape
,
6740 .finish_reshape
= raid5_finish_reshape
,
6741 .quiesce
= raid5_quiesce
,
6742 .takeover
= raid6_takeover
,
6744 static struct md_personality raid5_personality
=
6748 .owner
= THIS_MODULE
,
6749 .make_request
= make_request
,
6753 .error_handler
= error
,
6754 .hot_add_disk
= raid5_add_disk
,
6755 .hot_remove_disk
= raid5_remove_disk
,
6756 .spare_active
= raid5_spare_active
,
6757 .sync_request
= sync_request
,
6758 .resize
= raid5_resize
,
6760 .check_reshape
= raid5_check_reshape
,
6761 .start_reshape
= raid5_start_reshape
,
6762 .finish_reshape
= raid5_finish_reshape
,
6763 .quiesce
= raid5_quiesce
,
6764 .takeover
= raid5_takeover
,
6767 static struct md_personality raid4_personality
=
6771 .owner
= THIS_MODULE
,
6772 .make_request
= make_request
,
6776 .error_handler
= error
,
6777 .hot_add_disk
= raid5_add_disk
,
6778 .hot_remove_disk
= raid5_remove_disk
,
6779 .spare_active
= raid5_spare_active
,
6780 .sync_request
= sync_request
,
6781 .resize
= raid5_resize
,
6783 .check_reshape
= raid5_check_reshape
,
6784 .start_reshape
= raid5_start_reshape
,
6785 .finish_reshape
= raid5_finish_reshape
,
6786 .quiesce
= raid5_quiesce
,
6787 .takeover
= raid4_takeover
,
6790 static int __init
raid5_init(void)
6792 raid5_wq
= alloc_workqueue("raid5wq",
6793 WQ_UNBOUND
|WQ_MEM_RECLAIM
|WQ_CPU_INTENSIVE
|WQ_SYSFS
, 0);
6796 register_md_personality(&raid6_personality
);
6797 register_md_personality(&raid5_personality
);
6798 register_md_personality(&raid4_personality
);
6802 static void raid5_exit(void)
6804 unregister_md_personality(&raid6_personality
);
6805 unregister_md_personality(&raid5_personality
);
6806 unregister_md_personality(&raid4_personality
);
6807 destroy_workqueue(raid5_wq
);
6810 module_init(raid5_init
);
6811 module_exit(raid5_exit
);
6812 MODULE_LICENSE("GPL");
6813 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
6814 MODULE_ALIAS("md-personality-4"); /* RAID5 */
6815 MODULE_ALIAS("md-raid5");
6816 MODULE_ALIAS("md-raid4");
6817 MODULE_ALIAS("md-level-5");
6818 MODULE_ALIAS("md-level-4");
6819 MODULE_ALIAS("md-personality-8"); /* RAID6 */
6820 MODULE_ALIAS("md-raid6");
6821 MODULE_ALIAS("md-level-6");
6823 /* This used to be two separate modules, they were: */
6824 MODULE_ALIAS("raid5");
6825 MODULE_ALIAS("raid6");