2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
39 * far_offset (stored in bit 16 of layout )
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
47 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
49 * near_copies and far_copies must be at least one, and their product is at most
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
58 * Number of guaranteed r10bios in case of extreme VM load:
60 #define NR_RAID10_BIOS 256
62 /* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
66 static int max_queued_requests = 1024;
68 static void allow_barrier(struct r10conf *conf);
69 static void lower_barrier(struct r10conf *conf);
71 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
73 struct r10conf *conf = data;
74 int size = offsetof(struct r10bio, devs[conf->copies]);
76 /* allocate a r10bio with room for raid_disks entries in the bios array */
77 return kzalloc(size, gfp_flags);
80 static void r10bio_pool_free(void *r10_bio, void *data)
85 /* Maximum size of each resync request */
86 #define RESYNC_BLOCK_SIZE (64*1024)
87 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
88 /* amount of memory to reserve for resync requests */
89 #define RESYNC_WINDOW (1024*1024)
90 /* maximum number of concurrent requests, memory permitting */
91 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
94 * When performing a resync, we need to read and compare, so
95 * we need as many pages are there are copies.
96 * When performing a recovery, we need 2 bios, one for read,
97 * one for write (we recover only one drive per r10buf)
100 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
102 struct r10conf *conf = data;
104 struct r10bio *r10_bio;
109 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
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_kmalloc(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 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
135 &conf->mddev->recovery)) {
136 /* we can share bv_page's during recovery */
137 struct bio *rbio = r10_bio->devs[0].bio;
138 page = rbio->bi_io_vec[i].bv_page;
141 page = alloc_page(gfp_flags);
145 bio->bi_io_vec[i].bv_page = page;
153 safe_put_page(bio->bi_io_vec[i-1].bv_page);
155 for (i = 0; i < RESYNC_PAGES ; i++)
156 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
159 while ( ++j < nalloc )
160 bio_put(r10_bio->devs[j].bio);
161 r10bio_pool_free(r10_bio, conf);
165 static void r10buf_pool_free(void *__r10_bio, void *data)
168 struct r10conf *conf = data;
169 struct r10bio *r10bio = __r10_bio;
172 for (j=0; j < conf->copies; j++) {
173 struct bio *bio = r10bio->devs[j].bio;
175 for (i = 0; i < RESYNC_PAGES; i++) {
176 safe_put_page(bio->bi_io_vec[i].bv_page);
177 bio->bi_io_vec[i].bv_page = NULL;
182 r10bio_pool_free(r10bio, conf);
185 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
189 for (i = 0; i < conf->copies; i++) {
190 struct bio **bio = & r10_bio->devs[i].bio;
191 if (!BIO_SPECIAL(*bio))
197 static void free_r10bio(struct r10bio *r10_bio)
199 struct r10conf *conf = r10_bio->mddev->private;
201 put_all_bios(conf, r10_bio);
202 mempool_free(r10_bio, conf->r10bio_pool);
205 static void put_buf(struct r10bio *r10_bio)
207 struct r10conf *conf = r10_bio->mddev->private;
209 mempool_free(r10_bio, conf->r10buf_pool);
214 static void reschedule_retry(struct r10bio *r10_bio)
217 struct mddev *mddev = r10_bio->mddev;
218 struct r10conf *conf = mddev->private;
220 spin_lock_irqsave(&conf->device_lock, flags);
221 list_add(&r10_bio->retry_list, &conf->retry_list);
223 spin_unlock_irqrestore(&conf->device_lock, flags);
225 /* wake up frozen array... */
226 wake_up(&conf->wait_barrier);
228 md_wakeup_thread(mddev->thread);
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
236 static void raid_end_bio_io(struct r10bio *r10_bio)
238 struct bio *bio = r10_bio->master_bio;
240 struct r10conf *conf = r10_bio->mddev->private;
242 if (bio->bi_phys_segments) {
244 spin_lock_irqsave(&conf->device_lock, flags);
245 bio->bi_phys_segments--;
246 done = (bio->bi_phys_segments == 0);
247 spin_unlock_irqrestore(&conf->device_lock, flags);
250 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
251 clear_bit(BIO_UPTODATE, &bio->bi_flags);
255 * Wake up any possible resync thread that waits for the device
260 free_r10bio(r10_bio);
264 * Update disk head position estimator based on IRQ completion info.
266 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
268 struct r10conf *conf = r10_bio->mddev->private;
270 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
271 r10_bio->devs[slot].addr + (r10_bio->sectors);
275 * Find the disk number which triggered given bio
277 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
278 struct bio *bio, int *slotp)
282 for (slot = 0; slot < conf->copies; slot++)
283 if (r10_bio->devs[slot].bio == bio)
286 BUG_ON(slot == conf->copies);
287 update_head_pos(slot, r10_bio);
291 return r10_bio->devs[slot].devnum;
294 static void raid10_end_read_request(struct bio *bio, int error)
296 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
297 struct r10bio *r10_bio = bio->bi_private;
299 struct r10conf *conf = r10_bio->mddev->private;
302 slot = r10_bio->read_slot;
303 dev = r10_bio->devs[slot].devnum;
305 * this branch is our 'one mirror IO has finished' event handler:
307 update_head_pos(slot, r10_bio);
311 * Set R10BIO_Uptodate in our master bio, so that
312 * we will return a good error code to the higher
313 * levels even if IO on some other mirrored buffer fails.
315 * The 'master' represents the composite IO operation to
316 * user-side. So if something waits for IO, then it will
317 * wait for the 'master' bio.
319 set_bit(R10BIO_Uptodate, &r10_bio->state);
320 raid_end_bio_io(r10_bio);
321 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
324 * oops, read error - keep the refcount on the rdev
326 char b[BDEVNAME_SIZE];
327 printk_ratelimited(KERN_ERR
328 "md/raid10:%s: %s: rescheduling sector %llu\n",
330 bdevname(conf->mirrors[dev].rdev->bdev, b),
331 (unsigned long long)r10_bio->sector);
332 set_bit(R10BIO_ReadError, &r10_bio->state);
333 reschedule_retry(r10_bio);
337 static void close_write(struct r10bio *r10_bio)
339 /* clear the bitmap if all writes complete successfully */
340 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
342 !test_bit(R10BIO_Degraded, &r10_bio->state),
344 md_write_end(r10_bio->mddev);
347 static void one_write_done(struct r10bio *r10_bio)
349 if (atomic_dec_and_test(&r10_bio->remaining)) {
350 if (test_bit(R10BIO_WriteError, &r10_bio->state))
351 reschedule_retry(r10_bio);
353 close_write(r10_bio);
354 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
355 reschedule_retry(r10_bio);
357 raid_end_bio_io(r10_bio);
362 static void raid10_end_write_request(struct bio *bio, int error)
364 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
365 struct r10bio *r10_bio = bio->bi_private;
368 struct r10conf *conf = r10_bio->mddev->private;
371 dev = find_bio_disk(conf, r10_bio, bio, &slot);
374 * this branch is our 'one mirror IO has finished' event handler:
377 set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags);
378 set_bit(R10BIO_WriteError, &r10_bio->state);
382 * Set R10BIO_Uptodate in our master bio, so that
383 * we will return a good error code for to the higher
384 * levels even if IO on some other mirrored buffer fails.
386 * The 'master' represents the composite IO operation to
387 * user-side. So if something waits for IO, then it will
388 * wait for the 'master' bio.
393 set_bit(R10BIO_Uptodate, &r10_bio->state);
395 /* Maybe we can clear some bad blocks. */
396 if (is_badblock(conf->mirrors[dev].rdev,
397 r10_bio->devs[slot].addr,
399 &first_bad, &bad_sectors)) {
401 r10_bio->devs[slot].bio = IO_MADE_GOOD;
403 set_bit(R10BIO_MadeGood, &r10_bio->state);
409 * Let's see if all mirrored write operations have finished
412 one_write_done(r10_bio);
414 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
419 * RAID10 layout manager
420 * As well as the chunksize and raid_disks count, there are two
421 * parameters: near_copies and far_copies.
422 * near_copies * far_copies must be <= raid_disks.
423 * Normally one of these will be 1.
424 * If both are 1, we get raid0.
425 * If near_copies == raid_disks, we get raid1.
427 * Chunks are laid out in raid0 style with near_copies copies of the
428 * first chunk, followed by near_copies copies of the next chunk and
430 * If far_copies > 1, then after 1/far_copies of the array has been assigned
431 * as described above, we start again with a device offset of near_copies.
432 * So we effectively have another copy of the whole array further down all
433 * the drives, but with blocks on different drives.
434 * With this layout, and block is never stored twice on the one device.
436 * raid10_find_phys finds the sector offset of a given virtual sector
437 * on each device that it is on.
439 * raid10_find_virt does the reverse mapping, from a device and a
440 * sector offset to a virtual address
443 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
453 /* now calculate first sector/dev */
454 chunk = r10bio->sector >> conf->chunk_shift;
455 sector = r10bio->sector & conf->chunk_mask;
457 chunk *= conf->near_copies;
459 dev = sector_div(stripe, conf->raid_disks);
460 if (conf->far_offset)
461 stripe *= conf->far_copies;
463 sector += stripe << conf->chunk_shift;
465 /* and calculate all the others */
466 for (n=0; n < conf->near_copies; n++) {
469 r10bio->devs[slot].addr = sector;
470 r10bio->devs[slot].devnum = d;
473 for (f = 1; f < conf->far_copies; f++) {
474 d += conf->near_copies;
475 if (d >= conf->raid_disks)
476 d -= conf->raid_disks;
478 r10bio->devs[slot].devnum = d;
479 r10bio->devs[slot].addr = s;
483 if (dev >= conf->raid_disks) {
485 sector += (conf->chunk_mask + 1);
488 BUG_ON(slot != conf->copies);
491 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
493 sector_t offset, chunk, vchunk;
495 offset = sector & conf->chunk_mask;
496 if (conf->far_offset) {
498 chunk = sector >> conf->chunk_shift;
499 fc = sector_div(chunk, conf->far_copies);
500 dev -= fc * conf->near_copies;
502 dev += conf->raid_disks;
504 while (sector >= conf->stride) {
505 sector -= conf->stride;
506 if (dev < conf->near_copies)
507 dev += conf->raid_disks - conf->near_copies;
509 dev -= conf->near_copies;
511 chunk = sector >> conf->chunk_shift;
513 vchunk = chunk * conf->raid_disks + dev;
514 sector_div(vchunk, conf->near_copies);
515 return (vchunk << conf->chunk_shift) + offset;
519 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
521 * @bvm: properties of new bio
522 * @biovec: the request that could be merged to it.
524 * Return amount of bytes we can accept at this offset
525 * If near_copies == raid_disk, there are no striping issues,
526 * but in that case, the function isn't called at all.
528 static int raid10_mergeable_bvec(struct request_queue *q,
529 struct bvec_merge_data *bvm,
530 struct bio_vec *biovec)
532 struct mddev *mddev = q->queuedata;
533 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
535 unsigned int chunk_sectors = mddev->chunk_sectors;
536 unsigned int bio_sectors = bvm->bi_size >> 9;
538 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
539 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
540 if (max <= biovec->bv_len && bio_sectors == 0)
541 return biovec->bv_len;
547 * This routine returns the disk from which the requested read should
548 * be done. There is a per-array 'next expected sequential IO' sector
549 * number - if this matches on the next IO then we use the last disk.
550 * There is also a per-disk 'last know head position' sector that is
551 * maintained from IRQ contexts, both the normal and the resync IO
552 * completion handlers update this position correctly. If there is no
553 * perfect sequential match then we pick the disk whose head is closest.
555 * If there are 2 mirrors in the same 2 devices, performance degrades
556 * because position is mirror, not device based.
558 * The rdev for the device selected will have nr_pending incremented.
562 * FIXME: possibly should rethink readbalancing and do it differently
563 * depending on near_copies / far_copies geometry.
565 static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_sectors)
567 const sector_t this_sector = r10_bio->sector;
569 int sectors = r10_bio->sectors;
570 int best_good_sectors;
571 sector_t new_distance, best_dist;
572 struct md_rdev *rdev;
576 raid10_find_phys(conf, r10_bio);
579 sectors = r10_bio->sectors;
581 best_dist = MaxSector;
582 best_good_sectors = 0;
585 * Check if we can balance. We can balance on the whole
586 * device if no resync is going on (recovery is ok), or below
587 * the resync window. We take the first readable disk when
588 * above the resync window.
590 if (conf->mddev->recovery_cp < MaxSector
591 && (this_sector + sectors >= conf->next_resync))
594 for (slot = 0; slot < conf->copies ; slot++) {
599 if (r10_bio->devs[slot].bio == IO_BLOCKED)
601 disk = r10_bio->devs[slot].devnum;
602 rdev = rcu_dereference(conf->mirrors[disk].rdev);
605 if (!test_bit(In_sync, &rdev->flags))
608 dev_sector = r10_bio->devs[slot].addr;
609 if (is_badblock(rdev, dev_sector, sectors,
610 &first_bad, &bad_sectors)) {
611 if (best_dist < MaxSector)
612 /* Already have a better slot */
614 if (first_bad <= dev_sector) {
615 /* Cannot read here. If this is the
616 * 'primary' device, then we must not read
617 * beyond 'bad_sectors' from another device.
619 bad_sectors -= (dev_sector - first_bad);
620 if (!do_balance && sectors > bad_sectors)
621 sectors = bad_sectors;
622 if (best_good_sectors > sectors)
623 best_good_sectors = sectors;
625 sector_t good_sectors =
626 first_bad - dev_sector;
627 if (good_sectors > best_good_sectors) {
628 best_good_sectors = good_sectors;
632 /* Must read from here */
637 best_good_sectors = sectors;
642 /* This optimisation is debatable, and completely destroys
643 * sequential read speed for 'far copies' arrays. So only
644 * keep it for 'near' arrays, and review those later.
646 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
649 /* for far > 1 always use the lowest address */
650 if (conf->far_copies > 1)
651 new_distance = r10_bio->devs[slot].addr;
653 new_distance = abs(r10_bio->devs[slot].addr -
654 conf->mirrors[disk].head_position);
655 if (new_distance < best_dist) {
656 best_dist = new_distance;
660 if (slot == conf->copies)
664 disk = r10_bio->devs[slot].devnum;
665 rdev = rcu_dereference(conf->mirrors[disk].rdev);
668 atomic_inc(&rdev->nr_pending);
669 if (test_bit(Faulty, &rdev->flags)) {
670 /* Cannot risk returning a device that failed
671 * before we inc'ed nr_pending
673 rdev_dec_pending(rdev, conf->mddev);
676 r10_bio->read_slot = slot;
680 *max_sectors = best_good_sectors;
685 static int raid10_congested(void *data, int bits)
687 struct mddev *mddev = data;
688 struct r10conf *conf = mddev->private;
691 if ((bits & (1 << BDI_async_congested)) &&
692 conf->pending_count >= max_queued_requests)
695 if (mddev_congested(mddev, bits))
698 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
699 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
700 if (rdev && !test_bit(Faulty, &rdev->flags)) {
701 struct request_queue *q = bdev_get_queue(rdev->bdev);
703 ret |= bdi_congested(&q->backing_dev_info, bits);
710 static void flush_pending_writes(struct r10conf *conf)
712 /* Any writes that have been queued but are awaiting
713 * bitmap updates get flushed here.
715 spin_lock_irq(&conf->device_lock);
717 if (conf->pending_bio_list.head) {
719 bio = bio_list_get(&conf->pending_bio_list);
720 conf->pending_count = 0;
721 spin_unlock_irq(&conf->device_lock);
722 /* flush any pending bitmap writes to disk
723 * before proceeding w/ I/O */
724 bitmap_unplug(conf->mddev->bitmap);
725 wake_up(&conf->wait_barrier);
727 while (bio) { /* submit pending writes */
728 struct bio *next = bio->bi_next;
730 generic_make_request(bio);
734 spin_unlock_irq(&conf->device_lock);
738 * Sometimes we need to suspend IO while we do something else,
739 * either some resync/recovery, or reconfigure the array.
740 * To do this we raise a 'barrier'.
741 * The 'barrier' is a counter that can be raised multiple times
742 * to count how many activities are happening which preclude
744 * We can only raise the barrier if there is no pending IO.
745 * i.e. if nr_pending == 0.
746 * We choose only to raise the barrier if no-one is waiting for the
747 * barrier to go down. This means that as soon as an IO request
748 * is ready, no other operations which require a barrier will start
749 * until the IO request has had a chance.
751 * So: regular IO calls 'wait_barrier'. When that returns there
752 * is no backgroup IO happening, It must arrange to call
753 * allow_barrier when it has finished its IO.
754 * backgroup IO calls must call raise_barrier. Once that returns
755 * there is no normal IO happeing. It must arrange to call
756 * lower_barrier when the particular background IO completes.
759 static void raise_barrier(struct r10conf *conf, int force)
761 BUG_ON(force && !conf->barrier);
762 spin_lock_irq(&conf->resync_lock);
764 /* Wait until no block IO is waiting (unless 'force') */
765 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
766 conf->resync_lock, );
768 /* block any new IO from starting */
771 /* Now wait for all pending IO to complete */
772 wait_event_lock_irq(conf->wait_barrier,
773 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
774 conf->resync_lock, );
776 spin_unlock_irq(&conf->resync_lock);
779 static void lower_barrier(struct r10conf *conf)
782 spin_lock_irqsave(&conf->resync_lock, flags);
784 spin_unlock_irqrestore(&conf->resync_lock, flags);
785 wake_up(&conf->wait_barrier);
788 static void wait_barrier(struct r10conf *conf)
790 spin_lock_irq(&conf->resync_lock);
793 /* Wait for the barrier to drop.
794 * However if there are already pending
795 * requests (preventing the barrier from
796 * rising completely), and the
797 * pre-process bio queue isn't empty,
798 * then don't wait, as we need to empty
799 * that queue to get the nr_pending
802 wait_event_lock_irq(conf->wait_barrier,
806 !bio_list_empty(current->bio_list)),
812 spin_unlock_irq(&conf->resync_lock);
815 static void allow_barrier(struct r10conf *conf)
818 spin_lock_irqsave(&conf->resync_lock, flags);
820 spin_unlock_irqrestore(&conf->resync_lock, flags);
821 wake_up(&conf->wait_barrier);
824 static void freeze_array(struct r10conf *conf)
826 /* stop syncio and normal IO and wait for everything to
828 * We increment barrier and nr_waiting, and then
829 * wait until nr_pending match nr_queued+1
830 * This is called in the context of one normal IO request
831 * that has failed. Thus any sync request that might be pending
832 * will be blocked by nr_pending, and we need to wait for
833 * pending IO requests to complete or be queued for re-try.
834 * Thus the number queued (nr_queued) plus this request (1)
835 * must match the number of pending IOs (nr_pending) before
838 spin_lock_irq(&conf->resync_lock);
841 wait_event_lock_irq(conf->wait_barrier,
842 conf->nr_pending == conf->nr_queued+1,
844 flush_pending_writes(conf));
846 spin_unlock_irq(&conf->resync_lock);
849 static void unfreeze_array(struct r10conf *conf)
851 /* reverse the effect of the freeze */
852 spin_lock_irq(&conf->resync_lock);
855 wake_up(&conf->wait_barrier);
856 spin_unlock_irq(&conf->resync_lock);
859 static void make_request(struct mddev *mddev, struct bio * bio)
861 struct r10conf *conf = mddev->private;
862 struct mirror_info *mirror;
863 struct r10bio *r10_bio;
864 struct bio *read_bio;
866 int chunk_sects = conf->chunk_mask + 1;
867 const int rw = bio_data_dir(bio);
868 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
869 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
871 struct md_rdev *blocked_rdev;
876 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
877 md_flush_request(mddev, bio);
881 /* If this request crosses a chunk boundary, we need to
882 * split it. This will only happen for 1 PAGE (or less) requests.
884 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
886 conf->near_copies < conf->raid_disks)) {
888 /* Sanity check -- queue functions should prevent this happening */
889 if (bio->bi_vcnt != 1 ||
892 /* This is a one page bio that upper layers
893 * refuse to split for us, so we need to split it.
896 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
898 /* Each of these 'make_request' calls will call 'wait_barrier'.
899 * If the first succeeds but the second blocks due to the resync
900 * thread raising the barrier, we will deadlock because the
901 * IO to the underlying device will be queued in generic_make_request
902 * and will never complete, so will never reduce nr_pending.
903 * So increment nr_waiting here so no new raise_barriers will
904 * succeed, and so the second wait_barrier cannot block.
906 spin_lock_irq(&conf->resync_lock);
908 spin_unlock_irq(&conf->resync_lock);
910 make_request(mddev, &bp->bio1);
911 make_request(mddev, &bp->bio2);
913 spin_lock_irq(&conf->resync_lock);
915 wake_up(&conf->wait_barrier);
916 spin_unlock_irq(&conf->resync_lock);
918 bio_pair_release(bp);
921 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
922 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
923 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
929 md_write_start(mddev, bio);
932 * Register the new request and wait if the reconstruction
933 * thread has put up a bar for new requests.
934 * Continue immediately if no resync is active currently.
938 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
940 r10_bio->master_bio = bio;
941 r10_bio->sectors = bio->bi_size >> 9;
943 r10_bio->mddev = mddev;
944 r10_bio->sector = bio->bi_sector;
947 /* We might need to issue multiple reads to different
948 * devices if there are bad blocks around, so we keep
949 * track of the number of reads in bio->bi_phys_segments.
950 * If this is 0, there is only one r10_bio and no locking
951 * will be needed when the request completes. If it is
952 * non-zero, then it is the number of not-completed requests.
954 bio->bi_phys_segments = 0;
955 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
959 * read balancing logic:
965 disk = read_balance(conf, r10_bio, &max_sectors);
966 slot = r10_bio->read_slot;
968 raid_end_bio_io(r10_bio);
971 mirror = conf->mirrors + disk;
973 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
974 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
977 r10_bio->devs[slot].bio = read_bio;
979 read_bio->bi_sector = r10_bio->devs[slot].addr +
980 mirror->rdev->data_offset;
981 read_bio->bi_bdev = mirror->rdev->bdev;
982 read_bio->bi_end_io = raid10_end_read_request;
983 read_bio->bi_rw = READ | do_sync;
984 read_bio->bi_private = r10_bio;
986 if (max_sectors < r10_bio->sectors) {
987 /* Could not read all from this device, so we will
988 * need another r10_bio.
990 sectors_handled = (r10_bio->sectors + max_sectors
992 r10_bio->sectors = max_sectors;
993 spin_lock_irq(&conf->device_lock);
994 if (bio->bi_phys_segments == 0)
995 bio->bi_phys_segments = 2;
997 bio->bi_phys_segments++;
998 spin_unlock(&conf->device_lock);
999 /* Cannot call generic_make_request directly
1000 * as that will be queued in __generic_make_request
1001 * and subsequent mempool_alloc might block
1002 * waiting for it. so hand bio over to raid10d.
1004 reschedule_retry(r10_bio);
1006 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1008 r10_bio->master_bio = bio;
1009 r10_bio->sectors = ((bio->bi_size >> 9)
1012 r10_bio->mddev = mddev;
1013 r10_bio->sector = bio->bi_sector + sectors_handled;
1016 generic_make_request(read_bio);
1023 if (conf->pending_count >= max_queued_requests) {
1024 md_wakeup_thread(mddev->thread);
1025 wait_event(conf->wait_barrier,
1026 conf->pending_count < max_queued_requests);
1028 /* first select target devices under rcu_lock and
1029 * inc refcount on their rdev. Record them by setting
1031 * If there are known/acknowledged bad blocks on any device
1032 * on which we have seen a write error, we want to avoid
1033 * writing to those blocks. This potentially requires several
1034 * writes to write around the bad blocks. Each set of writes
1035 * gets its own r10_bio with a set of bios attached. The number
1036 * of r10_bios is recored in bio->bi_phys_segments just as with
1039 plugged = mddev_check_plugged(mddev);
1041 raid10_find_phys(conf, r10_bio);
1043 blocked_rdev = NULL;
1045 max_sectors = r10_bio->sectors;
1047 for (i = 0; i < conf->copies; i++) {
1048 int d = r10_bio->devs[i].devnum;
1049 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1050 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1051 atomic_inc(&rdev->nr_pending);
1052 blocked_rdev = rdev;
1055 r10_bio->devs[i].bio = NULL;
1056 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1057 set_bit(R10BIO_Degraded, &r10_bio->state);
1060 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1062 sector_t dev_sector = r10_bio->devs[i].addr;
1066 is_bad = is_badblock(rdev, dev_sector,
1068 &first_bad, &bad_sectors);
1070 /* Mustn't write here until the bad block
1073 atomic_inc(&rdev->nr_pending);
1074 set_bit(BlockedBadBlocks, &rdev->flags);
1075 blocked_rdev = rdev;
1078 if (is_bad && first_bad <= dev_sector) {
1079 /* Cannot write here at all */
1080 bad_sectors -= (dev_sector - first_bad);
1081 if (bad_sectors < max_sectors)
1082 /* Mustn't write more than bad_sectors
1083 * to other devices yet
1085 max_sectors = bad_sectors;
1086 /* We don't set R10BIO_Degraded as that
1087 * only applies if the disk is missing,
1088 * so it might be re-added, and we want to
1089 * know to recover this chunk.
1090 * In this case the device is here, and the
1091 * fact that this chunk is not in-sync is
1092 * recorded in the bad block log.
1097 int good_sectors = first_bad - dev_sector;
1098 if (good_sectors < max_sectors)
1099 max_sectors = good_sectors;
1102 r10_bio->devs[i].bio = bio;
1103 atomic_inc(&rdev->nr_pending);
1107 if (unlikely(blocked_rdev)) {
1108 /* Have to wait for this device to get unblocked, then retry */
1112 for (j = 0; j < i; j++)
1113 if (r10_bio->devs[j].bio) {
1114 d = r10_bio->devs[j].devnum;
1115 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1117 allow_barrier(conf);
1118 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1123 if (max_sectors < r10_bio->sectors) {
1124 /* We are splitting this into multiple parts, so
1125 * we need to prepare for allocating another r10_bio.
1127 r10_bio->sectors = max_sectors;
1128 spin_lock_irq(&conf->device_lock);
1129 if (bio->bi_phys_segments == 0)
1130 bio->bi_phys_segments = 2;
1132 bio->bi_phys_segments++;
1133 spin_unlock_irq(&conf->device_lock);
1135 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1137 atomic_set(&r10_bio->remaining, 1);
1138 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1140 for (i = 0; i < conf->copies; i++) {
1142 int d = r10_bio->devs[i].devnum;
1143 if (!r10_bio->devs[i].bio)
1146 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1147 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1149 r10_bio->devs[i].bio = mbio;
1151 mbio->bi_sector = (r10_bio->devs[i].addr+
1152 conf->mirrors[d].rdev->data_offset);
1153 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1154 mbio->bi_end_io = raid10_end_write_request;
1155 mbio->bi_rw = WRITE | do_sync | do_fua;
1156 mbio->bi_private = r10_bio;
1158 atomic_inc(&r10_bio->remaining);
1159 spin_lock_irqsave(&conf->device_lock, flags);
1160 bio_list_add(&conf->pending_bio_list, mbio);
1161 conf->pending_count++;
1162 spin_unlock_irqrestore(&conf->device_lock, flags);
1165 /* Don't remove the bias on 'remaining' (one_write_done) until
1166 * after checking if we need to go around again.
1169 if (sectors_handled < (bio->bi_size >> 9)) {
1170 one_write_done(r10_bio);
1171 /* We need another r10_bio. It has already been counted
1172 * in bio->bi_phys_segments.
1174 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1176 r10_bio->master_bio = bio;
1177 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1179 r10_bio->mddev = mddev;
1180 r10_bio->sector = bio->bi_sector + sectors_handled;
1184 one_write_done(r10_bio);
1186 /* In case raid10d snuck in to freeze_array */
1187 wake_up(&conf->wait_barrier);
1189 if (do_sync || !mddev->bitmap || !plugged)
1190 md_wakeup_thread(mddev->thread);
1193 static void status(struct seq_file *seq, struct mddev *mddev)
1195 struct r10conf *conf = mddev->private;
1198 if (conf->near_copies < conf->raid_disks)
1199 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1200 if (conf->near_copies > 1)
1201 seq_printf(seq, " %d near-copies", conf->near_copies);
1202 if (conf->far_copies > 1) {
1203 if (conf->far_offset)
1204 seq_printf(seq, " %d offset-copies", conf->far_copies);
1206 seq_printf(seq, " %d far-copies", conf->far_copies);
1208 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1209 conf->raid_disks - mddev->degraded);
1210 for (i = 0; i < conf->raid_disks; i++)
1211 seq_printf(seq, "%s",
1212 conf->mirrors[i].rdev &&
1213 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1214 seq_printf(seq, "]");
1217 /* check if there are enough drives for
1218 * every block to appear on atleast one.
1219 * Don't consider the device numbered 'ignore'
1220 * as we might be about to remove it.
1222 static int enough(struct r10conf *conf, int ignore)
1227 int n = conf->copies;
1230 if (conf->mirrors[first].rdev &&
1233 first = (first+1) % conf->raid_disks;
1237 } while (first != 0);
1241 static void error(struct mddev *mddev, struct md_rdev *rdev)
1243 char b[BDEVNAME_SIZE];
1244 struct r10conf *conf = mddev->private;
1247 * If it is not operational, then we have already marked it as dead
1248 * else if it is the last working disks, ignore the error, let the
1249 * next level up know.
1250 * else mark the drive as failed
1252 if (test_bit(In_sync, &rdev->flags)
1253 && !enough(conf, rdev->raid_disk))
1255 * Don't fail the drive, just return an IO error.
1258 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1259 unsigned long flags;
1260 spin_lock_irqsave(&conf->device_lock, flags);
1262 spin_unlock_irqrestore(&conf->device_lock, flags);
1264 * if recovery is running, make sure it aborts.
1266 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1268 set_bit(Blocked, &rdev->flags);
1269 set_bit(Faulty, &rdev->flags);
1270 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1272 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1273 "md/raid10:%s: Operation continuing on %d devices.\n",
1274 mdname(mddev), bdevname(rdev->bdev, b),
1275 mdname(mddev), conf->raid_disks - mddev->degraded);
1278 static void print_conf(struct r10conf *conf)
1281 struct mirror_info *tmp;
1283 printk(KERN_DEBUG "RAID10 conf printout:\n");
1285 printk(KERN_DEBUG "(!conf)\n");
1288 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1291 for (i = 0; i < conf->raid_disks; i++) {
1292 char b[BDEVNAME_SIZE];
1293 tmp = conf->mirrors + i;
1295 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1296 i, !test_bit(In_sync, &tmp->rdev->flags),
1297 !test_bit(Faulty, &tmp->rdev->flags),
1298 bdevname(tmp->rdev->bdev,b));
1302 static void close_sync(struct r10conf *conf)
1305 allow_barrier(conf);
1307 mempool_destroy(conf->r10buf_pool);
1308 conf->r10buf_pool = NULL;
1311 static int raid10_spare_active(struct mddev *mddev)
1314 struct r10conf *conf = mddev->private;
1315 struct mirror_info *tmp;
1317 unsigned long flags;
1320 * Find all non-in_sync disks within the RAID10 configuration
1321 * and mark them in_sync
1323 for (i = 0; i < conf->raid_disks; i++) {
1324 tmp = conf->mirrors + i;
1326 && !test_bit(Faulty, &tmp->rdev->flags)
1327 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1329 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1332 spin_lock_irqsave(&conf->device_lock, flags);
1333 mddev->degraded -= count;
1334 spin_unlock_irqrestore(&conf->device_lock, flags);
1341 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1343 struct r10conf *conf = mddev->private;
1347 int last = conf->raid_disks - 1;
1349 if (mddev->recovery_cp < MaxSector)
1350 /* only hot-add to in-sync arrays, as recovery is
1351 * very different from resync
1354 if (!enough(conf, -1))
1357 if (rdev->raid_disk >= 0)
1358 first = last = rdev->raid_disk;
1360 if (rdev->saved_raid_disk >= first &&
1361 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1362 mirror = rdev->saved_raid_disk;
1365 for ( ; mirror <= last ; mirror++) {
1366 struct mirror_info *p = &conf->mirrors[mirror];
1367 if (p->recovery_disabled == mddev->recovery_disabled)
1372 disk_stack_limits(mddev->gendisk, rdev->bdev,
1373 rdev->data_offset << 9);
1374 /* as we don't honour merge_bvec_fn, we must
1375 * never risk violating it, so limit
1376 * ->max_segments to one lying with a single
1377 * page, as a one page request is never in
1380 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1381 blk_queue_max_segments(mddev->queue, 1);
1382 blk_queue_segment_boundary(mddev->queue,
1383 PAGE_CACHE_SIZE - 1);
1386 p->head_position = 0;
1387 p->recovery_disabled = mddev->recovery_disabled - 1;
1388 rdev->raid_disk = mirror;
1390 if (rdev->saved_raid_disk != mirror)
1392 rcu_assign_pointer(p->rdev, rdev);
1396 md_integrity_add_rdev(rdev, mddev);
1401 static int raid10_remove_disk(struct mddev *mddev, int number)
1403 struct r10conf *conf = mddev->private;
1405 struct md_rdev *rdev;
1406 struct mirror_info *p = conf->mirrors+ number;
1411 if (test_bit(In_sync, &rdev->flags) ||
1412 atomic_read(&rdev->nr_pending)) {
1416 /* Only remove faulty devices in recovery
1419 if (!test_bit(Faulty, &rdev->flags) &&
1420 mddev->recovery_disabled != p->recovery_disabled &&
1427 if (atomic_read(&rdev->nr_pending)) {
1428 /* lost the race, try later */
1433 err = md_integrity_register(mddev);
1442 static void end_sync_read(struct bio *bio, int error)
1444 struct r10bio *r10_bio = bio->bi_private;
1445 struct r10conf *conf = r10_bio->mddev->private;
1448 d = find_bio_disk(conf, r10_bio, bio, NULL);
1450 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1451 set_bit(R10BIO_Uptodate, &r10_bio->state);
1453 /* The write handler will notice the lack of
1454 * R10BIO_Uptodate and record any errors etc
1456 atomic_add(r10_bio->sectors,
1457 &conf->mirrors[d].rdev->corrected_errors);
1459 /* for reconstruct, we always reschedule after a read.
1460 * for resync, only after all reads
1462 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1463 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1464 atomic_dec_and_test(&r10_bio->remaining)) {
1465 /* we have read all the blocks,
1466 * do the comparison in process context in raid10d
1468 reschedule_retry(r10_bio);
1472 static void end_sync_request(struct r10bio *r10_bio)
1474 struct mddev *mddev = r10_bio->mddev;
1476 while (atomic_dec_and_test(&r10_bio->remaining)) {
1477 if (r10_bio->master_bio == NULL) {
1478 /* the primary of several recovery bios */
1479 sector_t s = r10_bio->sectors;
1480 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1481 test_bit(R10BIO_WriteError, &r10_bio->state))
1482 reschedule_retry(r10_bio);
1485 md_done_sync(mddev, s, 1);
1488 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1489 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1490 test_bit(R10BIO_WriteError, &r10_bio->state))
1491 reschedule_retry(r10_bio);
1499 static void end_sync_write(struct bio *bio, int error)
1501 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1502 struct r10bio *r10_bio = bio->bi_private;
1503 struct mddev *mddev = r10_bio->mddev;
1504 struct r10conf *conf = mddev->private;
1510 d = find_bio_disk(conf, r10_bio, bio, &slot);
1513 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
1514 set_bit(R10BIO_WriteError, &r10_bio->state);
1515 } else if (is_badblock(conf->mirrors[d].rdev,
1516 r10_bio->devs[slot].addr,
1518 &first_bad, &bad_sectors))
1519 set_bit(R10BIO_MadeGood, &r10_bio->state);
1521 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1523 end_sync_request(r10_bio);
1527 * Note: sync and recover and handled very differently for raid10
1528 * This code is for resync.
1529 * For resync, we read through virtual addresses and read all blocks.
1530 * If there is any error, we schedule a write. The lowest numbered
1531 * drive is authoritative.
1532 * However requests come for physical address, so we need to map.
1533 * For every physical address there are raid_disks/copies virtual addresses,
1534 * which is always are least one, but is not necessarly an integer.
1535 * This means that a physical address can span multiple chunks, so we may
1536 * have to submit multiple io requests for a single sync request.
1539 * We check if all blocks are in-sync and only write to blocks that
1542 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1544 struct r10conf *conf = mddev->private;
1546 struct bio *tbio, *fbio;
1548 atomic_set(&r10_bio->remaining, 1);
1550 /* find the first device with a block */
1551 for (i=0; i<conf->copies; i++)
1552 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1555 if (i == conf->copies)
1559 fbio = r10_bio->devs[i].bio;
1561 /* now find blocks with errors */
1562 for (i=0 ; i < conf->copies ; i++) {
1564 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1566 tbio = r10_bio->devs[i].bio;
1568 if (tbio->bi_end_io != end_sync_read)
1572 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1573 /* We know that the bi_io_vec layout is the same for
1574 * both 'first' and 'i', so we just compare them.
1575 * All vec entries are PAGE_SIZE;
1577 for (j = 0; j < vcnt; j++)
1578 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1579 page_address(tbio->bi_io_vec[j].bv_page),
1584 mddev->resync_mismatches += r10_bio->sectors;
1585 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1586 /* Don't fix anything. */
1589 /* Ok, we need to write this bio, either to correct an
1590 * inconsistency or to correct an unreadable block.
1591 * First we need to fixup bv_offset, bv_len and
1592 * bi_vecs, as the read request might have corrupted these
1594 tbio->bi_vcnt = vcnt;
1595 tbio->bi_size = r10_bio->sectors << 9;
1597 tbio->bi_phys_segments = 0;
1598 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1599 tbio->bi_flags |= 1 << BIO_UPTODATE;
1600 tbio->bi_next = NULL;
1601 tbio->bi_rw = WRITE;
1602 tbio->bi_private = r10_bio;
1603 tbio->bi_sector = r10_bio->devs[i].addr;
1605 for (j=0; j < vcnt ; j++) {
1606 tbio->bi_io_vec[j].bv_offset = 0;
1607 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1609 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1610 page_address(fbio->bi_io_vec[j].bv_page),
1613 tbio->bi_end_io = end_sync_write;
1615 d = r10_bio->devs[i].devnum;
1616 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1617 atomic_inc(&r10_bio->remaining);
1618 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1620 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1621 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1622 generic_make_request(tbio);
1626 if (atomic_dec_and_test(&r10_bio->remaining)) {
1627 md_done_sync(mddev, r10_bio->sectors, 1);
1633 * Now for the recovery code.
1634 * Recovery happens across physical sectors.
1635 * We recover all non-is_sync drives by finding the virtual address of
1636 * each, and then choose a working drive that also has that virt address.
1637 * There is a separate r10_bio for each non-in_sync drive.
1638 * Only the first two slots are in use. The first for reading,
1639 * The second for writing.
1642 static void fix_recovery_read_error(struct r10bio *r10_bio)
1644 /* We got a read error during recovery.
1645 * We repeat the read in smaller page-sized sections.
1646 * If a read succeeds, write it to the new device or record
1647 * a bad block if we cannot.
1648 * If a read fails, record a bad block on both old and
1651 struct mddev *mddev = r10_bio->mddev;
1652 struct r10conf *conf = mddev->private;
1653 struct bio *bio = r10_bio->devs[0].bio;
1655 int sectors = r10_bio->sectors;
1657 int dr = r10_bio->devs[0].devnum;
1658 int dw = r10_bio->devs[1].devnum;
1662 struct md_rdev *rdev;
1666 if (s > (PAGE_SIZE>>9))
1669 rdev = conf->mirrors[dr].rdev;
1670 addr = r10_bio->devs[0].addr + sect,
1671 ok = sync_page_io(rdev,
1674 bio->bi_io_vec[idx].bv_page,
1677 rdev = conf->mirrors[dw].rdev;
1678 addr = r10_bio->devs[1].addr + sect;
1679 ok = sync_page_io(rdev,
1682 bio->bi_io_vec[idx].bv_page,
1685 set_bit(WriteErrorSeen, &rdev->flags);
1688 /* We don't worry if we cannot set a bad block -
1689 * it really is bad so there is no loss in not
1692 rdev_set_badblocks(rdev, addr, s, 0);
1694 if (rdev != conf->mirrors[dw].rdev) {
1695 /* need bad block on destination too */
1696 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
1697 addr = r10_bio->devs[1].addr + sect;
1698 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1700 /* just abort the recovery */
1702 "md/raid10:%s: recovery aborted"
1703 " due to read error\n",
1706 conf->mirrors[dw].recovery_disabled
1707 = mddev->recovery_disabled;
1708 set_bit(MD_RECOVERY_INTR,
1721 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1723 struct r10conf *conf = mddev->private;
1727 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1728 fix_recovery_read_error(r10_bio);
1729 end_sync_request(r10_bio);
1734 * share the pages with the first bio
1735 * and submit the write request
1737 wbio = r10_bio->devs[1].bio;
1738 d = r10_bio->devs[1].devnum;
1740 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1741 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1742 generic_make_request(wbio);
1747 * Used by fix_read_error() to decay the per rdev read_errors.
1748 * We halve the read error count for every hour that has elapsed
1749 * since the last recorded read error.
1752 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
1754 struct timespec cur_time_mon;
1755 unsigned long hours_since_last;
1756 unsigned int read_errors = atomic_read(&rdev->read_errors);
1758 ktime_get_ts(&cur_time_mon);
1760 if (rdev->last_read_error.tv_sec == 0 &&
1761 rdev->last_read_error.tv_nsec == 0) {
1762 /* first time we've seen a read error */
1763 rdev->last_read_error = cur_time_mon;
1767 hours_since_last = (cur_time_mon.tv_sec -
1768 rdev->last_read_error.tv_sec) / 3600;
1770 rdev->last_read_error = cur_time_mon;
1773 * if hours_since_last is > the number of bits in read_errors
1774 * just set read errors to 0. We do this to avoid
1775 * overflowing the shift of read_errors by hours_since_last.
1777 if (hours_since_last >= 8 * sizeof(read_errors))
1778 atomic_set(&rdev->read_errors, 0);
1780 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1783 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
1784 int sectors, struct page *page, int rw)
1789 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1790 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1792 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1796 set_bit(WriteErrorSeen, &rdev->flags);
1797 /* need to record an error - either for the block or the device */
1798 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1799 md_error(rdev->mddev, rdev);
1804 * This is a kernel thread which:
1806 * 1. Retries failed read operations on working mirrors.
1807 * 2. Updates the raid superblock when problems encounter.
1808 * 3. Performs writes following reads for array synchronising.
1811 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
1813 int sect = 0; /* Offset from r10_bio->sector */
1814 int sectors = r10_bio->sectors;
1815 struct md_rdev*rdev;
1816 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1817 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1819 /* still own a reference to this rdev, so it cannot
1820 * have been cleared recently.
1822 rdev = conf->mirrors[d].rdev;
1824 if (test_bit(Faulty, &rdev->flags))
1825 /* drive has already been failed, just ignore any
1826 more fix_read_error() attempts */
1829 check_decay_read_errors(mddev, rdev);
1830 atomic_inc(&rdev->read_errors);
1831 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1832 char b[BDEVNAME_SIZE];
1833 bdevname(rdev->bdev, b);
1836 "md/raid10:%s: %s: Raid device exceeded "
1837 "read_error threshold [cur %d:max %d]\n",
1839 atomic_read(&rdev->read_errors), max_read_errors);
1841 "md/raid10:%s: %s: Failing raid device\n",
1843 md_error(mddev, conf->mirrors[d].rdev);
1849 int sl = r10_bio->read_slot;
1853 if (s > (PAGE_SIZE>>9))
1861 d = r10_bio->devs[sl].devnum;
1862 rdev = rcu_dereference(conf->mirrors[d].rdev);
1864 test_bit(In_sync, &rdev->flags) &&
1865 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1866 &first_bad, &bad_sectors) == 0) {
1867 atomic_inc(&rdev->nr_pending);
1869 success = sync_page_io(rdev,
1870 r10_bio->devs[sl].addr +
1873 conf->tmppage, READ, false);
1874 rdev_dec_pending(rdev, mddev);
1880 if (sl == conf->copies)
1882 } while (!success && sl != r10_bio->read_slot);
1886 /* Cannot read from anywhere, just mark the block
1887 * as bad on the first device to discourage future
1890 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1891 rdev = conf->mirrors[dn].rdev;
1893 if (!rdev_set_badblocks(
1895 r10_bio->devs[r10_bio->read_slot].addr
1898 md_error(mddev, rdev);
1903 /* write it back and re-read */
1905 while (sl != r10_bio->read_slot) {
1906 char b[BDEVNAME_SIZE];
1911 d = r10_bio->devs[sl].devnum;
1912 rdev = rcu_dereference(conf->mirrors[d].rdev);
1914 !test_bit(In_sync, &rdev->flags))
1917 atomic_inc(&rdev->nr_pending);
1919 if (r10_sync_page_io(rdev,
1920 r10_bio->devs[sl].addr +
1922 s<<9, conf->tmppage, WRITE)
1924 /* Well, this device is dead */
1926 "md/raid10:%s: read correction "
1928 " (%d sectors at %llu on %s)\n",
1930 (unsigned long long)(
1931 sect + rdev->data_offset),
1932 bdevname(rdev->bdev, b));
1933 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1936 bdevname(rdev->bdev, b));
1938 rdev_dec_pending(rdev, mddev);
1942 while (sl != r10_bio->read_slot) {
1943 char b[BDEVNAME_SIZE];
1948 d = r10_bio->devs[sl].devnum;
1949 rdev = rcu_dereference(conf->mirrors[d].rdev);
1951 !test_bit(In_sync, &rdev->flags))
1954 atomic_inc(&rdev->nr_pending);
1956 switch (r10_sync_page_io(rdev,
1957 r10_bio->devs[sl].addr +
1959 s<<9, conf->tmppage,
1962 /* Well, this device is dead */
1964 "md/raid10:%s: unable to read back "
1966 " (%d sectors at %llu on %s)\n",
1968 (unsigned long long)(
1969 sect + rdev->data_offset),
1970 bdevname(rdev->bdev, b));
1971 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1974 bdevname(rdev->bdev, b));
1978 "md/raid10:%s: read error corrected"
1979 " (%d sectors at %llu on %s)\n",
1981 (unsigned long long)(
1982 sect + rdev->data_offset),
1983 bdevname(rdev->bdev, b));
1984 atomic_add(s, &rdev->corrected_errors);
1987 rdev_dec_pending(rdev, mddev);
1997 static void bi_complete(struct bio *bio, int error)
1999 complete((struct completion *)bio->bi_private);
2002 static int submit_bio_wait(int rw, struct bio *bio)
2004 struct completion event;
2007 init_completion(&event);
2008 bio->bi_private = &event;
2009 bio->bi_end_io = bi_complete;
2010 submit_bio(rw, bio);
2011 wait_for_completion(&event);
2013 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2016 static int narrow_write_error(struct r10bio *r10_bio, int i)
2018 struct bio *bio = r10_bio->master_bio;
2019 struct mddev *mddev = r10_bio->mddev;
2020 struct r10conf *conf = mddev->private;
2021 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2022 /* bio has the data to be written to slot 'i' where
2023 * we just recently had a write error.
2024 * We repeatedly clone the bio and trim down to one block,
2025 * then try the write. Where the write fails we record
2027 * It is conceivable that the bio doesn't exactly align with
2028 * blocks. We must handle this.
2030 * We currently own a reference to the rdev.
2036 int sect_to_write = r10_bio->sectors;
2039 if (rdev->badblocks.shift < 0)
2042 block_sectors = 1 << rdev->badblocks.shift;
2043 sector = r10_bio->sector;
2044 sectors = ((r10_bio->sector + block_sectors)
2045 & ~(sector_t)(block_sectors - 1))
2048 while (sect_to_write) {
2050 if (sectors > sect_to_write)
2051 sectors = sect_to_write;
2052 /* Write at 'sector' for 'sectors' */
2053 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2054 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2055 wbio->bi_sector = (r10_bio->devs[i].addr+
2057 (sector - r10_bio->sector));
2058 wbio->bi_bdev = rdev->bdev;
2059 if (submit_bio_wait(WRITE, wbio) == 0)
2061 ok = rdev_set_badblocks(rdev, sector,
2066 sect_to_write -= sectors;
2068 sectors = block_sectors;
2073 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2075 int slot = r10_bio->read_slot;
2076 int mirror = r10_bio->devs[slot].devnum;
2078 struct r10conf *conf = mddev->private;
2079 struct md_rdev *rdev;
2080 char b[BDEVNAME_SIZE];
2081 unsigned long do_sync;
2084 /* we got a read error. Maybe the drive is bad. Maybe just
2085 * the block and we can fix it.
2086 * We freeze all other IO, and try reading the block from
2087 * other devices. When we find one, we re-write
2088 * and check it that fixes the read error.
2089 * This is all done synchronously while the array is
2092 if (mddev->ro == 0) {
2094 fix_read_error(conf, mddev, r10_bio);
2095 unfreeze_array(conf);
2097 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
2099 bio = r10_bio->devs[slot].bio;
2100 bdevname(bio->bi_bdev, b);
2101 r10_bio->devs[slot].bio =
2102 mddev->ro ? IO_BLOCKED : NULL;
2104 mirror = read_balance(conf, r10_bio, &max_sectors);
2106 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2107 " read error for block %llu\n",
2109 (unsigned long long)r10_bio->sector);
2110 raid_end_bio_io(r10_bio);
2115 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
2118 slot = r10_bio->read_slot;
2119 rdev = conf->mirrors[mirror].rdev;
2122 "md/raid10:%s: %s: redirecting"
2123 "sector %llu to another mirror\n",
2125 bdevname(rdev->bdev, b),
2126 (unsigned long long)r10_bio->sector);
2127 bio = bio_clone_mddev(r10_bio->master_bio,
2130 r10_bio->sector - bio->bi_sector,
2132 r10_bio->devs[slot].bio = bio;
2133 bio->bi_sector = r10_bio->devs[slot].addr
2134 + rdev->data_offset;
2135 bio->bi_bdev = rdev->bdev;
2136 bio->bi_rw = READ | do_sync;
2137 bio->bi_private = r10_bio;
2138 bio->bi_end_io = raid10_end_read_request;
2139 if (max_sectors < r10_bio->sectors) {
2140 /* Drat - have to split this up more */
2141 struct bio *mbio = r10_bio->master_bio;
2142 int sectors_handled =
2143 r10_bio->sector + max_sectors
2145 r10_bio->sectors = max_sectors;
2146 spin_lock_irq(&conf->device_lock);
2147 if (mbio->bi_phys_segments == 0)
2148 mbio->bi_phys_segments = 2;
2150 mbio->bi_phys_segments++;
2151 spin_unlock_irq(&conf->device_lock);
2152 generic_make_request(bio);
2155 r10_bio = mempool_alloc(conf->r10bio_pool,
2157 r10_bio->master_bio = mbio;
2158 r10_bio->sectors = (mbio->bi_size >> 9)
2161 set_bit(R10BIO_ReadError,
2163 r10_bio->mddev = mddev;
2164 r10_bio->sector = mbio->bi_sector
2169 generic_make_request(bio);
2172 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2174 /* Some sort of write request has finished and it
2175 * succeeded in writing where we thought there was a
2176 * bad block. So forget the bad block.
2177 * Or possibly if failed and we need to record
2181 struct md_rdev *rdev;
2183 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2184 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2185 for (m = 0; m < conf->copies; m++) {
2186 int dev = r10_bio->devs[m].devnum;
2187 rdev = conf->mirrors[dev].rdev;
2188 if (r10_bio->devs[m].bio == NULL)
2190 if (test_bit(BIO_UPTODATE,
2191 &r10_bio->devs[m].bio->bi_flags)) {
2192 rdev_clear_badblocks(
2194 r10_bio->devs[m].addr,
2197 if (!rdev_set_badblocks(
2199 r10_bio->devs[m].addr,
2200 r10_bio->sectors, 0))
2201 md_error(conf->mddev, rdev);
2206 for (m = 0; m < conf->copies; m++) {
2207 int dev = r10_bio->devs[m].devnum;
2208 struct bio *bio = r10_bio->devs[m].bio;
2209 rdev = conf->mirrors[dev].rdev;
2210 if (bio == IO_MADE_GOOD) {
2211 rdev_clear_badblocks(
2213 r10_bio->devs[m].addr,
2215 rdev_dec_pending(rdev, conf->mddev);
2216 } else if (bio != NULL &&
2217 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2218 if (!narrow_write_error(r10_bio, m)) {
2219 md_error(conf->mddev, rdev);
2220 set_bit(R10BIO_Degraded,
2223 rdev_dec_pending(rdev, conf->mddev);
2226 if (test_bit(R10BIO_WriteError,
2228 close_write(r10_bio);
2229 raid_end_bio_io(r10_bio);
2233 static void raid10d(struct mddev *mddev)
2235 struct r10bio *r10_bio;
2236 unsigned long flags;
2237 struct r10conf *conf = mddev->private;
2238 struct list_head *head = &conf->retry_list;
2239 struct blk_plug plug;
2241 md_check_recovery(mddev);
2243 blk_start_plug(&plug);
2246 flush_pending_writes(conf);
2248 spin_lock_irqsave(&conf->device_lock, flags);
2249 if (list_empty(head)) {
2250 spin_unlock_irqrestore(&conf->device_lock, flags);
2253 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2254 list_del(head->prev);
2256 spin_unlock_irqrestore(&conf->device_lock, flags);
2258 mddev = r10_bio->mddev;
2259 conf = mddev->private;
2260 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2261 test_bit(R10BIO_WriteError, &r10_bio->state))
2262 handle_write_completed(conf, r10_bio);
2263 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2264 sync_request_write(mddev, r10_bio);
2265 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2266 recovery_request_write(mddev, r10_bio);
2267 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2268 handle_read_error(mddev, r10_bio);
2270 /* just a partial read to be scheduled from a
2273 int slot = r10_bio->read_slot;
2274 generic_make_request(r10_bio->devs[slot].bio);
2278 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2279 md_check_recovery(mddev);
2281 blk_finish_plug(&plug);
2285 static int init_resync(struct r10conf *conf)
2289 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2290 BUG_ON(conf->r10buf_pool);
2291 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2292 if (!conf->r10buf_pool)
2294 conf->next_resync = 0;
2299 * perform a "sync" on one "block"
2301 * We need to make sure that no normal I/O request - particularly write
2302 * requests - conflict with active sync requests.
2304 * This is achieved by tracking pending requests and a 'barrier' concept
2305 * that can be installed to exclude normal IO requests.
2307 * Resync and recovery are handled very differently.
2308 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2310 * For resync, we iterate over virtual addresses, read all copies,
2311 * and update if there are differences. If only one copy is live,
2313 * For recovery, we iterate over physical addresses, read a good
2314 * value for each non-in_sync drive, and over-write.
2316 * So, for recovery we may have several outstanding complex requests for a
2317 * given address, one for each out-of-sync device. We model this by allocating
2318 * a number of r10_bio structures, one for each out-of-sync device.
2319 * As we setup these structures, we collect all bio's together into a list
2320 * which we then process collectively to add pages, and then process again
2321 * to pass to generic_make_request.
2323 * The r10_bio structures are linked using a borrowed master_bio pointer.
2324 * This link is counted in ->remaining. When the r10_bio that points to NULL
2325 * has its remaining count decremented to 0, the whole complex operation
2330 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
2331 int *skipped, int go_faster)
2333 struct r10conf *conf = mddev->private;
2334 struct r10bio *r10_bio;
2335 struct bio *biolist = NULL, *bio;
2336 sector_t max_sector, nr_sectors;
2339 sector_t sync_blocks;
2340 sector_t sectors_skipped = 0;
2341 int chunks_skipped = 0;
2343 if (!conf->r10buf_pool)
2344 if (init_resync(conf))
2348 max_sector = mddev->dev_sectors;
2349 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2350 max_sector = mddev->resync_max_sectors;
2351 if (sector_nr >= max_sector) {
2352 /* If we aborted, we need to abort the
2353 * sync on the 'current' bitmap chucks (there can
2354 * be several when recovering multiple devices).
2355 * as we may have started syncing it but not finished.
2356 * We can find the current address in
2357 * mddev->curr_resync, but for recovery,
2358 * we need to convert that to several
2359 * virtual addresses.
2361 if (mddev->curr_resync < max_sector) { /* aborted */
2362 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2363 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2365 else for (i=0; i<conf->raid_disks; i++) {
2367 raid10_find_virt(conf, mddev->curr_resync, i);
2368 bitmap_end_sync(mddev->bitmap, sect,
2371 } else /* completed sync */
2374 bitmap_close_sync(mddev->bitmap);
2377 return sectors_skipped;
2379 if (chunks_skipped >= conf->raid_disks) {
2380 /* if there has been nothing to do on any drive,
2381 * then there is nothing to do at all..
2384 return (max_sector - sector_nr) + sectors_skipped;
2387 if (max_sector > mddev->resync_max)
2388 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2390 /* make sure whole request will fit in a chunk - if chunks
2393 if (conf->near_copies < conf->raid_disks &&
2394 max_sector > (sector_nr | conf->chunk_mask))
2395 max_sector = (sector_nr | conf->chunk_mask) + 1;
2397 * If there is non-resync activity waiting for us then
2398 * put in a delay to throttle resync.
2400 if (!go_faster && conf->nr_waiting)
2401 msleep_interruptible(1000);
2403 /* Again, very different code for resync and recovery.
2404 * Both must result in an r10bio with a list of bios that
2405 * have bi_end_io, bi_sector, bi_bdev set,
2406 * and bi_private set to the r10bio.
2407 * For recovery, we may actually create several r10bios
2408 * with 2 bios in each, that correspond to the bios in the main one.
2409 * In this case, the subordinate r10bios link back through a
2410 * borrowed master_bio pointer, and the counter in the master
2411 * includes a ref from each subordinate.
2413 /* First, we decide what to do and set ->bi_end_io
2414 * To end_sync_read if we want to read, and
2415 * end_sync_write if we will want to write.
2418 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2419 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2420 /* recovery... the complicated one */
2424 for (i=0 ; i<conf->raid_disks; i++) {
2431 if (conf->mirrors[i].rdev == NULL ||
2432 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2436 /* want to reconstruct this device */
2438 sect = raid10_find_virt(conf, sector_nr, i);
2439 /* Unless we are doing a full sync, we only need
2440 * to recover the block if it is set in the bitmap
2442 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2444 if (sync_blocks < max_sync)
2445 max_sync = sync_blocks;
2448 /* yep, skip the sync_blocks here, but don't assume
2449 * that there will never be anything to do here
2451 chunks_skipped = -1;
2455 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2456 raise_barrier(conf, rb2 != NULL);
2457 atomic_set(&r10_bio->remaining, 0);
2459 r10_bio->master_bio = (struct bio*)rb2;
2461 atomic_inc(&rb2->remaining);
2462 r10_bio->mddev = mddev;
2463 set_bit(R10BIO_IsRecover, &r10_bio->state);
2464 r10_bio->sector = sect;
2466 raid10_find_phys(conf, r10_bio);
2468 /* Need to check if the array will still be
2471 for (j=0; j<conf->raid_disks; j++)
2472 if (conf->mirrors[j].rdev == NULL ||
2473 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2478 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2479 &sync_blocks, still_degraded);
2482 for (j=0; j<conf->copies;j++) {
2484 int d = r10_bio->devs[j].devnum;
2485 sector_t from_addr, to_addr;
2486 struct md_rdev *rdev;
2487 sector_t sector, first_bad;
2489 if (!conf->mirrors[d].rdev ||
2490 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2492 /* This is where we read from */
2494 rdev = conf->mirrors[d].rdev;
2495 sector = r10_bio->devs[j].addr;
2497 if (is_badblock(rdev, sector, max_sync,
2498 &first_bad, &bad_sectors)) {
2499 if (first_bad > sector)
2500 max_sync = first_bad - sector;
2502 bad_sectors -= (sector
2504 if (max_sync > bad_sectors)
2505 max_sync = bad_sectors;
2509 bio = r10_bio->devs[0].bio;
2510 bio->bi_next = biolist;
2512 bio->bi_private = r10_bio;
2513 bio->bi_end_io = end_sync_read;
2515 from_addr = r10_bio->devs[j].addr;
2516 bio->bi_sector = from_addr +
2517 conf->mirrors[d].rdev->data_offset;
2518 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2519 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2520 atomic_inc(&r10_bio->remaining);
2521 /* and we write to 'i' */
2523 for (k=0; k<conf->copies; k++)
2524 if (r10_bio->devs[k].devnum == i)
2526 BUG_ON(k == conf->copies);
2527 bio = r10_bio->devs[1].bio;
2528 bio->bi_next = biolist;
2530 bio->bi_private = r10_bio;
2531 bio->bi_end_io = end_sync_write;
2533 to_addr = r10_bio->devs[k].addr;
2534 bio->bi_sector = to_addr +
2535 conf->mirrors[i].rdev->data_offset;
2536 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2538 r10_bio->devs[0].devnum = d;
2539 r10_bio->devs[0].addr = from_addr;
2540 r10_bio->devs[1].devnum = i;
2541 r10_bio->devs[1].addr = to_addr;
2545 if (j == conf->copies) {
2546 /* Cannot recover, so abort the recovery or
2547 * record a bad block */
2550 atomic_dec(&rb2->remaining);
2553 /* problem is that there are bad blocks
2554 * on other device(s)
2557 for (k = 0; k < conf->copies; k++)
2558 if (r10_bio->devs[k].devnum == i)
2560 if (!rdev_set_badblocks(
2561 conf->mirrors[i].rdev,
2562 r10_bio->devs[k].addr,
2567 if (!test_and_set_bit(MD_RECOVERY_INTR,
2569 printk(KERN_INFO "md/raid10:%s: insufficient "
2570 "working devices for recovery.\n",
2572 conf->mirrors[i].recovery_disabled
2573 = mddev->recovery_disabled;
2578 if (biolist == NULL) {
2580 struct r10bio *rb2 = r10_bio;
2581 r10_bio = (struct r10bio*) rb2->master_bio;
2582 rb2->master_bio = NULL;
2588 /* resync. Schedule a read for every block at this virt offset */
2591 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2593 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2594 &sync_blocks, mddev->degraded) &&
2595 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2596 &mddev->recovery)) {
2597 /* We can skip this block */
2599 return sync_blocks + sectors_skipped;
2601 if (sync_blocks < max_sync)
2602 max_sync = sync_blocks;
2603 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2605 r10_bio->mddev = mddev;
2606 atomic_set(&r10_bio->remaining, 0);
2607 raise_barrier(conf, 0);
2608 conf->next_resync = sector_nr;
2610 r10_bio->master_bio = NULL;
2611 r10_bio->sector = sector_nr;
2612 set_bit(R10BIO_IsSync, &r10_bio->state);
2613 raid10_find_phys(conf, r10_bio);
2614 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2616 for (i=0; i<conf->copies; i++) {
2617 int d = r10_bio->devs[i].devnum;
2618 sector_t first_bad, sector;
2621 bio = r10_bio->devs[i].bio;
2622 bio->bi_end_io = NULL;
2623 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2624 if (conf->mirrors[d].rdev == NULL ||
2625 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2627 sector = r10_bio->devs[i].addr;
2628 if (is_badblock(conf->mirrors[d].rdev,
2630 &first_bad, &bad_sectors)) {
2631 if (first_bad > sector)
2632 max_sync = first_bad - sector;
2634 bad_sectors -= (sector - first_bad);
2635 if (max_sync > bad_sectors)
2636 max_sync = max_sync;
2640 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2641 atomic_inc(&r10_bio->remaining);
2642 bio->bi_next = biolist;
2644 bio->bi_private = r10_bio;
2645 bio->bi_end_io = end_sync_read;
2647 bio->bi_sector = sector +
2648 conf->mirrors[d].rdev->data_offset;
2649 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2654 for (i=0; i<conf->copies; i++) {
2655 int d = r10_bio->devs[i].devnum;
2656 if (r10_bio->devs[i].bio->bi_end_io)
2657 rdev_dec_pending(conf->mirrors[d].rdev,
2666 for (bio = biolist; bio ; bio=bio->bi_next) {
2668 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2670 bio->bi_flags |= 1 << BIO_UPTODATE;
2673 bio->bi_phys_segments = 0;
2678 if (sector_nr + max_sync < max_sector)
2679 max_sector = sector_nr + max_sync;
2682 int len = PAGE_SIZE;
2683 if (sector_nr + (len>>9) > max_sector)
2684 len = (max_sector - sector_nr) << 9;
2687 for (bio= biolist ; bio ; bio=bio->bi_next) {
2689 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2690 if (bio_add_page(bio, page, len, 0))
2694 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2695 for (bio2 = biolist;
2696 bio2 && bio2 != bio;
2697 bio2 = bio2->bi_next) {
2698 /* remove last page from this bio */
2700 bio2->bi_size -= len;
2701 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2705 nr_sectors += len>>9;
2706 sector_nr += len>>9;
2707 } while (biolist->bi_vcnt < RESYNC_PAGES);
2709 r10_bio->sectors = nr_sectors;
2713 biolist = biolist->bi_next;
2715 bio->bi_next = NULL;
2716 r10_bio = bio->bi_private;
2717 r10_bio->sectors = nr_sectors;
2719 if (bio->bi_end_io == end_sync_read) {
2720 md_sync_acct(bio->bi_bdev, nr_sectors);
2721 generic_make_request(bio);
2725 if (sectors_skipped)
2726 /* pretend they weren't skipped, it makes
2727 * no important difference in this case
2729 md_done_sync(mddev, sectors_skipped, 1);
2731 return sectors_skipped + nr_sectors;
2733 /* There is nowhere to write, so all non-sync
2734 * drives must be failed or in resync, all drives
2735 * have a bad block, so try the next chunk...
2737 if (sector_nr + max_sync < max_sector)
2738 max_sector = sector_nr + max_sync;
2740 sectors_skipped += (max_sector - sector_nr);
2742 sector_nr = max_sector;
2747 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2750 struct r10conf *conf = mddev->private;
2753 raid_disks = conf->raid_disks;
2755 sectors = conf->dev_sectors;
2757 size = sectors >> conf->chunk_shift;
2758 sector_div(size, conf->far_copies);
2759 size = size * raid_disks;
2760 sector_div(size, conf->near_copies);
2762 return size << conf->chunk_shift;
2766 static struct r10conf *setup_conf(struct mddev *mddev)
2768 struct r10conf *conf = NULL;
2770 sector_t stride, size;
2773 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2774 !is_power_of_2(mddev->new_chunk_sectors)) {
2775 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2776 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2777 mdname(mddev), PAGE_SIZE);
2781 nc = mddev->new_layout & 255;
2782 fc = (mddev->new_layout >> 8) & 255;
2783 fo = mddev->new_layout & (1<<16);
2785 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2786 (mddev->new_layout >> 17)) {
2787 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2788 mdname(mddev), mddev->new_layout);
2793 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
2797 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2802 conf->tmppage = alloc_page(GFP_KERNEL);
2807 conf->raid_disks = mddev->raid_disks;
2808 conf->near_copies = nc;
2809 conf->far_copies = fc;
2810 conf->copies = nc*fc;
2811 conf->far_offset = fo;
2812 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2813 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2815 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2816 r10bio_pool_free, conf);
2817 if (!conf->r10bio_pool)
2820 size = mddev->dev_sectors >> conf->chunk_shift;
2821 sector_div(size, fc);
2822 size = size * conf->raid_disks;
2823 sector_div(size, nc);
2824 /* 'size' is now the number of chunks in the array */
2825 /* calculate "used chunks per device" in 'stride' */
2826 stride = size * conf->copies;
2828 /* We need to round up when dividing by raid_disks to
2829 * get the stride size.
2831 stride += conf->raid_disks - 1;
2832 sector_div(stride, conf->raid_disks);
2834 conf->dev_sectors = stride << conf->chunk_shift;
2839 sector_div(stride, fc);
2840 conf->stride = stride << conf->chunk_shift;
2843 spin_lock_init(&conf->device_lock);
2844 INIT_LIST_HEAD(&conf->retry_list);
2846 spin_lock_init(&conf->resync_lock);
2847 init_waitqueue_head(&conf->wait_barrier);
2849 conf->thread = md_register_thread(raid10d, mddev, NULL);
2853 conf->mddev = mddev;
2857 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
2860 if (conf->r10bio_pool)
2861 mempool_destroy(conf->r10bio_pool);
2862 kfree(conf->mirrors);
2863 safe_put_page(conf->tmppage);
2866 return ERR_PTR(err);
2869 static int run(struct mddev *mddev)
2871 struct r10conf *conf;
2872 int i, disk_idx, chunk_size;
2873 struct mirror_info *disk;
2874 struct md_rdev *rdev;
2878 * copy the already verified devices into our private RAID10
2879 * bookkeeping area. [whatever we allocate in run(),
2880 * should be freed in stop()]
2883 if (mddev->private == NULL) {
2884 conf = setup_conf(mddev);
2886 return PTR_ERR(conf);
2887 mddev->private = conf;
2889 conf = mddev->private;
2893 mddev->thread = conf->thread;
2894 conf->thread = NULL;
2896 chunk_size = mddev->chunk_sectors << 9;
2897 blk_queue_io_min(mddev->queue, chunk_size);
2898 if (conf->raid_disks % conf->near_copies)
2899 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2901 blk_queue_io_opt(mddev->queue, chunk_size *
2902 (conf->raid_disks / conf->near_copies));
2904 list_for_each_entry(rdev, &mddev->disks, same_set) {
2906 disk_idx = rdev->raid_disk;
2907 if (disk_idx >= conf->raid_disks
2910 disk = conf->mirrors + disk_idx;
2913 disk_stack_limits(mddev->gendisk, rdev->bdev,
2914 rdev->data_offset << 9);
2915 /* as we don't honour merge_bvec_fn, we must never risk
2916 * violating it, so limit max_segments to 1 lying
2917 * within a single page.
2919 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2920 blk_queue_max_segments(mddev->queue, 1);
2921 blk_queue_segment_boundary(mddev->queue,
2922 PAGE_CACHE_SIZE - 1);
2925 disk->head_position = 0;
2927 /* need to check that every block has at least one working mirror */
2928 if (!enough(conf, -1)) {
2929 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
2934 mddev->degraded = 0;
2935 for (i = 0; i < conf->raid_disks; i++) {
2937 disk = conf->mirrors + i;
2940 !test_bit(In_sync, &disk->rdev->flags)) {
2941 disk->head_position = 0;
2946 disk->recovery_disabled = mddev->recovery_disabled - 1;
2949 if (mddev->recovery_cp != MaxSector)
2950 printk(KERN_NOTICE "md/raid10:%s: not clean"
2951 " -- starting background reconstruction\n",
2954 "md/raid10:%s: active with %d out of %d devices\n",
2955 mdname(mddev), conf->raid_disks - mddev->degraded,
2958 * Ok, everything is just fine now
2960 mddev->dev_sectors = conf->dev_sectors;
2961 size = raid10_size(mddev, 0, 0);
2962 md_set_array_sectors(mddev, size);
2963 mddev->resync_max_sectors = size;
2965 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2966 mddev->queue->backing_dev_info.congested_data = mddev;
2968 /* Calculate max read-ahead size.
2969 * We need to readahead at least twice a whole stripe....
2973 int stripe = conf->raid_disks *
2974 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2975 stripe /= conf->near_copies;
2976 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2977 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2980 if (conf->near_copies < conf->raid_disks)
2981 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2983 if (md_integrity_register(mddev))
2989 md_unregister_thread(&mddev->thread);
2990 if (conf->r10bio_pool)
2991 mempool_destroy(conf->r10bio_pool);
2992 safe_put_page(conf->tmppage);
2993 kfree(conf->mirrors);
2995 mddev->private = NULL;
3000 static int stop(struct mddev *mddev)
3002 struct r10conf *conf = mddev->private;
3004 raise_barrier(conf, 0);
3005 lower_barrier(conf);
3007 md_unregister_thread(&mddev->thread);
3008 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3009 if (conf->r10bio_pool)
3010 mempool_destroy(conf->r10bio_pool);
3011 kfree(conf->mirrors);
3013 mddev->private = NULL;
3017 static void raid10_quiesce(struct mddev *mddev, int state)
3019 struct r10conf *conf = mddev->private;
3023 raise_barrier(conf, 0);
3026 lower_barrier(conf);
3031 static void *raid10_takeover_raid0(struct mddev *mddev)
3033 struct md_rdev *rdev;
3034 struct r10conf *conf;
3036 if (mddev->degraded > 0) {
3037 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3039 return ERR_PTR(-EINVAL);
3042 /* Set new parameters */
3043 mddev->new_level = 10;
3044 /* new layout: far_copies = 1, near_copies = 2 */
3045 mddev->new_layout = (1<<8) + 2;
3046 mddev->new_chunk_sectors = mddev->chunk_sectors;
3047 mddev->delta_disks = mddev->raid_disks;
3048 mddev->raid_disks *= 2;
3049 /* make sure it will be not marked as dirty */
3050 mddev->recovery_cp = MaxSector;
3052 conf = setup_conf(mddev);
3053 if (!IS_ERR(conf)) {
3054 list_for_each_entry(rdev, &mddev->disks, same_set)
3055 if (rdev->raid_disk >= 0)
3056 rdev->new_raid_disk = rdev->raid_disk * 2;
3063 static void *raid10_takeover(struct mddev *mddev)
3065 struct r0conf *raid0_conf;
3067 /* raid10 can take over:
3068 * raid0 - providing it has only two drives
3070 if (mddev->level == 0) {
3071 /* for raid0 takeover only one zone is supported */
3072 raid0_conf = mddev->private;
3073 if (raid0_conf->nr_strip_zones > 1) {
3074 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3075 " with more than one zone.\n",
3077 return ERR_PTR(-EINVAL);
3079 return raid10_takeover_raid0(mddev);
3081 return ERR_PTR(-EINVAL);
3084 static struct md_personality raid10_personality =
3088 .owner = THIS_MODULE,
3089 .make_request = make_request,
3093 .error_handler = error,
3094 .hot_add_disk = raid10_add_disk,
3095 .hot_remove_disk= raid10_remove_disk,
3096 .spare_active = raid10_spare_active,
3097 .sync_request = sync_request,
3098 .quiesce = raid10_quiesce,
3099 .size = raid10_size,
3100 .takeover = raid10_takeover,
3103 static int __init raid_init(void)
3105 return register_md_personality(&raid10_personality);
3108 static void raid_exit(void)
3110 unregister_md_personality(&raid10_personality);
3113 module_init(raid_init);
3114 module_exit(raid_exit);
3115 MODULE_LICENSE("GPL");
3116 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
3117 MODULE_ALIAS("md-personality-9"); /* RAID10 */
3118 MODULE_ALIAS("md-raid10");
3119 MODULE_ALIAS("md-level-10");
3121 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);