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 futher 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/delay.h>
22 #include <linux/blkdev.h>
23 #include <linux/seq_file.h>
29 * RAID10 provides a combination of RAID0 and RAID1 functionality.
30 * The layout of data is defined by
33 * near_copies (stored in low byte of layout)
34 * far_copies (stored in second byte of layout)
35 * far_offset (stored in bit 16 of layout )
37 * The data to be stored is divided into chunks using chunksize.
38 * Each device is divided into far_copies sections.
39 * In each section, chunks are laid out in a style similar to raid0, but
40 * near_copies copies of each chunk is stored (each on a different drive).
41 * The starting device for each section is offset near_copies from the starting
42 * device of the previous section.
43 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
45 * near_copies and far_copies must be at least one, and their product is at most
48 * If far_offset is true, then the far_copies are handled a bit differently.
49 * The copies are still in different stripes, but instead of be very far apart
50 * on disk, there are adjacent stripes.
54 * Number of guaranteed r10bios in case of extreme VM load:
56 #define NR_RAID10_BIOS 256
58 static void unplug_slaves(mddev_t
*mddev
);
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
)
67 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
70 r10_bio
= kzalloc(size
, gfp_flags
);
71 if (!r10_bio
&& conf
->mddev
)
72 unplug_slaves(conf
->mddev
);
77 static void r10bio_pool_free(void *r10_bio
, void *data
)
82 /* Maximum size of each resync request */
83 #define RESYNC_BLOCK_SIZE (64*1024)
84 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
85 /* amount of memory to reserve for resync requests */
86 #define RESYNC_WINDOW (1024*1024)
87 /* maximum number of concurrent requests, memory permitting */
88 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
91 * When performing a resync, we need to read and compare, so
92 * we need as many pages are there are copies.
93 * When performing a recovery, we need 2 bios, one for read,
94 * one for write (we recover only one drive per r10buf)
97 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
106 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
108 unplug_slaves(conf
->mddev
);
112 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
113 nalloc
= conf
->copies
; /* resync */
115 nalloc
= 2; /* recovery */
120 for (j
= nalloc
; j
-- ; ) {
121 bio
= bio_alloc(gfp_flags
, RESYNC_PAGES
);
124 r10_bio
->devs
[j
].bio
= bio
;
127 * Allocate RESYNC_PAGES data pages and attach them
130 for (j
= 0 ; j
< nalloc
; j
++) {
131 bio
= r10_bio
->devs
[j
].bio
;
132 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
133 page
= alloc_page(gfp_flags
);
137 bio
->bi_io_vec
[i
].bv_page
= page
;
145 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
147 for (i
= 0; i
< RESYNC_PAGES
; i
++)
148 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
151 while ( ++j
< nalloc
)
152 bio_put(r10_bio
->devs
[j
].bio
);
153 r10bio_pool_free(r10_bio
, conf
);
157 static void r10buf_pool_free(void *__r10_bio
, void *data
)
161 r10bio_t
*r10bio
= __r10_bio
;
164 for (j
=0; j
< conf
->copies
; j
++) {
165 struct bio
*bio
= r10bio
->devs
[j
].bio
;
167 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
168 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
169 bio
->bi_io_vec
[i
].bv_page
= NULL
;
174 r10bio_pool_free(r10bio
, conf
);
177 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
181 for (i
= 0; i
< conf
->copies
; i
++) {
182 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
183 if (*bio
&& *bio
!= IO_BLOCKED
)
189 static void free_r10bio(r10bio_t
*r10_bio
)
191 conf_t
*conf
= r10_bio
->mddev
->private;
194 * Wake up any possible resync thread that waits for the device
199 put_all_bios(conf
, r10_bio
);
200 mempool_free(r10_bio
, conf
->r10bio_pool
);
203 static void put_buf(r10bio_t
*r10_bio
)
205 conf_t
*conf
= r10_bio
->mddev
->private;
207 mempool_free(r10_bio
, conf
->r10buf_pool
);
212 static void reschedule_retry(r10bio_t
*r10_bio
)
215 mddev_t
*mddev
= r10_bio
->mddev
;
216 conf_t
*conf
= mddev
->private;
218 spin_lock_irqsave(&conf
->device_lock
, flags
);
219 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
221 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
223 /* wake up frozen array... */
224 wake_up(&conf
->wait_barrier
);
226 md_wakeup_thread(mddev
->thread
);
230 * raid_end_bio_io() is called when we have finished servicing a mirrored
231 * operation and are ready to return a success/failure code to the buffer
234 static void raid_end_bio_io(r10bio_t
*r10_bio
)
236 struct bio
*bio
= r10_bio
->master_bio
;
239 test_bit(R10BIO_Uptodate
, &r10_bio
->state
) ? 0 : -EIO
);
240 free_r10bio(r10_bio
);
244 * Update disk head position estimator based on IRQ completion info.
246 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
248 conf_t
*conf
= r10_bio
->mddev
->private;
250 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
251 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
254 static void raid10_end_read_request(struct bio
*bio
, int error
)
256 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
257 r10bio_t
*r10_bio
= bio
->bi_private
;
259 conf_t
*conf
= r10_bio
->mddev
->private;
262 slot
= r10_bio
->read_slot
;
263 dev
= r10_bio
->devs
[slot
].devnum
;
265 * this branch is our 'one mirror IO has finished' event handler:
267 update_head_pos(slot
, r10_bio
);
271 * Set R10BIO_Uptodate in our master bio, so that
272 * we will return a good error code to the higher
273 * levels even if IO on some other mirrored buffer fails.
275 * The 'master' represents the composite IO operation to
276 * user-side. So if something waits for IO, then it will
277 * wait for the 'master' bio.
279 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
280 raid_end_bio_io(r10_bio
);
285 char b
[BDEVNAME_SIZE
];
286 if (printk_ratelimit())
287 printk(KERN_ERR
"raid10: %s: rescheduling sector %llu\n",
288 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
289 reschedule_retry(r10_bio
);
292 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
295 static void raid10_end_write_request(struct bio
*bio
, int error
)
297 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
298 r10bio_t
*r10_bio
= bio
->bi_private
;
300 conf_t
*conf
= r10_bio
->mddev
->private;
302 for (slot
= 0; slot
< conf
->copies
; slot
++)
303 if (r10_bio
->devs
[slot
].bio
== bio
)
305 dev
= r10_bio
->devs
[slot
].devnum
;
308 * this branch is our 'one mirror IO has finished' event handler:
311 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
312 /* an I/O failed, we can't clear the bitmap */
313 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
316 * Set R10BIO_Uptodate in our master bio, so that
317 * we will return a good error code for to the higher
318 * levels even if IO on some other mirrored buffer fails.
320 * The 'master' represents the composite IO operation to
321 * user-side. So if something waits for IO, then it will
322 * wait for the 'master' bio.
324 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
326 update_head_pos(slot
, r10_bio
);
330 * Let's see if all mirrored write operations have finished
333 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
334 /* clear the bitmap if all writes complete successfully */
335 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
337 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
339 md_write_end(r10_bio
->mddev
);
340 raid_end_bio_io(r10_bio
);
343 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
348 * RAID10 layout manager
349 * Aswell as the chunksize and raid_disks count, there are two
350 * parameters: near_copies and far_copies.
351 * near_copies * far_copies must be <= raid_disks.
352 * Normally one of these will be 1.
353 * If both are 1, we get raid0.
354 * If near_copies == raid_disks, we get raid1.
356 * Chunks are layed out in raid0 style with near_copies copies of the
357 * first chunk, followed by near_copies copies of the next chunk and
359 * If far_copies > 1, then after 1/far_copies of the array has been assigned
360 * as described above, we start again with a device offset of near_copies.
361 * So we effectively have another copy of the whole array further down all
362 * the drives, but with blocks on different drives.
363 * With this layout, and block is never stored twice on the one device.
365 * raid10_find_phys finds the sector offset of a given virtual sector
366 * on each device that it is on.
368 * raid10_find_virt does the reverse mapping, from a device and a
369 * sector offset to a virtual address
372 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
382 /* now calculate first sector/dev */
383 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
384 sector
= r10bio
->sector
& conf
->chunk_mask
;
386 chunk
*= conf
->near_copies
;
388 dev
= sector_div(stripe
, conf
->raid_disks
);
389 if (conf
->far_offset
)
390 stripe
*= conf
->far_copies
;
392 sector
+= stripe
<< conf
->chunk_shift
;
394 /* and calculate all the others */
395 for (n
=0; n
< conf
->near_copies
; n
++) {
398 r10bio
->devs
[slot
].addr
= sector
;
399 r10bio
->devs
[slot
].devnum
= d
;
402 for (f
= 1; f
< conf
->far_copies
; f
++) {
403 d
+= conf
->near_copies
;
404 if (d
>= conf
->raid_disks
)
405 d
-= conf
->raid_disks
;
407 r10bio
->devs
[slot
].devnum
= d
;
408 r10bio
->devs
[slot
].addr
= s
;
412 if (dev
>= conf
->raid_disks
) {
414 sector
+= (conf
->chunk_mask
+ 1);
417 BUG_ON(slot
!= conf
->copies
);
420 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
422 sector_t offset
, chunk
, vchunk
;
424 offset
= sector
& conf
->chunk_mask
;
425 if (conf
->far_offset
) {
427 chunk
= sector
>> conf
->chunk_shift
;
428 fc
= sector_div(chunk
, conf
->far_copies
);
429 dev
-= fc
* conf
->near_copies
;
431 dev
+= conf
->raid_disks
;
433 while (sector
>= conf
->stride
) {
434 sector
-= conf
->stride
;
435 if (dev
< conf
->near_copies
)
436 dev
+= conf
->raid_disks
- conf
->near_copies
;
438 dev
-= conf
->near_copies
;
440 chunk
= sector
>> conf
->chunk_shift
;
442 vchunk
= chunk
* conf
->raid_disks
+ dev
;
443 sector_div(vchunk
, conf
->near_copies
);
444 return (vchunk
<< conf
->chunk_shift
) + offset
;
448 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
450 * @bvm: properties of new bio
451 * @biovec: the request that could be merged to it.
453 * Return amount of bytes we can accept at this offset
454 * If near_copies == raid_disk, there are no striping issues,
455 * but in that case, the function isn't called at all.
457 static int raid10_mergeable_bvec(struct request_queue
*q
,
458 struct bvec_merge_data
*bvm
,
459 struct bio_vec
*biovec
)
461 mddev_t
*mddev
= q
->queuedata
;
462 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
464 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
465 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
467 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
468 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
469 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
470 return biovec
->bv_len
;
476 * This routine returns the disk from which the requested read should
477 * be done. There is a per-array 'next expected sequential IO' sector
478 * number - if this matches on the next IO then we use the last disk.
479 * There is also a per-disk 'last know head position' sector that is
480 * maintained from IRQ contexts, both the normal and the resync IO
481 * completion handlers update this position correctly. If there is no
482 * perfect sequential match then we pick the disk whose head is closest.
484 * If there are 2 mirrors in the same 2 devices, performance degrades
485 * because position is mirror, not device based.
487 * The rdev for the device selected will have nr_pending incremented.
491 * FIXME: possibly should rethink readbalancing and do it differently
492 * depending on near_copies / far_copies geometry.
494 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
496 const unsigned long this_sector
= r10_bio
->sector
;
497 int disk
, slot
, nslot
;
498 const int sectors
= r10_bio
->sectors
;
499 sector_t new_distance
, current_distance
;
502 raid10_find_phys(conf
, r10_bio
);
505 * Check if we can balance. We can balance on the whole
506 * device if no resync is going on (recovery is ok), or below
507 * the resync window. We take the first readable disk when
508 * above the resync window.
510 if (conf
->mddev
->recovery_cp
< MaxSector
511 && (this_sector
+ sectors
>= conf
->next_resync
)) {
512 /* make sure that disk is operational */
514 disk
= r10_bio
->devs
[slot
].devnum
;
516 while ((rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
517 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
518 !test_bit(In_sync
, &rdev
->flags
)) {
520 if (slot
== conf
->copies
) {
525 disk
= r10_bio
->devs
[slot
].devnum
;
531 /* make sure the disk is operational */
533 disk
= r10_bio
->devs
[slot
].devnum
;
534 while ((rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
535 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
536 !test_bit(In_sync
, &rdev
->flags
)) {
538 if (slot
== conf
->copies
) {
542 disk
= r10_bio
->devs
[slot
].devnum
;
546 current_distance
= abs(r10_bio
->devs
[slot
].addr
-
547 conf
->mirrors
[disk
].head_position
);
549 /* Find the disk whose head is closest,
550 * or - for far > 1 - find the closest to partition beginning */
552 for (nslot
= slot
; nslot
< conf
->copies
; nslot
++) {
553 int ndisk
= r10_bio
->devs
[nslot
].devnum
;
556 if ((rdev
=rcu_dereference(conf
->mirrors
[ndisk
].rdev
)) == NULL
||
557 r10_bio
->devs
[nslot
].bio
== IO_BLOCKED
||
558 !test_bit(In_sync
, &rdev
->flags
))
561 /* This optimisation is debatable, and completely destroys
562 * sequential read speed for 'far copies' arrays. So only
563 * keep it for 'near' arrays, and review those later.
565 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
)) {
571 /* for far > 1 always use the lowest address */
572 if (conf
->far_copies
> 1)
573 new_distance
= r10_bio
->devs
[nslot
].addr
;
575 new_distance
= abs(r10_bio
->devs
[nslot
].addr
-
576 conf
->mirrors
[ndisk
].head_position
);
577 if (new_distance
< current_distance
) {
578 current_distance
= new_distance
;
585 r10_bio
->read_slot
= slot
;
586 /* conf->next_seq_sect = this_sector + sectors;*/
588 if (disk
>= 0 && (rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
))!= NULL
)
589 atomic_inc(&conf
->mirrors
[disk
].rdev
->nr_pending
);
597 static void unplug_slaves(mddev_t
*mddev
)
599 conf_t
*conf
= mddev
->private;
603 for (i
=0; i
<mddev
->raid_disks
; i
++) {
604 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
605 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
606 struct request_queue
*r_queue
= bdev_get_queue(rdev
->bdev
);
608 atomic_inc(&rdev
->nr_pending
);
613 rdev_dec_pending(rdev
, mddev
);
620 static void raid10_unplug(struct request_queue
*q
)
622 mddev_t
*mddev
= q
->queuedata
;
624 unplug_slaves(q
->queuedata
);
625 md_wakeup_thread(mddev
->thread
);
628 static int raid10_congested(void *data
, int bits
)
630 mddev_t
*mddev
= data
;
631 conf_t
*conf
= mddev
->private;
634 if (mddev_congested(mddev
, bits
))
637 for (i
= 0; i
< mddev
->raid_disks
&& ret
== 0; i
++) {
638 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
639 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
640 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
642 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
649 static int flush_pending_writes(conf_t
*conf
)
651 /* Any writes that have been queued but are awaiting
652 * bitmap updates get flushed here.
653 * We return 1 if any requests were actually submitted.
657 spin_lock_irq(&conf
->device_lock
);
659 if (conf
->pending_bio_list
.head
) {
661 bio
= bio_list_get(&conf
->pending_bio_list
);
662 blk_remove_plug(conf
->mddev
->queue
);
663 spin_unlock_irq(&conf
->device_lock
);
664 /* flush any pending bitmap writes to disk
665 * before proceeding w/ I/O */
666 bitmap_unplug(conf
->mddev
->bitmap
);
668 while (bio
) { /* submit pending writes */
669 struct bio
*next
= bio
->bi_next
;
671 generic_make_request(bio
);
676 spin_unlock_irq(&conf
->device_lock
);
680 * Sometimes we need to suspend IO while we do something else,
681 * either some resync/recovery, or reconfigure the array.
682 * To do this we raise a 'barrier'.
683 * The 'barrier' is a counter that can be raised multiple times
684 * to count how many activities are happening which preclude
686 * We can only raise the barrier if there is no pending IO.
687 * i.e. if nr_pending == 0.
688 * We choose only to raise the barrier if no-one is waiting for the
689 * barrier to go down. This means that as soon as an IO request
690 * is ready, no other operations which require a barrier will start
691 * until the IO request has had a chance.
693 * So: regular IO calls 'wait_barrier'. When that returns there
694 * is no backgroup IO happening, It must arrange to call
695 * allow_barrier when it has finished its IO.
696 * backgroup IO calls must call raise_barrier. Once that returns
697 * there is no normal IO happeing. It must arrange to call
698 * lower_barrier when the particular background IO completes.
701 static void raise_barrier(conf_t
*conf
, int force
)
703 BUG_ON(force
&& !conf
->barrier
);
704 spin_lock_irq(&conf
->resync_lock
);
706 /* Wait until no block IO is waiting (unless 'force') */
707 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
709 raid10_unplug(conf
->mddev
->queue
));
711 /* block any new IO from starting */
714 /* No wait for all pending IO to complete */
715 wait_event_lock_irq(conf
->wait_barrier
,
716 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
718 raid10_unplug(conf
->mddev
->queue
));
720 spin_unlock_irq(&conf
->resync_lock
);
723 static void lower_barrier(conf_t
*conf
)
726 spin_lock_irqsave(&conf
->resync_lock
, flags
);
728 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
729 wake_up(&conf
->wait_barrier
);
732 static void wait_barrier(conf_t
*conf
)
734 spin_lock_irq(&conf
->resync_lock
);
737 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
739 raid10_unplug(conf
->mddev
->queue
));
743 spin_unlock_irq(&conf
->resync_lock
);
746 static void allow_barrier(conf_t
*conf
)
749 spin_lock_irqsave(&conf
->resync_lock
, flags
);
751 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
752 wake_up(&conf
->wait_barrier
);
755 static void freeze_array(conf_t
*conf
)
757 /* stop syncio and normal IO and wait for everything to
759 * We increment barrier and nr_waiting, and then
760 * wait until nr_pending match nr_queued+1
761 * This is called in the context of one normal IO request
762 * that has failed. Thus any sync request that might be pending
763 * will be blocked by nr_pending, and we need to wait for
764 * pending IO requests to complete or be queued for re-try.
765 * Thus the number queued (nr_queued) plus this request (1)
766 * must match the number of pending IOs (nr_pending) before
769 spin_lock_irq(&conf
->resync_lock
);
772 wait_event_lock_irq(conf
->wait_barrier
,
773 conf
->nr_pending
== conf
->nr_queued
+1,
775 ({ flush_pending_writes(conf
);
776 raid10_unplug(conf
->mddev
->queue
); }));
777 spin_unlock_irq(&conf
->resync_lock
);
780 static void unfreeze_array(conf_t
*conf
)
782 /* reverse the effect of the freeze */
783 spin_lock_irq(&conf
->resync_lock
);
786 wake_up(&conf
->wait_barrier
);
787 spin_unlock_irq(&conf
->resync_lock
);
790 static int make_request(struct request_queue
*q
, struct bio
* bio
)
792 mddev_t
*mddev
= q
->queuedata
;
793 conf_t
*conf
= mddev
->private;
794 mirror_info_t
*mirror
;
796 struct bio
*read_bio
;
799 int chunk_sects
= conf
->chunk_mask
+ 1;
800 const int rw
= bio_data_dir(bio
);
801 const bool do_sync
= bio_rw_flagged(bio
, BIO_RW_SYNCIO
);
804 mdk_rdev_t
*blocked_rdev
;
806 if (unlikely(bio_rw_flagged(bio
, BIO_RW_BARRIER
))) {
807 md_barrier_request(mddev
, bio
);
811 /* If this request crosses a chunk boundary, we need to
812 * split it. This will only happen for 1 PAGE (or less) requests.
814 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
816 conf
->near_copies
< conf
->raid_disks
)) {
818 /* Sanity check -- queue functions should prevent this happening */
819 if (bio
->bi_vcnt
!= 1 ||
822 /* This is a one page bio that upper layers
823 * refuse to split for us, so we need to split it.
826 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
827 if (make_request(q
, &bp
->bio1
))
828 generic_make_request(&bp
->bio1
);
829 if (make_request(q
, &bp
->bio2
))
830 generic_make_request(&bp
->bio2
);
832 bio_pair_release(bp
);
835 printk("raid10_make_request bug: can't convert block across chunks"
836 " or bigger than %dk %llu %d\n", chunk_sects
/2,
837 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
843 md_write_start(mddev
, bio
);
846 * Register the new request and wait if the reconstruction
847 * thread has put up a bar for new requests.
848 * Continue immediately if no resync is active currently.
852 cpu
= part_stat_lock();
853 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
854 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
858 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
860 r10_bio
->master_bio
= bio
;
861 r10_bio
->sectors
= bio
->bi_size
>> 9;
863 r10_bio
->mddev
= mddev
;
864 r10_bio
->sector
= bio
->bi_sector
;
869 * read balancing logic:
871 int disk
= read_balance(conf
, r10_bio
);
872 int slot
= r10_bio
->read_slot
;
874 raid_end_bio_io(r10_bio
);
877 mirror
= conf
->mirrors
+ disk
;
879 read_bio
= bio_clone(bio
, GFP_NOIO
);
881 r10_bio
->devs
[slot
].bio
= read_bio
;
883 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
884 mirror
->rdev
->data_offset
;
885 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
886 read_bio
->bi_end_io
= raid10_end_read_request
;
887 read_bio
->bi_rw
= READ
| (do_sync
<< BIO_RW_SYNCIO
);
888 read_bio
->bi_private
= r10_bio
;
890 generic_make_request(read_bio
);
897 /* first select target devices under rcu_lock and
898 * inc refcount on their rdev. Record them by setting
901 raid10_find_phys(conf
, r10_bio
);
905 for (i
= 0; i
< conf
->copies
; i
++) {
906 int d
= r10_bio
->devs
[i
].devnum
;
907 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
908 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
909 atomic_inc(&rdev
->nr_pending
);
913 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
914 atomic_inc(&rdev
->nr_pending
);
915 r10_bio
->devs
[i
].bio
= bio
;
917 r10_bio
->devs
[i
].bio
= NULL
;
918 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
923 if (unlikely(blocked_rdev
)) {
924 /* Have to wait for this device to get unblocked, then retry */
928 for (j
= 0; j
< i
; j
++)
929 if (r10_bio
->devs
[j
].bio
) {
930 d
= r10_bio
->devs
[j
].devnum
;
931 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
934 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
939 atomic_set(&r10_bio
->remaining
, 0);
942 for (i
= 0; i
< conf
->copies
; i
++) {
944 int d
= r10_bio
->devs
[i
].devnum
;
945 if (!r10_bio
->devs
[i
].bio
)
948 mbio
= bio_clone(bio
, GFP_NOIO
);
949 r10_bio
->devs
[i
].bio
= mbio
;
951 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
952 conf
->mirrors
[d
].rdev
->data_offset
;
953 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
954 mbio
->bi_end_io
= raid10_end_write_request
;
955 mbio
->bi_rw
= WRITE
| (do_sync
<< BIO_RW_SYNCIO
);
956 mbio
->bi_private
= r10_bio
;
958 atomic_inc(&r10_bio
->remaining
);
959 bio_list_add(&bl
, mbio
);
962 if (unlikely(!atomic_read(&r10_bio
->remaining
))) {
963 /* the array is dead */
965 raid_end_bio_io(r10_bio
);
969 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
970 spin_lock_irqsave(&conf
->device_lock
, flags
);
971 bio_list_merge(&conf
->pending_bio_list
, &bl
);
972 blk_plug_device(mddev
->queue
);
973 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
975 /* In case raid10d snuck in to freeze_array */
976 wake_up(&conf
->wait_barrier
);
979 md_wakeup_thread(mddev
->thread
);
984 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
986 conf_t
*conf
= mddev
->private;
989 if (conf
->near_copies
< conf
->raid_disks
)
990 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
991 if (conf
->near_copies
> 1)
992 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
993 if (conf
->far_copies
> 1) {
994 if (conf
->far_offset
)
995 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
997 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
999 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1000 conf
->raid_disks
- mddev
->degraded
);
1001 for (i
= 0; i
< conf
->raid_disks
; i
++)
1002 seq_printf(seq
, "%s",
1003 conf
->mirrors
[i
].rdev
&&
1004 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
1005 seq_printf(seq
, "]");
1008 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1010 char b
[BDEVNAME_SIZE
];
1011 conf_t
*conf
= mddev
->private;
1014 * If it is not operational, then we have already marked it as dead
1015 * else if it is the last working disks, ignore the error, let the
1016 * next level up know.
1017 * else mark the drive as failed
1019 if (test_bit(In_sync
, &rdev
->flags
)
1020 && conf
->raid_disks
-mddev
->degraded
== 1)
1022 * Don't fail the drive, just return an IO error.
1023 * The test should really be more sophisticated than
1024 * "working_disks == 1", but it isn't critical, and
1025 * can wait until we do more sophisticated "is the drive
1026 * really dead" tests...
1029 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1030 unsigned long flags
;
1031 spin_lock_irqsave(&conf
->device_lock
, flags
);
1033 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1035 * if recovery is running, make sure it aborts.
1037 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1039 set_bit(Faulty
, &rdev
->flags
);
1040 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1041 printk(KERN_ALERT
"raid10: Disk failure on %s, disabling device.\n"
1042 "raid10: Operation continuing on %d devices.\n",
1043 bdevname(rdev
->bdev
,b
), conf
->raid_disks
- mddev
->degraded
);
1046 static void print_conf(conf_t
*conf
)
1051 printk("RAID10 conf printout:\n");
1053 printk("(!conf)\n");
1056 printk(" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1059 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1060 char b
[BDEVNAME_SIZE
];
1061 tmp
= conf
->mirrors
+ i
;
1063 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
1064 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1065 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1066 bdevname(tmp
->rdev
->bdev
,b
));
1070 static void close_sync(conf_t
*conf
)
1073 allow_barrier(conf
);
1075 mempool_destroy(conf
->r10buf_pool
);
1076 conf
->r10buf_pool
= NULL
;
1079 /* check if there are enough drives for
1080 * every block to appear on atleast one
1082 static int enough(conf_t
*conf
)
1087 int n
= conf
->copies
;
1090 if (conf
->mirrors
[first
].rdev
)
1092 first
= (first
+1) % conf
->raid_disks
;
1096 } while (first
!= 0);
1100 static int raid10_spare_active(mddev_t
*mddev
)
1103 conf_t
*conf
= mddev
->private;
1107 * Find all non-in_sync disks within the RAID10 configuration
1108 * and mark them in_sync
1110 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1111 tmp
= conf
->mirrors
+ i
;
1113 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1114 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1115 unsigned long flags
;
1116 spin_lock_irqsave(&conf
->device_lock
, flags
);
1118 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1127 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1129 conf_t
*conf
= mddev
->private;
1134 int last
= mddev
->raid_disks
- 1;
1136 if (mddev
->recovery_cp
< MaxSector
)
1137 /* only hot-add to in-sync arrays, as recovery is
1138 * very different from resync
1144 if (rdev
->raid_disk
>= 0)
1145 first
= last
= rdev
->raid_disk
;
1147 if (rdev
->saved_raid_disk
>= 0 &&
1148 rdev
->saved_raid_disk
>= first
&&
1149 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1150 mirror
= rdev
->saved_raid_disk
;
1153 for ( ; mirror
<= last
; mirror
++)
1154 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1156 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1157 rdev
->data_offset
<< 9);
1158 /* as we don't honour merge_bvec_fn, we must
1159 * never risk violating it, so limit
1160 * ->max_segments to one lying with a single
1161 * page, as a one page request is never in
1164 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1165 blk_queue_max_segments(mddev
->queue
, 1);
1166 blk_queue_segment_boundary(mddev
->queue
,
1167 PAGE_CACHE_SIZE
- 1);
1170 p
->head_position
= 0;
1171 rdev
->raid_disk
= mirror
;
1173 if (rdev
->saved_raid_disk
!= mirror
)
1175 rcu_assign_pointer(p
->rdev
, rdev
);
1179 md_integrity_add_rdev(rdev
, mddev
);
1184 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1186 conf_t
*conf
= mddev
->private;
1189 mirror_info_t
*p
= conf
->mirrors
+ number
;
1194 if (test_bit(In_sync
, &rdev
->flags
) ||
1195 atomic_read(&rdev
->nr_pending
)) {
1199 /* Only remove faulty devices in recovery
1202 if (!test_bit(Faulty
, &rdev
->flags
) &&
1209 if (atomic_read(&rdev
->nr_pending
)) {
1210 /* lost the race, try later */
1215 md_integrity_register(mddev
);
1224 static void end_sync_read(struct bio
*bio
, int error
)
1226 r10bio_t
*r10_bio
= bio
->bi_private
;
1227 conf_t
*conf
= r10_bio
->mddev
->private;
1230 for (i
=0; i
<conf
->copies
; i
++)
1231 if (r10_bio
->devs
[i
].bio
== bio
)
1233 BUG_ON(i
== conf
->copies
);
1234 update_head_pos(i
, r10_bio
);
1235 d
= r10_bio
->devs
[i
].devnum
;
1237 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1238 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1240 atomic_add(r10_bio
->sectors
,
1241 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1242 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1243 md_error(r10_bio
->mddev
,
1244 conf
->mirrors
[d
].rdev
);
1247 /* for reconstruct, we always reschedule after a read.
1248 * for resync, only after all reads
1250 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1251 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1252 atomic_dec_and_test(&r10_bio
->remaining
)) {
1253 /* we have read all the blocks,
1254 * do the comparison in process context in raid10d
1256 reschedule_retry(r10_bio
);
1260 static void end_sync_write(struct bio
*bio
, int error
)
1262 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1263 r10bio_t
*r10_bio
= bio
->bi_private
;
1264 mddev_t
*mddev
= r10_bio
->mddev
;
1265 conf_t
*conf
= mddev
->private;
1268 for (i
= 0; i
< conf
->copies
; i
++)
1269 if (r10_bio
->devs
[i
].bio
== bio
)
1271 d
= r10_bio
->devs
[i
].devnum
;
1274 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1276 update_head_pos(i
, r10_bio
);
1278 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1279 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1280 if (r10_bio
->master_bio
== NULL
) {
1281 /* the primary of several recovery bios */
1282 sector_t s
= r10_bio
->sectors
;
1284 md_done_sync(mddev
, s
, 1);
1287 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1295 * Note: sync and recover and handled very differently for raid10
1296 * This code is for resync.
1297 * For resync, we read through virtual addresses and read all blocks.
1298 * If there is any error, we schedule a write. The lowest numbered
1299 * drive is authoritative.
1300 * However requests come for physical address, so we need to map.
1301 * For every physical address there are raid_disks/copies virtual addresses,
1302 * which is always are least one, but is not necessarly an integer.
1303 * This means that a physical address can span multiple chunks, so we may
1304 * have to submit multiple io requests for a single sync request.
1307 * We check if all blocks are in-sync and only write to blocks that
1310 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1312 conf_t
*conf
= mddev
->private;
1314 struct bio
*tbio
, *fbio
;
1316 atomic_set(&r10_bio
->remaining
, 1);
1318 /* find the first device with a block */
1319 for (i
=0; i
<conf
->copies
; i
++)
1320 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1323 if (i
== conf
->copies
)
1327 fbio
= r10_bio
->devs
[i
].bio
;
1329 /* now find blocks with errors */
1330 for (i
=0 ; i
< conf
->copies
; i
++) {
1332 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1334 tbio
= r10_bio
->devs
[i
].bio
;
1336 if (tbio
->bi_end_io
!= end_sync_read
)
1340 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1341 /* We know that the bi_io_vec layout is the same for
1342 * both 'first' and 'i', so we just compare them.
1343 * All vec entries are PAGE_SIZE;
1345 for (j
= 0; j
< vcnt
; j
++)
1346 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1347 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1352 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1354 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1355 /* Don't fix anything. */
1357 /* Ok, we need to write this bio
1358 * First we need to fixup bv_offset, bv_len and
1359 * bi_vecs, as the read request might have corrupted these
1361 tbio
->bi_vcnt
= vcnt
;
1362 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1364 tbio
->bi_phys_segments
= 0;
1365 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1366 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1367 tbio
->bi_next
= NULL
;
1368 tbio
->bi_rw
= WRITE
;
1369 tbio
->bi_private
= r10_bio
;
1370 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1372 for (j
=0; j
< vcnt
; j
++) {
1373 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1374 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1376 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1377 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1380 tbio
->bi_end_io
= end_sync_write
;
1382 d
= r10_bio
->devs
[i
].devnum
;
1383 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1384 atomic_inc(&r10_bio
->remaining
);
1385 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1387 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1388 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1389 generic_make_request(tbio
);
1393 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1394 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1400 * Now for the recovery code.
1401 * Recovery happens across physical sectors.
1402 * We recover all non-is_sync drives by finding the virtual address of
1403 * each, and then choose a working drive that also has that virt address.
1404 * There is a separate r10_bio for each non-in_sync drive.
1405 * Only the first two slots are in use. The first for reading,
1406 * The second for writing.
1410 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1412 conf_t
*conf
= mddev
->private;
1414 struct bio
*bio
, *wbio
;
1417 /* move the pages across to the second bio
1418 * and submit the write request
1420 bio
= r10_bio
->devs
[0].bio
;
1421 wbio
= r10_bio
->devs
[1].bio
;
1422 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1423 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1424 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1425 wbio
->bi_io_vec
[i
].bv_page
= p
;
1427 d
= r10_bio
->devs
[1].devnum
;
1429 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1430 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1431 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1432 generic_make_request(wbio
);
1434 bio_endio(wbio
, -EIO
);
1439 * Used by fix_read_error() to decay the per rdev read_errors.
1440 * We halve the read error count for every hour that has elapsed
1441 * since the last recorded read error.
1444 static void check_decay_read_errors(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1446 struct timespec cur_time_mon
;
1447 unsigned long hours_since_last
;
1448 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
1450 ktime_get_ts(&cur_time_mon
);
1452 if (rdev
->last_read_error
.tv_sec
== 0 &&
1453 rdev
->last_read_error
.tv_nsec
== 0) {
1454 /* first time we've seen a read error */
1455 rdev
->last_read_error
= cur_time_mon
;
1459 hours_since_last
= (cur_time_mon
.tv_sec
-
1460 rdev
->last_read_error
.tv_sec
) / 3600;
1462 rdev
->last_read_error
= cur_time_mon
;
1465 * if hours_since_last is > the number of bits in read_errors
1466 * just set read errors to 0. We do this to avoid
1467 * overflowing the shift of read_errors by hours_since_last.
1469 if (hours_since_last
>= 8 * sizeof(read_errors
))
1470 atomic_set(&rdev
->read_errors
, 0);
1472 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
1476 * This is a kernel thread which:
1478 * 1. Retries failed read operations on working mirrors.
1479 * 2. Updates the raid superblock when problems encounter.
1480 * 3. Performs writes following reads for array synchronising.
1483 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1485 int sect
= 0; /* Offset from r10_bio->sector */
1486 int sectors
= r10_bio
->sectors
;
1488 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
1492 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1493 char b
[BDEVNAME_SIZE
];
1494 int cur_read_error_count
= 0;
1496 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1497 bdevname(rdev
->bdev
, b
);
1499 if (test_bit(Faulty
, &rdev
->flags
)) {
1501 /* drive has already been failed, just ignore any
1502 more fix_read_error() attempts */
1506 check_decay_read_errors(mddev
, rdev
);
1507 atomic_inc(&rdev
->read_errors
);
1508 cur_read_error_count
= atomic_read(&rdev
->read_errors
);
1509 if (cur_read_error_count
> max_read_errors
) {
1512 "raid10: %s: Raid device exceeded "
1513 "read_error threshold "
1514 "[cur %d:max %d]\n",
1515 b
, cur_read_error_count
, max_read_errors
);
1517 "raid10: %s: Failing raid "
1519 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1527 int sl
= r10_bio
->read_slot
;
1531 if (s
> (PAGE_SIZE
>>9))
1536 int d
= r10_bio
->devs
[sl
].devnum
;
1537 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1539 test_bit(In_sync
, &rdev
->flags
)) {
1540 atomic_inc(&rdev
->nr_pending
);
1542 success
= sync_page_io(rdev
->bdev
,
1543 r10_bio
->devs
[sl
].addr
+
1544 sect
+ rdev
->data_offset
,
1546 conf
->tmppage
, READ
);
1547 rdev_dec_pending(rdev
, mddev
);
1553 if (sl
== conf
->copies
)
1555 } while (!success
&& sl
!= r10_bio
->read_slot
);
1559 /* Cannot read from anywhere -- bye bye array */
1560 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1561 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1566 /* write it back and re-read */
1568 while (sl
!= r10_bio
->read_slot
) {
1569 char b
[BDEVNAME_SIZE
];
1574 d
= r10_bio
->devs
[sl
].devnum
;
1575 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1577 test_bit(In_sync
, &rdev
->flags
)) {
1578 atomic_inc(&rdev
->nr_pending
);
1580 atomic_add(s
, &rdev
->corrected_errors
);
1581 if (sync_page_io(rdev
->bdev
,
1582 r10_bio
->devs
[sl
].addr
+
1583 sect
+ rdev
->data_offset
,
1584 s
<<9, conf
->tmppage
, WRITE
)
1586 /* Well, this device is dead */
1588 "raid10:%s: read correction "
1590 " (%d sectors at %llu on %s)\n",
1592 (unsigned long long)(sect
+
1594 bdevname(rdev
->bdev
, b
));
1595 printk(KERN_NOTICE
"raid10:%s: failing "
1597 bdevname(rdev
->bdev
, b
));
1598 md_error(mddev
, rdev
);
1600 rdev_dec_pending(rdev
, mddev
);
1605 while (sl
!= r10_bio
->read_slot
) {
1610 d
= r10_bio
->devs
[sl
].devnum
;
1611 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1613 test_bit(In_sync
, &rdev
->flags
)) {
1614 char b
[BDEVNAME_SIZE
];
1615 atomic_inc(&rdev
->nr_pending
);
1617 if (sync_page_io(rdev
->bdev
,
1618 r10_bio
->devs
[sl
].addr
+
1619 sect
+ rdev
->data_offset
,
1620 s
<<9, conf
->tmppage
,
1622 /* Well, this device is dead */
1624 "raid10:%s: unable to read back "
1626 " (%d sectors at %llu on %s)\n",
1628 (unsigned long long)(sect
+
1630 bdevname(rdev
->bdev
, b
));
1631 printk(KERN_NOTICE
"raid10:%s: failing drive\n",
1632 bdevname(rdev
->bdev
, b
));
1634 md_error(mddev
, rdev
);
1637 "raid10:%s: read error corrected"
1638 " (%d sectors at %llu on %s)\n",
1640 (unsigned long long)(sect
+
1642 bdevname(rdev
->bdev
, b
));
1645 rdev_dec_pending(rdev
, mddev
);
1656 static void raid10d(mddev_t
*mddev
)
1660 unsigned long flags
;
1661 conf_t
*conf
= mddev
->private;
1662 struct list_head
*head
= &conf
->retry_list
;
1666 md_check_recovery(mddev
);
1669 char b
[BDEVNAME_SIZE
];
1671 unplug
+= flush_pending_writes(conf
);
1673 spin_lock_irqsave(&conf
->device_lock
, flags
);
1674 if (list_empty(head
)) {
1675 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1678 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1679 list_del(head
->prev
);
1681 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1683 mddev
= r10_bio
->mddev
;
1684 conf
= mddev
->private;
1685 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
)) {
1686 sync_request_write(mddev
, r10_bio
);
1688 } else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
1689 recovery_request_write(mddev
, r10_bio
);
1693 /* we got a read error. Maybe the drive is bad. Maybe just
1694 * the block and we can fix it.
1695 * We freeze all other IO, and try reading the block from
1696 * other devices. When we find one, we re-write
1697 * and check it that fixes the read error.
1698 * This is all done synchronously while the array is
1701 if (mddev
->ro
== 0) {
1703 fix_read_error(conf
, mddev
, r10_bio
);
1704 unfreeze_array(conf
);
1707 bio
= r10_bio
->devs
[r10_bio
->read_slot
].bio
;
1708 r10_bio
->devs
[r10_bio
->read_slot
].bio
=
1709 mddev
->ro
? IO_BLOCKED
: NULL
;
1710 mirror
= read_balance(conf
, r10_bio
);
1712 printk(KERN_ALERT
"raid10: %s: unrecoverable I/O"
1713 " read error for block %llu\n",
1714 bdevname(bio
->bi_bdev
,b
),
1715 (unsigned long long)r10_bio
->sector
);
1716 raid_end_bio_io(r10_bio
);
1719 const bool do_sync
= bio_rw_flagged(r10_bio
->master_bio
, BIO_RW_SYNCIO
);
1721 rdev
= conf
->mirrors
[mirror
].rdev
;
1722 if (printk_ratelimit())
1723 printk(KERN_ERR
"raid10: %s: redirecting sector %llu to"
1724 " another mirror\n",
1725 bdevname(rdev
->bdev
,b
),
1726 (unsigned long long)r10_bio
->sector
);
1727 bio
= bio_clone(r10_bio
->master_bio
, GFP_NOIO
);
1728 r10_bio
->devs
[r10_bio
->read_slot
].bio
= bio
;
1729 bio
->bi_sector
= r10_bio
->devs
[r10_bio
->read_slot
].addr
1730 + rdev
->data_offset
;
1731 bio
->bi_bdev
= rdev
->bdev
;
1732 bio
->bi_rw
= READ
| (do_sync
<< BIO_RW_SYNCIO
);
1733 bio
->bi_private
= r10_bio
;
1734 bio
->bi_end_io
= raid10_end_read_request
;
1736 generic_make_request(bio
);
1742 unplug_slaves(mddev
);
1746 static int init_resync(conf_t
*conf
)
1750 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1751 BUG_ON(conf
->r10buf_pool
);
1752 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1753 if (!conf
->r10buf_pool
)
1755 conf
->next_resync
= 0;
1760 * perform a "sync" on one "block"
1762 * We need to make sure that no normal I/O request - particularly write
1763 * requests - conflict with active sync requests.
1765 * This is achieved by tracking pending requests and a 'barrier' concept
1766 * that can be installed to exclude normal IO requests.
1768 * Resync and recovery are handled very differently.
1769 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1771 * For resync, we iterate over virtual addresses, read all copies,
1772 * and update if there are differences. If only one copy is live,
1774 * For recovery, we iterate over physical addresses, read a good
1775 * value for each non-in_sync drive, and over-write.
1777 * So, for recovery we may have several outstanding complex requests for a
1778 * given address, one for each out-of-sync device. We model this by allocating
1779 * a number of r10_bio structures, one for each out-of-sync device.
1780 * As we setup these structures, we collect all bio's together into a list
1781 * which we then process collectively to add pages, and then process again
1782 * to pass to generic_make_request.
1784 * The r10_bio structures are linked using a borrowed master_bio pointer.
1785 * This link is counted in ->remaining. When the r10_bio that points to NULL
1786 * has its remaining count decremented to 0, the whole complex operation
1791 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1793 conf_t
*conf
= mddev
->private;
1795 struct bio
*biolist
= NULL
, *bio
;
1796 sector_t max_sector
, nr_sectors
;
1802 sector_t sectors_skipped
= 0;
1803 int chunks_skipped
= 0;
1805 if (!conf
->r10buf_pool
)
1806 if (init_resync(conf
))
1810 max_sector
= mddev
->dev_sectors
;
1811 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1812 max_sector
= mddev
->resync_max_sectors
;
1813 if (sector_nr
>= max_sector
) {
1814 /* If we aborted, we need to abort the
1815 * sync on the 'current' bitmap chucks (there can
1816 * be several when recovering multiple devices).
1817 * as we may have started syncing it but not finished.
1818 * We can find the current address in
1819 * mddev->curr_resync, but for recovery,
1820 * we need to convert that to several
1821 * virtual addresses.
1823 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1824 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1825 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1827 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1829 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1830 bitmap_end_sync(mddev
->bitmap
, sect
,
1833 } else /* completed sync */
1836 bitmap_close_sync(mddev
->bitmap
);
1839 return sectors_skipped
;
1841 if (chunks_skipped
>= conf
->raid_disks
) {
1842 /* if there has been nothing to do on any drive,
1843 * then there is nothing to do at all..
1846 return (max_sector
- sector_nr
) + sectors_skipped
;
1849 if (max_sector
> mddev
->resync_max
)
1850 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
1852 /* make sure whole request will fit in a chunk - if chunks
1855 if (conf
->near_copies
< conf
->raid_disks
&&
1856 max_sector
> (sector_nr
| conf
->chunk_mask
))
1857 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1859 * If there is non-resync activity waiting for us then
1860 * put in a delay to throttle resync.
1862 if (!go_faster
&& conf
->nr_waiting
)
1863 msleep_interruptible(1000);
1865 /* Again, very different code for resync and recovery.
1866 * Both must result in an r10bio with a list of bios that
1867 * have bi_end_io, bi_sector, bi_bdev set,
1868 * and bi_private set to the r10bio.
1869 * For recovery, we may actually create several r10bios
1870 * with 2 bios in each, that correspond to the bios in the main one.
1871 * In this case, the subordinate r10bios link back through a
1872 * borrowed master_bio pointer, and the counter in the master
1873 * includes a ref from each subordinate.
1875 /* First, we decide what to do and set ->bi_end_io
1876 * To end_sync_read if we want to read, and
1877 * end_sync_write if we will want to write.
1880 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
1881 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1882 /* recovery... the complicated one */
1886 for (i
=0 ; i
<conf
->raid_disks
; i
++)
1887 if (conf
->mirrors
[i
].rdev
&&
1888 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
)) {
1889 int still_degraded
= 0;
1890 /* want to reconstruct this device */
1891 r10bio_t
*rb2
= r10_bio
;
1892 sector_t sect
= raid10_find_virt(conf
, sector_nr
, i
);
1894 /* Unless we are doing a full sync, we only need
1895 * to recover the block if it is set in the bitmap
1897 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1899 if (sync_blocks
< max_sync
)
1900 max_sync
= sync_blocks
;
1903 /* yep, skip the sync_blocks here, but don't assume
1904 * that there will never be anything to do here
1906 chunks_skipped
= -1;
1910 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1911 raise_barrier(conf
, rb2
!= NULL
);
1912 atomic_set(&r10_bio
->remaining
, 0);
1914 r10_bio
->master_bio
= (struct bio
*)rb2
;
1916 atomic_inc(&rb2
->remaining
);
1917 r10_bio
->mddev
= mddev
;
1918 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1919 r10_bio
->sector
= sect
;
1921 raid10_find_phys(conf
, r10_bio
);
1923 /* Need to check if the array will still be
1926 for (j
=0; j
<conf
->raid_disks
; j
++)
1927 if (conf
->mirrors
[j
].rdev
== NULL
||
1928 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
1933 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1934 &sync_blocks
, still_degraded
);
1936 for (j
=0; j
<conf
->copies
;j
++) {
1937 int d
= r10_bio
->devs
[j
].devnum
;
1938 if (conf
->mirrors
[d
].rdev
&&
1939 test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
)) {
1940 /* This is where we read from */
1941 bio
= r10_bio
->devs
[0].bio
;
1942 bio
->bi_next
= biolist
;
1944 bio
->bi_private
= r10_bio
;
1945 bio
->bi_end_io
= end_sync_read
;
1947 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1948 conf
->mirrors
[d
].rdev
->data_offset
;
1949 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1950 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1951 atomic_inc(&r10_bio
->remaining
);
1952 /* and we write to 'i' */
1954 for (k
=0; k
<conf
->copies
; k
++)
1955 if (r10_bio
->devs
[k
].devnum
== i
)
1957 BUG_ON(k
== conf
->copies
);
1958 bio
= r10_bio
->devs
[1].bio
;
1959 bio
->bi_next
= biolist
;
1961 bio
->bi_private
= r10_bio
;
1962 bio
->bi_end_io
= end_sync_write
;
1964 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1965 conf
->mirrors
[i
].rdev
->data_offset
;
1966 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1968 r10_bio
->devs
[0].devnum
= d
;
1969 r10_bio
->devs
[1].devnum
= i
;
1974 if (j
== conf
->copies
) {
1975 /* Cannot recover, so abort the recovery */
1978 atomic_dec(&rb2
->remaining
);
1980 if (!test_and_set_bit(MD_RECOVERY_INTR
,
1982 printk(KERN_INFO
"raid10: %s: insufficient working devices for recovery.\n",
1987 if (biolist
== NULL
) {
1989 r10bio_t
*rb2
= r10_bio
;
1990 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
1991 rb2
->master_bio
= NULL
;
1997 /* resync. Schedule a read for every block at this virt offset */
2000 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
2002 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2003 &sync_blocks
, mddev
->degraded
) &&
2004 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2005 /* We can skip this block */
2007 return sync_blocks
+ sectors_skipped
;
2009 if (sync_blocks
< max_sync
)
2010 max_sync
= sync_blocks
;
2011 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2013 r10_bio
->mddev
= mddev
;
2014 atomic_set(&r10_bio
->remaining
, 0);
2015 raise_barrier(conf
, 0);
2016 conf
->next_resync
= sector_nr
;
2018 r10_bio
->master_bio
= NULL
;
2019 r10_bio
->sector
= sector_nr
;
2020 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
2021 raid10_find_phys(conf
, r10_bio
);
2022 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
2024 for (i
=0; i
<conf
->copies
; i
++) {
2025 int d
= r10_bio
->devs
[i
].devnum
;
2026 bio
= r10_bio
->devs
[i
].bio
;
2027 bio
->bi_end_io
= NULL
;
2028 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2029 if (conf
->mirrors
[d
].rdev
== NULL
||
2030 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
2032 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2033 atomic_inc(&r10_bio
->remaining
);
2034 bio
->bi_next
= biolist
;
2036 bio
->bi_private
= r10_bio
;
2037 bio
->bi_end_io
= end_sync_read
;
2039 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
2040 conf
->mirrors
[d
].rdev
->data_offset
;
2041 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2046 for (i
=0; i
<conf
->copies
; i
++) {
2047 int d
= r10_bio
->devs
[i
].devnum
;
2048 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
2049 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
2057 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2059 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
2061 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2064 bio
->bi_phys_segments
= 0;
2069 if (sector_nr
+ max_sync
< max_sector
)
2070 max_sector
= sector_nr
+ max_sync
;
2073 int len
= PAGE_SIZE
;
2075 if (sector_nr
+ (len
>>9) > max_sector
)
2076 len
= (max_sector
- sector_nr
) << 9;
2079 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2080 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2081 if (bio_add_page(bio
, page
, len
, 0) == 0) {
2084 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2085 for (bio2
= biolist
; bio2
&& bio2
!= bio
; bio2
= bio2
->bi_next
) {
2086 /* remove last page from this bio */
2088 bio2
->bi_size
-= len
;
2089 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2095 nr_sectors
+= len
>>9;
2096 sector_nr
+= len
>>9;
2097 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
2099 r10_bio
->sectors
= nr_sectors
;
2103 biolist
= biolist
->bi_next
;
2105 bio
->bi_next
= NULL
;
2106 r10_bio
= bio
->bi_private
;
2107 r10_bio
->sectors
= nr_sectors
;
2109 if (bio
->bi_end_io
== end_sync_read
) {
2110 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2111 generic_make_request(bio
);
2115 if (sectors_skipped
)
2116 /* pretend they weren't skipped, it makes
2117 * no important difference in this case
2119 md_done_sync(mddev
, sectors_skipped
, 1);
2121 return sectors_skipped
+ nr_sectors
;
2123 /* There is nowhere to write, so all non-sync
2124 * drives must be failed, so try the next chunk...
2126 if (sector_nr
+ max_sync
< max_sector
)
2127 max_sector
= sector_nr
+ max_sync
;
2129 sectors_skipped
+= (max_sector
- sector_nr
);
2131 sector_nr
= max_sector
;
2136 raid10_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2139 conf_t
*conf
= mddev
->private;
2142 raid_disks
= mddev
->raid_disks
;
2144 sectors
= mddev
->dev_sectors
;
2146 size
= sectors
>> conf
->chunk_shift
;
2147 sector_div(size
, conf
->far_copies
);
2148 size
= size
* raid_disks
;
2149 sector_div(size
, conf
->near_copies
);
2151 return size
<< conf
->chunk_shift
;
2154 static int run(mddev_t
*mddev
)
2157 int i
, disk_idx
, chunk_size
;
2158 mirror_info_t
*disk
;
2161 sector_t stride
, size
;
2163 if (mddev
->chunk_sectors
< (PAGE_SIZE
>> 9) ||
2164 !is_power_of_2(mddev
->chunk_sectors
)) {
2165 printk(KERN_ERR
"md/raid10: chunk size must be "
2166 "at least PAGE_SIZE(%ld) and be a power of 2.\n", PAGE_SIZE
);
2170 nc
= mddev
->layout
& 255;
2171 fc
= (mddev
->layout
>> 8) & 255;
2172 fo
= mddev
->layout
& (1<<16);
2173 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2174 (mddev
->layout
>> 17)) {
2175 printk(KERN_ERR
"raid10: %s: unsupported raid10 layout: 0x%8x\n",
2176 mdname(mddev
), mddev
->layout
);
2180 * copy the already verified devices into our private RAID10
2181 * bookkeeping area. [whatever we allocate in run(),
2182 * should be freed in stop()]
2184 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2185 mddev
->private = conf
;
2187 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2191 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2193 if (!conf
->mirrors
) {
2194 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2199 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2203 conf
->raid_disks
= mddev
->raid_disks
;
2204 conf
->near_copies
= nc
;
2205 conf
->far_copies
= fc
;
2206 conf
->copies
= nc
*fc
;
2207 conf
->far_offset
= fo
;
2208 conf
->chunk_mask
= mddev
->chunk_sectors
- 1;
2209 conf
->chunk_shift
= ffz(~mddev
->chunk_sectors
);
2210 size
= mddev
->dev_sectors
>> conf
->chunk_shift
;
2211 sector_div(size
, fc
);
2212 size
= size
* conf
->raid_disks
;
2213 sector_div(size
, nc
);
2214 /* 'size' is now the number of chunks in the array */
2215 /* calculate "used chunks per device" in 'stride' */
2216 stride
= size
* conf
->copies
;
2218 /* We need to round up when dividing by raid_disks to
2219 * get the stride size.
2221 stride
+= conf
->raid_disks
- 1;
2222 sector_div(stride
, conf
->raid_disks
);
2223 mddev
->dev_sectors
= stride
<< conf
->chunk_shift
;
2228 sector_div(stride
, fc
);
2229 conf
->stride
= stride
<< conf
->chunk_shift
;
2231 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2232 r10bio_pool_free
, conf
);
2233 if (!conf
->r10bio_pool
) {
2234 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2239 conf
->mddev
= mddev
;
2240 spin_lock_init(&conf
->device_lock
);
2241 mddev
->queue
->queue_lock
= &conf
->device_lock
;
2243 chunk_size
= mddev
->chunk_sectors
<< 9;
2244 blk_queue_io_min(mddev
->queue
, chunk_size
);
2245 if (conf
->raid_disks
% conf
->near_copies
)
2246 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->raid_disks
);
2248 blk_queue_io_opt(mddev
->queue
, chunk_size
*
2249 (conf
->raid_disks
/ conf
->near_copies
));
2251 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2252 disk_idx
= rdev
->raid_disk
;
2253 if (disk_idx
>= mddev
->raid_disks
2256 disk
= conf
->mirrors
+ disk_idx
;
2259 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2260 rdev
->data_offset
<< 9);
2261 /* as we don't honour merge_bvec_fn, we must never risk
2262 * violating it, so limit max_segments to 1 lying
2263 * within a single page.
2265 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2266 blk_queue_max_segments(mddev
->queue
, 1);
2267 blk_queue_segment_boundary(mddev
->queue
,
2268 PAGE_CACHE_SIZE
- 1);
2271 disk
->head_position
= 0;
2273 INIT_LIST_HEAD(&conf
->retry_list
);
2275 spin_lock_init(&conf
->resync_lock
);
2276 init_waitqueue_head(&conf
->wait_barrier
);
2278 /* need to check that every block has at least one working mirror */
2279 if (!enough(conf
)) {
2280 printk(KERN_ERR
"raid10: not enough operational mirrors for %s\n",
2285 mddev
->degraded
= 0;
2286 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2288 disk
= conf
->mirrors
+ i
;
2291 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2292 disk
->head_position
= 0;
2300 mddev
->thread
= md_register_thread(raid10d
, mddev
, NULL
);
2301 if (!mddev
->thread
) {
2303 "raid10: couldn't allocate thread for %s\n",
2308 if (mddev
->recovery_cp
!= MaxSector
)
2309 printk(KERN_NOTICE
"raid10: %s is not clean"
2310 " -- starting background reconstruction\n",
2313 "raid10: raid set %s active with %d out of %d devices\n",
2314 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2317 * Ok, everything is just fine now
2319 md_set_array_sectors(mddev
, raid10_size(mddev
, 0, 0));
2320 mddev
->resync_max_sectors
= raid10_size(mddev
, 0, 0);
2322 mddev
->queue
->unplug_fn
= raid10_unplug
;
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
< mddev
->raid_disks
)
2339 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2340 md_integrity_register(mddev
);
2344 if (conf
->r10bio_pool
)
2345 mempool_destroy(conf
->r10bio_pool
);
2346 safe_put_page(conf
->tmppage
);
2347 kfree(conf
->mirrors
);
2349 mddev
->private = NULL
;
2354 static int stop(mddev_t
*mddev
)
2356 conf_t
*conf
= mddev
->private;
2358 raise_barrier(conf
, 0);
2359 lower_barrier(conf
);
2361 md_unregister_thread(mddev
->thread
);
2362 mddev
->thread
= NULL
;
2363 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2364 if (conf
->r10bio_pool
)
2365 mempool_destroy(conf
->r10bio_pool
);
2366 kfree(conf
->mirrors
);
2368 mddev
->private = NULL
;
2372 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2374 conf_t
*conf
= mddev
->private;
2378 raise_barrier(conf
, 0);
2381 lower_barrier(conf
);
2386 static struct mdk_personality raid10_personality
=
2390 .owner
= THIS_MODULE
,
2391 .make_request
= make_request
,
2395 .error_handler
= error
,
2396 .hot_add_disk
= raid10_add_disk
,
2397 .hot_remove_disk
= raid10_remove_disk
,
2398 .spare_active
= raid10_spare_active
,
2399 .sync_request
= sync_request
,
2400 .quiesce
= raid10_quiesce
,
2401 .size
= raid10_size
,
2404 static int __init
raid_init(void)
2406 return register_md_personality(&raid10_personality
);
2409 static void raid_exit(void)
2411 unregister_md_personality(&raid10_personality
);
2414 module_init(raid_init
);
2415 module_exit(raid_exit
);
2416 MODULE_LICENSE("GPL");
2417 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2418 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2419 MODULE_ALIAS("md-raid10");
2420 MODULE_ALIAS("md-level-10");