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/seq_file.h>
25 #include <linux/ratelimit.h>
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
38 * far_offset (stored in bit 16 of layout )
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
46 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
48 * near_copies and far_copies must be at least one, and their product is at most
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
57 * Number of guaranteed r10bios in case of extreme VM load:
59 #define NR_RAID10_BIOS 256
61 static void allow_barrier(conf_t
*conf
);
62 static void lower_barrier(conf_t
*conf
);
64 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
67 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
70 return kzalloc(size
, gfp_flags
);
73 static void r10bio_pool_free(void *r10_bio
, void *data
)
78 /* Maximum size of each resync request */
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
81 /* amount of memory to reserve for resync requests */
82 #define RESYNC_WINDOW (1024*1024)
83 /* maximum number of concurrent requests, memory permitting */
84 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
93 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
102 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
106 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
107 nalloc
= conf
->copies
; /* resync */
109 nalloc
= 2; /* recovery */
114 for (j
= nalloc
; j
-- ; ) {
115 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
118 r10_bio
->devs
[j
].bio
= bio
;
121 * Allocate RESYNC_PAGES data pages and attach them
124 for (j
= 0 ; j
< nalloc
; j
++) {
125 bio
= r10_bio
->devs
[j
].bio
;
126 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
127 if (j
== 1 && !test_bit(MD_RECOVERY_SYNC
,
128 &conf
->mddev
->recovery
)) {
129 /* we can share bv_page's during recovery */
130 struct bio
*rbio
= r10_bio
->devs
[0].bio
;
131 page
= rbio
->bi_io_vec
[i
].bv_page
;
134 page
= alloc_page(gfp_flags
);
138 bio
->bi_io_vec
[i
].bv_page
= page
;
146 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
148 for (i
= 0; i
< RESYNC_PAGES
; i
++)
149 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
152 while ( ++j
< nalloc
)
153 bio_put(r10_bio
->devs
[j
].bio
);
154 r10bio_pool_free(r10_bio
, conf
);
158 static void r10buf_pool_free(void *__r10_bio
, void *data
)
162 r10bio_t
*r10bio
= __r10_bio
;
165 for (j
=0; j
< conf
->copies
; j
++) {
166 struct bio
*bio
= r10bio
->devs
[j
].bio
;
168 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
169 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
170 bio
->bi_io_vec
[i
].bv_page
= NULL
;
175 r10bio_pool_free(r10bio
, conf
);
178 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
182 for (i
= 0; i
< conf
->copies
; i
++) {
183 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
184 if (*bio
&& *bio
!= IO_BLOCKED
)
190 static void free_r10bio(r10bio_t
*r10_bio
)
192 conf_t
*conf
= r10_bio
->mddev
->private;
194 put_all_bios(conf
, r10_bio
);
195 mempool_free(r10_bio
, conf
->r10bio_pool
);
198 static void put_buf(r10bio_t
*r10_bio
)
200 conf_t
*conf
= r10_bio
->mddev
->private;
202 mempool_free(r10_bio
, conf
->r10buf_pool
);
207 static void reschedule_retry(r10bio_t
*r10_bio
)
210 mddev_t
*mddev
= r10_bio
->mddev
;
211 conf_t
*conf
= mddev
->private;
213 spin_lock_irqsave(&conf
->device_lock
, flags
);
214 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
216 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
218 /* wake up frozen array... */
219 wake_up(&conf
->wait_barrier
);
221 md_wakeup_thread(mddev
->thread
);
225 * raid_end_bio_io() is called when we have finished servicing a mirrored
226 * operation and are ready to return a success/failure code to the buffer
229 static void raid_end_bio_io(r10bio_t
*r10_bio
)
231 struct bio
*bio
= r10_bio
->master_bio
;
233 conf_t
*conf
= r10_bio
->mddev
->private;
235 if (bio
->bi_phys_segments
) {
237 spin_lock_irqsave(&conf
->device_lock
, flags
);
238 bio
->bi_phys_segments
--;
239 done
= (bio
->bi_phys_segments
== 0);
240 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
243 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
244 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
248 * Wake up any possible resync thread that waits for the device
253 free_r10bio(r10_bio
);
257 * Update disk head position estimator based on IRQ completion info.
259 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
261 conf_t
*conf
= r10_bio
->mddev
->private;
263 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
264 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
268 * Find the disk number which triggered given bio
270 static int find_bio_disk(conf_t
*conf
, r10bio_t
*r10_bio
, struct bio
*bio
)
274 for (slot
= 0; slot
< conf
->copies
; slot
++)
275 if (r10_bio
->devs
[slot
].bio
== bio
)
278 BUG_ON(slot
== conf
->copies
);
279 update_head_pos(slot
, r10_bio
);
281 return r10_bio
->devs
[slot
].devnum
;
284 static void raid10_end_read_request(struct bio
*bio
, int error
)
286 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
287 r10bio_t
*r10_bio
= bio
->bi_private
;
289 conf_t
*conf
= r10_bio
->mddev
->private;
292 slot
= r10_bio
->read_slot
;
293 dev
= r10_bio
->devs
[slot
].devnum
;
295 * this branch is our 'one mirror IO has finished' event handler:
297 update_head_pos(slot
, r10_bio
);
301 * Set R10BIO_Uptodate in our master bio, so that
302 * we will return a good error code to the higher
303 * levels even if IO on some other mirrored buffer fails.
305 * The 'master' represents the composite IO operation to
306 * user-side. So if something waits for IO, then it will
307 * wait for the 'master' bio.
309 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
310 raid_end_bio_io(r10_bio
);
311 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
314 * oops, read error - keep the refcount on the rdev
316 char b
[BDEVNAME_SIZE
];
317 printk_ratelimited(KERN_ERR
318 "md/raid10:%s: %s: rescheduling sector %llu\n",
320 bdevname(conf
->mirrors
[dev
].rdev
->bdev
, b
),
321 (unsigned long long)r10_bio
->sector
);
322 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
323 reschedule_retry(r10_bio
);
327 static void raid10_end_write_request(struct bio
*bio
, int error
)
329 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
330 r10bio_t
*r10_bio
= bio
->bi_private
;
332 conf_t
*conf
= r10_bio
->mddev
->private;
334 dev
= find_bio_disk(conf
, r10_bio
, bio
);
337 * this branch is our 'one mirror IO has finished' event handler:
340 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
341 /* an I/O failed, we can't clear the bitmap */
342 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
345 * Set R10BIO_Uptodate in our master bio, so that
346 * we will return a good error code for to the higher
347 * levels even if IO on some other mirrored buffer fails.
349 * The 'master' represents the composite IO operation to
350 * user-side. So if something waits for IO, then it will
351 * wait for the 'master' bio.
353 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
357 * Let's see if all mirrored write operations have finished
360 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
361 /* clear the bitmap if all writes complete successfully */
362 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
364 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
366 md_write_end(r10_bio
->mddev
);
367 raid_end_bio_io(r10_bio
);
370 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
375 * RAID10 layout manager
376 * As well as the chunksize and raid_disks count, there are two
377 * parameters: near_copies and far_copies.
378 * near_copies * far_copies must be <= raid_disks.
379 * Normally one of these will be 1.
380 * If both are 1, we get raid0.
381 * If near_copies == raid_disks, we get raid1.
383 * Chunks are laid out in raid0 style with near_copies copies of the
384 * first chunk, followed by near_copies copies of the next chunk and
386 * If far_copies > 1, then after 1/far_copies of the array has been assigned
387 * as described above, we start again with a device offset of near_copies.
388 * So we effectively have another copy of the whole array further down all
389 * the drives, but with blocks on different drives.
390 * With this layout, and block is never stored twice on the one device.
392 * raid10_find_phys finds the sector offset of a given virtual sector
393 * on each device that it is on.
395 * raid10_find_virt does the reverse mapping, from a device and a
396 * sector offset to a virtual address
399 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
409 /* now calculate first sector/dev */
410 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
411 sector
= r10bio
->sector
& conf
->chunk_mask
;
413 chunk
*= conf
->near_copies
;
415 dev
= sector_div(stripe
, conf
->raid_disks
);
416 if (conf
->far_offset
)
417 stripe
*= conf
->far_copies
;
419 sector
+= stripe
<< conf
->chunk_shift
;
421 /* and calculate all the others */
422 for (n
=0; n
< conf
->near_copies
; n
++) {
425 r10bio
->devs
[slot
].addr
= sector
;
426 r10bio
->devs
[slot
].devnum
= d
;
429 for (f
= 1; f
< conf
->far_copies
; f
++) {
430 d
+= conf
->near_copies
;
431 if (d
>= conf
->raid_disks
)
432 d
-= conf
->raid_disks
;
434 r10bio
->devs
[slot
].devnum
= d
;
435 r10bio
->devs
[slot
].addr
= s
;
439 if (dev
>= conf
->raid_disks
) {
441 sector
+= (conf
->chunk_mask
+ 1);
444 BUG_ON(slot
!= conf
->copies
);
447 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
449 sector_t offset
, chunk
, vchunk
;
451 offset
= sector
& conf
->chunk_mask
;
452 if (conf
->far_offset
) {
454 chunk
= sector
>> conf
->chunk_shift
;
455 fc
= sector_div(chunk
, conf
->far_copies
);
456 dev
-= fc
* conf
->near_copies
;
458 dev
+= conf
->raid_disks
;
460 while (sector
>= conf
->stride
) {
461 sector
-= conf
->stride
;
462 if (dev
< conf
->near_copies
)
463 dev
+= conf
->raid_disks
- conf
->near_copies
;
465 dev
-= conf
->near_copies
;
467 chunk
= sector
>> conf
->chunk_shift
;
469 vchunk
= chunk
* conf
->raid_disks
+ dev
;
470 sector_div(vchunk
, conf
->near_copies
);
471 return (vchunk
<< conf
->chunk_shift
) + offset
;
475 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
477 * @bvm: properties of new bio
478 * @biovec: the request that could be merged to it.
480 * Return amount of bytes we can accept at this offset
481 * If near_copies == raid_disk, there are no striping issues,
482 * but in that case, the function isn't called at all.
484 static int raid10_mergeable_bvec(struct request_queue
*q
,
485 struct bvec_merge_data
*bvm
,
486 struct bio_vec
*biovec
)
488 mddev_t
*mddev
= q
->queuedata
;
489 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
491 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
492 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
494 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
495 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
496 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
497 return biovec
->bv_len
;
503 * This routine returns the disk from which the requested read should
504 * be done. There is a per-array 'next expected sequential IO' sector
505 * number - if this matches on the next IO then we use the last disk.
506 * There is also a per-disk 'last know head position' sector that is
507 * maintained from IRQ contexts, both the normal and the resync IO
508 * completion handlers update this position correctly. If there is no
509 * perfect sequential match then we pick the disk whose head is closest.
511 * If there are 2 mirrors in the same 2 devices, performance degrades
512 * because position is mirror, not device based.
514 * The rdev for the device selected will have nr_pending incremented.
518 * FIXME: possibly should rethink readbalancing and do it differently
519 * depending on near_copies / far_copies geometry.
521 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
, int *max_sectors
)
523 const sector_t this_sector
= r10_bio
->sector
;
525 int sectors
= r10_bio
->sectors
;
526 int best_good_sectors
;
527 sector_t new_distance
, best_dist
;
532 raid10_find_phys(conf
, r10_bio
);
535 sectors
= r10_bio
->sectors
;
537 best_dist
= MaxSector
;
538 best_good_sectors
= 0;
541 * Check if we can balance. We can balance on the whole
542 * device if no resync is going on (recovery is ok), or below
543 * the resync window. We take the first readable disk when
544 * above the resync window.
546 if (conf
->mddev
->recovery_cp
< MaxSector
547 && (this_sector
+ sectors
>= conf
->next_resync
))
550 for (slot
= 0; slot
< conf
->copies
; slot
++) {
555 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
557 disk
= r10_bio
->devs
[slot
].devnum
;
558 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
561 if (!test_bit(In_sync
, &rdev
->flags
))
564 dev_sector
= r10_bio
->devs
[slot
].addr
;
565 if (is_badblock(rdev
, dev_sector
, sectors
,
566 &first_bad
, &bad_sectors
)) {
567 if (best_dist
< MaxSector
)
568 /* Already have a better slot */
570 if (first_bad
<= dev_sector
) {
571 /* Cannot read here. If this is the
572 * 'primary' device, then we must not read
573 * beyond 'bad_sectors' from another device.
575 bad_sectors
-= (dev_sector
- first_bad
);
576 if (!do_balance
&& sectors
> bad_sectors
)
577 sectors
= bad_sectors
;
578 if (best_good_sectors
> sectors
)
579 best_good_sectors
= sectors
;
581 sector_t good_sectors
=
582 first_bad
- dev_sector
;
583 if (good_sectors
> best_good_sectors
) {
584 best_good_sectors
= good_sectors
;
588 /* Must read from here */
593 best_good_sectors
= sectors
;
598 /* This optimisation is debatable, and completely destroys
599 * sequential read speed for 'far copies' arrays. So only
600 * keep it for 'near' arrays, and review those later.
602 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
605 /* for far > 1 always use the lowest address */
606 if (conf
->far_copies
> 1)
607 new_distance
= r10_bio
->devs
[slot
].addr
;
609 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
610 conf
->mirrors
[disk
].head_position
);
611 if (new_distance
< best_dist
) {
612 best_dist
= new_distance
;
616 if (slot
== conf
->copies
)
620 disk
= r10_bio
->devs
[slot
].devnum
;
621 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
624 atomic_inc(&rdev
->nr_pending
);
625 if (test_bit(Faulty
, &rdev
->flags
)) {
626 /* Cannot risk returning a device that failed
627 * before we inc'ed nr_pending
629 rdev_dec_pending(rdev
, conf
->mddev
);
632 r10_bio
->read_slot
= slot
;
636 *max_sectors
= best_good_sectors
;
641 static int raid10_congested(void *data
, int bits
)
643 mddev_t
*mddev
= data
;
644 conf_t
*conf
= mddev
->private;
647 if (mddev_congested(mddev
, bits
))
650 for (i
= 0; i
< conf
->raid_disks
&& ret
== 0; i
++) {
651 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
652 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
653 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
655 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
662 static void flush_pending_writes(conf_t
*conf
)
664 /* Any writes that have been queued but are awaiting
665 * bitmap updates get flushed here.
667 spin_lock_irq(&conf
->device_lock
);
669 if (conf
->pending_bio_list
.head
) {
671 bio
= bio_list_get(&conf
->pending_bio_list
);
672 spin_unlock_irq(&conf
->device_lock
);
673 /* flush any pending bitmap writes to disk
674 * before proceeding w/ I/O */
675 bitmap_unplug(conf
->mddev
->bitmap
);
677 while (bio
) { /* submit pending writes */
678 struct bio
*next
= bio
->bi_next
;
680 generic_make_request(bio
);
684 spin_unlock_irq(&conf
->device_lock
);
688 * Sometimes we need to suspend IO while we do something else,
689 * either some resync/recovery, or reconfigure the array.
690 * To do this we raise a 'barrier'.
691 * The 'barrier' is a counter that can be raised multiple times
692 * to count how many activities are happening which preclude
694 * We can only raise the barrier if there is no pending IO.
695 * i.e. if nr_pending == 0.
696 * We choose only to raise the barrier if no-one is waiting for the
697 * barrier to go down. This means that as soon as an IO request
698 * is ready, no other operations which require a barrier will start
699 * until the IO request has had a chance.
701 * So: regular IO calls 'wait_barrier'. When that returns there
702 * is no backgroup IO happening, It must arrange to call
703 * allow_barrier when it has finished its IO.
704 * backgroup IO calls must call raise_barrier. Once that returns
705 * there is no normal IO happeing. It must arrange to call
706 * lower_barrier when the particular background IO completes.
709 static void raise_barrier(conf_t
*conf
, int force
)
711 BUG_ON(force
&& !conf
->barrier
);
712 spin_lock_irq(&conf
->resync_lock
);
714 /* Wait until no block IO is waiting (unless 'force') */
715 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
716 conf
->resync_lock
, );
718 /* block any new IO from starting */
721 /* Now wait for all pending IO to complete */
722 wait_event_lock_irq(conf
->wait_barrier
,
723 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
724 conf
->resync_lock
, );
726 spin_unlock_irq(&conf
->resync_lock
);
729 static void lower_barrier(conf_t
*conf
)
732 spin_lock_irqsave(&conf
->resync_lock
, flags
);
734 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
735 wake_up(&conf
->wait_barrier
);
738 static void wait_barrier(conf_t
*conf
)
740 spin_lock_irq(&conf
->resync_lock
);
743 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
749 spin_unlock_irq(&conf
->resync_lock
);
752 static void allow_barrier(conf_t
*conf
)
755 spin_lock_irqsave(&conf
->resync_lock
, flags
);
757 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
758 wake_up(&conf
->wait_barrier
);
761 static void freeze_array(conf_t
*conf
)
763 /* stop syncio and normal IO and wait for everything to
765 * We increment barrier and nr_waiting, and then
766 * wait until nr_pending match nr_queued+1
767 * This is called in the context of one normal IO request
768 * that has failed. Thus any sync request that might be pending
769 * will be blocked by nr_pending, and we need to wait for
770 * pending IO requests to complete or be queued for re-try.
771 * Thus the number queued (nr_queued) plus this request (1)
772 * must match the number of pending IOs (nr_pending) before
775 spin_lock_irq(&conf
->resync_lock
);
778 wait_event_lock_irq(conf
->wait_barrier
,
779 conf
->nr_pending
== conf
->nr_queued
+1,
781 flush_pending_writes(conf
));
783 spin_unlock_irq(&conf
->resync_lock
);
786 static void unfreeze_array(conf_t
*conf
)
788 /* reverse the effect of the freeze */
789 spin_lock_irq(&conf
->resync_lock
);
792 wake_up(&conf
->wait_barrier
);
793 spin_unlock_irq(&conf
->resync_lock
);
796 static int make_request(mddev_t
*mddev
, struct bio
* bio
)
798 conf_t
*conf
= mddev
->private;
799 mirror_info_t
*mirror
;
801 struct bio
*read_bio
;
803 int chunk_sects
= conf
->chunk_mask
+ 1;
804 const int rw
= bio_data_dir(bio
);
805 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
806 const unsigned long do_fua
= (bio
->bi_rw
& REQ_FUA
);
808 mdk_rdev_t
*blocked_rdev
;
811 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
812 md_flush_request(mddev
, bio
);
816 /* If this request crosses a chunk boundary, we need to
817 * split it. This will only happen for 1 PAGE (or less) requests.
819 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
821 conf
->near_copies
< conf
->raid_disks
)) {
823 /* Sanity check -- queue functions should prevent this happening */
824 if (bio
->bi_vcnt
!= 1 ||
827 /* This is a one page bio that upper layers
828 * refuse to split for us, so we need to split it.
831 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
833 /* Each of these 'make_request' calls will call 'wait_barrier'.
834 * If the first succeeds but the second blocks due to the resync
835 * thread raising the barrier, we will deadlock because the
836 * IO to the underlying device will be queued in generic_make_request
837 * and will never complete, so will never reduce nr_pending.
838 * So increment nr_waiting here so no new raise_barriers will
839 * succeed, and so the second wait_barrier cannot block.
841 spin_lock_irq(&conf
->resync_lock
);
843 spin_unlock_irq(&conf
->resync_lock
);
845 if (make_request(mddev
, &bp
->bio1
))
846 generic_make_request(&bp
->bio1
);
847 if (make_request(mddev
, &bp
->bio2
))
848 generic_make_request(&bp
->bio2
);
850 spin_lock_irq(&conf
->resync_lock
);
852 wake_up(&conf
->wait_barrier
);
853 spin_unlock_irq(&conf
->resync_lock
);
855 bio_pair_release(bp
);
858 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
859 " or bigger than %dk %llu %d\n", mdname(mddev
), chunk_sects
/2,
860 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
866 md_write_start(mddev
, bio
);
869 * Register the new request and wait if the reconstruction
870 * thread has put up a bar for new requests.
871 * Continue immediately if no resync is active currently.
875 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
877 r10_bio
->master_bio
= bio
;
878 r10_bio
->sectors
= bio
->bi_size
>> 9;
880 r10_bio
->mddev
= mddev
;
881 r10_bio
->sector
= bio
->bi_sector
;
884 /* We might need to issue multiple reads to different
885 * devices if there are bad blocks around, so we keep
886 * track of the number of reads in bio->bi_phys_segments.
887 * If this is 0, there is only one r10_bio and no locking
888 * will be needed when the request completes. If it is
889 * non-zero, then it is the number of not-completed requests.
891 bio
->bi_phys_segments
= 0;
892 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
896 * read balancing logic:
903 disk
= read_balance(conf
, r10_bio
, &max_sectors
);
904 slot
= r10_bio
->read_slot
;
906 raid_end_bio_io(r10_bio
);
909 mirror
= conf
->mirrors
+ disk
;
911 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
912 md_trim_bio(read_bio
, r10_bio
->sector
- bio
->bi_sector
,
915 r10_bio
->devs
[slot
].bio
= read_bio
;
917 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
918 mirror
->rdev
->data_offset
;
919 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
920 read_bio
->bi_end_io
= raid10_end_read_request
;
921 read_bio
->bi_rw
= READ
| do_sync
;
922 read_bio
->bi_private
= r10_bio
;
924 if (max_sectors
< r10_bio
->sectors
) {
925 /* Could not read all from this device, so we will
926 * need another r10_bio.
930 sectors_handled
= (r10_bio
->sectors
+ max_sectors
932 r10_bio
->sectors
= max_sectors
;
933 spin_lock_irq(&conf
->device_lock
);
934 if (bio
->bi_phys_segments
== 0)
935 bio
->bi_phys_segments
= 2;
937 bio
->bi_phys_segments
++;
938 spin_unlock(&conf
->device_lock
);
939 /* Cannot call generic_make_request directly
940 * as that will be queued in __generic_make_request
941 * and subsequent mempool_alloc might block
942 * waiting for it. so hand bio over to raid10d.
944 reschedule_retry(r10_bio
);
946 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
948 r10_bio
->master_bio
= bio
;
949 r10_bio
->sectors
= ((bio
->bi_size
>> 9)
952 r10_bio
->mddev
= mddev
;
953 r10_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
956 generic_make_request(read_bio
);
963 /* first select target devices under rcu_lock and
964 * inc refcount on their rdev. Record them by setting
967 plugged
= mddev_check_plugged(mddev
);
969 raid10_find_phys(conf
, r10_bio
);
973 for (i
= 0; i
< conf
->copies
; i
++) {
974 int d
= r10_bio
->devs
[i
].devnum
;
975 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
976 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
977 atomic_inc(&rdev
->nr_pending
);
981 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
982 atomic_inc(&rdev
->nr_pending
);
983 r10_bio
->devs
[i
].bio
= bio
;
985 r10_bio
->devs
[i
].bio
= NULL
;
986 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
991 if (unlikely(blocked_rdev
)) {
992 /* Have to wait for this device to get unblocked, then retry */
996 for (j
= 0; j
< i
; j
++)
997 if (r10_bio
->devs
[j
].bio
) {
998 d
= r10_bio
->devs
[j
].devnum
;
999 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1001 allow_barrier(conf
);
1002 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1007 atomic_set(&r10_bio
->remaining
, 1);
1008 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
1010 for (i
= 0; i
< conf
->copies
; i
++) {
1012 int d
= r10_bio
->devs
[i
].devnum
;
1013 if (!r10_bio
->devs
[i
].bio
)
1016 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1017 r10_bio
->devs
[i
].bio
= mbio
;
1019 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
1020 conf
->mirrors
[d
].rdev
->data_offset
;
1021 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1022 mbio
->bi_end_io
= raid10_end_write_request
;
1023 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
1024 mbio
->bi_private
= r10_bio
;
1026 atomic_inc(&r10_bio
->remaining
);
1027 spin_lock_irqsave(&conf
->device_lock
, flags
);
1028 bio_list_add(&conf
->pending_bio_list
, mbio
);
1029 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1032 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1033 /* This matches the end of raid10_end_write_request() */
1034 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
1036 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
1038 md_write_end(mddev
);
1039 raid_end_bio_io(r10_bio
);
1042 /* In case raid10d snuck in to freeze_array */
1043 wake_up(&conf
->wait_barrier
);
1045 if (do_sync
|| !mddev
->bitmap
|| !plugged
)
1046 md_wakeup_thread(mddev
->thread
);
1050 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
1052 conf_t
*conf
= mddev
->private;
1055 if (conf
->near_copies
< conf
->raid_disks
)
1056 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1057 if (conf
->near_copies
> 1)
1058 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
1059 if (conf
->far_copies
> 1) {
1060 if (conf
->far_offset
)
1061 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
1063 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
1065 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1066 conf
->raid_disks
- mddev
->degraded
);
1067 for (i
= 0; i
< conf
->raid_disks
; i
++)
1068 seq_printf(seq
, "%s",
1069 conf
->mirrors
[i
].rdev
&&
1070 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
1071 seq_printf(seq
, "]");
1074 /* check if there are enough drives for
1075 * every block to appear on atleast one.
1076 * Don't consider the device numbered 'ignore'
1077 * as we might be about to remove it.
1079 static int enough(conf_t
*conf
, int ignore
)
1084 int n
= conf
->copies
;
1087 if (conf
->mirrors
[first
].rdev
&&
1090 first
= (first
+1) % conf
->raid_disks
;
1094 } while (first
!= 0);
1098 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1100 char b
[BDEVNAME_SIZE
];
1101 conf_t
*conf
= mddev
->private;
1104 * If it is not operational, then we have already marked it as dead
1105 * else if it is the last working disks, ignore the error, let the
1106 * next level up know.
1107 * else mark the drive as failed
1109 if (test_bit(In_sync
, &rdev
->flags
)
1110 && !enough(conf
, rdev
->raid_disk
))
1112 * Don't fail the drive, just return an IO error.
1115 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1116 unsigned long flags
;
1117 spin_lock_irqsave(&conf
->device_lock
, flags
);
1119 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1121 * if recovery is running, make sure it aborts.
1123 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1125 set_bit(Blocked
, &rdev
->flags
);
1126 set_bit(Faulty
, &rdev
->flags
);
1127 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1129 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1130 "md/raid10:%s: Operation continuing on %d devices.\n",
1131 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1132 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1135 static void print_conf(conf_t
*conf
)
1140 printk(KERN_DEBUG
"RAID10 conf printout:\n");
1142 printk(KERN_DEBUG
"(!conf)\n");
1145 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1148 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1149 char b
[BDEVNAME_SIZE
];
1150 tmp
= conf
->mirrors
+ i
;
1152 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1153 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1154 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1155 bdevname(tmp
->rdev
->bdev
,b
));
1159 static void close_sync(conf_t
*conf
)
1162 allow_barrier(conf
);
1164 mempool_destroy(conf
->r10buf_pool
);
1165 conf
->r10buf_pool
= NULL
;
1168 static int raid10_spare_active(mddev_t
*mddev
)
1171 conf_t
*conf
= mddev
->private;
1174 unsigned long flags
;
1177 * Find all non-in_sync disks within the RAID10 configuration
1178 * and mark them in_sync
1180 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1181 tmp
= conf
->mirrors
+ i
;
1183 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1184 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1186 sysfs_notify_dirent(tmp
->rdev
->sysfs_state
);
1189 spin_lock_irqsave(&conf
->device_lock
, flags
);
1190 mddev
->degraded
-= count
;
1191 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1198 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1200 conf_t
*conf
= mddev
->private;
1204 int last
= conf
->raid_disks
- 1;
1206 if (mddev
->recovery_cp
< MaxSector
)
1207 /* only hot-add to in-sync arrays, as recovery is
1208 * very different from resync
1211 if (!enough(conf
, -1))
1214 if (rdev
->raid_disk
>= 0)
1215 first
= last
= rdev
->raid_disk
;
1217 if (rdev
->saved_raid_disk
>= first
&&
1218 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1219 mirror
= rdev
->saved_raid_disk
;
1222 for ( ; mirror
<= last
; mirror
++) {
1223 mirror_info_t
*p
= &conf
->mirrors
[mirror
];
1224 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
1229 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1230 rdev
->data_offset
<< 9);
1231 /* as we don't honour merge_bvec_fn, we must
1232 * never risk violating it, so limit
1233 * ->max_segments to one lying with a single
1234 * page, as a one page request is never in
1237 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1238 blk_queue_max_segments(mddev
->queue
, 1);
1239 blk_queue_segment_boundary(mddev
->queue
,
1240 PAGE_CACHE_SIZE
- 1);
1243 p
->head_position
= 0;
1244 rdev
->raid_disk
= mirror
;
1246 if (rdev
->saved_raid_disk
!= mirror
)
1248 rcu_assign_pointer(p
->rdev
, rdev
);
1252 md_integrity_add_rdev(rdev
, mddev
);
1257 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1259 conf_t
*conf
= mddev
->private;
1262 mirror_info_t
*p
= conf
->mirrors
+ number
;
1267 if (test_bit(In_sync
, &rdev
->flags
) ||
1268 atomic_read(&rdev
->nr_pending
)) {
1272 /* Only remove faulty devices in recovery
1275 if (!test_bit(Faulty
, &rdev
->flags
) &&
1276 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
1283 if (atomic_read(&rdev
->nr_pending
)) {
1284 /* lost the race, try later */
1289 err
= md_integrity_register(mddev
);
1298 static void end_sync_read(struct bio
*bio
, int error
)
1300 r10bio_t
*r10_bio
= bio
->bi_private
;
1301 conf_t
*conf
= r10_bio
->mddev
->private;
1304 d
= find_bio_disk(conf
, r10_bio
, bio
);
1306 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1307 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1309 atomic_add(r10_bio
->sectors
,
1310 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1311 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1312 md_error(r10_bio
->mddev
,
1313 conf
->mirrors
[d
].rdev
);
1316 /* for reconstruct, we always reschedule after a read.
1317 * for resync, only after all reads
1319 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1320 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1321 atomic_dec_and_test(&r10_bio
->remaining
)) {
1322 /* we have read all the blocks,
1323 * do the comparison in process context in raid10d
1325 reschedule_retry(r10_bio
);
1329 static void end_sync_write(struct bio
*bio
, int error
)
1331 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1332 r10bio_t
*r10_bio
= bio
->bi_private
;
1333 mddev_t
*mddev
= r10_bio
->mddev
;
1334 conf_t
*conf
= mddev
->private;
1337 d
= find_bio_disk(conf
, r10_bio
, bio
);
1340 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1342 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1343 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1344 if (r10_bio
->master_bio
== NULL
) {
1345 /* the primary of several recovery bios */
1346 sector_t s
= r10_bio
->sectors
;
1348 md_done_sync(mddev
, s
, 1);
1351 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1359 * Note: sync and recover and handled very differently for raid10
1360 * This code is for resync.
1361 * For resync, we read through virtual addresses and read all blocks.
1362 * If there is any error, we schedule a write. The lowest numbered
1363 * drive is authoritative.
1364 * However requests come for physical address, so we need to map.
1365 * For every physical address there are raid_disks/copies virtual addresses,
1366 * which is always are least one, but is not necessarly an integer.
1367 * This means that a physical address can span multiple chunks, so we may
1368 * have to submit multiple io requests for a single sync request.
1371 * We check if all blocks are in-sync and only write to blocks that
1374 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1376 conf_t
*conf
= mddev
->private;
1378 struct bio
*tbio
, *fbio
;
1380 atomic_set(&r10_bio
->remaining
, 1);
1382 /* find the first device with a block */
1383 for (i
=0; i
<conf
->copies
; i
++)
1384 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1387 if (i
== conf
->copies
)
1391 fbio
= r10_bio
->devs
[i
].bio
;
1393 /* now find blocks with errors */
1394 for (i
=0 ; i
< conf
->copies
; i
++) {
1396 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1398 tbio
= r10_bio
->devs
[i
].bio
;
1400 if (tbio
->bi_end_io
!= end_sync_read
)
1404 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1405 /* We know that the bi_io_vec layout is the same for
1406 * both 'first' and 'i', so we just compare them.
1407 * All vec entries are PAGE_SIZE;
1409 for (j
= 0; j
< vcnt
; j
++)
1410 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1411 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1416 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1418 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1419 /* Don't fix anything. */
1421 /* Ok, we need to write this bio
1422 * First we need to fixup bv_offset, bv_len and
1423 * bi_vecs, as the read request might have corrupted these
1425 tbio
->bi_vcnt
= vcnt
;
1426 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1428 tbio
->bi_phys_segments
= 0;
1429 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1430 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1431 tbio
->bi_next
= NULL
;
1432 tbio
->bi_rw
= WRITE
;
1433 tbio
->bi_private
= r10_bio
;
1434 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1436 for (j
=0; j
< vcnt
; j
++) {
1437 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1438 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1440 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1441 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1444 tbio
->bi_end_io
= end_sync_write
;
1446 d
= r10_bio
->devs
[i
].devnum
;
1447 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1448 atomic_inc(&r10_bio
->remaining
);
1449 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1451 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1452 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1453 generic_make_request(tbio
);
1457 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1458 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1464 * Now for the recovery code.
1465 * Recovery happens across physical sectors.
1466 * We recover all non-is_sync drives by finding the virtual address of
1467 * each, and then choose a working drive that also has that virt address.
1468 * There is a separate r10_bio for each non-in_sync drive.
1469 * Only the first two slots are in use. The first for reading,
1470 * The second for writing.
1474 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1476 conf_t
*conf
= mddev
->private;
1481 * share the pages with the first bio
1482 * and submit the write request
1484 wbio
= r10_bio
->devs
[1].bio
;
1485 d
= r10_bio
->devs
[1].devnum
;
1487 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1488 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1489 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1490 generic_make_request(wbio
);
1493 "md/raid10:%s: recovery aborted due to read error\n",
1495 conf
->mirrors
[d
].recovery_disabled
= mddev
->recovery_disabled
;
1496 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1503 * Used by fix_read_error() to decay the per rdev read_errors.
1504 * We halve the read error count for every hour that has elapsed
1505 * since the last recorded read error.
1508 static void check_decay_read_errors(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1510 struct timespec cur_time_mon
;
1511 unsigned long hours_since_last
;
1512 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
1514 ktime_get_ts(&cur_time_mon
);
1516 if (rdev
->last_read_error
.tv_sec
== 0 &&
1517 rdev
->last_read_error
.tv_nsec
== 0) {
1518 /* first time we've seen a read error */
1519 rdev
->last_read_error
= cur_time_mon
;
1523 hours_since_last
= (cur_time_mon
.tv_sec
-
1524 rdev
->last_read_error
.tv_sec
) / 3600;
1526 rdev
->last_read_error
= cur_time_mon
;
1529 * if hours_since_last is > the number of bits in read_errors
1530 * just set read errors to 0. We do this to avoid
1531 * overflowing the shift of read_errors by hours_since_last.
1533 if (hours_since_last
>= 8 * sizeof(read_errors
))
1534 atomic_set(&rdev
->read_errors
, 0);
1536 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
1540 * This is a kernel thread which:
1542 * 1. Retries failed read operations on working mirrors.
1543 * 2. Updates the raid superblock when problems encounter.
1544 * 3. Performs writes following reads for array synchronising.
1547 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1549 int sect
= 0; /* Offset from r10_bio->sector */
1550 int sectors
= r10_bio
->sectors
;
1552 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
1553 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1555 /* still own a reference to this rdev, so it cannot
1556 * have been cleared recently.
1558 rdev
= conf
->mirrors
[d
].rdev
;
1560 if (test_bit(Faulty
, &rdev
->flags
))
1561 /* drive has already been failed, just ignore any
1562 more fix_read_error() attempts */
1565 check_decay_read_errors(mddev
, rdev
);
1566 atomic_inc(&rdev
->read_errors
);
1567 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
1568 char b
[BDEVNAME_SIZE
];
1569 bdevname(rdev
->bdev
, b
);
1572 "md/raid10:%s: %s: Raid device exceeded "
1573 "read_error threshold [cur %d:max %d]\n",
1575 atomic_read(&rdev
->read_errors
), max_read_errors
);
1577 "md/raid10:%s: %s: Failing raid device\n",
1579 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1585 int sl
= r10_bio
->read_slot
;
1589 if (s
> (PAGE_SIZE
>>9))
1597 d
= r10_bio
->devs
[sl
].devnum
;
1598 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1600 test_bit(In_sync
, &rdev
->flags
) &&
1601 is_badblock(rdev
, r10_bio
->devs
[sl
].addr
+ sect
, s
,
1602 &first_bad
, &bad_sectors
) == 0) {
1603 atomic_inc(&rdev
->nr_pending
);
1605 success
= sync_page_io(rdev
,
1606 r10_bio
->devs
[sl
].addr
+
1609 conf
->tmppage
, READ
, false);
1610 rdev_dec_pending(rdev
, mddev
);
1616 if (sl
== conf
->copies
)
1618 } while (!success
&& sl
!= r10_bio
->read_slot
);
1622 /* Cannot read from anywhere -- bye bye array */
1623 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1624 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1629 /* write it back and re-read */
1631 while (sl
!= r10_bio
->read_slot
) {
1632 char b
[BDEVNAME_SIZE
];
1637 d
= r10_bio
->devs
[sl
].devnum
;
1638 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1640 !test_bit(In_sync
, &rdev
->flags
))
1643 atomic_inc(&rdev
->nr_pending
);
1645 if (sync_page_io(rdev
,
1646 r10_bio
->devs
[sl
].addr
+
1648 s
<<9, conf
->tmppage
, WRITE
, false)
1650 /* Well, this device is dead */
1652 "md/raid10:%s: read correction "
1654 " (%d sectors at %llu on %s)\n",
1656 (unsigned long long)(
1657 sect
+ rdev
->data_offset
),
1658 bdevname(rdev
->bdev
, b
));
1659 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1662 bdevname(rdev
->bdev
, b
));
1663 md_error(mddev
, rdev
);
1665 rdev_dec_pending(rdev
, mddev
);
1669 while (sl
!= r10_bio
->read_slot
) {
1670 char b
[BDEVNAME_SIZE
];
1675 d
= r10_bio
->devs
[sl
].devnum
;
1676 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1678 !test_bit(In_sync
, &rdev
->flags
))
1681 atomic_inc(&rdev
->nr_pending
);
1683 if (sync_page_io(rdev
,
1684 r10_bio
->devs
[sl
].addr
+
1686 s
<<9, conf
->tmppage
,
1687 READ
, false) == 0) {
1688 /* Well, this device is dead */
1690 "md/raid10:%s: unable to read back "
1692 " (%d sectors at %llu on %s)\n",
1694 (unsigned long long)(
1695 sect
+ rdev
->data_offset
),
1696 bdevname(rdev
->bdev
, b
));
1697 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1700 bdevname(rdev
->bdev
, b
));
1702 md_error(mddev
, rdev
);
1705 "md/raid10:%s: read error corrected"
1706 " (%d sectors at %llu on %s)\n",
1708 (unsigned long long)(
1709 sect
+ rdev
->data_offset
),
1710 bdevname(rdev
->bdev
, b
));
1711 atomic_add(s
, &rdev
->corrected_errors
);
1714 rdev_dec_pending(rdev
, mddev
);
1724 static void handle_read_error(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1726 int slot
= r10_bio
->read_slot
;
1727 int mirror
= r10_bio
->devs
[slot
].devnum
;
1729 conf_t
*conf
= mddev
->private;
1731 char b
[BDEVNAME_SIZE
];
1732 unsigned long do_sync
;
1735 /* we got a read error. Maybe the drive is bad. Maybe just
1736 * the block and we can fix it.
1737 * We freeze all other IO, and try reading the block from
1738 * other devices. When we find one, we re-write
1739 * and check it that fixes the read error.
1740 * This is all done synchronously while the array is
1743 if (mddev
->ro
== 0) {
1745 fix_read_error(conf
, mddev
, r10_bio
);
1746 unfreeze_array(conf
);
1748 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, mddev
);
1750 bio
= r10_bio
->devs
[slot
].bio
;
1751 bdevname(bio
->bi_bdev
, b
);
1752 r10_bio
->devs
[slot
].bio
=
1753 mddev
->ro
? IO_BLOCKED
: NULL
;
1755 mirror
= read_balance(conf
, r10_bio
, &max_sectors
);
1757 printk(KERN_ALERT
"md/raid10:%s: %s: unrecoverable I/O"
1758 " read error for block %llu\n",
1760 (unsigned long long)r10_bio
->sector
);
1761 raid_end_bio_io(r10_bio
);
1766 do_sync
= (r10_bio
->master_bio
->bi_rw
& REQ_SYNC
);
1769 slot
= r10_bio
->read_slot
;
1770 rdev
= conf
->mirrors
[mirror
].rdev
;
1773 "md/raid10:%s: %s: redirecting"
1774 "sector %llu to another mirror\n",
1776 bdevname(rdev
->bdev
, b
),
1777 (unsigned long long)r10_bio
->sector
);
1778 bio
= bio_clone_mddev(r10_bio
->master_bio
,
1781 r10_bio
->sector
- bio
->bi_sector
,
1783 r10_bio
->devs
[slot
].bio
= bio
;
1784 bio
->bi_sector
= r10_bio
->devs
[slot
].addr
1785 + rdev
->data_offset
;
1786 bio
->bi_bdev
= rdev
->bdev
;
1787 bio
->bi_rw
= READ
| do_sync
;
1788 bio
->bi_private
= r10_bio
;
1789 bio
->bi_end_io
= raid10_end_read_request
;
1790 if (max_sectors
< r10_bio
->sectors
) {
1791 /* Drat - have to split this up more */
1792 struct bio
*mbio
= r10_bio
->master_bio
;
1793 int sectors_handled
=
1794 r10_bio
->sector
+ max_sectors
1796 r10_bio
->sectors
= max_sectors
;
1797 spin_lock_irq(&conf
->device_lock
);
1798 if (mbio
->bi_phys_segments
== 0)
1799 mbio
->bi_phys_segments
= 2;
1801 mbio
->bi_phys_segments
++;
1802 spin_unlock_irq(&conf
->device_lock
);
1803 generic_make_request(bio
);
1806 r10_bio
= mempool_alloc(conf
->r10bio_pool
,
1808 r10_bio
->master_bio
= mbio
;
1809 r10_bio
->sectors
= (mbio
->bi_size
>> 9)
1812 set_bit(R10BIO_ReadError
,
1814 r10_bio
->mddev
= mddev
;
1815 r10_bio
->sector
= mbio
->bi_sector
1820 generic_make_request(bio
);
1823 static void raid10d(mddev_t
*mddev
)
1826 unsigned long flags
;
1827 conf_t
*conf
= mddev
->private;
1828 struct list_head
*head
= &conf
->retry_list
;
1829 struct blk_plug plug
;
1831 md_check_recovery(mddev
);
1833 blk_start_plug(&plug
);
1836 flush_pending_writes(conf
);
1838 spin_lock_irqsave(&conf
->device_lock
, flags
);
1839 if (list_empty(head
)) {
1840 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1843 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1844 list_del(head
->prev
);
1846 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1848 mddev
= r10_bio
->mddev
;
1849 conf
= mddev
->private;
1850 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
1851 sync_request_write(mddev
, r10_bio
);
1852 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
1853 recovery_request_write(mddev
, r10_bio
);
1854 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
1855 handle_read_error(mddev
, r10_bio
);
1857 /* just a partial read to be scheduled from a
1860 int slot
= r10_bio
->read_slot
;
1861 generic_make_request(r10_bio
->devs
[slot
].bio
);
1865 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
1866 md_check_recovery(mddev
);
1868 blk_finish_plug(&plug
);
1872 static int init_resync(conf_t
*conf
)
1876 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1877 BUG_ON(conf
->r10buf_pool
);
1878 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1879 if (!conf
->r10buf_pool
)
1881 conf
->next_resync
= 0;
1886 * perform a "sync" on one "block"
1888 * We need to make sure that no normal I/O request - particularly write
1889 * requests - conflict with active sync requests.
1891 * This is achieved by tracking pending requests and a 'barrier' concept
1892 * that can be installed to exclude normal IO requests.
1894 * Resync and recovery are handled very differently.
1895 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1897 * For resync, we iterate over virtual addresses, read all copies,
1898 * and update if there are differences. If only one copy is live,
1900 * For recovery, we iterate over physical addresses, read a good
1901 * value for each non-in_sync drive, and over-write.
1903 * So, for recovery we may have several outstanding complex requests for a
1904 * given address, one for each out-of-sync device. We model this by allocating
1905 * a number of r10_bio structures, one for each out-of-sync device.
1906 * As we setup these structures, we collect all bio's together into a list
1907 * which we then process collectively to add pages, and then process again
1908 * to pass to generic_make_request.
1910 * The r10_bio structures are linked using a borrowed master_bio pointer.
1911 * This link is counted in ->remaining. When the r10_bio that points to NULL
1912 * has its remaining count decremented to 0, the whole complex operation
1917 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
,
1918 int *skipped
, int go_faster
)
1920 conf_t
*conf
= mddev
->private;
1922 struct bio
*biolist
= NULL
, *bio
;
1923 sector_t max_sector
, nr_sectors
;
1926 sector_t sync_blocks
;
1927 sector_t sectors_skipped
= 0;
1928 int chunks_skipped
= 0;
1930 if (!conf
->r10buf_pool
)
1931 if (init_resync(conf
))
1935 max_sector
= mddev
->dev_sectors
;
1936 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1937 max_sector
= mddev
->resync_max_sectors
;
1938 if (sector_nr
>= max_sector
) {
1939 /* If we aborted, we need to abort the
1940 * sync on the 'current' bitmap chucks (there can
1941 * be several when recovering multiple devices).
1942 * as we may have started syncing it but not finished.
1943 * We can find the current address in
1944 * mddev->curr_resync, but for recovery,
1945 * we need to convert that to several
1946 * virtual addresses.
1948 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1949 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1950 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1952 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1954 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1955 bitmap_end_sync(mddev
->bitmap
, sect
,
1958 } else /* completed sync */
1961 bitmap_close_sync(mddev
->bitmap
);
1964 return sectors_skipped
;
1966 if (chunks_skipped
>= conf
->raid_disks
) {
1967 /* if there has been nothing to do on any drive,
1968 * then there is nothing to do at all..
1971 return (max_sector
- sector_nr
) + sectors_skipped
;
1974 if (max_sector
> mddev
->resync_max
)
1975 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
1977 /* make sure whole request will fit in a chunk - if chunks
1980 if (conf
->near_copies
< conf
->raid_disks
&&
1981 max_sector
> (sector_nr
| conf
->chunk_mask
))
1982 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1984 * If there is non-resync activity waiting for us then
1985 * put in a delay to throttle resync.
1987 if (!go_faster
&& conf
->nr_waiting
)
1988 msleep_interruptible(1000);
1990 /* Again, very different code for resync and recovery.
1991 * Both must result in an r10bio with a list of bios that
1992 * have bi_end_io, bi_sector, bi_bdev set,
1993 * and bi_private set to the r10bio.
1994 * For recovery, we may actually create several r10bios
1995 * with 2 bios in each, that correspond to the bios in the main one.
1996 * In this case, the subordinate r10bios link back through a
1997 * borrowed master_bio pointer, and the counter in the master
1998 * includes a ref from each subordinate.
2000 /* First, we decide what to do and set ->bi_end_io
2001 * To end_sync_read if we want to read, and
2002 * end_sync_write if we will want to write.
2005 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
2006 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2007 /* recovery... the complicated one */
2011 for (i
=0 ; i
<conf
->raid_disks
; i
++) {
2018 if (conf
->mirrors
[i
].rdev
== NULL
||
2019 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
))
2023 /* want to reconstruct this device */
2025 sect
= raid10_find_virt(conf
, sector_nr
, i
);
2026 /* Unless we are doing a full sync, we only need
2027 * to recover the block if it is set in the bitmap
2029 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
2031 if (sync_blocks
< max_sync
)
2032 max_sync
= sync_blocks
;
2035 /* yep, skip the sync_blocks here, but don't assume
2036 * that there will never be anything to do here
2038 chunks_skipped
= -1;
2042 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2043 raise_barrier(conf
, rb2
!= NULL
);
2044 atomic_set(&r10_bio
->remaining
, 0);
2046 r10_bio
->master_bio
= (struct bio
*)rb2
;
2048 atomic_inc(&rb2
->remaining
);
2049 r10_bio
->mddev
= mddev
;
2050 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
2051 r10_bio
->sector
= sect
;
2053 raid10_find_phys(conf
, r10_bio
);
2055 /* Need to check if the array will still be
2058 for (j
=0; j
<conf
->raid_disks
; j
++)
2059 if (conf
->mirrors
[j
].rdev
== NULL
||
2060 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
2065 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
2066 &sync_blocks
, still_degraded
);
2069 for (j
=0; j
<conf
->copies
;j
++) {
2071 int d
= r10_bio
->devs
[j
].devnum
;
2073 sector_t sector
, first_bad
;
2075 if (!conf
->mirrors
[d
].rdev
||
2076 !test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
))
2078 /* This is where we read from */
2080 rdev
= conf
->mirrors
[d
].rdev
;
2081 sector
= r10_bio
->devs
[j
].addr
;
2083 if (is_badblock(rdev
, sector
, max_sync
,
2084 &first_bad
, &bad_sectors
)) {
2085 if (first_bad
> sector
)
2086 max_sync
= first_bad
- sector
;
2088 bad_sectors
-= (sector
2090 if (max_sync
> bad_sectors
)
2091 max_sync
= bad_sectors
;
2095 bio
= r10_bio
->devs
[0].bio
;
2096 bio
->bi_next
= biolist
;
2098 bio
->bi_private
= r10_bio
;
2099 bio
->bi_end_io
= end_sync_read
;
2101 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
2102 conf
->mirrors
[d
].rdev
->data_offset
;
2103 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2104 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2105 atomic_inc(&r10_bio
->remaining
);
2106 /* and we write to 'i' */
2108 for (k
=0; k
<conf
->copies
; k
++)
2109 if (r10_bio
->devs
[k
].devnum
== i
)
2111 BUG_ON(k
== conf
->copies
);
2112 bio
= r10_bio
->devs
[1].bio
;
2113 bio
->bi_next
= biolist
;
2115 bio
->bi_private
= r10_bio
;
2116 bio
->bi_end_io
= end_sync_write
;
2118 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
2119 conf
->mirrors
[i
].rdev
->data_offset
;
2120 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
2122 r10_bio
->devs
[0].devnum
= d
;
2123 r10_bio
->devs
[1].devnum
= i
;
2127 if (j
== conf
->copies
) {
2128 /* Cannot recover, so abort the recovery or
2129 * record a bad block */
2132 atomic_dec(&rb2
->remaining
);
2135 /* problem is that there are bad blocks
2136 * on other device(s)
2139 for (k
= 0; k
< conf
->copies
; k
++)
2140 if (r10_bio
->devs
[k
].devnum
== i
)
2142 if (!rdev_set_badblocks(
2143 conf
->mirrors
[i
].rdev
,
2144 r10_bio
->devs
[k
].addr
,
2149 if (!test_and_set_bit(MD_RECOVERY_INTR
,
2151 printk(KERN_INFO
"md/raid10:%s: insufficient "
2152 "working devices for recovery.\n",
2154 conf
->mirrors
[i
].recovery_disabled
2155 = mddev
->recovery_disabled
;
2160 if (biolist
== NULL
) {
2162 r10bio_t
*rb2
= r10_bio
;
2163 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
2164 rb2
->master_bio
= NULL
;
2170 /* resync. Schedule a read for every block at this virt offset */
2173 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
2175 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2176 &sync_blocks
, mddev
->degraded
) &&
2177 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
2178 &mddev
->recovery
)) {
2179 /* We can skip this block */
2181 return sync_blocks
+ sectors_skipped
;
2183 if (sync_blocks
< max_sync
)
2184 max_sync
= sync_blocks
;
2185 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2187 r10_bio
->mddev
= mddev
;
2188 atomic_set(&r10_bio
->remaining
, 0);
2189 raise_barrier(conf
, 0);
2190 conf
->next_resync
= sector_nr
;
2192 r10_bio
->master_bio
= NULL
;
2193 r10_bio
->sector
= sector_nr
;
2194 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
2195 raid10_find_phys(conf
, r10_bio
);
2196 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
2198 for (i
=0; i
<conf
->copies
; i
++) {
2199 int d
= r10_bio
->devs
[i
].devnum
;
2200 sector_t first_bad
, sector
;
2203 bio
= r10_bio
->devs
[i
].bio
;
2204 bio
->bi_end_io
= NULL
;
2205 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2206 if (conf
->mirrors
[d
].rdev
== NULL
||
2207 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
2209 sector
= r10_bio
->devs
[i
].addr
;
2210 if (is_badblock(conf
->mirrors
[d
].rdev
,
2212 &first_bad
, &bad_sectors
)) {
2213 if (first_bad
> sector
)
2214 max_sync
= first_bad
- sector
;
2216 bad_sectors
-= (sector
- first_bad
);
2217 if (max_sync
> bad_sectors
)
2218 max_sync
= max_sync
;
2222 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2223 atomic_inc(&r10_bio
->remaining
);
2224 bio
->bi_next
= biolist
;
2226 bio
->bi_private
= r10_bio
;
2227 bio
->bi_end_io
= end_sync_read
;
2229 bio
->bi_sector
= sector
+
2230 conf
->mirrors
[d
].rdev
->data_offset
;
2231 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2236 for (i
=0; i
<conf
->copies
; i
++) {
2237 int d
= r10_bio
->devs
[i
].devnum
;
2238 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
2239 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
2248 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2250 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
2252 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2255 bio
->bi_phys_segments
= 0;
2260 if (sector_nr
+ max_sync
< max_sector
)
2261 max_sector
= sector_nr
+ max_sync
;
2264 int len
= PAGE_SIZE
;
2265 if (sector_nr
+ (len
>>9) > max_sector
)
2266 len
= (max_sector
- sector_nr
) << 9;
2269 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2271 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2272 if (bio_add_page(bio
, page
, len
, 0))
2276 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2277 for (bio2
= biolist
;
2278 bio2
&& bio2
!= bio
;
2279 bio2
= bio2
->bi_next
) {
2280 /* remove last page from this bio */
2282 bio2
->bi_size
-= len
;
2283 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2287 nr_sectors
+= len
>>9;
2288 sector_nr
+= len
>>9;
2289 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
2291 r10_bio
->sectors
= nr_sectors
;
2295 biolist
= biolist
->bi_next
;
2297 bio
->bi_next
= NULL
;
2298 r10_bio
= bio
->bi_private
;
2299 r10_bio
->sectors
= nr_sectors
;
2301 if (bio
->bi_end_io
== end_sync_read
) {
2302 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2303 generic_make_request(bio
);
2307 if (sectors_skipped
)
2308 /* pretend they weren't skipped, it makes
2309 * no important difference in this case
2311 md_done_sync(mddev
, sectors_skipped
, 1);
2313 return sectors_skipped
+ nr_sectors
;
2315 /* There is nowhere to write, so all non-sync
2316 * drives must be failed or in resync, all drives
2317 * have a bad block, so try the next chunk...
2319 if (sector_nr
+ max_sync
< max_sector
)
2320 max_sector
= sector_nr
+ max_sync
;
2322 sectors_skipped
+= (max_sector
- sector_nr
);
2324 sector_nr
= max_sector
;
2329 raid10_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2332 conf_t
*conf
= mddev
->private;
2335 raid_disks
= conf
->raid_disks
;
2337 sectors
= conf
->dev_sectors
;
2339 size
= sectors
>> conf
->chunk_shift
;
2340 sector_div(size
, conf
->far_copies
);
2341 size
= size
* raid_disks
;
2342 sector_div(size
, conf
->near_copies
);
2344 return size
<< conf
->chunk_shift
;
2348 static conf_t
*setup_conf(mddev_t
*mddev
)
2350 conf_t
*conf
= NULL
;
2352 sector_t stride
, size
;
2355 if (mddev
->new_chunk_sectors
< (PAGE_SIZE
>> 9) ||
2356 !is_power_of_2(mddev
->new_chunk_sectors
)) {
2357 printk(KERN_ERR
"md/raid10:%s: chunk size must be "
2358 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2359 mdname(mddev
), PAGE_SIZE
);
2363 nc
= mddev
->new_layout
& 255;
2364 fc
= (mddev
->new_layout
>> 8) & 255;
2365 fo
= mddev
->new_layout
& (1<<16);
2367 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2368 (mddev
->new_layout
>> 17)) {
2369 printk(KERN_ERR
"md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2370 mdname(mddev
), mddev
->new_layout
);
2375 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2379 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2384 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2389 conf
->raid_disks
= mddev
->raid_disks
;
2390 conf
->near_copies
= nc
;
2391 conf
->far_copies
= fc
;
2392 conf
->copies
= nc
*fc
;
2393 conf
->far_offset
= fo
;
2394 conf
->chunk_mask
= mddev
->new_chunk_sectors
- 1;
2395 conf
->chunk_shift
= ffz(~mddev
->new_chunk_sectors
);
2397 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2398 r10bio_pool_free
, conf
);
2399 if (!conf
->r10bio_pool
)
2402 size
= mddev
->dev_sectors
>> conf
->chunk_shift
;
2403 sector_div(size
, fc
);
2404 size
= size
* conf
->raid_disks
;
2405 sector_div(size
, nc
);
2406 /* 'size' is now the number of chunks in the array */
2407 /* calculate "used chunks per device" in 'stride' */
2408 stride
= size
* conf
->copies
;
2410 /* We need to round up when dividing by raid_disks to
2411 * get the stride size.
2413 stride
+= conf
->raid_disks
- 1;
2414 sector_div(stride
, conf
->raid_disks
);
2416 conf
->dev_sectors
= stride
<< conf
->chunk_shift
;
2421 sector_div(stride
, fc
);
2422 conf
->stride
= stride
<< conf
->chunk_shift
;
2425 spin_lock_init(&conf
->device_lock
);
2426 INIT_LIST_HEAD(&conf
->retry_list
);
2428 spin_lock_init(&conf
->resync_lock
);
2429 init_waitqueue_head(&conf
->wait_barrier
);
2431 conf
->thread
= md_register_thread(raid10d
, mddev
, NULL
);
2435 conf
->mddev
= mddev
;
2439 printk(KERN_ERR
"md/raid10:%s: couldn't allocate memory.\n",
2442 if (conf
->r10bio_pool
)
2443 mempool_destroy(conf
->r10bio_pool
);
2444 kfree(conf
->mirrors
);
2445 safe_put_page(conf
->tmppage
);
2448 return ERR_PTR(err
);
2451 static int run(mddev_t
*mddev
)
2454 int i
, disk_idx
, chunk_size
;
2455 mirror_info_t
*disk
;
2460 * copy the already verified devices into our private RAID10
2461 * bookkeeping area. [whatever we allocate in run(),
2462 * should be freed in stop()]
2465 if (mddev
->private == NULL
) {
2466 conf
= setup_conf(mddev
);
2468 return PTR_ERR(conf
);
2469 mddev
->private = conf
;
2471 conf
= mddev
->private;
2475 mddev
->thread
= conf
->thread
;
2476 conf
->thread
= NULL
;
2478 chunk_size
= mddev
->chunk_sectors
<< 9;
2479 blk_queue_io_min(mddev
->queue
, chunk_size
);
2480 if (conf
->raid_disks
% conf
->near_copies
)
2481 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->raid_disks
);
2483 blk_queue_io_opt(mddev
->queue
, chunk_size
*
2484 (conf
->raid_disks
/ conf
->near_copies
));
2486 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2488 disk_idx
= rdev
->raid_disk
;
2489 if (disk_idx
>= conf
->raid_disks
2492 disk
= conf
->mirrors
+ disk_idx
;
2495 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2496 rdev
->data_offset
<< 9);
2497 /* as we don't honour merge_bvec_fn, we must never risk
2498 * violating it, so limit max_segments to 1 lying
2499 * within a single page.
2501 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2502 blk_queue_max_segments(mddev
->queue
, 1);
2503 blk_queue_segment_boundary(mddev
->queue
,
2504 PAGE_CACHE_SIZE
- 1);
2507 disk
->head_position
= 0;
2509 /* need to check that every block has at least one working mirror */
2510 if (!enough(conf
, -1)) {
2511 printk(KERN_ERR
"md/raid10:%s: not enough operational mirrors.\n",
2516 mddev
->degraded
= 0;
2517 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2519 disk
= conf
->mirrors
+ i
;
2522 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2523 disk
->head_position
= 0;
2530 if (mddev
->recovery_cp
!= MaxSector
)
2531 printk(KERN_NOTICE
"md/raid10:%s: not clean"
2532 " -- starting background reconstruction\n",
2535 "md/raid10:%s: active with %d out of %d devices\n",
2536 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
,
2539 * Ok, everything is just fine now
2541 mddev
->dev_sectors
= conf
->dev_sectors
;
2542 size
= raid10_size(mddev
, 0, 0);
2543 md_set_array_sectors(mddev
, size
);
2544 mddev
->resync_max_sectors
= size
;
2546 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2547 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2549 /* Calculate max read-ahead size.
2550 * We need to readahead at least twice a whole stripe....
2554 int stripe
= conf
->raid_disks
*
2555 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
2556 stripe
/= conf
->near_copies
;
2557 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2558 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2561 if (conf
->near_copies
< conf
->raid_disks
)
2562 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2564 if (md_integrity_register(mddev
))
2570 md_unregister_thread(mddev
->thread
);
2571 if (conf
->r10bio_pool
)
2572 mempool_destroy(conf
->r10bio_pool
);
2573 safe_put_page(conf
->tmppage
);
2574 kfree(conf
->mirrors
);
2576 mddev
->private = NULL
;
2581 static int stop(mddev_t
*mddev
)
2583 conf_t
*conf
= mddev
->private;
2585 raise_barrier(conf
, 0);
2586 lower_barrier(conf
);
2588 md_unregister_thread(mddev
->thread
);
2589 mddev
->thread
= NULL
;
2590 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2591 if (conf
->r10bio_pool
)
2592 mempool_destroy(conf
->r10bio_pool
);
2593 kfree(conf
->mirrors
);
2595 mddev
->private = NULL
;
2599 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2601 conf_t
*conf
= mddev
->private;
2605 raise_barrier(conf
, 0);
2608 lower_barrier(conf
);
2613 static void *raid10_takeover_raid0(mddev_t
*mddev
)
2618 if (mddev
->degraded
> 0) {
2619 printk(KERN_ERR
"md/raid10:%s: Error: degraded raid0!\n",
2621 return ERR_PTR(-EINVAL
);
2624 /* Set new parameters */
2625 mddev
->new_level
= 10;
2626 /* new layout: far_copies = 1, near_copies = 2 */
2627 mddev
->new_layout
= (1<<8) + 2;
2628 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2629 mddev
->delta_disks
= mddev
->raid_disks
;
2630 mddev
->raid_disks
*= 2;
2631 /* make sure it will be not marked as dirty */
2632 mddev
->recovery_cp
= MaxSector
;
2634 conf
= setup_conf(mddev
);
2635 if (!IS_ERR(conf
)) {
2636 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2637 if (rdev
->raid_disk
>= 0)
2638 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
2645 static void *raid10_takeover(mddev_t
*mddev
)
2647 struct raid0_private_data
*raid0_priv
;
2649 /* raid10 can take over:
2650 * raid0 - providing it has only two drives
2652 if (mddev
->level
== 0) {
2653 /* for raid0 takeover only one zone is supported */
2654 raid0_priv
= mddev
->private;
2655 if (raid0_priv
->nr_strip_zones
> 1) {
2656 printk(KERN_ERR
"md/raid10:%s: cannot takeover raid 0"
2657 " with more than one zone.\n",
2659 return ERR_PTR(-EINVAL
);
2661 return raid10_takeover_raid0(mddev
);
2663 return ERR_PTR(-EINVAL
);
2666 static struct mdk_personality raid10_personality
=
2670 .owner
= THIS_MODULE
,
2671 .make_request
= make_request
,
2675 .error_handler
= error
,
2676 .hot_add_disk
= raid10_add_disk
,
2677 .hot_remove_disk
= raid10_remove_disk
,
2678 .spare_active
= raid10_spare_active
,
2679 .sync_request
= sync_request
,
2680 .quiesce
= raid10_quiesce
,
2681 .size
= raid10_size
,
2682 .takeover
= raid10_takeover
,
2685 static int __init
raid_init(void)
2687 return register_md_personality(&raid10_personality
);
2690 static void raid_exit(void)
2692 unregister_md_personality(&raid10_personality
);
2695 module_init(raid_init
);
2696 module_exit(raid_exit
);
2697 MODULE_LICENSE("GPL");
2698 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2699 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2700 MODULE_ALIAS("md-raid10");
2701 MODULE_ALIAS("md-level-10");