blob: 94f5c368c683163822b6b484c265fdf792d3671d [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
Al Virodd0fc662005-10-07 07:46:04 +0100113static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
NeilBrowne879a872011-10-11 16:49:02 +1100115 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100116 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
NeilBrown69335ef2011-12-23 10:17:54 +1100118 /* allocate a r10bio with room for raid_disks entries in the
119 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100120 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static void r10bio_pool_free(void *r10_bio, void *data)
124{
125 kfree(r10_bio);
126}
127
NeilBrown0310fa22008-08-05 15:54:14 +1000128/* amount of memory to reserve for resync requests */
129#define RESYNC_WINDOW (1024*1024)
130/* maximum number of concurrent requests, memory permitting */
131#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * When performing a resync, we need to read and compare, so
135 * we need as many pages are there are copies.
136 * When performing a recovery, we need 2 bios, one for read,
137 * one for write (we recover only one drive per r10buf)
138 *
139 */
Al Virodd0fc662005-10-07 07:46:04 +0100140static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
NeilBrowne879a872011-10-11 16:49:02 +1100142 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100144 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct bio *bio;
146 int i, j;
147 int nalloc;
148
149 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100150 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
NeilBrown3ea7daa2012-05-22 13:53:47 +1000153 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
154 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 nalloc = conf->copies; /* resync */
156 else
157 nalloc = 2; /* recovery */
158
159 /*
160 * Allocate bios.
161 */
162 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100163 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!bio)
165 goto out_free_bio;
166 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100167 if (!conf->have_replacement)
168 continue;
169 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
170 if (!bio)
171 goto out_free_bio;
172 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 /*
175 * Allocate RESYNC_PAGES data pages and attach them
176 * where needed.
177 */
178 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100179 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 bio = r10_bio->devs[j].bio;
181 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown3ea7daa2012-05-22 13:53:47 +1000182 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
183 &conf->mddev->recovery)) {
184 /* we can share bv_page's during recovery
185 * and reshape */
Namhyung Kimc65060a2011-07-18 17:38:49 +1000186 struct bio *rbio = r10_bio->devs[0].bio;
187 page = rbio->bi_io_vec[i].bv_page;
188 get_page(page);
189 } else
190 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (unlikely(!page))
192 goto out_free_pages;
193
194 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100195 if (rbio)
196 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 }
199
200 return r10_bio;
201
202out_free_pages:
203 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800204 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 while (j--)
206 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800207 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
majianpeng5fdd2cf2012-05-22 13:55:03 +1000208 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000210 for ( ; j < nalloc; j++) {
211 if (r10_bio->devs[j].bio)
212 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100213 if (r10_bio->devs[j].repl_bio)
214 bio_put(r10_bio->devs[j].repl_bio);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 r10bio_pool_free(r10_bio, conf);
217 return NULL;
218}
219
220static void r10buf_pool_free(void *__r10_bio, void *data)
221{
222 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100223 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100224 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int j;
226
227 for (j=0; j < conf->copies; j++) {
228 struct bio *bio = r10bio->devs[j].bio;
229 if (bio) {
230 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800231 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 bio->bi_io_vec[i].bv_page = NULL;
233 }
234 bio_put(bio);
235 }
NeilBrown69335ef2011-12-23 10:17:54 +1100236 bio = r10bio->devs[j].repl_bio;
237 if (bio)
238 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 r10bio_pool_free(r10bio, conf);
241}
242
NeilBrowne879a872011-10-11 16:49:02 +1100243static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int i;
246
247 for (i = 0; i < conf->copies; i++) {
248 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000249 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 bio_put(*bio);
251 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100252 bio = &r10_bio->devs[i].repl_bio;
253 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
254 bio_put(*bio);
255 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257}
258
NeilBrown9f2c9d12011-10-11 16:48:43 +1100259static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrowne879a872011-10-11 16:49:02 +1100261 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 put_all_bios(conf, r10_bio);
264 mempool_free(r10_bio, conf->r10bio_pool);
265}
266
NeilBrown9f2c9d12011-10-11 16:48:43 +1100267static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
NeilBrowne879a872011-10-11 16:49:02 +1100269 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 mempool_free(r10_bio, conf->r10buf_pool);
272
NeilBrown0a27ec92006-01-06 00:20:13 -0800273 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
NeilBrown9f2c9d12011-10-11 16:48:43 +1100276static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100279 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100280 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 spin_lock_irqsave(&conf->device_lock, flags);
283 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800284 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_unlock_irqrestore(&conf->device_lock, flags);
286
Arthur Jones388667b2008-07-25 12:03:38 -0700287 /* wake up frozen array... */
288 wake_up(&conf->wait_barrier);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 md_wakeup_thread(mddev->thread);
291}
292
293/*
294 * raid_end_bio_io() is called when we have finished servicing a mirrored
295 * operation and are ready to return a success/failure code to the buffer
296 * cache layer.
297 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100298static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100301 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
NeilBrown856e08e2011-07-28 11:39:23 +1000303 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200304 bio->bi_error = -EIO;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100305
306 bio_endio(bio);
307 /*
308 * Wake up any possible resync thread that waits for the device
309 * to go idle.
310 */
311 allow_barrier(conf);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 free_r10bio(r10_bio);
314}
315
316/*
317 * Update disk head position estimator based on IRQ completion info.
318 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100319static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
NeilBrowne879a872011-10-11 16:49:02 +1100321 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
324 r10_bio->devs[slot].addr + (r10_bio->sectors);
325}
326
Namhyung Kim778ca012011-07-18 17:38:47 +1000327/*
328 * Find the disk number which triggered given bio
329 */
NeilBrowne879a872011-10-11 16:49:02 +1100330static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100331 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000332{
333 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100334 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000335
NeilBrown69335ef2011-12-23 10:17:54 +1100336 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000337 if (r10_bio->devs[slot].bio == bio)
338 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100339 if (r10_bio->devs[slot].repl_bio == bio) {
340 repl = 1;
341 break;
342 }
343 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000344
345 BUG_ON(slot == conf->copies);
346 update_head_pos(slot, r10_bio);
347
NeilBrown749c55e2011-07-28 11:39:24 +1000348 if (slotp)
349 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100350 if (replp)
351 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000352 return r10_bio->devs[slot].devnum;
353}
354
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200355static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200357 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100358 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100360 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100361 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 slot = r10_bio->read_slot;
364 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100365 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * this branch is our 'one mirror IO has finished' event handler:
368 */
NeilBrown4443ae12006-01-06 00:20:28 -0800369 update_head_pos(slot, r10_bio);
370
371 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Set R10BIO_Uptodate in our master bio, so that
374 * we will return a good error code to the higher
375 * levels even if IO on some other mirrored buffer fails.
376 *
377 * The 'master' represents the composite IO operation to
378 * user-side. So if something waits for IO, then it will
379 * wait for the 'master' bio.
380 */
381 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100382 } else {
383 /* If all other devices that store this block have
384 * failed, we want to return the error upwards rather
385 * than fail the last device. Here we redefine
386 * "uptodate" to mean "Don't want to retry"
387 */
NeilBrown635f6412013-06-11 14:57:09 +1000388 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
389 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100390 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100391 }
392 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100394 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800395 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000397 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100400 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000401 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100402 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000403 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000404 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 reschedule_retry(r10_bio);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
NeilBrown9f2c9d12011-10-11 16:48:43 +1100409static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000410{
411 /* clear the bitmap if all writes complete successfully */
412 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
413 r10_bio->sectors,
414 !test_bit(R10BIO_Degraded, &r10_bio->state),
415 0);
416 md_write_end(r10_bio->mddev);
417}
418
NeilBrown9f2c9d12011-10-11 16:48:43 +1100419static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000420{
421 if (atomic_dec_and_test(&r10_bio->remaining)) {
422 if (test_bit(R10BIO_WriteError, &r10_bio->state))
423 reschedule_retry(r10_bio);
424 else {
425 close_write(r10_bio);
426 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
427 reschedule_retry(r10_bio);
428 else
429 raid_end_bio_io(r10_bio);
430 }
431 }
432}
433
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200434static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100436 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000437 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000438 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100439 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100440 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100441 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100442 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700443 bool discard_error;
444
445 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
NeilBrown475b0322011-12-23 10:17:55 +1100447 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
NeilBrown475b0322011-12-23 10:17:55 +1100449 if (repl)
450 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100451 if (!rdev) {
452 smp_rmb();
453 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100454 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * this branch is our 'one mirror IO has finished' event handler:
458 */
Shaohua Li579ed342016-10-06 14:13:52 -0700459 if (bio->bi_error && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100460 if (repl)
461 /* Never record new bad blocks to replacement,
462 * just fail it.
463 */
464 md_error(rdev->mddev, rdev);
465 else {
466 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100467 if (!test_and_set_bit(WantReplacement, &rdev->flags))
468 set_bit(MD_RECOVERY_NEEDED,
469 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100470
NeilBrown475b0322011-12-23 10:17:55 +1100471 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100472 if (test_bit(FailFast, &rdev->flags) &&
473 (bio->bi_opf & MD_FAILFAST)) {
474 md_error(rdev->mddev, rdev);
475 if (!test_bit(Faulty, &rdev->flags))
476 /* This is the only remaining device,
477 * We need to retry the write without
478 * FailFast
479 */
480 set_bit(R10BIO_WriteError, &r10_bio->state);
481 else {
482 r10_bio->devs[slot].bio = NULL;
483 to_put = bio;
484 dec_rdev = 1;
485 }
486 } else
487 set_bit(R10BIO_WriteError, &r10_bio->state);
NeilBrown475b0322011-12-23 10:17:55 +1100488 }
NeilBrown749c55e2011-07-28 11:39:24 +1000489 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * Set R10BIO_Uptodate in our master bio, so that
492 * we will return a good error code for to the higher
493 * levels even if IO on some other mirrored buffer fails.
494 *
495 * The 'master' represents the composite IO operation to
496 * user-side. So if something waits for IO, then it will
497 * wait for the 'master' bio.
498 */
NeilBrown749c55e2011-07-28 11:39:24 +1000499 sector_t first_bad;
500 int bad_sectors;
501
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300502 /*
503 * Do not set R10BIO_Uptodate if the current device is
504 * rebuilding or Faulty. This is because we cannot use
505 * such device for properly reading the data back (we could
506 * potentially use it, if the current write would have felt
507 * before rdev->recovery_offset, but for simplicity we don't
508 * check this here.
509 */
510 if (test_bit(In_sync, &rdev->flags) &&
511 !test_bit(Faulty, &rdev->flags))
512 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
NeilBrown749c55e2011-07-28 11:39:24 +1000514 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100515 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000516 r10_bio->devs[slot].addr,
517 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700518 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000519 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100520 if (repl)
521 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
522 else
523 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000524 dec_rdev = 0;
525 set_bit(R10BIO_MadeGood, &r10_bio->state);
526 }
527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 *
531 * Let's see if all mirrored write operations have finished
532 * already.
533 */
NeilBrown19d5f832011-09-10 17:21:17 +1000534 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000535 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100536 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100537 if (to_put)
538 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300543 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * parameters: near_copies and far_copies.
545 * near_copies * far_copies must be <= raid_disks.
546 * Normally one of these will be 1.
547 * If both are 1, we get raid0.
548 * If near_copies == raid_disks, we get raid1.
549 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300550 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * first chunk, followed by near_copies copies of the next chunk and
552 * so on.
553 * If far_copies > 1, then after 1/far_copies of the array has been assigned
554 * as described above, we start again with a device offset of near_copies.
555 * So we effectively have another copy of the whole array further down all
556 * the drives, but with blocks on different drives.
557 * With this layout, and block is never stored twice on the one device.
558 *
559 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700560 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
562 * raid10_find_virt does the reverse mapping, from a device and a
563 * sector offset to a virtual address
564 */
565
NeilBrownf8c9e742012-05-21 09:28:33 +1000566static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int n,f;
569 sector_t sector;
570 sector_t chunk;
571 sector_t stripe;
572 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100574 int last_far_set_start, last_far_set_size;
575
576 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
577 last_far_set_start *= geo->far_set_size;
578
579 last_far_set_size = geo->far_set_size;
580 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000583 chunk = r10bio->sector >> geo->chunk_shift;
584 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
NeilBrown5cf00fc2012-05-21 09:28:20 +1000586 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000588 dev = sector_div(stripe, geo->raid_disks);
589 if (geo->far_offset)
590 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
NeilBrown5cf00fc2012-05-21 09:28:20 +1000592 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000595 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100597 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100600 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 slot++;
602
NeilBrown5cf00fc2012-05-21 09:28:20 +1000603 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100604 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000605 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100606
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100607 if ((geo->raid_disks % geo->far_set_size) &&
608 (d > last_far_set_start)) {
609 d -= last_far_set_start;
610 d %= last_far_set_size;
611 d += last_far_set_start;
612 } else {
613 d %= geo->far_set_size;
614 d += geo->far_set_size * set;
615 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000616 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 r10bio->devs[slot].devnum = d;
618 r10bio->devs[slot].addr = s;
619 slot++;
620 }
621 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000622 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000624 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000627}
628
629static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
630{
631 struct geom *geo = &conf->geo;
632
633 if (conf->reshape_progress != MaxSector &&
634 ((r10bio->sector >= conf->reshape_progress) !=
635 conf->mddev->reshape_backwards)) {
636 set_bit(R10BIO_Previous, &r10bio->state);
637 geo = &conf->prev;
638 } else
639 clear_bit(R10BIO_Previous, &r10bio->state);
640
641 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
NeilBrowne879a872011-10-11 16:49:02 +1100644static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000647 /* Never use conf->prev as this is only called during resync
648 * or recovery, so reshape isn't happening
649 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000650 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100651 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
652 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100653 int last_far_set_start;
654
655 if (geo->raid_disks % geo->far_set_size) {
656 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
657 last_far_set_start *= geo->far_set_size;
658
659 if (dev >= last_far_set_start) {
660 far_set_size = geo->far_set_size;
661 far_set_size += (geo->raid_disks % geo->far_set_size);
662 far_set_start = last_far_set_start;
663 }
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
NeilBrown5cf00fc2012-05-21 09:28:20 +1000666 offset = sector & geo->chunk_mask;
667 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700668 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000669 chunk = sector >> geo->chunk_shift;
670 fc = sector_div(chunk, geo->far_copies);
671 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100672 if (dev < far_set_start)
673 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700674 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000675 while (sector >= geo->stride) {
676 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100677 if (dev < (geo->near_copies + far_set_start))
678 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700679 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000680 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700681 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000682 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700683 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000684 vchunk = chunk * geo->raid_disks + dev;
685 sector_div(vchunk, geo->near_copies);
686 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
690 * This routine returns the disk from which the requested read should
691 * be done. There is a per-array 'next expected sequential IO' sector
692 * number - if this matches on the next IO then we use the last disk.
693 * There is also a per-disk 'last know head position' sector that is
694 * maintained from IRQ contexts, both the normal and the resync IO
695 * completion handlers update this position correctly. If there is no
696 * perfect sequential match then we pick the disk whose head is closest.
697 *
698 * If there are 2 mirrors in the same 2 devices, performance degrades
699 * because position is mirror, not device based.
700 *
701 * The rdev for the device selected will have nr_pending incremented.
702 */
703
704/*
705 * FIXME: possibly should rethink readbalancing and do it differently
706 * depending on near_copies / far_copies geometry.
707 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100708static struct md_rdev *read_balance(struct r10conf *conf,
709 struct r10bio *r10_bio,
710 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000712 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000713 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000714 int sectors = r10_bio->sectors;
715 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000716 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000717 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000718 int do_balance;
719 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000720 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 raid10_find_phys(conf, r10_bio);
723 rcu_read_lock();
NeilBrown856e08e2011-07-28 11:39:23 +1000724 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000725 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100726 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000727 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000728 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000729 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100730 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800733 * device if no resync is going on (recovery is ok), or below
734 * the resync window. We take the first readable disk when
735 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
737 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000738 && (this_sector + sectors >= conf->next_resync))
739 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
NeilBrown56d99122011-05-11 14:27:03 +1000741 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000742 sector_t first_bad;
743 int bad_sectors;
744 sector_t dev_sector;
745
NeilBrown56d99122011-05-11 14:27:03 +1000746 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000748 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100749 rdev = rcu_dereference(conf->mirrors[disk].replacement);
750 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
751 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
752 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100753 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700754 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100755 continue;
756 if (!test_bit(In_sync, &rdev->flags) &&
757 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000758 continue;
759
NeilBrown856e08e2011-07-28 11:39:23 +1000760 dev_sector = r10_bio->devs[slot].addr;
761 if (is_badblock(rdev, dev_sector, sectors,
762 &first_bad, &bad_sectors)) {
763 if (best_dist < MaxSector)
764 /* Already have a better slot */
765 continue;
766 if (first_bad <= dev_sector) {
767 /* Cannot read here. If this is the
768 * 'primary' device, then we must not read
769 * beyond 'bad_sectors' from another device.
770 */
771 bad_sectors -= (dev_sector - first_bad);
772 if (!do_balance && sectors > bad_sectors)
773 sectors = bad_sectors;
774 if (best_good_sectors > sectors)
775 best_good_sectors = sectors;
776 } else {
777 sector_t good_sectors =
778 first_bad - dev_sector;
779 if (good_sectors > best_good_sectors) {
780 best_good_sectors = good_sectors;
781 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100782 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000783 }
784 if (!do_balance)
785 /* Must read from here */
786 break;
787 }
788 continue;
789 } else
790 best_good_sectors = sectors;
791
NeilBrown56d99122011-05-11 14:27:03 +1000792 if (!do_balance)
793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
NeilBrown8d3ca832016-11-18 16:16:12 +1100795 if (best_slot >= 0)
796 /* At least 2 disks to choose from so failfast is OK */
797 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800798 /* This optimisation is debatable, and completely destroys
799 * sequential read speed for 'far copies' arrays. So only
800 * keep it for 'near' arrays, and review those later.
801 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000802 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
NeilBrown8d3ca832016-11-18 16:16:12 +1100803 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800804
805 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100806 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000807 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800808 else
NeilBrown56d99122011-05-11 14:27:03 +1000809 new_distance = abs(r10_bio->devs[slot].addr -
810 conf->mirrors[disk].head_position);
811 if (new_distance < best_dist) {
812 best_dist = new_distance;
813 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100814 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 }
NeilBrownabbf0982011-12-23 10:17:54 +1100817 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000818 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100819 rdev = best_rdev;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
NeilBrown56d99122011-05-11 14:27:03 +1000822 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000823 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000824 r10_bio->read_slot = slot;
825 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100826 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000828 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
NeilBrown96c3fd12011-12-23 10:17:54 +1100830 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
NeilBrown5c675f82014-12-15 12:56:56 +1100833static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700834{
NeilBrowne879a872011-10-11 16:49:02 +1100835 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700836 int i, ret = 0;
837
Tejun Heo44522262015-05-22 17:13:26 -0400838 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100839 conf->pending_count >= max_queued_requests)
840 return 1;
841
NeilBrown0d129222006-10-03 01:15:54 -0700842 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000843 for (i = 0;
844 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
845 && ret == 0;
846 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100847 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700848 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200849 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700850
Jan Karadc3b17c2017-02-02 15:56:50 +0100851 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700852 }
853 }
854 rcu_read_unlock();
855 return ret;
856}
857
NeilBrowne879a872011-10-11 16:49:02 +1100858static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800859{
860 /* Any writes that have been queued but are awaiting
861 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800862 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800863 spin_lock_irq(&conf->device_lock);
864
865 if (conf->pending_bio_list.head) {
866 struct bio *bio;
867 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100868 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800869 spin_unlock_irq(&conf->device_lock);
870 /* flush any pending bitmap writes to disk
871 * before proceeding w/ I/O */
872 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100873 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800874
875 while (bio) { /* submit pending writes */
876 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100877 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800878 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100879 bio->bi_bdev = rdev->bdev;
880 if (test_bit(Faulty, &rdev->flags)) {
881 bio->bi_error = -EIO;
882 bio_endio(bio);
883 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
884 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li532a2a32012-10-11 13:30:52 +1100885 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200886 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100887 else
888 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800889 bio = next;
890 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800891 } else
892 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800893}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100894
NeilBrown0a27ec92006-01-06 00:20:13 -0800895/* Barriers....
896 * Sometimes we need to suspend IO while we do something else,
897 * either some resync/recovery, or reconfigure the array.
898 * To do this we raise a 'barrier'.
899 * The 'barrier' is a counter that can be raised multiple times
900 * to count how many activities are happening which preclude
901 * normal IO.
902 * We can only raise the barrier if there is no pending IO.
903 * i.e. if nr_pending == 0.
904 * We choose only to raise the barrier if no-one is waiting for the
905 * barrier to go down. This means that as soon as an IO request
906 * is ready, no other operations which require a barrier will start
907 * until the IO request has had a chance.
908 *
909 * So: regular IO calls 'wait_barrier'. When that returns there
910 * is no backgroup IO happening, It must arrange to call
911 * allow_barrier when it has finished its IO.
912 * backgroup IO calls must call raise_barrier. Once that returns
913 * there is no normal IO happeing. It must arrange to call
914 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
NeilBrowne879a872011-10-11 16:49:02 +1100917static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
NeilBrown6cce3b22006-01-06 00:20:16 -0800919 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
NeilBrown6cce3b22006-01-06 00:20:16 -0800922 /* Wait until no block IO is waiting (unless 'force') */
923 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100924 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800925
926 /* block any new IO from starting */
927 conf->barrier++;
928
NeilBrownc3b328a2011-04-18 18:25:43 +1000929 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800930 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200931 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100932 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 spin_unlock_irq(&conf->resync_lock);
935}
936
NeilBrowne879a872011-10-11 16:49:02 +1100937static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800938{
939 unsigned long flags;
940 spin_lock_irqsave(&conf->resync_lock, flags);
941 conf->barrier--;
942 spin_unlock_irqrestore(&conf->resync_lock, flags);
943 wake_up(&conf->wait_barrier);
944}
945
NeilBrowne879a872011-10-11 16:49:02 +1100946static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800947{
948 spin_lock_irq(&conf->resync_lock);
949 if (conf->barrier) {
950 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100951 /* Wait for the barrier to drop.
952 * However if there are already pending
953 * requests (preventing the barrier from
954 * rising completely), and the
955 * pre-process bio queue isn't empty,
956 * then don't wait, as we need to empty
957 * that queue to get the nr_pending
958 * count down.
959 */
NeilBrown578b54a2016-11-14 16:30:21 +1100960 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100961 wait_event_lock_irq(conf->wait_barrier,
962 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200963 (atomic_read(&conf->nr_pending) &&
NeilBrownd6b42dc2012-03-19 12:46:38 +1100964 current->bio_list &&
NeilBrownf5fe1b52017-03-10 17:00:47 +1100965 (!bio_list_empty(&current->bio_list[0]) ||
966 !bio_list_empty(&current->bio_list[1]))),
Lukas Czernereed8c022012-11-30 11:42:40 +0100967 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800968 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200969 if (!conf->nr_waiting)
970 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800971 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200972 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800973 spin_unlock_irq(&conf->resync_lock);
974}
975
NeilBrownfd16f2e2017-03-15 14:05:13 +1100976static void inc_pending(struct r10conf *conf)
977{
978 /* The current request requires multiple r10_bio, so
979 * we need to increment the pending count.
980 */
981 WARN_ON(!atomic_read(&conf->nr_pending));
982 atomic_inc(&conf->nr_pending);
983}
984
NeilBrowne879a872011-10-11 16:49:02 +1100985static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800986{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200987 if ((atomic_dec_and_test(&conf->nr_pending)) ||
988 (conf->array_freeze_pending))
989 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800990}
991
NeilBrowne2d59922013-06-12 11:01:22 +1000992static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -0800993{
994 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800995 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800996 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +1000997 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -0800998 * This is called in the context of one normal IO request
999 * that has failed. Thus any sync request that might be pending
1000 * will be blocked by nr_pending, and we need to wait for
1001 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001002 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001003 * must match the number of pending IOs (nr_pending) before
1004 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001005 */
1006 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001007 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001008 conf->barrier++;
1009 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001010 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001011 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001012 conf->resync_lock,
1013 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001014
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001015 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001016 spin_unlock_irq(&conf->resync_lock);
1017}
1018
NeilBrowne879a872011-10-11 16:49:02 +11001019static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001020{
1021 /* reverse the effect of the freeze */
1022 spin_lock_irq(&conf->resync_lock);
1023 conf->barrier--;
1024 conf->nr_waiting--;
1025 wake_up(&conf->wait_barrier);
1026 spin_unlock_irq(&conf->resync_lock);
1027}
1028
NeilBrownf8c9e742012-05-21 09:28:33 +10001029static sector_t choose_data_offset(struct r10bio *r10_bio,
1030 struct md_rdev *rdev)
1031{
1032 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1033 test_bit(R10BIO_Previous, &r10_bio->state))
1034 return rdev->data_offset;
1035 else
1036 return rdev->new_data_offset;
1037}
1038
NeilBrown57c67df2012-10-11 13:32:13 +11001039struct raid10_plug_cb {
1040 struct blk_plug_cb cb;
1041 struct bio_list pending;
1042 int pending_cnt;
1043};
1044
1045static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1046{
1047 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1048 cb);
1049 struct mddev *mddev = plug->cb.data;
1050 struct r10conf *conf = mddev->private;
1051 struct bio *bio;
1052
NeilBrown874807a2012-11-27 12:14:40 +11001053 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001054 spin_lock_irq(&conf->device_lock);
1055 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1056 conf->pending_count += plug->pending_cnt;
1057 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001058 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001059 md_wakeup_thread(mddev->thread);
1060 kfree(plug);
1061 return;
1062 }
1063
1064 /* we aren't scheduling, so we can do the write-out directly. */
1065 bio = bio_list_get(&plug->pending);
1066 bitmap_unplug(mddev->bitmap);
1067 wake_up(&conf->wait_barrier);
1068
1069 while (bio) { /* submit pending writes */
1070 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001071 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001072 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001073 bio->bi_bdev = rdev->bdev;
1074 if (test_bit(Faulty, &rdev->flags)) {
1075 bio->bi_error = -EIO;
1076 bio_endio(bio);
1077 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1078 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001079 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001080 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001081 else
1082 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001083 bio = next;
1084 }
1085 kfree(plug);
1086}
1087
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001088static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1089 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
NeilBrowne879a872011-10-11 16:49:02 +11001091 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001093 const int op = bio_op(bio);
1094 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1095 int sectors_handled;
1096 int max_sectors;
1097 sector_t sectors;
1098 struct md_rdev *rdev;
1099 int slot;
1100
1101 /*
1102 * Register the new request and wait if the reconstruction
1103 * thread has put up a bar for new requests.
1104 * Continue immediately if no resync is active currently.
1105 */
1106 wait_barrier(conf);
1107
1108 sectors = bio_sectors(bio);
1109 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1110 bio->bi_iter.bi_sector < conf->reshape_progress &&
1111 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1112 /*
1113 * IO spans the reshape position. Need to wait for reshape to
1114 * pass
1115 */
1116 raid10_log(conf->mddev, "wait reshape");
1117 allow_barrier(conf);
1118 wait_event(conf->wait_barrier,
1119 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1120 conf->reshape_progress >= bio->bi_iter.bi_sector +
1121 sectors);
1122 wait_barrier(conf);
1123 }
1124
1125read_again:
1126 rdev = read_balance(conf, r10_bio, &max_sectors);
1127 if (!rdev) {
1128 raid_end_bio_io(r10_bio);
1129 return;
1130 }
1131 slot = r10_bio->read_slot;
1132
Ming Leid7a10302017-02-14 23:29:03 +08001133 read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001134 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
1135 max_sectors);
1136
1137 r10_bio->devs[slot].bio = read_bio;
1138 r10_bio->devs[slot].rdev = rdev;
1139
1140 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1141 choose_data_offset(r10_bio, rdev);
1142 read_bio->bi_bdev = rdev->bdev;
1143 read_bio->bi_end_io = raid10_end_read_request;
1144 bio_set_op_attrs(read_bio, op, do_sync);
1145 if (test_bit(FailFast, &rdev->flags) &&
1146 test_bit(R10BIO_FailFast, &r10_bio->state))
1147 read_bio->bi_opf |= MD_FAILFAST;
1148 read_bio->bi_private = r10_bio;
1149
1150 if (mddev->gendisk)
1151 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1152 read_bio, disk_devt(mddev->gendisk),
1153 r10_bio->sector);
1154 if (max_sectors < r10_bio->sectors) {
1155 /*
1156 * Could not read all from this device, so we will need another
1157 * r10_bio.
1158 */
1159 sectors_handled = (r10_bio->sector + max_sectors
1160 - bio->bi_iter.bi_sector);
1161 r10_bio->sectors = max_sectors;
NeilBrownfd16f2e2017-03-15 14:05:13 +11001162 inc_pending(conf);
1163 bio_inc_remaining(bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001164 /*
1165 * Cannot call generic_make_request directly as that will be
1166 * queued in __generic_make_request and subsequent
1167 * mempool_alloc might block waiting for it. so hand bio over
1168 * to raid10d.
1169 */
1170 reschedule_retry(r10_bio);
1171
1172 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1173
1174 r10_bio->master_bio = bio;
1175 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1176 r10_bio->state = 0;
1177 r10_bio->mddev = mddev;
1178 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1179 goto read_again;
1180 } else
1181 generic_make_request(read_bio);
1182 return;
1183}
1184
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001185static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1186 struct bio *bio, bool replacement,
1187 int n_copy, int max_sectors)
1188{
1189 const int op = bio_op(bio);
1190 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1191 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1192 unsigned long flags;
1193 struct blk_plug_cb *cb;
1194 struct raid10_plug_cb *plug = NULL;
1195 struct r10conf *conf = mddev->private;
1196 struct md_rdev *rdev;
1197 int devnum = r10_bio->devs[n_copy].devnum;
1198 struct bio *mbio;
1199
1200 if (replacement) {
1201 rdev = conf->mirrors[devnum].replacement;
1202 if (rdev == NULL) {
1203 /* Replacement just got moved to main 'rdev' */
1204 smp_mb();
1205 rdev = conf->mirrors[devnum].rdev;
1206 }
1207 } else
1208 rdev = conf->mirrors[devnum].rdev;
1209
1210 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
1211 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
1212 if (replacement)
1213 r10_bio->devs[n_copy].repl_bio = mbio;
1214 else
1215 r10_bio->devs[n_copy].bio = mbio;
1216
1217 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1218 choose_data_offset(r10_bio, rdev));
1219 mbio->bi_bdev = rdev->bdev;
1220 mbio->bi_end_io = raid10_end_write_request;
1221 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1222 if (!replacement && test_bit(FailFast,
1223 &conf->mirrors[devnum].rdev->flags)
1224 && enough(conf, devnum))
1225 mbio->bi_opf |= MD_FAILFAST;
1226 mbio->bi_private = r10_bio;
1227
1228 if (conf->mddev->gendisk)
1229 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1230 mbio, disk_devt(conf->mddev->gendisk),
1231 r10_bio->sector);
1232 /* flush_pending_writes() needs access to the rdev so...*/
1233 mbio->bi_bdev = (void *)rdev;
1234
1235 atomic_inc(&r10_bio->remaining);
1236
1237 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1238 if (cb)
1239 plug = container_of(cb, struct raid10_plug_cb, cb);
1240 else
1241 plug = NULL;
1242 spin_lock_irqsave(&conf->device_lock, flags);
1243 if (plug) {
1244 bio_list_add(&plug->pending, mbio);
1245 plug->pending_cnt++;
1246 } else {
1247 bio_list_add(&conf->pending_bio_list, mbio);
1248 conf->pending_count++;
1249 }
1250 spin_unlock_irqrestore(&conf->device_lock, flags);
1251 if (!plug)
1252 md_wakeup_thread(mddev->thread);
1253}
1254
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001255static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1256 struct r10bio *r10_bio)
1257{
1258 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11001260 struct md_rdev *blocked_rdev;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001261 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001262 int sectors_handled;
1263 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Tomasz Majchrzak9b622e22016-07-28 10:28:25 +02001265 md_write_start(mddev, bio);
1266
NeilBrowncc13b1d2014-05-05 13:34:37 +10001267 /*
1268 * Register the new request and wait if the reconstruction
1269 * thread has put up a bar for new requests.
1270 * Continue immediately if no resync is active currently.
1271 */
1272 wait_barrier(conf);
1273
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001274 sectors = bio_sectors(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001275 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001276 bio->bi_iter.bi_sector < conf->reshape_progress &&
1277 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001278 /*
1279 * IO spans the reshape position. Need to wait for reshape to
1280 * pass
NeilBrown3ea7daa2012-05-22 13:53:47 +10001281 */
NeilBrown578b54a2016-11-14 16:30:21 +11001282 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001283 allow_barrier(conf);
1284 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001285 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1286 conf->reshape_progress >= bio->bi_iter.bi_sector +
1287 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001288 wait_barrier(conf);
1289 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001290
NeilBrown3ea7daa2012-05-22 13:53:47 +10001291 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001292 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001293 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1294 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1295 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1296 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001297 /* Need to update reshape_position in metadata */
1298 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001299 set_mask_bits(&mddev->sb_flags, 0,
1300 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001301 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001302 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001303 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001304 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001305
1306 conf->reshape_safe = mddev->reshape_position;
1307 }
1308
NeilBrown34db0cd2011-10-11 16:50:01 +11001309 if (conf->pending_count >= max_queued_requests) {
1310 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001311 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001312 wait_event(conf->wait_barrier,
1313 conf->pending_count < max_queued_requests);
1314 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001315 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * inc refcount on their rdev. Record them by setting
1317 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001318 * If there are known/acknowledged bad blocks on any device
1319 * on which we have seen a write error, we want to avoid
1320 * writing to those blocks. This potentially requires several
1321 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001322 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001324
NeilBrown69335ef2011-12-23 10:17:54 +11001325 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001327retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001328 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001330 max_sectors = r10_bio->sectors;
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 for (i = 0; i < conf->copies; i++) {
1333 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001334 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001335 struct md_rdev *rrdev = rcu_dereference(
1336 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001337 if (rdev == rrdev)
1338 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001339 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1340 atomic_inc(&rdev->nr_pending);
1341 blocked_rdev = rdev;
1342 break;
1343 }
NeilBrown475b0322011-12-23 10:17:55 +11001344 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1345 atomic_inc(&rrdev->nr_pending);
1346 blocked_rdev = rrdev;
1347 break;
1348 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001349 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001350 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001351 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001352 rrdev = NULL;
1353
NeilBrownd4432c22011-07-28 11:39:24 +10001354 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001355 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001356
1357 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001358 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001359 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001360 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001361 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001362 sector_t first_bad;
1363 sector_t dev_sector = r10_bio->devs[i].addr;
1364 int bad_sectors;
1365 int is_bad;
1366
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001367 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001368 &first_bad, &bad_sectors);
1369 if (is_bad < 0) {
1370 /* Mustn't write here until the bad block
1371 * is acknowledged
1372 */
1373 atomic_inc(&rdev->nr_pending);
1374 set_bit(BlockedBadBlocks, &rdev->flags);
1375 blocked_rdev = rdev;
1376 break;
1377 }
1378 if (is_bad && first_bad <= dev_sector) {
1379 /* Cannot write here at all */
1380 bad_sectors -= (dev_sector - first_bad);
1381 if (bad_sectors < max_sectors)
1382 /* Mustn't write more than bad_sectors
1383 * to other devices yet
1384 */
1385 max_sectors = bad_sectors;
1386 /* We don't set R10BIO_Degraded as that
1387 * only applies if the disk is missing,
1388 * so it might be re-added, and we want to
1389 * know to recover this chunk.
1390 * In this case the device is here, and the
1391 * fact that this chunk is not in-sync is
1392 * recorded in the bad block log.
1393 */
1394 continue;
1395 }
1396 if (is_bad) {
1397 int good_sectors = first_bad - dev_sector;
1398 if (good_sectors < max_sectors)
1399 max_sectors = good_sectors;
1400 }
1401 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001402 if (rdev) {
1403 r10_bio->devs[i].bio = bio;
1404 atomic_inc(&rdev->nr_pending);
1405 }
NeilBrown475b0322011-12-23 10:17:55 +11001406 if (rrdev) {
1407 r10_bio->devs[i].repl_bio = bio;
1408 atomic_inc(&rrdev->nr_pending);
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 rcu_read_unlock();
1412
Dan Williams6bfe0b42008-04-30 00:52:32 -07001413 if (unlikely(blocked_rdev)) {
1414 /* Have to wait for this device to get unblocked, then retry */
1415 int j;
1416 int d;
1417
NeilBrown475b0322011-12-23 10:17:55 +11001418 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001419 if (r10_bio->devs[j].bio) {
1420 d = r10_bio->devs[j].devnum;
1421 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1422 }
NeilBrown475b0322011-12-23 10:17:55 +11001423 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001424 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001425 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001426 rdev = conf->mirrors[d].replacement;
1427 if (!rdev) {
1428 /* Race with remove_disk */
1429 smp_mb();
1430 rdev = conf->mirrors[d].rdev;
1431 }
1432 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001433 }
1434 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001435 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001436 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001437 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1438 wait_barrier(conf);
1439 goto retry_write;
1440 }
1441
NeilBrown6b6c8112017-03-15 14:05:13 +11001442 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001443 r10_bio->sectors = max_sectors;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001444 sectors_handled = r10_bio->sector + max_sectors -
1445 bio->bi_iter.bi_sector;
NeilBrownd4432c22011-07-28 11:39:24 +10001446
NeilBrown4e780642010-10-19 12:54:01 +11001447 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001448 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 for (i = 0; i < conf->copies; i++) {
Guoqing Jiang27f26a02017-03-20 17:46:04 +08001451 if (r10_bio->devs[i].bio)
1452 raid10_write_one_disk(mddev, r10_bio, bio, false,
1453 i, max_sectors);
1454 if (r10_bio->devs[i].repl_bio)
1455 raid10_write_one_disk(mddev, r10_bio, bio, true,
1456 i, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
NeilBrown079fa162011-09-10 17:21:23 +10001459 /* Don't remove the bias on 'remaining' (one_write_done) until
1460 * after checking if we need to go around again.
1461 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001462
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001463 if (sectors_handled < bio_sectors(bio)) {
NeilBrownfd16f2e2017-03-15 14:05:13 +11001464 /* We need another r10_bio and it needs to be counted */
1465 inc_pending(conf);
1466 bio_inc_remaining(bio);
NeilBrown6b6c8112017-03-15 14:05:13 +11001467 one_write_done(r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001468 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1469
1470 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001471 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001472
1473 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001474 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001475 r10_bio->state = 0;
1476 goto retry_write;
1477 }
NeilBrown079fa162011-09-10 17:21:23 +10001478 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001479}
1480
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001481static void __make_request(struct mddev *mddev, struct bio *bio)
1482{
1483 struct r10conf *conf = mddev->private;
1484 struct r10bio *r10_bio;
1485
1486 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1487
1488 r10_bio->master_bio = bio;
1489 r10_bio->sectors = bio_sectors(bio);
1490
1491 r10_bio->mddev = mddev;
1492 r10_bio->sector = bio->bi_iter.bi_sector;
1493 r10_bio->state = 0;
1494
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001495 if (bio_data_dir(bio) == READ)
1496 raid10_read_request(mddev, bio, r10_bio);
1497 else
1498 raid10_write_request(mddev, bio, r10_bio);
1499}
1500
Shaohua Li849674e2016-01-20 13:52:20 -08001501static void raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001502{
1503 struct r10conf *conf = mddev->private;
1504 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1505 int chunk_sects = chunk_mask + 1;
1506
1507 struct bio *split;
1508
Jens Axboe1eff9d32016-08-05 15:35:16 -06001509 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001510 md_flush_request(mddev, bio);
1511 return;
1512 }
1513
Kent Overstreet20d01892013-11-23 18:21:01 -08001514 do {
1515
1516 /*
1517 * If this request crosses a chunk boundary, we need to split
1518 * it.
1519 */
1520 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1521 bio_sectors(bio) > chunk_sects
1522 && (conf->geo.near_copies < conf->geo.raid_disks
1523 || conf->prev.near_copies <
1524 conf->prev.raid_disks))) {
1525 split = bio_split(bio, chunk_sects -
1526 (bio->bi_iter.bi_sector &
1527 (chunk_sects - 1)),
1528 GFP_NOIO, fs_bio_set);
1529 bio_chain(split, bio);
1530 } else {
1531 split = bio;
1532 }
1533
Shaohua Li61eb2b42017-02-28 13:00:20 -08001534 /*
1535 * If a bio is splitted, the first part of bio will pass
1536 * barrier but the bio is queued in current->bio_list (see
1537 * generic_make_request). If there is a raise_barrier() called
1538 * here, the second part of bio can't pass barrier. But since
1539 * the first part bio isn't dispatched to underlaying disks
1540 * yet, the barrier is never released, hence raise_barrier will
1541 * alays wait. We have a deadlock.
1542 * Note, this only happens in read path. For write path, the
1543 * first part of bio is dispatched in a schedule() call
1544 * (because of blk plug) or offloaded to raid10d.
1545 * Quitting from the function immediately can change the bio
1546 * order queued in bio_list and avoid the deadlock.
1547 */
Kent Overstreet20d01892013-11-23 18:21:01 -08001548 __make_request(mddev, split);
Shaohua Li61eb2b42017-02-28 13:00:20 -08001549 if (split != bio && bio_data_dir(bio) == READ) {
1550 generic_make_request(bio);
1551 break;
1552 }
Kent Overstreet20d01892013-11-23 18:21:01 -08001553 } while (split != bio);
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{
1920 struct r10bio *r10_bio = bio->bi_private;
1921 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{
1929 struct r10bio *r10_bio = bio->bi_private;
1930
1931 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1932}
1933
NeilBrown9f2c9d12011-10-11 16:48:43 +11001934static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001935{
NeilBrownfd01b882011-10-11 16:47:53 +11001936 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001937
1938 while (atomic_dec_and_test(&r10_bio->remaining)) {
1939 if (r10_bio->master_bio == NULL) {
1940 /* the primary of several recovery bios */
1941 sector_t s = r10_bio->sectors;
1942 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1943 test_bit(R10BIO_WriteError, &r10_bio->state))
1944 reschedule_retry(r10_bio);
1945 else
1946 put_buf(r10_bio);
1947 md_done_sync(mddev, s, 1);
1948 break;
1949 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001950 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001951 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1952 test_bit(R10BIO_WriteError, &r10_bio->state))
1953 reschedule_retry(r10_bio);
1954 else
1955 put_buf(r10_bio);
1956 r10_bio = r10_bio2;
1957 }
1958 }
1959}
1960
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001961static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001963 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001964 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001965 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001966 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001967 sector_t first_bad;
1968 int bad_sectors;
1969 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001970 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001971 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
NeilBrown9ad1aef2011-12-23 10:17:55 +11001973 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1974 if (repl)
1975 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001976 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001977 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001979 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001980 if (repl)
1981 md_error(mddev, rdev);
1982 else {
1983 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001984 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1985 set_bit(MD_RECOVERY_NEEDED,
1986 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001987 set_bit(R10BIO_WriteError, &r10_bio->state);
1988 }
1989 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001990 r10_bio->devs[slot].addr,
1991 r10_bio->sectors,
1992 &first_bad, &bad_sectors))
1993 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001994
NeilBrown9ad1aef2011-12-23 10:17:55 +11001995 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001996
1997 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000/*
2001 * Note: sync and recover and handled very differently for raid10
2002 * This code is for resync.
2003 * For resync, we read through virtual addresses and read all blocks.
2004 * If there is any error, we schedule a write. The lowest numbered
2005 * drive is authoritative.
2006 * However requests come for physical address, so we need to map.
2007 * For every physical address there are raid_disks/copies virtual addresses,
2008 * which is always are least one, but is not necessarly an integer.
2009 * This means that a physical address can span multiple chunks, so we may
2010 * have to submit multiple io requests for a single sync request.
2011 */
2012/*
2013 * We check if all blocks are in-sync and only write to blocks that
2014 * aren't in sync
2015 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002016static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
NeilBrowne879a872011-10-11 16:49:02 +11002018 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 int i, first;
2020 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002021 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 atomic_set(&r10_bio->remaining, 1);
2024
2025 /* find the first device with a block */
2026 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002027 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 break;
2029
2030 if (i == conf->copies)
2031 goto done;
2032
2033 first = i;
2034 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002035 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2036 fbio->bi_iter.bi_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
majianpengf4380a92012-04-12 16:04:47 +10002038 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002040 for (i=0 ; i < conf->copies ; i++) {
2041 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002042 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002045
2046 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002048 if (i == first)
2049 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002050 d = r10_bio->devs[i].devnum;
2051 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002052 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002053 /* We know that the bi_io_vec layout is the same for
2054 * both 'first' and 'i', so we just compare them.
2055 * All vec entries are PAGE_SIZE;
2056 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002057 int sectors = r10_bio->sectors;
2058 for (j = 0; j < vcnt; j++) {
2059 int len = PAGE_SIZE;
2060 if (sectors < (len / 512))
2061 len = sectors * 512;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002062 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2063 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown7bb23c42013-07-16 16:50:47 +10002064 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002065 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002066 sectors -= len/512;
2067 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002068 if (j == vcnt)
2069 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002070 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002071 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2072 /* Don't fix anything. */
2073 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002074 } else if (test_bit(FailFast, &rdev->flags)) {
2075 /* Just give up on this device */
2076 md_error(rdev->mddev, rdev);
2077 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002078 }
NeilBrownf84ee362011-07-28 11:39:25 +10002079 /* Ok, we need to write this bio, either to correct an
2080 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 * First we need to fixup bv_offset, bv_len and
2082 * bi_vecs, as the read request might have corrupted these
2083 */
Kent Overstreet8be185f2012-09-06 14:14:43 -07002084 bio_reset(tbio);
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 tbio->bi_vcnt = vcnt;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002087 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 tbio->bi_private = r10_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002089 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002091 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Kent Overstreetc31df252015-05-06 23:34:20 -07002093 bio_copy_data(tbio, fbio);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2096 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002097 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
NeilBrown1919cbb2016-11-18 16:16:12 +11002099 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2100 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002101 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2103 generic_make_request(tbio);
2104 }
2105
NeilBrown9ad1aef2011-12-23 10:17:55 +11002106 /* Now write out to any replacement devices
2107 * that are active
2108 */
2109 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002110 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002111
2112 tbio = r10_bio->devs[i].repl_bio;
2113 if (!tbio || !tbio->bi_end_io)
2114 continue;
2115 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2116 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002117 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002118 d = r10_bio->devs[i].devnum;
2119 atomic_inc(&r10_bio->remaining);
2120 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002121 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002122 generic_make_request(tbio);
2123 }
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125done:
2126 if (atomic_dec_and_test(&r10_bio->remaining)) {
2127 md_done_sync(mddev, r10_bio->sectors, 1);
2128 put_buf(r10_bio);
2129 }
2130}
2131
2132/*
2133 * Now for the recovery code.
2134 * Recovery happens across physical sectors.
2135 * We recover all non-is_sync drives by finding the virtual address of
2136 * each, and then choose a working drive that also has that virt address.
2137 * There is a separate r10_bio for each non-in_sync drive.
2138 * Only the first two slots are in use. The first for reading,
2139 * The second for writing.
2140 *
2141 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002142static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002143{
2144 /* We got a read error during recovery.
2145 * We repeat the read in smaller page-sized sections.
2146 * If a read succeeds, write it to the new device or record
2147 * a bad block if we cannot.
2148 * If a read fails, record a bad block on both old and
2149 * new devices.
2150 */
NeilBrownfd01b882011-10-11 16:47:53 +11002151 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002152 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002153 struct bio *bio = r10_bio->devs[0].bio;
2154 sector_t sect = 0;
2155 int sectors = r10_bio->sectors;
2156 int idx = 0;
2157 int dr = r10_bio->devs[0].devnum;
2158 int dw = r10_bio->devs[1].devnum;
2159
2160 while (sectors) {
2161 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002162 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002163 sector_t addr;
2164 int ok;
2165
2166 if (s > (PAGE_SIZE>>9))
2167 s = PAGE_SIZE >> 9;
2168
2169 rdev = conf->mirrors[dr].rdev;
2170 addr = r10_bio->devs[0].addr + sect,
2171 ok = sync_page_io(rdev,
2172 addr,
2173 s << 9,
2174 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002175 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002176 if (ok) {
2177 rdev = conf->mirrors[dw].rdev;
2178 addr = r10_bio->devs[1].addr + sect;
2179 ok = sync_page_io(rdev,
2180 addr,
2181 s << 9,
2182 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002183 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002184 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002185 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002186 if (!test_and_set_bit(WantReplacement,
2187 &rdev->flags))
2188 set_bit(MD_RECOVERY_NEEDED,
2189 &rdev->mddev->recovery);
2190 }
NeilBrown5e570282011-07-28 11:39:25 +10002191 }
2192 if (!ok) {
2193 /* We don't worry if we cannot set a bad block -
2194 * it really is bad so there is no loss in not
2195 * recording it yet
2196 */
2197 rdev_set_badblocks(rdev, addr, s, 0);
2198
2199 if (rdev != conf->mirrors[dw].rdev) {
2200 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002201 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002202 addr = r10_bio->devs[1].addr + sect;
2203 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2204 if (!ok) {
2205 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002206 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2207 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002208
2209 conf->mirrors[dw].recovery_disabled
2210 = mddev->recovery_disabled;
2211 set_bit(MD_RECOVERY_INTR,
2212 &mddev->recovery);
2213 break;
2214 }
2215 }
2216 }
2217
2218 sectors -= s;
2219 sect += s;
2220 idx++;
2221 }
2222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
NeilBrown9f2c9d12011-10-11 16:48:43 +11002224static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
NeilBrowne879a872011-10-11 16:49:02 +11002226 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002227 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002228 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
NeilBrown5e570282011-07-28 11:39:25 +10002230 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2231 fix_recovery_read_error(r10_bio);
2232 end_sync_request(r10_bio);
2233 return;
2234 }
2235
Namhyung Kimc65060a2011-07-18 17:38:49 +10002236 /*
2237 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * and submit the write request
2239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002241 wbio = r10_bio->devs[1].bio;
2242 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002243 /* Need to test wbio2->bi_end_io before we call
2244 * generic_make_request as if the former is NULL,
2245 * the latter is free to free wbio2.
2246 */
2247 if (wbio2 && !wbio2->bi_end_io)
2248 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002249 if (wbio->bi_end_io) {
2250 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002251 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002252 generic_make_request(wbio);
2253 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002254 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002255 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2256 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002257 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002258 generic_make_request(wbio2);
2259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262/*
Robert Becker1e509152009-12-14 12:49:58 +11002263 * Used by fix_read_error() to decay the per rdev read_errors.
2264 * We halve the read error count for every hour that has elapsed
2265 * since the last recorded read error.
2266 *
2267 */
NeilBrownfd01b882011-10-11 16:47:53 +11002268static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002269{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002270 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002271 unsigned long hours_since_last;
2272 unsigned int read_errors = atomic_read(&rdev->read_errors);
2273
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002274 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002275
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002276 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002277 /* first time we've seen a read error */
2278 rdev->last_read_error = cur_time_mon;
2279 return;
2280 }
2281
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002282 hours_since_last = (long)(cur_time_mon -
2283 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002284
2285 rdev->last_read_error = cur_time_mon;
2286
2287 /*
2288 * if hours_since_last is > the number of bits in read_errors
2289 * just set read errors to 0. We do this to avoid
2290 * overflowing the shift of read_errors by hours_since_last.
2291 */
2292 if (hours_since_last >= 8 * sizeof(read_errors))
2293 atomic_set(&rdev->read_errors, 0);
2294 else
2295 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2296}
2297
NeilBrown3cb03002011-10-11 16:45:26 +11002298static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002299 int sectors, struct page *page, int rw)
2300{
2301 sector_t first_bad;
2302 int bad_sectors;
2303
2304 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2305 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2306 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002307 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002308 /* success */
2309 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002310 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002311 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002312 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2313 set_bit(MD_RECOVERY_NEEDED,
2314 &rdev->mddev->recovery);
2315 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002316 /* need to record an error - either for the block or the device */
2317 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2318 md_error(rdev->mddev, rdev);
2319 return 0;
2320}
2321
Robert Becker1e509152009-12-14 12:49:58 +11002322/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 * This is a kernel thread which:
2324 *
2325 * 1. Retries failed read operations on working mirrors.
2326 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002327 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 */
2329
NeilBrowne879a872011-10-11 16:49:02 +11002330static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002331{
2332 int sect = 0; /* Offset from r10_bio->sector */
2333 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002334 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002335 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002336 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002337
NeilBrown7c4e06f2011-05-11 14:53:17 +10002338 /* still own a reference to this rdev, so it cannot
2339 * have been cleared recently.
2340 */
2341 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002342
NeilBrown7c4e06f2011-05-11 14:53:17 +10002343 if (test_bit(Faulty, &rdev->flags))
2344 /* drive has already been failed, just ignore any
2345 more fix_read_error() attempts */
2346 return;
2347
2348 check_decay_read_errors(mddev, rdev);
2349 atomic_inc(&rdev->read_errors);
2350 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2351 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002352 bdevname(rdev->bdev, b);
2353
NeilBrown08464e02016-11-02 14:16:50 +11002354 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2355 mdname(mddev), b,
2356 atomic_read(&rdev->read_errors), max_read_errors);
2357 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2358 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002359 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002360 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002361 return;
Robert Becker1e509152009-12-14 12:49:58 +11002362 }
Robert Becker1e509152009-12-14 12:49:58 +11002363
NeilBrown6814d532006-10-03 01:15:45 -07002364 while(sectors) {
2365 int s = sectors;
2366 int sl = r10_bio->read_slot;
2367 int success = 0;
2368 int start;
2369
2370 if (s > (PAGE_SIZE>>9))
2371 s = PAGE_SIZE >> 9;
2372
2373 rcu_read_lock();
2374 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002375 sector_t first_bad;
2376 int bad_sectors;
2377
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002378 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002379 rdev = rcu_dereference(conf->mirrors[d].rdev);
2380 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002381 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002382 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002383 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2384 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002385 atomic_inc(&rdev->nr_pending);
2386 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002387 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002388 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002389 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002390 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002391 conf->tmppage,
2392 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002393 rdev_dec_pending(rdev, mddev);
2394 rcu_read_lock();
2395 if (success)
2396 break;
2397 }
2398 sl++;
2399 if (sl == conf->copies)
2400 sl = 0;
2401 } while (!success && sl != r10_bio->read_slot);
2402 rcu_read_unlock();
2403
2404 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002405 /* Cannot read from anywhere, just mark the block
2406 * as bad on the first device to discourage future
2407 * reads.
2408 */
NeilBrown6814d532006-10-03 01:15:45 -07002409 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002410 rdev = conf->mirrors[dn].rdev;
2411
2412 if (!rdev_set_badblocks(
2413 rdev,
2414 r10_bio->devs[r10_bio->read_slot].addr
2415 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002416 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002417 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002418 r10_bio->devs[r10_bio->read_slot].bio
2419 = IO_BLOCKED;
2420 }
NeilBrown6814d532006-10-03 01:15:45 -07002421 break;
2422 }
2423
2424 start = sl;
2425 /* write it back and re-read */
2426 rcu_read_lock();
2427 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002428 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002429
NeilBrown6814d532006-10-03 01:15:45 -07002430 if (sl==0)
2431 sl = conf->copies;
2432 sl--;
2433 d = r10_bio->devs[sl].devnum;
2434 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002435 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002436 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002437 !test_bit(In_sync, &rdev->flags))
2438 continue;
2439
2440 atomic_inc(&rdev->nr_pending);
2441 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002442 if (r10_sync_page_io(rdev,
2443 r10_bio->devs[sl].addr +
2444 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002445 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002446 == 0) {
2447 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002448 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2449 mdname(mddev), s,
2450 (unsigned long long)(
2451 sect +
2452 choose_data_offset(r10_bio,
2453 rdev)),
2454 bdevname(rdev->bdev, b));
2455 pr_notice("md/raid10:%s: %s: failing drive\n",
2456 mdname(mddev),
2457 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002458 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002459 rdev_dec_pending(rdev, mddev);
2460 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002461 }
2462 sl = start;
2463 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002464 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002465
NeilBrown6814d532006-10-03 01:15:45 -07002466 if (sl==0)
2467 sl = conf->copies;
2468 sl--;
2469 d = r10_bio->devs[sl].devnum;
2470 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002471 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002472 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002473 !test_bit(In_sync, &rdev->flags))
2474 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002475
NeilBrown1294b9c2011-07-28 11:39:23 +10002476 atomic_inc(&rdev->nr_pending);
2477 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002478 switch (r10_sync_page_io(rdev,
2479 r10_bio->devs[sl].addr +
2480 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002481 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002482 READ)) {
2483 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002484 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002485 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002486 mdname(mddev), s,
2487 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002488 sect +
2489 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002490 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002491 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002492 mdname(mddev),
2493 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002494 break;
2495 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002496 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002497 mdname(mddev), s,
2498 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002499 sect +
2500 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002501 bdevname(rdev->bdev, b));
2502 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002503 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002504
2505 rdev_dec_pending(rdev, mddev);
2506 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002507 }
2508 rcu_read_unlock();
2509
2510 sectors -= s;
2511 sect += s;
2512 }
2513}
2514
NeilBrown9f2c9d12011-10-11 16:48:43 +11002515static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002516{
2517 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002518 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002519 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002520 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002521 /* bio has the data to be written to slot 'i' where
2522 * we just recently had a write error.
2523 * We repeatedly clone the bio and trim down to one block,
2524 * then try the write. Where the write fails we record
2525 * a bad block.
2526 * It is conceivable that the bio doesn't exactly align with
2527 * blocks. We must handle this.
2528 *
2529 * We currently own a reference to the rdev.
2530 */
2531
2532 int block_sectors;
2533 sector_t sector;
2534 int sectors;
2535 int sect_to_write = r10_bio->sectors;
2536 int ok = 1;
2537
2538 if (rdev->badblocks.shift < 0)
2539 return 0;
2540
NeilBrownf04ebb02015-02-16 14:51:54 +11002541 block_sectors = roundup(1 << rdev->badblocks.shift,
2542 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002543 sector = r10_bio->sector;
2544 sectors = ((r10_bio->sector + block_sectors)
2545 & ~(sector_t)(block_sectors - 1))
2546 - sector;
2547
2548 while (sect_to_write) {
2549 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002550 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002551 if (sectors > sect_to_write)
2552 sectors = sect_to_write;
2553 /* Write at 'sector' for 'sectors' */
Ming Leid7a10302017-02-14 23:29:03 +08002554 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002555 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002556 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2557 wbio->bi_iter.bi_sector = wsector +
2558 choose_data_offset(r10_bio, rdev);
NeilBrownbd870a12011-07-28 11:39:24 +10002559 wbio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002560 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002561
2562 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002563 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002564 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002565 sectors, 0)
2566 && ok;
2567
2568 bio_put(wbio);
2569 sect_to_write -= sectors;
2570 sector += sectors;
2571 sectors = block_sectors;
2572 }
2573 return ok;
2574}
2575
NeilBrown9f2c9d12011-10-11 16:48:43 +11002576static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002577{
2578 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002579 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002580 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002581 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002582 char b[BDEVNAME_SIZE];
2583 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002584 int max_sectors;
NeilBrown109e3762016-11-18 13:22:04 +11002585 dev_t bio_dev;
2586 sector_t bio_last_sector;
NeilBrown560f8e52011-07-28 11:39:23 +10002587
2588 /* we got a read error. Maybe the drive is bad. Maybe just
2589 * the block and we can fix it.
2590 * We freeze all other IO, and try reading the block from
2591 * other devices. When we find one, we re-write
2592 * and check it that fixes the read error.
2593 * This is all done synchronously while the array is
2594 * frozen.
2595 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002596 bio = r10_bio->devs[slot].bio;
2597 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002598 bio_dev = bio->bi_bdev->bd_dev;
2599 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002600 bio_put(bio);
2601 r10_bio->devs[slot].bio = NULL;
2602
NeilBrown8d3ca832016-11-18 16:16:12 +11002603 if (mddev->ro)
2604 r10_bio->devs[slot].bio = IO_BLOCKED;
2605 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002606 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002607 fix_read_error(conf, mddev, r10_bio);
2608 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002609 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002610 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002611
NeilBrownabbf0982011-12-23 10:17:54 +11002612 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002613
NeilBrown7399c312011-07-28 11:39:23 +10002614read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002615 rdev = read_balance(conf, r10_bio, &max_sectors);
2616 if (rdev == NULL) {
NeilBrown08464e02016-11-02 14:16:50 +11002617 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2618 mdname(mddev), b,
2619 (unsigned long long)r10_bio->sector);
NeilBrown560f8e52011-07-28 11:39:23 +10002620 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002621 return;
2622 }
2623
Jens Axboe1eff9d32016-08-05 15:35:16 -06002624 do_sync = (r10_bio->master_bio->bi_opf & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002625 slot = r10_bio->read_slot;
NeilBrown08464e02016-11-02 14:16:50 +11002626 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2627 mdname(mddev),
2628 bdevname(rdev->bdev, b),
2629 (unsigned long long)r10_bio->sector);
Ming Leid7a10302017-02-14 23:29:03 +08002630 bio = bio_clone_fast(r10_bio->master_bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002631 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002632 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002633 r10_bio->devs[slot].rdev = rdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002634 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002635 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002636 bio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002637 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown8d3ca832016-11-18 16:16:12 +11002638 if (test_bit(FailFast, &rdev->flags) &&
2639 test_bit(R10BIO_FailFast, &r10_bio->state))
2640 bio->bi_opf |= MD_FAILFAST;
NeilBrown560f8e52011-07-28 11:39:23 +10002641 bio->bi_private = r10_bio;
2642 bio->bi_end_io = raid10_end_read_request;
NeilBrown109e3762016-11-18 13:22:04 +11002643 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2644 bio, bio_dev,
2645 bio_last_sector - r10_bio->sectors);
2646
NeilBrown7399c312011-07-28 11:39:23 +10002647 if (max_sectors < r10_bio->sectors) {
2648 /* Drat - have to split this up more */
2649 struct bio *mbio = r10_bio->master_bio;
2650 int sectors_handled =
2651 r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002652 - mbio->bi_iter.bi_sector;
NeilBrown7399c312011-07-28 11:39:23 +10002653 r10_bio->sectors = max_sectors;
NeilBrownfd16f2e2017-03-15 14:05:13 +11002654 bio_inc_remaining(mbio);
2655 inc_pending(conf);
NeilBrown7399c312011-07-28 11:39:23 +10002656 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002657
2658 r10_bio = mempool_alloc(conf->r10bio_pool,
2659 GFP_NOIO);
2660 r10_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002661 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown7399c312011-07-28 11:39:23 +10002662 r10_bio->state = 0;
2663 set_bit(R10BIO_ReadError,
2664 &r10_bio->state);
2665 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002666 r10_bio->sector = mbio->bi_iter.bi_sector
NeilBrown7399c312011-07-28 11:39:23 +10002667 + sectors_handled;
2668
2669 goto read_more;
2670 } else
2671 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002672}
2673
NeilBrowne879a872011-10-11 16:49:02 +11002674static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002675{
2676 /* Some sort of write request has finished and it
2677 * succeeded in writing where we thought there was a
2678 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002679 * Or possibly if failed and we need to record
2680 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002681 */
2682 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002683 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002684
2685 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2686 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002687 for (m = 0; m < conf->copies; m++) {
2688 int dev = r10_bio->devs[m].devnum;
2689 rdev = conf->mirrors[dev].rdev;
2690 if (r10_bio->devs[m].bio == NULL)
2691 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002692 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002693 rdev_clear_badblocks(
2694 rdev,
2695 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002696 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002697 } else {
2698 if (!rdev_set_badblocks(
2699 rdev,
2700 r10_bio->devs[m].addr,
2701 r10_bio->sectors, 0))
2702 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002703 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002704 rdev = conf->mirrors[dev].replacement;
2705 if (r10_bio->devs[m].repl_bio == NULL)
2706 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002707
2708 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002709 rdev_clear_badblocks(
2710 rdev,
2711 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002712 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002713 } else {
2714 if (!rdev_set_badblocks(
2715 rdev,
2716 r10_bio->devs[m].addr,
2717 r10_bio->sectors, 0))
2718 md_error(conf->mddev, rdev);
2719 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002720 }
NeilBrown749c55e2011-07-28 11:39:24 +10002721 put_buf(r10_bio);
2722 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002723 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002724 for (m = 0; m < conf->copies; m++) {
2725 int dev = r10_bio->devs[m].devnum;
2726 struct bio *bio = r10_bio->devs[m].bio;
2727 rdev = conf->mirrors[dev].rdev;
2728 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002729 rdev_clear_badblocks(
2730 rdev,
2731 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002732 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002733 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002734 } else if (bio != NULL && bio->bi_error) {
NeilBrown95af5872015-08-14 11:26:17 +10002735 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002736 if (!narrow_write_error(r10_bio, m)) {
2737 md_error(conf->mddev, rdev);
2738 set_bit(R10BIO_Degraded,
2739 &r10_bio->state);
2740 }
2741 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002742 }
NeilBrown475b0322011-12-23 10:17:55 +11002743 bio = r10_bio->devs[m].repl_bio;
2744 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002745 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002746 rdev_clear_badblocks(
2747 rdev,
2748 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002749 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002750 rdev_dec_pending(rdev, conf->mddev);
2751 }
NeilBrownbd870a12011-07-28 11:39:24 +10002752 }
NeilBrown95af5872015-08-14 11:26:17 +10002753 if (fail) {
2754 spin_lock_irq(&conf->device_lock);
2755 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002756 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002757 spin_unlock_irq(&conf->device_lock);
2758 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002759 } else {
2760 if (test_bit(R10BIO_WriteError,
2761 &r10_bio->state))
2762 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002763 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002764 }
NeilBrown749c55e2011-07-28 11:39:24 +10002765 }
2766}
2767
Shaohua Li4ed87312012-10-11 13:34:00 +11002768static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Shaohua Li4ed87312012-10-11 13:34:00 +11002770 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002771 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002773 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002775 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
NeilBrown95af5872015-08-14 11:26:17 +10002779 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002780 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002781 LIST_HEAD(tmp);
2782 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002783 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002784 while (!list_empty(&conf->bio_end_io_list)) {
2785 list_move(conf->bio_end_io_list.prev, &tmp);
2786 conf->nr_queued--;
2787 }
NeilBrown95af5872015-08-14 11:26:17 +10002788 }
2789 spin_unlock_irqrestore(&conf->device_lock, flags);
2790 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002791 r10_bio = list_first_entry(&tmp, struct r10bio,
2792 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002793 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002794 if (mddev->degraded)
2795 set_bit(R10BIO_Degraded, &r10_bio->state);
2796
2797 if (test_bit(R10BIO_WriteError,
2798 &r10_bio->state))
2799 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002800 raid_end_bio_io(r10_bio);
2801 }
2802 }
2803
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002804 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002806
NeilBrown0021b7b2012-07-31 09:08:14 +02002807 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002810 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002811 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002813 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002814 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002816 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 spin_unlock_irqrestore(&conf->device_lock, flags);
2818
2819 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002820 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002821 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2822 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002823 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002824 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2825 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002826 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002828 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002830 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002831 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002832 else {
2833 /* just a partial read to be scheduled from a
2834 * separate context
2835 */
2836 int slot = r10_bio->read_slot;
2837 generic_make_request(r10_bio->devs[slot].bio);
2838 }
NeilBrown4443ae12006-01-06 00:20:28 -08002839
NeilBrown1d9d5242009-10-16 15:55:32 +11002840 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002841 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002842 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002844 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
NeilBrowne879a872011-10-11 16:49:02 +11002847static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002853 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002854 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002855 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002856 if (conf->mirrors[i].replacement)
2857 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2859 if (!conf->r10buf_pool)
2860 return -ENOMEM;
2861 conf->next_resync = 0;
2862 return 0;
2863}
2864
2865/*
2866 * perform a "sync" on one "block"
2867 *
2868 * We need to make sure that no normal I/O request - particularly write
2869 * requests - conflict with active sync requests.
2870 *
2871 * This is achieved by tracking pending requests and a 'barrier' concept
2872 * that can be installed to exclude normal IO requests.
2873 *
2874 * Resync and recovery are handled very differently.
2875 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2876 *
2877 * For resync, we iterate over virtual addresses, read all copies,
2878 * and update if there are differences. If only one copy is live,
2879 * skip it.
2880 * For recovery, we iterate over physical addresses, read a good
2881 * value for each non-in_sync drive, and over-write.
2882 *
2883 * So, for recovery we may have several outstanding complex requests for a
2884 * given address, one for each out-of-sync device. We model this by allocating
2885 * a number of r10_bio structures, one for each out-of-sync device.
2886 * As we setup these structures, we collect all bio's together into a list
2887 * which we then process collectively to add pages, and then process again
2888 * to pass to generic_make_request.
2889 *
2890 * The r10_bio structures are linked using a borrowed master_bio pointer.
2891 * This link is counted in ->remaining. When the r10_bio that points to NULL
2892 * has its remaining count decremented to 0, the whole complex operation
2893 * is complete.
2894 *
2895 */
2896
Shaohua Li849674e2016-01-20 13:52:20 -08002897static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002898 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
NeilBrowne879a872011-10-11 16:49:02 +11002900 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002901 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct bio *biolist = NULL, *bio;
2903 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002905 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002906 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 sector_t sectors_skipped = 0;
2908 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002909 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 if (!conf->r10buf_pool)
2912 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002915 /*
2916 * Allow skipping a full rebuild for incremental assembly
2917 * of a clean array, like RAID1 does.
2918 */
2919 if (mddev->bitmap == NULL &&
2920 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002921 mddev->reshape_position == MaxSector &&
2922 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002923 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002924 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002925 conf->fullsync == 0) {
2926 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002927 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002928 }
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002931 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002932 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2933 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 max_sector = mddev->resync_max_sectors;
2935 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002936 /* If we aborted, we need to abort the
2937 * sync on the 'current' bitmap chucks (there can
2938 * be several when recovering multiple devices).
2939 * as we may have started syncing it but not finished.
2940 * We can find the current address in
2941 * mddev->curr_resync, but for recovery,
2942 * we need to convert that to several
2943 * virtual addresses.
2944 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002945 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2946 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002947 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002948 return 0;
2949 }
2950
NeilBrown6cce3b22006-01-06 00:20:16 -08002951 if (mddev->curr_resync < max_sector) { /* aborted */
2952 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2953 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2954 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002955 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002956 sector_t sect =
2957 raid10_find_virt(conf, mddev->curr_resync, i);
2958 bitmap_end_sync(mddev->bitmap, sect,
2959 &sync_blocks, 1);
2960 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002961 } else {
2962 /* completed sync */
2963 if ((!mddev->bitmap || conf->fullsync)
2964 && conf->have_replacement
2965 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2966 /* Completed a full sync so the replacements
2967 * are now fully recovered.
2968 */
NeilBrownf90145f2016-06-02 16:19:52 +10002969 rcu_read_lock();
2970 for (i = 0; i < conf->geo.raid_disks; i++) {
2971 struct md_rdev *rdev =
2972 rcu_dereference(conf->mirrors[i].replacement);
2973 if (rdev)
2974 rdev->recovery_offset = MaxSector;
2975 }
2976 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11002977 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002978 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002979 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002980 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002982 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return sectors_skipped;
2984 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002985
2986 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2987 return reshape_request(mddev, sector_nr, skipped);
2988
NeilBrown5cf00fc2012-05-21 09:28:20 +10002989 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 /* if there has been nothing to do on any drive,
2991 * then there is nothing to do at all..
2992 */
NeilBrown57afd892005-06-21 17:17:13 -07002993 *skipped = 1;
2994 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996
NeilBrownc6207272008-02-06 01:39:52 -08002997 if (max_sector > mddev->resync_max)
2998 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 /* make sure whole request will fit in a chunk - if chunks
3001 * are meaningful
3002 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003003 if (conf->geo.near_copies < conf->geo.raid_disks &&
3004 max_sector > (sector_nr | chunk_mask))
3005 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003007 /*
3008 * If there is non-resync activity waiting for a turn, then let it
3009 * though before starting on this new sync request.
3010 */
3011 if (conf->nr_waiting)
3012 schedule_timeout_uninterruptible(1);
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 /* Again, very different code for resync and recovery.
3015 * Both must result in an r10bio with a list of bios that
3016 * have bi_end_io, bi_sector, bi_bdev set,
3017 * and bi_private set to the r10bio.
3018 * For recovery, we may actually create several r10bios
3019 * with 2 bios in each, that correspond to the bios in the main one.
3020 * In this case, the subordinate r10bios link back through a
3021 * borrowed master_bio pointer, and the counter in the master
3022 * includes a ref from each subordinate.
3023 */
3024 /* First, we decide what to do and set ->bi_end_io
3025 * To end_sync_read if we want to read, and
3026 * end_sync_write if we will want to write.
3027 */
3028
NeilBrown6cce3b22006-01-06 00:20:16 -08003029 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3031 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003032 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 r10_bio = NULL;
3034
NeilBrown5cf00fc2012-05-21 09:28:20 +10003035 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003036 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003037 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003038 sector_t sect;
3039 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003040 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003041 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003042 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003043
NeilBrownf90145f2016-06-02 16:19:52 +10003044 rcu_read_lock();
3045 mrdev = rcu_dereference(mirror->rdev);
3046 mreplace = rcu_dereference(mirror->replacement);
3047
3048 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10003049 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10003050 test_bit(In_sync, &mrdev->flags)) &&
3051 (mreplace == NULL ||
3052 test_bit(Faulty, &mreplace->flags))) {
3053 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003054 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003055 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003056
3057 still_degraded = 0;
3058 /* want to reconstruct this device */
3059 rb2 = r10_bio;
3060 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003061 if (sect >= mddev->resync_max_sectors) {
3062 /* last stripe is not complete - don't
3063 * try to recover this sector.
3064 */
NeilBrownf90145f2016-06-02 16:19:52 +10003065 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003066 continue;
3067 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003068 if (mreplace && test_bit(Faulty, &mreplace->flags))
3069 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003070 /* Unless we are doing a full sync, or a replacement
3071 * we only need to recover the block if it is set in
3072 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003073 */
3074 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3075 &sync_blocks, 1);
3076 if (sync_blocks < max_sync)
3077 max_sync = sync_blocks;
3078 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003079 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003080 !conf->fullsync) {
3081 /* yep, skip the sync_blocks here, but don't assume
3082 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003083 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003084 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003085 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003086 continue;
3087 }
NeilBrownf90145f2016-06-02 16:19:52 +10003088 atomic_inc(&mrdev->nr_pending);
3089 if (mreplace)
3090 atomic_inc(&mreplace->nr_pending);
3091 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
NeilBrownab9d47e2011-05-11 14:54:41 +10003093 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003094 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003095 raise_barrier(conf, rb2 != NULL);
3096 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
NeilBrownab9d47e2011-05-11 14:54:41 +10003098 r10_bio->master_bio = (struct bio*)rb2;
3099 if (rb2)
3100 atomic_inc(&rb2->remaining);
3101 r10_bio->mddev = mddev;
3102 set_bit(R10BIO_IsRecover, &r10_bio->state);
3103 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003104
NeilBrownab9d47e2011-05-11 14:54:41 +10003105 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003106
NeilBrownab9d47e2011-05-11 14:54:41 +10003107 /* Need to check if the array will still be
3108 * degraded
3109 */
NeilBrownf90145f2016-06-02 16:19:52 +10003110 rcu_read_lock();
3111 for (j = 0; j < conf->geo.raid_disks; j++) {
3112 struct md_rdev *rdev = rcu_dereference(
3113 conf->mirrors[j].rdev);
3114 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003115 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
NeilBrownf90145f2016-06-02 16:19:52 +10003118 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003119
3120 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3121 &sync_blocks, still_degraded);
3122
NeilBrowne875ece2011-07-28 11:39:24 +10003123 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003124 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003125 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003126 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003127 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003128 struct md_rdev *rdev =
3129 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003130 sector_t sector, first_bad;
3131 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003132 if (!rdev ||
3133 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003134 continue;
3135 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003136 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003137 sector = r10_bio->devs[j].addr;
3138
3139 if (is_badblock(rdev, sector, max_sync,
3140 &first_bad, &bad_sectors)) {
3141 if (first_bad > sector)
3142 max_sync = first_bad - sector;
3143 else {
3144 bad_sectors -= (sector
3145 - first_bad);
3146 if (max_sync > bad_sectors)
3147 max_sync = bad_sectors;
3148 continue;
3149 }
3150 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003151 bio = r10_bio->devs[0].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003152 bio_reset(bio);
NeilBrownab9d47e2011-05-11 14:54:41 +10003153 bio->bi_next = biolist;
3154 biolist = bio;
3155 bio->bi_private = r10_bio;
3156 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003157 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003158 if (test_bit(FailFast, &rdev->flags))
3159 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003160 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003161 bio->bi_iter.bi_sector = from_addr +
3162 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003163 bio->bi_bdev = rdev->bdev;
3164 atomic_inc(&rdev->nr_pending);
3165 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003166
3167 for (k=0; k<conf->copies; k++)
3168 if (r10_bio->devs[k].devnum == i)
3169 break;
3170 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003171 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003172 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003173 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003174 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003175 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003176
NeilBrownf90145f2016-06-02 16:19:52 +10003177 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003178 bio = r10_bio->devs[1].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003179 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003180 bio->bi_next = biolist;
3181 biolist = bio;
3182 bio->bi_private = r10_bio;
3183 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003184 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003185 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003186 + mrdev->data_offset;
3187 bio->bi_bdev = mrdev->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003188 atomic_inc(&r10_bio->remaining);
3189 } else
3190 r10_bio->devs[1].bio->bi_end_io = NULL;
3191
3192 /* and maybe write to replacement */
3193 bio = r10_bio->devs[1].repl_bio;
3194 if (bio)
3195 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003196 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003197 * cannot be NULL as r10buf_pool_alloc will
3198 * have allocated it.
3199 * So the second test here is pointless.
3200 * But it keeps semantic-checkers happy, and
3201 * this comment keeps human reviewers
3202 * happy.
3203 */
NeilBrownf90145f2016-06-02 16:19:52 +10003204 if (mreplace == NULL || bio == NULL ||
3205 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003206 break;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003207 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003208 bio->bi_next = biolist;
3209 biolist = bio;
3210 bio->bi_private = r10_bio;
3211 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003212 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003213 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003214 mreplace->data_offset;
3215 bio->bi_bdev = mreplace->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003216 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
NeilBrownf90145f2016-06-02 16:19:52 +10003219 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003220 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003221 /* Cannot recover, so abort the recovery or
3222 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003223 if (any_working) {
3224 /* problem is that there are bad blocks
3225 * on other device(s)
3226 */
3227 int k;
3228 for (k = 0; k < conf->copies; k++)
3229 if (r10_bio->devs[k].devnum == i)
3230 break;
NeilBrown24afd802011-12-23 10:17:55 +11003231 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003232 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003233 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003234 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003235 r10_bio->devs[k].addr,
3236 max_sync, 0))
3237 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003238 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003239 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003240 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003241 r10_bio->devs[k].addr,
3242 max_sync, 0))
3243 any_working = 0;
3244 }
3245 if (!any_working) {
3246 if (!test_and_set_bit(MD_RECOVERY_INTR,
3247 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003248 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003249 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003250 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003251 = mddev->recovery_disabled;
3252 }
NeilBrowne8b84912014-01-06 10:35:34 +11003253 put_buf(r10_bio);
3254 if (rb2)
3255 atomic_dec(&rb2->remaining);
3256 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003257 rdev_dec_pending(mrdev, mddev);
3258 if (mreplace)
3259 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003260 break;
3261 }
NeilBrownf90145f2016-06-02 16:19:52 +10003262 rdev_dec_pending(mrdev, mddev);
3263 if (mreplace)
3264 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003265 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3266 /* Only want this if there is elsewhere to
3267 * read from. 'j' is currently the first
3268 * readable copy.
3269 */
3270 int targets = 1;
3271 for (; j < conf->copies; j++) {
3272 int d = r10_bio->devs[j].devnum;
3273 if (conf->mirrors[d].rdev &&
3274 test_bit(In_sync,
3275 &conf->mirrors[d].rdev->flags))
3276 targets++;
3277 }
3278 if (targets == 1)
3279 r10_bio->devs[0].bio->bi_opf
3280 &= ~MD_FAILFAST;
3281 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 if (biolist == NULL) {
3284 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003285 struct r10bio *rb2 = r10_bio;
3286 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 rb2->master_bio = NULL;
3288 put_buf(rb2);
3289 }
3290 goto giveup;
3291 }
3292 } else {
3293 /* resync. Schedule a read for every block at this virt offset */
3294 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003295
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003296 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003297
NeilBrown6cce3b22006-01-06 00:20:16 -08003298 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3299 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003300 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3301 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003302 /* We can skip this block */
3303 *skipped = 1;
3304 return sync_blocks + sectors_skipped;
3305 }
3306 if (sync_blocks < max_sync)
3307 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003309 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 r10_bio->mddev = mddev;
3312 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003313 raise_barrier(conf, 0);
3314 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 r10_bio->master_bio = NULL;
3317 r10_bio->sector = sector_nr;
3318 set_bit(R10BIO_IsSync, &r10_bio->state);
3319 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003320 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
NeilBrown5cf00fc2012-05-21 09:28:20 +10003322 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003324 sector_t first_bad, sector;
3325 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003326 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003327
NeilBrown9ad1aef2011-12-23 10:17:55 +11003328 if (r10_bio->devs[i].repl_bio)
3329 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 bio = r10_bio->devs[i].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003332 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003333 bio->bi_error = -EIO;
NeilBrownf90145f2016-06-02 16:19:52 +10003334 rcu_read_lock();
3335 rdev = rcu_dereference(conf->mirrors[d].rdev);
3336 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3337 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003339 }
NeilBrown40c356c2011-07-28 11:39:24 +10003340 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003341 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003342 &first_bad, &bad_sectors)) {
3343 if (first_bad > sector)
3344 max_sync = first_bad - sector;
3345 else {
3346 bad_sectors -= (sector - first_bad);
3347 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003348 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003349 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003350 continue;
3351 }
3352 }
NeilBrownf90145f2016-06-02 16:19:52 +10003353 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 atomic_inc(&r10_bio->remaining);
3355 bio->bi_next = biolist;
3356 biolist = bio;
3357 bio->bi_private = r10_bio;
3358 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003359 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003360 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3361 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003362 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3363 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003365
NeilBrownf90145f2016-06-02 16:19:52 +10003366 rdev = rcu_dereference(conf->mirrors[d].replacement);
3367 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3368 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003369 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003370 }
3371 atomic_inc(&rdev->nr_pending);
3372 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003373
3374 /* Need to set up for writing to the replacement */
3375 bio = r10_bio->devs[i].repl_bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003376 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003377 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003378
3379 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003380 bio->bi_next = biolist;
3381 biolist = bio;
3382 bio->bi_private = r10_bio;
3383 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003384 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
NeilBrown1919cbb2016-11-18 16:16:12 +11003385 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3386 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003387 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3388 bio->bi_bdev = rdev->bdev;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003389 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391
3392 if (count < 2) {
3393 for (i=0; i<conf->copies; i++) {
3394 int d = r10_bio->devs[i].devnum;
3395 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003396 rdev_dec_pending(conf->mirrors[d].rdev,
3397 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003398 if (r10_bio->devs[i].repl_bio &&
3399 r10_bio->devs[i].repl_bio->bi_end_io)
3400 rdev_dec_pending(
3401 conf->mirrors[d].replacement,
3402 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
3404 put_buf(r10_bio);
3405 biolist = NULL;
3406 goto giveup;
3407 }
3408 }
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003411 if (sector_nr + max_sync < max_sector)
3412 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 do {
3414 struct page *page;
3415 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (sector_nr + (len>>9) > max_sector)
3417 len = (max_sector - sector_nr) << 9;
3418 if (len == 0)
3419 break;
3420 for (bio= biolist ; bio ; bio=bio->bi_next) {
3421 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Ming Leic85ba142017-03-17 00:12:22 +08003422 /*
3423 * won't fail because the vec table is big enough
3424 * to hold all these pages
3425 */
3426 bio_add_page(bio, page, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 }
3428 nr_sectors += len>>9;
3429 sector_nr += len>>9;
3430 } while (biolist->bi_vcnt < RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 r10_bio->sectors = nr_sectors;
3432
3433 while (biolist) {
3434 bio = biolist;
3435 biolist = biolist->bi_next;
3436
3437 bio->bi_next = NULL;
3438 r10_bio = bio->bi_private;
3439 r10_bio->sectors = nr_sectors;
3440
3441 if (bio->bi_end_io == end_sync_read) {
3442 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003443 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 generic_make_request(bio);
3445 }
3446 }
3447
NeilBrown57afd892005-06-21 17:17:13 -07003448 if (sectors_skipped)
3449 /* pretend they weren't skipped, it makes
3450 * no important difference in this case
3451 */
3452 md_done_sync(mddev, sectors_skipped, 1);
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return sectors_skipped + nr_sectors;
3455 giveup:
3456 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003457 * drives must be failed or in resync, all drives
3458 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 */
NeilBrown09b40682009-02-25 13:18:47 +11003460 if (sector_nr + max_sync < max_sector)
3461 max_sector = sector_nr + max_sync;
3462
3463 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 chunks_skipped ++;
3465 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
Dan Williams80c3a6c2009-03-17 18:10:40 -07003469static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003470raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003471{
3472 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003473 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003474
3475 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003476 raid_disks = min(conf->geo.raid_disks,
3477 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003478 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003479 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003480
NeilBrown5cf00fc2012-05-21 09:28:20 +10003481 size = sectors >> conf->geo.chunk_shift;
3482 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003483 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003484 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003485
NeilBrown5cf00fc2012-05-21 09:28:20 +10003486 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003487}
3488
NeilBrown6508fdb2012-05-17 10:08:45 +10003489static void calc_sectors(struct r10conf *conf, sector_t size)
3490{
3491 /* Calculate the number of sectors-per-device that will
3492 * actually be used, and set conf->dev_sectors and
3493 * conf->stride
3494 */
3495
NeilBrown5cf00fc2012-05-21 09:28:20 +10003496 size = size >> conf->geo.chunk_shift;
3497 sector_div(size, conf->geo.far_copies);
3498 size = size * conf->geo.raid_disks;
3499 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003500 /* 'size' is now the number of chunks in the array */
3501 /* calculate "used chunks per device" */
3502 size = size * conf->copies;
3503
3504 /* We need to round up when dividing by raid_disks to
3505 * get the stride size.
3506 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003507 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003508
NeilBrown5cf00fc2012-05-21 09:28:20 +10003509 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003510
NeilBrown5cf00fc2012-05-21 09:28:20 +10003511 if (conf->geo.far_offset)
3512 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003513 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003514 sector_div(size, conf->geo.far_copies);
3515 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003516 }
3517}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003518
NeilBrowndeb200d2012-05-21 09:28:33 +10003519enum geo_type {geo_new, geo_old, geo_start};
3520static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3521{
3522 int nc, fc, fo;
3523 int layout, chunk, disks;
3524 switch (new) {
3525 case geo_old:
3526 layout = mddev->layout;
3527 chunk = mddev->chunk_sectors;
3528 disks = mddev->raid_disks - mddev->delta_disks;
3529 break;
3530 case geo_new:
3531 layout = mddev->new_layout;
3532 chunk = mddev->new_chunk_sectors;
3533 disks = mddev->raid_disks;
3534 break;
3535 default: /* avoid 'may be unused' warnings */
3536 case geo_start: /* new when starting reshape - raid_disks not
3537 * updated yet. */
3538 layout = mddev->new_layout;
3539 chunk = mddev->new_chunk_sectors;
3540 disks = mddev->raid_disks + mddev->delta_disks;
3541 break;
3542 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003543 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003544 return -1;
3545 if (chunk < (PAGE_SIZE >> 9) ||
3546 !is_power_of_2(chunk))
3547 return -2;
3548 nc = layout & 255;
3549 fc = (layout >> 8) & 255;
3550 fo = layout & (1<<16);
3551 geo->raid_disks = disks;
3552 geo->near_copies = nc;
3553 geo->far_copies = fc;
3554 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003555 switch (layout >> 17) {
3556 case 0: /* original layout. simple but not always optimal */
3557 geo->far_set_size = disks;
3558 break;
3559 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3560 * actually using this, but leave code here just in case.*/
3561 geo->far_set_size = disks/fc;
3562 WARN(geo->far_set_size < fc,
3563 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3564 break;
3565 case 2: /* "improved" layout fixed to match documentation */
3566 geo->far_set_size = fc * nc;
3567 break;
3568 default: /* Not a valid layout */
3569 return -1;
3570 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003571 geo->chunk_mask = chunk - 1;
3572 geo->chunk_shift = ffz(~chunk);
3573 return nc*fc;
3574}
3575
NeilBrowne879a872011-10-11 16:49:02 +11003576static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
NeilBrowne879a872011-10-11 16:49:02 +11003578 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003579 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003580 struct geom geo;
3581 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
NeilBrowndeb200d2012-05-21 09:28:33 +10003583 copies = setup_geo(&geo, mddev, geo_new);
3584
3585 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003586 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3587 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003588 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
NeilBrown2604b702006-01-06 00:20:36 -08003590
NeilBrowndeb200d2012-05-21 09:28:33 +10003591 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003592 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3593 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 goto out;
3595 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003596
3597 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003598 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003599 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003601
NeilBrown3ea7daa2012-05-22 13:53:47 +10003602 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003603 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003604 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003605 GFP_KERNEL);
3606 if (!conf->mirrors)
3607 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003608
3609 conf->tmppage = alloc_page(GFP_KERNEL);
3610 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003611 goto out;
3612
NeilBrowndeb200d2012-05-21 09:28:33 +10003613 conf->geo = geo;
3614 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003615 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3616 r10bio_pool_free, conf);
3617 if (!conf->r10bio_pool)
3618 goto out;
3619
NeilBrown6508fdb2012-05-17 10:08:45 +10003620 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003621 if (mddev->reshape_position == MaxSector) {
3622 conf->prev = conf->geo;
3623 conf->reshape_progress = MaxSector;
3624 } else {
3625 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3626 err = -EINVAL;
3627 goto out;
3628 }
3629 conf->reshape_progress = mddev->reshape_position;
3630 if (conf->prev.far_offset)
3631 conf->prev.stride = 1 << conf->prev.chunk_shift;
3632 else
3633 /* far_copies must be 1 */
3634 conf->prev.stride = conf->dev_sectors;
3635 }
NeilBrown299b0682015-07-06 17:37:49 +10003636 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003637 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003638 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003639 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003640
3641 spin_lock_init(&conf->resync_lock);
3642 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003643 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003644
NeilBrown02326052012-07-03 15:56:52 +10003645 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003646 if (!conf->thread)
3647 goto out;
3648
Trela, Maciejdab8b292010-03-08 16:02:45 +11003649 conf->mddev = mddev;
3650 return conf;
3651
3652 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003653 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003654 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003655 kfree(conf->mirrors);
3656 safe_put_page(conf->tmppage);
3657 kfree(conf);
3658 }
3659 return ERR_PTR(err);
3660}
3661
Shaohua Li849674e2016-01-20 13:52:20 -08003662static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003663{
NeilBrowne879a872011-10-11 16:49:02 +11003664 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003665 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003666 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003667 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003668 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003669 sector_t min_offset_diff = 0;
3670 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003671 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003672
3673 if (mddev->private == NULL) {
3674 conf = setup_conf(mddev);
3675 if (IS_ERR(conf))
3676 return PTR_ERR(conf);
3677 mddev->private = conf;
3678 }
3679 conf = mddev->private;
3680 if (!conf)
3681 goto out;
3682
Trela, Maciejdab8b292010-03-08 16:02:45 +11003683 mddev->thread = conf->thread;
3684 conf->thread = NULL;
3685
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003686 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003687 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003688 blk_queue_max_discard_sectors(mddev->queue,
3689 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003690 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003691 blk_queue_io_min(mddev->queue, chunk_size);
3692 if (conf->geo.raid_disks % conf->geo.near_copies)
3693 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3694 else
3695 blk_queue_io_opt(mddev->queue, chunk_size *
3696 (conf->geo.raid_disks / conf->geo.near_copies));
3697 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003698
NeilBrowndafb20f2012-03-19 12:46:39 +11003699 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003700 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003701 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003704 if (disk_idx < 0)
3705 continue;
3706 if (disk_idx >= conf->geo.raid_disks &&
3707 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 continue;
3709 disk = conf->mirrors + disk_idx;
3710
NeilBrown56a2559b2011-12-23 10:17:55 +11003711 if (test_bit(Replacement, &rdev->flags)) {
3712 if (disk->replacement)
3713 goto out_free_conf;
3714 disk->replacement = rdev;
3715 } else {
3716 if (disk->rdev)
3717 goto out_free_conf;
3718 disk->rdev = rdev;
3719 }
NeilBrownaba336b2012-05-31 15:39:11 +10003720 q = bdev_get_queue(rdev->bdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003721 diff = (rdev->new_data_offset - rdev->data_offset);
3722 if (!mddev->reshape_backwards)
3723 diff = -diff;
3724 if (diff < 0)
3725 diff = 0;
3726 if (first || diff < min_offset_diff)
3727 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11003728
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003729 if (mddev->gendisk)
3730 disk_stack_limits(mddev->gendisk, rdev->bdev,
3731 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003734
3735 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3736 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003738
Jonathan Brassowed30be02012-10-31 11:42:30 +11003739 if (mddev->queue) {
3740 if (discard_supported)
3741 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3742 mddev->queue);
3743 else
3744 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3745 mddev->queue);
3746 }
NeilBrown6d508242005-09-09 16:24:03 -07003747 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003748 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003749 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003750 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 goto out_free_conf;
3752 }
3753
NeilBrown3ea7daa2012-05-22 13:53:47 +10003754 if (conf->reshape_progress != MaxSector) {
3755 /* must ensure that shape change is supported */
3756 if (conf->geo.far_copies != 1 &&
3757 conf->geo.far_offset == 0)
3758 goto out_free_conf;
3759 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003760 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003761 goto out_free_conf;
3762 }
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003765 for (i = 0;
3766 i < conf->geo.raid_disks
3767 || i < conf->prev.raid_disks;
3768 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 disk = conf->mirrors + i;
3771
NeilBrown56a2559b2011-12-23 10:17:55 +11003772 if (!disk->rdev && disk->replacement) {
3773 /* The replacement is all we have - use it */
3774 disk->rdev = disk->replacement;
3775 disk->replacement = NULL;
3776 clear_bit(Replacement, &disk->rdev->flags);
3777 }
3778
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003779 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003780 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 disk->head_position = 0;
3782 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003783 if (disk->rdev &&
3784 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003785 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
NeilBrownd890fa22011-10-26 11:54:39 +11003787 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789
Andre Noll8c6ac8682009-06-18 08:48:06 +10003790 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003791 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3792 mdname(mddev));
3793 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003794 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3795 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 /*
3797 * Ok, everything is just fine now
3798 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003799 mddev->dev_sectors = conf->dev_sectors;
3800 size = raid10_size(mddev, 0, 0);
3801 md_set_array_sectors(mddev, size);
3802 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003803 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003805 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003806 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003807 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003808
3809 /* Calculate max read-ahead size.
3810 * We need to readahead at least twice a whole stripe....
3811 * maybe...
3812 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003813 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01003814 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3815 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 }
3817
Martin K. Petersena91a2782011-03-17 11:11:05 +01003818 if (md_integrity_register(mddev))
3819 goto out_free_conf;
3820
NeilBrown3ea7daa2012-05-22 13:53:47 +10003821 if (conf->reshape_progress != MaxSector) {
3822 unsigned long before_length, after_length;
3823
3824 before_length = ((1 << conf->prev.chunk_shift) *
3825 conf->prev.far_copies);
3826 after_length = ((1 << conf->geo.chunk_shift) *
3827 conf->geo.far_copies);
3828
3829 if (max(before_length, after_length) > min_offset_diff) {
3830 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003831 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003832 goto out_free_conf;
3833 }
3834 conf->offset_diff = min_offset_diff;
3835
NeilBrown3ea7daa2012-05-22 13:53:47 +10003836 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3837 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3838 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3839 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3840 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3841 "reshape");
3842 }
3843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 return 0;
3845
3846out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003847 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003848 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003849 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003850 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 kfree(conf);
3852 mddev->private = NULL;
3853out:
3854 return -EIO;
3855}
3856
NeilBrownafa0f552014-12-15 12:56:58 +11003857static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
NeilBrownafa0f552014-12-15 12:56:58 +11003859 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Julia Lawall644df1a2015-09-13 14:15:10 +02003861 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003862 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003863 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003864 kfree(conf->mirrors_old);
3865 kfree(conf->mirrors_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
NeilBrownfd01b882011-10-11 16:47:53 +11003869static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003870{
NeilBrowne879a872011-10-11 16:49:02 +11003871 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003872
3873 switch(state) {
3874 case 1:
3875 raise_barrier(conf, 0);
3876 break;
3877 case 0:
3878 lower_barrier(conf);
3879 break;
3880 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
NeilBrown006a09a2012-03-19 12:46:40 +11003883static int raid10_resize(struct mddev *mddev, sector_t sectors)
3884{
3885 /* Resize of 'far' arrays is not supported.
3886 * For 'near' and 'offset' arrays we can set the
3887 * number of sectors used to be an appropriate multiple
3888 * of the chunk size.
3889 * For 'offset', this is far_copies*chunksize.
3890 * For 'near' the multiplier is the LCM of
3891 * near_copies and raid_disks.
3892 * So if far_copies > 1 && !far_offset, fail.
3893 * Else find LCM(raid_disks, near_copy)*far_copies and
3894 * multiply by chunk_size. Then round to this number.
3895 * This is mostly done by raid10_size()
3896 */
3897 struct r10conf *conf = mddev->private;
3898 sector_t oldsize, size;
3899
NeilBrownf8c9e742012-05-21 09:28:33 +10003900 if (mddev->reshape_position != MaxSector)
3901 return -EBUSY;
3902
NeilBrown5cf00fc2012-05-21 09:28:20 +10003903 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003904 return -EINVAL;
3905
3906 oldsize = raid10_size(mddev, 0, 0);
3907 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003908 if (mddev->external_size &&
3909 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003910 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003911 if (mddev->bitmap) {
3912 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3913 if (ret)
3914 return ret;
3915 }
3916 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003917 if (sectors > mddev->dev_sectors &&
3918 mddev->recovery_cp > oldsize) {
3919 mddev->recovery_cp = oldsize;
3920 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3921 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003922 calc_sectors(conf, sectors);
3923 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003924 mddev->resync_max_sectors = size;
3925 return 0;
3926}
3927
NeilBrown53a6ab42015-02-12 14:09:57 +11003928static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003929{
NeilBrown3cb03002011-10-11 16:45:26 +11003930 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003931 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003932
3933 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003934 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3935 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003936 return ERR_PTR(-EINVAL);
3937 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003938 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003939
Trela, Maciejdab8b292010-03-08 16:02:45 +11003940 /* Set new parameters */
3941 mddev->new_level = 10;
3942 /* new layout: far_copies = 1, near_copies = 2 */
3943 mddev->new_layout = (1<<8) + 2;
3944 mddev->new_chunk_sectors = mddev->chunk_sectors;
3945 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003946 mddev->raid_disks *= 2;
3947 /* make sure it will be not marked as dirty */
3948 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003949 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003950
3951 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003952 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003953 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003954 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003955 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003956 rdev->sectors = size;
3957 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003958 conf->barrier = 1;
3959 }
3960
Trela, Maciejdab8b292010-03-08 16:02:45 +11003961 return conf;
3962}
3963
NeilBrownfd01b882011-10-11 16:47:53 +11003964static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003965{
NeilBrowne373ab12011-10-11 16:48:59 +11003966 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003967
3968 /* raid10 can take over:
3969 * raid0 - providing it has only two drives
3970 */
3971 if (mddev->level == 0) {
3972 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003973 raid0_conf = mddev->private;
3974 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11003975 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3976 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003977 return ERR_PTR(-EINVAL);
3978 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003979 return raid10_takeover_raid0(mddev,
3980 raid0_conf->strip_zone->zone_end,
3981 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003982 }
3983 return ERR_PTR(-EINVAL);
3984}
3985
NeilBrown3ea7daa2012-05-22 13:53:47 +10003986static int raid10_check_reshape(struct mddev *mddev)
3987{
3988 /* Called when there is a request to change
3989 * - layout (to ->new_layout)
3990 * - chunk size (to ->new_chunk_sectors)
3991 * - raid_disks (by delta_disks)
3992 * or when trying to restart a reshape that was ongoing.
3993 *
3994 * We need to validate the request and possibly allocate
3995 * space if that might be an issue later.
3996 *
3997 * Currently we reject any reshape of a 'far' mode array,
3998 * allow chunk size to change if new is generally acceptable,
3999 * allow raid_disks to increase, and allow
4000 * a switch between 'near' mode and 'offset' mode.
4001 */
4002 struct r10conf *conf = mddev->private;
4003 struct geom geo;
4004
4005 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4006 return -EINVAL;
4007
4008 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4009 /* mustn't change number of copies */
4010 return -EINVAL;
4011 if (geo.far_copies > 1 && !geo.far_offset)
4012 /* Cannot switch to 'far' mode */
4013 return -EINVAL;
4014
4015 if (mddev->array_sectors & geo.chunk_mask)
4016 /* not factor of array size */
4017 return -EINVAL;
4018
NeilBrown3ea7daa2012-05-22 13:53:47 +10004019 if (!enough(conf, -1))
4020 return -EINVAL;
4021
4022 kfree(conf->mirrors_new);
4023 conf->mirrors_new = NULL;
4024 if (mddev->delta_disks > 0) {
4025 /* allocate new 'mirrors' list */
4026 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004027 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004028 *(mddev->raid_disks +
4029 mddev->delta_disks),
4030 GFP_KERNEL);
4031 if (!conf->mirrors_new)
4032 return -ENOMEM;
4033 }
4034 return 0;
4035}
4036
4037/*
4038 * Need to check if array has failed when deciding whether to:
4039 * - start an array
4040 * - remove non-faulty devices
4041 * - add a spare
4042 * - allow a reshape
4043 * This determination is simple when no reshape is happening.
4044 * However if there is a reshape, we need to carefully check
4045 * both the before and after sections.
4046 * This is because some failed devices may only affect one
4047 * of the two sections, and some non-in_sync devices may
4048 * be insync in the section most affected by failed devices.
4049 */
4050static int calc_degraded(struct r10conf *conf)
4051{
4052 int degraded, degraded2;
4053 int i;
4054
4055 rcu_read_lock();
4056 degraded = 0;
4057 /* 'prev' section first */
4058 for (i = 0; i < conf->prev.raid_disks; i++) {
4059 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4060 if (!rdev || test_bit(Faulty, &rdev->flags))
4061 degraded++;
4062 else if (!test_bit(In_sync, &rdev->flags))
4063 /* When we can reduce the number of devices in
4064 * an array, this might not contribute to
4065 * 'degraded'. It does now.
4066 */
4067 degraded++;
4068 }
4069 rcu_read_unlock();
4070 if (conf->geo.raid_disks == conf->prev.raid_disks)
4071 return degraded;
4072 rcu_read_lock();
4073 degraded2 = 0;
4074 for (i = 0; i < conf->geo.raid_disks; i++) {
4075 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4076 if (!rdev || test_bit(Faulty, &rdev->flags))
4077 degraded2++;
4078 else if (!test_bit(In_sync, &rdev->flags)) {
4079 /* If reshape is increasing the number of devices,
4080 * this section has already been recovered, so
4081 * it doesn't contribute to degraded.
4082 * else it does.
4083 */
4084 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4085 degraded2++;
4086 }
4087 }
4088 rcu_read_unlock();
4089 if (degraded2 > degraded)
4090 return degraded2;
4091 return degraded;
4092}
4093
4094static int raid10_start_reshape(struct mddev *mddev)
4095{
4096 /* A 'reshape' has been requested. This commits
4097 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4098 * This also checks if there are enough spares and adds them
4099 * to the array.
4100 * We currently require enough spares to make the final
4101 * array non-degraded. We also require that the difference
4102 * between old and new data_offset - on each device - is
4103 * enough that we never risk over-writing.
4104 */
4105
4106 unsigned long before_length, after_length;
4107 sector_t min_offset_diff = 0;
4108 int first = 1;
4109 struct geom new;
4110 struct r10conf *conf = mddev->private;
4111 struct md_rdev *rdev;
4112 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004113 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004114
4115 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4116 return -EBUSY;
4117
4118 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4119 return -EINVAL;
4120
4121 before_length = ((1 << conf->prev.chunk_shift) *
4122 conf->prev.far_copies);
4123 after_length = ((1 << conf->geo.chunk_shift) *
4124 conf->geo.far_copies);
4125
4126 rdev_for_each(rdev, mddev) {
4127 if (!test_bit(In_sync, &rdev->flags)
4128 && !test_bit(Faulty, &rdev->flags))
4129 spares++;
4130 if (rdev->raid_disk >= 0) {
4131 long long diff = (rdev->new_data_offset
4132 - rdev->data_offset);
4133 if (!mddev->reshape_backwards)
4134 diff = -diff;
4135 if (diff < 0)
4136 diff = 0;
4137 if (first || diff < min_offset_diff)
4138 min_offset_diff = diff;
4139 }
4140 }
4141
4142 if (max(before_length, after_length) > min_offset_diff)
4143 return -EINVAL;
4144
4145 if (spares < mddev->delta_disks)
4146 return -EINVAL;
4147
4148 conf->offset_diff = min_offset_diff;
4149 spin_lock_irq(&conf->device_lock);
4150 if (conf->mirrors_new) {
4151 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004152 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004153 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004154 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004155 conf->mirrors_old = conf->mirrors;
4156 conf->mirrors = conf->mirrors_new;
4157 conf->mirrors_new = NULL;
4158 }
4159 setup_geo(&conf->geo, mddev, geo_start);
4160 smp_mb();
4161 if (mddev->reshape_backwards) {
4162 sector_t size = raid10_size(mddev, 0, 0);
4163 if (size < mddev->array_sectors) {
4164 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004165 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4166 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004167 return -EINVAL;
4168 }
4169 mddev->resync_max_sectors = size;
4170 conf->reshape_progress = size;
4171 } else
4172 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004173 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004174 spin_unlock_irq(&conf->device_lock);
4175
NeilBrownbb63a702012-05-22 13:55:28 +10004176 if (mddev->delta_disks && mddev->bitmap) {
4177 ret = bitmap_resize(mddev->bitmap,
4178 raid10_size(mddev, 0,
4179 conf->geo.raid_disks),
4180 0, 0);
4181 if (ret)
4182 goto abort;
4183 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004184 if (mddev->delta_disks > 0) {
4185 rdev_for_each(rdev, mddev)
4186 if (rdev->raid_disk < 0 &&
4187 !test_bit(Faulty, &rdev->flags)) {
4188 if (raid10_add_disk(mddev, rdev) == 0) {
4189 if (rdev->raid_disk >=
4190 conf->prev.raid_disks)
4191 set_bit(In_sync, &rdev->flags);
4192 else
4193 rdev->recovery_offset = 0;
4194
4195 if (sysfs_link_rdev(mddev, rdev))
4196 /* Failure here is OK */;
4197 }
4198 } else if (rdev->raid_disk >= conf->prev.raid_disks
4199 && !test_bit(Faulty, &rdev->flags)) {
4200 /* This is a spare that was manually added */
4201 set_bit(In_sync, &rdev->flags);
4202 }
4203 }
4204 /* When a reshape changes the number of devices,
4205 * ->degraded is measured against the larger of the
4206 * pre and post numbers.
4207 */
4208 spin_lock_irq(&conf->device_lock);
4209 mddev->degraded = calc_degraded(conf);
4210 spin_unlock_irq(&conf->device_lock);
4211 mddev->raid_disks = conf->geo.raid_disks;
4212 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004213 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004214
4215 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4216 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004217 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004218 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4219 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4220
4221 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4222 "reshape");
4223 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004224 ret = -EAGAIN;
4225 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004226 }
4227 conf->reshape_checkpoint = jiffies;
4228 md_wakeup_thread(mddev->sync_thread);
4229 md_new_event(mddev);
4230 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004231
4232abort:
4233 mddev->recovery = 0;
4234 spin_lock_irq(&conf->device_lock);
4235 conf->geo = conf->prev;
4236 mddev->raid_disks = conf->geo.raid_disks;
4237 rdev_for_each(rdev, mddev)
4238 rdev->new_data_offset = rdev->data_offset;
4239 smp_wmb();
4240 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004241 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004242 mddev->reshape_position = MaxSector;
4243 spin_unlock_irq(&conf->device_lock);
4244 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004245}
4246
4247/* Calculate the last device-address that could contain
4248 * any block from the chunk that includes the array-address 's'
4249 * and report the next address.
4250 * i.e. the address returned will be chunk-aligned and after
4251 * any data that is in the chunk containing 's'.
4252 */
4253static sector_t last_dev_address(sector_t s, struct geom *geo)
4254{
4255 s = (s | geo->chunk_mask) + 1;
4256 s >>= geo->chunk_shift;
4257 s *= geo->near_copies;
4258 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4259 s *= geo->far_copies;
4260 s <<= geo->chunk_shift;
4261 return s;
4262}
4263
4264/* Calculate the first device-address that could contain
4265 * any block from the chunk that includes the array-address 's'.
4266 * This too will be the start of a chunk
4267 */
4268static sector_t first_dev_address(sector_t s, struct geom *geo)
4269{
4270 s >>= geo->chunk_shift;
4271 s *= geo->near_copies;
4272 sector_div(s, geo->raid_disks);
4273 s *= geo->far_copies;
4274 s <<= geo->chunk_shift;
4275 return s;
4276}
4277
4278static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4279 int *skipped)
4280{
4281 /* We simply copy at most one chunk (smallest of old and new)
4282 * at a time, possibly less if that exceeds RESYNC_PAGES,
4283 * or we hit a bad block or something.
4284 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004285 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004286 * can record any location.
4287 *
4288 * If we will want to write to a location that isn't
4289 * yet recorded as 'safe' (i.e. in metadata on disk) then
4290 * we need to flush all reshape requests and update the metadata.
4291 *
4292 * When reshaping forwards (e.g. to more devices), we interpret
4293 * 'safe' as the earliest block which might not have been copied
4294 * down yet. We divide this by previous stripe size and multiply
4295 * by previous stripe length to get lowest device offset that we
4296 * cannot write to yet.
4297 * We interpret 'sector_nr' as an address that we want to write to.
4298 * From this we use last_device_address() to find where we might
4299 * write to, and first_device_address on the 'safe' position.
4300 * If this 'next' write position is after the 'safe' position,
4301 * we must update the metadata to increase the 'safe' position.
4302 *
4303 * When reshaping backwards, we round in the opposite direction
4304 * and perform the reverse test: next write position must not be
4305 * less than current safe position.
4306 *
4307 * In all this the minimum difference in data offsets
4308 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004309 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004310 *
4311 * We need to prepare all the bios here before we start any IO
4312 * to ensure the size we choose is acceptable to all devices.
4313 * The means one for each copy for write-out and an extra one for
4314 * read-in.
4315 * We store the read-in bio in ->master_bio and the others in
4316 * ->devs[x].bio and ->devs[x].repl_bio.
4317 */
4318 struct r10conf *conf = mddev->private;
4319 struct r10bio *r10_bio;
4320 sector_t next, safe, last;
4321 int max_sectors;
4322 int nr_sectors;
4323 int s;
4324 struct md_rdev *rdev;
4325 int need_flush = 0;
4326 struct bio *blist;
4327 struct bio *bio, *read_bio;
4328 int sectors_done = 0;
4329
4330 if (sector_nr == 0) {
4331 /* If restarting in the middle, skip the initial sectors */
4332 if (mddev->reshape_backwards &&
4333 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4334 sector_nr = (raid10_size(mddev, 0, 0)
4335 - conf->reshape_progress);
4336 } else if (!mddev->reshape_backwards &&
4337 conf->reshape_progress > 0)
4338 sector_nr = conf->reshape_progress;
4339 if (sector_nr) {
4340 mddev->curr_resync_completed = sector_nr;
4341 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4342 *skipped = 1;
4343 return sector_nr;
4344 }
4345 }
4346
4347 /* We don't use sector_nr to track where we are up to
4348 * as that doesn't work well for ->reshape_backwards.
4349 * So just use ->reshape_progress.
4350 */
4351 if (mddev->reshape_backwards) {
4352 /* 'next' is the earliest device address that we might
4353 * write to for this chunk in the new layout
4354 */
4355 next = first_dev_address(conf->reshape_progress - 1,
4356 &conf->geo);
4357
4358 /* 'safe' is the last device address that we might read from
4359 * in the old layout after a restart
4360 */
4361 safe = last_dev_address(conf->reshape_safe - 1,
4362 &conf->prev);
4363
4364 if (next + conf->offset_diff < safe)
4365 need_flush = 1;
4366
4367 last = conf->reshape_progress - 1;
4368 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4369 & conf->prev.chunk_mask);
4370 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4371 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4372 } else {
4373 /* 'next' is after the last device address that we
4374 * might write to for this chunk in the new layout
4375 */
4376 next = last_dev_address(conf->reshape_progress, &conf->geo);
4377
4378 /* 'safe' is the earliest device address that we might
4379 * read from in the old layout after a restart
4380 */
4381 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4382
4383 /* Need to update metadata if 'next' might be beyond 'safe'
4384 * as that would possibly corrupt data
4385 */
4386 if (next > safe + conf->offset_diff)
4387 need_flush = 1;
4388
4389 sector_nr = conf->reshape_progress;
4390 last = sector_nr | (conf->geo.chunk_mask
4391 & conf->prev.chunk_mask);
4392
4393 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4394 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4395 }
4396
4397 if (need_flush ||
4398 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4399 /* Need to update reshape_position in metadata */
4400 wait_barrier(conf);
4401 mddev->reshape_position = conf->reshape_progress;
4402 if (mddev->reshape_backwards)
4403 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4404 - conf->reshape_progress;
4405 else
4406 mddev->curr_resync_completed = conf->reshape_progress;
4407 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004408 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004409 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004410 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004411 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4412 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4413 allow_barrier(conf);
4414 return sectors_done;
4415 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004416 conf->reshape_safe = mddev->reshape_position;
4417 allow_barrier(conf);
4418 }
4419
4420read_more:
4421 /* Now schedule reads for blocks from sector_nr to last */
4422 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004423 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004424 raise_barrier(conf, sectors_done != 0);
4425 atomic_set(&r10_bio->remaining, 0);
4426 r10_bio->mddev = mddev;
4427 r10_bio->sector = sector_nr;
4428 set_bit(R10BIO_IsReshape, &r10_bio->state);
4429 r10_bio->sectors = last - sector_nr + 1;
4430 rdev = read_balance(conf, r10_bio, &max_sectors);
4431 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4432
4433 if (!rdev) {
4434 /* Cannot read from here, so need to record bad blocks
4435 * on all the target devices.
4436 */
4437 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004438 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004439 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4440 return sectors_done;
4441 }
4442
4443 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4444
4445 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004446 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004447 + rdev->data_offset);
4448 read_bio->bi_private = r10_bio;
Ming Lei81fa1522017-03-17 00:12:32 +08004449 read_bio->bi_end_io = end_reshape_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004450 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004451 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004452 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004453 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004454 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004455 r10_bio->master_bio = read_bio;
4456 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4457
4458 /* Now find the locations in the new layout */
4459 __raid10_find_phys(&conf->geo, r10_bio);
4460
4461 blist = read_bio;
4462 read_bio->bi_next = NULL;
4463
NeilBrownd094d682016-06-02 16:19:52 +10004464 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004465 for (s = 0; s < conf->copies*2; s++) {
4466 struct bio *b;
4467 int d = r10_bio->devs[s/2].devnum;
4468 struct md_rdev *rdev2;
4469 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004470 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004471 b = r10_bio->devs[s/2].repl_bio;
4472 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004473 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004474 b = r10_bio->devs[s/2].bio;
4475 }
4476 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4477 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004478
4479 bio_reset(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004480 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004481 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4482 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004483 b->bi_private = r10_bio;
4484 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004485 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004486 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004487 blist = b;
4488 }
4489
4490 /* Now add as many pages as possible to all of these bios. */
4491
4492 nr_sectors = 0;
4493 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4494 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4495 int len = (max_sectors - s) << 9;
4496 if (len > PAGE_SIZE)
4497 len = PAGE_SIZE;
4498 for (bio = blist; bio ; bio = bio->bi_next) {
Ming Leic85ba142017-03-17 00:12:22 +08004499 /*
4500 * won't fail because the vec table is big enough
4501 * to hold all these pages
4502 */
4503 bio_add_page(bio, page, len, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004504 }
4505 sector_nr += len >> 9;
4506 nr_sectors += len >> 9;
4507 }
NeilBrownd094d682016-06-02 16:19:52 +10004508 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004509 r10_bio->sectors = nr_sectors;
4510
4511 /* Now submit the read */
4512 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4513 atomic_inc(&r10_bio->remaining);
4514 read_bio->bi_next = NULL;
4515 generic_make_request(read_bio);
4516 sector_nr += nr_sectors;
4517 sectors_done += nr_sectors;
4518 if (sector_nr <= last)
4519 goto read_more;
4520
4521 /* Now that we have done the whole section we can
4522 * update reshape_progress
4523 */
4524 if (mddev->reshape_backwards)
4525 conf->reshape_progress -= sectors_done;
4526 else
4527 conf->reshape_progress += sectors_done;
4528
4529 return sectors_done;
4530}
4531
4532static void end_reshape_request(struct r10bio *r10_bio);
4533static int handle_reshape_read_error(struct mddev *mddev,
4534 struct r10bio *r10_bio);
4535static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4536{
4537 /* Reshape read completed. Hopefully we have a block
4538 * to write out.
4539 * If we got a read error then we do sync 1-page reads from
4540 * elsewhere until we find the data - or give up.
4541 */
4542 struct r10conf *conf = mddev->private;
4543 int s;
4544
4545 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4546 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4547 /* Reshape has been aborted */
4548 md_done_sync(mddev, r10_bio->sectors, 0);
4549 return;
4550 }
4551
4552 /* We definitely have the data in the pages, schedule the
4553 * writes.
4554 */
4555 atomic_set(&r10_bio->remaining, 1);
4556 for (s = 0; s < conf->copies*2; s++) {
4557 struct bio *b;
4558 int d = r10_bio->devs[s/2].devnum;
4559 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004560 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004561 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004562 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004563 b = r10_bio->devs[s/2].repl_bio;
4564 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004565 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004566 b = r10_bio->devs[s/2].bio;
4567 }
NeilBrownd094d682016-06-02 16:19:52 +10004568 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4569 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004570 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004571 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004572 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004573 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004574 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4575 atomic_inc(&r10_bio->remaining);
4576 b->bi_next = NULL;
4577 generic_make_request(b);
4578 }
4579 end_reshape_request(r10_bio);
4580}
4581
4582static void end_reshape(struct r10conf *conf)
4583{
4584 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4585 return;
4586
4587 spin_lock_irq(&conf->device_lock);
4588 conf->prev = conf->geo;
4589 md_finish_reshape(conf->mddev);
4590 smp_wmb();
4591 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004592 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004593 spin_unlock_irq(&conf->device_lock);
4594
4595 /* read-ahead size must cover two whole stripes, which is
4596 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4597 */
4598 if (conf->mddev->queue) {
4599 int stripe = conf->geo.raid_disks *
4600 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4601 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01004602 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4603 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004604 }
4605 conf->fullsync = 0;
4606}
4607
NeilBrown3ea7daa2012-05-22 13:53:47 +10004608static int handle_reshape_read_error(struct mddev *mddev,
4609 struct r10bio *r10_bio)
4610{
4611 /* Use sync reads to get the blocks from somewhere else */
4612 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004613 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004614 struct {
4615 struct r10bio r10_bio;
4616 struct r10dev devs[conf->copies];
4617 } on_stack;
4618 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004619 int slot = 0;
4620 int idx = 0;
4621 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4622
NeilBrowne0ee7782012-08-18 09:51:42 +10004623 r10b->sector = r10_bio->sector;
4624 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004625
4626 while (sectors) {
4627 int s = sectors;
4628 int success = 0;
4629 int first_slot = slot;
4630
4631 if (s > (PAGE_SIZE >> 9))
4632 s = PAGE_SIZE >> 9;
4633
NeilBrownd094d682016-06-02 16:19:52 +10004634 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004635 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004636 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004637 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004638 sector_t addr;
4639 if (rdev == NULL ||
4640 test_bit(Faulty, &rdev->flags) ||
4641 !test_bit(In_sync, &rdev->flags))
4642 goto failed;
4643
NeilBrowne0ee7782012-08-18 09:51:42 +10004644 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004645 atomic_inc(&rdev->nr_pending);
4646 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004647 success = sync_page_io(rdev,
4648 addr,
4649 s << 9,
4650 bvec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05004651 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004652 rdev_dec_pending(rdev, mddev);
4653 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004654 if (success)
4655 break;
4656 failed:
4657 slot++;
4658 if (slot >= conf->copies)
4659 slot = 0;
4660 if (slot == first_slot)
4661 break;
4662 }
NeilBrownd094d682016-06-02 16:19:52 +10004663 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004664 if (!success) {
4665 /* couldn't read this block, must give up */
4666 set_bit(MD_RECOVERY_INTR,
4667 &mddev->recovery);
4668 return -EIO;
4669 }
4670 sectors -= s;
4671 idx++;
4672 }
4673 return 0;
4674}
4675
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004676static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004677{
NeilBrown3ea7daa2012-05-22 13:53:47 +10004678 struct r10bio *r10_bio = bio->bi_private;
4679 struct mddev *mddev = r10_bio->mddev;
4680 struct r10conf *conf = mddev->private;
4681 int d;
4682 int slot;
4683 int repl;
4684 struct md_rdev *rdev = NULL;
4685
4686 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4687 if (repl)
4688 rdev = conf->mirrors[d].replacement;
4689 if (!rdev) {
4690 smp_mb();
4691 rdev = conf->mirrors[d].rdev;
4692 }
4693
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004694 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004695 /* FIXME should record badblock */
4696 md_error(mddev, rdev);
4697 }
4698
4699 rdev_dec_pending(rdev, mddev);
4700 end_reshape_request(r10_bio);
4701}
4702
4703static void end_reshape_request(struct r10bio *r10_bio)
4704{
4705 if (!atomic_dec_and_test(&r10_bio->remaining))
4706 return;
4707 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4708 bio_put(r10_bio->master_bio);
4709 put_buf(r10_bio);
4710}
4711
4712static void raid10_finish_reshape(struct mddev *mddev)
4713{
4714 struct r10conf *conf = mddev->private;
4715
4716 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4717 return;
4718
4719 if (mddev->delta_disks > 0) {
4720 sector_t size = raid10_size(mddev, 0, 0);
4721 md_set_array_sectors(mddev, size);
4722 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4723 mddev->recovery_cp = mddev->resync_max_sectors;
4724 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4725 }
4726 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004727 if (mddev->queue) {
4728 set_capacity(mddev->gendisk, mddev->array_sectors);
4729 revalidate_disk(mddev->gendisk);
4730 }
NeilBrown63aced62012-05-22 13:55:33 +10004731 } else {
4732 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004733 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004734 for (d = conf->geo.raid_disks ;
4735 d < conf->geo.raid_disks - mddev->delta_disks;
4736 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004737 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004738 if (rdev)
4739 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004740 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004741 if (rdev)
4742 clear_bit(In_sync, &rdev->flags);
4743 }
NeilBrownd094d682016-06-02 16:19:52 +10004744 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004745 }
4746 mddev->layout = mddev->new_layout;
4747 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4748 mddev->reshape_position = MaxSector;
4749 mddev->delta_disks = 0;
4750 mddev->reshape_backwards = 0;
4751}
4752
NeilBrown84fc4b52011-10-11 16:49:58 +11004753static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
4755 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004756 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004758 .make_request = raid10_make_request,
4759 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004760 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004761 .status = raid10_status,
4762 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 .hot_add_disk = raid10_add_disk,
4764 .hot_remove_disk= raid10_remove_disk,
4765 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004766 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004767 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004768 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004769 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004770 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004771 .check_reshape = raid10_check_reshape,
4772 .start_reshape = raid10_start_reshape,
4773 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004774 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775};
4776
4777static int __init raid_init(void)
4778{
NeilBrown2604b702006-01-06 00:20:36 -08004779 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
4782static void raid_exit(void)
4783{
NeilBrown2604b702006-01-06 00:20:36 -08004784 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
4787module_init(raid_init);
4788module_exit(raid_exit);
4789MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004790MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004792MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004793MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004794
4795module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);