blob: ade7d69234d54b3243792d182713cfec02f681aa [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;
NeilBrown856e08e2011-07-28 11:39:23 +1000304 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100305 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrown856e08e2011-07-28 11:39:23 +1000307 if (bio->bi_phys_segments) {
308 unsigned long flags;
309 spin_lock_irqsave(&conf->device_lock, flags);
310 bio->bi_phys_segments--;
311 done = (bio->bi_phys_segments == 0);
312 spin_unlock_irqrestore(&conf->device_lock, flags);
313 } else
314 done = 1;
315 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200316 bio->bi_error = -EIO;
NeilBrown856e08e2011-07-28 11:39:23 +1000317 if (done) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200318 bio_endio(bio);
NeilBrown856e08e2011-07-28 11:39:23 +1000319 /*
320 * Wake up any possible resync thread that waits for the device
321 * to go idle.
322 */
323 allow_barrier(conf);
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 free_r10bio(r10_bio);
326}
327
328/*
329 * Update disk head position estimator based on IRQ completion info.
330 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100331static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
NeilBrowne879a872011-10-11 16:49:02 +1100333 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
336 r10_bio->devs[slot].addr + (r10_bio->sectors);
337}
338
Namhyung Kim778ca012011-07-18 17:38:47 +1000339/*
340 * Find the disk number which triggered given bio
341 */
NeilBrowne879a872011-10-11 16:49:02 +1100342static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100343 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000344{
345 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100346 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000347
NeilBrown69335ef2011-12-23 10:17:54 +1100348 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000349 if (r10_bio->devs[slot].bio == bio)
350 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100351 if (r10_bio->devs[slot].repl_bio == bio) {
352 repl = 1;
353 break;
354 }
355 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000356
357 BUG_ON(slot == conf->copies);
358 update_head_pos(slot, r10_bio);
359
NeilBrown749c55e2011-07-28 11:39:24 +1000360 if (slotp)
361 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100362 if (replp)
363 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000364 return r10_bio->devs[slot].devnum;
365}
366
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200367static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200369 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100370 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100372 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100373 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 slot = r10_bio->read_slot;
376 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100377 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * this branch is our 'one mirror IO has finished' event handler:
380 */
NeilBrown4443ae12006-01-06 00:20:28 -0800381 update_head_pos(slot, r10_bio);
382
383 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
385 * Set R10BIO_Uptodate in our master bio, so that
386 * we will return a good error code to the higher
387 * levels even if IO on some other mirrored buffer fails.
388 *
389 * The 'master' represents the composite IO operation to
390 * user-side. So if something waits for IO, then it will
391 * wait for the 'master' bio.
392 */
393 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100394 } else {
395 /* If all other devices that store this block have
396 * failed, we want to return the error upwards rather
397 * than fail the last device. Here we redefine
398 * "uptodate" to mean "Don't want to retry"
399 */
NeilBrown635f6412013-06-11 14:57:09 +1000400 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
401 rdev->raid_disk))
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100402 uptodate = 1;
NeilBrownfae8cc5e2012-02-14 11:10:10 +1100403 }
404 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100406 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800407 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000409 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
411 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100412 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000413 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100414 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000415 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000416 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 reschedule_retry(r10_bio);
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
NeilBrown9f2c9d12011-10-11 16:48:43 +1100421static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000422{
423 /* clear the bitmap if all writes complete successfully */
424 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
425 r10_bio->sectors,
426 !test_bit(R10BIO_Degraded, &r10_bio->state),
427 0);
428 md_write_end(r10_bio->mddev);
429}
430
NeilBrown9f2c9d12011-10-11 16:48:43 +1100431static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000432{
433 if (atomic_dec_and_test(&r10_bio->remaining)) {
434 if (test_bit(R10BIO_WriteError, &r10_bio->state))
435 reschedule_retry(r10_bio);
436 else {
437 close_write(r10_bio);
438 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
439 reschedule_retry(r10_bio);
440 else
441 raid_end_bio_io(r10_bio);
442 }
443 }
444}
445
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200446static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100448 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000449 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000450 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100451 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100452 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100453 struct md_rdev *rdev = NULL;
NeilBrown1919cbb2016-11-18 16:16:12 +1100454 struct bio *to_put = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700455 bool discard_error;
456
457 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
NeilBrown475b0322011-12-23 10:17:55 +1100459 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
NeilBrown475b0322011-12-23 10:17:55 +1100461 if (repl)
462 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100463 if (!rdev) {
464 smp_rmb();
465 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100466 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * this branch is our 'one mirror IO has finished' event handler:
470 */
Shaohua Li579ed342016-10-06 14:13:52 -0700471 if (bio->bi_error && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100472 if (repl)
473 /* Never record new bad blocks to replacement,
474 * just fail it.
475 */
476 md_error(rdev->mddev, rdev);
477 else {
478 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100479 if (!test_and_set_bit(WantReplacement, &rdev->flags))
480 set_bit(MD_RECOVERY_NEEDED,
481 &rdev->mddev->recovery);
NeilBrown1919cbb2016-11-18 16:16:12 +1100482
NeilBrown475b0322011-12-23 10:17:55 +1100483 dec_rdev = 0;
NeilBrown1919cbb2016-11-18 16:16:12 +1100484 if (test_bit(FailFast, &rdev->flags) &&
485 (bio->bi_opf & MD_FAILFAST)) {
486 md_error(rdev->mddev, rdev);
487 if (!test_bit(Faulty, &rdev->flags))
488 /* This is the only remaining device,
489 * We need to retry the write without
490 * FailFast
491 */
492 set_bit(R10BIO_WriteError, &r10_bio->state);
493 else {
494 r10_bio->devs[slot].bio = NULL;
495 to_put = bio;
496 dec_rdev = 1;
497 }
498 } else
499 set_bit(R10BIO_WriteError, &r10_bio->state);
NeilBrown475b0322011-12-23 10:17:55 +1100500 }
NeilBrown749c55e2011-07-28 11:39:24 +1000501 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * Set R10BIO_Uptodate in our master bio, so that
504 * we will return a good error code for to the higher
505 * levels even if IO on some other mirrored buffer fails.
506 *
507 * The 'master' represents the composite IO operation to
508 * user-side. So if something waits for IO, then it will
509 * wait for the 'master' bio.
510 */
NeilBrown749c55e2011-07-28 11:39:24 +1000511 sector_t first_bad;
512 int bad_sectors;
513
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300514 /*
515 * Do not set R10BIO_Uptodate if the current device is
516 * rebuilding or Faulty. This is because we cannot use
517 * such device for properly reading the data back (we could
518 * potentially use it, if the current write would have felt
519 * before rdev->recovery_offset, but for simplicity we don't
520 * check this here.
521 */
522 if (test_bit(In_sync, &rdev->flags) &&
523 !test_bit(Faulty, &rdev->flags))
524 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
NeilBrown749c55e2011-07-28 11:39:24 +1000526 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100527 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000528 r10_bio->devs[slot].addr,
529 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700530 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000531 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100532 if (repl)
533 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
534 else
535 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000536 dec_rdev = 0;
537 set_bit(R10BIO_MadeGood, &r10_bio->state);
538 }
539 }
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 *
543 * Let's see if all mirrored write operations have finished
544 * already.
545 */
NeilBrown19d5f832011-09-10 17:21:17 +1000546 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000547 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100548 rdev_dec_pending(rdev, conf->mddev);
NeilBrown1919cbb2016-11-18 16:16:12 +1100549 if (to_put)
550 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
554 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300555 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * parameters: near_copies and far_copies.
557 * near_copies * far_copies must be <= raid_disks.
558 * Normally one of these will be 1.
559 * If both are 1, we get raid0.
560 * If near_copies == raid_disks, we get raid1.
561 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300562 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * first chunk, followed by near_copies copies of the next chunk and
564 * so on.
565 * If far_copies > 1, then after 1/far_copies of the array has been assigned
566 * as described above, we start again with a device offset of near_copies.
567 * So we effectively have another copy of the whole array further down all
568 * the drives, but with blocks on different drives.
569 * With this layout, and block is never stored twice on the one device.
570 *
571 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700572 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * raid10_find_virt does the reverse mapping, from a device and a
575 * sector offset to a virtual address
576 */
577
NeilBrownf8c9e742012-05-21 09:28:33 +1000578static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 int n,f;
581 sector_t sector;
582 sector_t chunk;
583 sector_t stripe;
584 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100586 int last_far_set_start, last_far_set_size;
587
588 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
589 last_far_set_start *= geo->far_set_size;
590
591 last_far_set_size = geo->far_set_size;
592 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000595 chunk = r10bio->sector >> geo->chunk_shift;
596 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
NeilBrown5cf00fc2012-05-21 09:28:20 +1000598 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000600 dev = sector_div(stripe, geo->raid_disks);
601 if (geo->far_offset)
602 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
NeilBrown5cf00fc2012-05-21 09:28:20 +1000604 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000607 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100609 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100612 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 slot++;
614
NeilBrown5cf00fc2012-05-21 09:28:20 +1000615 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100616 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000617 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100618
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100619 if ((geo->raid_disks % geo->far_set_size) &&
620 (d > last_far_set_start)) {
621 d -= last_far_set_start;
622 d %= last_far_set_size;
623 d += last_far_set_start;
624 } else {
625 d %= geo->far_set_size;
626 d += geo->far_set_size * set;
627 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000628 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 r10bio->devs[slot].devnum = d;
630 r10bio->devs[slot].addr = s;
631 slot++;
632 }
633 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000634 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000636 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000639}
640
641static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
642{
643 struct geom *geo = &conf->geo;
644
645 if (conf->reshape_progress != MaxSector &&
646 ((r10bio->sector >= conf->reshape_progress) !=
647 conf->mddev->reshape_backwards)) {
648 set_bit(R10BIO_Previous, &r10bio->state);
649 geo = &conf->prev;
650 } else
651 clear_bit(R10BIO_Previous, &r10bio->state);
652
653 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
NeilBrowne879a872011-10-11 16:49:02 +1100656static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000659 /* Never use conf->prev as this is only called during resync
660 * or recovery, so reshape isn't happening
661 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000662 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100663 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
664 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100665 int last_far_set_start;
666
667 if (geo->raid_disks % geo->far_set_size) {
668 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
669 last_far_set_start *= geo->far_set_size;
670
671 if (dev >= last_far_set_start) {
672 far_set_size = geo->far_set_size;
673 far_set_size += (geo->raid_disks % geo->far_set_size);
674 far_set_start = last_far_set_start;
675 }
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
NeilBrown5cf00fc2012-05-21 09:28:20 +1000678 offset = sector & geo->chunk_mask;
679 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700680 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000681 chunk = sector >> geo->chunk_shift;
682 fc = sector_div(chunk, geo->far_copies);
683 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100684 if (dev < far_set_start)
685 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700686 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000687 while (sector >= geo->stride) {
688 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100689 if (dev < (geo->near_copies + far_set_start))
690 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700691 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000692 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700693 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000694 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700695 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000696 vchunk = chunk * geo->raid_disks + dev;
697 sector_div(vchunk, geo->near_copies);
698 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
702 * This routine returns the disk from which the requested read should
703 * be done. There is a per-array 'next expected sequential IO' sector
704 * number - if this matches on the next IO then we use the last disk.
705 * There is also a per-disk 'last know head position' sector that is
706 * maintained from IRQ contexts, both the normal and the resync IO
707 * completion handlers update this position correctly. If there is no
708 * perfect sequential match then we pick the disk whose head is closest.
709 *
710 * If there are 2 mirrors in the same 2 devices, performance degrades
711 * because position is mirror, not device based.
712 *
713 * The rdev for the device selected will have nr_pending incremented.
714 */
715
716/*
717 * FIXME: possibly should rethink readbalancing and do it differently
718 * depending on near_copies / far_copies geometry.
719 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100720static struct md_rdev *read_balance(struct r10conf *conf,
721 struct r10bio *r10_bio,
722 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000724 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000725 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000726 int sectors = r10_bio->sectors;
727 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000728 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000729 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000730 int do_balance;
731 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000732 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 raid10_find_phys(conf, r10_bio);
735 rcu_read_lock();
NeilBrown856e08e2011-07-28 11:39:23 +1000736 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000737 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100738 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000739 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000740 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000741 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100742 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /*
744 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800745 * device if no resync is going on (recovery is ok), or below
746 * the resync window. We take the first readable disk when
747 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
749 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000750 && (this_sector + sectors >= conf->next_resync))
751 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
NeilBrown56d99122011-05-11 14:27:03 +1000753 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000754 sector_t first_bad;
755 int bad_sectors;
756 sector_t dev_sector;
757
NeilBrown56d99122011-05-11 14:27:03 +1000758 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000760 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100761 rdev = rcu_dereference(conf->mirrors[disk].replacement);
762 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
763 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
764 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100765 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700766 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100767 continue;
768 if (!test_bit(In_sync, &rdev->flags) &&
769 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000770 continue;
771
NeilBrown856e08e2011-07-28 11:39:23 +1000772 dev_sector = r10_bio->devs[slot].addr;
773 if (is_badblock(rdev, dev_sector, sectors,
774 &first_bad, &bad_sectors)) {
775 if (best_dist < MaxSector)
776 /* Already have a better slot */
777 continue;
778 if (first_bad <= dev_sector) {
779 /* Cannot read here. If this is the
780 * 'primary' device, then we must not read
781 * beyond 'bad_sectors' from another device.
782 */
783 bad_sectors -= (dev_sector - first_bad);
784 if (!do_balance && sectors > bad_sectors)
785 sectors = bad_sectors;
786 if (best_good_sectors > sectors)
787 best_good_sectors = sectors;
788 } else {
789 sector_t good_sectors =
790 first_bad - dev_sector;
791 if (good_sectors > best_good_sectors) {
792 best_good_sectors = good_sectors;
793 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100794 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000795 }
796 if (!do_balance)
797 /* Must read from here */
798 break;
799 }
800 continue;
801 } else
802 best_good_sectors = sectors;
803
NeilBrown56d99122011-05-11 14:27:03 +1000804 if (!do_balance)
805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
NeilBrown8d3ca832016-11-18 16:16:12 +1100807 if (best_slot >= 0)
808 /* At least 2 disks to choose from so failfast is OK */
809 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800810 /* This optimisation is debatable, and completely destroys
811 * sequential read speed for 'far copies' arrays. So only
812 * keep it for 'near' arrays, and review those later.
813 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000814 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
NeilBrown8d3ca832016-11-18 16:16:12 +1100815 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800816
817 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100818 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000819 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800820 else
NeilBrown56d99122011-05-11 14:27:03 +1000821 new_distance = abs(r10_bio->devs[slot].addr -
822 conf->mirrors[disk].head_position);
823 if (new_distance < best_dist) {
824 best_dist = new_distance;
825 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100826 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 }
NeilBrownabbf0982011-12-23 10:17:54 +1100829 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000830 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100831 rdev = best_rdev;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
NeilBrown56d99122011-05-11 14:27:03 +1000834 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000835 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000836 r10_bio->read_slot = slot;
837 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100838 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000840 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
NeilBrown96c3fd12011-12-23 10:17:54 +1100842 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
NeilBrown5c675f82014-12-15 12:56:56 +1100845static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700846{
NeilBrowne879a872011-10-11 16:49:02 +1100847 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700848 int i, ret = 0;
849
Tejun Heo44522262015-05-22 17:13:26 -0400850 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100851 conf->pending_count >= max_queued_requests)
852 return 1;
853
NeilBrown0d129222006-10-03 01:15:54 -0700854 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000855 for (i = 0;
856 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
857 && ret == 0;
858 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100859 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700860 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200861 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700862
863 ret |= bdi_congested(&q->backing_dev_info, bits);
864 }
865 }
866 rcu_read_unlock();
867 return ret;
868}
869
NeilBrowne879a872011-10-11 16:49:02 +1100870static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800871{
872 /* Any writes that have been queued but are awaiting
873 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800874 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800875 spin_lock_irq(&conf->device_lock);
876
877 if (conf->pending_bio_list.head) {
878 struct bio *bio;
879 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100880 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800881 spin_unlock_irq(&conf->device_lock);
882 /* flush any pending bitmap writes to disk
883 * before proceeding w/ I/O */
884 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100885 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800886
887 while (bio) { /* submit pending writes */
888 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100889 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800890 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100891 bio->bi_bdev = rdev->bdev;
892 if (test_bit(Faulty, &rdev->flags)) {
893 bio->bi_error = -EIO;
894 bio_endio(bio);
895 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
896 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li532a2a32012-10-11 13:30:52 +1100897 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200898 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100899 else
900 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800901 bio = next;
902 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800903 } else
904 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800905}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100906
NeilBrown0a27ec92006-01-06 00:20:13 -0800907/* Barriers....
908 * Sometimes we need to suspend IO while we do something else,
909 * either some resync/recovery, or reconfigure the array.
910 * To do this we raise a 'barrier'.
911 * The 'barrier' is a counter that can be raised multiple times
912 * to count how many activities are happening which preclude
913 * normal IO.
914 * We can only raise the barrier if there is no pending IO.
915 * i.e. if nr_pending == 0.
916 * We choose only to raise the barrier if no-one is waiting for the
917 * barrier to go down. This means that as soon as an IO request
918 * is ready, no other operations which require a barrier will start
919 * until the IO request has had a chance.
920 *
921 * So: regular IO calls 'wait_barrier'. When that returns there
922 * is no backgroup IO happening, It must arrange to call
923 * allow_barrier when it has finished its IO.
924 * backgroup IO calls must call raise_barrier. Once that returns
925 * there is no normal IO happeing. It must arrange to call
926 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
NeilBrowne879a872011-10-11 16:49:02 +1100929static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
NeilBrown6cce3b22006-01-06 00:20:16 -0800931 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
NeilBrown6cce3b22006-01-06 00:20:16 -0800934 /* Wait until no block IO is waiting (unless 'force') */
935 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100936 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800937
938 /* block any new IO from starting */
939 conf->barrier++;
940
NeilBrownc3b328a2011-04-18 18:25:43 +1000941 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800942 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200943 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100944 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 spin_unlock_irq(&conf->resync_lock);
947}
948
NeilBrowne879a872011-10-11 16:49:02 +1100949static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800950{
951 unsigned long flags;
952 spin_lock_irqsave(&conf->resync_lock, flags);
953 conf->barrier--;
954 spin_unlock_irqrestore(&conf->resync_lock, flags);
955 wake_up(&conf->wait_barrier);
956}
957
NeilBrowne879a872011-10-11 16:49:02 +1100958static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800959{
960 spin_lock_irq(&conf->resync_lock);
961 if (conf->barrier) {
962 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100963 /* Wait for the barrier to drop.
964 * However if there are already pending
965 * requests (preventing the barrier from
966 * rising completely), and the
967 * pre-process bio queue isn't empty,
968 * then don't wait, as we need to empty
969 * that queue to get the nr_pending
970 * count down.
971 */
NeilBrown578b54a2016-11-14 16:30:21 +1100972 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100973 wait_event_lock_irq(conf->wait_barrier,
974 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200975 (atomic_read(&conf->nr_pending) &&
NeilBrownd6b42dc2012-03-19 12:46:38 +1100976 current->bio_list &&
977 !bio_list_empty(current->bio_list)),
Lukas Czernereed8c022012-11-30 11:42:40 +0100978 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800979 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200980 if (!conf->nr_waiting)
981 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800982 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200983 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800984 spin_unlock_irq(&conf->resync_lock);
985}
986
NeilBrowne879a872011-10-11 16:49:02 +1100987static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800988{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200989 if ((atomic_dec_and_test(&conf->nr_pending)) ||
990 (conf->array_freeze_pending))
991 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800992}
993
NeilBrowne2d59922013-06-12 11:01:22 +1000994static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -0800995{
996 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800997 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800998 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +1000999 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001000 * This is called in the context of one normal IO request
1001 * that has failed. Thus any sync request that might be pending
1002 * will be blocked by nr_pending, and we need to wait for
1003 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001004 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001005 * must match the number of pending IOs (nr_pending) before
1006 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001007 */
1008 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001009 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001010 conf->barrier++;
1011 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001012 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001013 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001014 conf->resync_lock,
1015 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001016
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001017 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001018 spin_unlock_irq(&conf->resync_lock);
1019}
1020
NeilBrowne879a872011-10-11 16:49:02 +11001021static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001022{
1023 /* reverse the effect of the freeze */
1024 spin_lock_irq(&conf->resync_lock);
1025 conf->barrier--;
1026 conf->nr_waiting--;
1027 wake_up(&conf->wait_barrier);
1028 spin_unlock_irq(&conf->resync_lock);
1029}
1030
NeilBrownf8c9e742012-05-21 09:28:33 +10001031static sector_t choose_data_offset(struct r10bio *r10_bio,
1032 struct md_rdev *rdev)
1033{
1034 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1035 test_bit(R10BIO_Previous, &r10_bio->state))
1036 return rdev->data_offset;
1037 else
1038 return rdev->new_data_offset;
1039}
1040
NeilBrown57c67df2012-10-11 13:32:13 +11001041struct raid10_plug_cb {
1042 struct blk_plug_cb cb;
1043 struct bio_list pending;
1044 int pending_cnt;
1045};
1046
1047static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1048{
1049 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1050 cb);
1051 struct mddev *mddev = plug->cb.data;
1052 struct r10conf *conf = mddev->private;
1053 struct bio *bio;
1054
NeilBrown874807a2012-11-27 12:14:40 +11001055 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001056 spin_lock_irq(&conf->device_lock);
1057 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1058 conf->pending_count += plug->pending_cnt;
1059 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001060 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001061 md_wakeup_thread(mddev->thread);
1062 kfree(plug);
1063 return;
1064 }
1065
1066 /* we aren't scheduling, so we can do the write-out directly. */
1067 bio = bio_list_get(&plug->pending);
1068 bitmap_unplug(mddev->bitmap);
1069 wake_up(&conf->wait_barrier);
1070
1071 while (bio) { /* submit pending writes */
1072 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001073 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001074 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001075 bio->bi_bdev = rdev->bdev;
1076 if (test_bit(Faulty, &rdev->flags)) {
1077 bio->bi_error = -EIO;
1078 bio_endio(bio);
1079 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1080 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001081 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001082 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001083 else
1084 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001085 bio = next;
1086 }
1087 kfree(plug);
1088}
1089
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001090static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1091 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
NeilBrowne879a872011-10-11 16:49:02 +11001093 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001095 const int op = bio_op(bio);
1096 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1097 int sectors_handled;
1098 int max_sectors;
1099 sector_t sectors;
1100 struct md_rdev *rdev;
1101 int slot;
1102
1103 /*
1104 * Register the new request and wait if the reconstruction
1105 * thread has put up a bar for new requests.
1106 * Continue immediately if no resync is active currently.
1107 */
1108 wait_barrier(conf);
1109
1110 sectors = bio_sectors(bio);
1111 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1112 bio->bi_iter.bi_sector < conf->reshape_progress &&
1113 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1114 /*
1115 * IO spans the reshape position. Need to wait for reshape to
1116 * pass
1117 */
1118 raid10_log(conf->mddev, "wait reshape");
1119 allow_barrier(conf);
1120 wait_event(conf->wait_barrier,
1121 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1122 conf->reshape_progress >= bio->bi_iter.bi_sector +
1123 sectors);
1124 wait_barrier(conf);
1125 }
1126
1127read_again:
1128 rdev = read_balance(conf, r10_bio, &max_sectors);
1129 if (!rdev) {
1130 raid_end_bio_io(r10_bio);
1131 return;
1132 }
1133 slot = r10_bio->read_slot;
1134
Ming Leid7a10302017-02-14 23:29:03 +08001135 read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001136 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
1137 max_sectors);
1138
1139 r10_bio->devs[slot].bio = read_bio;
1140 r10_bio->devs[slot].rdev = rdev;
1141
1142 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1143 choose_data_offset(r10_bio, rdev);
1144 read_bio->bi_bdev = rdev->bdev;
1145 read_bio->bi_end_io = raid10_end_read_request;
1146 bio_set_op_attrs(read_bio, op, do_sync);
1147 if (test_bit(FailFast, &rdev->flags) &&
1148 test_bit(R10BIO_FailFast, &r10_bio->state))
1149 read_bio->bi_opf |= MD_FAILFAST;
1150 read_bio->bi_private = r10_bio;
1151
1152 if (mddev->gendisk)
1153 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1154 read_bio, disk_devt(mddev->gendisk),
1155 r10_bio->sector);
1156 if (max_sectors < r10_bio->sectors) {
1157 /*
1158 * Could not read all from this device, so we will need another
1159 * r10_bio.
1160 */
1161 sectors_handled = (r10_bio->sector + max_sectors
1162 - bio->bi_iter.bi_sector);
1163 r10_bio->sectors = max_sectors;
1164 spin_lock_irq(&conf->device_lock);
1165 if (bio->bi_phys_segments == 0)
1166 bio->bi_phys_segments = 2;
1167 else
1168 bio->bi_phys_segments++;
1169 spin_unlock_irq(&conf->device_lock);
1170 /*
1171 * Cannot call generic_make_request directly as that will be
1172 * queued in __generic_make_request and subsequent
1173 * mempool_alloc might block waiting for it. so hand bio over
1174 * to raid10d.
1175 */
1176 reschedule_retry(r10_bio);
1177
1178 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1179
1180 r10_bio->master_bio = bio;
1181 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1182 r10_bio->state = 0;
1183 r10_bio->mddev = mddev;
1184 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1185 goto read_again;
1186 } else
1187 generic_make_request(read_bio);
1188 return;
1189}
1190
1191static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1192 struct r10bio *r10_bio)
1193{
1194 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int i;
Mike Christie796a5cf2016-06-05 14:32:07 -05001196 const int op = bio_op(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001197 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1198 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -08001199 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001200 struct md_rdev *blocked_rdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001201 struct blk_plug_cb *cb;
1202 struct raid10_plug_cb *plug = NULL;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001203 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001204 int sectors_handled;
1205 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Tomasz Majchrzak9b622e22016-07-28 10:28:25 +02001207 md_write_start(mddev, bio);
1208
NeilBrowncc13b1d2014-05-05 13:34:37 +10001209 /*
1210 * Register the new request and wait if the reconstruction
1211 * thread has put up a bar for new requests.
1212 * Continue immediately if no resync is active currently.
1213 */
1214 wait_barrier(conf);
1215
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001216 sectors = bio_sectors(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001217 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001218 bio->bi_iter.bi_sector < conf->reshape_progress &&
1219 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001220 /*
1221 * IO spans the reshape position. Need to wait for reshape to
1222 * pass
NeilBrown3ea7daa2012-05-22 13:53:47 +10001223 */
NeilBrown578b54a2016-11-14 16:30:21 +11001224 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001225 allow_barrier(conf);
1226 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001227 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1228 conf->reshape_progress >= bio->bi_iter.bi_sector +
1229 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001230 wait_barrier(conf);
1231 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001232
NeilBrown3ea7daa2012-05-22 13:53:47 +10001233 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001234 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001235 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1236 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1237 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1238 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001239 /* Need to update reshape_position in metadata */
1240 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001241 set_mask_bits(&mddev->sb_flags, 0,
1242 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001243 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001244 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001245 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001246 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001247
1248 conf->reshape_safe = mddev->reshape_position;
1249 }
1250
NeilBrown34db0cd2011-10-11 16:50:01 +11001251 if (conf->pending_count >= max_queued_requests) {
1252 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001253 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001254 wait_event(conf->wait_barrier,
1255 conf->pending_count < max_queued_requests);
1256 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001257 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * inc refcount on their rdev. Record them by setting
1259 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001260 * If there are known/acknowledged bad blocks on any device
1261 * on which we have seen a write error, we want to avoid
1262 * writing to those blocks. This potentially requires several
1263 * writes to write around the bad blocks. Each set of writes
1264 * gets its own r10_bio with a set of bios attached. The number
1265 * of r10_bios is recored in bio->bi_phys_segments just as with
1266 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001268
NeilBrown69335ef2011-12-23 10:17:54 +11001269 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001271retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001272 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001274 max_sectors = r10_bio->sectors;
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 for (i = 0; i < conf->copies; i++) {
1277 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001278 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001279 struct md_rdev *rrdev = rcu_dereference(
1280 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001281 if (rdev == rrdev)
1282 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001283 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1284 atomic_inc(&rdev->nr_pending);
1285 blocked_rdev = rdev;
1286 break;
1287 }
NeilBrown475b0322011-12-23 10:17:55 +11001288 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1289 atomic_inc(&rrdev->nr_pending);
1290 blocked_rdev = rrdev;
1291 break;
1292 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001293 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001294 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001295 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001296 rrdev = NULL;
1297
NeilBrownd4432c22011-07-28 11:39:24 +10001298 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001299 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001300
1301 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001302 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001303 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001304 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001305 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001306 sector_t first_bad;
1307 sector_t dev_sector = r10_bio->devs[i].addr;
1308 int bad_sectors;
1309 int is_bad;
1310
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001311 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001312 &first_bad, &bad_sectors);
1313 if (is_bad < 0) {
1314 /* Mustn't write here until the bad block
1315 * is acknowledged
1316 */
1317 atomic_inc(&rdev->nr_pending);
1318 set_bit(BlockedBadBlocks, &rdev->flags);
1319 blocked_rdev = rdev;
1320 break;
1321 }
1322 if (is_bad && first_bad <= dev_sector) {
1323 /* Cannot write here at all */
1324 bad_sectors -= (dev_sector - first_bad);
1325 if (bad_sectors < max_sectors)
1326 /* Mustn't write more than bad_sectors
1327 * to other devices yet
1328 */
1329 max_sectors = bad_sectors;
1330 /* We don't set R10BIO_Degraded as that
1331 * only applies if the disk is missing,
1332 * so it might be re-added, and we want to
1333 * know to recover this chunk.
1334 * In this case the device is here, and the
1335 * fact that this chunk is not in-sync is
1336 * recorded in the bad block log.
1337 */
1338 continue;
1339 }
1340 if (is_bad) {
1341 int good_sectors = first_bad - dev_sector;
1342 if (good_sectors < max_sectors)
1343 max_sectors = good_sectors;
1344 }
1345 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001346 if (rdev) {
1347 r10_bio->devs[i].bio = bio;
1348 atomic_inc(&rdev->nr_pending);
1349 }
NeilBrown475b0322011-12-23 10:17:55 +11001350 if (rrdev) {
1351 r10_bio->devs[i].repl_bio = bio;
1352 atomic_inc(&rrdev->nr_pending);
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 rcu_read_unlock();
1356
Dan Williams6bfe0b42008-04-30 00:52:32 -07001357 if (unlikely(blocked_rdev)) {
1358 /* Have to wait for this device to get unblocked, then retry */
1359 int j;
1360 int d;
1361
NeilBrown475b0322011-12-23 10:17:55 +11001362 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001363 if (r10_bio->devs[j].bio) {
1364 d = r10_bio->devs[j].devnum;
1365 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1366 }
NeilBrown475b0322011-12-23 10:17:55 +11001367 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001368 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001369 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001370 rdev = conf->mirrors[d].replacement;
1371 if (!rdev) {
1372 /* Race with remove_disk */
1373 smp_mb();
1374 rdev = conf->mirrors[d].rdev;
1375 }
1376 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001377 }
1378 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001379 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001380 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001381 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1382 wait_barrier(conf);
1383 goto retry_write;
1384 }
1385
NeilBrownd4432c22011-07-28 11:39:24 +10001386 if (max_sectors < r10_bio->sectors) {
1387 /* We are splitting this into multiple parts, so
1388 * we need to prepare for allocating another r10_bio.
1389 */
1390 r10_bio->sectors = max_sectors;
1391 spin_lock_irq(&conf->device_lock);
1392 if (bio->bi_phys_segments == 0)
1393 bio->bi_phys_segments = 2;
1394 else
1395 bio->bi_phys_segments++;
1396 spin_unlock_irq(&conf->device_lock);
1397 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001398 sectors_handled = r10_bio->sector + max_sectors -
1399 bio->bi_iter.bi_sector;
NeilBrownd4432c22011-07-28 11:39:24 +10001400
NeilBrown4e780642010-10-19 12:54:01 +11001401 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001402 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for (i = 0; i < conf->copies; i++) {
1405 struct bio *mbio;
1406 int d = r10_bio->devs[i].devnum;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001407 if (r10_bio->devs[i].bio) {
1408 struct md_rdev *rdev = conf->mirrors[d].rdev;
Ming Leid7a10302017-02-14 23:29:03 +08001409 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001410 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001411 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001412 r10_bio->devs[i].bio = mbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Kent Overstreet4f024f32013-10-11 15:44:27 -07001414 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001415 choose_data_offset(r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001416 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001417 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001418 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrown1919cbb2016-11-18 16:16:12 +11001419 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags) &&
1420 enough(conf, d))
1421 mbio->bi_opf |= MD_FAILFAST;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001422 mbio->bi_private = r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
NeilBrown109e3762016-11-18 13:22:04 +11001424 if (conf->mddev->gendisk)
1425 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1426 mbio, disk_devt(conf->mddev->gendisk),
1427 r10_bio->sector);
1428 /* flush_pending_writes() needs access to the rdev so...*/
1429 mbio->bi_bdev = (void*)rdev;
1430
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001431 atomic_inc(&r10_bio->remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001433 cb = blk_check_plugged(raid10_unplug, mddev,
1434 sizeof(*plug));
1435 if (cb)
1436 plug = container_of(cb, struct raid10_plug_cb,
1437 cb);
1438 else
1439 plug = NULL;
1440 spin_lock_irqsave(&conf->device_lock, flags);
1441 if (plug) {
1442 bio_list_add(&plug->pending, mbio);
1443 plug->pending_cnt++;
1444 } else {
1445 bio_list_add(&conf->pending_bio_list, mbio);
1446 conf->pending_count++;
1447 }
1448 spin_unlock_irqrestore(&conf->device_lock, flags);
1449 if (!plug)
1450 md_wakeup_thread(mddev->thread);
1451 }
NeilBrown57c67df2012-10-11 13:32:13 +11001452
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001453 if (r10_bio->devs[i].repl_bio) {
1454 struct md_rdev *rdev = conf->mirrors[d].replacement;
1455 if (rdev == NULL) {
1456 /* Replacement just got moved to main 'rdev' */
1457 smp_mb();
1458 rdev = conf->mirrors[d].rdev;
1459 }
Ming Leid7a10302017-02-14 23:29:03 +08001460 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001461 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001462 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001463 r10_bio->devs[i].repl_bio = mbio;
1464
Kent Overstreet4f024f32013-10-11 15:44:27 -07001465 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001466 choose_data_offset(r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001467 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001468 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001469 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001470 mbio->bi_private = r10_bio;
1471
NeilBrown109e3762016-11-18 13:22:04 +11001472 if (conf->mddev->gendisk)
1473 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1474 mbio, disk_devt(conf->mddev->gendisk),
1475 r10_bio->sector);
1476 /* flush_pending_writes() needs access to the rdev so...*/
1477 mbio->bi_bdev = (void*)rdev;
1478
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001479 atomic_inc(&r10_bio->remaining);
1480 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown57c67df2012-10-11 13:32:13 +11001481 bio_list_add(&conf->pending_bio_list, mbio);
1482 conf->pending_count++;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001483 spin_unlock_irqrestore(&conf->device_lock, flags);
1484 if (!mddev_check_plugged(mddev))
1485 md_wakeup_thread(mddev->thread);
NeilBrown57c67df2012-10-11 13:32:13 +11001486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
NeilBrown079fa162011-09-10 17:21:23 +10001489 /* Don't remove the bias on 'remaining' (one_write_done) until
1490 * after checking if we need to go around again.
1491 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001492
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001493 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001494 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001495 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001496 * in bio->bi_phys_segments.
1497 */
1498 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1499
1500 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001501 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001502
1503 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001504 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001505 r10_bio->state = 0;
1506 goto retry_write;
1507 }
NeilBrown079fa162011-09-10 17:21:23 +10001508 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001509}
1510
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001511static void __make_request(struct mddev *mddev, struct bio *bio)
1512{
1513 struct r10conf *conf = mddev->private;
1514 struct r10bio *r10_bio;
1515
1516 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1517
1518 r10_bio->master_bio = bio;
1519 r10_bio->sectors = bio_sectors(bio);
1520
1521 r10_bio->mddev = mddev;
1522 r10_bio->sector = bio->bi_iter.bi_sector;
1523 r10_bio->state = 0;
1524
1525 /*
1526 * We might need to issue multiple reads to different devices if there
1527 * are bad blocks around, so we keep track of the number of reads in
1528 * bio->bi_phys_segments. If this is 0, there is only one r10_bio and
1529 * no locking will be needed when the request completes. If it is
1530 * non-zero, then it is the number of not-completed requests.
1531 */
1532 bio->bi_phys_segments = 0;
1533 bio_clear_flag(bio, BIO_SEG_VALID);
1534
1535 if (bio_data_dir(bio) == READ)
1536 raid10_read_request(mddev, bio, r10_bio);
1537 else
1538 raid10_write_request(mddev, bio, r10_bio);
1539}
1540
Shaohua Li849674e2016-01-20 13:52:20 -08001541static void raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001542{
1543 struct r10conf *conf = mddev->private;
1544 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1545 int chunk_sects = chunk_mask + 1;
1546
1547 struct bio *split;
1548
Jens Axboe1eff9d32016-08-05 15:35:16 -06001549 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001550 md_flush_request(mddev, bio);
1551 return;
1552 }
1553
Kent Overstreet20d01892013-11-23 18:21:01 -08001554 do {
1555
1556 /*
1557 * If this request crosses a chunk boundary, we need to split
1558 * it.
1559 */
1560 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1561 bio_sectors(bio) > chunk_sects
1562 && (conf->geo.near_copies < conf->geo.raid_disks
1563 || conf->prev.near_copies <
1564 conf->prev.raid_disks))) {
1565 split = bio_split(bio, chunk_sects -
1566 (bio->bi_iter.bi_sector &
1567 (chunk_sects - 1)),
1568 GFP_NOIO, fs_bio_set);
1569 bio_chain(split, bio);
1570 } else {
1571 split = bio;
1572 }
1573
1574 __make_request(mddev, split);
1575 } while (split != bio);
NeilBrown079fa162011-09-10 17:21:23 +10001576
1577 /* In case raid10d snuck in to freeze_array */
1578 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Shaohua Li849674e2016-01-20 13:52:20 -08001581static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
NeilBrowne879a872011-10-11 16:49:02 +11001583 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int i;
1585
NeilBrown5cf00fc2012-05-21 09:28:20 +10001586 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001587 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001588 if (conf->geo.near_copies > 1)
1589 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1590 if (conf->geo.far_copies > 1) {
1591 if (conf->geo.far_offset)
1592 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001593 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001594 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001595 if (conf->geo.far_set_size != conf->geo.raid_disks)
1596 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001597 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001598 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1599 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001600 rcu_read_lock();
1601 for (i = 0; i < conf->geo.raid_disks; i++) {
1602 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1603 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1604 }
1605 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 seq_printf(seq, "]");
1607}
1608
NeilBrown700c7212011-07-27 11:00:36 +10001609/* check if there are enough drives for
1610 * every block to appear on atleast one.
1611 * Don't consider the device numbered 'ignore'
1612 * as we might be about to remove it.
1613 */
NeilBrown635f6412013-06-11 14:57:09 +10001614static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001615{
1616 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001617 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001618 int disks, ncopies;
1619 if (previous) {
1620 disks = conf->prev.raid_disks;
1621 ncopies = conf->prev.near_copies;
1622 } else {
1623 disks = conf->geo.raid_disks;
1624 ncopies = conf->geo.near_copies;
1625 }
NeilBrown700c7212011-07-27 11:00:36 +10001626
NeilBrown725d6e52013-06-11 15:08:03 +10001627 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001628 do {
1629 int n = conf->copies;
1630 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001631 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001632 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001633 struct md_rdev *rdev;
1634 if (this != ignore &&
1635 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1636 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001637 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001638 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001639 }
1640 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001641 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001642 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001643 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001644 has_enough = 1;
1645out:
1646 rcu_read_unlock();
1647 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001648}
1649
NeilBrownf8c9e742012-05-21 09:28:33 +10001650static int enough(struct r10conf *conf, int ignore)
1651{
NeilBrown635f6412013-06-11 14:57:09 +10001652 /* when calling 'enough', both 'prev' and 'geo' must
1653 * be stable.
1654 * This is ensured if ->reconfig_mutex or ->device_lock
1655 * is held.
1656 */
1657 return _enough(conf, 0, ignore) &&
1658 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001659}
1660
Shaohua Li849674e2016-01-20 13:52:20 -08001661static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001664 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001665 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /*
1668 * If it is not operational, then we have already marked it as dead
1669 * else if it is the last working disks, ignore the error, let the
1670 * next level up know.
1671 * else mark the drive as failed
1672 */
NeilBrown635f6412013-06-11 14:57:09 +10001673 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001674 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001675 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
1677 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001679 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001680 return;
1681 }
NeilBrown2446dba2014-07-31 10:16:29 +10001682 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001683 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001684 /*
1685 * If recovery is running, make sure it aborts.
1686 */
1687 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001688 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001689 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001690 set_mask_bits(&mddev->sb_flags, 0,
1691 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001692 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001693 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1694 "md/raid10:%s: Operation continuing on %d devices.\n",
1695 mdname(mddev), bdevname(rdev->bdev, b),
1696 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
NeilBrowne879a872011-10-11 16:49:02 +11001699static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001702 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
NeilBrown08464e02016-11-02 14:16:50 +11001704 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001706 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return;
1708 }
NeilBrown08464e02016-11-02 14:16:50 +11001709 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1710 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
NeilBrown4056ca52016-06-02 16:19:52 +10001712 /* This is only called with ->reconfix_mutex held, so
1713 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001714 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001716 rdev = conf->mirrors[i].rdev;
1717 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001718 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1719 i, !test_bit(In_sync, &rdev->flags),
1720 !test_bit(Faulty, &rdev->flags),
1721 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723}
1724
NeilBrowne879a872011-10-11 16:49:02 +11001725static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
NeilBrown0a27ec92006-01-06 00:20:13 -08001727 wait_barrier(conf);
1728 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 mempool_destroy(conf->r10buf_pool);
1731 conf->r10buf_pool = NULL;
1732}
1733
NeilBrownfd01b882011-10-11 16:47:53 +11001734static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001737 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001738 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001739 int count = 0;
1740 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /*
1743 * Find all non-in_sync disks within the RAID10 configuration
1744 * and mark them in_sync
1745 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001746 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001748 if (tmp->replacement
1749 && tmp->replacement->recovery_offset == MaxSector
1750 && !test_bit(Faulty, &tmp->replacement->flags)
1751 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1752 /* Replacement has just become active */
1753 if (!tmp->rdev
1754 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1755 count++;
1756 if (tmp->rdev) {
1757 /* Replaced device not technically faulty,
1758 * but we need to be sure it gets removed
1759 * and never re-added.
1760 */
1761 set_bit(Faulty, &tmp->rdev->flags);
1762 sysfs_notify_dirent_safe(
1763 tmp->rdev->sysfs_state);
1764 }
1765 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1766 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001767 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001768 && !test_bit(Faulty, &tmp->rdev->flags)
1769 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001770 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001771 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 }
NeilBrown6b965622010-08-18 11:56:59 +10001774 spin_lock_irqsave(&conf->device_lock, flags);
1775 mddev->degraded -= count;
1776 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001779 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
NeilBrownfd01b882011-10-11 16:47:53 +11001782static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
NeilBrowne879a872011-10-11 16:49:02 +11001784 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001785 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001787 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001788 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (mddev->recovery_cp < MaxSector)
1791 /* only hot-add to in-sync arrays, as recovery is
1792 * very different from resync
1793 */
Neil Brown199050e2008-06-28 08:31:33 +10001794 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001795 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001796 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Dan Williams1501efa2016-01-13 16:00:07 -08001798 if (md_integrity_add_rdev(rdev, mddev))
1799 return -ENXIO;
1800
NeilBrowna53a6c82008-11-06 17:28:20 +11001801 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001802 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001804 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001805 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1806 mirror = rdev->saved_raid_disk;
1807 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001808 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001809 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001810 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001811 if (p->recovery_disabled == mddev->recovery_disabled)
1812 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001813 if (p->rdev) {
1814 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1815 p->replacement != NULL)
1816 continue;
1817 clear_bit(In_sync, &rdev->flags);
1818 set_bit(Replacement, &rdev->flags);
1819 rdev->raid_disk = mirror;
1820 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001821 if (mddev->gendisk)
1822 disk_stack_limits(mddev->gendisk, rdev->bdev,
1823 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001824 conf->fullsync = 1;
1825 rcu_assign_pointer(p->replacement, rdev);
1826 break;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jonathan Brassow9092c022013-05-02 14:19:24 -05001829 if (mddev->gendisk)
1830 disk_stack_limits(mddev->gendisk, rdev->bdev,
1831 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
NeilBrown2bb77732011-07-27 11:00:36 +10001833 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001834 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001835 rdev->raid_disk = mirror;
1836 err = 0;
1837 if (rdev->saved_raid_disk != mirror)
1838 conf->fullsync = 1;
1839 rcu_assign_pointer(p->rdev, rdev);
1840 break;
1841 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11001842 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001843 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
NeilBrownb8321b62011-12-23 10:17:51 +11001849static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
NeilBrowne879a872011-10-11 16:49:02 +11001851 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001853 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001854 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001855 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001858 if (rdev == p->rdev)
1859 rdevp = &p->rdev;
1860 else if (rdev == p->replacement)
1861 rdevp = &p->replacement;
1862 else
1863 return 0;
1864
1865 if (test_bit(In_sync, &rdev->flags) ||
1866 atomic_read(&rdev->nr_pending)) {
1867 err = -EBUSY;
1868 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
NeilBrownd787be42016-06-02 16:19:53 +10001870 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11001871 * is not possible.
1872 */
1873 if (!test_bit(Faulty, &rdev->flags) &&
1874 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001875 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001876 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001877 enough(conf, -1)) {
1878 err = -EBUSY;
1879 goto abort;
1880 }
1881 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001882 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1883 synchronize_rcu();
1884 if (atomic_read(&rdev->nr_pending)) {
1885 /* lost the race, try later */
1886 err = -EBUSY;
1887 *rdevp = rdev;
1888 goto abort;
1889 }
1890 }
1891 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001892 /* We must have just cleared 'rdev' */
1893 p->rdev = p->replacement;
1894 clear_bit(Replacement, &p->replacement->flags);
1895 smp_mb(); /* Make sure other CPUs may see both as identical
1896 * but will never see neither -- if they are careful.
1897 */
1898 p->replacement = NULL;
1899 clear_bit(WantReplacement, &rdev->flags);
1900 } else
1901 /* We might have just remove the Replacement as faulty
1902 * Clear the flag just in case
1903 */
1904 clear_bit(WantReplacement, &rdev->flags);
1905
NeilBrownc8ab9032011-12-23 10:17:54 +11001906 err = md_integrity_register(mddev);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908abort:
1909
1910 print_conf(conf);
1911 return err;
1912}
1913
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001914static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001916 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001917 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001918 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
NeilBrown3ea7daa2012-05-22 13:53:47 +10001920 if (bio == r10_bio->master_bio) {
1921 /* this is a reshape read */
1922 d = r10_bio->read_slot; /* really the read dev */
1923 } else
1924 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001925
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001926 if (!bio->bi_error)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001927 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001928 else
1929 /* The write handler will notice the lack of
1930 * R10BIO_Uptodate and record any errors etc
1931 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001932 atomic_add(r10_bio->sectors,
1933 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* for reconstruct, we always reschedule after a read.
1936 * for resync, only after all reads
1937 */
NeilBrown73d5c382009-02-25 13:18:47 +11001938 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1940 atomic_dec_and_test(&r10_bio->remaining)) {
1941 /* we have read all the blocks,
1942 * do the comparison in process context in raid10d
1943 */
1944 reschedule_retry(r10_bio);
1945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
NeilBrown9f2c9d12011-10-11 16:48:43 +11001948static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001949{
NeilBrownfd01b882011-10-11 16:47:53 +11001950 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001951
1952 while (atomic_dec_and_test(&r10_bio->remaining)) {
1953 if (r10_bio->master_bio == NULL) {
1954 /* the primary of several recovery bios */
1955 sector_t s = r10_bio->sectors;
1956 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1957 test_bit(R10BIO_WriteError, &r10_bio->state))
1958 reschedule_retry(r10_bio);
1959 else
1960 put_buf(r10_bio);
1961 md_done_sync(mddev, s, 1);
1962 break;
1963 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001964 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001965 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1966 test_bit(R10BIO_WriteError, &r10_bio->state))
1967 reschedule_retry(r10_bio);
1968 else
1969 put_buf(r10_bio);
1970 r10_bio = r10_bio2;
1971 }
1972 }
1973}
1974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001975static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001977 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001978 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001979 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001980 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001981 sector_t first_bad;
1982 int bad_sectors;
1983 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001984 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001985 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
NeilBrown9ad1aef2011-12-23 10:17:55 +11001987 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1988 if (repl)
1989 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001990 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001991 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001993 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001994 if (repl)
1995 md_error(mddev, rdev);
1996 else {
1997 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001998 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1999 set_bit(MD_RECOVERY_NEEDED,
2000 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002001 set_bit(R10BIO_WriteError, &r10_bio->state);
2002 }
2003 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10002004 r10_bio->devs[slot].addr,
2005 r10_bio->sectors,
2006 &first_bad, &bad_sectors))
2007 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07002008
NeilBrown9ad1aef2011-12-23 10:17:55 +11002009 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002010
2011 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014/*
2015 * Note: sync and recover and handled very differently for raid10
2016 * This code is for resync.
2017 * For resync, we read through virtual addresses and read all blocks.
2018 * If there is any error, we schedule a write. The lowest numbered
2019 * drive is authoritative.
2020 * However requests come for physical address, so we need to map.
2021 * For every physical address there are raid_disks/copies virtual addresses,
2022 * which is always are least one, but is not necessarly an integer.
2023 * This means that a physical address can span multiple chunks, so we may
2024 * have to submit multiple io requests for a single sync request.
2025 */
2026/*
2027 * We check if all blocks are in-sync and only write to blocks that
2028 * aren't in sync
2029 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002030static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
NeilBrowne879a872011-10-11 16:49:02 +11002032 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int i, first;
2034 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002035 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 atomic_set(&r10_bio->remaining, 1);
2038
2039 /* find the first device with a block */
2040 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002041 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 break;
2043
2044 if (i == conf->copies)
2045 goto done;
2046
2047 first = i;
2048 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002049 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2050 fbio->bi_iter.bi_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
majianpengf4380a92012-04-12 16:04:47 +10002052 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002054 for (i=0 ; i < conf->copies ; i++) {
2055 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002056 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002059
2060 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002062 if (i == first)
2063 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002064 d = r10_bio->devs[i].devnum;
2065 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002066 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002067 /* We know that the bi_io_vec layout is the same for
2068 * both 'first' and 'i', so we just compare them.
2069 * All vec entries are PAGE_SIZE;
2070 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002071 int sectors = r10_bio->sectors;
2072 for (j = 0; j < vcnt; j++) {
2073 int len = PAGE_SIZE;
2074 if (sectors < (len / 512))
2075 len = sectors * 512;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002076 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2077 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown7bb23c42013-07-16 16:50:47 +10002078 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002079 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002080 sectors -= len/512;
2081 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002082 if (j == vcnt)
2083 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002084 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002085 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2086 /* Don't fix anything. */
2087 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002088 } else if (test_bit(FailFast, &rdev->flags)) {
2089 /* Just give up on this device */
2090 md_error(rdev->mddev, rdev);
2091 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002092 }
NeilBrownf84ee362011-07-28 11:39:25 +10002093 /* Ok, we need to write this bio, either to correct an
2094 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * First we need to fixup bv_offset, bv_len and
2096 * bi_vecs, as the read request might have corrupted these
2097 */
Kent Overstreet8be185f2012-09-06 14:14:43 -07002098 bio_reset(tbio);
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 tbio->bi_vcnt = vcnt;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002101 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 tbio->bi_private = r10_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002103 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002105 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Kent Overstreetc31df252015-05-06 23:34:20 -07002107 bio_copy_data(tbio, fbio);
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2110 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002111 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
NeilBrown1919cbb2016-11-18 16:16:12 +11002113 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2114 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002115 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2117 generic_make_request(tbio);
2118 }
2119
NeilBrown9ad1aef2011-12-23 10:17:55 +11002120 /* Now write out to any replacement devices
2121 * that are active
2122 */
2123 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002124 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002125
2126 tbio = r10_bio->devs[i].repl_bio;
2127 if (!tbio || !tbio->bi_end_io)
2128 continue;
2129 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2130 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002131 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002132 d = r10_bio->devs[i].devnum;
2133 atomic_inc(&r10_bio->remaining);
2134 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002135 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002136 generic_make_request(tbio);
2137 }
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139done:
2140 if (atomic_dec_and_test(&r10_bio->remaining)) {
2141 md_done_sync(mddev, r10_bio->sectors, 1);
2142 put_buf(r10_bio);
2143 }
2144}
2145
2146/*
2147 * Now for the recovery code.
2148 * Recovery happens across physical sectors.
2149 * We recover all non-is_sync drives by finding the virtual address of
2150 * each, and then choose a working drive that also has that virt address.
2151 * There is a separate r10_bio for each non-in_sync drive.
2152 * Only the first two slots are in use. The first for reading,
2153 * The second for writing.
2154 *
2155 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002156static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002157{
2158 /* We got a read error during recovery.
2159 * We repeat the read in smaller page-sized sections.
2160 * If a read succeeds, write it to the new device or record
2161 * a bad block if we cannot.
2162 * If a read fails, record a bad block on both old and
2163 * new devices.
2164 */
NeilBrownfd01b882011-10-11 16:47:53 +11002165 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002166 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002167 struct bio *bio = r10_bio->devs[0].bio;
2168 sector_t sect = 0;
2169 int sectors = r10_bio->sectors;
2170 int idx = 0;
2171 int dr = r10_bio->devs[0].devnum;
2172 int dw = r10_bio->devs[1].devnum;
2173
2174 while (sectors) {
2175 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002176 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002177 sector_t addr;
2178 int ok;
2179
2180 if (s > (PAGE_SIZE>>9))
2181 s = PAGE_SIZE >> 9;
2182
2183 rdev = conf->mirrors[dr].rdev;
2184 addr = r10_bio->devs[0].addr + sect,
2185 ok = sync_page_io(rdev,
2186 addr,
2187 s << 9,
2188 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002189 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002190 if (ok) {
2191 rdev = conf->mirrors[dw].rdev;
2192 addr = r10_bio->devs[1].addr + sect;
2193 ok = sync_page_io(rdev,
2194 addr,
2195 s << 9,
2196 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002197 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002198 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002199 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002200 if (!test_and_set_bit(WantReplacement,
2201 &rdev->flags))
2202 set_bit(MD_RECOVERY_NEEDED,
2203 &rdev->mddev->recovery);
2204 }
NeilBrown5e570282011-07-28 11:39:25 +10002205 }
2206 if (!ok) {
2207 /* We don't worry if we cannot set a bad block -
2208 * it really is bad so there is no loss in not
2209 * recording it yet
2210 */
2211 rdev_set_badblocks(rdev, addr, s, 0);
2212
2213 if (rdev != conf->mirrors[dw].rdev) {
2214 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002215 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002216 addr = r10_bio->devs[1].addr + sect;
2217 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2218 if (!ok) {
2219 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002220 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2221 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002222
2223 conf->mirrors[dw].recovery_disabled
2224 = mddev->recovery_disabled;
2225 set_bit(MD_RECOVERY_INTR,
2226 &mddev->recovery);
2227 break;
2228 }
2229 }
2230 }
2231
2232 sectors -= s;
2233 sect += s;
2234 idx++;
2235 }
2236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
NeilBrown9f2c9d12011-10-11 16:48:43 +11002238static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
NeilBrowne879a872011-10-11 16:49:02 +11002240 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002241 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002242 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
NeilBrown5e570282011-07-28 11:39:25 +10002244 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2245 fix_recovery_read_error(r10_bio);
2246 end_sync_request(r10_bio);
2247 return;
2248 }
2249
Namhyung Kimc65060a2011-07-18 17:38:49 +10002250 /*
2251 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * and submit the write request
2253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002255 wbio = r10_bio->devs[1].bio;
2256 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002257 /* Need to test wbio2->bi_end_io before we call
2258 * generic_make_request as if the former is NULL,
2259 * the latter is free to free wbio2.
2260 */
2261 if (wbio2 && !wbio2->bi_end_io)
2262 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002263 if (wbio->bi_end_io) {
2264 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002265 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002266 generic_make_request(wbio);
2267 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002268 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002269 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2270 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002271 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002272 generic_make_request(wbio2);
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276/*
Robert Becker1e509152009-12-14 12:49:58 +11002277 * Used by fix_read_error() to decay the per rdev read_errors.
2278 * We halve the read error count for every hour that has elapsed
2279 * since the last recorded read error.
2280 *
2281 */
NeilBrownfd01b882011-10-11 16:47:53 +11002282static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002283{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002284 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002285 unsigned long hours_since_last;
2286 unsigned int read_errors = atomic_read(&rdev->read_errors);
2287
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002288 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002289
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002290 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002291 /* first time we've seen a read error */
2292 rdev->last_read_error = cur_time_mon;
2293 return;
2294 }
2295
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002296 hours_since_last = (long)(cur_time_mon -
2297 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002298
2299 rdev->last_read_error = cur_time_mon;
2300
2301 /*
2302 * if hours_since_last is > the number of bits in read_errors
2303 * just set read errors to 0. We do this to avoid
2304 * overflowing the shift of read_errors by hours_since_last.
2305 */
2306 if (hours_since_last >= 8 * sizeof(read_errors))
2307 atomic_set(&rdev->read_errors, 0);
2308 else
2309 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2310}
2311
NeilBrown3cb03002011-10-11 16:45:26 +11002312static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002313 int sectors, struct page *page, int rw)
2314{
2315 sector_t first_bad;
2316 int bad_sectors;
2317
2318 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2319 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2320 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002321 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002322 /* success */
2323 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002324 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002325 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002326 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2327 set_bit(MD_RECOVERY_NEEDED,
2328 &rdev->mddev->recovery);
2329 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002330 /* need to record an error - either for the block or the device */
2331 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2332 md_error(rdev->mddev, rdev);
2333 return 0;
2334}
2335
Robert Becker1e509152009-12-14 12:49:58 +11002336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 * This is a kernel thread which:
2338 *
2339 * 1. Retries failed read operations on working mirrors.
2340 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002341 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 */
2343
NeilBrowne879a872011-10-11 16:49:02 +11002344static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002345{
2346 int sect = 0; /* Offset from r10_bio->sector */
2347 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002348 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002349 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002350 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002351
NeilBrown7c4e06f2011-05-11 14:53:17 +10002352 /* still own a reference to this rdev, so it cannot
2353 * have been cleared recently.
2354 */
2355 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002356
NeilBrown7c4e06f2011-05-11 14:53:17 +10002357 if (test_bit(Faulty, &rdev->flags))
2358 /* drive has already been failed, just ignore any
2359 more fix_read_error() attempts */
2360 return;
2361
2362 check_decay_read_errors(mddev, rdev);
2363 atomic_inc(&rdev->read_errors);
2364 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2365 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002366 bdevname(rdev->bdev, b);
2367
NeilBrown08464e02016-11-02 14:16:50 +11002368 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2369 mdname(mddev), b,
2370 atomic_read(&rdev->read_errors), max_read_errors);
2371 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2372 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002373 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002374 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002375 return;
Robert Becker1e509152009-12-14 12:49:58 +11002376 }
Robert Becker1e509152009-12-14 12:49:58 +11002377
NeilBrown6814d532006-10-03 01:15:45 -07002378 while(sectors) {
2379 int s = sectors;
2380 int sl = r10_bio->read_slot;
2381 int success = 0;
2382 int start;
2383
2384 if (s > (PAGE_SIZE>>9))
2385 s = PAGE_SIZE >> 9;
2386
2387 rcu_read_lock();
2388 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002389 sector_t first_bad;
2390 int bad_sectors;
2391
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002392 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002393 rdev = rcu_dereference(conf->mirrors[d].rdev);
2394 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002395 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002396 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002397 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2398 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002399 atomic_inc(&rdev->nr_pending);
2400 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002401 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002402 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002403 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002404 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002405 conf->tmppage,
2406 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002407 rdev_dec_pending(rdev, mddev);
2408 rcu_read_lock();
2409 if (success)
2410 break;
2411 }
2412 sl++;
2413 if (sl == conf->copies)
2414 sl = 0;
2415 } while (!success && sl != r10_bio->read_slot);
2416 rcu_read_unlock();
2417
2418 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002419 /* Cannot read from anywhere, just mark the block
2420 * as bad on the first device to discourage future
2421 * reads.
2422 */
NeilBrown6814d532006-10-03 01:15:45 -07002423 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002424 rdev = conf->mirrors[dn].rdev;
2425
2426 if (!rdev_set_badblocks(
2427 rdev,
2428 r10_bio->devs[r10_bio->read_slot].addr
2429 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002430 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002431 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002432 r10_bio->devs[r10_bio->read_slot].bio
2433 = IO_BLOCKED;
2434 }
NeilBrown6814d532006-10-03 01:15:45 -07002435 break;
2436 }
2437
2438 start = sl;
2439 /* write it back and re-read */
2440 rcu_read_lock();
2441 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002442 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002443
NeilBrown6814d532006-10-03 01:15:45 -07002444 if (sl==0)
2445 sl = conf->copies;
2446 sl--;
2447 d = r10_bio->devs[sl].devnum;
2448 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002449 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002450 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002451 !test_bit(In_sync, &rdev->flags))
2452 continue;
2453
2454 atomic_inc(&rdev->nr_pending);
2455 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002456 if (r10_sync_page_io(rdev,
2457 r10_bio->devs[sl].addr +
2458 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002459 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002460 == 0) {
2461 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002462 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2463 mdname(mddev), s,
2464 (unsigned long long)(
2465 sect +
2466 choose_data_offset(r10_bio,
2467 rdev)),
2468 bdevname(rdev->bdev, b));
2469 pr_notice("md/raid10:%s: %s: failing drive\n",
2470 mdname(mddev),
2471 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002472 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002473 rdev_dec_pending(rdev, mddev);
2474 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002475 }
2476 sl = start;
2477 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002478 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002479
NeilBrown6814d532006-10-03 01:15:45 -07002480 if (sl==0)
2481 sl = conf->copies;
2482 sl--;
2483 d = r10_bio->devs[sl].devnum;
2484 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002485 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002486 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002487 !test_bit(In_sync, &rdev->flags))
2488 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002489
NeilBrown1294b9c2011-07-28 11:39:23 +10002490 atomic_inc(&rdev->nr_pending);
2491 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002492 switch (r10_sync_page_io(rdev,
2493 r10_bio->devs[sl].addr +
2494 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002495 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002496 READ)) {
2497 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002498 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002499 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002500 mdname(mddev), s,
2501 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002502 sect +
2503 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002504 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002505 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002506 mdname(mddev),
2507 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002508 break;
2509 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002510 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002511 mdname(mddev), s,
2512 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002513 sect +
2514 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002515 bdevname(rdev->bdev, b));
2516 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002517 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002518
2519 rdev_dec_pending(rdev, mddev);
2520 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002521 }
2522 rcu_read_unlock();
2523
2524 sectors -= s;
2525 sect += s;
2526 }
2527}
2528
NeilBrown9f2c9d12011-10-11 16:48:43 +11002529static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002530{
2531 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002532 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002533 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002534 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002535 /* bio has the data to be written to slot 'i' where
2536 * we just recently had a write error.
2537 * We repeatedly clone the bio and trim down to one block,
2538 * then try the write. Where the write fails we record
2539 * a bad block.
2540 * It is conceivable that the bio doesn't exactly align with
2541 * blocks. We must handle this.
2542 *
2543 * We currently own a reference to the rdev.
2544 */
2545
2546 int block_sectors;
2547 sector_t sector;
2548 int sectors;
2549 int sect_to_write = r10_bio->sectors;
2550 int ok = 1;
2551
2552 if (rdev->badblocks.shift < 0)
2553 return 0;
2554
NeilBrownf04ebb02015-02-16 14:51:54 +11002555 block_sectors = roundup(1 << rdev->badblocks.shift,
2556 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002557 sector = r10_bio->sector;
2558 sectors = ((r10_bio->sector + block_sectors)
2559 & ~(sector_t)(block_sectors - 1))
2560 - sector;
2561
2562 while (sect_to_write) {
2563 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002564 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002565 if (sectors > sect_to_write)
2566 sectors = sect_to_write;
2567 /* Write at 'sector' for 'sectors' */
Ming Leid7a10302017-02-14 23:29:03 +08002568 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002569 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002570 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2571 wbio->bi_iter.bi_sector = wsector +
2572 choose_data_offset(r10_bio, rdev);
NeilBrownbd870a12011-07-28 11:39:24 +10002573 wbio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002574 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002575
2576 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002577 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002578 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002579 sectors, 0)
2580 && ok;
2581
2582 bio_put(wbio);
2583 sect_to_write -= sectors;
2584 sector += sectors;
2585 sectors = block_sectors;
2586 }
2587 return ok;
2588}
2589
NeilBrown9f2c9d12011-10-11 16:48:43 +11002590static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002591{
2592 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002593 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002594 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002595 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002596 char b[BDEVNAME_SIZE];
2597 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002598 int max_sectors;
NeilBrown109e3762016-11-18 13:22:04 +11002599 dev_t bio_dev;
2600 sector_t bio_last_sector;
NeilBrown560f8e52011-07-28 11:39:23 +10002601
2602 /* we got a read error. Maybe the drive is bad. Maybe just
2603 * the block and we can fix it.
2604 * We freeze all other IO, and try reading the block from
2605 * other devices. When we find one, we re-write
2606 * and check it that fixes the read error.
2607 * This is all done synchronously while the array is
2608 * frozen.
2609 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002610 bio = r10_bio->devs[slot].bio;
2611 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002612 bio_dev = bio->bi_bdev->bd_dev;
2613 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002614 bio_put(bio);
2615 r10_bio->devs[slot].bio = NULL;
2616
NeilBrown8d3ca832016-11-18 16:16:12 +11002617 if (mddev->ro)
2618 r10_bio->devs[slot].bio = IO_BLOCKED;
2619 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002620 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002621 fix_read_error(conf, mddev, r10_bio);
2622 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002623 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002624 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002625
NeilBrownabbf0982011-12-23 10:17:54 +11002626 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002627
NeilBrown7399c312011-07-28 11:39:23 +10002628read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002629 rdev = read_balance(conf, r10_bio, &max_sectors);
2630 if (rdev == NULL) {
NeilBrown08464e02016-11-02 14:16:50 +11002631 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2632 mdname(mddev), b,
2633 (unsigned long long)r10_bio->sector);
NeilBrown560f8e52011-07-28 11:39:23 +10002634 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002635 return;
2636 }
2637
Jens Axboe1eff9d32016-08-05 15:35:16 -06002638 do_sync = (r10_bio->master_bio->bi_opf & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002639 slot = r10_bio->read_slot;
NeilBrown08464e02016-11-02 14:16:50 +11002640 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2641 mdname(mddev),
2642 bdevname(rdev->bdev, b),
2643 (unsigned long long)r10_bio->sector);
Ming Leid7a10302017-02-14 23:29:03 +08002644 bio = bio_clone_fast(r10_bio->master_bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002645 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002646 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002647 r10_bio->devs[slot].rdev = rdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002648 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002649 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002650 bio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002651 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown8d3ca832016-11-18 16:16:12 +11002652 if (test_bit(FailFast, &rdev->flags) &&
2653 test_bit(R10BIO_FailFast, &r10_bio->state))
2654 bio->bi_opf |= MD_FAILFAST;
NeilBrown560f8e52011-07-28 11:39:23 +10002655 bio->bi_private = r10_bio;
2656 bio->bi_end_io = raid10_end_read_request;
NeilBrown109e3762016-11-18 13:22:04 +11002657 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2658 bio, bio_dev,
2659 bio_last_sector - r10_bio->sectors);
2660
NeilBrown7399c312011-07-28 11:39:23 +10002661 if (max_sectors < r10_bio->sectors) {
2662 /* Drat - have to split this up more */
2663 struct bio *mbio = r10_bio->master_bio;
2664 int sectors_handled =
2665 r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002666 - mbio->bi_iter.bi_sector;
NeilBrown7399c312011-07-28 11:39:23 +10002667 r10_bio->sectors = max_sectors;
2668 spin_lock_irq(&conf->device_lock);
2669 if (mbio->bi_phys_segments == 0)
2670 mbio->bi_phys_segments = 2;
2671 else
2672 mbio->bi_phys_segments++;
2673 spin_unlock_irq(&conf->device_lock);
2674 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002675
2676 r10_bio = mempool_alloc(conf->r10bio_pool,
2677 GFP_NOIO);
2678 r10_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002679 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown7399c312011-07-28 11:39:23 +10002680 r10_bio->state = 0;
2681 set_bit(R10BIO_ReadError,
2682 &r10_bio->state);
2683 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002684 r10_bio->sector = mbio->bi_iter.bi_sector
NeilBrown7399c312011-07-28 11:39:23 +10002685 + sectors_handled;
2686
2687 goto read_more;
2688 } else
2689 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002690}
2691
NeilBrowne879a872011-10-11 16:49:02 +11002692static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002693{
2694 /* Some sort of write request has finished and it
2695 * succeeded in writing where we thought there was a
2696 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002697 * Or possibly if failed and we need to record
2698 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002699 */
2700 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002701 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002702
2703 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2704 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002705 for (m = 0; m < conf->copies; m++) {
2706 int dev = r10_bio->devs[m].devnum;
2707 rdev = conf->mirrors[dev].rdev;
2708 if (r10_bio->devs[m].bio == NULL)
2709 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002710 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002711 rdev_clear_badblocks(
2712 rdev,
2713 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002714 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002715 } else {
2716 if (!rdev_set_badblocks(
2717 rdev,
2718 r10_bio->devs[m].addr,
2719 r10_bio->sectors, 0))
2720 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002721 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002722 rdev = conf->mirrors[dev].replacement;
2723 if (r10_bio->devs[m].repl_bio == NULL)
2724 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002725
2726 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002727 rdev_clear_badblocks(
2728 rdev,
2729 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002730 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002731 } else {
2732 if (!rdev_set_badblocks(
2733 rdev,
2734 r10_bio->devs[m].addr,
2735 r10_bio->sectors, 0))
2736 md_error(conf->mddev, rdev);
2737 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002738 }
NeilBrown749c55e2011-07-28 11:39:24 +10002739 put_buf(r10_bio);
2740 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002741 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002742 for (m = 0; m < conf->copies; m++) {
2743 int dev = r10_bio->devs[m].devnum;
2744 struct bio *bio = r10_bio->devs[m].bio;
2745 rdev = conf->mirrors[dev].rdev;
2746 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002747 rdev_clear_badblocks(
2748 rdev,
2749 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002750 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002751 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002752 } else if (bio != NULL && bio->bi_error) {
NeilBrown95af5872015-08-14 11:26:17 +10002753 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002754 if (!narrow_write_error(r10_bio, m)) {
2755 md_error(conf->mddev, rdev);
2756 set_bit(R10BIO_Degraded,
2757 &r10_bio->state);
2758 }
2759 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002760 }
NeilBrown475b0322011-12-23 10:17:55 +11002761 bio = r10_bio->devs[m].repl_bio;
2762 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002763 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002764 rdev_clear_badblocks(
2765 rdev,
2766 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002767 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002768 rdev_dec_pending(rdev, conf->mddev);
2769 }
NeilBrownbd870a12011-07-28 11:39:24 +10002770 }
NeilBrown95af5872015-08-14 11:26:17 +10002771 if (fail) {
2772 spin_lock_irq(&conf->device_lock);
2773 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002774 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002775 spin_unlock_irq(&conf->device_lock);
2776 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002777 } else {
2778 if (test_bit(R10BIO_WriteError,
2779 &r10_bio->state))
2780 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002781 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002782 }
NeilBrown749c55e2011-07-28 11:39:24 +10002783 }
2784}
2785
Shaohua Li4ed87312012-10-11 13:34:00 +11002786static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Shaohua Li4ed87312012-10-11 13:34:00 +11002788 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002789 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002791 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002793 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
NeilBrown95af5872015-08-14 11:26:17 +10002797 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002798 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002799 LIST_HEAD(tmp);
2800 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002801 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002802 while (!list_empty(&conf->bio_end_io_list)) {
2803 list_move(conf->bio_end_io_list.prev, &tmp);
2804 conf->nr_queued--;
2805 }
NeilBrown95af5872015-08-14 11:26:17 +10002806 }
2807 spin_unlock_irqrestore(&conf->device_lock, flags);
2808 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002809 r10_bio = list_first_entry(&tmp, struct r10bio,
2810 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002811 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002812 if (mddev->degraded)
2813 set_bit(R10BIO_Degraded, &r10_bio->state);
2814
2815 if (test_bit(R10BIO_WriteError,
2816 &r10_bio->state))
2817 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002818 raid_end_bio_io(r10_bio);
2819 }
2820 }
2821
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002822 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002824
NeilBrown0021b7b2012-07-31 09:08:14 +02002825 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002828 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002829 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002831 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002832 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002834 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 spin_unlock_irqrestore(&conf->device_lock, flags);
2836
2837 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002838 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002839 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2840 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002841 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002842 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2843 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002844 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002846 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002848 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002849 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002850 else {
2851 /* just a partial read to be scheduled from a
2852 * separate context
2853 */
2854 int slot = r10_bio->read_slot;
2855 generic_make_request(r10_bio->devs[slot].bio);
2856 }
NeilBrown4443ae12006-01-06 00:20:28 -08002857
NeilBrown1d9d5242009-10-16 15:55:32 +11002858 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002859 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002860 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002862 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
NeilBrowne879a872011-10-11 16:49:02 +11002865static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002868 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002871 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002872 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002873 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002874 if (conf->mirrors[i].replacement)
2875 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2877 if (!conf->r10buf_pool)
2878 return -ENOMEM;
2879 conf->next_resync = 0;
2880 return 0;
2881}
2882
2883/*
2884 * perform a "sync" on one "block"
2885 *
2886 * We need to make sure that no normal I/O request - particularly write
2887 * requests - conflict with active sync requests.
2888 *
2889 * This is achieved by tracking pending requests and a 'barrier' concept
2890 * that can be installed to exclude normal IO requests.
2891 *
2892 * Resync and recovery are handled very differently.
2893 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2894 *
2895 * For resync, we iterate over virtual addresses, read all copies,
2896 * and update if there are differences. If only one copy is live,
2897 * skip it.
2898 * For recovery, we iterate over physical addresses, read a good
2899 * value for each non-in_sync drive, and over-write.
2900 *
2901 * So, for recovery we may have several outstanding complex requests for a
2902 * given address, one for each out-of-sync device. We model this by allocating
2903 * a number of r10_bio structures, one for each out-of-sync device.
2904 * As we setup these structures, we collect all bio's together into a list
2905 * which we then process collectively to add pages, and then process again
2906 * to pass to generic_make_request.
2907 *
2908 * The r10_bio structures are linked using a borrowed master_bio pointer.
2909 * This link is counted in ->remaining. When the r10_bio that points to NULL
2910 * has its remaining count decremented to 0, the whole complex operation
2911 * is complete.
2912 *
2913 */
2914
Shaohua Li849674e2016-01-20 13:52:20 -08002915static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002916 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
NeilBrowne879a872011-10-11 16:49:02 +11002918 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002919 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 struct bio *biolist = NULL, *bio;
2921 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002923 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002924 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 sector_t sectors_skipped = 0;
2926 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002927 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 if (!conf->r10buf_pool)
2930 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002933 /*
2934 * Allow skipping a full rebuild for incremental assembly
2935 * of a clean array, like RAID1 does.
2936 */
2937 if (mddev->bitmap == NULL &&
2938 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002939 mddev->reshape_position == MaxSector &&
2940 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002941 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002942 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002943 conf->fullsync == 0) {
2944 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002945 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002946 }
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002949 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002950 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2951 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 max_sector = mddev->resync_max_sectors;
2953 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002954 /* If we aborted, we need to abort the
2955 * sync on the 'current' bitmap chucks (there can
2956 * be several when recovering multiple devices).
2957 * as we may have started syncing it but not finished.
2958 * We can find the current address in
2959 * mddev->curr_resync, but for recovery,
2960 * we need to convert that to several
2961 * virtual addresses.
2962 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002963 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2964 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002965 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002966 return 0;
2967 }
2968
NeilBrown6cce3b22006-01-06 00:20:16 -08002969 if (mddev->curr_resync < max_sector) { /* aborted */
2970 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2971 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2972 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002973 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002974 sector_t sect =
2975 raid10_find_virt(conf, mddev->curr_resync, i);
2976 bitmap_end_sync(mddev->bitmap, sect,
2977 &sync_blocks, 1);
2978 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002979 } else {
2980 /* completed sync */
2981 if ((!mddev->bitmap || conf->fullsync)
2982 && conf->have_replacement
2983 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2984 /* Completed a full sync so the replacements
2985 * are now fully recovered.
2986 */
NeilBrownf90145f2016-06-02 16:19:52 +10002987 rcu_read_lock();
2988 for (i = 0; i < conf->geo.raid_disks; i++) {
2989 struct md_rdev *rdev =
2990 rcu_dereference(conf->mirrors[i].replacement);
2991 if (rdev)
2992 rdev->recovery_offset = MaxSector;
2993 }
2994 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11002995 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002996 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002997 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002998 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07003000 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return sectors_skipped;
3002 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003003
3004 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3005 return reshape_request(mddev, sector_nr, skipped);
3006
NeilBrown5cf00fc2012-05-21 09:28:20 +10003007 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 /* if there has been nothing to do on any drive,
3009 * then there is nothing to do at all..
3010 */
NeilBrown57afd892005-06-21 17:17:13 -07003011 *skipped = 1;
3012 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
3014
NeilBrownc6207272008-02-06 01:39:52 -08003015 if (max_sector > mddev->resync_max)
3016 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /* make sure whole request will fit in a chunk - if chunks
3019 * are meaningful
3020 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003021 if (conf->geo.near_copies < conf->geo.raid_disks &&
3022 max_sector > (sector_nr | chunk_mask))
3023 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003025 /*
3026 * If there is non-resync activity waiting for a turn, then let it
3027 * though before starting on this new sync request.
3028 */
3029 if (conf->nr_waiting)
3030 schedule_timeout_uninterruptible(1);
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 /* Again, very different code for resync and recovery.
3033 * Both must result in an r10bio with a list of bios that
3034 * have bi_end_io, bi_sector, bi_bdev set,
3035 * and bi_private set to the r10bio.
3036 * For recovery, we may actually create several r10bios
3037 * with 2 bios in each, that correspond to the bios in the main one.
3038 * In this case, the subordinate r10bios link back through a
3039 * borrowed master_bio pointer, and the counter in the master
3040 * includes a ref from each subordinate.
3041 */
3042 /* First, we decide what to do and set ->bi_end_io
3043 * To end_sync_read if we want to read, and
3044 * end_sync_write if we will want to write.
3045 */
3046
NeilBrown6cce3b22006-01-06 00:20:16 -08003047 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3049 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003050 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 r10_bio = NULL;
3052
NeilBrown5cf00fc2012-05-21 09:28:20 +10003053 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003054 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003055 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003056 sector_t sect;
3057 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003058 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003059 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003060 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003061
NeilBrownf90145f2016-06-02 16:19:52 +10003062 rcu_read_lock();
3063 mrdev = rcu_dereference(mirror->rdev);
3064 mreplace = rcu_dereference(mirror->replacement);
3065
3066 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10003067 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10003068 test_bit(In_sync, &mrdev->flags)) &&
3069 (mreplace == NULL ||
3070 test_bit(Faulty, &mreplace->flags))) {
3071 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003072 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003073 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003074
3075 still_degraded = 0;
3076 /* want to reconstruct this device */
3077 rb2 = r10_bio;
3078 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003079 if (sect >= mddev->resync_max_sectors) {
3080 /* last stripe is not complete - don't
3081 * try to recover this sector.
3082 */
NeilBrownf90145f2016-06-02 16:19:52 +10003083 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003084 continue;
3085 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003086 if (mreplace && test_bit(Faulty, &mreplace->flags))
3087 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003088 /* Unless we are doing a full sync, or a replacement
3089 * we only need to recover the block if it is set in
3090 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003091 */
3092 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3093 &sync_blocks, 1);
3094 if (sync_blocks < max_sync)
3095 max_sync = sync_blocks;
3096 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003097 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003098 !conf->fullsync) {
3099 /* yep, skip the sync_blocks here, but don't assume
3100 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003101 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003102 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003103 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003104 continue;
3105 }
NeilBrownf90145f2016-06-02 16:19:52 +10003106 atomic_inc(&mrdev->nr_pending);
3107 if (mreplace)
3108 atomic_inc(&mreplace->nr_pending);
3109 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
NeilBrownab9d47e2011-05-11 14:54:41 +10003111 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003112 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003113 raise_barrier(conf, rb2 != NULL);
3114 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
NeilBrownab9d47e2011-05-11 14:54:41 +10003116 r10_bio->master_bio = (struct bio*)rb2;
3117 if (rb2)
3118 atomic_inc(&rb2->remaining);
3119 r10_bio->mddev = mddev;
3120 set_bit(R10BIO_IsRecover, &r10_bio->state);
3121 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003122
NeilBrownab9d47e2011-05-11 14:54:41 +10003123 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003124
NeilBrownab9d47e2011-05-11 14:54:41 +10003125 /* Need to check if the array will still be
3126 * degraded
3127 */
NeilBrownf90145f2016-06-02 16:19:52 +10003128 rcu_read_lock();
3129 for (j = 0; j < conf->geo.raid_disks; j++) {
3130 struct md_rdev *rdev = rcu_dereference(
3131 conf->mirrors[j].rdev);
3132 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003133 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
NeilBrownf90145f2016-06-02 16:19:52 +10003136 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003137
3138 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3139 &sync_blocks, still_degraded);
3140
NeilBrowne875ece2011-07-28 11:39:24 +10003141 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003142 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003143 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003144 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003145 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003146 struct md_rdev *rdev =
3147 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003148 sector_t sector, first_bad;
3149 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003150 if (!rdev ||
3151 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003152 continue;
3153 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003154 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003155 sector = r10_bio->devs[j].addr;
3156
3157 if (is_badblock(rdev, sector, max_sync,
3158 &first_bad, &bad_sectors)) {
3159 if (first_bad > sector)
3160 max_sync = first_bad - sector;
3161 else {
3162 bad_sectors -= (sector
3163 - first_bad);
3164 if (max_sync > bad_sectors)
3165 max_sync = bad_sectors;
3166 continue;
3167 }
3168 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003169 bio = r10_bio->devs[0].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003170 bio_reset(bio);
NeilBrownab9d47e2011-05-11 14:54:41 +10003171 bio->bi_next = biolist;
3172 biolist = bio;
3173 bio->bi_private = r10_bio;
3174 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003175 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003176 if (test_bit(FailFast, &rdev->flags))
3177 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003178 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003179 bio->bi_iter.bi_sector = from_addr +
3180 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003181 bio->bi_bdev = rdev->bdev;
3182 atomic_inc(&rdev->nr_pending);
3183 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003184
3185 for (k=0; k<conf->copies; k++)
3186 if (r10_bio->devs[k].devnum == i)
3187 break;
3188 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003189 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003190 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003191 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003192 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003193 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003194
NeilBrownf90145f2016-06-02 16:19:52 +10003195 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003196 bio = r10_bio->devs[1].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003197 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003198 bio->bi_next = biolist;
3199 biolist = bio;
3200 bio->bi_private = r10_bio;
3201 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003202 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003203 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003204 + mrdev->data_offset;
3205 bio->bi_bdev = mrdev->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003206 atomic_inc(&r10_bio->remaining);
3207 } else
3208 r10_bio->devs[1].bio->bi_end_io = NULL;
3209
3210 /* and maybe write to replacement */
3211 bio = r10_bio->devs[1].repl_bio;
3212 if (bio)
3213 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003214 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003215 * cannot be NULL as r10buf_pool_alloc will
3216 * have allocated it.
3217 * So the second test here is pointless.
3218 * But it keeps semantic-checkers happy, and
3219 * this comment keeps human reviewers
3220 * happy.
3221 */
NeilBrownf90145f2016-06-02 16:19:52 +10003222 if (mreplace == NULL || bio == NULL ||
3223 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003224 break;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003225 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003226 bio->bi_next = biolist;
3227 biolist = bio;
3228 bio->bi_private = r10_bio;
3229 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003230 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003231 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003232 mreplace->data_offset;
3233 bio->bi_bdev = mreplace->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003234 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
NeilBrownf90145f2016-06-02 16:19:52 +10003237 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003238 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003239 /* Cannot recover, so abort the recovery or
3240 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003241 if (any_working) {
3242 /* problem is that there are bad blocks
3243 * on other device(s)
3244 */
3245 int k;
3246 for (k = 0; k < conf->copies; k++)
3247 if (r10_bio->devs[k].devnum == i)
3248 break;
NeilBrown24afd802011-12-23 10:17:55 +11003249 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003250 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003251 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003252 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003253 r10_bio->devs[k].addr,
3254 max_sync, 0))
3255 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003256 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003257 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003258 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003259 r10_bio->devs[k].addr,
3260 max_sync, 0))
3261 any_working = 0;
3262 }
3263 if (!any_working) {
3264 if (!test_and_set_bit(MD_RECOVERY_INTR,
3265 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003266 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003267 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003268 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003269 = mddev->recovery_disabled;
3270 }
NeilBrowne8b84912014-01-06 10:35:34 +11003271 put_buf(r10_bio);
3272 if (rb2)
3273 atomic_dec(&rb2->remaining);
3274 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003275 rdev_dec_pending(mrdev, mddev);
3276 if (mreplace)
3277 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003278 break;
3279 }
NeilBrownf90145f2016-06-02 16:19:52 +10003280 rdev_dec_pending(mrdev, mddev);
3281 if (mreplace)
3282 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003283 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3284 /* Only want this if there is elsewhere to
3285 * read from. 'j' is currently the first
3286 * readable copy.
3287 */
3288 int targets = 1;
3289 for (; j < conf->copies; j++) {
3290 int d = r10_bio->devs[j].devnum;
3291 if (conf->mirrors[d].rdev &&
3292 test_bit(In_sync,
3293 &conf->mirrors[d].rdev->flags))
3294 targets++;
3295 }
3296 if (targets == 1)
3297 r10_bio->devs[0].bio->bi_opf
3298 &= ~MD_FAILFAST;
3299 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (biolist == NULL) {
3302 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003303 struct r10bio *rb2 = r10_bio;
3304 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 rb2->master_bio = NULL;
3306 put_buf(rb2);
3307 }
3308 goto giveup;
3309 }
3310 } else {
3311 /* resync. Schedule a read for every block at this virt offset */
3312 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003313
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003314 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003315
NeilBrown6cce3b22006-01-06 00:20:16 -08003316 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3317 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003318 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3319 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003320 /* We can skip this block */
3321 *skipped = 1;
3322 return sync_blocks + sectors_skipped;
3323 }
3324 if (sync_blocks < max_sync)
3325 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003327 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 r10_bio->mddev = mddev;
3330 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003331 raise_barrier(conf, 0);
3332 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334 r10_bio->master_bio = NULL;
3335 r10_bio->sector = sector_nr;
3336 set_bit(R10BIO_IsSync, &r10_bio->state);
3337 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003338 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
NeilBrown5cf00fc2012-05-21 09:28:20 +10003340 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003342 sector_t first_bad, sector;
3343 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003344 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003345
NeilBrown9ad1aef2011-12-23 10:17:55 +11003346 if (r10_bio->devs[i].repl_bio)
3347 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 bio = r10_bio->devs[i].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003350 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003351 bio->bi_error = -EIO;
NeilBrownf90145f2016-06-02 16:19:52 +10003352 rcu_read_lock();
3353 rdev = rcu_dereference(conf->mirrors[d].rdev);
3354 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3355 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003357 }
NeilBrown40c356c2011-07-28 11:39:24 +10003358 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003359 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003360 &first_bad, &bad_sectors)) {
3361 if (first_bad > sector)
3362 max_sync = first_bad - sector;
3363 else {
3364 bad_sectors -= (sector - first_bad);
3365 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003366 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003367 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003368 continue;
3369 }
3370 }
NeilBrownf90145f2016-06-02 16:19:52 +10003371 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 atomic_inc(&r10_bio->remaining);
3373 bio->bi_next = biolist;
3374 biolist = bio;
3375 bio->bi_private = r10_bio;
3376 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003377 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003378 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3379 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003380 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3381 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003383
NeilBrownf90145f2016-06-02 16:19:52 +10003384 rdev = rcu_dereference(conf->mirrors[d].replacement);
3385 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3386 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003387 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003388 }
3389 atomic_inc(&rdev->nr_pending);
3390 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003391
3392 /* Need to set up for writing to the replacement */
3393 bio = r10_bio->devs[i].repl_bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003394 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003395 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003396
3397 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003398 bio->bi_next = biolist;
3399 biolist = bio;
3400 bio->bi_private = r10_bio;
3401 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003402 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
NeilBrown1919cbb2016-11-18 16:16:12 +11003403 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3404 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003405 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3406 bio->bi_bdev = rdev->bdev;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003407 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
3410 if (count < 2) {
3411 for (i=0; i<conf->copies; i++) {
3412 int d = r10_bio->devs[i].devnum;
3413 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003414 rdev_dec_pending(conf->mirrors[d].rdev,
3415 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003416 if (r10_bio->devs[i].repl_bio &&
3417 r10_bio->devs[i].repl_bio->bi_end_io)
3418 rdev_dec_pending(
3419 conf->mirrors[d].replacement,
3420 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
3422 put_buf(r10_bio);
3423 biolist = NULL;
3424 goto giveup;
3425 }
3426 }
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003429 if (sector_nr + max_sync < max_sector)
3430 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 do {
3432 struct page *page;
3433 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (sector_nr + (len>>9) > max_sector)
3435 len = (max_sector - sector_nr) << 9;
3436 if (len == 0)
3437 break;
3438 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003439 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003441 if (bio_add_page(bio, page, len, 0))
3442 continue;
3443
3444 /* stop here */
3445 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3446 for (bio2 = biolist;
3447 bio2 && bio2 != bio;
3448 bio2 = bio2->bi_next) {
3449 /* remove last page from this bio */
3450 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003451 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06003452 bio_clear_flag(bio2, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003454 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
3456 nr_sectors += len>>9;
3457 sector_nr += len>>9;
3458 } while (biolist->bi_vcnt < RESYNC_PAGES);
3459 bio_full:
3460 r10_bio->sectors = nr_sectors;
3461
3462 while (biolist) {
3463 bio = biolist;
3464 biolist = biolist->bi_next;
3465
3466 bio->bi_next = NULL;
3467 r10_bio = bio->bi_private;
3468 r10_bio->sectors = nr_sectors;
3469
3470 if (bio->bi_end_io == end_sync_read) {
3471 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003472 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 generic_make_request(bio);
3474 }
3475 }
3476
NeilBrown57afd892005-06-21 17:17:13 -07003477 if (sectors_skipped)
3478 /* pretend they weren't skipped, it makes
3479 * no important difference in this case
3480 */
3481 md_done_sync(mddev, sectors_skipped, 1);
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 return sectors_skipped + nr_sectors;
3484 giveup:
3485 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003486 * drives must be failed or in resync, all drives
3487 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 */
NeilBrown09b40682009-02-25 13:18:47 +11003489 if (sector_nr + max_sync < max_sector)
3490 max_sector = sector_nr + max_sync;
3491
3492 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 chunks_skipped ++;
3494 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Dan Williams80c3a6c2009-03-17 18:10:40 -07003498static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003499raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003500{
3501 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003502 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003503
3504 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003505 raid_disks = min(conf->geo.raid_disks,
3506 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003507 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003508 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003509
NeilBrown5cf00fc2012-05-21 09:28:20 +10003510 size = sectors >> conf->geo.chunk_shift;
3511 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003512 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003513 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003514
NeilBrown5cf00fc2012-05-21 09:28:20 +10003515 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003516}
3517
NeilBrown6508fdb2012-05-17 10:08:45 +10003518static void calc_sectors(struct r10conf *conf, sector_t size)
3519{
3520 /* Calculate the number of sectors-per-device that will
3521 * actually be used, and set conf->dev_sectors and
3522 * conf->stride
3523 */
3524
NeilBrown5cf00fc2012-05-21 09:28:20 +10003525 size = size >> conf->geo.chunk_shift;
3526 sector_div(size, conf->geo.far_copies);
3527 size = size * conf->geo.raid_disks;
3528 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003529 /* 'size' is now the number of chunks in the array */
3530 /* calculate "used chunks per device" */
3531 size = size * conf->copies;
3532
3533 /* We need to round up when dividing by raid_disks to
3534 * get the stride size.
3535 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003536 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003537
NeilBrown5cf00fc2012-05-21 09:28:20 +10003538 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003539
NeilBrown5cf00fc2012-05-21 09:28:20 +10003540 if (conf->geo.far_offset)
3541 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003542 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003543 sector_div(size, conf->geo.far_copies);
3544 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003545 }
3546}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003547
NeilBrowndeb200d2012-05-21 09:28:33 +10003548enum geo_type {geo_new, geo_old, geo_start};
3549static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3550{
3551 int nc, fc, fo;
3552 int layout, chunk, disks;
3553 switch (new) {
3554 case geo_old:
3555 layout = mddev->layout;
3556 chunk = mddev->chunk_sectors;
3557 disks = mddev->raid_disks - mddev->delta_disks;
3558 break;
3559 case geo_new:
3560 layout = mddev->new_layout;
3561 chunk = mddev->new_chunk_sectors;
3562 disks = mddev->raid_disks;
3563 break;
3564 default: /* avoid 'may be unused' warnings */
3565 case geo_start: /* new when starting reshape - raid_disks not
3566 * updated yet. */
3567 layout = mddev->new_layout;
3568 chunk = mddev->new_chunk_sectors;
3569 disks = mddev->raid_disks + mddev->delta_disks;
3570 break;
3571 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003572 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003573 return -1;
3574 if (chunk < (PAGE_SIZE >> 9) ||
3575 !is_power_of_2(chunk))
3576 return -2;
3577 nc = layout & 255;
3578 fc = (layout >> 8) & 255;
3579 fo = layout & (1<<16);
3580 geo->raid_disks = disks;
3581 geo->near_copies = nc;
3582 geo->far_copies = fc;
3583 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003584 switch (layout >> 17) {
3585 case 0: /* original layout. simple but not always optimal */
3586 geo->far_set_size = disks;
3587 break;
3588 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3589 * actually using this, but leave code here just in case.*/
3590 geo->far_set_size = disks/fc;
3591 WARN(geo->far_set_size < fc,
3592 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3593 break;
3594 case 2: /* "improved" layout fixed to match documentation */
3595 geo->far_set_size = fc * nc;
3596 break;
3597 default: /* Not a valid layout */
3598 return -1;
3599 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003600 geo->chunk_mask = chunk - 1;
3601 geo->chunk_shift = ffz(~chunk);
3602 return nc*fc;
3603}
3604
NeilBrowne879a872011-10-11 16:49:02 +11003605static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
NeilBrowne879a872011-10-11 16:49:02 +11003607 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003608 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003609 struct geom geo;
3610 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
NeilBrowndeb200d2012-05-21 09:28:33 +10003612 copies = setup_geo(&geo, mddev, geo_new);
3613
3614 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003615 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3616 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
NeilBrown2604b702006-01-06 00:20:36 -08003619
NeilBrowndeb200d2012-05-21 09:28:33 +10003620 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003621 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3622 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 goto out;
3624 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003625
3626 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003627 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003628 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003630
NeilBrown3ea7daa2012-05-22 13:53:47 +10003631 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003632 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003633 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003634 GFP_KERNEL);
3635 if (!conf->mirrors)
3636 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003637
3638 conf->tmppage = alloc_page(GFP_KERNEL);
3639 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003640 goto out;
3641
NeilBrowndeb200d2012-05-21 09:28:33 +10003642 conf->geo = geo;
3643 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003644 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3645 r10bio_pool_free, conf);
3646 if (!conf->r10bio_pool)
3647 goto out;
3648
NeilBrown6508fdb2012-05-17 10:08:45 +10003649 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003650 if (mddev->reshape_position == MaxSector) {
3651 conf->prev = conf->geo;
3652 conf->reshape_progress = MaxSector;
3653 } else {
3654 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3655 err = -EINVAL;
3656 goto out;
3657 }
3658 conf->reshape_progress = mddev->reshape_position;
3659 if (conf->prev.far_offset)
3660 conf->prev.stride = 1 << conf->prev.chunk_shift;
3661 else
3662 /* far_copies must be 1 */
3663 conf->prev.stride = conf->dev_sectors;
3664 }
NeilBrown299b0682015-07-06 17:37:49 +10003665 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003666 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003667 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003668 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003669
3670 spin_lock_init(&conf->resync_lock);
3671 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003672 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003673
NeilBrown02326052012-07-03 15:56:52 +10003674 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003675 if (!conf->thread)
3676 goto out;
3677
Trela, Maciejdab8b292010-03-08 16:02:45 +11003678 conf->mddev = mddev;
3679 return conf;
3680
3681 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003682 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003683 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003684 kfree(conf->mirrors);
3685 safe_put_page(conf->tmppage);
3686 kfree(conf);
3687 }
3688 return ERR_PTR(err);
3689}
3690
Shaohua Li849674e2016-01-20 13:52:20 -08003691static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003692{
NeilBrowne879a872011-10-11 16:49:02 +11003693 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003694 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003695 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003696 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003697 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003698 sector_t min_offset_diff = 0;
3699 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003700 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003701
3702 if (mddev->private == NULL) {
3703 conf = setup_conf(mddev);
3704 if (IS_ERR(conf))
3705 return PTR_ERR(conf);
3706 mddev->private = conf;
3707 }
3708 conf = mddev->private;
3709 if (!conf)
3710 goto out;
3711
Trela, Maciejdab8b292010-03-08 16:02:45 +11003712 mddev->thread = conf->thread;
3713 conf->thread = NULL;
3714
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003715 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003716 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003717 blk_queue_max_discard_sectors(mddev->queue,
3718 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003719 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003720 blk_queue_io_min(mddev->queue, chunk_size);
3721 if (conf->geo.raid_disks % conf->geo.near_copies)
3722 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3723 else
3724 blk_queue_io_opt(mddev->queue, chunk_size *
3725 (conf->geo.raid_disks / conf->geo.near_copies));
3726 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003727
NeilBrowndafb20f2012-03-19 12:46:39 +11003728 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003729 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003730 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003733 if (disk_idx < 0)
3734 continue;
3735 if (disk_idx >= conf->geo.raid_disks &&
3736 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 continue;
3738 disk = conf->mirrors + disk_idx;
3739
NeilBrown56a2559b2011-12-23 10:17:55 +11003740 if (test_bit(Replacement, &rdev->flags)) {
3741 if (disk->replacement)
3742 goto out_free_conf;
3743 disk->replacement = rdev;
3744 } else {
3745 if (disk->rdev)
3746 goto out_free_conf;
3747 disk->rdev = rdev;
3748 }
NeilBrownaba336b2012-05-31 15:39:11 +10003749 q = bdev_get_queue(rdev->bdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003750 diff = (rdev->new_data_offset - rdev->data_offset);
3751 if (!mddev->reshape_backwards)
3752 diff = -diff;
3753 if (diff < 0)
3754 diff = 0;
3755 if (first || diff < min_offset_diff)
3756 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11003757
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003758 if (mddev->gendisk)
3759 disk_stack_limits(mddev->gendisk, rdev->bdev,
3760 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003763
3764 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3765 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003767
Jonathan Brassowed30be02012-10-31 11:42:30 +11003768 if (mddev->queue) {
3769 if (discard_supported)
3770 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3771 mddev->queue);
3772 else
3773 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3774 mddev->queue);
3775 }
NeilBrown6d508242005-09-09 16:24:03 -07003776 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003777 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003778 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003779 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 goto out_free_conf;
3781 }
3782
NeilBrown3ea7daa2012-05-22 13:53:47 +10003783 if (conf->reshape_progress != MaxSector) {
3784 /* must ensure that shape change is supported */
3785 if (conf->geo.far_copies != 1 &&
3786 conf->geo.far_offset == 0)
3787 goto out_free_conf;
3788 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003789 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003790 goto out_free_conf;
3791 }
3792
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003794 for (i = 0;
3795 i < conf->geo.raid_disks
3796 || i < conf->prev.raid_disks;
3797 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 disk = conf->mirrors + i;
3800
NeilBrown56a2559b2011-12-23 10:17:55 +11003801 if (!disk->rdev && disk->replacement) {
3802 /* The replacement is all we have - use it */
3803 disk->rdev = disk->replacement;
3804 disk->replacement = NULL;
3805 clear_bit(Replacement, &disk->rdev->flags);
3806 }
3807
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003808 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003809 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 disk->head_position = 0;
3811 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003812 if (disk->rdev &&
3813 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003814 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
NeilBrownd890fa22011-10-26 11:54:39 +11003816 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818
Andre Noll8c6ac8682009-06-18 08:48:06 +10003819 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003820 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3821 mdname(mddev));
3822 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003823 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3824 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 /*
3826 * Ok, everything is just fine now
3827 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003828 mddev->dev_sectors = conf->dev_sectors;
3829 size = raid10_size(mddev, 0, 0);
3830 md_set_array_sectors(mddev, size);
3831 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003832 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003834 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003835 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003836 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003837
3838 /* Calculate max read-ahead size.
3839 * We need to readahead at least twice a whole stripe....
3840 * maybe...
3841 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003842 stripe /= conf->geo.near_copies;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003843 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3844 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 }
3846
Martin K. Petersena91a2782011-03-17 11:11:05 +01003847 if (md_integrity_register(mddev))
3848 goto out_free_conf;
3849
NeilBrown3ea7daa2012-05-22 13:53:47 +10003850 if (conf->reshape_progress != MaxSector) {
3851 unsigned long before_length, after_length;
3852
3853 before_length = ((1 << conf->prev.chunk_shift) *
3854 conf->prev.far_copies);
3855 after_length = ((1 << conf->geo.chunk_shift) *
3856 conf->geo.far_copies);
3857
3858 if (max(before_length, after_length) > min_offset_diff) {
3859 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003860 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003861 goto out_free_conf;
3862 }
3863 conf->offset_diff = min_offset_diff;
3864
NeilBrown3ea7daa2012-05-22 13:53:47 +10003865 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3866 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3867 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3868 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3869 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3870 "reshape");
3871 }
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return 0;
3874
3875out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003876 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003877 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003878 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003879 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 kfree(conf);
3881 mddev->private = NULL;
3882out:
3883 return -EIO;
3884}
3885
NeilBrownafa0f552014-12-15 12:56:58 +11003886static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
NeilBrownafa0f552014-12-15 12:56:58 +11003888 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Julia Lawall644df1a2015-09-13 14:15:10 +02003890 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003891 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003892 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003893 kfree(conf->mirrors_old);
3894 kfree(conf->mirrors_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
NeilBrownfd01b882011-10-11 16:47:53 +11003898static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003899{
NeilBrowne879a872011-10-11 16:49:02 +11003900 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003901
3902 switch(state) {
3903 case 1:
3904 raise_barrier(conf, 0);
3905 break;
3906 case 0:
3907 lower_barrier(conf);
3908 break;
3909 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003910}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
NeilBrown006a09a2012-03-19 12:46:40 +11003912static int raid10_resize(struct mddev *mddev, sector_t sectors)
3913{
3914 /* Resize of 'far' arrays is not supported.
3915 * For 'near' and 'offset' arrays we can set the
3916 * number of sectors used to be an appropriate multiple
3917 * of the chunk size.
3918 * For 'offset', this is far_copies*chunksize.
3919 * For 'near' the multiplier is the LCM of
3920 * near_copies and raid_disks.
3921 * So if far_copies > 1 && !far_offset, fail.
3922 * Else find LCM(raid_disks, near_copy)*far_copies and
3923 * multiply by chunk_size. Then round to this number.
3924 * This is mostly done by raid10_size()
3925 */
3926 struct r10conf *conf = mddev->private;
3927 sector_t oldsize, size;
3928
NeilBrownf8c9e742012-05-21 09:28:33 +10003929 if (mddev->reshape_position != MaxSector)
3930 return -EBUSY;
3931
NeilBrown5cf00fc2012-05-21 09:28:20 +10003932 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003933 return -EINVAL;
3934
3935 oldsize = raid10_size(mddev, 0, 0);
3936 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003937 if (mddev->external_size &&
3938 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003939 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003940 if (mddev->bitmap) {
3941 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3942 if (ret)
3943 return ret;
3944 }
3945 md_set_array_sectors(mddev, size);
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02003946 if (mddev->queue) {
3947 set_capacity(mddev->gendisk, mddev->array_sectors);
3948 revalidate_disk(mddev->gendisk);
3949 }
NeilBrown006a09a2012-03-19 12:46:40 +11003950 if (sectors > mddev->dev_sectors &&
3951 mddev->recovery_cp > oldsize) {
3952 mddev->recovery_cp = oldsize;
3953 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3954 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003955 calc_sectors(conf, sectors);
3956 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003957 mddev->resync_max_sectors = size;
3958 return 0;
3959}
3960
NeilBrown53a6ab42015-02-12 14:09:57 +11003961static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003962{
NeilBrown3cb03002011-10-11 16:45:26 +11003963 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003964 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003965
3966 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003967 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3968 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003969 return ERR_PTR(-EINVAL);
3970 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003971 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003972
Trela, Maciejdab8b292010-03-08 16:02:45 +11003973 /* Set new parameters */
3974 mddev->new_level = 10;
3975 /* new layout: far_copies = 1, near_copies = 2 */
3976 mddev->new_layout = (1<<8) + 2;
3977 mddev->new_chunk_sectors = mddev->chunk_sectors;
3978 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003979 mddev->raid_disks *= 2;
3980 /* make sure it will be not marked as dirty */
3981 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003982 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003983
3984 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003985 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003986 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003987 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003988 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003989 rdev->sectors = size;
3990 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003991 conf->barrier = 1;
3992 }
3993
Trela, Maciejdab8b292010-03-08 16:02:45 +11003994 return conf;
3995}
3996
NeilBrownfd01b882011-10-11 16:47:53 +11003997static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003998{
NeilBrowne373ab12011-10-11 16:48:59 +11003999 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004000
4001 /* raid10 can take over:
4002 * raid0 - providing it has only two drives
4003 */
4004 if (mddev->level == 0) {
4005 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11004006 raid0_conf = mddev->private;
4007 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11004008 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4009 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004010 return ERR_PTR(-EINVAL);
4011 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004012 return raid10_takeover_raid0(mddev,
4013 raid0_conf->strip_zone->zone_end,
4014 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004015 }
4016 return ERR_PTR(-EINVAL);
4017}
4018
NeilBrown3ea7daa2012-05-22 13:53:47 +10004019static int raid10_check_reshape(struct mddev *mddev)
4020{
4021 /* Called when there is a request to change
4022 * - layout (to ->new_layout)
4023 * - chunk size (to ->new_chunk_sectors)
4024 * - raid_disks (by delta_disks)
4025 * or when trying to restart a reshape that was ongoing.
4026 *
4027 * We need to validate the request and possibly allocate
4028 * space if that might be an issue later.
4029 *
4030 * Currently we reject any reshape of a 'far' mode array,
4031 * allow chunk size to change if new is generally acceptable,
4032 * allow raid_disks to increase, and allow
4033 * a switch between 'near' mode and 'offset' mode.
4034 */
4035 struct r10conf *conf = mddev->private;
4036 struct geom geo;
4037
4038 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4039 return -EINVAL;
4040
4041 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4042 /* mustn't change number of copies */
4043 return -EINVAL;
4044 if (geo.far_copies > 1 && !geo.far_offset)
4045 /* Cannot switch to 'far' mode */
4046 return -EINVAL;
4047
4048 if (mddev->array_sectors & geo.chunk_mask)
4049 /* not factor of array size */
4050 return -EINVAL;
4051
NeilBrown3ea7daa2012-05-22 13:53:47 +10004052 if (!enough(conf, -1))
4053 return -EINVAL;
4054
4055 kfree(conf->mirrors_new);
4056 conf->mirrors_new = NULL;
4057 if (mddev->delta_disks > 0) {
4058 /* allocate new 'mirrors' list */
4059 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004060 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004061 *(mddev->raid_disks +
4062 mddev->delta_disks),
4063 GFP_KERNEL);
4064 if (!conf->mirrors_new)
4065 return -ENOMEM;
4066 }
4067 return 0;
4068}
4069
4070/*
4071 * Need to check if array has failed when deciding whether to:
4072 * - start an array
4073 * - remove non-faulty devices
4074 * - add a spare
4075 * - allow a reshape
4076 * This determination is simple when no reshape is happening.
4077 * However if there is a reshape, we need to carefully check
4078 * both the before and after sections.
4079 * This is because some failed devices may only affect one
4080 * of the two sections, and some non-in_sync devices may
4081 * be insync in the section most affected by failed devices.
4082 */
4083static int calc_degraded(struct r10conf *conf)
4084{
4085 int degraded, degraded2;
4086 int i;
4087
4088 rcu_read_lock();
4089 degraded = 0;
4090 /* 'prev' section first */
4091 for (i = 0; i < conf->prev.raid_disks; i++) {
4092 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4093 if (!rdev || test_bit(Faulty, &rdev->flags))
4094 degraded++;
4095 else if (!test_bit(In_sync, &rdev->flags))
4096 /* When we can reduce the number of devices in
4097 * an array, this might not contribute to
4098 * 'degraded'. It does now.
4099 */
4100 degraded++;
4101 }
4102 rcu_read_unlock();
4103 if (conf->geo.raid_disks == conf->prev.raid_disks)
4104 return degraded;
4105 rcu_read_lock();
4106 degraded2 = 0;
4107 for (i = 0; i < conf->geo.raid_disks; i++) {
4108 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4109 if (!rdev || test_bit(Faulty, &rdev->flags))
4110 degraded2++;
4111 else if (!test_bit(In_sync, &rdev->flags)) {
4112 /* If reshape is increasing the number of devices,
4113 * this section has already been recovered, so
4114 * it doesn't contribute to degraded.
4115 * else it does.
4116 */
4117 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4118 degraded2++;
4119 }
4120 }
4121 rcu_read_unlock();
4122 if (degraded2 > degraded)
4123 return degraded2;
4124 return degraded;
4125}
4126
4127static int raid10_start_reshape(struct mddev *mddev)
4128{
4129 /* A 'reshape' has been requested. This commits
4130 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4131 * This also checks if there are enough spares and adds them
4132 * to the array.
4133 * We currently require enough spares to make the final
4134 * array non-degraded. We also require that the difference
4135 * between old and new data_offset - on each device - is
4136 * enough that we never risk over-writing.
4137 */
4138
4139 unsigned long before_length, after_length;
4140 sector_t min_offset_diff = 0;
4141 int first = 1;
4142 struct geom new;
4143 struct r10conf *conf = mddev->private;
4144 struct md_rdev *rdev;
4145 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004146 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004147
4148 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4149 return -EBUSY;
4150
4151 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4152 return -EINVAL;
4153
4154 before_length = ((1 << conf->prev.chunk_shift) *
4155 conf->prev.far_copies);
4156 after_length = ((1 << conf->geo.chunk_shift) *
4157 conf->geo.far_copies);
4158
4159 rdev_for_each(rdev, mddev) {
4160 if (!test_bit(In_sync, &rdev->flags)
4161 && !test_bit(Faulty, &rdev->flags))
4162 spares++;
4163 if (rdev->raid_disk >= 0) {
4164 long long diff = (rdev->new_data_offset
4165 - rdev->data_offset);
4166 if (!mddev->reshape_backwards)
4167 diff = -diff;
4168 if (diff < 0)
4169 diff = 0;
4170 if (first || diff < min_offset_diff)
4171 min_offset_diff = diff;
4172 }
4173 }
4174
4175 if (max(before_length, after_length) > min_offset_diff)
4176 return -EINVAL;
4177
4178 if (spares < mddev->delta_disks)
4179 return -EINVAL;
4180
4181 conf->offset_diff = min_offset_diff;
4182 spin_lock_irq(&conf->device_lock);
4183 if (conf->mirrors_new) {
4184 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004185 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004186 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004187 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004188 conf->mirrors_old = conf->mirrors;
4189 conf->mirrors = conf->mirrors_new;
4190 conf->mirrors_new = NULL;
4191 }
4192 setup_geo(&conf->geo, mddev, geo_start);
4193 smp_mb();
4194 if (mddev->reshape_backwards) {
4195 sector_t size = raid10_size(mddev, 0, 0);
4196 if (size < mddev->array_sectors) {
4197 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004198 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4199 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004200 return -EINVAL;
4201 }
4202 mddev->resync_max_sectors = size;
4203 conf->reshape_progress = size;
4204 } else
4205 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004206 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004207 spin_unlock_irq(&conf->device_lock);
4208
NeilBrownbb63a702012-05-22 13:55:28 +10004209 if (mddev->delta_disks && mddev->bitmap) {
4210 ret = bitmap_resize(mddev->bitmap,
4211 raid10_size(mddev, 0,
4212 conf->geo.raid_disks),
4213 0, 0);
4214 if (ret)
4215 goto abort;
4216 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004217 if (mddev->delta_disks > 0) {
4218 rdev_for_each(rdev, mddev)
4219 if (rdev->raid_disk < 0 &&
4220 !test_bit(Faulty, &rdev->flags)) {
4221 if (raid10_add_disk(mddev, rdev) == 0) {
4222 if (rdev->raid_disk >=
4223 conf->prev.raid_disks)
4224 set_bit(In_sync, &rdev->flags);
4225 else
4226 rdev->recovery_offset = 0;
4227
4228 if (sysfs_link_rdev(mddev, rdev))
4229 /* Failure here is OK */;
4230 }
4231 } else if (rdev->raid_disk >= conf->prev.raid_disks
4232 && !test_bit(Faulty, &rdev->flags)) {
4233 /* This is a spare that was manually added */
4234 set_bit(In_sync, &rdev->flags);
4235 }
4236 }
4237 /* When a reshape changes the number of devices,
4238 * ->degraded is measured against the larger of the
4239 * pre and post numbers.
4240 */
4241 spin_lock_irq(&conf->device_lock);
4242 mddev->degraded = calc_degraded(conf);
4243 spin_unlock_irq(&conf->device_lock);
4244 mddev->raid_disks = conf->geo.raid_disks;
4245 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004246 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004247
4248 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4249 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004250 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004251 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4252 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4253
4254 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4255 "reshape");
4256 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004257 ret = -EAGAIN;
4258 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004259 }
4260 conf->reshape_checkpoint = jiffies;
4261 md_wakeup_thread(mddev->sync_thread);
4262 md_new_event(mddev);
4263 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004264
4265abort:
4266 mddev->recovery = 0;
4267 spin_lock_irq(&conf->device_lock);
4268 conf->geo = conf->prev;
4269 mddev->raid_disks = conf->geo.raid_disks;
4270 rdev_for_each(rdev, mddev)
4271 rdev->new_data_offset = rdev->data_offset;
4272 smp_wmb();
4273 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004274 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004275 mddev->reshape_position = MaxSector;
4276 spin_unlock_irq(&conf->device_lock);
4277 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004278}
4279
4280/* Calculate the last device-address that could contain
4281 * any block from the chunk that includes the array-address 's'
4282 * and report the next address.
4283 * i.e. the address returned will be chunk-aligned and after
4284 * any data that is in the chunk containing 's'.
4285 */
4286static sector_t last_dev_address(sector_t s, struct geom *geo)
4287{
4288 s = (s | geo->chunk_mask) + 1;
4289 s >>= geo->chunk_shift;
4290 s *= geo->near_copies;
4291 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4292 s *= geo->far_copies;
4293 s <<= geo->chunk_shift;
4294 return s;
4295}
4296
4297/* Calculate the first device-address that could contain
4298 * any block from the chunk that includes the array-address 's'.
4299 * This too will be the start of a chunk
4300 */
4301static sector_t first_dev_address(sector_t s, struct geom *geo)
4302{
4303 s >>= geo->chunk_shift;
4304 s *= geo->near_copies;
4305 sector_div(s, geo->raid_disks);
4306 s *= geo->far_copies;
4307 s <<= geo->chunk_shift;
4308 return s;
4309}
4310
4311static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4312 int *skipped)
4313{
4314 /* We simply copy at most one chunk (smallest of old and new)
4315 * at a time, possibly less if that exceeds RESYNC_PAGES,
4316 * or we hit a bad block or something.
4317 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004318 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004319 * can record any location.
4320 *
4321 * If we will want to write to a location that isn't
4322 * yet recorded as 'safe' (i.e. in metadata on disk) then
4323 * we need to flush all reshape requests and update the metadata.
4324 *
4325 * When reshaping forwards (e.g. to more devices), we interpret
4326 * 'safe' as the earliest block which might not have been copied
4327 * down yet. We divide this by previous stripe size and multiply
4328 * by previous stripe length to get lowest device offset that we
4329 * cannot write to yet.
4330 * We interpret 'sector_nr' as an address that we want to write to.
4331 * From this we use last_device_address() to find where we might
4332 * write to, and first_device_address on the 'safe' position.
4333 * If this 'next' write position is after the 'safe' position,
4334 * we must update the metadata to increase the 'safe' position.
4335 *
4336 * When reshaping backwards, we round in the opposite direction
4337 * and perform the reverse test: next write position must not be
4338 * less than current safe position.
4339 *
4340 * In all this the minimum difference in data offsets
4341 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004342 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004343 *
4344 * We need to prepare all the bios here before we start any IO
4345 * to ensure the size we choose is acceptable to all devices.
4346 * The means one for each copy for write-out and an extra one for
4347 * read-in.
4348 * We store the read-in bio in ->master_bio and the others in
4349 * ->devs[x].bio and ->devs[x].repl_bio.
4350 */
4351 struct r10conf *conf = mddev->private;
4352 struct r10bio *r10_bio;
4353 sector_t next, safe, last;
4354 int max_sectors;
4355 int nr_sectors;
4356 int s;
4357 struct md_rdev *rdev;
4358 int need_flush = 0;
4359 struct bio *blist;
4360 struct bio *bio, *read_bio;
4361 int sectors_done = 0;
4362
4363 if (sector_nr == 0) {
4364 /* If restarting in the middle, skip the initial sectors */
4365 if (mddev->reshape_backwards &&
4366 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4367 sector_nr = (raid10_size(mddev, 0, 0)
4368 - conf->reshape_progress);
4369 } else if (!mddev->reshape_backwards &&
4370 conf->reshape_progress > 0)
4371 sector_nr = conf->reshape_progress;
4372 if (sector_nr) {
4373 mddev->curr_resync_completed = sector_nr;
4374 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4375 *skipped = 1;
4376 return sector_nr;
4377 }
4378 }
4379
4380 /* We don't use sector_nr to track where we are up to
4381 * as that doesn't work well for ->reshape_backwards.
4382 * So just use ->reshape_progress.
4383 */
4384 if (mddev->reshape_backwards) {
4385 /* 'next' is the earliest device address that we might
4386 * write to for this chunk in the new layout
4387 */
4388 next = first_dev_address(conf->reshape_progress - 1,
4389 &conf->geo);
4390
4391 /* 'safe' is the last device address that we might read from
4392 * in the old layout after a restart
4393 */
4394 safe = last_dev_address(conf->reshape_safe - 1,
4395 &conf->prev);
4396
4397 if (next + conf->offset_diff < safe)
4398 need_flush = 1;
4399
4400 last = conf->reshape_progress - 1;
4401 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4402 & conf->prev.chunk_mask);
4403 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4404 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4405 } else {
4406 /* 'next' is after the last device address that we
4407 * might write to for this chunk in the new layout
4408 */
4409 next = last_dev_address(conf->reshape_progress, &conf->geo);
4410
4411 /* 'safe' is the earliest device address that we might
4412 * read from in the old layout after a restart
4413 */
4414 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4415
4416 /* Need to update metadata if 'next' might be beyond 'safe'
4417 * as that would possibly corrupt data
4418 */
4419 if (next > safe + conf->offset_diff)
4420 need_flush = 1;
4421
4422 sector_nr = conf->reshape_progress;
4423 last = sector_nr | (conf->geo.chunk_mask
4424 & conf->prev.chunk_mask);
4425
4426 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4427 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4428 }
4429
4430 if (need_flush ||
4431 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4432 /* Need to update reshape_position in metadata */
4433 wait_barrier(conf);
4434 mddev->reshape_position = conf->reshape_progress;
4435 if (mddev->reshape_backwards)
4436 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4437 - conf->reshape_progress;
4438 else
4439 mddev->curr_resync_completed = conf->reshape_progress;
4440 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004441 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004442 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004443 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004444 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4445 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4446 allow_barrier(conf);
4447 return sectors_done;
4448 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004449 conf->reshape_safe = mddev->reshape_position;
4450 allow_barrier(conf);
4451 }
4452
4453read_more:
4454 /* Now schedule reads for blocks from sector_nr to last */
4455 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004456 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004457 raise_barrier(conf, sectors_done != 0);
4458 atomic_set(&r10_bio->remaining, 0);
4459 r10_bio->mddev = mddev;
4460 r10_bio->sector = sector_nr;
4461 set_bit(R10BIO_IsReshape, &r10_bio->state);
4462 r10_bio->sectors = last - sector_nr + 1;
4463 rdev = read_balance(conf, r10_bio, &max_sectors);
4464 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4465
4466 if (!rdev) {
4467 /* Cannot read from here, so need to record bad blocks
4468 * on all the target devices.
4469 */
4470 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004471 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004472 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4473 return sectors_done;
4474 }
4475
4476 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4477
4478 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004479 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004480 + rdev->data_offset);
4481 read_bio->bi_private = r10_bio;
4482 read_bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004483 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004484 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004485 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004486 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004487 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004488 r10_bio->master_bio = read_bio;
4489 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4490
4491 /* Now find the locations in the new layout */
4492 __raid10_find_phys(&conf->geo, r10_bio);
4493
4494 blist = read_bio;
4495 read_bio->bi_next = NULL;
4496
NeilBrownd094d682016-06-02 16:19:52 +10004497 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004498 for (s = 0; s < conf->copies*2; s++) {
4499 struct bio *b;
4500 int d = r10_bio->devs[s/2].devnum;
4501 struct md_rdev *rdev2;
4502 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004503 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004504 b = r10_bio->devs[s/2].repl_bio;
4505 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004506 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004507 b = r10_bio->devs[s/2].bio;
4508 }
4509 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4510 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004511
4512 bio_reset(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004513 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004514 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4515 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004516 b->bi_private = r10_bio;
4517 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004518 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004519 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004520 blist = b;
4521 }
4522
4523 /* Now add as many pages as possible to all of these bios. */
4524
4525 nr_sectors = 0;
4526 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4527 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4528 int len = (max_sectors - s) << 9;
4529 if (len > PAGE_SIZE)
4530 len = PAGE_SIZE;
4531 for (bio = blist; bio ; bio = bio->bi_next) {
4532 struct bio *bio2;
4533 if (bio_add_page(bio, page, len, 0))
4534 continue;
4535
4536 /* Didn't fit, must stop */
4537 for (bio2 = blist;
4538 bio2 && bio2 != bio;
4539 bio2 = bio2->bi_next) {
4540 /* Remove last page from this bio */
4541 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004542 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004543 bio_clear_flag(bio2, BIO_SEG_VALID);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004544 }
4545 goto bio_full;
4546 }
4547 sector_nr += len >> 9;
4548 nr_sectors += len >> 9;
4549 }
4550bio_full:
NeilBrownd094d682016-06-02 16:19:52 +10004551 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004552 r10_bio->sectors = nr_sectors;
4553
4554 /* Now submit the read */
4555 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4556 atomic_inc(&r10_bio->remaining);
4557 read_bio->bi_next = NULL;
4558 generic_make_request(read_bio);
4559 sector_nr += nr_sectors;
4560 sectors_done += nr_sectors;
4561 if (sector_nr <= last)
4562 goto read_more;
4563
4564 /* Now that we have done the whole section we can
4565 * update reshape_progress
4566 */
4567 if (mddev->reshape_backwards)
4568 conf->reshape_progress -= sectors_done;
4569 else
4570 conf->reshape_progress += sectors_done;
4571
4572 return sectors_done;
4573}
4574
4575static void end_reshape_request(struct r10bio *r10_bio);
4576static int handle_reshape_read_error(struct mddev *mddev,
4577 struct r10bio *r10_bio);
4578static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4579{
4580 /* Reshape read completed. Hopefully we have a block
4581 * to write out.
4582 * If we got a read error then we do sync 1-page reads from
4583 * elsewhere until we find the data - or give up.
4584 */
4585 struct r10conf *conf = mddev->private;
4586 int s;
4587
4588 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4589 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4590 /* Reshape has been aborted */
4591 md_done_sync(mddev, r10_bio->sectors, 0);
4592 return;
4593 }
4594
4595 /* We definitely have the data in the pages, schedule the
4596 * writes.
4597 */
4598 atomic_set(&r10_bio->remaining, 1);
4599 for (s = 0; s < conf->copies*2; s++) {
4600 struct bio *b;
4601 int d = r10_bio->devs[s/2].devnum;
4602 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004603 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004604 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004605 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004606 b = r10_bio->devs[s/2].repl_bio;
4607 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004608 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004609 b = r10_bio->devs[s/2].bio;
4610 }
NeilBrownd094d682016-06-02 16:19:52 +10004611 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4612 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004613 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004614 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004615 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004616 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004617 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4618 atomic_inc(&r10_bio->remaining);
4619 b->bi_next = NULL;
4620 generic_make_request(b);
4621 }
4622 end_reshape_request(r10_bio);
4623}
4624
4625static void end_reshape(struct r10conf *conf)
4626{
4627 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4628 return;
4629
4630 spin_lock_irq(&conf->device_lock);
4631 conf->prev = conf->geo;
4632 md_finish_reshape(conf->mddev);
4633 smp_wmb();
4634 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004635 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004636 spin_unlock_irq(&conf->device_lock);
4637
4638 /* read-ahead size must cover two whole stripes, which is
4639 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4640 */
4641 if (conf->mddev->queue) {
4642 int stripe = conf->geo.raid_disks *
4643 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4644 stripe /= conf->geo.near_copies;
4645 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4646 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4647 }
4648 conf->fullsync = 0;
4649}
4650
NeilBrown3ea7daa2012-05-22 13:53:47 +10004651static int handle_reshape_read_error(struct mddev *mddev,
4652 struct r10bio *r10_bio)
4653{
4654 /* Use sync reads to get the blocks from somewhere else */
4655 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004656 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004657 struct {
4658 struct r10bio r10_bio;
4659 struct r10dev devs[conf->copies];
4660 } on_stack;
4661 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004662 int slot = 0;
4663 int idx = 0;
4664 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4665
NeilBrowne0ee7782012-08-18 09:51:42 +10004666 r10b->sector = r10_bio->sector;
4667 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004668
4669 while (sectors) {
4670 int s = sectors;
4671 int success = 0;
4672 int first_slot = slot;
4673
4674 if (s > (PAGE_SIZE >> 9))
4675 s = PAGE_SIZE >> 9;
4676
NeilBrownd094d682016-06-02 16:19:52 +10004677 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004678 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004679 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004680 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004681 sector_t addr;
4682 if (rdev == NULL ||
4683 test_bit(Faulty, &rdev->flags) ||
4684 !test_bit(In_sync, &rdev->flags))
4685 goto failed;
4686
NeilBrowne0ee7782012-08-18 09:51:42 +10004687 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004688 atomic_inc(&rdev->nr_pending);
4689 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004690 success = sync_page_io(rdev,
4691 addr,
4692 s << 9,
4693 bvec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05004694 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004695 rdev_dec_pending(rdev, mddev);
4696 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004697 if (success)
4698 break;
4699 failed:
4700 slot++;
4701 if (slot >= conf->copies)
4702 slot = 0;
4703 if (slot == first_slot)
4704 break;
4705 }
NeilBrownd094d682016-06-02 16:19:52 +10004706 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004707 if (!success) {
4708 /* couldn't read this block, must give up */
4709 set_bit(MD_RECOVERY_INTR,
4710 &mddev->recovery);
4711 return -EIO;
4712 }
4713 sectors -= s;
4714 idx++;
4715 }
4716 return 0;
4717}
4718
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004719static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004720{
NeilBrown3ea7daa2012-05-22 13:53:47 +10004721 struct r10bio *r10_bio = bio->bi_private;
4722 struct mddev *mddev = r10_bio->mddev;
4723 struct r10conf *conf = mddev->private;
4724 int d;
4725 int slot;
4726 int repl;
4727 struct md_rdev *rdev = NULL;
4728
4729 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4730 if (repl)
4731 rdev = conf->mirrors[d].replacement;
4732 if (!rdev) {
4733 smp_mb();
4734 rdev = conf->mirrors[d].rdev;
4735 }
4736
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004737 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004738 /* FIXME should record badblock */
4739 md_error(mddev, rdev);
4740 }
4741
4742 rdev_dec_pending(rdev, mddev);
4743 end_reshape_request(r10_bio);
4744}
4745
4746static void end_reshape_request(struct r10bio *r10_bio)
4747{
4748 if (!atomic_dec_and_test(&r10_bio->remaining))
4749 return;
4750 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4751 bio_put(r10_bio->master_bio);
4752 put_buf(r10_bio);
4753}
4754
4755static void raid10_finish_reshape(struct mddev *mddev)
4756{
4757 struct r10conf *conf = mddev->private;
4758
4759 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4760 return;
4761
4762 if (mddev->delta_disks > 0) {
4763 sector_t size = raid10_size(mddev, 0, 0);
4764 md_set_array_sectors(mddev, size);
4765 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4766 mddev->recovery_cp = mddev->resync_max_sectors;
4767 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4768 }
4769 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004770 if (mddev->queue) {
4771 set_capacity(mddev->gendisk, mddev->array_sectors);
4772 revalidate_disk(mddev->gendisk);
4773 }
NeilBrown63aced62012-05-22 13:55:33 +10004774 } else {
4775 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004776 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004777 for (d = conf->geo.raid_disks ;
4778 d < conf->geo.raid_disks - mddev->delta_disks;
4779 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004780 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004781 if (rdev)
4782 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004783 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004784 if (rdev)
4785 clear_bit(In_sync, &rdev->flags);
4786 }
NeilBrownd094d682016-06-02 16:19:52 +10004787 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004788 }
4789 mddev->layout = mddev->new_layout;
4790 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4791 mddev->reshape_position = MaxSector;
4792 mddev->delta_disks = 0;
4793 mddev->reshape_backwards = 0;
4794}
4795
NeilBrown84fc4b52011-10-11 16:49:58 +11004796static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
4798 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004799 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004801 .make_request = raid10_make_request,
4802 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004803 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004804 .status = raid10_status,
4805 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 .hot_add_disk = raid10_add_disk,
4807 .hot_remove_disk= raid10_remove_disk,
4808 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004809 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004810 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004811 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004812 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004813 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004814 .check_reshape = raid10_check_reshape,
4815 .start_reshape = raid10_start_reshape,
4816 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004817 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818};
4819
4820static int __init raid_init(void)
4821{
NeilBrown2604b702006-01-06 00:20:36 -08004822 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
4825static void raid_exit(void)
4826{
NeilBrown2604b702006-01-06 00:20:36 -08004827 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
4830module_init(raid_init);
4831module_exit(raid_exit);
4832MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004833MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004835MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004836MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004837
4838module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);