1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * raid10.c : Multiple Devices driver for Linux
5 * Copyright (C) 2000-2004 Neil Brown
7 * RAID-10 support for md.
9 * Base on code in raid1.c. See raid1.c for further copyright information.
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/blkdev.h>
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
17 #include <linux/ratelimit.h>
18 #include <linux/kthread.h>
19 #include <linux/raid/md_p.h>
20 #include <trace/events/block.h>
24 #include "md-bitmap.h"
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
33 * far_offset (stored in bit 16 of layout )
34 * use_far_sets (stored in bit 17 of layout )
35 * use_far_sets_bugfixed (stored in bit 18 of layout )
37 * The data to be stored is divided into chunks using chunksize. Each device
38 * is divided into far_copies sections. In each section, chunks are laid out
39 * in a style similar to raid0, but near_copies copies of each chunk is stored
40 * (each on a different drive). The starting device for each section is offset
41 * near_copies from the starting device of the previous section. Thus there
42 * are (near_copies * far_copies) of each chunk, and each is on a different
43 * drive. near_copies and far_copies must be at least one, and their product
44 * is at most raid_disks.
46 * If far_offset is true, then the far_copies are handled a bit differently.
47 * The copies are still in different stripes, but instead of being very far
48 * apart on disk, there are adjacent stripes.
50 * The far and offset algorithms are handled slightly differently if
51 * 'use_far_sets' is true. In this case, the array's devices are grouped into
52 * sets that are (near_copies * far_copies) in size. The far copied stripes
53 * are still shifted by 'near_copies' devices, but this shifting stays confined
54 * to the set rather than the entire array. This is done to improve the number
55 * of device combinations that can fail without causing the array to fail.
56 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
61 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
62 * [A B] [C D] [A B] [C D E]
63 * |...| |...| |...| | ... |
64 * [B A] [D C] [B A] [E C D]
67 static void allow_barrier(struct r10conf *conf);
68 static void lower_barrier(struct r10conf *conf);
69 static int _enough(struct r10conf *conf, int previous, int ignore);
70 static int enough(struct r10conf *conf, int ignore);
71 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
73 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
74 static void end_reshape_write(struct bio *bio);
75 static void end_reshape(struct r10conf *conf);
77 #define raid10_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
83 #define cmd_before(conf, cmd) \
85 write_sequnlock_irq(&(conf)->resync_lock); \
88 #define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock)
90 #define wait_event_barrier_cmd(conf, cond, cmd) \
91 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \
94 #define wait_event_barrier(conf, cond) \
95 wait_event_barrier_cmd(conf, cond, NULL_CMD)
98 * for resync bio, r10bio pointer can be retrieved from the per-bio
99 * 'struct resync_pages'.
101 static inline struct r10bio *get_resync_r10bio(struct bio *bio)
103 return get_resync_pages(bio)->raid_bio;
106 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
108 struct r10conf *conf = data;
109 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
111 /* allocate a r10bio with room for raid_disks entries in the
113 return kzalloc(size, gfp_flags);
116 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
117 /* amount of memory to reserve for resync requests */
118 #define RESYNC_WINDOW (1024*1024)
119 /* maximum number of concurrent requests, memory permitting */
120 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
121 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
122 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
125 * When performing a resync, we need to read and compare, so
126 * we need as many pages are there are copies.
127 * When performing a recovery, we need 2 bios, one for read,
128 * one for write (we recover only one drive per r10buf)
131 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
133 struct r10conf *conf = data;
134 struct r10bio *r10_bio;
137 int nalloc, nalloc_rp;
138 struct resync_pages *rps;
140 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
144 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
145 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
146 nalloc = conf->copies; /* resync */
148 nalloc = 2; /* recovery */
150 /* allocate once for all bios */
151 if (!conf->have_replacement)
154 nalloc_rp = nalloc * 2;
155 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
157 goto out_free_r10bio;
162 for (j = nalloc ; j-- ; ) {
163 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
166 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
167 r10_bio->devs[j].bio = bio;
168 if (!conf->have_replacement)
170 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
173 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
174 r10_bio->devs[j].repl_bio = bio;
177 * Allocate RESYNC_PAGES data pages and attach them
180 for (j = 0; j < nalloc; j++) {
181 struct bio *rbio = r10_bio->devs[j].repl_bio;
182 struct resync_pages *rp, *rp_repl;
186 rp_repl = &rps[nalloc + j];
188 bio = r10_bio->devs[j].bio;
190 if (!j || test_bit(MD_RECOVERY_SYNC,
191 &conf->mddev->recovery)) {
192 if (resync_alloc_pages(rp, gfp_flags))
195 memcpy(rp, &rps[0], sizeof(*rp));
196 resync_get_all_pages(rp);
199 rp->raid_bio = r10_bio;
200 bio->bi_private = rp;
202 memcpy(rp_repl, rp, sizeof(*rp));
203 rbio->bi_private = rp_repl;
211 resync_free_pages(&rps[j]);
215 for ( ; j < nalloc; j++) {
216 if (r10_bio->devs[j].bio)
217 bio_uninit(r10_bio->devs[j].bio);
218 kfree(r10_bio->devs[j].bio);
219 if (r10_bio->devs[j].repl_bio)
220 bio_uninit(r10_bio->devs[j].repl_bio);
221 kfree(r10_bio->devs[j].repl_bio);
225 rbio_pool_free(r10_bio, conf);
229 static void r10buf_pool_free(void *__r10_bio, void *data)
231 struct r10conf *conf = data;
232 struct r10bio *r10bio = __r10_bio;
234 struct resync_pages *rp = NULL;
236 for (j = conf->copies; j--; ) {
237 struct bio *bio = r10bio->devs[j].bio;
240 rp = get_resync_pages(bio);
241 resync_free_pages(rp);
246 bio = r10bio->devs[j].repl_bio;
253 /* resync pages array stored in the 1st bio's .bi_private */
256 rbio_pool_free(r10bio, conf);
259 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
263 for (i = 0; i < conf->geo.raid_disks; i++) {
264 struct bio **bio = & r10_bio->devs[i].bio;
265 if (!BIO_SPECIAL(*bio))
268 bio = &r10_bio->devs[i].repl_bio;
269 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
275 static void free_r10bio(struct r10bio *r10_bio)
277 struct r10conf *conf = r10_bio->mddev->private;
279 put_all_bios(conf, r10_bio);
280 mempool_free(r10_bio, &conf->r10bio_pool);
283 static void put_buf(struct r10bio *r10_bio)
285 struct r10conf *conf = r10_bio->mddev->private;
287 mempool_free(r10_bio, &conf->r10buf_pool);
292 static void wake_up_barrier(struct r10conf *conf)
294 if (wq_has_sleeper(&conf->wait_barrier))
295 wake_up(&conf->wait_barrier);
298 static void reschedule_retry(struct r10bio *r10_bio)
301 struct mddev *mddev = r10_bio->mddev;
302 struct r10conf *conf = mddev->private;
304 spin_lock_irqsave(&conf->device_lock, flags);
305 list_add(&r10_bio->retry_list, &conf->retry_list);
307 spin_unlock_irqrestore(&conf->device_lock, flags);
309 /* wake up frozen array... */
310 wake_up(&conf->wait_barrier);
312 md_wakeup_thread(mddev->thread);
316 * raid_end_bio_io() is called when we have finished servicing a mirrored
317 * operation and are ready to return a success/failure code to the buffer
320 static void raid_end_bio_io(struct r10bio *r10_bio)
322 struct bio *bio = r10_bio->master_bio;
323 struct r10conf *conf = r10_bio->mddev->private;
325 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
326 bio->bi_status = BLK_STS_IOERR;
328 if (blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
329 bio_end_io_acct(bio, r10_bio->start_time);
332 * Wake up any possible resync thread that waits for the device
337 free_r10bio(r10_bio);
341 * Update disk head position estimator based on IRQ completion info.
343 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
345 struct r10conf *conf = r10_bio->mddev->private;
347 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
348 r10_bio->devs[slot].addr + (r10_bio->sectors);
352 * Find the disk number which triggered given bio
354 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
355 struct bio *bio, int *slotp, int *replp)
360 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
361 if (r10_bio->devs[slot].bio == bio)
363 if (r10_bio->devs[slot].repl_bio == bio) {
369 update_head_pos(slot, r10_bio);
375 return r10_bio->devs[slot].devnum;
378 static void raid10_end_read_request(struct bio *bio)
380 int uptodate = !bio->bi_status;
381 struct r10bio *r10_bio = bio->bi_private;
383 struct md_rdev *rdev;
384 struct r10conf *conf = r10_bio->mddev->private;
386 slot = r10_bio->read_slot;
387 rdev = r10_bio->devs[slot].rdev;
389 * this branch is our 'one mirror IO has finished' event handler:
391 update_head_pos(slot, r10_bio);
395 * Set R10BIO_Uptodate in our master bio, so that
396 * we will return a good error code to the higher
397 * levels even if IO on some other mirrored buffer fails.
399 * The 'master' represents the composite IO operation to
400 * user-side. So if something waits for IO, then it will
401 * wait for the 'master' bio.
403 set_bit(R10BIO_Uptodate, &r10_bio->state);
405 /* If all other devices that store this block have
406 * failed, we want to return the error upwards rather
407 * than fail the last device. Here we redefine
408 * "uptodate" to mean "Don't want to retry"
410 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
415 raid_end_bio_io(r10_bio);
416 rdev_dec_pending(rdev, conf->mddev);
419 * oops, read error - keep the refcount on the rdev
421 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n",
424 (unsigned long long)r10_bio->sector);
425 set_bit(R10BIO_ReadError, &r10_bio->state);
426 reschedule_retry(r10_bio);
430 static void close_write(struct r10bio *r10_bio)
432 /* clear the bitmap if all writes complete successfully */
433 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
435 !test_bit(R10BIO_Degraded, &r10_bio->state),
437 md_write_end(r10_bio->mddev);
440 static void one_write_done(struct r10bio *r10_bio)
442 if (atomic_dec_and_test(&r10_bio->remaining)) {
443 if (test_bit(R10BIO_WriteError, &r10_bio->state))
444 reschedule_retry(r10_bio);
446 close_write(r10_bio);
447 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
448 reschedule_retry(r10_bio);
450 raid_end_bio_io(r10_bio);
455 static void raid10_end_write_request(struct bio *bio)
457 struct r10bio *r10_bio = bio->bi_private;
460 struct r10conf *conf = r10_bio->mddev->private;
462 struct md_rdev *rdev = NULL;
463 struct bio *to_put = NULL;
466 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
468 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
471 rdev = conf->mirrors[dev].replacement;
475 rdev = conf->mirrors[dev].rdev;
478 * this branch is our 'one mirror IO has finished' event handler:
480 if (bio->bi_status && !discard_error) {
482 /* Never record new bad blocks to replacement,
485 md_error(rdev->mddev, rdev);
487 set_bit(WriteErrorSeen, &rdev->flags);
488 if (!test_and_set_bit(WantReplacement, &rdev->flags))
489 set_bit(MD_RECOVERY_NEEDED,
490 &rdev->mddev->recovery);
493 if (test_bit(FailFast, &rdev->flags) &&
494 (bio->bi_opf & MD_FAILFAST)) {
495 md_error(rdev->mddev, rdev);
499 * When the device is faulty, it is not necessary to
500 * handle write error.
502 if (!test_bit(Faulty, &rdev->flags))
503 set_bit(R10BIO_WriteError, &r10_bio->state);
505 /* Fail the request */
506 set_bit(R10BIO_Degraded, &r10_bio->state);
507 r10_bio->devs[slot].bio = NULL;
514 * Set R10BIO_Uptodate in our master bio, so that
515 * we will return a good error code for to the higher
516 * levels even if IO on some other mirrored buffer fails.
518 * The 'master' represents the composite IO operation to
519 * user-side. So if something waits for IO, then it will
520 * wait for the 'master' bio.
526 * Do not set R10BIO_Uptodate if the current device is
527 * rebuilding or Faulty. This is because we cannot use
528 * such device for properly reading the data back (we could
529 * potentially use it, if the current write would have felt
530 * before rdev->recovery_offset, but for simplicity we don't
533 if (test_bit(In_sync, &rdev->flags) &&
534 !test_bit(Faulty, &rdev->flags))
535 set_bit(R10BIO_Uptodate, &r10_bio->state);
537 /* Maybe we can clear some bad blocks. */
538 if (is_badblock(rdev,
539 r10_bio->devs[slot].addr,
541 &first_bad, &bad_sectors) && !discard_error) {
544 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
546 r10_bio->devs[slot].bio = IO_MADE_GOOD;
548 set_bit(R10BIO_MadeGood, &r10_bio->state);
554 * Let's see if all mirrored write operations have finished
557 one_write_done(r10_bio);
559 rdev_dec_pending(rdev, conf->mddev);
565 * RAID10 layout manager
566 * As well as the chunksize and raid_disks count, there are two
567 * parameters: near_copies and far_copies.
568 * near_copies * far_copies must be <= raid_disks.
569 * Normally one of these will be 1.
570 * If both are 1, we get raid0.
571 * If near_copies == raid_disks, we get raid1.
573 * Chunks are laid out in raid0 style with near_copies copies of the
574 * first chunk, followed by near_copies copies of the next chunk and
576 * If far_copies > 1, then after 1/far_copies of the array has been assigned
577 * as described above, we start again with a device offset of near_copies.
578 * So we effectively have another copy of the whole array further down all
579 * the drives, but with blocks on different drives.
580 * With this layout, and block is never stored twice on the one device.
582 * raid10_find_phys finds the sector offset of a given virtual sector
583 * on each device that it is on.
585 * raid10_find_virt does the reverse mapping, from a device and a
586 * sector offset to a virtual address
589 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
597 int last_far_set_start, last_far_set_size;
599 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
600 last_far_set_start *= geo->far_set_size;
602 last_far_set_size = geo->far_set_size;
603 last_far_set_size += (geo->raid_disks % geo->far_set_size);
605 /* now calculate first sector/dev */
606 chunk = r10bio->sector >> geo->chunk_shift;
607 sector = r10bio->sector & geo->chunk_mask;
609 chunk *= geo->near_copies;
611 dev = sector_div(stripe, geo->raid_disks);
613 stripe *= geo->far_copies;
615 sector += stripe << geo->chunk_shift;
617 /* and calculate all the others */
618 for (n = 0; n < geo->near_copies; n++) {
622 r10bio->devs[slot].devnum = d;
623 r10bio->devs[slot].addr = s;
626 for (f = 1; f < geo->far_copies; f++) {
627 set = d / geo->far_set_size;
628 d += geo->near_copies;
630 if ((geo->raid_disks % geo->far_set_size) &&
631 (d > last_far_set_start)) {
632 d -= last_far_set_start;
633 d %= last_far_set_size;
634 d += last_far_set_start;
636 d %= geo->far_set_size;
637 d += geo->far_set_size * set;
640 r10bio->devs[slot].devnum = d;
641 r10bio->devs[slot].addr = s;
645 if (dev >= geo->raid_disks) {
647 sector += (geo->chunk_mask + 1);
652 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
654 struct geom *geo = &conf->geo;
656 if (conf->reshape_progress != MaxSector &&
657 ((r10bio->sector >= conf->reshape_progress) !=
658 conf->mddev->reshape_backwards)) {
659 set_bit(R10BIO_Previous, &r10bio->state);
662 clear_bit(R10BIO_Previous, &r10bio->state);
664 __raid10_find_phys(geo, r10bio);
667 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
669 sector_t offset, chunk, vchunk;
670 /* Never use conf->prev as this is only called during resync
671 * or recovery, so reshape isn't happening
673 struct geom *geo = &conf->geo;
674 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
675 int far_set_size = geo->far_set_size;
676 int last_far_set_start;
678 if (geo->raid_disks % geo->far_set_size) {
679 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
680 last_far_set_start *= geo->far_set_size;
682 if (dev >= last_far_set_start) {
683 far_set_size = geo->far_set_size;
684 far_set_size += (geo->raid_disks % geo->far_set_size);
685 far_set_start = last_far_set_start;
689 offset = sector & geo->chunk_mask;
690 if (geo->far_offset) {
692 chunk = sector >> geo->chunk_shift;
693 fc = sector_div(chunk, geo->far_copies);
694 dev -= fc * geo->near_copies;
695 if (dev < far_set_start)
698 while (sector >= geo->stride) {
699 sector -= geo->stride;
700 if (dev < (geo->near_copies + far_set_start))
701 dev += far_set_size - geo->near_copies;
703 dev -= geo->near_copies;
705 chunk = sector >> geo->chunk_shift;
707 vchunk = chunk * geo->raid_disks + dev;
708 sector_div(vchunk, geo->near_copies);
709 return (vchunk << geo->chunk_shift) + offset;
713 * This routine returns the disk from which the requested read should
714 * be done. There is a per-array 'next expected sequential IO' sector
715 * number - if this matches on the next IO then we use the last disk.
716 * There is also a per-disk 'last know head position' sector that is
717 * maintained from IRQ contexts, both the normal and the resync IO
718 * completion handlers update this position correctly. If there is no
719 * perfect sequential match then we pick the disk whose head is closest.
721 * If there are 2 mirrors in the same 2 devices, performance degrades
722 * because position is mirror, not device based.
724 * The rdev for the device selected will have nr_pending incremented.
728 * FIXME: possibly should rethink readbalancing and do it differently
729 * depending on near_copies / far_copies geometry.
731 static struct md_rdev *read_balance(struct r10conf *conf,
732 struct r10bio *r10_bio,
735 const sector_t this_sector = r10_bio->sector;
737 int sectors = r10_bio->sectors;
738 int best_good_sectors;
739 sector_t new_distance, best_dist;
740 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
742 int best_dist_slot, best_pending_slot;
743 bool has_nonrot_disk = false;
744 unsigned int min_pending;
745 struct geom *geo = &conf->geo;
747 raid10_find_phys(conf, r10_bio);
750 min_pending = UINT_MAX;
751 best_dist_rdev = NULL;
752 best_pending_rdev = NULL;
753 best_dist = MaxSector;
754 best_good_sectors = 0;
756 clear_bit(R10BIO_FailFast, &r10_bio->state);
758 * Check if we can balance. We can balance on the whole
759 * device if no resync is going on (recovery is ok), or below
760 * the resync window. We take the first readable disk when
761 * above the resync window.
763 if ((conf->mddev->recovery_cp < MaxSector
764 && (this_sector + sectors >= conf->next_resync)) ||
765 (mddev_is_clustered(conf->mddev) &&
766 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
767 this_sector + sectors)))
770 for (slot = 0; slot < conf->copies ; slot++) {
774 unsigned int pending;
777 if (r10_bio->devs[slot].bio == IO_BLOCKED)
779 disk = r10_bio->devs[slot].devnum;
780 rdev = rcu_dereference(conf->mirrors[disk].replacement);
781 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
782 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
783 rdev = rcu_dereference(conf->mirrors[disk].rdev);
785 test_bit(Faulty, &rdev->flags))
787 if (!test_bit(In_sync, &rdev->flags) &&
788 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
791 dev_sector = r10_bio->devs[slot].addr;
792 if (is_badblock(rdev, dev_sector, sectors,
793 &first_bad, &bad_sectors)) {
794 if (best_dist < MaxSector)
795 /* Already have a better slot */
797 if (first_bad <= dev_sector) {
798 /* Cannot read here. If this is the
799 * 'primary' device, then we must not read
800 * beyond 'bad_sectors' from another device.
802 bad_sectors -= (dev_sector - first_bad);
803 if (!do_balance && sectors > bad_sectors)
804 sectors = bad_sectors;
805 if (best_good_sectors > sectors)
806 best_good_sectors = sectors;
808 sector_t good_sectors =
809 first_bad - dev_sector;
810 if (good_sectors > best_good_sectors) {
811 best_good_sectors = good_sectors;
812 best_dist_slot = slot;
813 best_dist_rdev = rdev;
816 /* Must read from here */
821 best_good_sectors = sectors;
826 nonrot = bdev_nonrot(rdev->bdev);
827 has_nonrot_disk |= nonrot;
828 pending = atomic_read(&rdev->nr_pending);
829 if (min_pending > pending && nonrot) {
830 min_pending = pending;
831 best_pending_slot = slot;
832 best_pending_rdev = rdev;
835 if (best_dist_slot >= 0)
836 /* At least 2 disks to choose from so failfast is OK */
837 set_bit(R10BIO_FailFast, &r10_bio->state);
838 /* This optimisation is debatable, and completely destroys
839 * sequential read speed for 'far copies' arrays. So only
840 * keep it for 'near' arrays, and review those later.
842 if (geo->near_copies > 1 && !pending)
845 /* for far > 1 always use the lowest address */
846 else if (geo->far_copies > 1)
847 new_distance = r10_bio->devs[slot].addr;
849 new_distance = abs(r10_bio->devs[slot].addr -
850 conf->mirrors[disk].head_position);
852 if (new_distance < best_dist) {
853 best_dist = new_distance;
854 best_dist_slot = slot;
855 best_dist_rdev = rdev;
858 if (slot >= conf->copies) {
859 if (has_nonrot_disk) {
860 slot = best_pending_slot;
861 rdev = best_pending_rdev;
863 slot = best_dist_slot;
864 rdev = best_dist_rdev;
869 atomic_inc(&rdev->nr_pending);
870 r10_bio->read_slot = slot;
874 *max_sectors = best_good_sectors;
879 static void flush_pending_writes(struct r10conf *conf)
881 /* Any writes that have been queued but are awaiting
882 * bitmap updates get flushed here.
884 spin_lock_irq(&conf->device_lock);
886 if (conf->pending_bio_list.head) {
887 struct blk_plug plug;
890 bio = bio_list_get(&conf->pending_bio_list);
891 spin_unlock_irq(&conf->device_lock);
894 * As this is called in a wait_event() loop (see freeze_array),
895 * current->state might be TASK_UNINTERRUPTIBLE which will
896 * cause a warning when we prepare to wait again. As it is
897 * rare that this path is taken, it is perfectly safe to force
898 * us to go around the wait_event() loop again, so the warning
899 * is a false-positive. Silence the warning by resetting
902 __set_current_state(TASK_RUNNING);
904 blk_start_plug(&plug);
905 /* flush any pending bitmap writes to disk
906 * before proceeding w/ I/O */
907 md_bitmap_unplug(conf->mddev->bitmap);
908 wake_up(&conf->wait_barrier);
910 while (bio) { /* submit pending writes */
911 struct bio *next = bio->bi_next;
912 struct md_rdev *rdev = (void*)bio->bi_bdev;
914 bio_set_dev(bio, rdev->bdev);
915 if (test_bit(Faulty, &rdev->flags)) {
917 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
918 !bdev_max_discard_sectors(bio->bi_bdev)))
922 submit_bio_noacct(bio);
925 blk_finish_plug(&plug);
927 spin_unlock_irq(&conf->device_lock);
931 * Sometimes we need to suspend IO while we do something else,
932 * either some resync/recovery, or reconfigure the array.
933 * To do this we raise a 'barrier'.
934 * The 'barrier' is a counter that can be raised multiple times
935 * to count how many activities are happening which preclude
937 * We can only raise the barrier if there is no pending IO.
938 * i.e. if nr_pending == 0.
939 * We choose only to raise the barrier if no-one is waiting for the
940 * barrier to go down. This means that as soon as an IO request
941 * is ready, no other operations which require a barrier will start
942 * until the IO request has had a chance.
944 * So: regular IO calls 'wait_barrier'. When that returns there
945 * is no backgroup IO happening, It must arrange to call
946 * allow_barrier when it has finished its IO.
947 * backgroup IO calls must call raise_barrier. Once that returns
948 * there is no normal IO happeing. It must arrange to call
949 * lower_barrier when the particular background IO completes.
952 static void raise_barrier(struct r10conf *conf, int force)
954 write_seqlock_irq(&conf->resync_lock);
956 if (WARN_ON_ONCE(force && !conf->barrier))
959 /* Wait until no block IO is waiting (unless 'force') */
960 wait_event_barrier(conf, force || !conf->nr_waiting);
962 /* block any new IO from starting */
963 WRITE_ONCE(conf->barrier, conf->barrier + 1);
965 /* Now wait for all pending IO to complete */
966 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) &&
967 conf->barrier < RESYNC_DEPTH);
969 write_sequnlock_irq(&conf->resync_lock);
972 static void lower_barrier(struct r10conf *conf)
976 write_seqlock_irqsave(&conf->resync_lock, flags);
977 WRITE_ONCE(conf->barrier, conf->barrier - 1);
978 write_sequnlock_irqrestore(&conf->resync_lock, flags);
979 wake_up(&conf->wait_barrier);
982 static bool stop_waiting_barrier(struct r10conf *conf)
984 struct bio_list *bio_list = current->bio_list;
986 /* barrier is dropped */
991 * If there are already pending requests (preventing the barrier from
992 * rising completely), and the pre-process bio queue isn't empty, then
993 * don't wait, as we need to empty that queue to get the nr_pending
996 if (atomic_read(&conf->nr_pending) && bio_list &&
997 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1])))
1001 * move on if io is issued from raid10d(), nr_pending is not released
1002 * from original io(see handle_read_error()). All raise barrier is
1003 * blocked until this io is done.
1005 if (conf->mddev->thread->tsk == current) {
1006 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0);
1013 static bool wait_barrier_nolock(struct r10conf *conf)
1015 unsigned int seq = read_seqbegin(&conf->resync_lock);
1017 if (READ_ONCE(conf->barrier))
1020 atomic_inc(&conf->nr_pending);
1021 if (!read_seqretry(&conf->resync_lock, seq))
1024 if (atomic_dec_and_test(&conf->nr_pending))
1025 wake_up_barrier(conf);
1030 static bool wait_barrier(struct r10conf *conf, bool nowait)
1034 if (wait_barrier_nolock(conf))
1037 write_seqlock_irq(&conf->resync_lock);
1038 if (conf->barrier) {
1039 /* Return false when nowait flag is set */
1044 raid10_log(conf->mddev, "wait barrier");
1045 wait_event_barrier(conf, stop_waiting_barrier(conf));
1048 if (!conf->nr_waiting)
1049 wake_up(&conf->wait_barrier);
1051 /* Only increment nr_pending when we wait */
1053 atomic_inc(&conf->nr_pending);
1054 write_sequnlock_irq(&conf->resync_lock);
1058 static void allow_barrier(struct r10conf *conf)
1060 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1061 (conf->array_freeze_pending))
1062 wake_up_barrier(conf);
1065 static void freeze_array(struct r10conf *conf, int extra)
1067 /* stop syncio and normal IO and wait for everything to
1069 * We increment barrier and nr_waiting, and then
1070 * wait until nr_pending match nr_queued+extra
1071 * This is called in the context of one normal IO request
1072 * that has failed. Thus any sync request that might be pending
1073 * will be blocked by nr_pending, and we need to wait for
1074 * pending IO requests to complete or be queued for re-try.
1075 * Thus the number queued (nr_queued) plus this request (extra)
1076 * must match the number of pending IOs (nr_pending) before
1079 write_seqlock_irq(&conf->resync_lock);
1080 conf->array_freeze_pending++;
1081 WRITE_ONCE(conf->barrier, conf->barrier + 1);
1083 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) ==
1084 conf->nr_queued + extra, flush_pending_writes(conf));
1085 conf->array_freeze_pending--;
1086 write_sequnlock_irq(&conf->resync_lock);
1089 static void unfreeze_array(struct r10conf *conf)
1091 /* reverse the effect of the freeze */
1092 write_seqlock_irq(&conf->resync_lock);
1093 WRITE_ONCE(conf->barrier, conf->barrier - 1);
1095 wake_up(&conf->wait_barrier);
1096 write_sequnlock_irq(&conf->resync_lock);
1099 static sector_t choose_data_offset(struct r10bio *r10_bio,
1100 struct md_rdev *rdev)
1102 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1103 test_bit(R10BIO_Previous, &r10_bio->state))
1104 return rdev->data_offset;
1106 return rdev->new_data_offset;
1109 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1111 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb);
1112 struct mddev *mddev = plug->cb.data;
1113 struct r10conf *conf = mddev->private;
1116 if (from_schedule || current->bio_list) {
1117 spin_lock_irq(&conf->device_lock);
1118 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1119 spin_unlock_irq(&conf->device_lock);
1120 wake_up(&conf->wait_barrier);
1121 md_wakeup_thread(mddev->thread);
1126 /* we aren't scheduling, so we can do the write-out directly. */
1127 bio = bio_list_get(&plug->pending);
1128 md_bitmap_unplug(mddev->bitmap);
1129 wake_up(&conf->wait_barrier);
1131 while (bio) { /* submit pending writes */
1132 struct bio *next = bio->bi_next;
1133 struct md_rdev *rdev = (void*)bio->bi_bdev;
1134 bio->bi_next = NULL;
1135 bio_set_dev(bio, rdev->bdev);
1136 if (test_bit(Faulty, &rdev->flags)) {
1138 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1139 !bdev_max_discard_sectors(bio->bi_bdev)))
1140 /* Just ignore it */
1143 submit_bio_noacct(bio);
1150 * 1. Register the new request and wait if the reconstruction thread has put
1151 * up a bar for new requests. Continue immediately if no resync is active
1153 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1155 static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf,
1156 struct bio *bio, sector_t sectors)
1158 /* Bail out if REQ_NOWAIT is set for the bio */
1159 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) {
1160 bio_wouldblock_error(bio);
1163 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1164 bio->bi_iter.bi_sector < conf->reshape_progress &&
1165 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1166 allow_barrier(conf);
1167 if (bio->bi_opf & REQ_NOWAIT) {
1168 bio_wouldblock_error(bio);
1171 raid10_log(conf->mddev, "wait reshape");
1172 wait_event(conf->wait_barrier,
1173 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1174 conf->reshape_progress >= bio->bi_iter.bi_sector +
1176 wait_barrier(conf, false);
1181 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1182 struct r10bio *r10_bio)
1184 struct r10conf *conf = mddev->private;
1185 struct bio *read_bio;
1186 const enum req_op op = bio_op(bio);
1187 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1189 struct md_rdev *rdev;
1190 char b[BDEVNAME_SIZE];
1191 int slot = r10_bio->read_slot;
1192 struct md_rdev *err_rdev = NULL;
1193 gfp_t gfp = GFP_NOIO;
1195 if (slot >= 0 && r10_bio->devs[slot].rdev) {
1197 * This is an error retry, but we cannot
1198 * safely dereference the rdev in the r10_bio,
1199 * we must use the one in conf.
1200 * If it has already been disconnected (unlikely)
1201 * we lose the device name in error messages.
1205 * As we are blocking raid10, it is a little safer to
1208 gfp = GFP_NOIO | __GFP_HIGH;
1211 disk = r10_bio->devs[slot].devnum;
1212 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1214 snprintf(b, sizeof(b), "%pg", err_rdev->bdev);
1217 /* This never gets dereferenced */
1218 err_rdev = r10_bio->devs[slot].rdev;
1223 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors))
1225 rdev = read_balance(conf, r10_bio, &max_sectors);
1228 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1230 (unsigned long long)r10_bio->sector);
1232 raid_end_bio_io(r10_bio);
1236 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
1239 (unsigned long long)r10_bio->sector);
1240 if (max_sectors < bio_sectors(bio)) {
1241 struct bio *split = bio_split(bio, max_sectors,
1242 gfp, &conf->bio_split);
1243 bio_chain(split, bio);
1244 allow_barrier(conf);
1245 submit_bio_noacct(bio);
1246 wait_barrier(conf, false);
1248 r10_bio->master_bio = bio;
1249 r10_bio->sectors = max_sectors;
1251 slot = r10_bio->read_slot;
1253 if (!r10_bio->start_time &&
1254 blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
1255 r10_bio->start_time = bio_start_io_acct(bio);
1256 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set);
1258 r10_bio->devs[slot].bio = read_bio;
1259 r10_bio->devs[slot].rdev = rdev;
1261 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1262 choose_data_offset(r10_bio, rdev);
1263 read_bio->bi_end_io = raid10_end_read_request;
1264 read_bio->bi_opf = op | do_sync;
1265 if (test_bit(FailFast, &rdev->flags) &&
1266 test_bit(R10BIO_FailFast, &r10_bio->state))
1267 read_bio->bi_opf |= MD_FAILFAST;
1268 read_bio->bi_private = r10_bio;
1271 trace_block_bio_remap(read_bio, disk_devt(mddev->gendisk),
1273 submit_bio_noacct(read_bio);
1277 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1278 struct bio *bio, bool replacement,
1281 const enum req_op op = bio_op(bio);
1282 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1283 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA;
1284 unsigned long flags;
1285 struct blk_plug_cb *cb;
1286 struct raid1_plug_cb *plug = NULL;
1287 struct r10conf *conf = mddev->private;
1288 struct md_rdev *rdev;
1289 int devnum = r10_bio->devs[n_copy].devnum;
1293 rdev = conf->mirrors[devnum].replacement;
1295 /* Replacement just got moved to main 'rdev' */
1297 rdev = conf->mirrors[devnum].rdev;
1300 rdev = conf->mirrors[devnum].rdev;
1302 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set);
1304 r10_bio->devs[n_copy].repl_bio = mbio;
1306 r10_bio->devs[n_copy].bio = mbio;
1308 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1309 choose_data_offset(r10_bio, rdev));
1310 mbio->bi_end_io = raid10_end_write_request;
1311 mbio->bi_opf = op | do_sync | do_fua;
1312 if (!replacement && test_bit(FailFast,
1313 &conf->mirrors[devnum].rdev->flags)
1314 && enough(conf, devnum))
1315 mbio->bi_opf |= MD_FAILFAST;
1316 mbio->bi_private = r10_bio;
1318 if (conf->mddev->gendisk)
1319 trace_block_bio_remap(mbio, disk_devt(conf->mddev->gendisk),
1321 /* flush_pending_writes() needs access to the rdev so...*/
1322 mbio->bi_bdev = (void *)rdev;
1324 atomic_inc(&r10_bio->remaining);
1326 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1328 plug = container_of(cb, struct raid1_plug_cb, cb);
1332 bio_list_add(&plug->pending, mbio);
1334 spin_lock_irqsave(&conf->device_lock, flags);
1335 bio_list_add(&conf->pending_bio_list, mbio);
1336 spin_unlock_irqrestore(&conf->device_lock, flags);
1337 md_wakeup_thread(mddev->thread);
1341 static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1344 struct r10conf *conf = mddev->private;
1345 struct md_rdev *blocked_rdev;
1348 blocked_rdev = NULL;
1350 for (i = 0; i < conf->copies; i++) {
1351 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1352 struct md_rdev *rrdev = rcu_dereference(
1353 conf->mirrors[i].replacement);
1356 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1357 atomic_inc(&rdev->nr_pending);
1358 blocked_rdev = rdev;
1361 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1362 atomic_inc(&rrdev->nr_pending);
1363 blocked_rdev = rrdev;
1367 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1369 sector_t dev_sector = r10_bio->devs[i].addr;
1374 * Discard request doesn't care the write result
1375 * so it doesn't need to wait blocked disk here.
1377 if (!r10_bio->sectors)
1380 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1381 &first_bad, &bad_sectors);
1384 * Mustn't write here until the bad block
1387 atomic_inc(&rdev->nr_pending);
1388 set_bit(BlockedBadBlocks, &rdev->flags);
1389 blocked_rdev = rdev;
1396 if (unlikely(blocked_rdev)) {
1397 /* Have to wait for this device to get unblocked, then retry */
1398 allow_barrier(conf);
1399 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1400 __func__, blocked_rdev->raid_disk);
1401 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1402 wait_barrier(conf, false);
1407 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1408 struct r10bio *r10_bio)
1410 struct r10conf *conf = mddev->private;
1415 if ((mddev_is_clustered(mddev) &&
1416 md_cluster_ops->area_resyncing(mddev, WRITE,
1417 bio->bi_iter.bi_sector,
1418 bio_end_sector(bio)))) {
1420 /* Bail out if REQ_NOWAIT is set for the bio */
1421 if (bio->bi_opf & REQ_NOWAIT) {
1422 bio_wouldblock_error(bio);
1426 prepare_to_wait(&conf->wait_barrier,
1428 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1429 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1433 finish_wait(&conf->wait_barrier, &w);
1436 sectors = r10_bio->sectors;
1437 if (!regular_request_wait(mddev, conf, bio, sectors))
1439 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1440 (mddev->reshape_backwards
1441 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1442 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1443 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1444 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1445 /* Need to update reshape_position in metadata */
1446 mddev->reshape_position = conf->reshape_progress;
1447 set_mask_bits(&mddev->sb_flags, 0,
1448 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1449 md_wakeup_thread(mddev->thread);
1450 if (bio->bi_opf & REQ_NOWAIT) {
1451 allow_barrier(conf);
1452 bio_wouldblock_error(bio);
1455 raid10_log(conf->mddev, "wait reshape metadata");
1456 wait_event(mddev->sb_wait,
1457 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1459 conf->reshape_safe = mddev->reshape_position;
1462 /* first select target devices under rcu_lock and
1463 * inc refcount on their rdev. Record them by setting
1465 * If there are known/acknowledged bad blocks on any device
1466 * on which we have seen a write error, we want to avoid
1467 * writing to those blocks. This potentially requires several
1468 * writes to write around the bad blocks. Each set of writes
1469 * gets its own r10_bio with a set of bios attached.
1472 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1473 raid10_find_phys(conf, r10_bio);
1475 wait_blocked_dev(mddev, r10_bio);
1478 max_sectors = r10_bio->sectors;
1480 for (i = 0; i < conf->copies; i++) {
1481 int d = r10_bio->devs[i].devnum;
1482 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1483 struct md_rdev *rrdev = rcu_dereference(
1484 conf->mirrors[d].replacement);
1487 if (rdev && (test_bit(Faulty, &rdev->flags)))
1489 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1492 r10_bio->devs[i].bio = NULL;
1493 r10_bio->devs[i].repl_bio = NULL;
1495 if (!rdev && !rrdev) {
1496 set_bit(R10BIO_Degraded, &r10_bio->state);
1499 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1501 sector_t dev_sector = r10_bio->devs[i].addr;
1505 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1506 &first_bad, &bad_sectors);
1507 if (is_bad && first_bad <= dev_sector) {
1508 /* Cannot write here at all */
1509 bad_sectors -= (dev_sector - first_bad);
1510 if (bad_sectors < max_sectors)
1511 /* Mustn't write more than bad_sectors
1512 * to other devices yet
1514 max_sectors = bad_sectors;
1515 /* We don't set R10BIO_Degraded as that
1516 * only applies if the disk is missing,
1517 * so it might be re-added, and we want to
1518 * know to recover this chunk.
1519 * In this case the device is here, and the
1520 * fact that this chunk is not in-sync is
1521 * recorded in the bad block log.
1526 int good_sectors = first_bad - dev_sector;
1527 if (good_sectors < max_sectors)
1528 max_sectors = good_sectors;
1532 r10_bio->devs[i].bio = bio;
1533 atomic_inc(&rdev->nr_pending);
1536 r10_bio->devs[i].repl_bio = bio;
1537 atomic_inc(&rrdev->nr_pending);
1542 if (max_sectors < r10_bio->sectors)
1543 r10_bio->sectors = max_sectors;
1545 if (r10_bio->sectors < bio_sectors(bio)) {
1546 struct bio *split = bio_split(bio, r10_bio->sectors,
1547 GFP_NOIO, &conf->bio_split);
1548 bio_chain(split, bio);
1549 allow_barrier(conf);
1550 submit_bio_noacct(bio);
1551 wait_barrier(conf, false);
1553 r10_bio->master_bio = bio;
1556 if (blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
1557 r10_bio->start_time = bio_start_io_acct(bio);
1558 atomic_set(&r10_bio->remaining, 1);
1559 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1561 for (i = 0; i < conf->copies; i++) {
1562 if (r10_bio->devs[i].bio)
1563 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1564 if (r10_bio->devs[i].repl_bio)
1565 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1567 one_write_done(r10_bio);
1570 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1572 struct r10conf *conf = mddev->private;
1573 struct r10bio *r10_bio;
1575 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1577 r10_bio->master_bio = bio;
1578 r10_bio->sectors = sectors;
1580 r10_bio->mddev = mddev;
1581 r10_bio->sector = bio->bi_iter.bi_sector;
1583 r10_bio->read_slot = -1;
1584 r10_bio->start_time = 0;
1585 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1586 conf->geo.raid_disks);
1588 if (bio_data_dir(bio) == READ)
1589 raid10_read_request(mddev, bio, r10_bio);
1591 raid10_write_request(mddev, bio, r10_bio);
1594 static void raid_end_discard_bio(struct r10bio *r10bio)
1596 struct r10conf *conf = r10bio->mddev->private;
1597 struct r10bio *first_r10bio;
1599 while (atomic_dec_and_test(&r10bio->remaining)) {
1601 allow_barrier(conf);
1603 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1604 first_r10bio = (struct r10bio *)r10bio->master_bio;
1605 free_r10bio(r10bio);
1606 r10bio = first_r10bio;
1608 md_write_end(r10bio->mddev);
1609 bio_endio(r10bio->master_bio);
1610 free_r10bio(r10bio);
1616 static void raid10_end_discard_request(struct bio *bio)
1618 struct r10bio *r10_bio = bio->bi_private;
1619 struct r10conf *conf = r10_bio->mddev->private;
1620 struct md_rdev *rdev = NULL;
1625 * We don't care the return value of discard bio
1627 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1628 set_bit(R10BIO_Uptodate, &r10_bio->state);
1630 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1632 rdev = conf->mirrors[dev].replacement;
1635 * raid10_remove_disk uses smp_mb to make sure rdev is set to
1636 * replacement before setting replacement to NULL. It can read
1637 * rdev first without barrier protect even replacement is NULL
1640 rdev = conf->mirrors[dev].rdev;
1643 raid_end_discard_bio(r10_bio);
1644 rdev_dec_pending(rdev, conf->mddev);
1648 * There are some limitations to handle discard bio
1649 * 1st, the discard size is bigger than stripe_size*2.
1650 * 2st, if the discard bio spans reshape progress, we use the old way to
1651 * handle discard bio
1653 static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1655 struct r10conf *conf = mddev->private;
1656 struct geom *geo = &conf->geo;
1657 int far_copies = geo->far_copies;
1658 bool first_copy = true;
1659 struct r10bio *r10_bio, *first_r10bio;
1663 unsigned int stripe_size;
1664 unsigned int stripe_data_disks;
1665 sector_t split_size;
1666 sector_t bio_start, bio_end;
1667 sector_t first_stripe_index, last_stripe_index;
1668 sector_t start_disk_offset;
1669 unsigned int start_disk_index;
1670 sector_t end_disk_offset;
1671 unsigned int end_disk_index;
1672 unsigned int remainder;
1674 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1677 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) {
1678 bio_wouldblock_error(bio);
1681 wait_barrier(conf, false);
1684 * Check reshape again to avoid reshape happens after checking
1685 * MD_RECOVERY_RESHAPE and before wait_barrier
1687 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1690 if (geo->near_copies)
1691 stripe_data_disks = geo->raid_disks / geo->near_copies +
1692 geo->raid_disks % geo->near_copies;
1694 stripe_data_disks = geo->raid_disks;
1696 stripe_size = stripe_data_disks << geo->chunk_shift;
1698 bio_start = bio->bi_iter.bi_sector;
1699 bio_end = bio_end_sector(bio);
1702 * Maybe one discard bio is smaller than strip size or across one
1703 * stripe and discard region is larger than one stripe size. For far
1704 * offset layout, if the discard region is not aligned with stripe
1705 * size, there is hole when we submit discard bio to member disk.
1706 * For simplicity, we only handle discard bio which discard region
1707 * is bigger than stripe_size * 2
1709 if (bio_sectors(bio) < stripe_size*2)
1713 * Keep bio aligned with strip size.
1715 div_u64_rem(bio_start, stripe_size, &remainder);
1717 split_size = stripe_size - remainder;
1718 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1719 bio_chain(split, bio);
1720 allow_barrier(conf);
1721 /* Resend the fist split part */
1722 submit_bio_noacct(split);
1723 wait_barrier(conf, false);
1725 div_u64_rem(bio_end, stripe_size, &remainder);
1727 split_size = bio_sectors(bio) - remainder;
1728 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1729 bio_chain(split, bio);
1730 allow_barrier(conf);
1731 /* Resend the second split part */
1732 submit_bio_noacct(bio);
1734 wait_barrier(conf, false);
1737 bio_start = bio->bi_iter.bi_sector;
1738 bio_end = bio_end_sector(bio);
1741 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1742 * One stripe contains the chunks from all member disk (one chunk from
1743 * one disk at the same HBA address). For layout detail, see 'man md 4'
1745 chunk = bio_start >> geo->chunk_shift;
1746 chunk *= geo->near_copies;
1747 first_stripe_index = chunk;
1748 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1749 if (geo->far_offset)
1750 first_stripe_index *= geo->far_copies;
1751 start_disk_offset = (bio_start & geo->chunk_mask) +
1752 (first_stripe_index << geo->chunk_shift);
1754 chunk = bio_end >> geo->chunk_shift;
1755 chunk *= geo->near_copies;
1756 last_stripe_index = chunk;
1757 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1758 if (geo->far_offset)
1759 last_stripe_index *= geo->far_copies;
1760 end_disk_offset = (bio_end & geo->chunk_mask) +
1761 (last_stripe_index << geo->chunk_shift);
1764 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1765 r10_bio->mddev = mddev;
1767 r10_bio->sectors = 0;
1768 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1769 wait_blocked_dev(mddev, r10_bio);
1772 * For far layout it needs more than one r10bio to cover all regions.
1773 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1774 * to record the discard bio. Other r10bio->master_bio record the first
1775 * r10bio. The first r10bio only release after all other r10bios finish.
1776 * The discard bio returns only first r10bio finishes
1779 r10_bio->master_bio = bio;
1780 set_bit(R10BIO_Discard, &r10_bio->state);
1782 first_r10bio = r10_bio;
1784 r10_bio->master_bio = (struct bio *)first_r10bio;
1787 * first select target devices under rcu_lock and
1788 * inc refcount on their rdev. Record them by setting
1792 for (disk = 0; disk < geo->raid_disks; disk++) {
1793 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1794 struct md_rdev *rrdev = rcu_dereference(
1795 conf->mirrors[disk].replacement);
1797 r10_bio->devs[disk].bio = NULL;
1798 r10_bio->devs[disk].repl_bio = NULL;
1800 if (rdev && (test_bit(Faulty, &rdev->flags)))
1802 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1804 if (!rdev && !rrdev)
1808 r10_bio->devs[disk].bio = bio;
1809 atomic_inc(&rdev->nr_pending);
1812 r10_bio->devs[disk].repl_bio = bio;
1813 atomic_inc(&rrdev->nr_pending);
1818 atomic_set(&r10_bio->remaining, 1);
1819 for (disk = 0; disk < geo->raid_disks; disk++) {
1820 sector_t dev_start, dev_end;
1821 struct bio *mbio, *rbio = NULL;
1824 * Now start to calculate the start and end address for each disk.
1825 * The space between dev_start and dev_end is the discard region.
1827 * For dev_start, it needs to consider three conditions:
1828 * 1st, the disk is before start_disk, you can imagine the disk in
1829 * the next stripe. So the dev_start is the start address of next
1831 * 2st, the disk is after start_disk, it means the disk is at the
1832 * same stripe of first disk
1833 * 3st, the first disk itself, we can use start_disk_offset directly
1835 if (disk < start_disk_index)
1836 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1837 else if (disk > start_disk_index)
1838 dev_start = first_stripe_index * mddev->chunk_sectors;
1840 dev_start = start_disk_offset;
1842 if (disk < end_disk_index)
1843 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1844 else if (disk > end_disk_index)
1845 dev_end = last_stripe_index * mddev->chunk_sectors;
1847 dev_end = end_disk_offset;
1850 * It only handles discard bio which size is >= stripe size, so
1851 * dev_end > dev_start all the time.
1852 * It doesn't need to use rcu lock to get rdev here. We already
1853 * add rdev->nr_pending in the first loop.
1855 if (r10_bio->devs[disk].bio) {
1856 struct md_rdev *rdev = conf->mirrors[disk].rdev;
1857 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1859 mbio->bi_end_io = raid10_end_discard_request;
1860 mbio->bi_private = r10_bio;
1861 r10_bio->devs[disk].bio = mbio;
1862 r10_bio->devs[disk].devnum = disk;
1863 atomic_inc(&r10_bio->remaining);
1864 md_submit_discard_bio(mddev, rdev, mbio,
1865 dev_start + choose_data_offset(r10_bio, rdev),
1866 dev_end - dev_start);
1869 if (r10_bio->devs[disk].repl_bio) {
1870 struct md_rdev *rrdev = conf->mirrors[disk].replacement;
1871 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1873 rbio->bi_end_io = raid10_end_discard_request;
1874 rbio->bi_private = r10_bio;
1875 r10_bio->devs[disk].repl_bio = rbio;
1876 r10_bio->devs[disk].devnum = disk;
1877 atomic_inc(&r10_bio->remaining);
1878 md_submit_discard_bio(mddev, rrdev, rbio,
1879 dev_start + choose_data_offset(r10_bio, rrdev),
1880 dev_end - dev_start);
1885 if (!geo->far_offset && --far_copies) {
1886 first_stripe_index += geo->stride >> geo->chunk_shift;
1887 start_disk_offset += geo->stride;
1888 last_stripe_index += geo->stride >> geo->chunk_shift;
1889 end_disk_offset += geo->stride;
1890 atomic_inc(&first_r10bio->remaining);
1891 raid_end_discard_bio(r10_bio);
1892 wait_barrier(conf, false);
1896 raid_end_discard_bio(r10_bio);
1900 allow_barrier(conf);
1904 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1906 struct r10conf *conf = mddev->private;
1907 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1908 int chunk_sects = chunk_mask + 1;
1909 int sectors = bio_sectors(bio);
1911 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1912 && md_flush_request(mddev, bio))
1915 if (!md_write_start(mddev, bio))
1918 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1919 if (!raid10_handle_discard(mddev, bio))
1923 * If this request crosses a chunk boundary, we need to split
1926 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1927 sectors > chunk_sects
1928 && (conf->geo.near_copies < conf->geo.raid_disks
1929 || conf->prev.near_copies <
1930 conf->prev.raid_disks)))
1931 sectors = chunk_sects -
1932 (bio->bi_iter.bi_sector &
1934 __make_request(mddev, bio, sectors);
1936 /* In case raid10d snuck in to freeze_array */
1937 wake_up_barrier(conf);
1941 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1943 struct r10conf *conf = mddev->private;
1946 if (conf->geo.near_copies < conf->geo.raid_disks)
1947 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1948 if (conf->geo.near_copies > 1)
1949 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1950 if (conf->geo.far_copies > 1) {
1951 if (conf->geo.far_offset)
1952 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1954 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1955 if (conf->geo.far_set_size != conf->geo.raid_disks)
1956 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1958 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1959 conf->geo.raid_disks - mddev->degraded);
1961 for (i = 0; i < conf->geo.raid_disks; i++) {
1962 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1963 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1966 seq_printf(seq, "]");
1969 /* check if there are enough drives for
1970 * every block to appear on atleast one.
1971 * Don't consider the device numbered 'ignore'
1972 * as we might be about to remove it.
1974 static int _enough(struct r10conf *conf, int previous, int ignore)
1980 disks = conf->prev.raid_disks;
1981 ncopies = conf->prev.near_copies;
1983 disks = conf->geo.raid_disks;
1984 ncopies = conf->geo.near_copies;
1989 int n = conf->copies;
1993 struct md_rdev *rdev;
1994 if (this != ignore &&
1995 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1996 test_bit(In_sync, &rdev->flags))
1998 this = (this+1) % disks;
2002 first = (first + ncopies) % disks;
2003 } while (first != 0);
2010 static int enough(struct r10conf *conf, int ignore)
2012 /* when calling 'enough', both 'prev' and 'geo' must
2014 * This is ensured if ->reconfig_mutex or ->device_lock
2017 return _enough(conf, 0, ignore) &&
2018 _enough(conf, 1, ignore);
2022 * raid10_error() - RAID10 error handler.
2023 * @mddev: affected md device.
2024 * @rdev: member device to fail.
2026 * The routine acknowledges &rdev failure and determines new @mddev state.
2027 * If it failed, then:
2028 * - &MD_BROKEN flag is set in &mddev->flags.
2029 * Otherwise, it must be degraded:
2030 * - recovery is interrupted.
2031 * - &mddev->degraded is bumped.
2033 * @rdev is marked as &Faulty excluding case when array is failed and
2034 * &mddev->fail_last_dev is off.
2036 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
2038 struct r10conf *conf = mddev->private;
2039 unsigned long flags;
2041 spin_lock_irqsave(&conf->device_lock, flags);
2043 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) {
2044 set_bit(MD_BROKEN, &mddev->flags);
2046 if (!mddev->fail_last_dev) {
2047 spin_unlock_irqrestore(&conf->device_lock, flags);
2051 if (test_and_clear_bit(In_sync, &rdev->flags))
2054 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2055 set_bit(Blocked, &rdev->flags);
2056 set_bit(Faulty, &rdev->flags);
2057 set_mask_bits(&mddev->sb_flags, 0,
2058 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2059 spin_unlock_irqrestore(&conf->device_lock, flags);
2060 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
2061 "md/raid10:%s: Operation continuing on %d devices.\n",
2062 mdname(mddev), rdev->bdev,
2063 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
2066 static void print_conf(struct r10conf *conf)
2069 struct md_rdev *rdev;
2071 pr_debug("RAID10 conf printout:\n");
2073 pr_debug("(!conf)\n");
2076 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
2077 conf->geo.raid_disks);
2079 /* This is only called with ->reconfix_mutex held, so
2080 * rcu protection of rdev is not needed */
2081 for (i = 0; i < conf->geo.raid_disks; i++) {
2082 rdev = conf->mirrors[i].rdev;
2084 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
2085 i, !test_bit(In_sync, &rdev->flags),
2086 !test_bit(Faulty, &rdev->flags),
2091 static void close_sync(struct r10conf *conf)
2093 wait_barrier(conf, false);
2094 allow_barrier(conf);
2096 mempool_exit(&conf->r10buf_pool);
2099 static int raid10_spare_active(struct mddev *mddev)
2102 struct r10conf *conf = mddev->private;
2103 struct raid10_info *tmp;
2105 unsigned long flags;
2108 * Find all non-in_sync disks within the RAID10 configuration
2109 * and mark them in_sync
2111 for (i = 0; i < conf->geo.raid_disks; i++) {
2112 tmp = conf->mirrors + i;
2113 if (tmp->replacement
2114 && tmp->replacement->recovery_offset == MaxSector
2115 && !test_bit(Faulty, &tmp->replacement->flags)
2116 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2117 /* Replacement has just become active */
2119 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2122 /* Replaced device not technically faulty,
2123 * but we need to be sure it gets removed
2124 * and never re-added.
2126 set_bit(Faulty, &tmp->rdev->flags);
2127 sysfs_notify_dirent_safe(
2128 tmp->rdev->sysfs_state);
2130 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2131 } else if (tmp->rdev
2132 && tmp->rdev->recovery_offset == MaxSector
2133 && !test_bit(Faulty, &tmp->rdev->flags)
2134 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
2136 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
2139 spin_lock_irqsave(&conf->device_lock, flags);
2140 mddev->degraded -= count;
2141 spin_unlock_irqrestore(&conf->device_lock, flags);
2147 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
2149 struct r10conf *conf = mddev->private;
2153 int last = conf->geo.raid_disks - 1;
2155 if (mddev->recovery_cp < MaxSector)
2156 /* only hot-add to in-sync arrays, as recovery is
2157 * very different from resync
2160 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
2163 if (md_integrity_add_rdev(rdev, mddev))
2166 if (rdev->raid_disk >= 0)
2167 first = last = rdev->raid_disk;
2169 if (rdev->saved_raid_disk >= first &&
2170 rdev->saved_raid_disk < conf->geo.raid_disks &&
2171 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2172 mirror = rdev->saved_raid_disk;
2175 for ( ; mirror <= last ; mirror++) {
2176 struct raid10_info *p = &conf->mirrors[mirror];
2177 if (p->recovery_disabled == mddev->recovery_disabled)
2180 if (!test_bit(WantReplacement, &p->rdev->flags) ||
2181 p->replacement != NULL)
2183 clear_bit(In_sync, &rdev->flags);
2184 set_bit(Replacement, &rdev->flags);
2185 rdev->raid_disk = mirror;
2188 disk_stack_limits(mddev->gendisk, rdev->bdev,
2189 rdev->data_offset << 9);
2191 rcu_assign_pointer(p->replacement, rdev);
2196 disk_stack_limits(mddev->gendisk, rdev->bdev,
2197 rdev->data_offset << 9);
2199 p->head_position = 0;
2200 p->recovery_disabled = mddev->recovery_disabled - 1;
2201 rdev->raid_disk = mirror;
2203 if (rdev->saved_raid_disk != mirror)
2205 rcu_assign_pointer(p->rdev, rdev);
2213 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
2215 struct r10conf *conf = mddev->private;
2217 int number = rdev->raid_disk;
2218 struct md_rdev **rdevp;
2219 struct raid10_info *p;
2222 if (unlikely(number >= mddev->raid_disks))
2224 p = conf->mirrors + number;
2225 if (rdev == p->rdev)
2227 else if (rdev == p->replacement)
2228 rdevp = &p->replacement;
2232 if (test_bit(In_sync, &rdev->flags) ||
2233 atomic_read(&rdev->nr_pending)) {
2237 /* Only remove non-faulty devices if recovery
2240 if (!test_bit(Faulty, &rdev->flags) &&
2241 mddev->recovery_disabled != p->recovery_disabled &&
2242 (!p->replacement || p->replacement == rdev) &&
2243 number < conf->geo.raid_disks &&
2249 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2251 if (atomic_read(&rdev->nr_pending)) {
2252 /* lost the race, try later */
2258 if (p->replacement) {
2259 /* We must have just cleared 'rdev' */
2260 p->rdev = p->replacement;
2261 clear_bit(Replacement, &p->replacement->flags);
2262 smp_mb(); /* Make sure other CPUs may see both as identical
2263 * but will never see neither -- if they are careful.
2265 p->replacement = NULL;
2268 clear_bit(WantReplacement, &rdev->flags);
2269 err = md_integrity_register(mddev);
2277 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
2279 struct r10conf *conf = r10_bio->mddev->private;
2281 if (!bio->bi_status)
2282 set_bit(R10BIO_Uptodate, &r10_bio->state);
2284 /* The write handler will notice the lack of
2285 * R10BIO_Uptodate and record any errors etc
2287 atomic_add(r10_bio->sectors,
2288 &conf->mirrors[d].rdev->corrected_errors);
2290 /* for reconstruct, we always reschedule after a read.
2291 * for resync, only after all reads
2293 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
2294 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2295 atomic_dec_and_test(&r10_bio->remaining)) {
2296 /* we have read all the blocks,
2297 * do the comparison in process context in raid10d
2299 reschedule_retry(r10_bio);
2303 static void end_sync_read(struct bio *bio)
2305 struct r10bio *r10_bio = get_resync_r10bio(bio);
2306 struct r10conf *conf = r10_bio->mddev->private;
2307 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2309 __end_sync_read(r10_bio, bio, d);
2312 static void end_reshape_read(struct bio *bio)
2314 /* reshape read bio isn't allocated from r10buf_pool */
2315 struct r10bio *r10_bio = bio->bi_private;
2317 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2320 static void end_sync_request(struct r10bio *r10_bio)
2322 struct mddev *mddev = r10_bio->mddev;
2324 while (atomic_dec_and_test(&r10_bio->remaining)) {
2325 if (r10_bio->master_bio == NULL) {
2326 /* the primary of several recovery bios */
2327 sector_t s = r10_bio->sectors;
2328 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2329 test_bit(R10BIO_WriteError, &r10_bio->state))
2330 reschedule_retry(r10_bio);
2333 md_done_sync(mddev, s, 1);
2336 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
2337 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2338 test_bit(R10BIO_WriteError, &r10_bio->state))
2339 reschedule_retry(r10_bio);
2347 static void end_sync_write(struct bio *bio)
2349 struct r10bio *r10_bio = get_resync_r10bio(bio);
2350 struct mddev *mddev = r10_bio->mddev;
2351 struct r10conf *conf = mddev->private;
2357 struct md_rdev *rdev = NULL;
2359 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2361 rdev = conf->mirrors[d].replacement;
2363 rdev = conf->mirrors[d].rdev;
2365 if (bio->bi_status) {
2367 md_error(mddev, rdev);
2369 set_bit(WriteErrorSeen, &rdev->flags);
2370 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2371 set_bit(MD_RECOVERY_NEEDED,
2372 &rdev->mddev->recovery);
2373 set_bit(R10BIO_WriteError, &r10_bio->state);
2375 } else if (is_badblock(rdev,
2376 r10_bio->devs[slot].addr,
2378 &first_bad, &bad_sectors))
2379 set_bit(R10BIO_MadeGood, &r10_bio->state);
2381 rdev_dec_pending(rdev, mddev);
2383 end_sync_request(r10_bio);
2387 * Note: sync and recover and handled very differently for raid10
2388 * This code is for resync.
2389 * For resync, we read through virtual addresses and read all blocks.
2390 * If there is any error, we schedule a write. The lowest numbered
2391 * drive is authoritative.
2392 * However requests come for physical address, so we need to map.
2393 * For every physical address there are raid_disks/copies virtual addresses,
2394 * which is always are least one, but is not necessarly an integer.
2395 * This means that a physical address can span multiple chunks, so we may
2396 * have to submit multiple io requests for a single sync request.
2399 * We check if all blocks are in-sync and only write to blocks that
2402 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2404 struct r10conf *conf = mddev->private;
2406 struct bio *tbio, *fbio;
2408 struct page **tpages, **fpages;
2410 atomic_set(&r10_bio->remaining, 1);
2412 /* find the first device with a block */
2413 for (i=0; i<conf->copies; i++)
2414 if (!r10_bio->devs[i].bio->bi_status)
2417 if (i == conf->copies)
2421 fbio = r10_bio->devs[i].bio;
2422 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2423 fbio->bi_iter.bi_idx = 0;
2424 fpages = get_resync_pages(fbio)->pages;
2426 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2427 /* now find blocks with errors */
2428 for (i=0 ; i < conf->copies ; i++) {
2430 struct md_rdev *rdev;
2431 struct resync_pages *rp;
2433 tbio = r10_bio->devs[i].bio;
2435 if (tbio->bi_end_io != end_sync_read)
2440 tpages = get_resync_pages(tbio)->pages;
2441 d = r10_bio->devs[i].devnum;
2442 rdev = conf->mirrors[d].rdev;
2443 if (!r10_bio->devs[i].bio->bi_status) {
2444 /* We know that the bi_io_vec layout is the same for
2445 * both 'first' and 'i', so we just compare them.
2446 * All vec entries are PAGE_SIZE;
2448 int sectors = r10_bio->sectors;
2449 for (j = 0; j < vcnt; j++) {
2450 int len = PAGE_SIZE;
2451 if (sectors < (len / 512))
2452 len = sectors * 512;
2453 if (memcmp(page_address(fpages[j]),
2454 page_address(tpages[j]),
2461 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2462 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2463 /* Don't fix anything. */
2465 } else if (test_bit(FailFast, &rdev->flags)) {
2466 /* Just give up on this device */
2467 md_error(rdev->mddev, rdev);
2470 /* Ok, we need to write this bio, either to correct an
2471 * inconsistency or to correct an unreadable block.
2472 * First we need to fixup bv_offset, bv_len and
2473 * bi_vecs, as the read request might have corrupted these
2475 rp = get_resync_pages(tbio);
2476 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE);
2478 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2480 rp->raid_bio = r10_bio;
2481 tbio->bi_private = rp;
2482 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2483 tbio->bi_end_io = end_sync_write;
2485 bio_copy_data(tbio, fbio);
2487 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2488 atomic_inc(&r10_bio->remaining);
2489 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2491 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2492 tbio->bi_opf |= MD_FAILFAST;
2493 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2494 submit_bio_noacct(tbio);
2497 /* Now write out to any replacement devices
2500 for (i = 0; i < conf->copies; i++) {
2503 tbio = r10_bio->devs[i].repl_bio;
2504 if (!tbio || !tbio->bi_end_io)
2506 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2507 && r10_bio->devs[i].bio != fbio)
2508 bio_copy_data(tbio, fbio);
2509 d = r10_bio->devs[i].devnum;
2510 atomic_inc(&r10_bio->remaining);
2511 md_sync_acct(conf->mirrors[d].replacement->bdev,
2513 submit_bio_noacct(tbio);
2517 if (atomic_dec_and_test(&r10_bio->remaining)) {
2518 md_done_sync(mddev, r10_bio->sectors, 1);
2524 * Now for the recovery code.
2525 * Recovery happens across physical sectors.
2526 * We recover all non-is_sync drives by finding the virtual address of
2527 * each, and then choose a working drive that also has that virt address.
2528 * There is a separate r10_bio for each non-in_sync drive.
2529 * Only the first two slots are in use. The first for reading,
2530 * The second for writing.
2533 static void fix_recovery_read_error(struct r10bio *r10_bio)
2535 /* We got a read error during recovery.
2536 * We repeat the read in smaller page-sized sections.
2537 * If a read succeeds, write it to the new device or record
2538 * a bad block if we cannot.
2539 * If a read fails, record a bad block on both old and
2542 struct mddev *mddev = r10_bio->mddev;
2543 struct r10conf *conf = mddev->private;
2544 struct bio *bio = r10_bio->devs[0].bio;
2546 int sectors = r10_bio->sectors;
2548 int dr = r10_bio->devs[0].devnum;
2549 int dw = r10_bio->devs[1].devnum;
2550 struct page **pages = get_resync_pages(bio)->pages;
2554 struct md_rdev *rdev;
2558 if (s > (PAGE_SIZE>>9))
2561 rdev = conf->mirrors[dr].rdev;
2562 addr = r10_bio->devs[0].addr + sect,
2563 ok = sync_page_io(rdev,
2567 REQ_OP_READ, false);
2569 rdev = conf->mirrors[dw].rdev;
2570 addr = r10_bio->devs[1].addr + sect;
2571 ok = sync_page_io(rdev,
2575 REQ_OP_WRITE, false);
2577 set_bit(WriteErrorSeen, &rdev->flags);
2578 if (!test_and_set_bit(WantReplacement,
2580 set_bit(MD_RECOVERY_NEEDED,
2581 &rdev->mddev->recovery);
2585 /* We don't worry if we cannot set a bad block -
2586 * it really is bad so there is no loss in not
2589 rdev_set_badblocks(rdev, addr, s, 0);
2591 if (rdev != conf->mirrors[dw].rdev) {
2592 /* need bad block on destination too */
2593 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2594 addr = r10_bio->devs[1].addr + sect;
2595 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2597 /* just abort the recovery */
2598 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2601 conf->mirrors[dw].recovery_disabled
2602 = mddev->recovery_disabled;
2603 set_bit(MD_RECOVERY_INTR,
2616 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2618 struct r10conf *conf = mddev->private;
2620 struct bio *wbio = r10_bio->devs[1].bio;
2621 struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2623 /* Need to test wbio2->bi_end_io before we call
2624 * submit_bio_noacct as if the former is NULL,
2625 * the latter is free to free wbio2.
2627 if (wbio2 && !wbio2->bi_end_io)
2630 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2631 fix_recovery_read_error(r10_bio);
2632 if (wbio->bi_end_io)
2633 end_sync_request(r10_bio);
2635 end_sync_request(r10_bio);
2640 * share the pages with the first bio
2641 * and submit the write request
2643 d = r10_bio->devs[1].devnum;
2644 if (wbio->bi_end_io) {
2645 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2646 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2647 submit_bio_noacct(wbio);
2650 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2651 md_sync_acct(conf->mirrors[d].replacement->bdev,
2652 bio_sectors(wbio2));
2653 submit_bio_noacct(wbio2);
2658 * Used by fix_read_error() to decay the per rdev read_errors.
2659 * We halve the read error count for every hour that has elapsed
2660 * since the last recorded read error.
2663 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2666 unsigned long hours_since_last;
2667 unsigned int read_errors = atomic_read(&rdev->read_errors);
2669 cur_time_mon = ktime_get_seconds();
2671 if (rdev->last_read_error == 0) {
2672 /* first time we've seen a read error */
2673 rdev->last_read_error = cur_time_mon;
2677 hours_since_last = (long)(cur_time_mon -
2678 rdev->last_read_error) / 3600;
2680 rdev->last_read_error = cur_time_mon;
2683 * if hours_since_last is > the number of bits in read_errors
2684 * just set read errors to 0. We do this to avoid
2685 * overflowing the shift of read_errors by hours_since_last.
2687 if (hours_since_last >= 8 * sizeof(read_errors))
2688 atomic_set(&rdev->read_errors, 0);
2690 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2693 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2694 int sectors, struct page *page, enum req_op op)
2699 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2700 && (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags)))
2702 if (sync_page_io(rdev, sector, sectors << 9, page, op, false))
2705 if (op == REQ_OP_WRITE) {
2706 set_bit(WriteErrorSeen, &rdev->flags);
2707 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2708 set_bit(MD_RECOVERY_NEEDED,
2709 &rdev->mddev->recovery);
2711 /* need to record an error - either for the block or the device */
2712 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2713 md_error(rdev->mddev, rdev);
2718 * This is a kernel thread which:
2720 * 1. Retries failed read operations on working mirrors.
2721 * 2. Updates the raid superblock when problems encounter.
2722 * 3. Performs writes following reads for array synchronising.
2725 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2727 int sect = 0; /* Offset from r10_bio->sector */
2728 int sectors = r10_bio->sectors;
2729 struct md_rdev *rdev;
2730 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2731 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2733 /* still own a reference to this rdev, so it cannot
2734 * have been cleared recently.
2736 rdev = conf->mirrors[d].rdev;
2738 if (test_bit(Faulty, &rdev->flags))
2739 /* drive has already been failed, just ignore any
2740 more fix_read_error() attempts */
2743 check_decay_read_errors(mddev, rdev);
2744 atomic_inc(&rdev->read_errors);
2745 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2746 pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2747 mdname(mddev), rdev->bdev,
2748 atomic_read(&rdev->read_errors), max_read_errors);
2749 pr_notice("md/raid10:%s: %pg: Failing raid device\n",
2750 mdname(mddev), rdev->bdev);
2751 md_error(mddev, rdev);
2752 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2758 int sl = r10_bio->read_slot;
2762 if (s > (PAGE_SIZE>>9))
2770 d = r10_bio->devs[sl].devnum;
2771 rdev = rcu_dereference(conf->mirrors[d].rdev);
2773 test_bit(In_sync, &rdev->flags) &&
2774 !test_bit(Faulty, &rdev->flags) &&
2775 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2776 &first_bad, &bad_sectors) == 0) {
2777 atomic_inc(&rdev->nr_pending);
2779 success = sync_page_io(rdev,
2780 r10_bio->devs[sl].addr +
2784 REQ_OP_READ, false);
2785 rdev_dec_pending(rdev, mddev);
2791 if (sl == conf->copies)
2793 } while (!success && sl != r10_bio->read_slot);
2797 /* Cannot read from anywhere, just mark the block
2798 * as bad on the first device to discourage future
2801 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2802 rdev = conf->mirrors[dn].rdev;
2804 if (!rdev_set_badblocks(
2806 r10_bio->devs[r10_bio->read_slot].addr
2809 md_error(mddev, rdev);
2810 r10_bio->devs[r10_bio->read_slot].bio
2817 /* write it back and re-read */
2819 while (sl != r10_bio->read_slot) {
2823 d = r10_bio->devs[sl].devnum;
2824 rdev = rcu_dereference(conf->mirrors[d].rdev);
2826 test_bit(Faulty, &rdev->flags) ||
2827 !test_bit(In_sync, &rdev->flags))
2830 atomic_inc(&rdev->nr_pending);
2832 if (r10_sync_page_io(rdev,
2833 r10_bio->devs[sl].addr +
2835 s, conf->tmppage, REQ_OP_WRITE)
2837 /* Well, this device is dead */
2838 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
2840 (unsigned long long)(
2842 choose_data_offset(r10_bio,
2845 pr_notice("md/raid10:%s: %pg: failing drive\n",
2849 rdev_dec_pending(rdev, mddev);
2853 while (sl != r10_bio->read_slot) {
2857 d = r10_bio->devs[sl].devnum;
2858 rdev = rcu_dereference(conf->mirrors[d].rdev);
2860 test_bit(Faulty, &rdev->flags) ||
2861 !test_bit(In_sync, &rdev->flags))
2864 atomic_inc(&rdev->nr_pending);
2866 switch (r10_sync_page_io(rdev,
2867 r10_bio->devs[sl].addr +
2869 s, conf->tmppage, REQ_OP_READ)) {
2871 /* Well, this device is dead */
2872 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
2874 (unsigned long long)(
2876 choose_data_offset(r10_bio, rdev)),
2878 pr_notice("md/raid10:%s: %pg: failing drive\n",
2883 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
2885 (unsigned long long)(
2887 choose_data_offset(r10_bio, rdev)),
2889 atomic_add(s, &rdev->corrected_errors);
2892 rdev_dec_pending(rdev, mddev);
2902 static int narrow_write_error(struct r10bio *r10_bio, int i)
2904 struct bio *bio = r10_bio->master_bio;
2905 struct mddev *mddev = r10_bio->mddev;
2906 struct r10conf *conf = mddev->private;
2907 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2908 /* bio has the data to be written to slot 'i' where
2909 * we just recently had a write error.
2910 * We repeatedly clone the bio and trim down to one block,
2911 * then try the write. Where the write fails we record
2913 * It is conceivable that the bio doesn't exactly align with
2914 * blocks. We must handle this.
2916 * We currently own a reference to the rdev.
2922 int sect_to_write = r10_bio->sectors;
2925 if (rdev->badblocks.shift < 0)
2928 block_sectors = roundup(1 << rdev->badblocks.shift,
2929 bdev_logical_block_size(rdev->bdev) >> 9);
2930 sector = r10_bio->sector;
2931 sectors = ((r10_bio->sector + block_sectors)
2932 & ~(sector_t)(block_sectors - 1))
2935 while (sect_to_write) {
2938 if (sectors > sect_to_write)
2939 sectors = sect_to_write;
2940 /* Write at 'sector' for 'sectors' */
2941 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
2943 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2944 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2945 wbio->bi_iter.bi_sector = wsector +
2946 choose_data_offset(r10_bio, rdev);
2947 wbio->bi_opf = REQ_OP_WRITE;
2949 if (submit_bio_wait(wbio) < 0)
2951 ok = rdev_set_badblocks(rdev, wsector,
2956 sect_to_write -= sectors;
2958 sectors = block_sectors;
2963 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2965 int slot = r10_bio->read_slot;
2967 struct r10conf *conf = mddev->private;
2968 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2970 /* we got a read error. Maybe the drive is bad. Maybe just
2971 * the block and we can fix it.
2972 * We freeze all other IO, and try reading the block from
2973 * other devices. When we find one, we re-write
2974 * and check it that fixes the read error.
2975 * This is all done synchronously while the array is
2978 bio = r10_bio->devs[slot].bio;
2980 r10_bio->devs[slot].bio = NULL;
2983 r10_bio->devs[slot].bio = IO_BLOCKED;
2984 else if (!test_bit(FailFast, &rdev->flags)) {
2985 freeze_array(conf, 1);
2986 fix_read_error(conf, mddev, r10_bio);
2987 unfreeze_array(conf);
2989 md_error(mddev, rdev);
2991 rdev_dec_pending(rdev, mddev);
2993 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2995 * allow_barrier after re-submit to ensure no sync io
2996 * can be issued while regular io pending.
2998 allow_barrier(conf);
3001 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
3003 /* Some sort of write request has finished and it
3004 * succeeded in writing where we thought there was a
3005 * bad block. So forget the bad block.
3006 * Or possibly if failed and we need to record
3010 struct md_rdev *rdev;
3012 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
3013 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
3014 for (m = 0; m < conf->copies; m++) {
3015 int dev = r10_bio->devs[m].devnum;
3016 rdev = conf->mirrors[dev].rdev;
3017 if (r10_bio->devs[m].bio == NULL ||
3018 r10_bio->devs[m].bio->bi_end_io == NULL)
3020 if (!r10_bio->devs[m].bio->bi_status) {
3021 rdev_clear_badblocks(
3023 r10_bio->devs[m].addr,
3024 r10_bio->sectors, 0);
3026 if (!rdev_set_badblocks(
3028 r10_bio->devs[m].addr,
3029 r10_bio->sectors, 0))
3030 md_error(conf->mddev, rdev);
3032 rdev = conf->mirrors[dev].replacement;
3033 if (r10_bio->devs[m].repl_bio == NULL ||
3034 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
3037 if (!r10_bio->devs[m].repl_bio->bi_status) {
3038 rdev_clear_badblocks(
3040 r10_bio->devs[m].addr,
3041 r10_bio->sectors, 0);
3043 if (!rdev_set_badblocks(
3045 r10_bio->devs[m].addr,
3046 r10_bio->sectors, 0))
3047 md_error(conf->mddev, rdev);
3053 for (m = 0; m < conf->copies; m++) {
3054 int dev = r10_bio->devs[m].devnum;
3055 struct bio *bio = r10_bio->devs[m].bio;
3056 rdev = conf->mirrors[dev].rdev;
3057 if (bio == IO_MADE_GOOD) {
3058 rdev_clear_badblocks(
3060 r10_bio->devs[m].addr,
3061 r10_bio->sectors, 0);
3062 rdev_dec_pending(rdev, conf->mddev);
3063 } else if (bio != NULL && bio->bi_status) {
3065 if (!narrow_write_error(r10_bio, m)) {
3066 md_error(conf->mddev, rdev);
3067 set_bit(R10BIO_Degraded,
3070 rdev_dec_pending(rdev, conf->mddev);
3072 bio = r10_bio->devs[m].repl_bio;
3073 rdev = conf->mirrors[dev].replacement;
3074 if (rdev && bio == IO_MADE_GOOD) {
3075 rdev_clear_badblocks(
3077 r10_bio->devs[m].addr,
3078 r10_bio->sectors, 0);
3079 rdev_dec_pending(rdev, conf->mddev);
3083 spin_lock_irq(&conf->device_lock);
3084 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
3086 spin_unlock_irq(&conf->device_lock);
3088 * In case freeze_array() is waiting for condition
3089 * nr_pending == nr_queued + extra to be true.
3091 wake_up(&conf->wait_barrier);
3092 md_wakeup_thread(conf->mddev->thread);
3094 if (test_bit(R10BIO_WriteError,
3096 close_write(r10_bio);
3097 raid_end_bio_io(r10_bio);
3102 static void raid10d(struct md_thread *thread)
3104 struct mddev *mddev = thread->mddev;
3105 struct r10bio *r10_bio;
3106 unsigned long flags;
3107 struct r10conf *conf = mddev->private;
3108 struct list_head *head = &conf->retry_list;
3109 struct blk_plug plug;
3111 md_check_recovery(mddev);
3113 if (!list_empty_careful(&conf->bio_end_io_list) &&
3114 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3116 spin_lock_irqsave(&conf->device_lock, flags);
3117 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3118 while (!list_empty(&conf->bio_end_io_list)) {
3119 list_move(conf->bio_end_io_list.prev, &tmp);
3123 spin_unlock_irqrestore(&conf->device_lock, flags);
3124 while (!list_empty(&tmp)) {
3125 r10_bio = list_first_entry(&tmp, struct r10bio,
3127 list_del(&r10_bio->retry_list);
3128 if (mddev->degraded)
3129 set_bit(R10BIO_Degraded, &r10_bio->state);
3131 if (test_bit(R10BIO_WriteError,
3133 close_write(r10_bio);
3134 raid_end_bio_io(r10_bio);
3138 blk_start_plug(&plug);
3141 flush_pending_writes(conf);
3143 spin_lock_irqsave(&conf->device_lock, flags);
3144 if (list_empty(head)) {
3145 spin_unlock_irqrestore(&conf->device_lock, flags);
3148 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
3149 list_del(head->prev);
3151 spin_unlock_irqrestore(&conf->device_lock, flags);
3153 mddev = r10_bio->mddev;
3154 conf = mddev->private;
3155 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3156 test_bit(R10BIO_WriteError, &r10_bio->state))
3157 handle_write_completed(conf, r10_bio);
3158 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3159 reshape_request_write(mddev, r10_bio);
3160 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
3161 sync_request_write(mddev, r10_bio);
3162 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
3163 recovery_request_write(mddev, r10_bio);
3164 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
3165 handle_read_error(mddev, r10_bio);
3170 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
3171 md_check_recovery(mddev);
3173 blk_finish_plug(&plug);
3176 static int init_resync(struct r10conf *conf)
3180 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
3181 BUG_ON(mempool_initialized(&conf->r10buf_pool));
3182 conf->have_replacement = 0;
3183 for (i = 0; i < conf->geo.raid_disks; i++)
3184 if (conf->mirrors[i].replacement)
3185 conf->have_replacement = 1;
3186 ret = mempool_init(&conf->r10buf_pool, buffs,
3187 r10buf_pool_alloc, r10buf_pool_free, conf);
3190 conf->next_resync = 0;
3194 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3196 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
3197 struct rsync_pages *rp;
3202 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3203 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3204 nalloc = conf->copies; /* resync */
3206 nalloc = 2; /* recovery */
3208 for (i = 0; i < nalloc; i++) {
3209 bio = r10bio->devs[i].bio;
3210 rp = bio->bi_private;
3211 bio_reset(bio, NULL, 0);
3212 bio->bi_private = rp;
3213 bio = r10bio->devs[i].repl_bio;
3215 rp = bio->bi_private;
3216 bio_reset(bio, NULL, 0);
3217 bio->bi_private = rp;
3224 * Set cluster_sync_high since we need other nodes to add the
3225 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3227 static void raid10_set_cluster_sync_high(struct r10conf *conf)
3229 sector_t window_size;
3230 int extra_chunk, chunks;
3233 * First, here we define "stripe" as a unit which across
3234 * all member devices one time, so we get chunks by use
3235 * raid_disks / near_copies. Otherwise, if near_copies is
3236 * close to raid_disks, then resync window could increases
3237 * linearly with the increase of raid_disks, which means
3238 * we will suspend a really large IO window while it is not
3239 * necessary. If raid_disks is not divisible by near_copies,
3240 * an extra chunk is needed to ensure the whole "stripe" is
3244 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3245 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3249 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3252 * At least use a 32M window to align with raid1's resync window
3254 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3255 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3257 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3261 * perform a "sync" on one "block"
3263 * We need to make sure that no normal I/O request - particularly write
3264 * requests - conflict with active sync requests.
3266 * This is achieved by tracking pending requests and a 'barrier' concept
3267 * that can be installed to exclude normal IO requests.
3269 * Resync and recovery are handled very differently.
3270 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3272 * For resync, we iterate over virtual addresses, read all copies,
3273 * and update if there are differences. If only one copy is live,
3275 * For recovery, we iterate over physical addresses, read a good
3276 * value for each non-in_sync drive, and over-write.
3278 * So, for recovery we may have several outstanding complex requests for a
3279 * given address, one for each out-of-sync device. We model this by allocating
3280 * a number of r10_bio structures, one for each out-of-sync device.
3281 * As we setup these structures, we collect all bio's together into a list
3282 * which we then process collectively to add pages, and then process again
3283 * to pass to submit_bio_noacct.
3285 * The r10_bio structures are linked using a borrowed master_bio pointer.
3286 * This link is counted in ->remaining. When the r10_bio that points to NULL
3287 * has its remaining count decremented to 0, the whole complex operation
3292 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
3295 struct r10conf *conf = mddev->private;
3296 struct r10bio *r10_bio;
3297 struct bio *biolist = NULL, *bio;
3298 sector_t max_sector, nr_sectors;
3301 sector_t sync_blocks;
3302 sector_t sectors_skipped = 0;
3303 int chunks_skipped = 0;
3304 sector_t chunk_mask = conf->geo.chunk_mask;
3308 * Allow skipping a full rebuild for incremental assembly
3309 * of a clean array, like RAID1 does.
3311 if (mddev->bitmap == NULL &&
3312 mddev->recovery_cp == MaxSector &&
3313 mddev->reshape_position == MaxSector &&
3314 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
3315 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
3316 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
3317 conf->fullsync == 0) {
3319 return mddev->dev_sectors - sector_nr;
3322 if (!mempool_initialized(&conf->r10buf_pool))
3323 if (init_resync(conf))
3327 max_sector = mddev->dev_sectors;
3328 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3329 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3330 max_sector = mddev->resync_max_sectors;
3331 if (sector_nr >= max_sector) {
3332 conf->cluster_sync_low = 0;
3333 conf->cluster_sync_high = 0;
3335 /* If we aborted, we need to abort the
3336 * sync on the 'current' bitmap chucks (there can
3337 * be several when recovering multiple devices).
3338 * as we may have started syncing it but not finished.
3339 * We can find the current address in
3340 * mddev->curr_resync, but for recovery,
3341 * we need to convert that to several
3342 * virtual addresses.
3344 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3350 if (mddev->curr_resync < max_sector) { /* aborted */
3351 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3352 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3354 else for (i = 0; i < conf->geo.raid_disks; i++) {
3356 raid10_find_virt(conf, mddev->curr_resync, i);
3357 md_bitmap_end_sync(mddev->bitmap, sect,
3361 /* completed sync */
3362 if ((!mddev->bitmap || conf->fullsync)
3363 && conf->have_replacement
3364 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3365 /* Completed a full sync so the replacements
3366 * are now fully recovered.
3369 for (i = 0; i < conf->geo.raid_disks; i++) {
3370 struct md_rdev *rdev =
3371 rcu_dereference(conf->mirrors[i].replacement);
3373 rdev->recovery_offset = MaxSector;
3379 md_bitmap_close_sync(mddev->bitmap);
3382 return sectors_skipped;
3385 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3386 return reshape_request(mddev, sector_nr, skipped);
3388 if (chunks_skipped >= conf->geo.raid_disks) {
3389 /* if there has been nothing to do on any drive,
3390 * then there is nothing to do at all..
3393 return (max_sector - sector_nr) + sectors_skipped;
3396 if (max_sector > mddev->resync_max)
3397 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3399 /* make sure whole request will fit in a chunk - if chunks
3402 if (conf->geo.near_copies < conf->geo.raid_disks &&
3403 max_sector > (sector_nr | chunk_mask))
3404 max_sector = (sector_nr | chunk_mask) + 1;
3407 * If there is non-resync activity waiting for a turn, then let it
3408 * though before starting on this new sync request.
3410 if (conf->nr_waiting)
3411 schedule_timeout_uninterruptible(1);
3413 /* Again, very different code for resync and recovery.
3414 * Both must result in an r10bio with a list of bios that
3415 * have bi_end_io, bi_sector, bi_bdev set,
3416 * and bi_private set to the r10bio.
3417 * For recovery, we may actually create several r10bios
3418 * with 2 bios in each, that correspond to the bios in the main one.
3419 * In this case, the subordinate r10bios link back through a
3420 * borrowed master_bio pointer, and the counter in the master
3421 * includes a ref from each subordinate.
3423 /* First, we decide what to do and set ->bi_end_io
3424 * To end_sync_read if we want to read, and
3425 * end_sync_write if we will want to write.
3428 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3429 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3430 /* recovery... the complicated one */
3434 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3440 int need_recover = 0;
3441 int need_replace = 0;
3442 struct raid10_info *mirror = &conf->mirrors[i];
3443 struct md_rdev *mrdev, *mreplace;
3446 mrdev = rcu_dereference(mirror->rdev);
3447 mreplace = rcu_dereference(mirror->replacement);
3449 if (mrdev != NULL &&
3450 !test_bit(Faulty, &mrdev->flags) &&
3451 !test_bit(In_sync, &mrdev->flags))
3453 if (mreplace != NULL &&
3454 !test_bit(Faulty, &mreplace->flags))
3457 if (!need_recover && !need_replace) {
3463 /* want to reconstruct this device */
3465 sect = raid10_find_virt(conf, sector_nr, i);
3466 if (sect >= mddev->resync_max_sectors) {
3467 /* last stripe is not complete - don't
3468 * try to recover this sector.
3473 if (mreplace && test_bit(Faulty, &mreplace->flags))
3475 /* Unless we are doing a full sync, or a replacement
3476 * we only need to recover the block if it is set in
3479 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3481 if (sync_blocks < max_sync)
3482 max_sync = sync_blocks;
3486 /* yep, skip the sync_blocks here, but don't assume
3487 * that there will never be anything to do here
3489 chunks_skipped = -1;
3493 atomic_inc(&mrdev->nr_pending);
3495 atomic_inc(&mreplace->nr_pending);
3498 r10_bio = raid10_alloc_init_r10buf(conf);
3500 raise_barrier(conf, rb2 != NULL);
3501 atomic_set(&r10_bio->remaining, 0);
3503 r10_bio->master_bio = (struct bio*)rb2;
3505 atomic_inc(&rb2->remaining);
3506 r10_bio->mddev = mddev;
3507 set_bit(R10BIO_IsRecover, &r10_bio->state);
3508 r10_bio->sector = sect;
3510 raid10_find_phys(conf, r10_bio);
3512 /* Need to check if the array will still be
3516 for (j = 0; j < conf->geo.raid_disks; j++) {
3517 struct md_rdev *rdev = rcu_dereference(
3518 conf->mirrors[j].rdev);
3519 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3525 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3526 &sync_blocks, still_degraded);
3529 for (j=0; j<conf->copies;j++) {
3531 int d = r10_bio->devs[j].devnum;
3532 sector_t from_addr, to_addr;
3533 struct md_rdev *rdev =
3534 rcu_dereference(conf->mirrors[d].rdev);
3535 sector_t sector, first_bad;
3538 !test_bit(In_sync, &rdev->flags))
3540 /* This is where we read from */
3542 sector = r10_bio->devs[j].addr;
3544 if (is_badblock(rdev, sector, max_sync,
3545 &first_bad, &bad_sectors)) {
3546 if (first_bad > sector)
3547 max_sync = first_bad - sector;
3549 bad_sectors -= (sector
3551 if (max_sync > bad_sectors)
3552 max_sync = bad_sectors;
3556 bio = r10_bio->devs[0].bio;
3557 bio->bi_next = biolist;
3559 bio->bi_end_io = end_sync_read;
3560 bio->bi_opf = REQ_OP_READ;
3561 if (test_bit(FailFast, &rdev->flags))
3562 bio->bi_opf |= MD_FAILFAST;
3563 from_addr = r10_bio->devs[j].addr;
3564 bio->bi_iter.bi_sector = from_addr +
3566 bio_set_dev(bio, rdev->bdev);
3567 atomic_inc(&rdev->nr_pending);
3568 /* and we write to 'i' (if not in_sync) */
3570 for (k=0; k<conf->copies; k++)
3571 if (r10_bio->devs[k].devnum == i)
3573 BUG_ON(k == conf->copies);
3574 to_addr = r10_bio->devs[k].addr;
3575 r10_bio->devs[0].devnum = d;
3576 r10_bio->devs[0].addr = from_addr;
3577 r10_bio->devs[1].devnum = i;
3578 r10_bio->devs[1].addr = to_addr;
3581 bio = r10_bio->devs[1].bio;
3582 bio->bi_next = biolist;
3584 bio->bi_end_io = end_sync_write;
3585 bio->bi_opf = REQ_OP_WRITE;
3586 bio->bi_iter.bi_sector = to_addr
3587 + mrdev->data_offset;
3588 bio_set_dev(bio, mrdev->bdev);
3589 atomic_inc(&r10_bio->remaining);
3591 r10_bio->devs[1].bio->bi_end_io = NULL;
3593 /* and maybe write to replacement */
3594 bio = r10_bio->devs[1].repl_bio;
3596 bio->bi_end_io = NULL;
3597 /* Note: if need_replace, then bio
3598 * cannot be NULL as r10buf_pool_alloc will
3599 * have allocated it.
3603 bio->bi_next = biolist;
3605 bio->bi_end_io = end_sync_write;
3606 bio->bi_opf = REQ_OP_WRITE;
3607 bio->bi_iter.bi_sector = to_addr +
3608 mreplace->data_offset;
3609 bio_set_dev(bio, mreplace->bdev);
3610 atomic_inc(&r10_bio->remaining);
3614 if (j == conf->copies) {
3615 /* Cannot recover, so abort the recovery or
3616 * record a bad block */
3618 /* problem is that there are bad blocks
3619 * on other device(s)
3622 for (k = 0; k < conf->copies; k++)
3623 if (r10_bio->devs[k].devnum == i)
3625 if (!test_bit(In_sync,
3627 && !rdev_set_badblocks(
3629 r10_bio->devs[k].addr,
3633 !rdev_set_badblocks(
3635 r10_bio->devs[k].addr,
3640 if (!test_and_set_bit(MD_RECOVERY_INTR,
3642 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3644 mirror->recovery_disabled
3645 = mddev->recovery_disabled;
3649 atomic_dec(&rb2->remaining);
3651 rdev_dec_pending(mrdev, mddev);
3653 rdev_dec_pending(mreplace, mddev);
3656 rdev_dec_pending(mrdev, mddev);
3658 rdev_dec_pending(mreplace, mddev);
3659 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3660 /* Only want this if there is elsewhere to
3661 * read from. 'j' is currently the first
3665 for (; j < conf->copies; j++) {
3666 int d = r10_bio->devs[j].devnum;
3667 if (conf->mirrors[d].rdev &&
3669 &conf->mirrors[d].rdev->flags))
3673 r10_bio->devs[0].bio->bi_opf
3677 if (biolist == NULL) {
3679 struct r10bio *rb2 = r10_bio;
3680 r10_bio = (struct r10bio*) rb2->master_bio;
3681 rb2->master_bio = NULL;
3687 /* resync. Schedule a read for every block at this virt offset */
3691 * Since curr_resync_completed could probably not update in
3692 * time, and we will set cluster_sync_low based on it.
3693 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3694 * safety reason, which ensures curr_resync_completed is
3695 * updated in bitmap_cond_end_sync.
3697 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3698 mddev_is_clustered(mddev) &&
3699 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
3701 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3702 &sync_blocks, mddev->degraded) &&
3703 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3704 &mddev->recovery)) {
3705 /* We can skip this block */
3707 return sync_blocks + sectors_skipped;
3709 if (sync_blocks < max_sync)
3710 max_sync = sync_blocks;
3711 r10_bio = raid10_alloc_init_r10buf(conf);
3714 r10_bio->mddev = mddev;
3715 atomic_set(&r10_bio->remaining, 0);
3716 raise_barrier(conf, 0);
3717 conf->next_resync = sector_nr;
3719 r10_bio->master_bio = NULL;
3720 r10_bio->sector = sector_nr;
3721 set_bit(R10BIO_IsSync, &r10_bio->state);
3722 raid10_find_phys(conf, r10_bio);
3723 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3725 for (i = 0; i < conf->copies; i++) {
3726 int d = r10_bio->devs[i].devnum;
3727 sector_t first_bad, sector;
3729 struct md_rdev *rdev;
3731 if (r10_bio->devs[i].repl_bio)
3732 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3734 bio = r10_bio->devs[i].bio;
3735 bio->bi_status = BLK_STS_IOERR;
3737 rdev = rcu_dereference(conf->mirrors[d].rdev);
3738 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3742 sector = r10_bio->devs[i].addr;
3743 if (is_badblock(rdev, sector, max_sync,
3744 &first_bad, &bad_sectors)) {
3745 if (first_bad > sector)
3746 max_sync = first_bad - sector;
3748 bad_sectors -= (sector - first_bad);
3749 if (max_sync > bad_sectors)
3750 max_sync = bad_sectors;
3755 atomic_inc(&rdev->nr_pending);
3756 atomic_inc(&r10_bio->remaining);
3757 bio->bi_next = biolist;
3759 bio->bi_end_io = end_sync_read;
3760 bio->bi_opf = REQ_OP_READ;
3761 if (test_bit(FailFast, &rdev->flags))
3762 bio->bi_opf |= MD_FAILFAST;
3763 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3764 bio_set_dev(bio, rdev->bdev);
3767 rdev = rcu_dereference(conf->mirrors[d].replacement);
3768 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3772 atomic_inc(&rdev->nr_pending);
3774 /* Need to set up for writing to the replacement */
3775 bio = r10_bio->devs[i].repl_bio;
3776 bio->bi_status = BLK_STS_IOERR;
3778 sector = r10_bio->devs[i].addr;
3779 bio->bi_next = biolist;
3781 bio->bi_end_io = end_sync_write;
3782 bio->bi_opf = REQ_OP_WRITE;
3783 if (test_bit(FailFast, &rdev->flags))
3784 bio->bi_opf |= MD_FAILFAST;
3785 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3786 bio_set_dev(bio, rdev->bdev);
3792 for (i=0; i<conf->copies; i++) {
3793 int d = r10_bio->devs[i].devnum;
3794 if (r10_bio->devs[i].bio->bi_end_io)
3795 rdev_dec_pending(conf->mirrors[d].rdev,
3797 if (r10_bio->devs[i].repl_bio &&
3798 r10_bio->devs[i].repl_bio->bi_end_io)
3800 conf->mirrors[d].replacement,
3810 if (sector_nr + max_sync < max_sector)
3811 max_sector = sector_nr + max_sync;
3814 int len = PAGE_SIZE;
3815 if (sector_nr + (len>>9) > max_sector)
3816 len = (max_sector - sector_nr) << 9;
3819 for (bio= biolist ; bio ; bio=bio->bi_next) {
3820 struct resync_pages *rp = get_resync_pages(bio);
3821 page = resync_fetch_page(rp, page_idx);
3823 * won't fail because the vec table is big enough
3824 * to hold all these pages
3826 bio_add_page(bio, page, len, 0);
3828 nr_sectors += len>>9;
3829 sector_nr += len>>9;
3830 } while (++page_idx < RESYNC_PAGES);
3831 r10_bio->sectors = nr_sectors;
3833 if (mddev_is_clustered(mddev) &&
3834 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3835 /* It is resync not recovery */
3836 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3837 conf->cluster_sync_low = mddev->curr_resync_completed;
3838 raid10_set_cluster_sync_high(conf);
3839 /* Send resync message */
3840 md_cluster_ops->resync_info_update(mddev,
3841 conf->cluster_sync_low,
3842 conf->cluster_sync_high);
3844 } else if (mddev_is_clustered(mddev)) {
3845 /* This is recovery not resync */
3846 sector_t sect_va1, sect_va2;
3847 bool broadcast_msg = false;
3849 for (i = 0; i < conf->geo.raid_disks; i++) {
3851 * sector_nr is a device address for recovery, so we
3852 * need translate it to array address before compare
3853 * with cluster_sync_high.
3855 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3857 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3858 broadcast_msg = true;
3860 * curr_resync_completed is similar as
3861 * sector_nr, so make the translation too.
3863 sect_va2 = raid10_find_virt(conf,
3864 mddev->curr_resync_completed, i);
3866 if (conf->cluster_sync_low == 0 ||
3867 conf->cluster_sync_low > sect_va2)
3868 conf->cluster_sync_low = sect_va2;
3871 if (broadcast_msg) {
3872 raid10_set_cluster_sync_high(conf);
3873 md_cluster_ops->resync_info_update(mddev,
3874 conf->cluster_sync_low,
3875 conf->cluster_sync_high);
3881 biolist = biolist->bi_next;
3883 bio->bi_next = NULL;
3884 r10_bio = get_resync_r10bio(bio);
3885 r10_bio->sectors = nr_sectors;
3887 if (bio->bi_end_io == end_sync_read) {
3888 md_sync_acct_bio(bio, nr_sectors);
3890 submit_bio_noacct(bio);
3894 if (sectors_skipped)
3895 /* pretend they weren't skipped, it makes
3896 * no important difference in this case
3898 md_done_sync(mddev, sectors_skipped, 1);
3900 return sectors_skipped + nr_sectors;
3902 /* There is nowhere to write, so all non-sync
3903 * drives must be failed or in resync, all drives
3904 * have a bad block, so try the next chunk...
3906 if (sector_nr + max_sync < max_sector)
3907 max_sector = sector_nr + max_sync;
3909 sectors_skipped += (max_sector - sector_nr);
3911 sector_nr = max_sector;
3916 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3919 struct r10conf *conf = mddev->private;
3922 raid_disks = min(conf->geo.raid_disks,
3923 conf->prev.raid_disks);
3925 sectors = conf->dev_sectors;
3927 size = sectors >> conf->geo.chunk_shift;
3928 sector_div(size, conf->geo.far_copies);
3929 size = size * raid_disks;
3930 sector_div(size, conf->geo.near_copies);
3932 return size << conf->geo.chunk_shift;
3935 static void calc_sectors(struct r10conf *conf, sector_t size)
3937 /* Calculate the number of sectors-per-device that will
3938 * actually be used, and set conf->dev_sectors and
3942 size = size >> conf->geo.chunk_shift;
3943 sector_div(size, conf->geo.far_copies);
3944 size = size * conf->geo.raid_disks;
3945 sector_div(size, conf->geo.near_copies);
3946 /* 'size' is now the number of chunks in the array */
3947 /* calculate "used chunks per device" */
3948 size = size * conf->copies;
3950 /* We need to round up when dividing by raid_disks to
3951 * get the stride size.
3953 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3955 conf->dev_sectors = size << conf->geo.chunk_shift;
3957 if (conf->geo.far_offset)
3958 conf->geo.stride = 1 << conf->geo.chunk_shift;
3960 sector_div(size, conf->geo.far_copies);
3961 conf->geo.stride = size << conf->geo.chunk_shift;
3965 enum geo_type {geo_new, geo_old, geo_start};
3966 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3969 int layout, chunk, disks;
3972 layout = mddev->layout;
3973 chunk = mddev->chunk_sectors;
3974 disks = mddev->raid_disks - mddev->delta_disks;
3977 layout = mddev->new_layout;
3978 chunk = mddev->new_chunk_sectors;
3979 disks = mddev->raid_disks;
3981 default: /* avoid 'may be unused' warnings */
3982 case geo_start: /* new when starting reshape - raid_disks not
3984 layout = mddev->new_layout;
3985 chunk = mddev->new_chunk_sectors;
3986 disks = mddev->raid_disks + mddev->delta_disks;
3991 if (chunk < (PAGE_SIZE >> 9) ||
3992 !is_power_of_2(chunk))
3995 fc = (layout >> 8) & 255;
3996 fo = layout & (1<<16);
3997 geo->raid_disks = disks;
3998 geo->near_copies = nc;
3999 geo->far_copies = fc;
4000 geo->far_offset = fo;
4001 switch (layout >> 17) {
4002 case 0: /* original layout. simple but not always optimal */
4003 geo->far_set_size = disks;
4005 case 1: /* "improved" layout which was buggy. Hopefully no-one is
4006 * actually using this, but leave code here just in case.*/
4007 geo->far_set_size = disks/fc;
4008 WARN(geo->far_set_size < fc,
4009 "This RAID10 layout does not provide data safety - please backup and create new array\n");
4011 case 2: /* "improved" layout fixed to match documentation */
4012 geo->far_set_size = fc * nc;
4014 default: /* Not a valid layout */
4017 geo->chunk_mask = chunk - 1;
4018 geo->chunk_shift = ffz(~chunk);
4022 static void raid10_free_conf(struct r10conf *conf)
4027 mempool_exit(&conf->r10bio_pool);
4028 kfree(conf->mirrors);
4029 kfree(conf->mirrors_old);
4030 kfree(conf->mirrors_new);
4031 safe_put_page(conf->tmppage);
4032 bioset_exit(&conf->bio_split);
4036 static struct r10conf *setup_conf(struct mddev *mddev)
4038 struct r10conf *conf = NULL;
4043 copies = setup_geo(&geo, mddev, geo_new);
4046 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
4047 mdname(mddev), PAGE_SIZE);
4051 if (copies < 2 || copies > mddev->raid_disks) {
4052 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
4053 mdname(mddev), mddev->new_layout);
4058 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
4062 /* FIXME calc properly */
4063 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
4064 sizeof(struct raid10_info),
4069 conf->tmppage = alloc_page(GFP_KERNEL);
4074 conf->copies = copies;
4075 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
4076 rbio_pool_free, conf);
4080 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
4084 calc_sectors(conf, mddev->dev_sectors);
4085 if (mddev->reshape_position == MaxSector) {
4086 conf->prev = conf->geo;
4087 conf->reshape_progress = MaxSector;
4089 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
4093 conf->reshape_progress = mddev->reshape_position;
4094 if (conf->prev.far_offset)
4095 conf->prev.stride = 1 << conf->prev.chunk_shift;
4097 /* far_copies must be 1 */
4098 conf->prev.stride = conf->dev_sectors;
4100 conf->reshape_safe = conf->reshape_progress;
4101 spin_lock_init(&conf->device_lock);
4102 INIT_LIST_HEAD(&conf->retry_list);
4103 INIT_LIST_HEAD(&conf->bio_end_io_list);
4105 seqlock_init(&conf->resync_lock);
4106 init_waitqueue_head(&conf->wait_barrier);
4107 atomic_set(&conf->nr_pending, 0);
4110 conf->thread = md_register_thread(raid10d, mddev, "raid10");
4114 conf->mddev = mddev;
4118 raid10_free_conf(conf);
4119 return ERR_PTR(err);
4122 static void raid10_set_io_opt(struct r10conf *conf)
4124 int raid_disks = conf->geo.raid_disks;
4126 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4127 raid_disks /= conf->geo.near_copies;
4128 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4132 static int raid10_run(struct mddev *mddev)
4134 struct r10conf *conf;
4136 struct raid10_info *disk;
4137 struct md_rdev *rdev;
4139 sector_t min_offset_diff = 0;
4142 if (mddev_init_writes_pending(mddev) < 0)
4145 if (mddev->private == NULL) {
4146 conf = setup_conf(mddev);
4148 return PTR_ERR(conf);
4149 mddev->private = conf;
4151 conf = mddev->private;
4155 mddev->thread = conf->thread;
4156 conf->thread = NULL;
4158 if (mddev_is_clustered(conf->mddev)) {
4161 fc = (mddev->layout >> 8) & 255;
4162 fo = mddev->layout & (1<<16);
4163 if (fc > 1 || fo > 0) {
4164 pr_err("only near layout is supported by clustered"
4171 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
4172 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4173 raid10_set_io_opt(conf);
4176 rdev_for_each(rdev, mddev) {
4179 disk_idx = rdev->raid_disk;
4182 if (disk_idx >= conf->geo.raid_disks &&
4183 disk_idx >= conf->prev.raid_disks)
4185 disk = conf->mirrors + disk_idx;
4187 if (test_bit(Replacement, &rdev->flags)) {
4188 if (disk->replacement)
4190 disk->replacement = rdev;
4196 diff = (rdev->new_data_offset - rdev->data_offset);
4197 if (!mddev->reshape_backwards)
4201 if (first || diff < min_offset_diff)
4202 min_offset_diff = diff;
4205 disk_stack_limits(mddev->gendisk, rdev->bdev,
4206 rdev->data_offset << 9);
4208 disk->head_position = 0;
4212 /* need to check that every block has at least one working mirror */
4213 if (!enough(conf, -1)) {
4214 pr_err("md/raid10:%s: not enough operational mirrors.\n",
4219 if (conf->reshape_progress != MaxSector) {
4220 /* must ensure that shape change is supported */
4221 if (conf->geo.far_copies != 1 &&
4222 conf->geo.far_offset == 0)
4224 if (conf->prev.far_copies != 1 &&
4225 conf->prev.far_offset == 0)
4229 mddev->degraded = 0;
4231 i < conf->geo.raid_disks
4232 || i < conf->prev.raid_disks;
4235 disk = conf->mirrors + i;
4237 if (!disk->rdev && disk->replacement) {
4238 /* The replacement is all we have - use it */
4239 disk->rdev = disk->replacement;
4240 disk->replacement = NULL;
4241 clear_bit(Replacement, &disk->rdev->flags);
4245 !test_bit(In_sync, &disk->rdev->flags)) {
4246 disk->head_position = 0;
4249 disk->rdev->saved_raid_disk < 0)
4253 if (disk->replacement &&
4254 !test_bit(In_sync, &disk->replacement->flags) &&
4255 disk->replacement->saved_raid_disk < 0) {
4259 disk->recovery_disabled = mddev->recovery_disabled - 1;
4262 if (mddev->recovery_cp != MaxSector)
4263 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4265 pr_info("md/raid10:%s: active with %d out of %d devices\n",
4266 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4267 conf->geo.raid_disks);
4269 * Ok, everything is just fine now
4271 mddev->dev_sectors = conf->dev_sectors;
4272 size = raid10_size(mddev, 0, 0);
4273 md_set_array_sectors(mddev, size);
4274 mddev->resync_max_sectors = size;
4275 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
4277 if (md_integrity_register(mddev))
4280 if (conf->reshape_progress != MaxSector) {
4281 unsigned long before_length, after_length;
4283 before_length = ((1 << conf->prev.chunk_shift) *
4284 conf->prev.far_copies);
4285 after_length = ((1 << conf->geo.chunk_shift) *
4286 conf->geo.far_copies);
4288 if (max(before_length, after_length) > min_offset_diff) {
4289 /* This cannot work */
4290 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
4293 conf->offset_diff = min_offset_diff;
4295 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4296 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4297 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4298 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4299 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4301 if (!mddev->sync_thread)
4308 md_unregister_thread(&mddev->thread);
4309 raid10_free_conf(conf);
4310 mddev->private = NULL;
4315 static void raid10_free(struct mddev *mddev, void *priv)
4317 raid10_free_conf(priv);
4320 static void raid10_quiesce(struct mddev *mddev, int quiesce)
4322 struct r10conf *conf = mddev->private;
4325 raise_barrier(conf, 0);
4327 lower_barrier(conf);
4330 static int raid10_resize(struct mddev *mddev, sector_t sectors)
4332 /* Resize of 'far' arrays is not supported.
4333 * For 'near' and 'offset' arrays we can set the
4334 * number of sectors used to be an appropriate multiple
4335 * of the chunk size.
4336 * For 'offset', this is far_copies*chunksize.
4337 * For 'near' the multiplier is the LCM of
4338 * near_copies and raid_disks.
4339 * So if far_copies > 1 && !far_offset, fail.
4340 * Else find LCM(raid_disks, near_copy)*far_copies and
4341 * multiply by chunk_size. Then round to this number.
4342 * This is mostly done by raid10_size()
4344 struct r10conf *conf = mddev->private;
4345 sector_t oldsize, size;
4347 if (mddev->reshape_position != MaxSector)
4350 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
4353 oldsize = raid10_size(mddev, 0, 0);
4354 size = raid10_size(mddev, sectors, 0);
4355 if (mddev->external_size &&
4356 mddev->array_sectors > size)
4358 if (mddev->bitmap) {
4359 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
4363 md_set_array_sectors(mddev, size);
4364 if (sectors > mddev->dev_sectors &&
4365 mddev->recovery_cp > oldsize) {
4366 mddev->recovery_cp = oldsize;
4367 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4369 calc_sectors(conf, sectors);
4370 mddev->dev_sectors = conf->dev_sectors;
4371 mddev->resync_max_sectors = size;
4375 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
4377 struct md_rdev *rdev;
4378 struct r10conf *conf;
4380 if (mddev->degraded > 0) {
4381 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4383 return ERR_PTR(-EINVAL);
4385 sector_div(size, devs);
4387 /* Set new parameters */
4388 mddev->new_level = 10;
4389 /* new layout: far_copies = 1, near_copies = 2 */
4390 mddev->new_layout = (1<<8) + 2;
4391 mddev->new_chunk_sectors = mddev->chunk_sectors;
4392 mddev->delta_disks = mddev->raid_disks;
4393 mddev->raid_disks *= 2;
4394 /* make sure it will be not marked as dirty */
4395 mddev->recovery_cp = MaxSector;
4396 mddev->dev_sectors = size;
4398 conf = setup_conf(mddev);
4399 if (!IS_ERR(conf)) {
4400 rdev_for_each(rdev, mddev)
4401 if (rdev->raid_disk >= 0) {
4402 rdev->new_raid_disk = rdev->raid_disk * 2;
4403 rdev->sectors = size;
4405 WRITE_ONCE(conf->barrier, 1);
4411 static void *raid10_takeover(struct mddev *mddev)
4413 struct r0conf *raid0_conf;
4415 /* raid10 can take over:
4416 * raid0 - providing it has only two drives
4418 if (mddev->level == 0) {
4419 /* for raid0 takeover only one zone is supported */
4420 raid0_conf = mddev->private;
4421 if (raid0_conf->nr_strip_zones > 1) {
4422 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4424 return ERR_PTR(-EINVAL);
4426 return raid10_takeover_raid0(mddev,
4427 raid0_conf->strip_zone->zone_end,
4428 raid0_conf->strip_zone->nb_dev);
4430 return ERR_PTR(-EINVAL);
4433 static int raid10_check_reshape(struct mddev *mddev)
4435 /* Called when there is a request to change
4436 * - layout (to ->new_layout)
4437 * - chunk size (to ->new_chunk_sectors)
4438 * - raid_disks (by delta_disks)
4439 * or when trying to restart a reshape that was ongoing.
4441 * We need to validate the request and possibly allocate
4442 * space if that might be an issue later.
4444 * Currently we reject any reshape of a 'far' mode array,
4445 * allow chunk size to change if new is generally acceptable,
4446 * allow raid_disks to increase, and allow
4447 * a switch between 'near' mode and 'offset' mode.
4449 struct r10conf *conf = mddev->private;
4452 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4455 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4456 /* mustn't change number of copies */
4458 if (geo.far_copies > 1 && !geo.far_offset)
4459 /* Cannot switch to 'far' mode */
4462 if (mddev->array_sectors & geo.chunk_mask)
4463 /* not factor of array size */
4466 if (!enough(conf, -1))
4469 kfree(conf->mirrors_new);
4470 conf->mirrors_new = NULL;
4471 if (mddev->delta_disks > 0) {
4472 /* allocate new 'mirrors' list */
4474 kcalloc(mddev->raid_disks + mddev->delta_disks,
4475 sizeof(struct raid10_info),
4477 if (!conf->mirrors_new)
4484 * Need to check if array has failed when deciding whether to:
4486 * - remove non-faulty devices
4489 * This determination is simple when no reshape is happening.
4490 * However if there is a reshape, we need to carefully check
4491 * both the before and after sections.
4492 * This is because some failed devices may only affect one
4493 * of the two sections, and some non-in_sync devices may
4494 * be insync in the section most affected by failed devices.
4496 static int calc_degraded(struct r10conf *conf)
4498 int degraded, degraded2;
4503 /* 'prev' section first */
4504 for (i = 0; i < conf->prev.raid_disks; i++) {
4505 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4506 if (!rdev || test_bit(Faulty, &rdev->flags))
4508 else if (!test_bit(In_sync, &rdev->flags))
4509 /* When we can reduce the number of devices in
4510 * an array, this might not contribute to
4511 * 'degraded'. It does now.
4516 if (conf->geo.raid_disks == conf->prev.raid_disks)
4520 for (i = 0; i < conf->geo.raid_disks; i++) {
4521 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4522 if (!rdev || test_bit(Faulty, &rdev->flags))
4524 else if (!test_bit(In_sync, &rdev->flags)) {
4525 /* If reshape is increasing the number of devices,
4526 * this section has already been recovered, so
4527 * it doesn't contribute to degraded.
4530 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4535 if (degraded2 > degraded)
4540 static int raid10_start_reshape(struct mddev *mddev)
4542 /* A 'reshape' has been requested. This commits
4543 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4544 * This also checks if there are enough spares and adds them
4546 * We currently require enough spares to make the final
4547 * array non-degraded. We also require that the difference
4548 * between old and new data_offset - on each device - is
4549 * enough that we never risk over-writing.
4552 unsigned long before_length, after_length;
4553 sector_t min_offset_diff = 0;
4556 struct r10conf *conf = mddev->private;
4557 struct md_rdev *rdev;
4561 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4564 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4567 before_length = ((1 << conf->prev.chunk_shift) *
4568 conf->prev.far_copies);
4569 after_length = ((1 << conf->geo.chunk_shift) *
4570 conf->geo.far_copies);
4572 rdev_for_each(rdev, mddev) {
4573 if (!test_bit(In_sync, &rdev->flags)
4574 && !test_bit(Faulty, &rdev->flags))
4576 if (rdev->raid_disk >= 0) {
4577 long long diff = (rdev->new_data_offset
4578 - rdev->data_offset);
4579 if (!mddev->reshape_backwards)
4583 if (first || diff < min_offset_diff)
4584 min_offset_diff = diff;
4589 if (max(before_length, after_length) > min_offset_diff)
4592 if (spares < mddev->delta_disks)
4595 conf->offset_diff = min_offset_diff;
4596 spin_lock_irq(&conf->device_lock);
4597 if (conf->mirrors_new) {
4598 memcpy(conf->mirrors_new, conf->mirrors,
4599 sizeof(struct raid10_info)*conf->prev.raid_disks);
4601 kfree(conf->mirrors_old);
4602 conf->mirrors_old = conf->mirrors;
4603 conf->mirrors = conf->mirrors_new;
4604 conf->mirrors_new = NULL;
4606 setup_geo(&conf->geo, mddev, geo_start);
4608 if (mddev->reshape_backwards) {
4609 sector_t size = raid10_size(mddev, 0, 0);
4610 if (size < mddev->array_sectors) {
4611 spin_unlock_irq(&conf->device_lock);
4612 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4616 mddev->resync_max_sectors = size;
4617 conf->reshape_progress = size;
4619 conf->reshape_progress = 0;
4620 conf->reshape_safe = conf->reshape_progress;
4621 spin_unlock_irq(&conf->device_lock);
4623 if (mddev->delta_disks && mddev->bitmap) {
4624 struct mdp_superblock_1 *sb = NULL;
4625 sector_t oldsize, newsize;
4627 oldsize = raid10_size(mddev, 0, 0);
4628 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4630 if (!mddev_is_clustered(mddev)) {
4631 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4638 rdev_for_each(rdev, mddev) {
4639 if (rdev->raid_disk > -1 &&
4640 !test_bit(Faulty, &rdev->flags))
4641 sb = page_address(rdev->sb_page);
4645 * some node is already performing reshape, and no need to
4646 * call md_bitmap_resize again since it should be called when
4647 * receiving BITMAP_RESIZE msg
4649 if ((sb && (le32_to_cpu(sb->feature_map) &
4650 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4653 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4657 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4659 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4664 if (mddev->delta_disks > 0) {
4665 rdev_for_each(rdev, mddev)
4666 if (rdev->raid_disk < 0 &&
4667 !test_bit(Faulty, &rdev->flags)) {
4668 if (raid10_add_disk(mddev, rdev) == 0) {
4669 if (rdev->raid_disk >=
4670 conf->prev.raid_disks)
4671 set_bit(In_sync, &rdev->flags);
4673 rdev->recovery_offset = 0;
4675 /* Failure here is OK */
4676 sysfs_link_rdev(mddev, rdev);
4678 } else if (rdev->raid_disk >= conf->prev.raid_disks
4679 && !test_bit(Faulty, &rdev->flags)) {
4680 /* This is a spare that was manually added */
4681 set_bit(In_sync, &rdev->flags);
4684 /* When a reshape changes the number of devices,
4685 * ->degraded is measured against the larger of the
4686 * pre and post numbers.
4688 spin_lock_irq(&conf->device_lock);
4689 mddev->degraded = calc_degraded(conf);
4690 spin_unlock_irq(&conf->device_lock);
4691 mddev->raid_disks = conf->geo.raid_disks;
4692 mddev->reshape_position = conf->reshape_progress;
4693 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4695 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4696 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4697 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4698 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4699 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4701 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4703 if (!mddev->sync_thread) {
4707 conf->reshape_checkpoint = jiffies;
4708 md_wakeup_thread(mddev->sync_thread);
4713 mddev->recovery = 0;
4714 spin_lock_irq(&conf->device_lock);
4715 conf->geo = conf->prev;
4716 mddev->raid_disks = conf->geo.raid_disks;
4717 rdev_for_each(rdev, mddev)
4718 rdev->new_data_offset = rdev->data_offset;
4720 conf->reshape_progress = MaxSector;
4721 conf->reshape_safe = MaxSector;
4722 mddev->reshape_position = MaxSector;
4723 spin_unlock_irq(&conf->device_lock);
4727 /* Calculate the last device-address that could contain
4728 * any block from the chunk that includes the array-address 's'
4729 * and report the next address.
4730 * i.e. the address returned will be chunk-aligned and after
4731 * any data that is in the chunk containing 's'.
4733 static sector_t last_dev_address(sector_t s, struct geom *geo)
4735 s = (s | geo->chunk_mask) + 1;
4736 s >>= geo->chunk_shift;
4737 s *= geo->near_copies;
4738 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4739 s *= geo->far_copies;
4740 s <<= geo->chunk_shift;
4744 /* Calculate the first device-address that could contain
4745 * any block from the chunk that includes the array-address 's'.
4746 * This too will be the start of a chunk
4748 static sector_t first_dev_address(sector_t s, struct geom *geo)
4750 s >>= geo->chunk_shift;
4751 s *= geo->near_copies;
4752 sector_div(s, geo->raid_disks);
4753 s *= geo->far_copies;
4754 s <<= geo->chunk_shift;
4758 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4761 /* We simply copy at most one chunk (smallest of old and new)
4762 * at a time, possibly less if that exceeds RESYNC_PAGES,
4763 * or we hit a bad block or something.
4764 * This might mean we pause for normal IO in the middle of
4765 * a chunk, but that is not a problem as mddev->reshape_position
4766 * can record any location.
4768 * If we will want to write to a location that isn't
4769 * yet recorded as 'safe' (i.e. in metadata on disk) then
4770 * we need to flush all reshape requests and update the metadata.
4772 * When reshaping forwards (e.g. to more devices), we interpret
4773 * 'safe' as the earliest block which might not have been copied
4774 * down yet. We divide this by previous stripe size and multiply
4775 * by previous stripe length to get lowest device offset that we
4776 * cannot write to yet.
4777 * We interpret 'sector_nr' as an address that we want to write to.
4778 * From this we use last_device_address() to find where we might
4779 * write to, and first_device_address on the 'safe' position.
4780 * If this 'next' write position is after the 'safe' position,
4781 * we must update the metadata to increase the 'safe' position.
4783 * When reshaping backwards, we round in the opposite direction
4784 * and perform the reverse test: next write position must not be
4785 * less than current safe position.
4787 * In all this the minimum difference in data offsets
4788 * (conf->offset_diff - always positive) allows a bit of slack,
4789 * so next can be after 'safe', but not by more than offset_diff
4791 * We need to prepare all the bios here before we start any IO
4792 * to ensure the size we choose is acceptable to all devices.
4793 * The means one for each copy for write-out and an extra one for
4795 * We store the read-in bio in ->master_bio and the others in
4796 * ->devs[x].bio and ->devs[x].repl_bio.
4798 struct r10conf *conf = mddev->private;
4799 struct r10bio *r10_bio;
4800 sector_t next, safe, last;
4804 struct md_rdev *rdev;
4807 struct bio *bio, *read_bio;
4808 int sectors_done = 0;
4809 struct page **pages;
4811 if (sector_nr == 0) {
4812 /* If restarting in the middle, skip the initial sectors */
4813 if (mddev->reshape_backwards &&
4814 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4815 sector_nr = (raid10_size(mddev, 0, 0)
4816 - conf->reshape_progress);
4817 } else if (!mddev->reshape_backwards &&
4818 conf->reshape_progress > 0)
4819 sector_nr = conf->reshape_progress;
4821 mddev->curr_resync_completed = sector_nr;
4822 sysfs_notify_dirent_safe(mddev->sysfs_completed);
4828 /* We don't use sector_nr to track where we are up to
4829 * as that doesn't work well for ->reshape_backwards.
4830 * So just use ->reshape_progress.
4832 if (mddev->reshape_backwards) {
4833 /* 'next' is the earliest device address that we might
4834 * write to for this chunk in the new layout
4836 next = first_dev_address(conf->reshape_progress - 1,
4839 /* 'safe' is the last device address that we might read from
4840 * in the old layout after a restart
4842 safe = last_dev_address(conf->reshape_safe - 1,
4845 if (next + conf->offset_diff < safe)
4848 last = conf->reshape_progress - 1;
4849 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4850 & conf->prev.chunk_mask);
4851 if (sector_nr + RESYNC_SECTORS < last)
4852 sector_nr = last + 1 - RESYNC_SECTORS;
4854 /* 'next' is after the last device address that we
4855 * might write to for this chunk in the new layout
4857 next = last_dev_address(conf->reshape_progress, &conf->geo);
4859 /* 'safe' is the earliest device address that we might
4860 * read from in the old layout after a restart
4862 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4864 /* Need to update metadata if 'next' might be beyond 'safe'
4865 * as that would possibly corrupt data
4867 if (next > safe + conf->offset_diff)
4870 sector_nr = conf->reshape_progress;
4871 last = sector_nr | (conf->geo.chunk_mask
4872 & conf->prev.chunk_mask);
4874 if (sector_nr + RESYNC_SECTORS <= last)
4875 last = sector_nr + RESYNC_SECTORS - 1;
4879 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4880 /* Need to update reshape_position in metadata */
4881 wait_barrier(conf, false);
4882 mddev->reshape_position = conf->reshape_progress;
4883 if (mddev->reshape_backwards)
4884 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4885 - conf->reshape_progress;
4887 mddev->curr_resync_completed = conf->reshape_progress;
4888 conf->reshape_checkpoint = jiffies;
4889 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4890 md_wakeup_thread(mddev->thread);
4891 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4892 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4893 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4894 allow_barrier(conf);
4895 return sectors_done;
4897 conf->reshape_safe = mddev->reshape_position;
4898 allow_barrier(conf);
4901 raise_barrier(conf, 0);
4903 /* Now schedule reads for blocks from sector_nr to last */
4904 r10_bio = raid10_alloc_init_r10buf(conf);
4906 raise_barrier(conf, 1);
4907 atomic_set(&r10_bio->remaining, 0);
4908 r10_bio->mddev = mddev;
4909 r10_bio->sector = sector_nr;
4910 set_bit(R10BIO_IsReshape, &r10_bio->state);
4911 r10_bio->sectors = last - sector_nr + 1;
4912 rdev = read_balance(conf, r10_bio, &max_sectors);
4913 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4916 /* Cannot read from here, so need to record bad blocks
4917 * on all the target devices.
4920 mempool_free(r10_bio, &conf->r10buf_pool);
4921 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4922 return sectors_done;
4925 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ,
4926 GFP_KERNEL, &mddev->bio_set);
4927 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4928 + rdev->data_offset);
4929 read_bio->bi_private = r10_bio;
4930 read_bio->bi_end_io = end_reshape_read;
4931 r10_bio->master_bio = read_bio;
4932 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4935 * Broadcast RESYNC message to other nodes, so all nodes would not
4936 * write to the region to avoid conflict.
4938 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4939 struct mdp_superblock_1 *sb = NULL;
4940 int sb_reshape_pos = 0;
4942 conf->cluster_sync_low = sector_nr;
4943 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4944 sb = page_address(rdev->sb_page);
4946 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4948 * Set cluster_sync_low again if next address for array
4949 * reshape is less than cluster_sync_low. Since we can't
4950 * update cluster_sync_low until it has finished reshape.
4952 if (sb_reshape_pos < conf->cluster_sync_low)
4953 conf->cluster_sync_low = sb_reshape_pos;
4956 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4957 conf->cluster_sync_high);
4960 /* Now find the locations in the new layout */
4961 __raid10_find_phys(&conf->geo, r10_bio);
4964 read_bio->bi_next = NULL;
4967 for (s = 0; s < conf->copies*2; s++) {
4969 int d = r10_bio->devs[s/2].devnum;
4970 struct md_rdev *rdev2;
4972 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4973 b = r10_bio->devs[s/2].repl_bio;
4975 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4976 b = r10_bio->devs[s/2].bio;
4978 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4981 bio_set_dev(b, rdev2->bdev);
4982 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4983 rdev2->new_data_offset;
4984 b->bi_end_io = end_reshape_write;
4985 b->bi_opf = REQ_OP_WRITE;
4990 /* Now add as many pages as possible to all of these bios. */
4993 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4994 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4995 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4996 int len = (max_sectors - s) << 9;
4997 if (len > PAGE_SIZE)
4999 for (bio = blist; bio ; bio = bio->bi_next) {
5001 * won't fail because the vec table is big enough
5002 * to hold all these pages
5004 bio_add_page(bio, page, len, 0);
5006 sector_nr += len >> 9;
5007 nr_sectors += len >> 9;
5010 r10_bio->sectors = nr_sectors;
5012 /* Now submit the read */
5013 md_sync_acct_bio(read_bio, r10_bio->sectors);
5014 atomic_inc(&r10_bio->remaining);
5015 read_bio->bi_next = NULL;
5016 submit_bio_noacct(read_bio);
5017 sectors_done += nr_sectors;
5018 if (sector_nr <= last)
5021 lower_barrier(conf);
5023 /* Now that we have done the whole section we can
5024 * update reshape_progress
5026 if (mddev->reshape_backwards)
5027 conf->reshape_progress -= sectors_done;
5029 conf->reshape_progress += sectors_done;
5031 return sectors_done;
5034 static void end_reshape_request(struct r10bio *r10_bio);
5035 static int handle_reshape_read_error(struct mddev *mddev,
5036 struct r10bio *r10_bio);
5037 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
5039 /* Reshape read completed. Hopefully we have a block
5041 * If we got a read error then we do sync 1-page reads from
5042 * elsewhere until we find the data - or give up.
5044 struct r10conf *conf = mddev->private;
5047 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
5048 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
5049 /* Reshape has been aborted */
5050 md_done_sync(mddev, r10_bio->sectors, 0);
5054 /* We definitely have the data in the pages, schedule the
5057 atomic_set(&r10_bio->remaining, 1);
5058 for (s = 0; s < conf->copies*2; s++) {
5060 int d = r10_bio->devs[s/2].devnum;
5061 struct md_rdev *rdev;
5064 rdev = rcu_dereference(conf->mirrors[d].replacement);
5065 b = r10_bio->devs[s/2].repl_bio;
5067 rdev = rcu_dereference(conf->mirrors[d].rdev);
5068 b = r10_bio->devs[s/2].bio;
5070 if (!rdev || test_bit(Faulty, &rdev->flags)) {
5074 atomic_inc(&rdev->nr_pending);
5076 md_sync_acct_bio(b, r10_bio->sectors);
5077 atomic_inc(&r10_bio->remaining);
5079 submit_bio_noacct(b);
5081 end_reshape_request(r10_bio);
5084 static void end_reshape(struct r10conf *conf)
5086 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5089 spin_lock_irq(&conf->device_lock);
5090 conf->prev = conf->geo;
5091 md_finish_reshape(conf->mddev);
5093 conf->reshape_progress = MaxSector;
5094 conf->reshape_safe = MaxSector;
5095 spin_unlock_irq(&conf->device_lock);
5097 if (conf->mddev->queue)
5098 raid10_set_io_opt(conf);
5102 static void raid10_update_reshape_pos(struct mddev *mddev)
5104 struct r10conf *conf = mddev->private;
5107 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5108 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5109 || mddev->reshape_position == MaxSector)
5110 conf->reshape_progress = mddev->reshape_position;
5115 static int handle_reshape_read_error(struct mddev *mddev,
5116 struct r10bio *r10_bio)
5118 /* Use sync reads to get the blocks from somewhere else */
5119 int sectors = r10_bio->sectors;
5120 struct r10conf *conf = mddev->private;
5121 struct r10bio *r10b;
5124 struct page **pages;
5126 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
5128 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5132 /* reshape IOs share pages from .devs[0].bio */
5133 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
5135 r10b->sector = r10_bio->sector;
5136 __raid10_find_phys(&conf->prev, r10b);
5141 int first_slot = slot;
5143 if (s > (PAGE_SIZE >> 9))
5148 int d = r10b->devs[slot].devnum;
5149 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5152 test_bit(Faulty, &rdev->flags) ||
5153 !test_bit(In_sync, &rdev->flags))
5156 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
5157 atomic_inc(&rdev->nr_pending);
5159 success = sync_page_io(rdev,
5163 REQ_OP_READ, false);
5164 rdev_dec_pending(rdev, mddev);
5170 if (slot >= conf->copies)
5172 if (slot == first_slot)
5177 /* couldn't read this block, must give up */
5178 set_bit(MD_RECOVERY_INTR,
5190 static void end_reshape_write(struct bio *bio)
5192 struct r10bio *r10_bio = get_resync_r10bio(bio);
5193 struct mddev *mddev = r10_bio->mddev;
5194 struct r10conf *conf = mddev->private;
5198 struct md_rdev *rdev = NULL;
5200 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5202 rdev = conf->mirrors[d].replacement;
5205 rdev = conf->mirrors[d].rdev;
5208 if (bio->bi_status) {
5209 /* FIXME should record badblock */
5210 md_error(mddev, rdev);
5213 rdev_dec_pending(rdev, mddev);
5214 end_reshape_request(r10_bio);
5217 static void end_reshape_request(struct r10bio *r10_bio)
5219 if (!atomic_dec_and_test(&r10_bio->remaining))
5221 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5222 bio_put(r10_bio->master_bio);
5226 static void raid10_finish_reshape(struct mddev *mddev)
5228 struct r10conf *conf = mddev->private;
5230 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5233 if (mddev->delta_disks > 0) {
5234 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5235 mddev->recovery_cp = mddev->resync_max_sectors;
5236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5238 mddev->resync_max_sectors = mddev->array_sectors;
5242 for (d = conf->geo.raid_disks ;
5243 d < conf->geo.raid_disks - mddev->delta_disks;
5245 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5247 clear_bit(In_sync, &rdev->flags);
5248 rdev = rcu_dereference(conf->mirrors[d].replacement);
5250 clear_bit(In_sync, &rdev->flags);
5254 mddev->layout = mddev->new_layout;
5255 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5256 mddev->reshape_position = MaxSector;
5257 mddev->delta_disks = 0;
5258 mddev->reshape_backwards = 0;
5261 static struct md_personality raid10_personality =
5265 .owner = THIS_MODULE,
5266 .make_request = raid10_make_request,
5268 .free = raid10_free,
5269 .status = raid10_status,
5270 .error_handler = raid10_error,
5271 .hot_add_disk = raid10_add_disk,
5272 .hot_remove_disk= raid10_remove_disk,
5273 .spare_active = raid10_spare_active,
5274 .sync_request = raid10_sync_request,
5275 .quiesce = raid10_quiesce,
5276 .size = raid10_size,
5277 .resize = raid10_resize,
5278 .takeover = raid10_takeover,
5279 .check_reshape = raid10_check_reshape,
5280 .start_reshape = raid10_start_reshape,
5281 .finish_reshape = raid10_finish_reshape,
5282 .update_reshape_pos = raid10_update_reshape_pos,
5285 static int __init raid_init(void)
5287 return register_md_personality(&raid10_personality);
5290 static void raid_exit(void)
5292 unregister_md_personality(&raid10_personality);
5295 module_init(raid_init);
5296 module_exit(raid_exit);
5297 MODULE_LICENSE("GPL");
5298 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
5299 MODULE_ALIAS("md-personality-9"); /* RAID10 */
5300 MODULE_ALIAS("md-raid10");
5301 MODULE_ALIAS("md-level-10");