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 static inline int stripe_hash_locks_hash(sector_t sect
)
90 return (sect
>> STRIPE_SHIFT
) & STRIPE_HASH_LOCKS_MASK
;
93 static inline void lock_device_hash_lock(struct r5conf
*conf
, int hash
)
95 spin_lock_irq(conf
->hash_locks
+ hash
);
96 spin_lock(&conf
->device_lock
);
99 static inline void unlock_device_hash_lock(struct r5conf
*conf
, int hash
)
101 spin_unlock(&conf
->device_lock
);
102 spin_unlock_irq(conf
->hash_locks
+ hash
);
105 static inline void lock_all_device_hash_locks_irq(struct r5conf
*conf
)
109 spin_lock(conf
->hash_locks
);
110 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
111 spin_lock_nest_lock(conf
->hash_locks
+ i
, conf
->hash_locks
);
112 spin_lock(&conf
->device_lock
);
115 static inline void unlock_all_device_hash_locks_irq(struct r5conf
*conf
)
118 spin_unlock(&conf
->device_lock
);
119 for (i
= NR_STRIPE_HASH_LOCKS
; i
; i
--)
120 spin_unlock(conf
->hash_locks
+ i
- 1);
124 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
125 * order without overlap. There may be several bio's per stripe+device, and
126 * a bio could span several devices.
127 * When walking this list for a particular stripe+device, we must never proceed
128 * beyond a bio that extends past this device, as the next bio might no longer
130 * This function is used to determine the 'next' bio in the list, given the sector
131 * of the current stripe+device
133 static inline struct bio
*r5_next_bio(struct bio
*bio
, sector_t sector
)
135 int sectors
= bio_sectors(bio
);
136 if (bio
->bi_sector
+ sectors
< sector
+ STRIPE_SECTORS
)
143 * We maintain a biased count of active stripes in the bottom 16 bits of
144 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
146 static inline int raid5_bi_processed_stripes(struct bio
*bio
)
148 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
149 return (atomic_read(segments
) >> 16) & 0xffff;
152 static inline int raid5_dec_bi_active_stripes(struct bio
*bio
)
154 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
155 return atomic_sub_return(1, segments
) & 0xffff;
158 static inline void raid5_inc_bi_active_stripes(struct bio
*bio
)
160 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
161 atomic_inc(segments
);
164 static inline void raid5_set_bi_processed_stripes(struct bio
*bio
,
167 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
171 old
= atomic_read(segments
);
172 new = (old
& 0xffff) | (cnt
<< 16);
173 } while (atomic_cmpxchg(segments
, old
, new) != old
);
176 static inline void raid5_set_bi_stripes(struct bio
*bio
, unsigned int cnt
)
178 atomic_t
*segments
= (atomic_t
*)&bio
->bi_phys_segments
;
179 atomic_set(segments
, cnt
);
182 /* Find first data disk in a raid6 stripe */
183 static inline int raid6_d0(struct stripe_head
*sh
)
186 /* ddf always start from first device */
188 /* md starts just after Q block */
189 if (sh
->qd_idx
== sh
->disks
- 1)
192 return sh
->qd_idx
+ 1;
194 static inline int raid6_next_disk(int disk
, int raid_disks
)
197 return (disk
< raid_disks
) ? disk
: 0;
200 /* When walking through the disks in a raid5, starting at raid6_d0,
201 * We need to map each disk to a 'slot', where the data disks are slot
202 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
203 * is raid_disks-1. This help does that mapping.
205 static int raid6_idx_to_slot(int idx
, struct stripe_head
*sh
,
206 int *count
, int syndrome_disks
)
212 if (idx
== sh
->pd_idx
)
213 return syndrome_disks
;
214 if (idx
== sh
->qd_idx
)
215 return syndrome_disks
+ 1;
221 static void return_io(struct bio
*return_bi
)
223 struct bio
*bi
= return_bi
;
226 return_bi
= bi
->bi_next
;
229 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
236 static void print_raid5_conf (struct r5conf
*conf
);
238 static int stripe_operations_active(struct stripe_head
*sh
)
240 return sh
->check_state
|| sh
->reconstruct_state
||
241 test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
) ||
242 test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
245 static void raid5_wakeup_stripe_thread(struct stripe_head
*sh
)
247 struct r5conf
*conf
= sh
->raid_conf
;
248 struct r5worker_group
*group
;
250 int i
, cpu
= sh
->cpu
;
252 if (!cpu_online(cpu
)) {
253 cpu
= cpumask_any(cpu_online_mask
);
257 if (list_empty(&sh
->lru
)) {
258 struct r5worker_group
*group
;
259 group
= conf
->worker_groups
+ cpu_to_group(cpu
);
260 list_add_tail(&sh
->lru
, &group
->handle_list
);
261 group
->stripes_cnt
++;
265 if (conf
->worker_cnt_per_group
== 0) {
266 md_wakeup_thread(conf
->mddev
->thread
);
270 group
= conf
->worker_groups
+ cpu_to_group(sh
->cpu
);
272 group
->workers
[0].working
= true;
273 /* at least one worker should run to avoid race */
274 queue_work_on(sh
->cpu
, raid5_wq
, &group
->workers
[0].work
);
276 thread_cnt
= group
->stripes_cnt
/ MAX_STRIPE_BATCH
- 1;
277 /* wakeup more workers */
278 for (i
= 1; i
< conf
->worker_cnt_per_group
&& thread_cnt
> 0; i
++) {
279 if (group
->workers
[i
].working
== false) {
280 group
->workers
[i
].working
= true;
281 queue_work_on(sh
->cpu
, raid5_wq
,
282 &group
->workers
[i
].work
);
288 static void do_release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
289 struct list_head
*temp_inactive_list
)
291 BUG_ON(!list_empty(&sh
->lru
));
292 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
293 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
294 if (test_bit(STRIPE_DELAYED
, &sh
->state
) &&
295 !test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
296 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
297 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
298 sh
->bm_seq
- conf
->seq_write
> 0)
299 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
301 clear_bit(STRIPE_DELAYED
, &sh
->state
);
302 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
303 if (conf
->worker_cnt_per_group
== 0) {
304 list_add_tail(&sh
->lru
, &conf
->handle_list
);
306 raid5_wakeup_stripe_thread(sh
);
310 md_wakeup_thread(conf
->mddev
->thread
);
312 BUG_ON(stripe_operations_active(sh
));
313 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
314 if (atomic_dec_return(&conf
->preread_active_stripes
)
316 md_wakeup_thread(conf
->mddev
->thread
);
317 atomic_dec(&conf
->active_stripes
);
318 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
))
319 list_add_tail(&sh
->lru
, temp_inactive_list
);
323 static void __release_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
324 struct list_head
*temp_inactive_list
)
326 if (atomic_dec_and_test(&sh
->count
))
327 do_release_stripe(conf
, sh
, temp_inactive_list
);
331 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
333 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
334 * given time. Adding stripes only takes device lock, while deleting stripes
335 * only takes hash lock.
337 static void release_inactive_stripe_list(struct r5conf
*conf
,
338 struct list_head
*temp_inactive_list
,
342 bool do_wakeup
= false;
345 if (hash
== NR_STRIPE_HASH_LOCKS
) {
346 size
= NR_STRIPE_HASH_LOCKS
;
347 hash
= NR_STRIPE_HASH_LOCKS
- 1;
351 struct list_head
*list
= &temp_inactive_list
[size
- 1];
354 * We don't hold any lock here yet, get_active_stripe() might
355 * remove stripes from the list
357 if (!list_empty_careful(list
)) {
358 spin_lock_irqsave(conf
->hash_locks
+ hash
, flags
);
359 if (list_empty(conf
->inactive_list
+ hash
) &&
361 atomic_dec(&conf
->empty_inactive_list_nr
);
362 list_splice_tail_init(list
, conf
->inactive_list
+ hash
);
364 spin_unlock_irqrestore(conf
->hash_locks
+ hash
, flags
);
371 wake_up(&conf
->wait_for_stripe
);
372 if (conf
->retry_read_aligned
)
373 md_wakeup_thread(conf
->mddev
->thread
);
377 /* should hold conf->device_lock already */
378 static int release_stripe_list(struct r5conf
*conf
,
379 struct list_head
*temp_inactive_list
)
381 struct stripe_head
*sh
;
383 struct llist_node
*head
;
385 head
= llist_del_all(&conf
->released_stripes
);
386 head
= llist_reverse_order(head
);
390 sh
= llist_entry(head
, struct stripe_head
, release_list
);
391 head
= llist_next(head
);
392 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
394 clear_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
);
396 * Don't worry the bit is set here, because if the bit is set
397 * again, the count is always > 1. This is true for
398 * STRIPE_ON_UNPLUG_LIST bit too.
400 hash
= sh
->hash_lock_index
;
401 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
408 static void release_stripe(struct stripe_head
*sh
)
410 struct r5conf
*conf
= sh
->raid_conf
;
412 struct list_head list
;
416 if (unlikely(!conf
->mddev
->thread
) ||
417 test_and_set_bit(STRIPE_ON_RELEASE_LIST
, &sh
->state
))
419 wakeup
= llist_add(&sh
->release_list
, &conf
->released_stripes
);
421 md_wakeup_thread(conf
->mddev
->thread
);
424 local_irq_save(flags
);
425 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
426 if (atomic_dec_and_lock(&sh
->count
, &conf
->device_lock
)) {
427 INIT_LIST_HEAD(&list
);
428 hash
= sh
->hash_lock_index
;
429 do_release_stripe(conf
, sh
, &list
);
430 spin_unlock(&conf
->device_lock
);
431 release_inactive_stripe_list(conf
, &list
, hash
);
433 local_irq_restore(flags
);
436 static inline void remove_hash(struct stripe_head
*sh
)
438 pr_debug("remove_hash(), stripe %llu\n",
439 (unsigned long long)sh
->sector
);
441 hlist_del_init(&sh
->hash
);
444 static inline void insert_hash(struct r5conf
*conf
, struct stripe_head
*sh
)
446 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
448 pr_debug("insert_hash(), stripe %llu\n",
449 (unsigned long long)sh
->sector
);
451 hlist_add_head(&sh
->hash
, hp
);
455 /* find an idle stripe, make sure it is unhashed, and return it. */
456 static struct stripe_head
*get_free_stripe(struct r5conf
*conf
, int hash
)
458 struct stripe_head
*sh
= NULL
;
459 struct list_head
*first
;
461 if (list_empty(conf
->inactive_list
+ hash
))
463 first
= (conf
->inactive_list
+ hash
)->next
;
464 sh
= list_entry(first
, struct stripe_head
, lru
);
465 list_del_init(first
);
467 atomic_inc(&conf
->active_stripes
);
468 BUG_ON(hash
!= sh
->hash_lock_index
);
469 if (list_empty(conf
->inactive_list
+ hash
))
470 atomic_inc(&conf
->empty_inactive_list_nr
);
475 static void shrink_buffers(struct stripe_head
*sh
)
479 int num
= sh
->raid_conf
->pool_size
;
481 for (i
= 0; i
< num
; i
++) {
485 sh
->dev
[i
].page
= NULL
;
490 static int grow_buffers(struct stripe_head
*sh
)
493 int num
= sh
->raid_conf
->pool_size
;
495 for (i
= 0; i
< num
; i
++) {
498 if (!(page
= alloc_page(GFP_KERNEL
))) {
501 sh
->dev
[i
].page
= page
;
506 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
);
507 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
508 struct stripe_head
*sh
);
510 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int previous
)
512 struct r5conf
*conf
= sh
->raid_conf
;
515 BUG_ON(atomic_read(&sh
->count
) != 0);
516 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
517 BUG_ON(stripe_operations_active(sh
));
519 pr_debug("init_stripe called, stripe %llu\n",
520 (unsigned long long)sh
->sector
);
524 seq
= read_seqcount_begin(&conf
->gen_lock
);
525 sh
->generation
= conf
->generation
- previous
;
526 sh
->disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
528 stripe_set_idx(sector
, conf
, previous
, sh
);
532 for (i
= sh
->disks
; i
--; ) {
533 struct r5dev
*dev
= &sh
->dev
[i
];
535 if (dev
->toread
|| dev
->read
|| dev
->towrite
|| dev
->written
||
536 test_bit(R5_LOCKED
, &dev
->flags
)) {
537 printk(KERN_ERR
"sector=%llx i=%d %p %p %p %p %d\n",
538 (unsigned long long)sh
->sector
, i
, dev
->toread
,
539 dev
->read
, dev
->towrite
, dev
->written
,
540 test_bit(R5_LOCKED
, &dev
->flags
));
544 raid5_build_block(sh
, i
, previous
);
546 if (read_seqcount_retry(&conf
->gen_lock
, seq
))
548 insert_hash(conf
, sh
);
549 sh
->cpu
= smp_processor_id();
552 static struct stripe_head
*__find_stripe(struct r5conf
*conf
, sector_t sector
,
555 struct stripe_head
*sh
;
557 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector
);
558 hlist_for_each_entry(sh
, stripe_hash(conf
, sector
), hash
)
559 if (sh
->sector
== sector
&& sh
->generation
== generation
)
561 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector
);
566 * Need to check if array has failed when deciding whether to:
568 * - remove non-faulty devices
571 * This determination is simple when no reshape is happening.
572 * However if there is a reshape, we need to carefully check
573 * both the before and after sections.
574 * This is because some failed devices may only affect one
575 * of the two sections, and some non-in_sync devices may
576 * be insync in the section most affected by failed devices.
578 static int calc_degraded(struct r5conf
*conf
)
580 int degraded
, degraded2
;
585 for (i
= 0; i
< conf
->previous_raid_disks
; i
++) {
586 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
587 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
588 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
589 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
591 else if (test_bit(In_sync
, &rdev
->flags
))
594 /* not in-sync or faulty.
595 * If the reshape increases the number of devices,
596 * this is being recovered by the reshape, so
597 * this 'previous' section is not in_sync.
598 * If the number of devices is being reduced however,
599 * the device can only be part of the array if
600 * we are reverting a reshape, so this section will
603 if (conf
->raid_disks
>= conf
->previous_raid_disks
)
607 if (conf
->raid_disks
== conf
->previous_raid_disks
)
611 for (i
= 0; i
< conf
->raid_disks
; i
++) {
612 struct md_rdev
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
613 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
614 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
615 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
617 else if (test_bit(In_sync
, &rdev
->flags
))
620 /* not in-sync or faulty.
621 * If reshape increases the number of devices, this
622 * section has already been recovered, else it
623 * almost certainly hasn't.
625 if (conf
->raid_disks
<= conf
->previous_raid_disks
)
629 if (degraded2
> degraded
)
634 static int has_failed(struct r5conf
*conf
)
638 if (conf
->mddev
->reshape_position
== MaxSector
)
639 return conf
->mddev
->degraded
> conf
->max_degraded
;
641 degraded
= calc_degraded(conf
);
642 if (degraded
> conf
->max_degraded
)
647 static struct stripe_head
*
648 get_active_stripe(struct r5conf
*conf
, sector_t sector
,
649 int previous
, int noblock
, int noquiesce
)
651 struct stripe_head
*sh
;
652 int hash
= stripe_hash_locks_hash(sector
);
654 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector
);
656 spin_lock_irq(conf
->hash_locks
+ hash
);
659 wait_event_lock_irq(conf
->wait_for_stripe
,
660 conf
->quiesce
== 0 || noquiesce
,
661 *(conf
->hash_locks
+ hash
));
662 sh
= __find_stripe(conf
, sector
, conf
->generation
- previous
);
664 if (!conf
->inactive_blocked
)
665 sh
= get_free_stripe(conf
, hash
);
666 if (noblock
&& sh
== NULL
)
669 conf
->inactive_blocked
= 1;
671 conf
->wait_for_stripe
,
672 !list_empty(conf
->inactive_list
+ hash
) &&
673 (atomic_read(&conf
->active_stripes
)
674 < (conf
->max_nr_stripes
* 3 / 4)
675 || !conf
->inactive_blocked
),
676 *(conf
->hash_locks
+ hash
));
677 conf
->inactive_blocked
= 0;
679 init_stripe(sh
, sector
, previous
);
681 spin_lock(&conf
->device_lock
);
682 if (atomic_read(&sh
->count
)) {
683 BUG_ON(!list_empty(&sh
->lru
)
684 && !test_bit(STRIPE_EXPANDING
, &sh
->state
)
685 && !test_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
)
688 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
689 atomic_inc(&conf
->active_stripes
);
690 BUG_ON(list_empty(&sh
->lru
));
691 list_del_init(&sh
->lru
);
693 sh
->group
->stripes_cnt
--;
697 spin_unlock(&conf
->device_lock
);
699 } while (sh
== NULL
);
702 atomic_inc(&sh
->count
);
704 spin_unlock_irq(conf
->hash_locks
+ hash
);
708 /* Determine if 'data_offset' or 'new_data_offset' should be used
709 * in this stripe_head.
711 static int use_new_offset(struct r5conf
*conf
, struct stripe_head
*sh
)
713 sector_t progress
= conf
->reshape_progress
;
714 /* Need a memory barrier to make sure we see the value
715 * of conf->generation, or ->data_offset that was set before
716 * reshape_progress was updated.
719 if (progress
== MaxSector
)
721 if (sh
->generation
== conf
->generation
- 1)
723 /* We are in a reshape, and this is a new-generation stripe,
724 * so use new_data_offset.
730 raid5_end_read_request(struct bio
*bi
, int error
);
732 raid5_end_write_request(struct bio
*bi
, int error
);
734 static void ops_run_io(struct stripe_head
*sh
, struct stripe_head_state
*s
)
736 struct r5conf
*conf
= sh
->raid_conf
;
737 int i
, disks
= sh
->disks
;
741 for (i
= disks
; i
--; ) {
743 int replace_only
= 0;
744 struct bio
*bi
, *rbi
;
745 struct md_rdev
*rdev
, *rrdev
= NULL
;
746 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
)) {
747 if (test_and_clear_bit(R5_WantFUA
, &sh
->dev
[i
].flags
))
751 if (test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
753 } else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
755 else if (test_and_clear_bit(R5_WantReplace
,
756 &sh
->dev
[i
].flags
)) {
761 if (test_and_clear_bit(R5_SyncIO
, &sh
->dev
[i
].flags
))
764 bi
= &sh
->dev
[i
].req
;
765 rbi
= &sh
->dev
[i
].rreq
; /* For writing to replacement */
768 rrdev
= rcu_dereference(conf
->disks
[i
].replacement
);
769 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
770 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
779 /* We raced and saw duplicates */
782 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
) && rrdev
)
787 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
790 atomic_inc(&rdev
->nr_pending
);
791 if (rrdev
&& test_bit(Faulty
, &rrdev
->flags
))
794 atomic_inc(&rrdev
->nr_pending
);
797 /* We have already checked bad blocks for reads. Now
798 * need to check for writes. We never accept write errors
799 * on the replacement, so we don't to check rrdev.
801 while ((rw
& WRITE
) && rdev
&&
802 test_bit(WriteErrorSeen
, &rdev
->flags
)) {
805 int bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
806 &first_bad
, &bad_sectors
);
811 set_bit(BlockedBadBlocks
, &rdev
->flags
);
812 if (!conf
->mddev
->external
&&
813 conf
->mddev
->flags
) {
814 /* It is very unlikely, but we might
815 * still need to write out the
816 * bad block log - better give it
818 md_check_recovery(conf
->mddev
);
821 * Because md_wait_for_blocked_rdev
822 * will dec nr_pending, we must
823 * increment it first.
825 atomic_inc(&rdev
->nr_pending
);
826 md_wait_for_blocked_rdev(rdev
, conf
->mddev
);
828 /* Acknowledged bad block - skip the write */
829 rdev_dec_pending(rdev
, conf
->mddev
);
835 if (s
->syncing
|| s
->expanding
|| s
->expanded
837 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
839 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
842 bi
->bi_bdev
= rdev
->bdev
;
844 bi
->bi_end_io
= (rw
& WRITE
)
845 ? raid5_end_write_request
846 : raid5_end_read_request
;
849 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
850 __func__
, (unsigned long long)sh
->sector
,
852 atomic_inc(&sh
->count
);
853 if (use_new_offset(conf
, sh
))
854 bi
->bi_sector
= (sh
->sector
855 + rdev
->new_data_offset
);
857 bi
->bi_sector
= (sh
->sector
858 + rdev
->data_offset
);
859 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
860 bi
->bi_rw
|= REQ_NOMERGE
;
863 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
864 bi
->bi_io_vec
[0].bv_offset
= 0;
865 bi
->bi_size
= STRIPE_SIZE
;
867 * If this is discard request, set bi_vcnt 0. We don't
868 * want to confuse SCSI because SCSI will replace payload
870 if (rw
& REQ_DISCARD
)
873 set_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
);
875 if (conf
->mddev
->gendisk
)
876 trace_block_bio_remap(bdev_get_queue(bi
->bi_bdev
),
877 bi
, disk_devt(conf
->mddev
->gendisk
),
879 generic_make_request(bi
);
882 if (s
->syncing
|| s
->expanding
|| s
->expanded
884 md_sync_acct(rrdev
->bdev
, STRIPE_SECTORS
);
886 set_bit(STRIPE_IO_STARTED
, &sh
->state
);
889 rbi
->bi_bdev
= rrdev
->bdev
;
891 BUG_ON(!(rw
& WRITE
));
892 rbi
->bi_end_io
= raid5_end_write_request
;
893 rbi
->bi_private
= sh
;
895 pr_debug("%s: for %llu schedule op %ld on "
896 "replacement disc %d\n",
897 __func__
, (unsigned long long)sh
->sector
,
899 atomic_inc(&sh
->count
);
900 if (use_new_offset(conf
, sh
))
901 rbi
->bi_sector
= (sh
->sector
902 + rrdev
->new_data_offset
);
904 rbi
->bi_sector
= (sh
->sector
905 + rrdev
->data_offset
);
907 rbi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
908 rbi
->bi_io_vec
[0].bv_offset
= 0;
909 rbi
->bi_size
= STRIPE_SIZE
;
911 * If this is discard request, set bi_vcnt 0. We don't
912 * want to confuse SCSI because SCSI will replace payload
914 if (rw
& REQ_DISCARD
)
916 if (conf
->mddev
->gendisk
)
917 trace_block_bio_remap(bdev_get_queue(rbi
->bi_bdev
),
918 rbi
, disk_devt(conf
->mddev
->gendisk
),
920 generic_make_request(rbi
);
922 if (!rdev
&& !rrdev
) {
924 set_bit(STRIPE_DEGRADED
, &sh
->state
);
925 pr_debug("skip op %ld on disc %d for sector %llu\n",
926 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
927 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
928 set_bit(STRIPE_HANDLE
, &sh
->state
);
933 static struct dma_async_tx_descriptor
*
934 async_copy_data(int frombio
, struct bio
*bio
, struct page
*page
,
935 sector_t sector
, struct dma_async_tx_descriptor
*tx
)
938 struct page
*bio_page
;
941 struct async_submit_ctl submit
;
942 enum async_tx_flags flags
= 0;
944 if (bio
->bi_sector
>= sector
)
945 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
947 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
950 flags
|= ASYNC_TX_FENCE
;
951 init_async_submit(&submit
, flags
, tx
, NULL
, NULL
, NULL
);
953 bio_for_each_segment(bvl
, bio
, i
) {
954 int len
= bvl
->bv_len
;
958 if (page_offset
< 0) {
959 b_offset
= -page_offset
;
960 page_offset
+= b_offset
;
964 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
965 clen
= STRIPE_SIZE
- page_offset
;
970 b_offset
+= bvl
->bv_offset
;
971 bio_page
= bvl
->bv_page
;
973 tx
= async_memcpy(page
, bio_page
, page_offset
,
974 b_offset
, clen
, &submit
);
976 tx
= async_memcpy(bio_page
, page
, b_offset
,
977 page_offset
, clen
, &submit
);
979 /* chain the operations */
980 submit
.depend_tx
= tx
;
982 if (clen
< len
) /* hit end of page */
990 static void ops_complete_biofill(void *stripe_head_ref
)
992 struct stripe_head
*sh
= stripe_head_ref
;
993 struct bio
*return_bi
= NULL
;
996 pr_debug("%s: stripe %llu\n", __func__
,
997 (unsigned long long)sh
->sector
);
999 /* clear completed biofills */
1000 for (i
= sh
->disks
; i
--; ) {
1001 struct r5dev
*dev
= &sh
->dev
[i
];
1003 /* acknowledge completion of a biofill operation */
1004 /* and check if we need to reply to a read request,
1005 * new R5_Wantfill requests are held off until
1006 * !STRIPE_BIOFILL_RUN
1008 if (test_and_clear_bit(R5_Wantfill
, &dev
->flags
)) {
1009 struct bio
*rbi
, *rbi2
;
1014 while (rbi
&& rbi
->bi_sector
<
1015 dev
->sector
+ STRIPE_SECTORS
) {
1016 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1017 if (!raid5_dec_bi_active_stripes(rbi
)) {
1018 rbi
->bi_next
= return_bi
;
1025 clear_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
1027 return_io(return_bi
);
1029 set_bit(STRIPE_HANDLE
, &sh
->state
);
1033 static void ops_run_biofill(struct stripe_head
*sh
)
1035 struct dma_async_tx_descriptor
*tx
= NULL
;
1036 struct async_submit_ctl submit
;
1039 pr_debug("%s: stripe %llu\n", __func__
,
1040 (unsigned long long)sh
->sector
);
1042 for (i
= sh
->disks
; i
--; ) {
1043 struct r5dev
*dev
= &sh
->dev
[i
];
1044 if (test_bit(R5_Wantfill
, &dev
->flags
)) {
1046 spin_lock_irq(&sh
->stripe_lock
);
1047 dev
->read
= rbi
= dev
->toread
;
1049 spin_unlock_irq(&sh
->stripe_lock
);
1050 while (rbi
&& rbi
->bi_sector
<
1051 dev
->sector
+ STRIPE_SECTORS
) {
1052 tx
= async_copy_data(0, rbi
, dev
->page
,
1054 rbi
= r5_next_bio(rbi
, dev
->sector
);
1059 atomic_inc(&sh
->count
);
1060 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_biofill
, sh
, NULL
);
1061 async_trigger_callback(&submit
);
1064 static void mark_target_uptodate(struct stripe_head
*sh
, int target
)
1071 tgt
= &sh
->dev
[target
];
1072 set_bit(R5_UPTODATE
, &tgt
->flags
);
1073 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1074 clear_bit(R5_Wantcompute
, &tgt
->flags
);
1077 static void ops_complete_compute(void *stripe_head_ref
)
1079 struct stripe_head
*sh
= stripe_head_ref
;
1081 pr_debug("%s: stripe %llu\n", __func__
,
1082 (unsigned long long)sh
->sector
);
1084 /* mark the computed target(s) as uptodate */
1085 mark_target_uptodate(sh
, sh
->ops
.target
);
1086 mark_target_uptodate(sh
, sh
->ops
.target2
);
1088 clear_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
1089 if (sh
->check_state
== check_state_compute_run
)
1090 sh
->check_state
= check_state_compute_result
;
1091 set_bit(STRIPE_HANDLE
, &sh
->state
);
1095 /* return a pointer to the address conversion region of the scribble buffer */
1096 static addr_conv_t
*to_addr_conv(struct stripe_head
*sh
,
1097 struct raid5_percpu
*percpu
)
1099 return percpu
->scribble
+ sizeof(struct page
*) * (sh
->disks
+ 2);
1102 static struct dma_async_tx_descriptor
*
1103 ops_run_compute5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1105 int disks
= sh
->disks
;
1106 struct page
**xor_srcs
= percpu
->scribble
;
1107 int target
= sh
->ops
.target
;
1108 struct r5dev
*tgt
= &sh
->dev
[target
];
1109 struct page
*xor_dest
= tgt
->page
;
1111 struct dma_async_tx_descriptor
*tx
;
1112 struct async_submit_ctl submit
;
1115 pr_debug("%s: stripe %llu block: %d\n",
1116 __func__
, (unsigned long long)sh
->sector
, target
);
1117 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1119 for (i
= disks
; i
--; )
1121 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1123 atomic_inc(&sh
->count
);
1125 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
, NULL
,
1126 ops_complete_compute
, sh
, to_addr_conv(sh
, percpu
));
1127 if (unlikely(count
== 1))
1128 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1130 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1135 /* set_syndrome_sources - populate source buffers for gen_syndrome
1136 * @srcs - (struct page *) array of size sh->disks
1137 * @sh - stripe_head to parse
1139 * Populates srcs in proper layout order for the stripe and returns the
1140 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1141 * destination buffer is recorded in srcs[count] and the Q destination
1142 * is recorded in srcs[count+1]].
1144 static int set_syndrome_sources(struct page
**srcs
, struct stripe_head
*sh
)
1146 int disks
= sh
->disks
;
1147 int syndrome_disks
= sh
->ddf_layout
? disks
: (disks
- 2);
1148 int d0_idx
= raid6_d0(sh
);
1152 for (i
= 0; i
< disks
; i
++)
1158 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1160 srcs
[slot
] = sh
->dev
[i
].page
;
1161 i
= raid6_next_disk(i
, disks
);
1162 } while (i
!= d0_idx
);
1164 return syndrome_disks
;
1167 static struct dma_async_tx_descriptor
*
1168 ops_run_compute6_1(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1170 int disks
= sh
->disks
;
1171 struct page
**blocks
= percpu
->scribble
;
1173 int qd_idx
= sh
->qd_idx
;
1174 struct dma_async_tx_descriptor
*tx
;
1175 struct async_submit_ctl submit
;
1181 if (sh
->ops
.target
< 0)
1182 target
= sh
->ops
.target2
;
1183 else if (sh
->ops
.target2
< 0)
1184 target
= sh
->ops
.target
;
1186 /* we should only have one valid target */
1189 pr_debug("%s: stripe %llu block: %d\n",
1190 __func__
, (unsigned long long)sh
->sector
, target
);
1192 tgt
= &sh
->dev
[target
];
1193 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1196 atomic_inc(&sh
->count
);
1198 if (target
== qd_idx
) {
1199 count
= set_syndrome_sources(blocks
, sh
);
1200 blocks
[count
] = NULL
; /* regenerating p is not necessary */
1201 BUG_ON(blocks
[count
+1] != dest
); /* q should already be set */
1202 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1203 ops_complete_compute
, sh
,
1204 to_addr_conv(sh
, percpu
));
1205 tx
= async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1207 /* Compute any data- or p-drive using XOR */
1209 for (i
= disks
; i
-- ; ) {
1210 if (i
== target
|| i
== qd_idx
)
1212 blocks
[count
++] = sh
->dev
[i
].page
;
1215 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1216 NULL
, ops_complete_compute
, sh
,
1217 to_addr_conv(sh
, percpu
));
1218 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
, &submit
);
1224 static struct dma_async_tx_descriptor
*
1225 ops_run_compute6_2(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1227 int i
, count
, disks
= sh
->disks
;
1228 int syndrome_disks
= sh
->ddf_layout
? disks
: disks
-2;
1229 int d0_idx
= raid6_d0(sh
);
1230 int faila
= -1, failb
= -1;
1231 int target
= sh
->ops
.target
;
1232 int target2
= sh
->ops
.target2
;
1233 struct r5dev
*tgt
= &sh
->dev
[target
];
1234 struct r5dev
*tgt2
= &sh
->dev
[target2
];
1235 struct dma_async_tx_descriptor
*tx
;
1236 struct page
**blocks
= percpu
->scribble
;
1237 struct async_submit_ctl submit
;
1239 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1240 __func__
, (unsigned long long)sh
->sector
, target
, target2
);
1241 BUG_ON(target
< 0 || target2
< 0);
1242 BUG_ON(!test_bit(R5_Wantcompute
, &tgt
->flags
));
1243 BUG_ON(!test_bit(R5_Wantcompute
, &tgt2
->flags
));
1245 /* we need to open-code set_syndrome_sources to handle the
1246 * slot number conversion for 'faila' and 'failb'
1248 for (i
= 0; i
< disks
; i
++)
1253 int slot
= raid6_idx_to_slot(i
, sh
, &count
, syndrome_disks
);
1255 blocks
[slot
] = sh
->dev
[i
].page
;
1261 i
= raid6_next_disk(i
, disks
);
1262 } while (i
!= d0_idx
);
1264 BUG_ON(faila
== failb
);
1267 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1268 __func__
, (unsigned long long)sh
->sector
, faila
, failb
);
1270 atomic_inc(&sh
->count
);
1272 if (failb
== syndrome_disks
+1) {
1273 /* Q disk is one of the missing disks */
1274 if (faila
== syndrome_disks
) {
1275 /* Missing P+Q, just recompute */
1276 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1277 ops_complete_compute
, sh
,
1278 to_addr_conv(sh
, percpu
));
1279 return async_gen_syndrome(blocks
, 0, syndrome_disks
+2,
1280 STRIPE_SIZE
, &submit
);
1284 int qd_idx
= sh
->qd_idx
;
1286 /* Missing D+Q: recompute D from P, then recompute Q */
1287 if (target
== qd_idx
)
1288 data_target
= target2
;
1290 data_target
= target
;
1293 for (i
= disks
; i
-- ; ) {
1294 if (i
== data_target
|| i
== qd_idx
)
1296 blocks
[count
++] = sh
->dev
[i
].page
;
1298 dest
= sh
->dev
[data_target
].page
;
1299 init_async_submit(&submit
,
1300 ASYNC_TX_FENCE
|ASYNC_TX_XOR_ZERO_DST
,
1302 to_addr_conv(sh
, percpu
));
1303 tx
= async_xor(dest
, blocks
, 0, count
, STRIPE_SIZE
,
1306 count
= set_syndrome_sources(blocks
, sh
);
1307 init_async_submit(&submit
, ASYNC_TX_FENCE
, tx
,
1308 ops_complete_compute
, sh
,
1309 to_addr_conv(sh
, percpu
));
1310 return async_gen_syndrome(blocks
, 0, count
+2,
1311 STRIPE_SIZE
, &submit
);
1314 init_async_submit(&submit
, ASYNC_TX_FENCE
, NULL
,
1315 ops_complete_compute
, sh
,
1316 to_addr_conv(sh
, percpu
));
1317 if (failb
== syndrome_disks
) {
1318 /* We're missing D+P. */
1319 return async_raid6_datap_recov(syndrome_disks
+2,
1323 /* We're missing D+D. */
1324 return async_raid6_2data_recov(syndrome_disks
+2,
1325 STRIPE_SIZE
, faila
, failb
,
1332 static void ops_complete_prexor(void *stripe_head_ref
)
1334 struct stripe_head
*sh
= stripe_head_ref
;
1336 pr_debug("%s: stripe %llu\n", __func__
,
1337 (unsigned long long)sh
->sector
);
1340 static struct dma_async_tx_descriptor
*
1341 ops_run_prexor(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1342 struct dma_async_tx_descriptor
*tx
)
1344 int disks
= sh
->disks
;
1345 struct page
**xor_srcs
= percpu
->scribble
;
1346 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1347 struct async_submit_ctl submit
;
1349 /* existing parity data subtracted */
1350 struct page
*xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1352 pr_debug("%s: stripe %llu\n", __func__
,
1353 (unsigned long long)sh
->sector
);
1355 for (i
= disks
; i
--; ) {
1356 struct r5dev
*dev
= &sh
->dev
[i
];
1357 /* Only process blocks that are known to be uptodate */
1358 if (test_bit(R5_Wantdrain
, &dev
->flags
))
1359 xor_srcs
[count
++] = dev
->page
;
1362 init_async_submit(&submit
, ASYNC_TX_FENCE
|ASYNC_TX_XOR_DROP_DST
, tx
,
1363 ops_complete_prexor
, sh
, to_addr_conv(sh
, percpu
));
1364 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1369 static struct dma_async_tx_descriptor
*
1370 ops_run_biodrain(struct stripe_head
*sh
, struct dma_async_tx_descriptor
*tx
)
1372 int disks
= sh
->disks
;
1375 pr_debug("%s: stripe %llu\n", __func__
,
1376 (unsigned long long)sh
->sector
);
1378 for (i
= disks
; i
--; ) {
1379 struct r5dev
*dev
= &sh
->dev
[i
];
1382 if (test_and_clear_bit(R5_Wantdrain
, &dev
->flags
)) {
1385 spin_lock_irq(&sh
->stripe_lock
);
1386 chosen
= dev
->towrite
;
1387 dev
->towrite
= NULL
;
1388 BUG_ON(dev
->written
);
1389 wbi
= dev
->written
= chosen
;
1390 spin_unlock_irq(&sh
->stripe_lock
);
1392 while (wbi
&& wbi
->bi_sector
<
1393 dev
->sector
+ STRIPE_SECTORS
) {
1394 if (wbi
->bi_rw
& REQ_FUA
)
1395 set_bit(R5_WantFUA
, &dev
->flags
);
1396 if (wbi
->bi_rw
& REQ_SYNC
)
1397 set_bit(R5_SyncIO
, &dev
->flags
);
1398 if (wbi
->bi_rw
& REQ_DISCARD
)
1399 set_bit(R5_Discard
, &dev
->flags
);
1401 tx
= async_copy_data(1, wbi
, dev
->page
,
1403 wbi
= r5_next_bio(wbi
, dev
->sector
);
1411 static void ops_complete_reconstruct(void *stripe_head_ref
)
1413 struct stripe_head
*sh
= stripe_head_ref
;
1414 int disks
= sh
->disks
;
1415 int pd_idx
= sh
->pd_idx
;
1416 int qd_idx
= sh
->qd_idx
;
1418 bool fua
= false, sync
= false, discard
= false;
1420 pr_debug("%s: stripe %llu\n", __func__
,
1421 (unsigned long long)sh
->sector
);
1423 for (i
= disks
; i
--; ) {
1424 fua
|= test_bit(R5_WantFUA
, &sh
->dev
[i
].flags
);
1425 sync
|= test_bit(R5_SyncIO
, &sh
->dev
[i
].flags
);
1426 discard
|= test_bit(R5_Discard
, &sh
->dev
[i
].flags
);
1429 for (i
= disks
; i
--; ) {
1430 struct r5dev
*dev
= &sh
->dev
[i
];
1432 if (dev
->written
|| i
== pd_idx
|| i
== qd_idx
) {
1434 set_bit(R5_UPTODATE
, &dev
->flags
);
1436 set_bit(R5_WantFUA
, &dev
->flags
);
1438 set_bit(R5_SyncIO
, &dev
->flags
);
1442 if (sh
->reconstruct_state
== reconstruct_state_drain_run
)
1443 sh
->reconstruct_state
= reconstruct_state_drain_result
;
1444 else if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
)
1445 sh
->reconstruct_state
= reconstruct_state_prexor_drain_result
;
1447 BUG_ON(sh
->reconstruct_state
!= reconstruct_state_run
);
1448 sh
->reconstruct_state
= reconstruct_state_result
;
1451 set_bit(STRIPE_HANDLE
, &sh
->state
);
1456 ops_run_reconstruct5(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1457 struct dma_async_tx_descriptor
*tx
)
1459 int disks
= sh
->disks
;
1460 struct page
**xor_srcs
= percpu
->scribble
;
1461 struct async_submit_ctl submit
;
1462 int count
= 0, pd_idx
= sh
->pd_idx
, i
;
1463 struct page
*xor_dest
;
1465 unsigned long flags
;
1467 pr_debug("%s: stripe %llu\n", __func__
,
1468 (unsigned long long)sh
->sector
);
1470 for (i
= 0; i
< sh
->disks
; i
++) {
1473 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1476 if (i
>= sh
->disks
) {
1477 atomic_inc(&sh
->count
);
1478 set_bit(R5_Discard
, &sh
->dev
[pd_idx
].flags
);
1479 ops_complete_reconstruct(sh
);
1482 /* check if prexor is active which means only process blocks
1483 * that are part of a read-modify-write (written)
1485 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_run
) {
1487 xor_dest
= xor_srcs
[count
++] = sh
->dev
[pd_idx
].page
;
1488 for (i
= disks
; i
--; ) {
1489 struct r5dev
*dev
= &sh
->dev
[i
];
1491 xor_srcs
[count
++] = dev
->page
;
1494 xor_dest
= sh
->dev
[pd_idx
].page
;
1495 for (i
= disks
; i
--; ) {
1496 struct r5dev
*dev
= &sh
->dev
[i
];
1498 xor_srcs
[count
++] = dev
->page
;
1502 /* 1/ if we prexor'd then the dest is reused as a source
1503 * 2/ if we did not prexor then we are redoing the parity
1504 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1505 * for the synchronous xor case
1507 flags
= ASYNC_TX_ACK
|
1508 (prexor
? ASYNC_TX_XOR_DROP_DST
: ASYNC_TX_XOR_ZERO_DST
);
1510 atomic_inc(&sh
->count
);
1512 init_async_submit(&submit
, flags
, tx
, ops_complete_reconstruct
, sh
,
1513 to_addr_conv(sh
, percpu
));
1514 if (unlikely(count
== 1))
1515 tx
= async_memcpy(xor_dest
, xor_srcs
[0], 0, 0, STRIPE_SIZE
, &submit
);
1517 tx
= async_xor(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
, &submit
);
1521 ops_run_reconstruct6(struct stripe_head
*sh
, struct raid5_percpu
*percpu
,
1522 struct dma_async_tx_descriptor
*tx
)
1524 struct async_submit_ctl submit
;
1525 struct page
**blocks
= percpu
->scribble
;
1528 pr_debug("%s: stripe %llu\n", __func__
, (unsigned long long)sh
->sector
);
1530 for (i
= 0; i
< sh
->disks
; i
++) {
1531 if (sh
->pd_idx
== i
|| sh
->qd_idx
== i
)
1533 if (!test_bit(R5_Discard
, &sh
->dev
[i
].flags
))
1536 if (i
>= sh
->disks
) {
1537 atomic_inc(&sh
->count
);
1538 set_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
1539 set_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
1540 ops_complete_reconstruct(sh
);
1544 count
= set_syndrome_sources(blocks
, sh
);
1546 atomic_inc(&sh
->count
);
1548 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_reconstruct
,
1549 sh
, to_addr_conv(sh
, percpu
));
1550 async_gen_syndrome(blocks
, 0, count
+2, STRIPE_SIZE
, &submit
);
1553 static void ops_complete_check(void *stripe_head_ref
)
1555 struct stripe_head
*sh
= stripe_head_ref
;
1557 pr_debug("%s: stripe %llu\n", __func__
,
1558 (unsigned long long)sh
->sector
);
1560 sh
->check_state
= check_state_check_result
;
1561 set_bit(STRIPE_HANDLE
, &sh
->state
);
1565 static void ops_run_check_p(struct stripe_head
*sh
, struct raid5_percpu
*percpu
)
1567 int disks
= sh
->disks
;
1568 int pd_idx
= sh
->pd_idx
;
1569 int qd_idx
= sh
->qd_idx
;
1570 struct page
*xor_dest
;
1571 struct page
**xor_srcs
= percpu
->scribble
;
1572 struct dma_async_tx_descriptor
*tx
;
1573 struct async_submit_ctl submit
;
1577 pr_debug("%s: stripe %llu\n", __func__
,
1578 (unsigned long long)sh
->sector
);
1581 xor_dest
= sh
->dev
[pd_idx
].page
;
1582 xor_srcs
[count
++] = xor_dest
;
1583 for (i
= disks
; i
--; ) {
1584 if (i
== pd_idx
|| i
== qd_idx
)
1586 xor_srcs
[count
++] = sh
->dev
[i
].page
;
1589 init_async_submit(&submit
, 0, NULL
, NULL
, NULL
,
1590 to_addr_conv(sh
, percpu
));
1591 tx
= async_xor_val(xor_dest
, xor_srcs
, 0, count
, STRIPE_SIZE
,
1592 &sh
->ops
.zero_sum_result
, &submit
);
1594 atomic_inc(&sh
->count
);
1595 init_async_submit(&submit
, ASYNC_TX_ACK
, tx
, ops_complete_check
, sh
, NULL
);
1596 tx
= async_trigger_callback(&submit
);
1599 static void ops_run_check_pq(struct stripe_head
*sh
, struct raid5_percpu
*percpu
, int checkp
)
1601 struct page
**srcs
= percpu
->scribble
;
1602 struct async_submit_ctl submit
;
1605 pr_debug("%s: stripe %llu checkp: %d\n", __func__
,
1606 (unsigned long long)sh
->sector
, checkp
);
1608 count
= set_syndrome_sources(srcs
, sh
);
1612 atomic_inc(&sh
->count
);
1613 init_async_submit(&submit
, ASYNC_TX_ACK
, NULL
, ops_complete_check
,
1614 sh
, to_addr_conv(sh
, percpu
));
1615 async_syndrome_val(srcs
, 0, count
+2, STRIPE_SIZE
,
1616 &sh
->ops
.zero_sum_result
, percpu
->spare_page
, &submit
);
1619 static void raid_run_ops(struct stripe_head
*sh
, unsigned long ops_request
)
1621 int overlap_clear
= 0, i
, disks
= sh
->disks
;
1622 struct dma_async_tx_descriptor
*tx
= NULL
;
1623 struct r5conf
*conf
= sh
->raid_conf
;
1624 int level
= conf
->level
;
1625 struct raid5_percpu
*percpu
;
1629 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1630 if (test_bit(STRIPE_OP_BIOFILL
, &ops_request
)) {
1631 ops_run_biofill(sh
);
1635 if (test_bit(STRIPE_OP_COMPUTE_BLK
, &ops_request
)) {
1637 tx
= ops_run_compute5(sh
, percpu
);
1639 if (sh
->ops
.target2
< 0 || sh
->ops
.target
< 0)
1640 tx
= ops_run_compute6_1(sh
, percpu
);
1642 tx
= ops_run_compute6_2(sh
, percpu
);
1644 /* terminate the chain if reconstruct is not set to be run */
1645 if (tx
&& !test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
))
1649 if (test_bit(STRIPE_OP_PREXOR
, &ops_request
))
1650 tx
= ops_run_prexor(sh
, percpu
, tx
);
1652 if (test_bit(STRIPE_OP_BIODRAIN
, &ops_request
)) {
1653 tx
= ops_run_biodrain(sh
, tx
);
1657 if (test_bit(STRIPE_OP_RECONSTRUCT
, &ops_request
)) {
1659 ops_run_reconstruct5(sh
, percpu
, tx
);
1661 ops_run_reconstruct6(sh
, percpu
, tx
);
1664 if (test_bit(STRIPE_OP_CHECK
, &ops_request
)) {
1665 if (sh
->check_state
== check_state_run
)
1666 ops_run_check_p(sh
, percpu
);
1667 else if (sh
->check_state
== check_state_run_q
)
1668 ops_run_check_pq(sh
, percpu
, 0);
1669 else if (sh
->check_state
== check_state_run_pq
)
1670 ops_run_check_pq(sh
, percpu
, 1);
1676 for (i
= disks
; i
--; ) {
1677 struct r5dev
*dev
= &sh
->dev
[i
];
1678 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1679 wake_up(&sh
->raid_conf
->wait_for_overlap
);
1684 static int grow_one_stripe(struct r5conf
*conf
, int hash
)
1686 struct stripe_head
*sh
;
1687 sh
= kmem_cache_zalloc(conf
->slab_cache
, GFP_KERNEL
);
1691 sh
->raid_conf
= conf
;
1693 spin_lock_init(&sh
->stripe_lock
);
1695 if (grow_buffers(sh
)) {
1697 kmem_cache_free(conf
->slab_cache
, sh
);
1700 sh
->hash_lock_index
= hash
;
1701 /* we just created an active stripe so... */
1702 atomic_set(&sh
->count
, 1);
1703 atomic_inc(&conf
->active_stripes
);
1704 INIT_LIST_HEAD(&sh
->lru
);
1709 static int grow_stripes(struct r5conf
*conf
, int num
)
1711 struct kmem_cache
*sc
;
1712 int devs
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
1715 if (conf
->mddev
->gendisk
)
1716 sprintf(conf
->cache_name
[0],
1717 "raid%d-%s", conf
->level
, mdname(conf
->mddev
));
1719 sprintf(conf
->cache_name
[0],
1720 "raid%d-%p", conf
->level
, conf
->mddev
);
1721 sprintf(conf
->cache_name
[1], "%s-alt", conf
->cache_name
[0]);
1723 conf
->active_name
= 0;
1724 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
1725 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
1729 conf
->slab_cache
= sc
;
1730 conf
->pool_size
= devs
;
1731 hash
= conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
;
1733 if (!grow_one_stripe(conf
, hash
))
1735 conf
->max_nr_stripes
++;
1736 hash
= (hash
+ 1) % NR_STRIPE_HASH_LOCKS
;
1742 * scribble_len - return the required size of the scribble region
1743 * @num - total number of disks in the array
1745 * The size must be enough to contain:
1746 * 1/ a struct page pointer for each device in the array +2
1747 * 2/ room to convert each entry in (1) to its corresponding dma
1748 * (dma_map_page()) or page (page_address()) address.
1750 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1751 * calculate over all devices (not just the data blocks), using zeros in place
1752 * of the P and Q blocks.
1754 static size_t scribble_len(int num
)
1758 len
= sizeof(struct page
*) * (num
+2) + sizeof(addr_conv_t
) * (num
+2);
1763 static int resize_stripes(struct r5conf
*conf
, int newsize
)
1765 /* Make all the stripes able to hold 'newsize' devices.
1766 * New slots in each stripe get 'page' set to a new page.
1768 * This happens in stages:
1769 * 1/ create a new kmem_cache and allocate the required number of
1771 * 2/ gather all the old stripe_heads and transfer the pages across
1772 * to the new stripe_heads. This will have the side effect of
1773 * freezing the array as once all stripe_heads have been collected,
1774 * no IO will be possible. Old stripe heads are freed once their
1775 * pages have been transferred over, and the old kmem_cache is
1776 * freed when all stripes are done.
1777 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1778 * we simple return a failre status - no need to clean anything up.
1779 * 4/ allocate new pages for the new slots in the new stripe_heads.
1780 * If this fails, we don't bother trying the shrink the
1781 * stripe_heads down again, we just leave them as they are.
1782 * As each stripe_head is processed the new one is released into
1785 * Once step2 is started, we cannot afford to wait for a write,
1786 * so we use GFP_NOIO allocations.
1788 struct stripe_head
*osh
, *nsh
;
1789 LIST_HEAD(newstripes
);
1790 struct disk_info
*ndisks
;
1793 struct kmem_cache
*sc
;
1797 if (newsize
<= conf
->pool_size
)
1798 return 0; /* never bother to shrink */
1800 err
= md_allow_write(conf
->mddev
);
1805 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
1806 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
1811 for (i
= conf
->max_nr_stripes
; i
; i
--) {
1812 nsh
= kmem_cache_zalloc(sc
, GFP_KERNEL
);
1816 nsh
->raid_conf
= conf
;
1817 spin_lock_init(&nsh
->stripe_lock
);
1819 list_add(&nsh
->lru
, &newstripes
);
1822 /* didn't get enough, give up */
1823 while (!list_empty(&newstripes
)) {
1824 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1825 list_del(&nsh
->lru
);
1826 kmem_cache_free(sc
, nsh
);
1828 kmem_cache_destroy(sc
);
1831 /* Step 2 - Must use GFP_NOIO now.
1832 * OK, we have enough stripes, start collecting inactive
1833 * stripes and copying them over
1837 list_for_each_entry(nsh
, &newstripes
, lru
) {
1838 lock_device_hash_lock(conf
, hash
);
1839 wait_event_cmd(conf
->wait_for_stripe
,
1840 !list_empty(conf
->inactive_list
+ hash
),
1841 unlock_device_hash_lock(conf
, hash
),
1842 lock_device_hash_lock(conf
, hash
));
1843 osh
= get_free_stripe(conf
, hash
);
1844 unlock_device_hash_lock(conf
, hash
);
1845 atomic_set(&nsh
->count
, 1);
1846 for(i
=0; i
<conf
->pool_size
; i
++)
1847 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
1848 for( ; i
<newsize
; i
++)
1849 nsh
->dev
[i
].page
= NULL
;
1850 nsh
->hash_lock_index
= hash
;
1851 kmem_cache_free(conf
->slab_cache
, osh
);
1853 if (cnt
>= conf
->max_nr_stripes
/ NR_STRIPE_HASH_LOCKS
+
1854 !!((conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
) > hash
)) {
1859 kmem_cache_destroy(conf
->slab_cache
);
1862 * At this point, we are holding all the stripes so the array
1863 * is completely stalled, so now is a good time to resize
1864 * conf->disks and the scribble region
1866 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
1868 for (i
=0; i
<conf
->raid_disks
; i
++)
1869 ndisks
[i
] = conf
->disks
[i
];
1871 conf
->disks
= ndisks
;
1876 conf
->scribble_len
= scribble_len(newsize
);
1877 for_each_present_cpu(cpu
) {
1878 struct raid5_percpu
*percpu
;
1881 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
1882 scribble
= kmalloc(conf
->scribble_len
, GFP_NOIO
);
1885 kfree(percpu
->scribble
);
1886 percpu
->scribble
= scribble
;
1894 /* Step 4, return new stripes to service */
1895 while(!list_empty(&newstripes
)) {
1896 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
1897 list_del_init(&nsh
->lru
);
1899 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
1900 if (nsh
->dev
[i
].page
== NULL
) {
1901 struct page
*p
= alloc_page(GFP_NOIO
);
1902 nsh
->dev
[i
].page
= p
;
1906 release_stripe(nsh
);
1908 /* critical section pass, GFP_NOIO no longer needed */
1910 conf
->slab_cache
= sc
;
1911 conf
->active_name
= 1-conf
->active_name
;
1912 conf
->pool_size
= newsize
;
1916 static int drop_one_stripe(struct r5conf
*conf
, int hash
)
1918 struct stripe_head
*sh
;
1920 spin_lock_irq(conf
->hash_locks
+ hash
);
1921 sh
= get_free_stripe(conf
, hash
);
1922 spin_unlock_irq(conf
->hash_locks
+ hash
);
1925 BUG_ON(atomic_read(&sh
->count
));
1927 kmem_cache_free(conf
->slab_cache
, sh
);
1928 atomic_dec(&conf
->active_stripes
);
1932 static void shrink_stripes(struct r5conf
*conf
)
1935 for (hash
= 0; hash
< NR_STRIPE_HASH_LOCKS
; hash
++)
1936 while (drop_one_stripe(conf
, hash
))
1939 if (conf
->slab_cache
)
1940 kmem_cache_destroy(conf
->slab_cache
);
1941 conf
->slab_cache
= NULL
;
1944 static void raid5_end_read_request(struct bio
* bi
, int error
)
1946 struct stripe_head
*sh
= bi
->bi_private
;
1947 struct r5conf
*conf
= sh
->raid_conf
;
1948 int disks
= sh
->disks
, i
;
1949 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1950 char b
[BDEVNAME_SIZE
];
1951 struct md_rdev
*rdev
= NULL
;
1954 for (i
=0 ; i
<disks
; i
++)
1955 if (bi
== &sh
->dev
[i
].req
)
1958 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1959 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
1965 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
1966 /* If replacement finished while this request was outstanding,
1967 * 'replacement' might be NULL already.
1968 * In that case it moved down to 'rdev'.
1969 * rdev is not removed until all requests are finished.
1971 rdev
= conf
->disks
[i
].replacement
;
1973 rdev
= conf
->disks
[i
].rdev
;
1975 if (use_new_offset(conf
, sh
))
1976 s
= sh
->sector
+ rdev
->new_data_offset
;
1978 s
= sh
->sector
+ rdev
->data_offset
;
1980 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1981 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1982 /* Note that this cannot happen on a
1983 * replacement device. We just fail those on
1988 "md/raid:%s: read error corrected"
1989 " (%lu sectors at %llu on %s)\n",
1990 mdname(conf
->mddev
), STRIPE_SECTORS
,
1991 (unsigned long long)s
,
1992 bdevname(rdev
->bdev
, b
));
1993 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1994 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
1995 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
1996 } else if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
1997 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
1999 if (atomic_read(&rdev
->read_errors
))
2000 atomic_set(&rdev
->read_errors
, 0);
2002 const char *bdn
= bdevname(rdev
->bdev
, b
);
2006 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
2007 atomic_inc(&rdev
->read_errors
);
2008 if (test_bit(R5_ReadRepl
, &sh
->dev
[i
].flags
))
2011 "md/raid:%s: read error on replacement device "
2012 "(sector %llu on %s).\n",
2013 mdname(conf
->mddev
),
2014 (unsigned long long)s
,
2016 else if (conf
->mddev
->degraded
>= conf
->max_degraded
) {
2020 "md/raid:%s: read error not correctable "
2021 "(sector %llu on %s).\n",
2022 mdname(conf
->mddev
),
2023 (unsigned long long)s
,
2025 } else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
)) {
2030 "md/raid:%s: read error NOT corrected!! "
2031 "(sector %llu on %s).\n",
2032 mdname(conf
->mddev
),
2033 (unsigned long long)s
,
2035 } else if (atomic_read(&rdev
->read_errors
)
2036 > conf
->max_nr_stripes
)
2038 "md/raid:%s: Too many read errors, failing device %s.\n",
2039 mdname(conf
->mddev
), bdn
);
2042 if (set_bad
&& test_bit(In_sync
, &rdev
->flags
)
2043 && !test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
))
2046 if (test_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
)) {
2047 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2048 clear_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2050 set_bit(R5_ReadNoMerge
, &sh
->dev
[i
].flags
);
2052 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
2053 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2055 && test_bit(In_sync
, &rdev
->flags
)
2056 && rdev_set_badblocks(
2057 rdev
, sh
->sector
, STRIPE_SECTORS
, 0)))
2058 md_error(conf
->mddev
, rdev
);
2061 rdev_dec_pending(rdev
, conf
->mddev
);
2062 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2063 set_bit(STRIPE_HANDLE
, &sh
->state
);
2067 static void raid5_end_write_request(struct bio
*bi
, int error
)
2069 struct stripe_head
*sh
= bi
->bi_private
;
2070 struct r5conf
*conf
= sh
->raid_conf
;
2071 int disks
= sh
->disks
, i
;
2072 struct md_rdev
*uninitialized_var(rdev
);
2073 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2076 int replacement
= 0;
2078 for (i
= 0 ; i
< disks
; i
++) {
2079 if (bi
== &sh
->dev
[i
].req
) {
2080 rdev
= conf
->disks
[i
].rdev
;
2083 if (bi
== &sh
->dev
[i
].rreq
) {
2084 rdev
= conf
->disks
[i
].replacement
;
2088 /* rdev was removed and 'replacement'
2089 * replaced it. rdev is not removed
2090 * until all requests are finished.
2092 rdev
= conf
->disks
[i
].rdev
;
2096 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
2097 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
2106 md_error(conf
->mddev
, rdev
);
2107 else if (is_badblock(rdev
, sh
->sector
,
2109 &first_bad
, &bad_sectors
))
2110 set_bit(R5_MadeGoodRepl
, &sh
->dev
[i
].flags
);
2113 set_bit(WriteErrorSeen
, &rdev
->flags
);
2114 set_bit(R5_WriteError
, &sh
->dev
[i
].flags
);
2115 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2116 set_bit(MD_RECOVERY_NEEDED
,
2117 &rdev
->mddev
->recovery
);
2118 } else if (is_badblock(rdev
, sh
->sector
,
2120 &first_bad
, &bad_sectors
)) {
2121 set_bit(R5_MadeGood
, &sh
->dev
[i
].flags
);
2122 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))
2123 /* That was a successful write so make
2124 * sure it looks like we already did
2127 set_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
2130 rdev_dec_pending(rdev
, conf
->mddev
);
2132 if (!test_and_clear_bit(R5_DOUBLE_LOCKED
, &sh
->dev
[i
].flags
))
2133 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2134 set_bit(STRIPE_HANDLE
, &sh
->state
);
2138 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
);
2140 static void raid5_build_block(struct stripe_head
*sh
, int i
, int previous
)
2142 struct r5dev
*dev
= &sh
->dev
[i
];
2144 bio_init(&dev
->req
);
2145 dev
->req
.bi_io_vec
= &dev
->vec
;
2147 dev
->req
.bi_max_vecs
++;
2148 dev
->req
.bi_private
= sh
;
2149 dev
->vec
.bv_page
= dev
->page
;
2151 bio_init(&dev
->rreq
);
2152 dev
->rreq
.bi_io_vec
= &dev
->rvec
;
2153 dev
->rreq
.bi_vcnt
++;
2154 dev
->rreq
.bi_max_vecs
++;
2155 dev
->rreq
.bi_private
= sh
;
2156 dev
->rvec
.bv_page
= dev
->page
;
2159 dev
->sector
= compute_blocknr(sh
, i
, previous
);
2162 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2164 char b
[BDEVNAME_SIZE
];
2165 struct r5conf
*conf
= mddev
->private;
2166 unsigned long flags
;
2167 pr_debug("raid456: error called\n");
2169 spin_lock_irqsave(&conf
->device_lock
, flags
);
2170 clear_bit(In_sync
, &rdev
->flags
);
2171 mddev
->degraded
= calc_degraded(conf
);
2172 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2173 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2175 set_bit(Blocked
, &rdev
->flags
);
2176 set_bit(Faulty
, &rdev
->flags
);
2177 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2179 "md/raid:%s: Disk failure on %s, disabling device.\n"
2180 "md/raid:%s: Operation continuing on %d devices.\n",
2182 bdevname(rdev
->bdev
, b
),
2184 conf
->raid_disks
- mddev
->degraded
);
2188 * Input: a 'big' sector number,
2189 * Output: index of the data and parity disk, and the sector # in them.
2191 static sector_t
raid5_compute_sector(struct r5conf
*conf
, sector_t r_sector
,
2192 int previous
, int *dd_idx
,
2193 struct stripe_head
*sh
)
2195 sector_t stripe
, stripe2
;
2196 sector_t chunk_number
;
2197 unsigned int chunk_offset
;
2200 sector_t new_sector
;
2201 int algorithm
= previous
? conf
->prev_algo
2203 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2204 : conf
->chunk_sectors
;
2205 int raid_disks
= previous
? conf
->previous_raid_disks
2207 int data_disks
= raid_disks
- conf
->max_degraded
;
2209 /* First compute the information on this sector */
2212 * Compute the chunk number and the sector offset inside the chunk
2214 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
2215 chunk_number
= r_sector
;
2218 * Compute the stripe number
2220 stripe
= chunk_number
;
2221 *dd_idx
= sector_div(stripe
, data_disks
);
2224 * Select the parity disk based on the user selected algorithm.
2226 pd_idx
= qd_idx
= -1;
2227 switch(conf
->level
) {
2229 pd_idx
= data_disks
;
2232 switch (algorithm
) {
2233 case ALGORITHM_LEFT_ASYMMETRIC
:
2234 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2235 if (*dd_idx
>= pd_idx
)
2238 case ALGORITHM_RIGHT_ASYMMETRIC
:
2239 pd_idx
= sector_div(stripe2
, raid_disks
);
2240 if (*dd_idx
>= pd_idx
)
2243 case ALGORITHM_LEFT_SYMMETRIC
:
2244 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
);
2245 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2247 case ALGORITHM_RIGHT_SYMMETRIC
:
2248 pd_idx
= sector_div(stripe2
, raid_disks
);
2249 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2251 case ALGORITHM_PARITY_0
:
2255 case ALGORITHM_PARITY_N
:
2256 pd_idx
= data_disks
;
2264 switch (algorithm
) {
2265 case ALGORITHM_LEFT_ASYMMETRIC
:
2266 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2267 qd_idx
= pd_idx
+ 1;
2268 if (pd_idx
== raid_disks
-1) {
2269 (*dd_idx
)++; /* Q D D D P */
2271 } else if (*dd_idx
>= pd_idx
)
2272 (*dd_idx
) += 2; /* D D P Q D */
2274 case ALGORITHM_RIGHT_ASYMMETRIC
:
2275 pd_idx
= sector_div(stripe2
, raid_disks
);
2276 qd_idx
= pd_idx
+ 1;
2277 if (pd_idx
== raid_disks
-1) {
2278 (*dd_idx
)++; /* Q D D D P */
2280 } else if (*dd_idx
>= pd_idx
)
2281 (*dd_idx
) += 2; /* D D P Q D */
2283 case ALGORITHM_LEFT_SYMMETRIC
:
2284 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2285 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2286 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2288 case ALGORITHM_RIGHT_SYMMETRIC
:
2289 pd_idx
= sector_div(stripe2
, raid_disks
);
2290 qd_idx
= (pd_idx
+ 1) % raid_disks
;
2291 *dd_idx
= (pd_idx
+ 2 + *dd_idx
) % raid_disks
;
2294 case ALGORITHM_PARITY_0
:
2299 case ALGORITHM_PARITY_N
:
2300 pd_idx
= data_disks
;
2301 qd_idx
= data_disks
+ 1;
2304 case ALGORITHM_ROTATING_ZERO_RESTART
:
2305 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2306 * of blocks for computing Q is different.
2308 pd_idx
= sector_div(stripe2
, raid_disks
);
2309 qd_idx
= pd_idx
+ 1;
2310 if (pd_idx
== raid_disks
-1) {
2311 (*dd_idx
)++; /* Q D D D P */
2313 } else if (*dd_idx
>= pd_idx
)
2314 (*dd_idx
) += 2; /* D D P Q D */
2318 case ALGORITHM_ROTATING_N_RESTART
:
2319 /* Same a left_asymmetric, by first stripe is
2320 * D D D P Q rather than
2324 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2325 qd_idx
= pd_idx
+ 1;
2326 if (pd_idx
== raid_disks
-1) {
2327 (*dd_idx
)++; /* Q D D D P */
2329 } else if (*dd_idx
>= pd_idx
)
2330 (*dd_idx
) += 2; /* D D P Q D */
2334 case ALGORITHM_ROTATING_N_CONTINUE
:
2335 /* Same as left_symmetric but Q is before P */
2336 pd_idx
= raid_disks
- 1 - sector_div(stripe2
, raid_disks
);
2337 qd_idx
= (pd_idx
+ raid_disks
- 1) % raid_disks
;
2338 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % raid_disks
;
2342 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2343 /* RAID5 left_asymmetric, with Q on last device */
2344 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2345 if (*dd_idx
>= pd_idx
)
2347 qd_idx
= raid_disks
- 1;
2350 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2351 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2352 if (*dd_idx
>= pd_idx
)
2354 qd_idx
= raid_disks
- 1;
2357 case ALGORITHM_LEFT_SYMMETRIC_6
:
2358 pd_idx
= data_disks
- sector_div(stripe2
, raid_disks
-1);
2359 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2360 qd_idx
= raid_disks
- 1;
2363 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2364 pd_idx
= sector_div(stripe2
, raid_disks
-1);
2365 *dd_idx
= (pd_idx
+ 1 + *dd_idx
) % (raid_disks
-1);
2366 qd_idx
= raid_disks
- 1;
2369 case ALGORITHM_PARITY_0_6
:
2372 qd_idx
= raid_disks
- 1;
2382 sh
->pd_idx
= pd_idx
;
2383 sh
->qd_idx
= qd_idx
;
2384 sh
->ddf_layout
= ddf_layout
;
2387 * Finally, compute the new sector number
2389 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
2394 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
, int previous
)
2396 struct r5conf
*conf
= sh
->raid_conf
;
2397 int raid_disks
= sh
->disks
;
2398 int data_disks
= raid_disks
- conf
->max_degraded
;
2399 sector_t new_sector
= sh
->sector
, check
;
2400 int sectors_per_chunk
= previous
? conf
->prev_chunk_sectors
2401 : conf
->chunk_sectors
;
2402 int algorithm
= previous
? conf
->prev_algo
2406 sector_t chunk_number
;
2407 int dummy1
, dd_idx
= i
;
2409 struct stripe_head sh2
;
2412 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
2413 stripe
= new_sector
;
2415 if (i
== sh
->pd_idx
)
2417 switch(conf
->level
) {
2420 switch (algorithm
) {
2421 case ALGORITHM_LEFT_ASYMMETRIC
:
2422 case ALGORITHM_RIGHT_ASYMMETRIC
:
2426 case ALGORITHM_LEFT_SYMMETRIC
:
2427 case ALGORITHM_RIGHT_SYMMETRIC
:
2430 i
-= (sh
->pd_idx
+ 1);
2432 case ALGORITHM_PARITY_0
:
2435 case ALGORITHM_PARITY_N
:
2442 if (i
== sh
->qd_idx
)
2443 return 0; /* It is the Q disk */
2444 switch (algorithm
) {
2445 case ALGORITHM_LEFT_ASYMMETRIC
:
2446 case ALGORITHM_RIGHT_ASYMMETRIC
:
2447 case ALGORITHM_ROTATING_ZERO_RESTART
:
2448 case ALGORITHM_ROTATING_N_RESTART
:
2449 if (sh
->pd_idx
== raid_disks
-1)
2450 i
--; /* Q D D D P */
2451 else if (i
> sh
->pd_idx
)
2452 i
-= 2; /* D D P Q D */
2454 case ALGORITHM_LEFT_SYMMETRIC
:
2455 case ALGORITHM_RIGHT_SYMMETRIC
:
2456 if (sh
->pd_idx
== raid_disks
-1)
2457 i
--; /* Q D D D P */
2462 i
-= (sh
->pd_idx
+ 2);
2465 case ALGORITHM_PARITY_0
:
2468 case ALGORITHM_PARITY_N
:
2470 case ALGORITHM_ROTATING_N_CONTINUE
:
2471 /* Like left_symmetric, but P is before Q */
2472 if (sh
->pd_idx
== 0)
2473 i
--; /* P D D D Q */
2478 i
-= (sh
->pd_idx
+ 1);
2481 case ALGORITHM_LEFT_ASYMMETRIC_6
:
2482 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
2486 case ALGORITHM_LEFT_SYMMETRIC_6
:
2487 case ALGORITHM_RIGHT_SYMMETRIC_6
:
2489 i
+= data_disks
+ 1;
2490 i
-= (sh
->pd_idx
+ 1);
2492 case ALGORITHM_PARITY_0_6
:
2501 chunk_number
= stripe
* data_disks
+ i
;
2502 r_sector
= chunk_number
* sectors_per_chunk
+ chunk_offset
;
2504 check
= raid5_compute_sector(conf
, r_sector
,
2505 previous
, &dummy1
, &sh2
);
2506 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| sh2
.pd_idx
!= sh
->pd_idx
2507 || sh2
.qd_idx
!= sh
->qd_idx
) {
2508 printk(KERN_ERR
"md/raid:%s: compute_blocknr: map not correct\n",
2509 mdname(conf
->mddev
));
2517 schedule_reconstruction(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2518 int rcw
, int expand
)
2520 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
;
2521 struct r5conf
*conf
= sh
->raid_conf
;
2522 int level
= conf
->level
;
2526 for (i
= disks
; i
--; ) {
2527 struct r5dev
*dev
= &sh
->dev
[i
];
2530 set_bit(R5_LOCKED
, &dev
->flags
);
2531 set_bit(R5_Wantdrain
, &dev
->flags
);
2533 clear_bit(R5_UPTODATE
, &dev
->flags
);
2537 /* if we are not expanding this is a proper write request, and
2538 * there will be bios with new data to be drained into the
2543 /* False alarm, nothing to do */
2545 sh
->reconstruct_state
= reconstruct_state_drain_run
;
2546 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2548 sh
->reconstruct_state
= reconstruct_state_run
;
2550 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2552 if (s
->locked
+ conf
->max_degraded
== disks
)
2553 if (!test_and_set_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2554 atomic_inc(&conf
->pending_full_writes
);
2557 BUG_ON(!(test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
) ||
2558 test_bit(R5_Wantcompute
, &sh
->dev
[pd_idx
].flags
)));
2560 for (i
= disks
; i
--; ) {
2561 struct r5dev
*dev
= &sh
->dev
[i
];
2566 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2567 test_bit(R5_Wantcompute
, &dev
->flags
))) {
2568 set_bit(R5_Wantdrain
, &dev
->flags
);
2569 set_bit(R5_LOCKED
, &dev
->flags
);
2570 clear_bit(R5_UPTODATE
, &dev
->flags
);
2575 /* False alarm - nothing to do */
2577 sh
->reconstruct_state
= reconstruct_state_prexor_drain_run
;
2578 set_bit(STRIPE_OP_PREXOR
, &s
->ops_request
);
2579 set_bit(STRIPE_OP_BIODRAIN
, &s
->ops_request
);
2580 set_bit(STRIPE_OP_RECONSTRUCT
, &s
->ops_request
);
2583 /* keep the parity disk(s) locked while asynchronous operations
2586 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
2587 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
2591 int qd_idx
= sh
->qd_idx
;
2592 struct r5dev
*dev
= &sh
->dev
[qd_idx
];
2594 set_bit(R5_LOCKED
, &dev
->flags
);
2595 clear_bit(R5_UPTODATE
, &dev
->flags
);
2599 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2600 __func__
, (unsigned long long)sh
->sector
,
2601 s
->locked
, s
->ops_request
);
2605 * Each stripe/dev can have one or more bion attached.
2606 * toread/towrite point to the first in a chain.
2607 * The bi_next chain must be in order.
2609 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
2612 struct r5conf
*conf
= sh
->raid_conf
;
2615 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2616 (unsigned long long)bi
->bi_sector
,
2617 (unsigned long long)sh
->sector
);
2620 * If several bio share a stripe. The bio bi_phys_segments acts as a
2621 * reference count to avoid race. The reference count should already be
2622 * increased before this function is called (for example, in
2623 * make_request()), so other bio sharing this stripe will not free the
2624 * stripe. If a stripe is owned by one stripe, the stripe lock will
2627 spin_lock_irq(&sh
->stripe_lock
);
2629 bip
= &sh
->dev
[dd_idx
].towrite
;
2633 bip
= &sh
->dev
[dd_idx
].toread
;
2634 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
2635 if (bio_end_sector(*bip
) > bi
->bi_sector
)
2637 bip
= & (*bip
)->bi_next
;
2639 if (*bip
&& (*bip
)->bi_sector
< bio_end_sector(bi
))
2642 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
2646 raid5_inc_bi_active_stripes(bi
);
2649 /* check if page is covered */
2650 sector_t sector
= sh
->dev
[dd_idx
].sector
;
2651 for (bi
=sh
->dev
[dd_idx
].towrite
;
2652 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
2653 bi
&& bi
->bi_sector
<= sector
;
2654 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
2655 if (bio_end_sector(bi
) >= sector
)
2656 sector
= bio_end_sector(bi
);
2658 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
2659 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
2662 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2663 (unsigned long long)(*bip
)->bi_sector
,
2664 (unsigned long long)sh
->sector
, dd_idx
);
2665 spin_unlock_irq(&sh
->stripe_lock
);
2667 if (conf
->mddev
->bitmap
&& firstwrite
) {
2668 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
2670 sh
->bm_seq
= conf
->seq_flush
+1;
2671 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
2676 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
2677 spin_unlock_irq(&sh
->stripe_lock
);
2681 static void end_reshape(struct r5conf
*conf
);
2683 static void stripe_set_idx(sector_t stripe
, struct r5conf
*conf
, int previous
,
2684 struct stripe_head
*sh
)
2686 int sectors_per_chunk
=
2687 previous
? conf
->prev_chunk_sectors
: conf
->chunk_sectors
;
2689 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
2690 int disks
= previous
? conf
->previous_raid_disks
: conf
->raid_disks
;
2692 raid5_compute_sector(conf
,
2693 stripe
* (disks
- conf
->max_degraded
)
2694 *sectors_per_chunk
+ chunk_offset
,
2700 handle_failed_stripe(struct r5conf
*conf
, struct stripe_head
*sh
,
2701 struct stripe_head_state
*s
, int disks
,
2702 struct bio
**return_bi
)
2705 for (i
= disks
; i
--; ) {
2709 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2710 struct md_rdev
*rdev
;
2712 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2713 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
2714 atomic_inc(&rdev
->nr_pending
);
2719 if (!rdev_set_badblocks(
2723 md_error(conf
->mddev
, rdev
);
2724 rdev_dec_pending(rdev
, conf
->mddev
);
2727 spin_lock_irq(&sh
->stripe_lock
);
2728 /* fail all writes first */
2729 bi
= sh
->dev
[i
].towrite
;
2730 sh
->dev
[i
].towrite
= NULL
;
2731 spin_unlock_irq(&sh
->stripe_lock
);
2735 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2736 wake_up(&conf
->wait_for_overlap
);
2738 while (bi
&& bi
->bi_sector
<
2739 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2740 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2741 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2742 if (!raid5_dec_bi_active_stripes(bi
)) {
2743 md_write_end(conf
->mddev
);
2744 bi
->bi_next
= *return_bi
;
2750 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2751 STRIPE_SECTORS
, 0, 0);
2753 /* and fail all 'written' */
2754 bi
= sh
->dev
[i
].written
;
2755 sh
->dev
[i
].written
= NULL
;
2756 if (bi
) bitmap_end
= 1;
2757 while (bi
&& bi
->bi_sector
<
2758 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2759 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2760 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2761 if (!raid5_dec_bi_active_stripes(bi
)) {
2762 md_write_end(conf
->mddev
);
2763 bi
->bi_next
= *return_bi
;
2769 /* fail any reads if this device is non-operational and
2770 * the data has not reached the cache yet.
2772 if (!test_bit(R5_Wantfill
, &sh
->dev
[i
].flags
) &&
2773 (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2774 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
))) {
2775 spin_lock_irq(&sh
->stripe_lock
);
2776 bi
= sh
->dev
[i
].toread
;
2777 sh
->dev
[i
].toread
= NULL
;
2778 spin_unlock_irq(&sh
->stripe_lock
);
2779 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2780 wake_up(&conf
->wait_for_overlap
);
2781 while (bi
&& bi
->bi_sector
<
2782 sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2783 struct bio
*nextbi
=
2784 r5_next_bio(bi
, sh
->dev
[i
].sector
);
2785 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2786 if (!raid5_dec_bi_active_stripes(bi
)) {
2787 bi
->bi_next
= *return_bi
;
2794 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2795 STRIPE_SECTORS
, 0, 0);
2796 /* If we were in the middle of a write the parity block might
2797 * still be locked - so just clear all R5_LOCKED flags
2799 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2802 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
2803 if (atomic_dec_and_test(&conf
->pending_full_writes
))
2804 md_wakeup_thread(conf
->mddev
->thread
);
2808 handle_failed_sync(struct r5conf
*conf
, struct stripe_head
*sh
,
2809 struct stripe_head_state
*s
)
2814 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2815 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
2816 wake_up(&conf
->wait_for_overlap
);
2819 /* There is nothing more to do for sync/check/repair.
2820 * Don't even need to abort as that is handled elsewhere
2821 * if needed, and not always wanted e.g. if there is a known
2823 * For recover/replace we need to record a bad block on all
2824 * non-sync devices, or abort the recovery
2826 if (test_bit(MD_RECOVERY_RECOVER
, &conf
->mddev
->recovery
)) {
2827 /* During recovery devices cannot be removed, so
2828 * locking and refcounting of rdevs is not needed
2830 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2831 struct md_rdev
*rdev
= conf
->disks
[i
].rdev
;
2833 && !test_bit(Faulty
, &rdev
->flags
)
2834 && !test_bit(In_sync
, &rdev
->flags
)
2835 && !rdev_set_badblocks(rdev
, sh
->sector
,
2838 rdev
= conf
->disks
[i
].replacement
;
2840 && !test_bit(Faulty
, &rdev
->flags
)
2841 && !test_bit(In_sync
, &rdev
->flags
)
2842 && !rdev_set_badblocks(rdev
, sh
->sector
,
2847 conf
->recovery_disabled
=
2848 conf
->mddev
->recovery_disabled
;
2850 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, !abort
);
2853 static int want_replace(struct stripe_head
*sh
, int disk_idx
)
2855 struct md_rdev
*rdev
;
2857 /* Doing recovery so rcu locking not required */
2858 rdev
= sh
->raid_conf
->disks
[disk_idx
].replacement
;
2860 && !test_bit(Faulty
, &rdev
->flags
)
2861 && !test_bit(In_sync
, &rdev
->flags
)
2862 && (rdev
->recovery_offset
<= sh
->sector
2863 || rdev
->mddev
->recovery_cp
<= sh
->sector
))
2869 /* fetch_block - checks the given member device to see if its data needs
2870 * to be read or computed to satisfy a request.
2872 * Returns 1 when no more member devices need to be checked, otherwise returns
2873 * 0 to tell the loop in handle_stripe_fill to continue
2875 static int fetch_block(struct stripe_head
*sh
, struct stripe_head_state
*s
,
2876 int disk_idx
, int disks
)
2878 struct r5dev
*dev
= &sh
->dev
[disk_idx
];
2879 struct r5dev
*fdev
[2] = { &sh
->dev
[s
->failed_num
[0]],
2880 &sh
->dev
[s
->failed_num
[1]] };
2882 /* is the data in this block needed, and can we get it? */
2883 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2884 !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2886 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2887 s
->syncing
|| s
->expanding
||
2888 (s
->replacing
&& want_replace(sh
, disk_idx
)) ||
2889 (s
->failed
>= 1 && fdev
[0]->toread
) ||
2890 (s
->failed
>= 2 && fdev
[1]->toread
) ||
2891 (sh
->raid_conf
->level
<= 5 && s
->failed
&& fdev
[0]->towrite
&&
2892 !test_bit(R5_OVERWRITE
, &fdev
[0]->flags
)) ||
2893 (sh
->raid_conf
->level
== 6 && s
->failed
&& s
->to_write
))) {
2894 /* we would like to get this block, possibly by computing it,
2895 * otherwise read it if the backing disk is insync
2897 BUG_ON(test_bit(R5_Wantcompute
, &dev
->flags
));
2898 BUG_ON(test_bit(R5_Wantread
, &dev
->flags
));
2899 if ((s
->uptodate
== disks
- 1) &&
2900 (s
->failed
&& (disk_idx
== s
->failed_num
[0] ||
2901 disk_idx
== s
->failed_num
[1]))) {
2902 /* have disk failed, and we're requested to fetch it;
2905 pr_debug("Computing stripe %llu block %d\n",
2906 (unsigned long long)sh
->sector
, disk_idx
);
2907 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2908 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2909 set_bit(R5_Wantcompute
, &dev
->flags
);
2910 sh
->ops
.target
= disk_idx
;
2911 sh
->ops
.target2
= -1; /* no 2nd target */
2913 /* Careful: from this point on 'uptodate' is in the eye
2914 * of raid_run_ops which services 'compute' operations
2915 * before writes. R5_Wantcompute flags a block that will
2916 * be R5_UPTODATE by the time it is needed for a
2917 * subsequent operation.
2921 } else if (s
->uptodate
== disks
-2 && s
->failed
>= 2) {
2922 /* Computing 2-failure is *very* expensive; only
2923 * do it if failed >= 2
2926 for (other
= disks
; other
--; ) {
2927 if (other
== disk_idx
)
2929 if (!test_bit(R5_UPTODATE
,
2930 &sh
->dev
[other
].flags
))
2934 pr_debug("Computing stripe %llu blocks %d,%d\n",
2935 (unsigned long long)sh
->sector
,
2937 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
2938 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
2939 set_bit(R5_Wantcompute
, &sh
->dev
[disk_idx
].flags
);
2940 set_bit(R5_Wantcompute
, &sh
->dev
[other
].flags
);
2941 sh
->ops
.target
= disk_idx
;
2942 sh
->ops
.target2
= other
;
2946 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2947 set_bit(R5_LOCKED
, &dev
->flags
);
2948 set_bit(R5_Wantread
, &dev
->flags
);
2950 pr_debug("Reading block %d (sync=%d)\n",
2951 disk_idx
, s
->syncing
);
2959 * handle_stripe_fill - read or compute data to satisfy pending requests.
2961 static void handle_stripe_fill(struct stripe_head
*sh
,
2962 struct stripe_head_state
*s
,
2967 /* look for blocks to read/compute, skip this if a compute
2968 * is already in flight, or if the stripe contents are in the
2969 * midst of changing due to a write
2971 if (!test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) && !sh
->check_state
&&
2972 !sh
->reconstruct_state
)
2973 for (i
= disks
; i
--; )
2974 if (fetch_block(sh
, s
, i
, disks
))
2976 set_bit(STRIPE_HANDLE
, &sh
->state
);
2980 /* handle_stripe_clean_event
2981 * any written block on an uptodate or failed drive can be returned.
2982 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2983 * never LOCKED, so we don't need to test 'failed' directly.
2985 static void handle_stripe_clean_event(struct r5conf
*conf
,
2986 struct stripe_head
*sh
, int disks
, struct bio
**return_bi
)
2990 int discard_pending
= 0;
2992 for (i
= disks
; i
--; )
2993 if (sh
->dev
[i
].written
) {
2995 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2996 (test_bit(R5_UPTODATE
, &dev
->flags
) ||
2997 test_bit(R5_Discard
, &dev
->flags
))) {
2998 /* We can return any write requests */
2999 struct bio
*wbi
, *wbi2
;
3000 pr_debug("Return write for disc %d\n", i
);
3001 if (test_and_clear_bit(R5_Discard
, &dev
->flags
))
3002 clear_bit(R5_UPTODATE
, &dev
->flags
);
3004 dev
->written
= NULL
;
3005 while (wbi
&& wbi
->bi_sector
<
3006 dev
->sector
+ STRIPE_SECTORS
) {
3007 wbi2
= r5_next_bio(wbi
, dev
->sector
);
3008 if (!raid5_dec_bi_active_stripes(wbi
)) {
3009 md_write_end(conf
->mddev
);
3010 wbi
->bi_next
= *return_bi
;
3015 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
3017 !test_bit(STRIPE_DEGRADED
, &sh
->state
),
3019 } else if (test_bit(R5_Discard
, &dev
->flags
))
3020 discard_pending
= 1;
3022 if (!discard_pending
&&
3023 test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
)) {
3024 clear_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
);
3025 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3026 if (sh
->qd_idx
>= 0) {
3027 clear_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
);
3028 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
);
3030 /* now that discard is done we can proceed with any sync */
3031 clear_bit(STRIPE_DISCARD
, &sh
->state
);
3033 * SCSI discard will change some bio fields and the stripe has
3034 * no updated data, so remove it from hash list and the stripe
3035 * will be reinitialized
3037 spin_lock_irq(&conf
->device_lock
);
3039 spin_unlock_irq(&conf
->device_lock
);
3040 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
))
3041 set_bit(STRIPE_HANDLE
, &sh
->state
);
3045 if (test_and_clear_bit(STRIPE_FULL_WRITE
, &sh
->state
))
3046 if (atomic_dec_and_test(&conf
->pending_full_writes
))
3047 md_wakeup_thread(conf
->mddev
->thread
);
3050 static void handle_stripe_dirtying(struct r5conf
*conf
,
3051 struct stripe_head
*sh
,
3052 struct stripe_head_state
*s
,
3055 int rmw
= 0, rcw
= 0, i
;
3056 sector_t recovery_cp
= conf
->mddev
->recovery_cp
;
3058 /* RAID6 requires 'rcw' in current implementation.
3059 * Otherwise, check whether resync is now happening or should start.
3060 * If yes, then the array is dirty (after unclean shutdown or
3061 * initial creation), so parity in some stripes might be inconsistent.
3062 * In this case, we need to always do reconstruct-write, to ensure
3063 * that in case of drive failure or read-error correction, we
3064 * generate correct data from the parity.
3066 if (conf
->max_degraded
== 2 ||
3067 (recovery_cp
< MaxSector
&& sh
->sector
>= recovery_cp
)) {
3068 /* Calculate the real rcw later - for now make it
3069 * look like rcw is cheaper
3072 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3073 conf
->max_degraded
, (unsigned long long)recovery_cp
,
3074 (unsigned long long)sh
->sector
);
3075 } else for (i
= disks
; i
--; ) {
3076 /* would I have to read this buffer for read_modify_write */
3077 struct r5dev
*dev
= &sh
->dev
[i
];
3078 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
3079 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3080 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3081 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3082 if (test_bit(R5_Insync
, &dev
->flags
))
3085 rmw
+= 2*disks
; /* cannot read it */
3087 /* Would I have to read this buffer for reconstruct_write */
3088 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
3089 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3090 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3091 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3092 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
3097 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
3098 (unsigned long long)sh
->sector
, rmw
, rcw
);
3099 set_bit(STRIPE_HANDLE
, &sh
->state
);
3100 if (rmw
< rcw
&& rmw
> 0) {
3101 /* prefer read-modify-write, but need to get some data */
3102 if (conf
->mddev
->queue
)
3103 blk_add_trace_msg(conf
->mddev
->queue
,
3104 "raid5 rmw %llu %d",
3105 (unsigned long long)sh
->sector
, rmw
);
3106 for (i
= disks
; i
--; ) {
3107 struct r5dev
*dev
= &sh
->dev
[i
];
3108 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
3109 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3110 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3111 test_bit(R5_Wantcompute
, &dev
->flags
)) &&
3112 test_bit(R5_Insync
, &dev
->flags
)) {
3114 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
3115 pr_debug("Read_old block "
3116 "%d for r-m-w\n", i
);
3117 set_bit(R5_LOCKED
, &dev
->flags
);
3118 set_bit(R5_Wantread
, &dev
->flags
);
3121 set_bit(STRIPE_DELAYED
, &sh
->state
);
3122 set_bit(STRIPE_HANDLE
, &sh
->state
);
3127 if (rcw
<= rmw
&& rcw
> 0) {
3128 /* want reconstruct write, but need to get some data */
3131 for (i
= disks
; i
--; ) {
3132 struct r5dev
*dev
= &sh
->dev
[i
];
3133 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) &&
3134 i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
&&
3135 !test_bit(R5_LOCKED
, &dev
->flags
) &&
3136 !(test_bit(R5_UPTODATE
, &dev
->flags
) ||
3137 test_bit(R5_Wantcompute
, &dev
->flags
))) {
3139 if (!test_bit(R5_Insync
, &dev
->flags
))
3140 continue; /* it's a failed drive */
3142 test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
3143 pr_debug("Read_old block "
3144 "%d for Reconstruct\n", i
);
3145 set_bit(R5_LOCKED
, &dev
->flags
);
3146 set_bit(R5_Wantread
, &dev
->flags
);
3150 set_bit(STRIPE_DELAYED
, &sh
->state
);
3151 set_bit(STRIPE_HANDLE
, &sh
->state
);
3155 if (rcw
&& conf
->mddev
->queue
)
3156 blk_add_trace_msg(conf
->mddev
->queue
, "raid5 rcw %llu %d %d %d",
3157 (unsigned long long)sh
->sector
,
3158 rcw
, qread
, test_bit(STRIPE_DELAYED
, &sh
->state
));
3160 /* now if nothing is locked, and if we have enough data,
3161 * we can start a write request
3163 /* since handle_stripe can be called at any time we need to handle the
3164 * case where a compute block operation has been submitted and then a
3165 * subsequent call wants to start a write request. raid_run_ops only
3166 * handles the case where compute block and reconstruct are requested
3167 * simultaneously. If this is not the case then new writes need to be
3168 * held off until the compute completes.
3170 if ((s
->req_compute
|| !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)) &&
3171 (s
->locked
== 0 && (rcw
== 0 || rmw
== 0) &&
3172 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)))
3173 schedule_reconstruction(sh
, s
, rcw
== 0, 0);
3176 static void handle_parity_checks5(struct r5conf
*conf
, struct stripe_head
*sh
,
3177 struct stripe_head_state
*s
, int disks
)
3179 struct r5dev
*dev
= NULL
;
3181 set_bit(STRIPE_HANDLE
, &sh
->state
);
3183 switch (sh
->check_state
) {
3184 case check_state_idle
:
3185 /* start a new check operation if there are no failures */
3186 if (s
->failed
== 0) {
3187 BUG_ON(s
->uptodate
!= disks
);
3188 sh
->check_state
= check_state_run
;
3189 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3190 clear_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
);
3194 dev
= &sh
->dev
[s
->failed_num
[0]];
3196 case check_state_compute_result
:
3197 sh
->check_state
= check_state_idle
;
3199 dev
= &sh
->dev
[sh
->pd_idx
];
3201 /* check that a write has not made the stripe insync */
3202 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3205 /* either failed parity check, or recovery is happening */
3206 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
3207 BUG_ON(s
->uptodate
!= disks
);
3209 set_bit(R5_LOCKED
, &dev
->flags
);
3211 set_bit(R5_Wantwrite
, &dev
->flags
);
3213 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3214 set_bit(STRIPE_INSYNC
, &sh
->state
);
3216 case check_state_run
:
3217 break; /* we will be called again upon completion */
3218 case check_state_check_result
:
3219 sh
->check_state
= check_state_idle
;
3221 /* if a failure occurred during the check operation, leave
3222 * STRIPE_INSYNC not set and let the stripe be handled again
3227 /* handle a successful check operation, if parity is correct
3228 * we are done. Otherwise update the mismatch count and repair
3229 * parity if !MD_RECOVERY_CHECK
3231 if ((sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) == 0)
3232 /* parity is correct (on disc,
3233 * not in buffer any more)
3235 set_bit(STRIPE_INSYNC
, &sh
->state
);
3237 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3238 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3239 /* don't try to repair!! */
3240 set_bit(STRIPE_INSYNC
, &sh
->state
);
3242 sh
->check_state
= check_state_compute_run
;
3243 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3244 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3245 set_bit(R5_Wantcompute
,
3246 &sh
->dev
[sh
->pd_idx
].flags
);
3247 sh
->ops
.target
= sh
->pd_idx
;
3248 sh
->ops
.target2
= -1;
3253 case check_state_compute_run
:
3256 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3257 __func__
, sh
->check_state
,
3258 (unsigned long long) sh
->sector
);
3264 static void handle_parity_checks6(struct r5conf
*conf
, struct stripe_head
*sh
,
3265 struct stripe_head_state
*s
,
3268 int pd_idx
= sh
->pd_idx
;
3269 int qd_idx
= sh
->qd_idx
;
3272 set_bit(STRIPE_HANDLE
, &sh
->state
);
3274 BUG_ON(s
->failed
> 2);
3276 /* Want to check and possibly repair P and Q.
3277 * However there could be one 'failed' device, in which
3278 * case we can only check one of them, possibly using the
3279 * other to generate missing data
3282 switch (sh
->check_state
) {
3283 case check_state_idle
:
3284 /* start a new check operation if there are < 2 failures */
3285 if (s
->failed
== s
->q_failed
) {
3286 /* The only possible failed device holds Q, so it
3287 * makes sense to check P (If anything else were failed,
3288 * we would have used P to recreate it).
3290 sh
->check_state
= check_state_run
;
3292 if (!s
->q_failed
&& s
->failed
< 2) {
3293 /* Q is not failed, and we didn't use it to generate
3294 * anything, so it makes sense to check it
3296 if (sh
->check_state
== check_state_run
)
3297 sh
->check_state
= check_state_run_pq
;
3299 sh
->check_state
= check_state_run_q
;
3302 /* discard potentially stale zero_sum_result */
3303 sh
->ops
.zero_sum_result
= 0;
3305 if (sh
->check_state
== check_state_run
) {
3306 /* async_xor_zero_sum destroys the contents of P */
3307 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
3310 if (sh
->check_state
>= check_state_run
&&
3311 sh
->check_state
<= check_state_run_pq
) {
3312 /* async_syndrome_zero_sum preserves P and Q, so
3313 * no need to mark them !uptodate here
3315 set_bit(STRIPE_OP_CHECK
, &s
->ops_request
);
3319 /* we have 2-disk failure */
3320 BUG_ON(s
->failed
!= 2);
3322 case check_state_compute_result
:
3323 sh
->check_state
= check_state_idle
;
3325 /* check that a write has not made the stripe insync */
3326 if (test_bit(STRIPE_INSYNC
, &sh
->state
))
3329 /* now write out any block on a failed drive,
3330 * or P or Q if they were recomputed
3332 BUG_ON(s
->uptodate
< disks
- 1); /* We don't need Q to recover */
3333 if (s
->failed
== 2) {
3334 dev
= &sh
->dev
[s
->failed_num
[1]];
3336 set_bit(R5_LOCKED
, &dev
->flags
);
3337 set_bit(R5_Wantwrite
, &dev
->flags
);
3339 if (s
->failed
>= 1) {
3340 dev
= &sh
->dev
[s
->failed_num
[0]];
3342 set_bit(R5_LOCKED
, &dev
->flags
);
3343 set_bit(R5_Wantwrite
, &dev
->flags
);
3345 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3346 dev
= &sh
->dev
[pd_idx
];
3348 set_bit(R5_LOCKED
, &dev
->flags
);
3349 set_bit(R5_Wantwrite
, &dev
->flags
);
3351 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3352 dev
= &sh
->dev
[qd_idx
];
3354 set_bit(R5_LOCKED
, &dev
->flags
);
3355 set_bit(R5_Wantwrite
, &dev
->flags
);
3357 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
3359 set_bit(STRIPE_INSYNC
, &sh
->state
);
3361 case check_state_run
:
3362 case check_state_run_q
:
3363 case check_state_run_pq
:
3364 break; /* we will be called again upon completion */
3365 case check_state_check_result
:
3366 sh
->check_state
= check_state_idle
;
3368 /* handle a successful check operation, if parity is correct
3369 * we are done. Otherwise update the mismatch count and repair
3370 * parity if !MD_RECOVERY_CHECK
3372 if (sh
->ops
.zero_sum_result
== 0) {
3373 /* both parities are correct */
3375 set_bit(STRIPE_INSYNC
, &sh
->state
);
3377 /* in contrast to the raid5 case we can validate
3378 * parity, but still have a failure to write
3381 sh
->check_state
= check_state_compute_result
;
3382 /* Returning at this point means that we may go
3383 * off and bring p and/or q uptodate again so
3384 * we make sure to check zero_sum_result again
3385 * to verify if p or q need writeback
3389 atomic64_add(STRIPE_SECTORS
, &conf
->mddev
->resync_mismatches
);
3390 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
3391 /* don't try to repair!! */
3392 set_bit(STRIPE_INSYNC
, &sh
->state
);
3394 int *target
= &sh
->ops
.target
;
3396 sh
->ops
.target
= -1;
3397 sh
->ops
.target2
= -1;
3398 sh
->check_state
= check_state_compute_run
;
3399 set_bit(STRIPE_COMPUTE_RUN
, &sh
->state
);
3400 set_bit(STRIPE_OP_COMPUTE_BLK
, &s
->ops_request
);
3401 if (sh
->ops
.zero_sum_result
& SUM_CHECK_P_RESULT
) {
3402 set_bit(R5_Wantcompute
,
3403 &sh
->dev
[pd_idx
].flags
);
3405 target
= &sh
->ops
.target2
;
3408 if (sh
->ops
.zero_sum_result
& SUM_CHECK_Q_RESULT
) {
3409 set_bit(R5_Wantcompute
,
3410 &sh
->dev
[qd_idx
].flags
);
3417 case check_state_compute_run
:
3420 printk(KERN_ERR
"%s: unknown check_state: %d sector: %llu\n",
3421 __func__
, sh
->check_state
,
3422 (unsigned long long) sh
->sector
);
3427 static void handle_stripe_expansion(struct r5conf
*conf
, struct stripe_head
*sh
)
3431 /* We have read all the blocks in this stripe and now we need to
3432 * copy some of them into a target stripe for expand.
3434 struct dma_async_tx_descriptor
*tx
= NULL
;
3435 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3436 for (i
= 0; i
< sh
->disks
; i
++)
3437 if (i
!= sh
->pd_idx
&& i
!= sh
->qd_idx
) {
3439 struct stripe_head
*sh2
;
3440 struct async_submit_ctl submit
;
3442 sector_t bn
= compute_blocknr(sh
, i
, 1);
3443 sector_t s
= raid5_compute_sector(conf
, bn
, 0,
3445 sh2
= get_active_stripe(conf
, s
, 0, 1, 1);
3447 /* so far only the early blocks of this stripe
3448 * have been requested. When later blocks
3449 * get requested, we will try again
3452 if (!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
3453 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
3454 /* must have already done this block */
3455 release_stripe(sh2
);
3459 /* place all the copies on one channel */
3460 init_async_submit(&submit
, 0, tx
, NULL
, NULL
, NULL
);
3461 tx
= async_memcpy(sh2
->dev
[dd_idx
].page
,
3462 sh
->dev
[i
].page
, 0, 0, STRIPE_SIZE
,
3465 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
3466 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
3467 for (j
= 0; j
< conf
->raid_disks
; j
++)
3468 if (j
!= sh2
->pd_idx
&&
3470 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
3472 if (j
== conf
->raid_disks
) {
3473 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
3474 set_bit(STRIPE_HANDLE
, &sh2
->state
);
3476 release_stripe(sh2
);
3479 /* done submitting copies, wait for them to complete */
3480 async_tx_quiesce(&tx
);
3484 * handle_stripe - do things to a stripe.
3486 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3487 * state of various bits to see what needs to be done.
3489 * return some read requests which now have data
3490 * return some write requests which are safely on storage
3491 * schedule a read on some buffers
3492 * schedule a write of some buffers
3493 * return confirmation of parity correctness
3497 static void analyse_stripe(struct stripe_head
*sh
, struct stripe_head_state
*s
)
3499 struct r5conf
*conf
= sh
->raid_conf
;
3500 int disks
= sh
->disks
;
3503 int do_recovery
= 0;
3505 memset(s
, 0, sizeof(*s
));
3507 s
->expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
3508 s
->expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3509 s
->failed_num
[0] = -1;
3510 s
->failed_num
[1] = -1;
3512 /* Now to look around and see what can be done */
3514 for (i
=disks
; i
--; ) {
3515 struct md_rdev
*rdev
;
3522 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3524 dev
->toread
, dev
->towrite
, dev
->written
);
3525 /* maybe we can reply to a read
3527 * new wantfill requests are only permitted while
3528 * ops_complete_biofill is guaranteed to be inactive
3530 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
&&
3531 !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
))
3532 set_bit(R5_Wantfill
, &dev
->flags
);
3534 /* now count some things */
3535 if (test_bit(R5_LOCKED
, &dev
->flags
))
3537 if (test_bit(R5_UPTODATE
, &dev
->flags
))
3539 if (test_bit(R5_Wantcompute
, &dev
->flags
)) {
3541 BUG_ON(s
->compute
> 2);
3544 if (test_bit(R5_Wantfill
, &dev
->flags
))
3546 else if (dev
->toread
)
3550 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
3555 /* Prefer to use the replacement for reads, but only
3556 * if it is recovered enough and has no bad blocks.
3558 rdev
= rcu_dereference(conf
->disks
[i
].replacement
);
3559 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) &&
3560 rdev
->recovery_offset
>= sh
->sector
+ STRIPE_SECTORS
&&
3561 !is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3562 &first_bad
, &bad_sectors
))
3563 set_bit(R5_ReadRepl
, &dev
->flags
);
3566 set_bit(R5_NeedReplace
, &dev
->flags
);
3567 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
3568 clear_bit(R5_ReadRepl
, &dev
->flags
);
3570 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
3573 is_bad
= is_badblock(rdev
, sh
->sector
, STRIPE_SECTORS
,
3574 &first_bad
, &bad_sectors
);
3575 if (s
->blocked_rdev
== NULL
3576 && (test_bit(Blocked
, &rdev
->flags
)
3579 set_bit(BlockedBadBlocks
,
3581 s
->blocked_rdev
= rdev
;
3582 atomic_inc(&rdev
->nr_pending
);
3585 clear_bit(R5_Insync
, &dev
->flags
);
3589 /* also not in-sync */
3590 if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
3591 test_bit(R5_UPTODATE
, &dev
->flags
)) {
3592 /* treat as in-sync, but with a read error
3593 * which we can now try to correct
3595 set_bit(R5_Insync
, &dev
->flags
);
3596 set_bit(R5_ReadError
, &dev
->flags
);
3598 } else if (test_bit(In_sync
, &rdev
->flags
))
3599 set_bit(R5_Insync
, &dev
->flags
);
3600 else if (sh
->sector
+ STRIPE_SECTORS
<= rdev
->recovery_offset
)
3601 /* in sync if before recovery_offset */
3602 set_bit(R5_Insync
, &dev
->flags
);
3603 else if (test_bit(R5_UPTODATE
, &dev
->flags
) &&
3604 test_bit(R5_Expanded
, &dev
->flags
))
3605 /* If we've reshaped into here, we assume it is Insync.
3606 * We will shortly update recovery_offset to make
3609 set_bit(R5_Insync
, &dev
->flags
);
3611 if (rdev
&& test_bit(R5_WriteError
, &dev
->flags
)) {
3612 /* This flag does not apply to '.replacement'
3613 * only to .rdev, so make sure to check that*/
3614 struct md_rdev
*rdev2
= rcu_dereference(
3615 conf
->disks
[i
].rdev
);
3617 clear_bit(R5_Insync
, &dev
->flags
);
3618 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3619 s
->handle_bad_blocks
= 1;
3620 atomic_inc(&rdev2
->nr_pending
);
3622 clear_bit(R5_WriteError
, &dev
->flags
);
3624 if (rdev
&& test_bit(R5_MadeGood
, &dev
->flags
)) {
3625 /* This flag does not apply to '.replacement'
3626 * only to .rdev, so make sure to check that*/
3627 struct md_rdev
*rdev2
= rcu_dereference(
3628 conf
->disks
[i
].rdev
);
3629 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3630 s
->handle_bad_blocks
= 1;
3631 atomic_inc(&rdev2
->nr_pending
);
3633 clear_bit(R5_MadeGood
, &dev
->flags
);
3635 if (test_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3636 struct md_rdev
*rdev2
= rcu_dereference(
3637 conf
->disks
[i
].replacement
);
3638 if (rdev2
&& !test_bit(Faulty
, &rdev2
->flags
)) {
3639 s
->handle_bad_blocks
= 1;
3640 atomic_inc(&rdev2
->nr_pending
);
3642 clear_bit(R5_MadeGoodRepl
, &dev
->flags
);
3644 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3645 /* The ReadError flag will just be confusing now */
3646 clear_bit(R5_ReadError
, &dev
->flags
);
3647 clear_bit(R5_ReWrite
, &dev
->flags
);
3649 if (test_bit(R5_ReadError
, &dev
->flags
))
3650 clear_bit(R5_Insync
, &dev
->flags
);
3651 if (!test_bit(R5_Insync
, &dev
->flags
)) {
3653 s
->failed_num
[s
->failed
] = i
;
3655 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
))
3659 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
3660 /* If there is a failed device being replaced,
3661 * we must be recovering.
3662 * else if we are after recovery_cp, we must be syncing
3663 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
3664 * else we can only be replacing
3665 * sync and recovery both need to read all devices, and so
3666 * use the same flag.
3669 sh
->sector
>= conf
->mddev
->recovery_cp
||
3670 test_bit(MD_RECOVERY_REQUESTED
, &(conf
->mddev
->recovery
)))
3678 static void handle_stripe(struct stripe_head
*sh
)
3680 struct stripe_head_state s
;
3681 struct r5conf
*conf
= sh
->raid_conf
;
3684 int disks
= sh
->disks
;
3685 struct r5dev
*pdev
, *qdev
;
3687 clear_bit(STRIPE_HANDLE
, &sh
->state
);
3688 if (test_and_set_bit_lock(STRIPE_ACTIVE
, &sh
->state
)) {
3689 /* already being handled, ensure it gets handled
3690 * again when current action finishes */
3691 set_bit(STRIPE_HANDLE
, &sh
->state
);
3695 if (test_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3696 spin_lock(&sh
->stripe_lock
);
3697 /* Cannot process 'sync' concurrently with 'discard' */
3698 if (!test_bit(STRIPE_DISCARD
, &sh
->state
) &&
3699 test_and_clear_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
)) {
3700 set_bit(STRIPE_SYNCING
, &sh
->state
);
3701 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3702 clear_bit(STRIPE_REPLACED
, &sh
->state
);
3704 spin_unlock(&sh
->stripe_lock
);
3706 clear_bit(STRIPE_DELAYED
, &sh
->state
);
3708 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3709 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3710 (unsigned long long)sh
->sector
, sh
->state
,
3711 atomic_read(&sh
->count
), sh
->pd_idx
, sh
->qd_idx
,
3712 sh
->check_state
, sh
->reconstruct_state
);
3714 analyse_stripe(sh
, &s
);
3716 if (s
.handle_bad_blocks
) {
3717 set_bit(STRIPE_HANDLE
, &sh
->state
);
3721 if (unlikely(s
.blocked_rdev
)) {
3722 if (s
.syncing
|| s
.expanding
|| s
.expanded
||
3723 s
.replacing
|| s
.to_write
|| s
.written
) {
3724 set_bit(STRIPE_HANDLE
, &sh
->state
);
3727 /* There is nothing for the blocked_rdev to block */
3728 rdev_dec_pending(s
.blocked_rdev
, conf
->mddev
);
3729 s
.blocked_rdev
= NULL
;
3732 if (s
.to_fill
&& !test_bit(STRIPE_BIOFILL_RUN
, &sh
->state
)) {
3733 set_bit(STRIPE_OP_BIOFILL
, &s
.ops_request
);
3734 set_bit(STRIPE_BIOFILL_RUN
, &sh
->state
);
3737 pr_debug("locked=%d uptodate=%d to_read=%d"
3738 " to_write=%d failed=%d failed_num=%d,%d\n",
3739 s
.locked
, s
.uptodate
, s
.to_read
, s
.to_write
, s
.failed
,
3740 s
.failed_num
[0], s
.failed_num
[1]);
3741 /* check if the array has lost more than max_degraded devices and,
3742 * if so, some requests might need to be failed.
3744 if (s
.failed
> conf
->max_degraded
) {
3745 sh
->check_state
= 0;
3746 sh
->reconstruct_state
= 0;
3747 if (s
.to_read
+s
.to_write
+s
.written
)
3748 handle_failed_stripe(conf
, sh
, &s
, disks
, &s
.return_bi
);
3749 if (s
.syncing
+ s
.replacing
)
3750 handle_failed_sync(conf
, sh
, &s
);
3753 /* Now we check to see if any write operations have recently
3757 if (sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
)
3759 if (sh
->reconstruct_state
== reconstruct_state_drain_result
||
3760 sh
->reconstruct_state
== reconstruct_state_prexor_drain_result
) {
3761 sh
->reconstruct_state
= reconstruct_state_idle
;
3763 /* All the 'written' buffers and the parity block are ready to
3764 * be written back to disk
3766 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[sh
->pd_idx
].flags
) &&
3767 !test_bit(R5_Discard
, &sh
->dev
[sh
->pd_idx
].flags
));
3768 BUG_ON(sh
->qd_idx
>= 0 &&
3769 !test_bit(R5_UPTODATE
, &sh
->dev
[sh
->qd_idx
].flags
) &&
3770 !test_bit(R5_Discard
, &sh
->dev
[sh
->qd_idx
].flags
));
3771 for (i
= disks
; i
--; ) {
3772 struct r5dev
*dev
= &sh
->dev
[i
];
3773 if (test_bit(R5_LOCKED
, &dev
->flags
) &&
3774 (i
== sh
->pd_idx
|| i
== sh
->qd_idx
||
3776 pr_debug("Writing block %d\n", i
);
3777 set_bit(R5_Wantwrite
, &dev
->flags
);
3780 if (!test_bit(R5_Insync
, &dev
->flags
) ||
3781 ((i
== sh
->pd_idx
|| i
== sh
->qd_idx
) &&
3783 set_bit(STRIPE_INSYNC
, &sh
->state
);
3786 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
3787 s
.dec_preread_active
= 1;
3791 * might be able to return some write requests if the parity blocks
3792 * are safe, or on a failed drive
3794 pdev
= &sh
->dev
[sh
->pd_idx
];
3795 s
.p_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->pd_idx
)
3796 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->pd_idx
);
3797 qdev
= &sh
->dev
[sh
->qd_idx
];
3798 s
.q_failed
= (s
.failed
>= 1 && s
.failed_num
[0] == sh
->qd_idx
)
3799 || (s
.failed
>= 2 && s
.failed_num
[1] == sh
->qd_idx
)
3803 (s
.p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
3804 && !test_bit(R5_LOCKED
, &pdev
->flags
)
3805 && (test_bit(R5_UPTODATE
, &pdev
->flags
) ||
3806 test_bit(R5_Discard
, &pdev
->flags
))))) &&
3807 (s
.q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
3808 && !test_bit(R5_LOCKED
, &qdev
->flags
)
3809 && (test_bit(R5_UPTODATE
, &qdev
->flags
) ||
3810 test_bit(R5_Discard
, &qdev
->flags
))))))
3811 handle_stripe_clean_event(conf
, sh
, disks
, &s
.return_bi
);
3813 /* Now we might consider reading some blocks, either to check/generate
3814 * parity, or to satisfy requests
3815 * or to load a block that is being partially written.
3817 if (s
.to_read
|| s
.non_overwrite
3818 || (conf
->level
== 6 && s
.to_write
&& s
.failed
)
3819 || (s
.syncing
&& (s
.uptodate
+ s
.compute
< disks
))
3822 handle_stripe_fill(sh
, &s
, disks
);
3824 /* Now to consider new write requests and what else, if anything
3825 * should be read. We do not handle new writes when:
3826 * 1/ A 'write' operation (copy+xor) is already in flight.
3827 * 2/ A 'check' operation is in flight, as it may clobber the parity
3830 if (s
.to_write
&& !sh
->reconstruct_state
&& !sh
->check_state
)
3831 handle_stripe_dirtying(conf
, sh
, &s
, disks
);
3833 /* maybe we need to check and possibly fix the parity for this stripe
3834 * Any reads will already have been scheduled, so we just see if enough
3835 * data is available. The parity check is held off while parity
3836 * dependent operations are in flight.
3838 if (sh
->check_state
||
3839 (s
.syncing
&& s
.locked
== 0 &&
3840 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3841 !test_bit(STRIPE_INSYNC
, &sh
->state
))) {
3842 if (conf
->level
== 6)
3843 handle_parity_checks6(conf
, sh
, &s
, disks
);
3845 handle_parity_checks5(conf
, sh
, &s
, disks
);
3848 if ((s
.replacing
|| s
.syncing
) && s
.locked
== 0
3849 && !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
)
3850 && !test_bit(STRIPE_REPLACED
, &sh
->state
)) {
3851 /* Write out to replacement devices where possible */
3852 for (i
= 0; i
< conf
->raid_disks
; i
++)
3853 if (test_bit(R5_NeedReplace
, &sh
->dev
[i
].flags
)) {
3854 WARN_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
));
3855 set_bit(R5_WantReplace
, &sh
->dev
[i
].flags
);
3856 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3860 set_bit(STRIPE_INSYNC
, &sh
->state
);
3861 set_bit(STRIPE_REPLACED
, &sh
->state
);
3863 if ((s
.syncing
|| s
.replacing
) && s
.locked
== 0 &&
3864 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
) &&
3865 test_bit(STRIPE_INSYNC
, &sh
->state
)) {
3866 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3867 clear_bit(STRIPE_SYNCING
, &sh
->state
);
3868 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
))
3869 wake_up(&conf
->wait_for_overlap
);
3872 /* If the failed drives are just a ReadError, then we might need
3873 * to progress the repair/check process
3875 if (s
.failed
<= conf
->max_degraded
&& !conf
->mddev
->ro
)
3876 for (i
= 0; i
< s
.failed
; i
++) {
3877 struct r5dev
*dev
= &sh
->dev
[s
.failed_num
[i
]];
3878 if (test_bit(R5_ReadError
, &dev
->flags
)
3879 && !test_bit(R5_LOCKED
, &dev
->flags
)
3880 && test_bit(R5_UPTODATE
, &dev
->flags
)
3882 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
3883 set_bit(R5_Wantwrite
, &dev
->flags
);
3884 set_bit(R5_ReWrite
, &dev
->flags
);
3885 set_bit(R5_LOCKED
, &dev
->flags
);
3888 /* let's read it back */
3889 set_bit(R5_Wantread
, &dev
->flags
);
3890 set_bit(R5_LOCKED
, &dev
->flags
);
3897 /* Finish reconstruct operations initiated by the expansion process */
3898 if (sh
->reconstruct_state
== reconstruct_state_result
) {
3899 struct stripe_head
*sh_src
3900 = get_active_stripe(conf
, sh
->sector
, 1, 1, 1);
3901 if (sh_src
&& test_bit(STRIPE_EXPAND_SOURCE
, &sh_src
->state
)) {
3902 /* sh cannot be written until sh_src has been read.
3903 * so arrange for sh to be delayed a little
3905 set_bit(STRIPE_DELAYED
, &sh
->state
);
3906 set_bit(STRIPE_HANDLE
, &sh
->state
);
3907 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
,
3909 atomic_inc(&conf
->preread_active_stripes
);
3910 release_stripe(sh_src
);
3914 release_stripe(sh_src
);
3916 sh
->reconstruct_state
= reconstruct_state_idle
;
3917 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
3918 for (i
= conf
->raid_disks
; i
--; ) {
3919 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
3920 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
3925 if (s
.expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
) &&
3926 !sh
->reconstruct_state
) {
3927 /* Need to write out all blocks after computing parity */
3928 sh
->disks
= conf
->raid_disks
;
3929 stripe_set_idx(sh
->sector
, conf
, 0, sh
);
3930 schedule_reconstruction(sh
, &s
, 1, 1);
3931 } else if (s
.expanded
&& !sh
->reconstruct_state
&& s
.locked
== 0) {
3932 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
3933 atomic_dec(&conf
->reshape_stripes
);
3934 wake_up(&conf
->wait_for_overlap
);
3935 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
3938 if (s
.expanding
&& s
.locked
== 0 &&
3939 !test_bit(STRIPE_COMPUTE_RUN
, &sh
->state
))
3940 handle_stripe_expansion(conf
, sh
);
3943 /* wait for this device to become unblocked */
3944 if (unlikely(s
.blocked_rdev
)) {
3945 if (conf
->mddev
->external
)
3946 md_wait_for_blocked_rdev(s
.blocked_rdev
,
3949 /* Internal metadata will immediately
3950 * be written by raid5d, so we don't
3951 * need to wait here.
3953 rdev_dec_pending(s
.blocked_rdev
,
3957 if (s
.handle_bad_blocks
)
3958 for (i
= disks
; i
--; ) {
3959 struct md_rdev
*rdev
;
3960 struct r5dev
*dev
= &sh
->dev
[i
];
3961 if (test_and_clear_bit(R5_WriteError
, &dev
->flags
)) {
3962 /* We own a safe reference to the rdev */
3963 rdev
= conf
->disks
[i
].rdev
;
3964 if (!rdev_set_badblocks(rdev
, sh
->sector
,
3966 md_error(conf
->mddev
, rdev
);
3967 rdev_dec_pending(rdev
, conf
->mddev
);
3969 if (test_and_clear_bit(R5_MadeGood
, &dev
->flags
)) {
3970 rdev
= conf
->disks
[i
].rdev
;
3971 rdev_clear_badblocks(rdev
, sh
->sector
,
3973 rdev_dec_pending(rdev
, conf
->mddev
);
3975 if (test_and_clear_bit(R5_MadeGoodRepl
, &dev
->flags
)) {
3976 rdev
= conf
->disks
[i
].replacement
;
3978 /* rdev have been moved down */
3979 rdev
= conf
->disks
[i
].rdev
;
3980 rdev_clear_badblocks(rdev
, sh
->sector
,
3982 rdev_dec_pending(rdev
, conf
->mddev
);
3987 raid_run_ops(sh
, s
.ops_request
);
3991 if (s
.dec_preread_active
) {
3992 /* We delay this until after ops_run_io so that if make_request
3993 * is waiting on a flush, it won't continue until the writes
3994 * have actually been submitted.
3996 atomic_dec(&conf
->preread_active_stripes
);
3997 if (atomic_read(&conf
->preread_active_stripes
) <
3999 md_wakeup_thread(conf
->mddev
->thread
);
4002 return_io(s
.return_bi
);
4004 clear_bit_unlock(STRIPE_ACTIVE
, &sh
->state
);
4007 static void raid5_activate_delayed(struct r5conf
*conf
)
4009 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
4010 while (!list_empty(&conf
->delayed_list
)) {
4011 struct list_head
*l
= conf
->delayed_list
.next
;
4012 struct stripe_head
*sh
;
4013 sh
= list_entry(l
, struct stripe_head
, lru
);
4015 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4016 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4017 atomic_inc(&conf
->preread_active_stripes
);
4018 list_add_tail(&sh
->lru
, &conf
->hold_list
);
4019 raid5_wakeup_stripe_thread(sh
);
4024 static void activate_bit_delay(struct r5conf
*conf
,
4025 struct list_head
*temp_inactive_list
)
4027 /* device_lock is held */
4028 struct list_head head
;
4029 list_add(&head
, &conf
->bitmap_list
);
4030 list_del_init(&conf
->bitmap_list
);
4031 while (!list_empty(&head
)) {
4032 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
4034 list_del_init(&sh
->lru
);
4035 atomic_inc(&sh
->count
);
4036 hash
= sh
->hash_lock_index
;
4037 __release_stripe(conf
, sh
, &temp_inactive_list
[hash
]);
4041 int md_raid5_congested(struct mddev
*mddev
, int bits
)
4043 struct r5conf
*conf
= mddev
->private;
4045 /* No difference between reads and writes. Just check
4046 * how busy the stripe_cache is
4049 if (conf
->inactive_blocked
)
4053 if (atomic_read(&conf
->empty_inactive_list_nr
))
4058 EXPORT_SYMBOL_GPL(md_raid5_congested
);
4060 static int raid5_congested(void *data
, int bits
)
4062 struct mddev
*mddev
= data
;
4064 return mddev_congested(mddev
, bits
) ||
4065 md_raid5_congested(mddev
, bits
);
4068 /* We want read requests to align with chunks where possible,
4069 * but write requests don't need to.
4071 static int raid5_mergeable_bvec(struct request_queue
*q
,
4072 struct bvec_merge_data
*bvm
,
4073 struct bio_vec
*biovec
)
4075 struct mddev
*mddev
= q
->queuedata
;
4076 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
4078 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
4079 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
4081 if ((bvm
->bi_rw
& 1) == WRITE
)
4082 return biovec
->bv_len
; /* always allow writes to be mergeable */
4084 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
4085 chunk_sectors
= mddev
->new_chunk_sectors
;
4086 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
4087 if (max
< 0) max
= 0;
4088 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
4089 return biovec
->bv_len
;
4095 static int in_chunk_boundary(struct mddev
*mddev
, struct bio
*bio
)
4097 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
4098 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
4099 unsigned int bio_sectors
= bio_sectors(bio
);
4101 if (mddev
->new_chunk_sectors
< mddev
->chunk_sectors
)
4102 chunk_sectors
= mddev
->new_chunk_sectors
;
4103 return chunk_sectors
>=
4104 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
4108 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4109 * later sampled by raid5d.
4111 static void add_bio_to_retry(struct bio
*bi
,struct r5conf
*conf
)
4113 unsigned long flags
;
4115 spin_lock_irqsave(&conf
->device_lock
, flags
);
4117 bi
->bi_next
= conf
->retry_read_aligned_list
;
4118 conf
->retry_read_aligned_list
= bi
;
4120 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
4121 md_wakeup_thread(conf
->mddev
->thread
);
4125 static struct bio
*remove_bio_from_retry(struct r5conf
*conf
)
4129 bi
= conf
->retry_read_aligned
;
4131 conf
->retry_read_aligned
= NULL
;
4134 bi
= conf
->retry_read_aligned_list
;
4136 conf
->retry_read_aligned_list
= bi
->bi_next
;
4139 * this sets the active strip count to 1 and the processed
4140 * strip count to zero (upper 8 bits)
4142 raid5_set_bi_stripes(bi
, 1); /* biased count of active stripes */
4150 * The "raid5_align_endio" should check if the read succeeded and if it
4151 * did, call bio_endio on the original bio (having bio_put the new bio
4153 * If the read failed..
4155 static void raid5_align_endio(struct bio
*bi
, int error
)
4157 struct bio
* raid_bi
= bi
->bi_private
;
4158 struct mddev
*mddev
;
4159 struct r5conf
*conf
;
4160 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4161 struct md_rdev
*rdev
;
4165 rdev
= (void*)raid_bi
->bi_next
;
4166 raid_bi
->bi_next
= NULL
;
4167 mddev
= rdev
->mddev
;
4168 conf
= mddev
->private;
4170 rdev_dec_pending(rdev
, conf
->mddev
);
4172 if (!error
&& uptodate
) {
4173 trace_block_bio_complete(bdev_get_queue(raid_bi
->bi_bdev
),
4175 bio_endio(raid_bi
, 0);
4176 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
4177 wake_up(&conf
->wait_for_stripe
);
4182 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
4184 add_bio_to_retry(raid_bi
, conf
);
4187 static int bio_fits_rdev(struct bio
*bi
)
4189 struct request_queue
*q
= bdev_get_queue(bi
->bi_bdev
);
4191 if (bio_sectors(bi
) > queue_max_sectors(q
))
4193 blk_recount_segments(q
, bi
);
4194 if (bi
->bi_phys_segments
> queue_max_segments(q
))
4197 if (q
->merge_bvec_fn
)
4198 /* it's too hard to apply the merge_bvec_fn at this stage,
4207 static int chunk_aligned_read(struct mddev
*mddev
, struct bio
* raid_bio
)
4209 struct r5conf
*conf
= mddev
->private;
4211 struct bio
* align_bi
;
4212 struct md_rdev
*rdev
;
4213 sector_t end_sector
;
4215 if (!in_chunk_boundary(mddev
, raid_bio
)) {
4216 pr_debug("chunk_aligned_read : non aligned\n");
4220 * use bio_clone_mddev to make a copy of the bio
4222 align_bi
= bio_clone_mddev(raid_bio
, GFP_NOIO
, mddev
);
4226 * set bi_end_io to a new function, and set bi_private to the
4229 align_bi
->bi_end_io
= raid5_align_endio
;
4230 align_bi
->bi_private
= raid_bio
;
4234 align_bi
->bi_sector
= raid5_compute_sector(conf
, raid_bio
->bi_sector
,
4238 end_sector
= bio_end_sector(align_bi
);
4240 rdev
= rcu_dereference(conf
->disks
[dd_idx
].replacement
);
4241 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
4242 rdev
->recovery_offset
< end_sector
) {
4243 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
4245 (test_bit(Faulty
, &rdev
->flags
) ||
4246 !(test_bit(In_sync
, &rdev
->flags
) ||
4247 rdev
->recovery_offset
>= end_sector
)))
4254 atomic_inc(&rdev
->nr_pending
);
4256 raid_bio
->bi_next
= (void*)rdev
;
4257 align_bi
->bi_bdev
= rdev
->bdev
;
4258 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
4260 if (!bio_fits_rdev(align_bi
) ||
4261 is_badblock(rdev
, align_bi
->bi_sector
, bio_sectors(align_bi
),
4262 &first_bad
, &bad_sectors
)) {
4263 /* too big in some way, or has a known bad block */
4265 rdev_dec_pending(rdev
, mddev
);
4269 /* No reshape active, so we can trust rdev->data_offset */
4270 align_bi
->bi_sector
+= rdev
->data_offset
;
4272 spin_lock_irq(&conf
->device_lock
);
4273 wait_event_lock_irq(conf
->wait_for_stripe
,
4276 atomic_inc(&conf
->active_aligned_reads
);
4277 spin_unlock_irq(&conf
->device_lock
);
4280 trace_block_bio_remap(bdev_get_queue(align_bi
->bi_bdev
),
4281 align_bi
, disk_devt(mddev
->gendisk
),
4282 raid_bio
->bi_sector
);
4283 generic_make_request(align_bi
);
4292 /* __get_priority_stripe - get the next stripe to process
4294 * Full stripe writes are allowed to pass preread active stripes up until
4295 * the bypass_threshold is exceeded. In general the bypass_count
4296 * increments when the handle_list is handled before the hold_list; however, it
4297 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4298 * stripe with in flight i/o. The bypass_count will be reset when the
4299 * head of the hold_list has changed, i.e. the head was promoted to the
4302 static struct stripe_head
*__get_priority_stripe(struct r5conf
*conf
, int group
)
4304 struct stripe_head
*sh
= NULL
, *tmp
;
4305 struct list_head
*handle_list
= NULL
;
4306 struct r5worker_group
*wg
= NULL
;
4308 if (conf
->worker_cnt_per_group
== 0) {
4309 handle_list
= &conf
->handle_list
;
4310 } else if (group
!= ANY_GROUP
) {
4311 handle_list
= &conf
->worker_groups
[group
].handle_list
;
4312 wg
= &conf
->worker_groups
[group
];
4315 for (i
= 0; i
< conf
->group_cnt
; i
++) {
4316 handle_list
= &conf
->worker_groups
[i
].handle_list
;
4317 wg
= &conf
->worker_groups
[i
];
4318 if (!list_empty(handle_list
))
4323 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4325 list_empty(handle_list
) ? "empty" : "busy",
4326 list_empty(&conf
->hold_list
) ? "empty" : "busy",
4327 atomic_read(&conf
->pending_full_writes
), conf
->bypass_count
);
4329 if (!list_empty(handle_list
)) {
4330 sh
= list_entry(handle_list
->next
, typeof(*sh
), lru
);
4332 if (list_empty(&conf
->hold_list
))
4333 conf
->bypass_count
= 0;
4334 else if (!test_bit(STRIPE_IO_STARTED
, &sh
->state
)) {
4335 if (conf
->hold_list
.next
== conf
->last_hold
)
4336 conf
->bypass_count
++;
4338 conf
->last_hold
= conf
->hold_list
.next
;
4339 conf
->bypass_count
-= conf
->bypass_threshold
;
4340 if (conf
->bypass_count
< 0)
4341 conf
->bypass_count
= 0;
4344 } else if (!list_empty(&conf
->hold_list
) &&
4345 ((conf
->bypass_threshold
&&
4346 conf
->bypass_count
> conf
->bypass_threshold
) ||
4347 atomic_read(&conf
->pending_full_writes
) == 0)) {
4349 list_for_each_entry(tmp
, &conf
->hold_list
, lru
) {
4350 if (conf
->worker_cnt_per_group
== 0 ||
4351 group
== ANY_GROUP
||
4352 !cpu_online(tmp
->cpu
) ||
4353 cpu_to_group(tmp
->cpu
) == group
) {
4360 conf
->bypass_count
-= conf
->bypass_threshold
;
4361 if (conf
->bypass_count
< 0)
4362 conf
->bypass_count
= 0;
4374 list_del_init(&sh
->lru
);
4375 atomic_inc(&sh
->count
);
4376 BUG_ON(atomic_read(&sh
->count
) != 1);
4380 struct raid5_plug_cb
{
4381 struct blk_plug_cb cb
;
4382 struct list_head list
;
4383 struct list_head temp_inactive_list
[NR_STRIPE_HASH_LOCKS
];
4386 static void raid5_unplug(struct blk_plug_cb
*blk_cb
, bool from_schedule
)
4388 struct raid5_plug_cb
*cb
= container_of(
4389 blk_cb
, struct raid5_plug_cb
, cb
);
4390 struct stripe_head
*sh
;
4391 struct mddev
*mddev
= cb
->cb
.data
;
4392 struct r5conf
*conf
= mddev
->private;
4396 if (cb
->list
.next
&& !list_empty(&cb
->list
)) {
4397 spin_lock_irq(&conf
->device_lock
);
4398 while (!list_empty(&cb
->list
)) {
4399 sh
= list_first_entry(&cb
->list
, struct stripe_head
, lru
);
4400 list_del_init(&sh
->lru
);
4402 * avoid race release_stripe_plug() sees
4403 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4404 * is still in our list
4406 smp_mb__before_clear_bit();
4407 clear_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
);
4409 * STRIPE_ON_RELEASE_LIST could be set here. In that
4410 * case, the count is always > 1 here
4412 hash
= sh
->hash_lock_index
;
4413 __release_stripe(conf
, sh
, &cb
->temp_inactive_list
[hash
]);
4416 spin_unlock_irq(&conf
->device_lock
);
4418 release_inactive_stripe_list(conf
, cb
->temp_inactive_list
,
4419 NR_STRIPE_HASH_LOCKS
);
4421 trace_block_unplug(mddev
->queue
, cnt
, !from_schedule
);
4425 static void release_stripe_plug(struct mddev
*mddev
,
4426 struct stripe_head
*sh
)
4428 struct blk_plug_cb
*blk_cb
= blk_check_plugged(
4429 raid5_unplug
, mddev
,
4430 sizeof(struct raid5_plug_cb
));
4431 struct raid5_plug_cb
*cb
;
4438 cb
= container_of(blk_cb
, struct raid5_plug_cb
, cb
);
4440 if (cb
->list
.next
== NULL
) {
4442 INIT_LIST_HEAD(&cb
->list
);
4443 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
4444 INIT_LIST_HEAD(cb
->temp_inactive_list
+ i
);
4447 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST
, &sh
->state
))
4448 list_add_tail(&sh
->lru
, &cb
->list
);
4453 static void make_discard_request(struct mddev
*mddev
, struct bio
*bi
)
4455 struct r5conf
*conf
= mddev
->private;
4456 sector_t logical_sector
, last_sector
;
4457 struct stripe_head
*sh
;
4461 if (mddev
->reshape_position
!= MaxSector
)
4462 /* Skip discard while reshape is happening */
4465 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4466 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
4469 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
4471 stripe_sectors
= conf
->chunk_sectors
*
4472 (conf
->raid_disks
- conf
->max_degraded
);
4473 logical_sector
= DIV_ROUND_UP_SECTOR_T(logical_sector
,
4475 sector_div(last_sector
, stripe_sectors
);
4477 logical_sector
*= conf
->chunk_sectors
;
4478 last_sector
*= conf
->chunk_sectors
;
4480 for (; logical_sector
< last_sector
;
4481 logical_sector
+= STRIPE_SECTORS
) {
4485 sh
= get_active_stripe(conf
, logical_sector
, 0, 0, 0);
4486 prepare_to_wait(&conf
->wait_for_overlap
, &w
,
4487 TASK_UNINTERRUPTIBLE
);
4488 set_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
4489 if (test_bit(STRIPE_SYNCING
, &sh
->state
)) {
4494 clear_bit(R5_Overlap
, &sh
->dev
[sh
->pd_idx
].flags
);
4495 spin_lock_irq(&sh
->stripe_lock
);
4496 for (d
= 0; d
< conf
->raid_disks
; d
++) {
4497 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
4499 if (sh
->dev
[d
].towrite
|| sh
->dev
[d
].toread
) {
4500 set_bit(R5_Overlap
, &sh
->dev
[d
].flags
);
4501 spin_unlock_irq(&sh
->stripe_lock
);
4507 set_bit(STRIPE_DISCARD
, &sh
->state
);
4508 finish_wait(&conf
->wait_for_overlap
, &w
);
4509 for (d
= 0; d
< conf
->raid_disks
; d
++) {
4510 if (d
== sh
->pd_idx
|| d
== sh
->qd_idx
)
4512 sh
->dev
[d
].towrite
= bi
;
4513 set_bit(R5_OVERWRITE
, &sh
->dev
[d
].flags
);
4514 raid5_inc_bi_active_stripes(bi
);
4516 spin_unlock_irq(&sh
->stripe_lock
);
4517 if (conf
->mddev
->bitmap
) {
4519 d
< conf
->raid_disks
- conf
->max_degraded
;
4521 bitmap_startwrite(mddev
->bitmap
,
4525 sh
->bm_seq
= conf
->seq_flush
+ 1;
4526 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
4529 set_bit(STRIPE_HANDLE
, &sh
->state
);
4530 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4531 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4532 atomic_inc(&conf
->preread_active_stripes
);
4533 release_stripe_plug(mddev
, sh
);
4536 remaining
= raid5_dec_bi_active_stripes(bi
);
4537 if (remaining
== 0) {
4538 md_write_end(mddev
);
4543 static void make_request(struct mddev
*mddev
, struct bio
* bi
)
4545 struct r5conf
*conf
= mddev
->private;
4547 sector_t new_sector
;
4548 sector_t logical_sector
, last_sector
;
4549 struct stripe_head
*sh
;
4550 const int rw
= bio_data_dir(bi
);
4553 if (unlikely(bi
->bi_rw
& REQ_FLUSH
)) {
4554 md_flush_request(mddev
, bi
);
4558 md_write_start(mddev
, bi
);
4561 mddev
->reshape_position
== MaxSector
&&
4562 chunk_aligned_read(mddev
,bi
))
4565 if (unlikely(bi
->bi_rw
& REQ_DISCARD
)) {
4566 make_discard_request(mddev
, bi
);
4570 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
4571 last_sector
= bio_end_sector(bi
);
4573 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
4575 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
4581 seq
= read_seqcount_begin(&conf
->gen_lock
);
4583 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
4584 if (unlikely(conf
->reshape_progress
!= MaxSector
)) {
4585 /* spinlock is needed as reshape_progress may be
4586 * 64bit on a 32bit platform, and so it might be
4587 * possible to see a half-updated value
4588 * Of course reshape_progress could change after
4589 * the lock is dropped, so once we get a reference
4590 * to the stripe that we think it is, we will have
4593 spin_lock_irq(&conf
->device_lock
);
4594 if (mddev
->reshape_backwards
4595 ? logical_sector
< conf
->reshape_progress
4596 : logical_sector
>= conf
->reshape_progress
) {
4599 if (mddev
->reshape_backwards
4600 ? logical_sector
< conf
->reshape_safe
4601 : logical_sector
>= conf
->reshape_safe
) {
4602 spin_unlock_irq(&conf
->device_lock
);
4607 spin_unlock_irq(&conf
->device_lock
);
4610 new_sector
= raid5_compute_sector(conf
, logical_sector
,
4613 pr_debug("raid456: make_request, sector %llu logical %llu\n",
4614 (unsigned long long)new_sector
,
4615 (unsigned long long)logical_sector
);
4617 sh
= get_active_stripe(conf
, new_sector
, previous
,
4618 (bi
->bi_rw
&RWA_MASK
), 0);
4620 if (unlikely(previous
)) {
4621 /* expansion might have moved on while waiting for a
4622 * stripe, so we must do the range check again.
4623 * Expansion could still move past after this
4624 * test, but as we are holding a reference to
4625 * 'sh', we know that if that happens,
4626 * STRIPE_EXPANDING will get set and the expansion
4627 * won't proceed until we finish with the stripe.
4630 spin_lock_irq(&conf
->device_lock
);
4631 if (mddev
->reshape_backwards
4632 ? logical_sector
>= conf
->reshape_progress
4633 : logical_sector
< conf
->reshape_progress
)
4634 /* mismatch, need to try again */
4636 spin_unlock_irq(&conf
->device_lock
);
4643 if (read_seqcount_retry(&conf
->gen_lock
, seq
)) {
4644 /* Might have got the wrong stripe_head
4652 logical_sector
>= mddev
->suspend_lo
&&
4653 logical_sector
< mddev
->suspend_hi
) {
4655 /* As the suspend_* range is controlled by
4656 * userspace, we want an interruptible
4659 flush_signals(current
);
4660 prepare_to_wait(&conf
->wait_for_overlap
,
4661 &w
, TASK_INTERRUPTIBLE
);
4662 if (logical_sector
>= mddev
->suspend_lo
&&
4663 logical_sector
< mddev
->suspend_hi
)
4668 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
4669 !add_stripe_bio(sh
, bi
, dd_idx
, rw
)) {
4670 /* Stripe is busy expanding or
4671 * add failed due to overlap. Flush everything
4674 md_wakeup_thread(mddev
->thread
);
4679 finish_wait(&conf
->wait_for_overlap
, &w
);
4680 set_bit(STRIPE_HANDLE
, &sh
->state
);
4681 clear_bit(STRIPE_DELAYED
, &sh
->state
);
4682 if ((bi
->bi_rw
& REQ_SYNC
) &&
4683 !test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
4684 atomic_inc(&conf
->preread_active_stripes
);
4685 release_stripe_plug(mddev
, sh
);
4687 /* cannot get stripe for read-ahead, just give-up */
4688 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
4689 finish_wait(&conf
->wait_for_overlap
, &w
);
4694 remaining
= raid5_dec_bi_active_stripes(bi
);
4695 if (remaining
== 0) {
4698 md_write_end(mddev
);
4700 trace_block_bio_complete(bdev_get_queue(bi
->bi_bdev
),
4706 static sector_t
raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
4708 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
)
4710 /* reshaping is quite different to recovery/resync so it is
4711 * handled quite separately ... here.
4713 * On each call to sync_request, we gather one chunk worth of
4714 * destination stripes and flag them as expanding.
4715 * Then we find all the source stripes and request reads.
4716 * As the reads complete, handle_stripe will copy the data
4717 * into the destination stripe and release that stripe.
4719 struct r5conf
*conf
= mddev
->private;
4720 struct stripe_head
*sh
;
4721 sector_t first_sector
, last_sector
;
4722 int raid_disks
= conf
->previous_raid_disks
;
4723 int data_disks
= raid_disks
- conf
->max_degraded
;
4724 int new_data_disks
= conf
->raid_disks
- conf
->max_degraded
;
4727 sector_t writepos
, readpos
, safepos
;
4728 sector_t stripe_addr
;
4729 int reshape_sectors
;
4730 struct list_head stripes
;
4732 if (sector_nr
== 0) {
4733 /* If restarting in the middle, skip the initial sectors */
4734 if (mddev
->reshape_backwards
&&
4735 conf
->reshape_progress
< raid5_size(mddev
, 0, 0)) {
4736 sector_nr
= raid5_size(mddev
, 0, 0)
4737 - conf
->reshape_progress
;
4738 } else if (!mddev
->reshape_backwards
&&
4739 conf
->reshape_progress
> 0)
4740 sector_nr
= conf
->reshape_progress
;
4741 sector_div(sector_nr
, new_data_disks
);
4743 mddev
->curr_resync_completed
= sector_nr
;
4744 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4750 /* We need to process a full chunk at a time.
4751 * If old and new chunk sizes differ, we need to process the
4754 if (mddev
->new_chunk_sectors
> mddev
->chunk_sectors
)
4755 reshape_sectors
= mddev
->new_chunk_sectors
;
4757 reshape_sectors
= mddev
->chunk_sectors
;
4759 /* We update the metadata at least every 10 seconds, or when
4760 * the data about to be copied would over-write the source of
4761 * the data at the front of the range. i.e. one new_stripe
4762 * along from reshape_progress new_maps to after where
4763 * reshape_safe old_maps to
4765 writepos
= conf
->reshape_progress
;
4766 sector_div(writepos
, new_data_disks
);
4767 readpos
= conf
->reshape_progress
;
4768 sector_div(readpos
, data_disks
);
4769 safepos
= conf
->reshape_safe
;
4770 sector_div(safepos
, data_disks
);
4771 if (mddev
->reshape_backwards
) {
4772 writepos
-= min_t(sector_t
, reshape_sectors
, writepos
);
4773 readpos
+= reshape_sectors
;
4774 safepos
+= reshape_sectors
;
4776 writepos
+= reshape_sectors
;
4777 readpos
-= min_t(sector_t
, reshape_sectors
, readpos
);
4778 safepos
-= min_t(sector_t
, reshape_sectors
, safepos
);
4781 /* Having calculated the 'writepos' possibly use it
4782 * to set 'stripe_addr' which is where we will write to.
4784 if (mddev
->reshape_backwards
) {
4785 BUG_ON(conf
->reshape_progress
== 0);
4786 stripe_addr
= writepos
;
4787 BUG_ON((mddev
->dev_sectors
&
4788 ~((sector_t
)reshape_sectors
- 1))
4789 - reshape_sectors
- stripe_addr
4792 BUG_ON(writepos
!= sector_nr
+ reshape_sectors
);
4793 stripe_addr
= sector_nr
;
4796 /* 'writepos' is the most advanced device address we might write.
4797 * 'readpos' is the least advanced device address we might read.
4798 * 'safepos' is the least address recorded in the metadata as having
4800 * If there is a min_offset_diff, these are adjusted either by
4801 * increasing the safepos/readpos if diff is negative, or
4802 * increasing writepos if diff is positive.
4803 * If 'readpos' is then behind 'writepos', there is no way that we can
4804 * ensure safety in the face of a crash - that must be done by userspace
4805 * making a backup of the data. So in that case there is no particular
4806 * rush to update metadata.
4807 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4808 * update the metadata to advance 'safepos' to match 'readpos' so that
4809 * we can be safe in the event of a crash.
4810 * So we insist on updating metadata if safepos is behind writepos and
4811 * readpos is beyond writepos.
4812 * In any case, update the metadata every 10 seconds.
4813 * Maybe that number should be configurable, but I'm not sure it is
4814 * worth it.... maybe it could be a multiple of safemode_delay???
4816 if (conf
->min_offset_diff
< 0) {
4817 safepos
+= -conf
->min_offset_diff
;
4818 readpos
+= -conf
->min_offset_diff
;
4820 writepos
+= conf
->min_offset_diff
;
4822 if ((mddev
->reshape_backwards
4823 ? (safepos
> writepos
&& readpos
< writepos
)
4824 : (safepos
< writepos
&& readpos
> writepos
)) ||
4825 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4826 /* Cannot proceed until we've updated the superblock... */
4827 wait_event(conf
->wait_for_overlap
,
4828 atomic_read(&conf
->reshape_stripes
)==0
4829 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4830 if (atomic_read(&conf
->reshape_stripes
) != 0)
4832 mddev
->reshape_position
= conf
->reshape_progress
;
4833 mddev
->curr_resync_completed
= sector_nr
;
4834 conf
->reshape_checkpoint
= jiffies
;
4835 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4836 md_wakeup_thread(mddev
->thread
);
4837 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4838 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4839 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
4841 spin_lock_irq(&conf
->device_lock
);
4842 conf
->reshape_safe
= mddev
->reshape_position
;
4843 spin_unlock_irq(&conf
->device_lock
);
4844 wake_up(&conf
->wait_for_overlap
);
4845 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4848 INIT_LIST_HEAD(&stripes
);
4849 for (i
= 0; i
< reshape_sectors
; i
+= STRIPE_SECTORS
) {
4851 int skipped_disk
= 0;
4852 sh
= get_active_stripe(conf
, stripe_addr
+i
, 0, 0, 1);
4853 set_bit(STRIPE_EXPANDING
, &sh
->state
);
4854 atomic_inc(&conf
->reshape_stripes
);
4855 /* If any of this stripe is beyond the end of the old
4856 * array, then we need to zero those blocks
4858 for (j
=sh
->disks
; j
--;) {
4860 if (j
== sh
->pd_idx
)
4862 if (conf
->level
== 6 &&
4865 s
= compute_blocknr(sh
, j
, 0);
4866 if (s
< raid5_size(mddev
, 0, 0)) {
4870 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
4871 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
4872 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
4874 if (!skipped_disk
) {
4875 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
4876 set_bit(STRIPE_HANDLE
, &sh
->state
);
4878 list_add(&sh
->lru
, &stripes
);
4880 spin_lock_irq(&conf
->device_lock
);
4881 if (mddev
->reshape_backwards
)
4882 conf
->reshape_progress
-= reshape_sectors
* new_data_disks
;
4884 conf
->reshape_progress
+= reshape_sectors
* new_data_disks
;
4885 spin_unlock_irq(&conf
->device_lock
);
4886 /* Ok, those stripe are ready. We can start scheduling
4887 * reads on the source stripes.
4888 * The source stripes are determined by mapping the first and last
4889 * block on the destination stripes.
4892 raid5_compute_sector(conf
, stripe_addr
*(new_data_disks
),
4895 raid5_compute_sector(conf
, ((stripe_addr
+reshape_sectors
)
4896 * new_data_disks
- 1),
4898 if (last_sector
>= mddev
->dev_sectors
)
4899 last_sector
= mddev
->dev_sectors
- 1;
4900 while (first_sector
<= last_sector
) {
4901 sh
= get_active_stripe(conf
, first_sector
, 1, 0, 1);
4902 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
4903 set_bit(STRIPE_HANDLE
, &sh
->state
);
4905 first_sector
+= STRIPE_SECTORS
;
4907 /* Now that the sources are clearly marked, we can release
4908 * the destination stripes
4910 while (!list_empty(&stripes
)) {
4911 sh
= list_entry(stripes
.next
, struct stripe_head
, lru
);
4912 list_del_init(&sh
->lru
);
4915 /* If this takes us to the resync_max point where we have to pause,
4916 * then we need to write out the superblock.
4918 sector_nr
+= reshape_sectors
;
4919 if ((sector_nr
- mddev
->curr_resync_completed
) * 2
4920 >= mddev
->resync_max
- mddev
->curr_resync_completed
) {
4921 /* Cannot proceed until we've updated the superblock... */
4922 wait_event(conf
->wait_for_overlap
,
4923 atomic_read(&conf
->reshape_stripes
) == 0
4924 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4925 if (atomic_read(&conf
->reshape_stripes
) != 0)
4927 mddev
->reshape_position
= conf
->reshape_progress
;
4928 mddev
->curr_resync_completed
= sector_nr
;
4929 conf
->reshape_checkpoint
= jiffies
;
4930 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4931 md_wakeup_thread(mddev
->thread
);
4932 wait_event(mddev
->sb_wait
,
4933 !test_bit(MD_CHANGE_DEVS
, &mddev
->flags
)
4934 || test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4935 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
4937 spin_lock_irq(&conf
->device_lock
);
4938 conf
->reshape_safe
= mddev
->reshape_position
;
4939 spin_unlock_irq(&conf
->device_lock
);
4940 wake_up(&conf
->wait_for_overlap
);
4941 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4944 return reshape_sectors
;
4947 /* FIXME go_faster isn't used */
4948 static inline sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
4950 struct r5conf
*conf
= mddev
->private;
4951 struct stripe_head
*sh
;
4952 sector_t max_sector
= mddev
->dev_sectors
;
4953 sector_t sync_blocks
;
4954 int still_degraded
= 0;
4957 if (sector_nr
>= max_sector
) {
4958 /* just being told to finish up .. nothing much to do */
4960 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
4965 if (mddev
->curr_resync
< max_sector
) /* aborted */
4966 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
4968 else /* completed sync */
4970 bitmap_close_sync(mddev
->bitmap
);
4975 /* Allow raid5_quiesce to complete */
4976 wait_event(conf
->wait_for_overlap
, conf
->quiesce
!= 2);
4978 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
4979 return reshape_request(mddev
, sector_nr
, skipped
);
4981 /* No need to check resync_max as we never do more than one
4982 * stripe, and as resync_max will always be on a chunk boundary,
4983 * if the check in md_do_sync didn't fire, there is no chance
4984 * of overstepping resync_max here
4987 /* if there is too many failed drives and we are trying
4988 * to resync, then assert that we are finished, because there is
4989 * nothing we can do.
4991 if (mddev
->degraded
>= conf
->max_degraded
&&
4992 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
4993 sector_t rv
= mddev
->dev_sectors
- sector_nr
;
4997 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
4999 !bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
5000 sync_blocks
>= STRIPE_SECTORS
) {
5001 /* we can skip this block, and probably more */
5002 sync_blocks
/= STRIPE_SECTORS
;
5004 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
5007 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
5009 sh
= get_active_stripe(conf
, sector_nr
, 0, 1, 0);
5011 sh
= get_active_stripe(conf
, sector_nr
, 0, 0, 0);
5012 /* make sure we don't swamp the stripe cache if someone else
5013 * is trying to get access
5015 schedule_timeout_uninterruptible(1);
5017 /* Need to check if array will still be degraded after recovery/resync
5018 * We don't need to check the 'failed' flag as when that gets set,
5021 for (i
= 0; i
< conf
->raid_disks
; i
++)
5022 if (conf
->disks
[i
].rdev
== NULL
)
5025 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
5027 set_bit(STRIPE_SYNC_REQUESTED
, &sh
->state
);
5032 return STRIPE_SECTORS
;
5035 static int retry_aligned_read(struct r5conf
*conf
, struct bio
*raid_bio
)
5037 /* We may not be able to submit a whole bio at once as there
5038 * may not be enough stripe_heads available.
5039 * We cannot pre-allocate enough stripe_heads as we may need
5040 * more than exist in the cache (if we allow ever large chunks).
5041 * So we do one stripe head at a time and record in
5042 * ->bi_hw_segments how many have been done.
5044 * We *know* that this entire raid_bio is in one chunk, so
5045 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5047 struct stripe_head
*sh
;
5049 sector_t sector
, logical_sector
, last_sector
;
5054 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
5055 sector
= raid5_compute_sector(conf
, logical_sector
,
5057 last_sector
= bio_end_sector(raid_bio
);
5059 for (; logical_sector
< last_sector
;
5060 logical_sector
+= STRIPE_SECTORS
,
5061 sector
+= STRIPE_SECTORS
,
5064 if (scnt
< raid5_bi_processed_stripes(raid_bio
))
5065 /* already done this stripe */
5068 sh
= get_active_stripe(conf
, sector
, 0, 1, 0);
5071 /* failed to get a stripe - must wait */
5072 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5073 conf
->retry_read_aligned
= raid_bio
;
5077 if (!add_stripe_bio(sh
, raid_bio
, dd_idx
, 0)) {
5079 raid5_set_bi_processed_stripes(raid_bio
, scnt
);
5080 conf
->retry_read_aligned
= raid_bio
;
5084 set_bit(R5_ReadNoMerge
, &sh
->dev
[dd_idx
].flags
);
5089 remaining
= raid5_dec_bi_active_stripes(raid_bio
);
5090 if (remaining
== 0) {
5091 trace_block_bio_complete(bdev_get_queue(raid_bio
->bi_bdev
),
5093 bio_endio(raid_bio
, 0);
5095 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
5096 wake_up(&conf
->wait_for_stripe
);
5100 static int handle_active_stripes(struct r5conf
*conf
, int group
,
5101 struct r5worker
*worker
,
5102 struct list_head
*temp_inactive_list
)
5104 struct stripe_head
*batch
[MAX_STRIPE_BATCH
], *sh
;
5105 int i
, batch_size
= 0, hash
;
5106 bool release_inactive
= false;
5108 while (batch_size
< MAX_STRIPE_BATCH
&&
5109 (sh
= __get_priority_stripe(conf
, group
)) != NULL
)
5110 batch
[batch_size
++] = sh
;
5112 if (batch_size
== 0) {
5113 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5114 if (!list_empty(temp_inactive_list
+ i
))
5116 if (i
== NR_STRIPE_HASH_LOCKS
)
5118 release_inactive
= true;
5120 spin_unlock_irq(&conf
->device_lock
);
5122 release_inactive_stripe_list(conf
, temp_inactive_list
,
5123 NR_STRIPE_HASH_LOCKS
);
5125 if (release_inactive
) {
5126 spin_lock_irq(&conf
->device_lock
);
5130 for (i
= 0; i
< batch_size
; i
++)
5131 handle_stripe(batch
[i
]);
5135 spin_lock_irq(&conf
->device_lock
);
5136 for (i
= 0; i
< batch_size
; i
++) {
5137 hash
= batch
[i
]->hash_lock_index
;
5138 __release_stripe(conf
, batch
[i
], &temp_inactive_list
[hash
]);
5143 static void raid5_do_work(struct work_struct
*work
)
5145 struct r5worker
*worker
= container_of(work
, struct r5worker
, work
);
5146 struct r5worker_group
*group
= worker
->group
;
5147 struct r5conf
*conf
= group
->conf
;
5148 int group_id
= group
- conf
->worker_groups
;
5150 struct blk_plug plug
;
5152 pr_debug("+++ raid5worker active\n");
5154 blk_start_plug(&plug
);
5156 spin_lock_irq(&conf
->device_lock
);
5158 int batch_size
, released
;
5160 released
= release_stripe_list(conf
, worker
->temp_inactive_list
);
5162 batch_size
= handle_active_stripes(conf
, group_id
, worker
,
5163 worker
->temp_inactive_list
);
5164 worker
->working
= false;
5165 if (!batch_size
&& !released
)
5167 handled
+= batch_size
;
5169 pr_debug("%d stripes handled\n", handled
);
5171 spin_unlock_irq(&conf
->device_lock
);
5172 blk_finish_plug(&plug
);
5174 pr_debug("--- raid5worker inactive\n");
5178 * This is our raid5 kernel thread.
5180 * We scan the hash table for stripes which can be handled now.
5181 * During the scan, completed stripes are saved for us by the interrupt
5182 * handler, so that they will not have to wait for our next wakeup.
5184 static void raid5d(struct md_thread
*thread
)
5186 struct mddev
*mddev
= thread
->mddev
;
5187 struct r5conf
*conf
= mddev
->private;
5189 struct blk_plug plug
;
5191 pr_debug("+++ raid5d active\n");
5193 md_check_recovery(mddev
);
5195 blk_start_plug(&plug
);
5197 spin_lock_irq(&conf
->device_lock
);
5200 int batch_size
, released
;
5202 released
= release_stripe_list(conf
, conf
->temp_inactive_list
);
5205 !list_empty(&conf
->bitmap_list
)) {
5206 /* Now is a good time to flush some bitmap updates */
5208 spin_unlock_irq(&conf
->device_lock
);
5209 bitmap_unplug(mddev
->bitmap
);
5210 spin_lock_irq(&conf
->device_lock
);
5211 conf
->seq_write
= conf
->seq_flush
;
5212 activate_bit_delay(conf
, conf
->temp_inactive_list
);
5214 raid5_activate_delayed(conf
);
5216 while ((bio
= remove_bio_from_retry(conf
))) {
5218 spin_unlock_irq(&conf
->device_lock
);
5219 ok
= retry_aligned_read(conf
, bio
);
5220 spin_lock_irq(&conf
->device_lock
);
5226 batch_size
= handle_active_stripes(conf
, ANY_GROUP
, NULL
,
5227 conf
->temp_inactive_list
);
5228 if (!batch_size
&& !released
)
5230 handled
+= batch_size
;
5232 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
)) {
5233 spin_unlock_irq(&conf
->device_lock
);
5234 md_check_recovery(mddev
);
5235 spin_lock_irq(&conf
->device_lock
);
5238 pr_debug("%d stripes handled\n", handled
);
5240 spin_unlock_irq(&conf
->device_lock
);
5242 async_tx_issue_pending_all();
5243 blk_finish_plug(&plug
);
5245 pr_debug("--- raid5d inactive\n");
5249 raid5_show_stripe_cache_size(struct mddev
*mddev
, char *page
)
5251 struct r5conf
*conf
= mddev
->private;
5253 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
5259 raid5_set_cache_size(struct mddev
*mddev
, int size
)
5261 struct r5conf
*conf
= mddev
->private;
5265 if (size
<= 16 || size
> 32768)
5267 hash
= (conf
->max_nr_stripes
- 1) % NR_STRIPE_HASH_LOCKS
;
5268 while (size
< conf
->max_nr_stripes
) {
5269 if (drop_one_stripe(conf
, hash
))
5270 conf
->max_nr_stripes
--;
5275 hash
= NR_STRIPE_HASH_LOCKS
- 1;
5277 err
= md_allow_write(mddev
);
5280 hash
= conf
->max_nr_stripes
% NR_STRIPE_HASH_LOCKS
;
5281 while (size
> conf
->max_nr_stripes
) {
5282 if (grow_one_stripe(conf
, hash
))
5283 conf
->max_nr_stripes
++;
5285 hash
= (hash
+ 1) % NR_STRIPE_HASH_LOCKS
;
5289 EXPORT_SYMBOL(raid5_set_cache_size
);
5292 raid5_store_stripe_cache_size(struct mddev
*mddev
, const char *page
, size_t len
)
5294 struct r5conf
*conf
= mddev
->private;
5298 if (len
>= PAGE_SIZE
)
5303 if (kstrtoul(page
, 10, &new))
5305 err
= raid5_set_cache_size(mddev
, new);
5311 static struct md_sysfs_entry
5312 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
5313 raid5_show_stripe_cache_size
,
5314 raid5_store_stripe_cache_size
);
5317 raid5_show_preread_threshold(struct mddev
*mddev
, char *page
)
5319 struct r5conf
*conf
= mddev
->private;
5321 return sprintf(page
, "%d\n", conf
->bypass_threshold
);
5327 raid5_store_preread_threshold(struct mddev
*mddev
, const char *page
, size_t len
)
5329 struct r5conf
*conf
= mddev
->private;
5331 if (len
>= PAGE_SIZE
)
5336 if (kstrtoul(page
, 10, &new))
5338 if (new > conf
->max_nr_stripes
)
5340 conf
->bypass_threshold
= new;
5344 static struct md_sysfs_entry
5345 raid5_preread_bypass_threshold
= __ATTR(preread_bypass_threshold
,
5347 raid5_show_preread_threshold
,
5348 raid5_store_preread_threshold
);
5351 stripe_cache_active_show(struct mddev
*mddev
, char *page
)
5353 struct r5conf
*conf
= mddev
->private;
5355 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
5360 static struct md_sysfs_entry
5361 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
5364 raid5_show_group_thread_cnt(struct mddev
*mddev
, char *page
)
5366 struct r5conf
*conf
= mddev
->private;
5368 return sprintf(page
, "%d\n", conf
->worker_cnt_per_group
);
5373 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
5375 int *worker_cnt_per_group
,
5376 struct r5worker_group
**worker_groups
);
5378 raid5_store_group_thread_cnt(struct mddev
*mddev
, const char *page
, size_t len
)
5380 struct r5conf
*conf
= mddev
->private;
5383 struct r5worker_group
*new_groups
, *old_groups
;
5384 int group_cnt
, worker_cnt_per_group
;
5386 if (len
>= PAGE_SIZE
)
5391 if (kstrtoul(page
, 10, &new))
5394 if (new == conf
->worker_cnt_per_group
)
5397 mddev_suspend(mddev
);
5399 old_groups
= conf
->worker_groups
;
5401 flush_workqueue(raid5_wq
);
5403 err
= alloc_thread_groups(conf
, new,
5404 &group_cnt
, &worker_cnt_per_group
,
5407 spin_lock_irq(&conf
->device_lock
);
5408 conf
->group_cnt
= group_cnt
;
5409 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
5410 conf
->worker_groups
= new_groups
;
5411 spin_unlock_irq(&conf
->device_lock
);
5414 kfree(old_groups
[0].workers
);
5418 mddev_resume(mddev
);
5425 static struct md_sysfs_entry
5426 raid5_group_thread_cnt
= __ATTR(group_thread_cnt
, S_IRUGO
| S_IWUSR
,
5427 raid5_show_group_thread_cnt
,
5428 raid5_store_group_thread_cnt
);
5430 static struct attribute
*raid5_attrs
[] = {
5431 &raid5_stripecache_size
.attr
,
5432 &raid5_stripecache_active
.attr
,
5433 &raid5_preread_bypass_threshold
.attr
,
5434 &raid5_group_thread_cnt
.attr
,
5437 static struct attribute_group raid5_attrs_group
= {
5439 .attrs
= raid5_attrs
,
5442 static int alloc_thread_groups(struct r5conf
*conf
, int cnt
,
5444 int *worker_cnt_per_group
,
5445 struct r5worker_group
**worker_groups
)
5449 struct r5worker
*workers
;
5451 *worker_cnt_per_group
= cnt
;
5454 *worker_groups
= NULL
;
5457 *group_cnt
= num_possible_nodes();
5458 size
= sizeof(struct r5worker
) * cnt
;
5459 workers
= kzalloc(size
* *group_cnt
, GFP_NOIO
);
5460 *worker_groups
= kzalloc(sizeof(struct r5worker_group
) *
5461 *group_cnt
, GFP_NOIO
);
5462 if (!*worker_groups
|| !workers
) {
5464 kfree(*worker_groups
);
5468 for (i
= 0; i
< *group_cnt
; i
++) {
5469 struct r5worker_group
*group
;
5471 group
= &(*worker_groups
)[i
];
5472 INIT_LIST_HEAD(&group
->handle_list
);
5474 group
->workers
= workers
+ i
* cnt
;
5476 for (j
= 0; j
< cnt
; j
++) {
5477 struct r5worker
*worker
= group
->workers
+ j
;
5478 worker
->group
= group
;
5479 INIT_WORK(&worker
->work
, raid5_do_work
);
5481 for (k
= 0; k
< NR_STRIPE_HASH_LOCKS
; k
++)
5482 INIT_LIST_HEAD(worker
->temp_inactive_list
+ k
);
5489 static void free_thread_groups(struct r5conf
*conf
)
5491 if (conf
->worker_groups
)
5492 kfree(conf
->worker_groups
[0].workers
);
5493 kfree(conf
->worker_groups
);
5494 conf
->worker_groups
= NULL
;
5498 raid5_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
5500 struct r5conf
*conf
= mddev
->private;
5503 sectors
= mddev
->dev_sectors
;
5505 /* size is defined by the smallest of previous and new size */
5506 raid_disks
= min(conf
->raid_disks
, conf
->previous_raid_disks
);
5508 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
5509 sectors
&= ~((sector_t
)mddev
->new_chunk_sectors
- 1);
5510 return sectors
* (raid_disks
- conf
->max_degraded
);
5513 static void raid5_free_percpu(struct r5conf
*conf
)
5515 struct raid5_percpu
*percpu
;
5522 for_each_possible_cpu(cpu
) {
5523 percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
5524 safe_put_page(percpu
->spare_page
);
5525 kfree(percpu
->scribble
);
5527 #ifdef CONFIG_HOTPLUG_CPU
5528 unregister_cpu_notifier(&conf
->cpu_notify
);
5532 free_percpu(conf
->percpu
);
5535 static void free_conf(struct r5conf
*conf
)
5537 free_thread_groups(conf
);
5538 shrink_stripes(conf
);
5539 raid5_free_percpu(conf
);
5541 kfree(conf
->stripe_hashtbl
);
5545 #ifdef CONFIG_HOTPLUG_CPU
5546 static int raid456_cpu_notify(struct notifier_block
*nfb
, unsigned long action
,
5549 struct r5conf
*conf
= container_of(nfb
, struct r5conf
, cpu_notify
);
5550 long cpu
= (long)hcpu
;
5551 struct raid5_percpu
*percpu
= per_cpu_ptr(conf
->percpu
, cpu
);
5554 case CPU_UP_PREPARE
:
5555 case CPU_UP_PREPARE_FROZEN
:
5556 if (conf
->level
== 6 && !percpu
->spare_page
)
5557 percpu
->spare_page
= alloc_page(GFP_KERNEL
);
5558 if (!percpu
->scribble
)
5559 percpu
->scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
5561 if (!percpu
->scribble
||
5562 (conf
->level
== 6 && !percpu
->spare_page
)) {
5563 safe_put_page(percpu
->spare_page
);
5564 kfree(percpu
->scribble
);
5565 pr_err("%s: failed memory allocation for cpu%ld\n",
5567 return notifier_from_errno(-ENOMEM
);
5571 case CPU_DEAD_FROZEN
:
5572 safe_put_page(percpu
->spare_page
);
5573 kfree(percpu
->scribble
);
5574 percpu
->spare_page
= NULL
;
5575 percpu
->scribble
= NULL
;
5584 static int raid5_alloc_percpu(struct r5conf
*conf
)
5587 struct page
*spare_page
;
5588 struct raid5_percpu __percpu
*allcpus
;
5592 allcpus
= alloc_percpu(struct raid5_percpu
);
5595 conf
->percpu
= allcpus
;
5599 for_each_present_cpu(cpu
) {
5600 if (conf
->level
== 6) {
5601 spare_page
= alloc_page(GFP_KERNEL
);
5606 per_cpu_ptr(conf
->percpu
, cpu
)->spare_page
= spare_page
;
5608 scribble
= kmalloc(conf
->scribble_len
, GFP_KERNEL
);
5613 per_cpu_ptr(conf
->percpu
, cpu
)->scribble
= scribble
;
5615 #ifdef CONFIG_HOTPLUG_CPU
5616 conf
->cpu_notify
.notifier_call
= raid456_cpu_notify
;
5617 conf
->cpu_notify
.priority
= 0;
5619 err
= register_cpu_notifier(&conf
->cpu_notify
);
5626 static struct r5conf
*setup_conf(struct mddev
*mddev
)
5628 struct r5conf
*conf
;
5629 int raid_disk
, memory
, max_disks
;
5630 struct md_rdev
*rdev
;
5631 struct disk_info
*disk
;
5634 int group_cnt
, worker_cnt_per_group
;
5635 struct r5worker_group
*new_group
;
5637 if (mddev
->new_level
!= 5
5638 && mddev
->new_level
!= 4
5639 && mddev
->new_level
!= 6) {
5640 printk(KERN_ERR
"md/raid:%s: raid level not set to 4/5/6 (%d)\n",
5641 mdname(mddev
), mddev
->new_level
);
5642 return ERR_PTR(-EIO
);
5644 if ((mddev
->new_level
== 5
5645 && !algorithm_valid_raid5(mddev
->new_layout
)) ||
5646 (mddev
->new_level
== 6
5647 && !algorithm_valid_raid6(mddev
->new_layout
))) {
5648 printk(KERN_ERR
"md/raid:%s: layout %d not supported\n",
5649 mdname(mddev
), mddev
->new_layout
);
5650 return ERR_PTR(-EIO
);
5652 if (mddev
->new_level
== 6 && mddev
->raid_disks
< 4) {
5653 printk(KERN_ERR
"md/raid:%s: not enough configured devices (%d, minimum 4)\n",
5654 mdname(mddev
), mddev
->raid_disks
);
5655 return ERR_PTR(-EINVAL
);
5658 if (!mddev
->new_chunk_sectors
||
5659 (mddev
->new_chunk_sectors
<< 9) % PAGE_SIZE
||
5660 !is_power_of_2(mddev
->new_chunk_sectors
)) {
5661 printk(KERN_ERR
"md/raid:%s: invalid chunk size %d\n",
5662 mdname(mddev
), mddev
->new_chunk_sectors
<< 9);
5663 return ERR_PTR(-EINVAL
);
5666 conf
= kzalloc(sizeof(struct r5conf
), GFP_KERNEL
);
5669 /* Don't enable multi-threading by default*/
5670 if (!alloc_thread_groups(conf
, 0, &group_cnt
, &worker_cnt_per_group
,
5672 conf
->group_cnt
= group_cnt
;
5673 conf
->worker_cnt_per_group
= worker_cnt_per_group
;
5674 conf
->worker_groups
= new_group
;
5677 spin_lock_init(&conf
->device_lock
);
5678 seqcount_init(&conf
->gen_lock
);
5679 init_waitqueue_head(&conf
->wait_for_stripe
);
5680 init_waitqueue_head(&conf
->wait_for_overlap
);
5681 INIT_LIST_HEAD(&conf
->handle_list
);
5682 INIT_LIST_HEAD(&conf
->hold_list
);
5683 INIT_LIST_HEAD(&conf
->delayed_list
);
5684 INIT_LIST_HEAD(&conf
->bitmap_list
);
5685 init_llist_head(&conf
->released_stripes
);
5686 atomic_set(&conf
->active_stripes
, 0);
5687 atomic_set(&conf
->preread_active_stripes
, 0);
5688 atomic_set(&conf
->active_aligned_reads
, 0);
5689 conf
->bypass_threshold
= BYPASS_THRESHOLD
;
5690 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
5692 conf
->raid_disks
= mddev
->raid_disks
;
5693 if (mddev
->reshape_position
== MaxSector
)
5694 conf
->previous_raid_disks
= mddev
->raid_disks
;
5696 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
5697 max_disks
= max(conf
->raid_disks
, conf
->previous_raid_disks
);
5698 conf
->scribble_len
= scribble_len(max_disks
);
5700 conf
->disks
= kzalloc(max_disks
* sizeof(struct disk_info
),
5705 conf
->mddev
= mddev
;
5707 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
5710 /* We init hash_locks[0] separately to that it can be used
5711 * as the reference lock in the spin_lock_nest_lock() call
5712 * in lock_all_device_hash_locks_irq in order to convince
5713 * lockdep that we know what we are doing.
5715 spin_lock_init(conf
->hash_locks
);
5716 for (i
= 1; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5717 spin_lock_init(conf
->hash_locks
+ i
);
5719 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5720 INIT_LIST_HEAD(conf
->inactive_list
+ i
);
5722 for (i
= 0; i
< NR_STRIPE_HASH_LOCKS
; i
++)
5723 INIT_LIST_HEAD(conf
->temp_inactive_list
+ i
);
5725 conf
->level
= mddev
->new_level
;
5726 if (raid5_alloc_percpu(conf
) != 0)
5729 pr_debug("raid456: run(%s) called.\n", mdname(mddev
));
5731 rdev_for_each(rdev
, mddev
) {
5732 raid_disk
= rdev
->raid_disk
;
5733 if (raid_disk
>= max_disks
5736 disk
= conf
->disks
+ raid_disk
;
5738 if (test_bit(Replacement
, &rdev
->flags
)) {
5739 if (disk
->replacement
)
5741 disk
->replacement
= rdev
;
5748 if (test_bit(In_sync
, &rdev
->flags
)) {
5749 char b
[BDEVNAME_SIZE
];
5750 printk(KERN_INFO
"md/raid:%s: device %s operational as raid"
5752 mdname(mddev
), bdevname(rdev
->bdev
, b
), raid_disk
);
5753 } else if (rdev
->saved_raid_disk
!= raid_disk
)
5754 /* Cannot rely on bitmap to complete recovery */
5758 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
5759 conf
->level
= mddev
->new_level
;
5760 if (conf
->level
== 6)
5761 conf
->max_degraded
= 2;
5763 conf
->max_degraded
= 1;
5764 conf
->algorithm
= mddev
->new_layout
;
5765 conf
->reshape_progress
= mddev
->reshape_position
;
5766 if (conf
->reshape_progress
!= MaxSector
) {
5767 conf
->prev_chunk_sectors
= mddev
->chunk_sectors
;
5768 conf
->prev_algo
= mddev
->layout
;
5771 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
5772 max_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
5773 atomic_set(&conf
->empty_inactive_list_nr
, NR_STRIPE_HASH_LOCKS
);
5774 if (grow_stripes(conf
, NR_STRIPES
)) {
5776 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5777 mdname(mddev
), memory
);
5780 printk(KERN_INFO
"md/raid:%s: allocated %dkB\n",
5781 mdname(mddev
), memory
);
5783 sprintf(pers_name
, "raid%d", mddev
->new_level
);
5784 conf
->thread
= md_register_thread(raid5d
, mddev
, pers_name
);
5785 if (!conf
->thread
) {
5787 "md/raid:%s: couldn't allocate thread.\n",
5797 return ERR_PTR(-EIO
);
5799 return ERR_PTR(-ENOMEM
);
5803 static int only_parity(int raid_disk
, int algo
, int raid_disks
, int max_degraded
)
5806 case ALGORITHM_PARITY_0
:
5807 if (raid_disk
< max_degraded
)
5810 case ALGORITHM_PARITY_N
:
5811 if (raid_disk
>= raid_disks
- max_degraded
)
5814 case ALGORITHM_PARITY_0_6
:
5815 if (raid_disk
== 0 ||
5816 raid_disk
== raid_disks
- 1)
5819 case ALGORITHM_LEFT_ASYMMETRIC_6
:
5820 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
5821 case ALGORITHM_LEFT_SYMMETRIC_6
:
5822 case ALGORITHM_RIGHT_SYMMETRIC_6
:
5823 if (raid_disk
== raid_disks
- 1)
5829 static int run(struct mddev
*mddev
)
5831 struct r5conf
*conf
;
5832 int working_disks
= 0;
5833 int dirty_parity_disks
= 0;
5834 struct md_rdev
*rdev
;
5835 sector_t reshape_offset
= 0;
5837 long long min_offset_diff
= 0;
5840 if (mddev
->recovery_cp
!= MaxSector
)
5841 printk(KERN_NOTICE
"md/raid:%s: not clean"
5842 " -- starting background reconstruction\n",
5845 rdev_for_each(rdev
, mddev
) {
5847 if (rdev
->raid_disk
< 0)
5849 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
5851 min_offset_diff
= diff
;
5853 } else if (mddev
->reshape_backwards
&&
5854 diff
< min_offset_diff
)
5855 min_offset_diff
= diff
;
5856 else if (!mddev
->reshape_backwards
&&
5857 diff
> min_offset_diff
)
5858 min_offset_diff
= diff
;
5861 if (mddev
->reshape_position
!= MaxSector
) {
5862 /* Check that we can continue the reshape.
5863 * Difficulties arise if the stripe we would write to
5864 * next is at or after the stripe we would read from next.
5865 * For a reshape that changes the number of devices, this
5866 * is only possible for a very short time, and mdadm makes
5867 * sure that time appears to have past before assembling
5868 * the array. So we fail if that time hasn't passed.
5869 * For a reshape that keeps the number of devices the same
5870 * mdadm must be monitoring the reshape can keeping the
5871 * critical areas read-only and backed up. It will start
5872 * the array in read-only mode, so we check for that.
5874 sector_t here_new
, here_old
;
5876 int max_degraded
= (mddev
->level
== 6 ? 2 : 1);
5878 if (mddev
->new_level
!= mddev
->level
) {
5879 printk(KERN_ERR
"md/raid:%s: unsupported reshape "
5880 "required - aborting.\n",
5884 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
5885 /* reshape_position must be on a new-stripe boundary, and one
5886 * further up in new geometry must map after here in old
5889 here_new
= mddev
->reshape_position
;
5890 if (sector_div(here_new
, mddev
->new_chunk_sectors
*
5891 (mddev
->raid_disks
- max_degraded
))) {
5892 printk(KERN_ERR
"md/raid:%s: reshape_position not "
5893 "on a stripe boundary\n", mdname(mddev
));
5896 reshape_offset
= here_new
* mddev
->new_chunk_sectors
;
5897 /* here_new is the stripe we will write to */
5898 here_old
= mddev
->reshape_position
;
5899 sector_div(here_old
, mddev
->chunk_sectors
*
5900 (old_disks
-max_degraded
));
5901 /* here_old is the first stripe that we might need to read
5903 if (mddev
->delta_disks
== 0) {
5904 if ((here_new
* mddev
->new_chunk_sectors
!=
5905 here_old
* mddev
->chunk_sectors
)) {
5906 printk(KERN_ERR
"md/raid:%s: reshape position is"
5907 " confused - aborting\n", mdname(mddev
));
5910 /* We cannot be sure it is safe to start an in-place
5911 * reshape. It is only safe if user-space is monitoring
5912 * and taking constant backups.
5913 * mdadm always starts a situation like this in
5914 * readonly mode so it can take control before
5915 * allowing any writes. So just check for that.
5917 if (abs(min_offset_diff
) >= mddev
->chunk_sectors
&&
5918 abs(min_offset_diff
) >= mddev
->new_chunk_sectors
)
5919 /* not really in-place - so OK */;
5920 else if (mddev
->ro
== 0) {
5921 printk(KERN_ERR
"md/raid:%s: in-place reshape "
5922 "must be started in read-only mode "
5927 } else if (mddev
->reshape_backwards
5928 ? (here_new
* mddev
->new_chunk_sectors
+ min_offset_diff
<=
5929 here_old
* mddev
->chunk_sectors
)
5930 : (here_new
* mddev
->new_chunk_sectors
>=
5931 here_old
* mddev
->chunk_sectors
+ (-min_offset_diff
))) {
5932 /* Reading from the same stripe as writing to - bad */
5933 printk(KERN_ERR
"md/raid:%s: reshape_position too early for "
5934 "auto-recovery - aborting.\n",
5938 printk(KERN_INFO
"md/raid:%s: reshape will continue\n",
5940 /* OK, we should be able to continue; */
5942 BUG_ON(mddev
->level
!= mddev
->new_level
);
5943 BUG_ON(mddev
->layout
!= mddev
->new_layout
);
5944 BUG_ON(mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
);
5945 BUG_ON(mddev
->delta_disks
!= 0);
5948 if (mddev
->private == NULL
)
5949 conf
= setup_conf(mddev
);
5951 conf
= mddev
->private;
5954 return PTR_ERR(conf
);
5956 conf
->min_offset_diff
= min_offset_diff
;
5957 mddev
->thread
= conf
->thread
;
5958 conf
->thread
= NULL
;
5959 mddev
->private = conf
;
5961 for (i
= 0; i
< conf
->raid_disks
&& conf
->previous_raid_disks
;
5963 rdev
= conf
->disks
[i
].rdev
;
5964 if (!rdev
&& conf
->disks
[i
].replacement
) {
5965 /* The replacement is all we have yet */
5966 rdev
= conf
->disks
[i
].replacement
;
5967 conf
->disks
[i
].replacement
= NULL
;
5968 clear_bit(Replacement
, &rdev
->flags
);
5969 conf
->disks
[i
].rdev
= rdev
;
5973 if (conf
->disks
[i
].replacement
&&
5974 conf
->reshape_progress
!= MaxSector
) {
5975 /* replacements and reshape simply do not mix. */
5976 printk(KERN_ERR
"md: cannot handle concurrent "
5977 "replacement and reshape.\n");
5980 if (test_bit(In_sync
, &rdev
->flags
)) {
5984 /* This disc is not fully in-sync. However if it
5985 * just stored parity (beyond the recovery_offset),
5986 * when we don't need to be concerned about the
5987 * array being dirty.
5988 * When reshape goes 'backwards', we never have
5989 * partially completed devices, so we only need
5990 * to worry about reshape going forwards.
5992 /* Hack because v0.91 doesn't store recovery_offset properly. */
5993 if (mddev
->major_version
== 0 &&
5994 mddev
->minor_version
> 90)
5995 rdev
->recovery_offset
= reshape_offset
;
5997 if (rdev
->recovery_offset
< reshape_offset
) {
5998 /* We need to check old and new layout */
5999 if (!only_parity(rdev
->raid_disk
,
6002 conf
->max_degraded
))
6005 if (!only_parity(rdev
->raid_disk
,
6007 conf
->previous_raid_disks
,
6008 conf
->max_degraded
))
6010 dirty_parity_disks
++;
6014 * 0 for a fully functional array, 1 or 2 for a degraded array.
6016 mddev
->degraded
= calc_degraded(conf
);
6018 if (has_failed(conf
)) {
6019 printk(KERN_ERR
"md/raid:%s: not enough operational devices"
6020 " (%d/%d failed)\n",
6021 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
6025 /* device size must be a multiple of chunk size */
6026 mddev
->dev_sectors
&= ~(mddev
->chunk_sectors
- 1);
6027 mddev
->resync_max_sectors
= mddev
->dev_sectors
;
6029 if (mddev
->degraded
> dirty_parity_disks
&&
6030 mddev
->recovery_cp
!= MaxSector
) {
6031 if (mddev
->ok_start_degraded
)
6033 "md/raid:%s: starting dirty degraded array"
6034 " - data corruption possible.\n",
6038 "md/raid:%s: cannot start dirty degraded array.\n",
6044 if (mddev
->degraded
== 0)
6045 printk(KERN_INFO
"md/raid:%s: raid level %d active with %d out of %d"
6046 " devices, algorithm %d\n", mdname(mddev
), conf
->level
,
6047 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
6050 printk(KERN_ALERT
"md/raid:%s: raid level %d active with %d"
6051 " out of %d devices, algorithm %d\n",
6052 mdname(mddev
), conf
->level
,
6053 mddev
->raid_disks
- mddev
->degraded
,
6054 mddev
->raid_disks
, mddev
->new_layout
);
6056 print_raid5_conf(conf
);
6058 if (conf
->reshape_progress
!= MaxSector
) {
6059 conf
->reshape_safe
= conf
->reshape_progress
;
6060 atomic_set(&conf
->reshape_stripes
, 0);
6061 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6062 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6063 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6064 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6065 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
6070 /* Ok, everything is just fine now */
6071 if (mddev
->to_remove
== &raid5_attrs_group
)
6072 mddev
->to_remove
= NULL
;
6073 else if (mddev
->kobj
.sd
&&
6074 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
))
6076 "raid5: failed to create sysfs attributes for %s\n",
6078 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
6082 bool discard_supported
= true;
6083 /* read-ahead size must cover two whole stripes, which
6084 * is 2 * (datadisks) * chunksize where 'n' is the
6085 * number of raid devices
6087 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
6088 int stripe
= data_disks
*
6089 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
6090 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
6091 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
6093 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
6095 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
6096 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
6098 chunk_size
= mddev
->chunk_sectors
<< 9;
6099 blk_queue_io_min(mddev
->queue
, chunk_size
);
6100 blk_queue_io_opt(mddev
->queue
, chunk_size
*
6101 (conf
->raid_disks
- conf
->max_degraded
));
6103 * We can only discard a whole stripe. It doesn't make sense to
6104 * discard data disk but write parity disk
6106 stripe
= stripe
* PAGE_SIZE
;
6107 /* Round up to power of 2, as discard handling
6108 * currently assumes that */
6109 while ((stripe
-1) & stripe
)
6110 stripe
= (stripe
| (stripe
-1)) + 1;
6111 mddev
->queue
->limits
.discard_alignment
= stripe
;
6112 mddev
->queue
->limits
.discard_granularity
= stripe
;
6114 * unaligned part of discard request will be ignored, so can't
6115 * guarantee discard_zerors_data
6117 mddev
->queue
->limits
.discard_zeroes_data
= 0;
6119 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
6121 rdev_for_each(rdev
, mddev
) {
6122 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
6123 rdev
->data_offset
<< 9);
6124 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
6125 rdev
->new_data_offset
<< 9);
6127 * discard_zeroes_data is required, otherwise data
6128 * could be lost. Consider a scenario: discard a stripe
6129 * (the stripe could be inconsistent if
6130 * discard_zeroes_data is 0); write one disk of the
6131 * stripe (the stripe could be inconsistent again
6132 * depending on which disks are used to calculate
6133 * parity); the disk is broken; The stripe data of this
6136 if (!blk_queue_discard(bdev_get_queue(rdev
->bdev
)) ||
6137 !bdev_get_queue(rdev
->bdev
)->
6138 limits
.discard_zeroes_data
)
6139 discard_supported
= false;
6142 if (discard_supported
&&
6143 mddev
->queue
->limits
.max_discard_sectors
>= stripe
&&
6144 mddev
->queue
->limits
.discard_granularity
>= stripe
)
6145 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
6148 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
6154 md_unregister_thread(&mddev
->thread
);
6155 print_raid5_conf(conf
);
6157 mddev
->private = NULL
;
6158 printk(KERN_ALERT
"md/raid:%s: failed to run raid set.\n", mdname(mddev
));
6162 static int stop(struct mddev
*mddev
)
6164 struct r5conf
*conf
= mddev
->private;
6166 md_unregister_thread(&mddev
->thread
);
6168 mddev
->queue
->backing_dev_info
.congested_fn
= NULL
;
6170 mddev
->private = NULL
;
6171 mddev
->to_remove
= &raid5_attrs_group
;
6175 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
6177 struct r5conf
*conf
= mddev
->private;
6180 seq_printf(seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
,
6181 mddev
->chunk_sectors
/ 2, mddev
->layout
);
6182 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
6183 for (i
= 0; i
< conf
->raid_disks
; i
++)
6184 seq_printf (seq
, "%s",
6185 conf
->disks
[i
].rdev
&&
6186 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
6187 seq_printf (seq
, "]");
6190 static void print_raid5_conf (struct r5conf
*conf
)
6193 struct disk_info
*tmp
;
6195 printk(KERN_DEBUG
"RAID conf printout:\n");
6197 printk("(conf==NULL)\n");
6200 printk(KERN_DEBUG
" --- level:%d rd:%d wd:%d\n", conf
->level
,
6202 conf
->raid_disks
- conf
->mddev
->degraded
);
6204 for (i
= 0; i
< conf
->raid_disks
; i
++) {
6205 char b
[BDEVNAME_SIZE
];
6206 tmp
= conf
->disks
+ i
;
6208 printk(KERN_DEBUG
" disk %d, o:%d, dev:%s\n",
6209 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
6210 bdevname(tmp
->rdev
->bdev
, b
));
6214 static int raid5_spare_active(struct mddev
*mddev
)
6217 struct r5conf
*conf
= mddev
->private;
6218 struct disk_info
*tmp
;
6220 unsigned long flags
;
6222 for (i
= 0; i
< conf
->raid_disks
; i
++) {
6223 tmp
= conf
->disks
+ i
;
6224 if (tmp
->replacement
6225 && tmp
->replacement
->recovery_offset
== MaxSector
6226 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
6227 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
6228 /* Replacement has just become active. */
6230 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
6233 /* Replaced device not technically faulty,
6234 * but we need to be sure it gets removed
6235 * and never re-added.
6237 set_bit(Faulty
, &tmp
->rdev
->flags
);
6238 sysfs_notify_dirent_safe(
6239 tmp
->rdev
->sysfs_state
);
6241 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
6242 } else if (tmp
->rdev
6243 && tmp
->rdev
->recovery_offset
== MaxSector
6244 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
6245 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
6247 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
6250 spin_lock_irqsave(&conf
->device_lock
, flags
);
6251 mddev
->degraded
= calc_degraded(conf
);
6252 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
6253 print_raid5_conf(conf
);
6257 static int raid5_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
6259 struct r5conf
*conf
= mddev
->private;
6261 int number
= rdev
->raid_disk
;
6262 struct md_rdev
**rdevp
;
6263 struct disk_info
*p
= conf
->disks
+ number
;
6265 print_raid5_conf(conf
);
6266 if (rdev
== p
->rdev
)
6268 else if (rdev
== p
->replacement
)
6269 rdevp
= &p
->replacement
;
6273 if (number
>= conf
->raid_disks
&&
6274 conf
->reshape_progress
== MaxSector
)
6275 clear_bit(In_sync
, &rdev
->flags
);
6277 if (test_bit(In_sync
, &rdev
->flags
) ||
6278 atomic_read(&rdev
->nr_pending
)) {
6282 /* Only remove non-faulty devices if recovery
6285 if (!test_bit(Faulty
, &rdev
->flags
) &&
6286 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
6287 !has_failed(conf
) &&
6288 (!p
->replacement
|| p
->replacement
== rdev
) &&
6289 number
< conf
->raid_disks
) {
6295 if (atomic_read(&rdev
->nr_pending
)) {
6296 /* lost the race, try later */
6299 } else if (p
->replacement
) {
6300 /* We must have just cleared 'rdev' */
6301 p
->rdev
= p
->replacement
;
6302 clear_bit(Replacement
, &p
->replacement
->flags
);
6303 smp_mb(); /* Make sure other CPUs may see both as identical
6304 * but will never see neither - if they are careful
6306 p
->replacement
= NULL
;
6307 clear_bit(WantReplacement
, &rdev
->flags
);
6309 /* We might have just removed the Replacement as faulty-
6310 * clear the bit just in case
6312 clear_bit(WantReplacement
, &rdev
->flags
);
6315 print_raid5_conf(conf
);
6319 static int raid5_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
6321 struct r5conf
*conf
= mddev
->private;
6324 struct disk_info
*p
;
6326 int last
= conf
->raid_disks
- 1;
6328 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
6331 if (rdev
->saved_raid_disk
< 0 && has_failed(conf
))
6332 /* no point adding a device */
6335 if (rdev
->raid_disk
>= 0)
6336 first
= last
= rdev
->raid_disk
;
6339 * find the disk ... but prefer rdev->saved_raid_disk
6342 if (rdev
->saved_raid_disk
>= 0 &&
6343 rdev
->saved_raid_disk
>= first
&&
6344 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
6345 first
= rdev
->saved_raid_disk
;
6347 for (disk
= first
; disk
<= last
; disk
++) {
6348 p
= conf
->disks
+ disk
;
6349 if (p
->rdev
== NULL
) {
6350 clear_bit(In_sync
, &rdev
->flags
);
6351 rdev
->raid_disk
= disk
;
6353 if (rdev
->saved_raid_disk
!= disk
)
6355 rcu_assign_pointer(p
->rdev
, rdev
);
6359 for (disk
= first
; disk
<= last
; disk
++) {
6360 p
= conf
->disks
+ disk
;
6361 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
6362 p
->replacement
== NULL
) {
6363 clear_bit(In_sync
, &rdev
->flags
);
6364 set_bit(Replacement
, &rdev
->flags
);
6365 rdev
->raid_disk
= disk
;
6368 rcu_assign_pointer(p
->replacement
, rdev
);
6373 print_raid5_conf(conf
);
6377 static int raid5_resize(struct mddev
*mddev
, sector_t sectors
)
6379 /* no resync is happening, and there is enough space
6380 * on all devices, so we can resize.
6381 * We need to make sure resync covers any new space.
6382 * If the array is shrinking we should possibly wait until
6383 * any io in the removed space completes, but it hardly seems
6387 sectors
&= ~((sector_t
)mddev
->chunk_sectors
- 1);
6388 newsize
= raid5_size(mddev
, sectors
, mddev
->raid_disks
);
6389 if (mddev
->external_size
&&
6390 mddev
->array_sectors
> newsize
)
6392 if (mddev
->bitmap
) {
6393 int ret
= bitmap_resize(mddev
->bitmap
, sectors
, 0, 0);
6397 md_set_array_sectors(mddev
, newsize
);
6398 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6399 revalidate_disk(mddev
->gendisk
);
6400 if (sectors
> mddev
->dev_sectors
&&
6401 mddev
->recovery_cp
> mddev
->dev_sectors
) {
6402 mddev
->recovery_cp
= mddev
->dev_sectors
;
6403 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
6405 mddev
->dev_sectors
= sectors
;
6406 mddev
->resync_max_sectors
= sectors
;
6410 static int check_stripe_cache(struct mddev
*mddev
)
6412 /* Can only proceed if there are plenty of stripe_heads.
6413 * We need a minimum of one full stripe,, and for sensible progress
6414 * it is best to have about 4 times that.
6415 * If we require 4 times, then the default 256 4K stripe_heads will
6416 * allow for chunk sizes up to 256K, which is probably OK.
6417 * If the chunk size is greater, user-space should request more
6418 * stripe_heads first.
6420 struct r5conf
*conf
= mddev
->private;
6421 if (((mddev
->chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
6422 > conf
->max_nr_stripes
||
6423 ((mddev
->new_chunk_sectors
<< 9) / STRIPE_SIZE
) * 4
6424 > conf
->max_nr_stripes
) {
6425 printk(KERN_WARNING
"md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6427 ((max(mddev
->chunk_sectors
, mddev
->new_chunk_sectors
) << 9)
6434 static int check_reshape(struct mddev
*mddev
)
6436 struct r5conf
*conf
= mddev
->private;
6438 if (mddev
->delta_disks
== 0 &&
6439 mddev
->new_layout
== mddev
->layout
&&
6440 mddev
->new_chunk_sectors
== mddev
->chunk_sectors
)
6441 return 0; /* nothing to do */
6442 if (has_failed(conf
))
6444 if (mddev
->delta_disks
< 0 && mddev
->reshape_position
== MaxSector
) {
6445 /* We might be able to shrink, but the devices must
6446 * be made bigger first.
6447 * For raid6, 4 is the minimum size.
6448 * Otherwise 2 is the minimum
6451 if (mddev
->level
== 6)
6453 if (mddev
->raid_disks
+ mddev
->delta_disks
< min
)
6457 if (!check_stripe_cache(mddev
))
6460 return resize_stripes(conf
, (conf
->previous_raid_disks
6461 + mddev
->delta_disks
));
6464 static int raid5_start_reshape(struct mddev
*mddev
)
6466 struct r5conf
*conf
= mddev
->private;
6467 struct md_rdev
*rdev
;
6469 unsigned long flags
;
6471 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
6474 if (!check_stripe_cache(mddev
))
6477 if (has_failed(conf
))
6480 rdev_for_each(rdev
, mddev
) {
6481 if (!test_bit(In_sync
, &rdev
->flags
)
6482 && !test_bit(Faulty
, &rdev
->flags
))
6486 if (spares
- mddev
->degraded
< mddev
->delta_disks
- conf
->max_degraded
)
6487 /* Not enough devices even to make a degraded array
6492 /* Refuse to reduce size of the array. Any reductions in
6493 * array size must be through explicit setting of array_size
6496 if (raid5_size(mddev
, 0, conf
->raid_disks
+ mddev
->delta_disks
)
6497 < mddev
->array_sectors
) {
6498 printk(KERN_ERR
"md/raid:%s: array size must be reduced "
6499 "before number of disks\n", mdname(mddev
));
6503 atomic_set(&conf
->reshape_stripes
, 0);
6504 spin_lock_irq(&conf
->device_lock
);
6505 write_seqcount_begin(&conf
->gen_lock
);
6506 conf
->previous_raid_disks
= conf
->raid_disks
;
6507 conf
->raid_disks
+= mddev
->delta_disks
;
6508 conf
->prev_chunk_sectors
= conf
->chunk_sectors
;
6509 conf
->chunk_sectors
= mddev
->new_chunk_sectors
;
6510 conf
->prev_algo
= conf
->algorithm
;
6511 conf
->algorithm
= mddev
->new_layout
;
6513 /* Code that selects data_offset needs to see the generation update
6514 * if reshape_progress has been set - so a memory barrier needed.
6517 if (mddev
->reshape_backwards
)
6518 conf
->reshape_progress
= raid5_size(mddev
, 0, 0);
6520 conf
->reshape_progress
= 0;
6521 conf
->reshape_safe
= conf
->reshape_progress
;
6522 write_seqcount_end(&conf
->gen_lock
);
6523 spin_unlock_irq(&conf
->device_lock
);
6525 /* Now make sure any requests that proceeded on the assumption
6526 * the reshape wasn't running - like Discard or Read - have
6529 mddev_suspend(mddev
);
6530 mddev_resume(mddev
);
6532 /* Add some new drives, as many as will fit.
6533 * We know there are enough to make the newly sized array work.
6534 * Don't add devices if we are reducing the number of
6535 * devices in the array. This is because it is not possible
6536 * to correctly record the "partially reconstructed" state of
6537 * such devices during the reshape and confusion could result.
6539 if (mddev
->delta_disks
>= 0) {
6540 rdev_for_each(rdev
, mddev
)
6541 if (rdev
->raid_disk
< 0 &&
6542 !test_bit(Faulty
, &rdev
->flags
)) {
6543 if (raid5_add_disk(mddev
, rdev
) == 0) {
6545 >= conf
->previous_raid_disks
)
6546 set_bit(In_sync
, &rdev
->flags
);
6548 rdev
->recovery_offset
= 0;
6550 if (sysfs_link_rdev(mddev
, rdev
))
6551 /* Failure here is OK */;
6553 } else if (rdev
->raid_disk
>= conf
->previous_raid_disks
6554 && !test_bit(Faulty
, &rdev
->flags
)) {
6555 /* This is a spare that was manually added */
6556 set_bit(In_sync
, &rdev
->flags
);
6559 /* When a reshape changes the number of devices,
6560 * ->degraded is measured against the larger of the
6561 * pre and post number of devices.
6563 spin_lock_irqsave(&conf
->device_lock
, flags
);
6564 mddev
->degraded
= calc_degraded(conf
);
6565 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
6567 mddev
->raid_disks
= conf
->raid_disks
;
6568 mddev
->reshape_position
= conf
->reshape_progress
;
6569 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6571 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
6572 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
6573 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
6574 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
6575 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
6577 if (!mddev
->sync_thread
) {
6578 mddev
->recovery
= 0;
6579 spin_lock_irq(&conf
->device_lock
);
6580 write_seqcount_begin(&conf
->gen_lock
);
6581 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
6582 mddev
->new_chunk_sectors
=
6583 conf
->chunk_sectors
= conf
->prev_chunk_sectors
;
6584 mddev
->new_layout
= conf
->algorithm
= conf
->prev_algo
;
6585 rdev_for_each(rdev
, mddev
)
6586 rdev
->new_data_offset
= rdev
->data_offset
;
6588 conf
->generation
--;
6589 conf
->reshape_progress
= MaxSector
;
6590 mddev
->reshape_position
= MaxSector
;
6591 write_seqcount_end(&conf
->gen_lock
);
6592 spin_unlock_irq(&conf
->device_lock
);
6595 conf
->reshape_checkpoint
= jiffies
;
6596 md_wakeup_thread(mddev
->sync_thread
);
6597 md_new_event(mddev
);
6601 /* This is called from the reshape thread and should make any
6602 * changes needed in 'conf'
6604 static void end_reshape(struct r5conf
*conf
)
6607 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
6608 struct md_rdev
*rdev
;
6610 spin_lock_irq(&conf
->device_lock
);
6611 conf
->previous_raid_disks
= conf
->raid_disks
;
6612 rdev_for_each(rdev
, conf
->mddev
)
6613 rdev
->data_offset
= rdev
->new_data_offset
;
6615 conf
->reshape_progress
= MaxSector
;
6616 spin_unlock_irq(&conf
->device_lock
);
6617 wake_up(&conf
->wait_for_overlap
);
6619 /* read-ahead size must cover two whole stripes, which is
6620 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6622 if (conf
->mddev
->queue
) {
6623 int data_disks
= conf
->raid_disks
- conf
->max_degraded
;
6624 int stripe
= data_disks
* ((conf
->chunk_sectors
<< 9)
6626 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
6627 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
6632 /* This is called from the raid5d thread with mddev_lock held.
6633 * It makes config changes to the device.
6635 static void raid5_finish_reshape(struct mddev
*mddev
)
6637 struct r5conf
*conf
= mddev
->private;
6639 if (!test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
6641 if (mddev
->delta_disks
> 0) {
6642 md_set_array_sectors(mddev
, raid5_size(mddev
, 0, 0));
6643 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
6644 revalidate_disk(mddev
->gendisk
);
6647 spin_lock_irq(&conf
->device_lock
);
6648 mddev
->degraded
= calc_degraded(conf
);
6649 spin_unlock_irq(&conf
->device_lock
);
6650 for (d
= conf
->raid_disks
;
6651 d
< conf
->raid_disks
- mddev
->delta_disks
;
6653 struct md_rdev
*rdev
= conf
->disks
[d
].rdev
;
6655 clear_bit(In_sync
, &rdev
->flags
);
6656 rdev
= conf
->disks
[d
].replacement
;
6658 clear_bit(In_sync
, &rdev
->flags
);
6661 mddev
->layout
= conf
->algorithm
;
6662 mddev
->chunk_sectors
= conf
->chunk_sectors
;
6663 mddev
->reshape_position
= MaxSector
;
6664 mddev
->delta_disks
= 0;
6665 mddev
->reshape_backwards
= 0;
6669 static void raid5_quiesce(struct mddev
*mddev
, int state
)
6671 struct r5conf
*conf
= mddev
->private;
6674 case 2: /* resume for a suspend */
6675 wake_up(&conf
->wait_for_overlap
);
6678 case 1: /* stop all writes */
6679 lock_all_device_hash_locks_irq(conf
);
6680 /* '2' tells resync/reshape to pause so that all
6681 * active stripes can drain
6684 wait_event_cmd(conf
->wait_for_stripe
,
6685 atomic_read(&conf
->active_stripes
) == 0 &&
6686 atomic_read(&conf
->active_aligned_reads
) == 0,
6687 unlock_all_device_hash_locks_irq(conf
),
6688 lock_all_device_hash_locks_irq(conf
));
6690 unlock_all_device_hash_locks_irq(conf
);
6691 /* allow reshape to continue */
6692 wake_up(&conf
->wait_for_overlap
);
6695 case 0: /* re-enable writes */
6696 lock_all_device_hash_locks_irq(conf
);
6698 wake_up(&conf
->wait_for_stripe
);
6699 wake_up(&conf
->wait_for_overlap
);
6700 unlock_all_device_hash_locks_irq(conf
);
6706 static void *raid45_takeover_raid0(struct mddev
*mddev
, int level
)
6708 struct r0conf
*raid0_conf
= mddev
->private;
6711 /* for raid0 takeover only one zone is supported */
6712 if (raid0_conf
->nr_strip_zones
> 1) {
6713 printk(KERN_ERR
"md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6715 return ERR_PTR(-EINVAL
);
6718 sectors
= raid0_conf
->strip_zone
[0].zone_end
;
6719 sector_div(sectors
, raid0_conf
->strip_zone
[0].nb_dev
);
6720 mddev
->dev_sectors
= sectors
;
6721 mddev
->new_level
= level
;
6722 mddev
->new_layout
= ALGORITHM_PARITY_N
;
6723 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
6724 mddev
->raid_disks
+= 1;
6725 mddev
->delta_disks
= 1;
6726 /* make sure it will be not marked as dirty */
6727 mddev
->recovery_cp
= MaxSector
;
6729 return setup_conf(mddev
);
6733 static void *raid5_takeover_raid1(struct mddev
*mddev
)
6737 if (mddev
->raid_disks
!= 2 ||
6738 mddev
->degraded
> 1)
6739 return ERR_PTR(-EINVAL
);
6741 /* Should check if there are write-behind devices? */
6743 chunksect
= 64*2; /* 64K by default */
6745 /* The array must be an exact multiple of chunksize */
6746 while (chunksect
&& (mddev
->array_sectors
& (chunksect
-1)))
6749 if ((chunksect
<<9) < STRIPE_SIZE
)
6750 /* array size does not allow a suitable chunk size */
6751 return ERR_PTR(-EINVAL
);
6753 mddev
->new_level
= 5;
6754 mddev
->new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
6755 mddev
->new_chunk_sectors
= chunksect
;
6757 return setup_conf(mddev
);
6760 static void *raid5_takeover_raid6(struct mddev
*mddev
)
6764 switch (mddev
->layout
) {
6765 case ALGORITHM_LEFT_ASYMMETRIC_6
:
6766 new_layout
= ALGORITHM_LEFT_ASYMMETRIC
;
6768 case ALGORITHM_RIGHT_ASYMMETRIC_6
:
6769 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
6771 case ALGORITHM_LEFT_SYMMETRIC_6
:
6772 new_layout
= ALGORITHM_LEFT_SYMMETRIC
;
6774 case ALGORITHM_RIGHT_SYMMETRIC_6
:
6775 new_layout
= ALGORITHM_RIGHT_SYMMETRIC
;
6777 case ALGORITHM_PARITY_0_6
:
6778 new_layout
= ALGORITHM_PARITY_0
;
6780 case ALGORITHM_PARITY_N
:
6781 new_layout
= ALGORITHM_PARITY_N
;
6784 return ERR_PTR(-EINVAL
);
6786 mddev
->new_level
= 5;
6787 mddev
->new_layout
= new_layout
;
6788 mddev
->delta_disks
= -1;
6789 mddev
->raid_disks
-= 1;
6790 return setup_conf(mddev
);
6794 static int raid5_check_reshape(struct mddev
*mddev
)
6796 /* For a 2-drive array, the layout and chunk size can be changed
6797 * immediately as not restriping is needed.
6798 * For larger arrays we record the new value - after validation
6799 * to be used by a reshape pass.
6801 struct r5conf
*conf
= mddev
->private;
6802 int new_chunk
= mddev
->new_chunk_sectors
;
6804 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid5(mddev
->new_layout
))
6806 if (new_chunk
> 0) {
6807 if (!is_power_of_2(new_chunk
))
6809 if (new_chunk
< (PAGE_SIZE
>>9))
6811 if (mddev
->array_sectors
& (new_chunk
-1))
6812 /* not factor of array size */
6816 /* They look valid */
6818 if (mddev
->raid_disks
== 2) {
6819 /* can make the change immediately */
6820 if (mddev
->new_layout
>= 0) {
6821 conf
->algorithm
= mddev
->new_layout
;
6822 mddev
->layout
= mddev
->new_layout
;
6824 if (new_chunk
> 0) {
6825 conf
->chunk_sectors
= new_chunk
;
6826 mddev
->chunk_sectors
= new_chunk
;
6828 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
6829 md_wakeup_thread(mddev
->thread
);
6831 return check_reshape(mddev
);
6834 static int raid6_check_reshape(struct mddev
*mddev
)
6836 int new_chunk
= mddev
->new_chunk_sectors
;
6838 if (mddev
->new_layout
>= 0 && !algorithm_valid_raid6(mddev
->new_layout
))
6840 if (new_chunk
> 0) {
6841 if (!is_power_of_2(new_chunk
))
6843 if (new_chunk
< (PAGE_SIZE
>> 9))
6845 if (mddev
->array_sectors
& (new_chunk
-1))
6846 /* not factor of array size */
6850 /* They look valid */
6851 return check_reshape(mddev
);
6854 static void *raid5_takeover(struct mddev
*mddev
)
6856 /* raid5 can take over:
6857 * raid0 - if there is only one strip zone - make it a raid4 layout
6858 * raid1 - if there are two drives. We need to know the chunk size
6859 * raid4 - trivial - just use a raid4 layout.
6860 * raid6 - Providing it is a *_6 layout
6862 if (mddev
->level
== 0)
6863 return raid45_takeover_raid0(mddev
, 5);
6864 if (mddev
->level
== 1)
6865 return raid5_takeover_raid1(mddev
);
6866 if (mddev
->level
== 4) {
6867 mddev
->new_layout
= ALGORITHM_PARITY_N
;
6868 mddev
->new_level
= 5;
6869 return setup_conf(mddev
);
6871 if (mddev
->level
== 6)
6872 return raid5_takeover_raid6(mddev
);
6874 return ERR_PTR(-EINVAL
);
6877 static void *raid4_takeover(struct mddev
*mddev
)
6879 /* raid4 can take over:
6880 * raid0 - if there is only one strip zone
6881 * raid5 - if layout is right
6883 if (mddev
->level
== 0)
6884 return raid45_takeover_raid0(mddev
, 4);
6885 if (mddev
->level
== 5 &&
6886 mddev
->layout
== ALGORITHM_PARITY_N
) {
6887 mddev
->new_layout
= 0;
6888 mddev
->new_level
= 4;
6889 return setup_conf(mddev
);
6891 return ERR_PTR(-EINVAL
);
6894 static struct md_personality raid5_personality
;
6896 static void *raid6_takeover(struct mddev
*mddev
)
6898 /* Currently can only take over a raid5. We map the
6899 * personality to an equivalent raid6 personality
6900 * with the Q block at the end.
6904 if (mddev
->pers
!= &raid5_personality
)
6905 return ERR_PTR(-EINVAL
);
6906 if (mddev
->degraded
> 1)
6907 return ERR_PTR(-EINVAL
);
6908 if (mddev
->raid_disks
> 253)
6909 return ERR_PTR(-EINVAL
);
6910 if (mddev
->raid_disks
< 3)
6911 return ERR_PTR(-EINVAL
);
6913 switch (mddev
->layout
) {
6914 case ALGORITHM_LEFT_ASYMMETRIC
:
6915 new_layout
= ALGORITHM_LEFT_ASYMMETRIC_6
;
6917 case ALGORITHM_RIGHT_ASYMMETRIC
:
6918 new_layout
= ALGORITHM_RIGHT_ASYMMETRIC_6
;
6920 case ALGORITHM_LEFT_SYMMETRIC
:
6921 new_layout
= ALGORITHM_LEFT_SYMMETRIC_6
;
6923 case ALGORITHM_RIGHT_SYMMETRIC
:
6924 new_layout
= ALGORITHM_RIGHT_SYMMETRIC_6
;
6926 case ALGORITHM_PARITY_0
:
6927 new_layout
= ALGORITHM_PARITY_0_6
;
6929 case ALGORITHM_PARITY_N
:
6930 new_layout
= ALGORITHM_PARITY_N
;
6933 return ERR_PTR(-EINVAL
);
6935 mddev
->new_level
= 6;
6936 mddev
->new_layout
= new_layout
;
6937 mddev
->delta_disks
= 1;
6938 mddev
->raid_disks
+= 1;
6939 return setup_conf(mddev
);
6943 static struct md_personality raid6_personality
=
6947 .owner
= THIS_MODULE
,
6948 .make_request
= make_request
,
6952 .error_handler
= error
,
6953 .hot_add_disk
= raid5_add_disk
,
6954 .hot_remove_disk
= raid5_remove_disk
,
6955 .spare_active
= raid5_spare_active
,
6956 .sync_request
= sync_request
,
6957 .resize
= raid5_resize
,
6959 .check_reshape
= raid6_check_reshape
,
6960 .start_reshape
= raid5_start_reshape
,
6961 .finish_reshape
= raid5_finish_reshape
,
6962 .quiesce
= raid5_quiesce
,
6963 .takeover
= raid6_takeover
,
6965 static struct md_personality raid5_personality
=
6969 .owner
= THIS_MODULE
,
6970 .make_request
= make_request
,
6974 .error_handler
= error
,
6975 .hot_add_disk
= raid5_add_disk
,
6976 .hot_remove_disk
= raid5_remove_disk
,
6977 .spare_active
= raid5_spare_active
,
6978 .sync_request
= sync_request
,
6979 .resize
= raid5_resize
,
6981 .check_reshape
= raid5_check_reshape
,
6982 .start_reshape
= raid5_start_reshape
,
6983 .finish_reshape
= raid5_finish_reshape
,
6984 .quiesce
= raid5_quiesce
,
6985 .takeover
= raid5_takeover
,
6988 static struct md_personality raid4_personality
=
6992 .owner
= THIS_MODULE
,
6993 .make_request
= make_request
,
6997 .error_handler
= error
,
6998 .hot_add_disk
= raid5_add_disk
,
6999 .hot_remove_disk
= raid5_remove_disk
,
7000 .spare_active
= raid5_spare_active
,
7001 .sync_request
= sync_request
,
7002 .resize
= raid5_resize
,
7004 .check_reshape
= raid5_check_reshape
,
7005 .start_reshape
= raid5_start_reshape
,
7006 .finish_reshape
= raid5_finish_reshape
,
7007 .quiesce
= raid5_quiesce
,
7008 .takeover
= raid4_takeover
,
7011 static int __init
raid5_init(void)
7013 raid5_wq
= alloc_workqueue("raid5wq",
7014 WQ_UNBOUND
|WQ_MEM_RECLAIM
|WQ_CPU_INTENSIVE
|WQ_SYSFS
, 0);
7017 register_md_personality(&raid6_personality
);
7018 register_md_personality(&raid5_personality
);
7019 register_md_personality(&raid4_personality
);
7023 static void raid5_exit(void)
7025 unregister_md_personality(&raid6_personality
);
7026 unregister_md_personality(&raid5_personality
);
7027 unregister_md_personality(&raid4_personality
);
7028 destroy_workqueue(raid5_wq
);
7031 module_init(raid5_init
);
7032 module_exit(raid5_exit
);
7033 MODULE_LICENSE("GPL");
7034 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
7035 MODULE_ALIAS("md-personality-4"); /* RAID5 */
7036 MODULE_ALIAS("md-raid5");
7037 MODULE_ALIAS("md-raid4");
7038 MODULE_ALIAS("md-level-5");
7039 MODULE_ALIAS("md-level-4");
7040 MODULE_ALIAS("md-personality-8"); /* RAID6 */
7041 MODULE_ALIAS("md-raid6");
7042 MODULE_ALIAS("md-level-6");
7044 /* This used to be two separate modules, they were: */
7045 MODULE_ALIAS("raid5");
7046 MODULE_ALIAS("raid6");