blob: 13f5e6b2a73d6a6aa27c69ad72cdf324c0027dc4 [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * raid10.c : Multiple Devices driver for Linux
4 *
5 * Copyright (C) 2000-2004 Neil Brown
6 *
7 * RAID-10 support for md.
8 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110013#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110014#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040015#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110016#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100017#include <linux/ratelimit.h>
NeilBrown3ea7daa2012-05-22 13:53:47 +100018#include <linux/kthread.h>
Guoqing Jiangafd75622018-10-18 16:37:41 +080019#include <linux/raid/md_p.h>
NeilBrown109e3762016-11-18 13:22:04 +110020#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110021#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110022#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110023#include "raid0.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040024#include "md-bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
29 * chunk_size
30 * raid_disks
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070033 * far_offset (stored in bit 16 of layout )
Jonathan Brassow475901a2013-02-21 13:28:10 +110034 * use_far_sets (stored in bit 17 of layout )
NeilBrown8bce6d32015-10-22 13:20:15 +110035 * use_far_sets_bugfixed (stored in bit 18 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
Jonathan Brassow475901a2013-02-21 13:28:10 +110037 * The data to be stored is divided into chunks using chunksize. Each device
38 * is divided into far_copies sections. In each section, chunks are laid out
39 * in a style similar to raid0, but near_copies copies of each chunk is stored
40 * (each on a different drive). The starting device for each section is offset
41 * near_copies from the starting device of the previous section. Thus there
42 * are (near_copies * far_copies) of each chunk, and each is on a different
43 * drive. near_copies and far_copies must be at least one, and their product
44 * is at most raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070045 *
46 * If far_offset is true, then the far_copies are handled a bit differently.
Jonathan Brassow475901a2013-02-21 13:28:10 +110047 * The copies are still in different stripes, but instead of being very far
48 * apart on disk, there are adjacent stripes.
49 *
50 * The far and offset algorithms are handled slightly differently if
51 * 'use_far_sets' is true. In this case, the array's devices are grouped into
52 * sets that are (near_copies * far_copies) in size. The far copied stripes
53 * are still shifted by 'near_copies' devices, but this shifting stays confined
54 * to the set rather than the entire array. This is done to improve the number
55 * of device combinations that can fail without causing the array to fail.
56 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
57 * on a device):
58 * A B C D A B C D E
59 * ... ...
60 * D A B C E A B C D
61 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
62 * [A B] [C D] [A B] [C D E]
63 * |...| |...| |...| | ... |
64 * [B A] [D C] [B A] [E C D]
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
NeilBrowne879a872011-10-11 16:49:02 +110067static void allow_barrier(struct r10conf *conf);
68static void lower_barrier(struct r10conf *conf);
NeilBrown635f6412013-06-11 14:57:09 +100069static int _enough(struct r10conf *conf, int previous, int ignore);
NeilBrown1919cbb2016-11-18 16:16:12 +110070static int enough(struct r10conf *conf, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +100071static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
72 int *skipped);
73static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020074static void end_reshape_write(struct bio *bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +100075static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080076
NeilBrown578b54a2016-11-14 16:30:21 +110077#define raid10_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
79
Ming Leifb0eb5d2017-07-14 16:14:43 +080080#include "raid1-10.c"
81
Ming Leif0250612017-03-17 00:12:33 +080082/*
Ming Leif0250612017-03-17 00:12:33 +080083 * for resync bio, r10bio pointer can be retrieved from the per-bio
84 * 'struct resync_pages'.
85 */
86static inline struct r10bio *get_resync_r10bio(struct bio *bio)
87{
88 return get_resync_pages(bio)->raid_bio;
89}
90
Al Virodd0fc662005-10-07 07:46:04 +010091static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
NeilBrowne879a872011-10-11 16:49:02 +110093 struct r10conf *conf = data;
Xiao Nic2968282021-02-04 15:50:44 +080094 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
NeilBrown69335ef2011-12-23 10:17:54 +110096 /* allocate a r10bio with room for raid_disks entries in the
97 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010098 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Guoqing Jiang8db87912017-10-24 15:11:52 +0800101#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
NeilBrown0310fa22008-08-05 15:54:14 +1000102/* amount of memory to reserve for resync requests */
103#define RESYNC_WINDOW (1024*1024)
104/* maximum number of concurrent requests, memory permitting */
105#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Guoqing Jiang4b242e92018-01-19 11:37:56 +0800106#define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
Guoqing Jiang8db87912017-10-24 15:11:52 +0800107#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
110 * When performing a resync, we need to read and compare, so
111 * we need as many pages are there are copies.
112 * When performing a recovery, we need 2 bios, one for read,
113 * one for write (we recover only one drive per r10buf)
114 *
115 */
Al Virodd0fc662005-10-07 07:46:04 +0100116static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
NeilBrowne879a872011-10-11 16:49:02 +1100118 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100119 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct bio *bio;
Ming Leif0250612017-03-17 00:12:33 +0800121 int j;
122 int nalloc, nalloc_rp;
123 struct resync_pages *rps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100126 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
NeilBrown3ea7daa2012-05-22 13:53:47 +1000129 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
130 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 nalloc = conf->copies; /* resync */
132 else
133 nalloc = 2; /* recovery */
134
Ming Leif0250612017-03-17 00:12:33 +0800135 /* allocate once for all bios */
136 if (!conf->have_replacement)
137 nalloc_rp = nalloc;
138 else
139 nalloc_rp = nalloc * 2;
Kees Cook6da2ec52018-06-12 13:55:00 -0700140 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
Ming Leif0250612017-03-17 00:12:33 +0800141 if (!rps)
142 goto out_free_r10bio;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /*
145 * Allocate bios.
146 */
147 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100148 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!bio)
150 goto out_free_bio;
151 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100152 if (!conf->have_replacement)
153 continue;
154 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
155 if (!bio)
156 goto out_free_bio;
157 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 /*
160 * Allocate RESYNC_PAGES data pages and attach them
161 * where needed.
162 */
Ming Leif0250612017-03-17 00:12:33 +0800163 for (j = 0; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100164 struct bio *rbio = r10_bio->devs[j].repl_bio;
Ming Leif0250612017-03-17 00:12:33 +0800165 struct resync_pages *rp, *rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Ming Leif0250612017-03-17 00:12:33 +0800167 rp = &rps[j];
168 if (rbio)
169 rp_repl = &rps[nalloc + j];
170
171 bio = r10_bio->devs[j].bio;
172
173 if (!j || test_bit(MD_RECOVERY_SYNC,
174 &conf->mddev->recovery)) {
175 if (resync_alloc_pages(rp, gfp_flags))
176 goto out_free_pages;
177 } else {
178 memcpy(rp, &rps[0], sizeof(*rp));
179 resync_get_all_pages(rp);
180 }
181
Ming Leif0250612017-03-17 00:12:33 +0800182 rp->raid_bio = r10_bio;
183 bio->bi_private = rp;
184 if (rbio) {
185 memcpy(rp_repl, rp, sizeof(*rp));
186 rbio->bi_private = rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 }
189
190 return r10_bio;
191
192out_free_pages:
Ming Leif0250612017-03-17 00:12:33 +0800193 while (--j >= 0)
John Pittman45422b72019-11-11 16:43:20 -0800194 resync_free_pages(&rps[j]);
Ming Leif0250612017-03-17 00:12:33 +0800195
majianpeng5fdd2cf2012-05-22 13:55:03 +1000196 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000198 for ( ; j < nalloc; j++) {
199 if (r10_bio->devs[j].bio)
200 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100201 if (r10_bio->devs[j].repl_bio)
202 bio_put(r10_bio->devs[j].repl_bio);
203 }
Ming Leif0250612017-03-17 00:12:33 +0800204 kfree(rps);
205out_free_r10bio:
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700206 rbio_pool_free(r10_bio, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return NULL;
208}
209
210static void r10buf_pool_free(void *__r10_bio, void *data)
211{
NeilBrowne879a872011-10-11 16:49:02 +1100212 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100213 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int j;
Ming Leif0250612017-03-17 00:12:33 +0800215 struct resync_pages *rp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Ming Leif0250612017-03-17 00:12:33 +0800217 for (j = conf->copies; j--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct bio *bio = r10bio->devs[j].bio;
Ming Leif0250612017-03-17 00:12:33 +0800219
Guoqing Jiangeb81b322018-04-26 10:56:37 +0800220 if (bio) {
221 rp = get_resync_pages(bio);
222 resync_free_pages(rp);
223 bio_put(bio);
224 }
Ming Leif0250612017-03-17 00:12:33 +0800225
NeilBrown69335ef2011-12-23 10:17:54 +1100226 bio = r10bio->devs[j].repl_bio;
227 if (bio)
228 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Ming Leif0250612017-03-17 00:12:33 +0800230
231 /* resync pages array stored in the 1st bio's .bi_private */
232 kfree(rp);
233
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700234 rbio_pool_free(r10bio, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
NeilBrowne879a872011-10-11 16:49:02 +1100237static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int i;
240
Xiao Nic2968282021-02-04 15:50:44 +0800241 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000243 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 bio_put(*bio);
245 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100246 bio = &r10_bio->devs[i].repl_bio;
247 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
248 bio_put(*bio);
249 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251}
252
NeilBrown9f2c9d12011-10-11 16:48:43 +1100253static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
NeilBrowne879a872011-10-11 16:49:02 +1100255 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 put_all_bios(conf, r10_bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400258 mempool_free(r10_bio, &conf->r10bio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
NeilBrown9f2c9d12011-10-11 16:48:43 +1100261static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
NeilBrowne879a872011-10-11 16:49:02 +1100263 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Kent Overstreetafeee512018-05-20 18:25:52 -0400265 mempool_free(r10_bio, &conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
NeilBrown0a27ec92006-01-06 00:20:13 -0800267 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
NeilBrown9f2c9d12011-10-11 16:48:43 +1100270static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100273 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100274 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 spin_lock_irqsave(&conf->device_lock, flags);
277 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800278 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 spin_unlock_irqrestore(&conf->device_lock, flags);
280
Arthur Jones388667b2008-07-25 12:03:38 -0700281 /* wake up frozen array... */
282 wake_up(&conf->wait_barrier);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 md_wakeup_thread(mddev->thread);
285}
286
287/*
288 * raid_end_bio_io() is called when we have finished servicing a mirrored
289 * operation and are ready to return a success/failure code to the buffer
290 * cache layer.
291 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100292static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100295 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
NeilBrown856e08e2011-07-28 11:39:23 +1000297 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200298 bio->bi_status = BLK_STS_IOERR;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100299
300 bio_endio(bio);
301 /*
302 * Wake up any possible resync thread that waits for the device
303 * to go idle.
304 */
305 allow_barrier(conf);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 free_r10bio(r10_bio);
308}
309
310/*
311 * Update disk head position estimator based on IRQ completion info.
312 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100313static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
NeilBrowne879a872011-10-11 16:49:02 +1100315 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
318 r10_bio->devs[slot].addr + (r10_bio->sectors);
319}
320
Namhyung Kim778ca012011-07-18 17:38:47 +1000321/*
322 * Find the disk number which triggered given bio
323 */
NeilBrowne879a872011-10-11 16:49:02 +1100324static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100325 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000326{
327 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100328 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000329
Xiao Nic2968282021-02-04 15:50:44 +0800330 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000331 if (r10_bio->devs[slot].bio == bio)
332 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100333 if (r10_bio->devs[slot].repl_bio == bio) {
334 repl = 1;
335 break;
336 }
337 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000338
Namhyung Kim778ca012011-07-18 17:38:47 +1000339 update_head_pos(slot, r10_bio);
340
NeilBrown749c55e2011-07-28 11:39:24 +1000341 if (slotp)
342 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100343 if (replp)
344 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000345 return r10_bio->devs[slot].devnum;
346}
347
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200348static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200350 int uptodate = !bio->bi_status;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100351 struct r10bio *r10_bio = bio->bi_private;
Colin Ian Kinga0e764c2017-10-11 11:46:54 +0100352 int slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100353 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100354 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 slot = r10_bio->read_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100357 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * this branch is our 'one mirror IO has finished' event handler:
360 */
NeilBrown4443ae12006-01-06 00:20:28 -0800361 update_head_pos(slot, r10_bio);
362
363 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Set R10BIO_Uptodate in our master bio, so that
366 * we will return a good error code to the higher
367 * levels even if IO on some other mirrored buffer fails.
368 *
369 * The 'master' represents the composite IO operation to
370 * user-side. So if something waits for IO, then it will
371 * wait for the 'master' bio.
372 */
373 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100374 } else {
375 /* If all other devices that store this block have
376 * failed, we want to return the error upwards rather
377 * than fail the last device. Here we redefine
378 * "uptodate" to mean "Don't want to retry"
379 */
NeilBrown635f6412013-06-11 14:57:09 +1000380 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
381 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100382 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100383 }
384 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100386 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800387 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000389 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100392 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000393 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100394 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000395 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000396 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 reschedule_retry(r10_bio);
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
NeilBrown9f2c9d12011-10-11 16:48:43 +1100401static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000402{
403 /* clear the bitmap if all writes complete successfully */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700404 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
405 r10_bio->sectors,
406 !test_bit(R10BIO_Degraded, &r10_bio->state),
407 0);
NeilBrownbd870a12011-07-28 11:39:24 +1000408 md_write_end(r10_bio->mddev);
409}
410
NeilBrown9f2c9d12011-10-11 16:48:43 +1100411static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000412{
413 if (atomic_dec_and_test(&r10_bio->remaining)) {
414 if (test_bit(R10BIO_WriteError, &r10_bio->state))
415 reschedule_retry(r10_bio);
416 else {
417 close_write(r10_bio);
418 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
419 reschedule_retry(r10_bio);
420 else
421 raid_end_bio_io(r10_bio);
422 }
423 }
424}
425
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200426static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100428 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000429 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000430 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100431 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100432 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100433 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100434 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700435 bool discard_error;
436
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200437 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
NeilBrown475b0322011-12-23 10:17:55 +1100439 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
NeilBrown475b0322011-12-23 10:17:55 +1100441 if (repl)
442 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100443 if (!rdev) {
444 smp_rmb();
445 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100446 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * this branch is our 'one mirror IO has finished' event handler:
450 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200451 if (bio->bi_status && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100452 if (repl)
453 /* Never record new bad blocks to replacement,
454 * just fail it.
455 */
456 md_error(rdev->mddev, rdev);
457 else {
458 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100459 if (!test_and_set_bit(WantReplacement, &rdev->flags))
460 set_bit(MD_RECOVERY_NEEDED,
461 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100462
NeilBrown475b0322011-12-23 10:17:55 +1100463 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100464 if (test_bit(FailFast, &rdev->flags) &&
465 (bio->bi_opf & MD_FAILFAST)) {
466 md_error(rdev->mddev, rdev);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800467 }
468
469 /*
470 * When the device is faulty, it is not necessary to
471 * handle write error.
472 * For failfast, this is the only remaining device,
473 * We need to retry the write without FailFast.
474 */
475 if (!test_bit(Faulty, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +1100476 set_bit(R10BIO_WriteError, &r10_bio->state);
Yufen Yu7cee6d42019-07-19 13:48:47 +0800477 else {
478 r10_bio->devs[slot].bio = NULL;
479 to_put = bio;
480 dec_rdev = 1;
481 }
NeilBrown475b0322011-12-23 10:17:55 +1100482 }
NeilBrown749c55e2011-07-28 11:39:24 +1000483 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Set R10BIO_Uptodate in our master bio, so that
486 * we will return a good error code for to the higher
487 * levels even if IO on some other mirrored buffer fails.
488 *
489 * The 'master' represents the composite IO operation to
490 * user-side. So if something waits for IO, then it will
491 * wait for the 'master' bio.
492 */
NeilBrown749c55e2011-07-28 11:39:24 +1000493 sector_t first_bad;
494 int bad_sectors;
495
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300496 /*
497 * Do not set R10BIO_Uptodate if the current device is
498 * rebuilding or Faulty. This is because we cannot use
499 * such device for properly reading the data back (we could
500 * potentially use it, if the current write would have felt
501 * before rdev->recovery_offset, but for simplicity we don't
502 * check this here.
503 */
504 if (test_bit(In_sync, &rdev->flags) &&
505 !test_bit(Faulty, &rdev->flags))
506 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
NeilBrown749c55e2011-07-28 11:39:24 +1000508 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100509 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000510 r10_bio->devs[slot].addr,
511 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700512 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000513 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100514 if (repl)
515 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
516 else
517 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000518 dec_rdev = 0;
519 set_bit(R10BIO_MadeGood, &r10_bio->state);
520 }
521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /*
524 *
525 * Let's see if all mirrored write operations have finished
526 * already.
527 */
NeilBrown19d5f832011-09-10 17:21:17 +1000528 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000529 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100530 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100531 if (to_put)
532 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300537 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * parameters: near_copies and far_copies.
539 * near_copies * far_copies must be <= raid_disks.
540 * Normally one of these will be 1.
541 * If both are 1, we get raid0.
542 * If near_copies == raid_disks, we get raid1.
543 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300544 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * first chunk, followed by near_copies copies of the next chunk and
546 * so on.
547 * If far_copies > 1, then after 1/far_copies of the array has been assigned
548 * as described above, we start again with a device offset of near_copies.
549 * So we effectively have another copy of the whole array further down all
550 * the drives, but with blocks on different drives.
551 * With this layout, and block is never stored twice on the one device.
552 *
553 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700554 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
556 * raid10_find_virt does the reverse mapping, from a device and a
557 * sector offset to a virtual address
558 */
559
NeilBrownf8c9e742012-05-21 09:28:33 +1000560static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 int n,f;
563 sector_t sector;
564 sector_t chunk;
565 sector_t stripe;
566 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100568 int last_far_set_start, last_far_set_size;
569
570 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
571 last_far_set_start *= geo->far_set_size;
572
573 last_far_set_size = geo->far_set_size;
574 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000577 chunk = r10bio->sector >> geo->chunk_shift;
578 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
NeilBrown5cf00fc2012-05-21 09:28:20 +1000580 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000582 dev = sector_div(stripe, geo->raid_disks);
583 if (geo->far_offset)
584 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
NeilBrown5cf00fc2012-05-21 09:28:20 +1000586 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000589 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100591 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100594 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 slot++;
596
NeilBrown5cf00fc2012-05-21 09:28:20 +1000597 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100598 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000599 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100600
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100601 if ((geo->raid_disks % geo->far_set_size) &&
602 (d > last_far_set_start)) {
603 d -= last_far_set_start;
604 d %= last_far_set_size;
605 d += last_far_set_start;
606 } else {
607 d %= geo->far_set_size;
608 d += geo->far_set_size * set;
609 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000610 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 r10bio->devs[slot].devnum = d;
612 r10bio->devs[slot].addr = s;
613 slot++;
614 }
615 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000616 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000618 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000621}
622
623static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
624{
625 struct geom *geo = &conf->geo;
626
627 if (conf->reshape_progress != MaxSector &&
628 ((r10bio->sector >= conf->reshape_progress) !=
629 conf->mddev->reshape_backwards)) {
630 set_bit(R10BIO_Previous, &r10bio->state);
631 geo = &conf->prev;
632 } else
633 clear_bit(R10BIO_Previous, &r10bio->state);
634
635 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
NeilBrowne879a872011-10-11 16:49:02 +1100638static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000641 /* Never use conf->prev as this is only called during resync
642 * or recovery, so reshape isn't happening
643 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000644 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100645 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
646 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100647 int last_far_set_start;
648
649 if (geo->raid_disks % geo->far_set_size) {
650 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
651 last_far_set_start *= geo->far_set_size;
652
653 if (dev >= last_far_set_start) {
654 far_set_size = geo->far_set_size;
655 far_set_size += (geo->raid_disks % geo->far_set_size);
656 far_set_start = last_far_set_start;
657 }
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
NeilBrown5cf00fc2012-05-21 09:28:20 +1000660 offset = sector & geo->chunk_mask;
661 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700662 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000663 chunk = sector >> geo->chunk_shift;
664 fc = sector_div(chunk, geo->far_copies);
665 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100666 if (dev < far_set_start)
667 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700668 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000669 while (sector >= geo->stride) {
670 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100671 if (dev < (geo->near_copies + far_set_start))
672 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700673 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000674 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700675 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000676 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700677 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000678 vchunk = chunk * geo->raid_disks + dev;
679 sector_div(vchunk, geo->near_copies);
680 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/*
684 * This routine returns the disk from which the requested read should
685 * be done. There is a per-array 'next expected sequential IO' sector
686 * number - if this matches on the next IO then we use the last disk.
687 * There is also a per-disk 'last know head position' sector that is
688 * maintained from IRQ contexts, both the normal and the resync IO
689 * completion handlers update this position correctly. If there is no
690 * perfect sequential match then we pick the disk whose head is closest.
691 *
692 * If there are 2 mirrors in the same 2 devices, performance degrades
693 * because position is mirror, not device based.
694 *
695 * The rdev for the device selected will have nr_pending incremented.
696 */
697
698/*
699 * FIXME: possibly should rethink readbalancing and do it differently
700 * depending on near_copies / far_copies geometry.
701 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100702static struct md_rdev *read_balance(struct r10conf *conf,
703 struct r10bio *r10_bio,
704 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000706 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000707 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000708 int sectors = r10_bio->sectors;
709 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000710 sector_t new_distance, best_dist;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700711 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000712 int do_balance;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700713 int best_dist_slot, best_pending_slot;
714 bool has_nonrot_disk = false;
715 unsigned int min_pending;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000716 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 raid10_find_phys(conf, r10_bio);
719 rcu_read_lock();
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700720 best_dist_slot = -1;
721 min_pending = UINT_MAX;
722 best_dist_rdev = NULL;
723 best_pending_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000724 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000725 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000726 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100727 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
729 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800730 * device if no resync is going on (recovery is ok), or below
731 * the resync window. We take the first readable disk when
732 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
Guoqing Jiangd4098c72017-10-24 15:11:50 +0800734 if ((conf->mddev->recovery_cp < MaxSector
735 && (this_sector + sectors >= conf->next_resync)) ||
736 (mddev_is_clustered(conf->mddev) &&
737 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
738 this_sector + sectors)))
NeilBrown56d99122011-05-11 14:27:03 +1000739 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
NeilBrown56d99122011-05-11 14:27:03 +1000741 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000742 sector_t first_bad;
743 int bad_sectors;
744 sector_t dev_sector;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700745 unsigned int pending;
746 bool nonrot;
NeilBrown856e08e2011-07-28 11:39:23 +1000747
NeilBrown56d99122011-05-11 14:27:03 +1000748 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000750 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100751 rdev = rcu_dereference(conf->mirrors[disk].replacement);
752 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
753 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
754 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100755 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700756 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100757 continue;
758 if (!test_bit(In_sync, &rdev->flags) &&
759 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000760 continue;
761
NeilBrown856e08e2011-07-28 11:39:23 +1000762 dev_sector = r10_bio->devs[slot].addr;
763 if (is_badblock(rdev, dev_sector, sectors,
764 &first_bad, &bad_sectors)) {
765 if (best_dist < MaxSector)
766 /* Already have a better slot */
767 continue;
768 if (first_bad <= dev_sector) {
769 /* Cannot read here. If this is the
770 * 'primary' device, then we must not read
771 * beyond 'bad_sectors' from another device.
772 */
773 bad_sectors -= (dev_sector - first_bad);
774 if (!do_balance && sectors > bad_sectors)
775 sectors = bad_sectors;
776 if (best_good_sectors > sectors)
777 best_good_sectors = sectors;
778 } else {
779 sector_t good_sectors =
780 first_bad - dev_sector;
781 if (good_sectors > best_good_sectors) {
782 best_good_sectors = good_sectors;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700783 best_dist_slot = slot;
784 best_dist_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000785 }
786 if (!do_balance)
787 /* Must read from here */
788 break;
789 }
790 continue;
791 } else
792 best_good_sectors = sectors;
793
NeilBrown56d99122011-05-11 14:27:03 +1000794 if (!do_balance)
795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700797 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
798 has_nonrot_disk |= nonrot;
799 pending = atomic_read(&rdev->nr_pending);
800 if (min_pending > pending && nonrot) {
801 min_pending = pending;
802 best_pending_slot = slot;
803 best_pending_rdev = rdev;
804 }
805
806 if (best_dist_slot >= 0)
NeilBrown8d3ca832016-11-18 16:16:12 +1100807 /* At least 2 disks to choose from so failfast is OK */
808 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800809 /* This optimisation is debatable, and completely destroys
810 * sequential read speed for 'far copies' arrays. So only
811 * keep it for 'near' arrays, and review those later.
812 */
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700813 if (geo->near_copies > 1 && !pending)
NeilBrown8d3ca832016-11-18 16:16:12 +1100814 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800815
816 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100817 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000818 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800819 else
NeilBrown56d99122011-05-11 14:27:03 +1000820 new_distance = abs(r10_bio->devs[slot].addr -
821 conf->mirrors[disk].head_position);
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700822
NeilBrown56d99122011-05-11 14:27:03 +1000823 if (new_distance < best_dist) {
824 best_dist = new_distance;
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700825 best_dist_slot = slot;
826 best_dist_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 }
NeilBrownabbf0982011-12-23 10:17:54 +1100829 if (slot >= conf->copies) {
Guoqing Jiange9eeba22019-06-14 15:41:11 -0700830 if (has_nonrot_disk) {
831 slot = best_pending_slot;
832 rdev = best_pending_rdev;
833 } else {
834 slot = best_dist_slot;
835 rdev = best_dist_rdev;
836 }
NeilBrownabbf0982011-12-23 10:17:54 +1100837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
NeilBrown56d99122011-05-11 14:27:03 +1000839 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000840 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000841 r10_bio->read_slot = slot;
842 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100843 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000845 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
NeilBrown96c3fd12011-12-23 10:17:54 +1100847 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
NeilBrowne879a872011-10-11 16:49:02 +1100850static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800851{
852 /* Any writes that have been queued but are awaiting
853 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800854 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800855 spin_lock_irq(&conf->device_lock);
856
857 if (conf->pending_bio_list.head) {
Shaohua Li18022a12017-12-01 12:12:34 -0800858 struct blk_plug plug;
NeilBrowna35e63e2008-03-04 14:29:29 -0800859 struct bio *bio;
Shaohua Li18022a12017-12-01 12:12:34 -0800860
NeilBrowna35e63e2008-03-04 14:29:29 -0800861 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100862 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800863 spin_unlock_irq(&conf->device_lock);
NeilBrown474beb52017-12-04 08:21:04 +1100864
865 /*
866 * As this is called in a wait_event() loop (see freeze_array),
867 * current->state might be TASK_UNINTERRUPTIBLE which will
868 * cause a warning when we prepare to wait again. As it is
869 * rare that this path is taken, it is perfectly safe to force
870 * us to go around the wait_event() loop again, so the warning
871 * is a false-positive. Silence the warning by resetting
872 * thread state
873 */
874 __set_current_state(TASK_RUNNING);
875
Shaohua Li18022a12017-12-01 12:12:34 -0800876 blk_start_plug(&plug);
NeilBrowna35e63e2008-03-04 14:29:29 -0800877 /* flush any pending bitmap writes to disk
878 * before proceeding w/ I/O */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700879 md_bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100880 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800881
882 while (bio) { /* submit pending writes */
883 struct bio *next = bio->bi_next;
Christoph Hellwig309dca302021-01-24 11:02:34 +0100884 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800885 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200886 bio_set_dev(bio, rdev->bdev);
NeilBrowna9ae93c2016-11-04 16:46:03 +1100887 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +0800888 bio_io_error(bio);
NeilBrowna9ae93c2016-11-04 16:46:03 +1100889 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig309dca302021-01-24 11:02:34 +0100890 !blk_queue_discard(bio->bi_bdev->bd_disk->queue)))
Shaohua Li532a2a32012-10-11 13:30:52 +1100891 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200892 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100893 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200894 submit_bio_noacct(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800895 bio = next;
896 }
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{
NeilBrown6cce3b22006-01-06 00:20:16 -0800926 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
NeilBrown6cce3b22006-01-06 00:20:16 -0800929 /* Wait until no block IO is waiting (unless 'force') */
930 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100931 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800932
933 /* block any new IO from starting */
934 conf->barrier++;
935
NeilBrownc3b328a2011-04-18 18:25:43 +1000936 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800937 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200938 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100939 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 spin_unlock_irq(&conf->resync_lock);
942}
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;
947 spin_lock_irqsave(&conf->resync_lock, flags);
948 conf->barrier--;
949 spin_unlock_irqrestore(&conf->resync_lock, flags);
950 wake_up(&conf->wait_barrier);
951}
952
NeilBrowne879a872011-10-11 16:49:02 +1100953static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800954{
955 spin_lock_irq(&conf->resync_lock);
956 if (conf->barrier) {
Vitaly Mayatskikhfe630de2020-03-03 13:14:40 -0500957 struct bio_list *bio_list = current->bio_list;
NeilBrown0a27ec92006-01-06 00:20:13 -0800958 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100959 /* Wait for the barrier to drop.
960 * However if there are already pending
961 * requests (preventing the barrier from
962 * rising completely), and the
963 * pre-process bio queue isn't empty,
964 * then don't wait, as we need to empty
965 * that queue to get the nr_pending
966 * count down.
967 */
NeilBrown578b54a2016-11-14 16:30:21 +1100968 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100969 wait_event_lock_irq(conf->wait_barrier,
970 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200971 (atomic_read(&conf->nr_pending) &&
Vitaly Mayatskikhfe630de2020-03-03 13:14:40 -0500972 bio_list &&
973 (!bio_list_empty(&bio_list[0]) ||
974 !bio_list_empty(&bio_list[1]))) ||
975 /* move on if recovery thread is
976 * blocked by us
977 */
978 (conf->mddev->thread->tsk == current &&
979 test_bit(MD_RECOVERY_RUNNING,
980 &conf->mddev->recovery) &&
981 conf->nr_queued > 0),
Lukas Czernereed8c022012-11-30 11:42:40 +0100982 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800983 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200984 if (!conf->nr_waiting)
985 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800986 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200987 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800988 spin_unlock_irq(&conf->resync_lock);
989}
990
NeilBrowne879a872011-10-11 16:49:02 +1100991static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800992{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200993 if ((atomic_dec_and_test(&conf->nr_pending)) ||
994 (conf->array_freeze_pending))
995 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800996}
997
NeilBrowne2d59922013-06-12 11:01:22 +1000998static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -0800999{
1000 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001001 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001002 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001003 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001004 * This is called in the context of one normal IO request
1005 * that has failed. Thus any sync request that might be pending
1006 * will be blocked by nr_pending, and we need to wait for
1007 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001008 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001009 * must match the number of pending IOs (nr_pending) before
1010 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001011 */
1012 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001013 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001014 conf->barrier++;
1015 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001016 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001017 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001018 conf->resync_lock,
1019 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001020
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001021 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001022 spin_unlock_irq(&conf->resync_lock);
1023}
1024
NeilBrowne879a872011-10-11 16:49:02 +11001025static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001026{
1027 /* reverse the effect of the freeze */
1028 spin_lock_irq(&conf->resync_lock);
1029 conf->barrier--;
1030 conf->nr_waiting--;
1031 wake_up(&conf->wait_barrier);
1032 spin_unlock_irq(&conf->resync_lock);
1033}
1034
NeilBrownf8c9e742012-05-21 09:28:33 +10001035static sector_t choose_data_offset(struct r10bio *r10_bio,
1036 struct md_rdev *rdev)
1037{
1038 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1039 test_bit(R10BIO_Previous, &r10_bio->state))
1040 return rdev->data_offset;
1041 else
1042 return rdev->new_data_offset;
1043}
1044
NeilBrown57c67df2012-10-11 13:32:13 +11001045struct raid10_plug_cb {
1046 struct blk_plug_cb cb;
1047 struct bio_list pending;
1048 int pending_cnt;
1049};
1050
1051static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1052{
1053 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1054 cb);
1055 struct mddev *mddev = plug->cb.data;
1056 struct r10conf *conf = mddev->private;
1057 struct bio *bio;
1058
NeilBrown874807a2012-11-27 12:14:40 +11001059 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001060 spin_lock_irq(&conf->device_lock);
1061 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1062 conf->pending_count += plug->pending_cnt;
1063 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001064 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001065 md_wakeup_thread(mddev->thread);
1066 kfree(plug);
1067 return;
1068 }
1069
1070 /* we aren't scheduling, so we can do the write-out directly. */
1071 bio = bio_list_get(&plug->pending);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001072 md_bitmap_unplug(mddev->bitmap);
NeilBrown57c67df2012-10-11 13:32:13 +11001073 wake_up(&conf->wait_barrier);
1074
1075 while (bio) { /* submit pending writes */
1076 struct bio *next = bio->bi_next;
Christoph Hellwig309dca302021-01-24 11:02:34 +01001077 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001078 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001079 bio_set_dev(bio, rdev->bdev);
NeilBrowna9ae93c2016-11-04 16:46:03 +11001080 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08001081 bio_io_error(bio);
NeilBrowna9ae93c2016-11-04 16:46:03 +11001082 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig309dca302021-01-24 11:02:34 +01001083 !blk_queue_discard(bio->bi_bdev->bd_disk->queue)))
Shaohua Li32f9f572013-04-28 18:26:38 +08001084 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001085 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001086 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001087 submit_bio_noacct(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001088 bio = next;
1089 }
1090 kfree(plug);
1091}
1092
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001093/*
1094 * 1. Register the new request and wait if the reconstruction thread has put
1095 * up a bar for new requests. Continue immediately if no resync is active
1096 * currently.
1097 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1098 */
1099static void regular_request_wait(struct mddev *mddev, struct r10conf *conf,
1100 struct bio *bio, sector_t sectors)
1101{
1102 wait_barrier(conf);
1103 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1104 bio->bi_iter.bi_sector < conf->reshape_progress &&
1105 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1106 raid10_log(conf->mddev, "wait reshape");
1107 allow_barrier(conf);
1108 wait_event(conf->wait_barrier,
1109 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1110 conf->reshape_progress >= bio->bi_iter.bi_sector +
1111 sectors);
1112 wait_barrier(conf);
1113 }
1114}
1115
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001116static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1117 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
NeilBrowne879a872011-10-11 16:49:02 +11001119 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001121 const int op = bio_op(bio);
1122 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001123 int max_sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001124 struct md_rdev *rdev;
NeilBrown545250f2017-04-05 14:05:51 +10001125 char b[BDEVNAME_SIZE];
1126 int slot = r10_bio->read_slot;
1127 struct md_rdev *err_rdev = NULL;
1128 gfp_t gfp = GFP_NOIO;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001129
Kevin Vigor93decc52020-11-06 14:20:34 -08001130 if (slot >= 0 && r10_bio->devs[slot].rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001131 /*
1132 * This is an error retry, but we cannot
1133 * safely dereference the rdev in the r10_bio,
1134 * we must use the one in conf.
1135 * If it has already been disconnected (unlikely)
1136 * we lose the device name in error messages.
1137 */
1138 int disk;
1139 /*
1140 * As we are blocking raid10, it is a little safer to
1141 * use __GFP_HIGH.
1142 */
1143 gfp = GFP_NOIO | __GFP_HIGH;
1144
1145 rcu_read_lock();
1146 disk = r10_bio->devs[slot].devnum;
1147 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1148 if (err_rdev)
1149 bdevname(err_rdev->bdev, b);
1150 else {
1151 strcpy(b, "???");
1152 /* This never gets dereferenced */
1153 err_rdev = r10_bio->devs[slot].rdev;
1154 }
1155 rcu_read_unlock();
1156 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001157
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001158 regular_request_wait(mddev, conf, bio, r10_bio->sectors);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001159 rdev = read_balance(conf, r10_bio, &max_sectors);
1160 if (!rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001161 if (err_rdev) {
1162 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1163 mdname(mddev), b,
1164 (unsigned long long)r10_bio->sector);
1165 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001166 raid_end_bio_io(r10_bio);
1167 return;
1168 }
NeilBrown545250f2017-04-05 14:05:51 +10001169 if (err_rdev)
1170 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1171 mdname(mddev),
1172 bdevname(rdev->bdev, b),
1173 (unsigned long long)r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001174 if (max_sectors < bio_sectors(bio)) {
1175 struct bio *split = bio_split(bio, max_sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001176 gfp, &conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001177 bio_chain(split, bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001178 allow_barrier(conf);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001179 submit_bio_noacct(bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001180 wait_barrier(conf);
NeilBrownfc9977d2017-04-05 14:05:51 +10001181 bio = split;
1182 r10_bio->master_bio = bio;
1183 r10_bio->sectors = max_sectors;
1184 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001185 slot = r10_bio->read_slot;
1186
Kent Overstreetafeee512018-05-20 18:25:52 -04001187 read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001188
1189 r10_bio->devs[slot].bio = read_bio;
1190 r10_bio->devs[slot].rdev = rdev;
1191
1192 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1193 choose_data_offset(r10_bio, rdev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001194 bio_set_dev(read_bio, rdev->bdev);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001195 read_bio->bi_end_io = raid10_end_read_request;
1196 bio_set_op_attrs(read_bio, op, do_sync);
1197 if (test_bit(FailFast, &rdev->flags) &&
1198 test_bit(R10BIO_FailFast, &r10_bio->state))
1199 read_bio->bi_opf |= MD_FAILFAST;
1200 read_bio->bi_private = r10_bio;
1201
1202 if (mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +01001203 trace_block_bio_remap(read_bio, disk_devt(mddev->gendisk),
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001204 r10_bio->sector);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001205 submit_bio_noacct(read_bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001206 return;
1207}
1208
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001209static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1210 struct bio *bio, bool replacement,
NeilBrownfc9977d2017-04-05 14:05:51 +10001211 int n_copy)
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001212{
1213 const int op = bio_op(bio);
1214 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1215 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1216 unsigned long flags;
1217 struct blk_plug_cb *cb;
1218 struct raid10_plug_cb *plug = NULL;
1219 struct r10conf *conf = mddev->private;
1220 struct md_rdev *rdev;
1221 int devnum = r10_bio->devs[n_copy].devnum;
1222 struct bio *mbio;
1223
1224 if (replacement) {
1225 rdev = conf->mirrors[devnum].replacement;
1226 if (rdev == NULL) {
1227 /* Replacement just got moved to main 'rdev' */
1228 smp_mb();
1229 rdev = conf->mirrors[devnum].rdev;
1230 }
1231 } else
1232 rdev = conf->mirrors[devnum].rdev;
1233
Kent Overstreetafeee512018-05-20 18:25:52 -04001234 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001235 if (replacement)
1236 r10_bio->devs[n_copy].repl_bio = mbio;
1237 else
1238 r10_bio->devs[n_copy].bio = mbio;
1239
1240 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1241 choose_data_offset(r10_bio, rdev));
Christoph Hellwig74d46992017-08-23 19:10:32 +02001242 bio_set_dev(mbio, rdev->bdev);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001243 mbio->bi_end_io = raid10_end_write_request;
1244 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1245 if (!replacement && test_bit(FailFast,
1246 &conf->mirrors[devnum].rdev->flags)
1247 && enough(conf, devnum))
1248 mbio->bi_opf |= MD_FAILFAST;
1249 mbio->bi_private = r10_bio;
1250
1251 if (conf->mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +01001252 trace_block_bio_remap(mbio, disk_devt(conf->mddev->gendisk),
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001253 r10_bio->sector);
1254 /* flush_pending_writes() needs access to the rdev so...*/
Christoph Hellwig309dca302021-01-24 11:02:34 +01001255 mbio->bi_bdev = (void *)rdev;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001256
1257 atomic_inc(&r10_bio->remaining);
1258
1259 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1260 if (cb)
1261 plug = container_of(cb, struct raid10_plug_cb, cb);
1262 else
1263 plug = NULL;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001264 if (plug) {
1265 bio_list_add(&plug->pending, mbio);
1266 plug->pending_cnt++;
1267 } else {
Shaohua Li23b245c2017-05-10 08:47:11 -07001268 spin_lock_irqsave(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001269 bio_list_add(&conf->pending_bio_list, mbio);
1270 conf->pending_count++;
Shaohua Li23b245c2017-05-10 08:47:11 -07001271 spin_unlock_irqrestore(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001272 md_wakeup_thread(mddev->thread);
Shaohua Li23b245c2017-05-10 08:47:11 -07001273 }
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001274}
1275
Xiao Nif2e7e262021-02-04 15:50:45 +08001276static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1277{
1278 int i;
1279 struct r10conf *conf = mddev->private;
1280 struct md_rdev *blocked_rdev;
1281
1282retry_wait:
1283 blocked_rdev = NULL;
1284 rcu_read_lock();
1285 for (i = 0; i < conf->copies; i++) {
1286 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1287 struct md_rdev *rrdev = rcu_dereference(
1288 conf->mirrors[i].replacement);
1289 if (rdev == rrdev)
1290 rrdev = NULL;
1291 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1292 atomic_inc(&rdev->nr_pending);
1293 blocked_rdev = rdev;
1294 break;
1295 }
1296 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1297 atomic_inc(&rrdev->nr_pending);
1298 blocked_rdev = rrdev;
1299 break;
1300 }
1301
1302 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1303 sector_t first_bad;
1304 sector_t dev_sector = r10_bio->devs[i].addr;
1305 int bad_sectors;
1306 int is_bad;
1307
1308 /*
1309 * Discard request doesn't care the write result
1310 * so it doesn't need to wait blocked disk here.
1311 */
1312 if (!r10_bio->sectors)
1313 continue;
1314
1315 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1316 &first_bad, &bad_sectors);
1317 if (is_bad < 0) {
1318 /*
1319 * Mustn't write here until the bad block
1320 * is acknowledged
1321 */
1322 atomic_inc(&rdev->nr_pending);
1323 set_bit(BlockedBadBlocks, &rdev->flags);
1324 blocked_rdev = rdev;
1325 break;
1326 }
1327 }
1328 }
1329 rcu_read_unlock();
1330
1331 if (unlikely(blocked_rdev)) {
1332 /* Have to wait for this device to get unblocked, then retry */
1333 allow_barrier(conf);
1334 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1335 __func__, blocked_rdev->raid_disk);
1336 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1337 wait_barrier(conf);
1338 goto retry_wait;
1339 }
1340}
1341
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001342static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1343 struct r10bio *r10_bio)
1344{
1345 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int i;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001347 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001348 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Guoqing Jiangcb8a7a72017-10-24 15:11:51 +08001350 if ((mddev_is_clustered(mddev) &&
1351 md_cluster_ops->area_resyncing(mddev, WRITE,
1352 bio->bi_iter.bi_sector,
1353 bio_end_sector(bio)))) {
1354 DEFINE_WAIT(w);
1355 for (;;) {
1356 prepare_to_wait(&conf->wait_barrier,
1357 &w, TASK_IDLE);
1358 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1359 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1360 break;
1361 schedule();
1362 }
1363 finish_wait(&conf->wait_barrier, &w);
1364 }
1365
NeilBrownfc9977d2017-04-05 14:05:51 +10001366 sectors = r10_bio->sectors;
Guoqing Jiangcaea3c42018-12-07 18:24:21 +08001367 regular_request_wait(mddev, conf, bio, sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001368 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001369 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001370 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1371 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1372 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1373 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001374 /* Need to update reshape_position in metadata */
1375 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001376 set_mask_bits(&mddev->sb_flags, 0,
1377 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001378 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001379 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001380 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001381 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001382
1383 conf->reshape_safe = mddev->reshape_position;
1384 }
1385
NeilBrown34db0cd2011-10-11 16:50:01 +11001386 if (conf->pending_count >= max_queued_requests) {
1387 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001388 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001389 wait_event(conf->wait_barrier,
1390 conf->pending_count < max_queued_requests);
1391 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001392 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * inc refcount on their rdev. Record them by setting
1394 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001395 * If there are known/acknowledged bad blocks on any device
1396 * on which we have seen a write error, we want to avoid
1397 * writing to those blocks. This potentially requires several
1398 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001399 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001401
NeilBrown69335ef2011-12-23 10:17:54 +11001402 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 raid10_find_phys(conf, r10_bio);
Xiao Nif2e7e262021-02-04 15:50:45 +08001404
1405 wait_blocked_dev(mddev, r10_bio);
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001408 max_sectors = r10_bio->sectors;
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 for (i = 0; i < conf->copies; i++) {
1411 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001412 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001413 struct md_rdev *rrdev = rcu_dereference(
1414 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001415 if (rdev == rrdev)
1416 rrdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001417 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001418 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001419 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001420 rrdev = NULL;
1421
NeilBrownd4432c22011-07-28 11:39:24 +10001422 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001423 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001424
1425 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001426 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001427 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001428 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001429 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001430 sector_t first_bad;
1431 sector_t dev_sector = r10_bio->devs[i].addr;
1432 int bad_sectors;
1433 int is_bad;
1434
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001435 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001436 &first_bad, &bad_sectors);
NeilBrownd4432c22011-07-28 11:39:24 +10001437 if (is_bad && first_bad <= dev_sector) {
1438 /* Cannot write here at all */
1439 bad_sectors -= (dev_sector - first_bad);
1440 if (bad_sectors < max_sectors)
1441 /* Mustn't write more than bad_sectors
1442 * to other devices yet
1443 */
1444 max_sectors = bad_sectors;
1445 /* We don't set R10BIO_Degraded as that
1446 * only applies if the disk is missing,
1447 * so it might be re-added, and we want to
1448 * know to recover this chunk.
1449 * In this case the device is here, and the
1450 * fact that this chunk is not in-sync is
1451 * recorded in the bad block log.
1452 */
1453 continue;
1454 }
1455 if (is_bad) {
1456 int good_sectors = first_bad - dev_sector;
1457 if (good_sectors < max_sectors)
1458 max_sectors = good_sectors;
1459 }
1460 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001461 if (rdev) {
1462 r10_bio->devs[i].bio = bio;
1463 atomic_inc(&rdev->nr_pending);
1464 }
NeilBrown475b0322011-12-23 10:17:55 +11001465 if (rrdev) {
1466 r10_bio->devs[i].repl_bio = bio;
1467 atomic_inc(&rrdev->nr_pending);
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 rcu_read_unlock();
1471
NeilBrown6b6c8112017-03-15 14:05:13 +11001472 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001473 r10_bio->sectors = max_sectors;
NeilBrownfc9977d2017-04-05 14:05:51 +10001474
1475 if (r10_bio->sectors < bio_sectors(bio)) {
1476 struct bio *split = bio_split(bio, r10_bio->sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001477 GFP_NOIO, &conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001478 bio_chain(split, bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001479 allow_barrier(conf);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001480 submit_bio_noacct(bio);
Guoqing Jiange820d552018-12-19 14:19:25 +08001481 wait_barrier(conf);
NeilBrownfc9977d2017-04-05 14:05:51 +10001482 bio = split;
1483 r10_bio->master_bio = bio;
NeilBrownd4432c22011-07-28 11:39:24 +10001484 }
NeilBrownd4432c22011-07-28 11:39:24 +10001485
NeilBrown4e780642010-10-19 12:54:01 +11001486 atomic_set(&r10_bio->remaining, 1);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001487 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 for (i = 0; i < conf->copies; i++) {
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001490 if (r10_bio->devs[i].bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001491 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001492 if (r10_bio->devs[i].repl_bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001493 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
NeilBrownd4432c22011-07-28 11:39:24 +10001494 }
NeilBrown079fa162011-09-10 17:21:23 +10001495 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001496}
1497
NeilBrownfc9977d2017-04-05 14:05:51 +10001498static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001499{
1500 struct r10conf *conf = mddev->private;
1501 struct r10bio *r10_bio;
1502
Kent Overstreetafeee512018-05-20 18:25:52 -04001503 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001504
1505 r10_bio->master_bio = bio;
NeilBrownfc9977d2017-04-05 14:05:51 +10001506 r10_bio->sectors = sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001507
1508 r10_bio->mddev = mddev;
1509 r10_bio->sector = bio->bi_iter.bi_sector;
1510 r10_bio->state = 0;
Kevin Vigor93decc52020-11-06 14:20:34 -08001511 r10_bio->read_slot = -1;
Xiao Nic2968282021-02-04 15:50:44 +08001512 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1513 conf->geo.raid_disks);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001514
1515 if (bio_data_dir(bio) == READ)
1516 raid10_read_request(mddev, bio, r10_bio);
1517 else
1518 raid10_write_request(mddev, bio, r10_bio);
1519}
1520
Xiao Ni254c2712021-02-04 15:50:47 +08001521static void raid_end_discard_bio(struct r10bio *r10bio)
1522{
1523 struct r10conf *conf = r10bio->mddev->private;
1524 struct r10bio *first_r10bio;
1525
1526 while (atomic_dec_and_test(&r10bio->remaining)) {
1527
1528 allow_barrier(conf);
1529
1530 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1531 first_r10bio = (struct r10bio *)r10bio->master_bio;
1532 free_r10bio(r10bio);
1533 r10bio = first_r10bio;
1534 } else {
1535 md_write_end(r10bio->mddev);
1536 bio_endio(r10bio->master_bio);
1537 free_r10bio(r10bio);
1538 break;
1539 }
1540 }
1541}
1542
Xiao Nid30588b2021-02-04 15:50:46 +08001543static void raid10_end_discard_request(struct bio *bio)
1544{
1545 struct r10bio *r10_bio = bio->bi_private;
1546 struct r10conf *conf = r10_bio->mddev->private;
1547 struct md_rdev *rdev = NULL;
1548 int dev;
1549 int slot, repl;
1550
1551 /*
1552 * We don't care the return value of discard bio
1553 */
1554 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1555 set_bit(R10BIO_Uptodate, &r10_bio->state);
1556
1557 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1558 if (repl)
1559 rdev = conf->mirrors[dev].replacement;
1560 if (!rdev) {
1561 /*
1562 * raid10_remove_disk uses smp_mb to make sure rdev is set to
1563 * replacement before setting replacement to NULL. It can read
1564 * rdev first without barrier protect even replacment is NULL
1565 */
1566 smp_rmb();
1567 rdev = conf->mirrors[dev].rdev;
1568 }
1569
Xiao Ni254c2712021-02-04 15:50:47 +08001570 raid_end_discard_bio(r10_bio);
Xiao Nid30588b2021-02-04 15:50:46 +08001571 rdev_dec_pending(rdev, conf->mddev);
1572}
1573
1574/*
1575 * There are some limitations to handle discard bio
1576 * 1st, the discard size is bigger than stripe_size*2.
1577 * 2st, if the discard bio spans reshape progress, we use the old way to
1578 * handle discard bio
1579 */
1580static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1581{
1582 struct r10conf *conf = mddev->private;
1583 struct geom *geo = &conf->geo;
Xiao Ni254c2712021-02-04 15:50:47 +08001584 int far_copies = geo->far_copies;
1585 bool first_copy = true;
1586 struct r10bio *r10_bio, *first_r10bio;
Xiao Nid30588b2021-02-04 15:50:46 +08001587 struct bio *split;
1588 int disk;
1589 sector_t chunk;
1590 unsigned int stripe_size;
1591 unsigned int stripe_data_disks;
1592 sector_t split_size;
1593 sector_t bio_start, bio_end;
1594 sector_t first_stripe_index, last_stripe_index;
1595 sector_t start_disk_offset;
1596 unsigned int start_disk_index;
1597 sector_t end_disk_offset;
1598 unsigned int end_disk_index;
1599 unsigned int remainder;
1600
1601 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1602 return -EAGAIN;
1603
1604 wait_barrier(conf);
1605
1606 /*
1607 * Check reshape again to avoid reshape happens after checking
1608 * MD_RECOVERY_RESHAPE and before wait_barrier
1609 */
1610 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1611 goto out;
1612
1613 if (geo->near_copies)
1614 stripe_data_disks = geo->raid_disks / geo->near_copies +
1615 geo->raid_disks % geo->near_copies;
1616 else
1617 stripe_data_disks = geo->raid_disks;
1618
1619 stripe_size = stripe_data_disks << geo->chunk_shift;
1620
1621 bio_start = bio->bi_iter.bi_sector;
1622 bio_end = bio_end_sector(bio);
1623
1624 /*
1625 * Maybe one discard bio is smaller than strip size or across one
1626 * stripe and discard region is larger than one stripe size. For far
1627 * offset layout, if the discard region is not aligned with stripe
1628 * size, there is hole when we submit discard bio to member disk.
1629 * For simplicity, we only handle discard bio which discard region
1630 * is bigger than stripe_size * 2
1631 */
1632 if (bio_sectors(bio) < stripe_size*2)
1633 goto out;
1634
1635 /*
1636 * Keep bio aligned with strip size.
1637 */
1638 div_u64_rem(bio_start, stripe_size, &remainder);
1639 if (remainder) {
1640 split_size = stripe_size - remainder;
1641 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1642 bio_chain(split, bio);
1643 allow_barrier(conf);
1644 /* Resend the fist split part */
1645 submit_bio_noacct(split);
1646 wait_barrier(conf);
1647 }
1648 div_u64_rem(bio_end, stripe_size, &remainder);
1649 if (remainder) {
1650 split_size = bio_sectors(bio) - remainder;
1651 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1652 bio_chain(split, bio);
1653 allow_barrier(conf);
1654 /* Resend the second split part */
1655 submit_bio_noacct(bio);
1656 bio = split;
1657 wait_barrier(conf);
1658 }
1659
Xiao Nid30588b2021-02-04 15:50:46 +08001660 bio_start = bio->bi_iter.bi_sector;
1661 bio_end = bio_end_sector(bio);
1662
1663 /*
1664 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1665 * One stripe contains the chunks from all member disk (one chunk from
1666 * one disk at the same HBA address). For layout detail, see 'man md 4'
1667 */
1668 chunk = bio_start >> geo->chunk_shift;
1669 chunk *= geo->near_copies;
1670 first_stripe_index = chunk;
1671 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1672 if (geo->far_offset)
1673 first_stripe_index *= geo->far_copies;
1674 start_disk_offset = (bio_start & geo->chunk_mask) +
1675 (first_stripe_index << geo->chunk_shift);
1676
1677 chunk = bio_end >> geo->chunk_shift;
1678 chunk *= geo->near_copies;
1679 last_stripe_index = chunk;
1680 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1681 if (geo->far_offset)
1682 last_stripe_index *= geo->far_copies;
1683 end_disk_offset = (bio_end & geo->chunk_mask) +
1684 (last_stripe_index << geo->chunk_shift);
1685
Xiao Ni254c2712021-02-04 15:50:47 +08001686retry_discard:
1687 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1688 r10_bio->mddev = mddev;
1689 r10_bio->state = 0;
1690 r10_bio->sectors = 0;
1691 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1692 wait_blocked_dev(mddev, r10_bio);
1693
1694 /*
1695 * For far layout it needs more than one r10bio to cover all regions.
1696 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1697 * to record the discard bio. Other r10bio->master_bio record the first
1698 * r10bio. The first r10bio only release after all other r10bios finish.
1699 * The discard bio returns only first r10bio finishes
1700 */
1701 if (first_copy) {
1702 r10_bio->master_bio = bio;
1703 set_bit(R10BIO_Discard, &r10_bio->state);
1704 first_copy = false;
1705 first_r10bio = r10_bio;
1706 } else
1707 r10_bio->master_bio = (struct bio *)first_r10bio;
1708
Xiao Nid30588b2021-02-04 15:50:46 +08001709 rcu_read_lock();
1710 for (disk = 0; disk < geo->raid_disks; disk++) {
1711 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1712 struct md_rdev *rrdev = rcu_dereference(
1713 conf->mirrors[disk].replacement);
1714
1715 r10_bio->devs[disk].bio = NULL;
1716 r10_bio->devs[disk].repl_bio = NULL;
1717
1718 if (rdev && (test_bit(Faulty, &rdev->flags)))
1719 rdev = NULL;
1720 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1721 rrdev = NULL;
1722 if (!rdev && !rrdev)
1723 continue;
1724
1725 if (rdev) {
1726 r10_bio->devs[disk].bio = bio;
1727 atomic_inc(&rdev->nr_pending);
1728 }
1729 if (rrdev) {
1730 r10_bio->devs[disk].repl_bio = bio;
1731 atomic_inc(&rrdev->nr_pending);
1732 }
1733 }
1734 rcu_read_unlock();
1735
1736 atomic_set(&r10_bio->remaining, 1);
1737 for (disk = 0; disk < geo->raid_disks; disk++) {
1738 sector_t dev_start, dev_end;
1739 struct bio *mbio, *rbio = NULL;
1740 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1741 struct md_rdev *rrdev = rcu_dereference(
1742 conf->mirrors[disk].replacement);
1743
1744 /*
1745 * Now start to calculate the start and end address for each disk.
1746 * The space between dev_start and dev_end is the discard region.
1747 *
1748 * For dev_start, it needs to consider three conditions:
1749 * 1st, the disk is before start_disk, you can imagine the disk in
1750 * the next stripe. So the dev_start is the start address of next
1751 * stripe.
1752 * 2st, the disk is after start_disk, it means the disk is at the
1753 * same stripe of first disk
1754 * 3st, the first disk itself, we can use start_disk_offset directly
1755 */
1756 if (disk < start_disk_index)
1757 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1758 else if (disk > start_disk_index)
1759 dev_start = first_stripe_index * mddev->chunk_sectors;
1760 else
1761 dev_start = start_disk_offset;
1762
1763 if (disk < end_disk_index)
1764 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1765 else if (disk > end_disk_index)
1766 dev_end = last_stripe_index * mddev->chunk_sectors;
1767 else
1768 dev_end = end_disk_offset;
1769
1770 /*
1771 * It only handles discard bio which size is >= stripe size, so
1772 * dev_end > dev_start all the time
1773 */
1774 if (r10_bio->devs[disk].bio) {
1775 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1776 mbio->bi_end_io = raid10_end_discard_request;
1777 mbio->bi_private = r10_bio;
1778 r10_bio->devs[disk].bio = mbio;
1779 r10_bio->devs[disk].devnum = disk;
1780 atomic_inc(&r10_bio->remaining);
1781 md_submit_discard_bio(mddev, rdev, mbio,
1782 dev_start + choose_data_offset(r10_bio, rdev),
1783 dev_end - dev_start);
1784 bio_endio(mbio);
1785 }
1786 if (r10_bio->devs[disk].repl_bio) {
1787 rbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1788 rbio->bi_end_io = raid10_end_discard_request;
1789 rbio->bi_private = r10_bio;
1790 r10_bio->devs[disk].repl_bio = rbio;
1791 r10_bio->devs[disk].devnum = disk;
1792 atomic_inc(&r10_bio->remaining);
1793 md_submit_discard_bio(mddev, rrdev, rbio,
1794 dev_start + choose_data_offset(r10_bio, rrdev),
1795 dev_end - dev_start);
1796 bio_endio(rbio);
1797 }
1798 }
1799
Xiao Ni254c2712021-02-04 15:50:47 +08001800 if (!geo->far_offset && --far_copies) {
1801 first_stripe_index += geo->stride >> geo->chunk_shift;
1802 start_disk_offset += geo->stride;
1803 last_stripe_index += geo->stride >> geo->chunk_shift;
1804 end_disk_offset += geo->stride;
1805 atomic_inc(&first_r10bio->remaining);
1806 raid_end_discard_bio(r10_bio);
1807 wait_barrier(conf);
1808 goto retry_discard;
Xiao Nid30588b2021-02-04 15:50:46 +08001809 }
1810
Xiao Ni254c2712021-02-04 15:50:47 +08001811 raid_end_discard_bio(r10_bio);
1812
Xiao Nid30588b2021-02-04 15:50:46 +08001813 return 0;
1814out:
1815 allow_barrier(conf);
1816 return -EAGAIN;
1817}
1818
NeilBrowncc27b0c2017-06-05 16:49:39 +10001819static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001820{
1821 struct r10conf *conf = mddev->private;
1822 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1823 int chunk_sects = chunk_mask + 1;
NeilBrownfc9977d2017-04-05 14:05:51 +10001824 int sectors = bio_sectors(bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001825
David Jeffery775d7832019-09-16 13:15:14 -04001826 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1827 && md_flush_request(mddev, bio))
NeilBrowncc27b0c2017-06-05 16:49:39 +10001828 return true;
Kent Overstreet20d01892013-11-23 18:21:01 -08001829
NeilBrowncc27b0c2017-06-05 16:49:39 +10001830 if (!md_write_start(mddev, bio))
1831 return false;
1832
Xiao Nid30588b2021-02-04 15:50:46 +08001833 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1834 if (!raid10_handle_discard(mddev, bio))
1835 return true;
1836
NeilBrownfc9977d2017-04-05 14:05:51 +10001837 /*
1838 * If this request crosses a chunk boundary, we need to split
1839 * it.
1840 */
1841 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1842 sectors > chunk_sects
1843 && (conf->geo.near_copies < conf->geo.raid_disks
1844 || conf->prev.near_copies <
1845 conf->prev.raid_disks)))
1846 sectors = chunk_sects -
1847 (bio->bi_iter.bi_sector &
1848 (chunk_sects - 1));
1849 __make_request(mddev, bio, sectors);
NeilBrown079fa162011-09-10 17:21:23 +10001850
1851 /* In case raid10d snuck in to freeze_array */
1852 wake_up(&conf->wait_barrier);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001853 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Shaohua Li849674e2016-01-20 13:52:20 -08001856static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
NeilBrowne879a872011-10-11 16:49:02 +11001858 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 int i;
1860
NeilBrown5cf00fc2012-05-21 09:28:20 +10001861 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001862 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001863 if (conf->geo.near_copies > 1)
1864 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1865 if (conf->geo.far_copies > 1) {
1866 if (conf->geo.far_offset)
1867 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001868 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001869 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001870 if (conf->geo.far_set_size != conf->geo.raid_disks)
1871 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001872 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001873 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1874 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001875 rcu_read_lock();
1876 for (i = 0; i < conf->geo.raid_disks; i++) {
1877 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1878 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1879 }
1880 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 seq_printf(seq, "]");
1882}
1883
NeilBrown700c7212011-07-27 11:00:36 +10001884/* check if there are enough drives for
1885 * every block to appear on atleast one.
1886 * Don't consider the device numbered 'ignore'
1887 * as we might be about to remove it.
1888 */
NeilBrown635f6412013-06-11 14:57:09 +10001889static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001890{
1891 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001892 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001893 int disks, ncopies;
1894 if (previous) {
1895 disks = conf->prev.raid_disks;
1896 ncopies = conf->prev.near_copies;
1897 } else {
1898 disks = conf->geo.raid_disks;
1899 ncopies = conf->geo.near_copies;
1900 }
NeilBrown700c7212011-07-27 11:00:36 +10001901
NeilBrown725d6e52013-06-11 15:08:03 +10001902 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001903 do {
1904 int n = conf->copies;
1905 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001906 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001907 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001908 struct md_rdev *rdev;
1909 if (this != ignore &&
1910 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1911 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001912 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001913 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001914 }
1915 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001916 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001917 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001918 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001919 has_enough = 1;
1920out:
1921 rcu_read_unlock();
1922 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001923}
1924
NeilBrownf8c9e742012-05-21 09:28:33 +10001925static int enough(struct r10conf *conf, int ignore)
1926{
NeilBrown635f6412013-06-11 14:57:09 +10001927 /* when calling 'enough', both 'prev' and 'geo' must
1928 * be stable.
1929 * This is ensured if ->reconfig_mutex or ->device_lock
1930 * is held.
1931 */
1932 return _enough(conf, 0, ignore) &&
1933 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001934}
1935
Shaohua Li849674e2016-01-20 13:52:20 -08001936static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001939 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001940 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 /*
1943 * If it is not operational, then we have already marked it as dead
Guoqing Jiang9a567842019-07-24 11:09:19 +02001944 * else if it is the last working disks with "fail_last_dev == false",
1945 * ignore the error, let the next level up know.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 * else mark the drive as failed
1947 */
NeilBrown635f6412013-06-11 14:57:09 +10001948 spin_lock_irqsave(&conf->device_lock, flags);
Guoqing Jiang9a567842019-07-24 11:09:19 +02001949 if (test_bit(In_sync, &rdev->flags) && !mddev->fail_last_dev
NeilBrown635f6412013-06-11 14:57:09 +10001950 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /*
1952 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001954 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001955 return;
1956 }
NeilBrown2446dba2014-07-31 10:16:29 +10001957 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001958 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001959 /*
1960 * If recovery is running, make sure it aborts.
1961 */
1962 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001963 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001964 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001965 set_mask_bits(&mddev->sb_flags, 0,
1966 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001967 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001968 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1969 "md/raid10:%s: Operation continuing on %d devices.\n",
1970 mdname(mddev), bdevname(rdev->bdev, b),
1971 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
NeilBrowne879a872011-10-11 16:49:02 +11001974static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001977 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
NeilBrown08464e02016-11-02 14:16:50 +11001979 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001981 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return;
1983 }
NeilBrown08464e02016-11-02 14:16:50 +11001984 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1985 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
NeilBrown4056ca52016-06-02 16:19:52 +10001987 /* This is only called with ->reconfix_mutex held, so
1988 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001989 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001991 rdev = conf->mirrors[i].rdev;
1992 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001993 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1994 i, !test_bit(In_sync, &rdev->flags),
1995 !test_bit(Faulty, &rdev->flags),
1996 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998}
1999
NeilBrowne879a872011-10-11 16:49:02 +11002000static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
NeilBrown0a27ec92006-01-06 00:20:13 -08002002 wait_barrier(conf);
2003 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Kent Overstreetafeee512018-05-20 18:25:52 -04002005 mempool_exit(&conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
NeilBrownfd01b882011-10-11 16:47:53 +11002008static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 int i;
NeilBrowne879a872011-10-11 16:49:02 +11002011 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002012 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10002013 int count = 0;
2014 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /*
2017 * Find all non-in_sync disks within the RAID10 configuration
2018 * and mark them in_sync
2019 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002020 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11002022 if (tmp->replacement
2023 && tmp->replacement->recovery_offset == MaxSector
2024 && !test_bit(Faulty, &tmp->replacement->flags)
2025 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2026 /* Replacement has just become active */
2027 if (!tmp->rdev
2028 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2029 count++;
2030 if (tmp->rdev) {
2031 /* Replaced device not technically faulty,
2032 * but we need to be sure it gets removed
2033 * and never re-added.
2034 */
2035 set_bit(Faulty, &tmp->rdev->flags);
2036 sysfs_notify_dirent_safe(
2037 tmp->rdev->sysfs_state);
2038 }
2039 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2040 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11002041 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11002042 && !test_bit(Faulty, &tmp->rdev->flags)
2043 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10002044 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002045 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047 }
NeilBrown6b965622010-08-18 11:56:59 +10002048 spin_lock_irqsave(&conf->device_lock, flags);
2049 mddev->degraded -= count;
2050 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10002053 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
NeilBrownfd01b882011-10-11 16:47:53 +11002056static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
NeilBrowne879a872011-10-11 16:49:02 +11002058 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10002059 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10002061 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002062 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if (mddev->recovery_cp < MaxSector)
2065 /* only hot-add to in-sync arrays, as recovery is
2066 * very different from resync
2067 */
Neil Brown199050e2008-06-28 08:31:33 +10002068 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10002069 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10002070 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Dan Williams1501efa2016-01-13 16:00:07 -08002072 if (md_integrity_add_rdev(rdev, mddev))
2073 return -ENXIO;
2074
NeilBrowna53a6c82008-11-06 17:28:20 +11002075 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10002076 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Namhyung Kim2c4193d2011-07-18 17:38:43 +10002078 if (rdev->saved_raid_disk >= first &&
Shaohua Li9e753ba2018-10-14 17:05:07 -07002079 rdev->saved_raid_disk < conf->geo.raid_disks &&
NeilBrown6cce3b22006-01-06 00:20:16 -08002080 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2081 mirror = rdev->saved_raid_disk;
2082 else
Neil Brown6c2fce22008-06-28 08:31:31 +10002083 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10002084 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002085 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10002086 if (p->recovery_disabled == mddev->recovery_disabled)
2087 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11002088 if (p->rdev) {
2089 if (!test_bit(WantReplacement, &p->rdev->flags) ||
2090 p->replacement != NULL)
2091 continue;
2092 clear_bit(In_sync, &rdev->flags);
2093 set_bit(Replacement, &rdev->flags);
2094 rdev->raid_disk = mirror;
2095 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05002096 if (mddev->gendisk)
2097 disk_stack_limits(mddev->gendisk, rdev->bdev,
2098 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11002099 conf->fullsync = 1;
2100 rcu_assign_pointer(p->replacement, rdev);
2101 break;
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Jonathan Brassow9092c022013-05-02 14:19:24 -05002104 if (mddev->gendisk)
2105 disk_stack_limits(mddev->gendisk, rdev->bdev,
2106 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
NeilBrown2bb77732011-07-27 11:00:36 +10002108 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11002109 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10002110 rdev->raid_disk = mirror;
2111 err = 0;
2112 if (rdev->saved_raid_disk != mirror)
2113 conf->fullsync = 1;
2114 rcu_assign_pointer(p->rdev, rdev);
2115 break;
2116 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11002117 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Bart Van Assche8b904b52018-03-07 17:10:10 -08002118 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
Shaohua Li532a2a32012-10-11 13:30:52 +11002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10002121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
NeilBrownb8321b62011-12-23 10:17:51 +11002124static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
NeilBrowne879a872011-10-11 16:49:02 +11002126 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11002128 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11002129 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002130 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11002133 if (rdev == p->rdev)
2134 rdevp = &p->rdev;
2135 else if (rdev == p->replacement)
2136 rdevp = &p->replacement;
2137 else
2138 return 0;
2139
2140 if (test_bit(In_sync, &rdev->flags) ||
2141 atomic_read(&rdev->nr_pending)) {
2142 err = -EBUSY;
2143 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
NeilBrownd787be42016-06-02 16:19:53 +10002145 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11002146 * is not possible.
2147 */
2148 if (!test_bit(Faulty, &rdev->flags) &&
2149 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11002150 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10002151 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11002152 enough(conf, -1)) {
2153 err = -EBUSY;
2154 goto abort;
2155 }
2156 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10002157 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2158 synchronize_rcu();
2159 if (atomic_read(&rdev->nr_pending)) {
2160 /* lost the race, try later */
2161 err = -EBUSY;
2162 *rdevp = rdev;
2163 goto abort;
2164 }
2165 }
2166 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11002167 /* We must have just cleared 'rdev' */
2168 p->rdev = p->replacement;
2169 clear_bit(Replacement, &p->replacement->flags);
2170 smp_mb(); /* Make sure other CPUs may see both as identical
2171 * but will never see neither -- if they are careful.
2172 */
2173 p->replacement = NULL;
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08002174 }
NeilBrown4ca40c22011-12-23 10:17:55 +11002175
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08002176 clear_bit(WantReplacement, &rdev->flags);
NeilBrownc8ab9032011-12-23 10:17:54 +11002177 err = md_integrity_register(mddev);
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179abort:
2180
2181 print_conf(conf);
2182 return err;
2183}
2184
Ming Lei81fa1522017-03-17 00:12:32 +08002185static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
NeilBrowne879a872011-10-11 16:49:02 +11002187 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002188
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002189 if (!bio->bi_status)
NeilBrown0eb3ff12006-01-06 00:20:29 -08002190 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10002191 else
2192 /* The write handler will notice the lack of
2193 * R10BIO_Uptodate and record any errors etc
2194 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08002195 atomic_add(r10_bio->sectors,
2196 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /* for reconstruct, we always reschedule after a read.
2199 * for resync, only after all reads
2200 */
NeilBrown73d5c382009-02-25 13:18:47 +11002201 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2203 atomic_dec_and_test(&r10_bio->remaining)) {
2204 /* we have read all the blocks,
2205 * do the comparison in process context in raid10d
2206 */
2207 reschedule_retry(r10_bio);
2208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Ming Lei81fa1522017-03-17 00:12:32 +08002211static void end_sync_read(struct bio *bio)
2212{
Ming Leif0250612017-03-17 00:12:33 +08002213 struct r10bio *r10_bio = get_resync_r10bio(bio);
Ming Lei81fa1522017-03-17 00:12:32 +08002214 struct r10conf *conf = r10_bio->mddev->private;
2215 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2216
2217 __end_sync_read(r10_bio, bio, d);
2218}
2219
2220static void end_reshape_read(struct bio *bio)
2221{
Ming Leif0250612017-03-17 00:12:33 +08002222 /* reshape read bio isn't allocated from r10buf_pool */
Ming Lei81fa1522017-03-17 00:12:32 +08002223 struct r10bio *r10_bio = bio->bi_private;
2224
2225 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2226}
2227
NeilBrown9f2c9d12011-10-11 16:48:43 +11002228static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002229{
NeilBrownfd01b882011-10-11 16:47:53 +11002230 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10002231
2232 while (atomic_dec_and_test(&r10_bio->remaining)) {
2233 if (r10_bio->master_bio == NULL) {
2234 /* the primary of several recovery bios */
2235 sector_t s = r10_bio->sectors;
2236 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2237 test_bit(R10BIO_WriteError, &r10_bio->state))
2238 reschedule_retry(r10_bio);
2239 else
2240 put_buf(r10_bio);
2241 md_done_sync(mddev, s, 1);
2242 break;
2243 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002244 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10002245 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2246 test_bit(R10BIO_WriteError, &r10_bio->state))
2247 reschedule_retry(r10_bio);
2248 else
2249 put_buf(r10_bio);
2250 r10_bio = r10_bio2;
2251 }
2252 }
2253}
2254
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002255static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Ming Leif0250612017-03-17 00:12:33 +08002257 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrownfd01b882011-10-11 16:47:53 +11002258 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002259 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10002260 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10002261 sector_t first_bad;
2262 int bad_sectors;
2263 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002264 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11002265 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
NeilBrown9ad1aef2011-12-23 10:17:55 +11002267 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2268 if (repl)
2269 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11002270 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11002271 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002273 if (bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002274 if (repl)
2275 md_error(mddev, rdev);
2276 else {
2277 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002278 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2279 set_bit(MD_RECOVERY_NEEDED,
2280 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002281 set_bit(R10BIO_WriteError, &r10_bio->state);
2282 }
2283 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10002284 r10_bio->devs[slot].addr,
2285 r10_bio->sectors,
2286 &first_bad, &bad_sectors))
2287 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07002288
NeilBrown9ad1aef2011-12-23 10:17:55 +11002289 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002290
2291 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294/*
2295 * Note: sync and recover and handled very differently for raid10
2296 * This code is for resync.
2297 * For resync, we read through virtual addresses and read all blocks.
2298 * If there is any error, we schedule a write. The lowest numbered
2299 * drive is authoritative.
2300 * However requests come for physical address, so we need to map.
2301 * For every physical address there are raid_disks/copies virtual addresses,
2302 * which is always are least one, but is not necessarly an integer.
2303 * This means that a physical address can span multiple chunks, so we may
2304 * have to submit multiple io requests for a single sync request.
2305 */
2306/*
2307 * We check if all blocks are in-sync and only write to blocks that
2308 * aren't in sync
2309 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002310static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
NeilBrowne879a872011-10-11 16:49:02 +11002312 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 int i, first;
2314 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002315 int vcnt;
Ming Leicdb76be2017-03-17 00:12:34 +08002316 struct page **tpages, **fpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 atomic_set(&r10_bio->remaining, 1);
2319
2320 /* find the first device with a block */
2321 for (i=0; i<conf->copies; i++)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002322 if (!r10_bio->devs[i].bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 break;
2324
2325 if (i == conf->copies)
2326 goto done;
2327
2328 first = i;
2329 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002330 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2331 fbio->bi_iter.bi_idx = 0;
Ming Leicdb76be2017-03-17 00:12:34 +08002332 fpages = get_resync_pages(fbio)->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
majianpengf4380a92012-04-12 16:04:47 +10002334 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002336 for (i=0 ; i < conf->copies ; i++) {
2337 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002338 struct md_rdev *rdev;
Ming Leif0250612017-03-17 00:12:33 +08002339 struct resync_pages *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002342
2343 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002345 if (i == first)
2346 continue;
Ming Leicdb76be2017-03-17 00:12:34 +08002347
2348 tpages = get_resync_pages(tbio)->pages;
NeilBrown8d3ca832016-11-18 16:16:12 +11002349 d = r10_bio->devs[i].devnum;
2350 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002351 if (!r10_bio->devs[i].bio->bi_status) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002352 /* We know that the bi_io_vec layout is the same for
2353 * both 'first' and 'i', so we just compare them.
2354 * All vec entries are PAGE_SIZE;
2355 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002356 int sectors = r10_bio->sectors;
2357 for (j = 0; j < vcnt; j++) {
2358 int len = PAGE_SIZE;
2359 if (sectors < (len / 512))
2360 len = sectors * 512;
Ming Leicdb76be2017-03-17 00:12:34 +08002361 if (memcmp(page_address(fpages[j]),
2362 page_address(tpages[j]),
NeilBrown7bb23c42013-07-16 16:50:47 +10002363 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002364 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002365 sectors -= len/512;
2366 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002367 if (j == vcnt)
2368 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002369 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002370 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2371 /* Don't fix anything. */
2372 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002373 } else if (test_bit(FailFast, &rdev->flags)) {
2374 /* Just give up on this device */
2375 md_error(rdev->mddev, rdev);
2376 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002377 }
NeilBrownf84ee362011-07-28 11:39:25 +10002378 /* Ok, we need to write this bio, either to correct an
2379 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 * First we need to fixup bv_offset, bv_len and
2381 * bi_vecs, as the read request might have corrupted these
2382 */
Ming Leif0250612017-03-17 00:12:33 +08002383 rp = get_resync_pages(tbio);
Kent Overstreet8be185f2012-09-06 14:14:43 -07002384 bio_reset(tbio);
2385
Ming Leifb0eb5d2017-07-14 16:14:43 +08002386 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2387
Ming Leif0250612017-03-17 00:12:33 +08002388 rp->raid_bio = r10_bio;
2389 tbio->bi_private = rp;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002390 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002392 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Kent Overstreetc31df252015-05-06 23:34:20 -07002394 bio_copy_data(tbio, fbio);
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2397 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002398 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
NeilBrown1919cbb2016-11-18 16:16:12 +11002400 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2401 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002402 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002403 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
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],
Mike Christie796a5cf2016-06-05 14:32:07 -05002477 REQ_OP_READ, 0, 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],
Mike Christie796a5cf2016-06-05 14:32:07 -05002485 REQ_OP_WRITE, 0, 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;
NeilBrown24afd802011-12-23 10:17:55 +11002530 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
NeilBrown5e570282011-07-28 11:39:25 +10002532 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2533 fix_recovery_read_error(r10_bio);
2534 end_sync_request(r10_bio);
2535 return;
2536 }
2537
Namhyung Kimc65060a2011-07-18 17:38:49 +10002538 /*
2539 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 * and submit the write request
2541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002543 wbio = r10_bio->devs[1].bio;
2544 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002545 /* Need to test wbio2->bi_end_io before we call
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002546 * submit_bio_noacct as if the former is NULL,
NeilBrown0eb25bb2013-07-24 15:37:42 +10002547 * the latter is free to free wbio2.
2548 */
2549 if (wbio2 && !wbio2->bi_end_io)
2550 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002551 if (wbio->bi_end_io) {
2552 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002553 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002554 submit_bio_noacct(wbio);
NeilBrown24afd802011-12-23 10:17:55 +11002555 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002556 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002557 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2558 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002559 bio_sectors(wbio2));
Christoph Hellwiged00aab2020-07-01 10:59:44 +02002560 submit_bio_noacct(wbio2);
NeilBrown24afd802011-12-23 10:17:55 +11002561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564/*
Robert Becker1e509152009-12-14 12:49:58 +11002565 * Used by fix_read_error() to decay the per rdev read_errors.
2566 * We halve the read error count for every hour that has elapsed
2567 * since the last recorded read error.
2568 *
2569 */
NeilBrownfd01b882011-10-11 16:47:53 +11002570static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002571{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002572 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002573 unsigned long hours_since_last;
2574 unsigned int read_errors = atomic_read(&rdev->read_errors);
2575
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002576 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002577
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002578 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002579 /* first time we've seen a read error */
2580 rdev->last_read_error = cur_time_mon;
2581 return;
2582 }
2583
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002584 hours_since_last = (long)(cur_time_mon -
2585 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002586
2587 rdev->last_read_error = cur_time_mon;
2588
2589 /*
2590 * if hours_since_last is > the number of bits in read_errors
2591 * just set read errors to 0. We do this to avoid
2592 * overflowing the shift of read_errors by hours_since_last.
2593 */
2594 if (hours_since_last >= 8 * sizeof(read_errors))
2595 atomic_set(&rdev->read_errors, 0);
2596 else
2597 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2598}
2599
NeilBrown3cb03002011-10-11 16:45:26 +11002600static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002601 int sectors, struct page *page, int rw)
2602{
2603 sector_t first_bad;
2604 int bad_sectors;
2605
2606 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2607 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2608 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002609 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002610 /* success */
2611 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002612 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002613 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002614 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2615 set_bit(MD_RECOVERY_NEEDED,
2616 &rdev->mddev->recovery);
2617 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002618 /* need to record an error - either for the block or the device */
2619 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2620 md_error(rdev->mddev, rdev);
2621 return 0;
2622}
2623
Robert Becker1e509152009-12-14 12:49:58 +11002624/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * This is a kernel thread which:
2626 *
2627 * 1. Retries failed read operations on working mirrors.
2628 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002629 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
2631
NeilBrowne879a872011-10-11 16:49:02 +11002632static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002633{
2634 int sect = 0; /* Offset from r10_bio->sector */
2635 int sectors = r10_bio->sectors;
Yufen Yu13db16d2018-04-23 17:37:30 +08002636 struct md_rdev *rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002637 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002638 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002639
NeilBrown7c4e06f2011-05-11 14:53:17 +10002640 /* still own a reference to this rdev, so it cannot
2641 * have been cleared recently.
2642 */
2643 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002644
NeilBrown7c4e06f2011-05-11 14:53:17 +10002645 if (test_bit(Faulty, &rdev->flags))
2646 /* drive has already been failed, just ignore any
2647 more fix_read_error() attempts */
2648 return;
2649
2650 check_decay_read_errors(mddev, rdev);
2651 atomic_inc(&rdev->read_errors);
2652 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2653 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002654 bdevname(rdev->bdev, b);
2655
NeilBrown08464e02016-11-02 14:16:50 +11002656 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2657 mdname(mddev), b,
2658 atomic_read(&rdev->read_errors), max_read_errors);
2659 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2660 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002661 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002662 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002663 return;
Robert Becker1e509152009-12-14 12:49:58 +11002664 }
Robert Becker1e509152009-12-14 12:49:58 +11002665
NeilBrown6814d532006-10-03 01:15:45 -07002666 while(sectors) {
2667 int s = sectors;
2668 int sl = r10_bio->read_slot;
2669 int success = 0;
2670 int start;
2671
2672 if (s > (PAGE_SIZE>>9))
2673 s = PAGE_SIZE >> 9;
2674
2675 rcu_read_lock();
2676 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002677 sector_t first_bad;
2678 int bad_sectors;
2679
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002680 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002681 rdev = rcu_dereference(conf->mirrors[d].rdev);
2682 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002683 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002684 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002685 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2686 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002687 atomic_inc(&rdev->nr_pending);
2688 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002689 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002690 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002691 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002692 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002693 conf->tmppage,
2694 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002695 rdev_dec_pending(rdev, mddev);
2696 rcu_read_lock();
2697 if (success)
2698 break;
2699 }
2700 sl++;
2701 if (sl == conf->copies)
2702 sl = 0;
2703 } while (!success && sl != r10_bio->read_slot);
2704 rcu_read_unlock();
2705
2706 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002707 /* Cannot read from anywhere, just mark the block
2708 * as bad on the first device to discourage future
2709 * reads.
2710 */
NeilBrown6814d532006-10-03 01:15:45 -07002711 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002712 rdev = conf->mirrors[dn].rdev;
2713
2714 if (!rdev_set_badblocks(
2715 rdev,
2716 r10_bio->devs[r10_bio->read_slot].addr
2717 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002718 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002719 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002720 r10_bio->devs[r10_bio->read_slot].bio
2721 = IO_BLOCKED;
2722 }
NeilBrown6814d532006-10-03 01:15:45 -07002723 break;
2724 }
2725
2726 start = sl;
2727 /* write it back and re-read */
2728 rcu_read_lock();
2729 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002730 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002731
NeilBrown6814d532006-10-03 01:15:45 -07002732 if (sl==0)
2733 sl = conf->copies;
2734 sl--;
2735 d = r10_bio->devs[sl].devnum;
2736 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002737 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002738 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002739 !test_bit(In_sync, &rdev->flags))
2740 continue;
2741
2742 atomic_inc(&rdev->nr_pending);
2743 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002744 if (r10_sync_page_io(rdev,
2745 r10_bio->devs[sl].addr +
2746 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002747 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002748 == 0) {
2749 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002750 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2751 mdname(mddev), s,
2752 (unsigned long long)(
2753 sect +
2754 choose_data_offset(r10_bio,
2755 rdev)),
2756 bdevname(rdev->bdev, b));
2757 pr_notice("md/raid10:%s: %s: failing drive\n",
2758 mdname(mddev),
2759 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002760 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002761 rdev_dec_pending(rdev, mddev);
2762 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002763 }
2764 sl = start;
2765 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002766 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002767
NeilBrown6814d532006-10-03 01:15:45 -07002768 if (sl==0)
2769 sl = conf->copies;
2770 sl--;
2771 d = r10_bio->devs[sl].devnum;
2772 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002773 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002774 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002775 !test_bit(In_sync, &rdev->flags))
2776 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002777
NeilBrown1294b9c2011-07-28 11:39:23 +10002778 atomic_inc(&rdev->nr_pending);
2779 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002780 switch (r10_sync_page_io(rdev,
2781 r10_bio->devs[sl].addr +
2782 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002783 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002784 READ)) {
2785 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002786 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002787 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002788 mdname(mddev), s,
2789 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002790 sect +
2791 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002792 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002793 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002794 mdname(mddev),
2795 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002796 break;
2797 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002798 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002799 mdname(mddev), s,
2800 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002801 sect +
2802 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002803 bdevname(rdev->bdev, b));
2804 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002805 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002806
2807 rdev_dec_pending(rdev, mddev);
2808 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002809 }
2810 rcu_read_unlock();
2811
2812 sectors -= s;
2813 sect += s;
2814 }
2815}
2816
NeilBrown9f2c9d12011-10-11 16:48:43 +11002817static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002818{
2819 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002820 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002821 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002822 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002823 /* bio has the data to be written to slot 'i' where
2824 * we just recently had a write error.
2825 * We repeatedly clone the bio and trim down to one block,
2826 * then try the write. Where the write fails we record
2827 * a bad block.
2828 * It is conceivable that the bio doesn't exactly align with
2829 * blocks. We must handle this.
2830 *
2831 * We currently own a reference to the rdev.
2832 */
2833
2834 int block_sectors;
2835 sector_t sector;
2836 int sectors;
2837 int sect_to_write = r10_bio->sectors;
2838 int ok = 1;
2839
2840 if (rdev->badblocks.shift < 0)
2841 return 0;
2842
NeilBrownf04ebb02015-02-16 14:51:54 +11002843 block_sectors = roundup(1 << rdev->badblocks.shift,
2844 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002845 sector = r10_bio->sector;
2846 sectors = ((r10_bio->sector + block_sectors)
2847 & ~(sector_t)(block_sectors - 1))
2848 - sector;
2849
2850 while (sect_to_write) {
2851 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002852 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002853 if (sectors > sect_to_write)
2854 sectors = sect_to_write;
2855 /* Write at 'sector' for 'sectors' */
Kent Overstreetafeee512018-05-20 18:25:52 -04002856 wbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002857 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002858 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2859 wbio->bi_iter.bi_sector = wsector +
2860 choose_data_offset(r10_bio, rdev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002861 bio_set_dev(wbio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05002862 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002863
2864 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002865 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002866 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002867 sectors, 0)
2868 && ok;
2869
2870 bio_put(wbio);
2871 sect_to_write -= sectors;
2872 sector += sectors;
2873 sectors = block_sectors;
2874 }
2875 return ok;
2876}
2877
NeilBrown9f2c9d12011-10-11 16:48:43 +11002878static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002879{
2880 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002881 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002882 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002883 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002884
2885 /* we got a read error. Maybe the drive is bad. Maybe just
2886 * the block and we can fix it.
2887 * We freeze all other IO, and try reading the block from
2888 * other devices. When we find one, we re-write
2889 * and check it that fixes the read error.
2890 * This is all done synchronously while the array is
2891 * frozen.
2892 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002893 bio = r10_bio->devs[slot].bio;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002894 bio_put(bio);
2895 r10_bio->devs[slot].bio = NULL;
2896
NeilBrown8d3ca832016-11-18 16:16:12 +11002897 if (mddev->ro)
2898 r10_bio->devs[slot].bio = IO_BLOCKED;
2899 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002900 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002901 fix_read_error(conf, mddev, r10_bio);
2902 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002903 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002904 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002905
NeilBrownabbf0982011-12-23 10:17:54 +11002906 rdev_dec_pending(rdev, mddev);
NeilBrown545250f2017-04-05 14:05:51 +10002907 allow_barrier(conf);
2908 r10_bio->state = 0;
2909 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002910}
2911
NeilBrowne879a872011-10-11 16:49:02 +11002912static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002913{
2914 /* Some sort of write request has finished and it
2915 * succeeded in writing where we thought there was a
2916 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002917 * Or possibly if failed and we need to record
2918 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002919 */
2920 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002921 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002922
2923 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2924 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002925 for (m = 0; m < conf->copies; m++) {
2926 int dev = r10_bio->devs[m].devnum;
2927 rdev = conf->mirrors[dev].rdev;
Yufen Yu01a69ca2018-02-06 17:39:15 +08002928 if (r10_bio->devs[m].bio == NULL ||
2929 r10_bio->devs[m].bio->bi_end_io == NULL)
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002930 continue;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002931 if (!r10_bio->devs[m].bio->bi_status) {
NeilBrown749c55e2011-07-28 11:39:24 +10002932 rdev_clear_badblocks(
2933 rdev,
2934 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002935 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002936 } else {
2937 if (!rdev_set_badblocks(
2938 rdev,
2939 r10_bio->devs[m].addr,
2940 r10_bio->sectors, 0))
2941 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002942 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002943 rdev = conf->mirrors[dev].replacement;
Yufen Yu01a69ca2018-02-06 17:39:15 +08002944 if (r10_bio->devs[m].repl_bio == NULL ||
2945 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002946 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002947
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002948 if (!r10_bio->devs[m].repl_bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002949 rdev_clear_badblocks(
2950 rdev,
2951 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002952 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002953 } else {
2954 if (!rdev_set_badblocks(
2955 rdev,
2956 r10_bio->devs[m].addr,
2957 r10_bio->sectors, 0))
2958 md_error(conf->mddev, rdev);
2959 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002960 }
NeilBrown749c55e2011-07-28 11:39:24 +10002961 put_buf(r10_bio);
2962 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002963 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002964 for (m = 0; m < conf->copies; m++) {
2965 int dev = r10_bio->devs[m].devnum;
2966 struct bio *bio = r10_bio->devs[m].bio;
2967 rdev = conf->mirrors[dev].rdev;
2968 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002969 rdev_clear_badblocks(
2970 rdev,
2971 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002972 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002973 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002974 } else if (bio != NULL && bio->bi_status) {
NeilBrown95af5872015-08-14 11:26:17 +10002975 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002976 if (!narrow_write_error(r10_bio, m)) {
2977 md_error(conf->mddev, rdev);
2978 set_bit(R10BIO_Degraded,
2979 &r10_bio->state);
2980 }
2981 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002982 }
NeilBrown475b0322011-12-23 10:17:55 +11002983 bio = r10_bio->devs[m].repl_bio;
2984 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002985 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002986 rdev_clear_badblocks(
2987 rdev,
2988 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002989 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002990 rdev_dec_pending(rdev, conf->mddev);
2991 }
NeilBrownbd870a12011-07-28 11:39:24 +10002992 }
NeilBrown95af5872015-08-14 11:26:17 +10002993 if (fail) {
2994 spin_lock_irq(&conf->device_lock);
2995 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002996 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002997 spin_unlock_irq(&conf->device_lock);
Guoqing Jiangcf25ae72017-04-17 17:11:05 +08002998 /*
2999 * In case freeze_array() is waiting for condition
3000 * nr_pending == nr_queued + extra to be true.
3001 */
3002 wake_up(&conf->wait_barrier);
NeilBrown95af5872015-08-14 11:26:17 +10003003 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11003004 } else {
3005 if (test_bit(R10BIO_WriteError,
3006 &r10_bio->state))
3007 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10003008 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11003009 }
NeilBrown749c55e2011-07-28 11:39:24 +10003010 }
3011}
3012
Shaohua Li4ed87312012-10-11 13:34:00 +11003013static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Shaohua Li4ed87312012-10-11 13:34:00 +11003015 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003016 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11003018 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003020 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
NeilBrown95af5872015-08-14 11:26:17 +10003024 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08003025 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10003026 LIST_HEAD(tmp);
3027 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08003028 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07003029 while (!list_empty(&conf->bio_end_io_list)) {
3030 list_move(conf->bio_end_io_list.prev, &tmp);
3031 conf->nr_queued--;
3032 }
NeilBrown95af5872015-08-14 11:26:17 +10003033 }
3034 spin_unlock_irqrestore(&conf->device_lock, flags);
3035 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04003036 r10_bio = list_first_entry(&tmp, struct r10bio,
3037 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003038 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11003039 if (mddev->degraded)
3040 set_bit(R10BIO_Degraded, &r10_bio->state);
3041
3042 if (test_bit(R10BIO_WriteError,
3043 &r10_bio->state))
3044 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10003045 raid_end_bio_io(r10_bio);
3046 }
3047 }
3048
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003049 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08003051
NeilBrown0021b7b2012-07-31 09:08:14 +02003052 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08003053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08003055 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003056 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08003058 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11003059 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08003061 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 spin_unlock_irqrestore(&conf->device_lock, flags);
3063
3064 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10003065 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10003066 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3067 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10003068 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003069 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3070 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10003071 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01003073 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10003075 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10003076 handle_read_error(mddev, r10_bio);
NeilBrownfc9977d2017-04-05 14:05:51 +10003077 else
3078 WARN_ON_ONCE(1);
NeilBrown4443ae12006-01-06 00:20:28 -08003079
NeilBrown1d9d5242009-10-16 15:55:32 +11003080 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08003081 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10003082 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10003084 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
NeilBrowne879a872011-10-11 16:49:02 +11003087static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Kent Overstreetafeee512018-05-20 18:25:52 -04003089 int ret, buffs, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Kent Overstreetafeee512018-05-20 18:25:52 -04003092 BUG_ON(mempool_initialized(&conf->r10buf_pool));
NeilBrown69335ef2011-12-23 10:17:54 +11003093 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003094 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11003095 if (conf->mirrors[i].replacement)
3096 conf->have_replacement = 1;
Kent Overstreetafeee512018-05-20 18:25:52 -04003097 ret = mempool_init(&conf->r10buf_pool, buffs,
3098 r10buf_pool_alloc, r10buf_pool_free, conf);
3099 if (ret)
3100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 conf->next_resync = 0;
3102 return 0;
3103}
3104
Shaohua Li208410b2017-08-24 17:50:40 -07003105static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3106{
Kent Overstreetafeee512018-05-20 18:25:52 -04003107 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
Shaohua Li208410b2017-08-24 17:50:40 -07003108 struct rsync_pages *rp;
3109 struct bio *bio;
3110 int nalloc;
3111 int i;
3112
3113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3114 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3115 nalloc = conf->copies; /* resync */
3116 else
3117 nalloc = 2; /* recovery */
3118
3119 for (i = 0; i < nalloc; i++) {
3120 bio = r10bio->devs[i].bio;
3121 rp = bio->bi_private;
3122 bio_reset(bio);
3123 bio->bi_private = rp;
3124 bio = r10bio->devs[i].repl_bio;
3125 if (bio) {
3126 rp = bio->bi_private;
3127 bio_reset(bio);
3128 bio->bi_private = rp;
3129 }
3130 }
3131 return r10bio;
3132}
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134/*
Guoqing Jiang8db87912017-10-24 15:11:52 +08003135 * Set cluster_sync_high since we need other nodes to add the
3136 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3137 */
3138static void raid10_set_cluster_sync_high(struct r10conf *conf)
3139{
3140 sector_t window_size;
3141 int extra_chunk, chunks;
3142
3143 /*
3144 * First, here we define "stripe" as a unit which across
3145 * all member devices one time, so we get chunks by use
3146 * raid_disks / near_copies. Otherwise, if near_copies is
3147 * close to raid_disks, then resync window could increases
3148 * linearly with the increase of raid_disks, which means
3149 * we will suspend a really large IO window while it is not
3150 * necessary. If raid_disks is not divisible by near_copies,
3151 * an extra chunk is needed to ensure the whole "stripe" is
3152 * covered.
3153 */
3154
3155 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3156 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3157 extra_chunk = 0;
3158 else
3159 extra_chunk = 1;
3160 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3161
3162 /*
3163 * At least use a 32M window to align with raid1's resync window
3164 */
3165 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3166 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3167
3168 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3169}
3170
3171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 * perform a "sync" on one "block"
3173 *
3174 * We need to make sure that no normal I/O request - particularly write
3175 * requests - conflict with active sync requests.
3176 *
3177 * This is achieved by tracking pending requests and a 'barrier' concept
3178 * that can be installed to exclude normal IO requests.
3179 *
3180 * Resync and recovery are handled very differently.
3181 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3182 *
3183 * For resync, we iterate over virtual addresses, read all copies,
3184 * and update if there are differences. If only one copy is live,
3185 * skip it.
3186 * For recovery, we iterate over physical addresses, read a good
3187 * value for each non-in_sync drive, and over-write.
3188 *
3189 * So, for recovery we may have several outstanding complex requests for a
3190 * given address, one for each out-of-sync device. We model this by allocating
3191 * a number of r10_bio structures, one for each out-of-sync device.
3192 * As we setup these structures, we collect all bio's together into a list
3193 * which we then process collectively to add pages, and then process again
Christoph Hellwiged00aab2020-07-01 10:59:44 +02003194 * to pass to submit_bio_noacct.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 *
3196 * The r10_bio structures are linked using a borrowed master_bio pointer.
3197 * This link is counted in ->remaining. When the r10_bio that points to NULL
3198 * has its remaining count decremented to 0, the whole complex operation
3199 * is complete.
3200 *
3201 */
3202
Shaohua Li849674e2016-01-20 13:52:20 -08003203static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11003204 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
NeilBrowne879a872011-10-11 16:49:02 +11003206 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003207 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 struct bio *biolist = NULL, *bio;
3209 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08003211 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11003212 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 sector_t sectors_skipped = 0;
3214 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003215 sector_t chunk_mask = conf->geo.chunk_mask;
Ming Lei022e5102017-07-14 16:14:42 +08003216 int page_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Kent Overstreetafeee512018-05-20 18:25:52 -04003218 if (!mempool_initialized(&conf->r10buf_pool))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07003220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003222 /*
3223 * Allow skipping a full rebuild for incremental assembly
3224 * of a clean array, like RAID1 does.
3225 */
3226 if (mddev->bitmap == NULL &&
3227 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10003228 mddev->reshape_position == MaxSector &&
3229 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003230 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10003231 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003232 conf->fullsync == 0) {
3233 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10003234 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10003235 }
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11003238 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003239 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3240 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 max_sector = mddev->resync_max_sectors;
3242 if (sector_nr >= max_sector) {
Guoqing Jiang8db87912017-10-24 15:11:52 +08003243 conf->cluster_sync_low = 0;
3244 conf->cluster_sync_high = 0;
3245
NeilBrown6cce3b22006-01-06 00:20:16 -08003246 /* If we aborted, we need to abort the
3247 * sync on the 'current' bitmap chucks (there can
3248 * be several when recovering multiple devices).
3249 * as we may have started syncing it but not finished.
3250 * We can find the current address in
3251 * mddev->curr_resync, but for recovery,
3252 * we need to convert that to several
3253 * virtual addresses.
3254 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10003255 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3256 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10003257 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003258 return 0;
3259 }
3260
NeilBrown6cce3b22006-01-06 00:20:16 -08003261 if (mddev->curr_resync < max_sector) { /* aborted */
3262 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003263 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3264 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003265 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003266 sector_t sect =
3267 raid10_find_virt(conf, mddev->curr_resync, i);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003268 md_bitmap_end_sync(mddev->bitmap, sect,
3269 &sync_blocks, 1);
NeilBrown6cce3b22006-01-06 00:20:16 -08003270 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11003271 } else {
3272 /* completed sync */
3273 if ((!mddev->bitmap || conf->fullsync)
3274 && conf->have_replacement
3275 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3276 /* Completed a full sync so the replacements
3277 * are now fully recovered.
3278 */
NeilBrownf90145f2016-06-02 16:19:52 +10003279 rcu_read_lock();
3280 for (i = 0; i < conf->geo.raid_disks; i++) {
3281 struct md_rdev *rdev =
3282 rcu_dereference(conf->mirrors[i].replacement);
3283 if (rdev)
3284 rdev->recovery_offset = MaxSector;
3285 }
3286 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003287 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003288 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003289 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003290 md_bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07003292 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return sectors_skipped;
3294 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003295
3296 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3297 return reshape_request(mddev, sector_nr, skipped);
3298
NeilBrown5cf00fc2012-05-21 09:28:20 +10003299 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 /* if there has been nothing to do on any drive,
3301 * then there is nothing to do at all..
3302 */
NeilBrown57afd892005-06-21 17:17:13 -07003303 *skipped = 1;
3304 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306
NeilBrownc6207272008-02-06 01:39:52 -08003307 if (max_sector > mddev->resync_max)
3308 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 /* make sure whole request will fit in a chunk - if chunks
3311 * are meaningful
3312 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003313 if (conf->geo.near_copies < conf->geo.raid_disks &&
3314 max_sector > (sector_nr | chunk_mask))
3315 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003317 /*
3318 * If there is non-resync activity waiting for a turn, then let it
3319 * though before starting on this new sync request.
3320 */
3321 if (conf->nr_waiting)
3322 schedule_timeout_uninterruptible(1);
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /* Again, very different code for resync and recovery.
3325 * Both must result in an r10bio with a list of bios that
Christoph Hellwig309dca302021-01-24 11:02:34 +01003326 * have bi_end_io, bi_sector, bi_bdev set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 * and bi_private set to the r10bio.
3328 * For recovery, we may actually create several r10bios
3329 * with 2 bios in each, that correspond to the bios in the main one.
3330 * In this case, the subordinate r10bios link back through a
3331 * borrowed master_bio pointer, and the counter in the master
3332 * includes a ref from each subordinate.
3333 */
3334 /* First, we decide what to do and set ->bi_end_io
3335 * To end_sync_read if we want to read, and
3336 * end_sync_write if we will want to write.
3337 */
3338
NeilBrown6cce3b22006-01-06 00:20:16 -08003339 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3341 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003342 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 r10_bio = NULL;
3344
NeilBrown5cf00fc2012-05-21 09:28:20 +10003345 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003346 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003347 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003348 sector_t sect;
3349 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003350 int any_working;
Alex Wuee37d732018-09-21 16:05:03 +08003351 int need_recover = 0;
3352 int need_replace = 0;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003353 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003354 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003355
NeilBrownf90145f2016-06-02 16:19:52 +10003356 rcu_read_lock();
3357 mrdev = rcu_dereference(mirror->rdev);
3358 mreplace = rcu_dereference(mirror->replacement);
3359
Alex Wuee37d732018-09-21 16:05:03 +08003360 if (mrdev != NULL &&
3361 !test_bit(Faulty, &mrdev->flags) &&
3362 !test_bit(In_sync, &mrdev->flags))
3363 need_recover = 1;
3364 if (mreplace != NULL &&
3365 !test_bit(Faulty, &mreplace->flags))
3366 need_replace = 1;
3367
3368 if (!need_recover && !need_replace) {
NeilBrownf90145f2016-06-02 16:19:52 +10003369 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003370 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003371 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003372
3373 still_degraded = 0;
3374 /* want to reconstruct this device */
3375 rb2 = r10_bio;
3376 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003377 if (sect >= mddev->resync_max_sectors) {
3378 /* last stripe is not complete - don't
3379 * try to recover this sector.
3380 */
NeilBrownf90145f2016-06-02 16:19:52 +10003381 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003382 continue;
3383 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003384 if (mreplace && test_bit(Faulty, &mreplace->flags))
3385 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003386 /* Unless we are doing a full sync, or a replacement
3387 * we only need to recover the block if it is set in
3388 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003389 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003390 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3391 &sync_blocks, 1);
NeilBrownab9d47e2011-05-11 14:54:41 +10003392 if (sync_blocks < max_sync)
3393 max_sync = sync_blocks;
3394 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003395 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003396 !conf->fullsync) {
3397 /* yep, skip the sync_blocks here, but don't assume
3398 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003399 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003400 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003401 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003402 continue;
3403 }
NeilBrownf90145f2016-06-02 16:19:52 +10003404 atomic_inc(&mrdev->nr_pending);
3405 if (mreplace)
3406 atomic_inc(&mreplace->nr_pending);
3407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Shaohua Li208410b2017-08-24 17:50:40 -07003409 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003410 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003411 raise_barrier(conf, rb2 != NULL);
3412 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
NeilBrownab9d47e2011-05-11 14:54:41 +10003414 r10_bio->master_bio = (struct bio*)rb2;
3415 if (rb2)
3416 atomic_inc(&rb2->remaining);
3417 r10_bio->mddev = mddev;
3418 set_bit(R10BIO_IsRecover, &r10_bio->state);
3419 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003420
NeilBrownab9d47e2011-05-11 14:54:41 +10003421 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003422
NeilBrownab9d47e2011-05-11 14:54:41 +10003423 /* Need to check if the array will still be
3424 * degraded
3425 */
NeilBrownf90145f2016-06-02 16:19:52 +10003426 rcu_read_lock();
3427 for (j = 0; j < conf->geo.raid_disks; j++) {
3428 struct md_rdev *rdev = rcu_dereference(
3429 conf->mirrors[j].rdev);
3430 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003431 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
NeilBrownf90145f2016-06-02 16:19:52 +10003434 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003435
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003436 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3437 &sync_blocks, still_degraded);
NeilBrownab9d47e2011-05-11 14:54:41 +10003438
NeilBrowne875ece2011-07-28 11:39:24 +10003439 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003440 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003441 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003442 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003443 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003444 struct md_rdev *rdev =
3445 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003446 sector_t sector, first_bad;
3447 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003448 if (!rdev ||
3449 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003450 continue;
3451 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003452 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003453 sector = r10_bio->devs[j].addr;
3454
3455 if (is_badblock(rdev, sector, max_sync,
3456 &first_bad, &bad_sectors)) {
3457 if (first_bad > sector)
3458 max_sync = first_bad - sector;
3459 else {
3460 bad_sectors -= (sector
3461 - first_bad);
3462 if (max_sync > bad_sectors)
3463 max_sync = bad_sectors;
3464 continue;
3465 }
3466 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003467 bio = r10_bio->devs[0].bio;
3468 bio->bi_next = biolist;
3469 biolist = bio;
NeilBrownab9d47e2011-05-11 14:54:41 +10003470 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003471 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003472 if (test_bit(FailFast, &rdev->flags))
3473 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003474 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003475 bio->bi_iter.bi_sector = from_addr +
3476 rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003477 bio_set_dev(bio, rdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003478 atomic_inc(&rdev->nr_pending);
3479 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003480
3481 for (k=0; k<conf->copies; k++)
3482 if (r10_bio->devs[k].devnum == i)
3483 break;
3484 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003485 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003486 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003487 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003488 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003489 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003490
Alex Wuee37d732018-09-21 16:05:03 +08003491 if (need_recover) {
NeilBrown24afd802011-12-23 10:17:55 +11003492 bio = r10_bio->devs[1].bio;
3493 bio->bi_next = biolist;
3494 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003495 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003496 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003497 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003498 + mrdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003499 bio_set_dev(bio, mrdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003500 atomic_inc(&r10_bio->remaining);
3501 } else
3502 r10_bio->devs[1].bio->bi_end_io = NULL;
3503
3504 /* and maybe write to replacement */
3505 bio = r10_bio->devs[1].repl_bio;
3506 if (bio)
3507 bio->bi_end_io = NULL;
Alex Wuee37d732018-09-21 16:05:03 +08003508 /* Note: if need_replace, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003509 * cannot be NULL as r10buf_pool_alloc will
3510 * have allocated it.
NeilBrown24afd802011-12-23 10:17:55 +11003511 */
Alex Wuee37d732018-09-21 16:05:03 +08003512 if (!need_replace)
NeilBrown24afd802011-12-23 10:17:55 +11003513 break;
3514 bio->bi_next = biolist;
3515 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003516 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003517 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003518 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003519 mreplace->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003520 bio_set_dev(bio, mreplace->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003521 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
NeilBrownf90145f2016-06-02 16:19:52 +10003524 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003525 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003526 /* Cannot recover, so abort the recovery or
3527 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003528 if (any_working) {
3529 /* problem is that there are bad blocks
3530 * on other device(s)
3531 */
3532 int k;
3533 for (k = 0; k < conf->copies; k++)
3534 if (r10_bio->devs[k].devnum == i)
3535 break;
NeilBrown24afd802011-12-23 10:17:55 +11003536 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003537 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003538 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003539 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003540 r10_bio->devs[k].addr,
3541 max_sync, 0))
3542 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003543 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003544 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003545 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003546 r10_bio->devs[k].addr,
3547 max_sync, 0))
3548 any_working = 0;
3549 }
3550 if (!any_working) {
3551 if (!test_and_set_bit(MD_RECOVERY_INTR,
3552 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003553 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003554 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003555 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003556 = mddev->recovery_disabled;
3557 }
NeilBrowne8b84912014-01-06 10:35:34 +11003558 put_buf(r10_bio);
3559 if (rb2)
3560 atomic_dec(&rb2->remaining);
3561 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003562 rdev_dec_pending(mrdev, mddev);
3563 if (mreplace)
3564 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003565 break;
3566 }
NeilBrownf90145f2016-06-02 16:19:52 +10003567 rdev_dec_pending(mrdev, mddev);
3568 if (mreplace)
3569 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003570 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3571 /* Only want this if there is elsewhere to
3572 * read from. 'j' is currently the first
3573 * readable copy.
3574 */
3575 int targets = 1;
3576 for (; j < conf->copies; j++) {
3577 int d = r10_bio->devs[j].devnum;
3578 if (conf->mirrors[d].rdev &&
3579 test_bit(In_sync,
3580 &conf->mirrors[d].rdev->flags))
3581 targets++;
3582 }
3583 if (targets == 1)
3584 r10_bio->devs[0].bio->bi_opf
3585 &= ~MD_FAILFAST;
3586 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (biolist == NULL) {
3589 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003590 struct r10bio *rb2 = r10_bio;
3591 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 rb2->master_bio = NULL;
3593 put_buf(rb2);
3594 }
3595 goto giveup;
3596 }
3597 } else {
3598 /* resync. Schedule a read for every block at this virt offset */
3599 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003600
Guoqing Jiang8db87912017-10-24 15:11:52 +08003601 /*
3602 * Since curr_resync_completed could probably not update in
3603 * time, and we will set cluster_sync_low based on it.
3604 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3605 * safety reason, which ensures curr_resync_completed is
3606 * updated in bitmap_cond_end_sync.
3607 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003608 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3609 mddev_is_clustered(mddev) &&
3610 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown78200d42009-02-25 13:18:47 +11003611
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003612 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3613 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003614 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3615 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003616 /* We can skip this block */
3617 *skipped = 1;
3618 return sync_blocks + sectors_skipped;
3619 }
3620 if (sync_blocks < max_sync)
3621 max_sync = sync_blocks;
Shaohua Li208410b2017-08-24 17:50:40 -07003622 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003623 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 r10_bio->mddev = mddev;
3626 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003627 raise_barrier(conf, 0);
3628 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 r10_bio->master_bio = NULL;
3631 r10_bio->sector = sector_nr;
3632 set_bit(R10BIO_IsSync, &r10_bio->state);
3633 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003634 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
NeilBrown5cf00fc2012-05-21 09:28:20 +10003636 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003638 sector_t first_bad, sector;
3639 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003640 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003641
NeilBrown9ad1aef2011-12-23 10:17:55 +11003642 if (r10_bio->devs[i].repl_bio)
3643 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 bio = r10_bio->devs[i].bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003646 bio->bi_status = BLK_STS_IOERR;
NeilBrownf90145f2016-06-02 16:19:52 +10003647 rcu_read_lock();
3648 rdev = rcu_dereference(conf->mirrors[d].rdev);
3649 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3650 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003652 }
NeilBrown40c356c2011-07-28 11:39:24 +10003653 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003654 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003655 &first_bad, &bad_sectors)) {
3656 if (first_bad > sector)
3657 max_sync = first_bad - sector;
3658 else {
3659 bad_sectors -= (sector - first_bad);
3660 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003661 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003662 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003663 continue;
3664 }
3665 }
NeilBrownf90145f2016-06-02 16:19:52 +10003666 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 atomic_inc(&r10_bio->remaining);
3668 bio->bi_next = biolist;
3669 biolist = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003671 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003672 if (test_bit(FailFast, &rdev->flags))
NeilBrown8d3ca832016-11-18 16:16:12 +11003673 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003674 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003675 bio_set_dev(bio, rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003677
NeilBrownf90145f2016-06-02 16:19:52 +10003678 rdev = rcu_dereference(conf->mirrors[d].replacement);
3679 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3680 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003681 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003682 }
3683 atomic_inc(&rdev->nr_pending);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003684
3685 /* Need to set up for writing to the replacement */
3686 bio = r10_bio->devs[i].repl_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003687 bio->bi_status = BLK_STS_IOERR;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003688
3689 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003690 bio->bi_next = biolist;
3691 biolist = bio;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003692 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003693 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003694 if (test_bit(FailFast, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +11003695 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003696 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003697 bio_set_dev(bio, rdev->bdev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003698 count++;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003699 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
3701
3702 if (count < 2) {
3703 for (i=0; i<conf->copies; i++) {
3704 int d = r10_bio->devs[i].devnum;
3705 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003706 rdev_dec_pending(conf->mirrors[d].rdev,
3707 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003708 if (r10_bio->devs[i].repl_bio &&
3709 r10_bio->devs[i].repl_bio->bi_end_io)
3710 rdev_dec_pending(
3711 conf->mirrors[d].replacement,
3712 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
3714 put_buf(r10_bio);
3715 biolist = NULL;
3716 goto giveup;
3717 }
3718 }
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003721 if (sector_nr + max_sync < max_sector)
3722 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 do {
3724 struct page *page;
3725 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 if (sector_nr + (len>>9) > max_sector)
3727 len = (max_sector - sector_nr) << 9;
3728 if (len == 0)
3729 break;
3730 for (bio= biolist ; bio ; bio=bio->bi_next) {
Ming Leif0250612017-03-17 00:12:33 +08003731 struct resync_pages *rp = get_resync_pages(bio);
Ming Lei022e5102017-07-14 16:14:42 +08003732 page = resync_fetch_page(rp, page_idx);
Ming Leic85ba142017-03-17 00:12:22 +08003733 /*
3734 * won't fail because the vec table is big enough
3735 * to hold all these pages
3736 */
3737 bio_add_page(bio, page, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
3739 nr_sectors += len>>9;
3740 sector_nr += len>>9;
Ming Lei022e5102017-07-14 16:14:42 +08003741 } while (++page_idx < RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 r10_bio->sectors = nr_sectors;
3743
Guoqing Jiang8db87912017-10-24 15:11:52 +08003744 if (mddev_is_clustered(mddev) &&
3745 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3746 /* It is resync not recovery */
3747 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3748 conf->cluster_sync_low = mddev->curr_resync_completed;
3749 raid10_set_cluster_sync_high(conf);
3750 /* Send resync message */
3751 md_cluster_ops->resync_info_update(mddev,
3752 conf->cluster_sync_low,
3753 conf->cluster_sync_high);
3754 }
3755 } else if (mddev_is_clustered(mddev)) {
3756 /* This is recovery not resync */
3757 sector_t sect_va1, sect_va2;
3758 bool broadcast_msg = false;
3759
3760 for (i = 0; i < conf->geo.raid_disks; i++) {
3761 /*
3762 * sector_nr is a device address for recovery, so we
3763 * need translate it to array address before compare
3764 * with cluster_sync_high.
3765 */
3766 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3767
3768 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3769 broadcast_msg = true;
3770 /*
3771 * curr_resync_completed is similar as
3772 * sector_nr, so make the translation too.
3773 */
3774 sect_va2 = raid10_find_virt(conf,
3775 mddev->curr_resync_completed, i);
3776
3777 if (conf->cluster_sync_low == 0 ||
3778 conf->cluster_sync_low > sect_va2)
3779 conf->cluster_sync_low = sect_va2;
3780 }
3781 }
3782 if (broadcast_msg) {
3783 raid10_set_cluster_sync_high(conf);
3784 md_cluster_ops->resync_info_update(mddev,
3785 conf->cluster_sync_low,
3786 conf->cluster_sync_high);
3787 }
3788 }
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 while (biolist) {
3791 bio = biolist;
3792 biolist = biolist->bi_next;
3793
3794 bio->bi_next = NULL;
Ming Leif0250612017-03-17 00:12:33 +08003795 r10_bio = get_resync_r10bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 r10_bio->sectors = nr_sectors;
3797
3798 if (bio->bi_end_io == end_sync_read) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02003799 md_sync_acct_bio(bio, nr_sectors);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003800 bio->bi_status = 0;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02003801 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803 }
3804
NeilBrown57afd892005-06-21 17:17:13 -07003805 if (sectors_skipped)
3806 /* pretend they weren't skipped, it makes
3807 * no important difference in this case
3808 */
3809 md_done_sync(mddev, sectors_skipped, 1);
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return sectors_skipped + nr_sectors;
3812 giveup:
3813 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003814 * drives must be failed or in resync, all drives
3815 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 */
NeilBrown09b40682009-02-25 13:18:47 +11003817 if (sector_nr + max_sync < max_sector)
3818 max_sector = sector_nr + max_sync;
3819
3820 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 chunks_skipped ++;
3822 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Dan Williams80c3a6c2009-03-17 18:10:40 -07003826static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003827raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003828{
3829 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003830 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003831
3832 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003833 raid_disks = min(conf->geo.raid_disks,
3834 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003835 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003836 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003837
NeilBrown5cf00fc2012-05-21 09:28:20 +10003838 size = sectors >> conf->geo.chunk_shift;
3839 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003840 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003841 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003842
NeilBrown5cf00fc2012-05-21 09:28:20 +10003843 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003844}
3845
NeilBrown6508fdb2012-05-17 10:08:45 +10003846static void calc_sectors(struct r10conf *conf, sector_t size)
3847{
3848 /* Calculate the number of sectors-per-device that will
3849 * actually be used, and set conf->dev_sectors and
3850 * conf->stride
3851 */
3852
NeilBrown5cf00fc2012-05-21 09:28:20 +10003853 size = size >> conf->geo.chunk_shift;
3854 sector_div(size, conf->geo.far_copies);
3855 size = size * conf->geo.raid_disks;
3856 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003857 /* 'size' is now the number of chunks in the array */
3858 /* calculate "used chunks per device" */
3859 size = size * conf->copies;
3860
3861 /* We need to round up when dividing by raid_disks to
3862 * get the stride size.
3863 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003864 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003865
NeilBrown5cf00fc2012-05-21 09:28:20 +10003866 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003867
NeilBrown5cf00fc2012-05-21 09:28:20 +10003868 if (conf->geo.far_offset)
3869 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003870 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003871 sector_div(size, conf->geo.far_copies);
3872 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003873 }
3874}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003875
NeilBrowndeb200d2012-05-21 09:28:33 +10003876enum geo_type {geo_new, geo_old, geo_start};
3877static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3878{
3879 int nc, fc, fo;
3880 int layout, chunk, disks;
3881 switch (new) {
3882 case geo_old:
3883 layout = mddev->layout;
3884 chunk = mddev->chunk_sectors;
3885 disks = mddev->raid_disks - mddev->delta_disks;
3886 break;
3887 case geo_new:
3888 layout = mddev->new_layout;
3889 chunk = mddev->new_chunk_sectors;
3890 disks = mddev->raid_disks;
3891 break;
3892 default: /* avoid 'may be unused' warnings */
3893 case geo_start: /* new when starting reshape - raid_disks not
3894 * updated yet. */
3895 layout = mddev->new_layout;
3896 chunk = mddev->new_chunk_sectors;
3897 disks = mddev->raid_disks + mddev->delta_disks;
3898 break;
3899 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003900 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003901 return -1;
3902 if (chunk < (PAGE_SIZE >> 9) ||
3903 !is_power_of_2(chunk))
3904 return -2;
3905 nc = layout & 255;
3906 fc = (layout >> 8) & 255;
3907 fo = layout & (1<<16);
3908 geo->raid_disks = disks;
3909 geo->near_copies = nc;
3910 geo->far_copies = fc;
3911 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003912 switch (layout >> 17) {
3913 case 0: /* original layout. simple but not always optimal */
3914 geo->far_set_size = disks;
3915 break;
3916 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3917 * actually using this, but leave code here just in case.*/
3918 geo->far_set_size = disks/fc;
3919 WARN(geo->far_set_size < fc,
3920 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3921 break;
3922 case 2: /* "improved" layout fixed to match documentation */
3923 geo->far_set_size = fc * nc;
3924 break;
3925 default: /* Not a valid layout */
3926 return -1;
3927 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003928 geo->chunk_mask = chunk - 1;
3929 geo->chunk_shift = ffz(~chunk);
3930 return nc*fc;
3931}
3932
NeilBrowne879a872011-10-11 16:49:02 +11003933static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
NeilBrowne879a872011-10-11 16:49:02 +11003935 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003936 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003937 struct geom geo;
3938 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
NeilBrowndeb200d2012-05-21 09:28:33 +10003940 copies = setup_geo(&geo, mddev, geo_new);
3941
3942 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003943 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3944 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
NeilBrown2604b702006-01-06 00:20:36 -08003947
NeilBrowndeb200d2012-05-21 09:28:33 +10003948 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003949 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3950 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 goto out;
3952 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003953
3954 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003955 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003956 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003958
NeilBrown3ea7daa2012-05-22 13:53:47 +10003959 /* FIXME calc properly */
Kees Cook6396bb22018-06-12 14:03:40 -07003960 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
3961 sizeof(struct raid10_info),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003962 GFP_KERNEL);
3963 if (!conf->mirrors)
3964 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003965
3966 conf->tmppage = alloc_page(GFP_KERNEL);
3967 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003968 goto out;
3969
NeilBrowndeb200d2012-05-21 09:28:33 +10003970 conf->geo = geo;
3971 conf->copies = copies;
Marcos Paulo de Souza3f677f92019-06-14 15:41:04 -07003972 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -07003973 rbio_pool_free, conf);
Kent Overstreetafeee512018-05-20 18:25:52 -04003974 if (err)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003975 goto out;
3976
Kent Overstreetafeee512018-05-20 18:25:52 -04003977 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
3978 if (err)
NeilBrownfc9977d2017-04-05 14:05:51 +10003979 goto out;
3980
NeilBrown6508fdb2012-05-17 10:08:45 +10003981 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003982 if (mddev->reshape_position == MaxSector) {
3983 conf->prev = conf->geo;
3984 conf->reshape_progress = MaxSector;
3985 } else {
3986 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3987 err = -EINVAL;
3988 goto out;
3989 }
3990 conf->reshape_progress = mddev->reshape_position;
3991 if (conf->prev.far_offset)
3992 conf->prev.stride = 1 << conf->prev.chunk_shift;
3993 else
3994 /* far_copies must be 1 */
3995 conf->prev.stride = conf->dev_sectors;
3996 }
NeilBrown299b0682015-07-06 17:37:49 +10003997 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003998 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003999 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10004000 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004001
4002 spin_lock_init(&conf->resync_lock);
4003 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02004004 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004005
Kent Overstreetafeee512018-05-20 18:25:52 -04004006 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10004007 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11004008 if (!conf->thread)
4009 goto out;
4010
Trela, Maciejdab8b292010-03-08 16:02:45 +11004011 conf->mddev = mddev;
4012 return conf;
4013
4014 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11004015 if (conf) {
Kent Overstreetafeee512018-05-20 18:25:52 -04004016 mempool_exit(&conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004017 kfree(conf->mirrors);
4018 safe_put_page(conf->tmppage);
Kent Overstreetafeee512018-05-20 18:25:52 -04004019 bioset_exit(&conf->bio_split);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004020 kfree(conf);
4021 }
4022 return ERR_PTR(err);
4023}
4024
Christoph Hellwig16ef5102020-09-24 08:51:33 +02004025static void raid10_set_io_opt(struct r10conf *conf)
4026{
4027 int raid_disks = conf->geo.raid_disks;
4028
4029 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4030 raid_disks /= conf->geo.near_copies;
4031 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4032 raid_disks);
4033}
4034
Shaohua Li849674e2016-01-20 13:52:20 -08004035static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004036{
NeilBrowne879a872011-10-11 16:49:02 +11004037 struct r10conf *conf;
Christoph Hellwig16ef5102020-09-24 08:51:33 +02004038 int i, disk_idx;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004039 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11004040 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004041 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004042 sector_t min_offset_diff = 0;
4043 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11004044 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004045
NeilBrowna415c0f2017-06-05 16:05:13 +10004046 if (mddev_init_writes_pending(mddev) < 0)
4047 return -ENOMEM;
4048
Trela, Maciejdab8b292010-03-08 16:02:45 +11004049 if (mddev->private == NULL) {
4050 conf = setup_conf(mddev);
4051 if (IS_ERR(conf))
4052 return PTR_ERR(conf);
4053 mddev->private = conf;
4054 }
4055 conf = mddev->private;
4056 if (!conf)
4057 goto out;
4058
Guoqing Jiang8db87912017-10-24 15:11:52 +08004059 if (mddev_is_clustered(conf->mddev)) {
4060 int fc, fo;
4061
4062 fc = (mddev->layout >> 8) & 255;
4063 fo = mddev->layout & (1<<16);
4064 if (fc > 1 || fo > 0) {
4065 pr_err("only near layout is supported by clustered"
4066 " raid10\n");
Lidong Zhong43a52122018-01-23 23:06:12 +08004067 goto out_free_conf;
Guoqing Jiang8db87912017-10-24 15:11:52 +08004068 }
4069 }
4070
Trela, Maciejdab8b292010-03-08 16:02:45 +11004071 mddev->thread = conf->thread;
4072 conf->thread = NULL;
4073
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10004074 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11004075 blk_queue_max_discard_sectors(mddev->queue,
Xiao Nid30588b2021-02-04 15:50:46 +08004076 UINT_MAX);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07004077 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02004078 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
Christoph Hellwig16ef5102020-09-24 08:51:33 +02004079 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4080 raid10_set_io_opt(conf);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10004081 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004082
NeilBrowndafb20f2012-03-19 12:46:39 +11004083 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004084 long long diff;
NeilBrown34b343c2011-07-28 11:31:47 +10004085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10004087 if (disk_idx < 0)
4088 continue;
4089 if (disk_idx >= conf->geo.raid_disks &&
4090 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 continue;
4092 disk = conf->mirrors + disk_idx;
4093
NeilBrown56a2559b2011-12-23 10:17:55 +11004094 if (test_bit(Replacement, &rdev->flags)) {
4095 if (disk->replacement)
4096 goto out_free_conf;
4097 disk->replacement = rdev;
4098 } else {
4099 if (disk->rdev)
4100 goto out_free_conf;
4101 disk->rdev = rdev;
4102 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004103 diff = (rdev->new_data_offset - rdev->data_offset);
4104 if (!mddev->reshape_backwards)
4105 diff = -diff;
4106 if (diff < 0)
4107 diff = 0;
4108 if (first || diff < min_offset_diff)
4109 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11004110
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10004111 if (mddev->gendisk)
4112 disk_stack_limits(mddev->gendisk, rdev->bdev,
4113 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11004116
4117 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
4118 discard_supported = true;
Guoqing Jiang6f287ca2017-04-06 09:12:18 +08004119 first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004121
Jonathan Brassowed30be02012-10-31 11:42:30 +11004122 if (mddev->queue) {
4123 if (discard_supported)
Bart Van Assche8b904b52018-03-07 17:10:10 -08004124 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
Jonathan Brassowed30be02012-10-31 11:42:30 +11004125 mddev->queue);
4126 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08004127 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
Jonathan Brassowed30be02012-10-31 11:42:30 +11004128 mddev->queue);
4129 }
NeilBrown6d508242005-09-09 16:24:03 -07004130 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10004131 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11004132 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07004133 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 goto out_free_conf;
4135 }
4136
NeilBrown3ea7daa2012-05-22 13:53:47 +10004137 if (conf->reshape_progress != MaxSector) {
4138 /* must ensure that shape change is supported */
4139 if (conf->geo.far_copies != 1 &&
4140 conf->geo.far_offset == 0)
4141 goto out_free_conf;
4142 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10004143 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004144 goto out_free_conf;
4145 }
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10004148 for (i = 0;
4149 i < conf->geo.raid_disks
4150 || i < conf->prev.raid_disks;
4151 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 disk = conf->mirrors + i;
4154
NeilBrown56a2559b2011-12-23 10:17:55 +11004155 if (!disk->rdev && disk->replacement) {
4156 /* The replacement is all we have - use it */
4157 disk->rdev = disk->replacement;
4158 disk->replacement = NULL;
4159 clear_bit(Replacement, &disk->rdev->flags);
4160 }
4161
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004162 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07004163 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 disk->head_position = 0;
4165 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11004166 if (disk->rdev &&
4167 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10004168 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
BingJing Changbda31532018-06-28 18:40:11 +08004170
4171 if (disk->replacement &&
4172 !test_bit(In_sync, &disk->replacement->flags) &&
4173 disk->replacement->saved_raid_disk < 0) {
4174 conf->fullsync = 1;
4175 }
4176
NeilBrownd890fa22011-10-26 11:54:39 +11004177 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
4179
Andre Noll8c6ac8682009-06-18 08:48:06 +10004180 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11004181 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4182 mdname(mddev));
4183 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10004184 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4185 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 /*
4187 * Ok, everything is just fine now
4188 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11004189 mddev->dev_sectors = conf->dev_sectors;
4190 size = raid10_size(mddev, 0, 0);
4191 md_set_array_sectors(mddev, size);
4192 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11004193 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Martin K. Petersena91a2782011-03-17 11:11:05 +01004195 if (md_integrity_register(mddev))
4196 goto out_free_conf;
4197
NeilBrown3ea7daa2012-05-22 13:53:47 +10004198 if (conf->reshape_progress != MaxSector) {
4199 unsigned long before_length, after_length;
4200
4201 before_length = ((1 << conf->prev.chunk_shift) *
4202 conf->prev.far_copies);
4203 after_length = ((1 << conf->geo.chunk_shift) *
4204 conf->geo.far_copies);
4205
4206 if (max(before_length, after_length) > min_offset_diff) {
4207 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11004208 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10004209 goto out_free_conf;
4210 }
4211 conf->offset_diff = min_offset_diff;
4212
NeilBrown3ea7daa2012-05-22 13:53:47 +10004213 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4214 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4215 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4216 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4217 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4218 "reshape");
Aditya Pakkie406f122019-03-04 16:48:54 -06004219 if (!mddev->sync_thread)
4220 goto out_free_conf;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004221 }
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return 0;
4224
4225out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10004226 md_unregister_thread(&mddev->thread);
Kent Overstreetafeee512018-05-20 18:25:52 -04004227 mempool_exit(&conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08004228 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07004229 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 kfree(conf);
4231 mddev->private = NULL;
4232out:
4233 return -EIO;
4234}
4235
NeilBrownafa0f552014-12-15 12:56:58 +11004236static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
NeilBrownafa0f552014-12-15 12:56:58 +11004238 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Kent Overstreetafeee512018-05-20 18:25:52 -04004240 mempool_exit(&conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10004241 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07004242 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10004243 kfree(conf->mirrors_old);
4244 kfree(conf->mirrors_new);
Kent Overstreetafeee512018-05-20 18:25:52 -04004245 bioset_exit(&conf->bio_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
NeilBrownb03e0cc2017-10-19 12:49:15 +11004249static void raid10_quiesce(struct mddev *mddev, int quiesce)
NeilBrown6cce3b22006-01-06 00:20:16 -08004250{
NeilBrowne879a872011-10-11 16:49:02 +11004251 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08004252
NeilBrownb03e0cc2017-10-19 12:49:15 +11004253 if (quiesce)
NeilBrown6cce3b22006-01-06 00:20:16 -08004254 raise_barrier(conf, 0);
NeilBrownb03e0cc2017-10-19 12:49:15 +11004255 else
NeilBrown6cce3b22006-01-06 00:20:16 -08004256 lower_barrier(conf);
NeilBrown6cce3b22006-01-06 00:20:16 -08004257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
NeilBrown006a09a2012-03-19 12:46:40 +11004259static int raid10_resize(struct mddev *mddev, sector_t sectors)
4260{
4261 /* Resize of 'far' arrays is not supported.
4262 * For 'near' and 'offset' arrays we can set the
4263 * number of sectors used to be an appropriate multiple
4264 * of the chunk size.
4265 * For 'offset', this is far_copies*chunksize.
4266 * For 'near' the multiplier is the LCM of
4267 * near_copies and raid_disks.
4268 * So if far_copies > 1 && !far_offset, fail.
4269 * Else find LCM(raid_disks, near_copy)*far_copies and
4270 * multiply by chunk_size. Then round to this number.
4271 * This is mostly done by raid10_size()
4272 */
4273 struct r10conf *conf = mddev->private;
4274 sector_t oldsize, size;
4275
NeilBrownf8c9e742012-05-21 09:28:33 +10004276 if (mddev->reshape_position != MaxSector)
4277 return -EBUSY;
4278
NeilBrown5cf00fc2012-05-21 09:28:20 +10004279 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11004280 return -EINVAL;
4281
4282 oldsize = raid10_size(mddev, 0, 0);
4283 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10004284 if (mddev->external_size &&
4285 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11004286 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10004287 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004288 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10004289 if (ret)
4290 return ret;
4291 }
4292 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11004293 if (sectors > mddev->dev_sectors &&
4294 mddev->recovery_cp > oldsize) {
4295 mddev->recovery_cp = oldsize;
4296 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4297 }
NeilBrown6508fdb2012-05-17 10:08:45 +10004298 calc_sectors(conf, sectors);
4299 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11004300 mddev->resync_max_sectors = size;
4301 return 0;
4302}
4303
NeilBrown53a6ab42015-02-12 14:09:57 +11004304static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004305{
NeilBrown3cb03002011-10-11 16:45:26 +11004306 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11004307 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004308
4309 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11004310 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4311 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004312 return ERR_PTR(-EINVAL);
4313 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004314 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004315
Trela, Maciejdab8b292010-03-08 16:02:45 +11004316 /* Set new parameters */
4317 mddev->new_level = 10;
4318 /* new layout: far_copies = 1, near_copies = 2 */
4319 mddev->new_layout = (1<<8) + 2;
4320 mddev->new_chunk_sectors = mddev->chunk_sectors;
4321 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004322 mddev->raid_disks *= 2;
4323 /* make sure it will be not marked as dirty */
4324 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11004325 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004326
4327 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004328 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11004329 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11004330 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004331 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11004332 rdev->sectors = size;
4333 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004334 conf->barrier = 1;
4335 }
4336
Trela, Maciejdab8b292010-03-08 16:02:45 +11004337 return conf;
4338}
4339
NeilBrownfd01b882011-10-11 16:47:53 +11004340static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004341{
NeilBrowne373ab12011-10-11 16:48:59 +11004342 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004343
4344 /* raid10 can take over:
4345 * raid0 - providing it has only two drives
4346 */
4347 if (mddev->level == 0) {
4348 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11004349 raid0_conf = mddev->private;
4350 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11004351 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4352 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004353 return ERR_PTR(-EINVAL);
4354 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004355 return raid10_takeover_raid0(mddev,
4356 raid0_conf->strip_zone->zone_end,
4357 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004358 }
4359 return ERR_PTR(-EINVAL);
4360}
4361
NeilBrown3ea7daa2012-05-22 13:53:47 +10004362static int raid10_check_reshape(struct mddev *mddev)
4363{
4364 /* Called when there is a request to change
4365 * - layout (to ->new_layout)
4366 * - chunk size (to ->new_chunk_sectors)
4367 * - raid_disks (by delta_disks)
4368 * or when trying to restart a reshape that was ongoing.
4369 *
4370 * We need to validate the request and possibly allocate
4371 * space if that might be an issue later.
4372 *
4373 * Currently we reject any reshape of a 'far' mode array,
4374 * allow chunk size to change if new is generally acceptable,
4375 * allow raid_disks to increase, and allow
4376 * a switch between 'near' mode and 'offset' mode.
4377 */
4378 struct r10conf *conf = mddev->private;
4379 struct geom geo;
4380
4381 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4382 return -EINVAL;
4383
4384 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4385 /* mustn't change number of copies */
4386 return -EINVAL;
4387 if (geo.far_copies > 1 && !geo.far_offset)
4388 /* Cannot switch to 'far' mode */
4389 return -EINVAL;
4390
4391 if (mddev->array_sectors & geo.chunk_mask)
4392 /* not factor of array size */
4393 return -EINVAL;
4394
NeilBrown3ea7daa2012-05-22 13:53:47 +10004395 if (!enough(conf, -1))
4396 return -EINVAL;
4397
4398 kfree(conf->mirrors_new);
4399 conf->mirrors_new = NULL;
4400 if (mddev->delta_disks > 0) {
4401 /* allocate new 'mirrors' list */
Kees Cook6396bb22018-06-12 14:03:40 -07004402 conf->mirrors_new =
4403 kcalloc(mddev->raid_disks + mddev->delta_disks,
4404 sizeof(struct raid10_info),
4405 GFP_KERNEL);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004406 if (!conf->mirrors_new)
4407 return -ENOMEM;
4408 }
4409 return 0;
4410}
4411
4412/*
4413 * Need to check if array has failed when deciding whether to:
4414 * - start an array
4415 * - remove non-faulty devices
4416 * - add a spare
4417 * - allow a reshape
4418 * This determination is simple when no reshape is happening.
4419 * However if there is a reshape, we need to carefully check
4420 * both the before and after sections.
4421 * This is because some failed devices may only affect one
4422 * of the two sections, and some non-in_sync devices may
4423 * be insync in the section most affected by failed devices.
4424 */
4425static int calc_degraded(struct r10conf *conf)
4426{
4427 int degraded, degraded2;
4428 int i;
4429
4430 rcu_read_lock();
4431 degraded = 0;
4432 /* 'prev' section first */
4433 for (i = 0; i < conf->prev.raid_disks; i++) {
4434 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4435 if (!rdev || test_bit(Faulty, &rdev->flags))
4436 degraded++;
4437 else if (!test_bit(In_sync, &rdev->flags))
4438 /* When we can reduce the number of devices in
4439 * an array, this might not contribute to
4440 * 'degraded'. It does now.
4441 */
4442 degraded++;
4443 }
4444 rcu_read_unlock();
4445 if (conf->geo.raid_disks == conf->prev.raid_disks)
4446 return degraded;
4447 rcu_read_lock();
4448 degraded2 = 0;
4449 for (i = 0; i < conf->geo.raid_disks; i++) {
4450 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4451 if (!rdev || test_bit(Faulty, &rdev->flags))
4452 degraded2++;
4453 else if (!test_bit(In_sync, &rdev->flags)) {
4454 /* If reshape is increasing the number of devices,
4455 * this section has already been recovered, so
4456 * it doesn't contribute to degraded.
4457 * else it does.
4458 */
4459 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4460 degraded2++;
4461 }
4462 }
4463 rcu_read_unlock();
4464 if (degraded2 > degraded)
4465 return degraded2;
4466 return degraded;
4467}
4468
4469static int raid10_start_reshape(struct mddev *mddev)
4470{
4471 /* A 'reshape' has been requested. This commits
4472 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4473 * This also checks if there are enough spares and adds them
4474 * to the array.
4475 * We currently require enough spares to make the final
4476 * array non-degraded. We also require that the difference
4477 * between old and new data_offset - on each device - is
4478 * enough that we never risk over-writing.
4479 */
4480
4481 unsigned long before_length, after_length;
4482 sector_t min_offset_diff = 0;
4483 int first = 1;
4484 struct geom new;
4485 struct r10conf *conf = mddev->private;
4486 struct md_rdev *rdev;
4487 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004488 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004489
4490 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4491 return -EBUSY;
4492
4493 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4494 return -EINVAL;
4495
4496 before_length = ((1 << conf->prev.chunk_shift) *
4497 conf->prev.far_copies);
4498 after_length = ((1 << conf->geo.chunk_shift) *
4499 conf->geo.far_copies);
4500
4501 rdev_for_each(rdev, mddev) {
4502 if (!test_bit(In_sync, &rdev->flags)
4503 && !test_bit(Faulty, &rdev->flags))
4504 spares++;
4505 if (rdev->raid_disk >= 0) {
4506 long long diff = (rdev->new_data_offset
4507 - rdev->data_offset);
4508 if (!mddev->reshape_backwards)
4509 diff = -diff;
4510 if (diff < 0)
4511 diff = 0;
4512 if (first || diff < min_offset_diff)
4513 min_offset_diff = diff;
Shaohua Lib5063352017-05-01 12:15:07 -07004514 first = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004515 }
4516 }
4517
4518 if (max(before_length, after_length) > min_offset_diff)
4519 return -EINVAL;
4520
4521 if (spares < mddev->delta_disks)
4522 return -EINVAL;
4523
4524 conf->offset_diff = min_offset_diff;
4525 spin_lock_irq(&conf->device_lock);
4526 if (conf->mirrors_new) {
4527 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004528 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004529 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004530 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004531 conf->mirrors_old = conf->mirrors;
4532 conf->mirrors = conf->mirrors_new;
4533 conf->mirrors_new = NULL;
4534 }
4535 setup_geo(&conf->geo, mddev, geo_start);
4536 smp_mb();
4537 if (mddev->reshape_backwards) {
4538 sector_t size = raid10_size(mddev, 0, 0);
4539 if (size < mddev->array_sectors) {
4540 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004541 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4542 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004543 return -EINVAL;
4544 }
4545 mddev->resync_max_sectors = size;
4546 conf->reshape_progress = size;
4547 } else
4548 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004549 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004550 spin_unlock_irq(&conf->device_lock);
4551
NeilBrownbb63a702012-05-22 13:55:28 +10004552 if (mddev->delta_disks && mddev->bitmap) {
Guoqing Jiangafd75622018-10-18 16:37:41 +08004553 struct mdp_superblock_1 *sb = NULL;
4554 sector_t oldsize, newsize;
4555
4556 oldsize = raid10_size(mddev, 0, 0);
4557 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4558
4559 if (!mddev_is_clustered(mddev)) {
4560 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4561 if (ret)
4562 goto abort;
4563 else
4564 goto out;
4565 }
4566
4567 rdev_for_each(rdev, mddev) {
4568 if (rdev->raid_disk > -1 &&
4569 !test_bit(Faulty, &rdev->flags))
4570 sb = page_address(rdev->sb_page);
4571 }
4572
4573 /*
4574 * some node is already performing reshape, and no need to
4575 * call md_bitmap_resize again since it should be called when
4576 * receiving BITMAP_RESIZE msg
4577 */
4578 if ((sb && (le32_to_cpu(sb->feature_map) &
4579 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4580 goto out;
4581
4582 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
NeilBrownbb63a702012-05-22 13:55:28 +10004583 if (ret)
4584 goto abort;
Guoqing Jiangafd75622018-10-18 16:37:41 +08004585
4586 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4587 if (ret) {
4588 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4589 goto abort;
4590 }
NeilBrownbb63a702012-05-22 13:55:28 +10004591 }
Guoqing Jiangafd75622018-10-18 16:37:41 +08004592out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10004593 if (mddev->delta_disks > 0) {
4594 rdev_for_each(rdev, mddev)
4595 if (rdev->raid_disk < 0 &&
4596 !test_bit(Faulty, &rdev->flags)) {
4597 if (raid10_add_disk(mddev, rdev) == 0) {
4598 if (rdev->raid_disk >=
4599 conf->prev.raid_disks)
4600 set_bit(In_sync, &rdev->flags);
4601 else
4602 rdev->recovery_offset = 0;
4603
Damien Le Moal38ffc012020-07-16 13:54:43 +09004604 /* Failure here is OK */
4605 sysfs_link_rdev(mddev, rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004606 }
4607 } else if (rdev->raid_disk >= conf->prev.raid_disks
4608 && !test_bit(Faulty, &rdev->flags)) {
4609 /* This is a spare that was manually added */
4610 set_bit(In_sync, &rdev->flags);
4611 }
4612 }
4613 /* When a reshape changes the number of devices,
4614 * ->degraded is measured against the larger of the
4615 * pre and post numbers.
4616 */
4617 spin_lock_irq(&conf->device_lock);
4618 mddev->degraded = calc_degraded(conf);
4619 spin_unlock_irq(&conf->device_lock);
4620 mddev->raid_disks = conf->geo.raid_disks;
4621 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004622 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004623
4624 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4625 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004626 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004627 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4628 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4629
4630 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4631 "reshape");
4632 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004633 ret = -EAGAIN;
4634 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004635 }
4636 conf->reshape_checkpoint = jiffies;
4637 md_wakeup_thread(mddev->sync_thread);
4638 md_new_event(mddev);
4639 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004640
4641abort:
4642 mddev->recovery = 0;
4643 spin_lock_irq(&conf->device_lock);
4644 conf->geo = conf->prev;
4645 mddev->raid_disks = conf->geo.raid_disks;
4646 rdev_for_each(rdev, mddev)
4647 rdev->new_data_offset = rdev->data_offset;
4648 smp_wmb();
4649 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004650 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004651 mddev->reshape_position = MaxSector;
4652 spin_unlock_irq(&conf->device_lock);
4653 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004654}
4655
4656/* Calculate the last device-address that could contain
4657 * any block from the chunk that includes the array-address 's'
4658 * and report the next address.
4659 * i.e. the address returned will be chunk-aligned and after
4660 * any data that is in the chunk containing 's'.
4661 */
4662static sector_t last_dev_address(sector_t s, struct geom *geo)
4663{
4664 s = (s | geo->chunk_mask) + 1;
4665 s >>= geo->chunk_shift;
4666 s *= geo->near_copies;
4667 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4668 s *= geo->far_copies;
4669 s <<= geo->chunk_shift;
4670 return s;
4671}
4672
4673/* Calculate the first device-address that could contain
4674 * any block from the chunk that includes the array-address 's'.
4675 * This too will be the start of a chunk
4676 */
4677static sector_t first_dev_address(sector_t s, struct geom *geo)
4678{
4679 s >>= geo->chunk_shift;
4680 s *= geo->near_copies;
4681 sector_div(s, geo->raid_disks);
4682 s *= geo->far_copies;
4683 s <<= geo->chunk_shift;
4684 return s;
4685}
4686
4687static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4688 int *skipped)
4689{
4690 /* We simply copy at most one chunk (smallest of old and new)
4691 * at a time, possibly less if that exceeds RESYNC_PAGES,
4692 * or we hit a bad block or something.
4693 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004694 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004695 * can record any location.
4696 *
4697 * If we will want to write to a location that isn't
4698 * yet recorded as 'safe' (i.e. in metadata on disk) then
4699 * we need to flush all reshape requests and update the metadata.
4700 *
4701 * When reshaping forwards (e.g. to more devices), we interpret
4702 * 'safe' as the earliest block which might not have been copied
4703 * down yet. We divide this by previous stripe size and multiply
4704 * by previous stripe length to get lowest device offset that we
4705 * cannot write to yet.
4706 * We interpret 'sector_nr' as an address that we want to write to.
4707 * From this we use last_device_address() to find where we might
4708 * write to, and first_device_address on the 'safe' position.
4709 * If this 'next' write position is after the 'safe' position,
4710 * we must update the metadata to increase the 'safe' position.
4711 *
4712 * When reshaping backwards, we round in the opposite direction
4713 * and perform the reverse test: next write position must not be
4714 * less than current safe position.
4715 *
4716 * In all this the minimum difference in data offsets
4717 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004718 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004719 *
4720 * We need to prepare all the bios here before we start any IO
4721 * to ensure the size we choose is acceptable to all devices.
4722 * The means one for each copy for write-out and an extra one for
4723 * read-in.
4724 * We store the read-in bio in ->master_bio and the others in
4725 * ->devs[x].bio and ->devs[x].repl_bio.
4726 */
4727 struct r10conf *conf = mddev->private;
4728 struct r10bio *r10_bio;
4729 sector_t next, safe, last;
4730 int max_sectors;
4731 int nr_sectors;
4732 int s;
4733 struct md_rdev *rdev;
4734 int need_flush = 0;
4735 struct bio *blist;
4736 struct bio *bio, *read_bio;
4737 int sectors_done = 0;
Ming Leif0250612017-03-17 00:12:33 +08004738 struct page **pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004739
4740 if (sector_nr == 0) {
4741 /* If restarting in the middle, skip the initial sectors */
4742 if (mddev->reshape_backwards &&
4743 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4744 sector_nr = (raid10_size(mddev, 0, 0)
4745 - conf->reshape_progress);
4746 } else if (!mddev->reshape_backwards &&
4747 conf->reshape_progress > 0)
4748 sector_nr = conf->reshape_progress;
4749 if (sector_nr) {
4750 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07004751 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004752 *skipped = 1;
4753 return sector_nr;
4754 }
4755 }
4756
4757 /* We don't use sector_nr to track where we are up to
4758 * as that doesn't work well for ->reshape_backwards.
4759 * So just use ->reshape_progress.
4760 */
4761 if (mddev->reshape_backwards) {
4762 /* 'next' is the earliest device address that we might
4763 * write to for this chunk in the new layout
4764 */
4765 next = first_dev_address(conf->reshape_progress - 1,
4766 &conf->geo);
4767
4768 /* 'safe' is the last device address that we might read from
4769 * in the old layout after a restart
4770 */
4771 safe = last_dev_address(conf->reshape_safe - 1,
4772 &conf->prev);
4773
4774 if (next + conf->offset_diff < safe)
4775 need_flush = 1;
4776
4777 last = conf->reshape_progress - 1;
4778 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4779 & conf->prev.chunk_mask);
Zhen Leie2873082020-08-21 09:29:18 +08004780 if (sector_nr + RESYNC_SECTORS < last)
4781 sector_nr = last + 1 - RESYNC_SECTORS;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004782 } else {
4783 /* 'next' is after the last device address that we
4784 * might write to for this chunk in the new layout
4785 */
4786 next = last_dev_address(conf->reshape_progress, &conf->geo);
4787
4788 /* 'safe' is the earliest device address that we might
4789 * read from in the old layout after a restart
4790 */
4791 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4792
4793 /* Need to update metadata if 'next' might be beyond 'safe'
4794 * as that would possibly corrupt data
4795 */
4796 if (next > safe + conf->offset_diff)
4797 need_flush = 1;
4798
4799 sector_nr = conf->reshape_progress;
4800 last = sector_nr | (conf->geo.chunk_mask
4801 & conf->prev.chunk_mask);
4802
Zhen Leie2873082020-08-21 09:29:18 +08004803 if (sector_nr + RESYNC_SECTORS <= last)
4804 last = sector_nr + RESYNC_SECTORS - 1;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004805 }
4806
4807 if (need_flush ||
4808 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4809 /* Need to update reshape_position in metadata */
4810 wait_barrier(conf);
4811 mddev->reshape_position = conf->reshape_progress;
4812 if (mddev->reshape_backwards)
4813 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4814 - conf->reshape_progress;
4815 else
4816 mddev->curr_resync_completed = conf->reshape_progress;
4817 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004818 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004819 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004820 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004821 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4822 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4823 allow_barrier(conf);
4824 return sectors_done;
4825 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004826 conf->reshape_safe = mddev->reshape_position;
4827 allow_barrier(conf);
4828 }
4829
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004830 raise_barrier(conf, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004831read_more:
4832 /* Now schedule reads for blocks from sector_nr to last */
Shaohua Li208410b2017-08-24 17:50:40 -07004833 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004834 r10_bio->state = 0;
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004835 raise_barrier(conf, 1);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004836 atomic_set(&r10_bio->remaining, 0);
4837 r10_bio->mddev = mddev;
4838 r10_bio->sector = sector_nr;
4839 set_bit(R10BIO_IsReshape, &r10_bio->state);
4840 r10_bio->sectors = last - sector_nr + 1;
4841 rdev = read_balance(conf, r10_bio, &max_sectors);
4842 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4843
4844 if (!rdev) {
4845 /* Cannot read from here, so need to record bad blocks
4846 * on all the target devices.
4847 */
4848 // FIXME
Kent Overstreetafeee512018-05-20 18:25:52 -04004849 mempool_free(r10_bio, &conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004850 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4851 return sectors_done;
4852 }
4853
Christoph Hellwiga78f18d2021-01-26 15:52:41 +01004854 read_bio = bio_alloc_bioset(GFP_KERNEL, RESYNC_PAGES, &mddev->bio_set);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004855
Christoph Hellwig74d46992017-08-23 19:10:32 +02004856 bio_set_dev(read_bio, rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004857 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004858 + rdev->data_offset);
4859 read_bio->bi_private = r10_bio;
Ming Lei81fa1522017-03-17 00:12:32 +08004860 read_bio->bi_end_io = end_reshape_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004861 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004862 r10_bio->master_bio = read_bio;
4863 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4864
Guoqing Jiang7564bed2018-10-18 16:37:42 +08004865 /*
4866 * Broadcast RESYNC message to other nodes, so all nodes would not
4867 * write to the region to avoid conflict.
4868 */
4869 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4870 struct mdp_superblock_1 *sb = NULL;
4871 int sb_reshape_pos = 0;
4872
4873 conf->cluster_sync_low = sector_nr;
4874 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4875 sb = page_address(rdev->sb_page);
4876 if (sb) {
4877 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4878 /*
4879 * Set cluster_sync_low again if next address for array
4880 * reshape is less than cluster_sync_low. Since we can't
4881 * update cluster_sync_low until it has finished reshape.
4882 */
4883 if (sb_reshape_pos < conf->cluster_sync_low)
4884 conf->cluster_sync_low = sb_reshape_pos;
4885 }
4886
4887 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4888 conf->cluster_sync_high);
4889 }
4890
NeilBrown3ea7daa2012-05-22 13:53:47 +10004891 /* Now find the locations in the new layout */
4892 __raid10_find_phys(&conf->geo, r10_bio);
4893
4894 blist = read_bio;
4895 read_bio->bi_next = NULL;
4896
NeilBrownd094d682016-06-02 16:19:52 +10004897 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004898 for (s = 0; s < conf->copies*2; s++) {
4899 struct bio *b;
4900 int d = r10_bio->devs[s/2].devnum;
4901 struct md_rdev *rdev2;
4902 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004903 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004904 b = r10_bio->devs[s/2].repl_bio;
4905 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004906 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004907 b = r10_bio->devs[s/2].bio;
4908 }
4909 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4910 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004911
Christoph Hellwig74d46992017-08-23 19:10:32 +02004912 bio_set_dev(b, rdev2->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004913 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4914 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004915 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004916 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004917 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004918 blist = b;
4919 }
4920
4921 /* Now add as many pages as possible to all of these bios. */
4922
4923 nr_sectors = 0;
Ming Leif0250612017-03-17 00:12:33 +08004924 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004925 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
Ming Leif0250612017-03-17 00:12:33 +08004926 struct page *page = pages[s / (PAGE_SIZE >> 9)];
NeilBrown3ea7daa2012-05-22 13:53:47 +10004927 int len = (max_sectors - s) << 9;
4928 if (len > PAGE_SIZE)
4929 len = PAGE_SIZE;
4930 for (bio = blist; bio ; bio = bio->bi_next) {
Ming Leic85ba142017-03-17 00:12:22 +08004931 /*
4932 * won't fail because the vec table is big enough
4933 * to hold all these pages
4934 */
4935 bio_add_page(bio, page, len, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004936 }
4937 sector_nr += len >> 9;
4938 nr_sectors += len >> 9;
4939 }
NeilBrownd094d682016-06-02 16:19:52 +10004940 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004941 r10_bio->sectors = nr_sectors;
4942
4943 /* Now submit the read */
Christoph Hellwig74d46992017-08-23 19:10:32 +02004944 md_sync_acct_bio(read_bio, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004945 atomic_inc(&r10_bio->remaining);
4946 read_bio->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02004947 submit_bio_noacct(read_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004948 sectors_done += nr_sectors;
4949 if (sector_nr <= last)
4950 goto read_more;
4951
Xiao Ni1d0ffd22018-08-30 15:57:09 +08004952 lower_barrier(conf);
4953
NeilBrown3ea7daa2012-05-22 13:53:47 +10004954 /* Now that we have done the whole section we can
4955 * update reshape_progress
4956 */
4957 if (mddev->reshape_backwards)
4958 conf->reshape_progress -= sectors_done;
4959 else
4960 conf->reshape_progress += sectors_done;
4961
4962 return sectors_done;
4963}
4964
4965static void end_reshape_request(struct r10bio *r10_bio);
4966static int handle_reshape_read_error(struct mddev *mddev,
4967 struct r10bio *r10_bio);
4968static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4969{
4970 /* Reshape read completed. Hopefully we have a block
4971 * to write out.
4972 * If we got a read error then we do sync 1-page reads from
4973 * elsewhere until we find the data - or give up.
4974 */
4975 struct r10conf *conf = mddev->private;
4976 int s;
4977
4978 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4979 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4980 /* Reshape has been aborted */
4981 md_done_sync(mddev, r10_bio->sectors, 0);
4982 return;
4983 }
4984
4985 /* We definitely have the data in the pages, schedule the
4986 * writes.
4987 */
4988 atomic_set(&r10_bio->remaining, 1);
4989 for (s = 0; s < conf->copies*2; s++) {
4990 struct bio *b;
4991 int d = r10_bio->devs[s/2].devnum;
4992 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004993 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004994 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004995 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004996 b = r10_bio->devs[s/2].repl_bio;
4997 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004998 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004999 b = r10_bio->devs[s/2].bio;
5000 }
NeilBrownd094d682016-06-02 16:19:52 +10005001 if (!rdev || test_bit(Faulty, &rdev->flags)) {
5002 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005003 continue;
NeilBrownd094d682016-06-02 16:19:52 +10005004 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10005005 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10005006 rcu_read_unlock();
Christoph Hellwig74d46992017-08-23 19:10:32 +02005007 md_sync_acct_bio(b, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005008 atomic_inc(&r10_bio->remaining);
5009 b->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005010 submit_bio_noacct(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005011 }
5012 end_reshape_request(r10_bio);
5013}
5014
5015static void end_reshape(struct r10conf *conf)
5016{
5017 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5018 return;
5019
5020 spin_lock_irq(&conf->device_lock);
5021 conf->prev = conf->geo;
5022 md_finish_reshape(conf->mddev);
5023 smp_wmb();
5024 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10005025 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005026 spin_unlock_irq(&conf->device_lock);
5027
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +02005028 if (conf->mddev->queue)
Christoph Hellwig16ef5102020-09-24 08:51:33 +02005029 raid10_set_io_opt(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005030 conf->fullsync = 0;
5031}
5032
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005033static void raid10_update_reshape_pos(struct mddev *mddev)
5034{
5035 struct r10conf *conf = mddev->private;
Guoqing Jiang5ebaf802018-10-18 16:37:43 +08005036 sector_t lo, hi;
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005037
Guoqing Jiang5ebaf802018-10-18 16:37:43 +08005038 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5039 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5040 || mddev->reshape_position == MaxSector)
5041 conf->reshape_progress = mddev->reshape_position;
5042 else
5043 WARN_ON_ONCE(1);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005044}
5045
NeilBrown3ea7daa2012-05-22 13:53:47 +10005046static int handle_reshape_read_error(struct mddev *mddev,
5047 struct r10bio *r10_bio)
5048{
5049 /* Use sync reads to get the blocks from somewhere else */
5050 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005051 struct r10conf *conf = mddev->private;
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005052 struct r10bio *r10b;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005053 int slot = 0;
5054 int idx = 0;
Ming Lei2d06e3b2017-03-17 00:12:35 +08005055 struct page **pages;
5056
Gustavo A. R. Silva8cf05a72019-06-14 15:41:09 -07005057 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005058 if (!r10b) {
5059 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5060 return -ENOMEM;
5061 }
5062
Ming Lei2d06e3b2017-03-17 00:12:35 +08005063 /* reshape IOs share pages from .devs[0].bio */
5064 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10005065
NeilBrowne0ee7782012-08-18 09:51:42 +10005066 r10b->sector = r10_bio->sector;
5067 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005068
5069 while (sectors) {
5070 int s = sectors;
5071 int success = 0;
5072 int first_slot = slot;
5073
5074 if (s > (PAGE_SIZE >> 9))
5075 s = PAGE_SIZE >> 9;
5076
NeilBrownd094d682016-06-02 16:19:52 +10005077 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005078 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10005079 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10005080 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005081 sector_t addr;
5082 if (rdev == NULL ||
5083 test_bit(Faulty, &rdev->flags) ||
5084 !test_bit(In_sync, &rdev->flags))
5085 goto failed;
5086
NeilBrowne0ee7782012-08-18 09:51:42 +10005087 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10005088 atomic_inc(&rdev->nr_pending);
5089 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005090 success = sync_page_io(rdev,
5091 addr,
5092 s << 9,
Ming Lei2d06e3b2017-03-17 00:12:35 +08005093 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05005094 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10005095 rdev_dec_pending(rdev, mddev);
5096 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005097 if (success)
5098 break;
5099 failed:
5100 slot++;
5101 if (slot >= conf->copies)
5102 slot = 0;
5103 if (slot == first_slot)
5104 break;
5105 }
NeilBrownd094d682016-06-02 16:19:52 +10005106 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005107 if (!success) {
5108 /* couldn't read this block, must give up */
5109 set_bit(MD_RECOVERY_INTR,
5110 &mddev->recovery);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005111 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005112 return -EIO;
5113 }
5114 sectors -= s;
5115 idx++;
5116 }
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07005117 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005118 return 0;
5119}
5120
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005121static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10005122{
Ming Leif0250612017-03-17 00:12:33 +08005123 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10005124 struct mddev *mddev = r10_bio->mddev;
5125 struct r10conf *conf = mddev->private;
5126 int d;
5127 int slot;
5128 int repl;
5129 struct md_rdev *rdev = NULL;
5130
5131 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5132 if (repl)
5133 rdev = conf->mirrors[d].replacement;
5134 if (!rdev) {
5135 smp_mb();
5136 rdev = conf->mirrors[d].rdev;
5137 }
5138
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005139 if (bio->bi_status) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10005140 /* FIXME should record badblock */
5141 md_error(mddev, rdev);
5142 }
5143
5144 rdev_dec_pending(rdev, mddev);
5145 end_reshape_request(r10_bio);
5146}
5147
5148static void end_reshape_request(struct r10bio *r10_bio)
5149{
5150 if (!atomic_dec_and_test(&r10_bio->remaining))
5151 return;
5152 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5153 bio_put(r10_bio->master_bio);
5154 put_buf(r10_bio);
5155}
5156
5157static void raid10_finish_reshape(struct mddev *mddev)
5158{
5159 struct r10conf *conf = mddev->private;
5160
5161 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5162 return;
5163
5164 if (mddev->delta_disks > 0) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10005165 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5166 mddev->recovery_cp = mddev->resync_max_sectors;
5167 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5168 }
BingJing Chang88763912018-02-22 13:34:46 +08005169 mddev->resync_max_sectors = mddev->array_sectors;
NeilBrown63aced62012-05-22 13:55:33 +10005170 } else {
5171 int d;
NeilBrownd094d682016-06-02 16:19:52 +10005172 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10005173 for (d = conf->geo.raid_disks ;
5174 d < conf->geo.raid_disks - mddev->delta_disks;
5175 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10005176 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10005177 if (rdev)
5178 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10005179 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10005180 if (rdev)
5181 clear_bit(In_sync, &rdev->flags);
5182 }
NeilBrownd094d682016-06-02 16:19:52 +10005183 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10005184 }
5185 mddev->layout = mddev->new_layout;
5186 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5187 mddev->reshape_position = MaxSector;
5188 mddev->delta_disks = 0;
5189 mddev->reshape_backwards = 0;
5190}
5191
NeilBrown84fc4b52011-10-11 16:49:58 +11005192static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193{
5194 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08005195 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08005197 .make_request = raid10_make_request,
5198 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11005199 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08005200 .status = raid10_status,
5201 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 .hot_add_disk = raid10_add_disk,
5203 .hot_remove_disk= raid10_remove_disk,
5204 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08005205 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08005206 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005207 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11005208 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11005209 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10005210 .check_reshape = raid10_check_reshape,
5211 .start_reshape = raid10_start_reshape,
5212 .finish_reshape = raid10_finish_reshape,
Guoqing Jiang7564bed2018-10-18 16:37:42 +08005213 .update_reshape_pos = raid10_update_reshape_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214};
5215
5216static int __init raid_init(void)
5217{
NeilBrown2604b702006-01-06 00:20:36 -08005218 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
5221static void raid_exit(void)
5222{
NeilBrown2604b702006-01-06 00:20:36 -08005223 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224}
5225
5226module_init(raid_init);
5227module_exit(raid_exit);
5228MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005229MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005231MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08005232MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11005233
5234module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);