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);
955 BUG_ON(force && !conf->barrier);
957 /* Wait until no block IO is waiting (unless 'force') */
958 wait_event_barrier(conf, force || !conf->nr_waiting);
960 /* block any new IO from starting */
961 WRITE_ONCE(conf->barrier, conf->barrier + 1);
963 /* Now wait for all pending IO to complete */
964 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) &&
965 conf->barrier < RESYNC_DEPTH);
967 write_sequnlock_irq(&conf->resync_lock);
970 static void lower_barrier(struct r10conf *conf)
974 write_seqlock_irqsave(&conf->resync_lock, flags);
975 WRITE_ONCE(conf->barrier, conf->barrier - 1);
976 write_sequnlock_irqrestore(&conf->resync_lock, flags);
977 wake_up(&conf->wait_barrier);
980 static bool stop_waiting_barrier(struct r10conf *conf)
982 struct bio_list *bio_list = current->bio_list;
984 /* barrier is dropped */
989 * If there are already pending requests (preventing the barrier from
990 * rising completely), and the pre-process bio queue isn't empty, then
991 * don't wait, as we need to empty that queue to get the nr_pending
994 if (atomic_read(&conf->nr_pending) && bio_list &&
995 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1])))
999 * move on if io is issued from raid10d(), nr_pending is not released
1000 * from original io(see handle_read_error()). All raise barrier is
1001 * blocked until this io is done.
1003 if (conf->mddev->thread->tsk == current) {
1004 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0);
1011 static bool wait_barrier_nolock(struct r10conf *conf)
1013 unsigned int seq = read_seqbegin(&conf->resync_lock);
1015 if (READ_ONCE(conf->barrier))
1018 atomic_inc(&conf->nr_pending);
1019 if (!read_seqretry(&conf->resync_lock, seq))
1022 if (atomic_dec_and_test(&conf->nr_pending))
1023 wake_up_barrier(conf);
1028 static bool wait_barrier(struct r10conf *conf, bool nowait)
1032 if (wait_barrier_nolock(conf))
1035 write_seqlock_irq(&conf->resync_lock);
1036 if (conf->barrier) {
1037 /* Return false when nowait flag is set */
1042 raid10_log(conf->mddev, "wait barrier");
1043 wait_event_barrier(conf, stop_waiting_barrier(conf));
1046 if (!conf->nr_waiting)
1047 wake_up(&conf->wait_barrier);
1049 /* Only increment nr_pending when we wait */
1051 atomic_inc(&conf->nr_pending);
1052 write_sequnlock_irq(&conf->resync_lock);
1056 static void allow_barrier(struct r10conf *conf)
1058 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1059 (conf->array_freeze_pending))
1060 wake_up_barrier(conf);
1063 static void freeze_array(struct r10conf *conf, int extra)
1065 /* stop syncio and normal IO and wait for everything to
1067 * We increment barrier and nr_waiting, and then
1068 * wait until nr_pending match nr_queued+extra
1069 * This is called in the context of one normal IO request
1070 * that has failed. Thus any sync request that might be pending
1071 * will be blocked by nr_pending, and we need to wait for
1072 * pending IO requests to complete or be queued for re-try.
1073 * Thus the number queued (nr_queued) plus this request (extra)
1074 * must match the number of pending IOs (nr_pending) before
1077 write_seqlock_irq(&conf->resync_lock);
1078 conf->array_freeze_pending++;
1079 WRITE_ONCE(conf->barrier, conf->barrier + 1);
1081 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) ==
1082 conf->nr_queued + extra, flush_pending_writes(conf));
1083 conf->array_freeze_pending--;
1084 write_sequnlock_irq(&conf->resync_lock);
1087 static void unfreeze_array(struct r10conf *conf)
1089 /* reverse the effect of the freeze */
1090 write_seqlock_irq(&conf->resync_lock);
1091 WRITE_ONCE(conf->barrier, conf->barrier - 1);
1093 wake_up(&conf->wait_barrier);
1094 write_sequnlock_irq(&conf->resync_lock);
1097 static sector_t choose_data_offset(struct r10bio *r10_bio,
1098 struct md_rdev *rdev)
1100 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1101 test_bit(R10BIO_Previous, &r10_bio->state))
1102 return rdev->data_offset;
1104 return rdev->new_data_offset;
1107 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1109 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb);
1110 struct mddev *mddev = plug->cb.data;
1111 struct r10conf *conf = mddev->private;
1114 if (from_schedule || current->bio_list) {
1115 spin_lock_irq(&conf->device_lock);
1116 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1117 spin_unlock_irq(&conf->device_lock);
1118 wake_up(&conf->wait_barrier);
1119 md_wakeup_thread(mddev->thread);
1124 /* we aren't scheduling, so we can do the write-out directly. */
1125 bio = bio_list_get(&plug->pending);
1126 md_bitmap_unplug(mddev->bitmap);
1127 wake_up(&conf->wait_barrier);
1129 while (bio) { /* submit pending writes */
1130 struct bio *next = bio->bi_next;
1131 struct md_rdev *rdev = (void*)bio->bi_bdev;
1132 bio->bi_next = NULL;
1133 bio_set_dev(bio, rdev->bdev);
1134 if (test_bit(Faulty, &rdev->flags)) {
1136 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1137 !bdev_max_discard_sectors(bio->bi_bdev)))
1138 /* Just ignore it */
1141 submit_bio_noacct(bio);
1148 * 1. Register the new request and wait if the reconstruction thread has put
1149 * up a bar for new requests. Continue immediately if no resync is active
1151 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1153 static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf,
1154 struct bio *bio, sector_t sectors)
1156 /* Bail out if REQ_NOWAIT is set for the bio */
1157 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) {
1158 bio_wouldblock_error(bio);
1161 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1162 bio->bi_iter.bi_sector < conf->reshape_progress &&
1163 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1164 allow_barrier(conf);
1165 if (bio->bi_opf & REQ_NOWAIT) {
1166 bio_wouldblock_error(bio);
1169 raid10_log(conf->mddev, "wait reshape");
1170 wait_event(conf->wait_barrier,
1171 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1172 conf->reshape_progress >= bio->bi_iter.bi_sector +
1174 wait_barrier(conf, false);
1179 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1180 struct r10bio *r10_bio)
1182 struct r10conf *conf = mddev->private;
1183 struct bio *read_bio;
1184 const enum req_op op = bio_op(bio);
1185 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1187 struct md_rdev *rdev;
1188 char b[BDEVNAME_SIZE];
1189 int slot = r10_bio->read_slot;
1190 struct md_rdev *err_rdev = NULL;
1191 gfp_t gfp = GFP_NOIO;
1193 if (slot >= 0 && r10_bio->devs[slot].rdev) {
1195 * This is an error retry, but we cannot
1196 * safely dereference the rdev in the r10_bio,
1197 * we must use the one in conf.
1198 * If it has already been disconnected (unlikely)
1199 * we lose the device name in error messages.
1203 * As we are blocking raid10, it is a little safer to
1206 gfp = GFP_NOIO | __GFP_HIGH;
1209 disk = r10_bio->devs[slot].devnum;
1210 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1212 snprintf(b, sizeof(b), "%pg", err_rdev->bdev);
1215 /* This never gets dereferenced */
1216 err_rdev = r10_bio->devs[slot].rdev;
1221 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors))
1223 rdev = read_balance(conf, r10_bio, &max_sectors);
1226 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1228 (unsigned long long)r10_bio->sector);
1230 raid_end_bio_io(r10_bio);
1234 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
1237 (unsigned long long)r10_bio->sector);
1238 if (max_sectors < bio_sectors(bio)) {
1239 struct bio *split = bio_split(bio, max_sectors,
1240 gfp, &conf->bio_split);
1241 bio_chain(split, bio);
1242 allow_barrier(conf);
1243 submit_bio_noacct(bio);
1244 wait_barrier(conf, false);
1246 r10_bio->master_bio = bio;
1247 r10_bio->sectors = max_sectors;
1249 slot = r10_bio->read_slot;
1251 if (!r10_bio->start_time &&
1252 blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
1253 r10_bio->start_time = bio_start_io_acct(bio);
1254 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set);
1256 r10_bio->devs[slot].bio = read_bio;
1257 r10_bio->devs[slot].rdev = rdev;
1259 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1260 choose_data_offset(r10_bio, rdev);
1261 read_bio->bi_end_io = raid10_end_read_request;
1262 bio_set_op_attrs(read_bio, op, do_sync);
1263 if (test_bit(FailFast, &rdev->flags) &&
1264 test_bit(R10BIO_FailFast, &r10_bio->state))
1265 read_bio->bi_opf |= MD_FAILFAST;
1266 read_bio->bi_private = r10_bio;
1269 trace_block_bio_remap(read_bio, disk_devt(mddev->gendisk),
1271 submit_bio_noacct(read_bio);
1275 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1276 struct bio *bio, bool replacement,
1279 const enum req_op op = bio_op(bio);
1280 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1281 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA;
1282 unsigned long flags;
1283 struct blk_plug_cb *cb;
1284 struct raid1_plug_cb *plug = NULL;
1285 struct r10conf *conf = mddev->private;
1286 struct md_rdev *rdev;
1287 int devnum = r10_bio->devs[n_copy].devnum;
1291 rdev = conf->mirrors[devnum].replacement;
1293 /* Replacement just got moved to main 'rdev' */
1295 rdev = conf->mirrors[devnum].rdev;
1298 rdev = conf->mirrors[devnum].rdev;
1300 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set);
1302 r10_bio->devs[n_copy].repl_bio = mbio;
1304 r10_bio->devs[n_copy].bio = mbio;
1306 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1307 choose_data_offset(r10_bio, rdev));
1308 mbio->bi_end_io = raid10_end_write_request;
1309 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1310 if (!replacement && test_bit(FailFast,
1311 &conf->mirrors[devnum].rdev->flags)
1312 && enough(conf, devnum))
1313 mbio->bi_opf |= MD_FAILFAST;
1314 mbio->bi_private = r10_bio;
1316 if (conf->mddev->gendisk)
1317 trace_block_bio_remap(mbio, disk_devt(conf->mddev->gendisk),
1319 /* flush_pending_writes() needs access to the rdev so...*/
1320 mbio->bi_bdev = (void *)rdev;
1322 atomic_inc(&r10_bio->remaining);
1324 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1326 plug = container_of(cb, struct raid1_plug_cb, cb);
1330 bio_list_add(&plug->pending, mbio);
1332 spin_lock_irqsave(&conf->device_lock, flags);
1333 bio_list_add(&conf->pending_bio_list, mbio);
1334 spin_unlock_irqrestore(&conf->device_lock, flags);
1335 md_wakeup_thread(mddev->thread);
1339 static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1342 struct r10conf *conf = mddev->private;
1343 struct md_rdev *blocked_rdev;
1346 blocked_rdev = NULL;
1348 for (i = 0; i < conf->copies; i++) {
1349 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1350 struct md_rdev *rrdev = rcu_dereference(
1351 conf->mirrors[i].replacement);
1354 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1355 atomic_inc(&rdev->nr_pending);
1356 blocked_rdev = rdev;
1359 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1360 atomic_inc(&rrdev->nr_pending);
1361 blocked_rdev = rrdev;
1365 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1367 sector_t dev_sector = r10_bio->devs[i].addr;
1372 * Discard request doesn't care the write result
1373 * so it doesn't need to wait blocked disk here.
1375 if (!r10_bio->sectors)
1378 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1379 &first_bad, &bad_sectors);
1382 * Mustn't write here until the bad block
1385 atomic_inc(&rdev->nr_pending);
1386 set_bit(BlockedBadBlocks, &rdev->flags);
1387 blocked_rdev = rdev;
1394 if (unlikely(blocked_rdev)) {
1395 /* Have to wait for this device to get unblocked, then retry */
1396 allow_barrier(conf);
1397 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1398 __func__, blocked_rdev->raid_disk);
1399 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1400 wait_barrier(conf, false);
1405 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1406 struct r10bio *r10_bio)
1408 struct r10conf *conf = mddev->private;
1413 if ((mddev_is_clustered(mddev) &&
1414 md_cluster_ops->area_resyncing(mddev, WRITE,
1415 bio->bi_iter.bi_sector,
1416 bio_end_sector(bio)))) {
1418 /* Bail out if REQ_NOWAIT is set for the bio */
1419 if (bio->bi_opf & REQ_NOWAIT) {
1420 bio_wouldblock_error(bio);
1424 prepare_to_wait(&conf->wait_barrier,
1426 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1427 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1431 finish_wait(&conf->wait_barrier, &w);
1434 sectors = r10_bio->sectors;
1435 if (!regular_request_wait(mddev, conf, bio, sectors))
1437 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1438 (mddev->reshape_backwards
1439 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1440 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1441 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1442 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1443 /* Need to update reshape_position in metadata */
1444 mddev->reshape_position = conf->reshape_progress;
1445 set_mask_bits(&mddev->sb_flags, 0,
1446 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1447 md_wakeup_thread(mddev->thread);
1448 if (bio->bi_opf & REQ_NOWAIT) {
1449 allow_barrier(conf);
1450 bio_wouldblock_error(bio);
1453 raid10_log(conf->mddev, "wait reshape metadata");
1454 wait_event(mddev->sb_wait,
1455 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1457 conf->reshape_safe = mddev->reshape_position;
1460 /* first select target devices under rcu_lock and
1461 * inc refcount on their rdev. Record them by setting
1463 * If there are known/acknowledged bad blocks on any device
1464 * on which we have seen a write error, we want to avoid
1465 * writing to those blocks. This potentially requires several
1466 * writes to write around the bad blocks. Each set of writes
1467 * gets its own r10_bio with a set of bios attached.
1470 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1471 raid10_find_phys(conf, r10_bio);
1473 wait_blocked_dev(mddev, r10_bio);
1476 max_sectors = r10_bio->sectors;
1478 for (i = 0; i < conf->copies; i++) {
1479 int d = r10_bio->devs[i].devnum;
1480 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1481 struct md_rdev *rrdev = rcu_dereference(
1482 conf->mirrors[d].replacement);
1485 if (rdev && (test_bit(Faulty, &rdev->flags)))
1487 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1490 r10_bio->devs[i].bio = NULL;
1491 r10_bio->devs[i].repl_bio = NULL;
1493 if (!rdev && !rrdev) {
1494 set_bit(R10BIO_Degraded, &r10_bio->state);
1497 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1499 sector_t dev_sector = r10_bio->devs[i].addr;
1503 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1504 &first_bad, &bad_sectors);
1505 if (is_bad && first_bad <= dev_sector) {
1506 /* Cannot write here at all */
1507 bad_sectors -= (dev_sector - first_bad);
1508 if (bad_sectors < max_sectors)
1509 /* Mustn't write more than bad_sectors
1510 * to other devices yet
1512 max_sectors = bad_sectors;
1513 /* We don't set R10BIO_Degraded as that
1514 * only applies if the disk is missing,
1515 * so it might be re-added, and we want to
1516 * know to recover this chunk.
1517 * In this case the device is here, and the
1518 * fact that this chunk is not in-sync is
1519 * recorded in the bad block log.
1524 int good_sectors = first_bad - dev_sector;
1525 if (good_sectors < max_sectors)
1526 max_sectors = good_sectors;
1530 r10_bio->devs[i].bio = bio;
1531 atomic_inc(&rdev->nr_pending);
1534 r10_bio->devs[i].repl_bio = bio;
1535 atomic_inc(&rrdev->nr_pending);
1540 if (max_sectors < r10_bio->sectors)
1541 r10_bio->sectors = max_sectors;
1543 if (r10_bio->sectors < bio_sectors(bio)) {
1544 struct bio *split = bio_split(bio, r10_bio->sectors,
1545 GFP_NOIO, &conf->bio_split);
1546 bio_chain(split, bio);
1547 allow_barrier(conf);
1548 submit_bio_noacct(bio);
1549 wait_barrier(conf, false);
1551 r10_bio->master_bio = bio;
1554 if (blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
1555 r10_bio->start_time = bio_start_io_acct(bio);
1556 atomic_set(&r10_bio->remaining, 1);
1557 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1559 for (i = 0; i < conf->copies; i++) {
1560 if (r10_bio->devs[i].bio)
1561 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1562 if (r10_bio->devs[i].repl_bio)
1563 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1565 one_write_done(r10_bio);
1568 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1570 struct r10conf *conf = mddev->private;
1571 struct r10bio *r10_bio;
1573 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1575 r10_bio->master_bio = bio;
1576 r10_bio->sectors = sectors;
1578 r10_bio->mddev = mddev;
1579 r10_bio->sector = bio->bi_iter.bi_sector;
1581 r10_bio->read_slot = -1;
1582 r10_bio->start_time = 0;
1583 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1584 conf->geo.raid_disks);
1586 if (bio_data_dir(bio) == READ)
1587 raid10_read_request(mddev, bio, r10_bio);
1589 raid10_write_request(mddev, bio, r10_bio);
1592 static void raid_end_discard_bio(struct r10bio *r10bio)
1594 struct r10conf *conf = r10bio->mddev->private;
1595 struct r10bio *first_r10bio;
1597 while (atomic_dec_and_test(&r10bio->remaining)) {
1599 allow_barrier(conf);
1601 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1602 first_r10bio = (struct r10bio *)r10bio->master_bio;
1603 free_r10bio(r10bio);
1604 r10bio = first_r10bio;
1606 md_write_end(r10bio->mddev);
1607 bio_endio(r10bio->master_bio);
1608 free_r10bio(r10bio);
1614 static void raid10_end_discard_request(struct bio *bio)
1616 struct r10bio *r10_bio = bio->bi_private;
1617 struct r10conf *conf = r10_bio->mddev->private;
1618 struct md_rdev *rdev = NULL;
1623 * We don't care the return value of discard bio
1625 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1626 set_bit(R10BIO_Uptodate, &r10_bio->state);
1628 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1630 rdev = conf->mirrors[dev].replacement;
1633 * raid10_remove_disk uses smp_mb to make sure rdev is set to
1634 * replacement before setting replacement to NULL. It can read
1635 * rdev first without barrier protect even replacment is NULL
1638 rdev = conf->mirrors[dev].rdev;
1641 raid_end_discard_bio(r10_bio);
1642 rdev_dec_pending(rdev, conf->mddev);
1646 * There are some limitations to handle discard bio
1647 * 1st, the discard size is bigger than stripe_size*2.
1648 * 2st, if the discard bio spans reshape progress, we use the old way to
1649 * handle discard bio
1651 static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1653 struct r10conf *conf = mddev->private;
1654 struct geom *geo = &conf->geo;
1655 int far_copies = geo->far_copies;
1656 bool first_copy = true;
1657 struct r10bio *r10_bio, *first_r10bio;
1661 unsigned int stripe_size;
1662 unsigned int stripe_data_disks;
1663 sector_t split_size;
1664 sector_t bio_start, bio_end;
1665 sector_t first_stripe_index, last_stripe_index;
1666 sector_t start_disk_offset;
1667 unsigned int start_disk_index;
1668 sector_t end_disk_offset;
1669 unsigned int end_disk_index;
1670 unsigned int remainder;
1672 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1675 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) {
1676 bio_wouldblock_error(bio);
1679 wait_barrier(conf, false);
1682 * Check reshape again to avoid reshape happens after checking
1683 * MD_RECOVERY_RESHAPE and before wait_barrier
1685 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1688 if (geo->near_copies)
1689 stripe_data_disks = geo->raid_disks / geo->near_copies +
1690 geo->raid_disks % geo->near_copies;
1692 stripe_data_disks = geo->raid_disks;
1694 stripe_size = stripe_data_disks << geo->chunk_shift;
1696 bio_start = bio->bi_iter.bi_sector;
1697 bio_end = bio_end_sector(bio);
1700 * Maybe one discard bio is smaller than strip size or across one
1701 * stripe and discard region is larger than one stripe size. For far
1702 * offset layout, if the discard region is not aligned with stripe
1703 * size, there is hole when we submit discard bio to member disk.
1704 * For simplicity, we only handle discard bio which discard region
1705 * is bigger than stripe_size * 2
1707 if (bio_sectors(bio) < stripe_size*2)
1711 * Keep bio aligned with strip size.
1713 div_u64_rem(bio_start, stripe_size, &remainder);
1715 split_size = stripe_size - remainder;
1716 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1717 bio_chain(split, bio);
1718 allow_barrier(conf);
1719 /* Resend the fist split part */
1720 submit_bio_noacct(split);
1721 wait_barrier(conf, false);
1723 div_u64_rem(bio_end, stripe_size, &remainder);
1725 split_size = bio_sectors(bio) - remainder;
1726 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1727 bio_chain(split, bio);
1728 allow_barrier(conf);
1729 /* Resend the second split part */
1730 submit_bio_noacct(bio);
1732 wait_barrier(conf, false);
1735 bio_start = bio->bi_iter.bi_sector;
1736 bio_end = bio_end_sector(bio);
1739 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1740 * One stripe contains the chunks from all member disk (one chunk from
1741 * one disk at the same HBA address). For layout detail, see 'man md 4'
1743 chunk = bio_start >> geo->chunk_shift;
1744 chunk *= geo->near_copies;
1745 first_stripe_index = chunk;
1746 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1747 if (geo->far_offset)
1748 first_stripe_index *= geo->far_copies;
1749 start_disk_offset = (bio_start & geo->chunk_mask) +
1750 (first_stripe_index << geo->chunk_shift);
1752 chunk = bio_end >> geo->chunk_shift;
1753 chunk *= geo->near_copies;
1754 last_stripe_index = chunk;
1755 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1756 if (geo->far_offset)
1757 last_stripe_index *= geo->far_copies;
1758 end_disk_offset = (bio_end & geo->chunk_mask) +
1759 (last_stripe_index << geo->chunk_shift);
1762 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1763 r10_bio->mddev = mddev;
1765 r10_bio->sectors = 0;
1766 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1767 wait_blocked_dev(mddev, r10_bio);
1770 * For far layout it needs more than one r10bio to cover all regions.
1771 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1772 * to record the discard bio. Other r10bio->master_bio record the first
1773 * r10bio. The first r10bio only release after all other r10bios finish.
1774 * The discard bio returns only first r10bio finishes
1777 r10_bio->master_bio = bio;
1778 set_bit(R10BIO_Discard, &r10_bio->state);
1780 first_r10bio = r10_bio;
1782 r10_bio->master_bio = (struct bio *)first_r10bio;
1785 * first select target devices under rcu_lock and
1786 * inc refcount on their rdev. Record them by setting
1790 for (disk = 0; disk < geo->raid_disks; disk++) {
1791 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1792 struct md_rdev *rrdev = rcu_dereference(
1793 conf->mirrors[disk].replacement);
1795 r10_bio->devs[disk].bio = NULL;
1796 r10_bio->devs[disk].repl_bio = NULL;
1798 if (rdev && (test_bit(Faulty, &rdev->flags)))
1800 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1802 if (!rdev && !rrdev)
1806 r10_bio->devs[disk].bio = bio;
1807 atomic_inc(&rdev->nr_pending);
1810 r10_bio->devs[disk].repl_bio = bio;
1811 atomic_inc(&rrdev->nr_pending);
1816 atomic_set(&r10_bio->remaining, 1);
1817 for (disk = 0; disk < geo->raid_disks; disk++) {
1818 sector_t dev_start, dev_end;
1819 struct bio *mbio, *rbio = NULL;
1822 * Now start to calculate the start and end address for each disk.
1823 * The space between dev_start and dev_end is the discard region.
1825 * For dev_start, it needs to consider three conditions:
1826 * 1st, the disk is before start_disk, you can imagine the disk in
1827 * the next stripe. So the dev_start is the start address of next
1829 * 2st, the disk is after start_disk, it means the disk is at the
1830 * same stripe of first disk
1831 * 3st, the first disk itself, we can use start_disk_offset directly
1833 if (disk < start_disk_index)
1834 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1835 else if (disk > start_disk_index)
1836 dev_start = first_stripe_index * mddev->chunk_sectors;
1838 dev_start = start_disk_offset;
1840 if (disk < end_disk_index)
1841 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1842 else if (disk > end_disk_index)
1843 dev_end = last_stripe_index * mddev->chunk_sectors;
1845 dev_end = end_disk_offset;
1848 * It only handles discard bio which size is >= stripe size, so
1849 * dev_end > dev_start all the time.
1850 * It doesn't need to use rcu lock to get rdev here. We already
1851 * add rdev->nr_pending in the first loop.
1853 if (r10_bio->devs[disk].bio) {
1854 struct md_rdev *rdev = conf->mirrors[disk].rdev;
1855 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1857 mbio->bi_end_io = raid10_end_discard_request;
1858 mbio->bi_private = r10_bio;
1859 r10_bio->devs[disk].bio = mbio;
1860 r10_bio->devs[disk].devnum = disk;
1861 atomic_inc(&r10_bio->remaining);
1862 md_submit_discard_bio(mddev, rdev, mbio,
1863 dev_start + choose_data_offset(r10_bio, rdev),
1864 dev_end - dev_start);
1867 if (r10_bio->devs[disk].repl_bio) {
1868 struct md_rdev *rrdev = conf->mirrors[disk].replacement;
1869 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1871 rbio->bi_end_io = raid10_end_discard_request;
1872 rbio->bi_private = r10_bio;
1873 r10_bio->devs[disk].repl_bio = rbio;
1874 r10_bio->devs[disk].devnum = disk;
1875 atomic_inc(&r10_bio->remaining);
1876 md_submit_discard_bio(mddev, rrdev, rbio,
1877 dev_start + choose_data_offset(r10_bio, rrdev),
1878 dev_end - dev_start);
1883 if (!geo->far_offset && --far_copies) {
1884 first_stripe_index += geo->stride >> geo->chunk_shift;
1885 start_disk_offset += geo->stride;
1886 last_stripe_index += geo->stride >> geo->chunk_shift;
1887 end_disk_offset += geo->stride;
1888 atomic_inc(&first_r10bio->remaining);
1889 raid_end_discard_bio(r10_bio);
1890 wait_barrier(conf, false);
1894 raid_end_discard_bio(r10_bio);
1898 allow_barrier(conf);
1902 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1904 struct r10conf *conf = mddev->private;
1905 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1906 int chunk_sects = chunk_mask + 1;
1907 int sectors = bio_sectors(bio);
1909 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1910 && md_flush_request(mddev, bio))
1913 if (!md_write_start(mddev, bio))
1916 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1917 if (!raid10_handle_discard(mddev, bio))
1921 * If this request crosses a chunk boundary, we need to split
1924 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1925 sectors > chunk_sects
1926 && (conf->geo.near_copies < conf->geo.raid_disks
1927 || conf->prev.near_copies <
1928 conf->prev.raid_disks)))
1929 sectors = chunk_sects -
1930 (bio->bi_iter.bi_sector &
1932 __make_request(mddev, bio, sectors);
1934 /* In case raid10d snuck in to freeze_array */
1935 wake_up_barrier(conf);
1939 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1941 struct r10conf *conf = mddev->private;
1944 if (conf->geo.near_copies < conf->geo.raid_disks)
1945 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1946 if (conf->geo.near_copies > 1)
1947 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1948 if (conf->geo.far_copies > 1) {
1949 if (conf->geo.far_offset)
1950 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1952 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1953 if (conf->geo.far_set_size != conf->geo.raid_disks)
1954 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1956 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1957 conf->geo.raid_disks - mddev->degraded);
1959 for (i = 0; i < conf->geo.raid_disks; i++) {
1960 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1961 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1964 seq_printf(seq, "]");
1967 /* check if there are enough drives for
1968 * every block to appear on atleast one.
1969 * Don't consider the device numbered 'ignore'
1970 * as we might be about to remove it.
1972 static int _enough(struct r10conf *conf, int previous, int ignore)
1978 disks = conf->prev.raid_disks;
1979 ncopies = conf->prev.near_copies;
1981 disks = conf->geo.raid_disks;
1982 ncopies = conf->geo.near_copies;
1987 int n = conf->copies;
1991 struct md_rdev *rdev;
1992 if (this != ignore &&
1993 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1994 test_bit(In_sync, &rdev->flags))
1996 this = (this+1) % disks;
2000 first = (first + ncopies) % disks;
2001 } while (first != 0);
2008 static int enough(struct r10conf *conf, int ignore)
2010 /* when calling 'enough', both 'prev' and 'geo' must
2012 * This is ensured if ->reconfig_mutex or ->device_lock
2015 return _enough(conf, 0, ignore) &&
2016 _enough(conf, 1, ignore);
2020 * raid10_error() - RAID10 error handler.
2021 * @mddev: affected md device.
2022 * @rdev: member device to fail.
2024 * The routine acknowledges &rdev failure and determines new @mddev state.
2025 * If it failed, then:
2026 * - &MD_BROKEN flag is set in &mddev->flags.
2027 * Otherwise, it must be degraded:
2028 * - recovery is interrupted.
2029 * - &mddev->degraded is bumped.
2031 * @rdev is marked as &Faulty excluding case when array is failed and
2032 * &mddev->fail_last_dev is off.
2034 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
2036 struct r10conf *conf = mddev->private;
2037 unsigned long flags;
2039 spin_lock_irqsave(&conf->device_lock, flags);
2041 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) {
2042 set_bit(MD_BROKEN, &mddev->flags);
2044 if (!mddev->fail_last_dev) {
2045 spin_unlock_irqrestore(&conf->device_lock, flags);
2049 if (test_and_clear_bit(In_sync, &rdev->flags))
2052 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2053 set_bit(Blocked, &rdev->flags);
2054 set_bit(Faulty, &rdev->flags);
2055 set_mask_bits(&mddev->sb_flags, 0,
2056 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2057 spin_unlock_irqrestore(&conf->device_lock, flags);
2058 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
2059 "md/raid10:%s: Operation continuing on %d devices.\n",
2060 mdname(mddev), rdev->bdev,
2061 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
2064 static void print_conf(struct r10conf *conf)
2067 struct md_rdev *rdev;
2069 pr_debug("RAID10 conf printout:\n");
2071 pr_debug("(!conf)\n");
2074 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
2075 conf->geo.raid_disks);
2077 /* This is only called with ->reconfix_mutex held, so
2078 * rcu protection of rdev is not needed */
2079 for (i = 0; i < conf->geo.raid_disks; i++) {
2080 rdev = conf->mirrors[i].rdev;
2082 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
2083 i, !test_bit(In_sync, &rdev->flags),
2084 !test_bit(Faulty, &rdev->flags),
2089 static void close_sync(struct r10conf *conf)
2091 wait_barrier(conf, false);
2092 allow_barrier(conf);
2094 mempool_exit(&conf->r10buf_pool);
2097 static int raid10_spare_active(struct mddev *mddev)
2100 struct r10conf *conf = mddev->private;
2101 struct raid10_info *tmp;
2103 unsigned long flags;
2106 * Find all non-in_sync disks within the RAID10 configuration
2107 * and mark them in_sync
2109 for (i = 0; i < conf->geo.raid_disks; i++) {
2110 tmp = conf->mirrors + i;
2111 if (tmp->replacement
2112 && tmp->replacement->recovery_offset == MaxSector
2113 && !test_bit(Faulty, &tmp->replacement->flags)
2114 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2115 /* Replacement has just become active */
2117 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2120 /* Replaced device not technically faulty,
2121 * but we need to be sure it gets removed
2122 * and never re-added.
2124 set_bit(Faulty, &tmp->rdev->flags);
2125 sysfs_notify_dirent_safe(
2126 tmp->rdev->sysfs_state);
2128 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2129 } else if (tmp->rdev
2130 && tmp->rdev->recovery_offset == MaxSector
2131 && !test_bit(Faulty, &tmp->rdev->flags)
2132 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
2134 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
2137 spin_lock_irqsave(&conf->device_lock, flags);
2138 mddev->degraded -= count;
2139 spin_unlock_irqrestore(&conf->device_lock, flags);
2145 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
2147 struct r10conf *conf = mddev->private;
2151 int last = conf->geo.raid_disks - 1;
2153 if (mddev->recovery_cp < MaxSector)
2154 /* only hot-add to in-sync arrays, as recovery is
2155 * very different from resync
2158 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
2161 if (md_integrity_add_rdev(rdev, mddev))
2164 if (rdev->raid_disk >= 0)
2165 first = last = rdev->raid_disk;
2167 if (rdev->saved_raid_disk >= first &&
2168 rdev->saved_raid_disk < conf->geo.raid_disks &&
2169 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2170 mirror = rdev->saved_raid_disk;
2173 for ( ; mirror <= last ; mirror++) {
2174 struct raid10_info *p = &conf->mirrors[mirror];
2175 if (p->recovery_disabled == mddev->recovery_disabled)
2178 if (!test_bit(WantReplacement, &p->rdev->flags) ||
2179 p->replacement != NULL)
2181 clear_bit(In_sync, &rdev->flags);
2182 set_bit(Replacement, &rdev->flags);
2183 rdev->raid_disk = mirror;
2186 disk_stack_limits(mddev->gendisk, rdev->bdev,
2187 rdev->data_offset << 9);
2189 rcu_assign_pointer(p->replacement, rdev);
2194 disk_stack_limits(mddev->gendisk, rdev->bdev,
2195 rdev->data_offset << 9);
2197 p->head_position = 0;
2198 p->recovery_disabled = mddev->recovery_disabled - 1;
2199 rdev->raid_disk = mirror;
2201 if (rdev->saved_raid_disk != mirror)
2203 rcu_assign_pointer(p->rdev, rdev);
2211 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
2213 struct r10conf *conf = mddev->private;
2215 int number = rdev->raid_disk;
2216 struct md_rdev **rdevp;
2217 struct raid10_info *p;
2220 if (unlikely(number >= mddev->raid_disks))
2222 p = conf->mirrors + number;
2223 if (rdev == p->rdev)
2225 else if (rdev == p->replacement)
2226 rdevp = &p->replacement;
2230 if (test_bit(In_sync, &rdev->flags) ||
2231 atomic_read(&rdev->nr_pending)) {
2235 /* Only remove non-faulty devices if recovery
2238 if (!test_bit(Faulty, &rdev->flags) &&
2239 mddev->recovery_disabled != p->recovery_disabled &&
2240 (!p->replacement || p->replacement == rdev) &&
2241 number < conf->geo.raid_disks &&
2247 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2249 if (atomic_read(&rdev->nr_pending)) {
2250 /* lost the race, try later */
2256 if (p->replacement) {
2257 /* We must have just cleared 'rdev' */
2258 p->rdev = p->replacement;
2259 clear_bit(Replacement, &p->replacement->flags);
2260 smp_mb(); /* Make sure other CPUs may see both as identical
2261 * but will never see neither -- if they are careful.
2263 p->replacement = NULL;
2266 clear_bit(WantReplacement, &rdev->flags);
2267 err = md_integrity_register(mddev);
2275 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
2277 struct r10conf *conf = r10_bio->mddev->private;
2279 if (!bio->bi_status)
2280 set_bit(R10BIO_Uptodate, &r10_bio->state);
2282 /* The write handler will notice the lack of
2283 * R10BIO_Uptodate and record any errors etc
2285 atomic_add(r10_bio->sectors,
2286 &conf->mirrors[d].rdev->corrected_errors);
2288 /* for reconstruct, we always reschedule after a read.
2289 * for resync, only after all reads
2291 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
2292 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2293 atomic_dec_and_test(&r10_bio->remaining)) {
2294 /* we have read all the blocks,
2295 * do the comparison in process context in raid10d
2297 reschedule_retry(r10_bio);
2301 static void end_sync_read(struct bio *bio)
2303 struct r10bio *r10_bio = get_resync_r10bio(bio);
2304 struct r10conf *conf = r10_bio->mddev->private;
2305 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2307 __end_sync_read(r10_bio, bio, d);
2310 static void end_reshape_read(struct bio *bio)
2312 /* reshape read bio isn't allocated from r10buf_pool */
2313 struct r10bio *r10_bio = bio->bi_private;
2315 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2318 static void end_sync_request(struct r10bio *r10_bio)
2320 struct mddev *mddev = r10_bio->mddev;
2322 while (atomic_dec_and_test(&r10_bio->remaining)) {
2323 if (r10_bio->master_bio == NULL) {
2324 /* the primary of several recovery bios */
2325 sector_t s = r10_bio->sectors;
2326 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2327 test_bit(R10BIO_WriteError, &r10_bio->state))
2328 reschedule_retry(r10_bio);
2331 md_done_sync(mddev, s, 1);
2334 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
2335 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2336 test_bit(R10BIO_WriteError, &r10_bio->state))
2337 reschedule_retry(r10_bio);
2345 static void end_sync_write(struct bio *bio)
2347 struct r10bio *r10_bio = get_resync_r10bio(bio);
2348 struct mddev *mddev = r10_bio->mddev;
2349 struct r10conf *conf = mddev->private;
2355 struct md_rdev *rdev = NULL;
2357 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2359 rdev = conf->mirrors[d].replacement;
2361 rdev = conf->mirrors[d].rdev;
2363 if (bio->bi_status) {
2365 md_error(mddev, rdev);
2367 set_bit(WriteErrorSeen, &rdev->flags);
2368 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2369 set_bit(MD_RECOVERY_NEEDED,
2370 &rdev->mddev->recovery);
2371 set_bit(R10BIO_WriteError, &r10_bio->state);
2373 } else if (is_badblock(rdev,
2374 r10_bio->devs[slot].addr,
2376 &first_bad, &bad_sectors))
2377 set_bit(R10BIO_MadeGood, &r10_bio->state);
2379 rdev_dec_pending(rdev, mddev);
2381 end_sync_request(r10_bio);
2385 * Note: sync and recover and handled very differently for raid10
2386 * This code is for resync.
2387 * For resync, we read through virtual addresses and read all blocks.
2388 * If there is any error, we schedule a write. The lowest numbered
2389 * drive is authoritative.
2390 * However requests come for physical address, so we need to map.
2391 * For every physical address there are raid_disks/copies virtual addresses,
2392 * which is always are least one, but is not necessarly an integer.
2393 * This means that a physical address can span multiple chunks, so we may
2394 * have to submit multiple io requests for a single sync request.
2397 * We check if all blocks are in-sync and only write to blocks that
2400 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2402 struct r10conf *conf = mddev->private;
2404 struct bio *tbio, *fbio;
2406 struct page **tpages, **fpages;
2408 atomic_set(&r10_bio->remaining, 1);
2410 /* find the first device with a block */
2411 for (i=0; i<conf->copies; i++)
2412 if (!r10_bio->devs[i].bio->bi_status)
2415 if (i == conf->copies)
2419 fbio = r10_bio->devs[i].bio;
2420 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2421 fbio->bi_iter.bi_idx = 0;
2422 fpages = get_resync_pages(fbio)->pages;
2424 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2425 /* now find blocks with errors */
2426 for (i=0 ; i < conf->copies ; i++) {
2428 struct md_rdev *rdev;
2429 struct resync_pages *rp;
2431 tbio = r10_bio->devs[i].bio;
2433 if (tbio->bi_end_io != end_sync_read)
2438 tpages = get_resync_pages(tbio)->pages;
2439 d = r10_bio->devs[i].devnum;
2440 rdev = conf->mirrors[d].rdev;
2441 if (!r10_bio->devs[i].bio->bi_status) {
2442 /* We know that the bi_io_vec layout is the same for
2443 * both 'first' and 'i', so we just compare them.
2444 * All vec entries are PAGE_SIZE;
2446 int sectors = r10_bio->sectors;
2447 for (j = 0; j < vcnt; j++) {
2448 int len = PAGE_SIZE;
2449 if (sectors < (len / 512))
2450 len = sectors * 512;
2451 if (memcmp(page_address(fpages[j]),
2452 page_address(tpages[j]),
2459 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2460 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2461 /* Don't fix anything. */
2463 } else if (test_bit(FailFast, &rdev->flags)) {
2464 /* Just give up on this device */
2465 md_error(rdev->mddev, rdev);
2468 /* Ok, we need to write this bio, either to correct an
2469 * inconsistency or to correct an unreadable block.
2470 * First we need to fixup bv_offset, bv_len and
2471 * bi_vecs, as the read request might have corrupted these
2473 rp = get_resync_pages(tbio);
2474 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE);
2476 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2478 rp->raid_bio = r10_bio;
2479 tbio->bi_private = rp;
2480 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2481 tbio->bi_end_io = end_sync_write;
2483 bio_copy_data(tbio, fbio);
2485 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2486 atomic_inc(&r10_bio->remaining);
2487 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2489 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2490 tbio->bi_opf |= MD_FAILFAST;
2491 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2492 submit_bio_noacct(tbio);
2495 /* Now write out to any replacement devices
2498 for (i = 0; i < conf->copies; i++) {
2501 tbio = r10_bio->devs[i].repl_bio;
2502 if (!tbio || !tbio->bi_end_io)
2504 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2505 && r10_bio->devs[i].bio != fbio)
2506 bio_copy_data(tbio, fbio);
2507 d = r10_bio->devs[i].devnum;
2508 atomic_inc(&r10_bio->remaining);
2509 md_sync_acct(conf->mirrors[d].replacement->bdev,
2511 submit_bio_noacct(tbio);
2515 if (atomic_dec_and_test(&r10_bio->remaining)) {
2516 md_done_sync(mddev, r10_bio->sectors, 1);
2522 * Now for the recovery code.
2523 * Recovery happens across physical sectors.
2524 * We recover all non-is_sync drives by finding the virtual address of
2525 * each, and then choose a working drive that also has that virt address.
2526 * There is a separate r10_bio for each non-in_sync drive.
2527 * Only the first two slots are in use. The first for reading,
2528 * The second for writing.
2531 static void fix_recovery_read_error(struct r10bio *r10_bio)
2533 /* We got a read error during recovery.
2534 * We repeat the read in smaller page-sized sections.
2535 * If a read succeeds, write it to the new device or record
2536 * a bad block if we cannot.
2537 * If a read fails, record a bad block on both old and
2540 struct mddev *mddev = r10_bio->mddev;
2541 struct r10conf *conf = mddev->private;
2542 struct bio *bio = r10_bio->devs[0].bio;
2544 int sectors = r10_bio->sectors;
2546 int dr = r10_bio->devs[0].devnum;
2547 int dw = r10_bio->devs[1].devnum;
2548 struct page **pages = get_resync_pages(bio)->pages;
2552 struct md_rdev *rdev;
2556 if (s > (PAGE_SIZE>>9))
2559 rdev = conf->mirrors[dr].rdev;
2560 addr = r10_bio->devs[0].addr + sect,
2561 ok = sync_page_io(rdev,
2565 REQ_OP_READ, false);
2567 rdev = conf->mirrors[dw].rdev;
2568 addr = r10_bio->devs[1].addr + sect;
2569 ok = sync_page_io(rdev,
2573 REQ_OP_WRITE, false);
2575 set_bit(WriteErrorSeen, &rdev->flags);
2576 if (!test_and_set_bit(WantReplacement,
2578 set_bit(MD_RECOVERY_NEEDED,
2579 &rdev->mddev->recovery);
2583 /* We don't worry if we cannot set a bad block -
2584 * it really is bad so there is no loss in not
2587 rdev_set_badblocks(rdev, addr, s, 0);
2589 if (rdev != conf->mirrors[dw].rdev) {
2590 /* need bad block on destination too */
2591 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2592 addr = r10_bio->devs[1].addr + sect;
2593 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2595 /* just abort the recovery */
2596 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2599 conf->mirrors[dw].recovery_disabled
2600 = mddev->recovery_disabled;
2601 set_bit(MD_RECOVERY_INTR,
2614 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2616 struct r10conf *conf = mddev->private;
2618 struct bio *wbio = r10_bio->devs[1].bio;
2619 struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2621 /* Need to test wbio2->bi_end_io before we call
2622 * submit_bio_noacct as if the former is NULL,
2623 * the latter is free to free wbio2.
2625 if (wbio2 && !wbio2->bi_end_io)
2628 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2629 fix_recovery_read_error(r10_bio);
2630 if (wbio->bi_end_io)
2631 end_sync_request(r10_bio);
2633 end_sync_request(r10_bio);
2638 * share the pages with the first bio
2639 * and submit the write request
2641 d = r10_bio->devs[1].devnum;
2642 if (wbio->bi_end_io) {
2643 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2644 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2645 submit_bio_noacct(wbio);
2648 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2649 md_sync_acct(conf->mirrors[d].replacement->bdev,
2650 bio_sectors(wbio2));
2651 submit_bio_noacct(wbio2);
2656 * Used by fix_read_error() to decay the per rdev read_errors.
2657 * We halve the read error count for every hour that has elapsed
2658 * since the last recorded read error.
2661 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2664 unsigned long hours_since_last;
2665 unsigned int read_errors = atomic_read(&rdev->read_errors);
2667 cur_time_mon = ktime_get_seconds();
2669 if (rdev->last_read_error == 0) {
2670 /* first time we've seen a read error */
2671 rdev->last_read_error = cur_time_mon;
2675 hours_since_last = (long)(cur_time_mon -
2676 rdev->last_read_error) / 3600;
2678 rdev->last_read_error = cur_time_mon;
2681 * if hours_since_last is > the number of bits in read_errors
2682 * just set read errors to 0. We do this to avoid
2683 * overflowing the shift of read_errors by hours_since_last.
2685 if (hours_since_last >= 8 * sizeof(read_errors))
2686 atomic_set(&rdev->read_errors, 0);
2688 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2691 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2692 int sectors, struct page *page, enum req_op op)
2697 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2698 && (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags)))
2700 if (sync_page_io(rdev, sector, sectors << 9, page, op, false))
2703 if (op == REQ_OP_WRITE) {
2704 set_bit(WriteErrorSeen, &rdev->flags);
2705 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2706 set_bit(MD_RECOVERY_NEEDED,
2707 &rdev->mddev->recovery);
2709 /* need to record an error - either for the block or the device */
2710 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2711 md_error(rdev->mddev, rdev);
2716 * This is a kernel thread which:
2718 * 1. Retries failed read operations on working mirrors.
2719 * 2. Updates the raid superblock when problems encounter.
2720 * 3. Performs writes following reads for array synchronising.
2723 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2725 int sect = 0; /* Offset from r10_bio->sector */
2726 int sectors = r10_bio->sectors;
2727 struct md_rdev *rdev;
2728 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2729 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2731 /* still own a reference to this rdev, so it cannot
2732 * have been cleared recently.
2734 rdev = conf->mirrors[d].rdev;
2736 if (test_bit(Faulty, &rdev->flags))
2737 /* drive has already been failed, just ignore any
2738 more fix_read_error() attempts */
2741 check_decay_read_errors(mddev, rdev);
2742 atomic_inc(&rdev->read_errors);
2743 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2744 pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2745 mdname(mddev), rdev->bdev,
2746 atomic_read(&rdev->read_errors), max_read_errors);
2747 pr_notice("md/raid10:%s: %pg: Failing raid device\n",
2748 mdname(mddev), rdev->bdev);
2749 md_error(mddev, rdev);
2750 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2756 int sl = r10_bio->read_slot;
2760 if (s > (PAGE_SIZE>>9))
2768 d = r10_bio->devs[sl].devnum;
2769 rdev = rcu_dereference(conf->mirrors[d].rdev);
2771 test_bit(In_sync, &rdev->flags) &&
2772 !test_bit(Faulty, &rdev->flags) &&
2773 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2774 &first_bad, &bad_sectors) == 0) {
2775 atomic_inc(&rdev->nr_pending);
2777 success = sync_page_io(rdev,
2778 r10_bio->devs[sl].addr +
2782 REQ_OP_READ, false);
2783 rdev_dec_pending(rdev, mddev);
2789 if (sl == conf->copies)
2791 } while (!success && sl != r10_bio->read_slot);
2795 /* Cannot read from anywhere, just mark the block
2796 * as bad on the first device to discourage future
2799 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2800 rdev = conf->mirrors[dn].rdev;
2802 if (!rdev_set_badblocks(
2804 r10_bio->devs[r10_bio->read_slot].addr
2807 md_error(mddev, rdev);
2808 r10_bio->devs[r10_bio->read_slot].bio
2815 /* write it back and re-read */
2817 while (sl != r10_bio->read_slot) {
2821 d = r10_bio->devs[sl].devnum;
2822 rdev = rcu_dereference(conf->mirrors[d].rdev);
2824 test_bit(Faulty, &rdev->flags) ||
2825 !test_bit(In_sync, &rdev->flags))
2828 atomic_inc(&rdev->nr_pending);
2830 if (r10_sync_page_io(rdev,
2831 r10_bio->devs[sl].addr +
2833 s, conf->tmppage, REQ_OP_WRITE)
2835 /* Well, this device is dead */
2836 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
2838 (unsigned long long)(
2840 choose_data_offset(r10_bio,
2843 pr_notice("md/raid10:%s: %pg: failing drive\n",
2847 rdev_dec_pending(rdev, mddev);
2851 while (sl != r10_bio->read_slot) {
2855 d = r10_bio->devs[sl].devnum;
2856 rdev = rcu_dereference(conf->mirrors[d].rdev);
2858 test_bit(Faulty, &rdev->flags) ||
2859 !test_bit(In_sync, &rdev->flags))
2862 atomic_inc(&rdev->nr_pending);
2864 switch (r10_sync_page_io(rdev,
2865 r10_bio->devs[sl].addr +
2867 s, conf->tmppage, REQ_OP_READ)) {
2869 /* Well, this device is dead */
2870 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
2872 (unsigned long long)(
2874 choose_data_offset(r10_bio, rdev)),
2876 pr_notice("md/raid10:%s: %pg: failing drive\n",
2881 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
2883 (unsigned long long)(
2885 choose_data_offset(r10_bio, rdev)),
2887 atomic_add(s, &rdev->corrected_errors);
2890 rdev_dec_pending(rdev, mddev);
2900 static int narrow_write_error(struct r10bio *r10_bio, int i)
2902 struct bio *bio = r10_bio->master_bio;
2903 struct mddev *mddev = r10_bio->mddev;
2904 struct r10conf *conf = mddev->private;
2905 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2906 /* bio has the data to be written to slot 'i' where
2907 * we just recently had a write error.
2908 * We repeatedly clone the bio and trim down to one block,
2909 * then try the write. Where the write fails we record
2911 * It is conceivable that the bio doesn't exactly align with
2912 * blocks. We must handle this.
2914 * We currently own a reference to the rdev.
2920 int sect_to_write = r10_bio->sectors;
2923 if (rdev->badblocks.shift < 0)
2926 block_sectors = roundup(1 << rdev->badblocks.shift,
2927 bdev_logical_block_size(rdev->bdev) >> 9);
2928 sector = r10_bio->sector;
2929 sectors = ((r10_bio->sector + block_sectors)
2930 & ~(sector_t)(block_sectors - 1))
2933 while (sect_to_write) {
2936 if (sectors > sect_to_write)
2937 sectors = sect_to_write;
2938 /* Write at 'sector' for 'sectors' */
2939 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
2941 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2942 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2943 wbio->bi_iter.bi_sector = wsector +
2944 choose_data_offset(r10_bio, rdev);
2945 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2947 if (submit_bio_wait(wbio) < 0)
2949 ok = rdev_set_badblocks(rdev, wsector,
2954 sect_to_write -= sectors;
2956 sectors = block_sectors;
2961 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2963 int slot = r10_bio->read_slot;
2965 struct r10conf *conf = mddev->private;
2966 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2968 /* we got a read error. Maybe the drive is bad. Maybe just
2969 * the block and we can fix it.
2970 * We freeze all other IO, and try reading the block from
2971 * other devices. When we find one, we re-write
2972 * and check it that fixes the read error.
2973 * This is all done synchronously while the array is
2976 bio = r10_bio->devs[slot].bio;
2978 r10_bio->devs[slot].bio = NULL;
2981 r10_bio->devs[slot].bio = IO_BLOCKED;
2982 else if (!test_bit(FailFast, &rdev->flags)) {
2983 freeze_array(conf, 1);
2984 fix_read_error(conf, mddev, r10_bio);
2985 unfreeze_array(conf);
2987 md_error(mddev, rdev);
2989 rdev_dec_pending(rdev, mddev);
2991 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2993 * allow_barrier after re-submit to ensure no sync io
2994 * can be issued while regular io pending.
2996 allow_barrier(conf);
2999 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
3001 /* Some sort of write request has finished and it
3002 * succeeded in writing where we thought there was a
3003 * bad block. So forget the bad block.
3004 * Or possibly if failed and we need to record
3008 struct md_rdev *rdev;
3010 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
3011 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
3012 for (m = 0; m < conf->copies; m++) {
3013 int dev = r10_bio->devs[m].devnum;
3014 rdev = conf->mirrors[dev].rdev;
3015 if (r10_bio->devs[m].bio == NULL ||
3016 r10_bio->devs[m].bio->bi_end_io == NULL)
3018 if (!r10_bio->devs[m].bio->bi_status) {
3019 rdev_clear_badblocks(
3021 r10_bio->devs[m].addr,
3022 r10_bio->sectors, 0);
3024 if (!rdev_set_badblocks(
3026 r10_bio->devs[m].addr,
3027 r10_bio->sectors, 0))
3028 md_error(conf->mddev, rdev);
3030 rdev = conf->mirrors[dev].replacement;
3031 if (r10_bio->devs[m].repl_bio == NULL ||
3032 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
3035 if (!r10_bio->devs[m].repl_bio->bi_status) {
3036 rdev_clear_badblocks(
3038 r10_bio->devs[m].addr,
3039 r10_bio->sectors, 0);
3041 if (!rdev_set_badblocks(
3043 r10_bio->devs[m].addr,
3044 r10_bio->sectors, 0))
3045 md_error(conf->mddev, rdev);
3051 for (m = 0; m < conf->copies; m++) {
3052 int dev = r10_bio->devs[m].devnum;
3053 struct bio *bio = r10_bio->devs[m].bio;
3054 rdev = conf->mirrors[dev].rdev;
3055 if (bio == IO_MADE_GOOD) {
3056 rdev_clear_badblocks(
3058 r10_bio->devs[m].addr,
3059 r10_bio->sectors, 0);
3060 rdev_dec_pending(rdev, conf->mddev);
3061 } else if (bio != NULL && bio->bi_status) {
3063 if (!narrow_write_error(r10_bio, m)) {
3064 md_error(conf->mddev, rdev);
3065 set_bit(R10BIO_Degraded,
3068 rdev_dec_pending(rdev, conf->mddev);
3070 bio = r10_bio->devs[m].repl_bio;
3071 rdev = conf->mirrors[dev].replacement;
3072 if (rdev && bio == IO_MADE_GOOD) {
3073 rdev_clear_badblocks(
3075 r10_bio->devs[m].addr,
3076 r10_bio->sectors, 0);
3077 rdev_dec_pending(rdev, conf->mddev);
3081 spin_lock_irq(&conf->device_lock);
3082 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
3084 spin_unlock_irq(&conf->device_lock);
3086 * In case freeze_array() is waiting for condition
3087 * nr_pending == nr_queued + extra to be true.
3089 wake_up(&conf->wait_barrier);
3090 md_wakeup_thread(conf->mddev->thread);
3092 if (test_bit(R10BIO_WriteError,
3094 close_write(r10_bio);
3095 raid_end_bio_io(r10_bio);
3100 static void raid10d(struct md_thread *thread)
3102 struct mddev *mddev = thread->mddev;
3103 struct r10bio *r10_bio;
3104 unsigned long flags;
3105 struct r10conf *conf = mddev->private;
3106 struct list_head *head = &conf->retry_list;
3107 struct blk_plug plug;
3109 md_check_recovery(mddev);
3111 if (!list_empty_careful(&conf->bio_end_io_list) &&
3112 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3114 spin_lock_irqsave(&conf->device_lock, flags);
3115 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3116 while (!list_empty(&conf->bio_end_io_list)) {
3117 list_move(conf->bio_end_io_list.prev, &tmp);
3121 spin_unlock_irqrestore(&conf->device_lock, flags);
3122 while (!list_empty(&tmp)) {
3123 r10_bio = list_first_entry(&tmp, struct r10bio,
3125 list_del(&r10_bio->retry_list);
3126 if (mddev->degraded)
3127 set_bit(R10BIO_Degraded, &r10_bio->state);
3129 if (test_bit(R10BIO_WriteError,
3131 close_write(r10_bio);
3132 raid_end_bio_io(r10_bio);
3136 blk_start_plug(&plug);
3139 flush_pending_writes(conf);
3141 spin_lock_irqsave(&conf->device_lock, flags);
3142 if (list_empty(head)) {
3143 spin_unlock_irqrestore(&conf->device_lock, flags);
3146 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
3147 list_del(head->prev);
3149 spin_unlock_irqrestore(&conf->device_lock, flags);
3151 mddev = r10_bio->mddev;
3152 conf = mddev->private;
3153 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3154 test_bit(R10BIO_WriteError, &r10_bio->state))
3155 handle_write_completed(conf, r10_bio);
3156 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3157 reshape_request_write(mddev, r10_bio);
3158 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
3159 sync_request_write(mddev, r10_bio);
3160 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
3161 recovery_request_write(mddev, r10_bio);
3162 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
3163 handle_read_error(mddev, r10_bio);
3168 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
3169 md_check_recovery(mddev);
3171 blk_finish_plug(&plug);
3174 static int init_resync(struct r10conf *conf)
3178 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
3179 BUG_ON(mempool_initialized(&conf->r10buf_pool));
3180 conf->have_replacement = 0;
3181 for (i = 0; i < conf->geo.raid_disks; i++)
3182 if (conf->mirrors[i].replacement)
3183 conf->have_replacement = 1;
3184 ret = mempool_init(&conf->r10buf_pool, buffs,
3185 r10buf_pool_alloc, r10buf_pool_free, conf);
3188 conf->next_resync = 0;
3192 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3194 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
3195 struct rsync_pages *rp;
3200 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3201 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3202 nalloc = conf->copies; /* resync */
3204 nalloc = 2; /* recovery */
3206 for (i = 0; i < nalloc; i++) {
3207 bio = r10bio->devs[i].bio;
3208 rp = bio->bi_private;
3209 bio_reset(bio, NULL, 0);
3210 bio->bi_private = rp;
3211 bio = r10bio->devs[i].repl_bio;
3213 rp = bio->bi_private;
3214 bio_reset(bio, NULL, 0);
3215 bio->bi_private = rp;
3222 * Set cluster_sync_high since we need other nodes to add the
3223 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3225 static void raid10_set_cluster_sync_high(struct r10conf *conf)
3227 sector_t window_size;
3228 int extra_chunk, chunks;
3231 * First, here we define "stripe" as a unit which across
3232 * all member devices one time, so we get chunks by use
3233 * raid_disks / near_copies. Otherwise, if near_copies is
3234 * close to raid_disks, then resync window could increases
3235 * linearly with the increase of raid_disks, which means
3236 * we will suspend a really large IO window while it is not
3237 * necessary. If raid_disks is not divisible by near_copies,
3238 * an extra chunk is needed to ensure the whole "stripe" is
3242 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3243 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3247 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3250 * At least use a 32M window to align with raid1's resync window
3252 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3253 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3255 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3259 * perform a "sync" on one "block"
3261 * We need to make sure that no normal I/O request - particularly write
3262 * requests - conflict with active sync requests.
3264 * This is achieved by tracking pending requests and a 'barrier' concept
3265 * that can be installed to exclude normal IO requests.
3267 * Resync and recovery are handled very differently.
3268 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3270 * For resync, we iterate over virtual addresses, read all copies,
3271 * and update if there are differences. If only one copy is live,
3273 * For recovery, we iterate over physical addresses, read a good
3274 * value for each non-in_sync drive, and over-write.
3276 * So, for recovery we may have several outstanding complex requests for a
3277 * given address, one for each out-of-sync device. We model this by allocating
3278 * a number of r10_bio structures, one for each out-of-sync device.
3279 * As we setup these structures, we collect all bio's together into a list
3280 * which we then process collectively to add pages, and then process again
3281 * to pass to submit_bio_noacct.
3283 * The r10_bio structures are linked using a borrowed master_bio pointer.
3284 * This link is counted in ->remaining. When the r10_bio that points to NULL
3285 * has its remaining count decremented to 0, the whole complex operation
3290 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
3293 struct r10conf *conf = mddev->private;
3294 struct r10bio *r10_bio;
3295 struct bio *biolist = NULL, *bio;
3296 sector_t max_sector, nr_sectors;
3299 sector_t sync_blocks;
3300 sector_t sectors_skipped = 0;
3301 int chunks_skipped = 0;
3302 sector_t chunk_mask = conf->geo.chunk_mask;
3306 * Allow skipping a full rebuild for incremental assembly
3307 * of a clean array, like RAID1 does.
3309 if (mddev->bitmap == NULL &&
3310 mddev->recovery_cp == MaxSector &&
3311 mddev->reshape_position == MaxSector &&
3312 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
3313 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
3314 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
3315 conf->fullsync == 0) {
3317 return mddev->dev_sectors - sector_nr;
3320 if (!mempool_initialized(&conf->r10buf_pool))
3321 if (init_resync(conf))
3325 max_sector = mddev->dev_sectors;
3326 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3327 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3328 max_sector = mddev->resync_max_sectors;
3329 if (sector_nr >= max_sector) {
3330 conf->cluster_sync_low = 0;
3331 conf->cluster_sync_high = 0;
3333 /* If we aborted, we need to abort the
3334 * sync on the 'current' bitmap chucks (there can
3335 * be several when recovering multiple devices).
3336 * as we may have started syncing it but not finished.
3337 * We can find the current address in
3338 * mddev->curr_resync, but for recovery,
3339 * we need to convert that to several
3340 * virtual addresses.
3342 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3348 if (mddev->curr_resync < max_sector) { /* aborted */
3349 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3350 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3352 else for (i = 0; i < conf->geo.raid_disks; i++) {
3354 raid10_find_virt(conf, mddev->curr_resync, i);
3355 md_bitmap_end_sync(mddev->bitmap, sect,
3359 /* completed sync */
3360 if ((!mddev->bitmap || conf->fullsync)
3361 && conf->have_replacement
3362 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3363 /* Completed a full sync so the replacements
3364 * are now fully recovered.
3367 for (i = 0; i < conf->geo.raid_disks; i++) {
3368 struct md_rdev *rdev =
3369 rcu_dereference(conf->mirrors[i].replacement);
3371 rdev->recovery_offset = MaxSector;
3377 md_bitmap_close_sync(mddev->bitmap);
3380 return sectors_skipped;
3383 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3384 return reshape_request(mddev, sector_nr, skipped);
3386 if (chunks_skipped >= conf->geo.raid_disks) {
3387 /* if there has been nothing to do on any drive,
3388 * then there is nothing to do at all..
3391 return (max_sector - sector_nr) + sectors_skipped;
3394 if (max_sector > mddev->resync_max)
3395 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3397 /* make sure whole request will fit in a chunk - if chunks
3400 if (conf->geo.near_copies < conf->geo.raid_disks &&
3401 max_sector > (sector_nr | chunk_mask))
3402 max_sector = (sector_nr | chunk_mask) + 1;
3405 * If there is non-resync activity waiting for a turn, then let it
3406 * though before starting on this new sync request.
3408 if (conf->nr_waiting)
3409 schedule_timeout_uninterruptible(1);
3411 /* Again, very different code for resync and recovery.
3412 * Both must result in an r10bio with a list of bios that
3413 * have bi_end_io, bi_sector, bi_bdev set,
3414 * and bi_private set to the r10bio.
3415 * For recovery, we may actually create several r10bios
3416 * with 2 bios in each, that correspond to the bios in the main one.
3417 * In this case, the subordinate r10bios link back through a
3418 * borrowed master_bio pointer, and the counter in the master
3419 * includes a ref from each subordinate.
3421 /* First, we decide what to do and set ->bi_end_io
3422 * To end_sync_read if we want to read, and
3423 * end_sync_write if we will want to write.
3426 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3427 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3428 /* recovery... the complicated one */
3432 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3438 int need_recover = 0;
3439 struct raid10_info *mirror = &conf->mirrors[i];
3440 struct md_rdev *mrdev, *mreplace;
3443 mrdev = rcu_dereference(mirror->rdev);
3444 mreplace = rcu_dereference(mirror->replacement);
3446 if (mrdev != NULL &&
3447 !test_bit(Faulty, &mrdev->flags) &&
3448 !test_bit(In_sync, &mrdev->flags))
3450 if (mreplace && test_bit(Faulty, &mreplace->flags))
3453 if (!need_recover && !mreplace) {
3459 /* want to reconstruct this device */
3461 sect = raid10_find_virt(conf, sector_nr, i);
3462 if (sect >= mddev->resync_max_sectors) {
3463 /* last stripe is not complete - don't
3464 * try to recover this sector.
3469 /* Unless we are doing a full sync, or a replacement
3470 * we only need to recover the block if it is set in
3473 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3475 if (sync_blocks < max_sync)
3476 max_sync = sync_blocks;
3480 /* yep, skip the sync_blocks here, but don't assume
3481 * that there will never be anything to do here
3483 chunks_skipped = -1;
3487 atomic_inc(&mrdev->nr_pending);
3489 atomic_inc(&mreplace->nr_pending);
3492 r10_bio = raid10_alloc_init_r10buf(conf);
3494 raise_barrier(conf, rb2 != NULL);
3495 atomic_set(&r10_bio->remaining, 0);
3497 r10_bio->master_bio = (struct bio*)rb2;
3499 atomic_inc(&rb2->remaining);
3500 r10_bio->mddev = mddev;
3501 set_bit(R10BIO_IsRecover, &r10_bio->state);
3502 r10_bio->sector = sect;
3504 raid10_find_phys(conf, r10_bio);
3506 /* Need to check if the array will still be
3510 for (j = 0; j < conf->geo.raid_disks; j++) {
3511 struct md_rdev *rdev = rcu_dereference(
3512 conf->mirrors[j].rdev);
3513 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3519 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3520 &sync_blocks, still_degraded);
3523 for (j=0; j<conf->copies;j++) {
3525 int d = r10_bio->devs[j].devnum;
3526 sector_t from_addr, to_addr;
3527 struct md_rdev *rdev =
3528 rcu_dereference(conf->mirrors[d].rdev);
3529 sector_t sector, first_bad;
3532 !test_bit(In_sync, &rdev->flags))
3534 /* This is where we read from */
3536 sector = r10_bio->devs[j].addr;
3538 if (is_badblock(rdev, sector, max_sync,
3539 &first_bad, &bad_sectors)) {
3540 if (first_bad > sector)
3541 max_sync = first_bad - sector;
3543 bad_sectors -= (sector
3545 if (max_sync > bad_sectors)
3546 max_sync = bad_sectors;
3550 bio = r10_bio->devs[0].bio;
3551 bio->bi_next = biolist;
3553 bio->bi_end_io = end_sync_read;
3554 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3555 if (test_bit(FailFast, &rdev->flags))
3556 bio->bi_opf |= MD_FAILFAST;
3557 from_addr = r10_bio->devs[j].addr;
3558 bio->bi_iter.bi_sector = from_addr +
3560 bio_set_dev(bio, rdev->bdev);
3561 atomic_inc(&rdev->nr_pending);
3562 /* and we write to 'i' (if not in_sync) */
3564 for (k=0; k<conf->copies; k++)
3565 if (r10_bio->devs[k].devnum == i)
3567 BUG_ON(k == conf->copies);
3568 to_addr = r10_bio->devs[k].addr;
3569 r10_bio->devs[0].devnum = d;
3570 r10_bio->devs[0].addr = from_addr;
3571 r10_bio->devs[1].devnum = i;
3572 r10_bio->devs[1].addr = to_addr;
3575 bio = r10_bio->devs[1].bio;
3576 bio->bi_next = biolist;
3578 bio->bi_end_io = end_sync_write;
3579 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3580 bio->bi_iter.bi_sector = to_addr
3581 + mrdev->data_offset;
3582 bio_set_dev(bio, mrdev->bdev);
3583 atomic_inc(&r10_bio->remaining);
3585 r10_bio->devs[1].bio->bi_end_io = NULL;
3587 /* and maybe write to replacement */
3588 bio = r10_bio->devs[1].repl_bio;
3590 bio->bi_end_io = NULL;
3591 /* Note: if replace is not NULL, then bio
3592 * cannot be NULL as r10buf_pool_alloc will
3593 * have allocated it.
3597 bio->bi_next = biolist;
3599 bio->bi_end_io = end_sync_write;
3600 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3601 bio->bi_iter.bi_sector = to_addr +
3602 mreplace->data_offset;
3603 bio_set_dev(bio, mreplace->bdev);
3604 atomic_inc(&r10_bio->remaining);
3608 if (j == conf->copies) {
3609 /* Cannot recover, so abort the recovery or
3610 * record a bad block */
3612 /* problem is that there are bad blocks
3613 * on other device(s)
3616 for (k = 0; k < conf->copies; k++)
3617 if (r10_bio->devs[k].devnum == i)
3619 if (!test_bit(In_sync,
3621 && !rdev_set_badblocks(
3623 r10_bio->devs[k].addr,
3627 !rdev_set_badblocks(
3629 r10_bio->devs[k].addr,
3634 if (!test_and_set_bit(MD_RECOVERY_INTR,
3636 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3638 mirror->recovery_disabled
3639 = mddev->recovery_disabled;
3643 atomic_dec(&rb2->remaining);
3645 rdev_dec_pending(mrdev, mddev);
3647 rdev_dec_pending(mreplace, mddev);
3650 rdev_dec_pending(mrdev, mddev);
3652 rdev_dec_pending(mreplace, mddev);
3653 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3654 /* Only want this if there is elsewhere to
3655 * read from. 'j' is currently the first
3659 for (; j < conf->copies; j++) {
3660 int d = r10_bio->devs[j].devnum;
3661 if (conf->mirrors[d].rdev &&
3663 &conf->mirrors[d].rdev->flags))
3667 r10_bio->devs[0].bio->bi_opf
3671 if (biolist == NULL) {
3673 struct r10bio *rb2 = r10_bio;
3674 r10_bio = (struct r10bio*) rb2->master_bio;
3675 rb2->master_bio = NULL;
3681 /* resync. Schedule a read for every block at this virt offset */
3685 * Since curr_resync_completed could probably not update in
3686 * time, and we will set cluster_sync_low based on it.
3687 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3688 * safety reason, which ensures curr_resync_completed is
3689 * updated in bitmap_cond_end_sync.
3691 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3692 mddev_is_clustered(mddev) &&
3693 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
3695 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3696 &sync_blocks, mddev->degraded) &&
3697 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3698 &mddev->recovery)) {
3699 /* We can skip this block */
3701 return sync_blocks + sectors_skipped;
3703 if (sync_blocks < max_sync)
3704 max_sync = sync_blocks;
3705 r10_bio = raid10_alloc_init_r10buf(conf);
3708 r10_bio->mddev = mddev;
3709 atomic_set(&r10_bio->remaining, 0);
3710 raise_barrier(conf, 0);
3711 conf->next_resync = sector_nr;
3713 r10_bio->master_bio = NULL;
3714 r10_bio->sector = sector_nr;
3715 set_bit(R10BIO_IsSync, &r10_bio->state);
3716 raid10_find_phys(conf, r10_bio);
3717 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3719 for (i = 0; i < conf->copies; i++) {
3720 int d = r10_bio->devs[i].devnum;
3721 sector_t first_bad, sector;
3723 struct md_rdev *rdev;
3725 if (r10_bio->devs[i].repl_bio)
3726 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3728 bio = r10_bio->devs[i].bio;
3729 bio->bi_status = BLK_STS_IOERR;
3731 rdev = rcu_dereference(conf->mirrors[d].rdev);
3732 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3736 sector = r10_bio->devs[i].addr;
3737 if (is_badblock(rdev, sector, max_sync,
3738 &first_bad, &bad_sectors)) {
3739 if (first_bad > sector)
3740 max_sync = first_bad - sector;
3742 bad_sectors -= (sector - first_bad);
3743 if (max_sync > bad_sectors)
3744 max_sync = bad_sectors;
3749 atomic_inc(&rdev->nr_pending);
3750 atomic_inc(&r10_bio->remaining);
3751 bio->bi_next = biolist;
3753 bio->bi_end_io = end_sync_read;
3754 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3755 if (test_bit(FailFast, &rdev->flags))
3756 bio->bi_opf |= MD_FAILFAST;
3757 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3758 bio_set_dev(bio, rdev->bdev);
3761 rdev = rcu_dereference(conf->mirrors[d].replacement);
3762 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3766 atomic_inc(&rdev->nr_pending);
3768 /* Need to set up for writing to the replacement */
3769 bio = r10_bio->devs[i].repl_bio;
3770 bio->bi_status = BLK_STS_IOERR;
3772 sector = r10_bio->devs[i].addr;
3773 bio->bi_next = biolist;
3775 bio->bi_end_io = end_sync_write;
3776 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3777 if (test_bit(FailFast, &rdev->flags))
3778 bio->bi_opf |= MD_FAILFAST;
3779 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3780 bio_set_dev(bio, rdev->bdev);
3786 for (i=0; i<conf->copies; i++) {
3787 int d = r10_bio->devs[i].devnum;
3788 if (r10_bio->devs[i].bio->bi_end_io)
3789 rdev_dec_pending(conf->mirrors[d].rdev,
3791 if (r10_bio->devs[i].repl_bio &&
3792 r10_bio->devs[i].repl_bio->bi_end_io)
3794 conf->mirrors[d].replacement,
3804 if (sector_nr + max_sync < max_sector)
3805 max_sector = sector_nr + max_sync;
3808 int len = PAGE_SIZE;
3809 if (sector_nr + (len>>9) > max_sector)
3810 len = (max_sector - sector_nr) << 9;
3813 for (bio= biolist ; bio ; bio=bio->bi_next) {
3814 struct resync_pages *rp = get_resync_pages(bio);
3815 page = resync_fetch_page(rp, page_idx);
3817 * won't fail because the vec table is big enough
3818 * to hold all these pages
3820 bio_add_page(bio, page, len, 0);
3822 nr_sectors += len>>9;
3823 sector_nr += len>>9;
3824 } while (++page_idx < RESYNC_PAGES);
3825 r10_bio->sectors = nr_sectors;
3827 if (mddev_is_clustered(mddev) &&
3828 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3829 /* It is resync not recovery */
3830 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3831 conf->cluster_sync_low = mddev->curr_resync_completed;
3832 raid10_set_cluster_sync_high(conf);
3833 /* Send resync message */
3834 md_cluster_ops->resync_info_update(mddev,
3835 conf->cluster_sync_low,
3836 conf->cluster_sync_high);
3838 } else if (mddev_is_clustered(mddev)) {
3839 /* This is recovery not resync */
3840 sector_t sect_va1, sect_va2;
3841 bool broadcast_msg = false;
3843 for (i = 0; i < conf->geo.raid_disks; i++) {
3845 * sector_nr is a device address for recovery, so we
3846 * need translate it to array address before compare
3847 * with cluster_sync_high.
3849 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3851 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3852 broadcast_msg = true;
3854 * curr_resync_completed is similar as
3855 * sector_nr, so make the translation too.
3857 sect_va2 = raid10_find_virt(conf,
3858 mddev->curr_resync_completed, i);
3860 if (conf->cluster_sync_low == 0 ||
3861 conf->cluster_sync_low > sect_va2)
3862 conf->cluster_sync_low = sect_va2;
3865 if (broadcast_msg) {
3866 raid10_set_cluster_sync_high(conf);
3867 md_cluster_ops->resync_info_update(mddev,
3868 conf->cluster_sync_low,
3869 conf->cluster_sync_high);
3875 biolist = biolist->bi_next;
3877 bio->bi_next = NULL;
3878 r10_bio = get_resync_r10bio(bio);
3879 r10_bio->sectors = nr_sectors;
3881 if (bio->bi_end_io == end_sync_read) {
3882 md_sync_acct_bio(bio, nr_sectors);
3884 submit_bio_noacct(bio);
3888 if (sectors_skipped)
3889 /* pretend they weren't skipped, it makes
3890 * no important difference in this case
3892 md_done_sync(mddev, sectors_skipped, 1);
3894 return sectors_skipped + nr_sectors;
3896 /* There is nowhere to write, so all non-sync
3897 * drives must be failed or in resync, all drives
3898 * have a bad block, so try the next chunk...
3900 if (sector_nr + max_sync < max_sector)
3901 max_sector = sector_nr + max_sync;
3903 sectors_skipped += (max_sector - sector_nr);
3905 sector_nr = max_sector;
3910 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3913 struct r10conf *conf = mddev->private;
3916 raid_disks = min(conf->geo.raid_disks,
3917 conf->prev.raid_disks);
3919 sectors = conf->dev_sectors;
3921 size = sectors >> conf->geo.chunk_shift;
3922 sector_div(size, conf->geo.far_copies);
3923 size = size * raid_disks;
3924 sector_div(size, conf->geo.near_copies);
3926 return size << conf->geo.chunk_shift;
3929 static void calc_sectors(struct r10conf *conf, sector_t size)
3931 /* Calculate the number of sectors-per-device that will
3932 * actually be used, and set conf->dev_sectors and
3936 size = size >> conf->geo.chunk_shift;
3937 sector_div(size, conf->geo.far_copies);
3938 size = size * conf->geo.raid_disks;
3939 sector_div(size, conf->geo.near_copies);
3940 /* 'size' is now the number of chunks in the array */
3941 /* calculate "used chunks per device" */
3942 size = size * conf->copies;
3944 /* We need to round up when dividing by raid_disks to
3945 * get the stride size.
3947 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3949 conf->dev_sectors = size << conf->geo.chunk_shift;
3951 if (conf->geo.far_offset)
3952 conf->geo.stride = 1 << conf->geo.chunk_shift;
3954 sector_div(size, conf->geo.far_copies);
3955 conf->geo.stride = size << conf->geo.chunk_shift;
3959 enum geo_type {geo_new, geo_old, geo_start};
3960 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3963 int layout, chunk, disks;
3966 layout = mddev->layout;
3967 chunk = mddev->chunk_sectors;
3968 disks = mddev->raid_disks - mddev->delta_disks;
3971 layout = mddev->new_layout;
3972 chunk = mddev->new_chunk_sectors;
3973 disks = mddev->raid_disks;
3975 default: /* avoid 'may be unused' warnings */
3976 case geo_start: /* new when starting reshape - raid_disks not
3978 layout = mddev->new_layout;
3979 chunk = mddev->new_chunk_sectors;
3980 disks = mddev->raid_disks + mddev->delta_disks;
3985 if (chunk < (PAGE_SIZE >> 9) ||
3986 !is_power_of_2(chunk))
3989 fc = (layout >> 8) & 255;
3990 fo = layout & (1<<16);
3991 geo->raid_disks = disks;
3992 geo->near_copies = nc;
3993 geo->far_copies = fc;
3994 geo->far_offset = fo;
3995 switch (layout >> 17) {
3996 case 0: /* original layout. simple but not always optimal */
3997 geo->far_set_size = disks;
3999 case 1: /* "improved" layout which was buggy. Hopefully no-one is
4000 * actually using this, but leave code here just in case.*/
4001 geo->far_set_size = disks/fc;
4002 WARN(geo->far_set_size < fc,
4003 "This RAID10 layout does not provide data safety - please backup and create new array\n");
4005 case 2: /* "improved" layout fixed to match documentation */
4006 geo->far_set_size = fc * nc;
4008 default: /* Not a valid layout */
4011 geo->chunk_mask = chunk - 1;
4012 geo->chunk_shift = ffz(~chunk);
4016 static void raid10_free_conf(struct r10conf *conf)
4021 mempool_exit(&conf->r10bio_pool);
4022 kfree(conf->mirrors);
4023 kfree(conf->mirrors_old);
4024 kfree(conf->mirrors_new);
4025 safe_put_page(conf->tmppage);
4026 bioset_exit(&conf->bio_split);
4030 static struct r10conf *setup_conf(struct mddev *mddev)
4032 struct r10conf *conf = NULL;
4037 copies = setup_geo(&geo, mddev, geo_new);
4040 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
4041 mdname(mddev), PAGE_SIZE);
4045 if (copies < 2 || copies > mddev->raid_disks) {
4046 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
4047 mdname(mddev), mddev->new_layout);
4052 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
4056 /* FIXME calc properly */
4057 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
4058 sizeof(struct raid10_info),
4063 conf->tmppage = alloc_page(GFP_KERNEL);
4068 conf->copies = copies;
4069 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
4070 rbio_pool_free, conf);
4074 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
4078 calc_sectors(conf, mddev->dev_sectors);
4079 if (mddev->reshape_position == MaxSector) {
4080 conf->prev = conf->geo;
4081 conf->reshape_progress = MaxSector;
4083 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
4087 conf->reshape_progress = mddev->reshape_position;
4088 if (conf->prev.far_offset)
4089 conf->prev.stride = 1 << conf->prev.chunk_shift;
4091 /* far_copies must be 1 */
4092 conf->prev.stride = conf->dev_sectors;
4094 conf->reshape_safe = conf->reshape_progress;
4095 spin_lock_init(&conf->device_lock);
4096 INIT_LIST_HEAD(&conf->retry_list);
4097 INIT_LIST_HEAD(&conf->bio_end_io_list);
4099 seqlock_init(&conf->resync_lock);
4100 init_waitqueue_head(&conf->wait_barrier);
4101 atomic_set(&conf->nr_pending, 0);
4104 conf->thread = md_register_thread(raid10d, mddev, "raid10");
4108 conf->mddev = mddev;
4112 raid10_free_conf(conf);
4113 return ERR_PTR(err);
4116 static void raid10_set_io_opt(struct r10conf *conf)
4118 int raid_disks = conf->geo.raid_disks;
4120 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4121 raid_disks /= conf->geo.near_copies;
4122 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4126 static int raid10_run(struct mddev *mddev)
4128 struct r10conf *conf;
4130 struct raid10_info *disk;
4131 struct md_rdev *rdev;
4133 sector_t min_offset_diff = 0;
4136 if (mddev_init_writes_pending(mddev) < 0)
4139 if (mddev->private == NULL) {
4140 conf = setup_conf(mddev);
4142 return PTR_ERR(conf);
4143 mddev->private = conf;
4145 conf = mddev->private;
4149 mddev->thread = conf->thread;
4150 conf->thread = NULL;
4152 if (mddev_is_clustered(conf->mddev)) {
4155 fc = (mddev->layout >> 8) & 255;
4156 fo = mddev->layout & (1<<16);
4157 if (fc > 1 || fo > 0) {
4158 pr_err("only near layout is supported by clustered"
4165 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
4166 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4167 raid10_set_io_opt(conf);
4170 rdev_for_each(rdev, mddev) {
4173 disk_idx = rdev->raid_disk;
4176 if (disk_idx >= conf->geo.raid_disks &&
4177 disk_idx >= conf->prev.raid_disks)
4179 disk = conf->mirrors + disk_idx;
4181 if (test_bit(Replacement, &rdev->flags)) {
4182 if (disk->replacement)
4184 disk->replacement = rdev;
4190 diff = (rdev->new_data_offset - rdev->data_offset);
4191 if (!mddev->reshape_backwards)
4195 if (first || diff < min_offset_diff)
4196 min_offset_diff = diff;
4199 disk_stack_limits(mddev->gendisk, rdev->bdev,
4200 rdev->data_offset << 9);
4202 disk->head_position = 0;
4206 /* need to check that every block has at least one working mirror */
4207 if (!enough(conf, -1)) {
4208 pr_err("md/raid10:%s: not enough operational mirrors.\n",
4213 if (conf->reshape_progress != MaxSector) {
4214 /* must ensure that shape change is supported */
4215 if (conf->geo.far_copies != 1 &&
4216 conf->geo.far_offset == 0)
4218 if (conf->prev.far_copies != 1 &&
4219 conf->prev.far_offset == 0)
4223 mddev->degraded = 0;
4225 i < conf->geo.raid_disks
4226 || i < conf->prev.raid_disks;
4229 disk = conf->mirrors + i;
4231 if (!disk->rdev && disk->replacement) {
4232 /* The replacement is all we have - use it */
4233 disk->rdev = disk->replacement;
4234 disk->replacement = NULL;
4235 clear_bit(Replacement, &disk->rdev->flags);
4239 !test_bit(In_sync, &disk->rdev->flags)) {
4240 disk->head_position = 0;
4243 disk->rdev->saved_raid_disk < 0)
4247 if (disk->replacement &&
4248 !test_bit(In_sync, &disk->replacement->flags) &&
4249 disk->replacement->saved_raid_disk < 0) {
4253 disk->recovery_disabled = mddev->recovery_disabled - 1;
4256 if (mddev->recovery_cp != MaxSector)
4257 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4259 pr_info("md/raid10:%s: active with %d out of %d devices\n",
4260 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4261 conf->geo.raid_disks);
4263 * Ok, everything is just fine now
4265 mddev->dev_sectors = conf->dev_sectors;
4266 size = raid10_size(mddev, 0, 0);
4267 md_set_array_sectors(mddev, size);
4268 mddev->resync_max_sectors = size;
4269 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
4271 if (md_integrity_register(mddev))
4274 if (conf->reshape_progress != MaxSector) {
4275 unsigned long before_length, after_length;
4277 before_length = ((1 << conf->prev.chunk_shift) *
4278 conf->prev.far_copies);
4279 after_length = ((1 << conf->geo.chunk_shift) *
4280 conf->geo.far_copies);
4282 if (max(before_length, after_length) > min_offset_diff) {
4283 /* This cannot work */
4284 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
4287 conf->offset_diff = min_offset_diff;
4289 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4290 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4291 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4292 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4293 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4295 if (!mddev->sync_thread)
4302 md_unregister_thread(&mddev->thread);
4303 raid10_free_conf(conf);
4304 mddev->private = NULL;
4309 static void raid10_free(struct mddev *mddev, void *priv)
4311 raid10_free_conf(priv);
4314 static void raid10_quiesce(struct mddev *mddev, int quiesce)
4316 struct r10conf *conf = mddev->private;
4319 raise_barrier(conf, 0);
4321 lower_barrier(conf);
4324 static int raid10_resize(struct mddev *mddev, sector_t sectors)
4326 /* Resize of 'far' arrays is not supported.
4327 * For 'near' and 'offset' arrays we can set the
4328 * number of sectors used to be an appropriate multiple
4329 * of the chunk size.
4330 * For 'offset', this is far_copies*chunksize.
4331 * For 'near' the multiplier is the LCM of
4332 * near_copies and raid_disks.
4333 * So if far_copies > 1 && !far_offset, fail.
4334 * Else find LCM(raid_disks, near_copy)*far_copies and
4335 * multiply by chunk_size. Then round to this number.
4336 * This is mostly done by raid10_size()
4338 struct r10conf *conf = mddev->private;
4339 sector_t oldsize, size;
4341 if (mddev->reshape_position != MaxSector)
4344 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
4347 oldsize = raid10_size(mddev, 0, 0);
4348 size = raid10_size(mddev, sectors, 0);
4349 if (mddev->external_size &&
4350 mddev->array_sectors > size)
4352 if (mddev->bitmap) {
4353 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
4357 md_set_array_sectors(mddev, size);
4358 if (sectors > mddev->dev_sectors &&
4359 mddev->recovery_cp > oldsize) {
4360 mddev->recovery_cp = oldsize;
4361 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4363 calc_sectors(conf, sectors);
4364 mddev->dev_sectors = conf->dev_sectors;
4365 mddev->resync_max_sectors = size;
4369 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
4371 struct md_rdev *rdev;
4372 struct r10conf *conf;
4374 if (mddev->degraded > 0) {
4375 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4377 return ERR_PTR(-EINVAL);
4379 sector_div(size, devs);
4381 /* Set new parameters */
4382 mddev->new_level = 10;
4383 /* new layout: far_copies = 1, near_copies = 2 */
4384 mddev->new_layout = (1<<8) + 2;
4385 mddev->new_chunk_sectors = mddev->chunk_sectors;
4386 mddev->delta_disks = mddev->raid_disks;
4387 mddev->raid_disks *= 2;
4388 /* make sure it will be not marked as dirty */
4389 mddev->recovery_cp = MaxSector;
4390 mddev->dev_sectors = size;
4392 conf = setup_conf(mddev);
4393 if (!IS_ERR(conf)) {
4394 rdev_for_each(rdev, mddev)
4395 if (rdev->raid_disk >= 0) {
4396 rdev->new_raid_disk = rdev->raid_disk * 2;
4397 rdev->sectors = size;
4399 WRITE_ONCE(conf->barrier, 1);
4405 static void *raid10_takeover(struct mddev *mddev)
4407 struct r0conf *raid0_conf;
4409 /* raid10 can take over:
4410 * raid0 - providing it has only two drives
4412 if (mddev->level == 0) {
4413 /* for raid0 takeover only one zone is supported */
4414 raid0_conf = mddev->private;
4415 if (raid0_conf->nr_strip_zones > 1) {
4416 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4418 return ERR_PTR(-EINVAL);
4420 return raid10_takeover_raid0(mddev,
4421 raid0_conf->strip_zone->zone_end,
4422 raid0_conf->strip_zone->nb_dev);
4424 return ERR_PTR(-EINVAL);
4427 static int raid10_check_reshape(struct mddev *mddev)
4429 /* Called when there is a request to change
4430 * - layout (to ->new_layout)
4431 * - chunk size (to ->new_chunk_sectors)
4432 * - raid_disks (by delta_disks)
4433 * or when trying to restart a reshape that was ongoing.
4435 * We need to validate the request and possibly allocate
4436 * space if that might be an issue later.
4438 * Currently we reject any reshape of a 'far' mode array,
4439 * allow chunk size to change if new is generally acceptable,
4440 * allow raid_disks to increase, and allow
4441 * a switch between 'near' mode and 'offset' mode.
4443 struct r10conf *conf = mddev->private;
4446 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4449 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4450 /* mustn't change number of copies */
4452 if (geo.far_copies > 1 && !geo.far_offset)
4453 /* Cannot switch to 'far' mode */
4456 if (mddev->array_sectors & geo.chunk_mask)
4457 /* not factor of array size */
4460 if (!enough(conf, -1))
4463 kfree(conf->mirrors_new);
4464 conf->mirrors_new = NULL;
4465 if (mddev->delta_disks > 0) {
4466 /* allocate new 'mirrors' list */
4468 kcalloc(mddev->raid_disks + mddev->delta_disks,
4469 sizeof(struct raid10_info),
4471 if (!conf->mirrors_new)
4478 * Need to check if array has failed when deciding whether to:
4480 * - remove non-faulty devices
4483 * This determination is simple when no reshape is happening.
4484 * However if there is a reshape, we need to carefully check
4485 * both the before and after sections.
4486 * This is because some failed devices may only affect one
4487 * of the two sections, and some non-in_sync devices may
4488 * be insync in the section most affected by failed devices.
4490 static int calc_degraded(struct r10conf *conf)
4492 int degraded, degraded2;
4497 /* 'prev' section first */
4498 for (i = 0; i < conf->prev.raid_disks; i++) {
4499 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4500 if (!rdev || test_bit(Faulty, &rdev->flags))
4502 else if (!test_bit(In_sync, &rdev->flags))
4503 /* When we can reduce the number of devices in
4504 * an array, this might not contribute to
4505 * 'degraded'. It does now.
4510 if (conf->geo.raid_disks == conf->prev.raid_disks)
4514 for (i = 0; i < conf->geo.raid_disks; i++) {
4515 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4516 if (!rdev || test_bit(Faulty, &rdev->flags))
4518 else if (!test_bit(In_sync, &rdev->flags)) {
4519 /* If reshape is increasing the number of devices,
4520 * this section has already been recovered, so
4521 * it doesn't contribute to degraded.
4524 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4529 if (degraded2 > degraded)
4534 static int raid10_start_reshape(struct mddev *mddev)
4536 /* A 'reshape' has been requested. This commits
4537 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4538 * This also checks if there are enough spares and adds them
4540 * We currently require enough spares to make the final
4541 * array non-degraded. We also require that the difference
4542 * between old and new data_offset - on each device - is
4543 * enough that we never risk over-writing.
4546 unsigned long before_length, after_length;
4547 sector_t min_offset_diff = 0;
4550 struct r10conf *conf = mddev->private;
4551 struct md_rdev *rdev;
4555 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4558 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4561 before_length = ((1 << conf->prev.chunk_shift) *
4562 conf->prev.far_copies);
4563 after_length = ((1 << conf->geo.chunk_shift) *
4564 conf->geo.far_copies);
4566 rdev_for_each(rdev, mddev) {
4567 if (!test_bit(In_sync, &rdev->flags)
4568 && !test_bit(Faulty, &rdev->flags))
4570 if (rdev->raid_disk >= 0) {
4571 long long diff = (rdev->new_data_offset
4572 - rdev->data_offset);
4573 if (!mddev->reshape_backwards)
4577 if (first || diff < min_offset_diff)
4578 min_offset_diff = diff;
4583 if (max(before_length, after_length) > min_offset_diff)
4586 if (spares < mddev->delta_disks)
4589 conf->offset_diff = min_offset_diff;
4590 spin_lock_irq(&conf->device_lock);
4591 if (conf->mirrors_new) {
4592 memcpy(conf->mirrors_new, conf->mirrors,
4593 sizeof(struct raid10_info)*conf->prev.raid_disks);
4595 kfree(conf->mirrors_old);
4596 conf->mirrors_old = conf->mirrors;
4597 conf->mirrors = conf->mirrors_new;
4598 conf->mirrors_new = NULL;
4600 setup_geo(&conf->geo, mddev, geo_start);
4602 if (mddev->reshape_backwards) {
4603 sector_t size = raid10_size(mddev, 0, 0);
4604 if (size < mddev->array_sectors) {
4605 spin_unlock_irq(&conf->device_lock);
4606 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4610 mddev->resync_max_sectors = size;
4611 conf->reshape_progress = size;
4613 conf->reshape_progress = 0;
4614 conf->reshape_safe = conf->reshape_progress;
4615 spin_unlock_irq(&conf->device_lock);
4617 if (mddev->delta_disks && mddev->bitmap) {
4618 struct mdp_superblock_1 *sb = NULL;
4619 sector_t oldsize, newsize;
4621 oldsize = raid10_size(mddev, 0, 0);
4622 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4624 if (!mddev_is_clustered(mddev)) {
4625 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4632 rdev_for_each(rdev, mddev) {
4633 if (rdev->raid_disk > -1 &&
4634 !test_bit(Faulty, &rdev->flags))
4635 sb = page_address(rdev->sb_page);
4639 * some node is already performing reshape, and no need to
4640 * call md_bitmap_resize again since it should be called when
4641 * receiving BITMAP_RESIZE msg
4643 if ((sb && (le32_to_cpu(sb->feature_map) &
4644 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4647 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4651 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4653 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4658 if (mddev->delta_disks > 0) {
4659 rdev_for_each(rdev, mddev)
4660 if (rdev->raid_disk < 0 &&
4661 !test_bit(Faulty, &rdev->flags)) {
4662 if (raid10_add_disk(mddev, rdev) == 0) {
4663 if (rdev->raid_disk >=
4664 conf->prev.raid_disks)
4665 set_bit(In_sync, &rdev->flags);
4667 rdev->recovery_offset = 0;
4669 /* Failure here is OK */
4670 sysfs_link_rdev(mddev, rdev);
4672 } else if (rdev->raid_disk >= conf->prev.raid_disks
4673 && !test_bit(Faulty, &rdev->flags)) {
4674 /* This is a spare that was manually added */
4675 set_bit(In_sync, &rdev->flags);
4678 /* When a reshape changes the number of devices,
4679 * ->degraded is measured against the larger of the
4680 * pre and post numbers.
4682 spin_lock_irq(&conf->device_lock);
4683 mddev->degraded = calc_degraded(conf);
4684 spin_unlock_irq(&conf->device_lock);
4685 mddev->raid_disks = conf->geo.raid_disks;
4686 mddev->reshape_position = conf->reshape_progress;
4687 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4689 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4690 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4691 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4692 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4693 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4695 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4697 if (!mddev->sync_thread) {
4701 conf->reshape_checkpoint = jiffies;
4702 md_wakeup_thread(mddev->sync_thread);
4707 mddev->recovery = 0;
4708 spin_lock_irq(&conf->device_lock);
4709 conf->geo = conf->prev;
4710 mddev->raid_disks = conf->geo.raid_disks;
4711 rdev_for_each(rdev, mddev)
4712 rdev->new_data_offset = rdev->data_offset;
4714 conf->reshape_progress = MaxSector;
4715 conf->reshape_safe = MaxSector;
4716 mddev->reshape_position = MaxSector;
4717 spin_unlock_irq(&conf->device_lock);
4721 /* Calculate the last device-address that could contain
4722 * any block from the chunk that includes the array-address 's'
4723 * and report the next address.
4724 * i.e. the address returned will be chunk-aligned and after
4725 * any data that is in the chunk containing 's'.
4727 static sector_t last_dev_address(sector_t s, struct geom *geo)
4729 s = (s | geo->chunk_mask) + 1;
4730 s >>= geo->chunk_shift;
4731 s *= geo->near_copies;
4732 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4733 s *= geo->far_copies;
4734 s <<= geo->chunk_shift;
4738 /* Calculate the first device-address that could contain
4739 * any block from the chunk that includes the array-address 's'.
4740 * This too will be the start of a chunk
4742 static sector_t first_dev_address(sector_t s, struct geom *geo)
4744 s >>= geo->chunk_shift;
4745 s *= geo->near_copies;
4746 sector_div(s, geo->raid_disks);
4747 s *= geo->far_copies;
4748 s <<= geo->chunk_shift;
4752 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4755 /* We simply copy at most one chunk (smallest of old and new)
4756 * at a time, possibly less if that exceeds RESYNC_PAGES,
4757 * or we hit a bad block or something.
4758 * This might mean we pause for normal IO in the middle of
4759 * a chunk, but that is not a problem as mddev->reshape_position
4760 * can record any location.
4762 * If we will want to write to a location that isn't
4763 * yet recorded as 'safe' (i.e. in metadata on disk) then
4764 * we need to flush all reshape requests and update the metadata.
4766 * When reshaping forwards (e.g. to more devices), we interpret
4767 * 'safe' as the earliest block which might not have been copied
4768 * down yet. We divide this by previous stripe size and multiply
4769 * by previous stripe length to get lowest device offset that we
4770 * cannot write to yet.
4771 * We interpret 'sector_nr' as an address that we want to write to.
4772 * From this we use last_device_address() to find where we might
4773 * write to, and first_device_address on the 'safe' position.
4774 * If this 'next' write position is after the 'safe' position,
4775 * we must update the metadata to increase the 'safe' position.
4777 * When reshaping backwards, we round in the opposite direction
4778 * and perform the reverse test: next write position must not be
4779 * less than current safe position.
4781 * In all this the minimum difference in data offsets
4782 * (conf->offset_diff - always positive) allows a bit of slack,
4783 * so next can be after 'safe', but not by more than offset_diff
4785 * We need to prepare all the bios here before we start any IO
4786 * to ensure the size we choose is acceptable to all devices.
4787 * The means one for each copy for write-out and an extra one for
4789 * We store the read-in bio in ->master_bio and the others in
4790 * ->devs[x].bio and ->devs[x].repl_bio.
4792 struct r10conf *conf = mddev->private;
4793 struct r10bio *r10_bio;
4794 sector_t next, safe, last;
4798 struct md_rdev *rdev;
4801 struct bio *bio, *read_bio;
4802 int sectors_done = 0;
4803 struct page **pages;
4805 if (sector_nr == 0) {
4806 /* If restarting in the middle, skip the initial sectors */
4807 if (mddev->reshape_backwards &&
4808 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4809 sector_nr = (raid10_size(mddev, 0, 0)
4810 - conf->reshape_progress);
4811 } else if (!mddev->reshape_backwards &&
4812 conf->reshape_progress > 0)
4813 sector_nr = conf->reshape_progress;
4815 mddev->curr_resync_completed = sector_nr;
4816 sysfs_notify_dirent_safe(mddev->sysfs_completed);
4822 /* We don't use sector_nr to track where we are up to
4823 * as that doesn't work well for ->reshape_backwards.
4824 * So just use ->reshape_progress.
4826 if (mddev->reshape_backwards) {
4827 /* 'next' is the earliest device address that we might
4828 * write to for this chunk in the new layout
4830 next = first_dev_address(conf->reshape_progress - 1,
4833 /* 'safe' is the last device address that we might read from
4834 * in the old layout after a restart
4836 safe = last_dev_address(conf->reshape_safe - 1,
4839 if (next + conf->offset_diff < safe)
4842 last = conf->reshape_progress - 1;
4843 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4844 & conf->prev.chunk_mask);
4845 if (sector_nr + RESYNC_SECTORS < last)
4846 sector_nr = last + 1 - RESYNC_SECTORS;
4848 /* 'next' is after the last device address that we
4849 * might write to for this chunk in the new layout
4851 next = last_dev_address(conf->reshape_progress, &conf->geo);
4853 /* 'safe' is the earliest device address that we might
4854 * read from in the old layout after a restart
4856 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4858 /* Need to update metadata if 'next' might be beyond 'safe'
4859 * as that would possibly corrupt data
4861 if (next > safe + conf->offset_diff)
4864 sector_nr = conf->reshape_progress;
4865 last = sector_nr | (conf->geo.chunk_mask
4866 & conf->prev.chunk_mask);
4868 if (sector_nr + RESYNC_SECTORS <= last)
4869 last = sector_nr + RESYNC_SECTORS - 1;
4873 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4874 /* Need to update reshape_position in metadata */
4875 wait_barrier(conf, false);
4876 mddev->reshape_position = conf->reshape_progress;
4877 if (mddev->reshape_backwards)
4878 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4879 - conf->reshape_progress;
4881 mddev->curr_resync_completed = conf->reshape_progress;
4882 conf->reshape_checkpoint = jiffies;
4883 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4884 md_wakeup_thread(mddev->thread);
4885 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4886 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4887 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4888 allow_barrier(conf);
4889 return sectors_done;
4891 conf->reshape_safe = mddev->reshape_position;
4892 allow_barrier(conf);
4895 raise_barrier(conf, 0);
4897 /* Now schedule reads for blocks from sector_nr to last */
4898 r10_bio = raid10_alloc_init_r10buf(conf);
4900 raise_barrier(conf, 1);
4901 atomic_set(&r10_bio->remaining, 0);
4902 r10_bio->mddev = mddev;
4903 r10_bio->sector = sector_nr;
4904 set_bit(R10BIO_IsReshape, &r10_bio->state);
4905 r10_bio->sectors = last - sector_nr + 1;
4906 rdev = read_balance(conf, r10_bio, &max_sectors);
4907 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4910 /* Cannot read from here, so need to record bad blocks
4911 * on all the target devices.
4914 mempool_free(r10_bio, &conf->r10buf_pool);
4915 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4916 return sectors_done;
4919 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ,
4920 GFP_KERNEL, &mddev->bio_set);
4921 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4922 + rdev->data_offset);
4923 read_bio->bi_private = r10_bio;
4924 read_bio->bi_end_io = end_reshape_read;
4925 r10_bio->master_bio = read_bio;
4926 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4929 * Broadcast RESYNC message to other nodes, so all nodes would not
4930 * write to the region to avoid conflict.
4932 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4933 struct mdp_superblock_1 *sb = NULL;
4934 int sb_reshape_pos = 0;
4936 conf->cluster_sync_low = sector_nr;
4937 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4938 sb = page_address(rdev->sb_page);
4940 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4942 * Set cluster_sync_low again if next address for array
4943 * reshape is less than cluster_sync_low. Since we can't
4944 * update cluster_sync_low until it has finished reshape.
4946 if (sb_reshape_pos < conf->cluster_sync_low)
4947 conf->cluster_sync_low = sb_reshape_pos;
4950 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4951 conf->cluster_sync_high);
4954 /* Now find the locations in the new layout */
4955 __raid10_find_phys(&conf->geo, r10_bio);
4958 read_bio->bi_next = NULL;
4961 for (s = 0; s < conf->copies*2; s++) {
4963 int d = r10_bio->devs[s/2].devnum;
4964 struct md_rdev *rdev2;
4966 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4967 b = r10_bio->devs[s/2].repl_bio;
4969 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4970 b = r10_bio->devs[s/2].bio;
4972 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4975 bio_set_dev(b, rdev2->bdev);
4976 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4977 rdev2->new_data_offset;
4978 b->bi_end_io = end_reshape_write;
4979 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4984 /* Now add as many pages as possible to all of these bios. */
4987 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4988 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4989 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4990 int len = (max_sectors - s) << 9;
4991 if (len > PAGE_SIZE)
4993 for (bio = blist; bio ; bio = bio->bi_next) {
4995 * won't fail because the vec table is big enough
4996 * to hold all these pages
4998 bio_add_page(bio, page, len, 0);
5000 sector_nr += len >> 9;
5001 nr_sectors += len >> 9;
5004 r10_bio->sectors = nr_sectors;
5006 /* Now submit the read */
5007 md_sync_acct_bio(read_bio, r10_bio->sectors);
5008 atomic_inc(&r10_bio->remaining);
5009 read_bio->bi_next = NULL;
5010 submit_bio_noacct(read_bio);
5011 sectors_done += nr_sectors;
5012 if (sector_nr <= last)
5015 lower_barrier(conf);
5017 /* Now that we have done the whole section we can
5018 * update reshape_progress
5020 if (mddev->reshape_backwards)
5021 conf->reshape_progress -= sectors_done;
5023 conf->reshape_progress += sectors_done;
5025 return sectors_done;
5028 static void end_reshape_request(struct r10bio *r10_bio);
5029 static int handle_reshape_read_error(struct mddev *mddev,
5030 struct r10bio *r10_bio);
5031 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
5033 /* Reshape read completed. Hopefully we have a block
5035 * If we got a read error then we do sync 1-page reads from
5036 * elsewhere until we find the data - or give up.
5038 struct r10conf *conf = mddev->private;
5041 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
5042 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
5043 /* Reshape has been aborted */
5044 md_done_sync(mddev, r10_bio->sectors, 0);
5048 /* We definitely have the data in the pages, schedule the
5051 atomic_set(&r10_bio->remaining, 1);
5052 for (s = 0; s < conf->copies*2; s++) {
5054 int d = r10_bio->devs[s/2].devnum;
5055 struct md_rdev *rdev;
5058 rdev = rcu_dereference(conf->mirrors[d].replacement);
5059 b = r10_bio->devs[s/2].repl_bio;
5061 rdev = rcu_dereference(conf->mirrors[d].rdev);
5062 b = r10_bio->devs[s/2].bio;
5064 if (!rdev || test_bit(Faulty, &rdev->flags)) {
5068 atomic_inc(&rdev->nr_pending);
5070 md_sync_acct_bio(b, r10_bio->sectors);
5071 atomic_inc(&r10_bio->remaining);
5073 submit_bio_noacct(b);
5075 end_reshape_request(r10_bio);
5078 static void end_reshape(struct r10conf *conf)
5080 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5083 spin_lock_irq(&conf->device_lock);
5084 conf->prev = conf->geo;
5085 md_finish_reshape(conf->mddev);
5087 conf->reshape_progress = MaxSector;
5088 conf->reshape_safe = MaxSector;
5089 spin_unlock_irq(&conf->device_lock);
5091 if (conf->mddev->queue)
5092 raid10_set_io_opt(conf);
5096 static void raid10_update_reshape_pos(struct mddev *mddev)
5098 struct r10conf *conf = mddev->private;
5101 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5102 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5103 || mddev->reshape_position == MaxSector)
5104 conf->reshape_progress = mddev->reshape_position;
5109 static int handle_reshape_read_error(struct mddev *mddev,
5110 struct r10bio *r10_bio)
5112 /* Use sync reads to get the blocks from somewhere else */
5113 int sectors = r10_bio->sectors;
5114 struct r10conf *conf = mddev->private;
5115 struct r10bio *r10b;
5118 struct page **pages;
5120 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
5122 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5126 /* reshape IOs share pages from .devs[0].bio */
5127 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
5129 r10b->sector = r10_bio->sector;
5130 __raid10_find_phys(&conf->prev, r10b);
5135 int first_slot = slot;
5137 if (s > (PAGE_SIZE >> 9))
5142 int d = r10b->devs[slot].devnum;
5143 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5146 test_bit(Faulty, &rdev->flags) ||
5147 !test_bit(In_sync, &rdev->flags))
5150 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
5151 atomic_inc(&rdev->nr_pending);
5153 success = sync_page_io(rdev,
5157 REQ_OP_READ, false);
5158 rdev_dec_pending(rdev, mddev);
5164 if (slot >= conf->copies)
5166 if (slot == first_slot)
5171 /* couldn't read this block, must give up */
5172 set_bit(MD_RECOVERY_INTR,
5184 static void end_reshape_write(struct bio *bio)
5186 struct r10bio *r10_bio = get_resync_r10bio(bio);
5187 struct mddev *mddev = r10_bio->mddev;
5188 struct r10conf *conf = mddev->private;
5192 struct md_rdev *rdev = NULL;
5194 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5196 rdev = conf->mirrors[d].replacement;
5199 rdev = conf->mirrors[d].rdev;
5202 if (bio->bi_status) {
5203 /* FIXME should record badblock */
5204 md_error(mddev, rdev);
5207 rdev_dec_pending(rdev, mddev);
5208 end_reshape_request(r10_bio);
5211 static void end_reshape_request(struct r10bio *r10_bio)
5213 if (!atomic_dec_and_test(&r10_bio->remaining))
5215 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5216 bio_put(r10_bio->master_bio);
5220 static void raid10_finish_reshape(struct mddev *mddev)
5222 struct r10conf *conf = mddev->private;
5224 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5227 if (mddev->delta_disks > 0) {
5228 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5229 mddev->recovery_cp = mddev->resync_max_sectors;
5230 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5232 mddev->resync_max_sectors = mddev->array_sectors;
5236 for (d = conf->geo.raid_disks ;
5237 d < conf->geo.raid_disks - mddev->delta_disks;
5239 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5241 clear_bit(In_sync, &rdev->flags);
5242 rdev = rcu_dereference(conf->mirrors[d].replacement);
5244 clear_bit(In_sync, &rdev->flags);
5248 mddev->layout = mddev->new_layout;
5249 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5250 mddev->reshape_position = MaxSector;
5251 mddev->delta_disks = 0;
5252 mddev->reshape_backwards = 0;
5255 static struct md_personality raid10_personality =
5259 .owner = THIS_MODULE,
5260 .make_request = raid10_make_request,
5262 .free = raid10_free,
5263 .status = raid10_status,
5264 .error_handler = raid10_error,
5265 .hot_add_disk = raid10_add_disk,
5266 .hot_remove_disk= raid10_remove_disk,
5267 .spare_active = raid10_spare_active,
5268 .sync_request = raid10_sync_request,
5269 .quiesce = raid10_quiesce,
5270 .size = raid10_size,
5271 .resize = raid10_resize,
5272 .takeover = raid10_takeover,
5273 .check_reshape = raid10_check_reshape,
5274 .start_reshape = raid10_start_reshape,
5275 .finish_reshape = raid10_finish_reshape,
5276 .update_reshape_pos = raid10_update_reshape_pos,
5279 static int __init raid_init(void)
5281 return register_md_personality(&raid10_personality);
5284 static void raid_exit(void)
5286 unregister_md_personality(&raid10_personality);
5289 module_init(raid_init);
5290 module_exit(raid_exit);
5291 MODULE_LICENSE("GPL");
5292 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
5293 MODULE_ALIAS("md-personality-9"); /* RAID10 */
5294 MODULE_ALIAS("md-raid10");
5295 MODULE_ALIAS("md-level-10");