blob: a4556d2e46bf95f3bb8020941a00119c050cf662 [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"
Li Nan1979dbb2023-12-15 10:38:51 +080022
23#define RAID_1_10_NAME "raid10"
Christoph Hellwigef740c32009-03-31 14:27:03 +110024#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110025#include "raid0.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040026#include "md-bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * RAID10 provides a combination of RAID0 and RAID1 functionality.
30 * The layout of data is defined by
31 * chunk_size
32 * raid_disks
33 * near_copies (stored in low byte of layout)
34 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070035 * far_offset (stored in bit 16 of layout )
Jonathan Brassow475901a2013-02-21 13:28:10 +110036 * use_far_sets (stored in bit 17 of layout )
NeilBrown8bce6d32015-10-22 13:20:15 +110037 * use_far_sets_bugfixed (stored in bit 18 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 *
Jonathan Brassow475901a2013-02-21 13:28:10 +110039 * The data to be stored is divided into chunks using chunksize. Each device
40 * is divided into far_copies sections. In each section, chunks are laid out
41 * in a style similar to raid0, but near_copies copies of each chunk is stored
42 * (each on a different drive). The starting device for each section is offset
43 * near_copies from the starting device of the previous section. Thus there
44 * are (near_copies * far_copies) of each chunk, and each is on a different
45 * drive. near_copies and far_copies must be at least one, and their product
46 * is at most raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070047 *
48 * If far_offset is true, then the far_copies are handled a bit differently.
Jonathan Brassow475901a2013-02-21 13:28:10 +110049 * The copies are still in different stripes, but instead of being very far
50 * apart on disk, there are adjacent stripes.
51 *
52 * The far and offset algorithms are handled slightly differently if
53 * 'use_far_sets' is true. In this case, the array's devices are grouped into
54 * sets that are (near_copies * far_copies) in size. The far copied stripes
55 * are still shifted by 'near_copies' devices, but this shifting stays confined
56 * to the set rather than the entire array. This is done to improve the number
57 * of device combinations that can fail without causing the array to fail.
58 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
59 * on a device):
60 * A B C D A B C D E
61 * ... ...
62 * D A B C E A B C D
63 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
64 * [A B] [C D] [A B] [C D E]
65 * |...| |...| |...| | ... |
66 * [B A] [D C] [B A] [E C D]
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68
NeilBrowne879a872011-10-11 16:49:02 +110069static void allow_barrier(struct r10conf *conf);
70static void lower_barrier(struct r10conf *conf);
NeilBrown635f6412013-06-11 14:57:09 +100071static int _enough(struct r10conf *conf, int previous, int ignore);
NeilBrown1919cbb2016-11-18 16:16:12 +110072static int enough(struct r10conf *conf, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +100073static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
74 int *skipped);
75static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020076static void end_reshape_write(struct bio *bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +100077static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080078
Ming Leifb0eb5d2017-07-14 16:14:43 +080079#include "raid1-10.c"
80
Yu Kuaib9b083f2022-09-16 19:34:28 +080081#define NULL_CMD
82#define cmd_before(conf, cmd) \
83 do { \
84 write_sequnlock_irq(&(conf)->resync_lock); \
85 cmd; \
86 } while (0)
87#define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock)
88
89#define wait_event_barrier_cmd(conf, cond, cmd) \
90 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \
91 cmd_after(conf))
92
93#define wait_event_barrier(conf, cond) \
94 wait_event_barrier_cmd(conf, cond, NULL_CMD)
95
Ming Leif0250612017-03-17 00:12:33 +080096/*
Ming Leif0250612017-03-17 00:12:33 +080097 * for resync bio, r10bio pointer can be retrieved from the per-bio
98 * 'struct resync_pages'.
99 */
100static inline struct r10bio *get_resync_r10bio(struct bio *bio)
101{
102 return get_resync_pages(bio)->raid_bio;
103}
104
Al Virodd0fc662005-10-07 07:46:04 +0100105static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
NeilBrowne879a872011-10-11 16:49:02 +1100107 struct r10conf *conf = data;
Xiao Nic2968282021-02-04 15:50:44 +0800108 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
NeilBrown69335ef2011-12-23 10:17:54 +1100110 /* allocate a r10bio with room for raid_disks entries in the
111 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100112 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Guoqing Jiang8db87912017-10-24 15:11:52 +0800115#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
NeilBrown0310fa22008-08-05 15:54:14 +1000116/* amount of memory to reserve for resync requests */
117#define RESYNC_WINDOW (1024*1024)
118/* maximum number of concurrent requests, memory permitting */
119#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Guoqing Jiang4b242e92018-01-19 11:37:56 +0800120#define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
Guoqing Jiang8db87912017-10-24 15:11:52 +0800121#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * When performing a resync, we need to read and compare, so
125 * we need as many pages are there are copies.
126 * When performing a recovery, we need 2 bios, one for read,
127 * one for write (we recover only one drive per r10buf)
128 *
129 */
Al Virodd0fc662005-10-07 07:46:04 +0100130static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
NeilBrowne879a872011-10-11 16:49:02 +1100132 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100133 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct bio *bio;
Ming Leif0250612017-03-17 00:12:33 +0800135 int j;
136 int nalloc, nalloc_rp;
137 struct resync_pages *rps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100140 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
NeilBrown3ea7daa2012-05-22 13:53:47 +1000143 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
144 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 nalloc = conf->copies; /* resync */
146 else
147 nalloc = 2; /* recovery */
148
Ming Leif0250612017-03-17 00:12:33 +0800149 /* allocate once for all bios */
150 if (!conf->have_replacement)
151 nalloc_rp = nalloc;
152 else
153 nalloc_rp = nalloc * 2;
Kees Cook6da2ec52018-06-12 13:55:00 -0700154 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
Ming Leif0250612017-03-17 00:12:33 +0800155 if (!rps)
156 goto out_free_r10bio;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * Allocate bios.
160 */
161 for (j = nalloc ; j-- ; ) {
Christoph Hellwig066ff572022-04-06 08:12:27 +0200162 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!bio)
164 goto out_free_bio;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200165 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100167 if (!conf->have_replacement)
168 continue;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200169 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
NeilBrown69335ef2011-12-23 10:17:54 +1100170 if (!bio)
171 goto out_free_bio;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200172 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
NeilBrown69335ef2011-12-23 10:17:54 +1100173 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 /*
176 * Allocate RESYNC_PAGES data pages and attach them
177 * where needed.
178 */
Ming Leif0250612017-03-17 00:12:33 +0800179 for (j = 0; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100180 struct bio *rbio = r10_bio->devs[j].repl_bio;
Ming Leif0250612017-03-17 00:12:33 +0800181 struct resync_pages *rp, *rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Ming Leif0250612017-03-17 00:12:33 +0800183 rp = &rps[j];
184 if (rbio)
185 rp_repl = &rps[nalloc + j];
186
187 bio = r10_bio->devs[j].bio;
188
189 if (!j || test_bit(MD_RECOVERY_SYNC,
190 &conf->mddev->recovery)) {
191 if (resync_alloc_pages(rp, gfp_flags))
192 goto out_free_pages;
193 } else {
194 memcpy(rp, &rps[0], sizeof(*rp));
195 resync_get_all_pages(rp);
196 }
197
Ming Leif0250612017-03-17 00:12:33 +0800198 rp->raid_bio = r10_bio;
199 bio->bi_private = rp;
200 if (rbio) {
201 memcpy(rp_repl, rp, sizeof(*rp));
202 rbio->bi_private = rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 }
205
206 return r10_bio;
207
208out_free_pages:
Ming Leif0250612017-03-17 00:12:33 +0800209 while (--j >= 0)
John Pittman45422b72019-11-11 16:43:20 -0800210 resync_free_pages(&rps[j]);
Ming Leif0250612017-03-17 00:12:33 +0800211
majianpeng5fdd2cf2012-05-22 13:55:03 +1000212 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000214 for ( ; j < nalloc; j++) {
215 if (r10_bio->devs[j].bio)
Christoph Hellwig066ff572022-04-06 08:12:27 +0200216 bio_uninit(r10_bio->devs[j].bio);
217 kfree(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100218 if (r10_bio->devs[j].repl_bio)
Christoph Hellwig066ff572022-04-06 08:12:27 +0200219 bio_uninit(r10_bio->devs[j].repl_bio);
220 kfree(r10_bio->devs[j].repl_bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100221 }
Ming Leif0250612017-03-17 00:12:33 +0800222 kfree(rps);
223out_free_r10bio:
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700224 rbio_pool_free(r10_bio, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return NULL;
226}
227
228static void r10buf_pool_free(void *__r10_bio, void *data)
229{
NeilBrowne879a872011-10-11 16:49:02 +1100230 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100231 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int j;
Ming Leif0250612017-03-17 00:12:33 +0800233 struct resync_pages *rp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Ming Leif0250612017-03-17 00:12:33 +0800235 for (j = conf->copies; j--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct bio *bio = r10bio->devs[j].bio;
Ming Leif0250612017-03-17 00:12:33 +0800237
Guoqing Jiangeb81b322018-04-26 10:56:37 +0800238 if (bio) {
239 rp = get_resync_pages(bio);
240 resync_free_pages(rp);
Christoph Hellwig066ff572022-04-06 08:12:27 +0200241 bio_uninit(bio);
242 kfree(bio);
Guoqing Jiangeb81b322018-04-26 10:56:37 +0800243 }
Ming Leif0250612017-03-17 00:12:33 +0800244
NeilBrown69335ef2011-12-23 10:17:54 +1100245 bio = r10bio->devs[j].repl_bio;
Christoph Hellwig066ff572022-04-06 08:12:27 +0200246 if (bio) {
247 bio_uninit(bio);
248 kfree(bio);
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Ming Leif0250612017-03-17 00:12:33 +0800251
252 /* resync pages array stored in the 1st bio's .bi_private */
253 kfree(rp);
254
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700255 rbio_pool_free(r10bio, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
NeilBrowne879a872011-10-11 16:49:02 +1100258static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 int i;
261
Xiao Nic2968282021-02-04 15:50:44 +0800262 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000264 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 bio_put(*bio);
266 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100267 bio = &r10_bio->devs[i].repl_bio;
268 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
269 bio_put(*bio);
270 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272}
273
NeilBrown9f2c9d12011-10-11 16:48:43 +1100274static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
NeilBrowne879a872011-10-11 16:49:02 +1100276 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 put_all_bios(conf, r10_bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400279 mempool_free(r10_bio, &conf->r10bio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
NeilBrown9f2c9d12011-10-11 16:48:43 +1100282static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
NeilBrowne879a872011-10-11 16:49:02 +1100284 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Kent Overstreetafeee512018-05-20 18:25:52 -0400286 mempool_free(r10_bio, &conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
NeilBrown0a27ec92006-01-06 00:20:13 -0800288 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Yu Kuai0c0be98b2022-09-16 19:34:26 +0800291static void wake_up_barrier(struct r10conf *conf)
292{
293 if (wq_has_sleeper(&conf->wait_barrier))
294 wake_up(&conf->wait_barrier);
295}
296
NeilBrown9f2c9d12011-10-11 16:48:43 +1100297static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100300 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100301 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 spin_lock_irqsave(&conf->device_lock, flags);
304 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800305 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 spin_unlock_irqrestore(&conf->device_lock, flags);
307
Arthur Jones388667b2008-07-25 12:03:38 -0700308 /* wake up frozen array... */
309 wake_up(&conf->wait_barrier);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 md_wakeup_thread(mddev->thread);
312}
313
314/*
315 * raid_end_bio_io() is called when we have finished servicing a mirrored
316 * operation and are ready to return a success/failure code to the buffer
317 * cache layer.
318 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100319static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100322 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
NeilBrown856e08e2011-07-28 11:39:23 +1000324 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200325 bio->bi_status = BLK_STS_IOERR;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100326
327 bio_endio(bio);
328 /*
329 * Wake up any possible resync thread that waits for the device
330 * to go idle.
331 */
332 allow_barrier(conf);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 free_r10bio(r10_bio);
335}
336
337/*
338 * Update disk head position estimator based on IRQ completion info.
339 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100340static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
NeilBrowne879a872011-10-11 16:49:02 +1100342 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
345 r10_bio->devs[slot].addr + (r10_bio->sectors);
346}
347
Namhyung Kim778ca012011-07-18 17:38:47 +1000348/*
349 * Find the disk number which triggered given bio
350 */
NeilBrowne879a872011-10-11 16:49:02 +1100351static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100352 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000353{
354 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100355 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000356
Xiao Nic2968282021-02-04 15:50:44 +0800357 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000358 if (r10_bio->devs[slot].bio == bio)
359 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100360 if (r10_bio->devs[slot].repl_bio == bio) {
361 repl = 1;
362 break;
363 }
364 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000365
Namhyung Kim778ca012011-07-18 17:38:47 +1000366 update_head_pos(slot, r10_bio);
367
NeilBrown749c55e2011-07-28 11:39:24 +1000368 if (slotp)
369 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100370 if (replp)
371 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000372 return r10_bio->devs[slot].devnum;
373}
374
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200375static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200377 int uptodate = !bio->bi_status;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100378 struct r10bio *r10_bio = bio->bi_private;
Colin Ian Kinga0e764c2017-10-11 11:46:54 +0100379 int slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100380 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100381 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 slot = r10_bio->read_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100384 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /*
386 * this branch is our 'one mirror IO has finished' event handler:
387 */
NeilBrown4443ae12006-01-06 00:20:28 -0800388 update_head_pos(slot, r10_bio);
389
390 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * Set R10BIO_Uptodate in our master bio, so that
393 * we will return a good error code to the higher
394 * levels even if IO on some other mirrored buffer fails.
395 *
396 * The 'master' represents the composite IO operation to
397 * user-side. So if something waits for IO, then it will
398 * wait for the 'master' bio.
399 */
400 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100401 } else {
402 /* If all other devices that store this block have
403 * failed, we want to return the error upwards rather
404 * than fail the last device. Here we redefine
405 * "uptodate" to mean "Don't want to retry"
406 */
NeilBrown635f6412013-06-11 14:57:09 +1000407 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
408 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100409 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100410 }
411 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100413 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800414 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000416 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Christoph Hellwig913cce52022-05-12 08:19:13 +0200418 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000419 mdname(conf->mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +0200420 rdev->bdev,
Christian Dietrich8bda4702011-07-27 11:00:36 +1000421 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000422 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 reschedule_retry(r10_bio);
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
NeilBrown9f2c9d12011-10-11 16:48:43 +1100427static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000428{
429 /* clear the bitmap if all writes complete successfully */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700430 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
431 r10_bio->sectors,
432 !test_bit(R10BIO_Degraded, &r10_bio->state),
433 0);
NeilBrownbd870a12011-07-28 11:39:24 +1000434 md_write_end(r10_bio->mddev);
435}
436
NeilBrown9f2c9d12011-10-11 16:48:43 +1100437static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000438{
439 if (atomic_dec_and_test(&r10_bio->remaining)) {
440 if (test_bit(R10BIO_WriteError, &r10_bio->state))
441 reschedule_retry(r10_bio);
442 else {
443 close_write(r10_bio);
444 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
445 reschedule_retry(r10_bio);
446 else
447 raid_end_bio_io(r10_bio);
448 }
449 }
450}
451
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200452static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100454 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000455 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000456 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100457 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100458 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100459 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100460 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700461 bool discard_error;
462
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200463 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
NeilBrown475b0322011-12-23 10:17:55 +1100465 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
NeilBrown475b0322011-12-23 10:17:55 +1100467 if (repl)
468 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100469 if (!rdev) {
470 smp_rmb();
471 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100472 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * this branch is our 'one mirror IO has finished' event handler:
476 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200477 if (bio->bi_status && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100478 if (repl)
479 /* Never record new bad blocks to replacement,
480 * just fail it.
481 */
482 md_error(rdev->mddev, rdev);
483 else {
484 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100485 if (!test_and_set_bit(WantReplacement, &rdev->flags))
486 set_bit(MD_RECOVERY_NEEDED,
487 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100488
NeilBrown475b0322011-12-23 10:17:55 +1100489 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100490 if (test_bit(FailFast, &rdev->flags) &&
491 (bio->bi_opf & MD_FAILFAST)) {
492 md_error(rdev->mddev, rdev);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800493 }
494
495 /*
496 * When the device is faulty, it is not necessary to
497 * handle write error.
Yufen Yu7cee6d42019-07-19 13:48:47 +0800498 */
499 if (!test_bit(Faulty, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +1100500 set_bit(R10BIO_WriteError, &r10_bio->state);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800501 else {
Wei Shuyu5ba03932021-06-28 15:15:08 +0800502 /* Fail the request */
503 set_bit(R10BIO_Degraded, &r10_bio->state);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800504 r10_bio->devs[slot].bio = NULL;
505 to_put = bio;
506 dec_rdev = 1;
507 }
NeilBrown475b0322011-12-23 10:17:55 +1100508 }
NeilBrown749c55e2011-07-28 11:39:24 +1000509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /*
511 * Set R10BIO_Uptodate in our master bio, so that
512 * we will return a good error code for to the higher
513 * levels even if IO on some other mirrored buffer fails.
514 *
515 * The 'master' represents the composite IO operation to
516 * user-side. So if something waits for IO, then it will
517 * wait for the 'master' bio.
Yu Kuai3a0f0072024-02-29 17:57:04 +0800518 *
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300519 * Do not set R10BIO_Uptodate if the current device is
520 * rebuilding or Faulty. This is because we cannot use
521 * such device for properly reading the data back (we could
522 * potentially use it, if the current write would have felt
523 * before rdev->recovery_offset, but for simplicity we don't
524 * check this here.
525 */
526 if (test_bit(In_sync, &rdev->flags) &&
527 !test_bit(Faulty, &rdev->flags))
528 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
NeilBrown749c55e2011-07-28 11:39:24 +1000530 /* Maybe we can clear some bad blocks. */
Yu Kuai3a0f0072024-02-29 17:57:04 +0800531 if (rdev_has_badblock(rdev, r10_bio->devs[slot].addr,
532 r10_bio->sectors) &&
533 !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000534 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100535 if (repl)
536 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
537 else
538 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000539 dec_rdev = 0;
540 set_bit(R10BIO_MadeGood, &r10_bio->state);
541 }
542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /*
545 *
546 * Let's see if all mirrored write operations have finished
547 * already.
548 */
NeilBrown19d5f832011-09-10 17:21:17 +1000549 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000550 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100551 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100552 if (to_put)
553 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
557 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300558 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * parameters: near_copies and far_copies.
560 * near_copies * far_copies must be <= raid_disks.
561 * Normally one of these will be 1.
562 * If both are 1, we get raid0.
563 * If near_copies == raid_disks, we get raid1.
564 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300565 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * first chunk, followed by near_copies copies of the next chunk and
567 * so on.
568 * If far_copies > 1, then after 1/far_copies of the array has been assigned
569 * as described above, we start again with a device offset of near_copies.
570 * So we effectively have another copy of the whole array further down all
571 * the drives, but with blocks on different drives.
572 * With this layout, and block is never stored twice on the one device.
573 *
574 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700575 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * raid10_find_virt does the reverse mapping, from a device and a
578 * sector offset to a virtual address
579 */
580
NeilBrownf8c9e742012-05-21 09:28:33 +1000581static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 int n,f;
584 sector_t sector;
585 sector_t chunk;
586 sector_t stripe;
587 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100589 int last_far_set_start, last_far_set_size;
590
591 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
592 last_far_set_start *= geo->far_set_size;
593
594 last_far_set_size = geo->far_set_size;
595 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000598 chunk = r10bio->sector >> geo->chunk_shift;
599 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
NeilBrown5cf00fc2012-05-21 09:28:20 +1000601 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000603 dev = sector_div(stripe, geo->raid_disks);
604 if (geo->far_offset)
605 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
NeilBrown5cf00fc2012-05-21 09:28:20 +1000607 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000610 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100612 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100615 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 slot++;
617
NeilBrown5cf00fc2012-05-21 09:28:20 +1000618 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100619 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000620 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100621
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100622 if ((geo->raid_disks % geo->far_set_size) &&
623 (d > last_far_set_start)) {
624 d -= last_far_set_start;
625 d %= last_far_set_size;
626 d += last_far_set_start;
627 } else {
628 d %= geo->far_set_size;
629 d += geo->far_set_size * set;
630 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000631 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 r10bio->devs[slot].devnum = d;
633 r10bio->devs[slot].addr = s;
634 slot++;
635 }
636 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000637 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000639 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000642}
643
644static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
645{
646 struct geom *geo = &conf->geo;
647
648 if (conf->reshape_progress != MaxSector &&
649 ((r10bio->sector >= conf->reshape_progress) !=
650 conf->mddev->reshape_backwards)) {
651 set_bit(R10BIO_Previous, &r10bio->state);
652 geo = &conf->prev;
653 } else
654 clear_bit(R10BIO_Previous, &r10bio->state);
655
656 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
NeilBrowne879a872011-10-11 16:49:02 +1100659static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000662 /* Never use conf->prev as this is only called during resync
663 * or recovery, so reshape isn't happening
664 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000665 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100666 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
667 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100668 int last_far_set_start;
669
670 if (geo->raid_disks % geo->far_set_size) {
671 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
672 last_far_set_start *= geo->far_set_size;
673
674 if (dev >= last_far_set_start) {
675 far_set_size = geo->far_set_size;
676 far_set_size += (geo->raid_disks % geo->far_set_size);
677 far_set_start = last_far_set_start;
678 }
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
NeilBrown5cf00fc2012-05-21 09:28:20 +1000681 offset = sector & geo->chunk_mask;
682 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700683 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000684 chunk = sector >> geo->chunk_shift;
685 fc = sector_div(chunk, geo->far_copies);
686 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100687 if (dev < far_set_start)
688 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700689 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000690 while (sector >= geo->stride) {
691 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100692 if (dev < (geo->near_copies + far_set_start))
693 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700694 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000695 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700696 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000697 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700698 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000699 vchunk = chunk * geo->raid_disks + dev;
700 sector_div(vchunk, geo->near_copies);
701 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * This routine returns the disk from which the requested read should
706 * be done. There is a per-array 'next expected sequential IO' sector
707 * number - if this matches on the next IO then we use the last disk.
708 * There is also a per-disk 'last know head position' sector that is
709 * maintained from IRQ contexts, both the normal and the resync IO
710 * completion handlers update this position correctly. If there is no
711 * perfect sequential match then we pick the disk whose head is closest.
712 *
713 * If there are 2 mirrors in the same 2 devices, performance degrades
714 * because position is mirror, not device based.
715 *
716 * The rdev for the device selected will have nr_pending incremented.
717 */
718
719/*
720 * FIXME: possibly should rethink readbalancing and do it differently
721 * depending on near_copies / far_copies geometry.
722 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100723static struct md_rdev *read_balance(struct r10conf *conf,
724 struct r10bio *r10_bio,
725 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000727 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000728 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000729 int sectors = r10_bio->sectors;
730 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000731 sector_t new_distance, best_dist;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700732 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000733 int do_balance;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700734 int best_dist_slot, best_pending_slot;
735 bool has_nonrot_disk = false;
736 unsigned int min_pending;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000737 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 raid10_find_phys(conf, r10_bio);
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700740 best_dist_slot = -1;
741 min_pending = UINT_MAX;
742 best_dist_rdev = NULL;
743 best_pending_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000744 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000745 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000746 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100747 clear_bit(R10BIO_FailFast, &r10_bio->state);
Yu Kuaif1092072024-02-29 17:57:09 +0800748
749 if (raid1_should_read_first(conf->mddev, this_sector, sectors))
NeilBrown56d99122011-05-11 14:27:03 +1000750 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
NeilBrown56d99122011-05-11 14:27:03 +1000752 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000753 sector_t first_bad;
754 int bad_sectors;
755 sector_t dev_sector;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700756 unsigned int pending;
757 bool nonrot;
NeilBrown856e08e2011-07-28 11:39:23 +1000758
NeilBrown56d99122011-05-11 14:27:03 +1000759 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000761 disk = r10_bio->devs[slot].devnum;
Yu Kuaia448af22023-11-25 16:16:01 +0800762 rdev = conf->mirrors[disk].replacement;
NeilBrownabbf0982011-12-23 10:17:54 +1100763 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
Li Nan2ae6aaf2023-06-02 17:18:39 +0800764 r10_bio->devs[slot].addr + sectors >
Yu Kuaia448af22023-11-25 16:16:01 +0800765 rdev->recovery_offset)
766 rdev = conf->mirrors[disk].rdev;
NeilBrown050b6612012-03-19 12:46:39 +1100767 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700768 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100769 continue;
770 if (!test_bit(In_sync, &rdev->flags) &&
771 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000772 continue;
773
NeilBrown856e08e2011-07-28 11:39:23 +1000774 dev_sector = r10_bio->devs[slot].addr;
775 if (is_badblock(rdev, dev_sector, sectors,
776 &first_bad, &bad_sectors)) {
777 if (best_dist < MaxSector)
778 /* Already have a better slot */
779 continue;
780 if (first_bad <= dev_sector) {
781 /* Cannot read here. If this is the
782 * 'primary' device, then we must not read
783 * beyond 'bad_sectors' from another device.
784 */
785 bad_sectors -= (dev_sector - first_bad);
786 if (!do_balance && sectors > bad_sectors)
787 sectors = bad_sectors;
788 if (best_good_sectors > sectors)
789 best_good_sectors = sectors;
790 } else {
791 sector_t good_sectors =
792 first_bad - dev_sector;
793 if (good_sectors > best_good_sectors) {
794 best_good_sectors = good_sectors;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700795 best_dist_slot = slot;
796 best_dist_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000797 }
798 if (!do_balance)
799 /* Must read from here */
800 break;
801 }
802 continue;
803 } else
804 best_good_sectors = sectors;
805
NeilBrown56d99122011-05-11 14:27:03 +1000806 if (!do_balance)
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Hellwig10f0d2a52022-04-15 06:52:42 +0200809 nonrot = bdev_nonrot(rdev->bdev);
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700810 has_nonrot_disk |= nonrot;
811 pending = atomic_read(&rdev->nr_pending);
812 if (min_pending > pending && nonrot) {
813 min_pending = pending;
814 best_pending_slot = slot;
815 best_pending_rdev = rdev;
816 }
817
818 if (best_dist_slot >= 0)
NeilBrown8d3ca832016-11-18 16:16:12 +1100819 /* At least 2 disks to choose from so failfast is OK */
820 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800821 /* This optimisation is debatable, and completely destroys
822 * sequential read speed for 'far copies' arrays. So only
823 * keep it for 'near' arrays, and review those later.
824 */
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700825 if (geo->near_copies > 1 && !pending)
NeilBrown8d3ca832016-11-18 16:16:12 +1100826 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800827
828 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100829 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000830 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800831 else
NeilBrown56d99122011-05-11 14:27:03 +1000832 new_distance = abs(r10_bio->devs[slot].addr -
833 conf->mirrors[disk].head_position);
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700834
NeilBrown56d99122011-05-11 14:27:03 +1000835 if (new_distance < best_dist) {
836 best_dist = new_distance;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700837 best_dist_slot = slot;
838 best_dist_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 }
NeilBrownabbf0982011-12-23 10:17:54 +1100841 if (slot >= conf->copies) {
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700842 if (has_nonrot_disk) {
843 slot = best_pending_slot;
844 rdev = best_pending_rdev;
845 } else {
846 slot = best_dist_slot;
847 rdev = best_dist_rdev;
848 }
NeilBrownabbf0982011-12-23 10:17:54 +1100849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
NeilBrown56d99122011-05-11 14:27:03 +1000851 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000852 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000853 r10_bio->read_slot = slot;
854 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100855 rdev = NULL;
NeilBrown856e08e2011-07-28 11:39:23 +1000856 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
NeilBrown96c3fd12011-12-23 10:17:54 +1100858 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
NeilBrowne879a872011-10-11 16:49:02 +1100861static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800862{
863 /* Any writes that have been queued but are awaiting
864 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800865 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800866 spin_lock_irq(&conf->device_lock);
867
868 if (conf->pending_bio_list.head) {
Shaohua Li18022a12017-12-01 12:12:34 -0800869 struct blk_plug plug;
NeilBrowna35e63e2008-03-04 14:29:29 -0800870 struct bio *bio;
Shaohua Li18022a12017-12-01 12:12:34 -0800871
NeilBrowna35e63e2008-03-04 14:29:29 -0800872 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800873 spin_unlock_irq(&conf->device_lock);
NeilBrown474beb52017-12-04 08:21:04 +1100874
875 /*
876 * As this is called in a wait_event() loop (see freeze_array),
877 * current->state might be TASK_UNINTERRUPTIBLE which will
878 * cause a warning when we prepare to wait again. As it is
879 * rare that this path is taken, it is perfectly safe to force
880 * us to go around the wait_event() loop again, so the warning
881 * is a false-positive. Silence the warning by resetting
882 * thread state
883 */
884 __set_current_state(TASK_RUNNING);
885
Shaohua Li18022a12017-12-01 12:12:34 -0800886 blk_start_plug(&plug);
Yu Kuai9efcc2c2023-05-29 21:11:05 +0800887 raid1_prepare_flush_writes(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100888 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800889
890 while (bio) { /* submit pending writes */
891 struct bio *next = bio->bi_next;
Yu Kuai8295efb2023-05-29 21:11:02 +0800892
893 raid1_submit_write(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800894 bio = next;
Yu Kuai01044462023-05-29 21:11:00 +0800895 cond_resched();
NeilBrowna35e63e2008-03-04 14:29:29 -0800896 }
Shaohua Li18022a12017-12-01 12:12:34 -0800897 blk_finish_plug(&plug);
NeilBrowna35e63e2008-03-04 14:29:29 -0800898 } else
899 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800900}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100901
NeilBrown0a27ec92006-01-06 00:20:13 -0800902/* Barriers....
903 * Sometimes we need to suspend IO while we do something else,
904 * either some resync/recovery, or reconfigure the array.
905 * To do this we raise a 'barrier'.
906 * The 'barrier' is a counter that can be raised multiple times
907 * to count how many activities are happening which preclude
908 * normal IO.
909 * We can only raise the barrier if there is no pending IO.
910 * i.e. if nr_pending == 0.
911 * We choose only to raise the barrier if no-one is waiting for the
912 * barrier to go down. This means that as soon as an IO request
913 * is ready, no other operations which require a barrier will start
914 * until the IO request has had a chance.
915 *
916 * So: regular IO calls 'wait_barrier'. When that returns there
917 * is no backgroup IO happening, It must arrange to call
918 * allow_barrier when it has finished its IO.
919 * backgroup IO calls must call raise_barrier. Once that returns
920 * there is no normal IO happeing. It must arrange to call
921 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
NeilBrowne879a872011-10-11 16:49:02 +1100924static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Yu Kuaib9b083f2022-09-16 19:34:28 +0800926 write_seqlock_irq(&conf->resync_lock);
Yu Kuai9fdfe6d2023-03-10 15:38:52 +0800927
928 if (WARN_ON_ONCE(force && !conf->barrier))
929 force = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
NeilBrown6cce3b22006-01-06 00:20:16 -0800931 /* Wait until no block IO is waiting (unless 'force') */
Yu Kuaib9b083f2022-09-16 19:34:28 +0800932 wait_event_barrier(conf, force || !conf->nr_waiting);
NeilBrown0a27ec92006-01-06 00:20:13 -0800933
934 /* block any new IO from starting */
Yu Kuaib9b083f2022-09-16 19:34:28 +0800935 WRITE_ONCE(conf->barrier, conf->barrier + 1);
NeilBrown0a27ec92006-01-06 00:20:13 -0800936
NeilBrownc3b328a2011-04-18 18:25:43 +1000937 /* Now wait for all pending IO to complete */
Yu Kuaib9b083f2022-09-16 19:34:28 +0800938 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) &&
939 conf->barrier < RESYNC_DEPTH);
NeilBrown0a27ec92006-01-06 00:20:13 -0800940
Yu Kuaib9b083f2022-09-16 19:34:28 +0800941 write_sequnlock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
NeilBrowne879a872011-10-11 16:49:02 +1100944static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800945{
946 unsigned long flags;
Yu Kuaib9b083f2022-09-16 19:34:28 +0800947
948 write_seqlock_irqsave(&conf->resync_lock, flags);
949 WRITE_ONCE(conf->barrier, conf->barrier - 1);
950 write_sequnlock_irqrestore(&conf->resync_lock, flags);
NeilBrown0a27ec92006-01-06 00:20:13 -0800951 wake_up(&conf->wait_barrier);
952}
953
Yu Kuaied2e0632022-09-16 19:34:24 +0800954static bool stop_waiting_barrier(struct r10conf *conf)
955{
956 struct bio_list *bio_list = current->bio_list;
Yu Kuai44693152023-05-23 10:10:17 +0800957 struct md_thread *thread;
Yu Kuaied2e0632022-09-16 19:34:24 +0800958
959 /* barrier is dropped */
960 if (!conf->barrier)
961 return true;
962
963 /*
964 * If there are already pending requests (preventing the barrier from
965 * rising completely), and the pre-process bio queue isn't empty, then
966 * don't wait, as we need to empty that queue to get the nr_pending
967 * count down.
968 */
969 if (atomic_read(&conf->nr_pending) && bio_list &&
970 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1])))
971 return true;
972
Yu Kuai44693152023-05-23 10:10:17 +0800973 /* daemon thread must exist while handling io */
974 thread = rcu_dereference_protected(conf->mddev->thread, true);
Li Nan72c215e2023-02-22 12:09:59 +0800975 /*
976 * move on if io is issued from raid10d(), nr_pending is not released
977 * from original io(see handle_read_error()). All raise barrier is
978 * blocked until this io is done.
979 */
Yu Kuai44693152023-05-23 10:10:17 +0800980 if (thread->tsk == current) {
Li Nan72c215e2023-02-22 12:09:59 +0800981 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0);
Yu Kuaied2e0632022-09-16 19:34:24 +0800982 return true;
Li Nan72c215e2023-02-22 12:09:59 +0800983 }
Yu Kuaied2e0632022-09-16 19:34:24 +0800984
985 return false;
986}
987
Yu Kuaib9b083f2022-09-16 19:34:28 +0800988static bool wait_barrier_nolock(struct r10conf *conf)
989{
990 unsigned int seq = read_seqbegin(&conf->resync_lock);
991
992 if (READ_ONCE(conf->barrier))
993 return false;
994
995 atomic_inc(&conf->nr_pending);
996 if (!read_seqretry(&conf->resync_lock, seq))
997 return true;
998
999 if (atomic_dec_and_test(&conf->nr_pending))
1000 wake_up_barrier(conf);
1001
1002 return false;
1003}
1004
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001005static bool wait_barrier(struct r10conf *conf, bool nowait)
NeilBrown0a27ec92006-01-06 00:20:13 -08001006{
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001007 bool ret = true;
1008
Yu Kuaib9b083f2022-09-16 19:34:28 +08001009 if (wait_barrier_nolock(conf))
1010 return true;
1011
1012 write_seqlock_irq(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08001013 if (conf->barrier) {
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001014 /* Return false when nowait flag is set */
1015 if (nowait) {
1016 ret = false;
1017 } else {
Yu Kuai0de57e52022-09-16 19:34:25 +08001018 conf->nr_waiting++;
Christoph Hellwig28be4fd2024-03-03 07:01:41 -07001019 mddev_add_trace_msg(conf->mddev, "raid10 wait barrier");
Yu Kuaib9b083f2022-09-16 19:34:28 +08001020 wait_event_barrier(conf, stop_waiting_barrier(conf));
Yu Kuai0de57e52022-09-16 19:34:25 +08001021 conf->nr_waiting--;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001022 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001023 if (!conf->nr_waiting)
1024 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -08001025 }
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001026 /* Only increment nr_pending when we wait */
1027 if (ret)
1028 atomic_inc(&conf->nr_pending);
Yu Kuaib9b083f2022-09-16 19:34:28 +08001029 write_sequnlock_irq(&conf->resync_lock);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001030 return ret;
NeilBrown0a27ec92006-01-06 00:20:13 -08001031}
1032
NeilBrowne879a872011-10-11 16:49:02 +11001033static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001034{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001035 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1036 (conf->array_freeze_pending))
Yu Kuai0c0be98b2022-09-16 19:34:26 +08001037 wake_up_barrier(conf);
NeilBrown0a27ec92006-01-06 00:20:13 -08001038}
1039
NeilBrowne2d59922013-06-12 11:01:22 +10001040static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -08001041{
1042 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001043 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001044 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001045 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001046 * This is called in the context of one normal IO request
1047 * that has failed. Thus any sync request that might be pending
1048 * will be blocked by nr_pending, and we need to wait for
1049 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001050 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001051 * must match the number of pending IOs (nr_pending) before
1052 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001053 */
Yu Kuaib9b083f2022-09-16 19:34:28 +08001054 write_seqlock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001055 conf->array_freeze_pending++;
Yu Kuaib9b083f2022-09-16 19:34:28 +08001056 WRITE_ONCE(conf->barrier, conf->barrier + 1);
NeilBrown4443ae12006-01-06 00:20:28 -08001057 conf->nr_waiting++;
Yu Kuaib9b083f2022-09-16 19:34:28 +08001058 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) ==
1059 conf->nr_queued + extra, flush_pending_writes(conf));
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001060 conf->array_freeze_pending--;
Yu Kuaib9b083f2022-09-16 19:34:28 +08001061 write_sequnlock_irq(&conf->resync_lock);
NeilBrown4443ae12006-01-06 00:20:28 -08001062}
1063
NeilBrowne879a872011-10-11 16:49:02 +11001064static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001065{
1066 /* reverse the effect of the freeze */
Yu Kuaib9b083f2022-09-16 19:34:28 +08001067 write_seqlock_irq(&conf->resync_lock);
1068 WRITE_ONCE(conf->barrier, conf->barrier - 1);
NeilBrown4443ae12006-01-06 00:20:28 -08001069 conf->nr_waiting--;
1070 wake_up(&conf->wait_barrier);
Yu Kuaib9b083f2022-09-16 19:34:28 +08001071 write_sequnlock_irq(&conf->resync_lock);
NeilBrown4443ae12006-01-06 00:20:28 -08001072}
1073
NeilBrownf8c9e742012-05-21 09:28:33 +10001074static sector_t choose_data_offset(struct r10bio *r10_bio,
1075 struct md_rdev *rdev)
1076{
1077 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1078 test_bit(R10BIO_Previous, &r10_bio->state))
1079 return rdev->data_offset;
1080 else
1081 return rdev->new_data_offset;
1082}
1083
NeilBrown57c67df2012-10-11 13:32:13 +11001084static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1085{
Mariusz Tkaczykdaae1612022-01-17 12:38:47 +01001086 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb);
NeilBrown57c67df2012-10-11 13:32:13 +11001087 struct mddev *mddev = plug->cb.data;
1088 struct r10conf *conf = mddev->private;
1089 struct bio *bio;
1090
Yu Kuai9efcc2c2023-05-29 21:11:05 +08001091 if (from_schedule) {
NeilBrown57c67df2012-10-11 13:32:13 +11001092 spin_lock_irq(&conf->device_lock);
1093 bio_list_merge(&conf->pending_bio_list, &plug->pending);
NeilBrown57c67df2012-10-11 13:32:13 +11001094 spin_unlock_irq(&conf->device_lock);
Yu Kuaia8d5fdd42023-06-21 18:57:28 +08001095 wake_up_barrier(conf);
NeilBrown57c67df2012-10-11 13:32:13 +11001096 md_wakeup_thread(mddev->thread);
1097 kfree(plug);
1098 return;
1099 }
1100
1101 /* we aren't scheduling, so we can do the write-out directly. */
1102 bio = bio_list_get(&plug->pending);
Yu Kuai9efcc2c2023-05-29 21:11:05 +08001103 raid1_prepare_flush_writes(mddev->bitmap);
Yu Kuaia8d5fdd42023-06-21 18:57:28 +08001104 wake_up_barrier(conf);
NeilBrown57c67df2012-10-11 13:32:13 +11001105
1106 while (bio) { /* submit pending writes */
1107 struct bio *next = bio->bi_next;
Yu Kuai8295efb2023-05-29 21:11:02 +08001108
1109 raid1_submit_write(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001110 bio = next;
Yu Kuai01044462023-05-29 21:11:00 +08001111 cond_resched();
NeilBrown57c67df2012-10-11 13:32:13 +11001112 }
1113 kfree(plug);
1114}
1115
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001116/*
1117 * 1. Register the new request and wait if the reconstruction thread has put
1118 * up a bar for new requests. Continue immediately if no resync is active
1119 * currently.
1120 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1121 */
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001122static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf,
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001123 struct bio *bio, sector_t sectors)
1124{
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001125 /* Bail out if REQ_NOWAIT is set for the bio */
1126 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) {
1127 bio_wouldblock_error(bio);
1128 return false;
1129 }
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001130 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1131 bio->bi_iter.bi_sector < conf->reshape_progress &&
1132 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001133 allow_barrier(conf);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001134 if (bio->bi_opf & REQ_NOWAIT) {
1135 bio_wouldblock_error(bio);
1136 return false;
1137 }
Christoph Hellwig28be4fd2024-03-03 07:01:41 -07001138 mddev_add_trace_msg(conf->mddev, "raid10 wait reshape");
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001139 wait_event(conf->wait_barrier,
1140 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1141 conf->reshape_progress >= bio->bi_iter.bi_sector +
1142 sectors);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001143 wait_barrier(conf, false);
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001144 }
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001145 return true;
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001146}
1147
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001148static void raid10_read_request(struct mddev *mddev, struct bio *bio,
Yu Kuai82045522023-06-22 00:51:07 +08001149 struct r10bio *r10_bio, bool io_accounting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
NeilBrowne879a872011-10-11 16:49:02 +11001151 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct bio *read_bio;
Bart Van Asschecb1802f2022-07-14 11:07:01 -07001153 const enum req_op op = bio_op(bio);
1154 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001155 int max_sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001156 struct md_rdev *rdev;
NeilBrown545250f2017-04-05 14:05:51 +10001157 char b[BDEVNAME_SIZE];
1158 int slot = r10_bio->read_slot;
1159 struct md_rdev *err_rdev = NULL;
1160 gfp_t gfp = GFP_NOIO;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001161
Kevin Vigor93decc52020-11-06 14:20:34 -08001162 if (slot >= 0 && r10_bio->devs[slot].rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001163 /*
1164 * This is an error retry, but we cannot
1165 * safely dereference the rdev in the r10_bio,
1166 * we must use the one in conf.
1167 * If it has already been disconnected (unlikely)
1168 * we lose the device name in error messages.
1169 */
1170 int disk;
1171 /*
1172 * As we are blocking raid10, it is a little safer to
1173 * use __GFP_HIGH.
1174 */
1175 gfp = GFP_NOIO | __GFP_HIGH;
1176
NeilBrown545250f2017-04-05 14:05:51 +10001177 disk = r10_bio->devs[slot].devnum;
Yu Kuaia448af22023-11-25 16:16:01 +08001178 err_rdev = conf->mirrors[disk].rdev;
NeilBrown545250f2017-04-05 14:05:51 +10001179 if (err_rdev)
Christoph Hellwig900d1562022-07-13 07:53:17 +02001180 snprintf(b, sizeof(b), "%pg", err_rdev->bdev);
NeilBrown545250f2017-04-05 14:05:51 +10001181 else {
1182 strcpy(b, "???");
1183 /* This never gets dereferenced */
1184 err_rdev = r10_bio->devs[slot].rdev;
1185 }
NeilBrown545250f2017-04-05 14:05:51 +10001186 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001187
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001188 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors))
1189 return;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001190 rdev = read_balance(conf, r10_bio, &max_sectors);
1191 if (!rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001192 if (err_rdev) {
1193 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1194 mdname(mddev), b,
1195 (unsigned long long)r10_bio->sector);
1196 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001197 raid_end_bio_io(r10_bio);
1198 return;
1199 }
NeilBrown545250f2017-04-05 14:05:51 +10001200 if (err_rdev)
Christoph Hellwig913cce52022-05-12 08:19:13 +02001201 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
NeilBrown545250f2017-04-05 14:05:51 +10001202 mdname(mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +02001203 rdev->bdev,
NeilBrown545250f2017-04-05 14:05:51 +10001204 (unsigned long long)r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001205 if (max_sectors < bio_sectors(bio)) {
1206 struct bio *split = bio_split(bio, max_sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001207 gfp, &conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001208 bio_chain(split, bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001209 allow_barrier(conf);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001210 submit_bio_noacct(bio);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001211 wait_barrier(conf, false);
NeilBrownfc9977d2017-04-05 14:05:51 +10001212 bio = split;
1213 r10_bio->master_bio = bio;
1214 r10_bio->sectors = max_sectors;
1215 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001216 slot = r10_bio->read_slot;
1217
Yu Kuai82045522023-06-22 00:51:07 +08001218 if (io_accounting) {
1219 md_account_bio(mddev, &bio);
1220 r10_bio->master_bio = bio;
1221 }
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001222 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001223
1224 r10_bio->devs[slot].bio = read_bio;
1225 r10_bio->devs[slot].rdev = rdev;
1226
1227 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1228 choose_data_offset(r10_bio, rdev);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001229 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01001230 read_bio->bi_opf = op | do_sync;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001231 if (test_bit(FailFast, &rdev->flags) &&
1232 test_bit(R10BIO_FailFast, &r10_bio->state))
1233 read_bio->bi_opf |= MD_FAILFAST;
1234 read_bio->bi_private = r10_bio;
Christoph Hellwigc396b902024-03-03 07:01:40 -07001235 mddev_trace_remap(mddev, read_bio, r10_bio->sector);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001236 submit_bio_noacct(read_bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001237 return;
1238}
1239
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001240static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1241 struct bio *bio, bool replacement,
NeilBrownfc9977d2017-04-05 14:05:51 +10001242 int n_copy)
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001243{
Bart Van Asschecb1802f2022-07-14 11:07:01 -07001244 const enum req_op op = bio_op(bio);
1245 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1246 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001247 unsigned long flags;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001248 struct r10conf *conf = mddev->private;
1249 struct md_rdev *rdev;
1250 int devnum = r10_bio->devs[n_copy].devnum;
1251 struct bio *mbio;
1252
Yu Kuaia448af22023-11-25 16:16:01 +08001253 rdev = replacement ? conf->mirrors[devnum].replacement :
1254 conf->mirrors[devnum].rdev;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001255
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001256 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001257 if (replacement)
1258 r10_bio->devs[n_copy].repl_bio = mbio;
1259 else
1260 r10_bio->devs[n_copy].bio = mbio;
1261
1262 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1263 choose_data_offset(r10_bio, rdev));
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001264 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01001265 mbio->bi_opf = op | do_sync | do_fua;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001266 if (!replacement && test_bit(FailFast,
1267 &conf->mirrors[devnum].rdev->flags)
1268 && enough(conf, devnum))
1269 mbio->bi_opf |= MD_FAILFAST;
1270 mbio->bi_private = r10_bio;
Christoph Hellwigc396b902024-03-03 07:01:40 -07001271 mddev_trace_remap(mddev, mbio, r10_bio->sector);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001272 /* flush_pending_writes() needs access to the rdev so...*/
Christoph Hellwig309dca302021-01-24 11:02:34 +01001273 mbio->bi_bdev = (void *)rdev;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001274
1275 atomic_inc(&r10_bio->remaining);
1276
Yu Kuai460af1f2023-05-29 21:11:06 +08001277 if (!raid1_add_bio_to_plug(mddev, mbio, raid10_unplug, conf->copies)) {
Shaohua Li23b245c2017-05-10 08:47:11 -07001278 spin_lock_irqsave(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001279 bio_list_add(&conf->pending_bio_list, mbio);
Shaohua Li23b245c2017-05-10 08:47:11 -07001280 spin_unlock_irqrestore(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001281 md_wakeup_thread(mddev->thread);
Shaohua Li23b245c2017-05-10 08:47:11 -07001282 }
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001283}
1284
Xiao Nif2e7e262021-02-04 15:50:45 +08001285static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1286{
1287 int i;
1288 struct r10conf *conf = mddev->private;
1289 struct md_rdev *blocked_rdev;
1290
1291retry_wait:
1292 blocked_rdev = NULL;
Xiao Nif2e7e262021-02-04 15:50:45 +08001293 for (i = 0; i < conf->copies; i++) {
Li Nan67364342023-07-01 16:05:29 +08001294 struct md_rdev *rdev, *rrdev;
1295
Yu Kuaia448af22023-11-25 16:16:01 +08001296 rdev = conf->mirrors[i].rdev;
1297 rrdev = conf->mirrors[i].replacement;
Xiao Nif2e7e262021-02-04 15:50:45 +08001298 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1299 atomic_inc(&rdev->nr_pending);
1300 blocked_rdev = rdev;
1301 break;
1302 }
1303 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1304 atomic_inc(&rrdev->nr_pending);
1305 blocked_rdev = rrdev;
1306 break;
1307 }
1308
1309 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
Xiao Nif2e7e262021-02-04 15:50:45 +08001310 sector_t dev_sector = r10_bio->devs[i].addr;
Xiao Nif2e7e262021-02-04 15:50:45 +08001311
1312 /*
1313 * Discard request doesn't care the write result
1314 * so it doesn't need to wait blocked disk here.
1315 */
1316 if (!r10_bio->sectors)
1317 continue;
1318
Yu Kuai3a0f0072024-02-29 17:57:04 +08001319 if (rdev_has_badblock(rdev, dev_sector,
1320 r10_bio->sectors) < 0) {
Xiao Nif2e7e262021-02-04 15:50:45 +08001321 /*
1322 * Mustn't write here until the bad block
1323 * is acknowledged
1324 */
1325 atomic_inc(&rdev->nr_pending);
1326 set_bit(BlockedBadBlocks, &rdev->flags);
1327 blocked_rdev = rdev;
1328 break;
1329 }
1330 }
1331 }
Xiao Nif2e7e262021-02-04 15:50:45 +08001332
1333 if (unlikely(blocked_rdev)) {
1334 /* Have to wait for this device to get unblocked, then retry */
1335 allow_barrier(conf);
Christoph Hellwig28be4fd2024-03-03 07:01:41 -07001336 mddev_add_trace_msg(conf->mddev,
1337 "raid10 %s wait rdev %d blocked",
1338 __func__, blocked_rdev->raid_disk);
Xiao Nif2e7e262021-02-04 15:50:45 +08001339 md_wait_for_blocked_rdev(blocked_rdev, mddev);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001340 wait_barrier(conf, false);
Xiao Nif2e7e262021-02-04 15:50:45 +08001341 goto retry_wait;
1342 }
1343}
1344
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001345static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1346 struct r10bio *r10_bio)
1347{
1348 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int i;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001350 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001351 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Guoqing Jiangcb8a7a72017-10-24 15:11:51 +08001353 if ((mddev_is_clustered(mddev) &&
1354 md_cluster_ops->area_resyncing(mddev, WRITE,
1355 bio->bi_iter.bi_sector,
1356 bio_end_sector(bio)))) {
1357 DEFINE_WAIT(w);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001358 /* Bail out if REQ_NOWAIT is set for the bio */
1359 if (bio->bi_opf & REQ_NOWAIT) {
1360 bio_wouldblock_error(bio);
1361 return;
1362 }
Guoqing Jiangcb8a7a72017-10-24 15:11:51 +08001363 for (;;) {
1364 prepare_to_wait(&conf->wait_barrier,
1365 &w, TASK_IDLE);
1366 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1367 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1368 break;
1369 schedule();
1370 }
1371 finish_wait(&conf->wait_barrier, &w);
1372 }
1373
NeilBrownfc9977d2017-04-05 14:05:51 +10001374 sectors = r10_bio->sectors;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001375 if (!regular_request_wait(mddev, conf, bio, sectors))
1376 return;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001377 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001378 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001379 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1380 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1381 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1382 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001383 /* Need to update reshape_position in metadata */
1384 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001385 set_mask_bits(&mddev->sb_flags, 0,
1386 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001387 md_wakeup_thread(mddev->thread);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001388 if (bio->bi_opf & REQ_NOWAIT) {
1389 allow_barrier(conf);
1390 bio_wouldblock_error(bio);
1391 return;
1392 }
Christoph Hellwig28be4fd2024-03-03 07:01:41 -07001393 mddev_add_trace_msg(conf->mddev,
1394 "raid10 wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001395 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001396 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001397
1398 conf->reshape_safe = mddev->reshape_position;
1399 }
1400
Dan Williams6bfe0b42008-04-30 00:52:32 -07001401 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * inc refcount on their rdev. Record them by setting
1403 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001404 * If there are known/acknowledged bad blocks on any device
1405 * on which we have seen a write error, we want to avoid
1406 * writing to those blocks. This potentially requires several
1407 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001408 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001410
NeilBrown69335ef2011-12-23 10:17:54 +11001411 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 raid10_find_phys(conf, r10_bio);
Xiao Nif2e7e262021-02-04 15:50:45 +08001413
1414 wait_blocked_dev(mddev, r10_bio);
1415
NeilBrownd4432c22011-07-28 11:39:24 +10001416 max_sectors = r10_bio->sectors;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 for (i = 0; i < conf->copies; i++) {
1419 int d = r10_bio->devs[i].devnum;
Li Nan2ae6aaf2023-06-02 17:18:39 +08001420 struct md_rdev *rdev, *rrdev;
1421
Yu Kuaia448af22023-11-25 16:16:01 +08001422 rdev = conf->mirrors[d].rdev;
1423 rrdev = conf->mirrors[d].replacement;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001424 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001425 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001426 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001427 rrdev = NULL;
1428
NeilBrownd4432c22011-07-28 11:39:24 +10001429 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001430 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001431
1432 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001433 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001434 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001435 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001436 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001437 sector_t first_bad;
1438 sector_t dev_sector = r10_bio->devs[i].addr;
1439 int bad_sectors;
1440 int is_bad;
1441
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001442 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001443 &first_bad, &bad_sectors);
NeilBrownd4432c22011-07-28 11:39:24 +10001444 if (is_bad && first_bad <= dev_sector) {
1445 /* Cannot write here at all */
1446 bad_sectors -= (dev_sector - first_bad);
1447 if (bad_sectors < max_sectors)
1448 /* Mustn't write more than bad_sectors
1449 * to other devices yet
1450 */
1451 max_sectors = bad_sectors;
1452 /* We don't set R10BIO_Degraded as that
1453 * only applies if the disk is missing,
1454 * so it might be re-added, and we want to
1455 * know to recover this chunk.
1456 * In this case the device is here, and the
1457 * fact that this chunk is not in-sync is
1458 * recorded in the bad block log.
1459 */
1460 continue;
1461 }
1462 if (is_bad) {
1463 int good_sectors = first_bad - dev_sector;
1464 if (good_sectors < max_sectors)
1465 max_sectors = good_sectors;
1466 }
1467 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001468 if (rdev) {
1469 r10_bio->devs[i].bio = bio;
1470 atomic_inc(&rdev->nr_pending);
1471 }
NeilBrown475b0322011-12-23 10:17:55 +11001472 if (rrdev) {
1473 r10_bio->devs[i].repl_bio = bio;
1474 atomic_inc(&rrdev->nr_pending);
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
NeilBrown6b6c8112017-03-15 14:05:13 +11001478 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001479 r10_bio->sectors = max_sectors;
NeilBrownfc9977d2017-04-05 14:05:51 +10001480
1481 if (r10_bio->sectors < bio_sectors(bio)) {
1482 struct bio *split = bio_split(bio, r10_bio->sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001483 GFP_NOIO, &conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001484 bio_chain(split, bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001485 allow_barrier(conf);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001486 submit_bio_noacct(bio);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001487 wait_barrier(conf, false);
NeilBrownfc9977d2017-04-05 14:05:51 +10001488 bio = split;
1489 r10_bio->master_bio = bio;
NeilBrownd4432c22011-07-28 11:39:24 +10001490 }
NeilBrownd4432c22011-07-28 11:39:24 +10001491
Yu Kuai82045522023-06-22 00:51:07 +08001492 md_account_bio(mddev, &bio);
1493 r10_bio->master_bio = bio;
NeilBrown4e780642010-10-19 12:54:01 +11001494 atomic_set(&r10_bio->remaining, 1);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001495 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (i = 0; i < conf->copies; i++) {
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001498 if (r10_bio->devs[i].bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001499 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001500 if (r10_bio->devs[i].repl_bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001501 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
NeilBrownd4432c22011-07-28 11:39:24 +10001502 }
NeilBrown079fa162011-09-10 17:21:23 +10001503 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001504}
1505
NeilBrownfc9977d2017-04-05 14:05:51 +10001506static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001507{
1508 struct r10conf *conf = mddev->private;
1509 struct r10bio *r10_bio;
1510
Kent Overstreetafeee512018-05-20 18:25:52 -04001511 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001512
1513 r10_bio->master_bio = bio;
NeilBrownfc9977d2017-04-05 14:05:51 +10001514 r10_bio->sectors = sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001515
1516 r10_bio->mddev = mddev;
1517 r10_bio->sector = bio->bi_iter.bi_sector;
1518 r10_bio->state = 0;
Kevin Vigor93decc52020-11-06 14:20:34 -08001519 r10_bio->read_slot = -1;
Xiao Nic2968282021-02-04 15:50:44 +08001520 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1521 conf->geo.raid_disks);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001522
1523 if (bio_data_dir(bio) == READ)
Yu Kuai82045522023-06-22 00:51:07 +08001524 raid10_read_request(mddev, bio, r10_bio, true);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001525 else
1526 raid10_write_request(mddev, bio, r10_bio);
1527}
1528
Xiao Ni254c2712021-02-04 15:50:47 +08001529static void raid_end_discard_bio(struct r10bio *r10bio)
1530{
1531 struct r10conf *conf = r10bio->mddev->private;
1532 struct r10bio *first_r10bio;
1533
1534 while (atomic_dec_and_test(&r10bio->remaining)) {
1535
1536 allow_barrier(conf);
1537
1538 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1539 first_r10bio = (struct r10bio *)r10bio->master_bio;
1540 free_r10bio(r10bio);
1541 r10bio = first_r10bio;
1542 } else {
1543 md_write_end(r10bio->mddev);
1544 bio_endio(r10bio->master_bio);
1545 free_r10bio(r10bio);
1546 break;
1547 }
1548 }
1549}
1550
Xiao Nid30588b2021-02-04 15:50:46 +08001551static void raid10_end_discard_request(struct bio *bio)
1552{
1553 struct r10bio *r10_bio = bio->bi_private;
1554 struct r10conf *conf = r10_bio->mddev->private;
1555 struct md_rdev *rdev = NULL;
1556 int dev;
1557 int slot, repl;
1558
1559 /*
1560 * We don't care the return value of discard bio
1561 */
1562 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1563 set_bit(R10BIO_Uptodate, &r10_bio->state);
1564
1565 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Yu Kuaia448af22023-11-25 16:16:01 +08001566 rdev = repl ? conf->mirrors[dev].replacement :
1567 conf->mirrors[dev].rdev;
Xiao Nid30588b2021-02-04 15:50:46 +08001568
Xiao Ni254c2712021-02-04 15:50:47 +08001569 raid_end_discard_bio(r10_bio);
Xiao Nid30588b2021-02-04 15:50:46 +08001570 rdev_dec_pending(rdev, conf->mddev);
1571}
1572
1573/*
1574 * There are some limitations to handle discard bio
1575 * 1st, the discard size is bigger than stripe_size*2.
1576 * 2st, if the discard bio spans reshape progress, we use the old way to
1577 * handle discard bio
1578 */
1579static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1580{
1581 struct r10conf *conf = mddev->private;
1582 struct geom *geo = &conf->geo;
Xiao Ni254c2712021-02-04 15:50:47 +08001583 int far_copies = geo->far_copies;
1584 bool first_copy = true;
1585 struct r10bio *r10_bio, *first_r10bio;
Xiao Nid30588b2021-02-04 15:50:46 +08001586 struct bio *split;
1587 int disk;
1588 sector_t chunk;
1589 unsigned int stripe_size;
1590 unsigned int stripe_data_disks;
1591 sector_t split_size;
1592 sector_t bio_start, bio_end;
1593 sector_t first_stripe_index, last_stripe_index;
1594 sector_t start_disk_offset;
1595 unsigned int start_disk_index;
1596 sector_t end_disk_offset;
1597 unsigned int end_disk_index;
1598 unsigned int remainder;
1599
1600 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1601 return -EAGAIN;
1602
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001603 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) {
1604 bio_wouldblock_error(bio);
1605 return 0;
1606 }
1607 wait_barrier(conf, false);
Xiao Nid30588b2021-02-04 15:50:46 +08001608
1609 /*
1610 * Check reshape again to avoid reshape happens after checking
1611 * MD_RECOVERY_RESHAPE and before wait_barrier
1612 */
1613 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1614 goto out;
1615
1616 if (geo->near_copies)
1617 stripe_data_disks = geo->raid_disks / geo->near_copies +
1618 geo->raid_disks % geo->near_copies;
1619 else
1620 stripe_data_disks = geo->raid_disks;
1621
1622 stripe_size = stripe_data_disks << geo->chunk_shift;
1623
1624 bio_start = bio->bi_iter.bi_sector;
1625 bio_end = bio_end_sector(bio);
1626
1627 /*
1628 * Maybe one discard bio is smaller than strip size or across one
1629 * stripe and discard region is larger than one stripe size. For far
1630 * offset layout, if the discard region is not aligned with stripe
1631 * size, there is hole when we submit discard bio to member disk.
1632 * For simplicity, we only handle discard bio which discard region
1633 * is bigger than stripe_size * 2
1634 */
1635 if (bio_sectors(bio) < stripe_size*2)
1636 goto out;
1637
1638 /*
1639 * Keep bio aligned with strip size.
1640 */
1641 div_u64_rem(bio_start, stripe_size, &remainder);
1642 if (remainder) {
1643 split_size = stripe_size - remainder;
1644 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1645 bio_chain(split, bio);
1646 allow_barrier(conf);
1647 /* Resend the fist split part */
1648 submit_bio_noacct(split);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001649 wait_barrier(conf, false);
Xiao Nid30588b2021-02-04 15:50:46 +08001650 }
1651 div_u64_rem(bio_end, stripe_size, &remainder);
1652 if (remainder) {
1653 split_size = bio_sectors(bio) - remainder;
1654 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1655 bio_chain(split, bio);
1656 allow_barrier(conf);
1657 /* Resend the second split part */
1658 submit_bio_noacct(bio);
1659 bio = split;
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001660 wait_barrier(conf, false);
Xiao Nid30588b2021-02-04 15:50:46 +08001661 }
1662
Xiao Nid30588b2021-02-04 15:50:46 +08001663 bio_start = bio->bi_iter.bi_sector;
1664 bio_end = bio_end_sector(bio);
1665
1666 /*
1667 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1668 * One stripe contains the chunks from all member disk (one chunk from
1669 * one disk at the same HBA address). For layout detail, see 'man md 4'
1670 */
1671 chunk = bio_start >> geo->chunk_shift;
1672 chunk *= geo->near_copies;
1673 first_stripe_index = chunk;
1674 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1675 if (geo->far_offset)
1676 first_stripe_index *= geo->far_copies;
1677 start_disk_offset = (bio_start & geo->chunk_mask) +
1678 (first_stripe_index << geo->chunk_shift);
1679
1680 chunk = bio_end >> geo->chunk_shift;
1681 chunk *= geo->near_copies;
1682 last_stripe_index = chunk;
1683 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1684 if (geo->far_offset)
1685 last_stripe_index *= geo->far_copies;
1686 end_disk_offset = (bio_end & geo->chunk_mask) +
1687 (last_stripe_index << geo->chunk_shift);
1688
Xiao Ni254c2712021-02-04 15:50:47 +08001689retry_discard:
1690 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1691 r10_bio->mddev = mddev;
1692 r10_bio->state = 0;
1693 r10_bio->sectors = 0;
1694 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1695 wait_blocked_dev(mddev, r10_bio);
1696
1697 /*
1698 * For far layout it needs more than one r10bio to cover all regions.
1699 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1700 * to record the discard bio. Other r10bio->master_bio record the first
1701 * r10bio. The first r10bio only release after all other r10bios finish.
1702 * The discard bio returns only first r10bio finishes
1703 */
1704 if (first_copy) {
1705 r10_bio->master_bio = bio;
1706 set_bit(R10BIO_Discard, &r10_bio->state);
1707 first_copy = false;
1708 first_r10bio = r10_bio;
1709 } else
1710 r10_bio->master_bio = (struct bio *)first_r10bio;
1711
Xiao Ni46d47032021-08-18 13:57:48 +08001712 /*
1713 * first select target devices under rcu_lock and
1714 * inc refcount on their rdev. Record them by setting
1715 * bios[x] to bio
1716 */
Xiao Nid30588b2021-02-04 15:50:46 +08001717 for (disk = 0; disk < geo->raid_disks; disk++) {
Li Nan67364342023-07-01 16:05:29 +08001718 struct md_rdev *rdev, *rrdev;
Xiao Nid30588b2021-02-04 15:50:46 +08001719
Yu Kuaia448af22023-11-25 16:16:01 +08001720 rdev = conf->mirrors[disk].rdev;
1721 rrdev = conf->mirrors[disk].replacement;
Xiao Nid30588b2021-02-04 15:50:46 +08001722 r10_bio->devs[disk].bio = NULL;
1723 r10_bio->devs[disk].repl_bio = NULL;
1724
1725 if (rdev && (test_bit(Faulty, &rdev->flags)))
1726 rdev = NULL;
1727 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1728 rrdev = NULL;
1729 if (!rdev && !rrdev)
1730 continue;
1731
1732 if (rdev) {
1733 r10_bio->devs[disk].bio = bio;
1734 atomic_inc(&rdev->nr_pending);
1735 }
1736 if (rrdev) {
1737 r10_bio->devs[disk].repl_bio = bio;
1738 atomic_inc(&rrdev->nr_pending);
1739 }
1740 }
Xiao Nid30588b2021-02-04 15:50:46 +08001741
1742 atomic_set(&r10_bio->remaining, 1);
1743 for (disk = 0; disk < geo->raid_disks; disk++) {
1744 sector_t dev_start, dev_end;
1745 struct bio *mbio, *rbio = NULL;
Xiao Nid30588b2021-02-04 15:50:46 +08001746
1747 /*
1748 * Now start to calculate the start and end address for each disk.
1749 * The space between dev_start and dev_end is the discard region.
1750 *
1751 * For dev_start, it needs to consider three conditions:
1752 * 1st, the disk is before start_disk, you can imagine the disk in
1753 * the next stripe. So the dev_start is the start address of next
1754 * stripe.
1755 * 2st, the disk is after start_disk, it means the disk is at the
1756 * same stripe of first disk
1757 * 3st, the first disk itself, we can use start_disk_offset directly
1758 */
1759 if (disk < start_disk_index)
1760 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1761 else if (disk > start_disk_index)
1762 dev_start = first_stripe_index * mddev->chunk_sectors;
1763 else
1764 dev_start = start_disk_offset;
1765
1766 if (disk < end_disk_index)
1767 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1768 else if (disk > end_disk_index)
1769 dev_end = last_stripe_index * mddev->chunk_sectors;
1770 else
1771 dev_end = end_disk_offset;
1772
1773 /*
1774 * It only handles discard bio which size is >= stripe size, so
Xiao Ni46d47032021-08-18 13:57:48 +08001775 * dev_end > dev_start all the time.
1776 * It doesn't need to use rcu lock to get rdev here. We already
1777 * add rdev->nr_pending in the first loop.
Xiao Nid30588b2021-02-04 15:50:46 +08001778 */
1779 if (r10_bio->devs[disk].bio) {
Xiao Ni46d47032021-08-18 13:57:48 +08001780 struct md_rdev *rdev = conf->mirrors[disk].rdev;
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001781 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1782 &mddev->bio_set);
Xiao Nid30588b2021-02-04 15:50:46 +08001783 mbio->bi_end_io = raid10_end_discard_request;
1784 mbio->bi_private = r10_bio;
1785 r10_bio->devs[disk].bio = mbio;
1786 r10_bio->devs[disk].devnum = disk;
1787 atomic_inc(&r10_bio->remaining);
1788 md_submit_discard_bio(mddev, rdev, mbio,
1789 dev_start + choose_data_offset(r10_bio, rdev),
1790 dev_end - dev_start);
1791 bio_endio(mbio);
1792 }
1793 if (r10_bio->devs[disk].repl_bio) {
Xiao Ni46d47032021-08-18 13:57:48 +08001794 struct md_rdev *rrdev = conf->mirrors[disk].replacement;
Christoph Hellwigabfc4262022-02-02 17:01:09 +01001795 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1796 &mddev->bio_set);
Xiao Nid30588b2021-02-04 15:50:46 +08001797 rbio->bi_end_io = raid10_end_discard_request;
1798 rbio->bi_private = r10_bio;
1799 r10_bio->devs[disk].repl_bio = rbio;
1800 r10_bio->devs[disk].devnum = disk;
1801 atomic_inc(&r10_bio->remaining);
1802 md_submit_discard_bio(mddev, rrdev, rbio,
1803 dev_start + choose_data_offset(r10_bio, rrdev),
1804 dev_end - dev_start);
1805 bio_endio(rbio);
1806 }
1807 }
1808
Xiao Ni254c2712021-02-04 15:50:47 +08001809 if (!geo->far_offset && --far_copies) {
1810 first_stripe_index += geo->stride >> geo->chunk_shift;
1811 start_disk_offset += geo->stride;
1812 last_stripe_index += geo->stride >> geo->chunk_shift;
1813 end_disk_offset += geo->stride;
1814 atomic_inc(&first_r10bio->remaining);
1815 raid_end_discard_bio(r10_bio);
Vishal Vermac9aa889b02021-12-21 20:06:21 +00001816 wait_barrier(conf, false);
Xiao Ni254c2712021-02-04 15:50:47 +08001817 goto retry_discard;
Xiao Nid30588b2021-02-04 15:50:46 +08001818 }
1819
Xiao Ni254c2712021-02-04 15:50:47 +08001820 raid_end_discard_bio(r10_bio);
1821
Xiao Nid30588b2021-02-04 15:50:46 +08001822 return 0;
1823out:
1824 allow_barrier(conf);
1825 return -EAGAIN;
1826}
1827
NeilBrowncc27b0c2017-06-05 16:49:39 +10001828static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001829{
1830 struct r10conf *conf = mddev->private;
1831 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1832 int chunk_sects = chunk_mask + 1;
NeilBrownfc9977d2017-04-05 14:05:51 +10001833 int sectors = bio_sectors(bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001834
David Jeffery775d7832019-09-16 13:15:14 -04001835 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1836 && md_flush_request(mddev, bio))
NeilBrowncc27b0c2017-06-05 16:49:39 +10001837 return true;
Kent Overstreet20d01892013-11-23 18:21:01 -08001838
NeilBrowncc27b0c2017-06-05 16:49:39 +10001839 if (!md_write_start(mddev, bio))
1840 return false;
1841
Xiao Nid30588b2021-02-04 15:50:46 +08001842 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1843 if (!raid10_handle_discard(mddev, bio))
1844 return true;
1845
NeilBrownfc9977d2017-04-05 14:05:51 +10001846 /*
1847 * If this request crosses a chunk boundary, we need to split
1848 * it.
1849 */
1850 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1851 sectors > chunk_sects
1852 && (conf->geo.near_copies < conf->geo.raid_disks
1853 || conf->prev.near_copies <
1854 conf->prev.raid_disks)))
1855 sectors = chunk_sects -
1856 (bio->bi_iter.bi_sector &
1857 (chunk_sects - 1));
1858 __make_request(mddev, bio, sectors);
NeilBrown079fa162011-09-10 17:21:23 +10001859
1860 /* In case raid10d snuck in to freeze_array */
Yu Kuai0c0be98b2022-09-16 19:34:26 +08001861 wake_up_barrier(conf);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001862 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Shaohua Li849674e2016-01-20 13:52:20 -08001865static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
NeilBrowne879a872011-10-11 16:49:02 +11001867 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int i;
1869
Yu Kuaia448af22023-11-25 16:16:01 +08001870 lockdep_assert_held(&mddev->lock);
1871
NeilBrown5cf00fc2012-05-21 09:28:20 +10001872 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001873 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001874 if (conf->geo.near_copies > 1)
1875 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1876 if (conf->geo.far_copies > 1) {
1877 if (conf->geo.far_offset)
1878 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001879 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001880 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001881 if (conf->geo.far_set_size != conf->geo.raid_disks)
1882 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001883 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001884 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1885 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001886 for (i = 0; i < conf->geo.raid_disks; i++) {
Yu Kuaia448af22023-11-25 16:16:01 +08001887 struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev);
1888
NeilBrownd44b0a92016-06-02 16:19:52 +10001889 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 seq_printf(seq, "]");
1892}
1893
NeilBrown700c7212011-07-27 11:00:36 +10001894/* check if there are enough drives for
1895 * every block to appear on atleast one.
1896 * Don't consider the device numbered 'ignore'
1897 * as we might be about to remove it.
1898 */
NeilBrown635f6412013-06-11 14:57:09 +10001899static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001900{
1901 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001902 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001903 int disks, ncopies;
1904 if (previous) {
1905 disks = conf->prev.raid_disks;
1906 ncopies = conf->prev.near_copies;
1907 } else {
1908 disks = conf->geo.raid_disks;
1909 ncopies = conf->geo.near_copies;
1910 }
NeilBrown700c7212011-07-27 11:00:36 +10001911
1912 do {
1913 int n = conf->copies;
1914 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001915 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001916 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001917 struct md_rdev *rdev;
1918 if (this != ignore &&
Yu Kuaia448af22023-11-25 16:16:01 +08001919 (rdev = conf->mirrors[this].rdev) &&
NeilBrown725d6e52013-06-11 15:08:03 +10001920 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001921 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001922 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001923 }
1924 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001925 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001926 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001927 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001928 has_enough = 1;
1929out:
NeilBrown725d6e52013-06-11 15:08:03 +10001930 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001931}
1932
NeilBrownf8c9e742012-05-21 09:28:33 +10001933static int enough(struct r10conf *conf, int ignore)
1934{
NeilBrown635f6412013-06-11 14:57:09 +10001935 /* when calling 'enough', both 'prev' and 'geo' must
1936 * be stable.
1937 * This is ensured if ->reconfig_mutex or ->device_lock
1938 * is held.
1939 */
1940 return _enough(conf, 0, ignore) &&
1941 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001942}
1943
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01001944/**
1945 * raid10_error() - RAID10 error handler.
1946 * @mddev: affected md device.
1947 * @rdev: member device to fail.
1948 *
1949 * The routine acknowledges &rdev failure and determines new @mddev state.
1950 * If it failed, then:
1951 * - &MD_BROKEN flag is set in &mddev->flags.
1952 * Otherwise, it must be degraded:
1953 * - recovery is interrupted.
1954 * - &mddev->degraded is bumped.
Guoqing Jiang62bca042022-08-22 15:45:39 +08001955 *
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01001956 * @rdev is marked as &Faulty excluding case when array is failed and
1957 * &mddev->fail_last_dev is off.
1958 */
Shaohua Li849674e2016-01-20 13:52:20 -08001959static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
NeilBrowne879a872011-10-11 16:49:02 +11001961 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001962 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
NeilBrown635f6412013-06-11 14:57:09 +10001964 spin_lock_irqsave(&conf->device_lock, flags);
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01001965
1966 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) {
1967 set_bit(MD_BROKEN, &mddev->flags);
1968
1969 if (!mddev->fail_last_dev) {
1970 spin_unlock_irqrestore(&conf->device_lock, flags);
1971 return;
1972 }
NeilBrown635f6412013-06-11 14:57:09 +10001973 }
NeilBrown2446dba2014-07-31 10:16:29 +10001974 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001975 mddev->degraded++;
Mariusz Tkaczyk9631abd2022-03-22 16:23:38 +01001976
NeilBrown2446dba2014-07-31 10:16:29 +10001977 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001978 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001979 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001980 set_mask_bits(&mddev->sb_flags, 0,
1981 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001982 spin_unlock_irqrestore(&conf->device_lock, flags);
Christoph Hellwig913cce52022-05-12 08:19:13 +02001983 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
NeilBrown08464e02016-11-02 14:16:50 +11001984 "md/raid10:%s: Operation continuing on %d devices.\n",
Christoph Hellwig913cce52022-05-12 08:19:13 +02001985 mdname(mddev), rdev->bdev,
NeilBrown08464e02016-11-02 14:16:50 +11001986 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
NeilBrowne879a872011-10-11 16:49:02 +11001989static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001992 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
NeilBrown08464e02016-11-02 14:16:50 +11001994 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001996 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return;
1998 }
NeilBrown08464e02016-11-02 14:16:50 +11001999 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
2000 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Yu Kuaia448af22023-11-25 16:16:01 +08002002 lockdep_assert_held(&conf->mddev->reconfig_mutex);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002003 for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown4056ca52016-06-02 16:19:52 +10002004 rdev = conf->mirrors[i].rdev;
2005 if (rdev)
Christoph Hellwig913cce52022-05-12 08:19:13 +02002006 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
NeilBrown08464e02016-11-02 14:16:50 +11002007 i, !test_bit(In_sync, &rdev->flags),
2008 !test_bit(Faulty, &rdev->flags),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002009 rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011}
2012
NeilBrowne879a872011-10-11 16:49:02 +11002013static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Vishal Vermac9aa889b02021-12-21 20:06:21 +00002015 wait_barrier(conf, false);
NeilBrown0a27ec92006-01-06 00:20:13 -08002016 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Kent Overstreetafeee512018-05-20 18:25:52 -04002018 mempool_exit(&conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
NeilBrownfd01b882011-10-11 16:47:53 +11002021static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 int i;
NeilBrowne879a872011-10-11 16:49:02 +11002024 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002025 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10002026 int count = 0;
2027 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /*
2030 * Find all non-in_sync disks within the RAID10 configuration
2031 * and mark them in_sync
2032 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002033 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11002035 if (tmp->replacement
2036 && tmp->replacement->recovery_offset == MaxSector
2037 && !test_bit(Faulty, &tmp->replacement->flags)
2038 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2039 /* Replacement has just become active */
2040 if (!tmp->rdev
2041 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2042 count++;
2043 if (tmp->rdev) {
2044 /* Replaced device not technically faulty,
2045 * but we need to be sure it gets removed
2046 * and never re-added.
2047 */
2048 set_bit(Faulty, &tmp->rdev->flags);
2049 sysfs_notify_dirent_safe(
2050 tmp->rdev->sysfs_state);
2051 }
2052 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2053 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11002054 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11002055 && !test_bit(Faulty, &tmp->rdev->flags)
2056 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10002057 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002058 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 }
NeilBrown6b965622010-08-18 11:56:59 +10002061 spin_lock_irqsave(&conf->device_lock, flags);
2062 mddev->degraded -= count;
2063 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10002066 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
NeilBrownfd01b882011-10-11 16:47:53 +11002069static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
NeilBrowne879a872011-10-11 16:49:02 +11002071 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10002072 int err = -EEXIST;
Li Nan60903682023-05-27 17:20:07 +08002073 int mirror, repl_slot = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002074 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002075 int last = conf->geo.raid_disks - 1;
Li Nan60903682023-05-27 17:20:07 +08002076 struct raid10_info *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 if (mddev->recovery_cp < MaxSector)
2079 /* only hot-add to in-sync arrays, as recovery is
2080 * very different from resync
2081 */
Neil Brown199050e2008-06-28 08:31:33 +10002082 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10002083 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10002084 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Dan Williams1501efa2016-01-13 16:00:07 -08002086 if (md_integrity_add_rdev(rdev, mddev))
2087 return -ENXIO;
2088
NeilBrowna53a6c82008-11-06 17:28:20 +11002089 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10002090 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Namhyung Kim2c4193d2011-07-18 17:38:43 +10002092 if (rdev->saved_raid_disk >= first &&
Shaohua Li9e753ba2018-10-14 17:05:07 -07002093 rdev->saved_raid_disk < conf->geo.raid_disks &&
NeilBrown6cce3b22006-01-06 00:20:16 -08002094 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2095 mirror = rdev->saved_raid_disk;
2096 else
Neil Brown6c2fce22008-06-28 08:31:31 +10002097 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10002098 for ( ; mirror <= last ; mirror++) {
Li Nan60903682023-05-27 17:20:07 +08002099 p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10002100 if (p->recovery_disabled == mddev->recovery_disabled)
2101 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11002102 if (p->rdev) {
Li Nan60903682023-05-27 17:20:07 +08002103 if (test_bit(WantReplacement, &p->rdev->flags) &&
2104 p->replacement == NULL && repl_slot < 0)
2105 repl_slot = mirror;
2106 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07002109 err = mddev_stack_new_rdev(mddev, rdev);
2110 if (err)
2111 return err;
NeilBrown2bb77732011-07-27 11:00:36 +10002112 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11002113 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10002114 rdev->raid_disk = mirror;
2115 err = 0;
2116 if (rdev->saved_raid_disk != mirror)
2117 conf->fullsync = 1;
Yu Kuaia448af22023-11-25 16:16:01 +08002118 WRITE_ONCE(p->rdev, rdev);
NeilBrown2bb77732011-07-27 11:00:36 +10002119 break;
2120 }
Shaohua Li532a2a32012-10-11 13:30:52 +11002121
Li Nan60903682023-05-27 17:20:07 +08002122 if (err && repl_slot >= 0) {
2123 p = &conf->mirrors[repl_slot];
2124 clear_bit(In_sync, &rdev->flags);
2125 set_bit(Replacement, &rdev->flags);
2126 rdev->raid_disk = repl_slot;
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07002127 err = mddev_stack_new_rdev(mddev, rdev);
2128 if (err)
2129 return err;
Li Nan60903682023-05-27 17:20:07 +08002130 conf->fullsync = 1;
Yu Kuaia448af22023-11-25 16:16:01 +08002131 WRITE_ONCE(p->replacement, rdev);
Li Nan60903682023-05-27 17:20:07 +08002132 }
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10002135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
NeilBrownb8321b62011-12-23 10:17:51 +11002138static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
NeilBrowne879a872011-10-11 16:49:02 +11002140 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11002142 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11002143 struct md_rdev **rdevp;
Mikulas Patockad17f7442022-07-26 04:33:12 -04002144 struct raid10_info *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 print_conf(conf);
Mikulas Patockad17f7442022-07-26 04:33:12 -04002147 if (unlikely(number >= mddev->raid_disks))
2148 return 0;
2149 p = conf->mirrors + number;
NeilBrownc8ab9032011-12-23 10:17:54 +11002150 if (rdev == p->rdev)
2151 rdevp = &p->rdev;
2152 else if (rdev == p->replacement)
2153 rdevp = &p->replacement;
2154 else
2155 return 0;
2156
2157 if (test_bit(In_sync, &rdev->flags) ||
2158 atomic_read(&rdev->nr_pending)) {
2159 err = -EBUSY;
2160 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
NeilBrownd787be42016-06-02 16:19:53 +10002162 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11002163 * is not possible.
2164 */
2165 if (!test_bit(Faulty, &rdev->flags) &&
2166 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11002167 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10002168 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11002169 enough(conf, -1)) {
2170 err = -EBUSY;
2171 goto abort;
2172 }
Yu Kuaia448af22023-11-25 16:16:01 +08002173 WRITE_ONCE(*rdevp, NULL);
NeilBrownd787be42016-06-02 16:19:53 +10002174 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11002175 /* We must have just cleared 'rdev' */
Yu Kuaia448af22023-11-25 16:16:01 +08002176 WRITE_ONCE(p->rdev, p->replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11002177 clear_bit(Replacement, &p->replacement->flags);
Yu Kuaia448af22023-11-25 16:16:01 +08002178 WRITE_ONCE(p->replacement, NULL);
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08002179 }
NeilBrown4ca40c22011-12-23 10:17:55 +11002180
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08002181 clear_bit(WantReplacement, &rdev->flags);
NeilBrownc8ab9032011-12-23 10:17:54 +11002182 err = md_integrity_register(mddev);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184abort:
2185
2186 print_conf(conf);
2187 return err;
2188}
2189
Ming Lei81fa1522017-03-17 00:12:32 +08002190static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
NeilBrowne879a872011-10-11 16:49:02 +11002192 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002193
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002194 if (!bio->bi_status)
NeilBrown0eb3ff12006-01-06 00:20:29 -08002195 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10002196 else
2197 /* The write handler will notice the lack of
2198 * R10BIO_Uptodate and record any errors etc
2199 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08002200 atomic_add(r10_bio->sectors,
2201 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* for reconstruct, we always reschedule after a read.
2204 * for resync, only after all reads
2205 */
NeilBrown73d5c382009-02-25 13:18:47 +11002206 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2208 atomic_dec_and_test(&r10_bio->remaining)) {
2209 /* we have read all the blocks,
2210 * do the comparison in process context in raid10d
2211 */
2212 reschedule_retry(r10_bio);
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Ming Lei81fa1522017-03-17 00:12:32 +08002216static void end_sync_read(struct bio *bio)
2217{
Ming Leif0250612017-03-17 00:12:33 +08002218 struct r10bio *r10_bio = get_resync_r10bio(bio);
Ming Lei81fa1522017-03-17 00:12:32 +08002219 struct r10conf *conf = r10_bio->mddev->private;
2220 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2221
2222 __end_sync_read(r10_bio, bio, d);
2223}
2224
2225static void end_reshape_read(struct bio *bio)
2226{
Ming Leif0250612017-03-17 00:12:33 +08002227 /* reshape read bio isn't allocated from r10buf_pool */
Ming Lei81fa1522017-03-17 00:12:32 +08002228 struct r10bio *r10_bio = bio->bi_private;
2229
2230 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2231}
2232
NeilBrown9f2c9d12011-10-11 16:48:43 +11002233static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002234{
NeilBrownfd01b882011-10-11 16:47:53 +11002235 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10002236
2237 while (atomic_dec_and_test(&r10_bio->remaining)) {
2238 if (r10_bio->master_bio == NULL) {
2239 /* the primary of several recovery bios */
2240 sector_t s = r10_bio->sectors;
2241 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2242 test_bit(R10BIO_WriteError, &r10_bio->state))
2243 reschedule_retry(r10_bio);
2244 else
2245 put_buf(r10_bio);
2246 md_done_sync(mddev, s, 1);
2247 break;
2248 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002249 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10002250 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2251 test_bit(R10BIO_WriteError, &r10_bio->state))
2252 reschedule_retry(r10_bio);
2253 else
2254 put_buf(r10_bio);
2255 r10_bio = r10_bio2;
2256 }
2257 }
2258}
2259
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002260static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Ming Leif0250612017-03-17 00:12:33 +08002262 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrownfd01b882011-10-11 16:47:53 +11002263 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002264 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10002265 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10002266 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002267 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11002268 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
NeilBrown9ad1aef2011-12-23 10:17:55 +11002270 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2271 if (repl)
2272 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11002273 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11002274 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002276 if (bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002277 if (repl)
2278 md_error(mddev, rdev);
2279 else {
2280 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002281 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2282 set_bit(MD_RECOVERY_NEEDED,
2283 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002284 set_bit(R10BIO_WriteError, &r10_bio->state);
2285 }
Yu Kuai3a0f0072024-02-29 17:57:04 +08002286 } else if (rdev_has_badblock(rdev, r10_bio->devs[slot].addr,
2287 r10_bio->sectors)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002288 set_bit(R10BIO_MadeGood, &r10_bio->state);
Yu Kuai3a0f0072024-02-29 17:57:04 +08002289 }
NeilBrowndfc70642008-05-23 13:04:39 -07002290
NeilBrown9ad1aef2011-12-23 10:17:55 +11002291 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002292
2293 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296/*
2297 * Note: sync and recover and handled very differently for raid10
2298 * This code is for resync.
2299 * For resync, we read through virtual addresses and read all blocks.
2300 * If there is any error, we schedule a write. The lowest numbered
2301 * drive is authoritative.
2302 * However requests come for physical address, so we need to map.
2303 * For every physical address there are raid_disks/copies virtual addresses,
2304 * which is always are least one, but is not necessarly an integer.
2305 * This means that a physical address can span multiple chunks, so we may
2306 * have to submit multiple io requests for a single sync request.
2307 */
2308/*
2309 * We check if all blocks are in-sync and only write to blocks that
2310 * aren't in sync
2311 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002312static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
NeilBrowne879a872011-10-11 16:49:02 +11002314 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int i, first;
2316 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002317 int vcnt;
Ming Leicdb76be2017-03-17 00:12:34 +08002318 struct page **tpages, **fpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 atomic_set(&r10_bio->remaining, 1);
2321
2322 /* find the first device with a block */
2323 for (i=0; i<conf->copies; i++)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002324 if (!r10_bio->devs[i].bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 break;
2326
2327 if (i == conf->copies)
2328 goto done;
2329
2330 first = i;
2331 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002332 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2333 fbio->bi_iter.bi_idx = 0;
Ming Leicdb76be2017-03-17 00:12:34 +08002334 fpages = get_resync_pages(fbio)->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
majianpengf4380a92012-04-12 16:04:47 +10002336 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002338 for (i=0 ; i < conf->copies ; i++) {
2339 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002340 struct md_rdev *rdev;
Ming Leif0250612017-03-17 00:12:33 +08002341 struct resync_pages *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002344
2345 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002347 if (i == first)
2348 continue;
Ming Leicdb76be2017-03-17 00:12:34 +08002349
2350 tpages = get_resync_pages(tbio)->pages;
NeilBrown8d3ca832016-11-18 16:16:12 +11002351 d = r10_bio->devs[i].devnum;
2352 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002353 if (!r10_bio->devs[i].bio->bi_status) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002354 /* We know that the bi_io_vec layout is the same for
2355 * both 'first' and 'i', so we just compare them.
2356 * All vec entries are PAGE_SIZE;
2357 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002358 int sectors = r10_bio->sectors;
2359 for (j = 0; j < vcnt; j++) {
2360 int len = PAGE_SIZE;
2361 if (sectors < (len / 512))
2362 len = sectors * 512;
Ming Leicdb76be2017-03-17 00:12:34 +08002363 if (memcmp(page_address(fpages[j]),
2364 page_address(tpages[j]),
NeilBrown7bb23c42013-07-16 16:50:47 +10002365 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002366 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002367 sectors -= len/512;
2368 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002369 if (j == vcnt)
2370 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002371 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002372 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2373 /* Don't fix anything. */
2374 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002375 } else if (test_bit(FailFast, &rdev->flags)) {
2376 /* Just give up on this device */
2377 md_error(rdev->mddev, rdev);
2378 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002379 }
NeilBrownf84ee362011-07-28 11:39:25 +10002380 /* Ok, we need to write this bio, either to correct an
2381 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * First we need to fixup bv_offset, bv_len and
2383 * bi_vecs, as the read request might have corrupted these
2384 */
Ming Leif0250612017-03-17 00:12:33 +08002385 rp = get_resync_pages(tbio);
Christoph Hellwiga7c50c92022-01-24 10:11:07 +01002386 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE);
Kent Overstreet8be185f2012-09-06 14:14:43 -07002387
Ming Leifb0eb5d2017-07-14 16:14:43 +08002388 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2389
Ming Leif0250612017-03-17 00:12:33 +08002390 rp->raid_bio = r10_bio;
2391 tbio->bi_private = rp;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002392 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 tbio->bi_end_io = end_sync_write;
2394
Kent Overstreetc31df252015-05-06 23:34:20 -07002395 bio_copy_data(tbio, fbio);
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2398 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002399 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
NeilBrown1919cbb2016-11-18 16:16:12 +11002401 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2402 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002403 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002404 submit_bio_noacct(tbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406
NeilBrown9ad1aef2011-12-23 10:17:55 +11002407 /* Now write out to any replacement devices
2408 * that are active
2409 */
2410 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002411 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002412
2413 tbio = r10_bio->devs[i].repl_bio;
2414 if (!tbio || !tbio->bi_end_io)
2415 continue;
2416 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2417 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002418 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002419 d = r10_bio->devs[i].devnum;
2420 atomic_inc(&r10_bio->remaining);
2421 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002422 bio_sectors(tbio));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002423 submit_bio_noacct(tbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002424 }
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426done:
2427 if (atomic_dec_and_test(&r10_bio->remaining)) {
2428 md_done_sync(mddev, r10_bio->sectors, 1);
2429 put_buf(r10_bio);
2430 }
2431}
2432
2433/*
2434 * Now for the recovery code.
2435 * Recovery happens across physical sectors.
2436 * We recover all non-is_sync drives by finding the virtual address of
2437 * each, and then choose a working drive that also has that virt address.
2438 * There is a separate r10_bio for each non-in_sync drive.
2439 * Only the first two slots are in use. The first for reading,
2440 * The second for writing.
2441 *
2442 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002443static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002444{
2445 /* We got a read error during recovery.
2446 * We repeat the read in smaller page-sized sections.
2447 * If a read succeeds, write it to the new device or record
2448 * a bad block if we cannot.
2449 * If a read fails, record a bad block on both old and
2450 * new devices.
2451 */
NeilBrownfd01b882011-10-11 16:47:53 +11002452 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002453 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002454 struct bio *bio = r10_bio->devs[0].bio;
2455 sector_t sect = 0;
2456 int sectors = r10_bio->sectors;
2457 int idx = 0;
2458 int dr = r10_bio->devs[0].devnum;
2459 int dw = r10_bio->devs[1].devnum;
Ming Leicdb76be2017-03-17 00:12:34 +08002460 struct page **pages = get_resync_pages(bio)->pages;
NeilBrown5e570282011-07-28 11:39:25 +10002461
2462 while (sectors) {
2463 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002464 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002465 sector_t addr;
2466 int ok;
2467
2468 if (s > (PAGE_SIZE>>9))
2469 s = PAGE_SIZE >> 9;
2470
2471 rdev = conf->mirrors[dr].rdev;
2472 addr = r10_bio->devs[0].addr + sect,
2473 ok = sync_page_io(rdev,
2474 addr,
2475 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002476 pages[idx],
Bart Van Assche4ce4c732022-07-14 11:06:57 -07002477 REQ_OP_READ, false);
NeilBrown5e570282011-07-28 11:39:25 +10002478 if (ok) {
2479 rdev = conf->mirrors[dw].rdev;
2480 addr = r10_bio->devs[1].addr + sect;
2481 ok = sync_page_io(rdev,
2482 addr,
2483 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002484 pages[idx],
Bart Van Assche4ce4c732022-07-14 11:06:57 -07002485 REQ_OP_WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002486 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002487 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002488 if (!test_and_set_bit(WantReplacement,
2489 &rdev->flags))
2490 set_bit(MD_RECOVERY_NEEDED,
2491 &rdev->mddev->recovery);
2492 }
NeilBrown5e570282011-07-28 11:39:25 +10002493 }
2494 if (!ok) {
2495 /* We don't worry if we cannot set a bad block -
2496 * it really is bad so there is no loss in not
2497 * recording it yet
2498 */
2499 rdev_set_badblocks(rdev, addr, s, 0);
2500
2501 if (rdev != conf->mirrors[dw].rdev) {
2502 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002503 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002504 addr = r10_bio->devs[1].addr + sect;
2505 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2506 if (!ok) {
2507 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002508 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2509 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002510
2511 conf->mirrors[dw].recovery_disabled
2512 = mddev->recovery_disabled;
2513 set_bit(MD_RECOVERY_INTR,
2514 &mddev->recovery);
2515 break;
2516 }
2517 }
2518 }
2519
2520 sectors -= s;
2521 sect += s;
2522 idx++;
2523 }
2524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
NeilBrown9f2c9d12011-10-11 16:48:43 +11002526static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
NeilBrowne879a872011-10-11 16:49:02 +11002528 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002529 int d;
Yu Kuai26208a72023-03-10 15:38:53 +08002530 struct bio *wbio = r10_bio->devs[1].bio;
2531 struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2532
2533 /* Need to test wbio2->bi_end_io before we call
2534 * submit_bio_noacct as if the former is NULL,
2535 * the latter is free to free wbio2.
2536 */
2537 if (wbio2 && !wbio2->bi_end_io)
2538 wbio2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
NeilBrown5e570282011-07-28 11:39:25 +10002540 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2541 fix_recovery_read_error(r10_bio);
Yu Kuai26208a72023-03-10 15:38:53 +08002542 if (wbio->bi_end_io)
2543 end_sync_request(r10_bio);
2544 if (wbio2)
2545 end_sync_request(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10002546 return;
2547 }
2548
Namhyung Kimc65060a2011-07-18 17:38:49 +10002549 /*
2550 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * and submit the write request
2552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002554 if (wbio->bi_end_io) {
2555 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002556 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002557 submit_bio_noacct(wbio);
NeilBrown24afd802011-12-23 10:17:55 +11002558 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002559 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002560 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2561 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002562 bio_sectors(wbio2));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002563 submit_bio_noacct(wbio2);
NeilBrown24afd802011-12-23 10:17:55 +11002564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
NeilBrown3cb03002011-10-11 16:45:26 +11002567static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
Bart Van Assche265ad472022-08-10 11:20:12 -07002568 int sectors, struct page *page, enum req_op op)
NeilBrown58c54fc2011-07-28 11:39:25 +10002569{
Yu Kuai3a0f0072024-02-29 17:57:04 +08002570 if (rdev_has_badblock(rdev, sector, sectors) &&
2571 (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags)))
NeilBrown58c54fc2011-07-28 11:39:25 +10002572 return -1;
Bart Van Assche265ad472022-08-10 11:20:12 -07002573 if (sync_page_io(rdev, sector, sectors << 9, page, op, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002574 /* success */
2575 return 1;
Bart Van Assche265ad472022-08-10 11:20:12 -07002576 if (op == REQ_OP_WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002577 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002578 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2579 set_bit(MD_RECOVERY_NEEDED,
2580 &rdev->mddev->recovery);
2581 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002582 /* need to record an error - either for the block or the device */
2583 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2584 md_error(rdev->mddev, rdev);
2585 return 0;
2586}
2587
Robert Becker1e509152009-12-14 12:49:58 +11002588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 * This is a kernel thread which:
2590 *
2591 * 1. Retries failed read operations on working mirrors.
2592 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002593 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 */
2595
NeilBrowne879a872011-10-11 16:49:02 +11002596static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002597{
2598 int sect = 0; /* Offset from r10_bio->sector */
Li Nan605eeda2023-06-24 01:32:34 +08002599 int sectors = r10_bio->sectors, slot = r10_bio->read_slot;
Yufen Yu13db16d2018-04-23 17:37:30 +08002600 struct md_rdev *rdev;
Li Nan605eeda2023-06-24 01:32:34 +08002601 int d = r10_bio->devs[slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002602
NeilBrown7c4e06f2011-05-11 14:53:17 +10002603 /* still own a reference to this rdev, so it cannot
2604 * have been cleared recently.
2605 */
2606 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002607
NeilBrown7c4e06f2011-05-11 14:53:17 +10002608 if (test_bit(Faulty, &rdev->flags))
2609 /* drive has already been failed, just ignore any
2610 more fix_read_error() attempts */
2611 return;
2612
Li Nan1979dbb2023-12-15 10:38:51 +08002613 if (exceed_read_errors(mddev, rdev)) {
Li Nan605eeda2023-06-24 01:32:34 +08002614 r10_bio->devs[slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002615 return;
Robert Becker1e509152009-12-14 12:49:58 +11002616 }
Robert Becker1e509152009-12-14 12:49:58 +11002617
NeilBrown6814d532006-10-03 01:15:45 -07002618 while(sectors) {
2619 int s = sectors;
Li Nan605eeda2023-06-24 01:32:34 +08002620 int sl = slot;
NeilBrown6814d532006-10-03 01:15:45 -07002621 int success = 0;
2622 int start;
2623
2624 if (s > (PAGE_SIZE>>9))
2625 s = PAGE_SIZE >> 9;
2626
NeilBrown6814d532006-10-03 01:15:45 -07002627 do {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002628 d = r10_bio->devs[sl].devnum;
Yu Kuaia448af22023-11-25 16:16:01 +08002629 rdev = conf->mirrors[d].rdev;
NeilBrown6814d532006-10-03 01:15:45 -07002630 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002631 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002632 !test_bit(Faulty, &rdev->flags) &&
Yu Kuai3a0f0072024-02-29 17:57:04 +08002633 rdev_has_badblock(rdev,
2634 r10_bio->devs[sl].addr + sect,
2635 s) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002636 atomic_inc(&rdev->nr_pending);
NeilBrown2b193362010-10-27 15:16:40 +11002637 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002638 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002639 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002640 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002641 conf->tmppage,
Bart Van Assche4ce4c732022-07-14 11:06:57 -07002642 REQ_OP_READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002643 rdev_dec_pending(rdev, mddev);
NeilBrown6814d532006-10-03 01:15:45 -07002644 if (success)
2645 break;
2646 }
2647 sl++;
2648 if (sl == conf->copies)
2649 sl = 0;
Li Nan02c67a32023-06-24 01:32:35 +08002650 } while (sl != slot);
NeilBrown6814d532006-10-03 01:15:45 -07002651
2652 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002653 /* Cannot read from anywhere, just mark the block
2654 * as bad on the first device to discourage future
2655 * reads.
2656 */
Li Nan605eeda2023-06-24 01:32:34 +08002657 int dn = r10_bio->devs[slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002658 rdev = conf->mirrors[dn].rdev;
2659
2660 if (!rdev_set_badblocks(
2661 rdev,
Li Nan605eeda2023-06-24 01:32:34 +08002662 r10_bio->devs[slot].addr
NeilBrown58c54fc2011-07-28 11:39:25 +10002663 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002664 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002665 md_error(mddev, rdev);
Li Nan605eeda2023-06-24 01:32:34 +08002666 r10_bio->devs[slot].bio
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002667 = IO_BLOCKED;
2668 }
NeilBrown6814d532006-10-03 01:15:45 -07002669 break;
2670 }
2671
2672 start = sl;
2673 /* write it back and re-read */
Li Nan605eeda2023-06-24 01:32:34 +08002674 while (sl != slot) {
NeilBrown6814d532006-10-03 01:15:45 -07002675 if (sl==0)
2676 sl = conf->copies;
2677 sl--;
2678 d = r10_bio->devs[sl].devnum;
Yu Kuaia448af22023-11-25 16:16:01 +08002679 rdev = conf->mirrors[d].rdev;
NeilBrown1294b9c2011-07-28 11:39:23 +10002680 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002681 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002682 !test_bit(In_sync, &rdev->flags))
2683 continue;
2684
2685 atomic_inc(&rdev->nr_pending);
NeilBrown58c54fc2011-07-28 11:39:25 +10002686 if (r10_sync_page_io(rdev,
2687 r10_bio->devs[sl].addr +
2688 sect,
Bart Van Assche265ad472022-08-10 11:20:12 -07002689 s, conf->tmppage, REQ_OP_WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002690 == 0) {
2691 /* Well, this device is dead */
Christoph Hellwig913cce52022-05-12 08:19:13 +02002692 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
NeilBrown08464e02016-11-02 14:16:50 +11002693 mdname(mddev), s,
2694 (unsigned long long)(
2695 sect +
2696 choose_data_offset(r10_bio,
2697 rdev)),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002698 rdev->bdev);
2699 pr_notice("md/raid10:%s: %pg: failing drive\n",
NeilBrown08464e02016-11-02 14:16:50 +11002700 mdname(mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002701 rdev->bdev);
NeilBrown6814d532006-10-03 01:15:45 -07002702 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002703 rdev_dec_pending(rdev, mddev);
NeilBrown6814d532006-10-03 01:15:45 -07002704 }
2705 sl = start;
Li Nan605eeda2023-06-24 01:32:34 +08002706 while (sl != slot) {
NeilBrown6814d532006-10-03 01:15:45 -07002707 if (sl==0)
2708 sl = conf->copies;
2709 sl--;
2710 d = r10_bio->devs[sl].devnum;
Yu Kuaia448af22023-11-25 16:16:01 +08002711 rdev = conf->mirrors[d].rdev;
NeilBrown1294b9c2011-07-28 11:39:23 +10002712 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002713 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002714 !test_bit(In_sync, &rdev->flags))
2715 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002716
NeilBrown1294b9c2011-07-28 11:39:23 +10002717 atomic_inc(&rdev->nr_pending);
NeilBrown58c54fc2011-07-28 11:39:25 +10002718 switch (r10_sync_page_io(rdev,
2719 r10_bio->devs[sl].addr +
2720 sect,
Bart Van Assche265ad472022-08-10 11:20:12 -07002721 s, conf->tmppage, REQ_OP_READ)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002722 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002723 /* Well, this device is dead */
Christoph Hellwig913cce52022-05-12 08:19:13 +02002724 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002725 mdname(mddev), s,
2726 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002727 sect +
2728 choose_data_offset(r10_bio, rdev)),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002729 rdev->bdev);
2730 pr_notice("md/raid10:%s: %pg: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002731 mdname(mddev),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002732 rdev->bdev);
NeilBrown58c54fc2011-07-28 11:39:25 +10002733 break;
2734 case 1:
Christoph Hellwig913cce52022-05-12 08:19:13 +02002735 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002736 mdname(mddev), s,
2737 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002738 sect +
2739 choose_data_offset(r10_bio, rdev)),
Christoph Hellwig913cce52022-05-12 08:19:13 +02002740 rdev->bdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002741 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002742 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002743
2744 rdev_dec_pending(rdev, mddev);
NeilBrown6814d532006-10-03 01:15:45 -07002745 }
NeilBrown6814d532006-10-03 01:15:45 -07002746
2747 sectors -= s;
2748 sect += s;
2749 }
2750}
2751
NeilBrown9f2c9d12011-10-11 16:48:43 +11002752static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002753{
2754 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002755 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002756 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002757 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002758 /* bio has the data to be written to slot 'i' where
2759 * we just recently had a write error.
2760 * We repeatedly clone the bio and trim down to one block,
2761 * then try the write. Where the write fails we record
2762 * a bad block.
2763 * It is conceivable that the bio doesn't exactly align with
2764 * blocks. We must handle this.
2765 *
2766 * We currently own a reference to the rdev.
2767 */
2768
2769 int block_sectors;
2770 sector_t sector;
2771 int sectors;
2772 int sect_to_write = r10_bio->sectors;
2773 int ok = 1;
2774
2775 if (rdev->badblocks.shift < 0)
2776 return 0;
2777
NeilBrownf04ebb02015-02-16 14:51:54 +11002778 block_sectors = roundup(1 << rdev->badblocks.shift,
2779 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002780 sector = r10_bio->sector;
2781 sectors = ((r10_bio->sector + block_sectors)
2782 & ~(sector_t)(block_sectors - 1))
2783 - sector;
2784
2785 while (sect_to_write) {
2786 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002787 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002788 if (sectors > sect_to_write)
2789 sectors = sect_to_write;
2790 /* Write at 'sector' for 'sectors' */
Christoph Hellwigabfc4262022-02-02 17:01:09 +01002791 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
2792 &mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002793 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002794 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2795 wbio->bi_iter.bi_sector = wsector +
2796 choose_data_offset(r10_bio, rdev);
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01002797 wbio->bi_opf = REQ_OP_WRITE;
Mike Christie4e49ea42016-06-05 14:31:41 -05002798
2799 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002800 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002801 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002802 sectors, 0)
2803 && ok;
2804
2805 bio_put(wbio);
2806 sect_to_write -= sectors;
2807 sector += sectors;
2808 sectors = block_sectors;
2809 }
2810 return ok;
2811}
2812
NeilBrown9f2c9d12011-10-11 16:48:43 +11002813static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002814{
2815 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002816 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002817 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002818 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002819
2820 /* we got a read error. Maybe the drive is bad. Maybe just
2821 * the block and we can fix it.
2822 * We freeze all other IO, and try reading the block from
2823 * other devices. When we find one, we re-write
2824 * and check it that fixes the read error.
2825 * This is all done synchronously while the array is
2826 * frozen.
2827 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002828 bio = r10_bio->devs[slot].bio;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002829 bio_put(bio);
2830 r10_bio->devs[slot].bio = NULL;
2831
NeilBrown8d3ca832016-11-18 16:16:12 +11002832 if (mddev->ro)
2833 r10_bio->devs[slot].bio = IO_BLOCKED;
2834 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002835 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002836 fix_read_error(conf, mddev, r10_bio);
2837 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002838 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002839 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002840
NeilBrownabbf0982011-12-23 10:17:54 +11002841 rdev_dec_pending(rdev, mddev);
NeilBrown545250f2017-04-05 14:05:51 +10002842 r10_bio->state = 0;
Yu Kuai82045522023-06-22 00:51:07 +08002843 raid10_read_request(mddev, r10_bio->master_bio, r10_bio, false);
Li Nan72c215e2023-02-22 12:09:59 +08002844 /*
2845 * allow_barrier after re-submit to ensure no sync io
2846 * can be issued while regular io pending.
2847 */
2848 allow_barrier(conf);
NeilBrown560f8e52011-07-28 11:39:23 +10002849}
2850
NeilBrowne879a872011-10-11 16:49:02 +11002851static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002852{
2853 /* Some sort of write request has finished and it
2854 * succeeded in writing where we thought there was a
2855 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002856 * Or possibly if failed and we need to record
2857 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002858 */
2859 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002860 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002861
2862 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2863 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002864 for (m = 0; m < conf->copies; m++) {
2865 int dev = r10_bio->devs[m].devnum;
2866 rdev = conf->mirrors[dev].rdev;
Yufen Yu01a69ca2018-02-06 17:39:15 +08002867 if (r10_bio->devs[m].bio == NULL ||
2868 r10_bio->devs[m].bio->bi_end_io == NULL)
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002869 continue;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002870 if (!r10_bio->devs[m].bio->bi_status) {
NeilBrown749c55e2011-07-28 11:39:24 +10002871 rdev_clear_badblocks(
2872 rdev,
2873 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002874 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002875 } else {
2876 if (!rdev_set_badblocks(
2877 rdev,
2878 r10_bio->devs[m].addr,
2879 r10_bio->sectors, 0))
2880 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002881 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002882 rdev = conf->mirrors[dev].replacement;
Yufen Yu01a69ca2018-02-06 17:39:15 +08002883 if (r10_bio->devs[m].repl_bio == NULL ||
2884 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002885 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002886
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002887 if (!r10_bio->devs[m].repl_bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002888 rdev_clear_badblocks(
2889 rdev,
2890 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002891 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002892 } else {
2893 if (!rdev_set_badblocks(
2894 rdev,
2895 r10_bio->devs[m].addr,
2896 r10_bio->sectors, 0))
2897 md_error(conf->mddev, rdev);
2898 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002899 }
NeilBrown749c55e2011-07-28 11:39:24 +10002900 put_buf(r10_bio);
2901 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002902 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002903 for (m = 0; m < conf->copies; m++) {
2904 int dev = r10_bio->devs[m].devnum;
2905 struct bio *bio = r10_bio->devs[m].bio;
2906 rdev = conf->mirrors[dev].rdev;
2907 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002908 rdev_clear_badblocks(
2909 rdev,
2910 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002911 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002912 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002913 } else if (bio != NULL && bio->bi_status) {
NeilBrown95af5872015-08-14 11:26:17 +10002914 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002915 if (!narrow_write_error(r10_bio, m)) {
2916 md_error(conf->mddev, rdev);
2917 set_bit(R10BIO_Degraded,
2918 &r10_bio->state);
2919 }
2920 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002921 }
NeilBrown475b0322011-12-23 10:17:55 +11002922 bio = r10_bio->devs[m].repl_bio;
2923 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002924 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002925 rdev_clear_badblocks(
2926 rdev,
2927 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002928 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002929 rdev_dec_pending(rdev, conf->mddev);
2930 }
NeilBrownbd870a12011-07-28 11:39:24 +10002931 }
NeilBrown95af5872015-08-14 11:26:17 +10002932 if (fail) {
2933 spin_lock_irq(&conf->device_lock);
2934 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002935 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002936 spin_unlock_irq(&conf->device_lock);
Guoqing Jiangcf25ae72017-04-17 17:11:05 +08002937 /*
2938 * In case freeze_array() is waiting for condition
2939 * nr_pending == nr_queued + extra to be true.
2940 */
2941 wake_up(&conf->wait_barrier);
NeilBrown95af5872015-08-14 11:26:17 +10002942 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002943 } else {
2944 if (test_bit(R10BIO_WriteError,
2945 &r10_bio->state))
2946 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002947 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002948 }
NeilBrown749c55e2011-07-28 11:39:24 +10002949 }
2950}
2951
Shaohua Li4ed87312012-10-11 13:34:00 +11002952static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
Shaohua Li4ed87312012-10-11 13:34:00 +11002954 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002955 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002957 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002959 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
NeilBrown95af5872015-08-14 11:26:17 +10002963 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002964 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002965 LIST_HEAD(tmp);
2966 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002967 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002968 while (!list_empty(&conf->bio_end_io_list)) {
2969 list_move(conf->bio_end_io_list.prev, &tmp);
2970 conf->nr_queued--;
2971 }
NeilBrown95af5872015-08-14 11:26:17 +10002972 }
2973 spin_unlock_irqrestore(&conf->device_lock, flags);
2974 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002975 r10_bio = list_first_entry(&tmp, struct r10bio,
2976 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002977 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002978 if (mddev->degraded)
2979 set_bit(R10BIO_Degraded, &r10_bio->state);
2980
2981 if (test_bit(R10BIO_WriteError,
2982 &r10_bio->state))
2983 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002984 raid_end_bio_io(r10_bio);
2985 }
2986 }
2987
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002988 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002990
NeilBrown0021b7b2012-07-31 09:08:14 +02002991 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002994 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002995 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002997 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002998 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08003000 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 spin_unlock_irqrestore(&conf->device_lock, flags);
3002
3003 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10003004 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10003005 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3006 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10003007 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003008 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3009 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10003010 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01003012 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10003014 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10003015 handle_read_error(mddev, r10_bio);
NeilBrownfc9977d2017-04-05 14:05:51 +10003016 else
3017 WARN_ON_ONCE(1);
NeilBrown4443ae12006-01-06 00:20:28 -08003018
NeilBrown1d9d5242009-10-16 15:55:32 +11003019 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08003020 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10003021 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003023 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
NeilBrowne879a872011-10-11 16:49:02 +11003026static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Kent Overstreetafeee512018-05-20 18:25:52 -04003028 int ret, buffs, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Kent Overstreetafeee512018-05-20 18:25:52 -04003031 BUG_ON(mempool_initialized(&conf->r10buf_pool));
NeilBrown69335ef2011-12-23 10:17:54 +11003032 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003033 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11003034 if (conf->mirrors[i].replacement)
3035 conf->have_replacement = 1;
Kent Overstreetafeee512018-05-20 18:25:52 -04003036 ret = mempool_init(&conf->r10buf_pool, buffs,
3037 r10buf_pool_alloc, r10buf_pool_free, conf);
3038 if (ret)
3039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 conf->next_resync = 0;
3041 return 0;
3042}
3043
Shaohua Li208410b2017-08-24 17:50:40 -07003044static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3045{
Kent Overstreetafeee512018-05-20 18:25:52 -04003046 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
Shaohua Li208410b2017-08-24 17:50:40 -07003047 struct rsync_pages *rp;
3048 struct bio *bio;
3049 int nalloc;
3050 int i;
3051
3052 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3053 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3054 nalloc = conf->copies; /* resync */
3055 else
3056 nalloc = 2; /* recovery */
3057
3058 for (i = 0; i < nalloc; i++) {
3059 bio = r10bio->devs[i].bio;
3060 rp = bio->bi_private;
Christoph Hellwiga7c50c92022-01-24 10:11:07 +01003061 bio_reset(bio, NULL, 0);
Shaohua Li208410b2017-08-24 17:50:40 -07003062 bio->bi_private = rp;
3063 bio = r10bio->devs[i].repl_bio;
3064 if (bio) {
3065 rp = bio->bi_private;
Christoph Hellwiga7c50c92022-01-24 10:11:07 +01003066 bio_reset(bio, NULL, 0);
Shaohua Li208410b2017-08-24 17:50:40 -07003067 bio->bi_private = rp;
3068 }
3069 }
3070 return r10bio;
3071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073/*
Guoqing Jiang8db87912017-10-24 15:11:52 +08003074 * Set cluster_sync_high since we need other nodes to add the
3075 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3076 */
3077static void raid10_set_cluster_sync_high(struct r10conf *conf)
3078{
3079 sector_t window_size;
3080 int extra_chunk, chunks;
3081
3082 /*
3083 * First, here we define "stripe" as a unit which across
3084 * all member devices one time, so we get chunks by use
3085 * raid_disks / near_copies. Otherwise, if near_copies is
3086 * close to raid_disks, then resync window could increases
3087 * linearly with the increase of raid_disks, which means
3088 * we will suspend a really large IO window while it is not
3089 * necessary. If raid_disks is not divisible by near_copies,
3090 * an extra chunk is needed to ensure the whole "stripe" is
3091 * covered.
3092 */
3093
3094 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3095 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3096 extra_chunk = 0;
3097 else
3098 extra_chunk = 1;
3099 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3100
3101 /*
3102 * At least use a 32M window to align with raid1's resync window
3103 */
3104 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3105 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3106
3107 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3108}
3109
3110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 * perform a "sync" on one "block"
3112 *
3113 * We need to make sure that no normal I/O request - particularly write
3114 * requests - conflict with active sync requests.
3115 *
3116 * This is achieved by tracking pending requests and a 'barrier' concept
3117 * that can be installed to exclude normal IO requests.
3118 *
3119 * Resync and recovery are handled very differently.
3120 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3121 *
3122 * For resync, we iterate over virtual addresses, read all copies,
3123 * and update if there are differences. If only one copy is live,
3124 * skip it.
3125 * For recovery, we iterate over physical addresses, read a good
3126 * value for each non-in_sync drive, and over-write.
3127 *
3128 * So, for recovery we may have several outstanding complex requests for a
3129 * given address, one for each out-of-sync device. We model this by allocating
3130 * a number of r10_bio structures, one for each out-of-sync device.
3131 * As we setup these structures, we collect all bio's together into a list
3132 * which we then process collectively to add pages, and then process again
Christoph Hellwiged00aab2020-07-01 10:59:44 +02003133 * to pass to submit_bio_noacct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 *
3135 * The r10_bio structures are linked using a borrowed master_bio pointer.
3136 * This link is counted in ->remaining. When the r10_bio that points to NULL
3137 * has its remaining count decremented to 0, the whole complex operation
3138 * is complete.
3139 *
3140 */
3141
Shaohua Li849674e2016-01-20 13:52:20 -08003142static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11003143 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
NeilBrowne879a872011-10-11 16:49:02 +11003145 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003146 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 struct bio *biolist = NULL, *bio;
3148 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08003150 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11003151 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 sector_t sectors_skipped = 0;
3153 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003154 sector_t chunk_mask = conf->geo.chunk_mask;
Ming Lei022e5102017-07-14 16:14:42 +08003155 int page_idx = 0;
Li Nan8d355a42023-06-02 17:18:38 +08003156 int error_disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003158 /*
3159 * Allow skipping a full rebuild for incremental assembly
3160 * of a clean array, like RAID1 does.
3161 */
3162 if (mddev->bitmap == NULL &&
3163 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10003164 mddev->reshape_position == MaxSector &&
3165 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003166 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10003167 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003168 conf->fullsync == 0) {
3169 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10003170 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003171 }
3172
Li Nana405c6f2023-02-22 12:10:00 +08003173 if (!mempool_initialized(&conf->r10buf_pool))
3174 if (init_resync(conf))
3175 return 0;
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11003178 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003179 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3180 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 max_sector = mddev->resync_max_sectors;
3182 if (sector_nr >= max_sector) {
Guoqing Jiang8db87912017-10-24 15:11:52 +08003183 conf->cluster_sync_low = 0;
3184 conf->cluster_sync_high = 0;
3185
NeilBrown6cce3b22006-01-06 00:20:16 -08003186 /* If we aborted, we need to abort the
3187 * sync on the 'current' bitmap chucks (there can
3188 * be several when recovering multiple devices).
3189 * as we may have started syncing it but not finished.
3190 * We can find the current address in
3191 * mddev->curr_resync, but for recovery,
3192 * we need to convert that to several
3193 * virtual addresses.
3194 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10003195 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3196 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10003197 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003198 return 0;
3199 }
3200
NeilBrown6cce3b22006-01-06 00:20:16 -08003201 if (mddev->curr_resync < max_sector) { /* aborted */
3202 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003203 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3204 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003205 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003206 sector_t sect =
3207 raid10_find_virt(conf, mddev->curr_resync, i);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003208 md_bitmap_end_sync(mddev->bitmap, sect,
3209 &sync_blocks, 1);
NeilBrown6cce3b22006-01-06 00:20:16 -08003210 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11003211 } else {
3212 /* completed sync */
3213 if ((!mddev->bitmap || conf->fullsync)
3214 && conf->have_replacement
3215 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3216 /* Completed a full sync so the replacements
3217 * are now fully recovered.
3218 */
NeilBrownf90145f2016-06-02 16:19:52 +10003219 for (i = 0; i < conf->geo.raid_disks; i++) {
3220 struct md_rdev *rdev =
Yu Kuaia448af22023-11-25 16:16:01 +08003221 conf->mirrors[i].replacement;
3222
NeilBrownf90145f2016-06-02 16:19:52 +10003223 if (rdev)
3224 rdev->recovery_offset = MaxSector;
3225 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11003226 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003227 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003228 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003229 md_bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07003231 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return sectors_skipped;
3233 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003234
3235 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3236 return reshape_request(mddev, sector_nr, skipped);
3237
NeilBrown5cf00fc2012-05-21 09:28:20 +10003238 if (chunks_skipped >= conf->geo.raid_disks) {
Li Nan8d355a42023-06-02 17:18:38 +08003239 pr_err("md/raid10:%s: %s fails\n", mdname(mddev),
3240 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? "resync" : "recovery");
3241 if (error_disk >= 0 &&
3242 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3243 /*
3244 * recovery fails, set mirrors.recovery_disabled,
3245 * device shouldn't be added to there.
3246 */
3247 conf->mirrors[error_disk].recovery_disabled =
3248 mddev->recovery_disabled;
3249 return 0;
3250 }
3251 /*
3252 * if there has been nothing to do on any drive,
3253 * then there is nothing to do at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
NeilBrown57afd892005-06-21 17:17:13 -07003255 *skipped = 1;
3256 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258
NeilBrownc6207272008-02-06 01:39:52 -08003259 if (max_sector > mddev->resync_max)
3260 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 /* make sure whole request will fit in a chunk - if chunks
3263 * are meaningful
3264 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003265 if (conf->geo.near_copies < conf->geo.raid_disks &&
3266 max_sector > (sector_nr | chunk_mask))
3267 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003269 /*
3270 * If there is non-resync activity waiting for a turn, then let it
3271 * though before starting on this new sync request.
3272 */
3273 if (conf->nr_waiting)
3274 schedule_timeout_uninterruptible(1);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 /* Again, very different code for resync and recovery.
3277 * Both must result in an r10bio with a list of bios that
Christoph Hellwig309dca302021-01-24 11:02:34 +01003278 * have bi_end_io, bi_sector, bi_bdev set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 * and bi_private set to the r10bio.
3280 * For recovery, we may actually create several r10bios
3281 * with 2 bios in each, that correspond to the bios in the main one.
3282 * In this case, the subordinate r10bios link back through a
3283 * borrowed master_bio pointer, and the counter in the master
3284 * includes a ref from each subordinate.
3285 */
3286 /* First, we decide what to do and set ->bi_end_io
3287 * To end_sync_read if we want to read, and
3288 * end_sync_write if we will want to write.
3289 */
3290
NeilBrown6cce3b22006-01-06 00:20:16 -08003291 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3293 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003294 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 r10_bio = NULL;
3296
NeilBrown5cf00fc2012-05-21 09:28:20 +10003297 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003298 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003299 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003300 sector_t sect;
3301 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003302 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003303 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003304 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003305
Yu Kuaia448af22023-11-25 16:16:01 +08003306 mrdev = mirror->rdev;
3307 mreplace = mirror->replacement;
NeilBrownf90145f2016-06-02 16:19:52 +10003308
Li Nan59f8f0b2023-05-27 15:22:16 +08003309 if (mrdev && (test_bit(Faulty, &mrdev->flags) ||
3310 test_bit(In_sync, &mrdev->flags)))
3311 mrdev = NULL;
Li Nan34817a22023-05-27 15:22:15 +08003312 if (mreplace && test_bit(Faulty, &mreplace->flags))
3313 mreplace = NULL;
Alex Wuee37d732018-09-21 16:05:03 +08003314
Yu Kuaia448af22023-11-25 16:16:01 +08003315 if (!mrdev && !mreplace)
NeilBrownab9d47e2011-05-11 14:54:41 +10003316 continue;
3317
3318 still_degraded = 0;
3319 /* want to reconstruct this device */
3320 rb2 = r10_bio;
3321 sect = raid10_find_virt(conf, sector_nr, i);
Yu Kuaia448af22023-11-25 16:16:01 +08003322 if (sect >= mddev->resync_max_sectors)
NeilBrownfc448a12012-07-03 10:37:30 +10003323 /* last stripe is not complete - don't
3324 * try to recover this sector.
3325 */
3326 continue;
NeilBrown24afd802011-12-23 10:17:55 +11003327 /* Unless we are doing a full sync, or a replacement
3328 * we only need to recover the block if it is set in
3329 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003330 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003331 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3332 &sync_blocks, 1);
NeilBrownab9d47e2011-05-11 14:54:41 +10003333 if (sync_blocks < max_sync)
3334 max_sync = sync_blocks;
3335 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003336 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003337 !conf->fullsync) {
3338 /* yep, skip the sync_blocks here, but don't assume
3339 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003340 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003341 chunks_skipped = -1;
3342 continue;
3343 }
Li Nan59f8f0b2023-05-27 15:22:16 +08003344 if (mrdev)
3345 atomic_inc(&mrdev->nr_pending);
NeilBrownf90145f2016-06-02 16:19:52 +10003346 if (mreplace)
3347 atomic_inc(&mreplace->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Shaohua Li208410b2017-08-24 17:50:40 -07003349 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003350 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003351 raise_barrier(conf, rb2 != NULL);
3352 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
NeilBrownab9d47e2011-05-11 14:54:41 +10003354 r10_bio->master_bio = (struct bio*)rb2;
3355 if (rb2)
3356 atomic_inc(&rb2->remaining);
3357 r10_bio->mddev = mddev;
3358 set_bit(R10BIO_IsRecover, &r10_bio->state);
3359 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003360
NeilBrownab9d47e2011-05-11 14:54:41 +10003361 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003362
NeilBrownab9d47e2011-05-11 14:54:41 +10003363 /* Need to check if the array will still be
3364 * degraded
3365 */
NeilBrownf90145f2016-06-02 16:19:52 +10003366 for (j = 0; j < conf->geo.raid_disks; j++) {
Yu Kuaia448af22023-11-25 16:16:01 +08003367 struct md_rdev *rdev = conf->mirrors[j].rdev;
3368
NeilBrownf90145f2016-06-02 16:19:52 +10003369 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003370 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
NeilBrownf90145f2016-06-02 16:19:52 +10003373 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003374
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003375 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3376 &sync_blocks, still_degraded);
NeilBrownab9d47e2011-05-11 14:54:41 +10003377
NeilBrowne875ece2011-07-28 11:39:24 +10003378 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003379 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003380 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003381 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003382 sector_t from_addr, to_addr;
Yu Kuaia448af22023-11-25 16:16:01 +08003383 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003384 sector_t sector, first_bad;
3385 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003386 if (!rdev ||
3387 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003388 continue;
3389 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003390 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003391 sector = r10_bio->devs[j].addr;
3392
3393 if (is_badblock(rdev, sector, max_sync,
3394 &first_bad, &bad_sectors)) {
3395 if (first_bad > sector)
3396 max_sync = first_bad - sector;
3397 else {
3398 bad_sectors -= (sector
3399 - first_bad);
3400 if (max_sync > bad_sectors)
3401 max_sync = bad_sectors;
3402 continue;
3403 }
3404 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003405 bio = r10_bio->devs[0].bio;
3406 bio->bi_next = biolist;
3407 biolist = bio;
NeilBrownab9d47e2011-05-11 14:54:41 +10003408 bio->bi_end_io = end_sync_read;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003409 bio->bi_opf = REQ_OP_READ;
NeilBrown8d3ca832016-11-18 16:16:12 +11003410 if (test_bit(FailFast, &rdev->flags))
3411 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003412 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003413 bio->bi_iter.bi_sector = from_addr +
3414 rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003415 bio_set_dev(bio, rdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003416 atomic_inc(&rdev->nr_pending);
3417 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003418
3419 for (k=0; k<conf->copies; k++)
3420 if (r10_bio->devs[k].devnum == i)
3421 break;
3422 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003423 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003424 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003425 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003426 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003427 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003428
Li Nan59f8f0b2023-05-27 15:22:16 +08003429 if (mrdev) {
NeilBrown24afd802011-12-23 10:17:55 +11003430 bio = r10_bio->devs[1].bio;
3431 bio->bi_next = biolist;
3432 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003433 bio->bi_end_io = end_sync_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003434 bio->bi_opf = REQ_OP_WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003435 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003436 + mrdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003437 bio_set_dev(bio, mrdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003438 atomic_inc(&r10_bio->remaining);
3439 } else
3440 r10_bio->devs[1].bio->bi_end_io = NULL;
3441
3442 /* and maybe write to replacement */
3443 bio = r10_bio->devs[1].repl_bio;
3444 if (bio)
3445 bio->bi_end_io = NULL;
Li Nan34817a22023-05-27 15:22:15 +08003446 /* Note: if replace is not NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003447 * cannot be NULL as r10buf_pool_alloc will
3448 * have allocated it.
NeilBrown24afd802011-12-23 10:17:55 +11003449 */
Li Nan34817a22023-05-27 15:22:15 +08003450 if (!mreplace)
NeilBrown24afd802011-12-23 10:17:55 +11003451 break;
3452 bio->bi_next = biolist;
3453 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003454 bio->bi_end_io = end_sync_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003455 bio->bi_opf = REQ_OP_WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003456 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003457 mreplace->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003458 bio_set_dev(bio, mreplace->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003459 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003462 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003463 /* Cannot recover, so abort the recovery or
3464 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003465 if (any_working) {
3466 /* problem is that there are bad blocks
3467 * on other device(s)
3468 */
3469 int k;
3470 for (k = 0; k < conf->copies; k++)
3471 if (r10_bio->devs[k].devnum == i)
3472 break;
Li Nan59f8f0b2023-05-27 15:22:16 +08003473 if (mrdev && !test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003474 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003475 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003476 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003477 r10_bio->devs[k].addr,
3478 max_sync, 0))
3479 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003480 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003481 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003482 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003483 r10_bio->devs[k].addr,
3484 max_sync, 0))
3485 any_working = 0;
3486 }
3487 if (!any_working) {
3488 if (!test_and_set_bit(MD_RECOVERY_INTR,
3489 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003490 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003491 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003492 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003493 = mddev->recovery_disabled;
Li Nan8d355a42023-06-02 17:18:38 +08003494 } else {
3495 error_disk = i;
NeilBrowne875ece2011-07-28 11:39:24 +10003496 }
NeilBrowne8b84912014-01-06 10:35:34 +11003497 put_buf(r10_bio);
3498 if (rb2)
3499 atomic_dec(&rb2->remaining);
3500 r10_bio = rb2;
Li Nan59f8f0b2023-05-27 15:22:16 +08003501 if (mrdev)
3502 rdev_dec_pending(mrdev, mddev);
NeilBrownf90145f2016-06-02 16:19:52 +10003503 if (mreplace)
3504 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003505 break;
3506 }
Li Nan59f8f0b2023-05-27 15:22:16 +08003507 if (mrdev)
3508 rdev_dec_pending(mrdev, mddev);
NeilBrownf90145f2016-06-02 16:19:52 +10003509 if (mreplace)
3510 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003511 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3512 /* Only want this if there is elsewhere to
3513 * read from. 'j' is currently the first
3514 * readable copy.
3515 */
3516 int targets = 1;
3517 for (; j < conf->copies; j++) {
3518 int d = r10_bio->devs[j].devnum;
3519 if (conf->mirrors[d].rdev &&
3520 test_bit(In_sync,
3521 &conf->mirrors[d].rdev->flags))
3522 targets++;
3523 }
3524 if (targets == 1)
3525 r10_bio->devs[0].bio->bi_opf
3526 &= ~MD_FAILFAST;
3527 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (biolist == NULL) {
3530 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003531 struct r10bio *rb2 = r10_bio;
3532 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 rb2->master_bio = NULL;
3534 put_buf(rb2);
3535 }
3536 goto giveup;
3537 }
3538 } else {
3539 /* resync. Schedule a read for every block at this virt offset */
3540 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003541
Guoqing Jiang8db87912017-10-24 15:11:52 +08003542 /*
3543 * Since curr_resync_completed could probably not update in
3544 * time, and we will set cluster_sync_low based on it.
3545 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3546 * safety reason, which ensures curr_resync_completed is
3547 * updated in bitmap_cond_end_sync.
3548 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003549 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3550 mddev_is_clustered(mddev) &&
3551 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown78200d42009-02-25 13:18:47 +11003552
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003553 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3554 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003555 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3556 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003557 /* We can skip this block */
3558 *skipped = 1;
3559 return sync_blocks + sectors_skipped;
3560 }
3561 if (sync_blocks < max_sync)
3562 max_sync = sync_blocks;
Shaohua Li208410b2017-08-24 17:50:40 -07003563 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003564 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 r10_bio->mddev = mddev;
3567 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003568 raise_barrier(conf, 0);
3569 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 r10_bio->master_bio = NULL;
3572 r10_bio->sector = sector_nr;
3573 set_bit(R10BIO_IsSync, &r10_bio->state);
3574 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003575 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
NeilBrown5cf00fc2012-05-21 09:28:20 +10003577 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003579 sector_t first_bad, sector;
3580 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003581 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003582
NeilBrown9ad1aef2011-12-23 10:17:55 +11003583 if (r10_bio->devs[i].repl_bio)
3584 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 bio = r10_bio->devs[i].bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003587 bio->bi_status = BLK_STS_IOERR;
Yu Kuaia448af22023-11-25 16:16:01 +08003588 rdev = conf->mirrors[d].rdev;
3589 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 continue;
Yu Kuaia448af22023-11-25 16:16:01 +08003591
NeilBrown40c356c2011-07-28 11:39:24 +10003592 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003593 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003594 &first_bad, &bad_sectors)) {
3595 if (first_bad > sector)
3596 max_sync = first_bad - sector;
3597 else {
3598 bad_sectors -= (sector - first_bad);
3599 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003600 max_sync = bad_sectors;
NeilBrown40c356c2011-07-28 11:39:24 +10003601 continue;
3602 }
3603 }
NeilBrownf90145f2016-06-02 16:19:52 +10003604 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 atomic_inc(&r10_bio->remaining);
3606 bio->bi_next = biolist;
3607 biolist = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 bio->bi_end_io = end_sync_read;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003609 bio->bi_opf = REQ_OP_READ;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003610 if (test_bit(FailFast, &rdev->flags))
NeilBrown8d3ca832016-11-18 16:16:12 +11003611 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003612 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003613 bio_set_dev(bio, rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003615
Yu Kuaia448af22023-11-25 16:16:01 +08003616 rdev = conf->mirrors[d].replacement;
3617 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown9ad1aef2011-12-23 10:17:55 +11003618 continue;
Yu Kuaia448af22023-11-25 16:16:01 +08003619
NeilBrownf90145f2016-06-02 16:19:52 +10003620 atomic_inc(&rdev->nr_pending);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003621
3622 /* Need to set up for writing to the replacement */
3623 bio = r10_bio->devs[i].repl_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003624 bio->bi_status = BLK_STS_IOERR;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003625
3626 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003627 bio->bi_next = biolist;
3628 biolist = bio;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003629 bio->bi_end_io = end_sync_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01003630 bio->bi_opf = REQ_OP_WRITE;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003631 if (test_bit(FailFast, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +11003632 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003633 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003634 bio_set_dev(bio, rdev->bdev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003635 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637
3638 if (count < 2) {
3639 for (i=0; i<conf->copies; i++) {
3640 int d = r10_bio->devs[i].devnum;
3641 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003642 rdev_dec_pending(conf->mirrors[d].rdev,
3643 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003644 if (r10_bio->devs[i].repl_bio &&
3645 r10_bio->devs[i].repl_bio->bi_end_io)
3646 rdev_dec_pending(
3647 conf->mirrors[d].replacement,
3648 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650 put_buf(r10_bio);
3651 biolist = NULL;
3652 goto giveup;
3653 }
3654 }
3655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003657 if (sector_nr + max_sync < max_sector)
3658 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 do {
3660 struct page *page;
3661 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if (sector_nr + (len>>9) > max_sector)
3663 len = (max_sector - sector_nr) << 9;
3664 if (len == 0)
3665 break;
3666 for (bio= biolist ; bio ; bio=bio->bi_next) {
Ming Leif0250612017-03-17 00:12:33 +08003667 struct resync_pages *rp = get_resync_pages(bio);
Ming Lei022e5102017-07-14 16:14:42 +08003668 page = resync_fetch_page(rp, page_idx);
Johannes Thumshirn0c67dd62023-05-31 04:50:38 -07003669 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
3670 bio->bi_status = BLK_STS_RESOURCE;
3671 bio_endio(bio);
3672 goto giveup;
3673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
3675 nr_sectors += len>>9;
3676 sector_nr += len>>9;
Ming Lei022e5102017-07-14 16:14:42 +08003677 } while (++page_idx < RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 r10_bio->sectors = nr_sectors;
3679
Guoqing Jiang8db87912017-10-24 15:11:52 +08003680 if (mddev_is_clustered(mddev) &&
3681 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3682 /* It is resync not recovery */
3683 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3684 conf->cluster_sync_low = mddev->curr_resync_completed;
3685 raid10_set_cluster_sync_high(conf);
3686 /* Send resync message */
3687 md_cluster_ops->resync_info_update(mddev,
3688 conf->cluster_sync_low,
3689 conf->cluster_sync_high);
3690 }
3691 } else if (mddev_is_clustered(mddev)) {
3692 /* This is recovery not resync */
3693 sector_t sect_va1, sect_va2;
3694 bool broadcast_msg = false;
3695
3696 for (i = 0; i < conf->geo.raid_disks; i++) {
3697 /*
3698 * sector_nr is a device address for recovery, so we
3699 * need translate it to array address before compare
3700 * with cluster_sync_high.
3701 */
3702 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3703
3704 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3705 broadcast_msg = true;
3706 /*
3707 * curr_resync_completed is similar as
3708 * sector_nr, so make the translation too.
3709 */
3710 sect_va2 = raid10_find_virt(conf,
3711 mddev->curr_resync_completed, i);
3712
3713 if (conf->cluster_sync_low == 0 ||
3714 conf->cluster_sync_low > sect_va2)
3715 conf->cluster_sync_low = sect_va2;
3716 }
3717 }
3718 if (broadcast_msg) {
3719 raid10_set_cluster_sync_high(conf);
3720 md_cluster_ops->resync_info_update(mddev,
3721 conf->cluster_sync_low,
3722 conf->cluster_sync_high);
3723 }
3724 }
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 while (biolist) {
3727 bio = biolist;
3728 biolist = biolist->bi_next;
3729
3730 bio->bi_next = NULL;
Ming Leif0250612017-03-17 00:12:33 +08003731 r10_bio = get_resync_r10bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 r10_bio->sectors = nr_sectors;
3733
3734 if (bio->bi_end_io == end_sync_read) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02003735 md_sync_acct_bio(bio, nr_sectors);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003736 bio->bi_status = 0;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02003737 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
3739 }
3740
NeilBrown57afd892005-06-21 17:17:13 -07003741 if (sectors_skipped)
3742 /* pretend they weren't skipped, it makes
3743 * no important difference in this case
3744 */
3745 md_done_sync(mddev, sectors_skipped, 1);
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return sectors_skipped + nr_sectors;
3748 giveup:
3749 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003750 * drives must be failed or in resync, all drives
3751 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 */
NeilBrown09b40682009-02-25 13:18:47 +11003753 if (sector_nr + max_sync < max_sector)
3754 max_sector = sector_nr + max_sync;
3755
3756 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 chunks_skipped ++;
3758 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760}
3761
Dan Williams80c3a6c2009-03-17 18:10:40 -07003762static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003763raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003764{
3765 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003766 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003767
3768 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003769 raid_disks = min(conf->geo.raid_disks,
3770 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003771 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003772 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003773
NeilBrown5cf00fc2012-05-21 09:28:20 +10003774 size = sectors >> conf->geo.chunk_shift;
3775 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003776 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003777 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003778
NeilBrown5cf00fc2012-05-21 09:28:20 +10003779 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003780}
3781
NeilBrown6508fdb2012-05-17 10:08:45 +10003782static void calc_sectors(struct r10conf *conf, sector_t size)
3783{
3784 /* Calculate the number of sectors-per-device that will
3785 * actually be used, and set conf->dev_sectors and
3786 * conf->stride
3787 */
3788
NeilBrown5cf00fc2012-05-21 09:28:20 +10003789 size = size >> conf->geo.chunk_shift;
3790 sector_div(size, conf->geo.far_copies);
3791 size = size * conf->geo.raid_disks;
3792 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003793 /* 'size' is now the number of chunks in the array */
3794 /* calculate "used chunks per device" */
3795 size = size * conf->copies;
3796
3797 /* We need to round up when dividing by raid_disks to
3798 * get the stride size.
3799 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003800 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003801
NeilBrown5cf00fc2012-05-21 09:28:20 +10003802 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003803
NeilBrown5cf00fc2012-05-21 09:28:20 +10003804 if (conf->geo.far_offset)
3805 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003806 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003807 sector_div(size, conf->geo.far_copies);
3808 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003809 }
3810}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003811
NeilBrowndeb200d2012-05-21 09:28:33 +10003812enum geo_type {geo_new, geo_old, geo_start};
3813static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3814{
3815 int nc, fc, fo;
3816 int layout, chunk, disks;
3817 switch (new) {
3818 case geo_old:
3819 layout = mddev->layout;
3820 chunk = mddev->chunk_sectors;
3821 disks = mddev->raid_disks - mddev->delta_disks;
3822 break;
3823 case geo_new:
3824 layout = mddev->new_layout;
3825 chunk = mddev->new_chunk_sectors;
3826 disks = mddev->raid_disks;
3827 break;
3828 default: /* avoid 'may be unused' warnings */
3829 case geo_start: /* new when starting reshape - raid_disks not
3830 * updated yet. */
3831 layout = mddev->new_layout;
3832 chunk = mddev->new_chunk_sectors;
3833 disks = mddev->raid_disks + mddev->delta_disks;
3834 break;
3835 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003836 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003837 return -1;
3838 if (chunk < (PAGE_SIZE >> 9) ||
3839 !is_power_of_2(chunk))
3840 return -2;
3841 nc = layout & 255;
3842 fc = (layout >> 8) & 255;
3843 fo = layout & (1<<16);
3844 geo->raid_disks = disks;
3845 geo->near_copies = nc;
3846 geo->far_copies = fc;
3847 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003848 switch (layout >> 17) {
3849 case 0: /* original layout. simple but not always optimal */
3850 geo->far_set_size = disks;
3851 break;
3852 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3853 * actually using this, but leave code here just in case.*/
3854 geo->far_set_size = disks/fc;
3855 WARN(geo->far_set_size < fc,
3856 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3857 break;
3858 case 2: /* "improved" layout fixed to match documentation */
3859 geo->far_set_size = fc * nc;
3860 break;
3861 default: /* Not a valid layout */
3862 return -1;
3863 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003864 geo->chunk_mask = chunk - 1;
3865 geo->chunk_shift = ffz(~chunk);
3866 return nc*fc;
3867}
3868
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08003869static void raid10_free_conf(struct r10conf *conf)
3870{
3871 if (!conf)
3872 return;
3873
3874 mempool_exit(&conf->r10bio_pool);
3875 kfree(conf->mirrors);
3876 kfree(conf->mirrors_old);
3877 kfree(conf->mirrors_new);
3878 safe_put_page(conf->tmppage);
3879 bioset_exit(&conf->bio_split);
3880 kfree(conf);
3881}
3882
NeilBrowne879a872011-10-11 16:49:02 +11003883static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
NeilBrowne879a872011-10-11 16:49:02 +11003885 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003886 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003887 struct geom geo;
3888 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
NeilBrowndeb200d2012-05-21 09:28:33 +10003890 copies = setup_geo(&geo, mddev, geo_new);
3891
3892 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003893 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3894 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003895 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
NeilBrown2604b702006-01-06 00:20:36 -08003897
NeilBrowndeb200d2012-05-21 09:28:33 +10003898 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003899 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3900 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 goto out;
3902 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003903
3904 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003905 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003906 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003908
NeilBrown3ea7daa2012-05-22 13:53:47 +10003909 /* FIXME calc properly */
Kees Cook6396bb22018-06-12 14:03:40 -07003910 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
3911 sizeof(struct raid10_info),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003912 GFP_KERNEL);
3913 if (!conf->mirrors)
3914 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003915
3916 conf->tmppage = alloc_page(GFP_KERNEL);
3917 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003918 goto out;
3919
NeilBrowndeb200d2012-05-21 09:28:33 +10003920 conf->geo = geo;
3921 conf->copies = copies;
Marcos Paulo de Souza3f677f92019-06-14 15:41:04 -07003922 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -07003923 rbio_pool_free, conf);
Kent Overstreetafeee512018-05-20 18:25:52 -04003924 if (err)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003925 goto out;
3926
Kent Overstreetafeee512018-05-20 18:25:52 -04003927 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
3928 if (err)
NeilBrownfc9977d2017-04-05 14:05:51 +10003929 goto out;
3930
NeilBrown6508fdb2012-05-17 10:08:45 +10003931 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003932 if (mddev->reshape_position == MaxSector) {
3933 conf->prev = conf->geo;
3934 conf->reshape_progress = MaxSector;
3935 } else {
3936 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3937 err = -EINVAL;
3938 goto out;
3939 }
3940 conf->reshape_progress = mddev->reshape_position;
3941 if (conf->prev.far_offset)
3942 conf->prev.stride = 1 << conf->prev.chunk_shift;
3943 else
3944 /* far_copies must be 1 */
3945 conf->prev.stride = conf->dev_sectors;
3946 }
NeilBrown299b0682015-07-06 17:37:49 +10003947 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003948 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003949 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003950 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003951
Yu Kuaib9b083f2022-09-16 19:34:28 +08003952 seqlock_init(&conf->resync_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003953 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003954 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003955
Kent Overstreetafeee512018-05-20 18:25:52 -04003956 err = -ENOMEM;
Yu Kuai44693152023-05-23 10:10:17 +08003957 rcu_assign_pointer(conf->thread,
3958 md_register_thread(raid10d, mddev, "raid10"));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003959 if (!conf->thread)
3960 goto out;
3961
Trela, Maciejdab8b292010-03-08 16:02:45 +11003962 conf->mddev = mddev;
3963 return conf;
3964
3965 out:
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08003966 raid10_free_conf(conf);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003967 return ERR_PTR(err);
3968}
3969
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07003970static unsigned int raid10_nr_stripes(struct r10conf *conf)
Christoph Hellwig16ef5102020-09-24 08:51:33 +02003971{
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07003972 unsigned int raid_disks = conf->geo.raid_disks;
Christoph Hellwig16ef5102020-09-24 08:51:33 +02003973
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07003974 if (conf->geo.raid_disks % conf->geo.near_copies)
3975 return raid_disks;
3976 return raid_disks / conf->geo.near_copies;
3977}
3978
3979static int raid10_set_queue_limits(struct mddev *mddev)
3980{
3981 struct r10conf *conf = mddev->private;
3982 struct queue_limits lim;
3983
3984 blk_set_stacking_limits(&lim);
3985 lim.max_write_zeroes_sectors = 0;
3986 lim.io_min = mddev->chunk_sectors << 9;
3987 lim.io_opt = lim.io_min * raid10_nr_stripes(conf);
3988 mddev_stack_rdev_limits(mddev, &lim);
Christoph Hellwig396799e2024-03-03 07:01:49 -07003989 return queue_limits_set(mddev->gendisk->queue, &lim);
Christoph Hellwig16ef5102020-09-24 08:51:33 +02003990}
3991
Shaohua Li849674e2016-01-20 13:52:20 -08003992static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003993{
NeilBrowne879a872011-10-11 16:49:02 +11003994 struct r10conf *conf;
Christoph Hellwig16ef5102020-09-24 08:51:33 +02003995 int i, disk_idx;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003996 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003997 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003998 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003999 sector_t min_offset_diff = 0;
4000 int first = 1;
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07004001 int ret = -EIO;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004002
4003 if (mddev->private == NULL) {
4004 conf = setup_conf(mddev);
4005 if (IS_ERR(conf))
4006 return PTR_ERR(conf);
4007 mddev->private = conf;
4008 }
4009 conf = mddev->private;
4010 if (!conf)
4011 goto out;
4012
Yu Kuai44693152023-05-23 10:10:17 +08004013 rcu_assign_pointer(mddev->thread, conf->thread);
4014 rcu_assign_pointer(conf->thread, NULL);
Yu Kuaif0ddb832023-03-10 15:38:55 +08004015
Guoqing Jiang8db87912017-10-24 15:11:52 +08004016 if (mddev_is_clustered(conf->mddev)) {
4017 int fc, fo;
4018
4019 fc = (mddev->layout >> 8) & 255;
4020 fo = mddev->layout & (1<<16);
4021 if (fc > 1 || fo > 0) {
4022 pr_err("only near layout is supported by clustered"
4023 " raid10\n");
Lidong Zhong43a52122018-01-23 23:06:12 +08004024 goto out_free_conf;
Guoqing Jiang8db87912017-10-24 15:11:52 +08004025 }
4026 }
4027
NeilBrowndafb20f2012-03-19 12:46:39 +11004028 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004029 long long diff;
NeilBrown34b343c2011-07-28 11:31:47 +10004030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10004032 if (disk_idx < 0)
4033 continue;
4034 if (disk_idx >= conf->geo.raid_disks &&
4035 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 continue;
4037 disk = conf->mirrors + disk_idx;
4038
NeilBrown56a2559b2011-12-23 10:17:55 +11004039 if (test_bit(Replacement, &rdev->flags)) {
4040 if (disk->replacement)
4041 goto out_free_conf;
4042 disk->replacement = rdev;
4043 } else {
4044 if (disk->rdev)
4045 goto out_free_conf;
4046 disk->rdev = rdev;
4047 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004048 diff = (rdev->new_data_offset - rdev->data_offset);
4049 if (!mddev->reshape_backwards)
4050 diff = -diff;
4051 if (diff < 0)
4052 diff = 0;
4053 if (first || diff < min_offset_diff)
4054 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 disk->head_position = 0;
Guoqing Jiang6f287ca2017-04-06 09:12:18 +08004057 first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004059
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07004060 if (!mddev_is_dm(conf->mddev)) {
4061 ret = raid10_set_queue_limits(mddev);
4062 if (ret)
4063 goto out_free_conf;
4064 }
4065
NeilBrown6d508242005-09-09 16:24:03 -07004066 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10004067 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11004068 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07004069 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 goto out_free_conf;
4071 }
4072
NeilBrown3ea7daa2012-05-22 13:53:47 +10004073 if (conf->reshape_progress != MaxSector) {
4074 /* must ensure that shape change is supported */
4075 if (conf->geo.far_copies != 1 &&
4076 conf->geo.far_offset == 0)
4077 goto out_free_conf;
4078 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10004079 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004080 goto out_free_conf;
4081 }
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10004084 for (i = 0;
4085 i < conf->geo.raid_disks
4086 || i < conf->prev.raid_disks;
4087 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 disk = conf->mirrors + i;
4090
NeilBrown56a2559b2011-12-23 10:17:55 +11004091 if (!disk->rdev && disk->replacement) {
4092 /* The replacement is all we have - use it */
4093 disk->rdev = disk->replacement;
4094 disk->replacement = NULL;
4095 clear_bit(Replacement, &disk->rdev->flags);
4096 }
4097
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004098 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07004099 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 disk->head_position = 0;
4101 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11004102 if (disk->rdev &&
4103 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10004104 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
BingJing Changbda31532018-06-28 18:40:11 +08004106
4107 if (disk->replacement &&
4108 !test_bit(In_sync, &disk->replacement->flags) &&
4109 disk->replacement->saved_raid_disk < 0) {
4110 conf->fullsync = 1;
4111 }
4112
NeilBrownd890fa22011-10-26 11:54:39 +11004113 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 }
4115
Andre Noll8c6ac8682009-06-18 08:48:06 +10004116 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11004117 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4118 mdname(mddev));
4119 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10004120 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4121 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 /*
4123 * Ok, everything is just fine now
4124 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11004125 mddev->dev_sectors = conf->dev_sectors;
4126 size = raid10_size(mddev, 0, 0);
4127 md_set_array_sectors(mddev, size);
4128 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11004129 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Martin K. Petersena91a2782011-03-17 11:11:05 +01004131 if (md_integrity_register(mddev))
4132 goto out_free_conf;
4133
NeilBrown3ea7daa2012-05-22 13:53:47 +10004134 if (conf->reshape_progress != MaxSector) {
4135 unsigned long before_length, after_length;
4136
4137 before_length = ((1 << conf->prev.chunk_shift) *
4138 conf->prev.far_copies);
4139 after_length = ((1 << conf->geo.chunk_shift) *
4140 conf->geo.far_copies);
4141
4142 if (max(before_length, after_length) > min_offset_diff) {
4143 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11004144 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10004145 goto out_free_conf;
4146 }
4147 conf->offset_diff = min_offset_diff;
4148
NeilBrown3ea7daa2012-05-22 13:53:47 +10004149 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4150 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4151 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Yu Kuaiad39c082024-02-01 17:25:49 +08004152 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004153 }
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return 0;
4156
4157out_free_conf:
Li Lingfeng7eb8ff02023-08-03 15:17:11 +08004158 md_unregister_thread(mddev, &mddev->thread);
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08004159 raid10_free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 mddev->private = NULL;
4161out:
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07004162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163}
4164
NeilBrownafa0f552014-12-15 12:56:58 +11004165static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Yu Kuaic9ac2ac2023-03-10 15:38:54 +08004167 raid10_free_conf(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168}
4169
NeilBrownb03e0cc2017-10-19 12:49:15 +11004170static void raid10_quiesce(struct mddev *mddev, int quiesce)
NeilBrown6cce3b22006-01-06 00:20:16 -08004171{
NeilBrowne879a872011-10-11 16:49:02 +11004172 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08004173
NeilBrownb03e0cc2017-10-19 12:49:15 +11004174 if (quiesce)
NeilBrown6cce3b22006-01-06 00:20:16 -08004175 raise_barrier(conf, 0);
NeilBrownb03e0cc2017-10-19 12:49:15 +11004176 else
NeilBrown6cce3b22006-01-06 00:20:16 -08004177 lower_barrier(conf);
NeilBrown6cce3b22006-01-06 00:20:16 -08004178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
NeilBrown006a09a2012-03-19 12:46:40 +11004180static int raid10_resize(struct mddev *mddev, sector_t sectors)
4181{
4182 /* Resize of 'far' arrays is not supported.
4183 * For 'near' and 'offset' arrays we can set the
4184 * number of sectors used to be an appropriate multiple
4185 * of the chunk size.
4186 * For 'offset', this is far_copies*chunksize.
4187 * For 'near' the multiplier is the LCM of
4188 * near_copies and raid_disks.
4189 * So if far_copies > 1 && !far_offset, fail.
4190 * Else find LCM(raid_disks, near_copy)*far_copies and
4191 * multiply by chunk_size. Then round to this number.
4192 * This is mostly done by raid10_size()
4193 */
4194 struct r10conf *conf = mddev->private;
4195 sector_t oldsize, size;
4196
NeilBrownf8c9e742012-05-21 09:28:33 +10004197 if (mddev->reshape_position != MaxSector)
4198 return -EBUSY;
4199
NeilBrown5cf00fc2012-05-21 09:28:20 +10004200 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11004201 return -EINVAL;
4202
4203 oldsize = raid10_size(mddev, 0, 0);
4204 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10004205 if (mddev->external_size &&
4206 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11004207 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10004208 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004209 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10004210 if (ret)
4211 return ret;
4212 }
4213 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11004214 if (sectors > mddev->dev_sectors &&
4215 mddev->recovery_cp > oldsize) {
4216 mddev->recovery_cp = oldsize;
4217 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4218 }
NeilBrown6508fdb2012-05-17 10:08:45 +10004219 calc_sectors(conf, sectors);
4220 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11004221 mddev->resync_max_sectors = size;
4222 return 0;
4223}
4224
NeilBrown53a6ab42015-02-12 14:09:57 +11004225static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004226{
NeilBrown3cb03002011-10-11 16:45:26 +11004227 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11004228 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004229
4230 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11004231 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4232 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004233 return ERR_PTR(-EINVAL);
4234 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004235 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004236
Trela, Maciejdab8b292010-03-08 16:02:45 +11004237 /* Set new parameters */
4238 mddev->new_level = 10;
4239 /* new layout: far_copies = 1, near_copies = 2 */
4240 mddev->new_layout = (1<<8) + 2;
4241 mddev->new_chunk_sectors = mddev->chunk_sectors;
4242 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004243 mddev->raid_disks *= 2;
4244 /* make sure it will be not marked as dirty */
4245 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11004246 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004247
4248 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004249 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11004250 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11004251 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004252 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11004253 rdev->sectors = size;
4254 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004255 }
4256
Trela, Maciejdab8b292010-03-08 16:02:45 +11004257 return conf;
4258}
4259
NeilBrownfd01b882011-10-11 16:47:53 +11004260static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004261{
NeilBrowne373ab12011-10-11 16:48:59 +11004262 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004263
4264 /* raid10 can take over:
4265 * raid0 - providing it has only two drives
4266 */
4267 if (mddev->level == 0) {
4268 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11004269 raid0_conf = mddev->private;
4270 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11004271 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4272 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004273 return ERR_PTR(-EINVAL);
4274 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004275 return raid10_takeover_raid0(mddev,
4276 raid0_conf->strip_zone->zone_end,
4277 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004278 }
4279 return ERR_PTR(-EINVAL);
4280}
4281
NeilBrown3ea7daa2012-05-22 13:53:47 +10004282static int raid10_check_reshape(struct mddev *mddev)
4283{
4284 /* Called when there is a request to change
4285 * - layout (to ->new_layout)
4286 * - chunk size (to ->new_chunk_sectors)
4287 * - raid_disks (by delta_disks)
4288 * or when trying to restart a reshape that was ongoing.
4289 *
4290 * We need to validate the request and possibly allocate
4291 * space if that might be an issue later.
4292 *
4293 * Currently we reject any reshape of a 'far' mode array,
4294 * allow chunk size to change if new is generally acceptable,
4295 * allow raid_disks to increase, and allow
4296 * a switch between 'near' mode and 'offset' mode.
4297 */
4298 struct r10conf *conf = mddev->private;
4299 struct geom geo;
4300
4301 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4302 return -EINVAL;
4303
4304 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4305 /* mustn't change number of copies */
4306 return -EINVAL;
4307 if (geo.far_copies > 1 && !geo.far_offset)
4308 /* Cannot switch to 'far' mode */
4309 return -EINVAL;
4310
4311 if (mddev->array_sectors & geo.chunk_mask)
4312 /* not factor of array size */
4313 return -EINVAL;
4314
NeilBrown3ea7daa2012-05-22 13:53:47 +10004315 if (!enough(conf, -1))
4316 return -EINVAL;
4317
4318 kfree(conf->mirrors_new);
4319 conf->mirrors_new = NULL;
4320 if (mddev->delta_disks > 0) {
4321 /* allocate new 'mirrors' list */
Kees Cook6396bb22018-06-12 14:03:40 -07004322 conf->mirrors_new =
4323 kcalloc(mddev->raid_disks + mddev->delta_disks,
4324 sizeof(struct raid10_info),
4325 GFP_KERNEL);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004326 if (!conf->mirrors_new)
4327 return -ENOMEM;
4328 }
4329 return 0;
4330}
4331
4332/*
4333 * Need to check if array has failed when deciding whether to:
4334 * - start an array
4335 * - remove non-faulty devices
4336 * - add a spare
4337 * - allow a reshape
4338 * This determination is simple when no reshape is happening.
4339 * However if there is a reshape, we need to carefully check
4340 * both the before and after sections.
4341 * This is because some failed devices may only affect one
4342 * of the two sections, and some non-in_sync devices may
4343 * be insync in the section most affected by failed devices.
4344 */
4345static int calc_degraded(struct r10conf *conf)
4346{
4347 int degraded, degraded2;
4348 int i;
4349
NeilBrown3ea7daa2012-05-22 13:53:47 +10004350 degraded = 0;
4351 /* 'prev' section first */
4352 for (i = 0; i < conf->prev.raid_disks; i++) {
Yu Kuaia448af22023-11-25 16:16:01 +08004353 struct md_rdev *rdev = conf->mirrors[i].rdev;
4354
NeilBrown3ea7daa2012-05-22 13:53:47 +10004355 if (!rdev || test_bit(Faulty, &rdev->flags))
4356 degraded++;
4357 else if (!test_bit(In_sync, &rdev->flags))
4358 /* When we can reduce the number of devices in
4359 * an array, this might not contribute to
4360 * 'degraded'. It does now.
4361 */
4362 degraded++;
4363 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004364 if (conf->geo.raid_disks == conf->prev.raid_disks)
4365 return degraded;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004366 degraded2 = 0;
4367 for (i = 0; i < conf->geo.raid_disks; i++) {
Yu Kuaia448af22023-11-25 16:16:01 +08004368 struct md_rdev *rdev = conf->mirrors[i].rdev;
4369
NeilBrown3ea7daa2012-05-22 13:53:47 +10004370 if (!rdev || test_bit(Faulty, &rdev->flags))
4371 degraded2++;
4372 else if (!test_bit(In_sync, &rdev->flags)) {
4373 /* If reshape is increasing the number of devices,
4374 * this section has already been recovered, so
4375 * it doesn't contribute to degraded.
4376 * else it does.
4377 */
4378 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4379 degraded2++;
4380 }
4381 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004382 if (degraded2 > degraded)
4383 return degraded2;
4384 return degraded;
4385}
4386
4387static int raid10_start_reshape(struct mddev *mddev)
4388{
4389 /* A 'reshape' has been requested. This commits
4390 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4391 * This also checks if there are enough spares and adds them
4392 * to the array.
4393 * We currently require enough spares to make the final
4394 * array non-degraded. We also require that the difference
4395 * between old and new data_offset - on each device - is
4396 * enough that we never risk over-writing.
4397 */
4398
4399 unsigned long before_length, after_length;
4400 sector_t min_offset_diff = 0;
4401 int first = 1;
4402 struct geom new;
4403 struct r10conf *conf = mddev->private;
4404 struct md_rdev *rdev;
4405 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004406 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004407
4408 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4409 return -EBUSY;
4410
4411 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4412 return -EINVAL;
4413
4414 before_length = ((1 << conf->prev.chunk_shift) *
4415 conf->prev.far_copies);
4416 after_length = ((1 << conf->geo.chunk_shift) *
4417 conf->geo.far_copies);
4418
4419 rdev_for_each(rdev, mddev) {
4420 if (!test_bit(In_sync, &rdev->flags)
4421 && !test_bit(Faulty, &rdev->flags))
4422 spares++;
4423 if (rdev->raid_disk >= 0) {
4424 long long diff = (rdev->new_data_offset
4425 - rdev->data_offset);
4426 if (!mddev->reshape_backwards)
4427 diff = -diff;
4428 if (diff < 0)
4429 diff = 0;
4430 if (first || diff < min_offset_diff)
4431 min_offset_diff = diff;
Shaohua Lib5063352017-05-01 12:15:07 -07004432 first = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004433 }
4434 }
4435
4436 if (max(before_length, after_length) > min_offset_diff)
4437 return -EINVAL;
4438
4439 if (spares < mddev->delta_disks)
4440 return -EINVAL;
4441
4442 conf->offset_diff = min_offset_diff;
4443 spin_lock_irq(&conf->device_lock);
4444 if (conf->mirrors_new) {
4445 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004446 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004447 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004448 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004449 conf->mirrors_old = conf->mirrors;
4450 conf->mirrors = conf->mirrors_new;
4451 conf->mirrors_new = NULL;
4452 }
4453 setup_geo(&conf->geo, mddev, geo_start);
4454 smp_mb();
4455 if (mddev->reshape_backwards) {
4456 sector_t size = raid10_size(mddev, 0, 0);
4457 if (size < mddev->array_sectors) {
4458 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004459 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4460 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004461 return -EINVAL;
4462 }
4463 mddev->resync_max_sectors = size;
4464 conf->reshape_progress = size;
4465 } else
4466 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004467 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004468 spin_unlock_irq(&conf->device_lock);
4469
NeilBrownbb63a702012-05-22 13:55:28 +10004470 if (mddev->delta_disks && mddev->bitmap) {
Guoqing Jiangafd75622018-10-18 16:37:41 +08004471 struct mdp_superblock_1 *sb = NULL;
4472 sector_t oldsize, newsize;
4473
4474 oldsize = raid10_size(mddev, 0, 0);
4475 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4476
4477 if (!mddev_is_clustered(mddev)) {
4478 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4479 if (ret)
4480 goto abort;
4481 else
4482 goto out;
4483 }
4484
4485 rdev_for_each(rdev, mddev) {
4486 if (rdev->raid_disk > -1 &&
4487 !test_bit(Faulty, &rdev->flags))
4488 sb = page_address(rdev->sb_page);
4489 }
4490
4491 /*
4492 * some node is already performing reshape, and no need to
4493 * call md_bitmap_resize again since it should be called when
4494 * receiving BITMAP_RESIZE msg
4495 */
4496 if ((sb && (le32_to_cpu(sb->feature_map) &
4497 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4498 goto out;
4499
4500 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
NeilBrownbb63a702012-05-22 13:55:28 +10004501 if (ret)
4502 goto abort;
Guoqing Jiangafd75622018-10-18 16:37:41 +08004503
4504 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4505 if (ret) {
4506 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4507 goto abort;
4508 }
NeilBrownbb63a702012-05-22 13:55:28 +10004509 }
Guoqing Jiangafd75622018-10-18 16:37:41 +08004510out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10004511 if (mddev->delta_disks > 0) {
4512 rdev_for_each(rdev, mddev)
4513 if (rdev->raid_disk < 0 &&
4514 !test_bit(Faulty, &rdev->flags)) {
4515 if (raid10_add_disk(mddev, rdev) == 0) {
4516 if (rdev->raid_disk >=
4517 conf->prev.raid_disks)
4518 set_bit(In_sync, &rdev->flags);
4519 else
4520 rdev->recovery_offset = 0;
4521
Damien Le Moal38ffc012020-07-16 13:54:43 +09004522 /* Failure here is OK */
4523 sysfs_link_rdev(mddev, rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004524 }
4525 } else if (rdev->raid_disk >= conf->prev.raid_disks
4526 && !test_bit(Faulty, &rdev->flags)) {
4527 /* This is a spare that was manually added */
4528 set_bit(In_sync, &rdev->flags);
4529 }
4530 }
4531 /* When a reshape changes the number of devices,
4532 * ->degraded is measured against the larger of the
4533 * pre and post numbers.
4534 */
4535 spin_lock_irq(&conf->device_lock);
4536 mddev->degraded = calc_degraded(conf);
4537 spin_unlock_irq(&conf->device_lock);
4538 mddev->raid_disks = conf->geo.raid_disks;
4539 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004540 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004541
4542 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4543 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004544 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004545 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Yu Kuaiad39c082024-02-01 17:25:49 +08004546 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004547 conf->reshape_checkpoint = jiffies;
Guoqing Jiang54679482021-10-04 23:34:53 +08004548 md_new_event();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004549 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004550
4551abort:
4552 mddev->recovery = 0;
4553 spin_lock_irq(&conf->device_lock);
4554 conf->geo = conf->prev;
4555 mddev->raid_disks = conf->geo.raid_disks;
4556 rdev_for_each(rdev, mddev)
4557 rdev->new_data_offset = rdev->data_offset;
4558 smp_wmb();
4559 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004560 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004561 mddev->reshape_position = MaxSector;
4562 spin_unlock_irq(&conf->device_lock);
4563 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004564}
4565
4566/* Calculate the last device-address that could contain
4567 * any block from the chunk that includes the array-address 's'
4568 * and report the next address.
4569 * i.e. the address returned will be chunk-aligned and after
4570 * any data that is in the chunk containing 's'.
4571 */
4572static sector_t last_dev_address(sector_t s, struct geom *geo)
4573{
4574 s = (s | geo->chunk_mask) + 1;
4575 s >>= geo->chunk_shift;
4576 s *= geo->near_copies;
4577 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4578 s *= geo->far_copies;
4579 s <<= geo->chunk_shift;
4580 return s;
4581}
4582
4583/* Calculate the first device-address that could contain
4584 * any block from the chunk that includes the array-address 's'.
4585 * This too will be the start of a chunk
4586 */
4587static sector_t first_dev_address(sector_t s, struct geom *geo)
4588{
4589 s >>= geo->chunk_shift;
4590 s *= geo->near_copies;
4591 sector_div(s, geo->raid_disks);
4592 s *= geo->far_copies;
4593 s <<= geo->chunk_shift;
4594 return s;
4595}
4596
4597static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4598 int *skipped)
4599{
4600 /* We simply copy at most one chunk (smallest of old and new)
4601 * at a time, possibly less if that exceeds RESYNC_PAGES,
4602 * or we hit a bad block or something.
4603 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004604 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004605 * can record any location.
4606 *
4607 * If we will want to write to a location that isn't
4608 * yet recorded as 'safe' (i.e. in metadata on disk) then
4609 * we need to flush all reshape requests and update the metadata.
4610 *
4611 * When reshaping forwards (e.g. to more devices), we interpret
4612 * 'safe' as the earliest block which might not have been copied
4613 * down yet. We divide this by previous stripe size and multiply
4614 * by previous stripe length to get lowest device offset that we
4615 * cannot write to yet.
4616 * We interpret 'sector_nr' as an address that we want to write to.
4617 * From this we use last_device_address() to find where we might
4618 * write to, and first_device_address on the 'safe' position.
4619 * If this 'next' write position is after the 'safe' position,
4620 * we must update the metadata to increase the 'safe' position.
4621 *
4622 * When reshaping backwards, we round in the opposite direction
4623 * and perform the reverse test: next write position must not be
4624 * less than current safe position.
4625 *
4626 * In all this the minimum difference in data offsets
4627 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004628 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004629 *
4630 * We need to prepare all the bios here before we start any IO
4631 * to ensure the size we choose is acceptable to all devices.
4632 * The means one for each copy for write-out and an extra one for
4633 * read-in.
4634 * We store the read-in bio in ->master_bio and the others in
4635 * ->devs[x].bio and ->devs[x].repl_bio.
4636 */
4637 struct r10conf *conf = mddev->private;
4638 struct r10bio *r10_bio;
4639 sector_t next, safe, last;
4640 int max_sectors;
4641 int nr_sectors;
4642 int s;
4643 struct md_rdev *rdev;
4644 int need_flush = 0;
4645 struct bio *blist;
4646 struct bio *bio, *read_bio;
4647 int sectors_done = 0;
Ming Leif0250612017-03-17 00:12:33 +08004648 struct page **pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004649
4650 if (sector_nr == 0) {
4651 /* If restarting in the middle, skip the initial sectors */
4652 if (mddev->reshape_backwards &&
4653 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4654 sector_nr = (raid10_size(mddev, 0, 0)
4655 - conf->reshape_progress);
4656 } else if (!mddev->reshape_backwards &&
4657 conf->reshape_progress > 0)
4658 sector_nr = conf->reshape_progress;
4659 if (sector_nr) {
4660 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07004661 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004662 *skipped = 1;
4663 return sector_nr;
4664 }
4665 }
4666
4667 /* We don't use sector_nr to track where we are up to
4668 * as that doesn't work well for ->reshape_backwards.
4669 * So just use ->reshape_progress.
4670 */
4671 if (mddev->reshape_backwards) {
4672 /* 'next' is the earliest device address that we might
4673 * write to for this chunk in the new layout
4674 */
4675 next = first_dev_address(conf->reshape_progress - 1,
4676 &conf->geo);
4677
4678 /* 'safe' is the last device address that we might read from
4679 * in the old layout after a restart
4680 */
4681 safe = last_dev_address(conf->reshape_safe - 1,
4682 &conf->prev);
4683
4684 if (next + conf->offset_diff < safe)
4685 need_flush = 1;
4686
4687 last = conf->reshape_progress - 1;
4688 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4689 & conf->prev.chunk_mask);
Zhen Leie2873082020-08-21 09:29:18 +08004690 if (sector_nr + RESYNC_SECTORS < last)
4691 sector_nr = last + 1 - RESYNC_SECTORS;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004692 } else {
4693 /* 'next' is after the last device address that we
4694 * might write to for this chunk in the new layout
4695 */
4696 next = last_dev_address(conf->reshape_progress, &conf->geo);
4697
4698 /* 'safe' is the earliest device address that we might
4699 * read from in the old layout after a restart
4700 */
4701 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4702
4703 /* Need to update metadata if 'next' might be beyond 'safe'
4704 * as that would possibly corrupt data
4705 */
4706 if (next > safe + conf->offset_diff)
4707 need_flush = 1;
4708
4709 sector_nr = conf->reshape_progress;
4710 last = sector_nr | (conf->geo.chunk_mask
4711 & conf->prev.chunk_mask);
4712
Zhen Leie2873082020-08-21 09:29:18 +08004713 if (sector_nr + RESYNC_SECTORS <= last)
4714 last = sector_nr + RESYNC_SECTORS - 1;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004715 }
4716
4717 if (need_flush ||
4718 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4719 /* Need to update reshape_position in metadata */
Vishal Vermac9aa889b02021-12-21 20:06:21 +00004720 wait_barrier(conf, false);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004721 mddev->reshape_position = conf->reshape_progress;
4722 if (mddev->reshape_backwards)
4723 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4724 - conf->reshape_progress;
4725 else
4726 mddev->curr_resync_completed = conf->reshape_progress;
4727 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004728 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004729 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004730 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004731 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4732 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4733 allow_barrier(conf);
4734 return sectors_done;
4735 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004736 conf->reshape_safe = mddev->reshape_position;
4737 allow_barrier(conf);
4738 }
4739
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004740 raise_barrier(conf, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004741read_more:
4742 /* Now schedule reads for blocks from sector_nr to last */
Shaohua Li208410b2017-08-24 17:50:40 -07004743 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004744 r10_bio->state = 0;
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004745 raise_barrier(conf, 1);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004746 atomic_set(&r10_bio->remaining, 0);
4747 r10_bio->mddev = mddev;
4748 r10_bio->sector = sector_nr;
4749 set_bit(R10BIO_IsReshape, &r10_bio->state);
4750 r10_bio->sectors = last - sector_nr + 1;
4751 rdev = read_balance(conf, r10_bio, &max_sectors);
4752 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4753
4754 if (!rdev) {
4755 /* Cannot read from here, so need to record bad blocks
4756 * on all the target devices.
4757 */
4758 // FIXME
Kent Overstreetafeee512018-05-20 18:25:52 -04004759 mempool_free(r10_bio, &conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004760 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4761 return sectors_done;
4762 }
4763
Christoph Hellwig609be102022-01-24 10:11:03 +01004764 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ,
4765 GFP_KERNEL, &mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004766 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004767 + rdev->data_offset);
4768 read_bio->bi_private = r10_bio;
Ming Lei81fa1522017-03-17 00:12:32 +08004769 read_bio->bi_end_io = end_reshape_read;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004770 r10_bio->master_bio = read_bio;
4771 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4772
Guoqing Jiang7564bed2018-10-18 16:37:42 +08004773 /*
4774 * Broadcast RESYNC message to other nodes, so all nodes would not
4775 * write to the region to avoid conflict.
4776 */
4777 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4778 struct mdp_superblock_1 *sb = NULL;
4779 int sb_reshape_pos = 0;
4780
4781 conf->cluster_sync_low = sector_nr;
4782 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4783 sb = page_address(rdev->sb_page);
4784 if (sb) {
4785 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4786 /*
4787 * Set cluster_sync_low again if next address for array
4788 * reshape is less than cluster_sync_low. Since we can't
4789 * update cluster_sync_low until it has finished reshape.
4790 */
4791 if (sb_reshape_pos < conf->cluster_sync_low)
4792 conf->cluster_sync_low = sb_reshape_pos;
4793 }
4794
4795 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4796 conf->cluster_sync_high);
4797 }
4798
NeilBrown3ea7daa2012-05-22 13:53:47 +10004799 /* Now find the locations in the new layout */
4800 __raid10_find_phys(&conf->geo, r10_bio);
4801
4802 blist = read_bio;
4803 read_bio->bi_next = NULL;
4804
4805 for (s = 0; s < conf->copies*2; s++) {
4806 struct bio *b;
4807 int d = r10_bio->devs[s/2].devnum;
4808 struct md_rdev *rdev2;
4809 if (s&1) {
Yu Kuaia448af22023-11-25 16:16:01 +08004810 rdev2 = conf->mirrors[d].replacement;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004811 b = r10_bio->devs[s/2].repl_bio;
4812 } else {
Yu Kuaia448af22023-11-25 16:16:01 +08004813 rdev2 = conf->mirrors[d].rdev;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004814 b = r10_bio->devs[s/2].bio;
4815 }
4816 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4817 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004818
Christoph Hellwig74d46992017-08-23 19:10:32 +02004819 bio_set_dev(b, rdev2->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004820 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4821 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004822 b->bi_end_io = end_reshape_write;
Christoph Hellwigc34b7ac2022-12-06 15:40:57 +01004823 b->bi_opf = REQ_OP_WRITE;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004824 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004825 blist = b;
4826 }
4827
4828 /* Now add as many pages as possible to all of these bios. */
4829
4830 nr_sectors = 0;
Ming Leif0250612017-03-17 00:12:33 +08004831 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004832 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
Ming Leif0250612017-03-17 00:12:33 +08004833 struct page *page = pages[s / (PAGE_SIZE >> 9)];
NeilBrown3ea7daa2012-05-22 13:53:47 +10004834 int len = (max_sectors - s) << 9;
4835 if (len > PAGE_SIZE)
4836 len = PAGE_SIZE;
4837 for (bio = blist; bio ; bio = bio->bi_next) {
Johannes Thumshirn0c67dd62023-05-31 04:50:38 -07004838 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
4839 bio->bi_status = BLK_STS_RESOURCE;
4840 bio_endio(bio);
4841 return sectors_done;
4842 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004843 }
4844 sector_nr += len >> 9;
4845 nr_sectors += len >> 9;
4846 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004847 r10_bio->sectors = nr_sectors;
4848
4849 /* Now submit the read */
Christoph Hellwig74d46992017-08-23 19:10:32 +02004850 md_sync_acct_bio(read_bio, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004851 atomic_inc(&r10_bio->remaining);
4852 read_bio->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02004853 submit_bio_noacct(read_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004854 sectors_done += nr_sectors;
4855 if (sector_nr <= last)
4856 goto read_more;
4857
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004858 lower_barrier(conf);
4859
NeilBrown3ea7daa2012-05-22 13:53:47 +10004860 /* Now that we have done the whole section we can
4861 * update reshape_progress
4862 */
4863 if (mddev->reshape_backwards)
4864 conf->reshape_progress -= sectors_done;
4865 else
4866 conf->reshape_progress += sectors_done;
4867
4868 return sectors_done;
4869}
4870
4871static void end_reshape_request(struct r10bio *r10_bio);
4872static int handle_reshape_read_error(struct mddev *mddev,
4873 struct r10bio *r10_bio);
4874static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4875{
4876 /* Reshape read completed. Hopefully we have a block
4877 * to write out.
4878 * If we got a read error then we do sync 1-page reads from
4879 * elsewhere until we find the data - or give up.
4880 */
4881 struct r10conf *conf = mddev->private;
4882 int s;
4883
4884 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4885 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4886 /* Reshape has been aborted */
4887 md_done_sync(mddev, r10_bio->sectors, 0);
4888 return;
4889 }
4890
4891 /* We definitely have the data in the pages, schedule the
4892 * writes.
4893 */
4894 atomic_set(&r10_bio->remaining, 1);
4895 for (s = 0; s < conf->copies*2; s++) {
4896 struct bio *b;
4897 int d = r10_bio->devs[s/2].devnum;
4898 struct md_rdev *rdev;
4899 if (s&1) {
Yu Kuaia448af22023-11-25 16:16:01 +08004900 rdev = conf->mirrors[d].replacement;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004901 b = r10_bio->devs[s/2].repl_bio;
4902 } else {
Yu Kuaia448af22023-11-25 16:16:01 +08004903 rdev = conf->mirrors[d].rdev;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004904 b = r10_bio->devs[s/2].bio;
4905 }
Yu Kuaia448af22023-11-25 16:16:01 +08004906 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown3ea7daa2012-05-22 13:53:47 +10004907 continue;
Yu Kuaia448af22023-11-25 16:16:01 +08004908
NeilBrown3ea7daa2012-05-22 13:53:47 +10004909 atomic_inc(&rdev->nr_pending);
Christoph Hellwig74d46992017-08-23 19:10:32 +02004910 md_sync_acct_bio(b, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004911 atomic_inc(&r10_bio->remaining);
4912 b->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02004913 submit_bio_noacct(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004914 }
4915 end_reshape_request(r10_bio);
4916}
4917
4918static void end_reshape(struct r10conf *conf)
4919{
4920 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4921 return;
4922
4923 spin_lock_irq(&conf->device_lock);
4924 conf->prev = conf->geo;
4925 md_finish_reshape(conf->mddev);
4926 smp_wmb();
4927 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004928 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004929 spin_unlock_irq(&conf->device_lock);
4930
Christoph Hellwig3d8466b2024-03-03 07:01:47 -07004931 mddev_update_io_opt(conf->mddev, raid10_nr_stripes(conf));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004932 conf->fullsync = 0;
4933}
4934
Guoqing Jiang7564bed2018-10-18 16:37:42 +08004935static void raid10_update_reshape_pos(struct mddev *mddev)
4936{
4937 struct r10conf *conf = mddev->private;
Guoqing Jiang5ebaf802018-10-18 16:37:43 +08004938 sector_t lo, hi;
Guoqing Jiang7564bed2018-10-18 16:37:42 +08004939
Guoqing Jiang5ebaf802018-10-18 16:37:43 +08004940 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
4941 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
4942 || mddev->reshape_position == MaxSector)
4943 conf->reshape_progress = mddev->reshape_position;
4944 else
4945 WARN_ON_ONCE(1);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08004946}
4947
NeilBrown3ea7daa2012-05-22 13:53:47 +10004948static int handle_reshape_read_error(struct mddev *mddev,
4949 struct r10bio *r10_bio)
4950{
4951 /* Use sync reads to get the blocks from somewhere else */
4952 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004953 struct r10conf *conf = mddev->private;
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07004954 struct r10bio *r10b;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004955 int slot = 0;
4956 int idx = 0;
Ming Lei2d06e3b2017-03-17 00:12:35 +08004957 struct page **pages;
4958
Gustavo A. R. Silva8cf05a72019-06-14 15:41:09 -07004959 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07004960 if (!r10b) {
4961 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4962 return -ENOMEM;
4963 }
4964
Ming Lei2d06e3b2017-03-17 00:12:35 +08004965 /* reshape IOs share pages from .devs[0].bio */
4966 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004967
NeilBrowne0ee7782012-08-18 09:51:42 +10004968 r10b->sector = r10_bio->sector;
4969 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004970
4971 while (sectors) {
4972 int s = sectors;
4973 int success = 0;
4974 int first_slot = slot;
4975
4976 if (s > (PAGE_SIZE >> 9))
4977 s = PAGE_SIZE >> 9;
4978
4979 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004980 int d = r10b->devs[slot].devnum;
Yu Kuaia448af22023-11-25 16:16:01 +08004981 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004982 sector_t addr;
4983 if (rdev == NULL ||
4984 test_bit(Faulty, &rdev->flags) ||
4985 !test_bit(In_sync, &rdev->flags))
4986 goto failed;
4987
NeilBrowne0ee7782012-08-18 09:51:42 +10004988 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004989 atomic_inc(&rdev->nr_pending);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004990 success = sync_page_io(rdev,
4991 addr,
4992 s << 9,
Ming Lei2d06e3b2017-03-17 00:12:35 +08004993 pages[idx],
Bart Van Assche4ce4c732022-07-14 11:06:57 -07004994 REQ_OP_READ, false);
NeilBrownd094d682016-06-02 16:19:52 +10004995 rdev_dec_pending(rdev, mddev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004996 if (success)
4997 break;
4998 failed:
4999 slot++;
5000 if (slot >= conf->copies)
5001 slot = 0;
5002 if (slot == first_slot)
5003 break;
5004 }
5005 if (!success) {
5006 /* couldn't read this block, must give up */
5007 set_bit(MD_RECOVERY_INTR,
5008 &mddev->recovery);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005009 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005010 return -EIO;
5011 }
5012 sectors -= s;
5013 idx++;
5014 }
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005015 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005016 return 0;
5017}
5018
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005019static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10005020{
Ming Leif0250612017-03-17 00:12:33 +08005021 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005022 struct mddev *mddev = r10_bio->mddev;
5023 struct r10conf *conf = mddev->private;
5024 int d;
5025 int slot;
5026 int repl;
5027 struct md_rdev *rdev = NULL;
5028
5029 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Yu Kuaia448af22023-11-25 16:16:01 +08005030 rdev = repl ? conf->mirrors[d].replacement :
5031 conf->mirrors[d].rdev;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005032
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005033 if (bio->bi_status) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10005034 /* FIXME should record badblock */
5035 md_error(mddev, rdev);
5036 }
5037
5038 rdev_dec_pending(rdev, mddev);
5039 end_reshape_request(r10_bio);
5040}
5041
5042static void end_reshape_request(struct r10bio *r10_bio)
5043{
5044 if (!atomic_dec_and_test(&r10_bio->remaining))
5045 return;
5046 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5047 bio_put(r10_bio->master_bio);
5048 put_buf(r10_bio);
5049}
5050
5051static void raid10_finish_reshape(struct mddev *mddev)
5052{
5053 struct r10conf *conf = mddev->private;
5054
5055 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5056 return;
5057
5058 if (mddev->delta_disks > 0) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10005059 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5060 mddev->recovery_cp = mddev->resync_max_sectors;
5061 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5062 }
BingJing Chang88763912018-02-22 13:34:46 +08005063 mddev->resync_max_sectors = mddev->array_sectors;
NeilBrown63aced62012-05-22 13:55:33 +10005064 } else {
5065 int d;
5066 for (d = conf->geo.raid_disks ;
5067 d < conf->geo.raid_disks - mddev->delta_disks;
5068 d++) {
Yu Kuaia448af22023-11-25 16:16:01 +08005069 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrown63aced62012-05-22 13:55:33 +10005070 if (rdev)
5071 clear_bit(In_sync, &rdev->flags);
Yu Kuaia448af22023-11-25 16:16:01 +08005072 rdev = conf->mirrors[d].replacement;
NeilBrown63aced62012-05-22 13:55:33 +10005073 if (rdev)
5074 clear_bit(In_sync, &rdev->flags);
5075 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10005076 }
5077 mddev->layout = mddev->new_layout;
5078 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5079 mddev->reshape_position = MaxSector;
5080 mddev->delta_disks = 0;
5081 mddev->reshape_backwards = 0;
5082}
5083
NeilBrown84fc4b52011-10-11 16:49:58 +11005084static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
5086 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08005087 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08005089 .make_request = raid10_make_request,
5090 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11005091 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08005092 .status = raid10_status,
5093 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 .hot_add_disk = raid10_add_disk,
5095 .hot_remove_disk= raid10_remove_disk,
5096 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08005097 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08005098 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005099 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11005100 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11005101 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10005102 .check_reshape = raid10_check_reshape,
5103 .start_reshape = raid10_start_reshape,
5104 .finish_reshape = raid10_finish_reshape,
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005105 .update_reshape_pos = raid10_update_reshape_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106};
5107
5108static int __init raid_init(void)
5109{
NeilBrown2604b702006-01-06 00:20:36 -08005110 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111}
5112
5113static void raid_exit(void)
5114{
NeilBrown2604b702006-01-06 00:20:36 -08005115 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116}
5117
5118module_init(raid_init);
5119module_exit(raid_exit);
5120MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005121MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005123MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08005124MODULE_ALIAS("md-level-10");