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>
27 #include <linux/kthread.h>
28 #include <trace/events/block.h>
32 #include "md-bitmap.h"
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
39 * near_copies (stored in low byte of layout)
40 * far_copies (stored in second byte of layout)
41 * far_offset (stored in bit 16 of layout )
42 * use_far_sets (stored in bit 17 of layout )
43 * use_far_sets_bugfixed (stored in bit 18 of layout )
45 * The data to be stored is divided into chunks using chunksize. Each device
46 * is divided into far_copies sections. In each section, chunks are laid out
47 * in a style similar to raid0, but near_copies copies of each chunk is stored
48 * (each on a different drive). The starting device for each section is offset
49 * near_copies from the starting device of the previous section. Thus there
50 * are (near_copies * far_copies) of each chunk, and each is on a different
51 * drive. near_copies and far_copies must be at least one, and their product
52 * is at most raid_disks.
54 * If far_offset is true, then the far_copies are handled a bit differently.
55 * The copies are still in different stripes, but instead of being very far
56 * apart on disk, there are adjacent stripes.
58 * The far and offset algorithms are handled slightly differently if
59 * 'use_far_sets' is true. In this case, the array's devices are grouped into
60 * sets that are (near_copies * far_copies) in size. The far copied stripes
61 * are still shifted by 'near_copies' devices, but this shifting stays confined
62 * to the set rather than the entire array. This is done to improve the number
63 * of device combinations that can fail without causing the array to fail.
64 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
69 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
70 * [A B] [C D] [A B] [C D E]
71 * |...| |...| |...| | ... |
72 * [B A] [D C] [B A] [E C D]
76 * Number of guaranteed r10bios in case of extreme VM load:
78 #define NR_RAID10_BIOS 256
80 /* when we get a read error on a read-only array, we redirect to another
81 * device without failing the first device, or trying to over-write to
82 * correct the read error. To keep track of bad blocks on a per-bio
83 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
85 #define IO_BLOCKED ((struct bio *)1)
86 /* When we successfully write to a known bad-block, we need to remove the
87 * bad-block marking which must be done from process context. So we record
88 * the success by setting devs[n].bio to IO_MADE_GOOD
90 #define IO_MADE_GOOD ((struct bio *)2)
92 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
94 /* When there are this many requests queued to be written by
95 * the raid10 thread, we become 'congested' to provide back-pressure
98 static int max_queued_requests = 1024;
100 static void allow_barrier(struct r10conf *conf);
101 static void lower_barrier(struct r10conf *conf);
102 static int _enough(struct r10conf *conf, int previous, int ignore);
103 static int enough(struct r10conf *conf, int ignore);
104 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
106 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
107 static void end_reshape_write(struct bio *bio);
108 static void end_reshape(struct r10conf *conf);
110 #define raid10_log(md, fmt, args...) \
111 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
113 #include "raid1-10.c"
116 * for resync bio, r10bio pointer can be retrieved from the per-bio
117 * 'struct resync_pages'.
119 static inline struct r10bio *get_resync_r10bio(struct bio *bio)
121 return get_resync_pages(bio)->raid_bio;
124 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
126 struct r10conf *conf = data;
127 int size = offsetof(struct r10bio, devs[conf->copies]);
129 /* allocate a r10bio with room for raid_disks entries in the
131 return kzalloc(size, gfp_flags);
134 static void r10bio_pool_free(void *r10_bio, void *data)
139 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
140 /* amount of memory to reserve for resync requests */
141 #define RESYNC_WINDOW (1024*1024)
142 /* maximum number of concurrent requests, memory permitting */
143 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
144 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
145 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
148 * When performing a resync, we need to read and compare, so
149 * we need as many pages are there are copies.
150 * When performing a recovery, we need 2 bios, one for read,
151 * one for write (we recover only one drive per r10buf)
154 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
156 struct r10conf *conf = data;
157 struct r10bio *r10_bio;
160 int nalloc, nalloc_rp;
161 struct resync_pages *rps;
163 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
167 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
168 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
169 nalloc = conf->copies; /* resync */
171 nalloc = 2; /* recovery */
173 /* allocate once for all bios */
174 if (!conf->have_replacement)
177 nalloc_rp = nalloc * 2;
178 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
180 goto out_free_r10bio;
185 for (j = nalloc ; j-- ; ) {
186 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
189 r10_bio->devs[j].bio = bio;
190 if (!conf->have_replacement)
192 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
195 r10_bio->devs[j].repl_bio = bio;
198 * Allocate RESYNC_PAGES data pages and attach them
201 for (j = 0; j < nalloc; j++) {
202 struct bio *rbio = r10_bio->devs[j].repl_bio;
203 struct resync_pages *rp, *rp_repl;
207 rp_repl = &rps[nalloc + j];
209 bio = r10_bio->devs[j].bio;
211 if (!j || test_bit(MD_RECOVERY_SYNC,
212 &conf->mddev->recovery)) {
213 if (resync_alloc_pages(rp, gfp_flags))
216 memcpy(rp, &rps[0], sizeof(*rp));
217 resync_get_all_pages(rp);
220 rp->raid_bio = r10_bio;
221 bio->bi_private = rp;
223 memcpy(rp_repl, rp, sizeof(*rp));
224 rbio->bi_private = rp_repl;
232 resync_free_pages(&rps[j]);
236 for ( ; j < nalloc; j++) {
237 if (r10_bio->devs[j].bio)
238 bio_put(r10_bio->devs[j].bio);
239 if (r10_bio->devs[j].repl_bio)
240 bio_put(r10_bio->devs[j].repl_bio);
244 r10bio_pool_free(r10_bio, conf);
248 static void r10buf_pool_free(void *__r10_bio, void *data)
250 struct r10conf *conf = data;
251 struct r10bio *r10bio = __r10_bio;
253 struct resync_pages *rp = NULL;
255 for (j = conf->copies; j--; ) {
256 struct bio *bio = r10bio->devs[j].bio;
259 rp = get_resync_pages(bio);
260 resync_free_pages(rp);
264 bio = r10bio->devs[j].repl_bio;
269 /* resync pages array stored in the 1st bio's .bi_private */
272 r10bio_pool_free(r10bio, conf);
275 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
279 for (i = 0; i < conf->copies; i++) {
280 struct bio **bio = & r10_bio->devs[i].bio;
281 if (!BIO_SPECIAL(*bio))
284 bio = &r10_bio->devs[i].repl_bio;
285 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
291 static void free_r10bio(struct r10bio *r10_bio)
293 struct r10conf *conf = r10_bio->mddev->private;
295 put_all_bios(conf, r10_bio);
296 mempool_free(r10_bio, &conf->r10bio_pool);
299 static void put_buf(struct r10bio *r10_bio)
301 struct r10conf *conf = r10_bio->mddev->private;
303 mempool_free(r10_bio, &conf->r10buf_pool);
308 static void reschedule_retry(struct r10bio *r10_bio)
311 struct mddev *mddev = r10_bio->mddev;
312 struct r10conf *conf = mddev->private;
314 spin_lock_irqsave(&conf->device_lock, flags);
315 list_add(&r10_bio->retry_list, &conf->retry_list);
317 spin_unlock_irqrestore(&conf->device_lock, flags);
319 /* wake up frozen array... */
320 wake_up(&conf->wait_barrier);
322 md_wakeup_thread(mddev->thread);
326 * raid_end_bio_io() is called when we have finished servicing a mirrored
327 * operation and are ready to return a success/failure code to the buffer
330 static void raid_end_bio_io(struct r10bio *r10_bio)
332 struct bio *bio = r10_bio->master_bio;
333 struct r10conf *conf = r10_bio->mddev->private;
335 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
336 bio->bi_status = BLK_STS_IOERR;
340 * Wake up any possible resync thread that waits for the device
345 free_r10bio(r10_bio);
349 * Update disk head position estimator based on IRQ completion info.
351 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
353 struct r10conf *conf = r10_bio->mddev->private;
355 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
356 r10_bio->devs[slot].addr + (r10_bio->sectors);
360 * Find the disk number which triggered given bio
362 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
363 struct bio *bio, int *slotp, int *replp)
368 for (slot = 0; slot < conf->copies; slot++) {
369 if (r10_bio->devs[slot].bio == bio)
371 if (r10_bio->devs[slot].repl_bio == bio) {
377 BUG_ON(slot == conf->copies);
378 update_head_pos(slot, r10_bio);
384 return r10_bio->devs[slot].devnum;
387 static void raid10_end_read_request(struct bio *bio)
389 int uptodate = !bio->bi_status;
390 struct r10bio *r10_bio = bio->bi_private;
392 struct md_rdev *rdev;
393 struct r10conf *conf = r10_bio->mddev->private;
395 slot = r10_bio->read_slot;
396 rdev = r10_bio->devs[slot].rdev;
398 * this branch is our 'one mirror IO has finished' event handler:
400 update_head_pos(slot, r10_bio);
404 * Set R10BIO_Uptodate in our master bio, so that
405 * we will return a good error code to the higher
406 * levels even if IO on some other mirrored buffer fails.
408 * The 'master' represents the composite IO operation to
409 * user-side. So if something waits for IO, then it will
410 * wait for the 'master' bio.
412 set_bit(R10BIO_Uptodate, &r10_bio->state);
414 /* If all other devices that store this block have
415 * failed, we want to return the error upwards rather
416 * than fail the last device. Here we redefine
417 * "uptodate" to mean "Don't want to retry"
419 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
424 raid_end_bio_io(r10_bio);
425 rdev_dec_pending(rdev, conf->mddev);
428 * oops, read error - keep the refcount on the rdev
430 char b[BDEVNAME_SIZE];
431 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
433 bdevname(rdev->bdev, b),
434 (unsigned long long)r10_bio->sector);
435 set_bit(R10BIO_ReadError, &r10_bio->state);
436 reschedule_retry(r10_bio);
440 static void close_write(struct r10bio *r10_bio)
442 /* clear the bitmap if all writes complete successfully */
443 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
445 !test_bit(R10BIO_Degraded, &r10_bio->state),
447 md_write_end(r10_bio->mddev);
450 static void one_write_done(struct r10bio *r10_bio)
452 if (atomic_dec_and_test(&r10_bio->remaining)) {
453 if (test_bit(R10BIO_WriteError, &r10_bio->state))
454 reschedule_retry(r10_bio);
456 close_write(r10_bio);
457 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
458 reschedule_retry(r10_bio);
460 raid_end_bio_io(r10_bio);
465 static void raid10_end_write_request(struct bio *bio)
467 struct r10bio *r10_bio = bio->bi_private;
470 struct r10conf *conf = r10_bio->mddev->private;
472 struct md_rdev *rdev = NULL;
473 struct bio *to_put = NULL;
476 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
478 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
481 rdev = conf->mirrors[dev].replacement;
485 rdev = conf->mirrors[dev].rdev;
488 * this branch is our 'one mirror IO has finished' event handler:
490 if (bio->bi_status && !discard_error) {
492 /* Never record new bad blocks to replacement,
495 md_error(rdev->mddev, rdev);
497 set_bit(WriteErrorSeen, &rdev->flags);
498 if (!test_and_set_bit(WantReplacement, &rdev->flags))
499 set_bit(MD_RECOVERY_NEEDED,
500 &rdev->mddev->recovery);
503 if (test_bit(FailFast, &rdev->flags) &&
504 (bio->bi_opf & MD_FAILFAST)) {
505 md_error(rdev->mddev, rdev);
506 if (!test_bit(Faulty, &rdev->flags))
507 /* This is the only remaining device,
508 * We need to retry the write without
511 set_bit(R10BIO_WriteError, &r10_bio->state);
513 r10_bio->devs[slot].bio = NULL;
518 set_bit(R10BIO_WriteError, &r10_bio->state);
522 * Set R10BIO_Uptodate in our master bio, so that
523 * we will return a good error code for to the higher
524 * levels even if IO on some other mirrored buffer fails.
526 * The 'master' represents the composite IO operation to
527 * user-side. So if something waits for IO, then it will
528 * wait for the 'master' bio.
534 * Do not set R10BIO_Uptodate if the current device is
535 * rebuilding or Faulty. This is because we cannot use
536 * such device for properly reading the data back (we could
537 * potentially use it, if the current write would have felt
538 * before rdev->recovery_offset, but for simplicity we don't
541 if (test_bit(In_sync, &rdev->flags) &&
542 !test_bit(Faulty, &rdev->flags))
543 set_bit(R10BIO_Uptodate, &r10_bio->state);
545 /* Maybe we can clear some bad blocks. */
546 if (is_badblock(rdev,
547 r10_bio->devs[slot].addr,
549 &first_bad, &bad_sectors) && !discard_error) {
552 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
554 r10_bio->devs[slot].bio = IO_MADE_GOOD;
556 set_bit(R10BIO_MadeGood, &r10_bio->state);
562 * Let's see if all mirrored write operations have finished
565 one_write_done(r10_bio);
567 rdev_dec_pending(rdev, conf->mddev);
573 * RAID10 layout manager
574 * As well as the chunksize and raid_disks count, there are two
575 * parameters: near_copies and far_copies.
576 * near_copies * far_copies must be <= raid_disks.
577 * Normally one of these will be 1.
578 * If both are 1, we get raid0.
579 * If near_copies == raid_disks, we get raid1.
581 * Chunks are laid out in raid0 style with near_copies copies of the
582 * first chunk, followed by near_copies copies of the next chunk and
584 * If far_copies > 1, then after 1/far_copies of the array has been assigned
585 * as described above, we start again with a device offset of near_copies.
586 * So we effectively have another copy of the whole array further down all
587 * the drives, but with blocks on different drives.
588 * With this layout, and block is never stored twice on the one device.
590 * raid10_find_phys finds the sector offset of a given virtual sector
591 * on each device that it is on.
593 * raid10_find_virt does the reverse mapping, from a device and a
594 * sector offset to a virtual address
597 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
605 int last_far_set_start, last_far_set_size;
607 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
608 last_far_set_start *= geo->far_set_size;
610 last_far_set_size = geo->far_set_size;
611 last_far_set_size += (geo->raid_disks % geo->far_set_size);
613 /* now calculate first sector/dev */
614 chunk = r10bio->sector >> geo->chunk_shift;
615 sector = r10bio->sector & geo->chunk_mask;
617 chunk *= geo->near_copies;
619 dev = sector_div(stripe, geo->raid_disks);
621 stripe *= geo->far_copies;
623 sector += stripe << geo->chunk_shift;
625 /* and calculate all the others */
626 for (n = 0; n < geo->near_copies; n++) {
630 r10bio->devs[slot].devnum = d;
631 r10bio->devs[slot].addr = s;
634 for (f = 1; f < geo->far_copies; f++) {
635 set = d / geo->far_set_size;
636 d += geo->near_copies;
638 if ((geo->raid_disks % geo->far_set_size) &&
639 (d > last_far_set_start)) {
640 d -= last_far_set_start;
641 d %= last_far_set_size;
642 d += last_far_set_start;
644 d %= geo->far_set_size;
645 d += geo->far_set_size * set;
648 r10bio->devs[slot].devnum = d;
649 r10bio->devs[slot].addr = s;
653 if (dev >= geo->raid_disks) {
655 sector += (geo->chunk_mask + 1);
660 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
662 struct geom *geo = &conf->geo;
664 if (conf->reshape_progress != MaxSector &&
665 ((r10bio->sector >= conf->reshape_progress) !=
666 conf->mddev->reshape_backwards)) {
667 set_bit(R10BIO_Previous, &r10bio->state);
670 clear_bit(R10BIO_Previous, &r10bio->state);
672 __raid10_find_phys(geo, r10bio);
675 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
677 sector_t offset, chunk, vchunk;
678 /* Never use conf->prev as this is only called during resync
679 * or recovery, so reshape isn't happening
681 struct geom *geo = &conf->geo;
682 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
683 int far_set_size = geo->far_set_size;
684 int last_far_set_start;
686 if (geo->raid_disks % geo->far_set_size) {
687 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
688 last_far_set_start *= geo->far_set_size;
690 if (dev >= last_far_set_start) {
691 far_set_size = geo->far_set_size;
692 far_set_size += (geo->raid_disks % geo->far_set_size);
693 far_set_start = last_far_set_start;
697 offset = sector & geo->chunk_mask;
698 if (geo->far_offset) {
700 chunk = sector >> geo->chunk_shift;
701 fc = sector_div(chunk, geo->far_copies);
702 dev -= fc * geo->near_copies;
703 if (dev < far_set_start)
706 while (sector >= geo->stride) {
707 sector -= geo->stride;
708 if (dev < (geo->near_copies + far_set_start))
709 dev += far_set_size - geo->near_copies;
711 dev -= geo->near_copies;
713 chunk = sector >> geo->chunk_shift;
715 vchunk = chunk * geo->raid_disks + dev;
716 sector_div(vchunk, geo->near_copies);
717 return (vchunk << geo->chunk_shift) + offset;
721 * This routine returns the disk from which the requested read should
722 * be done. There is a per-array 'next expected sequential IO' sector
723 * number - if this matches on the next IO then we use the last disk.
724 * There is also a per-disk 'last know head position' sector that is
725 * maintained from IRQ contexts, both the normal and the resync IO
726 * completion handlers update this position correctly. If there is no
727 * perfect sequential match then we pick the disk whose head is closest.
729 * If there are 2 mirrors in the same 2 devices, performance degrades
730 * because position is mirror, not device based.
732 * The rdev for the device selected will have nr_pending incremented.
736 * FIXME: possibly should rethink readbalancing and do it differently
737 * depending on near_copies / far_copies geometry.
739 static struct md_rdev *read_balance(struct r10conf *conf,
740 struct r10bio *r10_bio,
743 const sector_t this_sector = r10_bio->sector;
745 int sectors = r10_bio->sectors;
746 int best_good_sectors;
747 sector_t new_distance, best_dist;
748 struct md_rdev *best_rdev, *rdev = NULL;
751 struct geom *geo = &conf->geo;
753 raid10_find_phys(conf, r10_bio);
757 best_dist = MaxSector;
758 best_good_sectors = 0;
760 clear_bit(R10BIO_FailFast, &r10_bio->state);
762 * Check if we can balance. We can balance on the whole
763 * device if no resync is going on (recovery is ok), or below
764 * the resync window. We take the first readable disk when
765 * above the resync window.
767 if ((conf->mddev->recovery_cp < MaxSector
768 && (this_sector + sectors >= conf->next_resync)) ||
769 (mddev_is_clustered(conf->mddev) &&
770 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
771 this_sector + sectors)))
774 for (slot = 0; slot < conf->copies ; slot++) {
779 if (r10_bio->devs[slot].bio == IO_BLOCKED)
781 disk = r10_bio->devs[slot].devnum;
782 rdev = rcu_dereference(conf->mirrors[disk].replacement);
783 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
784 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
785 rdev = rcu_dereference(conf->mirrors[disk].rdev);
787 test_bit(Faulty, &rdev->flags))
789 if (!test_bit(In_sync, &rdev->flags) &&
790 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
793 dev_sector = r10_bio->devs[slot].addr;
794 if (is_badblock(rdev, dev_sector, sectors,
795 &first_bad, &bad_sectors)) {
796 if (best_dist < MaxSector)
797 /* Already have a better slot */
799 if (first_bad <= dev_sector) {
800 /* Cannot read here. If this is the
801 * 'primary' device, then we must not read
802 * beyond 'bad_sectors' from another device.
804 bad_sectors -= (dev_sector - first_bad);
805 if (!do_balance && sectors > bad_sectors)
806 sectors = bad_sectors;
807 if (best_good_sectors > sectors)
808 best_good_sectors = sectors;
810 sector_t good_sectors =
811 first_bad - dev_sector;
812 if (good_sectors > best_good_sectors) {
813 best_good_sectors = good_sectors;
818 /* Must read from here */
823 best_good_sectors = sectors;
829 /* At least 2 disks to choose from so failfast is OK */
830 set_bit(R10BIO_FailFast, &r10_bio->state);
831 /* This optimisation is debatable, and completely destroys
832 * sequential read speed for 'far copies' arrays. So only
833 * keep it for 'near' arrays, and review those later.
835 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
838 /* for far > 1 always use the lowest address */
839 else if (geo->far_copies > 1)
840 new_distance = r10_bio->devs[slot].addr;
842 new_distance = abs(r10_bio->devs[slot].addr -
843 conf->mirrors[disk].head_position);
844 if (new_distance < best_dist) {
845 best_dist = new_distance;
850 if (slot >= conf->copies) {
856 atomic_inc(&rdev->nr_pending);
857 r10_bio->read_slot = slot;
861 *max_sectors = best_good_sectors;
866 static int raid10_congested(struct mddev *mddev, int bits)
868 struct r10conf *conf = mddev->private;
871 if ((bits & (1 << WB_async_congested)) &&
872 conf->pending_count >= max_queued_requests)
877 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
880 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
881 if (rdev && !test_bit(Faulty, &rdev->flags)) {
882 struct request_queue *q = bdev_get_queue(rdev->bdev);
884 ret |= bdi_congested(q->backing_dev_info, bits);
891 static void flush_pending_writes(struct r10conf *conf)
893 /* Any writes that have been queued but are awaiting
894 * bitmap updates get flushed here.
896 spin_lock_irq(&conf->device_lock);
898 if (conf->pending_bio_list.head) {
899 struct blk_plug plug;
902 bio = bio_list_get(&conf->pending_bio_list);
903 conf->pending_count = 0;
904 spin_unlock_irq(&conf->device_lock);
907 * As this is called in a wait_event() loop (see freeze_array),
908 * current->state might be TASK_UNINTERRUPTIBLE which will
909 * cause a warning when we prepare to wait again. As it is
910 * rare that this path is taken, it is perfectly safe to force
911 * us to go around the wait_event() loop again, so the warning
912 * is a false-positive. Silence the warning by resetting
915 __set_current_state(TASK_RUNNING);
917 blk_start_plug(&plug);
918 /* flush any pending bitmap writes to disk
919 * before proceeding w/ I/O */
920 md_bitmap_unplug(conf->mddev->bitmap);
921 wake_up(&conf->wait_barrier);
923 while (bio) { /* submit pending writes */
924 struct bio *next = bio->bi_next;
925 struct md_rdev *rdev = (void*)bio->bi_disk;
927 bio_set_dev(bio, rdev->bdev);
928 if (test_bit(Faulty, &rdev->flags)) {
930 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
931 !blk_queue_discard(bio->bi_disk->queue)))
935 generic_make_request(bio);
938 blk_finish_plug(&plug);
940 spin_unlock_irq(&conf->device_lock);
944 * Sometimes we need to suspend IO while we do something else,
945 * either some resync/recovery, or reconfigure the array.
946 * To do this we raise a 'barrier'.
947 * The 'barrier' is a counter that can be raised multiple times
948 * to count how many activities are happening which preclude
950 * We can only raise the barrier if there is no pending IO.
951 * i.e. if nr_pending == 0.
952 * We choose only to raise the barrier if no-one is waiting for the
953 * barrier to go down. This means that as soon as an IO request
954 * is ready, no other operations which require a barrier will start
955 * until the IO request has had a chance.
957 * So: regular IO calls 'wait_barrier'. When that returns there
958 * is no backgroup IO happening, It must arrange to call
959 * allow_barrier when it has finished its IO.
960 * backgroup IO calls must call raise_barrier. Once that returns
961 * there is no normal IO happeing. It must arrange to call
962 * lower_barrier when the particular background IO completes.
965 static void raise_barrier(struct r10conf *conf, int force)
967 BUG_ON(force && !conf->barrier);
968 spin_lock_irq(&conf->resync_lock);
970 /* Wait until no block IO is waiting (unless 'force') */
971 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
974 /* block any new IO from starting */
977 /* Now wait for all pending IO to complete */
978 wait_event_lock_irq(conf->wait_barrier,
979 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
982 spin_unlock_irq(&conf->resync_lock);
985 static void lower_barrier(struct r10conf *conf)
988 spin_lock_irqsave(&conf->resync_lock, flags);
990 spin_unlock_irqrestore(&conf->resync_lock, flags);
991 wake_up(&conf->wait_barrier);
994 static void wait_barrier(struct r10conf *conf)
996 spin_lock_irq(&conf->resync_lock);
999 /* Wait for the barrier to drop.
1000 * However if there are already pending
1001 * requests (preventing the barrier from
1002 * rising completely), and the
1003 * pre-process bio queue isn't empty,
1004 * then don't wait, as we need to empty
1005 * that queue to get the nr_pending
1008 raid10_log(conf->mddev, "wait barrier");
1009 wait_event_lock_irq(conf->wait_barrier,
1011 (atomic_read(&conf->nr_pending) &&
1012 current->bio_list &&
1013 (!bio_list_empty(¤t->bio_list[0]) ||
1014 !bio_list_empty(¤t->bio_list[1]))),
1017 if (!conf->nr_waiting)
1018 wake_up(&conf->wait_barrier);
1020 atomic_inc(&conf->nr_pending);
1021 spin_unlock_irq(&conf->resync_lock);
1024 static void allow_barrier(struct r10conf *conf)
1026 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1027 (conf->array_freeze_pending))
1028 wake_up(&conf->wait_barrier);
1031 static void freeze_array(struct r10conf *conf, int extra)
1033 /* stop syncio and normal IO and wait for everything to
1035 * We increment barrier and nr_waiting, and then
1036 * wait until nr_pending match nr_queued+extra
1037 * This is called in the context of one normal IO request
1038 * that has failed. Thus any sync request that might be pending
1039 * will be blocked by nr_pending, and we need to wait for
1040 * pending IO requests to complete or be queued for re-try.
1041 * Thus the number queued (nr_queued) plus this request (extra)
1042 * must match the number of pending IOs (nr_pending) before
1045 spin_lock_irq(&conf->resync_lock);
1046 conf->array_freeze_pending++;
1049 wait_event_lock_irq_cmd(conf->wait_barrier,
1050 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
1052 flush_pending_writes(conf));
1054 conf->array_freeze_pending--;
1055 spin_unlock_irq(&conf->resync_lock);
1058 static void unfreeze_array(struct r10conf *conf)
1060 /* reverse the effect of the freeze */
1061 spin_lock_irq(&conf->resync_lock);
1064 wake_up(&conf->wait_barrier);
1065 spin_unlock_irq(&conf->resync_lock);
1068 static sector_t choose_data_offset(struct r10bio *r10_bio,
1069 struct md_rdev *rdev)
1071 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1072 test_bit(R10BIO_Previous, &r10_bio->state))
1073 return rdev->data_offset;
1075 return rdev->new_data_offset;
1078 struct raid10_plug_cb {
1079 struct blk_plug_cb cb;
1080 struct bio_list pending;
1084 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1086 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1088 struct mddev *mddev = plug->cb.data;
1089 struct r10conf *conf = mddev->private;
1092 if (from_schedule || current->bio_list) {
1093 spin_lock_irq(&conf->device_lock);
1094 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1095 conf->pending_count += plug->pending_cnt;
1096 spin_unlock_irq(&conf->device_lock);
1097 wake_up(&conf->wait_barrier);
1098 md_wakeup_thread(mddev->thread);
1103 /* we aren't scheduling, so we can do the write-out directly. */
1104 bio = bio_list_get(&plug->pending);
1105 md_bitmap_unplug(mddev->bitmap);
1106 wake_up(&conf->wait_barrier);
1108 while (bio) { /* submit pending writes */
1109 struct bio *next = bio->bi_next;
1110 struct md_rdev *rdev = (void*)bio->bi_disk;
1111 bio->bi_next = NULL;
1112 bio_set_dev(bio, rdev->bdev);
1113 if (test_bit(Faulty, &rdev->flags)) {
1115 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1116 !blk_queue_discard(bio->bi_disk->queue)))
1117 /* Just ignore it */
1120 generic_make_request(bio);
1126 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1127 struct r10bio *r10_bio)
1129 struct r10conf *conf = mddev->private;
1130 struct bio *read_bio;
1131 const int op = bio_op(bio);
1132 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1135 struct md_rdev *rdev;
1136 char b[BDEVNAME_SIZE];
1137 int slot = r10_bio->read_slot;
1138 struct md_rdev *err_rdev = NULL;
1139 gfp_t gfp = GFP_NOIO;
1141 if (slot >= 0 && r10_bio->devs[slot].rdev) {
1143 * This is an error retry, but we cannot
1144 * safely dereference the rdev in the r10_bio,
1145 * we must use the one in conf.
1146 * If it has already been disconnected (unlikely)
1147 * we lose the device name in error messages.
1151 * As we are blocking raid10, it is a little safer to
1154 gfp = GFP_NOIO | __GFP_HIGH;
1157 disk = r10_bio->devs[slot].devnum;
1158 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1160 bdevname(err_rdev->bdev, b);
1163 /* This never gets dereferenced */
1164 err_rdev = r10_bio->devs[slot].rdev;
1169 * Register the new request and wait if the reconstruction
1170 * thread has put up a bar for new requests.
1171 * Continue immediately if no resync is active currently.
1175 sectors = r10_bio->sectors;
1176 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1177 bio->bi_iter.bi_sector < conf->reshape_progress &&
1178 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1180 * IO spans the reshape position. Need to wait for reshape to
1183 raid10_log(conf->mddev, "wait reshape");
1184 allow_barrier(conf);
1185 wait_event(conf->wait_barrier,
1186 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1187 conf->reshape_progress >= bio->bi_iter.bi_sector +
1192 rdev = read_balance(conf, r10_bio, &max_sectors);
1195 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1197 (unsigned long long)r10_bio->sector);
1199 raid_end_bio_io(r10_bio);
1203 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1205 bdevname(rdev->bdev, b),
1206 (unsigned long long)r10_bio->sector);
1207 if (max_sectors < bio_sectors(bio)) {
1208 struct bio *split = bio_split(bio, max_sectors,
1209 gfp, &conf->bio_split);
1210 bio_chain(split, bio);
1211 allow_barrier(conf);
1212 generic_make_request(bio);
1215 r10_bio->master_bio = bio;
1216 r10_bio->sectors = max_sectors;
1218 slot = r10_bio->read_slot;
1220 read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
1222 r10_bio->devs[slot].bio = read_bio;
1223 r10_bio->devs[slot].rdev = rdev;
1225 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1226 choose_data_offset(r10_bio, rdev);
1227 bio_set_dev(read_bio, rdev->bdev);
1228 read_bio->bi_end_io = raid10_end_read_request;
1229 bio_set_op_attrs(read_bio, op, do_sync);
1230 if (test_bit(FailFast, &rdev->flags) &&
1231 test_bit(R10BIO_FailFast, &r10_bio->state))
1232 read_bio->bi_opf |= MD_FAILFAST;
1233 read_bio->bi_private = r10_bio;
1236 trace_block_bio_remap(read_bio->bi_disk->queue,
1237 read_bio, disk_devt(mddev->gendisk),
1239 generic_make_request(read_bio);
1243 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1244 struct bio *bio, bool replacement,
1247 const int op = bio_op(bio);
1248 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1249 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1250 unsigned long flags;
1251 struct blk_plug_cb *cb;
1252 struct raid10_plug_cb *plug = NULL;
1253 struct r10conf *conf = mddev->private;
1254 struct md_rdev *rdev;
1255 int devnum = r10_bio->devs[n_copy].devnum;
1259 rdev = conf->mirrors[devnum].replacement;
1261 /* Replacement just got moved to main 'rdev' */
1263 rdev = conf->mirrors[devnum].rdev;
1266 rdev = conf->mirrors[devnum].rdev;
1268 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1270 r10_bio->devs[n_copy].repl_bio = mbio;
1272 r10_bio->devs[n_copy].bio = mbio;
1274 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1275 choose_data_offset(r10_bio, rdev));
1276 bio_set_dev(mbio, rdev->bdev);
1277 mbio->bi_end_io = raid10_end_write_request;
1278 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1279 if (!replacement && test_bit(FailFast,
1280 &conf->mirrors[devnum].rdev->flags)
1281 && enough(conf, devnum))
1282 mbio->bi_opf |= MD_FAILFAST;
1283 mbio->bi_private = r10_bio;
1285 if (conf->mddev->gendisk)
1286 trace_block_bio_remap(mbio->bi_disk->queue,
1287 mbio, disk_devt(conf->mddev->gendisk),
1289 /* flush_pending_writes() needs access to the rdev so...*/
1290 mbio->bi_disk = (void *)rdev;
1292 atomic_inc(&r10_bio->remaining);
1294 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1296 plug = container_of(cb, struct raid10_plug_cb, cb);
1300 bio_list_add(&plug->pending, mbio);
1301 plug->pending_cnt++;
1303 spin_lock_irqsave(&conf->device_lock, flags);
1304 bio_list_add(&conf->pending_bio_list, mbio);
1305 conf->pending_count++;
1306 spin_unlock_irqrestore(&conf->device_lock, flags);
1307 md_wakeup_thread(mddev->thread);
1311 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1312 struct r10bio *r10_bio)
1314 struct r10conf *conf = mddev->private;
1316 struct md_rdev *blocked_rdev;
1320 if ((mddev_is_clustered(mddev) &&
1321 md_cluster_ops->area_resyncing(mddev, WRITE,
1322 bio->bi_iter.bi_sector,
1323 bio_end_sector(bio)))) {
1326 prepare_to_wait(&conf->wait_barrier,
1328 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1329 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1333 finish_wait(&conf->wait_barrier, &w);
1337 * Register the new request and wait if the reconstruction
1338 * thread has put up a bar for new requests.
1339 * Continue immediately if no resync is active currently.
1343 sectors = r10_bio->sectors;
1344 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1345 bio->bi_iter.bi_sector < conf->reshape_progress &&
1346 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1348 * IO spans the reshape position. Need to wait for reshape to
1351 raid10_log(conf->mddev, "wait reshape");
1352 allow_barrier(conf);
1353 wait_event(conf->wait_barrier,
1354 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1355 conf->reshape_progress >= bio->bi_iter.bi_sector +
1360 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1361 (mddev->reshape_backwards
1362 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1363 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1364 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1365 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1366 /* Need to update reshape_position in metadata */
1367 mddev->reshape_position = conf->reshape_progress;
1368 set_mask_bits(&mddev->sb_flags, 0,
1369 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1370 md_wakeup_thread(mddev->thread);
1371 raid10_log(conf->mddev, "wait reshape metadata");
1372 wait_event(mddev->sb_wait,
1373 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1375 conf->reshape_safe = mddev->reshape_position;
1378 if (conf->pending_count >= max_queued_requests) {
1379 md_wakeup_thread(mddev->thread);
1380 raid10_log(mddev, "wait queued");
1381 wait_event(conf->wait_barrier,
1382 conf->pending_count < max_queued_requests);
1384 /* first select target devices under rcu_lock and
1385 * inc refcount on their rdev. Record them by setting
1387 * If there are known/acknowledged bad blocks on any device
1388 * on which we have seen a write error, we want to avoid
1389 * writing to those blocks. This potentially requires several
1390 * writes to write around the bad blocks. Each set of writes
1391 * gets its own r10_bio with a set of bios attached.
1394 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1395 raid10_find_phys(conf, r10_bio);
1397 blocked_rdev = NULL;
1399 max_sectors = r10_bio->sectors;
1401 for (i = 0; i < conf->copies; i++) {
1402 int d = r10_bio->devs[i].devnum;
1403 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1404 struct md_rdev *rrdev = rcu_dereference(
1405 conf->mirrors[d].replacement);
1408 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1409 atomic_inc(&rdev->nr_pending);
1410 blocked_rdev = rdev;
1413 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1414 atomic_inc(&rrdev->nr_pending);
1415 blocked_rdev = rrdev;
1418 if (rdev && (test_bit(Faulty, &rdev->flags)))
1420 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1423 r10_bio->devs[i].bio = NULL;
1424 r10_bio->devs[i].repl_bio = NULL;
1426 if (!rdev && !rrdev) {
1427 set_bit(R10BIO_Degraded, &r10_bio->state);
1430 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1432 sector_t dev_sector = r10_bio->devs[i].addr;
1436 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1437 &first_bad, &bad_sectors);
1439 /* Mustn't write here until the bad block
1442 atomic_inc(&rdev->nr_pending);
1443 set_bit(BlockedBadBlocks, &rdev->flags);
1444 blocked_rdev = rdev;
1447 if (is_bad && first_bad <= dev_sector) {
1448 /* Cannot write here at all */
1449 bad_sectors -= (dev_sector - first_bad);
1450 if (bad_sectors < max_sectors)
1451 /* Mustn't write more than bad_sectors
1452 * to other devices yet
1454 max_sectors = bad_sectors;
1455 /* We don't set R10BIO_Degraded as that
1456 * only applies if the disk is missing,
1457 * so it might be re-added, and we want to
1458 * know to recover this chunk.
1459 * In this case the device is here, and the
1460 * fact that this chunk is not in-sync is
1461 * recorded in the bad block log.
1466 int good_sectors = first_bad - dev_sector;
1467 if (good_sectors < max_sectors)
1468 max_sectors = good_sectors;
1472 r10_bio->devs[i].bio = bio;
1473 atomic_inc(&rdev->nr_pending);
1476 r10_bio->devs[i].repl_bio = bio;
1477 atomic_inc(&rrdev->nr_pending);
1482 if (unlikely(blocked_rdev)) {
1483 /* Have to wait for this device to get unblocked, then retry */
1487 for (j = 0; j < i; j++) {
1488 if (r10_bio->devs[j].bio) {
1489 d = r10_bio->devs[j].devnum;
1490 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1492 if (r10_bio->devs[j].repl_bio) {
1493 struct md_rdev *rdev;
1494 d = r10_bio->devs[j].devnum;
1495 rdev = conf->mirrors[d].replacement;
1497 /* Race with remove_disk */
1499 rdev = conf->mirrors[d].rdev;
1501 rdev_dec_pending(rdev, mddev);
1504 allow_barrier(conf);
1505 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1506 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1511 if (max_sectors < r10_bio->sectors)
1512 r10_bio->sectors = max_sectors;
1514 if (r10_bio->sectors < bio_sectors(bio)) {
1515 struct bio *split = bio_split(bio, r10_bio->sectors,
1516 GFP_NOIO, &conf->bio_split);
1517 bio_chain(split, bio);
1518 allow_barrier(conf);
1519 generic_make_request(bio);
1522 r10_bio->master_bio = bio;
1525 atomic_set(&r10_bio->remaining, 1);
1526 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1528 for (i = 0; i < conf->copies; i++) {
1529 if (r10_bio->devs[i].bio)
1530 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1531 if (r10_bio->devs[i].repl_bio)
1532 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1534 one_write_done(r10_bio);
1537 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1539 struct r10conf *conf = mddev->private;
1540 struct r10bio *r10_bio;
1542 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1544 r10_bio->master_bio = bio;
1545 r10_bio->sectors = sectors;
1547 r10_bio->mddev = mddev;
1548 r10_bio->sector = bio->bi_iter.bi_sector;
1550 r10_bio->read_slot = -1;
1551 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
1553 if (bio_data_dir(bio) == READ)
1554 raid10_read_request(mddev, bio, r10_bio);
1556 raid10_write_request(mddev, bio, r10_bio);
1559 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1561 struct r10conf *conf = mddev->private;
1562 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1563 int chunk_sects = chunk_mask + 1;
1564 int sectors = bio_sectors(bio);
1566 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1567 && md_flush_request(mddev, bio))
1570 if (!md_write_start(mddev, bio))
1574 * If this request crosses a chunk boundary, we need to split
1577 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1578 sectors > chunk_sects
1579 && (conf->geo.near_copies < conf->geo.raid_disks
1580 || conf->prev.near_copies <
1581 conf->prev.raid_disks)))
1582 sectors = chunk_sects -
1583 (bio->bi_iter.bi_sector &
1585 __make_request(mddev, bio, sectors);
1587 /* In case raid10d snuck in to freeze_array */
1588 wake_up(&conf->wait_barrier);
1592 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1594 struct r10conf *conf = mddev->private;
1597 if (conf->geo.near_copies < conf->geo.raid_disks)
1598 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1599 if (conf->geo.near_copies > 1)
1600 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1601 if (conf->geo.far_copies > 1) {
1602 if (conf->geo.far_offset)
1603 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1605 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1606 if (conf->geo.far_set_size != conf->geo.raid_disks)
1607 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1609 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1610 conf->geo.raid_disks - mddev->degraded);
1612 for (i = 0; i < conf->geo.raid_disks; i++) {
1613 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1614 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1617 seq_printf(seq, "]");
1620 /* check if there are enough drives for
1621 * every block to appear on atleast one.
1622 * Don't consider the device numbered 'ignore'
1623 * as we might be about to remove it.
1625 static int _enough(struct r10conf *conf, int previous, int ignore)
1631 disks = conf->prev.raid_disks;
1632 ncopies = conf->prev.near_copies;
1634 disks = conf->geo.raid_disks;
1635 ncopies = conf->geo.near_copies;
1640 int n = conf->copies;
1644 struct md_rdev *rdev;
1645 if (this != ignore &&
1646 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1647 test_bit(In_sync, &rdev->flags))
1649 this = (this+1) % disks;
1653 first = (first + ncopies) % disks;
1654 } while (first != 0);
1661 static int enough(struct r10conf *conf, int ignore)
1663 /* when calling 'enough', both 'prev' and 'geo' must
1665 * This is ensured if ->reconfig_mutex or ->device_lock
1668 return _enough(conf, 0, ignore) &&
1669 _enough(conf, 1, ignore);
1672 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1674 char b[BDEVNAME_SIZE];
1675 struct r10conf *conf = mddev->private;
1676 unsigned long flags;
1679 * If it is not operational, then we have already marked it as dead
1680 * else if it is the last working disks, ignore the error, let the
1681 * next level up know.
1682 * else mark the drive as failed
1684 spin_lock_irqsave(&conf->device_lock, flags);
1685 if (test_bit(In_sync, &rdev->flags)
1686 && !enough(conf, rdev->raid_disk)) {
1688 * Don't fail the drive, just return an IO error.
1690 spin_unlock_irqrestore(&conf->device_lock, flags);
1693 if (test_and_clear_bit(In_sync, &rdev->flags))
1696 * If recovery is running, make sure it aborts.
1698 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1699 set_bit(Blocked, &rdev->flags);
1700 set_bit(Faulty, &rdev->flags);
1701 set_mask_bits(&mddev->sb_flags, 0,
1702 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1703 spin_unlock_irqrestore(&conf->device_lock, flags);
1704 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1705 "md/raid10:%s: Operation continuing on %d devices.\n",
1706 mdname(mddev), bdevname(rdev->bdev, b),
1707 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1710 static void print_conf(struct r10conf *conf)
1713 struct md_rdev *rdev;
1715 pr_debug("RAID10 conf printout:\n");
1717 pr_debug("(!conf)\n");
1720 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1721 conf->geo.raid_disks);
1723 /* This is only called with ->reconfix_mutex held, so
1724 * rcu protection of rdev is not needed */
1725 for (i = 0; i < conf->geo.raid_disks; i++) {
1726 char b[BDEVNAME_SIZE];
1727 rdev = conf->mirrors[i].rdev;
1729 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1730 i, !test_bit(In_sync, &rdev->flags),
1731 !test_bit(Faulty, &rdev->flags),
1732 bdevname(rdev->bdev,b));
1736 static void close_sync(struct r10conf *conf)
1739 allow_barrier(conf);
1741 mempool_exit(&conf->r10buf_pool);
1744 static int raid10_spare_active(struct mddev *mddev)
1747 struct r10conf *conf = mddev->private;
1748 struct raid10_info *tmp;
1750 unsigned long flags;
1753 * Find all non-in_sync disks within the RAID10 configuration
1754 * and mark them in_sync
1756 for (i = 0; i < conf->geo.raid_disks; i++) {
1757 tmp = conf->mirrors + i;
1758 if (tmp->replacement
1759 && tmp->replacement->recovery_offset == MaxSector
1760 && !test_bit(Faulty, &tmp->replacement->flags)
1761 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1762 /* Replacement has just become active */
1764 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1767 /* Replaced device not technically faulty,
1768 * but we need to be sure it gets removed
1769 * and never re-added.
1771 set_bit(Faulty, &tmp->rdev->flags);
1772 sysfs_notify_dirent_safe(
1773 tmp->rdev->sysfs_state);
1775 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1776 } else if (tmp->rdev
1777 && tmp->rdev->recovery_offset == MaxSector
1778 && !test_bit(Faulty, &tmp->rdev->flags)
1779 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1781 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1784 spin_lock_irqsave(&conf->device_lock, flags);
1785 mddev->degraded -= count;
1786 spin_unlock_irqrestore(&conf->device_lock, flags);
1792 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1794 struct r10conf *conf = mddev->private;
1798 int last = conf->geo.raid_disks - 1;
1800 if (mddev->recovery_cp < MaxSector)
1801 /* only hot-add to in-sync arrays, as recovery is
1802 * very different from resync
1805 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
1808 if (md_integrity_add_rdev(rdev, mddev))
1811 if (rdev->raid_disk >= 0)
1812 first = last = rdev->raid_disk;
1814 if (rdev->saved_raid_disk >= first &&
1815 rdev->saved_raid_disk < conf->geo.raid_disks &&
1816 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1817 mirror = rdev->saved_raid_disk;
1820 for ( ; mirror <= last ; mirror++) {
1821 struct raid10_info *p = &conf->mirrors[mirror];
1822 if (p->recovery_disabled == mddev->recovery_disabled)
1825 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1826 p->replacement != NULL)
1828 clear_bit(In_sync, &rdev->flags);
1829 set_bit(Replacement, &rdev->flags);
1830 rdev->raid_disk = mirror;
1833 disk_stack_limits(mddev->gendisk, rdev->bdev,
1834 rdev->data_offset << 9);
1836 rcu_assign_pointer(p->replacement, rdev);
1841 disk_stack_limits(mddev->gendisk, rdev->bdev,
1842 rdev->data_offset << 9);
1844 p->head_position = 0;
1845 p->recovery_disabled = mddev->recovery_disabled - 1;
1846 rdev->raid_disk = mirror;
1848 if (rdev->saved_raid_disk != mirror)
1850 rcu_assign_pointer(p->rdev, rdev);
1853 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1854 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
1860 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1862 struct r10conf *conf = mddev->private;
1864 int number = rdev->raid_disk;
1865 struct md_rdev **rdevp;
1866 struct raid10_info *p = conf->mirrors + number;
1869 if (rdev == p->rdev)
1871 else if (rdev == p->replacement)
1872 rdevp = &p->replacement;
1876 if (test_bit(In_sync, &rdev->flags) ||
1877 atomic_read(&rdev->nr_pending)) {
1881 /* Only remove non-faulty devices if recovery
1884 if (!test_bit(Faulty, &rdev->flags) &&
1885 mddev->recovery_disabled != p->recovery_disabled &&
1886 (!p->replacement || p->replacement == rdev) &&
1887 number < conf->geo.raid_disks &&
1893 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1895 if (atomic_read(&rdev->nr_pending)) {
1896 /* lost the race, try later */
1902 if (p->replacement) {
1903 /* We must have just cleared 'rdev' */
1904 p->rdev = p->replacement;
1905 clear_bit(Replacement, &p->replacement->flags);
1906 smp_mb(); /* Make sure other CPUs may see both as identical
1907 * but will never see neither -- if they are careful.
1909 p->replacement = NULL;
1912 clear_bit(WantReplacement, &rdev->flags);
1913 err = md_integrity_register(mddev);
1921 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
1923 struct r10conf *conf = r10_bio->mddev->private;
1925 if (!bio->bi_status)
1926 set_bit(R10BIO_Uptodate, &r10_bio->state);
1928 /* The write handler will notice the lack of
1929 * R10BIO_Uptodate and record any errors etc
1931 atomic_add(r10_bio->sectors,
1932 &conf->mirrors[d].rdev->corrected_errors);
1934 /* for reconstruct, we always reschedule after a read.
1935 * for resync, only after all reads
1937 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1938 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1939 atomic_dec_and_test(&r10_bio->remaining)) {
1940 /* we have read all the blocks,
1941 * do the comparison in process context in raid10d
1943 reschedule_retry(r10_bio);
1947 static void end_sync_read(struct bio *bio)
1949 struct r10bio *r10_bio = get_resync_r10bio(bio);
1950 struct r10conf *conf = r10_bio->mddev->private;
1951 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1953 __end_sync_read(r10_bio, bio, d);
1956 static void end_reshape_read(struct bio *bio)
1958 /* reshape read bio isn't allocated from r10buf_pool */
1959 struct r10bio *r10_bio = bio->bi_private;
1961 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1964 static void end_sync_request(struct r10bio *r10_bio)
1966 struct mddev *mddev = r10_bio->mddev;
1968 while (atomic_dec_and_test(&r10_bio->remaining)) {
1969 if (r10_bio->master_bio == NULL) {
1970 /* the primary of several recovery bios */
1971 sector_t s = r10_bio->sectors;
1972 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1973 test_bit(R10BIO_WriteError, &r10_bio->state))
1974 reschedule_retry(r10_bio);
1977 md_done_sync(mddev, s, 1);
1980 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1981 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1982 test_bit(R10BIO_WriteError, &r10_bio->state))
1983 reschedule_retry(r10_bio);
1991 static void end_sync_write(struct bio *bio)
1993 struct r10bio *r10_bio = get_resync_r10bio(bio);
1994 struct mddev *mddev = r10_bio->mddev;
1995 struct r10conf *conf = mddev->private;
2001 struct md_rdev *rdev = NULL;
2003 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2005 rdev = conf->mirrors[d].replacement;
2007 rdev = conf->mirrors[d].rdev;
2009 if (bio->bi_status) {
2011 md_error(mddev, rdev);
2013 set_bit(WriteErrorSeen, &rdev->flags);
2014 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2015 set_bit(MD_RECOVERY_NEEDED,
2016 &rdev->mddev->recovery);
2017 set_bit(R10BIO_WriteError, &r10_bio->state);
2019 } else if (is_badblock(rdev,
2020 r10_bio->devs[slot].addr,
2022 &first_bad, &bad_sectors))
2023 set_bit(R10BIO_MadeGood, &r10_bio->state);
2025 rdev_dec_pending(rdev, mddev);
2027 end_sync_request(r10_bio);
2031 * Note: sync and recover and handled very differently for raid10
2032 * This code is for resync.
2033 * For resync, we read through virtual addresses and read all blocks.
2034 * If there is any error, we schedule a write. The lowest numbered
2035 * drive is authoritative.
2036 * However requests come for physical address, so we need to map.
2037 * For every physical address there are raid_disks/copies virtual addresses,
2038 * which is always are least one, but is not necessarly an integer.
2039 * This means that a physical address can span multiple chunks, so we may
2040 * have to submit multiple io requests for a single sync request.
2043 * We check if all blocks are in-sync and only write to blocks that
2046 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2048 struct r10conf *conf = mddev->private;
2050 struct bio *tbio, *fbio;
2052 struct page **tpages, **fpages;
2054 atomic_set(&r10_bio->remaining, 1);
2056 /* find the first device with a block */
2057 for (i=0; i<conf->copies; i++)
2058 if (!r10_bio->devs[i].bio->bi_status)
2061 if (i == conf->copies)
2065 fbio = r10_bio->devs[i].bio;
2066 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2067 fbio->bi_iter.bi_idx = 0;
2068 fpages = get_resync_pages(fbio)->pages;
2070 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2071 /* now find blocks with errors */
2072 for (i=0 ; i < conf->copies ; i++) {
2074 struct md_rdev *rdev;
2075 struct resync_pages *rp;
2077 tbio = r10_bio->devs[i].bio;
2079 if (tbio->bi_end_io != end_sync_read)
2084 tpages = get_resync_pages(tbio)->pages;
2085 d = r10_bio->devs[i].devnum;
2086 rdev = conf->mirrors[d].rdev;
2087 if (!r10_bio->devs[i].bio->bi_status) {
2088 /* We know that the bi_io_vec layout is the same for
2089 * both 'first' and 'i', so we just compare them.
2090 * All vec entries are PAGE_SIZE;
2092 int sectors = r10_bio->sectors;
2093 for (j = 0; j < vcnt; j++) {
2094 int len = PAGE_SIZE;
2095 if (sectors < (len / 512))
2096 len = sectors * 512;
2097 if (memcmp(page_address(fpages[j]),
2098 page_address(tpages[j]),
2105 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2106 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2107 /* Don't fix anything. */
2109 } else if (test_bit(FailFast, &rdev->flags)) {
2110 /* Just give up on this device */
2111 md_error(rdev->mddev, rdev);
2114 /* Ok, we need to write this bio, either to correct an
2115 * inconsistency or to correct an unreadable block.
2116 * First we need to fixup bv_offset, bv_len and
2117 * bi_vecs, as the read request might have corrupted these
2119 rp = get_resync_pages(tbio);
2122 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2124 rp->raid_bio = r10_bio;
2125 tbio->bi_private = rp;
2126 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2127 tbio->bi_end_io = end_sync_write;
2128 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
2130 bio_copy_data(tbio, fbio);
2132 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2133 atomic_inc(&r10_bio->remaining);
2134 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2136 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2137 tbio->bi_opf |= MD_FAILFAST;
2138 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2139 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
2140 generic_make_request(tbio);
2143 /* Now write out to any replacement devices
2146 for (i = 0; i < conf->copies; i++) {
2149 tbio = r10_bio->devs[i].repl_bio;
2150 if (!tbio || !tbio->bi_end_io)
2152 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2153 && r10_bio->devs[i].bio != fbio)
2154 bio_copy_data(tbio, fbio);
2155 d = r10_bio->devs[i].devnum;
2156 atomic_inc(&r10_bio->remaining);
2157 md_sync_acct(conf->mirrors[d].replacement->bdev,
2159 generic_make_request(tbio);
2163 if (atomic_dec_and_test(&r10_bio->remaining)) {
2164 md_done_sync(mddev, r10_bio->sectors, 1);
2170 * Now for the recovery code.
2171 * Recovery happens across physical sectors.
2172 * We recover all non-is_sync drives by finding the virtual address of
2173 * each, and then choose a working drive that also has that virt address.
2174 * There is a separate r10_bio for each non-in_sync drive.
2175 * Only the first two slots are in use. The first for reading,
2176 * The second for writing.
2179 static void fix_recovery_read_error(struct r10bio *r10_bio)
2181 /* We got a read error during recovery.
2182 * We repeat the read in smaller page-sized sections.
2183 * If a read succeeds, write it to the new device or record
2184 * a bad block if we cannot.
2185 * If a read fails, record a bad block on both old and
2188 struct mddev *mddev = r10_bio->mddev;
2189 struct r10conf *conf = mddev->private;
2190 struct bio *bio = r10_bio->devs[0].bio;
2192 int sectors = r10_bio->sectors;
2194 int dr = r10_bio->devs[0].devnum;
2195 int dw = r10_bio->devs[1].devnum;
2196 struct page **pages = get_resync_pages(bio)->pages;
2200 struct md_rdev *rdev;
2204 if (s > (PAGE_SIZE>>9))
2207 rdev = conf->mirrors[dr].rdev;
2208 addr = r10_bio->devs[0].addr + sect,
2209 ok = sync_page_io(rdev,
2213 REQ_OP_READ, 0, false);
2215 rdev = conf->mirrors[dw].rdev;
2216 addr = r10_bio->devs[1].addr + sect;
2217 ok = sync_page_io(rdev,
2221 REQ_OP_WRITE, 0, false);
2223 set_bit(WriteErrorSeen, &rdev->flags);
2224 if (!test_and_set_bit(WantReplacement,
2226 set_bit(MD_RECOVERY_NEEDED,
2227 &rdev->mddev->recovery);
2231 /* We don't worry if we cannot set a bad block -
2232 * it really is bad so there is no loss in not
2235 rdev_set_badblocks(rdev, addr, s, 0);
2237 if (rdev != conf->mirrors[dw].rdev) {
2238 /* need bad block on destination too */
2239 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2240 addr = r10_bio->devs[1].addr + sect;
2241 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2243 /* just abort the recovery */
2244 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2247 conf->mirrors[dw].recovery_disabled
2248 = mddev->recovery_disabled;
2249 set_bit(MD_RECOVERY_INTR,
2262 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2264 struct r10conf *conf = mddev->private;
2266 struct bio *wbio, *wbio2;
2268 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2269 fix_recovery_read_error(r10_bio);
2270 end_sync_request(r10_bio);
2275 * share the pages with the first bio
2276 * and submit the write request
2278 d = r10_bio->devs[1].devnum;
2279 wbio = r10_bio->devs[1].bio;
2280 wbio2 = r10_bio->devs[1].repl_bio;
2281 /* Need to test wbio2->bi_end_io before we call
2282 * generic_make_request as if the former is NULL,
2283 * the latter is free to free wbio2.
2285 if (wbio2 && !wbio2->bi_end_io)
2287 if (wbio->bi_end_io) {
2288 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2289 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2290 generic_make_request(wbio);
2293 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2294 md_sync_acct(conf->mirrors[d].replacement->bdev,
2295 bio_sectors(wbio2));
2296 generic_make_request(wbio2);
2301 * Used by fix_read_error() to decay the per rdev read_errors.
2302 * We halve the read error count for every hour that has elapsed
2303 * since the last recorded read error.
2306 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2309 unsigned long hours_since_last;
2310 unsigned int read_errors = atomic_read(&rdev->read_errors);
2312 cur_time_mon = ktime_get_seconds();
2314 if (rdev->last_read_error == 0) {
2315 /* first time we've seen a read error */
2316 rdev->last_read_error = cur_time_mon;
2320 hours_since_last = (long)(cur_time_mon -
2321 rdev->last_read_error) / 3600;
2323 rdev->last_read_error = cur_time_mon;
2326 * if hours_since_last is > the number of bits in read_errors
2327 * just set read errors to 0. We do this to avoid
2328 * overflowing the shift of read_errors by hours_since_last.
2330 if (hours_since_last >= 8 * sizeof(read_errors))
2331 atomic_set(&rdev->read_errors, 0);
2333 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2336 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2337 int sectors, struct page *page, int rw)
2342 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2343 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2345 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
2349 set_bit(WriteErrorSeen, &rdev->flags);
2350 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2351 set_bit(MD_RECOVERY_NEEDED,
2352 &rdev->mddev->recovery);
2354 /* need to record an error - either for the block or the device */
2355 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2356 md_error(rdev->mddev, rdev);
2361 * This is a kernel thread which:
2363 * 1. Retries failed read operations on working mirrors.
2364 * 2. Updates the raid superblock when problems encounter.
2365 * 3. Performs writes following reads for array synchronising.
2368 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2370 int sect = 0; /* Offset from r10_bio->sector */
2371 int sectors = r10_bio->sectors;
2372 struct md_rdev *rdev;
2373 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2374 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2376 /* still own a reference to this rdev, so it cannot
2377 * have been cleared recently.
2379 rdev = conf->mirrors[d].rdev;
2381 if (test_bit(Faulty, &rdev->flags))
2382 /* drive has already been failed, just ignore any
2383 more fix_read_error() attempts */
2386 check_decay_read_errors(mddev, rdev);
2387 atomic_inc(&rdev->read_errors);
2388 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2389 char b[BDEVNAME_SIZE];
2390 bdevname(rdev->bdev, b);
2392 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2394 atomic_read(&rdev->read_errors), max_read_errors);
2395 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2397 md_error(mddev, rdev);
2398 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2404 int sl = r10_bio->read_slot;
2408 if (s > (PAGE_SIZE>>9))
2416 d = r10_bio->devs[sl].devnum;
2417 rdev = rcu_dereference(conf->mirrors[d].rdev);
2419 test_bit(In_sync, &rdev->flags) &&
2420 !test_bit(Faulty, &rdev->flags) &&
2421 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2422 &first_bad, &bad_sectors) == 0) {
2423 atomic_inc(&rdev->nr_pending);
2425 success = sync_page_io(rdev,
2426 r10_bio->devs[sl].addr +
2430 REQ_OP_READ, 0, false);
2431 rdev_dec_pending(rdev, mddev);
2437 if (sl == conf->copies)
2439 } while (!success && sl != r10_bio->read_slot);
2443 /* Cannot read from anywhere, just mark the block
2444 * as bad on the first device to discourage future
2447 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2448 rdev = conf->mirrors[dn].rdev;
2450 if (!rdev_set_badblocks(
2452 r10_bio->devs[r10_bio->read_slot].addr
2455 md_error(mddev, rdev);
2456 r10_bio->devs[r10_bio->read_slot].bio
2463 /* write it back and re-read */
2465 while (sl != r10_bio->read_slot) {
2466 char b[BDEVNAME_SIZE];
2471 d = r10_bio->devs[sl].devnum;
2472 rdev = rcu_dereference(conf->mirrors[d].rdev);
2474 test_bit(Faulty, &rdev->flags) ||
2475 !test_bit(In_sync, &rdev->flags))
2478 atomic_inc(&rdev->nr_pending);
2480 if (r10_sync_page_io(rdev,
2481 r10_bio->devs[sl].addr +
2483 s, conf->tmppage, WRITE)
2485 /* Well, this device is dead */
2486 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2488 (unsigned long long)(
2490 choose_data_offset(r10_bio,
2492 bdevname(rdev->bdev, b));
2493 pr_notice("md/raid10:%s: %s: failing drive\n",
2495 bdevname(rdev->bdev, b));
2497 rdev_dec_pending(rdev, mddev);
2501 while (sl != r10_bio->read_slot) {
2502 char b[BDEVNAME_SIZE];
2507 d = r10_bio->devs[sl].devnum;
2508 rdev = rcu_dereference(conf->mirrors[d].rdev);
2510 test_bit(Faulty, &rdev->flags) ||
2511 !test_bit(In_sync, &rdev->flags))
2514 atomic_inc(&rdev->nr_pending);
2516 switch (r10_sync_page_io(rdev,
2517 r10_bio->devs[sl].addr +
2522 /* Well, this device is dead */
2523 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2525 (unsigned long long)(
2527 choose_data_offset(r10_bio, rdev)),
2528 bdevname(rdev->bdev, b));
2529 pr_notice("md/raid10:%s: %s: failing drive\n",
2531 bdevname(rdev->bdev, b));
2534 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2536 (unsigned long long)(
2538 choose_data_offset(r10_bio, rdev)),
2539 bdevname(rdev->bdev, b));
2540 atomic_add(s, &rdev->corrected_errors);
2543 rdev_dec_pending(rdev, mddev);
2553 static int narrow_write_error(struct r10bio *r10_bio, int i)
2555 struct bio *bio = r10_bio->master_bio;
2556 struct mddev *mddev = r10_bio->mddev;
2557 struct r10conf *conf = mddev->private;
2558 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2559 /* bio has the data to be written to slot 'i' where
2560 * we just recently had a write error.
2561 * We repeatedly clone the bio and trim down to one block,
2562 * then try the write. Where the write fails we record
2564 * It is conceivable that the bio doesn't exactly align with
2565 * blocks. We must handle this.
2567 * We currently own a reference to the rdev.
2573 int sect_to_write = r10_bio->sectors;
2576 if (rdev->badblocks.shift < 0)
2579 block_sectors = roundup(1 << rdev->badblocks.shift,
2580 bdev_logical_block_size(rdev->bdev) >> 9);
2581 sector = r10_bio->sector;
2582 sectors = ((r10_bio->sector + block_sectors)
2583 & ~(sector_t)(block_sectors - 1))
2586 while (sect_to_write) {
2589 if (sectors > sect_to_write)
2590 sectors = sect_to_write;
2591 /* Write at 'sector' for 'sectors' */
2592 wbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
2593 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2594 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2595 wbio->bi_iter.bi_sector = wsector +
2596 choose_data_offset(r10_bio, rdev);
2597 bio_set_dev(wbio, rdev->bdev);
2598 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2600 if (submit_bio_wait(wbio) < 0)
2602 ok = rdev_set_badblocks(rdev, wsector,
2607 sect_to_write -= sectors;
2609 sectors = block_sectors;
2614 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2616 int slot = r10_bio->read_slot;
2618 struct r10conf *conf = mddev->private;
2619 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2621 /* we got a read error. Maybe the drive is bad. Maybe just
2622 * the block and we can fix it.
2623 * We freeze all other IO, and try reading the block from
2624 * other devices. When we find one, we re-write
2625 * and check it that fixes the read error.
2626 * This is all done synchronously while the array is
2629 bio = r10_bio->devs[slot].bio;
2631 r10_bio->devs[slot].bio = NULL;
2634 r10_bio->devs[slot].bio = IO_BLOCKED;
2635 else if (!test_bit(FailFast, &rdev->flags)) {
2636 freeze_array(conf, 1);
2637 fix_read_error(conf, mddev, r10_bio);
2638 unfreeze_array(conf);
2640 md_error(mddev, rdev);
2642 rdev_dec_pending(rdev, mddev);
2643 allow_barrier(conf);
2645 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2648 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2650 /* Some sort of write request has finished and it
2651 * succeeded in writing where we thought there was a
2652 * bad block. So forget the bad block.
2653 * Or possibly if failed and we need to record
2657 struct md_rdev *rdev;
2659 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2660 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2661 for (m = 0; m < conf->copies; m++) {
2662 int dev = r10_bio->devs[m].devnum;
2663 rdev = conf->mirrors[dev].rdev;
2664 if (r10_bio->devs[m].bio == NULL ||
2665 r10_bio->devs[m].bio->bi_end_io == NULL)
2667 if (!r10_bio->devs[m].bio->bi_status) {
2668 rdev_clear_badblocks(
2670 r10_bio->devs[m].addr,
2671 r10_bio->sectors, 0);
2673 if (!rdev_set_badblocks(
2675 r10_bio->devs[m].addr,
2676 r10_bio->sectors, 0))
2677 md_error(conf->mddev, rdev);
2679 rdev = conf->mirrors[dev].replacement;
2680 if (r10_bio->devs[m].repl_bio == NULL ||
2681 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
2684 if (!r10_bio->devs[m].repl_bio->bi_status) {
2685 rdev_clear_badblocks(
2687 r10_bio->devs[m].addr,
2688 r10_bio->sectors, 0);
2690 if (!rdev_set_badblocks(
2692 r10_bio->devs[m].addr,
2693 r10_bio->sectors, 0))
2694 md_error(conf->mddev, rdev);
2700 for (m = 0; m < conf->copies; m++) {
2701 int dev = r10_bio->devs[m].devnum;
2702 struct bio *bio = r10_bio->devs[m].bio;
2703 rdev = conf->mirrors[dev].rdev;
2704 if (bio == IO_MADE_GOOD) {
2705 rdev_clear_badblocks(
2707 r10_bio->devs[m].addr,
2708 r10_bio->sectors, 0);
2709 rdev_dec_pending(rdev, conf->mddev);
2710 } else if (bio != NULL && bio->bi_status) {
2712 if (!narrow_write_error(r10_bio, m)) {
2713 md_error(conf->mddev, rdev);
2714 set_bit(R10BIO_Degraded,
2717 rdev_dec_pending(rdev, conf->mddev);
2719 bio = r10_bio->devs[m].repl_bio;
2720 rdev = conf->mirrors[dev].replacement;
2721 if (rdev && bio == IO_MADE_GOOD) {
2722 rdev_clear_badblocks(
2724 r10_bio->devs[m].addr,
2725 r10_bio->sectors, 0);
2726 rdev_dec_pending(rdev, conf->mddev);
2730 spin_lock_irq(&conf->device_lock);
2731 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
2733 spin_unlock_irq(&conf->device_lock);
2735 * In case freeze_array() is waiting for condition
2736 * nr_pending == nr_queued + extra to be true.
2738 wake_up(&conf->wait_barrier);
2739 md_wakeup_thread(conf->mddev->thread);
2741 if (test_bit(R10BIO_WriteError,
2743 close_write(r10_bio);
2744 raid_end_bio_io(r10_bio);
2749 static void raid10d(struct md_thread *thread)
2751 struct mddev *mddev = thread->mddev;
2752 struct r10bio *r10_bio;
2753 unsigned long flags;
2754 struct r10conf *conf = mddev->private;
2755 struct list_head *head = &conf->retry_list;
2756 struct blk_plug plug;
2758 md_check_recovery(mddev);
2760 if (!list_empty_careful(&conf->bio_end_io_list) &&
2761 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2763 spin_lock_irqsave(&conf->device_lock, flags);
2764 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2765 while (!list_empty(&conf->bio_end_io_list)) {
2766 list_move(conf->bio_end_io_list.prev, &tmp);
2770 spin_unlock_irqrestore(&conf->device_lock, flags);
2771 while (!list_empty(&tmp)) {
2772 r10_bio = list_first_entry(&tmp, struct r10bio,
2774 list_del(&r10_bio->retry_list);
2775 if (mddev->degraded)
2776 set_bit(R10BIO_Degraded, &r10_bio->state);
2778 if (test_bit(R10BIO_WriteError,
2780 close_write(r10_bio);
2781 raid_end_bio_io(r10_bio);
2785 blk_start_plug(&plug);
2788 flush_pending_writes(conf);
2790 spin_lock_irqsave(&conf->device_lock, flags);
2791 if (list_empty(head)) {
2792 spin_unlock_irqrestore(&conf->device_lock, flags);
2795 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2796 list_del(head->prev);
2798 spin_unlock_irqrestore(&conf->device_lock, flags);
2800 mddev = r10_bio->mddev;
2801 conf = mddev->private;
2802 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2803 test_bit(R10BIO_WriteError, &r10_bio->state))
2804 handle_write_completed(conf, r10_bio);
2805 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2806 reshape_request_write(mddev, r10_bio);
2807 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2808 sync_request_write(mddev, r10_bio);
2809 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2810 recovery_request_write(mddev, r10_bio);
2811 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2812 handle_read_error(mddev, r10_bio);
2817 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2818 md_check_recovery(mddev);
2820 blk_finish_plug(&plug);
2823 static int init_resync(struct r10conf *conf)
2827 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2828 BUG_ON(mempool_initialized(&conf->r10buf_pool));
2829 conf->have_replacement = 0;
2830 for (i = 0; i < conf->geo.raid_disks; i++)
2831 if (conf->mirrors[i].replacement)
2832 conf->have_replacement = 1;
2833 ret = mempool_init(&conf->r10buf_pool, buffs,
2834 r10buf_pool_alloc, r10buf_pool_free, conf);
2837 conf->next_resync = 0;
2841 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
2843 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
2844 struct rsync_pages *rp;
2849 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
2850 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
2851 nalloc = conf->copies; /* resync */
2853 nalloc = 2; /* recovery */
2855 for (i = 0; i < nalloc; i++) {
2856 bio = r10bio->devs[i].bio;
2857 rp = bio->bi_private;
2859 bio->bi_private = rp;
2860 bio = r10bio->devs[i].repl_bio;
2862 rp = bio->bi_private;
2864 bio->bi_private = rp;
2871 * Set cluster_sync_high since we need other nodes to add the
2872 * range [cluster_sync_low, cluster_sync_high] to suspend list.
2874 static void raid10_set_cluster_sync_high(struct r10conf *conf)
2876 sector_t window_size;
2877 int extra_chunk, chunks;
2880 * First, here we define "stripe" as a unit which across
2881 * all member devices one time, so we get chunks by use
2882 * raid_disks / near_copies. Otherwise, if near_copies is
2883 * close to raid_disks, then resync window could increases
2884 * linearly with the increase of raid_disks, which means
2885 * we will suspend a really large IO window while it is not
2886 * necessary. If raid_disks is not divisible by near_copies,
2887 * an extra chunk is needed to ensure the whole "stripe" is
2891 chunks = conf->geo.raid_disks / conf->geo.near_copies;
2892 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
2896 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
2899 * At least use a 32M window to align with raid1's resync window
2901 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
2902 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
2904 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
2908 * perform a "sync" on one "block"
2910 * We need to make sure that no normal I/O request - particularly write
2911 * requests - conflict with active sync requests.
2913 * This is achieved by tracking pending requests and a 'barrier' concept
2914 * that can be installed to exclude normal IO requests.
2916 * Resync and recovery are handled very differently.
2917 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2919 * For resync, we iterate over virtual addresses, read all copies,
2920 * and update if there are differences. If only one copy is live,
2922 * For recovery, we iterate over physical addresses, read a good
2923 * value for each non-in_sync drive, and over-write.
2925 * So, for recovery we may have several outstanding complex requests for a
2926 * given address, one for each out-of-sync device. We model this by allocating
2927 * a number of r10_bio structures, one for each out-of-sync device.
2928 * As we setup these structures, we collect all bio's together into a list
2929 * which we then process collectively to add pages, and then process again
2930 * to pass to generic_make_request.
2932 * The r10_bio structures are linked using a borrowed master_bio pointer.
2933 * This link is counted in ->remaining. When the r10_bio that points to NULL
2934 * has its remaining count decremented to 0, the whole complex operation
2939 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
2942 struct r10conf *conf = mddev->private;
2943 struct r10bio *r10_bio;
2944 struct bio *biolist = NULL, *bio;
2945 sector_t max_sector, nr_sectors;
2948 sector_t sync_blocks;
2949 sector_t sectors_skipped = 0;
2950 int chunks_skipped = 0;
2951 sector_t chunk_mask = conf->geo.chunk_mask;
2954 if (!mempool_initialized(&conf->r10buf_pool))
2955 if (init_resync(conf))
2959 * Allow skipping a full rebuild for incremental assembly
2960 * of a clean array, like RAID1 does.
2962 if (mddev->bitmap == NULL &&
2963 mddev->recovery_cp == MaxSector &&
2964 mddev->reshape_position == MaxSector &&
2965 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2966 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2967 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2968 conf->fullsync == 0) {
2970 return mddev->dev_sectors - sector_nr;
2974 max_sector = mddev->dev_sectors;
2975 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2976 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2977 max_sector = mddev->resync_max_sectors;
2978 if (sector_nr >= max_sector) {
2979 conf->cluster_sync_low = 0;
2980 conf->cluster_sync_high = 0;
2982 /* If we aborted, we need to abort the
2983 * sync on the 'current' bitmap chucks (there can
2984 * be several when recovering multiple devices).
2985 * as we may have started syncing it but not finished.
2986 * We can find the current address in
2987 * mddev->curr_resync, but for recovery,
2988 * we need to convert that to several
2989 * virtual addresses.
2991 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2997 if (mddev->curr_resync < max_sector) { /* aborted */
2998 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2999 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3001 else for (i = 0; i < conf->geo.raid_disks; i++) {
3003 raid10_find_virt(conf, mddev->curr_resync, i);
3004 md_bitmap_end_sync(mddev->bitmap, sect,
3008 /* completed sync */
3009 if ((!mddev->bitmap || conf->fullsync)
3010 && conf->have_replacement
3011 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3012 /* Completed a full sync so the replacements
3013 * are now fully recovered.
3016 for (i = 0; i < conf->geo.raid_disks; i++) {
3017 struct md_rdev *rdev =
3018 rcu_dereference(conf->mirrors[i].replacement);
3020 rdev->recovery_offset = MaxSector;
3026 md_bitmap_close_sync(mddev->bitmap);
3029 return sectors_skipped;
3032 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3033 return reshape_request(mddev, sector_nr, skipped);
3035 if (chunks_skipped >= conf->geo.raid_disks) {
3036 /* if there has been nothing to do on any drive,
3037 * then there is nothing to do at all..
3040 return (max_sector - sector_nr) + sectors_skipped;
3043 if (max_sector > mddev->resync_max)
3044 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3046 /* make sure whole request will fit in a chunk - if chunks
3049 if (conf->geo.near_copies < conf->geo.raid_disks &&
3050 max_sector > (sector_nr | chunk_mask))
3051 max_sector = (sector_nr | chunk_mask) + 1;
3054 * If there is non-resync activity waiting for a turn, then let it
3055 * though before starting on this new sync request.
3057 if (conf->nr_waiting)
3058 schedule_timeout_uninterruptible(1);
3060 /* Again, very different code for resync and recovery.
3061 * Both must result in an r10bio with a list of bios that
3062 * have bi_end_io, bi_sector, bi_disk set,
3063 * and bi_private set to the r10bio.
3064 * For recovery, we may actually create several r10bios
3065 * with 2 bios in each, that correspond to the bios in the main one.
3066 * In this case, the subordinate r10bios link back through a
3067 * borrowed master_bio pointer, and the counter in the master
3068 * includes a ref from each subordinate.
3070 /* First, we decide what to do and set ->bi_end_io
3071 * To end_sync_read if we want to read, and
3072 * end_sync_write if we will want to write.
3075 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3076 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3077 /* recovery... the complicated one */
3081 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3087 struct raid10_info *mirror = &conf->mirrors[i];
3088 struct md_rdev *mrdev, *mreplace;
3091 mrdev = rcu_dereference(mirror->rdev);
3092 mreplace = rcu_dereference(mirror->replacement);
3094 if ((mrdev == NULL ||
3095 test_bit(Faulty, &mrdev->flags) ||
3096 test_bit(In_sync, &mrdev->flags)) &&
3097 (mreplace == NULL ||
3098 test_bit(Faulty, &mreplace->flags))) {
3104 /* want to reconstruct this device */
3106 sect = raid10_find_virt(conf, sector_nr, i);
3107 if (sect >= mddev->resync_max_sectors) {
3108 /* last stripe is not complete - don't
3109 * try to recover this sector.
3114 if (mreplace && test_bit(Faulty, &mreplace->flags))
3116 /* Unless we are doing a full sync, or a replacement
3117 * we only need to recover the block if it is set in
3120 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3122 if (sync_blocks < max_sync)
3123 max_sync = sync_blocks;
3127 /* yep, skip the sync_blocks here, but don't assume
3128 * that there will never be anything to do here
3130 chunks_skipped = -1;
3134 atomic_inc(&mrdev->nr_pending);
3136 atomic_inc(&mreplace->nr_pending);
3139 r10_bio = raid10_alloc_init_r10buf(conf);
3141 raise_barrier(conf, rb2 != NULL);
3142 atomic_set(&r10_bio->remaining, 0);
3144 r10_bio->master_bio = (struct bio*)rb2;
3146 atomic_inc(&rb2->remaining);
3147 r10_bio->mddev = mddev;
3148 set_bit(R10BIO_IsRecover, &r10_bio->state);
3149 r10_bio->sector = sect;
3151 raid10_find_phys(conf, r10_bio);
3153 /* Need to check if the array will still be
3157 for (j = 0; j < conf->geo.raid_disks; j++) {
3158 struct md_rdev *rdev = rcu_dereference(
3159 conf->mirrors[j].rdev);
3160 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3166 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3167 &sync_blocks, still_degraded);
3170 for (j=0; j<conf->copies;j++) {
3172 int d = r10_bio->devs[j].devnum;
3173 sector_t from_addr, to_addr;
3174 struct md_rdev *rdev =
3175 rcu_dereference(conf->mirrors[d].rdev);
3176 sector_t sector, first_bad;
3179 !test_bit(In_sync, &rdev->flags))
3181 /* This is where we read from */
3183 sector = r10_bio->devs[j].addr;
3185 if (is_badblock(rdev, sector, max_sync,
3186 &first_bad, &bad_sectors)) {
3187 if (first_bad > sector)
3188 max_sync = first_bad - sector;
3190 bad_sectors -= (sector
3192 if (max_sync > bad_sectors)
3193 max_sync = bad_sectors;
3197 bio = r10_bio->devs[0].bio;
3198 bio->bi_next = biolist;
3200 bio->bi_end_io = end_sync_read;
3201 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3202 if (test_bit(FailFast, &rdev->flags))
3203 bio->bi_opf |= MD_FAILFAST;
3204 from_addr = r10_bio->devs[j].addr;
3205 bio->bi_iter.bi_sector = from_addr +
3207 bio_set_dev(bio, rdev->bdev);
3208 atomic_inc(&rdev->nr_pending);
3209 /* and we write to 'i' (if not in_sync) */
3211 for (k=0; k<conf->copies; k++)
3212 if (r10_bio->devs[k].devnum == i)
3214 BUG_ON(k == conf->copies);
3215 to_addr = r10_bio->devs[k].addr;
3216 r10_bio->devs[0].devnum = d;
3217 r10_bio->devs[0].addr = from_addr;
3218 r10_bio->devs[1].devnum = i;
3219 r10_bio->devs[1].addr = to_addr;
3221 if (!test_bit(In_sync, &mrdev->flags)) {
3222 bio = r10_bio->devs[1].bio;
3223 bio->bi_next = biolist;
3225 bio->bi_end_io = end_sync_write;
3226 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3227 bio->bi_iter.bi_sector = to_addr
3228 + mrdev->data_offset;
3229 bio_set_dev(bio, mrdev->bdev);
3230 atomic_inc(&r10_bio->remaining);
3232 r10_bio->devs[1].bio->bi_end_io = NULL;
3234 /* and maybe write to replacement */
3235 bio = r10_bio->devs[1].repl_bio;
3237 bio->bi_end_io = NULL;
3238 /* Note: if mreplace != NULL, then bio
3239 * cannot be NULL as r10buf_pool_alloc will
3240 * have allocated it.
3241 * So the second test here is pointless.
3242 * But it keeps semantic-checkers happy, and
3243 * this comment keeps human reviewers
3246 if (mreplace == NULL || bio == NULL ||
3247 test_bit(Faulty, &mreplace->flags))
3249 bio->bi_next = biolist;
3251 bio->bi_end_io = end_sync_write;
3252 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3253 bio->bi_iter.bi_sector = to_addr +
3254 mreplace->data_offset;
3255 bio_set_dev(bio, mreplace->bdev);
3256 atomic_inc(&r10_bio->remaining);
3260 if (j == conf->copies) {
3261 /* Cannot recover, so abort the recovery or
3262 * record a bad block */
3264 /* problem is that there are bad blocks
3265 * on other device(s)
3268 for (k = 0; k < conf->copies; k++)
3269 if (r10_bio->devs[k].devnum == i)
3271 if (!test_bit(In_sync,
3273 && !rdev_set_badblocks(
3275 r10_bio->devs[k].addr,
3279 !rdev_set_badblocks(
3281 r10_bio->devs[k].addr,
3286 if (!test_and_set_bit(MD_RECOVERY_INTR,
3288 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3290 mirror->recovery_disabled
3291 = mddev->recovery_disabled;
3295 atomic_dec(&rb2->remaining);
3297 rdev_dec_pending(mrdev, mddev);
3299 rdev_dec_pending(mreplace, mddev);
3302 rdev_dec_pending(mrdev, mddev);
3304 rdev_dec_pending(mreplace, mddev);
3305 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3306 /* Only want this if there is elsewhere to
3307 * read from. 'j' is currently the first
3311 for (; j < conf->copies; j++) {
3312 int d = r10_bio->devs[j].devnum;
3313 if (conf->mirrors[d].rdev &&
3315 &conf->mirrors[d].rdev->flags))
3319 r10_bio->devs[0].bio->bi_opf
3323 if (biolist == NULL) {
3325 struct r10bio *rb2 = r10_bio;
3326 r10_bio = (struct r10bio*) rb2->master_bio;
3327 rb2->master_bio = NULL;
3333 /* resync. Schedule a read for every block at this virt offset */
3337 * Since curr_resync_completed could probably not update in
3338 * time, and we will set cluster_sync_low based on it.
3339 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3340 * safety reason, which ensures curr_resync_completed is
3341 * updated in bitmap_cond_end_sync.
3343 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3344 mddev_is_clustered(mddev) &&
3345 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
3347 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3348 &sync_blocks, mddev->degraded) &&
3349 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3350 &mddev->recovery)) {
3351 /* We can skip this block */
3353 return sync_blocks + sectors_skipped;
3355 if (sync_blocks < max_sync)
3356 max_sync = sync_blocks;
3357 r10_bio = raid10_alloc_init_r10buf(conf);
3360 r10_bio->mddev = mddev;
3361 atomic_set(&r10_bio->remaining, 0);
3362 raise_barrier(conf, 0);
3363 conf->next_resync = sector_nr;
3365 r10_bio->master_bio = NULL;
3366 r10_bio->sector = sector_nr;
3367 set_bit(R10BIO_IsSync, &r10_bio->state);
3368 raid10_find_phys(conf, r10_bio);
3369 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3371 for (i = 0; i < conf->copies; i++) {
3372 int d = r10_bio->devs[i].devnum;
3373 sector_t first_bad, sector;
3375 struct md_rdev *rdev;
3377 if (r10_bio->devs[i].repl_bio)
3378 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3380 bio = r10_bio->devs[i].bio;
3381 bio->bi_status = BLK_STS_IOERR;
3383 rdev = rcu_dereference(conf->mirrors[d].rdev);
3384 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3388 sector = r10_bio->devs[i].addr;
3389 if (is_badblock(rdev, sector, max_sync,
3390 &first_bad, &bad_sectors)) {
3391 if (first_bad > sector)
3392 max_sync = first_bad - sector;
3394 bad_sectors -= (sector - first_bad);
3395 if (max_sync > bad_sectors)
3396 max_sync = bad_sectors;
3401 atomic_inc(&rdev->nr_pending);
3402 atomic_inc(&r10_bio->remaining);
3403 bio->bi_next = biolist;
3405 bio->bi_end_io = end_sync_read;
3406 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3407 if (test_bit(FailFast, &rdev->flags))
3408 bio->bi_opf |= MD_FAILFAST;
3409 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3410 bio_set_dev(bio, rdev->bdev);
3413 rdev = rcu_dereference(conf->mirrors[d].replacement);
3414 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3418 atomic_inc(&rdev->nr_pending);
3420 /* Need to set up for writing to the replacement */
3421 bio = r10_bio->devs[i].repl_bio;
3422 bio->bi_status = BLK_STS_IOERR;
3424 sector = r10_bio->devs[i].addr;
3425 bio->bi_next = biolist;
3427 bio->bi_end_io = end_sync_write;
3428 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3429 if (test_bit(FailFast, &rdev->flags))
3430 bio->bi_opf |= MD_FAILFAST;
3431 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3432 bio_set_dev(bio, rdev->bdev);
3438 for (i=0; i<conf->copies; i++) {
3439 int d = r10_bio->devs[i].devnum;
3440 if (r10_bio->devs[i].bio->bi_end_io)
3441 rdev_dec_pending(conf->mirrors[d].rdev,
3443 if (r10_bio->devs[i].repl_bio &&
3444 r10_bio->devs[i].repl_bio->bi_end_io)
3446 conf->mirrors[d].replacement,
3456 if (sector_nr + max_sync < max_sector)
3457 max_sector = sector_nr + max_sync;
3460 int len = PAGE_SIZE;
3461 if (sector_nr + (len>>9) > max_sector)
3462 len = (max_sector - sector_nr) << 9;
3465 for (bio= biolist ; bio ; bio=bio->bi_next) {
3466 struct resync_pages *rp = get_resync_pages(bio);
3467 page = resync_fetch_page(rp, page_idx);
3469 * won't fail because the vec table is big enough
3470 * to hold all these pages
3472 bio_add_page(bio, page, len, 0);
3474 nr_sectors += len>>9;
3475 sector_nr += len>>9;
3476 } while (++page_idx < RESYNC_PAGES);
3477 r10_bio->sectors = nr_sectors;
3479 if (mddev_is_clustered(mddev) &&
3480 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3481 /* It is resync not recovery */
3482 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3483 conf->cluster_sync_low = mddev->curr_resync_completed;
3484 raid10_set_cluster_sync_high(conf);
3485 /* Send resync message */
3486 md_cluster_ops->resync_info_update(mddev,
3487 conf->cluster_sync_low,
3488 conf->cluster_sync_high);
3490 } else if (mddev_is_clustered(mddev)) {
3491 /* This is recovery not resync */
3492 sector_t sect_va1, sect_va2;
3493 bool broadcast_msg = false;
3495 for (i = 0; i < conf->geo.raid_disks; i++) {
3497 * sector_nr is a device address for recovery, so we
3498 * need translate it to array address before compare
3499 * with cluster_sync_high.
3501 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3503 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3504 broadcast_msg = true;
3506 * curr_resync_completed is similar as
3507 * sector_nr, so make the translation too.
3509 sect_va2 = raid10_find_virt(conf,
3510 mddev->curr_resync_completed, i);
3512 if (conf->cluster_sync_low == 0 ||
3513 conf->cluster_sync_low > sect_va2)
3514 conf->cluster_sync_low = sect_va2;
3517 if (broadcast_msg) {
3518 raid10_set_cluster_sync_high(conf);
3519 md_cluster_ops->resync_info_update(mddev,
3520 conf->cluster_sync_low,
3521 conf->cluster_sync_high);
3527 biolist = biolist->bi_next;
3529 bio->bi_next = NULL;
3530 r10_bio = get_resync_r10bio(bio);
3531 r10_bio->sectors = nr_sectors;
3533 if (bio->bi_end_io == end_sync_read) {
3534 md_sync_acct_bio(bio, nr_sectors);
3536 generic_make_request(bio);
3540 if (sectors_skipped)
3541 /* pretend they weren't skipped, it makes
3542 * no important difference in this case
3544 md_done_sync(mddev, sectors_skipped, 1);
3546 return sectors_skipped + nr_sectors;
3548 /* There is nowhere to write, so all non-sync
3549 * drives must be failed or in resync, all drives
3550 * have a bad block, so try the next chunk...
3552 if (sector_nr + max_sync < max_sector)
3553 max_sector = sector_nr + max_sync;
3555 sectors_skipped += (max_sector - sector_nr);
3557 sector_nr = max_sector;
3562 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3565 struct r10conf *conf = mddev->private;
3568 raid_disks = min(conf->geo.raid_disks,
3569 conf->prev.raid_disks);
3571 sectors = conf->dev_sectors;
3573 size = sectors >> conf->geo.chunk_shift;
3574 sector_div(size, conf->geo.far_copies);
3575 size = size * raid_disks;
3576 sector_div(size, conf->geo.near_copies);
3578 return size << conf->geo.chunk_shift;
3581 static void calc_sectors(struct r10conf *conf, sector_t size)
3583 /* Calculate the number of sectors-per-device that will
3584 * actually be used, and set conf->dev_sectors and
3588 size = size >> conf->geo.chunk_shift;
3589 sector_div(size, conf->geo.far_copies);
3590 size = size * conf->geo.raid_disks;
3591 sector_div(size, conf->geo.near_copies);
3592 /* 'size' is now the number of chunks in the array */
3593 /* calculate "used chunks per device" */
3594 size = size * conf->copies;
3596 /* We need to round up when dividing by raid_disks to
3597 * get the stride size.
3599 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3601 conf->dev_sectors = size << conf->geo.chunk_shift;
3603 if (conf->geo.far_offset)
3604 conf->geo.stride = 1 << conf->geo.chunk_shift;
3606 sector_div(size, conf->geo.far_copies);
3607 conf->geo.stride = size << conf->geo.chunk_shift;
3611 enum geo_type {geo_new, geo_old, geo_start};
3612 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3615 int layout, chunk, disks;
3618 layout = mddev->layout;
3619 chunk = mddev->chunk_sectors;
3620 disks = mddev->raid_disks - mddev->delta_disks;
3623 layout = mddev->new_layout;
3624 chunk = mddev->new_chunk_sectors;
3625 disks = mddev->raid_disks;
3627 default: /* avoid 'may be unused' warnings */
3628 case geo_start: /* new when starting reshape - raid_disks not
3630 layout = mddev->new_layout;
3631 chunk = mddev->new_chunk_sectors;
3632 disks = mddev->raid_disks + mddev->delta_disks;
3637 if (chunk < (PAGE_SIZE >> 9) ||
3638 !is_power_of_2(chunk))
3641 fc = (layout >> 8) & 255;
3642 fo = layout & (1<<16);
3643 geo->raid_disks = disks;
3644 geo->near_copies = nc;
3645 geo->far_copies = fc;
3646 geo->far_offset = fo;
3647 switch (layout >> 17) {
3648 case 0: /* original layout. simple but not always optimal */
3649 geo->far_set_size = disks;
3651 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3652 * actually using this, but leave code here just in case.*/
3653 geo->far_set_size = disks/fc;
3654 WARN(geo->far_set_size < fc,
3655 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3657 case 2: /* "improved" layout fixed to match documentation */
3658 geo->far_set_size = fc * nc;
3660 default: /* Not a valid layout */
3663 geo->chunk_mask = chunk - 1;
3664 geo->chunk_shift = ffz(~chunk);
3668 static struct r10conf *setup_conf(struct mddev *mddev)
3670 struct r10conf *conf = NULL;
3675 copies = setup_geo(&geo, mddev, geo_new);
3678 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3679 mdname(mddev), PAGE_SIZE);
3683 if (copies < 2 || copies > mddev->raid_disks) {
3684 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3685 mdname(mddev), mddev->new_layout);
3690 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3694 /* FIXME calc properly */
3695 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
3696 sizeof(struct raid10_info),
3701 conf->tmppage = alloc_page(GFP_KERNEL);
3706 conf->copies = copies;
3707 err = mempool_init(&conf->r10bio_pool, NR_RAID10_BIOS, r10bio_pool_alloc,
3708 r10bio_pool_free, conf);
3712 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
3716 calc_sectors(conf, mddev->dev_sectors);
3717 if (mddev->reshape_position == MaxSector) {
3718 conf->prev = conf->geo;
3719 conf->reshape_progress = MaxSector;
3721 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3725 conf->reshape_progress = mddev->reshape_position;
3726 if (conf->prev.far_offset)
3727 conf->prev.stride = 1 << conf->prev.chunk_shift;
3729 /* far_copies must be 1 */
3730 conf->prev.stride = conf->dev_sectors;
3732 conf->reshape_safe = conf->reshape_progress;
3733 spin_lock_init(&conf->device_lock);
3734 INIT_LIST_HEAD(&conf->retry_list);
3735 INIT_LIST_HEAD(&conf->bio_end_io_list);
3737 spin_lock_init(&conf->resync_lock);
3738 init_waitqueue_head(&conf->wait_barrier);
3739 atomic_set(&conf->nr_pending, 0);
3742 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3746 conf->mddev = mddev;
3751 mempool_exit(&conf->r10bio_pool);
3752 kfree(conf->mirrors);
3753 safe_put_page(conf->tmppage);
3754 bioset_exit(&conf->bio_split);
3757 return ERR_PTR(err);
3760 static int raid10_run(struct mddev *mddev)
3762 struct r10conf *conf;
3763 int i, disk_idx, chunk_size;
3764 struct raid10_info *disk;
3765 struct md_rdev *rdev;
3767 sector_t min_offset_diff = 0;
3769 bool discard_supported = false;
3771 if (mddev_init_writes_pending(mddev) < 0)
3774 if (mddev->private == NULL) {
3775 conf = setup_conf(mddev);
3777 return PTR_ERR(conf);
3778 mddev->private = conf;
3780 conf = mddev->private;
3784 if (mddev_is_clustered(conf->mddev)) {
3787 fc = (mddev->layout >> 8) & 255;
3788 fo = mddev->layout & (1<<16);
3789 if (fc > 1 || fo > 0) {
3790 pr_err("only near layout is supported by clustered"
3796 mddev->thread = conf->thread;
3797 conf->thread = NULL;
3799 chunk_size = mddev->chunk_sectors << 9;
3801 blk_queue_max_discard_sectors(mddev->queue,
3802 mddev->chunk_sectors);
3803 blk_queue_max_write_same_sectors(mddev->queue, 0);
3804 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3805 blk_queue_io_min(mddev->queue, chunk_size);
3806 if (conf->geo.raid_disks % conf->geo.near_copies)
3807 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3809 blk_queue_io_opt(mddev->queue, chunk_size *
3810 (conf->geo.raid_disks / conf->geo.near_copies));
3813 rdev_for_each(rdev, mddev) {
3816 disk_idx = rdev->raid_disk;
3819 if (disk_idx >= conf->geo.raid_disks &&
3820 disk_idx >= conf->prev.raid_disks)
3822 disk = conf->mirrors + disk_idx;
3824 if (test_bit(Replacement, &rdev->flags)) {
3825 if (disk->replacement)
3827 disk->replacement = rdev;
3833 diff = (rdev->new_data_offset - rdev->data_offset);
3834 if (!mddev->reshape_backwards)
3838 if (first || diff < min_offset_diff)
3839 min_offset_diff = diff;
3842 disk_stack_limits(mddev->gendisk, rdev->bdev,
3843 rdev->data_offset << 9);
3845 disk->head_position = 0;
3847 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3848 discard_supported = true;
3853 if (discard_supported)
3854 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
3857 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
3860 /* need to check that every block has at least one working mirror */
3861 if (!enough(conf, -1)) {
3862 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3867 if (conf->reshape_progress != MaxSector) {
3868 /* must ensure that shape change is supported */
3869 if (conf->geo.far_copies != 1 &&
3870 conf->geo.far_offset == 0)
3872 if (conf->prev.far_copies != 1 &&
3873 conf->prev.far_offset == 0)
3877 mddev->degraded = 0;
3879 i < conf->geo.raid_disks
3880 || i < conf->prev.raid_disks;
3883 disk = conf->mirrors + i;
3885 if (!disk->rdev && disk->replacement) {
3886 /* The replacement is all we have - use it */
3887 disk->rdev = disk->replacement;
3888 disk->replacement = NULL;
3889 clear_bit(Replacement, &disk->rdev->flags);
3893 !test_bit(In_sync, &disk->rdev->flags)) {
3894 disk->head_position = 0;
3897 disk->rdev->saved_raid_disk < 0)
3901 if (disk->replacement &&
3902 !test_bit(In_sync, &disk->replacement->flags) &&
3903 disk->replacement->saved_raid_disk < 0) {
3907 disk->recovery_disabled = mddev->recovery_disabled - 1;
3910 if (mddev->recovery_cp != MaxSector)
3911 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3913 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3914 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3915 conf->geo.raid_disks);
3917 * Ok, everything is just fine now
3919 mddev->dev_sectors = conf->dev_sectors;
3920 size = raid10_size(mddev, 0, 0);
3921 md_set_array_sectors(mddev, size);
3922 mddev->resync_max_sectors = size;
3923 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3926 int stripe = conf->geo.raid_disks *
3927 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3929 /* Calculate max read-ahead size.
3930 * We need to readahead at least twice a whole stripe....
3933 stripe /= conf->geo.near_copies;
3934 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3935 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
3938 if (md_integrity_register(mddev))
3941 if (conf->reshape_progress != MaxSector) {
3942 unsigned long before_length, after_length;
3944 before_length = ((1 << conf->prev.chunk_shift) *
3945 conf->prev.far_copies);
3946 after_length = ((1 << conf->geo.chunk_shift) *
3947 conf->geo.far_copies);
3949 if (max(before_length, after_length) > min_offset_diff) {
3950 /* This cannot work */
3951 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3954 conf->offset_diff = min_offset_diff;
3956 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3957 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3958 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3959 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3960 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3962 if (!mddev->sync_thread)
3969 md_unregister_thread(&mddev->thread);
3970 mempool_exit(&conf->r10bio_pool);
3971 safe_put_page(conf->tmppage);
3972 kfree(conf->mirrors);
3974 mddev->private = NULL;
3979 static void raid10_free(struct mddev *mddev, void *priv)
3981 struct r10conf *conf = priv;
3983 mempool_exit(&conf->r10bio_pool);
3984 safe_put_page(conf->tmppage);
3985 kfree(conf->mirrors);
3986 kfree(conf->mirrors_old);
3987 kfree(conf->mirrors_new);
3988 bioset_exit(&conf->bio_split);
3992 static void raid10_quiesce(struct mddev *mddev, int quiesce)
3994 struct r10conf *conf = mddev->private;
3997 raise_barrier(conf, 0);
3999 lower_barrier(conf);
4002 static int raid10_resize(struct mddev *mddev, sector_t sectors)
4004 /* Resize of 'far' arrays is not supported.
4005 * For 'near' and 'offset' arrays we can set the
4006 * number of sectors used to be an appropriate multiple
4007 * of the chunk size.
4008 * For 'offset', this is far_copies*chunksize.
4009 * For 'near' the multiplier is the LCM of
4010 * near_copies and raid_disks.
4011 * So if far_copies > 1 && !far_offset, fail.
4012 * Else find LCM(raid_disks, near_copy)*far_copies and
4013 * multiply by chunk_size. Then round to this number.
4014 * This is mostly done by raid10_size()
4016 struct r10conf *conf = mddev->private;
4017 sector_t oldsize, size;
4019 if (mddev->reshape_position != MaxSector)
4022 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
4025 oldsize = raid10_size(mddev, 0, 0);
4026 size = raid10_size(mddev, sectors, 0);
4027 if (mddev->external_size &&
4028 mddev->array_sectors > size)
4030 if (mddev->bitmap) {
4031 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
4035 md_set_array_sectors(mddev, size);
4036 if (sectors > mddev->dev_sectors &&
4037 mddev->recovery_cp > oldsize) {
4038 mddev->recovery_cp = oldsize;
4039 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4041 calc_sectors(conf, sectors);
4042 mddev->dev_sectors = conf->dev_sectors;
4043 mddev->resync_max_sectors = size;
4047 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
4049 struct md_rdev *rdev;
4050 struct r10conf *conf;
4052 if (mddev->degraded > 0) {
4053 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4055 return ERR_PTR(-EINVAL);
4057 sector_div(size, devs);
4059 /* Set new parameters */
4060 mddev->new_level = 10;
4061 /* new layout: far_copies = 1, near_copies = 2 */
4062 mddev->new_layout = (1<<8) + 2;
4063 mddev->new_chunk_sectors = mddev->chunk_sectors;
4064 mddev->delta_disks = mddev->raid_disks;
4065 mddev->raid_disks *= 2;
4066 /* make sure it will be not marked as dirty */
4067 mddev->recovery_cp = MaxSector;
4068 mddev->dev_sectors = size;
4070 conf = setup_conf(mddev);
4071 if (!IS_ERR(conf)) {
4072 rdev_for_each(rdev, mddev)
4073 if (rdev->raid_disk >= 0) {
4074 rdev->new_raid_disk = rdev->raid_disk * 2;
4075 rdev->sectors = size;
4083 static void *raid10_takeover(struct mddev *mddev)
4085 struct r0conf *raid0_conf;
4087 /* raid10 can take over:
4088 * raid0 - providing it has only two drives
4090 if (mddev->level == 0) {
4091 /* for raid0 takeover only one zone is supported */
4092 raid0_conf = mddev->private;
4093 if (raid0_conf->nr_strip_zones > 1) {
4094 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4096 return ERR_PTR(-EINVAL);
4098 return raid10_takeover_raid0(mddev,
4099 raid0_conf->strip_zone->zone_end,
4100 raid0_conf->strip_zone->nb_dev);
4102 return ERR_PTR(-EINVAL);
4105 static int raid10_check_reshape(struct mddev *mddev)
4107 /* Called when there is a request to change
4108 * - layout (to ->new_layout)
4109 * - chunk size (to ->new_chunk_sectors)
4110 * - raid_disks (by delta_disks)
4111 * or when trying to restart a reshape that was ongoing.
4113 * We need to validate the request and possibly allocate
4114 * space if that might be an issue later.
4116 * Currently we reject any reshape of a 'far' mode array,
4117 * allow chunk size to change if new is generally acceptable,
4118 * allow raid_disks to increase, and allow
4119 * a switch between 'near' mode and 'offset' mode.
4121 struct r10conf *conf = mddev->private;
4124 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4127 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4128 /* mustn't change number of copies */
4130 if (geo.far_copies > 1 && !geo.far_offset)
4131 /* Cannot switch to 'far' mode */
4134 if (mddev->array_sectors & geo.chunk_mask)
4135 /* not factor of array size */
4138 if (!enough(conf, -1))
4141 kfree(conf->mirrors_new);
4142 conf->mirrors_new = NULL;
4143 if (mddev->delta_disks > 0) {
4144 /* allocate new 'mirrors' list */
4146 kcalloc(mddev->raid_disks + mddev->delta_disks,
4147 sizeof(struct raid10_info),
4149 if (!conf->mirrors_new)
4156 * Need to check if array has failed when deciding whether to:
4158 * - remove non-faulty devices
4161 * This determination is simple when no reshape is happening.
4162 * However if there is a reshape, we need to carefully check
4163 * both the before and after sections.
4164 * This is because some failed devices may only affect one
4165 * of the two sections, and some non-in_sync devices may
4166 * be insync in the section most affected by failed devices.
4168 static int calc_degraded(struct r10conf *conf)
4170 int degraded, degraded2;
4175 /* 'prev' section first */
4176 for (i = 0; i < conf->prev.raid_disks; i++) {
4177 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4178 if (!rdev || test_bit(Faulty, &rdev->flags))
4180 else if (!test_bit(In_sync, &rdev->flags))
4181 /* When we can reduce the number of devices in
4182 * an array, this might not contribute to
4183 * 'degraded'. It does now.
4188 if (conf->geo.raid_disks == conf->prev.raid_disks)
4192 for (i = 0; i < conf->geo.raid_disks; i++) {
4193 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4194 if (!rdev || test_bit(Faulty, &rdev->flags))
4196 else if (!test_bit(In_sync, &rdev->flags)) {
4197 /* If reshape is increasing the number of devices,
4198 * this section has already been recovered, so
4199 * it doesn't contribute to degraded.
4202 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4207 if (degraded2 > degraded)
4212 static int raid10_start_reshape(struct mddev *mddev)
4214 /* A 'reshape' has been requested. This commits
4215 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4216 * This also checks if there are enough spares and adds them
4218 * We currently require enough spares to make the final
4219 * array non-degraded. We also require that the difference
4220 * between old and new data_offset - on each device - is
4221 * enough that we never risk over-writing.
4224 unsigned long before_length, after_length;
4225 sector_t min_offset_diff = 0;
4228 struct r10conf *conf = mddev->private;
4229 struct md_rdev *rdev;
4233 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4236 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4239 before_length = ((1 << conf->prev.chunk_shift) *
4240 conf->prev.far_copies);
4241 after_length = ((1 << conf->geo.chunk_shift) *
4242 conf->geo.far_copies);
4244 rdev_for_each(rdev, mddev) {
4245 if (!test_bit(In_sync, &rdev->flags)
4246 && !test_bit(Faulty, &rdev->flags))
4248 if (rdev->raid_disk >= 0) {
4249 long long diff = (rdev->new_data_offset
4250 - rdev->data_offset);
4251 if (!mddev->reshape_backwards)
4255 if (first || diff < min_offset_diff)
4256 min_offset_diff = diff;
4261 if (max(before_length, after_length) > min_offset_diff)
4264 if (spares < mddev->delta_disks)
4267 conf->offset_diff = min_offset_diff;
4268 spin_lock_irq(&conf->device_lock);
4269 if (conf->mirrors_new) {
4270 memcpy(conf->mirrors_new, conf->mirrors,
4271 sizeof(struct raid10_info)*conf->prev.raid_disks);
4273 kfree(conf->mirrors_old);
4274 conf->mirrors_old = conf->mirrors;
4275 conf->mirrors = conf->mirrors_new;
4276 conf->mirrors_new = NULL;
4278 setup_geo(&conf->geo, mddev, geo_start);
4280 if (mddev->reshape_backwards) {
4281 sector_t size = raid10_size(mddev, 0, 0);
4282 if (size < mddev->array_sectors) {
4283 spin_unlock_irq(&conf->device_lock);
4284 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4288 mddev->resync_max_sectors = size;
4289 conf->reshape_progress = size;
4291 conf->reshape_progress = 0;
4292 conf->reshape_safe = conf->reshape_progress;
4293 spin_unlock_irq(&conf->device_lock);
4295 if (mddev->delta_disks && mddev->bitmap) {
4296 ret = md_bitmap_resize(mddev->bitmap,
4297 raid10_size(mddev, 0, conf->geo.raid_disks),
4302 if (mddev->delta_disks > 0) {
4303 rdev_for_each(rdev, mddev)
4304 if (rdev->raid_disk < 0 &&
4305 !test_bit(Faulty, &rdev->flags)) {
4306 if (raid10_add_disk(mddev, rdev) == 0) {
4307 if (rdev->raid_disk >=
4308 conf->prev.raid_disks)
4309 set_bit(In_sync, &rdev->flags);
4311 rdev->recovery_offset = 0;
4313 if (sysfs_link_rdev(mddev, rdev))
4314 /* Failure here is OK */;
4316 } else if (rdev->raid_disk >= conf->prev.raid_disks
4317 && !test_bit(Faulty, &rdev->flags)) {
4318 /* This is a spare that was manually added */
4319 set_bit(In_sync, &rdev->flags);
4322 /* When a reshape changes the number of devices,
4323 * ->degraded is measured against the larger of the
4324 * pre and post numbers.
4326 spin_lock_irq(&conf->device_lock);
4327 mddev->degraded = calc_degraded(conf);
4328 spin_unlock_irq(&conf->device_lock);
4329 mddev->raid_disks = conf->geo.raid_disks;
4330 mddev->reshape_position = conf->reshape_progress;
4331 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4333 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4334 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4335 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4336 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4337 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4339 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4341 if (!mddev->sync_thread) {
4345 conf->reshape_checkpoint = jiffies;
4346 md_wakeup_thread(mddev->sync_thread);
4347 md_new_event(mddev);
4351 mddev->recovery = 0;
4352 spin_lock_irq(&conf->device_lock);
4353 conf->geo = conf->prev;
4354 mddev->raid_disks = conf->geo.raid_disks;
4355 rdev_for_each(rdev, mddev)
4356 rdev->new_data_offset = rdev->data_offset;
4358 conf->reshape_progress = MaxSector;
4359 conf->reshape_safe = MaxSector;
4360 mddev->reshape_position = MaxSector;
4361 spin_unlock_irq(&conf->device_lock);
4365 /* Calculate the last device-address that could contain
4366 * any block from the chunk that includes the array-address 's'
4367 * and report the next address.
4368 * i.e. the address returned will be chunk-aligned and after
4369 * any data that is in the chunk containing 's'.
4371 static sector_t last_dev_address(sector_t s, struct geom *geo)
4373 s = (s | geo->chunk_mask) + 1;
4374 s >>= geo->chunk_shift;
4375 s *= geo->near_copies;
4376 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4377 s *= geo->far_copies;
4378 s <<= geo->chunk_shift;
4382 /* Calculate the first device-address that could contain
4383 * any block from the chunk that includes the array-address 's'.
4384 * This too will be the start of a chunk
4386 static sector_t first_dev_address(sector_t s, struct geom *geo)
4388 s >>= geo->chunk_shift;
4389 s *= geo->near_copies;
4390 sector_div(s, geo->raid_disks);
4391 s *= geo->far_copies;
4392 s <<= geo->chunk_shift;
4396 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4399 /* We simply copy at most one chunk (smallest of old and new)
4400 * at a time, possibly less if that exceeds RESYNC_PAGES,
4401 * or we hit a bad block or something.
4402 * This might mean we pause for normal IO in the middle of
4403 * a chunk, but that is not a problem as mddev->reshape_position
4404 * can record any location.
4406 * If we will want to write to a location that isn't
4407 * yet recorded as 'safe' (i.e. in metadata on disk) then
4408 * we need to flush all reshape requests and update the metadata.
4410 * When reshaping forwards (e.g. to more devices), we interpret
4411 * 'safe' as the earliest block which might not have been copied
4412 * down yet. We divide this by previous stripe size and multiply
4413 * by previous stripe length to get lowest device offset that we
4414 * cannot write to yet.
4415 * We interpret 'sector_nr' as an address that we want to write to.
4416 * From this we use last_device_address() to find where we might
4417 * write to, and first_device_address on the 'safe' position.
4418 * If this 'next' write position is after the 'safe' position,
4419 * we must update the metadata to increase the 'safe' position.
4421 * When reshaping backwards, we round in the opposite direction
4422 * and perform the reverse test: next write position must not be
4423 * less than current safe position.
4425 * In all this the minimum difference in data offsets
4426 * (conf->offset_diff - always positive) allows a bit of slack,
4427 * so next can be after 'safe', but not by more than offset_diff
4429 * We need to prepare all the bios here before we start any IO
4430 * to ensure the size we choose is acceptable to all devices.
4431 * The means one for each copy for write-out and an extra one for
4433 * We store the read-in bio in ->master_bio and the others in
4434 * ->devs[x].bio and ->devs[x].repl_bio.
4436 struct r10conf *conf = mddev->private;
4437 struct r10bio *r10_bio;
4438 sector_t next, safe, last;
4442 struct md_rdev *rdev;
4445 struct bio *bio, *read_bio;
4446 int sectors_done = 0;
4447 struct page **pages;
4449 if (sector_nr == 0) {
4450 /* If restarting in the middle, skip the initial sectors */
4451 if (mddev->reshape_backwards &&
4452 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4453 sector_nr = (raid10_size(mddev, 0, 0)
4454 - conf->reshape_progress);
4455 } else if (!mddev->reshape_backwards &&
4456 conf->reshape_progress > 0)
4457 sector_nr = conf->reshape_progress;
4459 mddev->curr_resync_completed = sector_nr;
4460 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4466 /* We don't use sector_nr to track where we are up to
4467 * as that doesn't work well for ->reshape_backwards.
4468 * So just use ->reshape_progress.
4470 if (mddev->reshape_backwards) {
4471 /* 'next' is the earliest device address that we might
4472 * write to for this chunk in the new layout
4474 next = first_dev_address(conf->reshape_progress - 1,
4477 /* 'safe' is the last device address that we might read from
4478 * in the old layout after a restart
4480 safe = last_dev_address(conf->reshape_safe - 1,
4483 if (next + conf->offset_diff < safe)
4486 last = conf->reshape_progress - 1;
4487 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4488 & conf->prev.chunk_mask);
4489 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4490 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4492 /* 'next' is after the last device address that we
4493 * might write to for this chunk in the new layout
4495 next = last_dev_address(conf->reshape_progress, &conf->geo);
4497 /* 'safe' is the earliest device address that we might
4498 * read from in the old layout after a restart
4500 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4502 /* Need to update metadata if 'next' might be beyond 'safe'
4503 * as that would possibly corrupt data
4505 if (next > safe + conf->offset_diff)
4508 sector_nr = conf->reshape_progress;
4509 last = sector_nr | (conf->geo.chunk_mask
4510 & conf->prev.chunk_mask);
4512 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4513 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4517 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4518 /* Need to update reshape_position in metadata */
4520 mddev->reshape_position = conf->reshape_progress;
4521 if (mddev->reshape_backwards)
4522 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4523 - conf->reshape_progress;
4525 mddev->curr_resync_completed = conf->reshape_progress;
4526 conf->reshape_checkpoint = jiffies;
4527 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4528 md_wakeup_thread(mddev->thread);
4529 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4530 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4531 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4532 allow_barrier(conf);
4533 return sectors_done;
4535 conf->reshape_safe = mddev->reshape_position;
4536 allow_barrier(conf);
4539 raise_barrier(conf, 0);
4541 /* Now schedule reads for blocks from sector_nr to last */
4542 r10_bio = raid10_alloc_init_r10buf(conf);
4544 raise_barrier(conf, 1);
4545 atomic_set(&r10_bio->remaining, 0);
4546 r10_bio->mddev = mddev;
4547 r10_bio->sector = sector_nr;
4548 set_bit(R10BIO_IsReshape, &r10_bio->state);
4549 r10_bio->sectors = last - sector_nr + 1;
4550 rdev = read_balance(conf, r10_bio, &max_sectors);
4551 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4554 /* Cannot read from here, so need to record bad blocks
4555 * on all the target devices.
4558 mempool_free(r10_bio, &conf->r10buf_pool);
4559 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4560 return sectors_done;
4563 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4565 bio_set_dev(read_bio, rdev->bdev);
4566 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4567 + rdev->data_offset);
4568 read_bio->bi_private = r10_bio;
4569 read_bio->bi_end_io = end_reshape_read;
4570 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
4571 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4572 read_bio->bi_status = 0;
4573 read_bio->bi_vcnt = 0;
4574 read_bio->bi_iter.bi_size = 0;
4575 r10_bio->master_bio = read_bio;
4576 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4578 /* Now find the locations in the new layout */
4579 __raid10_find_phys(&conf->geo, r10_bio);
4582 read_bio->bi_next = NULL;
4585 for (s = 0; s < conf->copies*2; s++) {
4587 int d = r10_bio->devs[s/2].devnum;
4588 struct md_rdev *rdev2;
4590 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4591 b = r10_bio->devs[s/2].repl_bio;
4593 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4594 b = r10_bio->devs[s/2].bio;
4596 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4599 bio_set_dev(b, rdev2->bdev);
4600 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4601 rdev2->new_data_offset;
4602 b->bi_end_io = end_reshape_write;
4603 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4608 /* Now add as many pages as possible to all of these bios. */
4611 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4612 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4613 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4614 int len = (max_sectors - s) << 9;
4615 if (len > PAGE_SIZE)
4617 for (bio = blist; bio ; bio = bio->bi_next) {
4619 * won't fail because the vec table is big enough
4620 * to hold all these pages
4622 bio_add_page(bio, page, len, 0);
4624 sector_nr += len >> 9;
4625 nr_sectors += len >> 9;
4628 r10_bio->sectors = nr_sectors;
4630 /* Now submit the read */
4631 md_sync_acct_bio(read_bio, r10_bio->sectors);
4632 atomic_inc(&r10_bio->remaining);
4633 read_bio->bi_next = NULL;
4634 generic_make_request(read_bio);
4635 sectors_done += nr_sectors;
4636 if (sector_nr <= last)
4639 lower_barrier(conf);
4641 /* Now that we have done the whole section we can
4642 * update reshape_progress
4644 if (mddev->reshape_backwards)
4645 conf->reshape_progress -= sectors_done;
4647 conf->reshape_progress += sectors_done;
4649 return sectors_done;
4652 static void end_reshape_request(struct r10bio *r10_bio);
4653 static int handle_reshape_read_error(struct mddev *mddev,
4654 struct r10bio *r10_bio);
4655 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4657 /* Reshape read completed. Hopefully we have a block
4659 * If we got a read error then we do sync 1-page reads from
4660 * elsewhere until we find the data - or give up.
4662 struct r10conf *conf = mddev->private;
4665 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4666 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4667 /* Reshape has been aborted */
4668 md_done_sync(mddev, r10_bio->sectors, 0);
4672 /* We definitely have the data in the pages, schedule the
4675 atomic_set(&r10_bio->remaining, 1);
4676 for (s = 0; s < conf->copies*2; s++) {
4678 int d = r10_bio->devs[s/2].devnum;
4679 struct md_rdev *rdev;
4682 rdev = rcu_dereference(conf->mirrors[d].replacement);
4683 b = r10_bio->devs[s/2].repl_bio;
4685 rdev = rcu_dereference(conf->mirrors[d].rdev);
4686 b = r10_bio->devs[s/2].bio;
4688 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4692 atomic_inc(&rdev->nr_pending);
4694 md_sync_acct_bio(b, r10_bio->sectors);
4695 atomic_inc(&r10_bio->remaining);
4697 generic_make_request(b);
4699 end_reshape_request(r10_bio);
4702 static void end_reshape(struct r10conf *conf)
4704 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4707 spin_lock_irq(&conf->device_lock);
4708 conf->prev = conf->geo;
4709 md_finish_reshape(conf->mddev);
4711 conf->reshape_progress = MaxSector;
4712 conf->reshape_safe = MaxSector;
4713 spin_unlock_irq(&conf->device_lock);
4715 /* read-ahead size must cover two whole stripes, which is
4716 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4718 if (conf->mddev->queue) {
4719 int stripe = conf->geo.raid_disks *
4720 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4721 stripe /= conf->geo.near_copies;
4722 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4723 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
4728 static int handle_reshape_read_error(struct mddev *mddev,
4729 struct r10bio *r10_bio)
4731 /* Use sync reads to get the blocks from somewhere else */
4732 int sectors = r10_bio->sectors;
4733 struct r10conf *conf = mddev->private;
4734 struct r10bio *r10b;
4737 struct page **pages;
4739 r10b = kmalloc(sizeof(*r10b) +
4740 sizeof(struct r10dev) * conf->copies, GFP_NOIO);
4742 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4746 /* reshape IOs share pages from .devs[0].bio */
4747 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4749 r10b->sector = r10_bio->sector;
4750 __raid10_find_phys(&conf->prev, r10b);
4755 int first_slot = slot;
4757 if (s > (PAGE_SIZE >> 9))
4762 int d = r10b->devs[slot].devnum;
4763 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4766 test_bit(Faulty, &rdev->flags) ||
4767 !test_bit(In_sync, &rdev->flags))
4770 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4771 atomic_inc(&rdev->nr_pending);
4773 success = sync_page_io(rdev,
4777 REQ_OP_READ, 0, false);
4778 rdev_dec_pending(rdev, mddev);
4784 if (slot >= conf->copies)
4786 if (slot == first_slot)
4791 /* couldn't read this block, must give up */
4792 set_bit(MD_RECOVERY_INTR,
4804 static void end_reshape_write(struct bio *bio)
4806 struct r10bio *r10_bio = get_resync_r10bio(bio);
4807 struct mddev *mddev = r10_bio->mddev;
4808 struct r10conf *conf = mddev->private;
4812 struct md_rdev *rdev = NULL;
4814 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4816 rdev = conf->mirrors[d].replacement;
4819 rdev = conf->mirrors[d].rdev;
4822 if (bio->bi_status) {
4823 /* FIXME should record badblock */
4824 md_error(mddev, rdev);
4827 rdev_dec_pending(rdev, mddev);
4828 end_reshape_request(r10_bio);
4831 static void end_reshape_request(struct r10bio *r10_bio)
4833 if (!atomic_dec_and_test(&r10_bio->remaining))
4835 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4836 bio_put(r10_bio->master_bio);
4840 static void raid10_finish_reshape(struct mddev *mddev)
4842 struct r10conf *conf = mddev->private;
4844 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4847 if (mddev->delta_disks > 0) {
4848 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4849 mddev->recovery_cp = mddev->resync_max_sectors;
4850 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4852 mddev->resync_max_sectors = mddev->array_sectors;
4856 for (d = conf->geo.raid_disks ;
4857 d < conf->geo.raid_disks - mddev->delta_disks;
4859 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4861 clear_bit(In_sync, &rdev->flags);
4862 rdev = rcu_dereference(conf->mirrors[d].replacement);
4864 clear_bit(In_sync, &rdev->flags);
4868 mddev->layout = mddev->new_layout;
4869 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4870 mddev->reshape_position = MaxSector;
4871 mddev->delta_disks = 0;
4872 mddev->reshape_backwards = 0;
4875 static struct md_personality raid10_personality =
4879 .owner = THIS_MODULE,
4880 .make_request = raid10_make_request,
4882 .free = raid10_free,
4883 .status = raid10_status,
4884 .error_handler = raid10_error,
4885 .hot_add_disk = raid10_add_disk,
4886 .hot_remove_disk= raid10_remove_disk,
4887 .spare_active = raid10_spare_active,
4888 .sync_request = raid10_sync_request,
4889 .quiesce = raid10_quiesce,
4890 .size = raid10_size,
4891 .resize = raid10_resize,
4892 .takeover = raid10_takeover,
4893 .check_reshape = raid10_check_reshape,
4894 .start_reshape = raid10_start_reshape,
4895 .finish_reshape = raid10_finish_reshape,
4896 .congested = raid10_congested,
4899 static int __init raid_init(void)
4901 return register_md_personality(&raid10_personality);
4904 static void raid_exit(void)
4906 unregister_md_personality(&raid10_personality);
4909 module_init(raid_init);
4910 module_exit(raid_exit);
4911 MODULE_LICENSE("GPL");
4912 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4913 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4914 MODULE_ALIAS("md-raid10");
4915 MODULE_ALIAS("md-level-10");
4917 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);