blob: 316ff6f611e96c2d97feac3322a223e36ce7f23a [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>
NeilBrown43b2e5d2009-03-31 14:33:13 +110028#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110029#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110030#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110031#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * RAID10 provides a combination of RAID0 and RAID1 functionality.
35 * The layout of data is defined by
36 * chunk_size
37 * raid_disks
38 * near_copies (stored in low byte of layout)
39 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070040 * far_offset (stored in bit 16 of layout )
Jonathan Brassow475901a2013-02-21 13:28:10 +110041 * use_far_sets (stored in bit 17 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
Jonathan Brassow475901a2013-02-21 13:28:10 +110043 * The data to be stored is divided into chunks using chunksize. Each device
44 * is divided into far_copies sections. In each section, chunks are laid out
45 * in a style similar to raid0, but near_copies copies of each chunk is stored
46 * (each on a different drive). The starting device for each section is offset
47 * near_copies from the starting device of the previous section. Thus there
48 * are (near_copies * far_copies) of each chunk, and each is on a different
49 * drive. near_copies and far_copies must be at least one, and their product
50 * is at most raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070051 *
52 * If far_offset is true, then the far_copies are handled a bit differently.
Jonathan Brassow475901a2013-02-21 13:28:10 +110053 * The copies are still in different stripes, but instead of being very far
54 * apart on disk, there are adjacent stripes.
55 *
56 * The far and offset algorithms are handled slightly differently if
57 * 'use_far_sets' is true. In this case, the array's devices are grouped into
58 * sets that are (near_copies * far_copies) in size. The far copied stripes
59 * are still shifted by 'near_copies' devices, but this shifting stays confined
60 * to the set rather than the entire array. This is done to improve the number
61 * of device combinations that can fail without causing the array to fail.
62 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
63 * on a device):
64 * A B C D A B C D E
65 * ... ...
66 * D A B C E A B C D
67 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
68 * [A B] [C D] [A B] [C D E]
69 * |...| |...| |...| | ... |
70 * [B A] [D C] [B A] [E C D]
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
73/*
74 * Number of guaranteed r10bios in case of extreme VM load:
75 */
76#define NR_RAID10_BIOS 256
77
Jonathan Brassow473e87c2012-07-31 10:03:52 +100078/* when we get a read error on a read-only array, we redirect to another
79 * device without failing the first device, or trying to over-write to
80 * correct the read error. To keep track of bad blocks on a per-bio
81 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
82 */
83#define IO_BLOCKED ((struct bio *)1)
84/* When we successfully write to a known bad-block, we need to remove the
85 * bad-block marking which must be done from process context. So we record
86 * the success by setting devs[n].bio to IO_MADE_GOOD
87 */
88#define IO_MADE_GOOD ((struct bio *)2)
89
90#define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
91
92/* When there are this many requests queued to be written by
NeilBrown34db0cd2011-10-11 16:50:01 +110093 * the raid10 thread, we become 'congested' to provide back-pressure
94 * for writeback.
95 */
96static int max_queued_requests = 1024;
97
NeilBrowne879a872011-10-11 16:49:02 +110098static void allow_barrier(struct r10conf *conf);
99static void lower_barrier(struct r10conf *conf);
NeilBrown635f6412013-06-11 14:57:09 +1000100static int _enough(struct r10conf *conf, int previous, int ignore);
NeilBrown3ea7daa2012-05-22 13:53:47 +1000101static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
102 int *skipped);
103static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200104static void end_reshape_write(struct bio *bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +1000105static void end_reshape(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -0800106
Al Virodd0fc662005-10-07 07:46:04 +0100107static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
NeilBrowne879a872011-10-11 16:49:02 +1100109 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100110 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
NeilBrown69335ef2011-12-23 10:17:54 +1100112 /* allocate a r10bio with room for raid_disks entries in the
113 * bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100114 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void r10bio_pool_free(void *r10_bio, void *data)
118{
119 kfree(r10_bio);
120}
121
NeilBrown0310fa22008-08-05 15:54:14 +1000122/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +1000125/* amount of memory to reserve for resync requests */
126#define RESYNC_WINDOW (1024*1024)
127/* maximum number of concurrent requests, memory permitting */
128#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * When performing a resync, we need to read and compare, so
132 * we need as many pages are there are copies.
133 * When performing a recovery, we need 2 bios, one for read,
134 * one for write (we recover only one drive per r10buf)
135 *
136 */
Al Virodd0fc662005-10-07 07:46:04 +0100137static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
NeilBrowne879a872011-10-11 16:49:02 +1100139 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100141 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct bio *bio;
143 int i, j;
144 int nalloc;
145
146 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100147 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
NeilBrown3ea7daa2012-05-22 13:53:47 +1000150 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
151 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 nalloc = conf->copies; /* resync */
153 else
154 nalloc = 2; /* recovery */
155
156 /*
157 * Allocate bios.
158 */
159 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100160 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!bio)
162 goto out_free_bio;
163 r10_bio->devs[j].bio = bio;
NeilBrown69335ef2011-12-23 10:17:54 +1100164 if (!conf->have_replacement)
165 continue;
166 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
167 if (!bio)
168 goto out_free_bio;
169 r10_bio->devs[j].repl_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 /*
172 * Allocate RESYNC_PAGES data pages and attach them
173 * where needed.
174 */
175 for (j = 0 ; j < nalloc; j++) {
NeilBrown69335ef2011-12-23 10:17:54 +1100176 struct bio *rbio = r10_bio->devs[j].repl_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 bio = r10_bio->devs[j].bio;
178 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown3ea7daa2012-05-22 13:53:47 +1000179 if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
180 &conf->mddev->recovery)) {
181 /* we can share bv_page's during recovery
182 * and reshape */
Namhyung Kimc65060a2011-07-18 17:38:49 +1000183 struct bio *rbio = r10_bio->devs[0].bio;
184 page = rbio->bi_io_vec[i].bv_page;
185 get_page(page);
186 } else
187 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (unlikely(!page))
189 goto out_free_pages;
190
191 bio->bi_io_vec[i].bv_page = page;
NeilBrown69335ef2011-12-23 10:17:54 +1100192 if (rbio)
193 rbio->bi_io_vec[i].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 }
196
197 return r10_bio;
198
199out_free_pages:
200 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800201 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 while (j--)
203 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800204 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
majianpeng5fdd2cf2012-05-22 13:55:03 +1000205 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206out_free_bio:
majianpeng5fdd2cf2012-05-22 13:55:03 +1000207 for ( ; j < nalloc; j++) {
208 if (r10_bio->devs[j].bio)
209 bio_put(r10_bio->devs[j].bio);
NeilBrown69335ef2011-12-23 10:17:54 +1100210 if (r10_bio->devs[j].repl_bio)
211 bio_put(r10_bio->devs[j].repl_bio);
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 r10bio_pool_free(r10_bio, conf);
214 return NULL;
215}
216
217static void r10buf_pool_free(void *__r10_bio, void *data)
218{
219 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100220 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100221 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int j;
223
224 for (j=0; j < conf->copies; j++) {
225 struct bio *bio = r10bio->devs[j].bio;
226 if (bio) {
227 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800228 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 bio->bi_io_vec[i].bv_page = NULL;
230 }
231 bio_put(bio);
232 }
NeilBrown69335ef2011-12-23 10:17:54 +1100233 bio = r10bio->devs[j].repl_bio;
234 if (bio)
235 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 r10bio_pool_free(r10bio, conf);
238}
239
NeilBrowne879a872011-10-11 16:49:02 +1100240static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int i;
243
244 for (i = 0; i < conf->copies; i++) {
245 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000246 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 bio_put(*bio);
248 *bio = NULL;
NeilBrown69335ef2011-12-23 10:17:54 +1100249 bio = &r10_bio->devs[i].repl_bio;
250 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
251 bio_put(*bio);
252 *bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
255
NeilBrown9f2c9d12011-10-11 16:48:43 +1100256static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
NeilBrowne879a872011-10-11 16:49:02 +1100258 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 put_all_bios(conf, r10_bio);
261 mempool_free(r10_bio, conf->r10bio_pool);
262}
263
NeilBrown9f2c9d12011-10-11 16:48:43 +1100264static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
NeilBrowne879a872011-10-11 16:49:02 +1100266 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 mempool_free(r10_bio, conf->r10buf_pool);
269
NeilBrown0a27ec92006-01-06 00:20:13 -0800270 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
NeilBrown9f2c9d12011-10-11 16:48:43 +1100273static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100276 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100277 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 spin_lock_irqsave(&conf->device_lock, flags);
280 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800281 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 spin_unlock_irqrestore(&conf->device_lock, flags);
283
Arthur Jones388667b2008-07-25 12:03:38 -0700284 /* wake up frozen array... */
285 wake_up(&conf->wait_barrier);
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 md_wakeup_thread(mddev->thread);
288}
289
290/*
291 * raid_end_bio_io() is called when we have finished servicing a mirrored
292 * operation and are ready to return a success/failure code to the buffer
293 * cache layer.
294 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100295static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000298 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100299 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrown856e08e2011-07-28 11:39:23 +1000301 if (bio->bi_phys_segments) {
302 unsigned long flags;
303 spin_lock_irqsave(&conf->device_lock, flags);
304 bio->bi_phys_segments--;
305 done = (bio->bi_phys_segments == 0);
306 spin_unlock_irqrestore(&conf->device_lock, flags);
307 } else
308 done = 1;
309 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200310 bio->bi_error = -EIO;
NeilBrown856e08e2011-07-28 11:39:23 +1000311 if (done) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200312 bio_endio(bio);
NeilBrown856e08e2011-07-28 11:39:23 +1000313 /*
314 * Wake up any possible resync thread that waits for the device
315 * to go idle.
316 */
317 allow_barrier(conf);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 free_r10bio(r10_bio);
320}
321
322/*
323 * Update disk head position estimator based on IRQ completion info.
324 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100325static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
NeilBrowne879a872011-10-11 16:49:02 +1100327 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
330 r10_bio->devs[slot].addr + (r10_bio->sectors);
331}
332
Namhyung Kim778ca012011-07-18 17:38:47 +1000333/*
334 * Find the disk number which triggered given bio
335 */
NeilBrowne879a872011-10-11 16:49:02 +1100336static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown69335ef2011-12-23 10:17:54 +1100337 struct bio *bio, int *slotp, int *replp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000338{
339 int slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100340 int repl = 0;
Namhyung Kim778ca012011-07-18 17:38:47 +1000341
NeilBrown69335ef2011-12-23 10:17:54 +1100342 for (slot = 0; slot < conf->copies; slot++) {
Namhyung Kim778ca012011-07-18 17:38:47 +1000343 if (r10_bio->devs[slot].bio == bio)
344 break;
NeilBrown69335ef2011-12-23 10:17:54 +1100345 if (r10_bio->devs[slot].repl_bio == bio) {
346 repl = 1;
347 break;
348 }
349 }
Namhyung Kim778ca012011-07-18 17:38:47 +1000350
351 BUG_ON(slot == conf->copies);
352 update_head_pos(slot, r10_bio);
353
NeilBrown749c55e2011-07-28 11:39:24 +1000354 if (slotp)
355 *slotp = slot;
NeilBrown69335ef2011-12-23 10:17:54 +1100356 if (replp)
357 *replp = repl;
Namhyung Kim778ca012011-07-18 17:38:47 +1000358 return r10_bio->devs[slot].devnum;
359}
360
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200361static void raid10_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200363 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100364 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int slot, dev;
NeilBrownabbf0982011-12-23 10:17:54 +1100366 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +1100367 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 slot = r10_bio->read_slot;
370 dev = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100371 rdev = r10_bio->devs[slot].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * this branch is our 'one mirror IO has finished' event handler:
374 */
NeilBrown4443ae12006-01-06 00:20:28 -0800375 update_head_pos(slot, r10_bio);
376
377 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Set R10BIO_Uptodate in our master bio, so that
380 * we will return a good error code to the higher
381 * levels even if IO on some other mirrored buffer fails.
382 *
383 * The 'master' represents the composite IO operation to
384 * user-side. So if something waits for IO, then it will
385 * wait for the 'master' bio.
386 */
387 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrownfae8cc52012-02-14 11:10:10 +1100388 } else {
389 /* If all other devices that store this block have
390 * failed, we want to return the error upwards rather
391 * than fail the last device. Here we redefine
392 * "uptodate" to mean "Don't want to retry"
393 */
NeilBrown635f6412013-06-11 14:57:09 +1000394 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
395 rdev->raid_disk))
NeilBrownfae8cc52012-02-14 11:10:10 +1100396 uptodate = 1;
NeilBrownfae8cc52012-02-14 11:10:10 +1100397 }
398 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 raid_end_bio_io(r10_bio);
NeilBrownabbf0982011-12-23 10:17:54 +1100400 rdev_dec_pending(rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800401 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000403 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
405 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000406 printk_ratelimited(KERN_ERR
407 "md/raid10:%s: %s: rescheduling sector %llu\n",
408 mdname(conf->mddev),
NeilBrownabbf0982011-12-23 10:17:54 +1100409 bdevname(rdev->bdev, b),
Christian Dietrich8bda4702011-07-27 11:00:36 +1000410 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000411 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 reschedule_retry(r10_bio);
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
NeilBrown9f2c9d12011-10-11 16:48:43 +1100416static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000417{
418 /* clear the bitmap if all writes complete successfully */
419 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
420 r10_bio->sectors,
421 !test_bit(R10BIO_Degraded, &r10_bio->state),
422 0);
423 md_write_end(r10_bio->mddev);
424}
425
NeilBrown9f2c9d12011-10-11 16:48:43 +1100426static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000427{
428 if (atomic_dec_and_test(&r10_bio->remaining)) {
429 if (test_bit(R10BIO_WriteError, &r10_bio->state))
430 reschedule_retry(r10_bio);
431 else {
432 close_write(r10_bio);
433 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
434 reschedule_retry(r10_bio);
435 else
436 raid_end_bio_io(r10_bio);
437 }
438 }
439}
440
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200441static void raid10_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100443 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000444 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000445 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100446 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown475b0322011-12-23 10:17:55 +1100447 int slot, repl;
NeilBrown4ca40c22011-12-23 10:17:55 +1100448 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
NeilBrown475b0322011-12-23 10:17:55 +1100450 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
NeilBrown475b0322011-12-23 10:17:55 +1100452 if (repl)
453 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +1100454 if (!rdev) {
455 smp_rmb();
456 repl = 0;
NeilBrown475b0322011-12-23 10:17:55 +1100457 rdev = conf->mirrors[dev].rdev;
NeilBrown4ca40c22011-12-23 10:17:55 +1100458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /*
460 * this branch is our 'one mirror IO has finished' event handler:
461 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200462 if (bio->bi_error) {
NeilBrown475b0322011-12-23 10:17:55 +1100463 if (repl)
464 /* Never record new bad blocks to replacement,
465 * just fail it.
466 */
467 md_error(rdev->mddev, rdev);
468 else {
469 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +1100470 if (!test_and_set_bit(WantReplacement, &rdev->flags))
471 set_bit(MD_RECOVERY_NEEDED,
472 &rdev->mddev->recovery);
NeilBrown475b0322011-12-23 10:17:55 +1100473 set_bit(R10BIO_WriteError, &r10_bio->state);
474 dec_rdev = 0;
475 }
NeilBrown749c55e2011-07-28 11:39:24 +1000476 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
478 * Set R10BIO_Uptodate in our master bio, so that
479 * we will return a good error code for to the higher
480 * levels even if IO on some other mirrored buffer fails.
481 *
482 * The 'master' represents the composite IO operation to
483 * user-side. So if something waits for IO, then it will
484 * wait for the 'master' bio.
485 */
NeilBrown749c55e2011-07-28 11:39:24 +1000486 sector_t first_bad;
487 int bad_sectors;
488
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300489 /*
490 * Do not set R10BIO_Uptodate if the current device is
491 * rebuilding or Faulty. This is because we cannot use
492 * such device for properly reading the data back (we could
493 * potentially use it, if the current write would have felt
494 * before rdev->recovery_offset, but for simplicity we don't
495 * check this here.
496 */
497 if (test_bit(In_sync, &rdev->flags) &&
498 !test_bit(Faulty, &rdev->flags))
499 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
NeilBrown749c55e2011-07-28 11:39:24 +1000501 /* Maybe we can clear some bad blocks. */
NeilBrown475b0322011-12-23 10:17:55 +1100502 if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +1000503 r10_bio->devs[slot].addr,
504 r10_bio->sectors,
505 &first_bad, &bad_sectors)) {
506 bio_put(bio);
NeilBrown475b0322011-12-23 10:17:55 +1100507 if (repl)
508 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
509 else
510 r10_bio->devs[slot].bio = IO_MADE_GOOD;
NeilBrown749c55e2011-07-28 11:39:24 +1000511 dec_rdev = 0;
512 set_bit(R10BIO_MadeGood, &r10_bio->state);
513 }
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 *
518 * Let's see if all mirrored write operations have finished
519 * already.
520 */
NeilBrown19d5f832011-09-10 17:21:17 +1000521 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000522 if (dec_rdev)
NeilBrown884162d2012-11-22 15:12:09 +1100523 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*
527 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300528 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * parameters: near_copies and far_copies.
530 * near_copies * far_copies must be <= raid_disks.
531 * Normally one of these will be 1.
532 * If both are 1, we get raid0.
533 * If near_copies == raid_disks, we get raid1.
534 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300535 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * first chunk, followed by near_copies copies of the next chunk and
537 * so on.
538 * If far_copies > 1, then after 1/far_copies of the array has been assigned
539 * as described above, we start again with a device offset of near_copies.
540 * So we effectively have another copy of the whole array further down all
541 * the drives, but with blocks on different drives.
542 * With this layout, and block is never stored twice on the one device.
543 *
544 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700545 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *
547 * raid10_find_virt does the reverse mapping, from a device and a
548 * sector offset to a virtual address
549 */
550
NeilBrownf8c9e742012-05-21 09:28:33 +1000551static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 int n,f;
554 sector_t sector;
555 sector_t chunk;
556 sector_t stripe;
557 int dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int slot = 0;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100559 int last_far_set_start, last_far_set_size;
560
561 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
562 last_far_set_start *= geo->far_set_size;
563
564 last_far_set_size = geo->far_set_size;
565 last_far_set_size += (geo->raid_disks % geo->far_set_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* now calculate first sector/dev */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000568 chunk = r10bio->sector >> geo->chunk_shift;
569 sector = r10bio->sector & geo->chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
NeilBrown5cf00fc2012-05-21 09:28:20 +1000571 chunk *= geo->near_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 stripe = chunk;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000573 dev = sector_div(stripe, geo->raid_disks);
574 if (geo->far_offset)
575 stripe *= geo->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
NeilBrown5cf00fc2012-05-21 09:28:20 +1000577 sector += stripe << geo->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* and calculate all the others */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000580 for (n = 0; n < geo->near_copies; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int d = dev;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100582 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sector_t s = sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 r10bio->devs[slot].devnum = d;
Jonathan Brassow4c0ca262013-02-21 13:28:09 +1100585 r10bio->devs[slot].addr = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 slot++;
587
NeilBrown5cf00fc2012-05-21 09:28:20 +1000588 for (f = 1; f < geo->far_copies; f++) {
Jonathan Brassow475901a2013-02-21 13:28:10 +1100589 set = d / geo->far_set_size;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000590 d += geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100591
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100592 if ((geo->raid_disks % geo->far_set_size) &&
593 (d > last_far_set_start)) {
594 d -= last_far_set_start;
595 d %= last_far_set_size;
596 d += last_far_set_start;
597 } else {
598 d %= geo->far_set_size;
599 d += geo->far_set_size * set;
600 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000601 s += geo->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 r10bio->devs[slot].devnum = d;
603 r10bio->devs[slot].addr = s;
604 slot++;
605 }
606 dev++;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000607 if (dev >= geo->raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 dev = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000609 sector += (geo->chunk_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611 }
NeilBrownf8c9e742012-05-21 09:28:33 +1000612}
613
614static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
615{
616 struct geom *geo = &conf->geo;
617
618 if (conf->reshape_progress != MaxSector &&
619 ((r10bio->sector >= conf->reshape_progress) !=
620 conf->mddev->reshape_backwards)) {
621 set_bit(R10BIO_Previous, &r10bio->state);
622 geo = &conf->prev;
623 } else
624 clear_bit(R10BIO_Previous, &r10bio->state);
625
626 __raid10_find_phys(geo, r10bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
NeilBrowne879a872011-10-11 16:49:02 +1100629static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 sector_t offset, chunk, vchunk;
NeilBrownf8c9e742012-05-21 09:28:33 +1000632 /* Never use conf->prev as this is only called during resync
633 * or recovery, so reshape isn't happening
634 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000635 struct geom *geo = &conf->geo;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100636 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
637 int far_set_size = geo->far_set_size;
Jonathan Brassow9a3152a2013-02-21 13:28:10 +1100638 int last_far_set_start;
639
640 if (geo->raid_disks % geo->far_set_size) {
641 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
642 last_far_set_start *= geo->far_set_size;
643
644 if (dev >= last_far_set_start) {
645 far_set_size = geo->far_set_size;
646 far_set_size += (geo->raid_disks % geo->far_set_size);
647 far_set_start = last_far_set_start;
648 }
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
NeilBrown5cf00fc2012-05-21 09:28:20 +1000651 offset = sector & geo->chunk_mask;
652 if (geo->far_offset) {
NeilBrownc93983b2006-06-26 00:27:41 -0700653 int fc;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000654 chunk = sector >> geo->chunk_shift;
655 fc = sector_div(chunk, geo->far_copies);
656 dev -= fc * geo->near_copies;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100657 if (dev < far_set_start)
658 dev += far_set_size;
NeilBrownc93983b2006-06-26 00:27:41 -0700659 } else {
NeilBrown5cf00fc2012-05-21 09:28:20 +1000660 while (sector >= geo->stride) {
661 sector -= geo->stride;
Jonathan Brassow475901a2013-02-21 13:28:10 +1100662 if (dev < (geo->near_copies + far_set_start))
663 dev += far_set_size - geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700664 else
NeilBrown5cf00fc2012-05-21 09:28:20 +1000665 dev -= geo->near_copies;
NeilBrownc93983b2006-06-26 00:27:41 -0700666 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000667 chunk = sector >> geo->chunk_shift;
NeilBrownc93983b2006-06-26 00:27:41 -0700668 }
NeilBrown5cf00fc2012-05-21 09:28:20 +1000669 vchunk = chunk * geo->raid_disks + dev;
670 sector_div(vchunk, geo->near_copies);
671 return (vchunk << geo->chunk_shift) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/**
675 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
NeilBrown64590f42014-12-15 12:56:57 +1100676 * @mddev: the md device
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200677 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * @biovec: the request that could be merged to it.
679 *
680 * Return amount of bytes we can accept at this offset
NeilBrown050b6612012-03-19 12:46:39 +1100681 * This requires checking for end-of-chunk if near_copies != raid_disks,
682 * and for subordinate merge_bvec_fns if merge_check_needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
NeilBrown64590f42014-12-15 12:56:57 +1100684static int raid10_mergeable_bvec(struct mddev *mddev,
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200685 struct bvec_merge_data *bvm,
686 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
NeilBrown050b6612012-03-19 12:46:39 +1100688 struct r10conf *conf = mddev->private;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200689 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int max;
NeilBrown3ea7daa2012-05-22 13:53:47 +1000691 unsigned int chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200692 unsigned int bio_sectors = bvm->bi_size >> 9;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000693 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
NeilBrown3ea7daa2012-05-22 13:53:47 +1000695 chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
NeilBrownf8c9e742012-05-21 09:28:33 +1000696 if (conf->reshape_progress != MaxSector &&
697 ((sector >= conf->reshape_progress) !=
698 conf->mddev->reshape_backwards))
699 geo = &conf->prev;
700
NeilBrown5cf00fc2012-05-21 09:28:20 +1000701 if (geo->near_copies < geo->raid_disks) {
NeilBrown050b6612012-03-19 12:46:39 +1100702 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
703 + bio_sectors)) << 9;
704 if (max < 0)
705 /* bio_add cannot handle a negative return */
706 max = 0;
707 if (max <= biovec->bv_len && bio_sectors == 0)
708 return biovec->bv_len;
709 } else
710 max = biovec->bv_len;
711
712 if (mddev->merge_check_needed) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000713 struct {
714 struct r10bio r10_bio;
715 struct r10dev devs[conf->copies];
716 } on_stack;
717 struct r10bio *r10_bio = &on_stack.r10_bio;
NeilBrown050b6612012-03-19 12:46:39 +1100718 int s;
NeilBrownf8c9e742012-05-21 09:28:33 +1000719 if (conf->reshape_progress != MaxSector) {
720 /* Cannot give any guidance during reshape */
721 if (max <= biovec->bv_len && bio_sectors == 0)
722 return biovec->bv_len;
723 return 0;
724 }
NeilBrowne0ee7782012-08-18 09:51:42 +1000725 r10_bio->sector = sector;
726 raid10_find_phys(conf, r10_bio);
NeilBrown050b6612012-03-19 12:46:39 +1100727 rcu_read_lock();
728 for (s = 0; s < conf->copies; s++) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000729 int disk = r10_bio->devs[s].devnum;
NeilBrown050b6612012-03-19 12:46:39 +1100730 struct md_rdev *rdev = rcu_dereference(
731 conf->mirrors[disk].rdev);
732 if (rdev && !test_bit(Faulty, &rdev->flags)) {
733 struct request_queue *q =
734 bdev_get_queue(rdev->bdev);
735 if (q->merge_bvec_fn) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000736 bvm->bi_sector = r10_bio->devs[s].addr
NeilBrown050b6612012-03-19 12:46:39 +1100737 + rdev->data_offset;
738 bvm->bi_bdev = rdev->bdev;
739 max = min(max, q->merge_bvec_fn(
740 q, bvm, biovec));
741 }
742 }
743 rdev = rcu_dereference(conf->mirrors[disk].replacement);
744 if (rdev && !test_bit(Faulty, &rdev->flags)) {
745 struct request_queue *q =
746 bdev_get_queue(rdev->bdev);
747 if (q->merge_bvec_fn) {
NeilBrowne0ee7782012-08-18 09:51:42 +1000748 bvm->bi_sector = r10_bio->devs[s].addr
NeilBrown050b6612012-03-19 12:46:39 +1100749 + rdev->data_offset;
750 bvm->bi_bdev = rdev->bdev;
751 max = min(max, q->merge_bvec_fn(
752 q, bvm, biovec));
753 }
754 }
755 }
756 rcu_read_unlock();
757 }
758 return max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/*
762 * This routine returns the disk from which the requested read should
763 * be done. There is a per-array 'next expected sequential IO' sector
764 * number - if this matches on the next IO then we use the last disk.
765 * There is also a per-disk 'last know head position' sector that is
766 * maintained from IRQ contexts, both the normal and the resync IO
767 * completion handlers update this position correctly. If there is no
768 * perfect sequential match then we pick the disk whose head is closest.
769 *
770 * If there are 2 mirrors in the same 2 devices, performance degrades
771 * because position is mirror, not device based.
772 *
773 * The rdev for the device selected will have nr_pending incremented.
774 */
775
776/*
777 * FIXME: possibly should rethink readbalancing and do it differently
778 * depending on near_copies / far_copies geometry.
779 */
NeilBrown96c3fd12011-12-23 10:17:54 +1100780static struct md_rdev *read_balance(struct r10conf *conf,
781 struct r10bio *r10_bio,
782 int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000784 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000785 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000786 int sectors = r10_bio->sectors;
787 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000788 sector_t new_distance, best_dist;
Jonathan Brassow3bbae042012-07-31 10:03:52 +1000789 struct md_rdev *best_rdev, *rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000790 int do_balance;
791 int best_slot;
NeilBrown5cf00fc2012-05-21 09:28:20 +1000792 struct geom *geo = &conf->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 raid10_find_phys(conf, r10_bio);
795 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000796retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000797 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000798 best_slot = -1;
NeilBrownabbf0982011-12-23 10:17:54 +1100799 best_rdev = NULL;
NeilBrown56d99122011-05-11 14:27:03 +1000800 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000801 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000802 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
804 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800805 * device if no resync is going on (recovery is ok), or below
806 * the resync window. We take the first readable disk when
807 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000810 && (this_sector + sectors >= conf->next_resync))
811 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
NeilBrown56d99122011-05-11 14:27:03 +1000813 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000814 sector_t first_bad;
815 int bad_sectors;
816 sector_t dev_sector;
817
NeilBrown56d99122011-05-11 14:27:03 +1000818 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000820 disk = r10_bio->devs[slot].devnum;
NeilBrownabbf0982011-12-23 10:17:54 +1100821 rdev = rcu_dereference(conf->mirrors[disk].replacement);
822 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
NeilBrown050b6612012-03-19 12:46:39 +1100823 test_bit(Unmerged, &rdev->flags) ||
NeilBrownabbf0982011-12-23 10:17:54 +1100824 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
825 rdev = rcu_dereference(conf->mirrors[disk].rdev);
NeilBrown050b6612012-03-19 12:46:39 +1100826 if (rdev == NULL ||
827 test_bit(Faulty, &rdev->flags) ||
828 test_bit(Unmerged, &rdev->flags))
NeilBrownabbf0982011-12-23 10:17:54 +1100829 continue;
830 if (!test_bit(In_sync, &rdev->flags) &&
831 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
NeilBrown56d99122011-05-11 14:27:03 +1000832 continue;
833
NeilBrown856e08e2011-07-28 11:39:23 +1000834 dev_sector = r10_bio->devs[slot].addr;
835 if (is_badblock(rdev, dev_sector, sectors,
836 &first_bad, &bad_sectors)) {
837 if (best_dist < MaxSector)
838 /* Already have a better slot */
839 continue;
840 if (first_bad <= dev_sector) {
841 /* Cannot read here. If this is the
842 * 'primary' device, then we must not read
843 * beyond 'bad_sectors' from another device.
844 */
845 bad_sectors -= (dev_sector - first_bad);
846 if (!do_balance && sectors > bad_sectors)
847 sectors = bad_sectors;
848 if (best_good_sectors > sectors)
849 best_good_sectors = sectors;
850 } else {
851 sector_t good_sectors =
852 first_bad - dev_sector;
853 if (good_sectors > best_good_sectors) {
854 best_good_sectors = good_sectors;
855 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100856 best_rdev = rdev;
NeilBrown856e08e2011-07-28 11:39:23 +1000857 }
858 if (!do_balance)
859 /* Must read from here */
860 break;
861 }
862 continue;
863 } else
864 best_good_sectors = sectors;
865
NeilBrown56d99122011-05-11 14:27:03 +1000866 if (!do_balance)
867 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
NeilBrown22dfdf52005-11-28 13:44:09 -0800869 /* This optimisation is debatable, and completely destroys
870 * sequential read speed for 'far copies' arrays. So only
871 * keep it for 'near' arrays, and review those later.
872 */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000873 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800875
876 /* for far > 1 always use the lowest address */
NeilBrown5cf00fc2012-05-21 09:28:20 +1000877 if (geo->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000878 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800879 else
NeilBrown56d99122011-05-11 14:27:03 +1000880 new_distance = abs(r10_bio->devs[slot].addr -
881 conf->mirrors[disk].head_position);
882 if (new_distance < best_dist) {
883 best_dist = new_distance;
884 best_slot = slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100885 best_rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 }
NeilBrownabbf0982011-12-23 10:17:54 +1100888 if (slot >= conf->copies) {
NeilBrown56d99122011-05-11 14:27:03 +1000889 slot = best_slot;
NeilBrownabbf0982011-12-23 10:17:54 +1100890 rdev = best_rdev;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
NeilBrown56d99122011-05-11 14:27:03 +1000893 if (slot >= 0) {
NeilBrown56d99122011-05-11 14:27:03 +1000894 atomic_inc(&rdev->nr_pending);
895 if (test_bit(Faulty, &rdev->flags)) {
896 /* Cannot risk returning a device that failed
897 * before we inc'ed nr_pending
898 */
899 rdev_dec_pending(rdev, conf->mddev);
900 goto retry;
901 }
902 r10_bio->read_slot = slot;
903 } else
NeilBrown96c3fd12011-12-23 10:17:54 +1100904 rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000906 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
NeilBrown96c3fd12011-12-23 10:17:54 +1100908 return rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
NeilBrown5c675f82014-12-15 12:56:56 +1100911static int raid10_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700912{
NeilBrowne879a872011-10-11 16:49:02 +1100913 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700914 int i, ret = 0;
915
Tejun Heo44522262015-05-22 17:13:26 -0400916 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100917 conf->pending_count >= max_queued_requests)
918 return 1;
919
NeilBrown0d129222006-10-03 01:15:54 -0700920 rcu_read_lock();
NeilBrownf8c9e742012-05-21 09:28:33 +1000921 for (i = 0;
922 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
923 && ret == 0;
924 i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100925 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700926 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200927 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700928
929 ret |= bdi_congested(&q->backing_dev_info, bits);
930 }
931 }
932 rcu_read_unlock();
933 return ret;
934}
935
NeilBrowne879a872011-10-11 16:49:02 +1100936static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800937{
938 /* Any writes that have been queued but are awaiting
939 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800940 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800941 spin_lock_irq(&conf->device_lock);
942
943 if (conf->pending_bio_list.head) {
944 struct bio *bio;
945 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100946 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800947 spin_unlock_irq(&conf->device_lock);
948 /* flush any pending bitmap writes to disk
949 * before proceeding w/ I/O */
950 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100951 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800952
953 while (bio) { /* submit pending writes */
954 struct bio *next = bio->bi_next;
955 bio->bi_next = NULL;
Shaohua Li532a2a32012-10-11 13:30:52 +1100956 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
957 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
958 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200959 bio_endio(bio);
Shaohua Li532a2a32012-10-11 13:30:52 +1100960 else
961 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800962 bio = next;
963 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800964 } else
965 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800966}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100967
NeilBrown0a27ec92006-01-06 00:20:13 -0800968/* Barriers....
969 * Sometimes we need to suspend IO while we do something else,
970 * either some resync/recovery, or reconfigure the array.
971 * To do this we raise a 'barrier'.
972 * The 'barrier' is a counter that can be raised multiple times
973 * to count how many activities are happening which preclude
974 * normal IO.
975 * We can only raise the barrier if there is no pending IO.
976 * i.e. if nr_pending == 0.
977 * We choose only to raise the barrier if no-one is waiting for the
978 * barrier to go down. This means that as soon as an IO request
979 * is ready, no other operations which require a barrier will start
980 * until the IO request has had a chance.
981 *
982 * So: regular IO calls 'wait_barrier'. When that returns there
983 * is no backgroup IO happening, It must arrange to call
984 * allow_barrier when it has finished its IO.
985 * backgroup IO calls must call raise_barrier. Once that returns
986 * there is no normal IO happeing. It must arrange to call
987 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
NeilBrowne879a872011-10-11 16:49:02 +1100990static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
NeilBrown6cce3b22006-01-06 00:20:16 -0800992 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
NeilBrown6cce3b22006-01-06 00:20:16 -0800995 /* Wait until no block IO is waiting (unless 'force') */
996 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100997 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -0800998
999 /* block any new IO from starting */
1000 conf->barrier++;
1001
NeilBrownc3b328a2011-04-18 18:25:43 +10001002 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -08001003 wait_event_lock_irq(conf->wait_barrier,
1004 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +01001005 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 spin_unlock_irq(&conf->resync_lock);
1008}
1009
NeilBrowne879a872011-10-11 16:49:02 +11001010static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001011{
1012 unsigned long flags;
1013 spin_lock_irqsave(&conf->resync_lock, flags);
1014 conf->barrier--;
1015 spin_unlock_irqrestore(&conf->resync_lock, flags);
1016 wake_up(&conf->wait_barrier);
1017}
1018
NeilBrowne879a872011-10-11 16:49:02 +11001019static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001020{
1021 spin_lock_irq(&conf->resync_lock);
1022 if (conf->barrier) {
1023 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +11001024 /* Wait for the barrier to drop.
1025 * However if there are already pending
1026 * requests (preventing the barrier from
1027 * rising completely), and the
1028 * pre-process bio queue isn't empty,
1029 * then don't wait, as we need to empty
1030 * that queue to get the nr_pending
1031 * count down.
1032 */
1033 wait_event_lock_irq(conf->wait_barrier,
1034 !conf->barrier ||
1035 (conf->nr_pending &&
1036 current->bio_list &&
1037 !bio_list_empty(current->bio_list)),
Lukas Czernereed8c022012-11-30 11:42:40 +01001038 conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08001039 conf->nr_waiting--;
1040 }
1041 conf->nr_pending++;
1042 spin_unlock_irq(&conf->resync_lock);
1043}
1044
NeilBrowne879a872011-10-11 16:49:02 +11001045static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -08001046{
1047 unsigned long flags;
1048 spin_lock_irqsave(&conf->resync_lock, flags);
1049 conf->nr_pending--;
1050 spin_unlock_irqrestore(&conf->resync_lock, flags);
1051 wake_up(&conf->wait_barrier);
1052}
1053
NeilBrowne2d59922013-06-12 11:01:22 +10001054static void freeze_array(struct r10conf *conf, int extra)
NeilBrown4443ae12006-01-06 00:20:28 -08001055{
1056 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -08001057 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -08001058 * We increment barrier and nr_waiting, and then
NeilBrowne2d59922013-06-12 11:01:22 +10001059 * wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -08001060 * This is called in the context of one normal IO request
1061 * that has failed. Thus any sync request that might be pending
1062 * will be blocked by nr_pending, and we need to wait for
1063 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +10001064 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -08001065 * must match the number of pending IOs (nr_pending) before
1066 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -08001067 */
1068 spin_lock_irq(&conf->resync_lock);
1069 conf->barrier++;
1070 conf->nr_waiting++;
Lukas Czernereed8c022012-11-30 11:42:40 +01001071 wait_event_lock_irq_cmd(conf->wait_barrier,
NeilBrowne2d59922013-06-12 11:01:22 +10001072 conf->nr_pending == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +01001073 conf->resync_lock,
1074 flush_pending_writes(conf));
NeilBrownc3b328a2011-04-18 18:25:43 +10001075
NeilBrown4443ae12006-01-06 00:20:28 -08001076 spin_unlock_irq(&conf->resync_lock);
1077}
1078
NeilBrowne879a872011-10-11 16:49:02 +11001079static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -08001080{
1081 /* reverse the effect of the freeze */
1082 spin_lock_irq(&conf->resync_lock);
1083 conf->barrier--;
1084 conf->nr_waiting--;
1085 wake_up(&conf->wait_barrier);
1086 spin_unlock_irq(&conf->resync_lock);
1087}
1088
NeilBrownf8c9e742012-05-21 09:28:33 +10001089static sector_t choose_data_offset(struct r10bio *r10_bio,
1090 struct md_rdev *rdev)
1091{
1092 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1093 test_bit(R10BIO_Previous, &r10_bio->state))
1094 return rdev->data_offset;
1095 else
1096 return rdev->new_data_offset;
1097}
1098
NeilBrown57c67df2012-10-11 13:32:13 +11001099struct raid10_plug_cb {
1100 struct blk_plug_cb cb;
1101 struct bio_list pending;
1102 int pending_cnt;
1103};
1104
1105static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1106{
1107 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1108 cb);
1109 struct mddev *mddev = plug->cb.data;
1110 struct r10conf *conf = mddev->private;
1111 struct bio *bio;
1112
NeilBrown874807a2012-11-27 12:14:40 +11001113 if (from_schedule || current->bio_list) {
NeilBrown57c67df2012-10-11 13:32:13 +11001114 spin_lock_irq(&conf->device_lock);
1115 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1116 conf->pending_count += plug->pending_cnt;
1117 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001118 wake_up(&conf->wait_barrier);
NeilBrown57c67df2012-10-11 13:32:13 +11001119 md_wakeup_thread(mddev->thread);
1120 kfree(plug);
1121 return;
1122 }
1123
1124 /* we aren't scheduling, so we can do the write-out directly. */
1125 bio = bio_list_get(&plug->pending);
1126 bitmap_unplug(mddev->bitmap);
1127 wake_up(&conf->wait_barrier);
1128
1129 while (bio) { /* submit pending writes */
1130 struct bio *next = bio->bi_next;
1131 bio->bi_next = NULL;
Shaohua Li32f9f572013-04-28 18:26:38 +08001132 if (unlikely((bio->bi_rw & REQ_DISCARD) &&
1133 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
1134 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001135 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001136 else
1137 generic_make_request(bio);
NeilBrown57c67df2012-10-11 13:32:13 +11001138 bio = next;
1139 }
1140 kfree(plug);
1141}
1142
Kent Overstreet20d01892013-11-23 18:21:01 -08001143static void __make_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
NeilBrowne879a872011-10-11 16:49:02 +11001145 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001146 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct bio *read_bio;
1148 int i;
Jens Axboea3623572005-11-01 09:26:16 +01001149 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +10001150 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +02001151 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
Shaohua Li532a2a32012-10-11 13:30:52 +11001152 const unsigned long do_discard = (bio->bi_rw
1153 & (REQ_DISCARD | REQ_SECURE));
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11001154 const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
NeilBrown6cce3b22006-01-06 00:20:16 -08001155 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001156 struct md_rdev *blocked_rdev;
NeilBrown57c67df2012-10-11 13:32:13 +11001157 struct blk_plug_cb *cb;
1158 struct raid10_plug_cb *plug = NULL;
NeilBrownd4432c22011-07-28 11:39:24 +10001159 int sectors_handled;
1160 int max_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001161 int sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
NeilBrowncc13b1d2014-05-05 13:34:37 +10001163 /*
1164 * Register the new request and wait if the reconstruction
1165 * thread has put up a bar for new requests.
1166 * Continue immediately if no resync is active currently.
1167 */
1168 wait_barrier(conf);
1169
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001170 sectors = bio_sectors(bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001171 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001172 bio->bi_iter.bi_sector < conf->reshape_progress &&
1173 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001174 /* IO spans the reshape position. Need to wait for
1175 * reshape to pass
1176 */
1177 allow_barrier(conf);
1178 wait_event(conf->wait_barrier,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001179 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1180 conf->reshape_progress >= bio->bi_iter.bi_sector +
1181 sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10001182 wait_barrier(conf);
1183 }
1184 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1185 bio_data_dir(bio) == WRITE &&
1186 (mddev->reshape_backwards
Kent Overstreet4f024f32013-10-11 15:44:27 -07001187 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1188 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1189 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1190 bio->bi_iter.bi_sector < conf->reshape_progress))) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10001191 /* Need to update reshape_position in metadata */
1192 mddev->reshape_position = conf->reshape_progress;
1193 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1194 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1195 md_wakeup_thread(mddev->thread);
1196 wait_event(mddev->sb_wait,
1197 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1198
1199 conf->reshape_safe = mddev->reshape_position;
1200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1203
1204 r10_bio->master_bio = bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10001205 r10_bio->sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001208 r10_bio->sector = bio->bi_iter.bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -08001209 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
NeilBrown856e08e2011-07-28 11:39:23 +10001211 /* We might need to issue multiple reads to different
1212 * devices if there are bad blocks around, so we keep
1213 * track of the number of reads in bio->bi_phys_segments.
1214 * If this is 0, there is only one r10_bio and no locking
1215 * will be needed when the request completes. If it is
1216 * non-zero, then it is the number of not-completed requests.
1217 */
1218 bio->bi_phys_segments = 0;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06001219 bio_clear_flag(bio, BIO_SEG_VALID);
NeilBrown856e08e2011-07-28 11:39:23 +10001220
Jens Axboea3623572005-11-01 09:26:16 +01001221 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * read balancing logic:
1224 */
NeilBrown96c3fd12011-12-23 10:17:54 +11001225 struct md_rdev *rdev;
NeilBrown856e08e2011-07-28 11:39:23 +10001226 int slot;
1227
1228read_again:
NeilBrown96c3fd12011-12-23 10:17:54 +11001229 rdev = read_balance(conf, r10_bio, &max_sectors);
1230 if (!rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
NeilBrown96c3fd12011-12-23 10:17:54 +11001234 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
NeilBrowna167f662010-10-26 18:31:13 +11001236 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001237 bio_trim(read_bio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001238 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 r10_bio->devs[slot].bio = read_bio;
NeilBrownabbf0982011-12-23 10:17:54 +11001241 r10_bio->devs[slot].rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Kent Overstreet4f024f32013-10-11 15:44:27 -07001243 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
NeilBrownf8c9e742012-05-21 09:28:33 +10001244 choose_data_offset(r10_bio, rdev);
NeilBrown96c3fd12011-12-23 10:17:54 +11001245 read_bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001247 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 read_bio->bi_private = r10_bio;
1249
NeilBrown856e08e2011-07-28 11:39:23 +10001250 if (max_sectors < r10_bio->sectors) {
1251 /* Could not read all from this device, so we will
1252 * need another r10_bio.
1253 */
NeilBrownb50c2592014-01-14 10:38:09 +11001254 sectors_handled = (r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07001255 - bio->bi_iter.bi_sector);
NeilBrown856e08e2011-07-28 11:39:23 +10001256 r10_bio->sectors = max_sectors;
1257 spin_lock_irq(&conf->device_lock);
1258 if (bio->bi_phys_segments == 0)
1259 bio->bi_phys_segments = 2;
1260 else
1261 bio->bi_phys_segments++;
NeilBrownb50c2592014-01-14 10:38:09 +11001262 spin_unlock_irq(&conf->device_lock);
NeilBrown856e08e2011-07-28 11:39:23 +10001263 /* Cannot call generic_make_request directly
1264 * as that will be queued in __generic_make_request
1265 * and subsequent mempool_alloc might block
1266 * waiting for it. so hand bio over to raid10d.
1267 */
1268 reschedule_retry(r10_bio);
1269
1270 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1271
1272 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001273 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrown856e08e2011-07-28 11:39:23 +10001274 r10_bio->state = 0;
1275 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001276 r10_bio->sector = bio->bi_iter.bi_sector +
1277 sectors_handled;
NeilBrown856e08e2011-07-28 11:39:23 +10001278 goto read_again;
1279 } else
1280 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001281 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 /*
1285 * WRITE:
1286 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001287 if (conf->pending_count >= max_queued_requests) {
1288 md_wakeup_thread(mddev->thread);
1289 wait_event(conf->wait_barrier,
1290 conf->pending_count < max_queued_requests);
1291 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001292 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * inc refcount on their rdev. Record them by setting
1294 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001295 * If there are known/acknowledged bad blocks on any device
1296 * on which we have seen a write error, we want to avoid
1297 * writing to those blocks. This potentially requires several
1298 * writes to write around the bad blocks. Each set of writes
1299 * gets its own r10_bio with a set of bios attached. The number
1300 * of r10_bios is recored in bio->bi_phys_segments just as with
1301 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001303
NeilBrown69335ef2011-12-23 10:17:54 +11001304 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001306retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001307 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001309 max_sectors = r10_bio->sectors;
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 for (i = 0; i < conf->copies; i++) {
1312 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001313 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown475b0322011-12-23 10:17:55 +11001314 struct md_rdev *rrdev = rcu_dereference(
1315 conf->mirrors[d].replacement);
NeilBrown4ca40c22011-12-23 10:17:55 +11001316 if (rdev == rrdev)
1317 rrdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001318 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1319 atomic_inc(&rdev->nr_pending);
1320 blocked_rdev = rdev;
1321 break;
1322 }
NeilBrown475b0322011-12-23 10:17:55 +11001323 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1324 atomic_inc(&rrdev->nr_pending);
1325 blocked_rdev = rrdev;
1326 break;
1327 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001328 if (rdev && (test_bit(Faulty, &rdev->flags)
1329 || test_bit(Unmerged, &rdev->flags)))
1330 rdev = NULL;
NeilBrown050b6612012-03-19 12:46:39 +11001331 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1332 || test_bit(Unmerged, &rrdev->flags)))
NeilBrown475b0322011-12-23 10:17:55 +11001333 rrdev = NULL;
1334
NeilBrownd4432c22011-07-28 11:39:24 +10001335 r10_bio->devs[i].bio = NULL;
NeilBrown475b0322011-12-23 10:17:55 +11001336 r10_bio->devs[i].repl_bio = NULL;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001337
1338 if (!rdev && !rrdev) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001339 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001340 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001341 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001342 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
NeilBrownd4432c22011-07-28 11:39:24 +10001343 sector_t first_bad;
1344 sector_t dev_sector = r10_bio->devs[i].addr;
1345 int bad_sectors;
1346 int is_bad;
1347
1348 is_bad = is_badblock(rdev, dev_sector,
1349 max_sectors,
1350 &first_bad, &bad_sectors);
1351 if (is_bad < 0) {
1352 /* Mustn't write here until the bad block
1353 * is acknowledged
1354 */
1355 atomic_inc(&rdev->nr_pending);
1356 set_bit(BlockedBadBlocks, &rdev->flags);
1357 blocked_rdev = rdev;
1358 break;
1359 }
1360 if (is_bad && first_bad <= dev_sector) {
1361 /* Cannot write here at all */
1362 bad_sectors -= (dev_sector - first_bad);
1363 if (bad_sectors < max_sectors)
1364 /* Mustn't write more than bad_sectors
1365 * to other devices yet
1366 */
1367 max_sectors = bad_sectors;
1368 /* We don't set R10BIO_Degraded as that
1369 * only applies if the disk is missing,
1370 * so it might be re-added, and we want to
1371 * know to recover this chunk.
1372 * In this case the device is here, and the
1373 * fact that this chunk is not in-sync is
1374 * recorded in the bad block log.
1375 */
1376 continue;
1377 }
1378 if (is_bad) {
1379 int good_sectors = first_bad - dev_sector;
1380 if (good_sectors < max_sectors)
1381 max_sectors = good_sectors;
1382 }
1383 }
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001384 if (rdev) {
1385 r10_bio->devs[i].bio = bio;
1386 atomic_inc(&rdev->nr_pending);
1387 }
NeilBrown475b0322011-12-23 10:17:55 +11001388 if (rrdev) {
1389 r10_bio->devs[i].repl_bio = bio;
1390 atomic_inc(&rrdev->nr_pending);
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393 rcu_read_unlock();
1394
Dan Williams6bfe0b42008-04-30 00:52:32 -07001395 if (unlikely(blocked_rdev)) {
1396 /* Have to wait for this device to get unblocked, then retry */
1397 int j;
1398 int d;
1399
NeilBrown475b0322011-12-23 10:17:55 +11001400 for (j = 0; j < i; j++) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07001401 if (r10_bio->devs[j].bio) {
1402 d = r10_bio->devs[j].devnum;
1403 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1404 }
NeilBrown475b0322011-12-23 10:17:55 +11001405 if (r10_bio->devs[j].repl_bio) {
NeilBrown4ca40c22011-12-23 10:17:55 +11001406 struct md_rdev *rdev;
NeilBrown475b0322011-12-23 10:17:55 +11001407 d = r10_bio->devs[j].devnum;
NeilBrown4ca40c22011-12-23 10:17:55 +11001408 rdev = conf->mirrors[d].replacement;
1409 if (!rdev) {
1410 /* Race with remove_disk */
1411 smp_mb();
1412 rdev = conf->mirrors[d].rdev;
1413 }
1414 rdev_dec_pending(rdev, mddev);
NeilBrown475b0322011-12-23 10:17:55 +11001415 }
1416 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001417 allow_barrier(conf);
1418 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1419 wait_barrier(conf);
1420 goto retry_write;
1421 }
1422
NeilBrownd4432c22011-07-28 11:39:24 +10001423 if (max_sectors < r10_bio->sectors) {
1424 /* We are splitting this into multiple parts, so
1425 * we need to prepare for allocating another r10_bio.
1426 */
1427 r10_bio->sectors = max_sectors;
1428 spin_lock_irq(&conf->device_lock);
1429 if (bio->bi_phys_segments == 0)
1430 bio->bi_phys_segments = 2;
1431 else
1432 bio->bi_phys_segments++;
1433 spin_unlock_irq(&conf->device_lock);
1434 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001435 sectors_handled = r10_bio->sector + max_sectors -
1436 bio->bi_iter.bi_sector;
NeilBrownd4432c22011-07-28 11:39:24 +10001437
NeilBrown4e780642010-10-19 12:54:01 +11001438 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001439 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 for (i = 0; i < conf->copies; i++) {
1442 struct bio *mbio;
1443 int d = r10_bio->devs[i].devnum;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001444 if (r10_bio->devs[i].bio) {
1445 struct md_rdev *rdev = conf->mirrors[d].rdev;
1446 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001447 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001448 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001449 r10_bio->devs[i].bio = mbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Kent Overstreet4f024f32013-10-11 15:44:27 -07001451 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001452 choose_data_offset(r10_bio,
1453 rdev));
1454 mbio->bi_bdev = rdev->bdev;
1455 mbio->bi_end_io = raid10_end_write_request;
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11001456 mbio->bi_rw =
1457 WRITE | do_sync | do_fua | do_discard | do_same;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001458 mbio->bi_private = r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001460 atomic_inc(&r10_bio->remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001462 cb = blk_check_plugged(raid10_unplug, mddev,
1463 sizeof(*plug));
1464 if (cb)
1465 plug = container_of(cb, struct raid10_plug_cb,
1466 cb);
1467 else
1468 plug = NULL;
1469 spin_lock_irqsave(&conf->device_lock, flags);
1470 if (plug) {
1471 bio_list_add(&plug->pending, mbio);
1472 plug->pending_cnt++;
1473 } else {
1474 bio_list_add(&conf->pending_bio_list, mbio);
1475 conf->pending_count++;
1476 }
1477 spin_unlock_irqrestore(&conf->device_lock, flags);
1478 if (!plug)
1479 md_wakeup_thread(mddev->thread);
1480 }
NeilBrown57c67df2012-10-11 13:32:13 +11001481
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001482 if (r10_bio->devs[i].repl_bio) {
1483 struct md_rdev *rdev = conf->mirrors[d].replacement;
1484 if (rdev == NULL) {
1485 /* Replacement just got moved to main 'rdev' */
1486 smp_mb();
1487 rdev = conf->mirrors[d].rdev;
1488 }
1489 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001490 bio_trim(mbio, r10_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001491 max_sectors);
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001492 r10_bio->devs[i].repl_bio = mbio;
1493
Kent Overstreet4f024f32013-10-11 15:44:27 -07001494 mbio->bi_iter.bi_sector = (r10_bio->devs[i].addr +
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001495 choose_data_offset(
1496 r10_bio, rdev));
1497 mbio->bi_bdev = rdev->bdev;
1498 mbio->bi_end_io = raid10_end_write_request;
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11001499 mbio->bi_rw =
1500 WRITE | do_sync | do_fua | do_discard | do_same;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001501 mbio->bi_private = r10_bio;
1502
1503 atomic_inc(&r10_bio->remaining);
1504 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown57c67df2012-10-11 13:32:13 +11001505 bio_list_add(&conf->pending_bio_list, mbio);
1506 conf->pending_count++;
NeilBrowne7c0c3f2012-11-22 14:42:49 +11001507 spin_unlock_irqrestore(&conf->device_lock, flags);
1508 if (!mddev_check_plugged(mddev))
1509 md_wakeup_thread(mddev->thread);
NeilBrown57c67df2012-10-11 13:32:13 +11001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512
NeilBrown079fa162011-09-10 17:21:23 +10001513 /* Don't remove the bias on 'remaining' (one_write_done) until
1514 * after checking if we need to go around again.
1515 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001516
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001517 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001518 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001519 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001520 * in bio->bi_phys_segments.
1521 */
1522 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1523
1524 r10_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001525 r10_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001526
1527 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001528 r10_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrownd4432c22011-07-28 11:39:24 +10001529 r10_bio->state = 0;
1530 goto retry_write;
1531 }
NeilBrown079fa162011-09-10 17:21:23 +10001532 one_write_done(r10_bio);
Kent Overstreet20d01892013-11-23 18:21:01 -08001533}
1534
1535static void make_request(struct mddev *mddev, struct bio *bio)
1536{
1537 struct r10conf *conf = mddev->private;
1538 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1539 int chunk_sects = chunk_mask + 1;
1540
1541 struct bio *split;
1542
1543 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1544 md_flush_request(mddev, bio);
1545 return;
1546 }
1547
1548 md_write_start(mddev, bio);
1549
Kent Overstreet20d01892013-11-23 18:21:01 -08001550 do {
1551
1552 /*
1553 * If this request crosses a chunk boundary, we need to split
1554 * it.
1555 */
1556 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1557 bio_sectors(bio) > chunk_sects
1558 && (conf->geo.near_copies < conf->geo.raid_disks
1559 || conf->prev.near_copies <
1560 conf->prev.raid_disks))) {
1561 split = bio_split(bio, chunk_sects -
1562 (bio->bi_iter.bi_sector &
1563 (chunk_sects - 1)),
1564 GFP_NOIO, fs_bio_set);
1565 bio_chain(split, bio);
1566 } else {
1567 split = bio;
1568 }
1569
1570 __make_request(mddev, split);
1571 } while (split != bio);
NeilBrown079fa162011-09-10 17:21:23 +10001572
1573 /* In case raid10d snuck in to freeze_array */
1574 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
NeilBrownfd01b882011-10-11 16:47:53 +11001577static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
NeilBrowne879a872011-10-11 16:49:02 +11001579 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int i;
1581
NeilBrown5cf00fc2012-05-21 09:28:20 +10001582 if (conf->geo.near_copies < conf->geo.raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001583 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
NeilBrown5cf00fc2012-05-21 09:28:20 +10001584 if (conf->geo.near_copies > 1)
1585 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1586 if (conf->geo.far_copies > 1) {
1587 if (conf->geo.far_offset)
1588 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001589 else
NeilBrown5cf00fc2012-05-21 09:28:20 +10001590 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001591 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001592 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1593 conf->geo.raid_disks - mddev->degraded);
1594 for (i = 0; i < conf->geo.raid_disks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 seq_printf(seq, "%s",
1596 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001597 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 seq_printf(seq, "]");
1599}
1600
NeilBrown700c7212011-07-27 11:00:36 +10001601/* check if there are enough drives for
1602 * every block to appear on atleast one.
1603 * Don't consider the device numbered 'ignore'
1604 * as we might be about to remove it.
1605 */
NeilBrown635f6412013-06-11 14:57:09 +10001606static int _enough(struct r10conf *conf, int previous, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001607{
1608 int first = 0;
NeilBrown725d6e52013-06-11 15:08:03 +10001609 int has_enough = 0;
NeilBrown635f6412013-06-11 14:57:09 +10001610 int disks, ncopies;
1611 if (previous) {
1612 disks = conf->prev.raid_disks;
1613 ncopies = conf->prev.near_copies;
1614 } else {
1615 disks = conf->geo.raid_disks;
1616 ncopies = conf->geo.near_copies;
1617 }
NeilBrown700c7212011-07-27 11:00:36 +10001618
NeilBrown725d6e52013-06-11 15:08:03 +10001619 rcu_read_lock();
NeilBrown700c7212011-07-27 11:00:36 +10001620 do {
1621 int n = conf->copies;
1622 int cnt = 0;
NeilBrown80b48122012-09-27 12:35:21 +10001623 int this = first;
NeilBrown700c7212011-07-27 11:00:36 +10001624 while (n--) {
NeilBrown725d6e52013-06-11 15:08:03 +10001625 struct md_rdev *rdev;
1626 if (this != ignore &&
1627 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1628 test_bit(In_sync, &rdev->flags))
NeilBrown700c7212011-07-27 11:00:36 +10001629 cnt++;
NeilBrown635f6412013-06-11 14:57:09 +10001630 this = (this+1) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001631 }
1632 if (cnt == 0)
NeilBrown725d6e52013-06-11 15:08:03 +10001633 goto out;
NeilBrown635f6412013-06-11 14:57:09 +10001634 first = (first + ncopies) % disks;
NeilBrown700c7212011-07-27 11:00:36 +10001635 } while (first != 0);
NeilBrown725d6e52013-06-11 15:08:03 +10001636 has_enough = 1;
1637out:
1638 rcu_read_unlock();
1639 return has_enough;
NeilBrown700c7212011-07-27 11:00:36 +10001640}
1641
NeilBrownf8c9e742012-05-21 09:28:33 +10001642static int enough(struct r10conf *conf, int ignore)
1643{
NeilBrown635f6412013-06-11 14:57:09 +10001644 /* when calling 'enough', both 'prev' and 'geo' must
1645 * be stable.
1646 * This is ensured if ->reconfig_mutex or ->device_lock
1647 * is held.
1648 */
1649 return _enough(conf, 0, ignore) &&
1650 _enough(conf, 1, ignore);
NeilBrownf8c9e742012-05-21 09:28:33 +10001651}
1652
NeilBrownfd01b882011-10-11 16:47:53 +11001653static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001656 struct r10conf *conf = mddev->private;
NeilBrown635f6412013-06-11 14:57:09 +10001657 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /*
1660 * If it is not operational, then we have already marked it as dead
1661 * else if it is the last working disks, ignore the error, let the
1662 * next level up know.
1663 * else mark the drive as failed
1664 */
NeilBrown635f6412013-06-11 14:57:09 +10001665 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001666 if (test_bit(In_sync, &rdev->flags)
NeilBrown635f6412013-06-11 14:57:09 +10001667 && !enough(conf, rdev->raid_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /*
1669 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
NeilBrownc04be0a2006-10-03 01:15:53 -07001671 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown635f6412013-06-11 14:57:09 +10001672 return;
1673 }
NeilBrown2446dba2014-07-31 10:16:29 +10001674 if (test_and_clear_bit(In_sync, &rdev->flags))
NeilBrown635f6412013-06-11 14:57:09 +10001675 mddev->degraded++;
NeilBrown2446dba2014-07-31 10:16:29 +10001676 /*
1677 * If recovery is running, make sure it aborts.
1678 */
1679 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownde393cd2011-07-28 11:31:48 +10001680 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001681 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001682 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown635f6412013-06-11 14:57:09 +10001683 spin_unlock_irqrestore(&conf->device_lock, flags);
Joe Perches067032b2011-01-14 09:14:33 +11001684 printk(KERN_ALERT
1685 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1686 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001687 mdname(mddev), bdevname(rdev->bdev, b),
NeilBrown5cf00fc2012-05-21 09:28:20 +10001688 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
NeilBrowne879a872011-10-11 16:49:02 +11001691static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 int i;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001694 struct raid10_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
NeilBrown128595e2010-05-03 14:47:14 +10001696 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001698 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return;
1700 }
NeilBrown5cf00fc2012-05-21 09:28:20 +10001701 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1702 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
NeilBrown5cf00fc2012-05-21 09:28:20 +10001704 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 char b[BDEVNAME_SIZE];
1706 tmp = conf->mirrors + i;
1707 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001708 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001709 i, !test_bit(In_sync, &tmp->rdev->flags),
1710 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 bdevname(tmp->rdev->bdev,b));
1712 }
1713}
1714
NeilBrowne879a872011-10-11 16:49:02 +11001715static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
NeilBrown0a27ec92006-01-06 00:20:13 -08001717 wait_barrier(conf);
1718 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 mempool_destroy(conf->r10buf_pool);
1721 conf->r10buf_pool = NULL;
1722}
1723
NeilBrownfd01b882011-10-11 16:47:53 +11001724static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001727 struct r10conf *conf = mddev->private;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001728 struct raid10_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001729 int count = 0;
1730 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
1733 * Find all non-in_sync disks within the RAID10 configuration
1734 * and mark them in_sync
1735 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10001736 for (i = 0; i < conf->geo.raid_disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 tmp = conf->mirrors + i;
NeilBrown4ca40c22011-12-23 10:17:55 +11001738 if (tmp->replacement
1739 && tmp->replacement->recovery_offset == MaxSector
1740 && !test_bit(Faulty, &tmp->replacement->flags)
1741 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1742 /* Replacement has just become active */
1743 if (!tmp->rdev
1744 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1745 count++;
1746 if (tmp->rdev) {
1747 /* Replaced device not technically faulty,
1748 * but we need to be sure it gets removed
1749 * and never re-added.
1750 */
1751 set_bit(Faulty, &tmp->rdev->flags);
1752 sysfs_notify_dirent_safe(
1753 tmp->rdev->sysfs_state);
1754 }
1755 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1756 } else if (tmp->rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001757 && tmp->rdev->recovery_offset == MaxSector
NeilBrown4ca40c22011-12-23 10:17:55 +11001758 && !test_bit(Faulty, &tmp->rdev->flags)
1759 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001760 count++;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001761 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 }
NeilBrown6b965622010-08-18 11:56:59 +10001764 spin_lock_irqsave(&conf->device_lock, flags);
1765 mddev->degraded -= count;
1766 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001769 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
NeilBrownfd01b882011-10-11 16:47:53 +11001772static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
NeilBrowne879a872011-10-11 16:49:02 +11001774 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001775 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001777 int first = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10001778 int last = conf->geo.raid_disks - 1;
NeilBrown050b6612012-03-19 12:46:39 +11001779 struct request_queue *q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 if (mddev->recovery_cp < MaxSector)
1782 /* only hot-add to in-sync arrays, as recovery is
1783 * very different from resync
1784 */
Neil Brown199050e2008-06-28 08:31:33 +10001785 return -EBUSY;
NeilBrown635f6412013-06-11 14:57:09 +10001786 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001787 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
NeilBrowna53a6c82008-11-06 17:28:20 +11001789 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001790 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
NeilBrown050b6612012-03-19 12:46:39 +11001792 if (q->merge_bvec_fn) {
1793 set_bit(Unmerged, &rdev->flags);
1794 mddev->merge_check_needed = 1;
1795 }
1796
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001797 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001798 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1799 mirror = rdev->saved_raid_disk;
1800 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001801 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001802 for ( ; mirror <= last ; mirror++) {
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001803 struct raid10_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001804 if (p->recovery_disabled == mddev->recovery_disabled)
1805 continue;
NeilBrownb7044d42011-12-23 10:17:56 +11001806 if (p->rdev) {
1807 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1808 p->replacement != NULL)
1809 continue;
1810 clear_bit(In_sync, &rdev->flags);
1811 set_bit(Replacement, &rdev->flags);
1812 rdev->raid_disk = mirror;
1813 err = 0;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001814 if (mddev->gendisk)
1815 disk_stack_limits(mddev->gendisk, rdev->bdev,
1816 rdev->data_offset << 9);
NeilBrownb7044d42011-12-23 10:17:56 +11001817 conf->fullsync = 1;
1818 rcu_assign_pointer(p->replacement, rdev);
1819 break;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Jonathan Brassow9092c022013-05-02 14:19:24 -05001822 if (mddev->gendisk)
1823 disk_stack_limits(mddev->gendisk, rdev->bdev,
1824 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
NeilBrown2bb77732011-07-27 11:00:36 +10001826 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001827 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001828 rdev->raid_disk = mirror;
1829 err = 0;
1830 if (rdev->saved_raid_disk != mirror)
1831 conf->fullsync = 1;
1832 rcu_assign_pointer(p->rdev, rdev);
1833 break;
1834 }
NeilBrown050b6612012-03-19 12:46:39 +11001835 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1836 /* Some requests might not have seen this new
1837 * merge_bvec_fn. We must wait for them to complete
1838 * before merging the device fully.
1839 * First we make sure any code which has tested
1840 * our function has submitted the request, then
1841 * we wait for all outstanding requests to complete.
1842 */
1843 synchronize_sched();
NeilBrowne2d59922013-06-12 11:01:22 +10001844 freeze_array(conf, 0);
1845 unfreeze_array(conf);
NeilBrown050b6612012-03-19 12:46:39 +11001846 clear_bit(Unmerged, &rdev->flags);
1847 }
Andre Nollac5e7112009-08-03 10:59:47 +10001848 md_integrity_add_rdev(rdev, mddev);
Jonathan Brassowed30be02012-10-31 11:42:30 +11001849 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li532a2a32012-10-11 13:30:52 +11001850 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001853 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
NeilBrownb8321b62011-12-23 10:17:51 +11001856static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
NeilBrowne879a872011-10-11 16:49:02 +11001858 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001860 int number = rdev->raid_disk;
NeilBrownc8ab9032011-12-23 10:17:54 +11001861 struct md_rdev **rdevp;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10001862 struct raid10_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 print_conf(conf);
NeilBrownc8ab9032011-12-23 10:17:54 +11001865 if (rdev == p->rdev)
1866 rdevp = &p->rdev;
1867 else if (rdev == p->replacement)
1868 rdevp = &p->replacement;
1869 else
1870 return 0;
1871
1872 if (test_bit(In_sync, &rdev->flags) ||
1873 atomic_read(&rdev->nr_pending)) {
1874 err = -EBUSY;
1875 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
NeilBrownc8ab9032011-12-23 10:17:54 +11001877 /* Only remove faulty devices if recovery
1878 * is not possible.
1879 */
1880 if (!test_bit(Faulty, &rdev->flags) &&
1881 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown4ca40c22011-12-23 10:17:55 +11001882 (!p->replacement || p->replacement == rdev) &&
NeilBrown63aced62012-05-22 13:55:33 +10001883 number < conf->geo.raid_disks &&
NeilBrownc8ab9032011-12-23 10:17:54 +11001884 enough(conf, -1)) {
1885 err = -EBUSY;
1886 goto abort;
1887 }
1888 *rdevp = NULL;
1889 synchronize_rcu();
1890 if (atomic_read(&rdev->nr_pending)) {
1891 /* lost the race, try later */
1892 err = -EBUSY;
1893 *rdevp = rdev;
1894 goto abort;
NeilBrown4ca40c22011-12-23 10:17:55 +11001895 } else if (p->replacement) {
1896 /* We must have just cleared 'rdev' */
1897 p->rdev = p->replacement;
1898 clear_bit(Replacement, &p->replacement->flags);
1899 smp_mb(); /* Make sure other CPUs may see both as identical
1900 * but will never see neither -- if they are careful.
1901 */
1902 p->replacement = NULL;
1903 clear_bit(WantReplacement, &rdev->flags);
1904 } else
1905 /* We might have just remove the Replacement as faulty
1906 * Clear the flag just in case
1907 */
1908 clear_bit(WantReplacement, &rdev->flags);
1909
NeilBrownc8ab9032011-12-23 10:17:54 +11001910 err = md_integrity_register(mddev);
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912abort:
1913
1914 print_conf(conf);
1915 return err;
1916}
1917
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001918static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001920 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001921 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001922 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown3ea7daa2012-05-22 13:53:47 +10001924 if (bio == r10_bio->master_bio) {
1925 /* this is a reshape read */
1926 d = r10_bio->read_slot; /* really the read dev */
1927 } else
1928 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001929
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001930 if (!bio->bi_error)
NeilBrown0eb3ff12006-01-06 00:20:29 -08001931 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001932 else
1933 /* The write handler will notice the lack of
1934 * R10BIO_Uptodate and record any errors etc
1935 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001936 atomic_add(r10_bio->sectors,
1937 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* for reconstruct, we always reschedule after a read.
1940 * for resync, only after all reads
1941 */
NeilBrown73d5c382009-02-25 13:18:47 +11001942 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1944 atomic_dec_and_test(&r10_bio->remaining)) {
1945 /* we have read all the blocks,
1946 * do the comparison in process context in raid10d
1947 */
1948 reschedule_retry(r10_bio);
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
NeilBrown9f2c9d12011-10-11 16:48:43 +11001952static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001953{
NeilBrownfd01b882011-10-11 16:47:53 +11001954 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001955
1956 while (atomic_dec_and_test(&r10_bio->remaining)) {
1957 if (r10_bio->master_bio == NULL) {
1958 /* the primary of several recovery bios */
1959 sector_t s = r10_bio->sectors;
1960 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1961 test_bit(R10BIO_WriteError, &r10_bio->state))
1962 reschedule_retry(r10_bio);
1963 else
1964 put_buf(r10_bio);
1965 md_done_sync(mddev, s, 1);
1966 break;
1967 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001968 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001969 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1970 test_bit(R10BIO_WriteError, &r10_bio->state))
1971 reschedule_retry(r10_bio);
1972 else
1973 put_buf(r10_bio);
1974 r10_bio = r10_bio2;
1975 }
1976 }
1977}
1978
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001979static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001981 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001982 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001983 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001984 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001985 sector_t first_bad;
1986 int bad_sectors;
1987 int slot;
NeilBrown9ad1aef2011-12-23 10:17:55 +11001988 int repl;
NeilBrown4ca40c22011-12-23 10:17:55 +11001989 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
NeilBrown9ad1aef2011-12-23 10:17:55 +11001991 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1992 if (repl)
1993 rdev = conf->mirrors[d].replacement;
NeilBrown547414d2012-03-13 11:21:20 +11001994 else
NeilBrown9ad1aef2011-12-23 10:17:55 +11001995 rdev = conf->mirrors[d].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001997 if (bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11001998 if (repl)
1999 md_error(mddev, rdev);
2000 else {
2001 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002002 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2003 set_bit(MD_RECOVERY_NEEDED,
2004 &rdev->mddev->recovery);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002005 set_bit(R10BIO_WriteError, &r10_bio->state);
2006 }
2007 } else if (is_badblock(rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10002008 r10_bio->devs[slot].addr,
2009 r10_bio->sectors,
2010 &first_bad, &bad_sectors))
2011 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07002012
NeilBrown9ad1aef2011-12-23 10:17:55 +11002013 rdev_dec_pending(rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10002014
2015 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
2018/*
2019 * Note: sync and recover and handled very differently for raid10
2020 * This code is for resync.
2021 * For resync, we read through virtual addresses and read all blocks.
2022 * If there is any error, we schedule a write. The lowest numbered
2023 * drive is authoritative.
2024 * However requests come for physical address, so we need to map.
2025 * For every physical address there are raid_disks/copies virtual addresses,
2026 * which is always are least one, but is not necessarly an integer.
2027 * This means that a physical address can span multiple chunks, so we may
2028 * have to submit multiple io requests for a single sync request.
2029 */
2030/*
2031 * We check if all blocks are in-sync and only write to blocks that
2032 * aren't in sync
2033 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002034static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
NeilBrowne879a872011-10-11 16:49:02 +11002036 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int i, first;
2038 struct bio *tbio, *fbio;
majianpengf4380a92012-04-12 16:04:47 +10002039 int vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 atomic_set(&r10_bio->remaining, 1);
2042
2043 /* find the first device with a block */
2044 for (i=0; i<conf->copies; i++)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002045 if (!r10_bio->devs[i].bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 break;
2047
2048 if (i == conf->copies)
2049 goto done;
2050
2051 first = i;
2052 fbio = r10_bio->devs[i].bio;
2053
majianpengf4380a92012-04-12 16:04:47 +10002054 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08002056 for (i=0 ; i < conf->copies ; i++) {
2057 int j, d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002060
2061 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002063 if (i == first)
2064 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002065 if (!r10_bio->devs[i].bio->bi_error) {
NeilBrown0eb3ff12006-01-06 00:20:29 -08002066 /* We know that the bi_io_vec layout is the same for
2067 * both 'first' and 'i', so we just compare them.
2068 * All vec entries are PAGE_SIZE;
2069 */
NeilBrown7bb23c42013-07-16 16:50:47 +10002070 int sectors = r10_bio->sectors;
2071 for (j = 0; j < vcnt; j++) {
2072 int len = PAGE_SIZE;
2073 if (sectors < (len / 512))
2074 len = sectors * 512;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002075 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
2076 page_address(tbio->bi_io_vec[j].bv_page),
NeilBrown7bb23c42013-07-16 16:50:47 +10002077 len))
NeilBrown0eb3ff12006-01-06 00:20:29 -08002078 break;
NeilBrown7bb23c42013-07-16 16:50:47 +10002079 sectors -= len/512;
2080 }
NeilBrown0eb3ff12006-01-06 00:20:29 -08002081 if (j == vcnt)
2082 continue;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002083 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
NeilBrownf84ee362011-07-28 11:39:25 +10002084 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2085 /* Don't fix anything. */
2086 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08002087 }
NeilBrownf84ee362011-07-28 11:39:25 +10002088 /* Ok, we need to write this bio, either to correct an
2089 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 * First we need to fixup bv_offset, bv_len and
2091 * bi_vecs, as the read request might have corrupted these
2092 */
Kent Overstreet8be185f2012-09-06 14:14:43 -07002093 bio_reset(tbio);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 tbio->bi_vcnt = vcnt;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002096 tbio->bi_iter.bi_size = r10_bio->sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 tbio->bi_rw = WRITE;
2098 tbio->bi_private = r10_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002099 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 tbio->bi_end_io = end_sync_write;
2101
Kent Overstreetc31df252015-05-06 23:34:20 -07002102 bio_copy_data(tbio, fbio);
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 d = r10_bio->devs[i].devnum;
2105 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2106 atomic_inc(&r10_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002107 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Kent Overstreet4f024f32013-10-11 15:44:27 -07002109 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2111 generic_make_request(tbio);
2112 }
2113
NeilBrown9ad1aef2011-12-23 10:17:55 +11002114 /* Now write out to any replacement devices
2115 * that are active
2116 */
2117 for (i = 0; i < conf->copies; i++) {
Kent Overstreetc31df252015-05-06 23:34:20 -07002118 int d;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002119
2120 tbio = r10_bio->devs[i].repl_bio;
2121 if (!tbio || !tbio->bi_end_io)
2122 continue;
2123 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2124 && r10_bio->devs[i].bio != fbio)
Kent Overstreetc31df252015-05-06 23:34:20 -07002125 bio_copy_data(tbio, fbio);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002126 d = r10_bio->devs[i].devnum;
2127 atomic_inc(&r10_bio->remaining);
2128 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002129 bio_sectors(tbio));
NeilBrown9ad1aef2011-12-23 10:17:55 +11002130 generic_make_request(tbio);
2131 }
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133done:
2134 if (atomic_dec_and_test(&r10_bio->remaining)) {
2135 md_done_sync(mddev, r10_bio->sectors, 1);
2136 put_buf(r10_bio);
2137 }
2138}
2139
2140/*
2141 * Now for the recovery code.
2142 * Recovery happens across physical sectors.
2143 * We recover all non-is_sync drives by finding the virtual address of
2144 * each, and then choose a working drive that also has that virt address.
2145 * There is a separate r10_bio for each non-in_sync drive.
2146 * Only the first two slots are in use. The first for reading,
2147 * The second for writing.
2148 *
2149 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11002150static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10002151{
2152 /* We got a read error during recovery.
2153 * We repeat the read in smaller page-sized sections.
2154 * If a read succeeds, write it to the new device or record
2155 * a bad block if we cannot.
2156 * If a read fails, record a bad block on both old and
2157 * new devices.
2158 */
NeilBrownfd01b882011-10-11 16:47:53 +11002159 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002160 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10002161 struct bio *bio = r10_bio->devs[0].bio;
2162 sector_t sect = 0;
2163 int sectors = r10_bio->sectors;
2164 int idx = 0;
2165 int dr = r10_bio->devs[0].devnum;
2166 int dw = r10_bio->devs[1].devnum;
2167
2168 while (sectors) {
2169 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002170 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002171 sector_t addr;
2172 int ok;
2173
2174 if (s > (PAGE_SIZE>>9))
2175 s = PAGE_SIZE >> 9;
2176
2177 rdev = conf->mirrors[dr].rdev;
2178 addr = r10_bio->devs[0].addr + sect,
2179 ok = sync_page_io(rdev,
2180 addr,
2181 s << 9,
2182 bio->bi_io_vec[idx].bv_page,
2183 READ, false);
2184 if (ok) {
2185 rdev = conf->mirrors[dw].rdev;
2186 addr = r10_bio->devs[1].addr + sect;
2187 ok = sync_page_io(rdev,
2188 addr,
2189 s << 9,
2190 bio->bi_io_vec[idx].bv_page,
2191 WRITE, false);
NeilBrownb7044d42011-12-23 10:17:56 +11002192 if (!ok) {
NeilBrown5e570282011-07-28 11:39:25 +10002193 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002194 if (!test_and_set_bit(WantReplacement,
2195 &rdev->flags))
2196 set_bit(MD_RECOVERY_NEEDED,
2197 &rdev->mddev->recovery);
2198 }
NeilBrown5e570282011-07-28 11:39:25 +10002199 }
2200 if (!ok) {
2201 /* We don't worry if we cannot set a bad block -
2202 * it really is bad so there is no loss in not
2203 * recording it yet
2204 */
2205 rdev_set_badblocks(rdev, addr, s, 0);
2206
2207 if (rdev != conf->mirrors[dw].rdev) {
2208 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11002209 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10002210 addr = r10_bio->devs[1].addr + sect;
2211 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2212 if (!ok) {
2213 /* just abort the recovery */
2214 printk(KERN_NOTICE
2215 "md/raid10:%s: recovery aborted"
2216 " due to read error\n",
2217 mdname(mddev));
2218
2219 conf->mirrors[dw].recovery_disabled
2220 = mddev->recovery_disabled;
2221 set_bit(MD_RECOVERY_INTR,
2222 &mddev->recovery);
2223 break;
2224 }
2225 }
2226 }
2227
2228 sectors -= s;
2229 sect += s;
2230 idx++;
2231 }
2232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
NeilBrown9f2c9d12011-10-11 16:48:43 +11002234static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
NeilBrowne879a872011-10-11 16:49:02 +11002236 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10002237 int d;
NeilBrown24afd802011-12-23 10:17:55 +11002238 struct bio *wbio, *wbio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
NeilBrown5e570282011-07-28 11:39:25 +10002240 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2241 fix_recovery_read_error(r10_bio);
2242 end_sync_request(r10_bio);
2243 return;
2244 }
2245
Namhyung Kimc65060a2011-07-18 17:38:49 +10002246 /*
2247 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * and submit the write request
2249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 d = r10_bio->devs[1].devnum;
NeilBrown24afd802011-12-23 10:17:55 +11002251 wbio = r10_bio->devs[1].bio;
2252 wbio2 = r10_bio->devs[1].repl_bio;
NeilBrown0eb25bb2013-07-24 15:37:42 +10002253 /* Need to test wbio2->bi_end_io before we call
2254 * generic_make_request as if the former is NULL,
2255 * the latter is free to free wbio2.
2256 */
2257 if (wbio2 && !wbio2->bi_end_io)
2258 wbio2 = NULL;
NeilBrown24afd802011-12-23 10:17:55 +11002259 if (wbio->bi_end_io) {
2260 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002261 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
NeilBrown24afd802011-12-23 10:17:55 +11002262 generic_make_request(wbio);
2263 }
NeilBrown0eb25bb2013-07-24 15:37:42 +10002264 if (wbio2) {
NeilBrown24afd802011-12-23 10:17:55 +11002265 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2266 md_sync_acct(conf->mirrors[d].replacement->bdev,
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002267 bio_sectors(wbio2));
NeilBrown24afd802011-12-23 10:17:55 +11002268 generic_make_request(wbio2);
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272/*
Robert Becker1e509152009-12-14 12:49:58 +11002273 * Used by fix_read_error() to decay the per rdev read_errors.
2274 * We halve the read error count for every hour that has elapsed
2275 * since the last recorded read error.
2276 *
2277 */
NeilBrownfd01b882011-10-11 16:47:53 +11002278static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11002279{
2280 struct timespec cur_time_mon;
2281 unsigned long hours_since_last;
2282 unsigned int read_errors = atomic_read(&rdev->read_errors);
2283
2284 ktime_get_ts(&cur_time_mon);
2285
2286 if (rdev->last_read_error.tv_sec == 0 &&
2287 rdev->last_read_error.tv_nsec == 0) {
2288 /* first time we've seen a read error */
2289 rdev->last_read_error = cur_time_mon;
2290 return;
2291 }
2292
2293 hours_since_last = (cur_time_mon.tv_sec -
2294 rdev->last_read_error.tv_sec) / 3600;
2295
2296 rdev->last_read_error = cur_time_mon;
2297
2298 /*
2299 * if hours_since_last is > the number of bits in read_errors
2300 * just set read errors to 0. We do this to avoid
2301 * overflowing the shift of read_errors by hours_since_last.
2302 */
2303 if (hours_since_last >= 8 * sizeof(read_errors))
2304 atomic_set(&rdev->read_errors, 0);
2305 else
2306 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2307}
2308
NeilBrown3cb03002011-10-11 16:45:26 +11002309static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10002310 int sectors, struct page *page, int rw)
2311{
2312 sector_t first_bad;
2313 int bad_sectors;
2314
2315 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2316 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2317 return -1;
2318 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2319 /* success */
2320 return 1;
NeilBrownb7044d42011-12-23 10:17:56 +11002321 if (rw == WRITE) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002322 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrownb7044d42011-12-23 10:17:56 +11002323 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2324 set_bit(MD_RECOVERY_NEEDED,
2325 &rdev->mddev->recovery);
2326 }
NeilBrown58c54fc2011-07-28 11:39:25 +10002327 /* need to record an error - either for the block or the device */
2328 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2329 md_error(rdev->mddev, rdev);
2330 return 0;
2331}
2332
Robert Becker1e509152009-12-14 12:49:58 +11002333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 * This is a kernel thread which:
2335 *
2336 * 1. Retries failed read operations on working mirrors.
2337 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07002338 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 */
2340
NeilBrowne879a872011-10-11 16:49:02 +11002341static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07002342{
2343 int sect = 0; /* Offset from r10_bio->sector */
2344 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11002345 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002346 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002347 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11002348
NeilBrown7c4e06f2011-05-11 14:53:17 +10002349 /* still own a reference to this rdev, so it cannot
2350 * have been cleared recently.
2351 */
2352 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11002353
NeilBrown7c4e06f2011-05-11 14:53:17 +10002354 if (test_bit(Faulty, &rdev->flags))
2355 /* drive has already been failed, just ignore any
2356 more fix_read_error() attempts */
2357 return;
2358
2359 check_decay_read_errors(mddev, rdev);
2360 atomic_inc(&rdev->read_errors);
2361 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2362 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11002363 bdevname(rdev->bdev, b);
2364
NeilBrown7c4e06f2011-05-11 14:53:17 +10002365 printk(KERN_NOTICE
2366 "md/raid10:%s: %s: Raid device exceeded "
2367 "read_error threshold [cur %d:max %d]\n",
2368 mdname(mddev), b,
2369 atomic_read(&rdev->read_errors), max_read_errors);
2370 printk(KERN_NOTICE
2371 "md/raid10:%s: %s: Failing raid device\n",
2372 mdname(mddev), b);
2373 md_error(mddev, conf->mirrors[d].rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002374 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
NeilBrown7c4e06f2011-05-11 14:53:17 +10002375 return;
Robert Becker1e509152009-12-14 12:49:58 +11002376 }
Robert Becker1e509152009-12-14 12:49:58 +11002377
NeilBrown6814d532006-10-03 01:15:45 -07002378 while(sectors) {
2379 int s = sectors;
2380 int sl = r10_bio->read_slot;
2381 int success = 0;
2382 int start;
2383
2384 if (s > (PAGE_SIZE>>9))
2385 s = PAGE_SIZE >> 9;
2386
2387 rcu_read_lock();
2388 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10002389 sector_t first_bad;
2390 int bad_sectors;
2391
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002392 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07002393 rdev = rcu_dereference(conf->mirrors[d].rdev);
2394 if (rdev &&
NeilBrown050b6612012-03-19 12:46:39 +11002395 !test_bit(Unmerged, &rdev->flags) &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10002396 test_bit(In_sync, &rdev->flags) &&
2397 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2398 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07002399 atomic_inc(&rdev->nr_pending);
2400 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11002401 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07002402 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002403 sect,
NeilBrown6814d532006-10-03 01:15:45 -07002404 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11002405 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07002406 rdev_dec_pending(rdev, mddev);
2407 rcu_read_lock();
2408 if (success)
2409 break;
2410 }
2411 sl++;
2412 if (sl == conf->copies)
2413 sl = 0;
2414 } while (!success && sl != r10_bio->read_slot);
2415 rcu_read_unlock();
2416
2417 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002418 /* Cannot read from anywhere, just mark the block
2419 * as bad on the first device to discourage future
2420 * reads.
2421 */
NeilBrown6814d532006-10-03 01:15:45 -07002422 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10002423 rdev = conf->mirrors[dn].rdev;
2424
2425 if (!rdev_set_badblocks(
2426 rdev,
2427 r10_bio->devs[r10_bio->read_slot].addr
2428 + sect,
NeilBrownfae8cc52012-02-14 11:10:10 +11002429 s, 0)) {
NeilBrown58c54fc2011-07-28 11:39:25 +10002430 md_error(mddev, rdev);
NeilBrownfae8cc52012-02-14 11:10:10 +11002431 r10_bio->devs[r10_bio->read_slot].bio
2432 = IO_BLOCKED;
2433 }
NeilBrown6814d532006-10-03 01:15:45 -07002434 break;
2435 }
2436
2437 start = sl;
2438 /* write it back and re-read */
2439 rcu_read_lock();
2440 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11002441 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002442
NeilBrown6814d532006-10-03 01:15:45 -07002443 if (sl==0)
2444 sl = conf->copies;
2445 sl--;
2446 d = r10_bio->devs[sl].devnum;
2447 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002448 if (!rdev ||
NeilBrown050b6612012-03-19 12:46:39 +11002449 test_bit(Unmerged, &rdev->flags) ||
NeilBrown1294b9c2011-07-28 11:39:23 +10002450 !test_bit(In_sync, &rdev->flags))
2451 continue;
2452
2453 atomic_inc(&rdev->nr_pending);
2454 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002455 if (r10_sync_page_io(rdev,
2456 r10_bio->devs[sl].addr +
2457 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002458 s, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10002459 == 0) {
2460 /* Well, this device is dead */
2461 printk(KERN_NOTICE
2462 "md/raid10:%s: read correction "
2463 "write failed"
2464 " (%d sectors at %llu on %s)\n",
2465 mdname(mddev), s,
2466 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002467 sect +
2468 choose_data_offset(r10_bio,
2469 rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002470 bdevname(rdev->bdev, b));
2471 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2472 "drive\n",
2473 mdname(mddev),
2474 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07002475 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002476 rdev_dec_pending(rdev, mddev);
2477 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002478 }
2479 sl = start;
2480 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10002481 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10002482
NeilBrown6814d532006-10-03 01:15:45 -07002483 if (sl==0)
2484 sl = conf->copies;
2485 sl--;
2486 d = r10_bio->devs[sl].devnum;
2487 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10002488 if (!rdev ||
2489 !test_bit(In_sync, &rdev->flags))
2490 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11002491
NeilBrown1294b9c2011-07-28 11:39:23 +10002492 atomic_inc(&rdev->nr_pending);
2493 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10002494 switch (r10_sync_page_io(rdev,
2495 r10_bio->devs[sl].addr +
2496 sect,
NeilBrown055d3742012-07-03 15:55:33 +10002497 s, conf->tmppage,
NeilBrown58c54fc2011-07-28 11:39:25 +10002498 READ)) {
2499 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10002500 /* Well, this device is dead */
2501 printk(KERN_NOTICE
2502 "md/raid10:%s: unable to read back "
2503 "corrected sectors"
2504 " (%d sectors at %llu on %s)\n",
2505 mdname(mddev), s,
2506 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002507 sect +
2508 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002509 bdevname(rdev->bdev, b));
2510 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2511 "drive\n",
2512 mdname(mddev),
2513 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10002514 break;
2515 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10002516 printk(KERN_INFO
2517 "md/raid10:%s: read error corrected"
2518 " (%d sectors at %llu on %s)\n",
2519 mdname(mddev), s,
2520 (unsigned long long)(
NeilBrownf8c9e742012-05-21 09:28:33 +10002521 sect +
2522 choose_data_offset(r10_bio, rdev)),
NeilBrown1294b9c2011-07-28 11:39:23 +10002523 bdevname(rdev->bdev, b));
2524 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07002525 }
NeilBrown1294b9c2011-07-28 11:39:23 +10002526
2527 rdev_dec_pending(rdev, mddev);
2528 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07002529 }
2530 rcu_read_unlock();
2531
2532 sectors -= s;
2533 sect += s;
2534 }
2535}
2536
NeilBrown9f2c9d12011-10-11 16:48:43 +11002537static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002538{
2539 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002540 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002541 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002542 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002543 /* bio has the data to be written to slot 'i' where
2544 * we just recently had a write error.
2545 * We repeatedly clone the bio and trim down to one block,
2546 * then try the write. Where the write fails we record
2547 * a bad block.
2548 * It is conceivable that the bio doesn't exactly align with
2549 * blocks. We must handle this.
2550 *
2551 * We currently own a reference to the rdev.
2552 */
2553
2554 int block_sectors;
2555 sector_t sector;
2556 int sectors;
2557 int sect_to_write = r10_bio->sectors;
2558 int ok = 1;
2559
2560 if (rdev->badblocks.shift < 0)
2561 return 0;
2562
NeilBrownf04ebb02015-02-16 14:51:54 +11002563 block_sectors = roundup(1 << rdev->badblocks.shift,
2564 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrownbd870a12011-07-28 11:39:24 +10002565 sector = r10_bio->sector;
2566 sectors = ((r10_bio->sector + block_sectors)
2567 & ~(sector_t)(block_sectors - 1))
2568 - sector;
2569
2570 while (sect_to_write) {
2571 struct bio *wbio;
2572 if (sectors > sect_to_write)
2573 sectors = sect_to_write;
2574 /* Write at 'sector' for 'sectors' */
2575 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002576 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2577 wbio->bi_iter.bi_sector = (r10_bio->devs[i].addr+
NeilBrownf8c9e742012-05-21 09:28:33 +10002578 choose_data_offset(r10_bio, rdev) +
NeilBrownbd870a12011-07-28 11:39:24 +10002579 (sector - r10_bio->sector));
2580 wbio->bi_bdev = rdev->bdev;
2581 if (submit_bio_wait(WRITE, wbio) == 0)
2582 /* Failure! */
2583 ok = rdev_set_badblocks(rdev, sector,
2584 sectors, 0)
2585 && ok;
2586
2587 bio_put(wbio);
2588 sect_to_write -= sectors;
2589 sector += sectors;
2590 sectors = block_sectors;
2591 }
2592 return ok;
2593}
2594
NeilBrown9f2c9d12011-10-11 16:48:43 +11002595static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002596{
2597 int slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002598 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002599 struct r10conf *conf = mddev->private;
NeilBrownabbf0982011-12-23 10:17:54 +11002600 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002601 char b[BDEVNAME_SIZE];
2602 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002603 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002604
2605 /* we got a read error. Maybe the drive is bad. Maybe just
2606 * the block and we can fix it.
2607 * We freeze all other IO, and try reading the block from
2608 * other devices. When we find one, we re-write
2609 * and check it that fixes the read error.
2610 * This is all done synchronously while the array is
2611 * frozen.
2612 */
NeilBrownfae8cc52012-02-14 11:10:10 +11002613 bio = r10_bio->devs[slot].bio;
2614 bdevname(bio->bi_bdev, b);
2615 bio_put(bio);
2616 r10_bio->devs[slot].bio = NULL;
2617
NeilBrown560f8e52011-07-28 11:39:23 +10002618 if (mddev->ro == 0) {
NeilBrowne2d59922013-06-12 11:01:22 +10002619 freeze_array(conf, 1);
NeilBrown560f8e52011-07-28 11:39:23 +10002620 fix_read_error(conf, mddev, r10_bio);
2621 unfreeze_array(conf);
NeilBrownfae8cc52012-02-14 11:10:10 +11002622 } else
2623 r10_bio->devs[slot].bio = IO_BLOCKED;
2624
NeilBrownabbf0982011-12-23 10:17:54 +11002625 rdev_dec_pending(rdev, mddev);
NeilBrown560f8e52011-07-28 11:39:23 +10002626
NeilBrown7399c312011-07-28 11:39:23 +10002627read_more:
NeilBrown96c3fd12011-12-23 10:17:54 +11002628 rdev = read_balance(conf, r10_bio, &max_sectors);
2629 if (rdev == NULL) {
NeilBrown560f8e52011-07-28 11:39:23 +10002630 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2631 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002632 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002633 (unsigned long long)r10_bio->sector);
2634 raid_end_bio_io(r10_bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002635 return;
2636 }
2637
2638 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown560f8e52011-07-28 11:39:23 +10002639 slot = r10_bio->read_slot;
NeilBrown560f8e52011-07-28 11:39:23 +10002640 printk_ratelimited(
2641 KERN_ERR
NeilBrown055d3742012-07-03 15:55:33 +10002642 "md/raid10:%s: %s: redirecting "
NeilBrown560f8e52011-07-28 11:39:23 +10002643 "sector %llu to another mirror\n",
2644 mdname(mddev),
2645 bdevname(rdev->bdev, b),
2646 (unsigned long long)r10_bio->sector);
2647 bio = bio_clone_mddev(r10_bio->master_bio,
2648 GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002649 bio_trim(bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002650 r10_bio->devs[slot].bio = bio;
NeilBrownabbf0982011-12-23 10:17:54 +11002651 r10_bio->devs[slot].rdev = rdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002652 bio->bi_iter.bi_sector = r10_bio->devs[slot].addr
NeilBrownf8c9e742012-05-21 09:28:33 +10002653 + choose_data_offset(r10_bio, rdev);
NeilBrown560f8e52011-07-28 11:39:23 +10002654 bio->bi_bdev = rdev->bdev;
2655 bio->bi_rw = READ | do_sync;
2656 bio->bi_private = r10_bio;
2657 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002658 if (max_sectors < r10_bio->sectors) {
2659 /* Drat - have to split this up more */
2660 struct bio *mbio = r10_bio->master_bio;
2661 int sectors_handled =
2662 r10_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002663 - mbio->bi_iter.bi_sector;
NeilBrown7399c312011-07-28 11:39:23 +10002664 r10_bio->sectors = max_sectors;
2665 spin_lock_irq(&conf->device_lock);
2666 if (mbio->bi_phys_segments == 0)
2667 mbio->bi_phys_segments = 2;
2668 else
2669 mbio->bi_phys_segments++;
2670 spin_unlock_irq(&conf->device_lock);
2671 generic_make_request(bio);
NeilBrown7399c312011-07-28 11:39:23 +10002672
2673 r10_bio = mempool_alloc(conf->r10bio_pool,
2674 GFP_NOIO);
2675 r10_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002676 r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown7399c312011-07-28 11:39:23 +10002677 r10_bio->state = 0;
2678 set_bit(R10BIO_ReadError,
2679 &r10_bio->state);
2680 r10_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002681 r10_bio->sector = mbio->bi_iter.bi_sector
NeilBrown7399c312011-07-28 11:39:23 +10002682 + sectors_handled;
2683
2684 goto read_more;
2685 } else
2686 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002687}
2688
NeilBrowne879a872011-10-11 16:49:02 +11002689static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002690{
2691 /* Some sort of write request has finished and it
2692 * succeeded in writing where we thought there was a
2693 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002694 * Or possibly if failed and we need to record
2695 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002696 */
2697 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002698 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002699
2700 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2701 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002702 for (m = 0; m < conf->copies; m++) {
2703 int dev = r10_bio->devs[m].devnum;
2704 rdev = conf->mirrors[dev].rdev;
2705 if (r10_bio->devs[m].bio == NULL)
2706 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002707 if (!r10_bio->devs[m].bio->bi_error) {
NeilBrown749c55e2011-07-28 11:39:24 +10002708 rdev_clear_badblocks(
2709 rdev,
2710 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002711 r10_bio->sectors, 0);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002712 } else {
2713 if (!rdev_set_badblocks(
2714 rdev,
2715 r10_bio->devs[m].addr,
2716 r10_bio->sectors, 0))
2717 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002718 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002719 rdev = conf->mirrors[dev].replacement;
2720 if (r10_bio->devs[m].repl_bio == NULL)
2721 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002722
2723 if (!r10_bio->devs[m].repl_bio->bi_error) {
NeilBrown9ad1aef2011-12-23 10:17:55 +11002724 rdev_clear_badblocks(
2725 rdev,
2726 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002727 r10_bio->sectors, 0);
NeilBrown9ad1aef2011-12-23 10:17:55 +11002728 } else {
2729 if (!rdev_set_badblocks(
2730 rdev,
2731 r10_bio->devs[m].addr,
2732 r10_bio->sectors, 0))
2733 md_error(conf->mddev, rdev);
2734 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002735 }
NeilBrown749c55e2011-07-28 11:39:24 +10002736 put_buf(r10_bio);
2737 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002738 for (m = 0; m < conf->copies; m++) {
2739 int dev = r10_bio->devs[m].devnum;
2740 struct bio *bio = r10_bio->devs[m].bio;
2741 rdev = conf->mirrors[dev].rdev;
2742 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002743 rdev_clear_badblocks(
2744 rdev,
2745 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002746 r10_bio->sectors, 0);
NeilBrown749c55e2011-07-28 11:39:24 +10002747 rdev_dec_pending(rdev, conf->mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002748 } else if (bio != NULL && bio->bi_error) {
NeilBrownbd870a12011-07-28 11:39:24 +10002749 if (!narrow_write_error(r10_bio, m)) {
2750 md_error(conf->mddev, rdev);
2751 set_bit(R10BIO_Degraded,
2752 &r10_bio->state);
2753 }
2754 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002755 }
NeilBrown475b0322011-12-23 10:17:55 +11002756 bio = r10_bio->devs[m].repl_bio;
2757 rdev = conf->mirrors[dev].replacement;
NeilBrown4ca40c22011-12-23 10:17:55 +11002758 if (rdev && bio == IO_MADE_GOOD) {
NeilBrown475b0322011-12-23 10:17:55 +11002759 rdev_clear_badblocks(
2760 rdev,
2761 r10_bio->devs[m].addr,
NeilBrownc6563a82012-05-21 09:27:00 +10002762 r10_bio->sectors, 0);
NeilBrown475b0322011-12-23 10:17:55 +11002763 rdev_dec_pending(rdev, conf->mddev);
2764 }
NeilBrownbd870a12011-07-28 11:39:24 +10002765 }
2766 if (test_bit(R10BIO_WriteError,
2767 &r10_bio->state))
2768 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002769 raid_end_bio_io(r10_bio);
2770 }
2771}
2772
Shaohua Li4ed87312012-10-11 13:34:00 +11002773static void raid10d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Shaohua Li4ed87312012-10-11 13:34:00 +11002775 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002776 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002778 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002780 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002784 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002786
NeilBrown0021b7b2012-07-31 09:08:14 +02002787 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002790 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002791 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002793 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002794 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002796 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 spin_unlock_irqrestore(&conf->device_lock, flags);
2798
2799 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002800 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002801 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2802 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002803 handle_write_completed(conf, r10_bio);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002804 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2805 reshape_request_write(mddev, r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002806 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002808 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002810 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002811 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002812 else {
2813 /* just a partial read to be scheduled from a
2814 * separate context
2815 */
2816 int slot = r10_bio->read_slot;
2817 generic_make_request(r10_bio->devs[slot].bio);
2818 }
NeilBrown4443ae12006-01-06 00:20:28 -08002819
NeilBrown1d9d5242009-10-16 15:55:32 +11002820 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002821 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2822 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002824 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
NeilBrowne879a872011-10-11 16:49:02 +11002827static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 int buffs;
NeilBrown69335ef2011-12-23 10:17:54 +11002830 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002833 BUG_ON(conf->r10buf_pool);
NeilBrown69335ef2011-12-23 10:17:54 +11002834 conf->have_replacement = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002835 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown69335ef2011-12-23 10:17:54 +11002836 if (conf->mirrors[i].replacement)
2837 conf->have_replacement = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2839 if (!conf->r10buf_pool)
2840 return -ENOMEM;
2841 conf->next_resync = 0;
2842 return 0;
2843}
2844
2845/*
2846 * perform a "sync" on one "block"
2847 *
2848 * We need to make sure that no normal I/O request - particularly write
2849 * requests - conflict with active sync requests.
2850 *
2851 * This is achieved by tracking pending requests and a 'barrier' concept
2852 * that can be installed to exclude normal IO requests.
2853 *
2854 * Resync and recovery are handled very differently.
2855 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2856 *
2857 * For resync, we iterate over virtual addresses, read all copies,
2858 * and update if there are differences. If only one copy is live,
2859 * skip it.
2860 * For recovery, we iterate over physical addresses, read a good
2861 * value for each non-in_sync drive, and over-write.
2862 *
2863 * So, for recovery we may have several outstanding complex requests for a
2864 * given address, one for each out-of-sync device. We model this by allocating
2865 * a number of r10_bio structures, one for each out-of-sync device.
2866 * As we setup these structures, we collect all bio's together into a list
2867 * which we then process collectively to add pages, and then process again
2868 * to pass to generic_make_request.
2869 *
2870 * The r10_bio structures are linked using a borrowed master_bio pointer.
2871 * This link is counted in ->remaining. When the r10_bio that points to NULL
2872 * has its remaining count decremented to 0, the whole complex operation
2873 * is complete.
2874 *
2875 */
2876
NeilBrownfd01b882011-10-11 16:47:53 +11002877static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrown09314792015-02-19 16:04:40 +11002878 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
NeilBrowne879a872011-10-11 16:49:02 +11002880 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002881 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 struct bio *biolist = NULL, *bio;
2883 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002885 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002886 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 sector_t sectors_skipped = 0;
2888 int chunks_skipped = 0;
NeilBrown5cf00fc2012-05-21 09:28:20 +10002889 sector_t chunk_mask = conf->geo.chunk_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 if (!conf->r10buf_pool)
2892 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002895 /*
2896 * Allow skipping a full rebuild for incremental assembly
2897 * of a clean array, like RAID1 does.
2898 */
2899 if (mddev->bitmap == NULL &&
2900 mddev->recovery_cp == MaxSector &&
NeilBrown13765122013-07-04 16:41:53 +10002901 mddev->reshape_position == MaxSector &&
2902 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002903 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown13765122013-07-04 16:41:53 +10002904 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002905 conf->fullsync == 0) {
2906 *skipped = 1;
NeilBrown13765122013-07-04 16:41:53 +10002907 return mddev->dev_sectors - sector_nr;
Martin Wilck7e83ccb2013-04-24 11:42:42 +10002908 }
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002911 max_sector = mddev->dev_sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10002912 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2913 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 max_sector = mddev->resync_max_sectors;
2915 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002916 /* If we aborted, we need to abort the
2917 * sync on the 'current' bitmap chucks (there can
2918 * be several when recovering multiple devices).
2919 * as we may have started syncing it but not finished.
2920 * We can find the current address in
2921 * mddev->curr_resync, but for recovery,
2922 * we need to convert that to several
2923 * virtual addresses.
2924 */
NeilBrown3ea7daa2012-05-22 13:53:47 +10002925 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2926 end_reshape(conf);
NeilBrownb3968552014-08-18 13:59:50 +10002927 close_sync(conf);
NeilBrown3ea7daa2012-05-22 13:53:47 +10002928 return 0;
2929 }
2930
NeilBrown6cce3b22006-01-06 00:20:16 -08002931 if (mddev->curr_resync < max_sector) { /* aborted */
2932 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2933 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2934 &sync_blocks, 1);
NeilBrown5cf00fc2012-05-21 09:28:20 +10002935 else for (i = 0; i < conf->geo.raid_disks; i++) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002936 sector_t sect =
2937 raid10_find_virt(conf, mddev->curr_resync, i);
2938 bitmap_end_sync(mddev->bitmap, sect,
2939 &sync_blocks, 1);
2940 }
NeilBrown9ad1aef2011-12-23 10:17:55 +11002941 } else {
2942 /* completed sync */
2943 if ((!mddev->bitmap || conf->fullsync)
2944 && conf->have_replacement
2945 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2946 /* Completed a full sync so the replacements
2947 * are now fully recovered.
2948 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002949 for (i = 0; i < conf->geo.raid_disks; i++)
NeilBrown9ad1aef2011-12-23 10:17:55 +11002950 if (conf->mirrors[i].replacement)
2951 conf->mirrors[i].replacement
2952 ->recovery_offset
2953 = MaxSector;
2954 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002955 conf->fullsync = 0;
NeilBrown9ad1aef2011-12-23 10:17:55 +11002956 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002957 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002959 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return sectors_skipped;
2961 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10002962
2963 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2964 return reshape_request(mddev, sector_nr, skipped);
2965
NeilBrown5cf00fc2012-05-21 09:28:20 +10002966 if (chunks_skipped >= conf->geo.raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* if there has been nothing to do on any drive,
2968 * then there is nothing to do at all..
2969 */
NeilBrown57afd892005-06-21 17:17:13 -07002970 *skipped = 1;
2971 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973
NeilBrownc6207272008-02-06 01:39:52 -08002974 if (max_sector > mddev->resync_max)
2975 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /* make sure whole request will fit in a chunk - if chunks
2978 * are meaningful
2979 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10002980 if (conf->geo.near_copies < conf->geo.raid_disks &&
2981 max_sector > (sector_nr | chunk_mask))
2982 max_sector = (sector_nr | chunk_mask) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 /* Again, very different code for resync and recovery.
2985 * Both must result in an r10bio with a list of bios that
2986 * have bi_end_io, bi_sector, bi_bdev set,
2987 * and bi_private set to the r10bio.
2988 * For recovery, we may actually create several r10bios
2989 * with 2 bios in each, that correspond to the bios in the main one.
2990 * In this case, the subordinate r10bios link back through a
2991 * borrowed master_bio pointer, and the counter in the master
2992 * includes a ref from each subordinate.
2993 */
2994 /* First, we decide what to do and set ->bi_end_io
2995 * To end_sync_read if we want to read, and
2996 * end_sync_write if we will want to write.
2997 */
2998
NeilBrown6cce3b22006-01-06 00:20:16 -08002999 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3001 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10003002 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 r10_bio = NULL;
3004
NeilBrown5cf00fc2012-05-21 09:28:20 +10003005 for (i = 0 ; i < conf->geo.raid_disks; i++) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003006 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11003007 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003008 sector_t sect;
3009 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10003010 int any_working;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003011 struct raid10_info *mirror = &conf->mirrors[i];
NeilBrownab9d47e2011-05-11 14:54:41 +10003012
NeilBrown24afd802011-12-23 10:17:55 +11003013 if ((mirror->rdev == NULL ||
3014 test_bit(In_sync, &mirror->rdev->flags))
3015 &&
3016 (mirror->replacement == NULL ||
3017 test_bit(Faulty,
3018 &mirror->replacement->flags)))
NeilBrownab9d47e2011-05-11 14:54:41 +10003019 continue;
3020
3021 still_degraded = 0;
3022 /* want to reconstruct this device */
3023 rb2 = r10_bio;
3024 sect = raid10_find_virt(conf, sector_nr, i);
NeilBrownfc448a12012-07-03 10:37:30 +10003025 if (sect >= mddev->resync_max_sectors) {
3026 /* last stripe is not complete - don't
3027 * try to recover this sector.
3028 */
3029 continue;
3030 }
NeilBrown24afd802011-12-23 10:17:55 +11003031 /* Unless we are doing a full sync, or a replacement
3032 * we only need to recover the block if it is set in
3033 * the bitmap
NeilBrownab9d47e2011-05-11 14:54:41 +10003034 */
3035 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3036 &sync_blocks, 1);
3037 if (sync_blocks < max_sync)
3038 max_sync = sync_blocks;
3039 if (!must_sync &&
NeilBrown24afd802011-12-23 10:17:55 +11003040 mirror->replacement == NULL &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003041 !conf->fullsync) {
3042 /* yep, skip the sync_blocks here, but don't assume
3043 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08003044 */
NeilBrownab9d47e2011-05-11 14:54:41 +10003045 chunks_skipped = -1;
3046 continue;
3047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
NeilBrownab9d47e2011-05-11 14:54:41 +10003049 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003050 r10_bio->state = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003051 raise_barrier(conf, rb2 != NULL);
3052 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
NeilBrownab9d47e2011-05-11 14:54:41 +10003054 r10_bio->master_bio = (struct bio*)rb2;
3055 if (rb2)
3056 atomic_inc(&rb2->remaining);
3057 r10_bio->mddev = mddev;
3058 set_bit(R10BIO_IsRecover, &r10_bio->state);
3059 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08003060
NeilBrownab9d47e2011-05-11 14:54:41 +10003061 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10003062
NeilBrownab9d47e2011-05-11 14:54:41 +10003063 /* Need to check if the array will still be
3064 * degraded
3065 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003066 for (j = 0; j < conf->geo.raid_disks; j++)
NeilBrownab9d47e2011-05-11 14:54:41 +10003067 if (conf->mirrors[j].rdev == NULL ||
3068 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
3069 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07003070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003072
3073 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3074 &sync_blocks, still_degraded);
3075
NeilBrowne875ece2011-07-28 11:39:24 +10003076 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10003077 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10003078 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10003079 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10003080 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11003081 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10003082 sector_t sector, first_bad;
3083 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10003084 if (!conf->mirrors[d].rdev ||
3085 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
3086 continue;
3087 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10003088 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10003089 rdev = conf->mirrors[d].rdev;
3090 sector = r10_bio->devs[j].addr;
3091
3092 if (is_badblock(rdev, sector, max_sync,
3093 &first_bad, &bad_sectors)) {
3094 if (first_bad > sector)
3095 max_sync = first_bad - sector;
3096 else {
3097 bad_sectors -= (sector
3098 - first_bad);
3099 if (max_sync > bad_sectors)
3100 max_sync = bad_sectors;
3101 continue;
3102 }
3103 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003104 bio = r10_bio->devs[0].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003105 bio_reset(bio);
NeilBrownab9d47e2011-05-11 14:54:41 +10003106 bio->bi_next = biolist;
3107 biolist = bio;
3108 bio->bi_private = r10_bio;
3109 bio->bi_end_io = end_sync_read;
3110 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10003111 from_addr = r10_bio->devs[j].addr;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003112 bio->bi_iter.bi_sector = from_addr +
3113 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003114 bio->bi_bdev = rdev->bdev;
3115 atomic_inc(&rdev->nr_pending);
3116 /* and we write to 'i' (if not in_sync) */
NeilBrownab9d47e2011-05-11 14:54:41 +10003117
3118 for (k=0; k<conf->copies; k++)
3119 if (r10_bio->devs[k].devnum == i)
3120 break;
3121 BUG_ON(k == conf->copies);
NeilBrown5e570282011-07-28 11:39:25 +10003122 to_addr = r10_bio->devs[k].addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003123 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10003124 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003125 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10003126 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10003127
NeilBrown24afd802011-12-23 10:17:55 +11003128 rdev = mirror->rdev;
3129 if (!test_bit(In_sync, &rdev->flags)) {
3130 bio = r10_bio->devs[1].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003131 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003132 bio->bi_next = biolist;
3133 biolist = bio;
3134 bio->bi_private = r10_bio;
3135 bio->bi_end_io = end_sync_write;
3136 bio->bi_rw = WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003137 bio->bi_iter.bi_sector = to_addr
NeilBrown24afd802011-12-23 10:17:55 +11003138 + rdev->data_offset;
3139 bio->bi_bdev = rdev->bdev;
3140 atomic_inc(&r10_bio->remaining);
3141 } else
3142 r10_bio->devs[1].bio->bi_end_io = NULL;
3143
3144 /* and maybe write to replacement */
3145 bio = r10_bio->devs[1].repl_bio;
3146 if (bio)
3147 bio->bi_end_io = NULL;
3148 rdev = mirror->replacement;
3149 /* Note: if rdev != NULL, then bio
3150 * cannot be NULL as r10buf_pool_alloc will
3151 * have allocated it.
3152 * So the second test here is pointless.
3153 * But it keeps semantic-checkers happy, and
3154 * this comment keeps human reviewers
3155 * happy.
3156 */
3157 if (rdev == NULL || bio == NULL ||
3158 test_bit(Faulty, &rdev->flags))
3159 break;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003160 bio_reset(bio);
NeilBrown24afd802011-12-23 10:17:55 +11003161 bio->bi_next = biolist;
3162 biolist = bio;
3163 bio->bi_private = r10_bio;
3164 bio->bi_end_io = end_sync_write;
3165 bio->bi_rw = WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003166 bio->bi_iter.bi_sector = to_addr +
3167 rdev->data_offset;
NeilBrown24afd802011-12-23 10:17:55 +11003168 bio->bi_bdev = rdev->bdev;
3169 atomic_inc(&r10_bio->remaining);
NeilBrownab9d47e2011-05-11 14:54:41 +10003170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003172 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10003173 /* Cannot recover, so abort the recovery or
3174 * record a bad block */
NeilBrowne875ece2011-07-28 11:39:24 +10003175 if (any_working) {
3176 /* problem is that there are bad blocks
3177 * on other device(s)
3178 */
3179 int k;
3180 for (k = 0; k < conf->copies; k++)
3181 if (r10_bio->devs[k].devnum == i)
3182 break;
NeilBrown24afd802011-12-23 10:17:55 +11003183 if (!test_bit(In_sync,
3184 &mirror->rdev->flags)
3185 && !rdev_set_badblocks(
3186 mirror->rdev,
3187 r10_bio->devs[k].addr,
3188 max_sync, 0))
3189 any_working = 0;
3190 if (mirror->replacement &&
3191 !rdev_set_badblocks(
3192 mirror->replacement,
NeilBrowne875ece2011-07-28 11:39:24 +10003193 r10_bio->devs[k].addr,
3194 max_sync, 0))
3195 any_working = 0;
3196 }
3197 if (!any_working) {
3198 if (!test_and_set_bit(MD_RECOVERY_INTR,
3199 &mddev->recovery))
3200 printk(KERN_INFO "md/raid10:%s: insufficient "
3201 "working devices for recovery.\n",
3202 mdname(mddev));
NeilBrown24afd802011-12-23 10:17:55 +11003203 mirror->recovery_disabled
NeilBrowne875ece2011-07-28 11:39:24 +10003204 = mddev->recovery_disabled;
3205 }
NeilBrowne8b84912014-01-06 10:35:34 +11003206 put_buf(r10_bio);
3207 if (rb2)
3208 atomic_dec(&rb2->remaining);
3209 r10_bio = rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10003210 break;
3211 }
3212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (biolist == NULL) {
3214 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11003215 struct r10bio *rb2 = r10_bio;
3216 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 rb2->master_bio = NULL;
3218 put_buf(rb2);
3219 }
3220 goto giveup;
3221 }
3222 } else {
3223 /* resync. Schedule a read for every block at this virt offset */
3224 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003225
NeilBrown78200d42009-02-25 13:18:47 +11003226 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3227
NeilBrown6cce3b22006-01-06 00:20:16 -08003228 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3229 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10003230 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3231 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08003232 /* We can skip this block */
3233 *skipped = 1;
3234 return sync_blocks + sectors_skipped;
3235 }
3236 if (sync_blocks < max_sync)
3237 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10003239 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 r10_bio->mddev = mddev;
3242 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08003243 raise_barrier(conf, 0);
3244 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 r10_bio->master_bio = NULL;
3247 r10_bio->sector = sector_nr;
3248 set_bit(R10BIO_IsSync, &r10_bio->state);
3249 raid10_find_phys(conf, r10_bio);
NeilBrown5cf00fc2012-05-21 09:28:20 +10003250 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
NeilBrown5cf00fc2012-05-21 09:28:20 +10003252 for (i = 0; i < conf->copies; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10003254 sector_t first_bad, sector;
3255 int bad_sectors;
3256
NeilBrown9ad1aef2011-12-23 10:17:55 +11003257 if (r10_bio->devs[i].repl_bio)
3258 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 bio = r10_bio->devs[i].bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003261 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003262 bio->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08003264 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10003266 sector = r10_bio->devs[i].addr;
3267 if (is_badblock(conf->mirrors[d].rdev,
3268 sector, max_sync,
3269 &first_bad, &bad_sectors)) {
3270 if (first_bad > sector)
3271 max_sync = first_bad - sector;
3272 else {
3273 bad_sectors -= (sector - first_bad);
3274 if (max_sync > bad_sectors)
Dan Carpenter91502f02012-10-11 14:20:58 +11003275 max_sync = bad_sectors;
NeilBrown40c356c2011-07-28 11:39:24 +10003276 continue;
3277 }
3278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3280 atomic_inc(&r10_bio->remaining);
3281 bio->bi_next = biolist;
3282 biolist = bio;
3283 bio->bi_private = r10_bio;
3284 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08003285 bio->bi_rw = READ;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003286 bio->bi_iter.bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 conf->mirrors[d].rdev->data_offset;
3288 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3289 count++;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003290
3291 if (conf->mirrors[d].replacement == NULL ||
3292 test_bit(Faulty,
3293 &conf->mirrors[d].replacement->flags))
3294 continue;
3295
3296 /* Need to set up for writing to the replacement */
3297 bio = r10_bio->devs[i].repl_bio;
Kent Overstreet8be185f2012-09-06 14:14:43 -07003298 bio_reset(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003299 bio->bi_error = -EIO;
NeilBrown9ad1aef2011-12-23 10:17:55 +11003300
3301 sector = r10_bio->devs[i].addr;
3302 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3303 bio->bi_next = biolist;
3304 biolist = bio;
3305 bio->bi_private = r10_bio;
3306 bio->bi_end_io = end_sync_write;
3307 bio->bi_rw = WRITE;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003308 bio->bi_iter.bi_sector = sector +
NeilBrown9ad1aef2011-12-23 10:17:55 +11003309 conf->mirrors[d].replacement->data_offset;
3310 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3311 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
3313
3314 if (count < 2) {
3315 for (i=0; i<conf->copies; i++) {
3316 int d = r10_bio->devs[i].devnum;
3317 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10003318 rdev_dec_pending(conf->mirrors[d].rdev,
3319 mddev);
NeilBrown9ad1aef2011-12-23 10:17:55 +11003320 if (r10_bio->devs[i].repl_bio &&
3321 r10_bio->devs[i].repl_bio->bi_end_io)
3322 rdev_dec_pending(
3323 conf->mirrors[d].replacement,
3324 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326 put_buf(r10_bio);
3327 biolist = NULL;
3328 goto giveup;
3329 }
3330 }
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08003333 if (sector_nr + max_sync < max_sector)
3334 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 do {
3336 struct page *page;
3337 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (sector_nr + (len>>9) > max_sector)
3339 len = (max_sector - sector_nr) << 9;
3340 if (len == 0)
3341 break;
3342 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10003343 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10003345 if (bio_add_page(bio, page, len, 0))
3346 continue;
3347
3348 /* stop here */
3349 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3350 for (bio2 = biolist;
3351 bio2 && bio2 != bio;
3352 bio2 = bio2->bi_next) {
3353 /* remove last page from this bio */
3354 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003355 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06003356 bio_clear_flag(bio2, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
NeilBrownab9d47e2011-05-11 14:54:41 +10003358 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
3360 nr_sectors += len>>9;
3361 sector_nr += len>>9;
3362 } while (biolist->bi_vcnt < RESYNC_PAGES);
3363 bio_full:
3364 r10_bio->sectors = nr_sectors;
3365
3366 while (biolist) {
3367 bio = biolist;
3368 biolist = biolist->bi_next;
3369
3370 bio->bi_next = NULL;
3371 r10_bio = bio->bi_private;
3372 r10_bio->sectors = nr_sectors;
3373
3374 if (bio->bi_end_io == end_sync_read) {
3375 md_sync_acct(bio->bi_bdev, nr_sectors);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003376 bio->bi_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 generic_make_request(bio);
3378 }
3379 }
3380
NeilBrown57afd892005-06-21 17:17:13 -07003381 if (sectors_skipped)
3382 /* pretend they weren't skipped, it makes
3383 * no important difference in this case
3384 */
3385 md_done_sync(mddev, sectors_skipped, 1);
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return sectors_skipped + nr_sectors;
3388 giveup:
3389 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10003390 * drives must be failed or in resync, all drives
3391 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 */
NeilBrown09b40682009-02-25 13:18:47 +11003393 if (sector_nr + max_sync < max_sector)
3394 max_sector = sector_nr + max_sync;
3395
3396 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 chunks_skipped ++;
3398 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Dan Williams80c3a6c2009-03-17 18:10:40 -07003402static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11003403raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07003404{
3405 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11003406 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003407
3408 if (!raid_disks)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003409 raid_disks = min(conf->geo.raid_disks,
3410 conf->prev.raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003411 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003412 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003413
NeilBrown5cf00fc2012-05-21 09:28:20 +10003414 size = sectors >> conf->geo.chunk_shift;
3415 sector_div(size, conf->geo.far_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003416 size = size * raid_disks;
NeilBrown5cf00fc2012-05-21 09:28:20 +10003417 sector_div(size, conf->geo.near_copies);
Dan Williams80c3a6c2009-03-17 18:10:40 -07003418
NeilBrown5cf00fc2012-05-21 09:28:20 +10003419 return size << conf->geo.chunk_shift;
Dan Williams80c3a6c2009-03-17 18:10:40 -07003420}
3421
NeilBrown6508fdb2012-05-17 10:08:45 +10003422static void calc_sectors(struct r10conf *conf, sector_t size)
3423{
3424 /* Calculate the number of sectors-per-device that will
3425 * actually be used, and set conf->dev_sectors and
3426 * conf->stride
3427 */
3428
NeilBrown5cf00fc2012-05-21 09:28:20 +10003429 size = size >> conf->geo.chunk_shift;
3430 sector_div(size, conf->geo.far_copies);
3431 size = size * conf->geo.raid_disks;
3432 sector_div(size, conf->geo.near_copies);
NeilBrown6508fdb2012-05-17 10:08:45 +10003433 /* 'size' is now the number of chunks in the array */
3434 /* calculate "used chunks per device" */
3435 size = size * conf->copies;
3436
3437 /* We need to round up when dividing by raid_disks to
3438 * get the stride size.
3439 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003440 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
NeilBrown6508fdb2012-05-17 10:08:45 +10003441
NeilBrown5cf00fc2012-05-21 09:28:20 +10003442 conf->dev_sectors = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003443
NeilBrown5cf00fc2012-05-21 09:28:20 +10003444 if (conf->geo.far_offset)
3445 conf->geo.stride = 1 << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003446 else {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003447 sector_div(size, conf->geo.far_copies);
3448 conf->geo.stride = size << conf->geo.chunk_shift;
NeilBrown6508fdb2012-05-17 10:08:45 +10003449 }
3450}
Trela, Maciejdab8b292010-03-08 16:02:45 +11003451
NeilBrowndeb200d2012-05-21 09:28:33 +10003452enum geo_type {geo_new, geo_old, geo_start};
3453static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3454{
3455 int nc, fc, fo;
3456 int layout, chunk, disks;
3457 switch (new) {
3458 case geo_old:
3459 layout = mddev->layout;
3460 chunk = mddev->chunk_sectors;
3461 disks = mddev->raid_disks - mddev->delta_disks;
3462 break;
3463 case geo_new:
3464 layout = mddev->new_layout;
3465 chunk = mddev->new_chunk_sectors;
3466 disks = mddev->raid_disks;
3467 break;
3468 default: /* avoid 'may be unused' warnings */
3469 case geo_start: /* new when starting reshape - raid_disks not
3470 * updated yet. */
3471 layout = mddev->new_layout;
3472 chunk = mddev->new_chunk_sectors;
3473 disks = mddev->raid_disks + mddev->delta_disks;
3474 break;
3475 }
Jonathan Brassow475901a2013-02-21 13:28:10 +11003476 if (layout >> 18)
NeilBrowndeb200d2012-05-21 09:28:33 +10003477 return -1;
3478 if (chunk < (PAGE_SIZE >> 9) ||
3479 !is_power_of_2(chunk))
3480 return -2;
3481 nc = layout & 255;
3482 fc = (layout >> 8) & 255;
3483 fo = layout & (1<<16);
3484 geo->raid_disks = disks;
3485 geo->near_copies = nc;
3486 geo->far_copies = fc;
3487 geo->far_offset = fo;
Jonathan Brassow475901a2013-02-21 13:28:10 +11003488 geo->far_set_size = (layout & (1<<17)) ? disks / fc : disks;
NeilBrowndeb200d2012-05-21 09:28:33 +10003489 geo->chunk_mask = chunk - 1;
3490 geo->chunk_shift = ffz(~chunk);
3491 return nc*fc;
3492}
3493
NeilBrowne879a872011-10-11 16:49:02 +11003494static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
NeilBrowne879a872011-10-11 16:49:02 +11003496 struct r10conf *conf = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003497 int err = -EINVAL;
NeilBrowndeb200d2012-05-21 09:28:33 +10003498 struct geom geo;
3499 int copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
NeilBrowndeb200d2012-05-21 09:28:33 +10003501 copies = setup_geo(&geo, mddev, geo_new);
3502
3503 if (copies == -2) {
NeilBrown128595e2010-05-03 14:47:14 +10003504 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3505 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3506 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
NeilBrown2604b702006-01-06 00:20:36 -08003509
NeilBrowndeb200d2012-05-21 09:28:33 +10003510 if (copies < 2 || copies > mddev->raid_disks) {
NeilBrown128595e2010-05-03 14:47:14 +10003511 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01003512 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 goto out;
3514 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11003515
3516 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11003517 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003518 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003520
NeilBrown3ea7daa2012-05-22 13:53:47 +10003521 /* FIXME calc properly */
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003522 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
NeilBrown78eaa0d2013-07-02 15:58:05 +10003523 max(0,-mddev->delta_disks)),
Trela, Maciejdab8b292010-03-08 16:02:45 +11003524 GFP_KERNEL);
3525 if (!conf->mirrors)
3526 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08003527
3528 conf->tmppage = alloc_page(GFP_KERNEL);
3529 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003530 goto out;
3531
NeilBrowndeb200d2012-05-21 09:28:33 +10003532 conf->geo = geo;
3533 conf->copies = copies;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003534 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3535 r10bio_pool_free, conf);
3536 if (!conf->r10bio_pool)
3537 goto out;
3538
NeilBrown6508fdb2012-05-17 10:08:45 +10003539 calc_sectors(conf, mddev->dev_sectors);
NeilBrown3ea7daa2012-05-22 13:53:47 +10003540 if (mddev->reshape_position == MaxSector) {
3541 conf->prev = conf->geo;
3542 conf->reshape_progress = MaxSector;
3543 } else {
3544 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3545 err = -EINVAL;
3546 goto out;
3547 }
3548 conf->reshape_progress = mddev->reshape_position;
3549 if (conf->prev.far_offset)
3550 conf->prev.stride = 1 << conf->prev.chunk_shift;
3551 else
3552 /* far_copies must be 1 */
3553 conf->prev.stride = conf->dev_sectors;
3554 }
Neil Browne7e72bf2008-05-14 16:05:54 -07003555 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003556 INIT_LIST_HEAD(&conf->retry_list);
3557
3558 spin_lock_init(&conf->resync_lock);
3559 init_waitqueue_head(&conf->wait_barrier);
3560
NeilBrown02326052012-07-03 15:56:52 +10003561 conf->thread = md_register_thread(raid10d, mddev, "raid10");
Trela, Maciejdab8b292010-03-08 16:02:45 +11003562 if (!conf->thread)
3563 goto out;
3564
Trela, Maciejdab8b292010-03-08 16:02:45 +11003565 conf->mddev = mddev;
3566 return conf;
3567
3568 out:
NeilBrown3ea7daa2012-05-22 13:53:47 +10003569 if (err == -ENOMEM)
3570 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3571 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003572 if (conf) {
3573 if (conf->r10bio_pool)
3574 mempool_destroy(conf->r10bio_pool);
3575 kfree(conf->mirrors);
3576 safe_put_page(conf->tmppage);
3577 kfree(conf);
3578 }
3579 return ERR_PTR(err);
3580}
3581
NeilBrownfd01b882011-10-11 16:47:53 +11003582static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003583{
NeilBrowne879a872011-10-11 16:49:02 +11003584 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003585 int i, disk_idx, chunk_size;
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003586 struct raid10_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11003587 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003588 sector_t size;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003589 sector_t min_offset_diff = 0;
3590 int first = 1;
Shaohua Li532a2a32012-10-11 13:30:52 +11003591 bool discard_supported = false;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003592
3593 if (mddev->private == NULL) {
3594 conf = setup_conf(mddev);
3595 if (IS_ERR(conf))
3596 return PTR_ERR(conf);
3597 mddev->private = conf;
3598 }
3599 conf = mddev->private;
3600 if (!conf)
3601 goto out;
3602
Trela, Maciejdab8b292010-03-08 16:02:45 +11003603 mddev->thread = conf->thread;
3604 conf->thread = NULL;
3605
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003606 chunk_size = mddev->chunk_sectors << 9;
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003607 if (mddev->queue) {
Shaohua Li532a2a32012-10-11 13:30:52 +11003608 blk_queue_max_discard_sectors(mddev->queue,
3609 mddev->chunk_sectors);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003610 blk_queue_max_write_same_sectors(mddev->queue, 0);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003611 blk_queue_io_min(mddev->queue, chunk_size);
3612 if (conf->geo.raid_disks % conf->geo.near_copies)
3613 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3614 else
3615 blk_queue_io_opt(mddev->queue, chunk_size *
3616 (conf->geo.raid_disks / conf->geo.near_copies));
3617 }
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10003618
NeilBrowndafb20f2012-03-19 12:46:39 +11003619 rdev_for_each(rdev, mddev) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10003620 long long diff;
NeilBrownaba336b2012-05-31 15:39:11 +10003621 struct request_queue *q;
NeilBrown34b343c2011-07-28 11:31:47 +10003622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 disk_idx = rdev->raid_disk;
NeilBrownf8c9e742012-05-21 09:28:33 +10003624 if (disk_idx < 0)
3625 continue;
3626 if (disk_idx >= conf->geo.raid_disks &&
3627 disk_idx >= conf->prev.raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 continue;
3629 disk = conf->mirrors + disk_idx;
3630
NeilBrown56a25592011-12-23 10:17:55 +11003631 if (test_bit(Replacement, &rdev->flags)) {
3632 if (disk->replacement)
3633 goto out_free_conf;
3634 disk->replacement = rdev;
3635 } else {
3636 if (disk->rdev)
3637 goto out_free_conf;
3638 disk->rdev = rdev;
3639 }
NeilBrownaba336b2012-05-31 15:39:11 +10003640 q = bdev_get_queue(rdev->bdev);
3641 if (q->merge_bvec_fn)
3642 mddev->merge_check_needed = 1;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003643 diff = (rdev->new_data_offset - rdev->data_offset);
3644 if (!mddev->reshape_backwards)
3645 diff = -diff;
3646 if (diff < 0)
3647 diff = 0;
3648 if (first || diff < min_offset_diff)
3649 min_offset_diff = diff;
NeilBrown56a25592011-12-23 10:17:55 +11003650
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003651 if (mddev->gendisk)
3652 disk_stack_limits(mddev->gendisk, rdev->bdev,
3653 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
3655 disk->head_position = 0;
Shaohua Li532a2a32012-10-11 13:30:52 +11003656
3657 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3658 discard_supported = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10003660
Jonathan Brassowed30be02012-10-31 11:42:30 +11003661 if (mddev->queue) {
3662 if (discard_supported)
3663 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3664 mddev->queue);
3665 else
3666 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3667 mddev->queue);
3668 }
NeilBrown6d508242005-09-09 16:24:03 -07003669 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10003670 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10003671 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07003672 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 goto out_free_conf;
3674 }
3675
NeilBrown3ea7daa2012-05-22 13:53:47 +10003676 if (conf->reshape_progress != MaxSector) {
3677 /* must ensure that shape change is supported */
3678 if (conf->geo.far_copies != 1 &&
3679 conf->geo.far_offset == 0)
3680 goto out_free_conf;
3681 if (conf->prev.far_copies != 1 &&
NeilBrown78eaa0d2013-07-02 15:58:05 +10003682 conf->prev.far_offset == 0)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003683 goto out_free_conf;
3684 }
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 mddev->degraded = 0;
NeilBrownf8c9e742012-05-21 09:28:33 +10003687 for (i = 0;
3688 i < conf->geo.raid_disks
3689 || i < conf->prev.raid_disks;
3690 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 disk = conf->mirrors + i;
3693
NeilBrown56a25592011-12-23 10:17:55 +11003694 if (!disk->rdev && disk->replacement) {
3695 /* The replacement is all we have - use it */
3696 disk->rdev = disk->replacement;
3697 disk->replacement = NULL;
3698 clear_bit(Replacement, &disk->rdev->flags);
3699 }
3700
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003701 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07003702 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 disk->head_position = 0;
3704 mddev->degraded++;
NeilBrown0b59bb62014-01-14 16:30:10 +11003705 if (disk->rdev &&
3706 disk->rdev->saved_raid_disk < 0)
Neil Brown8c2e8702008-06-28 08:30:52 +10003707 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
NeilBrownd890fa22011-10-26 11:54:39 +11003709 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
3711
Andre Noll8c6ac862009-06-18 08:48:06 +10003712 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10003713 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10003714 " -- starting background reconstruction\n",
3715 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10003717 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown5cf00fc2012-05-21 09:28:20 +10003718 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3719 conf->geo.raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 /*
3721 * Ok, everything is just fine now
3722 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11003723 mddev->dev_sectors = conf->dev_sectors;
3724 size = raid10_size(mddev, 0, 0);
3725 md_set_array_sectors(mddev, size);
3726 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003728 if (mddev->queue) {
NeilBrown5cf00fc2012-05-21 09:28:20 +10003729 int stripe = conf->geo.raid_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10003730 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jonathan Brassowcc4d1ef2012-07-31 10:03:53 +10003731
3732 /* Calculate max read-ahead size.
3733 * We need to readahead at least twice a whole stripe....
3734 * maybe...
3735 */
NeilBrown5cf00fc2012-05-21 09:28:20 +10003736 stripe /= conf->geo.near_copies;
NeilBrown3ea7daa2012-05-22 13:53:47 +10003737 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3738 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
3740
Martin K. Petersena91a2782011-03-17 11:11:05 +01003741 if (md_integrity_register(mddev))
3742 goto out_free_conf;
3743
NeilBrown3ea7daa2012-05-22 13:53:47 +10003744 if (conf->reshape_progress != MaxSector) {
3745 unsigned long before_length, after_length;
3746
3747 before_length = ((1 << conf->prev.chunk_shift) *
3748 conf->prev.far_copies);
3749 after_length = ((1 << conf->geo.chunk_shift) *
3750 conf->geo.far_copies);
3751
3752 if (max(before_length, after_length) > min_offset_diff) {
3753 /* This cannot work */
3754 printk("md/raid10: offset difference not enough to continue reshape\n");
3755 goto out_free_conf;
3756 }
3757 conf->offset_diff = min_offset_diff;
3758
3759 conf->reshape_safe = conf->reshape_progress;
3760 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3761 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3762 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3763 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3764 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3765 "reshape");
3766 }
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 return 0;
3769
3770out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10003771 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 if (conf->r10bio_pool)
3773 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08003774 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003775 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 kfree(conf);
3777 mddev->private = NULL;
3778out:
3779 return -EIO;
3780}
3781
NeilBrownafa0f552014-12-15 12:56:58 +11003782static void raid10_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
NeilBrownafa0f552014-12-15 12:56:58 +11003784 struct r10conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (conf->r10bio_pool)
3787 mempool_destroy(conf->r10bio_pool);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003788 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003789 kfree(conf->mirrors);
NeilBrownc4796e22014-08-23 20:19:26 +10003790 kfree(conf->mirrors_old);
3791 kfree(conf->mirrors_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
NeilBrownfd01b882011-10-11 16:47:53 +11003795static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b22006-01-06 00:20:16 -08003796{
NeilBrowne879a872011-10-11 16:49:02 +11003797 struct r10conf *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08003798
3799 switch(state) {
3800 case 1:
3801 raise_barrier(conf, 0);
3802 break;
3803 case 0:
3804 lower_barrier(conf);
3805 break;
3806 }
NeilBrown6cce3b22006-01-06 00:20:16 -08003807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
NeilBrown006a09a2012-03-19 12:46:40 +11003809static int raid10_resize(struct mddev *mddev, sector_t sectors)
3810{
3811 /* Resize of 'far' arrays is not supported.
3812 * For 'near' and 'offset' arrays we can set the
3813 * number of sectors used to be an appropriate multiple
3814 * of the chunk size.
3815 * For 'offset', this is far_copies*chunksize.
3816 * For 'near' the multiplier is the LCM of
3817 * near_copies and raid_disks.
3818 * So if far_copies > 1 && !far_offset, fail.
3819 * Else find LCM(raid_disks, near_copy)*far_copies and
3820 * multiply by chunk_size. Then round to this number.
3821 * This is mostly done by raid10_size()
3822 */
3823 struct r10conf *conf = mddev->private;
3824 sector_t oldsize, size;
3825
NeilBrownf8c9e742012-05-21 09:28:33 +10003826 if (mddev->reshape_position != MaxSector)
3827 return -EBUSY;
3828
NeilBrown5cf00fc2012-05-21 09:28:20 +10003829 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
NeilBrown006a09a2012-03-19 12:46:40 +11003830 return -EINVAL;
3831
3832 oldsize = raid10_size(mddev, 0, 0);
3833 size = raid10_size(mddev, sectors, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003834 if (mddev->external_size &&
3835 mddev->array_sectors > size)
NeilBrown006a09a2012-03-19 12:46:40 +11003836 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003837 if (mddev->bitmap) {
3838 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3839 if (ret)
3840 return ret;
3841 }
3842 md_set_array_sectors(mddev, size);
NeilBrown006a09a2012-03-19 12:46:40 +11003843 set_capacity(mddev->gendisk, mddev->array_sectors);
3844 revalidate_disk(mddev->gendisk);
3845 if (sectors > mddev->dev_sectors &&
3846 mddev->recovery_cp > oldsize) {
3847 mddev->recovery_cp = oldsize;
3848 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3849 }
NeilBrown6508fdb2012-05-17 10:08:45 +10003850 calc_sectors(conf, sectors);
3851 mddev->dev_sectors = conf->dev_sectors;
NeilBrown006a09a2012-03-19 12:46:40 +11003852 mddev->resync_max_sectors = size;
3853 return 0;
3854}
3855
NeilBrown53a6ab42015-02-12 14:09:57 +11003856static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003857{
NeilBrown3cb03002011-10-11 16:45:26 +11003858 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003859 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003860
3861 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003862 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3863 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003864 return ERR_PTR(-EINVAL);
3865 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003866 sector_div(size, devs);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003867
Trela, Maciejdab8b292010-03-08 16:02:45 +11003868 /* Set new parameters */
3869 mddev->new_level = 10;
3870 /* new layout: far_copies = 1, near_copies = 2 */
3871 mddev->new_layout = (1<<8) + 2;
3872 mddev->new_chunk_sectors = mddev->chunk_sectors;
3873 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003874 mddev->raid_disks *= 2;
3875 /* make sure it will be not marked as dirty */
3876 mddev->recovery_cp = MaxSector;
NeilBrown53a6ab42015-02-12 14:09:57 +11003877 mddev->dev_sectors = size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003878
3879 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003880 if (!IS_ERR(conf)) {
NeilBrowndafb20f2012-03-19 12:46:39 +11003881 rdev_for_each(rdev, mddev)
NeilBrown53a6ab42015-02-12 14:09:57 +11003882 if (rdev->raid_disk >= 0) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003883 rdev->new_raid_disk = rdev->raid_disk * 2;
NeilBrown53a6ab42015-02-12 14:09:57 +11003884 rdev->sectors = size;
3885 }
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003886 conf->barrier = 1;
3887 }
3888
Trela, Maciejdab8b292010-03-08 16:02:45 +11003889 return conf;
3890}
3891
NeilBrownfd01b882011-10-11 16:47:53 +11003892static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003893{
NeilBrowne373ab12011-10-11 16:48:59 +11003894 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003895
3896 /* raid10 can take over:
3897 * raid0 - providing it has only two drives
3898 */
3899 if (mddev->level == 0) {
3900 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003901 raid0_conf = mddev->private;
3902 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003903 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3904 " with more than one zone.\n",
3905 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003906 return ERR_PTR(-EINVAL);
3907 }
NeilBrown53a6ab42015-02-12 14:09:57 +11003908 return raid10_takeover_raid0(mddev,
3909 raid0_conf->strip_zone->zone_end,
3910 raid0_conf->strip_zone->nb_dev);
Trela, Maciejdab8b292010-03-08 16:02:45 +11003911 }
3912 return ERR_PTR(-EINVAL);
3913}
3914
NeilBrown3ea7daa2012-05-22 13:53:47 +10003915static int raid10_check_reshape(struct mddev *mddev)
3916{
3917 /* Called when there is a request to change
3918 * - layout (to ->new_layout)
3919 * - chunk size (to ->new_chunk_sectors)
3920 * - raid_disks (by delta_disks)
3921 * or when trying to restart a reshape that was ongoing.
3922 *
3923 * We need to validate the request and possibly allocate
3924 * space if that might be an issue later.
3925 *
3926 * Currently we reject any reshape of a 'far' mode array,
3927 * allow chunk size to change if new is generally acceptable,
3928 * allow raid_disks to increase, and allow
3929 * a switch between 'near' mode and 'offset' mode.
3930 */
3931 struct r10conf *conf = mddev->private;
3932 struct geom geo;
3933
3934 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3935 return -EINVAL;
3936
3937 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3938 /* mustn't change number of copies */
3939 return -EINVAL;
3940 if (geo.far_copies > 1 && !geo.far_offset)
3941 /* Cannot switch to 'far' mode */
3942 return -EINVAL;
3943
3944 if (mddev->array_sectors & geo.chunk_mask)
3945 /* not factor of array size */
3946 return -EINVAL;
3947
NeilBrown3ea7daa2012-05-22 13:53:47 +10003948 if (!enough(conf, -1))
3949 return -EINVAL;
3950
3951 kfree(conf->mirrors_new);
3952 conf->mirrors_new = NULL;
3953 if (mddev->delta_disks > 0) {
3954 /* allocate new 'mirrors' list */
3955 conf->mirrors_new = kzalloc(
Jonathan Brassowdc280d982012-07-31 10:03:52 +10003956 sizeof(struct raid10_info)
NeilBrown3ea7daa2012-05-22 13:53:47 +10003957 *(mddev->raid_disks +
3958 mddev->delta_disks),
3959 GFP_KERNEL);
3960 if (!conf->mirrors_new)
3961 return -ENOMEM;
3962 }
3963 return 0;
3964}
3965
3966/*
3967 * Need to check if array has failed when deciding whether to:
3968 * - start an array
3969 * - remove non-faulty devices
3970 * - add a spare
3971 * - allow a reshape
3972 * This determination is simple when no reshape is happening.
3973 * However if there is a reshape, we need to carefully check
3974 * both the before and after sections.
3975 * This is because some failed devices may only affect one
3976 * of the two sections, and some non-in_sync devices may
3977 * be insync in the section most affected by failed devices.
3978 */
3979static int calc_degraded(struct r10conf *conf)
3980{
3981 int degraded, degraded2;
3982 int i;
3983
3984 rcu_read_lock();
3985 degraded = 0;
3986 /* 'prev' section first */
3987 for (i = 0; i < conf->prev.raid_disks; i++) {
3988 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3989 if (!rdev || test_bit(Faulty, &rdev->flags))
3990 degraded++;
3991 else if (!test_bit(In_sync, &rdev->flags))
3992 /* When we can reduce the number of devices in
3993 * an array, this might not contribute to
3994 * 'degraded'. It does now.
3995 */
3996 degraded++;
3997 }
3998 rcu_read_unlock();
3999 if (conf->geo.raid_disks == conf->prev.raid_disks)
4000 return degraded;
4001 rcu_read_lock();
4002 degraded2 = 0;
4003 for (i = 0; i < conf->geo.raid_disks; i++) {
4004 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4005 if (!rdev || test_bit(Faulty, &rdev->flags))
4006 degraded2++;
4007 else if (!test_bit(In_sync, &rdev->flags)) {
4008 /* If reshape is increasing the number of devices,
4009 * this section has already been recovered, so
4010 * it doesn't contribute to degraded.
4011 * else it does.
4012 */
4013 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4014 degraded2++;
4015 }
4016 }
4017 rcu_read_unlock();
4018 if (degraded2 > degraded)
4019 return degraded2;
4020 return degraded;
4021}
4022
4023static int raid10_start_reshape(struct mddev *mddev)
4024{
4025 /* A 'reshape' has been requested. This commits
4026 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4027 * This also checks if there are enough spares and adds them
4028 * to the array.
4029 * We currently require enough spares to make the final
4030 * array non-degraded. We also require that the difference
4031 * between old and new data_offset - on each device - is
4032 * enough that we never risk over-writing.
4033 */
4034
4035 unsigned long before_length, after_length;
4036 sector_t min_offset_diff = 0;
4037 int first = 1;
4038 struct geom new;
4039 struct r10conf *conf = mddev->private;
4040 struct md_rdev *rdev;
4041 int spares = 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004042 int ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004043
4044 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4045 return -EBUSY;
4046
4047 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4048 return -EINVAL;
4049
4050 before_length = ((1 << conf->prev.chunk_shift) *
4051 conf->prev.far_copies);
4052 after_length = ((1 << conf->geo.chunk_shift) *
4053 conf->geo.far_copies);
4054
4055 rdev_for_each(rdev, mddev) {
4056 if (!test_bit(In_sync, &rdev->flags)
4057 && !test_bit(Faulty, &rdev->flags))
4058 spares++;
4059 if (rdev->raid_disk >= 0) {
4060 long long diff = (rdev->new_data_offset
4061 - rdev->data_offset);
4062 if (!mddev->reshape_backwards)
4063 diff = -diff;
4064 if (diff < 0)
4065 diff = 0;
4066 if (first || diff < min_offset_diff)
4067 min_offset_diff = diff;
4068 }
4069 }
4070
4071 if (max(before_length, after_length) > min_offset_diff)
4072 return -EINVAL;
4073
4074 if (spares < mddev->delta_disks)
4075 return -EINVAL;
4076
4077 conf->offset_diff = min_offset_diff;
4078 spin_lock_irq(&conf->device_lock);
4079 if (conf->mirrors_new) {
4080 memcpy(conf->mirrors_new, conf->mirrors,
Jonathan Brassowdc280d982012-07-31 10:03:52 +10004081 sizeof(struct raid10_info)*conf->prev.raid_disks);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004082 smp_mb();
NeilBrownc4796e22014-08-23 20:19:26 +10004083 kfree(conf->mirrors_old);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004084 conf->mirrors_old = conf->mirrors;
4085 conf->mirrors = conf->mirrors_new;
4086 conf->mirrors_new = NULL;
4087 }
4088 setup_geo(&conf->geo, mddev, geo_start);
4089 smp_mb();
4090 if (mddev->reshape_backwards) {
4091 sector_t size = raid10_size(mddev, 0, 0);
4092 if (size < mddev->array_sectors) {
4093 spin_unlock_irq(&conf->device_lock);
4094 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
4095 mdname(mddev));
4096 return -EINVAL;
4097 }
4098 mddev->resync_max_sectors = size;
4099 conf->reshape_progress = size;
4100 } else
4101 conf->reshape_progress = 0;
4102 spin_unlock_irq(&conf->device_lock);
4103
NeilBrownbb63a702012-05-22 13:55:28 +10004104 if (mddev->delta_disks && mddev->bitmap) {
4105 ret = bitmap_resize(mddev->bitmap,
4106 raid10_size(mddev, 0,
4107 conf->geo.raid_disks),
4108 0, 0);
4109 if (ret)
4110 goto abort;
4111 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004112 if (mddev->delta_disks > 0) {
4113 rdev_for_each(rdev, mddev)
4114 if (rdev->raid_disk < 0 &&
4115 !test_bit(Faulty, &rdev->flags)) {
4116 if (raid10_add_disk(mddev, rdev) == 0) {
4117 if (rdev->raid_disk >=
4118 conf->prev.raid_disks)
4119 set_bit(In_sync, &rdev->flags);
4120 else
4121 rdev->recovery_offset = 0;
4122
4123 if (sysfs_link_rdev(mddev, rdev))
4124 /* Failure here is OK */;
4125 }
4126 } else if (rdev->raid_disk >= conf->prev.raid_disks
4127 && !test_bit(Faulty, &rdev->flags)) {
4128 /* This is a spare that was manually added */
4129 set_bit(In_sync, &rdev->flags);
4130 }
4131 }
4132 /* When a reshape changes the number of devices,
4133 * ->degraded is measured against the larger of the
4134 * pre and post numbers.
4135 */
4136 spin_lock_irq(&conf->device_lock);
4137 mddev->degraded = calc_degraded(conf);
4138 spin_unlock_irq(&conf->device_lock);
4139 mddev->raid_disks = conf->geo.raid_disks;
4140 mddev->reshape_position = conf->reshape_progress;
4141 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4142
4143 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4144 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10004145 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004146 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4147 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4148
4149 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4150 "reshape");
4151 if (!mddev->sync_thread) {
NeilBrownbb63a702012-05-22 13:55:28 +10004152 ret = -EAGAIN;
4153 goto abort;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004154 }
4155 conf->reshape_checkpoint = jiffies;
4156 md_wakeup_thread(mddev->sync_thread);
4157 md_new_event(mddev);
4158 return 0;
NeilBrownbb63a702012-05-22 13:55:28 +10004159
4160abort:
4161 mddev->recovery = 0;
4162 spin_lock_irq(&conf->device_lock);
4163 conf->geo = conf->prev;
4164 mddev->raid_disks = conf->geo.raid_disks;
4165 rdev_for_each(rdev, mddev)
4166 rdev->new_data_offset = rdev->data_offset;
4167 smp_wmb();
4168 conf->reshape_progress = MaxSector;
4169 mddev->reshape_position = MaxSector;
4170 spin_unlock_irq(&conf->device_lock);
4171 return ret;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004172}
4173
4174/* Calculate the last device-address that could contain
4175 * any block from the chunk that includes the array-address 's'
4176 * and report the next address.
4177 * i.e. the address returned will be chunk-aligned and after
4178 * any data that is in the chunk containing 's'.
4179 */
4180static sector_t last_dev_address(sector_t s, struct geom *geo)
4181{
4182 s = (s | geo->chunk_mask) + 1;
4183 s >>= geo->chunk_shift;
4184 s *= geo->near_copies;
4185 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4186 s *= geo->far_copies;
4187 s <<= geo->chunk_shift;
4188 return s;
4189}
4190
4191/* Calculate the first device-address that could contain
4192 * any block from the chunk that includes the array-address 's'.
4193 * This too will be the start of a chunk
4194 */
4195static sector_t first_dev_address(sector_t s, struct geom *geo)
4196{
4197 s >>= geo->chunk_shift;
4198 s *= geo->near_copies;
4199 sector_div(s, geo->raid_disks);
4200 s *= geo->far_copies;
4201 s <<= geo->chunk_shift;
4202 return s;
4203}
4204
4205static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4206 int *skipped)
4207{
4208 /* We simply copy at most one chunk (smallest of old and new)
4209 * at a time, possibly less if that exceeds RESYNC_PAGES,
4210 * or we hit a bad block or something.
4211 * This might mean we pause for normal IO in the middle of
4212 * a chunk, but that is not a problem was mddev->reshape_position
4213 * can record any location.
4214 *
4215 * If we will want to write to a location that isn't
4216 * yet recorded as 'safe' (i.e. in metadata on disk) then
4217 * we need to flush all reshape requests and update the metadata.
4218 *
4219 * When reshaping forwards (e.g. to more devices), we interpret
4220 * 'safe' as the earliest block which might not have been copied
4221 * down yet. We divide this by previous stripe size and multiply
4222 * by previous stripe length to get lowest device offset that we
4223 * cannot write to yet.
4224 * We interpret 'sector_nr' as an address that we want to write to.
4225 * From this we use last_device_address() to find where we might
4226 * write to, and first_device_address on the 'safe' position.
4227 * If this 'next' write position is after the 'safe' position,
4228 * we must update the metadata to increase the 'safe' position.
4229 *
4230 * When reshaping backwards, we round in the opposite direction
4231 * and perform the reverse test: next write position must not be
4232 * less than current safe position.
4233 *
4234 * In all this the minimum difference in data offsets
4235 * (conf->offset_diff - always positive) allows a bit of slack,
4236 * so next can be after 'safe', but not by more than offset_disk
4237 *
4238 * We need to prepare all the bios here before we start any IO
4239 * to ensure the size we choose is acceptable to all devices.
4240 * The means one for each copy for write-out and an extra one for
4241 * read-in.
4242 * We store the read-in bio in ->master_bio and the others in
4243 * ->devs[x].bio and ->devs[x].repl_bio.
4244 */
4245 struct r10conf *conf = mddev->private;
4246 struct r10bio *r10_bio;
4247 sector_t next, safe, last;
4248 int max_sectors;
4249 int nr_sectors;
4250 int s;
4251 struct md_rdev *rdev;
4252 int need_flush = 0;
4253 struct bio *blist;
4254 struct bio *bio, *read_bio;
4255 int sectors_done = 0;
4256
4257 if (sector_nr == 0) {
4258 /* If restarting in the middle, skip the initial sectors */
4259 if (mddev->reshape_backwards &&
4260 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4261 sector_nr = (raid10_size(mddev, 0, 0)
4262 - conf->reshape_progress);
4263 } else if (!mddev->reshape_backwards &&
4264 conf->reshape_progress > 0)
4265 sector_nr = conf->reshape_progress;
4266 if (sector_nr) {
4267 mddev->curr_resync_completed = sector_nr;
4268 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4269 *skipped = 1;
4270 return sector_nr;
4271 }
4272 }
4273
4274 /* We don't use sector_nr to track where we are up to
4275 * as that doesn't work well for ->reshape_backwards.
4276 * So just use ->reshape_progress.
4277 */
4278 if (mddev->reshape_backwards) {
4279 /* 'next' is the earliest device address that we might
4280 * write to for this chunk in the new layout
4281 */
4282 next = first_dev_address(conf->reshape_progress - 1,
4283 &conf->geo);
4284
4285 /* 'safe' is the last device address that we might read from
4286 * in the old layout after a restart
4287 */
4288 safe = last_dev_address(conf->reshape_safe - 1,
4289 &conf->prev);
4290
4291 if (next + conf->offset_diff < safe)
4292 need_flush = 1;
4293
4294 last = conf->reshape_progress - 1;
4295 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4296 & conf->prev.chunk_mask);
4297 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4298 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4299 } else {
4300 /* 'next' is after the last device address that we
4301 * might write to for this chunk in the new layout
4302 */
4303 next = last_dev_address(conf->reshape_progress, &conf->geo);
4304
4305 /* 'safe' is the earliest device address that we might
4306 * read from in the old layout after a restart
4307 */
4308 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4309
4310 /* Need to update metadata if 'next' might be beyond 'safe'
4311 * as that would possibly corrupt data
4312 */
4313 if (next > safe + conf->offset_diff)
4314 need_flush = 1;
4315
4316 sector_nr = conf->reshape_progress;
4317 last = sector_nr | (conf->geo.chunk_mask
4318 & conf->prev.chunk_mask);
4319
4320 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4321 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4322 }
4323
4324 if (need_flush ||
4325 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4326 /* Need to update reshape_position in metadata */
4327 wait_barrier(conf);
4328 mddev->reshape_position = conf->reshape_progress;
4329 if (mddev->reshape_backwards)
4330 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4331 - conf->reshape_progress;
4332 else
4333 mddev->curr_resync_completed = conf->reshape_progress;
4334 conf->reshape_checkpoint = jiffies;
4335 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4336 md_wakeup_thread(mddev->thread);
4337 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004338 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4339 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4340 allow_barrier(conf);
4341 return sectors_done;
4342 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004343 conf->reshape_safe = mddev->reshape_position;
4344 allow_barrier(conf);
4345 }
4346
4347read_more:
4348 /* Now schedule reads for blocks from sector_nr to last */
4349 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrowncb8b12b2014-08-18 14:38:45 +10004350 r10_bio->state = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004351 raise_barrier(conf, sectors_done != 0);
4352 atomic_set(&r10_bio->remaining, 0);
4353 r10_bio->mddev = mddev;
4354 r10_bio->sector = sector_nr;
4355 set_bit(R10BIO_IsReshape, &r10_bio->state);
4356 r10_bio->sectors = last - sector_nr + 1;
4357 rdev = read_balance(conf, r10_bio, &max_sectors);
4358 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4359
4360 if (!rdev) {
4361 /* Cannot read from here, so need to record bad blocks
4362 * on all the target devices.
4363 */
4364 // FIXME
NeilBrowne337aea2014-08-18 14:48:54 +10004365 mempool_free(r10_bio, conf->r10buf_pool);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004366 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4367 return sectors_done;
4368 }
4369
4370 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4371
4372 read_bio->bi_bdev = rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004373 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
NeilBrown3ea7daa2012-05-22 13:53:47 +10004374 + rdev->data_offset);
4375 read_bio->bi_private = r10_bio;
4376 read_bio->bi_end_io = end_sync_read;
4377 read_bio->bi_rw = READ;
NeilBrownce0b0a42014-08-18 13:56:38 +10004378 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004379 read_bio->bi_error = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004380 read_bio->bi_vcnt = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004381 read_bio->bi_iter.bi_size = 0;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004382 r10_bio->master_bio = read_bio;
4383 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4384
4385 /* Now find the locations in the new layout */
4386 __raid10_find_phys(&conf->geo, r10_bio);
4387
4388 blist = read_bio;
4389 read_bio->bi_next = NULL;
4390
4391 for (s = 0; s < conf->copies*2; s++) {
4392 struct bio *b;
4393 int d = r10_bio->devs[s/2].devnum;
4394 struct md_rdev *rdev2;
4395 if (s&1) {
4396 rdev2 = conf->mirrors[d].replacement;
4397 b = r10_bio->devs[s/2].repl_bio;
4398 } else {
4399 rdev2 = conf->mirrors[d].rdev;
4400 b = r10_bio->devs[s/2].bio;
4401 }
4402 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4403 continue;
Kent Overstreet8be185f2012-09-06 14:14:43 -07004404
4405 bio_reset(b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004406 b->bi_bdev = rdev2->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004407 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4408 rdev2->new_data_offset;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004409 b->bi_private = r10_bio;
4410 b->bi_end_io = end_reshape_write;
4411 b->bi_rw = WRITE;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004412 b->bi_next = blist;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004413 blist = b;
4414 }
4415
4416 /* Now add as many pages as possible to all of these bios. */
4417
4418 nr_sectors = 0;
4419 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4420 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4421 int len = (max_sectors - s) << 9;
4422 if (len > PAGE_SIZE)
4423 len = PAGE_SIZE;
4424 for (bio = blist; bio ; bio = bio->bi_next) {
4425 struct bio *bio2;
4426 if (bio_add_page(bio, page, len, 0))
4427 continue;
4428
4429 /* Didn't fit, must stop */
4430 for (bio2 = blist;
4431 bio2 && bio2 != bio;
4432 bio2 = bio2->bi_next) {
4433 /* Remove last page from this bio */
4434 bio2->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004435 bio2->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004436 bio_clear_flag(bio2, BIO_SEG_VALID);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004437 }
4438 goto bio_full;
4439 }
4440 sector_nr += len >> 9;
4441 nr_sectors += len >> 9;
4442 }
4443bio_full:
4444 r10_bio->sectors = nr_sectors;
4445
4446 /* Now submit the read */
4447 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4448 atomic_inc(&r10_bio->remaining);
4449 read_bio->bi_next = NULL;
4450 generic_make_request(read_bio);
4451 sector_nr += nr_sectors;
4452 sectors_done += nr_sectors;
4453 if (sector_nr <= last)
4454 goto read_more;
4455
4456 /* Now that we have done the whole section we can
4457 * update reshape_progress
4458 */
4459 if (mddev->reshape_backwards)
4460 conf->reshape_progress -= sectors_done;
4461 else
4462 conf->reshape_progress += sectors_done;
4463
4464 return sectors_done;
4465}
4466
4467static void end_reshape_request(struct r10bio *r10_bio);
4468static int handle_reshape_read_error(struct mddev *mddev,
4469 struct r10bio *r10_bio);
4470static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4471{
4472 /* Reshape read completed. Hopefully we have a block
4473 * to write out.
4474 * If we got a read error then we do sync 1-page reads from
4475 * elsewhere until we find the data - or give up.
4476 */
4477 struct r10conf *conf = mddev->private;
4478 int s;
4479
4480 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4481 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4482 /* Reshape has been aborted */
4483 md_done_sync(mddev, r10_bio->sectors, 0);
4484 return;
4485 }
4486
4487 /* We definitely have the data in the pages, schedule the
4488 * writes.
4489 */
4490 atomic_set(&r10_bio->remaining, 1);
4491 for (s = 0; s < conf->copies*2; s++) {
4492 struct bio *b;
4493 int d = r10_bio->devs[s/2].devnum;
4494 struct md_rdev *rdev;
4495 if (s&1) {
4496 rdev = conf->mirrors[d].replacement;
4497 b = r10_bio->devs[s/2].repl_bio;
4498 } else {
4499 rdev = conf->mirrors[d].rdev;
4500 b = r10_bio->devs[s/2].bio;
4501 }
4502 if (!rdev || test_bit(Faulty, &rdev->flags))
4503 continue;
4504 atomic_inc(&rdev->nr_pending);
4505 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4506 atomic_inc(&r10_bio->remaining);
4507 b->bi_next = NULL;
4508 generic_make_request(b);
4509 }
4510 end_reshape_request(r10_bio);
4511}
4512
4513static void end_reshape(struct r10conf *conf)
4514{
4515 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4516 return;
4517
4518 spin_lock_irq(&conf->device_lock);
4519 conf->prev = conf->geo;
4520 md_finish_reshape(conf->mddev);
4521 smp_wmb();
4522 conf->reshape_progress = MaxSector;
4523 spin_unlock_irq(&conf->device_lock);
4524
4525 /* read-ahead size must cover two whole stripes, which is
4526 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4527 */
4528 if (conf->mddev->queue) {
4529 int stripe = conf->geo.raid_disks *
4530 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4531 stripe /= conf->geo.near_copies;
4532 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4533 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4534 }
4535 conf->fullsync = 0;
4536}
4537
NeilBrown3ea7daa2012-05-22 13:53:47 +10004538static int handle_reshape_read_error(struct mddev *mddev,
4539 struct r10bio *r10_bio)
4540{
4541 /* Use sync reads to get the blocks from somewhere else */
4542 int sectors = r10_bio->sectors;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004543 struct r10conf *conf = mddev->private;
NeilBrowne0ee7782012-08-18 09:51:42 +10004544 struct {
4545 struct r10bio r10_bio;
4546 struct r10dev devs[conf->copies];
4547 } on_stack;
4548 struct r10bio *r10b = &on_stack.r10_bio;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004549 int slot = 0;
4550 int idx = 0;
4551 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4552
NeilBrowne0ee7782012-08-18 09:51:42 +10004553 r10b->sector = r10_bio->sector;
4554 __raid10_find_phys(&conf->prev, r10b);
NeilBrown3ea7daa2012-05-22 13:53:47 +10004555
4556 while (sectors) {
4557 int s = sectors;
4558 int success = 0;
4559 int first_slot = slot;
4560
4561 if (s > (PAGE_SIZE >> 9))
4562 s = PAGE_SIZE >> 9;
4563
4564 while (!success) {
NeilBrowne0ee7782012-08-18 09:51:42 +10004565 int d = r10b->devs[slot].devnum;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004566 struct md_rdev *rdev = conf->mirrors[d].rdev;
4567 sector_t addr;
4568 if (rdev == NULL ||
4569 test_bit(Faulty, &rdev->flags) ||
4570 !test_bit(In_sync, &rdev->flags))
4571 goto failed;
4572
NeilBrowne0ee7782012-08-18 09:51:42 +10004573 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
NeilBrown3ea7daa2012-05-22 13:53:47 +10004574 success = sync_page_io(rdev,
4575 addr,
4576 s << 9,
4577 bvec[idx].bv_page,
4578 READ, false);
4579 if (success)
4580 break;
4581 failed:
4582 slot++;
4583 if (slot >= conf->copies)
4584 slot = 0;
4585 if (slot == first_slot)
4586 break;
4587 }
4588 if (!success) {
4589 /* couldn't read this block, must give up */
4590 set_bit(MD_RECOVERY_INTR,
4591 &mddev->recovery);
4592 return -EIO;
4593 }
4594 sectors -= s;
4595 idx++;
4596 }
4597 return 0;
4598}
4599
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004600static void end_reshape_write(struct bio *bio)
NeilBrown3ea7daa2012-05-22 13:53:47 +10004601{
NeilBrown3ea7daa2012-05-22 13:53:47 +10004602 struct r10bio *r10_bio = bio->bi_private;
4603 struct mddev *mddev = r10_bio->mddev;
4604 struct r10conf *conf = mddev->private;
4605 int d;
4606 int slot;
4607 int repl;
4608 struct md_rdev *rdev = NULL;
4609
4610 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4611 if (repl)
4612 rdev = conf->mirrors[d].replacement;
4613 if (!rdev) {
4614 smp_mb();
4615 rdev = conf->mirrors[d].rdev;
4616 }
4617
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004618 if (bio->bi_error) {
NeilBrown3ea7daa2012-05-22 13:53:47 +10004619 /* FIXME should record badblock */
4620 md_error(mddev, rdev);
4621 }
4622
4623 rdev_dec_pending(rdev, mddev);
4624 end_reshape_request(r10_bio);
4625}
4626
4627static void end_reshape_request(struct r10bio *r10_bio)
4628{
4629 if (!atomic_dec_and_test(&r10_bio->remaining))
4630 return;
4631 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4632 bio_put(r10_bio->master_bio);
4633 put_buf(r10_bio);
4634}
4635
4636static void raid10_finish_reshape(struct mddev *mddev)
4637{
4638 struct r10conf *conf = mddev->private;
4639
4640 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4641 return;
4642
4643 if (mddev->delta_disks > 0) {
4644 sector_t size = raid10_size(mddev, 0, 0);
4645 md_set_array_sectors(mddev, size);
4646 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4647 mddev->recovery_cp = mddev->resync_max_sectors;
4648 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4649 }
4650 mddev->resync_max_sectors = size;
4651 set_capacity(mddev->gendisk, mddev->array_sectors);
4652 revalidate_disk(mddev->gendisk);
NeilBrown63aced62012-05-22 13:55:33 +10004653 } else {
4654 int d;
4655 for (d = conf->geo.raid_disks ;
4656 d < conf->geo.raid_disks - mddev->delta_disks;
4657 d++) {
4658 struct md_rdev *rdev = conf->mirrors[d].rdev;
4659 if (rdev)
4660 clear_bit(In_sync, &rdev->flags);
4661 rdev = conf->mirrors[d].replacement;
4662 if (rdev)
4663 clear_bit(In_sync, &rdev->flags);
4664 }
NeilBrown3ea7daa2012-05-22 13:53:47 +10004665 }
4666 mddev->layout = mddev->new_layout;
4667 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4668 mddev->reshape_position = MaxSector;
4669 mddev->delta_disks = 0;
4670 mddev->reshape_backwards = 0;
4671}
4672
NeilBrown84fc4b52011-10-11 16:49:58 +11004673static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
4675 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08004676 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 .owner = THIS_MODULE,
4678 .make_request = make_request,
4679 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11004680 .free = raid10_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 .status = status,
4682 .error_handler = error,
4683 .hot_add_disk = raid10_add_disk,
4684 .hot_remove_disk= raid10_remove_disk,
4685 .spare_active = raid10_spare_active,
4686 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08004687 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07004688 .size = raid10_size,
NeilBrown006a09a2012-03-19 12:46:40 +11004689 .resize = raid10_resize,
Trela, Maciejdab8b292010-03-08 16:02:45 +11004690 .takeover = raid10_takeover,
NeilBrown3ea7daa2012-05-22 13:53:47 +10004691 .check_reshape = raid10_check_reshape,
4692 .start_reshape = raid10_start_reshape,
4693 .finish_reshape = raid10_finish_reshape,
NeilBrown5c675f82014-12-15 12:56:56 +11004694 .congested = raid10_congested,
NeilBrown64590f42014-12-15 12:56:57 +11004695 .mergeable_bvec = raid10_mergeable_bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696};
4697
4698static int __init raid_init(void)
4699{
NeilBrown2604b702006-01-06 00:20:36 -08004700 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
4703static void raid_exit(void)
4704{
NeilBrown2604b702006-01-06 00:20:36 -08004705 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
4708module_init(raid_init);
4709module_exit(raid_exit);
4710MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11004711MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004713MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08004714MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11004715
4716module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);