2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "dm-bio-list.h"
22 #include <linux/delay.h>
23 #include <linux/raid/raid10.h>
24 #include <linux/raid/bitmap.h>
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
33 * far_offset (stored in bit 16 of layout )
35 * The data to be stored is divided into chunks using chunksize.
36 * Each device is divided into far_copies sections.
37 * In each section, chunks are laid out in a style similar to raid0, but
38 * near_copies copies of each chunk is stored (each on a different drive).
39 * The starting device for each section is offset near_copies from the starting
40 * device of the previous section.
41 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
43 * near_copies and far_copies must be at least one, and their product is at most
46 * If far_offset is true, then the far_copies are handled a bit differently.
47 * The copies are still in different stripes, but instead of be very far apart
48 * on disk, there are adjacent stripes.
52 * Number of guaranteed r10bios in case of extreme VM load:
54 #define NR_RAID10_BIOS 256
56 static void unplug_slaves(mddev_t
*mddev
);
58 static void allow_barrier(conf_t
*conf
);
59 static void lower_barrier(conf_t
*conf
);
61 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
65 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
67 /* allocate a r10bio with room for raid_disks entries in the bios array */
68 r10_bio
= kzalloc(size
, gfp_flags
);
70 unplug_slaves(conf
->mddev
);
75 static void r10bio_pool_free(void *r10_bio
, void *data
)
80 /* Maximum size of each resync request */
81 #define RESYNC_BLOCK_SIZE (64*1024)
82 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
83 /* amount of memory to reserve for resync requests */
84 #define RESYNC_WINDOW (1024*1024)
85 /* maximum number of concurrent requests, memory permitting */
86 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
89 * When performing a resync, we need to read and compare, so
90 * we need as many pages are there are copies.
91 * When performing a recovery, we need 2 bios, one for read,
92 * one for write (we recover only one drive per r10buf)
95 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
104 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
106 unplug_slaves(conf
->mddev
);
110 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
111 nalloc
= conf
->copies
; /* resync */
113 nalloc
= 2; /* recovery */
118 for (j
= nalloc
; j
-- ; ) {
119 bio
= bio_alloc(gfp_flags
, RESYNC_PAGES
);
122 r10_bio
->devs
[j
].bio
= bio
;
125 * Allocate RESYNC_PAGES data pages and attach them
128 for (j
= 0 ; j
< nalloc
; j
++) {
129 bio
= r10_bio
->devs
[j
].bio
;
130 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
131 page
= alloc_page(gfp_flags
);
135 bio
->bi_io_vec
[i
].bv_page
= page
;
143 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
145 for (i
= 0; i
< RESYNC_PAGES
; i
++)
146 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
149 while ( ++j
< nalloc
)
150 bio_put(r10_bio
->devs
[j
].bio
);
151 r10bio_pool_free(r10_bio
, conf
);
155 static void r10buf_pool_free(void *__r10_bio
, void *data
)
159 r10bio_t
*r10bio
= __r10_bio
;
162 for (j
=0; j
< conf
->copies
; j
++) {
163 struct bio
*bio
= r10bio
->devs
[j
].bio
;
165 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
166 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
167 bio
->bi_io_vec
[i
].bv_page
= NULL
;
172 r10bio_pool_free(r10bio
, conf
);
175 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
179 for (i
= 0; i
< conf
->copies
; i
++) {
180 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
181 if (*bio
&& *bio
!= IO_BLOCKED
)
187 static void free_r10bio(r10bio_t
*r10_bio
)
189 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
192 * Wake up any possible resync thread that waits for the device
197 put_all_bios(conf
, r10_bio
);
198 mempool_free(r10_bio
, conf
->r10bio_pool
);
201 static void put_buf(r10bio_t
*r10_bio
)
203 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
205 mempool_free(r10_bio
, conf
->r10buf_pool
);
210 static void reschedule_retry(r10bio_t
*r10_bio
)
213 mddev_t
*mddev
= r10_bio
->mddev
;
214 conf_t
*conf
= mddev_to_conf(mddev
);
216 spin_lock_irqsave(&conf
->device_lock
, flags
);
217 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
219 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
221 /* wake up frozen array... */
222 wake_up(&conf
->wait_barrier
);
224 md_wakeup_thread(mddev
->thread
);
228 * raid_end_bio_io() is called when we have finished servicing a mirrored
229 * operation and are ready to return a success/failure code to the buffer
232 static void raid_end_bio_io(r10bio_t
*r10_bio
)
234 struct bio
*bio
= r10_bio
->master_bio
;
237 test_bit(R10BIO_Uptodate
, &r10_bio
->state
) ? 0 : -EIO
);
238 free_r10bio(r10_bio
);
242 * Update disk head position estimator based on IRQ completion info.
244 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
246 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
248 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
249 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
252 static void raid10_end_read_request(struct bio
*bio
, int error
)
254 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
255 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
257 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
260 slot
= r10_bio
->read_slot
;
261 dev
= r10_bio
->devs
[slot
].devnum
;
263 * this branch is our 'one mirror IO has finished' event handler:
265 update_head_pos(slot
, r10_bio
);
269 * Set R10BIO_Uptodate in our master bio, so that
270 * we will return a good error code to the higher
271 * levels even if IO on some other mirrored buffer fails.
273 * The 'master' represents the composite IO operation to
274 * user-side. So if something waits for IO, then it will
275 * wait for the 'master' bio.
277 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
278 raid_end_bio_io(r10_bio
);
283 char b
[BDEVNAME_SIZE
];
284 if (printk_ratelimit())
285 printk(KERN_ERR
"raid10: %s: rescheduling sector %llu\n",
286 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
287 reschedule_retry(r10_bio
);
290 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
293 static void raid10_end_write_request(struct bio
*bio
, int error
)
295 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
296 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
298 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
300 for (slot
= 0; slot
< conf
->copies
; slot
++)
301 if (r10_bio
->devs
[slot
].bio
== bio
)
303 dev
= r10_bio
->devs
[slot
].devnum
;
306 * this branch is our 'one mirror IO has finished' event handler:
309 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
310 /* an I/O failed, we can't clear the bitmap */
311 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
314 * Set R10BIO_Uptodate in our master bio, so that
315 * we will return a good error code for to the higher
316 * levels even if IO on some other mirrored buffer fails.
318 * The 'master' represents the composite IO operation to
319 * user-side. So if something waits for IO, then it will
320 * wait for the 'master' bio.
322 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
324 update_head_pos(slot
, r10_bio
);
328 * Let's see if all mirrored write operations have finished
331 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
332 /* clear the bitmap if all writes complete successfully */
333 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
335 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
337 md_write_end(r10_bio
->mddev
);
338 raid_end_bio_io(r10_bio
);
341 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
346 * RAID10 layout manager
347 * Aswell as the chunksize and raid_disks count, there are two
348 * parameters: near_copies and far_copies.
349 * near_copies * far_copies must be <= raid_disks.
350 * Normally one of these will be 1.
351 * If both are 1, we get raid0.
352 * If near_copies == raid_disks, we get raid1.
354 * Chunks are layed out in raid0 style with near_copies copies of the
355 * first chunk, followed by near_copies copies of the next chunk and
357 * If far_copies > 1, then after 1/far_copies of the array has been assigned
358 * as described above, we start again with a device offset of near_copies.
359 * So we effectively have another copy of the whole array further down all
360 * the drives, but with blocks on different drives.
361 * With this layout, and block is never stored twice on the one device.
363 * raid10_find_phys finds the sector offset of a given virtual sector
364 * on each device that it is on.
366 * raid10_find_virt does the reverse mapping, from a device and a
367 * sector offset to a virtual address
370 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
380 /* now calculate first sector/dev */
381 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
382 sector
= r10bio
->sector
& conf
->chunk_mask
;
384 chunk
*= conf
->near_copies
;
386 dev
= sector_div(stripe
, conf
->raid_disks
);
387 if (conf
->far_offset
)
388 stripe
*= conf
->far_copies
;
390 sector
+= stripe
<< conf
->chunk_shift
;
392 /* and calculate all the others */
393 for (n
=0; n
< conf
->near_copies
; n
++) {
396 r10bio
->devs
[slot
].addr
= sector
;
397 r10bio
->devs
[slot
].devnum
= d
;
400 for (f
= 1; f
< conf
->far_copies
; f
++) {
401 d
+= conf
->near_copies
;
402 if (d
>= conf
->raid_disks
)
403 d
-= conf
->raid_disks
;
405 r10bio
->devs
[slot
].devnum
= d
;
406 r10bio
->devs
[slot
].addr
= s
;
410 if (dev
>= conf
->raid_disks
) {
412 sector
+= (conf
->chunk_mask
+ 1);
415 BUG_ON(slot
!= conf
->copies
);
418 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
420 sector_t offset
, chunk
, vchunk
;
422 offset
= sector
& conf
->chunk_mask
;
423 if (conf
->far_offset
) {
425 chunk
= sector
>> conf
->chunk_shift
;
426 fc
= sector_div(chunk
, conf
->far_copies
);
427 dev
-= fc
* conf
->near_copies
;
429 dev
+= conf
->raid_disks
;
431 while (sector
>= conf
->stride
) {
432 sector
-= conf
->stride
;
433 if (dev
< conf
->near_copies
)
434 dev
+= conf
->raid_disks
- conf
->near_copies
;
436 dev
-= conf
->near_copies
;
438 chunk
= sector
>> conf
->chunk_shift
;
440 vchunk
= chunk
* conf
->raid_disks
+ dev
;
441 sector_div(vchunk
, conf
->near_copies
);
442 return (vchunk
<< conf
->chunk_shift
) + offset
;
446 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
448 * @bvm: properties of new bio
449 * @biovec: the request that could be merged to it.
451 * Return amount of bytes we can accept at this offset
452 * If near_copies == raid_disk, there are no striping issues,
453 * but in that case, the function isn't called at all.
455 static int raid10_mergeable_bvec(struct request_queue
*q
,
456 struct bvec_merge_data
*bvm
,
457 struct bio_vec
*biovec
)
459 mddev_t
*mddev
= q
->queuedata
;
460 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
462 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
463 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
465 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
466 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
467 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
468 return biovec
->bv_len
;
474 * This routine returns the disk from which the requested read should
475 * be done. There is a per-array 'next expected sequential IO' sector
476 * number - if this matches on the next IO then we use the last disk.
477 * There is also a per-disk 'last know head position' sector that is
478 * maintained from IRQ contexts, both the normal and the resync IO
479 * completion handlers update this position correctly. If there is no
480 * perfect sequential match then we pick the disk whose head is closest.
482 * If there are 2 mirrors in the same 2 devices, performance degrades
483 * because position is mirror, not device based.
485 * The rdev for the device selected will have nr_pending incremented.
489 * FIXME: possibly should rethink readbalancing and do it differently
490 * depending on near_copies / far_copies geometry.
492 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
494 const unsigned long this_sector
= r10_bio
->sector
;
495 int disk
, slot
, nslot
;
496 const int sectors
= r10_bio
->sectors
;
497 sector_t new_distance
, current_distance
;
500 raid10_find_phys(conf
, r10_bio
);
503 * Check if we can balance. We can balance on the whole
504 * device if no resync is going on (recovery is ok), or below
505 * the resync window. We take the first readable disk when
506 * 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
||
515 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
516 !test_bit(In_sync
, &rdev
->flags
)) {
518 if (slot
== conf
->copies
) {
523 disk
= r10_bio
->devs
[slot
].devnum
;
529 /* make sure the disk is operational */
531 disk
= r10_bio
->devs
[slot
].devnum
;
532 while ((rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
533 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
534 !test_bit(In_sync
, &rdev
->flags
)) {
536 if (slot
== conf
->copies
) {
540 disk
= r10_bio
->devs
[slot
].devnum
;
544 current_distance
= abs(r10_bio
->devs
[slot
].addr
-
545 conf
->mirrors
[disk
].head_position
);
547 /* Find the disk whose head is closest,
548 * or - for far > 1 - find the closest to partition beginning */
550 for (nslot
= slot
; nslot
< conf
->copies
; nslot
++) {
551 int ndisk
= r10_bio
->devs
[nslot
].devnum
;
554 if ((rdev
=rcu_dereference(conf
->mirrors
[ndisk
].rdev
)) == NULL
||
555 r10_bio
->devs
[nslot
].bio
== IO_BLOCKED
||
556 !test_bit(In_sync
, &rdev
->flags
))
559 /* This optimisation is debatable, and completely destroys
560 * sequential read speed for 'far copies' arrays. So only
561 * keep it for 'near' arrays, and review those later.
563 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
)) {
569 /* for far > 1 always use the lowest address */
570 if (conf
->far_copies
> 1)
571 new_distance
= r10_bio
->devs
[nslot
].addr
;
573 new_distance
= abs(r10_bio
->devs
[nslot
].addr
-
574 conf
->mirrors
[ndisk
].head_position
);
575 if (new_distance
< current_distance
) {
576 current_distance
= new_distance
;
583 r10_bio
->read_slot
= slot
;
584 /* conf->next_seq_sect = this_sector + sectors;*/
586 if (disk
>= 0 && (rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
))!= NULL
)
587 atomic_inc(&conf
->mirrors
[disk
].rdev
->nr_pending
);
595 static void unplug_slaves(mddev_t
*mddev
)
597 conf_t
*conf
= mddev_to_conf(mddev
);
601 for (i
=0; i
<mddev
->raid_disks
; i
++) {
602 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
603 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
604 struct request_queue
*r_queue
= bdev_get_queue(rdev
->bdev
);
606 atomic_inc(&rdev
->nr_pending
);
611 rdev_dec_pending(rdev
, mddev
);
618 static void raid10_unplug(struct request_queue
*q
)
620 mddev_t
*mddev
= q
->queuedata
;
622 unplug_slaves(q
->queuedata
);
623 md_wakeup_thread(mddev
->thread
);
626 static int raid10_congested(void *data
, int bits
)
628 mddev_t
*mddev
= data
;
629 conf_t
*conf
= mddev_to_conf(mddev
);
633 for (i
= 0; i
< mddev
->raid_disks
&& ret
== 0; i
++) {
634 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
635 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
636 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
638 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
645 static int flush_pending_writes(conf_t
*conf
)
647 /* Any writes that have been queued but are awaiting
648 * bitmap updates get flushed here.
649 * We return 1 if any requests were actually submitted.
653 spin_lock_irq(&conf
->device_lock
);
655 if (conf
->pending_bio_list
.head
) {
657 bio
= bio_list_get(&conf
->pending_bio_list
);
658 blk_remove_plug(conf
->mddev
->queue
);
659 spin_unlock_irq(&conf
->device_lock
);
660 /* flush any pending bitmap writes to disk
661 * before proceeding w/ I/O */
662 bitmap_unplug(conf
->mddev
->bitmap
);
664 while (bio
) { /* submit pending writes */
665 struct bio
*next
= bio
->bi_next
;
667 generic_make_request(bio
);
672 spin_unlock_irq(&conf
->device_lock
);
676 * Sometimes we need to suspend IO while we do something else,
677 * either some resync/recovery, or reconfigure the array.
678 * To do this we raise a 'barrier'.
679 * The 'barrier' is a counter that can be raised multiple times
680 * to count how many activities are happening which preclude
682 * We can only raise the barrier if there is no pending IO.
683 * i.e. if nr_pending == 0.
684 * We choose only to raise the barrier if no-one is waiting for the
685 * barrier to go down. This means that as soon as an IO request
686 * is ready, no other operations which require a barrier will start
687 * until the IO request has had a chance.
689 * So: regular IO calls 'wait_barrier'. When that returns there
690 * is no backgroup IO happening, It must arrange to call
691 * allow_barrier when it has finished its IO.
692 * backgroup IO calls must call raise_barrier. Once that returns
693 * there is no normal IO happeing. It must arrange to call
694 * lower_barrier when the particular background IO completes.
697 static void raise_barrier(conf_t
*conf
, int force
)
699 BUG_ON(force
&& !conf
->barrier
);
700 spin_lock_irq(&conf
->resync_lock
);
702 /* Wait until no block IO is waiting (unless 'force') */
703 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
705 raid10_unplug(conf
->mddev
->queue
));
707 /* block any new IO from starting */
710 /* No wait for all pending IO to complete */
711 wait_event_lock_irq(conf
->wait_barrier
,
712 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
714 raid10_unplug(conf
->mddev
->queue
));
716 spin_unlock_irq(&conf
->resync_lock
);
719 static void lower_barrier(conf_t
*conf
)
722 spin_lock_irqsave(&conf
->resync_lock
, flags
);
724 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
725 wake_up(&conf
->wait_barrier
);
728 static void wait_barrier(conf_t
*conf
)
730 spin_lock_irq(&conf
->resync_lock
);
733 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
735 raid10_unplug(conf
->mddev
->queue
));
739 spin_unlock_irq(&conf
->resync_lock
);
742 static void allow_barrier(conf_t
*conf
)
745 spin_lock_irqsave(&conf
->resync_lock
, flags
);
747 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
748 wake_up(&conf
->wait_barrier
);
751 static void freeze_array(conf_t
*conf
)
753 /* stop syncio and normal IO and wait for everything to
755 * We increment barrier and nr_waiting, and then
756 * wait until nr_pending match nr_queued+1
757 * This is called in the context of one normal IO request
758 * that has failed. Thus any sync request that might be pending
759 * will be blocked by nr_pending, and we need to wait for
760 * pending IO requests to complete or be queued for re-try.
761 * Thus the number queued (nr_queued) plus this request (1)
762 * must match the number of pending IOs (nr_pending) before
765 spin_lock_irq(&conf
->resync_lock
);
768 wait_event_lock_irq(conf
->wait_barrier
,
769 conf
->nr_pending
== conf
->nr_queued
+1,
771 ({ flush_pending_writes(conf
);
772 raid10_unplug(conf
->mddev
->queue
); }));
773 spin_unlock_irq(&conf
->resync_lock
);
776 static void unfreeze_array(conf_t
*conf
)
778 /* reverse the effect of the freeze */
779 spin_lock_irq(&conf
->resync_lock
);
782 wake_up(&conf
->wait_barrier
);
783 spin_unlock_irq(&conf
->resync_lock
);
786 static int make_request(struct request_queue
*q
, struct bio
* bio
)
788 mddev_t
*mddev
= q
->queuedata
;
789 conf_t
*conf
= mddev_to_conf(mddev
);
790 mirror_info_t
*mirror
;
792 struct bio
*read_bio
;
795 int chunk_sects
= conf
->chunk_mask
+ 1;
796 const int rw
= bio_data_dir(bio
);
797 const int do_sync
= bio_sync(bio
);
800 mdk_rdev_t
*blocked_rdev
;
802 if (unlikely(bio_barrier(bio
))) {
803 bio_endio(bio
, -EOPNOTSUPP
);
807 /* If this request crosses a chunk boundary, we need to
808 * split it. This will only happen for 1 PAGE (or less) requests.
810 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
812 conf
->near_copies
< conf
->raid_disks
)) {
814 /* Sanity check -- queue functions should prevent this happening */
815 if (bio
->bi_vcnt
!= 1 ||
818 /* This is a one page bio that upper layers
819 * refuse to split for us, so we need to split it.
822 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
823 if (make_request(q
, &bp
->bio1
))
824 generic_make_request(&bp
->bio1
);
825 if (make_request(q
, &bp
->bio2
))
826 generic_make_request(&bp
->bio2
);
828 bio_pair_release(bp
);
831 printk("raid10_make_request bug: can't convert block across chunks"
832 " or bigger than %dk %llu %d\n", chunk_sects
/2,
833 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
839 md_write_start(mddev
, bio
);
842 * Register the new request and wait if the reconstruction
843 * thread has put up a bar for new requests.
844 * Continue immediately if no resync is active currently.
848 cpu
= part_stat_lock();
849 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
850 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
854 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
856 r10_bio
->master_bio
= bio
;
857 r10_bio
->sectors
= bio
->bi_size
>> 9;
859 r10_bio
->mddev
= mddev
;
860 r10_bio
->sector
= bio
->bi_sector
;
865 * read balancing logic:
867 int disk
= read_balance(conf
, r10_bio
);
868 int slot
= r10_bio
->read_slot
;
870 raid_end_bio_io(r10_bio
);
873 mirror
= conf
->mirrors
+ disk
;
875 read_bio
= bio_clone(bio
, GFP_NOIO
);
877 r10_bio
->devs
[slot
].bio
= read_bio
;
879 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
880 mirror
->rdev
->data_offset
;
881 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
882 read_bio
->bi_end_io
= raid10_end_read_request
;
883 read_bio
->bi_rw
= READ
| do_sync
;
884 read_bio
->bi_private
= r10_bio
;
886 generic_make_request(read_bio
);
893 /* first select target devices under rcu_lock and
894 * inc refcount on their rdev. Record them by setting
897 raid10_find_phys(conf
, r10_bio
);
901 for (i
= 0; i
< conf
->copies
; i
++) {
902 int d
= r10_bio
->devs
[i
].devnum
;
903 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
904 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
905 atomic_inc(&rdev
->nr_pending
);
909 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
910 atomic_inc(&rdev
->nr_pending
);
911 r10_bio
->devs
[i
].bio
= bio
;
913 r10_bio
->devs
[i
].bio
= NULL
;
914 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
919 if (unlikely(blocked_rdev
)) {
920 /* Have to wait for this device to get unblocked, then retry */
924 for (j
= 0; j
< i
; j
++)
925 if (r10_bio
->devs
[j
].bio
) {
926 d
= r10_bio
->devs
[j
].devnum
;
927 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
930 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
935 atomic_set(&r10_bio
->remaining
, 0);
938 for (i
= 0; i
< conf
->copies
; i
++) {
940 int d
= r10_bio
->devs
[i
].devnum
;
941 if (!r10_bio
->devs
[i
].bio
)
944 mbio
= bio_clone(bio
, GFP_NOIO
);
945 r10_bio
->devs
[i
].bio
= mbio
;
947 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
948 conf
->mirrors
[d
].rdev
->data_offset
;
949 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
950 mbio
->bi_end_io
= raid10_end_write_request
;
951 mbio
->bi_rw
= WRITE
| do_sync
;
952 mbio
->bi_private
= r10_bio
;
954 atomic_inc(&r10_bio
->remaining
);
955 bio_list_add(&bl
, mbio
);
958 if (unlikely(!atomic_read(&r10_bio
->remaining
))) {
959 /* the array is dead */
961 raid_end_bio_io(r10_bio
);
965 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
966 spin_lock_irqsave(&conf
->device_lock
, flags
);
967 bio_list_merge(&conf
->pending_bio_list
, &bl
);
968 blk_plug_device(mddev
->queue
);
969 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
971 /* In case raid10d snuck in to freeze_array */
972 wake_up(&conf
->wait_barrier
);
975 md_wakeup_thread(mddev
->thread
);
980 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
982 conf_t
*conf
= mddev_to_conf(mddev
);
985 if (conf
->near_copies
< conf
->raid_disks
)
986 seq_printf(seq
, " %dK chunks", mddev
->chunk_size
/1024);
987 if (conf
->near_copies
> 1)
988 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
989 if (conf
->far_copies
> 1) {
990 if (conf
->far_offset
)
991 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
993 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
995 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
996 conf
->raid_disks
- mddev
->degraded
);
997 for (i
= 0; i
< conf
->raid_disks
; i
++)
998 seq_printf(seq
, "%s",
999 conf
->mirrors
[i
].rdev
&&
1000 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
1001 seq_printf(seq
, "]");
1004 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1006 char b
[BDEVNAME_SIZE
];
1007 conf_t
*conf
= mddev_to_conf(mddev
);
1010 * If it is not operational, then we have already marked it as dead
1011 * else if it is the last working disks, ignore the error, let the
1012 * next level up know.
1013 * else mark the drive as failed
1015 if (test_bit(In_sync
, &rdev
->flags
)
1016 && conf
->raid_disks
-mddev
->degraded
== 1)
1018 * Don't fail the drive, just return an IO error.
1019 * The test should really be more sophisticated than
1020 * "working_disks == 1", but it isn't critical, and
1021 * can wait until we do more sophisticated "is the drive
1022 * really dead" tests...
1025 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1026 unsigned long flags
;
1027 spin_lock_irqsave(&conf
->device_lock
, flags
);
1029 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1031 * if recovery is running, make sure it aborts.
1033 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1035 set_bit(Faulty
, &rdev
->flags
);
1036 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1037 printk(KERN_ALERT
"raid10: Disk failure on %s, disabling device.\n"
1038 "raid10: Operation continuing on %d devices.\n",
1039 bdevname(rdev
->bdev
,b
), conf
->raid_disks
- mddev
->degraded
);
1042 static void print_conf(conf_t
*conf
)
1047 printk("RAID10 conf printout:\n");
1049 printk("(!conf)\n");
1052 printk(" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1055 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1056 char b
[BDEVNAME_SIZE
];
1057 tmp
= conf
->mirrors
+ i
;
1059 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
1060 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1061 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1062 bdevname(tmp
->rdev
->bdev
,b
));
1066 static void close_sync(conf_t
*conf
)
1069 allow_barrier(conf
);
1071 mempool_destroy(conf
->r10buf_pool
);
1072 conf
->r10buf_pool
= NULL
;
1075 /* check if there are enough drives for
1076 * every block to appear on atleast one
1078 static int enough(conf_t
*conf
)
1083 int n
= conf
->copies
;
1086 if (conf
->mirrors
[first
].rdev
)
1088 first
= (first
+1) % conf
->raid_disks
;
1092 } while (first
!= 0);
1096 static int raid10_spare_active(mddev_t
*mddev
)
1099 conf_t
*conf
= mddev
->private;
1103 * Find all non-in_sync disks within the RAID10 configuration
1104 * and mark them in_sync
1106 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1107 tmp
= conf
->mirrors
+ i
;
1109 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1110 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1111 unsigned long flags
;
1112 spin_lock_irqsave(&conf
->device_lock
, flags
);
1114 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1123 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1125 conf_t
*conf
= mddev
->private;
1130 int last
= mddev
->raid_disks
- 1;
1132 if (mddev
->recovery_cp
< MaxSector
)
1133 /* only hot-add to in-sync arrays, as recovery is
1134 * very different from resync
1140 if (rdev
->raid_disk
>= 0)
1141 first
= last
= rdev
->raid_disk
;
1143 if (rdev
->saved_raid_disk
>= 0 &&
1144 rdev
->saved_raid_disk
>= first
&&
1145 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1146 mirror
= rdev
->saved_raid_disk
;
1149 for ( ; mirror
<= last
; mirror
++)
1150 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1152 blk_queue_stack_limits(mddev
->queue
,
1153 rdev
->bdev
->bd_disk
->queue
);
1154 /* as we don't honour merge_bvec_fn, we must never risk
1155 * violating it, so limit ->max_sector to one PAGE, as
1156 * a one page request is never in violation.
1158 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
1159 mddev
->queue
->max_sectors
> (PAGE_SIZE
>>9))
1160 mddev
->queue
->max_sectors
= (PAGE_SIZE
>>9);
1162 p
->head_position
= 0;
1163 rdev
->raid_disk
= mirror
;
1165 if (rdev
->saved_raid_disk
!= mirror
)
1167 rcu_assign_pointer(p
->rdev
, rdev
);
1175 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1177 conf_t
*conf
= mddev
->private;
1180 mirror_info_t
*p
= conf
->mirrors
+ number
;
1185 if (test_bit(In_sync
, &rdev
->flags
) ||
1186 atomic_read(&rdev
->nr_pending
)) {
1190 /* Only remove faulty devices in recovery
1193 if (!test_bit(Faulty
, &rdev
->flags
) &&
1200 if (atomic_read(&rdev
->nr_pending
)) {
1201 /* lost the race, try later */
1213 static void end_sync_read(struct bio
*bio
, int error
)
1215 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
1216 conf_t
*conf
= mddev_to_conf(r10_bio
->mddev
);
1219 for (i
=0; i
<conf
->copies
; i
++)
1220 if (r10_bio
->devs
[i
].bio
== bio
)
1222 BUG_ON(i
== conf
->copies
);
1223 update_head_pos(i
, r10_bio
);
1224 d
= r10_bio
->devs
[i
].devnum
;
1226 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1227 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1229 atomic_add(r10_bio
->sectors
,
1230 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1231 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1232 md_error(r10_bio
->mddev
,
1233 conf
->mirrors
[d
].rdev
);
1236 /* for reconstruct, we always reschedule after a read.
1237 * for resync, only after all reads
1239 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1240 atomic_dec_and_test(&r10_bio
->remaining
)) {
1241 /* we have read all the blocks,
1242 * do the comparison in process context in raid10d
1244 reschedule_retry(r10_bio
);
1246 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1249 static void end_sync_write(struct bio
*bio
, int error
)
1251 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1252 r10bio_t
* r10_bio
= (r10bio_t
*)(bio
->bi_private
);
1253 mddev_t
*mddev
= r10_bio
->mddev
;
1254 conf_t
*conf
= mddev_to_conf(mddev
);
1257 for (i
= 0; i
< conf
->copies
; i
++)
1258 if (r10_bio
->devs
[i
].bio
== bio
)
1260 d
= r10_bio
->devs
[i
].devnum
;
1263 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1265 update_head_pos(i
, r10_bio
);
1267 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1268 if (r10_bio
->master_bio
== NULL
) {
1269 /* the primary of several recovery bios */
1270 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1274 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1279 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1283 * Note: sync and recover and handled very differently for raid10
1284 * This code is for resync.
1285 * For resync, we read through virtual addresses and read all blocks.
1286 * If there is any error, we schedule a write. The lowest numbered
1287 * drive is authoritative.
1288 * However requests come for physical address, so we need to map.
1289 * For every physical address there are raid_disks/copies virtual addresses,
1290 * which is always are least one, but is not necessarly an integer.
1291 * This means that a physical address can span multiple chunks, so we may
1292 * have to submit multiple io requests for a single sync request.
1295 * We check if all blocks are in-sync and only write to blocks that
1298 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1300 conf_t
*conf
= mddev_to_conf(mddev
);
1302 struct bio
*tbio
, *fbio
;
1304 atomic_set(&r10_bio
->remaining
, 1);
1306 /* find the first device with a block */
1307 for (i
=0; i
<conf
->copies
; i
++)
1308 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1311 if (i
== conf
->copies
)
1315 fbio
= r10_bio
->devs
[i
].bio
;
1317 /* now find blocks with errors */
1318 for (i
=0 ; i
< conf
->copies
; i
++) {
1320 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1322 tbio
= r10_bio
->devs
[i
].bio
;
1324 if (tbio
->bi_end_io
!= end_sync_read
)
1328 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1329 /* We know that the bi_io_vec layout is the same for
1330 * both 'first' and 'i', so we just compare them.
1331 * All vec entries are PAGE_SIZE;
1333 for (j
= 0; j
< vcnt
; j
++)
1334 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1335 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1340 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1342 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1343 /* Don't fix anything. */
1345 /* Ok, we need to write this bio
1346 * First we need to fixup bv_offset, bv_len and
1347 * bi_vecs, as the read request might have corrupted these
1349 tbio
->bi_vcnt
= vcnt
;
1350 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1352 tbio
->bi_phys_segments
= 0;
1353 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1354 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1355 tbio
->bi_next
= NULL
;
1356 tbio
->bi_rw
= WRITE
;
1357 tbio
->bi_private
= r10_bio
;
1358 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1360 for (j
=0; j
< vcnt
; j
++) {
1361 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1362 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1364 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1365 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1368 tbio
->bi_end_io
= end_sync_write
;
1370 d
= r10_bio
->devs
[i
].devnum
;
1371 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1372 atomic_inc(&r10_bio
->remaining
);
1373 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1375 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1376 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1377 generic_make_request(tbio
);
1381 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1382 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1388 * Now for the recovery code.
1389 * Recovery happens across physical sectors.
1390 * We recover all non-is_sync drives by finding the virtual address of
1391 * each, and then choose a working drive that also has that virt address.
1392 * There is a separate r10_bio for each non-in_sync drive.
1393 * Only the first two slots are in use. The first for reading,
1394 * The second for writing.
1398 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1400 conf_t
*conf
= mddev_to_conf(mddev
);
1402 struct bio
*bio
, *wbio
;
1405 /* move the pages across to the second bio
1406 * and submit the write request
1408 bio
= r10_bio
->devs
[0].bio
;
1409 wbio
= r10_bio
->devs
[1].bio
;
1410 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1411 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1412 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1413 wbio
->bi_io_vec
[i
].bv_page
= p
;
1415 d
= r10_bio
->devs
[1].devnum
;
1417 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1418 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1419 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1420 generic_make_request(wbio
);
1422 bio_endio(wbio
, -EIO
);
1427 * This is a kernel thread which:
1429 * 1. Retries failed read operations on working mirrors.
1430 * 2. Updates the raid superblock when problems encounter.
1431 * 3. Performs writes following reads for array synchronising.
1434 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1436 int sect
= 0; /* Offset from r10_bio->sector */
1437 int sectors
= r10_bio
->sectors
;
1441 int sl
= r10_bio
->read_slot
;
1445 if (s
> (PAGE_SIZE
>>9))
1450 int d
= r10_bio
->devs
[sl
].devnum
;
1451 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1453 test_bit(In_sync
, &rdev
->flags
)) {
1454 atomic_inc(&rdev
->nr_pending
);
1456 success
= sync_page_io(rdev
->bdev
,
1457 r10_bio
->devs
[sl
].addr
+
1458 sect
+ rdev
->data_offset
,
1460 conf
->tmppage
, READ
);
1461 rdev_dec_pending(rdev
, mddev
);
1467 if (sl
== conf
->copies
)
1469 } while (!success
&& sl
!= r10_bio
->read_slot
);
1473 /* Cannot read from anywhere -- bye bye array */
1474 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1475 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1480 /* write it back and re-read */
1482 while (sl
!= r10_bio
->read_slot
) {
1487 d
= r10_bio
->devs
[sl
].devnum
;
1488 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1490 test_bit(In_sync
, &rdev
->flags
)) {
1491 atomic_inc(&rdev
->nr_pending
);
1493 atomic_add(s
, &rdev
->corrected_errors
);
1494 if (sync_page_io(rdev
->bdev
,
1495 r10_bio
->devs
[sl
].addr
+
1496 sect
+ rdev
->data_offset
,
1497 s
<<9, conf
->tmppage
, WRITE
)
1499 /* Well, this device is dead */
1500 md_error(mddev
, rdev
);
1501 rdev_dec_pending(rdev
, mddev
);
1506 while (sl
!= r10_bio
->read_slot
) {
1511 d
= r10_bio
->devs
[sl
].devnum
;
1512 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1514 test_bit(In_sync
, &rdev
->flags
)) {
1515 char b
[BDEVNAME_SIZE
];
1516 atomic_inc(&rdev
->nr_pending
);
1518 if (sync_page_io(rdev
->bdev
,
1519 r10_bio
->devs
[sl
].addr
+
1520 sect
+ rdev
->data_offset
,
1521 s
<<9, conf
->tmppage
, READ
) == 0)
1522 /* Well, this device is dead */
1523 md_error(mddev
, rdev
);
1526 "raid10:%s: read error corrected"
1527 " (%d sectors at %llu on %s)\n",
1529 (unsigned long long)(sect
+
1531 bdevname(rdev
->bdev
, b
));
1533 rdev_dec_pending(rdev
, mddev
);
1544 static void raid10d(mddev_t
*mddev
)
1548 unsigned long flags
;
1549 conf_t
*conf
= mddev_to_conf(mddev
);
1550 struct list_head
*head
= &conf
->retry_list
;
1554 md_check_recovery(mddev
);
1557 char b
[BDEVNAME_SIZE
];
1559 unplug
+= flush_pending_writes(conf
);
1561 spin_lock_irqsave(&conf
->device_lock
, flags
);
1562 if (list_empty(head
)) {
1563 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1566 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1567 list_del(head
->prev
);
1569 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1571 mddev
= r10_bio
->mddev
;
1572 conf
= mddev_to_conf(mddev
);
1573 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
)) {
1574 sync_request_write(mddev
, r10_bio
);
1576 } else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
1577 recovery_request_write(mddev
, r10_bio
);
1581 /* we got a read error. Maybe the drive is bad. Maybe just
1582 * the block and we can fix it.
1583 * We freeze all other IO, and try reading the block from
1584 * other devices. When we find one, we re-write
1585 * and check it that fixes the read error.
1586 * This is all done synchronously while the array is
1589 if (mddev
->ro
== 0) {
1591 fix_read_error(conf
, mddev
, r10_bio
);
1592 unfreeze_array(conf
);
1595 bio
= r10_bio
->devs
[r10_bio
->read_slot
].bio
;
1596 r10_bio
->devs
[r10_bio
->read_slot
].bio
=
1597 mddev
->ro
? IO_BLOCKED
: NULL
;
1598 mirror
= read_balance(conf
, r10_bio
);
1600 printk(KERN_ALERT
"raid10: %s: unrecoverable I/O"
1601 " read error for block %llu\n",
1602 bdevname(bio
->bi_bdev
,b
),
1603 (unsigned long long)r10_bio
->sector
);
1604 raid_end_bio_io(r10_bio
);
1607 const int do_sync
= bio_sync(r10_bio
->master_bio
);
1609 rdev
= conf
->mirrors
[mirror
].rdev
;
1610 if (printk_ratelimit())
1611 printk(KERN_ERR
"raid10: %s: redirecting sector %llu to"
1612 " another mirror\n",
1613 bdevname(rdev
->bdev
,b
),
1614 (unsigned long long)r10_bio
->sector
);
1615 bio
= bio_clone(r10_bio
->master_bio
, GFP_NOIO
);
1616 r10_bio
->devs
[r10_bio
->read_slot
].bio
= bio
;
1617 bio
->bi_sector
= r10_bio
->devs
[r10_bio
->read_slot
].addr
1618 + rdev
->data_offset
;
1619 bio
->bi_bdev
= rdev
->bdev
;
1620 bio
->bi_rw
= READ
| do_sync
;
1621 bio
->bi_private
= r10_bio
;
1622 bio
->bi_end_io
= raid10_end_read_request
;
1624 generic_make_request(bio
);
1629 unplug_slaves(mddev
);
1633 static int init_resync(conf_t
*conf
)
1637 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1638 BUG_ON(conf
->r10buf_pool
);
1639 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1640 if (!conf
->r10buf_pool
)
1642 conf
->next_resync
= 0;
1647 * perform a "sync" on one "block"
1649 * We need to make sure that no normal I/O request - particularly write
1650 * requests - conflict with active sync requests.
1652 * This is achieved by tracking pending requests and a 'barrier' concept
1653 * that can be installed to exclude normal IO requests.
1655 * Resync and recovery are handled very differently.
1656 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1658 * For resync, we iterate over virtual addresses, read all copies,
1659 * and update if there are differences. If only one copy is live,
1661 * For recovery, we iterate over physical addresses, read a good
1662 * value for each non-in_sync drive, and over-write.
1664 * So, for recovery we may have several outstanding complex requests for a
1665 * given address, one for each out-of-sync device. We model this by allocating
1666 * a number of r10_bio structures, one for each out-of-sync device.
1667 * As we setup these structures, we collect all bio's together into a list
1668 * which we then process collectively to add pages, and then process again
1669 * to pass to generic_make_request.
1671 * The r10_bio structures are linked using a borrowed master_bio pointer.
1672 * This link is counted in ->remaining. When the r10_bio that points to NULL
1673 * has its remaining count decremented to 0, the whole complex operation
1678 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1680 conf_t
*conf
= mddev_to_conf(mddev
);
1682 struct bio
*biolist
= NULL
, *bio
;
1683 sector_t max_sector
, nr_sectors
;
1689 sector_t sectors_skipped
= 0;
1690 int chunks_skipped
= 0;
1692 if (!conf
->r10buf_pool
)
1693 if (init_resync(conf
))
1697 max_sector
= mddev
->size
<< 1;
1698 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1699 max_sector
= mddev
->resync_max_sectors
;
1700 if (sector_nr
>= max_sector
) {
1701 /* If we aborted, we need to abort the
1702 * sync on the 'current' bitmap chucks (there can
1703 * be several when recovering multiple devices).
1704 * as we may have started syncing it but not finished.
1705 * We can find the current address in
1706 * mddev->curr_resync, but for recovery,
1707 * we need to convert that to several
1708 * virtual addresses.
1710 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1711 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1712 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1714 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1716 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1717 bitmap_end_sync(mddev
->bitmap
, sect
,
1720 } else /* completed sync */
1723 bitmap_close_sync(mddev
->bitmap
);
1726 return sectors_skipped
;
1728 if (chunks_skipped
>= conf
->raid_disks
) {
1729 /* if there has been nothing to do on any drive,
1730 * then there is nothing to do at all..
1733 return (max_sector
- sector_nr
) + sectors_skipped
;
1736 if (max_sector
> mddev
->resync_max
)
1737 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
1739 /* make sure whole request will fit in a chunk - if chunks
1742 if (conf
->near_copies
< conf
->raid_disks
&&
1743 max_sector
> (sector_nr
| conf
->chunk_mask
))
1744 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1746 * If there is non-resync activity waiting for us then
1747 * put in a delay to throttle resync.
1749 if (!go_faster
&& conf
->nr_waiting
)
1750 msleep_interruptible(1000);
1752 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
1754 /* Again, very different code for resync and recovery.
1755 * Both must result in an r10bio with a list of bios that
1756 * have bi_end_io, bi_sector, bi_bdev set,
1757 * and bi_private set to the r10bio.
1758 * For recovery, we may actually create several r10bios
1759 * with 2 bios in each, that correspond to the bios in the main one.
1760 * In this case, the subordinate r10bios link back through a
1761 * borrowed master_bio pointer, and the counter in the master
1762 * includes a ref from each subordinate.
1764 /* First, we decide what to do and set ->bi_end_io
1765 * To end_sync_read if we want to read, and
1766 * end_sync_write if we will want to write.
1769 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
1770 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1771 /* recovery... the complicated one */
1775 for (i
=0 ; i
<conf
->raid_disks
; i
++)
1776 if (conf
->mirrors
[i
].rdev
&&
1777 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
)) {
1778 int still_degraded
= 0;
1779 /* want to reconstruct this device */
1780 r10bio_t
*rb2
= r10_bio
;
1781 sector_t sect
= raid10_find_virt(conf
, sector_nr
, i
);
1783 /* Unless we are doing a full sync, we only need
1784 * to recover the block if it is set in the bitmap
1786 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1788 if (sync_blocks
< max_sync
)
1789 max_sync
= sync_blocks
;
1792 /* yep, skip the sync_blocks here, but don't assume
1793 * that there will never be anything to do here
1795 chunks_skipped
= -1;
1799 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1800 raise_barrier(conf
, rb2
!= NULL
);
1801 atomic_set(&r10_bio
->remaining
, 0);
1803 r10_bio
->master_bio
= (struct bio
*)rb2
;
1805 atomic_inc(&rb2
->remaining
);
1806 r10_bio
->mddev
= mddev
;
1807 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1808 r10_bio
->sector
= sect
;
1810 raid10_find_phys(conf
, r10_bio
);
1811 /* Need to check if this section will still be
1814 for (j
=0; j
<conf
->copies
;j
++) {
1815 int d
= r10_bio
->devs
[j
].devnum
;
1816 if (conf
->mirrors
[d
].rdev
== NULL
||
1817 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
)) {
1822 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1823 &sync_blocks
, still_degraded
);
1825 for (j
=0; j
<conf
->copies
;j
++) {
1826 int d
= r10_bio
->devs
[j
].devnum
;
1827 if (conf
->mirrors
[d
].rdev
&&
1828 test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
)) {
1829 /* This is where we read from */
1830 bio
= r10_bio
->devs
[0].bio
;
1831 bio
->bi_next
= biolist
;
1833 bio
->bi_private
= r10_bio
;
1834 bio
->bi_end_io
= end_sync_read
;
1836 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1837 conf
->mirrors
[d
].rdev
->data_offset
;
1838 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1839 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1840 atomic_inc(&r10_bio
->remaining
);
1841 /* and we write to 'i' */
1843 for (k
=0; k
<conf
->copies
; k
++)
1844 if (r10_bio
->devs
[k
].devnum
== i
)
1846 BUG_ON(k
== conf
->copies
);
1847 bio
= r10_bio
->devs
[1].bio
;
1848 bio
->bi_next
= biolist
;
1850 bio
->bi_private
= r10_bio
;
1851 bio
->bi_end_io
= end_sync_write
;
1853 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1854 conf
->mirrors
[i
].rdev
->data_offset
;
1855 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1857 r10_bio
->devs
[0].devnum
= d
;
1858 r10_bio
->devs
[1].devnum
= i
;
1863 if (j
== conf
->copies
) {
1864 /* Cannot recover, so abort the recovery */
1867 atomic_dec(&rb2
->remaining
);
1869 if (!test_and_set_bit(MD_RECOVERY_INTR
,
1871 printk(KERN_INFO
"raid10: %s: insufficient working devices for recovery.\n",
1876 if (biolist
== NULL
) {
1878 r10bio_t
*rb2
= r10_bio
;
1879 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
1880 rb2
->master_bio
= NULL
;
1886 /* resync. Schedule a read for every block at this virt offset */
1889 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
1890 &sync_blocks
, mddev
->degraded
) &&
1891 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
1892 /* We can skip this block */
1894 return sync_blocks
+ sectors_skipped
;
1896 if (sync_blocks
< max_sync
)
1897 max_sync
= sync_blocks
;
1898 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1900 r10_bio
->mddev
= mddev
;
1901 atomic_set(&r10_bio
->remaining
, 0);
1902 raise_barrier(conf
, 0);
1903 conf
->next_resync
= sector_nr
;
1905 r10_bio
->master_bio
= NULL
;
1906 r10_bio
->sector
= sector_nr
;
1907 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
1908 raid10_find_phys(conf
, r10_bio
);
1909 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
1911 for (i
=0; i
<conf
->copies
; i
++) {
1912 int d
= r10_bio
->devs
[i
].devnum
;
1913 bio
= r10_bio
->devs
[i
].bio
;
1914 bio
->bi_end_io
= NULL
;
1915 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1916 if (conf
->mirrors
[d
].rdev
== NULL
||
1917 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
1919 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1920 atomic_inc(&r10_bio
->remaining
);
1921 bio
->bi_next
= biolist
;
1923 bio
->bi_private
= r10_bio
;
1924 bio
->bi_end_io
= end_sync_read
;
1926 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
1927 conf
->mirrors
[d
].rdev
->data_offset
;
1928 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1933 for (i
=0; i
<conf
->copies
; i
++) {
1934 int d
= r10_bio
->devs
[i
].devnum
;
1935 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
1936 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1944 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
1946 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1948 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
1951 bio
->bi_phys_segments
= 0;
1956 if (sector_nr
+ max_sync
< max_sector
)
1957 max_sector
= sector_nr
+ max_sync
;
1960 int len
= PAGE_SIZE
;
1962 if (sector_nr
+ (len
>>9) > max_sector
)
1963 len
= (max_sector
- sector_nr
) << 9;
1966 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
1967 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
1968 if (bio_add_page(bio
, page
, len
, 0) == 0) {
1971 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
1972 for (bio2
= biolist
; bio2
&& bio2
!= bio
; bio2
= bio2
->bi_next
) {
1973 /* remove last page from this bio */
1975 bio2
->bi_size
-= len
;
1976 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
1982 nr_sectors
+= len
>>9;
1983 sector_nr
+= len
>>9;
1984 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
1986 r10_bio
->sectors
= nr_sectors
;
1990 biolist
= biolist
->bi_next
;
1992 bio
->bi_next
= NULL
;
1993 r10_bio
= bio
->bi_private
;
1994 r10_bio
->sectors
= nr_sectors
;
1996 if (bio
->bi_end_io
== end_sync_read
) {
1997 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
1998 generic_make_request(bio
);
2002 if (sectors_skipped
)
2003 /* pretend they weren't skipped, it makes
2004 * no important difference in this case
2006 md_done_sync(mddev
, sectors_skipped
, 1);
2008 return sectors_skipped
+ nr_sectors
;
2010 /* There is nowhere to write, so all non-sync
2011 * drives must be failed, so try the next chunk...
2014 sector_t sec
= max_sector
- sector_nr
;
2015 sectors_skipped
+= sec
;
2017 sector_nr
= max_sector
;
2022 static int run(mddev_t
*mddev
)
2026 mirror_info_t
*disk
;
2028 struct list_head
*tmp
;
2030 sector_t stride
, size
;
2032 if (mddev
->chunk_size
< PAGE_SIZE
) {
2033 printk(KERN_ERR
"md/raid10: chunk size must be "
2034 "at least PAGE_SIZE(%ld).\n", PAGE_SIZE
);
2038 nc
= mddev
->layout
& 255;
2039 fc
= (mddev
->layout
>> 8) & 255;
2040 fo
= mddev
->layout
& (1<<16);
2041 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2042 (mddev
->layout
>> 17)) {
2043 printk(KERN_ERR
"raid10: %s: unsupported raid10 layout: 0x%8x\n",
2044 mdname(mddev
), mddev
->layout
);
2048 * copy the already verified devices into our private RAID10
2049 * bookkeeping area. [whatever we allocate in run(),
2050 * should be freed in stop()]
2052 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2053 mddev
->private = conf
;
2055 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2059 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2061 if (!conf
->mirrors
) {
2062 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2067 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2071 conf
->mddev
= mddev
;
2072 conf
->raid_disks
= mddev
->raid_disks
;
2073 conf
->near_copies
= nc
;
2074 conf
->far_copies
= fc
;
2075 conf
->copies
= nc
*fc
;
2076 conf
->far_offset
= fo
;
2077 conf
->chunk_mask
= (sector_t
)(mddev
->chunk_size
>>9)-1;
2078 conf
->chunk_shift
= ffz(~mddev
->chunk_size
) - 9;
2079 size
= mddev
->size
>> (conf
->chunk_shift
-1);
2080 sector_div(size
, fc
);
2081 size
= size
* conf
->raid_disks
;
2082 sector_div(size
, nc
);
2083 /* 'size' is now the number of chunks in the array */
2084 /* calculate "used chunks per device" in 'stride' */
2085 stride
= size
* conf
->copies
;
2087 /* We need to round up when dividing by raid_disks to
2088 * get the stride size.
2090 stride
+= conf
->raid_disks
- 1;
2091 sector_div(stride
, conf
->raid_disks
);
2092 mddev
->size
= stride
<< (conf
->chunk_shift
-1);
2097 sector_div(stride
, fc
);
2098 conf
->stride
= stride
<< conf
->chunk_shift
;
2100 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2101 r10bio_pool_free
, conf
);
2102 if (!conf
->r10bio_pool
) {
2103 printk(KERN_ERR
"raid10: couldn't allocate memory for %s\n",
2108 spin_lock_init(&conf
->device_lock
);
2109 mddev
->queue
->queue_lock
= &conf
->device_lock
;
2111 rdev_for_each(rdev
, tmp
, mddev
) {
2112 disk_idx
= rdev
->raid_disk
;
2113 if (disk_idx
>= mddev
->raid_disks
2116 disk
= conf
->mirrors
+ disk_idx
;
2120 blk_queue_stack_limits(mddev
->queue
,
2121 rdev
->bdev
->bd_disk
->queue
);
2122 /* as we don't honour merge_bvec_fn, we must never risk
2123 * violating it, so limit ->max_sector to one PAGE, as
2124 * a one page request is never in violation.
2126 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
2127 mddev
->queue
->max_sectors
> (PAGE_SIZE
>>9))
2128 mddev
->queue
->max_sectors
= (PAGE_SIZE
>>9);
2130 disk
->head_position
= 0;
2132 INIT_LIST_HEAD(&conf
->retry_list
);
2134 spin_lock_init(&conf
->resync_lock
);
2135 init_waitqueue_head(&conf
->wait_barrier
);
2137 /* need to check that every block has at least one working mirror */
2138 if (!enough(conf
)) {
2139 printk(KERN_ERR
"raid10: not enough operational mirrors for %s\n",
2144 mddev
->degraded
= 0;
2145 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2147 disk
= conf
->mirrors
+ i
;
2150 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2151 disk
->head_position
= 0;
2159 mddev
->thread
= md_register_thread(raid10d
, mddev
, "%s_raid10");
2160 if (!mddev
->thread
) {
2162 "raid10: couldn't allocate thread for %s\n",
2168 "raid10: raid set %s active with %d out of %d devices\n",
2169 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2172 * Ok, everything is just fine now
2174 mddev
->array_sectors
= size
<< conf
->chunk_shift
;
2175 mddev
->resync_max_sectors
= size
<< conf
->chunk_shift
;
2177 mddev
->queue
->unplug_fn
= raid10_unplug
;
2178 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2179 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2181 /* Calculate max read-ahead size.
2182 * We need to readahead at least twice a whole stripe....
2186 int stripe
= conf
->raid_disks
* (mddev
->chunk_size
/ PAGE_SIZE
);
2187 stripe
/= conf
->near_copies
;
2188 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2189 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2192 if (conf
->near_copies
< mddev
->raid_disks
)
2193 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2197 if (conf
->r10bio_pool
)
2198 mempool_destroy(conf
->r10bio_pool
);
2199 safe_put_page(conf
->tmppage
);
2200 kfree(conf
->mirrors
);
2202 mddev
->private = NULL
;
2207 static int stop(mddev_t
*mddev
)
2209 conf_t
*conf
= mddev_to_conf(mddev
);
2211 md_unregister_thread(mddev
->thread
);
2212 mddev
->thread
= NULL
;
2213 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2214 if (conf
->r10bio_pool
)
2215 mempool_destroy(conf
->r10bio_pool
);
2216 kfree(conf
->mirrors
);
2218 mddev
->private = NULL
;
2222 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2224 conf_t
*conf
= mddev_to_conf(mddev
);
2228 raise_barrier(conf
, 0);
2231 lower_barrier(conf
);
2234 if (mddev
->thread
) {
2236 mddev
->thread
->timeout
= mddev
->bitmap
->daemon_sleep
* HZ
;
2238 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
2239 md_wakeup_thread(mddev
->thread
);
2243 static struct mdk_personality raid10_personality
=
2247 .owner
= THIS_MODULE
,
2248 .make_request
= make_request
,
2252 .error_handler
= error
,
2253 .hot_add_disk
= raid10_add_disk
,
2254 .hot_remove_disk
= raid10_remove_disk
,
2255 .spare_active
= raid10_spare_active
,
2256 .sync_request
= sync_request
,
2257 .quiesce
= raid10_quiesce
,
2260 static int __init
raid_init(void)
2262 return register_md_personality(&raid10_personality
);
2265 static void raid_exit(void)
2267 unregister_md_personality(&raid10_personality
);
2270 module_init(raid_init
);
2271 module_exit(raid_exit
);
2272 MODULE_LICENSE("GPL");
2273 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2274 MODULE_ALIAS("md-raid10");
2275 MODULE_ALIAS("md-level-10");