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 mddev->resync_mismatches += r10_bio->sectors;
1210 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1211 /* Don't fix anything. */
1213 /* Ok, we need to write this bio
1214 * First we need to fixup bv_offset, bv_len and
1215 * bi_vecs, as the read request might have corrupted these
1217 tbio->bi_vcnt = vcnt;
1218 tbio->bi_size = r10_bio->sectors << 9;
1220 tbio->bi_phys_segments = 0;
1221 tbio->bi_hw_segments = 0;
1222 tbio->bi_hw_front_size = 0;
1223 tbio->bi_hw_back_size = 0;
1224 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1225 tbio->bi_flags |= 1 << BIO_UPTODATE;
1226 tbio->bi_next = NULL;
1227 tbio->bi_rw = WRITE;
1228 tbio->bi_private = r10_bio;
1229 tbio->bi_sector = r10_bio->devs[i].addr;
1231 for (j=0; j < vcnt ; j++) {
1232 tbio->bi_io_vec[j].bv_offset = 0;
1233 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1235 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1236 page_address(fbio->bi_io_vec[j].bv_page),
1239 tbio->bi_end_io = end_sync_write;
1241 d = r10_bio->devs[i].devnum;
1242 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1243 atomic_inc(&r10_bio->remaining);
1244 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1246 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1247 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1248 generic_make_request(tbio);
1252 if (atomic_dec_and_test(&r10_bio->remaining)) {
1253 md_done_sync(mddev, r10_bio->sectors, 1);
1259 * Now for the recovery code.
1260 * Recovery happens across physical sectors.
1261 * We recover all non-is_sync drives by finding the virtual address of
1262 * each, and then choose a working drive that also has that virt address.
1263 * There is a separate r10_bio for each non-in_sync drive.
1264 * Only the first two slots are in use. The first for reading,
1265 * The second for writing.
1269 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1271 conf_t *conf = mddev_to_conf(mddev);
1273 struct bio *bio, *wbio;
1276 /* move the pages across to the second bio
1277 * and submit the write request
1279 bio = r10_bio->devs[0].bio;
1280 wbio = r10_bio->devs[1].bio;
1281 for (i=0; i < wbio->bi_vcnt; i++) {
1282 struct page *p = bio->bi_io_vec[i].bv_page;
1283 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1284 wbio->bi_io_vec[i].bv_page = p;
1286 d = r10_bio->devs[1].devnum;
1288 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1289 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1290 generic_make_request(wbio);
1295 * This is a kernel thread which:
1297 * 1. Retries failed read operations on working mirrors.
1298 * 2. Updates the raid superblock when problems encounter.
1299 * 3. Performs writes following reads for array syncronising.
1302 static void raid10d(mddev_t *mddev)
1306 unsigned long flags;
1307 conf_t *conf = mddev_to_conf(mddev);
1308 struct list_head *head = &conf->retry_list;
1312 md_check_recovery(mddev);
1315 char b[BDEVNAME_SIZE];
1316 spin_lock_irqsave(&conf->device_lock, flags);
1318 if (conf->pending_bio_list.head) {
1319 bio = bio_list_get(&conf->pending_bio_list);
1320 blk_remove_plug(mddev->queue);
1321 spin_unlock_irqrestore(&conf->device_lock, flags);
1322 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1323 if (bitmap_unplug(mddev->bitmap) != 0)
1324 printk("%s: bitmap file write failed!\n", mdname(mddev));
1326 while (bio) { /* submit pending writes */
1327 struct bio *next = bio->bi_next;
1328 bio->bi_next = NULL;
1329 generic_make_request(bio);
1337 if (list_empty(head))
1339 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1340 list_del(head->prev);
1341 spin_unlock_irqrestore(&conf->device_lock, flags);
1343 mddev = r10_bio->mddev;
1344 conf = mddev_to_conf(mddev);
1345 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1346 sync_request_write(mddev, r10_bio);
1348 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1349 recovery_request_write(mddev, r10_bio);
1353 bio = r10_bio->devs[r10_bio->read_slot].bio;
1354 r10_bio->devs[r10_bio->read_slot].bio = NULL;
1356 mirror = read_balance(conf, r10_bio);
1358 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1359 " read error for block %llu\n",
1360 bdevname(bio->bi_bdev,b),
1361 (unsigned long long)r10_bio->sector);
1362 raid_end_bio_io(r10_bio);
1364 rdev = conf->mirrors[mirror].rdev;
1365 if (printk_ratelimit())
1366 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1367 " another mirror\n",
1368 bdevname(rdev->bdev,b),
1369 (unsigned long long)r10_bio->sector);
1370 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1371 r10_bio->devs[r10_bio->read_slot].bio = bio;
1372 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1373 + rdev->data_offset;
1374 bio->bi_bdev = rdev->bdev;
1376 bio->bi_private = r10_bio;
1377 bio->bi_end_io = raid10_end_read_request;
1379 generic_make_request(bio);
1383 spin_unlock_irqrestore(&conf->device_lock, flags);
1385 unplug_slaves(mddev);
1389 static int init_resync(conf_t *conf)
1393 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1394 if (conf->r10buf_pool)
1396 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1397 if (!conf->r10buf_pool)
1399 conf->next_resync = 0;
1404 * perform a "sync" on one "block"
1406 * We need to make sure that no normal I/O request - particularly write
1407 * requests - conflict with active sync requests.
1409 * This is achieved by tracking pending requests and a 'barrier' concept
1410 * that can be installed to exclude normal IO requests.
1412 * Resync and recovery are handled very differently.
1413 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1415 * For resync, we iterate over virtual addresses, read all copies,
1416 * and update if there are differences. If only one copy is live,
1418 * For recovery, we iterate over physical addresses, read a good
1419 * value for each non-in_sync drive, and over-write.
1421 * So, for recovery we may have several outstanding complex requests for a
1422 * given address, one for each out-of-sync device. We model this by allocating
1423 * a number of r10_bio structures, one for each out-of-sync device.
1424 * As we setup these structures, we collect all bio's together into a list
1425 * which we then process collectively to add pages, and then process again
1426 * to pass to generic_make_request.
1428 * The r10_bio structures are linked using a borrowed master_bio pointer.
1429 * This link is counted in ->remaining. When the r10_bio that points to NULL
1430 * has its remaining count decremented to 0, the whole complex operation
1435 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1437 conf_t *conf = mddev_to_conf(mddev);
1439 struct bio *biolist = NULL, *bio;
1440 sector_t max_sector, nr_sectors;
1446 sector_t sectors_skipped = 0;
1447 int chunks_skipped = 0;
1449 if (!conf->r10buf_pool)
1450 if (init_resync(conf))
1454 max_sector = mddev->size << 1;
1455 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1456 max_sector = mddev->resync_max_sectors;
1457 if (sector_nr >= max_sector) {
1458 /* If we aborted, we need to abort the
1459 * sync on the 'current' bitmap chucks (there can
1460 * be several when recovering multiple devices).
1461 * as we may have started syncing it but not finished.
1462 * We can find the current address in
1463 * mddev->curr_resync, but for recovery,
1464 * we need to convert that to several
1465 * virtual addresses.
1467 if (mddev->curr_resync < max_sector) { /* aborted */
1468 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1469 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1471 else for (i=0; i<conf->raid_disks; i++) {
1473 raid10_find_virt(conf, mddev->curr_resync, i);
1474 bitmap_end_sync(mddev->bitmap, sect,
1477 } else /* completed sync */
1480 bitmap_close_sync(mddev->bitmap);
1483 return sectors_skipped;
1485 if (chunks_skipped >= conf->raid_disks) {
1486 /* if there has been nothing to do on any drive,
1487 * then there is nothing to do at all..
1490 return (max_sector - sector_nr) + sectors_skipped;
1493 /* make sure whole request will fit in a chunk - if chunks
1496 if (conf->near_copies < conf->raid_disks &&
1497 max_sector > (sector_nr | conf->chunk_mask))
1498 max_sector = (sector_nr | conf->chunk_mask) + 1;
1500 * If there is non-resync activity waiting for us then
1501 * put in a delay to throttle resync.
1503 if (!go_faster && conf->nr_waiting)
1504 msleep_interruptible(1000);
1506 /* Again, very different code for resync and recovery.
1507 * Both must result in an r10bio with a list of bios that
1508 * have bi_end_io, bi_sector, bi_bdev set,
1509 * and bi_private set to the r10bio.
1510 * For recovery, we may actually create several r10bios
1511 * with 2 bios in each, that correspond to the bios in the main one.
1512 * In this case, the subordinate r10bios link back through a
1513 * borrowed master_bio pointer, and the counter in the master
1514 * includes a ref from each subordinate.
1516 /* First, we decide what to do and set ->bi_end_io
1517 * To end_sync_read if we want to read, and
1518 * end_sync_write if we will want to write.
1521 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1522 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1523 /* recovery... the complicated one */
1527 for (i=0 ; i<conf->raid_disks; i++)
1528 if (conf->mirrors[i].rdev &&
1529 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1530 int still_degraded = 0;
1531 /* want to reconstruct this device */
1532 r10bio_t *rb2 = r10_bio;
1533 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1535 /* Unless we are doing a full sync, we only need
1536 * to recover the block if it is set in the bitmap
1538 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1540 if (sync_blocks < max_sync)
1541 max_sync = sync_blocks;
1544 /* yep, skip the sync_blocks here, but don't assume
1545 * that there will never be anything to do here
1547 chunks_skipped = -1;
1551 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1552 raise_barrier(conf, rb2 != NULL);
1553 atomic_set(&r10_bio->remaining, 0);
1555 r10_bio->master_bio = (struct bio*)rb2;
1557 atomic_inc(&rb2->remaining);
1558 r10_bio->mddev = mddev;
1559 set_bit(R10BIO_IsRecover, &r10_bio->state);
1560 r10_bio->sector = sect;
1562 raid10_find_phys(conf, r10_bio);
1563 /* Need to check if this section will still be
1566 for (j=0; j<conf->copies;j++) {
1567 int d = r10_bio->devs[j].devnum;
1568 if (conf->mirrors[d].rdev == NULL ||
1569 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1572 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1573 &sync_blocks, still_degraded);
1575 for (j=0; j<conf->copies;j++) {
1576 int d = r10_bio->devs[j].devnum;
1577 if (conf->mirrors[d].rdev &&
1578 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1579 /* This is where we read from */
1580 bio = r10_bio->devs[0].bio;
1581 bio->bi_next = biolist;
1583 bio->bi_private = r10_bio;
1584 bio->bi_end_io = end_sync_read;
1586 bio->bi_sector = r10_bio->devs[j].addr +
1587 conf->mirrors[d].rdev->data_offset;
1588 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1589 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1590 atomic_inc(&r10_bio->remaining);
1591 /* and we write to 'i' */
1593 for (k=0; k<conf->copies; k++)
1594 if (r10_bio->devs[k].devnum == i)
1596 bio = r10_bio->devs[1].bio;
1597 bio->bi_next = biolist;
1599 bio->bi_private = r10_bio;
1600 bio->bi_end_io = end_sync_write;
1602 bio->bi_sector = r10_bio->devs[k].addr +
1603 conf->mirrors[i].rdev->data_offset;
1604 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1606 r10_bio->devs[0].devnum = d;
1607 r10_bio->devs[1].devnum = i;
1612 if (j == conf->copies) {
1613 /* Cannot recover, so abort the recovery */
1616 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1617 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1622 if (biolist == NULL) {
1624 r10bio_t *rb2 = r10_bio;
1625 r10_bio = (r10bio_t*) rb2->master_bio;
1626 rb2->master_bio = NULL;
1632 /* resync. Schedule a read for every block at this virt offset */
1635 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1636 &sync_blocks, mddev->degraded) &&
1637 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1638 /* We can skip this block */
1640 return sync_blocks + sectors_skipped;
1642 if (sync_blocks < max_sync)
1643 max_sync = sync_blocks;
1644 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1646 r10_bio->mddev = mddev;
1647 atomic_set(&r10_bio->remaining, 0);
1648 raise_barrier(conf, 0);
1649 conf->next_resync = sector_nr;
1651 r10_bio->master_bio = NULL;
1652 r10_bio->sector = sector_nr;
1653 set_bit(R10BIO_IsSync, &r10_bio->state);
1654 raid10_find_phys(conf, r10_bio);
1655 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1657 for (i=0; i<conf->copies; i++) {
1658 int d = r10_bio->devs[i].devnum;
1659 bio = r10_bio->devs[i].bio;
1660 bio->bi_end_io = NULL;
1661 if (conf->mirrors[d].rdev == NULL ||
1662 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1664 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1665 atomic_inc(&r10_bio->remaining);
1666 bio->bi_next = biolist;
1668 bio->bi_private = r10_bio;
1669 bio->bi_end_io = end_sync_read;
1671 bio->bi_sector = r10_bio->devs[i].addr +
1672 conf->mirrors[d].rdev->data_offset;
1673 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1678 for (i=0; i<conf->copies; i++) {
1679 int d = r10_bio->devs[i].devnum;
1680 if (r10_bio->devs[i].bio->bi_end_io)
1681 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1689 for (bio = biolist; bio ; bio=bio->bi_next) {
1691 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1693 bio->bi_flags |= 1 << BIO_UPTODATE;
1696 bio->bi_phys_segments = 0;
1697 bio->bi_hw_segments = 0;
1702 if (sector_nr + max_sync < max_sector)
1703 max_sector = sector_nr + max_sync;
1706 int len = PAGE_SIZE;
1708 if (sector_nr + (len>>9) > max_sector)
1709 len = (max_sector - sector_nr) << 9;
1712 for (bio= biolist ; bio ; bio=bio->bi_next) {
1713 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1714 if (bio_add_page(bio, page, len, 0) == 0) {
1717 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1718 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1719 /* remove last page from this bio */
1721 bio2->bi_size -= len;
1722 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1728 nr_sectors += len>>9;
1729 sector_nr += len>>9;
1730 } while (biolist->bi_vcnt < RESYNC_PAGES);
1732 r10_bio->sectors = nr_sectors;
1736 biolist = biolist->bi_next;
1738 bio->bi_next = NULL;
1739 r10_bio = bio->bi_private;
1740 r10_bio->sectors = nr_sectors;
1742 if (bio->bi_end_io == end_sync_read) {
1743 md_sync_acct(bio->bi_bdev, nr_sectors);
1744 generic_make_request(bio);
1748 if (sectors_skipped)
1749 /* pretend they weren't skipped, it makes
1750 * no important difference in this case
1752 md_done_sync(mddev, sectors_skipped, 1);
1754 return sectors_skipped + nr_sectors;
1756 /* There is nowhere to write, so all non-sync
1757 * drives must be failed, so try the next chunk...
1760 sector_t sec = max_sector - sector_nr;
1761 sectors_skipped += sec;
1763 sector_nr = max_sector;
1768 static int run(mddev_t *mddev)
1772 mirror_info_t *disk;
1774 struct list_head *tmp;
1776 sector_t stride, size;
1778 if (mddev->level != 10) {
1779 printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
1780 mdname(mddev), mddev->level);
1783 nc = mddev->layout & 255;
1784 fc = (mddev->layout >> 8) & 255;
1785 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1786 (mddev->layout >> 16)) {
1787 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1788 mdname(mddev), mddev->layout);
1792 * copy the already verified devices into our private RAID10
1793 * bookkeeping area. [whatever we allocate in run(),
1794 * should be freed in stop()]
1796 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1797 mddev->private = conf;
1799 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1803 memset(conf, 0, sizeof(*conf));
1804 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1806 if (!conf->mirrors) {
1807 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1811 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1813 conf->near_copies = nc;
1814 conf->far_copies = fc;
1815 conf->copies = nc*fc;
1816 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1817 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1818 stride = mddev->size >> (conf->chunk_shift-1);
1819 sector_div(stride, fc);
1820 conf->stride = stride << conf->chunk_shift;
1822 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1823 r10bio_pool_free, conf);
1824 if (!conf->r10bio_pool) {
1825 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1830 ITERATE_RDEV(mddev, rdev, tmp) {
1831 disk_idx = rdev->raid_disk;
1832 if (disk_idx >= mddev->raid_disks
1835 disk = conf->mirrors + disk_idx;
1839 blk_queue_stack_limits(mddev->queue,
1840 rdev->bdev->bd_disk->queue);
1841 /* as we don't honour merge_bvec_fn, we must never risk
1842 * violating it, so limit ->max_sector to one PAGE, as
1843 * a one page request is never in violation.
1845 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1846 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1847 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1849 disk->head_position = 0;
1850 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1851 conf->working_disks++;
1853 conf->raid_disks = mddev->raid_disks;
1854 conf->mddev = mddev;
1855 spin_lock_init(&conf->device_lock);
1856 INIT_LIST_HEAD(&conf->retry_list);
1858 spin_lock_init(&conf->resync_lock);
1859 init_waitqueue_head(&conf->wait_barrier);
1861 /* need to check that every block has at least one working mirror */
1862 if (!enough(conf)) {
1863 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
1868 mddev->degraded = 0;
1869 for (i = 0; i < conf->raid_disks; i++) {
1871 disk = conf->mirrors + i;
1874 disk->head_position = 0;
1880 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
1881 if (!mddev->thread) {
1883 "raid10: couldn't allocate thread for %s\n",
1889 "raid10: raid set %s active with %d out of %d devices\n",
1890 mdname(mddev), mddev->raid_disks - mddev->degraded,
1893 * Ok, everything is just fine now
1895 size = conf->stride * conf->raid_disks;
1896 sector_div(size, conf->near_copies);
1897 mddev->array_size = size/2;
1898 mddev->resync_max_sectors = size;
1900 mddev->queue->unplug_fn = raid10_unplug;
1901 mddev->queue->issue_flush_fn = raid10_issue_flush;
1903 /* Calculate max read-ahead size.
1904 * We need to readahead at least twice a whole stripe....
1908 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
1909 stripe /= conf->near_copies;
1910 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
1911 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
1914 if (conf->near_copies < mddev->raid_disks)
1915 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1919 if (conf->r10bio_pool)
1920 mempool_destroy(conf->r10bio_pool);
1921 kfree(conf->mirrors);
1923 mddev->private = NULL;
1928 static int stop(mddev_t *mddev)
1930 conf_t *conf = mddev_to_conf(mddev);
1932 md_unregister_thread(mddev->thread);
1933 mddev->thread = NULL;
1934 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1935 if (conf->r10bio_pool)
1936 mempool_destroy(conf->r10bio_pool);
1937 kfree(conf->mirrors);
1939 mddev->private = NULL;
1943 static void raid10_quiesce(mddev_t *mddev, int state)
1945 conf_t *conf = mddev_to_conf(mddev);
1949 raise_barrier(conf, 0);
1952 lower_barrier(conf);
1955 if (mddev->thread) {
1957 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1959 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1960 md_wakeup_thread(mddev->thread);
1964 static mdk_personality_t raid10_personality =
1967 .owner = THIS_MODULE,
1968 .make_request = make_request,
1972 .error_handler = error,
1973 .hot_add_disk = raid10_add_disk,
1974 .hot_remove_disk= raid10_remove_disk,
1975 .spare_active = raid10_spare_active,
1976 .sync_request = sync_request,
1977 .quiesce = raid10_quiesce,
1980 static int __init raid_init(void)
1982 return register_md_personality(RAID10, &raid10_personality);
1985 static void raid_exit(void)
1987 unregister_md_personality(RAID10);
1990 module_init(raid_init);
1991 module_exit(raid_exit);
1992 MODULE_LICENSE("GPL");
1993 MODULE_ALIAS("md-personality-9"); /* RAID10 */