2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/seq_file.h>
25 #include <linux/ratelimit.h>
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
38 * far_offset (stored in bit 16 of layout )
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
46 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
48 * near_copies and far_copies must be at least one, and their product is at most
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
57 * Number of guaranteed r10bios in case of extreme VM load:
59 #define NR_RAID10_BIOS 256
61 static void allow_barrier(conf_t
*conf
);
62 static void lower_barrier(conf_t
*conf
);
64 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
67 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
70 return kzalloc(size
, gfp_flags
);
73 static void r10bio_pool_free(void *r10_bio
, void *data
)
78 /* Maximum size of each resync request */
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
81 /* amount of memory to reserve for resync requests */
82 #define RESYNC_WINDOW (1024*1024)
83 /* maximum number of concurrent requests, memory permitting */
84 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
93 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
102 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
106 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
107 nalloc
= conf
->copies
; /* resync */
109 nalloc
= 2; /* recovery */
114 for (j
= nalloc
; j
-- ; ) {
115 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
118 r10_bio
->devs
[j
].bio
= bio
;
121 * Allocate RESYNC_PAGES data pages and attach them
124 for (j
= 0 ; j
< nalloc
; j
++) {
125 bio
= r10_bio
->devs
[j
].bio
;
126 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
127 if (j
== 1 && !test_bit(MD_RECOVERY_SYNC
,
128 &conf
->mddev
->recovery
)) {
129 /* we can share bv_page's during recovery */
130 struct bio
*rbio
= r10_bio
->devs
[0].bio
;
131 page
= rbio
->bi_io_vec
[i
].bv_page
;
134 page
= alloc_page(gfp_flags
);
138 bio
->bi_io_vec
[i
].bv_page
= page
;
146 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
148 for (i
= 0; i
< RESYNC_PAGES
; i
++)
149 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
152 while ( ++j
< nalloc
)
153 bio_put(r10_bio
->devs
[j
].bio
);
154 r10bio_pool_free(r10_bio
, conf
);
158 static void r10buf_pool_free(void *__r10_bio
, void *data
)
162 r10bio_t
*r10bio
= __r10_bio
;
165 for (j
=0; j
< conf
->copies
; j
++) {
166 struct bio
*bio
= r10bio
->devs
[j
].bio
;
168 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
169 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
170 bio
->bi_io_vec
[i
].bv_page
= NULL
;
175 r10bio_pool_free(r10bio
, conf
);
178 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
182 for (i
= 0; i
< conf
->copies
; i
++) {
183 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
184 if (!BIO_SPECIAL(*bio
))
190 static void free_r10bio(r10bio_t
*r10_bio
)
192 conf_t
*conf
= r10_bio
->mddev
->private;
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
= r10_bio
->mddev
->private;
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
->private;
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 /* wake up frozen array... */
219 wake_up(&conf
->wait_barrier
);
221 md_wakeup_thread(mddev
->thread
);
225 * raid_end_bio_io() is called when we have finished servicing a mirrored
226 * operation and are ready to return a success/failure code to the buffer
229 static void raid_end_bio_io(r10bio_t
*r10_bio
)
231 struct bio
*bio
= r10_bio
->master_bio
;
233 conf_t
*conf
= r10_bio
->mddev
->private;
235 if (bio
->bi_phys_segments
) {
237 spin_lock_irqsave(&conf
->device_lock
, flags
);
238 bio
->bi_phys_segments
--;
239 done
= (bio
->bi_phys_segments
== 0);
240 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
243 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
244 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
248 * Wake up any possible resync thread that waits for the device
253 free_r10bio(r10_bio
);
257 * Update disk head position estimator based on IRQ completion info.
259 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
261 conf_t
*conf
= r10_bio
->mddev
->private;
263 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
264 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
268 * Find the disk number which triggered given bio
270 static int find_bio_disk(conf_t
*conf
, r10bio_t
*r10_bio
,
271 struct bio
*bio
, int *slotp
)
275 for (slot
= 0; slot
< conf
->copies
; slot
++)
276 if (r10_bio
->devs
[slot
].bio
== bio
)
279 BUG_ON(slot
== conf
->copies
);
280 update_head_pos(slot
, r10_bio
);
284 return r10_bio
->devs
[slot
].devnum
;
287 static void raid10_end_read_request(struct bio
*bio
, int error
)
289 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
290 r10bio_t
*r10_bio
= bio
->bi_private
;
292 conf_t
*conf
= r10_bio
->mddev
->private;
295 slot
= r10_bio
->read_slot
;
296 dev
= r10_bio
->devs
[slot
].devnum
;
298 * this branch is our 'one mirror IO has finished' event handler:
300 update_head_pos(slot
, r10_bio
);
304 * Set R10BIO_Uptodate in our master bio, so that
305 * we will return a good error code to the higher
306 * levels even if IO on some other mirrored buffer fails.
308 * The 'master' represents the composite IO operation to
309 * user-side. So if something waits for IO, then it will
310 * wait for the 'master' bio.
312 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
313 raid_end_bio_io(r10_bio
);
314 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
317 * oops, read error - keep the refcount on the rdev
319 char b
[BDEVNAME_SIZE
];
320 printk_ratelimited(KERN_ERR
321 "md/raid10:%s: %s: rescheduling sector %llu\n",
323 bdevname(conf
->mirrors
[dev
].rdev
->bdev
, b
),
324 (unsigned long long)r10_bio
->sector
);
325 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
326 reschedule_retry(r10_bio
);
330 static void raid10_end_write_request(struct bio
*bio
, int error
)
332 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
333 r10bio_t
*r10_bio
= bio
->bi_private
;
336 conf_t
*conf
= r10_bio
->mddev
->private;
339 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
);
342 * this branch is our 'one mirror IO has finished' event handler:
345 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
346 /* an I/O failed, we can't clear the bitmap */
347 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
350 * Set R10BIO_Uptodate in our master bio, so that
351 * we will return a good error code for to the higher
352 * levels even if IO on some other mirrored buffer fails.
354 * The 'master' represents the composite IO operation to
355 * user-side. So if something waits for IO, then it will
356 * wait for the 'master' bio.
361 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
363 /* Maybe we can clear some bad blocks. */
364 if (is_badblock(conf
->mirrors
[dev
].rdev
,
365 r10_bio
->devs
[slot
].addr
,
367 &first_bad
, &bad_sectors
)) {
369 r10_bio
->devs
[slot
].bio
= IO_MADE_GOOD
;
371 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
377 * Let's see if all mirrored write operations have finished
380 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
381 /* clear the bitmap if all writes complete successfully */
382 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
384 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
386 md_write_end(r10_bio
->mddev
);
387 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
388 reschedule_retry(r10_bio
);
390 raid_end_bio_io(r10_bio
);
393 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
398 * RAID10 layout manager
399 * As well as the chunksize and raid_disks count, there are two
400 * parameters: near_copies and far_copies.
401 * near_copies * far_copies must be <= raid_disks.
402 * Normally one of these will be 1.
403 * If both are 1, we get raid0.
404 * If near_copies == raid_disks, we get raid1.
406 * Chunks are laid out in raid0 style with near_copies copies of the
407 * first chunk, followed by near_copies copies of the next chunk and
409 * If far_copies > 1, then after 1/far_copies of the array has been assigned
410 * as described above, we start again with a device offset of near_copies.
411 * So we effectively have another copy of the whole array further down all
412 * the drives, but with blocks on different drives.
413 * With this layout, and block is never stored twice on the one device.
415 * raid10_find_phys finds the sector offset of a given virtual sector
416 * on each device that it is on.
418 * raid10_find_virt does the reverse mapping, from a device and a
419 * sector offset to a virtual address
422 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
432 /* now calculate first sector/dev */
433 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
434 sector
= r10bio
->sector
& conf
->chunk_mask
;
436 chunk
*= conf
->near_copies
;
438 dev
= sector_div(stripe
, conf
->raid_disks
);
439 if (conf
->far_offset
)
440 stripe
*= conf
->far_copies
;
442 sector
+= stripe
<< conf
->chunk_shift
;
444 /* and calculate all the others */
445 for (n
=0; n
< conf
->near_copies
; n
++) {
448 r10bio
->devs
[slot
].addr
= sector
;
449 r10bio
->devs
[slot
].devnum
= d
;
452 for (f
= 1; f
< conf
->far_copies
; f
++) {
453 d
+= conf
->near_copies
;
454 if (d
>= conf
->raid_disks
)
455 d
-= conf
->raid_disks
;
457 r10bio
->devs
[slot
].devnum
= d
;
458 r10bio
->devs
[slot
].addr
= s
;
462 if (dev
>= conf
->raid_disks
) {
464 sector
+= (conf
->chunk_mask
+ 1);
467 BUG_ON(slot
!= conf
->copies
);
470 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
472 sector_t offset
, chunk
, vchunk
;
474 offset
= sector
& conf
->chunk_mask
;
475 if (conf
->far_offset
) {
477 chunk
= sector
>> conf
->chunk_shift
;
478 fc
= sector_div(chunk
, conf
->far_copies
);
479 dev
-= fc
* conf
->near_copies
;
481 dev
+= conf
->raid_disks
;
483 while (sector
>= conf
->stride
) {
484 sector
-= conf
->stride
;
485 if (dev
< conf
->near_copies
)
486 dev
+= conf
->raid_disks
- conf
->near_copies
;
488 dev
-= conf
->near_copies
;
490 chunk
= sector
>> conf
->chunk_shift
;
492 vchunk
= chunk
* conf
->raid_disks
+ dev
;
493 sector_div(vchunk
, conf
->near_copies
);
494 return (vchunk
<< conf
->chunk_shift
) + offset
;
498 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
500 * @bvm: properties of new bio
501 * @biovec: the request that could be merged to it.
503 * Return amount of bytes we can accept at this offset
504 * If near_copies == raid_disk, there are no striping issues,
505 * but in that case, the function isn't called at all.
507 static int raid10_mergeable_bvec(struct request_queue
*q
,
508 struct bvec_merge_data
*bvm
,
509 struct bio_vec
*biovec
)
511 mddev_t
*mddev
= q
->queuedata
;
512 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
514 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
515 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
517 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
518 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
519 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
520 return biovec
->bv_len
;
526 * This routine returns the disk from which the requested read should
527 * be done. There is a per-array 'next expected sequential IO' sector
528 * number - if this matches on the next IO then we use the last disk.
529 * There is also a per-disk 'last know head position' sector that is
530 * maintained from IRQ contexts, both the normal and the resync IO
531 * completion handlers update this position correctly. If there is no
532 * perfect sequential match then we pick the disk whose head is closest.
534 * If there are 2 mirrors in the same 2 devices, performance degrades
535 * because position is mirror, not device based.
537 * The rdev for the device selected will have nr_pending incremented.
541 * FIXME: possibly should rethink readbalancing and do it differently
542 * depending on near_copies / far_copies geometry.
544 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
, int *max_sectors
)
546 const sector_t this_sector
= r10_bio
->sector
;
548 int sectors
= r10_bio
->sectors
;
549 int best_good_sectors
;
550 sector_t new_distance
, best_dist
;
555 raid10_find_phys(conf
, r10_bio
);
558 sectors
= r10_bio
->sectors
;
560 best_dist
= MaxSector
;
561 best_good_sectors
= 0;
564 * Check if we can balance. We can balance on the whole
565 * device if no resync is going on (recovery is ok), or below
566 * the resync window. We take the first readable disk when
567 * above the resync window.
569 if (conf
->mddev
->recovery_cp
< MaxSector
570 && (this_sector
+ sectors
>= conf
->next_resync
))
573 for (slot
= 0; slot
< conf
->copies
; slot
++) {
578 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
580 disk
= r10_bio
->devs
[slot
].devnum
;
581 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
584 if (!test_bit(In_sync
, &rdev
->flags
))
587 dev_sector
= r10_bio
->devs
[slot
].addr
;
588 if (is_badblock(rdev
, dev_sector
, sectors
,
589 &first_bad
, &bad_sectors
)) {
590 if (best_dist
< MaxSector
)
591 /* Already have a better slot */
593 if (first_bad
<= dev_sector
) {
594 /* Cannot read here. If this is the
595 * 'primary' device, then we must not read
596 * beyond 'bad_sectors' from another device.
598 bad_sectors
-= (dev_sector
- first_bad
);
599 if (!do_balance
&& sectors
> bad_sectors
)
600 sectors
= bad_sectors
;
601 if (best_good_sectors
> sectors
)
602 best_good_sectors
= sectors
;
604 sector_t good_sectors
=
605 first_bad
- dev_sector
;
606 if (good_sectors
> best_good_sectors
) {
607 best_good_sectors
= good_sectors
;
611 /* Must read from here */
616 best_good_sectors
= sectors
;
621 /* This optimisation is debatable, and completely destroys
622 * sequential read speed for 'far copies' arrays. So only
623 * keep it for 'near' arrays, and review those later.
625 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
628 /* for far > 1 always use the lowest address */
629 if (conf
->far_copies
> 1)
630 new_distance
= r10_bio
->devs
[slot
].addr
;
632 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
633 conf
->mirrors
[disk
].head_position
);
634 if (new_distance
< best_dist
) {
635 best_dist
= new_distance
;
639 if (slot
== conf
->copies
)
643 disk
= r10_bio
->devs
[slot
].devnum
;
644 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
647 atomic_inc(&rdev
->nr_pending
);
648 if (test_bit(Faulty
, &rdev
->flags
)) {
649 /* Cannot risk returning a device that failed
650 * before we inc'ed nr_pending
652 rdev_dec_pending(rdev
, conf
->mddev
);
655 r10_bio
->read_slot
= slot
;
659 *max_sectors
= best_good_sectors
;
664 static int raid10_congested(void *data
, int bits
)
666 mddev_t
*mddev
= data
;
667 conf_t
*conf
= mddev
->private;
670 if (mddev_congested(mddev
, bits
))
673 for (i
= 0; i
< conf
->raid_disks
&& ret
== 0; i
++) {
674 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
675 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
676 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
678 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
685 static void flush_pending_writes(conf_t
*conf
)
687 /* Any writes that have been queued but are awaiting
688 * bitmap updates get flushed here.
690 spin_lock_irq(&conf
->device_lock
);
692 if (conf
->pending_bio_list
.head
) {
694 bio
= bio_list_get(&conf
->pending_bio_list
);
695 spin_unlock_irq(&conf
->device_lock
);
696 /* flush any pending bitmap writes to disk
697 * before proceeding w/ I/O */
698 bitmap_unplug(conf
->mddev
->bitmap
);
700 while (bio
) { /* submit pending writes */
701 struct bio
*next
= bio
->bi_next
;
703 generic_make_request(bio
);
707 spin_unlock_irq(&conf
->device_lock
);
711 * Sometimes we need to suspend IO while we do something else,
712 * either some resync/recovery, or reconfigure the array.
713 * To do this we raise a 'barrier'.
714 * The 'barrier' is a counter that can be raised multiple times
715 * to count how many activities are happening which preclude
717 * We can only raise the barrier if there is no pending IO.
718 * i.e. if nr_pending == 0.
719 * We choose only to raise the barrier if no-one is waiting for the
720 * barrier to go down. This means that as soon as an IO request
721 * is ready, no other operations which require a barrier will start
722 * until the IO request has had a chance.
724 * So: regular IO calls 'wait_barrier'. When that returns there
725 * is no backgroup IO happening, It must arrange to call
726 * allow_barrier when it has finished its IO.
727 * backgroup IO calls must call raise_barrier. Once that returns
728 * there is no normal IO happeing. It must arrange to call
729 * lower_barrier when the particular background IO completes.
732 static void raise_barrier(conf_t
*conf
, int force
)
734 BUG_ON(force
&& !conf
->barrier
);
735 spin_lock_irq(&conf
->resync_lock
);
737 /* Wait until no block IO is waiting (unless 'force') */
738 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
739 conf
->resync_lock
, );
741 /* block any new IO from starting */
744 /* Now wait for all pending IO to complete */
745 wait_event_lock_irq(conf
->wait_barrier
,
746 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
747 conf
->resync_lock
, );
749 spin_unlock_irq(&conf
->resync_lock
);
752 static void lower_barrier(conf_t
*conf
)
755 spin_lock_irqsave(&conf
->resync_lock
, flags
);
757 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
758 wake_up(&conf
->wait_barrier
);
761 static void wait_barrier(conf_t
*conf
)
763 spin_lock_irq(&conf
->resync_lock
);
766 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
772 spin_unlock_irq(&conf
->resync_lock
);
775 static void allow_barrier(conf_t
*conf
)
778 spin_lock_irqsave(&conf
->resync_lock
, flags
);
780 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
781 wake_up(&conf
->wait_barrier
);
784 static void freeze_array(conf_t
*conf
)
786 /* stop syncio and normal IO and wait for everything to
788 * We increment barrier and nr_waiting, and then
789 * wait until nr_pending match nr_queued+1
790 * This is called in the context of one normal IO request
791 * that has failed. Thus any sync request that might be pending
792 * will be blocked by nr_pending, and we need to wait for
793 * pending IO requests to complete or be queued for re-try.
794 * Thus the number queued (nr_queued) plus this request (1)
795 * must match the number of pending IOs (nr_pending) before
798 spin_lock_irq(&conf
->resync_lock
);
801 wait_event_lock_irq(conf
->wait_barrier
,
802 conf
->nr_pending
== conf
->nr_queued
+1,
804 flush_pending_writes(conf
));
806 spin_unlock_irq(&conf
->resync_lock
);
809 static void unfreeze_array(conf_t
*conf
)
811 /* reverse the effect of the freeze */
812 spin_lock_irq(&conf
->resync_lock
);
815 wake_up(&conf
->wait_barrier
);
816 spin_unlock_irq(&conf
->resync_lock
);
819 static int make_request(mddev_t
*mddev
, struct bio
* bio
)
821 conf_t
*conf
= mddev
->private;
822 mirror_info_t
*mirror
;
824 struct bio
*read_bio
;
826 int chunk_sects
= conf
->chunk_mask
+ 1;
827 const int rw
= bio_data_dir(bio
);
828 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
829 const unsigned long do_fua
= (bio
->bi_rw
& REQ_FUA
);
831 mdk_rdev_t
*blocked_rdev
;
836 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
837 md_flush_request(mddev
, bio
);
841 /* If this request crosses a chunk boundary, we need to
842 * split it. This will only happen for 1 PAGE (or less) requests.
844 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
846 conf
->near_copies
< conf
->raid_disks
)) {
848 /* Sanity check -- queue functions should prevent this happening */
849 if (bio
->bi_vcnt
!= 1 ||
852 /* This is a one page bio that upper layers
853 * refuse to split for us, so we need to split it.
856 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
858 /* Each of these 'make_request' calls will call 'wait_barrier'.
859 * If the first succeeds but the second blocks due to the resync
860 * thread raising the barrier, we will deadlock because the
861 * IO to the underlying device will be queued in generic_make_request
862 * and will never complete, so will never reduce nr_pending.
863 * So increment nr_waiting here so no new raise_barriers will
864 * succeed, and so the second wait_barrier cannot block.
866 spin_lock_irq(&conf
->resync_lock
);
868 spin_unlock_irq(&conf
->resync_lock
);
870 if (make_request(mddev
, &bp
->bio1
))
871 generic_make_request(&bp
->bio1
);
872 if (make_request(mddev
, &bp
->bio2
))
873 generic_make_request(&bp
->bio2
);
875 spin_lock_irq(&conf
->resync_lock
);
877 wake_up(&conf
->wait_barrier
);
878 spin_unlock_irq(&conf
->resync_lock
);
880 bio_pair_release(bp
);
883 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
884 " or bigger than %dk %llu %d\n", mdname(mddev
), chunk_sects
/2,
885 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
891 md_write_start(mddev
, bio
);
894 * Register the new request and wait if the reconstruction
895 * thread has put up a bar for new requests.
896 * Continue immediately if no resync is active currently.
900 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
902 r10_bio
->master_bio
= bio
;
903 r10_bio
->sectors
= bio
->bi_size
>> 9;
905 r10_bio
->mddev
= mddev
;
906 r10_bio
->sector
= bio
->bi_sector
;
909 /* We might need to issue multiple reads to different
910 * devices if there are bad blocks around, so we keep
911 * track of the number of reads in bio->bi_phys_segments.
912 * If this is 0, there is only one r10_bio and no locking
913 * will be needed when the request completes. If it is
914 * non-zero, then it is the number of not-completed requests.
916 bio
->bi_phys_segments
= 0;
917 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
921 * read balancing logic:
927 disk
= read_balance(conf
, r10_bio
, &max_sectors
);
928 slot
= r10_bio
->read_slot
;
930 raid_end_bio_io(r10_bio
);
933 mirror
= conf
->mirrors
+ disk
;
935 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
936 md_trim_bio(read_bio
, r10_bio
->sector
- bio
->bi_sector
,
939 r10_bio
->devs
[slot
].bio
= read_bio
;
941 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
942 mirror
->rdev
->data_offset
;
943 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
944 read_bio
->bi_end_io
= raid10_end_read_request
;
945 read_bio
->bi_rw
= READ
| do_sync
;
946 read_bio
->bi_private
= r10_bio
;
948 if (max_sectors
< r10_bio
->sectors
) {
949 /* Could not read all from this device, so we will
950 * need another r10_bio.
952 sectors_handled
= (r10_bio
->sectors
+ max_sectors
954 r10_bio
->sectors
= max_sectors
;
955 spin_lock_irq(&conf
->device_lock
);
956 if (bio
->bi_phys_segments
== 0)
957 bio
->bi_phys_segments
= 2;
959 bio
->bi_phys_segments
++;
960 spin_unlock(&conf
->device_lock
);
961 /* Cannot call generic_make_request directly
962 * as that will be queued in __generic_make_request
963 * and subsequent mempool_alloc might block
964 * waiting for it. so hand bio over to raid10d.
966 reschedule_retry(r10_bio
);
968 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
970 r10_bio
->master_bio
= bio
;
971 r10_bio
->sectors
= ((bio
->bi_size
>> 9)
974 r10_bio
->mddev
= mddev
;
975 r10_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
978 generic_make_request(read_bio
);
985 /* first select target devices under rcu_lock and
986 * inc refcount on their rdev. Record them by setting
988 * If there are known/acknowledged bad blocks on any device
989 * on which we have seen a write error, we want to avoid
990 * writing to those blocks. This potentially requires several
991 * writes to write around the bad blocks. Each set of writes
992 * gets its own r10_bio with a set of bios attached. The number
993 * of r10_bios is recored in bio->bi_phys_segments just as with
996 plugged
= mddev_check_plugged(mddev
);
998 raid10_find_phys(conf
, r10_bio
);
1000 blocked_rdev
= NULL
;
1002 max_sectors
= r10_bio
->sectors
;
1004 for (i
= 0; i
< conf
->copies
; i
++) {
1005 int d
= r10_bio
->devs
[i
].devnum
;
1006 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1007 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1008 atomic_inc(&rdev
->nr_pending
);
1009 blocked_rdev
= rdev
;
1012 r10_bio
->devs
[i
].bio
= NULL
;
1013 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)) {
1014 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
1017 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1019 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1023 is_bad
= is_badblock(rdev
, dev_sector
,
1025 &first_bad
, &bad_sectors
);
1027 /* Mustn't write here until the bad block
1030 atomic_inc(&rdev
->nr_pending
);
1031 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1032 blocked_rdev
= rdev
;
1035 if (is_bad
&& first_bad
<= dev_sector
) {
1036 /* Cannot write here at all */
1037 bad_sectors
-= (dev_sector
- first_bad
);
1038 if (bad_sectors
< max_sectors
)
1039 /* Mustn't write more than bad_sectors
1040 * to other devices yet
1042 max_sectors
= bad_sectors
;
1043 /* We don't set R10BIO_Degraded as that
1044 * only applies if the disk is missing,
1045 * so it might be re-added, and we want to
1046 * know to recover this chunk.
1047 * In this case the device is here, and the
1048 * fact that this chunk is not in-sync is
1049 * recorded in the bad block log.
1054 int good_sectors
= first_bad
- dev_sector
;
1055 if (good_sectors
< max_sectors
)
1056 max_sectors
= good_sectors
;
1059 r10_bio
->devs
[i
].bio
= bio
;
1060 atomic_inc(&rdev
->nr_pending
);
1064 if (unlikely(blocked_rdev
)) {
1065 /* Have to wait for this device to get unblocked, then retry */
1069 for (j
= 0; j
< i
; j
++)
1070 if (r10_bio
->devs
[j
].bio
) {
1071 d
= r10_bio
->devs
[j
].devnum
;
1072 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1074 allow_barrier(conf
);
1075 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1080 if (max_sectors
< r10_bio
->sectors
) {
1081 /* We are splitting this into multiple parts, so
1082 * we need to prepare for allocating another r10_bio.
1084 r10_bio
->sectors
= max_sectors
;
1085 spin_lock_irq(&conf
->device_lock
);
1086 if (bio
->bi_phys_segments
== 0)
1087 bio
->bi_phys_segments
= 2;
1089 bio
->bi_phys_segments
++;
1090 spin_unlock_irq(&conf
->device_lock
);
1092 sectors_handled
= r10_bio
->sector
+ max_sectors
- bio
->bi_sector
;
1094 atomic_set(&r10_bio
->remaining
, 1);
1095 bitmap_startwrite(mddev
->bitmap
, r10_bio
->sector
, r10_bio
->sectors
, 0);
1097 for (i
= 0; i
< conf
->copies
; i
++) {
1099 int d
= r10_bio
->devs
[i
].devnum
;
1100 if (!r10_bio
->devs
[i
].bio
)
1103 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1104 md_trim_bio(mbio
, r10_bio
->sector
- bio
->bi_sector
,
1106 r10_bio
->devs
[i
].bio
= mbio
;
1108 mbio
->bi_sector
= (r10_bio
->devs
[i
].addr
+
1109 conf
->mirrors
[d
].rdev
->data_offset
);
1110 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1111 mbio
->bi_end_io
= raid10_end_write_request
;
1112 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
1113 mbio
->bi_private
= r10_bio
;
1115 atomic_inc(&r10_bio
->remaining
);
1116 spin_lock_irqsave(&conf
->device_lock
, flags
);
1117 bio_list_add(&conf
->pending_bio_list
, mbio
);
1118 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1121 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1122 /* This matches the end of raid10_end_write_request() */
1123 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
1125 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
1127 md_write_end(mddev
);
1128 raid_end_bio_io(r10_bio
);
1131 /* In case raid10d snuck in to freeze_array */
1132 wake_up(&conf
->wait_barrier
);
1134 if (sectors_handled
< (bio
->bi_size
>> 9)) {
1135 /* We need another r1_bio. It has already been counted
1136 * in bio->bi_phys_segments.
1138 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1140 r10_bio
->master_bio
= bio
;
1141 r10_bio
->sectors
= (bio
->bi_size
>> 9) - sectors_handled
;
1143 r10_bio
->mddev
= mddev
;
1144 r10_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
1149 if (do_sync
|| !mddev
->bitmap
|| !plugged
)
1150 md_wakeup_thread(mddev
->thread
);
1154 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
1156 conf_t
*conf
= mddev
->private;
1159 if (conf
->near_copies
< conf
->raid_disks
)
1160 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1161 if (conf
->near_copies
> 1)
1162 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
1163 if (conf
->far_copies
> 1) {
1164 if (conf
->far_offset
)
1165 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
1167 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
1169 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1170 conf
->raid_disks
- mddev
->degraded
);
1171 for (i
= 0; i
< conf
->raid_disks
; i
++)
1172 seq_printf(seq
, "%s",
1173 conf
->mirrors
[i
].rdev
&&
1174 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
1175 seq_printf(seq
, "]");
1178 /* check if there are enough drives for
1179 * every block to appear on atleast one.
1180 * Don't consider the device numbered 'ignore'
1181 * as we might be about to remove it.
1183 static int enough(conf_t
*conf
, int ignore
)
1188 int n
= conf
->copies
;
1191 if (conf
->mirrors
[first
].rdev
&&
1194 first
= (first
+1) % conf
->raid_disks
;
1198 } while (first
!= 0);
1202 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1204 char b
[BDEVNAME_SIZE
];
1205 conf_t
*conf
= mddev
->private;
1208 * If it is not operational, then we have already marked it as dead
1209 * else if it is the last working disks, ignore the error, let the
1210 * next level up know.
1211 * else mark the drive as failed
1213 if (test_bit(In_sync
, &rdev
->flags
)
1214 && !enough(conf
, rdev
->raid_disk
))
1216 * Don't fail the drive, just return an IO error.
1219 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1220 unsigned long flags
;
1221 spin_lock_irqsave(&conf
->device_lock
, flags
);
1223 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1225 * if recovery is running, make sure it aborts.
1227 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1229 set_bit(Blocked
, &rdev
->flags
);
1230 set_bit(Faulty
, &rdev
->flags
);
1231 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1233 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1234 "md/raid10:%s: Operation continuing on %d devices.\n",
1235 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1236 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1239 static void print_conf(conf_t
*conf
)
1244 printk(KERN_DEBUG
"RAID10 conf printout:\n");
1246 printk(KERN_DEBUG
"(!conf)\n");
1249 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1252 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1253 char b
[BDEVNAME_SIZE
];
1254 tmp
= conf
->mirrors
+ i
;
1256 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1257 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1258 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1259 bdevname(tmp
->rdev
->bdev
,b
));
1263 static void close_sync(conf_t
*conf
)
1266 allow_barrier(conf
);
1268 mempool_destroy(conf
->r10buf_pool
);
1269 conf
->r10buf_pool
= NULL
;
1272 static int raid10_spare_active(mddev_t
*mddev
)
1275 conf_t
*conf
= mddev
->private;
1278 unsigned long flags
;
1281 * Find all non-in_sync disks within the RAID10 configuration
1282 * and mark them in_sync
1284 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1285 tmp
= conf
->mirrors
+ i
;
1287 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1288 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1290 sysfs_notify_dirent(tmp
->rdev
->sysfs_state
);
1293 spin_lock_irqsave(&conf
->device_lock
, flags
);
1294 mddev
->degraded
-= count
;
1295 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1302 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1304 conf_t
*conf
= mddev
->private;
1308 int last
= conf
->raid_disks
- 1;
1310 if (mddev
->recovery_cp
< MaxSector
)
1311 /* only hot-add to in-sync arrays, as recovery is
1312 * very different from resync
1315 if (!enough(conf
, -1))
1318 if (rdev
->raid_disk
>= 0)
1319 first
= last
= rdev
->raid_disk
;
1321 if (rdev
->saved_raid_disk
>= first
&&
1322 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1323 mirror
= rdev
->saved_raid_disk
;
1326 for ( ; mirror
<= last
; mirror
++) {
1327 mirror_info_t
*p
= &conf
->mirrors
[mirror
];
1328 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
1333 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1334 rdev
->data_offset
<< 9);
1335 /* as we don't honour merge_bvec_fn, we must
1336 * never risk violating it, so limit
1337 * ->max_segments to one lying with a single
1338 * page, as a one page request is never in
1341 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1342 blk_queue_max_segments(mddev
->queue
, 1);
1343 blk_queue_segment_boundary(mddev
->queue
,
1344 PAGE_CACHE_SIZE
- 1);
1347 p
->head_position
= 0;
1348 rdev
->raid_disk
= mirror
;
1350 if (rdev
->saved_raid_disk
!= mirror
)
1352 rcu_assign_pointer(p
->rdev
, rdev
);
1356 md_integrity_add_rdev(rdev
, mddev
);
1361 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1363 conf_t
*conf
= mddev
->private;
1366 mirror_info_t
*p
= conf
->mirrors
+ number
;
1371 if (test_bit(In_sync
, &rdev
->flags
) ||
1372 atomic_read(&rdev
->nr_pending
)) {
1376 /* Only remove faulty devices in recovery
1379 if (!test_bit(Faulty
, &rdev
->flags
) &&
1380 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
1387 if (atomic_read(&rdev
->nr_pending
)) {
1388 /* lost the race, try later */
1393 err
= md_integrity_register(mddev
);
1402 static void end_sync_read(struct bio
*bio
, int error
)
1404 r10bio_t
*r10_bio
= bio
->bi_private
;
1405 conf_t
*conf
= r10_bio
->mddev
->private;
1408 d
= find_bio_disk(conf
, r10_bio
, bio
, NULL
);
1410 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1411 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1413 atomic_add(r10_bio
->sectors
,
1414 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1415 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1416 md_error(r10_bio
->mddev
,
1417 conf
->mirrors
[d
].rdev
);
1420 /* for reconstruct, we always reschedule after a read.
1421 * for resync, only after all reads
1423 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1424 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1425 atomic_dec_and_test(&r10_bio
->remaining
)) {
1426 /* we have read all the blocks,
1427 * do the comparison in process context in raid10d
1429 reschedule_retry(r10_bio
);
1433 static void end_sync_write(struct bio
*bio
, int error
)
1435 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1436 r10bio_t
*r10_bio
= bio
->bi_private
;
1437 mddev_t
*mddev
= r10_bio
->mddev
;
1438 conf_t
*conf
= mddev
->private;
1444 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
);
1447 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1448 else if (is_badblock(conf
->mirrors
[d
].rdev
,
1449 r10_bio
->devs
[slot
].addr
,
1451 &first_bad
, &bad_sectors
))
1452 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
1454 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1455 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1456 if (r10_bio
->master_bio
== NULL
) {
1457 /* the primary of several recovery bios */
1458 sector_t s
= r10_bio
->sectors
;
1459 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
1460 reschedule_retry(r10_bio
);
1463 md_done_sync(mddev
, s
, 1);
1466 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1467 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
1468 reschedule_retry(r10_bio
);
1477 * Note: sync and recover and handled very differently for raid10
1478 * This code is for resync.
1479 * For resync, we read through virtual addresses and read all blocks.
1480 * If there is any error, we schedule a write. The lowest numbered
1481 * drive is authoritative.
1482 * However requests come for physical address, so we need to map.
1483 * For every physical address there are raid_disks/copies virtual addresses,
1484 * which is always are least one, but is not necessarly an integer.
1485 * This means that a physical address can span multiple chunks, so we may
1486 * have to submit multiple io requests for a single sync request.
1489 * We check if all blocks are in-sync and only write to blocks that
1492 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1494 conf_t
*conf
= mddev
->private;
1496 struct bio
*tbio
, *fbio
;
1498 atomic_set(&r10_bio
->remaining
, 1);
1500 /* find the first device with a block */
1501 for (i
=0; i
<conf
->copies
; i
++)
1502 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1505 if (i
== conf
->copies
)
1509 fbio
= r10_bio
->devs
[i
].bio
;
1511 /* now find blocks with errors */
1512 for (i
=0 ; i
< conf
->copies
; i
++) {
1514 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1516 tbio
= r10_bio
->devs
[i
].bio
;
1518 if (tbio
->bi_end_io
!= end_sync_read
)
1522 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1523 /* We know that the bi_io_vec layout is the same for
1524 * both 'first' and 'i', so we just compare them.
1525 * All vec entries are PAGE_SIZE;
1527 for (j
= 0; j
< vcnt
; j
++)
1528 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1529 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1534 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1536 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1537 /* Don't fix anything. */
1539 /* Ok, we need to write this bio
1540 * First we need to fixup bv_offset, bv_len and
1541 * bi_vecs, as the read request might have corrupted these
1543 tbio
->bi_vcnt
= vcnt
;
1544 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1546 tbio
->bi_phys_segments
= 0;
1547 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1548 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1549 tbio
->bi_next
= NULL
;
1550 tbio
->bi_rw
= WRITE
;
1551 tbio
->bi_private
= r10_bio
;
1552 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1554 for (j
=0; j
< vcnt
; j
++) {
1555 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1556 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1558 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1559 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1562 tbio
->bi_end_io
= end_sync_write
;
1564 d
= r10_bio
->devs
[i
].devnum
;
1565 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1566 atomic_inc(&r10_bio
->remaining
);
1567 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1569 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1570 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1571 generic_make_request(tbio
);
1575 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1576 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1582 * Now for the recovery code.
1583 * Recovery happens across physical sectors.
1584 * We recover all non-is_sync drives by finding the virtual address of
1585 * each, and then choose a working drive that also has that virt address.
1586 * There is a separate r10_bio for each non-in_sync drive.
1587 * Only the first two slots are in use. The first for reading,
1588 * The second for writing.
1592 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1594 conf_t
*conf
= mddev
->private;
1599 * share the pages with the first bio
1600 * and submit the write request
1602 wbio
= r10_bio
->devs
[1].bio
;
1603 d
= r10_bio
->devs
[1].devnum
;
1605 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1606 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1607 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1608 generic_make_request(wbio
);
1611 "md/raid10:%s: recovery aborted due to read error\n",
1613 conf
->mirrors
[d
].recovery_disabled
= mddev
->recovery_disabled
;
1614 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1621 * Used by fix_read_error() to decay the per rdev read_errors.
1622 * We halve the read error count for every hour that has elapsed
1623 * since the last recorded read error.
1626 static void check_decay_read_errors(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1628 struct timespec cur_time_mon
;
1629 unsigned long hours_since_last
;
1630 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
1632 ktime_get_ts(&cur_time_mon
);
1634 if (rdev
->last_read_error
.tv_sec
== 0 &&
1635 rdev
->last_read_error
.tv_nsec
== 0) {
1636 /* first time we've seen a read error */
1637 rdev
->last_read_error
= cur_time_mon
;
1641 hours_since_last
= (cur_time_mon
.tv_sec
-
1642 rdev
->last_read_error
.tv_sec
) / 3600;
1644 rdev
->last_read_error
= cur_time_mon
;
1647 * if hours_since_last is > the number of bits in read_errors
1648 * just set read errors to 0. We do this to avoid
1649 * overflowing the shift of read_errors by hours_since_last.
1651 if (hours_since_last
>= 8 * sizeof(read_errors
))
1652 atomic_set(&rdev
->read_errors
, 0);
1654 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
1658 * This is a kernel thread which:
1660 * 1. Retries failed read operations on working mirrors.
1661 * 2. Updates the raid superblock when problems encounter.
1662 * 3. Performs writes following reads for array synchronising.
1665 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1667 int sect
= 0; /* Offset from r10_bio->sector */
1668 int sectors
= r10_bio
->sectors
;
1670 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
1671 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1673 /* still own a reference to this rdev, so it cannot
1674 * have been cleared recently.
1676 rdev
= conf
->mirrors
[d
].rdev
;
1678 if (test_bit(Faulty
, &rdev
->flags
))
1679 /* drive has already been failed, just ignore any
1680 more fix_read_error() attempts */
1683 check_decay_read_errors(mddev
, rdev
);
1684 atomic_inc(&rdev
->read_errors
);
1685 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
1686 char b
[BDEVNAME_SIZE
];
1687 bdevname(rdev
->bdev
, b
);
1690 "md/raid10:%s: %s: Raid device exceeded "
1691 "read_error threshold [cur %d:max %d]\n",
1693 atomic_read(&rdev
->read_errors
), max_read_errors
);
1695 "md/raid10:%s: %s: Failing raid device\n",
1697 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1703 int sl
= r10_bio
->read_slot
;
1707 if (s
> (PAGE_SIZE
>>9))
1715 d
= r10_bio
->devs
[sl
].devnum
;
1716 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1718 test_bit(In_sync
, &rdev
->flags
) &&
1719 is_badblock(rdev
, r10_bio
->devs
[sl
].addr
+ sect
, s
,
1720 &first_bad
, &bad_sectors
) == 0) {
1721 atomic_inc(&rdev
->nr_pending
);
1723 success
= sync_page_io(rdev
,
1724 r10_bio
->devs
[sl
].addr
+
1727 conf
->tmppage
, READ
, false);
1728 rdev_dec_pending(rdev
, mddev
);
1734 if (sl
== conf
->copies
)
1736 } while (!success
&& sl
!= r10_bio
->read_slot
);
1740 /* Cannot read from anywhere -- bye bye array */
1741 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1742 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1747 /* write it back and re-read */
1749 while (sl
!= r10_bio
->read_slot
) {
1750 char b
[BDEVNAME_SIZE
];
1755 d
= r10_bio
->devs
[sl
].devnum
;
1756 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1758 !test_bit(In_sync
, &rdev
->flags
))
1761 atomic_inc(&rdev
->nr_pending
);
1763 if (sync_page_io(rdev
,
1764 r10_bio
->devs
[sl
].addr
+
1766 s
<<9, conf
->tmppage
, WRITE
, false)
1768 /* Well, this device is dead */
1770 "md/raid10:%s: read correction "
1772 " (%d sectors at %llu on %s)\n",
1774 (unsigned long long)(
1775 sect
+ rdev
->data_offset
),
1776 bdevname(rdev
->bdev
, b
));
1777 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1780 bdevname(rdev
->bdev
, b
));
1781 md_error(mddev
, rdev
);
1783 rdev_dec_pending(rdev
, mddev
);
1787 while (sl
!= r10_bio
->read_slot
) {
1788 char b
[BDEVNAME_SIZE
];
1793 d
= r10_bio
->devs
[sl
].devnum
;
1794 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1796 !test_bit(In_sync
, &rdev
->flags
))
1799 atomic_inc(&rdev
->nr_pending
);
1801 if (sync_page_io(rdev
,
1802 r10_bio
->devs
[sl
].addr
+
1804 s
<<9, conf
->tmppage
,
1805 READ
, false) == 0) {
1806 /* Well, this device is dead */
1808 "md/raid10:%s: unable to read back "
1810 " (%d sectors at %llu on %s)\n",
1812 (unsigned long long)(
1813 sect
+ rdev
->data_offset
),
1814 bdevname(rdev
->bdev
, b
));
1815 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1818 bdevname(rdev
->bdev
, b
));
1820 md_error(mddev
, rdev
);
1823 "md/raid10:%s: read error corrected"
1824 " (%d sectors at %llu on %s)\n",
1826 (unsigned long long)(
1827 sect
+ rdev
->data_offset
),
1828 bdevname(rdev
->bdev
, b
));
1829 atomic_add(s
, &rdev
->corrected_errors
);
1832 rdev_dec_pending(rdev
, mddev
);
1842 static void handle_read_error(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1844 int slot
= r10_bio
->read_slot
;
1845 int mirror
= r10_bio
->devs
[slot
].devnum
;
1847 conf_t
*conf
= mddev
->private;
1849 char b
[BDEVNAME_SIZE
];
1850 unsigned long do_sync
;
1853 /* we got a read error. Maybe the drive is bad. Maybe just
1854 * the block and we can fix it.
1855 * We freeze all other IO, and try reading the block from
1856 * other devices. When we find one, we re-write
1857 * and check it that fixes the read error.
1858 * This is all done synchronously while the array is
1861 if (mddev
->ro
== 0) {
1863 fix_read_error(conf
, mddev
, r10_bio
);
1864 unfreeze_array(conf
);
1866 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, mddev
);
1868 bio
= r10_bio
->devs
[slot
].bio
;
1869 bdevname(bio
->bi_bdev
, b
);
1870 r10_bio
->devs
[slot
].bio
=
1871 mddev
->ro
? IO_BLOCKED
: NULL
;
1873 mirror
= read_balance(conf
, r10_bio
, &max_sectors
);
1875 printk(KERN_ALERT
"md/raid10:%s: %s: unrecoverable I/O"
1876 " read error for block %llu\n",
1878 (unsigned long long)r10_bio
->sector
);
1879 raid_end_bio_io(r10_bio
);
1884 do_sync
= (r10_bio
->master_bio
->bi_rw
& REQ_SYNC
);
1887 slot
= r10_bio
->read_slot
;
1888 rdev
= conf
->mirrors
[mirror
].rdev
;
1891 "md/raid10:%s: %s: redirecting"
1892 "sector %llu to another mirror\n",
1894 bdevname(rdev
->bdev
, b
),
1895 (unsigned long long)r10_bio
->sector
);
1896 bio
= bio_clone_mddev(r10_bio
->master_bio
,
1899 r10_bio
->sector
- bio
->bi_sector
,
1901 r10_bio
->devs
[slot
].bio
= bio
;
1902 bio
->bi_sector
= r10_bio
->devs
[slot
].addr
1903 + rdev
->data_offset
;
1904 bio
->bi_bdev
= rdev
->bdev
;
1905 bio
->bi_rw
= READ
| do_sync
;
1906 bio
->bi_private
= r10_bio
;
1907 bio
->bi_end_io
= raid10_end_read_request
;
1908 if (max_sectors
< r10_bio
->sectors
) {
1909 /* Drat - have to split this up more */
1910 struct bio
*mbio
= r10_bio
->master_bio
;
1911 int sectors_handled
=
1912 r10_bio
->sector
+ max_sectors
1914 r10_bio
->sectors
= max_sectors
;
1915 spin_lock_irq(&conf
->device_lock
);
1916 if (mbio
->bi_phys_segments
== 0)
1917 mbio
->bi_phys_segments
= 2;
1919 mbio
->bi_phys_segments
++;
1920 spin_unlock_irq(&conf
->device_lock
);
1921 generic_make_request(bio
);
1924 r10_bio
= mempool_alloc(conf
->r10bio_pool
,
1926 r10_bio
->master_bio
= mbio
;
1927 r10_bio
->sectors
= (mbio
->bi_size
>> 9)
1930 set_bit(R10BIO_ReadError
,
1932 r10_bio
->mddev
= mddev
;
1933 r10_bio
->sector
= mbio
->bi_sector
1938 generic_make_request(bio
);
1941 static void handle_write_completed(conf_t
*conf
, r10bio_t
*r10_bio
)
1943 /* Some sort of write request has finished and it
1944 * succeeded in writing where we thought there was a
1945 * bad block. So forget the bad block.
1950 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
) ||
1951 test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
1952 for (m
= 0; m
< conf
->copies
; m
++)
1953 if (r10_bio
->devs
[m
].bio
&&
1954 test_bit(BIO_UPTODATE
,
1955 &r10_bio
->devs
[m
].bio
->bi_flags
)) {
1956 int dev
= r10_bio
->devs
[m
].devnum
;
1957 rdev
= conf
->mirrors
[dev
].rdev
;
1958 rdev_clear_badblocks(
1960 r10_bio
->devs
[m
].addr
,
1965 for (m
= 0; m
< conf
->copies
; m
++)
1966 if (r10_bio
->devs
[m
].bio
== IO_MADE_GOOD
) {
1967 int dev
= r10_bio
->devs
[m
].devnum
;
1968 rdev
= conf
->mirrors
[dev
].rdev
;
1969 rdev_clear_badblocks(
1971 r10_bio
->devs
[m
].addr
,
1973 rdev_dec_pending(rdev
, conf
->mddev
);
1975 raid_end_bio_io(r10_bio
);
1979 static void raid10d(mddev_t
*mddev
)
1982 unsigned long flags
;
1983 conf_t
*conf
= mddev
->private;
1984 struct list_head
*head
= &conf
->retry_list
;
1985 struct blk_plug plug
;
1987 md_check_recovery(mddev
);
1989 blk_start_plug(&plug
);
1992 flush_pending_writes(conf
);
1994 spin_lock_irqsave(&conf
->device_lock
, flags
);
1995 if (list_empty(head
)) {
1996 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1999 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
2000 list_del(head
->prev
);
2002 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2004 mddev
= r10_bio
->mddev
;
2005 conf
= mddev
->private;
2006 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
2007 handle_write_completed(conf
, r10_bio
);
2008 else if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
2009 sync_request_write(mddev
, r10_bio
);
2010 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
2011 recovery_request_write(mddev
, r10_bio
);
2012 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
2013 handle_read_error(mddev
, r10_bio
);
2015 /* just a partial read to be scheduled from a
2018 int slot
= r10_bio
->read_slot
;
2019 generic_make_request(r10_bio
->devs
[slot
].bio
);
2023 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
2024 md_check_recovery(mddev
);
2026 blk_finish_plug(&plug
);
2030 static int init_resync(conf_t
*conf
)
2034 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2035 BUG_ON(conf
->r10buf_pool
);
2036 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
2037 if (!conf
->r10buf_pool
)
2039 conf
->next_resync
= 0;
2044 * perform a "sync" on one "block"
2046 * We need to make sure that no normal I/O request - particularly write
2047 * requests - conflict with active sync requests.
2049 * This is achieved by tracking pending requests and a 'barrier' concept
2050 * that can be installed to exclude normal IO requests.
2052 * Resync and recovery are handled very differently.
2053 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2055 * For resync, we iterate over virtual addresses, read all copies,
2056 * and update if there are differences. If only one copy is live,
2058 * For recovery, we iterate over physical addresses, read a good
2059 * value for each non-in_sync drive, and over-write.
2061 * So, for recovery we may have several outstanding complex requests for a
2062 * given address, one for each out-of-sync device. We model this by allocating
2063 * a number of r10_bio structures, one for each out-of-sync device.
2064 * As we setup these structures, we collect all bio's together into a list
2065 * which we then process collectively to add pages, and then process again
2066 * to pass to generic_make_request.
2068 * The r10_bio structures are linked using a borrowed master_bio pointer.
2069 * This link is counted in ->remaining. When the r10_bio that points to NULL
2070 * has its remaining count decremented to 0, the whole complex operation
2075 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
,
2076 int *skipped
, int go_faster
)
2078 conf_t
*conf
= mddev
->private;
2080 struct bio
*biolist
= NULL
, *bio
;
2081 sector_t max_sector
, nr_sectors
;
2084 sector_t sync_blocks
;
2085 sector_t sectors_skipped
= 0;
2086 int chunks_skipped
= 0;
2088 if (!conf
->r10buf_pool
)
2089 if (init_resync(conf
))
2093 max_sector
= mddev
->dev_sectors
;
2094 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2095 max_sector
= mddev
->resync_max_sectors
;
2096 if (sector_nr
>= max_sector
) {
2097 /* If we aborted, we need to abort the
2098 * sync on the 'current' bitmap chucks (there can
2099 * be several when recovering multiple devices).
2100 * as we may have started syncing it but not finished.
2101 * We can find the current address in
2102 * mddev->curr_resync, but for recovery,
2103 * we need to convert that to several
2104 * virtual addresses.
2106 if (mddev
->curr_resync
< max_sector
) { /* aborted */
2107 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2108 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2110 else for (i
=0; i
<conf
->raid_disks
; i
++) {
2112 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
2113 bitmap_end_sync(mddev
->bitmap
, sect
,
2116 } else /* completed sync */
2119 bitmap_close_sync(mddev
->bitmap
);
2122 return sectors_skipped
;
2124 if (chunks_skipped
>= conf
->raid_disks
) {
2125 /* if there has been nothing to do on any drive,
2126 * then there is nothing to do at all..
2129 return (max_sector
- sector_nr
) + sectors_skipped
;
2132 if (max_sector
> mddev
->resync_max
)
2133 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
2135 /* make sure whole request will fit in a chunk - if chunks
2138 if (conf
->near_copies
< conf
->raid_disks
&&
2139 max_sector
> (sector_nr
| conf
->chunk_mask
))
2140 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
2142 * If there is non-resync activity waiting for us then
2143 * put in a delay to throttle resync.
2145 if (!go_faster
&& conf
->nr_waiting
)
2146 msleep_interruptible(1000);
2148 /* Again, very different code for resync and recovery.
2149 * Both must result in an r10bio with a list of bios that
2150 * have bi_end_io, bi_sector, bi_bdev set,
2151 * and bi_private set to the r10bio.
2152 * For recovery, we may actually create several r10bios
2153 * with 2 bios in each, that correspond to the bios in the main one.
2154 * In this case, the subordinate r10bios link back through a
2155 * borrowed master_bio pointer, and the counter in the master
2156 * includes a ref from each subordinate.
2158 /* First, we decide what to do and set ->bi_end_io
2159 * To end_sync_read if we want to read, and
2160 * end_sync_write if we will want to write.
2163 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
2164 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2165 /* recovery... the complicated one */
2169 for (i
=0 ; i
<conf
->raid_disks
; i
++) {
2176 if (conf
->mirrors
[i
].rdev
== NULL
||
2177 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
))
2181 /* want to reconstruct this device */
2183 sect
= raid10_find_virt(conf
, sector_nr
, i
);
2184 /* Unless we are doing a full sync, we only need
2185 * to recover the block if it is set in the bitmap
2187 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
2189 if (sync_blocks
< max_sync
)
2190 max_sync
= sync_blocks
;
2193 /* yep, skip the sync_blocks here, but don't assume
2194 * that there will never be anything to do here
2196 chunks_skipped
= -1;
2200 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2201 raise_barrier(conf
, rb2
!= NULL
);
2202 atomic_set(&r10_bio
->remaining
, 0);
2204 r10_bio
->master_bio
= (struct bio
*)rb2
;
2206 atomic_inc(&rb2
->remaining
);
2207 r10_bio
->mddev
= mddev
;
2208 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
2209 r10_bio
->sector
= sect
;
2211 raid10_find_phys(conf
, r10_bio
);
2213 /* Need to check if the array will still be
2216 for (j
=0; j
<conf
->raid_disks
; j
++)
2217 if (conf
->mirrors
[j
].rdev
== NULL
||
2218 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
2223 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
2224 &sync_blocks
, still_degraded
);
2227 for (j
=0; j
<conf
->copies
;j
++) {
2229 int d
= r10_bio
->devs
[j
].devnum
;
2231 sector_t sector
, first_bad
;
2233 if (!conf
->mirrors
[d
].rdev
||
2234 !test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
))
2236 /* This is where we read from */
2238 rdev
= conf
->mirrors
[d
].rdev
;
2239 sector
= r10_bio
->devs
[j
].addr
;
2241 if (is_badblock(rdev
, sector
, max_sync
,
2242 &first_bad
, &bad_sectors
)) {
2243 if (first_bad
> sector
)
2244 max_sync
= first_bad
- sector
;
2246 bad_sectors
-= (sector
2248 if (max_sync
> bad_sectors
)
2249 max_sync
= bad_sectors
;
2253 bio
= r10_bio
->devs
[0].bio
;
2254 bio
->bi_next
= biolist
;
2256 bio
->bi_private
= r10_bio
;
2257 bio
->bi_end_io
= end_sync_read
;
2259 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
2260 conf
->mirrors
[d
].rdev
->data_offset
;
2261 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2262 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2263 atomic_inc(&r10_bio
->remaining
);
2264 /* and we write to 'i' */
2266 for (k
=0; k
<conf
->copies
; k
++)
2267 if (r10_bio
->devs
[k
].devnum
== i
)
2269 BUG_ON(k
== conf
->copies
);
2270 bio
= r10_bio
->devs
[1].bio
;
2271 bio
->bi_next
= biolist
;
2273 bio
->bi_private
= r10_bio
;
2274 bio
->bi_end_io
= end_sync_write
;
2276 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
2277 conf
->mirrors
[i
].rdev
->data_offset
;
2278 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
2280 r10_bio
->devs
[0].devnum
= d
;
2281 r10_bio
->devs
[1].devnum
= i
;
2285 if (j
== conf
->copies
) {
2286 /* Cannot recover, so abort the recovery or
2287 * record a bad block */
2290 atomic_dec(&rb2
->remaining
);
2293 /* problem is that there are bad blocks
2294 * on other device(s)
2297 for (k
= 0; k
< conf
->copies
; k
++)
2298 if (r10_bio
->devs
[k
].devnum
== i
)
2300 if (!rdev_set_badblocks(
2301 conf
->mirrors
[i
].rdev
,
2302 r10_bio
->devs
[k
].addr
,
2307 if (!test_and_set_bit(MD_RECOVERY_INTR
,
2309 printk(KERN_INFO
"md/raid10:%s: insufficient "
2310 "working devices for recovery.\n",
2312 conf
->mirrors
[i
].recovery_disabled
2313 = mddev
->recovery_disabled
;
2318 if (biolist
== NULL
) {
2320 r10bio_t
*rb2
= r10_bio
;
2321 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
2322 rb2
->master_bio
= NULL
;
2328 /* resync. Schedule a read for every block at this virt offset */
2331 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
2333 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2334 &sync_blocks
, mddev
->degraded
) &&
2335 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
2336 &mddev
->recovery
)) {
2337 /* We can skip this block */
2339 return sync_blocks
+ sectors_skipped
;
2341 if (sync_blocks
< max_sync
)
2342 max_sync
= sync_blocks
;
2343 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2345 r10_bio
->mddev
= mddev
;
2346 atomic_set(&r10_bio
->remaining
, 0);
2347 raise_barrier(conf
, 0);
2348 conf
->next_resync
= sector_nr
;
2350 r10_bio
->master_bio
= NULL
;
2351 r10_bio
->sector
= sector_nr
;
2352 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
2353 raid10_find_phys(conf
, r10_bio
);
2354 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
2356 for (i
=0; i
<conf
->copies
; i
++) {
2357 int d
= r10_bio
->devs
[i
].devnum
;
2358 sector_t first_bad
, sector
;
2361 bio
= r10_bio
->devs
[i
].bio
;
2362 bio
->bi_end_io
= NULL
;
2363 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2364 if (conf
->mirrors
[d
].rdev
== NULL
||
2365 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
2367 sector
= r10_bio
->devs
[i
].addr
;
2368 if (is_badblock(conf
->mirrors
[d
].rdev
,
2370 &first_bad
, &bad_sectors
)) {
2371 if (first_bad
> sector
)
2372 max_sync
= first_bad
- sector
;
2374 bad_sectors
-= (sector
- first_bad
);
2375 if (max_sync
> bad_sectors
)
2376 max_sync
= max_sync
;
2380 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2381 atomic_inc(&r10_bio
->remaining
);
2382 bio
->bi_next
= biolist
;
2384 bio
->bi_private
= r10_bio
;
2385 bio
->bi_end_io
= end_sync_read
;
2387 bio
->bi_sector
= sector
+
2388 conf
->mirrors
[d
].rdev
->data_offset
;
2389 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2394 for (i
=0; i
<conf
->copies
; i
++) {
2395 int d
= r10_bio
->devs
[i
].devnum
;
2396 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
2397 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
2406 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2408 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
2410 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2413 bio
->bi_phys_segments
= 0;
2418 if (sector_nr
+ max_sync
< max_sector
)
2419 max_sector
= sector_nr
+ max_sync
;
2422 int len
= PAGE_SIZE
;
2423 if (sector_nr
+ (len
>>9) > max_sector
)
2424 len
= (max_sector
- sector_nr
) << 9;
2427 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2429 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2430 if (bio_add_page(bio
, page
, len
, 0))
2434 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2435 for (bio2
= biolist
;
2436 bio2
&& bio2
!= bio
;
2437 bio2
= bio2
->bi_next
) {
2438 /* remove last page from this bio */
2440 bio2
->bi_size
-= len
;
2441 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2445 nr_sectors
+= len
>>9;
2446 sector_nr
+= len
>>9;
2447 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
2449 r10_bio
->sectors
= nr_sectors
;
2453 biolist
= biolist
->bi_next
;
2455 bio
->bi_next
= NULL
;
2456 r10_bio
= bio
->bi_private
;
2457 r10_bio
->sectors
= nr_sectors
;
2459 if (bio
->bi_end_io
== end_sync_read
) {
2460 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2461 generic_make_request(bio
);
2465 if (sectors_skipped
)
2466 /* pretend they weren't skipped, it makes
2467 * no important difference in this case
2469 md_done_sync(mddev
, sectors_skipped
, 1);
2471 return sectors_skipped
+ nr_sectors
;
2473 /* There is nowhere to write, so all non-sync
2474 * drives must be failed or in resync, all drives
2475 * have a bad block, so try the next chunk...
2477 if (sector_nr
+ max_sync
< max_sector
)
2478 max_sector
= sector_nr
+ max_sync
;
2480 sectors_skipped
+= (max_sector
- sector_nr
);
2482 sector_nr
= max_sector
;
2487 raid10_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2490 conf_t
*conf
= mddev
->private;
2493 raid_disks
= conf
->raid_disks
;
2495 sectors
= conf
->dev_sectors
;
2497 size
= sectors
>> conf
->chunk_shift
;
2498 sector_div(size
, conf
->far_copies
);
2499 size
= size
* raid_disks
;
2500 sector_div(size
, conf
->near_copies
);
2502 return size
<< conf
->chunk_shift
;
2506 static conf_t
*setup_conf(mddev_t
*mddev
)
2508 conf_t
*conf
= NULL
;
2510 sector_t stride
, size
;
2513 if (mddev
->new_chunk_sectors
< (PAGE_SIZE
>> 9) ||
2514 !is_power_of_2(mddev
->new_chunk_sectors
)) {
2515 printk(KERN_ERR
"md/raid10:%s: chunk size must be "
2516 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2517 mdname(mddev
), PAGE_SIZE
);
2521 nc
= mddev
->new_layout
& 255;
2522 fc
= (mddev
->new_layout
>> 8) & 255;
2523 fo
= mddev
->new_layout
& (1<<16);
2525 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2526 (mddev
->new_layout
>> 17)) {
2527 printk(KERN_ERR
"md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2528 mdname(mddev
), mddev
->new_layout
);
2533 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2537 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2542 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2547 conf
->raid_disks
= mddev
->raid_disks
;
2548 conf
->near_copies
= nc
;
2549 conf
->far_copies
= fc
;
2550 conf
->copies
= nc
*fc
;
2551 conf
->far_offset
= fo
;
2552 conf
->chunk_mask
= mddev
->new_chunk_sectors
- 1;
2553 conf
->chunk_shift
= ffz(~mddev
->new_chunk_sectors
);
2555 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2556 r10bio_pool_free
, conf
);
2557 if (!conf
->r10bio_pool
)
2560 size
= mddev
->dev_sectors
>> conf
->chunk_shift
;
2561 sector_div(size
, fc
);
2562 size
= size
* conf
->raid_disks
;
2563 sector_div(size
, nc
);
2564 /* 'size' is now the number of chunks in the array */
2565 /* calculate "used chunks per device" in 'stride' */
2566 stride
= size
* conf
->copies
;
2568 /* We need to round up when dividing by raid_disks to
2569 * get the stride size.
2571 stride
+= conf
->raid_disks
- 1;
2572 sector_div(stride
, conf
->raid_disks
);
2574 conf
->dev_sectors
= stride
<< conf
->chunk_shift
;
2579 sector_div(stride
, fc
);
2580 conf
->stride
= stride
<< conf
->chunk_shift
;
2583 spin_lock_init(&conf
->device_lock
);
2584 INIT_LIST_HEAD(&conf
->retry_list
);
2586 spin_lock_init(&conf
->resync_lock
);
2587 init_waitqueue_head(&conf
->wait_barrier
);
2589 conf
->thread
= md_register_thread(raid10d
, mddev
, NULL
);
2593 conf
->mddev
= mddev
;
2597 printk(KERN_ERR
"md/raid10:%s: couldn't allocate memory.\n",
2600 if (conf
->r10bio_pool
)
2601 mempool_destroy(conf
->r10bio_pool
);
2602 kfree(conf
->mirrors
);
2603 safe_put_page(conf
->tmppage
);
2606 return ERR_PTR(err
);
2609 static int run(mddev_t
*mddev
)
2612 int i
, disk_idx
, chunk_size
;
2613 mirror_info_t
*disk
;
2618 * copy the already verified devices into our private RAID10
2619 * bookkeeping area. [whatever we allocate in run(),
2620 * should be freed in stop()]
2623 if (mddev
->private == NULL
) {
2624 conf
= setup_conf(mddev
);
2626 return PTR_ERR(conf
);
2627 mddev
->private = conf
;
2629 conf
= mddev
->private;
2633 mddev
->thread
= conf
->thread
;
2634 conf
->thread
= NULL
;
2636 chunk_size
= mddev
->chunk_sectors
<< 9;
2637 blk_queue_io_min(mddev
->queue
, chunk_size
);
2638 if (conf
->raid_disks
% conf
->near_copies
)
2639 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->raid_disks
);
2641 blk_queue_io_opt(mddev
->queue
, chunk_size
*
2642 (conf
->raid_disks
/ conf
->near_copies
));
2644 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2646 disk_idx
= rdev
->raid_disk
;
2647 if (disk_idx
>= conf
->raid_disks
2650 disk
= conf
->mirrors
+ disk_idx
;
2653 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2654 rdev
->data_offset
<< 9);
2655 /* as we don't honour merge_bvec_fn, we must never risk
2656 * violating it, so limit max_segments to 1 lying
2657 * within a single page.
2659 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2660 blk_queue_max_segments(mddev
->queue
, 1);
2661 blk_queue_segment_boundary(mddev
->queue
,
2662 PAGE_CACHE_SIZE
- 1);
2665 disk
->head_position
= 0;
2667 /* need to check that every block has at least one working mirror */
2668 if (!enough(conf
, -1)) {
2669 printk(KERN_ERR
"md/raid10:%s: not enough operational mirrors.\n",
2674 mddev
->degraded
= 0;
2675 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2677 disk
= conf
->mirrors
+ i
;
2680 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2681 disk
->head_position
= 0;
2688 if (mddev
->recovery_cp
!= MaxSector
)
2689 printk(KERN_NOTICE
"md/raid10:%s: not clean"
2690 " -- starting background reconstruction\n",
2693 "md/raid10:%s: active with %d out of %d devices\n",
2694 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
,
2697 * Ok, everything is just fine now
2699 mddev
->dev_sectors
= conf
->dev_sectors
;
2700 size
= raid10_size(mddev
, 0, 0);
2701 md_set_array_sectors(mddev
, size
);
2702 mddev
->resync_max_sectors
= size
;
2704 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2705 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2707 /* Calculate max read-ahead size.
2708 * We need to readahead at least twice a whole stripe....
2712 int stripe
= conf
->raid_disks
*
2713 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
2714 stripe
/= conf
->near_copies
;
2715 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2716 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2719 if (conf
->near_copies
< conf
->raid_disks
)
2720 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2722 if (md_integrity_register(mddev
))
2728 md_unregister_thread(mddev
->thread
);
2729 if (conf
->r10bio_pool
)
2730 mempool_destroy(conf
->r10bio_pool
);
2731 safe_put_page(conf
->tmppage
);
2732 kfree(conf
->mirrors
);
2734 mddev
->private = NULL
;
2739 static int stop(mddev_t
*mddev
)
2741 conf_t
*conf
= mddev
->private;
2743 raise_barrier(conf
, 0);
2744 lower_barrier(conf
);
2746 md_unregister_thread(mddev
->thread
);
2747 mddev
->thread
= NULL
;
2748 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2749 if (conf
->r10bio_pool
)
2750 mempool_destroy(conf
->r10bio_pool
);
2751 kfree(conf
->mirrors
);
2753 mddev
->private = NULL
;
2757 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2759 conf_t
*conf
= mddev
->private;
2763 raise_barrier(conf
, 0);
2766 lower_barrier(conf
);
2771 static void *raid10_takeover_raid0(mddev_t
*mddev
)
2776 if (mddev
->degraded
> 0) {
2777 printk(KERN_ERR
"md/raid10:%s: Error: degraded raid0!\n",
2779 return ERR_PTR(-EINVAL
);
2782 /* Set new parameters */
2783 mddev
->new_level
= 10;
2784 /* new layout: far_copies = 1, near_copies = 2 */
2785 mddev
->new_layout
= (1<<8) + 2;
2786 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2787 mddev
->delta_disks
= mddev
->raid_disks
;
2788 mddev
->raid_disks
*= 2;
2789 /* make sure it will be not marked as dirty */
2790 mddev
->recovery_cp
= MaxSector
;
2792 conf
= setup_conf(mddev
);
2793 if (!IS_ERR(conf
)) {
2794 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2795 if (rdev
->raid_disk
>= 0)
2796 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
2803 static void *raid10_takeover(mddev_t
*mddev
)
2805 struct raid0_private_data
*raid0_priv
;
2807 /* raid10 can take over:
2808 * raid0 - providing it has only two drives
2810 if (mddev
->level
== 0) {
2811 /* for raid0 takeover only one zone is supported */
2812 raid0_priv
= mddev
->private;
2813 if (raid0_priv
->nr_strip_zones
> 1) {
2814 printk(KERN_ERR
"md/raid10:%s: cannot takeover raid 0"
2815 " with more than one zone.\n",
2817 return ERR_PTR(-EINVAL
);
2819 return raid10_takeover_raid0(mddev
);
2821 return ERR_PTR(-EINVAL
);
2824 static struct mdk_personality raid10_personality
=
2828 .owner
= THIS_MODULE
,
2829 .make_request
= make_request
,
2833 .error_handler
= error
,
2834 .hot_add_disk
= raid10_add_disk
,
2835 .hot_remove_disk
= raid10_remove_disk
,
2836 .spare_active
= raid10_spare_active
,
2837 .sync_request
= sync_request
,
2838 .quiesce
= raid10_quiesce
,
2839 .size
= raid10_size
,
2840 .takeover
= raid10_takeover
,
2843 static int __init
raid_init(void)
2845 return register_md_personality(&raid10_personality
);
2848 static void raid_exit(void)
2850 unregister_md_personality(&raid10_personality
);
2853 module_init(raid_init
);
2854 module_exit(raid_exit
);
2855 MODULE_LICENSE("GPL");
2856 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2857 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2858 MODULE_ALIAS("md-raid10");
2859 MODULE_ALIAS("md-level-10");