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
);
248 * Find the disk number which triggered given bio
250 static int find_bio_disk(conf_t
*conf
, r10bio_t
*r10_bio
, struct bio
*bio
)
254 for (slot
= 0; slot
< conf
->copies
; slot
++)
255 if (r10_bio
->devs
[slot
].bio
== bio
)
258 BUG_ON(slot
== conf
->copies
);
259 update_head_pos(slot
, r10_bio
);
261 return r10_bio
->devs
[slot
].devnum
;
264 static void raid10_end_read_request(struct bio
*bio
, int error
)
266 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
267 r10bio_t
*r10_bio
= bio
->bi_private
;
269 conf_t
*conf
= r10_bio
->mddev
->private;
272 slot
= r10_bio
->read_slot
;
273 dev
= r10_bio
->devs
[slot
].devnum
;
275 * this branch is our 'one mirror IO has finished' event handler:
277 update_head_pos(slot
, r10_bio
);
281 * Set R10BIO_Uptodate in our master bio, so that
282 * we will return a good error code to the higher
283 * levels even if IO on some other mirrored buffer fails.
285 * The 'master' represents the composite IO operation to
286 * user-side. So if something waits for IO, then it will
287 * wait for the 'master' bio.
289 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
290 raid_end_bio_io(r10_bio
);
291 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
294 * oops, read error - keep the refcount on the rdev
296 char b
[BDEVNAME_SIZE
];
297 if (printk_ratelimit())
298 printk(KERN_ERR
"md/raid10:%s: %s: rescheduling sector %llu\n",
300 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
301 reschedule_retry(r10_bio
);
305 static void raid10_end_write_request(struct bio
*bio
, int error
)
307 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
308 r10bio_t
*r10_bio
= bio
->bi_private
;
310 conf_t
*conf
= r10_bio
->mddev
->private;
312 dev
= find_bio_disk(conf
, r10_bio
, bio
);
315 * this branch is our 'one mirror IO has finished' event handler:
318 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
319 /* an I/O failed, we can't clear the bitmap */
320 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
323 * Set R10BIO_Uptodate in our master bio, so that
324 * we will return a good error code for to the higher
325 * levels even if IO on some other mirrored buffer fails.
327 * The 'master' represents the composite IO operation to
328 * user-side. So if something waits for IO, then it will
329 * wait for the 'master' bio.
331 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
335 * Let's see if all mirrored write operations have finished
338 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
339 /* clear the bitmap if all writes complete successfully */
340 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
342 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
344 md_write_end(r10_bio
->mddev
);
345 raid_end_bio_io(r10_bio
);
348 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
353 * RAID10 layout manager
354 * As well as the chunksize and raid_disks count, there are two
355 * parameters: near_copies and far_copies.
356 * near_copies * far_copies must be <= raid_disks.
357 * Normally one of these will be 1.
358 * If both are 1, we get raid0.
359 * If near_copies == raid_disks, we get raid1.
361 * Chunks are laid out in raid0 style with near_copies copies of the
362 * first chunk, followed by near_copies copies of the next chunk and
364 * If far_copies > 1, then after 1/far_copies of the array has been assigned
365 * as described above, we start again with a device offset of near_copies.
366 * So we effectively have another copy of the whole array further down all
367 * the drives, but with blocks on different drives.
368 * With this layout, and block is never stored twice on the one device.
370 * raid10_find_phys finds the sector offset of a given virtual sector
371 * on each device that it is on.
373 * raid10_find_virt does the reverse mapping, from a device and a
374 * sector offset to a virtual address
377 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
387 /* now calculate first sector/dev */
388 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
389 sector
= r10bio
->sector
& conf
->chunk_mask
;
391 chunk
*= conf
->near_copies
;
393 dev
= sector_div(stripe
, conf
->raid_disks
);
394 if (conf
->far_offset
)
395 stripe
*= conf
->far_copies
;
397 sector
+= stripe
<< conf
->chunk_shift
;
399 /* and calculate all the others */
400 for (n
=0; n
< conf
->near_copies
; n
++) {
403 r10bio
->devs
[slot
].addr
= sector
;
404 r10bio
->devs
[slot
].devnum
= d
;
407 for (f
= 1; f
< conf
->far_copies
; f
++) {
408 d
+= conf
->near_copies
;
409 if (d
>= conf
->raid_disks
)
410 d
-= conf
->raid_disks
;
412 r10bio
->devs
[slot
].devnum
= d
;
413 r10bio
->devs
[slot
].addr
= s
;
417 if (dev
>= conf
->raid_disks
) {
419 sector
+= (conf
->chunk_mask
+ 1);
422 BUG_ON(slot
!= conf
->copies
);
425 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
427 sector_t offset
, chunk
, vchunk
;
429 offset
= sector
& conf
->chunk_mask
;
430 if (conf
->far_offset
) {
432 chunk
= sector
>> conf
->chunk_shift
;
433 fc
= sector_div(chunk
, conf
->far_copies
);
434 dev
-= fc
* conf
->near_copies
;
436 dev
+= conf
->raid_disks
;
438 while (sector
>= conf
->stride
) {
439 sector
-= conf
->stride
;
440 if (dev
< conf
->near_copies
)
441 dev
+= conf
->raid_disks
- conf
->near_copies
;
443 dev
-= conf
->near_copies
;
445 chunk
= sector
>> conf
->chunk_shift
;
447 vchunk
= chunk
* conf
->raid_disks
+ dev
;
448 sector_div(vchunk
, conf
->near_copies
);
449 return (vchunk
<< conf
->chunk_shift
) + offset
;
453 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
455 * @bvm: properties of new bio
456 * @biovec: the request that could be merged to it.
458 * Return amount of bytes we can accept at this offset
459 * If near_copies == raid_disk, there are no striping issues,
460 * but in that case, the function isn't called at all.
462 static int raid10_mergeable_bvec(struct request_queue
*q
,
463 struct bvec_merge_data
*bvm
,
464 struct bio_vec
*biovec
)
466 mddev_t
*mddev
= q
->queuedata
;
467 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
469 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
470 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
472 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
473 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
474 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
475 return biovec
->bv_len
;
481 * This routine returns the disk from which the requested read should
482 * be done. There is a per-array 'next expected sequential IO' sector
483 * number - if this matches on the next IO then we use the last disk.
484 * There is also a per-disk 'last know head position' sector that is
485 * maintained from IRQ contexts, both the normal and the resync IO
486 * completion handlers update this position correctly. If there is no
487 * perfect sequential match then we pick the disk whose head is closest.
489 * If there are 2 mirrors in the same 2 devices, performance degrades
490 * because position is mirror, not device based.
492 * The rdev for the device selected will have nr_pending incremented.
496 * FIXME: possibly should rethink readbalancing and do it differently
497 * depending on near_copies / far_copies geometry.
499 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
501 const sector_t this_sector
= r10_bio
->sector
;
503 const int sectors
= r10_bio
->sectors
;
504 sector_t new_distance
, best_dist
;
509 raid10_find_phys(conf
, r10_bio
);
513 best_dist
= MaxSector
;
516 * Check if we can balance. We can balance on the whole
517 * device if no resync is going on (recovery is ok), or below
518 * the resync window. We take the first readable disk when
519 * above the resync window.
521 if (conf
->mddev
->recovery_cp
< MaxSector
522 && (this_sector
+ sectors
>= conf
->next_resync
))
525 for (slot
= 0; slot
< conf
->copies
; slot
++) {
526 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
528 disk
= r10_bio
->devs
[slot
].devnum
;
529 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
532 if (!test_bit(In_sync
, &rdev
->flags
))
538 /* This optimisation is debatable, and completely destroys
539 * sequential read speed for 'far copies' arrays. So only
540 * keep it for 'near' arrays, and review those later.
542 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
545 /* for far > 1 always use the lowest address */
546 if (conf
->far_copies
> 1)
547 new_distance
= r10_bio
->devs
[slot
].addr
;
549 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
550 conf
->mirrors
[disk
].head_position
);
551 if (new_distance
< best_dist
) {
552 best_dist
= new_distance
;
556 if (slot
== conf
->copies
)
560 disk
= r10_bio
->devs
[slot
].devnum
;
561 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
564 atomic_inc(&rdev
->nr_pending
);
565 if (test_bit(Faulty
, &rdev
->flags
)) {
566 /* Cannot risk returning a device that failed
567 * before we inc'ed nr_pending
569 rdev_dec_pending(rdev
, conf
->mddev
);
572 r10_bio
->read_slot
= slot
;
580 static int raid10_congested(void *data
, int bits
)
582 mddev_t
*mddev
= data
;
583 conf_t
*conf
= mddev
->private;
586 if (mddev_congested(mddev
, bits
))
589 for (i
= 0; i
< conf
->raid_disks
&& ret
== 0; i
++) {
590 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
591 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
592 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
594 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
601 static void flush_pending_writes(conf_t
*conf
)
603 /* Any writes that have been queued but are awaiting
604 * bitmap updates get flushed here.
606 spin_lock_irq(&conf
->device_lock
);
608 if (conf
->pending_bio_list
.head
) {
610 bio
= bio_list_get(&conf
->pending_bio_list
);
611 spin_unlock_irq(&conf
->device_lock
);
612 /* flush any pending bitmap writes to disk
613 * before proceeding w/ I/O */
614 bitmap_unplug(conf
->mddev
->bitmap
);
616 while (bio
) { /* submit pending writes */
617 struct bio
*next
= bio
->bi_next
;
619 generic_make_request(bio
);
623 spin_unlock_irq(&conf
->device_lock
);
627 * Sometimes we need to suspend IO while we do something else,
628 * either some resync/recovery, or reconfigure the array.
629 * To do this we raise a 'barrier'.
630 * The 'barrier' is a counter that can be raised multiple times
631 * to count how many activities are happening which preclude
633 * We can only raise the barrier if there is no pending IO.
634 * i.e. if nr_pending == 0.
635 * We choose only to raise the barrier if no-one is waiting for the
636 * barrier to go down. This means that as soon as an IO request
637 * is ready, no other operations which require a barrier will start
638 * until the IO request has had a chance.
640 * So: regular IO calls 'wait_barrier'. When that returns there
641 * is no backgroup IO happening, It must arrange to call
642 * allow_barrier when it has finished its IO.
643 * backgroup IO calls must call raise_barrier. Once that returns
644 * there is no normal IO happeing. It must arrange to call
645 * lower_barrier when the particular background IO completes.
648 static void raise_barrier(conf_t
*conf
, int force
)
650 BUG_ON(force
&& !conf
->barrier
);
651 spin_lock_irq(&conf
->resync_lock
);
653 /* Wait until no block IO is waiting (unless 'force') */
654 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
655 conf
->resync_lock
, );
657 /* block any new IO from starting */
660 /* Now wait for all pending IO to complete */
661 wait_event_lock_irq(conf
->wait_barrier
,
662 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
663 conf
->resync_lock
, );
665 spin_unlock_irq(&conf
->resync_lock
);
668 static void lower_barrier(conf_t
*conf
)
671 spin_lock_irqsave(&conf
->resync_lock
, flags
);
673 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
674 wake_up(&conf
->wait_barrier
);
677 static void wait_barrier(conf_t
*conf
)
679 spin_lock_irq(&conf
->resync_lock
);
682 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
688 spin_unlock_irq(&conf
->resync_lock
);
691 static void allow_barrier(conf_t
*conf
)
694 spin_lock_irqsave(&conf
->resync_lock
, flags
);
696 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
697 wake_up(&conf
->wait_barrier
);
700 static void freeze_array(conf_t
*conf
)
702 /* stop syncio and normal IO and wait for everything to
704 * We increment barrier and nr_waiting, and then
705 * wait until nr_pending match nr_queued+1
706 * This is called in the context of one normal IO request
707 * that has failed. Thus any sync request that might be pending
708 * will be blocked by nr_pending, and we need to wait for
709 * pending IO requests to complete or be queued for re-try.
710 * Thus the number queued (nr_queued) plus this request (1)
711 * must match the number of pending IOs (nr_pending) before
714 spin_lock_irq(&conf
->resync_lock
);
717 wait_event_lock_irq(conf
->wait_barrier
,
718 conf
->nr_pending
== conf
->nr_queued
+1,
720 flush_pending_writes(conf
));
722 spin_unlock_irq(&conf
->resync_lock
);
725 static void unfreeze_array(conf_t
*conf
)
727 /* reverse the effect of the freeze */
728 spin_lock_irq(&conf
->resync_lock
);
731 wake_up(&conf
->wait_barrier
);
732 spin_unlock_irq(&conf
->resync_lock
);
735 static int make_request(mddev_t
*mddev
, struct bio
* bio
)
737 conf_t
*conf
= mddev
->private;
738 mirror_info_t
*mirror
;
740 struct bio
*read_bio
;
742 int chunk_sects
= conf
->chunk_mask
+ 1;
743 const int rw
= bio_data_dir(bio
);
744 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
745 const unsigned long do_fua
= (bio
->bi_rw
& REQ_FUA
);
747 mdk_rdev_t
*blocked_rdev
;
750 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
751 md_flush_request(mddev
, bio
);
755 /* If this request crosses a chunk boundary, we need to
756 * split it. This will only happen for 1 PAGE (or less) requests.
758 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
760 conf
->near_copies
< conf
->raid_disks
)) {
762 /* Sanity check -- queue functions should prevent this happening */
763 if (bio
->bi_vcnt
!= 1 ||
766 /* This is a one page bio that upper layers
767 * refuse to split for us, so we need to split it.
770 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
772 /* Each of these 'make_request' calls will call 'wait_barrier'.
773 * If the first succeeds but the second blocks due to the resync
774 * thread raising the barrier, we will deadlock because the
775 * IO to the underlying device will be queued in generic_make_request
776 * and will never complete, so will never reduce nr_pending.
777 * So increment nr_waiting here so no new raise_barriers will
778 * succeed, and so the second wait_barrier cannot block.
780 spin_lock_irq(&conf
->resync_lock
);
782 spin_unlock_irq(&conf
->resync_lock
);
784 if (make_request(mddev
, &bp
->bio1
))
785 generic_make_request(&bp
->bio1
);
786 if (make_request(mddev
, &bp
->bio2
))
787 generic_make_request(&bp
->bio2
);
789 spin_lock_irq(&conf
->resync_lock
);
791 wake_up(&conf
->wait_barrier
);
792 spin_unlock_irq(&conf
->resync_lock
);
794 bio_pair_release(bp
);
797 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
798 " or bigger than %dk %llu %d\n", mdname(mddev
), chunk_sects
/2,
799 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
805 md_write_start(mddev
, bio
);
808 * Register the new request and wait if the reconstruction
809 * thread has put up a bar for new requests.
810 * Continue immediately if no resync is active currently.
814 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
816 r10_bio
->master_bio
= bio
;
817 r10_bio
->sectors
= bio
->bi_size
>> 9;
819 r10_bio
->mddev
= mddev
;
820 r10_bio
->sector
= bio
->bi_sector
;
825 * read balancing logic:
827 int disk
= read_balance(conf
, r10_bio
);
828 int slot
= r10_bio
->read_slot
;
830 raid_end_bio_io(r10_bio
);
833 mirror
= conf
->mirrors
+ disk
;
835 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
837 r10_bio
->devs
[slot
].bio
= read_bio
;
839 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
840 mirror
->rdev
->data_offset
;
841 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
842 read_bio
->bi_end_io
= raid10_end_read_request
;
843 read_bio
->bi_rw
= READ
| do_sync
;
844 read_bio
->bi_private
= r10_bio
;
846 generic_make_request(read_bio
);
853 /* first select target devices under rcu_lock and
854 * inc refcount on their rdev. Record them by setting
857 plugged
= mddev_check_plugged(mddev
);
859 raid10_find_phys(conf
, r10_bio
);
863 for (i
= 0; i
< conf
->copies
; i
++) {
864 int d
= r10_bio
->devs
[i
].devnum
;
865 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
866 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
867 atomic_inc(&rdev
->nr_pending
);
871 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
872 atomic_inc(&rdev
->nr_pending
);
873 r10_bio
->devs
[i
].bio
= bio
;
875 r10_bio
->devs
[i
].bio
= NULL
;
876 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
881 if (unlikely(blocked_rdev
)) {
882 /* Have to wait for this device to get unblocked, then retry */
886 for (j
= 0; j
< i
; j
++)
887 if (r10_bio
->devs
[j
].bio
) {
888 d
= r10_bio
->devs
[j
].devnum
;
889 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
892 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
897 atomic_set(&r10_bio
->remaining
, 1);
898 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
900 for (i
= 0; i
< conf
->copies
; i
++) {
902 int d
= r10_bio
->devs
[i
].devnum
;
903 if (!r10_bio
->devs
[i
].bio
)
906 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
907 r10_bio
->devs
[i
].bio
= mbio
;
909 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
910 conf
->mirrors
[d
].rdev
->data_offset
;
911 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
912 mbio
->bi_end_io
= raid10_end_write_request
;
913 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
914 mbio
->bi_private
= r10_bio
;
916 atomic_inc(&r10_bio
->remaining
);
917 spin_lock_irqsave(&conf
->device_lock
, flags
);
918 bio_list_add(&conf
->pending_bio_list
, mbio
);
919 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
922 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
923 /* This matches the end of raid10_end_write_request() */
924 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
926 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
929 raid_end_bio_io(r10_bio
);
932 /* In case raid10d snuck in to freeze_array */
933 wake_up(&conf
->wait_barrier
);
935 if (do_sync
|| !mddev
->bitmap
|| !plugged
)
936 md_wakeup_thread(mddev
->thread
);
940 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
942 conf_t
*conf
= mddev
->private;
945 if (conf
->near_copies
< conf
->raid_disks
)
946 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
947 if (conf
->near_copies
> 1)
948 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
949 if (conf
->far_copies
> 1) {
950 if (conf
->far_offset
)
951 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
953 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
955 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
956 conf
->raid_disks
- mddev
->degraded
);
957 for (i
= 0; i
< conf
->raid_disks
; i
++)
958 seq_printf(seq
, "%s",
959 conf
->mirrors
[i
].rdev
&&
960 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
961 seq_printf(seq
, "]");
964 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
966 char b
[BDEVNAME_SIZE
];
967 conf_t
*conf
= mddev
->private;
970 * If it is not operational, then we have already marked it as dead
971 * else if it is the last working disks, ignore the error, let the
972 * next level up know.
973 * else mark the drive as failed
975 if (test_bit(In_sync
, &rdev
->flags
)
976 && conf
->raid_disks
-mddev
->degraded
== 1)
978 * Don't fail the drive, just return an IO error.
979 * The test should really be more sophisticated than
980 * "working_disks == 1", but it isn't critical, and
981 * can wait until we do more sophisticated "is the drive
982 * really dead" tests...
985 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
987 spin_lock_irqsave(&conf
->device_lock
, flags
);
989 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
991 * if recovery is running, make sure it aborts.
993 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
995 set_bit(Faulty
, &rdev
->flags
);
996 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
998 "md/raid10:%s: Disk failure on %s, disabling device.\n"
999 "md/raid10:%s: Operation continuing on %d devices.\n",
1000 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1001 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1004 static void print_conf(conf_t
*conf
)
1009 printk(KERN_DEBUG
"RAID10 conf printout:\n");
1011 printk(KERN_DEBUG
"(!conf)\n");
1014 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1017 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1018 char b
[BDEVNAME_SIZE
];
1019 tmp
= conf
->mirrors
+ i
;
1021 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1022 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1023 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1024 bdevname(tmp
->rdev
->bdev
,b
));
1028 static void close_sync(conf_t
*conf
)
1031 allow_barrier(conf
);
1033 mempool_destroy(conf
->r10buf_pool
);
1034 conf
->r10buf_pool
= NULL
;
1037 /* check if there are enough drives for
1038 * every block to appear on atleast one
1040 static int enough(conf_t
*conf
)
1045 int n
= conf
->copies
;
1048 if (conf
->mirrors
[first
].rdev
)
1050 first
= (first
+1) % conf
->raid_disks
;
1054 } while (first
!= 0);
1058 static int raid10_spare_active(mddev_t
*mddev
)
1061 conf_t
*conf
= mddev
->private;
1064 unsigned long flags
;
1067 * Find all non-in_sync disks within the RAID10 configuration
1068 * and mark them in_sync
1070 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1071 tmp
= conf
->mirrors
+ i
;
1073 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1074 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1076 sysfs_notify_dirent(tmp
->rdev
->sysfs_state
);
1079 spin_lock_irqsave(&conf
->device_lock
, flags
);
1080 mddev
->degraded
-= count
;
1081 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1088 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1090 conf_t
*conf
= mddev
->private;
1095 int last
= conf
->raid_disks
- 1;
1097 if (mddev
->recovery_cp
< MaxSector
)
1098 /* only hot-add to in-sync arrays, as recovery is
1099 * very different from resync
1105 if (rdev
->raid_disk
>= 0)
1106 first
= last
= rdev
->raid_disk
;
1108 if (rdev
->saved_raid_disk
>= first
&&
1109 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1110 mirror
= rdev
->saved_raid_disk
;
1113 for ( ; mirror
<= last
; mirror
++)
1114 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1116 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1117 rdev
->data_offset
<< 9);
1118 /* as we don't honour merge_bvec_fn, we must
1119 * never risk violating it, so limit
1120 * ->max_segments to one lying with a single
1121 * page, as a one page request is never in
1124 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1125 blk_queue_max_segments(mddev
->queue
, 1);
1126 blk_queue_segment_boundary(mddev
->queue
,
1127 PAGE_CACHE_SIZE
- 1);
1130 p
->head_position
= 0;
1131 rdev
->raid_disk
= mirror
;
1133 if (rdev
->saved_raid_disk
!= mirror
)
1135 rcu_assign_pointer(p
->rdev
, rdev
);
1139 md_integrity_add_rdev(rdev
, mddev
);
1144 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1146 conf_t
*conf
= mddev
->private;
1149 mirror_info_t
*p
= conf
->mirrors
+ number
;
1154 if (test_bit(In_sync
, &rdev
->flags
) ||
1155 atomic_read(&rdev
->nr_pending
)) {
1159 /* Only remove faulty devices in recovery
1162 if (!test_bit(Faulty
, &rdev
->flags
) &&
1169 if (atomic_read(&rdev
->nr_pending
)) {
1170 /* lost the race, try later */
1175 err
= md_integrity_register(mddev
);
1184 static void end_sync_read(struct bio
*bio
, int error
)
1186 r10bio_t
*r10_bio
= bio
->bi_private
;
1187 conf_t
*conf
= r10_bio
->mddev
->private;
1190 d
= find_bio_disk(conf
, r10_bio
, bio
);
1192 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1193 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1195 atomic_add(r10_bio
->sectors
,
1196 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1197 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1198 md_error(r10_bio
->mddev
,
1199 conf
->mirrors
[d
].rdev
);
1202 /* for reconstruct, we always reschedule after a read.
1203 * for resync, only after all reads
1205 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1206 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1207 atomic_dec_and_test(&r10_bio
->remaining
)) {
1208 /* we have read all the blocks,
1209 * do the comparison in process context in raid10d
1211 reschedule_retry(r10_bio
);
1215 static void end_sync_write(struct bio
*bio
, int error
)
1217 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1218 r10bio_t
*r10_bio
= bio
->bi_private
;
1219 mddev_t
*mddev
= r10_bio
->mddev
;
1220 conf_t
*conf
= mddev
->private;
1223 d
= find_bio_disk(conf
, r10_bio
, bio
);
1226 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1228 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1229 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1230 if (r10_bio
->master_bio
== NULL
) {
1231 /* the primary of several recovery bios */
1232 sector_t s
= r10_bio
->sectors
;
1234 md_done_sync(mddev
, s
, 1);
1237 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1245 * Note: sync and recover and handled very differently for raid10
1246 * This code is for resync.
1247 * For resync, we read through virtual addresses and read all blocks.
1248 * If there is any error, we schedule a write. The lowest numbered
1249 * drive is authoritative.
1250 * However requests come for physical address, so we need to map.
1251 * For every physical address there are raid_disks/copies virtual addresses,
1252 * which is always are least one, but is not necessarly an integer.
1253 * This means that a physical address can span multiple chunks, so we may
1254 * have to submit multiple io requests for a single sync request.
1257 * We check if all blocks are in-sync and only write to blocks that
1260 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1262 conf_t
*conf
= mddev
->private;
1264 struct bio
*tbio
, *fbio
;
1266 atomic_set(&r10_bio
->remaining
, 1);
1268 /* find the first device with a block */
1269 for (i
=0; i
<conf
->copies
; i
++)
1270 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1273 if (i
== conf
->copies
)
1277 fbio
= r10_bio
->devs
[i
].bio
;
1279 /* now find blocks with errors */
1280 for (i
=0 ; i
< conf
->copies
; i
++) {
1282 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1284 tbio
= r10_bio
->devs
[i
].bio
;
1286 if (tbio
->bi_end_io
!= end_sync_read
)
1290 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1291 /* We know that the bi_io_vec layout is the same for
1292 * both 'first' and 'i', so we just compare them.
1293 * All vec entries are PAGE_SIZE;
1295 for (j
= 0; j
< vcnt
; j
++)
1296 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1297 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1302 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1304 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1305 /* Don't fix anything. */
1307 /* Ok, we need to write this bio
1308 * First we need to fixup bv_offset, bv_len and
1309 * bi_vecs, as the read request might have corrupted these
1311 tbio
->bi_vcnt
= vcnt
;
1312 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1314 tbio
->bi_phys_segments
= 0;
1315 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1316 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1317 tbio
->bi_next
= NULL
;
1318 tbio
->bi_rw
= WRITE
;
1319 tbio
->bi_private
= r10_bio
;
1320 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1322 for (j
=0; j
< vcnt
; j
++) {
1323 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1324 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1326 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1327 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1330 tbio
->bi_end_io
= end_sync_write
;
1332 d
= r10_bio
->devs
[i
].devnum
;
1333 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1334 atomic_inc(&r10_bio
->remaining
);
1335 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1337 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1338 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1339 generic_make_request(tbio
);
1343 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1344 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1350 * Now for the recovery code.
1351 * Recovery happens across physical sectors.
1352 * We recover all non-is_sync drives by finding the virtual address of
1353 * each, and then choose a working drive that also has that virt address.
1354 * There is a separate r10_bio for each non-in_sync drive.
1355 * Only the first two slots are in use. The first for reading,
1356 * The second for writing.
1360 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1362 conf_t
*conf
= mddev
->private;
1364 struct bio
*bio
, *wbio
;
1367 /* move the pages across to the second bio
1368 * and submit the write request
1370 bio
= r10_bio
->devs
[0].bio
;
1371 wbio
= r10_bio
->devs
[1].bio
;
1372 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1373 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1374 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1375 wbio
->bi_io_vec
[i
].bv_page
= p
;
1377 d
= r10_bio
->devs
[1].devnum
;
1379 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1380 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1381 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1382 generic_make_request(wbio
);
1384 bio_endio(wbio
, -EIO
);
1389 * Used by fix_read_error() to decay the per rdev read_errors.
1390 * We halve the read error count for every hour that has elapsed
1391 * since the last recorded read error.
1394 static void check_decay_read_errors(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1396 struct timespec cur_time_mon
;
1397 unsigned long hours_since_last
;
1398 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
1400 ktime_get_ts(&cur_time_mon
);
1402 if (rdev
->last_read_error
.tv_sec
== 0 &&
1403 rdev
->last_read_error
.tv_nsec
== 0) {
1404 /* first time we've seen a read error */
1405 rdev
->last_read_error
= cur_time_mon
;
1409 hours_since_last
= (cur_time_mon
.tv_sec
-
1410 rdev
->last_read_error
.tv_sec
) / 3600;
1412 rdev
->last_read_error
= cur_time_mon
;
1415 * if hours_since_last is > the number of bits in read_errors
1416 * just set read errors to 0. We do this to avoid
1417 * overflowing the shift of read_errors by hours_since_last.
1419 if (hours_since_last
>= 8 * sizeof(read_errors
))
1420 atomic_set(&rdev
->read_errors
, 0);
1422 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
1426 * This is a kernel thread which:
1428 * 1. Retries failed read operations on working mirrors.
1429 * 2. Updates the raid superblock when problems encounter.
1430 * 3. Performs writes following reads for array synchronising.
1433 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1435 int sect
= 0; /* Offset from r10_bio->sector */
1436 int sectors
= r10_bio
->sectors
;
1438 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
1439 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1441 /* still own a reference to this rdev, so it cannot
1442 * have been cleared recently.
1444 rdev
= conf
->mirrors
[d
].rdev
;
1446 if (test_bit(Faulty
, &rdev
->flags
))
1447 /* drive has already been failed, just ignore any
1448 more fix_read_error() attempts */
1451 check_decay_read_errors(mddev
, rdev
);
1452 atomic_inc(&rdev
->read_errors
);
1453 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
1454 char b
[BDEVNAME_SIZE
];
1455 bdevname(rdev
->bdev
, b
);
1458 "md/raid10:%s: %s: Raid device exceeded "
1459 "read_error threshold [cur %d:max %d]\n",
1461 atomic_read(&rdev
->read_errors
), max_read_errors
);
1463 "md/raid10:%s: %s: Failing raid device\n",
1465 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1471 int sl
= r10_bio
->read_slot
;
1475 if (s
> (PAGE_SIZE
>>9))
1480 d
= r10_bio
->devs
[sl
].devnum
;
1481 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1483 test_bit(In_sync
, &rdev
->flags
)) {
1484 atomic_inc(&rdev
->nr_pending
);
1486 success
= sync_page_io(rdev
,
1487 r10_bio
->devs
[sl
].addr
+
1490 conf
->tmppage
, READ
, false);
1491 rdev_dec_pending(rdev
, mddev
);
1497 if (sl
== conf
->copies
)
1499 } while (!success
&& sl
!= r10_bio
->read_slot
);
1503 /* Cannot read from anywhere -- bye bye array */
1504 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1505 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1510 /* write it back and re-read */
1512 while (sl
!= r10_bio
->read_slot
) {
1513 char b
[BDEVNAME_SIZE
];
1518 d
= r10_bio
->devs
[sl
].devnum
;
1519 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1521 test_bit(In_sync
, &rdev
->flags
)) {
1522 atomic_inc(&rdev
->nr_pending
);
1524 atomic_add(s
, &rdev
->corrected_errors
);
1525 if (sync_page_io(rdev
,
1526 r10_bio
->devs
[sl
].addr
+
1528 s
<<9, conf
->tmppage
, WRITE
, false)
1530 /* Well, this device is dead */
1532 "md/raid10:%s: read correction "
1534 " (%d sectors at %llu on %s)\n",
1536 (unsigned long long)(
1537 sect
+ rdev
->data_offset
),
1538 bdevname(rdev
->bdev
, b
));
1539 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1542 bdevname(rdev
->bdev
, b
));
1543 md_error(mddev
, rdev
);
1545 rdev_dec_pending(rdev
, mddev
);
1550 while (sl
!= r10_bio
->read_slot
) {
1555 d
= r10_bio
->devs
[sl
].devnum
;
1556 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1558 test_bit(In_sync
, &rdev
->flags
)) {
1559 char b
[BDEVNAME_SIZE
];
1560 atomic_inc(&rdev
->nr_pending
);
1562 if (sync_page_io(rdev
,
1563 r10_bio
->devs
[sl
].addr
+
1565 s
<<9, conf
->tmppage
,
1566 READ
, false) == 0) {
1567 /* Well, this device is dead */
1569 "md/raid10:%s: unable to read back "
1571 " (%d sectors at %llu on %s)\n",
1573 (unsigned long long)(
1574 sect
+ rdev
->data_offset
),
1575 bdevname(rdev
->bdev
, b
));
1576 printk(KERN_NOTICE
"md/raid10:%s: %s: failing drive\n",
1578 bdevname(rdev
->bdev
, b
));
1580 md_error(mddev
, rdev
);
1583 "md/raid10:%s: read error corrected"
1584 " (%d sectors at %llu on %s)\n",
1586 (unsigned long long)(
1587 sect
+ rdev
->data_offset
),
1588 bdevname(rdev
->bdev
, b
));
1591 rdev_dec_pending(rdev
, mddev
);
1602 static void raid10d(mddev_t
*mddev
)
1606 unsigned long flags
;
1607 conf_t
*conf
= mddev
->private;
1608 struct list_head
*head
= &conf
->retry_list
;
1610 struct blk_plug plug
;
1612 md_check_recovery(mddev
);
1614 blk_start_plug(&plug
);
1616 char b
[BDEVNAME_SIZE
];
1618 flush_pending_writes(conf
);
1620 spin_lock_irqsave(&conf
->device_lock
, flags
);
1621 if (list_empty(head
)) {
1622 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1625 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1626 list_del(head
->prev
);
1628 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1630 mddev
= r10_bio
->mddev
;
1631 conf
= mddev
->private;
1632 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
1633 sync_request_write(mddev
, r10_bio
);
1634 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
1635 recovery_request_write(mddev
, r10_bio
);
1637 int slot
= r10_bio
->read_slot
;
1638 int mirror
= r10_bio
->devs
[slot
].devnum
;
1639 /* we got a read error. Maybe the drive is bad. Maybe just
1640 * the block and we can fix it.
1641 * We freeze all other IO, and try reading the block from
1642 * other devices. When we find one, we re-write
1643 * and check it that fixes the read error.
1644 * This is all done synchronously while the array is
1647 if (mddev
->ro
== 0) {
1649 fix_read_error(conf
, mddev
, r10_bio
);
1650 unfreeze_array(conf
);
1652 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, mddev
);
1654 bio
= r10_bio
->devs
[slot
].bio
;
1655 r10_bio
->devs
[slot
].bio
=
1656 mddev
->ro
? IO_BLOCKED
: NULL
;
1657 mirror
= read_balance(conf
, r10_bio
);
1659 printk(KERN_ALERT
"md/raid10:%s: %s: unrecoverable I/O"
1660 " read error for block %llu\n",
1662 bdevname(bio
->bi_bdev
,b
),
1663 (unsigned long long)r10_bio
->sector
);
1664 raid_end_bio_io(r10_bio
);
1667 const unsigned long do_sync
= (r10_bio
->master_bio
->bi_rw
& REQ_SYNC
);
1669 slot
= r10_bio
->read_slot
;
1670 rdev
= conf
->mirrors
[mirror
].rdev
;
1671 if (printk_ratelimit())
1672 printk(KERN_ERR
"md/raid10:%s: %s: redirecting sector %llu to"
1673 " another mirror\n",
1675 bdevname(rdev
->bdev
,b
),
1676 (unsigned long long)r10_bio
->sector
);
1677 bio
= bio_clone_mddev(r10_bio
->master_bio
,
1679 r10_bio
->devs
[slot
].bio
= bio
;
1680 bio
->bi_sector
= r10_bio
->devs
[slot
].addr
1681 + rdev
->data_offset
;
1682 bio
->bi_bdev
= rdev
->bdev
;
1683 bio
->bi_rw
= READ
| do_sync
;
1684 bio
->bi_private
= r10_bio
;
1685 bio
->bi_end_io
= raid10_end_read_request
;
1686 generic_make_request(bio
);
1691 blk_finish_plug(&plug
);
1695 static int init_resync(conf_t
*conf
)
1699 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1700 BUG_ON(conf
->r10buf_pool
);
1701 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1702 if (!conf
->r10buf_pool
)
1704 conf
->next_resync
= 0;
1709 * perform a "sync" on one "block"
1711 * We need to make sure that no normal I/O request - particularly write
1712 * requests - conflict with active sync requests.
1714 * This is achieved by tracking pending requests and a 'barrier' concept
1715 * that can be installed to exclude normal IO requests.
1717 * Resync and recovery are handled very differently.
1718 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1720 * For resync, we iterate over virtual addresses, read all copies,
1721 * and update if there are differences. If only one copy is live,
1723 * For recovery, we iterate over physical addresses, read a good
1724 * value for each non-in_sync drive, and over-write.
1726 * So, for recovery we may have several outstanding complex requests for a
1727 * given address, one for each out-of-sync device. We model this by allocating
1728 * a number of r10_bio structures, one for each out-of-sync device.
1729 * As we setup these structures, we collect all bio's together into a list
1730 * which we then process collectively to add pages, and then process again
1731 * to pass to generic_make_request.
1733 * The r10_bio structures are linked using a borrowed master_bio pointer.
1734 * This link is counted in ->remaining. When the r10_bio that points to NULL
1735 * has its remaining count decremented to 0, the whole complex operation
1740 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
,
1741 int *skipped
, int go_faster
)
1743 conf_t
*conf
= mddev
->private;
1745 struct bio
*biolist
= NULL
, *bio
;
1746 sector_t max_sector
, nr_sectors
;
1749 sector_t sync_blocks
;
1751 sector_t sectors_skipped
= 0;
1752 int chunks_skipped
= 0;
1754 if (!conf
->r10buf_pool
)
1755 if (init_resync(conf
))
1759 max_sector
= mddev
->dev_sectors
;
1760 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1761 max_sector
= mddev
->resync_max_sectors
;
1762 if (sector_nr
>= max_sector
) {
1763 /* If we aborted, we need to abort the
1764 * sync on the 'current' bitmap chucks (there can
1765 * be several when recovering multiple devices).
1766 * as we may have started syncing it but not finished.
1767 * We can find the current address in
1768 * mddev->curr_resync, but for recovery,
1769 * we need to convert that to several
1770 * virtual addresses.
1772 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1773 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1774 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1776 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1778 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1779 bitmap_end_sync(mddev
->bitmap
, sect
,
1782 } else /* completed sync */
1785 bitmap_close_sync(mddev
->bitmap
);
1788 return sectors_skipped
;
1790 if (chunks_skipped
>= conf
->raid_disks
) {
1791 /* if there has been nothing to do on any drive,
1792 * then there is nothing to do at all..
1795 return (max_sector
- sector_nr
) + sectors_skipped
;
1798 if (max_sector
> mddev
->resync_max
)
1799 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
1801 /* make sure whole request will fit in a chunk - if chunks
1804 if (conf
->near_copies
< conf
->raid_disks
&&
1805 max_sector
> (sector_nr
| conf
->chunk_mask
))
1806 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1808 * If there is non-resync activity waiting for us then
1809 * put in a delay to throttle resync.
1811 if (!go_faster
&& conf
->nr_waiting
)
1812 msleep_interruptible(1000);
1814 /* Again, very different code for resync and recovery.
1815 * Both must result in an r10bio with a list of bios that
1816 * have bi_end_io, bi_sector, bi_bdev set,
1817 * and bi_private set to the r10bio.
1818 * For recovery, we may actually create several r10bios
1819 * with 2 bios in each, that correspond to the bios in the main one.
1820 * In this case, the subordinate r10bios link back through a
1821 * borrowed master_bio pointer, and the counter in the master
1822 * includes a ref from each subordinate.
1824 /* First, we decide what to do and set ->bi_end_io
1825 * To end_sync_read if we want to read, and
1826 * end_sync_write if we will want to write.
1829 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
1830 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1831 /* recovery... the complicated one */
1835 for (i
=0 ; i
<conf
->raid_disks
; i
++) {
1841 if (conf
->mirrors
[i
].rdev
== NULL
||
1842 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
))
1846 /* want to reconstruct this device */
1848 sect
= raid10_find_virt(conf
, sector_nr
, i
);
1849 /* Unless we are doing a full sync, we only need
1850 * to recover the block if it is set in the bitmap
1852 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1854 if (sync_blocks
< max_sync
)
1855 max_sync
= sync_blocks
;
1858 /* yep, skip the sync_blocks here, but don't assume
1859 * that there will never be anything to do here
1861 chunks_skipped
= -1;
1865 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1866 raise_barrier(conf
, rb2
!= NULL
);
1867 atomic_set(&r10_bio
->remaining
, 0);
1869 r10_bio
->master_bio
= (struct bio
*)rb2
;
1871 atomic_inc(&rb2
->remaining
);
1872 r10_bio
->mddev
= mddev
;
1873 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1874 r10_bio
->sector
= sect
;
1876 raid10_find_phys(conf
, r10_bio
);
1878 /* Need to check if the array will still be
1881 for (j
=0; j
<conf
->raid_disks
; j
++)
1882 if (conf
->mirrors
[j
].rdev
== NULL
||
1883 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
1888 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1889 &sync_blocks
, still_degraded
);
1891 for (j
=0; j
<conf
->copies
;j
++) {
1892 int d
= r10_bio
->devs
[j
].devnum
;
1893 if (!conf
->mirrors
[d
].rdev
||
1894 !test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
))
1896 /* This is where we read from */
1897 bio
= r10_bio
->devs
[0].bio
;
1898 bio
->bi_next
= biolist
;
1900 bio
->bi_private
= r10_bio
;
1901 bio
->bi_end_io
= end_sync_read
;
1903 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1904 conf
->mirrors
[d
].rdev
->data_offset
;
1905 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1906 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1907 atomic_inc(&r10_bio
->remaining
);
1908 /* and we write to 'i' */
1910 for (k
=0; k
<conf
->copies
; k
++)
1911 if (r10_bio
->devs
[k
].devnum
== i
)
1913 BUG_ON(k
== conf
->copies
);
1914 bio
= r10_bio
->devs
[1].bio
;
1915 bio
->bi_next
= biolist
;
1917 bio
->bi_private
= r10_bio
;
1918 bio
->bi_end_io
= end_sync_write
;
1920 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1921 conf
->mirrors
[i
].rdev
->data_offset
;
1922 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1924 r10_bio
->devs
[0].devnum
= d
;
1925 r10_bio
->devs
[1].devnum
= i
;
1929 if (j
== conf
->copies
) {
1930 /* Cannot recover, so abort the recovery */
1933 atomic_dec(&rb2
->remaining
);
1935 if (!test_and_set_bit(MD_RECOVERY_INTR
,
1937 printk(KERN_INFO
"md/raid10:%s: insufficient "
1938 "working devices for recovery.\n",
1943 if (biolist
== NULL
) {
1945 r10bio_t
*rb2
= r10_bio
;
1946 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
1947 rb2
->master_bio
= NULL
;
1953 /* resync. Schedule a read for every block at this virt offset */
1956 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
1958 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
1959 &sync_blocks
, mddev
->degraded
) &&
1960 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
1961 &mddev
->recovery
)) {
1962 /* We can skip this block */
1964 return sync_blocks
+ sectors_skipped
;
1966 if (sync_blocks
< max_sync
)
1967 max_sync
= sync_blocks
;
1968 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1970 r10_bio
->mddev
= mddev
;
1971 atomic_set(&r10_bio
->remaining
, 0);
1972 raise_barrier(conf
, 0);
1973 conf
->next_resync
= sector_nr
;
1975 r10_bio
->master_bio
= NULL
;
1976 r10_bio
->sector
= sector_nr
;
1977 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
1978 raid10_find_phys(conf
, r10_bio
);
1979 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
1981 for (i
=0; i
<conf
->copies
; i
++) {
1982 int d
= r10_bio
->devs
[i
].devnum
;
1983 bio
= r10_bio
->devs
[i
].bio
;
1984 bio
->bi_end_io
= NULL
;
1985 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1986 if (conf
->mirrors
[d
].rdev
== NULL
||
1987 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
1989 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1990 atomic_inc(&r10_bio
->remaining
);
1991 bio
->bi_next
= biolist
;
1993 bio
->bi_private
= r10_bio
;
1994 bio
->bi_end_io
= end_sync_read
;
1996 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
1997 conf
->mirrors
[d
].rdev
->data_offset
;
1998 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2003 for (i
=0; i
<conf
->copies
; i
++) {
2004 int d
= r10_bio
->devs
[i
].devnum
;
2005 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
2006 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
2015 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2017 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
2019 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2022 bio
->bi_phys_segments
= 0;
2027 if (sector_nr
+ max_sync
< max_sector
)
2028 max_sector
= sector_nr
+ max_sync
;
2031 int len
= PAGE_SIZE
;
2032 if (sector_nr
+ (len
>>9) > max_sector
)
2033 len
= (max_sector
- sector_nr
) << 9;
2036 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2038 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2039 if (bio_add_page(bio
, page
, len
, 0))
2043 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2044 for (bio2
= biolist
;
2045 bio2
&& bio2
!= bio
;
2046 bio2
= bio2
->bi_next
) {
2047 /* remove last page from this bio */
2049 bio2
->bi_size
-= len
;
2050 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2054 nr_sectors
+= len
>>9;
2055 sector_nr
+= len
>>9;
2056 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
2058 r10_bio
->sectors
= nr_sectors
;
2062 biolist
= biolist
->bi_next
;
2064 bio
->bi_next
= NULL
;
2065 r10_bio
= bio
->bi_private
;
2066 r10_bio
->sectors
= nr_sectors
;
2068 if (bio
->bi_end_io
== end_sync_read
) {
2069 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2070 generic_make_request(bio
);
2074 if (sectors_skipped
)
2075 /* pretend they weren't skipped, it makes
2076 * no important difference in this case
2078 md_done_sync(mddev
, sectors_skipped
, 1);
2080 return sectors_skipped
+ nr_sectors
;
2082 /* There is nowhere to write, so all non-sync
2083 * drives must be failed, so try the next chunk...
2085 if (sector_nr
+ max_sync
< max_sector
)
2086 max_sector
= sector_nr
+ max_sync
;
2088 sectors_skipped
+= (max_sector
- sector_nr
);
2090 sector_nr
= max_sector
;
2095 raid10_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2098 conf_t
*conf
= mddev
->private;
2101 raid_disks
= conf
->raid_disks
;
2103 sectors
= conf
->dev_sectors
;
2105 size
= sectors
>> conf
->chunk_shift
;
2106 sector_div(size
, conf
->far_copies
);
2107 size
= size
* raid_disks
;
2108 sector_div(size
, conf
->near_copies
);
2110 return size
<< conf
->chunk_shift
;
2114 static conf_t
*setup_conf(mddev_t
*mddev
)
2116 conf_t
*conf
= NULL
;
2118 sector_t stride
, size
;
2121 if (mddev
->new_chunk_sectors
< (PAGE_SIZE
>> 9) ||
2122 !is_power_of_2(mddev
->new_chunk_sectors
)) {
2123 printk(KERN_ERR
"md/raid10:%s: chunk size must be "
2124 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2125 mdname(mddev
), PAGE_SIZE
);
2129 nc
= mddev
->new_layout
& 255;
2130 fc
= (mddev
->new_layout
>> 8) & 255;
2131 fo
= mddev
->new_layout
& (1<<16);
2133 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2134 (mddev
->new_layout
>> 17)) {
2135 printk(KERN_ERR
"md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2136 mdname(mddev
), mddev
->new_layout
);
2141 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2145 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2150 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2155 conf
->raid_disks
= mddev
->raid_disks
;
2156 conf
->near_copies
= nc
;
2157 conf
->far_copies
= fc
;
2158 conf
->copies
= nc
*fc
;
2159 conf
->far_offset
= fo
;
2160 conf
->chunk_mask
= mddev
->new_chunk_sectors
- 1;
2161 conf
->chunk_shift
= ffz(~mddev
->new_chunk_sectors
);
2163 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2164 r10bio_pool_free
, conf
);
2165 if (!conf
->r10bio_pool
)
2168 size
= mddev
->dev_sectors
>> conf
->chunk_shift
;
2169 sector_div(size
, fc
);
2170 size
= size
* conf
->raid_disks
;
2171 sector_div(size
, nc
);
2172 /* 'size' is now the number of chunks in the array */
2173 /* calculate "used chunks per device" in 'stride' */
2174 stride
= size
* conf
->copies
;
2176 /* We need to round up when dividing by raid_disks to
2177 * get the stride size.
2179 stride
+= conf
->raid_disks
- 1;
2180 sector_div(stride
, conf
->raid_disks
);
2182 conf
->dev_sectors
= stride
<< conf
->chunk_shift
;
2187 sector_div(stride
, fc
);
2188 conf
->stride
= stride
<< conf
->chunk_shift
;
2191 spin_lock_init(&conf
->device_lock
);
2192 INIT_LIST_HEAD(&conf
->retry_list
);
2194 spin_lock_init(&conf
->resync_lock
);
2195 init_waitqueue_head(&conf
->wait_barrier
);
2197 conf
->thread
= md_register_thread(raid10d
, mddev
, NULL
);
2201 conf
->mddev
= mddev
;
2205 printk(KERN_ERR
"md/raid10:%s: couldn't allocate memory.\n",
2208 if (conf
->r10bio_pool
)
2209 mempool_destroy(conf
->r10bio_pool
);
2210 kfree(conf
->mirrors
);
2211 safe_put_page(conf
->tmppage
);
2214 return ERR_PTR(err
);
2217 static int run(mddev_t
*mddev
)
2220 int i
, disk_idx
, chunk_size
;
2221 mirror_info_t
*disk
;
2226 * copy the already verified devices into our private RAID10
2227 * bookkeeping area. [whatever we allocate in run(),
2228 * should be freed in stop()]
2231 if (mddev
->private == NULL
) {
2232 conf
= setup_conf(mddev
);
2234 return PTR_ERR(conf
);
2235 mddev
->private = conf
;
2237 conf
= mddev
->private;
2241 mddev
->thread
= conf
->thread
;
2242 conf
->thread
= NULL
;
2244 chunk_size
= mddev
->chunk_sectors
<< 9;
2245 blk_queue_io_min(mddev
->queue
, chunk_size
);
2246 if (conf
->raid_disks
% conf
->near_copies
)
2247 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->raid_disks
);
2249 blk_queue_io_opt(mddev
->queue
, chunk_size
*
2250 (conf
->raid_disks
/ conf
->near_copies
));
2252 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2253 disk_idx
= rdev
->raid_disk
;
2254 if (disk_idx
>= conf
->raid_disks
2257 disk
= conf
->mirrors
+ disk_idx
;
2260 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2261 rdev
->data_offset
<< 9);
2262 /* as we don't honour merge_bvec_fn, we must never risk
2263 * violating it, so limit max_segments to 1 lying
2264 * within a single page.
2266 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2267 blk_queue_max_segments(mddev
->queue
, 1);
2268 blk_queue_segment_boundary(mddev
->queue
,
2269 PAGE_CACHE_SIZE
- 1);
2272 disk
->head_position
= 0;
2274 /* need to check that every block has at least one working mirror */
2275 if (!enough(conf
)) {
2276 printk(KERN_ERR
"md/raid10:%s: not enough operational mirrors.\n",
2281 mddev
->degraded
= 0;
2282 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2284 disk
= conf
->mirrors
+ i
;
2287 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2288 disk
->head_position
= 0;
2295 if (mddev
->recovery_cp
!= MaxSector
)
2296 printk(KERN_NOTICE
"md/raid10:%s: not clean"
2297 " -- starting background reconstruction\n",
2300 "md/raid10:%s: active with %d out of %d devices\n",
2301 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
,
2304 * Ok, everything is just fine now
2306 mddev
->dev_sectors
= conf
->dev_sectors
;
2307 size
= raid10_size(mddev
, 0, 0);
2308 md_set_array_sectors(mddev
, size
);
2309 mddev
->resync_max_sectors
= size
;
2311 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2312 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2314 /* Calculate max read-ahead size.
2315 * We need to readahead at least twice a whole stripe....
2319 int stripe
= conf
->raid_disks
*
2320 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
2321 stripe
/= conf
->near_copies
;
2322 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2323 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2326 if (conf
->near_copies
< conf
->raid_disks
)
2327 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2329 if (md_integrity_register(mddev
))
2335 md_unregister_thread(mddev
->thread
);
2336 if (conf
->r10bio_pool
)
2337 mempool_destroy(conf
->r10bio_pool
);
2338 safe_put_page(conf
->tmppage
);
2339 kfree(conf
->mirrors
);
2341 mddev
->private = NULL
;
2346 static int stop(mddev_t
*mddev
)
2348 conf_t
*conf
= mddev
->private;
2350 raise_barrier(conf
, 0);
2351 lower_barrier(conf
);
2353 md_unregister_thread(mddev
->thread
);
2354 mddev
->thread
= NULL
;
2355 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2356 if (conf
->r10bio_pool
)
2357 mempool_destroy(conf
->r10bio_pool
);
2358 kfree(conf
->mirrors
);
2360 mddev
->private = NULL
;
2364 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2366 conf_t
*conf
= mddev
->private;
2370 raise_barrier(conf
, 0);
2373 lower_barrier(conf
);
2378 static void *raid10_takeover_raid0(mddev_t
*mddev
)
2383 if (mddev
->degraded
> 0) {
2384 printk(KERN_ERR
"md/raid10:%s: Error: degraded raid0!\n",
2386 return ERR_PTR(-EINVAL
);
2389 /* Set new parameters */
2390 mddev
->new_level
= 10;
2391 /* new layout: far_copies = 1, near_copies = 2 */
2392 mddev
->new_layout
= (1<<8) + 2;
2393 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2394 mddev
->delta_disks
= mddev
->raid_disks
;
2395 mddev
->raid_disks
*= 2;
2396 /* make sure it will be not marked as dirty */
2397 mddev
->recovery_cp
= MaxSector
;
2399 conf
= setup_conf(mddev
);
2400 if (!IS_ERR(conf
)) {
2401 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2402 if (rdev
->raid_disk
>= 0)
2403 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
2410 static void *raid10_takeover(mddev_t
*mddev
)
2412 struct raid0_private_data
*raid0_priv
;
2414 /* raid10 can take over:
2415 * raid0 - providing it has only two drives
2417 if (mddev
->level
== 0) {
2418 /* for raid0 takeover only one zone is supported */
2419 raid0_priv
= mddev
->private;
2420 if (raid0_priv
->nr_strip_zones
> 1) {
2421 printk(KERN_ERR
"md/raid10:%s: cannot takeover raid 0"
2422 " with more than one zone.\n",
2424 return ERR_PTR(-EINVAL
);
2426 return raid10_takeover_raid0(mddev
);
2428 return ERR_PTR(-EINVAL
);
2431 static struct mdk_personality raid10_personality
=
2435 .owner
= THIS_MODULE
,
2436 .make_request
= make_request
,
2440 .error_handler
= error
,
2441 .hot_add_disk
= raid10_add_disk
,
2442 .hot_remove_disk
= raid10_remove_disk
,
2443 .spare_active
= raid10_spare_active
,
2444 .sync_request
= sync_request
,
2445 .quiesce
= raid10_quiesce
,
2446 .size
= raid10_size
,
2447 .takeover
= raid10_takeover
,
2450 static int __init
raid_init(void)
2452 return register_md_personality(&raid10_personality
);
2455 static void raid_exit(void)
2457 unregister_md_personality(&raid10_personality
);
2460 module_init(raid_init
);
2461 module_exit(raid_exit
);
2462 MODULE_LICENSE("GPL");
2463 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2464 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2465 MODULE_ALIAS("md-raid10");
2466 MODULE_ALIAS("md-level-10");