blob: b0de5b5ee68946fc698e20e0384b8187c82847d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040024#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100026#include <linux/ratelimit.h>
NeilBrown3ea7daa2012-05-22 13:53:47 +100027#include <linux/kthread.h>
NeilBrown109e3762016-11-18 13:22:04 +110028#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110029#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110031#include "raid0.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040032#include "md-bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
37 * chunk_size
38 * raid_disks
39 * near_copies (stored in low byte of layout)
40 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070041 * far_offset (stored in bit 16 of layout )
Jonathan Brassow475901a2013-02-21 13:28:10 +110042 * use_far_sets (stored in bit 17 of layout )
NeilBrown8bce6d32015-10-22 13:20:15 +110043 * use_far_sets_bugfixed (stored in bit 18 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
Jonathan Brassow475901a2013-02-21 13:28:10 +110045 * The data to be stored is divided into chunks using chunksize. Each device
46 * is divided into far_copies sections. In each section, chunks are laid out
47 * in a style similar to raid0, but near_copies copies of each chunk is stored
48 * (each on a different drive). The starting device for each section is offset
49 * near_copies from the starting device of the previous section. Thus there
50 * are (near_copies * far_copies) of each chunk, and each is on a different
51 * drive. near_copies and far_copies must be at least one, and their product
52 * is at most raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070053 *
54 * If far_offset is true, then the far_copies are handled a bit differently.
Jonathan Brassow475901a2013-02-21 13:28:10 +110055 * The copies are still in different stripes, but instead of being very far
56 * apart on disk, there are adjacent stripes.
57 *
58 * The far and offset algorithms are handled slightly differently if
59 * 'use_far_sets' is true. In this case, the array's devices are grouped into
60 * sets that are (near_copies * far_copies) in size. The far copied stripes
61 * are still shifted by 'near_copies' devices, but this shifting stays confined
62 * to the set rather than the entire array. This is done to improve the number
63 * of device combinations that can fail without causing the array to fail.
64 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
65 * on a device):
66 * A B C D A B C D E
67 * ... ...
68 * D A B C E A B C D
69 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
70 * [A B] [C D] [A B] [C D E]
71 * |...| |...| |...| | ... |
72 * [B A] [D C] [B A] [E C D]
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
75/*
76 * Number of guaranteed r10bios in case of extreme VM load:
77 */
78#define NR_RAID10_BIOS 256
79
Jonathan Brassow473e87c2012-07-31 10:03:52 +100080/* when we get a read error on a read-only array, we redirect to another
81 * device without failing the first device, or trying to over-write to
82 * correct the read error. To keep track of bad blocks on a per-bio
83 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
84 */
85#define IO_BLOCKED ((struct bio *)1)
86/* When we successfully write to a known bad-block, we need to remove the
87 * bad-block marking which must be done from process context. So we record
88 * the success by setting devs[n].bio to IO_MADE_GOOD
89 */
90#define IO_MADE_GOOD ((struct bio *)2)
91
92#define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
93
94/* When there are this many requests queued to be written by
NeilBrown34db0cd2011-10-11 16:50:01 +110095 * the raid10 thread, we become 'congested' to provide back-pressure
96 * for writeback.
97 */
98static int max_queued_requests = 1024;
99
NeilBrowne879a872011-10-11 16:49:02 +1100100static void allow_barrier(struct r10conf *conf);
101static void lower_barrier(struct r10conf *conf);
NeilBrown635f6412013-06-11 14:57:09 +1000102static int _enough(struct r10conf *conf, int previous, int ignore);
NeilBrown1919cbb2016-11-18 16:16:12 +1100103static int enough(struct r10conf *conf, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +1000104static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
105 int *skipped);
106static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200107static void end_reshape_write(struct bio *bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +1000108static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -0800109
NeilBrown578b54a2016-11-14 16:30:21 +1100110#define raid10_log(md, fmt, args...) \
111 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
112
Ming Leifb0eb5d2017-07-14 16:14:43 +0800113#include "raid1-10.c"
114
Ming Leif0250612017-03-17 00:12:33 +0800115/*
Ming Leif0250612017-03-17 00:12:33 +0800116 * for resync bio, r10bio pointer can be retrieved from the per-bio
117 * 'struct resync_pages'.
118 */
119static inline struct r10bio *get_resync_r10bio(struct bio *bio)
120{
121 return get_resync_pages(bio)->raid_bio;
122}
123
Al Virodd0fc662005-10-07 07:46:04 +0100124static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
NeilBrowne879a872011-10-11 16:49:02 +1100126 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100127 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
NeilBrown69335ef2011-12-23 10:17:54 +1100129 /* allocate a r10bio with room for raid_disks entries in the
130 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100131 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static void r10bio_pool_free(void *r10_bio, void *data)
135{
136 kfree(r10_bio);
137}
138
NeilBrown0310fa22008-08-05 15:54:14 +1000139/* amount of memory to reserve for resync requests */
140#define RESYNC_WINDOW (1024*1024)
141/* maximum number of concurrent requests, memory permitting */
142#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/*
145 * When performing a resync, we need to read and compare, so
146 * we need as many pages are there are copies.
147 * When performing a recovery, we need 2 bios, one for read,
148 * one for write (we recover only one drive per r10buf)
149 *
150 */
Al Virodd0fc662005-10-07 07:46:04 +0100151static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
NeilBrowne879a872011-10-11 16:49:02 +1100153 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100154 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct bio *bio;
Ming Leif0250612017-03-17 00:12:33 +0800156 int j;
157 int nalloc, nalloc_rp;
158 struct resync_pages *rps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100161 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
NeilBrown3ea7daa2012-05-22 13:53:47 +1000164 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
165 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 nalloc = conf->copies; /* resync */
167 else
168 nalloc = 2; /* recovery */
169
Ming Leif0250612017-03-17 00:12:33 +0800170 /* allocate once for all bios */
171 if (!conf->have_replacement)
172 nalloc_rp = nalloc;
173 else
174 nalloc_rp = nalloc * 2;
175 rps = kmalloc(sizeof(struct resync_pages) * nalloc_rp, gfp_flags);
176 if (!rps)
177 goto out_free_r10bio;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Allocate bios.
181 */
182 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100183 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!bio)
185 goto out_free_bio;
186 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100187 if (!conf->have_replacement)
188 continue;
189 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
190 if (!bio)
191 goto out_free_bio;
192 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194 /*
195 * Allocate RESYNC_PAGES data pages and attach them
196 * where needed.
197 */
Ming Leif0250612017-03-17 00:12:33 +0800198 for (j = 0; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100199 struct bio *rbio = r10_bio->devs[j].repl_bio;
Ming Leif0250612017-03-17 00:12:33 +0800200 struct resync_pages *rp, *rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Ming Leif0250612017-03-17 00:12:33 +0800202 rp = &rps[j];
203 if (rbio)
204 rp_repl = &rps[nalloc + j];
205
206 bio = r10_bio->devs[j].bio;
207
208 if (!j || test_bit(MD_RECOVERY_SYNC,
209 &conf->mddev->recovery)) {
210 if (resync_alloc_pages(rp, gfp_flags))
211 goto out_free_pages;
212 } else {
213 memcpy(rp, &rps[0], sizeof(*rp));
214 resync_get_all_pages(rp);
215 }
216
Ming Leif0250612017-03-17 00:12:33 +0800217 rp->raid_bio = r10_bio;
218 bio->bi_private = rp;
219 if (rbio) {
220 memcpy(rp_repl, rp, sizeof(*rp));
221 rbio->bi_private = rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224
225 return r10_bio;
226
227out_free_pages:
Ming Leif0250612017-03-17 00:12:33 +0800228 while (--j >= 0)
229 resync_free_pages(&rps[j * 2]);
230
majianpeng5fdd2cf2012-05-22 13:55:03 +1000231 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000233 for ( ; j < nalloc; j++) {
234 if (r10_bio->devs[j].bio)
235 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100236 if (r10_bio->devs[j].repl_bio)
237 bio_put(r10_bio->devs[j].repl_bio);
238 }
Ming Leif0250612017-03-17 00:12:33 +0800239 kfree(rps);
240out_free_r10bio:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 r10bio_pool_free(r10_bio, conf);
242 return NULL;
243}
244
245static void r10buf_pool_free(void *__r10_bio, void *data)
246{
NeilBrowne879a872011-10-11 16:49:02 +1100247 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100248 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int j;
Ming Leif0250612017-03-17 00:12:33 +0800250 struct resync_pages *rp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Ming Leif0250612017-03-17 00:12:33 +0800252 for (j = conf->copies; j--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct bio *bio = r10bio->devs[j].bio;
Ming Leif0250612017-03-17 00:12:33 +0800254
255 rp = get_resync_pages(bio);
256 resync_free_pages(rp);
257 bio_put(bio);
258
NeilBrown69335ef2011-12-23 10:17:54 +1100259 bio = r10bio->devs[j].repl_bio;
260 if (bio)
261 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Ming Leif0250612017-03-17 00:12:33 +0800263
264 /* resync pages array stored in the 1st bio's .bi_private */
265 kfree(rp);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 r10bio_pool_free(r10bio, conf);
268}
269
NeilBrowne879a872011-10-11 16:49:02 +1100270static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int i;
273
274 for (i = 0; i < conf->copies; i++) {
275 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000276 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 bio_put(*bio);
278 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100279 bio = &r10_bio->devs[i].repl_bio;
280 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
281 bio_put(*bio);
282 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284}
285
NeilBrown9f2c9d12011-10-11 16:48:43 +1100286static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
NeilBrowne879a872011-10-11 16:49:02 +1100288 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 put_all_bios(conf, r10_bio);
291 mempool_free(r10_bio, conf->r10bio_pool);
292}
293
NeilBrown9f2c9d12011-10-11 16:48:43 +1100294static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
NeilBrowne879a872011-10-11 16:49:02 +1100296 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 mempool_free(r10_bio, conf->r10buf_pool);
299
NeilBrown0a27ec92006-01-06 00:20:13 -0800300 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
NeilBrown9f2c9d12011-10-11 16:48:43 +1100303static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100306 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100307 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 spin_lock_irqsave(&conf->device_lock, flags);
310 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800311 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock_irqrestore(&conf->device_lock, flags);
313
Arthur Jones388667b2008-07-25 12:03:38 -0700314 /* wake up frozen array... */
315 wake_up(&conf->wait_barrier);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 md_wakeup_thread(mddev->thread);
318}
319
320/*
321 * raid_end_bio_io() is called when we have finished servicing a mirrored
322 * operation and are ready to return a success/failure code to the buffer
323 * cache layer.
324 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100325static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100328 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
NeilBrown856e08e2011-07-28 11:39:23 +1000330 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200331 bio->bi_status = BLK_STS_IOERR;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100332
333 bio_endio(bio);
334 /*
335 * Wake up any possible resync thread that waits for the device
336 * to go idle.
337 */
338 allow_barrier(conf);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 free_r10bio(r10_bio);
341}
342
343/*
344 * Update disk head position estimator based on IRQ completion info.
345 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100346static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
NeilBrowne879a872011-10-11 16:49:02 +1100348 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
351 r10_bio->devs[slot].addr + (r10_bio->sectors);
352}
353
Namhyung Kim778ca012011-07-18 17:38:47 +1000354/*
355 * Find the disk number which triggered given bio
356 */
NeilBrowne879a872011-10-11 16:49:02 +1100357static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100358 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000359{
360 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100361 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000362
NeilBrown69335ef2011-12-23 10:17:54 +1100363 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000364 if (r10_bio->devs[slot].bio == bio)
365 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100366 if (r10_bio->devs[slot].repl_bio == bio) {
367 repl = 1;
368 break;
369 }
370 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000371
372 BUG_ON(slot == conf->copies);
373 update_head_pos(slot, r10_bio);
374
NeilBrown749c55e2011-07-28 11:39:24 +1000375 if (slotp)
376 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100377 if (replp)
378 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000379 return r10_bio->devs[slot].devnum;
380}
381
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200382static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200384 int uptodate = !bio->bi_status;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100385 struct r10bio *r10_bio = bio->bi_private;
Colin Ian Kinga0e764c2017-10-11 11:46:54 +0100386 int slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100387 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100388 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 slot = r10_bio->read_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100391 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
393 * this branch is our 'one mirror IO has finished' event handler:
394 */
NeilBrown4443ae12006-01-06 00:20:28 -0800395 update_head_pos(slot, r10_bio);
396
397 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
399 * Set R10BIO_Uptodate in our master bio, so that
400 * we will return a good error code to the higher
401 * levels even if IO on some other mirrored buffer fails.
402 *
403 * The 'master' represents the composite IO operation to
404 * user-side. So if something waits for IO, then it will
405 * wait for the 'master' bio.
406 */
407 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100408 } else {
409 /* If all other devices that store this block have
410 * failed, we want to return the error upwards rather
411 * than fail the last device. Here we redefine
412 * "uptodate" to mean "Don't want to retry"
413 */
NeilBrown635f6412013-06-11 14:57:09 +1000414 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
415 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100416 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100417 }
418 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100420 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800421 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000423 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
425 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100426 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000427 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100428 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000429 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000430 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 reschedule_retry(r10_bio);
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
NeilBrown9f2c9d12011-10-11 16:48:43 +1100435static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000436{
437 /* clear the bitmap if all writes complete successfully */
438 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
439 r10_bio->sectors,
440 !test_bit(R10BIO_Degraded, &r10_bio->state),
441 0);
442 md_write_end(r10_bio->mddev);
443}
444
NeilBrown9f2c9d12011-10-11 16:48:43 +1100445static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000446{
447 if (atomic_dec_and_test(&r10_bio->remaining)) {
448 if (test_bit(R10BIO_WriteError, &r10_bio->state))
449 reschedule_retry(r10_bio);
450 else {
451 close_write(r10_bio);
452 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
453 reschedule_retry(r10_bio);
454 else
455 raid_end_bio_io(r10_bio);
456 }
457 }
458}
459
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200460static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100462 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000463 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000464 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100465 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100466 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100467 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100468 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700469 bool discard_error;
470
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200471 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
NeilBrown475b0322011-12-23 10:17:55 +1100473 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
NeilBrown475b0322011-12-23 10:17:55 +1100475 if (repl)
476 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100477 if (!rdev) {
478 smp_rmb();
479 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100480 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /*
483 * this branch is our 'one mirror IO has finished' event handler:
484 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200485 if (bio->bi_status && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100486 if (repl)
487 /* Never record new bad blocks to replacement,
488 * just fail it.
489 */
490 md_error(rdev->mddev, rdev);
491 else {
492 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100493 if (!test_and_set_bit(WantReplacement, &rdev->flags))
494 set_bit(MD_RECOVERY_NEEDED,
495 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100496
NeilBrown475b0322011-12-23 10:17:55 +1100497 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100498 if (test_bit(FailFast, &rdev->flags) &&
499 (bio->bi_opf & MD_FAILFAST)) {
500 md_error(rdev->mddev, rdev);
501 if (!test_bit(Faulty, &rdev->flags))
502 /* This is the only remaining device,
503 * We need to retry the write without
504 * FailFast
505 */
506 set_bit(R10BIO_WriteError, &r10_bio->state);
507 else {
508 r10_bio->devs[slot].bio = NULL;
509 to_put = bio;
510 dec_rdev = 1;
511 }
512 } else
513 set_bit(R10BIO_WriteError, &r10_bio->state);
NeilBrown475b0322011-12-23 10:17:55 +1100514 }
NeilBrown749c55e2011-07-28 11:39:24 +1000515 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * Set R10BIO_Uptodate in our master bio, so that
518 * we will return a good error code for to the higher
519 * levels even if IO on some other mirrored buffer fails.
520 *
521 * The 'master' represents the composite IO operation to
522 * user-side. So if something waits for IO, then it will
523 * wait for the 'master' bio.
524 */
NeilBrown749c55e2011-07-28 11:39:24 +1000525 sector_t first_bad;
526 int bad_sectors;
527
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300528 /*
529 * Do not set R10BIO_Uptodate if the current device is
530 * rebuilding or Faulty. This is because we cannot use
531 * such device for properly reading the data back (we could
532 * potentially use it, if the current write would have felt
533 * before rdev->recovery_offset, but for simplicity we don't
534 * check this here.
535 */
536 if (test_bit(In_sync, &rdev->flags) &&
537 !test_bit(Faulty, &rdev->flags))
538 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
NeilBrown749c55e2011-07-28 11:39:24 +1000540 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100541 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000542 r10_bio->devs[slot].addr,
543 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700544 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000545 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100546 if (repl)
547 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
548 else
549 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000550 dec_rdev = 0;
551 set_bit(R10BIO_MadeGood, &r10_bio->state);
552 }
553 }
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 *
557 * Let's see if all mirrored write operations have finished
558 * already.
559 */
NeilBrown19d5f832011-09-10 17:21:17 +1000560 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000561 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100562 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100563 if (to_put)
564 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
568 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300569 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * parameters: near_copies and far_copies.
571 * near_copies * far_copies must be <= raid_disks.
572 * Normally one of these will be 1.
573 * If both are 1, we get raid0.
574 * If near_copies == raid_disks, we get raid1.
575 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300576 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * first chunk, followed by near_copies copies of the next chunk and
578 * so on.
579 * If far_copies > 1, then after 1/far_copies of the array has been assigned
580 * as described above, we start again with a device offset of near_copies.
581 * So we effectively have another copy of the whole array further down all
582 * the drives, but with blocks on different drives.
583 * With this layout, and block is never stored twice on the one device.
584 *
585 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700586 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 *
588 * raid10_find_virt does the reverse mapping, from a device and a
589 * sector offset to a virtual address
590 */
591
NeilBrownf8c9e742012-05-21 09:28:33 +1000592static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 int n,f;
595 sector_t sector;
596 sector_t chunk;
597 sector_t stripe;
598 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100600 int last_far_set_start, last_far_set_size;
601
602 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
603 last_far_set_start *= geo->far_set_size;
604
605 last_far_set_size = geo->far_set_size;
606 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000609 chunk = r10bio->sector >> geo->chunk_shift;
610 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
NeilBrown5cf00fc2012-05-21 09:28:20 +1000612 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000614 dev = sector_div(stripe, geo->raid_disks);
615 if (geo->far_offset)
616 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
NeilBrown5cf00fc2012-05-21 09:28:20 +1000618 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000621 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100623 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100626 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 slot++;
628
NeilBrown5cf00fc2012-05-21 09:28:20 +1000629 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100630 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000631 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100632
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100633 if ((geo->raid_disks % geo->far_set_size) &&
634 (d > last_far_set_start)) {
635 d -= last_far_set_start;
636 d %= last_far_set_size;
637 d += last_far_set_start;
638 } else {
639 d %= geo->far_set_size;
640 d += geo->far_set_size * set;
641 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000642 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 r10bio->devs[slot].devnum = d;
644 r10bio->devs[slot].addr = s;
645 slot++;
646 }
647 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000648 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000650 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000653}
654
655static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
656{
657 struct geom *geo = &conf->geo;
658
659 if (conf->reshape_progress != MaxSector &&
660 ((r10bio->sector >= conf->reshape_progress) !=
661 conf->mddev->reshape_backwards)) {
662 set_bit(R10BIO_Previous, &r10bio->state);
663 geo = &conf->prev;
664 } else
665 clear_bit(R10BIO_Previous, &r10bio->state);
666
667 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
NeilBrowne879a872011-10-11 16:49:02 +1100670static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000673 /* Never use conf->prev as this is only called during resync
674 * or recovery, so reshape isn't happening
675 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000676 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100677 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
678 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100679 int last_far_set_start;
680
681 if (geo->raid_disks % geo->far_set_size) {
682 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
683 last_far_set_start *= geo->far_set_size;
684
685 if (dev >= last_far_set_start) {
686 far_set_size = geo->far_set_size;
687 far_set_size += (geo->raid_disks % geo->far_set_size);
688 far_set_start = last_far_set_start;
689 }
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
NeilBrown5cf00fc2012-05-21 09:28:20 +1000692 offset = sector & geo->chunk_mask;
693 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700694 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000695 chunk = sector >> geo->chunk_shift;
696 fc = sector_div(chunk, geo->far_copies);
697 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100698 if (dev < far_set_start)
699 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700700 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000701 while (sector >= geo->stride) {
702 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100703 if (dev < (geo->near_copies + far_set_start))
704 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700705 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000706 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700707 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000708 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700709 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000710 vchunk = chunk * geo->raid_disks + dev;
711 sector_div(vchunk, geo->near_copies);
712 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * This routine returns the disk from which the requested read should
717 * be done. There is a per-array 'next expected sequential IO' sector
718 * number - if this matches on the next IO then we use the last disk.
719 * There is also a per-disk 'last know head position' sector that is
720 * maintained from IRQ contexts, both the normal and the resync IO
721 * completion handlers update this position correctly. If there is no
722 * perfect sequential match then we pick the disk whose head is closest.
723 *
724 * If there are 2 mirrors in the same 2 devices, performance degrades
725 * because position is mirror, not device based.
726 *
727 * The rdev for the device selected will have nr_pending incremented.
728 */
729
730/*
731 * FIXME: possibly should rethink readbalancing and do it differently
732 * depending on near_copies / far_copies geometry.
733 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100734static struct md_rdev *read_balance(struct r10conf *conf,
735 struct r10bio *r10_bio,
736 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000738 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000739 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000740 int sectors = r10_bio->sectors;
741 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000742 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000743 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000744 int do_balance;
745 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000746 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 raid10_find_phys(conf, r10_bio);
749 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000750 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100751 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000752 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000753 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000754 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100755 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800758 * device if no resync is going on (recovery is ok), or below
759 * the resync window. We take the first readable disk when
760 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
762 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000763 && (this_sector + sectors >= conf->next_resync))
764 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
NeilBrown56d99122011-05-11 14:27:03 +1000766 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000767 sector_t first_bad;
768 int bad_sectors;
769 sector_t dev_sector;
770
NeilBrown56d99122011-05-11 14:27:03 +1000771 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000773 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100774 rdev = rcu_dereference(conf->mirrors[disk].replacement);
775 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
776 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
777 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100778 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700779 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100780 continue;
781 if (!test_bit(In_sync, &rdev->flags) &&
782 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000783 continue;
784
NeilBrown856e08e2011-07-28 11:39:23 +1000785 dev_sector = r10_bio->devs[slot].addr;
786 if (is_badblock(rdev, dev_sector, sectors,
787 &first_bad, &bad_sectors)) {
788 if (best_dist < MaxSector)
789 /* Already have a better slot */
790 continue;
791 if (first_bad <= dev_sector) {
792 /* Cannot read here. If this is the
793 * 'primary' device, then we must not read
794 * beyond 'bad_sectors' from another device.
795 */
796 bad_sectors -= (dev_sector - first_bad);
797 if (!do_balance && sectors > bad_sectors)
798 sectors = bad_sectors;
799 if (best_good_sectors > sectors)
800 best_good_sectors = sectors;
801 } else {
802 sector_t good_sectors =
803 first_bad - dev_sector;
804 if (good_sectors > best_good_sectors) {
805 best_good_sectors = good_sectors;
806 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100807 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000808 }
809 if (!do_balance)
810 /* Must read from here */
811 break;
812 }
813 continue;
814 } else
815 best_good_sectors = sectors;
816
NeilBrown56d99122011-05-11 14:27:03 +1000817 if (!do_balance)
818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
NeilBrown8d3ca832016-11-18 16:16:12 +1100820 if (best_slot >= 0)
821 /* At least 2 disks to choose from so failfast is OK */
822 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800823 /* This optimisation is debatable, and completely destroys
824 * sequential read speed for 'far copies' arrays. So only
825 * keep it for 'near' arrays, and review those later.
826 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000827 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
NeilBrown8d3ca832016-11-18 16:16:12 +1100828 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800829
830 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100831 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000832 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800833 else
NeilBrown56d99122011-05-11 14:27:03 +1000834 new_distance = abs(r10_bio->devs[slot].addr -
835 conf->mirrors[disk].head_position);
836 if (new_distance < best_dist) {
837 best_dist = new_distance;
838 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100839 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 }
NeilBrownabbf0982011-12-23 10:17:54 +1100842 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000843 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100844 rdev = best_rdev;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
NeilBrown56d99122011-05-11 14:27:03 +1000847 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000848 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000849 r10_bio->read_slot = slot;
850 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100851 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000853 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
NeilBrown96c3fd12011-12-23 10:17:54 +1100855 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
NeilBrown5c675f82014-12-15 12:56:56 +1100858static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700859{
NeilBrowne879a872011-10-11 16:49:02 +1100860 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700861 int i, ret = 0;
862
Tejun Heo44522262015-05-22 17:13:26 -0400863 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100864 conf->pending_count >= max_queued_requests)
865 return 1;
866
NeilBrown0d129222006-10-03 01:15:54 -0700867 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000868 for (i = 0;
869 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
870 && ret == 0;
871 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100872 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700873 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200874 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700875
Jan Karadc3b17c2017-02-02 15:56:50 +0100876 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700877 }
878 }
879 rcu_read_unlock();
880 return ret;
881}
882
NeilBrowne879a872011-10-11 16:49:02 +1100883static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800884{
885 /* Any writes that have been queued but are awaiting
886 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800887 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800888 spin_lock_irq(&conf->device_lock);
889
890 if (conf->pending_bio_list.head) {
891 struct bio *bio;
892 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100893 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800894 spin_unlock_irq(&conf->device_lock);
895 /* flush any pending bitmap writes to disk
896 * before proceeding w/ I/O */
897 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100898 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800899
900 while (bio) { /* submit pending writes */
901 struct bio *next = bio->bi_next;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200902 struct md_rdev *rdev = (void*)bio->bi_disk;
NeilBrowna35e63e2008-03-04 14:29:29 -0800903 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200904 bio_set_dev(bio, rdev->bdev);
NeilBrowna9ae93c2016-11-04 16:46:03 +1100905 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +0800906 bio_io_error(bio);
NeilBrowna9ae93c2016-11-04 16:46:03 +1100907 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig74d46992017-08-23 19:10:32 +0200908 !blk_queue_discard(bio->bi_disk->queue)))
Shaohua Li532a2a32012-10-11 13:30:52 +1100909 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200910 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100911 else
912 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800913 bio = next;
914 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800915 } else
916 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800917}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100918
NeilBrown0a27ec92006-01-06 00:20:13 -0800919/* Barriers....
920 * Sometimes we need to suspend IO while we do something else,
921 * either some resync/recovery, or reconfigure the array.
922 * To do this we raise a 'barrier'.
923 * The 'barrier' is a counter that can be raised multiple times
924 * to count how many activities are happening which preclude
925 * normal IO.
926 * We can only raise the barrier if there is no pending IO.
927 * i.e. if nr_pending == 0.
928 * We choose only to raise the barrier if no-one is waiting for the
929 * barrier to go down. This means that as soon as an IO request
930 * is ready, no other operations which require a barrier will start
931 * until the IO request has had a chance.
932 *
933 * So: regular IO calls 'wait_barrier'. When that returns there
934 * is no backgroup IO happening, It must arrange to call
935 * allow_barrier when it has finished its IO.
936 * backgroup IO calls must call raise_barrier. Once that returns
937 * there is no normal IO happeing. It must arrange to call
938 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
NeilBrowne879a872011-10-11 16:49:02 +1100941static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
NeilBrown6cce3b22006-01-06 00:20:16 -0800943 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
NeilBrown6cce3b22006-01-06 00:20:16 -0800946 /* Wait until no block IO is waiting (unless 'force') */
947 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100948 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800949
950 /* block any new IO from starting */
951 conf->barrier++;
952
NeilBrownc3b328a2011-04-18 18:25:43 +1000953 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800954 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200955 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100956 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 spin_unlock_irq(&conf->resync_lock);
959}
960
NeilBrowne879a872011-10-11 16:49:02 +1100961static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800962{
963 unsigned long flags;
964 spin_lock_irqsave(&conf->resync_lock, flags);
965 conf->barrier--;
966 spin_unlock_irqrestore(&conf->resync_lock, flags);
967 wake_up(&conf->wait_barrier);
968}
969
NeilBrowne879a872011-10-11 16:49:02 +1100970static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800971{
972 spin_lock_irq(&conf->resync_lock);
973 if (conf->barrier) {
974 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100975 /* Wait for the barrier to drop.
976 * However if there are already pending
977 * requests (preventing the barrier from
978 * rising completely), and the
979 * pre-process bio queue isn't empty,
980 * then don't wait, as we need to empty
981 * that queue to get the nr_pending
982 * count down.
983 */
NeilBrown578b54a2016-11-14 16:30:21 +1100984 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100985 wait_event_lock_irq(conf->wait_barrier,
986 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200987 (atomic_read(&conf->nr_pending) &&
NeilBrownd6b42dc2012-03-19 12:46:38 +1100988 current->bio_list &&
NeilBrownf5fe1b52017-03-10 17:00:47 +1100989 (!bio_list_empty(&current->bio_list[0]) ||
990 !bio_list_empty(&current->bio_list[1]))),
Lukas Czernereed8c022012-11-30 11:42:40 +0100991 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800992 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200993 if (!conf->nr_waiting)
994 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800995 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200996 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800997 spin_unlock_irq(&conf->resync_lock);
998}
999
NeilBrowne879a872011-10-11 16:49:02 +11001000static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001001{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001002 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1003 (conf->array_freeze_pending))
1004 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -08001005}
1006
NeilBrowne2d59922013-06-12 11:01:22 +10001007static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -08001008{
1009 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001010 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001011 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001012 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001013 * This is called in the context of one normal IO request
1014 * that has failed. Thus any sync request that might be pending
1015 * will be blocked by nr_pending, and we need to wait for
1016 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001017 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001018 * must match the number of pending IOs (nr_pending) before
1019 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001020 */
1021 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001022 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001023 conf->barrier++;
1024 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001025 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001026 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001027 conf->resync_lock,
1028 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001029
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001030 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001031 spin_unlock_irq(&conf->resync_lock);
1032}
1033
NeilBrowne879a872011-10-11 16:49:02 +11001034static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001035{
1036 /* reverse the effect of the freeze */
1037 spin_lock_irq(&conf->resync_lock);
1038 conf->barrier--;
1039 conf->nr_waiting--;
1040 wake_up(&conf->wait_barrier);
1041 spin_unlock_irq(&conf->resync_lock);
1042}
1043
NeilBrownf8c9e742012-05-21 09:28:33 +10001044static sector_t choose_data_offset(struct r10bio *r10_bio,
1045 struct md_rdev *rdev)
1046{
1047 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1048 test_bit(R10BIO_Previous, &r10_bio->state))
1049 return rdev->data_offset;
1050 else
1051 return rdev->new_data_offset;
1052}
1053
NeilBrown57c67df2012-10-11 13:32:13 +11001054struct raid10_plug_cb {
1055 struct blk_plug_cb cb;
1056 struct bio_list pending;
1057 int pending_cnt;
1058};
1059
1060static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1061{
1062 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1063 cb);
1064 struct mddev *mddev = plug->cb.data;
1065 struct r10conf *conf = mddev->private;
1066 struct bio *bio;
1067
NeilBrown874807a2012-11-27 12:14:40 +11001068 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001069 spin_lock_irq(&conf->device_lock);
1070 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1071 conf->pending_count += plug->pending_cnt;
1072 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001073 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001074 md_wakeup_thread(mddev->thread);
1075 kfree(plug);
1076 return;
1077 }
1078
1079 /* we aren't scheduling, so we can do the write-out directly. */
1080 bio = bio_list_get(&plug->pending);
1081 bitmap_unplug(mddev->bitmap);
1082 wake_up(&conf->wait_barrier);
1083
1084 while (bio) { /* submit pending writes */
1085 struct bio *next = bio->bi_next;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001086 struct md_rdev *rdev = (void*)bio->bi_disk;
NeilBrown57c67df2012-10-11 13:32:13 +11001087 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001088 bio_set_dev(bio, rdev->bdev);
NeilBrowna9ae93c2016-11-04 16:46:03 +11001089 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08001090 bio_io_error(bio);
NeilBrowna9ae93c2016-11-04 16:46:03 +11001091 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig74d46992017-08-23 19:10:32 +02001092 !blk_queue_discard(bio->bi_disk->queue)))
Shaohua Li32f9f572013-04-28 18:26:38 +08001093 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001094 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001095 else
1096 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001097 bio = next;
1098 }
1099 kfree(plug);
1100}
1101
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001102static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1103 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
NeilBrowne879a872011-10-11 16:49:02 +11001105 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001107 const int op = bio_op(bio);
1108 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001109 int max_sectors;
1110 sector_t sectors;
1111 struct md_rdev *rdev;
NeilBrown545250f2017-04-05 14:05:51 +10001112 char b[BDEVNAME_SIZE];
1113 int slot = r10_bio->read_slot;
1114 struct md_rdev *err_rdev = NULL;
1115 gfp_t gfp = GFP_NOIO;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001116
NeilBrown545250f2017-04-05 14:05:51 +10001117 if (r10_bio->devs[slot].rdev) {
1118 /*
1119 * This is an error retry, but we cannot
1120 * safely dereference the rdev in the r10_bio,
1121 * we must use the one in conf.
1122 * If it has already been disconnected (unlikely)
1123 * we lose the device name in error messages.
1124 */
1125 int disk;
1126 /*
1127 * As we are blocking raid10, it is a little safer to
1128 * use __GFP_HIGH.
1129 */
1130 gfp = GFP_NOIO | __GFP_HIGH;
1131
1132 rcu_read_lock();
1133 disk = r10_bio->devs[slot].devnum;
1134 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1135 if (err_rdev)
1136 bdevname(err_rdev->bdev, b);
1137 else {
1138 strcpy(b, "???");
1139 /* This never gets dereferenced */
1140 err_rdev = r10_bio->devs[slot].rdev;
1141 }
1142 rcu_read_unlock();
1143 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001144 /*
1145 * Register the new request and wait if the reconstruction
1146 * thread has put up a bar for new requests.
1147 * Continue immediately if no resync is active currently.
1148 */
1149 wait_barrier(conf);
1150
NeilBrownfc9977d2017-04-05 14:05:51 +10001151 sectors = r10_bio->sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001152 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1153 bio->bi_iter.bi_sector < conf->reshape_progress &&
1154 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1155 /*
1156 * IO spans the reshape position. Need to wait for reshape to
1157 * pass
1158 */
1159 raid10_log(conf->mddev, "wait reshape");
1160 allow_barrier(conf);
1161 wait_event(conf->wait_barrier,
1162 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1163 conf->reshape_progress >= bio->bi_iter.bi_sector +
1164 sectors);
1165 wait_barrier(conf);
1166 }
1167
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001168 rdev = read_balance(conf, r10_bio, &max_sectors);
1169 if (!rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001170 if (err_rdev) {
1171 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1172 mdname(mddev), b,
1173 (unsigned long long)r10_bio->sector);
1174 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001175 raid_end_bio_io(r10_bio);
1176 return;
1177 }
NeilBrown545250f2017-04-05 14:05:51 +10001178 if (err_rdev)
1179 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1180 mdname(mddev),
1181 bdevname(rdev->bdev, b),
1182 (unsigned long long)r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001183 if (max_sectors < bio_sectors(bio)) {
1184 struct bio *split = bio_split(bio, max_sectors,
NeilBrown545250f2017-04-05 14:05:51 +10001185 gfp, conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001186 bio_chain(split, bio);
1187 generic_make_request(bio);
1188 bio = split;
1189 r10_bio->master_bio = bio;
1190 r10_bio->sectors = max_sectors;
1191 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001192 slot = r10_bio->read_slot;
1193
NeilBrown545250f2017-04-05 14:05:51 +10001194 read_bio = bio_clone_fast(bio, gfp, mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001195
1196 r10_bio->devs[slot].bio = read_bio;
1197 r10_bio->devs[slot].rdev = rdev;
1198
1199 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1200 choose_data_offset(r10_bio, rdev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001201 bio_set_dev(read_bio, rdev->bdev);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001202 read_bio->bi_end_io = raid10_end_read_request;
1203 bio_set_op_attrs(read_bio, op, do_sync);
1204 if (test_bit(FailFast, &rdev->flags) &&
1205 test_bit(R10BIO_FailFast, &r10_bio->state))
1206 read_bio->bi_opf |= MD_FAILFAST;
1207 read_bio->bi_private = r10_bio;
1208
1209 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001210 trace_block_bio_remap(read_bio->bi_disk->queue,
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001211 read_bio, disk_devt(mddev->gendisk),
1212 r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001213 generic_make_request(read_bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001214 return;
1215}
1216
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001217static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1218 struct bio *bio, bool replacement,
NeilBrownfc9977d2017-04-05 14:05:51 +10001219 int n_copy)
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001220{
1221 const int op = bio_op(bio);
1222 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1223 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1224 unsigned long flags;
1225 struct blk_plug_cb *cb;
1226 struct raid10_plug_cb *plug = NULL;
1227 struct r10conf *conf = mddev->private;
1228 struct md_rdev *rdev;
1229 int devnum = r10_bio->devs[n_copy].devnum;
1230 struct bio *mbio;
1231
1232 if (replacement) {
1233 rdev = conf->mirrors[devnum].replacement;
1234 if (rdev == NULL) {
1235 /* Replacement just got moved to main 'rdev' */
1236 smp_mb();
1237 rdev = conf->mirrors[devnum].rdev;
1238 }
1239 } else
1240 rdev = conf->mirrors[devnum].rdev;
1241
1242 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001243 if (replacement)
1244 r10_bio->devs[n_copy].repl_bio = mbio;
1245 else
1246 r10_bio->devs[n_copy].bio = mbio;
1247
1248 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1249 choose_data_offset(r10_bio, rdev));
Christoph Hellwig74d46992017-08-23 19:10:32 +02001250 bio_set_dev(mbio, rdev->bdev);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001251 mbio->bi_end_io = raid10_end_write_request;
1252 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1253 if (!replacement && test_bit(FailFast,
1254 &conf->mirrors[devnum].rdev->flags)
1255 && enough(conf, devnum))
1256 mbio->bi_opf |= MD_FAILFAST;
1257 mbio->bi_private = r10_bio;
1258
1259 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001260 trace_block_bio_remap(mbio->bi_disk->queue,
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001261 mbio, disk_devt(conf->mddev->gendisk),
1262 r10_bio->sector);
1263 /* flush_pending_writes() needs access to the rdev so...*/
Christoph Hellwig74d46992017-08-23 19:10:32 +02001264 mbio->bi_disk = (void *)rdev;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001265
1266 atomic_inc(&r10_bio->remaining);
1267
1268 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1269 if (cb)
1270 plug = container_of(cb, struct raid10_plug_cb, cb);
1271 else
1272 plug = NULL;
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001273 if (plug) {
1274 bio_list_add(&plug->pending, mbio);
1275 plug->pending_cnt++;
1276 } else {
Shaohua Li23b245c2017-05-10 08:47:11 -07001277 spin_lock_irqsave(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001278 bio_list_add(&conf->pending_bio_list, mbio);
1279 conf->pending_count++;
Shaohua Li23b245c2017-05-10 08:47:11 -07001280 spin_unlock_irqrestore(&conf->device_lock, flags);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001281 md_wakeup_thread(mddev->thread);
Shaohua Li23b245c2017-05-10 08:47:11 -07001282 }
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001283}
1284
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001285static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1286 struct r10bio *r10_bio)
1287{
1288 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11001290 struct md_rdev *blocked_rdev;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001291 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001292 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
NeilBrowncc13b1d2014-05-05 13:34:37 +10001294 /*
1295 * Register the new request and wait if the reconstruction
1296 * thread has put up a bar for new requests.
1297 * Continue immediately if no resync is active currently.
1298 */
1299 wait_barrier(conf);
1300
NeilBrownfc9977d2017-04-05 14:05:51 +10001301 sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001302 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001303 bio->bi_iter.bi_sector < conf->reshape_progress &&
1304 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001305 /*
1306 * IO spans the reshape position. Need to wait for reshape to
1307 * pass
NeilBrown3ea7daa2012-05-22 13:53:47 +10001308 */
NeilBrown578b54a2016-11-14 16:30:21 +11001309 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001310 allow_barrier(conf);
1311 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001312 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1313 conf->reshape_progress >= bio->bi_iter.bi_sector +
1314 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001315 wait_barrier(conf);
1316 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001317
NeilBrown3ea7daa2012-05-22 13:53:47 +10001318 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001319 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001320 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1321 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1322 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1323 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001324 /* Need to update reshape_position in metadata */
1325 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001326 set_mask_bits(&mddev->sb_flags, 0,
1327 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001328 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001329 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001330 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001331 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001332
1333 conf->reshape_safe = mddev->reshape_position;
1334 }
1335
NeilBrown34db0cd2011-10-11 16:50:01 +11001336 if (conf->pending_count >= max_queued_requests) {
1337 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001338 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001339 wait_event(conf->wait_barrier,
1340 conf->pending_count < max_queued_requests);
1341 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001342 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * inc refcount on their rdev. Record them by setting
1344 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001345 * If there are known/acknowledged bad blocks on any device
1346 * on which we have seen a write error, we want to avoid
1347 * writing to those blocks. This potentially requires several
1348 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001349 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001351
NeilBrown69335ef2011-12-23 10:17:54 +11001352 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001354retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001355 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001357 max_sectors = r10_bio->sectors;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 for (i = 0; i < conf->copies; i++) {
1360 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001361 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001362 struct md_rdev *rrdev = rcu_dereference(
1363 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001364 if (rdev == rrdev)
1365 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001366 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1367 atomic_inc(&rdev->nr_pending);
1368 blocked_rdev = rdev;
1369 break;
1370 }
NeilBrown475b0322011-12-23 10:17:55 +11001371 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1372 atomic_inc(&rrdev->nr_pending);
1373 blocked_rdev = rrdev;
1374 break;
1375 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001376 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001377 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001378 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001379 rrdev = NULL;
1380
NeilBrownd4432c22011-07-28 11:39:24 +10001381 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001382 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001383
1384 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001385 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001386 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001387 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001388 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001389 sector_t first_bad;
1390 sector_t dev_sector = r10_bio->devs[i].addr;
1391 int bad_sectors;
1392 int is_bad;
1393
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001394 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001395 &first_bad, &bad_sectors);
1396 if (is_bad < 0) {
1397 /* Mustn't write here until the bad block
1398 * is acknowledged
1399 */
1400 atomic_inc(&rdev->nr_pending);
1401 set_bit(BlockedBadBlocks, &rdev->flags);
1402 blocked_rdev = rdev;
1403 break;
1404 }
1405 if (is_bad && first_bad <= dev_sector) {
1406 /* Cannot write here at all */
1407 bad_sectors -= (dev_sector - first_bad);
1408 if (bad_sectors < max_sectors)
1409 /* Mustn't write more than bad_sectors
1410 * to other devices yet
1411 */
1412 max_sectors = bad_sectors;
1413 /* We don't set R10BIO_Degraded as that
1414 * only applies if the disk is missing,
1415 * so it might be re-added, and we want to
1416 * know to recover this chunk.
1417 * In this case the device is here, and the
1418 * fact that this chunk is not in-sync is
1419 * recorded in the bad block log.
1420 */
1421 continue;
1422 }
1423 if (is_bad) {
1424 int good_sectors = first_bad - dev_sector;
1425 if (good_sectors < max_sectors)
1426 max_sectors = good_sectors;
1427 }
1428 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001429 if (rdev) {
1430 r10_bio->devs[i].bio = bio;
1431 atomic_inc(&rdev->nr_pending);
1432 }
NeilBrown475b0322011-12-23 10:17:55 +11001433 if (rrdev) {
1434 r10_bio->devs[i].repl_bio = bio;
1435 atomic_inc(&rrdev->nr_pending);
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 rcu_read_unlock();
1439
Dan Williams6bfe0b42008-04-30 00:52:32 -07001440 if (unlikely(blocked_rdev)) {
1441 /* Have to wait for this device to get unblocked, then retry */
1442 int j;
1443 int d;
1444
NeilBrown475b0322011-12-23 10:17:55 +11001445 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001446 if (r10_bio->devs[j].bio) {
1447 d = r10_bio->devs[j].devnum;
1448 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1449 }
NeilBrown475b0322011-12-23 10:17:55 +11001450 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001451 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001452 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001453 rdev = conf->mirrors[d].replacement;
1454 if (!rdev) {
1455 /* Race with remove_disk */
1456 smp_mb();
1457 rdev = conf->mirrors[d].rdev;
1458 }
1459 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001460 }
1461 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001462 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001463 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001464 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1465 wait_barrier(conf);
1466 goto retry_write;
1467 }
1468
NeilBrown6b6c8112017-03-15 14:05:13 +11001469 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001470 r10_bio->sectors = max_sectors;
NeilBrownfc9977d2017-04-05 14:05:51 +10001471
1472 if (r10_bio->sectors < bio_sectors(bio)) {
1473 struct bio *split = bio_split(bio, r10_bio->sectors,
1474 GFP_NOIO, conf->bio_split);
1475 bio_chain(split, bio);
1476 generic_make_request(bio);
1477 bio = split;
1478 r10_bio->master_bio = bio;
NeilBrownd4432c22011-07-28 11:39:24 +10001479 }
NeilBrownd4432c22011-07-28 11:39:24 +10001480
NeilBrown4e780642010-10-19 12:54:01 +11001481 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001482 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 for (i = 0; i < conf->copies; i++) {
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001485 if (r10_bio->devs[i].bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001486 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001487 if (r10_bio->devs[i].repl_bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001488 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
NeilBrownd4432c22011-07-28 11:39:24 +10001489 }
NeilBrown079fa162011-09-10 17:21:23 +10001490 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001491}
1492
NeilBrownfc9977d2017-04-05 14:05:51 +10001493static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001494{
1495 struct r10conf *conf = mddev->private;
1496 struct r10bio *r10_bio;
1497
1498 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1499
1500 r10_bio->master_bio = bio;
NeilBrownfc9977d2017-04-05 14:05:51 +10001501 r10_bio->sectors = sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001502
1503 r10_bio->mddev = mddev;
1504 r10_bio->sector = bio->bi_iter.bi_sector;
1505 r10_bio->state = 0;
NeilBrown545250f2017-04-05 14:05:51 +10001506 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001507
1508 if (bio_data_dir(bio) == READ)
1509 raid10_read_request(mddev, bio, r10_bio);
1510 else
1511 raid10_write_request(mddev, bio, r10_bio);
1512}
1513
NeilBrowncc27b0c2017-06-05 16:49:39 +10001514static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001515{
1516 struct r10conf *conf = mddev->private;
1517 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1518 int chunk_sects = chunk_mask + 1;
NeilBrownfc9977d2017-04-05 14:05:51 +10001519 int sectors = bio_sectors(bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001520
Jens Axboe1eff9d32016-08-05 15:35:16 -06001521 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001522 md_flush_request(mddev, bio);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001523 return true;
Kent Overstreet20d01892013-11-23 18:21:01 -08001524 }
1525
NeilBrowncc27b0c2017-06-05 16:49:39 +10001526 if (!md_write_start(mddev, bio))
1527 return false;
1528
NeilBrownfc9977d2017-04-05 14:05:51 +10001529 /*
1530 * If this request crosses a chunk boundary, we need to split
1531 * it.
1532 */
1533 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1534 sectors > chunk_sects
1535 && (conf->geo.near_copies < conf->geo.raid_disks
1536 || conf->prev.near_copies <
1537 conf->prev.raid_disks)))
1538 sectors = chunk_sects -
1539 (bio->bi_iter.bi_sector &
1540 (chunk_sects - 1));
1541 __make_request(mddev, bio, sectors);
NeilBrown079fa162011-09-10 17:21:23 +10001542
1543 /* In case raid10d snuck in to freeze_array */
1544 wake_up(&conf->wait_barrier);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001545 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Shaohua Li849674e2016-01-20 13:52:20 -08001548static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
NeilBrowne879a872011-10-11 16:49:02 +11001550 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int i;
1552
NeilBrown5cf00fc2012-05-21 09:28:20 +10001553 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001554 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001555 if (conf->geo.near_copies > 1)
1556 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1557 if (conf->geo.far_copies > 1) {
1558 if (conf->geo.far_offset)
1559 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001560 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001561 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001562 if (conf->geo.far_set_size != conf->geo.raid_disks)
1563 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001564 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001565 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1566 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001567 rcu_read_lock();
1568 for (i = 0; i < conf->geo.raid_disks; i++) {
1569 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1570 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1571 }
1572 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 seq_printf(seq, "]");
1574}
1575
NeilBrown700c7212011-07-27 11:00:36 +10001576/* check if there are enough drives for
1577 * every block to appear on atleast one.
1578 * Don't consider the device numbered 'ignore'
1579 * as we might be about to remove it.
1580 */
NeilBrown635f6412013-06-11 14:57:09 +10001581static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001582{
1583 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001584 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001585 int disks, ncopies;
1586 if (previous) {
1587 disks = conf->prev.raid_disks;
1588 ncopies = conf->prev.near_copies;
1589 } else {
1590 disks = conf->geo.raid_disks;
1591 ncopies = conf->geo.near_copies;
1592 }
NeilBrown700c7212011-07-27 11:00:36 +10001593
NeilBrown725d6e52013-06-11 15:08:03 +10001594 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001595 do {
1596 int n = conf->copies;
1597 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001598 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001599 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001600 struct md_rdev *rdev;
1601 if (this != ignore &&
1602 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1603 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001604 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001605 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001606 }
1607 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001608 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001609 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001610 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001611 has_enough = 1;
1612out:
1613 rcu_read_unlock();
1614 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001615}
1616
NeilBrownf8c9e742012-05-21 09:28:33 +10001617static int enough(struct r10conf *conf, int ignore)
1618{
NeilBrown635f6412013-06-11 14:57:09 +10001619 /* when calling 'enough', both 'prev' and 'geo' must
1620 * be stable.
1621 * This is ensured if ->reconfig_mutex or ->device_lock
1622 * is held.
1623 */
1624 return _enough(conf, 0, ignore) &&
1625 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001626}
1627
Shaohua Li849674e2016-01-20 13:52:20 -08001628static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001631 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001632 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /*
1635 * If it is not operational, then we have already marked it as dead
1636 * else if it is the last working disks, ignore the error, let the
1637 * next level up know.
1638 * else mark the drive as failed
1639 */
NeilBrown635f6412013-06-11 14:57:09 +10001640 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001641 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001642 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /*
1644 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001646 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001647 return;
1648 }
NeilBrown2446dba2014-07-31 10:16:29 +10001649 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001650 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001651 /*
1652 * If recovery is running, make sure it aborts.
1653 */
1654 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001655 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001656 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001657 set_mask_bits(&mddev->sb_flags, 0,
1658 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001659 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001660 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1661 "md/raid10:%s: Operation continuing on %d devices.\n",
1662 mdname(mddev), bdevname(rdev->bdev, b),
1663 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
NeilBrowne879a872011-10-11 16:49:02 +11001666static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001669 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
NeilBrown08464e02016-11-02 14:16:50 +11001671 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001673 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return;
1675 }
NeilBrown08464e02016-11-02 14:16:50 +11001676 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1677 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
NeilBrown4056ca52016-06-02 16:19:52 +10001679 /* This is only called with ->reconfix_mutex held, so
1680 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001681 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001683 rdev = conf->mirrors[i].rdev;
1684 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001685 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1686 i, !test_bit(In_sync, &rdev->flags),
1687 !test_bit(Faulty, &rdev->flags),
1688 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690}
1691
NeilBrowne879a872011-10-11 16:49:02 +11001692static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
NeilBrown0a27ec92006-01-06 00:20:13 -08001694 wait_barrier(conf);
1695 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 mempool_destroy(conf->r10buf_pool);
1698 conf->r10buf_pool = NULL;
1699}
1700
NeilBrownfd01b882011-10-11 16:47:53 +11001701static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001704 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001705 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001706 int count = 0;
1707 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 /*
1710 * Find all non-in_sync disks within the RAID10 configuration
1711 * and mark them in_sync
1712 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001713 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001715 if (tmp->replacement
1716 && tmp->replacement->recovery_offset == MaxSector
1717 && !test_bit(Faulty, &tmp->replacement->flags)
1718 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1719 /* Replacement has just become active */
1720 if (!tmp->rdev
1721 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1722 count++;
1723 if (tmp->rdev) {
1724 /* Replaced device not technically faulty,
1725 * but we need to be sure it gets removed
1726 * and never re-added.
1727 */
1728 set_bit(Faulty, &tmp->rdev->flags);
1729 sysfs_notify_dirent_safe(
1730 tmp->rdev->sysfs_state);
1731 }
1732 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1733 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001734 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001735 && !test_bit(Faulty, &tmp->rdev->flags)
1736 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001737 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001738 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740 }
NeilBrown6b965622010-08-18 11:56:59 +10001741 spin_lock_irqsave(&conf->device_lock, flags);
1742 mddev->degraded -= count;
1743 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001746 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
NeilBrownfd01b882011-10-11 16:47:53 +11001749static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
NeilBrowne879a872011-10-11 16:49:02 +11001751 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001752 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001754 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001755 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (mddev->recovery_cp < MaxSector)
1758 /* only hot-add to in-sync arrays, as recovery is
1759 * very different from resync
1760 */
Neil Brown199050e2008-06-28 08:31:33 +10001761 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001762 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Dan Williams1501efa2016-01-13 16:00:07 -08001765 if (md_integrity_add_rdev(rdev, mddev))
1766 return -ENXIO;
1767
NeilBrowna53a6c82008-11-06 17:28:20 +11001768 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001769 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001771 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001772 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1773 mirror = rdev->saved_raid_disk;
1774 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001775 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001776 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001777 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001778 if (p->recovery_disabled == mddev->recovery_disabled)
1779 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001780 if (p->rdev) {
1781 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1782 p->replacement != NULL)
1783 continue;
1784 clear_bit(In_sync, &rdev->flags);
1785 set_bit(Replacement, &rdev->flags);
1786 rdev->raid_disk = mirror;
1787 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001788 if (mddev->gendisk)
1789 disk_stack_limits(mddev->gendisk, rdev->bdev,
1790 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001791 conf->fullsync = 1;
1792 rcu_assign_pointer(p->replacement, rdev);
1793 break;
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jonathan Brassow9092c022013-05-02 14:19:24 -05001796 if (mddev->gendisk)
1797 disk_stack_limits(mddev->gendisk, rdev->bdev,
1798 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
NeilBrown2bb77732011-07-27 11:00:36 +10001800 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001801 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001802 rdev->raid_disk = mirror;
1803 err = 0;
1804 if (rdev->saved_raid_disk != mirror)
1805 conf->fullsync = 1;
1806 rcu_assign_pointer(p->rdev, rdev);
1807 break;
1808 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11001809 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001810 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001813 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
NeilBrownb8321b62011-12-23 10:17:51 +11001816static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
NeilBrowne879a872011-10-11 16:49:02 +11001818 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001820 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001821 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001822 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001825 if (rdev == p->rdev)
1826 rdevp = &p->rdev;
1827 else if (rdev == p->replacement)
1828 rdevp = &p->replacement;
1829 else
1830 return 0;
1831
1832 if (test_bit(In_sync, &rdev->flags) ||
1833 atomic_read(&rdev->nr_pending)) {
1834 err = -EBUSY;
1835 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
NeilBrownd787be42016-06-02 16:19:53 +10001837 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11001838 * is not possible.
1839 */
1840 if (!test_bit(Faulty, &rdev->flags) &&
1841 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001842 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001843 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001844 enough(conf, -1)) {
1845 err = -EBUSY;
1846 goto abort;
1847 }
1848 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001849 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1850 synchronize_rcu();
1851 if (atomic_read(&rdev->nr_pending)) {
1852 /* lost the race, try later */
1853 err = -EBUSY;
1854 *rdevp = rdev;
1855 goto abort;
1856 }
1857 }
1858 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001859 /* We must have just cleared 'rdev' */
1860 p->rdev = p->replacement;
1861 clear_bit(Replacement, &p->replacement->flags);
1862 smp_mb(); /* Make sure other CPUs may see both as identical
1863 * but will never see neither -- if they are careful.
1864 */
1865 p->replacement = NULL;
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08001866 }
NeilBrown4ca40c22011-12-23 10:17:55 +11001867
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08001868 clear_bit(WantReplacement, &rdev->flags);
NeilBrownc8ab9032011-12-23 10:17:54 +11001869 err = md_integrity_register(mddev);
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871abort:
1872
1873 print_conf(conf);
1874 return err;
1875}
1876
Ming Lei81fa1522017-03-17 00:12:32 +08001877static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
NeilBrowne879a872011-10-11 16:49:02 +11001879 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001880
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001881 if (!bio->bi_status)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001882 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001883 else
1884 /* The write handler will notice the lack of
1885 * R10BIO_Uptodate and record any errors etc
1886 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001887 atomic_add(r10_bio->sectors,
1888 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* for reconstruct, we always reschedule after a read.
1891 * for resync, only after all reads
1892 */
NeilBrown73d5c382009-02-25 13:18:47 +11001893 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1895 atomic_dec_and_test(&r10_bio->remaining)) {
1896 /* we have read all the blocks,
1897 * do the comparison in process context in raid10d
1898 */
1899 reschedule_retry(r10_bio);
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Ming Lei81fa1522017-03-17 00:12:32 +08001903static void end_sync_read(struct bio *bio)
1904{
Ming Leif0250612017-03-17 00:12:33 +08001905 struct r10bio *r10_bio = get_resync_r10bio(bio);
Ming Lei81fa1522017-03-17 00:12:32 +08001906 struct r10conf *conf = r10_bio->mddev->private;
1907 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1908
1909 __end_sync_read(r10_bio, bio, d);
1910}
1911
1912static void end_reshape_read(struct bio *bio)
1913{
Ming Leif0250612017-03-17 00:12:33 +08001914 /* reshape read bio isn't allocated from r10buf_pool */
Ming Lei81fa1522017-03-17 00:12:32 +08001915 struct r10bio *r10_bio = bio->bi_private;
1916
1917 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1918}
1919
NeilBrown9f2c9d12011-10-11 16:48:43 +11001920static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001921{
NeilBrownfd01b882011-10-11 16:47:53 +11001922 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001923
1924 while (atomic_dec_and_test(&r10_bio->remaining)) {
1925 if (r10_bio->master_bio == NULL) {
1926 /* the primary of several recovery bios */
1927 sector_t s = r10_bio->sectors;
1928 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1929 test_bit(R10BIO_WriteError, &r10_bio->state))
1930 reschedule_retry(r10_bio);
1931 else
1932 put_buf(r10_bio);
1933 md_done_sync(mddev, s, 1);
1934 break;
1935 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001936 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001937 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1938 test_bit(R10BIO_WriteError, &r10_bio->state))
1939 reschedule_retry(r10_bio);
1940 else
1941 put_buf(r10_bio);
1942 r10_bio = r10_bio2;
1943 }
1944 }
1945}
1946
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001947static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Ming Leif0250612017-03-17 00:12:33 +08001949 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrownfd01b882011-10-11 16:47:53 +11001950 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001951 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001952 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001953 sector_t first_bad;
1954 int bad_sectors;
1955 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001956 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001957 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
NeilBrown9ad1aef2011-12-23 10:17:55 +11001959 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1960 if (repl)
1961 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001962 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001963 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001965 if (bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001966 if (repl)
1967 md_error(mddev, rdev);
1968 else {
1969 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001970 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1971 set_bit(MD_RECOVERY_NEEDED,
1972 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001973 set_bit(R10BIO_WriteError, &r10_bio->state);
1974 }
1975 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001976 r10_bio->devs[slot].addr,
1977 r10_bio->sectors,
1978 &first_bad, &bad_sectors))
1979 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001980
NeilBrown9ad1aef2011-12-23 10:17:55 +11001981 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001982
1983 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
1986/*
1987 * Note: sync and recover and handled very differently for raid10
1988 * This code is for resync.
1989 * For resync, we read through virtual addresses and read all blocks.
1990 * If there is any error, we schedule a write. The lowest numbered
1991 * drive is authoritative.
1992 * However requests come for physical address, so we need to map.
1993 * For every physical address there are raid_disks/copies virtual addresses,
1994 * which is always are least one, but is not necessarly an integer.
1995 * This means that a physical address can span multiple chunks, so we may
1996 * have to submit multiple io requests for a single sync request.
1997 */
1998/*
1999 * We check if all blocks are in-sync and only write to blocks that
2000 * aren't in sync
2001 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002002static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
NeilBrowne879a872011-10-11 16:49:02 +11002004 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int i, first;
2006 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002007 int vcnt;
Ming Leicdb76be2017-03-17 00:12:34 +08002008 struct page **tpages, **fpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 atomic_set(&r10_bio->remaining, 1);
2011
2012 /* find the first device with a block */
2013 for (i=0; i<conf->copies; i++)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002014 if (!r10_bio->devs[i].bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 break;
2016
2017 if (i == conf->copies)
2018 goto done;
2019
2020 first = i;
2021 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002022 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2023 fbio->bi_iter.bi_idx = 0;
Ming Leicdb76be2017-03-17 00:12:34 +08002024 fpages = get_resync_pages(fbio)->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
majianpengf4380a92012-04-12 16:04:47 +10002026 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002028 for (i=0 ; i < conf->copies ; i++) {
2029 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002030 struct md_rdev *rdev;
Ming Leif0250612017-03-17 00:12:33 +08002031 struct resync_pages *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002034
2035 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002037 if (i == first)
2038 continue;
Ming Leicdb76be2017-03-17 00:12:34 +08002039
2040 tpages = get_resync_pages(tbio)->pages;
NeilBrown8d3ca832016-11-18 16:16:12 +11002041 d = r10_bio->devs[i].devnum;
2042 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002043 if (!r10_bio->devs[i].bio->bi_status) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002044 /* We know that the bi_io_vec layout is the same for
2045 * both 'first' and 'i', so we just compare them.
2046 * All vec entries are PAGE_SIZE;
2047 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002048 int sectors = r10_bio->sectors;
2049 for (j = 0; j < vcnt; j++) {
2050 int len = PAGE_SIZE;
2051 if (sectors < (len / 512))
2052 len = sectors * 512;
Ming Leicdb76be2017-03-17 00:12:34 +08002053 if (memcmp(page_address(fpages[j]),
2054 page_address(tpages[j]),
NeilBrown7bb23c42013-07-16 16:50:47 +10002055 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002056 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002057 sectors -= len/512;
2058 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002059 if (j == vcnt)
2060 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002061 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002062 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2063 /* Don't fix anything. */
2064 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002065 } else if (test_bit(FailFast, &rdev->flags)) {
2066 /* Just give up on this device */
2067 md_error(rdev->mddev, rdev);
2068 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002069 }
NeilBrownf84ee362011-07-28 11:39:25 +10002070 /* Ok, we need to write this bio, either to correct an
2071 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 * First we need to fixup bv_offset, bv_len and
2073 * bi_vecs, as the read request might have corrupted these
2074 */
Ming Leif0250612017-03-17 00:12:33 +08002075 rp = get_resync_pages(tbio);
Kent Overstreet8be185f2012-09-06 14:14:43 -07002076 bio_reset(tbio);
2077
Ming Leifb0eb5d2017-07-14 16:14:43 +08002078 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2079
Ming Leif0250612017-03-17 00:12:33 +08002080 rp->raid_bio = r10_bio;
2081 tbio->bi_private = rp;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002082 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002084 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Kent Overstreetc31df252015-05-06 23:34:20 -07002086 bio_copy_data(tbio, fbio);
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2089 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002090 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
NeilBrown1919cbb2016-11-18 16:16:12 +11002092 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2093 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002094 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002095 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 generic_make_request(tbio);
2097 }
2098
NeilBrown9ad1aef2011-12-23 10:17:55 +11002099 /* Now write out to any replacement devices
2100 * that are active
2101 */
2102 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002103 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002104
2105 tbio = r10_bio->devs[i].repl_bio;
2106 if (!tbio || !tbio->bi_end_io)
2107 continue;
2108 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2109 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002110 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002111 d = r10_bio->devs[i].devnum;
2112 atomic_inc(&r10_bio->remaining);
2113 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002114 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002115 generic_make_request(tbio);
2116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118done:
2119 if (atomic_dec_and_test(&r10_bio->remaining)) {
2120 md_done_sync(mddev, r10_bio->sectors, 1);
2121 put_buf(r10_bio);
2122 }
2123}
2124
2125/*
2126 * Now for the recovery code.
2127 * Recovery happens across physical sectors.
2128 * We recover all non-is_sync drives by finding the virtual address of
2129 * each, and then choose a working drive that also has that virt address.
2130 * There is a separate r10_bio for each non-in_sync drive.
2131 * Only the first two slots are in use. The first for reading,
2132 * The second for writing.
2133 *
2134 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002135static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002136{
2137 /* We got a read error during recovery.
2138 * We repeat the read in smaller page-sized sections.
2139 * If a read succeeds, write it to the new device or record
2140 * a bad block if we cannot.
2141 * If a read fails, record a bad block on both old and
2142 * new devices.
2143 */
NeilBrownfd01b882011-10-11 16:47:53 +11002144 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002145 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002146 struct bio *bio = r10_bio->devs[0].bio;
2147 sector_t sect = 0;
2148 int sectors = r10_bio->sectors;
2149 int idx = 0;
2150 int dr = r10_bio->devs[0].devnum;
2151 int dw = r10_bio->devs[1].devnum;
Ming Leicdb76be2017-03-17 00:12:34 +08002152 struct page **pages = get_resync_pages(bio)->pages;
NeilBrown5e570282011-07-28 11:39:25 +10002153
2154 while (sectors) {
2155 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002156 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002157 sector_t addr;
2158 int ok;
2159
2160 if (s > (PAGE_SIZE>>9))
2161 s = PAGE_SIZE >> 9;
2162
2163 rdev = conf->mirrors[dr].rdev;
2164 addr = r10_bio->devs[0].addr + sect,
2165 ok = sync_page_io(rdev,
2166 addr,
2167 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002168 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05002169 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002170 if (ok) {
2171 rdev = conf->mirrors[dw].rdev;
2172 addr = r10_bio->devs[1].addr + sect;
2173 ok = sync_page_io(rdev,
2174 addr,
2175 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002176 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05002177 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002178 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002179 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002180 if (!test_and_set_bit(WantReplacement,
2181 &rdev->flags))
2182 set_bit(MD_RECOVERY_NEEDED,
2183 &rdev->mddev->recovery);
2184 }
NeilBrown5e570282011-07-28 11:39:25 +10002185 }
2186 if (!ok) {
2187 /* We don't worry if we cannot set a bad block -
2188 * it really is bad so there is no loss in not
2189 * recording it yet
2190 */
2191 rdev_set_badblocks(rdev, addr, s, 0);
2192
2193 if (rdev != conf->mirrors[dw].rdev) {
2194 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002195 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002196 addr = r10_bio->devs[1].addr + sect;
2197 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2198 if (!ok) {
2199 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002200 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2201 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002202
2203 conf->mirrors[dw].recovery_disabled
2204 = mddev->recovery_disabled;
2205 set_bit(MD_RECOVERY_INTR,
2206 &mddev->recovery);
2207 break;
2208 }
2209 }
2210 }
2211
2212 sectors -= s;
2213 sect += s;
2214 idx++;
2215 }
2216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
NeilBrown9f2c9d12011-10-11 16:48:43 +11002218static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
NeilBrowne879a872011-10-11 16:49:02 +11002220 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002221 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002222 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
NeilBrown5e570282011-07-28 11:39:25 +10002224 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2225 fix_recovery_read_error(r10_bio);
2226 end_sync_request(r10_bio);
2227 return;
2228 }
2229
Namhyung Kimc65060a2011-07-18 17:38:49 +10002230 /*
2231 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * and submit the write request
2233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002235 wbio = r10_bio->devs[1].bio;
2236 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002237 /* Need to test wbio2->bi_end_io before we call
2238 * generic_make_request as if the former is NULL,
2239 * the latter is free to free wbio2.
2240 */
2241 if (wbio2 && !wbio2->bi_end_io)
2242 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002243 if (wbio->bi_end_io) {
2244 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002245 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002246 generic_make_request(wbio);
2247 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002248 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002249 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2250 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002251 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002252 generic_make_request(wbio2);
2253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256/*
Robert Becker1e509152009-12-14 12:49:58 +11002257 * Used by fix_read_error() to decay the per rdev read_errors.
2258 * We halve the read error count for every hour that has elapsed
2259 * since the last recorded read error.
2260 *
2261 */
NeilBrownfd01b882011-10-11 16:47:53 +11002262static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002263{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002264 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002265 unsigned long hours_since_last;
2266 unsigned int read_errors = atomic_read(&rdev->read_errors);
2267
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002268 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002269
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002270 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002271 /* first time we've seen a read error */
2272 rdev->last_read_error = cur_time_mon;
2273 return;
2274 }
2275
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002276 hours_since_last = (long)(cur_time_mon -
2277 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002278
2279 rdev->last_read_error = cur_time_mon;
2280
2281 /*
2282 * if hours_since_last is > the number of bits in read_errors
2283 * just set read errors to 0. We do this to avoid
2284 * overflowing the shift of read_errors by hours_since_last.
2285 */
2286 if (hours_since_last >= 8 * sizeof(read_errors))
2287 atomic_set(&rdev->read_errors, 0);
2288 else
2289 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2290}
2291
NeilBrown3cb03002011-10-11 16:45:26 +11002292static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002293 int sectors, struct page *page, int rw)
2294{
2295 sector_t first_bad;
2296 int bad_sectors;
2297
2298 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2299 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2300 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002301 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002302 /* success */
2303 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002304 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002305 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002306 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2307 set_bit(MD_RECOVERY_NEEDED,
2308 &rdev->mddev->recovery);
2309 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002310 /* need to record an error - either for the block or the device */
2311 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2312 md_error(rdev->mddev, rdev);
2313 return 0;
2314}
2315
Robert Becker1e509152009-12-14 12:49:58 +11002316/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 * This is a kernel thread which:
2318 *
2319 * 1. Retries failed read operations on working mirrors.
2320 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002321 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 */
2323
NeilBrowne879a872011-10-11 16:49:02 +11002324static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002325{
2326 int sect = 0; /* Offset from r10_bio->sector */
2327 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002328 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002329 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002330 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002331
NeilBrown7c4e06f2011-05-11 14:53:17 +10002332 /* still own a reference to this rdev, so it cannot
2333 * have been cleared recently.
2334 */
2335 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002336
NeilBrown7c4e06f2011-05-11 14:53:17 +10002337 if (test_bit(Faulty, &rdev->flags))
2338 /* drive has already been failed, just ignore any
2339 more fix_read_error() attempts */
2340 return;
2341
2342 check_decay_read_errors(mddev, rdev);
2343 atomic_inc(&rdev->read_errors);
2344 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2345 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002346 bdevname(rdev->bdev, b);
2347
NeilBrown08464e02016-11-02 14:16:50 +11002348 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2349 mdname(mddev), b,
2350 atomic_read(&rdev->read_errors), max_read_errors);
2351 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2352 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002353 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002354 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002355 return;
Robert Becker1e509152009-12-14 12:49:58 +11002356 }
Robert Becker1e509152009-12-14 12:49:58 +11002357
NeilBrown6814d532006-10-03 01:15:45 -07002358 while(sectors) {
2359 int s = sectors;
2360 int sl = r10_bio->read_slot;
2361 int success = 0;
2362 int start;
2363
2364 if (s > (PAGE_SIZE>>9))
2365 s = PAGE_SIZE >> 9;
2366
2367 rcu_read_lock();
2368 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002369 sector_t first_bad;
2370 int bad_sectors;
2371
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002372 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002373 rdev = rcu_dereference(conf->mirrors[d].rdev);
2374 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002375 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002376 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002377 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2378 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002379 atomic_inc(&rdev->nr_pending);
2380 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002381 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002382 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002383 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002384 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002385 conf->tmppage,
2386 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002387 rdev_dec_pending(rdev, mddev);
2388 rcu_read_lock();
2389 if (success)
2390 break;
2391 }
2392 sl++;
2393 if (sl == conf->copies)
2394 sl = 0;
2395 } while (!success && sl != r10_bio->read_slot);
2396 rcu_read_unlock();
2397
2398 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002399 /* Cannot read from anywhere, just mark the block
2400 * as bad on the first device to discourage future
2401 * reads.
2402 */
NeilBrown6814d532006-10-03 01:15:45 -07002403 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002404 rdev = conf->mirrors[dn].rdev;
2405
2406 if (!rdev_set_badblocks(
2407 rdev,
2408 r10_bio->devs[r10_bio->read_slot].addr
2409 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002410 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002411 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002412 r10_bio->devs[r10_bio->read_slot].bio
2413 = IO_BLOCKED;
2414 }
NeilBrown6814d532006-10-03 01:15:45 -07002415 break;
2416 }
2417
2418 start = sl;
2419 /* write it back and re-read */
2420 rcu_read_lock();
2421 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002422 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002423
NeilBrown6814d532006-10-03 01:15:45 -07002424 if (sl==0)
2425 sl = conf->copies;
2426 sl--;
2427 d = r10_bio->devs[sl].devnum;
2428 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002429 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002430 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002431 !test_bit(In_sync, &rdev->flags))
2432 continue;
2433
2434 atomic_inc(&rdev->nr_pending);
2435 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002436 if (r10_sync_page_io(rdev,
2437 r10_bio->devs[sl].addr +
2438 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002439 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002440 == 0) {
2441 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002442 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2443 mdname(mddev), s,
2444 (unsigned long long)(
2445 sect +
2446 choose_data_offset(r10_bio,
2447 rdev)),
2448 bdevname(rdev->bdev, b));
2449 pr_notice("md/raid10:%s: %s: failing drive\n",
2450 mdname(mddev),
2451 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002452 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002453 rdev_dec_pending(rdev, mddev);
2454 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002455 }
2456 sl = start;
2457 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002458 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002459
NeilBrown6814d532006-10-03 01:15:45 -07002460 if (sl==0)
2461 sl = conf->copies;
2462 sl--;
2463 d = r10_bio->devs[sl].devnum;
2464 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002465 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002466 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002467 !test_bit(In_sync, &rdev->flags))
2468 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002469
NeilBrown1294b9c2011-07-28 11:39:23 +10002470 atomic_inc(&rdev->nr_pending);
2471 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002472 switch (r10_sync_page_io(rdev,
2473 r10_bio->devs[sl].addr +
2474 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002475 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002476 READ)) {
2477 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002478 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002479 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002480 mdname(mddev), s,
2481 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002482 sect +
2483 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002484 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002485 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002486 mdname(mddev),
2487 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002488 break;
2489 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002490 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002491 mdname(mddev), s,
2492 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002493 sect +
2494 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002495 bdevname(rdev->bdev, b));
2496 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002497 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002498
2499 rdev_dec_pending(rdev, mddev);
2500 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002501 }
2502 rcu_read_unlock();
2503
2504 sectors -= s;
2505 sect += s;
2506 }
2507}
2508
NeilBrown9f2c9d12011-10-11 16:48:43 +11002509static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002510{
2511 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002512 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002513 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002514 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002515 /* bio has the data to be written to slot 'i' where
2516 * we just recently had a write error.
2517 * We repeatedly clone the bio and trim down to one block,
2518 * then try the write. Where the write fails we record
2519 * a bad block.
2520 * It is conceivable that the bio doesn't exactly align with
2521 * blocks. We must handle this.
2522 *
2523 * We currently own a reference to the rdev.
2524 */
2525
2526 int block_sectors;
2527 sector_t sector;
2528 int sectors;
2529 int sect_to_write = r10_bio->sectors;
2530 int ok = 1;
2531
2532 if (rdev->badblocks.shift < 0)
2533 return 0;
2534
NeilBrownf04ebb02015-02-16 14:51:54 +11002535 block_sectors = roundup(1 << rdev->badblocks.shift,
2536 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002537 sector = r10_bio->sector;
2538 sectors = ((r10_bio->sector + block_sectors)
2539 & ~(sector_t)(block_sectors - 1))
2540 - sector;
2541
2542 while (sect_to_write) {
2543 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002544 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002545 if (sectors > sect_to_write)
2546 sectors = sect_to_write;
2547 /* Write at 'sector' for 'sectors' */
Ming Leid7a10302017-02-14 23:29:03 +08002548 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002549 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002550 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2551 wbio->bi_iter.bi_sector = wsector +
2552 choose_data_offset(r10_bio, rdev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002553 bio_set_dev(wbio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05002554 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002555
2556 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002557 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002558 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002559 sectors, 0)
2560 && ok;
2561
2562 bio_put(wbio);
2563 sect_to_write -= sectors;
2564 sector += sectors;
2565 sectors = block_sectors;
2566 }
2567 return ok;
2568}
2569
NeilBrown9f2c9d12011-10-11 16:48:43 +11002570static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002571{
2572 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002573 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002574 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002575 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002576
2577 /* we got a read error. Maybe the drive is bad. Maybe just
2578 * the block and we can fix it.
2579 * We freeze all other IO, and try reading the block from
2580 * other devices. When we find one, we re-write
2581 * and check it that fixes the read error.
2582 * This is all done synchronously while the array is
2583 * frozen.
2584 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002585 bio = r10_bio->devs[slot].bio;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002586 bio_put(bio);
2587 r10_bio->devs[slot].bio = NULL;
2588
NeilBrown8d3ca832016-11-18 16:16:12 +11002589 if (mddev->ro)
2590 r10_bio->devs[slot].bio = IO_BLOCKED;
2591 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002592 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002593 fix_read_error(conf, mddev, r10_bio);
2594 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002595 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002596 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002597
NeilBrownabbf0982011-12-23 10:17:54 +11002598 rdev_dec_pending(rdev, mddev);
NeilBrown545250f2017-04-05 14:05:51 +10002599 allow_barrier(conf);
2600 r10_bio->state = 0;
2601 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002602}
2603
NeilBrowne879a872011-10-11 16:49:02 +11002604static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002605{
2606 /* Some sort of write request has finished and it
2607 * succeeded in writing where we thought there was a
2608 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002609 * Or possibly if failed and we need to record
2610 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002611 */
2612 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002613 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002614
2615 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2616 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002617 for (m = 0; m < conf->copies; m++) {
2618 int dev = r10_bio->devs[m].devnum;
2619 rdev = conf->mirrors[dev].rdev;
2620 if (r10_bio->devs[m].bio == NULL)
2621 continue;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002622 if (!r10_bio->devs[m].bio->bi_status) {
NeilBrown749c55e2011-07-28 11:39:24 +10002623 rdev_clear_badblocks(
2624 rdev,
2625 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002626 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002627 } else {
2628 if (!rdev_set_badblocks(
2629 rdev,
2630 r10_bio->devs[m].addr,
2631 r10_bio->sectors, 0))
2632 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002633 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002634 rdev = conf->mirrors[dev].replacement;
2635 if (r10_bio->devs[m].repl_bio == NULL)
2636 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002637
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002638 if (!r10_bio->devs[m].repl_bio->bi_status) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002639 rdev_clear_badblocks(
2640 rdev,
2641 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002642 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002643 } else {
2644 if (!rdev_set_badblocks(
2645 rdev,
2646 r10_bio->devs[m].addr,
2647 r10_bio->sectors, 0))
2648 md_error(conf->mddev, rdev);
2649 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002650 }
NeilBrown749c55e2011-07-28 11:39:24 +10002651 put_buf(r10_bio);
2652 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002653 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002654 for (m = 0; m < conf->copies; m++) {
2655 int dev = r10_bio->devs[m].devnum;
2656 struct bio *bio = r10_bio->devs[m].bio;
2657 rdev = conf->mirrors[dev].rdev;
2658 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002659 rdev_clear_badblocks(
2660 rdev,
2661 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002662 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002663 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002664 } else if (bio != NULL && bio->bi_status) {
NeilBrown95af5872015-08-14 11:26:17 +10002665 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002666 if (!narrow_write_error(r10_bio, m)) {
2667 md_error(conf->mddev, rdev);
2668 set_bit(R10BIO_Degraded,
2669 &r10_bio->state);
2670 }
2671 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002672 }
NeilBrown475b0322011-12-23 10:17:55 +11002673 bio = r10_bio->devs[m].repl_bio;
2674 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002675 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002676 rdev_clear_badblocks(
2677 rdev,
2678 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002679 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002680 rdev_dec_pending(rdev, conf->mddev);
2681 }
NeilBrownbd870a12011-07-28 11:39:24 +10002682 }
NeilBrown95af5872015-08-14 11:26:17 +10002683 if (fail) {
2684 spin_lock_irq(&conf->device_lock);
2685 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002686 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002687 spin_unlock_irq(&conf->device_lock);
Guoqing Jiangcf25ae72017-04-17 17:11:05 +08002688 /*
2689 * In case freeze_array() is waiting for condition
2690 * nr_pending == nr_queued + extra to be true.
2691 */
2692 wake_up(&conf->wait_barrier);
NeilBrown95af5872015-08-14 11:26:17 +10002693 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002694 } else {
2695 if (test_bit(R10BIO_WriteError,
2696 &r10_bio->state))
2697 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002698 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002699 }
NeilBrown749c55e2011-07-28 11:39:24 +10002700 }
2701}
2702
Shaohua Li4ed87312012-10-11 13:34:00 +11002703static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Shaohua Li4ed87312012-10-11 13:34:00 +11002705 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002706 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002708 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002710 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
NeilBrown95af5872015-08-14 11:26:17 +10002714 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002715 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002716 LIST_HEAD(tmp);
2717 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002718 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002719 while (!list_empty(&conf->bio_end_io_list)) {
2720 list_move(conf->bio_end_io_list.prev, &tmp);
2721 conf->nr_queued--;
2722 }
NeilBrown95af5872015-08-14 11:26:17 +10002723 }
2724 spin_unlock_irqrestore(&conf->device_lock, flags);
2725 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002726 r10_bio = list_first_entry(&tmp, struct r10bio,
2727 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002728 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002729 if (mddev->degraded)
2730 set_bit(R10BIO_Degraded, &r10_bio->state);
2731
2732 if (test_bit(R10BIO_WriteError,
2733 &r10_bio->state))
2734 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002735 raid_end_bio_io(r10_bio);
2736 }
2737 }
2738
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002739 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002741
NeilBrown0021b7b2012-07-31 09:08:14 +02002742 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002745 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002746 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002748 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002749 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002751 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 spin_unlock_irqrestore(&conf->device_lock, flags);
2753
2754 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002755 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002756 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2757 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002758 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002759 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2760 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002761 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002763 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002765 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002766 handle_read_error(mddev, r10_bio);
NeilBrownfc9977d2017-04-05 14:05:51 +10002767 else
2768 WARN_ON_ONCE(1);
NeilBrown4443ae12006-01-06 00:20:28 -08002769
NeilBrown1d9d5242009-10-16 15:55:32 +11002770 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002771 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002772 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002774 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}
2776
NeilBrowne879a872011-10-11 16:49:02 +11002777static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002780 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002783 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002784 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002785 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002786 if (conf->mirrors[i].replacement)
2787 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2789 if (!conf->r10buf_pool)
2790 return -ENOMEM;
2791 conf->next_resync = 0;
2792 return 0;
2793}
2794
Shaohua Li208410b2017-08-24 17:50:40 -07002795static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
2796{
2797 struct r10bio *r10bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2798 struct rsync_pages *rp;
2799 struct bio *bio;
2800 int nalloc;
2801 int i;
2802
2803 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
2804 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
2805 nalloc = conf->copies; /* resync */
2806 else
2807 nalloc = 2; /* recovery */
2808
2809 for (i = 0; i < nalloc; i++) {
2810 bio = r10bio->devs[i].bio;
2811 rp = bio->bi_private;
2812 bio_reset(bio);
2813 bio->bi_private = rp;
2814 bio = r10bio->devs[i].repl_bio;
2815 if (bio) {
2816 rp = bio->bi_private;
2817 bio_reset(bio);
2818 bio->bi_private = rp;
2819 }
2820 }
2821 return r10bio;
2822}
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824/*
2825 * perform a "sync" on one "block"
2826 *
2827 * We need to make sure that no normal I/O request - particularly write
2828 * requests - conflict with active sync requests.
2829 *
2830 * This is achieved by tracking pending requests and a 'barrier' concept
2831 * that can be installed to exclude normal IO requests.
2832 *
2833 * Resync and recovery are handled very differently.
2834 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2835 *
2836 * For resync, we iterate over virtual addresses, read all copies,
2837 * and update if there are differences. If only one copy is live,
2838 * skip it.
2839 * For recovery, we iterate over physical addresses, read a good
2840 * value for each non-in_sync drive, and over-write.
2841 *
2842 * So, for recovery we may have several outstanding complex requests for a
2843 * given address, one for each out-of-sync device. We model this by allocating
2844 * a number of r10_bio structures, one for each out-of-sync device.
2845 * As we setup these structures, we collect all bio's together into a list
2846 * which we then process collectively to add pages, and then process again
2847 * to pass to generic_make_request.
2848 *
2849 * The r10_bio structures are linked using a borrowed master_bio pointer.
2850 * This link is counted in ->remaining. When the r10_bio that points to NULL
2851 * has its remaining count decremented to 0, the whole complex operation
2852 * is complete.
2853 *
2854 */
2855
Shaohua Li849674e2016-01-20 13:52:20 -08002856static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002857 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
NeilBrowne879a872011-10-11 16:49:02 +11002859 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002860 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 struct bio *biolist = NULL, *bio;
2862 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002864 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002865 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 sector_t sectors_skipped = 0;
2867 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002868 sector_t chunk_mask = conf->geo.chunk_mask;
Ming Lei022e5102017-07-14 16:14:42 +08002869 int page_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 if (!conf->r10buf_pool)
2872 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002875 /*
2876 * Allow skipping a full rebuild for incremental assembly
2877 * of a clean array, like RAID1 does.
2878 */
2879 if (mddev->bitmap == NULL &&
2880 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002881 mddev->reshape_position == MaxSector &&
2882 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002883 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002884 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002885 conf->fullsync == 0) {
2886 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002887 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002888 }
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002891 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002892 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2893 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 max_sector = mddev->resync_max_sectors;
2895 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002896 /* If we aborted, we need to abort the
2897 * sync on the 'current' bitmap chucks (there can
2898 * be several when recovering multiple devices).
2899 * as we may have started syncing it but not finished.
2900 * We can find the current address in
2901 * mddev->curr_resync, but for recovery,
2902 * we need to convert that to several
2903 * virtual addresses.
2904 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002905 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2906 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002907 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002908 return 0;
2909 }
2910
NeilBrown6cce3b22006-01-06 00:20:16 -08002911 if (mddev->curr_resync < max_sector) { /* aborted */
2912 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2913 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2914 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002915 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002916 sector_t sect =
2917 raid10_find_virt(conf, mddev->curr_resync, i);
2918 bitmap_end_sync(mddev->bitmap, sect,
2919 &sync_blocks, 1);
2920 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002921 } else {
2922 /* completed sync */
2923 if ((!mddev->bitmap || conf->fullsync)
2924 && conf->have_replacement
2925 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2926 /* Completed a full sync so the replacements
2927 * are now fully recovered.
2928 */
NeilBrownf90145f2016-06-02 16:19:52 +10002929 rcu_read_lock();
2930 for (i = 0; i < conf->geo.raid_disks; i++) {
2931 struct md_rdev *rdev =
2932 rcu_dereference(conf->mirrors[i].replacement);
2933 if (rdev)
2934 rdev->recovery_offset = MaxSector;
2935 }
2936 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11002937 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002938 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002939 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002940 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002942 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return sectors_skipped;
2944 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002945
2946 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2947 return reshape_request(mddev, sector_nr, skipped);
2948
NeilBrown5cf00fc2012-05-21 09:28:20 +10002949 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 /* if there has been nothing to do on any drive,
2951 * then there is nothing to do at all..
2952 */
NeilBrown57afd892005-06-21 17:17:13 -07002953 *skipped = 1;
2954 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
NeilBrownc6207272008-02-06 01:39:52 -08002957 if (max_sector > mddev->resync_max)
2958 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* make sure whole request will fit in a chunk - if chunks
2961 * are meaningful
2962 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002963 if (conf->geo.near_copies < conf->geo.raid_disks &&
2964 max_sector > (sector_nr | chunk_mask))
2965 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002967 /*
2968 * If there is non-resync activity waiting for a turn, then let it
2969 * though before starting on this new sync request.
2970 */
2971 if (conf->nr_waiting)
2972 schedule_timeout_uninterruptible(1);
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 /* Again, very different code for resync and recovery.
2975 * Both must result in an r10bio with a list of bios that
Christoph Hellwig74d46992017-08-23 19:10:32 +02002976 * have bi_end_io, bi_sector, bi_disk set,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 * and bi_private set to the r10bio.
2978 * For recovery, we may actually create several r10bios
2979 * with 2 bios in each, that correspond to the bios in the main one.
2980 * In this case, the subordinate r10bios link back through a
2981 * borrowed master_bio pointer, and the counter in the master
2982 * includes a ref from each subordinate.
2983 */
2984 /* First, we decide what to do and set ->bi_end_io
2985 * To end_sync_read if we want to read, and
2986 * end_sync_write if we will want to write.
2987 */
2988
NeilBrown6cce3b22006-01-06 00:20:16 -08002989 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2991 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002992 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 r10_bio = NULL;
2994
NeilBrown5cf00fc2012-05-21 09:28:20 +10002995 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002996 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002997 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002998 sector_t sect;
2999 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003000 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003001 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003002 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003003
NeilBrownf90145f2016-06-02 16:19:52 +10003004 rcu_read_lock();
3005 mrdev = rcu_dereference(mirror->rdev);
3006 mreplace = rcu_dereference(mirror->replacement);
3007
3008 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10003009 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10003010 test_bit(In_sync, &mrdev->flags)) &&
3011 (mreplace == NULL ||
3012 test_bit(Faulty, &mreplace->flags))) {
3013 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003014 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003015 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003016
3017 still_degraded = 0;
3018 /* want to reconstruct this device */
3019 rb2 = r10_bio;
3020 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003021 if (sect >= mddev->resync_max_sectors) {
3022 /* last stripe is not complete - don't
3023 * try to recover this sector.
3024 */
NeilBrownf90145f2016-06-02 16:19:52 +10003025 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003026 continue;
3027 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003028 if (mreplace && test_bit(Faulty, &mreplace->flags))
3029 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003030 /* Unless we are doing a full sync, or a replacement
3031 * we only need to recover the block if it is set in
3032 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003033 */
3034 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3035 &sync_blocks, 1);
3036 if (sync_blocks < max_sync)
3037 max_sync = sync_blocks;
3038 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003039 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003040 !conf->fullsync) {
3041 /* yep, skip the sync_blocks here, but don't assume
3042 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003043 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003044 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003045 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003046 continue;
3047 }
NeilBrownf90145f2016-06-02 16:19:52 +10003048 atomic_inc(&mrdev->nr_pending);
3049 if (mreplace)
3050 atomic_inc(&mreplace->nr_pending);
3051 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Shaohua Li208410b2017-08-24 17:50:40 -07003053 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003054 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003055 raise_barrier(conf, rb2 != NULL);
3056 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
NeilBrownab9d47e2011-05-11 14:54:41 +10003058 r10_bio->master_bio = (struct bio*)rb2;
3059 if (rb2)
3060 atomic_inc(&rb2->remaining);
3061 r10_bio->mddev = mddev;
3062 set_bit(R10BIO_IsRecover, &r10_bio->state);
3063 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003064
NeilBrownab9d47e2011-05-11 14:54:41 +10003065 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003066
NeilBrownab9d47e2011-05-11 14:54:41 +10003067 /* Need to check if the array will still be
3068 * degraded
3069 */
NeilBrownf90145f2016-06-02 16:19:52 +10003070 rcu_read_lock();
3071 for (j = 0; j < conf->geo.raid_disks; j++) {
3072 struct md_rdev *rdev = rcu_dereference(
3073 conf->mirrors[j].rdev);
3074 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003075 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
NeilBrownf90145f2016-06-02 16:19:52 +10003078 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003079
3080 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3081 &sync_blocks, still_degraded);
3082
NeilBrowne875ece2011-07-28 11:39:24 +10003083 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003084 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003085 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003086 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003087 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003088 struct md_rdev *rdev =
3089 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003090 sector_t sector, first_bad;
3091 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003092 if (!rdev ||
3093 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003094 continue;
3095 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003096 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003097 sector = r10_bio->devs[j].addr;
3098
3099 if (is_badblock(rdev, sector, max_sync,
3100 &first_bad, &bad_sectors)) {
3101 if (first_bad > sector)
3102 max_sync = first_bad - sector;
3103 else {
3104 bad_sectors -= (sector
3105 - first_bad);
3106 if (max_sync > bad_sectors)
3107 max_sync = bad_sectors;
3108 continue;
3109 }
3110 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003111 bio = r10_bio->devs[0].bio;
3112 bio->bi_next = biolist;
3113 biolist = bio;
NeilBrownab9d47e2011-05-11 14:54:41 +10003114 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003115 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003116 if (test_bit(FailFast, &rdev->flags))
3117 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003118 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003119 bio->bi_iter.bi_sector = from_addr +
3120 rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003121 bio_set_dev(bio, rdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003122 atomic_inc(&rdev->nr_pending);
3123 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003124
3125 for (k=0; k<conf->copies; k++)
3126 if (r10_bio->devs[k].devnum == i)
3127 break;
3128 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003129 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003130 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003131 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003132 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003133 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003134
NeilBrownf90145f2016-06-02 16:19:52 +10003135 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003136 bio = r10_bio->devs[1].bio;
3137 bio->bi_next = biolist;
3138 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003139 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003140 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003141 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003142 + mrdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003143 bio_set_dev(bio, mrdev->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003144 atomic_inc(&r10_bio->remaining);
3145 } else
3146 r10_bio->devs[1].bio->bi_end_io = NULL;
3147
3148 /* and maybe write to replacement */
3149 bio = r10_bio->devs[1].repl_bio;
3150 if (bio)
3151 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003152 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003153 * cannot be NULL as r10buf_pool_alloc will
3154 * have allocated it.
3155 * So the second test here is pointless.
3156 * But it keeps semantic-checkers happy, and
3157 * this comment keeps human reviewers
3158 * happy.
3159 */
NeilBrownf90145f2016-06-02 16:19:52 +10003160 if (mreplace == NULL || bio == NULL ||
3161 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003162 break;
3163 bio->bi_next = biolist;
3164 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003165 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003166 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003167 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003168 mreplace->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003169 bio_set_dev(bio, mreplace->bdev);
NeilBrown24afd802011-12-23 10:17:55 +11003170 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
NeilBrownf90145f2016-06-02 16:19:52 +10003173 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003174 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003175 /* Cannot recover, so abort the recovery or
3176 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003177 if (any_working) {
3178 /* problem is that there are bad blocks
3179 * on other device(s)
3180 */
3181 int k;
3182 for (k = 0; k < conf->copies; k++)
3183 if (r10_bio->devs[k].devnum == i)
3184 break;
NeilBrown24afd802011-12-23 10:17:55 +11003185 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003186 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003187 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003188 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003189 r10_bio->devs[k].addr,
3190 max_sync, 0))
3191 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003192 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003193 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003194 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003195 r10_bio->devs[k].addr,
3196 max_sync, 0))
3197 any_working = 0;
3198 }
3199 if (!any_working) {
3200 if (!test_and_set_bit(MD_RECOVERY_INTR,
3201 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003202 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003203 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003204 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003205 = mddev->recovery_disabled;
3206 }
NeilBrowne8b84912014-01-06 10:35:34 +11003207 put_buf(r10_bio);
3208 if (rb2)
3209 atomic_dec(&rb2->remaining);
3210 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003211 rdev_dec_pending(mrdev, mddev);
3212 if (mreplace)
3213 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003214 break;
3215 }
NeilBrownf90145f2016-06-02 16:19:52 +10003216 rdev_dec_pending(mrdev, mddev);
3217 if (mreplace)
3218 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003219 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3220 /* Only want this if there is elsewhere to
3221 * read from. 'j' is currently the first
3222 * readable copy.
3223 */
3224 int targets = 1;
3225 for (; j < conf->copies; j++) {
3226 int d = r10_bio->devs[j].devnum;
3227 if (conf->mirrors[d].rdev &&
3228 test_bit(In_sync,
3229 &conf->mirrors[d].rdev->flags))
3230 targets++;
3231 }
3232 if (targets == 1)
3233 r10_bio->devs[0].bio->bi_opf
3234 &= ~MD_FAILFAST;
3235 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (biolist == NULL) {
3238 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003239 struct r10bio *rb2 = r10_bio;
3240 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 rb2->master_bio = NULL;
3242 put_buf(rb2);
3243 }
3244 goto giveup;
3245 }
3246 } else {
3247 /* resync. Schedule a read for every block at this virt offset */
3248 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003249
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003250 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003251
NeilBrown6cce3b22006-01-06 00:20:16 -08003252 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3253 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003254 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3255 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003256 /* We can skip this block */
3257 *skipped = 1;
3258 return sync_blocks + sectors_skipped;
3259 }
3260 if (sync_blocks < max_sync)
3261 max_sync = sync_blocks;
Shaohua Li208410b2017-08-24 17:50:40 -07003262 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003263 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 r10_bio->mddev = mddev;
3266 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003267 raise_barrier(conf, 0);
3268 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 r10_bio->master_bio = NULL;
3271 r10_bio->sector = sector_nr;
3272 set_bit(R10BIO_IsSync, &r10_bio->state);
3273 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003274 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
NeilBrown5cf00fc2012-05-21 09:28:20 +10003276 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003278 sector_t first_bad, sector;
3279 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003280 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003281
NeilBrown9ad1aef2011-12-23 10:17:55 +11003282 if (r10_bio->devs[i].repl_bio)
3283 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 bio = r10_bio->devs[i].bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003286 bio->bi_status = BLK_STS_IOERR;
NeilBrownf90145f2016-06-02 16:19:52 +10003287 rcu_read_lock();
3288 rdev = rcu_dereference(conf->mirrors[d].rdev);
3289 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3290 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003292 }
NeilBrown40c356c2011-07-28 11:39:24 +10003293 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003294 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003295 &first_bad, &bad_sectors)) {
3296 if (first_bad > sector)
3297 max_sync = first_bad - sector;
3298 else {
3299 bad_sectors -= (sector - first_bad);
3300 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003301 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003302 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003303 continue;
3304 }
3305 }
NeilBrownf90145f2016-06-02 16:19:52 +10003306 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 atomic_inc(&r10_bio->remaining);
3308 bio->bi_next = biolist;
3309 biolist = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003311 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003312 if (test_bit(FailFast, &rdev->flags))
NeilBrown8d3ca832016-11-18 16:16:12 +11003313 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003314 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003315 bio_set_dev(bio, rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003317
NeilBrownf90145f2016-06-02 16:19:52 +10003318 rdev = rcu_dereference(conf->mirrors[d].replacement);
3319 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3320 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003321 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003322 }
3323 atomic_inc(&rdev->nr_pending);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003324
3325 /* Need to set up for writing to the replacement */
3326 bio = r10_bio->devs[i].repl_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003327 bio->bi_status = BLK_STS_IOERR;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003328
3329 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003330 bio->bi_next = biolist;
3331 biolist = bio;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003332 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003333 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003334 if (test_bit(FailFast, &rdev->flags))
NeilBrown1919cbb2016-11-18 16:16:12 +11003335 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003336 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003337 bio_set_dev(bio, rdev->bdev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003338 count++;
Guoqing Jiang1cdd1252017-06-13 11:16:08 +08003339 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
3341
3342 if (count < 2) {
3343 for (i=0; i<conf->copies; i++) {
3344 int d = r10_bio->devs[i].devnum;
3345 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003346 rdev_dec_pending(conf->mirrors[d].rdev,
3347 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003348 if (r10_bio->devs[i].repl_bio &&
3349 r10_bio->devs[i].repl_bio->bi_end_io)
3350 rdev_dec_pending(
3351 conf->mirrors[d].replacement,
3352 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354 put_buf(r10_bio);
3355 biolist = NULL;
3356 goto giveup;
3357 }
3358 }
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003361 if (sector_nr + max_sync < max_sector)
3362 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 do {
3364 struct page *page;
3365 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (sector_nr + (len>>9) > max_sector)
3367 len = (max_sector - sector_nr) << 9;
3368 if (len == 0)
3369 break;
3370 for (bio= biolist ; bio ; bio=bio->bi_next) {
Ming Leif0250612017-03-17 00:12:33 +08003371 struct resync_pages *rp = get_resync_pages(bio);
Ming Lei022e5102017-07-14 16:14:42 +08003372 page = resync_fetch_page(rp, page_idx);
Ming Leic85ba142017-03-17 00:12:22 +08003373 /*
3374 * won't fail because the vec table is big enough
3375 * to hold all these pages
3376 */
3377 bio_add_page(bio, page, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 }
3379 nr_sectors += len>>9;
3380 sector_nr += len>>9;
Ming Lei022e5102017-07-14 16:14:42 +08003381 } while (++page_idx < RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 r10_bio->sectors = nr_sectors;
3383
3384 while (biolist) {
3385 bio = biolist;
3386 biolist = biolist->bi_next;
3387
3388 bio->bi_next = NULL;
Ming Leif0250612017-03-17 00:12:33 +08003389 r10_bio = get_resync_r10bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 r10_bio->sectors = nr_sectors;
3391
3392 if (bio->bi_end_io == end_sync_read) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02003393 md_sync_acct_bio(bio, nr_sectors);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003394 bio->bi_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 generic_make_request(bio);
3396 }
3397 }
3398
NeilBrown57afd892005-06-21 17:17:13 -07003399 if (sectors_skipped)
3400 /* pretend they weren't skipped, it makes
3401 * no important difference in this case
3402 */
3403 md_done_sync(mddev, sectors_skipped, 1);
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return sectors_skipped + nr_sectors;
3406 giveup:
3407 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003408 * drives must be failed or in resync, all drives
3409 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 */
NeilBrown09b40682009-02-25 13:18:47 +11003411 if (sector_nr + max_sync < max_sector)
3412 max_sector = sector_nr + max_sync;
3413
3414 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 chunks_skipped ++;
3416 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Dan Williams80c3a6c2009-03-17 18:10:40 -07003420static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003421raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003422{
3423 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003424 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003425
3426 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003427 raid_disks = min(conf->geo.raid_disks,
3428 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003429 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003430 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003431
NeilBrown5cf00fc2012-05-21 09:28:20 +10003432 size = sectors >> conf->geo.chunk_shift;
3433 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003434 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003435 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003436
NeilBrown5cf00fc2012-05-21 09:28:20 +10003437 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003438}
3439
NeilBrown6508fdb2012-05-17 10:08:45 +10003440static void calc_sectors(struct r10conf *conf, sector_t size)
3441{
3442 /* Calculate the number of sectors-per-device that will
3443 * actually be used, and set conf->dev_sectors and
3444 * conf->stride
3445 */
3446
NeilBrown5cf00fc2012-05-21 09:28:20 +10003447 size = size >> conf->geo.chunk_shift;
3448 sector_div(size, conf->geo.far_copies);
3449 size = size * conf->geo.raid_disks;
3450 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003451 /* 'size' is now the number of chunks in the array */
3452 /* calculate "used chunks per device" */
3453 size = size * conf->copies;
3454
3455 /* We need to round up when dividing by raid_disks to
3456 * get the stride size.
3457 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003458 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003459
NeilBrown5cf00fc2012-05-21 09:28:20 +10003460 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003461
NeilBrown5cf00fc2012-05-21 09:28:20 +10003462 if (conf->geo.far_offset)
3463 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003464 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003465 sector_div(size, conf->geo.far_copies);
3466 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003467 }
3468}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003469
NeilBrowndeb200d2012-05-21 09:28:33 +10003470enum geo_type {geo_new, geo_old, geo_start};
3471static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3472{
3473 int nc, fc, fo;
3474 int layout, chunk, disks;
3475 switch (new) {
3476 case geo_old:
3477 layout = mddev->layout;
3478 chunk = mddev->chunk_sectors;
3479 disks = mddev->raid_disks - mddev->delta_disks;
3480 break;
3481 case geo_new:
3482 layout = mddev->new_layout;
3483 chunk = mddev->new_chunk_sectors;
3484 disks = mddev->raid_disks;
3485 break;
3486 default: /* avoid 'may be unused' warnings */
3487 case geo_start: /* new when starting reshape - raid_disks not
3488 * updated yet. */
3489 layout = mddev->new_layout;
3490 chunk = mddev->new_chunk_sectors;
3491 disks = mddev->raid_disks + mddev->delta_disks;
3492 break;
3493 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003494 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003495 return -1;
3496 if (chunk < (PAGE_SIZE >> 9) ||
3497 !is_power_of_2(chunk))
3498 return -2;
3499 nc = layout & 255;
3500 fc = (layout >> 8) & 255;
3501 fo = layout & (1<<16);
3502 geo->raid_disks = disks;
3503 geo->near_copies = nc;
3504 geo->far_copies = fc;
3505 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003506 switch (layout >> 17) {
3507 case 0: /* original layout. simple but not always optimal */
3508 geo->far_set_size = disks;
3509 break;
3510 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3511 * actually using this, but leave code here just in case.*/
3512 geo->far_set_size = disks/fc;
3513 WARN(geo->far_set_size < fc,
3514 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3515 break;
3516 case 2: /* "improved" layout fixed to match documentation */
3517 geo->far_set_size = fc * nc;
3518 break;
3519 default: /* Not a valid layout */
3520 return -1;
3521 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003522 geo->chunk_mask = chunk - 1;
3523 geo->chunk_shift = ffz(~chunk);
3524 return nc*fc;
3525}
3526
NeilBrowne879a872011-10-11 16:49:02 +11003527static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
NeilBrowne879a872011-10-11 16:49:02 +11003529 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003530 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003531 struct geom geo;
3532 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
NeilBrowndeb200d2012-05-21 09:28:33 +10003534 copies = setup_geo(&geo, mddev, geo_new);
3535
3536 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003537 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3538 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003539 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
NeilBrown2604b702006-01-06 00:20:36 -08003541
NeilBrowndeb200d2012-05-21 09:28:33 +10003542 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003543 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3544 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 goto out;
3546 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003547
3548 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003549 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003550 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003552
NeilBrown3ea7daa2012-05-22 13:53:47 +10003553 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003554 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003555 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003556 GFP_KERNEL);
3557 if (!conf->mirrors)
3558 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003559
3560 conf->tmppage = alloc_page(GFP_KERNEL);
3561 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003562 goto out;
3563
NeilBrowndeb200d2012-05-21 09:28:33 +10003564 conf->geo = geo;
3565 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003566 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3567 r10bio_pool_free, conf);
3568 if (!conf->r10bio_pool)
3569 goto out;
3570
NeilBrown011067b2017-06-18 14:38:57 +10003571 conf->bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
NeilBrownfc9977d2017-04-05 14:05:51 +10003572 if (!conf->bio_split)
3573 goto out;
3574
NeilBrown6508fdb2012-05-17 10:08:45 +10003575 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003576 if (mddev->reshape_position == MaxSector) {
3577 conf->prev = conf->geo;
3578 conf->reshape_progress = MaxSector;
3579 } else {
3580 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3581 err = -EINVAL;
3582 goto out;
3583 }
3584 conf->reshape_progress = mddev->reshape_position;
3585 if (conf->prev.far_offset)
3586 conf->prev.stride = 1 << conf->prev.chunk_shift;
3587 else
3588 /* far_copies must be 1 */
3589 conf->prev.stride = conf->dev_sectors;
3590 }
NeilBrown299b0682015-07-06 17:37:49 +10003591 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003592 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003593 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003594 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003595
3596 spin_lock_init(&conf->resync_lock);
3597 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003598 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003599
NeilBrown02326052012-07-03 15:56:52 +10003600 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003601 if (!conf->thread)
3602 goto out;
3603
Trela, Maciejdab8b292010-03-08 16:02:45 +11003604 conf->mddev = mddev;
3605 return conf;
3606
3607 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003608 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003609 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003610 kfree(conf->mirrors);
3611 safe_put_page(conf->tmppage);
NeilBrownfc9977d2017-04-05 14:05:51 +10003612 if (conf->bio_split)
3613 bioset_free(conf->bio_split);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003614 kfree(conf);
3615 }
3616 return ERR_PTR(err);
3617}
3618
Shaohua Li849674e2016-01-20 13:52:20 -08003619static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003620{
NeilBrowne879a872011-10-11 16:49:02 +11003621 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003622 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003623 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003624 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003625 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003626 sector_t min_offset_diff = 0;
3627 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003628 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003629
NeilBrowna415c0f2017-06-05 16:05:13 +10003630 if (mddev_init_writes_pending(mddev) < 0)
3631 return -ENOMEM;
3632
Trela, Maciejdab8b292010-03-08 16:02:45 +11003633 if (mddev->private == NULL) {
3634 conf = setup_conf(mddev);
3635 if (IS_ERR(conf))
3636 return PTR_ERR(conf);
3637 mddev->private = conf;
3638 }
3639 conf = mddev->private;
3640 if (!conf)
3641 goto out;
3642
Trela, Maciejdab8b292010-03-08 16:02:45 +11003643 mddev->thread = conf->thread;
3644 conf->thread = NULL;
3645
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003646 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003647 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003648 blk_queue_max_discard_sectors(mddev->queue,
3649 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003650 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02003651 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003652 blk_queue_io_min(mddev->queue, chunk_size);
3653 if (conf->geo.raid_disks % conf->geo.near_copies)
3654 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3655 else
3656 blk_queue_io_opt(mddev->queue, chunk_size *
3657 (conf->geo.raid_disks / conf->geo.near_copies));
3658 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003659
NeilBrowndafb20f2012-03-19 12:46:39 +11003660 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003661 long long diff;
NeilBrown34b343c2011-07-28 11:31:47 +10003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003664 if (disk_idx < 0)
3665 continue;
3666 if (disk_idx >= conf->geo.raid_disks &&
3667 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 continue;
3669 disk = conf->mirrors + disk_idx;
3670
NeilBrown56a2559b2011-12-23 10:17:55 +11003671 if (test_bit(Replacement, &rdev->flags)) {
3672 if (disk->replacement)
3673 goto out_free_conf;
3674 disk->replacement = rdev;
3675 } else {
3676 if (disk->rdev)
3677 goto out_free_conf;
3678 disk->rdev = rdev;
3679 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003680 diff = (rdev->new_data_offset - rdev->data_offset);
3681 if (!mddev->reshape_backwards)
3682 diff = -diff;
3683 if (diff < 0)
3684 diff = 0;
3685 if (first || diff < min_offset_diff)
3686 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11003687
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003688 if (mddev->gendisk)
3689 disk_stack_limits(mddev->gendisk, rdev->bdev,
3690 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003693
3694 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3695 discard_supported = true;
Guoqing Jiang6f287ca2017-04-06 09:12:18 +08003696 first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003698
Jonathan Brassowed30be02012-10-31 11:42:30 +11003699 if (mddev->queue) {
3700 if (discard_supported)
3701 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3702 mddev->queue);
3703 else
3704 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3705 mddev->queue);
3706 }
NeilBrown6d508242005-09-09 16:24:03 -07003707 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003708 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003709 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003710 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 goto out_free_conf;
3712 }
3713
NeilBrown3ea7daa2012-05-22 13:53:47 +10003714 if (conf->reshape_progress != MaxSector) {
3715 /* must ensure that shape change is supported */
3716 if (conf->geo.far_copies != 1 &&
3717 conf->geo.far_offset == 0)
3718 goto out_free_conf;
3719 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003720 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003721 goto out_free_conf;
3722 }
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003725 for (i = 0;
3726 i < conf->geo.raid_disks
3727 || i < conf->prev.raid_disks;
3728 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 disk = conf->mirrors + i;
3731
NeilBrown56a2559b2011-12-23 10:17:55 +11003732 if (!disk->rdev && disk->replacement) {
3733 /* The replacement is all we have - use it */
3734 disk->rdev = disk->replacement;
3735 disk->replacement = NULL;
3736 clear_bit(Replacement, &disk->rdev->flags);
3737 }
3738
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003739 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003740 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 disk->head_position = 0;
3742 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003743 if (disk->rdev &&
3744 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003745 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 }
NeilBrownd890fa22011-10-26 11:54:39 +11003747 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
3749
Andre Noll8c6ac8682009-06-18 08:48:06 +10003750 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003751 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3752 mdname(mddev));
3753 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003754 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3755 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 /*
3757 * Ok, everything is just fine now
3758 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003759 mddev->dev_sectors = conf->dev_sectors;
3760 size = raid10_size(mddev, 0, 0);
3761 md_set_array_sectors(mddev, size);
3762 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003763 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003765 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003766 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003767 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003768
3769 /* Calculate max read-ahead size.
3770 * We need to readahead at least twice a whole stripe....
3771 * maybe...
3772 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003773 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01003774 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3775 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777
Martin K. Petersena91a2782011-03-17 11:11:05 +01003778 if (md_integrity_register(mddev))
3779 goto out_free_conf;
3780
NeilBrown3ea7daa2012-05-22 13:53:47 +10003781 if (conf->reshape_progress != MaxSector) {
3782 unsigned long before_length, after_length;
3783
3784 before_length = ((1 << conf->prev.chunk_shift) *
3785 conf->prev.far_copies);
3786 after_length = ((1 << conf->geo.chunk_shift) *
3787 conf->geo.far_copies);
3788
3789 if (max(before_length, after_length) > min_offset_diff) {
3790 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003791 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003792 goto out_free_conf;
3793 }
3794 conf->offset_diff = min_offset_diff;
3795
NeilBrown3ea7daa2012-05-22 13:53:47 +10003796 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3797 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3798 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3799 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3800 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3801 "reshape");
3802 }
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return 0;
3805
3806out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003807 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003808 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003809 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003810 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 kfree(conf);
3812 mddev->private = NULL;
3813out:
3814 return -EIO;
3815}
3816
NeilBrownafa0f552014-12-15 12:56:58 +11003817static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
NeilBrownafa0f552014-12-15 12:56:58 +11003819 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Julia Lawall644df1a2015-09-13 14:15:10 +02003821 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003822 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003823 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003824 kfree(conf->mirrors_old);
3825 kfree(conf->mirrors_new);
NeilBrownfc9977d2017-04-05 14:05:51 +10003826 if (conf->bio_split)
3827 bioset_free(conf->bio_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
NeilBrownfd01b882011-10-11 16:47:53 +11003831static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003832{
NeilBrowne879a872011-10-11 16:49:02 +11003833 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003834
3835 switch(state) {
3836 case 1:
3837 raise_barrier(conf, 0);
3838 break;
3839 case 0:
3840 lower_barrier(conf);
3841 break;
3842 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
NeilBrown006a09a2012-03-19 12:46:40 +11003845static int raid10_resize(struct mddev *mddev, sector_t sectors)
3846{
3847 /* Resize of 'far' arrays is not supported.
3848 * For 'near' and 'offset' arrays we can set the
3849 * number of sectors used to be an appropriate multiple
3850 * of the chunk size.
3851 * For 'offset', this is far_copies*chunksize.
3852 * For 'near' the multiplier is the LCM of
3853 * near_copies and raid_disks.
3854 * So if far_copies > 1 && !far_offset, fail.
3855 * Else find LCM(raid_disks, near_copy)*far_copies and
3856 * multiply by chunk_size. Then round to this number.
3857 * This is mostly done by raid10_size()
3858 */
3859 struct r10conf *conf = mddev->private;
3860 sector_t oldsize, size;
3861
NeilBrownf8c9e742012-05-21 09:28:33 +10003862 if (mddev->reshape_position != MaxSector)
3863 return -EBUSY;
3864
NeilBrown5cf00fc2012-05-21 09:28:20 +10003865 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003866 return -EINVAL;
3867
3868 oldsize = raid10_size(mddev, 0, 0);
3869 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003870 if (mddev->external_size &&
3871 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003872 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003873 if (mddev->bitmap) {
3874 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3875 if (ret)
3876 return ret;
3877 }
3878 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003879 if (sectors > mddev->dev_sectors &&
3880 mddev->recovery_cp > oldsize) {
3881 mddev->recovery_cp = oldsize;
3882 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3883 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003884 calc_sectors(conf, sectors);
3885 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003886 mddev->resync_max_sectors = size;
3887 return 0;
3888}
3889
NeilBrown53a6ab42015-02-12 14:09:57 +11003890static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003891{
NeilBrown3cb03002011-10-11 16:45:26 +11003892 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003893 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003894
3895 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003896 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3897 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003898 return ERR_PTR(-EINVAL);
3899 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003900 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003901
Trela, Maciejdab8b292010-03-08 16:02:45 +11003902 /* Set new parameters */
3903 mddev->new_level = 10;
3904 /* new layout: far_copies = 1, near_copies = 2 */
3905 mddev->new_layout = (1<<8) + 2;
3906 mddev->new_chunk_sectors = mddev->chunk_sectors;
3907 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003908 mddev->raid_disks *= 2;
3909 /* make sure it will be not marked as dirty */
3910 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003911 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003912
3913 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003914 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003915 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003916 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003917 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003918 rdev->sectors = size;
3919 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003920 conf->barrier = 1;
3921 }
3922
Trela, Maciejdab8b292010-03-08 16:02:45 +11003923 return conf;
3924}
3925
NeilBrownfd01b882011-10-11 16:47:53 +11003926static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003927{
NeilBrowne373ab12011-10-11 16:48:59 +11003928 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003929
3930 /* raid10 can take over:
3931 * raid0 - providing it has only two drives
3932 */
3933 if (mddev->level == 0) {
3934 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003935 raid0_conf = mddev->private;
3936 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11003937 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3938 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003939 return ERR_PTR(-EINVAL);
3940 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003941 return raid10_takeover_raid0(mddev,
3942 raid0_conf->strip_zone->zone_end,
3943 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003944 }
3945 return ERR_PTR(-EINVAL);
3946}
3947
NeilBrown3ea7daa2012-05-22 13:53:47 +10003948static int raid10_check_reshape(struct mddev *mddev)
3949{
3950 /* Called when there is a request to change
3951 * - layout (to ->new_layout)
3952 * - chunk size (to ->new_chunk_sectors)
3953 * - raid_disks (by delta_disks)
3954 * or when trying to restart a reshape that was ongoing.
3955 *
3956 * We need to validate the request and possibly allocate
3957 * space if that might be an issue later.
3958 *
3959 * Currently we reject any reshape of a 'far' mode array,
3960 * allow chunk size to change if new is generally acceptable,
3961 * allow raid_disks to increase, and allow
3962 * a switch between 'near' mode and 'offset' mode.
3963 */
3964 struct r10conf *conf = mddev->private;
3965 struct geom geo;
3966
3967 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3968 return -EINVAL;
3969
3970 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3971 /* mustn't change number of copies */
3972 return -EINVAL;
3973 if (geo.far_copies > 1 && !geo.far_offset)
3974 /* Cannot switch to 'far' mode */
3975 return -EINVAL;
3976
3977 if (mddev->array_sectors & geo.chunk_mask)
3978 /* not factor of array size */
3979 return -EINVAL;
3980
NeilBrown3ea7daa2012-05-22 13:53:47 +10003981 if (!enough(conf, -1))
3982 return -EINVAL;
3983
3984 kfree(conf->mirrors_new);
3985 conf->mirrors_new = NULL;
3986 if (mddev->delta_disks > 0) {
3987 /* allocate new 'mirrors' list */
3988 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003989 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003990 *(mddev->raid_disks +
3991 mddev->delta_disks),
3992 GFP_KERNEL);
3993 if (!conf->mirrors_new)
3994 return -ENOMEM;
3995 }
3996 return 0;
3997}
3998
3999/*
4000 * Need to check if array has failed when deciding whether to:
4001 * - start an array
4002 * - remove non-faulty devices
4003 * - add a spare
4004 * - allow a reshape
4005 * This determination is simple when no reshape is happening.
4006 * However if there is a reshape, we need to carefully check
4007 * both the before and after sections.
4008 * This is because some failed devices may only affect one
4009 * of the two sections, and some non-in_sync devices may
4010 * be insync in the section most affected by failed devices.
4011 */
4012static int calc_degraded(struct r10conf *conf)
4013{
4014 int degraded, degraded2;
4015 int i;
4016
4017 rcu_read_lock();
4018 degraded = 0;
4019 /* 'prev' section first */
4020 for (i = 0; i < conf->prev.raid_disks; i++) {
4021 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4022 if (!rdev || test_bit(Faulty, &rdev->flags))
4023 degraded++;
4024 else if (!test_bit(In_sync, &rdev->flags))
4025 /* When we can reduce the number of devices in
4026 * an array, this might not contribute to
4027 * 'degraded'. It does now.
4028 */
4029 degraded++;
4030 }
4031 rcu_read_unlock();
4032 if (conf->geo.raid_disks == conf->prev.raid_disks)
4033 return degraded;
4034 rcu_read_lock();
4035 degraded2 = 0;
4036 for (i = 0; i < conf->geo.raid_disks; i++) {
4037 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4038 if (!rdev || test_bit(Faulty, &rdev->flags))
4039 degraded2++;
4040 else if (!test_bit(In_sync, &rdev->flags)) {
4041 /* If reshape is increasing the number of devices,
4042 * this section has already been recovered, so
4043 * it doesn't contribute to degraded.
4044 * else it does.
4045 */
4046 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4047 degraded2++;
4048 }
4049 }
4050 rcu_read_unlock();
4051 if (degraded2 > degraded)
4052 return degraded2;
4053 return degraded;
4054}
4055
4056static int raid10_start_reshape(struct mddev *mddev)
4057{
4058 /* A 'reshape' has been requested. This commits
4059 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4060 * This also checks if there are enough spares and adds them
4061 * to the array.
4062 * We currently require enough spares to make the final
4063 * array non-degraded. We also require that the difference
4064 * between old and new data_offset - on each device - is
4065 * enough that we never risk over-writing.
4066 */
4067
4068 unsigned long before_length, after_length;
4069 sector_t min_offset_diff = 0;
4070 int first = 1;
4071 struct geom new;
4072 struct r10conf *conf = mddev->private;
4073 struct md_rdev *rdev;
4074 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004075 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004076
4077 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4078 return -EBUSY;
4079
4080 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4081 return -EINVAL;
4082
4083 before_length = ((1 << conf->prev.chunk_shift) *
4084 conf->prev.far_copies);
4085 after_length = ((1 << conf->geo.chunk_shift) *
4086 conf->geo.far_copies);
4087
4088 rdev_for_each(rdev, mddev) {
4089 if (!test_bit(In_sync, &rdev->flags)
4090 && !test_bit(Faulty, &rdev->flags))
4091 spares++;
4092 if (rdev->raid_disk >= 0) {
4093 long long diff = (rdev->new_data_offset
4094 - rdev->data_offset);
4095 if (!mddev->reshape_backwards)
4096 diff = -diff;
4097 if (diff < 0)
4098 diff = 0;
4099 if (first || diff < min_offset_diff)
4100 min_offset_diff = diff;
Shaohua Lib5063352017-05-01 12:15:07 -07004101 first = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004102 }
4103 }
4104
4105 if (max(before_length, after_length) > min_offset_diff)
4106 return -EINVAL;
4107
4108 if (spares < mddev->delta_disks)
4109 return -EINVAL;
4110
4111 conf->offset_diff = min_offset_diff;
4112 spin_lock_irq(&conf->device_lock);
4113 if (conf->mirrors_new) {
4114 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004115 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004116 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004117 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004118 conf->mirrors_old = conf->mirrors;
4119 conf->mirrors = conf->mirrors_new;
4120 conf->mirrors_new = NULL;
4121 }
4122 setup_geo(&conf->geo, mddev, geo_start);
4123 smp_mb();
4124 if (mddev->reshape_backwards) {
4125 sector_t size = raid10_size(mddev, 0, 0);
4126 if (size < mddev->array_sectors) {
4127 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004128 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4129 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004130 return -EINVAL;
4131 }
4132 mddev->resync_max_sectors = size;
4133 conf->reshape_progress = size;
4134 } else
4135 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004136 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004137 spin_unlock_irq(&conf->device_lock);
4138
NeilBrownbb63a702012-05-22 13:55:28 +10004139 if (mddev->delta_disks && mddev->bitmap) {
4140 ret = bitmap_resize(mddev->bitmap,
4141 raid10_size(mddev, 0,
4142 conf->geo.raid_disks),
4143 0, 0);
4144 if (ret)
4145 goto abort;
4146 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004147 if (mddev->delta_disks > 0) {
4148 rdev_for_each(rdev, mddev)
4149 if (rdev->raid_disk < 0 &&
4150 !test_bit(Faulty, &rdev->flags)) {
4151 if (raid10_add_disk(mddev, rdev) == 0) {
4152 if (rdev->raid_disk >=
4153 conf->prev.raid_disks)
4154 set_bit(In_sync, &rdev->flags);
4155 else
4156 rdev->recovery_offset = 0;
4157
4158 if (sysfs_link_rdev(mddev, rdev))
4159 /* Failure here is OK */;
4160 }
4161 } else if (rdev->raid_disk >= conf->prev.raid_disks
4162 && !test_bit(Faulty, &rdev->flags)) {
4163 /* This is a spare that was manually added */
4164 set_bit(In_sync, &rdev->flags);
4165 }
4166 }
4167 /* When a reshape changes the number of devices,
4168 * ->degraded is measured against the larger of the
4169 * pre and post numbers.
4170 */
4171 spin_lock_irq(&conf->device_lock);
4172 mddev->degraded = calc_degraded(conf);
4173 spin_unlock_irq(&conf->device_lock);
4174 mddev->raid_disks = conf->geo.raid_disks;
4175 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004176 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004177
4178 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4179 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004180 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004181 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4182 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4183
4184 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4185 "reshape");
4186 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004187 ret = -EAGAIN;
4188 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004189 }
4190 conf->reshape_checkpoint = jiffies;
4191 md_wakeup_thread(mddev->sync_thread);
4192 md_new_event(mddev);
4193 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004194
4195abort:
4196 mddev->recovery = 0;
4197 spin_lock_irq(&conf->device_lock);
4198 conf->geo = conf->prev;
4199 mddev->raid_disks = conf->geo.raid_disks;
4200 rdev_for_each(rdev, mddev)
4201 rdev->new_data_offset = rdev->data_offset;
4202 smp_wmb();
4203 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004204 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004205 mddev->reshape_position = MaxSector;
4206 spin_unlock_irq(&conf->device_lock);
4207 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004208}
4209
4210/* Calculate the last device-address that could contain
4211 * any block from the chunk that includes the array-address 's'
4212 * and report the next address.
4213 * i.e. the address returned will be chunk-aligned and after
4214 * any data that is in the chunk containing 's'.
4215 */
4216static sector_t last_dev_address(sector_t s, struct geom *geo)
4217{
4218 s = (s | geo->chunk_mask) + 1;
4219 s >>= geo->chunk_shift;
4220 s *= geo->near_copies;
4221 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4222 s *= geo->far_copies;
4223 s <<= geo->chunk_shift;
4224 return s;
4225}
4226
4227/* Calculate the first device-address that could contain
4228 * any block from the chunk that includes the array-address 's'.
4229 * This too will be the start of a chunk
4230 */
4231static sector_t first_dev_address(sector_t s, struct geom *geo)
4232{
4233 s >>= geo->chunk_shift;
4234 s *= geo->near_copies;
4235 sector_div(s, geo->raid_disks);
4236 s *= geo->far_copies;
4237 s <<= geo->chunk_shift;
4238 return s;
4239}
4240
4241static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4242 int *skipped)
4243{
4244 /* We simply copy at most one chunk (smallest of old and new)
4245 * at a time, possibly less if that exceeds RESYNC_PAGES,
4246 * or we hit a bad block or something.
4247 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004248 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004249 * can record any location.
4250 *
4251 * If we will want to write to a location that isn't
4252 * yet recorded as 'safe' (i.e. in metadata on disk) then
4253 * we need to flush all reshape requests and update the metadata.
4254 *
4255 * When reshaping forwards (e.g. to more devices), we interpret
4256 * 'safe' as the earliest block which might not have been copied
4257 * down yet. We divide this by previous stripe size and multiply
4258 * by previous stripe length to get lowest device offset that we
4259 * cannot write to yet.
4260 * We interpret 'sector_nr' as an address that we want to write to.
4261 * From this we use last_device_address() to find where we might
4262 * write to, and first_device_address on the 'safe' position.
4263 * If this 'next' write position is after the 'safe' position,
4264 * we must update the metadata to increase the 'safe' position.
4265 *
4266 * When reshaping backwards, we round in the opposite direction
4267 * and perform the reverse test: next write position must not be
4268 * less than current safe position.
4269 *
4270 * In all this the minimum difference in data offsets
4271 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004272 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004273 *
4274 * We need to prepare all the bios here before we start any IO
4275 * to ensure the size we choose is acceptable to all devices.
4276 * The means one for each copy for write-out and an extra one for
4277 * read-in.
4278 * We store the read-in bio in ->master_bio and the others in
4279 * ->devs[x].bio and ->devs[x].repl_bio.
4280 */
4281 struct r10conf *conf = mddev->private;
4282 struct r10bio *r10_bio;
4283 sector_t next, safe, last;
4284 int max_sectors;
4285 int nr_sectors;
4286 int s;
4287 struct md_rdev *rdev;
4288 int need_flush = 0;
4289 struct bio *blist;
4290 struct bio *bio, *read_bio;
4291 int sectors_done = 0;
Ming Leif0250612017-03-17 00:12:33 +08004292 struct page **pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004293
4294 if (sector_nr == 0) {
4295 /* If restarting in the middle, skip the initial sectors */
4296 if (mddev->reshape_backwards &&
4297 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4298 sector_nr = (raid10_size(mddev, 0, 0)
4299 - conf->reshape_progress);
4300 } else if (!mddev->reshape_backwards &&
4301 conf->reshape_progress > 0)
4302 sector_nr = conf->reshape_progress;
4303 if (sector_nr) {
4304 mddev->curr_resync_completed = sector_nr;
4305 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4306 *skipped = 1;
4307 return sector_nr;
4308 }
4309 }
4310
4311 /* We don't use sector_nr to track where we are up to
4312 * as that doesn't work well for ->reshape_backwards.
4313 * So just use ->reshape_progress.
4314 */
4315 if (mddev->reshape_backwards) {
4316 /* 'next' is the earliest device address that we might
4317 * write to for this chunk in the new layout
4318 */
4319 next = first_dev_address(conf->reshape_progress - 1,
4320 &conf->geo);
4321
4322 /* 'safe' is the last device address that we might read from
4323 * in the old layout after a restart
4324 */
4325 safe = last_dev_address(conf->reshape_safe - 1,
4326 &conf->prev);
4327
4328 if (next + conf->offset_diff < safe)
4329 need_flush = 1;
4330
4331 last = conf->reshape_progress - 1;
4332 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4333 & conf->prev.chunk_mask);
4334 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4335 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4336 } else {
4337 /* 'next' is after the last device address that we
4338 * might write to for this chunk in the new layout
4339 */
4340 next = last_dev_address(conf->reshape_progress, &conf->geo);
4341
4342 /* 'safe' is the earliest device address that we might
4343 * read from in the old layout after a restart
4344 */
4345 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4346
4347 /* Need to update metadata if 'next' might be beyond 'safe'
4348 * as that would possibly corrupt data
4349 */
4350 if (next > safe + conf->offset_diff)
4351 need_flush = 1;
4352
4353 sector_nr = conf->reshape_progress;
4354 last = sector_nr | (conf->geo.chunk_mask
4355 & conf->prev.chunk_mask);
4356
4357 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4358 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4359 }
4360
4361 if (need_flush ||
4362 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4363 /* Need to update reshape_position in metadata */
4364 wait_barrier(conf);
4365 mddev->reshape_position = conf->reshape_progress;
4366 if (mddev->reshape_backwards)
4367 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4368 - conf->reshape_progress;
4369 else
4370 mddev->curr_resync_completed = conf->reshape_progress;
4371 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004372 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004373 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004374 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004375 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4376 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4377 allow_barrier(conf);
4378 return sectors_done;
4379 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004380 conf->reshape_safe = mddev->reshape_position;
4381 allow_barrier(conf);
4382 }
4383
4384read_more:
4385 /* Now schedule reads for blocks from sector_nr to last */
Shaohua Li208410b2017-08-24 17:50:40 -07004386 r10_bio = raid10_alloc_init_r10buf(conf);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004387 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004388 raise_barrier(conf, sectors_done != 0);
4389 atomic_set(&r10_bio->remaining, 0);
4390 r10_bio->mddev = mddev;
4391 r10_bio->sector = sector_nr;
4392 set_bit(R10BIO_IsReshape, &r10_bio->state);
4393 r10_bio->sectors = last - sector_nr + 1;
4394 rdev = read_balance(conf, r10_bio, &max_sectors);
4395 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4396
4397 if (!rdev) {
4398 /* Cannot read from here, so need to record bad blocks
4399 * on all the target devices.
4400 */
4401 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004402 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004403 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4404 return sectors_done;
4405 }
4406
4407 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4408
Christoph Hellwig74d46992017-08-23 19:10:32 +02004409 bio_set_dev(read_bio, rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004410 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004411 + rdev->data_offset);
4412 read_bio->bi_private = r10_bio;
Ming Lei81fa1522017-03-17 00:12:32 +08004413 read_bio->bi_end_io = end_reshape_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004414 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004415 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02004416 read_bio->bi_status = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004417 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004418 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004419 r10_bio->master_bio = read_bio;
4420 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4421
4422 /* Now find the locations in the new layout */
4423 __raid10_find_phys(&conf->geo, r10_bio);
4424
4425 blist = read_bio;
4426 read_bio->bi_next = NULL;
4427
NeilBrownd094d682016-06-02 16:19:52 +10004428 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004429 for (s = 0; s < conf->copies*2; s++) {
4430 struct bio *b;
4431 int d = r10_bio->devs[s/2].devnum;
4432 struct md_rdev *rdev2;
4433 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004434 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004435 b = r10_bio->devs[s/2].repl_bio;
4436 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004437 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004438 b = r10_bio->devs[s/2].bio;
4439 }
4440 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4441 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004442
Christoph Hellwig74d46992017-08-23 19:10:32 +02004443 bio_set_dev(b, rdev2->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004444 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4445 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004446 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004447 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004448 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004449 blist = b;
4450 }
4451
4452 /* Now add as many pages as possible to all of these bios. */
4453
4454 nr_sectors = 0;
Ming Leif0250612017-03-17 00:12:33 +08004455 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004456 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
Ming Leif0250612017-03-17 00:12:33 +08004457 struct page *page = pages[s / (PAGE_SIZE >> 9)];
NeilBrown3ea7daa2012-05-22 13:53:47 +10004458 int len = (max_sectors - s) << 9;
4459 if (len > PAGE_SIZE)
4460 len = PAGE_SIZE;
4461 for (bio = blist; bio ; bio = bio->bi_next) {
Ming Leic85ba142017-03-17 00:12:22 +08004462 /*
4463 * won't fail because the vec table is big enough
4464 * to hold all these pages
4465 */
4466 bio_add_page(bio, page, len, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004467 }
4468 sector_nr += len >> 9;
4469 nr_sectors += len >> 9;
4470 }
NeilBrownd094d682016-06-02 16:19:52 +10004471 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004472 r10_bio->sectors = nr_sectors;
4473
4474 /* Now submit the read */
Christoph Hellwig74d46992017-08-23 19:10:32 +02004475 md_sync_acct_bio(read_bio, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004476 atomic_inc(&r10_bio->remaining);
4477 read_bio->bi_next = NULL;
4478 generic_make_request(read_bio);
4479 sector_nr += nr_sectors;
4480 sectors_done += nr_sectors;
4481 if (sector_nr <= last)
4482 goto read_more;
4483
4484 /* Now that we have done the whole section we can
4485 * update reshape_progress
4486 */
4487 if (mddev->reshape_backwards)
4488 conf->reshape_progress -= sectors_done;
4489 else
4490 conf->reshape_progress += sectors_done;
4491
4492 return sectors_done;
4493}
4494
4495static void end_reshape_request(struct r10bio *r10_bio);
4496static int handle_reshape_read_error(struct mddev *mddev,
4497 struct r10bio *r10_bio);
4498static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4499{
4500 /* Reshape read completed. Hopefully we have a block
4501 * to write out.
4502 * If we got a read error then we do sync 1-page reads from
4503 * elsewhere until we find the data - or give up.
4504 */
4505 struct r10conf *conf = mddev->private;
4506 int s;
4507
4508 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4509 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4510 /* Reshape has been aborted */
4511 md_done_sync(mddev, r10_bio->sectors, 0);
4512 return;
4513 }
4514
4515 /* We definitely have the data in the pages, schedule the
4516 * writes.
4517 */
4518 atomic_set(&r10_bio->remaining, 1);
4519 for (s = 0; s < conf->copies*2; s++) {
4520 struct bio *b;
4521 int d = r10_bio->devs[s/2].devnum;
4522 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004523 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004524 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004525 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004526 b = r10_bio->devs[s/2].repl_bio;
4527 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004528 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004529 b = r10_bio->devs[s/2].bio;
4530 }
NeilBrownd094d682016-06-02 16:19:52 +10004531 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4532 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004533 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004534 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004535 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004536 rcu_read_unlock();
Christoph Hellwig74d46992017-08-23 19:10:32 +02004537 md_sync_acct_bio(b, r10_bio->sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004538 atomic_inc(&r10_bio->remaining);
4539 b->bi_next = NULL;
4540 generic_make_request(b);
4541 }
4542 end_reshape_request(r10_bio);
4543}
4544
4545static void end_reshape(struct r10conf *conf)
4546{
4547 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4548 return;
4549
4550 spin_lock_irq(&conf->device_lock);
4551 conf->prev = conf->geo;
4552 md_finish_reshape(conf->mddev);
4553 smp_wmb();
4554 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004555 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004556 spin_unlock_irq(&conf->device_lock);
4557
4558 /* read-ahead size must cover two whole stripes, which is
4559 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4560 */
4561 if (conf->mddev->queue) {
4562 int stripe = conf->geo.raid_disks *
4563 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4564 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01004565 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4566 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004567 }
4568 conf->fullsync = 0;
4569}
4570
NeilBrown3ea7daa2012-05-22 13:53:47 +10004571static int handle_reshape_read_error(struct mddev *mddev,
4572 struct r10bio *r10_bio)
4573{
4574 /* Use sync reads to get the blocks from somewhere else */
4575 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004576 struct r10conf *conf = mddev->private;
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07004577 struct r10bio *r10b;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004578 int slot = 0;
4579 int idx = 0;
Ming Lei2d06e3b2017-03-17 00:12:35 +08004580 struct page **pages;
4581
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07004582 r10b = kmalloc(sizeof(*r10b) +
4583 sizeof(struct r10dev) * conf->copies, GFP_NOIO);
4584 if (!r10b) {
4585 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4586 return -ENOMEM;
4587 }
4588
Ming Lei2d06e3b2017-03-17 00:12:35 +08004589 /* reshape IOs share pages from .devs[0].bio */
4590 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004591
NeilBrowne0ee7782012-08-18 09:51:42 +10004592 r10b->sector = r10_bio->sector;
4593 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004594
4595 while (sectors) {
4596 int s = sectors;
4597 int success = 0;
4598 int first_slot = slot;
4599
4600 if (s > (PAGE_SIZE >> 9))
4601 s = PAGE_SIZE >> 9;
4602
NeilBrownd094d682016-06-02 16:19:52 +10004603 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004604 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004605 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004606 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004607 sector_t addr;
4608 if (rdev == NULL ||
4609 test_bit(Faulty, &rdev->flags) ||
4610 !test_bit(In_sync, &rdev->flags))
4611 goto failed;
4612
NeilBrowne0ee7782012-08-18 09:51:42 +10004613 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004614 atomic_inc(&rdev->nr_pending);
4615 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004616 success = sync_page_io(rdev,
4617 addr,
4618 s << 9,
Ming Lei2d06e3b2017-03-17 00:12:35 +08004619 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05004620 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004621 rdev_dec_pending(rdev, mddev);
4622 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004623 if (success)
4624 break;
4625 failed:
4626 slot++;
4627 if (slot >= conf->copies)
4628 slot = 0;
4629 if (slot == first_slot)
4630 break;
4631 }
NeilBrownd094d682016-06-02 16:19:52 +10004632 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004633 if (!success) {
4634 /* couldn't read this block, must give up */
4635 set_bit(MD_RECOVERY_INTR,
4636 &mddev->recovery);
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07004637 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004638 return -EIO;
4639 }
4640 sectors -= s;
4641 idx++;
4642 }
Matthias Kaehlcke584ed9fa2017-10-05 11:28:47 -07004643 kfree(r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004644 return 0;
4645}
4646
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004647static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004648{
Ming Leif0250612017-03-17 00:12:33 +08004649 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004650 struct mddev *mddev = r10_bio->mddev;
4651 struct r10conf *conf = mddev->private;
4652 int d;
4653 int slot;
4654 int repl;
4655 struct md_rdev *rdev = NULL;
4656
4657 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4658 if (repl)
4659 rdev = conf->mirrors[d].replacement;
4660 if (!rdev) {
4661 smp_mb();
4662 rdev = conf->mirrors[d].rdev;
4663 }
4664
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02004665 if (bio->bi_status) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004666 /* FIXME should record badblock */
4667 md_error(mddev, rdev);
4668 }
4669
4670 rdev_dec_pending(rdev, mddev);
4671 end_reshape_request(r10_bio);
4672}
4673
4674static void end_reshape_request(struct r10bio *r10_bio)
4675{
4676 if (!atomic_dec_and_test(&r10_bio->remaining))
4677 return;
4678 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4679 bio_put(r10_bio->master_bio);
4680 put_buf(r10_bio);
4681}
4682
4683static void raid10_finish_reshape(struct mddev *mddev)
4684{
4685 struct r10conf *conf = mddev->private;
4686
4687 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4688 return;
4689
4690 if (mddev->delta_disks > 0) {
4691 sector_t size = raid10_size(mddev, 0, 0);
4692 md_set_array_sectors(mddev, size);
4693 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4694 mddev->recovery_cp = mddev->resync_max_sectors;
4695 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4696 }
4697 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004698 if (mddev->queue) {
4699 set_capacity(mddev->gendisk, mddev->array_sectors);
4700 revalidate_disk(mddev->gendisk);
4701 }
NeilBrown63aced62012-05-22 13:55:33 +10004702 } else {
4703 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004704 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004705 for (d = conf->geo.raid_disks ;
4706 d < conf->geo.raid_disks - mddev->delta_disks;
4707 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004708 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004709 if (rdev)
4710 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004711 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004712 if (rdev)
4713 clear_bit(In_sync, &rdev->flags);
4714 }
NeilBrownd094d682016-06-02 16:19:52 +10004715 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004716 }
4717 mddev->layout = mddev->new_layout;
4718 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4719 mddev->reshape_position = MaxSector;
4720 mddev->delta_disks = 0;
4721 mddev->reshape_backwards = 0;
4722}
4723
NeilBrown84fc4b52011-10-11 16:49:58 +11004724static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
4726 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004727 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004729 .make_request = raid10_make_request,
4730 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004731 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004732 .status = raid10_status,
4733 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 .hot_add_disk = raid10_add_disk,
4735 .hot_remove_disk= raid10_remove_disk,
4736 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004737 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004738 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004739 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004740 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004741 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004742 .check_reshape = raid10_check_reshape,
4743 .start_reshape = raid10_start_reshape,
4744 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004745 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746};
4747
4748static int __init raid_init(void)
4749{
NeilBrown2604b702006-01-06 00:20:36 -08004750 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
4753static void raid_exit(void)
4754{
NeilBrown2604b702006-01-06 00:20:36 -08004755 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
4757
4758module_init(raid_init);
4759module_exit(raid_exit);
4760MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004761MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004763MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004764MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004765
4766module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);