2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
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.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
34 * RAID10 provides a combination of RAID0 and RAID1 functionality.
35 * The layout of data is defined by
38 * near_copies (stored in low byte of layout)
39 * far_copies (stored in second byte of layout)
40 * far_offset (stored in bit 16 of layout )
42 * The data to be stored is divided into chunks using chunksize.
43 * Each device is divided into far_copies sections.
44 * In each section, chunks are laid out in a style similar to raid0, but
45 * near_copies copies of each chunk is stored (each on a different drive).
46 * The starting device for each section is offset near_copies from the starting
47 * device of the previous section.
48 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
50 * near_copies and far_copies must be at least one, and their product is at most
53 * If far_offset is true, then the far_copies are handled a bit differently.
54 * The copies are still in different stripes, but instead of be very far apart
55 * on disk, there are adjacent stripes.
59 * Number of guaranteed r10bios in case of extreme VM load:
61 #define NR_RAID10_BIOS 256
63 /* When there are this many requests queue to be written by
64 * the raid10 thread, we become 'congested' to provide back-pressure
67 static int max_queued_requests
= 1024;
69 static void allow_barrier(struct r10conf
*conf
);
70 static void lower_barrier(struct r10conf
*conf
);
71 static int enough(struct r10conf
*conf
, int ignore
);
72 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
74 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
);
75 static void end_reshape_write(struct bio
*bio
, int error
);
76 static void end_reshape(struct r10conf
*conf
);
78 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
80 struct r10conf
*conf
= data
;
81 int size
= offsetof(struct r10bio
, devs
[conf
->copies
]);
83 /* allocate a r10bio with room for raid_disks entries in the
85 return kzalloc(size
, gfp_flags
);
88 static void r10bio_pool_free(void *r10_bio
, void *data
)
93 /* Maximum size of each resync request */
94 #define RESYNC_BLOCK_SIZE (64*1024)
95 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
96 /* amount of memory to reserve for resync requests */
97 #define RESYNC_WINDOW (1024*1024)
98 /* maximum number of concurrent requests, memory permitting */
99 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
102 * When performing a resync, we need to read and compare, so
103 * we need as many pages are there are copies.
104 * When performing a recovery, we need 2 bios, one for read,
105 * one for write (we recover only one drive per r10buf)
108 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
110 struct r10conf
*conf
= data
;
112 struct r10bio
*r10_bio
;
117 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
121 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
122 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
123 nalloc
= conf
->copies
; /* resync */
125 nalloc
= 2; /* recovery */
130 for (j
= nalloc
; j
-- ; ) {
131 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
134 r10_bio
->devs
[j
].bio
= bio
;
135 if (!conf
->have_replacement
)
137 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
140 r10_bio
->devs
[j
].repl_bio
= bio
;
143 * Allocate RESYNC_PAGES data pages and attach them
146 for (j
= 0 ; j
< nalloc
; j
++) {
147 struct bio
*rbio
= r10_bio
->devs
[j
].repl_bio
;
148 bio
= r10_bio
->devs
[j
].bio
;
149 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
150 if (j
> 0 && !test_bit(MD_RECOVERY_SYNC
,
151 &conf
->mddev
->recovery
)) {
152 /* we can share bv_page's during recovery
154 struct bio
*rbio
= r10_bio
->devs
[0].bio
;
155 page
= rbio
->bi_io_vec
[i
].bv_page
;
158 page
= alloc_page(gfp_flags
);
162 bio
->bi_io_vec
[i
].bv_page
= page
;
164 rbio
->bi_io_vec
[i
].bv_page
= page
;
172 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
174 for (i
= 0; i
< RESYNC_PAGES
; i
++)
175 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
178 for ( ; j
< nalloc
; j
++) {
179 if (r10_bio
->devs
[j
].bio
)
180 bio_put(r10_bio
->devs
[j
].bio
);
181 if (r10_bio
->devs
[j
].repl_bio
)
182 bio_put(r10_bio
->devs
[j
].repl_bio
);
184 r10bio_pool_free(r10_bio
, conf
);
188 static void r10buf_pool_free(void *__r10_bio
, void *data
)
191 struct r10conf
*conf
= data
;
192 struct r10bio
*r10bio
= __r10_bio
;
195 for (j
=0; j
< conf
->copies
; j
++) {
196 struct bio
*bio
= r10bio
->devs
[j
].bio
;
198 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
199 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
200 bio
->bi_io_vec
[i
].bv_page
= NULL
;
204 bio
= r10bio
->devs
[j
].repl_bio
;
208 r10bio_pool_free(r10bio
, conf
);
211 static void put_all_bios(struct r10conf
*conf
, struct r10bio
*r10_bio
)
215 for (i
= 0; i
< conf
->copies
; i
++) {
216 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
217 if (!BIO_SPECIAL(*bio
))
220 bio
= &r10_bio
->devs
[i
].repl_bio
;
221 if (r10_bio
->read_slot
< 0 && !BIO_SPECIAL(*bio
))
227 static void free_r10bio(struct r10bio
*r10_bio
)
229 struct r10conf
*conf
= r10_bio
->mddev
->private;
231 put_all_bios(conf
, r10_bio
);
232 mempool_free(r10_bio
, conf
->r10bio_pool
);
235 static void put_buf(struct r10bio
*r10_bio
)
237 struct r10conf
*conf
= r10_bio
->mddev
->private;
239 mempool_free(r10_bio
, conf
->r10buf_pool
);
244 static void reschedule_retry(struct r10bio
*r10_bio
)
247 struct mddev
*mddev
= r10_bio
->mddev
;
248 struct r10conf
*conf
= mddev
->private;
250 spin_lock_irqsave(&conf
->device_lock
, flags
);
251 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
253 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
255 /* wake up frozen array... */
256 wake_up(&conf
->wait_barrier
);
258 md_wakeup_thread(mddev
->thread
);
262 * raid_end_bio_io() is called when we have finished servicing a mirrored
263 * operation and are ready to return a success/failure code to the buffer
266 static void raid_end_bio_io(struct r10bio
*r10_bio
)
268 struct bio
*bio
= r10_bio
->master_bio
;
270 struct r10conf
*conf
= r10_bio
->mddev
->private;
272 if (bio
->bi_phys_segments
) {
274 spin_lock_irqsave(&conf
->device_lock
, flags
);
275 bio
->bi_phys_segments
--;
276 done
= (bio
->bi_phys_segments
== 0);
277 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
280 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
281 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
285 * Wake up any possible resync thread that waits for the device
290 free_r10bio(r10_bio
);
294 * Update disk head position estimator based on IRQ completion info.
296 static inline void update_head_pos(int slot
, struct r10bio
*r10_bio
)
298 struct r10conf
*conf
= r10_bio
->mddev
->private;
300 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
301 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
305 * Find the disk number which triggered given bio
307 static int find_bio_disk(struct r10conf
*conf
, struct r10bio
*r10_bio
,
308 struct bio
*bio
, int *slotp
, int *replp
)
313 for (slot
= 0; slot
< conf
->copies
; slot
++) {
314 if (r10_bio
->devs
[slot
].bio
== bio
)
316 if (r10_bio
->devs
[slot
].repl_bio
== bio
) {
322 BUG_ON(slot
== conf
->copies
);
323 update_head_pos(slot
, r10_bio
);
329 return r10_bio
->devs
[slot
].devnum
;
332 static void raid10_end_read_request(struct bio
*bio
, int error
)
334 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
335 struct r10bio
*r10_bio
= bio
->bi_private
;
337 struct md_rdev
*rdev
;
338 struct r10conf
*conf
= r10_bio
->mddev
->private;
341 slot
= r10_bio
->read_slot
;
342 dev
= r10_bio
->devs
[slot
].devnum
;
343 rdev
= r10_bio
->devs
[slot
].rdev
;
345 * this branch is our 'one mirror IO has finished' event handler:
347 update_head_pos(slot
, r10_bio
);
351 * Set R10BIO_Uptodate in our master bio, so that
352 * we will return a good error code to the higher
353 * levels even if IO on some other mirrored buffer fails.
355 * The 'master' represents the composite IO operation to
356 * user-side. So if something waits for IO, then it will
357 * wait for the 'master' bio.
359 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
361 /* If all other devices that store this block have
362 * failed, we want to return the error upwards rather
363 * than fail the last device. Here we redefine
364 * "uptodate" to mean "Don't want to retry"
367 spin_lock_irqsave(&conf
->device_lock
, flags
);
368 if (!enough(conf
, rdev
->raid_disk
))
370 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
373 raid_end_bio_io(r10_bio
);
374 rdev_dec_pending(rdev
, conf
->mddev
);
377 * oops, read error - keep the refcount on the rdev
379 char b
[BDEVNAME_SIZE
];
380 printk_ratelimited(KERN_ERR
381 "md/raid10:%s: %s: rescheduling sector %llu\n",
383 bdevname(rdev
->bdev
, b
),
384 (unsigned long long)r10_bio
->sector
);
385 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
386 reschedule_retry(r10_bio
);
390 static void close_write(struct r10bio
*r10_bio
)
392 /* clear the bitmap if all writes complete successfully */
393 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
395 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
397 md_write_end(r10_bio
->mddev
);
400 static void one_write_done(struct r10bio
*r10_bio
)
402 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
403 if (test_bit(R10BIO_WriteError
, &r10_bio
->state
))
404 reschedule_retry(r10_bio
);
406 close_write(r10_bio
);
407 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
408 reschedule_retry(r10_bio
);
410 raid_end_bio_io(r10_bio
);
415 static void raid10_end_write_request(struct bio
*bio
, int error
)
417 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
418 struct r10bio
*r10_bio
= bio
->bi_private
;
421 struct r10conf
*conf
= r10_bio
->mddev
->private;
423 struct md_rdev
*rdev
= NULL
;
425 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
428 rdev
= conf
->mirrors
[dev
].replacement
;
432 rdev
= conf
->mirrors
[dev
].rdev
;
435 * this branch is our 'one mirror IO has finished' event handler:
439 /* Never record new bad blocks to replacement,
442 md_error(rdev
->mddev
, rdev
);
444 set_bit(WriteErrorSeen
, &rdev
->flags
);
445 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
446 set_bit(MD_RECOVERY_NEEDED
,
447 &rdev
->mddev
->recovery
);
448 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
453 * Set R10BIO_Uptodate in our master bio, so that
454 * we will return a good error code for to the higher
455 * levels even if IO on some other mirrored buffer fails.
457 * The 'master' represents the composite IO operation to
458 * user-side. So if something waits for IO, then it will
459 * wait for the 'master' bio.
464 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
466 /* Maybe we can clear some bad blocks. */
467 if (is_badblock(rdev
,
468 r10_bio
->devs
[slot
].addr
,
470 &first_bad
, &bad_sectors
)) {
473 r10_bio
->devs
[slot
].repl_bio
= IO_MADE_GOOD
;
475 r10_bio
->devs
[slot
].bio
= IO_MADE_GOOD
;
477 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
483 * Let's see if all mirrored write operations have finished
486 one_write_done(r10_bio
);
488 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
492 * RAID10 layout manager
493 * As well as the chunksize and raid_disks count, there are two
494 * parameters: near_copies and far_copies.
495 * near_copies * far_copies must be <= raid_disks.
496 * Normally one of these will be 1.
497 * If both are 1, we get raid0.
498 * If near_copies == raid_disks, we get raid1.
500 * Chunks are laid out in raid0 style with near_copies copies of the
501 * first chunk, followed by near_copies copies of the next chunk and
503 * If far_copies > 1, then after 1/far_copies of the array has been assigned
504 * as described above, we start again with a device offset of near_copies.
505 * So we effectively have another copy of the whole array further down all
506 * the drives, but with blocks on different drives.
507 * With this layout, and block is never stored twice on the one device.
509 * raid10_find_phys finds the sector offset of a given virtual sector
510 * on each device that it is on.
512 * raid10_find_virt does the reverse mapping, from a device and a
513 * sector offset to a virtual address
516 static void __raid10_find_phys(struct geom
*geo
, struct r10bio
*r10bio
)
525 /* now calculate first sector/dev */
526 chunk
= r10bio
->sector
>> geo
->chunk_shift
;
527 sector
= r10bio
->sector
& geo
->chunk_mask
;
529 chunk
*= geo
->near_copies
;
531 dev
= sector_div(stripe
, geo
->raid_disks
);
533 stripe
*= geo
->far_copies
;
535 sector
+= stripe
<< geo
->chunk_shift
;
537 /* and calculate all the others */
538 for (n
= 0; n
< geo
->near_copies
; n
++) {
541 r10bio
->devs
[slot
].addr
= sector
;
542 r10bio
->devs
[slot
].devnum
= d
;
545 for (f
= 1; f
< geo
->far_copies
; f
++) {
546 d
+= geo
->near_copies
;
547 if (d
>= geo
->raid_disks
)
548 d
-= geo
->raid_disks
;
550 r10bio
->devs
[slot
].devnum
= d
;
551 r10bio
->devs
[slot
].addr
= s
;
555 if (dev
>= geo
->raid_disks
) {
557 sector
+= (geo
->chunk_mask
+ 1);
562 static void raid10_find_phys(struct r10conf
*conf
, struct r10bio
*r10bio
)
564 struct geom
*geo
= &conf
->geo
;
566 if (conf
->reshape_progress
!= MaxSector
&&
567 ((r10bio
->sector
>= conf
->reshape_progress
) !=
568 conf
->mddev
->reshape_backwards
)) {
569 set_bit(R10BIO_Previous
, &r10bio
->state
);
572 clear_bit(R10BIO_Previous
, &r10bio
->state
);
574 __raid10_find_phys(geo
, r10bio
);
577 static sector_t
raid10_find_virt(struct r10conf
*conf
, sector_t sector
, int dev
)
579 sector_t offset
, chunk
, vchunk
;
580 /* Never use conf->prev as this is only called during resync
581 * or recovery, so reshape isn't happening
583 struct geom
*geo
= &conf
->geo
;
585 offset
= sector
& geo
->chunk_mask
;
586 if (geo
->far_offset
) {
588 chunk
= sector
>> geo
->chunk_shift
;
589 fc
= sector_div(chunk
, geo
->far_copies
);
590 dev
-= fc
* geo
->near_copies
;
592 dev
+= geo
->raid_disks
;
594 while (sector
>= geo
->stride
) {
595 sector
-= geo
->stride
;
596 if (dev
< geo
->near_copies
)
597 dev
+= geo
->raid_disks
- geo
->near_copies
;
599 dev
-= geo
->near_copies
;
601 chunk
= sector
>> geo
->chunk_shift
;
603 vchunk
= chunk
* geo
->raid_disks
+ dev
;
604 sector_div(vchunk
, geo
->near_copies
);
605 return (vchunk
<< geo
->chunk_shift
) + offset
;
609 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
611 * @bvm: properties of new bio
612 * @biovec: the request that could be merged to it.
614 * Return amount of bytes we can accept at this offset
615 * This requires checking for end-of-chunk if near_copies != raid_disks,
616 * and for subordinate merge_bvec_fns if merge_check_needed.
618 static int raid10_mergeable_bvec(struct request_queue
*q
,
619 struct bvec_merge_data
*bvm
,
620 struct bio_vec
*biovec
)
622 struct mddev
*mddev
= q
->queuedata
;
623 struct r10conf
*conf
= mddev
->private;
624 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
626 unsigned int chunk_sectors
;
627 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
628 struct geom
*geo
= &conf
->geo
;
630 chunk_sectors
= (conf
->geo
.chunk_mask
& conf
->prev
.chunk_mask
) + 1;
631 if (conf
->reshape_progress
!= MaxSector
&&
632 ((sector
>= conf
->reshape_progress
) !=
633 conf
->mddev
->reshape_backwards
))
636 if (geo
->near_copies
< geo
->raid_disks
) {
637 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1))
638 + bio_sectors
)) << 9;
640 /* bio_add cannot handle a negative return */
642 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
643 return biovec
->bv_len
;
645 max
= biovec
->bv_len
;
647 if (mddev
->merge_check_needed
) {
648 struct r10bio r10_bio
;
650 if (conf
->reshape_progress
!= MaxSector
) {
651 /* Cannot give any guidance during reshape */
652 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
653 return biovec
->bv_len
;
656 r10_bio
.sector
= sector
;
657 raid10_find_phys(conf
, &r10_bio
);
659 for (s
= 0; s
< conf
->copies
; s
++) {
660 int disk
= r10_bio
.devs
[s
].devnum
;
661 struct md_rdev
*rdev
= rcu_dereference(
662 conf
->mirrors
[disk
].rdev
);
663 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
664 struct request_queue
*q
=
665 bdev_get_queue(rdev
->bdev
);
666 if (q
->merge_bvec_fn
) {
667 bvm
->bi_sector
= r10_bio
.devs
[s
].addr
669 bvm
->bi_bdev
= rdev
->bdev
;
670 max
= min(max
, q
->merge_bvec_fn(
674 rdev
= rcu_dereference(conf
->mirrors
[disk
].replacement
);
675 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
676 struct request_queue
*q
=
677 bdev_get_queue(rdev
->bdev
);
678 if (q
->merge_bvec_fn
) {
679 bvm
->bi_sector
= r10_bio
.devs
[s
].addr
681 bvm
->bi_bdev
= rdev
->bdev
;
682 max
= min(max
, q
->merge_bvec_fn(
693 * This routine returns the disk from which the requested read should
694 * be done. There is a per-array 'next expected sequential IO' sector
695 * number - if this matches on the next IO then we use the last disk.
696 * There is also a per-disk 'last know head position' sector that is
697 * maintained from IRQ contexts, both the normal and the resync IO
698 * completion handlers update this position correctly. If there is no
699 * perfect sequential match then we pick the disk whose head is closest.
701 * If there are 2 mirrors in the same 2 devices, performance degrades
702 * because position is mirror, not device based.
704 * The rdev for the device selected will have nr_pending incremented.
708 * FIXME: possibly should rethink readbalancing and do it differently
709 * depending on near_copies / far_copies geometry.
711 static struct md_rdev
*read_balance(struct r10conf
*conf
,
712 struct r10bio
*r10_bio
,
715 const sector_t this_sector
= r10_bio
->sector
;
717 int sectors
= r10_bio
->sectors
;
718 int best_good_sectors
;
719 sector_t new_distance
, best_dist
;
720 struct md_rdev
*rdev
, *best_rdev
;
723 struct geom
*geo
= &conf
->geo
;
725 raid10_find_phys(conf
, r10_bio
);
728 sectors
= r10_bio
->sectors
;
731 best_dist
= MaxSector
;
732 best_good_sectors
= 0;
735 * Check if we can balance. We can balance on the whole
736 * device if no resync is going on (recovery is ok), or below
737 * the resync window. We take the first readable disk when
738 * above the resync window.
740 if (conf
->mddev
->recovery_cp
< MaxSector
741 && (this_sector
+ sectors
>= conf
->next_resync
))
744 for (slot
= 0; slot
< conf
->copies
; slot
++) {
749 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
751 disk
= r10_bio
->devs
[slot
].devnum
;
752 rdev
= rcu_dereference(conf
->mirrors
[disk
].replacement
);
753 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
) ||
754 test_bit(Unmerged
, &rdev
->flags
) ||
755 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
756 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
758 test_bit(Faulty
, &rdev
->flags
) ||
759 test_bit(Unmerged
, &rdev
->flags
))
761 if (!test_bit(In_sync
, &rdev
->flags
) &&
762 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
765 dev_sector
= r10_bio
->devs
[slot
].addr
;
766 if (is_badblock(rdev
, dev_sector
, sectors
,
767 &first_bad
, &bad_sectors
)) {
768 if (best_dist
< MaxSector
)
769 /* Already have a better slot */
771 if (first_bad
<= dev_sector
) {
772 /* Cannot read here. If this is the
773 * 'primary' device, then we must not read
774 * beyond 'bad_sectors' from another device.
776 bad_sectors
-= (dev_sector
- first_bad
);
777 if (!do_balance
&& sectors
> bad_sectors
)
778 sectors
= bad_sectors
;
779 if (best_good_sectors
> sectors
)
780 best_good_sectors
= sectors
;
782 sector_t good_sectors
=
783 first_bad
- dev_sector
;
784 if (good_sectors
> best_good_sectors
) {
785 best_good_sectors
= good_sectors
;
790 /* Must read from here */
795 best_good_sectors
= sectors
;
800 /* This optimisation is debatable, and completely destroys
801 * sequential read speed for 'far copies' arrays. So only
802 * keep it for 'near' arrays, and review those later.
804 if (geo
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
807 /* for far > 1 always use the lowest address */
808 if (geo
->far_copies
> 1)
809 new_distance
= r10_bio
->devs
[slot
].addr
;
811 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
812 conf
->mirrors
[disk
].head_position
);
813 if (new_distance
< best_dist
) {
814 best_dist
= new_distance
;
819 if (slot
>= conf
->copies
) {
825 atomic_inc(&rdev
->nr_pending
);
826 if (test_bit(Faulty
, &rdev
->flags
)) {
827 /* Cannot risk returning a device that failed
828 * before we inc'ed nr_pending
830 rdev_dec_pending(rdev
, conf
->mddev
);
833 r10_bio
->read_slot
= slot
;
837 *max_sectors
= best_good_sectors
;
842 static int raid10_congested(void *data
, int bits
)
844 struct mddev
*mddev
= data
;
845 struct r10conf
*conf
= mddev
->private;
848 if ((bits
& (1 << BDI_async_congested
)) &&
849 conf
->pending_count
>= max_queued_requests
)
852 if (mddev_congested(mddev
, bits
))
856 (i
< conf
->geo
.raid_disks
|| i
< conf
->prev
.raid_disks
)
859 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
860 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
861 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
863 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
870 static void flush_pending_writes(struct r10conf
*conf
)
872 /* Any writes that have been queued but are awaiting
873 * bitmap updates get flushed here.
875 spin_lock_irq(&conf
->device_lock
);
877 if (conf
->pending_bio_list
.head
) {
879 bio
= bio_list_get(&conf
->pending_bio_list
);
880 conf
->pending_count
= 0;
881 spin_unlock_irq(&conf
->device_lock
);
882 /* flush any pending bitmap writes to disk
883 * before proceeding w/ I/O */
884 bitmap_unplug(conf
->mddev
->bitmap
);
885 wake_up(&conf
->wait_barrier
);
887 while (bio
) { /* submit pending writes */
888 struct bio
*next
= bio
->bi_next
;
890 generic_make_request(bio
);
894 spin_unlock_irq(&conf
->device_lock
);
898 * Sometimes we need to suspend IO while we do something else,
899 * either some resync/recovery, or reconfigure the array.
900 * To do this we raise a 'barrier'.
901 * The 'barrier' is a counter that can be raised multiple times
902 * to count how many activities are happening which preclude
904 * We can only raise the barrier if there is no pending IO.
905 * i.e. if nr_pending == 0.
906 * We choose only to raise the barrier if no-one is waiting for the
907 * barrier to go down. This means that as soon as an IO request
908 * is ready, no other operations which require a barrier will start
909 * until the IO request has had a chance.
911 * So: regular IO calls 'wait_barrier'. When that returns there
912 * is no backgroup IO happening, It must arrange to call
913 * allow_barrier when it has finished its IO.
914 * backgroup IO calls must call raise_barrier. Once that returns
915 * there is no normal IO happeing. It must arrange to call
916 * lower_barrier when the particular background IO completes.
919 static void raise_barrier(struct r10conf
*conf
, int force
)
921 BUG_ON(force
&& !conf
->barrier
);
922 spin_lock_irq(&conf
->resync_lock
);
924 /* Wait until no block IO is waiting (unless 'force') */
925 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
926 conf
->resync_lock
, );
928 /* block any new IO from starting */
931 /* Now wait for all pending IO to complete */
932 wait_event_lock_irq(conf
->wait_barrier
,
933 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
934 conf
->resync_lock
, );
936 spin_unlock_irq(&conf
->resync_lock
);
939 static void lower_barrier(struct r10conf
*conf
)
942 spin_lock_irqsave(&conf
->resync_lock
, flags
);
944 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
945 wake_up(&conf
->wait_barrier
);
948 static void wait_barrier(struct r10conf
*conf
)
950 spin_lock_irq(&conf
->resync_lock
);
953 /* Wait for the barrier to drop.
954 * However if there are already pending
955 * requests (preventing the barrier from
956 * rising completely), and the
957 * pre-process bio queue isn't empty,
958 * then don't wait, as we need to empty
959 * that queue to get the nr_pending
962 wait_event_lock_irq(conf
->wait_barrier
,
966 !bio_list_empty(current
->bio_list
)),
972 spin_unlock_irq(&conf
->resync_lock
);
975 static void allow_barrier(struct r10conf
*conf
)
978 spin_lock_irqsave(&conf
->resync_lock
, flags
);
980 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
981 wake_up(&conf
->wait_barrier
);
984 static void freeze_array(struct r10conf
*conf
)
986 /* stop syncio and normal IO and wait for everything to
988 * We increment barrier and nr_waiting, and then
989 * wait until nr_pending match nr_queued+1
990 * This is called in the context of one normal IO request
991 * that has failed. Thus any sync request that might be pending
992 * will be blocked by nr_pending, and we need to wait for
993 * pending IO requests to complete or be queued for re-try.
994 * Thus the number queued (nr_queued) plus this request (1)
995 * must match the number of pending IOs (nr_pending) before
998 spin_lock_irq(&conf
->resync_lock
);
1001 wait_event_lock_irq(conf
->wait_barrier
,
1002 conf
->nr_pending
== conf
->nr_queued
+1,
1004 flush_pending_writes(conf
));
1006 spin_unlock_irq(&conf
->resync_lock
);
1009 static void unfreeze_array(struct r10conf
*conf
)
1011 /* reverse the effect of the freeze */
1012 spin_lock_irq(&conf
->resync_lock
);
1015 wake_up(&conf
->wait_barrier
);
1016 spin_unlock_irq(&conf
->resync_lock
);
1019 static sector_t
choose_data_offset(struct r10bio
*r10_bio
,
1020 struct md_rdev
*rdev
)
1022 if (!test_bit(MD_RECOVERY_RESHAPE
, &rdev
->mddev
->recovery
) ||
1023 test_bit(R10BIO_Previous
, &r10_bio
->state
))
1024 return rdev
->data_offset
;
1026 return rdev
->new_data_offset
;
1029 static void make_request(struct mddev
*mddev
, struct bio
* bio
)
1031 struct r10conf
*conf
= mddev
->private;
1032 struct r10bio
*r10_bio
;
1033 struct bio
*read_bio
;
1035 sector_t chunk_mask
= (conf
->geo
.chunk_mask
& conf
->prev
.chunk_mask
);
1036 int chunk_sects
= chunk_mask
+ 1;
1037 const int rw
= bio_data_dir(bio
);
1038 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
1039 const unsigned long do_fua
= (bio
->bi_rw
& REQ_FUA
);
1040 unsigned long flags
;
1041 struct md_rdev
*blocked_rdev
;
1042 int sectors_handled
;
1046 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
1047 md_flush_request(mddev
, bio
);
1051 /* If this request crosses a chunk boundary, we need to
1052 * split it. This will only happen for 1 PAGE (or less) requests.
1054 if (unlikely((bio
->bi_sector
& chunk_mask
) + (bio
->bi_size
>> 9)
1056 && (conf
->geo
.near_copies
< conf
->geo
.raid_disks
1057 || conf
->prev
.near_copies
< conf
->prev
.raid_disks
))) {
1058 struct bio_pair
*bp
;
1059 /* Sanity check -- queue functions should prevent this happening */
1060 if (bio
->bi_vcnt
!= 1 ||
1063 /* This is a one page bio that upper layers
1064 * refuse to split for us, so we need to split it.
1067 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
1069 /* Each of these 'make_request' calls will call 'wait_barrier'.
1070 * If the first succeeds but the second blocks due to the resync
1071 * thread raising the barrier, we will deadlock because the
1072 * IO to the underlying device will be queued in generic_make_request
1073 * and will never complete, so will never reduce nr_pending.
1074 * So increment nr_waiting here so no new raise_barriers will
1075 * succeed, and so the second wait_barrier cannot block.
1077 spin_lock_irq(&conf
->resync_lock
);
1079 spin_unlock_irq(&conf
->resync_lock
);
1081 make_request(mddev
, &bp
->bio1
);
1082 make_request(mddev
, &bp
->bio2
);
1084 spin_lock_irq(&conf
->resync_lock
);
1086 wake_up(&conf
->wait_barrier
);
1087 spin_unlock_irq(&conf
->resync_lock
);
1089 bio_pair_release(bp
);
1092 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1093 " or bigger than %dk %llu %d\n", mdname(mddev
), chunk_sects
/2,
1094 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
1100 md_write_start(mddev
, bio
);
1103 * Register the new request and wait if the reconstruction
1104 * thread has put up a bar for new requests.
1105 * Continue immediately if no resync is active currently.
1109 sectors
= bio
->bi_size
>> 9;
1110 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1111 bio
->bi_sector
< conf
->reshape_progress
&&
1112 bio
->bi_sector
+ sectors
> conf
->reshape_progress
) {
1113 /* IO spans the reshape position. Need to wait for
1116 allow_barrier(conf
);
1117 wait_event(conf
->wait_barrier
,
1118 conf
->reshape_progress
<= bio
->bi_sector
||
1119 conf
->reshape_progress
>= bio
->bi_sector
+ sectors
);
1122 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1123 bio_data_dir(bio
) == WRITE
&&
1124 (mddev
->reshape_backwards
1125 ? (bio
->bi_sector
< conf
->reshape_safe
&&
1126 bio
->bi_sector
+ sectors
> conf
->reshape_progress
)
1127 : (bio
->bi_sector
+ sectors
> conf
->reshape_safe
&&
1128 bio
->bi_sector
< conf
->reshape_progress
))) {
1129 /* Need to update reshape_position in metadata */
1130 mddev
->reshape_position
= conf
->reshape_progress
;
1131 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1132 set_bit(MD_CHANGE_PENDING
, &mddev
->flags
);
1133 md_wakeup_thread(mddev
->thread
);
1134 wait_event(mddev
->sb_wait
,
1135 !test_bit(MD_CHANGE_PENDING
, &mddev
->flags
));
1137 conf
->reshape_safe
= mddev
->reshape_position
;
1140 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1142 r10_bio
->master_bio
= bio
;
1143 r10_bio
->sectors
= sectors
;
1145 r10_bio
->mddev
= mddev
;
1146 r10_bio
->sector
= bio
->bi_sector
;
1149 /* We might need to issue multiple reads to different
1150 * devices if there are bad blocks around, so we keep
1151 * track of the number of reads in bio->bi_phys_segments.
1152 * If this is 0, there is only one r10_bio and no locking
1153 * will be needed when the request completes. If it is
1154 * non-zero, then it is the number of not-completed requests.
1156 bio
->bi_phys_segments
= 0;
1157 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
1161 * read balancing logic:
1163 struct md_rdev
*rdev
;
1167 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
1169 raid_end_bio_io(r10_bio
);
1172 slot
= r10_bio
->read_slot
;
1174 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1175 md_trim_bio(read_bio
, r10_bio
->sector
- bio
->bi_sector
,
1178 r10_bio
->devs
[slot
].bio
= read_bio
;
1179 r10_bio
->devs
[slot
].rdev
= rdev
;
1181 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
1182 choose_data_offset(r10_bio
, rdev
);
1183 read_bio
->bi_bdev
= rdev
->bdev
;
1184 read_bio
->bi_end_io
= raid10_end_read_request
;
1185 read_bio
->bi_rw
= READ
| do_sync
;
1186 read_bio
->bi_private
= r10_bio
;
1188 if (max_sectors
< r10_bio
->sectors
) {
1189 /* Could not read all from this device, so we will
1190 * need another r10_bio.
1192 sectors_handled
= (r10_bio
->sectors
+ max_sectors
1194 r10_bio
->sectors
= max_sectors
;
1195 spin_lock_irq(&conf
->device_lock
);
1196 if (bio
->bi_phys_segments
== 0)
1197 bio
->bi_phys_segments
= 2;
1199 bio
->bi_phys_segments
++;
1200 spin_unlock(&conf
->device_lock
);
1201 /* Cannot call generic_make_request directly
1202 * as that will be queued in __generic_make_request
1203 * and subsequent mempool_alloc might block
1204 * waiting for it. so hand bio over to raid10d.
1206 reschedule_retry(r10_bio
);
1208 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1210 r10_bio
->master_bio
= bio
;
1211 r10_bio
->sectors
= ((bio
->bi_size
>> 9)
1214 r10_bio
->mddev
= mddev
;
1215 r10_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
1218 generic_make_request(read_bio
);
1225 if (conf
->pending_count
>= max_queued_requests
) {
1226 md_wakeup_thread(mddev
->thread
);
1227 wait_event(conf
->wait_barrier
,
1228 conf
->pending_count
< max_queued_requests
);
1230 /* first select target devices under rcu_lock and
1231 * inc refcount on their rdev. Record them by setting
1233 * If there are known/acknowledged bad blocks on any device
1234 * on which we have seen a write error, we want to avoid
1235 * writing to those blocks. This potentially requires several
1236 * writes to write around the bad blocks. Each set of writes
1237 * gets its own r10_bio with a set of bios attached. The number
1238 * of r10_bios is recored in bio->bi_phys_segments just as with
1242 r10_bio
->read_slot
= -1; /* make sure repl_bio gets freed */
1243 raid10_find_phys(conf
, r10_bio
);
1245 blocked_rdev
= NULL
;
1247 max_sectors
= r10_bio
->sectors
;
1249 for (i
= 0; i
< conf
->copies
; i
++) {
1250 int d
= r10_bio
->devs
[i
].devnum
;
1251 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1252 struct md_rdev
*rrdev
= rcu_dereference(
1253 conf
->mirrors
[d
].replacement
);
1256 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1257 atomic_inc(&rdev
->nr_pending
);
1258 blocked_rdev
= rdev
;
1261 if (rrdev
&& unlikely(test_bit(Blocked
, &rrdev
->flags
))) {
1262 atomic_inc(&rrdev
->nr_pending
);
1263 blocked_rdev
= rrdev
;
1266 if (rrdev
&& (test_bit(Faulty
, &rrdev
->flags
)
1267 || test_bit(Unmerged
, &rrdev
->flags
)))
1270 r10_bio
->devs
[i
].bio
= NULL
;
1271 r10_bio
->devs
[i
].repl_bio
= NULL
;
1272 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
) ||
1273 test_bit(Unmerged
, &rdev
->flags
)) {
1274 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
1277 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1279 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1283 is_bad
= is_badblock(rdev
, dev_sector
,
1285 &first_bad
, &bad_sectors
);
1287 /* Mustn't write here until the bad block
1290 atomic_inc(&rdev
->nr_pending
);
1291 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1292 blocked_rdev
= rdev
;
1295 if (is_bad
&& first_bad
<= dev_sector
) {
1296 /* Cannot write here at all */
1297 bad_sectors
-= (dev_sector
- first_bad
);
1298 if (bad_sectors
< max_sectors
)
1299 /* Mustn't write more than bad_sectors
1300 * to other devices yet
1302 max_sectors
= bad_sectors
;
1303 /* We don't set R10BIO_Degraded as that
1304 * only applies if the disk is missing,
1305 * so it might be re-added, and we want to
1306 * know to recover this chunk.
1307 * In this case the device is here, and the
1308 * fact that this chunk is not in-sync is
1309 * recorded in the bad block log.
1314 int good_sectors
= first_bad
- dev_sector
;
1315 if (good_sectors
< max_sectors
)
1316 max_sectors
= good_sectors
;
1319 r10_bio
->devs
[i
].bio
= bio
;
1320 atomic_inc(&rdev
->nr_pending
);
1322 r10_bio
->devs
[i
].repl_bio
= bio
;
1323 atomic_inc(&rrdev
->nr_pending
);
1328 if (unlikely(blocked_rdev
)) {
1329 /* Have to wait for this device to get unblocked, then retry */
1333 for (j
= 0; j
< i
; j
++) {
1334 if (r10_bio
->devs
[j
].bio
) {
1335 d
= r10_bio
->devs
[j
].devnum
;
1336 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1338 if (r10_bio
->devs
[j
].repl_bio
) {
1339 struct md_rdev
*rdev
;
1340 d
= r10_bio
->devs
[j
].devnum
;
1341 rdev
= conf
->mirrors
[d
].replacement
;
1343 /* Race with remove_disk */
1345 rdev
= conf
->mirrors
[d
].rdev
;
1347 rdev_dec_pending(rdev
, mddev
);
1350 allow_barrier(conf
);
1351 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1356 if (max_sectors
< r10_bio
->sectors
) {
1357 /* We are splitting this into multiple parts, so
1358 * we need to prepare for allocating another r10_bio.
1360 r10_bio
->sectors
= max_sectors
;
1361 spin_lock_irq(&conf
->device_lock
);
1362 if (bio
->bi_phys_segments
== 0)
1363 bio
->bi_phys_segments
= 2;
1365 bio
->bi_phys_segments
++;
1366 spin_unlock_irq(&conf
->device_lock
);
1368 sectors_handled
= r10_bio
->sector
+ max_sectors
- bio
->bi_sector
;
1370 atomic_set(&r10_bio
->remaining
, 1);
1371 bitmap_startwrite(mddev
->bitmap
, r10_bio
->sector
, r10_bio
->sectors
, 0);
1373 for (i
= 0; i
< conf
->copies
; i
++) {
1375 int d
= r10_bio
->devs
[i
].devnum
;
1376 if (!r10_bio
->devs
[i
].bio
)
1379 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1380 md_trim_bio(mbio
, r10_bio
->sector
- bio
->bi_sector
,
1382 r10_bio
->devs
[i
].bio
= mbio
;
1384 mbio
->bi_sector
= (r10_bio
->devs
[i
].addr
+
1385 choose_data_offset(r10_bio
,
1386 conf
->mirrors
[d
].rdev
));
1387 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1388 mbio
->bi_end_io
= raid10_end_write_request
;
1389 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
1390 mbio
->bi_private
= r10_bio
;
1392 atomic_inc(&r10_bio
->remaining
);
1393 spin_lock_irqsave(&conf
->device_lock
, flags
);
1394 bio_list_add(&conf
->pending_bio_list
, mbio
);
1395 conf
->pending_count
++;
1396 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1397 if (!mddev_check_plugged(mddev
))
1398 md_wakeup_thread(mddev
->thread
);
1400 if (!r10_bio
->devs
[i
].repl_bio
)
1403 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1404 md_trim_bio(mbio
, r10_bio
->sector
- bio
->bi_sector
,
1406 r10_bio
->devs
[i
].repl_bio
= mbio
;
1408 /* We are actively writing to the original device
1409 * so it cannot disappear, so the replacement cannot
1412 mbio
->bi_sector
= (r10_bio
->devs
[i
].addr
+
1415 conf
->mirrors
[d
].replacement
));
1416 mbio
->bi_bdev
= conf
->mirrors
[d
].replacement
->bdev
;
1417 mbio
->bi_end_io
= raid10_end_write_request
;
1418 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
1419 mbio
->bi_private
= r10_bio
;
1421 atomic_inc(&r10_bio
->remaining
);
1422 spin_lock_irqsave(&conf
->device_lock
, flags
);
1423 bio_list_add(&conf
->pending_bio_list
, mbio
);
1424 conf
->pending_count
++;
1425 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1426 if (!mddev_check_plugged(mddev
))
1427 md_wakeup_thread(mddev
->thread
);
1430 /* Don't remove the bias on 'remaining' (one_write_done) until
1431 * after checking if we need to go around again.
1434 if (sectors_handled
< (bio
->bi_size
>> 9)) {
1435 one_write_done(r10_bio
);
1436 /* We need another r10_bio. It has already been counted
1437 * in bio->bi_phys_segments.
1439 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1441 r10_bio
->master_bio
= bio
;
1442 r10_bio
->sectors
= (bio
->bi_size
>> 9) - sectors_handled
;
1444 r10_bio
->mddev
= mddev
;
1445 r10_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
1449 one_write_done(r10_bio
);
1451 /* In case raid10d snuck in to freeze_array */
1452 wake_up(&conf
->wait_barrier
);
1455 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
1457 struct r10conf
*conf
= mddev
->private;
1460 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
)
1461 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1462 if (conf
->geo
.near_copies
> 1)
1463 seq_printf(seq
, " %d near-copies", conf
->geo
.near_copies
);
1464 if (conf
->geo
.far_copies
> 1) {
1465 if (conf
->geo
.far_offset
)
1466 seq_printf(seq
, " %d offset-copies", conf
->geo
.far_copies
);
1468 seq_printf(seq
, " %d far-copies", conf
->geo
.far_copies
);
1470 seq_printf(seq
, " [%d/%d] [", conf
->geo
.raid_disks
,
1471 conf
->geo
.raid_disks
- mddev
->degraded
);
1472 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
1473 seq_printf(seq
, "%s",
1474 conf
->mirrors
[i
].rdev
&&
1475 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
1476 seq_printf(seq
, "]");
1479 /* check if there are enough drives for
1480 * every block to appear on atleast one.
1481 * Don't consider the device numbered 'ignore'
1482 * as we might be about to remove it.
1484 static int _enough(struct r10conf
*conf
, struct geom
*geo
, int ignore
)
1489 int n
= conf
->copies
;
1492 if (conf
->mirrors
[first
].rdev
&&
1495 first
= (first
+1) % geo
->raid_disks
;
1499 } while (first
!= 0);
1503 static int enough(struct r10conf
*conf
, int ignore
)
1505 return _enough(conf
, &conf
->geo
, ignore
) &&
1506 _enough(conf
, &conf
->prev
, ignore
);
1509 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1511 char b
[BDEVNAME_SIZE
];
1512 struct r10conf
*conf
= mddev
->private;
1515 * If it is not operational, then we have already marked it as dead
1516 * else if it is the last working disks, ignore the error, let the
1517 * next level up know.
1518 * else mark the drive as failed
1520 if (test_bit(In_sync
, &rdev
->flags
)
1521 && !enough(conf
, rdev
->raid_disk
))
1523 * Don't fail the drive, just return an IO error.
1526 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1527 unsigned long flags
;
1528 spin_lock_irqsave(&conf
->device_lock
, flags
);
1530 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1532 * if recovery is running, make sure it aborts.
1534 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1536 set_bit(Blocked
, &rdev
->flags
);
1537 set_bit(Faulty
, &rdev
->flags
);
1538 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1540 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1541 "md/raid10:%s: Operation continuing on %d devices.\n",
1542 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1543 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
);
1546 static void print_conf(struct r10conf
*conf
)
1549 struct mirror_info
*tmp
;
1551 printk(KERN_DEBUG
"RAID10 conf printout:\n");
1553 printk(KERN_DEBUG
"(!conf)\n");
1556 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->geo
.raid_disks
- conf
->mddev
->degraded
,
1557 conf
->geo
.raid_disks
);
1559 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1560 char b
[BDEVNAME_SIZE
];
1561 tmp
= conf
->mirrors
+ i
;
1563 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1564 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1565 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1566 bdevname(tmp
->rdev
->bdev
,b
));
1570 static void close_sync(struct r10conf
*conf
)
1573 allow_barrier(conf
);
1575 mempool_destroy(conf
->r10buf_pool
);
1576 conf
->r10buf_pool
= NULL
;
1579 static int raid10_spare_active(struct mddev
*mddev
)
1582 struct r10conf
*conf
= mddev
->private;
1583 struct mirror_info
*tmp
;
1585 unsigned long flags
;
1588 * Find all non-in_sync disks within the RAID10 configuration
1589 * and mark them in_sync
1591 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1592 tmp
= conf
->mirrors
+ i
;
1593 if (tmp
->replacement
1594 && tmp
->replacement
->recovery_offset
== MaxSector
1595 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
1596 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
1597 /* Replacement has just become active */
1599 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
1602 /* Replaced device not technically faulty,
1603 * but we need to be sure it gets removed
1604 * and never re-added.
1606 set_bit(Faulty
, &tmp
->rdev
->flags
);
1607 sysfs_notify_dirent_safe(
1608 tmp
->rdev
->sysfs_state
);
1610 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
1611 } else if (tmp
->rdev
1612 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1613 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1615 sysfs_notify_dirent(tmp
->rdev
->sysfs_state
);
1618 spin_lock_irqsave(&conf
->device_lock
, flags
);
1619 mddev
->degraded
-= count
;
1620 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1627 static int raid10_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1629 struct r10conf
*conf
= mddev
->private;
1633 int last
= conf
->geo
.raid_disks
- 1;
1634 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
1636 if (mddev
->recovery_cp
< MaxSector
)
1637 /* only hot-add to in-sync arrays, as recovery is
1638 * very different from resync
1641 if (rdev
->saved_raid_disk
< 0 && !_enough(conf
, &conf
->prev
, -1))
1644 if (rdev
->raid_disk
>= 0)
1645 first
= last
= rdev
->raid_disk
;
1647 if (q
->merge_bvec_fn
) {
1648 set_bit(Unmerged
, &rdev
->flags
);
1649 mddev
->merge_check_needed
= 1;
1652 if (rdev
->saved_raid_disk
>= first
&&
1653 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1654 mirror
= rdev
->saved_raid_disk
;
1657 for ( ; mirror
<= last
; mirror
++) {
1658 struct mirror_info
*p
= &conf
->mirrors
[mirror
];
1659 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
1662 if (!test_bit(WantReplacement
, &p
->rdev
->flags
) ||
1663 p
->replacement
!= NULL
)
1665 clear_bit(In_sync
, &rdev
->flags
);
1666 set_bit(Replacement
, &rdev
->flags
);
1667 rdev
->raid_disk
= mirror
;
1669 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1670 rdev
->data_offset
<< 9);
1672 rcu_assign_pointer(p
->replacement
, rdev
);
1676 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1677 rdev
->data_offset
<< 9);
1679 p
->head_position
= 0;
1680 p
->recovery_disabled
= mddev
->recovery_disabled
- 1;
1681 rdev
->raid_disk
= mirror
;
1683 if (rdev
->saved_raid_disk
!= mirror
)
1685 rcu_assign_pointer(p
->rdev
, rdev
);
1688 if (err
== 0 && test_bit(Unmerged
, &rdev
->flags
)) {
1689 /* Some requests might not have seen this new
1690 * merge_bvec_fn. We must wait for them to complete
1691 * before merging the device fully.
1692 * First we make sure any code which has tested
1693 * our function has submitted the request, then
1694 * we wait for all outstanding requests to complete.
1696 synchronize_sched();
1697 raise_barrier(conf
, 0);
1698 lower_barrier(conf
);
1699 clear_bit(Unmerged
, &rdev
->flags
);
1701 md_integrity_add_rdev(rdev
, mddev
);
1706 static int raid10_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1708 struct r10conf
*conf
= mddev
->private;
1710 int number
= rdev
->raid_disk
;
1711 struct md_rdev
**rdevp
;
1712 struct mirror_info
*p
= conf
->mirrors
+ number
;
1715 if (rdev
== p
->rdev
)
1717 else if (rdev
== p
->replacement
)
1718 rdevp
= &p
->replacement
;
1722 if (test_bit(In_sync
, &rdev
->flags
) ||
1723 atomic_read(&rdev
->nr_pending
)) {
1727 /* Only remove faulty devices if recovery
1730 if (!test_bit(Faulty
, &rdev
->flags
) &&
1731 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
1732 (!p
->replacement
|| p
->replacement
== rdev
) &&
1733 number
< conf
->geo
.raid_disks
&&
1740 if (atomic_read(&rdev
->nr_pending
)) {
1741 /* lost the race, try later */
1745 } else if (p
->replacement
) {
1746 /* We must have just cleared 'rdev' */
1747 p
->rdev
= p
->replacement
;
1748 clear_bit(Replacement
, &p
->replacement
->flags
);
1749 smp_mb(); /* Make sure other CPUs may see both as identical
1750 * but will never see neither -- if they are careful.
1752 p
->replacement
= NULL
;
1753 clear_bit(WantReplacement
, &rdev
->flags
);
1755 /* We might have just remove the Replacement as faulty
1756 * Clear the flag just in case
1758 clear_bit(WantReplacement
, &rdev
->flags
);
1760 err
= md_integrity_register(mddev
);
1769 static void end_sync_read(struct bio
*bio
, int error
)
1771 struct r10bio
*r10_bio
= bio
->bi_private
;
1772 struct r10conf
*conf
= r10_bio
->mddev
->private;
1775 if (bio
== r10_bio
->master_bio
) {
1776 /* this is a reshape read */
1777 d
= r10_bio
->read_slot
; /* really the read dev */
1779 d
= find_bio_disk(conf
, r10_bio
, bio
, NULL
, NULL
);
1781 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1782 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1784 /* The write handler will notice the lack of
1785 * R10BIO_Uptodate and record any errors etc
1787 atomic_add(r10_bio
->sectors
,
1788 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1790 /* for reconstruct, we always reschedule after a read.
1791 * for resync, only after all reads
1793 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1794 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1795 atomic_dec_and_test(&r10_bio
->remaining
)) {
1796 /* we have read all the blocks,
1797 * do the comparison in process context in raid10d
1799 reschedule_retry(r10_bio
);
1803 static void end_sync_request(struct r10bio
*r10_bio
)
1805 struct mddev
*mddev
= r10_bio
->mddev
;
1807 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1808 if (r10_bio
->master_bio
== NULL
) {
1809 /* the primary of several recovery bios */
1810 sector_t s
= r10_bio
->sectors
;
1811 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1812 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1813 reschedule_retry(r10_bio
);
1816 md_done_sync(mddev
, s
, 1);
1819 struct r10bio
*r10_bio2
= (struct r10bio
*)r10_bio
->master_bio
;
1820 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1821 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1822 reschedule_retry(r10_bio
);
1830 static void end_sync_write(struct bio
*bio
, int error
)
1832 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1833 struct r10bio
*r10_bio
= bio
->bi_private
;
1834 struct mddev
*mddev
= r10_bio
->mddev
;
1835 struct r10conf
*conf
= mddev
->private;
1841 struct md_rdev
*rdev
= NULL
;
1843 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
1845 rdev
= conf
->mirrors
[d
].replacement
;
1847 rdev
= conf
->mirrors
[d
].rdev
;
1851 md_error(mddev
, rdev
);
1853 set_bit(WriteErrorSeen
, &rdev
->flags
);
1854 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1855 set_bit(MD_RECOVERY_NEEDED
,
1856 &rdev
->mddev
->recovery
);
1857 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
1859 } else if (is_badblock(rdev
,
1860 r10_bio
->devs
[slot
].addr
,
1862 &first_bad
, &bad_sectors
))
1863 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
1865 rdev_dec_pending(rdev
, mddev
);
1867 end_sync_request(r10_bio
);
1871 * Note: sync and recover and handled very differently for raid10
1872 * This code is for resync.
1873 * For resync, we read through virtual addresses and read all blocks.
1874 * If there is any error, we schedule a write. The lowest numbered
1875 * drive is authoritative.
1876 * However requests come for physical address, so we need to map.
1877 * For every physical address there are raid_disks/copies virtual addresses,
1878 * which is always are least one, but is not necessarly an integer.
1879 * This means that a physical address can span multiple chunks, so we may
1880 * have to submit multiple io requests for a single sync request.
1883 * We check if all blocks are in-sync and only write to blocks that
1886 static void sync_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
1888 struct r10conf
*conf
= mddev
->private;
1890 struct bio
*tbio
, *fbio
;
1893 atomic_set(&r10_bio
->remaining
, 1);
1895 /* find the first device with a block */
1896 for (i
=0; i
<conf
->copies
; i
++)
1897 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1900 if (i
== conf
->copies
)
1904 fbio
= r10_bio
->devs
[i
].bio
;
1906 vcnt
= (r10_bio
->sectors
+ (PAGE_SIZE
>> 9) - 1) >> (PAGE_SHIFT
- 9);
1907 /* now find blocks with errors */
1908 for (i
=0 ; i
< conf
->copies
; i
++) {
1911 tbio
= r10_bio
->devs
[i
].bio
;
1913 if (tbio
->bi_end_io
!= end_sync_read
)
1917 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1918 /* We know that the bi_io_vec layout is the same for
1919 * both 'first' and 'i', so we just compare them.
1920 * All vec entries are PAGE_SIZE;
1922 for (j
= 0; j
< vcnt
; j
++)
1923 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1924 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1925 fbio
->bi_io_vec
[j
].bv_len
))
1929 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1930 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1931 /* Don't fix anything. */
1934 /* Ok, we need to write this bio, either to correct an
1935 * inconsistency or to correct an unreadable block.
1936 * First we need to fixup bv_offset, bv_len and
1937 * bi_vecs, as the read request might have corrupted these
1939 tbio
->bi_vcnt
= vcnt
;
1940 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1942 tbio
->bi_phys_segments
= 0;
1943 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1944 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1945 tbio
->bi_next
= NULL
;
1946 tbio
->bi_rw
= WRITE
;
1947 tbio
->bi_private
= r10_bio
;
1948 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1950 for (j
=0; j
< vcnt
; j
++) {
1951 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1952 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1954 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1955 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1958 tbio
->bi_end_io
= end_sync_write
;
1960 d
= r10_bio
->devs
[i
].devnum
;
1961 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1962 atomic_inc(&r10_bio
->remaining
);
1963 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1965 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1966 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1967 generic_make_request(tbio
);
1970 /* Now write out to any replacement devices
1973 for (i
= 0; i
< conf
->copies
; i
++) {
1976 tbio
= r10_bio
->devs
[i
].repl_bio
;
1977 if (!tbio
|| !tbio
->bi_end_io
)
1979 if (r10_bio
->devs
[i
].bio
->bi_end_io
!= end_sync_write
1980 && r10_bio
->devs
[i
].bio
!= fbio
)
1981 for (j
= 0; j
< vcnt
; j
++)
1982 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1983 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1985 d
= r10_bio
->devs
[i
].devnum
;
1986 atomic_inc(&r10_bio
->remaining
);
1987 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
1988 tbio
->bi_size
>> 9);
1989 generic_make_request(tbio
);
1993 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1994 md_done_sync(mddev
, r10_bio
->sectors
, 1);
2000 * Now for the recovery code.
2001 * Recovery happens across physical sectors.
2002 * We recover all non-is_sync drives by finding the virtual address of
2003 * each, and then choose a working drive that also has that virt address.
2004 * There is a separate r10_bio for each non-in_sync drive.
2005 * Only the first two slots are in use. The first for reading,
2006 * The second for writing.
2009 static void fix_recovery_read_error(struct r10bio
*r10_bio
)
2011 /* We got a read error during recovery.
2012 * We repeat the read in smaller page-sized sections.
2013 * If a read succeeds, write it to the new device or record
2014 * a bad block if we cannot.
2015 * If a read fails, record a bad block on both old and
2018 struct mddev
*mddev
= r10_bio
->mddev
;
2019 struct r10conf
*conf
= mddev
->private;
2020 struct bio
*bio
= r10_bio
->devs
[0].bio
;
2022 int sectors
= r10_bio
->sectors
;
2024 int dr
= r10_bio
->devs
[0].devnum
;
2025 int dw
= r10_bio
->devs
[1].devnum
;
2029 struct md_rdev
*rdev
;
2033 if (s
> (PAGE_SIZE
>>9))
2036 rdev
= conf
->mirrors
[dr
].rdev
;
2037 addr
= r10_bio
->devs
[0].addr
+ sect
,
2038 ok
= sync_page_io(rdev
,
2041 bio
->bi_io_vec
[idx
].bv_page
,
2044 rdev
= conf
->mirrors
[dw
].rdev
;
2045 addr
= r10_bio
->devs
[1].addr
+ sect
;
2046 ok
= sync_page_io(rdev
,
2049 bio
->bi_io_vec
[idx
].bv_page
,
2052 set_bit(WriteErrorSeen
, &rdev
->flags
);
2053 if (!test_and_set_bit(WantReplacement
,
2055 set_bit(MD_RECOVERY_NEEDED
,
2056 &rdev
->mddev
->recovery
);
2060 /* We don't worry if we cannot set a bad block -
2061 * it really is bad so there is no loss in not
2064 rdev_set_badblocks(rdev
, addr
, s
, 0);
2066 if (rdev
!= conf
->mirrors
[dw
].rdev
) {
2067 /* need bad block on destination too */
2068 struct md_rdev
*rdev2
= conf
->mirrors
[dw
].rdev
;
2069 addr
= r10_bio
->devs
[1].addr
+ sect
;
2070 ok
= rdev_set_badblocks(rdev2
, addr
, s
, 0);
2072 /* just abort the recovery */
2074 "md/raid10:%s: recovery aborted"
2075 " due to read error\n",
2078 conf
->mirrors
[dw
].recovery_disabled
2079 = mddev
->recovery_disabled
;
2080 set_bit(MD_RECOVERY_INTR
,
2093 static void recovery_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2095 struct r10conf
*conf
= mddev
->private;
2097 struct bio
*wbio
, *wbio2
;
2099 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
)) {
2100 fix_recovery_read_error(r10_bio
);
2101 end_sync_request(r10_bio
);
2106 * share the pages with the first bio
2107 * and submit the write request
2109 d
= r10_bio
->devs
[1].devnum
;
2110 wbio
= r10_bio
->devs
[1].bio
;
2111 wbio2
= r10_bio
->devs
[1].repl_bio
;
2112 if (wbio
->bi_end_io
) {
2113 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2114 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
2115 generic_make_request(wbio
);
2117 if (wbio2
&& wbio2
->bi_end_io
) {
2118 atomic_inc(&conf
->mirrors
[d
].replacement
->nr_pending
);
2119 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2120 wbio2
->bi_size
>> 9);
2121 generic_make_request(wbio2
);
2127 * Used by fix_read_error() to decay the per rdev read_errors.
2128 * We halve the read error count for every hour that has elapsed
2129 * since the last recorded read error.
2132 static void check_decay_read_errors(struct mddev
*mddev
, struct md_rdev
*rdev
)
2134 struct timespec cur_time_mon
;
2135 unsigned long hours_since_last
;
2136 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
2138 ktime_get_ts(&cur_time_mon
);
2140 if (rdev
->last_read_error
.tv_sec
== 0 &&
2141 rdev
->last_read_error
.tv_nsec
== 0) {
2142 /* first time we've seen a read error */
2143 rdev
->last_read_error
= cur_time_mon
;
2147 hours_since_last
= (cur_time_mon
.tv_sec
-
2148 rdev
->last_read_error
.tv_sec
) / 3600;
2150 rdev
->last_read_error
= cur_time_mon
;
2153 * if hours_since_last is > the number of bits in read_errors
2154 * just set read errors to 0. We do this to avoid
2155 * overflowing the shift of read_errors by hours_since_last.
2157 if (hours_since_last
>= 8 * sizeof(read_errors
))
2158 atomic_set(&rdev
->read_errors
, 0);
2160 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
2163 static int r10_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
2164 int sectors
, struct page
*page
, int rw
)
2169 if (is_badblock(rdev
, sector
, sectors
, &first_bad
, &bad_sectors
)
2170 && (rw
== READ
|| test_bit(WriteErrorSeen
, &rdev
->flags
)))
2172 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, rw
, false))
2176 set_bit(WriteErrorSeen
, &rdev
->flags
);
2177 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2178 set_bit(MD_RECOVERY_NEEDED
,
2179 &rdev
->mddev
->recovery
);
2181 /* need to record an error - either for the block or the device */
2182 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
2183 md_error(rdev
->mddev
, rdev
);
2188 * This is a kernel thread which:
2190 * 1. Retries failed read operations on working mirrors.
2191 * 2. Updates the raid superblock when problems encounter.
2192 * 3. Performs writes following reads for array synchronising.
2195 static void fix_read_error(struct r10conf
*conf
, struct mddev
*mddev
, struct r10bio
*r10_bio
)
2197 int sect
= 0; /* Offset from r10_bio->sector */
2198 int sectors
= r10_bio
->sectors
;
2199 struct md_rdev
*rdev
;
2200 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
2201 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2203 /* still own a reference to this rdev, so it cannot
2204 * have been cleared recently.
2206 rdev
= conf
->mirrors
[d
].rdev
;
2208 if (test_bit(Faulty
, &rdev
->flags
))
2209 /* drive has already been failed, just ignore any
2210 more fix_read_error() attempts */
2213 check_decay_read_errors(mddev
, rdev
);
2214 atomic_inc(&rdev
->read_errors
);
2215 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
2216 char b
[BDEVNAME_SIZE
];
2217 bdevname(rdev
->bdev
, b
);
2220 "md/raid10:%s: %s: Raid device exceeded "
2221 "read_error threshold [cur %d:max %d]\n",
2223 atomic_read(&rdev
->read_errors
), max_read_errors
);
2225 "md/raid10:%s: %s: Failing raid device\n",
2227 md_error(mddev
, conf
->mirrors
[d
].rdev
);
2228 r10_bio
->devs
[r10_bio
->read_slot
].bio
= IO_BLOCKED
;
2234 int sl
= r10_bio
->read_slot
;
2238 if (s
> (PAGE_SIZE
>>9))
2246 d
= r10_bio
->devs
[sl
].devnum
;
2247 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2249 !test_bit(Unmerged
, &rdev
->flags
) &&
2250 test_bit(In_sync
, &rdev
->flags
) &&
2251 is_badblock(rdev
, r10_bio
->devs
[sl
].addr
+ sect
, s
,
2252 &first_bad
, &bad_sectors
) == 0) {
2253 atomic_inc(&rdev
->nr_pending
);
2255 success
= sync_page_io(rdev
,
2256 r10_bio
->devs
[sl
].addr
+
2259 conf
->tmppage
, READ
, false);
2260 rdev_dec_pending(rdev
, mddev
);
2266 if (sl
== conf
->copies
)
2268 } while (!success
&& sl
!= r10_bio
->read_slot
);
2272 /* Cannot read from anywhere, just mark the block
2273 * as bad on the first device to discourage future
2276 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2277 rdev
= conf
->mirrors
[dn
].rdev
;
2279 if (!rdev_set_badblocks(
2281 r10_bio
->devs
[r10_bio
->read_slot
].addr
2284 md_error(mddev
, rdev
);
2285 r10_bio
->devs
[r10_bio
->read_slot
].bio
2292 /* write it back and re-read */
2294 while (sl
!= r10_bio
->read_slot
) {
2295 char b
[BDEVNAME_SIZE
];
2300 d
= r10_bio
->devs
[sl
].devnum
;
2301 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2303 test_bit(Unmerged
, &rdev
->flags
) ||
2304 !test_bit(In_sync
, &rdev
->flags
))
2307 atomic_inc(&rdev
->nr_pending
);
2309 if (r10_sync_page_io(rdev
,
2310 r10_bio
->devs
[sl
].addr
+
2312 s
, conf
->tmppage
, WRITE
)
2314 /* Well, this device is dead */
2316 "md/raid10:%s: read correction "
2318 " (%d sectors at %llu on %s)\n",
2320 (unsigned long long)(
2322 choose_data_offset(r10_bio
,
2324 bdevname(rdev
->bdev
, b
));
2325 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
2328 bdevname(rdev
->bdev
, b
));
2330 rdev_dec_pending(rdev
, mddev
);
2334 while (sl
!= r10_bio
->read_slot
) {
2335 char b
[BDEVNAME_SIZE
];
2340 d
= r10_bio
->devs
[sl
].devnum
;
2341 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2343 !test_bit(In_sync
, &rdev
->flags
))
2346 atomic_inc(&rdev
->nr_pending
);
2348 switch (r10_sync_page_io(rdev
,
2349 r10_bio
->devs
[sl
].addr
+
2354 /* Well, this device is dead */
2356 "md/raid10:%s: unable to read back "
2358 " (%d sectors at %llu on %s)\n",
2360 (unsigned long long)(
2362 choose_data_offset(r10_bio
, rdev
)),
2363 bdevname(rdev
->bdev
, b
));
2364 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
2367 bdevname(rdev
->bdev
, b
));
2371 "md/raid10:%s: read error corrected"
2372 " (%d sectors at %llu on %s)\n",
2374 (unsigned long long)(
2376 choose_data_offset(r10_bio
, rdev
)),
2377 bdevname(rdev
->bdev
, b
));
2378 atomic_add(s
, &rdev
->corrected_errors
);
2381 rdev_dec_pending(rdev
, mddev
);
2391 static void bi_complete(struct bio
*bio
, int error
)
2393 complete((struct completion
*)bio
->bi_private
);
2396 static int submit_bio_wait(int rw
, struct bio
*bio
)
2398 struct completion event
;
2401 init_completion(&event
);
2402 bio
->bi_private
= &event
;
2403 bio
->bi_end_io
= bi_complete
;
2404 submit_bio(rw
, bio
);
2405 wait_for_completion(&event
);
2407 return test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2410 static int narrow_write_error(struct r10bio
*r10_bio
, int i
)
2412 struct bio
*bio
= r10_bio
->master_bio
;
2413 struct mddev
*mddev
= r10_bio
->mddev
;
2414 struct r10conf
*conf
= mddev
->private;
2415 struct md_rdev
*rdev
= conf
->mirrors
[r10_bio
->devs
[i
].devnum
].rdev
;
2416 /* bio has the data to be written to slot 'i' where
2417 * we just recently had a write error.
2418 * We repeatedly clone the bio and trim down to one block,
2419 * then try the write. Where the write fails we record
2421 * It is conceivable that the bio doesn't exactly align with
2422 * blocks. We must handle this.
2424 * We currently own a reference to the rdev.
2430 int sect_to_write
= r10_bio
->sectors
;
2433 if (rdev
->badblocks
.shift
< 0)
2436 block_sectors
= 1 << rdev
->badblocks
.shift
;
2437 sector
= r10_bio
->sector
;
2438 sectors
= ((r10_bio
->sector
+ block_sectors
)
2439 & ~(sector_t
)(block_sectors
- 1))
2442 while (sect_to_write
) {
2444 if (sectors
> sect_to_write
)
2445 sectors
= sect_to_write
;
2446 /* Write at 'sector' for 'sectors' */
2447 wbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
2448 md_trim_bio(wbio
, sector
- bio
->bi_sector
, sectors
);
2449 wbio
->bi_sector
= (r10_bio
->devs
[i
].addr
+
2450 choose_data_offset(r10_bio
, rdev
) +
2451 (sector
- r10_bio
->sector
));
2452 wbio
->bi_bdev
= rdev
->bdev
;
2453 if (submit_bio_wait(WRITE
, wbio
) == 0)
2455 ok
= rdev_set_badblocks(rdev
, sector
,
2460 sect_to_write
-= sectors
;
2462 sectors
= block_sectors
;
2467 static void handle_read_error(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2469 int slot
= r10_bio
->read_slot
;
2471 struct r10conf
*conf
= mddev
->private;
2472 struct md_rdev
*rdev
= r10_bio
->devs
[slot
].rdev
;
2473 char b
[BDEVNAME_SIZE
];
2474 unsigned long do_sync
;
2477 /* we got a read error. Maybe the drive is bad. Maybe just
2478 * the block and we can fix it.
2479 * We freeze all other IO, and try reading the block from
2480 * other devices. When we find one, we re-write
2481 * and check it that fixes the read error.
2482 * This is all done synchronously while the array is
2485 bio
= r10_bio
->devs
[slot
].bio
;
2486 bdevname(bio
->bi_bdev
, b
);
2488 r10_bio
->devs
[slot
].bio
= NULL
;
2490 if (mddev
->ro
== 0) {
2492 fix_read_error(conf
, mddev
, r10_bio
);
2493 unfreeze_array(conf
);
2495 r10_bio
->devs
[slot
].bio
= IO_BLOCKED
;
2497 rdev_dec_pending(rdev
, mddev
);
2500 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
2502 printk(KERN_ALERT
"md/raid10:%s: %s: unrecoverable I/O"
2503 " read error for block %llu\n",
2505 (unsigned long long)r10_bio
->sector
);
2506 raid_end_bio_io(r10_bio
);
2510 do_sync
= (r10_bio
->master_bio
->bi_rw
& REQ_SYNC
);
2511 slot
= r10_bio
->read_slot
;
2514 "md/raid10:%s: %s: redirecting "
2515 "sector %llu to another mirror\n",
2517 bdevname(rdev
->bdev
, b
),
2518 (unsigned long long)r10_bio
->sector
);
2519 bio
= bio_clone_mddev(r10_bio
->master_bio
,
2522 r10_bio
->sector
- bio
->bi_sector
,
2524 r10_bio
->devs
[slot
].bio
= bio
;
2525 r10_bio
->devs
[slot
].rdev
= rdev
;
2526 bio
->bi_sector
= r10_bio
->devs
[slot
].addr
2527 + choose_data_offset(r10_bio
, rdev
);
2528 bio
->bi_bdev
= rdev
->bdev
;
2529 bio
->bi_rw
= READ
| do_sync
;
2530 bio
->bi_private
= r10_bio
;
2531 bio
->bi_end_io
= raid10_end_read_request
;
2532 if (max_sectors
< r10_bio
->sectors
) {
2533 /* Drat - have to split this up more */
2534 struct bio
*mbio
= r10_bio
->master_bio
;
2535 int sectors_handled
=
2536 r10_bio
->sector
+ max_sectors
2538 r10_bio
->sectors
= max_sectors
;
2539 spin_lock_irq(&conf
->device_lock
);
2540 if (mbio
->bi_phys_segments
== 0)
2541 mbio
->bi_phys_segments
= 2;
2543 mbio
->bi_phys_segments
++;
2544 spin_unlock_irq(&conf
->device_lock
);
2545 generic_make_request(bio
);
2547 r10_bio
= mempool_alloc(conf
->r10bio_pool
,
2549 r10_bio
->master_bio
= mbio
;
2550 r10_bio
->sectors
= (mbio
->bi_size
>> 9)
2553 set_bit(R10BIO_ReadError
,
2555 r10_bio
->mddev
= mddev
;
2556 r10_bio
->sector
= mbio
->bi_sector
2561 generic_make_request(bio
);
2564 static void handle_write_completed(struct r10conf
*conf
, struct r10bio
*r10_bio
)
2566 /* Some sort of write request has finished and it
2567 * succeeded in writing where we thought there was a
2568 * bad block. So forget the bad block.
2569 * Or possibly if failed and we need to record
2573 struct md_rdev
*rdev
;
2575 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
) ||
2576 test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
2577 for (m
= 0; m
< conf
->copies
; m
++) {
2578 int dev
= r10_bio
->devs
[m
].devnum
;
2579 rdev
= conf
->mirrors
[dev
].rdev
;
2580 if (r10_bio
->devs
[m
].bio
== NULL
)
2582 if (test_bit(BIO_UPTODATE
,
2583 &r10_bio
->devs
[m
].bio
->bi_flags
)) {
2584 rdev_clear_badblocks(
2586 r10_bio
->devs
[m
].addr
,
2587 r10_bio
->sectors
, 0);
2589 if (!rdev_set_badblocks(
2591 r10_bio
->devs
[m
].addr
,
2592 r10_bio
->sectors
, 0))
2593 md_error(conf
->mddev
, rdev
);
2595 rdev
= conf
->mirrors
[dev
].replacement
;
2596 if (r10_bio
->devs
[m
].repl_bio
== NULL
)
2598 if (test_bit(BIO_UPTODATE
,
2599 &r10_bio
->devs
[m
].repl_bio
->bi_flags
)) {
2600 rdev_clear_badblocks(
2602 r10_bio
->devs
[m
].addr
,
2603 r10_bio
->sectors
, 0);
2605 if (!rdev_set_badblocks(
2607 r10_bio
->devs
[m
].addr
,
2608 r10_bio
->sectors
, 0))
2609 md_error(conf
->mddev
, rdev
);
2614 for (m
= 0; m
< conf
->copies
; m
++) {
2615 int dev
= r10_bio
->devs
[m
].devnum
;
2616 struct bio
*bio
= r10_bio
->devs
[m
].bio
;
2617 rdev
= conf
->mirrors
[dev
].rdev
;
2618 if (bio
== IO_MADE_GOOD
) {
2619 rdev_clear_badblocks(
2621 r10_bio
->devs
[m
].addr
,
2622 r10_bio
->sectors
, 0);
2623 rdev_dec_pending(rdev
, conf
->mddev
);
2624 } else if (bio
!= NULL
&&
2625 !test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
2626 if (!narrow_write_error(r10_bio
, m
)) {
2627 md_error(conf
->mddev
, rdev
);
2628 set_bit(R10BIO_Degraded
,
2631 rdev_dec_pending(rdev
, conf
->mddev
);
2633 bio
= r10_bio
->devs
[m
].repl_bio
;
2634 rdev
= conf
->mirrors
[dev
].replacement
;
2635 if (rdev
&& bio
== IO_MADE_GOOD
) {
2636 rdev_clear_badblocks(
2638 r10_bio
->devs
[m
].addr
,
2639 r10_bio
->sectors
, 0);
2640 rdev_dec_pending(rdev
, conf
->mddev
);
2643 if (test_bit(R10BIO_WriteError
,
2645 close_write(r10_bio
);
2646 raid_end_bio_io(r10_bio
);
2650 static void raid10d(struct mddev
*mddev
)
2652 struct r10bio
*r10_bio
;
2653 unsigned long flags
;
2654 struct r10conf
*conf
= mddev
->private;
2655 struct list_head
*head
= &conf
->retry_list
;
2656 struct blk_plug plug
;
2658 md_check_recovery(mddev
);
2660 blk_start_plug(&plug
);
2663 flush_pending_writes(conf
);
2665 spin_lock_irqsave(&conf
->device_lock
, flags
);
2666 if (list_empty(head
)) {
2667 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2670 r10_bio
= list_entry(head
->prev
, struct r10bio
, retry_list
);
2671 list_del(head
->prev
);
2673 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2675 mddev
= r10_bio
->mddev
;
2676 conf
= mddev
->private;
2677 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
2678 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
2679 handle_write_completed(conf
, r10_bio
);
2680 else if (test_bit(R10BIO_IsReshape
, &r10_bio
->state
))
2681 reshape_request_write(mddev
, r10_bio
);
2682 else if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
2683 sync_request_write(mddev
, r10_bio
);
2684 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
2685 recovery_request_write(mddev
, r10_bio
);
2686 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
2687 handle_read_error(mddev
, r10_bio
);
2689 /* just a partial read to be scheduled from a
2692 int slot
= r10_bio
->read_slot
;
2693 generic_make_request(r10_bio
->devs
[slot
].bio
);
2697 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
2698 md_check_recovery(mddev
);
2700 blk_finish_plug(&plug
);
2704 static int init_resync(struct r10conf
*conf
)
2709 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2710 BUG_ON(conf
->r10buf_pool
);
2711 conf
->have_replacement
= 0;
2712 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
2713 if (conf
->mirrors
[i
].replacement
)
2714 conf
->have_replacement
= 1;
2715 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
2716 if (!conf
->r10buf_pool
)
2718 conf
->next_resync
= 0;
2723 * perform a "sync" on one "block"
2725 * We need to make sure that no normal I/O request - particularly write
2726 * requests - conflict with active sync requests.
2728 * This is achieved by tracking pending requests and a 'barrier' concept
2729 * that can be installed to exclude normal IO requests.
2731 * Resync and recovery are handled very differently.
2732 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2734 * For resync, we iterate over virtual addresses, read all copies,
2735 * and update if there are differences. If only one copy is live,
2737 * For recovery, we iterate over physical addresses, read a good
2738 * value for each non-in_sync drive, and over-write.
2740 * So, for recovery we may have several outstanding complex requests for a
2741 * given address, one for each out-of-sync device. We model this by allocating
2742 * a number of r10_bio structures, one for each out-of-sync device.
2743 * As we setup these structures, we collect all bio's together into a list
2744 * which we then process collectively to add pages, and then process again
2745 * to pass to generic_make_request.
2747 * The r10_bio structures are linked using a borrowed master_bio pointer.
2748 * This link is counted in ->remaining. When the r10_bio that points to NULL
2749 * has its remaining count decremented to 0, the whole complex operation
2754 static sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
,
2755 int *skipped
, int go_faster
)
2757 struct r10conf
*conf
= mddev
->private;
2758 struct r10bio
*r10_bio
;
2759 struct bio
*biolist
= NULL
, *bio
;
2760 sector_t max_sector
, nr_sectors
;
2763 sector_t sync_blocks
;
2764 sector_t sectors_skipped
= 0;
2765 int chunks_skipped
= 0;
2766 sector_t chunk_mask
= conf
->geo
.chunk_mask
;
2768 if (!conf
->r10buf_pool
)
2769 if (init_resync(conf
))
2773 max_sector
= mddev
->dev_sectors
;
2774 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
2775 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2776 max_sector
= mddev
->resync_max_sectors
;
2777 if (sector_nr
>= max_sector
) {
2778 /* If we aborted, we need to abort the
2779 * sync on the 'current' bitmap chucks (there can
2780 * be several when recovering multiple devices).
2781 * as we may have started syncing it but not finished.
2782 * We can find the current address in
2783 * mddev->curr_resync, but for recovery,
2784 * we need to convert that to several
2785 * virtual addresses.
2787 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
2792 if (mddev
->curr_resync
< max_sector
) { /* aborted */
2793 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2794 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2796 else for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2798 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
2799 bitmap_end_sync(mddev
->bitmap
, sect
,
2803 /* completed sync */
2804 if ((!mddev
->bitmap
|| conf
->fullsync
)
2805 && conf
->have_replacement
2806 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2807 /* Completed a full sync so the replacements
2808 * are now fully recovered.
2810 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
2811 if (conf
->mirrors
[i
].replacement
)
2812 conf
->mirrors
[i
].replacement
2818 bitmap_close_sync(mddev
->bitmap
);
2821 return sectors_skipped
;
2824 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2825 return reshape_request(mddev
, sector_nr
, skipped
);
2827 if (chunks_skipped
>= conf
->geo
.raid_disks
) {
2828 /* if there has been nothing to do on any drive,
2829 * then there is nothing to do at all..
2832 return (max_sector
- sector_nr
) + sectors_skipped
;
2835 if (max_sector
> mddev
->resync_max
)
2836 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
2838 /* make sure whole request will fit in a chunk - if chunks
2841 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
&&
2842 max_sector
> (sector_nr
| chunk_mask
))
2843 max_sector
= (sector_nr
| chunk_mask
) + 1;
2845 * If there is non-resync activity waiting for us then
2846 * put in a delay to throttle resync.
2848 if (!go_faster
&& conf
->nr_waiting
)
2849 msleep_interruptible(1000);
2851 /* Again, very different code for resync and recovery.
2852 * Both must result in an r10bio with a list of bios that
2853 * have bi_end_io, bi_sector, bi_bdev set,
2854 * and bi_private set to the r10bio.
2855 * For recovery, we may actually create several r10bios
2856 * with 2 bios in each, that correspond to the bios in the main one.
2857 * In this case, the subordinate r10bios link back through a
2858 * borrowed master_bio pointer, and the counter in the master
2859 * includes a ref from each subordinate.
2861 /* First, we decide what to do and set ->bi_end_io
2862 * To end_sync_read if we want to read, and
2863 * end_sync_write if we will want to write.
2866 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
2867 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2868 /* recovery... the complicated one */
2872 for (i
= 0 ; i
< conf
->geo
.raid_disks
; i
++) {
2878 struct mirror_info
*mirror
= &conf
->mirrors
[i
];
2880 if ((mirror
->rdev
== NULL
||
2881 test_bit(In_sync
, &mirror
->rdev
->flags
))
2883 (mirror
->replacement
== NULL
||
2885 &mirror
->replacement
->flags
)))
2889 /* want to reconstruct this device */
2891 sect
= raid10_find_virt(conf
, sector_nr
, i
);
2892 if (sect
>= mddev
->resync_max_sectors
) {
2893 /* last stripe is not complete - don't
2894 * try to recover this sector.
2898 /* Unless we are doing a full sync, or a replacement
2899 * we only need to recover the block if it is set in
2902 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
2904 if (sync_blocks
< max_sync
)
2905 max_sync
= sync_blocks
;
2907 mirror
->replacement
== NULL
&&
2909 /* yep, skip the sync_blocks here, but don't assume
2910 * that there will never be anything to do here
2912 chunks_skipped
= -1;
2916 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2917 raise_barrier(conf
, rb2
!= NULL
);
2918 atomic_set(&r10_bio
->remaining
, 0);
2920 r10_bio
->master_bio
= (struct bio
*)rb2
;
2922 atomic_inc(&rb2
->remaining
);
2923 r10_bio
->mddev
= mddev
;
2924 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
2925 r10_bio
->sector
= sect
;
2927 raid10_find_phys(conf
, r10_bio
);
2929 /* Need to check if the array will still be
2932 for (j
= 0; j
< conf
->geo
.raid_disks
; j
++)
2933 if (conf
->mirrors
[j
].rdev
== NULL
||
2934 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
2939 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
2940 &sync_blocks
, still_degraded
);
2943 for (j
=0; j
<conf
->copies
;j
++) {
2945 int d
= r10_bio
->devs
[j
].devnum
;
2946 sector_t from_addr
, to_addr
;
2947 struct md_rdev
*rdev
;
2948 sector_t sector
, first_bad
;
2950 if (!conf
->mirrors
[d
].rdev
||
2951 !test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
))
2953 /* This is where we read from */
2955 rdev
= conf
->mirrors
[d
].rdev
;
2956 sector
= r10_bio
->devs
[j
].addr
;
2958 if (is_badblock(rdev
, sector
, max_sync
,
2959 &first_bad
, &bad_sectors
)) {
2960 if (first_bad
> sector
)
2961 max_sync
= first_bad
- sector
;
2963 bad_sectors
-= (sector
2965 if (max_sync
> bad_sectors
)
2966 max_sync
= bad_sectors
;
2970 bio
= r10_bio
->devs
[0].bio
;
2971 bio
->bi_next
= biolist
;
2973 bio
->bi_private
= r10_bio
;
2974 bio
->bi_end_io
= end_sync_read
;
2976 from_addr
= r10_bio
->devs
[j
].addr
;
2977 bio
->bi_sector
= from_addr
+ rdev
->data_offset
;
2978 bio
->bi_bdev
= rdev
->bdev
;
2979 atomic_inc(&rdev
->nr_pending
);
2980 /* and we write to 'i' (if not in_sync) */
2982 for (k
=0; k
<conf
->copies
; k
++)
2983 if (r10_bio
->devs
[k
].devnum
== i
)
2985 BUG_ON(k
== conf
->copies
);
2986 to_addr
= r10_bio
->devs
[k
].addr
;
2987 r10_bio
->devs
[0].devnum
= d
;
2988 r10_bio
->devs
[0].addr
= from_addr
;
2989 r10_bio
->devs
[1].devnum
= i
;
2990 r10_bio
->devs
[1].addr
= to_addr
;
2992 rdev
= mirror
->rdev
;
2993 if (!test_bit(In_sync
, &rdev
->flags
)) {
2994 bio
= r10_bio
->devs
[1].bio
;
2995 bio
->bi_next
= biolist
;
2997 bio
->bi_private
= r10_bio
;
2998 bio
->bi_end_io
= end_sync_write
;
3000 bio
->bi_sector
= to_addr
3001 + rdev
->data_offset
;
3002 bio
->bi_bdev
= rdev
->bdev
;
3003 atomic_inc(&r10_bio
->remaining
);
3005 r10_bio
->devs
[1].bio
->bi_end_io
= NULL
;
3007 /* and maybe write to replacement */
3008 bio
= r10_bio
->devs
[1].repl_bio
;
3010 bio
->bi_end_io
= NULL
;
3011 rdev
= mirror
->replacement
;
3012 /* Note: if rdev != NULL, then bio
3013 * cannot be NULL as r10buf_pool_alloc will
3014 * have allocated it.
3015 * So the second test here is pointless.
3016 * But it keeps semantic-checkers happy, and
3017 * this comment keeps human reviewers
3020 if (rdev
== NULL
|| bio
== NULL
||
3021 test_bit(Faulty
, &rdev
->flags
))
3023 bio
->bi_next
= biolist
;
3025 bio
->bi_private
= r10_bio
;
3026 bio
->bi_end_io
= end_sync_write
;
3028 bio
->bi_sector
= to_addr
+ rdev
->data_offset
;
3029 bio
->bi_bdev
= rdev
->bdev
;
3030 atomic_inc(&r10_bio
->remaining
);
3033 if (j
== conf
->copies
) {
3034 /* Cannot recover, so abort the recovery or
3035 * record a bad block */
3038 atomic_dec(&rb2
->remaining
);
3041 /* problem is that there are bad blocks
3042 * on other device(s)
3045 for (k
= 0; k
< conf
->copies
; k
++)
3046 if (r10_bio
->devs
[k
].devnum
== i
)
3048 if (!test_bit(In_sync
,
3049 &mirror
->rdev
->flags
)
3050 && !rdev_set_badblocks(
3052 r10_bio
->devs
[k
].addr
,
3055 if (mirror
->replacement
&&
3056 !rdev_set_badblocks(
3057 mirror
->replacement
,
3058 r10_bio
->devs
[k
].addr
,
3063 if (!test_and_set_bit(MD_RECOVERY_INTR
,
3065 printk(KERN_INFO
"md/raid10:%s: insufficient "
3066 "working devices for recovery.\n",
3068 mirror
->recovery_disabled
3069 = mddev
->recovery_disabled
;
3074 if (biolist
== NULL
) {
3076 struct r10bio
*rb2
= r10_bio
;
3077 r10_bio
= (struct r10bio
*) rb2
->master_bio
;
3078 rb2
->master_bio
= NULL
;
3084 /* resync. Schedule a read for every block at this virt offset */
3087 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
3089 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
3090 &sync_blocks
, mddev
->degraded
) &&
3091 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
3092 &mddev
->recovery
)) {
3093 /* We can skip this block */
3095 return sync_blocks
+ sectors_skipped
;
3097 if (sync_blocks
< max_sync
)
3098 max_sync
= sync_blocks
;
3099 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
3101 r10_bio
->mddev
= mddev
;
3102 atomic_set(&r10_bio
->remaining
, 0);
3103 raise_barrier(conf
, 0);
3104 conf
->next_resync
= sector_nr
;
3106 r10_bio
->master_bio
= NULL
;
3107 r10_bio
->sector
= sector_nr
;
3108 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
3109 raid10_find_phys(conf
, r10_bio
);
3110 r10_bio
->sectors
= (sector_nr
| chunk_mask
) - sector_nr
+ 1;
3112 for (i
= 0; i
< conf
->copies
; i
++) {
3113 int d
= r10_bio
->devs
[i
].devnum
;
3114 sector_t first_bad
, sector
;
3117 if (r10_bio
->devs
[i
].repl_bio
)
3118 r10_bio
->devs
[i
].repl_bio
->bi_end_io
= NULL
;
3120 bio
= r10_bio
->devs
[i
].bio
;
3121 bio
->bi_end_io
= NULL
;
3122 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3123 if (conf
->mirrors
[d
].rdev
== NULL
||
3124 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
3126 sector
= r10_bio
->devs
[i
].addr
;
3127 if (is_badblock(conf
->mirrors
[d
].rdev
,
3129 &first_bad
, &bad_sectors
)) {
3130 if (first_bad
> sector
)
3131 max_sync
= first_bad
- sector
;
3133 bad_sectors
-= (sector
- first_bad
);
3134 if (max_sync
> bad_sectors
)
3135 max_sync
= max_sync
;
3139 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
3140 atomic_inc(&r10_bio
->remaining
);
3141 bio
->bi_next
= biolist
;
3143 bio
->bi_private
= r10_bio
;
3144 bio
->bi_end_io
= end_sync_read
;
3146 bio
->bi_sector
= sector
+
3147 conf
->mirrors
[d
].rdev
->data_offset
;
3148 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
3151 if (conf
->mirrors
[d
].replacement
== NULL
||
3153 &conf
->mirrors
[d
].replacement
->flags
))
3156 /* Need to set up for writing to the replacement */
3157 bio
= r10_bio
->devs
[i
].repl_bio
;
3158 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
3160 sector
= r10_bio
->devs
[i
].addr
;
3161 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
3162 bio
->bi_next
= biolist
;
3164 bio
->bi_private
= r10_bio
;
3165 bio
->bi_end_io
= end_sync_write
;
3167 bio
->bi_sector
= sector
+
3168 conf
->mirrors
[d
].replacement
->data_offset
;
3169 bio
->bi_bdev
= conf
->mirrors
[d
].replacement
->bdev
;
3174 for (i
=0; i
<conf
->copies
; i
++) {
3175 int d
= r10_bio
->devs
[i
].devnum
;
3176 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
3177 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
3179 if (r10_bio
->devs
[i
].repl_bio
&&
3180 r10_bio
->devs
[i
].repl_bio
->bi_end_io
)
3182 conf
->mirrors
[d
].replacement
,
3191 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
3193 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
3195 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
3198 bio
->bi_phys_segments
= 0;
3203 if (sector_nr
+ max_sync
< max_sector
)
3204 max_sector
= sector_nr
+ max_sync
;
3207 int len
= PAGE_SIZE
;
3208 if (sector_nr
+ (len
>>9) > max_sector
)
3209 len
= (max_sector
- sector_nr
) << 9;
3212 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
3214 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
3215 if (bio_add_page(bio
, page
, len
, 0))
3219 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
3220 for (bio2
= biolist
;
3221 bio2
&& bio2
!= bio
;
3222 bio2
= bio2
->bi_next
) {
3223 /* remove last page from this bio */
3225 bio2
->bi_size
-= len
;
3226 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
3230 nr_sectors
+= len
>>9;
3231 sector_nr
+= len
>>9;
3232 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
3234 r10_bio
->sectors
= nr_sectors
;
3238 biolist
= biolist
->bi_next
;
3240 bio
->bi_next
= NULL
;
3241 r10_bio
= bio
->bi_private
;
3242 r10_bio
->sectors
= nr_sectors
;
3244 if (bio
->bi_end_io
== end_sync_read
) {
3245 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
3246 generic_make_request(bio
);
3250 if (sectors_skipped
)
3251 /* pretend they weren't skipped, it makes
3252 * no important difference in this case
3254 md_done_sync(mddev
, sectors_skipped
, 1);
3256 return sectors_skipped
+ nr_sectors
;
3258 /* There is nowhere to write, so all non-sync
3259 * drives must be failed or in resync, all drives
3260 * have a bad block, so try the next chunk...
3262 if (sector_nr
+ max_sync
< max_sector
)
3263 max_sector
= sector_nr
+ max_sync
;
3265 sectors_skipped
+= (max_sector
- sector_nr
);
3267 sector_nr
= max_sector
;
3272 raid10_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
3275 struct r10conf
*conf
= mddev
->private;
3278 raid_disks
= min(conf
->geo
.raid_disks
,
3279 conf
->prev
.raid_disks
);
3281 sectors
= conf
->dev_sectors
;
3283 size
= sectors
>> conf
->geo
.chunk_shift
;
3284 sector_div(size
, conf
->geo
.far_copies
);
3285 size
= size
* raid_disks
;
3286 sector_div(size
, conf
->geo
.near_copies
);
3288 return size
<< conf
->geo
.chunk_shift
;
3291 static void calc_sectors(struct r10conf
*conf
, sector_t size
)
3293 /* Calculate the number of sectors-per-device that will
3294 * actually be used, and set conf->dev_sectors and
3298 size
= size
>> conf
->geo
.chunk_shift
;
3299 sector_div(size
, conf
->geo
.far_copies
);
3300 size
= size
* conf
->geo
.raid_disks
;
3301 sector_div(size
, conf
->geo
.near_copies
);
3302 /* 'size' is now the number of chunks in the array */
3303 /* calculate "used chunks per device" */
3304 size
= size
* conf
->copies
;
3306 /* We need to round up when dividing by raid_disks to
3307 * get the stride size.
3309 size
= DIV_ROUND_UP_SECTOR_T(size
, conf
->geo
.raid_disks
);
3311 conf
->dev_sectors
= size
<< conf
->geo
.chunk_shift
;
3313 if (conf
->geo
.far_offset
)
3314 conf
->geo
.stride
= 1 << conf
->geo
.chunk_shift
;
3316 sector_div(size
, conf
->geo
.far_copies
);
3317 conf
->geo
.stride
= size
<< conf
->geo
.chunk_shift
;
3321 enum geo_type
{geo_new
, geo_old
, geo_start
};
3322 static int setup_geo(struct geom
*geo
, struct mddev
*mddev
, enum geo_type
new)
3325 int layout
, chunk
, disks
;
3328 layout
= mddev
->layout
;
3329 chunk
= mddev
->chunk_sectors
;
3330 disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3333 layout
= mddev
->new_layout
;
3334 chunk
= mddev
->new_chunk_sectors
;
3335 disks
= mddev
->raid_disks
;
3337 default: /* avoid 'may be unused' warnings */
3338 case geo_start
: /* new when starting reshape - raid_disks not
3340 layout
= mddev
->new_layout
;
3341 chunk
= mddev
->new_chunk_sectors
;
3342 disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3347 if (chunk
< (PAGE_SIZE
>> 9) ||
3348 !is_power_of_2(chunk
))
3351 fc
= (layout
>> 8) & 255;
3352 fo
= layout
& (1<<16);
3353 geo
->raid_disks
= disks
;
3354 geo
->near_copies
= nc
;
3355 geo
->far_copies
= fc
;
3356 geo
->far_offset
= fo
;
3357 geo
->chunk_mask
= chunk
- 1;
3358 geo
->chunk_shift
= ffz(~chunk
);
3362 static struct r10conf
*setup_conf(struct mddev
*mddev
)
3364 struct r10conf
*conf
= NULL
;
3369 copies
= setup_geo(&geo
, mddev
, geo_new
);
3372 printk(KERN_ERR
"md/raid10:%s: chunk size must be "
3373 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3374 mdname(mddev
), PAGE_SIZE
);
3378 if (copies
< 2 || copies
> mddev
->raid_disks
) {
3379 printk(KERN_ERR
"md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3380 mdname(mddev
), mddev
->new_layout
);
3385 conf
= kzalloc(sizeof(struct r10conf
), GFP_KERNEL
);
3389 /* FIXME calc properly */
3390 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*(mddev
->raid_disks
+
3391 max(0,mddev
->delta_disks
)),
3396 conf
->tmppage
= alloc_page(GFP_KERNEL
);
3401 conf
->copies
= copies
;
3402 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
3403 r10bio_pool_free
, conf
);
3404 if (!conf
->r10bio_pool
)
3407 calc_sectors(conf
, mddev
->dev_sectors
);
3408 if (mddev
->reshape_position
== MaxSector
) {
3409 conf
->prev
= conf
->geo
;
3410 conf
->reshape_progress
= MaxSector
;
3412 if (setup_geo(&conf
->prev
, mddev
, geo_old
) != conf
->copies
) {
3416 conf
->reshape_progress
= mddev
->reshape_position
;
3417 if (conf
->prev
.far_offset
)
3418 conf
->prev
.stride
= 1 << conf
->prev
.chunk_shift
;
3420 /* far_copies must be 1 */
3421 conf
->prev
.stride
= conf
->dev_sectors
;
3423 spin_lock_init(&conf
->device_lock
);
3424 INIT_LIST_HEAD(&conf
->retry_list
);
3426 spin_lock_init(&conf
->resync_lock
);
3427 init_waitqueue_head(&conf
->wait_barrier
);
3429 conf
->thread
= md_register_thread(raid10d
, mddev
, "raid10");
3433 conf
->mddev
= mddev
;
3438 printk(KERN_ERR
"md/raid10:%s: couldn't allocate memory.\n",
3441 if (conf
->r10bio_pool
)
3442 mempool_destroy(conf
->r10bio_pool
);
3443 kfree(conf
->mirrors
);
3444 safe_put_page(conf
->tmppage
);
3447 return ERR_PTR(err
);
3450 static int run(struct mddev
*mddev
)
3452 struct r10conf
*conf
;
3453 int i
, disk_idx
, chunk_size
;
3454 struct mirror_info
*disk
;
3455 struct md_rdev
*rdev
;
3457 sector_t min_offset_diff
= 0;
3460 if (mddev
->private == NULL
) {
3461 conf
= setup_conf(mddev
);
3463 return PTR_ERR(conf
);
3464 mddev
->private = conf
;
3466 conf
= mddev
->private;
3470 mddev
->thread
= conf
->thread
;
3471 conf
->thread
= NULL
;
3473 chunk_size
= mddev
->chunk_sectors
<< 9;
3474 blk_queue_io_min(mddev
->queue
, chunk_size
);
3475 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
)
3476 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->geo
.raid_disks
);
3478 blk_queue_io_opt(mddev
->queue
, chunk_size
*
3479 (conf
->geo
.raid_disks
/ conf
->geo
.near_copies
));
3481 rdev_for_each(rdev
, mddev
) {
3483 struct request_queue
*q
;
3485 disk_idx
= rdev
->raid_disk
;
3488 if (disk_idx
>= conf
->geo
.raid_disks
&&
3489 disk_idx
>= conf
->prev
.raid_disks
)
3491 disk
= conf
->mirrors
+ disk_idx
;
3493 if (test_bit(Replacement
, &rdev
->flags
)) {
3494 if (disk
->replacement
)
3496 disk
->replacement
= rdev
;
3502 q
= bdev_get_queue(rdev
->bdev
);
3503 if (q
->merge_bvec_fn
)
3504 mddev
->merge_check_needed
= 1;
3505 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
3506 if (!mddev
->reshape_backwards
)
3510 if (first
|| diff
< min_offset_diff
)
3511 min_offset_diff
= diff
;
3513 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
3514 rdev
->data_offset
<< 9);
3516 disk
->head_position
= 0;
3519 /* need to check that every block has at least one working mirror */
3520 if (!enough(conf
, -1)) {
3521 printk(KERN_ERR
"md/raid10:%s: not enough operational mirrors.\n",
3526 if (conf
->reshape_progress
!= MaxSector
) {
3527 /* must ensure that shape change is supported */
3528 if (conf
->geo
.far_copies
!= 1 &&
3529 conf
->geo
.far_offset
== 0)
3531 if (conf
->prev
.far_copies
!= 1 &&
3532 conf
->geo
.far_offset
== 0)
3536 mddev
->degraded
= 0;
3538 i
< conf
->geo
.raid_disks
3539 || i
< conf
->prev
.raid_disks
;
3542 disk
= conf
->mirrors
+ i
;
3544 if (!disk
->rdev
&& disk
->replacement
) {
3545 /* The replacement is all we have - use it */
3546 disk
->rdev
= disk
->replacement
;
3547 disk
->replacement
= NULL
;
3548 clear_bit(Replacement
, &disk
->rdev
->flags
);
3552 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
3553 disk
->head_position
= 0;
3558 disk
->recovery_disabled
= mddev
->recovery_disabled
- 1;
3561 if (mddev
->recovery_cp
!= MaxSector
)
3562 printk(KERN_NOTICE
"md/raid10:%s: not clean"
3563 " -- starting background reconstruction\n",
3566 "md/raid10:%s: active with %d out of %d devices\n",
3567 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
,
3568 conf
->geo
.raid_disks
);
3570 * Ok, everything is just fine now
3572 mddev
->dev_sectors
= conf
->dev_sectors
;
3573 size
= raid10_size(mddev
, 0, 0);
3574 md_set_array_sectors(mddev
, size
);
3575 mddev
->resync_max_sectors
= size
;
3577 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
3578 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
3580 /* Calculate max read-ahead size.
3581 * We need to readahead at least twice a whole stripe....
3585 int stripe
= conf
->geo
.raid_disks
*
3586 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
3587 stripe
/= conf
->geo
.near_copies
;
3588 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
3589 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
3592 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
3594 if (md_integrity_register(mddev
))
3597 if (conf
->reshape_progress
!= MaxSector
) {
3598 unsigned long before_length
, after_length
;
3600 before_length
= ((1 << conf
->prev
.chunk_shift
) *
3601 conf
->prev
.far_copies
);
3602 after_length
= ((1 << conf
->geo
.chunk_shift
) *
3603 conf
->geo
.far_copies
);
3605 if (max(before_length
, after_length
) > min_offset_diff
) {
3606 /* This cannot work */
3607 printk("md/raid10: offset difference not enough to continue reshape\n");
3610 conf
->offset_diff
= min_offset_diff
;
3612 conf
->reshape_safe
= conf
->reshape_progress
;
3613 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3614 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3615 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3616 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3617 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3624 md_unregister_thread(&mddev
->thread
);
3625 if (conf
->r10bio_pool
)
3626 mempool_destroy(conf
->r10bio_pool
);
3627 safe_put_page(conf
->tmppage
);
3628 kfree(conf
->mirrors
);
3630 mddev
->private = NULL
;
3635 static int stop(struct mddev
*mddev
)
3637 struct r10conf
*conf
= mddev
->private;
3639 raise_barrier(conf
, 0);
3640 lower_barrier(conf
);
3642 md_unregister_thread(&mddev
->thread
);
3643 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
3644 if (conf
->r10bio_pool
)
3645 mempool_destroy(conf
->r10bio_pool
);
3646 kfree(conf
->mirrors
);
3648 mddev
->private = NULL
;
3652 static void raid10_quiesce(struct mddev
*mddev
, int state
)
3654 struct r10conf
*conf
= mddev
->private;
3658 raise_barrier(conf
, 0);
3661 lower_barrier(conf
);
3666 static int raid10_resize(struct mddev
*mddev
, sector_t sectors
)
3668 /* Resize of 'far' arrays is not supported.
3669 * For 'near' and 'offset' arrays we can set the
3670 * number of sectors used to be an appropriate multiple
3671 * of the chunk size.
3672 * For 'offset', this is far_copies*chunksize.
3673 * For 'near' the multiplier is the LCM of
3674 * near_copies and raid_disks.
3675 * So if far_copies > 1 && !far_offset, fail.
3676 * Else find LCM(raid_disks, near_copy)*far_copies and
3677 * multiply by chunk_size. Then round to this number.
3678 * This is mostly done by raid10_size()
3680 struct r10conf
*conf
= mddev
->private;
3681 sector_t oldsize
, size
;
3683 if (mddev
->reshape_position
!= MaxSector
)
3686 if (conf
->geo
.far_copies
> 1 && !conf
->geo
.far_offset
)
3689 oldsize
= raid10_size(mddev
, 0, 0);
3690 size
= raid10_size(mddev
, sectors
, 0);
3691 if (mddev
->external_size
&&
3692 mddev
->array_sectors
> size
)
3694 if (mddev
->bitmap
) {
3695 int ret
= bitmap_resize(mddev
->bitmap
, size
, 0, 0);
3699 md_set_array_sectors(mddev
, size
);
3700 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3701 revalidate_disk(mddev
->gendisk
);
3702 if (sectors
> mddev
->dev_sectors
&&
3703 mddev
->recovery_cp
> oldsize
) {
3704 mddev
->recovery_cp
= oldsize
;
3705 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3707 calc_sectors(conf
, sectors
);
3708 mddev
->dev_sectors
= conf
->dev_sectors
;
3709 mddev
->resync_max_sectors
= size
;
3713 static void *raid10_takeover_raid0(struct mddev
*mddev
)
3715 struct md_rdev
*rdev
;
3716 struct r10conf
*conf
;
3718 if (mddev
->degraded
> 0) {
3719 printk(KERN_ERR
"md/raid10:%s: Error: degraded raid0!\n",
3721 return ERR_PTR(-EINVAL
);
3724 /* Set new parameters */
3725 mddev
->new_level
= 10;
3726 /* new layout: far_copies = 1, near_copies = 2 */
3727 mddev
->new_layout
= (1<<8) + 2;
3728 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3729 mddev
->delta_disks
= mddev
->raid_disks
;
3730 mddev
->raid_disks
*= 2;
3731 /* make sure it will be not marked as dirty */
3732 mddev
->recovery_cp
= MaxSector
;
3734 conf
= setup_conf(mddev
);
3735 if (!IS_ERR(conf
)) {
3736 rdev_for_each(rdev
, mddev
)
3737 if (rdev
->raid_disk
>= 0)
3738 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
3745 static void *raid10_takeover(struct mddev
*mddev
)
3747 struct r0conf
*raid0_conf
;
3749 /* raid10 can take over:
3750 * raid0 - providing it has only two drives
3752 if (mddev
->level
== 0) {
3753 /* for raid0 takeover only one zone is supported */
3754 raid0_conf
= mddev
->private;
3755 if (raid0_conf
->nr_strip_zones
> 1) {
3756 printk(KERN_ERR
"md/raid10:%s: cannot takeover raid 0"
3757 " with more than one zone.\n",
3759 return ERR_PTR(-EINVAL
);
3761 return raid10_takeover_raid0(mddev
);
3763 return ERR_PTR(-EINVAL
);
3766 static int raid10_check_reshape(struct mddev
*mddev
)
3768 /* Called when there is a request to change
3769 * - layout (to ->new_layout)
3770 * - chunk size (to ->new_chunk_sectors)
3771 * - raid_disks (by delta_disks)
3772 * or when trying to restart a reshape that was ongoing.
3774 * We need to validate the request and possibly allocate
3775 * space if that might be an issue later.
3777 * Currently we reject any reshape of a 'far' mode array,
3778 * allow chunk size to change if new is generally acceptable,
3779 * allow raid_disks to increase, and allow
3780 * a switch between 'near' mode and 'offset' mode.
3782 struct r10conf
*conf
= mddev
->private;
3785 if (conf
->geo
.far_copies
!= 1 && !conf
->geo
.far_offset
)
3788 if (setup_geo(&geo
, mddev
, geo_start
) != conf
->copies
)
3789 /* mustn't change number of copies */
3791 if (geo
.far_copies
> 1 && !geo
.far_offset
)
3792 /* Cannot switch to 'far' mode */
3795 if (mddev
->array_sectors
& geo
.chunk_mask
)
3796 /* not factor of array size */
3799 if (!enough(conf
, -1))
3802 kfree(conf
->mirrors_new
);
3803 conf
->mirrors_new
= NULL
;
3804 if (mddev
->delta_disks
> 0) {
3805 /* allocate new 'mirrors' list */
3806 conf
->mirrors_new
= kzalloc(
3807 sizeof(struct mirror_info
)
3808 *(mddev
->raid_disks
+
3809 mddev
->delta_disks
),
3811 if (!conf
->mirrors_new
)
3818 * Need to check if array has failed when deciding whether to:
3820 * - remove non-faulty devices
3823 * This determination is simple when no reshape is happening.
3824 * However if there is a reshape, we need to carefully check
3825 * both the before and after sections.
3826 * This is because some failed devices may only affect one
3827 * of the two sections, and some non-in_sync devices may
3828 * be insync in the section most affected by failed devices.
3830 static int calc_degraded(struct r10conf
*conf
)
3832 int degraded
, degraded2
;
3837 /* 'prev' section first */
3838 for (i
= 0; i
< conf
->prev
.raid_disks
; i
++) {
3839 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
3840 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
3842 else if (!test_bit(In_sync
, &rdev
->flags
))
3843 /* When we can reduce the number of devices in
3844 * an array, this might not contribute to
3845 * 'degraded'. It does now.
3850 if (conf
->geo
.raid_disks
== conf
->prev
.raid_disks
)
3854 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
3855 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
3856 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
3858 else if (!test_bit(In_sync
, &rdev
->flags
)) {
3859 /* If reshape is increasing the number of devices,
3860 * this section has already been recovered, so
3861 * it doesn't contribute to degraded.
3864 if (conf
->geo
.raid_disks
<= conf
->prev
.raid_disks
)
3869 if (degraded2
> degraded
)
3874 static int raid10_start_reshape(struct mddev
*mddev
)
3876 /* A 'reshape' has been requested. This commits
3877 * the various 'new' fields and sets MD_RECOVER_RESHAPE
3878 * This also checks if there are enough spares and adds them
3880 * We currently require enough spares to make the final
3881 * array non-degraded. We also require that the difference
3882 * between old and new data_offset - on each device - is
3883 * enough that we never risk over-writing.
3886 unsigned long before_length
, after_length
;
3887 sector_t min_offset_diff
= 0;
3890 struct r10conf
*conf
= mddev
->private;
3891 struct md_rdev
*rdev
;
3895 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3898 if (setup_geo(&new, mddev
, geo_start
) != conf
->copies
)
3901 before_length
= ((1 << conf
->prev
.chunk_shift
) *
3902 conf
->prev
.far_copies
);
3903 after_length
= ((1 << conf
->geo
.chunk_shift
) *
3904 conf
->geo
.far_copies
);
3906 rdev_for_each(rdev
, mddev
) {
3907 if (!test_bit(In_sync
, &rdev
->flags
)
3908 && !test_bit(Faulty
, &rdev
->flags
))
3910 if (rdev
->raid_disk
>= 0) {
3911 long long diff
= (rdev
->new_data_offset
3912 - rdev
->data_offset
);
3913 if (!mddev
->reshape_backwards
)
3917 if (first
|| diff
< min_offset_diff
)
3918 min_offset_diff
= diff
;
3922 if (max(before_length
, after_length
) > min_offset_diff
)
3925 if (spares
< mddev
->delta_disks
)
3928 conf
->offset_diff
= min_offset_diff
;
3929 spin_lock_irq(&conf
->device_lock
);
3930 if (conf
->mirrors_new
) {
3931 memcpy(conf
->mirrors_new
, conf
->mirrors
,
3932 sizeof(struct mirror_info
)*conf
->prev
.raid_disks
);
3934 kfree(conf
->mirrors_old
); /* FIXME and elsewhere */
3935 conf
->mirrors_old
= conf
->mirrors
;
3936 conf
->mirrors
= conf
->mirrors_new
;
3937 conf
->mirrors_new
= NULL
;
3939 setup_geo(&conf
->geo
, mddev
, geo_start
);
3941 if (mddev
->reshape_backwards
) {
3942 sector_t size
= raid10_size(mddev
, 0, 0);
3943 if (size
< mddev
->array_sectors
) {
3944 spin_unlock_irq(&conf
->device_lock
);
3945 printk(KERN_ERR
"md/raid10:%s: array size must be reduce before number of disks\n",
3949 mddev
->resync_max_sectors
= size
;
3950 conf
->reshape_progress
= size
;
3952 conf
->reshape_progress
= 0;
3953 spin_unlock_irq(&conf
->device_lock
);
3955 if (mddev
->delta_disks
&& mddev
->bitmap
) {
3956 ret
= bitmap_resize(mddev
->bitmap
,
3957 raid10_size(mddev
, 0,
3958 conf
->geo
.raid_disks
),
3963 if (mddev
->delta_disks
> 0) {
3964 rdev_for_each(rdev
, mddev
)
3965 if (rdev
->raid_disk
< 0 &&
3966 !test_bit(Faulty
, &rdev
->flags
)) {
3967 if (raid10_add_disk(mddev
, rdev
) == 0) {
3968 if (rdev
->raid_disk
>=
3969 conf
->prev
.raid_disks
)
3970 set_bit(In_sync
, &rdev
->flags
);
3972 rdev
->recovery_offset
= 0;
3974 if (sysfs_link_rdev(mddev
, rdev
))
3975 /* Failure here is OK */;
3977 } else if (rdev
->raid_disk
>= conf
->prev
.raid_disks
3978 && !test_bit(Faulty
, &rdev
->flags
)) {
3979 /* This is a spare that was manually added */
3980 set_bit(In_sync
, &rdev
->flags
);
3983 /* When a reshape changes the number of devices,
3984 * ->degraded is measured against the larger of the
3985 * pre and post numbers.
3987 spin_lock_irq(&conf
->device_lock
);
3988 mddev
->degraded
= calc_degraded(conf
);
3989 spin_unlock_irq(&conf
->device_lock
);
3990 mddev
->raid_disks
= conf
->geo
.raid_disks
;
3991 mddev
->reshape_position
= conf
->reshape_progress
;
3992 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3994 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3995 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3996 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3997 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3999 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
4001 if (!mddev
->sync_thread
) {
4005 conf
->reshape_checkpoint
= jiffies
;
4006 md_wakeup_thread(mddev
->sync_thread
);
4007 md_new_event(mddev
);
4011 mddev
->recovery
= 0;
4012 spin_lock_irq(&conf
->device_lock
);
4013 conf
->geo
= conf
->prev
;
4014 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4015 rdev_for_each(rdev
, mddev
)
4016 rdev
->new_data_offset
= rdev
->data_offset
;
4018 conf
->reshape_progress
= MaxSector
;
4019 mddev
->reshape_position
= MaxSector
;
4020 spin_unlock_irq(&conf
->device_lock
);
4024 /* Calculate the last device-address that could contain
4025 * any block from the chunk that includes the array-address 's'
4026 * and report the next address.
4027 * i.e. the address returned will be chunk-aligned and after
4028 * any data that is in the chunk containing 's'.
4030 static sector_t
last_dev_address(sector_t s
, struct geom
*geo
)
4032 s
= (s
| geo
->chunk_mask
) + 1;
4033 s
>>= geo
->chunk_shift
;
4034 s
*= geo
->near_copies
;
4035 s
= DIV_ROUND_UP_SECTOR_T(s
, geo
->raid_disks
);
4036 s
*= geo
->far_copies
;
4037 s
<<= geo
->chunk_shift
;
4041 /* Calculate the first device-address that could contain
4042 * any block from the chunk that includes the array-address 's'.
4043 * This too will be the start of a chunk
4045 static sector_t
first_dev_address(sector_t s
, struct geom
*geo
)
4047 s
>>= geo
->chunk_shift
;
4048 s
*= geo
->near_copies
;
4049 sector_div(s
, geo
->raid_disks
);
4050 s
*= geo
->far_copies
;
4051 s
<<= geo
->chunk_shift
;
4055 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
4058 /* We simply copy at most one chunk (smallest of old and new)
4059 * at a time, possibly less if that exceeds RESYNC_PAGES,
4060 * or we hit a bad block or something.
4061 * This might mean we pause for normal IO in the middle of
4062 * a chunk, but that is not a problem was mddev->reshape_position
4063 * can record any location.
4065 * If we will want to write to a location that isn't
4066 * yet recorded as 'safe' (i.e. in metadata on disk) then
4067 * we need to flush all reshape requests and update the metadata.
4069 * When reshaping forwards (e.g. to more devices), we interpret
4070 * 'safe' as the earliest block which might not have been copied
4071 * down yet. We divide this by previous stripe size and multiply
4072 * by previous stripe length to get lowest device offset that we
4073 * cannot write to yet.
4074 * We interpret 'sector_nr' as an address that we want to write to.
4075 * From this we use last_device_address() to find where we might
4076 * write to, and first_device_address on the 'safe' position.
4077 * If this 'next' write position is after the 'safe' position,
4078 * we must update the metadata to increase the 'safe' position.
4080 * When reshaping backwards, we round in the opposite direction
4081 * and perform the reverse test: next write position must not be
4082 * less than current safe position.
4084 * In all this the minimum difference in data offsets
4085 * (conf->offset_diff - always positive) allows a bit of slack,
4086 * so next can be after 'safe', but not by more than offset_disk
4088 * We need to prepare all the bios here before we start any IO
4089 * to ensure the size we choose is acceptable to all devices.
4090 * The means one for each copy for write-out and an extra one for
4092 * We store the read-in bio in ->master_bio and the others in
4093 * ->devs[x].bio and ->devs[x].repl_bio.
4095 struct r10conf
*conf
= mddev
->private;
4096 struct r10bio
*r10_bio
;
4097 sector_t next
, safe
, last
;
4101 struct md_rdev
*rdev
;
4104 struct bio
*bio
, *read_bio
;
4105 int sectors_done
= 0;
4107 if (sector_nr
== 0) {
4108 /* If restarting in the middle, skip the initial sectors */
4109 if (mddev
->reshape_backwards
&&
4110 conf
->reshape_progress
< raid10_size(mddev
, 0, 0)) {
4111 sector_nr
= (raid10_size(mddev
, 0, 0)
4112 - conf
->reshape_progress
);
4113 } else if (!mddev
->reshape_backwards
&&
4114 conf
->reshape_progress
> 0)
4115 sector_nr
= conf
->reshape_progress
;
4117 mddev
->curr_resync_completed
= sector_nr
;
4118 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4124 /* We don't use sector_nr to track where we are up to
4125 * as that doesn't work well for ->reshape_backwards.
4126 * So just use ->reshape_progress.
4128 if (mddev
->reshape_backwards
) {
4129 /* 'next' is the earliest device address that we might
4130 * write to for this chunk in the new layout
4132 next
= first_dev_address(conf
->reshape_progress
- 1,
4135 /* 'safe' is the last device address that we might read from
4136 * in the old layout after a restart
4138 safe
= last_dev_address(conf
->reshape_safe
- 1,
4141 if (next
+ conf
->offset_diff
< safe
)
4144 last
= conf
->reshape_progress
- 1;
4145 sector_nr
= last
& ~(sector_t
)(conf
->geo
.chunk_mask
4146 & conf
->prev
.chunk_mask
);
4147 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 < last
)
4148 sector_nr
= last
+ 1 - RESYNC_BLOCK_SIZE
/512;
4150 /* 'next' is after the last device address that we
4151 * might write to for this chunk in the new layout
4153 next
= last_dev_address(conf
->reshape_progress
, &conf
->geo
);
4155 /* 'safe' is the earliest device address that we might
4156 * read from in the old layout after a restart
4158 safe
= first_dev_address(conf
->reshape_safe
, &conf
->prev
);
4160 /* Need to update metadata if 'next' might be beyond 'safe'
4161 * as that would possibly corrupt data
4163 if (next
> safe
+ conf
->offset_diff
)
4166 sector_nr
= conf
->reshape_progress
;
4167 last
= sector_nr
| (conf
->geo
.chunk_mask
4168 & conf
->prev
.chunk_mask
);
4170 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 <= last
)
4171 last
= sector_nr
+ RESYNC_BLOCK_SIZE
/512 - 1;
4175 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4176 /* Need to update reshape_position in metadata */
4178 mddev
->reshape_position
= conf
->reshape_progress
;
4179 if (mddev
->reshape_backwards
)
4180 mddev
->curr_resync_completed
= raid10_size(mddev
, 0, 0)
4181 - conf
->reshape_progress
;
4183 mddev
->curr_resync_completed
= conf
->reshape_progress
;
4184 conf
->reshape_checkpoint
= jiffies
;
4185 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
4186 md_wakeup_thread(mddev
->thread
);
4187 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
4188 kthread_should_stop());
4189 conf
->reshape_safe
= mddev
->reshape_position
;
4190 allow_barrier(conf
);
4194 /* Now schedule reads for blocks from sector_nr to last */
4195 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
4196 raise_barrier(conf
, sectors_done
!= 0);
4197 atomic_set(&r10_bio
->remaining
, 0);
4198 r10_bio
->mddev
= mddev
;
4199 r10_bio
->sector
= sector_nr
;
4200 set_bit(R10BIO_IsReshape
, &r10_bio
->state
);
4201 r10_bio
->sectors
= last
- sector_nr
+ 1;
4202 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
4203 BUG_ON(!test_bit(R10BIO_Previous
, &r10_bio
->state
));
4206 /* Cannot read from here, so need to record bad blocks
4207 * on all the target devices.
4210 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4211 return sectors_done
;
4214 read_bio
= bio_alloc_mddev(GFP_KERNEL
, RESYNC_PAGES
, mddev
);
4216 read_bio
->bi_bdev
= rdev
->bdev
;
4217 read_bio
->bi_sector
= (r10_bio
->devs
[r10_bio
->read_slot
].addr
4218 + rdev
->data_offset
);
4219 read_bio
->bi_private
= r10_bio
;
4220 read_bio
->bi_end_io
= end_sync_read
;
4221 read_bio
->bi_rw
= READ
;
4222 read_bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
4223 read_bio
->bi_flags
|= 1 << BIO_UPTODATE
;
4224 read_bio
->bi_vcnt
= 0;
4225 read_bio
->bi_idx
= 0;
4226 read_bio
->bi_size
= 0;
4227 r10_bio
->master_bio
= read_bio
;
4228 r10_bio
->read_slot
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
4230 /* Now find the locations in the new layout */
4231 __raid10_find_phys(&conf
->geo
, r10_bio
);
4234 read_bio
->bi_next
= NULL
;
4236 for (s
= 0; s
< conf
->copies
*2; s
++) {
4238 int d
= r10_bio
->devs
[s
/2].devnum
;
4239 struct md_rdev
*rdev2
;
4241 rdev2
= conf
->mirrors
[d
].replacement
;
4242 b
= r10_bio
->devs
[s
/2].repl_bio
;
4244 rdev2
= conf
->mirrors
[d
].rdev
;
4245 b
= r10_bio
->devs
[s
/2].bio
;
4247 if (!rdev2
|| test_bit(Faulty
, &rdev2
->flags
))
4249 b
->bi_bdev
= rdev2
->bdev
;
4250 b
->bi_sector
= r10_bio
->devs
[s
/2].addr
+ rdev2
->new_data_offset
;
4251 b
->bi_private
= r10_bio
;
4252 b
->bi_end_io
= end_reshape_write
;
4254 b
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
4255 b
->bi_flags
|= 1 << BIO_UPTODATE
;
4263 /* Now add as many pages as possible to all of these bios. */
4266 for (s
= 0 ; s
< max_sectors
; s
+= PAGE_SIZE
>> 9) {
4267 struct page
*page
= r10_bio
->devs
[0].bio
->bi_io_vec
[s
/(PAGE_SIZE
>>9)].bv_page
;
4268 int len
= (max_sectors
- s
) << 9;
4269 if (len
> PAGE_SIZE
)
4271 for (bio
= blist
; bio
; bio
= bio
->bi_next
) {
4273 if (bio_add_page(bio
, page
, len
, 0))
4276 /* Didn't fit, must stop */
4278 bio2
&& bio2
!= bio
;
4279 bio2
= bio2
->bi_next
) {
4280 /* Remove last page from this bio */
4282 bio2
->bi_size
-= len
;
4283 bio2
->bi_flags
&= ~(1<<BIO_SEG_VALID
);
4287 sector_nr
+= len
>> 9;
4288 nr_sectors
+= len
>> 9;
4291 r10_bio
->sectors
= nr_sectors
;
4293 /* Now submit the read */
4294 md_sync_acct(read_bio
->bi_bdev
, r10_bio
->sectors
);
4295 atomic_inc(&r10_bio
->remaining
);
4296 read_bio
->bi_next
= NULL
;
4297 generic_make_request(read_bio
);
4298 sector_nr
+= nr_sectors
;
4299 sectors_done
+= nr_sectors
;
4300 if (sector_nr
<= last
)
4303 /* Now that we have done the whole section we can
4304 * update reshape_progress
4306 if (mddev
->reshape_backwards
)
4307 conf
->reshape_progress
-= sectors_done
;
4309 conf
->reshape_progress
+= sectors_done
;
4311 return sectors_done
;
4314 static void end_reshape_request(struct r10bio
*r10_bio
);
4315 static int handle_reshape_read_error(struct mddev
*mddev
,
4316 struct r10bio
*r10_bio
);
4317 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
4319 /* Reshape read completed. Hopefully we have a block
4321 * If we got a read error then we do sync 1-page reads from
4322 * elsewhere until we find the data - or give up.
4324 struct r10conf
*conf
= mddev
->private;
4327 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
4328 if (handle_reshape_read_error(mddev
, r10_bio
) < 0) {
4329 /* Reshape has been aborted */
4330 md_done_sync(mddev
, r10_bio
->sectors
, 0);
4334 /* We definitely have the data in the pages, schedule the
4337 atomic_set(&r10_bio
->remaining
, 1);
4338 for (s
= 0; s
< conf
->copies
*2; s
++) {
4340 int d
= r10_bio
->devs
[s
/2].devnum
;
4341 struct md_rdev
*rdev
;
4343 rdev
= conf
->mirrors
[d
].replacement
;
4344 b
= r10_bio
->devs
[s
/2].repl_bio
;
4346 rdev
= conf
->mirrors
[d
].rdev
;
4347 b
= r10_bio
->devs
[s
/2].bio
;
4349 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4351 atomic_inc(&rdev
->nr_pending
);
4352 md_sync_acct(b
->bi_bdev
, r10_bio
->sectors
);
4353 atomic_inc(&r10_bio
->remaining
);
4355 generic_make_request(b
);
4357 end_reshape_request(r10_bio
);
4360 static void end_reshape(struct r10conf
*conf
)
4362 if (test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
))
4365 spin_lock_irq(&conf
->device_lock
);
4366 conf
->prev
= conf
->geo
;
4367 md_finish_reshape(conf
->mddev
);
4369 conf
->reshape_progress
= MaxSector
;
4370 spin_unlock_irq(&conf
->device_lock
);
4372 /* read-ahead size must cover two whole stripes, which is
4373 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4375 if (conf
->mddev
->queue
) {
4376 int stripe
= conf
->geo
.raid_disks
*
4377 ((conf
->mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
4378 stripe
/= conf
->geo
.near_copies
;
4379 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
4380 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
4386 static int handle_reshape_read_error(struct mddev
*mddev
,
4387 struct r10bio
*r10_bio
)
4389 /* Use sync reads to get the blocks from somewhere else */
4390 int sectors
= r10_bio
->sectors
;
4392 struct r10conf
*conf
= mddev
->private;
4395 struct bio_vec
*bvec
= r10_bio
->master_bio
->bi_io_vec
;
4397 r10b
.sector
= r10_bio
->sector
;
4398 __raid10_find_phys(&conf
->prev
, &r10b
);
4403 int first_slot
= slot
;
4405 if (s
> (PAGE_SIZE
>> 9))
4409 int d
= r10b
.devs
[slot
].devnum
;
4410 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
4413 test_bit(Faulty
, &rdev
->flags
) ||
4414 !test_bit(In_sync
, &rdev
->flags
))
4417 addr
= r10b
.devs
[slot
].addr
+ idx
* PAGE_SIZE
;
4418 success
= sync_page_io(rdev
,
4427 if (slot
>= conf
->copies
)
4429 if (slot
== first_slot
)
4433 /* couldn't read this block, must give up */
4434 set_bit(MD_RECOVERY_INTR
,
4444 static void end_reshape_write(struct bio
*bio
, int error
)
4446 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4447 struct r10bio
*r10_bio
= bio
->bi_private
;
4448 struct mddev
*mddev
= r10_bio
->mddev
;
4449 struct r10conf
*conf
= mddev
->private;
4453 struct md_rdev
*rdev
= NULL
;
4455 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
4457 rdev
= conf
->mirrors
[d
].replacement
;
4460 rdev
= conf
->mirrors
[d
].rdev
;
4464 /* FIXME should record badblock */
4465 md_error(mddev
, rdev
);
4468 rdev_dec_pending(rdev
, mddev
);
4469 end_reshape_request(r10_bio
);
4472 static void end_reshape_request(struct r10bio
*r10_bio
)
4474 if (!atomic_dec_and_test(&r10_bio
->remaining
))
4476 md_done_sync(r10_bio
->mddev
, r10_bio
->sectors
, 1);
4477 bio_put(r10_bio
->master_bio
);
4481 static void raid10_finish_reshape(struct mddev
*mddev
)
4483 struct r10conf
*conf
= mddev
->private;
4485 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
4488 if (mddev
->delta_disks
> 0) {
4489 sector_t size
= raid10_size(mddev
, 0, 0);
4490 md_set_array_sectors(mddev
, size
);
4491 if (mddev
->recovery_cp
> mddev
->resync_max_sectors
) {
4492 mddev
->recovery_cp
= mddev
->resync_max_sectors
;
4493 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4495 mddev
->resync_max_sectors
= size
;
4496 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4497 revalidate_disk(mddev
->gendisk
);
4500 for (d
= conf
->geo
.raid_disks
;
4501 d
< conf
->geo
.raid_disks
- mddev
->delta_disks
;
4503 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
4505 clear_bit(In_sync
, &rdev
->flags
);
4506 rdev
= conf
->mirrors
[d
].replacement
;
4508 clear_bit(In_sync
, &rdev
->flags
);
4511 mddev
->layout
= mddev
->new_layout
;
4512 mddev
->chunk_sectors
= 1 << conf
->geo
.chunk_shift
;
4513 mddev
->reshape_position
= MaxSector
;
4514 mddev
->delta_disks
= 0;
4515 mddev
->reshape_backwards
= 0;
4518 static struct md_personality raid10_personality
=
4522 .owner
= THIS_MODULE
,
4523 .make_request
= make_request
,
4527 .error_handler
= error
,
4528 .hot_add_disk
= raid10_add_disk
,
4529 .hot_remove_disk
= raid10_remove_disk
,
4530 .spare_active
= raid10_spare_active
,
4531 .sync_request
= sync_request
,
4532 .quiesce
= raid10_quiesce
,
4533 .size
= raid10_size
,
4534 .resize
= raid10_resize
,
4535 .takeover
= raid10_takeover
,
4536 .check_reshape
= raid10_check_reshape
,
4537 .start_reshape
= raid10_start_reshape
,
4538 .finish_reshape
= raid10_finish_reshape
,
4541 static int __init
raid_init(void)
4543 return register_md_personality(&raid10_personality
);
4546 static void raid_exit(void)
4548 unregister_md_personality(&raid10_personality
);
4551 module_init(raid_init
);
4552 module_exit(raid_exit
);
4553 MODULE_LICENSE("GPL");
4554 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4555 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4556 MODULE_ALIAS("md-raid10");
4557 MODULE_ALIAS("md-level-10");
4559 module_param(max_queued_requests
, int, S_IRUGO
|S_IWUSR
);