2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/seq_file.h>
25 #include <linux/ratelimit.h>
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
38 * far_offset (stored in bit 16 of layout )
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
46 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
48 * near_copies and far_copies must be at least one, and their product is at most
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
57 * Number of guaranteed r10bios in case of extreme VM load:
59 #define NR_RAID10_BIOS 256
61 static void allow_barrier(conf_t *conf);
62 static void lower_barrier(conf_t *conf);
64 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
67 int size = offsetof(struct r10bio_s, devs[conf->copies]);
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
70 return kzalloc(size, gfp_flags);
73 static void r10bio_pool_free(void *r10_bio, void *data)
78 /* Maximum size of each resync request */
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
81 /* amount of memory to reserve for resync requests */
82 #define RESYNC_WINDOW (1024*1024)
83 /* maximum number of concurrent requests, memory permitting */
84 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
93 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
102 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
106 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
107 nalloc = conf->copies; /* resync */
109 nalloc = 2; /* recovery */
114 for (j = nalloc ; j-- ; ) {
115 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
118 r10_bio->devs[j].bio = bio;
121 * Allocate RESYNC_PAGES data pages and attach them
124 for (j = 0 ; j < nalloc; j++) {
125 bio = r10_bio->devs[j].bio;
126 for (i = 0; i < RESYNC_PAGES; i++) {
127 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
128 &conf->mddev->recovery)) {
129 /* we can share bv_page's during recovery */
130 struct bio *rbio = r10_bio->devs[0].bio;
131 page = rbio->bi_io_vec[i].bv_page;
134 page = alloc_page(gfp_flags);
138 bio->bi_io_vec[i].bv_page = page;
146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
148 for (i = 0; i < RESYNC_PAGES ; i++)
149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
158 static void r10buf_pool_free(void *__r10_bio, void *data)
162 r10bio_t *r10bio = __r10_bio;
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
168 for (i = 0; i < RESYNC_PAGES; i++) {
169 safe_put_page(bio->bi_io_vec[i].bv_page);
170 bio->bi_io_vec[i].bv_page = NULL;
175 r10bio_pool_free(r10bio, conf);
178 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
184 if (*bio && *bio != IO_BLOCKED)
190 static void free_r10bio(r10bio_t *r10_bio)
192 conf_t *conf = r10_bio->mddev->private;
195 * Wake up any possible resync thread that waits for the device
200 put_all_bios(conf, r10_bio);
201 mempool_free(r10_bio, conf->r10bio_pool);
204 static void put_buf(r10bio_t *r10_bio)
206 conf_t *conf = r10_bio->mddev->private;
208 mempool_free(r10_bio, conf->r10buf_pool);
213 static void reschedule_retry(r10bio_t *r10_bio)
216 mddev_t *mddev = r10_bio->mddev;
217 conf_t *conf = mddev->private;
219 spin_lock_irqsave(&conf->device_lock, flags);
220 list_add(&r10_bio->retry_list, &conf->retry_list);
222 spin_unlock_irqrestore(&conf->device_lock, flags);
224 /* wake up frozen array... */
225 wake_up(&conf->wait_barrier);
227 md_wakeup_thread(mddev->thread);
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
235 static void raid_end_bio_io(r10bio_t *r10_bio)
237 struct bio *bio = r10_bio->master_bio;
240 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
241 free_r10bio(r10_bio);
245 * Update disk head position estimator based on IRQ completion info.
247 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
249 conf_t *conf = r10_bio->mddev->private;
251 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
252 r10_bio->devs[slot].addr + (r10_bio->sectors);
256 * Find the disk number which triggered given bio
258 static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio, struct bio *bio)
262 for (slot = 0; slot < conf->copies; slot++)
263 if (r10_bio->devs[slot].bio == bio)
266 BUG_ON(slot == conf->copies);
267 update_head_pos(slot, r10_bio);
269 return r10_bio->devs[slot].devnum;
272 static void raid10_end_read_request(struct bio *bio, int error)
274 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
275 r10bio_t *r10_bio = bio->bi_private;
277 conf_t *conf = r10_bio->mddev->private;
280 slot = r10_bio->read_slot;
281 dev = r10_bio->devs[slot].devnum;
283 * this branch is our 'one mirror IO has finished' event handler:
285 update_head_pos(slot, r10_bio);
289 * Set R10BIO_Uptodate in our master bio, so that
290 * we will return a good error code to the higher
291 * levels even if IO on some other mirrored buffer fails.
293 * The 'master' represents the composite IO operation to
294 * user-side. So if something waits for IO, then it will
295 * wait for the 'master' bio.
297 set_bit(R10BIO_Uptodate, &r10_bio->state);
298 raid_end_bio_io(r10_bio);
299 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
302 * oops, read error - keep the refcount on the rdev
304 char b[BDEVNAME_SIZE];
305 printk_ratelimited(KERN_ERR
306 "md/raid10:%s: %s: rescheduling sector %llu\n",
308 bdevname(conf->mirrors[dev].rdev->bdev, b),
309 (unsigned long long)r10_bio->sector);
310 reschedule_retry(r10_bio);
314 static void raid10_end_write_request(struct bio *bio, int error)
316 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
317 r10bio_t *r10_bio = bio->bi_private;
319 conf_t *conf = r10_bio->mddev->private;
321 dev = find_bio_disk(conf, r10_bio, bio);
324 * this branch is our 'one mirror IO has finished' event handler:
327 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
328 /* an I/O failed, we can't clear the bitmap */
329 set_bit(R10BIO_Degraded, &r10_bio->state);
332 * Set R10BIO_Uptodate in our master bio, so that
333 * we will return a good error code for to the higher
334 * levels even if IO on some other mirrored buffer fails.
336 * The 'master' represents the composite IO operation to
337 * user-side. So if something waits for IO, then it will
338 * wait for the 'master' bio.
340 set_bit(R10BIO_Uptodate, &r10_bio->state);
344 * Let's see if all mirrored write operations have finished
347 if (atomic_dec_and_test(&r10_bio->remaining)) {
348 /* clear the bitmap if all writes complete successfully */
349 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
351 !test_bit(R10BIO_Degraded, &r10_bio->state),
353 md_write_end(r10_bio->mddev);
354 raid_end_bio_io(r10_bio);
357 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
362 * RAID10 layout manager
363 * As well as the chunksize and raid_disks count, there are two
364 * parameters: near_copies and far_copies.
365 * near_copies * far_copies must be <= raid_disks.
366 * Normally one of these will be 1.
367 * If both are 1, we get raid0.
368 * If near_copies == raid_disks, we get raid1.
370 * Chunks are laid out in raid0 style with near_copies copies of the
371 * first chunk, followed by near_copies copies of the next chunk and
373 * If far_copies > 1, then after 1/far_copies of the array has been assigned
374 * as described above, we start again with a device offset of near_copies.
375 * So we effectively have another copy of the whole array further down all
376 * the drives, but with blocks on different drives.
377 * With this layout, and block is never stored twice on the one device.
379 * raid10_find_phys finds the sector offset of a given virtual sector
380 * on each device that it is on.
382 * raid10_find_virt does the reverse mapping, from a device and a
383 * sector offset to a virtual address
386 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
396 /* now calculate first sector/dev */
397 chunk = r10bio->sector >> conf->chunk_shift;
398 sector = r10bio->sector & conf->chunk_mask;
400 chunk *= conf->near_copies;
402 dev = sector_div(stripe, conf->raid_disks);
403 if (conf->far_offset)
404 stripe *= conf->far_copies;
406 sector += stripe << conf->chunk_shift;
408 /* and calculate all the others */
409 for (n=0; n < conf->near_copies; n++) {
412 r10bio->devs[slot].addr = sector;
413 r10bio->devs[slot].devnum = d;
416 for (f = 1; f < conf->far_copies; f++) {
417 d += conf->near_copies;
418 if (d >= conf->raid_disks)
419 d -= conf->raid_disks;
421 r10bio->devs[slot].devnum = d;
422 r10bio->devs[slot].addr = s;
426 if (dev >= conf->raid_disks) {
428 sector += (conf->chunk_mask + 1);
431 BUG_ON(slot != conf->copies);
434 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
436 sector_t offset, chunk, vchunk;
438 offset = sector & conf->chunk_mask;
439 if (conf->far_offset) {
441 chunk = sector >> conf->chunk_shift;
442 fc = sector_div(chunk, conf->far_copies);
443 dev -= fc * conf->near_copies;
445 dev += conf->raid_disks;
447 while (sector >= conf->stride) {
448 sector -= conf->stride;
449 if (dev < conf->near_copies)
450 dev += conf->raid_disks - conf->near_copies;
452 dev -= conf->near_copies;
454 chunk = sector >> conf->chunk_shift;
456 vchunk = chunk * conf->raid_disks + dev;
457 sector_div(vchunk, conf->near_copies);
458 return (vchunk << conf->chunk_shift) + offset;
462 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
464 * @bvm: properties of new bio
465 * @biovec: the request that could be merged to it.
467 * Return amount of bytes we can accept at this offset
468 * If near_copies == raid_disk, there are no striping issues,
469 * but in that case, the function isn't called at all.
471 static int raid10_mergeable_bvec(struct request_queue *q,
472 struct bvec_merge_data *bvm,
473 struct bio_vec *biovec)
475 mddev_t *mddev = q->queuedata;
476 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
478 unsigned int chunk_sectors = mddev->chunk_sectors;
479 unsigned int bio_sectors = bvm->bi_size >> 9;
481 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
482 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
483 if (max <= biovec->bv_len && bio_sectors == 0)
484 return biovec->bv_len;
490 * This routine returns the disk from which the requested read should
491 * be done. There is a per-array 'next expected sequential IO' sector
492 * number - if this matches on the next IO then we use the last disk.
493 * There is also a per-disk 'last know head position' sector that is
494 * maintained from IRQ contexts, both the normal and the resync IO
495 * completion handlers update this position correctly. If there is no
496 * perfect sequential match then we pick the disk whose head is closest.
498 * If there are 2 mirrors in the same 2 devices, performance degrades
499 * because position is mirror, not device based.
501 * The rdev for the device selected will have nr_pending incremented.
505 * FIXME: possibly should rethink readbalancing and do it differently
506 * depending on near_copies / far_copies geometry.
508 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
510 const sector_t this_sector = r10_bio->sector;
512 const int sectors = r10_bio->sectors;
513 sector_t new_distance, best_dist;
518 raid10_find_phys(conf, r10_bio);
522 best_dist = MaxSector;
525 * Check if we can balance. We can balance on the whole
526 * device if no resync is going on (recovery is ok), or below
527 * the resync window. We take the first readable disk when
528 * above the resync window.
530 if (conf->mddev->recovery_cp < MaxSector
531 && (this_sector + sectors >= conf->next_resync))
534 for (slot = 0; slot < conf->copies ; slot++) {
535 if (r10_bio->devs[slot].bio == IO_BLOCKED)
537 disk = r10_bio->devs[slot].devnum;
538 rdev = rcu_dereference(conf->mirrors[disk].rdev);
541 if (!test_bit(In_sync, &rdev->flags))
547 /* This optimisation is debatable, and completely destroys
548 * sequential read speed for 'far copies' arrays. So only
549 * keep it for 'near' arrays, and review those later.
551 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
554 /* for far > 1 always use the lowest address */
555 if (conf->far_copies > 1)
556 new_distance = r10_bio->devs[slot].addr;
558 new_distance = abs(r10_bio->devs[slot].addr -
559 conf->mirrors[disk].head_position);
560 if (new_distance < best_dist) {
561 best_dist = new_distance;
565 if (slot == conf->copies)
569 disk = r10_bio->devs[slot].devnum;
570 rdev = rcu_dereference(conf->mirrors[disk].rdev);
573 atomic_inc(&rdev->nr_pending);
574 if (test_bit(Faulty, &rdev->flags)) {
575 /* Cannot risk returning a device that failed
576 * before we inc'ed nr_pending
578 rdev_dec_pending(rdev, conf->mddev);
581 r10_bio->read_slot = slot;
589 static int raid10_congested(void *data, int bits)
591 mddev_t *mddev = data;
592 conf_t *conf = mddev->private;
595 if (mddev_congested(mddev, bits))
598 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
599 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
600 if (rdev && !test_bit(Faulty, &rdev->flags)) {
601 struct request_queue *q = bdev_get_queue(rdev->bdev);
603 ret |= bdi_congested(&q->backing_dev_info, bits);
610 static void flush_pending_writes(conf_t *conf)
612 /* Any writes that have been queued but are awaiting
613 * bitmap updates get flushed here.
615 spin_lock_irq(&conf->device_lock);
617 if (conf->pending_bio_list.head) {
619 bio = bio_list_get(&conf->pending_bio_list);
620 spin_unlock_irq(&conf->device_lock);
621 /* flush any pending bitmap writes to disk
622 * before proceeding w/ I/O */
623 bitmap_unplug(conf->mddev->bitmap);
625 while (bio) { /* submit pending writes */
626 struct bio *next = bio->bi_next;
628 generic_make_request(bio);
632 spin_unlock_irq(&conf->device_lock);
636 * Sometimes we need to suspend IO while we do something else,
637 * either some resync/recovery, or reconfigure the array.
638 * To do this we raise a 'barrier'.
639 * The 'barrier' is a counter that can be raised multiple times
640 * to count how many activities are happening which preclude
642 * We can only raise the barrier if there is no pending IO.
643 * i.e. if nr_pending == 0.
644 * We choose only to raise the barrier if no-one is waiting for the
645 * barrier to go down. This means that as soon as an IO request
646 * is ready, no other operations which require a barrier will start
647 * until the IO request has had a chance.
649 * So: regular IO calls 'wait_barrier'. When that returns there
650 * is no backgroup IO happening, It must arrange to call
651 * allow_barrier when it has finished its IO.
652 * backgroup IO calls must call raise_barrier. Once that returns
653 * there is no normal IO happeing. It must arrange to call
654 * lower_barrier when the particular background IO completes.
657 static void raise_barrier(conf_t *conf, int force)
659 BUG_ON(force && !conf->barrier);
660 spin_lock_irq(&conf->resync_lock);
662 /* Wait until no block IO is waiting (unless 'force') */
663 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
664 conf->resync_lock, );
666 /* block any new IO from starting */
669 /* Now wait for all pending IO to complete */
670 wait_event_lock_irq(conf->wait_barrier,
671 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
672 conf->resync_lock, );
674 spin_unlock_irq(&conf->resync_lock);
677 static void lower_barrier(conf_t *conf)
680 spin_lock_irqsave(&conf->resync_lock, flags);
682 spin_unlock_irqrestore(&conf->resync_lock, flags);
683 wake_up(&conf->wait_barrier);
686 static void wait_barrier(conf_t *conf)
688 spin_lock_irq(&conf->resync_lock);
691 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
697 spin_unlock_irq(&conf->resync_lock);
700 static void allow_barrier(conf_t *conf)
703 spin_lock_irqsave(&conf->resync_lock, flags);
705 spin_unlock_irqrestore(&conf->resync_lock, flags);
706 wake_up(&conf->wait_barrier);
709 static void freeze_array(conf_t *conf)
711 /* stop syncio and normal IO and wait for everything to
713 * We increment barrier and nr_waiting, and then
714 * wait until nr_pending match nr_queued+1
715 * This is called in the context of one normal IO request
716 * that has failed. Thus any sync request that might be pending
717 * will be blocked by nr_pending, and we need to wait for
718 * pending IO requests to complete or be queued for re-try.
719 * Thus the number queued (nr_queued) plus this request (1)
720 * must match the number of pending IOs (nr_pending) before
723 spin_lock_irq(&conf->resync_lock);
726 wait_event_lock_irq(conf->wait_barrier,
727 conf->nr_pending == conf->nr_queued+1,
729 flush_pending_writes(conf));
731 spin_unlock_irq(&conf->resync_lock);
734 static void unfreeze_array(conf_t *conf)
736 /* reverse the effect of the freeze */
737 spin_lock_irq(&conf->resync_lock);
740 wake_up(&conf->wait_barrier);
741 spin_unlock_irq(&conf->resync_lock);
744 static int make_request(mddev_t *mddev, struct bio * bio)
746 conf_t *conf = mddev->private;
747 mirror_info_t *mirror;
749 struct bio *read_bio;
751 int chunk_sects = conf->chunk_mask + 1;
752 const int rw = bio_data_dir(bio);
753 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
754 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
756 mdk_rdev_t *blocked_rdev;
759 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
760 md_flush_request(mddev, bio);
764 /* If this request crosses a chunk boundary, we need to
765 * split it. This will only happen for 1 PAGE (or less) requests.
767 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
769 conf->near_copies < conf->raid_disks)) {
771 /* Sanity check -- queue functions should prevent this happening */
772 if (bio->bi_vcnt != 1 ||
775 /* This is a one page bio that upper layers
776 * refuse to split for us, so we need to split it.
779 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
781 /* Each of these 'make_request' calls will call 'wait_barrier'.
782 * If the first succeeds but the second blocks due to the resync
783 * thread raising the barrier, we will deadlock because the
784 * IO to the underlying device will be queued in generic_make_request
785 * and will never complete, so will never reduce nr_pending.
786 * So increment nr_waiting here so no new raise_barriers will
787 * succeed, and so the second wait_barrier cannot block.
789 spin_lock_irq(&conf->resync_lock);
791 spin_unlock_irq(&conf->resync_lock);
793 if (make_request(mddev, &bp->bio1))
794 generic_make_request(&bp->bio1);
795 if (make_request(mddev, &bp->bio2))
796 generic_make_request(&bp->bio2);
798 spin_lock_irq(&conf->resync_lock);
800 wake_up(&conf->wait_barrier);
801 spin_unlock_irq(&conf->resync_lock);
803 bio_pair_release(bp);
806 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
807 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
808 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
814 md_write_start(mddev, bio);
817 * Register the new request and wait if the reconstruction
818 * thread has put up a bar for new requests.
819 * Continue immediately if no resync is active currently.
823 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
825 r10_bio->master_bio = bio;
826 r10_bio->sectors = bio->bi_size >> 9;
828 r10_bio->mddev = mddev;
829 r10_bio->sector = bio->bi_sector;
834 * read balancing logic:
836 int disk = read_balance(conf, r10_bio);
837 int slot = r10_bio->read_slot;
839 raid_end_bio_io(r10_bio);
842 mirror = conf->mirrors + disk;
844 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
846 r10_bio->devs[slot].bio = read_bio;
848 read_bio->bi_sector = r10_bio->devs[slot].addr +
849 mirror->rdev->data_offset;
850 read_bio->bi_bdev = mirror->rdev->bdev;
851 read_bio->bi_end_io = raid10_end_read_request;
852 read_bio->bi_rw = READ | do_sync;
853 read_bio->bi_private = r10_bio;
855 generic_make_request(read_bio);
862 /* first select target devices under rcu_lock and
863 * inc refcount on their rdev. Record them by setting
866 plugged = mddev_check_plugged(mddev);
868 raid10_find_phys(conf, r10_bio);
872 for (i = 0; i < conf->copies; i++) {
873 int d = r10_bio->devs[i].devnum;
874 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
875 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
876 atomic_inc(&rdev->nr_pending);
880 if (rdev && !test_bit(Faulty, &rdev->flags)) {
881 atomic_inc(&rdev->nr_pending);
882 r10_bio->devs[i].bio = bio;
884 r10_bio->devs[i].bio = NULL;
885 set_bit(R10BIO_Degraded, &r10_bio->state);
890 if (unlikely(blocked_rdev)) {
891 /* Have to wait for this device to get unblocked, then retry */
895 for (j = 0; j < i; j++)
896 if (r10_bio->devs[j].bio) {
897 d = r10_bio->devs[j].devnum;
898 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
901 md_wait_for_blocked_rdev(blocked_rdev, mddev);
906 atomic_set(&r10_bio->remaining, 1);
907 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
909 for (i = 0; i < conf->copies; i++) {
911 int d = r10_bio->devs[i].devnum;
912 if (!r10_bio->devs[i].bio)
915 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
916 r10_bio->devs[i].bio = mbio;
918 mbio->bi_sector = r10_bio->devs[i].addr+
919 conf->mirrors[d].rdev->data_offset;
920 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
921 mbio->bi_end_io = raid10_end_write_request;
922 mbio->bi_rw = WRITE | do_sync | do_fua;
923 mbio->bi_private = r10_bio;
925 atomic_inc(&r10_bio->remaining);
926 spin_lock_irqsave(&conf->device_lock, flags);
927 bio_list_add(&conf->pending_bio_list, mbio);
928 spin_unlock_irqrestore(&conf->device_lock, flags);
931 if (atomic_dec_and_test(&r10_bio->remaining)) {
932 /* This matches the end of raid10_end_write_request() */
933 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
935 !test_bit(R10BIO_Degraded, &r10_bio->state),
938 raid_end_bio_io(r10_bio);
941 /* In case raid10d snuck in to freeze_array */
942 wake_up(&conf->wait_barrier);
944 if (do_sync || !mddev->bitmap || !plugged)
945 md_wakeup_thread(mddev->thread);
949 static void status(struct seq_file *seq, mddev_t *mddev)
951 conf_t *conf = mddev->private;
954 if (conf->near_copies < conf->raid_disks)
955 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
956 if (conf->near_copies > 1)
957 seq_printf(seq, " %d near-copies", conf->near_copies);
958 if (conf->far_copies > 1) {
959 if (conf->far_offset)
960 seq_printf(seq, " %d offset-copies", conf->far_copies);
962 seq_printf(seq, " %d far-copies", conf->far_copies);
964 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
965 conf->raid_disks - mddev->degraded);
966 for (i = 0; i < conf->raid_disks; i++)
967 seq_printf(seq, "%s",
968 conf->mirrors[i].rdev &&
969 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
970 seq_printf(seq, "]");
973 /* check if there are enough drives for
974 * every block to appear on atleast one.
975 * Don't consider the device numbered 'ignore'
976 * as we might be about to remove it.
978 static int enough(conf_t *conf, int ignore)
983 int n = conf->copies;
986 if (conf->mirrors[first].rdev &&
989 first = (first+1) % conf->raid_disks;
993 } while (first != 0);
997 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
999 char b[BDEVNAME_SIZE];
1000 conf_t *conf = mddev->private;
1003 * If it is not operational, then we have already marked it as dead
1004 * else if it is the last working disks, ignore the error, let the
1005 * next level up know.
1006 * else mark the drive as failed
1008 if (test_bit(In_sync, &rdev->flags)
1009 && !enough(conf, rdev->raid_disk))
1011 * Don't fail the drive, just return an IO error.
1014 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1015 unsigned long flags;
1016 spin_lock_irqsave(&conf->device_lock, flags);
1018 spin_unlock_irqrestore(&conf->device_lock, flags);
1020 * if recovery is running, make sure it aborts.
1022 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1024 set_bit(Faulty, &rdev->flags);
1025 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1027 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1028 "md/raid10:%s: Operation continuing on %d devices.\n",
1029 mdname(mddev), bdevname(rdev->bdev, b),
1030 mdname(mddev), conf->raid_disks - mddev->degraded);
1033 static void print_conf(conf_t *conf)
1038 printk(KERN_DEBUG "RAID10 conf printout:\n");
1040 printk(KERN_DEBUG "(!conf)\n");
1043 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1046 for (i = 0; i < conf->raid_disks; i++) {
1047 char b[BDEVNAME_SIZE];
1048 tmp = conf->mirrors + i;
1050 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1051 i, !test_bit(In_sync, &tmp->rdev->flags),
1052 !test_bit(Faulty, &tmp->rdev->flags),
1053 bdevname(tmp->rdev->bdev,b));
1057 static void close_sync(conf_t *conf)
1060 allow_barrier(conf);
1062 mempool_destroy(conf->r10buf_pool);
1063 conf->r10buf_pool = NULL;
1066 static int raid10_spare_active(mddev_t *mddev)
1069 conf_t *conf = mddev->private;
1072 unsigned long flags;
1075 * Find all non-in_sync disks within the RAID10 configuration
1076 * and mark them in_sync
1078 for (i = 0; i < conf->raid_disks; i++) {
1079 tmp = conf->mirrors + i;
1081 && !test_bit(Faulty, &tmp->rdev->flags)
1082 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1084 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1087 spin_lock_irqsave(&conf->device_lock, flags);
1088 mddev->degraded -= count;
1089 spin_unlock_irqrestore(&conf->device_lock, flags);
1096 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1098 conf_t *conf = mddev->private;
1102 int last = conf->raid_disks - 1;
1104 if (mddev->recovery_cp < MaxSector)
1105 /* only hot-add to in-sync arrays, as recovery is
1106 * very different from resync
1109 if (!enough(conf, -1))
1112 if (rdev->raid_disk >= 0)
1113 first = last = rdev->raid_disk;
1115 if (rdev->saved_raid_disk >= first &&
1116 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1117 mirror = rdev->saved_raid_disk;
1120 for ( ; mirror <= last ; mirror++) {
1121 mirror_info_t *p = &conf->mirrors[mirror];
1122 if (p->recovery_disabled == mddev->recovery_disabled)
1127 disk_stack_limits(mddev->gendisk, rdev->bdev,
1128 rdev->data_offset << 9);
1129 /* as we don't honour merge_bvec_fn, we must
1130 * never risk violating it, so limit
1131 * ->max_segments to one lying with a single
1132 * page, as a one page request is never in
1135 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1136 blk_queue_max_segments(mddev->queue, 1);
1137 blk_queue_segment_boundary(mddev->queue,
1138 PAGE_CACHE_SIZE - 1);
1141 p->head_position = 0;
1142 rdev->raid_disk = mirror;
1144 if (rdev->saved_raid_disk != mirror)
1146 rcu_assign_pointer(p->rdev, rdev);
1150 md_integrity_add_rdev(rdev, mddev);
1155 static int raid10_remove_disk(mddev_t *mddev, int number)
1157 conf_t *conf = mddev->private;
1160 mirror_info_t *p = conf->mirrors+ number;
1165 if (test_bit(In_sync, &rdev->flags) ||
1166 atomic_read(&rdev->nr_pending)) {
1170 /* Only remove faulty devices in recovery
1173 if (!test_bit(Faulty, &rdev->flags) &&
1174 mddev->recovery_disabled != p->recovery_disabled &&
1181 if (atomic_read(&rdev->nr_pending)) {
1182 /* lost the race, try later */
1187 err = md_integrity_register(mddev);
1196 static void end_sync_read(struct bio *bio, int error)
1198 r10bio_t *r10_bio = bio->bi_private;
1199 conf_t *conf = r10_bio->mddev->private;
1202 d = find_bio_disk(conf, r10_bio, bio);
1204 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1205 set_bit(R10BIO_Uptodate, &r10_bio->state);
1207 atomic_add(r10_bio->sectors,
1208 &conf->mirrors[d].rdev->corrected_errors);
1209 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1210 md_error(r10_bio->mddev,
1211 conf->mirrors[d].rdev);
1214 /* for reconstruct, we always reschedule after a read.
1215 * for resync, only after all reads
1217 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1218 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1219 atomic_dec_and_test(&r10_bio->remaining)) {
1220 /* we have read all the blocks,
1221 * do the comparison in process context in raid10d
1223 reschedule_retry(r10_bio);
1227 static void end_sync_write(struct bio *bio, int error)
1229 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1230 r10bio_t *r10_bio = bio->bi_private;
1231 mddev_t *mddev = r10_bio->mddev;
1232 conf_t *conf = mddev->private;
1235 d = find_bio_disk(conf, r10_bio, bio);
1238 md_error(mddev, conf->mirrors[d].rdev);
1240 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1241 while (atomic_dec_and_test(&r10_bio->remaining)) {
1242 if (r10_bio->master_bio == NULL) {
1243 /* the primary of several recovery bios */
1244 sector_t s = r10_bio->sectors;
1246 md_done_sync(mddev, s, 1);
1249 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1257 * Note: sync and recover and handled very differently for raid10
1258 * This code is for resync.
1259 * For resync, we read through virtual addresses and read all blocks.
1260 * If there is any error, we schedule a write. The lowest numbered
1261 * drive is authoritative.
1262 * However requests come for physical address, so we need to map.
1263 * For every physical address there are raid_disks/copies virtual addresses,
1264 * which is always are least one, but is not necessarly an integer.
1265 * This means that a physical address can span multiple chunks, so we may
1266 * have to submit multiple io requests for a single sync request.
1269 * We check if all blocks are in-sync and only write to blocks that
1272 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1274 conf_t *conf = mddev->private;
1276 struct bio *tbio, *fbio;
1278 atomic_set(&r10_bio->remaining, 1);
1280 /* find the first device with a block */
1281 for (i=0; i<conf->copies; i++)
1282 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1285 if (i == conf->copies)
1289 fbio = r10_bio->devs[i].bio;
1291 /* now find blocks with errors */
1292 for (i=0 ; i < conf->copies ; i++) {
1294 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1296 tbio = r10_bio->devs[i].bio;
1298 if (tbio->bi_end_io != end_sync_read)
1302 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1303 /* We know that the bi_io_vec layout is the same for
1304 * both 'first' and 'i', so we just compare them.
1305 * All vec entries are PAGE_SIZE;
1307 for (j = 0; j < vcnt; j++)
1308 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1309 page_address(tbio->bi_io_vec[j].bv_page),
1314 mddev->resync_mismatches += r10_bio->sectors;
1316 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1317 /* Don't fix anything. */
1319 /* Ok, we need to write this bio
1320 * First we need to fixup bv_offset, bv_len and
1321 * bi_vecs, as the read request might have corrupted these
1323 tbio->bi_vcnt = vcnt;
1324 tbio->bi_size = r10_bio->sectors << 9;
1326 tbio->bi_phys_segments = 0;
1327 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1328 tbio->bi_flags |= 1 << BIO_UPTODATE;
1329 tbio->bi_next = NULL;
1330 tbio->bi_rw = WRITE;
1331 tbio->bi_private = r10_bio;
1332 tbio->bi_sector = r10_bio->devs[i].addr;
1334 for (j=0; j < vcnt ; j++) {
1335 tbio->bi_io_vec[j].bv_offset = 0;
1336 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1338 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1339 page_address(fbio->bi_io_vec[j].bv_page),
1342 tbio->bi_end_io = end_sync_write;
1344 d = r10_bio->devs[i].devnum;
1345 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1346 atomic_inc(&r10_bio->remaining);
1347 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1349 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1350 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1351 generic_make_request(tbio);
1355 if (atomic_dec_and_test(&r10_bio->remaining)) {
1356 md_done_sync(mddev, r10_bio->sectors, 1);
1362 * Now for the recovery code.
1363 * Recovery happens across physical sectors.
1364 * We recover all non-is_sync drives by finding the virtual address of
1365 * each, and then choose a working drive that also has that virt address.
1366 * There is a separate r10_bio for each non-in_sync drive.
1367 * Only the first two slots are in use. The first for reading,
1368 * The second for writing.
1372 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1374 conf_t *conf = mddev->private;
1379 * share the pages with the first bio
1380 * and submit the write request
1382 wbio = r10_bio->devs[1].bio;
1383 d = r10_bio->devs[1].devnum;
1385 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1386 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1387 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1388 generic_make_request(wbio);
1391 "md/raid10:%s: recovery aborted due to read error\n",
1393 conf->mirrors[d].recovery_disabled = mddev->recovery_disabled;
1394 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1401 * Used by fix_read_error() to decay the per rdev read_errors.
1402 * We halve the read error count for every hour that has elapsed
1403 * since the last recorded read error.
1406 static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1408 struct timespec cur_time_mon;
1409 unsigned long hours_since_last;
1410 unsigned int read_errors = atomic_read(&rdev->read_errors);
1412 ktime_get_ts(&cur_time_mon);
1414 if (rdev->last_read_error.tv_sec == 0 &&
1415 rdev->last_read_error.tv_nsec == 0) {
1416 /* first time we've seen a read error */
1417 rdev->last_read_error = cur_time_mon;
1421 hours_since_last = (cur_time_mon.tv_sec -
1422 rdev->last_read_error.tv_sec) / 3600;
1424 rdev->last_read_error = cur_time_mon;
1427 * if hours_since_last is > the number of bits in read_errors
1428 * just set read errors to 0. We do this to avoid
1429 * overflowing the shift of read_errors by hours_since_last.
1431 if (hours_since_last >= 8 * sizeof(read_errors))
1432 atomic_set(&rdev->read_errors, 0);
1434 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1438 * This is a kernel thread which:
1440 * 1. Retries failed read operations on working mirrors.
1441 * 2. Updates the raid superblock when problems encounter.
1442 * 3. Performs writes following reads for array synchronising.
1445 static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1447 int sect = 0; /* Offset from r10_bio->sector */
1448 int sectors = r10_bio->sectors;
1450 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1451 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1453 /* still own a reference to this rdev, so it cannot
1454 * have been cleared recently.
1456 rdev = conf->mirrors[d].rdev;
1458 if (test_bit(Faulty, &rdev->flags))
1459 /* drive has already been failed, just ignore any
1460 more fix_read_error() attempts */
1463 check_decay_read_errors(mddev, rdev);
1464 atomic_inc(&rdev->read_errors);
1465 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1466 char b[BDEVNAME_SIZE];
1467 bdevname(rdev->bdev, b);
1470 "md/raid10:%s: %s: Raid device exceeded "
1471 "read_error threshold [cur %d:max %d]\n",
1473 atomic_read(&rdev->read_errors), max_read_errors);
1475 "md/raid10:%s: %s: Failing raid device\n",
1477 md_error(mddev, conf->mirrors[d].rdev);
1483 int sl = r10_bio->read_slot;
1487 if (s > (PAGE_SIZE>>9))
1492 d = r10_bio->devs[sl].devnum;
1493 rdev = rcu_dereference(conf->mirrors[d].rdev);
1495 test_bit(In_sync, &rdev->flags)) {
1496 atomic_inc(&rdev->nr_pending);
1498 success = sync_page_io(rdev,
1499 r10_bio->devs[sl].addr +
1502 conf->tmppage, READ, false);
1503 rdev_dec_pending(rdev, mddev);
1509 if (sl == conf->copies)
1511 } while (!success && sl != r10_bio->read_slot);
1515 /* Cannot read from anywhere -- bye bye array */
1516 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1517 md_error(mddev, conf->mirrors[dn].rdev);
1522 /* write it back and re-read */
1524 while (sl != r10_bio->read_slot) {
1525 char b[BDEVNAME_SIZE];
1530 d = r10_bio->devs[sl].devnum;
1531 rdev = rcu_dereference(conf->mirrors[d].rdev);
1533 test_bit(In_sync, &rdev->flags)) {
1534 atomic_inc(&rdev->nr_pending);
1536 if (sync_page_io(rdev,
1537 r10_bio->devs[sl].addr +
1539 s<<9, conf->tmppage, WRITE, false)
1541 /* Well, this device is dead */
1543 "md/raid10:%s: read correction "
1545 " (%d sectors at %llu on %s)\n",
1547 (unsigned long long)(
1548 sect + rdev->data_offset),
1549 bdevname(rdev->bdev, b));
1550 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1553 bdevname(rdev->bdev, b));
1554 md_error(mddev, rdev);
1556 rdev_dec_pending(rdev, mddev);
1561 while (sl != r10_bio->read_slot) {
1566 d = r10_bio->devs[sl].devnum;
1567 rdev = rcu_dereference(conf->mirrors[d].rdev);
1569 test_bit(In_sync, &rdev->flags)) {
1570 char b[BDEVNAME_SIZE];
1571 atomic_inc(&rdev->nr_pending);
1573 if (sync_page_io(rdev,
1574 r10_bio->devs[sl].addr +
1576 s<<9, conf->tmppage,
1577 READ, false) == 0) {
1578 /* Well, this device is dead */
1580 "md/raid10:%s: unable to read back "
1582 " (%d sectors at %llu on %s)\n",
1584 (unsigned long long)(
1585 sect + rdev->data_offset),
1586 bdevname(rdev->bdev, b));
1587 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1589 bdevname(rdev->bdev, b));
1591 md_error(mddev, rdev);
1594 "md/raid10:%s: read error corrected"
1595 " (%d sectors at %llu on %s)\n",
1597 (unsigned long long)(
1598 sect + rdev->data_offset),
1599 bdevname(rdev->bdev, b));
1600 atomic_add(s, &rdev->corrected_errors);
1603 rdev_dec_pending(rdev, mddev);
1614 static void raid10d(mddev_t *mddev)
1618 unsigned long flags;
1619 conf_t *conf = mddev->private;
1620 struct list_head *head = &conf->retry_list;
1622 struct blk_plug plug;
1624 md_check_recovery(mddev);
1626 blk_start_plug(&plug);
1628 char b[BDEVNAME_SIZE];
1630 flush_pending_writes(conf);
1632 spin_lock_irqsave(&conf->device_lock, flags);
1633 if (list_empty(head)) {
1634 spin_unlock_irqrestore(&conf->device_lock, flags);
1637 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1638 list_del(head->prev);
1640 spin_unlock_irqrestore(&conf->device_lock, flags);
1642 mddev = r10_bio->mddev;
1643 conf = mddev->private;
1644 if (test_bit(R10BIO_IsSync, &r10_bio->state))
1645 sync_request_write(mddev, r10_bio);
1646 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
1647 recovery_request_write(mddev, r10_bio);
1649 int slot = r10_bio->read_slot;
1650 int mirror = r10_bio->devs[slot].devnum;
1651 /* we got a read error. Maybe the drive is bad. Maybe just
1652 * the block and we can fix it.
1653 * We freeze all other IO, and try reading the block from
1654 * other devices. When we find one, we re-write
1655 * and check it that fixes the read error.
1656 * This is all done synchronously while the array is
1659 if (mddev->ro == 0) {
1661 fix_read_error(conf, mddev, r10_bio);
1662 unfreeze_array(conf);
1664 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1666 bio = r10_bio->devs[slot].bio;
1667 r10_bio->devs[slot].bio =
1668 mddev->ro ? IO_BLOCKED : NULL;
1669 mirror = read_balance(conf, r10_bio);
1671 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
1672 " read error for block %llu\n",
1674 bdevname(bio->bi_bdev,b),
1675 (unsigned long long)r10_bio->sector);
1676 raid_end_bio_io(r10_bio);
1679 const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
1681 slot = r10_bio->read_slot;
1682 rdev = conf->mirrors[mirror].rdev;
1685 "md/raid10:%s: %s: redirecting"
1686 "sector %llu to another mirror\n",
1688 bdevname(rdev->bdev, b),
1689 (unsigned long long)r10_bio->sector);
1690 bio = bio_clone_mddev(r10_bio->master_bio,
1692 r10_bio->devs[slot].bio = bio;
1693 bio->bi_sector = r10_bio->devs[slot].addr
1694 + rdev->data_offset;
1695 bio->bi_bdev = rdev->bdev;
1696 bio->bi_rw = READ | do_sync;
1697 bio->bi_private = r10_bio;
1698 bio->bi_end_io = raid10_end_read_request;
1699 generic_make_request(bio);
1704 blk_finish_plug(&plug);
1708 static int init_resync(conf_t *conf)
1712 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1713 BUG_ON(conf->r10buf_pool);
1714 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1715 if (!conf->r10buf_pool)
1717 conf->next_resync = 0;
1722 * perform a "sync" on one "block"
1724 * We need to make sure that no normal I/O request - particularly write
1725 * requests - conflict with active sync requests.
1727 * This is achieved by tracking pending requests and a 'barrier' concept
1728 * that can be installed to exclude normal IO requests.
1730 * Resync and recovery are handled very differently.
1731 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1733 * For resync, we iterate over virtual addresses, read all copies,
1734 * and update if there are differences. If only one copy is live,
1736 * For recovery, we iterate over physical addresses, read a good
1737 * value for each non-in_sync drive, and over-write.
1739 * So, for recovery we may have several outstanding complex requests for a
1740 * given address, one for each out-of-sync device. We model this by allocating
1741 * a number of r10_bio structures, one for each out-of-sync device.
1742 * As we setup these structures, we collect all bio's together into a list
1743 * which we then process collectively to add pages, and then process again
1744 * to pass to generic_make_request.
1746 * The r10_bio structures are linked using a borrowed master_bio pointer.
1747 * This link is counted in ->remaining. When the r10_bio that points to NULL
1748 * has its remaining count decremented to 0, the whole complex operation
1753 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
1754 int *skipped, int go_faster)
1756 conf_t *conf = mddev->private;
1758 struct bio *biolist = NULL, *bio;
1759 sector_t max_sector, nr_sectors;
1762 sector_t sync_blocks;
1764 sector_t sectors_skipped = 0;
1765 int chunks_skipped = 0;
1767 if (!conf->r10buf_pool)
1768 if (init_resync(conf))
1772 max_sector = mddev->dev_sectors;
1773 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1774 max_sector = mddev->resync_max_sectors;
1775 if (sector_nr >= max_sector) {
1776 /* If we aborted, we need to abort the
1777 * sync on the 'current' bitmap chucks (there can
1778 * be several when recovering multiple devices).
1779 * as we may have started syncing it but not finished.
1780 * We can find the current address in
1781 * mddev->curr_resync, but for recovery,
1782 * we need to convert that to several
1783 * virtual addresses.
1785 if (mddev->curr_resync < max_sector) { /* aborted */
1786 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1787 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1789 else for (i=0; i<conf->raid_disks; i++) {
1791 raid10_find_virt(conf, mddev->curr_resync, i);
1792 bitmap_end_sync(mddev->bitmap, sect,
1795 } else /* completed sync */
1798 bitmap_close_sync(mddev->bitmap);
1801 return sectors_skipped;
1803 if (chunks_skipped >= conf->raid_disks) {
1804 /* if there has been nothing to do on any drive,
1805 * then there is nothing to do at all..
1808 return (max_sector - sector_nr) + sectors_skipped;
1811 if (max_sector > mddev->resync_max)
1812 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1814 /* make sure whole request will fit in a chunk - if chunks
1817 if (conf->near_copies < conf->raid_disks &&
1818 max_sector > (sector_nr | conf->chunk_mask))
1819 max_sector = (sector_nr | conf->chunk_mask) + 1;
1821 * If there is non-resync activity waiting for us then
1822 * put in a delay to throttle resync.
1824 if (!go_faster && conf->nr_waiting)
1825 msleep_interruptible(1000);
1827 /* Again, very different code for resync and recovery.
1828 * Both must result in an r10bio with a list of bios that
1829 * have bi_end_io, bi_sector, bi_bdev set,
1830 * and bi_private set to the r10bio.
1831 * For recovery, we may actually create several r10bios
1832 * with 2 bios in each, that correspond to the bios in the main one.
1833 * In this case, the subordinate r10bios link back through a
1834 * borrowed master_bio pointer, and the counter in the master
1835 * includes a ref from each subordinate.
1837 /* First, we decide what to do and set ->bi_end_io
1838 * To end_sync_read if we want to read, and
1839 * end_sync_write if we will want to write.
1842 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1843 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1844 /* recovery... the complicated one */
1848 for (i=0 ; i<conf->raid_disks; i++) {
1854 if (conf->mirrors[i].rdev == NULL ||
1855 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
1859 /* want to reconstruct this device */
1861 sect = raid10_find_virt(conf, sector_nr, i);
1862 /* Unless we are doing a full sync, we only need
1863 * to recover the block if it is set in the bitmap
1865 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1867 if (sync_blocks < max_sync)
1868 max_sync = sync_blocks;
1871 /* yep, skip the sync_blocks here, but don't assume
1872 * that there will never be anything to do here
1874 chunks_skipped = -1;
1878 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1879 raise_barrier(conf, rb2 != NULL);
1880 atomic_set(&r10_bio->remaining, 0);
1882 r10_bio->master_bio = (struct bio*)rb2;
1884 atomic_inc(&rb2->remaining);
1885 r10_bio->mddev = mddev;
1886 set_bit(R10BIO_IsRecover, &r10_bio->state);
1887 r10_bio->sector = sect;
1889 raid10_find_phys(conf, r10_bio);
1891 /* Need to check if the array will still be
1894 for (j=0; j<conf->raid_disks; j++)
1895 if (conf->mirrors[j].rdev == NULL ||
1896 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1901 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1902 &sync_blocks, still_degraded);
1904 for (j=0; j<conf->copies;j++) {
1905 int d = r10_bio->devs[j].devnum;
1906 if (!conf->mirrors[d].rdev ||
1907 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
1909 /* This is where we read from */
1910 bio = r10_bio->devs[0].bio;
1911 bio->bi_next = biolist;
1913 bio->bi_private = r10_bio;
1914 bio->bi_end_io = end_sync_read;
1916 bio->bi_sector = r10_bio->devs[j].addr +
1917 conf->mirrors[d].rdev->data_offset;
1918 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1919 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1920 atomic_inc(&r10_bio->remaining);
1921 /* and we write to 'i' */
1923 for (k=0; k<conf->copies; k++)
1924 if (r10_bio->devs[k].devnum == i)
1926 BUG_ON(k == conf->copies);
1927 bio = r10_bio->devs[1].bio;
1928 bio->bi_next = biolist;
1930 bio->bi_private = r10_bio;
1931 bio->bi_end_io = end_sync_write;
1933 bio->bi_sector = r10_bio->devs[k].addr +
1934 conf->mirrors[i].rdev->data_offset;
1935 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1937 r10_bio->devs[0].devnum = d;
1938 r10_bio->devs[1].devnum = i;
1942 if (j == conf->copies) {
1943 /* Cannot recover, so abort the recovery */
1946 atomic_dec(&rb2->remaining);
1948 if (!test_and_set_bit(MD_RECOVERY_INTR,
1950 printk(KERN_INFO "md/raid10:%s: insufficient "
1951 "working devices for recovery.\n",
1956 if (biolist == NULL) {
1958 r10bio_t *rb2 = r10_bio;
1959 r10_bio = (r10bio_t*) rb2->master_bio;
1960 rb2->master_bio = NULL;
1966 /* resync. Schedule a read for every block at this virt offset */
1969 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1971 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1972 &sync_blocks, mddev->degraded) &&
1973 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
1974 &mddev->recovery)) {
1975 /* We can skip this block */
1977 return sync_blocks + sectors_skipped;
1979 if (sync_blocks < max_sync)
1980 max_sync = sync_blocks;
1981 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1983 r10_bio->mddev = mddev;
1984 atomic_set(&r10_bio->remaining, 0);
1985 raise_barrier(conf, 0);
1986 conf->next_resync = sector_nr;
1988 r10_bio->master_bio = NULL;
1989 r10_bio->sector = sector_nr;
1990 set_bit(R10BIO_IsSync, &r10_bio->state);
1991 raid10_find_phys(conf, r10_bio);
1992 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1994 for (i=0; i<conf->copies; i++) {
1995 int d = r10_bio->devs[i].devnum;
1996 bio = r10_bio->devs[i].bio;
1997 bio->bi_end_io = NULL;
1998 clear_bit(BIO_UPTODATE, &bio->bi_flags);
1999 if (conf->mirrors[d].rdev == NULL ||
2000 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2002 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2003 atomic_inc(&r10_bio->remaining);
2004 bio->bi_next = biolist;
2006 bio->bi_private = r10_bio;
2007 bio->bi_end_io = end_sync_read;
2009 bio->bi_sector = r10_bio->devs[i].addr +
2010 conf->mirrors[d].rdev->data_offset;
2011 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2016 for (i=0; i<conf->copies; i++) {
2017 int d = r10_bio->devs[i].devnum;
2018 if (r10_bio->devs[i].bio->bi_end_io)
2019 rdev_dec_pending(conf->mirrors[d].rdev,
2028 for (bio = biolist; bio ; bio=bio->bi_next) {
2030 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2032 bio->bi_flags |= 1 << BIO_UPTODATE;
2035 bio->bi_phys_segments = 0;
2040 if (sector_nr + max_sync < max_sector)
2041 max_sector = sector_nr + max_sync;
2044 int len = PAGE_SIZE;
2045 if (sector_nr + (len>>9) > max_sector)
2046 len = (max_sector - sector_nr) << 9;
2049 for (bio= biolist ; bio ; bio=bio->bi_next) {
2051 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2052 if (bio_add_page(bio, page, len, 0))
2056 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2057 for (bio2 = biolist;
2058 bio2 && bio2 != bio;
2059 bio2 = bio2->bi_next) {
2060 /* remove last page from this bio */
2062 bio2->bi_size -= len;
2063 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2067 nr_sectors += len>>9;
2068 sector_nr += len>>9;
2069 } while (biolist->bi_vcnt < RESYNC_PAGES);
2071 r10_bio->sectors = nr_sectors;
2075 biolist = biolist->bi_next;
2077 bio->bi_next = NULL;
2078 r10_bio = bio->bi_private;
2079 r10_bio->sectors = nr_sectors;
2081 if (bio->bi_end_io == end_sync_read) {
2082 md_sync_acct(bio->bi_bdev, nr_sectors);
2083 generic_make_request(bio);
2087 if (sectors_skipped)
2088 /* pretend they weren't skipped, it makes
2089 * no important difference in this case
2091 md_done_sync(mddev, sectors_skipped, 1);
2093 return sectors_skipped + nr_sectors;
2095 /* There is nowhere to write, so all non-sync
2096 * drives must be failed, so try the next chunk...
2098 if (sector_nr + max_sync < max_sector)
2099 max_sector = sector_nr + max_sync;
2101 sectors_skipped += (max_sector - sector_nr);
2103 sector_nr = max_sector;
2108 raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2111 conf_t *conf = mddev->private;
2114 raid_disks = conf->raid_disks;
2116 sectors = conf->dev_sectors;
2118 size = sectors >> conf->chunk_shift;
2119 sector_div(size, conf->far_copies);
2120 size = size * raid_disks;
2121 sector_div(size, conf->near_copies);
2123 return size << conf->chunk_shift;
2127 static conf_t *setup_conf(mddev_t *mddev)
2129 conf_t *conf = NULL;
2131 sector_t stride, size;
2134 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2135 !is_power_of_2(mddev->new_chunk_sectors)) {
2136 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2137 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2138 mdname(mddev), PAGE_SIZE);
2142 nc = mddev->new_layout & 255;
2143 fc = (mddev->new_layout >> 8) & 255;
2144 fo = mddev->new_layout & (1<<16);
2146 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2147 (mddev->new_layout >> 17)) {
2148 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2149 mdname(mddev), mddev->new_layout);
2154 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2158 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2163 conf->tmppage = alloc_page(GFP_KERNEL);
2168 conf->raid_disks = mddev->raid_disks;
2169 conf->near_copies = nc;
2170 conf->far_copies = fc;
2171 conf->copies = nc*fc;
2172 conf->far_offset = fo;
2173 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2174 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2176 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2177 r10bio_pool_free, conf);
2178 if (!conf->r10bio_pool)
2181 size = mddev->dev_sectors >> conf->chunk_shift;
2182 sector_div(size, fc);
2183 size = size * conf->raid_disks;
2184 sector_div(size, nc);
2185 /* 'size' is now the number of chunks in the array */
2186 /* calculate "used chunks per device" in 'stride' */
2187 stride = size * conf->copies;
2189 /* We need to round up when dividing by raid_disks to
2190 * get the stride size.
2192 stride += conf->raid_disks - 1;
2193 sector_div(stride, conf->raid_disks);
2195 conf->dev_sectors = stride << conf->chunk_shift;
2200 sector_div(stride, fc);
2201 conf->stride = stride << conf->chunk_shift;
2204 spin_lock_init(&conf->device_lock);
2205 INIT_LIST_HEAD(&conf->retry_list);
2207 spin_lock_init(&conf->resync_lock);
2208 init_waitqueue_head(&conf->wait_barrier);
2210 conf->thread = md_register_thread(raid10d, mddev, NULL);
2214 conf->mddev = mddev;
2218 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
2221 if (conf->r10bio_pool)
2222 mempool_destroy(conf->r10bio_pool);
2223 kfree(conf->mirrors);
2224 safe_put_page(conf->tmppage);
2227 return ERR_PTR(err);
2230 static int run(mddev_t *mddev)
2233 int i, disk_idx, chunk_size;
2234 mirror_info_t *disk;
2239 * copy the already verified devices into our private RAID10
2240 * bookkeeping area. [whatever we allocate in run(),
2241 * should be freed in stop()]
2244 if (mddev->private == NULL) {
2245 conf = setup_conf(mddev);
2247 return PTR_ERR(conf);
2248 mddev->private = conf;
2250 conf = mddev->private;
2254 mddev->thread = conf->thread;
2255 conf->thread = NULL;
2257 chunk_size = mddev->chunk_sectors << 9;
2258 blk_queue_io_min(mddev->queue, chunk_size);
2259 if (conf->raid_disks % conf->near_copies)
2260 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2262 blk_queue_io_opt(mddev->queue, chunk_size *
2263 (conf->raid_disks / conf->near_copies));
2265 list_for_each_entry(rdev, &mddev->disks, same_set) {
2266 disk_idx = rdev->raid_disk;
2267 if (disk_idx >= conf->raid_disks
2270 disk = conf->mirrors + disk_idx;
2273 disk_stack_limits(mddev->gendisk, rdev->bdev,
2274 rdev->data_offset << 9);
2275 /* as we don't honour merge_bvec_fn, we must never risk
2276 * violating it, so limit max_segments to 1 lying
2277 * within a single page.
2279 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2280 blk_queue_max_segments(mddev->queue, 1);
2281 blk_queue_segment_boundary(mddev->queue,
2282 PAGE_CACHE_SIZE - 1);
2285 disk->head_position = 0;
2287 /* need to check that every block has at least one working mirror */
2288 if (!enough(conf, -1)) {
2289 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
2294 mddev->degraded = 0;
2295 for (i = 0; i < conf->raid_disks; i++) {
2297 disk = conf->mirrors + i;
2300 !test_bit(In_sync, &disk->rdev->flags)) {
2301 disk->head_position = 0;
2308 if (mddev->recovery_cp != MaxSector)
2309 printk(KERN_NOTICE "md/raid10:%s: not clean"
2310 " -- starting background reconstruction\n",
2313 "md/raid10:%s: active with %d out of %d devices\n",
2314 mdname(mddev), conf->raid_disks - mddev->degraded,
2317 * Ok, everything is just fine now
2319 mddev->dev_sectors = conf->dev_sectors;
2320 size = raid10_size(mddev, 0, 0);
2321 md_set_array_sectors(mddev, size);
2322 mddev->resync_max_sectors = size;
2324 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2325 mddev->queue->backing_dev_info.congested_data = mddev;
2327 /* Calculate max read-ahead size.
2328 * We need to readahead at least twice a whole stripe....
2332 int stripe = conf->raid_disks *
2333 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2334 stripe /= conf->near_copies;
2335 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2336 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2339 if (conf->near_copies < conf->raid_disks)
2340 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2342 if (md_integrity_register(mddev))
2348 md_unregister_thread(mddev->thread);
2349 if (conf->r10bio_pool)
2350 mempool_destroy(conf->r10bio_pool);
2351 safe_put_page(conf->tmppage);
2352 kfree(conf->mirrors);
2354 mddev->private = NULL;
2359 static int stop(mddev_t *mddev)
2361 conf_t *conf = mddev->private;
2363 raise_barrier(conf, 0);
2364 lower_barrier(conf);
2366 md_unregister_thread(mddev->thread);
2367 mddev->thread = NULL;
2368 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2369 if (conf->r10bio_pool)
2370 mempool_destroy(conf->r10bio_pool);
2371 kfree(conf->mirrors);
2373 mddev->private = NULL;
2377 static void raid10_quiesce(mddev_t *mddev, int state)
2379 conf_t *conf = mddev->private;
2383 raise_barrier(conf, 0);
2386 lower_barrier(conf);
2391 static void *raid10_takeover_raid0(mddev_t *mddev)
2396 if (mddev->degraded > 0) {
2397 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2399 return ERR_PTR(-EINVAL);
2402 /* Set new parameters */
2403 mddev->new_level = 10;
2404 /* new layout: far_copies = 1, near_copies = 2 */
2405 mddev->new_layout = (1<<8) + 2;
2406 mddev->new_chunk_sectors = mddev->chunk_sectors;
2407 mddev->delta_disks = mddev->raid_disks;
2408 mddev->raid_disks *= 2;
2409 /* make sure it will be not marked as dirty */
2410 mddev->recovery_cp = MaxSector;
2412 conf = setup_conf(mddev);
2413 if (!IS_ERR(conf)) {
2414 list_for_each_entry(rdev, &mddev->disks, same_set)
2415 if (rdev->raid_disk >= 0)
2416 rdev->new_raid_disk = rdev->raid_disk * 2;
2423 static void *raid10_takeover(mddev_t *mddev)
2425 struct raid0_private_data *raid0_priv;
2427 /* raid10 can take over:
2428 * raid0 - providing it has only two drives
2430 if (mddev->level == 0) {
2431 /* for raid0 takeover only one zone is supported */
2432 raid0_priv = mddev->private;
2433 if (raid0_priv->nr_strip_zones > 1) {
2434 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2435 " with more than one zone.\n",
2437 return ERR_PTR(-EINVAL);
2439 return raid10_takeover_raid0(mddev);
2441 return ERR_PTR(-EINVAL);
2444 static struct mdk_personality raid10_personality =
2448 .owner = THIS_MODULE,
2449 .make_request = make_request,
2453 .error_handler = error,
2454 .hot_add_disk = raid10_add_disk,
2455 .hot_remove_disk= raid10_remove_disk,
2456 .spare_active = raid10_spare_active,
2457 .sync_request = sync_request,
2458 .quiesce = raid10_quiesce,
2459 .size = raid10_size,
2460 .takeover = raid10_takeover,
2463 static int __init raid_init(void)
2465 return register_md_personality(&raid10_personality);
2468 static void raid_exit(void)
2470 unregister_md_personality(&raid10_personality);
2473 module_init(raid_init);
2474 module_exit(raid_exit);
2475 MODULE_LICENSE("GPL");
2476 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2477 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2478 MODULE_ALIAS("md-raid10");
2479 MODULE_ALIAS("md-level-10");