blob: acb3f46f522f792a3315fdf165fa7cb67c2bd77d [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"
Christoph Hellwigef740c32009-03-31 14:27:03 +110032#include "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 Leif0250612017-03-17 00:12:33 +0800113/*
114 * 'strct resync_pages' stores actual pages used for doing the resync
115 * IO, and it is per-bio, so make .bi_private points to it.
116 */
117static inline struct resync_pages *get_resync_pages(struct bio *bio)
118{
119 return bio->bi_private;
120}
121
122/*
123 * for resync bio, r10bio pointer can be retrieved from the per-bio
124 * 'struct resync_pages'.
125 */
126static inline struct r10bio *get_resync_r10bio(struct bio *bio)
127{
128 return get_resync_pages(bio)->raid_bio;
129}
130
Al Virodd0fc662005-10-07 07:46:04 +0100131static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
NeilBrowne879a872011-10-11 16:49:02 +1100133 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100134 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
NeilBrown69335ef2011-12-23 10:17:54 +1100136 /* allocate a r10bio with room for raid_disks entries in the
137 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100138 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141static void r10bio_pool_free(void *r10_bio, void *data)
142{
143 kfree(r10_bio);
144}
145
NeilBrown0310fa22008-08-05 15:54:14 +1000146/* amount of memory to reserve for resync requests */
147#define RESYNC_WINDOW (1024*1024)
148/* maximum number of concurrent requests, memory permitting */
149#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
152 * When performing a resync, we need to read and compare, so
153 * we need as many pages are there are copies.
154 * When performing a recovery, we need 2 bios, one for read,
155 * one for write (we recover only one drive per r10buf)
156 *
157 */
Al Virodd0fc662005-10-07 07:46:04 +0100158static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
NeilBrowne879a872011-10-11 16:49:02 +1100160 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100161 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct bio *bio;
Ming Leif0250612017-03-17 00:12:33 +0800163 int j;
164 int nalloc, nalloc_rp;
165 struct resync_pages *rps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100168 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
NeilBrown3ea7daa2012-05-22 13:53:47 +1000171 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
172 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 nalloc = conf->copies; /* resync */
174 else
175 nalloc = 2; /* recovery */
176
Ming Leif0250612017-03-17 00:12:33 +0800177 /* allocate once for all bios */
178 if (!conf->have_replacement)
179 nalloc_rp = nalloc;
180 else
181 nalloc_rp = nalloc * 2;
182 rps = kmalloc(sizeof(struct resync_pages) * nalloc_rp, gfp_flags);
183 if (!rps)
184 goto out_free_r10bio;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /*
187 * Allocate bios.
188 */
189 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100190 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!bio)
192 goto out_free_bio;
193 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100194 if (!conf->have_replacement)
195 continue;
196 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
197 if (!bio)
198 goto out_free_bio;
199 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 /*
202 * Allocate RESYNC_PAGES data pages and attach them
203 * where needed.
204 */
Ming Leif0250612017-03-17 00:12:33 +0800205 for (j = 0; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100206 struct bio *rbio = r10_bio->devs[j].repl_bio;
Ming Leif0250612017-03-17 00:12:33 +0800207 struct resync_pages *rp, *rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Ming Leif0250612017-03-17 00:12:33 +0800209 rp = &rps[j];
210 if (rbio)
211 rp_repl = &rps[nalloc + j];
212
213 bio = r10_bio->devs[j].bio;
214
215 if (!j || test_bit(MD_RECOVERY_SYNC,
216 &conf->mddev->recovery)) {
217 if (resync_alloc_pages(rp, gfp_flags))
218 goto out_free_pages;
219 } else {
220 memcpy(rp, &rps[0], sizeof(*rp));
221 resync_get_all_pages(rp);
222 }
223
224 rp->idx = 0;
225 rp->raid_bio = r10_bio;
226 bio->bi_private = rp;
227 if (rbio) {
228 memcpy(rp_repl, rp, sizeof(*rp));
229 rbio->bi_private = rp_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232
233 return r10_bio;
234
235out_free_pages:
Ming Leif0250612017-03-17 00:12:33 +0800236 while (--j >= 0)
237 resync_free_pages(&rps[j * 2]);
238
majianpeng5fdd2cf2012-05-22 13:55:03 +1000239 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000241 for ( ; j < nalloc; j++) {
242 if (r10_bio->devs[j].bio)
243 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100244 if (r10_bio->devs[j].repl_bio)
245 bio_put(r10_bio->devs[j].repl_bio);
246 }
Ming Leif0250612017-03-17 00:12:33 +0800247 kfree(rps);
248out_free_r10bio:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 r10bio_pool_free(r10_bio, conf);
250 return NULL;
251}
252
253static void r10buf_pool_free(void *__r10_bio, void *data)
254{
NeilBrowne879a872011-10-11 16:49:02 +1100255 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100256 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int j;
Ming Leif0250612017-03-17 00:12:33 +0800258 struct resync_pages *rp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Ming Leif0250612017-03-17 00:12:33 +0800260 for (j = conf->copies; j--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct bio *bio = r10bio->devs[j].bio;
Ming Leif0250612017-03-17 00:12:33 +0800262
263 rp = get_resync_pages(bio);
264 resync_free_pages(rp);
265 bio_put(bio);
266
NeilBrown69335ef2011-12-23 10:17:54 +1100267 bio = r10bio->devs[j].repl_bio;
268 if (bio)
269 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Ming Leif0250612017-03-17 00:12:33 +0800271
272 /* resync pages array stored in the 1st bio's .bi_private */
273 kfree(rp);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 r10bio_pool_free(r10bio, conf);
276}
277
NeilBrowne879a872011-10-11 16:49:02 +1100278static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int i;
281
282 for (i = 0; i < conf->copies; i++) {
283 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000284 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 bio_put(*bio);
286 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100287 bio = &r10_bio->devs[i].repl_bio;
288 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
289 bio_put(*bio);
290 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292}
293
NeilBrown9f2c9d12011-10-11 16:48:43 +1100294static void free_r10bio(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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 put_all_bios(conf, r10_bio);
299 mempool_free(r10_bio, conf->r10bio_pool);
300}
301
NeilBrown9f2c9d12011-10-11 16:48:43 +1100302static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
NeilBrowne879a872011-10-11 16:49:02 +1100304 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 mempool_free(r10_bio, conf->r10buf_pool);
307
NeilBrown0a27ec92006-01-06 00:20:13 -0800308 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
NeilBrown9f2c9d12011-10-11 16:48:43 +1100311static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100314 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100315 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 spin_lock_irqsave(&conf->device_lock, flags);
318 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800319 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 spin_unlock_irqrestore(&conf->device_lock, flags);
321
Arthur Jones388667b2008-07-25 12:03:38 -0700322 /* wake up frozen array... */
323 wake_up(&conf->wait_barrier);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 md_wakeup_thread(mddev->thread);
326}
327
328/*
329 * raid_end_bio_io() is called when we have finished servicing a mirrored
330 * operation and are ready to return a success/failure code to the buffer
331 * cache layer.
332 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100333static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100336 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
NeilBrown856e08e2011-07-28 11:39:23 +1000338 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200339 bio->bi_error = -EIO;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100340
341 bio_endio(bio);
342 /*
343 * Wake up any possible resync thread that waits for the device
344 * to go idle.
345 */
346 allow_barrier(conf);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 free_r10bio(r10_bio);
349}
350
351/*
352 * Update disk head position estimator based on IRQ completion info.
353 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100354static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
NeilBrowne879a872011-10-11 16:49:02 +1100356 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
359 r10_bio->devs[slot].addr + (r10_bio->sectors);
360}
361
Namhyung Kim778ca012011-07-18 17:38:47 +1000362/*
363 * Find the disk number which triggered given bio
364 */
NeilBrowne879a872011-10-11 16:49:02 +1100365static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100366 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000367{
368 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100369 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000370
NeilBrown69335ef2011-12-23 10:17:54 +1100371 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000372 if (r10_bio->devs[slot].bio == bio)
373 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100374 if (r10_bio->devs[slot].repl_bio == bio) {
375 repl = 1;
376 break;
377 }
378 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000379
380 BUG_ON(slot == conf->copies);
381 update_head_pos(slot, r10_bio);
382
NeilBrown749c55e2011-07-28 11:39:24 +1000383 if (slotp)
384 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100385 if (replp)
386 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000387 return r10_bio->devs[slot].devnum;
388}
389
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200390static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200392 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100393 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100395 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100396 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 slot = r10_bio->read_slot;
399 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100400 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /*
402 * this branch is our 'one mirror IO has finished' event handler:
403 */
NeilBrown4443ae12006-01-06 00:20:28 -0800404 update_head_pos(slot, r10_bio);
405
406 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
408 * Set R10BIO_Uptodate in our master bio, so that
409 * we will return a good error code to the higher
410 * levels even if IO on some other mirrored buffer fails.
411 *
412 * The 'master' represents the composite IO operation to
413 * user-side. So if something waits for IO, then it will
414 * wait for the 'master' bio.
415 */
416 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc52012-02-14 11:10:10 +1100417 } else {
418 /* If all other devices that store this block have
419 * failed, we want to return the error upwards rather
420 * than fail the last device. Here we redefine
421 * "uptodate" to mean "Don't want to retry"
422 */
NeilBrown635f6412013-06-11 14:57:09 +1000423 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
424 rdev->raid_disk))
NeilBrownfae8cc52012-02-14 11:10:10 +1100425 uptodate = 1;
NeilBrownfae8cc52012-02-14 11:10:10 +1100426 }
427 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100429 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800430 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000432 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100435 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000436 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100437 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000438 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000439 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 reschedule_retry(r10_bio);
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
NeilBrown9f2c9d12011-10-11 16:48:43 +1100444static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000445{
446 /* clear the bitmap if all writes complete successfully */
447 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
448 r10_bio->sectors,
449 !test_bit(R10BIO_Degraded, &r10_bio->state),
450 0);
451 md_write_end(r10_bio->mddev);
452}
453
NeilBrown9f2c9d12011-10-11 16:48:43 +1100454static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000455{
456 if (atomic_dec_and_test(&r10_bio->remaining)) {
457 if (test_bit(R10BIO_WriteError, &r10_bio->state))
458 reschedule_retry(r10_bio);
459 else {
460 close_write(r10_bio);
461 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
462 reschedule_retry(r10_bio);
463 else
464 raid_end_bio_io(r10_bio);
465 }
466 }
467}
468
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200469static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100471 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000472 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000473 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100474 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100475 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100476 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100477 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700478 bool discard_error;
479
480 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
NeilBrown475b0322011-12-23 10:17:55 +1100482 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
NeilBrown475b0322011-12-23 10:17:55 +1100484 if (repl)
485 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100486 if (!rdev) {
487 smp_rmb();
488 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100489 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /*
492 * this branch is our 'one mirror IO has finished' event handler:
493 */
Shaohua Li579ed342016-10-06 14:13:52 -0700494 if (bio->bi_error && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100495 if (repl)
496 /* Never record new bad blocks to replacement,
497 * just fail it.
498 */
499 md_error(rdev->mddev, rdev);
500 else {
501 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100502 if (!test_and_set_bit(WantReplacement, &rdev->flags))
503 set_bit(MD_RECOVERY_NEEDED,
504 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100505
NeilBrown475b0322011-12-23 10:17:55 +1100506 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100507 if (test_bit(FailFast, &rdev->flags) &&
508 (bio->bi_opf & MD_FAILFAST)) {
509 md_error(rdev->mddev, rdev);
510 if (!test_bit(Faulty, &rdev->flags))
511 /* This is the only remaining device,
512 * We need to retry the write without
513 * FailFast
514 */
515 set_bit(R10BIO_WriteError, &r10_bio->state);
516 else {
517 r10_bio->devs[slot].bio = NULL;
518 to_put = bio;
519 dec_rdev = 1;
520 }
521 } else
522 set_bit(R10BIO_WriteError, &r10_bio->state);
NeilBrown475b0322011-12-23 10:17:55 +1100523 }
NeilBrown749c55e2011-07-28 11:39:24 +1000524 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /*
526 * Set R10BIO_Uptodate in our master bio, so that
527 * we will return a good error code for to the higher
528 * levels even if IO on some other mirrored buffer fails.
529 *
530 * The 'master' represents the composite IO operation to
531 * user-side. So if something waits for IO, then it will
532 * wait for the 'master' bio.
533 */
NeilBrown749c55e2011-07-28 11:39:24 +1000534 sector_t first_bad;
535 int bad_sectors;
536
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300537 /*
538 * Do not set R10BIO_Uptodate if the current device is
539 * rebuilding or Faulty. This is because we cannot use
540 * such device for properly reading the data back (we could
541 * potentially use it, if the current write would have felt
542 * before rdev->recovery_offset, but for simplicity we don't
543 * check this here.
544 */
545 if (test_bit(In_sync, &rdev->flags) &&
546 !test_bit(Faulty, &rdev->flags))
547 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
NeilBrown749c55e2011-07-28 11:39:24 +1000549 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100550 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000551 r10_bio->devs[slot].addr,
552 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700553 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000554 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100555 if (repl)
556 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
557 else
558 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000559 dec_rdev = 0;
560 set_bit(R10BIO_MadeGood, &r10_bio->state);
561 }
562 }
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 *
566 * Let's see if all mirrored write operations have finished
567 * already.
568 */
NeilBrown19d5f832011-09-10 17:21:17 +1000569 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000570 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100571 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100572 if (to_put)
573 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300578 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * parameters: near_copies and far_copies.
580 * near_copies * far_copies must be <= raid_disks.
581 * Normally one of these will be 1.
582 * If both are 1, we get raid0.
583 * If near_copies == raid_disks, we get raid1.
584 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300585 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * first chunk, followed by near_copies copies of the next chunk and
587 * so on.
588 * If far_copies > 1, then after 1/far_copies of the array has been assigned
589 * as described above, we start again with a device offset of near_copies.
590 * So we effectively have another copy of the whole array further down all
591 * the drives, but with blocks on different drives.
592 * With this layout, and block is never stored twice on the one device.
593 *
594 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700595 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * raid10_find_virt does the reverse mapping, from a device and a
598 * sector offset to a virtual address
599 */
600
NeilBrownf8c9e742012-05-21 09:28:33 +1000601static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int n,f;
604 sector_t sector;
605 sector_t chunk;
606 sector_t stripe;
607 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100609 int last_far_set_start, last_far_set_size;
610
611 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
612 last_far_set_start *= geo->far_set_size;
613
614 last_far_set_size = geo->far_set_size;
615 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000618 chunk = r10bio->sector >> geo->chunk_shift;
619 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
NeilBrown5cf00fc2012-05-21 09:28:20 +1000621 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000623 dev = sector_div(stripe, geo->raid_disks);
624 if (geo->far_offset)
625 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
NeilBrown5cf00fc2012-05-21 09:28:20 +1000627 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000630 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100632 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100635 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 slot++;
637
NeilBrown5cf00fc2012-05-21 09:28:20 +1000638 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100639 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000640 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100641
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100642 if ((geo->raid_disks % geo->far_set_size) &&
643 (d > last_far_set_start)) {
644 d -= last_far_set_start;
645 d %= last_far_set_size;
646 d += last_far_set_start;
647 } else {
648 d %= geo->far_set_size;
649 d += geo->far_set_size * set;
650 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000651 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 r10bio->devs[slot].devnum = d;
653 r10bio->devs[slot].addr = s;
654 slot++;
655 }
656 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000657 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000659 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000662}
663
664static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
665{
666 struct geom *geo = &conf->geo;
667
668 if (conf->reshape_progress != MaxSector &&
669 ((r10bio->sector >= conf->reshape_progress) !=
670 conf->mddev->reshape_backwards)) {
671 set_bit(R10BIO_Previous, &r10bio->state);
672 geo = &conf->prev;
673 } else
674 clear_bit(R10BIO_Previous, &r10bio->state);
675
676 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
NeilBrowne879a872011-10-11 16:49:02 +1100679static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000682 /* Never use conf->prev as this is only called during resync
683 * or recovery, so reshape isn't happening
684 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000685 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100686 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
687 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100688 int last_far_set_start;
689
690 if (geo->raid_disks % geo->far_set_size) {
691 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
692 last_far_set_start *= geo->far_set_size;
693
694 if (dev >= last_far_set_start) {
695 far_set_size = geo->far_set_size;
696 far_set_size += (geo->raid_disks % geo->far_set_size);
697 far_set_start = last_far_set_start;
698 }
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
NeilBrown5cf00fc2012-05-21 09:28:20 +1000701 offset = sector & geo->chunk_mask;
702 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700703 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000704 chunk = sector >> geo->chunk_shift;
705 fc = sector_div(chunk, geo->far_copies);
706 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100707 if (dev < far_set_start)
708 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700709 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000710 while (sector >= geo->stride) {
711 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100712 if (dev < (geo->near_copies + far_set_start))
713 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700714 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000715 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700716 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000717 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700718 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000719 vchunk = chunk * geo->raid_disks + dev;
720 sector_div(vchunk, geo->near_copies);
721 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
725 * This routine returns the disk from which the requested read should
726 * be done. There is a per-array 'next expected sequential IO' sector
727 * number - if this matches on the next IO then we use the last disk.
728 * There is also a per-disk 'last know head position' sector that is
729 * maintained from IRQ contexts, both the normal and the resync IO
730 * completion handlers update this position correctly. If there is no
731 * perfect sequential match then we pick the disk whose head is closest.
732 *
733 * If there are 2 mirrors in the same 2 devices, performance degrades
734 * because position is mirror, not device based.
735 *
736 * The rdev for the device selected will have nr_pending incremented.
737 */
738
739/*
740 * FIXME: possibly should rethink readbalancing and do it differently
741 * depending on near_copies / far_copies geometry.
742 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100743static struct md_rdev *read_balance(struct r10conf *conf,
744 struct r10bio *r10_bio,
745 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000747 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000748 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000749 int sectors = r10_bio->sectors;
750 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000751 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000752 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000753 int do_balance;
754 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000755 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 raid10_find_phys(conf, r10_bio);
758 rcu_read_lock();
NeilBrown856e08e2011-07-28 11:39:23 +1000759 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000760 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100761 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000762 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000763 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000764 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100765 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800768 * device if no resync is going on (recovery is ok), or below
769 * the resync window. We take the first readable disk when
770 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
772 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000773 && (this_sector + sectors >= conf->next_resync))
774 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
NeilBrown56d99122011-05-11 14:27:03 +1000776 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000777 sector_t first_bad;
778 int bad_sectors;
779 sector_t dev_sector;
780
NeilBrown56d99122011-05-11 14:27:03 +1000781 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000783 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100784 rdev = rcu_dereference(conf->mirrors[disk].replacement);
785 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
786 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
787 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100788 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700789 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100790 continue;
791 if (!test_bit(In_sync, &rdev->flags) &&
792 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000793 continue;
794
NeilBrown856e08e2011-07-28 11:39:23 +1000795 dev_sector = r10_bio->devs[slot].addr;
796 if (is_badblock(rdev, dev_sector, sectors,
797 &first_bad, &bad_sectors)) {
798 if (best_dist < MaxSector)
799 /* Already have a better slot */
800 continue;
801 if (first_bad <= dev_sector) {
802 /* Cannot read here. If this is the
803 * 'primary' device, then we must not read
804 * beyond 'bad_sectors' from another device.
805 */
806 bad_sectors -= (dev_sector - first_bad);
807 if (!do_balance && sectors > bad_sectors)
808 sectors = bad_sectors;
809 if (best_good_sectors > sectors)
810 best_good_sectors = sectors;
811 } else {
812 sector_t good_sectors =
813 first_bad - dev_sector;
814 if (good_sectors > best_good_sectors) {
815 best_good_sectors = good_sectors;
816 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100817 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000818 }
819 if (!do_balance)
820 /* Must read from here */
821 break;
822 }
823 continue;
824 } else
825 best_good_sectors = sectors;
826
NeilBrown56d99122011-05-11 14:27:03 +1000827 if (!do_balance)
828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
NeilBrown8d3ca832016-11-18 16:16:12 +1100830 if (best_slot >= 0)
831 /* At least 2 disks to choose from so failfast is OK */
832 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800833 /* This optimisation is debatable, and completely destroys
834 * sequential read speed for 'far copies' arrays. So only
835 * keep it for 'near' arrays, and review those later.
836 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000837 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
NeilBrown8d3ca832016-11-18 16:16:12 +1100838 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800839
840 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100841 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000842 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800843 else
NeilBrown56d99122011-05-11 14:27:03 +1000844 new_distance = abs(r10_bio->devs[slot].addr -
845 conf->mirrors[disk].head_position);
846 if (new_distance < best_dist) {
847 best_dist = new_distance;
848 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100849 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 }
NeilBrownabbf0982011-12-23 10:17:54 +1100852 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000853 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100854 rdev = best_rdev;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
NeilBrown56d99122011-05-11 14:27:03 +1000857 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000858 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000859 r10_bio->read_slot = slot;
860 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100861 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000863 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
NeilBrown96c3fd12011-12-23 10:17:54 +1100865 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
NeilBrown5c675f82014-12-15 12:56:56 +1100868static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700869{
NeilBrowne879a872011-10-11 16:49:02 +1100870 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700871 int i, ret = 0;
872
Tejun Heo44522262015-05-22 17:13:26 -0400873 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100874 conf->pending_count >= max_queued_requests)
875 return 1;
876
NeilBrown0d129222006-10-03 01:15:54 -0700877 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000878 for (i = 0;
879 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
880 && ret == 0;
881 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100882 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700883 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200884 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700885
Jan Karadc3b17c2017-02-02 15:56:50 +0100886 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700887 }
888 }
889 rcu_read_unlock();
890 return ret;
891}
892
NeilBrowne879a872011-10-11 16:49:02 +1100893static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800894{
895 /* Any writes that have been queued but are awaiting
896 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800897 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800898 spin_lock_irq(&conf->device_lock);
899
900 if (conf->pending_bio_list.head) {
901 struct bio *bio;
902 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100903 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800904 spin_unlock_irq(&conf->device_lock);
905 /* flush any pending bitmap writes to disk
906 * before proceeding w/ I/O */
907 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100908 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800909
910 while (bio) { /* submit pending writes */
911 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100912 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800913 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100914 bio->bi_bdev = rdev->bdev;
915 if (test_bit(Faulty, &rdev->flags)) {
916 bio->bi_error = -EIO;
917 bio_endio(bio);
918 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
919 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li532a2a32012-10-11 13:30:52 +1100920 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200921 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100922 else
923 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800924 bio = next;
925 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800926 } else
927 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800928}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100929
NeilBrown0a27ec92006-01-06 00:20:13 -0800930/* Barriers....
931 * Sometimes we need to suspend IO while we do something else,
932 * either some resync/recovery, or reconfigure the array.
933 * To do this we raise a 'barrier'.
934 * The 'barrier' is a counter that can be raised multiple times
935 * to count how many activities are happening which preclude
936 * normal IO.
937 * We can only raise the barrier if there is no pending IO.
938 * i.e. if nr_pending == 0.
939 * We choose only to raise the barrier if no-one is waiting for the
940 * barrier to go down. This means that as soon as an IO request
941 * is ready, no other operations which require a barrier will start
942 * until the IO request has had a chance.
943 *
944 * So: regular IO calls 'wait_barrier'. When that returns there
945 * is no backgroup IO happening, It must arrange to call
946 * allow_barrier when it has finished its IO.
947 * backgroup IO calls must call raise_barrier. Once that returns
948 * there is no normal IO happeing. It must arrange to call
949 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
NeilBrowne879a872011-10-11 16:49:02 +1100952static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
NeilBrown6cce3b22006-01-06 00:20:16 -0800954 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
NeilBrown6cce3b22006-01-06 00:20:16 -0800957 /* Wait until no block IO is waiting (unless 'force') */
958 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100959 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800960
961 /* block any new IO from starting */
962 conf->barrier++;
963
NeilBrownc3b328a2011-04-18 18:25:43 +1000964 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800965 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200966 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100967 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 spin_unlock_irq(&conf->resync_lock);
970}
971
NeilBrowne879a872011-10-11 16:49:02 +1100972static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800973{
974 unsigned long flags;
975 spin_lock_irqsave(&conf->resync_lock, flags);
976 conf->barrier--;
977 spin_unlock_irqrestore(&conf->resync_lock, flags);
978 wake_up(&conf->wait_barrier);
979}
980
NeilBrowne879a872011-10-11 16:49:02 +1100981static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800982{
983 spin_lock_irq(&conf->resync_lock);
984 if (conf->barrier) {
985 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100986 /* Wait for the barrier to drop.
987 * However if there are already pending
988 * requests (preventing the barrier from
989 * rising completely), and the
990 * pre-process bio queue isn't empty,
991 * then don't wait, as we need to empty
992 * that queue to get the nr_pending
993 * count down.
994 */
NeilBrown578b54a2016-11-14 16:30:21 +1100995 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100996 wait_event_lock_irq(conf->wait_barrier,
997 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200998 (atomic_read(&conf->nr_pending) &&
NeilBrownd6b42dc2012-03-19 12:46:38 +1100999 current->bio_list &&
NeilBrownf5fe1b52017-03-10 17:00:47 +11001000 (!bio_list_empty(&current->bio_list[0]) ||
1001 !bio_list_empty(&current->bio_list[1]))),
Lukas Czernereed8c022012-11-30 11:42:40 +01001002 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08001003 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001004 if (!conf->nr_waiting)
1005 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -08001006 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001007 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -08001008 spin_unlock_irq(&conf->resync_lock);
1009}
1010
NeilBrowne879a872011-10-11 16:49:02 +11001011static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001012{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001013 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1014 (conf->array_freeze_pending))
1015 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -08001016}
1017
NeilBrowne2d59922013-06-12 11:01:22 +10001018static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -08001019{
1020 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001021 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001022 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001023 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001024 * This is called in the context of one normal IO request
1025 * that has failed. Thus any sync request that might be pending
1026 * will be blocked by nr_pending, and we need to wait for
1027 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001028 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001029 * must match the number of pending IOs (nr_pending) before
1030 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001031 */
1032 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001033 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001034 conf->barrier++;
1035 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001036 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001037 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001038 conf->resync_lock,
1039 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001040
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001041 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001042 spin_unlock_irq(&conf->resync_lock);
1043}
1044
NeilBrowne879a872011-10-11 16:49:02 +11001045static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001046{
1047 /* reverse the effect of the freeze */
1048 spin_lock_irq(&conf->resync_lock);
1049 conf->barrier--;
1050 conf->nr_waiting--;
1051 wake_up(&conf->wait_barrier);
1052 spin_unlock_irq(&conf->resync_lock);
1053}
1054
NeilBrownf8c9e742012-05-21 09:28:33 +10001055static sector_t choose_data_offset(struct r10bio *r10_bio,
1056 struct md_rdev *rdev)
1057{
1058 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1059 test_bit(R10BIO_Previous, &r10_bio->state))
1060 return rdev->data_offset;
1061 else
1062 return rdev->new_data_offset;
1063}
1064
NeilBrown57c67df2012-10-11 13:32:13 +11001065struct raid10_plug_cb {
1066 struct blk_plug_cb cb;
1067 struct bio_list pending;
1068 int pending_cnt;
1069};
1070
1071static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1072{
1073 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1074 cb);
1075 struct mddev *mddev = plug->cb.data;
1076 struct r10conf *conf = mddev->private;
1077 struct bio *bio;
1078
NeilBrown874807a2012-11-27 12:14:40 +11001079 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001080 spin_lock_irq(&conf->device_lock);
1081 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1082 conf->pending_count += plug->pending_cnt;
1083 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001084 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001085 md_wakeup_thread(mddev->thread);
1086 kfree(plug);
1087 return;
1088 }
1089
1090 /* we aren't scheduling, so we can do the write-out directly. */
1091 bio = bio_list_get(&plug->pending);
1092 bitmap_unplug(mddev->bitmap);
1093 wake_up(&conf->wait_barrier);
1094
1095 while (bio) { /* submit pending writes */
1096 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001097 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001098 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001099 bio->bi_bdev = rdev->bdev;
1100 if (test_bit(Faulty, &rdev->flags)) {
1101 bio->bi_error = -EIO;
1102 bio_endio(bio);
1103 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1104 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001105 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001106 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001107 else
1108 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001109 bio = next;
1110 }
1111 kfree(plug);
1112}
1113
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001114static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1115 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
NeilBrowne879a872011-10-11 16:49:02 +11001117 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001119 const int op = bio_op(bio);
1120 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001121 int max_sectors;
1122 sector_t sectors;
1123 struct md_rdev *rdev;
NeilBrown545250f2017-04-05 14:05:51 +10001124 char b[BDEVNAME_SIZE];
1125 int slot = r10_bio->read_slot;
1126 struct md_rdev *err_rdev = NULL;
1127 gfp_t gfp = GFP_NOIO;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001128
NeilBrown545250f2017-04-05 14:05:51 +10001129 if (r10_bio->devs[slot].rdev) {
1130 /*
1131 * This is an error retry, but we cannot
1132 * safely dereference the rdev in the r10_bio,
1133 * we must use the one in conf.
1134 * If it has already been disconnected (unlikely)
1135 * we lose the device name in error messages.
1136 */
1137 int disk;
1138 /*
1139 * As we are blocking raid10, it is a little safer to
1140 * use __GFP_HIGH.
1141 */
1142 gfp = GFP_NOIO | __GFP_HIGH;
1143
1144 rcu_read_lock();
1145 disk = r10_bio->devs[slot].devnum;
1146 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1147 if (err_rdev)
1148 bdevname(err_rdev->bdev, b);
1149 else {
1150 strcpy(b, "???");
1151 /* This never gets dereferenced */
1152 err_rdev = r10_bio->devs[slot].rdev;
1153 }
1154 rcu_read_unlock();
1155 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001156 /*
1157 * Register the new request and wait if the reconstruction
1158 * thread has put up a bar for new requests.
1159 * Continue immediately if no resync is active currently.
1160 */
1161 wait_barrier(conf);
1162
NeilBrownfc9977d2017-04-05 14:05:51 +10001163 sectors = r10_bio->sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001164 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1165 bio->bi_iter.bi_sector < conf->reshape_progress &&
1166 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1167 /*
1168 * IO spans the reshape position. Need to wait for reshape to
1169 * pass
1170 */
1171 raid10_log(conf->mddev, "wait reshape");
1172 allow_barrier(conf);
1173 wait_event(conf->wait_barrier,
1174 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1175 conf->reshape_progress >= bio->bi_iter.bi_sector +
1176 sectors);
1177 wait_barrier(conf);
1178 }
1179
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001180 rdev = read_balance(conf, r10_bio, &max_sectors);
1181 if (!rdev) {
NeilBrown545250f2017-04-05 14:05:51 +10001182 if (err_rdev) {
1183 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1184 mdname(mddev), b,
1185 (unsigned long long)r10_bio->sector);
1186 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001187 raid_end_bio_io(r10_bio);
1188 return;
1189 }
NeilBrown545250f2017-04-05 14:05:51 +10001190 if (err_rdev)
1191 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1192 mdname(mddev),
1193 bdevname(rdev->bdev, b),
1194 (unsigned long long)r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001195 if (max_sectors < bio_sectors(bio)) {
1196 struct bio *split = bio_split(bio, max_sectors,
NeilBrown545250f2017-04-05 14:05:51 +10001197 gfp, conf->bio_split);
NeilBrownfc9977d2017-04-05 14:05:51 +10001198 bio_chain(split, bio);
1199 generic_make_request(bio);
1200 bio = split;
1201 r10_bio->master_bio = bio;
1202 r10_bio->sectors = max_sectors;
1203 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001204 slot = r10_bio->read_slot;
1205
NeilBrown545250f2017-04-05 14:05:51 +10001206 read_bio = bio_clone_fast(bio, gfp, mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001207
1208 r10_bio->devs[slot].bio = read_bio;
1209 r10_bio->devs[slot].rdev = rdev;
1210
1211 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1212 choose_data_offset(r10_bio, rdev);
1213 read_bio->bi_bdev = rdev->bdev;
1214 read_bio->bi_end_io = raid10_end_read_request;
1215 bio_set_op_attrs(read_bio, op, do_sync);
1216 if (test_bit(FailFast, &rdev->flags) &&
1217 test_bit(R10BIO_FailFast, &r10_bio->state))
1218 read_bio->bi_opf |= MD_FAILFAST;
1219 read_bio->bi_private = r10_bio;
1220
1221 if (mddev->gendisk)
1222 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1223 read_bio, disk_devt(mddev->gendisk),
1224 r10_bio->sector);
NeilBrownfc9977d2017-04-05 14:05:51 +10001225 generic_make_request(read_bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001226 return;
1227}
1228
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001229static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1230 struct bio *bio, bool replacement,
NeilBrownfc9977d2017-04-05 14:05:51 +10001231 int n_copy)
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001232{
1233 const int op = bio_op(bio);
1234 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1235 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1236 unsigned long flags;
1237 struct blk_plug_cb *cb;
1238 struct raid10_plug_cb *plug = NULL;
1239 struct r10conf *conf = mddev->private;
1240 struct md_rdev *rdev;
1241 int devnum = r10_bio->devs[n_copy].devnum;
1242 struct bio *mbio;
1243
1244 if (replacement) {
1245 rdev = conf->mirrors[devnum].replacement;
1246 if (rdev == NULL) {
1247 /* Replacement just got moved to main 'rdev' */
1248 smp_mb();
1249 rdev = conf->mirrors[devnum].rdev;
1250 }
1251 } else
1252 rdev = conf->mirrors[devnum].rdev;
1253
1254 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001255 if (replacement)
1256 r10_bio->devs[n_copy].repl_bio = mbio;
1257 else
1258 r10_bio->devs[n_copy].bio = mbio;
1259
1260 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1261 choose_data_offset(r10_bio, rdev));
1262 mbio->bi_bdev = rdev->bdev;
1263 mbio->bi_end_io = raid10_end_write_request;
1264 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1265 if (!replacement && test_bit(FailFast,
1266 &conf->mirrors[devnum].rdev->flags)
1267 && enough(conf, devnum))
1268 mbio->bi_opf |= MD_FAILFAST;
1269 mbio->bi_private = r10_bio;
1270
1271 if (conf->mddev->gendisk)
1272 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1273 mbio, disk_devt(conf->mddev->gendisk),
1274 r10_bio->sector);
1275 /* flush_pending_writes() needs access to the rdev so...*/
1276 mbio->bi_bdev = (void *)rdev;
1277
1278 atomic_inc(&r10_bio->remaining);
1279
1280 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1281 if (cb)
1282 plug = container_of(cb, struct raid10_plug_cb, cb);
1283 else
1284 plug = NULL;
1285 spin_lock_irqsave(&conf->device_lock, flags);
1286 if (plug) {
1287 bio_list_add(&plug->pending, mbio);
1288 plug->pending_cnt++;
1289 } else {
1290 bio_list_add(&conf->pending_bio_list, mbio);
1291 conf->pending_count++;
1292 }
1293 spin_unlock_irqrestore(&conf->device_lock, flags);
1294 if (!plug)
1295 md_wakeup_thread(mddev->thread);
1296}
1297
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001298static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1299 struct r10bio *r10_bio)
1300{
1301 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11001303 struct md_rdev *blocked_rdev;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001304 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001305 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Tomasz Majchrzak9b622e22016-07-28 10:28:25 +02001307 md_write_start(mddev, bio);
1308
NeilBrowncc13b1d2014-05-05 13:34:37 +10001309 /*
1310 * Register the new request and wait if the reconstruction
1311 * thread has put up a bar for new requests.
1312 * Continue immediately if no resync is active currently.
1313 */
1314 wait_barrier(conf);
1315
NeilBrownfc9977d2017-04-05 14:05:51 +10001316 sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001317 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001318 bio->bi_iter.bi_sector < conf->reshape_progress &&
1319 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001320 /*
1321 * IO spans the reshape position. Need to wait for reshape to
1322 * pass
NeilBrown3ea7daa2012-05-22 13:53:47 +10001323 */
NeilBrown578b54a2016-11-14 16:30:21 +11001324 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001325 allow_barrier(conf);
1326 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001327 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1328 conf->reshape_progress >= bio->bi_iter.bi_sector +
1329 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001330 wait_barrier(conf);
1331 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001332
NeilBrown3ea7daa2012-05-22 13:53:47 +10001333 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001334 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001335 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1336 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1337 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1338 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001339 /* Need to update reshape_position in metadata */
1340 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001341 set_mask_bits(&mddev->sb_flags, 0,
1342 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001343 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001344 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001345 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001346 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001347
1348 conf->reshape_safe = mddev->reshape_position;
1349 }
1350
NeilBrown34db0cd2011-10-11 16:50:01 +11001351 if (conf->pending_count >= max_queued_requests) {
1352 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001353 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001354 wait_event(conf->wait_barrier,
1355 conf->pending_count < max_queued_requests);
1356 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001357 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * inc refcount on their rdev. Record them by setting
1359 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001360 * If there are known/acknowledged bad blocks on any device
1361 * on which we have seen a write error, we want to avoid
1362 * writing to those blocks. This potentially requires several
1363 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001364 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001366
NeilBrown69335ef2011-12-23 10:17:54 +11001367 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001369retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001370 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001372 max_sectors = r10_bio->sectors;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 for (i = 0; i < conf->copies; i++) {
1375 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001376 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001377 struct md_rdev *rrdev = rcu_dereference(
1378 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001379 if (rdev == rrdev)
1380 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001381 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1382 atomic_inc(&rdev->nr_pending);
1383 blocked_rdev = rdev;
1384 break;
1385 }
NeilBrown475b0322011-12-23 10:17:55 +11001386 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1387 atomic_inc(&rrdev->nr_pending);
1388 blocked_rdev = rrdev;
1389 break;
1390 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001391 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001392 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001393 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001394 rrdev = NULL;
1395
NeilBrownd4432c22011-07-28 11:39:24 +10001396 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001397 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001398
1399 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001400 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001401 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001402 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001403 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001404 sector_t first_bad;
1405 sector_t dev_sector = r10_bio->devs[i].addr;
1406 int bad_sectors;
1407 int is_bad;
1408
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001409 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001410 &first_bad, &bad_sectors);
1411 if (is_bad < 0) {
1412 /* Mustn't write here until the bad block
1413 * is acknowledged
1414 */
1415 atomic_inc(&rdev->nr_pending);
1416 set_bit(BlockedBadBlocks, &rdev->flags);
1417 blocked_rdev = rdev;
1418 break;
1419 }
1420 if (is_bad && first_bad <= dev_sector) {
1421 /* Cannot write here at all */
1422 bad_sectors -= (dev_sector - first_bad);
1423 if (bad_sectors < max_sectors)
1424 /* Mustn't write more than bad_sectors
1425 * to other devices yet
1426 */
1427 max_sectors = bad_sectors;
1428 /* We don't set R10BIO_Degraded as that
1429 * only applies if the disk is missing,
1430 * so it might be re-added, and we want to
1431 * know to recover this chunk.
1432 * In this case the device is here, and the
1433 * fact that this chunk is not in-sync is
1434 * recorded in the bad block log.
1435 */
1436 continue;
1437 }
1438 if (is_bad) {
1439 int good_sectors = first_bad - dev_sector;
1440 if (good_sectors < max_sectors)
1441 max_sectors = good_sectors;
1442 }
1443 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001444 if (rdev) {
1445 r10_bio->devs[i].bio = bio;
1446 atomic_inc(&rdev->nr_pending);
1447 }
NeilBrown475b0322011-12-23 10:17:55 +11001448 if (rrdev) {
1449 r10_bio->devs[i].repl_bio = bio;
1450 atomic_inc(&rrdev->nr_pending);
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 rcu_read_unlock();
1454
Dan Williams6bfe0b42008-04-30 00:52:32 -07001455 if (unlikely(blocked_rdev)) {
1456 /* Have to wait for this device to get unblocked, then retry */
1457 int j;
1458 int d;
1459
NeilBrown475b0322011-12-23 10:17:55 +11001460 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001461 if (r10_bio->devs[j].bio) {
1462 d = r10_bio->devs[j].devnum;
1463 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1464 }
NeilBrown475b0322011-12-23 10:17:55 +11001465 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001466 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001467 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001468 rdev = conf->mirrors[d].replacement;
1469 if (!rdev) {
1470 /* Race with remove_disk */
1471 smp_mb();
1472 rdev = conf->mirrors[d].rdev;
1473 }
1474 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001475 }
1476 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001477 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001478 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001479 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1480 wait_barrier(conf);
1481 goto retry_write;
1482 }
1483
NeilBrown6b6c8112017-03-15 14:05:13 +11001484 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001485 r10_bio->sectors = max_sectors;
NeilBrownfc9977d2017-04-05 14:05:51 +10001486
1487 if (r10_bio->sectors < bio_sectors(bio)) {
1488 struct bio *split = bio_split(bio, r10_bio->sectors,
1489 GFP_NOIO, conf->bio_split);
1490 bio_chain(split, bio);
1491 generic_make_request(bio);
1492 bio = split;
1493 r10_bio->master_bio = bio;
1494 }
NeilBrownd4432c22011-07-28 11:39:24 +10001495
NeilBrown4e780642010-10-19 12:54:01 +11001496 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001497 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 for (i = 0; i < conf->copies; i++) {
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001500 if (r10_bio->devs[i].bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001501 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001502 if (r10_bio->devs[i].repl_bio)
NeilBrownfc9977d2017-04-05 14:05:51 +10001503 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
NeilBrownd4432c22011-07-28 11:39:24 +10001504 }
NeilBrown079fa162011-09-10 17:21:23 +10001505 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001506}
1507
NeilBrownfc9977d2017-04-05 14:05:51 +10001508static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001509{
1510 struct r10conf *conf = mddev->private;
1511 struct r10bio *r10_bio;
1512
1513 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1514
1515 r10_bio->master_bio = bio;
NeilBrownfc9977d2017-04-05 14:05:51 +10001516 r10_bio->sectors = sectors;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001517
1518 r10_bio->mddev = mddev;
1519 r10_bio->sector = bio->bi_iter.bi_sector;
1520 r10_bio->state = 0;
NeilBrown545250f2017-04-05 14:05:51 +10001521 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001522
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001523 if (bio_data_dir(bio) == READ)
1524 raid10_read_request(mddev, bio, r10_bio);
1525 else
1526 raid10_write_request(mddev, bio, r10_bio);
1527}
1528
Shaohua Li849674e2016-01-20 13:52:20 -08001529static void raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001530{
1531 struct r10conf *conf = mddev->private;
1532 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1533 int chunk_sects = chunk_mask + 1;
NeilBrownfc9977d2017-04-05 14:05:51 +10001534 int sectors = bio_sectors(bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001535
Jens Axboe1eff9d32016-08-05 15:35:16 -06001536 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001537 md_flush_request(mddev, bio);
1538 return;
1539 }
1540
NeilBrownfc9977d2017-04-05 14:05:51 +10001541 /*
1542 * If this request crosses a chunk boundary, we need to split
1543 * it.
1544 */
1545 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1546 sectors > chunk_sects
1547 && (conf->geo.near_copies < conf->geo.raid_disks
1548 || conf->prev.near_copies <
1549 conf->prev.raid_disks)))
1550 sectors = chunk_sects -
1551 (bio->bi_iter.bi_sector &
1552 (chunk_sects - 1));
1553 __make_request(mddev, bio, sectors);
NeilBrown079fa162011-09-10 17:21:23 +10001554
1555 /* In case raid10d snuck in to freeze_array */
1556 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Shaohua Li849674e2016-01-20 13:52:20 -08001559static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
NeilBrowne879a872011-10-11 16:49:02 +11001561 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int i;
1563
NeilBrown5cf00fc2012-05-21 09:28:20 +10001564 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001565 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001566 if (conf->geo.near_copies > 1)
1567 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1568 if (conf->geo.far_copies > 1) {
1569 if (conf->geo.far_offset)
1570 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001571 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001572 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001573 if (conf->geo.far_set_size != conf->geo.raid_disks)
1574 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001575 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001576 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1577 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001578 rcu_read_lock();
1579 for (i = 0; i < conf->geo.raid_disks; i++) {
1580 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1581 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1582 }
1583 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 seq_printf(seq, "]");
1585}
1586
NeilBrown700c7212011-07-27 11:00:36 +10001587/* check if there are enough drives for
1588 * every block to appear on atleast one.
1589 * Don't consider the device numbered 'ignore'
1590 * as we might be about to remove it.
1591 */
NeilBrown635f6412013-06-11 14:57:09 +10001592static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001593{
1594 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001595 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001596 int disks, ncopies;
1597 if (previous) {
1598 disks = conf->prev.raid_disks;
1599 ncopies = conf->prev.near_copies;
1600 } else {
1601 disks = conf->geo.raid_disks;
1602 ncopies = conf->geo.near_copies;
1603 }
NeilBrown700c7212011-07-27 11:00:36 +10001604
NeilBrown725d6e52013-06-11 15:08:03 +10001605 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001606 do {
1607 int n = conf->copies;
1608 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001609 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001610 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001611 struct md_rdev *rdev;
1612 if (this != ignore &&
1613 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1614 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001615 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001616 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001617 }
1618 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001619 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001620 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001621 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001622 has_enough = 1;
1623out:
1624 rcu_read_unlock();
1625 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001626}
1627
NeilBrownf8c9e742012-05-21 09:28:33 +10001628static int enough(struct r10conf *conf, int ignore)
1629{
NeilBrown635f6412013-06-11 14:57:09 +10001630 /* when calling 'enough', both 'prev' and 'geo' must
1631 * be stable.
1632 * This is ensured if ->reconfig_mutex or ->device_lock
1633 * is held.
1634 */
1635 return _enough(conf, 0, ignore) &&
1636 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001637}
1638
Shaohua Li849674e2016-01-20 13:52:20 -08001639static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001642 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001643 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /*
1646 * If it is not operational, then we have already marked it as dead
1647 * else if it is the last working disks, ignore the error, let the
1648 * next level up know.
1649 * else mark the drive as failed
1650 */
NeilBrown635f6412013-06-11 14:57:09 +10001651 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001652 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001653 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /*
1655 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001657 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001658 return;
1659 }
NeilBrown2446dba2014-07-31 10:16:29 +10001660 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001661 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001662 /*
1663 * If recovery is running, make sure it aborts.
1664 */
1665 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001666 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001667 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001668 set_mask_bits(&mddev->sb_flags, 0,
1669 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001670 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001671 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1672 "md/raid10:%s: Operation continuing on %d devices.\n",
1673 mdname(mddev), bdevname(rdev->bdev, b),
1674 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
NeilBrowne879a872011-10-11 16:49:02 +11001677static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001680 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
NeilBrown08464e02016-11-02 14:16:50 +11001682 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001684 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return;
1686 }
NeilBrown08464e02016-11-02 14:16:50 +11001687 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1688 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
NeilBrown4056ca52016-06-02 16:19:52 +10001690 /* This is only called with ->reconfix_mutex held, so
1691 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001692 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001694 rdev = conf->mirrors[i].rdev;
1695 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001696 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1697 i, !test_bit(In_sync, &rdev->flags),
1698 !test_bit(Faulty, &rdev->flags),
1699 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701}
1702
NeilBrowne879a872011-10-11 16:49:02 +11001703static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
NeilBrown0a27ec92006-01-06 00:20:13 -08001705 wait_barrier(conf);
1706 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 mempool_destroy(conf->r10buf_pool);
1709 conf->r10buf_pool = NULL;
1710}
1711
NeilBrownfd01b882011-10-11 16:47:53 +11001712static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001715 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001716 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001717 int count = 0;
1718 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /*
1721 * Find all non-in_sync disks within the RAID10 configuration
1722 * and mark them in_sync
1723 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001724 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001726 if (tmp->replacement
1727 && tmp->replacement->recovery_offset == MaxSector
1728 && !test_bit(Faulty, &tmp->replacement->flags)
1729 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1730 /* Replacement has just become active */
1731 if (!tmp->rdev
1732 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1733 count++;
1734 if (tmp->rdev) {
1735 /* Replaced device not technically faulty,
1736 * but we need to be sure it gets removed
1737 * and never re-added.
1738 */
1739 set_bit(Faulty, &tmp->rdev->flags);
1740 sysfs_notify_dirent_safe(
1741 tmp->rdev->sysfs_state);
1742 }
1743 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1744 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001745 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001746 && !test_bit(Faulty, &tmp->rdev->flags)
1747 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001748 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001749 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751 }
NeilBrown6b965622010-08-18 11:56:59 +10001752 spin_lock_irqsave(&conf->device_lock, flags);
1753 mddev->degraded -= count;
1754 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001757 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
NeilBrownfd01b882011-10-11 16:47:53 +11001760static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
NeilBrowne879a872011-10-11 16:49:02 +11001762 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001763 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001765 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001766 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 if (mddev->recovery_cp < MaxSector)
1769 /* only hot-add to in-sync arrays, as recovery is
1770 * very different from resync
1771 */
Neil Brown199050e2008-06-28 08:31:33 +10001772 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001773 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Dan Williams1501efa2016-01-13 16:00:07 -08001776 if (md_integrity_add_rdev(rdev, mddev))
1777 return -ENXIO;
1778
NeilBrowna53a6c82008-11-06 17:28:20 +11001779 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001780 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001782 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001783 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1784 mirror = rdev->saved_raid_disk;
1785 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001786 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001787 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001788 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001789 if (p->recovery_disabled == mddev->recovery_disabled)
1790 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001791 if (p->rdev) {
1792 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1793 p->replacement != NULL)
1794 continue;
1795 clear_bit(In_sync, &rdev->flags);
1796 set_bit(Replacement, &rdev->flags);
1797 rdev->raid_disk = mirror;
1798 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001799 if (mddev->gendisk)
1800 disk_stack_limits(mddev->gendisk, rdev->bdev,
1801 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001802 conf->fullsync = 1;
1803 rcu_assign_pointer(p->replacement, rdev);
1804 break;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Jonathan Brassow9092c022013-05-02 14:19:24 -05001807 if (mddev->gendisk)
1808 disk_stack_limits(mddev->gendisk, rdev->bdev,
1809 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
NeilBrown2bb77732011-07-27 11:00:36 +10001811 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001812 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001813 rdev->raid_disk = mirror;
1814 err = 0;
1815 if (rdev->saved_raid_disk != mirror)
1816 conf->fullsync = 1;
1817 rcu_assign_pointer(p->rdev, rdev);
1818 break;
1819 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11001820 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001821 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
NeilBrownb8321b62011-12-23 10:17:51 +11001827static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
NeilBrowne879a872011-10-11 16:49:02 +11001829 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001831 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001832 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001833 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001836 if (rdev == p->rdev)
1837 rdevp = &p->rdev;
1838 else if (rdev == p->replacement)
1839 rdevp = &p->replacement;
1840 else
1841 return 0;
1842
1843 if (test_bit(In_sync, &rdev->flags) ||
1844 atomic_read(&rdev->nr_pending)) {
1845 err = -EBUSY;
1846 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
NeilBrownd787be42016-06-02 16:19:53 +10001848 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11001849 * is not possible.
1850 */
1851 if (!test_bit(Faulty, &rdev->flags) &&
1852 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001853 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001854 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001855 enough(conf, -1)) {
1856 err = -EBUSY;
1857 goto abort;
1858 }
1859 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001860 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1861 synchronize_rcu();
1862 if (atomic_read(&rdev->nr_pending)) {
1863 /* lost the race, try later */
1864 err = -EBUSY;
1865 *rdevp = rdev;
1866 goto abort;
1867 }
1868 }
1869 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001870 /* We must have just cleared 'rdev' */
1871 p->rdev = p->replacement;
1872 clear_bit(Replacement, &p->replacement->flags);
1873 smp_mb(); /* Make sure other CPUs may see both as identical
1874 * but will never see neither -- if they are careful.
1875 */
1876 p->replacement = NULL;
1877 clear_bit(WantReplacement, &rdev->flags);
1878 } else
1879 /* We might have just remove the Replacement as faulty
1880 * Clear the flag just in case
1881 */
1882 clear_bit(WantReplacement, &rdev->flags);
1883
NeilBrownc8ab9032011-12-23 10:17:54 +11001884 err = md_integrity_register(mddev);
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886abort:
1887
1888 print_conf(conf);
1889 return err;
1890}
1891
Ming Lei81fa1522017-03-17 00:12:32 +08001892static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
NeilBrowne879a872011-10-11 16:49:02 +11001894 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001895
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001896 if (!bio->bi_error)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001897 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001898 else
1899 /* The write handler will notice the lack of
1900 * R10BIO_Uptodate and record any errors etc
1901 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001902 atomic_add(r10_bio->sectors,
1903 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /* for reconstruct, we always reschedule after a read.
1906 * for resync, only after all reads
1907 */
NeilBrown73d5c382009-02-25 13:18:47 +11001908 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1910 atomic_dec_and_test(&r10_bio->remaining)) {
1911 /* we have read all the blocks,
1912 * do the comparison in process context in raid10d
1913 */
1914 reschedule_retry(r10_bio);
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Ming Lei81fa1522017-03-17 00:12:32 +08001918static void end_sync_read(struct bio *bio)
1919{
Ming Leif0250612017-03-17 00:12:33 +08001920 struct r10bio *r10_bio = get_resync_r10bio(bio);
Ming Lei81fa1522017-03-17 00:12:32 +08001921 struct r10conf *conf = r10_bio->mddev->private;
1922 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1923
1924 __end_sync_read(r10_bio, bio, d);
1925}
1926
1927static void end_reshape_read(struct bio *bio)
1928{
Ming Leif0250612017-03-17 00:12:33 +08001929 /* reshape read bio isn't allocated from r10buf_pool */
Ming Lei81fa1522017-03-17 00:12:32 +08001930 struct r10bio *r10_bio = bio->bi_private;
1931
1932 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1933}
1934
NeilBrown9f2c9d12011-10-11 16:48:43 +11001935static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001936{
NeilBrownfd01b882011-10-11 16:47:53 +11001937 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001938
1939 while (atomic_dec_and_test(&r10_bio->remaining)) {
1940 if (r10_bio->master_bio == NULL) {
1941 /* the primary of several recovery bios */
1942 sector_t s = r10_bio->sectors;
1943 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1944 test_bit(R10BIO_WriteError, &r10_bio->state))
1945 reschedule_retry(r10_bio);
1946 else
1947 put_buf(r10_bio);
1948 md_done_sync(mddev, s, 1);
1949 break;
1950 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001951 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001952 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1953 test_bit(R10BIO_WriteError, &r10_bio->state))
1954 reschedule_retry(r10_bio);
1955 else
1956 put_buf(r10_bio);
1957 r10_bio = r10_bio2;
1958 }
1959 }
1960}
1961
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001962static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Ming Leif0250612017-03-17 00:12:33 +08001964 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrownfd01b882011-10-11 16:47:53 +11001965 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001966 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001967 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001968 sector_t first_bad;
1969 int bad_sectors;
1970 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001971 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001972 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
NeilBrown9ad1aef2011-12-23 10:17:55 +11001974 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1975 if (repl)
1976 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001977 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001978 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001980 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001981 if (repl)
1982 md_error(mddev, rdev);
1983 else {
1984 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001985 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1986 set_bit(MD_RECOVERY_NEEDED,
1987 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001988 set_bit(R10BIO_WriteError, &r10_bio->state);
1989 }
1990 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001991 r10_bio->devs[slot].addr,
1992 r10_bio->sectors,
1993 &first_bad, &bad_sectors))
1994 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001995
NeilBrown9ad1aef2011-12-23 10:17:55 +11001996 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001997
1998 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001/*
2002 * Note: sync and recover and handled very differently for raid10
2003 * This code is for resync.
2004 * For resync, we read through virtual addresses and read all blocks.
2005 * If there is any error, we schedule a write. The lowest numbered
2006 * drive is authoritative.
2007 * However requests come for physical address, so we need to map.
2008 * For every physical address there are raid_disks/copies virtual addresses,
2009 * which is always are least one, but is not necessarly an integer.
2010 * This means that a physical address can span multiple chunks, so we may
2011 * have to submit multiple io requests for a single sync request.
2012 */
2013/*
2014 * We check if all blocks are in-sync and only write to blocks that
2015 * aren't in sync
2016 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002017static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
NeilBrowne879a872011-10-11 16:49:02 +11002019 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int i, first;
2021 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002022 int vcnt;
Ming Leicdb76be2017-03-17 00:12:34 +08002023 struct page **tpages, **fpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 atomic_set(&r10_bio->remaining, 1);
2026
2027 /* find the first device with a block */
2028 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002029 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031
2032 if (i == conf->copies)
2033 goto done;
2034
2035 first = i;
2036 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002037 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2038 fbio->bi_iter.bi_idx = 0;
Ming Leicdb76be2017-03-17 00:12:34 +08002039 fpages = get_resync_pages(fbio)->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
majianpengf4380a92012-04-12 16:04:47 +10002041 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002043 for (i=0 ; i < conf->copies ; i++) {
2044 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002045 struct md_rdev *rdev;
Ming Leif0250612017-03-17 00:12:33 +08002046 struct resync_pages *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002049
2050 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002052 if (i == first)
2053 continue;
Ming Leicdb76be2017-03-17 00:12:34 +08002054
2055 tpages = get_resync_pages(tbio)->pages;
NeilBrown8d3ca832016-11-18 16:16:12 +11002056 d = r10_bio->devs[i].devnum;
2057 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002058 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002059 /* We know that the bi_io_vec layout is the same for
2060 * both 'first' and 'i', so we just compare them.
2061 * All vec entries are PAGE_SIZE;
2062 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002063 int sectors = r10_bio->sectors;
2064 for (j = 0; j < vcnt; j++) {
2065 int len = PAGE_SIZE;
2066 if (sectors < (len / 512))
2067 len = sectors * 512;
Ming Leicdb76be2017-03-17 00:12:34 +08002068 if (memcmp(page_address(fpages[j]),
2069 page_address(tpages[j]),
NeilBrown7bb23c42013-07-16 16:50:47 +10002070 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002071 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002072 sectors -= len/512;
2073 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002074 if (j == vcnt)
2075 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002076 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002077 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2078 /* Don't fix anything. */
2079 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002080 } else if (test_bit(FailFast, &rdev->flags)) {
2081 /* Just give up on this device */
2082 md_error(rdev->mddev, rdev);
2083 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002084 }
NeilBrownf84ee362011-07-28 11:39:25 +10002085 /* Ok, we need to write this bio, either to correct an
2086 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 * First we need to fixup bv_offset, bv_len and
2088 * bi_vecs, as the read request might have corrupted these
2089 */
Ming Leif0250612017-03-17 00:12:33 +08002090 rp = get_resync_pages(tbio);
Kent Overstreet8be185f2012-09-06 14:14:43 -07002091 bio_reset(tbio);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 tbio->bi_vcnt = vcnt;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002094 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
Ming Leif0250612017-03-17 00:12:33 +08002095 rp->raid_bio = r10_bio;
2096 tbio->bi_private = rp;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002097 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002099 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Kent Overstreetc31df252015-05-06 23:34:20 -07002101 bio_copy_data(tbio, fbio);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2104 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002105 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
NeilBrown1919cbb2016-11-18 16:16:12 +11002107 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2108 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002109 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2111 generic_make_request(tbio);
2112 }
2113
NeilBrown9ad1aef2011-12-23 10:17:55 +11002114 /* Now write out to any replacement devices
2115 * that are active
2116 */
2117 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002118 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002119
2120 tbio = r10_bio->devs[i].repl_bio;
2121 if (!tbio || !tbio->bi_end_io)
2122 continue;
2123 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2124 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002125 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002126 d = r10_bio->devs[i].devnum;
2127 atomic_inc(&r10_bio->remaining);
2128 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002129 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002130 generic_make_request(tbio);
2131 }
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133done:
2134 if (atomic_dec_and_test(&r10_bio->remaining)) {
2135 md_done_sync(mddev, r10_bio->sectors, 1);
2136 put_buf(r10_bio);
2137 }
2138}
2139
2140/*
2141 * Now for the recovery code.
2142 * Recovery happens across physical sectors.
2143 * We recover all non-is_sync drives by finding the virtual address of
2144 * each, and then choose a working drive that also has that virt address.
2145 * There is a separate r10_bio for each non-in_sync drive.
2146 * Only the first two slots are in use. The first for reading,
2147 * The second for writing.
2148 *
2149 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002150static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002151{
2152 /* We got a read error during recovery.
2153 * We repeat the read in smaller page-sized sections.
2154 * If a read succeeds, write it to the new device or record
2155 * a bad block if we cannot.
2156 * If a read fails, record a bad block on both old and
2157 * new devices.
2158 */
NeilBrownfd01b882011-10-11 16:47:53 +11002159 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002160 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002161 struct bio *bio = r10_bio->devs[0].bio;
2162 sector_t sect = 0;
2163 int sectors = r10_bio->sectors;
2164 int idx = 0;
2165 int dr = r10_bio->devs[0].devnum;
2166 int dw = r10_bio->devs[1].devnum;
Ming Leicdb76be2017-03-17 00:12:34 +08002167 struct page **pages = get_resync_pages(bio)->pages;
NeilBrown5e570282011-07-28 11:39:25 +10002168
2169 while (sectors) {
2170 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002171 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002172 sector_t addr;
2173 int ok;
2174
2175 if (s > (PAGE_SIZE>>9))
2176 s = PAGE_SIZE >> 9;
2177
2178 rdev = conf->mirrors[dr].rdev;
2179 addr = r10_bio->devs[0].addr + sect,
2180 ok = sync_page_io(rdev,
2181 addr,
2182 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002183 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05002184 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002185 if (ok) {
2186 rdev = conf->mirrors[dw].rdev;
2187 addr = r10_bio->devs[1].addr + sect;
2188 ok = sync_page_io(rdev,
2189 addr,
2190 s << 9,
Ming Leicdb76be2017-03-17 00:12:34 +08002191 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05002192 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002193 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002194 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002195 if (!test_and_set_bit(WantReplacement,
2196 &rdev->flags))
2197 set_bit(MD_RECOVERY_NEEDED,
2198 &rdev->mddev->recovery);
2199 }
NeilBrown5e570282011-07-28 11:39:25 +10002200 }
2201 if (!ok) {
2202 /* We don't worry if we cannot set a bad block -
2203 * it really is bad so there is no loss in not
2204 * recording it yet
2205 */
2206 rdev_set_badblocks(rdev, addr, s, 0);
2207
2208 if (rdev != conf->mirrors[dw].rdev) {
2209 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002210 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002211 addr = r10_bio->devs[1].addr + sect;
2212 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2213 if (!ok) {
2214 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002215 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2216 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002217
2218 conf->mirrors[dw].recovery_disabled
2219 = mddev->recovery_disabled;
2220 set_bit(MD_RECOVERY_INTR,
2221 &mddev->recovery);
2222 break;
2223 }
2224 }
2225 }
2226
2227 sectors -= s;
2228 sect += s;
2229 idx++;
2230 }
2231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
NeilBrown9f2c9d12011-10-11 16:48:43 +11002233static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
NeilBrowne879a872011-10-11 16:49:02 +11002235 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002236 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002237 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
NeilBrown5e570282011-07-28 11:39:25 +10002239 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2240 fix_recovery_read_error(r10_bio);
2241 end_sync_request(r10_bio);
2242 return;
2243 }
2244
Namhyung Kimc65060a2011-07-18 17:38:49 +10002245 /*
2246 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 * and submit the write request
2248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002250 wbio = r10_bio->devs[1].bio;
2251 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002252 /* Need to test wbio2->bi_end_io before we call
2253 * generic_make_request as if the former is NULL,
2254 * the latter is free to free wbio2.
2255 */
2256 if (wbio2 && !wbio2->bi_end_io)
2257 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002258 if (wbio->bi_end_io) {
2259 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002260 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002261 generic_make_request(wbio);
2262 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002263 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002264 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2265 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002266 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002267 generic_make_request(wbio2);
2268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271/*
Robert Becker1e509152009-12-14 12:49:58 +11002272 * Used by fix_read_error() to decay the per rdev read_errors.
2273 * We halve the read error count for every hour that has elapsed
2274 * since the last recorded read error.
2275 *
2276 */
NeilBrownfd01b882011-10-11 16:47:53 +11002277static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002278{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002279 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002280 unsigned long hours_since_last;
2281 unsigned int read_errors = atomic_read(&rdev->read_errors);
2282
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002283 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002284
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002285 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002286 /* first time we've seen a read error */
2287 rdev->last_read_error = cur_time_mon;
2288 return;
2289 }
2290
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002291 hours_since_last = (long)(cur_time_mon -
2292 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002293
2294 rdev->last_read_error = cur_time_mon;
2295
2296 /*
2297 * if hours_since_last is > the number of bits in read_errors
2298 * just set read errors to 0. We do this to avoid
2299 * overflowing the shift of read_errors by hours_since_last.
2300 */
2301 if (hours_since_last >= 8 * sizeof(read_errors))
2302 atomic_set(&rdev->read_errors, 0);
2303 else
2304 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2305}
2306
NeilBrown3cb03002011-10-11 16:45:26 +11002307static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002308 int sectors, struct page *page, int rw)
2309{
2310 sector_t first_bad;
2311 int bad_sectors;
2312
2313 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2314 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2315 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002316 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002317 /* success */
2318 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002319 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002320 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002321 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2322 set_bit(MD_RECOVERY_NEEDED,
2323 &rdev->mddev->recovery);
2324 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002325 /* need to record an error - either for the block or the device */
2326 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2327 md_error(rdev->mddev, rdev);
2328 return 0;
2329}
2330
Robert Becker1e509152009-12-14 12:49:58 +11002331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * This is a kernel thread which:
2333 *
2334 * 1. Retries failed read operations on working mirrors.
2335 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002336 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
2338
NeilBrowne879a872011-10-11 16:49:02 +11002339static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002340{
2341 int sect = 0; /* Offset from r10_bio->sector */
2342 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002343 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002344 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002345 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002346
NeilBrown7c4e06f2011-05-11 14:53:17 +10002347 /* still own a reference to this rdev, so it cannot
2348 * have been cleared recently.
2349 */
2350 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002351
NeilBrown7c4e06f2011-05-11 14:53:17 +10002352 if (test_bit(Faulty, &rdev->flags))
2353 /* drive has already been failed, just ignore any
2354 more fix_read_error() attempts */
2355 return;
2356
2357 check_decay_read_errors(mddev, rdev);
2358 atomic_inc(&rdev->read_errors);
2359 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2360 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002361 bdevname(rdev->bdev, b);
2362
NeilBrown08464e02016-11-02 14:16:50 +11002363 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2364 mdname(mddev), b,
2365 atomic_read(&rdev->read_errors), max_read_errors);
2366 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2367 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002368 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002369 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002370 return;
Robert Becker1e509152009-12-14 12:49:58 +11002371 }
Robert Becker1e509152009-12-14 12:49:58 +11002372
NeilBrown6814d532006-10-03 01:15:45 -07002373 while(sectors) {
2374 int s = sectors;
2375 int sl = r10_bio->read_slot;
2376 int success = 0;
2377 int start;
2378
2379 if (s > (PAGE_SIZE>>9))
2380 s = PAGE_SIZE >> 9;
2381
2382 rcu_read_lock();
2383 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002384 sector_t first_bad;
2385 int bad_sectors;
2386
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002387 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002388 rdev = rcu_dereference(conf->mirrors[d].rdev);
2389 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002390 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002391 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002392 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2393 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002394 atomic_inc(&rdev->nr_pending);
2395 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002396 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002397 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002398 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002399 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002400 conf->tmppage,
2401 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002402 rdev_dec_pending(rdev, mddev);
2403 rcu_read_lock();
2404 if (success)
2405 break;
2406 }
2407 sl++;
2408 if (sl == conf->copies)
2409 sl = 0;
2410 } while (!success && sl != r10_bio->read_slot);
2411 rcu_read_unlock();
2412
2413 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002414 /* Cannot read from anywhere, just mark the block
2415 * as bad on the first device to discourage future
2416 * reads.
2417 */
NeilBrown6814d532006-10-03 01:15:45 -07002418 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002419 rdev = conf->mirrors[dn].rdev;
2420
2421 if (!rdev_set_badblocks(
2422 rdev,
2423 r10_bio->devs[r10_bio->read_slot].addr
2424 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002425 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002426 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002427 r10_bio->devs[r10_bio->read_slot].bio
2428 = IO_BLOCKED;
2429 }
NeilBrown6814d532006-10-03 01:15:45 -07002430 break;
2431 }
2432
2433 start = sl;
2434 /* write it back and re-read */
2435 rcu_read_lock();
2436 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002437 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002438
NeilBrown6814d532006-10-03 01:15:45 -07002439 if (sl==0)
2440 sl = conf->copies;
2441 sl--;
2442 d = r10_bio->devs[sl].devnum;
2443 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002444 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002445 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002446 !test_bit(In_sync, &rdev->flags))
2447 continue;
2448
2449 atomic_inc(&rdev->nr_pending);
2450 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002451 if (r10_sync_page_io(rdev,
2452 r10_bio->devs[sl].addr +
2453 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002454 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002455 == 0) {
2456 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002457 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2458 mdname(mddev), s,
2459 (unsigned long long)(
2460 sect +
2461 choose_data_offset(r10_bio,
2462 rdev)),
2463 bdevname(rdev->bdev, b));
2464 pr_notice("md/raid10:%s: %s: failing drive\n",
2465 mdname(mddev),
2466 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002467 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002468 rdev_dec_pending(rdev, mddev);
2469 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002470 }
2471 sl = start;
2472 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002473 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002474
NeilBrown6814d532006-10-03 01:15:45 -07002475 if (sl==0)
2476 sl = conf->copies;
2477 sl--;
2478 d = r10_bio->devs[sl].devnum;
2479 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002480 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002481 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002482 !test_bit(In_sync, &rdev->flags))
2483 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002484
NeilBrown1294b9c2011-07-28 11:39:23 +10002485 atomic_inc(&rdev->nr_pending);
2486 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002487 switch (r10_sync_page_io(rdev,
2488 r10_bio->devs[sl].addr +
2489 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002490 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002491 READ)) {
2492 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002493 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002494 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002495 mdname(mddev), s,
2496 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002497 sect +
2498 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002499 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002500 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002501 mdname(mddev),
2502 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002503 break;
2504 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002505 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002506 mdname(mddev), s,
2507 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002508 sect +
2509 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002510 bdevname(rdev->bdev, b));
2511 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002512 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002513
2514 rdev_dec_pending(rdev, mddev);
2515 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002516 }
2517 rcu_read_unlock();
2518
2519 sectors -= s;
2520 sect += s;
2521 }
2522}
2523
NeilBrown9f2c9d12011-10-11 16:48:43 +11002524static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002525{
2526 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002527 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002528 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002529 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002530 /* bio has the data to be written to slot 'i' where
2531 * we just recently had a write error.
2532 * We repeatedly clone the bio and trim down to one block,
2533 * then try the write. Where the write fails we record
2534 * a bad block.
2535 * It is conceivable that the bio doesn't exactly align with
2536 * blocks. We must handle this.
2537 *
2538 * We currently own a reference to the rdev.
2539 */
2540
2541 int block_sectors;
2542 sector_t sector;
2543 int sectors;
2544 int sect_to_write = r10_bio->sectors;
2545 int ok = 1;
2546
2547 if (rdev->badblocks.shift < 0)
2548 return 0;
2549
NeilBrownf04ebb02015-02-16 14:51:54 +11002550 block_sectors = roundup(1 << rdev->badblocks.shift,
2551 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002552 sector = r10_bio->sector;
2553 sectors = ((r10_bio->sector + block_sectors)
2554 & ~(sector_t)(block_sectors - 1))
2555 - sector;
2556
2557 while (sect_to_write) {
2558 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002559 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002560 if (sectors > sect_to_write)
2561 sectors = sect_to_write;
2562 /* Write at 'sector' for 'sectors' */
Ming Leid7a10302017-02-14 23:29:03 +08002563 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002564 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002565 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2566 wbio->bi_iter.bi_sector = wsector +
2567 choose_data_offset(r10_bio, rdev);
NeilBrownbd870a12011-07-28 11:39:24 +10002568 wbio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002569 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002570
2571 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002572 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002573 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002574 sectors, 0)
2575 && ok;
2576
2577 bio_put(wbio);
2578 sect_to_write -= sectors;
2579 sector += sectors;
2580 sectors = block_sectors;
2581 }
2582 return ok;
2583}
2584
NeilBrown9f2c9d12011-10-11 16:48:43 +11002585static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002586{
2587 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002588 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002589 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002590 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown109e3762016-11-18 13:22:04 +11002591 dev_t bio_dev;
2592 sector_t bio_last_sector;
NeilBrown560f8e52011-07-28 11:39:23 +10002593
2594 /* we got a read error. Maybe the drive is bad. Maybe just
2595 * the block and we can fix it.
2596 * We freeze all other IO, and try reading the block from
2597 * other devices. When we find one, we re-write
2598 * and check it that fixes the read error.
2599 * This is all done synchronously while the array is
2600 * frozen.
2601 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002602 bio = r10_bio->devs[slot].bio;
NeilBrown109e3762016-11-18 13:22:04 +11002603 bio_dev = bio->bi_bdev->bd_dev;
2604 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
NeilBrownfae8cc52012-02-14 11:10:10 +11002605 bio_put(bio);
2606 r10_bio->devs[slot].bio = NULL;
2607
NeilBrown8d3ca832016-11-18 16:16:12 +11002608 if (mddev->ro)
2609 r10_bio->devs[slot].bio = IO_BLOCKED;
2610 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002611 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002612 fix_read_error(conf, mddev, r10_bio);
2613 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002614 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002615 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002616
NeilBrownabbf0982011-12-23 10:17:54 +11002617 rdev_dec_pending(rdev, mddev);
NeilBrown545250f2017-04-05 14:05:51 +10002618 allow_barrier(conf);
2619 r10_bio->state = 0;
2620 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002621}
2622
NeilBrowne879a872011-10-11 16:49:02 +11002623static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002624{
2625 /* Some sort of write request has finished and it
2626 * succeeded in writing where we thought there was a
2627 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002628 * Or possibly if failed and we need to record
2629 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002630 */
2631 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002632 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002633
2634 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2635 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002636 for (m = 0; m < conf->copies; m++) {
2637 int dev = r10_bio->devs[m].devnum;
2638 rdev = conf->mirrors[dev].rdev;
2639 if (r10_bio->devs[m].bio == NULL)
2640 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002641 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002642 rdev_clear_badblocks(
2643 rdev,
2644 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002645 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002646 } else {
2647 if (!rdev_set_badblocks(
2648 rdev,
2649 r10_bio->devs[m].addr,
2650 r10_bio->sectors, 0))
2651 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002652 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002653 rdev = conf->mirrors[dev].replacement;
2654 if (r10_bio->devs[m].repl_bio == NULL)
2655 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002656
2657 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002658 rdev_clear_badblocks(
2659 rdev,
2660 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002661 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002662 } else {
2663 if (!rdev_set_badblocks(
2664 rdev,
2665 r10_bio->devs[m].addr,
2666 r10_bio->sectors, 0))
2667 md_error(conf->mddev, rdev);
2668 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002669 }
NeilBrown749c55e2011-07-28 11:39:24 +10002670 put_buf(r10_bio);
2671 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002672 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002673 for (m = 0; m < conf->copies; m++) {
2674 int dev = r10_bio->devs[m].devnum;
2675 struct bio *bio = r10_bio->devs[m].bio;
2676 rdev = conf->mirrors[dev].rdev;
2677 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002678 rdev_clear_badblocks(
2679 rdev,
2680 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002681 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002682 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002683 } else if (bio != NULL && bio->bi_error) {
NeilBrown95af5872015-08-14 11:26:17 +10002684 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002685 if (!narrow_write_error(r10_bio, m)) {
2686 md_error(conf->mddev, rdev);
2687 set_bit(R10BIO_Degraded,
2688 &r10_bio->state);
2689 }
2690 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002691 }
NeilBrown475b0322011-12-23 10:17:55 +11002692 bio = r10_bio->devs[m].repl_bio;
2693 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002694 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002695 rdev_clear_badblocks(
2696 rdev,
2697 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002698 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002699 rdev_dec_pending(rdev, conf->mddev);
2700 }
NeilBrownbd870a12011-07-28 11:39:24 +10002701 }
NeilBrown95af5872015-08-14 11:26:17 +10002702 if (fail) {
2703 spin_lock_irq(&conf->device_lock);
2704 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002705 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002706 spin_unlock_irq(&conf->device_lock);
Guoqing Jiangcf25ae72017-04-17 17:11:05 +08002707 /*
2708 * In case freeze_array() is waiting for condition
2709 * nr_pending == nr_queued + extra to be true.
2710 */
2711 wake_up(&conf->wait_barrier);
NeilBrown95af5872015-08-14 11:26:17 +10002712 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002713 } else {
2714 if (test_bit(R10BIO_WriteError,
2715 &r10_bio->state))
2716 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002717 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002718 }
NeilBrown749c55e2011-07-28 11:39:24 +10002719 }
2720}
2721
Shaohua Li4ed87312012-10-11 13:34:00 +11002722static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Shaohua Li4ed87312012-10-11 13:34:00 +11002724 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002725 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002727 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002729 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
NeilBrown95af5872015-08-14 11:26:17 +10002733 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002734 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002735 LIST_HEAD(tmp);
2736 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002737 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002738 while (!list_empty(&conf->bio_end_io_list)) {
2739 list_move(conf->bio_end_io_list.prev, &tmp);
2740 conf->nr_queued--;
2741 }
NeilBrown95af5872015-08-14 11:26:17 +10002742 }
2743 spin_unlock_irqrestore(&conf->device_lock, flags);
2744 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002745 r10_bio = list_first_entry(&tmp, struct r10bio,
2746 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002747 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002748 if (mddev->degraded)
2749 set_bit(R10BIO_Degraded, &r10_bio->state);
2750
2751 if (test_bit(R10BIO_WriteError,
2752 &r10_bio->state))
2753 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002754 raid_end_bio_io(r10_bio);
2755 }
2756 }
2757
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002758 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002760
NeilBrown0021b7b2012-07-31 09:08:14 +02002761 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002764 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002765 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002767 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002768 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002770 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 spin_unlock_irqrestore(&conf->device_lock, flags);
2772
2773 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002774 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002775 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2776 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002777 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002778 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2779 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002780 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002782 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002784 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002785 handle_read_error(mddev, r10_bio);
NeilBrownfc9977d2017-04-05 14:05:51 +10002786 else
2787 WARN_ON_ONCE(1);
NeilBrown4443ae12006-01-06 00:20:28 -08002788
NeilBrown1d9d5242009-10-16 15:55:32 +11002789 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002790 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002791 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002793 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
NeilBrowne879a872011-10-11 16:49:02 +11002796static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002799 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002802 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002803 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002804 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002805 if (conf->mirrors[i].replacement)
2806 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2808 if (!conf->r10buf_pool)
2809 return -ENOMEM;
2810 conf->next_resync = 0;
2811 return 0;
2812}
2813
2814/*
2815 * perform a "sync" on one "block"
2816 *
2817 * We need to make sure that no normal I/O request - particularly write
2818 * requests - conflict with active sync requests.
2819 *
2820 * This is achieved by tracking pending requests and a 'barrier' concept
2821 * that can be installed to exclude normal IO requests.
2822 *
2823 * Resync and recovery are handled very differently.
2824 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2825 *
2826 * For resync, we iterate over virtual addresses, read all copies,
2827 * and update if there are differences. If only one copy is live,
2828 * skip it.
2829 * For recovery, we iterate over physical addresses, read a good
2830 * value for each non-in_sync drive, and over-write.
2831 *
2832 * So, for recovery we may have several outstanding complex requests for a
2833 * given address, one for each out-of-sync device. We model this by allocating
2834 * a number of r10_bio structures, one for each out-of-sync device.
2835 * As we setup these structures, we collect all bio's together into a list
2836 * which we then process collectively to add pages, and then process again
2837 * to pass to generic_make_request.
2838 *
2839 * The r10_bio structures are linked using a borrowed master_bio pointer.
2840 * This link is counted in ->remaining. When the r10_bio that points to NULL
2841 * has its remaining count decremented to 0, the whole complex operation
2842 * is complete.
2843 *
2844 */
2845
Shaohua Li849674e2016-01-20 13:52:20 -08002846static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002847 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
NeilBrowne879a872011-10-11 16:49:02 +11002849 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002850 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 struct bio *biolist = NULL, *bio;
2852 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002854 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002855 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 sector_t sectors_skipped = 0;
2857 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002858 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 if (!conf->r10buf_pool)
2861 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002864 /*
2865 * Allow skipping a full rebuild for incremental assembly
2866 * of a clean array, like RAID1 does.
2867 */
2868 if (mddev->bitmap == NULL &&
2869 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002870 mddev->reshape_position == MaxSector &&
2871 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002872 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002873 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002874 conf->fullsync == 0) {
2875 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002876 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002877 }
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002880 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002881 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2882 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 max_sector = mddev->resync_max_sectors;
2884 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002885 /* If we aborted, we need to abort the
2886 * sync on the 'current' bitmap chucks (there can
2887 * be several when recovering multiple devices).
2888 * as we may have started syncing it but not finished.
2889 * We can find the current address in
2890 * mddev->curr_resync, but for recovery,
2891 * we need to convert that to several
2892 * virtual addresses.
2893 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002894 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2895 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002896 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002897 return 0;
2898 }
2899
NeilBrown6cce3b22006-01-06 00:20:16 -08002900 if (mddev->curr_resync < max_sector) { /* aborted */
2901 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2902 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2903 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002904 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002905 sector_t sect =
2906 raid10_find_virt(conf, mddev->curr_resync, i);
2907 bitmap_end_sync(mddev->bitmap, sect,
2908 &sync_blocks, 1);
2909 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002910 } else {
2911 /* completed sync */
2912 if ((!mddev->bitmap || conf->fullsync)
2913 && conf->have_replacement
2914 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2915 /* Completed a full sync so the replacements
2916 * are now fully recovered.
2917 */
NeilBrownf90145f2016-06-02 16:19:52 +10002918 rcu_read_lock();
2919 for (i = 0; i < conf->geo.raid_disks; i++) {
2920 struct md_rdev *rdev =
2921 rcu_dereference(conf->mirrors[i].replacement);
2922 if (rdev)
2923 rdev->recovery_offset = MaxSector;
2924 }
2925 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11002926 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002927 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002928 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002929 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002931 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return sectors_skipped;
2933 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002934
2935 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2936 return reshape_request(mddev, sector_nr, skipped);
2937
NeilBrown5cf00fc2012-05-21 09:28:20 +10002938 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 /* if there has been nothing to do on any drive,
2940 * then there is nothing to do at all..
2941 */
NeilBrown57afd892005-06-21 17:17:13 -07002942 *skipped = 1;
2943 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
2945
NeilBrownc6207272008-02-06 01:39:52 -08002946 if (max_sector > mddev->resync_max)
2947 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* make sure whole request will fit in a chunk - if chunks
2950 * are meaningful
2951 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002952 if (conf->geo.near_copies < conf->geo.raid_disks &&
2953 max_sector > (sector_nr | chunk_mask))
2954 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002956 /*
2957 * If there is non-resync activity waiting for a turn, then let it
2958 * though before starting on this new sync request.
2959 */
2960 if (conf->nr_waiting)
2961 schedule_timeout_uninterruptible(1);
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 /* Again, very different code for resync and recovery.
2964 * Both must result in an r10bio with a list of bios that
2965 * have bi_end_io, bi_sector, bi_bdev set,
2966 * and bi_private set to the r10bio.
2967 * For recovery, we may actually create several r10bios
2968 * with 2 bios in each, that correspond to the bios in the main one.
2969 * In this case, the subordinate r10bios link back through a
2970 * borrowed master_bio pointer, and the counter in the master
2971 * includes a ref from each subordinate.
2972 */
2973 /* First, we decide what to do and set ->bi_end_io
2974 * To end_sync_read if we want to read, and
2975 * end_sync_write if we will want to write.
2976 */
2977
NeilBrown6cce3b22006-01-06 00:20:16 -08002978 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2980 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002981 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 r10_bio = NULL;
2983
NeilBrown5cf00fc2012-05-21 09:28:20 +10002984 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002985 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002986 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002987 sector_t sect;
2988 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002989 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10002990 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10002991 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10002992
NeilBrownf90145f2016-06-02 16:19:52 +10002993 rcu_read_lock();
2994 mrdev = rcu_dereference(mirror->rdev);
2995 mreplace = rcu_dereference(mirror->replacement);
2996
2997 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002998 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10002999 test_bit(In_sync, &mrdev->flags)) &&
3000 (mreplace == NULL ||
3001 test_bit(Faulty, &mreplace->flags))) {
3002 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003003 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003004 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003005
3006 still_degraded = 0;
3007 /* want to reconstruct this device */
3008 rb2 = r10_bio;
3009 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003010 if (sect >= mddev->resync_max_sectors) {
3011 /* last stripe is not complete - don't
3012 * try to recover this sector.
3013 */
NeilBrownf90145f2016-06-02 16:19:52 +10003014 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003015 continue;
3016 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003017 if (mreplace && test_bit(Faulty, &mreplace->flags))
3018 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003019 /* Unless we are doing a full sync, or a replacement
3020 * we only need to recover the block if it is set in
3021 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003022 */
3023 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3024 &sync_blocks, 1);
3025 if (sync_blocks < max_sync)
3026 max_sync = sync_blocks;
3027 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003028 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003029 !conf->fullsync) {
3030 /* yep, skip the sync_blocks here, but don't assume
3031 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003032 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003033 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003034 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003035 continue;
3036 }
NeilBrownf90145f2016-06-02 16:19:52 +10003037 atomic_inc(&mrdev->nr_pending);
3038 if (mreplace)
3039 atomic_inc(&mreplace->nr_pending);
3040 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
NeilBrownab9d47e2011-05-11 14:54:41 +10003042 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003043 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003044 raise_barrier(conf, rb2 != NULL);
3045 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
NeilBrownab9d47e2011-05-11 14:54:41 +10003047 r10_bio->master_bio = (struct bio*)rb2;
3048 if (rb2)
3049 atomic_inc(&rb2->remaining);
3050 r10_bio->mddev = mddev;
3051 set_bit(R10BIO_IsRecover, &r10_bio->state);
3052 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003053
NeilBrownab9d47e2011-05-11 14:54:41 +10003054 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003055
NeilBrownab9d47e2011-05-11 14:54:41 +10003056 /* Need to check if the array will still be
3057 * degraded
3058 */
NeilBrownf90145f2016-06-02 16:19:52 +10003059 rcu_read_lock();
3060 for (j = 0; j < conf->geo.raid_disks; j++) {
3061 struct md_rdev *rdev = rcu_dereference(
3062 conf->mirrors[j].rdev);
3063 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003064 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
NeilBrownf90145f2016-06-02 16:19:52 +10003067 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003068
3069 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3070 &sync_blocks, still_degraded);
3071
NeilBrowne875ece2011-07-28 11:39:24 +10003072 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003073 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003074 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003075 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003076 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003077 struct md_rdev *rdev =
3078 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003079 sector_t sector, first_bad;
3080 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003081 if (!rdev ||
3082 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003083 continue;
3084 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003085 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003086 sector = r10_bio->devs[j].addr;
3087
3088 if (is_badblock(rdev, sector, max_sync,
3089 &first_bad, &bad_sectors)) {
3090 if (first_bad > sector)
3091 max_sync = first_bad - sector;
3092 else {
3093 bad_sectors -= (sector
3094 - first_bad);
3095 if (max_sync > bad_sectors)
3096 max_sync = bad_sectors;
3097 continue;
3098 }
3099 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003100 bio = r10_bio->devs[0].bio;
3101 bio->bi_next = biolist;
3102 biolist = bio;
NeilBrownab9d47e2011-05-11 14:54:41 +10003103 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003104 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003105 if (test_bit(FailFast, &rdev->flags))
3106 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003107 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003108 bio->bi_iter.bi_sector = from_addr +
3109 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003110 bio->bi_bdev = rdev->bdev;
3111 atomic_inc(&rdev->nr_pending);
3112 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003113
3114 for (k=0; k<conf->copies; k++)
3115 if (r10_bio->devs[k].devnum == i)
3116 break;
3117 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003118 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003119 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003120 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003121 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003122 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003123
NeilBrownf90145f2016-06-02 16:19:52 +10003124 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003125 bio = r10_bio->devs[1].bio;
3126 bio->bi_next = biolist;
3127 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003128 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003129 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003130 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003131 + mrdev->data_offset;
3132 bio->bi_bdev = mrdev->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003133 atomic_inc(&r10_bio->remaining);
3134 } else
3135 r10_bio->devs[1].bio->bi_end_io = NULL;
3136
3137 /* and maybe write to replacement */
3138 bio = r10_bio->devs[1].repl_bio;
3139 if (bio)
3140 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003141 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003142 * cannot be NULL as r10buf_pool_alloc will
3143 * have allocated it.
3144 * So the second test here is pointless.
3145 * But it keeps semantic-checkers happy, and
3146 * this comment keeps human reviewers
3147 * happy.
3148 */
NeilBrownf90145f2016-06-02 16:19:52 +10003149 if (mreplace == NULL || bio == NULL ||
3150 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003151 break;
3152 bio->bi_next = biolist;
3153 biolist = bio;
NeilBrown24afd802011-12-23 10:17:55 +11003154 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003155 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003156 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003157 mreplace->data_offset;
3158 bio->bi_bdev = mreplace->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003159 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
NeilBrownf90145f2016-06-02 16:19:52 +10003162 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003163 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003164 /* Cannot recover, so abort the recovery or
3165 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003166 if (any_working) {
3167 /* problem is that there are bad blocks
3168 * on other device(s)
3169 */
3170 int k;
3171 for (k = 0; k < conf->copies; k++)
3172 if (r10_bio->devs[k].devnum == i)
3173 break;
NeilBrown24afd802011-12-23 10:17:55 +11003174 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003175 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003176 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003177 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003178 r10_bio->devs[k].addr,
3179 max_sync, 0))
3180 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003181 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003182 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003183 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003184 r10_bio->devs[k].addr,
3185 max_sync, 0))
3186 any_working = 0;
3187 }
3188 if (!any_working) {
3189 if (!test_and_set_bit(MD_RECOVERY_INTR,
3190 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003191 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003192 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003193 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003194 = mddev->recovery_disabled;
3195 }
NeilBrowne8b84912014-01-06 10:35:34 +11003196 put_buf(r10_bio);
3197 if (rb2)
3198 atomic_dec(&rb2->remaining);
3199 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003200 rdev_dec_pending(mrdev, mddev);
3201 if (mreplace)
3202 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003203 break;
3204 }
NeilBrownf90145f2016-06-02 16:19:52 +10003205 rdev_dec_pending(mrdev, mddev);
3206 if (mreplace)
3207 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003208 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3209 /* Only want this if there is elsewhere to
3210 * read from. 'j' is currently the first
3211 * readable copy.
3212 */
3213 int targets = 1;
3214 for (; j < conf->copies; j++) {
3215 int d = r10_bio->devs[j].devnum;
3216 if (conf->mirrors[d].rdev &&
3217 test_bit(In_sync,
3218 &conf->mirrors[d].rdev->flags))
3219 targets++;
3220 }
3221 if (targets == 1)
3222 r10_bio->devs[0].bio->bi_opf
3223 &= ~MD_FAILFAST;
3224 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (biolist == NULL) {
3227 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003228 struct r10bio *rb2 = r10_bio;
3229 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 rb2->master_bio = NULL;
3231 put_buf(rb2);
3232 }
3233 goto giveup;
3234 }
3235 } else {
3236 /* resync. Schedule a read for every block at this virt offset */
3237 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003238
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003239 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003240
NeilBrown6cce3b22006-01-06 00:20:16 -08003241 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3242 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003243 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3244 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003245 /* We can skip this block */
3246 *skipped = 1;
3247 return sync_blocks + sectors_skipped;
3248 }
3249 if (sync_blocks < max_sync)
3250 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003252 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 r10_bio->mddev = mddev;
3255 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003256 raise_barrier(conf, 0);
3257 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 r10_bio->master_bio = NULL;
3260 r10_bio->sector = sector_nr;
3261 set_bit(R10BIO_IsSync, &r10_bio->state);
3262 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003263 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
NeilBrown5cf00fc2012-05-21 09:28:20 +10003265 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003267 sector_t first_bad, sector;
3268 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003269 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003270
NeilBrown9ad1aef2011-12-23 10:17:55 +11003271 if (r10_bio->devs[i].repl_bio)
3272 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 bio = r10_bio->devs[i].bio;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003275 bio->bi_error = -EIO;
NeilBrownf90145f2016-06-02 16:19:52 +10003276 rcu_read_lock();
3277 rdev = rcu_dereference(conf->mirrors[d].rdev);
3278 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3279 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003281 }
NeilBrown40c356c2011-07-28 11:39:24 +10003282 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003283 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003284 &first_bad, &bad_sectors)) {
3285 if (first_bad > sector)
3286 max_sync = first_bad - sector;
3287 else {
3288 bad_sectors -= (sector - first_bad);
3289 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003290 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003291 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003292 continue;
3293 }
3294 }
NeilBrownf90145f2016-06-02 16:19:52 +10003295 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 atomic_inc(&r10_bio->remaining);
3297 bio->bi_next = biolist;
3298 biolist = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003300 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003301 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3302 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003303 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3304 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003306
NeilBrownf90145f2016-06-02 16:19:52 +10003307 rdev = rcu_dereference(conf->mirrors[d].replacement);
3308 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3309 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003310 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003311 }
3312 atomic_inc(&rdev->nr_pending);
3313 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003314
3315 /* Need to set up for writing to the replacement */
3316 bio = r10_bio->devs[i].repl_bio;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003317 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003318
3319 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003320 bio->bi_next = biolist;
3321 biolist = bio;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003322 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003323 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
NeilBrown1919cbb2016-11-18 16:16:12 +11003324 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3325 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003326 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3327 bio->bi_bdev = rdev->bdev;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003328 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330
3331 if (count < 2) {
3332 for (i=0; i<conf->copies; i++) {
3333 int d = r10_bio->devs[i].devnum;
3334 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003335 rdev_dec_pending(conf->mirrors[d].rdev,
3336 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003337 if (r10_bio->devs[i].repl_bio &&
3338 r10_bio->devs[i].repl_bio->bi_end_io)
3339 rdev_dec_pending(
3340 conf->mirrors[d].replacement,
3341 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 put_buf(r10_bio);
3344 biolist = NULL;
3345 goto giveup;
3346 }
3347 }
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003350 if (sector_nr + max_sync < max_sector)
3351 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 do {
3353 struct page *page;
3354 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (sector_nr + (len>>9) > max_sector)
3356 len = (max_sector - sector_nr) << 9;
3357 if (len == 0)
3358 break;
3359 for (bio= biolist ; bio ; bio=bio->bi_next) {
Ming Leif0250612017-03-17 00:12:33 +08003360 struct resync_pages *rp = get_resync_pages(bio);
3361 page = resync_fetch_page(rp, rp->idx++);
Ming Leic85ba142017-03-17 00:12:22 +08003362 /*
3363 * won't fail because the vec table is big enough
3364 * to hold all these pages
3365 */
3366 bio_add_page(bio, page, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368 nr_sectors += len>>9;
3369 sector_nr += len>>9;
Ming Leif0250612017-03-17 00:12:33 +08003370 } while (get_resync_pages(biolist)->idx < RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 r10_bio->sectors = nr_sectors;
3372
3373 while (biolist) {
3374 bio = biolist;
3375 biolist = biolist->bi_next;
3376
3377 bio->bi_next = NULL;
Ming Leif0250612017-03-17 00:12:33 +08003378 r10_bio = get_resync_r10bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 r10_bio->sectors = nr_sectors;
3380
3381 if (bio->bi_end_io == end_sync_read) {
3382 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003383 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 generic_make_request(bio);
3385 }
3386 }
3387
NeilBrown57afd892005-06-21 17:17:13 -07003388 if (sectors_skipped)
3389 /* pretend they weren't skipped, it makes
3390 * no important difference in this case
3391 */
3392 md_done_sync(mddev, sectors_skipped, 1);
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return sectors_skipped + nr_sectors;
3395 giveup:
3396 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003397 * drives must be failed or in resync, all drives
3398 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 */
NeilBrown09b40682009-02-25 13:18:47 +11003400 if (sector_nr + max_sync < max_sector)
3401 max_sector = sector_nr + max_sync;
3402
3403 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 chunks_skipped ++;
3405 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Dan Williams80c3a6c2009-03-17 18:10:40 -07003409static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003410raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003411{
3412 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003413 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003414
3415 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003416 raid_disks = min(conf->geo.raid_disks,
3417 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003418 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003419 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003420
NeilBrown5cf00fc2012-05-21 09:28:20 +10003421 size = sectors >> conf->geo.chunk_shift;
3422 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003423 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003424 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003425
NeilBrown5cf00fc2012-05-21 09:28:20 +10003426 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003427}
3428
NeilBrown6508fdb2012-05-17 10:08:45 +10003429static void calc_sectors(struct r10conf *conf, sector_t size)
3430{
3431 /* Calculate the number of sectors-per-device that will
3432 * actually be used, and set conf->dev_sectors and
3433 * conf->stride
3434 */
3435
NeilBrown5cf00fc2012-05-21 09:28:20 +10003436 size = size >> conf->geo.chunk_shift;
3437 sector_div(size, conf->geo.far_copies);
3438 size = size * conf->geo.raid_disks;
3439 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003440 /* 'size' is now the number of chunks in the array */
3441 /* calculate "used chunks per device" */
3442 size = size * conf->copies;
3443
3444 /* We need to round up when dividing by raid_disks to
3445 * get the stride size.
3446 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003447 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003448
NeilBrown5cf00fc2012-05-21 09:28:20 +10003449 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003450
NeilBrown5cf00fc2012-05-21 09:28:20 +10003451 if (conf->geo.far_offset)
3452 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003453 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003454 sector_div(size, conf->geo.far_copies);
3455 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003456 }
3457}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003458
NeilBrowndeb200d2012-05-21 09:28:33 +10003459enum geo_type {geo_new, geo_old, geo_start};
3460static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3461{
3462 int nc, fc, fo;
3463 int layout, chunk, disks;
3464 switch (new) {
3465 case geo_old:
3466 layout = mddev->layout;
3467 chunk = mddev->chunk_sectors;
3468 disks = mddev->raid_disks - mddev->delta_disks;
3469 break;
3470 case geo_new:
3471 layout = mddev->new_layout;
3472 chunk = mddev->new_chunk_sectors;
3473 disks = mddev->raid_disks;
3474 break;
3475 default: /* avoid 'may be unused' warnings */
3476 case geo_start: /* new when starting reshape - raid_disks not
3477 * updated yet. */
3478 layout = mddev->new_layout;
3479 chunk = mddev->new_chunk_sectors;
3480 disks = mddev->raid_disks + mddev->delta_disks;
3481 break;
3482 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003483 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003484 return -1;
3485 if (chunk < (PAGE_SIZE >> 9) ||
3486 !is_power_of_2(chunk))
3487 return -2;
3488 nc = layout & 255;
3489 fc = (layout >> 8) & 255;
3490 fo = layout & (1<<16);
3491 geo->raid_disks = disks;
3492 geo->near_copies = nc;
3493 geo->far_copies = fc;
3494 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003495 switch (layout >> 17) {
3496 case 0: /* original layout. simple but not always optimal */
3497 geo->far_set_size = disks;
3498 break;
3499 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3500 * actually using this, but leave code here just in case.*/
3501 geo->far_set_size = disks/fc;
3502 WARN(geo->far_set_size < fc,
3503 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3504 break;
3505 case 2: /* "improved" layout fixed to match documentation */
3506 geo->far_set_size = fc * nc;
3507 break;
3508 default: /* Not a valid layout */
3509 return -1;
3510 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003511 geo->chunk_mask = chunk - 1;
3512 geo->chunk_shift = ffz(~chunk);
3513 return nc*fc;
3514}
3515
NeilBrowne879a872011-10-11 16:49:02 +11003516static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
NeilBrowne879a872011-10-11 16:49:02 +11003518 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003519 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003520 struct geom geo;
3521 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
NeilBrowndeb200d2012-05-21 09:28:33 +10003523 copies = setup_geo(&geo, mddev, geo_new);
3524
3525 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003526 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3527 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
NeilBrown2604b702006-01-06 00:20:36 -08003530
NeilBrowndeb200d2012-05-21 09:28:33 +10003531 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003532 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3533 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 goto out;
3535 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003536
3537 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003538 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003539 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003541
NeilBrown3ea7daa2012-05-22 13:53:47 +10003542 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003543 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003544 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003545 GFP_KERNEL);
3546 if (!conf->mirrors)
3547 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003548
3549 conf->tmppage = alloc_page(GFP_KERNEL);
3550 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003551 goto out;
3552
NeilBrowndeb200d2012-05-21 09:28:33 +10003553 conf->geo = geo;
3554 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003555 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3556 r10bio_pool_free, conf);
3557 if (!conf->r10bio_pool)
3558 goto out;
3559
NeilBrownfc9977d2017-04-05 14:05:51 +10003560 conf->bio_split = bioset_create(BIO_POOL_SIZE, 0);
3561 if (!conf->bio_split)
3562 goto out;
3563
NeilBrown6508fdb2012-05-17 10:08:45 +10003564 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003565 if (mddev->reshape_position == MaxSector) {
3566 conf->prev = conf->geo;
3567 conf->reshape_progress = MaxSector;
3568 } else {
3569 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3570 err = -EINVAL;
3571 goto out;
3572 }
3573 conf->reshape_progress = mddev->reshape_position;
3574 if (conf->prev.far_offset)
3575 conf->prev.stride = 1 << conf->prev.chunk_shift;
3576 else
3577 /* far_copies must be 1 */
3578 conf->prev.stride = conf->dev_sectors;
3579 }
NeilBrown299b0682015-07-06 17:37:49 +10003580 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003581 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003582 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003583 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003584
3585 spin_lock_init(&conf->resync_lock);
3586 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003587 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003588
NeilBrown02326052012-07-03 15:56:52 +10003589 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003590 if (!conf->thread)
3591 goto out;
3592
Trela, Maciejdab8b292010-03-08 16:02:45 +11003593 conf->mddev = mddev;
3594 return conf;
3595
3596 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003597 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003598 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003599 kfree(conf->mirrors);
3600 safe_put_page(conf->tmppage);
NeilBrownfc9977d2017-04-05 14:05:51 +10003601 if (conf->bio_split)
3602 bioset_free(conf->bio_split);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003603 kfree(conf);
3604 }
3605 return ERR_PTR(err);
3606}
3607
Shaohua Li849674e2016-01-20 13:52:20 -08003608static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003609{
NeilBrowne879a872011-10-11 16:49:02 +11003610 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003611 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003612 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003613 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003614 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003615 sector_t min_offset_diff = 0;
3616 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003617 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003618
3619 if (mddev->private == NULL) {
3620 conf = setup_conf(mddev);
3621 if (IS_ERR(conf))
3622 return PTR_ERR(conf);
3623 mddev->private = conf;
3624 }
3625 conf = mddev->private;
3626 if (!conf)
3627 goto out;
3628
Trela, Maciejdab8b292010-03-08 16:02:45 +11003629 mddev->thread = conf->thread;
3630 conf->thread = NULL;
3631
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003632 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003633 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003634 blk_queue_max_discard_sectors(mddev->queue,
3635 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003636 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003637 blk_queue_io_min(mddev->queue, chunk_size);
3638 if (conf->geo.raid_disks % conf->geo.near_copies)
3639 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3640 else
3641 blk_queue_io_opt(mddev->queue, chunk_size *
3642 (conf->geo.raid_disks / conf->geo.near_copies));
3643 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003644
NeilBrowndafb20f2012-03-19 12:46:39 +11003645 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003646 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003647 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003650 if (disk_idx < 0)
3651 continue;
3652 if (disk_idx >= conf->geo.raid_disks &&
3653 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 continue;
3655 disk = conf->mirrors + disk_idx;
3656
NeilBrown56a25592011-12-23 10:17:55 +11003657 if (test_bit(Replacement, &rdev->flags)) {
3658 if (disk->replacement)
3659 goto out_free_conf;
3660 disk->replacement = rdev;
3661 } else {
3662 if (disk->rdev)
3663 goto out_free_conf;
3664 disk->rdev = rdev;
3665 }
NeilBrownaba336b2012-05-31 15:39:11 +10003666 q = bdev_get_queue(rdev->bdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003667 diff = (rdev->new_data_offset - rdev->data_offset);
3668 if (!mddev->reshape_backwards)
3669 diff = -diff;
3670 if (diff < 0)
3671 diff = 0;
3672 if (first || diff < min_offset_diff)
3673 min_offset_diff = diff;
NeilBrown56a25592011-12-23 10:17:55 +11003674
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003675 if (mddev->gendisk)
3676 disk_stack_limits(mddev->gendisk, rdev->bdev,
3677 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003680
3681 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3682 discard_supported = true;
Guoqing Jiang6f287ca2017-04-06 09:12:18 +08003683 first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003685
Jonathan Brassowed30be02012-10-31 11:42:30 +11003686 if (mddev->queue) {
3687 if (discard_supported)
3688 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3689 mddev->queue);
3690 else
3691 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3692 mddev->queue);
3693 }
NeilBrown6d508242005-09-09 16:24:03 -07003694 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003695 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003696 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003697 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 goto out_free_conf;
3699 }
3700
NeilBrown3ea7daa2012-05-22 13:53:47 +10003701 if (conf->reshape_progress != MaxSector) {
3702 /* must ensure that shape change is supported */
3703 if (conf->geo.far_copies != 1 &&
3704 conf->geo.far_offset == 0)
3705 goto out_free_conf;
3706 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003707 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003708 goto out_free_conf;
3709 }
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003712 for (i = 0;
3713 i < conf->geo.raid_disks
3714 || i < conf->prev.raid_disks;
3715 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 disk = conf->mirrors + i;
3718
NeilBrown56a25592011-12-23 10:17:55 +11003719 if (!disk->rdev && disk->replacement) {
3720 /* The replacement is all we have - use it */
3721 disk->rdev = disk->replacement;
3722 disk->replacement = NULL;
3723 clear_bit(Replacement, &disk->rdev->flags);
3724 }
3725
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003726 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003727 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 disk->head_position = 0;
3729 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003730 if (disk->rdev &&
3731 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003732 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
NeilBrownd890fa22011-10-26 11:54:39 +11003734 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
3736
Andre Noll8c6ac862009-06-18 08:48:06 +10003737 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003738 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3739 mdname(mddev));
3740 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003741 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3742 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 /*
3744 * Ok, everything is just fine now
3745 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003746 mddev->dev_sectors = conf->dev_sectors;
3747 size = raid10_size(mddev, 0, 0);
3748 md_set_array_sectors(mddev, size);
3749 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003750 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003752 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003753 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003754 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003755
3756 /* Calculate max read-ahead size.
3757 * We need to readahead at least twice a whole stripe....
3758 * maybe...
3759 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003760 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01003761 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3762 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
3764
Martin K. Petersena91a2782011-03-17 11:11:05 +01003765 if (md_integrity_register(mddev))
3766 goto out_free_conf;
3767
NeilBrown3ea7daa2012-05-22 13:53:47 +10003768 if (conf->reshape_progress != MaxSector) {
3769 unsigned long before_length, after_length;
3770
3771 before_length = ((1 << conf->prev.chunk_shift) *
3772 conf->prev.far_copies);
3773 after_length = ((1 << conf->geo.chunk_shift) *
3774 conf->geo.far_copies);
3775
3776 if (max(before_length, after_length) > min_offset_diff) {
3777 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003778 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003779 goto out_free_conf;
3780 }
3781 conf->offset_diff = min_offset_diff;
3782
NeilBrown3ea7daa2012-05-22 13:53:47 +10003783 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3784 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3785 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3786 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3787 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3788 "reshape");
3789 }
3790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return 0;
3792
3793out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003794 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003795 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003796 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003797 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 kfree(conf);
3799 mddev->private = NULL;
3800out:
3801 return -EIO;
3802}
3803
NeilBrownafa0f552014-12-15 12:56:58 +11003804static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
NeilBrownafa0f552014-12-15 12:56:58 +11003806 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Julia Lawall644df1a2015-09-13 14:15:10 +02003808 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003809 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003810 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003811 kfree(conf->mirrors_old);
3812 kfree(conf->mirrors_new);
NeilBrownfc9977d2017-04-05 14:05:51 +10003813 if (conf->bio_split)
3814 bioset_free(conf->bio_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
NeilBrownfd01b882011-10-11 16:47:53 +11003818static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003819{
NeilBrowne879a872011-10-11 16:49:02 +11003820 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003821
3822 switch(state) {
3823 case 1:
3824 raise_barrier(conf, 0);
3825 break;
3826 case 0:
3827 lower_barrier(conf);
3828 break;
3829 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
NeilBrown006a09a2012-03-19 12:46:40 +11003832static int raid10_resize(struct mddev *mddev, sector_t sectors)
3833{
3834 /* Resize of 'far' arrays is not supported.
3835 * For 'near' and 'offset' arrays we can set the
3836 * number of sectors used to be an appropriate multiple
3837 * of the chunk size.
3838 * For 'offset', this is far_copies*chunksize.
3839 * For 'near' the multiplier is the LCM of
3840 * near_copies and raid_disks.
3841 * So if far_copies > 1 && !far_offset, fail.
3842 * Else find LCM(raid_disks, near_copy)*far_copies and
3843 * multiply by chunk_size. Then round to this number.
3844 * This is mostly done by raid10_size()
3845 */
3846 struct r10conf *conf = mddev->private;
3847 sector_t oldsize, size;
3848
NeilBrownf8c9e742012-05-21 09:28:33 +10003849 if (mddev->reshape_position != MaxSector)
3850 return -EBUSY;
3851
NeilBrown5cf00fc2012-05-21 09:28:20 +10003852 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003853 return -EINVAL;
3854
3855 oldsize = raid10_size(mddev, 0, 0);
3856 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003857 if (mddev->external_size &&
3858 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003859 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003860 if (mddev->bitmap) {
3861 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3862 if (ret)
3863 return ret;
3864 }
3865 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003866 if (sectors > mddev->dev_sectors &&
3867 mddev->recovery_cp > oldsize) {
3868 mddev->recovery_cp = oldsize;
3869 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3870 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003871 calc_sectors(conf, sectors);
3872 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003873 mddev->resync_max_sectors = size;
3874 return 0;
3875}
3876
NeilBrown53a6ab42015-02-12 14:09:57 +11003877static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003878{
NeilBrown3cb03002011-10-11 16:45:26 +11003879 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003880 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003881
3882 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003883 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3884 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003885 return ERR_PTR(-EINVAL);
3886 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003887 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003888
Trela, Maciejdab8b292010-03-08 16:02:45 +11003889 /* Set new parameters */
3890 mddev->new_level = 10;
3891 /* new layout: far_copies = 1, near_copies = 2 */
3892 mddev->new_layout = (1<<8) + 2;
3893 mddev->new_chunk_sectors = mddev->chunk_sectors;
3894 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003895 mddev->raid_disks *= 2;
3896 /* make sure it will be not marked as dirty */
3897 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003898 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003899
3900 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003901 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003902 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003903 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003904 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003905 rdev->sectors = size;
3906 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003907 conf->barrier = 1;
3908 }
3909
Trela, Maciejdab8b292010-03-08 16:02:45 +11003910 return conf;
3911}
3912
NeilBrownfd01b882011-10-11 16:47:53 +11003913static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003914{
NeilBrowne373ab12011-10-11 16:48:59 +11003915 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003916
3917 /* raid10 can take over:
3918 * raid0 - providing it has only two drives
3919 */
3920 if (mddev->level == 0) {
3921 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003922 raid0_conf = mddev->private;
3923 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11003924 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3925 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003926 return ERR_PTR(-EINVAL);
3927 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003928 return raid10_takeover_raid0(mddev,
3929 raid0_conf->strip_zone->zone_end,
3930 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003931 }
3932 return ERR_PTR(-EINVAL);
3933}
3934
NeilBrown3ea7daa2012-05-22 13:53:47 +10003935static int raid10_check_reshape(struct mddev *mddev)
3936{
3937 /* Called when there is a request to change
3938 * - layout (to ->new_layout)
3939 * - chunk size (to ->new_chunk_sectors)
3940 * - raid_disks (by delta_disks)
3941 * or when trying to restart a reshape that was ongoing.
3942 *
3943 * We need to validate the request and possibly allocate
3944 * space if that might be an issue later.
3945 *
3946 * Currently we reject any reshape of a 'far' mode array,
3947 * allow chunk size to change if new is generally acceptable,
3948 * allow raid_disks to increase, and allow
3949 * a switch between 'near' mode and 'offset' mode.
3950 */
3951 struct r10conf *conf = mddev->private;
3952 struct geom geo;
3953
3954 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3955 return -EINVAL;
3956
3957 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3958 /* mustn't change number of copies */
3959 return -EINVAL;
3960 if (geo.far_copies > 1 && !geo.far_offset)
3961 /* Cannot switch to 'far' mode */
3962 return -EINVAL;
3963
3964 if (mddev->array_sectors & geo.chunk_mask)
3965 /* not factor of array size */
3966 return -EINVAL;
3967
NeilBrown3ea7daa2012-05-22 13:53:47 +10003968 if (!enough(conf, -1))
3969 return -EINVAL;
3970
3971 kfree(conf->mirrors_new);
3972 conf->mirrors_new = NULL;
3973 if (mddev->delta_disks > 0) {
3974 /* allocate new 'mirrors' list */
3975 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003976 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003977 *(mddev->raid_disks +
3978 mddev->delta_disks),
3979 GFP_KERNEL);
3980 if (!conf->mirrors_new)
3981 return -ENOMEM;
3982 }
3983 return 0;
3984}
3985
3986/*
3987 * Need to check if array has failed when deciding whether to:
3988 * - start an array
3989 * - remove non-faulty devices
3990 * - add a spare
3991 * - allow a reshape
3992 * This determination is simple when no reshape is happening.
3993 * However if there is a reshape, we need to carefully check
3994 * both the before and after sections.
3995 * This is because some failed devices may only affect one
3996 * of the two sections, and some non-in_sync devices may
3997 * be insync in the section most affected by failed devices.
3998 */
3999static int calc_degraded(struct r10conf *conf)
4000{
4001 int degraded, degraded2;
4002 int i;
4003
4004 rcu_read_lock();
4005 degraded = 0;
4006 /* 'prev' section first */
4007 for (i = 0; i < conf->prev.raid_disks; i++) {
4008 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4009 if (!rdev || test_bit(Faulty, &rdev->flags))
4010 degraded++;
4011 else if (!test_bit(In_sync, &rdev->flags))
4012 /* When we can reduce the number of devices in
4013 * an array, this might not contribute to
4014 * 'degraded'. It does now.
4015 */
4016 degraded++;
4017 }
4018 rcu_read_unlock();
4019 if (conf->geo.raid_disks == conf->prev.raid_disks)
4020 return degraded;
4021 rcu_read_lock();
4022 degraded2 = 0;
4023 for (i = 0; i < conf->geo.raid_disks; i++) {
4024 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4025 if (!rdev || test_bit(Faulty, &rdev->flags))
4026 degraded2++;
4027 else if (!test_bit(In_sync, &rdev->flags)) {
4028 /* If reshape is increasing the number of devices,
4029 * this section has already been recovered, so
4030 * it doesn't contribute to degraded.
4031 * else it does.
4032 */
4033 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4034 degraded2++;
4035 }
4036 }
4037 rcu_read_unlock();
4038 if (degraded2 > degraded)
4039 return degraded2;
4040 return degraded;
4041}
4042
4043static int raid10_start_reshape(struct mddev *mddev)
4044{
4045 /* A 'reshape' has been requested. This commits
4046 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4047 * This also checks if there are enough spares and adds them
4048 * to the array.
4049 * We currently require enough spares to make the final
4050 * array non-degraded. We also require that the difference
4051 * between old and new data_offset - on each device - is
4052 * enough that we never risk over-writing.
4053 */
4054
4055 unsigned long before_length, after_length;
4056 sector_t min_offset_diff = 0;
4057 int first = 1;
4058 struct geom new;
4059 struct r10conf *conf = mddev->private;
4060 struct md_rdev *rdev;
4061 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004062 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004063
4064 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4065 return -EBUSY;
4066
4067 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4068 return -EINVAL;
4069
4070 before_length = ((1 << conf->prev.chunk_shift) *
4071 conf->prev.far_copies);
4072 after_length = ((1 << conf->geo.chunk_shift) *
4073 conf->geo.far_copies);
4074
4075 rdev_for_each(rdev, mddev) {
4076 if (!test_bit(In_sync, &rdev->flags)
4077 && !test_bit(Faulty, &rdev->flags))
4078 spares++;
4079 if (rdev->raid_disk >= 0) {
4080 long long diff = (rdev->new_data_offset
4081 - rdev->data_offset);
4082 if (!mddev->reshape_backwards)
4083 diff = -diff;
4084 if (diff < 0)
4085 diff = 0;
4086 if (first || diff < min_offset_diff)
4087 min_offset_diff = diff;
4088 }
Guoqing Jiang6f287ca2017-04-06 09:12:18 +08004089 first = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004090 }
4091
4092 if (max(before_length, after_length) > min_offset_diff)
4093 return -EINVAL;
4094
4095 if (spares < mddev->delta_disks)
4096 return -EINVAL;
4097
4098 conf->offset_diff = min_offset_diff;
4099 spin_lock_irq(&conf->device_lock);
4100 if (conf->mirrors_new) {
4101 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004102 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004103 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004104 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004105 conf->mirrors_old = conf->mirrors;
4106 conf->mirrors = conf->mirrors_new;
4107 conf->mirrors_new = NULL;
4108 }
4109 setup_geo(&conf->geo, mddev, geo_start);
4110 smp_mb();
4111 if (mddev->reshape_backwards) {
4112 sector_t size = raid10_size(mddev, 0, 0);
4113 if (size < mddev->array_sectors) {
4114 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004115 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4116 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004117 return -EINVAL;
4118 }
4119 mddev->resync_max_sectors = size;
4120 conf->reshape_progress = size;
4121 } else
4122 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004123 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004124 spin_unlock_irq(&conf->device_lock);
4125
NeilBrownbb63a702012-05-22 13:55:28 +10004126 if (mddev->delta_disks && mddev->bitmap) {
4127 ret = bitmap_resize(mddev->bitmap,
4128 raid10_size(mddev, 0,
4129 conf->geo.raid_disks),
4130 0, 0);
4131 if (ret)
4132 goto abort;
4133 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004134 if (mddev->delta_disks > 0) {
4135 rdev_for_each(rdev, mddev)
4136 if (rdev->raid_disk < 0 &&
4137 !test_bit(Faulty, &rdev->flags)) {
4138 if (raid10_add_disk(mddev, rdev) == 0) {
4139 if (rdev->raid_disk >=
4140 conf->prev.raid_disks)
4141 set_bit(In_sync, &rdev->flags);
4142 else
4143 rdev->recovery_offset = 0;
4144
4145 if (sysfs_link_rdev(mddev, rdev))
4146 /* Failure here is OK */;
4147 }
4148 } else if (rdev->raid_disk >= conf->prev.raid_disks
4149 && !test_bit(Faulty, &rdev->flags)) {
4150 /* This is a spare that was manually added */
4151 set_bit(In_sync, &rdev->flags);
4152 }
4153 }
4154 /* When a reshape changes the number of devices,
4155 * ->degraded is measured against the larger of the
4156 * pre and post numbers.
4157 */
4158 spin_lock_irq(&conf->device_lock);
4159 mddev->degraded = calc_degraded(conf);
4160 spin_unlock_irq(&conf->device_lock);
4161 mddev->raid_disks = conf->geo.raid_disks;
4162 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004163 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004164
4165 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4166 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004167 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004168 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4169 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4170
4171 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4172 "reshape");
4173 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004174 ret = -EAGAIN;
4175 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004176 }
4177 conf->reshape_checkpoint = jiffies;
4178 md_wakeup_thread(mddev->sync_thread);
4179 md_new_event(mddev);
4180 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004181
4182abort:
4183 mddev->recovery = 0;
4184 spin_lock_irq(&conf->device_lock);
4185 conf->geo = conf->prev;
4186 mddev->raid_disks = conf->geo.raid_disks;
4187 rdev_for_each(rdev, mddev)
4188 rdev->new_data_offset = rdev->data_offset;
4189 smp_wmb();
4190 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004191 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004192 mddev->reshape_position = MaxSector;
4193 spin_unlock_irq(&conf->device_lock);
4194 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004195}
4196
4197/* Calculate the last device-address that could contain
4198 * any block from the chunk that includes the array-address 's'
4199 * and report the next address.
4200 * i.e. the address returned will be chunk-aligned and after
4201 * any data that is in the chunk containing 's'.
4202 */
4203static sector_t last_dev_address(sector_t s, struct geom *geo)
4204{
4205 s = (s | geo->chunk_mask) + 1;
4206 s >>= geo->chunk_shift;
4207 s *= geo->near_copies;
4208 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4209 s *= geo->far_copies;
4210 s <<= geo->chunk_shift;
4211 return s;
4212}
4213
4214/* Calculate the first device-address that could contain
4215 * any block from the chunk that includes the array-address 's'.
4216 * This too will be the start of a chunk
4217 */
4218static sector_t first_dev_address(sector_t s, struct geom *geo)
4219{
4220 s >>= geo->chunk_shift;
4221 s *= geo->near_copies;
4222 sector_div(s, geo->raid_disks);
4223 s *= geo->far_copies;
4224 s <<= geo->chunk_shift;
4225 return s;
4226}
4227
4228static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4229 int *skipped)
4230{
4231 /* We simply copy at most one chunk (smallest of old and new)
4232 * at a time, possibly less if that exceeds RESYNC_PAGES,
4233 * or we hit a bad block or something.
4234 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004235 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004236 * can record any location.
4237 *
4238 * If we will want to write to a location that isn't
4239 * yet recorded as 'safe' (i.e. in metadata on disk) then
4240 * we need to flush all reshape requests and update the metadata.
4241 *
4242 * When reshaping forwards (e.g. to more devices), we interpret
4243 * 'safe' as the earliest block which might not have been copied
4244 * down yet. We divide this by previous stripe size and multiply
4245 * by previous stripe length to get lowest device offset that we
4246 * cannot write to yet.
4247 * We interpret 'sector_nr' as an address that we want to write to.
4248 * From this we use last_device_address() to find where we might
4249 * write to, and first_device_address on the 'safe' position.
4250 * If this 'next' write position is after the 'safe' position,
4251 * we must update the metadata to increase the 'safe' position.
4252 *
4253 * When reshaping backwards, we round in the opposite direction
4254 * and perform the reverse test: next write position must not be
4255 * less than current safe position.
4256 *
4257 * In all this the minimum difference in data offsets
4258 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004259 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004260 *
4261 * We need to prepare all the bios here before we start any IO
4262 * to ensure the size we choose is acceptable to all devices.
4263 * The means one for each copy for write-out and an extra one for
4264 * read-in.
4265 * We store the read-in bio in ->master_bio and the others in
4266 * ->devs[x].bio and ->devs[x].repl_bio.
4267 */
4268 struct r10conf *conf = mddev->private;
4269 struct r10bio *r10_bio;
4270 sector_t next, safe, last;
4271 int max_sectors;
4272 int nr_sectors;
4273 int s;
4274 struct md_rdev *rdev;
4275 int need_flush = 0;
4276 struct bio *blist;
4277 struct bio *bio, *read_bio;
4278 int sectors_done = 0;
Ming Leif0250612017-03-17 00:12:33 +08004279 struct page **pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004280
4281 if (sector_nr == 0) {
4282 /* If restarting in the middle, skip the initial sectors */
4283 if (mddev->reshape_backwards &&
4284 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4285 sector_nr = (raid10_size(mddev, 0, 0)
4286 - conf->reshape_progress);
4287 } else if (!mddev->reshape_backwards &&
4288 conf->reshape_progress > 0)
4289 sector_nr = conf->reshape_progress;
4290 if (sector_nr) {
4291 mddev->curr_resync_completed = sector_nr;
4292 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4293 *skipped = 1;
4294 return sector_nr;
4295 }
4296 }
4297
4298 /* We don't use sector_nr to track where we are up to
4299 * as that doesn't work well for ->reshape_backwards.
4300 * So just use ->reshape_progress.
4301 */
4302 if (mddev->reshape_backwards) {
4303 /* 'next' is the earliest device address that we might
4304 * write to for this chunk in the new layout
4305 */
4306 next = first_dev_address(conf->reshape_progress - 1,
4307 &conf->geo);
4308
4309 /* 'safe' is the last device address that we might read from
4310 * in the old layout after a restart
4311 */
4312 safe = last_dev_address(conf->reshape_safe - 1,
4313 &conf->prev);
4314
4315 if (next + conf->offset_diff < safe)
4316 need_flush = 1;
4317
4318 last = conf->reshape_progress - 1;
4319 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4320 & conf->prev.chunk_mask);
4321 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4322 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4323 } else {
4324 /* 'next' is after the last device address that we
4325 * might write to for this chunk in the new layout
4326 */
4327 next = last_dev_address(conf->reshape_progress, &conf->geo);
4328
4329 /* 'safe' is the earliest device address that we might
4330 * read from in the old layout after a restart
4331 */
4332 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4333
4334 /* Need to update metadata if 'next' might be beyond 'safe'
4335 * as that would possibly corrupt data
4336 */
4337 if (next > safe + conf->offset_diff)
4338 need_flush = 1;
4339
4340 sector_nr = conf->reshape_progress;
4341 last = sector_nr | (conf->geo.chunk_mask
4342 & conf->prev.chunk_mask);
4343
4344 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4345 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4346 }
4347
4348 if (need_flush ||
4349 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4350 /* Need to update reshape_position in metadata */
4351 wait_barrier(conf);
4352 mddev->reshape_position = conf->reshape_progress;
4353 if (mddev->reshape_backwards)
4354 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4355 - conf->reshape_progress;
4356 else
4357 mddev->curr_resync_completed = conf->reshape_progress;
4358 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004359 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004360 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004361 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004362 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4363 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4364 allow_barrier(conf);
4365 return sectors_done;
4366 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004367 conf->reshape_safe = mddev->reshape_position;
4368 allow_barrier(conf);
4369 }
4370
4371read_more:
4372 /* Now schedule reads for blocks from sector_nr to last */
4373 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004374 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004375 raise_barrier(conf, sectors_done != 0);
4376 atomic_set(&r10_bio->remaining, 0);
4377 r10_bio->mddev = mddev;
4378 r10_bio->sector = sector_nr;
4379 set_bit(R10BIO_IsReshape, &r10_bio->state);
4380 r10_bio->sectors = last - sector_nr + 1;
4381 rdev = read_balance(conf, r10_bio, &max_sectors);
4382 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4383
4384 if (!rdev) {
4385 /* Cannot read from here, so need to record bad blocks
4386 * on all the target devices.
4387 */
4388 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004389 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004390 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4391 return sectors_done;
4392 }
4393
4394 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4395
4396 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004397 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004398 + rdev->data_offset);
4399 read_bio->bi_private = r10_bio;
Ming Lei81fa1522017-03-17 00:12:32 +08004400 read_bio->bi_end_io = end_reshape_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004401 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004402 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004403 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004404 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004405 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004406 r10_bio->master_bio = read_bio;
4407 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4408
4409 /* Now find the locations in the new layout */
4410 __raid10_find_phys(&conf->geo, r10_bio);
4411
4412 blist = read_bio;
4413 read_bio->bi_next = NULL;
4414
NeilBrownd094d682016-06-02 16:19:52 +10004415 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004416 for (s = 0; s < conf->copies*2; s++) {
4417 struct bio *b;
4418 int d = r10_bio->devs[s/2].devnum;
4419 struct md_rdev *rdev2;
4420 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004421 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004422 b = r10_bio->devs[s/2].repl_bio;
4423 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004424 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004425 b = r10_bio->devs[s/2].bio;
4426 }
4427 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4428 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004429
NeilBrown3ea7daa2012-05-22 13:53:47 +10004430 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004431 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4432 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004433 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004434 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004435 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004436 blist = b;
4437 }
4438
4439 /* Now add as many pages as possible to all of these bios. */
4440
4441 nr_sectors = 0;
Ming Leif0250612017-03-17 00:12:33 +08004442 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004443 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
Ming Leif0250612017-03-17 00:12:33 +08004444 struct page *page = pages[s / (PAGE_SIZE >> 9)];
NeilBrown3ea7daa2012-05-22 13:53:47 +10004445 int len = (max_sectors - s) << 9;
4446 if (len > PAGE_SIZE)
4447 len = PAGE_SIZE;
4448 for (bio = blist; bio ; bio = bio->bi_next) {
Ming Leic85ba142017-03-17 00:12:22 +08004449 /*
4450 * won't fail because the vec table is big enough
4451 * to hold all these pages
4452 */
4453 bio_add_page(bio, page, len, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004454 }
4455 sector_nr += len >> 9;
4456 nr_sectors += len >> 9;
4457 }
NeilBrownd094d682016-06-02 16:19:52 +10004458 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004459 r10_bio->sectors = nr_sectors;
4460
4461 /* Now submit the read */
4462 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4463 atomic_inc(&r10_bio->remaining);
4464 read_bio->bi_next = NULL;
4465 generic_make_request(read_bio);
4466 sector_nr += nr_sectors;
4467 sectors_done += nr_sectors;
4468 if (sector_nr <= last)
4469 goto read_more;
4470
4471 /* Now that we have done the whole section we can
4472 * update reshape_progress
4473 */
4474 if (mddev->reshape_backwards)
4475 conf->reshape_progress -= sectors_done;
4476 else
4477 conf->reshape_progress += sectors_done;
4478
4479 return sectors_done;
4480}
4481
4482static void end_reshape_request(struct r10bio *r10_bio);
4483static int handle_reshape_read_error(struct mddev *mddev,
4484 struct r10bio *r10_bio);
4485static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4486{
4487 /* Reshape read completed. Hopefully we have a block
4488 * to write out.
4489 * If we got a read error then we do sync 1-page reads from
4490 * elsewhere until we find the data - or give up.
4491 */
4492 struct r10conf *conf = mddev->private;
4493 int s;
4494
4495 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4496 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4497 /* Reshape has been aborted */
4498 md_done_sync(mddev, r10_bio->sectors, 0);
4499 return;
4500 }
4501
4502 /* We definitely have the data in the pages, schedule the
4503 * writes.
4504 */
4505 atomic_set(&r10_bio->remaining, 1);
4506 for (s = 0; s < conf->copies*2; s++) {
4507 struct bio *b;
4508 int d = r10_bio->devs[s/2].devnum;
4509 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004510 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004511 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004512 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004513 b = r10_bio->devs[s/2].repl_bio;
4514 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004515 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004516 b = r10_bio->devs[s/2].bio;
4517 }
NeilBrownd094d682016-06-02 16:19:52 +10004518 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4519 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004520 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004521 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004522 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004523 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004524 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4525 atomic_inc(&r10_bio->remaining);
4526 b->bi_next = NULL;
4527 generic_make_request(b);
4528 }
4529 end_reshape_request(r10_bio);
4530}
4531
4532static void end_reshape(struct r10conf *conf)
4533{
4534 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4535 return;
4536
4537 spin_lock_irq(&conf->device_lock);
4538 conf->prev = conf->geo;
4539 md_finish_reshape(conf->mddev);
4540 smp_wmb();
4541 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004542 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004543 spin_unlock_irq(&conf->device_lock);
4544
4545 /* read-ahead size must cover two whole stripes, which is
4546 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4547 */
4548 if (conf->mddev->queue) {
4549 int stripe = conf->geo.raid_disks *
4550 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4551 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01004552 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4553 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004554 }
4555 conf->fullsync = 0;
4556}
4557
NeilBrown3ea7daa2012-05-22 13:53:47 +10004558static int handle_reshape_read_error(struct mddev *mddev,
4559 struct r10bio *r10_bio)
4560{
4561 /* Use sync reads to get the blocks from somewhere else */
4562 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004563 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004564 struct {
4565 struct r10bio r10_bio;
4566 struct r10dev devs[conf->copies];
4567 } on_stack;
4568 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004569 int slot = 0;
4570 int idx = 0;
Ming Lei2d06e3b2017-03-17 00:12:35 +08004571 struct page **pages;
4572
4573 /* reshape IOs share pages from .devs[0].bio */
4574 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004575
NeilBrowne0ee7782012-08-18 09:51:42 +10004576 r10b->sector = r10_bio->sector;
4577 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004578
4579 while (sectors) {
4580 int s = sectors;
4581 int success = 0;
4582 int first_slot = slot;
4583
4584 if (s > (PAGE_SIZE >> 9))
4585 s = PAGE_SIZE >> 9;
4586
NeilBrownd094d682016-06-02 16:19:52 +10004587 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004588 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004589 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004590 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004591 sector_t addr;
4592 if (rdev == NULL ||
4593 test_bit(Faulty, &rdev->flags) ||
4594 !test_bit(In_sync, &rdev->flags))
4595 goto failed;
4596
NeilBrowne0ee7782012-08-18 09:51:42 +10004597 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004598 atomic_inc(&rdev->nr_pending);
4599 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004600 success = sync_page_io(rdev,
4601 addr,
4602 s << 9,
Ming Lei2d06e3b2017-03-17 00:12:35 +08004603 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05004604 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004605 rdev_dec_pending(rdev, mddev);
4606 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004607 if (success)
4608 break;
4609 failed:
4610 slot++;
4611 if (slot >= conf->copies)
4612 slot = 0;
4613 if (slot == first_slot)
4614 break;
4615 }
NeilBrownd094d682016-06-02 16:19:52 +10004616 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004617 if (!success) {
4618 /* couldn't read this block, must give up */
4619 set_bit(MD_RECOVERY_INTR,
4620 &mddev->recovery);
4621 return -EIO;
4622 }
4623 sectors -= s;
4624 idx++;
4625 }
4626 return 0;
4627}
4628
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004629static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004630{
Ming Leif0250612017-03-17 00:12:33 +08004631 struct r10bio *r10_bio = get_resync_r10bio(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004632 struct mddev *mddev = r10_bio->mddev;
4633 struct r10conf *conf = mddev->private;
4634 int d;
4635 int slot;
4636 int repl;
4637 struct md_rdev *rdev = NULL;
4638
4639 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4640 if (repl)
4641 rdev = conf->mirrors[d].replacement;
4642 if (!rdev) {
4643 smp_mb();
4644 rdev = conf->mirrors[d].rdev;
4645 }
4646
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004647 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004648 /* FIXME should record badblock */
4649 md_error(mddev, rdev);
4650 }
4651
4652 rdev_dec_pending(rdev, mddev);
4653 end_reshape_request(r10_bio);
4654}
4655
4656static void end_reshape_request(struct r10bio *r10_bio)
4657{
4658 if (!atomic_dec_and_test(&r10_bio->remaining))
4659 return;
4660 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4661 bio_put(r10_bio->master_bio);
4662 put_buf(r10_bio);
4663}
4664
4665static void raid10_finish_reshape(struct mddev *mddev)
4666{
4667 struct r10conf *conf = mddev->private;
4668
4669 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4670 return;
4671
4672 if (mddev->delta_disks > 0) {
4673 sector_t size = raid10_size(mddev, 0, 0);
4674 md_set_array_sectors(mddev, size);
4675 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4676 mddev->recovery_cp = mddev->resync_max_sectors;
4677 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4678 }
4679 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004680 if (mddev->queue) {
4681 set_capacity(mddev->gendisk, mddev->array_sectors);
4682 revalidate_disk(mddev->gendisk);
4683 }
NeilBrown63aced62012-05-22 13:55:33 +10004684 } else {
4685 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004686 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004687 for (d = conf->geo.raid_disks ;
4688 d < conf->geo.raid_disks - mddev->delta_disks;
4689 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004690 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004691 if (rdev)
4692 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004693 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004694 if (rdev)
4695 clear_bit(In_sync, &rdev->flags);
4696 }
NeilBrownd094d682016-06-02 16:19:52 +10004697 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004698 }
4699 mddev->layout = mddev->new_layout;
4700 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4701 mddev->reshape_position = MaxSector;
4702 mddev->delta_disks = 0;
4703 mddev->reshape_backwards = 0;
4704}
4705
NeilBrown84fc4b52011-10-11 16:49:58 +11004706static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
4708 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004709 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004711 .make_request = raid10_make_request,
4712 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004713 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004714 .status = raid10_status,
4715 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 .hot_add_disk = raid10_add_disk,
4717 .hot_remove_disk= raid10_remove_disk,
4718 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004719 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004720 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004721 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004722 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004723 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004724 .check_reshape = raid10_check_reshape,
4725 .start_reshape = raid10_start_reshape,
4726 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004727 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728};
4729
4730static int __init raid_init(void)
4731{
NeilBrown2604b702006-01-06 00:20:36 -08004732 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733}
4734
4735static void raid_exit(void)
4736{
NeilBrown2604b702006-01-06 00:20:36 -08004737 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
4740module_init(raid_init);
4741module_exit(raid_exit);
4742MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004743MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004745MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004746MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004747
4748module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);