2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "dm-bio-list.h"
22 #include <linux/raid/raid10.h>
23 #include <linux/raid/bitmap.h>
26 * RAID10 provides a combination of RAID0 and RAID1 functionality.
27 * The layout of data is defined by
30 * near_copies (stored in low byte of layout)
31 * far_copies (stored in second byte of layout)
33 * The data to be stored is divided into chunks using chunksize.
34 * Each device is divided into far_copies sections.
35 * In each section, chunks are laid out in a style similar to raid0, but
36 * near_copies copies of each chunk is stored (each on a different drive).
37 * The starting device for each section is offset near_copies from the starting
38 * device of the previous section.
39 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
41 * near_copies and far_copies must be at least one, and their product is at most
46 * Number of guaranteed r10bios in case of extreme VM load:
48 #define NR_RAID10_BIOS 256
50 static void unplug_slaves(mddev_t *mddev);
52 static void allow_barrier(conf_t *conf);
53 static void lower_barrier(conf_t *conf);
55 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
59 int size = offsetof(struct r10bio_s, devs[conf->copies]);
61 /* allocate a r10bio with room for raid_disks entries in the bios array */
62 r10_bio = kmalloc(size, gfp_flags);
64 memset(r10_bio, 0, size);
66 unplug_slaves(conf->mddev);
71 static void r10bio_pool_free(void *r10_bio, void *data)
76 #define RESYNC_BLOCK_SIZE (64*1024)
77 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
78 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
79 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
80 #define RESYNC_WINDOW (2048*1024)
83 * When performing a resync, we need to read and compare, so
84 * we need as many pages are there are copies.
85 * When performing a recovery, we need 2 bios, one for read,
86 * one for write (we recover only one drive per r10buf)
89 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
98 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
100 unplug_slaves(conf->mddev);
104 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
105 nalloc = conf->copies; /* resync */
107 nalloc = 2; /* recovery */
112 for (j = nalloc ; j-- ; ) {
113 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
116 r10_bio->devs[j].bio = bio;
119 * Allocate RESYNC_PAGES data pages and attach them
122 for (j = 0 ; j < nalloc; j++) {
123 bio = r10_bio->devs[j].bio;
124 for (i = 0; i < RESYNC_PAGES; i++) {
125 page = alloc_page(gfp_flags);
129 bio->bi_io_vec[i].bv_page = page;
137 __free_page(bio->bi_io_vec[i-1].bv_page);
139 for (i = 0; i < RESYNC_PAGES ; i++)
140 __free_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
143 while ( ++j < nalloc )
144 bio_put(r10_bio->devs[j].bio);
145 r10bio_pool_free(r10_bio, conf);
149 static void r10buf_pool_free(void *__r10_bio, void *data)
153 r10bio_t *r10bio = __r10_bio;
156 for (j=0; j < conf->copies; j++) {
157 struct bio *bio = r10bio->devs[j].bio;
159 for (i = 0; i < RESYNC_PAGES; i++) {
160 __free_page(bio->bi_io_vec[i].bv_page);
161 bio->bi_io_vec[i].bv_page = NULL;
166 r10bio_pool_free(r10bio, conf);
169 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
173 for (i = 0; i < conf->copies; i++) {
174 struct bio **bio = & r10_bio->devs[i].bio;
181 static inline void free_r10bio(r10bio_t *r10_bio)
183 conf_t *conf = mddev_to_conf(r10_bio->mddev);
186 * Wake up any possible resync thread that waits for the device
191 put_all_bios(conf, r10_bio);
192 mempool_free(r10_bio, conf->r10bio_pool);
195 static inline void put_buf(r10bio_t *r10_bio)
197 conf_t *conf = mddev_to_conf(r10_bio->mddev);
199 mempool_free(r10_bio, conf->r10buf_pool);
204 static void reschedule_retry(r10bio_t *r10_bio)
207 mddev_t *mddev = r10_bio->mddev;
208 conf_t *conf = mddev_to_conf(mddev);
210 spin_lock_irqsave(&conf->device_lock, flags);
211 list_add(&r10_bio->retry_list, &conf->retry_list);
212 spin_unlock_irqrestore(&conf->device_lock, flags);
214 md_wakeup_thread(mddev->thread);
218 * raid_end_bio_io() is called when we have finished servicing a mirrored
219 * operation and are ready to return a success/failure code to the buffer
222 static void raid_end_bio_io(r10bio_t *r10_bio)
224 struct bio *bio = r10_bio->master_bio;
226 bio_endio(bio, bio->bi_size,
227 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
228 free_r10bio(r10_bio);
232 * Update disk head position estimator based on IRQ completion info.
234 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
236 conf_t *conf = mddev_to_conf(r10_bio->mddev);
238 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
239 r10_bio->devs[slot].addr + (r10_bio->sectors);
242 static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
244 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
245 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
247 conf_t *conf = mddev_to_conf(r10_bio->mddev);
252 slot = r10_bio->read_slot;
253 dev = r10_bio->devs[slot].devnum;
255 * this branch is our 'one mirror IO has finished' event handler:
258 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
261 * Set R10BIO_Uptodate in our master bio, so that
262 * we will return a good error code to the higher
263 * levels even if IO on some other mirrored buffer fails.
265 * The 'master' represents the composite IO operation to
266 * user-side. So if something waits for IO, then it will
267 * wait for the 'master' bio.
269 set_bit(R10BIO_Uptodate, &r10_bio->state);
271 update_head_pos(slot, r10_bio);
274 * we have only one bio on the read side
277 raid_end_bio_io(r10_bio);
282 char b[BDEVNAME_SIZE];
283 if (printk_ratelimit())
284 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
285 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
286 reschedule_retry(r10_bio);
289 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
293 static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
295 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
296 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
298 conf_t *conf = mddev_to_conf(r10_bio->mddev);
303 for (slot = 0; slot < conf->copies; slot++)
304 if (r10_bio->devs[slot].bio == bio)
306 dev = r10_bio->devs[slot].devnum;
309 * this branch is our 'one mirror IO has finished' event handler:
312 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
313 /* an I/O failed, we can't clear the bitmap */
314 set_bit(R10BIO_Degraded, &r10_bio->state);
317 * Set R10BIO_Uptodate in our master bio, so that
318 * we will return a good error code for to the higher
319 * levels even if IO on some other mirrored buffer fails.
321 * The 'master' represents the composite IO operation to
322 * user-side. So if something waits for IO, then it will
323 * wait for the 'master' bio.
325 set_bit(R10BIO_Uptodate, &r10_bio->state);
327 update_head_pos(slot, r10_bio);
331 * Let's see if all mirrored write operations have finished
334 if (atomic_dec_and_test(&r10_bio->remaining)) {
335 /* clear the bitmap if all writes complete successfully */
336 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
338 !test_bit(R10BIO_Degraded, &r10_bio->state),
340 md_write_end(r10_bio->mddev);
341 raid_end_bio_io(r10_bio);
344 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
350 * RAID10 layout manager
351 * Aswell as the chunksize and raid_disks count, there are two
352 * parameters: near_copies and far_copies.
353 * near_copies * far_copies must be <= raid_disks.
354 * Normally one of these will be 1.
355 * If both are 1, we get raid0.
356 * If near_copies == raid_disks, we get raid1.
358 * Chunks are layed out in raid0 style with near_copies copies of the
359 * first chunk, followed by near_copies copies of the next chunk and
361 * If far_copies > 1, then after 1/far_copies of the array has been assigned
362 * as described above, we start again with a device offset of near_copies.
363 * So we effectively have another copy of the whole array further down all
364 * the drives, but with blocks on different drives.
365 * With this layout, and block is never stored twice on the one device.
367 * raid10_find_phys finds the sector offset of a given virtual sector
368 * on each device that it is on. If a block isn't on a device,
369 * that entry in the array is set to MaxSector.
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);
393 sector += stripe << conf->chunk_shift;
395 /* and calculate all the others */
396 for (n=0; n < conf->near_copies; n++) {
399 r10bio->devs[slot].addr = sector;
400 r10bio->devs[slot].devnum = d;
403 for (f = 1; f < conf->far_copies; f++) {
404 d += conf->near_copies;
405 if (d >= conf->raid_disks)
406 d -= conf->raid_disks;
408 r10bio->devs[slot].devnum = d;
409 r10bio->devs[slot].addr = s;
413 if (dev >= conf->raid_disks) {
415 sector += (conf->chunk_mask + 1);
418 BUG_ON(slot != conf->copies);
421 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
423 sector_t offset, chunk, vchunk;
425 while (sector > conf->stride) {
426 sector -= conf->stride;
427 if (dev < conf->near_copies)
428 dev += conf->raid_disks - conf->near_copies;
430 dev -= conf->near_copies;
433 offset = sector & conf->chunk_mask;
434 chunk = sector >> conf->chunk_shift;
435 vchunk = chunk * conf->raid_disks + dev;
436 sector_div(vchunk, conf->near_copies);
437 return (vchunk << conf->chunk_shift) + offset;
441 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
443 * @bio: the buffer head that's been built up so far
444 * @biovec: the request that could be merged to it.
446 * Return amount of bytes we can accept at this offset
447 * If near_copies == raid_disk, there are no striping issues,
448 * but in that case, the function isn't called at all.
450 static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
451 struct bio_vec *bio_vec)
453 mddev_t *mddev = q->queuedata;
454 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
456 unsigned int chunk_sectors = mddev->chunk_size >> 9;
457 unsigned int bio_sectors = bio->bi_size >> 9;
459 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
460 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
461 if (max <= bio_vec->bv_len && bio_sectors == 0)
462 return bio_vec->bv_len;
468 * This routine returns the disk from which the requested read should
469 * be done. There is a per-array 'next expected sequential IO' sector
470 * number - if this matches on the next IO then we use the last disk.
471 * There is also a per-disk 'last know head position' sector that is
472 * maintained from IRQ contexts, both the normal and the resync IO
473 * completion handlers update this position correctly. If there is no
474 * perfect sequential match then we pick the disk whose head is closest.
476 * If there are 2 mirrors in the same 2 devices, performance degrades
477 * because position is mirror, not device based.
479 * The rdev for the device selected will have nr_pending incremented.
483 * FIXME: possibly should rethink readbalancing and do it differently
484 * depending on near_copies / far_copies geometry.
486 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
488 const unsigned long this_sector = r10_bio->sector;
489 int disk, slot, nslot;
490 const int sectors = r10_bio->sectors;
491 sector_t new_distance, current_distance;
494 raid10_find_phys(conf, r10_bio);
497 * Check if we can balance. We can balance on the whole
498 * device if no resync is going on (recovery is ok), or below
499 * the resync window. We take the first readable disk when
500 * above the resync window.
502 if (conf->mddev->recovery_cp < MaxSector
503 && (this_sector + sectors >= conf->next_resync)) {
504 /* make sure that disk is operational */
506 disk = r10_bio->devs[slot].devnum;
508 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
509 !test_bit(In_sync, &rdev->flags)) {
511 if (slot == conf->copies) {
516 disk = r10_bio->devs[slot].devnum;
522 /* make sure the disk is operational */
524 disk = r10_bio->devs[slot].devnum;
525 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
526 !test_bit(In_sync, &rdev->flags)) {
528 if (slot == conf->copies) {
532 disk = r10_bio->devs[slot].devnum;
536 current_distance = abs(r10_bio->devs[slot].addr -
537 conf->mirrors[disk].head_position);
539 /* Find the disk whose head is closest */
541 for (nslot = slot; nslot < conf->copies; nslot++) {
542 int ndisk = r10_bio->devs[nslot].devnum;
545 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
546 !test_bit(In_sync, &rdev->flags))
549 /* This optimisation is debatable, and completely destroys
550 * sequential read speed for 'far copies' arrays. So only
551 * keep it for 'near' arrays, and review those later.
553 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
558 new_distance = abs(r10_bio->devs[nslot].addr -
559 conf->mirrors[ndisk].head_position);
560 if (new_distance < current_distance) {
561 current_distance = new_distance;
568 r10_bio->read_slot = slot;
569 /* conf->next_seq_sect = this_sector + sectors;*/
571 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
572 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
578 static void unplug_slaves(mddev_t *mddev)
580 conf_t *conf = mddev_to_conf(mddev);
584 for (i=0; i<mddev->raid_disks; i++) {
585 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
586 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
587 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
589 atomic_inc(&rdev->nr_pending);
592 if (r_queue->unplug_fn)
593 r_queue->unplug_fn(r_queue);
595 rdev_dec_pending(rdev, mddev);
602 static void raid10_unplug(request_queue_t *q)
604 mddev_t *mddev = q->queuedata;
606 unplug_slaves(q->queuedata);
607 md_wakeup_thread(mddev->thread);
610 static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
611 sector_t *error_sector)
613 mddev_t *mddev = q->queuedata;
614 conf_t *conf = mddev_to_conf(mddev);
618 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
619 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
620 if (rdev && !test_bit(Faulty, &rdev->flags)) {
621 struct block_device *bdev = rdev->bdev;
622 request_queue_t *r_queue = bdev_get_queue(bdev);
624 if (!r_queue->issue_flush_fn)
627 atomic_inc(&rdev->nr_pending);
629 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
631 rdev_dec_pending(rdev, mddev);
641 * Sometimes we need to suspend IO while we do something else,
642 * either some resync/recovery, or reconfigure the array.
643 * To do this we raise a 'barrier'.
644 * The 'barrier' is a counter that can be raised multiple times
645 * to count how many activities are happening which preclude
647 * We can only raise the barrier if there is no pending IO.
648 * i.e. if nr_pending == 0.
649 * We choose only to raise the barrier if no-one is waiting for the
650 * barrier to go down. This means that as soon as an IO request
651 * is ready, no other operations which require a barrier will start
652 * until the IO request has had a chance.
654 * So: regular IO calls 'wait_barrier'. When that returns there
655 * is no backgroup IO happening, It must arrange to call
656 * allow_barrier when it has finished its IO.
657 * backgroup IO calls must call raise_barrier. Once that returns
658 * there is no normal IO happeing. It must arrange to call
659 * lower_barrier when the particular background IO completes.
661 #define RESYNC_DEPTH 32
663 static void raise_barrier(conf_t *conf, int force)
665 BUG_ON(force && !conf->barrier);
666 spin_lock_irq(&conf->resync_lock);
668 /* Wait until no block IO is waiting (unless 'force') */
669 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
671 raid10_unplug(conf->mddev->queue));
673 /* block any new IO from starting */
676 /* No wait for all pending IO to complete */
677 wait_event_lock_irq(conf->wait_barrier,
678 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
680 raid10_unplug(conf->mddev->queue));
682 spin_unlock_irq(&conf->resync_lock);
685 static void lower_barrier(conf_t *conf)
688 spin_lock_irqsave(&conf->resync_lock, flags);
690 spin_unlock_irqrestore(&conf->resync_lock, flags);
691 wake_up(&conf->wait_barrier);
694 static void wait_barrier(conf_t *conf)
696 spin_lock_irq(&conf->resync_lock);
699 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
701 raid10_unplug(conf->mddev->queue));
705 spin_unlock_irq(&conf->resync_lock);
708 static void allow_barrier(conf_t *conf)
711 spin_lock_irqsave(&conf->resync_lock, flags);
713 spin_unlock_irqrestore(&conf->resync_lock, flags);
714 wake_up(&conf->wait_barrier);
717 static int make_request(request_queue_t *q, struct bio * bio)
719 mddev_t *mddev = q->queuedata;
720 conf_t *conf = mddev_to_conf(mddev);
721 mirror_info_t *mirror;
723 struct bio *read_bio;
725 int chunk_sects = conf->chunk_mask + 1;
726 const int rw = bio_data_dir(bio);
730 if (unlikely(bio_barrier(bio))) {
731 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
735 /* If this request crosses a chunk boundary, we need to
736 * split it. This will only happen for 1 PAGE (or less) requests.
738 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
740 conf->near_copies < conf->raid_disks)) {
742 /* Sanity check -- queue functions should prevent this happening */
743 if (bio->bi_vcnt != 1 ||
746 /* This is a one page bio that upper layers
747 * refuse to split for us, so we need to split it.
749 bp = bio_split(bio, bio_split_pool,
750 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
751 if (make_request(q, &bp->bio1))
752 generic_make_request(&bp->bio1);
753 if (make_request(q, &bp->bio2))
754 generic_make_request(&bp->bio2);
756 bio_pair_release(bp);
759 printk("raid10_make_request bug: can't convert block across chunks"
760 " or bigger than %dk %llu %d\n", chunk_sects/2,
761 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
763 bio_io_error(bio, bio->bi_size);
767 md_write_start(mddev, bio);
770 * Register the new request and wait if the reconstruction
771 * thread has put up a bar for new requests.
772 * Continue immediately if no resync is active currently.
776 disk_stat_inc(mddev->gendisk, ios[rw]);
777 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
779 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
781 r10_bio->master_bio = bio;
782 r10_bio->sectors = bio->bi_size >> 9;
784 r10_bio->mddev = mddev;
785 r10_bio->sector = bio->bi_sector;
790 * read balancing logic:
792 int disk = read_balance(conf, r10_bio);
793 int slot = r10_bio->read_slot;
795 raid_end_bio_io(r10_bio);
798 mirror = conf->mirrors + disk;
800 read_bio = bio_clone(bio, GFP_NOIO);
802 r10_bio->devs[slot].bio = read_bio;
804 read_bio->bi_sector = r10_bio->devs[slot].addr +
805 mirror->rdev->data_offset;
806 read_bio->bi_bdev = mirror->rdev->bdev;
807 read_bio->bi_end_io = raid10_end_read_request;
808 read_bio->bi_rw = READ;
809 read_bio->bi_private = r10_bio;
811 generic_make_request(read_bio);
818 /* first select target devices under spinlock and
819 * inc refcount on their rdev. Record them by setting
822 raid10_find_phys(conf, r10_bio);
824 for (i = 0; i < conf->copies; i++) {
825 int d = r10_bio->devs[i].devnum;
826 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
828 !test_bit(Faulty, &rdev->flags)) {
829 atomic_inc(&rdev->nr_pending);
830 r10_bio->devs[i].bio = bio;
832 r10_bio->devs[i].bio = NULL;
833 set_bit(R10BIO_Degraded, &r10_bio->state);
838 atomic_set(&r10_bio->remaining, 0);
841 for (i = 0; i < conf->copies; i++) {
843 int d = r10_bio->devs[i].devnum;
844 if (!r10_bio->devs[i].bio)
847 mbio = bio_clone(bio, GFP_NOIO);
848 r10_bio->devs[i].bio = mbio;
850 mbio->bi_sector = r10_bio->devs[i].addr+
851 conf->mirrors[d].rdev->data_offset;
852 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
853 mbio->bi_end_io = raid10_end_write_request;
855 mbio->bi_private = r10_bio;
857 atomic_inc(&r10_bio->remaining);
858 bio_list_add(&bl, mbio);
861 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
862 spin_lock_irqsave(&conf->device_lock, flags);
863 bio_list_merge(&conf->pending_bio_list, &bl);
864 blk_plug_device(mddev->queue);
865 spin_unlock_irqrestore(&conf->device_lock, flags);
870 static void status(struct seq_file *seq, mddev_t *mddev)
872 conf_t *conf = mddev_to_conf(mddev);
875 if (conf->near_copies < conf->raid_disks)
876 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
877 if (conf->near_copies > 1)
878 seq_printf(seq, " %d near-copies", conf->near_copies);
879 if (conf->far_copies > 1)
880 seq_printf(seq, " %d far-copies", conf->far_copies);
882 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
883 conf->working_disks);
884 for (i = 0; i < conf->raid_disks; i++)
885 seq_printf(seq, "%s",
886 conf->mirrors[i].rdev &&
887 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
888 seq_printf(seq, "]");
891 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
893 char b[BDEVNAME_SIZE];
894 conf_t *conf = mddev_to_conf(mddev);
897 * If it is not operational, then we have already marked it as dead
898 * else if it is the last working disks, ignore the error, let the
899 * next level up know.
900 * else mark the drive as failed
902 if (test_bit(In_sync, &rdev->flags)
903 && conf->working_disks == 1)
905 * Don't fail the drive, just return an IO error.
906 * The test should really be more sophisticated than
907 * "working_disks == 1", but it isn't critical, and
908 * can wait until we do more sophisticated "is the drive
909 * really dead" tests...
912 if (test_bit(In_sync, &rdev->flags)) {
914 conf->working_disks--;
916 * if recovery is running, make sure it aborts.
918 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
920 clear_bit(In_sync, &rdev->flags);
921 set_bit(Faulty, &rdev->flags);
923 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
924 " Operation continuing on %d devices\n",
925 bdevname(rdev->bdev,b), conf->working_disks);
928 static void print_conf(conf_t *conf)
933 printk("RAID10 conf printout:\n");
938 printk(" --- wd:%d rd:%d\n", conf->working_disks,
941 for (i = 0; i < conf->raid_disks; i++) {
942 char b[BDEVNAME_SIZE];
943 tmp = conf->mirrors + i;
945 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
946 i, !test_bit(In_sync, &tmp->rdev->flags),
947 !test_bit(Faulty, &tmp->rdev->flags),
948 bdevname(tmp->rdev->bdev,b));
952 static void close_sync(conf_t *conf)
957 mempool_destroy(conf->r10buf_pool);
958 conf->r10buf_pool = NULL;
961 /* check if there are enough drives for
962 * every block to appear on atleast one
964 static int enough(conf_t *conf)
969 int n = conf->copies;
972 if (conf->mirrors[first].rdev)
974 first = (first+1) % conf->raid_disks;
978 } while (first != 0);
982 static int raid10_spare_active(mddev_t *mddev)
985 conf_t *conf = mddev->private;
989 * Find all non-in_sync disks within the RAID10 configuration
990 * and mark them in_sync
992 for (i = 0; i < conf->raid_disks; i++) {
993 tmp = conf->mirrors + i;
995 && !test_bit(Faulty, &tmp->rdev->flags)
996 && !test_bit(In_sync, &tmp->rdev->flags)) {
997 conf->working_disks++;
999 set_bit(In_sync, &tmp->rdev->flags);
1008 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1010 conf_t *conf = mddev->private;
1015 if (mddev->recovery_cp < MaxSector)
1016 /* only hot-add to in-sync arrays, as recovery is
1017 * very different from resync
1023 if (rdev->saved_raid_disk >= 0 &&
1024 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1025 mirror = rdev->saved_raid_disk;
1028 for ( ; mirror < mddev->raid_disks; mirror++)
1029 if ( !(p=conf->mirrors+mirror)->rdev) {
1031 blk_queue_stack_limits(mddev->queue,
1032 rdev->bdev->bd_disk->queue);
1033 /* as we don't honour merge_bvec_fn, we must never risk
1034 * violating it, so limit ->max_sector to one PAGE, as
1035 * a one page request is never in violation.
1037 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1038 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1039 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1041 p->head_position = 0;
1042 rdev->raid_disk = mirror;
1044 if (rdev->saved_raid_disk != mirror)
1046 rcu_assign_pointer(p->rdev, rdev);
1054 static int raid10_remove_disk(mddev_t *mddev, int number)
1056 conf_t *conf = mddev->private;
1059 mirror_info_t *p = conf->mirrors+ number;
1064 if (test_bit(In_sync, &rdev->flags) ||
1065 atomic_read(&rdev->nr_pending)) {
1071 if (atomic_read(&rdev->nr_pending)) {
1072 /* lost the race, try later */
1084 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1086 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1087 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1088 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1094 for (i=0; i<conf->copies; i++)
1095 if (r10_bio->devs[i].bio == bio)
1097 if (i == conf->copies)
1099 update_head_pos(i, r10_bio);
1100 d = r10_bio->devs[i].devnum;
1102 md_error(r10_bio->mddev,
1103 conf->mirrors[d].rdev);
1105 /* for reconstruct, we always reschedule after a read.
1106 * for resync, only after all reads
1108 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1109 atomic_dec_and_test(&r10_bio->remaining)) {
1110 /* we have read all the blocks,
1111 * do the comparison in process context in raid10d
1113 reschedule_retry(r10_bio);
1115 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1119 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1121 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1122 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1123 mddev_t *mddev = r10_bio->mddev;
1124 conf_t *conf = mddev_to_conf(mddev);
1130 for (i = 0; i < conf->copies; i++)
1131 if (r10_bio->devs[i].bio == bio)
1133 d = r10_bio->devs[i].devnum;
1136 md_error(mddev, conf->mirrors[d].rdev);
1137 update_head_pos(i, r10_bio);
1139 while (atomic_dec_and_test(&r10_bio->remaining)) {
1140 if (r10_bio->master_bio == NULL) {
1141 /* the primary of several recovery bios */
1142 md_done_sync(mddev, r10_bio->sectors, 1);
1146 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1151 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1156 * Note: sync and recover and handled very differently for raid10
1157 * This code is for resync.
1158 * For resync, we read through virtual addresses and read all blocks.
1159 * If there is any error, we schedule a write. The lowest numbered
1160 * drive is authoritative.
1161 * However requests come for physical address, so we need to map.
1162 * For every physical address there are raid_disks/copies virtual addresses,
1163 * which is always are least one, but is not necessarly an integer.
1164 * This means that a physical address can span multiple chunks, so we may
1165 * have to submit multiple io requests for a single sync request.
1168 * We check if all blocks are in-sync and only write to blocks that
1171 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1173 conf_t *conf = mddev_to_conf(mddev);
1175 struct bio *tbio, *fbio;
1177 atomic_set(&r10_bio->remaining, 1);
1179 /* find the first device with a block */
1180 for (i=0; i<conf->copies; i++)
1181 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1184 if (i == conf->copies)
1188 fbio = r10_bio->devs[i].bio;
1190 /* now find blocks with errors */
1191 for (i=first+1 ; i < conf->copies ; i++) {
1194 if (!test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1196 /* We know that the bi_io_vec layout is the same for
1197 * both 'first' and 'i', so we just compare them.
1198 * All vec entries are PAGE_SIZE;
1200 tbio = r10_bio->devs[i].bio;
1201 vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1202 for (j = 0; j < vcnt; j++)
1203 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1204 page_address(tbio->bi_io_vec[j].bv_page),
1209 /* Ok, we need to write this bio
1210 * First we need to fixup bv_offset, bv_len and
1211 * bi_vecs, as the read request might have corrupted these
1213 tbio->bi_vcnt = vcnt;
1214 tbio->bi_size = r10_bio->sectors << 9;
1216 tbio->bi_phys_segments = 0;
1217 tbio->bi_hw_segments = 0;
1218 tbio->bi_hw_front_size = 0;
1219 tbio->bi_hw_back_size = 0;
1220 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1221 tbio->bi_flags |= 1 << BIO_UPTODATE;
1222 tbio->bi_next = NULL;
1223 tbio->bi_rw = WRITE;
1224 tbio->bi_private = r10_bio;
1225 tbio->bi_sector = r10_bio->devs[i].addr;
1227 for (j=0; j < vcnt ; j++) {
1228 tbio->bi_io_vec[j].bv_offset = 0;
1229 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1231 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1232 page_address(fbio->bi_io_vec[j].bv_page),
1235 tbio->bi_end_io = end_sync_write;
1237 d = r10_bio->devs[i].devnum;
1238 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1239 atomic_inc(&r10_bio->remaining);
1240 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1242 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1243 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1244 generic_make_request(tbio);
1248 if (atomic_dec_and_test(&r10_bio->remaining)) {
1249 md_done_sync(mddev, r10_bio->sectors, 1);
1255 * Now for the recovery code.
1256 * Recovery happens across physical sectors.
1257 * We recover all non-is_sync drives by finding the virtual address of
1258 * each, and then choose a working drive that also has that virt address.
1259 * There is a separate r10_bio for each non-in_sync drive.
1260 * Only the first two slots are in use. The first for reading,
1261 * The second for writing.
1265 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1267 conf_t *conf = mddev_to_conf(mddev);
1269 struct bio *bio, *wbio;
1272 /* move the pages across to the second bio
1273 * and submit the write request
1275 bio = r10_bio->devs[0].bio;
1276 wbio = r10_bio->devs[1].bio;
1277 for (i=0; i < wbio->bi_vcnt; i++) {
1278 struct page *p = bio->bi_io_vec[i].bv_page;
1279 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1280 wbio->bi_io_vec[i].bv_page = p;
1282 d = r10_bio->devs[1].devnum;
1284 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1285 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1286 generic_make_request(wbio);
1291 * This is a kernel thread which:
1293 * 1. Retries failed read operations on working mirrors.
1294 * 2. Updates the raid superblock when problems encounter.
1295 * 3. Performs writes following reads for array syncronising.
1298 static void raid10d(mddev_t *mddev)
1302 unsigned long flags;
1303 conf_t *conf = mddev_to_conf(mddev);
1304 struct list_head *head = &conf->retry_list;
1308 md_check_recovery(mddev);
1311 char b[BDEVNAME_SIZE];
1312 spin_lock_irqsave(&conf->device_lock, flags);
1314 if (conf->pending_bio_list.head) {
1315 bio = bio_list_get(&conf->pending_bio_list);
1316 blk_remove_plug(mddev->queue);
1317 spin_unlock_irqrestore(&conf->device_lock, flags);
1318 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1319 if (bitmap_unplug(mddev->bitmap) != 0)
1320 printk("%s: bitmap file write failed!\n", mdname(mddev));
1322 while (bio) { /* submit pending writes */
1323 struct bio *next = bio->bi_next;
1324 bio->bi_next = NULL;
1325 generic_make_request(bio);
1333 if (list_empty(head))
1335 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1336 list_del(head->prev);
1337 spin_unlock_irqrestore(&conf->device_lock, flags);
1339 mddev = r10_bio->mddev;
1340 conf = mddev_to_conf(mddev);
1341 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1342 sync_request_write(mddev, r10_bio);
1344 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1345 recovery_request_write(mddev, r10_bio);
1349 bio = r10_bio->devs[r10_bio->read_slot].bio;
1350 r10_bio->devs[r10_bio->read_slot].bio = NULL;
1352 mirror = read_balance(conf, r10_bio);
1354 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1355 " read error for block %llu\n",
1356 bdevname(bio->bi_bdev,b),
1357 (unsigned long long)r10_bio->sector);
1358 raid_end_bio_io(r10_bio);
1360 rdev = conf->mirrors[mirror].rdev;
1361 if (printk_ratelimit())
1362 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1363 " another mirror\n",
1364 bdevname(rdev->bdev,b),
1365 (unsigned long long)r10_bio->sector);
1366 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1367 r10_bio->devs[r10_bio->read_slot].bio = bio;
1368 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1369 + rdev->data_offset;
1370 bio->bi_bdev = rdev->bdev;
1372 bio->bi_private = r10_bio;
1373 bio->bi_end_io = raid10_end_read_request;
1375 generic_make_request(bio);
1379 spin_unlock_irqrestore(&conf->device_lock, flags);
1381 unplug_slaves(mddev);
1385 static int init_resync(conf_t *conf)
1389 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1390 if (conf->r10buf_pool)
1392 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1393 if (!conf->r10buf_pool)
1395 conf->next_resync = 0;
1400 * perform a "sync" on one "block"
1402 * We need to make sure that no normal I/O request - particularly write
1403 * requests - conflict with active sync requests.
1405 * This is achieved by tracking pending requests and a 'barrier' concept
1406 * that can be installed to exclude normal IO requests.
1408 * Resync and recovery are handled very differently.
1409 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1411 * For resync, we iterate over virtual addresses, read all copies,
1412 * and update if there are differences. If only one copy is live,
1414 * For recovery, we iterate over physical addresses, read a good
1415 * value for each non-in_sync drive, and over-write.
1417 * So, for recovery we may have several outstanding complex requests for a
1418 * given address, one for each out-of-sync device. We model this by allocating
1419 * a number of r10_bio structures, one for each out-of-sync device.
1420 * As we setup these structures, we collect all bio's together into a list
1421 * which we then process collectively to add pages, and then process again
1422 * to pass to generic_make_request.
1424 * The r10_bio structures are linked using a borrowed master_bio pointer.
1425 * This link is counted in ->remaining. When the r10_bio that points to NULL
1426 * has its remaining count decremented to 0, the whole complex operation
1431 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1433 conf_t *conf = mddev_to_conf(mddev);
1435 struct bio *biolist = NULL, *bio;
1436 sector_t max_sector, nr_sectors;
1442 sector_t sectors_skipped = 0;
1443 int chunks_skipped = 0;
1445 if (!conf->r10buf_pool)
1446 if (init_resync(conf))
1450 max_sector = mddev->size << 1;
1451 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1452 max_sector = mddev->resync_max_sectors;
1453 if (sector_nr >= max_sector) {
1454 /* If we aborted, we need to abort the
1455 * sync on the 'current' bitmap chucks (there can
1456 * be several when recovering multiple devices).
1457 * as we may have started syncing it but not finished.
1458 * We can find the current address in
1459 * mddev->curr_resync, but for recovery,
1460 * we need to convert that to several
1461 * virtual addresses.
1463 if (mddev->curr_resync < max_sector) { /* aborted */
1464 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1465 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1467 else for (i=0; i<conf->raid_disks; i++) {
1469 raid10_find_virt(conf, mddev->curr_resync, i);
1470 bitmap_end_sync(mddev->bitmap, sect,
1473 } else /* completed sync */
1476 bitmap_close_sync(mddev->bitmap);
1479 return sectors_skipped;
1481 if (chunks_skipped >= conf->raid_disks) {
1482 /* if there has been nothing to do on any drive,
1483 * then there is nothing to do at all..
1486 return (max_sector - sector_nr) + sectors_skipped;
1489 /* make sure whole request will fit in a chunk - if chunks
1492 if (conf->near_copies < conf->raid_disks &&
1493 max_sector > (sector_nr | conf->chunk_mask))
1494 max_sector = (sector_nr | conf->chunk_mask) + 1;
1496 * If there is non-resync activity waiting for us then
1497 * put in a delay to throttle resync.
1499 if (!go_faster && conf->nr_waiting)
1500 msleep_interruptible(1000);
1502 /* Again, very different code for resync and recovery.
1503 * Both must result in an r10bio with a list of bios that
1504 * have bi_end_io, bi_sector, bi_bdev set,
1505 * and bi_private set to the r10bio.
1506 * For recovery, we may actually create several r10bios
1507 * with 2 bios in each, that correspond to the bios in the main one.
1508 * In this case, the subordinate r10bios link back through a
1509 * borrowed master_bio pointer, and the counter in the master
1510 * includes a ref from each subordinate.
1512 /* First, we decide what to do and set ->bi_end_io
1513 * To end_sync_read if we want to read, and
1514 * end_sync_write if we will want to write.
1517 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1518 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1519 /* recovery... the complicated one */
1523 for (i=0 ; i<conf->raid_disks; i++)
1524 if (conf->mirrors[i].rdev &&
1525 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1526 int still_degraded = 0;
1527 /* want to reconstruct this device */
1528 r10bio_t *rb2 = r10_bio;
1529 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1531 /* Unless we are doing a full sync, we only need
1532 * to recover the block if it is set in the bitmap
1534 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1536 if (sync_blocks < max_sync)
1537 max_sync = sync_blocks;
1540 /* yep, skip the sync_blocks here, but don't assume
1541 * that there will never be anything to do here
1543 chunks_skipped = -1;
1547 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1548 raise_barrier(conf, rb2 != NULL);
1549 atomic_set(&r10_bio->remaining, 0);
1551 r10_bio->master_bio = (struct bio*)rb2;
1553 atomic_inc(&rb2->remaining);
1554 r10_bio->mddev = mddev;
1555 set_bit(R10BIO_IsRecover, &r10_bio->state);
1556 r10_bio->sector = sect;
1558 raid10_find_phys(conf, r10_bio);
1559 /* Need to check if this section will still be
1562 for (j=0; j<conf->copies;j++) {
1563 int d = r10_bio->devs[j].devnum;
1564 if (conf->mirrors[d].rdev == NULL ||
1565 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1568 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1569 &sync_blocks, still_degraded);
1571 for (j=0; j<conf->copies;j++) {
1572 int d = r10_bio->devs[j].devnum;
1573 if (conf->mirrors[d].rdev &&
1574 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1575 /* This is where we read from */
1576 bio = r10_bio->devs[0].bio;
1577 bio->bi_next = biolist;
1579 bio->bi_private = r10_bio;
1580 bio->bi_end_io = end_sync_read;
1582 bio->bi_sector = r10_bio->devs[j].addr +
1583 conf->mirrors[d].rdev->data_offset;
1584 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1585 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1586 atomic_inc(&r10_bio->remaining);
1587 /* and we write to 'i' */
1589 for (k=0; k<conf->copies; k++)
1590 if (r10_bio->devs[k].devnum == i)
1592 bio = r10_bio->devs[1].bio;
1593 bio->bi_next = biolist;
1595 bio->bi_private = r10_bio;
1596 bio->bi_end_io = end_sync_write;
1598 bio->bi_sector = r10_bio->devs[k].addr +
1599 conf->mirrors[i].rdev->data_offset;
1600 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1602 r10_bio->devs[0].devnum = d;
1603 r10_bio->devs[1].devnum = i;
1608 if (j == conf->copies) {
1609 /* Cannot recover, so abort the recovery */
1612 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1613 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1618 if (biolist == NULL) {
1620 r10bio_t *rb2 = r10_bio;
1621 r10_bio = (r10bio_t*) rb2->master_bio;
1622 rb2->master_bio = NULL;
1628 /* resync. Schedule a read for every block at this virt offset */
1631 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1632 &sync_blocks, mddev->degraded) &&
1633 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1634 /* We can skip this block */
1636 return sync_blocks + sectors_skipped;
1638 if (sync_blocks < max_sync)
1639 max_sync = sync_blocks;
1640 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1642 r10_bio->mddev = mddev;
1643 atomic_set(&r10_bio->remaining, 0);
1644 raise_barrier(conf, 0);
1645 conf->next_resync = sector_nr;
1647 r10_bio->master_bio = NULL;
1648 r10_bio->sector = sector_nr;
1649 set_bit(R10BIO_IsSync, &r10_bio->state);
1650 raid10_find_phys(conf, r10_bio);
1651 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1653 for (i=0; i<conf->copies; i++) {
1654 int d = r10_bio->devs[i].devnum;
1655 bio = r10_bio->devs[i].bio;
1656 bio->bi_end_io = NULL;
1657 if (conf->mirrors[d].rdev == NULL ||
1658 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1660 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1661 atomic_inc(&r10_bio->remaining);
1662 bio->bi_next = biolist;
1664 bio->bi_private = r10_bio;
1665 bio->bi_end_io = end_sync_read;
1667 bio->bi_sector = r10_bio->devs[i].addr +
1668 conf->mirrors[d].rdev->data_offset;
1669 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1674 for (i=0; i<conf->copies; i++) {
1675 int d = r10_bio->devs[i].devnum;
1676 if (r10_bio->devs[i].bio->bi_end_io)
1677 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1685 for (bio = biolist; bio ; bio=bio->bi_next) {
1687 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1689 bio->bi_flags |= 1 << BIO_UPTODATE;
1692 bio->bi_phys_segments = 0;
1693 bio->bi_hw_segments = 0;
1698 if (sector_nr + max_sync < max_sector)
1699 max_sector = sector_nr + max_sync;
1702 int len = PAGE_SIZE;
1704 if (sector_nr + (len>>9) > max_sector)
1705 len = (max_sector - sector_nr) << 9;
1708 for (bio= biolist ; bio ; bio=bio->bi_next) {
1709 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1710 if (bio_add_page(bio, page, len, 0) == 0) {
1713 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1714 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1715 /* remove last page from this bio */
1717 bio2->bi_size -= len;
1718 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1724 nr_sectors += len>>9;
1725 sector_nr += len>>9;
1726 } while (biolist->bi_vcnt < RESYNC_PAGES);
1728 r10_bio->sectors = nr_sectors;
1732 biolist = biolist->bi_next;
1734 bio->bi_next = NULL;
1735 r10_bio = bio->bi_private;
1736 r10_bio->sectors = nr_sectors;
1738 if (bio->bi_end_io == end_sync_read) {
1739 md_sync_acct(bio->bi_bdev, nr_sectors);
1740 generic_make_request(bio);
1744 if (sectors_skipped)
1745 /* pretend they weren't skipped, it makes
1746 * no important difference in this case
1748 md_done_sync(mddev, sectors_skipped, 1);
1750 return sectors_skipped + nr_sectors;
1752 /* There is nowhere to write, so all non-sync
1753 * drives must be failed, so try the next chunk...
1756 sector_t sec = max_sector - sector_nr;
1757 sectors_skipped += sec;
1759 sector_nr = max_sector;
1764 static int run(mddev_t *mddev)
1768 mirror_info_t *disk;
1770 struct list_head *tmp;
1772 sector_t stride, size;
1774 if (mddev->level != 10) {
1775 printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
1776 mdname(mddev), mddev->level);
1779 nc = mddev->layout & 255;
1780 fc = (mddev->layout >> 8) & 255;
1781 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1782 (mddev->layout >> 16)) {
1783 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1784 mdname(mddev), mddev->layout);
1788 * copy the already verified devices into our private RAID10
1789 * bookkeeping area. [whatever we allocate in run(),
1790 * should be freed in stop()]
1792 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1793 mddev->private = conf;
1795 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1799 memset(conf, 0, sizeof(*conf));
1800 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1802 if (!conf->mirrors) {
1803 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1807 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1809 conf->near_copies = nc;
1810 conf->far_copies = fc;
1811 conf->copies = nc*fc;
1812 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1813 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1814 stride = mddev->size >> (conf->chunk_shift-1);
1815 sector_div(stride, fc);
1816 conf->stride = stride << conf->chunk_shift;
1818 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1819 r10bio_pool_free, conf);
1820 if (!conf->r10bio_pool) {
1821 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1826 ITERATE_RDEV(mddev, rdev, tmp) {
1827 disk_idx = rdev->raid_disk;
1828 if (disk_idx >= mddev->raid_disks
1831 disk = conf->mirrors + disk_idx;
1835 blk_queue_stack_limits(mddev->queue,
1836 rdev->bdev->bd_disk->queue);
1837 /* as we don't honour merge_bvec_fn, we must never risk
1838 * violating it, so limit ->max_sector to one PAGE, as
1839 * a one page request is never in violation.
1841 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1842 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1843 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1845 disk->head_position = 0;
1846 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1847 conf->working_disks++;
1849 conf->raid_disks = mddev->raid_disks;
1850 conf->mddev = mddev;
1851 spin_lock_init(&conf->device_lock);
1852 INIT_LIST_HEAD(&conf->retry_list);
1854 spin_lock_init(&conf->resync_lock);
1855 init_waitqueue_head(&conf->wait_barrier);
1857 /* need to check that every block has at least one working mirror */
1858 if (!enough(conf)) {
1859 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
1864 mddev->degraded = 0;
1865 for (i = 0; i < conf->raid_disks; i++) {
1867 disk = conf->mirrors + i;
1870 disk->head_position = 0;
1876 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
1877 if (!mddev->thread) {
1879 "raid10: couldn't allocate thread for %s\n",
1885 "raid10: raid set %s active with %d out of %d devices\n",
1886 mdname(mddev), mddev->raid_disks - mddev->degraded,
1889 * Ok, everything is just fine now
1891 size = conf->stride * conf->raid_disks;
1892 sector_div(size, conf->near_copies);
1893 mddev->array_size = size/2;
1894 mddev->resync_max_sectors = size;
1896 mddev->queue->unplug_fn = raid10_unplug;
1897 mddev->queue->issue_flush_fn = raid10_issue_flush;
1899 /* Calculate max read-ahead size.
1900 * We need to readahead at least twice a whole stripe....
1904 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
1905 stripe /= conf->near_copies;
1906 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
1907 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
1910 if (conf->near_copies < mddev->raid_disks)
1911 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1915 if (conf->r10bio_pool)
1916 mempool_destroy(conf->r10bio_pool);
1917 kfree(conf->mirrors);
1919 mddev->private = NULL;
1924 static int stop(mddev_t *mddev)
1926 conf_t *conf = mddev_to_conf(mddev);
1928 md_unregister_thread(mddev->thread);
1929 mddev->thread = NULL;
1930 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1931 if (conf->r10bio_pool)
1932 mempool_destroy(conf->r10bio_pool);
1933 kfree(conf->mirrors);
1935 mddev->private = NULL;
1939 static void raid10_quiesce(mddev_t *mddev, int state)
1941 conf_t *conf = mddev_to_conf(mddev);
1945 raise_barrier(conf, 0);
1948 lower_barrier(conf);
1951 if (mddev->thread) {
1953 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1955 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1956 md_wakeup_thread(mddev->thread);
1960 static mdk_personality_t raid10_personality =
1963 .owner = THIS_MODULE,
1964 .make_request = make_request,
1968 .error_handler = error,
1969 .hot_add_disk = raid10_add_disk,
1970 .hot_remove_disk= raid10_remove_disk,
1971 .spare_active = raid10_spare_active,
1972 .sync_request = sync_request,
1973 .quiesce = raid10_quiesce,
1976 static int __init raid_init(void)
1978 return register_md_personality(RAID10, &raid10_personality);
1981 static void raid_exit(void)
1983 unregister_md_personality(RAID10);
1986 module_init(raid_init);
1987 module_exit(raid_exit);
1988 MODULE_LICENSE("GPL");
1989 MODULE_ALIAS("md-personality-9"); /* RAID10 */