blob: 0f1b78b386491e3cbddd7bd5cbd3ff8a841dbf02 [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/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +1000131/* amount of memory to reserve for resync requests */
132#define RESYNC_WINDOW (1024*1024)
133/* maximum number of concurrent requests, memory permitting */
134#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
137 * When performing a resync, we need to read and compare, so
138 * we need as many pages are there are copies.
139 * When performing a recovery, we need 2 bios, one for read,
140 * one for write (we recover only one drive per r10buf)
141 *
142 */
Al Virodd0fc662005-10-07 07:46:04 +0100143static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
NeilBrowne879a872011-10-11 16:49:02 +1100145 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100147 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct bio *bio;
149 int i, j;
150 int nalloc;
151
152 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100153 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
NeilBrown3ea7daa2012-05-22 13:53:47 +1000156 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
157 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 nalloc = conf->copies; /* resync */
159 else
160 nalloc = 2; /* recovery */
161
162 /*
163 * Allocate bios.
164 */
165 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100166 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!bio)
168 goto out_free_bio;
169 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100170 if (!conf->have_replacement)
171 continue;
172 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
173 if (!bio)
174 goto out_free_bio;
175 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 /*
178 * Allocate RESYNC_PAGES data pages and attach them
179 * where needed.
180 */
181 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100182 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 bio = r10_bio->devs[j].bio;
184 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown3ea7daa2012-05-22 13:53:47 +1000185 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
186 &conf->mddev->recovery)) {
187 /* we can share bv_page's during recovery
188 * and reshape */
Namhyung Kimc65060a2011-07-18 17:38:49 +1000189 struct bio *rbio = r10_bio->devs[0].bio;
190 page = rbio->bi_io_vec[i].bv_page;
191 get_page(page);
192 } else
193 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (unlikely(!page))
195 goto out_free_pages;
196
197 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100198 if (rbio)
199 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 }
202
203 return r10_bio;
204
205out_free_pages:
206 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800207 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 while (j--)
209 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800210 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
majianpeng5fdd2cf2012-05-22 13:55:03 +1000211 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000213 for ( ; j < nalloc; j++) {
214 if (r10_bio->devs[j].bio)
215 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100216 if (r10_bio->devs[j].repl_bio)
217 bio_put(r10_bio->devs[j].repl_bio);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 r10bio_pool_free(r10_bio, conf);
220 return NULL;
221}
222
223static void r10buf_pool_free(void *__r10_bio, void *data)
224{
225 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100226 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100227 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int j;
229
230 for (j=0; j < conf->copies; j++) {
231 struct bio *bio = r10bio->devs[j].bio;
232 if (bio) {
233 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800234 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 bio->bi_io_vec[i].bv_page = NULL;
236 }
237 bio_put(bio);
238 }
NeilBrown69335ef2011-12-23 10:17:54 +1100239 bio = r10bio->devs[j].repl_bio;
240 if (bio)
241 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 r10bio_pool_free(r10bio, conf);
244}
245
NeilBrowne879a872011-10-11 16:49:02 +1100246static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int i;
249
250 for (i = 0; i < conf->copies; i++) {
251 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000252 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 bio_put(*bio);
254 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100255 bio = &r10_bio->devs[i].repl_bio;
256 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
257 bio_put(*bio);
258 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260}
261
NeilBrown9f2c9d12011-10-11 16:48:43 +1100262static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
NeilBrowne879a872011-10-11 16:49:02 +1100264 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 put_all_bios(conf, r10_bio);
267 mempool_free(r10_bio, conf->r10bio_pool);
268}
269
NeilBrown9f2c9d12011-10-11 16:48:43 +1100270static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
NeilBrowne879a872011-10-11 16:49:02 +1100272 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 mempool_free(r10_bio, conf->r10buf_pool);
275
NeilBrown0a27ec92006-01-06 00:20:13 -0800276 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
NeilBrown9f2c9d12011-10-11 16:48:43 +1100279static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100282 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100283 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 spin_lock_irqsave(&conf->device_lock, flags);
286 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800287 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 spin_unlock_irqrestore(&conf->device_lock, flags);
289
Arthur Jones388667b2008-07-25 12:03:38 -0700290 /* wake up frozen array... */
291 wake_up(&conf->wait_barrier);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 md_wakeup_thread(mddev->thread);
294}
295
296/*
297 * raid_end_bio_io() is called when we have finished servicing a mirrored
298 * operation and are ready to return a success/failure code to the buffer
299 * cache layer.
300 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100301static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct bio *bio = r10_bio->master_bio;
NeilBrowne879a872011-10-11 16:49:02 +1100304 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
NeilBrown856e08e2011-07-28 11:39:23 +1000306 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200307 bio->bi_error = -EIO;
NeilBrownfd16f2e2017-03-15 14:05:13 +1100308
309 bio_endio(bio);
310 /*
311 * Wake up any possible resync thread that waits for the device
312 * to go idle.
313 */
314 allow_barrier(conf);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 free_r10bio(r10_bio);
317}
318
319/*
320 * Update disk head position estimator based on IRQ completion info.
321 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100322static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrowne879a872011-10-11 16:49:02 +1100324 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
327 r10_bio->devs[slot].addr + (r10_bio->sectors);
328}
329
Namhyung Kim778ca012011-07-18 17:38:47 +1000330/*
331 * Find the disk number which triggered given bio
332 */
NeilBrowne879a872011-10-11 16:49:02 +1100333static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100334 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000335{
336 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100337 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000338
NeilBrown69335ef2011-12-23 10:17:54 +1100339 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000340 if (r10_bio->devs[slot].bio == bio)
341 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100342 if (r10_bio->devs[slot].repl_bio == bio) {
343 repl = 1;
344 break;
345 }
346 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000347
348 BUG_ON(slot == conf->copies);
349 update_head_pos(slot, r10_bio);
350
NeilBrown749c55e2011-07-28 11:39:24 +1000351 if (slotp)
352 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100353 if (replp)
354 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000355 return r10_bio->devs[slot].devnum;
356}
357
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200358static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200360 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100361 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100363 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100364 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 slot = r10_bio->read_slot;
367 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100368 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
370 * this branch is our 'one mirror IO has finished' event handler:
371 */
NeilBrown4443ae12006-01-06 00:20:28 -0800372 update_head_pos(slot, r10_bio);
373
374 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * Set R10BIO_Uptodate in our master bio, so that
377 * we will return a good error code to the higher
378 * levels even if IO on some other mirrored buffer fails.
379 *
380 * The 'master' represents the composite IO operation to
381 * user-side. So if something waits for IO, then it will
382 * wait for the 'master' bio.
383 */
384 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100385 } else {
386 /* If all other devices that store this block have
387 * failed, we want to return the error upwards rather
388 * than fail the last device. Here we redefine
389 * "uptodate" to mean "Don't want to retry"
390 */
NeilBrown635f6412013-06-11 14:57:09 +1000391 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
392 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100393 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100394 }
395 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100397 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000400 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
402 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100403 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000404 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100405 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000406 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000407 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 reschedule_retry(r10_bio);
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
NeilBrown9f2c9d12011-10-11 16:48:43 +1100412static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000413{
414 /* clear the bitmap if all writes complete successfully */
415 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
416 r10_bio->sectors,
417 !test_bit(R10BIO_Degraded, &r10_bio->state),
418 0);
419 md_write_end(r10_bio->mddev);
420}
421
NeilBrown9f2c9d12011-10-11 16:48:43 +1100422static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000423{
424 if (atomic_dec_and_test(&r10_bio->remaining)) {
425 if (test_bit(R10BIO_WriteError, &r10_bio->state))
426 reschedule_retry(r10_bio);
427 else {
428 close_write(r10_bio);
429 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
430 reschedule_retry(r10_bio);
431 else
432 raid_end_bio_io(r10_bio);
433 }
434 }
435}
436
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200437static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100439 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000440 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000441 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100442 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100443 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100444 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100445 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700446 bool discard_error;
447
448 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
NeilBrown475b0322011-12-23 10:17:55 +1100450 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
NeilBrown475b0322011-12-23 10:17:55 +1100452 if (repl)
453 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100454 if (!rdev) {
455 smp_rmb();
456 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100457 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /*
460 * this branch is our 'one mirror IO has finished' event handler:
461 */
Shaohua Li579ed342016-10-06 14:13:52 -0700462 if (bio->bi_error && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100463 if (repl)
464 /* Never record new bad blocks to replacement,
465 * just fail it.
466 */
467 md_error(rdev->mddev, rdev);
468 else {
469 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100470 if (!test_and_set_bit(WantReplacement, &rdev->flags))
471 set_bit(MD_RECOVERY_NEEDED,
472 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100473
NeilBrown475b0322011-12-23 10:17:55 +1100474 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100475 if (test_bit(FailFast, &rdev->flags) &&
476 (bio->bi_opf & MD_FAILFAST)) {
477 md_error(rdev->mddev, rdev);
478 if (!test_bit(Faulty, &rdev->flags))
479 /* This is the only remaining device,
480 * We need to retry the write without
481 * FailFast
482 */
483 set_bit(R10BIO_WriteError, &r10_bio->state);
484 else {
485 r10_bio->devs[slot].bio = NULL;
486 to_put = bio;
487 dec_rdev = 1;
488 }
489 } else
490 set_bit(R10BIO_WriteError, &r10_bio->state);
NeilBrown475b0322011-12-23 10:17:55 +1100491 }
NeilBrown749c55e2011-07-28 11:39:24 +1000492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Set R10BIO_Uptodate in our master bio, so that
495 * we will return a good error code for to the higher
496 * levels even if IO on some other mirrored buffer fails.
497 *
498 * The 'master' represents the composite IO operation to
499 * user-side. So if something waits for IO, then it will
500 * wait for the 'master' bio.
501 */
NeilBrown749c55e2011-07-28 11:39:24 +1000502 sector_t first_bad;
503 int bad_sectors;
504
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300505 /*
506 * Do not set R10BIO_Uptodate if the current device is
507 * rebuilding or Faulty. This is because we cannot use
508 * such device for properly reading the data back (we could
509 * potentially use it, if the current write would have felt
510 * before rdev->recovery_offset, but for simplicity we don't
511 * check this here.
512 */
513 if (test_bit(In_sync, &rdev->flags) &&
514 !test_bit(Faulty, &rdev->flags))
515 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
NeilBrown749c55e2011-07-28 11:39:24 +1000517 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100518 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000519 r10_bio->devs[slot].addr,
520 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700521 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000522 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100523 if (repl)
524 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
525 else
526 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000527 dec_rdev = 0;
528 set_bit(R10BIO_MadeGood, &r10_bio->state);
529 }
530 }
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 *
534 * Let's see if all mirrored write operations have finished
535 * already.
536 */
NeilBrown19d5f832011-09-10 17:21:17 +1000537 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000538 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100539 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100540 if (to_put)
541 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
545 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300546 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * parameters: near_copies and far_copies.
548 * near_copies * far_copies must be <= raid_disks.
549 * Normally one of these will be 1.
550 * If both are 1, we get raid0.
551 * If near_copies == raid_disks, we get raid1.
552 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300553 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * first chunk, followed by near_copies copies of the next chunk and
555 * so on.
556 * If far_copies > 1, then after 1/far_copies of the array has been assigned
557 * as described above, we start again with a device offset of near_copies.
558 * So we effectively have another copy of the whole array further down all
559 * the drives, but with blocks on different drives.
560 * With this layout, and block is never stored twice on the one device.
561 *
562 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700563 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *
565 * raid10_find_virt does the reverse mapping, from a device and a
566 * sector offset to a virtual address
567 */
568
NeilBrownf8c9e742012-05-21 09:28:33 +1000569static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 int n,f;
572 sector_t sector;
573 sector_t chunk;
574 sector_t stripe;
575 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100577 int last_far_set_start, last_far_set_size;
578
579 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
580 last_far_set_start *= geo->far_set_size;
581
582 last_far_set_size = geo->far_set_size;
583 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000586 chunk = r10bio->sector >> geo->chunk_shift;
587 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
NeilBrown5cf00fc2012-05-21 09:28:20 +1000589 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000591 dev = sector_div(stripe, geo->raid_disks);
592 if (geo->far_offset)
593 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
NeilBrown5cf00fc2012-05-21 09:28:20 +1000595 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000598 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100600 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100603 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 slot++;
605
NeilBrown5cf00fc2012-05-21 09:28:20 +1000606 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100607 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000608 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100609
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100610 if ((geo->raid_disks % geo->far_set_size) &&
611 (d > last_far_set_start)) {
612 d -= last_far_set_start;
613 d %= last_far_set_size;
614 d += last_far_set_start;
615 } else {
616 d %= geo->far_set_size;
617 d += geo->far_set_size * set;
618 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000619 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 r10bio->devs[slot].devnum = d;
621 r10bio->devs[slot].addr = s;
622 slot++;
623 }
624 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000625 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000627 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000630}
631
632static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
633{
634 struct geom *geo = &conf->geo;
635
636 if (conf->reshape_progress != MaxSector &&
637 ((r10bio->sector >= conf->reshape_progress) !=
638 conf->mddev->reshape_backwards)) {
639 set_bit(R10BIO_Previous, &r10bio->state);
640 geo = &conf->prev;
641 } else
642 clear_bit(R10BIO_Previous, &r10bio->state);
643
644 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
NeilBrowne879a872011-10-11 16:49:02 +1100647static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000650 /* Never use conf->prev as this is only called during resync
651 * or recovery, so reshape isn't happening
652 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000653 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100654 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
655 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100656 int last_far_set_start;
657
658 if (geo->raid_disks % geo->far_set_size) {
659 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
660 last_far_set_start *= geo->far_set_size;
661
662 if (dev >= last_far_set_start) {
663 far_set_size = geo->far_set_size;
664 far_set_size += (geo->raid_disks % geo->far_set_size);
665 far_set_start = last_far_set_start;
666 }
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
NeilBrown5cf00fc2012-05-21 09:28:20 +1000669 offset = sector & geo->chunk_mask;
670 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700671 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000672 chunk = sector >> geo->chunk_shift;
673 fc = sector_div(chunk, geo->far_copies);
674 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100675 if (dev < far_set_start)
676 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700677 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000678 while (sector >= geo->stride) {
679 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100680 if (dev < (geo->near_copies + far_set_start))
681 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700682 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000683 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700684 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000685 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700686 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000687 vchunk = chunk * geo->raid_disks + dev;
688 sector_div(vchunk, geo->near_copies);
689 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
693 * This routine returns the disk from which the requested read should
694 * be done. There is a per-array 'next expected sequential IO' sector
695 * number - if this matches on the next IO then we use the last disk.
696 * There is also a per-disk 'last know head position' sector that is
697 * maintained from IRQ contexts, both the normal and the resync IO
698 * completion handlers update this position correctly. If there is no
699 * perfect sequential match then we pick the disk whose head is closest.
700 *
701 * If there are 2 mirrors in the same 2 devices, performance degrades
702 * because position is mirror, not device based.
703 *
704 * The rdev for the device selected will have nr_pending incremented.
705 */
706
707/*
708 * FIXME: possibly should rethink readbalancing and do it differently
709 * depending on near_copies / far_copies geometry.
710 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100711static struct md_rdev *read_balance(struct r10conf *conf,
712 struct r10bio *r10_bio,
713 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000715 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000716 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000717 int sectors = r10_bio->sectors;
718 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000719 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000720 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000721 int do_balance;
722 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000723 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 raid10_find_phys(conf, r10_bio);
726 rcu_read_lock();
NeilBrown856e08e2011-07-28 11:39:23 +1000727 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000728 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100729 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000730 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000731 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000732 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100733 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800736 * device if no resync is going on (recovery is ok), or below
737 * the resync window. We take the first readable disk when
738 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
740 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000741 && (this_sector + sectors >= conf->next_resync))
742 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
NeilBrown56d99122011-05-11 14:27:03 +1000744 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000745 sector_t first_bad;
746 int bad_sectors;
747 sector_t dev_sector;
748
NeilBrown56d99122011-05-11 14:27:03 +1000749 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000751 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100752 rdev = rcu_dereference(conf->mirrors[disk].replacement);
753 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
754 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
755 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100756 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700757 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100758 continue;
759 if (!test_bit(In_sync, &rdev->flags) &&
760 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000761 continue;
762
NeilBrown856e08e2011-07-28 11:39:23 +1000763 dev_sector = r10_bio->devs[slot].addr;
764 if (is_badblock(rdev, dev_sector, sectors,
765 &first_bad, &bad_sectors)) {
766 if (best_dist < MaxSector)
767 /* Already have a better slot */
768 continue;
769 if (first_bad <= dev_sector) {
770 /* Cannot read here. If this is the
771 * 'primary' device, then we must not read
772 * beyond 'bad_sectors' from another device.
773 */
774 bad_sectors -= (dev_sector - first_bad);
775 if (!do_balance && sectors > bad_sectors)
776 sectors = bad_sectors;
777 if (best_good_sectors > sectors)
778 best_good_sectors = sectors;
779 } else {
780 sector_t good_sectors =
781 first_bad - dev_sector;
782 if (good_sectors > best_good_sectors) {
783 best_good_sectors = good_sectors;
784 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100785 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000786 }
787 if (!do_balance)
788 /* Must read from here */
789 break;
790 }
791 continue;
792 } else
793 best_good_sectors = sectors;
794
NeilBrown56d99122011-05-11 14:27:03 +1000795 if (!do_balance)
796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
NeilBrown8d3ca832016-11-18 16:16:12 +1100798 if (best_slot >= 0)
799 /* At least 2 disks to choose from so failfast is OK */
800 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800801 /* This optimisation is debatable, and completely destroys
802 * sequential read speed for 'far copies' arrays. So only
803 * keep it for 'near' arrays, and review those later.
804 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000805 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
NeilBrown8d3ca832016-11-18 16:16:12 +1100806 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800807
808 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100809 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000810 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800811 else
NeilBrown56d99122011-05-11 14:27:03 +1000812 new_distance = abs(r10_bio->devs[slot].addr -
813 conf->mirrors[disk].head_position);
814 if (new_distance < best_dist) {
815 best_dist = new_distance;
816 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100817 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 }
NeilBrownabbf0982011-12-23 10:17:54 +1100820 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000821 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100822 rdev = best_rdev;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
NeilBrown56d99122011-05-11 14:27:03 +1000825 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000826 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000827 r10_bio->read_slot = slot;
828 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100829 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000831 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
NeilBrown96c3fd12011-12-23 10:17:54 +1100833 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
NeilBrown5c675f82014-12-15 12:56:56 +1100836static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700837{
NeilBrowne879a872011-10-11 16:49:02 +1100838 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700839 int i, ret = 0;
840
Tejun Heo44522262015-05-22 17:13:26 -0400841 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100842 conf->pending_count >= max_queued_requests)
843 return 1;
844
NeilBrown0d129222006-10-03 01:15:54 -0700845 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000846 for (i = 0;
847 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
848 && ret == 0;
849 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100850 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700851 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200852 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700853
Jan Karadc3b17c2017-02-02 15:56:50 +0100854 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700855 }
856 }
857 rcu_read_unlock();
858 return ret;
859}
860
NeilBrowne879a872011-10-11 16:49:02 +1100861static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800862{
863 /* Any writes that have been queued but are awaiting
864 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800865 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800866 spin_lock_irq(&conf->device_lock);
867
868 if (conf->pending_bio_list.head) {
869 struct bio *bio;
870 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100871 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800872 spin_unlock_irq(&conf->device_lock);
873 /* flush any pending bitmap writes to disk
874 * before proceeding w/ I/O */
875 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100876 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800877
878 while (bio) { /* submit pending writes */
879 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100880 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800881 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100882 bio->bi_bdev = rdev->bdev;
883 if (test_bit(Faulty, &rdev->flags)) {
884 bio->bi_error = -EIO;
885 bio_endio(bio);
886 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
887 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li532a2a32012-10-11 13:30:52 +1100888 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200889 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100890 else
891 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800892 bio = next;
893 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800894 } else
895 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800896}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100897
NeilBrown0a27ec92006-01-06 00:20:13 -0800898/* Barriers....
899 * Sometimes we need to suspend IO while we do something else,
900 * either some resync/recovery, or reconfigure the array.
901 * To do this we raise a 'barrier'.
902 * The 'barrier' is a counter that can be raised multiple times
903 * to count how many activities are happening which preclude
904 * normal IO.
905 * We can only raise the barrier if there is no pending IO.
906 * i.e. if nr_pending == 0.
907 * We choose only to raise the barrier if no-one is waiting for the
908 * barrier to go down. This means that as soon as an IO request
909 * is ready, no other operations which require a barrier will start
910 * until the IO request has had a chance.
911 *
912 * So: regular IO calls 'wait_barrier'. When that returns there
913 * is no backgroup IO happening, It must arrange to call
914 * allow_barrier when it has finished its IO.
915 * backgroup IO calls must call raise_barrier. Once that returns
916 * there is no normal IO happeing. It must arrange to call
917 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
NeilBrowne879a872011-10-11 16:49:02 +1100920static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
NeilBrown6cce3b22006-01-06 00:20:16 -0800922 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
NeilBrown6cce3b22006-01-06 00:20:16 -0800925 /* Wait until no block IO is waiting (unless 'force') */
926 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100927 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800928
929 /* block any new IO from starting */
930 conf->barrier++;
931
NeilBrownc3b328a2011-04-18 18:25:43 +1000932 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800933 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200934 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100935 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 spin_unlock_irq(&conf->resync_lock);
938}
939
NeilBrowne879a872011-10-11 16:49:02 +1100940static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800941{
942 unsigned long flags;
943 spin_lock_irqsave(&conf->resync_lock, flags);
944 conf->barrier--;
945 spin_unlock_irqrestore(&conf->resync_lock, flags);
946 wake_up(&conf->wait_barrier);
947}
948
NeilBrowne879a872011-10-11 16:49:02 +1100949static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800950{
951 spin_lock_irq(&conf->resync_lock);
952 if (conf->barrier) {
953 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100954 /* Wait for the barrier to drop.
955 * However if there are already pending
956 * requests (preventing the barrier from
957 * rising completely), and the
958 * pre-process bio queue isn't empty,
959 * then don't wait, as we need to empty
960 * that queue to get the nr_pending
961 * count down.
962 */
NeilBrown578b54a2016-11-14 16:30:21 +1100963 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100964 wait_event_lock_irq(conf->wait_barrier,
965 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200966 (atomic_read(&conf->nr_pending) &&
NeilBrownd6b42dc2012-03-19 12:46:38 +1100967 current->bio_list &&
NeilBrownf5fe1b52017-03-10 17:00:47 +1100968 (!bio_list_empty(&current->bio_list[0]) ||
969 !bio_list_empty(&current->bio_list[1]))),
Lukas Czernereed8c022012-11-30 11:42:40 +0100970 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800971 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200972 if (!conf->nr_waiting)
973 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800974 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200975 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800976 spin_unlock_irq(&conf->resync_lock);
977}
978
NeilBrownfd16f2e2017-03-15 14:05:13 +1100979static void inc_pending(struct r10conf *conf)
980{
981 /* The current request requires multiple r10_bio, so
982 * we need to increment the pending count.
983 */
984 WARN_ON(!atomic_read(&conf->nr_pending));
985 atomic_inc(&conf->nr_pending);
986}
987
NeilBrowne879a872011-10-11 16:49:02 +1100988static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800989{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200990 if ((atomic_dec_and_test(&conf->nr_pending)) ||
991 (conf->array_freeze_pending))
992 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800993}
994
NeilBrowne2d59922013-06-12 11:01:22 +1000995static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -0800996{
997 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800998 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800999 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001000 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001001 * This is called in the context of one normal IO request
1002 * that has failed. Thus any sync request that might be pending
1003 * will be blocked by nr_pending, and we need to wait for
1004 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001005 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001006 * must match the number of pending IOs (nr_pending) before
1007 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001008 */
1009 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001010 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001011 conf->barrier++;
1012 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001013 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001014 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001015 conf->resync_lock,
1016 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001017
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001018 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001019 spin_unlock_irq(&conf->resync_lock);
1020}
1021
NeilBrowne879a872011-10-11 16:49:02 +11001022static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001023{
1024 /* reverse the effect of the freeze */
1025 spin_lock_irq(&conf->resync_lock);
1026 conf->barrier--;
1027 conf->nr_waiting--;
1028 wake_up(&conf->wait_barrier);
1029 spin_unlock_irq(&conf->resync_lock);
1030}
1031
NeilBrownf8c9e742012-05-21 09:28:33 +10001032static sector_t choose_data_offset(struct r10bio *r10_bio,
1033 struct md_rdev *rdev)
1034{
1035 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1036 test_bit(R10BIO_Previous, &r10_bio->state))
1037 return rdev->data_offset;
1038 else
1039 return rdev->new_data_offset;
1040}
1041
NeilBrown57c67df2012-10-11 13:32:13 +11001042struct raid10_plug_cb {
1043 struct blk_plug_cb cb;
1044 struct bio_list pending;
1045 int pending_cnt;
1046};
1047
1048static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1049{
1050 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1051 cb);
1052 struct mddev *mddev = plug->cb.data;
1053 struct r10conf *conf = mddev->private;
1054 struct bio *bio;
1055
NeilBrown874807a2012-11-27 12:14:40 +11001056 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001057 spin_lock_irq(&conf->device_lock);
1058 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1059 conf->pending_count += plug->pending_cnt;
1060 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001061 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001062 md_wakeup_thread(mddev->thread);
1063 kfree(plug);
1064 return;
1065 }
1066
1067 /* we aren't scheduling, so we can do the write-out directly. */
1068 bio = bio_list_get(&plug->pending);
1069 bitmap_unplug(mddev->bitmap);
1070 wake_up(&conf->wait_barrier);
1071
1072 while (bio) { /* submit pending writes */
1073 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001074 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001075 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001076 bio->bi_bdev = rdev->bdev;
1077 if (test_bit(Faulty, &rdev->flags)) {
1078 bio->bi_error = -EIO;
1079 bio_endio(bio);
1080 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1081 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001082 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001083 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001084 else
1085 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001086 bio = next;
1087 }
1088 kfree(plug);
1089}
1090
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001091static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1092 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
NeilBrowne879a872011-10-11 16:49:02 +11001094 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001096 const int op = bio_op(bio);
1097 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1098 int sectors_handled;
1099 int max_sectors;
1100 sector_t sectors;
1101 struct md_rdev *rdev;
1102 int slot;
1103
1104 /*
1105 * Register the new request and wait if the reconstruction
1106 * thread has put up a bar for new requests.
1107 * Continue immediately if no resync is active currently.
1108 */
1109 wait_barrier(conf);
1110
1111 sectors = bio_sectors(bio);
1112 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1113 bio->bi_iter.bi_sector < conf->reshape_progress &&
1114 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1115 /*
1116 * IO spans the reshape position. Need to wait for reshape to
1117 * pass
1118 */
1119 raid10_log(conf->mddev, "wait reshape");
1120 allow_barrier(conf);
1121 wait_event(conf->wait_barrier,
1122 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1123 conf->reshape_progress >= bio->bi_iter.bi_sector +
1124 sectors);
1125 wait_barrier(conf);
1126 }
1127
1128read_again:
1129 rdev = read_balance(conf, r10_bio, &max_sectors);
1130 if (!rdev) {
1131 raid_end_bio_io(r10_bio);
1132 return;
1133 }
1134 slot = r10_bio->read_slot;
1135
Ming Leid7a10302017-02-14 23:29:03 +08001136 read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001137 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
1138 max_sectors);
1139
1140 r10_bio->devs[slot].bio = read_bio;
1141 r10_bio->devs[slot].rdev = rdev;
1142
1143 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1144 choose_data_offset(r10_bio, rdev);
1145 read_bio->bi_bdev = rdev->bdev;
1146 read_bio->bi_end_io = raid10_end_read_request;
1147 bio_set_op_attrs(read_bio, op, do_sync);
1148 if (test_bit(FailFast, &rdev->flags) &&
1149 test_bit(R10BIO_FailFast, &r10_bio->state))
1150 read_bio->bi_opf |= MD_FAILFAST;
1151 read_bio->bi_private = r10_bio;
1152
1153 if (mddev->gendisk)
1154 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1155 read_bio, disk_devt(mddev->gendisk),
1156 r10_bio->sector);
1157 if (max_sectors < r10_bio->sectors) {
1158 /*
1159 * Could not read all from this device, so we will need another
1160 * r10_bio.
1161 */
1162 sectors_handled = (r10_bio->sector + max_sectors
1163 - bio->bi_iter.bi_sector);
1164 r10_bio->sectors = max_sectors;
NeilBrownfd16f2e2017-03-15 14:05:13 +11001165 inc_pending(conf);
1166 bio_inc_remaining(bio);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001167 /*
1168 * Cannot call generic_make_request directly as that will be
1169 * queued in __generic_make_request and subsequent
1170 * mempool_alloc might block waiting for it. so hand bio over
1171 * to raid10d.
1172 */
1173 reschedule_retry(r10_bio);
1174
1175 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1176
1177 r10_bio->master_bio = bio;
1178 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1179 r10_bio->state = 0;
1180 r10_bio->mddev = mddev;
1181 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1182 goto read_again;
1183 } else
1184 generic_make_request(read_bio);
1185 return;
1186}
1187
1188static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1189 struct r10bio *r10_bio)
1190{
1191 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int i;
Mike Christie796a5cf2016-06-05 14:32:07 -05001193 const int op = bio_op(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001194 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1195 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -08001196 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001197 struct md_rdev *blocked_rdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001198 struct blk_plug_cb *cb;
1199 struct raid10_plug_cb *plug = NULL;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001200 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001201 int sectors_handled;
1202 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tomasz Majchrzak9b622e22016-07-28 10:28:25 +02001204 md_write_start(mddev, bio);
1205
NeilBrowncc13b1d2014-05-05 13:34:37 +10001206 /*
1207 * Register the new request and wait if the reconstruction
1208 * thread has put up a bar for new requests.
1209 * Continue immediately if no resync is active currently.
1210 */
1211 wait_barrier(conf);
1212
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001213 sectors = bio_sectors(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001214 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001215 bio->bi_iter.bi_sector < conf->reshape_progress &&
1216 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001217 /*
1218 * IO spans the reshape position. Need to wait for reshape to
1219 * pass
NeilBrown3ea7daa2012-05-22 13:53:47 +10001220 */
NeilBrown578b54a2016-11-14 16:30:21 +11001221 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001222 allow_barrier(conf);
1223 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001224 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1225 conf->reshape_progress >= bio->bi_iter.bi_sector +
1226 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001227 wait_barrier(conf);
1228 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001229
NeilBrown3ea7daa2012-05-22 13:53:47 +10001230 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001231 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001232 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1233 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1234 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1235 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001236 /* Need to update reshape_position in metadata */
1237 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001238 set_mask_bits(&mddev->sb_flags, 0,
1239 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001240 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001241 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001242 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001243 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001244
1245 conf->reshape_safe = mddev->reshape_position;
1246 }
1247
NeilBrown34db0cd2011-10-11 16:50:01 +11001248 if (conf->pending_count >= max_queued_requests) {
1249 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001250 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001251 wait_event(conf->wait_barrier,
1252 conf->pending_count < max_queued_requests);
1253 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001254 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * inc refcount on their rdev. Record them by setting
1256 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001257 * If there are known/acknowledged bad blocks on any device
1258 * on which we have seen a write error, we want to avoid
1259 * writing to those blocks. This potentially requires several
1260 * writes to write around the bad blocks. Each set of writes
NeilBrownfd16f2e2017-03-15 14:05:13 +11001261 * gets its own r10_bio with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001263
NeilBrown69335ef2011-12-23 10:17:54 +11001264 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001266retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001267 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001269 max_sectors = r10_bio->sectors;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 for (i = 0; i < conf->copies; i++) {
1272 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001273 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001274 struct md_rdev *rrdev = rcu_dereference(
1275 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001276 if (rdev == rrdev)
1277 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001278 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1279 atomic_inc(&rdev->nr_pending);
1280 blocked_rdev = rdev;
1281 break;
1282 }
NeilBrown475b0322011-12-23 10:17:55 +11001283 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1284 atomic_inc(&rrdev->nr_pending);
1285 blocked_rdev = rrdev;
1286 break;
1287 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001288 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001289 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001290 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001291 rrdev = NULL;
1292
NeilBrownd4432c22011-07-28 11:39:24 +10001293 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001294 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001295
1296 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001297 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001298 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001299 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001300 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001301 sector_t first_bad;
1302 sector_t dev_sector = r10_bio->devs[i].addr;
1303 int bad_sectors;
1304 int is_bad;
1305
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001306 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001307 &first_bad, &bad_sectors);
1308 if (is_bad < 0) {
1309 /* Mustn't write here until the bad block
1310 * is acknowledged
1311 */
1312 atomic_inc(&rdev->nr_pending);
1313 set_bit(BlockedBadBlocks, &rdev->flags);
1314 blocked_rdev = rdev;
1315 break;
1316 }
1317 if (is_bad && first_bad <= dev_sector) {
1318 /* Cannot write here at all */
1319 bad_sectors -= (dev_sector - first_bad);
1320 if (bad_sectors < max_sectors)
1321 /* Mustn't write more than bad_sectors
1322 * to other devices yet
1323 */
1324 max_sectors = bad_sectors;
1325 /* We don't set R10BIO_Degraded as that
1326 * only applies if the disk is missing,
1327 * so it might be re-added, and we want to
1328 * know to recover this chunk.
1329 * In this case the device is here, and the
1330 * fact that this chunk is not in-sync is
1331 * recorded in the bad block log.
1332 */
1333 continue;
1334 }
1335 if (is_bad) {
1336 int good_sectors = first_bad - dev_sector;
1337 if (good_sectors < max_sectors)
1338 max_sectors = good_sectors;
1339 }
1340 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001341 if (rdev) {
1342 r10_bio->devs[i].bio = bio;
1343 atomic_inc(&rdev->nr_pending);
1344 }
NeilBrown475b0322011-12-23 10:17:55 +11001345 if (rrdev) {
1346 r10_bio->devs[i].repl_bio = bio;
1347 atomic_inc(&rrdev->nr_pending);
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 rcu_read_unlock();
1351
Dan Williams6bfe0b42008-04-30 00:52:32 -07001352 if (unlikely(blocked_rdev)) {
1353 /* Have to wait for this device to get unblocked, then retry */
1354 int j;
1355 int d;
1356
NeilBrown475b0322011-12-23 10:17:55 +11001357 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001358 if (r10_bio->devs[j].bio) {
1359 d = r10_bio->devs[j].devnum;
1360 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1361 }
NeilBrown475b0322011-12-23 10:17:55 +11001362 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001363 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001364 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001365 rdev = conf->mirrors[d].replacement;
1366 if (!rdev) {
1367 /* Race with remove_disk */
1368 smp_mb();
1369 rdev = conf->mirrors[d].rdev;
1370 }
1371 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001372 }
1373 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001374 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001375 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001376 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1377 wait_barrier(conf);
1378 goto retry_write;
1379 }
1380
NeilBrown6b6c8112017-03-15 14:05:13 +11001381 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001382 r10_bio->sectors = max_sectors;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001383 sectors_handled = r10_bio->sector + max_sectors -
1384 bio->bi_iter.bi_sector;
NeilBrownd4432c22011-07-28 11:39:24 +10001385
NeilBrown4e780642010-10-19 12:54:01 +11001386 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001387 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 for (i = 0; i < conf->copies; i++) {
1390 struct bio *mbio;
1391 int d = r10_bio->devs[i].devnum;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001392 if (r10_bio->devs[i].bio) {
1393 struct md_rdev *rdev = conf->mirrors[d].rdev;
Ming Leid7a10302017-02-14 23:29:03 +08001394 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001395 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001396 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001397 r10_bio->devs[i].bio = mbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Kent Overstreet4f024f32013-10-11 15:44:27 -07001399 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001400 choose_data_offset(r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001401 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001402 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001403 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrown1919cbb2016-11-18 16:16:12 +11001404 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags) &&
1405 enough(conf, d))
1406 mbio->bi_opf |= MD_FAILFAST;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001407 mbio->bi_private = r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
NeilBrown109e3762016-11-18 13:22:04 +11001409 if (conf->mddev->gendisk)
1410 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1411 mbio, disk_devt(conf->mddev->gendisk),
1412 r10_bio->sector);
1413 /* flush_pending_writes() needs access to the rdev so...*/
1414 mbio->bi_bdev = (void*)rdev;
1415
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001416 atomic_inc(&r10_bio->remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001418 cb = blk_check_plugged(raid10_unplug, mddev,
1419 sizeof(*plug));
1420 if (cb)
1421 plug = container_of(cb, struct raid10_plug_cb,
1422 cb);
1423 else
1424 plug = NULL;
1425 spin_lock_irqsave(&conf->device_lock, flags);
1426 if (plug) {
1427 bio_list_add(&plug->pending, mbio);
1428 plug->pending_cnt++;
1429 } else {
1430 bio_list_add(&conf->pending_bio_list, mbio);
1431 conf->pending_count++;
1432 }
1433 spin_unlock_irqrestore(&conf->device_lock, flags);
1434 if (!plug)
1435 md_wakeup_thread(mddev->thread);
1436 }
NeilBrown57c67df2012-10-11 13:32:13 +11001437
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001438 if (r10_bio->devs[i].repl_bio) {
1439 struct md_rdev *rdev = conf->mirrors[d].replacement;
1440 if (rdev == NULL) {
1441 /* Replacement just got moved to main 'rdev' */
1442 smp_mb();
1443 rdev = conf->mirrors[d].rdev;
1444 }
Ming Leid7a10302017-02-14 23:29:03 +08001445 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001446 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001447 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001448 r10_bio->devs[i].repl_bio = mbio;
1449
Kent Overstreet4f024f32013-10-11 15:44:27 -07001450 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001451 choose_data_offset(r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001452 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001453 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001454 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001455 mbio->bi_private = r10_bio;
1456
NeilBrown109e3762016-11-18 13:22:04 +11001457 if (conf->mddev->gendisk)
1458 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1459 mbio, disk_devt(conf->mddev->gendisk),
1460 r10_bio->sector);
1461 /* flush_pending_writes() needs access to the rdev so...*/
1462 mbio->bi_bdev = (void*)rdev;
1463
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001464 atomic_inc(&r10_bio->remaining);
Shaohua Li6d399782017-02-23 12:26:41 -08001465
1466 cb = blk_check_plugged(raid10_unplug, mddev,
1467 sizeof(*plug));
1468 if (cb)
1469 plug = container_of(cb, struct raid10_plug_cb,
1470 cb);
1471 else
1472 plug = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001473 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li6d399782017-02-23 12:26:41 -08001474 if (plug) {
1475 bio_list_add(&plug->pending, mbio);
1476 plug->pending_cnt++;
1477 } else {
1478 bio_list_add(&conf->pending_bio_list, mbio);
1479 conf->pending_count++;
1480 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001481 spin_unlock_irqrestore(&conf->device_lock, flags);
Shaohua Li6d399782017-02-23 12:26:41 -08001482 if (!plug)
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001483 md_wakeup_thread(mddev->thread);
NeilBrown57c67df2012-10-11 13:32:13 +11001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
NeilBrown079fa162011-09-10 17:21:23 +10001487 /* Don't remove the bias on 'remaining' (one_write_done) until
1488 * after checking if we need to go around again.
1489 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001490
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001491 if (sectors_handled < bio_sectors(bio)) {
NeilBrownfd16f2e2017-03-15 14:05:13 +11001492 /* We need another r10_bio and it needs to be counted */
1493 inc_pending(conf);
1494 bio_inc_remaining(bio);
NeilBrown6b6c8112017-03-15 14:05:13 +11001495 one_write_done(r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001496 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1497
1498 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001499 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001500
1501 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001502 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001503 r10_bio->state = 0;
1504 goto retry_write;
1505 }
NeilBrown079fa162011-09-10 17:21:23 +10001506 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001507}
1508
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001509static void __make_request(struct mddev *mddev, struct bio *bio)
1510{
1511 struct r10conf *conf = mddev->private;
1512 struct r10bio *r10_bio;
1513
1514 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1515
1516 r10_bio->master_bio = bio;
1517 r10_bio->sectors = bio_sectors(bio);
1518
1519 r10_bio->mddev = mddev;
1520 r10_bio->sector = bio->bi_iter.bi_sector;
1521 r10_bio->state = 0;
1522
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001523 if (bio_data_dir(bio) == READ)
1524 raid10_read_request(mddev, bio, r10_bio);
1525 else
1526 raid10_write_request(mddev, bio, r10_bio);
1527}
1528
Shaohua Li849674e2016-01-20 13:52:20 -08001529static void raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001530{
1531 struct r10conf *conf = mddev->private;
1532 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1533 int chunk_sects = chunk_mask + 1;
1534
1535 struct bio *split;
1536
Jens Axboe1eff9d32016-08-05 15:35:16 -06001537 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001538 md_flush_request(mddev, bio);
1539 return;
1540 }
1541
Kent Overstreet20d01892013-11-23 18:21:01 -08001542 do {
1543
1544 /*
1545 * If this request crosses a chunk boundary, we need to split
1546 * it.
1547 */
1548 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1549 bio_sectors(bio) > chunk_sects
1550 && (conf->geo.near_copies < conf->geo.raid_disks
1551 || conf->prev.near_copies <
1552 conf->prev.raid_disks))) {
1553 split = bio_split(bio, chunk_sects -
1554 (bio->bi_iter.bi_sector &
1555 (chunk_sects - 1)),
1556 GFP_NOIO, fs_bio_set);
1557 bio_chain(split, bio);
1558 } else {
1559 split = bio;
1560 }
1561
Shaohua Li61eb2b42017-02-28 13:00:20 -08001562 /*
1563 * If a bio is splitted, the first part of bio will pass
1564 * barrier but the bio is queued in current->bio_list (see
1565 * generic_make_request). If there is a raise_barrier() called
1566 * here, the second part of bio can't pass barrier. But since
1567 * the first part bio isn't dispatched to underlaying disks
1568 * yet, the barrier is never released, hence raise_barrier will
1569 * alays wait. We have a deadlock.
1570 * Note, this only happens in read path. For write path, the
1571 * first part of bio is dispatched in a schedule() call
1572 * (because of blk plug) or offloaded to raid10d.
1573 * Quitting from the function immediately can change the bio
1574 * order queued in bio_list and avoid the deadlock.
1575 */
Kent Overstreet20d01892013-11-23 18:21:01 -08001576 __make_request(mddev, split);
Shaohua Li61eb2b42017-02-28 13:00:20 -08001577 if (split != bio && bio_data_dir(bio) == READ) {
1578 generic_make_request(bio);
1579 break;
1580 }
Kent Overstreet20d01892013-11-23 18:21:01 -08001581 } while (split != bio);
NeilBrown079fa162011-09-10 17:21:23 +10001582
1583 /* In case raid10d snuck in to freeze_array */
1584 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Shaohua Li849674e2016-01-20 13:52:20 -08001587static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
NeilBrowne879a872011-10-11 16:49:02 +11001589 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int i;
1591
NeilBrown5cf00fc2012-05-21 09:28:20 +10001592 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001593 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001594 if (conf->geo.near_copies > 1)
1595 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1596 if (conf->geo.far_copies > 1) {
1597 if (conf->geo.far_offset)
1598 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001599 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001600 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001601 if (conf->geo.far_set_size != conf->geo.raid_disks)
1602 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001603 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001604 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1605 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001606 rcu_read_lock();
1607 for (i = 0; i < conf->geo.raid_disks; i++) {
1608 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1609 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1610 }
1611 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 seq_printf(seq, "]");
1613}
1614
NeilBrown700c7212011-07-27 11:00:36 +10001615/* check if there are enough drives for
1616 * every block to appear on atleast one.
1617 * Don't consider the device numbered 'ignore'
1618 * as we might be about to remove it.
1619 */
NeilBrown635f6412013-06-11 14:57:09 +10001620static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001621{
1622 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001623 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001624 int disks, ncopies;
1625 if (previous) {
1626 disks = conf->prev.raid_disks;
1627 ncopies = conf->prev.near_copies;
1628 } else {
1629 disks = conf->geo.raid_disks;
1630 ncopies = conf->geo.near_copies;
1631 }
NeilBrown700c7212011-07-27 11:00:36 +10001632
NeilBrown725d6e52013-06-11 15:08:03 +10001633 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001634 do {
1635 int n = conf->copies;
1636 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001637 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001638 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001639 struct md_rdev *rdev;
1640 if (this != ignore &&
1641 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1642 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001643 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001644 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001645 }
1646 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001647 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001648 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001649 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001650 has_enough = 1;
1651out:
1652 rcu_read_unlock();
1653 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001654}
1655
NeilBrownf8c9e742012-05-21 09:28:33 +10001656static int enough(struct r10conf *conf, int ignore)
1657{
NeilBrown635f6412013-06-11 14:57:09 +10001658 /* when calling 'enough', both 'prev' and 'geo' must
1659 * be stable.
1660 * This is ensured if ->reconfig_mutex or ->device_lock
1661 * is held.
1662 */
1663 return _enough(conf, 0, ignore) &&
1664 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001665}
1666
Shaohua Li849674e2016-01-20 13:52:20 -08001667static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001670 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001671 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /*
1674 * If it is not operational, then we have already marked it as dead
1675 * else if it is the last working disks, ignore the error, let the
1676 * next level up know.
1677 * else mark the drive as failed
1678 */
NeilBrown635f6412013-06-11 14:57:09 +10001679 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001680 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001681 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /*
1683 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001685 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001686 return;
1687 }
NeilBrown2446dba2014-07-31 10:16:29 +10001688 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001689 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001690 /*
1691 * If recovery is running, make sure it aborts.
1692 */
1693 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001694 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001695 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001696 set_mask_bits(&mddev->sb_flags, 0,
1697 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001698 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001699 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1700 "md/raid10:%s: Operation continuing on %d devices.\n",
1701 mdname(mddev), bdevname(rdev->bdev, b),
1702 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
NeilBrowne879a872011-10-11 16:49:02 +11001705static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001708 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
NeilBrown08464e02016-11-02 14:16:50 +11001710 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001712 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return;
1714 }
NeilBrown08464e02016-11-02 14:16:50 +11001715 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1716 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
NeilBrown4056ca52016-06-02 16:19:52 +10001718 /* This is only called with ->reconfix_mutex held, so
1719 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001720 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001722 rdev = conf->mirrors[i].rdev;
1723 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001724 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1725 i, !test_bit(In_sync, &rdev->flags),
1726 !test_bit(Faulty, &rdev->flags),
1727 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729}
1730
NeilBrowne879a872011-10-11 16:49:02 +11001731static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
NeilBrown0a27ec92006-01-06 00:20:13 -08001733 wait_barrier(conf);
1734 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 mempool_destroy(conf->r10buf_pool);
1737 conf->r10buf_pool = NULL;
1738}
1739
NeilBrownfd01b882011-10-11 16:47:53 +11001740static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001743 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001744 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001745 int count = 0;
1746 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /*
1749 * Find all non-in_sync disks within the RAID10 configuration
1750 * and mark them in_sync
1751 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001752 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001754 if (tmp->replacement
1755 && tmp->replacement->recovery_offset == MaxSector
1756 && !test_bit(Faulty, &tmp->replacement->flags)
1757 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1758 /* Replacement has just become active */
1759 if (!tmp->rdev
1760 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1761 count++;
1762 if (tmp->rdev) {
1763 /* Replaced device not technically faulty,
1764 * but we need to be sure it gets removed
1765 * and never re-added.
1766 */
1767 set_bit(Faulty, &tmp->rdev->flags);
1768 sysfs_notify_dirent_safe(
1769 tmp->rdev->sysfs_state);
1770 }
1771 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1772 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001773 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001774 && !test_bit(Faulty, &tmp->rdev->flags)
1775 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001776 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001777 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779 }
NeilBrown6b965622010-08-18 11:56:59 +10001780 spin_lock_irqsave(&conf->device_lock, flags);
1781 mddev->degraded -= count;
1782 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001785 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
NeilBrownfd01b882011-10-11 16:47:53 +11001788static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
NeilBrowne879a872011-10-11 16:49:02 +11001790 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001791 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001793 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001794 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (mddev->recovery_cp < MaxSector)
1797 /* only hot-add to in-sync arrays, as recovery is
1798 * very different from resync
1799 */
Neil Brown199050e2008-06-28 08:31:33 +10001800 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001801 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001802 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Dan Williams1501efa2016-01-13 16:00:07 -08001804 if (md_integrity_add_rdev(rdev, mddev))
1805 return -ENXIO;
1806
NeilBrowna53a6c82008-11-06 17:28:20 +11001807 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001808 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001810 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001811 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1812 mirror = rdev->saved_raid_disk;
1813 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001814 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001815 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001816 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001817 if (p->recovery_disabled == mddev->recovery_disabled)
1818 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001819 if (p->rdev) {
1820 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1821 p->replacement != NULL)
1822 continue;
1823 clear_bit(In_sync, &rdev->flags);
1824 set_bit(Replacement, &rdev->flags);
1825 rdev->raid_disk = mirror;
1826 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001827 if (mddev->gendisk)
1828 disk_stack_limits(mddev->gendisk, rdev->bdev,
1829 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001830 conf->fullsync = 1;
1831 rcu_assign_pointer(p->replacement, rdev);
1832 break;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jonathan Brassow9092c022013-05-02 14:19:24 -05001835 if (mddev->gendisk)
1836 disk_stack_limits(mddev->gendisk, rdev->bdev,
1837 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
NeilBrown2bb77732011-07-27 11:00:36 +10001839 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001840 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001841 rdev->raid_disk = mirror;
1842 err = 0;
1843 if (rdev->saved_raid_disk != mirror)
1844 conf->fullsync = 1;
1845 rcu_assign_pointer(p->rdev, rdev);
1846 break;
1847 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11001848 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001849 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
NeilBrownb8321b62011-12-23 10:17:51 +11001855static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
NeilBrowne879a872011-10-11 16:49:02 +11001857 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001859 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001860 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001861 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001864 if (rdev == p->rdev)
1865 rdevp = &p->rdev;
1866 else if (rdev == p->replacement)
1867 rdevp = &p->replacement;
1868 else
1869 return 0;
1870
1871 if (test_bit(In_sync, &rdev->flags) ||
1872 atomic_read(&rdev->nr_pending)) {
1873 err = -EBUSY;
1874 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
NeilBrownd787be42016-06-02 16:19:53 +10001876 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11001877 * is not possible.
1878 */
1879 if (!test_bit(Faulty, &rdev->flags) &&
1880 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001881 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001882 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001883 enough(conf, -1)) {
1884 err = -EBUSY;
1885 goto abort;
1886 }
1887 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001888 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1889 synchronize_rcu();
1890 if (atomic_read(&rdev->nr_pending)) {
1891 /* lost the race, try later */
1892 err = -EBUSY;
1893 *rdevp = rdev;
1894 goto abort;
1895 }
1896 }
1897 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001898 /* We must have just cleared 'rdev' */
1899 p->rdev = p->replacement;
1900 clear_bit(Replacement, &p->replacement->flags);
1901 smp_mb(); /* Make sure other CPUs may see both as identical
1902 * but will never see neither -- if they are careful.
1903 */
1904 p->replacement = NULL;
1905 clear_bit(WantReplacement, &rdev->flags);
1906 } else
1907 /* We might have just remove the Replacement as faulty
1908 * Clear the flag just in case
1909 */
1910 clear_bit(WantReplacement, &rdev->flags);
1911
NeilBrownc8ab9032011-12-23 10:17:54 +11001912 err = md_integrity_register(mddev);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914abort:
1915
1916 print_conf(conf);
1917 return err;
1918}
1919
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001920static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001922 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001923 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001924 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
NeilBrown3ea7daa2012-05-22 13:53:47 +10001926 if (bio == r10_bio->master_bio) {
1927 /* this is a reshape read */
1928 d = r10_bio->read_slot; /* really the read dev */
1929 } else
1930 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001931
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001932 if (!bio->bi_error)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001933 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001934 else
1935 /* The write handler will notice the lack of
1936 * R10BIO_Uptodate and record any errors etc
1937 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001938 atomic_add(r10_bio->sectors,
1939 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /* for reconstruct, we always reschedule after a read.
1942 * for resync, only after all reads
1943 */
NeilBrown73d5c382009-02-25 13:18:47 +11001944 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1946 atomic_dec_and_test(&r10_bio->remaining)) {
1947 /* we have read all the blocks,
1948 * do the comparison in process context in raid10d
1949 */
1950 reschedule_retry(r10_bio);
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
NeilBrown9f2c9d12011-10-11 16:48:43 +11001954static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001955{
NeilBrownfd01b882011-10-11 16:47:53 +11001956 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001957
1958 while (atomic_dec_and_test(&r10_bio->remaining)) {
1959 if (r10_bio->master_bio == NULL) {
1960 /* the primary of several recovery bios */
1961 sector_t s = r10_bio->sectors;
1962 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1963 test_bit(R10BIO_WriteError, &r10_bio->state))
1964 reschedule_retry(r10_bio);
1965 else
1966 put_buf(r10_bio);
1967 md_done_sync(mddev, s, 1);
1968 break;
1969 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001970 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001971 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1972 test_bit(R10BIO_WriteError, &r10_bio->state))
1973 reschedule_retry(r10_bio);
1974 else
1975 put_buf(r10_bio);
1976 r10_bio = r10_bio2;
1977 }
1978 }
1979}
1980
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001981static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001983 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001984 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001985 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001986 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001987 sector_t first_bad;
1988 int bad_sectors;
1989 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001990 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001991 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
NeilBrown9ad1aef2011-12-23 10:17:55 +11001993 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1994 if (repl)
1995 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001996 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001997 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001999 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002000 if (repl)
2001 md_error(mddev, rdev);
2002 else {
2003 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002004 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2005 set_bit(MD_RECOVERY_NEEDED,
2006 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002007 set_bit(R10BIO_WriteError, &r10_bio->state);
2008 }
2009 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10002010 r10_bio->devs[slot].addr,
2011 r10_bio->sectors,
2012 &first_bad, &bad_sectors))
2013 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07002014
NeilBrown9ad1aef2011-12-23 10:17:55 +11002015 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002016
2017 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
2020/*
2021 * Note: sync and recover and handled very differently for raid10
2022 * This code is for resync.
2023 * For resync, we read through virtual addresses and read all blocks.
2024 * If there is any error, we schedule a write. The lowest numbered
2025 * drive is authoritative.
2026 * However requests come for physical address, so we need to map.
2027 * For every physical address there are raid_disks/copies virtual addresses,
2028 * which is always are least one, but is not necessarly an integer.
2029 * This means that a physical address can span multiple chunks, so we may
2030 * have to submit multiple io requests for a single sync request.
2031 */
2032/*
2033 * We check if all blocks are in-sync and only write to blocks that
2034 * aren't in sync
2035 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002036static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
NeilBrowne879a872011-10-11 16:49:02 +11002038 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int i, first;
2040 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002041 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 atomic_set(&r10_bio->remaining, 1);
2044
2045 /* find the first device with a block */
2046 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002047 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 break;
2049
2050 if (i == conf->copies)
2051 goto done;
2052
2053 first = i;
2054 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002055 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2056 fbio->bi_iter.bi_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
majianpengf4380a92012-04-12 16:04:47 +10002058 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002060 for (i=0 ; i < conf->copies ; i++) {
2061 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002062 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002065
2066 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002068 if (i == first)
2069 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002070 d = r10_bio->devs[i].devnum;
2071 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002072 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002073 /* We know that the bi_io_vec layout is the same for
2074 * both 'first' and 'i', so we just compare them.
2075 * All vec entries are PAGE_SIZE;
2076 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002077 int sectors = r10_bio->sectors;
2078 for (j = 0; j < vcnt; j++) {
2079 int len = PAGE_SIZE;
2080 if (sectors < (len / 512))
2081 len = sectors * 512;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002082 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2083 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown7bb23c42013-07-16 16:50:47 +10002084 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002085 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002086 sectors -= len/512;
2087 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002088 if (j == vcnt)
2089 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002090 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002091 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2092 /* Don't fix anything. */
2093 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002094 } else if (test_bit(FailFast, &rdev->flags)) {
2095 /* Just give up on this device */
2096 md_error(rdev->mddev, rdev);
2097 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002098 }
NeilBrownf84ee362011-07-28 11:39:25 +10002099 /* Ok, we need to write this bio, either to correct an
2100 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * First we need to fixup bv_offset, bv_len and
2102 * bi_vecs, as the read request might have corrupted these
2103 */
Kent Overstreet8be185f2012-09-06 14:14:43 -07002104 bio_reset(tbio);
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 tbio->bi_vcnt = vcnt;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002107 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 tbio->bi_private = r10_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002109 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002111 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Kent Overstreetc31df252015-05-06 23:34:20 -07002113 bio_copy_data(tbio, fbio);
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2116 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002117 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
NeilBrown1919cbb2016-11-18 16:16:12 +11002119 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2120 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002121 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2123 generic_make_request(tbio);
2124 }
2125
NeilBrown9ad1aef2011-12-23 10:17:55 +11002126 /* Now write out to any replacement devices
2127 * that are active
2128 */
2129 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002130 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002131
2132 tbio = r10_bio->devs[i].repl_bio;
2133 if (!tbio || !tbio->bi_end_io)
2134 continue;
2135 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2136 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002137 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002138 d = r10_bio->devs[i].devnum;
2139 atomic_inc(&r10_bio->remaining);
2140 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002141 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002142 generic_make_request(tbio);
2143 }
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145done:
2146 if (atomic_dec_and_test(&r10_bio->remaining)) {
2147 md_done_sync(mddev, r10_bio->sectors, 1);
2148 put_buf(r10_bio);
2149 }
2150}
2151
2152/*
2153 * Now for the recovery code.
2154 * Recovery happens across physical sectors.
2155 * We recover all non-is_sync drives by finding the virtual address of
2156 * each, and then choose a working drive that also has that virt address.
2157 * There is a separate r10_bio for each non-in_sync drive.
2158 * Only the first two slots are in use. The first for reading,
2159 * The second for writing.
2160 *
2161 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002162static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002163{
2164 /* We got a read error during recovery.
2165 * We repeat the read in smaller page-sized sections.
2166 * If a read succeeds, write it to the new device or record
2167 * a bad block if we cannot.
2168 * If a read fails, record a bad block on both old and
2169 * new devices.
2170 */
NeilBrownfd01b882011-10-11 16:47:53 +11002171 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002172 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002173 struct bio *bio = r10_bio->devs[0].bio;
2174 sector_t sect = 0;
2175 int sectors = r10_bio->sectors;
2176 int idx = 0;
2177 int dr = r10_bio->devs[0].devnum;
2178 int dw = r10_bio->devs[1].devnum;
2179
2180 while (sectors) {
2181 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002182 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002183 sector_t addr;
2184 int ok;
2185
2186 if (s > (PAGE_SIZE>>9))
2187 s = PAGE_SIZE >> 9;
2188
2189 rdev = conf->mirrors[dr].rdev;
2190 addr = r10_bio->devs[0].addr + sect,
2191 ok = sync_page_io(rdev,
2192 addr,
2193 s << 9,
2194 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002195 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002196 if (ok) {
2197 rdev = conf->mirrors[dw].rdev;
2198 addr = r10_bio->devs[1].addr + sect;
2199 ok = sync_page_io(rdev,
2200 addr,
2201 s << 9,
2202 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002203 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002204 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002205 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002206 if (!test_and_set_bit(WantReplacement,
2207 &rdev->flags))
2208 set_bit(MD_RECOVERY_NEEDED,
2209 &rdev->mddev->recovery);
2210 }
NeilBrown5e570282011-07-28 11:39:25 +10002211 }
2212 if (!ok) {
2213 /* We don't worry if we cannot set a bad block -
2214 * it really is bad so there is no loss in not
2215 * recording it yet
2216 */
2217 rdev_set_badblocks(rdev, addr, s, 0);
2218
2219 if (rdev != conf->mirrors[dw].rdev) {
2220 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002221 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002222 addr = r10_bio->devs[1].addr + sect;
2223 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2224 if (!ok) {
2225 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002226 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2227 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002228
2229 conf->mirrors[dw].recovery_disabled
2230 = mddev->recovery_disabled;
2231 set_bit(MD_RECOVERY_INTR,
2232 &mddev->recovery);
2233 break;
2234 }
2235 }
2236 }
2237
2238 sectors -= s;
2239 sect += s;
2240 idx++;
2241 }
2242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
NeilBrown9f2c9d12011-10-11 16:48:43 +11002244static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
NeilBrowne879a872011-10-11 16:49:02 +11002246 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002247 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002248 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
NeilBrown5e570282011-07-28 11:39:25 +10002250 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2251 fix_recovery_read_error(r10_bio);
2252 end_sync_request(r10_bio);
2253 return;
2254 }
2255
Namhyung Kimc65060a2011-07-18 17:38:49 +10002256 /*
2257 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * and submit the write request
2259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002261 wbio = r10_bio->devs[1].bio;
2262 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002263 /* Need to test wbio2->bi_end_io before we call
2264 * generic_make_request as if the former is NULL,
2265 * the latter is free to free wbio2.
2266 */
2267 if (wbio2 && !wbio2->bi_end_io)
2268 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002269 if (wbio->bi_end_io) {
2270 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002271 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002272 generic_make_request(wbio);
2273 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002274 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002275 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2276 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002277 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002278 generic_make_request(wbio2);
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/*
Robert Becker1e509152009-12-14 12:49:58 +11002283 * Used by fix_read_error() to decay the per rdev read_errors.
2284 * We halve the read error count for every hour that has elapsed
2285 * since the last recorded read error.
2286 *
2287 */
NeilBrownfd01b882011-10-11 16:47:53 +11002288static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002289{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002290 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002291 unsigned long hours_since_last;
2292 unsigned int read_errors = atomic_read(&rdev->read_errors);
2293
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002294 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002295
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002296 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002297 /* first time we've seen a read error */
2298 rdev->last_read_error = cur_time_mon;
2299 return;
2300 }
2301
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002302 hours_since_last = (long)(cur_time_mon -
2303 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002304
2305 rdev->last_read_error = cur_time_mon;
2306
2307 /*
2308 * if hours_since_last is > the number of bits in read_errors
2309 * just set read errors to 0. We do this to avoid
2310 * overflowing the shift of read_errors by hours_since_last.
2311 */
2312 if (hours_since_last >= 8 * sizeof(read_errors))
2313 atomic_set(&rdev->read_errors, 0);
2314 else
2315 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2316}
2317
NeilBrown3cb03002011-10-11 16:45:26 +11002318static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002319 int sectors, struct page *page, int rw)
2320{
2321 sector_t first_bad;
2322 int bad_sectors;
2323
2324 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2325 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2326 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002327 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002328 /* success */
2329 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002330 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002331 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002332 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2333 set_bit(MD_RECOVERY_NEEDED,
2334 &rdev->mddev->recovery);
2335 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002336 /* need to record an error - either for the block or the device */
2337 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2338 md_error(rdev->mddev, rdev);
2339 return 0;
2340}
2341
Robert Becker1e509152009-12-14 12:49:58 +11002342/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 * This is a kernel thread which:
2344 *
2345 * 1. Retries failed read operations on working mirrors.
2346 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002347 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 */
2349
NeilBrowne879a872011-10-11 16:49:02 +11002350static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002351{
2352 int sect = 0; /* Offset from r10_bio->sector */
2353 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002354 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002355 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002356 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002357
NeilBrown7c4e06f2011-05-11 14:53:17 +10002358 /* still own a reference to this rdev, so it cannot
2359 * have been cleared recently.
2360 */
2361 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002362
NeilBrown7c4e06f2011-05-11 14:53:17 +10002363 if (test_bit(Faulty, &rdev->flags))
2364 /* drive has already been failed, just ignore any
2365 more fix_read_error() attempts */
2366 return;
2367
2368 check_decay_read_errors(mddev, rdev);
2369 atomic_inc(&rdev->read_errors);
2370 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2371 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002372 bdevname(rdev->bdev, b);
2373
NeilBrown08464e02016-11-02 14:16:50 +11002374 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2375 mdname(mddev), b,
2376 atomic_read(&rdev->read_errors), max_read_errors);
2377 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2378 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002379 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002380 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002381 return;
Robert Becker1e509152009-12-14 12:49:58 +11002382 }
Robert Becker1e509152009-12-14 12:49:58 +11002383
NeilBrown6814d532006-10-03 01:15:45 -07002384 while(sectors) {
2385 int s = sectors;
2386 int sl = r10_bio->read_slot;
2387 int success = 0;
2388 int start;
2389
2390 if (s > (PAGE_SIZE>>9))
2391 s = PAGE_SIZE >> 9;
2392
2393 rcu_read_lock();
2394 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002395 sector_t first_bad;
2396 int bad_sectors;
2397
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002398 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002399 rdev = rcu_dereference(conf->mirrors[d].rdev);
2400 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002401 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002402 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002403 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2404 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002405 atomic_inc(&rdev->nr_pending);
2406 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002407 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002408 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002409 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002410 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002411 conf->tmppage,
2412 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002413 rdev_dec_pending(rdev, mddev);
2414 rcu_read_lock();
2415 if (success)
2416 break;
2417 }
2418 sl++;
2419 if (sl == conf->copies)
2420 sl = 0;
2421 } while (!success && sl != r10_bio->read_slot);
2422 rcu_read_unlock();
2423
2424 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002425 /* Cannot read from anywhere, just mark the block
2426 * as bad on the first device to discourage future
2427 * reads.
2428 */
NeilBrown6814d532006-10-03 01:15:45 -07002429 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002430 rdev = conf->mirrors[dn].rdev;
2431
2432 if (!rdev_set_badblocks(
2433 rdev,
2434 r10_bio->devs[r10_bio->read_slot].addr
2435 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002436 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002437 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002438 r10_bio->devs[r10_bio->read_slot].bio
2439 = IO_BLOCKED;
2440 }
NeilBrown6814d532006-10-03 01:15:45 -07002441 break;
2442 }
2443
2444 start = sl;
2445 /* write it back and re-read */
2446 rcu_read_lock();
2447 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002448 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002449
NeilBrown6814d532006-10-03 01:15:45 -07002450 if (sl==0)
2451 sl = conf->copies;
2452 sl--;
2453 d = r10_bio->devs[sl].devnum;
2454 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002455 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002456 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002457 !test_bit(In_sync, &rdev->flags))
2458 continue;
2459
2460 atomic_inc(&rdev->nr_pending);
2461 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002462 if (r10_sync_page_io(rdev,
2463 r10_bio->devs[sl].addr +
2464 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002465 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002466 == 0) {
2467 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002468 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2469 mdname(mddev), s,
2470 (unsigned long long)(
2471 sect +
2472 choose_data_offset(r10_bio,
2473 rdev)),
2474 bdevname(rdev->bdev, b));
2475 pr_notice("md/raid10:%s: %s: failing drive\n",
2476 mdname(mddev),
2477 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002478 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002479 rdev_dec_pending(rdev, mddev);
2480 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002481 }
2482 sl = start;
2483 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002484 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002485
NeilBrown6814d532006-10-03 01:15:45 -07002486 if (sl==0)
2487 sl = conf->copies;
2488 sl--;
2489 d = r10_bio->devs[sl].devnum;
2490 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002491 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002492 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002493 !test_bit(In_sync, &rdev->flags))
2494 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002495
NeilBrown1294b9c2011-07-28 11:39:23 +10002496 atomic_inc(&rdev->nr_pending);
2497 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002498 switch (r10_sync_page_io(rdev,
2499 r10_bio->devs[sl].addr +
2500 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002501 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002502 READ)) {
2503 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002504 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002505 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002506 mdname(mddev), s,
2507 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002508 sect +
2509 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002510 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002511 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002512 mdname(mddev),
2513 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002514 break;
2515 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002516 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002517 mdname(mddev), s,
2518 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002519 sect +
2520 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002521 bdevname(rdev->bdev, b));
2522 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002523 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002524
2525 rdev_dec_pending(rdev, mddev);
2526 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002527 }
2528 rcu_read_unlock();
2529
2530 sectors -= s;
2531 sect += s;
2532 }
2533}
2534
NeilBrown9f2c9d12011-10-11 16:48:43 +11002535static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002536{
2537 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002538 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002539 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002540 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002541 /* bio has the data to be written to slot 'i' where
2542 * we just recently had a write error.
2543 * We repeatedly clone the bio and trim down to one block,
2544 * then try the write. Where the write fails we record
2545 * a bad block.
2546 * It is conceivable that the bio doesn't exactly align with
2547 * blocks. We must handle this.
2548 *
2549 * We currently own a reference to the rdev.
2550 */
2551
2552 int block_sectors;
2553 sector_t sector;
2554 int sectors;
2555 int sect_to_write = r10_bio->sectors;
2556 int ok = 1;
2557
2558 if (rdev->badblocks.shift < 0)
2559 return 0;
2560
NeilBrownf04ebb02015-02-16 14:51:54 +11002561 block_sectors = roundup(1 << rdev->badblocks.shift,
2562 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002563 sector = r10_bio->sector;
2564 sectors = ((r10_bio->sector + block_sectors)
2565 & ~(sector_t)(block_sectors - 1))
2566 - sector;
2567
2568 while (sect_to_write) {
2569 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002570 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002571 if (sectors > sect_to_write)
2572 sectors = sect_to_write;
2573 /* Write at 'sector' for 'sectors' */
Ming Leid7a10302017-02-14 23:29:03 +08002574 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002575 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002576 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2577 wbio->bi_iter.bi_sector = wsector +
2578 choose_data_offset(r10_bio, rdev);
NeilBrownbd870a12011-07-28 11:39:24 +10002579 wbio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002580 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002581
2582 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002583 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002584 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002585 sectors, 0)
2586 && ok;
2587
2588 bio_put(wbio);
2589 sect_to_write -= sectors;
2590 sector += sectors;
2591 sectors = block_sectors;
2592 }
2593 return ok;
2594}
2595
NeilBrown9f2c9d12011-10-11 16:48:43 +11002596static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002597{
2598 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002599 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002600 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002601 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002602 char b[BDEVNAME_SIZE];
2603 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002604 int max_sectors;
NeilBrown109e3762016-11-18 13:22:04 +11002605 dev_t bio_dev;
2606 sector_t bio_last_sector;
NeilBrown560f8e52011-07-28 11:39:23 +10002607
2608 /* we got a read error. Maybe the drive is bad. Maybe just
2609 * the block and we can fix it.
2610 * We freeze all other IO, and try reading the block from
2611 * other devices. When we find one, we re-write
2612 * and check it that fixes the read error.
2613 * This is all done synchronously while the array is
2614 * frozen.
2615 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002616 bio = r10_bio->devs[slot].bio;
2617 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002618 bio_dev = bio->bi_bdev->bd_dev;
2619 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002620 bio_put(bio);
2621 r10_bio->devs[slot].bio = NULL;
2622
NeilBrown8d3ca832016-11-18 16:16:12 +11002623 if (mddev->ro)
2624 r10_bio->devs[slot].bio = IO_BLOCKED;
2625 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002626 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002627 fix_read_error(conf, mddev, r10_bio);
2628 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002629 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002630 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002631
NeilBrownabbf0982011-12-23 10:17:54 +11002632 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002633
NeilBrown7399c312011-07-28 11:39:23 +10002634read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002635 rdev = read_balance(conf, r10_bio, &max_sectors);
2636 if (rdev == NULL) {
NeilBrown08464e02016-11-02 14:16:50 +11002637 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2638 mdname(mddev), b,
2639 (unsigned long long)r10_bio->sector);
NeilBrown560f8e52011-07-28 11:39:23 +10002640 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002641 return;
2642 }
2643
Jens Axboe1eff9d32016-08-05 15:35:16 -06002644 do_sync = (r10_bio->master_bio->bi_opf & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002645 slot = r10_bio->read_slot;
NeilBrown08464e02016-11-02 14:16:50 +11002646 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2647 mdname(mddev),
2648 bdevname(rdev->bdev, b),
2649 (unsigned long long)r10_bio->sector);
Ming Leid7a10302017-02-14 23:29:03 +08002650 bio = bio_clone_fast(r10_bio->master_bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002651 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002652 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002653 r10_bio->devs[slot].rdev = rdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002654 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002655 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002656 bio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002657 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown8d3ca832016-11-18 16:16:12 +11002658 if (test_bit(FailFast, &rdev->flags) &&
2659 test_bit(R10BIO_FailFast, &r10_bio->state))
2660 bio->bi_opf |= MD_FAILFAST;
NeilBrown560f8e52011-07-28 11:39:23 +10002661 bio->bi_private = r10_bio;
2662 bio->bi_end_io = raid10_end_read_request;
NeilBrown109e3762016-11-18 13:22:04 +11002663 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2664 bio, bio_dev,
2665 bio_last_sector - r10_bio->sectors);
2666
NeilBrown7399c312011-07-28 11:39:23 +10002667 if (max_sectors < r10_bio->sectors) {
2668 /* Drat - have to split this up more */
2669 struct bio *mbio = r10_bio->master_bio;
2670 int sectors_handled =
2671 r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002672 - mbio->bi_iter.bi_sector;
NeilBrown7399c312011-07-28 11:39:23 +10002673 r10_bio->sectors = max_sectors;
NeilBrownfd16f2e2017-03-15 14:05:13 +11002674 bio_inc_remaining(mbio);
2675 inc_pending(conf);
NeilBrown7399c312011-07-28 11:39:23 +10002676 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002677
2678 r10_bio = mempool_alloc(conf->r10bio_pool,
2679 GFP_NOIO);
2680 r10_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002681 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown7399c312011-07-28 11:39:23 +10002682 r10_bio->state = 0;
2683 set_bit(R10BIO_ReadError,
2684 &r10_bio->state);
2685 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002686 r10_bio->sector = mbio->bi_iter.bi_sector
NeilBrown7399c312011-07-28 11:39:23 +10002687 + sectors_handled;
2688
2689 goto read_more;
2690 } else
2691 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002692}
2693
NeilBrowne879a872011-10-11 16:49:02 +11002694static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002695{
2696 /* Some sort of write request has finished and it
2697 * succeeded in writing where we thought there was a
2698 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002699 * Or possibly if failed and we need to record
2700 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002701 */
2702 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002703 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002704
2705 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2706 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002707 for (m = 0; m < conf->copies; m++) {
2708 int dev = r10_bio->devs[m].devnum;
2709 rdev = conf->mirrors[dev].rdev;
2710 if (r10_bio->devs[m].bio == NULL)
2711 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002712 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002713 rdev_clear_badblocks(
2714 rdev,
2715 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002716 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002717 } else {
2718 if (!rdev_set_badblocks(
2719 rdev,
2720 r10_bio->devs[m].addr,
2721 r10_bio->sectors, 0))
2722 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002723 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002724 rdev = conf->mirrors[dev].replacement;
2725 if (r10_bio->devs[m].repl_bio == NULL)
2726 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002727
2728 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002729 rdev_clear_badblocks(
2730 rdev,
2731 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002732 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002733 } else {
2734 if (!rdev_set_badblocks(
2735 rdev,
2736 r10_bio->devs[m].addr,
2737 r10_bio->sectors, 0))
2738 md_error(conf->mddev, rdev);
2739 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002740 }
NeilBrown749c55e2011-07-28 11:39:24 +10002741 put_buf(r10_bio);
2742 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002743 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002744 for (m = 0; m < conf->copies; m++) {
2745 int dev = r10_bio->devs[m].devnum;
2746 struct bio *bio = r10_bio->devs[m].bio;
2747 rdev = conf->mirrors[dev].rdev;
2748 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002749 rdev_clear_badblocks(
2750 rdev,
2751 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002752 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002753 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002754 } else if (bio != NULL && bio->bi_error) {
NeilBrown95af5872015-08-14 11:26:17 +10002755 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002756 if (!narrow_write_error(r10_bio, m)) {
2757 md_error(conf->mddev, rdev);
2758 set_bit(R10BIO_Degraded,
2759 &r10_bio->state);
2760 }
2761 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002762 }
NeilBrown475b0322011-12-23 10:17:55 +11002763 bio = r10_bio->devs[m].repl_bio;
2764 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002765 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002766 rdev_clear_badblocks(
2767 rdev,
2768 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002769 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002770 rdev_dec_pending(rdev, conf->mddev);
2771 }
NeilBrownbd870a12011-07-28 11:39:24 +10002772 }
NeilBrown95af5872015-08-14 11:26:17 +10002773 if (fail) {
2774 spin_lock_irq(&conf->device_lock);
2775 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002776 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002777 spin_unlock_irq(&conf->device_lock);
2778 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002779 } else {
2780 if (test_bit(R10BIO_WriteError,
2781 &r10_bio->state))
2782 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002783 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002784 }
NeilBrown749c55e2011-07-28 11:39:24 +10002785 }
2786}
2787
Shaohua Li4ed87312012-10-11 13:34:00 +11002788static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Shaohua Li4ed87312012-10-11 13:34:00 +11002790 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002791 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002793 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002795 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
NeilBrown95af5872015-08-14 11:26:17 +10002799 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002800 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002801 LIST_HEAD(tmp);
2802 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002803 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002804 while (!list_empty(&conf->bio_end_io_list)) {
2805 list_move(conf->bio_end_io_list.prev, &tmp);
2806 conf->nr_queued--;
2807 }
NeilBrown95af5872015-08-14 11:26:17 +10002808 }
2809 spin_unlock_irqrestore(&conf->device_lock, flags);
2810 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002811 r10_bio = list_first_entry(&tmp, struct r10bio,
2812 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002813 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002814 if (mddev->degraded)
2815 set_bit(R10BIO_Degraded, &r10_bio->state);
2816
2817 if (test_bit(R10BIO_WriteError,
2818 &r10_bio->state))
2819 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002820 raid_end_bio_io(r10_bio);
2821 }
2822 }
2823
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002824 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002826
NeilBrown0021b7b2012-07-31 09:08:14 +02002827 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002830 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002831 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002833 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002834 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002836 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 spin_unlock_irqrestore(&conf->device_lock, flags);
2838
2839 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002840 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002841 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2842 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002843 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002844 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2845 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002846 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002848 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002850 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002851 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002852 else {
2853 /* just a partial read to be scheduled from a
2854 * separate context
2855 */
2856 int slot = r10_bio->read_slot;
2857 generic_make_request(r10_bio->devs[slot].bio);
2858 }
NeilBrown4443ae12006-01-06 00:20:28 -08002859
NeilBrown1d9d5242009-10-16 15:55:32 +11002860 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002861 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002862 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002864 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
NeilBrowne879a872011-10-11 16:49:02 +11002867static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002870 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002873 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002874 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002875 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002876 if (conf->mirrors[i].replacement)
2877 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2879 if (!conf->r10buf_pool)
2880 return -ENOMEM;
2881 conf->next_resync = 0;
2882 return 0;
2883}
2884
2885/*
2886 * perform a "sync" on one "block"
2887 *
2888 * We need to make sure that no normal I/O request - particularly write
2889 * requests - conflict with active sync requests.
2890 *
2891 * This is achieved by tracking pending requests and a 'barrier' concept
2892 * that can be installed to exclude normal IO requests.
2893 *
2894 * Resync and recovery are handled very differently.
2895 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2896 *
2897 * For resync, we iterate over virtual addresses, read all copies,
2898 * and update if there are differences. If only one copy is live,
2899 * skip it.
2900 * For recovery, we iterate over physical addresses, read a good
2901 * value for each non-in_sync drive, and over-write.
2902 *
2903 * So, for recovery we may have several outstanding complex requests for a
2904 * given address, one for each out-of-sync device. We model this by allocating
2905 * a number of r10_bio structures, one for each out-of-sync device.
2906 * As we setup these structures, we collect all bio's together into a list
2907 * which we then process collectively to add pages, and then process again
2908 * to pass to generic_make_request.
2909 *
2910 * The r10_bio structures are linked using a borrowed master_bio pointer.
2911 * This link is counted in ->remaining. When the r10_bio that points to NULL
2912 * has its remaining count decremented to 0, the whole complex operation
2913 * is complete.
2914 *
2915 */
2916
Shaohua Li849674e2016-01-20 13:52:20 -08002917static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002918 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
NeilBrowne879a872011-10-11 16:49:02 +11002920 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002921 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 struct bio *biolist = NULL, *bio;
2923 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002925 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002926 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 sector_t sectors_skipped = 0;
2928 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002929 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 if (!conf->r10buf_pool)
2932 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002935 /*
2936 * Allow skipping a full rebuild for incremental assembly
2937 * of a clean array, like RAID1 does.
2938 */
2939 if (mddev->bitmap == NULL &&
2940 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002941 mddev->reshape_position == MaxSector &&
2942 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002943 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002944 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002945 conf->fullsync == 0) {
2946 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002947 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002948 }
2949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002951 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002952 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2953 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 max_sector = mddev->resync_max_sectors;
2955 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002956 /* If we aborted, we need to abort the
2957 * sync on the 'current' bitmap chucks (there can
2958 * be several when recovering multiple devices).
2959 * as we may have started syncing it but not finished.
2960 * We can find the current address in
2961 * mddev->curr_resync, but for recovery,
2962 * we need to convert that to several
2963 * virtual addresses.
2964 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002965 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2966 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002967 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002968 return 0;
2969 }
2970
NeilBrown6cce3b22006-01-06 00:20:16 -08002971 if (mddev->curr_resync < max_sector) { /* aborted */
2972 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2973 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2974 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002975 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002976 sector_t sect =
2977 raid10_find_virt(conf, mddev->curr_resync, i);
2978 bitmap_end_sync(mddev->bitmap, sect,
2979 &sync_blocks, 1);
2980 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002981 } else {
2982 /* completed sync */
2983 if ((!mddev->bitmap || conf->fullsync)
2984 && conf->have_replacement
2985 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2986 /* Completed a full sync so the replacements
2987 * are now fully recovered.
2988 */
NeilBrownf90145f2016-06-02 16:19:52 +10002989 rcu_read_lock();
2990 for (i = 0; i < conf->geo.raid_disks; i++) {
2991 struct md_rdev *rdev =
2992 rcu_dereference(conf->mirrors[i].replacement);
2993 if (rdev)
2994 rdev->recovery_offset = MaxSector;
2995 }
2996 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11002997 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002998 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002999 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003000 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07003002 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return sectors_skipped;
3004 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003005
3006 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3007 return reshape_request(mddev, sector_nr, skipped);
3008
NeilBrown5cf00fc2012-05-21 09:28:20 +10003009 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 /* if there has been nothing to do on any drive,
3011 * then there is nothing to do at all..
3012 */
NeilBrown57afd892005-06-21 17:17:13 -07003013 *skipped = 1;
3014 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016
NeilBrownc6207272008-02-06 01:39:52 -08003017 if (max_sector > mddev->resync_max)
3018 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 /* make sure whole request will fit in a chunk - if chunks
3021 * are meaningful
3022 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003023 if (conf->geo.near_copies < conf->geo.raid_disks &&
3024 max_sector > (sector_nr | chunk_mask))
3025 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003027 /*
3028 * If there is non-resync activity waiting for a turn, then let it
3029 * though before starting on this new sync request.
3030 */
3031 if (conf->nr_waiting)
3032 schedule_timeout_uninterruptible(1);
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /* Again, very different code for resync and recovery.
3035 * Both must result in an r10bio with a list of bios that
3036 * have bi_end_io, bi_sector, bi_bdev set,
3037 * and bi_private set to the r10bio.
3038 * For recovery, we may actually create several r10bios
3039 * with 2 bios in each, that correspond to the bios in the main one.
3040 * In this case, the subordinate r10bios link back through a
3041 * borrowed master_bio pointer, and the counter in the master
3042 * includes a ref from each subordinate.
3043 */
3044 /* First, we decide what to do and set ->bi_end_io
3045 * To end_sync_read if we want to read, and
3046 * end_sync_write if we will want to write.
3047 */
3048
NeilBrown6cce3b22006-01-06 00:20:16 -08003049 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3051 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003052 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 r10_bio = NULL;
3054
NeilBrown5cf00fc2012-05-21 09:28:20 +10003055 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003056 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003057 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003058 sector_t sect;
3059 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003060 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003061 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003062 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003063
NeilBrownf90145f2016-06-02 16:19:52 +10003064 rcu_read_lock();
3065 mrdev = rcu_dereference(mirror->rdev);
3066 mreplace = rcu_dereference(mirror->replacement);
3067
3068 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10003069 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10003070 test_bit(In_sync, &mrdev->flags)) &&
3071 (mreplace == NULL ||
3072 test_bit(Faulty, &mreplace->flags))) {
3073 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003074 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003075 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003076
3077 still_degraded = 0;
3078 /* want to reconstruct this device */
3079 rb2 = r10_bio;
3080 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003081 if (sect >= mddev->resync_max_sectors) {
3082 /* last stripe is not complete - don't
3083 * try to recover this sector.
3084 */
NeilBrownf90145f2016-06-02 16:19:52 +10003085 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003086 continue;
3087 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003088 if (mreplace && test_bit(Faulty, &mreplace->flags))
3089 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003090 /* Unless we are doing a full sync, or a replacement
3091 * we only need to recover the block if it is set in
3092 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003093 */
3094 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3095 &sync_blocks, 1);
3096 if (sync_blocks < max_sync)
3097 max_sync = sync_blocks;
3098 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003099 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003100 !conf->fullsync) {
3101 /* yep, skip the sync_blocks here, but don't assume
3102 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003103 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003104 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003105 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003106 continue;
3107 }
NeilBrownf90145f2016-06-02 16:19:52 +10003108 atomic_inc(&mrdev->nr_pending);
3109 if (mreplace)
3110 atomic_inc(&mreplace->nr_pending);
3111 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
NeilBrownab9d47e2011-05-11 14:54:41 +10003113 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003114 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003115 raise_barrier(conf, rb2 != NULL);
3116 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
NeilBrownab9d47e2011-05-11 14:54:41 +10003118 r10_bio->master_bio = (struct bio*)rb2;
3119 if (rb2)
3120 atomic_inc(&rb2->remaining);
3121 r10_bio->mddev = mddev;
3122 set_bit(R10BIO_IsRecover, &r10_bio->state);
3123 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003124
NeilBrownab9d47e2011-05-11 14:54:41 +10003125 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003126
NeilBrownab9d47e2011-05-11 14:54:41 +10003127 /* Need to check if the array will still be
3128 * degraded
3129 */
NeilBrownf90145f2016-06-02 16:19:52 +10003130 rcu_read_lock();
3131 for (j = 0; j < conf->geo.raid_disks; j++) {
3132 struct md_rdev *rdev = rcu_dereference(
3133 conf->mirrors[j].rdev);
3134 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003135 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
NeilBrownf90145f2016-06-02 16:19:52 +10003138 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003139
3140 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3141 &sync_blocks, still_degraded);
3142
NeilBrowne875ece2011-07-28 11:39:24 +10003143 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003144 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003145 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003146 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003147 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003148 struct md_rdev *rdev =
3149 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003150 sector_t sector, first_bad;
3151 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003152 if (!rdev ||
3153 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003154 continue;
3155 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003156 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003157 sector = r10_bio->devs[j].addr;
3158
3159 if (is_badblock(rdev, sector, max_sync,
3160 &first_bad, &bad_sectors)) {
3161 if (first_bad > sector)
3162 max_sync = first_bad - sector;
3163 else {
3164 bad_sectors -= (sector
3165 - first_bad);
3166 if (max_sync > bad_sectors)
3167 max_sync = bad_sectors;
3168 continue;
3169 }
3170 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003171 bio = r10_bio->devs[0].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003172 bio_reset(bio);
NeilBrownab9d47e2011-05-11 14:54:41 +10003173 bio->bi_next = biolist;
3174 biolist = bio;
3175 bio->bi_private = r10_bio;
3176 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003177 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003178 if (test_bit(FailFast, &rdev->flags))
3179 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003180 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003181 bio->bi_iter.bi_sector = from_addr +
3182 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003183 bio->bi_bdev = rdev->bdev;
3184 atomic_inc(&rdev->nr_pending);
3185 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003186
3187 for (k=0; k<conf->copies; k++)
3188 if (r10_bio->devs[k].devnum == i)
3189 break;
3190 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003191 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003192 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003193 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003194 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003195 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003196
NeilBrownf90145f2016-06-02 16:19:52 +10003197 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003198 bio = r10_bio->devs[1].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003199 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003200 bio->bi_next = biolist;
3201 biolist = bio;
3202 bio->bi_private = r10_bio;
3203 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003204 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003205 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003206 + mrdev->data_offset;
3207 bio->bi_bdev = mrdev->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003208 atomic_inc(&r10_bio->remaining);
3209 } else
3210 r10_bio->devs[1].bio->bi_end_io = NULL;
3211
3212 /* and maybe write to replacement */
3213 bio = r10_bio->devs[1].repl_bio;
3214 if (bio)
3215 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003216 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003217 * cannot be NULL as r10buf_pool_alloc will
3218 * have allocated it.
3219 * So the second test here is pointless.
3220 * But it keeps semantic-checkers happy, and
3221 * this comment keeps human reviewers
3222 * happy.
3223 */
NeilBrownf90145f2016-06-02 16:19:52 +10003224 if (mreplace == NULL || bio == NULL ||
3225 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003226 break;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003227 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003228 bio->bi_next = biolist;
3229 biolist = bio;
3230 bio->bi_private = r10_bio;
3231 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003232 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003233 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003234 mreplace->data_offset;
3235 bio->bi_bdev = mreplace->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003236 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
NeilBrownf90145f2016-06-02 16:19:52 +10003239 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003240 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003241 /* Cannot recover, so abort the recovery or
3242 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003243 if (any_working) {
3244 /* problem is that there are bad blocks
3245 * on other device(s)
3246 */
3247 int k;
3248 for (k = 0; k < conf->copies; k++)
3249 if (r10_bio->devs[k].devnum == i)
3250 break;
NeilBrown24afd802011-12-23 10:17:55 +11003251 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003252 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003253 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003254 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003255 r10_bio->devs[k].addr,
3256 max_sync, 0))
3257 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003258 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003259 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003260 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003261 r10_bio->devs[k].addr,
3262 max_sync, 0))
3263 any_working = 0;
3264 }
3265 if (!any_working) {
3266 if (!test_and_set_bit(MD_RECOVERY_INTR,
3267 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003268 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003269 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003270 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003271 = mddev->recovery_disabled;
3272 }
NeilBrowne8b84912014-01-06 10:35:34 +11003273 put_buf(r10_bio);
3274 if (rb2)
3275 atomic_dec(&rb2->remaining);
3276 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003277 rdev_dec_pending(mrdev, mddev);
3278 if (mreplace)
3279 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003280 break;
3281 }
NeilBrownf90145f2016-06-02 16:19:52 +10003282 rdev_dec_pending(mrdev, mddev);
3283 if (mreplace)
3284 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003285 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3286 /* Only want this if there is elsewhere to
3287 * read from. 'j' is currently the first
3288 * readable copy.
3289 */
3290 int targets = 1;
3291 for (; j < conf->copies; j++) {
3292 int d = r10_bio->devs[j].devnum;
3293 if (conf->mirrors[d].rdev &&
3294 test_bit(In_sync,
3295 &conf->mirrors[d].rdev->flags))
3296 targets++;
3297 }
3298 if (targets == 1)
3299 r10_bio->devs[0].bio->bi_opf
3300 &= ~MD_FAILFAST;
3301 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (biolist == NULL) {
3304 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003305 struct r10bio *rb2 = r10_bio;
3306 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 rb2->master_bio = NULL;
3308 put_buf(rb2);
3309 }
3310 goto giveup;
3311 }
3312 } else {
3313 /* resync. Schedule a read for every block at this virt offset */
3314 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003315
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003316 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003317
NeilBrown6cce3b22006-01-06 00:20:16 -08003318 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3319 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003320 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3321 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003322 /* We can skip this block */
3323 *skipped = 1;
3324 return sync_blocks + sectors_skipped;
3325 }
3326 if (sync_blocks < max_sync)
3327 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003329 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 r10_bio->mddev = mddev;
3332 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003333 raise_barrier(conf, 0);
3334 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 r10_bio->master_bio = NULL;
3337 r10_bio->sector = sector_nr;
3338 set_bit(R10BIO_IsSync, &r10_bio->state);
3339 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003340 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
NeilBrown5cf00fc2012-05-21 09:28:20 +10003342 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003344 sector_t first_bad, sector;
3345 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003346 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003347
NeilBrown9ad1aef2011-12-23 10:17:55 +11003348 if (r10_bio->devs[i].repl_bio)
3349 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 bio = r10_bio->devs[i].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003352 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003353 bio->bi_error = -EIO;
NeilBrownf90145f2016-06-02 16:19:52 +10003354 rcu_read_lock();
3355 rdev = rcu_dereference(conf->mirrors[d].rdev);
3356 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3357 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003359 }
NeilBrown40c356c2011-07-28 11:39:24 +10003360 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003361 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003362 &first_bad, &bad_sectors)) {
3363 if (first_bad > sector)
3364 max_sync = first_bad - sector;
3365 else {
3366 bad_sectors -= (sector - first_bad);
3367 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003368 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003369 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003370 continue;
3371 }
3372 }
NeilBrownf90145f2016-06-02 16:19:52 +10003373 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 atomic_inc(&r10_bio->remaining);
3375 bio->bi_next = biolist;
3376 biolist = bio;
3377 bio->bi_private = r10_bio;
3378 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003379 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003380 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3381 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003382 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3383 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003385
NeilBrownf90145f2016-06-02 16:19:52 +10003386 rdev = rcu_dereference(conf->mirrors[d].replacement);
3387 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3388 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003389 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003390 }
3391 atomic_inc(&rdev->nr_pending);
3392 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003393
3394 /* Need to set up for writing to the replacement */
3395 bio = r10_bio->devs[i].repl_bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003396 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003397 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003398
3399 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003400 bio->bi_next = biolist;
3401 biolist = bio;
3402 bio->bi_private = r10_bio;
3403 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003404 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
NeilBrown1919cbb2016-11-18 16:16:12 +11003405 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3406 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003407 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3408 bio->bi_bdev = rdev->bdev;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003409 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
3411
3412 if (count < 2) {
3413 for (i=0; i<conf->copies; i++) {
3414 int d = r10_bio->devs[i].devnum;
3415 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003416 rdev_dec_pending(conf->mirrors[d].rdev,
3417 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003418 if (r10_bio->devs[i].repl_bio &&
3419 r10_bio->devs[i].repl_bio->bi_end_io)
3420 rdev_dec_pending(
3421 conf->mirrors[d].replacement,
3422 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424 put_buf(r10_bio);
3425 biolist = NULL;
3426 goto giveup;
3427 }
3428 }
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003431 if (sector_nr + max_sync < max_sector)
3432 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 do {
3434 struct page *page;
3435 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 if (sector_nr + (len>>9) > max_sector)
3437 len = (max_sector - sector_nr) << 9;
3438 if (len == 0)
3439 break;
3440 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003441 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003443 if (bio_add_page(bio, page, len, 0))
3444 continue;
3445
3446 /* stop here */
3447 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3448 for (bio2 = biolist;
3449 bio2 && bio2 != bio;
3450 bio2 = bio2->bi_next) {
3451 /* remove last page from this bio */
3452 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003453 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06003454 bio_clear_flag(bio2, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003456 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458 nr_sectors += len>>9;
3459 sector_nr += len>>9;
3460 } while (biolist->bi_vcnt < RESYNC_PAGES);
3461 bio_full:
3462 r10_bio->sectors = nr_sectors;
3463
3464 while (biolist) {
3465 bio = biolist;
3466 biolist = biolist->bi_next;
3467
3468 bio->bi_next = NULL;
3469 r10_bio = bio->bi_private;
3470 r10_bio->sectors = nr_sectors;
3471
3472 if (bio->bi_end_io == end_sync_read) {
3473 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003474 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 generic_make_request(bio);
3476 }
3477 }
3478
NeilBrown57afd892005-06-21 17:17:13 -07003479 if (sectors_skipped)
3480 /* pretend they weren't skipped, it makes
3481 * no important difference in this case
3482 */
3483 md_done_sync(mddev, sectors_skipped, 1);
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return sectors_skipped + nr_sectors;
3486 giveup:
3487 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003488 * drives must be failed or in resync, all drives
3489 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 */
NeilBrown09b40682009-02-25 13:18:47 +11003491 if (sector_nr + max_sync < max_sector)
3492 max_sector = sector_nr + max_sync;
3493
3494 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 chunks_skipped ++;
3496 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
Dan Williams80c3a6c2009-03-17 18:10:40 -07003500static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003501raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003502{
3503 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003504 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003505
3506 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003507 raid_disks = min(conf->geo.raid_disks,
3508 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003509 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003510 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003511
NeilBrown5cf00fc2012-05-21 09:28:20 +10003512 size = sectors >> conf->geo.chunk_shift;
3513 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003514 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003515 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003516
NeilBrown5cf00fc2012-05-21 09:28:20 +10003517 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003518}
3519
NeilBrown6508fdb2012-05-17 10:08:45 +10003520static void calc_sectors(struct r10conf *conf, sector_t size)
3521{
3522 /* Calculate the number of sectors-per-device that will
3523 * actually be used, and set conf->dev_sectors and
3524 * conf->stride
3525 */
3526
NeilBrown5cf00fc2012-05-21 09:28:20 +10003527 size = size >> conf->geo.chunk_shift;
3528 sector_div(size, conf->geo.far_copies);
3529 size = size * conf->geo.raid_disks;
3530 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003531 /* 'size' is now the number of chunks in the array */
3532 /* calculate "used chunks per device" */
3533 size = size * conf->copies;
3534
3535 /* We need to round up when dividing by raid_disks to
3536 * get the stride size.
3537 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003538 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003539
NeilBrown5cf00fc2012-05-21 09:28:20 +10003540 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003541
NeilBrown5cf00fc2012-05-21 09:28:20 +10003542 if (conf->geo.far_offset)
3543 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003544 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003545 sector_div(size, conf->geo.far_copies);
3546 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003547 }
3548}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003549
NeilBrowndeb200d2012-05-21 09:28:33 +10003550enum geo_type {geo_new, geo_old, geo_start};
3551static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3552{
3553 int nc, fc, fo;
3554 int layout, chunk, disks;
3555 switch (new) {
3556 case geo_old:
3557 layout = mddev->layout;
3558 chunk = mddev->chunk_sectors;
3559 disks = mddev->raid_disks - mddev->delta_disks;
3560 break;
3561 case geo_new:
3562 layout = mddev->new_layout;
3563 chunk = mddev->new_chunk_sectors;
3564 disks = mddev->raid_disks;
3565 break;
3566 default: /* avoid 'may be unused' warnings */
3567 case geo_start: /* new when starting reshape - raid_disks not
3568 * updated yet. */
3569 layout = mddev->new_layout;
3570 chunk = mddev->new_chunk_sectors;
3571 disks = mddev->raid_disks + mddev->delta_disks;
3572 break;
3573 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003574 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003575 return -1;
3576 if (chunk < (PAGE_SIZE >> 9) ||
3577 !is_power_of_2(chunk))
3578 return -2;
3579 nc = layout & 255;
3580 fc = (layout >> 8) & 255;
3581 fo = layout & (1<<16);
3582 geo->raid_disks = disks;
3583 geo->near_copies = nc;
3584 geo->far_copies = fc;
3585 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003586 switch (layout >> 17) {
3587 case 0: /* original layout. simple but not always optimal */
3588 geo->far_set_size = disks;
3589 break;
3590 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3591 * actually using this, but leave code here just in case.*/
3592 geo->far_set_size = disks/fc;
3593 WARN(geo->far_set_size < fc,
3594 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3595 break;
3596 case 2: /* "improved" layout fixed to match documentation */
3597 geo->far_set_size = fc * nc;
3598 break;
3599 default: /* Not a valid layout */
3600 return -1;
3601 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003602 geo->chunk_mask = chunk - 1;
3603 geo->chunk_shift = ffz(~chunk);
3604 return nc*fc;
3605}
3606
NeilBrowne879a872011-10-11 16:49:02 +11003607static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
NeilBrowne879a872011-10-11 16:49:02 +11003609 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003610 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003611 struct geom geo;
3612 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
NeilBrowndeb200d2012-05-21 09:28:33 +10003614 copies = setup_geo(&geo, mddev, geo_new);
3615
3616 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003617 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3618 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003619 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
NeilBrown2604b702006-01-06 00:20:36 -08003621
NeilBrowndeb200d2012-05-21 09:28:33 +10003622 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003623 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3624 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 goto out;
3626 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003627
3628 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003629 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003630 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003632
NeilBrown3ea7daa2012-05-22 13:53:47 +10003633 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003634 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003635 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003636 GFP_KERNEL);
3637 if (!conf->mirrors)
3638 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003639
3640 conf->tmppage = alloc_page(GFP_KERNEL);
3641 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003642 goto out;
3643
NeilBrowndeb200d2012-05-21 09:28:33 +10003644 conf->geo = geo;
3645 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003646 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3647 r10bio_pool_free, conf);
3648 if (!conf->r10bio_pool)
3649 goto out;
3650
NeilBrown6508fdb2012-05-17 10:08:45 +10003651 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003652 if (mddev->reshape_position == MaxSector) {
3653 conf->prev = conf->geo;
3654 conf->reshape_progress = MaxSector;
3655 } else {
3656 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3657 err = -EINVAL;
3658 goto out;
3659 }
3660 conf->reshape_progress = mddev->reshape_position;
3661 if (conf->prev.far_offset)
3662 conf->prev.stride = 1 << conf->prev.chunk_shift;
3663 else
3664 /* far_copies must be 1 */
3665 conf->prev.stride = conf->dev_sectors;
3666 }
NeilBrown299b0682015-07-06 17:37:49 +10003667 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003668 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003669 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003670 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003671
3672 spin_lock_init(&conf->resync_lock);
3673 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003674 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003675
NeilBrown02326052012-07-03 15:56:52 +10003676 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003677 if (!conf->thread)
3678 goto out;
3679
Trela, Maciejdab8b292010-03-08 16:02:45 +11003680 conf->mddev = mddev;
3681 return conf;
3682
3683 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003684 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003685 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003686 kfree(conf->mirrors);
3687 safe_put_page(conf->tmppage);
3688 kfree(conf);
3689 }
3690 return ERR_PTR(err);
3691}
3692
Shaohua Li849674e2016-01-20 13:52:20 -08003693static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003694{
NeilBrowne879a872011-10-11 16:49:02 +11003695 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003696 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003697 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003698 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003699 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003700 sector_t min_offset_diff = 0;
3701 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003702 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003703
3704 if (mddev->private == NULL) {
3705 conf = setup_conf(mddev);
3706 if (IS_ERR(conf))
3707 return PTR_ERR(conf);
3708 mddev->private = conf;
3709 }
3710 conf = mddev->private;
3711 if (!conf)
3712 goto out;
3713
Trela, Maciejdab8b292010-03-08 16:02:45 +11003714 mddev->thread = conf->thread;
3715 conf->thread = NULL;
3716
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003717 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003718 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003719 blk_queue_max_discard_sectors(mddev->queue,
3720 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003721 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003722 blk_queue_io_min(mddev->queue, chunk_size);
3723 if (conf->geo.raid_disks % conf->geo.near_copies)
3724 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3725 else
3726 blk_queue_io_opt(mddev->queue, chunk_size *
3727 (conf->geo.raid_disks / conf->geo.near_copies));
3728 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003729
NeilBrowndafb20f2012-03-19 12:46:39 +11003730 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003731 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003732 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003735 if (disk_idx < 0)
3736 continue;
3737 if (disk_idx >= conf->geo.raid_disks &&
3738 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 continue;
3740 disk = conf->mirrors + disk_idx;
3741
NeilBrown56a2559b2011-12-23 10:17:55 +11003742 if (test_bit(Replacement, &rdev->flags)) {
3743 if (disk->replacement)
3744 goto out_free_conf;
3745 disk->replacement = rdev;
3746 } else {
3747 if (disk->rdev)
3748 goto out_free_conf;
3749 disk->rdev = rdev;
3750 }
NeilBrownaba336b2012-05-31 15:39:11 +10003751 q = bdev_get_queue(rdev->bdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003752 diff = (rdev->new_data_offset - rdev->data_offset);
3753 if (!mddev->reshape_backwards)
3754 diff = -diff;
3755 if (diff < 0)
3756 diff = 0;
3757 if (first || diff < min_offset_diff)
3758 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11003759
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003760 if (mddev->gendisk)
3761 disk_stack_limits(mddev->gendisk, rdev->bdev,
3762 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003765
3766 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3767 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003769
Jonathan Brassowed30be02012-10-31 11:42:30 +11003770 if (mddev->queue) {
3771 if (discard_supported)
3772 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3773 mddev->queue);
3774 else
3775 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3776 mddev->queue);
3777 }
NeilBrown6d508242005-09-09 16:24:03 -07003778 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003779 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003780 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003781 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 goto out_free_conf;
3783 }
3784
NeilBrown3ea7daa2012-05-22 13:53:47 +10003785 if (conf->reshape_progress != MaxSector) {
3786 /* must ensure that shape change is supported */
3787 if (conf->geo.far_copies != 1 &&
3788 conf->geo.far_offset == 0)
3789 goto out_free_conf;
3790 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003791 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003792 goto out_free_conf;
3793 }
3794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003796 for (i = 0;
3797 i < conf->geo.raid_disks
3798 || i < conf->prev.raid_disks;
3799 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 disk = conf->mirrors + i;
3802
NeilBrown56a2559b2011-12-23 10:17:55 +11003803 if (!disk->rdev && disk->replacement) {
3804 /* The replacement is all we have - use it */
3805 disk->rdev = disk->replacement;
3806 disk->replacement = NULL;
3807 clear_bit(Replacement, &disk->rdev->flags);
3808 }
3809
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003810 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003811 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 disk->head_position = 0;
3813 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003814 if (disk->rdev &&
3815 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003816 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
NeilBrownd890fa22011-10-26 11:54:39 +11003818 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 }
3820
Andre Noll8c6ac8682009-06-18 08:48:06 +10003821 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003822 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3823 mdname(mddev));
3824 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003825 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3826 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 /*
3828 * Ok, everything is just fine now
3829 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003830 mddev->dev_sectors = conf->dev_sectors;
3831 size = raid10_size(mddev, 0, 0);
3832 md_set_array_sectors(mddev, size);
3833 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003834 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003836 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003837 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003838 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003839
3840 /* Calculate max read-ahead size.
3841 * We need to readahead at least twice a whole stripe....
3842 * maybe...
3843 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003844 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01003845 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3846 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 }
3848
Martin K. Petersena91a2782011-03-17 11:11:05 +01003849 if (md_integrity_register(mddev))
3850 goto out_free_conf;
3851
NeilBrown3ea7daa2012-05-22 13:53:47 +10003852 if (conf->reshape_progress != MaxSector) {
3853 unsigned long before_length, after_length;
3854
3855 before_length = ((1 << conf->prev.chunk_shift) *
3856 conf->prev.far_copies);
3857 after_length = ((1 << conf->geo.chunk_shift) *
3858 conf->geo.far_copies);
3859
3860 if (max(before_length, after_length) > min_offset_diff) {
3861 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003862 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003863 goto out_free_conf;
3864 }
3865 conf->offset_diff = min_offset_diff;
3866
NeilBrown3ea7daa2012-05-22 13:53:47 +10003867 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3868 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3869 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3870 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3871 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3872 "reshape");
3873 }
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return 0;
3876
3877out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003878 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003879 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003880 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003881 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 kfree(conf);
3883 mddev->private = NULL;
3884out:
3885 return -EIO;
3886}
3887
NeilBrownafa0f552014-12-15 12:56:58 +11003888static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
NeilBrownafa0f552014-12-15 12:56:58 +11003890 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Julia Lawall644df1a2015-09-13 14:15:10 +02003892 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003893 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003894 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003895 kfree(conf->mirrors_old);
3896 kfree(conf->mirrors_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
NeilBrownfd01b882011-10-11 16:47:53 +11003900static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003901{
NeilBrowne879a872011-10-11 16:49:02 +11003902 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003903
3904 switch(state) {
3905 case 1:
3906 raise_barrier(conf, 0);
3907 break;
3908 case 0:
3909 lower_barrier(conf);
3910 break;
3911 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
NeilBrown006a09a2012-03-19 12:46:40 +11003914static int raid10_resize(struct mddev *mddev, sector_t sectors)
3915{
3916 /* Resize of 'far' arrays is not supported.
3917 * For 'near' and 'offset' arrays we can set the
3918 * number of sectors used to be an appropriate multiple
3919 * of the chunk size.
3920 * For 'offset', this is far_copies*chunksize.
3921 * For 'near' the multiplier is the LCM of
3922 * near_copies and raid_disks.
3923 * So if far_copies > 1 && !far_offset, fail.
3924 * Else find LCM(raid_disks, near_copy)*far_copies and
3925 * multiply by chunk_size. Then round to this number.
3926 * This is mostly done by raid10_size()
3927 */
3928 struct r10conf *conf = mddev->private;
3929 sector_t oldsize, size;
3930
NeilBrownf8c9e742012-05-21 09:28:33 +10003931 if (mddev->reshape_position != MaxSector)
3932 return -EBUSY;
3933
NeilBrown5cf00fc2012-05-21 09:28:20 +10003934 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003935 return -EINVAL;
3936
3937 oldsize = raid10_size(mddev, 0, 0);
3938 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003939 if (mddev->external_size &&
3940 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003941 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003942 if (mddev->bitmap) {
3943 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3944 if (ret)
3945 return ret;
3946 }
3947 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003948 if (sectors > mddev->dev_sectors &&
3949 mddev->recovery_cp > oldsize) {
3950 mddev->recovery_cp = oldsize;
3951 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3952 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003953 calc_sectors(conf, sectors);
3954 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003955 mddev->resync_max_sectors = size;
3956 return 0;
3957}
3958
NeilBrown53a6ab42015-02-12 14:09:57 +11003959static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003960{
NeilBrown3cb03002011-10-11 16:45:26 +11003961 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003962 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003963
3964 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003965 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3966 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003967 return ERR_PTR(-EINVAL);
3968 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003969 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003970
Trela, Maciejdab8b292010-03-08 16:02:45 +11003971 /* Set new parameters */
3972 mddev->new_level = 10;
3973 /* new layout: far_copies = 1, near_copies = 2 */
3974 mddev->new_layout = (1<<8) + 2;
3975 mddev->new_chunk_sectors = mddev->chunk_sectors;
3976 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003977 mddev->raid_disks *= 2;
3978 /* make sure it will be not marked as dirty */
3979 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003980 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003981
3982 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003983 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003984 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003985 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003986 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003987 rdev->sectors = size;
3988 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003989 conf->barrier = 1;
3990 }
3991
Trela, Maciejdab8b292010-03-08 16:02:45 +11003992 return conf;
3993}
3994
NeilBrownfd01b882011-10-11 16:47:53 +11003995static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003996{
NeilBrowne373ab12011-10-11 16:48:59 +11003997 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003998
3999 /* raid10 can take over:
4000 * raid0 - providing it has only two drives
4001 */
4002 if (mddev->level == 0) {
4003 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11004004 raid0_conf = mddev->private;
4005 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11004006 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4007 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004008 return ERR_PTR(-EINVAL);
4009 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004010 return raid10_takeover_raid0(mddev,
4011 raid0_conf->strip_zone->zone_end,
4012 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004013 }
4014 return ERR_PTR(-EINVAL);
4015}
4016
NeilBrown3ea7daa2012-05-22 13:53:47 +10004017static int raid10_check_reshape(struct mddev *mddev)
4018{
4019 /* Called when there is a request to change
4020 * - layout (to ->new_layout)
4021 * - chunk size (to ->new_chunk_sectors)
4022 * - raid_disks (by delta_disks)
4023 * or when trying to restart a reshape that was ongoing.
4024 *
4025 * We need to validate the request and possibly allocate
4026 * space if that might be an issue later.
4027 *
4028 * Currently we reject any reshape of a 'far' mode array,
4029 * allow chunk size to change if new is generally acceptable,
4030 * allow raid_disks to increase, and allow
4031 * a switch between 'near' mode and 'offset' mode.
4032 */
4033 struct r10conf *conf = mddev->private;
4034 struct geom geo;
4035
4036 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4037 return -EINVAL;
4038
4039 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4040 /* mustn't change number of copies */
4041 return -EINVAL;
4042 if (geo.far_copies > 1 && !geo.far_offset)
4043 /* Cannot switch to 'far' mode */
4044 return -EINVAL;
4045
4046 if (mddev->array_sectors & geo.chunk_mask)
4047 /* not factor of array size */
4048 return -EINVAL;
4049
NeilBrown3ea7daa2012-05-22 13:53:47 +10004050 if (!enough(conf, -1))
4051 return -EINVAL;
4052
4053 kfree(conf->mirrors_new);
4054 conf->mirrors_new = NULL;
4055 if (mddev->delta_disks > 0) {
4056 /* allocate new 'mirrors' list */
4057 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004058 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004059 *(mddev->raid_disks +
4060 mddev->delta_disks),
4061 GFP_KERNEL);
4062 if (!conf->mirrors_new)
4063 return -ENOMEM;
4064 }
4065 return 0;
4066}
4067
4068/*
4069 * Need to check if array has failed when deciding whether to:
4070 * - start an array
4071 * - remove non-faulty devices
4072 * - add a spare
4073 * - allow a reshape
4074 * This determination is simple when no reshape is happening.
4075 * However if there is a reshape, we need to carefully check
4076 * both the before and after sections.
4077 * This is because some failed devices may only affect one
4078 * of the two sections, and some non-in_sync devices may
4079 * be insync in the section most affected by failed devices.
4080 */
4081static int calc_degraded(struct r10conf *conf)
4082{
4083 int degraded, degraded2;
4084 int i;
4085
4086 rcu_read_lock();
4087 degraded = 0;
4088 /* 'prev' section first */
4089 for (i = 0; i < conf->prev.raid_disks; i++) {
4090 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4091 if (!rdev || test_bit(Faulty, &rdev->flags))
4092 degraded++;
4093 else if (!test_bit(In_sync, &rdev->flags))
4094 /* When we can reduce the number of devices in
4095 * an array, this might not contribute to
4096 * 'degraded'. It does now.
4097 */
4098 degraded++;
4099 }
4100 rcu_read_unlock();
4101 if (conf->geo.raid_disks == conf->prev.raid_disks)
4102 return degraded;
4103 rcu_read_lock();
4104 degraded2 = 0;
4105 for (i = 0; i < conf->geo.raid_disks; i++) {
4106 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4107 if (!rdev || test_bit(Faulty, &rdev->flags))
4108 degraded2++;
4109 else if (!test_bit(In_sync, &rdev->flags)) {
4110 /* If reshape is increasing the number of devices,
4111 * this section has already been recovered, so
4112 * it doesn't contribute to degraded.
4113 * else it does.
4114 */
4115 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4116 degraded2++;
4117 }
4118 }
4119 rcu_read_unlock();
4120 if (degraded2 > degraded)
4121 return degraded2;
4122 return degraded;
4123}
4124
4125static int raid10_start_reshape(struct mddev *mddev)
4126{
4127 /* A 'reshape' has been requested. This commits
4128 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4129 * This also checks if there are enough spares and adds them
4130 * to the array.
4131 * We currently require enough spares to make the final
4132 * array non-degraded. We also require that the difference
4133 * between old and new data_offset - on each device - is
4134 * enough that we never risk over-writing.
4135 */
4136
4137 unsigned long before_length, after_length;
4138 sector_t min_offset_diff = 0;
4139 int first = 1;
4140 struct geom new;
4141 struct r10conf *conf = mddev->private;
4142 struct md_rdev *rdev;
4143 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004144 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004145
4146 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4147 return -EBUSY;
4148
4149 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4150 return -EINVAL;
4151
4152 before_length = ((1 << conf->prev.chunk_shift) *
4153 conf->prev.far_copies);
4154 after_length = ((1 << conf->geo.chunk_shift) *
4155 conf->geo.far_copies);
4156
4157 rdev_for_each(rdev, mddev) {
4158 if (!test_bit(In_sync, &rdev->flags)
4159 && !test_bit(Faulty, &rdev->flags))
4160 spares++;
4161 if (rdev->raid_disk >= 0) {
4162 long long diff = (rdev->new_data_offset
4163 - rdev->data_offset);
4164 if (!mddev->reshape_backwards)
4165 diff = -diff;
4166 if (diff < 0)
4167 diff = 0;
4168 if (first || diff < min_offset_diff)
4169 min_offset_diff = diff;
4170 }
4171 }
4172
4173 if (max(before_length, after_length) > min_offset_diff)
4174 return -EINVAL;
4175
4176 if (spares < mddev->delta_disks)
4177 return -EINVAL;
4178
4179 conf->offset_diff = min_offset_diff;
4180 spin_lock_irq(&conf->device_lock);
4181 if (conf->mirrors_new) {
4182 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004183 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004184 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004185 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004186 conf->mirrors_old = conf->mirrors;
4187 conf->mirrors = conf->mirrors_new;
4188 conf->mirrors_new = NULL;
4189 }
4190 setup_geo(&conf->geo, mddev, geo_start);
4191 smp_mb();
4192 if (mddev->reshape_backwards) {
4193 sector_t size = raid10_size(mddev, 0, 0);
4194 if (size < mddev->array_sectors) {
4195 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004196 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4197 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004198 return -EINVAL;
4199 }
4200 mddev->resync_max_sectors = size;
4201 conf->reshape_progress = size;
4202 } else
4203 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004204 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004205 spin_unlock_irq(&conf->device_lock);
4206
NeilBrownbb63a702012-05-22 13:55:28 +10004207 if (mddev->delta_disks && mddev->bitmap) {
4208 ret = bitmap_resize(mddev->bitmap,
4209 raid10_size(mddev, 0,
4210 conf->geo.raid_disks),
4211 0, 0);
4212 if (ret)
4213 goto abort;
4214 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004215 if (mddev->delta_disks > 0) {
4216 rdev_for_each(rdev, mddev)
4217 if (rdev->raid_disk < 0 &&
4218 !test_bit(Faulty, &rdev->flags)) {
4219 if (raid10_add_disk(mddev, rdev) == 0) {
4220 if (rdev->raid_disk >=
4221 conf->prev.raid_disks)
4222 set_bit(In_sync, &rdev->flags);
4223 else
4224 rdev->recovery_offset = 0;
4225
4226 if (sysfs_link_rdev(mddev, rdev))
4227 /* Failure here is OK */;
4228 }
4229 } else if (rdev->raid_disk >= conf->prev.raid_disks
4230 && !test_bit(Faulty, &rdev->flags)) {
4231 /* This is a spare that was manually added */
4232 set_bit(In_sync, &rdev->flags);
4233 }
4234 }
4235 /* When a reshape changes the number of devices,
4236 * ->degraded is measured against the larger of the
4237 * pre and post numbers.
4238 */
4239 spin_lock_irq(&conf->device_lock);
4240 mddev->degraded = calc_degraded(conf);
4241 spin_unlock_irq(&conf->device_lock);
4242 mddev->raid_disks = conf->geo.raid_disks;
4243 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004244 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004245
4246 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4247 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004248 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004249 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4250 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4251
4252 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4253 "reshape");
4254 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004255 ret = -EAGAIN;
4256 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004257 }
4258 conf->reshape_checkpoint = jiffies;
4259 md_wakeup_thread(mddev->sync_thread);
4260 md_new_event(mddev);
4261 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004262
4263abort:
4264 mddev->recovery = 0;
4265 spin_lock_irq(&conf->device_lock);
4266 conf->geo = conf->prev;
4267 mddev->raid_disks = conf->geo.raid_disks;
4268 rdev_for_each(rdev, mddev)
4269 rdev->new_data_offset = rdev->data_offset;
4270 smp_wmb();
4271 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004272 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004273 mddev->reshape_position = MaxSector;
4274 spin_unlock_irq(&conf->device_lock);
4275 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004276}
4277
4278/* Calculate the last device-address that could contain
4279 * any block from the chunk that includes the array-address 's'
4280 * and report the next address.
4281 * i.e. the address returned will be chunk-aligned and after
4282 * any data that is in the chunk containing 's'.
4283 */
4284static sector_t last_dev_address(sector_t s, struct geom *geo)
4285{
4286 s = (s | geo->chunk_mask) + 1;
4287 s >>= geo->chunk_shift;
4288 s *= geo->near_copies;
4289 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4290 s *= geo->far_copies;
4291 s <<= geo->chunk_shift;
4292 return s;
4293}
4294
4295/* Calculate the first device-address that could contain
4296 * any block from the chunk that includes the array-address 's'.
4297 * This too will be the start of a chunk
4298 */
4299static sector_t first_dev_address(sector_t s, struct geom *geo)
4300{
4301 s >>= geo->chunk_shift;
4302 s *= geo->near_copies;
4303 sector_div(s, geo->raid_disks);
4304 s *= geo->far_copies;
4305 s <<= geo->chunk_shift;
4306 return s;
4307}
4308
4309static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4310 int *skipped)
4311{
4312 /* We simply copy at most one chunk (smallest of old and new)
4313 * at a time, possibly less if that exceeds RESYNC_PAGES,
4314 * or we hit a bad block or something.
4315 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004316 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004317 * can record any location.
4318 *
4319 * If we will want to write to a location that isn't
4320 * yet recorded as 'safe' (i.e. in metadata on disk) then
4321 * we need to flush all reshape requests and update the metadata.
4322 *
4323 * When reshaping forwards (e.g. to more devices), we interpret
4324 * 'safe' as the earliest block which might not have been copied
4325 * down yet. We divide this by previous stripe size and multiply
4326 * by previous stripe length to get lowest device offset that we
4327 * cannot write to yet.
4328 * We interpret 'sector_nr' as an address that we want to write to.
4329 * From this we use last_device_address() to find where we might
4330 * write to, and first_device_address on the 'safe' position.
4331 * If this 'next' write position is after the 'safe' position,
4332 * we must update the metadata to increase the 'safe' position.
4333 *
4334 * When reshaping backwards, we round in the opposite direction
4335 * and perform the reverse test: next write position must not be
4336 * less than current safe position.
4337 *
4338 * In all this the minimum difference in data offsets
4339 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004340 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004341 *
4342 * We need to prepare all the bios here before we start any IO
4343 * to ensure the size we choose is acceptable to all devices.
4344 * The means one for each copy for write-out and an extra one for
4345 * read-in.
4346 * We store the read-in bio in ->master_bio and the others in
4347 * ->devs[x].bio and ->devs[x].repl_bio.
4348 */
4349 struct r10conf *conf = mddev->private;
4350 struct r10bio *r10_bio;
4351 sector_t next, safe, last;
4352 int max_sectors;
4353 int nr_sectors;
4354 int s;
4355 struct md_rdev *rdev;
4356 int need_flush = 0;
4357 struct bio *blist;
4358 struct bio *bio, *read_bio;
4359 int sectors_done = 0;
4360
4361 if (sector_nr == 0) {
4362 /* If restarting in the middle, skip the initial sectors */
4363 if (mddev->reshape_backwards &&
4364 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4365 sector_nr = (raid10_size(mddev, 0, 0)
4366 - conf->reshape_progress);
4367 } else if (!mddev->reshape_backwards &&
4368 conf->reshape_progress > 0)
4369 sector_nr = conf->reshape_progress;
4370 if (sector_nr) {
4371 mddev->curr_resync_completed = sector_nr;
4372 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4373 *skipped = 1;
4374 return sector_nr;
4375 }
4376 }
4377
4378 /* We don't use sector_nr to track where we are up to
4379 * as that doesn't work well for ->reshape_backwards.
4380 * So just use ->reshape_progress.
4381 */
4382 if (mddev->reshape_backwards) {
4383 /* 'next' is the earliest device address that we might
4384 * write to for this chunk in the new layout
4385 */
4386 next = first_dev_address(conf->reshape_progress - 1,
4387 &conf->geo);
4388
4389 /* 'safe' is the last device address that we might read from
4390 * in the old layout after a restart
4391 */
4392 safe = last_dev_address(conf->reshape_safe - 1,
4393 &conf->prev);
4394
4395 if (next + conf->offset_diff < safe)
4396 need_flush = 1;
4397
4398 last = conf->reshape_progress - 1;
4399 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4400 & conf->prev.chunk_mask);
4401 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4402 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4403 } else {
4404 /* 'next' is after the last device address that we
4405 * might write to for this chunk in the new layout
4406 */
4407 next = last_dev_address(conf->reshape_progress, &conf->geo);
4408
4409 /* 'safe' is the earliest device address that we might
4410 * read from in the old layout after a restart
4411 */
4412 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4413
4414 /* Need to update metadata if 'next' might be beyond 'safe'
4415 * as that would possibly corrupt data
4416 */
4417 if (next > safe + conf->offset_diff)
4418 need_flush = 1;
4419
4420 sector_nr = conf->reshape_progress;
4421 last = sector_nr | (conf->geo.chunk_mask
4422 & conf->prev.chunk_mask);
4423
4424 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4425 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4426 }
4427
4428 if (need_flush ||
4429 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4430 /* Need to update reshape_position in metadata */
4431 wait_barrier(conf);
4432 mddev->reshape_position = conf->reshape_progress;
4433 if (mddev->reshape_backwards)
4434 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4435 - conf->reshape_progress;
4436 else
4437 mddev->curr_resync_completed = conf->reshape_progress;
4438 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004439 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004440 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004441 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004442 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4443 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4444 allow_barrier(conf);
4445 return sectors_done;
4446 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004447 conf->reshape_safe = mddev->reshape_position;
4448 allow_barrier(conf);
4449 }
4450
4451read_more:
4452 /* Now schedule reads for blocks from sector_nr to last */
4453 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004454 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004455 raise_barrier(conf, sectors_done != 0);
4456 atomic_set(&r10_bio->remaining, 0);
4457 r10_bio->mddev = mddev;
4458 r10_bio->sector = sector_nr;
4459 set_bit(R10BIO_IsReshape, &r10_bio->state);
4460 r10_bio->sectors = last - sector_nr + 1;
4461 rdev = read_balance(conf, r10_bio, &max_sectors);
4462 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4463
4464 if (!rdev) {
4465 /* Cannot read from here, so need to record bad blocks
4466 * on all the target devices.
4467 */
4468 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004469 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004470 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4471 return sectors_done;
4472 }
4473
4474 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4475
4476 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004477 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004478 + rdev->data_offset);
4479 read_bio->bi_private = r10_bio;
4480 read_bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004481 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004482 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004483 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004484 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004485 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004486 r10_bio->master_bio = read_bio;
4487 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4488
4489 /* Now find the locations in the new layout */
4490 __raid10_find_phys(&conf->geo, r10_bio);
4491
4492 blist = read_bio;
4493 read_bio->bi_next = NULL;
4494
NeilBrownd094d682016-06-02 16:19:52 +10004495 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004496 for (s = 0; s < conf->copies*2; s++) {
4497 struct bio *b;
4498 int d = r10_bio->devs[s/2].devnum;
4499 struct md_rdev *rdev2;
4500 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004501 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004502 b = r10_bio->devs[s/2].repl_bio;
4503 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004504 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004505 b = r10_bio->devs[s/2].bio;
4506 }
4507 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4508 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004509
4510 bio_reset(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004511 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004512 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4513 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004514 b->bi_private = r10_bio;
4515 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004516 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004517 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004518 blist = b;
4519 }
4520
4521 /* Now add as many pages as possible to all of these bios. */
4522
4523 nr_sectors = 0;
4524 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4525 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4526 int len = (max_sectors - s) << 9;
4527 if (len > PAGE_SIZE)
4528 len = PAGE_SIZE;
4529 for (bio = blist; bio ; bio = bio->bi_next) {
4530 struct bio *bio2;
4531 if (bio_add_page(bio, page, len, 0))
4532 continue;
4533
4534 /* Didn't fit, must stop */
4535 for (bio2 = blist;
4536 bio2 && bio2 != bio;
4537 bio2 = bio2->bi_next) {
4538 /* Remove last page from this bio */
4539 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004540 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004541 bio_clear_flag(bio2, BIO_SEG_VALID);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004542 }
4543 goto bio_full;
4544 }
4545 sector_nr += len >> 9;
4546 nr_sectors += len >> 9;
4547 }
4548bio_full:
NeilBrownd094d682016-06-02 16:19:52 +10004549 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004550 r10_bio->sectors = nr_sectors;
4551
4552 /* Now submit the read */
4553 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4554 atomic_inc(&r10_bio->remaining);
4555 read_bio->bi_next = NULL;
4556 generic_make_request(read_bio);
4557 sector_nr += nr_sectors;
4558 sectors_done += nr_sectors;
4559 if (sector_nr <= last)
4560 goto read_more;
4561
4562 /* Now that we have done the whole section we can
4563 * update reshape_progress
4564 */
4565 if (mddev->reshape_backwards)
4566 conf->reshape_progress -= sectors_done;
4567 else
4568 conf->reshape_progress += sectors_done;
4569
4570 return sectors_done;
4571}
4572
4573static void end_reshape_request(struct r10bio *r10_bio);
4574static int handle_reshape_read_error(struct mddev *mddev,
4575 struct r10bio *r10_bio);
4576static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4577{
4578 /* Reshape read completed. Hopefully we have a block
4579 * to write out.
4580 * If we got a read error then we do sync 1-page reads from
4581 * elsewhere until we find the data - or give up.
4582 */
4583 struct r10conf *conf = mddev->private;
4584 int s;
4585
4586 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4587 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4588 /* Reshape has been aborted */
4589 md_done_sync(mddev, r10_bio->sectors, 0);
4590 return;
4591 }
4592
4593 /* We definitely have the data in the pages, schedule the
4594 * writes.
4595 */
4596 atomic_set(&r10_bio->remaining, 1);
4597 for (s = 0; s < conf->copies*2; s++) {
4598 struct bio *b;
4599 int d = r10_bio->devs[s/2].devnum;
4600 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004601 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004602 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004603 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004604 b = r10_bio->devs[s/2].repl_bio;
4605 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004606 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004607 b = r10_bio->devs[s/2].bio;
4608 }
NeilBrownd094d682016-06-02 16:19:52 +10004609 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4610 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004611 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004612 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004613 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004614 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004615 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4616 atomic_inc(&r10_bio->remaining);
4617 b->bi_next = NULL;
4618 generic_make_request(b);
4619 }
4620 end_reshape_request(r10_bio);
4621}
4622
4623static void end_reshape(struct r10conf *conf)
4624{
4625 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4626 return;
4627
4628 spin_lock_irq(&conf->device_lock);
4629 conf->prev = conf->geo;
4630 md_finish_reshape(conf->mddev);
4631 smp_wmb();
4632 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004633 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004634 spin_unlock_irq(&conf->device_lock);
4635
4636 /* read-ahead size must cover two whole stripes, which is
4637 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4638 */
4639 if (conf->mddev->queue) {
4640 int stripe = conf->geo.raid_disks *
4641 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4642 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01004643 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4644 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004645 }
4646 conf->fullsync = 0;
4647}
4648
NeilBrown3ea7daa2012-05-22 13:53:47 +10004649static int handle_reshape_read_error(struct mddev *mddev,
4650 struct r10bio *r10_bio)
4651{
4652 /* Use sync reads to get the blocks from somewhere else */
4653 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004654 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004655 struct {
4656 struct r10bio r10_bio;
4657 struct r10dev devs[conf->copies];
4658 } on_stack;
4659 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004660 int slot = 0;
4661 int idx = 0;
4662 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4663
NeilBrowne0ee7782012-08-18 09:51:42 +10004664 r10b->sector = r10_bio->sector;
4665 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004666
4667 while (sectors) {
4668 int s = sectors;
4669 int success = 0;
4670 int first_slot = slot;
4671
4672 if (s > (PAGE_SIZE >> 9))
4673 s = PAGE_SIZE >> 9;
4674
NeilBrownd094d682016-06-02 16:19:52 +10004675 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004676 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004677 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004678 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004679 sector_t addr;
4680 if (rdev == NULL ||
4681 test_bit(Faulty, &rdev->flags) ||
4682 !test_bit(In_sync, &rdev->flags))
4683 goto failed;
4684
NeilBrowne0ee7782012-08-18 09:51:42 +10004685 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004686 atomic_inc(&rdev->nr_pending);
4687 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004688 success = sync_page_io(rdev,
4689 addr,
4690 s << 9,
4691 bvec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05004692 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004693 rdev_dec_pending(rdev, mddev);
4694 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004695 if (success)
4696 break;
4697 failed:
4698 slot++;
4699 if (slot >= conf->copies)
4700 slot = 0;
4701 if (slot == first_slot)
4702 break;
4703 }
NeilBrownd094d682016-06-02 16:19:52 +10004704 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004705 if (!success) {
4706 /* couldn't read this block, must give up */
4707 set_bit(MD_RECOVERY_INTR,
4708 &mddev->recovery);
4709 return -EIO;
4710 }
4711 sectors -= s;
4712 idx++;
4713 }
4714 return 0;
4715}
4716
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004717static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004718{
NeilBrown3ea7daa2012-05-22 13:53:47 +10004719 struct r10bio *r10_bio = bio->bi_private;
4720 struct mddev *mddev = r10_bio->mddev;
4721 struct r10conf *conf = mddev->private;
4722 int d;
4723 int slot;
4724 int repl;
4725 struct md_rdev *rdev = NULL;
4726
4727 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4728 if (repl)
4729 rdev = conf->mirrors[d].replacement;
4730 if (!rdev) {
4731 smp_mb();
4732 rdev = conf->mirrors[d].rdev;
4733 }
4734
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004735 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004736 /* FIXME should record badblock */
4737 md_error(mddev, rdev);
4738 }
4739
4740 rdev_dec_pending(rdev, mddev);
4741 end_reshape_request(r10_bio);
4742}
4743
4744static void end_reshape_request(struct r10bio *r10_bio)
4745{
4746 if (!atomic_dec_and_test(&r10_bio->remaining))
4747 return;
4748 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4749 bio_put(r10_bio->master_bio);
4750 put_buf(r10_bio);
4751}
4752
4753static void raid10_finish_reshape(struct mddev *mddev)
4754{
4755 struct r10conf *conf = mddev->private;
4756
4757 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4758 return;
4759
4760 if (mddev->delta_disks > 0) {
4761 sector_t size = raid10_size(mddev, 0, 0);
4762 md_set_array_sectors(mddev, size);
4763 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4764 mddev->recovery_cp = mddev->resync_max_sectors;
4765 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4766 }
4767 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004768 if (mddev->queue) {
4769 set_capacity(mddev->gendisk, mddev->array_sectors);
4770 revalidate_disk(mddev->gendisk);
4771 }
NeilBrown63aced62012-05-22 13:55:33 +10004772 } else {
4773 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004774 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004775 for (d = conf->geo.raid_disks ;
4776 d < conf->geo.raid_disks - mddev->delta_disks;
4777 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004778 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004779 if (rdev)
4780 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004781 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004782 if (rdev)
4783 clear_bit(In_sync, &rdev->flags);
4784 }
NeilBrownd094d682016-06-02 16:19:52 +10004785 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004786 }
4787 mddev->layout = mddev->new_layout;
4788 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4789 mddev->reshape_position = MaxSector;
4790 mddev->delta_disks = 0;
4791 mddev->reshape_backwards = 0;
4792}
4793
NeilBrown84fc4b52011-10-11 16:49:58 +11004794static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
4796 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004797 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004799 .make_request = raid10_make_request,
4800 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004801 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004802 .status = raid10_status,
4803 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 .hot_add_disk = raid10_add_disk,
4805 .hot_remove_disk= raid10_remove_disk,
4806 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004807 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004808 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004809 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004810 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004811 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004812 .check_reshape = raid10_check_reshape,
4813 .start_reshape = raid10_start_reshape,
4814 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004815 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816};
4817
4818static int __init raid_init(void)
4819{
NeilBrown2604b702006-01-06 00:20:36 -08004820 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
4823static void raid_exit(void)
4824{
NeilBrown2604b702006-01-06 00:20:36 -08004825 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
4828module_init(raid_init);
4829module_exit(raid_exit);
4830MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004831MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004833MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004834MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004835
4836module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);