blob: 0ae7e52983fa0b256d5bcf0522dcb123bc19a8cc [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * raid10.c : Multiple Devices driver for Linux
4 *
5 * Copyright (C) 2000-2004 Neil Brown
6 *
7 * RAID-10 support for md.
8 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110013#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110014#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040015#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110016#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100017#include <linux/ratelimit.h>
NeilBrown3ea7daa2012-05-22 13:53:47 +100018#include <linux/kthread.h>
Guoqing Jiangafd75622018-10-18 16:37:41 +080019#include <linux/raid/md_p.h>
NeilBrown109e3762016-11-18 13:22:04 +110020#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110021#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110022#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110023#include "raid0.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040024#include "md-bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
29 * chunk_size
30 * raid_disks
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070033 * far_offset (stored in bit 16 of layout )
Jonathan Brassow475901a2013-02-21 13:28:10 +110034 * use_far_sets (stored in bit 17 of layout )
NeilBrown8bce6d32015-10-22 13:20:15 +110035 * use_far_sets_bugfixed (stored in bit 18 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
Jonathan Brassow475901a2013-02-21 13:28:10 +110037 * 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.
NeilBrownc93983b2006-06-26 00:27:41 -070045 *
46 * If far_offset is true, then the far_copies are handled a bit differently.
Jonathan Brassow475901a2013-02-21 13:28:10 +110047 * The copies are still in different stripes, but instead of being very far
48 * apart on disk, there are adjacent stripes.
49 *
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
57 * on a device):
58 * A B C D A B C D E
59 * ... ...
60 * D A B C E A B C D
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]
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
NeilBrowne879a872011-10-11 16:49:02 +110067static void allow_barrier(struct r10conf *conf);
68static void lower_barrier(struct r10conf *conf);
NeilBrown635f6412013-06-11 14:57:09 +100069static int _enough(struct r10conf *conf, int previous, int ignore);
NeilBrown1919cbb2016-11-18 16:16:12 +110070static int enough(struct r10conf *conf, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +100071static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
72 int *skipped);
73static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020074static void end_reshape_write(struct bio *bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +100075static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080076
NeilBrown578b54a2016-11-14 16:30:21 +110077#define raid10_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
79
Ming Leifb0eb5d2017-07-14 16:14:43 +080080#include "raid1-10.c"
81
Yu Kuaib9b083f2022-09-16 19:34:28 +080082#define NULL_CMD
83#define cmd_before(conf, cmd) \
84 do { \
85 write_sequnlock_irq(&(conf)->resync_lock); \
86 cmd; \
87 } while (0)
88#define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock)
89
90#define wait_event_barrier_cmd(conf, cond, cmd) \
91 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \
92 cmd_after(conf))
93
94#define wait_event_barrier(conf, cond) \
95 wait_event_barrier_cmd(conf, cond, NULL_CMD)
96
Ming Leif0250612017-03-17 00:12:33 +080097/*
Ming Leif0250612017-03-17 00:12:33 +080098 * for resync bio, r10bio pointer can be retrieved from the per-bio
99 * 'struct resync_pages'.
100 */
101static inline struct r10bio *get_resync_r10bio(struct bio *bio)
102{
103 return get_resync_pages(bio)->raid_bio;
104}
105
Al Virodd0fc662005-10-07 07:46:04 +0100106static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
NeilBrowne879a872011-10-11 16:49:02 +1100108 struct r10conf *conf = data;
Xiao Nic2968282021-02-04 15:50:44 +0800109 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
NeilBrown69335ef2011-12-23 10:17:54 +1100111 /* allocate a r10bio with room for raid_disks entries in the
112 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100113 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Guoqing Jiang8db87912017-10-24 15:11:52 +0800116#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
NeilBrown0310fa22008-08-05 15:54:14 +1000117/* 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)
Guoqing Jiang4b242e92018-01-19 11:37:56 +0800121#define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
Guoqing Jiang8db87912017-10-24 15:11:52 +0800122#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*
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)
129 *
130 */
Al Virodd0fc662005-10-07 07:46:04 +0100131static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
NeilBrowne879a872011-10-11 16:49:02 +1100133 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100134 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct bio *bio;
Ming Leif0250612017-03-17 00:12:33 +0800136 int j;
137 int nalloc, nalloc_rp;
138 struct resync_pages *rps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100141 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
NeilBrown3ea7daa2012-05-22 13:53:47 +1000144 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
145 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 nalloc = conf->copies; /* resync */
147 else
148 nalloc = 2; /* recovery */
149
Ming Leif0250612017-03-17 00:12:33 +0800150 /* allocate once for all bios */
151 if (!conf->have_replacement)
152 nalloc_rp = nalloc;
153 else
154 nalloc_rp = nalloc * 2;
Kees Cook6da2ec52018-06-12 13:55:00 -0700155 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
Ming Leif0250612017-03-17 00:12:33 +0800156 if (!rps)
157 goto out_free_r10bio;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * Allocate bios.
161 */
162 for (j = nalloc ; j-- ; ) {
Christoph Hellwig066ff572022-04-06 08:12:27 +0200163 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!bio)
165 goto out_free_bio;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200166 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100168 if (!conf->have_replacement)
169 continue;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200170 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
NeilBrown69335ef2011-12-23 10:17:54 +1100171 if (!bio)
172 goto out_free_bio;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200173 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
NeilBrown69335ef2011-12-23 10:17:54 +1100174 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 /*
177 * Allocate RESYNC_PAGES data pages and attach them
178 * where needed.
179 */
Ming Leif0250612017-03-17 00:12:33 +0800180 for (j = 0; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100181 struct bio *rbio = r10_bio->devs[j].repl_bio;
Ming Leif0250612017-03-17 00:12:33 +0800182 struct resync_pages *rp, *rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ming Leif0250612017-03-17 00:12:33 +0800184 rp = &rps[j];
185 if (rbio)
186 rp_repl = &rps[nalloc + j];
187
188 bio = r10_bio->devs[j].bio;
189
190 if (!j || test_bit(MD_RECOVERY_SYNC,
191 &conf->mddev->recovery)) {
192 if (resync_alloc_pages(rp, gfp_flags))
193 goto out_free_pages;
194 } else {
195 memcpy(rp, &rps[0], sizeof(*rp));
196 resync_get_all_pages(rp);
197 }
198
Ming Leif0250612017-03-17 00:12:33 +0800199 rp->raid_bio = r10_bio;
200 bio->bi_private = rp;
201 if (rbio) {
202 memcpy(rp_repl, rp, sizeof(*rp));
203 rbio->bi_private = rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 }
206
207 return r10_bio;
208
209out_free_pages:
Ming Leif0250612017-03-17 00:12:33 +0800210 while (--j >= 0)
John Pittman45422b72019-11-11 16:43:20 -0800211 resync_free_pages(&rps[j]);
Ming Leif0250612017-03-17 00:12:33 +0800212
majianpeng5fdd2cf2012-05-22 13:55:03 +1000213 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000215 for ( ; j < nalloc; j++) {
216 if (r10_bio->devs[j].bio)
Christoph Hellwig066ff572022-04-06 08:12:27 +0200217 bio_uninit(r10_bio->devs[j].bio);
218 kfree(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100219 if (r10_bio->devs[j].repl_bio)
Christoph Hellwig066ff572022-04-06 08:12:27 +0200220 bio_uninit(r10_bio->devs[j].repl_bio);
221 kfree(r10_bio->devs[j].repl_bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100222 }
Ming Leif0250612017-03-17 00:12:33 +0800223 kfree(rps);
224out_free_r10bio:
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700225 rbio_pool_free(r10_bio, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return NULL;
227}
228
229static void r10buf_pool_free(void *__r10_bio, void *data)
230{
NeilBrowne879a872011-10-11 16:49:02 +1100231 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100232 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int j;
Ming Leif0250612017-03-17 00:12:33 +0800234 struct resync_pages *rp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Ming Leif0250612017-03-17 00:12:33 +0800236 for (j = conf->copies; j--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct bio *bio = r10bio->devs[j].bio;
Ming Leif0250612017-03-17 00:12:33 +0800238
Guoqing Jiangeb81b322018-04-26 10:56:37 +0800239 if (bio) {
240 rp = get_resync_pages(bio);
241 resync_free_pages(rp);
Christoph Hellwig066ff572022-04-06 08:12:27 +0200242 bio_uninit(bio);
243 kfree(bio);
Guoqing Jiangeb81b322018-04-26 10:56:37 +0800244 }
Ming Leif0250612017-03-17 00:12:33 +0800245
NeilBrown69335ef2011-12-23 10:17:54 +1100246 bio = r10bio->devs[j].repl_bio;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200247 if (bio) {
248 bio_uninit(bio);
249 kfree(bio);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Ming Leif0250612017-03-17 00:12:33 +0800252
253 /* resync pages array stored in the 1st bio's .bi_private */
254 kfree(rp);
255
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700256 rbio_pool_free(r10bio, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrowne879a872011-10-11 16:49:02 +1100259static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int i;
262
Xiao Nic2968282021-02-04 15:50:44 +0800263 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000265 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 bio_put(*bio);
267 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100268 bio = &r10_bio->devs[i].repl_bio;
269 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
270 bio_put(*bio);
271 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273}
274
NeilBrown9f2c9d12011-10-11 16:48:43 +1100275static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
NeilBrowne879a872011-10-11 16:49:02 +1100277 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 put_all_bios(conf, r10_bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400280 mempool_free(r10_bio, &conf->r10bio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
NeilBrown9f2c9d12011-10-11 16:48:43 +1100283static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
NeilBrowne879a872011-10-11 16:49:02 +1100285 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Kent Overstreetafeee512018-05-20 18:25:52 -0400287 mempool_free(r10_bio, &conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
NeilBrown0a27ec92006-01-06 00:20:13 -0800289 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Yu Kuai0c0be98b2022-09-16 19:34:26 +0800292static void wake_up_barrier(struct r10conf *conf)
293{
294 if (wq_has_sleeper(&conf->wait_barrier))
295 wake_up(&conf->wait_barrier);
296}
297
NeilBrown9f2c9d12011-10-11 16:48:43 +1100298static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100301 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100302 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 spin_lock_irqsave(&conf->device_lock, flags);
305 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800306 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 spin_unlock_irqrestore(&conf->device_lock, flags);
308
Arthur Jones388667b2008-07-25 12:03:38 -0700309 /* wake up frozen array... */
310 wake_up(&conf->wait_barrier);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 md_wakeup_thread(mddev->thread);
313}
314
315/*
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
318 * cache layer.
319 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100320static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100323 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
NeilBrown856e08e2011-07-28 11:39:23 +1000325 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200326 bio->bi_status = BLK_STS_IOERR;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100327
Guoqing Jiang528bc2c2021-05-25 17:46:22 +0800328 if (blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
329 bio_end_io_acct(bio, r10_bio->start_time);
NeilBrownfd16f2e2017-03-15 14:05:13 +1100330 bio_endio(bio);
331 /*
332 * Wake up any possible resync thread that waits for the device
333 * to go idle.
334 */
335 allow_barrier(conf);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 free_r10bio(r10_bio);
338}
339
340/*
341 * Update disk head position estimator based on IRQ completion info.
342 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100343static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
NeilBrowne879a872011-10-11 16:49:02 +1100345 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
348 r10_bio->devs[slot].addr + (r10_bio->sectors);
349}
350
Namhyung Kim778ca012011-07-18 17:38:47 +1000351/*
352 * Find the disk number which triggered given bio
353 */
NeilBrowne879a872011-10-11 16:49:02 +1100354static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100355 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000356{
357 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100358 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000359
Xiao Nic2968282021-02-04 15:50:44 +0800360 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000361 if (r10_bio->devs[slot].bio == bio)
362 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100363 if (r10_bio->devs[slot].repl_bio == bio) {
364 repl = 1;
365 break;
366 }
367 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000368
Namhyung Kim778ca012011-07-18 17:38:47 +1000369 update_head_pos(slot, r10_bio);
370
NeilBrown749c55e2011-07-28 11:39:24 +1000371 if (slotp)
372 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100373 if (replp)
374 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000375 return r10_bio->devs[slot].devnum;
376}
377
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200378static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200380 int uptodate = !bio->bi_status;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100381 struct r10bio *r10_bio = bio->bi_private;
Colin Ian Kinga0e764c2017-10-11 11:46:54 +0100382 int slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100383 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100384 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 slot = r10_bio->read_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100387 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
389 * this branch is our 'one mirror IO has finished' event handler:
390 */
NeilBrown4443ae12006-01-06 00:20:28 -0800391 update_head_pos(slot, r10_bio);
392
393 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
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.
398 *
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.
402 */
403 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100404 } else {
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"
409 */
NeilBrown635f6412013-06-11 14:57:09 +1000410 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
411 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100412 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100413 }
414 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100416 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800417 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000419 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Christoph Hellwig913cce52022-05-12 08:19:13 +0200421 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000422 mdname(conf->mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +0200423 rdev->bdev,
Christian Dietrich8bda4702011-07-27 11:00:36 +1000424 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000425 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 reschedule_retry(r10_bio);
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
NeilBrown9f2c9d12011-10-11 16:48:43 +1100430static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000431{
432 /* clear the bitmap if all writes complete successfully */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700433 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
434 r10_bio->sectors,
435 !test_bit(R10BIO_Degraded, &r10_bio->state),
436 0);
NeilBrownbd870a12011-07-28 11:39:24 +1000437 md_write_end(r10_bio->mddev);
438}
439
NeilBrown9f2c9d12011-10-11 16:48:43 +1100440static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000441{
442 if (atomic_dec_and_test(&r10_bio->remaining)) {
443 if (test_bit(R10BIO_WriteError, &r10_bio->state))
444 reschedule_retry(r10_bio);
445 else {
446 close_write(r10_bio);
447 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
448 reschedule_retry(r10_bio);
449 else
450 raid_end_bio_io(r10_bio);
451 }
452 }
453}
454
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200455static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100457 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000458 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000459 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100460 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100461 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100462 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100463 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700464 bool discard_error;
465
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200466 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
NeilBrown475b0322011-12-23 10:17:55 +1100468 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
NeilBrown475b0322011-12-23 10:17:55 +1100470 if (repl)
471 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100472 if (!rdev) {
473 smp_rmb();
474 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100475 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
478 * this branch is our 'one mirror IO has finished' event handler:
479 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200480 if (bio->bi_status && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100481 if (repl)
482 /* Never record new bad blocks to replacement,
483 * just fail it.
484 */
485 md_error(rdev->mddev, rdev);
486 else {
487 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100488 if (!test_and_set_bit(WantReplacement, &rdev->flags))
489 set_bit(MD_RECOVERY_NEEDED,
490 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100491
NeilBrown475b0322011-12-23 10:17:55 +1100492 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100493 if (test_bit(FailFast, &rdev->flags) &&
494 (bio->bi_opf & MD_FAILFAST)) {
495 md_error(rdev->mddev, rdev);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800496 }
497
498 /*
499 * When the device is faulty, it is not necessary to
500 * handle write error.
Yufen Yu7cee6d42019-07-19 13:48:47 +0800501 */
502 if (!test_bit(Faulty, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +1100503 set_bit(R10BIO_WriteError, &r10_bio->state);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800504 else {
Wei Shuyu5ba03932021-06-28 15:15:08 +0800505 /* Fail the request */
506 set_bit(R10BIO_Degraded, &r10_bio->state);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800507 r10_bio->devs[slot].bio = NULL;
508 to_put = bio;
509 dec_rdev = 1;
510 }
NeilBrown475b0322011-12-23 10:17:55 +1100511 }
NeilBrown749c55e2011-07-28 11:39:24 +1000512 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
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.
517 *
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.
521 */
NeilBrown749c55e2011-07-28 11:39:24 +1000522 sector_t first_bad;
523 int bad_sectors;
524
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300525 /*
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
531 * check this here.
532 */
533 if (test_bit(In_sync, &rdev->flags) &&
534 !test_bit(Faulty, &rdev->flags))
535 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
NeilBrown749c55e2011-07-28 11:39:24 +1000537 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100538 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000539 r10_bio->devs[slot].addr,
540 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700541 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000542 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100543 if (repl)
544 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
545 else
546 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000547 dec_rdev = 0;
548 set_bit(R10BIO_MadeGood, &r10_bio->state);
549 }
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
553 *
554 * Let's see if all mirrored write operations have finished
555 * already.
556 */
NeilBrown19d5f832011-09-10 17:21:17 +1000557 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000558 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100559 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100560 if (to_put)
561 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300566 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * 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.
572 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300573 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * first chunk, followed by near_copies copies of the next chunk and
575 * so on.
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.
581 *
582 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700583 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 *
585 * raid10_find_virt does the reverse mapping, from a device and a
586 * sector offset to a virtual address
587 */
588
NeilBrownf8c9e742012-05-21 09:28:33 +1000589static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int n,f;
592 sector_t sector;
593 sector_t chunk;
594 sector_t stripe;
595 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100597 int last_far_set_start, last_far_set_size;
598
599 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
600 last_far_set_start *= geo->far_set_size;
601
602 last_far_set_size = geo->far_set_size;
603 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000606 chunk = r10bio->sector >> geo->chunk_shift;
607 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
NeilBrown5cf00fc2012-05-21 09:28:20 +1000609 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000611 dev = sector_div(stripe, geo->raid_disks);
612 if (geo->far_offset)
613 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
NeilBrown5cf00fc2012-05-21 09:28:20 +1000615 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000618 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100620 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100623 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 slot++;
625
NeilBrown5cf00fc2012-05-21 09:28:20 +1000626 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100627 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000628 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100629
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100630 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;
635 } else {
636 d %= geo->far_set_size;
637 d += geo->far_set_size * set;
638 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000639 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 r10bio->devs[slot].devnum = d;
641 r10bio->devs[slot].addr = s;
642 slot++;
643 }
644 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000645 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000647 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000650}
651
652static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
653{
654 struct geom *geo = &conf->geo;
655
656 if (conf->reshape_progress != MaxSector &&
657 ((r10bio->sector >= conf->reshape_progress) !=
658 conf->mddev->reshape_backwards)) {
659 set_bit(R10BIO_Previous, &r10bio->state);
660 geo = &conf->prev;
661 } else
662 clear_bit(R10BIO_Previous, &r10bio->state);
663
664 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
NeilBrowne879a872011-10-11 16:49:02 +1100667static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000670 /* Never use conf->prev as this is only called during resync
671 * or recovery, so reshape isn't happening
672 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000673 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100674 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
675 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100676 int last_far_set_start;
677
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;
681
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;
686 }
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
NeilBrown5cf00fc2012-05-21 09:28:20 +1000689 offset = sector & geo->chunk_mask;
690 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700691 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000692 chunk = sector >> geo->chunk_shift;
693 fc = sector_div(chunk, geo->far_copies);
694 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100695 if (dev < far_set_start)
696 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700697 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000698 while (sector >= geo->stride) {
699 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100700 if (dev < (geo->near_copies + far_set_start))
701 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700702 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000703 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700704 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000705 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700706 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000707 vchunk = chunk * geo->raid_disks + dev;
708 sector_div(vchunk, geo->near_copies);
709 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/*
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.
720 *
721 * If there are 2 mirrors in the same 2 devices, performance degrades
722 * because position is mirror, not device based.
723 *
724 * The rdev for the device selected will have nr_pending incremented.
725 */
726
727/*
728 * FIXME: possibly should rethink readbalancing and do it differently
729 * depending on near_copies / far_copies geometry.
730 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100731static struct md_rdev *read_balance(struct r10conf *conf,
732 struct r10bio *r10_bio,
733 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000735 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000736 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000737 int sectors = r10_bio->sectors;
738 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000739 sector_t new_distance, best_dist;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700740 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000741 int do_balance;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700742 int best_dist_slot, best_pending_slot;
743 bool has_nonrot_disk = false;
744 unsigned int min_pending;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000745 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 raid10_find_phys(conf, r10_bio);
748 rcu_read_lock();
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700749 best_dist_slot = -1;
750 min_pending = UINT_MAX;
751 best_dist_rdev = NULL;
752 best_pending_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000753 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000754 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000755 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100756 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
758 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800759 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Guoqing Jiangd4098c72017-10-24 15:11:50 +0800763 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)))
NeilBrown56d99122011-05-11 14:27:03 +1000768 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
NeilBrown56d99122011-05-11 14:27:03 +1000770 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000771 sector_t first_bad;
772 int bad_sectors;
773 sector_t dev_sector;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700774 unsigned int pending;
775 bool nonrot;
NeilBrown856e08e2011-07-28 11:39:23 +1000776
NeilBrown56d99122011-05-11 14:27:03 +1000777 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000779 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100780 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);
NeilBrown050b6612012-03-19 12:46:39 +1100784 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700785 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100786 continue;
787 if (!test_bit(In_sync, &rdev->flags) &&
788 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000789 continue;
790
NeilBrown856e08e2011-07-28 11:39:23 +1000791 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 */
796 continue;
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.
801 */
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;
807 } else {
808 sector_t good_sectors =
809 first_bad - dev_sector;
810 if (good_sectors > best_good_sectors) {
811 best_good_sectors = good_sectors;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700812 best_dist_slot = slot;
813 best_dist_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000814 }
815 if (!do_balance)
816 /* Must read from here */
817 break;
818 }
819 continue;
820 } else
821 best_good_sectors = sectors;
822
NeilBrown56d99122011-05-11 14:27:03 +1000823 if (!do_balance)
824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Christoph Hellwig10f0d2a52022-04-15 06:52:42 +0200826 nonrot = bdev_nonrot(rdev->bdev);
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700827 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;
833 }
834
835 if (best_dist_slot >= 0)
NeilBrown8d3ca832016-11-18 16:16:12 +1100836 /* At least 2 disks to choose from so failfast is OK */
837 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800838 /* 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.
841 */
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700842 if (geo->near_copies > 1 && !pending)
NeilBrown8d3ca832016-11-18 16:16:12 +1100843 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800844
845 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100846 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000847 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800848 else
NeilBrown56d99122011-05-11 14:27:03 +1000849 new_distance = abs(r10_bio->devs[slot].addr -
850 conf->mirrors[disk].head_position);
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700851
NeilBrown56d99122011-05-11 14:27:03 +1000852 if (new_distance < best_dist) {
853 best_dist = new_distance;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700854 best_dist_slot = slot;
855 best_dist_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 }
NeilBrownabbf0982011-12-23 10:17:54 +1100858 if (slot >= conf->copies) {
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700859 if (has_nonrot_disk) {
860 slot = best_pending_slot;
861 rdev = best_pending_rdev;
862 } else {
863 slot = best_dist_slot;
864 rdev = best_dist_rdev;
865 }
NeilBrownabbf0982011-12-23 10:17:54 +1100866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
NeilBrown56d99122011-05-11 14:27:03 +1000868 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000869 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000870 r10_bio->read_slot = slot;
871 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100872 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000874 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
NeilBrown96c3fd12011-12-23 10:17:54 +1100876 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
NeilBrowne879a872011-10-11 16:49:02 +1100879static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800880{
881 /* Any writes that have been queued but are awaiting
882 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800883 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800884 spin_lock_irq(&conf->device_lock);
885
886 if (conf->pending_bio_list.head) {
Shaohua Li18022a12017-12-01 12:12:34 -0800887 struct blk_plug plug;
NeilBrowna35e63e2008-03-04 14:29:29 -0800888 struct bio *bio;
Shaohua Li18022a12017-12-01 12:12:34 -0800889
NeilBrowna35e63e2008-03-04 14:29:29 -0800890 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800891 spin_unlock_irq(&conf->device_lock);
NeilBrown474beb52017-12-04 08:21:04 +1100892
893 /*
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
900 * thread state
901 */
902 __set_current_state(TASK_RUNNING);
903
Shaohua Li18022a12017-12-01 12:12:34 -0800904 blk_start_plug(&plug);
NeilBrowna35e63e2008-03-04 14:29:29 -0800905 /* flush any pending bitmap writes to disk
906 * before proceeding w/ I/O */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700907 md_bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100908 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800909
910 while (bio) { /* submit pending writes */
911 struct bio *next = bio->bi_next;
Christoph Hellwig309dca302021-01-24 11:02:34 +0100912 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800913 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200914 bio_set_dev(bio, rdev->bdev);
NeilBrowna9ae93c2016-11-04 16:46:03 +1100915 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +0800916 bio_io_error(bio);
NeilBrowna9ae93c2016-11-04 16:46:03 +1100917 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig70200572022-04-15 06:52:55 +0200918 !bdev_max_discard_sectors(bio->bi_bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +1100919 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200920 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100921 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200922 submit_bio_noacct(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800923 bio = next;
924 }
Shaohua Li18022a12017-12-01 12:12:34 -0800925 blk_finish_plug(&plug);
NeilBrowna35e63e2008-03-04 14:29:29 -0800926 } else
927 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800928}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100929
NeilBrown0a27ec92006-01-06 00:20:13 -0800930/* Barriers....
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
936 * normal IO.
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.
943 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
NeilBrowne879a872011-10-11 16:49:02 +1100952static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Yu Kuaib9b083f2022-09-16 19:34:28 +0800954 write_seqlock_irq(&conf->resync_lock);
Yu Kuai9fdfe6d2023-03-10 15:38:52 +0800955
956 if (WARN_ON_ONCE(force && !conf->barrier))
957 force = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
NeilBrown6cce3b22006-01-06 00:20:16 -0800959 /* Wait until no block IO is waiting (unless 'force') */
Yu Kuaib9b083f2022-09-16 19:34:28 +0800960 wait_event_barrier(conf, force || !conf->nr_waiting);
NeilBrown0a27ec92006-01-06 00:20:13 -0800961
962 /* block any new IO from starting */
Yu Kuaib9b083f2022-09-16 19:34:28 +0800963 WRITE_ONCE(conf->barrier, conf->barrier + 1);
NeilBrown0a27ec92006-01-06 00:20:13 -0800964
NeilBrownc3b328a2011-04-18 18:25:43 +1000965 /* Now wait for all pending IO to complete */
Yu Kuaib9b083f2022-09-16 19:34:28 +0800966 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) &&
967 conf->barrier < RESYNC_DEPTH);
NeilBrown0a27ec92006-01-06 00:20:13 -0800968
Yu Kuaib9b083f2022-09-16 19:34:28 +0800969 write_sequnlock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
NeilBrowne879a872011-10-11 16:49:02 +1100972static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800973{
974 unsigned long flags;
Yu Kuaib9b083f2022-09-16 19:34:28 +0800975
976 write_seqlock_irqsave(&conf->resync_lock, flags);
977 WRITE_ONCE(conf->barrier, conf->barrier - 1);
978 write_sequnlock_irqrestore(&conf->resync_lock, flags);
NeilBrown0a27ec92006-01-06 00:20:13 -0800979 wake_up(&conf->wait_barrier);
980}
981
Yu Kuaied2e0632022-09-16 19:34:24 +0800982static bool stop_waiting_barrier(struct r10conf *conf)
983{
984 struct bio_list *bio_list = current->bio_list;
Yu Kuai44693152023-05-23 10:10:17 +0800985 struct md_thread *thread;
Yu Kuaied2e0632022-09-16 19:34:24 +0800986
987 /* barrier is dropped */
988 if (!conf->barrier)
989 return true;
990
991 /*
992 * If there are already pending requests (preventing the barrier from
993 * rising completely), and the pre-process bio queue isn't empty, then
994 * don't wait, as we need to empty that queue to get the nr_pending
995 * count down.
996 */
997 if (atomic_read(&conf->nr_pending) && bio_list &&
998 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1])))
999 return true;
1000
Yu Kuai44693152023-05-23 10:10:17 +08001001 /* daemon thread must exist while handling io */
1002 thread = rcu_dereference_protected(conf->mddev->thread, true);
Li Nan72c215e2023-02-22 12:09:59 +08001003 /*
1004 * move on if io is issued from raid10d(), nr_pending is not released
1005 * from original io(see handle_read_error()). All raise barrier is
1006 * blocked until this io is done.
1007 */
Yu Kuai44693152023-05-23 10:10:17 +08001008 if (thread->tsk == current) {
Li Nan72c215e2023-02-22 12:09:59 +08001009 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0);
Yu Kuaied2e0632022-09-16 19:34:24 +08001010 return true;
Li Nan72c215e2023-02-22 12:09:59 +08001011 }
Yu Kuaied2e0632022-09-16 19:34:24 +08001012
1013 return false;
1014}
1015
Yu Kuaib9b083f2022-09-16 19:34:28 +08001016static bool wait_barrier_nolock(struct r10conf *conf)
1017{
1018 unsigned int seq = read_seqbegin(&conf->resync_lock);
1019
1020 if (READ_ONCE(conf->barrier))
1021 return false;
1022
1023 atomic_inc(&conf->nr_pending);
1024 if (!read_seqretry(&conf->resync_lock, seq))
1025 return true;
1026
1027 if (atomic_dec_and_test(&conf->nr_pending))
1028 wake_up_barrier(conf);
1029
1030 return false;
1031}
1032
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001033static bool wait_barrier(struct r10conf *conf, bool nowait)
NeilBrown0a27ec92006-01-06 00:20:13 -08001034{
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001035 bool ret = true;
1036
Yu Kuaib9b083f2022-09-16 19:34:28 +08001037 if (wait_barrier_nolock(conf))
1038 return true;
1039
1040 write_seqlock_irq(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08001041 if (conf->barrier) {
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001042 /* Return false when nowait flag is set */
1043 if (nowait) {
1044 ret = false;
1045 } else {
Yu Kuai0de57e52022-09-16 19:34:25 +08001046 conf->nr_waiting++;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001047 raid10_log(conf->mddev, "wait barrier");
Yu Kuaib9b083f2022-09-16 19:34:28 +08001048 wait_event_barrier(conf, stop_waiting_barrier(conf));
Yu Kuai0de57e52022-09-16 19:34:25 +08001049 conf->nr_waiting--;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001050 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001051 if (!conf->nr_waiting)
1052 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -08001053 }
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001054 /* Only increment nr_pending when we wait */
1055 if (ret)
1056 atomic_inc(&conf->nr_pending);
Yu Kuaib9b083f2022-09-16 19:34:28 +08001057 write_sequnlock_irq(&conf->resync_lock);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001058 return ret;
NeilBrown0a27ec92006-01-06 00:20:13 -08001059}
1060
NeilBrowne879a872011-10-11 16:49:02 +11001061static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001062{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001063 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1064 (conf->array_freeze_pending))
Yu Kuai0c0be98b2022-09-16 19:34:26 +08001065 wake_up_barrier(conf);
NeilBrown0a27ec92006-01-06 00:20:13 -08001066}
1067
NeilBrowne2d59922013-06-12 11:01:22 +10001068static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -08001069{
1070 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001071 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001072 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001073 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001074 * This is called in the context of one normal IO request
1075 * that has failed. Thus any sync request that might be pending
1076 * will be blocked by nr_pending, and we need to wait for
1077 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001078 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001079 * must match the number of pending IOs (nr_pending) before
1080 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001081 */
Yu Kuaib9b083f2022-09-16 19:34:28 +08001082 write_seqlock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001083 conf->array_freeze_pending++;
Yu Kuaib9b083f2022-09-16 19:34:28 +08001084 WRITE_ONCE(conf->barrier, conf->barrier + 1);
NeilBrown4443ae12006-01-06 00:20:28 -08001085 conf->nr_waiting++;
Yu Kuaib9b083f2022-09-16 19:34:28 +08001086 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) ==
1087 conf->nr_queued + extra, flush_pending_writes(conf));
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001088 conf->array_freeze_pending--;
Yu Kuaib9b083f2022-09-16 19:34:28 +08001089 write_sequnlock_irq(&conf->resync_lock);
NeilBrown4443ae12006-01-06 00:20:28 -08001090}
1091
NeilBrowne879a872011-10-11 16:49:02 +11001092static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001093{
1094 /* reverse the effect of the freeze */
Yu Kuaib9b083f2022-09-16 19:34:28 +08001095 write_seqlock_irq(&conf->resync_lock);
1096 WRITE_ONCE(conf->barrier, conf->barrier - 1);
NeilBrown4443ae12006-01-06 00:20:28 -08001097 conf->nr_waiting--;
1098 wake_up(&conf->wait_barrier);
Yu Kuaib9b083f2022-09-16 19:34:28 +08001099 write_sequnlock_irq(&conf->resync_lock);
NeilBrown4443ae12006-01-06 00:20:28 -08001100}
1101
NeilBrownf8c9e742012-05-21 09:28:33 +10001102static sector_t choose_data_offset(struct r10bio *r10_bio,
1103 struct md_rdev *rdev)
1104{
1105 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1106 test_bit(R10BIO_Previous, &r10_bio->state))
1107 return rdev->data_offset;
1108 else
1109 return rdev->new_data_offset;
1110}
1111
NeilBrown57c67df2012-10-11 13:32:13 +11001112static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1113{
Mariusz Tkaczykdaae1612022-01-17 12:38:47 +01001114 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb);
NeilBrown57c67df2012-10-11 13:32:13 +11001115 struct mddev *mddev = plug->cb.data;
1116 struct r10conf *conf = mddev->private;
1117 struct bio *bio;
1118
NeilBrown874807a2012-11-27 12:14:40 +11001119 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001120 spin_lock_irq(&conf->device_lock);
1121 bio_list_merge(&conf->pending_bio_list, &plug->pending);
NeilBrown57c67df2012-10-11 13:32:13 +11001122 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001123 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001124 md_wakeup_thread(mddev->thread);
1125 kfree(plug);
1126 return;
1127 }
1128
1129 /* we aren't scheduling, so we can do the write-out directly. */
1130 bio = bio_list_get(&plug->pending);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001131 md_bitmap_unplug(mddev->bitmap);
NeilBrown57c67df2012-10-11 13:32:13 +11001132 wake_up(&conf->wait_barrier);
1133
1134 while (bio) { /* submit pending writes */
1135 struct bio *next = bio->bi_next;
Christoph Hellwig309dca302021-01-24 11:02:34 +01001136 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001137 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001138 bio_set_dev(bio, rdev->bdev);
NeilBrowna9ae93c2016-11-04 16:46:03 +11001139 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08001140 bio_io_error(bio);
NeilBrowna9ae93c2016-11-04 16:46:03 +11001141 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig70200572022-04-15 06:52:55 +02001142 !bdev_max_discard_sectors(bio->bi_bdev)))
Shaohua Li32f9f572013-04-28 18:26:38 +08001143 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001144 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001145 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001146 submit_bio_noacct(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001147 bio = next;
1148 }
1149 kfree(plug);
1150}
1151
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001152/*
1153 * 1. Register the new request and wait if the reconstruction thread has put
1154 * up a bar for new requests. Continue immediately if no resync is active
1155 * currently.
1156 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1157 */
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001158static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf,
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001159 struct bio *bio, sector_t sectors)
1160{
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001161 /* Bail out if REQ_NOWAIT is set for the bio */
1162 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) {
1163 bio_wouldblock_error(bio);
1164 return false;
1165 }
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001166 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1167 bio->bi_iter.bi_sector < conf->reshape_progress &&
1168 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001169 allow_barrier(conf);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001170 if (bio->bi_opf & REQ_NOWAIT) {
1171 bio_wouldblock_error(bio);
1172 return false;
1173 }
1174 raid10_log(conf->mddev, "wait reshape");
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001175 wait_event(conf->wait_barrier,
1176 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1177 conf->reshape_progress >= bio->bi_iter.bi_sector +
1178 sectors);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001179 wait_barrier(conf, false);
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001180 }
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001181 return true;
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001182}
1183
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001184static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1185 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
NeilBrowne879a872011-10-11 16:49:02 +11001187 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct bio *read_bio;
Bart Van Asschecb1802f2022-07-14 11:07:01 -07001189 const enum req_op op = bio_op(bio);
1190 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001191 int max_sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001192 struct md_rdev *rdev;
NeilBrown545250f2017-04-05 14:05:51 +10001193 char b[BDEVNAME_SIZE];
1194 int slot = r10_bio->read_slot;
1195 struct md_rdev *err_rdev = NULL;
1196 gfp_t gfp = GFP_NOIO;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001197
Kevin Vigor93decc52020-11-06 14:20:34 -08001198 if (slot >= 0 && r10_bio->devs[slot].rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001199 /*
1200 * This is an error retry, but we cannot
1201 * safely dereference the rdev in the r10_bio,
1202 * we must use the one in conf.
1203 * If it has already been disconnected (unlikely)
1204 * we lose the device name in error messages.
1205 */
1206 int disk;
1207 /*
1208 * As we are blocking raid10, it is a little safer to
1209 * use __GFP_HIGH.
1210 */
1211 gfp = GFP_NOIO | __GFP_HIGH;
1212
1213 rcu_read_lock();
1214 disk = r10_bio->devs[slot].devnum;
1215 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1216 if (err_rdev)
Christoph Hellwig900d1562022-07-13 07:53:17 +02001217 snprintf(b, sizeof(b), "%pg", err_rdev->bdev);
NeilBrown545250f2017-04-05 14:05:51 +10001218 else {
1219 strcpy(b, "???");
1220 /* This never gets dereferenced */
1221 err_rdev = r10_bio->devs[slot].rdev;
1222 }
1223 rcu_read_unlock();
1224 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001225
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001226 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors))
1227 return;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001228 rdev = read_balance(conf, r10_bio, &max_sectors);
1229 if (!rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001230 if (err_rdev) {
1231 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1232 mdname(mddev), b,
1233 (unsigned long long)r10_bio->sector);
1234 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001235 raid_end_bio_io(r10_bio);
1236 return;
1237 }
NeilBrown545250f2017-04-05 14:05:51 +10001238 if (err_rdev)
Christoph Hellwig913cce52022-05-12 08:19:13 +02001239 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
NeilBrown545250f2017-04-05 14:05:51 +10001240 mdname(mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +02001241 rdev->bdev,
NeilBrown545250f2017-04-05 14:05:51 +10001242 (unsigned long long)r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001243 if (max_sectors < bio_sectors(bio)) {
1244 struct bio *split = bio_split(bio, max_sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001245 gfp, &conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001246 bio_chain(split, bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001247 allow_barrier(conf);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001248 submit_bio_noacct(bio);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001249 wait_barrier(conf, false);
NeilBrownfc9977d2017-04-05 14:05:51 +10001250 bio = split;
1251 r10_bio->master_bio = bio;
1252 r10_bio->sectors = max_sectors;
1253 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001254 slot = r10_bio->read_slot;
1255
Yu Kuai7cddb052023-03-14 09:22:58 +08001256 if (!r10_bio->start_time &&
1257 blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
Guoqing Jiang528bc2c2021-05-25 17:46:22 +08001258 r10_bio->start_time = bio_start_io_acct(bio);
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001259 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001260
1261 r10_bio->devs[slot].bio = read_bio;
1262 r10_bio->devs[slot].rdev = rdev;
1263
1264 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1265 choose_data_offset(r10_bio, rdev);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001266 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01001267 read_bio->bi_opf = op | do_sync;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001268 if (test_bit(FailFast, &rdev->flags) &&
1269 test_bit(R10BIO_FailFast, &r10_bio->state))
1270 read_bio->bi_opf |= MD_FAILFAST;
1271 read_bio->bi_private = r10_bio;
1272
1273 if (mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +01001274 trace_block_bio_remap(read_bio, disk_devt(mddev->gendisk),
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001275 r10_bio->sector);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001276 submit_bio_noacct(read_bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001277 return;
1278}
1279
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001280static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1281 struct bio *bio, bool replacement,
NeilBrownfc9977d2017-04-05 14:05:51 +10001282 int n_copy)
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001283{
Bart Van Asschecb1802f2022-07-14 11:07:01 -07001284 const enum req_op op = bio_op(bio);
1285 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1286 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001287 unsigned long flags;
1288 struct blk_plug_cb *cb;
Mariusz Tkaczykdaae1612022-01-17 12:38:47 +01001289 struct raid1_plug_cb *plug = NULL;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001290 struct r10conf *conf = mddev->private;
1291 struct md_rdev *rdev;
1292 int devnum = r10_bio->devs[n_copy].devnum;
1293 struct bio *mbio;
1294
1295 if (replacement) {
1296 rdev = conf->mirrors[devnum].replacement;
1297 if (rdev == NULL) {
1298 /* Replacement just got moved to main 'rdev' */
1299 smp_mb();
1300 rdev = conf->mirrors[devnum].rdev;
1301 }
1302 } else
1303 rdev = conf->mirrors[devnum].rdev;
1304
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001305 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001306 if (replacement)
1307 r10_bio->devs[n_copy].repl_bio = mbio;
1308 else
1309 r10_bio->devs[n_copy].bio = mbio;
1310
1311 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1312 choose_data_offset(r10_bio, rdev));
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001313 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01001314 mbio->bi_opf = op | do_sync | do_fua;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001315 if (!replacement && test_bit(FailFast,
1316 &conf->mirrors[devnum].rdev->flags)
1317 && enough(conf, devnum))
1318 mbio->bi_opf |= MD_FAILFAST;
1319 mbio->bi_private = r10_bio;
1320
1321 if (conf->mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +01001322 trace_block_bio_remap(mbio, disk_devt(conf->mddev->gendisk),
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001323 r10_bio->sector);
1324 /* flush_pending_writes() needs access to the rdev so...*/
Christoph Hellwig309dca302021-01-24 11:02:34 +01001325 mbio->bi_bdev = (void *)rdev;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001326
1327 atomic_inc(&r10_bio->remaining);
1328
1329 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1330 if (cb)
Mariusz Tkaczykdaae1612022-01-17 12:38:47 +01001331 plug = container_of(cb, struct raid1_plug_cb, cb);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001332 else
1333 plug = NULL;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001334 if (plug) {
1335 bio_list_add(&plug->pending, mbio);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001336 } else {
Shaohua Li23b245c2017-05-10 08:47:11 -07001337 spin_lock_irqsave(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001338 bio_list_add(&conf->pending_bio_list, mbio);
Shaohua Li23b245c2017-05-10 08:47:11 -07001339 spin_unlock_irqrestore(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001340 md_wakeup_thread(mddev->thread);
Shaohua Li23b245c2017-05-10 08:47:11 -07001341 }
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001342}
1343
Xiao Nif2e7e262021-02-04 15:50:45 +08001344static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1345{
1346 int i;
1347 struct r10conf *conf = mddev->private;
1348 struct md_rdev *blocked_rdev;
1349
1350retry_wait:
1351 blocked_rdev = NULL;
1352 rcu_read_lock();
1353 for (i = 0; i < conf->copies; i++) {
1354 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1355 struct md_rdev *rrdev = rcu_dereference(
1356 conf->mirrors[i].replacement);
1357 if (rdev == rrdev)
1358 rrdev = NULL;
1359 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1360 atomic_inc(&rdev->nr_pending);
1361 blocked_rdev = rdev;
1362 break;
1363 }
1364 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1365 atomic_inc(&rrdev->nr_pending);
1366 blocked_rdev = rrdev;
1367 break;
1368 }
1369
1370 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1371 sector_t first_bad;
1372 sector_t dev_sector = r10_bio->devs[i].addr;
1373 int bad_sectors;
1374 int is_bad;
1375
1376 /*
1377 * Discard request doesn't care the write result
1378 * so it doesn't need to wait blocked disk here.
1379 */
1380 if (!r10_bio->sectors)
1381 continue;
1382
1383 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1384 &first_bad, &bad_sectors);
1385 if (is_bad < 0) {
1386 /*
1387 * Mustn't write here until the bad block
1388 * is acknowledged
1389 */
1390 atomic_inc(&rdev->nr_pending);
1391 set_bit(BlockedBadBlocks, &rdev->flags);
1392 blocked_rdev = rdev;
1393 break;
1394 }
1395 }
1396 }
1397 rcu_read_unlock();
1398
1399 if (unlikely(blocked_rdev)) {
1400 /* Have to wait for this device to get unblocked, then retry */
1401 allow_barrier(conf);
1402 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1403 __func__, blocked_rdev->raid_disk);
1404 md_wait_for_blocked_rdev(blocked_rdev, mddev);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001405 wait_barrier(conf, false);
Xiao Nif2e7e262021-02-04 15:50:45 +08001406 goto retry_wait;
1407 }
1408}
1409
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001410static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1411 struct r10bio *r10_bio)
1412{
1413 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 int i;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001415 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001416 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Guoqing Jiangcb8a7a72017-10-24 15:11:51 +08001418 if ((mddev_is_clustered(mddev) &&
1419 md_cluster_ops->area_resyncing(mddev, WRITE,
1420 bio->bi_iter.bi_sector,
1421 bio_end_sector(bio)))) {
1422 DEFINE_WAIT(w);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001423 /* Bail out if REQ_NOWAIT is set for the bio */
1424 if (bio->bi_opf & REQ_NOWAIT) {
1425 bio_wouldblock_error(bio);
1426 return;
1427 }
Guoqing Jiangcb8a7a72017-10-24 15:11:51 +08001428 for (;;) {
1429 prepare_to_wait(&conf->wait_barrier,
1430 &w, TASK_IDLE);
1431 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1432 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1433 break;
1434 schedule();
1435 }
1436 finish_wait(&conf->wait_barrier, &w);
1437 }
1438
NeilBrownfc9977d2017-04-05 14:05:51 +10001439 sectors = r10_bio->sectors;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001440 if (!regular_request_wait(mddev, conf, bio, sectors))
1441 return;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001442 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001443 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001444 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1445 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1446 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1447 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001448 /* Need to update reshape_position in metadata */
1449 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001450 set_mask_bits(&mddev->sb_flags, 0,
1451 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001452 md_wakeup_thread(mddev->thread);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001453 if (bio->bi_opf & REQ_NOWAIT) {
1454 allow_barrier(conf);
1455 bio_wouldblock_error(bio);
1456 return;
1457 }
NeilBrown578b54a2016-11-14 16:30:21 +11001458 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001459 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001460 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001461
1462 conf->reshape_safe = mddev->reshape_position;
1463 }
1464
Dan Williams6bfe0b42008-04-30 00:52:32 -07001465 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 * inc refcount on their rdev. Record them by setting
1467 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001468 * If there are known/acknowledged bad blocks on any device
1469 * on which we have seen a write error, we want to avoid
1470 * writing to those blocks. This potentially requires several
1471 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001472 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001474
NeilBrown69335ef2011-12-23 10:17:54 +11001475 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 raid10_find_phys(conf, r10_bio);
Xiao Nif2e7e262021-02-04 15:50:45 +08001477
1478 wait_blocked_dev(mddev, r10_bio);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001481 max_sectors = r10_bio->sectors;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 for (i = 0; i < conf->copies; i++) {
1484 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001485 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001486 struct md_rdev *rrdev = rcu_dereference(
1487 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001488 if (rdev == rrdev)
1489 rrdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001490 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001491 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001492 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001493 rrdev = NULL;
1494
NeilBrownd4432c22011-07-28 11:39:24 +10001495 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001496 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001497
1498 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001499 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001500 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001501 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001502 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001503 sector_t first_bad;
1504 sector_t dev_sector = r10_bio->devs[i].addr;
1505 int bad_sectors;
1506 int is_bad;
1507
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001508 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001509 &first_bad, &bad_sectors);
NeilBrownd4432c22011-07-28 11:39:24 +10001510 if (is_bad && first_bad <= dev_sector) {
1511 /* Cannot write here at all */
1512 bad_sectors -= (dev_sector - first_bad);
1513 if (bad_sectors < max_sectors)
1514 /* Mustn't write more than bad_sectors
1515 * to other devices yet
1516 */
1517 max_sectors = bad_sectors;
1518 /* We don't set R10BIO_Degraded as that
1519 * only applies if the disk is missing,
1520 * so it might be re-added, and we want to
1521 * know to recover this chunk.
1522 * In this case the device is here, and the
1523 * fact that this chunk is not in-sync is
1524 * recorded in the bad block log.
1525 */
1526 continue;
1527 }
1528 if (is_bad) {
1529 int good_sectors = first_bad - dev_sector;
1530 if (good_sectors < max_sectors)
1531 max_sectors = good_sectors;
1532 }
1533 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001534 if (rdev) {
1535 r10_bio->devs[i].bio = bio;
1536 atomic_inc(&rdev->nr_pending);
1537 }
NeilBrown475b0322011-12-23 10:17:55 +11001538 if (rrdev) {
1539 r10_bio->devs[i].repl_bio = bio;
1540 atomic_inc(&rrdev->nr_pending);
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 rcu_read_unlock();
1544
NeilBrown6b6c8112017-03-15 14:05:13 +11001545 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001546 r10_bio->sectors = max_sectors;
NeilBrownfc9977d2017-04-05 14:05:51 +10001547
1548 if (r10_bio->sectors < bio_sectors(bio)) {
1549 struct bio *split = bio_split(bio, r10_bio->sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001550 GFP_NOIO, &conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001551 bio_chain(split, bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001552 allow_barrier(conf);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001553 submit_bio_noacct(bio);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001554 wait_barrier(conf, false);
NeilBrownfc9977d2017-04-05 14:05:51 +10001555 bio = split;
1556 r10_bio->master_bio = bio;
NeilBrownd4432c22011-07-28 11:39:24 +10001557 }
NeilBrownd4432c22011-07-28 11:39:24 +10001558
Guoqing Jiang528bc2c2021-05-25 17:46:22 +08001559 if (blk_queue_io_stat(bio->bi_bdev->bd_disk->queue))
1560 r10_bio->start_time = bio_start_io_acct(bio);
NeilBrown4e780642010-10-19 12:54:01 +11001561 atomic_set(&r10_bio->remaining, 1);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001562 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 for (i = 0; i < conf->copies; i++) {
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001565 if (r10_bio->devs[i].bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001566 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001567 if (r10_bio->devs[i].repl_bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001568 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
NeilBrownd4432c22011-07-28 11:39:24 +10001569 }
NeilBrown079fa162011-09-10 17:21:23 +10001570 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001571}
1572
NeilBrownfc9977d2017-04-05 14:05:51 +10001573static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001574{
1575 struct r10conf *conf = mddev->private;
1576 struct r10bio *r10_bio;
1577
Kent Overstreetafeee512018-05-20 18:25:52 -04001578 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001579
1580 r10_bio->master_bio = bio;
NeilBrownfc9977d2017-04-05 14:05:51 +10001581 r10_bio->sectors = sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001582
1583 r10_bio->mddev = mddev;
1584 r10_bio->sector = bio->bi_iter.bi_sector;
1585 r10_bio->state = 0;
Kevin Vigor93decc52020-11-06 14:20:34 -08001586 r10_bio->read_slot = -1;
Yu Kuai7cddb052023-03-14 09:22:58 +08001587 r10_bio->start_time = 0;
Xiao Nic2968282021-02-04 15:50:44 +08001588 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1589 conf->geo.raid_disks);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001590
1591 if (bio_data_dir(bio) == READ)
1592 raid10_read_request(mddev, bio, r10_bio);
1593 else
1594 raid10_write_request(mddev, bio, r10_bio);
1595}
1596
Xiao Ni254c2712021-02-04 15:50:47 +08001597static void raid_end_discard_bio(struct r10bio *r10bio)
1598{
1599 struct r10conf *conf = r10bio->mddev->private;
1600 struct r10bio *first_r10bio;
1601
1602 while (atomic_dec_and_test(&r10bio->remaining)) {
1603
1604 allow_barrier(conf);
1605
1606 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1607 first_r10bio = (struct r10bio *)r10bio->master_bio;
1608 free_r10bio(r10bio);
1609 r10bio = first_r10bio;
1610 } else {
1611 md_write_end(r10bio->mddev);
1612 bio_endio(r10bio->master_bio);
1613 free_r10bio(r10bio);
1614 break;
1615 }
1616 }
1617}
1618
Xiao Nid30588b2021-02-04 15:50:46 +08001619static void raid10_end_discard_request(struct bio *bio)
1620{
1621 struct r10bio *r10_bio = bio->bi_private;
1622 struct r10conf *conf = r10_bio->mddev->private;
1623 struct md_rdev *rdev = NULL;
1624 int dev;
1625 int slot, repl;
1626
1627 /*
1628 * We don't care the return value of discard bio
1629 */
1630 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1631 set_bit(R10BIO_Uptodate, &r10_bio->state);
1632
1633 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1634 if (repl)
1635 rdev = conf->mirrors[dev].replacement;
1636 if (!rdev) {
1637 /*
1638 * raid10_remove_disk uses smp_mb to make sure rdev is set to
1639 * replacement before setting replacement to NULL. It can read
Jiangshan Yidccb8ad2023-02-14 14:40:13 +08001640 * rdev first without barrier protect even replacement is NULL
Xiao Nid30588b2021-02-04 15:50:46 +08001641 */
1642 smp_rmb();
1643 rdev = conf->mirrors[dev].rdev;
1644 }
1645
Xiao Ni254c2712021-02-04 15:50:47 +08001646 raid_end_discard_bio(r10_bio);
Xiao Nid30588b2021-02-04 15:50:46 +08001647 rdev_dec_pending(rdev, conf->mddev);
1648}
1649
1650/*
1651 * There are some limitations to handle discard bio
1652 * 1st, the discard size is bigger than stripe_size*2.
1653 * 2st, if the discard bio spans reshape progress, we use the old way to
1654 * handle discard bio
1655 */
1656static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1657{
1658 struct r10conf *conf = mddev->private;
1659 struct geom *geo = &conf->geo;
Xiao Ni254c2712021-02-04 15:50:47 +08001660 int far_copies = geo->far_copies;
1661 bool first_copy = true;
1662 struct r10bio *r10_bio, *first_r10bio;
Xiao Nid30588b2021-02-04 15:50:46 +08001663 struct bio *split;
1664 int disk;
1665 sector_t chunk;
1666 unsigned int stripe_size;
1667 unsigned int stripe_data_disks;
1668 sector_t split_size;
1669 sector_t bio_start, bio_end;
1670 sector_t first_stripe_index, last_stripe_index;
1671 sector_t start_disk_offset;
1672 unsigned int start_disk_index;
1673 sector_t end_disk_offset;
1674 unsigned int end_disk_index;
1675 unsigned int remainder;
1676
1677 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1678 return -EAGAIN;
1679
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001680 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) {
1681 bio_wouldblock_error(bio);
1682 return 0;
1683 }
1684 wait_barrier(conf, false);
Xiao Nid30588b2021-02-04 15:50:46 +08001685
1686 /*
1687 * Check reshape again to avoid reshape happens after checking
1688 * MD_RECOVERY_RESHAPE and before wait_barrier
1689 */
1690 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1691 goto out;
1692
1693 if (geo->near_copies)
1694 stripe_data_disks = geo->raid_disks / geo->near_copies +
1695 geo->raid_disks % geo->near_copies;
1696 else
1697 stripe_data_disks = geo->raid_disks;
1698
1699 stripe_size = stripe_data_disks << geo->chunk_shift;
1700
1701 bio_start = bio->bi_iter.bi_sector;
1702 bio_end = bio_end_sector(bio);
1703
1704 /*
1705 * Maybe one discard bio is smaller than strip size or across one
1706 * stripe and discard region is larger than one stripe size. For far
1707 * offset layout, if the discard region is not aligned with stripe
1708 * size, there is hole when we submit discard bio to member disk.
1709 * For simplicity, we only handle discard bio which discard region
1710 * is bigger than stripe_size * 2
1711 */
1712 if (bio_sectors(bio) < stripe_size*2)
1713 goto out;
1714
1715 /*
1716 * Keep bio aligned with strip size.
1717 */
1718 div_u64_rem(bio_start, stripe_size, &remainder);
1719 if (remainder) {
1720 split_size = stripe_size - remainder;
1721 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1722 bio_chain(split, bio);
1723 allow_barrier(conf);
1724 /* Resend the fist split part */
1725 submit_bio_noacct(split);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001726 wait_barrier(conf, false);
Xiao Nid30588b2021-02-04 15:50:46 +08001727 }
1728 div_u64_rem(bio_end, stripe_size, &remainder);
1729 if (remainder) {
1730 split_size = bio_sectors(bio) - remainder;
1731 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1732 bio_chain(split, bio);
1733 allow_barrier(conf);
1734 /* Resend the second split part */
1735 submit_bio_noacct(bio);
1736 bio = split;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001737 wait_barrier(conf, false);
Xiao Nid30588b2021-02-04 15:50:46 +08001738 }
1739
Xiao Nid30588b2021-02-04 15:50:46 +08001740 bio_start = bio->bi_iter.bi_sector;
1741 bio_end = bio_end_sector(bio);
1742
1743 /*
1744 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1745 * One stripe contains the chunks from all member disk (one chunk from
1746 * one disk at the same HBA address). For layout detail, see 'man md 4'
1747 */
1748 chunk = bio_start >> geo->chunk_shift;
1749 chunk *= geo->near_copies;
1750 first_stripe_index = chunk;
1751 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1752 if (geo->far_offset)
1753 first_stripe_index *= geo->far_copies;
1754 start_disk_offset = (bio_start & geo->chunk_mask) +
1755 (first_stripe_index << geo->chunk_shift);
1756
1757 chunk = bio_end >> geo->chunk_shift;
1758 chunk *= geo->near_copies;
1759 last_stripe_index = chunk;
1760 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1761 if (geo->far_offset)
1762 last_stripe_index *= geo->far_copies;
1763 end_disk_offset = (bio_end & geo->chunk_mask) +
1764 (last_stripe_index << geo->chunk_shift);
1765
Xiao Ni254c2712021-02-04 15:50:47 +08001766retry_discard:
1767 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1768 r10_bio->mddev = mddev;
1769 r10_bio->state = 0;
1770 r10_bio->sectors = 0;
1771 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1772 wait_blocked_dev(mddev, r10_bio);
1773
1774 /*
1775 * For far layout it needs more than one r10bio to cover all regions.
1776 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1777 * to record the discard bio. Other r10bio->master_bio record the first
1778 * r10bio. The first r10bio only release after all other r10bios finish.
1779 * The discard bio returns only first r10bio finishes
1780 */
1781 if (first_copy) {
1782 r10_bio->master_bio = bio;
1783 set_bit(R10BIO_Discard, &r10_bio->state);
1784 first_copy = false;
1785 first_r10bio = r10_bio;
1786 } else
1787 r10_bio->master_bio = (struct bio *)first_r10bio;
1788
Xiao Ni46d47032021-08-18 13:57:48 +08001789 /*
1790 * first select target devices under rcu_lock and
1791 * inc refcount on their rdev. Record them by setting
1792 * bios[x] to bio
1793 */
Xiao Nid30588b2021-02-04 15:50:46 +08001794 rcu_read_lock();
1795 for (disk = 0; disk < geo->raid_disks; disk++) {
1796 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1797 struct md_rdev *rrdev = rcu_dereference(
1798 conf->mirrors[disk].replacement);
1799
1800 r10_bio->devs[disk].bio = NULL;
1801 r10_bio->devs[disk].repl_bio = NULL;
1802
1803 if (rdev && (test_bit(Faulty, &rdev->flags)))
1804 rdev = NULL;
1805 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1806 rrdev = NULL;
1807 if (!rdev && !rrdev)
1808 continue;
1809
1810 if (rdev) {
1811 r10_bio->devs[disk].bio = bio;
1812 atomic_inc(&rdev->nr_pending);
1813 }
1814 if (rrdev) {
1815 r10_bio->devs[disk].repl_bio = bio;
1816 atomic_inc(&rrdev->nr_pending);
1817 }
1818 }
1819 rcu_read_unlock();
1820
1821 atomic_set(&r10_bio->remaining, 1);
1822 for (disk = 0; disk < geo->raid_disks; disk++) {
1823 sector_t dev_start, dev_end;
1824 struct bio *mbio, *rbio = NULL;
Xiao Nid30588b2021-02-04 15:50:46 +08001825
1826 /*
1827 * Now start to calculate the start and end address for each disk.
1828 * The space between dev_start and dev_end is the discard region.
1829 *
1830 * For dev_start, it needs to consider three conditions:
1831 * 1st, the disk is before start_disk, you can imagine the disk in
1832 * the next stripe. So the dev_start is the start address of next
1833 * stripe.
1834 * 2st, the disk is after start_disk, it means the disk is at the
1835 * same stripe of first disk
1836 * 3st, the first disk itself, we can use start_disk_offset directly
1837 */
1838 if (disk < start_disk_index)
1839 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1840 else if (disk > start_disk_index)
1841 dev_start = first_stripe_index * mddev->chunk_sectors;
1842 else
1843 dev_start = start_disk_offset;
1844
1845 if (disk < end_disk_index)
1846 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1847 else if (disk > end_disk_index)
1848 dev_end = last_stripe_index * mddev->chunk_sectors;
1849 else
1850 dev_end = end_disk_offset;
1851
1852 /*
1853 * It only handles discard bio which size is >= stripe size, so
Xiao Ni46d47032021-08-18 13:57:48 +08001854 * dev_end > dev_start all the time.
1855 * It doesn't need to use rcu lock to get rdev here. We already
1856 * add rdev->nr_pending in the first loop.
Xiao Nid30588b2021-02-04 15:50:46 +08001857 */
1858 if (r10_bio->devs[disk].bio) {
Xiao Ni46d47032021-08-18 13:57:48 +08001859 struct md_rdev *rdev = conf->mirrors[disk].rdev;
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001860 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1861 &mddev->bio_set);
Xiao Nid30588b2021-02-04 15:50:46 +08001862 mbio->bi_end_io = raid10_end_discard_request;
1863 mbio->bi_private = r10_bio;
1864 r10_bio->devs[disk].bio = mbio;
1865 r10_bio->devs[disk].devnum = disk;
1866 atomic_inc(&r10_bio->remaining);
1867 md_submit_discard_bio(mddev, rdev, mbio,
1868 dev_start + choose_data_offset(r10_bio, rdev),
1869 dev_end - dev_start);
1870 bio_endio(mbio);
1871 }
1872 if (r10_bio->devs[disk].repl_bio) {
Xiao Ni46d47032021-08-18 13:57:48 +08001873 struct md_rdev *rrdev = conf->mirrors[disk].replacement;
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001874 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1875 &mddev->bio_set);
Xiao Nid30588b2021-02-04 15:50:46 +08001876 rbio->bi_end_io = raid10_end_discard_request;
1877 rbio->bi_private = r10_bio;
1878 r10_bio->devs[disk].repl_bio = rbio;
1879 r10_bio->devs[disk].devnum = disk;
1880 atomic_inc(&r10_bio->remaining);
1881 md_submit_discard_bio(mddev, rrdev, rbio,
1882 dev_start + choose_data_offset(r10_bio, rrdev),
1883 dev_end - dev_start);
1884 bio_endio(rbio);
1885 }
1886 }
1887
Xiao Ni254c2712021-02-04 15:50:47 +08001888 if (!geo->far_offset && --far_copies) {
1889 first_stripe_index += geo->stride >> geo->chunk_shift;
1890 start_disk_offset += geo->stride;
1891 last_stripe_index += geo->stride >> geo->chunk_shift;
1892 end_disk_offset += geo->stride;
1893 atomic_inc(&first_r10bio->remaining);
1894 raid_end_discard_bio(r10_bio);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001895 wait_barrier(conf, false);
Xiao Ni254c2712021-02-04 15:50:47 +08001896 goto retry_discard;
Xiao Nid30588b2021-02-04 15:50:46 +08001897 }
1898
Xiao Ni254c2712021-02-04 15:50:47 +08001899 raid_end_discard_bio(r10_bio);
1900
Xiao Nid30588b2021-02-04 15:50:46 +08001901 return 0;
1902out:
1903 allow_barrier(conf);
1904 return -EAGAIN;
1905}
1906
NeilBrowncc27b0c2017-06-05 16:49:39 +10001907static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001908{
1909 struct r10conf *conf = mddev->private;
1910 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1911 int chunk_sects = chunk_mask + 1;
NeilBrownfc9977d2017-04-05 14:05:51 +10001912 int sectors = bio_sectors(bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001913
David Jeffery775d7832019-09-16 13:15:14 -04001914 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1915 && md_flush_request(mddev, bio))
NeilBrowncc27b0c2017-06-05 16:49:39 +10001916 return true;
Kent Overstreet20d01892013-11-23 18:21:01 -08001917
NeilBrowncc27b0c2017-06-05 16:49:39 +10001918 if (!md_write_start(mddev, bio))
1919 return false;
1920
Xiao Nid30588b2021-02-04 15:50:46 +08001921 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1922 if (!raid10_handle_discard(mddev, bio))
1923 return true;
1924
NeilBrownfc9977d2017-04-05 14:05:51 +10001925 /*
1926 * If this request crosses a chunk boundary, we need to split
1927 * it.
1928 */
1929 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1930 sectors > chunk_sects
1931 && (conf->geo.near_copies < conf->geo.raid_disks
1932 || conf->prev.near_copies <
1933 conf->prev.raid_disks)))
1934 sectors = chunk_sects -
1935 (bio->bi_iter.bi_sector &
1936 (chunk_sects - 1));
1937 __make_request(mddev, bio, sectors);
NeilBrown079fa162011-09-10 17:21:23 +10001938
1939 /* In case raid10d snuck in to freeze_array */
Yu Kuai0c0be98b2022-09-16 19:34:26 +08001940 wake_up_barrier(conf);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001941 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Shaohua Li849674e2016-01-20 13:52:20 -08001944static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
NeilBrowne879a872011-10-11 16:49:02 +11001946 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 int i;
1948
NeilBrown5cf00fc2012-05-21 09:28:20 +10001949 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001950 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001951 if (conf->geo.near_copies > 1)
1952 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1953 if (conf->geo.far_copies > 1) {
1954 if (conf->geo.far_offset)
1955 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001956 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001957 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001958 if (conf->geo.far_set_size != conf->geo.raid_disks)
1959 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001960 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001961 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1962 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001963 rcu_read_lock();
1964 for (i = 0; i < conf->geo.raid_disks; i++) {
1965 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1966 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1967 }
1968 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 seq_printf(seq, "]");
1970}
1971
NeilBrown700c7212011-07-27 11:00:36 +10001972/* check if there are enough drives for
1973 * every block to appear on atleast one.
1974 * Don't consider the device numbered 'ignore'
1975 * as we might be about to remove it.
1976 */
NeilBrown635f6412013-06-11 14:57:09 +10001977static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001978{
1979 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001980 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001981 int disks, ncopies;
1982 if (previous) {
1983 disks = conf->prev.raid_disks;
1984 ncopies = conf->prev.near_copies;
1985 } else {
1986 disks = conf->geo.raid_disks;
1987 ncopies = conf->geo.near_copies;
1988 }
NeilBrown700c7212011-07-27 11:00:36 +10001989
NeilBrown725d6e52013-06-11 15:08:03 +10001990 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001991 do {
1992 int n = conf->copies;
1993 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001994 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001995 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001996 struct md_rdev *rdev;
1997 if (this != ignore &&
1998 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1999 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10002000 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10002001 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10002002 }
2003 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10002004 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10002005 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10002006 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10002007 has_enough = 1;
2008out:
2009 rcu_read_unlock();
2010 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10002011}
2012
NeilBrownf8c9e742012-05-21 09:28:33 +10002013static int enough(struct r10conf *conf, int ignore)
2014{
NeilBrown635f6412013-06-11 14:57:09 +10002015 /* when calling 'enough', both 'prev' and 'geo' must
2016 * be stable.
2017 * This is ensured if ->reconfig_mutex or ->device_lock
2018 * is held.
2019 */
2020 return _enough(conf, 0, ignore) &&
2021 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10002022}
2023
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01002024/**
2025 * raid10_error() - RAID10 error handler.
2026 * @mddev: affected md device.
2027 * @rdev: member device to fail.
2028 *
2029 * The routine acknowledges &rdev failure and determines new @mddev state.
2030 * If it failed, then:
2031 * - &MD_BROKEN flag is set in &mddev->flags.
2032 * Otherwise, it must be degraded:
2033 * - recovery is interrupted.
2034 * - &mddev->degraded is bumped.
Guoqing Jiang62bca042022-08-22 15:45:39 +08002035 *
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01002036 * @rdev is marked as &Faulty excluding case when array is failed and
2037 * &mddev->fail_last_dev is off.
2038 */
Shaohua Li849674e2016-01-20 13:52:20 -08002039static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
NeilBrowne879a872011-10-11 16:49:02 +11002041 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10002042 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
NeilBrown635f6412013-06-11 14:57:09 +10002044 spin_lock_irqsave(&conf->device_lock, flags);
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01002045
2046 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) {
2047 set_bit(MD_BROKEN, &mddev->flags);
2048
2049 if (!mddev->fail_last_dev) {
2050 spin_unlock_irqrestore(&conf->device_lock, flags);
2051 return;
2052 }
NeilBrown635f6412013-06-11 14:57:09 +10002053 }
NeilBrown2446dba2014-07-31 10:16:29 +10002054 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10002055 mddev->degraded++;
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01002056
NeilBrown2446dba2014-07-31 10:16:29 +10002057 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10002058 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08002059 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002060 set_mask_bits(&mddev->sb_flags, 0,
2061 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10002062 spin_unlock_irqrestore(&conf->device_lock, flags);
Christoph Hellwig913cce52022-05-12 08:19:13 +02002063 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
NeilBrown08464e02016-11-02 14:16:50 +11002064 "md/raid10:%s: Operation continuing on %d devices.\n",
Christoph Hellwig913cce52022-05-12 08:19:13 +02002065 mdname(mddev), rdev->bdev,
NeilBrown08464e02016-11-02 14:16:50 +11002066 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
NeilBrowne879a872011-10-11 16:49:02 +11002069static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10002072 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
NeilBrown08464e02016-11-02 14:16:50 +11002074 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11002076 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return;
2078 }
NeilBrown08464e02016-11-02 14:16:50 +11002079 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
2080 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
NeilBrown4056ca52016-06-02 16:19:52 +10002082 /* This is only called with ->reconfix_mutex held, so
2083 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002084 for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown4056ca52016-06-02 16:19:52 +10002085 rdev = conf->mirrors[i].rdev;
2086 if (rdev)
Christoph Hellwig913cce52022-05-12 08:19:13 +02002087 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
NeilBrown08464e02016-11-02 14:16:50 +11002088 i, !test_bit(In_sync, &rdev->flags),
2089 !test_bit(Faulty, &rdev->flags),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002090 rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092}
2093
NeilBrowne879a872011-10-11 16:49:02 +11002094static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Vishal Vermac9aa889b02021-12-21 20:06:21 +00002096 wait_barrier(conf, false);
NeilBrown0a27ec92006-01-06 00:20:13 -08002097 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Kent Overstreetafeee512018-05-20 18:25:52 -04002099 mempool_exit(&conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
NeilBrownfd01b882011-10-11 16:47:53 +11002102static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int i;
NeilBrowne879a872011-10-11 16:49:02 +11002105 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002106 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10002107 int count = 0;
2108 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /*
2111 * Find all non-in_sync disks within the RAID10 configuration
2112 * and mark them in_sync
2113 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002114 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11002116 if (tmp->replacement
2117 && tmp->replacement->recovery_offset == MaxSector
2118 && !test_bit(Faulty, &tmp->replacement->flags)
2119 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2120 /* Replacement has just become active */
2121 if (!tmp->rdev
2122 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2123 count++;
2124 if (tmp->rdev) {
2125 /* Replaced device not technically faulty,
2126 * but we need to be sure it gets removed
2127 * and never re-added.
2128 */
2129 set_bit(Faulty, &tmp->rdev->flags);
2130 sysfs_notify_dirent_safe(
2131 tmp->rdev->sysfs_state);
2132 }
2133 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2134 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11002135 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11002136 && !test_bit(Faulty, &tmp->rdev->flags)
2137 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10002138 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002139 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
NeilBrown6b965622010-08-18 11:56:59 +10002142 spin_lock_irqsave(&conf->device_lock, flags);
2143 mddev->degraded -= count;
2144 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10002147 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
NeilBrownfd01b882011-10-11 16:47:53 +11002150static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
NeilBrowne879a872011-10-11 16:49:02 +11002152 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10002153 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10002155 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002156 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 if (mddev->recovery_cp < MaxSector)
2159 /* only hot-add to in-sync arrays, as recovery is
2160 * very different from resync
2161 */
Neil Brown199050e2008-06-28 08:31:33 +10002162 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10002163 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10002164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Dan Williams1501efa2016-01-13 16:00:07 -08002166 if (md_integrity_add_rdev(rdev, mddev))
2167 return -ENXIO;
2168
NeilBrowna53a6c82008-11-06 17:28:20 +11002169 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10002170 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Namhyung Kim2c4193d2011-07-18 17:38:43 +10002172 if (rdev->saved_raid_disk >= first &&
Shaohua Li9e753ba2018-10-14 17:05:07 -07002173 rdev->saved_raid_disk < conf->geo.raid_disks &&
NeilBrown6cce3b22006-01-06 00:20:16 -08002174 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2175 mirror = rdev->saved_raid_disk;
2176 else
Neil Brown6c2fce22008-06-28 08:31:31 +10002177 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10002178 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002179 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10002180 if (p->recovery_disabled == mddev->recovery_disabled)
2181 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11002182 if (p->rdev) {
2183 if (!test_bit(WantReplacement, &p->rdev->flags) ||
2184 p->replacement != NULL)
2185 continue;
2186 clear_bit(In_sync, &rdev->flags);
2187 set_bit(Replacement, &rdev->flags);
2188 rdev->raid_disk = mirror;
2189 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05002190 if (mddev->gendisk)
2191 disk_stack_limits(mddev->gendisk, rdev->bdev,
2192 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11002193 conf->fullsync = 1;
2194 rcu_assign_pointer(p->replacement, rdev);
2195 break;
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Jonathan Brassow9092c022013-05-02 14:19:24 -05002198 if (mddev->gendisk)
2199 disk_stack_limits(mddev->gendisk, rdev->bdev,
2200 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
NeilBrown2bb77732011-07-27 11:00:36 +10002202 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11002203 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10002204 rdev->raid_disk = mirror;
2205 err = 0;
2206 if (rdev->saved_raid_disk != mirror)
2207 conf->fullsync = 1;
2208 rcu_assign_pointer(p->rdev, rdev);
2209 break;
2210 }
Shaohua Li532a2a32012-10-11 13:30:52 +11002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10002213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
NeilBrownb8321b62011-12-23 10:17:51 +11002216static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
NeilBrowne879a872011-10-11 16:49:02 +11002218 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11002220 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11002221 struct md_rdev **rdevp;
Mikulas Patockad17f7442022-07-26 04:33:12 -04002222 struct raid10_info *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 print_conf(conf);
Mikulas Patockad17f7442022-07-26 04:33:12 -04002225 if (unlikely(number >= mddev->raid_disks))
2226 return 0;
2227 p = conf->mirrors + number;
NeilBrownc8ab9032011-12-23 10:17:54 +11002228 if (rdev == p->rdev)
2229 rdevp = &p->rdev;
2230 else if (rdev == p->replacement)
2231 rdevp = &p->replacement;
2232 else
2233 return 0;
2234
2235 if (test_bit(In_sync, &rdev->flags) ||
2236 atomic_read(&rdev->nr_pending)) {
2237 err = -EBUSY;
2238 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
NeilBrownd787be42016-06-02 16:19:53 +10002240 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11002241 * is not possible.
2242 */
2243 if (!test_bit(Faulty, &rdev->flags) &&
2244 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11002245 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10002246 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11002247 enough(conf, -1)) {
2248 err = -EBUSY;
2249 goto abort;
2250 }
2251 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10002252 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2253 synchronize_rcu();
2254 if (atomic_read(&rdev->nr_pending)) {
2255 /* lost the race, try later */
2256 err = -EBUSY;
2257 *rdevp = rdev;
2258 goto abort;
2259 }
2260 }
2261 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11002262 /* We must have just cleared 'rdev' */
2263 p->rdev = p->replacement;
2264 clear_bit(Replacement, &p->replacement->flags);
2265 smp_mb(); /* Make sure other CPUs may see both as identical
2266 * but will never see neither -- if they are careful.
2267 */
2268 p->replacement = NULL;
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08002269 }
NeilBrown4ca40c22011-12-23 10:17:55 +11002270
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08002271 clear_bit(WantReplacement, &rdev->flags);
NeilBrownc8ab9032011-12-23 10:17:54 +11002272 err = md_integrity_register(mddev);
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274abort:
2275
2276 print_conf(conf);
2277 return err;
2278}
2279
Ming Lei81fa1522017-03-17 00:12:32 +08002280static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
NeilBrowne879a872011-10-11 16:49:02 +11002282 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002283
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002284 if (!bio->bi_status)
NeilBrown0eb3ff12006-01-06 00:20:29 -08002285 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10002286 else
2287 /* The write handler will notice the lack of
2288 * R10BIO_Uptodate and record any errors etc
2289 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08002290 atomic_add(r10_bio->sectors,
2291 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 /* for reconstruct, we always reschedule after a read.
2294 * for resync, only after all reads
2295 */
NeilBrown73d5c382009-02-25 13:18:47 +11002296 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2298 atomic_dec_and_test(&r10_bio->remaining)) {
2299 /* we have read all the blocks,
2300 * do the comparison in process context in raid10d
2301 */
2302 reschedule_retry(r10_bio);
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Ming Lei81fa1522017-03-17 00:12:32 +08002306static void end_sync_read(struct bio *bio)
2307{
Ming Leif0250612017-03-17 00:12:33 +08002308 struct r10bio *r10_bio = get_resync_r10bio(bio);
Ming Lei81fa1522017-03-17 00:12:32 +08002309 struct r10conf *conf = r10_bio->mddev->private;
2310 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2311
2312 __end_sync_read(r10_bio, bio, d);
2313}
2314
2315static void end_reshape_read(struct bio *bio)
2316{
Ming Leif0250612017-03-17 00:12:33 +08002317 /* reshape read bio isn't allocated from r10buf_pool */
Ming Lei81fa1522017-03-17 00:12:32 +08002318 struct r10bio *r10_bio = bio->bi_private;
2319
2320 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2321}
2322
NeilBrown9f2c9d12011-10-11 16:48:43 +11002323static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002324{
NeilBrownfd01b882011-10-11 16:47:53 +11002325 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10002326
2327 while (atomic_dec_and_test(&r10_bio->remaining)) {
2328 if (r10_bio->master_bio == NULL) {
2329 /* the primary of several recovery bios */
2330 sector_t s = r10_bio->sectors;
2331 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2332 test_bit(R10BIO_WriteError, &r10_bio->state))
2333 reschedule_retry(r10_bio);
2334 else
2335 put_buf(r10_bio);
2336 md_done_sync(mddev, s, 1);
2337 break;
2338 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002339 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10002340 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2341 test_bit(R10BIO_WriteError, &r10_bio->state))
2342 reschedule_retry(r10_bio);
2343 else
2344 put_buf(r10_bio);
2345 r10_bio = r10_bio2;
2346 }
2347 }
2348}
2349
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002350static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Ming Leif0250612017-03-17 00:12:33 +08002352 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrownfd01b882011-10-11 16:47:53 +11002353 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002354 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10002355 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10002356 sector_t first_bad;
2357 int bad_sectors;
2358 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002359 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11002360 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
NeilBrown9ad1aef2011-12-23 10:17:55 +11002362 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2363 if (repl)
2364 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11002365 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11002366 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002368 if (bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002369 if (repl)
2370 md_error(mddev, rdev);
2371 else {
2372 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002373 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2374 set_bit(MD_RECOVERY_NEEDED,
2375 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002376 set_bit(R10BIO_WriteError, &r10_bio->state);
2377 }
2378 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10002379 r10_bio->devs[slot].addr,
2380 r10_bio->sectors,
2381 &first_bad, &bad_sectors))
2382 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07002383
NeilBrown9ad1aef2011-12-23 10:17:55 +11002384 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002385
2386 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
2389/*
2390 * Note: sync and recover and handled very differently for raid10
2391 * This code is for resync.
2392 * For resync, we read through virtual addresses and read all blocks.
2393 * If there is any error, we schedule a write. The lowest numbered
2394 * drive is authoritative.
2395 * However requests come for physical address, so we need to map.
2396 * For every physical address there are raid_disks/copies virtual addresses,
2397 * which is always are least one, but is not necessarly an integer.
2398 * This means that a physical address can span multiple chunks, so we may
2399 * have to submit multiple io requests for a single sync request.
2400 */
2401/*
2402 * We check if all blocks are in-sync and only write to blocks that
2403 * aren't in sync
2404 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002405static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
NeilBrowne879a872011-10-11 16:49:02 +11002407 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int i, first;
2409 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002410 int vcnt;
Ming Leicdb76be2017-03-17 00:12:34 +08002411 struct page **tpages, **fpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 atomic_set(&r10_bio->remaining, 1);
2414
2415 /* find the first device with a block */
2416 for (i=0; i<conf->copies; i++)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002417 if (!r10_bio->devs[i].bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 break;
2419
2420 if (i == conf->copies)
2421 goto done;
2422
2423 first = i;
2424 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002425 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2426 fbio->bi_iter.bi_idx = 0;
Ming Leicdb76be2017-03-17 00:12:34 +08002427 fpages = get_resync_pages(fbio)->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
majianpengf4380a92012-04-12 16:04:47 +10002429 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002431 for (i=0 ; i < conf->copies ; i++) {
2432 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002433 struct md_rdev *rdev;
Ming Leif0250612017-03-17 00:12:33 +08002434 struct resync_pages *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002437
2438 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002440 if (i == first)
2441 continue;
Ming Leicdb76be2017-03-17 00:12:34 +08002442
2443 tpages = get_resync_pages(tbio)->pages;
NeilBrown8d3ca832016-11-18 16:16:12 +11002444 d = r10_bio->devs[i].devnum;
2445 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002446 if (!r10_bio->devs[i].bio->bi_status) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002447 /* We know that the bi_io_vec layout is the same for
2448 * both 'first' and 'i', so we just compare them.
2449 * All vec entries are PAGE_SIZE;
2450 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002451 int sectors = r10_bio->sectors;
2452 for (j = 0; j < vcnt; j++) {
2453 int len = PAGE_SIZE;
2454 if (sectors < (len / 512))
2455 len = sectors * 512;
Ming Leicdb76be2017-03-17 00:12:34 +08002456 if (memcmp(page_address(fpages[j]),
2457 page_address(tpages[j]),
NeilBrown7bb23c42013-07-16 16:50:47 +10002458 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002459 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002460 sectors -= len/512;
2461 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002462 if (j == vcnt)
2463 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002464 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002465 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2466 /* Don't fix anything. */
2467 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002468 } else if (test_bit(FailFast, &rdev->flags)) {
2469 /* Just give up on this device */
2470 md_error(rdev->mddev, rdev);
2471 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002472 }
NeilBrownf84ee362011-07-28 11:39:25 +10002473 /* Ok, we need to write this bio, either to correct an
2474 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 * First we need to fixup bv_offset, bv_len and
2476 * bi_vecs, as the read request might have corrupted these
2477 */
Ming Leif0250612017-03-17 00:12:33 +08002478 rp = get_resync_pages(tbio);
Christoph Hellwiga7c50c92022-01-24 10:11:07 +01002479 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE);
Kent Overstreet8be185f2012-09-06 14:14:43 -07002480
Ming Leifb0eb5d2017-07-14 16:14:43 +08002481 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2482
Ming Leif0250612017-03-17 00:12:33 +08002483 rp->raid_bio = r10_bio;
2484 tbio->bi_private = rp;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002485 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 tbio->bi_end_io = end_sync_write;
2487
Kent Overstreetc31df252015-05-06 23:34:20 -07002488 bio_copy_data(tbio, fbio);
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2491 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002492 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
NeilBrown1919cbb2016-11-18 16:16:12 +11002494 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2495 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002496 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002497 submit_bio_noacct(tbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499
NeilBrown9ad1aef2011-12-23 10:17:55 +11002500 /* Now write out to any replacement devices
2501 * that are active
2502 */
2503 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002504 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002505
2506 tbio = r10_bio->devs[i].repl_bio;
2507 if (!tbio || !tbio->bi_end_io)
2508 continue;
2509 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2510 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002511 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002512 d = r10_bio->devs[i].devnum;
2513 atomic_inc(&r10_bio->remaining);
2514 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002515 bio_sectors(tbio));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002516 submit_bio_noacct(tbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002517 }
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519done:
2520 if (atomic_dec_and_test(&r10_bio->remaining)) {
2521 md_done_sync(mddev, r10_bio->sectors, 1);
2522 put_buf(r10_bio);
2523 }
2524}
2525
2526/*
2527 * Now for the recovery code.
2528 * Recovery happens across physical sectors.
2529 * We recover all non-is_sync drives by finding the virtual address of
2530 * each, and then choose a working drive that also has that virt address.
2531 * There is a separate r10_bio for each non-in_sync drive.
2532 * Only the first two slots are in use. The first for reading,
2533 * The second for writing.
2534 *
2535 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002536static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002537{
2538 /* We got a read error during recovery.
2539 * We repeat the read in smaller page-sized sections.
2540 * If a read succeeds, write it to the new device or record
2541 * a bad block if we cannot.
2542 * If a read fails, record a bad block on both old and
2543 * new devices.
2544 */
NeilBrownfd01b882011-10-11 16:47:53 +11002545 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002546 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002547 struct bio *bio = r10_bio->devs[0].bio;
2548 sector_t sect = 0;
2549 int sectors = r10_bio->sectors;
2550 int idx = 0;
2551 int dr = r10_bio->devs[0].devnum;
2552 int dw = r10_bio->devs[1].devnum;
Ming Leicdb76be2017-03-17 00:12:34 +08002553 struct page **pages = get_resync_pages(bio)->pages;
NeilBrown5e570282011-07-28 11:39:25 +10002554
2555 while (sectors) {
2556 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002557 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002558 sector_t addr;
2559 int ok;
2560
2561 if (s > (PAGE_SIZE>>9))
2562 s = PAGE_SIZE >> 9;
2563
2564 rdev = conf->mirrors[dr].rdev;
2565 addr = r10_bio->devs[0].addr + sect,
2566 ok = sync_page_io(rdev,
2567 addr,
2568 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002569 pages[idx],
Bart Van Assche4ce4c732022-07-14 11:06:57 -07002570 REQ_OP_READ, false);
NeilBrown5e570282011-07-28 11:39:25 +10002571 if (ok) {
2572 rdev = conf->mirrors[dw].rdev;
2573 addr = r10_bio->devs[1].addr + sect;
2574 ok = sync_page_io(rdev,
2575 addr,
2576 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002577 pages[idx],
Bart Van Assche4ce4c732022-07-14 11:06:57 -07002578 REQ_OP_WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002579 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002580 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002581 if (!test_and_set_bit(WantReplacement,
2582 &rdev->flags))
2583 set_bit(MD_RECOVERY_NEEDED,
2584 &rdev->mddev->recovery);
2585 }
NeilBrown5e570282011-07-28 11:39:25 +10002586 }
2587 if (!ok) {
2588 /* We don't worry if we cannot set a bad block -
2589 * it really is bad so there is no loss in not
2590 * recording it yet
2591 */
2592 rdev_set_badblocks(rdev, addr, s, 0);
2593
2594 if (rdev != conf->mirrors[dw].rdev) {
2595 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002596 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002597 addr = r10_bio->devs[1].addr + sect;
2598 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2599 if (!ok) {
2600 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002601 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2602 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002603
2604 conf->mirrors[dw].recovery_disabled
2605 = mddev->recovery_disabled;
2606 set_bit(MD_RECOVERY_INTR,
2607 &mddev->recovery);
2608 break;
2609 }
2610 }
2611 }
2612
2613 sectors -= s;
2614 sect += s;
2615 idx++;
2616 }
2617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
NeilBrown9f2c9d12011-10-11 16:48:43 +11002619static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
NeilBrowne879a872011-10-11 16:49:02 +11002621 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002622 int d;
Yu Kuai26208a72023-03-10 15:38:53 +08002623 struct bio *wbio = r10_bio->devs[1].bio;
2624 struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2625
2626 /* Need to test wbio2->bi_end_io before we call
2627 * submit_bio_noacct as if the former is NULL,
2628 * the latter is free to free wbio2.
2629 */
2630 if (wbio2 && !wbio2->bi_end_io)
2631 wbio2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
NeilBrown5e570282011-07-28 11:39:25 +10002633 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2634 fix_recovery_read_error(r10_bio);
Yu Kuai26208a72023-03-10 15:38:53 +08002635 if (wbio->bi_end_io)
2636 end_sync_request(r10_bio);
2637 if (wbio2)
2638 end_sync_request(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10002639 return;
2640 }
2641
Namhyung Kimc65060a2011-07-18 17:38:49 +10002642 /*
2643 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * and submit the write request
2645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002647 if (wbio->bi_end_io) {
2648 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002649 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002650 submit_bio_noacct(wbio);
NeilBrown24afd802011-12-23 10:17:55 +11002651 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002652 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002653 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2654 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002655 bio_sectors(wbio2));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002656 submit_bio_noacct(wbio2);
NeilBrown24afd802011-12-23 10:17:55 +11002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660/*
Robert Becker1e509152009-12-14 12:49:58 +11002661 * Used by fix_read_error() to decay the per rdev read_errors.
2662 * We halve the read error count for every hour that has elapsed
2663 * since the last recorded read error.
2664 *
2665 */
NeilBrownfd01b882011-10-11 16:47:53 +11002666static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002667{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002668 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002669 unsigned long hours_since_last;
2670 unsigned int read_errors = atomic_read(&rdev->read_errors);
2671
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002672 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002673
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002674 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002675 /* first time we've seen a read error */
2676 rdev->last_read_error = cur_time_mon;
2677 return;
2678 }
2679
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002680 hours_since_last = (long)(cur_time_mon -
2681 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002682
2683 rdev->last_read_error = cur_time_mon;
2684
2685 /*
2686 * if hours_since_last is > the number of bits in read_errors
2687 * just set read errors to 0. We do this to avoid
2688 * overflowing the shift of read_errors by hours_since_last.
2689 */
2690 if (hours_since_last >= 8 * sizeof(read_errors))
2691 atomic_set(&rdev->read_errors, 0);
2692 else
2693 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2694}
2695
NeilBrown3cb03002011-10-11 16:45:26 +11002696static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
Bart Van Assche265ad472022-08-10 11:20:12 -07002697 int sectors, struct page *page, enum req_op op)
NeilBrown58c54fc2011-07-28 11:39:25 +10002698{
2699 sector_t first_bad;
2700 int bad_sectors;
2701
2702 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
Bart Van Assche265ad472022-08-10 11:20:12 -07002703 && (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags)))
NeilBrown58c54fc2011-07-28 11:39:25 +10002704 return -1;
Bart Van Assche265ad472022-08-10 11:20:12 -07002705 if (sync_page_io(rdev, sector, sectors << 9, page, op, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002706 /* success */
2707 return 1;
Bart Van Assche265ad472022-08-10 11:20:12 -07002708 if (op == REQ_OP_WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002709 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002710 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2711 set_bit(MD_RECOVERY_NEEDED,
2712 &rdev->mddev->recovery);
2713 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002714 /* need to record an error - either for the block or the device */
2715 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2716 md_error(rdev->mddev, rdev);
2717 return 0;
2718}
2719
Robert Becker1e509152009-12-14 12:49:58 +11002720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * This is a kernel thread which:
2722 *
2723 * 1. Retries failed read operations on working mirrors.
2724 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002725 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 */
2727
NeilBrowne879a872011-10-11 16:49:02 +11002728static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002729{
2730 int sect = 0; /* Offset from r10_bio->sector */
2731 int sectors = r10_bio->sectors;
Yufen Yu13db16d2018-04-23 17:37:30 +08002732 struct md_rdev *rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002733 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002734 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002735
NeilBrown7c4e06f2011-05-11 14:53:17 +10002736 /* still own a reference to this rdev, so it cannot
2737 * have been cleared recently.
2738 */
2739 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002740
NeilBrown7c4e06f2011-05-11 14:53:17 +10002741 if (test_bit(Faulty, &rdev->flags))
2742 /* drive has already been failed, just ignore any
2743 more fix_read_error() attempts */
2744 return;
2745
2746 check_decay_read_errors(mddev, rdev);
2747 atomic_inc(&rdev->read_errors);
2748 if (atomic_read(&rdev->read_errors) > max_read_errors) {
Christoph Hellwig913cce52022-05-12 08:19:13 +02002749 pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2750 mdname(mddev), rdev->bdev,
NeilBrown08464e02016-11-02 14:16:50 +11002751 atomic_read(&rdev->read_errors), max_read_errors);
Christoph Hellwig913cce52022-05-12 08:19:13 +02002752 pr_notice("md/raid10:%s: %pg: Failing raid device\n",
2753 mdname(mddev), rdev->bdev);
NeilBrownd683c8e2016-06-02 16:19:52 +10002754 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002755 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002756 return;
Robert Becker1e509152009-12-14 12:49:58 +11002757 }
Robert Becker1e509152009-12-14 12:49:58 +11002758
NeilBrown6814d532006-10-03 01:15:45 -07002759 while(sectors) {
2760 int s = sectors;
2761 int sl = r10_bio->read_slot;
2762 int success = 0;
2763 int start;
2764
2765 if (s > (PAGE_SIZE>>9))
2766 s = PAGE_SIZE >> 9;
2767
2768 rcu_read_lock();
2769 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002770 sector_t first_bad;
2771 int bad_sectors;
2772
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002773 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002774 rdev = rcu_dereference(conf->mirrors[d].rdev);
2775 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002776 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002777 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002778 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2779 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002780 atomic_inc(&rdev->nr_pending);
2781 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002782 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002783 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002784 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002785 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002786 conf->tmppage,
Bart Van Assche4ce4c732022-07-14 11:06:57 -07002787 REQ_OP_READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002788 rdev_dec_pending(rdev, mddev);
2789 rcu_read_lock();
2790 if (success)
2791 break;
2792 }
2793 sl++;
2794 if (sl == conf->copies)
2795 sl = 0;
2796 } while (!success && sl != r10_bio->read_slot);
2797 rcu_read_unlock();
2798
2799 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002800 /* Cannot read from anywhere, just mark the block
2801 * as bad on the first device to discourage future
2802 * reads.
2803 */
NeilBrown6814d532006-10-03 01:15:45 -07002804 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002805 rdev = conf->mirrors[dn].rdev;
2806
2807 if (!rdev_set_badblocks(
2808 rdev,
2809 r10_bio->devs[r10_bio->read_slot].addr
2810 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002811 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002812 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002813 r10_bio->devs[r10_bio->read_slot].bio
2814 = IO_BLOCKED;
2815 }
NeilBrown6814d532006-10-03 01:15:45 -07002816 break;
2817 }
2818
2819 start = sl;
2820 /* write it back and re-read */
2821 rcu_read_lock();
2822 while (sl != r10_bio->read_slot) {
NeilBrown6814d532006-10-03 01:15:45 -07002823 if (sl==0)
2824 sl = conf->copies;
2825 sl--;
2826 d = r10_bio->devs[sl].devnum;
2827 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002828 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002829 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002830 !test_bit(In_sync, &rdev->flags))
2831 continue;
2832
2833 atomic_inc(&rdev->nr_pending);
2834 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002835 if (r10_sync_page_io(rdev,
2836 r10_bio->devs[sl].addr +
2837 sect,
Bart Van Assche265ad472022-08-10 11:20:12 -07002838 s, conf->tmppage, REQ_OP_WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002839 == 0) {
2840 /* Well, this device is dead */
Christoph Hellwig913cce52022-05-12 08:19:13 +02002841 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
NeilBrown08464e02016-11-02 14:16:50 +11002842 mdname(mddev), s,
2843 (unsigned long long)(
2844 sect +
2845 choose_data_offset(r10_bio,
2846 rdev)),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002847 rdev->bdev);
2848 pr_notice("md/raid10:%s: %pg: failing drive\n",
NeilBrown08464e02016-11-02 14:16:50 +11002849 mdname(mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002850 rdev->bdev);
NeilBrown6814d532006-10-03 01:15:45 -07002851 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002852 rdev_dec_pending(rdev, mddev);
2853 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002854 }
2855 sl = start;
2856 while (sl != r10_bio->read_slot) {
NeilBrown6814d532006-10-03 01:15:45 -07002857 if (sl==0)
2858 sl = conf->copies;
2859 sl--;
2860 d = r10_bio->devs[sl].devnum;
2861 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002862 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002863 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002864 !test_bit(In_sync, &rdev->flags))
2865 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002866
NeilBrown1294b9c2011-07-28 11:39:23 +10002867 atomic_inc(&rdev->nr_pending);
2868 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002869 switch (r10_sync_page_io(rdev,
2870 r10_bio->devs[sl].addr +
2871 sect,
Bart Van Assche265ad472022-08-10 11:20:12 -07002872 s, conf->tmppage, REQ_OP_READ)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002873 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002874 /* Well, this device is dead */
Christoph Hellwig913cce52022-05-12 08:19:13 +02002875 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002876 mdname(mddev), s,
2877 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002878 sect +
2879 choose_data_offset(r10_bio, rdev)),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002880 rdev->bdev);
2881 pr_notice("md/raid10:%s: %pg: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002882 mdname(mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002883 rdev->bdev);
NeilBrown58c54fc2011-07-28 11:39:25 +10002884 break;
2885 case 1:
Christoph Hellwig913cce52022-05-12 08:19:13 +02002886 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002887 mdname(mddev), s,
2888 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002889 sect +
2890 choose_data_offset(r10_bio, rdev)),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002891 rdev->bdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002892 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002893 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002894
2895 rdev_dec_pending(rdev, mddev);
2896 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002897 }
2898 rcu_read_unlock();
2899
2900 sectors -= s;
2901 sect += s;
2902 }
2903}
2904
NeilBrown9f2c9d12011-10-11 16:48:43 +11002905static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002906{
2907 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002908 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002909 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002910 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002911 /* bio has the data to be written to slot 'i' where
2912 * we just recently had a write error.
2913 * We repeatedly clone the bio and trim down to one block,
2914 * then try the write. Where the write fails we record
2915 * a bad block.
2916 * It is conceivable that the bio doesn't exactly align with
2917 * blocks. We must handle this.
2918 *
2919 * We currently own a reference to the rdev.
2920 */
2921
2922 int block_sectors;
2923 sector_t sector;
2924 int sectors;
2925 int sect_to_write = r10_bio->sectors;
2926 int ok = 1;
2927
2928 if (rdev->badblocks.shift < 0)
2929 return 0;
2930
NeilBrownf04ebb02015-02-16 14:51:54 +11002931 block_sectors = roundup(1 << rdev->badblocks.shift,
2932 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002933 sector = r10_bio->sector;
2934 sectors = ((r10_bio->sector + block_sectors)
2935 & ~(sector_t)(block_sectors - 1))
2936 - sector;
2937
2938 while (sect_to_write) {
2939 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002940 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002941 if (sectors > sect_to_write)
2942 sectors = sect_to_write;
2943 /* Write at 'sector' for 'sectors' */
Christoph Hellwigabfc4262022-02-02 17:01:09 +01002944 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
2945 &mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002946 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002947 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2948 wbio->bi_iter.bi_sector = wsector +
2949 choose_data_offset(r10_bio, rdev);
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01002950 wbio->bi_opf = REQ_OP_WRITE;
Mike Christie4e49ea42016-06-05 14:31:41 -05002951
2952 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002953 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002954 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002955 sectors, 0)
2956 && ok;
2957
2958 bio_put(wbio);
2959 sect_to_write -= sectors;
2960 sector += sectors;
2961 sectors = block_sectors;
2962 }
2963 return ok;
2964}
2965
NeilBrown9f2c9d12011-10-11 16:48:43 +11002966static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002967{
2968 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002969 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002970 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002971 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002972
2973 /* we got a read error. Maybe the drive is bad. Maybe just
2974 * the block and we can fix it.
2975 * We freeze all other IO, and try reading the block from
2976 * other devices. When we find one, we re-write
2977 * and check it that fixes the read error.
2978 * This is all done synchronously while the array is
2979 * frozen.
2980 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002981 bio = r10_bio->devs[slot].bio;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002982 bio_put(bio);
2983 r10_bio->devs[slot].bio = NULL;
2984
NeilBrown8d3ca832016-11-18 16:16:12 +11002985 if (mddev->ro)
2986 r10_bio->devs[slot].bio = IO_BLOCKED;
2987 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002988 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002989 fix_read_error(conf, mddev, r10_bio);
2990 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002991 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002992 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002993
NeilBrownabbf0982011-12-23 10:17:54 +11002994 rdev_dec_pending(rdev, mddev);
NeilBrown545250f2017-04-05 14:05:51 +10002995 r10_bio->state = 0;
2996 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
Li Nan72c215e2023-02-22 12:09:59 +08002997 /*
2998 * allow_barrier after re-submit to ensure no sync io
2999 * can be issued while regular io pending.
3000 */
3001 allow_barrier(conf);
NeilBrown560f8e52011-07-28 11:39:23 +10003002}
3003
NeilBrowne879a872011-10-11 16:49:02 +11003004static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10003005{
3006 /* Some sort of write request has finished and it
3007 * succeeded in writing where we thought there was a
3008 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10003009 * Or possibly if failed and we need to record
3010 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10003011 */
3012 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11003013 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10003014
3015 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
3016 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10003017 for (m = 0; m < conf->copies; m++) {
3018 int dev = r10_bio->devs[m].devnum;
3019 rdev = conf->mirrors[dev].rdev;
Yufen Yu01a69ca2018-02-06 17:39:15 +08003020 if (r10_bio->devs[m].bio == NULL ||
3021 r10_bio->devs[m].bio->bi_end_io == NULL)
NeilBrown1a0b7cd2011-07-28 11:39:25 +10003022 continue;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003023 if (!r10_bio->devs[m].bio->bi_status) {
NeilBrown749c55e2011-07-28 11:39:24 +10003024 rdev_clear_badblocks(
3025 rdev,
3026 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10003027 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10003028 } else {
3029 if (!rdev_set_badblocks(
3030 rdev,
3031 r10_bio->devs[m].addr,
3032 r10_bio->sectors, 0))
3033 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10003034 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11003035 rdev = conf->mirrors[dev].replacement;
Yufen Yu01a69ca2018-02-06 17:39:15 +08003036 if (r10_bio->devs[m].repl_bio == NULL ||
3037 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
NeilBrown9ad1aef2011-12-23 10:17:55 +11003038 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003039
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003040 if (!r10_bio->devs[m].repl_bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11003041 rdev_clear_badblocks(
3042 rdev,
3043 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10003044 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003045 } else {
3046 if (!rdev_set_badblocks(
3047 rdev,
3048 r10_bio->devs[m].addr,
3049 r10_bio->sectors, 0))
3050 md_error(conf->mddev, rdev);
3051 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10003052 }
NeilBrown749c55e2011-07-28 11:39:24 +10003053 put_buf(r10_bio);
3054 } else {
NeilBrown95af5872015-08-14 11:26:17 +10003055 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10003056 for (m = 0; m < conf->copies; m++) {
3057 int dev = r10_bio->devs[m].devnum;
3058 struct bio *bio = r10_bio->devs[m].bio;
3059 rdev = conf->mirrors[dev].rdev;
3060 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10003061 rdev_clear_badblocks(
3062 rdev,
3063 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10003064 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10003065 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003066 } else if (bio != NULL && bio->bi_status) {
NeilBrown95af5872015-08-14 11:26:17 +10003067 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10003068 if (!narrow_write_error(r10_bio, m)) {
3069 md_error(conf->mddev, rdev);
3070 set_bit(R10BIO_Degraded,
3071 &r10_bio->state);
3072 }
3073 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10003074 }
NeilBrown475b0322011-12-23 10:17:55 +11003075 bio = r10_bio->devs[m].repl_bio;
3076 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11003077 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11003078 rdev_clear_badblocks(
3079 rdev,
3080 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10003081 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11003082 rdev_dec_pending(rdev, conf->mddev);
3083 }
NeilBrownbd870a12011-07-28 11:39:24 +10003084 }
NeilBrown95af5872015-08-14 11:26:17 +10003085 if (fail) {
3086 spin_lock_irq(&conf->device_lock);
3087 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07003088 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10003089 spin_unlock_irq(&conf->device_lock);
Guoqing Jiangcf25ae72017-04-17 17:11:05 +08003090 /*
3091 * In case freeze_array() is waiting for condition
3092 * nr_pending == nr_queued + extra to be true.
3093 */
3094 wake_up(&conf->wait_barrier);
NeilBrown95af5872015-08-14 11:26:17 +10003095 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11003096 } else {
3097 if (test_bit(R10BIO_WriteError,
3098 &r10_bio->state))
3099 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10003100 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11003101 }
NeilBrown749c55e2011-07-28 11:39:24 +10003102 }
3103}
3104
Shaohua Li4ed87312012-10-11 13:34:00 +11003105static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Shaohua Li4ed87312012-10-11 13:34:00 +11003107 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003108 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11003110 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003112 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
NeilBrown95af5872015-08-14 11:26:17 +10003116 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08003117 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10003118 LIST_HEAD(tmp);
3119 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08003120 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07003121 while (!list_empty(&conf->bio_end_io_list)) {
3122 list_move(conf->bio_end_io_list.prev, &tmp);
3123 conf->nr_queued--;
3124 }
NeilBrown95af5872015-08-14 11:26:17 +10003125 }
3126 spin_unlock_irqrestore(&conf->device_lock, flags);
3127 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04003128 r10_bio = list_first_entry(&tmp, struct r10bio,
3129 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003130 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11003131 if (mddev->degraded)
3132 set_bit(R10BIO_Degraded, &r10_bio->state);
3133
3134 if (test_bit(R10BIO_WriteError,
3135 &r10_bio->state))
3136 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10003137 raid_end_bio_io(r10_bio);
3138 }
3139 }
3140
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003141 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08003143
NeilBrown0021b7b2012-07-31 09:08:14 +02003144 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08003147 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003148 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08003150 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11003151 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08003153 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 spin_unlock_irqrestore(&conf->device_lock, flags);
3155
3156 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10003157 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10003158 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3159 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10003160 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003161 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3162 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10003163 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01003165 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10003167 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10003168 handle_read_error(mddev, r10_bio);
NeilBrownfc9977d2017-04-05 14:05:51 +10003169 else
3170 WARN_ON_ONCE(1);
NeilBrown4443ae12006-01-06 00:20:28 -08003171
NeilBrown1d9d5242009-10-16 15:55:32 +11003172 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08003173 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10003174 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003176 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
NeilBrowne879a872011-10-11 16:49:02 +11003179static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Kent Overstreetafeee512018-05-20 18:25:52 -04003181 int ret, buffs, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Kent Overstreetafeee512018-05-20 18:25:52 -04003184 BUG_ON(mempool_initialized(&conf->r10buf_pool));
NeilBrown69335ef2011-12-23 10:17:54 +11003185 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003186 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11003187 if (conf->mirrors[i].replacement)
3188 conf->have_replacement = 1;
Kent Overstreetafeee512018-05-20 18:25:52 -04003189 ret = mempool_init(&conf->r10buf_pool, buffs,
3190 r10buf_pool_alloc, r10buf_pool_free, conf);
3191 if (ret)
3192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 conf->next_resync = 0;
3194 return 0;
3195}
3196
Shaohua Li208410b2017-08-24 17:50:40 -07003197static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3198{
Kent Overstreetafeee512018-05-20 18:25:52 -04003199 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
Shaohua Li208410b2017-08-24 17:50:40 -07003200 struct rsync_pages *rp;
3201 struct bio *bio;
3202 int nalloc;
3203 int i;
3204
3205 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3206 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3207 nalloc = conf->copies; /* resync */
3208 else
3209 nalloc = 2; /* recovery */
3210
3211 for (i = 0; i < nalloc; i++) {
3212 bio = r10bio->devs[i].bio;
3213 rp = bio->bi_private;
Christoph Hellwiga7c50c92022-01-24 10:11:07 +01003214 bio_reset(bio, NULL, 0);
Shaohua Li208410b2017-08-24 17:50:40 -07003215 bio->bi_private = rp;
3216 bio = r10bio->devs[i].repl_bio;
3217 if (bio) {
3218 rp = bio->bi_private;
Christoph Hellwiga7c50c92022-01-24 10:11:07 +01003219 bio_reset(bio, NULL, 0);
Shaohua Li208410b2017-08-24 17:50:40 -07003220 bio->bi_private = rp;
3221 }
3222 }
3223 return r10bio;
3224}
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/*
Guoqing Jiang8db87912017-10-24 15:11:52 +08003227 * Set cluster_sync_high since we need other nodes to add the
3228 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3229 */
3230static void raid10_set_cluster_sync_high(struct r10conf *conf)
3231{
3232 sector_t window_size;
3233 int extra_chunk, chunks;
3234
3235 /*
3236 * First, here we define "stripe" as a unit which across
3237 * all member devices one time, so we get chunks by use
3238 * raid_disks / near_copies. Otherwise, if near_copies is
3239 * close to raid_disks, then resync window could increases
3240 * linearly with the increase of raid_disks, which means
3241 * we will suspend a really large IO window while it is not
3242 * necessary. If raid_disks is not divisible by near_copies,
3243 * an extra chunk is needed to ensure the whole "stripe" is
3244 * covered.
3245 */
3246
3247 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3248 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3249 extra_chunk = 0;
3250 else
3251 extra_chunk = 1;
3252 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3253
3254 /*
3255 * At least use a 32M window to align with raid1's resync window
3256 */
3257 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3258 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3259
3260 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3261}
3262
3263/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 * perform a "sync" on one "block"
3265 *
3266 * We need to make sure that no normal I/O request - particularly write
3267 * requests - conflict with active sync requests.
3268 *
3269 * This is achieved by tracking pending requests and a 'barrier' concept
3270 * that can be installed to exclude normal IO requests.
3271 *
3272 * Resync and recovery are handled very differently.
3273 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3274 *
3275 * For resync, we iterate over virtual addresses, read all copies,
3276 * and update if there are differences. If only one copy is live,
3277 * skip it.
3278 * For recovery, we iterate over physical addresses, read a good
3279 * value for each non-in_sync drive, and over-write.
3280 *
3281 * So, for recovery we may have several outstanding complex requests for a
3282 * given address, one for each out-of-sync device. We model this by allocating
3283 * a number of r10_bio structures, one for each out-of-sync device.
3284 * As we setup these structures, we collect all bio's together into a list
3285 * which we then process collectively to add pages, and then process again
Christoph Hellwiged00aab2020-07-01 10:59:44 +02003286 * to pass to submit_bio_noacct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 *
3288 * The r10_bio structures are linked using a borrowed master_bio pointer.
3289 * This link is counted in ->remaining. When the r10_bio that points to NULL
3290 * has its remaining count decremented to 0, the whole complex operation
3291 * is complete.
3292 *
3293 */
3294
Shaohua Li849674e2016-01-20 13:52:20 -08003295static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11003296 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
NeilBrowne879a872011-10-11 16:49:02 +11003298 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003299 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct bio *biolist = NULL, *bio;
3301 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08003303 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11003304 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 sector_t sectors_skipped = 0;
3306 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003307 sector_t chunk_mask = conf->geo.chunk_mask;
Ming Lei022e5102017-07-14 16:14:42 +08003308 int page_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003310 /*
3311 * Allow skipping a full rebuild for incremental assembly
3312 * of a clean array, like RAID1 does.
3313 */
3314 if (mddev->bitmap == NULL &&
3315 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10003316 mddev->reshape_position == MaxSector &&
3317 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003318 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10003319 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003320 conf->fullsync == 0) {
3321 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10003322 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003323 }
3324
Li Nana405c6f2023-02-22 12:10:00 +08003325 if (!mempool_initialized(&conf->r10buf_pool))
3326 if (init_resync(conf))
3327 return 0;
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11003330 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003331 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3332 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 max_sector = mddev->resync_max_sectors;
3334 if (sector_nr >= max_sector) {
Guoqing Jiang8db87912017-10-24 15:11:52 +08003335 conf->cluster_sync_low = 0;
3336 conf->cluster_sync_high = 0;
3337
NeilBrown6cce3b22006-01-06 00:20:16 -08003338 /* If we aborted, we need to abort the
3339 * sync on the 'current' bitmap chucks (there can
3340 * be several when recovering multiple devices).
3341 * as we may have started syncing it but not finished.
3342 * We can find the current address in
3343 * mddev->curr_resync, but for recovery,
3344 * we need to convert that to several
3345 * virtual addresses.
3346 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10003347 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3348 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10003349 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003350 return 0;
3351 }
3352
NeilBrown6cce3b22006-01-06 00:20:16 -08003353 if (mddev->curr_resync < max_sector) { /* aborted */
3354 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003355 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3356 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003357 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003358 sector_t sect =
3359 raid10_find_virt(conf, mddev->curr_resync, i);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003360 md_bitmap_end_sync(mddev->bitmap, sect,
3361 &sync_blocks, 1);
NeilBrown6cce3b22006-01-06 00:20:16 -08003362 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11003363 } else {
3364 /* completed sync */
3365 if ((!mddev->bitmap || conf->fullsync)
3366 && conf->have_replacement
3367 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3368 /* Completed a full sync so the replacements
3369 * are now fully recovered.
3370 */
NeilBrownf90145f2016-06-02 16:19:52 +10003371 rcu_read_lock();
3372 for (i = 0; i < conf->geo.raid_disks; i++) {
3373 struct md_rdev *rdev =
3374 rcu_dereference(conf->mirrors[i].replacement);
3375 if (rdev)
3376 rdev->recovery_offset = MaxSector;
3377 }
3378 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003379 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003380 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003381 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003382 md_bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07003384 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return sectors_skipped;
3386 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003387
3388 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3389 return reshape_request(mddev, sector_nr, skipped);
3390
NeilBrown5cf00fc2012-05-21 09:28:20 +10003391 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /* if there has been nothing to do on any drive,
3393 * then there is nothing to do at all..
3394 */
NeilBrown57afd892005-06-21 17:17:13 -07003395 *skipped = 1;
3396 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398
NeilBrownc6207272008-02-06 01:39:52 -08003399 if (max_sector > mddev->resync_max)
3400 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /* make sure whole request will fit in a chunk - if chunks
3403 * are meaningful
3404 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003405 if (conf->geo.near_copies < conf->geo.raid_disks &&
3406 max_sector > (sector_nr | chunk_mask))
3407 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003409 /*
3410 * If there is non-resync activity waiting for a turn, then let it
3411 * though before starting on this new sync request.
3412 */
3413 if (conf->nr_waiting)
3414 schedule_timeout_uninterruptible(1);
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 /* Again, very different code for resync and recovery.
3417 * Both must result in an r10bio with a list of bios that
Christoph Hellwig309dca302021-01-24 11:02:34 +01003418 * have bi_end_io, bi_sector, bi_bdev set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 * and bi_private set to the r10bio.
3420 * For recovery, we may actually create several r10bios
3421 * with 2 bios in each, that correspond to the bios in the main one.
3422 * In this case, the subordinate r10bios link back through a
3423 * borrowed master_bio pointer, and the counter in the master
3424 * includes a ref from each subordinate.
3425 */
3426 /* First, we decide what to do and set ->bi_end_io
3427 * To end_sync_read if we want to read, and
3428 * end_sync_write if we will want to write.
3429 */
3430
NeilBrown6cce3b22006-01-06 00:20:16 -08003431 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3433 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003434 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 r10_bio = NULL;
3436
NeilBrown5cf00fc2012-05-21 09:28:20 +10003437 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003438 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003439 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003440 sector_t sect;
3441 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003442 int any_working;
Alex Wuee37d732018-09-21 16:05:03 +08003443 int need_recover = 0;
3444 int need_replace = 0;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003445 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003446 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003447
NeilBrownf90145f2016-06-02 16:19:52 +10003448 rcu_read_lock();
3449 mrdev = rcu_dereference(mirror->rdev);
3450 mreplace = rcu_dereference(mirror->replacement);
3451
Alex Wuee37d732018-09-21 16:05:03 +08003452 if (mrdev != NULL &&
3453 !test_bit(Faulty, &mrdev->flags) &&
3454 !test_bit(In_sync, &mrdev->flags))
3455 need_recover = 1;
3456 if (mreplace != NULL &&
3457 !test_bit(Faulty, &mreplace->flags))
3458 need_replace = 1;
3459
3460 if (!need_recover && !need_replace) {
NeilBrownf90145f2016-06-02 16:19:52 +10003461 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003462 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003463 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003464
3465 still_degraded = 0;
3466 /* want to reconstruct this device */
3467 rb2 = r10_bio;
3468 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003469 if (sect >= mddev->resync_max_sectors) {
3470 /* last stripe is not complete - don't
3471 * try to recover this sector.
3472 */
NeilBrownf90145f2016-06-02 16:19:52 +10003473 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003474 continue;
3475 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003476 if (mreplace && test_bit(Faulty, &mreplace->flags))
3477 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003478 /* Unless we are doing a full sync, or a replacement
3479 * we only need to recover the block if it is set in
3480 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003481 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003482 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3483 &sync_blocks, 1);
NeilBrownab9d47e2011-05-11 14:54:41 +10003484 if (sync_blocks < max_sync)
3485 max_sync = sync_blocks;
3486 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003487 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003488 !conf->fullsync) {
3489 /* yep, skip the sync_blocks here, but don't assume
3490 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003491 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003492 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003493 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003494 continue;
3495 }
NeilBrownf90145f2016-06-02 16:19:52 +10003496 atomic_inc(&mrdev->nr_pending);
3497 if (mreplace)
3498 atomic_inc(&mreplace->nr_pending);
3499 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Shaohua Li208410b2017-08-24 17:50:40 -07003501 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003502 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003503 raise_barrier(conf, rb2 != NULL);
3504 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
NeilBrownab9d47e2011-05-11 14:54:41 +10003506 r10_bio->master_bio = (struct bio*)rb2;
3507 if (rb2)
3508 atomic_inc(&rb2->remaining);
3509 r10_bio->mddev = mddev;
3510 set_bit(R10BIO_IsRecover, &r10_bio->state);
3511 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003512
NeilBrownab9d47e2011-05-11 14:54:41 +10003513 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003514
NeilBrownab9d47e2011-05-11 14:54:41 +10003515 /* Need to check if the array will still be
3516 * degraded
3517 */
NeilBrownf90145f2016-06-02 16:19:52 +10003518 rcu_read_lock();
3519 for (j = 0; j < conf->geo.raid_disks; j++) {
3520 struct md_rdev *rdev = rcu_dereference(
3521 conf->mirrors[j].rdev);
3522 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003523 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
NeilBrownf90145f2016-06-02 16:19:52 +10003526 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003527
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003528 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3529 &sync_blocks, still_degraded);
NeilBrownab9d47e2011-05-11 14:54:41 +10003530
NeilBrowne875ece2011-07-28 11:39:24 +10003531 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003532 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003533 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003534 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003535 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003536 struct md_rdev *rdev =
3537 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003538 sector_t sector, first_bad;
3539 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003540 if (!rdev ||
3541 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003542 continue;
3543 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003544 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003545 sector = r10_bio->devs[j].addr;
3546
3547 if (is_badblock(rdev, sector, max_sync,
3548 &first_bad, &bad_sectors)) {
3549 if (first_bad > sector)
3550 max_sync = first_bad - sector;
3551 else {
3552 bad_sectors -= (sector
3553 - first_bad);
3554 if (max_sync > bad_sectors)
3555 max_sync = bad_sectors;
3556 continue;
3557 }
3558 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003559 bio = r10_bio->devs[0].bio;
3560 bio->bi_next = biolist;
3561 biolist = bio;
NeilBrownab9d47e2011-05-11 14:54:41 +10003562 bio->bi_end_io = end_sync_read;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003563 bio->bi_opf = REQ_OP_READ;
NeilBrown8d3ca832016-11-18 16:16:12 +11003564 if (test_bit(FailFast, &rdev->flags))
3565 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003566 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003567 bio->bi_iter.bi_sector = from_addr +
3568 rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003569 bio_set_dev(bio, rdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003570 atomic_inc(&rdev->nr_pending);
3571 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003572
3573 for (k=0; k<conf->copies; k++)
3574 if (r10_bio->devs[k].devnum == i)
3575 break;
3576 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003577 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003578 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003579 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003580 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003581 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003582
Alex Wuee37d732018-09-21 16:05:03 +08003583 if (need_recover) {
NeilBrown24afd802011-12-23 10:17:55 +11003584 bio = r10_bio->devs[1].bio;
3585 bio->bi_next = biolist;
3586 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003587 bio->bi_end_io = end_sync_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003588 bio->bi_opf = REQ_OP_WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003589 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003590 + mrdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003591 bio_set_dev(bio, mrdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003592 atomic_inc(&r10_bio->remaining);
3593 } else
3594 r10_bio->devs[1].bio->bi_end_io = NULL;
3595
3596 /* and maybe write to replacement */
3597 bio = r10_bio->devs[1].repl_bio;
3598 if (bio)
3599 bio->bi_end_io = NULL;
Alex Wuee37d732018-09-21 16:05:03 +08003600 /* Note: if need_replace, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003601 * cannot be NULL as r10buf_pool_alloc will
3602 * have allocated it.
NeilBrown24afd802011-12-23 10:17:55 +11003603 */
Alex Wuee37d732018-09-21 16:05:03 +08003604 if (!need_replace)
NeilBrown24afd802011-12-23 10:17:55 +11003605 break;
3606 bio->bi_next = biolist;
3607 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003608 bio->bi_end_io = end_sync_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003609 bio->bi_opf = REQ_OP_WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003610 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003611 mreplace->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003612 bio_set_dev(bio, mreplace->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003613 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003614 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 }
NeilBrownf90145f2016-06-02 16:19:52 +10003616 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003617 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003618 /* Cannot recover, so abort the recovery or
3619 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003620 if (any_working) {
3621 /* problem is that there are bad blocks
3622 * on other device(s)
3623 */
3624 int k;
3625 for (k = 0; k < conf->copies; k++)
3626 if (r10_bio->devs[k].devnum == i)
3627 break;
NeilBrown24afd802011-12-23 10:17:55 +11003628 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003629 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003630 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003631 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003632 r10_bio->devs[k].addr,
3633 max_sync, 0))
3634 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003635 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003636 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003637 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003638 r10_bio->devs[k].addr,
3639 max_sync, 0))
3640 any_working = 0;
3641 }
3642 if (!any_working) {
3643 if (!test_and_set_bit(MD_RECOVERY_INTR,
3644 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003645 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003646 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003647 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003648 = mddev->recovery_disabled;
3649 }
NeilBrowne8b84912014-01-06 10:35:34 +11003650 put_buf(r10_bio);
3651 if (rb2)
3652 atomic_dec(&rb2->remaining);
3653 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003654 rdev_dec_pending(mrdev, mddev);
3655 if (mreplace)
3656 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003657 break;
3658 }
NeilBrownf90145f2016-06-02 16:19:52 +10003659 rdev_dec_pending(mrdev, mddev);
3660 if (mreplace)
3661 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003662 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3663 /* Only want this if there is elsewhere to
3664 * read from. 'j' is currently the first
3665 * readable copy.
3666 */
3667 int targets = 1;
3668 for (; j < conf->copies; j++) {
3669 int d = r10_bio->devs[j].devnum;
3670 if (conf->mirrors[d].rdev &&
3671 test_bit(In_sync,
3672 &conf->mirrors[d].rdev->flags))
3673 targets++;
3674 }
3675 if (targets == 1)
3676 r10_bio->devs[0].bio->bi_opf
3677 &= ~MD_FAILFAST;
3678 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (biolist == NULL) {
3681 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003682 struct r10bio *rb2 = r10_bio;
3683 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 rb2->master_bio = NULL;
3685 put_buf(rb2);
3686 }
3687 goto giveup;
3688 }
3689 } else {
3690 /* resync. Schedule a read for every block at this virt offset */
3691 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003692
Guoqing Jiang8db87912017-10-24 15:11:52 +08003693 /*
3694 * Since curr_resync_completed could probably not update in
3695 * time, and we will set cluster_sync_low based on it.
3696 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3697 * safety reason, which ensures curr_resync_completed is
3698 * updated in bitmap_cond_end_sync.
3699 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003700 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3701 mddev_is_clustered(mddev) &&
3702 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown78200d42009-02-25 13:18:47 +11003703
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003704 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3705 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003706 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3707 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003708 /* We can skip this block */
3709 *skipped = 1;
3710 return sync_blocks + sectors_skipped;
3711 }
3712 if (sync_blocks < max_sync)
3713 max_sync = sync_blocks;
Shaohua Li208410b2017-08-24 17:50:40 -07003714 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003715 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 r10_bio->mddev = mddev;
3718 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003719 raise_barrier(conf, 0);
3720 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 r10_bio->master_bio = NULL;
3723 r10_bio->sector = sector_nr;
3724 set_bit(R10BIO_IsSync, &r10_bio->state);
3725 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003726 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
NeilBrown5cf00fc2012-05-21 09:28:20 +10003728 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003730 sector_t first_bad, sector;
3731 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003732 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003733
NeilBrown9ad1aef2011-12-23 10:17:55 +11003734 if (r10_bio->devs[i].repl_bio)
3735 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3736
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 bio = r10_bio->devs[i].bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003738 bio->bi_status = BLK_STS_IOERR;
NeilBrownf90145f2016-06-02 16:19:52 +10003739 rcu_read_lock();
3740 rdev = rcu_dereference(conf->mirrors[d].rdev);
3741 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3742 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003744 }
NeilBrown40c356c2011-07-28 11:39:24 +10003745 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003746 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003747 &first_bad, &bad_sectors)) {
3748 if (first_bad > sector)
3749 max_sync = first_bad - sector;
3750 else {
3751 bad_sectors -= (sector - first_bad);
3752 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003753 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003754 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003755 continue;
3756 }
3757 }
NeilBrownf90145f2016-06-02 16:19:52 +10003758 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 atomic_inc(&r10_bio->remaining);
3760 bio->bi_next = biolist;
3761 biolist = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 bio->bi_end_io = end_sync_read;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003763 bio->bi_opf = REQ_OP_READ;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003764 if (test_bit(FailFast, &rdev->flags))
NeilBrown8d3ca832016-11-18 16:16:12 +11003765 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003766 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003767 bio_set_dev(bio, rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003769
NeilBrownf90145f2016-06-02 16:19:52 +10003770 rdev = rcu_dereference(conf->mirrors[d].replacement);
3771 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3772 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003773 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003774 }
3775 atomic_inc(&rdev->nr_pending);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003776
3777 /* Need to set up for writing to the replacement */
3778 bio = r10_bio->devs[i].repl_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003779 bio->bi_status = BLK_STS_IOERR;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003780
3781 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003782 bio->bi_next = biolist;
3783 biolist = bio;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003784 bio->bi_end_io = end_sync_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003785 bio->bi_opf = REQ_OP_WRITE;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003786 if (test_bit(FailFast, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +11003787 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003788 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003789 bio_set_dev(bio, rdev->bdev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003790 count++;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003791 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793
3794 if (count < 2) {
3795 for (i=0; i<conf->copies; i++) {
3796 int d = r10_bio->devs[i].devnum;
3797 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003798 rdev_dec_pending(conf->mirrors[d].rdev,
3799 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003800 if (r10_bio->devs[i].repl_bio &&
3801 r10_bio->devs[i].repl_bio->bi_end_io)
3802 rdev_dec_pending(
3803 conf->mirrors[d].replacement,
3804 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806 put_buf(r10_bio);
3807 biolist = NULL;
3808 goto giveup;
3809 }
3810 }
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003813 if (sector_nr + max_sync < max_sector)
3814 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 do {
3816 struct page *page;
3817 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 if (sector_nr + (len>>9) > max_sector)
3819 len = (max_sector - sector_nr) << 9;
3820 if (len == 0)
3821 break;
3822 for (bio= biolist ; bio ; bio=bio->bi_next) {
Ming Leif0250612017-03-17 00:12:33 +08003823 struct resync_pages *rp = get_resync_pages(bio);
Ming Lei022e5102017-07-14 16:14:42 +08003824 page = resync_fetch_page(rp, page_idx);
Johannes Thumshirn0c67dd62023-05-31 04:50:38 -07003825 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
3826 bio->bi_status = BLK_STS_RESOURCE;
3827 bio_endio(bio);
3828 goto giveup;
3829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
3831 nr_sectors += len>>9;
3832 sector_nr += len>>9;
Ming Lei022e5102017-07-14 16:14:42 +08003833 } while (++page_idx < RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 r10_bio->sectors = nr_sectors;
3835
Guoqing Jiang8db87912017-10-24 15:11:52 +08003836 if (mddev_is_clustered(mddev) &&
3837 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3838 /* It is resync not recovery */
3839 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3840 conf->cluster_sync_low = mddev->curr_resync_completed;
3841 raid10_set_cluster_sync_high(conf);
3842 /* Send resync message */
3843 md_cluster_ops->resync_info_update(mddev,
3844 conf->cluster_sync_low,
3845 conf->cluster_sync_high);
3846 }
3847 } else if (mddev_is_clustered(mddev)) {
3848 /* This is recovery not resync */
3849 sector_t sect_va1, sect_va2;
3850 bool broadcast_msg = false;
3851
3852 for (i = 0; i < conf->geo.raid_disks; i++) {
3853 /*
3854 * sector_nr is a device address for recovery, so we
3855 * need translate it to array address before compare
3856 * with cluster_sync_high.
3857 */
3858 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3859
3860 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3861 broadcast_msg = true;
3862 /*
3863 * curr_resync_completed is similar as
3864 * sector_nr, so make the translation too.
3865 */
3866 sect_va2 = raid10_find_virt(conf,
3867 mddev->curr_resync_completed, i);
3868
3869 if (conf->cluster_sync_low == 0 ||
3870 conf->cluster_sync_low > sect_va2)
3871 conf->cluster_sync_low = sect_va2;
3872 }
3873 }
3874 if (broadcast_msg) {
3875 raid10_set_cluster_sync_high(conf);
3876 md_cluster_ops->resync_info_update(mddev,
3877 conf->cluster_sync_low,
3878 conf->cluster_sync_high);
3879 }
3880 }
3881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 while (biolist) {
3883 bio = biolist;
3884 biolist = biolist->bi_next;
3885
3886 bio->bi_next = NULL;
Ming Leif0250612017-03-17 00:12:33 +08003887 r10_bio = get_resync_r10bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 r10_bio->sectors = nr_sectors;
3889
3890 if (bio->bi_end_io == end_sync_read) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02003891 md_sync_acct_bio(bio, nr_sectors);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003892 bio->bi_status = 0;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02003893 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 }
3895 }
3896
NeilBrown57afd892005-06-21 17:17:13 -07003897 if (sectors_skipped)
3898 /* pretend they weren't skipped, it makes
3899 * no important difference in this case
3900 */
3901 md_done_sync(mddev, sectors_skipped, 1);
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return sectors_skipped + nr_sectors;
3904 giveup:
3905 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003906 * drives must be failed or in resync, all drives
3907 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 */
NeilBrown09b40682009-02-25 13:18:47 +11003909 if (sector_nr + max_sync < max_sector)
3910 max_sector = sector_nr + max_sync;
3911
3912 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 chunks_skipped ++;
3914 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
Dan Williams80c3a6c2009-03-17 18:10:40 -07003918static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003919raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003920{
3921 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003922 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003923
3924 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003925 raid_disks = min(conf->geo.raid_disks,
3926 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003927 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003928 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003929
NeilBrown5cf00fc2012-05-21 09:28:20 +10003930 size = sectors >> conf->geo.chunk_shift;
3931 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003932 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003933 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003934
NeilBrown5cf00fc2012-05-21 09:28:20 +10003935 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003936}
3937
NeilBrown6508fdb2012-05-17 10:08:45 +10003938static void calc_sectors(struct r10conf *conf, sector_t size)
3939{
3940 /* Calculate the number of sectors-per-device that will
3941 * actually be used, and set conf->dev_sectors and
3942 * conf->stride
3943 */
3944
NeilBrown5cf00fc2012-05-21 09:28:20 +10003945 size = size >> conf->geo.chunk_shift;
3946 sector_div(size, conf->geo.far_copies);
3947 size = size * conf->geo.raid_disks;
3948 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003949 /* 'size' is now the number of chunks in the array */
3950 /* calculate "used chunks per device" */
3951 size = size * conf->copies;
3952
3953 /* We need to round up when dividing by raid_disks to
3954 * get the stride size.
3955 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003956 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003957
NeilBrown5cf00fc2012-05-21 09:28:20 +10003958 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003959
NeilBrown5cf00fc2012-05-21 09:28:20 +10003960 if (conf->geo.far_offset)
3961 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003962 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003963 sector_div(size, conf->geo.far_copies);
3964 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003965 }
3966}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003967
NeilBrowndeb200d2012-05-21 09:28:33 +10003968enum geo_type {geo_new, geo_old, geo_start};
3969static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3970{
3971 int nc, fc, fo;
3972 int layout, chunk, disks;
3973 switch (new) {
3974 case geo_old:
3975 layout = mddev->layout;
3976 chunk = mddev->chunk_sectors;
3977 disks = mddev->raid_disks - mddev->delta_disks;
3978 break;
3979 case geo_new:
3980 layout = mddev->new_layout;
3981 chunk = mddev->new_chunk_sectors;
3982 disks = mddev->raid_disks;
3983 break;
3984 default: /* avoid 'may be unused' warnings */
3985 case geo_start: /* new when starting reshape - raid_disks not
3986 * updated yet. */
3987 layout = mddev->new_layout;
3988 chunk = mddev->new_chunk_sectors;
3989 disks = mddev->raid_disks + mddev->delta_disks;
3990 break;
3991 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003992 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003993 return -1;
3994 if (chunk < (PAGE_SIZE >> 9) ||
3995 !is_power_of_2(chunk))
3996 return -2;
3997 nc = layout & 255;
3998 fc = (layout >> 8) & 255;
3999 fo = layout & (1<<16);
4000 geo->raid_disks = disks;
4001 geo->near_copies = nc;
4002 geo->far_copies = fc;
4003 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11004004 switch (layout >> 17) {
4005 case 0: /* original layout. simple but not always optimal */
4006 geo->far_set_size = disks;
4007 break;
4008 case 1: /* "improved" layout which was buggy. Hopefully no-one is
4009 * actually using this, but leave code here just in case.*/
4010 geo->far_set_size = disks/fc;
4011 WARN(geo->far_set_size < fc,
4012 "This RAID10 layout does not provide data safety - please backup and create new array\n");
4013 break;
4014 case 2: /* "improved" layout fixed to match documentation */
4015 geo->far_set_size = fc * nc;
4016 break;
4017 default: /* Not a valid layout */
4018 return -1;
4019 }
NeilBrowndeb200d2012-05-21 09:28:33 +10004020 geo->chunk_mask = chunk - 1;
4021 geo->chunk_shift = ffz(~chunk);
4022 return nc*fc;
4023}
4024
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08004025static void raid10_free_conf(struct r10conf *conf)
4026{
4027 if (!conf)
4028 return;
4029
4030 mempool_exit(&conf->r10bio_pool);
4031 kfree(conf->mirrors);
4032 kfree(conf->mirrors_old);
4033 kfree(conf->mirrors_new);
4034 safe_put_page(conf->tmppage);
4035 bioset_exit(&conf->bio_split);
4036 kfree(conf);
4037}
4038
NeilBrowne879a872011-10-11 16:49:02 +11004039static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
NeilBrowne879a872011-10-11 16:49:02 +11004041 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004042 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10004043 struct geom geo;
4044 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
NeilBrowndeb200d2012-05-21 09:28:33 +10004046 copies = setup_geo(&geo, mddev, geo_new);
4047
4048 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11004049 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
4050 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
NeilBrown2604b702006-01-06 00:20:36 -08004053
NeilBrowndeb200d2012-05-21 09:28:33 +10004054 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11004055 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
4056 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 goto out;
4058 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11004059
4060 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11004061 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004062 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004064
NeilBrown3ea7daa2012-05-22 13:53:47 +10004065 /* FIXME calc properly */
Kees Cook6396bb22018-06-12 14:03:40 -07004066 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
4067 sizeof(struct raid10_info),
Trela, Maciejdab8b292010-03-08 16:02:45 +11004068 GFP_KERNEL);
4069 if (!conf->mirrors)
4070 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08004071
4072 conf->tmppage = alloc_page(GFP_KERNEL);
4073 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004074 goto out;
4075
NeilBrowndeb200d2012-05-21 09:28:33 +10004076 conf->geo = geo;
4077 conf->copies = copies;
Marcos Paulo de Souza3f677f92019-06-14 15:41:04 -07004078 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -07004079 rbio_pool_free, conf);
Kent Overstreetafeee512018-05-20 18:25:52 -04004080 if (err)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004081 goto out;
4082
Kent Overstreetafeee512018-05-20 18:25:52 -04004083 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
4084 if (err)
NeilBrownfc9977d2017-04-05 14:05:51 +10004085 goto out;
4086
NeilBrown6508fdb2012-05-17 10:08:45 +10004087 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004088 if (mddev->reshape_position == MaxSector) {
4089 conf->prev = conf->geo;
4090 conf->reshape_progress = MaxSector;
4091 } else {
4092 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
4093 err = -EINVAL;
4094 goto out;
4095 }
4096 conf->reshape_progress = mddev->reshape_position;
4097 if (conf->prev.far_offset)
4098 conf->prev.stride = 1 << conf->prev.chunk_shift;
4099 else
4100 /* far_copies must be 1 */
4101 conf->prev.stride = conf->dev_sectors;
4102 }
NeilBrown299b0682015-07-06 17:37:49 +10004103 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07004104 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004105 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10004106 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004107
Yu Kuaib9b083f2022-09-16 19:34:28 +08004108 seqlock_init(&conf->resync_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004109 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02004110 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004111
Kent Overstreetafeee512018-05-20 18:25:52 -04004112 err = -ENOMEM;
Yu Kuai44693152023-05-23 10:10:17 +08004113 rcu_assign_pointer(conf->thread,
4114 md_register_thread(raid10d, mddev, "raid10"));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004115 if (!conf->thread)
4116 goto out;
4117
Trela, Maciejdab8b292010-03-08 16:02:45 +11004118 conf->mddev = mddev;
4119 return conf;
4120
4121 out:
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08004122 raid10_free_conf(conf);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004123 return ERR_PTR(err);
4124}
4125
Christoph Hellwig16ef5102020-09-24 08:51:33 +02004126static void raid10_set_io_opt(struct r10conf *conf)
4127{
4128 int raid_disks = conf->geo.raid_disks;
4129
4130 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4131 raid_disks /= conf->geo.near_copies;
4132 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4133 raid_disks);
4134}
4135
Shaohua Li849674e2016-01-20 13:52:20 -08004136static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004137{
NeilBrowne879a872011-10-11 16:49:02 +11004138 struct r10conf *conf;
Christoph Hellwig16ef5102020-09-24 08:51:33 +02004139 int i, disk_idx;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004140 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11004141 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004142 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004143 sector_t min_offset_diff = 0;
4144 int first = 1;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004145
NeilBrowna415c0f2017-06-05 16:05:13 +10004146 if (mddev_init_writes_pending(mddev) < 0)
4147 return -ENOMEM;
4148
Trela, Maciejdab8b292010-03-08 16:02:45 +11004149 if (mddev->private == NULL) {
4150 conf = setup_conf(mddev);
4151 if (IS_ERR(conf))
4152 return PTR_ERR(conf);
4153 mddev->private = conf;
4154 }
4155 conf = mddev->private;
4156 if (!conf)
4157 goto out;
4158
Yu Kuai44693152023-05-23 10:10:17 +08004159 rcu_assign_pointer(mddev->thread, conf->thread);
4160 rcu_assign_pointer(conf->thread, NULL);
Yu Kuaif0ddb832023-03-10 15:38:55 +08004161
Guoqing Jiang8db87912017-10-24 15:11:52 +08004162 if (mddev_is_clustered(conf->mddev)) {
4163 int fc, fo;
4164
4165 fc = (mddev->layout >> 8) & 255;
4166 fo = mddev->layout & (1<<16);
4167 if (fc > 1 || fo > 0) {
4168 pr_err("only near layout is supported by clustered"
4169 " raid10\n");
Lidong Zhong43a52122018-01-23 23:06:12 +08004170 goto out_free_conf;
Guoqing Jiang8db87912017-10-24 15:11:52 +08004171 }
4172 }
4173
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10004174 if (mddev->queue) {
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02004175 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
Christoph Hellwig16ef5102020-09-24 08:51:33 +02004176 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4177 raid10_set_io_opt(conf);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10004178 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004179
NeilBrowndafb20f2012-03-19 12:46:39 +11004180 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004181 long long diff;
NeilBrown34b343c2011-07-28 11:31:47 +10004182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10004184 if (disk_idx < 0)
4185 continue;
4186 if (disk_idx >= conf->geo.raid_disks &&
4187 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 continue;
4189 disk = conf->mirrors + disk_idx;
4190
NeilBrown56a2559b2011-12-23 10:17:55 +11004191 if (test_bit(Replacement, &rdev->flags)) {
4192 if (disk->replacement)
4193 goto out_free_conf;
4194 disk->replacement = rdev;
4195 } else {
4196 if (disk->rdev)
4197 goto out_free_conf;
4198 disk->rdev = rdev;
4199 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004200 diff = (rdev->new_data_offset - rdev->data_offset);
4201 if (!mddev->reshape_backwards)
4202 diff = -diff;
4203 if (diff < 0)
4204 diff = 0;
4205 if (first || diff < min_offset_diff)
4206 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11004207
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10004208 if (mddev->gendisk)
4209 disk_stack_limits(mddev->gendisk, rdev->bdev,
4210 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
4212 disk->head_position = 0;
Guoqing Jiang6f287ca2017-04-06 09:12:18 +08004213 first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004215
NeilBrown6d508242005-09-09 16:24:03 -07004216 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10004217 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11004218 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07004219 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 goto out_free_conf;
4221 }
4222
NeilBrown3ea7daa2012-05-22 13:53:47 +10004223 if (conf->reshape_progress != MaxSector) {
4224 /* must ensure that shape change is supported */
4225 if (conf->geo.far_copies != 1 &&
4226 conf->geo.far_offset == 0)
4227 goto out_free_conf;
4228 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10004229 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004230 goto out_free_conf;
4231 }
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10004234 for (i = 0;
4235 i < conf->geo.raid_disks
4236 || i < conf->prev.raid_disks;
4237 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 disk = conf->mirrors + i;
4240
NeilBrown56a2559b2011-12-23 10:17:55 +11004241 if (!disk->rdev && disk->replacement) {
4242 /* The replacement is all we have - use it */
4243 disk->rdev = disk->replacement;
4244 disk->replacement = NULL;
4245 clear_bit(Replacement, &disk->rdev->flags);
4246 }
4247
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004248 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07004249 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 disk->head_position = 0;
4251 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11004252 if (disk->rdev &&
4253 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10004254 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
BingJing Changbda31532018-06-28 18:40:11 +08004256
4257 if (disk->replacement &&
4258 !test_bit(In_sync, &disk->replacement->flags) &&
4259 disk->replacement->saved_raid_disk < 0) {
4260 conf->fullsync = 1;
4261 }
4262
NeilBrownd890fa22011-10-26 11:54:39 +11004263 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265
Andre Noll8c6ac8682009-06-18 08:48:06 +10004266 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11004267 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4268 mdname(mddev));
4269 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10004270 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4271 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 /*
4273 * Ok, everything is just fine now
4274 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11004275 mddev->dev_sectors = conf->dev_sectors;
4276 size = raid10_size(mddev, 0, 0);
4277 md_set_array_sectors(mddev, size);
4278 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11004279 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Martin K. Petersena91a2782011-03-17 11:11:05 +01004281 if (md_integrity_register(mddev))
4282 goto out_free_conf;
4283
NeilBrown3ea7daa2012-05-22 13:53:47 +10004284 if (conf->reshape_progress != MaxSector) {
4285 unsigned long before_length, after_length;
4286
4287 before_length = ((1 << conf->prev.chunk_shift) *
4288 conf->prev.far_copies);
4289 after_length = ((1 << conf->geo.chunk_shift) *
4290 conf->geo.far_copies);
4291
4292 if (max(before_length, after_length) > min_offset_diff) {
4293 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11004294 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10004295 goto out_free_conf;
4296 }
4297 conf->offset_diff = min_offset_diff;
4298
NeilBrown3ea7daa2012-05-22 13:53:47 +10004299 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4300 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4301 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4302 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
Yu Kuai44693152023-05-23 10:10:17 +08004303 rcu_assign_pointer(mddev->sync_thread,
4304 md_register_thread(md_do_sync, mddev, "reshape"));
Aditya Pakkie406f122019-03-04 16:48:54 -06004305 if (!mddev->sync_thread)
4306 goto out_free_conf;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004307 }
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return 0;
4310
4311out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10004312 md_unregister_thread(&mddev->thread);
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08004313 raid10_free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 mddev->private = NULL;
4315out:
4316 return -EIO;
4317}
4318
NeilBrownafa0f552014-12-15 12:56:58 +11004319static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08004321 raid10_free_conf(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
NeilBrownb03e0cc2017-10-19 12:49:15 +11004324static void raid10_quiesce(struct mddev *mddev, int quiesce)
NeilBrown6cce3b22006-01-06 00:20:16 -08004325{
NeilBrowne879a872011-10-11 16:49:02 +11004326 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08004327
NeilBrownb03e0cc2017-10-19 12:49:15 +11004328 if (quiesce)
NeilBrown6cce3b22006-01-06 00:20:16 -08004329 raise_barrier(conf, 0);
NeilBrownb03e0cc2017-10-19 12:49:15 +11004330 else
NeilBrown6cce3b22006-01-06 00:20:16 -08004331 lower_barrier(conf);
NeilBrown6cce3b22006-01-06 00:20:16 -08004332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
NeilBrown006a09a2012-03-19 12:46:40 +11004334static int raid10_resize(struct mddev *mddev, sector_t sectors)
4335{
4336 /* Resize of 'far' arrays is not supported.
4337 * For 'near' and 'offset' arrays we can set the
4338 * number of sectors used to be an appropriate multiple
4339 * of the chunk size.
4340 * For 'offset', this is far_copies*chunksize.
4341 * For 'near' the multiplier is the LCM of
4342 * near_copies and raid_disks.
4343 * So if far_copies > 1 && !far_offset, fail.
4344 * Else find LCM(raid_disks, near_copy)*far_copies and
4345 * multiply by chunk_size. Then round to this number.
4346 * This is mostly done by raid10_size()
4347 */
4348 struct r10conf *conf = mddev->private;
4349 sector_t oldsize, size;
4350
NeilBrownf8c9e742012-05-21 09:28:33 +10004351 if (mddev->reshape_position != MaxSector)
4352 return -EBUSY;
4353
NeilBrown5cf00fc2012-05-21 09:28:20 +10004354 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11004355 return -EINVAL;
4356
4357 oldsize = raid10_size(mddev, 0, 0);
4358 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10004359 if (mddev->external_size &&
4360 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11004361 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10004362 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004363 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10004364 if (ret)
4365 return ret;
4366 }
4367 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11004368 if (sectors > mddev->dev_sectors &&
4369 mddev->recovery_cp > oldsize) {
4370 mddev->recovery_cp = oldsize;
4371 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4372 }
NeilBrown6508fdb2012-05-17 10:08:45 +10004373 calc_sectors(conf, sectors);
4374 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11004375 mddev->resync_max_sectors = size;
4376 return 0;
4377}
4378
NeilBrown53a6ab42015-02-12 14:09:57 +11004379static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004380{
NeilBrown3cb03002011-10-11 16:45:26 +11004381 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11004382 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004383
4384 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11004385 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4386 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004387 return ERR_PTR(-EINVAL);
4388 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004389 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004390
Trela, Maciejdab8b292010-03-08 16:02:45 +11004391 /* Set new parameters */
4392 mddev->new_level = 10;
4393 /* new layout: far_copies = 1, near_copies = 2 */
4394 mddev->new_layout = (1<<8) + 2;
4395 mddev->new_chunk_sectors = mddev->chunk_sectors;
4396 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004397 mddev->raid_disks *= 2;
4398 /* make sure it will be not marked as dirty */
4399 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11004400 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004401
4402 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004403 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11004404 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11004405 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004406 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11004407 rdev->sectors = size;
4408 }
Yu Kuaib9b083f2022-09-16 19:34:28 +08004409 WRITE_ONCE(conf->barrier, 1);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004410 }
4411
Trela, Maciejdab8b292010-03-08 16:02:45 +11004412 return conf;
4413}
4414
NeilBrownfd01b882011-10-11 16:47:53 +11004415static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004416{
NeilBrowne373ab12011-10-11 16:48:59 +11004417 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004418
4419 /* raid10 can take over:
4420 * raid0 - providing it has only two drives
4421 */
4422 if (mddev->level == 0) {
4423 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11004424 raid0_conf = mddev->private;
4425 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11004426 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4427 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004428 return ERR_PTR(-EINVAL);
4429 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004430 return raid10_takeover_raid0(mddev,
4431 raid0_conf->strip_zone->zone_end,
4432 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004433 }
4434 return ERR_PTR(-EINVAL);
4435}
4436
NeilBrown3ea7daa2012-05-22 13:53:47 +10004437static int raid10_check_reshape(struct mddev *mddev)
4438{
4439 /* Called when there is a request to change
4440 * - layout (to ->new_layout)
4441 * - chunk size (to ->new_chunk_sectors)
4442 * - raid_disks (by delta_disks)
4443 * or when trying to restart a reshape that was ongoing.
4444 *
4445 * We need to validate the request and possibly allocate
4446 * space if that might be an issue later.
4447 *
4448 * Currently we reject any reshape of a 'far' mode array,
4449 * allow chunk size to change if new is generally acceptable,
4450 * allow raid_disks to increase, and allow
4451 * a switch between 'near' mode and 'offset' mode.
4452 */
4453 struct r10conf *conf = mddev->private;
4454 struct geom geo;
4455
4456 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4457 return -EINVAL;
4458
4459 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4460 /* mustn't change number of copies */
4461 return -EINVAL;
4462 if (geo.far_copies > 1 && !geo.far_offset)
4463 /* Cannot switch to 'far' mode */
4464 return -EINVAL;
4465
4466 if (mddev->array_sectors & geo.chunk_mask)
4467 /* not factor of array size */
4468 return -EINVAL;
4469
NeilBrown3ea7daa2012-05-22 13:53:47 +10004470 if (!enough(conf, -1))
4471 return -EINVAL;
4472
4473 kfree(conf->mirrors_new);
4474 conf->mirrors_new = NULL;
4475 if (mddev->delta_disks > 0) {
4476 /* allocate new 'mirrors' list */
Kees Cook6396bb22018-06-12 14:03:40 -07004477 conf->mirrors_new =
4478 kcalloc(mddev->raid_disks + mddev->delta_disks,
4479 sizeof(struct raid10_info),
4480 GFP_KERNEL);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004481 if (!conf->mirrors_new)
4482 return -ENOMEM;
4483 }
4484 return 0;
4485}
4486
4487/*
4488 * Need to check if array has failed when deciding whether to:
4489 * - start an array
4490 * - remove non-faulty devices
4491 * - add a spare
4492 * - allow a reshape
4493 * This determination is simple when no reshape is happening.
4494 * However if there is a reshape, we need to carefully check
4495 * both the before and after sections.
4496 * This is because some failed devices may only affect one
4497 * of the two sections, and some non-in_sync devices may
4498 * be insync in the section most affected by failed devices.
4499 */
4500static int calc_degraded(struct r10conf *conf)
4501{
4502 int degraded, degraded2;
4503 int i;
4504
4505 rcu_read_lock();
4506 degraded = 0;
4507 /* 'prev' section first */
4508 for (i = 0; i < conf->prev.raid_disks; i++) {
4509 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4510 if (!rdev || test_bit(Faulty, &rdev->flags))
4511 degraded++;
4512 else if (!test_bit(In_sync, &rdev->flags))
4513 /* When we can reduce the number of devices in
4514 * an array, this might not contribute to
4515 * 'degraded'. It does now.
4516 */
4517 degraded++;
4518 }
4519 rcu_read_unlock();
4520 if (conf->geo.raid_disks == conf->prev.raid_disks)
4521 return degraded;
4522 rcu_read_lock();
4523 degraded2 = 0;
4524 for (i = 0; i < conf->geo.raid_disks; i++) {
4525 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4526 if (!rdev || test_bit(Faulty, &rdev->flags))
4527 degraded2++;
4528 else if (!test_bit(In_sync, &rdev->flags)) {
4529 /* If reshape is increasing the number of devices,
4530 * this section has already been recovered, so
4531 * it doesn't contribute to degraded.
4532 * else it does.
4533 */
4534 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4535 degraded2++;
4536 }
4537 }
4538 rcu_read_unlock();
4539 if (degraded2 > degraded)
4540 return degraded2;
4541 return degraded;
4542}
4543
4544static int raid10_start_reshape(struct mddev *mddev)
4545{
4546 /* A 'reshape' has been requested. This commits
4547 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4548 * This also checks if there are enough spares and adds them
4549 * to the array.
4550 * We currently require enough spares to make the final
4551 * array non-degraded. We also require that the difference
4552 * between old and new data_offset - on each device - is
4553 * enough that we never risk over-writing.
4554 */
4555
4556 unsigned long before_length, after_length;
4557 sector_t min_offset_diff = 0;
4558 int first = 1;
4559 struct geom new;
4560 struct r10conf *conf = mddev->private;
4561 struct md_rdev *rdev;
4562 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004563 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004564
4565 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4566 return -EBUSY;
4567
4568 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4569 return -EINVAL;
4570
4571 before_length = ((1 << conf->prev.chunk_shift) *
4572 conf->prev.far_copies);
4573 after_length = ((1 << conf->geo.chunk_shift) *
4574 conf->geo.far_copies);
4575
4576 rdev_for_each(rdev, mddev) {
4577 if (!test_bit(In_sync, &rdev->flags)
4578 && !test_bit(Faulty, &rdev->flags))
4579 spares++;
4580 if (rdev->raid_disk >= 0) {
4581 long long diff = (rdev->new_data_offset
4582 - rdev->data_offset);
4583 if (!mddev->reshape_backwards)
4584 diff = -diff;
4585 if (diff < 0)
4586 diff = 0;
4587 if (first || diff < min_offset_diff)
4588 min_offset_diff = diff;
Shaohua Lib5063352017-05-01 12:15:07 -07004589 first = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004590 }
4591 }
4592
4593 if (max(before_length, after_length) > min_offset_diff)
4594 return -EINVAL;
4595
4596 if (spares < mddev->delta_disks)
4597 return -EINVAL;
4598
4599 conf->offset_diff = min_offset_diff;
4600 spin_lock_irq(&conf->device_lock);
4601 if (conf->mirrors_new) {
4602 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004603 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004604 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004605 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004606 conf->mirrors_old = conf->mirrors;
4607 conf->mirrors = conf->mirrors_new;
4608 conf->mirrors_new = NULL;
4609 }
4610 setup_geo(&conf->geo, mddev, geo_start);
4611 smp_mb();
4612 if (mddev->reshape_backwards) {
4613 sector_t size = raid10_size(mddev, 0, 0);
4614 if (size < mddev->array_sectors) {
4615 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004616 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4617 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004618 return -EINVAL;
4619 }
4620 mddev->resync_max_sectors = size;
4621 conf->reshape_progress = size;
4622 } else
4623 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004624 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004625 spin_unlock_irq(&conf->device_lock);
4626
NeilBrownbb63a702012-05-22 13:55:28 +10004627 if (mddev->delta_disks && mddev->bitmap) {
Guoqing Jiangafd75622018-10-18 16:37:41 +08004628 struct mdp_superblock_1 *sb = NULL;
4629 sector_t oldsize, newsize;
4630
4631 oldsize = raid10_size(mddev, 0, 0);
4632 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4633
4634 if (!mddev_is_clustered(mddev)) {
4635 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4636 if (ret)
4637 goto abort;
4638 else
4639 goto out;
4640 }
4641
4642 rdev_for_each(rdev, mddev) {
4643 if (rdev->raid_disk > -1 &&
4644 !test_bit(Faulty, &rdev->flags))
4645 sb = page_address(rdev->sb_page);
4646 }
4647
4648 /*
4649 * some node is already performing reshape, and no need to
4650 * call md_bitmap_resize again since it should be called when
4651 * receiving BITMAP_RESIZE msg
4652 */
4653 if ((sb && (le32_to_cpu(sb->feature_map) &
4654 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4655 goto out;
4656
4657 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
NeilBrownbb63a702012-05-22 13:55:28 +10004658 if (ret)
4659 goto abort;
Guoqing Jiangafd75622018-10-18 16:37:41 +08004660
4661 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4662 if (ret) {
4663 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4664 goto abort;
4665 }
NeilBrownbb63a702012-05-22 13:55:28 +10004666 }
Guoqing Jiangafd75622018-10-18 16:37:41 +08004667out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10004668 if (mddev->delta_disks > 0) {
4669 rdev_for_each(rdev, mddev)
4670 if (rdev->raid_disk < 0 &&
4671 !test_bit(Faulty, &rdev->flags)) {
4672 if (raid10_add_disk(mddev, rdev) == 0) {
4673 if (rdev->raid_disk >=
4674 conf->prev.raid_disks)
4675 set_bit(In_sync, &rdev->flags);
4676 else
4677 rdev->recovery_offset = 0;
4678
Damien Le Moal38ffc012020-07-16 13:54:43 +09004679 /* Failure here is OK */
4680 sysfs_link_rdev(mddev, rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004681 }
4682 } else if (rdev->raid_disk >= conf->prev.raid_disks
4683 && !test_bit(Faulty, &rdev->flags)) {
4684 /* This is a spare that was manually added */
4685 set_bit(In_sync, &rdev->flags);
4686 }
4687 }
4688 /* When a reshape changes the number of devices,
4689 * ->degraded is measured against the larger of the
4690 * pre and post numbers.
4691 */
4692 spin_lock_irq(&conf->device_lock);
4693 mddev->degraded = calc_degraded(conf);
4694 spin_unlock_irq(&conf->device_lock);
4695 mddev->raid_disks = conf->geo.raid_disks;
4696 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004697 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004698
4699 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4700 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004701 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004702 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4703 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4704
Yu Kuai44693152023-05-23 10:10:17 +08004705 rcu_assign_pointer(mddev->sync_thread,
4706 md_register_thread(md_do_sync, mddev, "reshape"));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004707 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004708 ret = -EAGAIN;
4709 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004710 }
4711 conf->reshape_checkpoint = jiffies;
4712 md_wakeup_thread(mddev->sync_thread);
Guoqing Jiang54679482021-10-04 23:34:53 +08004713 md_new_event();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004714 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004715
4716abort:
4717 mddev->recovery = 0;
4718 spin_lock_irq(&conf->device_lock);
4719 conf->geo = conf->prev;
4720 mddev->raid_disks = conf->geo.raid_disks;
4721 rdev_for_each(rdev, mddev)
4722 rdev->new_data_offset = rdev->data_offset;
4723 smp_wmb();
4724 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004725 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004726 mddev->reshape_position = MaxSector;
4727 spin_unlock_irq(&conf->device_lock);
4728 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004729}
4730
4731/* Calculate the last device-address that could contain
4732 * any block from the chunk that includes the array-address 's'
4733 * and report the next address.
4734 * i.e. the address returned will be chunk-aligned and after
4735 * any data that is in the chunk containing 's'.
4736 */
4737static sector_t last_dev_address(sector_t s, struct geom *geo)
4738{
4739 s = (s | geo->chunk_mask) + 1;
4740 s >>= geo->chunk_shift;
4741 s *= geo->near_copies;
4742 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4743 s *= geo->far_copies;
4744 s <<= geo->chunk_shift;
4745 return s;
4746}
4747
4748/* Calculate the first device-address that could contain
4749 * any block from the chunk that includes the array-address 's'.
4750 * This too will be the start of a chunk
4751 */
4752static sector_t first_dev_address(sector_t s, struct geom *geo)
4753{
4754 s >>= geo->chunk_shift;
4755 s *= geo->near_copies;
4756 sector_div(s, geo->raid_disks);
4757 s *= geo->far_copies;
4758 s <<= geo->chunk_shift;
4759 return s;
4760}
4761
4762static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4763 int *skipped)
4764{
4765 /* We simply copy at most one chunk (smallest of old and new)
4766 * at a time, possibly less if that exceeds RESYNC_PAGES,
4767 * or we hit a bad block or something.
4768 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004769 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004770 * can record any location.
4771 *
4772 * If we will want to write to a location that isn't
4773 * yet recorded as 'safe' (i.e. in metadata on disk) then
4774 * we need to flush all reshape requests and update the metadata.
4775 *
4776 * When reshaping forwards (e.g. to more devices), we interpret
4777 * 'safe' as the earliest block which might not have been copied
4778 * down yet. We divide this by previous stripe size and multiply
4779 * by previous stripe length to get lowest device offset that we
4780 * cannot write to yet.
4781 * We interpret 'sector_nr' as an address that we want to write to.
4782 * From this we use last_device_address() to find where we might
4783 * write to, and first_device_address on the 'safe' position.
4784 * If this 'next' write position is after the 'safe' position,
4785 * we must update the metadata to increase the 'safe' position.
4786 *
4787 * When reshaping backwards, we round in the opposite direction
4788 * and perform the reverse test: next write position must not be
4789 * less than current safe position.
4790 *
4791 * In all this the minimum difference in data offsets
4792 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004793 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004794 *
4795 * We need to prepare all the bios here before we start any IO
4796 * to ensure the size we choose is acceptable to all devices.
4797 * The means one for each copy for write-out and an extra one for
4798 * read-in.
4799 * We store the read-in bio in ->master_bio and the others in
4800 * ->devs[x].bio and ->devs[x].repl_bio.
4801 */
4802 struct r10conf *conf = mddev->private;
4803 struct r10bio *r10_bio;
4804 sector_t next, safe, last;
4805 int max_sectors;
4806 int nr_sectors;
4807 int s;
4808 struct md_rdev *rdev;
4809 int need_flush = 0;
4810 struct bio *blist;
4811 struct bio *bio, *read_bio;
4812 int sectors_done = 0;
Ming Leif0250612017-03-17 00:12:33 +08004813 struct page **pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004814
4815 if (sector_nr == 0) {
4816 /* If restarting in the middle, skip the initial sectors */
4817 if (mddev->reshape_backwards &&
4818 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4819 sector_nr = (raid10_size(mddev, 0, 0)
4820 - conf->reshape_progress);
4821 } else if (!mddev->reshape_backwards &&
4822 conf->reshape_progress > 0)
4823 sector_nr = conf->reshape_progress;
4824 if (sector_nr) {
4825 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07004826 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004827 *skipped = 1;
4828 return sector_nr;
4829 }
4830 }
4831
4832 /* We don't use sector_nr to track where we are up to
4833 * as that doesn't work well for ->reshape_backwards.
4834 * So just use ->reshape_progress.
4835 */
4836 if (mddev->reshape_backwards) {
4837 /* 'next' is the earliest device address that we might
4838 * write to for this chunk in the new layout
4839 */
4840 next = first_dev_address(conf->reshape_progress - 1,
4841 &conf->geo);
4842
4843 /* 'safe' is the last device address that we might read from
4844 * in the old layout after a restart
4845 */
4846 safe = last_dev_address(conf->reshape_safe - 1,
4847 &conf->prev);
4848
4849 if (next + conf->offset_diff < safe)
4850 need_flush = 1;
4851
4852 last = conf->reshape_progress - 1;
4853 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4854 & conf->prev.chunk_mask);
Zhen Leie2873082020-08-21 09:29:18 +08004855 if (sector_nr + RESYNC_SECTORS < last)
4856 sector_nr = last + 1 - RESYNC_SECTORS;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004857 } else {
4858 /* 'next' is after the last device address that we
4859 * might write to for this chunk in the new layout
4860 */
4861 next = last_dev_address(conf->reshape_progress, &conf->geo);
4862
4863 /* 'safe' is the earliest device address that we might
4864 * read from in the old layout after a restart
4865 */
4866 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4867
4868 /* Need to update metadata if 'next' might be beyond 'safe'
4869 * as that would possibly corrupt data
4870 */
4871 if (next > safe + conf->offset_diff)
4872 need_flush = 1;
4873
4874 sector_nr = conf->reshape_progress;
4875 last = sector_nr | (conf->geo.chunk_mask
4876 & conf->prev.chunk_mask);
4877
Zhen Leie2873082020-08-21 09:29:18 +08004878 if (sector_nr + RESYNC_SECTORS <= last)
4879 last = sector_nr + RESYNC_SECTORS - 1;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004880 }
4881
4882 if (need_flush ||
4883 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4884 /* Need to update reshape_position in metadata */
Vishal Vermac9aa889b02021-12-21 20:06:21 +00004885 wait_barrier(conf, false);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004886 mddev->reshape_position = conf->reshape_progress;
4887 if (mddev->reshape_backwards)
4888 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4889 - conf->reshape_progress;
4890 else
4891 mddev->curr_resync_completed = conf->reshape_progress;
4892 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004893 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004894 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004895 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004896 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4897 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4898 allow_barrier(conf);
4899 return sectors_done;
4900 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004901 conf->reshape_safe = mddev->reshape_position;
4902 allow_barrier(conf);
4903 }
4904
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004905 raise_barrier(conf, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004906read_more:
4907 /* Now schedule reads for blocks from sector_nr to last */
Shaohua Li208410b2017-08-24 17:50:40 -07004908 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004909 r10_bio->state = 0;
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004910 raise_barrier(conf, 1);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004911 atomic_set(&r10_bio->remaining, 0);
4912 r10_bio->mddev = mddev;
4913 r10_bio->sector = sector_nr;
4914 set_bit(R10BIO_IsReshape, &r10_bio->state);
4915 r10_bio->sectors = last - sector_nr + 1;
4916 rdev = read_balance(conf, r10_bio, &max_sectors);
4917 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4918
4919 if (!rdev) {
4920 /* Cannot read from here, so need to record bad blocks
4921 * on all the target devices.
4922 */
4923 // FIXME
Kent Overstreetafeee512018-05-20 18:25:52 -04004924 mempool_free(r10_bio, &conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004925 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4926 return sectors_done;
4927 }
4928
Christoph Hellwig609be102022-01-24 10:11:03 +01004929 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ,
4930 GFP_KERNEL, &mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004931 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004932 + rdev->data_offset);
4933 read_bio->bi_private = r10_bio;
Ming Lei81fa1522017-03-17 00:12:32 +08004934 read_bio->bi_end_io = end_reshape_read;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004935 r10_bio->master_bio = read_bio;
4936 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4937
Guoqing Jiang7564bed2018-10-18 16:37:42 +08004938 /*
4939 * Broadcast RESYNC message to other nodes, so all nodes would not
4940 * write to the region to avoid conflict.
4941 */
4942 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4943 struct mdp_superblock_1 *sb = NULL;
4944 int sb_reshape_pos = 0;
4945
4946 conf->cluster_sync_low = sector_nr;
4947 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4948 sb = page_address(rdev->sb_page);
4949 if (sb) {
4950 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4951 /*
4952 * Set cluster_sync_low again if next address for array
4953 * reshape is less than cluster_sync_low. Since we can't
4954 * update cluster_sync_low until it has finished reshape.
4955 */
4956 if (sb_reshape_pos < conf->cluster_sync_low)
4957 conf->cluster_sync_low = sb_reshape_pos;
4958 }
4959
4960 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4961 conf->cluster_sync_high);
4962 }
4963
NeilBrown3ea7daa2012-05-22 13:53:47 +10004964 /* Now find the locations in the new layout */
4965 __raid10_find_phys(&conf->geo, r10_bio);
4966
4967 blist = read_bio;
4968 read_bio->bi_next = NULL;
4969
NeilBrownd094d682016-06-02 16:19:52 +10004970 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004971 for (s = 0; s < conf->copies*2; s++) {
4972 struct bio *b;
4973 int d = r10_bio->devs[s/2].devnum;
4974 struct md_rdev *rdev2;
4975 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004976 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004977 b = r10_bio->devs[s/2].repl_bio;
4978 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004979 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004980 b = r10_bio->devs[s/2].bio;
4981 }
4982 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4983 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004984
Christoph Hellwig74d46992017-08-23 19:10:32 +02004985 bio_set_dev(b, rdev2->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004986 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4987 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004988 b->bi_end_io = end_reshape_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01004989 b->bi_opf = REQ_OP_WRITE;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004990 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004991 blist = b;
4992 }
4993
4994 /* Now add as many pages as possible to all of these bios. */
4995
4996 nr_sectors = 0;
Ming Leif0250612017-03-17 00:12:33 +08004997 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004998 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
Ming Leif0250612017-03-17 00:12:33 +08004999 struct page *page = pages[s / (PAGE_SIZE >> 9)];
NeilBrown3ea7daa2012-05-22 13:53:47 +10005000 int len = (max_sectors - s) << 9;
5001 if (len > PAGE_SIZE)
5002 len = PAGE_SIZE;
5003 for (bio = blist; bio ; bio = bio->bi_next) {
Johannes Thumshirn0c67dd62023-05-31 04:50:38 -07005004 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
5005 bio->bi_status = BLK_STS_RESOURCE;
5006 bio_endio(bio);
5007 return sectors_done;
5008 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10005009 }
5010 sector_nr += len >> 9;
5011 nr_sectors += len >> 9;
5012 }
NeilBrownd094d682016-06-02 16:19:52 +10005013 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005014 r10_bio->sectors = nr_sectors;
5015
5016 /* Now submit the read */
Christoph Hellwig74d46992017-08-23 19:10:32 +02005017 md_sync_acct_bio(read_bio, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005018 atomic_inc(&r10_bio->remaining);
5019 read_bio->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005020 submit_bio_noacct(read_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005021 sectors_done += nr_sectors;
5022 if (sector_nr <= last)
5023 goto read_more;
5024
Xiao Ni1d0ffd22018-08-30 15:57:09 +08005025 lower_barrier(conf);
5026
NeilBrown3ea7daa2012-05-22 13:53:47 +10005027 /* Now that we have done the whole section we can
5028 * update reshape_progress
5029 */
5030 if (mddev->reshape_backwards)
5031 conf->reshape_progress -= sectors_done;
5032 else
5033 conf->reshape_progress += sectors_done;
5034
5035 return sectors_done;
5036}
5037
5038static void end_reshape_request(struct r10bio *r10_bio);
5039static int handle_reshape_read_error(struct mddev *mddev,
5040 struct r10bio *r10_bio);
5041static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
5042{
5043 /* Reshape read completed. Hopefully we have a block
5044 * to write out.
5045 * If we got a read error then we do sync 1-page reads from
5046 * elsewhere until we find the data - or give up.
5047 */
5048 struct r10conf *conf = mddev->private;
5049 int s;
5050
5051 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
5052 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
5053 /* Reshape has been aborted */
5054 md_done_sync(mddev, r10_bio->sectors, 0);
5055 return;
5056 }
5057
5058 /* We definitely have the data in the pages, schedule the
5059 * writes.
5060 */
5061 atomic_set(&r10_bio->remaining, 1);
5062 for (s = 0; s < conf->copies*2; s++) {
5063 struct bio *b;
5064 int d = r10_bio->devs[s/2].devnum;
5065 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10005066 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005067 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10005068 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005069 b = r10_bio->devs[s/2].repl_bio;
5070 } else {
NeilBrownd094d682016-06-02 16:19:52 +10005071 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005072 b = r10_bio->devs[s/2].bio;
5073 }
NeilBrownd094d682016-06-02 16:19:52 +10005074 if (!rdev || test_bit(Faulty, &rdev->flags)) {
5075 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005076 continue;
NeilBrownd094d682016-06-02 16:19:52 +10005077 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10005078 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10005079 rcu_read_unlock();
Christoph Hellwig74d46992017-08-23 19:10:32 +02005080 md_sync_acct_bio(b, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005081 atomic_inc(&r10_bio->remaining);
5082 b->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005083 submit_bio_noacct(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005084 }
5085 end_reshape_request(r10_bio);
5086}
5087
5088static void end_reshape(struct r10conf *conf)
5089{
5090 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5091 return;
5092
5093 spin_lock_irq(&conf->device_lock);
5094 conf->prev = conf->geo;
5095 md_finish_reshape(conf->mddev);
5096 smp_wmb();
5097 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10005098 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005099 spin_unlock_irq(&conf->device_lock);
5100
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +02005101 if (conf->mddev->queue)
Christoph Hellwig16ef5102020-09-24 08:51:33 +02005102 raid10_set_io_opt(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005103 conf->fullsync = 0;
5104}
5105
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005106static void raid10_update_reshape_pos(struct mddev *mddev)
5107{
5108 struct r10conf *conf = mddev->private;
Guoqing Jiang5ebaf802018-10-18 16:37:43 +08005109 sector_t lo, hi;
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005110
Guoqing Jiang5ebaf802018-10-18 16:37:43 +08005111 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5112 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5113 || mddev->reshape_position == MaxSector)
5114 conf->reshape_progress = mddev->reshape_position;
5115 else
5116 WARN_ON_ONCE(1);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005117}
5118
NeilBrown3ea7daa2012-05-22 13:53:47 +10005119static int handle_reshape_read_error(struct mddev *mddev,
5120 struct r10bio *r10_bio)
5121{
5122 /* Use sync reads to get the blocks from somewhere else */
5123 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005124 struct r10conf *conf = mddev->private;
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005125 struct r10bio *r10b;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005126 int slot = 0;
5127 int idx = 0;
Ming Lei2d06e3b2017-03-17 00:12:35 +08005128 struct page **pages;
5129
Gustavo A. R. Silva8cf05a72019-06-14 15:41:09 -07005130 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005131 if (!r10b) {
5132 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5133 return -ENOMEM;
5134 }
5135
Ming Lei2d06e3b2017-03-17 00:12:35 +08005136 /* reshape IOs share pages from .devs[0].bio */
5137 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005138
NeilBrowne0ee7782012-08-18 09:51:42 +10005139 r10b->sector = r10_bio->sector;
5140 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005141
5142 while (sectors) {
5143 int s = sectors;
5144 int success = 0;
5145 int first_slot = slot;
5146
5147 if (s > (PAGE_SIZE >> 9))
5148 s = PAGE_SIZE >> 9;
5149
NeilBrownd094d682016-06-02 16:19:52 +10005150 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005151 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10005152 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10005153 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005154 sector_t addr;
5155 if (rdev == NULL ||
5156 test_bit(Faulty, &rdev->flags) ||
5157 !test_bit(In_sync, &rdev->flags))
5158 goto failed;
5159
NeilBrowne0ee7782012-08-18 09:51:42 +10005160 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10005161 atomic_inc(&rdev->nr_pending);
5162 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005163 success = sync_page_io(rdev,
5164 addr,
5165 s << 9,
Ming Lei2d06e3b2017-03-17 00:12:35 +08005166 pages[idx],
Bart Van Assche4ce4c732022-07-14 11:06:57 -07005167 REQ_OP_READ, false);
NeilBrownd094d682016-06-02 16:19:52 +10005168 rdev_dec_pending(rdev, mddev);
5169 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005170 if (success)
5171 break;
5172 failed:
5173 slot++;
5174 if (slot >= conf->copies)
5175 slot = 0;
5176 if (slot == first_slot)
5177 break;
5178 }
NeilBrownd094d682016-06-02 16:19:52 +10005179 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005180 if (!success) {
5181 /* couldn't read this block, must give up */
5182 set_bit(MD_RECOVERY_INTR,
5183 &mddev->recovery);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005184 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005185 return -EIO;
5186 }
5187 sectors -= s;
5188 idx++;
5189 }
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005190 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005191 return 0;
5192}
5193
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005194static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10005195{
Ming Leif0250612017-03-17 00:12:33 +08005196 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005197 struct mddev *mddev = r10_bio->mddev;
5198 struct r10conf *conf = mddev->private;
5199 int d;
5200 int slot;
5201 int repl;
5202 struct md_rdev *rdev = NULL;
5203
5204 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5205 if (repl)
5206 rdev = conf->mirrors[d].replacement;
5207 if (!rdev) {
5208 smp_mb();
5209 rdev = conf->mirrors[d].rdev;
5210 }
5211
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005212 if (bio->bi_status) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10005213 /* FIXME should record badblock */
5214 md_error(mddev, rdev);
5215 }
5216
5217 rdev_dec_pending(rdev, mddev);
5218 end_reshape_request(r10_bio);
5219}
5220
5221static void end_reshape_request(struct r10bio *r10_bio)
5222{
5223 if (!atomic_dec_and_test(&r10_bio->remaining))
5224 return;
5225 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5226 bio_put(r10_bio->master_bio);
5227 put_buf(r10_bio);
5228}
5229
5230static void raid10_finish_reshape(struct mddev *mddev)
5231{
5232 struct r10conf *conf = mddev->private;
5233
5234 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5235 return;
5236
5237 if (mddev->delta_disks > 0) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10005238 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5239 mddev->recovery_cp = mddev->resync_max_sectors;
5240 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5241 }
BingJing Chang88763912018-02-22 13:34:46 +08005242 mddev->resync_max_sectors = mddev->array_sectors;
NeilBrown63aced62012-05-22 13:55:33 +10005243 } else {
5244 int d;
NeilBrownd094d682016-06-02 16:19:52 +10005245 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10005246 for (d = conf->geo.raid_disks ;
5247 d < conf->geo.raid_disks - mddev->delta_disks;
5248 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10005249 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10005250 if (rdev)
5251 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10005252 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10005253 if (rdev)
5254 clear_bit(In_sync, &rdev->flags);
5255 }
NeilBrownd094d682016-06-02 16:19:52 +10005256 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005257 }
5258 mddev->layout = mddev->new_layout;
5259 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5260 mddev->reshape_position = MaxSector;
5261 mddev->delta_disks = 0;
5262 mddev->reshape_backwards = 0;
5263}
5264
NeilBrown84fc4b52011-10-11 16:49:58 +11005265static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08005268 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08005270 .make_request = raid10_make_request,
5271 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11005272 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08005273 .status = raid10_status,
5274 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 .hot_add_disk = raid10_add_disk,
5276 .hot_remove_disk= raid10_remove_disk,
5277 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08005278 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08005279 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005280 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11005281 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11005282 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10005283 .check_reshape = raid10_check_reshape,
5284 .start_reshape = raid10_start_reshape,
5285 .finish_reshape = raid10_finish_reshape,
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005286 .update_reshape_pos = raid10_update_reshape_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287};
5288
5289static int __init raid_init(void)
5290{
NeilBrown2604b702006-01-06 00:20:36 -08005291 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292}
5293
5294static void raid_exit(void)
5295{
NeilBrown2604b702006-01-06 00:20:36 -08005296 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297}
5298
5299module_init(raid_init);
5300module_exit(raid_exit);
5301MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005302MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005304MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08005305MODULE_ALIAS("md-level-10");