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>
31 * RAID10 provides a combination of RAID0 and RAID1 functionality.
32 * The layout of data is defined by
35 * near_copies (stored in low byte of layout)
36 * far_copies (stored in second byte of layout)
37 * far_offset (stored in bit 16 of layout )
39 * The data to be stored is divided into chunks using chunksize.
40 * Each device is divided into far_copies sections.
41 * In each section, chunks are laid out in a style similar to raid0, but
42 * near_copies copies of each chunk is stored (each on a different drive).
43 * The starting device for each section is offset near_copies from the starting
44 * device of the previous section.
45 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
47 * near_copies and far_copies must be at least one, and their product is at most
50 * If far_offset is true, then the far_copies are handled a bit differently.
51 * The copies are still in different stripes, but instead of be very far apart
52 * on disk, there are adjacent stripes.
56 * Number of guaranteed r10bios in case of extreme VM load:
58 #define NR_RAID10_BIOS 256
60 static void allow_barrier(conf_t
*conf
);
61 static void lower_barrier(conf_t
*conf
);
63 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
66 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
68 /* allocate a r10bio with room for raid_disks entries in the bios array */
69 return kzalloc(size
, gfp_flags
);
72 static void r10bio_pool_free(void *r10_bio
, void *data
)
77 /* Maximum size of each resync request */
78 #define RESYNC_BLOCK_SIZE (64*1024)
79 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
80 /* amount of memory to reserve for resync requests */
81 #define RESYNC_WINDOW (1024*1024)
82 /* maximum number of concurrent requests, memory permitting */
83 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
86 * When performing a resync, we need to read and compare, so
87 * we need as many pages are there are copies.
88 * When performing a recovery, we need 2 bios, one for read,
89 * one for write (we recover only one drive per r10buf)
92 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
101 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
105 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
106 nalloc
= conf
->copies
; /* resync */
108 nalloc
= 2; /* recovery */
113 for (j
= nalloc
; j
-- ; ) {
114 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
117 r10_bio
->devs
[j
].bio
= bio
;
120 * Allocate RESYNC_PAGES data pages and attach them
123 for (j
= 0 ; j
< nalloc
; j
++) {
124 bio
= r10_bio
->devs
[j
].bio
;
125 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
126 page
= alloc_page(gfp_flags
);
130 bio
->bi_io_vec
[i
].bv_page
= page
;
138 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
140 for (i
= 0; i
< RESYNC_PAGES
; i
++)
141 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
144 while ( ++j
< nalloc
)
145 bio_put(r10_bio
->devs
[j
].bio
);
146 r10bio_pool_free(r10_bio
, conf
);
150 static void r10buf_pool_free(void *__r10_bio
, void *data
)
154 r10bio_t
*r10bio
= __r10_bio
;
157 for (j
=0; j
< conf
->copies
; j
++) {
158 struct bio
*bio
= r10bio
->devs
[j
].bio
;
160 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
161 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
162 bio
->bi_io_vec
[i
].bv_page
= NULL
;
167 r10bio_pool_free(r10bio
, conf
);
170 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
174 for (i
= 0; i
< conf
->copies
; i
++) {
175 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
176 if (*bio
&& *bio
!= IO_BLOCKED
)
182 static void free_r10bio(r10bio_t
*r10_bio
)
184 conf_t
*conf
= r10_bio
->mddev
->private;
187 * Wake up any possible resync thread that waits for the device
192 put_all_bios(conf
, r10_bio
);
193 mempool_free(r10_bio
, conf
->r10bio_pool
);
196 static void put_buf(r10bio_t
*r10_bio
)
198 conf_t
*conf
= r10_bio
->mddev
->private;
200 mempool_free(r10_bio
, conf
->r10buf_pool
);
205 static void reschedule_retry(r10bio_t
*r10_bio
)
208 mddev_t
*mddev
= r10_bio
->mddev
;
209 conf_t
*conf
= mddev
->private;
211 spin_lock_irqsave(&conf
->device_lock
, flags
);
212 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
214 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
216 /* wake up frozen array... */
217 wake_up(&conf
->wait_barrier
);
219 md_wakeup_thread(mddev
->thread
);
223 * raid_end_bio_io() is called when we have finished servicing a mirrored
224 * operation and are ready to return a success/failure code to the buffer
227 static void raid_end_bio_io(r10bio_t
*r10_bio
)
229 struct bio
*bio
= r10_bio
->master_bio
;
232 test_bit(R10BIO_Uptodate
, &r10_bio
->state
) ? 0 : -EIO
);
233 free_r10bio(r10_bio
);
237 * Update disk head position estimator based on IRQ completion info.
239 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
241 conf_t
*conf
= r10_bio
->mddev
->private;
243 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
244 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
247 static void raid10_end_read_request(struct bio
*bio
, int error
)
249 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
250 r10bio_t
*r10_bio
= bio
->bi_private
;
252 conf_t
*conf
= r10_bio
->mddev
->private;
255 slot
= r10_bio
->read_slot
;
256 dev
= r10_bio
->devs
[slot
].devnum
;
258 * this branch is our 'one mirror IO has finished' event handler:
260 update_head_pos(slot
, r10_bio
);
264 * Set R10BIO_Uptodate in our master bio, so that
265 * we will return a good error code to the higher
266 * levels even if IO on some other mirrored buffer fails.
268 * The 'master' represents the composite IO operation to
269 * user-side. So if something waits for IO, then it will
270 * wait for the 'master' bio.
272 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
273 raid_end_bio_io(r10_bio
);
274 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
277 * oops, read error - keep the refcount on the rdev
279 char b
[BDEVNAME_SIZE
];
280 if (printk_ratelimit())
281 printk(KERN_ERR
"md/raid10:%s: %s: rescheduling sector %llu\n",
283 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
284 reschedule_retry(r10_bio
);
288 static void raid10_end_write_request(struct bio
*bio
, int error
)
290 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
291 r10bio_t
*r10_bio
= bio
->bi_private
;
293 conf_t
*conf
= r10_bio
->mddev
->private;
295 for (slot
= 0; slot
< conf
->copies
; slot
++)
296 if (r10_bio
->devs
[slot
].bio
== bio
)
298 dev
= r10_bio
->devs
[slot
].devnum
;
301 * this branch is our 'one mirror IO has finished' event handler:
304 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
305 /* an I/O failed, we can't clear the bitmap */
306 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
309 * Set R10BIO_Uptodate in our master bio, so that
310 * we will return a good error code for to the higher
311 * levels even if IO on some other mirrored buffer fails.
313 * The 'master' represents the composite IO operation to
314 * user-side. So if something waits for IO, then it will
315 * wait for the 'master' bio.
317 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
319 update_head_pos(slot
, r10_bio
);
323 * Let's see if all mirrored write operations have finished
326 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
327 /* clear the bitmap if all writes complete successfully */
328 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
330 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
332 md_write_end(r10_bio
->mddev
);
333 raid_end_bio_io(r10_bio
);
336 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
341 * RAID10 layout manager
342 * As well as the chunksize and raid_disks count, there are two
343 * parameters: near_copies and far_copies.
344 * near_copies * far_copies must be <= raid_disks.
345 * Normally one of these will be 1.
346 * If both are 1, we get raid0.
347 * If near_copies == raid_disks, we get raid1.
349 * Chunks are laid out in raid0 style with near_copies copies of the
350 * first chunk, followed by near_copies copies of the next chunk and
352 * If far_copies > 1, then after 1/far_copies of the array has been assigned
353 * as described above, we start again with a device offset of near_copies.
354 * So we effectively have another copy of the whole array further down all
355 * the drives, but with blocks on different drives.
356 * With this layout, and block is never stored twice on the one device.
358 * raid10_find_phys finds the sector offset of a given virtual sector
359 * on each device that it is on.
361 * raid10_find_virt does the reverse mapping, from a device and a
362 * sector offset to a virtual address
365 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
375 /* now calculate first sector/dev */
376 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
377 sector
= r10bio
->sector
& conf
->chunk_mask
;
379 chunk
*= conf
->near_copies
;
381 dev
= sector_div(stripe
, conf
->raid_disks
);
382 if (conf
->far_offset
)
383 stripe
*= conf
->far_copies
;
385 sector
+= stripe
<< conf
->chunk_shift
;
387 /* and calculate all the others */
388 for (n
=0; n
< conf
->near_copies
; n
++) {
391 r10bio
->devs
[slot
].addr
= sector
;
392 r10bio
->devs
[slot
].devnum
= d
;
395 for (f
= 1; f
< conf
->far_copies
; f
++) {
396 d
+= conf
->near_copies
;
397 if (d
>= conf
->raid_disks
)
398 d
-= conf
->raid_disks
;
400 r10bio
->devs
[slot
].devnum
= d
;
401 r10bio
->devs
[slot
].addr
= s
;
405 if (dev
>= conf
->raid_disks
) {
407 sector
+= (conf
->chunk_mask
+ 1);
410 BUG_ON(slot
!= conf
->copies
);
413 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
415 sector_t offset
, chunk
, vchunk
;
417 offset
= sector
& conf
->chunk_mask
;
418 if (conf
->far_offset
) {
420 chunk
= sector
>> conf
->chunk_shift
;
421 fc
= sector_div(chunk
, conf
->far_copies
);
422 dev
-= fc
* conf
->near_copies
;
424 dev
+= conf
->raid_disks
;
426 while (sector
>= conf
->stride
) {
427 sector
-= conf
->stride
;
428 if (dev
< conf
->near_copies
)
429 dev
+= conf
->raid_disks
- conf
->near_copies
;
431 dev
-= conf
->near_copies
;
433 chunk
= sector
>> conf
->chunk_shift
;
435 vchunk
= chunk
* conf
->raid_disks
+ dev
;
436 sector_div(vchunk
, conf
->near_copies
);
437 return (vchunk
<< conf
->chunk_shift
) + offset
;
441 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
443 * @bvm: properties of new bio
444 * @biovec: the request that could be merged to it.
446 * Return amount of bytes we can accept at this offset
447 * If near_copies == raid_disk, there are no striping issues,
448 * but in that case, the function isn't called at all.
450 static int raid10_mergeable_bvec(struct request_queue
*q
,
451 struct bvec_merge_data
*bvm
,
452 struct bio_vec
*biovec
)
454 mddev_t
*mddev
= q
->queuedata
;
455 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
457 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
458 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
460 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
461 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
462 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
463 return biovec
->bv_len
;
469 * This routine returns the disk from which the requested read should
470 * be done. There is a per-array 'next expected sequential IO' sector
471 * number - if this matches on the next IO then we use the last disk.
472 * There is also a per-disk 'last know head position' sector that is
473 * maintained from IRQ contexts, both the normal and the resync IO
474 * completion handlers update this position correctly. If there is no
475 * perfect sequential match then we pick the disk whose head is closest.
477 * If there are 2 mirrors in the same 2 devices, performance degrades
478 * because position is mirror, not device based.
480 * The rdev for the device selected will have nr_pending incremented.
484 * FIXME: possibly should rethink readbalancing and do it differently
485 * depending on near_copies / far_copies geometry.
487 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
489 const sector_t this_sector
= r10_bio
->sector
;
491 const int sectors
= r10_bio
->sectors
;
492 sector_t new_distance
, best_dist
;
497 raid10_find_phys(conf
, r10_bio
);
501 best_dist
= MaxSector
;
504 * Check if we can balance. We can balance on the whole
505 * device if no resync is going on (recovery is ok), or below
506 * the resync window. We take the first readable disk when
507 * above the resync window.
509 if (conf
->mddev
->recovery_cp
< MaxSector
510 && (this_sector
+ sectors
>= conf
->next_resync
))
513 for (slot
= 0; slot
< conf
->copies
; slot
++) {
514 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
516 disk
= r10_bio
->devs
[slot
].devnum
;
517 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
520 if (!test_bit(In_sync
, &rdev
->flags
))
526 /* This optimisation is debatable, and completely destroys
527 * sequential read speed for 'far copies' arrays. So only
528 * keep it for 'near' arrays, and review those later.
530 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
533 /* for far > 1 always use the lowest address */
534 if (conf
->far_copies
> 1)
535 new_distance
= r10_bio
->devs
[slot
].addr
;
537 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
538 conf
->mirrors
[disk
].head_position
);
539 if (new_distance
< best_dist
) {
540 best_dist
= new_distance
;
544 if (slot
== conf
->copies
)
548 disk
= r10_bio
->devs
[slot
].devnum
;
549 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
552 atomic_inc(&rdev
->nr_pending
);
553 if (test_bit(Faulty
, &rdev
->flags
)) {
554 /* Cannot risk returning a device that failed
555 * before we inc'ed nr_pending
557 rdev_dec_pending(rdev
, conf
->mddev
);
560 r10_bio
->read_slot
= slot
;
568 static int raid10_congested(void *data
, int bits
)
570 mddev_t
*mddev
= data
;
571 conf_t
*conf
= mddev
->private;
574 if (mddev_congested(mddev
, bits
))
577 for (i
= 0; i
< conf
->raid_disks
&& ret
== 0; i
++) {
578 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
579 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
580 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
582 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
589 static void flush_pending_writes(conf_t
*conf
)
591 /* Any writes that have been queued but are awaiting
592 * bitmap updates get flushed here.
594 spin_lock_irq(&conf
->device_lock
);
596 if (conf
->pending_bio_list
.head
) {
598 bio
= bio_list_get(&conf
->pending_bio_list
);
599 spin_unlock_irq(&conf
->device_lock
);
600 /* flush any pending bitmap writes to disk
601 * before proceeding w/ I/O */
602 bitmap_unplug(conf
->mddev
->bitmap
);
604 while (bio
) { /* submit pending writes */
605 struct bio
*next
= bio
->bi_next
;
607 generic_make_request(bio
);
611 spin_unlock_irq(&conf
->device_lock
);
615 * Sometimes we need to suspend IO while we do something else,
616 * either some resync/recovery, or reconfigure the array.
617 * To do this we raise a 'barrier'.
618 * The 'barrier' is a counter that can be raised multiple times
619 * to count how many activities are happening which preclude
621 * We can only raise the barrier if there is no pending IO.
622 * i.e. if nr_pending == 0.
623 * We choose only to raise the barrier if no-one is waiting for the
624 * barrier to go down. This means that as soon as an IO request
625 * is ready, no other operations which require a barrier will start
626 * until the IO request has had a chance.
628 * So: regular IO calls 'wait_barrier'. When that returns there
629 * is no backgroup IO happening, It must arrange to call
630 * allow_barrier when it has finished its IO.
631 * backgroup IO calls must call raise_barrier. Once that returns
632 * there is no normal IO happeing. It must arrange to call
633 * lower_barrier when the particular background IO completes.
636 static void raise_barrier(conf_t
*conf
, int force
)
638 BUG_ON(force
&& !conf
->barrier
);
639 spin_lock_irq(&conf
->resync_lock
);
641 /* Wait until no block IO is waiting (unless 'force') */
642 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
643 conf
->resync_lock
, );
645 /* block any new IO from starting */
648 /* Now wait for all pending IO to complete */
649 wait_event_lock_irq(conf
->wait_barrier
,
650 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
651 conf
->resync_lock
, );
653 spin_unlock_irq(&conf
->resync_lock
);
656 static void lower_barrier(conf_t
*conf
)
659 spin_lock_irqsave(&conf
->resync_lock
, flags
);
661 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
662 wake_up(&conf
->wait_barrier
);
665 static void wait_barrier(conf_t
*conf
)
667 spin_lock_irq(&conf
->resync_lock
);
670 /* Wait for the barrier to drop.
671 * However if there are already pending
672 * requests (preventing the barrier from
673 * rising completely), and the
674 * pre-process bio queue isn't empty,
675 * then don't wait, as we need to empty
676 * that queue to get the nr_pending
679 wait_event_lock_irq(conf
->wait_barrier
,
683 !bio_list_empty(current
->bio_list
)),
689 spin_unlock_irq(&conf
->resync_lock
);
692 static void allow_barrier(conf_t
*conf
)
695 spin_lock_irqsave(&conf
->resync_lock
, flags
);
697 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
698 wake_up(&conf
->wait_barrier
);
701 static void freeze_array(conf_t
*conf
)
703 /* stop syncio and normal IO and wait for everything to
705 * We increment barrier and nr_waiting, and then
706 * wait until nr_pending match nr_queued+1
707 * This is called in the context of one normal IO request
708 * that has failed. Thus any sync request that might be pending
709 * will be blocked by nr_pending, and we need to wait for
710 * pending IO requests to complete or be queued for re-try.
711 * Thus the number queued (nr_queued) plus this request (1)
712 * must match the number of pending IOs (nr_pending) before
715 spin_lock_irq(&conf
->resync_lock
);
718 wait_event_lock_irq(conf
->wait_barrier
,
719 conf
->nr_pending
== conf
->nr_queued
+1,
721 flush_pending_writes(conf
));
723 spin_unlock_irq(&conf
->resync_lock
);
726 static void unfreeze_array(conf_t
*conf
)
728 /* reverse the effect of the freeze */
729 spin_lock_irq(&conf
->resync_lock
);
732 wake_up(&conf
->wait_barrier
);
733 spin_unlock_irq(&conf
->resync_lock
);
736 static int make_request(mddev_t
*mddev
, struct bio
* bio
)
738 conf_t
*conf
= mddev
->private;
739 mirror_info_t
*mirror
;
741 struct bio
*read_bio
;
743 int chunk_sects
= conf
->chunk_mask
+ 1;
744 const int rw
= bio_data_dir(bio
);
745 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
746 const unsigned long do_fua
= (bio
->bi_rw
& REQ_FUA
);
748 mdk_rdev_t
*blocked_rdev
;
751 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
752 md_flush_request(mddev
, bio
);
756 /* If this request crosses a chunk boundary, we need to
757 * split it. This will only happen for 1 PAGE (or less) requests.
759 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
761 conf
->near_copies
< conf
->raid_disks
)) {
763 /* Sanity check -- queue functions should prevent this happening */
764 if (bio
->bi_vcnt
!= 1 ||
767 /* This is a one page bio that upper layers
768 * refuse to split for us, so we need to split it.
771 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
773 /* Each of these 'make_request' calls will call 'wait_barrier'.
774 * If the first succeeds but the second blocks due to the resync
775 * thread raising the barrier, we will deadlock because the
776 * IO to the underlying device will be queued in generic_make_request
777 * and will never complete, so will never reduce nr_pending.
778 * So increment nr_waiting here so no new raise_barriers will
779 * succeed, and so the second wait_barrier cannot block.
781 spin_lock_irq(&conf
->resync_lock
);
783 spin_unlock_irq(&conf
->resync_lock
);
785 if (make_request(mddev
, &bp
->bio1
))
786 generic_make_request(&bp
->bio1
);
787 if (make_request(mddev
, &bp
->bio2
))
788 generic_make_request(&bp
->bio2
);
790 spin_lock_irq(&conf
->resync_lock
);
792 wake_up(&conf
->wait_barrier
);
793 spin_unlock_irq(&conf
->resync_lock
);
795 bio_pair_release(bp
);
798 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
799 " or bigger than %dk %llu %d\n", mdname(mddev
), chunk_sects
/2,
800 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
806 md_write_start(mddev
, bio
);
809 * Register the new request and wait if the reconstruction
810 * thread has put up a bar for new requests.
811 * Continue immediately if no resync is active currently.
815 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
817 r10_bio
->master_bio
= bio
;
818 r10_bio
->sectors
= bio
->bi_size
>> 9;
820 r10_bio
->mddev
= mddev
;
821 r10_bio
->sector
= bio
->bi_sector
;
826 * read balancing logic:
828 int disk
= read_balance(conf
, r10_bio
);
829 int slot
= r10_bio
->read_slot
;
831 raid_end_bio_io(r10_bio
);
834 mirror
= conf
->mirrors
+ disk
;
836 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
838 r10_bio
->devs
[slot
].bio
= read_bio
;
840 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
841 mirror
->rdev
->data_offset
;
842 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
843 read_bio
->bi_end_io
= raid10_end_read_request
;
844 read_bio
->bi_rw
= READ
| do_sync
;
845 read_bio
->bi_private
= r10_bio
;
847 generic_make_request(read_bio
);
854 /* first select target devices under rcu_lock and
855 * inc refcount on their rdev. Record them by setting
858 plugged
= mddev_check_plugged(mddev
);
860 raid10_find_phys(conf
, r10_bio
);
864 for (i
= 0; i
< conf
->copies
; i
++) {
865 int d
= r10_bio
->devs
[i
].devnum
;
866 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
867 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
868 atomic_inc(&rdev
->nr_pending
);
872 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
873 atomic_inc(&rdev
->nr_pending
);
874 r10_bio
->devs
[i
].bio
= bio
;
876 r10_bio
->devs
[i
].bio
= NULL
;
877 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
882 if (unlikely(blocked_rdev
)) {
883 /* Have to wait for this device to get unblocked, then retry */
887 for (j
= 0; j
< i
; j
++)
888 if (r10_bio
->devs
[j
].bio
) {
889 d
= r10_bio
->devs
[j
].devnum
;
890 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
893 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
898 atomic_set(&r10_bio
->remaining
, 1);
899 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
901 for (i
= 0; i
< conf
->copies
; i
++) {
903 int d
= r10_bio
->devs
[i
].devnum
;
904 if (!r10_bio
->devs
[i
].bio
)
907 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
908 r10_bio
->devs
[i
].bio
= mbio
;
910 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
911 conf
->mirrors
[d
].rdev
->data_offset
;
912 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
913 mbio
->bi_end_io
= raid10_end_write_request
;
914 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
915 mbio
->bi_private
= r10_bio
;
917 atomic_inc(&r10_bio
->remaining
);
918 spin_lock_irqsave(&conf
->device_lock
, flags
);
919 bio_list_add(&conf
->pending_bio_list
, mbio
);
920 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
923 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
924 /* This matches the end of raid10_end_write_request() */
925 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
927 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
930 raid_end_bio_io(r10_bio
);
933 /* In case raid10d snuck in to freeze_array */
934 wake_up(&conf
->wait_barrier
);
936 if (do_sync
|| !mddev
->bitmap
|| !plugged
)
937 md_wakeup_thread(mddev
->thread
);
941 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
943 conf_t
*conf
= mddev
->private;
946 if (conf
->near_copies
< conf
->raid_disks
)
947 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
948 if (conf
->near_copies
> 1)
949 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
950 if (conf
->far_copies
> 1) {
951 if (conf
->far_offset
)
952 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
954 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
956 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
957 conf
->raid_disks
- mddev
->degraded
);
958 for (i
= 0; i
< conf
->raid_disks
; i
++)
959 seq_printf(seq
, "%s",
960 conf
->mirrors
[i
].rdev
&&
961 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
962 seq_printf(seq
, "]");
965 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
967 char b
[BDEVNAME_SIZE
];
968 conf_t
*conf
= mddev
->private;
971 * If it is not operational, then we have already marked it as dead
972 * else if it is the last working disks, ignore the error, let the
973 * next level up know.
974 * else mark the drive as failed
976 if (test_bit(In_sync
, &rdev
->flags
)
977 && conf
->raid_disks
-mddev
->degraded
== 1)
979 * Don't fail the drive, just return an IO error.
980 * The test should really be more sophisticated than
981 * "working_disks == 1", but it isn't critical, and
982 * can wait until we do more sophisticated "is the drive
983 * really dead" tests...
986 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
988 spin_lock_irqsave(&conf
->device_lock
, flags
);
990 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
992 * if recovery is running, make sure it aborts.
994 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
996 set_bit(Faulty
, &rdev
->flags
);
997 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
999 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1000 "md/raid10:%s: Operation continuing on %d devices.\n",
1001 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1002 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1005 static void print_conf(conf_t
*conf
)
1010 printk(KERN_DEBUG
"RAID10 conf printout:\n");
1012 printk(KERN_DEBUG
"(!conf)\n");
1015 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1018 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1019 char b
[BDEVNAME_SIZE
];
1020 tmp
= conf
->mirrors
+ i
;
1022 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1023 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1024 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1025 bdevname(tmp
->rdev
->bdev
,b
));
1029 static void close_sync(conf_t
*conf
)
1032 allow_barrier(conf
);
1034 mempool_destroy(conf
->r10buf_pool
);
1035 conf
->r10buf_pool
= NULL
;
1038 /* check if there are enough drives for
1039 * every block to appear on atleast one
1041 static int enough(conf_t
*conf
)
1046 int n
= conf
->copies
;
1049 if (conf
->mirrors
[first
].rdev
)
1051 first
= (first
+1) % conf
->raid_disks
;
1055 } while (first
!= 0);
1059 static int raid10_spare_active(mddev_t
*mddev
)
1062 conf_t
*conf
= mddev
->private;
1065 unsigned long flags
;
1068 * Find all non-in_sync disks within the RAID10 configuration
1069 * and mark them in_sync
1071 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1072 tmp
= conf
->mirrors
+ i
;
1074 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1075 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1077 sysfs_notify_dirent(tmp
->rdev
->sysfs_state
);
1080 spin_lock_irqsave(&conf
->device_lock
, flags
);
1081 mddev
->degraded
-= count
;
1082 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1089 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1091 conf_t
*conf
= mddev
->private;
1096 int last
= conf
->raid_disks
- 1;
1098 if (mddev
->recovery_cp
< MaxSector
)
1099 /* only hot-add to in-sync arrays, as recovery is
1100 * very different from resync
1106 if (rdev
->raid_disk
>= 0)
1107 first
= last
= rdev
->raid_disk
;
1109 if (rdev
->saved_raid_disk
>= 0 &&
1110 rdev
->saved_raid_disk
>= first
&&
1111 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1112 mirror
= rdev
->saved_raid_disk
;
1115 for ( ; mirror
<= last
; mirror
++)
1116 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1118 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1119 rdev
->data_offset
<< 9);
1120 /* as we don't honour merge_bvec_fn, we must
1121 * never risk violating it, so limit
1122 * ->max_segments to one lying with a single
1123 * page, as a one page request is never in
1126 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1127 blk_queue_max_segments(mddev
->queue
, 1);
1128 blk_queue_segment_boundary(mddev
->queue
,
1129 PAGE_CACHE_SIZE
- 1);
1132 p
->head_position
= 0;
1133 rdev
->raid_disk
= mirror
;
1135 if (rdev
->saved_raid_disk
!= mirror
)
1137 rcu_assign_pointer(p
->rdev
, rdev
);
1141 md_integrity_add_rdev(rdev
, mddev
);
1146 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1148 conf_t
*conf
= mddev
->private;
1151 mirror_info_t
*p
= conf
->mirrors
+ number
;
1156 if (test_bit(In_sync
, &rdev
->flags
) ||
1157 atomic_read(&rdev
->nr_pending
)) {
1161 /* Only remove faulty devices in recovery
1164 if (!test_bit(Faulty
, &rdev
->flags
) &&
1171 if (atomic_read(&rdev
->nr_pending
)) {
1172 /* lost the race, try later */
1177 err
= md_integrity_register(mddev
);
1186 static void end_sync_read(struct bio
*bio
, int error
)
1188 r10bio_t
*r10_bio
= bio
->bi_private
;
1189 conf_t
*conf
= r10_bio
->mddev
->private;
1192 for (i
=0; i
<conf
->copies
; i
++)
1193 if (r10_bio
->devs
[i
].bio
== bio
)
1195 BUG_ON(i
== conf
->copies
);
1196 update_head_pos(i
, r10_bio
);
1197 d
= r10_bio
->devs
[i
].devnum
;
1199 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1200 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1202 atomic_add(r10_bio
->sectors
,
1203 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1204 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1205 md_error(r10_bio
->mddev
,
1206 conf
->mirrors
[d
].rdev
);
1209 /* for reconstruct, we always reschedule after a read.
1210 * for resync, only after all reads
1212 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1213 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1214 atomic_dec_and_test(&r10_bio
->remaining
)) {
1215 /* we have read all the blocks,
1216 * do the comparison in process context in raid10d
1218 reschedule_retry(r10_bio
);
1222 static void end_sync_write(struct bio
*bio
, int error
)
1224 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1225 r10bio_t
*r10_bio
= bio
->bi_private
;
1226 mddev_t
*mddev
= r10_bio
->mddev
;
1227 conf_t
*conf
= mddev
->private;
1230 for (i
= 0; i
< conf
->copies
; i
++)
1231 if (r10_bio
->devs
[i
].bio
== bio
)
1233 d
= r10_bio
->devs
[i
].devnum
;
1236 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1238 update_head_pos(i
, r10_bio
);
1240 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1241 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1242 if (r10_bio
->master_bio
== NULL
) {
1243 /* the primary of several recovery bios */
1244 sector_t s
= r10_bio
->sectors
;
1246 md_done_sync(mddev
, s
, 1);
1249 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1257 * Note: sync and recover and handled very differently for raid10
1258 * This code is for resync.
1259 * For resync, we read through virtual addresses and read all blocks.
1260 * If there is any error, we schedule a write. The lowest numbered
1261 * drive is authoritative.
1262 * However requests come for physical address, so we need to map.
1263 * For every physical address there are raid_disks/copies virtual addresses,
1264 * which is always are least one, but is not necessarly an integer.
1265 * This means that a physical address can span multiple chunks, so we may
1266 * have to submit multiple io requests for a single sync request.
1269 * We check if all blocks are in-sync and only write to blocks that
1272 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1274 conf_t
*conf
= mddev
->private;
1276 struct bio
*tbio
, *fbio
;
1278 atomic_set(&r10_bio
->remaining
, 1);
1280 /* find the first device with a block */
1281 for (i
=0; i
<conf
->copies
; i
++)
1282 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1285 if (i
== conf
->copies
)
1289 fbio
= r10_bio
->devs
[i
].bio
;
1291 /* now find blocks with errors */
1292 for (i
=0 ; i
< conf
->copies
; i
++) {
1294 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1296 tbio
= r10_bio
->devs
[i
].bio
;
1298 if (tbio
->bi_end_io
!= end_sync_read
)
1302 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1303 /* We know that the bi_io_vec layout is the same for
1304 * both 'first' and 'i', so we just compare them.
1305 * All vec entries are PAGE_SIZE;
1307 for (j
= 0; j
< vcnt
; j
++)
1308 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1309 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1314 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1316 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1317 /* Don't fix anything. */
1319 /* Ok, we need to write this bio
1320 * First we need to fixup bv_offset, bv_len and
1321 * bi_vecs, as the read request might have corrupted these
1323 tbio
->bi_vcnt
= vcnt
;
1324 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1326 tbio
->bi_phys_segments
= 0;
1327 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1328 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1329 tbio
->bi_next
= NULL
;
1330 tbio
->bi_rw
= WRITE
;
1331 tbio
->bi_private
= r10_bio
;
1332 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1334 for (j
=0; j
< vcnt
; j
++) {
1335 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1336 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1338 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1339 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1342 tbio
->bi_end_io
= end_sync_write
;
1344 d
= r10_bio
->devs
[i
].devnum
;
1345 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1346 atomic_inc(&r10_bio
->remaining
);
1347 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1349 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1350 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1351 generic_make_request(tbio
);
1355 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1356 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1362 * Now for the recovery code.
1363 * Recovery happens across physical sectors.
1364 * We recover all non-is_sync drives by finding the virtual address of
1365 * each, and then choose a working drive that also has that virt address.
1366 * There is a separate r10_bio for each non-in_sync drive.
1367 * Only the first two slots are in use. The first for reading,
1368 * The second for writing.
1372 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1374 conf_t
*conf
= mddev
->private;
1376 struct bio
*bio
, *wbio
;
1379 /* move the pages across to the second bio
1380 * and submit the write request
1382 bio
= r10_bio
->devs
[0].bio
;
1383 wbio
= r10_bio
->devs
[1].bio
;
1384 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1385 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1386 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1387 wbio
->bi_io_vec
[i
].bv_page
= p
;
1389 d
= r10_bio
->devs
[1].devnum
;
1391 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1392 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1393 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1394 generic_make_request(wbio
);
1396 bio_endio(wbio
, -EIO
);
1401 * Used by fix_read_error() to decay the per rdev read_errors.
1402 * We halve the read error count for every hour that has elapsed
1403 * since the last recorded read error.
1406 static void check_decay_read_errors(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1408 struct timespec cur_time_mon
;
1409 unsigned long hours_since_last
;
1410 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
1412 ktime_get_ts(&cur_time_mon
);
1414 if (rdev
->last_read_error
.tv_sec
== 0 &&
1415 rdev
->last_read_error
.tv_nsec
== 0) {
1416 /* first time we've seen a read error */
1417 rdev
->last_read_error
= cur_time_mon
;
1421 hours_since_last
= (cur_time_mon
.tv_sec
-
1422 rdev
->last_read_error
.tv_sec
) / 3600;
1424 rdev
->last_read_error
= cur_time_mon
;
1427 * if hours_since_last is > the number of bits in read_errors
1428 * just set read errors to 0. We do this to avoid
1429 * overflowing the shift of read_errors by hours_since_last.
1431 if (hours_since_last
>= 8 * sizeof(read_errors
))
1432 atomic_set(&rdev
->read_errors
, 0);
1434 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
1438 * This is a kernel thread which:
1440 * 1. Retries failed read operations on working mirrors.
1441 * 2. Updates the raid superblock when problems encounter.
1442 * 3. Performs writes following reads for array synchronising.
1445 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1447 int sect
= 0; /* Offset from r10_bio->sector */
1448 int sectors
= r10_bio
->sectors
;
1450 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
1451 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1453 /* still own a reference to this rdev, so it cannot
1454 * have been cleared recently.
1456 rdev
= conf
->mirrors
[d
].rdev
;
1458 if (test_bit(Faulty
, &rdev
->flags
))
1459 /* drive has already been failed, just ignore any
1460 more fix_read_error() attempts */
1463 check_decay_read_errors(mddev
, rdev
);
1464 atomic_inc(&rdev
->read_errors
);
1465 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
1466 char b
[BDEVNAME_SIZE
];
1467 bdevname(rdev
->bdev
, b
);
1470 "md/raid10:%s: %s: Raid device exceeded "
1471 "read_error threshold [cur %d:max %d]\n",
1473 atomic_read(&rdev
->read_errors
), max_read_errors
);
1475 "md/raid10:%s: %s: Failing raid device\n",
1477 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1483 int sl
= r10_bio
->read_slot
;
1487 if (s
> (PAGE_SIZE
>>9))
1492 d
= r10_bio
->devs
[sl
].devnum
;
1493 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1495 test_bit(In_sync
, &rdev
->flags
)) {
1496 atomic_inc(&rdev
->nr_pending
);
1498 success
= sync_page_io(rdev
,
1499 r10_bio
->devs
[sl
].addr
+
1502 conf
->tmppage
, READ
, false);
1503 rdev_dec_pending(rdev
, mddev
);
1509 if (sl
== conf
->copies
)
1511 } while (!success
&& sl
!= r10_bio
->read_slot
);
1515 /* Cannot read from anywhere -- bye bye array */
1516 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1517 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1522 /* write it back and re-read */
1524 while (sl
!= r10_bio
->read_slot
) {
1525 char b
[BDEVNAME_SIZE
];
1530 d
= r10_bio
->devs
[sl
].devnum
;
1531 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1533 test_bit(In_sync
, &rdev
->flags
)) {
1534 atomic_inc(&rdev
->nr_pending
);
1536 atomic_add(s
, &rdev
->corrected_errors
);
1537 if (sync_page_io(rdev
,
1538 r10_bio
->devs
[sl
].addr
+
1540 s
<<9, conf
->tmppage
, WRITE
, false)
1542 /* Well, this device is dead */
1544 "md/raid10:%s: read correction "
1546 " (%d sectors at %llu on %s)\n",
1548 (unsigned long long)(
1549 sect
+ rdev
->data_offset
),
1550 bdevname(rdev
->bdev
, b
));
1551 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1554 bdevname(rdev
->bdev
, b
));
1555 md_error(mddev
, rdev
);
1557 rdev_dec_pending(rdev
, mddev
);
1562 while (sl
!= r10_bio
->read_slot
) {
1567 d
= r10_bio
->devs
[sl
].devnum
;
1568 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1570 test_bit(In_sync
, &rdev
->flags
)) {
1571 char b
[BDEVNAME_SIZE
];
1572 atomic_inc(&rdev
->nr_pending
);
1574 if (sync_page_io(rdev
,
1575 r10_bio
->devs
[sl
].addr
+
1577 s
<<9, conf
->tmppage
,
1578 READ
, false) == 0) {
1579 /* Well, this device is dead */
1581 "md/raid10:%s: unable to read back "
1583 " (%d sectors at %llu on %s)\n",
1585 (unsigned long long)(
1586 sect
+ rdev
->data_offset
),
1587 bdevname(rdev
->bdev
, b
));
1588 printk(KERN_NOTICE
"md/raid10:%s: %s: failing drive\n",
1590 bdevname(rdev
->bdev
, b
));
1592 md_error(mddev
, rdev
);
1595 "md/raid10:%s: read error corrected"
1596 " (%d sectors at %llu on %s)\n",
1598 (unsigned long long)(
1599 sect
+ rdev
->data_offset
),
1600 bdevname(rdev
->bdev
, b
));
1603 rdev_dec_pending(rdev
, mddev
);
1614 static void raid10d(mddev_t
*mddev
)
1618 unsigned long flags
;
1619 conf_t
*conf
= mddev
->private;
1620 struct list_head
*head
= &conf
->retry_list
;
1622 struct blk_plug plug
;
1624 md_check_recovery(mddev
);
1626 blk_start_plug(&plug
);
1628 char b
[BDEVNAME_SIZE
];
1630 flush_pending_writes(conf
);
1632 spin_lock_irqsave(&conf
->device_lock
, flags
);
1633 if (list_empty(head
)) {
1634 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1637 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1638 list_del(head
->prev
);
1640 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1642 mddev
= r10_bio
->mddev
;
1643 conf
= mddev
->private;
1644 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
1645 sync_request_write(mddev
, r10_bio
);
1646 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
1647 recovery_request_write(mddev
, r10_bio
);
1649 int slot
= r10_bio
->read_slot
;
1650 int mirror
= r10_bio
->devs
[slot
].devnum
;
1651 /* we got a read error. Maybe the drive is bad. Maybe just
1652 * the block and we can fix it.
1653 * We freeze all other IO, and try reading the block from
1654 * other devices. When we find one, we re-write
1655 * and check it that fixes the read error.
1656 * This is all done synchronously while the array is
1659 if (mddev
->ro
== 0) {
1661 fix_read_error(conf
, mddev
, r10_bio
);
1662 unfreeze_array(conf
);
1664 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, mddev
);
1666 bio
= r10_bio
->devs
[slot
].bio
;
1667 r10_bio
->devs
[slot
].bio
=
1668 mddev
->ro
? IO_BLOCKED
: NULL
;
1669 mirror
= read_balance(conf
, r10_bio
);
1671 printk(KERN_ALERT
"md/raid10:%s: %s: unrecoverable I/O"
1672 " read error for block %llu\n",
1674 bdevname(bio
->bi_bdev
,b
),
1675 (unsigned long long)r10_bio
->sector
);
1676 raid_end_bio_io(r10_bio
);
1679 const unsigned long do_sync
= (r10_bio
->master_bio
->bi_rw
& REQ_SYNC
);
1681 slot
= r10_bio
->read_slot
;
1682 rdev
= conf
->mirrors
[mirror
].rdev
;
1683 if (printk_ratelimit())
1684 printk(KERN_ERR
"md/raid10:%s: %s: redirecting sector %llu to"
1685 " another mirror\n",
1687 bdevname(rdev
->bdev
,b
),
1688 (unsigned long long)r10_bio
->sector
);
1689 bio
= bio_clone_mddev(r10_bio
->master_bio
,
1691 r10_bio
->devs
[slot
].bio
= bio
;
1692 bio
->bi_sector
= r10_bio
->devs
[slot
].addr
1693 + rdev
->data_offset
;
1694 bio
->bi_bdev
= rdev
->bdev
;
1695 bio
->bi_rw
= READ
| do_sync
;
1696 bio
->bi_private
= r10_bio
;
1697 bio
->bi_end_io
= raid10_end_read_request
;
1698 generic_make_request(bio
);
1703 blk_finish_plug(&plug
);
1707 static int init_resync(conf_t
*conf
)
1711 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1712 BUG_ON(conf
->r10buf_pool
);
1713 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1714 if (!conf
->r10buf_pool
)
1716 conf
->next_resync
= 0;
1721 * perform a "sync" on one "block"
1723 * We need to make sure that no normal I/O request - particularly write
1724 * requests - conflict with active sync requests.
1726 * This is achieved by tracking pending requests and a 'barrier' concept
1727 * that can be installed to exclude normal IO requests.
1729 * Resync and recovery are handled very differently.
1730 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1732 * For resync, we iterate over virtual addresses, read all copies,
1733 * and update if there are differences. If only one copy is live,
1735 * For recovery, we iterate over physical addresses, read a good
1736 * value for each non-in_sync drive, and over-write.
1738 * So, for recovery we may have several outstanding complex requests for a
1739 * given address, one for each out-of-sync device. We model this by allocating
1740 * a number of r10_bio structures, one for each out-of-sync device.
1741 * As we setup these structures, we collect all bio's together into a list
1742 * which we then process collectively to add pages, and then process again
1743 * to pass to generic_make_request.
1745 * The r10_bio structures are linked using a borrowed master_bio pointer.
1746 * This link is counted in ->remaining. When the r10_bio that points to NULL
1747 * has its remaining count decremented to 0, the whole complex operation
1752 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
,
1753 int *skipped
, int go_faster
)
1755 conf_t
*conf
= mddev
->private;
1757 struct bio
*biolist
= NULL
, *bio
;
1758 sector_t max_sector
, nr_sectors
;
1761 sector_t sync_blocks
;
1763 sector_t sectors_skipped
= 0;
1764 int chunks_skipped
= 0;
1766 if (!conf
->r10buf_pool
)
1767 if (init_resync(conf
))
1771 max_sector
= mddev
->dev_sectors
;
1772 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1773 max_sector
= mddev
->resync_max_sectors
;
1774 if (sector_nr
>= max_sector
) {
1775 /* If we aborted, we need to abort the
1776 * sync on the 'current' bitmap chucks (there can
1777 * be several when recovering multiple devices).
1778 * as we may have started syncing it but not finished.
1779 * We can find the current address in
1780 * mddev->curr_resync, but for recovery,
1781 * we need to convert that to several
1782 * virtual addresses.
1784 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1785 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1786 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1788 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1790 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1791 bitmap_end_sync(mddev
->bitmap
, sect
,
1794 } else /* completed sync */
1797 bitmap_close_sync(mddev
->bitmap
);
1800 return sectors_skipped
;
1802 if (chunks_skipped
>= conf
->raid_disks
) {
1803 /* if there has been nothing to do on any drive,
1804 * then there is nothing to do at all..
1807 return (max_sector
- sector_nr
) + sectors_skipped
;
1810 if (max_sector
> mddev
->resync_max
)
1811 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
1813 /* make sure whole request will fit in a chunk - if chunks
1816 if (conf
->near_copies
< conf
->raid_disks
&&
1817 max_sector
> (sector_nr
| conf
->chunk_mask
))
1818 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1820 * If there is non-resync activity waiting for us then
1821 * put in a delay to throttle resync.
1823 if (!go_faster
&& conf
->nr_waiting
)
1824 msleep_interruptible(1000);
1826 /* Again, very different code for resync and recovery.
1827 * Both must result in an r10bio with a list of bios that
1828 * have bi_end_io, bi_sector, bi_bdev set,
1829 * and bi_private set to the r10bio.
1830 * For recovery, we may actually create several r10bios
1831 * with 2 bios in each, that correspond to the bios in the main one.
1832 * In this case, the subordinate r10bios link back through a
1833 * borrowed master_bio pointer, and the counter in the master
1834 * includes a ref from each subordinate.
1836 /* First, we decide what to do and set ->bi_end_io
1837 * To end_sync_read if we want to read, and
1838 * end_sync_write if we will want to write.
1841 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
1842 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1843 /* recovery... the complicated one */
1847 for (i
=0 ; i
<conf
->raid_disks
; i
++) {
1853 if (conf
->mirrors
[i
].rdev
== NULL
||
1854 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
))
1858 /* want to reconstruct this device */
1860 sect
= raid10_find_virt(conf
, sector_nr
, i
);
1861 /* Unless we are doing a full sync, we only need
1862 * to recover the block if it is set in the bitmap
1864 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1866 if (sync_blocks
< max_sync
)
1867 max_sync
= sync_blocks
;
1870 /* yep, skip the sync_blocks here, but don't assume
1871 * that there will never be anything to do here
1873 chunks_skipped
= -1;
1877 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1878 raise_barrier(conf
, rb2
!= NULL
);
1879 atomic_set(&r10_bio
->remaining
, 0);
1881 r10_bio
->master_bio
= (struct bio
*)rb2
;
1883 atomic_inc(&rb2
->remaining
);
1884 r10_bio
->mddev
= mddev
;
1885 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1886 r10_bio
->sector
= sect
;
1888 raid10_find_phys(conf
, r10_bio
);
1890 /* Need to check if the array will still be
1893 for (j
=0; j
<conf
->raid_disks
; j
++)
1894 if (conf
->mirrors
[j
].rdev
== NULL
||
1895 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
1900 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1901 &sync_blocks
, still_degraded
);
1903 for (j
=0; j
<conf
->copies
;j
++) {
1904 int d
= r10_bio
->devs
[j
].devnum
;
1905 if (!conf
->mirrors
[d
].rdev
||
1906 !test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
))
1908 /* This is where we read from */
1909 bio
= r10_bio
->devs
[0].bio
;
1910 bio
->bi_next
= biolist
;
1912 bio
->bi_private
= r10_bio
;
1913 bio
->bi_end_io
= end_sync_read
;
1915 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1916 conf
->mirrors
[d
].rdev
->data_offset
;
1917 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1918 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1919 atomic_inc(&r10_bio
->remaining
);
1920 /* and we write to 'i' */
1922 for (k
=0; k
<conf
->copies
; k
++)
1923 if (r10_bio
->devs
[k
].devnum
== i
)
1925 BUG_ON(k
== conf
->copies
);
1926 bio
= r10_bio
->devs
[1].bio
;
1927 bio
->bi_next
= biolist
;
1929 bio
->bi_private
= r10_bio
;
1930 bio
->bi_end_io
= end_sync_write
;
1932 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1933 conf
->mirrors
[i
].rdev
->data_offset
;
1934 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1936 r10_bio
->devs
[0].devnum
= d
;
1937 r10_bio
->devs
[1].devnum
= i
;
1941 if (j
== conf
->copies
) {
1942 /* Cannot recover, so abort the recovery */
1945 atomic_dec(&rb2
->remaining
);
1947 if (!test_and_set_bit(MD_RECOVERY_INTR
,
1949 printk(KERN_INFO
"md/raid10:%s: insufficient "
1950 "working devices for recovery.\n",
1955 if (biolist
== NULL
) {
1957 r10bio_t
*rb2
= r10_bio
;
1958 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
1959 rb2
->master_bio
= NULL
;
1965 /* resync. Schedule a read for every block at this virt offset */
1968 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
1970 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
1971 &sync_blocks
, mddev
->degraded
) &&
1972 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
1973 &mddev
->recovery
)) {
1974 /* We can skip this block */
1976 return sync_blocks
+ sectors_skipped
;
1978 if (sync_blocks
< max_sync
)
1979 max_sync
= sync_blocks
;
1980 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1982 r10_bio
->mddev
= mddev
;
1983 atomic_set(&r10_bio
->remaining
, 0);
1984 raise_barrier(conf
, 0);
1985 conf
->next_resync
= sector_nr
;
1987 r10_bio
->master_bio
= NULL
;
1988 r10_bio
->sector
= sector_nr
;
1989 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
1990 raid10_find_phys(conf
, r10_bio
);
1991 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
1993 for (i
=0; i
<conf
->copies
; i
++) {
1994 int d
= r10_bio
->devs
[i
].devnum
;
1995 bio
= r10_bio
->devs
[i
].bio
;
1996 bio
->bi_end_io
= NULL
;
1997 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1998 if (conf
->mirrors
[d
].rdev
== NULL
||
1999 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
2001 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2002 atomic_inc(&r10_bio
->remaining
);
2003 bio
->bi_next
= biolist
;
2005 bio
->bi_private
= r10_bio
;
2006 bio
->bi_end_io
= end_sync_read
;
2008 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
2009 conf
->mirrors
[d
].rdev
->data_offset
;
2010 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2015 for (i
=0; i
<conf
->copies
; i
++) {
2016 int d
= r10_bio
->devs
[i
].devnum
;
2017 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
2018 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
2027 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2029 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
2031 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2034 bio
->bi_phys_segments
= 0;
2039 if (sector_nr
+ max_sync
< max_sector
)
2040 max_sector
= sector_nr
+ max_sync
;
2043 int len
= PAGE_SIZE
;
2044 if (sector_nr
+ (len
>>9) > max_sector
)
2045 len
= (max_sector
- sector_nr
) << 9;
2048 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2050 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2051 if (bio_add_page(bio
, page
, len
, 0))
2055 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2056 for (bio2
= biolist
;
2057 bio2
&& bio2
!= bio
;
2058 bio2
= bio2
->bi_next
) {
2059 /* remove last page from this bio */
2061 bio2
->bi_size
-= len
;
2062 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2066 nr_sectors
+= len
>>9;
2067 sector_nr
+= len
>>9;
2068 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
2070 r10_bio
->sectors
= nr_sectors
;
2074 biolist
= biolist
->bi_next
;
2076 bio
->bi_next
= NULL
;
2077 r10_bio
= bio
->bi_private
;
2078 r10_bio
->sectors
= nr_sectors
;
2080 if (bio
->bi_end_io
== end_sync_read
) {
2081 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2082 generic_make_request(bio
);
2086 if (sectors_skipped
)
2087 /* pretend they weren't skipped, it makes
2088 * no important difference in this case
2090 md_done_sync(mddev
, sectors_skipped
, 1);
2092 return sectors_skipped
+ nr_sectors
;
2094 /* There is nowhere to write, so all non-sync
2095 * drives must be failed, so try the next chunk...
2097 if (sector_nr
+ max_sync
< max_sector
)
2098 max_sector
= sector_nr
+ max_sync
;
2100 sectors_skipped
+= (max_sector
- sector_nr
);
2102 sector_nr
= max_sector
;
2107 raid10_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2110 conf_t
*conf
= mddev
->private;
2113 raid_disks
= conf
->raid_disks
;
2115 sectors
= conf
->dev_sectors
;
2117 size
= sectors
>> conf
->chunk_shift
;
2118 sector_div(size
, conf
->far_copies
);
2119 size
= size
* raid_disks
;
2120 sector_div(size
, conf
->near_copies
);
2122 return size
<< conf
->chunk_shift
;
2126 static conf_t
*setup_conf(mddev_t
*mddev
)
2128 conf_t
*conf
= NULL
;
2130 sector_t stride
, size
;
2133 if (mddev
->new_chunk_sectors
< (PAGE_SIZE
>> 9) ||
2134 !is_power_of_2(mddev
->new_chunk_sectors
)) {
2135 printk(KERN_ERR
"md/raid10:%s: chunk size must be "
2136 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2137 mdname(mddev
), PAGE_SIZE
);
2141 nc
= mddev
->new_layout
& 255;
2142 fc
= (mddev
->new_layout
>> 8) & 255;
2143 fo
= mddev
->new_layout
& (1<<16);
2145 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2146 (mddev
->new_layout
>> 17)) {
2147 printk(KERN_ERR
"md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2148 mdname(mddev
), mddev
->new_layout
);
2153 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2157 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2162 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2167 conf
->raid_disks
= mddev
->raid_disks
;
2168 conf
->near_copies
= nc
;
2169 conf
->far_copies
= fc
;
2170 conf
->copies
= nc
*fc
;
2171 conf
->far_offset
= fo
;
2172 conf
->chunk_mask
= mddev
->new_chunk_sectors
- 1;
2173 conf
->chunk_shift
= ffz(~mddev
->new_chunk_sectors
);
2175 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2176 r10bio_pool_free
, conf
);
2177 if (!conf
->r10bio_pool
)
2180 size
= mddev
->dev_sectors
>> conf
->chunk_shift
;
2181 sector_div(size
, fc
);
2182 size
= size
* conf
->raid_disks
;
2183 sector_div(size
, nc
);
2184 /* 'size' is now the number of chunks in the array */
2185 /* calculate "used chunks per device" in 'stride' */
2186 stride
= size
* conf
->copies
;
2188 /* We need to round up when dividing by raid_disks to
2189 * get the stride size.
2191 stride
+= conf
->raid_disks
- 1;
2192 sector_div(stride
, conf
->raid_disks
);
2194 conf
->dev_sectors
= stride
<< conf
->chunk_shift
;
2199 sector_div(stride
, fc
);
2200 conf
->stride
= stride
<< conf
->chunk_shift
;
2203 spin_lock_init(&conf
->device_lock
);
2204 INIT_LIST_HEAD(&conf
->retry_list
);
2206 spin_lock_init(&conf
->resync_lock
);
2207 init_waitqueue_head(&conf
->wait_barrier
);
2209 conf
->thread
= md_register_thread(raid10d
, mddev
, NULL
);
2213 conf
->mddev
= mddev
;
2217 printk(KERN_ERR
"md/raid10:%s: couldn't allocate memory.\n",
2220 if (conf
->r10bio_pool
)
2221 mempool_destroy(conf
->r10bio_pool
);
2222 kfree(conf
->mirrors
);
2223 safe_put_page(conf
->tmppage
);
2226 return ERR_PTR(err
);
2229 static int run(mddev_t
*mddev
)
2232 int i
, disk_idx
, chunk_size
;
2233 mirror_info_t
*disk
;
2238 * copy the already verified devices into our private RAID10
2239 * bookkeeping area. [whatever we allocate in run(),
2240 * should be freed in stop()]
2243 if (mddev
->private == NULL
) {
2244 conf
= setup_conf(mddev
);
2246 return PTR_ERR(conf
);
2247 mddev
->private = conf
;
2249 conf
= mddev
->private;
2253 mddev
->thread
= conf
->thread
;
2254 conf
->thread
= NULL
;
2256 chunk_size
= mddev
->chunk_sectors
<< 9;
2257 blk_queue_io_min(mddev
->queue
, chunk_size
);
2258 if (conf
->raid_disks
% conf
->near_copies
)
2259 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->raid_disks
);
2261 blk_queue_io_opt(mddev
->queue
, chunk_size
*
2262 (conf
->raid_disks
/ conf
->near_copies
));
2264 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2265 disk_idx
= rdev
->raid_disk
;
2266 if (disk_idx
>= conf
->raid_disks
2269 disk
= conf
->mirrors
+ disk_idx
;
2272 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2273 rdev
->data_offset
<< 9);
2274 /* as we don't honour merge_bvec_fn, we must never risk
2275 * violating it, so limit max_segments to 1 lying
2276 * within a single page.
2278 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2279 blk_queue_max_segments(mddev
->queue
, 1);
2280 blk_queue_segment_boundary(mddev
->queue
,
2281 PAGE_CACHE_SIZE
- 1);
2284 disk
->head_position
= 0;
2286 /* need to check that every block has at least one working mirror */
2287 if (!enough(conf
)) {
2288 printk(KERN_ERR
"md/raid10:%s: not enough operational mirrors.\n",
2293 mddev
->degraded
= 0;
2294 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2296 disk
= conf
->mirrors
+ i
;
2299 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2300 disk
->head_position
= 0;
2307 if (mddev
->recovery_cp
!= MaxSector
)
2308 printk(KERN_NOTICE
"md/raid10:%s: not clean"
2309 " -- starting background reconstruction\n",
2312 "md/raid10:%s: active with %d out of %d devices\n",
2313 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
,
2316 * Ok, everything is just fine now
2318 mddev
->dev_sectors
= conf
->dev_sectors
;
2319 size
= raid10_size(mddev
, 0, 0);
2320 md_set_array_sectors(mddev
, size
);
2321 mddev
->resync_max_sectors
= size
;
2323 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2324 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2326 /* Calculate max read-ahead size.
2327 * We need to readahead at least twice a whole stripe....
2331 int stripe
= conf
->raid_disks
*
2332 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
2333 stripe
/= conf
->near_copies
;
2334 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2335 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2338 if (conf
->near_copies
< conf
->raid_disks
)
2339 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2341 if (md_integrity_register(mddev
))
2347 md_unregister_thread(&mddev
->thread
);
2348 if (conf
->r10bio_pool
)
2349 mempool_destroy(conf
->r10bio_pool
);
2350 safe_put_page(conf
->tmppage
);
2351 kfree(conf
->mirrors
);
2353 mddev
->private = NULL
;
2358 static int stop(mddev_t
*mddev
)
2360 conf_t
*conf
= mddev
->private;
2362 raise_barrier(conf
, 0);
2363 lower_barrier(conf
);
2365 md_unregister_thread(&mddev
->thread
);
2366 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2367 if (conf
->r10bio_pool
)
2368 mempool_destroy(conf
->r10bio_pool
);
2369 kfree(conf
->mirrors
);
2371 mddev
->private = NULL
;
2375 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2377 conf_t
*conf
= mddev
->private;
2381 raise_barrier(conf
, 0);
2384 lower_barrier(conf
);
2389 static void *raid10_takeover_raid0(mddev_t
*mddev
)
2394 if (mddev
->degraded
> 0) {
2395 printk(KERN_ERR
"md/raid10:%s: Error: degraded raid0!\n",
2397 return ERR_PTR(-EINVAL
);
2400 /* Set new parameters */
2401 mddev
->new_level
= 10;
2402 /* new layout: far_copies = 1, near_copies = 2 */
2403 mddev
->new_layout
= (1<<8) + 2;
2404 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2405 mddev
->delta_disks
= mddev
->raid_disks
;
2406 mddev
->raid_disks
*= 2;
2407 /* make sure it will be not marked as dirty */
2408 mddev
->recovery_cp
= MaxSector
;
2410 conf
= setup_conf(mddev
);
2411 if (!IS_ERR(conf
)) {
2412 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2413 if (rdev
->raid_disk
>= 0)
2414 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
2421 static void *raid10_takeover(mddev_t
*mddev
)
2423 struct raid0_private_data
*raid0_priv
;
2425 /* raid10 can take over:
2426 * raid0 - providing it has only two drives
2428 if (mddev
->level
== 0) {
2429 /* for raid0 takeover only one zone is supported */
2430 raid0_priv
= mddev
->private;
2431 if (raid0_priv
->nr_strip_zones
> 1) {
2432 printk(KERN_ERR
"md/raid10:%s: cannot takeover raid 0"
2433 " with more than one zone.\n",
2435 return ERR_PTR(-EINVAL
);
2437 return raid10_takeover_raid0(mddev
);
2439 return ERR_PTR(-EINVAL
);
2442 static struct mdk_personality raid10_personality
=
2446 .owner
= THIS_MODULE
,
2447 .make_request
= make_request
,
2451 .error_handler
= error
,
2452 .hot_add_disk
= raid10_add_disk
,
2453 .hot_remove_disk
= raid10_remove_disk
,
2454 .spare_active
= raid10_spare_active
,
2455 .sync_request
= sync_request
,
2456 .quiesce
= raid10_quiesce
,
2457 .size
= raid10_size
,
2458 .takeover
= raid10_takeover
,
2461 static int __init
raid_init(void)
2463 return register_md_personality(&raid10_personality
);
2466 static void raid_exit(void)
2468 unregister_md_personality(&raid10_personality
);
2471 module_init(raid_init
);
2472 module_exit(raid_exit
);
2473 MODULE_LICENSE("GPL");
2474 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2475 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2476 MODULE_ALIAS("md-raid10");
2477 MODULE_ALIAS("md-level-10");