blob: 7cdc9bcd21b0f801f07eaa054c7e936074f500c8 [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);
NeilBrown3ea7daa2012-05-22 13:53:47 +1000103static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
104 int *skipped);
105static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200106static void end_reshape_write(struct bio *bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +1000107static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -0800108
NeilBrown578b54a2016-11-14 16:30:21 +1100109#define raid10_log(md, fmt, args...) \
110 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
111
Al Virodd0fc662005-10-07 07:46:04 +0100112static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
NeilBrowne879a872011-10-11 16:49:02 +1100114 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100115 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
NeilBrown69335ef2011-12-23 10:17:54 +1100117 /* allocate a r10bio with room for raid_disks entries in the
118 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100119 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static void r10bio_pool_free(void *r10_bio, void *data)
123{
124 kfree(r10_bio);
125}
126
NeilBrown0310fa22008-08-05 15:54:14 +1000127/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +1000130/* amount of memory to reserve for resync requests */
131#define RESYNC_WINDOW (1024*1024)
132/* maximum number of concurrent requests, memory permitting */
133#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*
136 * When performing a resync, we need to read and compare, so
137 * we need as many pages are there are copies.
138 * When performing a recovery, we need 2 bios, one for read,
139 * one for write (we recover only one drive per r10buf)
140 *
141 */
Al Virodd0fc662005-10-07 07:46:04 +0100142static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
NeilBrowne879a872011-10-11 16:49:02 +1100144 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100146 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct bio *bio;
148 int i, j;
149 int nalloc;
150
151 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100152 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrown3ea7daa2012-05-22 13:53:47 +1000155 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
156 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 nalloc = conf->copies; /* resync */
158 else
159 nalloc = 2; /* recovery */
160
161 /*
162 * Allocate bios.
163 */
164 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100165 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (!bio)
167 goto out_free_bio;
168 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100169 if (!conf->have_replacement)
170 continue;
171 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
172 if (!bio)
173 goto out_free_bio;
174 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 /*
177 * Allocate RESYNC_PAGES data pages and attach them
178 * where needed.
179 */
180 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100181 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 bio = r10_bio->devs[j].bio;
183 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown3ea7daa2012-05-22 13:53:47 +1000184 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
185 &conf->mddev->recovery)) {
186 /* we can share bv_page's during recovery
187 * and reshape */
Namhyung Kimc65060a2011-07-18 17:38:49 +1000188 struct bio *rbio = r10_bio->devs[0].bio;
189 page = rbio->bi_io_vec[i].bv_page;
190 get_page(page);
191 } else
192 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (unlikely(!page))
194 goto out_free_pages;
195
196 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100197 if (rbio)
198 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 }
201
202 return r10_bio;
203
204out_free_pages:
205 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800206 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 while (j--)
208 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800209 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
majianpeng5fdd2cf2012-05-22 13:55:03 +1000210 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000212 for ( ; j < nalloc; j++) {
213 if (r10_bio->devs[j].bio)
214 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100215 if (r10_bio->devs[j].repl_bio)
216 bio_put(r10_bio->devs[j].repl_bio);
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 r10bio_pool_free(r10_bio, conf);
219 return NULL;
220}
221
222static void r10buf_pool_free(void *__r10_bio, void *data)
223{
224 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100225 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100226 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 int j;
228
229 for (j=0; j < conf->copies; j++) {
230 struct bio *bio = r10bio->devs[j].bio;
231 if (bio) {
232 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800233 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 bio->bi_io_vec[i].bv_page = NULL;
235 }
236 bio_put(bio);
237 }
NeilBrown69335ef2011-12-23 10:17:54 +1100238 bio = r10bio->devs[j].repl_bio;
239 if (bio)
240 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 r10bio_pool_free(r10bio, conf);
243}
244
NeilBrowne879a872011-10-11 16:49:02 +1100245static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 int i;
248
249 for (i = 0; i < conf->copies; i++) {
250 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000251 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 bio_put(*bio);
253 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100254 bio = &r10_bio->devs[i].repl_bio;
255 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
256 bio_put(*bio);
257 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259}
260
NeilBrown9f2c9d12011-10-11 16:48:43 +1100261static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
NeilBrowne879a872011-10-11 16:49:02 +1100263 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 put_all_bios(conf, r10_bio);
266 mempool_free(r10_bio, conf->r10bio_pool);
267}
268
NeilBrown9f2c9d12011-10-11 16:48:43 +1100269static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
NeilBrowne879a872011-10-11 16:49:02 +1100271 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 mempool_free(r10_bio, conf->r10buf_pool);
274
NeilBrown0a27ec92006-01-06 00:20:13 -0800275 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
NeilBrown9f2c9d12011-10-11 16:48:43 +1100278static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100281 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100282 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 spin_lock_irqsave(&conf->device_lock, flags);
285 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800286 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_unlock_irqrestore(&conf->device_lock, flags);
288
Arthur Jones388667b2008-07-25 12:03:38 -0700289 /* wake up frozen array... */
290 wake_up(&conf->wait_barrier);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 md_wakeup_thread(mddev->thread);
293}
294
295/*
296 * raid_end_bio_io() is called when we have finished servicing a mirrored
297 * operation and are ready to return a success/failure code to the buffer
298 * cache layer.
299 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100300static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000303 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100304 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
NeilBrown856e08e2011-07-28 11:39:23 +1000306 if (bio->bi_phys_segments) {
307 unsigned long flags;
308 spin_lock_irqsave(&conf->device_lock, flags);
309 bio->bi_phys_segments--;
310 done = (bio->bi_phys_segments == 0);
311 spin_unlock_irqrestore(&conf->device_lock, flags);
312 } else
313 done = 1;
314 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200315 bio->bi_error = -EIO;
NeilBrown856e08e2011-07-28 11:39:23 +1000316 if (done) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200317 bio_endio(bio);
NeilBrown856e08e2011-07-28 11:39:23 +1000318 /*
319 * Wake up any possible resync thread that waits for the device
320 * to go idle.
321 */
322 allow_barrier(conf);
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 free_r10bio(r10_bio);
325}
326
327/*
328 * Update disk head position estimator based on IRQ completion info.
329 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100330static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
NeilBrowne879a872011-10-11 16:49:02 +1100332 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
335 r10_bio->devs[slot].addr + (r10_bio->sectors);
336}
337
Namhyung Kim778ca012011-07-18 17:38:47 +1000338/*
339 * Find the disk number which triggered given bio
340 */
NeilBrowne879a872011-10-11 16:49:02 +1100341static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100342 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000343{
344 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100345 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000346
NeilBrown69335ef2011-12-23 10:17:54 +1100347 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000348 if (r10_bio->devs[slot].bio == bio)
349 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100350 if (r10_bio->devs[slot].repl_bio == bio) {
351 repl = 1;
352 break;
353 }
354 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000355
356 BUG_ON(slot == conf->copies);
357 update_head_pos(slot, r10_bio);
358
NeilBrown749c55e2011-07-28 11:39:24 +1000359 if (slotp)
360 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100361 if (replp)
362 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000363 return r10_bio->devs[slot].devnum;
364}
365
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200366static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200368 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100369 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100371 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100372 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 slot = r10_bio->read_slot;
375 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100376 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * this branch is our 'one mirror IO has finished' event handler:
379 */
NeilBrown4443ae12006-01-06 00:20:28 -0800380 update_head_pos(slot, r10_bio);
381
382 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Set R10BIO_Uptodate in our master bio, so that
385 * we will return a good error code to the higher
386 * levels even if IO on some other mirrored buffer fails.
387 *
388 * The 'master' represents the composite IO operation to
389 * user-side. So if something waits for IO, then it will
390 * wait for the 'master' bio.
391 */
392 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc52012-02-14 11:10:10 +1100393 } else {
394 /* If all other devices that store this block have
395 * failed, we want to return the error upwards rather
396 * than fail the last device. Here we redefine
397 * "uptodate" to mean "Don't want to retry"
398 */
NeilBrown635f6412013-06-11 14:57:09 +1000399 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
400 rdev->raid_disk))
NeilBrownfae8cc52012-02-14 11:10:10 +1100401 uptodate = 1;
NeilBrownfae8cc52012-02-14 11:10:10 +1100402 }
403 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100405 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800406 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000408 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410 char b[BDEVNAME_SIZE];
NeilBrown08464e02016-11-02 14:16:50 +1100411 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +1000412 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100413 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000414 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000415 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 reschedule_retry(r10_bio);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
NeilBrown9f2c9d12011-10-11 16:48:43 +1100420static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000421{
422 /* clear the bitmap if all writes complete successfully */
423 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
424 r10_bio->sectors,
425 !test_bit(R10BIO_Degraded, &r10_bio->state),
426 0);
427 md_write_end(r10_bio->mddev);
428}
429
NeilBrown9f2c9d12011-10-11 16:48:43 +1100430static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000431{
432 if (atomic_dec_and_test(&r10_bio->remaining)) {
433 if (test_bit(R10BIO_WriteError, &r10_bio->state))
434 reschedule_retry(r10_bio);
435 else {
436 close_write(r10_bio);
437 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
438 reschedule_retry(r10_bio);
439 else
440 raid_end_bio_io(r10_bio);
441 }
442 }
443}
444
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200445static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100447 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000448 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000449 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100450 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100451 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100452 struct md_rdev *rdev = NULL;
Shaohua Li579ed342016-10-06 14:13:52 -0700453 bool discard_error;
454
455 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
NeilBrown475b0322011-12-23 10:17:55 +1100457 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
NeilBrown475b0322011-12-23 10:17:55 +1100459 if (repl)
460 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100461 if (!rdev) {
462 smp_rmb();
463 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100464 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * this branch is our 'one mirror IO has finished' event handler:
468 */
Shaohua Li579ed342016-10-06 14:13:52 -0700469 if (bio->bi_error && !discard_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100470 if (repl)
471 /* Never record new bad blocks to replacement,
472 * just fail it.
473 */
474 md_error(rdev->mddev, rdev);
475 else {
476 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100477 if (!test_and_set_bit(WantReplacement, &rdev->flags))
478 set_bit(MD_RECOVERY_NEEDED,
479 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100480 set_bit(R10BIO_WriteError, &r10_bio->state);
481 dec_rdev = 0;
482 }
NeilBrown749c55e2011-07-28 11:39:24 +1000483 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Set R10BIO_Uptodate in our master bio, so that
486 * we will return a good error code for to the higher
487 * levels even if IO on some other mirrored buffer fails.
488 *
489 * The 'master' represents the composite IO operation to
490 * user-side. So if something waits for IO, then it will
491 * wait for the 'master' bio.
492 */
NeilBrown749c55e2011-07-28 11:39:24 +1000493 sector_t first_bad;
494 int bad_sectors;
495
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300496 /*
497 * Do not set R10BIO_Uptodate if the current device is
498 * rebuilding or Faulty. This is because we cannot use
499 * such device for properly reading the data back (we could
500 * potentially use it, if the current write would have felt
501 * before rdev->recovery_offset, but for simplicity we don't
502 * check this here.
503 */
504 if (test_bit(In_sync, &rdev->flags) &&
505 !test_bit(Faulty, &rdev->flags))
506 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
NeilBrown749c55e2011-07-28 11:39:24 +1000508 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100509 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000510 r10_bio->devs[slot].addr,
511 r10_bio->sectors,
Shaohua Li579ed342016-10-06 14:13:52 -0700512 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown749c55e2011-07-28 11:39:24 +1000513 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100514 if (repl)
515 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
516 else
517 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000518 dec_rdev = 0;
519 set_bit(R10BIO_MadeGood, &r10_bio->state);
520 }
521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /*
524 *
525 * Let's see if all mirrored write operations have finished
526 * already.
527 */
NeilBrown19d5f832011-09-10 17:21:17 +1000528 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000529 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100530 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
534 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300535 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * parameters: near_copies and far_copies.
537 * near_copies * far_copies must be <= raid_disks.
538 * Normally one of these will be 1.
539 * If both are 1, we get raid0.
540 * If near_copies == raid_disks, we get raid1.
541 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300542 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * first chunk, followed by near_copies copies of the next chunk and
544 * so on.
545 * If far_copies > 1, then after 1/far_copies of the array has been assigned
546 * as described above, we start again with a device offset of near_copies.
547 * So we effectively have another copy of the whole array further down all
548 * the drives, but with blocks on different drives.
549 * With this layout, and block is never stored twice on the one device.
550 *
551 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700552 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
554 * raid10_find_virt does the reverse mapping, from a device and a
555 * sector offset to a virtual address
556 */
557
NeilBrownf8c9e742012-05-21 09:28:33 +1000558static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int n,f;
561 sector_t sector;
562 sector_t chunk;
563 sector_t stripe;
564 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100566 int last_far_set_start, last_far_set_size;
567
568 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
569 last_far_set_start *= geo->far_set_size;
570
571 last_far_set_size = geo->far_set_size;
572 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000575 chunk = r10bio->sector >> geo->chunk_shift;
576 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
NeilBrown5cf00fc2012-05-21 09:28:20 +1000578 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000580 dev = sector_div(stripe, geo->raid_disks);
581 if (geo->far_offset)
582 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
NeilBrown5cf00fc2012-05-21 09:28:20 +1000584 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000587 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100589 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100592 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 slot++;
594
NeilBrown5cf00fc2012-05-21 09:28:20 +1000595 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100596 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000597 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100598
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100599 if ((geo->raid_disks % geo->far_set_size) &&
600 (d > last_far_set_start)) {
601 d -= last_far_set_start;
602 d %= last_far_set_size;
603 d += last_far_set_start;
604 } else {
605 d %= geo->far_set_size;
606 d += geo->far_set_size * set;
607 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000608 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 r10bio->devs[slot].devnum = d;
610 r10bio->devs[slot].addr = s;
611 slot++;
612 }
613 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000614 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000616 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000619}
620
621static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
622{
623 struct geom *geo = &conf->geo;
624
625 if (conf->reshape_progress != MaxSector &&
626 ((r10bio->sector >= conf->reshape_progress) !=
627 conf->mddev->reshape_backwards)) {
628 set_bit(R10BIO_Previous, &r10bio->state);
629 geo = &conf->prev;
630 } else
631 clear_bit(R10BIO_Previous, &r10bio->state);
632
633 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
NeilBrowne879a872011-10-11 16:49:02 +1100636static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000639 /* Never use conf->prev as this is only called during resync
640 * or recovery, so reshape isn't happening
641 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000642 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100643 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
644 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100645 int last_far_set_start;
646
647 if (geo->raid_disks % geo->far_set_size) {
648 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
649 last_far_set_start *= geo->far_set_size;
650
651 if (dev >= last_far_set_start) {
652 far_set_size = geo->far_set_size;
653 far_set_size += (geo->raid_disks % geo->far_set_size);
654 far_set_start = last_far_set_start;
655 }
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
NeilBrown5cf00fc2012-05-21 09:28:20 +1000658 offset = sector & geo->chunk_mask;
659 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700660 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000661 chunk = sector >> geo->chunk_shift;
662 fc = sector_div(chunk, geo->far_copies);
663 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100664 if (dev < far_set_start)
665 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700666 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000667 while (sector >= geo->stride) {
668 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100669 if (dev < (geo->near_copies + far_set_start))
670 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700671 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000672 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700673 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000674 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700675 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000676 vchunk = chunk * geo->raid_disks + dev;
677 sector_div(vchunk, geo->near_copies);
678 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/*
682 * This routine returns the disk from which the requested read should
683 * be done. There is a per-array 'next expected sequential IO' sector
684 * number - if this matches on the next IO then we use the last disk.
685 * There is also a per-disk 'last know head position' sector that is
686 * maintained from IRQ contexts, both the normal and the resync IO
687 * completion handlers update this position correctly. If there is no
688 * perfect sequential match then we pick the disk whose head is closest.
689 *
690 * If there are 2 mirrors in the same 2 devices, performance degrades
691 * because position is mirror, not device based.
692 *
693 * The rdev for the device selected will have nr_pending incremented.
694 */
695
696/*
697 * FIXME: possibly should rethink readbalancing and do it differently
698 * depending on near_copies / far_copies geometry.
699 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100700static struct md_rdev *read_balance(struct r10conf *conf,
701 struct r10bio *r10_bio,
702 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000704 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000705 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000706 int sectors = r10_bio->sectors;
707 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000708 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000709 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000710 int do_balance;
711 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000712 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 raid10_find_phys(conf, r10_bio);
715 rcu_read_lock();
NeilBrown856e08e2011-07-28 11:39:23 +1000716 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000717 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100718 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000719 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000720 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000721 do_balance = 1;
NeilBrown8d3ca832016-11-18 16:16:12 +1100722 clear_bit(R10BIO_FailFast, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800725 * device if no resync is going on (recovery is ok), or below
726 * the resync window. We take the first readable disk when
727 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
729 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000730 && (this_sector + sectors >= conf->next_resync))
731 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
NeilBrown56d99122011-05-11 14:27:03 +1000733 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000734 sector_t first_bad;
735 int bad_sectors;
736 sector_t dev_sector;
737
NeilBrown56d99122011-05-11 14:27:03 +1000738 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000740 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100741 rdev = rcu_dereference(conf->mirrors[disk].replacement);
742 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
743 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
744 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100745 if (rdev == NULL ||
Kent Overstreet8ae12662015-04-27 23:48:34 -0700746 test_bit(Faulty, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100747 continue;
748 if (!test_bit(In_sync, &rdev->flags) &&
749 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000750 continue;
751
NeilBrown856e08e2011-07-28 11:39:23 +1000752 dev_sector = r10_bio->devs[slot].addr;
753 if (is_badblock(rdev, dev_sector, sectors,
754 &first_bad, &bad_sectors)) {
755 if (best_dist < MaxSector)
756 /* Already have a better slot */
757 continue;
758 if (first_bad <= dev_sector) {
759 /* Cannot read here. If this is the
760 * 'primary' device, then we must not read
761 * beyond 'bad_sectors' from another device.
762 */
763 bad_sectors -= (dev_sector - first_bad);
764 if (!do_balance && sectors > bad_sectors)
765 sectors = bad_sectors;
766 if (best_good_sectors > sectors)
767 best_good_sectors = sectors;
768 } else {
769 sector_t good_sectors =
770 first_bad - dev_sector;
771 if (good_sectors > best_good_sectors) {
772 best_good_sectors = good_sectors;
773 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100774 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000775 }
776 if (!do_balance)
777 /* Must read from here */
778 break;
779 }
780 continue;
781 } else
782 best_good_sectors = sectors;
783
NeilBrown56d99122011-05-11 14:27:03 +1000784 if (!do_balance)
785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
NeilBrown8d3ca832016-11-18 16:16:12 +1100787 if (best_slot >= 0)
788 /* At least 2 disks to choose from so failfast is OK */
789 set_bit(R10BIO_FailFast, &r10_bio->state);
NeilBrown22dfdf52005-11-28 13:44:09 -0800790 /* This optimisation is debatable, and completely destroys
791 * sequential read speed for 'far copies' arrays. So only
792 * keep it for 'near' arrays, and review those later.
793 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000794 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
NeilBrown8d3ca832016-11-18 16:16:12 +1100795 new_distance = 0;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800796
797 /* for far > 1 always use the lowest address */
NeilBrown8d3ca832016-11-18 16:16:12 +1100798 else if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000799 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800800 else
NeilBrown56d99122011-05-11 14:27:03 +1000801 new_distance = abs(r10_bio->devs[slot].addr -
802 conf->mirrors[disk].head_position);
803 if (new_distance < best_dist) {
804 best_dist = new_distance;
805 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100806 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 }
NeilBrownabbf0982011-12-23 10:17:54 +1100809 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000810 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100811 rdev = best_rdev;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
NeilBrown56d99122011-05-11 14:27:03 +1000814 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000815 atomic_inc(&rdev->nr_pending);
NeilBrown56d99122011-05-11 14:27:03 +1000816 r10_bio->read_slot = slot;
817 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100818 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000820 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
NeilBrown96c3fd12011-12-23 10:17:54 +1100822 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
NeilBrown5c675f82014-12-15 12:56:56 +1100825static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700826{
NeilBrowne879a872011-10-11 16:49:02 +1100827 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700828 int i, ret = 0;
829
Tejun Heo44522262015-05-22 17:13:26 -0400830 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100831 conf->pending_count >= max_queued_requests)
832 return 1;
833
NeilBrown0d129222006-10-03 01:15:54 -0700834 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000835 for (i = 0;
836 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
837 && ret == 0;
838 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100839 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700840 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200841 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700842
843 ret |= bdi_congested(&q->backing_dev_info, bits);
844 }
845 }
846 rcu_read_unlock();
847 return ret;
848}
849
NeilBrowne879a872011-10-11 16:49:02 +1100850static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800851{
852 /* Any writes that have been queued but are awaiting
853 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800854 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800855 spin_lock_irq(&conf->device_lock);
856
857 if (conf->pending_bio_list.head) {
858 struct bio *bio;
859 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100860 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800861 spin_unlock_irq(&conf->device_lock);
862 /* flush any pending bitmap writes to disk
863 * before proceeding w/ I/O */
864 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100865 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800866
867 while (bio) { /* submit pending writes */
868 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100869 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800870 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +1100871 bio->bi_bdev = rdev->bdev;
872 if (test_bit(Faulty, &rdev->flags)) {
873 bio->bi_error = -EIO;
874 bio_endio(bio);
875 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
876 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li532a2a32012-10-11 13:30:52 +1100877 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200878 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100879 else
880 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800881 bio = next;
882 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800883 } else
884 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800885}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100886
NeilBrown0a27ec92006-01-06 00:20:13 -0800887/* Barriers....
888 * Sometimes we need to suspend IO while we do something else,
889 * either some resync/recovery, or reconfigure the array.
890 * To do this we raise a 'barrier'.
891 * The 'barrier' is a counter that can be raised multiple times
892 * to count how many activities are happening which preclude
893 * normal IO.
894 * We can only raise the barrier if there is no pending IO.
895 * i.e. if nr_pending == 0.
896 * We choose only to raise the barrier if no-one is waiting for the
897 * barrier to go down. This means that as soon as an IO request
898 * is ready, no other operations which require a barrier will start
899 * until the IO request has had a chance.
900 *
901 * So: regular IO calls 'wait_barrier'. When that returns there
902 * is no backgroup IO happening, It must arrange to call
903 * allow_barrier when it has finished its IO.
904 * backgroup IO calls must call raise_barrier. Once that returns
905 * there is no normal IO happeing. It must arrange to call
906 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
NeilBrowne879a872011-10-11 16:49:02 +1100909static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
NeilBrown6cce3b22006-01-06 00:20:16 -0800911 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
NeilBrown6cce3b22006-01-06 00:20:16 -0800914 /* Wait until no block IO is waiting (unless 'force') */
915 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100916 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800917
918 /* block any new IO from starting */
919 conf->barrier++;
920
NeilBrownc3b328a2011-04-18 18:25:43 +1000921 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800922 wait_event_lock_irq(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200923 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100924 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 spin_unlock_irq(&conf->resync_lock);
927}
928
NeilBrowne879a872011-10-11 16:49:02 +1100929static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800930{
931 unsigned long flags;
932 spin_lock_irqsave(&conf->resync_lock, flags);
933 conf->barrier--;
934 spin_unlock_irqrestore(&conf->resync_lock, flags);
935 wake_up(&conf->wait_barrier);
936}
937
NeilBrowne879a872011-10-11 16:49:02 +1100938static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800939{
940 spin_lock_irq(&conf->resync_lock);
941 if (conf->barrier) {
942 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100943 /* Wait for the barrier to drop.
944 * However if there are already pending
945 * requests (preventing the barrier from
946 * rising completely), and the
947 * pre-process bio queue isn't empty,
948 * then don't wait, as we need to empty
949 * that queue to get the nr_pending
950 * count down.
951 */
NeilBrown578b54a2016-11-14 16:30:21 +1100952 raid10_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100953 wait_event_lock_irq(conf->wait_barrier,
954 !conf->barrier ||
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200955 (atomic_read(&conf->nr_pending) &&
NeilBrownd6b42dc2012-03-19 12:46:38 +1100956 current->bio_list &&
957 !bio_list_empty(current->bio_list)),
Lukas Czernereed8c022012-11-30 11:42:40 +0100958 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800959 conf->nr_waiting--;
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200960 if (!conf->nr_waiting)
961 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800962 }
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200963 atomic_inc(&conf->nr_pending);
NeilBrown0a27ec92006-01-06 00:20:13 -0800964 spin_unlock_irq(&conf->resync_lock);
965}
966
NeilBrowne879a872011-10-11 16:49:02 +1100967static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800968{
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200969 if ((atomic_dec_and_test(&conf->nr_pending)) ||
970 (conf->array_freeze_pending))
971 wake_up(&conf->wait_barrier);
NeilBrown0a27ec92006-01-06 00:20:13 -0800972}
973
NeilBrowne2d59922013-06-12 11:01:22 +1000974static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -0800975{
976 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800977 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800978 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +1000979 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -0800980 * This is called in the context of one normal IO request
981 * that has failed. Thus any sync request that might be pending
982 * will be blocked by nr_pending, and we need to wait for
983 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +1000984 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -0800985 * must match the number of pending IOs (nr_pending) before
986 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800987 */
988 spin_lock_irq(&conf->resync_lock);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200989 conf->array_freeze_pending++;
NeilBrown4443ae12006-01-06 00:20:28 -0800990 conf->barrier++;
991 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +0100992 wait_event_lock_irq_cmd(conf->wait_barrier,
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200993 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +0100994 conf->resync_lock,
995 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +1000996
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +0200997 conf->array_freeze_pending--;
NeilBrown4443ae12006-01-06 00:20:28 -0800998 spin_unlock_irq(&conf->resync_lock);
999}
1000
NeilBrowne879a872011-10-11 16:49:02 +11001001static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001002{
1003 /* reverse the effect of the freeze */
1004 spin_lock_irq(&conf->resync_lock);
1005 conf->barrier--;
1006 conf->nr_waiting--;
1007 wake_up(&conf->wait_barrier);
1008 spin_unlock_irq(&conf->resync_lock);
1009}
1010
NeilBrownf8c9e742012-05-21 09:28:33 +10001011static sector_t choose_data_offset(struct r10bio *r10_bio,
1012 struct md_rdev *rdev)
1013{
1014 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1015 test_bit(R10BIO_Previous, &r10_bio->state))
1016 return rdev->data_offset;
1017 else
1018 return rdev->new_data_offset;
1019}
1020
NeilBrown57c67df2012-10-11 13:32:13 +11001021struct raid10_plug_cb {
1022 struct blk_plug_cb cb;
1023 struct bio_list pending;
1024 int pending_cnt;
1025};
1026
1027static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1028{
1029 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1030 cb);
1031 struct mddev *mddev = plug->cb.data;
1032 struct r10conf *conf = mddev->private;
1033 struct bio *bio;
1034
NeilBrown874807a2012-11-27 12:14:40 +11001035 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001036 spin_lock_irq(&conf->device_lock);
1037 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1038 conf->pending_count += plug->pending_cnt;
1039 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001040 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001041 md_wakeup_thread(mddev->thread);
1042 kfree(plug);
1043 return;
1044 }
1045
1046 /* we aren't scheduling, so we can do the write-out directly. */
1047 bio = bio_list_get(&plug->pending);
1048 bitmap_unplug(mddev->bitmap);
1049 wake_up(&conf->wait_barrier);
1050
1051 while (bio) { /* submit pending writes */
1052 struct bio *next = bio->bi_next;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001053 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001054 bio->bi_next = NULL;
NeilBrowna9ae93c2016-11-04 16:46:03 +11001055 bio->bi_bdev = rdev->bdev;
1056 if (test_bit(Faulty, &rdev->flags)) {
1057 bio->bi_error = -EIO;
1058 bio_endio(bio);
1059 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1060 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001061 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001062 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001063 else
1064 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001065 bio = next;
1066 }
1067 kfree(plug);
1068}
1069
Kent Overstreet20d01892013-11-23 18:21:01 -08001070static void __make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
NeilBrowne879a872011-10-11 16:49:02 +11001072 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001073 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct bio *read_bio;
1075 int i;
Mike Christie796a5cf2016-06-05 14:32:07 -05001076 const int op = bio_op(bio);
Jens Axboea3623572005-11-01 09:26:16 +01001077 const int rw = bio_data_dir(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001078 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1079 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -08001080 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001081 struct md_rdev *blocked_rdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001082 struct blk_plug_cb *cb;
1083 struct raid10_plug_cb *plug = NULL;
NeilBrownd4432c22011-07-28 11:39:24 +10001084 int sectors_handled;
1085 int max_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001086 int sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Tomasz Majchrzak9b622e22016-07-28 10:28:25 +02001088 md_write_start(mddev, bio);
1089
NeilBrowncc13b1d2014-05-05 13:34:37 +10001090 /*
1091 * Register the new request and wait if the reconstruction
1092 * thread has put up a bar for new requests.
1093 * Continue immediately if no resync is active currently.
1094 */
1095 wait_barrier(conf);
1096
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001097 sectors = bio_sectors(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001098 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001099 bio->bi_iter.bi_sector < conf->reshape_progress &&
1100 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001101 /* IO spans the reshape position. Need to wait for
1102 * reshape to pass
1103 */
NeilBrown578b54a2016-11-14 16:30:21 +11001104 raid10_log(conf->mddev, "wait reshape");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001105 allow_barrier(conf);
1106 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001107 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1108 conf->reshape_progress >= bio->bi_iter.bi_sector +
1109 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001110 wait_barrier(conf);
1111 }
1112 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1113 bio_data_dir(bio) == WRITE &&
1114 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001115 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1116 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1117 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1118 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001119 /* Need to update reshape_position in metadata */
1120 mddev->reshape_position = conf->reshape_progress;
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04001121 set_mask_bits(&mddev->flags, 0,
1122 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
NeilBrown3ea7daa2012-05-22 13:53:47 +10001123 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001124 raid10_log(conf->mddev, "wait reshape metadata");
NeilBrown3ea7daa2012-05-22 13:53:47 +10001125 wait_event(mddev->sb_wait,
1126 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1127
1128 conf->reshape_safe = mddev->reshape_position;
1129 }
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1132
1133 r10_bio->master_bio = bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001134 r10_bio->sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001137 r10_bio->sector = bio->bi_iter.bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -08001138 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
NeilBrown856e08e2011-07-28 11:39:23 +10001140 /* We might need to issue multiple reads to different
1141 * devices if there are bad blocks around, so we keep
1142 * track of the number of reads in bio->bi_phys_segments.
1143 * If this is 0, there is only one r10_bio and no locking
1144 * will be needed when the request completes. If it is
1145 * non-zero, then it is the number of not-completed requests.
1146 */
1147 bio->bi_phys_segments = 0;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06001148 bio_clear_flag(bio, BIO_SEG_VALID);
NeilBrown856e08e2011-07-28 11:39:23 +10001149
Jens Axboea3623572005-11-01 09:26:16 +01001150 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
1152 * read balancing logic:
1153 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001154 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001155 int slot;
1156
1157read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001158 rdev = read_balance(conf, r10_bio, &max_sectors);
1159 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001163 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
NeilBrowna167f662010-10-26 18:31:13 +11001165 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001166 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001167 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001170 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Kent Overstreet4f024f32013-10-11 15:44:27 -07001172 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
NeilBrownf8c9e742012-05-21 09:28:33 +10001173 choose_data_offset(r10_bio, rdev);
NeilBrown96c3fd12011-12-23 10:17:54 +11001174 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 read_bio->bi_end_io = raid10_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05001176 bio_set_op_attrs(read_bio, op, do_sync);
NeilBrown8d3ca832016-11-18 16:16:12 +11001177 if (test_bit(FailFast, &rdev->flags) &&
1178 test_bit(R10BIO_FailFast, &r10_bio->state))
1179 read_bio->bi_opf |= MD_FAILFAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 read_bio->bi_private = r10_bio;
1181
NeilBrown109e3762016-11-18 13:22:04 +11001182 if (mddev->gendisk)
1183 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1184 read_bio, disk_devt(mddev->gendisk),
1185 r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +10001186 if (max_sectors < r10_bio->sectors) {
1187 /* Could not read all from this device, so we will
1188 * need another r10_bio.
1189 */
NeilBrownb50c2592014-01-14 10:38:09 +11001190 sectors_handled = (r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07001191 - bio->bi_iter.bi_sector);
NeilBrown856e08e2011-07-28 11:39:23 +10001192 r10_bio->sectors = max_sectors;
1193 spin_lock_irq(&conf->device_lock);
1194 if (bio->bi_phys_segments == 0)
1195 bio->bi_phys_segments = 2;
1196 else
1197 bio->bi_phys_segments++;
NeilBrownb50c2592014-01-14 10:38:09 +11001198 spin_unlock_irq(&conf->device_lock);
NeilBrown856e08e2011-07-28 11:39:23 +10001199 /* Cannot call generic_make_request directly
1200 * as that will be queued in __generic_make_request
1201 * and subsequent mempool_alloc might block
1202 * waiting for it. so hand bio over to raid10d.
1203 */
1204 reschedule_retry(r10_bio);
1205
1206 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1207
1208 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001209 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrown856e08e2011-07-28 11:39:23 +10001210 r10_bio->state = 0;
1211 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001212 r10_bio->sector = bio->bi_iter.bi_sector +
1213 sectors_handled;
NeilBrown856e08e2011-07-28 11:39:23 +10001214 goto read_again;
1215 } else
1216 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001217 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
1220 /*
1221 * WRITE:
1222 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001223 if (conf->pending_count >= max_queued_requests) {
1224 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001225 raid10_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001226 wait_event(conf->wait_barrier,
1227 conf->pending_count < max_queued_requests);
1228 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001229 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 * inc refcount on their rdev. Record them by setting
1231 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001232 * If there are known/acknowledged bad blocks on any device
1233 * on which we have seen a write error, we want to avoid
1234 * writing to those blocks. This potentially requires several
1235 * writes to write around the bad blocks. Each set of writes
1236 * gets its own r10_bio with a set of bios attached. The number
1237 * of r10_bios is recored in bio->bi_phys_segments just as with
1238 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001240
NeilBrown69335ef2011-12-23 10:17:54 +11001241 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001243retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001244 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001246 max_sectors = r10_bio->sectors;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 for (i = 0; i < conf->copies; i++) {
1249 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001250 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001251 struct md_rdev *rrdev = rcu_dereference(
1252 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001253 if (rdev == rrdev)
1254 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001255 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1256 atomic_inc(&rdev->nr_pending);
1257 blocked_rdev = rdev;
1258 break;
1259 }
NeilBrown475b0322011-12-23 10:17:55 +11001260 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1261 atomic_inc(&rrdev->nr_pending);
1262 blocked_rdev = rrdev;
1263 break;
1264 }
Kent Overstreet8ae12662015-04-27 23:48:34 -07001265 if (rdev && (test_bit(Faulty, &rdev->flags)))
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001266 rdev = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001267 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001268 rrdev = NULL;
1269
NeilBrownd4432c22011-07-28 11:39:24 +10001270 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001271 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001272
1273 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001274 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001275 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001276 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001277 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001278 sector_t first_bad;
1279 sector_t dev_sector = r10_bio->devs[i].addr;
1280 int bad_sectors;
1281 int is_bad;
1282
1283 is_bad = is_badblock(rdev, dev_sector,
1284 max_sectors,
1285 &first_bad, &bad_sectors);
1286 if (is_bad < 0) {
1287 /* Mustn't write here until the bad block
1288 * is acknowledged
1289 */
1290 atomic_inc(&rdev->nr_pending);
1291 set_bit(BlockedBadBlocks, &rdev->flags);
1292 blocked_rdev = rdev;
1293 break;
1294 }
1295 if (is_bad && first_bad <= dev_sector) {
1296 /* Cannot write here at all */
1297 bad_sectors -= (dev_sector - first_bad);
1298 if (bad_sectors < max_sectors)
1299 /* Mustn't write more than bad_sectors
1300 * to other devices yet
1301 */
1302 max_sectors = bad_sectors;
1303 /* We don't set R10BIO_Degraded as that
1304 * only applies if the disk is missing,
1305 * so it might be re-added, and we want to
1306 * know to recover this chunk.
1307 * In this case the device is here, and the
1308 * fact that this chunk is not in-sync is
1309 * recorded in the bad block log.
1310 */
1311 continue;
1312 }
1313 if (is_bad) {
1314 int good_sectors = first_bad - dev_sector;
1315 if (good_sectors < max_sectors)
1316 max_sectors = good_sectors;
1317 }
1318 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001319 if (rdev) {
1320 r10_bio->devs[i].bio = bio;
1321 atomic_inc(&rdev->nr_pending);
1322 }
NeilBrown475b0322011-12-23 10:17:55 +11001323 if (rrdev) {
1324 r10_bio->devs[i].repl_bio = bio;
1325 atomic_inc(&rrdev->nr_pending);
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 rcu_read_unlock();
1329
Dan Williams6bfe0b42008-04-30 00:52:32 -07001330 if (unlikely(blocked_rdev)) {
1331 /* Have to wait for this device to get unblocked, then retry */
1332 int j;
1333 int d;
1334
NeilBrown475b0322011-12-23 10:17:55 +11001335 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001336 if (r10_bio->devs[j].bio) {
1337 d = r10_bio->devs[j].devnum;
1338 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1339 }
NeilBrown475b0322011-12-23 10:17:55 +11001340 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001341 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001342 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001343 rdev = conf->mirrors[d].replacement;
1344 if (!rdev) {
1345 /* Race with remove_disk */
1346 smp_mb();
1347 rdev = conf->mirrors[d].rdev;
1348 }
1349 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001350 }
1351 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001352 allow_barrier(conf);
NeilBrown578b54a2016-11-14 16:30:21 +11001353 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001354 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1355 wait_barrier(conf);
1356 goto retry_write;
1357 }
1358
NeilBrownd4432c22011-07-28 11:39:24 +10001359 if (max_sectors < r10_bio->sectors) {
1360 /* We are splitting this into multiple parts, so
1361 * we need to prepare for allocating another r10_bio.
1362 */
1363 r10_bio->sectors = max_sectors;
1364 spin_lock_irq(&conf->device_lock);
1365 if (bio->bi_phys_segments == 0)
1366 bio->bi_phys_segments = 2;
1367 else
1368 bio->bi_phys_segments++;
1369 spin_unlock_irq(&conf->device_lock);
1370 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001371 sectors_handled = r10_bio->sector + max_sectors -
1372 bio->bi_iter.bi_sector;
NeilBrownd4432c22011-07-28 11:39:24 +10001373
NeilBrown4e780642010-10-19 12:54:01 +11001374 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001375 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 for (i = 0; i < conf->copies; i++) {
1378 struct bio *mbio;
1379 int d = r10_bio->devs[i].devnum;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001380 if (r10_bio->devs[i].bio) {
1381 struct md_rdev *rdev = conf->mirrors[d].rdev;
1382 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001383 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001384 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001385 r10_bio->devs[i].bio = mbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Kent Overstreet4f024f32013-10-11 15:44:27 -07001387 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001388 choose_data_offset(r10_bio,
1389 rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001390 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001391 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001392 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001393 mbio->bi_private = r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
NeilBrown109e3762016-11-18 13:22:04 +11001395 if (conf->mddev->gendisk)
1396 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1397 mbio, disk_devt(conf->mddev->gendisk),
1398 r10_bio->sector);
1399 /* flush_pending_writes() needs access to the rdev so...*/
1400 mbio->bi_bdev = (void*)rdev;
1401
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001402 atomic_inc(&r10_bio->remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001404 cb = blk_check_plugged(raid10_unplug, mddev,
1405 sizeof(*plug));
1406 if (cb)
1407 plug = container_of(cb, struct raid10_plug_cb,
1408 cb);
1409 else
1410 plug = NULL;
1411 spin_lock_irqsave(&conf->device_lock, flags);
1412 if (plug) {
1413 bio_list_add(&plug->pending, mbio);
1414 plug->pending_cnt++;
1415 } else {
1416 bio_list_add(&conf->pending_bio_list, mbio);
1417 conf->pending_count++;
1418 }
1419 spin_unlock_irqrestore(&conf->device_lock, flags);
1420 if (!plug)
1421 md_wakeup_thread(mddev->thread);
1422 }
NeilBrown57c67df2012-10-11 13:32:13 +11001423
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001424 if (r10_bio->devs[i].repl_bio) {
1425 struct md_rdev *rdev = conf->mirrors[d].replacement;
1426 if (rdev == NULL) {
1427 /* Replacement just got moved to main 'rdev' */
1428 smp_mb();
1429 rdev = conf->mirrors[d].rdev;
1430 }
1431 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001432 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001433 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001434 r10_bio->devs[i].repl_bio = mbio;
1435
Kent Overstreet4f024f32013-10-11 15:44:27 -07001436 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001437 choose_data_offset(
1438 r10_bio, rdev));
NeilBrown109e3762016-11-18 13:22:04 +11001439 mbio->bi_bdev = rdev->bdev;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001440 mbio->bi_end_io = raid10_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001441 bio_set_op_attrs(mbio, op, do_sync | do_fua);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001442 mbio->bi_private = r10_bio;
1443
NeilBrown109e3762016-11-18 13:22:04 +11001444 if (conf->mddev->gendisk)
1445 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1446 mbio, disk_devt(conf->mddev->gendisk),
1447 r10_bio->sector);
1448 /* flush_pending_writes() needs access to the rdev so...*/
1449 mbio->bi_bdev = (void*)rdev;
1450
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001451 atomic_inc(&r10_bio->remaining);
1452 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown57c67df2012-10-11 13:32:13 +11001453 bio_list_add(&conf->pending_bio_list, mbio);
1454 conf->pending_count++;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001455 spin_unlock_irqrestore(&conf->device_lock, flags);
1456 if (!mddev_check_plugged(mddev))
1457 md_wakeup_thread(mddev->thread);
NeilBrown57c67df2012-10-11 13:32:13 +11001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
NeilBrown079fa162011-09-10 17:21:23 +10001461 /* Don't remove the bias on 'remaining' (one_write_done) until
1462 * after checking if we need to go around again.
1463 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001464
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001465 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001466 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001467 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001468 * in bio->bi_phys_segments.
1469 */
1470 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1471
1472 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001473 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001474
1475 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001476 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001477 r10_bio->state = 0;
1478 goto retry_write;
1479 }
NeilBrown079fa162011-09-10 17:21:23 +10001480 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001481}
1482
Shaohua Li849674e2016-01-20 13:52:20 -08001483static void raid10_make_request(struct mddev *mddev, struct bio *bio)
Kent Overstreet20d01892013-11-23 18:21:01 -08001484{
1485 struct r10conf *conf = mddev->private;
1486 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1487 int chunk_sects = chunk_mask + 1;
1488
1489 struct bio *split;
1490
Jens Axboe1eff9d32016-08-05 15:35:16 -06001491 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
Kent Overstreet20d01892013-11-23 18:21:01 -08001492 md_flush_request(mddev, bio);
1493 return;
1494 }
1495
Kent Overstreet20d01892013-11-23 18:21:01 -08001496 do {
1497
1498 /*
1499 * If this request crosses a chunk boundary, we need to split
1500 * it.
1501 */
1502 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1503 bio_sectors(bio) > chunk_sects
1504 && (conf->geo.near_copies < conf->geo.raid_disks
1505 || conf->prev.near_copies <
1506 conf->prev.raid_disks))) {
1507 split = bio_split(bio, chunk_sects -
1508 (bio->bi_iter.bi_sector &
1509 (chunk_sects - 1)),
1510 GFP_NOIO, fs_bio_set);
1511 bio_chain(split, bio);
1512 } else {
1513 split = bio;
1514 }
1515
1516 __make_request(mddev, split);
1517 } while (split != bio);
NeilBrown079fa162011-09-10 17:21:23 +10001518
1519 /* In case raid10d snuck in to freeze_array */
1520 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Shaohua Li849674e2016-01-20 13:52:20 -08001523static void raid10_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
NeilBrowne879a872011-10-11 16:49:02 +11001525 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 int i;
1527
NeilBrown5cf00fc2012-05-21 09:28:20 +10001528 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001529 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001530 if (conf->geo.near_copies > 1)
1531 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1532 if (conf->geo.far_copies > 1) {
1533 if (conf->geo.far_offset)
1534 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001535 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001536 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrown8bce6d32015-10-22 13:20:15 +11001537 if (conf->geo.far_set_size != conf->geo.raid_disks)
1538 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
NeilBrownc93983b2006-06-26 00:27:41 -07001539 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001540 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1541 conf->geo.raid_disks - mddev->degraded);
NeilBrownd44b0a92016-06-02 16:19:52 +10001542 rcu_read_lock();
1543 for (i = 0; i < conf->geo.raid_disks; i++) {
1544 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1545 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1546 }
1547 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 seq_printf(seq, "]");
1549}
1550
NeilBrown700c7212011-07-27 11:00:36 +10001551/* check if there are enough drives for
1552 * every block to appear on atleast one.
1553 * Don't consider the device numbered 'ignore'
1554 * as we might be about to remove it.
1555 */
NeilBrown635f6412013-06-11 14:57:09 +10001556static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001557{
1558 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001559 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001560 int disks, ncopies;
1561 if (previous) {
1562 disks = conf->prev.raid_disks;
1563 ncopies = conf->prev.near_copies;
1564 } else {
1565 disks = conf->geo.raid_disks;
1566 ncopies = conf->geo.near_copies;
1567 }
NeilBrown700c7212011-07-27 11:00:36 +10001568
NeilBrown725d6e52013-06-11 15:08:03 +10001569 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001570 do {
1571 int n = conf->copies;
1572 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001573 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001574 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001575 struct md_rdev *rdev;
1576 if (this != ignore &&
1577 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1578 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001579 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001580 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001581 }
1582 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001583 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001584 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001585 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001586 has_enough = 1;
1587out:
1588 rcu_read_unlock();
1589 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001590}
1591
NeilBrownf8c9e742012-05-21 09:28:33 +10001592static int enough(struct r10conf *conf, int ignore)
1593{
NeilBrown635f6412013-06-11 14:57:09 +10001594 /* when calling 'enough', both 'prev' and 'geo' must
1595 * be stable.
1596 * This is ensured if ->reconfig_mutex or ->device_lock
1597 * is held.
1598 */
1599 return _enough(conf, 0, ignore) &&
1600 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001601}
1602
Shaohua Li849674e2016-01-20 13:52:20 -08001603static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001606 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001607 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /*
1610 * If it is not operational, then we have already marked it as dead
1611 * else if it is the last working disks, ignore the error, let the
1612 * next level up know.
1613 * else mark the drive as failed
1614 */
NeilBrown635f6412013-06-11 14:57:09 +10001615 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001616 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001617 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001621 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001622 return;
1623 }
NeilBrown2446dba2014-07-31 10:16:29 +10001624 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001625 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001626 /*
1627 * If recovery is running, make sure it aborts.
1628 */
1629 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001630 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001631 set_bit(Faulty, &rdev->flags);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04001632 set_mask_bits(&mddev->flags, 0,
1633 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
NeilBrown635f6412013-06-11 14:57:09 +10001634 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown08464e02016-11-02 14:16:50 +11001635 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1636 "md/raid10:%s: Operation continuing on %d devices.\n",
1637 mdname(mddev), bdevname(rdev->bdev, b),
1638 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
NeilBrowne879a872011-10-11 16:49:02 +11001641static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int i;
NeilBrown4056ca52016-06-02 16:19:52 +10001644 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
NeilBrown08464e02016-11-02 14:16:50 +11001646 pr_debug("RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!conf) {
NeilBrown08464e02016-11-02 14:16:50 +11001648 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return;
1650 }
NeilBrown08464e02016-11-02 14:16:50 +11001651 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1652 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
NeilBrown4056ca52016-06-02 16:19:52 +10001654 /* This is only called with ->reconfix_mutex held, so
1655 * rcu protection of rdev is not needed */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001656 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 char b[BDEVNAME_SIZE];
NeilBrown4056ca52016-06-02 16:19:52 +10001658 rdev = conf->mirrors[i].rdev;
1659 if (rdev)
NeilBrown08464e02016-11-02 14:16:50 +11001660 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1661 i, !test_bit(In_sync, &rdev->flags),
1662 !test_bit(Faulty, &rdev->flags),
1663 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665}
1666
NeilBrowne879a872011-10-11 16:49:02 +11001667static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
NeilBrown0a27ec92006-01-06 00:20:13 -08001669 wait_barrier(conf);
1670 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 mempool_destroy(conf->r10buf_pool);
1673 conf->r10buf_pool = NULL;
1674}
1675
NeilBrownfd01b882011-10-11 16:47:53 +11001676static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001679 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001680 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001681 int count = 0;
1682 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /*
1685 * Find all non-in_sync disks within the RAID10 configuration
1686 * and mark them in_sync
1687 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001688 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001690 if (tmp->replacement
1691 && tmp->replacement->recovery_offset == MaxSector
1692 && !test_bit(Faulty, &tmp->replacement->flags)
1693 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1694 /* Replacement has just become active */
1695 if (!tmp->rdev
1696 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1697 count++;
1698 if (tmp->rdev) {
1699 /* Replaced device not technically faulty,
1700 * but we need to be sure it gets removed
1701 * and never re-added.
1702 */
1703 set_bit(Faulty, &tmp->rdev->flags);
1704 sysfs_notify_dirent_safe(
1705 tmp->rdev->sysfs_state);
1706 }
1707 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1708 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001709 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001710 && !test_bit(Faulty, &tmp->rdev->flags)
1711 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001712 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001713 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715 }
NeilBrown6b965622010-08-18 11:56:59 +10001716 spin_lock_irqsave(&conf->device_lock, flags);
1717 mddev->degraded -= count;
1718 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001721 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
NeilBrownfd01b882011-10-11 16:47:53 +11001724static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
NeilBrowne879a872011-10-11 16:49:02 +11001726 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001727 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001729 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001730 int last = conf->geo.raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 if (mddev->recovery_cp < MaxSector)
1733 /* only hot-add to in-sync arrays, as recovery is
1734 * very different from resync
1735 */
Neil Brown199050e2008-06-28 08:31:33 +10001736 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001737 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Dan Williams1501efa2016-01-13 16:00:07 -08001740 if (md_integrity_add_rdev(rdev, mddev))
1741 return -ENXIO;
1742
NeilBrowna53a6c82008-11-06 17:28:20 +11001743 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001744 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001746 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001747 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1748 mirror = rdev->saved_raid_disk;
1749 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001750 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001751 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001752 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001753 if (p->recovery_disabled == mddev->recovery_disabled)
1754 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001755 if (p->rdev) {
1756 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1757 p->replacement != NULL)
1758 continue;
1759 clear_bit(In_sync, &rdev->flags);
1760 set_bit(Replacement, &rdev->flags);
1761 rdev->raid_disk = mirror;
1762 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001763 if (mddev->gendisk)
1764 disk_stack_limits(mddev->gendisk, rdev->bdev,
1765 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001766 conf->fullsync = 1;
1767 rcu_assign_pointer(p->replacement, rdev);
1768 break;
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jonathan Brassow9092c022013-05-02 14:19:24 -05001771 if (mddev->gendisk)
1772 disk_stack_limits(mddev->gendisk, rdev->bdev,
1773 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
NeilBrown2bb77732011-07-27 11:00:36 +10001775 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001776 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001777 rdev->raid_disk = mirror;
1778 err = 0;
1779 if (rdev->saved_raid_disk != mirror)
1780 conf->fullsync = 1;
1781 rcu_assign_pointer(p->rdev, rdev);
1782 break;
1783 }
Jonathan Brassowed30be02012-10-31 11:42:30 +11001784 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001785 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
NeilBrownb8321b62011-12-23 10:17:51 +11001791static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
NeilBrowne879a872011-10-11 16:49:02 +11001793 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001795 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001796 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001797 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001800 if (rdev == p->rdev)
1801 rdevp = &p->rdev;
1802 else if (rdev == p->replacement)
1803 rdevp = &p->replacement;
1804 else
1805 return 0;
1806
1807 if (test_bit(In_sync, &rdev->flags) ||
1808 atomic_read(&rdev->nr_pending)) {
1809 err = -EBUSY;
1810 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
NeilBrownd787be42016-06-02 16:19:53 +10001812 /* Only remove non-faulty devices if recovery
NeilBrownc8ab9032011-12-23 10:17:54 +11001813 * is not possible.
1814 */
1815 if (!test_bit(Faulty, &rdev->flags) &&
1816 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001817 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001818 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001819 enough(conf, -1)) {
1820 err = -EBUSY;
1821 goto abort;
1822 }
1823 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001824 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1825 synchronize_rcu();
1826 if (atomic_read(&rdev->nr_pending)) {
1827 /* lost the race, try later */
1828 err = -EBUSY;
1829 *rdevp = rdev;
1830 goto abort;
1831 }
1832 }
1833 if (p->replacement) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001834 /* We must have just cleared 'rdev' */
1835 p->rdev = p->replacement;
1836 clear_bit(Replacement, &p->replacement->flags);
1837 smp_mb(); /* Make sure other CPUs may see both as identical
1838 * but will never see neither -- if they are careful.
1839 */
1840 p->replacement = NULL;
1841 clear_bit(WantReplacement, &rdev->flags);
1842 } else
1843 /* We might have just remove the Replacement as faulty
1844 * Clear the flag just in case
1845 */
1846 clear_bit(WantReplacement, &rdev->flags);
1847
NeilBrownc8ab9032011-12-23 10:17:54 +11001848 err = md_integrity_register(mddev);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850abort:
1851
1852 print_conf(conf);
1853 return err;
1854}
1855
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001856static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001858 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001859 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001860 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
NeilBrown3ea7daa2012-05-22 13:53:47 +10001862 if (bio == r10_bio->master_bio) {
1863 /* this is a reshape read */
1864 d = r10_bio->read_slot; /* really the read dev */
1865 } else
1866 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001867
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001868 if (!bio->bi_error)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001869 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001870 else
1871 /* The write handler will notice the lack of
1872 * R10BIO_Uptodate and record any errors etc
1873 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001874 atomic_add(r10_bio->sectors,
1875 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* for reconstruct, we always reschedule after a read.
1878 * for resync, only after all reads
1879 */
NeilBrown73d5c382009-02-25 13:18:47 +11001880 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1882 atomic_dec_and_test(&r10_bio->remaining)) {
1883 /* we have read all the blocks,
1884 * do the comparison in process context in raid10d
1885 */
1886 reschedule_retry(r10_bio);
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
NeilBrown9f2c9d12011-10-11 16:48:43 +11001890static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001891{
NeilBrownfd01b882011-10-11 16:47:53 +11001892 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001893
1894 while (atomic_dec_and_test(&r10_bio->remaining)) {
1895 if (r10_bio->master_bio == NULL) {
1896 /* the primary of several recovery bios */
1897 sector_t s = r10_bio->sectors;
1898 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1899 test_bit(R10BIO_WriteError, &r10_bio->state))
1900 reschedule_retry(r10_bio);
1901 else
1902 put_buf(r10_bio);
1903 md_done_sync(mddev, s, 1);
1904 break;
1905 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001906 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001907 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1908 test_bit(R10BIO_WriteError, &r10_bio->state))
1909 reschedule_retry(r10_bio);
1910 else
1911 put_buf(r10_bio);
1912 r10_bio = r10_bio2;
1913 }
1914 }
1915}
1916
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001917static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001919 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001920 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001921 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001922 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001923 sector_t first_bad;
1924 int bad_sectors;
1925 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001926 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001927 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
NeilBrown9ad1aef2011-12-23 10:17:55 +11001929 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1930 if (repl)
1931 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001932 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001933 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001935 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001936 if (repl)
1937 md_error(mddev, rdev);
1938 else {
1939 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11001940 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1941 set_bit(MD_RECOVERY_NEEDED,
1942 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11001943 set_bit(R10BIO_WriteError, &r10_bio->state);
1944 }
1945 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001946 r10_bio->devs[slot].addr,
1947 r10_bio->sectors,
1948 &first_bad, &bad_sectors))
1949 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001950
NeilBrown9ad1aef2011-12-23 10:17:55 +11001951 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001952
1953 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956/*
1957 * Note: sync and recover and handled very differently for raid10
1958 * This code is for resync.
1959 * For resync, we read through virtual addresses and read all blocks.
1960 * If there is any error, we schedule a write. The lowest numbered
1961 * drive is authoritative.
1962 * However requests come for physical address, so we need to map.
1963 * For every physical address there are raid_disks/copies virtual addresses,
1964 * which is always are least one, but is not necessarly an integer.
1965 * This means that a physical address can span multiple chunks, so we may
1966 * have to submit multiple io requests for a single sync request.
1967 */
1968/*
1969 * We check if all blocks are in-sync and only write to blocks that
1970 * aren't in sync
1971 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001972static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
NeilBrowne879a872011-10-11 16:49:02 +11001974 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int i, first;
1976 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10001977 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 atomic_set(&r10_bio->remaining, 1);
1980
1981 /* find the first device with a block */
1982 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001983 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
1985
1986 if (i == conf->copies)
1987 goto done;
1988
1989 first = i;
1990 fbio = r10_bio->devs[i].bio;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11001991 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
1992 fbio->bi_iter.bi_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
majianpengf4380a92012-04-12 16:04:47 +10001994 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001996 for (i=0 ; i < conf->copies ; i++) {
1997 int j, d;
NeilBrown8d3ca832016-11-18 16:16:12 +11001998 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002001
2002 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002004 if (i == first)
2005 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002006 d = r10_bio->devs[i].devnum;
2007 rdev = conf->mirrors[d].rdev;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002008 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002009 /* We know that the bi_io_vec layout is the same for
2010 * both 'first' and 'i', so we just compare them.
2011 * All vec entries are PAGE_SIZE;
2012 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002013 int sectors = r10_bio->sectors;
2014 for (j = 0; j < vcnt; j++) {
2015 int len = PAGE_SIZE;
2016 if (sectors < (len / 512))
2017 len = sectors * 512;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002018 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2019 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown7bb23c42013-07-16 16:50:47 +10002020 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002021 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002022 sectors -= len/512;
2023 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002024 if (j == vcnt)
2025 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002026 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002027 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2028 /* Don't fix anything. */
2029 continue;
NeilBrown8d3ca832016-11-18 16:16:12 +11002030 } else if (test_bit(FailFast, &rdev->flags)) {
2031 /* Just give up on this device */
2032 md_error(rdev->mddev, rdev);
2033 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002034 }
NeilBrownf84ee362011-07-28 11:39:25 +10002035 /* Ok, we need to write this bio, either to correct an
2036 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 * First we need to fixup bv_offset, bv_len and
2038 * bi_vecs, as the read request might have corrupted these
2039 */
Kent Overstreet8be185f2012-09-06 14:14:43 -07002040 bio_reset(tbio);
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 tbio->bi_vcnt = vcnt;
Artur Paszkiewiczcc578582015-12-18 15:19:16 +11002043 tbio->bi_iter.bi_size = fbio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 tbio->bi_private = r10_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002045 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 tbio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05002047 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Kent Overstreetc31df252015-05-06 23:34:20 -07002049 bio_copy_data(tbio, fbio);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2052 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002053 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Kent Overstreet4f024f32013-10-11 15:44:27 -07002055 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2057 generic_make_request(tbio);
2058 }
2059
NeilBrown9ad1aef2011-12-23 10:17:55 +11002060 /* Now write out to any replacement devices
2061 * that are active
2062 */
2063 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002064 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002065
2066 tbio = r10_bio->devs[i].repl_bio;
2067 if (!tbio || !tbio->bi_end_io)
2068 continue;
2069 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2070 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002071 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002072 d = r10_bio->devs[i].devnum;
2073 atomic_inc(&r10_bio->remaining);
2074 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002075 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002076 generic_make_request(tbio);
2077 }
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079done:
2080 if (atomic_dec_and_test(&r10_bio->remaining)) {
2081 md_done_sync(mddev, r10_bio->sectors, 1);
2082 put_buf(r10_bio);
2083 }
2084}
2085
2086/*
2087 * Now for the recovery code.
2088 * Recovery happens across physical sectors.
2089 * We recover all non-is_sync drives by finding the virtual address of
2090 * each, and then choose a working drive that also has that virt address.
2091 * There is a separate r10_bio for each non-in_sync drive.
2092 * Only the first two slots are in use. The first for reading,
2093 * The second for writing.
2094 *
2095 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002096static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002097{
2098 /* We got a read error during recovery.
2099 * We repeat the read in smaller page-sized sections.
2100 * If a read succeeds, write it to the new device or record
2101 * a bad block if we cannot.
2102 * If a read fails, record a bad block on both old and
2103 * new devices.
2104 */
NeilBrownfd01b882011-10-11 16:47:53 +11002105 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002106 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002107 struct bio *bio = r10_bio->devs[0].bio;
2108 sector_t sect = 0;
2109 int sectors = r10_bio->sectors;
2110 int idx = 0;
2111 int dr = r10_bio->devs[0].devnum;
2112 int dw = r10_bio->devs[1].devnum;
2113
2114 while (sectors) {
2115 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002116 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002117 sector_t addr;
2118 int ok;
2119
2120 if (s > (PAGE_SIZE>>9))
2121 s = PAGE_SIZE >> 9;
2122
2123 rdev = conf->mirrors[dr].rdev;
2124 addr = r10_bio->devs[0].addr + sect,
2125 ok = sync_page_io(rdev,
2126 addr,
2127 s << 9,
2128 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002129 REQ_OP_READ, 0, false);
NeilBrown5e570282011-07-28 11:39:25 +10002130 if (ok) {
2131 rdev = conf->mirrors[dw].rdev;
2132 addr = r10_bio->devs[1].addr + sect;
2133 ok = sync_page_io(rdev,
2134 addr,
2135 s << 9,
2136 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05002137 REQ_OP_WRITE, 0, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002138 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002139 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002140 if (!test_and_set_bit(WantReplacement,
2141 &rdev->flags))
2142 set_bit(MD_RECOVERY_NEEDED,
2143 &rdev->mddev->recovery);
2144 }
NeilBrown5e570282011-07-28 11:39:25 +10002145 }
2146 if (!ok) {
2147 /* We don't worry if we cannot set a bad block -
2148 * it really is bad so there is no loss in not
2149 * recording it yet
2150 */
2151 rdev_set_badblocks(rdev, addr, s, 0);
2152
2153 if (rdev != conf->mirrors[dw].rdev) {
2154 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002155 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002156 addr = r10_bio->devs[1].addr + sect;
2157 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2158 if (!ok) {
2159 /* just abort the recovery */
NeilBrown08464e02016-11-02 14:16:50 +11002160 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2161 mdname(mddev));
NeilBrown5e570282011-07-28 11:39:25 +10002162
2163 conf->mirrors[dw].recovery_disabled
2164 = mddev->recovery_disabled;
2165 set_bit(MD_RECOVERY_INTR,
2166 &mddev->recovery);
2167 break;
2168 }
2169 }
2170 }
2171
2172 sectors -= s;
2173 sect += s;
2174 idx++;
2175 }
2176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
NeilBrown9f2c9d12011-10-11 16:48:43 +11002178static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
NeilBrowne879a872011-10-11 16:49:02 +11002180 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002181 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002182 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
NeilBrown5e570282011-07-28 11:39:25 +10002184 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2185 fix_recovery_read_error(r10_bio);
2186 end_sync_request(r10_bio);
2187 return;
2188 }
2189
Namhyung Kimc65060a2011-07-18 17:38:49 +10002190 /*
2191 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 * and submit the write request
2193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002195 wbio = r10_bio->devs[1].bio;
2196 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002197 /* Need to test wbio2->bi_end_io before we call
2198 * generic_make_request as if the former is NULL,
2199 * the latter is free to free wbio2.
2200 */
2201 if (wbio2 && !wbio2->bi_end_io)
2202 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002203 if (wbio->bi_end_io) {
2204 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002205 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002206 generic_make_request(wbio);
2207 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002208 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002209 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2210 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002211 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002212 generic_make_request(wbio2);
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216/*
Robert Becker1e509152009-12-14 12:49:58 +11002217 * Used by fix_read_error() to decay the per rdev read_errors.
2218 * We halve the read error count for every hour that has elapsed
2219 * since the last recorded read error.
2220 *
2221 */
NeilBrownfd01b882011-10-11 16:47:53 +11002222static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002223{
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002224 long cur_time_mon;
Robert Becker1e509152009-12-14 12:49:58 +11002225 unsigned long hours_since_last;
2226 unsigned int read_errors = atomic_read(&rdev->read_errors);
2227
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002228 cur_time_mon = ktime_get_seconds();
Robert Becker1e509152009-12-14 12:49:58 +11002229
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002230 if (rdev->last_read_error == 0) {
Robert Becker1e509152009-12-14 12:49:58 +11002231 /* first time we've seen a read error */
2232 rdev->last_read_error = cur_time_mon;
2233 return;
2234 }
2235
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02002236 hours_since_last = (long)(cur_time_mon -
2237 rdev->last_read_error) / 3600;
Robert Becker1e509152009-12-14 12:49:58 +11002238
2239 rdev->last_read_error = cur_time_mon;
2240
2241 /*
2242 * if hours_since_last is > the number of bits in read_errors
2243 * just set read errors to 0. We do this to avoid
2244 * overflowing the shift of read_errors by hours_since_last.
2245 */
2246 if (hours_since_last >= 8 * sizeof(read_errors))
2247 atomic_set(&rdev->read_errors, 0);
2248 else
2249 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2250}
2251
NeilBrown3cb03002011-10-11 16:45:26 +11002252static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002253 int sectors, struct page *page, int rw)
2254{
2255 sector_t first_bad;
2256 int bad_sectors;
2257
2258 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2259 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2260 return -1;
Mike Christie796a5cf2016-06-05 14:32:07 -05002261 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrown58c54fc2011-07-28 11:39:25 +10002262 /* success */
2263 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002264 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002265 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002266 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2267 set_bit(MD_RECOVERY_NEEDED,
2268 &rdev->mddev->recovery);
2269 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002270 /* need to record an error - either for the block or the device */
2271 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2272 md_error(rdev->mddev, rdev);
2273 return 0;
2274}
2275
Robert Becker1e509152009-12-14 12:49:58 +11002276/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * This is a kernel thread which:
2278 *
2279 * 1. Retries failed read operations on working mirrors.
2280 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002281 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 */
2283
NeilBrowne879a872011-10-11 16:49:02 +11002284static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002285{
2286 int sect = 0; /* Offset from r10_bio->sector */
2287 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002288 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002289 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002290 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002291
NeilBrown7c4e06f2011-05-11 14:53:17 +10002292 /* still own a reference to this rdev, so it cannot
2293 * have been cleared recently.
2294 */
2295 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002296
NeilBrown7c4e06f2011-05-11 14:53:17 +10002297 if (test_bit(Faulty, &rdev->flags))
2298 /* drive has already been failed, just ignore any
2299 more fix_read_error() attempts */
2300 return;
2301
2302 check_decay_read_errors(mddev, rdev);
2303 atomic_inc(&rdev->read_errors);
2304 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2305 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002306 bdevname(rdev->bdev, b);
2307
NeilBrown08464e02016-11-02 14:16:50 +11002308 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2309 mdname(mddev), b,
2310 atomic_read(&rdev->read_errors), max_read_errors);
2311 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2312 mdname(mddev), b);
NeilBrownd683c8e2016-06-02 16:19:52 +10002313 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002314 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002315 return;
Robert Becker1e509152009-12-14 12:49:58 +11002316 }
Robert Becker1e509152009-12-14 12:49:58 +11002317
NeilBrown6814d532006-10-03 01:15:45 -07002318 while(sectors) {
2319 int s = sectors;
2320 int sl = r10_bio->read_slot;
2321 int success = 0;
2322 int start;
2323
2324 if (s > (PAGE_SIZE>>9))
2325 s = PAGE_SIZE >> 9;
2326
2327 rcu_read_lock();
2328 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002329 sector_t first_bad;
2330 int bad_sectors;
2331
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002332 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002333 rdev = rcu_dereference(conf->mirrors[d].rdev);
2334 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002335 test_bit(In_sync, &rdev->flags) &&
NeilBrownf5b67ae2016-06-02 16:19:53 +10002336 !test_bit(Faulty, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002337 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2338 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002339 atomic_inc(&rdev->nr_pending);
2340 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002341 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002342 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002343 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002344 s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002345 conf->tmppage,
2346 REQ_OP_READ, 0, false);
NeilBrown6814d532006-10-03 01:15:45 -07002347 rdev_dec_pending(rdev, mddev);
2348 rcu_read_lock();
2349 if (success)
2350 break;
2351 }
2352 sl++;
2353 if (sl == conf->copies)
2354 sl = 0;
2355 } while (!success && sl != r10_bio->read_slot);
2356 rcu_read_unlock();
2357
2358 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002359 /* Cannot read from anywhere, just mark the block
2360 * as bad on the first device to discourage future
2361 * reads.
2362 */
NeilBrown6814d532006-10-03 01:15:45 -07002363 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002364 rdev = conf->mirrors[dn].rdev;
2365
2366 if (!rdev_set_badblocks(
2367 rdev,
2368 r10_bio->devs[r10_bio->read_slot].addr
2369 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002370 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002371 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002372 r10_bio->devs[r10_bio->read_slot].bio
2373 = IO_BLOCKED;
2374 }
NeilBrown6814d532006-10-03 01:15:45 -07002375 break;
2376 }
2377
2378 start = sl;
2379 /* write it back and re-read */
2380 rcu_read_lock();
2381 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002382 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002383
NeilBrown6814d532006-10-03 01:15:45 -07002384 if (sl==0)
2385 sl = conf->copies;
2386 sl--;
2387 d = r10_bio->devs[sl].devnum;
2388 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002389 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002390 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002391 !test_bit(In_sync, &rdev->flags))
2392 continue;
2393
2394 atomic_inc(&rdev->nr_pending);
2395 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002396 if (r10_sync_page_io(rdev,
2397 r10_bio->devs[sl].addr +
2398 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002399 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002400 == 0) {
2401 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002402 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2403 mdname(mddev), s,
2404 (unsigned long long)(
2405 sect +
2406 choose_data_offset(r10_bio,
2407 rdev)),
2408 bdevname(rdev->bdev, b));
2409 pr_notice("md/raid10:%s: %s: failing drive\n",
2410 mdname(mddev),
2411 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002412 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002413 rdev_dec_pending(rdev, mddev);
2414 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002415 }
2416 sl = start;
2417 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002418 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002419
NeilBrown6814d532006-10-03 01:15:45 -07002420 if (sl==0)
2421 sl = conf->copies;
2422 sl--;
2423 d = r10_bio->devs[sl].devnum;
2424 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002425 if (!rdev ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10002426 test_bit(Faulty, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002427 !test_bit(In_sync, &rdev->flags))
2428 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002429
NeilBrown1294b9c2011-07-28 11:39:23 +10002430 atomic_inc(&rdev->nr_pending);
2431 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002432 switch (r10_sync_page_io(rdev,
2433 r10_bio->devs[sl].addr +
2434 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002435 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002436 READ)) {
2437 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002438 /* Well, this device is dead */
NeilBrown08464e02016-11-02 14:16:50 +11002439 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002440 mdname(mddev), s,
2441 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002442 sect +
2443 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002444 bdevname(rdev->bdev, b));
NeilBrown08464e02016-11-02 14:16:50 +11002445 pr_notice("md/raid10:%s: %s: failing drive\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002446 mdname(mddev),
2447 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002448 break;
2449 case 1:
NeilBrown08464e02016-11-02 14:16:50 +11002450 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
NeilBrown1294b9c2011-07-28 11:39:23 +10002451 mdname(mddev), s,
2452 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002453 sect +
2454 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002455 bdevname(rdev->bdev, b));
2456 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002457 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002458
2459 rdev_dec_pending(rdev, mddev);
2460 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002461 }
2462 rcu_read_unlock();
2463
2464 sectors -= s;
2465 sect += s;
2466 }
2467}
2468
NeilBrown9f2c9d12011-10-11 16:48:43 +11002469static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002470{
2471 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002472 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002473 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002474 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002475 /* bio has the data to be written to slot 'i' where
2476 * we just recently had a write error.
2477 * We repeatedly clone the bio and trim down to one block,
2478 * then try the write. Where the write fails we record
2479 * a bad block.
2480 * It is conceivable that the bio doesn't exactly align with
2481 * blocks. We must handle this.
2482 *
2483 * We currently own a reference to the rdev.
2484 */
2485
2486 int block_sectors;
2487 sector_t sector;
2488 int sectors;
2489 int sect_to_write = r10_bio->sectors;
2490 int ok = 1;
2491
2492 if (rdev->badblocks.shift < 0)
2493 return 0;
2494
NeilBrownf04ebb02015-02-16 14:51:54 +11002495 block_sectors = roundup(1 << rdev->badblocks.shift,
2496 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002497 sector = r10_bio->sector;
2498 sectors = ((r10_bio->sector + block_sectors)
2499 & ~(sector_t)(block_sectors - 1))
2500 - sector;
2501
2502 while (sect_to_write) {
2503 struct bio *wbio;
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002504 sector_t wsector;
NeilBrownbd870a12011-07-28 11:39:24 +10002505 if (sectors > sect_to_write)
2506 sectors = sect_to_write;
2507 /* Write at 'sector' for 'sectors' */
2508 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002509 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002510 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2511 wbio->bi_iter.bi_sector = wsector +
2512 choose_data_offset(r10_bio, rdev);
NeilBrownbd870a12011-07-28 11:39:24 +10002513 wbio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002514 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05002515
2516 if (submit_bio_wait(wbio) < 0)
NeilBrownbd870a12011-07-28 11:39:24 +10002517 /* Failure! */
Tomasz Majchrzak27028622016-08-23 10:53:57 +02002518 ok = rdev_set_badblocks(rdev, wsector,
NeilBrownbd870a12011-07-28 11:39:24 +10002519 sectors, 0)
2520 && ok;
2521
2522 bio_put(wbio);
2523 sect_to_write -= sectors;
2524 sector += sectors;
2525 sectors = block_sectors;
2526 }
2527 return ok;
2528}
2529
NeilBrown9f2c9d12011-10-11 16:48:43 +11002530static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002531{
2532 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002533 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002534 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002535 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002536 char b[BDEVNAME_SIZE];
2537 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002538 int max_sectors;
NeilBrown109e3762016-11-18 13:22:04 +11002539 dev_t bio_dev;
2540 sector_t bio_last_sector;
NeilBrown560f8e52011-07-28 11:39:23 +10002541
2542 /* we got a read error. Maybe the drive is bad. Maybe just
2543 * the block and we can fix it.
2544 * We freeze all other IO, and try reading the block from
2545 * other devices. When we find one, we re-write
2546 * and check it that fixes the read error.
2547 * This is all done synchronously while the array is
2548 * frozen.
2549 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002550 bio = r10_bio->devs[slot].bio;
2551 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002552 bio_dev = bio->bi_bdev->bd_dev;
2553 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
NeilBrownfae8cc52012-02-14 11:10:10 +11002554 bio_put(bio);
2555 r10_bio->devs[slot].bio = NULL;
2556
NeilBrown8d3ca832016-11-18 16:16:12 +11002557 if (mddev->ro)
2558 r10_bio->devs[slot].bio = IO_BLOCKED;
2559 else if (!test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002560 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002561 fix_read_error(conf, mddev, r10_bio);
2562 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002563 } else
NeilBrown8d3ca832016-11-18 16:16:12 +11002564 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002565
NeilBrownabbf0982011-12-23 10:17:54 +11002566 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002567
NeilBrown7399c312011-07-28 11:39:23 +10002568read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002569 rdev = read_balance(conf, r10_bio, &max_sectors);
2570 if (rdev == NULL) {
NeilBrown08464e02016-11-02 14:16:50 +11002571 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2572 mdname(mddev), b,
2573 (unsigned long long)r10_bio->sector);
NeilBrown560f8e52011-07-28 11:39:23 +10002574 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002575 return;
2576 }
2577
Jens Axboe1eff9d32016-08-05 15:35:16 -06002578 do_sync = (r10_bio->master_bio->bi_opf & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002579 slot = r10_bio->read_slot;
NeilBrown08464e02016-11-02 14:16:50 +11002580 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2581 mdname(mddev),
2582 bdevname(rdev->bdev, b),
2583 (unsigned long long)r10_bio->sector);
NeilBrown560f8e52011-07-28 11:39:23 +10002584 bio = bio_clone_mddev(r10_bio->master_bio,
2585 GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002586 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002587 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002588 r10_bio->devs[slot].rdev = rdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002589 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002590 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002591 bio->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05002592 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown8d3ca832016-11-18 16:16:12 +11002593 if (test_bit(FailFast, &rdev->flags) &&
2594 test_bit(R10BIO_FailFast, &r10_bio->state))
2595 bio->bi_opf |= MD_FAILFAST;
NeilBrown560f8e52011-07-28 11:39:23 +10002596 bio->bi_private = r10_bio;
2597 bio->bi_end_io = raid10_end_read_request;
NeilBrown109e3762016-11-18 13:22:04 +11002598 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2599 bio, bio_dev,
2600 bio_last_sector - r10_bio->sectors);
2601
NeilBrown7399c312011-07-28 11:39:23 +10002602 if (max_sectors < r10_bio->sectors) {
2603 /* Drat - have to split this up more */
2604 struct bio *mbio = r10_bio->master_bio;
2605 int sectors_handled =
2606 r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002607 - mbio->bi_iter.bi_sector;
NeilBrown7399c312011-07-28 11:39:23 +10002608 r10_bio->sectors = max_sectors;
2609 spin_lock_irq(&conf->device_lock);
2610 if (mbio->bi_phys_segments == 0)
2611 mbio->bi_phys_segments = 2;
2612 else
2613 mbio->bi_phys_segments++;
2614 spin_unlock_irq(&conf->device_lock);
2615 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002616
2617 r10_bio = mempool_alloc(conf->r10bio_pool,
2618 GFP_NOIO);
2619 r10_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002620 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown7399c312011-07-28 11:39:23 +10002621 r10_bio->state = 0;
2622 set_bit(R10BIO_ReadError,
2623 &r10_bio->state);
2624 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002625 r10_bio->sector = mbio->bi_iter.bi_sector
NeilBrown7399c312011-07-28 11:39:23 +10002626 + sectors_handled;
2627
2628 goto read_more;
2629 } else
2630 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002631}
2632
NeilBrowne879a872011-10-11 16:49:02 +11002633static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002634{
2635 /* Some sort of write request has finished and it
2636 * succeeded in writing where we thought there was a
2637 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002638 * Or possibly if failed and we need to record
2639 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002640 */
2641 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002642 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002643
2644 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2645 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002646 for (m = 0; m < conf->copies; m++) {
2647 int dev = r10_bio->devs[m].devnum;
2648 rdev = conf->mirrors[dev].rdev;
2649 if (r10_bio->devs[m].bio == NULL)
2650 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002651 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002652 rdev_clear_badblocks(
2653 rdev,
2654 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002655 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002656 } else {
2657 if (!rdev_set_badblocks(
2658 rdev,
2659 r10_bio->devs[m].addr,
2660 r10_bio->sectors, 0))
2661 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002662 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002663 rdev = conf->mirrors[dev].replacement;
2664 if (r10_bio->devs[m].repl_bio == NULL)
2665 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002666
2667 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002668 rdev_clear_badblocks(
2669 rdev,
2670 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002671 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002672 } else {
2673 if (!rdev_set_badblocks(
2674 rdev,
2675 r10_bio->devs[m].addr,
2676 r10_bio->sectors, 0))
2677 md_error(conf->mddev, rdev);
2678 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002679 }
NeilBrown749c55e2011-07-28 11:39:24 +10002680 put_buf(r10_bio);
2681 } else {
NeilBrown95af5872015-08-14 11:26:17 +10002682 bool fail = false;
NeilBrownbd870a12011-07-28 11:39:24 +10002683 for (m = 0; m < conf->copies; m++) {
2684 int dev = r10_bio->devs[m].devnum;
2685 struct bio *bio = r10_bio->devs[m].bio;
2686 rdev = conf->mirrors[dev].rdev;
2687 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002688 rdev_clear_badblocks(
2689 rdev,
2690 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002691 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002692 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002693 } else if (bio != NULL && bio->bi_error) {
NeilBrown95af5872015-08-14 11:26:17 +10002694 fail = true;
NeilBrownbd870a12011-07-28 11:39:24 +10002695 if (!narrow_write_error(r10_bio, m)) {
2696 md_error(conf->mddev, rdev);
2697 set_bit(R10BIO_Degraded,
2698 &r10_bio->state);
2699 }
2700 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002701 }
NeilBrown475b0322011-12-23 10:17:55 +11002702 bio = r10_bio->devs[m].repl_bio;
2703 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002704 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002705 rdev_clear_badblocks(
2706 rdev,
2707 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002708 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002709 rdev_dec_pending(rdev, conf->mddev);
2710 }
NeilBrownbd870a12011-07-28 11:39:24 +10002711 }
NeilBrown95af5872015-08-14 11:26:17 +10002712 if (fail) {
2713 spin_lock_irq(&conf->device_lock);
2714 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
Shaohua Li23ddba82016-03-14 11:49:32 -07002715 conf->nr_queued++;
NeilBrown95af5872015-08-14 11:26:17 +10002716 spin_unlock_irq(&conf->device_lock);
2717 md_wakeup_thread(conf->mddev->thread);
NeilBrownc3407022015-10-24 16:23:48 +11002718 } else {
2719 if (test_bit(R10BIO_WriteError,
2720 &r10_bio->state))
2721 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002722 raid_end_bio_io(r10_bio);
NeilBrownc3407022015-10-24 16:23:48 +11002723 }
NeilBrown749c55e2011-07-28 11:39:24 +10002724 }
2725}
2726
Shaohua Li4ed87312012-10-11 13:34:00 +11002727static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Shaohua Li4ed87312012-10-11 13:34:00 +11002729 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002730 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002732 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002734 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
NeilBrown95af5872015-08-14 11:26:17 +10002738 if (!list_empty_careful(&conf->bio_end_io_list) &&
2739 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2740 LIST_HEAD(tmp);
2741 spin_lock_irqsave(&conf->device_lock, flags);
2742 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
Shaohua Li23ddba82016-03-14 11:49:32 -07002743 while (!list_empty(&conf->bio_end_io_list)) {
2744 list_move(conf->bio_end_io_list.prev, &tmp);
2745 conf->nr_queued--;
2746 }
NeilBrown95af5872015-08-14 11:26:17 +10002747 }
2748 spin_unlock_irqrestore(&conf->device_lock, flags);
2749 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002750 r10_bio = list_first_entry(&tmp, struct r10bio,
2751 retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10002752 list_del(&r10_bio->retry_list);
NeilBrownc3407022015-10-24 16:23:48 +11002753 if (mddev->degraded)
2754 set_bit(R10BIO_Degraded, &r10_bio->state);
2755
2756 if (test_bit(R10BIO_WriteError,
2757 &r10_bio->state))
2758 close_write(r10_bio);
NeilBrown95af5872015-08-14 11:26:17 +10002759 raid_end_bio_io(r10_bio);
2760 }
2761 }
2762
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002763 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002765
NeilBrown0021b7b2012-07-31 09:08:14 +02002766 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002769 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002770 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002772 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002773 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002775 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 spin_unlock_irqrestore(&conf->device_lock, flags);
2777
2778 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002779 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002780 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2781 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002782 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002783 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2784 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002785 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002787 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002789 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002790 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002791 else {
2792 /* just a partial read to be scheduled from a
2793 * separate context
2794 */
2795 int slot = r10_bio->read_slot;
2796 generic_make_request(r10_bio->devs[slot].bio);
2797 }
NeilBrown4443ae12006-01-06 00:20:28 -08002798
NeilBrown1d9d5242009-10-16 15:55:32 +11002799 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002800 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2801 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002803 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
NeilBrowne879a872011-10-11 16:49:02 +11002806static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002809 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002812 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002813 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002814 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002815 if (conf->mirrors[i].replacement)
2816 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2818 if (!conf->r10buf_pool)
2819 return -ENOMEM;
2820 conf->next_resync = 0;
2821 return 0;
2822}
2823
2824/*
2825 * perform a "sync" on one "block"
2826 *
2827 * We need to make sure that no normal I/O request - particularly write
2828 * requests - conflict with active sync requests.
2829 *
2830 * This is achieved by tracking pending requests and a 'barrier' concept
2831 * that can be installed to exclude normal IO requests.
2832 *
2833 * Resync and recovery are handled very differently.
2834 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2835 *
2836 * For resync, we iterate over virtual addresses, read all copies,
2837 * and update if there are differences. If only one copy is live,
2838 * skip it.
2839 * For recovery, we iterate over physical addresses, read a good
2840 * value for each non-in_sync drive, and over-write.
2841 *
2842 * So, for recovery we may have several outstanding complex requests for a
2843 * given address, one for each out-of-sync device. We model this by allocating
2844 * a number of r10_bio structures, one for each out-of-sync device.
2845 * As we setup these structures, we collect all bio's together into a list
2846 * which we then process collectively to add pages, and then process again
2847 * to pass to generic_make_request.
2848 *
2849 * The r10_bio structures are linked using a borrowed master_bio pointer.
2850 * This link is counted in ->remaining. When the r10_bio that points to NULL
2851 * has its remaining count decremented to 0, the whole complex operation
2852 * is complete.
2853 *
2854 */
2855
Shaohua Li849674e2016-01-20 13:52:20 -08002856static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002857 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
NeilBrowne879a872011-10-11 16:49:02 +11002859 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002860 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 struct bio *biolist = NULL, *bio;
2862 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002864 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002865 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 sector_t sectors_skipped = 0;
2867 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002868 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 if (!conf->r10buf_pool)
2871 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002874 /*
2875 * Allow skipping a full rebuild for incremental assembly
2876 * of a clean array, like RAID1 does.
2877 */
2878 if (mddev->bitmap == NULL &&
2879 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002880 mddev->reshape_position == MaxSector &&
2881 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002882 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002883 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002884 conf->fullsync == 0) {
2885 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002886 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002887 }
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002890 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002891 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2892 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 max_sector = mddev->resync_max_sectors;
2894 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002895 /* If we aborted, we need to abort the
2896 * sync on the 'current' bitmap chucks (there can
2897 * be several when recovering multiple devices).
2898 * as we may have started syncing it but not finished.
2899 * We can find the current address in
2900 * mddev->curr_resync, but for recovery,
2901 * we need to convert that to several
2902 * virtual addresses.
2903 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002904 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2905 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002906 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002907 return 0;
2908 }
2909
NeilBrown6cce3b22006-01-06 00:20:16 -08002910 if (mddev->curr_resync < max_sector) { /* aborted */
2911 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2912 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2913 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002914 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002915 sector_t sect =
2916 raid10_find_virt(conf, mddev->curr_resync, i);
2917 bitmap_end_sync(mddev->bitmap, sect,
2918 &sync_blocks, 1);
2919 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002920 } else {
2921 /* completed sync */
2922 if ((!mddev->bitmap || conf->fullsync)
2923 && conf->have_replacement
2924 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2925 /* Completed a full sync so the replacements
2926 * are now fully recovered.
2927 */
NeilBrownf90145f2016-06-02 16:19:52 +10002928 rcu_read_lock();
2929 for (i = 0; i < conf->geo.raid_disks; i++) {
2930 struct md_rdev *rdev =
2931 rcu_dereference(conf->mirrors[i].replacement);
2932 if (rdev)
2933 rdev->recovery_offset = MaxSector;
2934 }
2935 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11002936 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002937 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002938 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002939 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002941 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return sectors_skipped;
2943 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002944
2945 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2946 return reshape_request(mddev, sector_nr, skipped);
2947
NeilBrown5cf00fc2012-05-21 09:28:20 +10002948 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* if there has been nothing to do on any drive,
2950 * then there is nothing to do at all..
2951 */
NeilBrown57afd892005-06-21 17:17:13 -07002952 *skipped = 1;
2953 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
2955
NeilBrownc6207272008-02-06 01:39:52 -08002956 if (max_sector > mddev->resync_max)
2957 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /* make sure whole request will fit in a chunk - if chunks
2960 * are meaningful
2961 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002962 if (conf->geo.near_copies < conf->geo.raid_disks &&
2963 max_sector > (sector_nr | chunk_mask))
2964 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002966 /*
2967 * If there is non-resync activity waiting for a turn, then let it
2968 * though before starting on this new sync request.
2969 */
2970 if (conf->nr_waiting)
2971 schedule_timeout_uninterruptible(1);
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 /* Again, very different code for resync and recovery.
2974 * Both must result in an r10bio with a list of bios that
2975 * have bi_end_io, bi_sector, bi_bdev set,
2976 * and bi_private set to the r10bio.
2977 * For recovery, we may actually create several r10bios
2978 * with 2 bios in each, that correspond to the bios in the main one.
2979 * In this case, the subordinate r10bios link back through a
2980 * borrowed master_bio pointer, and the counter in the master
2981 * includes a ref from each subordinate.
2982 */
2983 /* First, we decide what to do and set ->bi_end_io
2984 * To end_sync_read if we want to read, and
2985 * end_sync_write if we will want to write.
2986 */
2987
NeilBrown6cce3b22006-01-06 00:20:16 -08002988 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2990 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002991 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 r10_bio = NULL;
2993
NeilBrown5cf00fc2012-05-21 09:28:20 +10002994 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002995 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002996 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002997 sector_t sect;
2998 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002999 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003000 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownf90145f2016-06-02 16:19:52 +10003001 struct md_rdev *mrdev, *mreplace;
NeilBrownab9d47e2011-05-11 14:54:41 +10003002
NeilBrownf90145f2016-06-02 16:19:52 +10003003 rcu_read_lock();
3004 mrdev = rcu_dereference(mirror->rdev);
3005 mreplace = rcu_dereference(mirror->replacement);
3006
3007 if ((mrdev == NULL ||
NeilBrownf5b67ae2016-06-02 16:19:53 +10003008 test_bit(Faulty, &mrdev->flags) ||
NeilBrownf90145f2016-06-02 16:19:52 +10003009 test_bit(In_sync, &mrdev->flags)) &&
3010 (mreplace == NULL ||
3011 test_bit(Faulty, &mreplace->flags))) {
3012 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003013 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003014 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003015
3016 still_degraded = 0;
3017 /* want to reconstruct this device */
3018 rb2 = r10_bio;
3019 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003020 if (sect >= mddev->resync_max_sectors) {
3021 /* last stripe is not complete - don't
3022 * try to recover this sector.
3023 */
NeilBrownf90145f2016-06-02 16:19:52 +10003024 rcu_read_unlock();
NeilBrownfc448a12012-07-03 10:37:30 +10003025 continue;
3026 }
NeilBrownf5b67ae2016-06-02 16:19:53 +10003027 if (mreplace && test_bit(Faulty, &mreplace->flags))
3028 mreplace = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11003029 /* Unless we are doing a full sync, or a replacement
3030 * we only need to recover the block if it is set in
3031 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003032 */
3033 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3034 &sync_blocks, 1);
3035 if (sync_blocks < max_sync)
3036 max_sync = sync_blocks;
3037 if (!must_sync &&
NeilBrownf90145f2016-06-02 16:19:52 +10003038 mreplace == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003039 !conf->fullsync) {
3040 /* yep, skip the sync_blocks here, but don't assume
3041 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003042 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003043 chunks_skipped = -1;
NeilBrownf90145f2016-06-02 16:19:52 +10003044 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003045 continue;
3046 }
NeilBrownf90145f2016-06-02 16:19:52 +10003047 atomic_inc(&mrdev->nr_pending);
3048 if (mreplace)
3049 atomic_inc(&mreplace->nr_pending);
3050 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
NeilBrownab9d47e2011-05-11 14:54:41 +10003052 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003053 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003054 raise_barrier(conf, rb2 != NULL);
3055 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
NeilBrownab9d47e2011-05-11 14:54:41 +10003057 r10_bio->master_bio = (struct bio*)rb2;
3058 if (rb2)
3059 atomic_inc(&rb2->remaining);
3060 r10_bio->mddev = mddev;
3061 set_bit(R10BIO_IsRecover, &r10_bio->state);
3062 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003063
NeilBrownab9d47e2011-05-11 14:54:41 +10003064 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003065
NeilBrownab9d47e2011-05-11 14:54:41 +10003066 /* Need to check if the array will still be
3067 * degraded
3068 */
NeilBrownf90145f2016-06-02 16:19:52 +10003069 rcu_read_lock();
3070 for (j = 0; j < conf->geo.raid_disks; j++) {
3071 struct md_rdev *rdev = rcu_dereference(
3072 conf->mirrors[j].rdev);
3073 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003074 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
NeilBrownf90145f2016-06-02 16:19:52 +10003077 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003078
3079 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3080 &sync_blocks, still_degraded);
3081
NeilBrowne875ece2011-07-28 11:39:24 +10003082 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003083 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003084 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003085 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003086 sector_t from_addr, to_addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003087 struct md_rdev *rdev =
3088 rcu_dereference(conf->mirrors[d].rdev);
NeilBrown40c356c2011-07-28 11:39:24 +10003089 sector_t sector, first_bad;
3090 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003091 if (!rdev ||
3092 !test_bit(In_sync, &rdev->flags))
NeilBrownab9d47e2011-05-11 14:54:41 +10003093 continue;
3094 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003095 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003096 sector = r10_bio->devs[j].addr;
3097
3098 if (is_badblock(rdev, sector, max_sync,
3099 &first_bad, &bad_sectors)) {
3100 if (first_bad > sector)
3101 max_sync = first_bad - sector;
3102 else {
3103 bad_sectors -= (sector
3104 - first_bad);
3105 if (max_sync > bad_sectors)
3106 max_sync = bad_sectors;
3107 continue;
3108 }
3109 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003110 bio = r10_bio->devs[0].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003111 bio_reset(bio);
NeilBrownab9d47e2011-05-11 14:54:41 +10003112 bio->bi_next = biolist;
3113 biolist = bio;
3114 bio->bi_private = r10_bio;
3115 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003116 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003117 if (test_bit(FailFast, &rdev->flags))
3118 bio->bi_opf |= MD_FAILFAST;
NeilBrown5e570282011-07-28 11:39:25 +10003119 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003120 bio->bi_iter.bi_sector = from_addr +
3121 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003122 bio->bi_bdev = rdev->bdev;
3123 atomic_inc(&rdev->nr_pending);
3124 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003125
3126 for (k=0; k<conf->copies; k++)
3127 if (r10_bio->devs[k].devnum == i)
3128 break;
3129 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003130 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003131 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003132 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003133 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003134 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003135
NeilBrownf90145f2016-06-02 16:19:52 +10003136 if (!test_bit(In_sync, &mrdev->flags)) {
NeilBrown24afd802011-12-23 10:17:55 +11003137 bio = r10_bio->devs[1].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003138 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003139 bio->bi_next = biolist;
3140 biolist = bio;
3141 bio->bi_private = r10_bio;
3142 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003143 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003144 bio->bi_iter.bi_sector = to_addr
NeilBrownf90145f2016-06-02 16:19:52 +10003145 + mrdev->data_offset;
3146 bio->bi_bdev = mrdev->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003147 atomic_inc(&r10_bio->remaining);
3148 } else
3149 r10_bio->devs[1].bio->bi_end_io = NULL;
3150
3151 /* and maybe write to replacement */
3152 bio = r10_bio->devs[1].repl_bio;
3153 if (bio)
3154 bio->bi_end_io = NULL;
NeilBrownf90145f2016-06-02 16:19:52 +10003155 /* Note: if mreplace != NULL, then bio
NeilBrown24afd802011-12-23 10:17:55 +11003156 * cannot be NULL as r10buf_pool_alloc will
3157 * have allocated it.
3158 * So the second test here is pointless.
3159 * But it keeps semantic-checkers happy, and
3160 * this comment keeps human reviewers
3161 * happy.
3162 */
NeilBrownf90145f2016-06-02 16:19:52 +10003163 if (mreplace == NULL || bio == NULL ||
3164 test_bit(Faulty, &mreplace->flags))
NeilBrown24afd802011-12-23 10:17:55 +11003165 break;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003166 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003167 bio->bi_next = biolist;
3168 biolist = bio;
3169 bio->bi_private = r10_bio;
3170 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003171 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003172 bio->bi_iter.bi_sector = to_addr +
NeilBrownf90145f2016-06-02 16:19:52 +10003173 mreplace->data_offset;
3174 bio->bi_bdev = mreplace->bdev;
NeilBrown24afd802011-12-23 10:17:55 +11003175 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
NeilBrownf90145f2016-06-02 16:19:52 +10003178 rcu_read_unlock();
NeilBrownab9d47e2011-05-11 14:54:41 +10003179 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003180 /* Cannot recover, so abort the recovery or
3181 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003182 if (any_working) {
3183 /* problem is that there are bad blocks
3184 * on other device(s)
3185 */
3186 int k;
3187 for (k = 0; k < conf->copies; k++)
3188 if (r10_bio->devs[k].devnum == i)
3189 break;
NeilBrown24afd802011-12-23 10:17:55 +11003190 if (!test_bit(In_sync,
NeilBrownf90145f2016-06-02 16:19:52 +10003191 &mrdev->flags)
NeilBrown24afd802011-12-23 10:17:55 +11003192 && !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003193 mrdev,
NeilBrown24afd802011-12-23 10:17:55 +11003194 r10_bio->devs[k].addr,
3195 max_sync, 0))
3196 any_working = 0;
NeilBrownf90145f2016-06-02 16:19:52 +10003197 if (mreplace &&
NeilBrown24afd802011-12-23 10:17:55 +11003198 !rdev_set_badblocks(
NeilBrownf90145f2016-06-02 16:19:52 +10003199 mreplace,
NeilBrowne875ece2011-07-28 11:39:24 +10003200 r10_bio->devs[k].addr,
3201 max_sync, 0))
3202 any_working = 0;
3203 }
3204 if (!any_working) {
3205 if (!test_and_set_bit(MD_RECOVERY_INTR,
3206 &mddev->recovery))
NeilBrown08464e02016-11-02 14:16:50 +11003207 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
NeilBrowne875ece2011-07-28 11:39:24 +10003208 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003209 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003210 = mddev->recovery_disabled;
3211 }
NeilBrowne8b84912014-01-06 10:35:34 +11003212 put_buf(r10_bio);
3213 if (rb2)
3214 atomic_dec(&rb2->remaining);
3215 r10_bio = rb2;
NeilBrownf90145f2016-06-02 16:19:52 +10003216 rdev_dec_pending(mrdev, mddev);
3217 if (mreplace)
3218 rdev_dec_pending(mreplace, mddev);
NeilBrownab9d47e2011-05-11 14:54:41 +10003219 break;
3220 }
NeilBrownf90145f2016-06-02 16:19:52 +10003221 rdev_dec_pending(mrdev, mddev);
3222 if (mreplace)
3223 rdev_dec_pending(mreplace, mddev);
NeilBrown8d3ca832016-11-18 16:16:12 +11003224 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3225 /* Only want this if there is elsewhere to
3226 * read from. 'j' is currently the first
3227 * readable copy.
3228 */
3229 int targets = 1;
3230 for (; j < conf->copies; j++) {
3231 int d = r10_bio->devs[j].devnum;
3232 if (conf->mirrors[d].rdev &&
3233 test_bit(In_sync,
3234 &conf->mirrors[d].rdev->flags))
3235 targets++;
3236 }
3237 if (targets == 1)
3238 r10_bio->devs[0].bio->bi_opf
3239 &= ~MD_FAILFAST;
3240 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 if (biolist == NULL) {
3243 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003244 struct r10bio *rb2 = r10_bio;
3245 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 rb2->master_bio = NULL;
3247 put_buf(rb2);
3248 }
3249 goto giveup;
3250 }
3251 } else {
3252 /* resync. Schedule a read for every block at this virt offset */
3253 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003254
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10003255 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
NeilBrown78200d42009-02-25 13:18:47 +11003256
NeilBrown6cce3b22006-01-06 00:20:16 -08003257 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3258 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003259 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3260 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003261 /* We can skip this block */
3262 *skipped = 1;
3263 return sync_blocks + sectors_skipped;
3264 }
3265 if (sync_blocks < max_sync)
3266 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003268 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 r10_bio->mddev = mddev;
3271 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003272 raise_barrier(conf, 0);
3273 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 r10_bio->master_bio = NULL;
3276 r10_bio->sector = sector_nr;
3277 set_bit(R10BIO_IsSync, &r10_bio->state);
3278 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003279 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
NeilBrown5cf00fc2012-05-21 09:28:20 +10003281 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003283 sector_t first_bad, sector;
3284 int bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003285 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003286
NeilBrown9ad1aef2011-12-23 10:17:55 +11003287 if (r10_bio->devs[i].repl_bio)
3288 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 bio = r10_bio->devs[i].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003291 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003292 bio->bi_error = -EIO;
NeilBrownf90145f2016-06-02 16:19:52 +10003293 rcu_read_lock();
3294 rdev = rcu_dereference(conf->mirrors[d].rdev);
3295 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3296 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003298 }
NeilBrown40c356c2011-07-28 11:39:24 +10003299 sector = r10_bio->devs[i].addr;
NeilBrownf90145f2016-06-02 16:19:52 +10003300 if (is_badblock(rdev, sector, max_sync,
NeilBrown40c356c2011-07-28 11:39:24 +10003301 &first_bad, &bad_sectors)) {
3302 if (first_bad > sector)
3303 max_sync = first_bad - sector;
3304 else {
3305 bad_sectors -= (sector - first_bad);
3306 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003307 max_sync = bad_sectors;
NeilBrownf90145f2016-06-02 16:19:52 +10003308 rcu_read_unlock();
NeilBrown40c356c2011-07-28 11:39:24 +10003309 continue;
3310 }
3311 }
NeilBrownf90145f2016-06-02 16:19:52 +10003312 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 atomic_inc(&r10_bio->remaining);
3314 bio->bi_next = biolist;
3315 biolist = bio;
3316 bio->bi_private = r10_bio;
3317 bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05003318 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown8d3ca832016-11-18 16:16:12 +11003319 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
3320 bio->bi_opf |= MD_FAILFAST;
NeilBrownf90145f2016-06-02 16:19:52 +10003321 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3322 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003324
NeilBrownf90145f2016-06-02 16:19:52 +10003325 rdev = rcu_dereference(conf->mirrors[d].replacement);
3326 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3327 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003328 continue;
NeilBrownf90145f2016-06-02 16:19:52 +10003329 }
3330 atomic_inc(&rdev->nr_pending);
3331 rcu_read_unlock();
NeilBrown9ad1aef2011-12-23 10:17:55 +11003332
3333 /* Need to set up for writing to the replacement */
3334 bio = r10_bio->devs[i].repl_bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003335 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003336 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003337
3338 sector = r10_bio->devs[i].addr;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003339 bio->bi_next = biolist;
3340 biolist = bio;
3341 bio->bi_private = r10_bio;
3342 bio->bi_end_io = end_sync_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05003343 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
NeilBrownf90145f2016-06-02 16:19:52 +10003344 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3345 bio->bi_bdev = rdev->bdev;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003346 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
3348
3349 if (count < 2) {
3350 for (i=0; i<conf->copies; i++) {
3351 int d = r10_bio->devs[i].devnum;
3352 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003353 rdev_dec_pending(conf->mirrors[d].rdev,
3354 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003355 if (r10_bio->devs[i].repl_bio &&
3356 r10_bio->devs[i].repl_bio->bi_end_io)
3357 rdev_dec_pending(
3358 conf->mirrors[d].replacement,
3359 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 put_buf(r10_bio);
3362 biolist = NULL;
3363 goto giveup;
3364 }
3365 }
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003368 if (sector_nr + max_sync < max_sector)
3369 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 do {
3371 struct page *page;
3372 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 if (sector_nr + (len>>9) > max_sector)
3374 len = (max_sector - sector_nr) << 9;
3375 if (len == 0)
3376 break;
3377 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003378 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003380 if (bio_add_page(bio, page, len, 0))
3381 continue;
3382
3383 /* stop here */
3384 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3385 for (bio2 = biolist;
3386 bio2 && bio2 != bio;
3387 bio2 = bio2->bi_next) {
3388 /* remove last page from this bio */
3389 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003390 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06003391 bio_clear_flag(bio2, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003393 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
3395 nr_sectors += len>>9;
3396 sector_nr += len>>9;
3397 } while (biolist->bi_vcnt < RESYNC_PAGES);
3398 bio_full:
3399 r10_bio->sectors = nr_sectors;
3400
3401 while (biolist) {
3402 bio = biolist;
3403 biolist = biolist->bi_next;
3404
3405 bio->bi_next = NULL;
3406 r10_bio = bio->bi_private;
3407 r10_bio->sectors = nr_sectors;
3408
3409 if (bio->bi_end_io == end_sync_read) {
3410 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003411 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 generic_make_request(bio);
3413 }
3414 }
3415
NeilBrown57afd892005-06-21 17:17:13 -07003416 if (sectors_skipped)
3417 /* pretend they weren't skipped, it makes
3418 * no important difference in this case
3419 */
3420 md_done_sync(mddev, sectors_skipped, 1);
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return sectors_skipped + nr_sectors;
3423 giveup:
3424 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003425 * drives must be failed or in resync, all drives
3426 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 */
NeilBrown09b40682009-02-25 13:18:47 +11003428 if (sector_nr + max_sync < max_sector)
3429 max_sector = sector_nr + max_sync;
3430
3431 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 chunks_skipped ++;
3433 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Dan Williams80c3a6c2009-03-17 18:10:40 -07003437static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003438raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003439{
3440 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003441 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003442
3443 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003444 raid_disks = min(conf->geo.raid_disks,
3445 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003446 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003447 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003448
NeilBrown5cf00fc2012-05-21 09:28:20 +10003449 size = sectors >> conf->geo.chunk_shift;
3450 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003451 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003452 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003453
NeilBrown5cf00fc2012-05-21 09:28:20 +10003454 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003455}
3456
NeilBrown6508fdb2012-05-17 10:08:45 +10003457static void calc_sectors(struct r10conf *conf, sector_t size)
3458{
3459 /* Calculate the number of sectors-per-device that will
3460 * actually be used, and set conf->dev_sectors and
3461 * conf->stride
3462 */
3463
NeilBrown5cf00fc2012-05-21 09:28:20 +10003464 size = size >> conf->geo.chunk_shift;
3465 sector_div(size, conf->geo.far_copies);
3466 size = size * conf->geo.raid_disks;
3467 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003468 /* 'size' is now the number of chunks in the array */
3469 /* calculate "used chunks per device" */
3470 size = size * conf->copies;
3471
3472 /* We need to round up when dividing by raid_disks to
3473 * get the stride size.
3474 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003475 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003476
NeilBrown5cf00fc2012-05-21 09:28:20 +10003477 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003478
NeilBrown5cf00fc2012-05-21 09:28:20 +10003479 if (conf->geo.far_offset)
3480 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003481 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003482 sector_div(size, conf->geo.far_copies);
3483 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003484 }
3485}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003486
NeilBrowndeb200d2012-05-21 09:28:33 +10003487enum geo_type {geo_new, geo_old, geo_start};
3488static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3489{
3490 int nc, fc, fo;
3491 int layout, chunk, disks;
3492 switch (new) {
3493 case geo_old:
3494 layout = mddev->layout;
3495 chunk = mddev->chunk_sectors;
3496 disks = mddev->raid_disks - mddev->delta_disks;
3497 break;
3498 case geo_new:
3499 layout = mddev->new_layout;
3500 chunk = mddev->new_chunk_sectors;
3501 disks = mddev->raid_disks;
3502 break;
3503 default: /* avoid 'may be unused' warnings */
3504 case geo_start: /* new when starting reshape - raid_disks not
3505 * updated yet. */
3506 layout = mddev->new_layout;
3507 chunk = mddev->new_chunk_sectors;
3508 disks = mddev->raid_disks + mddev->delta_disks;
3509 break;
3510 }
NeilBrown8bce6d32015-10-22 13:20:15 +11003511 if (layout >> 19)
NeilBrowndeb200d2012-05-21 09:28:33 +10003512 return -1;
3513 if (chunk < (PAGE_SIZE >> 9) ||
3514 !is_power_of_2(chunk))
3515 return -2;
3516 nc = layout & 255;
3517 fc = (layout >> 8) & 255;
3518 fo = layout & (1<<16);
3519 geo->raid_disks = disks;
3520 geo->near_copies = nc;
3521 geo->far_copies = fc;
3522 geo->far_offset = fo;
NeilBrown8bce6d32015-10-22 13:20:15 +11003523 switch (layout >> 17) {
3524 case 0: /* original layout. simple but not always optimal */
3525 geo->far_set_size = disks;
3526 break;
3527 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3528 * actually using this, but leave code here just in case.*/
3529 geo->far_set_size = disks/fc;
3530 WARN(geo->far_set_size < fc,
3531 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3532 break;
3533 case 2: /* "improved" layout fixed to match documentation */
3534 geo->far_set_size = fc * nc;
3535 break;
3536 default: /* Not a valid layout */
3537 return -1;
3538 }
NeilBrowndeb200d2012-05-21 09:28:33 +10003539 geo->chunk_mask = chunk - 1;
3540 geo->chunk_shift = ffz(~chunk);
3541 return nc*fc;
3542}
3543
NeilBrowne879a872011-10-11 16:49:02 +11003544static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
NeilBrowne879a872011-10-11 16:49:02 +11003546 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003547 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003548 struct geom geo;
3549 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
NeilBrowndeb200d2012-05-21 09:28:33 +10003551 copies = setup_geo(&geo, mddev, geo_new);
3552
3553 if (copies == -2) {
NeilBrown08464e02016-11-02 14:16:50 +11003554 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3555 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
NeilBrown2604b702006-01-06 00:20:36 -08003558
NeilBrowndeb200d2012-05-21 09:28:33 +10003559 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown08464e02016-11-02 14:16:50 +11003560 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3561 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 goto out;
3563 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003564
3565 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003566 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003567 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003569
NeilBrown3ea7daa2012-05-22 13:53:47 +10003570 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003571 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003572 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003573 GFP_KERNEL);
3574 if (!conf->mirrors)
3575 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003576
3577 conf->tmppage = alloc_page(GFP_KERNEL);
3578 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003579 goto out;
3580
NeilBrowndeb200d2012-05-21 09:28:33 +10003581 conf->geo = geo;
3582 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003583 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3584 r10bio_pool_free, conf);
3585 if (!conf->r10bio_pool)
3586 goto out;
3587
NeilBrown6508fdb2012-05-17 10:08:45 +10003588 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003589 if (mddev->reshape_position == MaxSector) {
3590 conf->prev = conf->geo;
3591 conf->reshape_progress = MaxSector;
3592 } else {
3593 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3594 err = -EINVAL;
3595 goto out;
3596 }
3597 conf->reshape_progress = mddev->reshape_position;
3598 if (conf->prev.far_offset)
3599 conf->prev.stride = 1 << conf->prev.chunk_shift;
3600 else
3601 /* far_copies must be 1 */
3602 conf->prev.stride = conf->dev_sectors;
3603 }
NeilBrown299b0682015-07-06 17:37:49 +10003604 conf->reshape_safe = conf->reshape_progress;
Neil Browne7e72bf2008-05-14 16:05:54 -07003605 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003606 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown95af5872015-08-14 11:26:17 +10003607 INIT_LIST_HEAD(&conf->bio_end_io_list);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003608
3609 spin_lock_init(&conf->resync_lock);
3610 init_waitqueue_head(&conf->wait_barrier);
Tomasz Majchrzak0e5313e2016-06-24 14:20:16 +02003611 atomic_set(&conf->nr_pending, 0);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003612
NeilBrown02326052012-07-03 15:56:52 +10003613 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003614 if (!conf->thread)
3615 goto out;
3616
Trela, Maciejdab8b292010-03-08 16:02:45 +11003617 conf->mddev = mddev;
3618 return conf;
3619
3620 out:
Trela, Maciejdab8b292010-03-08 16:02:45 +11003621 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003622 mempool_destroy(conf->r10bio_pool);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003623 kfree(conf->mirrors);
3624 safe_put_page(conf->tmppage);
3625 kfree(conf);
3626 }
3627 return ERR_PTR(err);
3628}
3629
Shaohua Li849674e2016-01-20 13:52:20 -08003630static int raid10_run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003631{
NeilBrowne879a872011-10-11 16:49:02 +11003632 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003633 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003634 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003635 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003636 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003637 sector_t min_offset_diff = 0;
3638 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003639 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003640
3641 if (mddev->private == NULL) {
3642 conf = setup_conf(mddev);
3643 if (IS_ERR(conf))
3644 return PTR_ERR(conf);
3645 mddev->private = conf;
3646 }
3647 conf = mddev->private;
3648 if (!conf)
3649 goto out;
3650
Trela, Maciejdab8b292010-03-08 16:02:45 +11003651 mddev->thread = conf->thread;
3652 conf->thread = NULL;
3653
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003654 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003655 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003656 blk_queue_max_discard_sectors(mddev->queue,
3657 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003658 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003659 blk_queue_io_min(mddev->queue, chunk_size);
3660 if (conf->geo.raid_disks % conf->geo.near_copies)
3661 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3662 else
3663 blk_queue_io_opt(mddev->queue, chunk_size *
3664 (conf->geo.raid_disks / conf->geo.near_copies));
3665 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003666
NeilBrowndafb20f2012-03-19 12:46:39 +11003667 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003668 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003669 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003672 if (disk_idx < 0)
3673 continue;
3674 if (disk_idx >= conf->geo.raid_disks &&
3675 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 continue;
3677 disk = conf->mirrors + disk_idx;
3678
NeilBrown56a25592011-12-23 10:17:55 +11003679 if (test_bit(Replacement, &rdev->flags)) {
3680 if (disk->replacement)
3681 goto out_free_conf;
3682 disk->replacement = rdev;
3683 } else {
3684 if (disk->rdev)
3685 goto out_free_conf;
3686 disk->rdev = rdev;
3687 }
NeilBrownaba336b2012-05-31 15:39:11 +10003688 q = bdev_get_queue(rdev->bdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003689 diff = (rdev->new_data_offset - rdev->data_offset);
3690 if (!mddev->reshape_backwards)
3691 diff = -diff;
3692 if (diff < 0)
3693 diff = 0;
3694 if (first || diff < min_offset_diff)
3695 min_offset_diff = diff;
NeilBrown56a25592011-12-23 10:17:55 +11003696
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003697 if (mddev->gendisk)
3698 disk_stack_limits(mddev->gendisk, rdev->bdev,
3699 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003702
3703 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3704 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003706
Jonathan Brassowed30be02012-10-31 11:42:30 +11003707 if (mddev->queue) {
3708 if (discard_supported)
3709 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3710 mddev->queue);
3711 else
3712 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3713 mddev->queue);
3714 }
NeilBrown6d508242005-09-09 16:24:03 -07003715 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003716 if (!enough(conf, -1)) {
NeilBrown08464e02016-11-02 14:16:50 +11003717 pr_err("md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003718 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 goto out_free_conf;
3720 }
3721
NeilBrown3ea7daa2012-05-22 13:53:47 +10003722 if (conf->reshape_progress != MaxSector) {
3723 /* must ensure that shape change is supported */
3724 if (conf->geo.far_copies != 1 &&
3725 conf->geo.far_offset == 0)
3726 goto out_free_conf;
3727 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003728 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003729 goto out_free_conf;
3730 }
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003733 for (i = 0;
3734 i < conf->geo.raid_disks
3735 || i < conf->prev.raid_disks;
3736 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 disk = conf->mirrors + i;
3739
NeilBrown56a25592011-12-23 10:17:55 +11003740 if (!disk->rdev && disk->replacement) {
3741 /* The replacement is all we have - use it */
3742 disk->rdev = disk->replacement;
3743 disk->replacement = NULL;
3744 clear_bit(Replacement, &disk->rdev->flags);
3745 }
3746
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003747 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003748 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 disk->head_position = 0;
3750 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003751 if (disk->rdev &&
3752 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003753 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
NeilBrownd890fa22011-10-26 11:54:39 +11003755 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
3757
Andre Noll8c6ac862009-06-18 08:48:06 +10003758 if (mddev->recovery_cp != MaxSector)
NeilBrown08464e02016-11-02 14:16:50 +11003759 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3760 mdname(mddev));
3761 pr_info("md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003762 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3763 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 /*
3765 * Ok, everything is just fine now
3766 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003767 mddev->dev_sectors = conf->dev_sectors;
3768 size = raid10_size(mddev, 0, 0);
3769 md_set_array_sectors(mddev, size);
3770 mddev->resync_max_sectors = size;
NeilBrown46533ff2016-11-18 16:16:11 +11003771 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003773 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003774 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003775 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003776
3777 /* Calculate max read-ahead size.
3778 * We need to readahead at least twice a whole stripe....
3779 * maybe...
3780 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003781 stripe /= conf->geo.near_copies;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003782 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3783 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
Martin K. Petersena91a2782011-03-17 11:11:05 +01003786 if (md_integrity_register(mddev))
3787 goto out_free_conf;
3788
NeilBrown3ea7daa2012-05-22 13:53:47 +10003789 if (conf->reshape_progress != MaxSector) {
3790 unsigned long before_length, after_length;
3791
3792 before_length = ((1 << conf->prev.chunk_shift) *
3793 conf->prev.far_copies);
3794 after_length = ((1 << conf->geo.chunk_shift) *
3795 conf->geo.far_copies);
3796
3797 if (max(before_length, after_length) > min_offset_diff) {
3798 /* This cannot work */
NeilBrown08464e02016-11-02 14:16:50 +11003799 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
NeilBrown3ea7daa2012-05-22 13:53:47 +10003800 goto out_free_conf;
3801 }
3802 conf->offset_diff = min_offset_diff;
3803
NeilBrown3ea7daa2012-05-22 13:53:47 +10003804 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3805 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3806 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3807 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3808 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3809 "reshape");
3810 }
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return 0;
3813
3814out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003815 md_unregister_thread(&mddev->thread);
Julia Lawall644df1a2015-09-13 14:15:10 +02003816 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003817 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003818 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 kfree(conf);
3820 mddev->private = NULL;
3821out:
3822 return -EIO;
3823}
3824
NeilBrownafa0f552014-12-15 12:56:58 +11003825static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
NeilBrownafa0f552014-12-15 12:56:58 +11003827 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Julia Lawall644df1a2015-09-13 14:15:10 +02003829 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003830 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003831 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003832 kfree(conf->mirrors_old);
3833 kfree(conf->mirrors_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
NeilBrownfd01b882011-10-11 16:47:53 +11003837static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003838{
NeilBrowne879a872011-10-11 16:49:02 +11003839 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003840
3841 switch(state) {
3842 case 1:
3843 raise_barrier(conf, 0);
3844 break;
3845 case 0:
3846 lower_barrier(conf);
3847 break;
3848 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003849}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
NeilBrown006a09a2012-03-19 12:46:40 +11003851static int raid10_resize(struct mddev *mddev, sector_t sectors)
3852{
3853 /* Resize of 'far' arrays is not supported.
3854 * For 'near' and 'offset' arrays we can set the
3855 * number of sectors used to be an appropriate multiple
3856 * of the chunk size.
3857 * For 'offset', this is far_copies*chunksize.
3858 * For 'near' the multiplier is the LCM of
3859 * near_copies and raid_disks.
3860 * So if far_copies > 1 && !far_offset, fail.
3861 * Else find LCM(raid_disks, near_copy)*far_copies and
3862 * multiply by chunk_size. Then round to this number.
3863 * This is mostly done by raid10_size()
3864 */
3865 struct r10conf *conf = mddev->private;
3866 sector_t oldsize, size;
3867
NeilBrownf8c9e742012-05-21 09:28:33 +10003868 if (mddev->reshape_position != MaxSector)
3869 return -EBUSY;
3870
NeilBrown5cf00fc2012-05-21 09:28:20 +10003871 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003872 return -EINVAL;
3873
3874 oldsize = raid10_size(mddev, 0, 0);
3875 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003876 if (mddev->external_size &&
3877 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003878 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003879 if (mddev->bitmap) {
3880 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3881 if (ret)
3882 return ret;
3883 }
3884 md_set_array_sectors(mddev, size);
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02003885 if (mddev->queue) {
3886 set_capacity(mddev->gendisk, mddev->array_sectors);
3887 revalidate_disk(mddev->gendisk);
3888 }
NeilBrown006a09a2012-03-19 12:46:40 +11003889 if (sectors > mddev->dev_sectors &&
3890 mddev->recovery_cp > oldsize) {
3891 mddev->recovery_cp = oldsize;
3892 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3893 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003894 calc_sectors(conf, sectors);
3895 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003896 mddev->resync_max_sectors = size;
3897 return 0;
3898}
3899
NeilBrown53a6ab42015-02-12 14:09:57 +11003900static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003901{
NeilBrown3cb03002011-10-11 16:45:26 +11003902 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003903 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003904
3905 if (mddev->degraded > 0) {
NeilBrown08464e02016-11-02 14:16:50 +11003906 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3907 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003908 return ERR_PTR(-EINVAL);
3909 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003910 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003911
Trela, Maciejdab8b292010-03-08 16:02:45 +11003912 /* Set new parameters */
3913 mddev->new_level = 10;
3914 /* new layout: far_copies = 1, near_copies = 2 */
3915 mddev->new_layout = (1<<8) + 2;
3916 mddev->new_chunk_sectors = mddev->chunk_sectors;
3917 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003918 mddev->raid_disks *= 2;
3919 /* make sure it will be not marked as dirty */
3920 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003921 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003922
3923 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003924 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003925 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003926 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003927 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003928 rdev->sectors = size;
3929 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003930 conf->barrier = 1;
3931 }
3932
Trela, Maciejdab8b292010-03-08 16:02:45 +11003933 return conf;
3934}
3935
NeilBrownfd01b882011-10-11 16:47:53 +11003936static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003937{
NeilBrowne373ab12011-10-11 16:48:59 +11003938 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003939
3940 /* raid10 can take over:
3941 * raid0 - providing it has only two drives
3942 */
3943 if (mddev->level == 0) {
3944 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003945 raid0_conf = mddev->private;
3946 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown08464e02016-11-02 14:16:50 +11003947 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3948 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003949 return ERR_PTR(-EINVAL);
3950 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003951 return raid10_takeover_raid0(mddev,
3952 raid0_conf->strip_zone->zone_end,
3953 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003954 }
3955 return ERR_PTR(-EINVAL);
3956}
3957
NeilBrown3ea7daa2012-05-22 13:53:47 +10003958static int raid10_check_reshape(struct mddev *mddev)
3959{
3960 /* Called when there is a request to change
3961 * - layout (to ->new_layout)
3962 * - chunk size (to ->new_chunk_sectors)
3963 * - raid_disks (by delta_disks)
3964 * or when trying to restart a reshape that was ongoing.
3965 *
3966 * We need to validate the request and possibly allocate
3967 * space if that might be an issue later.
3968 *
3969 * Currently we reject any reshape of a 'far' mode array,
3970 * allow chunk size to change if new is generally acceptable,
3971 * allow raid_disks to increase, and allow
3972 * a switch between 'near' mode and 'offset' mode.
3973 */
3974 struct r10conf *conf = mddev->private;
3975 struct geom geo;
3976
3977 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3978 return -EINVAL;
3979
3980 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3981 /* mustn't change number of copies */
3982 return -EINVAL;
3983 if (geo.far_copies > 1 && !geo.far_offset)
3984 /* Cannot switch to 'far' mode */
3985 return -EINVAL;
3986
3987 if (mddev->array_sectors & geo.chunk_mask)
3988 /* not factor of array size */
3989 return -EINVAL;
3990
NeilBrown3ea7daa2012-05-22 13:53:47 +10003991 if (!enough(conf, -1))
3992 return -EINVAL;
3993
3994 kfree(conf->mirrors_new);
3995 conf->mirrors_new = NULL;
3996 if (mddev->delta_disks > 0) {
3997 /* allocate new 'mirrors' list */
3998 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003999 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004000 *(mddev->raid_disks +
4001 mddev->delta_disks),
4002 GFP_KERNEL);
4003 if (!conf->mirrors_new)
4004 return -ENOMEM;
4005 }
4006 return 0;
4007}
4008
4009/*
4010 * Need to check if array has failed when deciding whether to:
4011 * - start an array
4012 * - remove non-faulty devices
4013 * - add a spare
4014 * - allow a reshape
4015 * This determination is simple when no reshape is happening.
4016 * However if there is a reshape, we need to carefully check
4017 * both the before and after sections.
4018 * This is because some failed devices may only affect one
4019 * of the two sections, and some non-in_sync devices may
4020 * be insync in the section most affected by failed devices.
4021 */
4022static int calc_degraded(struct r10conf *conf)
4023{
4024 int degraded, degraded2;
4025 int i;
4026
4027 rcu_read_lock();
4028 degraded = 0;
4029 /* 'prev' section first */
4030 for (i = 0; i < conf->prev.raid_disks; i++) {
4031 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4032 if (!rdev || test_bit(Faulty, &rdev->flags))
4033 degraded++;
4034 else if (!test_bit(In_sync, &rdev->flags))
4035 /* When we can reduce the number of devices in
4036 * an array, this might not contribute to
4037 * 'degraded'. It does now.
4038 */
4039 degraded++;
4040 }
4041 rcu_read_unlock();
4042 if (conf->geo.raid_disks == conf->prev.raid_disks)
4043 return degraded;
4044 rcu_read_lock();
4045 degraded2 = 0;
4046 for (i = 0; i < conf->geo.raid_disks; i++) {
4047 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4048 if (!rdev || test_bit(Faulty, &rdev->flags))
4049 degraded2++;
4050 else if (!test_bit(In_sync, &rdev->flags)) {
4051 /* If reshape is increasing the number of devices,
4052 * this section has already been recovered, so
4053 * it doesn't contribute to degraded.
4054 * else it does.
4055 */
4056 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4057 degraded2++;
4058 }
4059 }
4060 rcu_read_unlock();
4061 if (degraded2 > degraded)
4062 return degraded2;
4063 return degraded;
4064}
4065
4066static int raid10_start_reshape(struct mddev *mddev)
4067{
4068 /* A 'reshape' has been requested. This commits
4069 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4070 * This also checks if there are enough spares and adds them
4071 * to the array.
4072 * We currently require enough spares to make the final
4073 * array non-degraded. We also require that the difference
4074 * between old and new data_offset - on each device - is
4075 * enough that we never risk over-writing.
4076 */
4077
4078 unsigned long before_length, after_length;
4079 sector_t min_offset_diff = 0;
4080 int first = 1;
4081 struct geom new;
4082 struct r10conf *conf = mddev->private;
4083 struct md_rdev *rdev;
4084 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004085 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004086
4087 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4088 return -EBUSY;
4089
4090 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4091 return -EINVAL;
4092
4093 before_length = ((1 << conf->prev.chunk_shift) *
4094 conf->prev.far_copies);
4095 after_length = ((1 << conf->geo.chunk_shift) *
4096 conf->geo.far_copies);
4097
4098 rdev_for_each(rdev, mddev) {
4099 if (!test_bit(In_sync, &rdev->flags)
4100 && !test_bit(Faulty, &rdev->flags))
4101 spares++;
4102 if (rdev->raid_disk >= 0) {
4103 long long diff = (rdev->new_data_offset
4104 - rdev->data_offset);
4105 if (!mddev->reshape_backwards)
4106 diff = -diff;
4107 if (diff < 0)
4108 diff = 0;
4109 if (first || diff < min_offset_diff)
4110 min_offset_diff = diff;
4111 }
4112 }
4113
4114 if (max(before_length, after_length) > min_offset_diff)
4115 return -EINVAL;
4116
4117 if (spares < mddev->delta_disks)
4118 return -EINVAL;
4119
4120 conf->offset_diff = min_offset_diff;
4121 spin_lock_irq(&conf->device_lock);
4122 if (conf->mirrors_new) {
4123 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004124 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004125 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004126 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004127 conf->mirrors_old = conf->mirrors;
4128 conf->mirrors = conf->mirrors_new;
4129 conf->mirrors_new = NULL;
4130 }
4131 setup_geo(&conf->geo, mddev, geo_start);
4132 smp_mb();
4133 if (mddev->reshape_backwards) {
4134 sector_t size = raid10_size(mddev, 0, 0);
4135 if (size < mddev->array_sectors) {
4136 spin_unlock_irq(&conf->device_lock);
NeilBrown08464e02016-11-02 14:16:50 +11004137 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4138 mdname(mddev));
NeilBrown3ea7daa2012-05-22 13:53:47 +10004139 return -EINVAL;
4140 }
4141 mddev->resync_max_sectors = size;
4142 conf->reshape_progress = size;
4143 } else
4144 conf->reshape_progress = 0;
NeilBrown299b0682015-07-06 17:37:49 +10004145 conf->reshape_safe = conf->reshape_progress;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004146 spin_unlock_irq(&conf->device_lock);
4147
NeilBrownbb63a702012-05-22 13:55:28 +10004148 if (mddev->delta_disks && mddev->bitmap) {
4149 ret = bitmap_resize(mddev->bitmap,
4150 raid10_size(mddev, 0,
4151 conf->geo.raid_disks),
4152 0, 0);
4153 if (ret)
4154 goto abort;
4155 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004156 if (mddev->delta_disks > 0) {
4157 rdev_for_each(rdev, mddev)
4158 if (rdev->raid_disk < 0 &&
4159 !test_bit(Faulty, &rdev->flags)) {
4160 if (raid10_add_disk(mddev, rdev) == 0) {
4161 if (rdev->raid_disk >=
4162 conf->prev.raid_disks)
4163 set_bit(In_sync, &rdev->flags);
4164 else
4165 rdev->recovery_offset = 0;
4166
4167 if (sysfs_link_rdev(mddev, rdev))
4168 /* Failure here is OK */;
4169 }
4170 } else if (rdev->raid_disk >= conf->prev.raid_disks
4171 && !test_bit(Faulty, &rdev->flags)) {
4172 /* This is a spare that was manually added */
4173 set_bit(In_sync, &rdev->flags);
4174 }
4175 }
4176 /* When a reshape changes the number of devices,
4177 * ->degraded is measured against the larger of the
4178 * pre and post numbers.
4179 */
4180 spin_lock_irq(&conf->device_lock);
4181 mddev->degraded = calc_degraded(conf);
4182 spin_unlock_irq(&conf->device_lock);
4183 mddev->raid_disks = conf->geo.raid_disks;
4184 mddev->reshape_position = conf->reshape_progress;
4185 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4186
4187 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4188 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004189 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004190 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4191 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4192
4193 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4194 "reshape");
4195 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004196 ret = -EAGAIN;
4197 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004198 }
4199 conf->reshape_checkpoint = jiffies;
4200 md_wakeup_thread(mddev->sync_thread);
4201 md_new_event(mddev);
4202 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004203
4204abort:
4205 mddev->recovery = 0;
4206 spin_lock_irq(&conf->device_lock);
4207 conf->geo = conf->prev;
4208 mddev->raid_disks = conf->geo.raid_disks;
4209 rdev_for_each(rdev, mddev)
4210 rdev->new_data_offset = rdev->data_offset;
4211 smp_wmb();
4212 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004213 conf->reshape_safe = MaxSector;
NeilBrownbb63a702012-05-22 13:55:28 +10004214 mddev->reshape_position = MaxSector;
4215 spin_unlock_irq(&conf->device_lock);
4216 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004217}
4218
4219/* Calculate the last device-address that could contain
4220 * any block from the chunk that includes the array-address 's'
4221 * and report the next address.
4222 * i.e. the address returned will be chunk-aligned and after
4223 * any data that is in the chunk containing 's'.
4224 */
4225static sector_t last_dev_address(sector_t s, struct geom *geo)
4226{
4227 s = (s | geo->chunk_mask) + 1;
4228 s >>= geo->chunk_shift;
4229 s *= geo->near_copies;
4230 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4231 s *= geo->far_copies;
4232 s <<= geo->chunk_shift;
4233 return s;
4234}
4235
4236/* Calculate the first device-address that could contain
4237 * any block from the chunk that includes the array-address 's'.
4238 * This too will be the start of a chunk
4239 */
4240static sector_t first_dev_address(sector_t s, struct geom *geo)
4241{
4242 s >>= geo->chunk_shift;
4243 s *= geo->near_copies;
4244 sector_div(s, geo->raid_disks);
4245 s *= geo->far_copies;
4246 s <<= geo->chunk_shift;
4247 return s;
4248}
4249
4250static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4251 int *skipped)
4252{
4253 /* We simply copy at most one chunk (smallest of old and new)
4254 * at a time, possibly less if that exceeds RESYNC_PAGES,
4255 * or we hit a bad block or something.
4256 * This might mean we pause for normal IO in the middle of
NeilBrown02ec5022015-07-06 16:33:47 +10004257 * a chunk, but that is not a problem as mddev->reshape_position
NeilBrown3ea7daa2012-05-22 13:53:47 +10004258 * can record any location.
4259 *
4260 * If we will want to write to a location that isn't
4261 * yet recorded as 'safe' (i.e. in metadata on disk) then
4262 * we need to flush all reshape requests and update the metadata.
4263 *
4264 * When reshaping forwards (e.g. to more devices), we interpret
4265 * 'safe' as the earliest block which might not have been copied
4266 * down yet. We divide this by previous stripe size and multiply
4267 * by previous stripe length to get lowest device offset that we
4268 * cannot write to yet.
4269 * We interpret 'sector_nr' as an address that we want to write to.
4270 * From this we use last_device_address() to find where we might
4271 * write to, and first_device_address on the 'safe' position.
4272 * If this 'next' write position is after the 'safe' position,
4273 * we must update the metadata to increase the 'safe' position.
4274 *
4275 * When reshaping backwards, we round in the opposite direction
4276 * and perform the reverse test: next write position must not be
4277 * less than current safe position.
4278 *
4279 * In all this the minimum difference in data offsets
4280 * (conf->offset_diff - always positive) allows a bit of slack,
NeilBrown02ec5022015-07-06 16:33:47 +10004281 * so next can be after 'safe', but not by more than offset_diff
NeilBrown3ea7daa2012-05-22 13:53:47 +10004282 *
4283 * We need to prepare all the bios here before we start any IO
4284 * to ensure the size we choose is acceptable to all devices.
4285 * The means one for each copy for write-out and an extra one for
4286 * read-in.
4287 * We store the read-in bio in ->master_bio and the others in
4288 * ->devs[x].bio and ->devs[x].repl_bio.
4289 */
4290 struct r10conf *conf = mddev->private;
4291 struct r10bio *r10_bio;
4292 sector_t next, safe, last;
4293 int max_sectors;
4294 int nr_sectors;
4295 int s;
4296 struct md_rdev *rdev;
4297 int need_flush = 0;
4298 struct bio *blist;
4299 struct bio *bio, *read_bio;
4300 int sectors_done = 0;
4301
4302 if (sector_nr == 0) {
4303 /* If restarting in the middle, skip the initial sectors */
4304 if (mddev->reshape_backwards &&
4305 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4306 sector_nr = (raid10_size(mddev, 0, 0)
4307 - conf->reshape_progress);
4308 } else if (!mddev->reshape_backwards &&
4309 conf->reshape_progress > 0)
4310 sector_nr = conf->reshape_progress;
4311 if (sector_nr) {
4312 mddev->curr_resync_completed = sector_nr;
4313 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4314 *skipped = 1;
4315 return sector_nr;
4316 }
4317 }
4318
4319 /* We don't use sector_nr to track where we are up to
4320 * as that doesn't work well for ->reshape_backwards.
4321 * So just use ->reshape_progress.
4322 */
4323 if (mddev->reshape_backwards) {
4324 /* 'next' is the earliest device address that we might
4325 * write to for this chunk in the new layout
4326 */
4327 next = first_dev_address(conf->reshape_progress - 1,
4328 &conf->geo);
4329
4330 /* 'safe' is the last device address that we might read from
4331 * in the old layout after a restart
4332 */
4333 safe = last_dev_address(conf->reshape_safe - 1,
4334 &conf->prev);
4335
4336 if (next + conf->offset_diff < safe)
4337 need_flush = 1;
4338
4339 last = conf->reshape_progress - 1;
4340 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4341 & conf->prev.chunk_mask);
4342 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4343 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4344 } else {
4345 /* 'next' is after the last device address that we
4346 * might write to for this chunk in the new layout
4347 */
4348 next = last_dev_address(conf->reshape_progress, &conf->geo);
4349
4350 /* 'safe' is the earliest device address that we might
4351 * read from in the old layout after a restart
4352 */
4353 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4354
4355 /* Need to update metadata if 'next' might be beyond 'safe'
4356 * as that would possibly corrupt data
4357 */
4358 if (next > safe + conf->offset_diff)
4359 need_flush = 1;
4360
4361 sector_nr = conf->reshape_progress;
4362 last = sector_nr | (conf->geo.chunk_mask
4363 & conf->prev.chunk_mask);
4364
4365 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4366 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4367 }
4368
4369 if (need_flush ||
4370 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4371 /* Need to update reshape_position in metadata */
4372 wait_barrier(conf);
4373 mddev->reshape_position = conf->reshape_progress;
4374 if (mddev->reshape_backwards)
4375 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4376 - conf->reshape_progress;
4377 else
4378 mddev->curr_resync_completed = conf->reshape_progress;
4379 conf->reshape_checkpoint = jiffies;
4380 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4381 md_wakeup_thread(mddev->thread);
4382 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004383 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4384 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4385 allow_barrier(conf);
4386 return sectors_done;
4387 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004388 conf->reshape_safe = mddev->reshape_position;
4389 allow_barrier(conf);
4390 }
4391
4392read_more:
4393 /* Now schedule reads for blocks from sector_nr to last */
4394 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004395 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004396 raise_barrier(conf, sectors_done != 0);
4397 atomic_set(&r10_bio->remaining, 0);
4398 r10_bio->mddev = mddev;
4399 r10_bio->sector = sector_nr;
4400 set_bit(R10BIO_IsReshape, &r10_bio->state);
4401 r10_bio->sectors = last - sector_nr + 1;
4402 rdev = read_balance(conf, r10_bio, &max_sectors);
4403 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4404
4405 if (!rdev) {
4406 /* Cannot read from here, so need to record bad blocks
4407 * on all the target devices.
4408 */
4409 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004410 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004411 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4412 return sectors_done;
4413 }
4414
4415 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4416
4417 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004418 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004419 + rdev->data_offset);
4420 read_bio->bi_private = r10_bio;
4421 read_bio->bi_end_io = end_sync_read;
Mike Christie796a5cf2016-06-05 14:32:07 -05004422 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
NeilBrownce0b0a42014-08-18 13:56:38 +10004423 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004424 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004425 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004426 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004427 r10_bio->master_bio = read_bio;
4428 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4429
4430 /* Now find the locations in the new layout */
4431 __raid10_find_phys(&conf->geo, r10_bio);
4432
4433 blist = read_bio;
4434 read_bio->bi_next = NULL;
4435
NeilBrownd094d682016-06-02 16:19:52 +10004436 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004437 for (s = 0; s < conf->copies*2; s++) {
4438 struct bio *b;
4439 int d = r10_bio->devs[s/2].devnum;
4440 struct md_rdev *rdev2;
4441 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004442 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004443 b = r10_bio->devs[s/2].repl_bio;
4444 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004445 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004446 b = r10_bio->devs[s/2].bio;
4447 }
4448 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4449 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004450
4451 bio_reset(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004452 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004453 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4454 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004455 b->bi_private = r10_bio;
4456 b->bi_end_io = end_reshape_write;
Mike Christie796a5cf2016-06-05 14:32:07 -05004457 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004458 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004459 blist = b;
4460 }
4461
4462 /* Now add as many pages as possible to all of these bios. */
4463
4464 nr_sectors = 0;
4465 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4466 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4467 int len = (max_sectors - s) << 9;
4468 if (len > PAGE_SIZE)
4469 len = PAGE_SIZE;
4470 for (bio = blist; bio ; bio = bio->bi_next) {
4471 struct bio *bio2;
4472 if (bio_add_page(bio, page, len, 0))
4473 continue;
4474
4475 /* Didn't fit, must stop */
4476 for (bio2 = blist;
4477 bio2 && bio2 != bio;
4478 bio2 = bio2->bi_next) {
4479 /* Remove last page from this bio */
4480 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004481 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004482 bio_clear_flag(bio2, BIO_SEG_VALID);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004483 }
4484 goto bio_full;
4485 }
4486 sector_nr += len >> 9;
4487 nr_sectors += len >> 9;
4488 }
4489bio_full:
NeilBrownd094d682016-06-02 16:19:52 +10004490 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004491 r10_bio->sectors = nr_sectors;
4492
4493 /* Now submit the read */
4494 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4495 atomic_inc(&r10_bio->remaining);
4496 read_bio->bi_next = NULL;
4497 generic_make_request(read_bio);
4498 sector_nr += nr_sectors;
4499 sectors_done += nr_sectors;
4500 if (sector_nr <= last)
4501 goto read_more;
4502
4503 /* Now that we have done the whole section we can
4504 * update reshape_progress
4505 */
4506 if (mddev->reshape_backwards)
4507 conf->reshape_progress -= sectors_done;
4508 else
4509 conf->reshape_progress += sectors_done;
4510
4511 return sectors_done;
4512}
4513
4514static void end_reshape_request(struct r10bio *r10_bio);
4515static int handle_reshape_read_error(struct mddev *mddev,
4516 struct r10bio *r10_bio);
4517static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4518{
4519 /* Reshape read completed. Hopefully we have a block
4520 * to write out.
4521 * If we got a read error then we do sync 1-page reads from
4522 * elsewhere until we find the data - or give up.
4523 */
4524 struct r10conf *conf = mddev->private;
4525 int s;
4526
4527 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4528 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4529 /* Reshape has been aborted */
4530 md_done_sync(mddev, r10_bio->sectors, 0);
4531 return;
4532 }
4533
4534 /* We definitely have the data in the pages, schedule the
4535 * writes.
4536 */
4537 atomic_set(&r10_bio->remaining, 1);
4538 for (s = 0; s < conf->copies*2; s++) {
4539 struct bio *b;
4540 int d = r10_bio->devs[s/2].devnum;
4541 struct md_rdev *rdev;
NeilBrownd094d682016-06-02 16:19:52 +10004542 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004543 if (s&1) {
NeilBrownd094d682016-06-02 16:19:52 +10004544 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004545 b = r10_bio->devs[s/2].repl_bio;
4546 } else {
NeilBrownd094d682016-06-02 16:19:52 +10004547 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004548 b = r10_bio->devs[s/2].bio;
4549 }
NeilBrownd094d682016-06-02 16:19:52 +10004550 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4551 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004552 continue;
NeilBrownd094d682016-06-02 16:19:52 +10004553 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004554 atomic_inc(&rdev->nr_pending);
NeilBrownd094d682016-06-02 16:19:52 +10004555 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004556 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4557 atomic_inc(&r10_bio->remaining);
4558 b->bi_next = NULL;
4559 generic_make_request(b);
4560 }
4561 end_reshape_request(r10_bio);
4562}
4563
4564static void end_reshape(struct r10conf *conf)
4565{
4566 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4567 return;
4568
4569 spin_lock_irq(&conf->device_lock);
4570 conf->prev = conf->geo;
4571 md_finish_reshape(conf->mddev);
4572 smp_wmb();
4573 conf->reshape_progress = MaxSector;
NeilBrown299b0682015-07-06 17:37:49 +10004574 conf->reshape_safe = MaxSector;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004575 spin_unlock_irq(&conf->device_lock);
4576
4577 /* read-ahead size must cover two whole stripes, which is
4578 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4579 */
4580 if (conf->mddev->queue) {
4581 int stripe = conf->geo.raid_disks *
4582 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4583 stripe /= conf->geo.near_copies;
4584 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4585 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4586 }
4587 conf->fullsync = 0;
4588}
4589
NeilBrown3ea7daa2012-05-22 13:53:47 +10004590static int handle_reshape_read_error(struct mddev *mddev,
4591 struct r10bio *r10_bio)
4592{
4593 /* Use sync reads to get the blocks from somewhere else */
4594 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004595 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004596 struct {
4597 struct r10bio r10_bio;
4598 struct r10dev devs[conf->copies];
4599 } on_stack;
4600 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004601 int slot = 0;
4602 int idx = 0;
4603 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4604
NeilBrowne0ee7782012-08-18 09:51:42 +10004605 r10b->sector = r10_bio->sector;
4606 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004607
4608 while (sectors) {
4609 int s = sectors;
4610 int success = 0;
4611 int first_slot = slot;
4612
4613 if (s > (PAGE_SIZE >> 9))
4614 s = PAGE_SIZE >> 9;
4615
NeilBrownd094d682016-06-02 16:19:52 +10004616 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004617 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004618 int d = r10b->devs[slot].devnum;
NeilBrownd094d682016-06-02 16:19:52 +10004619 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004620 sector_t addr;
4621 if (rdev == NULL ||
4622 test_bit(Faulty, &rdev->flags) ||
4623 !test_bit(In_sync, &rdev->flags))
4624 goto failed;
4625
NeilBrowne0ee7782012-08-18 09:51:42 +10004626 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrownd094d682016-06-02 16:19:52 +10004627 atomic_inc(&rdev->nr_pending);
4628 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004629 success = sync_page_io(rdev,
4630 addr,
4631 s << 9,
4632 bvec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05004633 REQ_OP_READ, 0, false);
NeilBrownd094d682016-06-02 16:19:52 +10004634 rdev_dec_pending(rdev, mddev);
4635 rcu_read_lock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004636 if (success)
4637 break;
4638 failed:
4639 slot++;
4640 if (slot >= conf->copies)
4641 slot = 0;
4642 if (slot == first_slot)
4643 break;
4644 }
NeilBrownd094d682016-06-02 16:19:52 +10004645 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004646 if (!success) {
4647 /* couldn't read this block, must give up */
4648 set_bit(MD_RECOVERY_INTR,
4649 &mddev->recovery);
4650 return -EIO;
4651 }
4652 sectors -= s;
4653 idx++;
4654 }
4655 return 0;
4656}
4657
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004658static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004659{
NeilBrown3ea7daa2012-05-22 13:53:47 +10004660 struct r10bio *r10_bio = bio->bi_private;
4661 struct mddev *mddev = r10_bio->mddev;
4662 struct r10conf *conf = mddev->private;
4663 int d;
4664 int slot;
4665 int repl;
4666 struct md_rdev *rdev = NULL;
4667
4668 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4669 if (repl)
4670 rdev = conf->mirrors[d].replacement;
4671 if (!rdev) {
4672 smp_mb();
4673 rdev = conf->mirrors[d].rdev;
4674 }
4675
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004676 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004677 /* FIXME should record badblock */
4678 md_error(mddev, rdev);
4679 }
4680
4681 rdev_dec_pending(rdev, mddev);
4682 end_reshape_request(r10_bio);
4683}
4684
4685static void end_reshape_request(struct r10bio *r10_bio)
4686{
4687 if (!atomic_dec_and_test(&r10_bio->remaining))
4688 return;
4689 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4690 bio_put(r10_bio->master_bio);
4691 put_buf(r10_bio);
4692}
4693
4694static void raid10_finish_reshape(struct mddev *mddev)
4695{
4696 struct r10conf *conf = mddev->private;
4697
4698 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4699 return;
4700
4701 if (mddev->delta_disks > 0) {
4702 sector_t size = raid10_size(mddev, 0, 0);
4703 md_set_array_sectors(mddev, size);
4704 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4705 mddev->recovery_cp = mddev->resync_max_sectors;
4706 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4707 }
4708 mddev->resync_max_sectors = size;
Heinz Mauelshagen859644f2016-05-03 19:43:24 +02004709 if (mddev->queue) {
4710 set_capacity(mddev->gendisk, mddev->array_sectors);
4711 revalidate_disk(mddev->gendisk);
4712 }
NeilBrown63aced62012-05-22 13:55:33 +10004713 } else {
4714 int d;
NeilBrownd094d682016-06-02 16:19:52 +10004715 rcu_read_lock();
NeilBrown63aced62012-05-22 13:55:33 +10004716 for (d = conf->geo.raid_disks ;
4717 d < conf->geo.raid_disks - mddev->delta_disks;
4718 d++) {
NeilBrownd094d682016-06-02 16:19:52 +10004719 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown63aced62012-05-22 13:55:33 +10004720 if (rdev)
4721 clear_bit(In_sync, &rdev->flags);
NeilBrownd094d682016-06-02 16:19:52 +10004722 rdev = rcu_dereference(conf->mirrors[d].replacement);
NeilBrown63aced62012-05-22 13:55:33 +10004723 if (rdev)
4724 clear_bit(In_sync, &rdev->flags);
4725 }
NeilBrownd094d682016-06-02 16:19:52 +10004726 rcu_read_unlock();
NeilBrown3ea7daa2012-05-22 13:53:47 +10004727 }
4728 mddev->layout = mddev->new_layout;
4729 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4730 mddev->reshape_position = MaxSector;
4731 mddev->delta_disks = 0;
4732 mddev->reshape_backwards = 0;
4733}
4734
NeilBrown84fc4b52011-10-11 16:49:58 +11004735static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
4737 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004738 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08004740 .make_request = raid10_make_request,
4741 .run = raid10_run,
NeilBrownafa0f552014-12-15 12:56:58 +11004742 .free = raid10_free,
Shaohua Li849674e2016-01-20 13:52:20 -08004743 .status = raid10_status,
4744 .error_handler = raid10_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 .hot_add_disk = raid10_add_disk,
4746 .hot_remove_disk= raid10_remove_disk,
4747 .spare_active = raid10_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08004748 .sync_request = raid10_sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004749 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004750 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004751 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004752 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004753 .check_reshape = raid10_check_reshape,
4754 .start_reshape = raid10_start_reshape,
4755 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004756 .congested = raid10_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757};
4758
4759static int __init raid_init(void)
4760{
NeilBrown2604b702006-01-06 00:20:36 -08004761 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
4764static void raid_exit(void)
4765{
NeilBrown2604b702006-01-06 00:20:36 -08004766 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
4769module_init(raid_init);
4770module_exit(raid_exit);
4771MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004772MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004774MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004775MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004776
4777module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);