2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/seq_file.h>
31 * RAID10 provides a combination of RAID0 and RAID1 functionality.
32 * The layout of data is defined by
35 * near_copies (stored in low byte of layout)
36 * far_copies (stored in second byte of layout)
37 * far_offset (stored in bit 16 of layout )
39 * The data to be stored is divided into chunks using chunksize.
40 * Each device is divided into far_copies sections.
41 * In each section, chunks are laid out in a style similar to raid0, but
42 * near_copies copies of each chunk is stored (each on a different drive).
43 * The starting device for each section is offset near_copies from the starting
44 * device of the previous section.
45 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
47 * near_copies and far_copies must be at least one, and their product is at most
50 * If far_offset is true, then the far_copies are handled a bit differently.
51 * The copies are still in different stripes, but instead of be very far apart
52 * on disk, there are adjacent stripes.
56 * Number of guaranteed r10bios in case of extreme VM load:
58 #define NR_RAID10_BIOS 256
60 static void unplug_slaves(mddev_t
*mddev
);
62 static void allow_barrier(conf_t
*conf
);
63 static void lower_barrier(conf_t
*conf
);
65 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
69 int size
= offsetof(struct r10bio_s
, devs
[conf
->copies
]);
71 /* allocate a r10bio with room for raid_disks entries in the bios array */
72 r10_bio
= kzalloc(size
, gfp_flags
);
73 if (!r10_bio
&& conf
->mddev
)
74 unplug_slaves(conf
->mddev
);
79 static void r10bio_pool_free(void *r10_bio
, void *data
)
84 /* Maximum size of each resync request */
85 #define RESYNC_BLOCK_SIZE (64*1024)
86 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
87 /* amount of memory to reserve for resync requests */
88 #define RESYNC_WINDOW (1024*1024)
89 /* maximum number of concurrent requests, memory permitting */
90 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
93 * When performing a resync, we need to read and compare, so
94 * we need as many pages are there are copies.
95 * When performing a recovery, we need 2 bios, one for read,
96 * one for write (we recover only one drive per r10buf)
99 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
108 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
110 unplug_slaves(conf
->mddev
);
114 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
115 nalloc
= conf
->copies
; /* resync */
117 nalloc
= 2; /* recovery */
122 for (j
= nalloc
; j
-- ; ) {
123 bio
= bio_alloc(gfp_flags
, RESYNC_PAGES
);
126 r10_bio
->devs
[j
].bio
= bio
;
129 * Allocate RESYNC_PAGES data pages and attach them
132 for (j
= 0 ; j
< nalloc
; j
++) {
133 bio
= r10_bio
->devs
[j
].bio
;
134 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
135 page
= alloc_page(gfp_flags
);
139 bio
->bi_io_vec
[i
].bv_page
= page
;
147 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
149 for (i
= 0; i
< RESYNC_PAGES
; i
++)
150 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
153 while ( ++j
< nalloc
)
154 bio_put(r10_bio
->devs
[j
].bio
);
155 r10bio_pool_free(r10_bio
, conf
);
159 static void r10buf_pool_free(void *__r10_bio
, void *data
)
163 r10bio_t
*r10bio
= __r10_bio
;
166 for (j
=0; j
< conf
->copies
; j
++) {
167 struct bio
*bio
= r10bio
->devs
[j
].bio
;
169 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
170 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
171 bio
->bi_io_vec
[i
].bv_page
= NULL
;
176 r10bio_pool_free(r10bio
, conf
);
179 static void put_all_bios(conf_t
*conf
, r10bio_t
*r10_bio
)
183 for (i
= 0; i
< conf
->copies
; i
++) {
184 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
185 if (*bio
&& *bio
!= IO_BLOCKED
)
191 static void free_r10bio(r10bio_t
*r10_bio
)
193 conf_t
*conf
= r10_bio
->mddev
->private;
196 * Wake up any possible resync thread that waits for the device
201 put_all_bios(conf
, r10_bio
);
202 mempool_free(r10_bio
, conf
->r10bio_pool
);
205 static void put_buf(r10bio_t
*r10_bio
)
207 conf_t
*conf
= r10_bio
->mddev
->private;
209 mempool_free(r10_bio
, conf
->r10buf_pool
);
214 static void reschedule_retry(r10bio_t
*r10_bio
)
217 mddev_t
*mddev
= r10_bio
->mddev
;
218 conf_t
*conf
= mddev
->private;
220 spin_lock_irqsave(&conf
->device_lock
, flags
);
221 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
223 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
225 /* wake up frozen array... */
226 wake_up(&conf
->wait_barrier
);
228 md_wakeup_thread(mddev
->thread
);
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
236 static void raid_end_bio_io(r10bio_t
*r10_bio
)
238 struct bio
*bio
= r10_bio
->master_bio
;
241 test_bit(R10BIO_Uptodate
, &r10_bio
->state
) ? 0 : -EIO
);
242 free_r10bio(r10_bio
);
246 * Update disk head position estimator based on IRQ completion info.
248 static inline void update_head_pos(int slot
, r10bio_t
*r10_bio
)
250 conf_t
*conf
= r10_bio
->mddev
->private;
252 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
253 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
256 static void raid10_end_read_request(struct bio
*bio
, int error
)
258 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
259 r10bio_t
*r10_bio
= bio
->bi_private
;
261 conf_t
*conf
= r10_bio
->mddev
->private;
264 slot
= r10_bio
->read_slot
;
265 dev
= r10_bio
->devs
[slot
].devnum
;
267 * this branch is our 'one mirror IO has finished' event handler:
269 update_head_pos(slot
, r10_bio
);
273 * Set R10BIO_Uptodate in our master bio, so that
274 * we will return a good error code to the higher
275 * levels even if IO on some other mirrored buffer fails.
277 * The 'master' represents the composite IO operation to
278 * user-side. So if something waits for IO, then it will
279 * wait for the 'master' bio.
281 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
282 raid_end_bio_io(r10_bio
);
287 char b
[BDEVNAME_SIZE
];
288 if (printk_ratelimit())
289 printk(KERN_ERR
"md/raid10:%s: %s: rescheduling sector %llu\n",
291 bdevname(conf
->mirrors
[dev
].rdev
->bdev
,b
), (unsigned long long)r10_bio
->sector
);
292 reschedule_retry(r10_bio
);
295 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
298 static void raid10_end_write_request(struct bio
*bio
, int error
)
300 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
301 r10bio_t
*r10_bio
= bio
->bi_private
;
303 conf_t
*conf
= r10_bio
->mddev
->private;
305 for (slot
= 0; slot
< conf
->copies
; slot
++)
306 if (r10_bio
->devs
[slot
].bio
== bio
)
308 dev
= r10_bio
->devs
[slot
].devnum
;
311 * this branch is our 'one mirror IO has finished' event handler:
314 md_error(r10_bio
->mddev
, conf
->mirrors
[dev
].rdev
);
315 /* an I/O failed, we can't clear the bitmap */
316 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
319 * Set R10BIO_Uptodate in our master bio, so that
320 * we will return a good error code for to the higher
321 * levels even if IO on some other mirrored buffer fails.
323 * The 'master' represents the composite IO operation to
324 * user-side. So if something waits for IO, then it will
325 * wait for the 'master' bio.
327 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
329 update_head_pos(slot
, r10_bio
);
333 * Let's see if all mirrored write operations have finished
336 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
337 /* clear the bitmap if all writes complete successfully */
338 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
340 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
342 md_write_end(r10_bio
->mddev
);
343 raid_end_bio_io(r10_bio
);
346 rdev_dec_pending(conf
->mirrors
[dev
].rdev
, conf
->mddev
);
351 * RAID10 layout manager
352 * Aswell as the chunksize and raid_disks count, there are two
353 * parameters: near_copies and far_copies.
354 * near_copies * far_copies must be <= raid_disks.
355 * Normally one of these will be 1.
356 * If both are 1, we get raid0.
357 * If near_copies == raid_disks, we get raid1.
359 * Chunks are layed out in raid0 style with near_copies copies of the
360 * first chunk, followed by near_copies copies of the next chunk and
362 * If far_copies > 1, then after 1/far_copies of the array has been assigned
363 * as described above, we start again with a device offset of near_copies.
364 * So we effectively have another copy of the whole array further down all
365 * the drives, but with blocks on different drives.
366 * With this layout, and block is never stored twice on the one device.
368 * raid10_find_phys finds the sector offset of a given virtual sector
369 * on each device that it is on.
371 * raid10_find_virt does the reverse mapping, from a device and a
372 * sector offset to a virtual address
375 static void raid10_find_phys(conf_t
*conf
, r10bio_t
*r10bio
)
385 /* now calculate first sector/dev */
386 chunk
= r10bio
->sector
>> conf
->chunk_shift
;
387 sector
= r10bio
->sector
& conf
->chunk_mask
;
389 chunk
*= conf
->near_copies
;
391 dev
= sector_div(stripe
, conf
->raid_disks
);
392 if (conf
->far_offset
)
393 stripe
*= conf
->far_copies
;
395 sector
+= stripe
<< conf
->chunk_shift
;
397 /* and calculate all the others */
398 for (n
=0; n
< conf
->near_copies
; n
++) {
401 r10bio
->devs
[slot
].addr
= sector
;
402 r10bio
->devs
[slot
].devnum
= d
;
405 for (f
= 1; f
< conf
->far_copies
; f
++) {
406 d
+= conf
->near_copies
;
407 if (d
>= conf
->raid_disks
)
408 d
-= conf
->raid_disks
;
410 r10bio
->devs
[slot
].devnum
= d
;
411 r10bio
->devs
[slot
].addr
= s
;
415 if (dev
>= conf
->raid_disks
) {
417 sector
+= (conf
->chunk_mask
+ 1);
420 BUG_ON(slot
!= conf
->copies
);
423 static sector_t
raid10_find_virt(conf_t
*conf
, sector_t sector
, int dev
)
425 sector_t offset
, chunk
, vchunk
;
427 offset
= sector
& conf
->chunk_mask
;
428 if (conf
->far_offset
) {
430 chunk
= sector
>> conf
->chunk_shift
;
431 fc
= sector_div(chunk
, conf
->far_copies
);
432 dev
-= fc
* conf
->near_copies
;
434 dev
+= conf
->raid_disks
;
436 while (sector
>= conf
->stride
) {
437 sector
-= conf
->stride
;
438 if (dev
< conf
->near_copies
)
439 dev
+= conf
->raid_disks
- conf
->near_copies
;
441 dev
-= conf
->near_copies
;
443 chunk
= sector
>> conf
->chunk_shift
;
445 vchunk
= chunk
* conf
->raid_disks
+ dev
;
446 sector_div(vchunk
, conf
->near_copies
);
447 return (vchunk
<< conf
->chunk_shift
) + offset
;
451 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
453 * @bvm: properties of new bio
454 * @biovec: the request that could be merged to it.
456 * Return amount of bytes we can accept at this offset
457 * If near_copies == raid_disk, there are no striping issues,
458 * but in that case, the function isn't called at all.
460 static int raid10_mergeable_bvec(struct request_queue
*q
,
461 struct bvec_merge_data
*bvm
,
462 struct bio_vec
*biovec
)
464 mddev_t
*mddev
= q
->queuedata
;
465 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
467 unsigned int chunk_sectors
= mddev
->chunk_sectors
;
468 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
470 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
471 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
472 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
473 return biovec
->bv_len
;
479 * This routine returns the disk from which the requested read should
480 * be done. There is a per-array 'next expected sequential IO' sector
481 * number - if this matches on the next IO then we use the last disk.
482 * There is also a per-disk 'last know head position' sector that is
483 * maintained from IRQ contexts, both the normal and the resync IO
484 * completion handlers update this position correctly. If there is no
485 * perfect sequential match then we pick the disk whose head is closest.
487 * If there are 2 mirrors in the same 2 devices, performance degrades
488 * because position is mirror, not device based.
490 * The rdev for the device selected will have nr_pending incremented.
494 * FIXME: possibly should rethink readbalancing and do it differently
495 * depending on near_copies / far_copies geometry.
497 static int read_balance(conf_t
*conf
, r10bio_t
*r10_bio
)
499 const sector_t this_sector
= r10_bio
->sector
;
500 int disk
, slot
, nslot
;
501 const int sectors
= r10_bio
->sectors
;
502 sector_t new_distance
, current_distance
;
505 raid10_find_phys(conf
, r10_bio
);
508 * Check if we can balance. We can balance on the whole
509 * device if no resync is going on (recovery is ok), or below
510 * the resync window. We take the first readable disk when
511 * above the resync window.
513 if (conf
->mddev
->recovery_cp
< MaxSector
514 && (this_sector
+ sectors
>= conf
->next_resync
)) {
515 /* make sure that disk is operational */
517 disk
= r10_bio
->devs
[slot
].devnum
;
519 while ((rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
520 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
521 !test_bit(In_sync
, &rdev
->flags
)) {
523 if (slot
== conf
->copies
) {
528 disk
= r10_bio
->devs
[slot
].devnum
;
534 /* make sure the disk is operational */
536 disk
= r10_bio
->devs
[slot
].devnum
;
537 while ((rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
)) == NULL
||
538 r10_bio
->devs
[slot
].bio
== IO_BLOCKED
||
539 !test_bit(In_sync
, &rdev
->flags
)) {
541 if (slot
== conf
->copies
) {
545 disk
= r10_bio
->devs
[slot
].devnum
;
549 current_distance
= abs(r10_bio
->devs
[slot
].addr
-
550 conf
->mirrors
[disk
].head_position
);
552 /* Find the disk whose head is closest,
553 * or - for far > 1 - find the closest to partition beginning */
555 for (nslot
= slot
; nslot
< conf
->copies
; nslot
++) {
556 int ndisk
= r10_bio
->devs
[nslot
].devnum
;
559 if ((rdev
=rcu_dereference(conf
->mirrors
[ndisk
].rdev
)) == NULL
||
560 r10_bio
->devs
[nslot
].bio
== IO_BLOCKED
||
561 !test_bit(In_sync
, &rdev
->flags
))
564 /* This optimisation is debatable, and completely destroys
565 * sequential read speed for 'far copies' arrays. So only
566 * keep it for 'near' arrays, and review those later.
568 if (conf
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
)) {
574 /* for far > 1 always use the lowest address */
575 if (conf
->far_copies
> 1)
576 new_distance
= r10_bio
->devs
[nslot
].addr
;
578 new_distance
= abs(r10_bio
->devs
[nslot
].addr
-
579 conf
->mirrors
[ndisk
].head_position
);
580 if (new_distance
< current_distance
) {
581 current_distance
= new_distance
;
588 r10_bio
->read_slot
= slot
;
589 /* conf->next_seq_sect = this_sector + sectors;*/
591 if (disk
>= 0 && (rdev
=rcu_dereference(conf
->mirrors
[disk
].rdev
))!= NULL
)
592 atomic_inc(&conf
->mirrors
[disk
].rdev
->nr_pending
);
600 static void unplug_slaves(mddev_t
*mddev
)
602 conf_t
*conf
= mddev
->private;
606 for (i
=0; i
< conf
->raid_disks
; i
++) {
607 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
608 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
609 struct request_queue
*r_queue
= bdev_get_queue(rdev
->bdev
);
611 atomic_inc(&rdev
->nr_pending
);
616 rdev_dec_pending(rdev
, mddev
);
623 static void raid10_unplug(struct request_queue
*q
)
625 mddev_t
*mddev
= q
->queuedata
;
627 unplug_slaves(q
->queuedata
);
628 md_wakeup_thread(mddev
->thread
);
631 static int raid10_congested(void *data
, int bits
)
633 mddev_t
*mddev
= data
;
634 conf_t
*conf
= mddev
->private;
637 if (mddev_congested(mddev
, bits
))
640 for (i
= 0; i
< conf
->raid_disks
&& ret
== 0; i
++) {
641 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
642 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
643 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
645 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
652 static int flush_pending_writes(conf_t
*conf
)
654 /* Any writes that have been queued but are awaiting
655 * bitmap updates get flushed here.
656 * We return 1 if any requests were actually submitted.
660 spin_lock_irq(&conf
->device_lock
);
662 if (conf
->pending_bio_list
.head
) {
664 bio
= bio_list_get(&conf
->pending_bio_list
);
665 blk_remove_plug(conf
->mddev
->queue
);
666 spin_unlock_irq(&conf
->device_lock
);
667 /* flush any pending bitmap writes to disk
668 * before proceeding w/ I/O */
669 bitmap_unplug(conf
->mddev
->bitmap
);
671 while (bio
) { /* submit pending writes */
672 struct bio
*next
= bio
->bi_next
;
674 generic_make_request(bio
);
679 spin_unlock_irq(&conf
->device_lock
);
683 * Sometimes we need to suspend IO while we do something else,
684 * either some resync/recovery, or reconfigure the array.
685 * To do this we raise a 'barrier'.
686 * The 'barrier' is a counter that can be raised multiple times
687 * to count how many activities are happening which preclude
689 * We can only raise the barrier if there is no pending IO.
690 * i.e. if nr_pending == 0.
691 * We choose only to raise the barrier if no-one is waiting for the
692 * barrier to go down. This means that as soon as an IO request
693 * is ready, no other operations which require a barrier will start
694 * until the IO request has had a chance.
696 * So: regular IO calls 'wait_barrier'. When that returns there
697 * is no backgroup IO happening, It must arrange to call
698 * allow_barrier when it has finished its IO.
699 * backgroup IO calls must call raise_barrier. Once that returns
700 * there is no normal IO happeing. It must arrange to call
701 * lower_barrier when the particular background IO completes.
704 static void raise_barrier(conf_t
*conf
, int force
)
706 BUG_ON(force
&& !conf
->barrier
);
707 spin_lock_irq(&conf
->resync_lock
);
709 /* Wait until no block IO is waiting (unless 'force') */
710 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
712 raid10_unplug(conf
->mddev
->queue
));
714 /* block any new IO from starting */
717 /* No wait for all pending IO to complete */
718 wait_event_lock_irq(conf
->wait_barrier
,
719 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
721 raid10_unplug(conf
->mddev
->queue
));
723 spin_unlock_irq(&conf
->resync_lock
);
726 static void lower_barrier(conf_t
*conf
)
729 spin_lock_irqsave(&conf
->resync_lock
, flags
);
731 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
732 wake_up(&conf
->wait_barrier
);
735 static void wait_barrier(conf_t
*conf
)
737 spin_lock_irq(&conf
->resync_lock
);
740 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
742 raid10_unplug(conf
->mddev
->queue
));
746 spin_unlock_irq(&conf
->resync_lock
);
749 static void allow_barrier(conf_t
*conf
)
752 spin_lock_irqsave(&conf
->resync_lock
, flags
);
754 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
755 wake_up(&conf
->wait_barrier
);
758 static void freeze_array(conf_t
*conf
)
760 /* stop syncio and normal IO and wait for everything to
762 * We increment barrier and nr_waiting, and then
763 * wait until nr_pending match nr_queued+1
764 * This is called in the context of one normal IO request
765 * that has failed. Thus any sync request that might be pending
766 * will be blocked by nr_pending, and we need to wait for
767 * pending IO requests to complete or be queued for re-try.
768 * Thus the number queued (nr_queued) plus this request (1)
769 * must match the number of pending IOs (nr_pending) before
772 spin_lock_irq(&conf
->resync_lock
);
775 wait_event_lock_irq(conf
->wait_barrier
,
776 conf
->nr_pending
== conf
->nr_queued
+1,
778 ({ flush_pending_writes(conf
);
779 raid10_unplug(conf
->mddev
->queue
); }));
780 spin_unlock_irq(&conf
->resync_lock
);
783 static void unfreeze_array(conf_t
*conf
)
785 /* reverse the effect of the freeze */
786 spin_lock_irq(&conf
->resync_lock
);
789 wake_up(&conf
->wait_barrier
);
790 spin_unlock_irq(&conf
->resync_lock
);
793 static int make_request(mddev_t
*mddev
, struct bio
* bio
)
795 conf_t
*conf
= mddev
->private;
796 mirror_info_t
*mirror
;
798 struct bio
*read_bio
;
800 int chunk_sects
= conf
->chunk_mask
+ 1;
801 const int rw
= bio_data_dir(bio
);
802 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
803 const unsigned long do_fua
= (bio
->bi_rw
& REQ_FUA
);
806 mdk_rdev_t
*blocked_rdev
;
808 if (unlikely(bio
->bi_rw
& REQ_FLUSH
)) {
809 md_flush_request(mddev
, bio
);
813 /* If this request crosses a chunk boundary, we need to
814 * split it. This will only happen for 1 PAGE (or less) requests.
816 if (unlikely( (bio
->bi_sector
& conf
->chunk_mask
) + (bio
->bi_size
>> 9)
818 conf
->near_copies
< conf
->raid_disks
)) {
820 /* Sanity check -- queue functions should prevent this happening */
821 if (bio
->bi_vcnt
!= 1 ||
824 /* This is a one page bio that upper layers
825 * refuse to split for us, so we need to split it.
828 chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)) );
830 /* Each of these 'make_request' calls will call 'wait_barrier'.
831 * If the first succeeds but the second blocks due to the resync
832 * thread raising the barrier, we will deadlock because the
833 * IO to the underlying device will be queued in generic_make_request
834 * and will never complete, so will never reduce nr_pending.
835 * So increment nr_waiting here so no new raise_barriers will
836 * succeed, and so the second wait_barrier cannot block.
838 spin_lock_irq(&conf
->resync_lock
);
840 spin_unlock_irq(&conf
->resync_lock
);
842 if (make_request(mddev
, &bp
->bio1
))
843 generic_make_request(&bp
->bio1
);
844 if (make_request(mddev
, &bp
->bio2
))
845 generic_make_request(&bp
->bio2
);
847 spin_lock_irq(&conf
->resync_lock
);
849 wake_up(&conf
->wait_barrier
);
850 spin_unlock_irq(&conf
->resync_lock
);
852 bio_pair_release(bp
);
855 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
856 " or bigger than %dk %llu %d\n", mdname(mddev
), chunk_sects
/2,
857 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
863 md_write_start(mddev
, bio
);
866 * Register the new request and wait if the reconstruction
867 * thread has put up a bar for new requests.
868 * Continue immediately if no resync is active currently.
872 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
874 r10_bio
->master_bio
= bio
;
875 r10_bio
->sectors
= bio
->bi_size
>> 9;
877 r10_bio
->mddev
= mddev
;
878 r10_bio
->sector
= bio
->bi_sector
;
883 * read balancing logic:
885 int disk
= read_balance(conf
, r10_bio
);
886 int slot
= r10_bio
->read_slot
;
888 raid_end_bio_io(r10_bio
);
891 mirror
= conf
->mirrors
+ disk
;
893 read_bio
= bio_clone(bio
, GFP_NOIO
);
895 r10_bio
->devs
[slot
].bio
= read_bio
;
897 read_bio
->bi_sector
= r10_bio
->devs
[slot
].addr
+
898 mirror
->rdev
->data_offset
;
899 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
900 read_bio
->bi_end_io
= raid10_end_read_request
;
901 read_bio
->bi_rw
= READ
| do_sync
;
902 read_bio
->bi_private
= r10_bio
;
904 generic_make_request(read_bio
);
911 /* first select target devices under rcu_lock and
912 * inc refcount on their rdev. Record them by setting
915 raid10_find_phys(conf
, r10_bio
);
919 for (i
= 0; i
< conf
->copies
; i
++) {
920 int d
= r10_bio
->devs
[i
].devnum
;
921 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
922 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
923 atomic_inc(&rdev
->nr_pending
);
927 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
928 atomic_inc(&rdev
->nr_pending
);
929 r10_bio
->devs
[i
].bio
= bio
;
931 r10_bio
->devs
[i
].bio
= NULL
;
932 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
937 if (unlikely(blocked_rdev
)) {
938 /* Have to wait for this device to get unblocked, then retry */
942 for (j
= 0; j
< i
; j
++)
943 if (r10_bio
->devs
[j
].bio
) {
944 d
= r10_bio
->devs
[j
].devnum
;
945 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
948 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
953 atomic_set(&r10_bio
->remaining
, 0);
956 for (i
= 0; i
< conf
->copies
; i
++) {
958 int d
= r10_bio
->devs
[i
].devnum
;
959 if (!r10_bio
->devs
[i
].bio
)
962 mbio
= bio_clone(bio
, GFP_NOIO
);
963 r10_bio
->devs
[i
].bio
= mbio
;
965 mbio
->bi_sector
= r10_bio
->devs
[i
].addr
+
966 conf
->mirrors
[d
].rdev
->data_offset
;
967 mbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
968 mbio
->bi_end_io
= raid10_end_write_request
;
969 mbio
->bi_rw
= WRITE
| do_sync
| do_fua
;
970 mbio
->bi_private
= r10_bio
;
972 atomic_inc(&r10_bio
->remaining
);
973 bio_list_add(&bl
, mbio
);
976 if (unlikely(!atomic_read(&r10_bio
->remaining
))) {
977 /* the array is dead */
979 raid_end_bio_io(r10_bio
);
983 bitmap_startwrite(mddev
->bitmap
, bio
->bi_sector
, r10_bio
->sectors
, 0);
984 spin_lock_irqsave(&conf
->device_lock
, flags
);
985 bio_list_merge(&conf
->pending_bio_list
, &bl
);
986 blk_plug_device(mddev
->queue
);
987 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
989 /* In case raid10d snuck in to freeze_array */
990 wake_up(&conf
->wait_barrier
);
993 md_wakeup_thread(mddev
->thread
);
998 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
1000 conf_t
*conf
= mddev
->private;
1003 if (conf
->near_copies
< conf
->raid_disks
)
1004 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1005 if (conf
->near_copies
> 1)
1006 seq_printf(seq
, " %d near-copies", conf
->near_copies
);
1007 if (conf
->far_copies
> 1) {
1008 if (conf
->far_offset
)
1009 seq_printf(seq
, " %d offset-copies", conf
->far_copies
);
1011 seq_printf(seq
, " %d far-copies", conf
->far_copies
);
1013 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1014 conf
->raid_disks
- mddev
->degraded
);
1015 for (i
= 0; i
< conf
->raid_disks
; i
++)
1016 seq_printf(seq
, "%s",
1017 conf
->mirrors
[i
].rdev
&&
1018 test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ? "U" : "_");
1019 seq_printf(seq
, "]");
1022 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1024 char b
[BDEVNAME_SIZE
];
1025 conf_t
*conf
= mddev
->private;
1028 * If it is not operational, then we have already marked it as dead
1029 * else if it is the last working disks, ignore the error, let the
1030 * next level up know.
1031 * else mark the drive as failed
1033 if (test_bit(In_sync
, &rdev
->flags
)
1034 && conf
->raid_disks
-mddev
->degraded
== 1)
1036 * Don't fail the drive, just return an IO error.
1037 * The test should really be more sophisticated than
1038 * "working_disks == 1", but it isn't critical, and
1039 * can wait until we do more sophisticated "is the drive
1040 * really dead" tests...
1043 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1044 unsigned long flags
;
1045 spin_lock_irqsave(&conf
->device_lock
, flags
);
1047 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1049 * if recovery is running, make sure it aborts.
1051 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1053 set_bit(Faulty
, &rdev
->flags
);
1054 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1055 printk(KERN_ALERT
"md/raid10:%s: Disk failure on %s, disabling device.\n"
1056 KERN_ALERT
"md/raid10:%s: Operation continuing on %d devices.\n",
1057 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1058 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1061 static void print_conf(conf_t
*conf
)
1066 printk(KERN_DEBUG
"RAID10 conf printout:\n");
1068 printk(KERN_DEBUG
"(!conf)\n");
1071 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1074 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1075 char b
[BDEVNAME_SIZE
];
1076 tmp
= conf
->mirrors
+ i
;
1078 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1079 i
, !test_bit(In_sync
, &tmp
->rdev
->flags
),
1080 !test_bit(Faulty
, &tmp
->rdev
->flags
),
1081 bdevname(tmp
->rdev
->bdev
,b
));
1085 static void close_sync(conf_t
*conf
)
1088 allow_barrier(conf
);
1090 mempool_destroy(conf
->r10buf_pool
);
1091 conf
->r10buf_pool
= NULL
;
1094 /* check if there are enough drives for
1095 * every block to appear on atleast one
1097 static int enough(conf_t
*conf
)
1102 int n
= conf
->copies
;
1105 if (conf
->mirrors
[first
].rdev
)
1107 first
= (first
+1) % conf
->raid_disks
;
1111 } while (first
!= 0);
1115 static int raid10_spare_active(mddev_t
*mddev
)
1118 conf_t
*conf
= mddev
->private;
1121 unsigned long flags
;
1124 * Find all non-in_sync disks within the RAID10 configuration
1125 * and mark them in_sync
1127 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1128 tmp
= conf
->mirrors
+ i
;
1130 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1131 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1133 sysfs_notify_dirent(tmp
->rdev
->sysfs_state
);
1136 spin_lock_irqsave(&conf
->device_lock
, flags
);
1137 mddev
->degraded
-= count
;
1138 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1145 static int raid10_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1147 conf_t
*conf
= mddev
->private;
1152 int last
= conf
->raid_disks
- 1;
1154 if (mddev
->recovery_cp
< MaxSector
)
1155 /* only hot-add to in-sync arrays, as recovery is
1156 * very different from resync
1162 if (rdev
->raid_disk
>= 0)
1163 first
= last
= rdev
->raid_disk
;
1165 if (rdev
->saved_raid_disk
>= 0 &&
1166 rdev
->saved_raid_disk
>= first
&&
1167 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1168 mirror
= rdev
->saved_raid_disk
;
1171 for ( ; mirror
<= last
; mirror
++)
1172 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1174 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1175 rdev
->data_offset
<< 9);
1176 /* as we don't honour merge_bvec_fn, we must
1177 * never risk violating it, so limit
1178 * ->max_segments to one lying with a single
1179 * page, as a one page request is never in
1182 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1183 blk_queue_max_segments(mddev
->queue
, 1);
1184 blk_queue_segment_boundary(mddev
->queue
,
1185 PAGE_CACHE_SIZE
- 1);
1188 p
->head_position
= 0;
1189 rdev
->raid_disk
= mirror
;
1191 if (rdev
->saved_raid_disk
!= mirror
)
1193 rcu_assign_pointer(p
->rdev
, rdev
);
1197 md_integrity_add_rdev(rdev
, mddev
);
1202 static int raid10_remove_disk(mddev_t
*mddev
, int number
)
1204 conf_t
*conf
= mddev
->private;
1207 mirror_info_t
*p
= conf
->mirrors
+ number
;
1212 if (test_bit(In_sync
, &rdev
->flags
) ||
1213 atomic_read(&rdev
->nr_pending
)) {
1217 /* Only remove faulty devices in recovery
1220 if (!test_bit(Faulty
, &rdev
->flags
) &&
1227 if (atomic_read(&rdev
->nr_pending
)) {
1228 /* lost the race, try later */
1233 md_integrity_register(mddev
);
1242 static void end_sync_read(struct bio
*bio
, int error
)
1244 r10bio_t
*r10_bio
= bio
->bi_private
;
1245 conf_t
*conf
= r10_bio
->mddev
->private;
1248 for (i
=0; i
<conf
->copies
; i
++)
1249 if (r10_bio
->devs
[i
].bio
== bio
)
1251 BUG_ON(i
== conf
->copies
);
1252 update_head_pos(i
, r10_bio
);
1253 d
= r10_bio
->devs
[i
].devnum
;
1255 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1256 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1258 atomic_add(r10_bio
->sectors
,
1259 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1260 if (!test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
))
1261 md_error(r10_bio
->mddev
,
1262 conf
->mirrors
[d
].rdev
);
1265 /* for reconstruct, we always reschedule after a read.
1266 * for resync, only after all reads
1268 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1269 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1270 atomic_dec_and_test(&r10_bio
->remaining
)) {
1271 /* we have read all the blocks,
1272 * do the comparison in process context in raid10d
1274 reschedule_retry(r10_bio
);
1278 static void end_sync_write(struct bio
*bio
, int error
)
1280 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1281 r10bio_t
*r10_bio
= bio
->bi_private
;
1282 mddev_t
*mddev
= r10_bio
->mddev
;
1283 conf_t
*conf
= mddev
->private;
1286 for (i
= 0; i
< conf
->copies
; i
++)
1287 if (r10_bio
->devs
[i
].bio
== bio
)
1289 d
= r10_bio
->devs
[i
].devnum
;
1292 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1294 update_head_pos(i
, r10_bio
);
1296 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1297 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1298 if (r10_bio
->master_bio
== NULL
) {
1299 /* the primary of several recovery bios */
1300 sector_t s
= r10_bio
->sectors
;
1302 md_done_sync(mddev
, s
, 1);
1305 r10bio_t
*r10_bio2
= (r10bio_t
*)r10_bio
->master_bio
;
1313 * Note: sync and recover and handled very differently for raid10
1314 * This code is for resync.
1315 * For resync, we read through virtual addresses and read all blocks.
1316 * If there is any error, we schedule a write. The lowest numbered
1317 * drive is authoritative.
1318 * However requests come for physical address, so we need to map.
1319 * For every physical address there are raid_disks/copies virtual addresses,
1320 * which is always are least one, but is not necessarly an integer.
1321 * This means that a physical address can span multiple chunks, so we may
1322 * have to submit multiple io requests for a single sync request.
1325 * We check if all blocks are in-sync and only write to blocks that
1328 static void sync_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1330 conf_t
*conf
= mddev
->private;
1332 struct bio
*tbio
, *fbio
;
1334 atomic_set(&r10_bio
->remaining
, 1);
1336 /* find the first device with a block */
1337 for (i
=0; i
<conf
->copies
; i
++)
1338 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
))
1341 if (i
== conf
->copies
)
1345 fbio
= r10_bio
->devs
[i
].bio
;
1347 /* now find blocks with errors */
1348 for (i
=0 ; i
< conf
->copies
; i
++) {
1350 int vcnt
= r10_bio
->sectors
>> (PAGE_SHIFT
-9);
1352 tbio
= r10_bio
->devs
[i
].bio
;
1354 if (tbio
->bi_end_io
!= end_sync_read
)
1358 if (test_bit(BIO_UPTODATE
, &r10_bio
->devs
[i
].bio
->bi_flags
)) {
1359 /* We know that the bi_io_vec layout is the same for
1360 * both 'first' and 'i', so we just compare them.
1361 * All vec entries are PAGE_SIZE;
1363 for (j
= 0; j
< vcnt
; j
++)
1364 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
1365 page_address(tbio
->bi_io_vec
[j
].bv_page
),
1370 mddev
->resync_mismatches
+= r10_bio
->sectors
;
1372 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1373 /* Don't fix anything. */
1375 /* Ok, we need to write this bio
1376 * First we need to fixup bv_offset, bv_len and
1377 * bi_vecs, as the read request might have corrupted these
1379 tbio
->bi_vcnt
= vcnt
;
1380 tbio
->bi_size
= r10_bio
->sectors
<< 9;
1382 tbio
->bi_phys_segments
= 0;
1383 tbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1384 tbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1385 tbio
->bi_next
= NULL
;
1386 tbio
->bi_rw
= WRITE
;
1387 tbio
->bi_private
= r10_bio
;
1388 tbio
->bi_sector
= r10_bio
->devs
[i
].addr
;
1390 for (j
=0; j
< vcnt
; j
++) {
1391 tbio
->bi_io_vec
[j
].bv_offset
= 0;
1392 tbio
->bi_io_vec
[j
].bv_len
= PAGE_SIZE
;
1394 memcpy(page_address(tbio
->bi_io_vec
[j
].bv_page
),
1395 page_address(fbio
->bi_io_vec
[j
].bv_page
),
1398 tbio
->bi_end_io
= end_sync_write
;
1400 d
= r10_bio
->devs
[i
].devnum
;
1401 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1402 atomic_inc(&r10_bio
->remaining
);
1403 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, tbio
->bi_size
>> 9);
1405 tbio
->bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
1406 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1407 generic_make_request(tbio
);
1411 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
1412 md_done_sync(mddev
, r10_bio
->sectors
, 1);
1418 * Now for the recovery code.
1419 * Recovery happens across physical sectors.
1420 * We recover all non-is_sync drives by finding the virtual address of
1421 * each, and then choose a working drive that also has that virt address.
1422 * There is a separate r10_bio for each non-in_sync drive.
1423 * Only the first two slots are in use. The first for reading,
1424 * The second for writing.
1428 static void recovery_request_write(mddev_t
*mddev
, r10bio_t
*r10_bio
)
1430 conf_t
*conf
= mddev
->private;
1432 struct bio
*bio
, *wbio
;
1435 /* move the pages across to the second bio
1436 * and submit the write request
1438 bio
= r10_bio
->devs
[0].bio
;
1439 wbio
= r10_bio
->devs
[1].bio
;
1440 for (i
=0; i
< wbio
->bi_vcnt
; i
++) {
1441 struct page
*p
= bio
->bi_io_vec
[i
].bv_page
;
1442 bio
->bi_io_vec
[i
].bv_page
= wbio
->bi_io_vec
[i
].bv_page
;
1443 wbio
->bi_io_vec
[i
].bv_page
= p
;
1445 d
= r10_bio
->devs
[1].devnum
;
1447 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1448 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, wbio
->bi_size
>> 9);
1449 if (test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1450 generic_make_request(wbio
);
1452 bio_endio(wbio
, -EIO
);
1457 * Used by fix_read_error() to decay the per rdev read_errors.
1458 * We halve the read error count for every hour that has elapsed
1459 * since the last recorded read error.
1462 static void check_decay_read_errors(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1464 struct timespec cur_time_mon
;
1465 unsigned long hours_since_last
;
1466 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
1468 ktime_get_ts(&cur_time_mon
);
1470 if (rdev
->last_read_error
.tv_sec
== 0 &&
1471 rdev
->last_read_error
.tv_nsec
== 0) {
1472 /* first time we've seen a read error */
1473 rdev
->last_read_error
= cur_time_mon
;
1477 hours_since_last
= (cur_time_mon
.tv_sec
-
1478 rdev
->last_read_error
.tv_sec
) / 3600;
1480 rdev
->last_read_error
= cur_time_mon
;
1483 * if hours_since_last is > the number of bits in read_errors
1484 * just set read errors to 0. We do this to avoid
1485 * overflowing the shift of read_errors by hours_since_last.
1487 if (hours_since_last
>= 8 * sizeof(read_errors
))
1488 atomic_set(&rdev
->read_errors
, 0);
1490 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
1494 * This is a kernel thread which:
1496 * 1. Retries failed read operations on working mirrors.
1497 * 2. Updates the raid superblock when problems encounter.
1498 * 3. Performs writes following reads for array synchronising.
1501 static void fix_read_error(conf_t
*conf
, mddev_t
*mddev
, r10bio_t
*r10_bio
)
1503 int sect
= 0; /* Offset from r10_bio->sector */
1504 int sectors
= r10_bio
->sectors
;
1506 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
1507 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1510 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1511 if (rdev
) { /* If rdev is not NULL */
1512 char b
[BDEVNAME_SIZE
];
1513 int cur_read_error_count
= 0;
1515 bdevname(rdev
->bdev
, b
);
1517 if (test_bit(Faulty
, &rdev
->flags
)) {
1519 /* drive has already been failed, just ignore any
1520 more fix_read_error() attempts */
1524 check_decay_read_errors(mddev
, rdev
);
1525 atomic_inc(&rdev
->read_errors
);
1526 cur_read_error_count
= atomic_read(&rdev
->read_errors
);
1527 if (cur_read_error_count
> max_read_errors
) {
1530 "md/raid10:%s: %s: Raid device exceeded "
1531 "read_error threshold "
1532 "[cur %d:max %d]\n",
1534 b
, cur_read_error_count
, max_read_errors
);
1536 "md/raid10:%s: %s: Failing raid "
1537 "device\n", mdname(mddev
), b
);
1538 md_error(mddev
, conf
->mirrors
[d
].rdev
);
1546 int sl
= r10_bio
->read_slot
;
1550 if (s
> (PAGE_SIZE
>>9))
1555 d
= r10_bio
->devs
[sl
].devnum
;
1556 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1558 test_bit(In_sync
, &rdev
->flags
)) {
1559 atomic_inc(&rdev
->nr_pending
);
1561 success
= sync_page_io(rdev
->bdev
,
1562 r10_bio
->devs
[sl
].addr
+
1563 sect
+ rdev
->data_offset
,
1565 conf
->tmppage
, READ
);
1566 rdev_dec_pending(rdev
, mddev
);
1572 if (sl
== conf
->copies
)
1574 } while (!success
&& sl
!= r10_bio
->read_slot
);
1578 /* Cannot read from anywhere -- bye bye array */
1579 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
1580 md_error(mddev
, conf
->mirrors
[dn
].rdev
);
1585 /* write it back and re-read */
1587 while (sl
!= r10_bio
->read_slot
) {
1588 char b
[BDEVNAME_SIZE
];
1593 d
= r10_bio
->devs
[sl
].devnum
;
1594 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1596 test_bit(In_sync
, &rdev
->flags
)) {
1597 atomic_inc(&rdev
->nr_pending
);
1599 atomic_add(s
, &rdev
->corrected_errors
);
1600 if (sync_page_io(rdev
->bdev
,
1601 r10_bio
->devs
[sl
].addr
+
1602 sect
+ rdev
->data_offset
,
1603 s
<<9, conf
->tmppage
, WRITE
)
1605 /* Well, this device is dead */
1607 "md/raid10:%s: read correction "
1609 " (%d sectors at %llu on %s)\n",
1611 (unsigned long long)(sect
+
1613 bdevname(rdev
->bdev
, b
));
1614 printk(KERN_NOTICE
"md/raid10:%s: %s: failing "
1617 bdevname(rdev
->bdev
, b
));
1618 md_error(mddev
, rdev
);
1620 rdev_dec_pending(rdev
, mddev
);
1625 while (sl
!= r10_bio
->read_slot
) {
1630 d
= r10_bio
->devs
[sl
].devnum
;
1631 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1633 test_bit(In_sync
, &rdev
->flags
)) {
1634 char b
[BDEVNAME_SIZE
];
1635 atomic_inc(&rdev
->nr_pending
);
1637 if (sync_page_io(rdev
->bdev
,
1638 r10_bio
->devs
[sl
].addr
+
1639 sect
+ rdev
->data_offset
,
1640 s
<<9, conf
->tmppage
,
1642 /* Well, this device is dead */
1644 "md/raid10:%s: unable to read back "
1646 " (%d sectors at %llu on %s)\n",
1648 (unsigned long long)(sect
+
1650 bdevname(rdev
->bdev
, b
));
1651 printk(KERN_NOTICE
"md/raid10:%s: %s: failing drive\n",
1653 bdevname(rdev
->bdev
, b
));
1655 md_error(mddev
, rdev
);
1658 "md/raid10:%s: read error corrected"
1659 " (%d sectors at %llu on %s)\n",
1661 (unsigned long long)(sect
+
1663 bdevname(rdev
->bdev
, b
));
1666 rdev_dec_pending(rdev
, mddev
);
1677 static void raid10d(mddev_t
*mddev
)
1681 unsigned long flags
;
1682 conf_t
*conf
= mddev
->private;
1683 struct list_head
*head
= &conf
->retry_list
;
1687 md_check_recovery(mddev
);
1690 char b
[BDEVNAME_SIZE
];
1692 unplug
+= flush_pending_writes(conf
);
1694 spin_lock_irqsave(&conf
->device_lock
, flags
);
1695 if (list_empty(head
)) {
1696 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1699 r10_bio
= list_entry(head
->prev
, r10bio_t
, retry_list
);
1700 list_del(head
->prev
);
1702 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1704 mddev
= r10_bio
->mddev
;
1705 conf
= mddev
->private;
1706 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
)) {
1707 sync_request_write(mddev
, r10_bio
);
1709 } else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
1710 recovery_request_write(mddev
, r10_bio
);
1714 /* we got a read error. Maybe the drive is bad. Maybe just
1715 * the block and we can fix it.
1716 * We freeze all other IO, and try reading the block from
1717 * other devices. When we find one, we re-write
1718 * and check it that fixes the read error.
1719 * This is all done synchronously while the array is
1722 if (mddev
->ro
== 0) {
1724 fix_read_error(conf
, mddev
, r10_bio
);
1725 unfreeze_array(conf
);
1728 bio
= r10_bio
->devs
[r10_bio
->read_slot
].bio
;
1729 r10_bio
->devs
[r10_bio
->read_slot
].bio
=
1730 mddev
->ro
? IO_BLOCKED
: NULL
;
1731 mirror
= read_balance(conf
, r10_bio
);
1733 printk(KERN_ALERT
"md/raid10:%s: %s: unrecoverable I/O"
1734 " read error for block %llu\n",
1736 bdevname(bio
->bi_bdev
,b
),
1737 (unsigned long long)r10_bio
->sector
);
1738 raid_end_bio_io(r10_bio
);
1741 const unsigned long do_sync
= (r10_bio
->master_bio
->bi_rw
& REQ_SYNC
);
1743 rdev
= conf
->mirrors
[mirror
].rdev
;
1744 if (printk_ratelimit())
1745 printk(KERN_ERR
"md/raid10:%s: %s: redirecting sector %llu to"
1746 " another mirror\n",
1748 bdevname(rdev
->bdev
,b
),
1749 (unsigned long long)r10_bio
->sector
);
1750 bio
= bio_clone(r10_bio
->master_bio
, GFP_NOIO
);
1751 r10_bio
->devs
[r10_bio
->read_slot
].bio
= bio
;
1752 bio
->bi_sector
= r10_bio
->devs
[r10_bio
->read_slot
].addr
1753 + rdev
->data_offset
;
1754 bio
->bi_bdev
= rdev
->bdev
;
1755 bio
->bi_rw
= READ
| do_sync
;
1756 bio
->bi_private
= r10_bio
;
1757 bio
->bi_end_io
= raid10_end_read_request
;
1759 generic_make_request(bio
);
1765 unplug_slaves(mddev
);
1769 static int init_resync(conf_t
*conf
)
1773 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1774 BUG_ON(conf
->r10buf_pool
);
1775 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
1776 if (!conf
->r10buf_pool
)
1778 conf
->next_resync
= 0;
1783 * perform a "sync" on one "block"
1785 * We need to make sure that no normal I/O request - particularly write
1786 * requests - conflict with active sync requests.
1788 * This is achieved by tracking pending requests and a 'barrier' concept
1789 * that can be installed to exclude normal IO requests.
1791 * Resync and recovery are handled very differently.
1792 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1794 * For resync, we iterate over virtual addresses, read all copies,
1795 * and update if there are differences. If only one copy is live,
1797 * For recovery, we iterate over physical addresses, read a good
1798 * value for each non-in_sync drive, and over-write.
1800 * So, for recovery we may have several outstanding complex requests for a
1801 * given address, one for each out-of-sync device. We model this by allocating
1802 * a number of r10_bio structures, one for each out-of-sync device.
1803 * As we setup these structures, we collect all bio's together into a list
1804 * which we then process collectively to add pages, and then process again
1805 * to pass to generic_make_request.
1807 * The r10_bio structures are linked using a borrowed master_bio pointer.
1808 * This link is counted in ->remaining. When the r10_bio that points to NULL
1809 * has its remaining count decremented to 0, the whole complex operation
1814 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1816 conf_t
*conf
= mddev
->private;
1818 struct bio
*biolist
= NULL
, *bio
;
1819 sector_t max_sector
, nr_sectors
;
1823 sector_t sync_blocks
;
1825 sector_t sectors_skipped
= 0;
1826 int chunks_skipped
= 0;
1828 if (!conf
->r10buf_pool
)
1829 if (init_resync(conf
))
1833 max_sector
= mddev
->dev_sectors
;
1834 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1835 max_sector
= mddev
->resync_max_sectors
;
1836 if (sector_nr
>= max_sector
) {
1837 /* If we aborted, we need to abort the
1838 * sync on the 'current' bitmap chucks (there can
1839 * be several when recovering multiple devices).
1840 * as we may have started syncing it but not finished.
1841 * We can find the current address in
1842 * mddev->curr_resync, but for recovery,
1843 * we need to convert that to several
1844 * virtual addresses.
1846 if (mddev
->curr_resync
< max_sector
) { /* aborted */
1847 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
1848 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1850 else for (i
=0; i
<conf
->raid_disks
; i
++) {
1852 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
1853 bitmap_end_sync(mddev
->bitmap
, sect
,
1856 } else /* completed sync */
1859 bitmap_close_sync(mddev
->bitmap
);
1862 return sectors_skipped
;
1864 if (chunks_skipped
>= conf
->raid_disks
) {
1865 /* if there has been nothing to do on any drive,
1866 * then there is nothing to do at all..
1869 return (max_sector
- sector_nr
) + sectors_skipped
;
1872 if (max_sector
> mddev
->resync_max
)
1873 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
1875 /* make sure whole request will fit in a chunk - if chunks
1878 if (conf
->near_copies
< conf
->raid_disks
&&
1879 max_sector
> (sector_nr
| conf
->chunk_mask
))
1880 max_sector
= (sector_nr
| conf
->chunk_mask
) + 1;
1882 * If there is non-resync activity waiting for us then
1883 * put in a delay to throttle resync.
1885 if (!go_faster
&& conf
->nr_waiting
)
1886 msleep_interruptible(1000);
1888 /* Again, very different code for resync and recovery.
1889 * Both must result in an r10bio with a list of bios that
1890 * have bi_end_io, bi_sector, bi_bdev set,
1891 * and bi_private set to the r10bio.
1892 * For recovery, we may actually create several r10bios
1893 * with 2 bios in each, that correspond to the bios in the main one.
1894 * In this case, the subordinate r10bios link back through a
1895 * borrowed master_bio pointer, and the counter in the master
1896 * includes a ref from each subordinate.
1898 /* First, we decide what to do and set ->bi_end_io
1899 * To end_sync_read if we want to read, and
1900 * end_sync_write if we will want to write.
1903 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
1904 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1905 /* recovery... the complicated one */
1909 for (i
=0 ; i
<conf
->raid_disks
; i
++)
1910 if (conf
->mirrors
[i
].rdev
&&
1911 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
)) {
1912 int still_degraded
= 0;
1913 /* want to reconstruct this device */
1914 r10bio_t
*rb2
= r10_bio
;
1915 sector_t sect
= raid10_find_virt(conf
, sector_nr
, i
);
1917 /* Unless we are doing a full sync, we only need
1918 * to recover the block if it is set in the bitmap
1920 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1922 if (sync_blocks
< max_sync
)
1923 max_sync
= sync_blocks
;
1926 /* yep, skip the sync_blocks here, but don't assume
1927 * that there will never be anything to do here
1929 chunks_skipped
= -1;
1933 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
1934 raise_barrier(conf
, rb2
!= NULL
);
1935 atomic_set(&r10_bio
->remaining
, 0);
1937 r10_bio
->master_bio
= (struct bio
*)rb2
;
1939 atomic_inc(&rb2
->remaining
);
1940 r10_bio
->mddev
= mddev
;
1941 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
1942 r10_bio
->sector
= sect
;
1944 raid10_find_phys(conf
, r10_bio
);
1946 /* Need to check if the array will still be
1949 for (j
=0; j
<conf
->raid_disks
; j
++)
1950 if (conf
->mirrors
[j
].rdev
== NULL
||
1951 test_bit(Faulty
, &conf
->mirrors
[j
].rdev
->flags
)) {
1956 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
1957 &sync_blocks
, still_degraded
);
1959 for (j
=0; j
<conf
->copies
;j
++) {
1960 int d
= r10_bio
->devs
[j
].devnum
;
1961 if (conf
->mirrors
[d
].rdev
&&
1962 test_bit(In_sync
, &conf
->mirrors
[d
].rdev
->flags
)) {
1963 /* This is where we read from */
1964 bio
= r10_bio
->devs
[0].bio
;
1965 bio
->bi_next
= biolist
;
1967 bio
->bi_private
= r10_bio
;
1968 bio
->bi_end_io
= end_sync_read
;
1970 bio
->bi_sector
= r10_bio
->devs
[j
].addr
+
1971 conf
->mirrors
[d
].rdev
->data_offset
;
1972 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
1973 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
1974 atomic_inc(&r10_bio
->remaining
);
1975 /* and we write to 'i' */
1977 for (k
=0; k
<conf
->copies
; k
++)
1978 if (r10_bio
->devs
[k
].devnum
== i
)
1980 BUG_ON(k
== conf
->copies
);
1981 bio
= r10_bio
->devs
[1].bio
;
1982 bio
->bi_next
= biolist
;
1984 bio
->bi_private
= r10_bio
;
1985 bio
->bi_end_io
= end_sync_write
;
1987 bio
->bi_sector
= r10_bio
->devs
[k
].addr
+
1988 conf
->mirrors
[i
].rdev
->data_offset
;
1989 bio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1991 r10_bio
->devs
[0].devnum
= d
;
1992 r10_bio
->devs
[1].devnum
= i
;
1997 if (j
== conf
->copies
) {
1998 /* Cannot recover, so abort the recovery */
2001 atomic_dec(&rb2
->remaining
);
2003 if (!test_and_set_bit(MD_RECOVERY_INTR
,
2005 printk(KERN_INFO
"md/raid10:%s: insufficient "
2006 "working devices for recovery.\n",
2011 if (biolist
== NULL
) {
2013 r10bio_t
*rb2
= r10_bio
;
2014 r10_bio
= (r10bio_t
*) rb2
->master_bio
;
2015 rb2
->master_bio
= NULL
;
2021 /* resync. Schedule a read for every block at this virt offset */
2024 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
2026 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2027 &sync_blocks
, mddev
->degraded
) &&
2028 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2029 /* We can skip this block */
2031 return sync_blocks
+ sectors_skipped
;
2033 if (sync_blocks
< max_sync
)
2034 max_sync
= sync_blocks
;
2035 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2037 r10_bio
->mddev
= mddev
;
2038 atomic_set(&r10_bio
->remaining
, 0);
2039 raise_barrier(conf
, 0);
2040 conf
->next_resync
= sector_nr
;
2042 r10_bio
->master_bio
= NULL
;
2043 r10_bio
->sector
= sector_nr
;
2044 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
2045 raid10_find_phys(conf
, r10_bio
);
2046 r10_bio
->sectors
= (sector_nr
| conf
->chunk_mask
) - sector_nr
+1;
2048 for (i
=0; i
<conf
->copies
; i
++) {
2049 int d
= r10_bio
->devs
[i
].devnum
;
2050 bio
= r10_bio
->devs
[i
].bio
;
2051 bio
->bi_end_io
= NULL
;
2052 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2053 if (conf
->mirrors
[d
].rdev
== NULL
||
2054 test_bit(Faulty
, &conf
->mirrors
[d
].rdev
->flags
))
2056 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2057 atomic_inc(&r10_bio
->remaining
);
2058 bio
->bi_next
= biolist
;
2060 bio
->bi_private
= r10_bio
;
2061 bio
->bi_end_io
= end_sync_read
;
2063 bio
->bi_sector
= r10_bio
->devs
[i
].addr
+
2064 conf
->mirrors
[d
].rdev
->data_offset
;
2065 bio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2070 for (i
=0; i
<conf
->copies
; i
++) {
2071 int d
= r10_bio
->devs
[i
].devnum
;
2072 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
2073 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
2081 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2083 bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
2085 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2088 bio
->bi_phys_segments
= 0;
2093 if (sector_nr
+ max_sync
< max_sector
)
2094 max_sector
= sector_nr
+ max_sync
;
2097 int len
= PAGE_SIZE
;
2099 if (sector_nr
+ (len
>>9) > max_sector
)
2100 len
= (max_sector
- sector_nr
) << 9;
2103 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
2104 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2105 if (bio_add_page(bio
, page
, len
, 0) == 0) {
2108 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2109 for (bio2
= biolist
; bio2
&& bio2
!= bio
; bio2
= bio2
->bi_next
) {
2110 /* remove last page from this bio */
2112 bio2
->bi_size
-= len
;
2113 bio2
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2119 nr_sectors
+= len
>>9;
2120 sector_nr
+= len
>>9;
2121 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
2123 r10_bio
->sectors
= nr_sectors
;
2127 biolist
= biolist
->bi_next
;
2129 bio
->bi_next
= NULL
;
2130 r10_bio
= bio
->bi_private
;
2131 r10_bio
->sectors
= nr_sectors
;
2133 if (bio
->bi_end_io
== end_sync_read
) {
2134 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2135 generic_make_request(bio
);
2139 if (sectors_skipped
)
2140 /* pretend they weren't skipped, it makes
2141 * no important difference in this case
2143 md_done_sync(mddev
, sectors_skipped
, 1);
2145 return sectors_skipped
+ nr_sectors
;
2147 /* There is nowhere to write, so all non-sync
2148 * drives must be failed, so try the next chunk...
2150 if (sector_nr
+ max_sync
< max_sector
)
2151 max_sector
= sector_nr
+ max_sync
;
2153 sectors_skipped
+= (max_sector
- sector_nr
);
2155 sector_nr
= max_sector
;
2160 raid10_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2163 conf_t
*conf
= mddev
->private;
2166 raid_disks
= conf
->raid_disks
;
2168 sectors
= conf
->dev_sectors
;
2170 size
= sectors
>> conf
->chunk_shift
;
2171 sector_div(size
, conf
->far_copies
);
2172 size
= size
* raid_disks
;
2173 sector_div(size
, conf
->near_copies
);
2175 return size
<< conf
->chunk_shift
;
2179 static conf_t
*setup_conf(mddev_t
*mddev
)
2181 conf_t
*conf
= NULL
;
2183 sector_t stride
, size
;
2186 if (mddev
->new_chunk_sectors
< (PAGE_SIZE
>> 9) ||
2187 !is_power_of_2(mddev
->new_chunk_sectors
)) {
2188 printk(KERN_ERR
"md/raid10:%s: chunk size must be "
2189 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2190 mdname(mddev
), PAGE_SIZE
);
2194 nc
= mddev
->new_layout
& 255;
2195 fc
= (mddev
->new_layout
>> 8) & 255;
2196 fo
= mddev
->new_layout
& (1<<16);
2198 if ((nc
*fc
) <2 || (nc
*fc
) > mddev
->raid_disks
||
2199 (mddev
->new_layout
>> 17)) {
2200 printk(KERN_ERR
"md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2201 mdname(mddev
), mddev
->new_layout
);
2206 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2210 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2215 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2220 conf
->raid_disks
= mddev
->raid_disks
;
2221 conf
->near_copies
= nc
;
2222 conf
->far_copies
= fc
;
2223 conf
->copies
= nc
*fc
;
2224 conf
->far_offset
= fo
;
2225 conf
->chunk_mask
= mddev
->new_chunk_sectors
- 1;
2226 conf
->chunk_shift
= ffz(~mddev
->new_chunk_sectors
);
2228 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
2229 r10bio_pool_free
, conf
);
2230 if (!conf
->r10bio_pool
)
2233 size
= mddev
->dev_sectors
>> conf
->chunk_shift
;
2234 sector_div(size
, fc
);
2235 size
= size
* conf
->raid_disks
;
2236 sector_div(size
, nc
);
2237 /* 'size' is now the number of chunks in the array */
2238 /* calculate "used chunks per device" in 'stride' */
2239 stride
= size
* conf
->copies
;
2241 /* We need to round up when dividing by raid_disks to
2242 * get the stride size.
2244 stride
+= conf
->raid_disks
- 1;
2245 sector_div(stride
, conf
->raid_disks
);
2247 conf
->dev_sectors
= stride
<< conf
->chunk_shift
;
2252 sector_div(stride
, fc
);
2253 conf
->stride
= stride
<< conf
->chunk_shift
;
2256 spin_lock_init(&conf
->device_lock
);
2257 INIT_LIST_HEAD(&conf
->retry_list
);
2259 spin_lock_init(&conf
->resync_lock
);
2260 init_waitqueue_head(&conf
->wait_barrier
);
2262 conf
->thread
= md_register_thread(raid10d
, mddev
, NULL
);
2266 conf
->mddev
= mddev
;
2270 printk(KERN_ERR
"md/raid10:%s: couldn't allocate memory.\n",
2273 if (conf
->r10bio_pool
)
2274 mempool_destroy(conf
->r10bio_pool
);
2275 kfree(conf
->mirrors
);
2276 safe_put_page(conf
->tmppage
);
2279 return ERR_PTR(err
);
2282 static int run(mddev_t
*mddev
)
2285 int i
, disk_idx
, chunk_size
;
2286 mirror_info_t
*disk
;
2291 * copy the already verified devices into our private RAID10
2292 * bookkeeping area. [whatever we allocate in run(),
2293 * should be freed in stop()]
2296 if (mddev
->private == NULL
) {
2297 conf
= setup_conf(mddev
);
2299 return PTR_ERR(conf
);
2300 mddev
->private = conf
;
2302 conf
= mddev
->private;
2306 mddev
->queue
->queue_lock
= &conf
->device_lock
;
2308 mddev
->thread
= conf
->thread
;
2309 conf
->thread
= NULL
;
2311 chunk_size
= mddev
->chunk_sectors
<< 9;
2312 blk_queue_io_min(mddev
->queue
, chunk_size
);
2313 if (conf
->raid_disks
% conf
->near_copies
)
2314 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->raid_disks
);
2316 blk_queue_io_opt(mddev
->queue
, chunk_size
*
2317 (conf
->raid_disks
/ conf
->near_copies
));
2319 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2320 disk_idx
= rdev
->raid_disk
;
2321 if (disk_idx
>= conf
->raid_disks
2324 disk
= conf
->mirrors
+ disk_idx
;
2327 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2328 rdev
->data_offset
<< 9);
2329 /* as we don't honour merge_bvec_fn, we must never risk
2330 * violating it, so limit max_segments to 1 lying
2331 * within a single page.
2333 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2334 blk_queue_max_segments(mddev
->queue
, 1);
2335 blk_queue_segment_boundary(mddev
->queue
,
2336 PAGE_CACHE_SIZE
- 1);
2339 disk
->head_position
= 0;
2341 /* need to check that every block has at least one working mirror */
2342 if (!enough(conf
)) {
2343 printk(KERN_ERR
"md/raid10:%s: not enough operational mirrors.\n",
2348 mddev
->degraded
= 0;
2349 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2351 disk
= conf
->mirrors
+ i
;
2354 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2355 disk
->head_position
= 0;
2362 if (mddev
->recovery_cp
!= MaxSector
)
2363 printk(KERN_NOTICE
"md/raid10:%s: not clean"
2364 " -- starting background reconstruction\n",
2367 "md/raid10:%s: active with %d out of %d devices\n",
2368 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
,
2371 * Ok, everything is just fine now
2373 mddev
->dev_sectors
= conf
->dev_sectors
;
2374 size
= raid10_size(mddev
, 0, 0);
2375 md_set_array_sectors(mddev
, size
);
2376 mddev
->resync_max_sectors
= size
;
2378 mddev
->queue
->unplug_fn
= raid10_unplug
;
2379 mddev
->queue
->backing_dev_info
.congested_fn
= raid10_congested
;
2380 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2382 /* Calculate max read-ahead size.
2383 * We need to readahead at least twice a whole stripe....
2387 int stripe
= conf
->raid_disks
*
2388 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
2389 stripe
/= conf
->near_copies
;
2390 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
2391 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
2394 if (conf
->near_copies
< conf
->raid_disks
)
2395 blk_queue_merge_bvec(mddev
->queue
, raid10_mergeable_bvec
);
2396 md_integrity_register(mddev
);
2400 if (conf
->r10bio_pool
)
2401 mempool_destroy(conf
->r10bio_pool
);
2402 safe_put_page(conf
->tmppage
);
2403 kfree(conf
->mirrors
);
2405 mddev
->private = NULL
;
2406 md_unregister_thread(mddev
->thread
);
2411 static int stop(mddev_t
*mddev
)
2413 conf_t
*conf
= mddev
->private;
2415 raise_barrier(conf
, 0);
2416 lower_barrier(conf
);
2418 md_unregister_thread(mddev
->thread
);
2419 mddev
->thread
= NULL
;
2420 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2421 if (conf
->r10bio_pool
)
2422 mempool_destroy(conf
->r10bio_pool
);
2423 kfree(conf
->mirrors
);
2425 mddev
->private = NULL
;
2429 static void raid10_quiesce(mddev_t
*mddev
, int state
)
2431 conf_t
*conf
= mddev
->private;
2435 raise_barrier(conf
, 0);
2438 lower_barrier(conf
);
2443 static void *raid10_takeover_raid0(mddev_t
*mddev
)
2448 if (mddev
->degraded
> 0) {
2449 printk(KERN_ERR
"md/raid10:%s: Error: degraded raid0!\n",
2451 return ERR_PTR(-EINVAL
);
2454 /* Set new parameters */
2455 mddev
->new_level
= 10;
2456 /* new layout: far_copies = 1, near_copies = 2 */
2457 mddev
->new_layout
= (1<<8) + 2;
2458 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2459 mddev
->delta_disks
= mddev
->raid_disks
;
2460 mddev
->raid_disks
*= 2;
2461 /* make sure it will be not marked as dirty */
2462 mddev
->recovery_cp
= MaxSector
;
2464 conf
= setup_conf(mddev
);
2466 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
2467 if (rdev
->raid_disk
>= 0)
2468 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
2473 static void *raid10_takeover(mddev_t
*mddev
)
2475 struct raid0_private_data
*raid0_priv
;
2477 /* raid10 can take over:
2478 * raid0 - providing it has only two drives
2480 if (mddev
->level
== 0) {
2481 /* for raid0 takeover only one zone is supported */
2482 raid0_priv
= mddev
->private;
2483 if (raid0_priv
->nr_strip_zones
> 1) {
2484 printk(KERN_ERR
"md/raid10:%s: cannot takeover raid 0"
2485 " with more than one zone.\n",
2487 return ERR_PTR(-EINVAL
);
2489 return raid10_takeover_raid0(mddev
);
2491 return ERR_PTR(-EINVAL
);
2494 static struct mdk_personality raid10_personality
=
2498 .owner
= THIS_MODULE
,
2499 .make_request
= make_request
,
2503 .error_handler
= error
,
2504 .hot_add_disk
= raid10_add_disk
,
2505 .hot_remove_disk
= raid10_remove_disk
,
2506 .spare_active
= raid10_spare_active
,
2507 .sync_request
= sync_request
,
2508 .quiesce
= raid10_quiesce
,
2509 .size
= raid10_size
,
2510 .takeover
= raid10_takeover
,
2513 static int __init
raid_init(void)
2515 return register_md_personality(&raid10_personality
);
2518 static void raid_exit(void)
2520 unregister_md_personality(&raid10_personality
);
2523 module_init(raid_init
);
2524 module_exit(raid_exit
);
2525 MODULE_LICENSE("GPL");
2526 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2527 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2528 MODULE_ALIAS("md-raid10");
2529 MODULE_ALIAS("md-level-10");