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/raid/raid10.h>
24 * RAID10 provides a combination of RAID0 and RAID1 functionality.
25 * The layout of data is defined by
28 * near_copies (stored in low byte of layout)
29 * far_copies (stored in second byte of layout)
31 * The data to be stored is divided into chunks using chunksize.
32 * Each device is divided into far_copies sections.
33 * In each section, chunks are laid out in a style similar to raid0, but
34 * near_copies copies of each chunk is stored (each on a different drive).
35 * The starting device for each section is offset near_copies from the starting
36 * device of the previous section.
37 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
39 * near_copies and far_copies must be at least one, and their product is at most
44 * Number of guaranteed r10bios in case of extreme VM load:
46 #define NR_RAID10_BIOS 256
48 static void unplug_slaves(mddev_t
*mddev
);
50 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
54 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
56 /* allocate a r10bio with room for raid_disks entries in the bios array */
57 r10_bio
= kmalloc(size
, gfp_flags
);
59 memset(r10_bio
, 0, size
);
61 unplug_slaves(conf
->mddev
);
66 static void r10bio_pool_free(void *r10_bio
, void *data
)
71 #define RESYNC_BLOCK_SIZE (64*1024)
72 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
73 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
74 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
75 #define RESYNC_WINDOW (2048*1024)
78 * When performing a resync, we need to read and compare, so
79 * we need as many pages are there are copies.
80 * When performing a recovery, we need 2 bios, one for read,
81 * one for write (we recover only one drive per r10buf)
84 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
93 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
95 unplug_slaves(conf
->mddev
);
99 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
100 nalloc
= conf
->copies
; /* resync */
102 nalloc
= 2; /* recovery */
107 for (j
= nalloc
; j
-- ; ) {
108 bio
= bio_alloc(gfp_flags
, RESYNC_PAGES
);
111 r10_bio
->devs
[j
].bio
= bio
;
114 * Allocate RESYNC_PAGES data pages and attach them
117 for (j
= 0 ; j
< nalloc
; j
++) {
118 bio
= r10_bio
->devs
[j
].bio
;
119 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
120 page
= alloc_page(gfp_flags
);
124 bio
->bi_io_vec
[i
].bv_page
= page
;
132 __free_page(bio
->bi_io_vec
[i
-1].bv_page
);
134 for (i
= 0; i
< RESYNC_PAGES
; i
++)
135 __free_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
138 while ( ++j
< nalloc
)
139 bio_put(r10_bio
->devs
[j
].bio
);
140 r10bio_pool_free(r10_bio
, conf
);
144 static void r10buf_pool_free(void *__r10_bio
, void *data
)
148 r10bio_t
*r10bio
= __r10_bio
;
151 for (j
=0; j
< conf
->copies
; j
++) {
152 struct bio
*bio
= r10bio
->devs
[j
].bio
;
154 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
155 __free_page(bio
->bi_io_vec
[i
].bv_page
);
156 bio
->bi_io_vec
[i
].bv_page
= NULL
;
161 r10bio_pool_free(r10bio
, conf
);
164 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
168 for (i
= 0; i
< conf
->copies
; i
++) {
169 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
176 static inline void free_r10bio(r10bio_t
*r10_bio
)
180 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
183 * Wake up any possible resync thread that waits for the device
186 spin_lock_irqsave(&conf
->resync_lock
, flags
);
187 if (!--conf
->nr_pending
) {
188 wake_up(&conf
->wait_idle
);
189 wake_up(&conf
->wait_resume
);
191 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
193 put_all_bios(conf
, r10_bio
);
194 mempool_free(r10_bio
, conf
->r10bio_pool
);
197 static inline void put_buf(r10bio_t
*r10_bio
)
199 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
202 mempool_free(r10_bio
, conf
->r10buf_pool
);
204 spin_lock_irqsave(&conf
->resync_lock
, flags
);
208 wake_up(&conf
->wait_resume
);
209 wake_up(&conf
->wait_idle
);
211 if (!--conf
->nr_pending
) {
212 wake_up(&conf
->wait_idle
);
213 wake_up(&conf
->wait_resume
);
215 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
218 static void reschedule_retry(r10bio_t
*r10_bio
)
221 mddev_t
*mddev
= r10_bio
->mddev
;
222 conf_t
*conf
= mddev_to_conf(mddev
);
224 spin_lock_irqsave(&conf
->device_lock
, flags
);
225 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
226 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
228 md_wakeup_thread(mddev
->thread
);
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
236 static void raid_end_bio_io(r10bio_t
*r10_bio
)
238 struct bio
*bio
= r10_bio
->master_bio
;
240 bio_endio(bio
, bio
->bi_size
,
241 test_bit(R10BIO_Uptodate
, &r10_bio
->state
) ? 0 : -EIO
);
242 free_r10bio(r10_bio
);
246 * Update disk head position estimator based on IRQ completion info.
248 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
250 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
252 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
253 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
256 static int raid10_end_read_request(struct bio
*bio
, unsigned int bytes_done
, int error
)
258 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
259 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
261 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
266 slot
= r10_bio
->read_slot
;
267 dev
= r10_bio
->devs
[slot
].devnum
;
269 * this branch is our 'one mirror IO has finished' event handler:
272 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
275 * Set R10BIO_Uptodate in our master bio, so that
276 * we will return a good error code to the higher
277 * levels even if IO on some other mirrored buffer fails.
279 * The 'master' represents the composite IO operation to
280 * user-side. So if something waits for IO, then it will
281 * wait for the 'master' bio.
283 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
285 update_head_pos(slot
, r10_bio
);
288 * we have only one bio on the read side
291 raid_end_bio_io(r10_bio
);
296 char b
[BDEVNAME_SIZE
];
297 if (printk_ratelimit())
298 printk(KERN_ERR
"raid10: %s: rescheduling sector %llu\n",
299 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
300 reschedule_retry(r10_bio
);
303 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
307 static int raid10_end_write_request(struct bio
*bio
, unsigned int bytes_done
, int error
)
309 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
310 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
312 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
317 for (slot
= 0; slot
< conf
->copies
; slot
++)
318 if (r10_bio
->devs
[slot
].bio
== bio
)
320 dev
= r10_bio
->devs
[slot
].devnum
;
323 * this branch is our 'one mirror IO has finished' event handler:
326 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
329 * Set R10BIO_Uptodate in our master bio, so that
330 * we will return a good error code for to the higher
331 * levels even if IO on some other mirrored buffer fails.
333 * The 'master' represents the composite IO operation to
334 * user-side. So if something waits for IO, then it will
335 * wait for the 'master' bio.
337 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
339 update_head_pos(slot
, r10_bio
);
343 * Let's see if all mirrored write operations have finished
346 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
347 md_write_end(r10_bio
->mddev
);
348 raid_end_bio_io(r10_bio
);
351 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
357 * RAID10 layout manager
358 * Aswell as the chunksize and raid_disks count, there are two
359 * parameters: near_copies and far_copies.
360 * near_copies * far_copies must be <= raid_disks.
361 * Normally one of these will be 1.
362 * If both are 1, we get raid0.
363 * If near_copies == raid_disks, we get raid1.
365 * Chunks are layed out in raid0 style with near_copies copies of the
366 * first chunk, followed by near_copies copies of the next chunk and
368 * If far_copies > 1, then after 1/far_copies of the array has been assigned
369 * as described above, we start again with a device offset of near_copies.
370 * So we effectively have another copy of the whole array further down all
371 * the drives, but with blocks on different drives.
372 * With this layout, and block is never stored twice on the one device.
374 * raid10_find_phys finds the sector offset of a given virtual sector
375 * on each device that it is on. If a block isn't on a device,
376 * that entry in the array is set to MaxSector.
378 * raid10_find_virt does the reverse mapping, from a device and a
379 * sector offset to a virtual address
382 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
392 /* now calculate first sector/dev */
393 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
394 sector
= r10bio
->sector
& conf
->chunk_mask
;
396 chunk
*= conf
->near_copies
;
398 dev
= sector_div(stripe
, conf
->raid_disks
);
400 sector
+= stripe
<< conf
->chunk_shift
;
402 /* and calculate all the others */
403 for (n
=0; n
< conf
->near_copies
; n
++) {
406 r10bio
->devs
[slot
].addr
= sector
;
407 r10bio
->devs
[slot
].devnum
= d
;
410 for (f
= 1; f
< conf
->far_copies
; f
++) {
411 d
+= conf
->near_copies
;
412 if (d
>= conf
->raid_disks
)
413 d
-= conf
->raid_disks
;
415 r10bio
->devs
[slot
].devnum
= d
;
416 r10bio
->devs
[slot
].addr
= s
;
420 if (dev
>= conf
->raid_disks
) {
422 sector
+= (conf
->chunk_mask
+ 1);
425 BUG_ON(slot
!= conf
->copies
);
428 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
430 sector_t offset
, chunk
, vchunk
;
432 while (sector
> conf
->stride
) {
433 sector
-= conf
->stride
;
434 if (dev
< conf
->near_copies
)
435 dev
+= conf
->raid_disks
- conf
->near_copies
;
437 dev
-= conf
->near_copies
;
440 offset
= sector
& conf
->chunk_mask
;
441 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 * @bio: the buffer head that's been built up so far
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(request_queue_t
*q
, struct bio
*bio
,
458 struct bio_vec
*bio_vec
)
460 mddev_t
*mddev
= q
->queuedata
;
461 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
463 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
464 unsigned int bio_sectors
= bio
->bi_size
>> 9;
466 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
467 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
468 if (max
<= bio_vec
->bv_len
&& bio_sectors
== 0)
469 return bio_vec
->bv_len
;
475 * This routine returns the disk from which the requested read should
476 * be done. There is a per-array 'next expected sequential IO' sector
477 * number - if this matches on the next IO then we use the last disk.
478 * There is also a per-disk 'last know head position' sector that is
479 * maintained from IRQ contexts, both the normal and the resync IO
480 * completion handlers update this position correctly. If there is no
481 * perfect sequential match then we pick the disk whose head is closest.
483 * If there are 2 mirrors in the same 2 devices, performance degrades
484 * because position is mirror, not device based.
486 * The rdev for the device selected will have nr_pending incremented.
490 * FIXME: possibly should rethink readbalancing and do it differently
491 * depending on near_copies / far_copies geometry.
493 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
495 const unsigned long this_sector
= r10_bio
->sector
;
496 int disk
, slot
, nslot
;
497 const int sectors
= r10_bio
->sectors
;
498 sector_t new_distance
, current_distance
;
501 raid10_find_phys(conf
, r10_bio
);
504 * Check if we can balance. We can balance on the whole
505 * device if no resync is going on, or below the resync window.
506 * We take the first readable disk when above the resync window.
508 if (conf
->mddev
->recovery_cp
< MaxSector
509 && (this_sector
+ sectors
>= conf
->next_resync
)) {
510 /* make sure that disk is operational */
512 disk
= r10_bio
->devs
[slot
].devnum
;
514 while ((rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
517 if (slot
== conf
->copies
) {
522 disk
= r10_bio
->devs
[slot
].devnum
;
528 /* make sure the disk is operational */
530 disk
= r10_bio
->devs
[slot
].devnum
;
531 while ((rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
534 if (slot
== conf
->copies
) {
538 disk
= r10_bio
->devs
[slot
].devnum
;
542 current_distance
= abs(r10_bio
->devs
[slot
].addr
-
543 conf
->mirrors
[disk
].head_position
);
545 /* Find the disk whose head is closest */
547 for (nslot
= slot
; nslot
< conf
->copies
; nslot
++) {
548 int ndisk
= r10_bio
->devs
[nslot
].devnum
;
551 if ((rdev
=rcu_dereference(conf
->mirrors
[ndisk
].rdev
)) == NULL
||
555 if (!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
);
580 static void unplug_slaves(mddev_t
*mddev
)
582 conf_t
*conf
= mddev_to_conf(mddev
);
586 for (i
=0; i
<mddev
->raid_disks
; i
++) {
587 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
588 if (rdev
&& !rdev
->faulty
&& atomic_read(&rdev
->nr_pending
)) {
589 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
591 atomic_inc(&rdev
->nr_pending
);
594 if (r_queue
->unplug_fn
)
595 r_queue
->unplug_fn(r_queue
);
597 rdev_dec_pending(rdev
, mddev
);
604 static void raid10_unplug(request_queue_t
*q
)
606 unplug_slaves(q
->queuedata
);
609 static int raid10_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
610 sector_t
*error_sector
)
612 mddev_t
*mddev
= q
->queuedata
;
613 conf_t
*conf
= mddev_to_conf(mddev
);
617 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
618 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
619 if (rdev
&& !rdev
->faulty
) {
620 struct block_device
*bdev
= rdev
->bdev
;
621 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
623 if (!r_queue
->issue_flush_fn
)
626 atomic_inc(&rdev
->nr_pending
);
628 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
630 rdev_dec_pending(rdev
, mddev
);
640 * Throttle resync depth, so that we can both get proper overlapping of
641 * requests, but are still able to handle normal requests quickly.
643 #define RESYNC_DEPTH 32
645 static void device_barrier(conf_t
*conf
, sector_t sect
)
647 spin_lock_irq(&conf
->resync_lock
);
648 wait_event_lock_irq(conf
->wait_idle
, !waitqueue_active(&conf
->wait_resume
),
649 conf
->resync_lock
, unplug_slaves(conf
->mddev
));
651 if (!conf
->barrier
++) {
652 wait_event_lock_irq(conf
->wait_idle
, !conf
->nr_pending
,
653 conf
->resync_lock
, unplug_slaves(conf
->mddev
));
654 if (conf
->nr_pending
)
657 wait_event_lock_irq(conf
->wait_resume
, conf
->barrier
< RESYNC_DEPTH
,
658 conf
->resync_lock
, unplug_slaves(conf
->mddev
));
659 conf
->next_resync
= sect
;
660 spin_unlock_irq(&conf
->resync_lock
);
663 static int make_request(request_queue_t
*q
, struct bio
* bio
)
665 mddev_t
*mddev
= q
->queuedata
;
666 conf_t
*conf
= mddev_to_conf(mddev
);
667 mirror_info_t
*mirror
;
669 struct bio
*read_bio
;
671 int chunk_sects
= conf
->chunk_mask
+ 1;
672 const int rw
= bio_data_dir(bio
);
674 if (unlikely(bio_barrier(bio
))) {
675 bio_endio(bio
, bio
->bi_size
, -EOPNOTSUPP
);
679 /* If this request crosses a chunk boundary, we need to
680 * split it. This will only happen for 1 PAGE (or less) requests.
682 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
684 conf
->near_copies
< conf
->raid_disks
)) {
686 /* Sanity check -- queue functions should prevent this happening */
687 if (bio
->bi_vcnt
!= 1 ||
690 /* This is a one page bio that upper layers
691 * refuse to split for us, so we need to split it.
693 bp
= bio_split(bio
, bio_split_pool
,
694 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
695 if (make_request(q
, &bp
->bio1
))
696 generic_make_request(&bp
->bio1
);
697 if (make_request(q
, &bp
->bio2
))
698 generic_make_request(&bp
->bio2
);
700 bio_pair_release(bp
);
703 printk("raid10_make_request bug: can't convert block across chunks"
704 " or bigger than %dk %llu %d\n", chunk_sects
/2,
705 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
707 bio_io_error(bio
, bio
->bi_size
);
711 md_write_start(mddev
, bio
);
714 * Register the new request and wait if the reconstruction
715 * thread has put up a bar for new requests.
716 * Continue immediately if no resync is active currently.
718 spin_lock_irq(&conf
->resync_lock
);
719 wait_event_lock_irq(conf
->wait_resume
, !conf
->barrier
, conf
->resync_lock
, );
721 spin_unlock_irq(&conf
->resync_lock
);
723 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
724 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bio
));
726 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
728 r10_bio
->master_bio
= bio
;
729 r10_bio
->sectors
= bio
->bi_size
>> 9;
731 r10_bio
->mddev
= mddev
;
732 r10_bio
->sector
= bio
->bi_sector
;
736 * read balancing logic:
738 int disk
= read_balance(conf
, r10_bio
);
739 int slot
= r10_bio
->read_slot
;
741 raid_end_bio_io(r10_bio
);
744 mirror
= conf
->mirrors
+ disk
;
746 read_bio
= bio_clone(bio
, GFP_NOIO
);
748 r10_bio
->devs
[slot
].bio
= read_bio
;
750 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
751 mirror
->rdev
->data_offset
;
752 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
753 read_bio
->bi_end_io
= raid10_end_read_request
;
754 read_bio
->bi_rw
= READ
;
755 read_bio
->bi_private
= r10_bio
;
757 generic_make_request(read_bio
);
764 /* first select target devices under spinlock and
765 * inc refcount on their rdev. Record them by setting
768 raid10_find_phys(conf
, r10_bio
);
770 for (i
= 0; i
< conf
->copies
; i
++) {
771 int d
= r10_bio
->devs
[i
].devnum
;
772 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
775 atomic_inc(&rdev
->nr_pending
);
776 r10_bio
->devs
[i
].bio
= bio
;
778 r10_bio
->devs
[i
].bio
= NULL
;
782 atomic_set(&r10_bio
->remaining
, 1);
784 for (i
= 0; i
< conf
->copies
; i
++) {
786 int d
= r10_bio
->devs
[i
].devnum
;
787 if (!r10_bio
->devs
[i
].bio
)
790 mbio
= bio_clone(bio
, GFP_NOIO
);
791 r10_bio
->devs
[i
].bio
= mbio
;
793 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
794 conf
->mirrors
[d
].rdev
->data_offset
;
795 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
796 mbio
->bi_end_io
= raid10_end_write_request
;
798 mbio
->bi_private
= r10_bio
;
800 atomic_inc(&r10_bio
->remaining
);
801 generic_make_request(mbio
);
804 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
806 raid_end_bio_io(r10_bio
);
812 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
814 conf_t
*conf
= mddev_to_conf(mddev
);
817 if (conf
->near_copies
< conf
->raid_disks
)
818 seq_printf(seq
, " %dK chunks", mddev
->chunk_size
/1024);
819 if (conf
->near_copies
> 1)
820 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
821 if (conf
->far_copies
> 1)
822 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
824 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
825 conf
->working_disks
);
826 for (i
= 0; i
< conf
->raid_disks
; i
++)
827 seq_printf(seq
, "%s",
828 conf
->mirrors
[i
].rdev
&&
829 conf
->mirrors
[i
].rdev
->in_sync
? "U" : "_");
830 seq_printf(seq
, "]");
833 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
835 char b
[BDEVNAME_SIZE
];
836 conf_t
*conf
= mddev_to_conf(mddev
);
839 * If it is not operational, then we have already marked it as dead
840 * else if it is the last working disks, ignore the error, let the
841 * next level up know.
842 * else mark the drive as failed
845 && conf
->working_disks
== 1)
847 * Don't fail the drive, just return an IO error.
848 * The test should really be more sophisticated than
849 * "working_disks == 1", but it isn't critical, and
850 * can wait until we do more sophisticated "is the drive
851 * really dead" tests...
856 conf
->working_disks
--;
858 * if recovery is running, make sure it aborts.
860 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
865 printk(KERN_ALERT
"raid10: Disk failure on %s, disabling device. \n"
866 " Operation continuing on %d devices\n",
867 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
870 static void print_conf(conf_t
*conf
)
875 printk("RAID10 conf printout:\n");
880 printk(" --- wd:%d rd:%d\n", conf
->working_disks
,
883 for (i
= 0; i
< conf
->raid_disks
; i
++) {
884 char b
[BDEVNAME_SIZE
];
885 tmp
= conf
->mirrors
+ i
;
887 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
888 i
, !tmp
->rdev
->in_sync
, !tmp
->rdev
->faulty
,
889 bdevname(tmp
->rdev
->bdev
,b
));
893 static void close_sync(conf_t
*conf
)
895 spin_lock_irq(&conf
->resync_lock
);
896 wait_event_lock_irq(conf
->wait_resume
, !conf
->barrier
,
897 conf
->resync_lock
, unplug_slaves(conf
->mddev
));
898 spin_unlock_irq(&conf
->resync_lock
);
900 if (conf
->barrier
) BUG();
901 if (waitqueue_active(&conf
->wait_idle
)) BUG();
903 mempool_destroy(conf
->r10buf_pool
);
904 conf
->r10buf_pool
= NULL
;
907 /* check if there are enough drives for
908 * every block to appear on atleast one
910 static int enough(conf_t
*conf
)
915 int n
= conf
->copies
;
918 if (conf
->mirrors
[first
].rdev
)
920 first
= (first
+1) % conf
->raid_disks
;
924 } while (first
!= 0);
928 static int raid10_spare_active(mddev_t
*mddev
)
931 conf_t
*conf
= mddev
->private;
935 * Find all non-in_sync disks within the RAID10 configuration
936 * and mark them in_sync
938 for (i
= 0; i
< conf
->raid_disks
; i
++) {
939 tmp
= conf
->mirrors
+ i
;
941 && !tmp
->rdev
->faulty
942 && !tmp
->rdev
->in_sync
) {
943 conf
->working_disks
++;
945 tmp
->rdev
->in_sync
= 1;
954 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
956 conf_t
*conf
= mddev
->private;
961 if (mddev
->recovery_cp
< MaxSector
)
962 /* only hot-add to in-sync arrays, as recovery is
963 * very different from resync
969 for (mirror
=0; mirror
< mddev
->raid_disks
; mirror
++)
970 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
972 blk_queue_stack_limits(mddev
->queue
,
973 rdev
->bdev
->bd_disk
->queue
);
974 /* as we don't honour merge_bvec_fn, we must never risk
975 * violating it, so limit ->max_sector to one PAGE, as
976 * a one page request is never in violation.
978 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
979 mddev
->queue
->max_sectors
> (PAGE_SIZE
>>9))
980 mddev
->queue
->max_sectors
= (PAGE_SIZE
>>9);
982 p
->head_position
= 0;
983 rdev
->raid_disk
= mirror
;
985 rcu_assign_pointer(p
->rdev
, rdev
);
993 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
995 conf_t
*conf
= mddev
->private;
998 mirror_info_t
*p
= conf
->mirrors
+ number
;
1003 if (rdev
->in_sync
||
1004 atomic_read(&rdev
->nr_pending
)) {
1010 if (atomic_read(&rdev
->nr_pending
)) {
1011 /* lost the race, try later */
1023 static int end_sync_read(struct bio
*bio
, unsigned int bytes_done
, int error
)
1025 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1026 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
1027 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
1033 for (i
=0; i
<conf
->copies
; i
++)
1034 if (r10_bio
->devs
[i
].bio
== bio
)
1036 if (i
== conf
->copies
)
1038 update_head_pos(i
, r10_bio
);
1039 d
= r10_bio
->devs
[i
].devnum
;
1041 md_error(r10_bio
->mddev
,
1042 conf
->mirrors
[d
].rdev
);
1044 /* for reconstruct, we always reschedule after a read.
1045 * for resync, only after all reads
1047 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1048 atomic_dec_and_test(&r10_bio
->remaining
)) {
1049 /* we have read all the blocks,
1050 * do the comparison in process context in raid10d
1052 reschedule_retry(r10_bio
);
1054 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1058 static int end_sync_write(struct bio
*bio
, unsigned int bytes_done
, int error
)
1060 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1061 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
1062 mddev_t
*mddev
= r10_bio
->mddev
;
1063 conf_t
*conf
= mddev_to_conf(mddev
);
1069 for (i
= 0; i
< conf
->copies
; i
++)
1070 if (r10_bio
->devs
[i
].bio
== bio
)
1072 d
= r10_bio
->devs
[i
].devnum
;
1075 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1076 update_head_pos(i
, r10_bio
);
1078 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1079 if (r10_bio
->master_bio
== NULL
) {
1080 /* the primary of several recovery bios */
1081 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1085 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1090 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1095 * Note: sync and recover and handled very differently for raid10
1096 * This code is for resync.
1097 * For resync, we read through virtual addresses and read all blocks.
1098 * If there is any error, we schedule a write. The lowest numbered
1099 * drive is authoritative.
1100 * However requests come for physical address, so we need to map.
1101 * For every physical address there are raid_disks/copies virtual addresses,
1102 * which is always are least one, but is not necessarly an integer.
1103 * This means that a physical address can span multiple chunks, so we may
1104 * have to submit multiple io requests for a single sync request.
1107 * We check if all blocks are in-sync and only write to blocks that
1110 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1112 conf_t
*conf
= mddev_to_conf(mddev
);
1114 struct bio
*tbio
, *fbio
;
1116 atomic_set(&r10_bio
->remaining
, 1);
1118 /* find the first device with a block */
1119 for (i
=0; i
<conf
->copies
; i
++)
1120 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1123 if (i
== conf
->copies
)
1127 fbio
= r10_bio
->devs
[i
].bio
;
1129 /* now find blocks with errors */
1130 for (i
=first
+1 ; i
< conf
->copies
; i
++) {
1133 if (!test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1135 /* We know that the bi_io_vec layout is the same for
1136 * both 'first' and 'i', so we just compare them.
1137 * All vec entries are PAGE_SIZE;
1139 tbio
= r10_bio
->devs
[i
].bio
;
1140 vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1141 for (j
= 0; j
< vcnt
; j
++)
1142 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1143 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1148 /* Ok, we need to write this bio
1149 * First we need to fixup bv_offset, bv_len and
1150 * bi_vecs, as the read request might have corrupted these
1152 tbio
->bi_vcnt
= vcnt
;
1153 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1155 tbio
->bi_phys_segments
= 0;
1156 tbio
->bi_hw_segments
= 0;
1157 tbio
->bi_hw_front_size
= 0;
1158 tbio
->bi_hw_back_size
= 0;
1159 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1160 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1161 tbio
->bi_next
= NULL
;
1162 tbio
->bi_rw
= WRITE
;
1163 tbio
->bi_private
= r10_bio
;
1164 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1166 for (j
=0; j
< vcnt
; j
++) {
1167 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1168 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1170 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1171 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1174 tbio
->bi_end_io
= end_sync_write
;
1176 d
= r10_bio
->devs
[i
].devnum
;
1177 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1178 atomic_inc(&r10_bio
->remaining
);
1179 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1181 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1182 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1183 generic_make_request(tbio
);
1187 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1188 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1194 * Now for the recovery code.
1195 * Recovery happens across physical sectors.
1196 * We recover all non-is_sync drives by finding the virtual address of
1197 * each, and then choose a working drive that also has that virt address.
1198 * There is a separate r10_bio for each non-in_sync drive.
1199 * Only the first two slots are in use. The first for reading,
1200 * The second for writing.
1204 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1206 conf_t
*conf
= mddev_to_conf(mddev
);
1208 struct bio
*bio
, *wbio
;
1211 /* move the pages across to the second bio
1212 * and submit the write request
1214 bio
= r10_bio
->devs
[0].bio
;
1215 wbio
= r10_bio
->devs
[1].bio
;
1216 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1217 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1218 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1219 wbio
->bi_io_vec
[i
].bv_page
= p
;
1221 d
= r10_bio
->devs
[1].devnum
;
1223 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1224 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1225 generic_make_request(wbio
);
1230 * This is a kernel thread which:
1232 * 1. Retries failed read operations on working mirrors.
1233 * 2. Updates the raid superblock when problems encounter.
1234 * 3. Performs writes following reads for array syncronising.
1237 static void raid10d(mddev_t
*mddev
)
1241 unsigned long flags
;
1242 conf_t
*conf
= mddev_to_conf(mddev
);
1243 struct list_head
*head
= &conf
->retry_list
;
1247 md_check_recovery(mddev
);
1250 char b
[BDEVNAME_SIZE
];
1251 spin_lock_irqsave(&conf
->device_lock
, flags
);
1252 if (list_empty(head
))
1254 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1255 list_del(head
->prev
);
1256 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1258 mddev
= r10_bio
->mddev
;
1259 conf
= mddev_to_conf(mddev
);
1260 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
)) {
1261 sync_request_write(mddev
, r10_bio
);
1263 } else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
1264 recovery_request_write(mddev
, r10_bio
);
1268 bio
= r10_bio
->devs
[r10_bio
->read_slot
].bio
;
1269 r10_bio
->devs
[r10_bio
->read_slot
].bio
= NULL
;
1271 mirror
= read_balance(conf
, r10_bio
);
1273 printk(KERN_ALERT
"raid10: %s: unrecoverable I/O"
1274 " read error for block %llu\n",
1275 bdevname(bio
->bi_bdev
,b
),
1276 (unsigned long long)r10_bio
->sector
);
1277 raid_end_bio_io(r10_bio
);
1279 rdev
= conf
->mirrors
[mirror
].rdev
;
1280 if (printk_ratelimit())
1281 printk(KERN_ERR
"raid10: %s: redirecting sector %llu to"
1282 " another mirror\n",
1283 bdevname(rdev
->bdev
,b
),
1284 (unsigned long long)r10_bio
->sector
);
1285 bio
= bio_clone(r10_bio
->master_bio
, GFP_NOIO
);
1286 r10_bio
->devs
[r10_bio
->read_slot
].bio
= bio
;
1287 bio
->bi_sector
= r10_bio
->devs
[r10_bio
->read_slot
].addr
1288 + rdev
->data_offset
;
1289 bio
->bi_bdev
= rdev
->bdev
;
1291 bio
->bi_private
= r10_bio
;
1292 bio
->bi_end_io
= raid10_end_read_request
;
1294 generic_make_request(bio
);
1298 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1300 unplug_slaves(mddev
);
1304 static int init_resync(conf_t
*conf
)
1308 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1309 if (conf
->r10buf_pool
)
1311 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1312 if (!conf
->r10buf_pool
)
1314 conf
->next_resync
= 0;
1319 * perform a "sync" on one "block"
1321 * We need to make sure that no normal I/O request - particularly write
1322 * requests - conflict with active sync requests.
1324 * This is achieved by tracking pending requests and a 'barrier' concept
1325 * that can be installed to exclude normal IO requests.
1327 * Resync and recovery are handled very differently.
1328 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1330 * For resync, we iterate over virtual addresses, read all copies,
1331 * and update if there are differences. If only one copy is live,
1333 * For recovery, we iterate over physical addresses, read a good
1334 * value for each non-in_sync drive, and over-write.
1336 * So, for recovery we may have several outstanding complex requests for a
1337 * given address, one for each out-of-sync device. We model this by allocating
1338 * a number of r10_bio structures, one for each out-of-sync device.
1339 * As we setup these structures, we collect all bio's together into a list
1340 * which we then process collectively to add pages, and then process again
1341 * to pass to generic_make_request.
1343 * The r10_bio structures are linked using a borrowed master_bio pointer.
1344 * This link is counted in ->remaining. When the r10_bio that points to NULL
1345 * has its remaining count decremented to 0, the whole complex operation
1350 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1352 conf_t
*conf
= mddev_to_conf(mddev
);
1354 struct bio
*biolist
= NULL
, *bio
;
1355 sector_t max_sector
, nr_sectors
;
1359 sector_t sectors_skipped
= 0;
1360 int chunks_skipped
= 0;
1362 if (!conf
->r10buf_pool
)
1363 if (init_resync(conf
))
1367 max_sector
= mddev
->size
<< 1;
1368 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1369 max_sector
= mddev
->resync_max_sectors
;
1370 if (sector_nr
>= max_sector
) {
1373 return sectors_skipped
;
1375 if (chunks_skipped
>= conf
->raid_disks
) {
1376 /* if there has been nothing to do on any drive,
1377 * then there is nothing to do at all..
1380 return (max_sector
- sector_nr
) + sectors_skipped
;
1383 /* make sure whole request will fit in a chunk - if chunks
1386 if (conf
->near_copies
< conf
->raid_disks
&&
1387 max_sector
> (sector_nr
| conf
->chunk_mask
))
1388 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1390 * If there is non-resync activity waiting for us then
1391 * put in a delay to throttle resync.
1393 if (!go_faster
&& waitqueue_active(&conf
->wait_resume
))
1394 msleep_interruptible(1000);
1395 device_barrier(conf
, sector_nr
+ RESYNC_SECTORS
);
1397 /* Again, very different code for resync and recovery.
1398 * Both must result in an r10bio with a list of bios that
1399 * have bi_end_io, bi_sector, bi_bdev set,
1400 * and bi_private set to the r10bio.
1401 * For recovery, we may actually create several r10bios
1402 * with 2 bios in each, that correspond to the bios in the main one.
1403 * In this case, the subordinate r10bios link back through a
1404 * borrowed master_bio pointer, and the counter in the master
1405 * includes a ref from each subordinate.
1407 /* First, we decide what to do and set ->bi_end_io
1408 * To end_sync_read if we want to read, and
1409 * end_sync_write if we will want to write.
1412 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1413 /* recovery... the complicated one */
1417 for (i
=0 ; i
<conf
->raid_disks
; i
++)
1418 if (conf
->mirrors
[i
].rdev
&&
1419 !conf
->mirrors
[i
].rdev
->in_sync
) {
1420 /* want to reconstruct this device */
1421 r10bio_t
*rb2
= r10_bio
;
1423 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1424 spin_lock_irq(&conf
->resync_lock
);
1426 if (rb2
) conf
->barrier
++;
1427 spin_unlock_irq(&conf
->resync_lock
);
1428 atomic_set(&r10_bio
->remaining
, 0);
1430 r10_bio
->master_bio
= (struct bio
*)rb2
;
1432 atomic_inc(&rb2
->remaining
);
1433 r10_bio
->mddev
= mddev
;
1434 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1435 r10_bio
->sector
= raid10_find_virt(conf
, sector_nr
, i
);
1436 raid10_find_phys(conf
, r10_bio
);
1437 for (j
=0; j
<conf
->copies
;j
++) {
1438 int d
= r10_bio
->devs
[j
].devnum
;
1439 if (conf
->mirrors
[d
].rdev
&&
1440 conf
->mirrors
[d
].rdev
->in_sync
) {
1441 /* This is where we read from */
1442 bio
= r10_bio
->devs
[0].bio
;
1443 bio
->bi_next
= biolist
;
1445 bio
->bi_private
= r10_bio
;
1446 bio
->bi_end_io
= end_sync_read
;
1448 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1449 conf
->mirrors
[d
].rdev
->data_offset
;
1450 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1451 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1452 atomic_inc(&r10_bio
->remaining
);
1453 /* and we write to 'i' */
1455 for (k
=0; k
<conf
->copies
; k
++)
1456 if (r10_bio
->devs
[k
].devnum
== i
)
1458 bio
= r10_bio
->devs
[1].bio
;
1459 bio
->bi_next
= biolist
;
1461 bio
->bi_private
= r10_bio
;
1462 bio
->bi_end_io
= end_sync_write
;
1464 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1465 conf
->mirrors
[i
].rdev
->data_offset
;
1466 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1468 r10_bio
->devs
[0].devnum
= d
;
1469 r10_bio
->devs
[1].devnum
= i
;
1474 if (j
== conf
->copies
) {
1475 /* Cannot recover, so abort the recovery */
1478 if (!test_and_set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
))
1479 printk(KERN_INFO
"raid10: %s: insufficient working devices for recovery.\n",
1484 if (biolist
== NULL
) {
1486 r10bio_t
*rb2
= r10_bio
;
1487 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
1488 rb2
->master_bio
= NULL
;
1494 /* resync. Schedule a read for every block at this virt offset */
1496 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1498 spin_lock_irq(&conf
->resync_lock
);
1500 spin_unlock_irq(&conf
->resync_lock
);
1502 r10_bio
->mddev
= mddev
;
1503 atomic_set(&r10_bio
->remaining
, 0);
1505 r10_bio
->master_bio
= NULL
;
1506 r10_bio
->sector
= sector_nr
;
1507 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
1508 raid10_find_phys(conf
, r10_bio
);
1509 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
1511 for (i
=0; i
<conf
->copies
; i
++) {
1512 int d
= r10_bio
->devs
[i
].devnum
;
1513 bio
= r10_bio
->devs
[i
].bio
;
1514 bio
->bi_end_io
= NULL
;
1515 if (conf
->mirrors
[d
].rdev
== NULL
||
1516 conf
->mirrors
[d
].rdev
->faulty
)
1518 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1519 atomic_inc(&r10_bio
->remaining
);
1520 bio
->bi_next
= biolist
;
1522 bio
->bi_private
= r10_bio
;
1523 bio
->bi_end_io
= end_sync_read
;
1525 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
1526 conf
->mirrors
[d
].rdev
->data_offset
;
1527 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1532 for (i
=0; i
<conf
->copies
; i
++) {
1533 int d
= r10_bio
->devs
[i
].devnum
;
1534 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
1535 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1543 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
1545 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1547 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
1550 bio
->bi_phys_segments
= 0;
1551 bio
->bi_hw_segments
= 0;
1558 int len
= PAGE_SIZE
;
1560 if (sector_nr
+ (len
>>9) > max_sector
)
1561 len
= (max_sector
- sector_nr
) << 9;
1564 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
1565 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
1566 if (bio_add_page(bio
, page
, len
, 0) == 0) {
1569 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
1570 for (bio2
= biolist
; bio2
&& bio2
!= bio
; bio2
= bio2
->bi_next
) {
1571 /* remove last page from this bio */
1573 bio2
->bi_size
-= len
;
1574 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
1580 nr_sectors
+= len
>>9;
1581 sector_nr
+= len
>>9;
1582 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
1584 r10_bio
->sectors
= nr_sectors
;
1588 biolist
= biolist
->bi_next
;
1590 bio
->bi_next
= NULL
;
1591 r10_bio
= bio
->bi_private
;
1592 r10_bio
->sectors
= nr_sectors
;
1594 if (bio
->bi_end_io
== end_sync_read
) {
1595 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
1596 generic_make_request(bio
);
1600 if (sectors_skipped
)
1601 /* pretend they weren't skipped, it makes
1602 * no important difference in this case
1604 md_done_sync(mddev
, sectors_skipped
, 1);
1606 return sectors_skipped
+ nr_sectors
;
1608 /* There is nowhere to write, so all non-sync
1609 * drives must be failed, so try the next chunk...
1612 sector_t sec
= max_sector
- sector_nr
;
1613 sectors_skipped
+= sec
;
1615 sector_nr
= max_sector
;
1620 static int run(mddev_t
*mddev
)
1624 mirror_info_t
*disk
;
1626 struct list_head
*tmp
;
1628 sector_t stride
, size
;
1630 if (mddev
->level
!= 10) {
1631 printk(KERN_ERR
"raid10: %s: raid level not set correctly... (%d)\n",
1632 mdname(mddev
), mddev
->level
);
1635 nc
= mddev
->layout
& 255;
1636 fc
= (mddev
->layout
>> 8) & 255;
1637 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
1638 (mddev
->layout
>> 16)) {
1639 printk(KERN_ERR
"raid10: %s: unsupported raid10 layout: 0x%8x\n",
1640 mdname(mddev
), mddev
->layout
);
1644 * copy the already verified devices into our private RAID10
1645 * bookkeeping area. [whatever we allocate in run(),
1646 * should be freed in stop()]
1648 conf
= kmalloc(sizeof(conf_t
), GFP_KERNEL
);
1649 mddev
->private = conf
;
1651 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
1655 memset(conf
, 0, sizeof(*conf
));
1656 conf
->mirrors
= kmalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
1658 if (!conf
->mirrors
) {
1659 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
1663 memset(conf
->mirrors
, 0, sizeof(struct mirror_info
)*mddev
->raid_disks
);
1665 conf
->near_copies
= nc
;
1666 conf
->far_copies
= fc
;
1667 conf
->copies
= nc
*fc
;
1668 conf
->chunk_mask
= (sector_t
)(mddev
->chunk_size
>>9)-1;
1669 conf
->chunk_shift
= ffz(~mddev
->chunk_size
) - 9;
1670 stride
= mddev
->size
>> (conf
->chunk_shift
-1);
1671 sector_div(stride
, fc
);
1672 conf
->stride
= stride
<< conf
->chunk_shift
;
1674 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
1675 r10bio_pool_free
, conf
);
1676 if (!conf
->r10bio_pool
) {
1677 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
1682 ITERATE_RDEV(mddev
, rdev
, tmp
) {
1683 disk_idx
= rdev
->raid_disk
;
1684 if (disk_idx
>= mddev
->raid_disks
1687 disk
= conf
->mirrors
+ disk_idx
;
1691 blk_queue_stack_limits(mddev
->queue
,
1692 rdev
->bdev
->bd_disk
->queue
);
1693 /* as we don't honour merge_bvec_fn, we must never risk
1694 * violating it, so limit ->max_sector to one PAGE, as
1695 * a one page request is never in violation.
1697 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
1698 mddev
->queue
->max_sectors
> (PAGE_SIZE
>>9))
1699 mddev
->queue
->max_sectors
= (PAGE_SIZE
>>9);
1701 disk
->head_position
= 0;
1702 if (!rdev
->faulty
&& rdev
->in_sync
)
1703 conf
->working_disks
++;
1705 conf
->raid_disks
= mddev
->raid_disks
;
1706 conf
->mddev
= mddev
;
1707 spin_lock_init(&conf
->device_lock
);
1708 INIT_LIST_HEAD(&conf
->retry_list
);
1710 spin_lock_init(&conf
->resync_lock
);
1711 init_waitqueue_head(&conf
->wait_idle
);
1712 init_waitqueue_head(&conf
->wait_resume
);
1714 /* need to check that every block has at least one working mirror */
1715 if (!enough(conf
)) {
1716 printk(KERN_ERR
"raid10: not enough operational mirrors for %s\n",
1721 mddev
->degraded
= 0;
1722 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1724 disk
= conf
->mirrors
+ i
;
1727 disk
->head_position
= 0;
1733 mddev
->thread
= md_register_thread(raid10d
, mddev
, "%s_raid10");
1734 if (!mddev
->thread
) {
1736 "raid10: couldn't allocate thread for %s\n",
1742 "raid10: raid set %s active with %d out of %d devices\n",
1743 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
1746 * Ok, everything is just fine now
1748 size
= conf
->stride
* conf
->raid_disks
;
1749 sector_div(size
, conf
->near_copies
);
1750 mddev
->array_size
= size
/2;
1751 mddev
->resync_max_sectors
= size
;
1753 mddev
->queue
->unplug_fn
= raid10_unplug
;
1754 mddev
->queue
->issue_flush_fn
= raid10_issue_flush
;
1756 /* Calculate max read-ahead size.
1757 * We need to readahead at least twice a whole stripe....
1761 int stripe
= conf
->raid_disks
* mddev
->chunk_size
/ PAGE_CACHE_SIZE
;
1762 stripe
/= conf
->near_copies
;
1763 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
1764 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
1767 if (conf
->near_copies
< mddev
->raid_disks
)
1768 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
1772 if (conf
->r10bio_pool
)
1773 mempool_destroy(conf
->r10bio_pool
);
1774 kfree(conf
->mirrors
);
1776 mddev
->private = NULL
;
1781 static int stop(mddev_t
*mddev
)
1783 conf_t
*conf
= mddev_to_conf(mddev
);
1785 md_unregister_thread(mddev
->thread
);
1786 mddev
->thread
= NULL
;
1787 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
1788 if (conf
->r10bio_pool
)
1789 mempool_destroy(conf
->r10bio_pool
);
1790 kfree(conf
->mirrors
);
1792 mddev
->private = NULL
;
1797 static mdk_personality_t raid10_personality
=
1800 .owner
= THIS_MODULE
,
1801 .make_request
= make_request
,
1805 .error_handler
= error
,
1806 .hot_add_disk
= raid10_add_disk
,
1807 .hot_remove_disk
= raid10_remove_disk
,
1808 .spare_active
= raid10_spare_active
,
1809 .sync_request
= sync_request
,
1812 static int __init
raid_init(void)
1814 return register_md_personality(RAID10
, &raid10_personality
);
1817 static void raid_exit(void)
1819 unregister_md_personality(RAID10
);
1822 module_init(raid_init
);
1823 module_exit(raid_exit
);
1824 MODULE_LICENSE("GPL");
1825 MODULE_ALIAS("md-personality-9"); /* RAID10 */