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/delay.h>
22 #include <linux/blkdev.h>
23 #include <linux/seq_file.h>
30 * RAID10 provides a combination of RAID0 and RAID1 functionality.
31 * The layout of data is defined by
34 * near_copies (stored in low byte of layout)
35 * far_copies (stored in second byte of layout)
36 * far_offset (stored in bit 16 of layout )
38 * The data to be stored is divided into chunks using chunksize.
39 * Each device is divided into far_copies sections.
40 * In each section, chunks are laid out in a style similar to raid0, but
41 * near_copies copies of each chunk is stored (each on a different drive).
42 * The starting device for each section is offset near_copies from the starting
43 * device of the previous section.
44 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
46 * near_copies and far_copies must be at least one, and their product is at most
49 * If far_offset is true, then the far_copies are handled a bit differently.
50 * The copies are still in different stripes, but instead of be very far apart
51 * on disk, there are adjacent stripes.
55 * Number of guaranteed r10bios in case of extreme VM load:
57 #define NR_RAID10_BIOS 256
59 static void unplug_slaves(mddev_t *mddev);
61 static void allow_barrier(conf_t *conf);
62 static void lower_barrier(conf_t *conf);
64 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
68 int size = offsetof(struct r10bio_s, devs[conf->copies]);
70 /* allocate a r10bio with room for raid_disks entries in the bios array */
71 r10_bio = kzalloc(size, gfp_flags);
72 if (!r10_bio && conf->mddev)
73 unplug_slaves(conf->mddev);
78 static void r10bio_pool_free(void *r10_bio, void *data)
83 /* Maximum size of each resync request */
84 #define RESYNC_BLOCK_SIZE (64*1024)
85 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
86 /* amount of memory to reserve for resync requests */
87 #define RESYNC_WINDOW (1024*1024)
88 /* maximum number of concurrent requests, memory permitting */
89 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
92 * When performing a resync, we need to read and compare, so
93 * we need as many pages are there are copies.
94 * When performing a recovery, we need 2 bios, one for read,
95 * one for write (we recover only one drive per r10buf)
98 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
107 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
109 unplug_slaves(conf->mddev);
113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
114 nalloc = conf->copies; /* resync */
116 nalloc = 2; /* recovery */
121 for (j = nalloc ; j-- ; ) {
122 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
125 r10_bio->devs[j].bio = bio;
128 * Allocate RESYNC_PAGES data pages and attach them
131 for (j = 0 ; j < nalloc; j++) {
132 bio = r10_bio->devs[j].bio;
133 for (i = 0; i < RESYNC_PAGES; i++) {
134 page = alloc_page(gfp_flags);
138 bio->bi_io_vec[i].bv_page = page;
146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
148 for (i = 0; i < RESYNC_PAGES ; i++)
149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
158 static void r10buf_pool_free(void *__r10_bio, void *data)
162 r10bio_t *r10bio = __r10_bio;
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
168 for (i = 0; i < RESYNC_PAGES; i++) {
169 safe_put_page(bio->bi_io_vec[i].bv_page);
170 bio->bi_io_vec[i].bv_page = NULL;
175 r10bio_pool_free(r10bio, conf);
178 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
184 if (*bio && *bio != IO_BLOCKED)
190 static void free_r10bio(r10bio_t *r10_bio)
192 conf_t *conf = r10_bio->mddev->private;
195 * Wake up any possible resync thread that waits for the device
200 put_all_bios(conf, r10_bio);
201 mempool_free(r10_bio, conf->r10bio_pool);
204 static void put_buf(r10bio_t *r10_bio)
206 conf_t *conf = r10_bio->mddev->private;
208 mempool_free(r10_bio, conf->r10buf_pool);
213 static void reschedule_retry(r10bio_t *r10_bio)
216 mddev_t *mddev = r10_bio->mddev;
217 conf_t *conf = mddev->private;
219 spin_lock_irqsave(&conf->device_lock, flags);
220 list_add(&r10_bio->retry_list, &conf->retry_list);
222 spin_unlock_irqrestore(&conf->device_lock, flags);
224 /* wake up frozen array... */
225 wake_up(&conf->wait_barrier);
227 md_wakeup_thread(mddev->thread);
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
235 static void raid_end_bio_io(r10bio_t *r10_bio)
237 struct bio *bio = r10_bio->master_bio;
240 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
241 free_r10bio(r10_bio);
245 * Update disk head position estimator based on IRQ completion info.
247 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
249 conf_t *conf = r10_bio->mddev->private;
251 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
252 r10_bio->devs[slot].addr + (r10_bio->sectors);
255 static void raid10_end_read_request(struct bio *bio, int error)
257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
258 r10bio_t *r10_bio = bio->bi_private;
260 conf_t *conf = r10_bio->mddev->private;
263 slot = r10_bio->read_slot;
264 dev = r10_bio->devs[slot].devnum;
266 * this branch is our 'one mirror IO has finished' event handler:
268 update_head_pos(slot, r10_bio);
272 * Set R10BIO_Uptodate in our master bio, so that
273 * we will return a good error code to the higher
274 * levels even if IO on some other mirrored buffer fails.
276 * The 'master' represents the composite IO operation to
277 * user-side. So if something waits for IO, then it will
278 * wait for the 'master' bio.
280 set_bit(R10BIO_Uptodate, &r10_bio->state);
281 raid_end_bio_io(r10_bio);
286 char b[BDEVNAME_SIZE];
287 if (printk_ratelimit())
288 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
289 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
290 reschedule_retry(r10_bio);
293 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
296 static void raid10_end_write_request(struct bio *bio, int error)
298 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
299 r10bio_t *r10_bio = bio->bi_private;
301 conf_t *conf = r10_bio->mddev->private;
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);
349 * RAID10 layout manager
350 * Aswell as the chunksize and raid_disks count, there are two
351 * parameters: near_copies and far_copies.
352 * near_copies * far_copies must be <= raid_disks.
353 * Normally one of these will be 1.
354 * If both are 1, we get raid0.
355 * If near_copies == raid_disks, we get raid1.
357 * Chunks are layed out in raid0 style with near_copies copies of the
358 * first chunk, followed by near_copies copies of the next chunk and
360 * If far_copies > 1, then after 1/far_copies of the array has been assigned
361 * as described above, we start again with a device offset of near_copies.
362 * So we effectively have another copy of the whole array further down all
363 * the drives, but with blocks on different drives.
364 * With this layout, and block is never stored twice on the one device.
366 * raid10_find_phys finds the sector offset of a given virtual sector
367 * on each device that it is on.
369 * raid10_find_virt does the reverse mapping, from a device and a
370 * sector offset to a virtual address
373 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
383 /* now calculate first sector/dev */
384 chunk = r10bio->sector >> conf->chunk_shift;
385 sector = r10bio->sector & conf->chunk_mask;
387 chunk *= conf->near_copies;
389 dev = sector_div(stripe, conf->raid_disks);
390 if (conf->far_offset)
391 stripe *= conf->far_copies;
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 offset = sector & conf->chunk_mask;
426 if (conf->far_offset) {
428 chunk = sector >> conf->chunk_shift;
429 fc = sector_div(chunk, conf->far_copies);
430 dev -= fc * conf->near_copies;
432 dev += conf->raid_disks;
434 while (sector >= conf->stride) {
435 sector -= conf->stride;
436 if (dev < conf->near_copies)
437 dev += conf->raid_disks - conf->near_copies;
439 dev -= conf->near_copies;
441 chunk = sector >> conf->chunk_shift;
443 vchunk = chunk * conf->raid_disks + dev;
444 sector_div(vchunk, conf->near_copies);
445 return (vchunk << conf->chunk_shift) + offset;
449 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
451 * @bvm: properties of new bio
452 * @biovec: the request that could be merged to it.
454 * Return amount of bytes we can accept at this offset
455 * If near_copies == raid_disk, there are no striping issues,
456 * but in that case, the function isn't called at all.
458 static int raid10_mergeable_bvec(struct request_queue *q,
459 struct bvec_merge_data *bvm,
460 struct bio_vec *biovec)
462 mddev_t *mddev = q->queuedata;
463 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
465 unsigned int chunk_sectors = mddev->chunk_sectors;
466 unsigned int bio_sectors = bvm->bi_size >> 9;
468 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
469 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
470 if (max <= biovec->bv_len && bio_sectors == 0)
471 return biovec->bv_len;
477 * This routine returns the disk from which the requested read should
478 * be done. There is a per-array 'next expected sequential IO' sector
479 * number - if this matches on the next IO then we use the last disk.
480 * There is also a per-disk 'last know head position' sector that is
481 * maintained from IRQ contexts, both the normal and the resync IO
482 * completion handlers update this position correctly. If there is no
483 * perfect sequential match then we pick the disk whose head is closest.
485 * If there are 2 mirrors in the same 2 devices, performance degrades
486 * because position is mirror, not device based.
488 * The rdev for the device selected will have nr_pending incremented.
492 * FIXME: possibly should rethink readbalancing and do it differently
493 * depending on near_copies / far_copies geometry.
495 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
497 const unsigned long this_sector = r10_bio->sector;
498 int disk, slot, nslot;
499 const int sectors = r10_bio->sectors;
500 sector_t new_distance, current_distance;
503 raid10_find_phys(conf, r10_bio);
506 * Check if we can balance. We can balance on the whole
507 * device if no resync is going on (recovery is ok), or below
508 * the resync window. We take the first readable disk when
509 * above the resync window.
511 if (conf->mddev->recovery_cp < MaxSector
512 && (this_sector + sectors >= conf->next_resync)) {
513 /* make sure that disk is operational */
515 disk = r10_bio->devs[slot].devnum;
517 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
518 r10_bio->devs[slot].bio == IO_BLOCKED ||
519 !test_bit(In_sync, &rdev->flags)) {
521 if (slot == conf->copies) {
526 disk = r10_bio->devs[slot].devnum;
532 /* make sure the disk is operational */
534 disk = r10_bio->devs[slot].devnum;
535 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
536 r10_bio->devs[slot].bio == IO_BLOCKED ||
537 !test_bit(In_sync, &rdev->flags)) {
539 if (slot == conf->copies) {
543 disk = r10_bio->devs[slot].devnum;
547 current_distance = abs(r10_bio->devs[slot].addr -
548 conf->mirrors[disk].head_position);
550 /* Find the disk whose head is closest,
551 * or - for far > 1 - find the closest to partition beginning */
553 for (nslot = slot; nslot < conf->copies; nslot++) {
554 int ndisk = r10_bio->devs[nslot].devnum;
557 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
558 r10_bio->devs[nslot].bio == IO_BLOCKED ||
559 !test_bit(In_sync, &rdev->flags))
562 /* This optimisation is debatable, and completely destroys
563 * sequential read speed for 'far copies' arrays. So only
564 * keep it for 'near' arrays, and review those later.
566 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
572 /* for far > 1 always use the lowest address */
573 if (conf->far_copies > 1)
574 new_distance = r10_bio->devs[nslot].addr;
576 new_distance = abs(r10_bio->devs[nslot].addr -
577 conf->mirrors[ndisk].head_position);
578 if (new_distance < current_distance) {
579 current_distance = new_distance;
586 r10_bio->read_slot = slot;
587 /* conf->next_seq_sect = this_sector + sectors;*/
589 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
590 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
598 static void unplug_slaves(mddev_t *mddev)
600 conf_t *conf = mddev->private;
604 for (i=0; i < conf->raid_disks; i++) {
605 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
606 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
607 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
609 atomic_inc(&rdev->nr_pending);
614 rdev_dec_pending(rdev, mddev);
621 static void raid10_unplug(struct request_queue *q)
623 mddev_t *mddev = q->queuedata;
625 unplug_slaves(q->queuedata);
626 md_wakeup_thread(mddev->thread);
629 static int raid10_congested(void *data, int bits)
631 mddev_t *mddev = data;
632 conf_t *conf = mddev->private;
635 if (mddev_congested(mddev, bits))
638 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
639 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
640 if (rdev && !test_bit(Faulty, &rdev->flags)) {
641 struct request_queue *q = bdev_get_queue(rdev->bdev);
643 ret |= bdi_congested(&q->backing_dev_info, bits);
650 static int flush_pending_writes(conf_t *conf)
652 /* Any writes that have been queued but are awaiting
653 * bitmap updates get flushed here.
654 * We return 1 if any requests were actually submitted.
658 spin_lock_irq(&conf->device_lock);
660 if (conf->pending_bio_list.head) {
662 bio = bio_list_get(&conf->pending_bio_list);
663 blk_remove_plug(conf->mddev->queue);
664 spin_unlock_irq(&conf->device_lock);
665 /* flush any pending bitmap writes to disk
666 * before proceeding w/ I/O */
667 bitmap_unplug(conf->mddev->bitmap);
669 while (bio) { /* submit pending writes */
670 struct bio *next = bio->bi_next;
672 generic_make_request(bio);
677 spin_unlock_irq(&conf->device_lock);
681 * Sometimes we need to suspend IO while we do something else,
682 * either some resync/recovery, or reconfigure the array.
683 * To do this we raise a 'barrier'.
684 * The 'barrier' is a counter that can be raised multiple times
685 * to count how many activities are happening which preclude
687 * We can only raise the barrier if there is no pending IO.
688 * i.e. if nr_pending == 0.
689 * We choose only to raise the barrier if no-one is waiting for the
690 * barrier to go down. This means that as soon as an IO request
691 * is ready, no other operations which require a barrier will start
692 * until the IO request has had a chance.
694 * So: regular IO calls 'wait_barrier'. When that returns there
695 * is no backgroup IO happening, It must arrange to call
696 * allow_barrier when it has finished its IO.
697 * backgroup IO calls must call raise_barrier. Once that returns
698 * there is no normal IO happeing. It must arrange to call
699 * lower_barrier when the particular background IO completes.
702 static void raise_barrier(conf_t *conf, int force)
704 BUG_ON(force && !conf->barrier);
705 spin_lock_irq(&conf->resync_lock);
707 /* Wait until no block IO is waiting (unless 'force') */
708 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
710 raid10_unplug(conf->mddev->queue));
712 /* block any new IO from starting */
715 /* No wait for all pending IO to complete */
716 wait_event_lock_irq(conf->wait_barrier,
717 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
719 raid10_unplug(conf->mddev->queue));
721 spin_unlock_irq(&conf->resync_lock);
724 static void lower_barrier(conf_t *conf)
727 spin_lock_irqsave(&conf->resync_lock, flags);
729 spin_unlock_irqrestore(&conf->resync_lock, flags);
730 wake_up(&conf->wait_barrier);
733 static void wait_barrier(conf_t *conf)
735 spin_lock_irq(&conf->resync_lock);
738 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
740 raid10_unplug(conf->mddev->queue));
744 spin_unlock_irq(&conf->resync_lock);
747 static void allow_barrier(conf_t *conf)
750 spin_lock_irqsave(&conf->resync_lock, flags);
752 spin_unlock_irqrestore(&conf->resync_lock, flags);
753 wake_up(&conf->wait_barrier);
756 static void freeze_array(conf_t *conf)
758 /* stop syncio and normal IO and wait for everything to
760 * We increment barrier and nr_waiting, and then
761 * wait until nr_pending match nr_queued+1
762 * This is called in the context of one normal IO request
763 * that has failed. Thus any sync request that might be pending
764 * will be blocked by nr_pending, and we need to wait for
765 * pending IO requests to complete or be queued for re-try.
766 * Thus the number queued (nr_queued) plus this request (1)
767 * must match the number of pending IOs (nr_pending) before
770 spin_lock_irq(&conf->resync_lock);
773 wait_event_lock_irq(conf->wait_barrier,
774 conf->nr_pending == conf->nr_queued+1,
776 ({ flush_pending_writes(conf);
777 raid10_unplug(conf->mddev->queue); }));
778 spin_unlock_irq(&conf->resync_lock);
781 static void unfreeze_array(conf_t *conf)
783 /* reverse the effect of the freeze */
784 spin_lock_irq(&conf->resync_lock);
787 wake_up(&conf->wait_barrier);
788 spin_unlock_irq(&conf->resync_lock);
791 static int make_request(mddev_t *mddev, struct bio * bio)
793 conf_t *conf = mddev->private;
794 mirror_info_t *mirror;
796 struct bio *read_bio;
798 int chunk_sects = conf->chunk_mask + 1;
799 const int rw = bio_data_dir(bio);
800 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
803 mdk_rdev_t *blocked_rdev;
805 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
806 md_barrier_request(mddev, bio);
810 /* If this request crosses a chunk boundary, we need to
811 * split it. This will only happen for 1 PAGE (or less) requests.
813 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
815 conf->near_copies < conf->raid_disks)) {
817 /* Sanity check -- queue functions should prevent this happening */
818 if (bio->bi_vcnt != 1 ||
821 /* This is a one page bio that upper layers
822 * refuse to split for us, so we need to split it.
825 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
826 if (make_request(mddev, &bp->bio1))
827 generic_make_request(&bp->bio1);
828 if (make_request(mddev, &bp->bio2))
829 generic_make_request(&bp->bio2);
831 bio_pair_release(bp);
834 printk("raid10_make_request bug: can't convert block across chunks"
835 " or bigger than %dk %llu %d\n", chunk_sects/2,
836 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
842 md_write_start(mddev, bio);
845 * Register the new request and wait if the reconstruction
846 * thread has put up a bar for new requests.
847 * Continue immediately if no resync is active currently.
851 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
853 r10_bio->master_bio = bio;
854 r10_bio->sectors = bio->bi_size >> 9;
856 r10_bio->mddev = mddev;
857 r10_bio->sector = bio->bi_sector;
862 * read balancing logic:
864 int disk = read_balance(conf, r10_bio);
865 int slot = r10_bio->read_slot;
867 raid_end_bio_io(r10_bio);
870 mirror = conf->mirrors + disk;
872 read_bio = bio_clone(bio, GFP_NOIO);
874 r10_bio->devs[slot].bio = read_bio;
876 read_bio->bi_sector = r10_bio->devs[slot].addr +
877 mirror->rdev->data_offset;
878 read_bio->bi_bdev = mirror->rdev->bdev;
879 read_bio->bi_end_io = raid10_end_read_request;
880 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
881 read_bio->bi_private = r10_bio;
883 generic_make_request(read_bio);
890 /* first select target devices under rcu_lock and
891 * inc refcount on their rdev. Record them by setting
894 raid10_find_phys(conf, r10_bio);
898 for (i = 0; i < conf->copies; i++) {
899 int d = r10_bio->devs[i].devnum;
900 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
901 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
902 atomic_inc(&rdev->nr_pending);
906 if (rdev && !test_bit(Faulty, &rdev->flags)) {
907 atomic_inc(&rdev->nr_pending);
908 r10_bio->devs[i].bio = bio;
910 r10_bio->devs[i].bio = NULL;
911 set_bit(R10BIO_Degraded, &r10_bio->state);
916 if (unlikely(blocked_rdev)) {
917 /* Have to wait for this device to get unblocked, then retry */
921 for (j = 0; j < i; j++)
922 if (r10_bio->devs[j].bio) {
923 d = r10_bio->devs[j].devnum;
924 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
927 md_wait_for_blocked_rdev(blocked_rdev, mddev);
932 atomic_set(&r10_bio->remaining, 0);
935 for (i = 0; i < conf->copies; i++) {
937 int d = r10_bio->devs[i].devnum;
938 if (!r10_bio->devs[i].bio)
941 mbio = bio_clone(bio, GFP_NOIO);
942 r10_bio->devs[i].bio = mbio;
944 mbio->bi_sector = r10_bio->devs[i].addr+
945 conf->mirrors[d].rdev->data_offset;
946 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
947 mbio->bi_end_io = raid10_end_write_request;
948 mbio->bi_rw = WRITE | (do_sync << BIO_RW_SYNCIO);
949 mbio->bi_private = r10_bio;
951 atomic_inc(&r10_bio->remaining);
952 bio_list_add(&bl, mbio);
955 if (unlikely(!atomic_read(&r10_bio->remaining))) {
956 /* the array is dead */
958 raid_end_bio_io(r10_bio);
962 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
963 spin_lock_irqsave(&conf->device_lock, flags);
964 bio_list_merge(&conf->pending_bio_list, &bl);
965 blk_plug_device(mddev->queue);
966 spin_unlock_irqrestore(&conf->device_lock, flags);
968 /* In case raid10d snuck in to freeze_array */
969 wake_up(&conf->wait_barrier);
972 md_wakeup_thread(mddev->thread);
977 static void status(struct seq_file *seq, mddev_t *mddev)
979 conf_t *conf = mddev->private;
982 if (conf->near_copies < conf->raid_disks)
983 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
984 if (conf->near_copies > 1)
985 seq_printf(seq, " %d near-copies", conf->near_copies);
986 if (conf->far_copies > 1) {
987 if (conf->far_offset)
988 seq_printf(seq, " %d offset-copies", conf->far_copies);
990 seq_printf(seq, " %d far-copies", conf->far_copies);
992 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
993 conf->raid_disks - mddev->degraded);
994 for (i = 0; i < conf->raid_disks; i++)
995 seq_printf(seq, "%s",
996 conf->mirrors[i].rdev &&
997 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
998 seq_printf(seq, "]");
1001 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1003 char b[BDEVNAME_SIZE];
1004 conf_t *conf = mddev->private;
1007 * If it is not operational, then we have already marked it as dead
1008 * else if it is the last working disks, ignore the error, let the
1009 * next level up know.
1010 * else mark the drive as failed
1012 if (test_bit(In_sync, &rdev->flags)
1013 && conf->raid_disks-mddev->degraded == 1)
1015 * Don't fail the drive, just return an IO error.
1016 * The test should really be more sophisticated than
1017 * "working_disks == 1", but it isn't critical, and
1018 * can wait until we do more sophisticated "is the drive
1019 * really dead" tests...
1022 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1023 unsigned long flags;
1024 spin_lock_irqsave(&conf->device_lock, flags);
1026 spin_unlock_irqrestore(&conf->device_lock, flags);
1028 * if recovery is running, make sure it aborts.
1030 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1032 set_bit(Faulty, &rdev->flags);
1033 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1034 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1035 "raid10: Operation continuing on %d devices.\n",
1036 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1039 static void print_conf(conf_t *conf)
1044 printk("RAID10 conf printout:\n");
1046 printk("(!conf)\n");
1049 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1052 for (i = 0; i < conf->raid_disks; i++) {
1053 char b[BDEVNAME_SIZE];
1054 tmp = conf->mirrors + i;
1056 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
1057 i, !test_bit(In_sync, &tmp->rdev->flags),
1058 !test_bit(Faulty, &tmp->rdev->flags),
1059 bdevname(tmp->rdev->bdev,b));
1063 static void close_sync(conf_t *conf)
1066 allow_barrier(conf);
1068 mempool_destroy(conf->r10buf_pool);
1069 conf->r10buf_pool = NULL;
1072 /* check if there are enough drives for
1073 * every block to appear on atleast one
1075 static int enough(conf_t *conf)
1080 int n = conf->copies;
1083 if (conf->mirrors[first].rdev)
1085 first = (first+1) % conf->raid_disks;
1089 } while (first != 0);
1093 static int raid10_spare_active(mddev_t *mddev)
1096 conf_t *conf = mddev->private;
1100 * Find all non-in_sync disks within the RAID10 configuration
1101 * and mark them in_sync
1103 for (i = 0; i < conf->raid_disks; i++) {
1104 tmp = conf->mirrors + i;
1106 && !test_bit(Faulty, &tmp->rdev->flags)
1107 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1108 unsigned long flags;
1109 spin_lock_irqsave(&conf->device_lock, flags);
1111 spin_unlock_irqrestore(&conf->device_lock, flags);
1120 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1122 conf_t *conf = mddev->private;
1127 int last = conf->raid_disks - 1;
1129 if (mddev->recovery_cp < MaxSector)
1130 /* only hot-add to in-sync arrays, as recovery is
1131 * very different from resync
1137 if (rdev->raid_disk >= 0)
1138 first = last = rdev->raid_disk;
1140 if (rdev->saved_raid_disk >= 0 &&
1141 rdev->saved_raid_disk >= first &&
1142 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1143 mirror = rdev->saved_raid_disk;
1146 for ( ; mirror <= last ; mirror++)
1147 if ( !(p=conf->mirrors+mirror)->rdev) {
1149 disk_stack_limits(mddev->gendisk, rdev->bdev,
1150 rdev->data_offset << 9);
1151 /* as we don't honour merge_bvec_fn, we must
1152 * never risk violating it, so limit
1153 * ->max_segments to one lying with a single
1154 * page, as a one page request is never in
1157 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1158 blk_queue_max_segments(mddev->queue, 1);
1159 blk_queue_segment_boundary(mddev->queue,
1160 PAGE_CACHE_SIZE - 1);
1163 p->head_position = 0;
1164 rdev->raid_disk = mirror;
1166 if (rdev->saved_raid_disk != mirror)
1168 rcu_assign_pointer(p->rdev, rdev);
1172 md_integrity_add_rdev(rdev, mddev);
1177 static int raid10_remove_disk(mddev_t *mddev, int number)
1179 conf_t *conf = mddev->private;
1182 mirror_info_t *p = conf->mirrors+ number;
1187 if (test_bit(In_sync, &rdev->flags) ||
1188 atomic_read(&rdev->nr_pending)) {
1192 /* Only remove faulty devices in recovery
1195 if (!test_bit(Faulty, &rdev->flags) &&
1202 if (atomic_read(&rdev->nr_pending)) {
1203 /* lost the race, try later */
1208 md_integrity_register(mddev);
1217 static void end_sync_read(struct bio *bio, int error)
1219 r10bio_t *r10_bio = bio->bi_private;
1220 conf_t *conf = r10_bio->mddev->private;
1223 for (i=0; i<conf->copies; i++)
1224 if (r10_bio->devs[i].bio == bio)
1226 BUG_ON(i == conf->copies);
1227 update_head_pos(i, r10_bio);
1228 d = r10_bio->devs[i].devnum;
1230 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1231 set_bit(R10BIO_Uptodate, &r10_bio->state);
1233 atomic_add(r10_bio->sectors,
1234 &conf->mirrors[d].rdev->corrected_errors);
1235 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1236 md_error(r10_bio->mddev,
1237 conf->mirrors[d].rdev);
1240 /* for reconstruct, we always reschedule after a read.
1241 * for resync, only after all reads
1243 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1244 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1245 atomic_dec_and_test(&r10_bio->remaining)) {
1246 /* we have read all the blocks,
1247 * do the comparison in process context in raid10d
1249 reschedule_retry(r10_bio);
1253 static void end_sync_write(struct bio *bio, int error)
1255 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1256 r10bio_t *r10_bio = bio->bi_private;
1257 mddev_t *mddev = r10_bio->mddev;
1258 conf_t *conf = mddev->private;
1261 for (i = 0; i < conf->copies; i++)
1262 if (r10_bio->devs[i].bio == bio)
1264 d = r10_bio->devs[i].devnum;
1267 md_error(mddev, conf->mirrors[d].rdev);
1269 update_head_pos(i, r10_bio);
1271 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1272 while (atomic_dec_and_test(&r10_bio->remaining)) {
1273 if (r10_bio->master_bio == NULL) {
1274 /* the primary of several recovery bios */
1275 sector_t s = r10_bio->sectors;
1277 md_done_sync(mddev, s, 1);
1280 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1288 * Note: sync and recover and handled very differently for raid10
1289 * This code is for resync.
1290 * For resync, we read through virtual addresses and read all blocks.
1291 * If there is any error, we schedule a write. The lowest numbered
1292 * drive is authoritative.
1293 * However requests come for physical address, so we need to map.
1294 * For every physical address there are raid_disks/copies virtual addresses,
1295 * which is always are least one, but is not necessarly an integer.
1296 * This means that a physical address can span multiple chunks, so we may
1297 * have to submit multiple io requests for a single sync request.
1300 * We check if all blocks are in-sync and only write to blocks that
1303 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1305 conf_t *conf = mddev->private;
1307 struct bio *tbio, *fbio;
1309 atomic_set(&r10_bio->remaining, 1);
1311 /* find the first device with a block */
1312 for (i=0; i<conf->copies; i++)
1313 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1316 if (i == conf->copies)
1320 fbio = r10_bio->devs[i].bio;
1322 /* now find blocks with errors */
1323 for (i=0 ; i < conf->copies ; i++) {
1325 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1327 tbio = r10_bio->devs[i].bio;
1329 if (tbio->bi_end_io != end_sync_read)
1333 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1334 /* We know that the bi_io_vec layout is the same for
1335 * both 'first' and 'i', so we just compare them.
1336 * All vec entries are PAGE_SIZE;
1338 for (j = 0; j < vcnt; j++)
1339 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1340 page_address(tbio->bi_io_vec[j].bv_page),
1345 mddev->resync_mismatches += r10_bio->sectors;
1347 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1348 /* Don't fix anything. */
1350 /* Ok, we need to write this bio
1351 * First we need to fixup bv_offset, bv_len and
1352 * bi_vecs, as the read request might have corrupted these
1354 tbio->bi_vcnt = vcnt;
1355 tbio->bi_size = r10_bio->sectors << 9;
1357 tbio->bi_phys_segments = 0;
1358 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1359 tbio->bi_flags |= 1 << BIO_UPTODATE;
1360 tbio->bi_next = NULL;
1361 tbio->bi_rw = WRITE;
1362 tbio->bi_private = r10_bio;
1363 tbio->bi_sector = r10_bio->devs[i].addr;
1365 for (j=0; j < vcnt ; j++) {
1366 tbio->bi_io_vec[j].bv_offset = 0;
1367 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1369 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1370 page_address(fbio->bi_io_vec[j].bv_page),
1373 tbio->bi_end_io = end_sync_write;
1375 d = r10_bio->devs[i].devnum;
1376 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1377 atomic_inc(&r10_bio->remaining);
1378 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1380 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1381 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1382 generic_make_request(tbio);
1386 if (atomic_dec_and_test(&r10_bio->remaining)) {
1387 md_done_sync(mddev, r10_bio->sectors, 1);
1393 * Now for the recovery code.
1394 * Recovery happens across physical sectors.
1395 * We recover all non-is_sync drives by finding the virtual address of
1396 * each, and then choose a working drive that also has that virt address.
1397 * There is a separate r10_bio for each non-in_sync drive.
1398 * Only the first two slots are in use. The first for reading,
1399 * The second for writing.
1403 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1405 conf_t *conf = mddev->private;
1407 struct bio *bio, *wbio;
1410 /* move the pages across to the second bio
1411 * and submit the write request
1413 bio = r10_bio->devs[0].bio;
1414 wbio = r10_bio->devs[1].bio;
1415 for (i=0; i < wbio->bi_vcnt; i++) {
1416 struct page *p = bio->bi_io_vec[i].bv_page;
1417 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1418 wbio->bi_io_vec[i].bv_page = p;
1420 d = r10_bio->devs[1].devnum;
1422 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1423 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1424 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1425 generic_make_request(wbio);
1427 bio_endio(wbio, -EIO);
1432 * Used by fix_read_error() to decay the per rdev read_errors.
1433 * We halve the read error count for every hour that has elapsed
1434 * since the last recorded read error.
1437 static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1439 struct timespec cur_time_mon;
1440 unsigned long hours_since_last;
1441 unsigned int read_errors = atomic_read(&rdev->read_errors);
1443 ktime_get_ts(&cur_time_mon);
1445 if (rdev->last_read_error.tv_sec == 0 &&
1446 rdev->last_read_error.tv_nsec == 0) {
1447 /* first time we've seen a read error */
1448 rdev->last_read_error = cur_time_mon;
1452 hours_since_last = (cur_time_mon.tv_sec -
1453 rdev->last_read_error.tv_sec) / 3600;
1455 rdev->last_read_error = cur_time_mon;
1458 * if hours_since_last is > the number of bits in read_errors
1459 * just set read errors to 0. We do this to avoid
1460 * overflowing the shift of read_errors by hours_since_last.
1462 if (hours_since_last >= 8 * sizeof(read_errors))
1463 atomic_set(&rdev->read_errors, 0);
1465 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1469 * This is a kernel thread which:
1471 * 1. Retries failed read operations on working mirrors.
1472 * 2. Updates the raid superblock when problems encounter.
1473 * 3. Performs writes following reads for array synchronising.
1476 static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1478 int sect = 0; /* Offset from r10_bio->sector */
1479 int sectors = r10_bio->sectors;
1481 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1485 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1486 char b[BDEVNAME_SIZE];
1487 int cur_read_error_count = 0;
1489 rdev = rcu_dereference(conf->mirrors[d].rdev);
1490 bdevname(rdev->bdev, b);
1492 if (test_bit(Faulty, &rdev->flags)) {
1494 /* drive has already been failed, just ignore any
1495 more fix_read_error() attempts */
1499 check_decay_read_errors(mddev, rdev);
1500 atomic_inc(&rdev->read_errors);
1501 cur_read_error_count = atomic_read(&rdev->read_errors);
1502 if (cur_read_error_count > max_read_errors) {
1505 "raid10: %s: Raid device exceeded "
1506 "read_error threshold "
1507 "[cur %d:max %d]\n",
1508 b, cur_read_error_count, max_read_errors);
1510 "raid10: %s: Failing raid "
1512 md_error(mddev, conf->mirrors[d].rdev);
1520 int sl = r10_bio->read_slot;
1524 if (s > (PAGE_SIZE>>9))
1529 int d = r10_bio->devs[sl].devnum;
1530 rdev = rcu_dereference(conf->mirrors[d].rdev);
1532 test_bit(In_sync, &rdev->flags)) {
1533 atomic_inc(&rdev->nr_pending);
1535 success = sync_page_io(rdev->bdev,
1536 r10_bio->devs[sl].addr +
1537 sect + rdev->data_offset,
1539 conf->tmppage, READ);
1540 rdev_dec_pending(rdev, mddev);
1546 if (sl == conf->copies)
1548 } while (!success && sl != r10_bio->read_slot);
1552 /* Cannot read from anywhere -- bye bye array */
1553 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1554 md_error(mddev, conf->mirrors[dn].rdev);
1559 /* write it back and re-read */
1561 while (sl != r10_bio->read_slot) {
1562 char b[BDEVNAME_SIZE];
1567 d = r10_bio->devs[sl].devnum;
1568 rdev = rcu_dereference(conf->mirrors[d].rdev);
1570 test_bit(In_sync, &rdev->flags)) {
1571 atomic_inc(&rdev->nr_pending);
1573 atomic_add(s, &rdev->corrected_errors);
1574 if (sync_page_io(rdev->bdev,
1575 r10_bio->devs[sl].addr +
1576 sect + rdev->data_offset,
1577 s<<9, conf->tmppage, WRITE)
1579 /* Well, this device is dead */
1581 "raid10:%s: read correction "
1583 " (%d sectors at %llu on %s)\n",
1585 (unsigned long long)(sect+
1587 bdevname(rdev->bdev, b));
1588 printk(KERN_NOTICE "raid10:%s: failing "
1590 bdevname(rdev->bdev, b));
1591 md_error(mddev, rdev);
1593 rdev_dec_pending(rdev, mddev);
1598 while (sl != r10_bio->read_slot) {
1603 d = r10_bio->devs[sl].devnum;
1604 rdev = rcu_dereference(conf->mirrors[d].rdev);
1606 test_bit(In_sync, &rdev->flags)) {
1607 char b[BDEVNAME_SIZE];
1608 atomic_inc(&rdev->nr_pending);
1610 if (sync_page_io(rdev->bdev,
1611 r10_bio->devs[sl].addr +
1612 sect + rdev->data_offset,
1613 s<<9, conf->tmppage,
1615 /* Well, this device is dead */
1617 "raid10:%s: unable to read back "
1619 " (%d sectors at %llu on %s)\n",
1621 (unsigned long long)(sect+
1623 bdevname(rdev->bdev, b));
1624 printk(KERN_NOTICE "raid10:%s: failing drive\n",
1625 bdevname(rdev->bdev, b));
1627 md_error(mddev, rdev);
1630 "raid10:%s: read error corrected"
1631 " (%d sectors at %llu on %s)\n",
1633 (unsigned long long)(sect+
1635 bdevname(rdev->bdev, b));
1638 rdev_dec_pending(rdev, mddev);
1649 static void raid10d(mddev_t *mddev)
1653 unsigned long flags;
1654 conf_t *conf = mddev->private;
1655 struct list_head *head = &conf->retry_list;
1659 md_check_recovery(mddev);
1662 char b[BDEVNAME_SIZE];
1664 unplug += flush_pending_writes(conf);
1666 spin_lock_irqsave(&conf->device_lock, flags);
1667 if (list_empty(head)) {
1668 spin_unlock_irqrestore(&conf->device_lock, flags);
1671 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1672 list_del(head->prev);
1674 spin_unlock_irqrestore(&conf->device_lock, flags);
1676 mddev = r10_bio->mddev;
1677 conf = mddev->private;
1678 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1679 sync_request_write(mddev, r10_bio);
1681 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1682 recovery_request_write(mddev, r10_bio);
1686 /* we got a read error. Maybe the drive is bad. Maybe just
1687 * the block and we can fix it.
1688 * We freeze all other IO, and try reading the block from
1689 * other devices. When we find one, we re-write
1690 * and check it that fixes the read error.
1691 * This is all done synchronously while the array is
1694 if (mddev->ro == 0) {
1696 fix_read_error(conf, mddev, r10_bio);
1697 unfreeze_array(conf);
1700 bio = r10_bio->devs[r10_bio->read_slot].bio;
1701 r10_bio->devs[r10_bio->read_slot].bio =
1702 mddev->ro ? IO_BLOCKED : NULL;
1703 mirror = read_balance(conf, r10_bio);
1705 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1706 " read error for block %llu\n",
1707 bdevname(bio->bi_bdev,b),
1708 (unsigned long long)r10_bio->sector);
1709 raid_end_bio_io(r10_bio);
1712 const bool do_sync = bio_rw_flagged(r10_bio->master_bio, BIO_RW_SYNCIO);
1714 rdev = conf->mirrors[mirror].rdev;
1715 if (printk_ratelimit())
1716 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1717 " another mirror\n",
1718 bdevname(rdev->bdev,b),
1719 (unsigned long long)r10_bio->sector);
1720 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1721 r10_bio->devs[r10_bio->read_slot].bio = bio;
1722 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1723 + rdev->data_offset;
1724 bio->bi_bdev = rdev->bdev;
1725 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
1726 bio->bi_private = r10_bio;
1727 bio->bi_end_io = raid10_end_read_request;
1729 generic_make_request(bio);
1735 unplug_slaves(mddev);
1739 static int init_resync(conf_t *conf)
1743 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1744 BUG_ON(conf->r10buf_pool);
1745 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1746 if (!conf->r10buf_pool)
1748 conf->next_resync = 0;
1753 * perform a "sync" on one "block"
1755 * We need to make sure that no normal I/O request - particularly write
1756 * requests - conflict with active sync requests.
1758 * This is achieved by tracking pending requests and a 'barrier' concept
1759 * that can be installed to exclude normal IO requests.
1761 * Resync and recovery are handled very differently.
1762 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1764 * For resync, we iterate over virtual addresses, read all copies,
1765 * and update if there are differences. If only one copy is live,
1767 * For recovery, we iterate over physical addresses, read a good
1768 * value for each non-in_sync drive, and over-write.
1770 * So, for recovery we may have several outstanding complex requests for a
1771 * given address, one for each out-of-sync device. We model this by allocating
1772 * a number of r10_bio structures, one for each out-of-sync device.
1773 * As we setup these structures, we collect all bio's together into a list
1774 * which we then process collectively to add pages, and then process again
1775 * to pass to generic_make_request.
1777 * The r10_bio structures are linked using a borrowed master_bio pointer.
1778 * This link is counted in ->remaining. When the r10_bio that points to NULL
1779 * has its remaining count decremented to 0, the whole complex operation
1784 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1786 conf_t *conf = mddev->private;
1788 struct bio *biolist = NULL, *bio;
1789 sector_t max_sector, nr_sectors;
1795 sector_t sectors_skipped = 0;
1796 int chunks_skipped = 0;
1798 if (!conf->r10buf_pool)
1799 if (init_resync(conf))
1803 max_sector = mddev->dev_sectors;
1804 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1805 max_sector = mddev->resync_max_sectors;
1806 if (sector_nr >= max_sector) {
1807 /* If we aborted, we need to abort the
1808 * sync on the 'current' bitmap chucks (there can
1809 * be several when recovering multiple devices).
1810 * as we may have started syncing it but not finished.
1811 * We can find the current address in
1812 * mddev->curr_resync, but for recovery,
1813 * we need to convert that to several
1814 * virtual addresses.
1816 if (mddev->curr_resync < max_sector) { /* aborted */
1817 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1818 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1820 else for (i=0; i<conf->raid_disks; i++) {
1822 raid10_find_virt(conf, mddev->curr_resync, i);
1823 bitmap_end_sync(mddev->bitmap, sect,
1826 } else /* completed sync */
1829 bitmap_close_sync(mddev->bitmap);
1832 return sectors_skipped;
1834 if (chunks_skipped >= conf->raid_disks) {
1835 /* if there has been nothing to do on any drive,
1836 * then there is nothing to do at all..
1839 return (max_sector - sector_nr) + sectors_skipped;
1842 if (max_sector > mddev->resync_max)
1843 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1845 /* make sure whole request will fit in a chunk - if chunks
1848 if (conf->near_copies < conf->raid_disks &&
1849 max_sector > (sector_nr | conf->chunk_mask))
1850 max_sector = (sector_nr | conf->chunk_mask) + 1;
1852 * If there is non-resync activity waiting for us then
1853 * put in a delay to throttle resync.
1855 if (!go_faster && conf->nr_waiting)
1856 msleep_interruptible(1000);
1858 /* Again, very different code for resync and recovery.
1859 * Both must result in an r10bio with a list of bios that
1860 * have bi_end_io, bi_sector, bi_bdev set,
1861 * and bi_private set to the r10bio.
1862 * For recovery, we may actually create several r10bios
1863 * with 2 bios in each, that correspond to the bios in the main one.
1864 * In this case, the subordinate r10bios link back through a
1865 * borrowed master_bio pointer, and the counter in the master
1866 * includes a ref from each subordinate.
1868 /* First, we decide what to do and set ->bi_end_io
1869 * To end_sync_read if we want to read, and
1870 * end_sync_write if we will want to write.
1873 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1874 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1875 /* recovery... the complicated one */
1879 for (i=0 ; i<conf->raid_disks; i++)
1880 if (conf->mirrors[i].rdev &&
1881 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1882 int still_degraded = 0;
1883 /* want to reconstruct this device */
1884 r10bio_t *rb2 = r10_bio;
1885 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1887 /* Unless we are doing a full sync, we only need
1888 * to recover the block if it is set in the bitmap
1890 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1892 if (sync_blocks < max_sync)
1893 max_sync = sync_blocks;
1896 /* yep, skip the sync_blocks here, but don't assume
1897 * that there will never be anything to do here
1899 chunks_skipped = -1;
1903 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1904 raise_barrier(conf, rb2 != NULL);
1905 atomic_set(&r10_bio->remaining, 0);
1907 r10_bio->master_bio = (struct bio*)rb2;
1909 atomic_inc(&rb2->remaining);
1910 r10_bio->mddev = mddev;
1911 set_bit(R10BIO_IsRecover, &r10_bio->state);
1912 r10_bio->sector = sect;
1914 raid10_find_phys(conf, r10_bio);
1916 /* Need to check if the array will still be
1919 for (j=0; j<conf->raid_disks; j++)
1920 if (conf->mirrors[j].rdev == NULL ||
1921 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1926 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1927 &sync_blocks, still_degraded);
1929 for (j=0; j<conf->copies;j++) {
1930 int d = r10_bio->devs[j].devnum;
1931 if (conf->mirrors[d].rdev &&
1932 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1933 /* This is where we read from */
1934 bio = r10_bio->devs[0].bio;
1935 bio->bi_next = biolist;
1937 bio->bi_private = r10_bio;
1938 bio->bi_end_io = end_sync_read;
1940 bio->bi_sector = r10_bio->devs[j].addr +
1941 conf->mirrors[d].rdev->data_offset;
1942 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1943 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1944 atomic_inc(&r10_bio->remaining);
1945 /* and we write to 'i' */
1947 for (k=0; k<conf->copies; k++)
1948 if (r10_bio->devs[k].devnum == i)
1950 BUG_ON(k == conf->copies);
1951 bio = r10_bio->devs[1].bio;
1952 bio->bi_next = biolist;
1954 bio->bi_private = r10_bio;
1955 bio->bi_end_io = end_sync_write;
1957 bio->bi_sector = r10_bio->devs[k].addr +
1958 conf->mirrors[i].rdev->data_offset;
1959 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1961 r10_bio->devs[0].devnum = d;
1962 r10_bio->devs[1].devnum = i;
1967 if (j == conf->copies) {
1968 /* Cannot recover, so abort the recovery */
1971 atomic_dec(&rb2->remaining);
1973 if (!test_and_set_bit(MD_RECOVERY_INTR,
1975 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1980 if (biolist == NULL) {
1982 r10bio_t *rb2 = r10_bio;
1983 r10_bio = (r10bio_t*) rb2->master_bio;
1984 rb2->master_bio = NULL;
1990 /* resync. Schedule a read for every block at this virt offset */
1993 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1995 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1996 &sync_blocks, mddev->degraded) &&
1997 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1998 /* We can skip this block */
2000 return sync_blocks + sectors_skipped;
2002 if (sync_blocks < max_sync)
2003 max_sync = sync_blocks;
2004 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2006 r10_bio->mddev = mddev;
2007 atomic_set(&r10_bio->remaining, 0);
2008 raise_barrier(conf, 0);
2009 conf->next_resync = sector_nr;
2011 r10_bio->master_bio = NULL;
2012 r10_bio->sector = sector_nr;
2013 set_bit(R10BIO_IsSync, &r10_bio->state);
2014 raid10_find_phys(conf, r10_bio);
2015 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2017 for (i=0; i<conf->copies; i++) {
2018 int d = r10_bio->devs[i].devnum;
2019 bio = r10_bio->devs[i].bio;
2020 bio->bi_end_io = NULL;
2021 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2022 if (conf->mirrors[d].rdev == NULL ||
2023 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2025 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2026 atomic_inc(&r10_bio->remaining);
2027 bio->bi_next = biolist;
2029 bio->bi_private = r10_bio;
2030 bio->bi_end_io = end_sync_read;
2032 bio->bi_sector = r10_bio->devs[i].addr +
2033 conf->mirrors[d].rdev->data_offset;
2034 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2039 for (i=0; i<conf->copies; i++) {
2040 int d = r10_bio->devs[i].devnum;
2041 if (r10_bio->devs[i].bio->bi_end_io)
2042 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
2050 for (bio = biolist; bio ; bio=bio->bi_next) {
2052 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2054 bio->bi_flags |= 1 << BIO_UPTODATE;
2057 bio->bi_phys_segments = 0;
2062 if (sector_nr + max_sync < max_sector)
2063 max_sector = sector_nr + max_sync;
2066 int len = PAGE_SIZE;
2068 if (sector_nr + (len>>9) > max_sector)
2069 len = (max_sector - sector_nr) << 9;
2072 for (bio= biolist ; bio ; bio=bio->bi_next) {
2073 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2074 if (bio_add_page(bio, page, len, 0) == 0) {
2077 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2078 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
2079 /* remove last page from this bio */
2081 bio2->bi_size -= len;
2082 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2088 nr_sectors += len>>9;
2089 sector_nr += len>>9;
2090 } while (biolist->bi_vcnt < RESYNC_PAGES);
2092 r10_bio->sectors = nr_sectors;
2096 biolist = biolist->bi_next;
2098 bio->bi_next = NULL;
2099 r10_bio = bio->bi_private;
2100 r10_bio->sectors = nr_sectors;
2102 if (bio->bi_end_io == end_sync_read) {
2103 md_sync_acct(bio->bi_bdev, nr_sectors);
2104 generic_make_request(bio);
2108 if (sectors_skipped)
2109 /* pretend they weren't skipped, it makes
2110 * no important difference in this case
2112 md_done_sync(mddev, sectors_skipped, 1);
2114 return sectors_skipped + nr_sectors;
2116 /* There is nowhere to write, so all non-sync
2117 * drives must be failed, so try the next chunk...
2119 if (sector_nr + max_sync < max_sector)
2120 max_sector = sector_nr + max_sync;
2122 sectors_skipped += (max_sector - sector_nr);
2124 sector_nr = max_sector;
2129 raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2132 conf_t *conf = mddev->private;
2135 raid_disks = conf->raid_disks;
2137 sectors = conf->dev_sectors;
2139 size = sectors >> conf->chunk_shift;
2140 sector_div(size, conf->far_copies);
2141 size = size * raid_disks;
2142 sector_div(size, conf->near_copies);
2144 return size << conf->chunk_shift;
2148 static conf_t *setup_conf(mddev_t *mddev)
2150 conf_t *conf = NULL;
2152 sector_t stride, size;
2155 if (mddev->chunk_sectors < (PAGE_SIZE >> 9) ||
2156 !is_power_of_2(mddev->chunk_sectors)) {
2157 printk(KERN_ERR "md/raid10: chunk size must be "
2158 "at least PAGE_SIZE(%ld) and be a power of 2.\n", PAGE_SIZE);
2162 nc = mddev->layout & 255;
2163 fc = (mddev->layout >> 8) & 255;
2164 fo = mddev->layout & (1<<16);
2166 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2167 (mddev->layout >> 17)) {
2168 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2169 mdname(mddev), mddev->layout);
2174 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2178 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2183 conf->tmppage = alloc_page(GFP_KERNEL);
2188 conf->raid_disks = mddev->raid_disks;
2189 conf->near_copies = nc;
2190 conf->far_copies = fc;
2191 conf->copies = nc*fc;
2192 conf->far_offset = fo;
2193 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2194 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2196 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2197 r10bio_pool_free, conf);
2198 if (!conf->r10bio_pool)
2201 size = mddev->dev_sectors >> conf->chunk_shift;
2202 sector_div(size, fc);
2203 size = size * conf->raid_disks;
2204 sector_div(size, nc);
2205 /* 'size' is now the number of chunks in the array */
2206 /* calculate "used chunks per device" in 'stride' */
2207 stride = size * conf->copies;
2209 /* We need to round up when dividing by raid_disks to
2210 * get the stride size.
2212 stride += conf->raid_disks - 1;
2213 sector_div(stride, conf->raid_disks);
2215 conf->dev_sectors = stride << conf->chunk_shift;
2220 sector_div(stride, fc);
2221 conf->stride = stride << conf->chunk_shift;
2224 spin_lock_init(&conf->device_lock);
2225 INIT_LIST_HEAD(&conf->retry_list);
2227 spin_lock_init(&conf->resync_lock);
2228 init_waitqueue_head(&conf->wait_barrier);
2230 conf->thread = md_register_thread(raid10d, mddev, NULL);
2234 conf->scale_disks = 0;
2235 conf->mddev = mddev;
2239 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2242 if (conf->r10bio_pool)
2243 mempool_destroy(conf->r10bio_pool);
2244 kfree(conf->mirrors);
2245 safe_put_page(conf->tmppage);
2248 return ERR_PTR(err);
2251 static int run(mddev_t *mddev)
2254 int i, disk_idx, chunk_size;
2255 mirror_info_t *disk;
2260 * copy the already verified devices into our private RAID10
2261 * bookkeeping area. [whatever we allocate in run(),
2262 * should be freed in stop()]
2265 if (mddev->private == NULL) {
2266 conf = setup_conf(mddev);
2268 return PTR_ERR(conf);
2269 mddev->private = conf;
2271 conf = mddev->private;
2275 mddev->queue->queue_lock = &conf->device_lock;
2277 mddev->thread = conf->thread;
2278 conf->thread = NULL;
2280 chunk_size = mddev->chunk_sectors << 9;
2281 blk_queue_io_min(mddev->queue, chunk_size);
2282 if (conf->raid_disks % conf->near_copies)
2283 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2285 blk_queue_io_opt(mddev->queue, chunk_size *
2286 (conf->raid_disks / conf->near_copies));
2288 list_for_each_entry(rdev, &mddev->disks, same_set) {
2289 disk_idx = rdev->raid_disk;
2290 if (disk_idx >= conf->raid_disks
2293 if (conf->scale_disks) {
2294 disk_idx *= conf->scale_disks;
2295 rdev->raid_disk = disk_idx;
2296 /* MOVE 'rd%d' link !! */
2298 disk = conf->mirrors + disk_idx;
2301 disk_stack_limits(mddev->gendisk, rdev->bdev,
2302 rdev->data_offset << 9);
2303 /* as we don't honour merge_bvec_fn, we must never risk
2304 * violating it, so limit max_segments to 1 lying
2305 * within a single page.
2307 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2308 blk_queue_max_segments(mddev->queue, 1);
2309 blk_queue_segment_boundary(mddev->queue,
2310 PAGE_CACHE_SIZE - 1);
2313 disk->head_position = 0;
2315 /* need to check that every block has at least one working mirror */
2316 if (!enough(conf)) {
2317 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2322 mddev->degraded = 0;
2323 for (i = 0; i < conf->raid_disks; i++) {
2325 disk = conf->mirrors + i;
2328 !test_bit(In_sync, &disk->rdev->flags)) {
2329 disk->head_position = 0;
2336 if (mddev->recovery_cp != MaxSector)
2337 printk(KERN_NOTICE "raid10: %s is not clean"
2338 " -- starting background reconstruction\n",
2341 "raid10: raid set %s active with %d out of %d devices\n",
2342 mdname(mddev), conf->raid_disks - mddev->degraded,
2345 * Ok, everything is just fine now
2347 mddev->dev_sectors = conf->dev_sectors;
2348 size = raid10_size(mddev, 0, 0);
2349 md_set_array_sectors(mddev, size);
2350 mddev->resync_max_sectors = size;
2352 mddev->queue->unplug_fn = raid10_unplug;
2353 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2354 mddev->queue->backing_dev_info.congested_data = mddev;
2356 /* Calculate max read-ahead size.
2357 * We need to readahead at least twice a whole stripe....
2361 int stripe = conf->raid_disks *
2362 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2363 stripe /= conf->near_copies;
2364 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2365 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2368 if (conf->near_copies < conf->raid_disks)
2369 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2370 md_integrity_register(mddev);
2374 if (conf->r10bio_pool)
2375 mempool_destroy(conf->r10bio_pool);
2376 safe_put_page(conf->tmppage);
2377 kfree(conf->mirrors);
2379 mddev->private = NULL;
2380 md_unregister_thread(mddev->thread);
2385 static int stop(mddev_t *mddev)
2387 conf_t *conf = mddev->private;
2389 raise_barrier(conf, 0);
2390 lower_barrier(conf);
2392 md_unregister_thread(mddev->thread);
2393 mddev->thread = NULL;
2394 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2395 if (conf->r10bio_pool)
2396 mempool_destroy(conf->r10bio_pool);
2397 kfree(conf->mirrors);
2399 mddev->private = NULL;
2403 static void raid10_quiesce(mddev_t *mddev, int state)
2405 conf_t *conf = mddev->private;
2409 raise_barrier(conf, 0);
2412 lower_barrier(conf);
2417 static void *raid10_takeover_raid0(mddev_t *mddev)
2422 if (mddev->degraded > 0) {
2423 printk(KERN_ERR "error: degraded raid0!\n");
2424 return ERR_PTR(-EINVAL);
2427 /* Update slot numbers to obtain
2428 * degraded raid10 with missing mirrors
2430 list_for_each_entry(rdev, &mddev->disks, same_set) {
2431 rdev->raid_disk *= 2;
2434 /* Set new parameters */
2435 mddev->new_level = 10;
2436 /* new layout: far_copies = 1, near_copies = 2 */
2437 mddev->new_layout = (1<<8) + 2;
2438 mddev->new_chunk_sectors = mddev->chunk_sectors;
2439 mddev->delta_disks = mddev->raid_disks;
2440 mddev->degraded = mddev->raid_disks;
2441 mddev->raid_disks *= 2;
2442 /* make sure it will be not marked as dirty */
2443 mddev->recovery_cp = MaxSector;
2445 conf = setup_conf(mddev);
2446 conf->scale_disks = 2;
2450 static void *raid10_takeover(mddev_t *mddev)
2452 struct raid0_private_data *raid0_priv;
2454 /* raid10 can take over:
2455 * raid0 - providing it has only two drives
2457 if (mddev->level == 0) {
2458 /* for raid0 takeover only one zone is supported */
2459 raid0_priv = mddev->private;
2460 if (raid0_priv->nr_strip_zones > 1) {
2461 printk(KERN_ERR "md: cannot takeover raid 0 with more than one zone.\n");
2462 return ERR_PTR(-EINVAL);
2464 return raid10_takeover_raid0(mddev);
2466 return ERR_PTR(-EINVAL);
2469 static struct mdk_personality raid10_personality =
2473 .owner = THIS_MODULE,
2474 .make_request = make_request,
2478 .error_handler = error,
2479 .hot_add_disk = raid10_add_disk,
2480 .hot_remove_disk= raid10_remove_disk,
2481 .spare_active = raid10_spare_active,
2482 .sync_request = sync_request,
2483 .quiesce = raid10_quiesce,
2484 .size = raid10_size,
2485 .takeover = raid10_takeover,
2488 static int __init raid_init(void)
2490 return register_md_personality(&raid10_personality);
2493 static void raid_exit(void)
2495 unregister_md_personality(&raid10_personality);
2498 module_init(raid_init);
2499 module_exit(raid_exit);
2500 MODULE_LICENSE("GPL");
2501 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2502 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2503 MODULE_ALIAS("md-raid10");
2504 MODULE_ALIAS("md-level-10");