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 "dm-bio-list.h"
22 #include <linux/raid/raid10.h>
23 #include <linux/raid/bitmap.h>
26 * RAID10 provides a combination of RAID0 and RAID1 functionality.
27 * The layout of data is defined by
30 * near_copies (stored in low byte of layout)
31 * far_copies (stored in second byte of layout)
32 * far_offset (stored in bit 16 of layout )
34 * The data to be stored is divided into chunks using chunksize.
35 * Each device is divided into far_copies sections.
36 * In each section, chunks are laid out in a style similar to raid0, but
37 * near_copies copies of each chunk is stored (each on a different drive).
38 * The starting device for each section is offset near_copies from the starting
39 * device of the previous section.
40 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
42 * near_copies and far_copies must be at least one, and their product is at most
45 * If far_offset is true, then the far_copies are handled a bit differently.
46 * The copies are still in different stripes, but instead of be very far apart
47 * on disk, there are adjacent stripes.
51 * Number of guaranteed r10bios in case of extreme VM load:
53 #define NR_RAID10_BIOS 256
55 static void unplug_slaves(mddev_t
*mddev
);
57 static void allow_barrier(conf_t
*conf
);
58 static void lower_barrier(conf_t
*conf
);
60 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
64 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
66 /* allocate a r10bio with room for raid_disks entries in the bios array */
67 r10_bio
= kzalloc(size
, gfp_flags
);
69 unplug_slaves(conf
->mddev
);
74 static void r10bio_pool_free(void *r10_bio
, void *data
)
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
81 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
82 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
83 #define RESYNC_WINDOW (2048*1024)
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
);
103 unplug_slaves(conf
->mddev
);
107 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
108 nalloc
= conf
->copies
; /* resync */
110 nalloc
= 2; /* recovery */
115 for (j
= nalloc
; j
-- ; ) {
116 bio
= bio_alloc(gfp_flags
, RESYNC_PAGES
);
119 r10_bio
->devs
[j
].bio
= bio
;
122 * Allocate RESYNC_PAGES data pages and attach them
125 for (j
= 0 ; j
< nalloc
; j
++) {
126 bio
= r10_bio
->devs
[j
].bio
;
127 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
128 page
= alloc_page(gfp_flags
);
132 bio
->bi_io_vec
[i
].bv_page
= page
;
140 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
142 for (i
= 0; i
< RESYNC_PAGES
; i
++)
143 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
146 while ( ++j
< nalloc
)
147 bio_put(r10_bio
->devs
[j
].bio
);
148 r10bio_pool_free(r10_bio
, conf
);
152 static void r10buf_pool_free(void *__r10_bio
, void *data
)
156 r10bio_t
*r10bio
= __r10_bio
;
159 for (j
=0; j
< conf
->copies
; j
++) {
160 struct bio
*bio
= r10bio
->devs
[j
].bio
;
162 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
163 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
164 bio
->bi_io_vec
[i
].bv_page
= NULL
;
169 r10bio_pool_free(r10bio
, conf
);
172 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
176 for (i
= 0; i
< conf
->copies
; i
++) {
177 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
178 if (*bio
&& *bio
!= IO_BLOCKED
)
184 static void free_r10bio(r10bio_t
*r10_bio
)
186 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
189 * Wake up any possible resync thread that waits for the device
194 put_all_bios(conf
, r10_bio
);
195 mempool_free(r10_bio
, conf
->r10bio_pool
);
198 static void put_buf(r10bio_t
*r10_bio
)
200 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
202 mempool_free(r10_bio
, conf
->r10buf_pool
);
207 static void reschedule_retry(r10bio_t
*r10_bio
)
210 mddev_t
*mddev
= r10_bio
->mddev
;
211 conf_t
*conf
= mddev_to_conf(mddev
);
213 spin_lock_irqsave(&conf
->device_lock
, flags
);
214 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
216 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
218 md_wakeup_thread(mddev
->thread
);
222 * raid_end_bio_io() is called when we have finished servicing a mirrored
223 * operation and are ready to return a success/failure code to the buffer
226 static void raid_end_bio_io(r10bio_t
*r10_bio
)
228 struct bio
*bio
= r10_bio
->master_bio
;
231 test_bit(R10BIO_Uptodate
, &r10_bio
->state
) ? 0 : -EIO
);
232 free_r10bio(r10_bio
);
236 * Update disk head position estimator based on IRQ completion info.
238 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
240 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
242 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
243 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
246 static void raid10_end_read_request(struct bio
*bio
, int error
)
248 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
249 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
251 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
254 slot
= r10_bio
->read_slot
;
255 dev
= r10_bio
->devs
[slot
].devnum
;
257 * this branch is our 'one mirror IO has finished' event handler:
259 update_head_pos(slot
, r10_bio
);
263 * Set R10BIO_Uptodate in our master bio, so that
264 * we will return a good error code to the higher
265 * levels even if IO on some other mirrored buffer fails.
267 * The 'master' represents the composite IO operation to
268 * user-side. So if something waits for IO, then it will
269 * wait for the 'master' bio.
271 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
272 raid_end_bio_io(r10_bio
);
277 char b
[BDEVNAME_SIZE
];
278 if (printk_ratelimit())
279 printk(KERN_ERR
"raid10: %s: rescheduling sector %llu\n",
280 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
281 reschedule_retry(r10_bio
);
284 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
287 static void raid10_end_write_request(struct bio
*bio
, int error
)
289 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
290 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
292 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
294 for (slot
= 0; slot
< conf
->copies
; slot
++)
295 if (r10_bio
->devs
[slot
].bio
== bio
)
297 dev
= r10_bio
->devs
[slot
].devnum
;
300 * this branch is our 'one mirror IO has finished' event handler:
303 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
304 /* an I/O failed, we can't clear the bitmap */
305 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
308 * Set R10BIO_Uptodate in our master bio, so that
309 * we will return a good error code for to the higher
310 * levels even if IO on some other mirrored buffer fails.
312 * The 'master' represents the composite IO operation to
313 * user-side. So if something waits for IO, then it will
314 * wait for the 'master' bio.
316 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
318 update_head_pos(slot
, r10_bio
);
322 * Let's see if all mirrored write operations have finished
325 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
326 /* clear the bitmap if all writes complete successfully */
327 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
329 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
331 md_write_end(r10_bio
->mddev
);
332 raid_end_bio_io(r10_bio
);
335 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
340 * RAID10 layout manager
341 * Aswell as the chunksize and raid_disks count, there are two
342 * parameters: near_copies and far_copies.
343 * near_copies * far_copies must be <= raid_disks.
344 * Normally one of these will be 1.
345 * If both are 1, we get raid0.
346 * If near_copies == raid_disks, we get raid1.
348 * Chunks are layed out in raid0 style with near_copies copies of the
349 * first chunk, followed by near_copies copies of the next chunk and
351 * If far_copies > 1, then after 1/far_copies of the array has been assigned
352 * as described above, we start again with a device offset of near_copies.
353 * So we effectively have another copy of the whole array further down all
354 * the drives, but with blocks on different drives.
355 * With this layout, and block is never stored twice on the one device.
357 * raid10_find_phys finds the sector offset of a given virtual sector
358 * on each device that it is on.
360 * raid10_find_virt does the reverse mapping, from a device and a
361 * sector offset to a virtual address
364 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
374 /* now calculate first sector/dev */
375 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
376 sector
= r10bio
->sector
& conf
->chunk_mask
;
378 chunk
*= conf
->near_copies
;
380 dev
= sector_div(stripe
, conf
->raid_disks
);
381 if (conf
->far_offset
)
382 stripe
*= conf
->far_copies
;
384 sector
+= stripe
<< conf
->chunk_shift
;
386 /* and calculate all the others */
387 for (n
=0; n
< conf
->near_copies
; n
++) {
390 r10bio
->devs
[slot
].addr
= sector
;
391 r10bio
->devs
[slot
].devnum
= d
;
394 for (f
= 1; f
< conf
->far_copies
; f
++) {
395 d
+= conf
->near_copies
;
396 if (d
>= conf
->raid_disks
)
397 d
-= conf
->raid_disks
;
399 r10bio
->devs
[slot
].devnum
= d
;
400 r10bio
->devs
[slot
].addr
= s
;
404 if (dev
>= conf
->raid_disks
) {
406 sector
+= (conf
->chunk_mask
+ 1);
409 BUG_ON(slot
!= conf
->copies
);
412 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
414 sector_t offset
, chunk
, vchunk
;
416 offset
= sector
& conf
->chunk_mask
;
417 if (conf
->far_offset
) {
419 chunk
= sector
>> conf
->chunk_shift
;
420 fc
= sector_div(chunk
, conf
->far_copies
);
421 dev
-= fc
* conf
->near_copies
;
423 dev
+= conf
->raid_disks
;
425 while (sector
>= conf
->stride
) {
426 sector
-= conf
->stride
;
427 if (dev
< conf
->near_copies
)
428 dev
+= conf
->raid_disks
- conf
->near_copies
;
430 dev
-= conf
->near_copies
;
432 chunk
= sector
>> conf
->chunk_shift
;
434 vchunk
= chunk
* conf
->raid_disks
+ dev
;
435 sector_div(vchunk
, conf
->near_copies
);
436 return (vchunk
<< conf
->chunk_shift
) + offset
;
440 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
442 * @bio: the buffer head that's been built up so far
443 * @biovec: the request that could be merged to it.
445 * Return amount of bytes we can accept at this offset
446 * If near_copies == raid_disk, there are no striping issues,
447 * but in that case, the function isn't called at all.
449 static int raid10_mergeable_bvec(struct request_queue
*q
, struct bio
*bio
,
450 struct bio_vec
*bio_vec
)
452 mddev_t
*mddev
= q
->queuedata
;
453 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
455 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
456 unsigned int bio_sectors
= bio
->bi_size
>> 9;
458 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
459 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
460 if (max
<= bio_vec
->bv_len
&& bio_sectors
== 0)
461 return bio_vec
->bv_len
;
467 * This routine returns the disk from which the requested read should
468 * be done. There is a per-array 'next expected sequential IO' sector
469 * number - if this matches on the next IO then we use the last disk.
470 * There is also a per-disk 'last know head position' sector that is
471 * maintained from IRQ contexts, both the normal and the resync IO
472 * completion handlers update this position correctly. If there is no
473 * perfect sequential match then we pick the disk whose head is closest.
475 * If there are 2 mirrors in the same 2 devices, performance degrades
476 * because position is mirror, not device based.
478 * The rdev for the device selected will have nr_pending incremented.
482 * FIXME: possibly should rethink readbalancing and do it differently
483 * depending on near_copies / far_copies geometry.
485 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
487 const unsigned long this_sector
= r10_bio
->sector
;
488 int disk
, slot
, nslot
;
489 const int sectors
= r10_bio
->sectors
;
490 sector_t new_distance
, current_distance
;
493 raid10_find_phys(conf
, r10_bio
);
496 * Check if we can balance. We can balance on the whole
497 * device if no resync is going on (recovery is ok), or below
498 * the resync window. We take the first readable disk when
499 * above the resync window.
501 if (conf
->mddev
->recovery_cp
< MaxSector
502 && (this_sector
+ sectors
>= conf
->next_resync
)) {
503 /* make sure that disk is operational */
505 disk
= r10_bio
->devs
[slot
].devnum
;
507 while ((rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
508 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
509 !test_bit(In_sync
, &rdev
->flags
)) {
511 if (slot
== conf
->copies
) {
516 disk
= r10_bio
->devs
[slot
].devnum
;
522 /* make sure the disk is operational */
524 disk
= r10_bio
->devs
[slot
].devnum
;
525 while ((rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
526 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
527 !test_bit(In_sync
, &rdev
->flags
)) {
529 if (slot
== conf
->copies
) {
533 disk
= r10_bio
->devs
[slot
].devnum
;
537 current_distance
= abs(r10_bio
->devs
[slot
].addr
-
538 conf
->mirrors
[disk
].head_position
);
540 /* Find the disk whose head is closest */
542 for (nslot
= slot
; nslot
< conf
->copies
; nslot
++) {
543 int ndisk
= r10_bio
->devs
[nslot
].devnum
;
546 if ((rdev
=rcu_dereference(conf
->mirrors
[ndisk
].rdev
)) == NULL
||
547 r10_bio
->devs
[nslot
].bio
== IO_BLOCKED
||
548 !test_bit(In_sync
, &rdev
->flags
))
551 /* This optimisation is debatable, and completely destroys
552 * sequential read speed for 'far copies' arrays. So only
553 * keep it for 'near' arrays, and review those later.
555 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
)) {
560 new_distance
= abs(r10_bio
->devs
[nslot
].addr
-
561 conf
->mirrors
[ndisk
].head_position
);
562 if (new_distance
< current_distance
) {
563 current_distance
= new_distance
;
570 r10_bio
->read_slot
= slot
;
571 /* conf->next_seq_sect = this_sector + sectors;*/
573 if (disk
>= 0 && (rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
))!= NULL
)
574 atomic_inc(&conf
->mirrors
[disk
].rdev
->nr_pending
);
582 static void unplug_slaves(mddev_t
*mddev
)
584 conf_t
*conf
= mddev_to_conf(mddev
);
588 for (i
=0; i
<mddev
->raid_disks
; i
++) {
589 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
590 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
591 struct request_queue
*r_queue
= bdev_get_queue(rdev
->bdev
);
593 atomic_inc(&rdev
->nr_pending
);
596 if (r_queue
->unplug_fn
)
597 r_queue
->unplug_fn(r_queue
);
599 rdev_dec_pending(rdev
, mddev
);
606 static void raid10_unplug(struct request_queue
*q
)
608 mddev_t
*mddev
= q
->queuedata
;
610 unplug_slaves(q
->queuedata
);
611 md_wakeup_thread(mddev
->thread
);
614 static int raid10_congested(void *data
, int bits
)
616 mddev_t
*mddev
= data
;
617 conf_t
*conf
= mddev_to_conf(mddev
);
621 for (i
= 0; i
< mddev
->raid_disks
&& ret
== 0; i
++) {
622 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
623 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
624 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
626 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
635 * Sometimes we need to suspend IO while we do something else,
636 * either some resync/recovery, or reconfigure the array.
637 * To do this we raise a 'barrier'.
638 * The 'barrier' is a counter that can be raised multiple times
639 * to count how many activities are happening which preclude
641 * We can only raise the barrier if there is no pending IO.
642 * i.e. if nr_pending == 0.
643 * We choose only to raise the barrier if no-one is waiting for the
644 * barrier to go down. This means that as soon as an IO request
645 * is ready, no other operations which require a barrier will start
646 * until the IO request has had a chance.
648 * So: regular IO calls 'wait_barrier'. When that returns there
649 * is no backgroup IO happening, It must arrange to call
650 * allow_barrier when it has finished its IO.
651 * backgroup IO calls must call raise_barrier. Once that returns
652 * there is no normal IO happeing. It must arrange to call
653 * lower_barrier when the particular background IO completes.
655 #define RESYNC_DEPTH 32
657 static void raise_barrier(conf_t
*conf
, int force
)
659 BUG_ON(force
&& !conf
->barrier
);
660 spin_lock_irq(&conf
->resync_lock
);
662 /* Wait until no block IO is waiting (unless 'force') */
663 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
665 raid10_unplug(conf
->mddev
->queue
));
667 /* block any new IO from starting */
670 /* No wait for all pending IO to complete */
671 wait_event_lock_irq(conf
->wait_barrier
,
672 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
674 raid10_unplug(conf
->mddev
->queue
));
676 spin_unlock_irq(&conf
->resync_lock
);
679 static void lower_barrier(conf_t
*conf
)
682 spin_lock_irqsave(&conf
->resync_lock
, flags
);
684 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
685 wake_up(&conf
->wait_barrier
);
688 static void wait_barrier(conf_t
*conf
)
690 spin_lock_irq(&conf
->resync_lock
);
693 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
695 raid10_unplug(conf
->mddev
->queue
));
699 spin_unlock_irq(&conf
->resync_lock
);
702 static void allow_barrier(conf_t
*conf
)
705 spin_lock_irqsave(&conf
->resync_lock
, flags
);
707 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
708 wake_up(&conf
->wait_barrier
);
711 static void freeze_array(conf_t
*conf
)
713 /* stop syncio and normal IO and wait for everything to
715 * We increment barrier and nr_waiting, and then
716 * wait until barrier+nr_pending match nr_queued+2
718 spin_lock_irq(&conf
->resync_lock
);
721 wait_event_lock_irq(conf
->wait_barrier
,
722 conf
->barrier
+conf
->nr_pending
== conf
->nr_queued
+2,
724 raid10_unplug(conf
->mddev
->queue
));
725 spin_unlock_irq(&conf
->resync_lock
);
728 static void unfreeze_array(conf_t
*conf
)
730 /* reverse the effect of the freeze */
731 spin_lock_irq(&conf
->resync_lock
);
734 wake_up(&conf
->wait_barrier
);
735 spin_unlock_irq(&conf
->resync_lock
);
738 static int make_request(struct request_queue
*q
, struct bio
* bio
)
740 mddev_t
*mddev
= q
->queuedata
;
741 conf_t
*conf
= mddev_to_conf(mddev
);
742 mirror_info_t
*mirror
;
744 struct bio
*read_bio
;
746 int chunk_sects
= conf
->chunk_mask
+ 1;
747 const int rw
= bio_data_dir(bio
);
748 const int do_sync
= bio_sync(bio
);
752 if (unlikely(bio_barrier(bio
))) {
753 bio_endio(bio
, -EOPNOTSUPP
);
757 /* If this request crosses a chunk boundary, we need to
758 * split it. This will only happen for 1 PAGE (or less) requests.
760 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
762 conf
->near_copies
< conf
->raid_disks
)) {
764 /* Sanity check -- queue functions should prevent this happening */
765 if (bio
->bi_vcnt
!= 1 ||
768 /* This is a one page bio that upper layers
769 * refuse to split for us, so we need to split it.
771 bp
= bio_split(bio
, bio_split_pool
,
772 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
773 if (make_request(q
, &bp
->bio1
))
774 generic_make_request(&bp
->bio1
);
775 if (make_request(q
, &bp
->bio2
))
776 generic_make_request(&bp
->bio2
);
778 bio_pair_release(bp
);
781 printk("raid10_make_request bug: can't convert block across chunks"
782 " or bigger than %dk %llu %d\n", chunk_sects
/2,
783 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
789 md_write_start(mddev
, bio
);
792 * Register the new request and wait if the reconstruction
793 * thread has put up a bar for new requests.
794 * Continue immediately if no resync is active currently.
798 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
799 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bio
));
801 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
803 r10_bio
->master_bio
= bio
;
804 r10_bio
->sectors
= bio
->bi_size
>> 9;
806 r10_bio
->mddev
= mddev
;
807 r10_bio
->sector
= bio
->bi_sector
;
812 * read balancing logic:
814 int disk
= read_balance(conf
, r10_bio
);
815 int slot
= r10_bio
->read_slot
;
817 raid_end_bio_io(r10_bio
);
820 mirror
= conf
->mirrors
+ disk
;
822 read_bio
= bio_clone(bio
, GFP_NOIO
);
824 r10_bio
->devs
[slot
].bio
= read_bio
;
826 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
827 mirror
->rdev
->data_offset
;
828 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
829 read_bio
->bi_end_io
= raid10_end_read_request
;
830 read_bio
->bi_rw
= READ
| do_sync
;
831 read_bio
->bi_private
= r10_bio
;
833 generic_make_request(read_bio
);
840 /* first select target devices under spinlock and
841 * inc refcount on their rdev. Record them by setting
844 raid10_find_phys(conf
, r10_bio
);
846 for (i
= 0; i
< conf
->copies
; i
++) {
847 int d
= r10_bio
->devs
[i
].devnum
;
848 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
850 !test_bit(Faulty
, &rdev
->flags
)) {
851 atomic_inc(&rdev
->nr_pending
);
852 r10_bio
->devs
[i
].bio
= bio
;
854 r10_bio
->devs
[i
].bio
= NULL
;
855 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
860 atomic_set(&r10_bio
->remaining
, 0);
863 for (i
= 0; i
< conf
->copies
; i
++) {
865 int d
= r10_bio
->devs
[i
].devnum
;
866 if (!r10_bio
->devs
[i
].bio
)
869 mbio
= bio_clone(bio
, GFP_NOIO
);
870 r10_bio
->devs
[i
].bio
= mbio
;
872 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
873 conf
->mirrors
[d
].rdev
->data_offset
;
874 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
875 mbio
->bi_end_io
= raid10_end_write_request
;
876 mbio
->bi_rw
= WRITE
| do_sync
;
877 mbio
->bi_private
= r10_bio
;
879 atomic_inc(&r10_bio
->remaining
);
880 bio_list_add(&bl
, mbio
);
883 if (unlikely(!atomic_read(&r10_bio
->remaining
))) {
884 /* the array is dead */
886 raid_end_bio_io(r10_bio
);
890 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
891 spin_lock_irqsave(&conf
->device_lock
, flags
);
892 bio_list_merge(&conf
->pending_bio_list
, &bl
);
893 blk_plug_device(mddev
->queue
);
894 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
897 md_wakeup_thread(mddev
->thread
);
902 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
904 conf_t
*conf
= mddev_to_conf(mddev
);
907 if (conf
->near_copies
< conf
->raid_disks
)
908 seq_printf(seq
, " %dK chunks", mddev
->chunk_size
/1024);
909 if (conf
->near_copies
> 1)
910 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
911 if (conf
->far_copies
> 1) {
912 if (conf
->far_offset
)
913 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
915 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
917 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
918 conf
->raid_disks
- mddev
->degraded
);
919 for (i
= 0; i
< conf
->raid_disks
; i
++)
920 seq_printf(seq
, "%s",
921 conf
->mirrors
[i
].rdev
&&
922 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
923 seq_printf(seq
, "]");
926 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
928 char b
[BDEVNAME_SIZE
];
929 conf_t
*conf
= mddev_to_conf(mddev
);
932 * If it is not operational, then we have already marked it as dead
933 * else if it is the last working disks, ignore the error, let the
934 * next level up know.
935 * else mark the drive as failed
937 if (test_bit(In_sync
, &rdev
->flags
)
938 && conf
->raid_disks
-mddev
->degraded
== 1)
940 * Don't fail the drive, just return an IO error.
941 * The test should really be more sophisticated than
942 * "working_disks == 1", but it isn't critical, and
943 * can wait until we do more sophisticated "is the drive
944 * really dead" tests...
947 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
949 spin_lock_irqsave(&conf
->device_lock
, flags
);
951 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
953 * if recovery is running, make sure it aborts.
955 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
957 set_bit(Faulty
, &rdev
->flags
);
958 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
959 printk(KERN_ALERT
"raid10: Disk failure on %s, disabling device. \n"
960 " Operation continuing on %d devices\n",
961 bdevname(rdev
->bdev
,b
), conf
->raid_disks
- mddev
->degraded
);
964 static void print_conf(conf_t
*conf
)
969 printk("RAID10 conf printout:\n");
974 printk(" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
977 for (i
= 0; i
< conf
->raid_disks
; i
++) {
978 char b
[BDEVNAME_SIZE
];
979 tmp
= conf
->mirrors
+ i
;
981 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
982 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
983 !test_bit(Faulty
, &tmp
->rdev
->flags
),
984 bdevname(tmp
->rdev
->bdev
,b
));
988 static void close_sync(conf_t
*conf
)
993 mempool_destroy(conf
->r10buf_pool
);
994 conf
->r10buf_pool
= NULL
;
997 /* check if there are enough drives for
998 * every block to appear on atleast one
1000 static int enough(conf_t
*conf
)
1005 int n
= conf
->copies
;
1008 if (conf
->mirrors
[first
].rdev
)
1010 first
= (first
+1) % conf
->raid_disks
;
1014 } while (first
!= 0);
1018 static int raid10_spare_active(mddev_t
*mddev
)
1021 conf_t
*conf
= mddev
->private;
1025 * Find all non-in_sync disks within the RAID10 configuration
1026 * and mark them in_sync
1028 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1029 tmp
= conf
->mirrors
+ i
;
1031 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1032 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1033 unsigned long flags
;
1034 spin_lock_irqsave(&conf
->device_lock
, flags
);
1036 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1045 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1047 conf_t
*conf
= mddev
->private;
1052 if (mddev
->recovery_cp
< MaxSector
)
1053 /* only hot-add to in-sync arrays, as recovery is
1054 * very different from resync
1060 if (rdev
->saved_raid_disk
>= 0 &&
1061 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1062 mirror
= rdev
->saved_raid_disk
;
1065 for ( ; mirror
< mddev
->raid_disks
; mirror
++)
1066 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1068 blk_queue_stack_limits(mddev
->queue
,
1069 rdev
->bdev
->bd_disk
->queue
);
1070 /* as we don't honour merge_bvec_fn, we must never risk
1071 * violating it, so limit ->max_sector to one PAGE, as
1072 * a one page request is never in violation.
1074 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
1075 mddev
->queue
->max_sectors
> (PAGE_SIZE
>>9))
1076 mddev
->queue
->max_sectors
= (PAGE_SIZE
>>9);
1078 p
->head_position
= 0;
1079 rdev
->raid_disk
= mirror
;
1081 if (rdev
->saved_raid_disk
!= mirror
)
1083 rcu_assign_pointer(p
->rdev
, rdev
);
1091 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1093 conf_t
*conf
= mddev
->private;
1096 mirror_info_t
*p
= conf
->mirrors
+ number
;
1101 if (test_bit(In_sync
, &rdev
->flags
) ||
1102 atomic_read(&rdev
->nr_pending
)) {
1108 if (atomic_read(&rdev
->nr_pending
)) {
1109 /* lost the race, try later */
1121 static void end_sync_read(struct bio
*bio
, int error
)
1123 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
1124 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
1127 for (i
=0; i
<conf
->copies
; i
++)
1128 if (r10_bio
->devs
[i
].bio
== bio
)
1130 BUG_ON(i
== conf
->copies
);
1131 update_head_pos(i
, r10_bio
);
1132 d
= r10_bio
->devs
[i
].devnum
;
1134 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1135 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1137 atomic_add(r10_bio
->sectors
,
1138 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1139 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1140 md_error(r10_bio
->mddev
,
1141 conf
->mirrors
[d
].rdev
);
1144 /* for reconstruct, we always reschedule after a read.
1145 * for resync, only after all reads
1147 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1148 atomic_dec_and_test(&r10_bio
->remaining
)) {
1149 /* we have read all the blocks,
1150 * do the comparison in process context in raid10d
1152 reschedule_retry(r10_bio
);
1154 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1157 static void end_sync_write(struct bio
*bio
, int error
)
1159 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1160 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
1161 mddev_t
*mddev
= r10_bio
->mddev
;
1162 conf_t
*conf
= mddev_to_conf(mddev
);
1165 for (i
= 0; i
< conf
->copies
; i
++)
1166 if (r10_bio
->devs
[i
].bio
== bio
)
1168 d
= r10_bio
->devs
[i
].devnum
;
1171 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1172 update_head_pos(i
, r10_bio
);
1174 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1175 if (r10_bio
->master_bio
== NULL
) {
1176 /* the primary of several recovery bios */
1177 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1181 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1186 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1190 * Note: sync and recover and handled very differently for raid10
1191 * This code is for resync.
1192 * For resync, we read through virtual addresses and read all blocks.
1193 * If there is any error, we schedule a write. The lowest numbered
1194 * drive is authoritative.
1195 * However requests come for physical address, so we need to map.
1196 * For every physical address there are raid_disks/copies virtual addresses,
1197 * which is always are least one, but is not necessarly an integer.
1198 * This means that a physical address can span multiple chunks, so we may
1199 * have to submit multiple io requests for a single sync request.
1202 * We check if all blocks are in-sync and only write to blocks that
1205 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1207 conf_t
*conf
= mddev_to_conf(mddev
);
1209 struct bio
*tbio
, *fbio
;
1211 atomic_set(&r10_bio
->remaining
, 1);
1213 /* find the first device with a block */
1214 for (i
=0; i
<conf
->copies
; i
++)
1215 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1218 if (i
== conf
->copies
)
1222 fbio
= r10_bio
->devs
[i
].bio
;
1224 /* now find blocks with errors */
1225 for (i
=0 ; i
< conf
->copies
; i
++) {
1227 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1229 tbio
= r10_bio
->devs
[i
].bio
;
1231 if (tbio
->bi_end_io
!= end_sync_read
)
1235 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1236 /* We know that the bi_io_vec layout is the same for
1237 * both 'first' and 'i', so we just compare them.
1238 * All vec entries are PAGE_SIZE;
1240 for (j
= 0; j
< vcnt
; j
++)
1241 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1242 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1247 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1249 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1250 /* Don't fix anything. */
1252 /* Ok, we need to write this bio
1253 * First we need to fixup bv_offset, bv_len and
1254 * bi_vecs, as the read request might have corrupted these
1256 tbio
->bi_vcnt
= vcnt
;
1257 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1259 tbio
->bi_phys_segments
= 0;
1260 tbio
->bi_hw_segments
= 0;
1261 tbio
->bi_hw_front_size
= 0;
1262 tbio
->bi_hw_back_size
= 0;
1263 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1264 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1265 tbio
->bi_next
= NULL
;
1266 tbio
->bi_rw
= WRITE
;
1267 tbio
->bi_private
= r10_bio
;
1268 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1270 for (j
=0; j
< vcnt
; j
++) {
1271 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1272 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1274 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1275 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1278 tbio
->bi_end_io
= end_sync_write
;
1280 d
= r10_bio
->devs
[i
].devnum
;
1281 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1282 atomic_inc(&r10_bio
->remaining
);
1283 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1285 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1286 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1287 generic_make_request(tbio
);
1291 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1292 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1298 * Now for the recovery code.
1299 * Recovery happens across physical sectors.
1300 * We recover all non-is_sync drives by finding the virtual address of
1301 * each, and then choose a working drive that also has that virt address.
1302 * There is a separate r10_bio for each non-in_sync drive.
1303 * Only the first two slots are in use. The first for reading,
1304 * The second for writing.
1308 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1310 conf_t
*conf
= mddev_to_conf(mddev
);
1312 struct bio
*bio
, *wbio
;
1315 /* move the pages across to the second bio
1316 * and submit the write request
1318 bio
= r10_bio
->devs
[0].bio
;
1319 wbio
= r10_bio
->devs
[1].bio
;
1320 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1321 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1322 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1323 wbio
->bi_io_vec
[i
].bv_page
= p
;
1325 d
= r10_bio
->devs
[1].devnum
;
1327 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1328 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1329 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1330 generic_make_request(wbio
);
1332 bio_endio(wbio
, -EIO
);
1337 * This is a kernel thread which:
1339 * 1. Retries failed read operations on working mirrors.
1340 * 2. Updates the raid superblock when problems encounter.
1341 * 3. Performs writes following reads for array synchronising.
1344 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1346 int sect
= 0; /* Offset from r10_bio->sector */
1347 int sectors
= r10_bio
->sectors
;
1351 int sl
= r10_bio
->read_slot
;
1355 if (s
> (PAGE_SIZE
>>9))
1360 int d
= r10_bio
->devs
[sl
].devnum
;
1361 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1363 test_bit(In_sync
, &rdev
->flags
)) {
1364 atomic_inc(&rdev
->nr_pending
);
1366 success
= sync_page_io(rdev
->bdev
,
1367 r10_bio
->devs
[sl
].addr
+
1368 sect
+ rdev
->data_offset
,
1370 conf
->tmppage
, READ
);
1371 rdev_dec_pending(rdev
, mddev
);
1377 if (sl
== conf
->copies
)
1379 } while (!success
&& sl
!= r10_bio
->read_slot
);
1383 /* Cannot read from anywhere -- bye bye array */
1384 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1385 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1390 /* write it back and re-read */
1392 while (sl
!= r10_bio
->read_slot
) {
1397 d
= r10_bio
->devs
[sl
].devnum
;
1398 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1400 test_bit(In_sync
, &rdev
->flags
)) {
1401 atomic_inc(&rdev
->nr_pending
);
1403 atomic_add(s
, &rdev
->corrected_errors
);
1404 if (sync_page_io(rdev
->bdev
,
1405 r10_bio
->devs
[sl
].addr
+
1406 sect
+ rdev
->data_offset
,
1407 s
<<9, conf
->tmppage
, WRITE
)
1409 /* Well, this device is dead */
1410 md_error(mddev
, rdev
);
1411 rdev_dec_pending(rdev
, mddev
);
1416 while (sl
!= r10_bio
->read_slot
) {
1421 d
= r10_bio
->devs
[sl
].devnum
;
1422 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1424 test_bit(In_sync
, &rdev
->flags
)) {
1425 char b
[BDEVNAME_SIZE
];
1426 atomic_inc(&rdev
->nr_pending
);
1428 if (sync_page_io(rdev
->bdev
,
1429 r10_bio
->devs
[sl
].addr
+
1430 sect
+ rdev
->data_offset
,
1431 s
<<9, conf
->tmppage
, READ
) == 0)
1432 /* Well, this device is dead */
1433 md_error(mddev
, rdev
);
1436 "raid10:%s: read error corrected"
1437 " (%d sectors at %llu on %s)\n",
1439 (unsigned long long)(sect
+
1441 bdevname(rdev
->bdev
, b
));
1443 rdev_dec_pending(rdev
, mddev
);
1454 static void raid10d(mddev_t
*mddev
)
1458 unsigned long flags
;
1459 conf_t
*conf
= mddev_to_conf(mddev
);
1460 struct list_head
*head
= &conf
->retry_list
;
1464 md_check_recovery(mddev
);
1467 char b
[BDEVNAME_SIZE
];
1468 spin_lock_irqsave(&conf
->device_lock
, flags
);
1470 if (conf
->pending_bio_list
.head
) {
1471 bio
= bio_list_get(&conf
->pending_bio_list
);
1472 blk_remove_plug(mddev
->queue
);
1473 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1474 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1475 bitmap_unplug(mddev
->bitmap
);
1477 while (bio
) { /* submit pending writes */
1478 struct bio
*next
= bio
->bi_next
;
1479 bio
->bi_next
= NULL
;
1480 generic_make_request(bio
);
1488 if (list_empty(head
))
1490 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1491 list_del(head
->prev
);
1493 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1495 mddev
= r10_bio
->mddev
;
1496 conf
= mddev_to_conf(mddev
);
1497 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
)) {
1498 sync_request_write(mddev
, r10_bio
);
1500 } else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
1501 recovery_request_write(mddev
, r10_bio
);
1505 /* we got a read error. Maybe the drive is bad. Maybe just
1506 * the block and we can fix it.
1507 * We freeze all other IO, and try reading the block from
1508 * other devices. When we find one, we re-write
1509 * and check it that fixes the read error.
1510 * This is all done synchronously while the array is
1513 if (mddev
->ro
== 0) {
1515 fix_read_error(conf
, mddev
, r10_bio
);
1516 unfreeze_array(conf
);
1519 bio
= r10_bio
->devs
[r10_bio
->read_slot
].bio
;
1520 r10_bio
->devs
[r10_bio
->read_slot
].bio
=
1521 mddev
->ro
? IO_BLOCKED
: NULL
;
1522 mirror
= read_balance(conf
, r10_bio
);
1524 printk(KERN_ALERT
"raid10: %s: unrecoverable I/O"
1525 " read error for block %llu\n",
1526 bdevname(bio
->bi_bdev
,b
),
1527 (unsigned long long)r10_bio
->sector
);
1528 raid_end_bio_io(r10_bio
);
1531 const int do_sync
= bio_sync(r10_bio
->master_bio
);
1533 rdev
= conf
->mirrors
[mirror
].rdev
;
1534 if (printk_ratelimit())
1535 printk(KERN_ERR
"raid10: %s: redirecting sector %llu to"
1536 " another mirror\n",
1537 bdevname(rdev
->bdev
,b
),
1538 (unsigned long long)r10_bio
->sector
);
1539 bio
= bio_clone(r10_bio
->master_bio
, GFP_NOIO
);
1540 r10_bio
->devs
[r10_bio
->read_slot
].bio
= bio
;
1541 bio
->bi_sector
= r10_bio
->devs
[r10_bio
->read_slot
].addr
1542 + rdev
->data_offset
;
1543 bio
->bi_bdev
= rdev
->bdev
;
1544 bio
->bi_rw
= READ
| do_sync
;
1545 bio
->bi_private
= r10_bio
;
1546 bio
->bi_end_io
= raid10_end_read_request
;
1548 generic_make_request(bio
);
1552 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1554 unplug_slaves(mddev
);
1558 static int init_resync(conf_t
*conf
)
1562 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1563 BUG_ON(conf
->r10buf_pool
);
1564 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1565 if (!conf
->r10buf_pool
)
1567 conf
->next_resync
= 0;
1572 * perform a "sync" on one "block"
1574 * We need to make sure that no normal I/O request - particularly write
1575 * requests - conflict with active sync requests.
1577 * This is achieved by tracking pending requests and a 'barrier' concept
1578 * that can be installed to exclude normal IO requests.
1580 * Resync and recovery are handled very differently.
1581 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1583 * For resync, we iterate over virtual addresses, read all copies,
1584 * and update if there are differences. If only one copy is live,
1586 * For recovery, we iterate over physical addresses, read a good
1587 * value for each non-in_sync drive, and over-write.
1589 * So, for recovery we may have several outstanding complex requests for a
1590 * given address, one for each out-of-sync device. We model this by allocating
1591 * a number of r10_bio structures, one for each out-of-sync device.
1592 * As we setup these structures, we collect all bio's together into a list
1593 * which we then process collectively to add pages, and then process again
1594 * to pass to generic_make_request.
1596 * The r10_bio structures are linked using a borrowed master_bio pointer.
1597 * This link is counted in ->remaining. When the r10_bio that points to NULL
1598 * has its remaining count decremented to 0, the whole complex operation
1603 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1605 conf_t
*conf
= mddev_to_conf(mddev
);
1607 struct bio
*biolist
= NULL
, *bio
;
1608 sector_t max_sector
, nr_sectors
;
1614 sector_t sectors_skipped
= 0;
1615 int chunks_skipped
= 0;
1617 if (!conf
->r10buf_pool
)
1618 if (init_resync(conf
))
1622 max_sector
= mddev
->size
<< 1;
1623 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1624 max_sector
= mddev
->resync_max_sectors
;
1625 if (sector_nr
>= max_sector
) {
1626 /* If we aborted, we need to abort the
1627 * sync on the 'current' bitmap chucks (there can
1628 * be several when recovering multiple devices).
1629 * as we may have started syncing it but not finished.
1630 * We can find the current address in
1631 * mddev->curr_resync, but for recovery,
1632 * we need to convert that to several
1633 * virtual addresses.
1635 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1636 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1637 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1639 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1641 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1642 bitmap_end_sync(mddev
->bitmap
, sect
,
1645 } else /* completed sync */
1648 bitmap_close_sync(mddev
->bitmap
);
1651 return sectors_skipped
;
1653 if (chunks_skipped
>= conf
->raid_disks
) {
1654 /* if there has been nothing to do on any drive,
1655 * then there is nothing to do at all..
1658 return (max_sector
- sector_nr
) + sectors_skipped
;
1661 /* make sure whole request will fit in a chunk - if chunks
1664 if (conf
->near_copies
< conf
->raid_disks
&&
1665 max_sector
> (sector_nr
| conf
->chunk_mask
))
1666 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1668 * If there is non-resync activity waiting for us then
1669 * put in a delay to throttle resync.
1671 if (!go_faster
&& conf
->nr_waiting
)
1672 msleep_interruptible(1000);
1674 /* Again, very different code for resync and recovery.
1675 * Both must result in an r10bio with a list of bios that
1676 * have bi_end_io, bi_sector, bi_bdev set,
1677 * and bi_private set to the r10bio.
1678 * For recovery, we may actually create several r10bios
1679 * with 2 bios in each, that correspond to the bios in the main one.
1680 * In this case, the subordinate r10bios link back through a
1681 * borrowed master_bio pointer, and the counter in the master
1682 * includes a ref from each subordinate.
1684 /* First, we decide what to do and set ->bi_end_io
1685 * To end_sync_read if we want to read, and
1686 * end_sync_write if we will want to write.
1689 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
1690 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1691 /* recovery... the complicated one */
1695 for (i
=0 ; i
<conf
->raid_disks
; i
++)
1696 if (conf
->mirrors
[i
].rdev
&&
1697 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
)) {
1698 int still_degraded
= 0;
1699 /* want to reconstruct this device */
1700 r10bio_t
*rb2
= r10_bio
;
1701 sector_t sect
= raid10_find_virt(conf
, sector_nr
, i
);
1703 /* Unless we are doing a full sync, we only need
1704 * to recover the block if it is set in the bitmap
1706 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1708 if (sync_blocks
< max_sync
)
1709 max_sync
= sync_blocks
;
1712 /* yep, skip the sync_blocks here, but don't assume
1713 * that there will never be anything to do here
1715 chunks_skipped
= -1;
1719 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1720 raise_barrier(conf
, rb2
!= NULL
);
1721 atomic_set(&r10_bio
->remaining
, 0);
1723 r10_bio
->master_bio
= (struct bio
*)rb2
;
1725 atomic_inc(&rb2
->remaining
);
1726 r10_bio
->mddev
= mddev
;
1727 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1728 r10_bio
->sector
= sect
;
1730 raid10_find_phys(conf
, r10_bio
);
1731 /* Need to check if this section will still be
1734 for (j
=0; j
<conf
->copies
;j
++) {
1735 int d
= r10_bio
->devs
[j
].devnum
;
1736 if (conf
->mirrors
[d
].rdev
== NULL
||
1737 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
)) {
1742 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1743 &sync_blocks
, still_degraded
);
1745 for (j
=0; j
<conf
->copies
;j
++) {
1746 int d
= r10_bio
->devs
[j
].devnum
;
1747 if (conf
->mirrors
[d
].rdev
&&
1748 test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
)) {
1749 /* This is where we read from */
1750 bio
= r10_bio
->devs
[0].bio
;
1751 bio
->bi_next
= biolist
;
1753 bio
->bi_private
= r10_bio
;
1754 bio
->bi_end_io
= end_sync_read
;
1756 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1757 conf
->mirrors
[d
].rdev
->data_offset
;
1758 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1759 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1760 atomic_inc(&r10_bio
->remaining
);
1761 /* and we write to 'i' */
1763 for (k
=0; k
<conf
->copies
; k
++)
1764 if (r10_bio
->devs
[k
].devnum
== i
)
1766 BUG_ON(k
== conf
->copies
);
1767 bio
= r10_bio
->devs
[1].bio
;
1768 bio
->bi_next
= biolist
;
1770 bio
->bi_private
= r10_bio
;
1771 bio
->bi_end_io
= end_sync_write
;
1773 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1774 conf
->mirrors
[i
].rdev
->data_offset
;
1775 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1777 r10_bio
->devs
[0].devnum
= d
;
1778 r10_bio
->devs
[1].devnum
= i
;
1783 if (j
== conf
->copies
) {
1784 /* Cannot recover, so abort the recovery */
1787 if (!test_and_set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
1788 printk(KERN_INFO
"raid10: %s: insufficient working devices for recovery.\n",
1793 if (biolist
== NULL
) {
1795 r10bio_t
*rb2
= r10_bio
;
1796 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
1797 rb2
->master_bio
= NULL
;
1803 /* resync. Schedule a read for every block at this virt offset */
1806 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
1807 &sync_blocks
, mddev
->degraded
) &&
1808 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
1809 /* We can skip this block */
1811 return sync_blocks
+ sectors_skipped
;
1813 if (sync_blocks
< max_sync
)
1814 max_sync
= sync_blocks
;
1815 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1817 r10_bio
->mddev
= mddev
;
1818 atomic_set(&r10_bio
->remaining
, 0);
1819 raise_barrier(conf
, 0);
1820 conf
->next_resync
= sector_nr
;
1822 r10_bio
->master_bio
= NULL
;
1823 r10_bio
->sector
= sector_nr
;
1824 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
1825 raid10_find_phys(conf
, r10_bio
);
1826 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
1828 for (i
=0; i
<conf
->copies
; i
++) {
1829 int d
= r10_bio
->devs
[i
].devnum
;
1830 bio
= r10_bio
->devs
[i
].bio
;
1831 bio
->bi_end_io
= NULL
;
1832 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1833 if (conf
->mirrors
[d
].rdev
== NULL
||
1834 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
1836 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1837 atomic_inc(&r10_bio
->remaining
);
1838 bio
->bi_next
= biolist
;
1840 bio
->bi_private
= r10_bio
;
1841 bio
->bi_end_io
= end_sync_read
;
1843 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
1844 conf
->mirrors
[d
].rdev
->data_offset
;
1845 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1850 for (i
=0; i
<conf
->copies
; i
++) {
1851 int d
= r10_bio
->devs
[i
].devnum
;
1852 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
1853 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1861 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
1863 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1865 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
1868 bio
->bi_phys_segments
= 0;
1869 bio
->bi_hw_segments
= 0;
1874 if (sector_nr
+ max_sync
< max_sector
)
1875 max_sector
= sector_nr
+ max_sync
;
1878 int len
= PAGE_SIZE
;
1880 if (sector_nr
+ (len
>>9) > max_sector
)
1881 len
= (max_sector
- sector_nr
) << 9;
1884 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
1885 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
1886 if (bio_add_page(bio
, page
, len
, 0) == 0) {
1889 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
1890 for (bio2
= biolist
; bio2
&& bio2
!= bio
; bio2
= bio2
->bi_next
) {
1891 /* remove last page from this bio */
1893 bio2
->bi_size
-= len
;
1894 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
1900 nr_sectors
+= len
>>9;
1901 sector_nr
+= len
>>9;
1902 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
1904 r10_bio
->sectors
= nr_sectors
;
1908 biolist
= biolist
->bi_next
;
1910 bio
->bi_next
= NULL
;
1911 r10_bio
= bio
->bi_private
;
1912 r10_bio
->sectors
= nr_sectors
;
1914 if (bio
->bi_end_io
== end_sync_read
) {
1915 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
1916 generic_make_request(bio
);
1920 if (sectors_skipped
)
1921 /* pretend they weren't skipped, it makes
1922 * no important difference in this case
1924 md_done_sync(mddev
, sectors_skipped
, 1);
1926 return sectors_skipped
+ nr_sectors
;
1928 /* There is nowhere to write, so all non-sync
1929 * drives must be failed, so try the next chunk...
1932 sector_t sec
= max_sector
- sector_nr
;
1933 sectors_skipped
+= sec
;
1935 sector_nr
= max_sector
;
1940 static int run(mddev_t
*mddev
)
1944 mirror_info_t
*disk
;
1946 struct list_head
*tmp
;
1948 sector_t stride
, size
;
1950 if (mddev
->chunk_size
== 0) {
1951 printk(KERN_ERR
"md/raid10: non-zero chunk size required.\n");
1955 nc
= mddev
->layout
& 255;
1956 fc
= (mddev
->layout
>> 8) & 255;
1957 fo
= mddev
->layout
& (1<<16);
1958 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
1959 (mddev
->layout
>> 17)) {
1960 printk(KERN_ERR
"raid10: %s: unsupported raid10 layout: 0x%8x\n",
1961 mdname(mddev
), mddev
->layout
);
1965 * copy the already verified devices into our private RAID10
1966 * bookkeeping area. [whatever we allocate in run(),
1967 * should be freed in stop()]
1969 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
1970 mddev
->private = conf
;
1972 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
1976 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
1978 if (!conf
->mirrors
) {
1979 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
1984 conf
->tmppage
= alloc_page(GFP_KERNEL
);
1988 conf
->mddev
= mddev
;
1989 conf
->raid_disks
= mddev
->raid_disks
;
1990 conf
->near_copies
= nc
;
1991 conf
->far_copies
= fc
;
1992 conf
->copies
= nc
*fc
;
1993 conf
->far_offset
= fo
;
1994 conf
->chunk_mask
= (sector_t
)(mddev
->chunk_size
>>9)-1;
1995 conf
->chunk_shift
= ffz(~mddev
->chunk_size
) - 9;
1996 size
= mddev
->size
>> (conf
->chunk_shift
-1);
1997 sector_div(size
, fc
);
1998 size
= size
* conf
->raid_disks
;
1999 sector_div(size
, nc
);
2000 /* 'size' is now the number of chunks in the array */
2001 /* calculate "used chunks per device" in 'stride' */
2002 stride
= size
* conf
->copies
;
2004 /* We need to round up when dividing by raid_disks to
2005 * get the stride size.
2007 stride
+= conf
->raid_disks
- 1;
2008 sector_div(stride
, conf
->raid_disks
);
2009 mddev
->size
= stride
<< (conf
->chunk_shift
-1);
2014 sector_div(stride
, fc
);
2015 conf
->stride
= stride
<< conf
->chunk_shift
;
2017 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2018 r10bio_pool_free
, conf
);
2019 if (!conf
->r10bio_pool
) {
2020 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2025 ITERATE_RDEV(mddev
, rdev
, tmp
) {
2026 disk_idx
= rdev
->raid_disk
;
2027 if (disk_idx
>= mddev
->raid_disks
2030 disk
= conf
->mirrors
+ disk_idx
;
2034 blk_queue_stack_limits(mddev
->queue
,
2035 rdev
->bdev
->bd_disk
->queue
);
2036 /* as we don't honour merge_bvec_fn, we must never risk
2037 * violating it, so limit ->max_sector to one PAGE, as
2038 * a one page request is never in violation.
2040 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
2041 mddev
->queue
->max_sectors
> (PAGE_SIZE
>>9))
2042 mddev
->queue
->max_sectors
= (PAGE_SIZE
>>9);
2044 disk
->head_position
= 0;
2046 spin_lock_init(&conf
->device_lock
);
2047 INIT_LIST_HEAD(&conf
->retry_list
);
2049 spin_lock_init(&conf
->resync_lock
);
2050 init_waitqueue_head(&conf
->wait_barrier
);
2052 /* need to check that every block has at least one working mirror */
2053 if (!enough(conf
)) {
2054 printk(KERN_ERR
"raid10: not enough operational mirrors for %s\n",
2059 mddev
->degraded
= 0;
2060 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2062 disk
= conf
->mirrors
+ i
;
2065 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2066 disk
->head_position
= 0;
2072 mddev
->thread
= md_register_thread(raid10d
, mddev
, "%s_raid10");
2073 if (!mddev
->thread
) {
2075 "raid10: couldn't allocate thread for %s\n",
2081 "raid10: raid set %s active with %d out of %d devices\n",
2082 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2085 * Ok, everything is just fine now
2087 mddev
->array_size
= size
<< (conf
->chunk_shift
-1);
2088 mddev
->resync_max_sectors
= size
<< conf
->chunk_shift
;
2090 mddev
->queue
->unplug_fn
= raid10_unplug
;
2091 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2092 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2094 /* Calculate max read-ahead size.
2095 * We need to readahead at least twice a whole stripe....
2099 int stripe
= conf
->raid_disks
* (mddev
->chunk_size
/ PAGE_SIZE
);
2100 stripe
/= conf
->near_copies
;
2101 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2102 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2105 if (conf
->near_copies
< mddev
->raid_disks
)
2106 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2110 if (conf
->r10bio_pool
)
2111 mempool_destroy(conf
->r10bio_pool
);
2112 safe_put_page(conf
->tmppage
);
2113 kfree(conf
->mirrors
);
2115 mddev
->private = NULL
;
2120 static int stop(mddev_t
*mddev
)
2122 conf_t
*conf
= mddev_to_conf(mddev
);
2124 md_unregister_thread(mddev
->thread
);
2125 mddev
->thread
= NULL
;
2126 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2127 if (conf
->r10bio_pool
)
2128 mempool_destroy(conf
->r10bio_pool
);
2129 kfree(conf
->mirrors
);
2131 mddev
->private = NULL
;
2135 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2137 conf_t
*conf
= mddev_to_conf(mddev
);
2141 raise_barrier(conf
, 0);
2144 lower_barrier(conf
);
2147 if (mddev
->thread
) {
2149 mddev
->thread
->timeout
= mddev
->bitmap
->daemon_sleep
* HZ
;
2151 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
2152 md_wakeup_thread(mddev
->thread
);
2156 static struct mdk_personality raid10_personality
=
2160 .owner
= THIS_MODULE
,
2161 .make_request
= make_request
,
2165 .error_handler
= error
,
2166 .hot_add_disk
= raid10_add_disk
,
2167 .hot_remove_disk
= raid10_remove_disk
,
2168 .spare_active
= raid10_spare_active
,
2169 .sync_request
= sync_request
,
2170 .quiesce
= raid10_quiesce
,
2173 static int __init
raid_init(void)
2175 return register_md_personality(&raid10_personality
);
2178 static void raid_exit(void)
2180 unregister_md_personality(&raid10_personality
);
2183 module_init(raid_init
);
2184 module_exit(raid_exit
);
2185 MODULE_LICENSE("GPL");
2186 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2187 MODULE_ALIAS("md-raid10");
2188 MODULE_ALIAS("md-level-10");