blob: c7c5b2693fc96ac2033524c067648c470bede1e9 [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
Jan Karadc3b17c2017-02-02 15:56:50 +0100863 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700864 }
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 &&
NeilBrownf5fe1b52017-03-10 17:00:47 +1100977 (!bio_list_empty(&current->bio_list[0]) ||
978 !bio_list_empty(&current->bio_list[1]))),
Lukas Czernereed8c022012-11-30 11:42:40 +0100979 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800980 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200981 if (!conf->nr_waiting)
982 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800983 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200984 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800985 spin_unlock_irq(&conf->resync_lock);
986}
987
NeilBrowne879a872011-10-11 16:49:02 +1100988static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800989{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200990 if ((atomic_dec_and_test(&conf->nr_pending)) ||
991 (conf->array_freeze_pending))
992 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800993}
994
NeilBrowne2d59922013-06-12 11:01:22 +1000995static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -0800996{
997 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800998 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800999 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001000 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001001 * This is called in the context of one normal IO request
1002 * that has failed. Thus any sync request that might be pending
1003 * will be blocked by nr_pending, and we need to wait for
1004 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001005 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001006 * must match the number of pending IOs (nr_pending) before
1007 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001008 */
1009 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001010 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -08001011 conf->barrier++;
1012 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001013 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001014 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001015 conf->resync_lock,
1016 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001017
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02001018 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -08001019 spin_unlock_irq(&conf->resync_lock);
1020}
1021
NeilBrowne879a872011-10-11 16:49:02 +11001022static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001023{
1024 /* reverse the effect of the freeze */
1025 spin_lock_irq(&conf->resync_lock);
1026 conf->barrier--;
1027 conf->nr_waiting--;
1028 wake_up(&conf->wait_barrier);
1029 spin_unlock_irq(&conf->resync_lock);
1030}
1031
NeilBrownf8c9e742012-05-21 09:28:33 +10001032static sector_t choose_data_offset(struct r10bio *r10_bio,
1033 struct md_rdev *rdev)
1034{
1035 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1036 test_bit(R10BIO_Previous, &r10_bio->state))
1037 return rdev->data_offset;
1038 else
1039 return rdev->new_data_offset;
1040}
1041
NeilBrown57c67df2012-10-11 13:32:13 +11001042struct raid10_plug_cb {
1043 struct blk_plug_cb cb;
1044 struct bio_list pending;
1045 int pending_cnt;
1046};
1047
1048static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1049{
1050 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1051 cb);
1052 struct mddev *mddev = plug->cb.data;
1053 struct r10conf *conf = mddev->private;
1054 struct bio *bio;
1055
NeilBrown874807a2012-11-27 12:14:40 +11001056 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001057 spin_lock_irq(&conf->device_lock);
1058 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1059 conf->pending_count += plug->pending_cnt;
1060 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001061 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001062 md_wakeup_thread(mddev->thread);
1063 kfree(plug);
1064 return;
1065 }
1066
1067 /* we aren't scheduling, so we can do the write-out directly. */
1068 bio = bio_list_get(&plug->pending);
1069 bitmap_unplug(mddev->bitmap);
1070 wake_up(&conf->wait_barrier);
1071
1072 while (bio) { /* submit pending writes */
1073 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001074 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001075 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001076 bio->bi_bdev = rdev->bdev;
1077 if (test_bit(Faulty, &rdev->flags)) {
1078 bio->bi_error = -EIO;
1079 bio_endio(bio);
1080 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1081 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001082 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001083 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001084 else
1085 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001086 bio = next;
1087 }
1088 kfree(plug);
1089}
1090
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001091static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1092 struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
NeilBrowne879a872011-10-11 16:49:02 +11001094 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct bio *read_bio;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001096 const int op = bio_op(bio);
1097 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1098 int sectors_handled;
1099 int max_sectors;
1100 sector_t sectors;
1101 struct md_rdev *rdev;
1102 int slot;
1103
1104 /*
1105 * Register the new request and wait if the reconstruction
1106 * thread has put up a bar for new requests.
1107 * Continue immediately if no resync is active currently.
1108 */
1109 wait_barrier(conf);
1110
1111 sectors = bio_sectors(bio);
1112 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1113 bio->bi_iter.bi_sector < conf->reshape_progress &&
1114 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1115 /*
1116 * IO spans the reshape position. Need to wait for reshape to
1117 * pass
1118 */
1119 raid10_log(conf->mddev, "wait reshape");
1120 allow_barrier(conf);
1121 wait_event(conf->wait_barrier,
1122 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1123 conf->reshape_progress >= bio->bi_iter.bi_sector +
1124 sectors);
1125 wait_barrier(conf);
1126 }
1127
1128read_again:
1129 rdev = read_balance(conf, r10_bio, &max_sectors);
1130 if (!rdev) {
1131 raid_end_bio_io(r10_bio);
1132 return;
1133 }
1134 slot = r10_bio->read_slot;
1135
Ming Leid7a10302017-02-14 23:29:03 +08001136 read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001137 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
1138 max_sectors);
1139
1140 r10_bio->devs[slot].bio = read_bio;
1141 r10_bio->devs[slot].rdev = rdev;
1142
1143 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1144 choose_data_offset(r10_bio, rdev);
1145 read_bio->bi_bdev = rdev->bdev;
1146 read_bio->bi_end_io = raid10_end_read_request;
1147 bio_set_op_attrs(read_bio, op, do_sync);
1148 if (test_bit(FailFast, &rdev->flags) &&
1149 test_bit(R10BIO_FailFast, &r10_bio->state))
1150 read_bio->bi_opf |= MD_FAILFAST;
1151 read_bio->bi_private = r10_bio;
1152
1153 if (mddev->gendisk)
1154 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1155 read_bio, disk_devt(mddev->gendisk),
1156 r10_bio->sector);
1157 if (max_sectors < r10_bio->sectors) {
1158 /*
1159 * Could not read all from this device, so we will need another
1160 * r10_bio.
1161 */
1162 sectors_handled = (r10_bio->sector + max_sectors
1163 - bio->bi_iter.bi_sector);
1164 r10_bio->sectors = max_sectors;
1165 spin_lock_irq(&conf->device_lock);
1166 if (bio->bi_phys_segments == 0)
1167 bio->bi_phys_segments = 2;
1168 else
1169 bio->bi_phys_segments++;
1170 spin_unlock_irq(&conf->device_lock);
1171 /*
1172 * Cannot call generic_make_request directly as that will be
1173 * queued in __generic_make_request and subsequent
1174 * mempool_alloc might block waiting for it. so hand bio over
1175 * to raid10d.
1176 */
1177 reschedule_retry(r10_bio);
1178
1179 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1180
1181 r10_bio->master_bio = bio;
1182 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
1183 r10_bio->state = 0;
1184 r10_bio->mddev = mddev;
1185 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1186 goto read_again;
1187 } else
1188 generic_make_request(read_bio);
1189 return;
1190}
1191
1192static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1193 struct r10bio *r10_bio)
1194{
1195 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int i;
Mike Christie796a5cf2016-06-05 14:32:07 -05001197 const int op = bio_op(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001198 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1199 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -08001200 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001201 struct md_rdev *blocked_rdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001202 struct blk_plug_cb *cb;
1203 struct raid10_plug_cb *plug = NULL;
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001204 sector_t sectors;
NeilBrownd4432c22011-07-28 11:39:24 +10001205 int sectors_handled;
1206 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Tomasz Majchrzak9b622e22016-07-28 10:28:25 +02001208 md_write_start(mddev, bio);
1209
NeilBrowncc13b1d2014-05-05 13:34:37 +10001210 /*
1211 * Register the new request and wait if the reconstruction
1212 * thread has put up a bar for new requests.
1213 * Continue immediately if no resync is active currently.
1214 */
1215 wait_barrier(conf);
1216
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001217 sectors = bio_sectors(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001218 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001219 bio->bi_iter.bi_sector < conf->reshape_progress &&
1220 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001221 /*
1222 * IO spans the reshape position. Need to wait for reshape to
1223 * pass
NeilBrown3ea7daa2012-05-22 13:53:47 +10001224 */
NeilBrown578b54a2016-11-14 16:30:21 +11001225 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001226 allow_barrier(conf);
1227 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001228 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1229 conf->reshape_progress >= bio->bi_iter.bi_sector +
1230 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001231 wait_barrier(conf);
1232 }
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001233
NeilBrown3ea7daa2012-05-22 13:53:47 +10001234 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
NeilBrown3ea7daa2012-05-22 13:53:47 +10001235 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001236 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1237 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1238 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1239 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001240 /* Need to update reshape_position in metadata */
1241 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08001242 set_mask_bits(&mddev->sb_flags, 0,
1243 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001244 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001245 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001246 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08001247 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001248
1249 conf->reshape_safe = mddev->reshape_position;
1250 }
1251
NeilBrown34db0cd2011-10-11 16:50:01 +11001252 if (conf->pending_count >= max_queued_requests) {
1253 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001254 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001255 wait_event(conf->wait_barrier,
1256 conf->pending_count < max_queued_requests);
1257 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001258 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * inc refcount on their rdev. Record them by setting
1260 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001261 * If there are known/acknowledged bad blocks on any device
1262 * on which we have seen a write error, we want to avoid
1263 * writing to those blocks. This potentially requires several
1264 * writes to write around the bad blocks. Each set of writes
1265 * gets its own r10_bio with a set of bios attached. The number
1266 * of r10_bios is recored in bio->bi_phys_segments just as with
1267 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001269
NeilBrown69335ef2011-12-23 10:17:54 +11001270 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001272retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001273 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001275 max_sectors = r10_bio->sectors;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 for (i = 0; i < conf->copies; i++) {
1278 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001279 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001280 struct md_rdev *rrdev = rcu_dereference(
1281 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001282 if (rdev == rrdev)
1283 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001284 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1285 atomic_inc(&rdev->nr_pending);
1286 blocked_rdev = rdev;
1287 break;
1288 }
NeilBrown475b0322011-12-23 10:17:55 +11001289 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1290 atomic_inc(&rrdev->nr_pending);
1291 blocked_rdev = rrdev;
1292 break;
1293 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001294 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001295 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001296 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001297 rrdev = NULL;
1298
NeilBrownd4432c22011-07-28 11:39:24 +10001299 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001300 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001301
1302 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001303 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001304 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001305 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001306 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001307 sector_t first_bad;
1308 sector_t dev_sector = r10_bio->devs[i].addr;
1309 int bad_sectors;
1310 int is_bad;
1311
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001312 is_bad = is_badblock(rdev, dev_sector, max_sectors,
NeilBrownd4432c22011-07-28 11:39:24 +10001313 &first_bad, &bad_sectors);
1314 if (is_bad < 0) {
1315 /* Mustn't write here until the bad block
1316 * is acknowledged
1317 */
1318 atomic_inc(&rdev->nr_pending);
1319 set_bit(BlockedBadBlocks, &rdev->flags);
1320 blocked_rdev = rdev;
1321 break;
1322 }
1323 if (is_bad && first_bad <= dev_sector) {
1324 /* Cannot write here at all */
1325 bad_sectors -= (dev_sector - first_bad);
1326 if (bad_sectors < max_sectors)
1327 /* Mustn't write more than bad_sectors
1328 * to other devices yet
1329 */
1330 max_sectors = bad_sectors;
1331 /* We don't set R10BIO_Degraded as that
1332 * only applies if the disk is missing,
1333 * so it might be re-added, and we want to
1334 * know to recover this chunk.
1335 * In this case the device is here, and the
1336 * fact that this chunk is not in-sync is
1337 * recorded in the bad block log.
1338 */
1339 continue;
1340 }
1341 if (is_bad) {
1342 int good_sectors = first_bad - dev_sector;
1343 if (good_sectors < max_sectors)
1344 max_sectors = good_sectors;
1345 }
1346 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001347 if (rdev) {
1348 r10_bio->devs[i].bio = bio;
1349 atomic_inc(&rdev->nr_pending);
1350 }
NeilBrown475b0322011-12-23 10:17:55 +11001351 if (rrdev) {
1352 r10_bio->devs[i].repl_bio = bio;
1353 atomic_inc(&rrdev->nr_pending);
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 rcu_read_unlock();
1357
Dan Williams6bfe0b42008-04-30 00:52:32 -07001358 if (unlikely(blocked_rdev)) {
1359 /* Have to wait for this device to get unblocked, then retry */
1360 int j;
1361 int d;
1362
NeilBrown475b0322011-12-23 10:17:55 +11001363 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001364 if (r10_bio->devs[j].bio) {
1365 d = r10_bio->devs[j].devnum;
1366 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1367 }
NeilBrown475b0322011-12-23 10:17:55 +11001368 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001369 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001370 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001371 rdev = conf->mirrors[d].replacement;
1372 if (!rdev) {
1373 /* Race with remove_disk */
1374 smp_mb();
1375 rdev = conf->mirrors[d].rdev;
1376 }
1377 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001378 }
1379 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001380 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001381 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001382 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1383 wait_barrier(conf);
1384 goto retry_write;
1385 }
1386
NeilBrown6b6c8112017-03-15 14:05:13 +11001387 if (max_sectors < r10_bio->sectors)
NeilBrownd4432c22011-07-28 11:39:24 +10001388 r10_bio->sectors = max_sectors;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001389 sectors_handled = r10_bio->sector + max_sectors -
1390 bio->bi_iter.bi_sector;
NeilBrownd4432c22011-07-28 11:39:24 +10001391
NeilBrown4e780642010-10-19 12:54:01 +11001392 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001393 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 for (i = 0; i < conf->copies; i++) {
1396 struct bio *mbio;
1397 int d = r10_bio->devs[i].devnum;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001398 if (r10_bio->devs[i].bio) {
1399 struct md_rdev *rdev = conf->mirrors[d].rdev;
Ming Leid7a10302017-02-14 23:29:03 +08001400 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001401 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001402 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001403 r10_bio->devs[i].bio = mbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Kent Overstreet4f024f32013-10-11 15:44:27 -07001405 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001406 choose_data_offset(r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001407 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001408 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001409 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrown1919cbb2016-11-18 16:16:12 +11001410 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags) &&
1411 enough(conf, d))
1412 mbio->bi_opf |= MD_FAILFAST;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001413 mbio->bi_private = r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
NeilBrown109e3762016-11-18 13:22:04 +11001415 if (conf->mddev->gendisk)
1416 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1417 mbio, disk_devt(conf->mddev->gendisk),
1418 r10_bio->sector);
1419 /* flush_pending_writes() needs access to the rdev so...*/
1420 mbio->bi_bdev = (void*)rdev;
1421
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001422 atomic_inc(&r10_bio->remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001424 cb = blk_check_plugged(raid10_unplug, mddev,
1425 sizeof(*plug));
1426 if (cb)
1427 plug = container_of(cb, struct raid10_plug_cb,
1428 cb);
1429 else
1430 plug = NULL;
1431 spin_lock_irqsave(&conf->device_lock, flags);
1432 if (plug) {
1433 bio_list_add(&plug->pending, mbio);
1434 plug->pending_cnt++;
1435 } else {
1436 bio_list_add(&conf->pending_bio_list, mbio);
1437 conf->pending_count++;
1438 }
1439 spin_unlock_irqrestore(&conf->device_lock, flags);
1440 if (!plug)
1441 md_wakeup_thread(mddev->thread);
1442 }
NeilBrown57c67df2012-10-11 13:32:13 +11001443
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001444 if (r10_bio->devs[i].repl_bio) {
1445 struct md_rdev *rdev = conf->mirrors[d].replacement;
1446 if (rdev == NULL) {
1447 /* Replacement just got moved to main 'rdev' */
1448 smp_mb();
1449 rdev = conf->mirrors[d].rdev;
1450 }
Ming Leid7a10302017-02-14 23:29:03 +08001451 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001452 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001453 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001454 r10_bio->devs[i].repl_bio = mbio;
1455
Kent Overstreet4f024f32013-10-11 15:44:27 -07001456 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001457 choose_data_offset(r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001458 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001459 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001460 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001461 mbio->bi_private = r10_bio;
1462
NeilBrown109e3762016-11-18 13:22:04 +11001463 if (conf->mddev->gendisk)
1464 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1465 mbio, disk_devt(conf->mddev->gendisk),
1466 r10_bio->sector);
1467 /* flush_pending_writes() needs access to the rdev so...*/
1468 mbio->bi_bdev = (void*)rdev;
1469
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001470 atomic_inc(&r10_bio->remaining);
Shaohua Li6d399782017-02-23 12:26:41 -08001471
1472 cb = blk_check_plugged(raid10_unplug, mddev,
1473 sizeof(*plug));
1474 if (cb)
1475 plug = container_of(cb, struct raid10_plug_cb,
1476 cb);
1477 else
1478 plug = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001479 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li6d399782017-02-23 12:26:41 -08001480 if (plug) {
1481 bio_list_add(&plug->pending, mbio);
1482 plug->pending_cnt++;
1483 } else {
1484 bio_list_add(&conf->pending_bio_list, mbio);
1485 conf->pending_count++;
1486 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001487 spin_unlock_irqrestore(&conf->device_lock, flags);
Shaohua Li6d399782017-02-23 12:26:41 -08001488 if (!plug)
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001489 md_wakeup_thread(mddev->thread);
NeilBrown57c67df2012-10-11 13:32:13 +11001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492
NeilBrown079fa162011-09-10 17:21:23 +10001493 /* Don't remove the bias on 'remaining' (one_write_done) until
1494 * after checking if we need to go around again.
1495 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001496
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001497 if (sectors_handled < bio_sectors(bio)) {
NeilBrown6b6c8112017-03-15 14:05:13 +11001498 /* We need another r10_bio and it needs to be counted
NeilBrownd4432c22011-07-28 11:39:24 +10001499 * in bio->bi_phys_segments.
1500 */
NeilBrown6b6c8112017-03-15 14:05:13 +11001501 spin_lock_irq(&conf->device_lock);
1502 if (bio->bi_phys_segments == 0)
1503 bio->bi_phys_segments = 2;
1504 else
1505 bio->bi_phys_segments++;
1506 spin_unlock_irq(&conf->device_lock);
1507 one_write_done(r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001508 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1509
1510 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001511 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001512
1513 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001514 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001515 r10_bio->state = 0;
1516 goto retry_write;
1517 }
NeilBrown079fa162011-09-10 17:21:23 +10001518 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001519}
1520
Robert LeBlancbb5f1ed2016-12-05 13:02:58 -07001521static void __make_request(struct mddev *mddev, struct bio *bio)
1522{
1523 struct r10conf *conf = mddev->private;
1524 struct r10bio *r10_bio;
1525
1526 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1527
1528 r10_bio->master_bio = bio;
1529 r10_bio->sectors = bio_sectors(bio);
1530
1531 r10_bio->mddev = mddev;
1532 r10_bio->sector = bio->bi_iter.bi_sector;
1533 r10_bio->state = 0;
1534
1535 /*
1536 * We might need to issue multiple reads to different devices if there
1537 * are bad blocks around, so we keep track of the number of reads in
1538 * bio->bi_phys_segments. If this is 0, there is only one r10_bio and
1539 * no locking will be needed when the request completes. If it is
1540 * non-zero, then it is the number of not-completed requests.
1541 */
1542 bio->bi_phys_segments = 0;
1543 bio_clear_flag(bio, BIO_SEG_VALID);
1544
1545 if (bio_data_dir(bio) == READ)
1546 raid10_read_request(mddev, bio, r10_bio);
1547 else
1548 raid10_write_request(mddev, bio, r10_bio);
1549}
1550
Shaohua Li849674e2016-01-20 13:52:20 -08001551static void raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001552{
1553 struct r10conf *conf = mddev->private;
1554 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1555 int chunk_sects = chunk_mask + 1;
1556
1557 struct bio *split;
1558
Jens Axboe1eff9d32016-08-05 15:35:16 -06001559 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001560 md_flush_request(mddev, bio);
1561 return;
1562 }
1563
Kent Overstreet20d01892013-11-23 18:21:01 -08001564 do {
1565
1566 /*
1567 * If this request crosses a chunk boundary, we need to split
1568 * it.
1569 */
1570 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1571 bio_sectors(bio) > chunk_sects
1572 && (conf->geo.near_copies < conf->geo.raid_disks
1573 || conf->prev.near_copies <
1574 conf->prev.raid_disks))) {
1575 split = bio_split(bio, chunk_sects -
1576 (bio->bi_iter.bi_sector &
1577 (chunk_sects - 1)),
1578 GFP_NOIO, fs_bio_set);
1579 bio_chain(split, bio);
1580 } else {
1581 split = bio;
1582 }
1583
Shaohua Li61eb2b42017-02-28 13:00:20 -08001584 /*
1585 * If a bio is splitted, the first part of bio will pass
1586 * barrier but the bio is queued in current->bio_list (see
1587 * generic_make_request). If there is a raise_barrier() called
1588 * here, the second part of bio can't pass barrier. But since
1589 * the first part bio isn't dispatched to underlaying disks
1590 * yet, the barrier is never released, hence raise_barrier will
1591 * alays wait. We have a deadlock.
1592 * Note, this only happens in read path. For write path, the
1593 * first part of bio is dispatched in a schedule() call
1594 * (because of blk plug) or offloaded to raid10d.
1595 * Quitting from the function immediately can change the bio
1596 * order queued in bio_list and avoid the deadlock.
1597 */
Kent Overstreet20d01892013-11-23 18:21:01 -08001598 __make_request(mddev, split);
Shaohua Li61eb2b42017-02-28 13:00:20 -08001599 if (split != bio && bio_data_dir(bio) == READ) {
1600 generic_make_request(bio);
1601 break;
1602 }
Kent Overstreet20d01892013-11-23 18:21:01 -08001603 } while (split != bio);
NeilBrown079fa162011-09-10 17:21:23 +10001604
1605 /* In case raid10d snuck in to freeze_array */
1606 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Shaohua Li849674e2016-01-20 13:52:20 -08001609static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
NeilBrowne879a872011-10-11 16:49:02 +11001611 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int i;
1613
NeilBrown5cf00fc2012-05-21 09:28:20 +10001614 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001615 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001616 if (conf->geo.near_copies > 1)
1617 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1618 if (conf->geo.far_copies > 1) {
1619 if (conf->geo.far_offset)
1620 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001621 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001622 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001623 if (conf->geo.far_set_size != conf->geo.raid_disks)
1624 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001625 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001626 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1627 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001628 rcu_read_lock();
1629 for (i = 0; i < conf->geo.raid_disks; i++) {
1630 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1631 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1632 }
1633 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 seq_printf(seq, "]");
1635}
1636
NeilBrown700c7212011-07-27 11:00:36 +10001637/* check if there are enough drives for
1638 * every block to appear on atleast one.
1639 * Don't consider the device numbered 'ignore'
1640 * as we might be about to remove it.
1641 */
NeilBrown635f6412013-06-11 14:57:09 +10001642static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001643{
1644 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001645 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001646 int disks, ncopies;
1647 if (previous) {
1648 disks = conf->prev.raid_disks;
1649 ncopies = conf->prev.near_copies;
1650 } else {
1651 disks = conf->geo.raid_disks;
1652 ncopies = conf->geo.near_copies;
1653 }
NeilBrown700c7212011-07-27 11:00:36 +10001654
NeilBrown725d6e52013-06-11 15:08:03 +10001655 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001656 do {
1657 int n = conf->copies;
1658 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001659 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001660 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001661 struct md_rdev *rdev;
1662 if (this != ignore &&
1663 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1664 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001665 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001666 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001667 }
1668 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001669 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001670 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001671 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001672 has_enough = 1;
1673out:
1674 rcu_read_unlock();
1675 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001676}
1677
NeilBrownf8c9e742012-05-21 09:28:33 +10001678static int enough(struct r10conf *conf, int ignore)
1679{
NeilBrown635f6412013-06-11 14:57:09 +10001680 /* when calling 'enough', both 'prev' and 'geo' must
1681 * be stable.
1682 * This is ensured if ->reconfig_mutex or ->device_lock
1683 * is held.
1684 */
1685 return _enough(conf, 0, ignore) &&
1686 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001687}
1688
Shaohua Li849674e2016-01-20 13:52:20 -08001689static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001692 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001693 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 /*
1696 * If it is not operational, then we have already marked it as dead
1697 * else if it is the last working disks, ignore the error, let the
1698 * next level up know.
1699 * else mark the drive as failed
1700 */
NeilBrown635f6412013-06-11 14:57:09 +10001701 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001702 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001703 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /*
1705 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001707 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001708 return;
1709 }
NeilBrown2446dba2014-07-31 10:16:29 +10001710 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001711 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001712 /*
1713 * If recovery is running, make sure it aborts.
1714 */
1715 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001716 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001717 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08001718 set_mask_bits(&mddev->sb_flags, 0,
1719 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001720 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001721 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1722 "md/raid10:%s: Operation continuing on %d devices.\n",
1723 mdname(mddev), bdevname(rdev->bdev, b),
1724 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
NeilBrowne879a872011-10-11 16:49:02 +11001727static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001730 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
NeilBrown08464e02016-11-02 14:16:50 +11001732 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001734 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return;
1736 }
NeilBrown08464e02016-11-02 14:16:50 +11001737 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1738 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
NeilBrown4056ca52016-06-02 16:19:52 +10001740 /* This is only called with ->reconfix_mutex held, so
1741 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001742 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001744 rdev = conf->mirrors[i].rdev;
1745 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001746 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1747 i, !test_bit(In_sync, &rdev->flags),
1748 !test_bit(Faulty, &rdev->flags),
1749 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751}
1752
NeilBrowne879a872011-10-11 16:49:02 +11001753static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
NeilBrown0a27ec92006-01-06 00:20:13 -08001755 wait_barrier(conf);
1756 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 mempool_destroy(conf->r10buf_pool);
1759 conf->r10buf_pool = NULL;
1760}
1761
NeilBrownfd01b882011-10-11 16:47:53 +11001762static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001765 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001766 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001767 int count = 0;
1768 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /*
1771 * Find all non-in_sync disks within the RAID10 configuration
1772 * and mark them in_sync
1773 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001774 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001776 if (tmp->replacement
1777 && tmp->replacement->recovery_offset == MaxSector
1778 && !test_bit(Faulty, &tmp->replacement->flags)
1779 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1780 /* Replacement has just become active */
1781 if (!tmp->rdev
1782 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1783 count++;
1784 if (tmp->rdev) {
1785 /* Replaced device not technically faulty,
1786 * but we need to be sure it gets removed
1787 * and never re-added.
1788 */
1789 set_bit(Faulty, &tmp->rdev->flags);
1790 sysfs_notify_dirent_safe(
1791 tmp->rdev->sysfs_state);
1792 }
1793 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1794 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001795 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001796 && !test_bit(Faulty, &tmp->rdev->flags)
1797 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001798 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001799 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 }
NeilBrown6b965622010-08-18 11:56:59 +10001802 spin_lock_irqsave(&conf->device_lock, flags);
1803 mddev->degraded -= count;
1804 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001807 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
NeilBrownfd01b882011-10-11 16:47:53 +11001810static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
NeilBrowne879a872011-10-11 16:49:02 +11001812 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001813 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001815 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001816 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 if (mddev->recovery_cp < MaxSector)
1819 /* only hot-add to in-sync arrays, as recovery is
1820 * very different from resync
1821 */
Neil Brown199050e2008-06-28 08:31:33 +10001822 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001823 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Dan Williams1501efa2016-01-13 16:00:07 -08001826 if (md_integrity_add_rdev(rdev, mddev))
1827 return -ENXIO;
1828
NeilBrowna53a6c82008-11-06 17:28:20 +11001829 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001830 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001832 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001833 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1834 mirror = rdev->saved_raid_disk;
1835 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001836 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001837 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001838 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001839 if (p->recovery_disabled == mddev->recovery_disabled)
1840 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001841 if (p->rdev) {
1842 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1843 p->replacement != NULL)
1844 continue;
1845 clear_bit(In_sync, &rdev->flags);
1846 set_bit(Replacement, &rdev->flags);
1847 rdev->raid_disk = mirror;
1848 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001849 if (mddev->gendisk)
1850 disk_stack_limits(mddev->gendisk, rdev->bdev,
1851 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001852 conf->fullsync = 1;
1853 rcu_assign_pointer(p->replacement, rdev);
1854 break;
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Jonathan Brassow9092c022013-05-02 14:19:24 -05001857 if (mddev->gendisk)
1858 disk_stack_limits(mddev->gendisk, rdev->bdev,
1859 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
NeilBrown2bb77732011-07-27 11:00:36 +10001861 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001862 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001863 rdev->raid_disk = mirror;
1864 err = 0;
1865 if (rdev->saved_raid_disk != mirror)
1866 conf->fullsync = 1;
1867 rcu_assign_pointer(p->rdev, rdev);
1868 break;
1869 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11001870 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001871 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
NeilBrownb8321b62011-12-23 10:17:51 +11001877static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
NeilBrowne879a872011-10-11 16:49:02 +11001879 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001881 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001882 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001883 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001886 if (rdev == p->rdev)
1887 rdevp = &p->rdev;
1888 else if (rdev == p->replacement)
1889 rdevp = &p->replacement;
1890 else
1891 return 0;
1892
1893 if (test_bit(In_sync, &rdev->flags) ||
1894 atomic_read(&rdev->nr_pending)) {
1895 err = -EBUSY;
1896 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
NeilBrownd787be42016-06-02 16:19:53 +10001898 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11001899 * is not possible.
1900 */
1901 if (!test_bit(Faulty, &rdev->flags) &&
1902 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001903 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001904 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001905 enough(conf, -1)) {
1906 err = -EBUSY;
1907 goto abort;
1908 }
1909 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001910 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1911 synchronize_rcu();
1912 if (atomic_read(&rdev->nr_pending)) {
1913 /* lost the race, try later */
1914 err = -EBUSY;
1915 *rdevp = rdev;
1916 goto abort;
1917 }
1918 }
1919 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001920 /* We must have just cleared 'rdev' */
1921 p->rdev = p->replacement;
1922 clear_bit(Replacement, &p->replacement->flags);
1923 smp_mb(); /* Make sure other CPUs may see both as identical
1924 * but will never see neither -- if they are careful.
1925 */
1926 p->replacement = NULL;
1927 clear_bit(WantReplacement, &rdev->flags);
1928 } else
1929 /* We might have just remove the Replacement as faulty
1930 * Clear the flag just in case
1931 */
1932 clear_bit(WantReplacement, &rdev->flags);
1933
NeilBrownc8ab9032011-12-23 10:17:54 +11001934 err = md_integrity_register(mddev);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936abort:
1937
1938 print_conf(conf);
1939 return err;
1940}
1941
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001942static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001944 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001945 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001946 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
NeilBrown3ea7daa2012-05-22 13:53:47 +10001948 if (bio == r10_bio->master_bio) {
1949 /* this is a reshape read */
1950 d = r10_bio->read_slot; /* really the read dev */
1951 } else
1952 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001953
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001954 if (!bio->bi_error)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001955 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001956 else
1957 /* The write handler will notice the lack of
1958 * R10BIO_Uptodate and record any errors etc
1959 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001960 atomic_add(r10_bio->sectors,
1961 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /* for reconstruct, we always reschedule after a read.
1964 * for resync, only after all reads
1965 */
NeilBrown73d5c382009-02-25 13:18:47 +11001966 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1968 atomic_dec_and_test(&r10_bio->remaining)) {
1969 /* we have read all the blocks,
1970 * do the comparison in process context in raid10d
1971 */
1972 reschedule_retry(r10_bio);
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
NeilBrown9f2c9d12011-10-11 16:48:43 +11001976static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001977{
NeilBrownfd01b882011-10-11 16:47:53 +11001978 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001979
1980 while (atomic_dec_and_test(&r10_bio->remaining)) {
1981 if (r10_bio->master_bio == NULL) {
1982 /* the primary of several recovery bios */
1983 sector_t s = r10_bio->sectors;
1984 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1985 test_bit(R10BIO_WriteError, &r10_bio->state))
1986 reschedule_retry(r10_bio);
1987 else
1988 put_buf(r10_bio);
1989 md_done_sync(mddev, s, 1);
1990 break;
1991 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001992 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001993 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1994 test_bit(R10BIO_WriteError, &r10_bio->state))
1995 reschedule_retry(r10_bio);
1996 else
1997 put_buf(r10_bio);
1998 r10_bio = r10_bio2;
1999 }
2000 }
2001}
2002
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002003static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002005 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11002006 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002007 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10002008 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10002009 sector_t first_bad;
2010 int bad_sectors;
2011 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002012 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11002013 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
NeilBrown9ad1aef2011-12-23 10:17:55 +11002015 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2016 if (repl)
2017 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11002018 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11002019 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002021 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002022 if (repl)
2023 md_error(mddev, rdev);
2024 else {
2025 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002026 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2027 set_bit(MD_RECOVERY_NEEDED,
2028 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002029 set_bit(R10BIO_WriteError, &r10_bio->state);
2030 }
2031 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10002032 r10_bio->devs[slot].addr,
2033 r10_bio->sectors,
2034 &first_bad, &bad_sectors))
2035 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07002036
NeilBrown9ad1aef2011-12-23 10:17:55 +11002037 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002038
2039 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042/*
2043 * Note: sync and recover and handled very differently for raid10
2044 * This code is for resync.
2045 * For resync, we read through virtual addresses and read all blocks.
2046 * If there is any error, we schedule a write. The lowest numbered
2047 * drive is authoritative.
2048 * However requests come for physical address, so we need to map.
2049 * For every physical address there are raid_disks/copies virtual addresses,
2050 * which is always are least one, but is not necessarly an integer.
2051 * This means that a physical address can span multiple chunks, so we may
2052 * have to submit multiple io requests for a single sync request.
2053 */
2054/*
2055 * We check if all blocks are in-sync and only write to blocks that
2056 * aren't in sync
2057 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002058static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
NeilBrowne879a872011-10-11 16:49:02 +11002060 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int i, first;
2062 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002063 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 atomic_set(&r10_bio->remaining, 1);
2066
2067 /* find the first device with a block */
2068 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002069 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 break;
2071
2072 if (i == conf->copies)
2073 goto done;
2074
2075 first = i;
2076 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002077 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2078 fbio->bi_iter.bi_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
majianpengf4380a92012-04-12 16:04:47 +10002080 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002082 for (i=0 ; i < conf->copies ; i++) {
2083 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11002084 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002087
2088 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002090 if (i == first)
2091 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002092 d = r10_bio->devs[i].devnum;
2093 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002094 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002095 /* We know that the bi_io_vec layout is the same for
2096 * both 'first' and 'i', so we just compare them.
2097 * All vec entries are PAGE_SIZE;
2098 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002099 int sectors = r10_bio->sectors;
2100 for (j = 0; j < vcnt; j++) {
2101 int len = PAGE_SIZE;
2102 if (sectors < (len / 512))
2103 len = sectors * 512;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002104 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2105 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown7bb23c42013-07-16 16:50:47 +10002106 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002107 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002108 sectors -= len/512;
2109 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002110 if (j == vcnt)
2111 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002112 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002113 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2114 /* Don't fix anything. */
2115 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002116 } else if (test_bit(FailFast, &rdev->flags)) {
2117 /* Just give up on this device */
2118 md_error(rdev->mddev, rdev);
2119 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002120 }
NeilBrownf84ee362011-07-28 11:39:25 +10002121 /* Ok, we need to write this bio, either to correct an
2122 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 * First we need to fixup bv_offset, bv_len and
2124 * bi_vecs, as the read request might have corrupted these
2125 */
Kent Overstreet8be185f2012-09-06 14:14:43 -07002126 bio_reset(tbio);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 tbio->bi_vcnt = vcnt;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002129 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 tbio->bi_private = r10_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002131 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002133 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Kent Overstreetc31df252015-05-06 23:34:20 -07002135 bio_copy_data(tbio, fbio);
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2138 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002139 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
NeilBrown1919cbb2016-11-18 16:16:12 +11002141 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2142 tbio->bi_opf |= MD_FAILFAST;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002143 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2145 generic_make_request(tbio);
2146 }
2147
NeilBrown9ad1aef2011-12-23 10:17:55 +11002148 /* Now write out to any replacement devices
2149 * that are active
2150 */
2151 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002152 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002153
2154 tbio = r10_bio->devs[i].repl_bio;
2155 if (!tbio || !tbio->bi_end_io)
2156 continue;
2157 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2158 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002159 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002160 d = r10_bio->devs[i].devnum;
2161 atomic_inc(&r10_bio->remaining);
2162 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002163 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002164 generic_make_request(tbio);
2165 }
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167done:
2168 if (atomic_dec_and_test(&r10_bio->remaining)) {
2169 md_done_sync(mddev, r10_bio->sectors, 1);
2170 put_buf(r10_bio);
2171 }
2172}
2173
2174/*
2175 * Now for the recovery code.
2176 * Recovery happens across physical sectors.
2177 * We recover all non-is_sync drives by finding the virtual address of
2178 * each, and then choose a working drive that also has that virt address.
2179 * There is a separate r10_bio for each non-in_sync drive.
2180 * Only the first two slots are in use. The first for reading,
2181 * The second for writing.
2182 *
2183 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002184static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002185{
2186 /* We got a read error during recovery.
2187 * We repeat the read in smaller page-sized sections.
2188 * If a read succeeds, write it to the new device or record
2189 * a bad block if we cannot.
2190 * If a read fails, record a bad block on both old and
2191 * new devices.
2192 */
NeilBrownfd01b882011-10-11 16:47:53 +11002193 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002194 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002195 struct bio *bio = r10_bio->devs[0].bio;
2196 sector_t sect = 0;
2197 int sectors = r10_bio->sectors;
2198 int idx = 0;
2199 int dr = r10_bio->devs[0].devnum;
2200 int dw = r10_bio->devs[1].devnum;
2201
2202 while (sectors) {
2203 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002204 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002205 sector_t addr;
2206 int ok;
2207
2208 if (s > (PAGE_SIZE>>9))
2209 s = PAGE_SIZE >> 9;
2210
2211 rdev = conf->mirrors[dr].rdev;
2212 addr = r10_bio->devs[0].addr + sect,
2213 ok = sync_page_io(rdev,
2214 addr,
2215 s << 9,
2216 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002217 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002218 if (ok) {
2219 rdev = conf->mirrors[dw].rdev;
2220 addr = r10_bio->devs[1].addr + sect;
2221 ok = sync_page_io(rdev,
2222 addr,
2223 s << 9,
2224 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002225 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002226 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002227 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002228 if (!test_and_set_bit(WantReplacement,
2229 &rdev->flags))
2230 set_bit(MD_RECOVERY_NEEDED,
2231 &rdev->mddev->recovery);
2232 }
NeilBrown5e570282011-07-28 11:39:25 +10002233 }
2234 if (!ok) {
2235 /* We don't worry if we cannot set a bad block -
2236 * it really is bad so there is no loss in not
2237 * recording it yet
2238 */
2239 rdev_set_badblocks(rdev, addr, s, 0);
2240
2241 if (rdev != conf->mirrors[dw].rdev) {
2242 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002243 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002244 addr = r10_bio->devs[1].addr + sect;
2245 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2246 if (!ok) {
2247 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002248 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2249 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002250
2251 conf->mirrors[dw].recovery_disabled
2252 = mddev->recovery_disabled;
2253 set_bit(MD_RECOVERY_INTR,
2254 &mddev->recovery);
2255 break;
2256 }
2257 }
2258 }
2259
2260 sectors -= s;
2261 sect += s;
2262 idx++;
2263 }
2264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
NeilBrown9f2c9d12011-10-11 16:48:43 +11002266static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
NeilBrowne879a872011-10-11 16:49:02 +11002268 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002269 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002270 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
NeilBrown5e570282011-07-28 11:39:25 +10002272 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2273 fix_recovery_read_error(r10_bio);
2274 end_sync_request(r10_bio);
2275 return;
2276 }
2277
Namhyung Kimc65060a2011-07-18 17:38:49 +10002278 /*
2279 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 * and submit the write request
2281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002283 wbio = r10_bio->devs[1].bio;
2284 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002285 /* Need to test wbio2->bi_end_io before we call
2286 * generic_make_request as if the former is NULL,
2287 * the latter is free to free wbio2.
2288 */
2289 if (wbio2 && !wbio2->bi_end_io)
2290 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002291 if (wbio->bi_end_io) {
2292 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002293 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002294 generic_make_request(wbio);
2295 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002296 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002297 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2298 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002299 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002300 generic_make_request(wbio2);
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/*
Robert Becker1e509152009-12-14 12:49:58 +11002305 * Used by fix_read_error() to decay the per rdev read_errors.
2306 * We halve the read error count for every hour that has elapsed
2307 * since the last recorded read error.
2308 *
2309 */
NeilBrownfd01b882011-10-11 16:47:53 +11002310static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002311{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002312 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002313 unsigned long hours_since_last;
2314 unsigned int read_errors = atomic_read(&rdev->read_errors);
2315
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002316 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002317
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002318 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002319 /* first time we've seen a read error */
2320 rdev->last_read_error = cur_time_mon;
2321 return;
2322 }
2323
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002324 hours_since_last = (long)(cur_time_mon -
2325 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002326
2327 rdev->last_read_error = cur_time_mon;
2328
2329 /*
2330 * if hours_since_last is > the number of bits in read_errors
2331 * just set read errors to 0. We do this to avoid
2332 * overflowing the shift of read_errors by hours_since_last.
2333 */
2334 if (hours_since_last >= 8 * sizeof(read_errors))
2335 atomic_set(&rdev->read_errors, 0);
2336 else
2337 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2338}
2339
NeilBrown3cb03002011-10-11 16:45:26 +11002340static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002341 int sectors, struct page *page, int rw)
2342{
2343 sector_t first_bad;
2344 int bad_sectors;
2345
2346 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2347 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2348 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002349 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002350 /* success */
2351 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002352 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002353 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002354 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2355 set_bit(MD_RECOVERY_NEEDED,
2356 &rdev->mddev->recovery);
2357 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002358 /* need to record an error - either for the block or the device */
2359 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2360 md_error(rdev->mddev, rdev);
2361 return 0;
2362}
2363
Robert Becker1e509152009-12-14 12:49:58 +11002364/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 * This is a kernel thread which:
2366 *
2367 * 1. Retries failed read operations on working mirrors.
2368 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002369 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 */
2371
NeilBrowne879a872011-10-11 16:49:02 +11002372static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002373{
2374 int sect = 0; /* Offset from r10_bio->sector */
2375 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002376 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002377 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002378 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002379
NeilBrown7c4e06f2011-05-11 14:53:17 +10002380 /* still own a reference to this rdev, so it cannot
2381 * have been cleared recently.
2382 */
2383 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002384
NeilBrown7c4e06f2011-05-11 14:53:17 +10002385 if (test_bit(Faulty, &rdev->flags))
2386 /* drive has already been failed, just ignore any
2387 more fix_read_error() attempts */
2388 return;
2389
2390 check_decay_read_errors(mddev, rdev);
2391 atomic_inc(&rdev->read_errors);
2392 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2393 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002394 bdevname(rdev->bdev, b);
2395
NeilBrown08464e02016-11-02 14:16:50 +11002396 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2397 mdname(mddev), b,
2398 atomic_read(&rdev->read_errors), max_read_errors);
2399 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2400 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002401 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002402 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002403 return;
Robert Becker1e509152009-12-14 12:49:58 +11002404 }
Robert Becker1e509152009-12-14 12:49:58 +11002405
NeilBrown6814d532006-10-03 01:15:45 -07002406 while(sectors) {
2407 int s = sectors;
2408 int sl = r10_bio->read_slot;
2409 int success = 0;
2410 int start;
2411
2412 if (s > (PAGE_SIZE>>9))
2413 s = PAGE_SIZE >> 9;
2414
2415 rcu_read_lock();
2416 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002417 sector_t first_bad;
2418 int bad_sectors;
2419
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002420 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002421 rdev = rcu_dereference(conf->mirrors[d].rdev);
2422 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002423 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002424 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002425 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2426 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002427 atomic_inc(&rdev->nr_pending);
2428 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002429 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002430 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002431 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002432 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002433 conf->tmppage,
2434 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002435 rdev_dec_pending(rdev, mddev);
2436 rcu_read_lock();
2437 if (success)
2438 break;
2439 }
2440 sl++;
2441 if (sl == conf->copies)
2442 sl = 0;
2443 } while (!success && sl != r10_bio->read_slot);
2444 rcu_read_unlock();
2445
2446 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002447 /* Cannot read from anywhere, just mark the block
2448 * as bad on the first device to discourage future
2449 * reads.
2450 */
NeilBrown6814d532006-10-03 01:15:45 -07002451 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002452 rdev = conf->mirrors[dn].rdev;
2453
2454 if (!rdev_set_badblocks(
2455 rdev,
2456 r10_bio->devs[r10_bio->read_slot].addr
2457 + sect,
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002458 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002459 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002460 r10_bio->devs[r10_bio->read_slot].bio
2461 = IO_BLOCKED;
2462 }
NeilBrown6814d532006-10-03 01:15:45 -07002463 break;
2464 }
2465
2466 start = sl;
2467 /* write it back and re-read */
2468 rcu_read_lock();
2469 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002470 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002471
NeilBrown6814d532006-10-03 01:15:45 -07002472 if (sl==0)
2473 sl = conf->copies;
2474 sl--;
2475 d = r10_bio->devs[sl].devnum;
2476 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002477 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002478 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002479 !test_bit(In_sync, &rdev->flags))
2480 continue;
2481
2482 atomic_inc(&rdev->nr_pending);
2483 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002484 if (r10_sync_page_io(rdev,
2485 r10_bio->devs[sl].addr +
2486 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002487 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002488 == 0) {
2489 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002490 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2491 mdname(mddev), s,
2492 (unsigned long long)(
2493 sect +
2494 choose_data_offset(r10_bio,
2495 rdev)),
2496 bdevname(rdev->bdev, b));
2497 pr_notice("md/raid10:%s: %s: failing drive\n",
2498 mdname(mddev),
2499 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002500 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002501 rdev_dec_pending(rdev, mddev);
2502 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002503 }
2504 sl = start;
2505 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002506 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002507
NeilBrown6814d532006-10-03 01:15:45 -07002508 if (sl==0)
2509 sl = conf->copies;
2510 sl--;
2511 d = r10_bio->devs[sl].devnum;
2512 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002513 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002514 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002515 !test_bit(In_sync, &rdev->flags))
2516 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002517
NeilBrown1294b9c2011-07-28 11:39:23 +10002518 atomic_inc(&rdev->nr_pending);
2519 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002520 switch (r10_sync_page_io(rdev,
2521 r10_bio->devs[sl].addr +
2522 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002523 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002524 READ)) {
2525 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002526 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002527 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002528 mdname(mddev), s,
2529 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002530 sect +
2531 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002532 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002533 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002534 mdname(mddev),
2535 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002536 break;
2537 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002538 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002539 mdname(mddev), s,
2540 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002541 sect +
2542 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002543 bdevname(rdev->bdev, b));
2544 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002545 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002546
2547 rdev_dec_pending(rdev, mddev);
2548 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002549 }
2550 rcu_read_unlock();
2551
2552 sectors -= s;
2553 sect += s;
2554 }
2555}
2556
NeilBrown9f2c9d12011-10-11 16:48:43 +11002557static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002558{
2559 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002560 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002561 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002562 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002563 /* bio has the data to be written to slot 'i' where
2564 * we just recently had a write error.
2565 * We repeatedly clone the bio and trim down to one block,
2566 * then try the write. Where the write fails we record
2567 * a bad block.
2568 * It is conceivable that the bio doesn't exactly align with
2569 * blocks. We must handle this.
2570 *
2571 * We currently own a reference to the rdev.
2572 */
2573
2574 int block_sectors;
2575 sector_t sector;
2576 int sectors;
2577 int sect_to_write = r10_bio->sectors;
2578 int ok = 1;
2579
2580 if (rdev->badblocks.shift < 0)
2581 return 0;
2582
NeilBrownf04ebb02015-02-16 14:51:54 +11002583 block_sectors = roundup(1 << rdev->badblocks.shift,
2584 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002585 sector = r10_bio->sector;
2586 sectors = ((r10_bio->sector + block_sectors)
2587 & ~(sector_t)(block_sectors - 1))
2588 - sector;
2589
2590 while (sect_to_write) {
2591 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002592 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002593 if (sectors > sect_to_write)
2594 sectors = sect_to_write;
2595 /* Write at 'sector' for 'sectors' */
Ming Leid7a10302017-02-14 23:29:03 +08002596 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002597 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002598 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2599 wbio->bi_iter.bi_sector = wsector +
2600 choose_data_offset(r10_bio, rdev);
NeilBrownbd870a12011-07-28 11:39:24 +10002601 wbio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002602 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002603
2604 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002605 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002606 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002607 sectors, 0)
2608 && ok;
2609
2610 bio_put(wbio);
2611 sect_to_write -= sectors;
2612 sector += sectors;
2613 sectors = block_sectors;
2614 }
2615 return ok;
2616}
2617
NeilBrown9f2c9d12011-10-11 16:48:43 +11002618static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002619{
2620 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002621 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002622 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002623 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002624 char b[BDEVNAME_SIZE];
2625 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002626 int max_sectors;
NeilBrown109e3762016-11-18 13:22:04 +11002627 dev_t bio_dev;
2628 sector_t bio_last_sector;
NeilBrown560f8e52011-07-28 11:39:23 +10002629
2630 /* we got a read error. Maybe the drive is bad. Maybe just
2631 * the block and we can fix it.
2632 * We freeze all other IO, and try reading the block from
2633 * other devices. When we find one, we re-write
2634 * and check it that fixes the read error.
2635 * This is all done synchronously while the array is
2636 * frozen.
2637 */
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002638 bio = r10_bio->devs[slot].bio;
2639 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002640 bio_dev = bio->bi_bdev->bd_dev;
2641 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002642 bio_put(bio);
2643 r10_bio->devs[slot].bio = NULL;
2644
NeilBrown8d3ca832016-11-18 16:16:12 +11002645 if (mddev->ro)
2646 r10_bio->devs[slot].bio = IO_BLOCKED;
2647 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002648 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002649 fix_read_error(conf, mddev, r10_bio);
2650 unfreeze_array(conf);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002651 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002652 md_error(mddev, rdev);
NeilBrownfae8cc5e2012-02-14 11:10:10 +11002653
NeilBrownabbf0982011-12-23 10:17:54 +11002654 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002655
NeilBrown7399c312011-07-28 11:39:23 +10002656read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002657 rdev = read_balance(conf, r10_bio, &max_sectors);
2658 if (rdev == NULL) {
NeilBrown08464e02016-11-02 14:16:50 +11002659 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2660 mdname(mddev), b,
2661 (unsigned long long)r10_bio->sector);
NeilBrown560f8e52011-07-28 11:39:23 +10002662 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002663 return;
2664 }
2665
Jens Axboe1eff9d32016-08-05 15:35:16 -06002666 do_sync = (r10_bio->master_bio->bi_opf & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002667 slot = r10_bio->read_slot;
NeilBrown08464e02016-11-02 14:16:50 +11002668 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2669 mdname(mddev),
2670 bdevname(rdev->bdev, b),
2671 (unsigned long long)r10_bio->sector);
Ming Leid7a10302017-02-14 23:29:03 +08002672 bio = bio_clone_fast(r10_bio->master_bio, GFP_NOIO, mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002673 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002674 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002675 r10_bio->devs[slot].rdev = rdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002676 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002677 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002678 bio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002679 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown8d3ca832016-11-18 16:16:12 +11002680 if (test_bit(FailFast, &rdev->flags) &&
2681 test_bit(R10BIO_FailFast, &r10_bio->state))
2682 bio->bi_opf |= MD_FAILFAST;
NeilBrown560f8e52011-07-28 11:39:23 +10002683 bio->bi_private = r10_bio;
2684 bio->bi_end_io = raid10_end_read_request;
NeilBrown109e3762016-11-18 13:22:04 +11002685 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2686 bio, bio_dev,
2687 bio_last_sector - r10_bio->sectors);
2688
NeilBrown7399c312011-07-28 11:39:23 +10002689 if (max_sectors < r10_bio->sectors) {
2690 /* Drat - have to split this up more */
2691 struct bio *mbio = r10_bio->master_bio;
2692 int sectors_handled =
2693 r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002694 - mbio->bi_iter.bi_sector;
NeilBrown7399c312011-07-28 11:39:23 +10002695 r10_bio->sectors = max_sectors;
2696 spin_lock_irq(&conf->device_lock);
2697 if (mbio->bi_phys_segments == 0)
2698 mbio->bi_phys_segments = 2;
2699 else
2700 mbio->bi_phys_segments++;
2701 spin_unlock_irq(&conf->device_lock);
2702 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002703
2704 r10_bio = mempool_alloc(conf->r10bio_pool,
2705 GFP_NOIO);
2706 r10_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002707 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown7399c312011-07-28 11:39:23 +10002708 r10_bio->state = 0;
2709 set_bit(R10BIO_ReadError,
2710 &r10_bio->state);
2711 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002712 r10_bio->sector = mbio->bi_iter.bi_sector
NeilBrown7399c312011-07-28 11:39:23 +10002713 + sectors_handled;
2714
2715 goto read_more;
2716 } else
2717 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002718}
2719
NeilBrowne879a872011-10-11 16:49:02 +11002720static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002721{
2722 /* Some sort of write request has finished and it
2723 * succeeded in writing where we thought there was a
2724 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002725 * Or possibly if failed and we need to record
2726 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002727 */
2728 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002729 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002730
2731 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2732 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002733 for (m = 0; m < conf->copies; m++) {
2734 int dev = r10_bio->devs[m].devnum;
2735 rdev = conf->mirrors[dev].rdev;
2736 if (r10_bio->devs[m].bio == NULL)
2737 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002738 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002739 rdev_clear_badblocks(
2740 rdev,
2741 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002742 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002743 } else {
2744 if (!rdev_set_badblocks(
2745 rdev,
2746 r10_bio->devs[m].addr,
2747 r10_bio->sectors, 0))
2748 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002749 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002750 rdev = conf->mirrors[dev].replacement;
2751 if (r10_bio->devs[m].repl_bio == NULL)
2752 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002753
2754 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002755 rdev_clear_badblocks(
2756 rdev,
2757 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002758 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002759 } else {
2760 if (!rdev_set_badblocks(
2761 rdev,
2762 r10_bio->devs[m].addr,
2763 r10_bio->sectors, 0))
2764 md_error(conf->mddev, rdev);
2765 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002766 }
NeilBrown749c55e2011-07-28 11:39:24 +10002767 put_buf(r10_bio);
2768 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002769 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002770 for (m = 0; m < conf->copies; m++) {
2771 int dev = r10_bio->devs[m].devnum;
2772 struct bio *bio = r10_bio->devs[m].bio;
2773 rdev = conf->mirrors[dev].rdev;
2774 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002775 rdev_clear_badblocks(
2776 rdev,
2777 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002778 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002779 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002780 } else if (bio != NULL && bio->bi_error) {
NeilBrown95af5872015-08-14 11:26:17 +10002781 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002782 if (!narrow_write_error(r10_bio, m)) {
2783 md_error(conf->mddev, rdev);
2784 set_bit(R10BIO_Degraded,
2785 &r10_bio->state);
2786 }
2787 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002788 }
NeilBrown475b0322011-12-23 10:17:55 +11002789 bio = r10_bio->devs[m].repl_bio;
2790 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002791 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002792 rdev_clear_badblocks(
2793 rdev,
2794 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002795 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002796 rdev_dec_pending(rdev, conf->mddev);
2797 }
NeilBrownbd870a12011-07-28 11:39:24 +10002798 }
NeilBrown95af5872015-08-14 11:26:17 +10002799 if (fail) {
2800 spin_lock_irq(&conf->device_lock);
2801 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002802 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002803 spin_unlock_irq(&conf->device_lock);
2804 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002805 } else {
2806 if (test_bit(R10BIO_WriteError,
2807 &r10_bio->state))
2808 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002809 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002810 }
NeilBrown749c55e2011-07-28 11:39:24 +10002811 }
2812}
2813
Shaohua Li4ed87312012-10-11 13:34:00 +11002814static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Shaohua Li4ed87312012-10-11 13:34:00 +11002816 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002817 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002819 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002821 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
NeilBrown95af5872015-08-14 11:26:17 +10002825 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002826 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown95af5872015-08-14 11:26:17 +10002827 LIST_HEAD(tmp);
2828 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002829 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002830 while (!list_empty(&conf->bio_end_io_list)) {
2831 list_move(conf->bio_end_io_list.prev, &tmp);
2832 conf->nr_queued--;
2833 }
NeilBrown95af5872015-08-14 11:26:17 +10002834 }
2835 spin_unlock_irqrestore(&conf->device_lock, flags);
2836 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002837 r10_bio = list_first_entry(&tmp, struct r10bio,
2838 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002839 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002840 if (mddev->degraded)
2841 set_bit(R10BIO_Degraded, &r10_bio->state);
2842
2843 if (test_bit(R10BIO_WriteError,
2844 &r10_bio->state))
2845 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002846 raid_end_bio_io(r10_bio);
2847 }
2848 }
2849
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002850 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002852
NeilBrown0021b7b2012-07-31 09:08:14 +02002853 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002856 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002857 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002859 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002860 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002862 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 spin_unlock_irqrestore(&conf->device_lock, flags);
2864
2865 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002866 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002867 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2868 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002869 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002870 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2871 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002872 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002874 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002876 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002877 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002878 else {
2879 /* just a partial read to be scheduled from a
2880 * separate context
2881 */
2882 int slot = r10_bio->read_slot;
2883 generic_make_request(r10_bio->devs[slot].bio);
2884 }
NeilBrown4443ae12006-01-06 00:20:28 -08002885
NeilBrown1d9d5242009-10-16 15:55:32 +11002886 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002887 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002888 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002890 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
NeilBrowne879a872011-10-11 16:49:02 +11002893static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
2895 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002896 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002899 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002900 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002901 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002902 if (conf->mirrors[i].replacement)
2903 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2905 if (!conf->r10buf_pool)
2906 return -ENOMEM;
2907 conf->next_resync = 0;
2908 return 0;
2909}
2910
2911/*
2912 * perform a "sync" on one "block"
2913 *
2914 * We need to make sure that no normal I/O request - particularly write
2915 * requests - conflict with active sync requests.
2916 *
2917 * This is achieved by tracking pending requests and a 'barrier' concept
2918 * that can be installed to exclude normal IO requests.
2919 *
2920 * Resync and recovery are handled very differently.
2921 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2922 *
2923 * For resync, we iterate over virtual addresses, read all copies,
2924 * and update if there are differences. If only one copy is live,
2925 * skip it.
2926 * For recovery, we iterate over physical addresses, read a good
2927 * value for each non-in_sync drive, and over-write.
2928 *
2929 * So, for recovery we may have several outstanding complex requests for a
2930 * given address, one for each out-of-sync device. We model this by allocating
2931 * a number of r10_bio structures, one for each out-of-sync device.
2932 * As we setup these structures, we collect all bio's together into a list
2933 * which we then process collectively to add pages, and then process again
2934 * to pass to generic_make_request.
2935 *
2936 * The r10_bio structures are linked using a borrowed master_bio pointer.
2937 * This link is counted in ->remaining. When the r10_bio that points to NULL
2938 * has its remaining count decremented to 0, the whole complex operation
2939 * is complete.
2940 *
2941 */
2942
Shaohua Li849674e2016-01-20 13:52:20 -08002943static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002944 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
NeilBrowne879a872011-10-11 16:49:02 +11002946 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002947 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 struct bio *biolist = NULL, *bio;
2949 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002951 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002952 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 sector_t sectors_skipped = 0;
2954 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002955 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 if (!conf->r10buf_pool)
2958 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002961 /*
2962 * Allow skipping a full rebuild for incremental assembly
2963 * of a clean array, like RAID1 does.
2964 */
2965 if (mddev->bitmap == NULL &&
2966 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002967 mddev->reshape_position == MaxSector &&
2968 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002969 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002970 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002971 conf->fullsync == 0) {
2972 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002973 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002974 }
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002977 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002978 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2979 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 max_sector = mddev->resync_max_sectors;
2981 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002982 /* If we aborted, we need to abort the
2983 * sync on the 'current' bitmap chucks (there can
2984 * be several when recovering multiple devices).
2985 * as we may have started syncing it but not finished.
2986 * We can find the current address in
2987 * mddev->curr_resync, but for recovery,
2988 * we need to convert that to several
2989 * virtual addresses.
2990 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002991 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2992 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002993 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002994 return 0;
2995 }
2996
NeilBrown6cce3b22006-01-06 00:20:16 -08002997 if (mddev->curr_resync < max_sector) { /* aborted */
2998 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2999 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3000 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003001 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003002 sector_t sect =
3003 raid10_find_virt(conf, mddev->curr_resync, i);
3004 bitmap_end_sync(mddev->bitmap, sect,
3005 &sync_blocks, 1);
3006 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11003007 } else {
3008 /* completed sync */
3009 if ((!mddev->bitmap || conf->fullsync)
3010 && conf->have_replacement
3011 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3012 /* Completed a full sync so the replacements
3013 * are now fully recovered.
3014 */
NeilBrownf90145f2016-06-02 16:19:52 +10003015 rcu_read_lock();
3016 for (i = 0; i < conf->geo.raid_disks; i++) {
3017 struct md_rdev *rdev =
3018 rcu_dereference(conf->mirrors[i].replacement);
3019 if (rdev)
3020 rdev->recovery_offset = MaxSector;
3021 }
3022 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003023 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003024 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003025 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003026 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07003028 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return sectors_skipped;
3030 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003031
3032 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3033 return reshape_request(mddev, sector_nr, skipped);
3034
NeilBrown5cf00fc2012-05-21 09:28:20 +10003035 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 /* if there has been nothing to do on any drive,
3037 * then there is nothing to do at all..
3038 */
NeilBrown57afd892005-06-21 17:17:13 -07003039 *skipped = 1;
3040 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042
NeilBrownc6207272008-02-06 01:39:52 -08003043 if (max_sector > mddev->resync_max)
3044 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 /* make sure whole request will fit in a chunk - if chunks
3047 * are meaningful
3048 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003049 if (conf->geo.near_copies < conf->geo.raid_disks &&
3050 max_sector > (sector_nr | chunk_mask))
3051 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02003053 /*
3054 * If there is non-resync activity waiting for a turn, then let it
3055 * though before starting on this new sync request.
3056 */
3057 if (conf->nr_waiting)
3058 schedule_timeout_uninterruptible(1);
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 /* Again, very different code for resync and recovery.
3061 * Both must result in an r10bio with a list of bios that
3062 * have bi_end_io, bi_sector, bi_bdev set,
3063 * and bi_private set to the r10bio.
3064 * For recovery, we may actually create several r10bios
3065 * with 2 bios in each, that correspond to the bios in the main one.
3066 * In this case, the subordinate r10bios link back through a
3067 * borrowed master_bio pointer, and the counter in the master
3068 * includes a ref from each subordinate.
3069 */
3070 /* First, we decide what to do and set ->bi_end_io
3071 * To end_sync_read if we want to read, and
3072 * end_sync_write if we will want to write.
3073 */
3074
NeilBrown6cce3b22006-01-06 00:20:16 -08003075 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3077 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003078 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 r10_bio = NULL;
3080
NeilBrown5cf00fc2012-05-21 09:28:20 +10003081 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003082 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003083 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003084 sector_t sect;
3085 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003086 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003087 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003088 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003089
NeilBrownf90145f2016-06-02 16:19:52 +10003090 rcu_read_lock();
3091 mrdev = rcu_dereference(mirror->rdev);
3092 mreplace = rcu_dereference(mirror->replacement);
3093
3094 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10003095 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10003096 test_bit(In_sync, &mrdev->flags)) &&
3097 (mreplace == NULL ||
3098 test_bit(Faulty, &mreplace->flags))) {
3099 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003100 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003101 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003102
3103 still_degraded = 0;
3104 /* want to reconstruct this device */
3105 rb2 = r10_bio;
3106 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003107 if (sect >= mddev->resync_max_sectors) {
3108 /* last stripe is not complete - don't
3109 * try to recover this sector.
3110 */
NeilBrownf90145f2016-06-02 16:19:52 +10003111 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003112 continue;
3113 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003114 if (mreplace && test_bit(Faulty, &mreplace->flags))
3115 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003116 /* Unless we are doing a full sync, or a replacement
3117 * we only need to recover the block if it is set in
3118 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003119 */
3120 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3121 &sync_blocks, 1);
3122 if (sync_blocks < max_sync)
3123 max_sync = sync_blocks;
3124 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003125 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003126 !conf->fullsync) {
3127 /* yep, skip the sync_blocks here, but don't assume
3128 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003129 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003130 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003131 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003132 continue;
3133 }
NeilBrownf90145f2016-06-02 16:19:52 +10003134 atomic_inc(&mrdev->nr_pending);
3135 if (mreplace)
3136 atomic_inc(&mreplace->nr_pending);
3137 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
NeilBrownab9d47e2011-05-11 14:54:41 +10003139 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003140 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003141 raise_barrier(conf, rb2 != NULL);
3142 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
NeilBrownab9d47e2011-05-11 14:54:41 +10003144 r10_bio->master_bio = (struct bio*)rb2;
3145 if (rb2)
3146 atomic_inc(&rb2->remaining);
3147 r10_bio->mddev = mddev;
3148 set_bit(R10BIO_IsRecover, &r10_bio->state);
3149 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003150
NeilBrownab9d47e2011-05-11 14:54:41 +10003151 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003152
NeilBrownab9d47e2011-05-11 14:54:41 +10003153 /* Need to check if the array will still be
3154 * degraded
3155 */
NeilBrownf90145f2016-06-02 16:19:52 +10003156 rcu_read_lock();
3157 for (j = 0; j < conf->geo.raid_disks; j++) {
3158 struct md_rdev *rdev = rcu_dereference(
3159 conf->mirrors[j].rdev);
3160 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003161 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
NeilBrownf90145f2016-06-02 16:19:52 +10003164 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003165
3166 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3167 &sync_blocks, still_degraded);
3168
NeilBrowne875ece2011-07-28 11:39:24 +10003169 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003170 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003171 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003172 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003173 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003174 struct md_rdev *rdev =
3175 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003176 sector_t sector, first_bad;
3177 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003178 if (!rdev ||
3179 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003180 continue;
3181 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003182 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003183 sector = r10_bio->devs[j].addr;
3184
3185 if (is_badblock(rdev, sector, max_sync,
3186 &first_bad, &bad_sectors)) {
3187 if (first_bad > sector)
3188 max_sync = first_bad - sector;
3189 else {
3190 bad_sectors -= (sector
3191 - first_bad);
3192 if (max_sync > bad_sectors)
3193 max_sync = bad_sectors;
3194 continue;
3195 }
3196 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003197 bio = r10_bio->devs[0].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003198 bio_reset(bio);
NeilBrownab9d47e2011-05-11 14:54:41 +10003199 bio->bi_next = biolist;
3200 biolist = bio;
3201 bio->bi_private = r10_bio;
3202 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003203 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003204 if (test_bit(FailFast, &rdev->flags))
3205 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003206 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003207 bio->bi_iter.bi_sector = from_addr +
3208 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003209 bio->bi_bdev = rdev->bdev;
3210 atomic_inc(&rdev->nr_pending);
3211 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003212
3213 for (k=0; k<conf->copies; k++)
3214 if (r10_bio->devs[k].devnum == i)
3215 break;
3216 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003217 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003218 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003219 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003220 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003221 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003222
NeilBrownf90145f2016-06-02 16:19:52 +10003223 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003224 bio = r10_bio->devs[1].bio;
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 + mrdev->data_offset;
3233 bio->bi_bdev = mrdev->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003234 atomic_inc(&r10_bio->remaining);
3235 } else
3236 r10_bio->devs[1].bio->bi_end_io = NULL;
3237
3238 /* and maybe write to replacement */
3239 bio = r10_bio->devs[1].repl_bio;
3240 if (bio)
3241 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003242 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003243 * cannot be NULL as r10buf_pool_alloc will
3244 * have allocated it.
3245 * So the second test here is pointless.
3246 * But it keeps semantic-checkers happy, and
3247 * this comment keeps human reviewers
3248 * happy.
3249 */
NeilBrownf90145f2016-06-02 16:19:52 +10003250 if (mreplace == NULL || bio == NULL ||
3251 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003252 break;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003253 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003254 bio->bi_next = biolist;
3255 biolist = bio;
3256 bio->bi_private = r10_bio;
3257 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003258 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003259 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003260 mreplace->data_offset;
3261 bio->bi_bdev = mreplace->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003262 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
NeilBrownf90145f2016-06-02 16:19:52 +10003265 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003266 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003267 /* Cannot recover, so abort the recovery or
3268 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003269 if (any_working) {
3270 /* problem is that there are bad blocks
3271 * on other device(s)
3272 */
3273 int k;
3274 for (k = 0; k < conf->copies; k++)
3275 if (r10_bio->devs[k].devnum == i)
3276 break;
NeilBrown24afd802011-12-23 10:17:55 +11003277 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003278 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003279 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003280 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003281 r10_bio->devs[k].addr,
3282 max_sync, 0))
3283 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003284 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003285 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003286 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003287 r10_bio->devs[k].addr,
3288 max_sync, 0))
3289 any_working = 0;
3290 }
3291 if (!any_working) {
3292 if (!test_and_set_bit(MD_RECOVERY_INTR,
3293 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003294 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003295 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003296 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003297 = mddev->recovery_disabled;
3298 }
NeilBrowne8b84912014-01-06 10:35:34 +11003299 put_buf(r10_bio);
3300 if (rb2)
3301 atomic_dec(&rb2->remaining);
3302 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003303 rdev_dec_pending(mrdev, mddev);
3304 if (mreplace)
3305 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003306 break;
3307 }
NeilBrownf90145f2016-06-02 16:19:52 +10003308 rdev_dec_pending(mrdev, mddev);
3309 if (mreplace)
3310 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003311 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3312 /* Only want this if there is elsewhere to
3313 * read from. 'j' is currently the first
3314 * readable copy.
3315 */
3316 int targets = 1;
3317 for (; j < conf->copies; j++) {
3318 int d = r10_bio->devs[j].devnum;
3319 if (conf->mirrors[d].rdev &&
3320 test_bit(In_sync,
3321 &conf->mirrors[d].rdev->flags))
3322 targets++;
3323 }
3324 if (targets == 1)
3325 r10_bio->devs[0].bio->bi_opf
3326 &= ~MD_FAILFAST;
3327 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (biolist == NULL) {
3330 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003331 struct r10bio *rb2 = r10_bio;
3332 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 rb2->master_bio = NULL;
3334 put_buf(rb2);
3335 }
3336 goto giveup;
3337 }
3338 } else {
3339 /* resync. Schedule a read for every block at this virt offset */
3340 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003341
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003342 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003343
NeilBrown6cce3b22006-01-06 00:20:16 -08003344 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3345 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003346 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3347 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003348 /* We can skip this block */
3349 *skipped = 1;
3350 return sync_blocks + sectors_skipped;
3351 }
3352 if (sync_blocks < max_sync)
3353 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003355 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 r10_bio->mddev = mddev;
3358 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003359 raise_barrier(conf, 0);
3360 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 r10_bio->master_bio = NULL;
3363 r10_bio->sector = sector_nr;
3364 set_bit(R10BIO_IsSync, &r10_bio->state);
3365 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003366 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
NeilBrown5cf00fc2012-05-21 09:28:20 +10003368 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003370 sector_t first_bad, sector;
3371 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003372 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003373
NeilBrown9ad1aef2011-12-23 10:17:55 +11003374 if (r10_bio->devs[i].repl_bio)
3375 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 bio = r10_bio->devs[i].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003378 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003379 bio->bi_error = -EIO;
NeilBrownf90145f2016-06-02 16:19:52 +10003380 rcu_read_lock();
3381 rdev = rcu_dereference(conf->mirrors[d].rdev);
3382 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3383 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003385 }
NeilBrown40c356c2011-07-28 11:39:24 +10003386 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003387 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003388 &first_bad, &bad_sectors)) {
3389 if (first_bad > sector)
3390 max_sync = first_bad - sector;
3391 else {
3392 bad_sectors -= (sector - first_bad);
3393 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003394 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003395 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003396 continue;
3397 }
3398 }
NeilBrownf90145f2016-06-02 16:19:52 +10003399 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 atomic_inc(&r10_bio->remaining);
3401 bio->bi_next = biolist;
3402 biolist = bio;
3403 bio->bi_private = r10_bio;
3404 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003405 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003406 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3407 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003408 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3409 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003411
NeilBrownf90145f2016-06-02 16:19:52 +10003412 rdev = rcu_dereference(conf->mirrors[d].replacement);
3413 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3414 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003415 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003416 }
3417 atomic_inc(&rdev->nr_pending);
3418 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003419
3420 /* Need to set up for writing to the replacement */
3421 bio = r10_bio->devs[i].repl_bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003422 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003423 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003424
3425 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003426 bio->bi_next = biolist;
3427 biolist = bio;
3428 bio->bi_private = r10_bio;
3429 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003430 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
NeilBrown1919cbb2016-11-18 16:16:12 +11003431 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3432 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003433 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3434 bio->bi_bdev = rdev->bdev;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003435 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
3437
3438 if (count < 2) {
3439 for (i=0; i<conf->copies; i++) {
3440 int d = r10_bio->devs[i].devnum;
3441 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003442 rdev_dec_pending(conf->mirrors[d].rdev,
3443 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003444 if (r10_bio->devs[i].repl_bio &&
3445 r10_bio->devs[i].repl_bio->bi_end_io)
3446 rdev_dec_pending(
3447 conf->mirrors[d].replacement,
3448 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
3450 put_buf(r10_bio);
3451 biolist = NULL;
3452 goto giveup;
3453 }
3454 }
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003457 if (sector_nr + max_sync < max_sector)
3458 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 do {
3460 struct page *page;
3461 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (sector_nr + (len>>9) > max_sector)
3463 len = (max_sector - sector_nr) << 9;
3464 if (len == 0)
3465 break;
3466 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003467 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003469 if (bio_add_page(bio, page, len, 0))
3470 continue;
3471
3472 /* stop here */
3473 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3474 for (bio2 = biolist;
3475 bio2 && bio2 != bio;
3476 bio2 = bio2->bi_next) {
3477 /* remove last page from this bio */
3478 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003479 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06003480 bio_clear_flag(bio2, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003482 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 }
3484 nr_sectors += len>>9;
3485 sector_nr += len>>9;
3486 } while (biolist->bi_vcnt < RESYNC_PAGES);
3487 bio_full:
3488 r10_bio->sectors = nr_sectors;
3489
3490 while (biolist) {
3491 bio = biolist;
3492 biolist = biolist->bi_next;
3493
3494 bio->bi_next = NULL;
3495 r10_bio = bio->bi_private;
3496 r10_bio->sectors = nr_sectors;
3497
3498 if (bio->bi_end_io == end_sync_read) {
3499 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003500 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 generic_make_request(bio);
3502 }
3503 }
3504
NeilBrown57afd892005-06-21 17:17:13 -07003505 if (sectors_skipped)
3506 /* pretend they weren't skipped, it makes
3507 * no important difference in this case
3508 */
3509 md_done_sync(mddev, sectors_skipped, 1);
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 return sectors_skipped + nr_sectors;
3512 giveup:
3513 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003514 * drives must be failed or in resync, all drives
3515 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 */
NeilBrown09b40682009-02-25 13:18:47 +11003517 if (sector_nr + max_sync < max_sector)
3518 max_sector = sector_nr + max_sync;
3519
3520 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 chunks_skipped ++;
3522 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
Dan Williams80c3a6c2009-03-17 18:10:40 -07003526static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003527raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003528{
3529 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003530 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003531
3532 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003533 raid_disks = min(conf->geo.raid_disks,
3534 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003535 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003536 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003537
NeilBrown5cf00fc2012-05-21 09:28:20 +10003538 size = sectors >> conf->geo.chunk_shift;
3539 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003540 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003541 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003542
NeilBrown5cf00fc2012-05-21 09:28:20 +10003543 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003544}
3545
NeilBrown6508fdb2012-05-17 10:08:45 +10003546static void calc_sectors(struct r10conf *conf, sector_t size)
3547{
3548 /* Calculate the number of sectors-per-device that will
3549 * actually be used, and set conf->dev_sectors and
3550 * conf->stride
3551 */
3552
NeilBrown5cf00fc2012-05-21 09:28:20 +10003553 size = size >> conf->geo.chunk_shift;
3554 sector_div(size, conf->geo.far_copies);
3555 size = size * conf->geo.raid_disks;
3556 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003557 /* 'size' is now the number of chunks in the array */
3558 /* calculate "used chunks per device" */
3559 size = size * conf->copies;
3560
3561 /* We need to round up when dividing by raid_disks to
3562 * get the stride size.
3563 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003564 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003565
NeilBrown5cf00fc2012-05-21 09:28:20 +10003566 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003567
NeilBrown5cf00fc2012-05-21 09:28:20 +10003568 if (conf->geo.far_offset)
3569 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003570 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003571 sector_div(size, conf->geo.far_copies);
3572 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003573 }
3574}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003575
NeilBrowndeb200d2012-05-21 09:28:33 +10003576enum geo_type {geo_new, geo_old, geo_start};
3577static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3578{
3579 int nc, fc, fo;
3580 int layout, chunk, disks;
3581 switch (new) {
3582 case geo_old:
3583 layout = mddev->layout;
3584 chunk = mddev->chunk_sectors;
3585 disks = mddev->raid_disks - mddev->delta_disks;
3586 break;
3587 case geo_new:
3588 layout = mddev->new_layout;
3589 chunk = mddev->new_chunk_sectors;
3590 disks = mddev->raid_disks;
3591 break;
3592 default: /* avoid 'may be unused' warnings */
3593 case geo_start: /* new when starting reshape - raid_disks not
3594 * updated yet. */
3595 layout = mddev->new_layout;
3596 chunk = mddev->new_chunk_sectors;
3597 disks = mddev->raid_disks + mddev->delta_disks;
3598 break;
3599 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003600 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003601 return -1;
3602 if (chunk < (PAGE_SIZE >> 9) ||
3603 !is_power_of_2(chunk))
3604 return -2;
3605 nc = layout & 255;
3606 fc = (layout >> 8) & 255;
3607 fo = layout & (1<<16);
3608 geo->raid_disks = disks;
3609 geo->near_copies = nc;
3610 geo->far_copies = fc;
3611 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003612 switch (layout >> 17) {
3613 case 0: /* original layout. simple but not always optimal */
3614 geo->far_set_size = disks;
3615 break;
3616 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3617 * actually using this, but leave code here just in case.*/
3618 geo->far_set_size = disks/fc;
3619 WARN(geo->far_set_size < fc,
3620 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3621 break;
3622 case 2: /* "improved" layout fixed to match documentation */
3623 geo->far_set_size = fc * nc;
3624 break;
3625 default: /* Not a valid layout */
3626 return -1;
3627 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003628 geo->chunk_mask = chunk - 1;
3629 geo->chunk_shift = ffz(~chunk);
3630 return nc*fc;
3631}
3632
NeilBrowne879a872011-10-11 16:49:02 +11003633static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
NeilBrowne879a872011-10-11 16:49:02 +11003635 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003636 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003637 struct geom geo;
3638 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
NeilBrowndeb200d2012-05-21 09:28:33 +10003640 copies = setup_geo(&geo, mddev, geo_new);
3641
3642 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003643 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3644 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003645 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
NeilBrown2604b702006-01-06 00:20:36 -08003647
NeilBrowndeb200d2012-05-21 09:28:33 +10003648 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003649 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3650 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 goto out;
3652 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003653
3654 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003655 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003656 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003658
NeilBrown3ea7daa2012-05-22 13:53:47 +10003659 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003660 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003661 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003662 GFP_KERNEL);
3663 if (!conf->mirrors)
3664 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003665
3666 conf->tmppage = alloc_page(GFP_KERNEL);
3667 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003668 goto out;
3669
NeilBrowndeb200d2012-05-21 09:28:33 +10003670 conf->geo = geo;
3671 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003672 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3673 r10bio_pool_free, conf);
3674 if (!conf->r10bio_pool)
3675 goto out;
3676
NeilBrown6508fdb2012-05-17 10:08:45 +10003677 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003678 if (mddev->reshape_position == MaxSector) {
3679 conf->prev = conf->geo;
3680 conf->reshape_progress = MaxSector;
3681 } else {
3682 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3683 err = -EINVAL;
3684 goto out;
3685 }
3686 conf->reshape_progress = mddev->reshape_position;
3687 if (conf->prev.far_offset)
3688 conf->prev.stride = 1 << conf->prev.chunk_shift;
3689 else
3690 /* far_copies must be 1 */
3691 conf->prev.stride = conf->dev_sectors;
3692 }
NeilBrown299b0682015-07-06 17:37:49 +10003693 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003694 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003695 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003696 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003697
3698 spin_lock_init(&conf->resync_lock);
3699 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003700 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003701
NeilBrown02326052012-07-03 15:56:52 +10003702 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003703 if (!conf->thread)
3704 goto out;
3705
Trela, Maciejdab8b292010-03-08 16:02:45 +11003706 conf->mddev = mddev;
3707 return conf;
3708
3709 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003710 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003711 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003712 kfree(conf->mirrors);
3713 safe_put_page(conf->tmppage);
3714 kfree(conf);
3715 }
3716 return ERR_PTR(err);
3717}
3718
Shaohua Li849674e2016-01-20 13:52:20 -08003719static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003720{
NeilBrowne879a872011-10-11 16:49:02 +11003721 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003722 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003723 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003724 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003725 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003726 sector_t min_offset_diff = 0;
3727 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003728 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003729
3730 if (mddev->private == NULL) {
3731 conf = setup_conf(mddev);
3732 if (IS_ERR(conf))
3733 return PTR_ERR(conf);
3734 mddev->private = conf;
3735 }
3736 conf = mddev->private;
3737 if (!conf)
3738 goto out;
3739
Trela, Maciejdab8b292010-03-08 16:02:45 +11003740 mddev->thread = conf->thread;
3741 conf->thread = NULL;
3742
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003743 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003744 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003745 blk_queue_max_discard_sectors(mddev->queue,
3746 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003747 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003748 blk_queue_io_min(mddev->queue, chunk_size);
3749 if (conf->geo.raid_disks % conf->geo.near_copies)
3750 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3751 else
3752 blk_queue_io_opt(mddev->queue, chunk_size *
3753 (conf->geo.raid_disks / conf->geo.near_copies));
3754 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003755
NeilBrowndafb20f2012-03-19 12:46:39 +11003756 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003757 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003758 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003761 if (disk_idx < 0)
3762 continue;
3763 if (disk_idx >= conf->geo.raid_disks &&
3764 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 continue;
3766 disk = conf->mirrors + disk_idx;
3767
NeilBrown56a2559b2011-12-23 10:17:55 +11003768 if (test_bit(Replacement, &rdev->flags)) {
3769 if (disk->replacement)
3770 goto out_free_conf;
3771 disk->replacement = rdev;
3772 } else {
3773 if (disk->rdev)
3774 goto out_free_conf;
3775 disk->rdev = rdev;
3776 }
NeilBrownaba336b2012-05-31 15:39:11 +10003777 q = bdev_get_queue(rdev->bdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003778 diff = (rdev->new_data_offset - rdev->data_offset);
3779 if (!mddev->reshape_backwards)
3780 diff = -diff;
3781 if (diff < 0)
3782 diff = 0;
3783 if (first || diff < min_offset_diff)
3784 min_offset_diff = diff;
NeilBrown56a2559b2011-12-23 10:17:55 +11003785
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003786 if (mddev->gendisk)
3787 disk_stack_limits(mddev->gendisk, rdev->bdev,
3788 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003791
3792 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3793 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003795
Jonathan Brassowed30be02012-10-31 11:42:30 +11003796 if (mddev->queue) {
3797 if (discard_supported)
3798 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3799 mddev->queue);
3800 else
3801 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3802 mddev->queue);
3803 }
NeilBrown6d508242005-09-09 16:24:03 -07003804 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003805 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003806 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003807 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 goto out_free_conf;
3809 }
3810
NeilBrown3ea7daa2012-05-22 13:53:47 +10003811 if (conf->reshape_progress != MaxSector) {
3812 /* must ensure that shape change is supported */
3813 if (conf->geo.far_copies != 1 &&
3814 conf->geo.far_offset == 0)
3815 goto out_free_conf;
3816 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003817 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003818 goto out_free_conf;
3819 }
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003822 for (i = 0;
3823 i < conf->geo.raid_disks
3824 || i < conf->prev.raid_disks;
3825 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 disk = conf->mirrors + i;
3828
NeilBrown56a2559b2011-12-23 10:17:55 +11003829 if (!disk->rdev && disk->replacement) {
3830 /* The replacement is all we have - use it */
3831 disk->rdev = disk->replacement;
3832 disk->replacement = NULL;
3833 clear_bit(Replacement, &disk->rdev->flags);
3834 }
3835
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003836 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003837 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 disk->head_position = 0;
3839 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003840 if (disk->rdev &&
3841 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003842 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
NeilBrownd890fa22011-10-26 11:54:39 +11003844 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 }
3846
Andre Noll8c6ac8682009-06-18 08:48:06 +10003847 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003848 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3849 mdname(mddev));
3850 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003851 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3852 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 /*
3854 * Ok, everything is just fine now
3855 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003856 mddev->dev_sectors = conf->dev_sectors;
3857 size = raid10_size(mddev, 0, 0);
3858 md_set_array_sectors(mddev, size);
3859 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003860 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003862 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003863 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003864 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003865
3866 /* Calculate max read-ahead size.
3867 * We need to readahead at least twice a whole stripe....
3868 * maybe...
3869 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003870 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01003871 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3872 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
3874
Martin K. Petersena91a2782011-03-17 11:11:05 +01003875 if (md_integrity_register(mddev))
3876 goto out_free_conf;
3877
NeilBrown3ea7daa2012-05-22 13:53:47 +10003878 if (conf->reshape_progress != MaxSector) {
3879 unsigned long before_length, after_length;
3880
3881 before_length = ((1 << conf->prev.chunk_shift) *
3882 conf->prev.far_copies);
3883 after_length = ((1 << conf->geo.chunk_shift) *
3884 conf->geo.far_copies);
3885
3886 if (max(before_length, after_length) > min_offset_diff) {
3887 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003888 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003889 goto out_free_conf;
3890 }
3891 conf->offset_diff = min_offset_diff;
3892
NeilBrown3ea7daa2012-05-22 13:53:47 +10003893 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3894 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3895 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3896 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3897 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3898 "reshape");
3899 }
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return 0;
3902
3903out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003904 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003905 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003906 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003907 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 kfree(conf);
3909 mddev->private = NULL;
3910out:
3911 return -EIO;
3912}
3913
NeilBrownafa0f552014-12-15 12:56:58 +11003914static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
NeilBrownafa0f552014-12-15 12:56:58 +11003916 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Julia Lawall644df1a2015-09-13 14:15:10 +02003918 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003919 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003920 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003921 kfree(conf->mirrors_old);
3922 kfree(conf->mirrors_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924}
3925
NeilBrownfd01b882011-10-11 16:47:53 +11003926static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003927{
NeilBrowne879a872011-10-11 16:49:02 +11003928 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003929
3930 switch(state) {
3931 case 1:
3932 raise_barrier(conf, 0);
3933 break;
3934 case 0:
3935 lower_barrier(conf);
3936 break;
3937 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
NeilBrown006a09a2012-03-19 12:46:40 +11003940static int raid10_resize(struct mddev *mddev, sector_t sectors)
3941{
3942 /* Resize of 'far' arrays is not supported.
3943 * For 'near' and 'offset' arrays we can set the
3944 * number of sectors used to be an appropriate multiple
3945 * of the chunk size.
3946 * For 'offset', this is far_copies*chunksize.
3947 * For 'near' the multiplier is the LCM of
3948 * near_copies and raid_disks.
3949 * So if far_copies > 1 && !far_offset, fail.
3950 * Else find LCM(raid_disks, near_copy)*far_copies and
3951 * multiply by chunk_size. Then round to this number.
3952 * This is mostly done by raid10_size()
3953 */
3954 struct r10conf *conf = mddev->private;
3955 sector_t oldsize, size;
3956
NeilBrownf8c9e742012-05-21 09:28:33 +10003957 if (mddev->reshape_position != MaxSector)
3958 return -EBUSY;
3959
NeilBrown5cf00fc2012-05-21 09:28:20 +10003960 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003961 return -EINVAL;
3962
3963 oldsize = raid10_size(mddev, 0, 0);
3964 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003965 if (mddev->external_size &&
3966 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003967 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003968 if (mddev->bitmap) {
3969 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3970 if (ret)
3971 return ret;
3972 }
3973 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003974 if (sectors > mddev->dev_sectors &&
3975 mddev->recovery_cp > oldsize) {
3976 mddev->recovery_cp = oldsize;
3977 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3978 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003979 calc_sectors(conf, sectors);
3980 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003981 mddev->resync_max_sectors = size;
3982 return 0;
3983}
3984
NeilBrown53a6ab42015-02-12 14:09:57 +11003985static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003986{
NeilBrown3cb03002011-10-11 16:45:26 +11003987 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003988 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003989
3990 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003991 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3992 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003993 return ERR_PTR(-EINVAL);
3994 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003995 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003996
Trela, Maciejdab8b292010-03-08 16:02:45 +11003997 /* Set new parameters */
3998 mddev->new_level = 10;
3999 /* new layout: far_copies = 1, near_copies = 2 */
4000 mddev->new_layout = (1<<8) + 2;
4001 mddev->new_chunk_sectors = mddev->chunk_sectors;
4002 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004003 mddev->raid_disks *= 2;
4004 /* make sure it will be not marked as dirty */
4005 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11004006 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004007
4008 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004009 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11004010 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11004011 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004012 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11004013 rdev->sectors = size;
4014 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01004015 conf->barrier = 1;
4016 }
4017
Trela, Maciejdab8b292010-03-08 16:02:45 +11004018 return conf;
4019}
4020
NeilBrownfd01b882011-10-11 16:47:53 +11004021static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11004022{
NeilBrowne373ab12011-10-11 16:48:59 +11004023 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11004024
4025 /* raid10 can take over:
4026 * raid0 - providing it has only two drives
4027 */
4028 if (mddev->level == 0) {
4029 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11004030 raid0_conf = mddev->private;
4031 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11004032 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4033 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11004034 return ERR_PTR(-EINVAL);
4035 }
NeilBrown53a6ab42015-02-12 14:09:57 +11004036 return raid10_takeover_raid0(mddev,
4037 raid0_conf->strip_zone->zone_end,
4038 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11004039 }
4040 return ERR_PTR(-EINVAL);
4041}
4042
NeilBrown3ea7daa2012-05-22 13:53:47 +10004043static int raid10_check_reshape(struct mddev *mddev)
4044{
4045 /* Called when there is a request to change
4046 * - layout (to ->new_layout)
4047 * - chunk size (to ->new_chunk_sectors)
4048 * - raid_disks (by delta_disks)
4049 * or when trying to restart a reshape that was ongoing.
4050 *
4051 * We need to validate the request and possibly allocate
4052 * space if that might be an issue later.
4053 *
4054 * Currently we reject any reshape of a 'far' mode array,
4055 * allow chunk size to change if new is generally acceptable,
4056 * allow raid_disks to increase, and allow
4057 * a switch between 'near' mode and 'offset' mode.
4058 */
4059 struct r10conf *conf = mddev->private;
4060 struct geom geo;
4061
4062 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4063 return -EINVAL;
4064
4065 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4066 /* mustn't change number of copies */
4067 return -EINVAL;
4068 if (geo.far_copies > 1 && !geo.far_offset)
4069 /* Cannot switch to 'far' mode */
4070 return -EINVAL;
4071
4072 if (mddev->array_sectors & geo.chunk_mask)
4073 /* not factor of array size */
4074 return -EINVAL;
4075
NeilBrown3ea7daa2012-05-22 13:53:47 +10004076 if (!enough(conf, -1))
4077 return -EINVAL;
4078
4079 kfree(conf->mirrors_new);
4080 conf->mirrors_new = NULL;
4081 if (mddev->delta_disks > 0) {
4082 /* allocate new 'mirrors' list */
4083 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004084 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004085 *(mddev->raid_disks +
4086 mddev->delta_disks),
4087 GFP_KERNEL);
4088 if (!conf->mirrors_new)
4089 return -ENOMEM;
4090 }
4091 return 0;
4092}
4093
4094/*
4095 * Need to check if array has failed when deciding whether to:
4096 * - start an array
4097 * - remove non-faulty devices
4098 * - add a spare
4099 * - allow a reshape
4100 * This determination is simple when no reshape is happening.
4101 * However if there is a reshape, we need to carefully check
4102 * both the before and after sections.
4103 * This is because some failed devices may only affect one
4104 * of the two sections, and some non-in_sync devices may
4105 * be insync in the section most affected by failed devices.
4106 */
4107static int calc_degraded(struct r10conf *conf)
4108{
4109 int degraded, degraded2;
4110 int i;
4111
4112 rcu_read_lock();
4113 degraded = 0;
4114 /* 'prev' section first */
4115 for (i = 0; i < conf->prev.raid_disks; i++) {
4116 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4117 if (!rdev || test_bit(Faulty, &rdev->flags))
4118 degraded++;
4119 else if (!test_bit(In_sync, &rdev->flags))
4120 /* When we can reduce the number of devices in
4121 * an array, this might not contribute to
4122 * 'degraded'. It does now.
4123 */
4124 degraded++;
4125 }
4126 rcu_read_unlock();
4127 if (conf->geo.raid_disks == conf->prev.raid_disks)
4128 return degraded;
4129 rcu_read_lock();
4130 degraded2 = 0;
4131 for (i = 0; i < conf->geo.raid_disks; i++) {
4132 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4133 if (!rdev || test_bit(Faulty, &rdev->flags))
4134 degraded2++;
4135 else if (!test_bit(In_sync, &rdev->flags)) {
4136 /* If reshape is increasing the number of devices,
4137 * this section has already been recovered, so
4138 * it doesn't contribute to degraded.
4139 * else it does.
4140 */
4141 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4142 degraded2++;
4143 }
4144 }
4145 rcu_read_unlock();
4146 if (degraded2 > degraded)
4147 return degraded2;
4148 return degraded;
4149}
4150
4151static int raid10_start_reshape(struct mddev *mddev)
4152{
4153 /* A 'reshape' has been requested. This commits
4154 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4155 * This also checks if there are enough spares and adds them
4156 * to the array.
4157 * We currently require enough spares to make the final
4158 * array non-degraded. We also require that the difference
4159 * between old and new data_offset - on each device - is
4160 * enough that we never risk over-writing.
4161 */
4162
4163 unsigned long before_length, after_length;
4164 sector_t min_offset_diff = 0;
4165 int first = 1;
4166 struct geom new;
4167 struct r10conf *conf = mddev->private;
4168 struct md_rdev *rdev;
4169 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004170 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004171
4172 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4173 return -EBUSY;
4174
4175 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4176 return -EINVAL;
4177
4178 before_length = ((1 << conf->prev.chunk_shift) *
4179 conf->prev.far_copies);
4180 after_length = ((1 << conf->geo.chunk_shift) *
4181 conf->geo.far_copies);
4182
4183 rdev_for_each(rdev, mddev) {
4184 if (!test_bit(In_sync, &rdev->flags)
4185 && !test_bit(Faulty, &rdev->flags))
4186 spares++;
4187 if (rdev->raid_disk >= 0) {
4188 long long diff = (rdev->new_data_offset
4189 - rdev->data_offset);
4190 if (!mddev->reshape_backwards)
4191 diff = -diff;
4192 if (diff < 0)
4193 diff = 0;
4194 if (first || diff < min_offset_diff)
4195 min_offset_diff = diff;
4196 }
4197 }
4198
4199 if (max(before_length, after_length) > min_offset_diff)
4200 return -EINVAL;
4201
4202 if (spares < mddev->delta_disks)
4203 return -EINVAL;
4204
4205 conf->offset_diff = min_offset_diff;
4206 spin_lock_irq(&conf->device_lock);
4207 if (conf->mirrors_new) {
4208 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004209 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004210 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004211 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004212 conf->mirrors_old = conf->mirrors;
4213 conf->mirrors = conf->mirrors_new;
4214 conf->mirrors_new = NULL;
4215 }
4216 setup_geo(&conf->geo, mddev, geo_start);
4217 smp_mb();
4218 if (mddev->reshape_backwards) {
4219 sector_t size = raid10_size(mddev, 0, 0);
4220 if (size < mddev->array_sectors) {
4221 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004222 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4223 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004224 return -EINVAL;
4225 }
4226 mddev->resync_max_sectors = size;
4227 conf->reshape_progress = size;
4228 } else
4229 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004230 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004231 spin_unlock_irq(&conf->device_lock);
4232
NeilBrownbb63a702012-05-22 13:55:28 +10004233 if (mddev->delta_disks && mddev->bitmap) {
4234 ret = bitmap_resize(mddev->bitmap,
4235 raid10_size(mddev, 0,
4236 conf->geo.raid_disks),
4237 0, 0);
4238 if (ret)
4239 goto abort;
4240 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004241 if (mddev->delta_disks > 0) {
4242 rdev_for_each(rdev, mddev)
4243 if (rdev->raid_disk < 0 &&
4244 !test_bit(Faulty, &rdev->flags)) {
4245 if (raid10_add_disk(mddev, rdev) == 0) {
4246 if (rdev->raid_disk >=
4247 conf->prev.raid_disks)
4248 set_bit(In_sync, &rdev->flags);
4249 else
4250 rdev->recovery_offset = 0;
4251
4252 if (sysfs_link_rdev(mddev, rdev))
4253 /* Failure here is OK */;
4254 }
4255 } else if (rdev->raid_disk >= conf->prev.raid_disks
4256 && !test_bit(Faulty, &rdev->flags)) {
4257 /* This is a spare that was manually added */
4258 set_bit(In_sync, &rdev->flags);
4259 }
4260 }
4261 /* When a reshape changes the number of devices,
4262 * ->degraded is measured against the larger of the
4263 * pre and post numbers.
4264 */
4265 spin_lock_irq(&conf->device_lock);
4266 mddev->degraded = calc_degraded(conf);
4267 spin_unlock_irq(&conf->device_lock);
4268 mddev->raid_disks = conf->geo.raid_disks;
4269 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08004270 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004271
4272 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4273 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004274 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004275 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4276 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4277
4278 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4279 "reshape");
4280 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004281 ret = -EAGAIN;
4282 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004283 }
4284 conf->reshape_checkpoint = jiffies;
4285 md_wakeup_thread(mddev->sync_thread);
4286 md_new_event(mddev);
4287 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004288
4289abort:
4290 mddev->recovery = 0;
4291 spin_lock_irq(&conf->device_lock);
4292 conf->geo = conf->prev;
4293 mddev->raid_disks = conf->geo.raid_disks;
4294 rdev_for_each(rdev, mddev)
4295 rdev->new_data_offset = rdev->data_offset;
4296 smp_wmb();
4297 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004298 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004299 mddev->reshape_position = MaxSector;
4300 spin_unlock_irq(&conf->device_lock);
4301 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004302}
4303
4304/* Calculate the last device-address that could contain
4305 * any block from the chunk that includes the array-address 's'
4306 * and report the next address.
4307 * i.e. the address returned will be chunk-aligned and after
4308 * any data that is in the chunk containing 's'.
4309 */
4310static sector_t last_dev_address(sector_t s, struct geom *geo)
4311{
4312 s = (s | geo->chunk_mask) + 1;
4313 s >>= geo->chunk_shift;
4314 s *= geo->near_copies;
4315 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4316 s *= geo->far_copies;
4317 s <<= geo->chunk_shift;
4318 return s;
4319}
4320
4321/* Calculate the first device-address that could contain
4322 * any block from the chunk that includes the array-address 's'.
4323 * This too will be the start of a chunk
4324 */
4325static sector_t first_dev_address(sector_t s, struct geom *geo)
4326{
4327 s >>= geo->chunk_shift;
4328 s *= geo->near_copies;
4329 sector_div(s, geo->raid_disks);
4330 s *= geo->far_copies;
4331 s <<= geo->chunk_shift;
4332 return s;
4333}
4334
4335static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4336 int *skipped)
4337{
4338 /* We simply copy at most one chunk (smallest of old and new)
4339 * at a time, possibly less if that exceeds RESYNC_PAGES,
4340 * or we hit a bad block or something.
4341 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004342 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004343 * can record any location.
4344 *
4345 * If we will want to write to a location that isn't
4346 * yet recorded as 'safe' (i.e. in metadata on disk) then
4347 * we need to flush all reshape requests and update the metadata.
4348 *
4349 * When reshaping forwards (e.g. to more devices), we interpret
4350 * 'safe' as the earliest block which might not have been copied
4351 * down yet. We divide this by previous stripe size and multiply
4352 * by previous stripe length to get lowest device offset that we
4353 * cannot write to yet.
4354 * We interpret 'sector_nr' as an address that we want to write to.
4355 * From this we use last_device_address() to find where we might
4356 * write to, and first_device_address on the 'safe' position.
4357 * If this 'next' write position is after the 'safe' position,
4358 * we must update the metadata to increase the 'safe' position.
4359 *
4360 * When reshaping backwards, we round in the opposite direction
4361 * and perform the reverse test: next write position must not be
4362 * less than current safe position.
4363 *
4364 * In all this the minimum difference in data offsets
4365 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004366 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004367 *
4368 * We need to prepare all the bios here before we start any IO
4369 * to ensure the size we choose is acceptable to all devices.
4370 * The means one for each copy for write-out and an extra one for
4371 * read-in.
4372 * We store the read-in bio in ->master_bio and the others in
4373 * ->devs[x].bio and ->devs[x].repl_bio.
4374 */
4375 struct r10conf *conf = mddev->private;
4376 struct r10bio *r10_bio;
4377 sector_t next, safe, last;
4378 int max_sectors;
4379 int nr_sectors;
4380 int s;
4381 struct md_rdev *rdev;
4382 int need_flush = 0;
4383 struct bio *blist;
4384 struct bio *bio, *read_bio;
4385 int sectors_done = 0;
4386
4387 if (sector_nr == 0) {
4388 /* If restarting in the middle, skip the initial sectors */
4389 if (mddev->reshape_backwards &&
4390 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4391 sector_nr = (raid10_size(mddev, 0, 0)
4392 - conf->reshape_progress);
4393 } else if (!mddev->reshape_backwards &&
4394 conf->reshape_progress > 0)
4395 sector_nr = conf->reshape_progress;
4396 if (sector_nr) {
4397 mddev->curr_resync_completed = sector_nr;
4398 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4399 *skipped = 1;
4400 return sector_nr;
4401 }
4402 }
4403
4404 /* We don't use sector_nr to track where we are up to
4405 * as that doesn't work well for ->reshape_backwards.
4406 * So just use ->reshape_progress.
4407 */
4408 if (mddev->reshape_backwards) {
4409 /* 'next' is the earliest device address that we might
4410 * write to for this chunk in the new layout
4411 */
4412 next = first_dev_address(conf->reshape_progress - 1,
4413 &conf->geo);
4414
4415 /* 'safe' is the last device address that we might read from
4416 * in the old layout after a restart
4417 */
4418 safe = last_dev_address(conf->reshape_safe - 1,
4419 &conf->prev);
4420
4421 if (next + conf->offset_diff < safe)
4422 need_flush = 1;
4423
4424 last = conf->reshape_progress - 1;
4425 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4426 & conf->prev.chunk_mask);
4427 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4428 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4429 } else {
4430 /* 'next' is after the last device address that we
4431 * might write to for this chunk in the new layout
4432 */
4433 next = last_dev_address(conf->reshape_progress, &conf->geo);
4434
4435 /* 'safe' is the earliest device address that we might
4436 * read from in the old layout after a restart
4437 */
4438 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4439
4440 /* Need to update metadata if 'next' might be beyond 'safe'
4441 * as that would possibly corrupt data
4442 */
4443 if (next > safe + conf->offset_diff)
4444 need_flush = 1;
4445
4446 sector_nr = conf->reshape_progress;
4447 last = sector_nr | (conf->geo.chunk_mask
4448 & conf->prev.chunk_mask);
4449
4450 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4451 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4452 }
4453
4454 if (need_flush ||
4455 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4456 /* Need to update reshape_position in metadata */
4457 wait_barrier(conf);
4458 mddev->reshape_position = conf->reshape_progress;
4459 if (mddev->reshape_backwards)
4460 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4461 - conf->reshape_progress;
4462 else
4463 mddev->curr_resync_completed = conf->reshape_progress;
4464 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08004465 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004466 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08004467 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004468 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4469 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4470 allow_barrier(conf);
4471 return sectors_done;
4472 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004473 conf->reshape_safe = mddev->reshape_position;
4474 allow_barrier(conf);
4475 }
4476
4477read_more:
4478 /* Now schedule reads for blocks from sector_nr to last */
4479 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004480 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004481 raise_barrier(conf, sectors_done != 0);
4482 atomic_set(&r10_bio->remaining, 0);
4483 r10_bio->mddev = mddev;
4484 r10_bio->sector = sector_nr;
4485 set_bit(R10BIO_IsReshape, &r10_bio->state);
4486 r10_bio->sectors = last - sector_nr + 1;
4487 rdev = read_balance(conf, r10_bio, &max_sectors);
4488 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4489
4490 if (!rdev) {
4491 /* Cannot read from here, so need to record bad blocks
4492 * on all the target devices.
4493 */
4494 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004495 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004496 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4497 return sectors_done;
4498 }
4499
4500 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4501
4502 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004503 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004504 + rdev->data_offset);
4505 read_bio->bi_private = r10_bio;
4506 read_bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004507 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004508 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004509 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004510 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004511 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004512 r10_bio->master_bio = read_bio;
4513 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4514
4515 /* Now find the locations in the new layout */
4516 __raid10_find_phys(&conf->geo, r10_bio);
4517
4518 blist = read_bio;
4519 read_bio->bi_next = NULL;
4520
NeilBrownd094d682016-06-02 16:19:52 +10004521 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004522 for (s = 0; s < conf->copies*2; s++) {
4523 struct bio *b;
4524 int d = r10_bio->devs[s/2].devnum;
4525 struct md_rdev *rdev2;
4526 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004527 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004528 b = r10_bio->devs[s/2].repl_bio;
4529 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004530 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004531 b = r10_bio->devs[s/2].bio;
4532 }
4533 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4534 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004535
4536 bio_reset(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004537 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004538 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4539 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004540 b->bi_private = r10_bio;
4541 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004542 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004543 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004544 blist = b;
4545 }
4546
4547 /* Now add as many pages as possible to all of these bios. */
4548
4549 nr_sectors = 0;
4550 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4551 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4552 int len = (max_sectors - s) << 9;
4553 if (len > PAGE_SIZE)
4554 len = PAGE_SIZE;
4555 for (bio = blist; bio ; bio = bio->bi_next) {
4556 struct bio *bio2;
4557 if (bio_add_page(bio, page, len, 0))
4558 continue;
4559
4560 /* Didn't fit, must stop */
4561 for (bio2 = blist;
4562 bio2 && bio2 != bio;
4563 bio2 = bio2->bi_next) {
4564 /* Remove last page from this bio */
4565 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004566 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004567 bio_clear_flag(bio2, BIO_SEG_VALID);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004568 }
4569 goto bio_full;
4570 }
4571 sector_nr += len >> 9;
4572 nr_sectors += len >> 9;
4573 }
4574bio_full:
NeilBrownd094d682016-06-02 16:19:52 +10004575 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004576 r10_bio->sectors = nr_sectors;
4577
4578 /* Now submit the read */
4579 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4580 atomic_inc(&r10_bio->remaining);
4581 read_bio->bi_next = NULL;
4582 generic_make_request(read_bio);
4583 sector_nr += nr_sectors;
4584 sectors_done += nr_sectors;
4585 if (sector_nr <= last)
4586 goto read_more;
4587
4588 /* Now that we have done the whole section we can
4589 * update reshape_progress
4590 */
4591 if (mddev->reshape_backwards)
4592 conf->reshape_progress -= sectors_done;
4593 else
4594 conf->reshape_progress += sectors_done;
4595
4596 return sectors_done;
4597}
4598
4599static void end_reshape_request(struct r10bio *r10_bio);
4600static int handle_reshape_read_error(struct mddev *mddev,
4601 struct r10bio *r10_bio);
4602static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4603{
4604 /* Reshape read completed. Hopefully we have a block
4605 * to write out.
4606 * If we got a read error then we do sync 1-page reads from
4607 * elsewhere until we find the data - or give up.
4608 */
4609 struct r10conf *conf = mddev->private;
4610 int s;
4611
4612 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4613 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4614 /* Reshape has been aborted */
4615 md_done_sync(mddev, r10_bio->sectors, 0);
4616 return;
4617 }
4618
4619 /* We definitely have the data in the pages, schedule the
4620 * writes.
4621 */
4622 atomic_set(&r10_bio->remaining, 1);
4623 for (s = 0; s < conf->copies*2; s++) {
4624 struct bio *b;
4625 int d = r10_bio->devs[s/2].devnum;
4626 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004627 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004628 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004629 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004630 b = r10_bio->devs[s/2].repl_bio;
4631 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004632 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004633 b = r10_bio->devs[s/2].bio;
4634 }
NeilBrownd094d682016-06-02 16:19:52 +10004635 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4636 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004637 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004638 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004639 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004640 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004641 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4642 atomic_inc(&r10_bio->remaining);
4643 b->bi_next = NULL;
4644 generic_make_request(b);
4645 }
4646 end_reshape_request(r10_bio);
4647}
4648
4649static void end_reshape(struct r10conf *conf)
4650{
4651 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4652 return;
4653
4654 spin_lock_irq(&conf->device_lock);
4655 conf->prev = conf->geo;
4656 md_finish_reshape(conf->mddev);
4657 smp_wmb();
4658 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004659 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004660 spin_unlock_irq(&conf->device_lock);
4661
4662 /* read-ahead size must cover two whole stripes, which is
4663 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4664 */
4665 if (conf->mddev->queue) {
4666 int stripe = conf->geo.raid_disks *
4667 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4668 stripe /= conf->geo.near_copies;
Jan Karadc3b17c2017-02-02 15:56:50 +01004669 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4670 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004671 }
4672 conf->fullsync = 0;
4673}
4674
NeilBrown3ea7daa2012-05-22 13:53:47 +10004675static int handle_reshape_read_error(struct mddev *mddev,
4676 struct r10bio *r10_bio)
4677{
4678 /* Use sync reads to get the blocks from somewhere else */
4679 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004680 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004681 struct {
4682 struct r10bio r10_bio;
4683 struct r10dev devs[conf->copies];
4684 } on_stack;
4685 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004686 int slot = 0;
4687 int idx = 0;
4688 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4689
NeilBrowne0ee7782012-08-18 09:51:42 +10004690 r10b->sector = r10_bio->sector;
4691 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004692
4693 while (sectors) {
4694 int s = sectors;
4695 int success = 0;
4696 int first_slot = slot;
4697
4698 if (s > (PAGE_SIZE >> 9))
4699 s = PAGE_SIZE >> 9;
4700
NeilBrownd094d682016-06-02 16:19:52 +10004701 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004702 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004703 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004704 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004705 sector_t addr;
4706 if (rdev == NULL ||
4707 test_bit(Faulty, &rdev->flags) ||
4708 !test_bit(In_sync, &rdev->flags))
4709 goto failed;
4710
NeilBrowne0ee7782012-08-18 09:51:42 +10004711 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004712 atomic_inc(&rdev->nr_pending);
4713 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004714 success = sync_page_io(rdev,
4715 addr,
4716 s << 9,
4717 bvec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05004718 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004719 rdev_dec_pending(rdev, mddev);
4720 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004721 if (success)
4722 break;
4723 failed:
4724 slot++;
4725 if (slot >= conf->copies)
4726 slot = 0;
4727 if (slot == first_slot)
4728 break;
4729 }
NeilBrownd094d682016-06-02 16:19:52 +10004730 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004731 if (!success) {
4732 /* couldn't read this block, must give up */
4733 set_bit(MD_RECOVERY_INTR,
4734 &mddev->recovery);
4735 return -EIO;
4736 }
4737 sectors -= s;
4738 idx++;
4739 }
4740 return 0;
4741}
4742
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004743static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004744{
NeilBrown3ea7daa2012-05-22 13:53:47 +10004745 struct r10bio *r10_bio = bio->bi_private;
4746 struct mddev *mddev = r10_bio->mddev;
4747 struct r10conf *conf = mddev->private;
4748 int d;
4749 int slot;
4750 int repl;
4751 struct md_rdev *rdev = NULL;
4752
4753 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4754 if (repl)
4755 rdev = conf->mirrors[d].replacement;
4756 if (!rdev) {
4757 smp_mb();
4758 rdev = conf->mirrors[d].rdev;
4759 }
4760
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004761 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004762 /* FIXME should record badblock */
4763 md_error(mddev, rdev);
4764 }
4765
4766 rdev_dec_pending(rdev, mddev);
4767 end_reshape_request(r10_bio);
4768}
4769
4770static void end_reshape_request(struct r10bio *r10_bio)
4771{
4772 if (!atomic_dec_and_test(&r10_bio->remaining))
4773 return;
4774 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4775 bio_put(r10_bio->master_bio);
4776 put_buf(r10_bio);
4777}
4778
4779static void raid10_finish_reshape(struct mddev *mddev)
4780{
4781 struct r10conf *conf = mddev->private;
4782
4783 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4784 return;
4785
4786 if (mddev->delta_disks > 0) {
4787 sector_t size = raid10_size(mddev, 0, 0);
4788 md_set_array_sectors(mddev, size);
4789 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4790 mddev->recovery_cp = mddev->resync_max_sectors;
4791 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4792 }
4793 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004794 if (mddev->queue) {
4795 set_capacity(mddev->gendisk, mddev->array_sectors);
4796 revalidate_disk(mddev->gendisk);
4797 }
NeilBrown63aced62012-05-22 13:55:33 +10004798 } else {
4799 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004800 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004801 for (d = conf->geo.raid_disks ;
4802 d < conf->geo.raid_disks - mddev->delta_disks;
4803 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004804 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004805 if (rdev)
4806 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004807 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004808 if (rdev)
4809 clear_bit(In_sync, &rdev->flags);
4810 }
NeilBrownd094d682016-06-02 16:19:52 +10004811 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004812 }
4813 mddev->layout = mddev->new_layout;
4814 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4815 mddev->reshape_position = MaxSector;
4816 mddev->delta_disks = 0;
4817 mddev->reshape_backwards = 0;
4818}
4819
NeilBrown84fc4b52011-10-11 16:49:58 +11004820static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004823 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004825 .make_request = raid10_make_request,
4826 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004827 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004828 .status = raid10_status,
4829 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 .hot_add_disk = raid10_add_disk,
4831 .hot_remove_disk= raid10_remove_disk,
4832 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004833 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004834 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004835 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004836 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004837 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004838 .check_reshape = raid10_check_reshape,
4839 .start_reshape = raid10_start_reshape,
4840 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004841 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842};
4843
4844static int __init raid_init(void)
4845{
NeilBrown2604b702006-01-06 00:20:36 -08004846 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
4849static void raid_exit(void)
4850{
NeilBrown2604b702006-01-06 00:20:36 -08004851 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
4854module_init(raid_init);
4855module_exit(raid_exit);
4856MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004857MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004859MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004860MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004861
4862module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);