2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/raid/raid10.h>
24 * RAID10 provides a combination of RAID0 and RAID1 functionality.
25 * The layout of data is defined by
28 * near_copies (stored in low byte of layout)
29 * far_copies (stored in second byte of layout)
31 * The data to be stored is divided into chunks using chunksize.
32 * Each device is divided into far_copies sections.
33 * In each section, chunks are laid out in a style similar to raid0, but
34 * near_copies copies of each chunk is stored (each on a different drive).
35 * The starting device for each section is offset near_copies from the starting
36 * device of the previous section.
37 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
39 * near_copies and far_copies must be at least one, and their product is at most
44 * Number of guaranteed r10bios in case of extreme VM load:
46 #define NR_RAID10_BIOS 256
48 static void unplug_slaves(mddev_t *mddev);
50 static void allow_barrier(conf_t *conf);
51 static void lower_barrier(conf_t *conf);
53 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
57 int size = offsetof(struct r10bio_s, devs[conf->copies]);
59 /* allocate a r10bio with room for raid_disks entries in the bios array */
60 r10_bio = kmalloc(size, gfp_flags);
62 memset(r10_bio, 0, size);
64 unplug_slaves(conf->mddev);
69 static void r10bio_pool_free(void *r10_bio, void *data)
74 #define RESYNC_BLOCK_SIZE (64*1024)
75 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
76 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
77 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
78 #define RESYNC_WINDOW (2048*1024)
81 * When performing a resync, we need to read and compare, so
82 * we need as many pages are there are copies.
83 * When performing a recovery, we need 2 bios, one for read,
84 * one for write (we recover only one drive per r10buf)
87 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
96 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
98 unplug_slaves(conf->mddev);
102 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
103 nalloc = conf->copies; /* resync */
105 nalloc = 2; /* recovery */
110 for (j = nalloc ; j-- ; ) {
111 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
114 r10_bio->devs[j].bio = bio;
117 * Allocate RESYNC_PAGES data pages and attach them
120 for (j = 0 ; j < nalloc; j++) {
121 bio = r10_bio->devs[j].bio;
122 for (i = 0; i < RESYNC_PAGES; i++) {
123 page = alloc_page(gfp_flags);
127 bio->bi_io_vec[i].bv_page = page;
135 __free_page(bio->bi_io_vec[i-1].bv_page);
137 for (i = 0; i < RESYNC_PAGES ; i++)
138 __free_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
141 while ( ++j < nalloc )
142 bio_put(r10_bio->devs[j].bio);
143 r10bio_pool_free(r10_bio, conf);
147 static void r10buf_pool_free(void *__r10_bio, void *data)
151 r10bio_t *r10bio = __r10_bio;
154 for (j=0; j < conf->copies; j++) {
155 struct bio *bio = r10bio->devs[j].bio;
157 for (i = 0; i < RESYNC_PAGES; i++) {
158 __free_page(bio->bi_io_vec[i].bv_page);
159 bio->bi_io_vec[i].bv_page = NULL;
164 r10bio_pool_free(r10bio, conf);
167 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
171 for (i = 0; i < conf->copies; i++) {
172 struct bio **bio = & r10_bio->devs[i].bio;
179 static inline void free_r10bio(r10bio_t *r10_bio)
181 conf_t *conf = mddev_to_conf(r10_bio->mddev);
184 * Wake up any possible resync thread that waits for the device
189 put_all_bios(conf, r10_bio);
190 mempool_free(r10_bio, conf->r10bio_pool);
193 static inline void put_buf(r10bio_t *r10_bio)
195 conf_t *conf = mddev_to_conf(r10_bio->mddev);
197 mempool_free(r10_bio, conf->r10buf_pool);
202 static void reschedule_retry(r10bio_t *r10_bio)
205 mddev_t *mddev = r10_bio->mddev;
206 conf_t *conf = mddev_to_conf(mddev);
208 spin_lock_irqsave(&conf->device_lock, flags);
209 list_add(&r10_bio->retry_list, &conf->retry_list);
210 spin_unlock_irqrestore(&conf->device_lock, flags);
212 md_wakeup_thread(mddev->thread);
216 * raid_end_bio_io() is called when we have finished servicing a mirrored
217 * operation and are ready to return a success/failure code to the buffer
220 static void raid_end_bio_io(r10bio_t *r10_bio)
222 struct bio *bio = r10_bio->master_bio;
224 bio_endio(bio, bio->bi_size,
225 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
226 free_r10bio(r10_bio);
230 * Update disk head position estimator based on IRQ completion info.
232 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
234 conf_t *conf = mddev_to_conf(r10_bio->mddev);
236 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
237 r10_bio->devs[slot].addr + (r10_bio->sectors);
240 static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
242 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
243 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
245 conf_t *conf = mddev_to_conf(r10_bio->mddev);
250 slot = r10_bio->read_slot;
251 dev = r10_bio->devs[slot].devnum;
253 * this branch is our 'one mirror IO has finished' event handler:
256 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
259 * Set R10BIO_Uptodate in our master bio, so that
260 * we will return a good error code to the higher
261 * levels even if IO on some other mirrored buffer fails.
263 * The 'master' represents the composite IO operation to
264 * user-side. So if something waits for IO, then it will
265 * wait for the 'master' bio.
267 set_bit(R10BIO_Uptodate, &r10_bio->state);
269 update_head_pos(slot, r10_bio);
272 * we have only one bio on the read side
275 raid_end_bio_io(r10_bio);
280 char b[BDEVNAME_SIZE];
281 if (printk_ratelimit())
282 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
283 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
284 reschedule_retry(r10_bio);
287 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
291 static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
293 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
294 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
296 conf_t *conf = mddev_to_conf(r10_bio->mddev);
301 for (slot = 0; slot < conf->copies; slot++)
302 if (r10_bio->devs[slot].bio == bio)
304 dev = r10_bio->devs[slot].devnum;
307 * this branch is our 'one mirror IO has finished' event handler:
310 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
313 * Set R10BIO_Uptodate in our master bio, so that
314 * we will return a good error code for to the higher
315 * levels even if IO on some other mirrored buffer fails.
317 * The 'master' represents the composite IO operation to
318 * user-side. So if something waits for IO, then it will
319 * wait for the 'master' bio.
321 set_bit(R10BIO_Uptodate, &r10_bio->state);
323 update_head_pos(slot, r10_bio);
327 * Let's see if all mirrored write operations have finished
330 if (atomic_dec_and_test(&r10_bio->remaining)) {
331 md_write_end(r10_bio->mddev);
332 raid_end_bio_io(r10_bio);
335 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
341 * RAID10 layout manager
342 * Aswell as the chunksize and raid_disks count, there are two
343 * parameters: near_copies and far_copies.
344 * near_copies * far_copies must be <= raid_disks.
345 * Normally one of these will be 1.
346 * If both are 1, we get raid0.
347 * If near_copies == raid_disks, we get raid1.
349 * Chunks are layed out in raid0 style with near_copies copies of the
350 * first chunk, followed by near_copies copies of the next chunk and
352 * If far_copies > 1, then after 1/far_copies of the array has been assigned
353 * as described above, we start again with a device offset of near_copies.
354 * So we effectively have another copy of the whole array further down all
355 * the drives, but with blocks on different drives.
356 * With this layout, and block is never stored twice on the one device.
358 * raid10_find_phys finds the sector offset of a given virtual sector
359 * on each device that it is on. If a block isn't on a device,
360 * that entry in the array is set to MaxSector.
362 * raid10_find_virt does the reverse mapping, from a device and a
363 * sector offset to a virtual address
366 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
376 /* now calculate first sector/dev */
377 chunk = r10bio->sector >> conf->chunk_shift;
378 sector = r10bio->sector & conf->chunk_mask;
380 chunk *= conf->near_copies;
382 dev = sector_div(stripe, conf->raid_disks);
384 sector += stripe << conf->chunk_shift;
386 /* and calculate all the others */
387 for (n=0; n < conf->near_copies; n++) {
390 r10bio->devs[slot].addr = sector;
391 r10bio->devs[slot].devnum = d;
394 for (f = 1; f < conf->far_copies; f++) {
395 d += conf->near_copies;
396 if (d >= conf->raid_disks)
397 d -= conf->raid_disks;
399 r10bio->devs[slot].devnum = d;
400 r10bio->devs[slot].addr = s;
404 if (dev >= conf->raid_disks) {
406 sector += (conf->chunk_mask + 1);
409 BUG_ON(slot != conf->copies);
412 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
414 sector_t offset, chunk, vchunk;
416 while (sector > conf->stride) {
417 sector -= conf->stride;
418 if (dev < conf->near_copies)
419 dev += conf->raid_disks - conf->near_copies;
421 dev -= conf->near_copies;
424 offset = sector & conf->chunk_mask;
425 chunk = sector >> conf->chunk_shift;
426 vchunk = chunk * conf->raid_disks + dev;
427 sector_div(vchunk, conf->near_copies);
428 return (vchunk << conf->chunk_shift) + offset;
432 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
434 * @bio: the buffer head that's been built up so far
435 * @biovec: the request that could be merged to it.
437 * Return amount of bytes we can accept at this offset
438 * If near_copies == raid_disk, there are no striping issues,
439 * but in that case, the function isn't called at all.
441 static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
442 struct bio_vec *bio_vec)
444 mddev_t *mddev = q->queuedata;
445 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
447 unsigned int chunk_sectors = mddev->chunk_size >> 9;
448 unsigned int bio_sectors = bio->bi_size >> 9;
450 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
451 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
452 if (max <= bio_vec->bv_len && bio_sectors == 0)
453 return bio_vec->bv_len;
459 * This routine returns the disk from which the requested read should
460 * be done. There is a per-array 'next expected sequential IO' sector
461 * number - if this matches on the next IO then we use the last disk.
462 * There is also a per-disk 'last know head position' sector that is
463 * maintained from IRQ contexts, both the normal and the resync IO
464 * completion handlers update this position correctly. If there is no
465 * perfect sequential match then we pick the disk whose head is closest.
467 * If there are 2 mirrors in the same 2 devices, performance degrades
468 * because position is mirror, not device based.
470 * The rdev for the device selected will have nr_pending incremented.
474 * FIXME: possibly should rethink readbalancing and do it differently
475 * depending on near_copies / far_copies geometry.
477 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
479 const unsigned long this_sector = r10_bio->sector;
480 int disk, slot, nslot;
481 const int sectors = r10_bio->sectors;
482 sector_t new_distance, current_distance;
485 raid10_find_phys(conf, r10_bio);
488 * Check if we can balance. We can balance on the whole
489 * device if no resync is going on, or below the resync window.
490 * We take the first readable disk when above the resync window.
492 if (conf->mddev->recovery_cp < MaxSector
493 && (this_sector + sectors >= conf->next_resync)) {
494 /* make sure that disk is operational */
496 disk = r10_bio->devs[slot].devnum;
498 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
499 !test_bit(In_sync, &rdev->flags)) {
501 if (slot == conf->copies) {
506 disk = r10_bio->devs[slot].devnum;
512 /* make sure the disk is operational */
514 disk = r10_bio->devs[slot].devnum;
515 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
516 !test_bit(In_sync, &rdev->flags)) {
518 if (slot == conf->copies) {
522 disk = r10_bio->devs[slot].devnum;
526 current_distance = abs(r10_bio->devs[slot].addr -
527 conf->mirrors[disk].head_position);
529 /* Find the disk whose head is closest */
531 for (nslot = slot; nslot < conf->copies; nslot++) {
532 int ndisk = r10_bio->devs[nslot].devnum;
535 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
536 !test_bit(In_sync, &rdev->flags))
539 /* This optimisation is debatable, and completely destroys
540 * sequential read speed for 'far copies' arrays. So only
541 * keep it for 'near' arrays, and review those later.
543 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
548 new_distance = abs(r10_bio->devs[nslot].addr -
549 conf->mirrors[ndisk].head_position);
550 if (new_distance < current_distance) {
551 current_distance = new_distance;
558 r10_bio->read_slot = slot;
559 /* conf->next_seq_sect = this_sector + sectors;*/
561 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
562 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
568 static void unplug_slaves(mddev_t *mddev)
570 conf_t *conf = mddev_to_conf(mddev);
574 for (i=0; i<mddev->raid_disks; i++) {
575 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
576 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
577 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
579 atomic_inc(&rdev->nr_pending);
582 if (r_queue->unplug_fn)
583 r_queue->unplug_fn(r_queue);
585 rdev_dec_pending(rdev, mddev);
592 static void raid10_unplug(request_queue_t *q)
594 unplug_slaves(q->queuedata);
597 static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
598 sector_t *error_sector)
600 mddev_t *mddev = q->queuedata;
601 conf_t *conf = mddev_to_conf(mddev);
605 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
606 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
607 if (rdev && !test_bit(Faulty, &rdev->flags)) {
608 struct block_device *bdev = rdev->bdev;
609 request_queue_t *r_queue = bdev_get_queue(bdev);
611 if (!r_queue->issue_flush_fn)
614 atomic_inc(&rdev->nr_pending);
616 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
618 rdev_dec_pending(rdev, mddev);
628 * Sometimes we need to suspend IO while we do something else,
629 * either some resync/recovery, or reconfigure the array.
630 * To do this we raise a 'barrier'.
631 * The 'barrier' is a counter that can be raised multiple times
632 * to count how many activities are happening which preclude
634 * We can only raise the barrier if there is no pending IO.
635 * i.e. if nr_pending == 0.
636 * We choose only to raise the barrier if no-one is waiting for the
637 * barrier to go down. This means that as soon as an IO request
638 * is ready, no other operations which require a barrier will start
639 * until the IO request has had a chance.
641 * So: regular IO calls 'wait_barrier'. When that returns there
642 * is no backgroup IO happening, It must arrange to call
643 * allow_barrier when it has finished its IO.
644 * backgroup IO calls must call raise_barrier. Once that returns
645 * there is no normal IO happeing. It must arrange to call
646 * lower_barrier when the particular background IO completes.
648 #define RESYNC_DEPTH 32
650 static void raise_barrier(conf_t *conf)
652 spin_lock_irq(&conf->resync_lock);
654 /* Wait until no block IO is waiting */
655 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
657 raid10_unplug(conf->mddev->queue));
659 /* block any new IO from starting */
662 /* No wait for all pending IO to complete */
663 wait_event_lock_irq(conf->wait_barrier,
664 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
666 raid10_unplug(conf->mddev->queue));
668 spin_unlock_irq(&conf->resync_lock);
671 static void lower_barrier(conf_t *conf)
674 spin_lock_irqsave(&conf->resync_lock, flags);
676 spin_unlock_irqrestore(&conf->resync_lock, flags);
677 wake_up(&conf->wait_barrier);
680 static void wait_barrier(conf_t *conf)
682 spin_lock_irq(&conf->resync_lock);
685 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
687 raid10_unplug(conf->mddev->queue));
691 spin_unlock_irq(&conf->resync_lock);
694 static void allow_barrier(conf_t *conf)
697 spin_lock_irqsave(&conf->resync_lock, flags);
699 spin_unlock_irqrestore(&conf->resync_lock, flags);
700 wake_up(&conf->wait_barrier);
703 static int make_request(request_queue_t *q, struct bio * bio)
705 mddev_t *mddev = q->queuedata;
706 conf_t *conf = mddev_to_conf(mddev);
707 mirror_info_t *mirror;
709 struct bio *read_bio;
711 int chunk_sects = conf->chunk_mask + 1;
712 const int rw = bio_data_dir(bio);
714 if (unlikely(bio_barrier(bio))) {
715 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
719 /* If this request crosses a chunk boundary, we need to
720 * split it. This will only happen for 1 PAGE (or less) requests.
722 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
724 conf->near_copies < conf->raid_disks)) {
726 /* Sanity check -- queue functions should prevent this happening */
727 if (bio->bi_vcnt != 1 ||
730 /* This is a one page bio that upper layers
731 * refuse to split for us, so we need to split it.
733 bp = bio_split(bio, bio_split_pool,
734 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
735 if (make_request(q, &bp->bio1))
736 generic_make_request(&bp->bio1);
737 if (make_request(q, &bp->bio2))
738 generic_make_request(&bp->bio2);
740 bio_pair_release(bp);
743 printk("raid10_make_request bug: can't convert block across chunks"
744 " or bigger than %dk %llu %d\n", chunk_sects/2,
745 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
747 bio_io_error(bio, bio->bi_size);
751 md_write_start(mddev, bio);
754 * Register the new request and wait if the reconstruction
755 * thread has put up a bar for new requests.
756 * Continue immediately if no resync is active currently.
760 disk_stat_inc(mddev->gendisk, ios[rw]);
761 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
763 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
765 r10_bio->master_bio = bio;
766 r10_bio->sectors = bio->bi_size >> 9;
768 r10_bio->mddev = mddev;
769 r10_bio->sector = bio->bi_sector;
773 * read balancing logic:
775 int disk = read_balance(conf, r10_bio);
776 int slot = r10_bio->read_slot;
778 raid_end_bio_io(r10_bio);
781 mirror = conf->mirrors + disk;
783 read_bio = bio_clone(bio, GFP_NOIO);
785 r10_bio->devs[slot].bio = read_bio;
787 read_bio->bi_sector = r10_bio->devs[slot].addr +
788 mirror->rdev->data_offset;
789 read_bio->bi_bdev = mirror->rdev->bdev;
790 read_bio->bi_end_io = raid10_end_read_request;
791 read_bio->bi_rw = READ;
792 read_bio->bi_private = r10_bio;
794 generic_make_request(read_bio);
801 /* first select target devices under spinlock and
802 * inc refcount on their rdev. Record them by setting
805 raid10_find_phys(conf, r10_bio);
807 for (i = 0; i < conf->copies; i++) {
808 int d = r10_bio->devs[i].devnum;
809 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
811 !test_bit(Faulty, &rdev->flags)) {
812 atomic_inc(&rdev->nr_pending);
813 r10_bio->devs[i].bio = bio;
815 r10_bio->devs[i].bio = NULL;
819 atomic_set(&r10_bio->remaining, 1);
821 for (i = 0; i < conf->copies; i++) {
823 int d = r10_bio->devs[i].devnum;
824 if (!r10_bio->devs[i].bio)
827 mbio = bio_clone(bio, GFP_NOIO);
828 r10_bio->devs[i].bio = mbio;
830 mbio->bi_sector = r10_bio->devs[i].addr+
831 conf->mirrors[d].rdev->data_offset;
832 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
833 mbio->bi_end_io = raid10_end_write_request;
835 mbio->bi_private = r10_bio;
837 atomic_inc(&r10_bio->remaining);
838 generic_make_request(mbio);
841 if (atomic_dec_and_test(&r10_bio->remaining)) {
843 raid_end_bio_io(r10_bio);
849 static void status(struct seq_file *seq, mddev_t *mddev)
851 conf_t *conf = mddev_to_conf(mddev);
854 if (conf->near_copies < conf->raid_disks)
855 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
856 if (conf->near_copies > 1)
857 seq_printf(seq, " %d near-copies", conf->near_copies);
858 if (conf->far_copies > 1)
859 seq_printf(seq, " %d far-copies", conf->far_copies);
861 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
862 conf->working_disks);
863 for (i = 0; i < conf->raid_disks; i++)
864 seq_printf(seq, "%s",
865 conf->mirrors[i].rdev &&
866 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
867 seq_printf(seq, "]");
870 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
872 char b[BDEVNAME_SIZE];
873 conf_t *conf = mddev_to_conf(mddev);
876 * If it is not operational, then we have already marked it as dead
877 * else if it is the last working disks, ignore the error, let the
878 * next level up know.
879 * else mark the drive as failed
881 if (test_bit(In_sync, &rdev->flags)
882 && conf->working_disks == 1)
884 * Don't fail the drive, just return an IO error.
885 * The test should really be more sophisticated than
886 * "working_disks == 1", but it isn't critical, and
887 * can wait until we do more sophisticated "is the drive
888 * really dead" tests...
891 if (test_bit(In_sync, &rdev->flags)) {
893 conf->working_disks--;
895 * if recovery is running, make sure it aborts.
897 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
899 clear_bit(In_sync, &rdev->flags);
900 set_bit(Faulty, &rdev->flags);
902 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
903 " Operation continuing on %d devices\n",
904 bdevname(rdev->bdev,b), conf->working_disks);
907 static void print_conf(conf_t *conf)
912 printk("RAID10 conf printout:\n");
917 printk(" --- wd:%d rd:%d\n", conf->working_disks,
920 for (i = 0; i < conf->raid_disks; i++) {
921 char b[BDEVNAME_SIZE];
922 tmp = conf->mirrors + i;
924 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
925 i, !test_bit(In_sync, &tmp->rdev->flags),
926 !test_bit(Faulty, &tmp->rdev->flags),
927 bdevname(tmp->rdev->bdev,b));
931 static void close_sync(conf_t *conf)
936 mempool_destroy(conf->r10buf_pool);
937 conf->r10buf_pool = NULL;
940 /* check if there are enough drives for
941 * every block to appear on atleast one
943 static int enough(conf_t *conf)
948 int n = conf->copies;
951 if (conf->mirrors[first].rdev)
953 first = (first+1) % conf->raid_disks;
957 } while (first != 0);
961 static int raid10_spare_active(mddev_t *mddev)
964 conf_t *conf = mddev->private;
968 * Find all non-in_sync disks within the RAID10 configuration
969 * and mark them in_sync
971 for (i = 0; i < conf->raid_disks; i++) {
972 tmp = conf->mirrors + i;
974 && !test_bit(Faulty, &tmp->rdev->flags)
975 && !test_bit(In_sync, &tmp->rdev->flags)) {
976 conf->working_disks++;
978 set_bit(In_sync, &tmp->rdev->flags);
987 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
989 conf_t *conf = mddev->private;
994 if (mddev->recovery_cp < MaxSector)
995 /* only hot-add to in-sync arrays, as recovery is
996 * very different from resync
1002 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1003 if ( !(p=conf->mirrors+mirror)->rdev) {
1005 blk_queue_stack_limits(mddev->queue,
1006 rdev->bdev->bd_disk->queue);
1007 /* as we don't honour merge_bvec_fn, we must never risk
1008 * violating it, so limit ->max_sector to one PAGE, as
1009 * a one page request is never in violation.
1011 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1012 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1013 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1015 p->head_position = 0;
1016 rdev->raid_disk = mirror;
1018 rcu_assign_pointer(p->rdev, rdev);
1026 static int raid10_remove_disk(mddev_t *mddev, int number)
1028 conf_t *conf = mddev->private;
1031 mirror_info_t *p = conf->mirrors+ number;
1036 if (test_bit(In_sync, &rdev->flags) ||
1037 atomic_read(&rdev->nr_pending)) {
1043 if (atomic_read(&rdev->nr_pending)) {
1044 /* lost the race, try later */
1056 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1058 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1059 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1060 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1066 for (i=0; i<conf->copies; i++)
1067 if (r10_bio->devs[i].bio == bio)
1069 if (i == conf->copies)
1071 update_head_pos(i, r10_bio);
1072 d = r10_bio->devs[i].devnum;
1074 md_error(r10_bio->mddev,
1075 conf->mirrors[d].rdev);
1077 /* for reconstruct, we always reschedule after a read.
1078 * for resync, only after all reads
1080 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1081 atomic_dec_and_test(&r10_bio->remaining)) {
1082 /* we have read all the blocks,
1083 * do the comparison in process context in raid10d
1085 reschedule_retry(r10_bio);
1087 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1091 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1093 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1094 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1095 mddev_t *mddev = r10_bio->mddev;
1096 conf_t *conf = mddev_to_conf(mddev);
1102 for (i = 0; i < conf->copies; i++)
1103 if (r10_bio->devs[i].bio == bio)
1105 d = r10_bio->devs[i].devnum;
1108 md_error(mddev, conf->mirrors[d].rdev);
1109 update_head_pos(i, r10_bio);
1111 while (atomic_dec_and_test(&r10_bio->remaining)) {
1112 if (r10_bio->master_bio == NULL) {
1113 /* the primary of several recovery bios */
1114 md_done_sync(mddev, r10_bio->sectors, 1);
1118 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1123 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1128 * Note: sync and recover and handled very differently for raid10
1129 * This code is for resync.
1130 * For resync, we read through virtual addresses and read all blocks.
1131 * If there is any error, we schedule a write. The lowest numbered
1132 * drive is authoritative.
1133 * However requests come for physical address, so we need to map.
1134 * For every physical address there are raid_disks/copies virtual addresses,
1135 * which is always are least one, but is not necessarly an integer.
1136 * This means that a physical address can span multiple chunks, so we may
1137 * have to submit multiple io requests for a single sync request.
1140 * We check if all blocks are in-sync and only write to blocks that
1143 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1145 conf_t *conf = mddev_to_conf(mddev);
1147 struct bio *tbio, *fbio;
1149 atomic_set(&r10_bio->remaining, 1);
1151 /* find the first device with a block */
1152 for (i=0; i<conf->copies; i++)
1153 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1156 if (i == conf->copies)
1160 fbio = r10_bio->devs[i].bio;
1162 /* now find blocks with errors */
1163 for (i=first+1 ; i < conf->copies ; i++) {
1166 if (!test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1168 /* We know that the bi_io_vec layout is the same for
1169 * both 'first' and 'i', so we just compare them.
1170 * All vec entries are PAGE_SIZE;
1172 tbio = r10_bio->devs[i].bio;
1173 vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1174 for (j = 0; j < vcnt; j++)
1175 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1176 page_address(tbio->bi_io_vec[j].bv_page),
1181 /* Ok, we need to write this bio
1182 * First we need to fixup bv_offset, bv_len and
1183 * bi_vecs, as the read request might have corrupted these
1185 tbio->bi_vcnt = vcnt;
1186 tbio->bi_size = r10_bio->sectors << 9;
1188 tbio->bi_phys_segments = 0;
1189 tbio->bi_hw_segments = 0;
1190 tbio->bi_hw_front_size = 0;
1191 tbio->bi_hw_back_size = 0;
1192 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1193 tbio->bi_flags |= 1 << BIO_UPTODATE;
1194 tbio->bi_next = NULL;
1195 tbio->bi_rw = WRITE;
1196 tbio->bi_private = r10_bio;
1197 tbio->bi_sector = r10_bio->devs[i].addr;
1199 for (j=0; j < vcnt ; j++) {
1200 tbio->bi_io_vec[j].bv_offset = 0;
1201 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1203 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1204 page_address(fbio->bi_io_vec[j].bv_page),
1207 tbio->bi_end_io = end_sync_write;
1209 d = r10_bio->devs[i].devnum;
1210 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1211 atomic_inc(&r10_bio->remaining);
1212 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1214 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1215 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1216 generic_make_request(tbio);
1220 if (atomic_dec_and_test(&r10_bio->remaining)) {
1221 md_done_sync(mddev, r10_bio->sectors, 1);
1227 * Now for the recovery code.
1228 * Recovery happens across physical sectors.
1229 * We recover all non-is_sync drives by finding the virtual address of
1230 * each, and then choose a working drive that also has that virt address.
1231 * There is a separate r10_bio for each non-in_sync drive.
1232 * Only the first two slots are in use. The first for reading,
1233 * The second for writing.
1237 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1239 conf_t *conf = mddev_to_conf(mddev);
1241 struct bio *bio, *wbio;
1244 /* move the pages across to the second bio
1245 * and submit the write request
1247 bio = r10_bio->devs[0].bio;
1248 wbio = r10_bio->devs[1].bio;
1249 for (i=0; i < wbio->bi_vcnt; i++) {
1250 struct page *p = bio->bi_io_vec[i].bv_page;
1251 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1252 wbio->bi_io_vec[i].bv_page = p;
1254 d = r10_bio->devs[1].devnum;
1256 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1257 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1258 generic_make_request(wbio);
1263 * This is a kernel thread which:
1265 * 1. Retries failed read operations on working mirrors.
1266 * 2. Updates the raid superblock when problems encounter.
1267 * 3. Performs writes following reads for array syncronising.
1270 static void raid10d(mddev_t *mddev)
1274 unsigned long flags;
1275 conf_t *conf = mddev_to_conf(mddev);
1276 struct list_head *head = &conf->retry_list;
1280 md_check_recovery(mddev);
1283 char b[BDEVNAME_SIZE];
1284 spin_lock_irqsave(&conf->device_lock, flags);
1285 if (list_empty(head))
1287 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1288 list_del(head->prev);
1289 spin_unlock_irqrestore(&conf->device_lock, flags);
1291 mddev = r10_bio->mddev;
1292 conf = mddev_to_conf(mddev);
1293 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1294 sync_request_write(mddev, r10_bio);
1296 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1297 recovery_request_write(mddev, r10_bio);
1301 bio = r10_bio->devs[r10_bio->read_slot].bio;
1302 r10_bio->devs[r10_bio->read_slot].bio = NULL;
1304 mirror = read_balance(conf, r10_bio);
1306 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1307 " read error for block %llu\n",
1308 bdevname(bio->bi_bdev,b),
1309 (unsigned long long)r10_bio->sector);
1310 raid_end_bio_io(r10_bio);
1312 rdev = conf->mirrors[mirror].rdev;
1313 if (printk_ratelimit())
1314 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1315 " another mirror\n",
1316 bdevname(rdev->bdev,b),
1317 (unsigned long long)r10_bio->sector);
1318 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1319 r10_bio->devs[r10_bio->read_slot].bio = bio;
1320 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1321 + rdev->data_offset;
1322 bio->bi_bdev = rdev->bdev;
1324 bio->bi_private = r10_bio;
1325 bio->bi_end_io = raid10_end_read_request;
1327 generic_make_request(bio);
1331 spin_unlock_irqrestore(&conf->device_lock, flags);
1333 unplug_slaves(mddev);
1337 static int init_resync(conf_t *conf)
1341 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1342 if (conf->r10buf_pool)
1344 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1345 if (!conf->r10buf_pool)
1347 conf->next_resync = 0;
1352 * perform a "sync" on one "block"
1354 * We need to make sure that no normal I/O request - particularly write
1355 * requests - conflict with active sync requests.
1357 * This is achieved by tracking pending requests and a 'barrier' concept
1358 * that can be installed to exclude normal IO requests.
1360 * Resync and recovery are handled very differently.
1361 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1363 * For resync, we iterate over virtual addresses, read all copies,
1364 * and update if there are differences. If only one copy is live,
1366 * For recovery, we iterate over physical addresses, read a good
1367 * value for each non-in_sync drive, and over-write.
1369 * So, for recovery we may have several outstanding complex requests for a
1370 * given address, one for each out-of-sync device. We model this by allocating
1371 * a number of r10_bio structures, one for each out-of-sync device.
1372 * As we setup these structures, we collect all bio's together into a list
1373 * which we then process collectively to add pages, and then process again
1374 * to pass to generic_make_request.
1376 * The r10_bio structures are linked using a borrowed master_bio pointer.
1377 * This link is counted in ->remaining. When the r10_bio that points to NULL
1378 * has its remaining count decremented to 0, the whole complex operation
1383 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1385 conf_t *conf = mddev_to_conf(mddev);
1387 struct bio *biolist = NULL, *bio;
1388 sector_t max_sector, nr_sectors;
1392 sector_t sectors_skipped = 0;
1393 int chunks_skipped = 0;
1395 if (!conf->r10buf_pool)
1396 if (init_resync(conf))
1400 max_sector = mddev->size << 1;
1401 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1402 max_sector = mddev->resync_max_sectors;
1403 if (sector_nr >= max_sector) {
1406 return sectors_skipped;
1408 if (chunks_skipped >= conf->raid_disks) {
1409 /* if there has been nothing to do on any drive,
1410 * then there is nothing to do at all..
1413 return (max_sector - sector_nr) + sectors_skipped;
1416 /* make sure whole request will fit in a chunk - if chunks
1419 if (conf->near_copies < conf->raid_disks &&
1420 max_sector > (sector_nr | conf->chunk_mask))
1421 max_sector = (sector_nr | conf->chunk_mask) + 1;
1423 * If there is non-resync activity waiting for us then
1424 * put in a delay to throttle resync.
1426 if (!go_faster && conf->nr_waiting)
1427 msleep_interruptible(1000);
1428 raise_barrier(conf);
1429 conf->next_resync = sector_nr;
1431 /* Again, very different code for resync and recovery.
1432 * Both must result in an r10bio with a list of bios that
1433 * have bi_end_io, bi_sector, bi_bdev set,
1434 * and bi_private set to the r10bio.
1435 * For recovery, we may actually create several r10bios
1436 * with 2 bios in each, that correspond to the bios in the main one.
1437 * In this case, the subordinate r10bios link back through a
1438 * borrowed master_bio pointer, and the counter in the master
1439 * includes a ref from each subordinate.
1441 /* First, we decide what to do and set ->bi_end_io
1442 * To end_sync_read if we want to read, and
1443 * end_sync_write if we will want to write.
1446 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1447 /* recovery... the complicated one */
1451 for (i=0 ; i<conf->raid_disks; i++)
1452 if (conf->mirrors[i].rdev &&
1453 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1454 /* want to reconstruct this device */
1455 r10bio_t *rb2 = r10_bio;
1457 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1458 spin_lock_irq(&conf->resync_lock);
1459 if (rb2) conf->barrier++;
1460 spin_unlock_irq(&conf->resync_lock);
1461 atomic_set(&r10_bio->remaining, 0);
1463 r10_bio->master_bio = (struct bio*)rb2;
1465 atomic_inc(&rb2->remaining);
1466 r10_bio->mddev = mddev;
1467 set_bit(R10BIO_IsRecover, &r10_bio->state);
1468 r10_bio->sector = raid10_find_virt(conf, sector_nr, i);
1469 raid10_find_phys(conf, r10_bio);
1470 for (j=0; j<conf->copies;j++) {
1471 int d = r10_bio->devs[j].devnum;
1472 if (conf->mirrors[d].rdev &&
1473 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1474 /* This is where we read from */
1475 bio = r10_bio->devs[0].bio;
1476 bio->bi_next = biolist;
1478 bio->bi_private = r10_bio;
1479 bio->bi_end_io = end_sync_read;
1481 bio->bi_sector = r10_bio->devs[j].addr +
1482 conf->mirrors[d].rdev->data_offset;
1483 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1484 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1485 atomic_inc(&r10_bio->remaining);
1486 /* and we write to 'i' */
1488 for (k=0; k<conf->copies; k++)
1489 if (r10_bio->devs[k].devnum == i)
1491 bio = r10_bio->devs[1].bio;
1492 bio->bi_next = biolist;
1494 bio->bi_private = r10_bio;
1495 bio->bi_end_io = end_sync_write;
1497 bio->bi_sector = r10_bio->devs[k].addr +
1498 conf->mirrors[i].rdev->data_offset;
1499 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1501 r10_bio->devs[0].devnum = d;
1502 r10_bio->devs[1].devnum = i;
1507 if (j == conf->copies) {
1508 /* Cannot recover, so abort the recovery */
1511 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1512 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1517 if (biolist == NULL) {
1519 r10bio_t *rb2 = r10_bio;
1520 r10_bio = (r10bio_t*) rb2->master_bio;
1521 rb2->master_bio = NULL;
1527 /* resync. Schedule a read for every block at this virt offset */
1529 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1531 r10_bio->mddev = mddev;
1532 atomic_set(&r10_bio->remaining, 0);
1534 r10_bio->master_bio = NULL;
1535 r10_bio->sector = sector_nr;
1536 set_bit(R10BIO_IsSync, &r10_bio->state);
1537 raid10_find_phys(conf, r10_bio);
1538 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1540 for (i=0; i<conf->copies; i++) {
1541 int d = r10_bio->devs[i].devnum;
1542 bio = r10_bio->devs[i].bio;
1543 bio->bi_end_io = NULL;
1544 if (conf->mirrors[d].rdev == NULL ||
1545 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1547 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1548 atomic_inc(&r10_bio->remaining);
1549 bio->bi_next = biolist;
1551 bio->bi_private = r10_bio;
1552 bio->bi_end_io = end_sync_read;
1554 bio->bi_sector = r10_bio->devs[i].addr +
1555 conf->mirrors[d].rdev->data_offset;
1556 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1561 for (i=0; i<conf->copies; i++) {
1562 int d = r10_bio->devs[i].devnum;
1563 if (r10_bio->devs[i].bio->bi_end_io)
1564 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1572 for (bio = biolist; bio ; bio=bio->bi_next) {
1574 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1576 bio->bi_flags |= 1 << BIO_UPTODATE;
1579 bio->bi_phys_segments = 0;
1580 bio->bi_hw_segments = 0;
1587 int len = PAGE_SIZE;
1589 if (sector_nr + (len>>9) > max_sector)
1590 len = (max_sector - sector_nr) << 9;
1593 for (bio= biolist ; bio ; bio=bio->bi_next) {
1594 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1595 if (bio_add_page(bio, page, len, 0) == 0) {
1598 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1599 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1600 /* remove last page from this bio */
1602 bio2->bi_size -= len;
1603 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1609 nr_sectors += len>>9;
1610 sector_nr += len>>9;
1611 } while (biolist->bi_vcnt < RESYNC_PAGES);
1613 r10_bio->sectors = nr_sectors;
1617 biolist = biolist->bi_next;
1619 bio->bi_next = NULL;
1620 r10_bio = bio->bi_private;
1621 r10_bio->sectors = nr_sectors;
1623 if (bio->bi_end_io == end_sync_read) {
1624 md_sync_acct(bio->bi_bdev, nr_sectors);
1625 generic_make_request(bio);
1629 if (sectors_skipped)
1630 /* pretend they weren't skipped, it makes
1631 * no important difference in this case
1633 md_done_sync(mddev, sectors_skipped, 1);
1635 return sectors_skipped + nr_sectors;
1637 /* There is nowhere to write, so all non-sync
1638 * drives must be failed, so try the next chunk...
1641 sector_t sec = max_sector - sector_nr;
1642 sectors_skipped += sec;
1644 sector_nr = max_sector;
1649 static int run(mddev_t *mddev)
1653 mirror_info_t *disk;
1655 struct list_head *tmp;
1657 sector_t stride, size;
1659 if (mddev->level != 10) {
1660 printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
1661 mdname(mddev), mddev->level);
1664 nc = mddev->layout & 255;
1665 fc = (mddev->layout >> 8) & 255;
1666 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1667 (mddev->layout >> 16)) {
1668 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1669 mdname(mddev), mddev->layout);
1673 * copy the already verified devices into our private RAID10
1674 * bookkeeping area. [whatever we allocate in run(),
1675 * should be freed in stop()]
1677 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1678 mddev->private = conf;
1680 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1684 memset(conf, 0, sizeof(*conf));
1685 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1687 if (!conf->mirrors) {
1688 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1692 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1694 conf->near_copies = nc;
1695 conf->far_copies = fc;
1696 conf->copies = nc*fc;
1697 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1698 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1699 stride = mddev->size >> (conf->chunk_shift-1);
1700 sector_div(stride, fc);
1701 conf->stride = stride << conf->chunk_shift;
1703 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1704 r10bio_pool_free, conf);
1705 if (!conf->r10bio_pool) {
1706 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1711 ITERATE_RDEV(mddev, rdev, tmp) {
1712 disk_idx = rdev->raid_disk;
1713 if (disk_idx >= mddev->raid_disks
1716 disk = conf->mirrors + disk_idx;
1720 blk_queue_stack_limits(mddev->queue,
1721 rdev->bdev->bd_disk->queue);
1722 /* as we don't honour merge_bvec_fn, we must never risk
1723 * violating it, so limit ->max_sector to one PAGE, as
1724 * a one page request is never in violation.
1726 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1727 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1728 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1730 disk->head_position = 0;
1731 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1732 conf->working_disks++;
1734 conf->raid_disks = mddev->raid_disks;
1735 conf->mddev = mddev;
1736 spin_lock_init(&conf->device_lock);
1737 INIT_LIST_HEAD(&conf->retry_list);
1739 spin_lock_init(&conf->resync_lock);
1740 init_waitqueue_head(&conf->wait_barrier);
1742 /* need to check that every block has at least one working mirror */
1743 if (!enough(conf)) {
1744 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
1749 mddev->degraded = 0;
1750 for (i = 0; i < conf->raid_disks; i++) {
1752 disk = conf->mirrors + i;
1755 disk->head_position = 0;
1761 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
1762 if (!mddev->thread) {
1764 "raid10: couldn't allocate thread for %s\n",
1770 "raid10: raid set %s active with %d out of %d devices\n",
1771 mdname(mddev), mddev->raid_disks - mddev->degraded,
1774 * Ok, everything is just fine now
1776 size = conf->stride * conf->raid_disks;
1777 sector_div(size, conf->near_copies);
1778 mddev->array_size = size/2;
1779 mddev->resync_max_sectors = size;
1781 mddev->queue->unplug_fn = raid10_unplug;
1782 mddev->queue->issue_flush_fn = raid10_issue_flush;
1784 /* Calculate max read-ahead size.
1785 * We need to readahead at least twice a whole stripe....
1789 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
1790 stripe /= conf->near_copies;
1791 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
1792 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
1795 if (conf->near_copies < mddev->raid_disks)
1796 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1800 if (conf->r10bio_pool)
1801 mempool_destroy(conf->r10bio_pool);
1802 kfree(conf->mirrors);
1804 mddev->private = NULL;
1809 static int stop(mddev_t *mddev)
1811 conf_t *conf = mddev_to_conf(mddev);
1813 md_unregister_thread(mddev->thread);
1814 mddev->thread = NULL;
1815 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1816 if (conf->r10bio_pool)
1817 mempool_destroy(conf->r10bio_pool);
1818 kfree(conf->mirrors);
1820 mddev->private = NULL;
1825 static mdk_personality_t raid10_personality =
1828 .owner = THIS_MODULE,
1829 .make_request = make_request,
1833 .error_handler = error,
1834 .hot_add_disk = raid10_add_disk,
1835 .hot_remove_disk= raid10_remove_disk,
1836 .spare_active = raid10_spare_active,
1837 .sync_request = sync_request,
1840 static int __init raid_init(void)
1842 return register_md_personality(RAID10, &raid10_personality);
1845 static void raid_exit(void)
1847 unregister_md_personality(RAID10);
1850 module_init(raid_init);
1851 module_exit(raid_exit);
1852 MODULE_LICENSE("GPL");
1853 MODULE_ALIAS("md-personality-9"); /* RAID10 */