Merge tag 'devicetree-fixes-for-6.6-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / md / raid10.c
CommitLineData
af1a8899 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * raid10.c : Multiple Devices driver for Linux
4 *
5 * Copyright (C) 2000-2004 Neil Brown
6 *
7 * RAID-10 support for md.
8 *
25985edc 9 * Base on code in raid1.c. See raid1.c for further copyright information.
1da177e4
LT
10 */
11
5a0e3ad6 12#include <linux/slab.h>
25570727 13#include <linux/delay.h>
bff61975 14#include <linux/blkdev.h>
056075c7 15#include <linux/module.h>
bff61975 16#include <linux/seq_file.h>
8bda470e 17#include <linux/ratelimit.h>
3ea7daa5 18#include <linux/kthread.h>
afd75628 19#include <linux/raid/md_p.h>
109e3765 20#include <trace/events/block.h>
43b2e5d8 21#include "md.h"
ef740c37 22#include "raid10.h"
dab8b292 23#include "raid0.h"
935fe098 24#include "md-bitmap.h"
1da177e4
LT
25
26/*
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
29 * chunk_size
30 * raid_disks
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
c93983bf 33 * far_offset (stored in bit 16 of layout )
475901af 34 * use_far_sets (stored in bit 17 of layout )
8bce6d35 35 * use_far_sets_bugfixed (stored in bit 18 of layout )
1da177e4 36 *
475901af
JB
37 * The data to be stored is divided into chunks using chunksize. Each device
38 * is divided into far_copies sections. In each section, chunks are laid out
39 * in a style similar to raid0, but near_copies copies of each chunk is stored
40 * (each on a different drive). The starting device for each section is offset
41 * near_copies from the starting device of the previous section. Thus there
42 * are (near_copies * far_copies) of each chunk, and each is on a different
43 * drive. near_copies and far_copies must be at least one, and their product
44 * is at most raid_disks.
c93983bf
N
45 *
46 * If far_offset is true, then the far_copies are handled a bit differently.
475901af
JB
47 * The copies are still in different stripes, but instead of being very far
48 * apart on disk, there are adjacent stripes.
49 *
50 * The far and offset algorithms are handled slightly differently if
51 * 'use_far_sets' is true. In this case, the array's devices are grouped into
52 * sets that are (near_copies * far_copies) in size. The far copied stripes
53 * are still shifted by 'near_copies' devices, but this shifting stays confined
54 * to the set rather than the entire array. This is done to improve the number
55 * of device combinations that can fail without causing the array to fail.
56 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
57 * on a device):
58 * A B C D A B C D E
59 * ... ...
60 * D A B C E A B C D
61 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
62 * [A B] [C D] [A B] [C D E]
63 * |...| |...| |...| | ... |
64 * [B A] [D C] [B A] [E C D]
1da177e4
LT
65 */
66
e879a879
N
67static void allow_barrier(struct r10conf *conf);
68static void lower_barrier(struct r10conf *conf);
635f6416 69static int _enough(struct r10conf *conf, int previous, int ignore);
1919cbb2 70static int enough(struct r10conf *conf, int ignore);
3ea7daa5
N
71static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
72 int *skipped);
73static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
4246a0b6 74static void end_reshape_write(struct bio *bio);
3ea7daa5 75static void end_reshape(struct r10conf *conf);
0a27ec96 76
578b54ad
N
77#define raid10_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
79
fb0eb5df
ML
80#include "raid1-10.c"
81
b9b083f9
YK
82#define NULL_CMD
83#define cmd_before(conf, cmd) \
84 do { \
85 write_sequnlock_irq(&(conf)->resync_lock); \
86 cmd; \
87 } while (0)
88#define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock)
89
90#define wait_event_barrier_cmd(conf, cond, cmd) \
91 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \
92 cmd_after(conf))
93
94#define wait_event_barrier(conf, cond) \
95 wait_event_barrier_cmd(conf, cond, NULL_CMD)
96
f0250618
ML
97/*
98 * for resync bio, r10bio pointer can be retrieved from the per-bio
99 * 'struct resync_pages'.
100 */
101static inline struct r10bio *get_resync_r10bio(struct bio *bio)
102{
103 return get_resync_pages(bio)->raid_bio;
104}
105
dd0fc66f 106static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
1da177e4 107{
e879a879 108 struct r10conf *conf = data;
c2968285 109 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
1da177e4 110
69335ef3
N
111 /* allocate a r10bio with room for raid_disks entries in the
112 * bios array */
7eaceacc 113 return kzalloc(size, gfp_flags);
1da177e4
LT
114}
115
8db87912 116#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
0310fa21
N
117/* amount of memory to reserve for resync requests */
118#define RESYNC_WINDOW (1024*1024)
119/* maximum number of concurrent requests, memory permitting */
120#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
4b242e97 121#define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
8db87912 122#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
1da177e4
LT
123
124/*
125 * When performing a resync, we need to read and compare, so
126 * we need as many pages are there are copies.
127 * When performing a recovery, we need 2 bios, one for read,
128 * one for write (we recover only one drive per r10buf)
129 *
130 */
dd0fc66f 131static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
1da177e4 132{
e879a879 133 struct r10conf *conf = data;
9f2c9d12 134 struct r10bio *r10_bio;
1da177e4 135 struct bio *bio;
f0250618
ML
136 int j;
137 int nalloc, nalloc_rp;
138 struct resync_pages *rps;
1da177e4
LT
139
140 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
7eaceacc 141 if (!r10_bio)
1da177e4 142 return NULL;
1da177e4 143
3ea7daa5
N
144 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
145 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
1da177e4
LT
146 nalloc = conf->copies; /* resync */
147 else
148 nalloc = 2; /* recovery */
149
f0250618
ML
150 /* allocate once for all bios */
151 if (!conf->have_replacement)
152 nalloc_rp = nalloc;
153 else
154 nalloc_rp = nalloc * 2;
6da2ec56 155 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
f0250618
ML
156 if (!rps)
157 goto out_free_r10bio;
158
1da177e4
LT
159 /*
160 * Allocate bios.
161 */
162 for (j = nalloc ; j-- ; ) {
066ff571 163 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
1da177e4
LT
164 if (!bio)
165 goto out_free_bio;
066ff571 166 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
1da177e4 167 r10_bio->devs[j].bio = bio;
69335ef3
N
168 if (!conf->have_replacement)
169 continue;
066ff571 170 bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
69335ef3
N
171 if (!bio)
172 goto out_free_bio;
066ff571 173 bio_init(bio, NULL, bio->bi_inline_vecs, RESYNC_PAGES, 0);
69335ef3 174 r10_bio->devs[j].repl_bio = bio;
1da177e4
LT
175 }
176 /*
177 * Allocate RESYNC_PAGES data pages and attach them
178 * where needed.
179 */
f0250618 180 for (j = 0; j < nalloc; j++) {
69335ef3 181 struct bio *rbio = r10_bio->devs[j].repl_bio;
f0250618
ML
182 struct resync_pages *rp, *rp_repl;
183
184 rp = &rps[j];
185 if (rbio)
186 rp_repl = &rps[nalloc + j];
187
1da177e4 188 bio = r10_bio->devs[j].bio;
f0250618
ML
189
190 if (!j || test_bit(MD_RECOVERY_SYNC,
191 &conf->mddev->recovery)) {
192 if (resync_alloc_pages(rp, gfp_flags))
1da177e4 193 goto out_free_pages;
f0250618
ML
194 } else {
195 memcpy(rp, &rps[0], sizeof(*rp));
196 resync_get_all_pages(rp);
197 }
1da177e4 198
f0250618
ML
199 rp->raid_bio = r10_bio;
200 bio->bi_private = rp;
201 if (rbio) {
202 memcpy(rp_repl, rp, sizeof(*rp));
203 rbio->bi_private = rp_repl;
1da177e4
LT
204 }
205 }
206
207 return r10_bio;
208
209out_free_pages:
f0250618 210 while (--j >= 0)
45422b70 211 resync_free_pages(&rps[j]);
f0250618 212
5fdd2cf8 213 j = 0;
1da177e4 214out_free_bio:
5fdd2cf8 215 for ( ; j < nalloc; j++) {
216 if (r10_bio->devs[j].bio)
066ff571
CH
217 bio_uninit(r10_bio->devs[j].bio);
218 kfree(r10_bio->devs[j].bio);
69335ef3 219 if (r10_bio->devs[j].repl_bio)
066ff571
CH
220 bio_uninit(r10_bio->devs[j].repl_bio);
221 kfree(r10_bio->devs[j].repl_bio);
69335ef3 222 }
f0250618
ML
223 kfree(rps);
224out_free_r10bio:
c7afa803 225 rbio_pool_free(r10_bio, conf);
1da177e4
LT
226 return NULL;
227}
228
229static void r10buf_pool_free(void *__r10_bio, void *data)
230{
e879a879 231 struct r10conf *conf = data;
9f2c9d12 232 struct r10bio *r10bio = __r10_bio;
1da177e4 233 int j;
f0250618 234 struct resync_pages *rp = NULL;
1da177e4 235
f0250618 236 for (j = conf->copies; j--; ) {
1da177e4 237 struct bio *bio = r10bio->devs[j].bio;
f0250618 238
eb81b328
GJ
239 if (bio) {
240 rp = get_resync_pages(bio);
241 resync_free_pages(rp);
066ff571
CH
242 bio_uninit(bio);
243 kfree(bio);
eb81b328 244 }
f0250618 245
69335ef3 246 bio = r10bio->devs[j].repl_bio;
066ff571
CH
247 if (bio) {
248 bio_uninit(bio);
249 kfree(bio);
250 }
1da177e4 251 }
f0250618
ML
252
253 /* resync pages array stored in the 1st bio's .bi_private */
254 kfree(rp);
255
c7afa803 256 rbio_pool_free(r10bio, conf);
1da177e4
LT
257}
258
e879a879 259static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
1da177e4
LT
260{
261 int i;
262
c2968285 263 for (i = 0; i < conf->geo.raid_disks; i++) {
1da177e4 264 struct bio **bio = & r10_bio->devs[i].bio;
749c55e9 265 if (!BIO_SPECIAL(*bio))
1da177e4
LT
266 bio_put(*bio);
267 *bio = NULL;
69335ef3
N
268 bio = &r10_bio->devs[i].repl_bio;
269 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
270 bio_put(*bio);
271 *bio = NULL;
1da177e4
LT
272 }
273}
274
9f2c9d12 275static void free_r10bio(struct r10bio *r10_bio)
1da177e4 276{
e879a879 277 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 278
1da177e4 279 put_all_bios(conf, r10_bio);
afeee514 280 mempool_free(r10_bio, &conf->r10bio_pool);
1da177e4
LT
281}
282
9f2c9d12 283static void put_buf(struct r10bio *r10_bio)
1da177e4 284{
e879a879 285 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 286
afeee514 287 mempool_free(r10_bio, &conf->r10buf_pool);
1da177e4 288
0a27ec96 289 lower_barrier(conf);
1da177e4
LT
290}
291
0c0be98b
YK
292static void wake_up_barrier(struct r10conf *conf)
293{
294 if (wq_has_sleeper(&conf->wait_barrier))
295 wake_up(&conf->wait_barrier);
296}
297
9f2c9d12 298static void reschedule_retry(struct r10bio *r10_bio)
1da177e4
LT
299{
300 unsigned long flags;
fd01b88c 301 struct mddev *mddev = r10_bio->mddev;
e879a879 302 struct r10conf *conf = mddev->private;
1da177e4
LT
303
304 spin_lock_irqsave(&conf->device_lock, flags);
305 list_add(&r10_bio->retry_list, &conf->retry_list);
4443ae10 306 conf->nr_queued ++;
1da177e4
LT
307 spin_unlock_irqrestore(&conf->device_lock, flags);
308
388667be
AJ
309 /* wake up frozen array... */
310 wake_up(&conf->wait_barrier);
311
1da177e4
LT
312 md_wakeup_thread(mddev->thread);
313}
314
315/*
316 * raid_end_bio_io() is called when we have finished servicing a mirrored
317 * operation and are ready to return a success/failure code to the buffer
318 * cache layer.
319 */
9f2c9d12 320static void raid_end_bio_io(struct r10bio *r10_bio)
1da177e4
LT
321{
322 struct bio *bio = r10_bio->master_bio;
e879a879 323 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 324
856e08e2 325 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4e4cbee9 326 bio->bi_status = BLK_STS_IOERR;
fd16f2e8
N
327
328 bio_endio(bio);
329 /*
330 * Wake up any possible resync thread that waits for the device
331 * to go idle.
332 */
333 allow_barrier(conf);
334
1da177e4
LT
335 free_r10bio(r10_bio);
336}
337
338/*
339 * Update disk head position estimator based on IRQ completion info.
340 */
9f2c9d12 341static inline void update_head_pos(int slot, struct r10bio *r10_bio)
1da177e4 342{
e879a879 343 struct r10conf *conf = r10_bio->mddev->private;
1da177e4
LT
344
345 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
346 r10_bio->devs[slot].addr + (r10_bio->sectors);
347}
348
778ca018
NK
349/*
350 * Find the disk number which triggered given bio
351 */
e879a879 352static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
69335ef3 353 struct bio *bio, int *slotp, int *replp)
778ca018
NK
354{
355 int slot;
69335ef3 356 int repl = 0;
778ca018 357
c2968285 358 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
778ca018
NK
359 if (r10_bio->devs[slot].bio == bio)
360 break;
69335ef3
N
361 if (r10_bio->devs[slot].repl_bio == bio) {
362 repl = 1;
363 break;
364 }
365 }
778ca018 366
778ca018
NK
367 update_head_pos(slot, r10_bio);
368
749c55e9
N
369 if (slotp)
370 *slotp = slot;
69335ef3
N
371 if (replp)
372 *replp = repl;
778ca018
NK
373 return r10_bio->devs[slot].devnum;
374}
375
4246a0b6 376static void raid10_end_read_request(struct bio *bio)
1da177e4 377{
4e4cbee9 378 int uptodate = !bio->bi_status;
9f2c9d12 379 struct r10bio *r10_bio = bio->bi_private;
a0e764c5 380 int slot;
abbf098e 381 struct md_rdev *rdev;
e879a879 382 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 383
1da177e4 384 slot = r10_bio->read_slot;
abbf098e 385 rdev = r10_bio->devs[slot].rdev;
1da177e4
LT
386 /*
387 * this branch is our 'one mirror IO has finished' event handler:
388 */
4443ae10
N
389 update_head_pos(slot, r10_bio);
390
391 if (uptodate) {
1da177e4
LT
392 /*
393 * Set R10BIO_Uptodate in our master bio, so that
394 * we will return a good error code to the higher
395 * levels even if IO on some other mirrored buffer fails.
396 *
397 * The 'master' represents the composite IO operation to
398 * user-side. So if something waits for IO, then it will
399 * wait for the 'master' bio.
400 */
401 set_bit(R10BIO_Uptodate, &r10_bio->state);
fae8cc5e
N
402 } else {
403 /* If all other devices that store this block have
404 * failed, we want to return the error upwards rather
405 * than fail the last device. Here we redefine
406 * "uptodate" to mean "Don't want to retry"
407 */
635f6416
N
408 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
409 rdev->raid_disk))
fae8cc5e 410 uptodate = 1;
fae8cc5e
N
411 }
412 if (uptodate) {
1da177e4 413 raid_end_bio_io(r10_bio);
abbf098e 414 rdev_dec_pending(rdev, conf->mddev);
4443ae10 415 } else {
1da177e4 416 /*
7c4e06ff 417 * oops, read error - keep the refcount on the rdev
1da177e4 418 */
913cce5a 419 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n",
8bda470e 420 mdname(conf->mddev),
913cce5a 421 rdev->bdev,
8bda470e 422 (unsigned long long)r10_bio->sector);
856e08e2 423 set_bit(R10BIO_ReadError, &r10_bio->state);
1da177e4
LT
424 reschedule_retry(r10_bio);
425 }
1da177e4
LT
426}
427
9f2c9d12 428static void close_write(struct r10bio *r10_bio)
bd870a16
N
429{
430 /* clear the bitmap if all writes complete successfully */
e64e4018
AS
431 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
432 r10_bio->sectors,
433 !test_bit(R10BIO_Degraded, &r10_bio->state),
434 0);
bd870a16
N
435 md_write_end(r10_bio->mddev);
436}
437
9f2c9d12 438static void one_write_done(struct r10bio *r10_bio)
19d5f834
N
439{
440 if (atomic_dec_and_test(&r10_bio->remaining)) {
441 if (test_bit(R10BIO_WriteError, &r10_bio->state))
442 reschedule_retry(r10_bio);
443 else {
444 close_write(r10_bio);
445 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
446 reschedule_retry(r10_bio);
447 else
448 raid_end_bio_io(r10_bio);
449 }
450 }
451}
452
4246a0b6 453static void raid10_end_write_request(struct bio *bio)
1da177e4 454{
9f2c9d12 455 struct r10bio *r10_bio = bio->bi_private;
778ca018 456 int dev;
749c55e9 457 int dec_rdev = 1;
e879a879 458 struct r10conf *conf = r10_bio->mddev->private;
475b0321 459 int slot, repl;
4ca40c2c 460 struct md_rdev *rdev = NULL;
1919cbb2 461 struct bio *to_put = NULL;
579ed34f
SL
462 bool discard_error;
463
4e4cbee9 464 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
1da177e4 465
475b0321 466 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1da177e4 467
475b0321
N
468 if (repl)
469 rdev = conf->mirrors[dev].replacement;
4ca40c2c
N
470 if (!rdev) {
471 smp_rmb();
472 repl = 0;
475b0321 473 rdev = conf->mirrors[dev].rdev;
4ca40c2c 474 }
1da177e4
LT
475 /*
476 * this branch is our 'one mirror IO has finished' event handler:
477 */
4e4cbee9 478 if (bio->bi_status && !discard_error) {
475b0321
N
479 if (repl)
480 /* Never record new bad blocks to replacement,
481 * just fail it.
482 */
483 md_error(rdev->mddev, rdev);
484 else {
485 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
486 if (!test_and_set_bit(WantReplacement, &rdev->flags))
487 set_bit(MD_RECOVERY_NEEDED,
488 &rdev->mddev->recovery);
1919cbb2 489
475b0321 490 dec_rdev = 0;
1919cbb2
N
491 if (test_bit(FailFast, &rdev->flags) &&
492 (bio->bi_opf & MD_FAILFAST)) {
493 md_error(rdev->mddev, rdev);
7cee6d4e
YY
494 }
495
496 /*
497 * When the device is faulty, it is not necessary to
498 * handle write error.
7cee6d4e
YY
499 */
500 if (!test_bit(Faulty, &rdev->flags))
1919cbb2 501 set_bit(R10BIO_WriteError, &r10_bio->state);
7cee6d4e 502 else {
5ba03936
WS
503 /* Fail the request */
504 set_bit(R10BIO_Degraded, &r10_bio->state);
7cee6d4e
YY
505 r10_bio->devs[slot].bio = NULL;
506 to_put = bio;
507 dec_rdev = 1;
508 }
475b0321 509 }
749c55e9 510 } else {
1da177e4
LT
511 /*
512 * Set R10BIO_Uptodate in our master bio, so that
513 * we will return a good error code for to the higher
514 * levels even if IO on some other mirrored buffer fails.
515 *
516 * The 'master' represents the composite IO operation to
517 * user-side. So if something waits for IO, then it will
518 * wait for the 'master' bio.
519 */
749c55e9
N
520 sector_t first_bad;
521 int bad_sectors;
522
3056e3ae
AL
523 /*
524 * Do not set R10BIO_Uptodate if the current device is
525 * rebuilding or Faulty. This is because we cannot use
526 * such device for properly reading the data back (we could
527 * potentially use it, if the current write would have felt
528 * before rdev->recovery_offset, but for simplicity we don't
529 * check this here.
530 */
531 if (test_bit(In_sync, &rdev->flags) &&
532 !test_bit(Faulty, &rdev->flags))
533 set_bit(R10BIO_Uptodate, &r10_bio->state);
1da177e4 534
749c55e9 535 /* Maybe we can clear some bad blocks. */
475b0321 536 if (is_badblock(rdev,
749c55e9
N
537 r10_bio->devs[slot].addr,
538 r10_bio->sectors,
579ed34f 539 &first_bad, &bad_sectors) && !discard_error) {
749c55e9 540 bio_put(bio);
475b0321
N
541 if (repl)
542 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
543 else
544 r10_bio->devs[slot].bio = IO_MADE_GOOD;
749c55e9
N
545 dec_rdev = 0;
546 set_bit(R10BIO_MadeGood, &r10_bio->state);
547 }
548 }
549
1da177e4
LT
550 /*
551 *
552 * Let's see if all mirrored write operations have finished
553 * already.
554 */
19d5f834 555 one_write_done(r10_bio);
749c55e9 556 if (dec_rdev)
884162df 557 rdev_dec_pending(rdev, conf->mddev);
1919cbb2
N
558 if (to_put)
559 bio_put(to_put);
1da177e4
LT
560}
561
1da177e4
LT
562/*
563 * RAID10 layout manager
25985edc 564 * As well as the chunksize and raid_disks count, there are two
1da177e4
LT
565 * parameters: near_copies and far_copies.
566 * near_copies * far_copies must be <= raid_disks.
567 * Normally one of these will be 1.
568 * If both are 1, we get raid0.
569 * If near_copies == raid_disks, we get raid1.
570 *
25985edc 571 * Chunks are laid out in raid0 style with near_copies copies of the
1da177e4
LT
572 * first chunk, followed by near_copies copies of the next chunk and
573 * so on.
574 * If far_copies > 1, then after 1/far_copies of the array has been assigned
575 * as described above, we start again with a device offset of near_copies.
576 * So we effectively have another copy of the whole array further down all
577 * the drives, but with blocks on different drives.
578 * With this layout, and block is never stored twice on the one device.
579 *
580 * raid10_find_phys finds the sector offset of a given virtual sector
c93983bf 581 * on each device that it is on.
1da177e4
LT
582 *
583 * raid10_find_virt does the reverse mapping, from a device and a
584 * sector offset to a virtual address
585 */
586
f8c9e74f 587static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
1da177e4
LT
588{
589 int n,f;
590 sector_t sector;
591 sector_t chunk;
592 sector_t stripe;
593 int dev;
1da177e4 594 int slot = 0;
9a3152ab
JB
595 int last_far_set_start, last_far_set_size;
596
597 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
598 last_far_set_start *= geo->far_set_size;
599
600 last_far_set_size = geo->far_set_size;
601 last_far_set_size += (geo->raid_disks % geo->far_set_size);
1da177e4
LT
602
603 /* now calculate first sector/dev */
5cf00fcd
N
604 chunk = r10bio->sector >> geo->chunk_shift;
605 sector = r10bio->sector & geo->chunk_mask;
1da177e4 606
5cf00fcd 607 chunk *= geo->near_copies;
1da177e4 608 stripe = chunk;
5cf00fcd
N
609 dev = sector_div(stripe, geo->raid_disks);
610 if (geo->far_offset)
611 stripe *= geo->far_copies;
1da177e4 612
5cf00fcd 613 sector += stripe << geo->chunk_shift;
1da177e4
LT
614
615 /* and calculate all the others */
5cf00fcd 616 for (n = 0; n < geo->near_copies; n++) {
1da177e4 617 int d = dev;
475901af 618 int set;
1da177e4 619 sector_t s = sector;
1da177e4 620 r10bio->devs[slot].devnum = d;
4c0ca26b 621 r10bio->devs[slot].addr = s;
1da177e4
LT
622 slot++;
623
5cf00fcd 624 for (f = 1; f < geo->far_copies; f++) {
475901af 625 set = d / geo->far_set_size;
5cf00fcd 626 d += geo->near_copies;
475901af 627
9a3152ab
JB
628 if ((geo->raid_disks % geo->far_set_size) &&
629 (d > last_far_set_start)) {
630 d -= last_far_set_start;
631 d %= last_far_set_size;
632 d += last_far_set_start;
633 } else {
634 d %= geo->far_set_size;
635 d += geo->far_set_size * set;
636 }
5cf00fcd 637 s += geo->stride;
1da177e4
LT
638 r10bio->devs[slot].devnum = d;
639 r10bio->devs[slot].addr = s;
640 slot++;
641 }
642 dev++;
5cf00fcd 643 if (dev >= geo->raid_disks) {
1da177e4 644 dev = 0;
5cf00fcd 645 sector += (geo->chunk_mask + 1);
1da177e4
LT
646 }
647 }
f8c9e74f
N
648}
649
650static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
651{
652 struct geom *geo = &conf->geo;
653
654 if (conf->reshape_progress != MaxSector &&
655 ((r10bio->sector >= conf->reshape_progress) !=
656 conf->mddev->reshape_backwards)) {
657 set_bit(R10BIO_Previous, &r10bio->state);
658 geo = &conf->prev;
659 } else
660 clear_bit(R10BIO_Previous, &r10bio->state);
661
662 __raid10_find_phys(geo, r10bio);
1da177e4
LT
663}
664
e879a879 665static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
1da177e4
LT
666{
667 sector_t offset, chunk, vchunk;
f8c9e74f
N
668 /* Never use conf->prev as this is only called during resync
669 * or recovery, so reshape isn't happening
670 */
5cf00fcd 671 struct geom *geo = &conf->geo;
475901af
JB
672 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
673 int far_set_size = geo->far_set_size;
9a3152ab
JB
674 int last_far_set_start;
675
676 if (geo->raid_disks % geo->far_set_size) {
677 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
678 last_far_set_start *= geo->far_set_size;
679
680 if (dev >= last_far_set_start) {
681 far_set_size = geo->far_set_size;
682 far_set_size += (geo->raid_disks % geo->far_set_size);
683 far_set_start = last_far_set_start;
684 }
685 }
1da177e4 686
5cf00fcd
N
687 offset = sector & geo->chunk_mask;
688 if (geo->far_offset) {
c93983bf 689 int fc;
5cf00fcd
N
690 chunk = sector >> geo->chunk_shift;
691 fc = sector_div(chunk, geo->far_copies);
692 dev -= fc * geo->near_copies;
475901af
JB
693 if (dev < far_set_start)
694 dev += far_set_size;
c93983bf 695 } else {
5cf00fcd
N
696 while (sector >= geo->stride) {
697 sector -= geo->stride;
475901af
JB
698 if (dev < (geo->near_copies + far_set_start))
699 dev += far_set_size - geo->near_copies;
c93983bf 700 else
5cf00fcd 701 dev -= geo->near_copies;
c93983bf 702 }
5cf00fcd 703 chunk = sector >> geo->chunk_shift;
c93983bf 704 }
5cf00fcd
N
705 vchunk = chunk * geo->raid_disks + dev;
706 sector_div(vchunk, geo->near_copies);
707 return (vchunk << geo->chunk_shift) + offset;
1da177e4
LT
708}
709
1da177e4
LT
710/*
711 * This routine returns the disk from which the requested read should
712 * be done. There is a per-array 'next expected sequential IO' sector
713 * number - if this matches on the next IO then we use the last disk.
714 * There is also a per-disk 'last know head position' sector that is
715 * maintained from IRQ contexts, both the normal and the resync IO
716 * completion handlers update this position correctly. If there is no
717 * perfect sequential match then we pick the disk whose head is closest.
718 *
719 * If there are 2 mirrors in the same 2 devices, performance degrades
720 * because position is mirror, not device based.
721 *
722 * The rdev for the device selected will have nr_pending incremented.
723 */
724
725/*
726 * FIXME: possibly should rethink readbalancing and do it differently
727 * depending on near_copies / far_copies geometry.
728 */
96c3fd1f
N
729static struct md_rdev *read_balance(struct r10conf *conf,
730 struct r10bio *r10_bio,
731 int *max_sectors)
1da177e4 732{
af3a2cd6 733 const sector_t this_sector = r10_bio->sector;
56d99121 734 int disk, slot;
856e08e2
N
735 int sectors = r10_bio->sectors;
736 int best_good_sectors;
56d99121 737 sector_t new_distance, best_dist;
e9eeba28 738 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
56d99121 739 int do_balance;
e9eeba28
GJ
740 int best_dist_slot, best_pending_slot;
741 bool has_nonrot_disk = false;
742 unsigned int min_pending;
5cf00fcd 743 struct geom *geo = &conf->geo;
1da177e4
LT
744
745 raid10_find_phys(conf, r10_bio);
746 rcu_read_lock();
e9eeba28
GJ
747 best_dist_slot = -1;
748 min_pending = UINT_MAX;
749 best_dist_rdev = NULL;
750 best_pending_rdev = NULL;
56d99121 751 best_dist = MaxSector;
856e08e2 752 best_good_sectors = 0;
56d99121 753 do_balance = 1;
8d3ca83d 754 clear_bit(R10BIO_FailFast, &r10_bio->state);
1da177e4
LT
755 /*
756 * Check if we can balance. We can balance on the whole
6cce3b23
N
757 * device if no resync is going on (recovery is ok), or below
758 * the resync window. We take the first readable disk when
759 * above the resync window.
1da177e4 760 */
d4098c72
GJ
761 if ((conf->mddev->recovery_cp < MaxSector
762 && (this_sector + sectors >= conf->next_resync)) ||
763 (mddev_is_clustered(conf->mddev) &&
764 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
765 this_sector + sectors)))
56d99121 766 do_balance = 0;
1da177e4 767
56d99121 768 for (slot = 0; slot < conf->copies ; slot++) {
856e08e2
N
769 sector_t first_bad;
770 int bad_sectors;
771 sector_t dev_sector;
e9eeba28
GJ
772 unsigned int pending;
773 bool nonrot;
856e08e2 774
56d99121
N
775 if (r10_bio->devs[slot].bio == IO_BLOCKED)
776 continue;
1da177e4 777 disk = r10_bio->devs[slot].devnum;
abbf098e
N
778 rdev = rcu_dereference(conf->mirrors[disk].replacement);
779 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
2ae6aaf7
LN
780 r10_bio->devs[slot].addr + sectors >
781 rdev->recovery_offset) {
782 /*
783 * Read replacement first to prevent reading both rdev
784 * and replacement as NULL during replacement replace
785 * rdev.
786 */
787 smp_mb();
abbf098e 788 rdev = rcu_dereference(conf->mirrors[disk].rdev);
2ae6aaf7 789 }
050b6615 790 if (rdev == NULL ||
8ae12666 791 test_bit(Faulty, &rdev->flags))
abbf098e
N
792 continue;
793 if (!test_bit(In_sync, &rdev->flags) &&
794 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
56d99121
N
795 continue;
796
856e08e2
N
797 dev_sector = r10_bio->devs[slot].addr;
798 if (is_badblock(rdev, dev_sector, sectors,
799 &first_bad, &bad_sectors)) {
800 if (best_dist < MaxSector)
801 /* Already have a better slot */
802 continue;
803 if (first_bad <= dev_sector) {
804 /* Cannot read here. If this is the
805 * 'primary' device, then we must not read
806 * beyond 'bad_sectors' from another device.
807 */
808 bad_sectors -= (dev_sector - first_bad);
809 if (!do_balance && sectors > bad_sectors)
810 sectors = bad_sectors;
811 if (best_good_sectors > sectors)
812 best_good_sectors = sectors;
813 } else {
814 sector_t good_sectors =
815 first_bad - dev_sector;
816 if (good_sectors > best_good_sectors) {
817 best_good_sectors = good_sectors;
e9eeba28
GJ
818 best_dist_slot = slot;
819 best_dist_rdev = rdev;
856e08e2
N
820 }
821 if (!do_balance)
822 /* Must read from here */
823 break;
824 }
825 continue;
826 } else
827 best_good_sectors = sectors;
828
56d99121
N
829 if (!do_balance)
830 break;
1da177e4 831
10f0d2a5 832 nonrot = bdev_nonrot(rdev->bdev);
e9eeba28
GJ
833 has_nonrot_disk |= nonrot;
834 pending = atomic_read(&rdev->nr_pending);
835 if (min_pending > pending && nonrot) {
836 min_pending = pending;
837 best_pending_slot = slot;
838 best_pending_rdev = rdev;
839 }
840
841 if (best_dist_slot >= 0)
8d3ca83d
N
842 /* At least 2 disks to choose from so failfast is OK */
843 set_bit(R10BIO_FailFast, &r10_bio->state);
22dfdf52
N
844 /* This optimisation is debatable, and completely destroys
845 * sequential read speed for 'far copies' arrays. So only
846 * keep it for 'near' arrays, and review those later.
847 */
e9eeba28 848 if (geo->near_copies > 1 && !pending)
8d3ca83d 849 new_distance = 0;
8ed3a195
KS
850
851 /* for far > 1 always use the lowest address */
8d3ca83d 852 else if (geo->far_copies > 1)
56d99121 853 new_distance = r10_bio->devs[slot].addr;
8ed3a195 854 else
56d99121
N
855 new_distance = abs(r10_bio->devs[slot].addr -
856 conf->mirrors[disk].head_position);
e9eeba28 857
56d99121
N
858 if (new_distance < best_dist) {
859 best_dist = new_distance;
e9eeba28
GJ
860 best_dist_slot = slot;
861 best_dist_rdev = rdev;
1da177e4
LT
862 }
863 }
abbf098e 864 if (slot >= conf->copies) {
e9eeba28
GJ
865 if (has_nonrot_disk) {
866 slot = best_pending_slot;
867 rdev = best_pending_rdev;
868 } else {
869 slot = best_dist_slot;
870 rdev = best_dist_rdev;
871 }
abbf098e 872 }
1da177e4 873
56d99121 874 if (slot >= 0) {
56d99121 875 atomic_inc(&rdev->nr_pending);
56d99121
N
876 r10_bio->read_slot = slot;
877 } else
96c3fd1f 878 rdev = NULL;
1da177e4 879 rcu_read_unlock();
856e08e2 880 *max_sectors = best_good_sectors;
1da177e4 881
96c3fd1f 882 return rdev;
1da177e4
LT
883}
884
e879a879 885static void flush_pending_writes(struct r10conf *conf)
a35e63ef
N
886{
887 /* Any writes that have been queued but are awaiting
888 * bitmap updates get flushed here.
a35e63ef 889 */
a35e63ef
N
890 spin_lock_irq(&conf->device_lock);
891
892 if (conf->pending_bio_list.head) {
18022a1b 893 struct blk_plug plug;
a35e63ef 894 struct bio *bio;
18022a1b 895
a35e63ef 896 bio = bio_list_get(&conf->pending_bio_list);
a35e63ef 897 spin_unlock_irq(&conf->device_lock);
474beb57
N
898
899 /*
900 * As this is called in a wait_event() loop (see freeze_array),
901 * current->state might be TASK_UNINTERRUPTIBLE which will
902 * cause a warning when we prepare to wait again. As it is
903 * rare that this path is taken, it is perfectly safe to force
904 * us to go around the wait_event() loop again, so the warning
905 * is a false-positive. Silence the warning by resetting
906 * thread state
907 */
908 __set_current_state(TASK_RUNNING);
909
18022a1b 910 blk_start_plug(&plug);
9efcc2c3 911 raid1_prepare_flush_writes(conf->mddev->bitmap);
34db0cd6 912 wake_up(&conf->wait_barrier);
a35e63ef
N
913
914 while (bio) { /* submit pending writes */
915 struct bio *next = bio->bi_next;
8295efbe
YK
916
917 raid1_submit_write(bio);
a35e63ef 918 bio = next;
01044462 919 cond_resched();
a35e63ef 920 }
18022a1b 921 blk_finish_plug(&plug);
a35e63ef
N
922 } else
923 spin_unlock_irq(&conf->device_lock);
a35e63ef 924}
7eaceacc 925
0a27ec96
N
926/* Barriers....
927 * Sometimes we need to suspend IO while we do something else,
928 * either some resync/recovery, or reconfigure the array.
929 * To do this we raise a 'barrier'.
930 * The 'barrier' is a counter that can be raised multiple times
931 * to count how many activities are happening which preclude
932 * normal IO.
933 * We can only raise the barrier if there is no pending IO.
934 * i.e. if nr_pending == 0.
935 * We choose only to raise the barrier if no-one is waiting for the
936 * barrier to go down. This means that as soon as an IO request
937 * is ready, no other operations which require a barrier will start
938 * until the IO request has had a chance.
939 *
940 * So: regular IO calls 'wait_barrier'. When that returns there
941 * is no backgroup IO happening, It must arrange to call
942 * allow_barrier when it has finished its IO.
943 * backgroup IO calls must call raise_barrier. Once that returns
944 * there is no normal IO happeing. It must arrange to call
945 * lower_barrier when the particular background IO completes.
1da177e4 946 */
1da177e4 947
e879a879 948static void raise_barrier(struct r10conf *conf, int force)
1da177e4 949{
b9b083f9 950 write_seqlock_irq(&conf->resync_lock);
9fdfe6d4
YK
951
952 if (WARN_ON_ONCE(force && !conf->barrier))
953 force = false;
0a27ec96 954
6cce3b23 955 /* Wait until no block IO is waiting (unless 'force') */
b9b083f9 956 wait_event_barrier(conf, force || !conf->nr_waiting);
0a27ec96
N
957
958 /* block any new IO from starting */
b9b083f9 959 WRITE_ONCE(conf->barrier, conf->barrier + 1);
0a27ec96 960
c3b328ac 961 /* Now wait for all pending IO to complete */
b9b083f9
YK
962 wait_event_barrier(conf, !atomic_read(&conf->nr_pending) &&
963 conf->barrier < RESYNC_DEPTH);
0a27ec96 964
b9b083f9 965 write_sequnlock_irq(&conf->resync_lock);
0a27ec96
N
966}
967
e879a879 968static void lower_barrier(struct r10conf *conf)
0a27ec96
N
969{
970 unsigned long flags;
b9b083f9
YK
971
972 write_seqlock_irqsave(&conf->resync_lock, flags);
973 WRITE_ONCE(conf->barrier, conf->barrier - 1);
974 write_sequnlock_irqrestore(&conf->resync_lock, flags);
0a27ec96
N
975 wake_up(&conf->wait_barrier);
976}
977
ed2e063f
YK
978static bool stop_waiting_barrier(struct r10conf *conf)
979{
980 struct bio_list *bio_list = current->bio_list;
44693154 981 struct md_thread *thread;
ed2e063f
YK
982
983 /* barrier is dropped */
984 if (!conf->barrier)
985 return true;
986
987 /*
988 * If there are already pending requests (preventing the barrier from
989 * rising completely), and the pre-process bio queue isn't empty, then
990 * don't wait, as we need to empty that queue to get the nr_pending
991 * count down.
992 */
993 if (atomic_read(&conf->nr_pending) && bio_list &&
994 (!bio_list_empty(&bio_list[0]) || !bio_list_empty(&bio_list[1])))
995 return true;
996
44693154
YK
997 /* daemon thread must exist while handling io */
998 thread = rcu_dereference_protected(conf->mddev->thread, true);
72c215ed
LN
999 /*
1000 * move on if io is issued from raid10d(), nr_pending is not released
1001 * from original io(see handle_read_error()). All raise barrier is
1002 * blocked until this io is done.
1003 */
44693154 1004 if (thread->tsk == current) {
72c215ed 1005 WARN_ON_ONCE(atomic_read(&conf->nr_pending) == 0);
ed2e063f 1006 return true;
72c215ed 1007 }
ed2e063f
YK
1008
1009 return false;
1010}
1011
b9b083f9
YK
1012static bool wait_barrier_nolock(struct r10conf *conf)
1013{
1014 unsigned int seq = read_seqbegin(&conf->resync_lock);
1015
1016 if (READ_ONCE(conf->barrier))
1017 return false;
1018
1019 atomic_inc(&conf->nr_pending);
1020 if (!read_seqretry(&conf->resync_lock, seq))
1021 return true;
1022
1023 if (atomic_dec_and_test(&conf->nr_pending))
1024 wake_up_barrier(conf);
1025
1026 return false;
1027}
1028
c9aa889b 1029static bool wait_barrier(struct r10conf *conf, bool nowait)
0a27ec96 1030{
c9aa889b
VV
1031 bool ret = true;
1032
b9b083f9
YK
1033 if (wait_barrier_nolock(conf))
1034 return true;
1035
1036 write_seqlock_irq(&conf->resync_lock);
0a27ec96 1037 if (conf->barrier) {
c9aa889b
VV
1038 /* Return false when nowait flag is set */
1039 if (nowait) {
1040 ret = false;
1041 } else {
0de57e54 1042 conf->nr_waiting++;
c9aa889b 1043 raid10_log(conf->mddev, "wait barrier");
b9b083f9 1044 wait_event_barrier(conf, stop_waiting_barrier(conf));
0de57e54 1045 conf->nr_waiting--;
c9aa889b 1046 }
0e5313e2
TM
1047 if (!conf->nr_waiting)
1048 wake_up(&conf->wait_barrier);
1da177e4 1049 }
c9aa889b
VV
1050 /* Only increment nr_pending when we wait */
1051 if (ret)
1052 atomic_inc(&conf->nr_pending);
b9b083f9 1053 write_sequnlock_irq(&conf->resync_lock);
c9aa889b 1054 return ret;
1da177e4
LT
1055}
1056
e879a879 1057static void allow_barrier(struct r10conf *conf)
0a27ec96 1058{
0e5313e2
TM
1059 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1060 (conf->array_freeze_pending))
0c0be98b 1061 wake_up_barrier(conf);
0a27ec96
N
1062}
1063
e2d59925 1064static void freeze_array(struct r10conf *conf, int extra)
4443ae10
N
1065{
1066 /* stop syncio and normal IO and wait for everything to
f188593e 1067 * go quiet.
4443ae10 1068 * We increment barrier and nr_waiting, and then
e2d59925 1069 * wait until nr_pending match nr_queued+extra
1c830532
N
1070 * This is called in the context of one normal IO request
1071 * that has failed. Thus any sync request that might be pending
1072 * will be blocked by nr_pending, and we need to wait for
1073 * pending IO requests to complete or be queued for re-try.
e2d59925 1074 * Thus the number queued (nr_queued) plus this request (extra)
1c830532
N
1075 * must match the number of pending IOs (nr_pending) before
1076 * we continue.
4443ae10 1077 */
b9b083f9 1078 write_seqlock_irq(&conf->resync_lock);
0e5313e2 1079 conf->array_freeze_pending++;
b9b083f9 1080 WRITE_ONCE(conf->barrier, conf->barrier + 1);
4443ae10 1081 conf->nr_waiting++;
b9b083f9
YK
1082 wait_event_barrier_cmd(conf, atomic_read(&conf->nr_pending) ==
1083 conf->nr_queued + extra, flush_pending_writes(conf));
0e5313e2 1084 conf->array_freeze_pending--;
b9b083f9 1085 write_sequnlock_irq(&conf->resync_lock);
4443ae10
N
1086}
1087
e879a879 1088static void unfreeze_array(struct r10conf *conf)
4443ae10
N
1089{
1090 /* reverse the effect of the freeze */
b9b083f9
YK
1091 write_seqlock_irq(&conf->resync_lock);
1092 WRITE_ONCE(conf->barrier, conf->barrier - 1);
4443ae10
N
1093 conf->nr_waiting--;
1094 wake_up(&conf->wait_barrier);
b9b083f9 1095 write_sequnlock_irq(&conf->resync_lock);
4443ae10
N
1096}
1097
f8c9e74f
N
1098static sector_t choose_data_offset(struct r10bio *r10_bio,
1099 struct md_rdev *rdev)
1100{
1101 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1102 test_bit(R10BIO_Previous, &r10_bio->state))
1103 return rdev->data_offset;
1104 else
1105 return rdev->new_data_offset;
1106}
1107
57c67df4
N
1108static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1109{
daae161f 1110 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, cb);
57c67df4
N
1111 struct mddev *mddev = plug->cb.data;
1112 struct r10conf *conf = mddev->private;
1113 struct bio *bio;
1114
9efcc2c3 1115 if (from_schedule) {
57c67df4
N
1116 spin_lock_irq(&conf->device_lock);
1117 bio_list_merge(&conf->pending_bio_list, &plug->pending);
57c67df4 1118 spin_unlock_irq(&conf->device_lock);
a8d5fdd4 1119 wake_up_barrier(conf);
57c67df4
N
1120 md_wakeup_thread(mddev->thread);
1121 kfree(plug);
1122 return;
1123 }
1124
1125 /* we aren't scheduling, so we can do the write-out directly. */
1126 bio = bio_list_get(&plug->pending);
9efcc2c3 1127 raid1_prepare_flush_writes(mddev->bitmap);
a8d5fdd4 1128 wake_up_barrier(conf);
57c67df4
N
1129
1130 while (bio) { /* submit pending writes */
1131 struct bio *next = bio->bi_next;
8295efbe
YK
1132
1133 raid1_submit_write(bio);
57c67df4 1134 bio = next;
01044462 1135 cond_resched();
57c67df4
N
1136 }
1137 kfree(plug);
1138}
1139
caea3c47
GJ
1140/*
1141 * 1. Register the new request and wait if the reconstruction thread has put
1142 * up a bar for new requests. Continue immediately if no resync is active
1143 * currently.
1144 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1145 */
c9aa889b 1146static bool regular_request_wait(struct mddev *mddev, struct r10conf *conf,
caea3c47
GJ
1147 struct bio *bio, sector_t sectors)
1148{
c9aa889b
VV
1149 /* Bail out if REQ_NOWAIT is set for the bio */
1150 if (!wait_barrier(conf, bio->bi_opf & REQ_NOWAIT)) {
1151 bio_wouldblock_error(bio);
1152 return false;
1153 }
caea3c47
GJ
1154 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1155 bio->bi_iter.bi_sector < conf->reshape_progress &&
1156 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
caea3c47 1157 allow_barrier(conf);
c9aa889b
VV
1158 if (bio->bi_opf & REQ_NOWAIT) {
1159 bio_wouldblock_error(bio);
1160 return false;
1161 }
1162 raid10_log(conf->mddev, "wait reshape");
caea3c47
GJ
1163 wait_event(conf->wait_barrier,
1164 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1165 conf->reshape_progress >= bio->bi_iter.bi_sector +
1166 sectors);
c9aa889b 1167 wait_barrier(conf, false);
caea3c47 1168 }
c9aa889b 1169 return true;
caea3c47
GJ
1170}
1171
bb5f1ed7 1172static void raid10_read_request(struct mddev *mddev, struct bio *bio,
82045523 1173 struct r10bio *r10_bio, bool io_accounting)
1da177e4 1174{
e879a879 1175 struct r10conf *conf = mddev->private;
1da177e4 1176 struct bio *read_bio;
cb1802ff
BVA
1177 const enum req_op op = bio_op(bio);
1178 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
bb5f1ed7 1179 int max_sectors;
bb5f1ed7 1180 struct md_rdev *rdev;
545250f2
N
1181 char b[BDEVNAME_SIZE];
1182 int slot = r10_bio->read_slot;
1183 struct md_rdev *err_rdev = NULL;
1184 gfp_t gfp = GFP_NOIO;
bb5f1ed7 1185
93decc56 1186 if (slot >= 0 && r10_bio->devs[slot].rdev) {
545250f2
N
1187 /*
1188 * This is an error retry, but we cannot
1189 * safely dereference the rdev in the r10_bio,
1190 * we must use the one in conf.
1191 * If it has already been disconnected (unlikely)
1192 * we lose the device name in error messages.
1193 */
1194 int disk;
1195 /*
1196 * As we are blocking raid10, it is a little safer to
1197 * use __GFP_HIGH.
1198 */
1199 gfp = GFP_NOIO | __GFP_HIGH;
1200
1201 rcu_read_lock();
1202 disk = r10_bio->devs[slot].devnum;
1203 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1204 if (err_rdev)
900d156b 1205 snprintf(b, sizeof(b), "%pg", err_rdev->bdev);
545250f2
N
1206 else {
1207 strcpy(b, "???");
1208 /* This never gets dereferenced */
1209 err_rdev = r10_bio->devs[slot].rdev;
1210 }
1211 rcu_read_unlock();
1212 }
bb5f1ed7 1213
c9aa889b
VV
1214 if (!regular_request_wait(mddev, conf, bio, r10_bio->sectors))
1215 return;
bb5f1ed7
RL
1216 rdev = read_balance(conf, r10_bio, &max_sectors);
1217 if (!rdev) {
545250f2
N
1218 if (err_rdev) {
1219 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1220 mdname(mddev), b,
1221 (unsigned long long)r10_bio->sector);
1222 }
bb5f1ed7
RL
1223 raid_end_bio_io(r10_bio);
1224 return;
1225 }
545250f2 1226 if (err_rdev)
913cce5a 1227 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
545250f2 1228 mdname(mddev),
913cce5a 1229 rdev->bdev,
545250f2 1230 (unsigned long long)r10_bio->sector);
fc9977dd
N
1231 if (max_sectors < bio_sectors(bio)) {
1232 struct bio *split = bio_split(bio, max_sectors,
afeee514 1233 gfp, &conf->bio_split);
fc9977dd 1234 bio_chain(split, bio);
e820d55c 1235 allow_barrier(conf);
ed00aabd 1236 submit_bio_noacct(bio);
c9aa889b 1237 wait_barrier(conf, false);
fc9977dd
N
1238 bio = split;
1239 r10_bio->master_bio = bio;
1240 r10_bio->sectors = max_sectors;
1241 }
bb5f1ed7
RL
1242 slot = r10_bio->read_slot;
1243
82045523
YK
1244 if (io_accounting) {
1245 md_account_bio(mddev, &bio);
1246 r10_bio->master_bio = bio;
1247 }
abfc426d 1248 read_bio = bio_alloc_clone(rdev->bdev, bio, gfp, &mddev->bio_set);
bb5f1ed7
RL
1249
1250 r10_bio->devs[slot].bio = read_bio;
1251 r10_bio->devs[slot].rdev = rdev;
1252
1253 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1254 choose_data_offset(r10_bio, rdev);
bb5f1ed7 1255 read_bio->bi_end_io = raid10_end_read_request;
c34b7ac6 1256 read_bio->bi_opf = op | do_sync;
bb5f1ed7
RL
1257 if (test_bit(FailFast, &rdev->flags) &&
1258 test_bit(R10BIO_FailFast, &r10_bio->state))
1259 read_bio->bi_opf |= MD_FAILFAST;
1260 read_bio->bi_private = r10_bio;
1261
1262 if (mddev->gendisk)
1c02fca6 1263 trace_block_bio_remap(read_bio, disk_devt(mddev->gendisk),
bb5f1ed7 1264 r10_bio->sector);
ed00aabd 1265 submit_bio_noacct(read_bio);
bb5f1ed7
RL
1266 return;
1267}
1268
27f26a0f
GJ
1269static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1270 struct bio *bio, bool replacement,
fc9977dd 1271 int n_copy)
bb5f1ed7 1272{
cb1802ff
BVA
1273 const enum req_op op = bio_op(bio);
1274 const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
1275 const blk_opf_t do_fua = bio->bi_opf & REQ_FUA;
6cce3b23 1276 unsigned long flags;
27f26a0f
GJ
1277 struct r10conf *conf = mddev->private;
1278 struct md_rdev *rdev;
1279 int devnum = r10_bio->devs[n_copy].devnum;
1280 struct bio *mbio;
1281
1282 if (replacement) {
1283 rdev = conf->mirrors[devnum].replacement;
1284 if (rdev == NULL) {
1285 /* Replacement just got moved to main 'rdev' */
1286 smp_mb();
1287 rdev = conf->mirrors[devnum].rdev;
1288 }
1289 } else
1290 rdev = conf->mirrors[devnum].rdev;
1291
abfc426d 1292 mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO, &mddev->bio_set);
27f26a0f
GJ
1293 if (replacement)
1294 r10_bio->devs[n_copy].repl_bio = mbio;
1295 else
1296 r10_bio->devs[n_copy].bio = mbio;
1297
1298 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1299 choose_data_offset(r10_bio, rdev));
27f26a0f 1300 mbio->bi_end_io = raid10_end_write_request;
c34b7ac6 1301 mbio->bi_opf = op | do_sync | do_fua;
27f26a0f
GJ
1302 if (!replacement && test_bit(FailFast,
1303 &conf->mirrors[devnum].rdev->flags)
1304 && enough(conf, devnum))
1305 mbio->bi_opf |= MD_FAILFAST;
1306 mbio->bi_private = r10_bio;
1307
1308 if (conf->mddev->gendisk)
1c02fca6 1309 trace_block_bio_remap(mbio, disk_devt(conf->mddev->gendisk),
27f26a0f
GJ
1310 r10_bio->sector);
1311 /* flush_pending_writes() needs access to the rdev so...*/
309dca30 1312 mbio->bi_bdev = (void *)rdev;
27f26a0f
GJ
1313
1314 atomic_inc(&r10_bio->remaining);
1315
460af1f9 1316 if (!raid1_add_bio_to_plug(mddev, mbio, raid10_unplug, conf->copies)) {
23b245c0 1317 spin_lock_irqsave(&conf->device_lock, flags);
27f26a0f 1318 bio_list_add(&conf->pending_bio_list, mbio);
23b245c0 1319 spin_unlock_irqrestore(&conf->device_lock, flags);
27f26a0f 1320 md_wakeup_thread(mddev->thread);
23b245c0 1321 }
27f26a0f
GJ
1322}
1323
b99f8fd2
LN
1324static struct md_rdev *dereference_rdev_and_rrdev(struct raid10_info *mirror,
1325 struct md_rdev **prrdev)
1326{
1327 struct md_rdev *rdev, *rrdev;
1328
1329 rrdev = rcu_dereference(mirror->replacement);
1330 /*
1331 * Read replacement first to prevent reading both rdev and
1332 * replacement as NULL during replacement replace rdev.
1333 */
1334 smp_mb();
1335 rdev = rcu_dereference(mirror->rdev);
1336 if (rdev == rrdev)
1337 rrdev = NULL;
1338
1339 *prrdev = rrdev;
1340 return rdev;
1341}
1342
f2e7e269
XN
1343static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1344{
1345 int i;
1346 struct r10conf *conf = mddev->private;
1347 struct md_rdev *blocked_rdev;
1348
1349retry_wait:
1350 blocked_rdev = NULL;
1351 rcu_read_lock();
1352 for (i = 0; i < conf->copies; i++) {
67364349
LN
1353 struct md_rdev *rdev, *rrdev;
1354
1355 rdev = dereference_rdev_and_rrdev(&conf->mirrors[i], &rrdev);
f2e7e269
XN
1356 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1357 atomic_inc(&rdev->nr_pending);
1358 blocked_rdev = rdev;
1359 break;
1360 }
1361 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1362 atomic_inc(&rrdev->nr_pending);
1363 blocked_rdev = rrdev;
1364 break;
1365 }
1366
1367 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1368 sector_t first_bad;
1369 sector_t dev_sector = r10_bio->devs[i].addr;
1370 int bad_sectors;
1371 int is_bad;
1372
1373 /*
1374 * Discard request doesn't care the write result
1375 * so it doesn't need to wait blocked disk here.
1376 */
1377 if (!r10_bio->sectors)
1378 continue;
1379
1380 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1381 &first_bad, &bad_sectors);
1382 if (is_bad < 0) {
1383 /*
1384 * Mustn't write here until the bad block
1385 * is acknowledged
1386 */
1387 atomic_inc(&rdev->nr_pending);
1388 set_bit(BlockedBadBlocks, &rdev->flags);
1389 blocked_rdev = rdev;
1390 break;
1391 }
1392 }
1393 }
1394 rcu_read_unlock();
1395
1396 if (unlikely(blocked_rdev)) {
1397 /* Have to wait for this device to get unblocked, then retry */
1398 allow_barrier(conf);
1399 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1400 __func__, blocked_rdev->raid_disk);
1401 md_wait_for_blocked_rdev(blocked_rdev, mddev);
c9aa889b 1402 wait_barrier(conf, false);
f2e7e269
XN
1403 goto retry_wait;
1404 }
1405}
1406
27f26a0f
GJ
1407static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1408 struct r10bio *r10_bio)
1409{
1410 struct r10conf *conf = mddev->private;
1411 int i;
bb5f1ed7 1412 sector_t sectors;
d4432c23 1413 int max_sectors;
1da177e4 1414
cb8a7a7e
GJ
1415 if ((mddev_is_clustered(mddev) &&
1416 md_cluster_ops->area_resyncing(mddev, WRITE,
1417 bio->bi_iter.bi_sector,
1418 bio_end_sector(bio)))) {
1419 DEFINE_WAIT(w);
c9aa889b
VV
1420 /* Bail out if REQ_NOWAIT is set for the bio */
1421 if (bio->bi_opf & REQ_NOWAIT) {
1422 bio_wouldblock_error(bio);
1423 return;
1424 }
cb8a7a7e
GJ
1425 for (;;) {
1426 prepare_to_wait(&conf->wait_barrier,
1427 &w, TASK_IDLE);
1428 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1429 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1430 break;
1431 schedule();
1432 }
1433 finish_wait(&conf->wait_barrier, &w);
1434 }
1435
fc9977dd 1436 sectors = r10_bio->sectors;
c9aa889b
VV
1437 if (!regular_request_wait(mddev, conf, bio, sectors))
1438 return;
3ea7daa5 1439 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
3ea7daa5 1440 (mddev->reshape_backwards
4f024f37
KO
1441 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1442 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1443 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1444 bio->bi_iter.bi_sector < conf->reshape_progress))) {
3ea7daa5
N
1445 /* Need to update reshape_position in metadata */
1446 mddev->reshape_position = conf->reshape_progress;
2953079c
SL
1447 set_mask_bits(&mddev->sb_flags, 0,
1448 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
3ea7daa5 1449 md_wakeup_thread(mddev->thread);
c9aa889b
VV
1450 if (bio->bi_opf & REQ_NOWAIT) {
1451 allow_barrier(conf);
1452 bio_wouldblock_error(bio);
1453 return;
1454 }
578b54ad 1455 raid10_log(conf->mddev, "wait reshape metadata");
3ea7daa5 1456 wait_event(mddev->sb_wait,
2953079c 1457 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
3ea7daa5
N
1458
1459 conf->reshape_safe = mddev->reshape_position;
1460 }
1461
6bfe0b49 1462 /* first select target devices under rcu_lock and
1da177e4
LT
1463 * inc refcount on their rdev. Record them by setting
1464 * bios[x] to bio
d4432c23
N
1465 * If there are known/acknowledged bad blocks on any device
1466 * on which we have seen a write error, we want to avoid
1467 * writing to those blocks. This potentially requires several
1468 * writes to write around the bad blocks. Each set of writes
fd16f2e8 1469 * gets its own r10_bio with a set of bios attached.
1da177e4 1470 */
c3b328ac 1471
69335ef3 1472 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1da177e4 1473 raid10_find_phys(conf, r10_bio);
f2e7e269
XN
1474
1475 wait_blocked_dev(mddev, r10_bio);
1476
1da177e4 1477 rcu_read_lock();
d4432c23
N
1478 max_sectors = r10_bio->sectors;
1479
1da177e4
LT
1480 for (i = 0; i < conf->copies; i++) {
1481 int d = r10_bio->devs[i].devnum;
2ae6aaf7
LN
1482 struct md_rdev *rdev, *rrdev;
1483
b99f8fd2 1484 rdev = dereference_rdev_and_rrdev(&conf->mirrors[d], &rrdev);
8ae12666 1485 if (rdev && (test_bit(Faulty, &rdev->flags)))
e7c0c3fa 1486 rdev = NULL;
8ae12666 1487 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
475b0321
N
1488 rrdev = NULL;
1489
d4432c23 1490 r10_bio->devs[i].bio = NULL;
475b0321 1491 r10_bio->devs[i].repl_bio = NULL;
e7c0c3fa
N
1492
1493 if (!rdev && !rrdev) {
6cce3b23 1494 set_bit(R10BIO_Degraded, &r10_bio->state);
d4432c23
N
1495 continue;
1496 }
e7c0c3fa 1497 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
d4432c23
N
1498 sector_t first_bad;
1499 sector_t dev_sector = r10_bio->devs[i].addr;
1500 int bad_sectors;
1501 int is_bad;
1502
bb5f1ed7 1503 is_bad = is_badblock(rdev, dev_sector, max_sectors,
d4432c23 1504 &first_bad, &bad_sectors);
d4432c23
N
1505 if (is_bad && first_bad <= dev_sector) {
1506 /* Cannot write here at all */
1507 bad_sectors -= (dev_sector - first_bad);
1508 if (bad_sectors < max_sectors)
1509 /* Mustn't write more than bad_sectors
1510 * to other devices yet
1511 */
1512 max_sectors = bad_sectors;
1513 /* We don't set R10BIO_Degraded as that
1514 * only applies if the disk is missing,
1515 * so it might be re-added, and we want to
1516 * know to recover this chunk.
1517 * In this case the device is here, and the
1518 * fact that this chunk is not in-sync is
1519 * recorded in the bad block log.
1520 */
1521 continue;
1522 }
1523 if (is_bad) {
1524 int good_sectors = first_bad - dev_sector;
1525 if (good_sectors < max_sectors)
1526 max_sectors = good_sectors;
1527 }
6cce3b23 1528 }
e7c0c3fa
N
1529 if (rdev) {
1530 r10_bio->devs[i].bio = bio;
1531 atomic_inc(&rdev->nr_pending);
1532 }
475b0321
N
1533 if (rrdev) {
1534 r10_bio->devs[i].repl_bio = bio;
1535 atomic_inc(&rrdev->nr_pending);
1536 }
1da177e4
LT
1537 }
1538 rcu_read_unlock();
1539
6b6c8110 1540 if (max_sectors < r10_bio->sectors)
d4432c23 1541 r10_bio->sectors = max_sectors;
fc9977dd
N
1542
1543 if (r10_bio->sectors < bio_sectors(bio)) {
1544 struct bio *split = bio_split(bio, r10_bio->sectors,
afeee514 1545 GFP_NOIO, &conf->bio_split);
fc9977dd 1546 bio_chain(split, bio);
e820d55c 1547 allow_barrier(conf);
ed00aabd 1548 submit_bio_noacct(bio);
c9aa889b 1549 wait_barrier(conf, false);
fc9977dd
N
1550 bio = split;
1551 r10_bio->master_bio = bio;
d4432c23 1552 }
d4432c23 1553
82045523
YK
1554 md_account_bio(mddev, &bio);
1555 r10_bio->master_bio = bio;
4e78064f 1556 atomic_set(&r10_bio->remaining, 1);
e64e4018 1557 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
06d91a5f 1558
1da177e4 1559 for (i = 0; i < conf->copies; i++) {
27f26a0f 1560 if (r10_bio->devs[i].bio)
fc9977dd 1561 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
27f26a0f 1562 if (r10_bio->devs[i].repl_bio)
fc9977dd 1563 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
d4432c23 1564 }
079fa166 1565 one_write_done(r10_bio);
20d0189b
KO
1566}
1567
fc9977dd 1568static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
bb5f1ed7
RL
1569{
1570 struct r10conf *conf = mddev->private;
1571 struct r10bio *r10_bio;
1572
afeee514 1573 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
bb5f1ed7
RL
1574
1575 r10_bio->master_bio = bio;
fc9977dd 1576 r10_bio->sectors = sectors;
bb5f1ed7
RL
1577
1578 r10_bio->mddev = mddev;
1579 r10_bio->sector = bio->bi_iter.bi_sector;
1580 r10_bio->state = 0;
93decc56 1581 r10_bio->read_slot = -1;
c2968285
XN
1582 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) *
1583 conf->geo.raid_disks);
bb5f1ed7
RL
1584
1585 if (bio_data_dir(bio) == READ)
82045523 1586 raid10_read_request(mddev, bio, r10_bio, true);
bb5f1ed7
RL
1587 else
1588 raid10_write_request(mddev, bio, r10_bio);
1589}
1590
254c271d
XN
1591static void raid_end_discard_bio(struct r10bio *r10bio)
1592{
1593 struct r10conf *conf = r10bio->mddev->private;
1594 struct r10bio *first_r10bio;
1595
1596 while (atomic_dec_and_test(&r10bio->remaining)) {
1597
1598 allow_barrier(conf);
1599
1600 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1601 first_r10bio = (struct r10bio *)r10bio->master_bio;
1602 free_r10bio(r10bio);
1603 r10bio = first_r10bio;
1604 } else {
1605 md_write_end(r10bio->mddev);
1606 bio_endio(r10bio->master_bio);
1607 free_r10bio(r10bio);
1608 break;
1609 }
1610 }
1611}
1612
d30588b2
XN
1613static void raid10_end_discard_request(struct bio *bio)
1614{
1615 struct r10bio *r10_bio = bio->bi_private;
1616 struct r10conf *conf = r10_bio->mddev->private;
1617 struct md_rdev *rdev = NULL;
1618 int dev;
1619 int slot, repl;
1620
1621 /*
1622 * We don't care the return value of discard bio
1623 */
1624 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1625 set_bit(R10BIO_Uptodate, &r10_bio->state);
1626
1627 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1628 if (repl)
1629 rdev = conf->mirrors[dev].replacement;
1630 if (!rdev) {
1631 /*
1632 * raid10_remove_disk uses smp_mb to make sure rdev is set to
1633 * replacement before setting replacement to NULL. It can read
dccb8ad6 1634 * rdev first without barrier protect even replacement is NULL
d30588b2
XN
1635 */
1636 smp_rmb();
1637 rdev = conf->mirrors[dev].rdev;
1638 }
1639
254c271d 1640 raid_end_discard_bio(r10_bio);
d30588b2
XN
1641 rdev_dec_pending(rdev, conf->mddev);
1642}
1643
1644/*
1645 * There are some limitations to handle discard bio
1646 * 1st, the discard size is bigger than stripe_size*2.
1647 * 2st, if the discard bio spans reshape progress, we use the old way to
1648 * handle discard bio
1649 */
1650static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1651{
1652 struct r10conf *conf = mddev->private;
1653 struct geom *geo = &conf->geo;
254c271d
XN
1654 int far_copies = geo->far_copies;
1655 bool first_copy = true;
1656 struct r10bio *r10_bio, *first_r10bio;
d30588b2
XN
1657 struct bio *split;
1658 int disk;
1659 sector_t chunk;
1660 unsigned int stripe_size;
1661 unsigned int stripe_data_disks;
1662 sector_t split_size;
1663 sector_t bio_start, bio_end;
1664 sector_t first_stripe_index, last_stripe_index;
1665 sector_t start_disk_offset;
1666 unsigned int start_disk_index;
1667 sector_t end_disk_offset;
1668 unsigned int end_disk_index;
1669 unsigned int remainder;
1670
1671 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1672 return -EAGAIN;
1673
c9aa889b
VV
1674 if (WARN_ON_ONCE(bio->bi_opf & REQ_NOWAIT)) {
1675 bio_wouldblock_error(bio);
1676 return 0;
1677 }
1678 wait_barrier(conf, false);
d30588b2
XN
1679
1680 /*
1681 * Check reshape again to avoid reshape happens after checking
1682 * MD_RECOVERY_RESHAPE and before wait_barrier
1683 */
1684 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1685 goto out;
1686
1687 if (geo->near_copies)
1688 stripe_data_disks = geo->raid_disks / geo->near_copies +
1689 geo->raid_disks % geo->near_copies;
1690 else
1691 stripe_data_disks = geo->raid_disks;
1692
1693 stripe_size = stripe_data_disks << geo->chunk_shift;
1694
1695 bio_start = bio->bi_iter.bi_sector;
1696 bio_end = bio_end_sector(bio);
1697
1698 /*
1699 * Maybe one discard bio is smaller than strip size or across one
1700 * stripe and discard region is larger than one stripe size. For far
1701 * offset layout, if the discard region is not aligned with stripe
1702 * size, there is hole when we submit discard bio to member disk.
1703 * For simplicity, we only handle discard bio which discard region
1704 * is bigger than stripe_size * 2
1705 */
1706 if (bio_sectors(bio) < stripe_size*2)
1707 goto out;
1708
1709 /*
1710 * Keep bio aligned with strip size.
1711 */
1712 div_u64_rem(bio_start, stripe_size, &remainder);
1713 if (remainder) {
1714 split_size = stripe_size - remainder;
1715 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1716 bio_chain(split, bio);
1717 allow_barrier(conf);
1718 /* Resend the fist split part */
1719 submit_bio_noacct(split);
c9aa889b 1720 wait_barrier(conf, false);
d30588b2
XN
1721 }
1722 div_u64_rem(bio_end, stripe_size, &remainder);
1723 if (remainder) {
1724 split_size = bio_sectors(bio) - remainder;
1725 split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
1726 bio_chain(split, bio);
1727 allow_barrier(conf);
1728 /* Resend the second split part */
1729 submit_bio_noacct(bio);
1730 bio = split;
c9aa889b 1731 wait_barrier(conf, false);
d30588b2
XN
1732 }
1733
d30588b2
XN
1734 bio_start = bio->bi_iter.bi_sector;
1735 bio_end = bio_end_sector(bio);
1736
1737 /*
1738 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1739 * One stripe contains the chunks from all member disk (one chunk from
1740 * one disk at the same HBA address). For layout detail, see 'man md 4'
1741 */
1742 chunk = bio_start >> geo->chunk_shift;
1743 chunk *= geo->near_copies;
1744 first_stripe_index = chunk;
1745 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1746 if (geo->far_offset)
1747 first_stripe_index *= geo->far_copies;
1748 start_disk_offset = (bio_start & geo->chunk_mask) +
1749 (first_stripe_index << geo->chunk_shift);
1750
1751 chunk = bio_end >> geo->chunk_shift;
1752 chunk *= geo->near_copies;
1753 last_stripe_index = chunk;
1754 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1755 if (geo->far_offset)
1756 last_stripe_index *= geo->far_copies;
1757 end_disk_offset = (bio_end & geo->chunk_mask) +
1758 (last_stripe_index << geo->chunk_shift);
1759
254c271d
XN
1760retry_discard:
1761 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1762 r10_bio->mddev = mddev;
1763 r10_bio->state = 0;
1764 r10_bio->sectors = 0;
1765 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1766 wait_blocked_dev(mddev, r10_bio);
1767
1768 /*
1769 * For far layout it needs more than one r10bio to cover all regions.
1770 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1771 * to record the discard bio. Other r10bio->master_bio record the first
1772 * r10bio. The first r10bio only release after all other r10bios finish.
1773 * The discard bio returns only first r10bio finishes
1774 */
1775 if (first_copy) {
1776 r10_bio->master_bio = bio;
1777 set_bit(R10BIO_Discard, &r10_bio->state);
1778 first_copy = false;
1779 first_r10bio = r10_bio;
1780 } else
1781 r10_bio->master_bio = (struct bio *)first_r10bio;
1782
46d4703b
XN
1783 /*
1784 * first select target devices under rcu_lock and
1785 * inc refcount on their rdev. Record them by setting
1786 * bios[x] to bio
1787 */
d30588b2
XN
1788 rcu_read_lock();
1789 for (disk = 0; disk < geo->raid_disks; disk++) {
67364349 1790 struct md_rdev *rdev, *rrdev;
d30588b2 1791
67364349 1792 rdev = dereference_rdev_and_rrdev(&conf->mirrors[disk], &rrdev);
d30588b2
XN
1793 r10_bio->devs[disk].bio = NULL;
1794 r10_bio->devs[disk].repl_bio = NULL;
1795
1796 if (rdev && (test_bit(Faulty, &rdev->flags)))
1797 rdev = NULL;
1798 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1799 rrdev = NULL;
1800 if (!rdev && !rrdev)
1801 continue;
1802
1803 if (rdev) {
1804 r10_bio->devs[disk].bio = bio;
1805 atomic_inc(&rdev->nr_pending);
1806 }
1807 if (rrdev) {
1808 r10_bio->devs[disk].repl_bio = bio;
1809 atomic_inc(&rrdev->nr_pending);
1810 }
1811 }
1812 rcu_read_unlock();
1813
1814 atomic_set(&r10_bio->remaining, 1);
1815 for (disk = 0; disk < geo->raid_disks; disk++) {
1816 sector_t dev_start, dev_end;
1817 struct bio *mbio, *rbio = NULL;
d30588b2
XN
1818
1819 /*
1820 * Now start to calculate the start and end address for each disk.
1821 * The space between dev_start and dev_end is the discard region.
1822 *
1823 * For dev_start, it needs to consider three conditions:
1824 * 1st, the disk is before start_disk, you can imagine the disk in
1825 * the next stripe. So the dev_start is the start address of next
1826 * stripe.
1827 * 2st, the disk is after start_disk, it means the disk is at the
1828 * same stripe of first disk
1829 * 3st, the first disk itself, we can use start_disk_offset directly
1830 */
1831 if (disk < start_disk_index)
1832 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1833 else if (disk > start_disk_index)
1834 dev_start = first_stripe_index * mddev->chunk_sectors;
1835 else
1836 dev_start = start_disk_offset;
1837
1838 if (disk < end_disk_index)
1839 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1840 else if (disk > end_disk_index)
1841 dev_end = last_stripe_index * mddev->chunk_sectors;
1842 else
1843 dev_end = end_disk_offset;
1844
1845 /*
1846 * It only handles discard bio which size is >= stripe size, so
46d4703b
XN
1847 * dev_end > dev_start all the time.
1848 * It doesn't need to use rcu lock to get rdev here. We already
1849 * add rdev->nr_pending in the first loop.
d30588b2
XN
1850 */
1851 if (r10_bio->devs[disk].bio) {
46d4703b 1852 struct md_rdev *rdev = conf->mirrors[disk].rdev;
abfc426d
CH
1853 mbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1854 &mddev->bio_set);
d30588b2
XN
1855 mbio->bi_end_io = raid10_end_discard_request;
1856 mbio->bi_private = r10_bio;
1857 r10_bio->devs[disk].bio = mbio;
1858 r10_bio->devs[disk].devnum = disk;
1859 atomic_inc(&r10_bio->remaining);
1860 md_submit_discard_bio(mddev, rdev, mbio,
1861 dev_start + choose_data_offset(r10_bio, rdev),
1862 dev_end - dev_start);
1863 bio_endio(mbio);
1864 }
1865 if (r10_bio->devs[disk].repl_bio) {
46d4703b 1866 struct md_rdev *rrdev = conf->mirrors[disk].replacement;
abfc426d
CH
1867 rbio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO,
1868 &mddev->bio_set);
d30588b2
XN
1869 rbio->bi_end_io = raid10_end_discard_request;
1870 rbio->bi_private = r10_bio;
1871 r10_bio->devs[disk].repl_bio = rbio;
1872 r10_bio->devs[disk].devnum = disk;
1873 atomic_inc(&r10_bio->remaining);
1874 md_submit_discard_bio(mddev, rrdev, rbio,
1875 dev_start + choose_data_offset(r10_bio, rrdev),
1876 dev_end - dev_start);
1877 bio_endio(rbio);
1878 }
1879 }
1880
254c271d
XN
1881 if (!geo->far_offset && --far_copies) {
1882 first_stripe_index += geo->stride >> geo->chunk_shift;
1883 start_disk_offset += geo->stride;
1884 last_stripe_index += geo->stride >> geo->chunk_shift;
1885 end_disk_offset += geo->stride;
1886 atomic_inc(&first_r10bio->remaining);
1887 raid_end_discard_bio(r10_bio);
c9aa889b 1888 wait_barrier(conf, false);
254c271d 1889 goto retry_discard;
d30588b2
XN
1890 }
1891
254c271d
XN
1892 raid_end_discard_bio(r10_bio);
1893
d30588b2
XN
1894 return 0;
1895out:
1896 allow_barrier(conf);
1897 return -EAGAIN;
1898}
1899
cc27b0c7 1900static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
20d0189b
KO
1901{
1902 struct r10conf *conf = mddev->private;
1903 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1904 int chunk_sects = chunk_mask + 1;
fc9977dd 1905 int sectors = bio_sectors(bio);
20d0189b 1906
775d7831
DJ
1907 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1908 && md_flush_request(mddev, bio))
cc27b0c7 1909 return true;
20d0189b 1910
cc27b0c7
N
1911 if (!md_write_start(mddev, bio))
1912 return false;
1913
d30588b2
XN
1914 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1915 if (!raid10_handle_discard(mddev, bio))
1916 return true;
1917
fc9977dd
N
1918 /*
1919 * If this request crosses a chunk boundary, we need to split
1920 * it.
1921 */
1922 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1923 sectors > chunk_sects
1924 && (conf->geo.near_copies < conf->geo.raid_disks
1925 || conf->prev.near_copies <
1926 conf->prev.raid_disks)))
1927 sectors = chunk_sects -
1928 (bio->bi_iter.bi_sector &
1929 (chunk_sects - 1));
1930 __make_request(mddev, bio, sectors);
079fa166
N
1931
1932 /* In case raid10d snuck in to freeze_array */
0c0be98b 1933 wake_up_barrier(conf);
cc27b0c7 1934 return true;
1da177e4
LT
1935}
1936
849674e4 1937static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1da177e4 1938{
e879a879 1939 struct r10conf *conf = mddev->private;
1da177e4
LT
1940 int i;
1941
5cf00fcd 1942 if (conf->geo.near_copies < conf->geo.raid_disks)
9d8f0363 1943 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
5cf00fcd
N
1944 if (conf->geo.near_copies > 1)
1945 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1946 if (conf->geo.far_copies > 1) {
1947 if (conf->geo.far_offset)
1948 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
c93983bf 1949 else
5cf00fcd 1950 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
8bce6d35
N
1951 if (conf->geo.far_set_size != conf->geo.raid_disks)
1952 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
c93983bf 1953 }
5cf00fcd
N
1954 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1955 conf->geo.raid_disks - mddev->degraded);
d44b0a92
N
1956 rcu_read_lock();
1957 for (i = 0; i < conf->geo.raid_disks; i++) {
1958 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1959 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1960 }
1961 rcu_read_unlock();
1da177e4
LT
1962 seq_printf(seq, "]");
1963}
1964
700c7213
N
1965/* check if there are enough drives for
1966 * every block to appear on atleast one.
1967 * Don't consider the device numbered 'ignore'
1968 * as we might be about to remove it.
1969 */
635f6416 1970static int _enough(struct r10conf *conf, int previous, int ignore)
700c7213
N
1971{
1972 int first = 0;
725d6e57 1973 int has_enough = 0;
635f6416
N
1974 int disks, ncopies;
1975 if (previous) {
1976 disks = conf->prev.raid_disks;
1977 ncopies = conf->prev.near_copies;
1978 } else {
1979 disks = conf->geo.raid_disks;
1980 ncopies = conf->geo.near_copies;
1981 }
700c7213 1982
725d6e57 1983 rcu_read_lock();
700c7213
N
1984 do {
1985 int n = conf->copies;
1986 int cnt = 0;
80b48124 1987 int this = first;
700c7213 1988 while (n--) {
725d6e57
N
1989 struct md_rdev *rdev;
1990 if (this != ignore &&
1991 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1992 test_bit(In_sync, &rdev->flags))
700c7213 1993 cnt++;
635f6416 1994 this = (this+1) % disks;
700c7213
N
1995 }
1996 if (cnt == 0)
725d6e57 1997 goto out;
635f6416 1998 first = (first + ncopies) % disks;
700c7213 1999 } while (first != 0);
725d6e57
N
2000 has_enough = 1;
2001out:
2002 rcu_read_unlock();
2003 return has_enough;
700c7213
N
2004}
2005
f8c9e74f
N
2006static int enough(struct r10conf *conf, int ignore)
2007{
635f6416
N
2008 /* when calling 'enough', both 'prev' and 'geo' must
2009 * be stable.
2010 * This is ensured if ->reconfig_mutex or ->device_lock
2011 * is held.
2012 */
2013 return _enough(conf, 0, ignore) &&
2014 _enough(conf, 1, ignore);
f8c9e74f
N
2015}
2016
9631abdb
MT
2017/**
2018 * raid10_error() - RAID10 error handler.
2019 * @mddev: affected md device.
2020 * @rdev: member device to fail.
2021 *
2022 * The routine acknowledges &rdev failure and determines new @mddev state.
2023 * If it failed, then:
2024 * - &MD_BROKEN flag is set in &mddev->flags.
2025 * Otherwise, it must be degraded:
2026 * - recovery is interrupted.
2027 * - &mddev->degraded is bumped.
62bca04b 2028 *
9631abdb
MT
2029 * @rdev is marked as &Faulty excluding case when array is failed and
2030 * &mddev->fail_last_dev is off.
2031 */
849674e4 2032static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 2033{
e879a879 2034 struct r10conf *conf = mddev->private;
635f6416 2035 unsigned long flags;
1da177e4 2036
635f6416 2037 spin_lock_irqsave(&conf->device_lock, flags);
9631abdb
MT
2038
2039 if (test_bit(In_sync, &rdev->flags) && !enough(conf, rdev->raid_disk)) {
2040 set_bit(MD_BROKEN, &mddev->flags);
2041
2042 if (!mddev->fail_last_dev) {
2043 spin_unlock_irqrestore(&conf->device_lock, flags);
2044 return;
2045 }
635f6416 2046 }
2446dba0 2047 if (test_and_clear_bit(In_sync, &rdev->flags))
1da177e4 2048 mddev->degraded++;
9631abdb 2049
2446dba0 2050 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
de393cde 2051 set_bit(Blocked, &rdev->flags);
b2d444d7 2052 set_bit(Faulty, &rdev->flags);
2953079c
SL
2053 set_mask_bits(&mddev->sb_flags, 0,
2054 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
635f6416 2055 spin_unlock_irqrestore(&conf->device_lock, flags);
913cce5a 2056 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
08464e09 2057 "md/raid10:%s: Operation continuing on %d devices.\n",
913cce5a 2058 mdname(mddev), rdev->bdev,
08464e09 2059 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1da177e4
LT
2060}
2061
e879a879 2062static void print_conf(struct r10conf *conf)
1da177e4
LT
2063{
2064 int i;
4056ca51 2065 struct md_rdev *rdev;
1da177e4 2066
08464e09 2067 pr_debug("RAID10 conf printout:\n");
1da177e4 2068 if (!conf) {
08464e09 2069 pr_debug("(!conf)\n");
1da177e4
LT
2070 return;
2071 }
08464e09
N
2072 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
2073 conf->geo.raid_disks);
1da177e4 2074
4056ca51
N
2075 /* This is only called with ->reconfix_mutex held, so
2076 * rcu protection of rdev is not needed */
5cf00fcd 2077 for (i = 0; i < conf->geo.raid_disks; i++) {
4056ca51
N
2078 rdev = conf->mirrors[i].rdev;
2079 if (rdev)
913cce5a 2080 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
08464e09
N
2081 i, !test_bit(In_sync, &rdev->flags),
2082 !test_bit(Faulty, &rdev->flags),
913cce5a 2083 rdev->bdev);
1da177e4
LT
2084 }
2085}
2086
e879a879 2087static void close_sync(struct r10conf *conf)
1da177e4 2088{
c9aa889b 2089 wait_barrier(conf, false);
0a27ec96 2090 allow_barrier(conf);
1da177e4 2091
afeee514 2092 mempool_exit(&conf->r10buf_pool);
1da177e4
LT
2093}
2094
fd01b88c 2095static int raid10_spare_active(struct mddev *mddev)
1da177e4
LT
2096{
2097 int i;
e879a879 2098 struct r10conf *conf = mddev->private;
dc280d98 2099 struct raid10_info *tmp;
6b965620
N
2100 int count = 0;
2101 unsigned long flags;
1da177e4
LT
2102
2103 /*
2104 * Find all non-in_sync disks within the RAID10 configuration
2105 * and mark them in_sync
2106 */
5cf00fcd 2107 for (i = 0; i < conf->geo.raid_disks; i++) {
1da177e4 2108 tmp = conf->mirrors + i;
4ca40c2c
N
2109 if (tmp->replacement
2110 && tmp->replacement->recovery_offset == MaxSector
2111 && !test_bit(Faulty, &tmp->replacement->flags)
2112 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2113 /* Replacement has just become active */
2114 if (!tmp->rdev
2115 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2116 count++;
2117 if (tmp->rdev) {
2118 /* Replaced device not technically faulty,
2119 * but we need to be sure it gets removed
2120 * and never re-added.
2121 */
2122 set_bit(Faulty, &tmp->rdev->flags);
2123 sysfs_notify_dirent_safe(
2124 tmp->rdev->sysfs_state);
2125 }
2126 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2127 } else if (tmp->rdev
61e4947c 2128 && tmp->rdev->recovery_offset == MaxSector
4ca40c2c
N
2129 && !test_bit(Faulty, &tmp->rdev->flags)
2130 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 2131 count++;
2863b9eb 2132 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1da177e4
LT
2133 }
2134 }
6b965620
N
2135 spin_lock_irqsave(&conf->device_lock, flags);
2136 mddev->degraded -= count;
2137 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
2138
2139 print_conf(conf);
6b965620 2140 return count;
1da177e4
LT
2141}
2142
fd01b88c 2143static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 2144{
e879a879 2145 struct r10conf *conf = mddev->private;
199050ea 2146 int err = -EEXIST;
6090368a 2147 int mirror, repl_slot = -1;
6c2fce2e 2148 int first = 0;
5cf00fcd 2149 int last = conf->geo.raid_disks - 1;
6090368a 2150 struct raid10_info *p;
1da177e4
LT
2151
2152 if (mddev->recovery_cp < MaxSector)
2153 /* only hot-add to in-sync arrays, as recovery is
2154 * very different from resync
2155 */
199050ea 2156 return -EBUSY;
635f6416 2157 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
199050ea 2158 return -EINVAL;
1da177e4 2159
1501efad
DW
2160 if (md_integrity_add_rdev(rdev, mddev))
2161 return -ENXIO;
2162
a53a6c85 2163 if (rdev->raid_disk >= 0)
6c2fce2e 2164 first = last = rdev->raid_disk;
1da177e4 2165
2c4193df 2166 if (rdev->saved_raid_disk >= first &&
9e753ba9 2167 rdev->saved_raid_disk < conf->geo.raid_disks &&
6cce3b23
N
2168 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2169 mirror = rdev->saved_raid_disk;
2170 else
6c2fce2e 2171 mirror = first;
2bb77736 2172 for ( ; mirror <= last ; mirror++) {
6090368a 2173 p = &conf->mirrors[mirror];
2bb77736
N
2174 if (p->recovery_disabled == mddev->recovery_disabled)
2175 continue;
b7044d41 2176 if (p->rdev) {
6090368a
LN
2177 if (test_bit(WantReplacement, &p->rdev->flags) &&
2178 p->replacement == NULL && repl_slot < 0)
2179 repl_slot = mirror;
2180 continue;
b7044d41 2181 }
1da177e4 2182
9092c02d
JB
2183 if (mddev->gendisk)
2184 disk_stack_limits(mddev->gendisk, rdev->bdev,
2185 rdev->data_offset << 9);
1da177e4 2186
2bb77736 2187 p->head_position = 0;
d890fa2b 2188 p->recovery_disabled = mddev->recovery_disabled - 1;
2bb77736
N
2189 rdev->raid_disk = mirror;
2190 err = 0;
2191 if (rdev->saved_raid_disk != mirror)
2192 conf->fullsync = 1;
2193 rcu_assign_pointer(p->rdev, rdev);
2194 break;
2195 }
532a2a3f 2196
6090368a
LN
2197 if (err && repl_slot >= 0) {
2198 p = &conf->mirrors[repl_slot];
2199 clear_bit(In_sync, &rdev->flags);
2200 set_bit(Replacement, &rdev->flags);
2201 rdev->raid_disk = repl_slot;
2202 err = 0;
2203 if (mddev->gendisk)
2204 disk_stack_limits(mddev->gendisk, rdev->bdev,
2205 rdev->data_offset << 9);
2206 conf->fullsync = 1;
2207 rcu_assign_pointer(p->replacement, rdev);
2208 }
2209
1da177e4 2210 print_conf(conf);
199050ea 2211 return err;
1da177e4
LT
2212}
2213
b8321b68 2214static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 2215{
e879a879 2216 struct r10conf *conf = mddev->private;
1da177e4 2217 int err = 0;
b8321b68 2218 int number = rdev->raid_disk;
c8ab903e 2219 struct md_rdev **rdevp;
d17f744e 2220 struct raid10_info *p;
1da177e4
LT
2221
2222 print_conf(conf);
d17f744e
MP
2223 if (unlikely(number >= mddev->raid_disks))
2224 return 0;
2225 p = conf->mirrors + number;
c8ab903e
N
2226 if (rdev == p->rdev)
2227 rdevp = &p->rdev;
2228 else if (rdev == p->replacement)
2229 rdevp = &p->replacement;
2230 else
2231 return 0;
2232
2233 if (test_bit(In_sync, &rdev->flags) ||
2234 atomic_read(&rdev->nr_pending)) {
2235 err = -EBUSY;
2236 goto abort;
2237 }
d787be40 2238 /* Only remove non-faulty devices if recovery
c8ab903e
N
2239 * is not possible.
2240 */
2241 if (!test_bit(Faulty, &rdev->flags) &&
2242 mddev->recovery_disabled != p->recovery_disabled &&
4ca40c2c 2243 (!p->replacement || p->replacement == rdev) &&
63aced61 2244 number < conf->geo.raid_disks &&
c8ab903e
N
2245 enough(conf, -1)) {
2246 err = -EBUSY;
2247 goto abort;
1da177e4 2248 }
c8ab903e 2249 *rdevp = NULL;
d787be40
N
2250 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2251 synchronize_rcu();
2252 if (atomic_read(&rdev->nr_pending)) {
2253 /* lost the race, try later */
2254 err = -EBUSY;
2255 *rdevp = rdev;
2256 goto abort;
2257 }
2258 }
2259 if (p->replacement) {
4ca40c2c
N
2260 /* We must have just cleared 'rdev' */
2261 p->rdev = p->replacement;
2262 clear_bit(Replacement, &p->replacement->flags);
2263 smp_mb(); /* Make sure other CPUs may see both as identical
2264 * but will never see neither -- if they are careful.
2265 */
2266 p->replacement = NULL;
e5bc9c3c 2267 }
4ca40c2c 2268
e5bc9c3c 2269 clear_bit(WantReplacement, &rdev->flags);
c8ab903e
N
2270 err = md_integrity_register(mddev);
2271
1da177e4
LT
2272abort:
2273
2274 print_conf(conf);
2275 return err;
2276}
2277
81fa1520 2278static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
1da177e4 2279{
e879a879 2280 struct r10conf *conf = r10_bio->mddev->private;
0eb3ff12 2281
4e4cbee9 2282 if (!bio->bi_status)
0eb3ff12 2283 set_bit(R10BIO_Uptodate, &r10_bio->state);
e684e41d
N
2284 else
2285 /* The write handler will notice the lack of
2286 * R10BIO_Uptodate and record any errors etc
2287 */
4dbcdc75
N
2288 atomic_add(r10_bio->sectors,
2289 &conf->mirrors[d].rdev->corrected_errors);
1da177e4
LT
2290
2291 /* for reconstruct, we always reschedule after a read.
2292 * for resync, only after all reads
2293 */
73d5c38a 2294 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1da177e4
LT
2295 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2296 atomic_dec_and_test(&r10_bio->remaining)) {
2297 /* we have read all the blocks,
2298 * do the comparison in process context in raid10d
2299 */
2300 reschedule_retry(r10_bio);
2301 }
1da177e4
LT
2302}
2303
81fa1520
ML
2304static void end_sync_read(struct bio *bio)
2305{
f0250618 2306 struct r10bio *r10_bio = get_resync_r10bio(bio);
81fa1520
ML
2307 struct r10conf *conf = r10_bio->mddev->private;
2308 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2309
2310 __end_sync_read(r10_bio, bio, d);
2311}
2312
2313static void end_reshape_read(struct bio *bio)
2314{
f0250618 2315 /* reshape read bio isn't allocated from r10buf_pool */
81fa1520
ML
2316 struct r10bio *r10_bio = bio->bi_private;
2317
2318 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2319}
2320
9f2c9d12 2321static void end_sync_request(struct r10bio *r10_bio)
1da177e4 2322{
fd01b88c 2323 struct mddev *mddev = r10_bio->mddev;
dfc70645 2324
1da177e4
LT
2325 while (atomic_dec_and_test(&r10_bio->remaining)) {
2326 if (r10_bio->master_bio == NULL) {
2327 /* the primary of several recovery bios */
73d5c38a 2328 sector_t s = r10_bio->sectors;
1a0b7cd8
N
2329 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2330 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
2331 reschedule_retry(r10_bio);
2332 else
2333 put_buf(r10_bio);
73d5c38a 2334 md_done_sync(mddev, s, 1);
1da177e4
LT
2335 break;
2336 } else {
9f2c9d12 2337 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1a0b7cd8
N
2338 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2339 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
2340 reschedule_retry(r10_bio);
2341 else
2342 put_buf(r10_bio);
1da177e4
LT
2343 r10_bio = r10_bio2;
2344 }
2345 }
1da177e4
LT
2346}
2347
4246a0b6 2348static void end_sync_write(struct bio *bio)
5e570289 2349{
f0250618 2350 struct r10bio *r10_bio = get_resync_r10bio(bio);
fd01b88c 2351 struct mddev *mddev = r10_bio->mddev;
e879a879 2352 struct r10conf *conf = mddev->private;
5e570289
N
2353 int d;
2354 sector_t first_bad;
2355 int bad_sectors;
2356 int slot;
9ad1aefc 2357 int repl;
4ca40c2c 2358 struct md_rdev *rdev = NULL;
5e570289 2359
9ad1aefc
N
2360 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2361 if (repl)
2362 rdev = conf->mirrors[d].replacement;
547414d1 2363 else
9ad1aefc 2364 rdev = conf->mirrors[d].rdev;
5e570289 2365
4e4cbee9 2366 if (bio->bi_status) {
9ad1aefc
N
2367 if (repl)
2368 md_error(mddev, rdev);
2369 else {
2370 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
2371 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2372 set_bit(MD_RECOVERY_NEEDED,
2373 &rdev->mddev->recovery);
9ad1aefc
N
2374 set_bit(R10BIO_WriteError, &r10_bio->state);
2375 }
2376 } else if (is_badblock(rdev,
5e570289
N
2377 r10_bio->devs[slot].addr,
2378 r10_bio->sectors,
2379 &first_bad, &bad_sectors))
2380 set_bit(R10BIO_MadeGood, &r10_bio->state);
2381
9ad1aefc 2382 rdev_dec_pending(rdev, mddev);
5e570289
N
2383
2384 end_sync_request(r10_bio);
2385}
2386
1da177e4
LT
2387/*
2388 * Note: sync and recover and handled very differently for raid10
2389 * This code is for resync.
2390 * For resync, we read through virtual addresses and read all blocks.
2391 * If there is any error, we schedule a write. The lowest numbered
2392 * drive is authoritative.
2393 * However requests come for physical address, so we need to map.
2394 * For every physical address there are raid_disks/copies virtual addresses,
2395 * which is always are least one, but is not necessarly an integer.
2396 * This means that a physical address can span multiple chunks, so we may
2397 * have to submit multiple io requests for a single sync request.
2398 */
2399/*
2400 * We check if all blocks are in-sync and only write to blocks that
2401 * aren't in sync
2402 */
9f2c9d12 2403static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1da177e4 2404{
e879a879 2405 struct r10conf *conf = mddev->private;
1da177e4
LT
2406 int i, first;
2407 struct bio *tbio, *fbio;
f4380a91 2408 int vcnt;
cdb76be3 2409 struct page **tpages, **fpages;
1da177e4
LT
2410
2411 atomic_set(&r10_bio->remaining, 1);
2412
2413 /* find the first device with a block */
2414 for (i=0; i<conf->copies; i++)
4e4cbee9 2415 if (!r10_bio->devs[i].bio->bi_status)
1da177e4
LT
2416 break;
2417
2418 if (i == conf->copies)
2419 goto done;
2420
2421 first = i;
2422 fbio = r10_bio->devs[i].bio;
cc578588
AP
2423 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2424 fbio->bi_iter.bi_idx = 0;
cdb76be3 2425 fpages = get_resync_pages(fbio)->pages;
1da177e4 2426
f4380a91 2427 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
1da177e4 2428 /* now find blocks with errors */
0eb3ff12
N
2429 for (i=0 ; i < conf->copies ; i++) {
2430 int j, d;
8d3ca83d 2431 struct md_rdev *rdev;
f0250618 2432 struct resync_pages *rp;
1da177e4 2433
1da177e4 2434 tbio = r10_bio->devs[i].bio;
0eb3ff12
N
2435
2436 if (tbio->bi_end_io != end_sync_read)
2437 continue;
2438 if (i == first)
1da177e4 2439 continue;
cdb76be3
ML
2440
2441 tpages = get_resync_pages(tbio)->pages;
8d3ca83d
N
2442 d = r10_bio->devs[i].devnum;
2443 rdev = conf->mirrors[d].rdev;
4e4cbee9 2444 if (!r10_bio->devs[i].bio->bi_status) {
0eb3ff12
N
2445 /* We know that the bi_io_vec layout is the same for
2446 * both 'first' and 'i', so we just compare them.
2447 * All vec entries are PAGE_SIZE;
2448 */
7bb23c49
N
2449 int sectors = r10_bio->sectors;
2450 for (j = 0; j < vcnt; j++) {
2451 int len = PAGE_SIZE;
2452 if (sectors < (len / 512))
2453 len = sectors * 512;
cdb76be3
ML
2454 if (memcmp(page_address(fpages[j]),
2455 page_address(tpages[j]),
7bb23c49 2456 len))
0eb3ff12 2457 break;
7bb23c49
N
2458 sectors -= len/512;
2459 }
0eb3ff12
N
2460 if (j == vcnt)
2461 continue;
7f7583d4 2462 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
f84ee364
N
2463 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2464 /* Don't fix anything. */
2465 continue;
8d3ca83d
N
2466 } else if (test_bit(FailFast, &rdev->flags)) {
2467 /* Just give up on this device */
2468 md_error(rdev->mddev, rdev);
2469 continue;
0eb3ff12 2470 }
f84ee364
N
2471 /* Ok, we need to write this bio, either to correct an
2472 * inconsistency or to correct an unreadable block.
1da177e4
LT
2473 * First we need to fixup bv_offset, bv_len and
2474 * bi_vecs, as the read request might have corrupted these
2475 */
f0250618 2476 rp = get_resync_pages(tbio);
a7c50c94 2477 bio_reset(tbio, conf->mirrors[d].rdev->bdev, REQ_OP_WRITE);
8be185f2 2478
fb0eb5df
ML
2479 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2480
f0250618
ML
2481 rp->raid_bio = r10_bio;
2482 tbio->bi_private = rp;
4f024f37 2483 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
1da177e4
LT
2484 tbio->bi_end_io = end_sync_write;
2485
c31df25f
KO
2486 bio_copy_data(tbio, fbio);
2487
1da177e4
LT
2488 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2489 atomic_inc(&r10_bio->remaining);
aa8b57aa 2490 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
1da177e4 2491
1919cbb2
N
2492 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2493 tbio->bi_opf |= MD_FAILFAST;
4f024f37 2494 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
ed00aabd 2495 submit_bio_noacct(tbio);
1da177e4
LT
2496 }
2497
9ad1aefc
N
2498 /* Now write out to any replacement devices
2499 * that are active
2500 */
2501 for (i = 0; i < conf->copies; i++) {
c31df25f 2502 int d;
9ad1aefc
N
2503
2504 tbio = r10_bio->devs[i].repl_bio;
2505 if (!tbio || !tbio->bi_end_io)
2506 continue;
2507 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2508 && r10_bio->devs[i].bio != fbio)
c31df25f 2509 bio_copy_data(tbio, fbio);
9ad1aefc
N
2510 d = r10_bio->devs[i].devnum;
2511 atomic_inc(&r10_bio->remaining);
2512 md_sync_acct(conf->mirrors[d].replacement->bdev,
aa8b57aa 2513 bio_sectors(tbio));
ed00aabd 2514 submit_bio_noacct(tbio);
9ad1aefc
N
2515 }
2516
1da177e4
LT
2517done:
2518 if (atomic_dec_and_test(&r10_bio->remaining)) {
2519 md_done_sync(mddev, r10_bio->sectors, 1);
2520 put_buf(r10_bio);
2521 }
2522}
2523
2524/*
2525 * Now for the recovery code.
2526 * Recovery happens across physical sectors.
2527 * We recover all non-is_sync drives by finding the virtual address of
2528 * each, and then choose a working drive that also has that virt address.
2529 * There is a separate r10_bio for each non-in_sync drive.
2530 * Only the first two slots are in use. The first for reading,
2531 * The second for writing.
2532 *
2533 */
9f2c9d12 2534static void fix_recovery_read_error(struct r10bio *r10_bio)
5e570289
N
2535{
2536 /* We got a read error during recovery.
2537 * We repeat the read in smaller page-sized sections.
2538 * If a read succeeds, write it to the new device or record
2539 * a bad block if we cannot.
2540 * If a read fails, record a bad block on both old and
2541 * new devices.
2542 */
fd01b88c 2543 struct mddev *mddev = r10_bio->mddev;
e879a879 2544 struct r10conf *conf = mddev->private;
5e570289
N
2545 struct bio *bio = r10_bio->devs[0].bio;
2546 sector_t sect = 0;
2547 int sectors = r10_bio->sectors;
2548 int idx = 0;
2549 int dr = r10_bio->devs[0].devnum;
2550 int dw = r10_bio->devs[1].devnum;
cdb76be3 2551 struct page **pages = get_resync_pages(bio)->pages;
5e570289
N
2552
2553 while (sectors) {
2554 int s = sectors;
3cb03002 2555 struct md_rdev *rdev;
5e570289
N
2556 sector_t addr;
2557 int ok;
2558
2559 if (s > (PAGE_SIZE>>9))
2560 s = PAGE_SIZE >> 9;
2561
2562 rdev = conf->mirrors[dr].rdev;
2563 addr = r10_bio->devs[0].addr + sect,
2564 ok = sync_page_io(rdev,
2565 addr,
2566 s << 9,
cdb76be3 2567 pages[idx],
4ce4c73f 2568 REQ_OP_READ, false);
5e570289
N
2569 if (ok) {
2570 rdev = conf->mirrors[dw].rdev;
2571 addr = r10_bio->devs[1].addr + sect;
2572 ok = sync_page_io(rdev,
2573 addr,
2574 s << 9,
cdb76be3 2575 pages[idx],
4ce4c73f 2576 REQ_OP_WRITE, false);
b7044d41 2577 if (!ok) {
5e570289 2578 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
2579 if (!test_and_set_bit(WantReplacement,
2580 &rdev->flags))
2581 set_bit(MD_RECOVERY_NEEDED,
2582 &rdev->mddev->recovery);
2583 }
5e570289
N
2584 }
2585 if (!ok) {
2586 /* We don't worry if we cannot set a bad block -
2587 * it really is bad so there is no loss in not
2588 * recording it yet
2589 */
2590 rdev_set_badblocks(rdev, addr, s, 0);
2591
2592 if (rdev != conf->mirrors[dw].rdev) {
2593 /* need bad block on destination too */
3cb03002 2594 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
5e570289
N
2595 addr = r10_bio->devs[1].addr + sect;
2596 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2597 if (!ok) {
2598 /* just abort the recovery */
08464e09
N
2599 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2600 mdname(mddev));
5e570289
N
2601
2602 conf->mirrors[dw].recovery_disabled
2603 = mddev->recovery_disabled;
2604 set_bit(MD_RECOVERY_INTR,
2605 &mddev->recovery);
2606 break;
2607 }
2608 }
2609 }
2610
2611 sectors -= s;
2612 sect += s;
2613 idx++;
2614 }
2615}
1da177e4 2616
9f2c9d12 2617static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1da177e4 2618{
e879a879 2619 struct r10conf *conf = mddev->private;
c65060ad 2620 int d;
26208a7c
YK
2621 struct bio *wbio = r10_bio->devs[1].bio;
2622 struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2623
2624 /* Need to test wbio2->bi_end_io before we call
2625 * submit_bio_noacct as if the former is NULL,
2626 * the latter is free to free wbio2.
2627 */
2628 if (wbio2 && !wbio2->bi_end_io)
2629 wbio2 = NULL;
1da177e4 2630
5e570289
N
2631 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2632 fix_recovery_read_error(r10_bio);
26208a7c
YK
2633 if (wbio->bi_end_io)
2634 end_sync_request(r10_bio);
2635 if (wbio2)
2636 end_sync_request(r10_bio);
5e570289
N
2637 return;
2638 }
2639
c65060ad
NK
2640 /*
2641 * share the pages with the first bio
1da177e4
LT
2642 * and submit the write request
2643 */
1da177e4 2644 d = r10_bio->devs[1].devnum;
24afd80d
N
2645 if (wbio->bi_end_io) {
2646 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
aa8b57aa 2647 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
ed00aabd 2648 submit_bio_noacct(wbio);
24afd80d 2649 }
0eb25bb0 2650 if (wbio2) {
24afd80d
N
2651 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2652 md_sync_acct(conf->mirrors[d].replacement->bdev,
aa8b57aa 2653 bio_sectors(wbio2));
ed00aabd 2654 submit_bio_noacct(wbio2);
24afd80d 2655 }
1da177e4
LT
2656}
2657
1e50915f
RB
2658/*
2659 * Used by fix_read_error() to decay the per rdev read_errors.
2660 * We halve the read error count for every hour that has elapsed
2661 * since the last recorded read error.
2662 *
2663 */
fd01b88c 2664static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
1e50915f 2665{
0e3ef49e 2666 long cur_time_mon;
1e50915f
RB
2667 unsigned long hours_since_last;
2668 unsigned int read_errors = atomic_read(&rdev->read_errors);
2669
0e3ef49e 2670 cur_time_mon = ktime_get_seconds();
1e50915f 2671
0e3ef49e 2672 if (rdev->last_read_error == 0) {
1e50915f
RB
2673 /* first time we've seen a read error */
2674 rdev->last_read_error = cur_time_mon;
2675 return;
2676 }
2677
0e3ef49e
AB
2678 hours_since_last = (long)(cur_time_mon -
2679 rdev->last_read_error) / 3600;
1e50915f
RB
2680
2681 rdev->last_read_error = cur_time_mon;
2682
2683 /*
2684 * if hours_since_last is > the number of bits in read_errors
2685 * just set read errors to 0. We do this to avoid
2686 * overflowing the shift of read_errors by hours_since_last.
2687 */
2688 if (hours_since_last >= 8 * sizeof(read_errors))
2689 atomic_set(&rdev->read_errors, 0);
2690 else
2691 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2692}
2693
3cb03002 2694static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
265ad47a 2695 int sectors, struct page *page, enum req_op op)
58c54fcc
N
2696{
2697 sector_t first_bad;
2698 int bad_sectors;
2699
2700 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
265ad47a 2701 && (op == REQ_OP_READ || test_bit(WriteErrorSeen, &rdev->flags)))
58c54fcc 2702 return -1;
265ad47a 2703 if (sync_page_io(rdev, sector, sectors << 9, page, op, false))
58c54fcc
N
2704 /* success */
2705 return 1;
265ad47a 2706 if (op == REQ_OP_WRITE) {
58c54fcc 2707 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
2708 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2709 set_bit(MD_RECOVERY_NEEDED,
2710 &rdev->mddev->recovery);
2711 }
58c54fcc
N
2712 /* need to record an error - either for the block or the device */
2713 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2714 md_error(rdev->mddev, rdev);
2715 return 0;
2716}
2717
1da177e4
LT
2718/*
2719 * This is a kernel thread which:
2720 *
2721 * 1. Retries failed read operations on working mirrors.
2722 * 2. Updates the raid superblock when problems encounter.
6814d536 2723 * 3. Performs writes following reads for array synchronising.
1da177e4
LT
2724 */
2725
e879a879 2726static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
6814d536
N
2727{
2728 int sect = 0; /* Offset from r10_bio->sector */
605eeda6 2729 int sectors = r10_bio->sectors, slot = r10_bio->read_slot;
13db16d7 2730 struct md_rdev *rdev;
1e50915f 2731 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
605eeda6 2732 int d = r10_bio->devs[slot].devnum;
1e50915f 2733
7c4e06ff
N
2734 /* still own a reference to this rdev, so it cannot
2735 * have been cleared recently.
2736 */
2737 rdev = conf->mirrors[d].rdev;
1e50915f 2738
7c4e06ff
N
2739 if (test_bit(Faulty, &rdev->flags))
2740 /* drive has already been failed, just ignore any
2741 more fix_read_error() attempts */
2742 return;
1e50915f 2743
7c4e06ff
N
2744 check_decay_read_errors(mddev, rdev);
2745 atomic_inc(&rdev->read_errors);
2746 if (atomic_read(&rdev->read_errors) > max_read_errors) {
913cce5a
CH
2747 pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2748 mdname(mddev), rdev->bdev,
08464e09 2749 atomic_read(&rdev->read_errors), max_read_errors);
913cce5a
CH
2750 pr_notice("md/raid10:%s: %pg: Failing raid device\n",
2751 mdname(mddev), rdev->bdev);
d683c8e0 2752 md_error(mddev, rdev);
605eeda6 2753 r10_bio->devs[slot].bio = IO_BLOCKED;
7c4e06ff 2754 return;
1e50915f 2755 }
1e50915f 2756
6814d536
N
2757 while(sectors) {
2758 int s = sectors;
605eeda6 2759 int sl = slot;
6814d536
N
2760 int success = 0;
2761 int start;
2762
2763 if (s > (PAGE_SIZE>>9))
2764 s = PAGE_SIZE >> 9;
2765
2766 rcu_read_lock();
2767 do {
8dbed5ce
N
2768 sector_t first_bad;
2769 int bad_sectors;
2770
0544a21d 2771 d = r10_bio->devs[sl].devnum;
6814d536
N
2772 rdev = rcu_dereference(conf->mirrors[d].rdev);
2773 if (rdev &&
8dbed5ce 2774 test_bit(In_sync, &rdev->flags) &&
f5b67ae8 2775 !test_bit(Faulty, &rdev->flags) &&
8dbed5ce
N
2776 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2777 &first_bad, &bad_sectors) == 0) {
6814d536
N
2778 atomic_inc(&rdev->nr_pending);
2779 rcu_read_unlock();
2b193363 2780 success = sync_page_io(rdev,
6814d536 2781 r10_bio->devs[sl].addr +
ccebd4c4 2782 sect,
6814d536 2783 s<<9,
796a5cf0 2784 conf->tmppage,
4ce4c73f 2785 REQ_OP_READ, false);
6814d536
N
2786 rdev_dec_pending(rdev, mddev);
2787 rcu_read_lock();
2788 if (success)
2789 break;
2790 }
2791 sl++;
2792 if (sl == conf->copies)
2793 sl = 0;
02c67a3b 2794 } while (sl != slot);
6814d536
N
2795 rcu_read_unlock();
2796
2797 if (!success) {
58c54fcc
N
2798 /* Cannot read from anywhere, just mark the block
2799 * as bad on the first device to discourage future
2800 * reads.
2801 */
605eeda6 2802 int dn = r10_bio->devs[slot].devnum;
58c54fcc
N
2803 rdev = conf->mirrors[dn].rdev;
2804
2805 if (!rdev_set_badblocks(
2806 rdev,
605eeda6 2807 r10_bio->devs[slot].addr
58c54fcc 2808 + sect,
fae8cc5e 2809 s, 0)) {
58c54fcc 2810 md_error(mddev, rdev);
605eeda6 2811 r10_bio->devs[slot].bio
fae8cc5e
N
2812 = IO_BLOCKED;
2813 }
6814d536
N
2814 break;
2815 }
2816
2817 start = sl;
2818 /* write it back and re-read */
2819 rcu_read_lock();
605eeda6 2820 while (sl != slot) {
6814d536
N
2821 if (sl==0)
2822 sl = conf->copies;
2823 sl--;
2824 d = r10_bio->devs[sl].devnum;
2825 rdev = rcu_dereference(conf->mirrors[d].rdev);
1294b9c9 2826 if (!rdev ||
f5b67ae8 2827 test_bit(Faulty, &rdev->flags) ||
1294b9c9
N
2828 !test_bit(In_sync, &rdev->flags))
2829 continue;
2830
2831 atomic_inc(&rdev->nr_pending);
2832 rcu_read_unlock();
58c54fcc
N
2833 if (r10_sync_page_io(rdev,
2834 r10_bio->devs[sl].addr +
2835 sect,
265ad47a 2836 s, conf->tmppage, REQ_OP_WRITE)
1294b9c9
N
2837 == 0) {
2838 /* Well, this device is dead */
913cce5a 2839 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
08464e09
N
2840 mdname(mddev), s,
2841 (unsigned long long)(
2842 sect +
2843 choose_data_offset(r10_bio,
2844 rdev)),
913cce5a
CH
2845 rdev->bdev);
2846 pr_notice("md/raid10:%s: %pg: failing drive\n",
08464e09 2847 mdname(mddev),
913cce5a 2848 rdev->bdev);
6814d536 2849 }
1294b9c9
N
2850 rdev_dec_pending(rdev, mddev);
2851 rcu_read_lock();
6814d536
N
2852 }
2853 sl = start;
605eeda6 2854 while (sl != slot) {
6814d536
N
2855 if (sl==0)
2856 sl = conf->copies;
2857 sl--;
2858 d = r10_bio->devs[sl].devnum;
2859 rdev = rcu_dereference(conf->mirrors[d].rdev);
1294b9c9 2860 if (!rdev ||
f5b67ae8 2861 test_bit(Faulty, &rdev->flags) ||
1294b9c9
N
2862 !test_bit(In_sync, &rdev->flags))
2863 continue;
6814d536 2864
1294b9c9
N
2865 atomic_inc(&rdev->nr_pending);
2866 rcu_read_unlock();
58c54fcc
N
2867 switch (r10_sync_page_io(rdev,
2868 r10_bio->devs[sl].addr +
2869 sect,
265ad47a 2870 s, conf->tmppage, REQ_OP_READ)) {
58c54fcc 2871 case 0:
1294b9c9 2872 /* Well, this device is dead */
913cce5a 2873 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
1294b9c9
N
2874 mdname(mddev), s,
2875 (unsigned long long)(
f8c9e74f
N
2876 sect +
2877 choose_data_offset(r10_bio, rdev)),
913cce5a
CH
2878 rdev->bdev);
2879 pr_notice("md/raid10:%s: %pg: failing drive\n",
1294b9c9 2880 mdname(mddev),
913cce5a 2881 rdev->bdev);
58c54fcc
N
2882 break;
2883 case 1:
913cce5a 2884 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
1294b9c9
N
2885 mdname(mddev), s,
2886 (unsigned long long)(
f8c9e74f
N
2887 sect +
2888 choose_data_offset(r10_bio, rdev)),
913cce5a 2889 rdev->bdev);
1294b9c9 2890 atomic_add(s, &rdev->corrected_errors);
6814d536 2891 }
1294b9c9
N
2892
2893 rdev_dec_pending(rdev, mddev);
2894 rcu_read_lock();
6814d536
N
2895 }
2896 rcu_read_unlock();
2897
2898 sectors -= s;
2899 sect += s;
2900 }
2901}
2902
9f2c9d12 2903static int narrow_write_error(struct r10bio *r10_bio, int i)
bd870a16
N
2904{
2905 struct bio *bio = r10_bio->master_bio;
fd01b88c 2906 struct mddev *mddev = r10_bio->mddev;
e879a879 2907 struct r10conf *conf = mddev->private;
3cb03002 2908 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
bd870a16
N
2909 /* bio has the data to be written to slot 'i' where
2910 * we just recently had a write error.
2911 * We repeatedly clone the bio and trim down to one block,
2912 * then try the write. Where the write fails we record
2913 * a bad block.
2914 * It is conceivable that the bio doesn't exactly align with
2915 * blocks. We must handle this.
2916 *
2917 * We currently own a reference to the rdev.
2918 */
2919
2920 int block_sectors;
2921 sector_t sector;
2922 int sectors;
2923 int sect_to_write = r10_bio->sectors;
2924 int ok = 1;
2925
2926 if (rdev->badblocks.shift < 0)
2927 return 0;
2928
f04ebb0b
N
2929 block_sectors = roundup(1 << rdev->badblocks.shift,
2930 bdev_logical_block_size(rdev->bdev) >> 9);
bd870a16
N
2931 sector = r10_bio->sector;
2932 sectors = ((r10_bio->sector + block_sectors)
2933 & ~(sector_t)(block_sectors - 1))
2934 - sector;
2935
2936 while (sect_to_write) {
2937 struct bio *wbio;
27028626 2938 sector_t wsector;
bd870a16
N
2939 if (sectors > sect_to_write)
2940 sectors = sect_to_write;
2941 /* Write at 'sector' for 'sectors' */
abfc426d
CH
2942 wbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
2943 &mddev->bio_set);
4f024f37 2944 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
27028626
TM
2945 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2946 wbio->bi_iter.bi_sector = wsector +
2947 choose_data_offset(r10_bio, rdev);
c34b7ac6 2948 wbio->bi_opf = REQ_OP_WRITE;
4e49ea4a
MC
2949
2950 if (submit_bio_wait(wbio) < 0)
bd870a16 2951 /* Failure! */
27028626 2952 ok = rdev_set_badblocks(rdev, wsector,
bd870a16
N
2953 sectors, 0)
2954 && ok;
2955
2956 bio_put(wbio);
2957 sect_to_write -= sectors;
2958 sector += sectors;
2959 sectors = block_sectors;
2960 }
2961 return ok;
2962}
2963
9f2c9d12 2964static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
560f8e55
N
2965{
2966 int slot = r10_bio->read_slot;
560f8e55 2967 struct bio *bio;
e879a879 2968 struct r10conf *conf = mddev->private;
abbf098e 2969 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
560f8e55
N
2970
2971 /* we got a read error. Maybe the drive is bad. Maybe just
2972 * the block and we can fix it.
2973 * We freeze all other IO, and try reading the block from
2974 * other devices. When we find one, we re-write
2975 * and check it that fixes the read error.
2976 * This is all done synchronously while the array is
2977 * frozen.
2978 */
fae8cc5e 2979 bio = r10_bio->devs[slot].bio;
fae8cc5e
N
2980 bio_put(bio);
2981 r10_bio->devs[slot].bio = NULL;
2982
8d3ca83d
N
2983 if (mddev->ro)
2984 r10_bio->devs[slot].bio = IO_BLOCKED;
2985 else if (!test_bit(FailFast, &rdev->flags)) {
e2d59925 2986 freeze_array(conf, 1);
560f8e55
N
2987 fix_read_error(conf, mddev, r10_bio);
2988 unfreeze_array(conf);
fae8cc5e 2989 } else
8d3ca83d 2990 md_error(mddev, rdev);
fae8cc5e 2991
abbf098e 2992 rdev_dec_pending(rdev, mddev);
545250f2 2993 r10_bio->state = 0;
82045523 2994 raid10_read_request(mddev, r10_bio->master_bio, r10_bio, false);
72c215ed
LN
2995 /*
2996 * allow_barrier after re-submit to ensure no sync io
2997 * can be issued while regular io pending.
2998 */
2999 allow_barrier(conf);
560f8e55
N
3000}
3001
e879a879 3002static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
749c55e9
N
3003{
3004 /* Some sort of write request has finished and it
3005 * succeeded in writing where we thought there was a
3006 * bad block. So forget the bad block.
1a0b7cd8
N
3007 * Or possibly if failed and we need to record
3008 * a bad block.
749c55e9
N
3009 */
3010 int m;
3cb03002 3011 struct md_rdev *rdev;
749c55e9
N
3012
3013 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
3014 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1a0b7cd8
N
3015 for (m = 0; m < conf->copies; m++) {
3016 int dev = r10_bio->devs[m].devnum;
3017 rdev = conf->mirrors[dev].rdev;
01a69cab
YY
3018 if (r10_bio->devs[m].bio == NULL ||
3019 r10_bio->devs[m].bio->bi_end_io == NULL)
1a0b7cd8 3020 continue;
4e4cbee9 3021 if (!r10_bio->devs[m].bio->bi_status) {
749c55e9
N
3022 rdev_clear_badblocks(
3023 rdev,
3024 r10_bio->devs[m].addr,
c6563a8c 3025 r10_bio->sectors, 0);
1a0b7cd8
N
3026 } else {
3027 if (!rdev_set_badblocks(
3028 rdev,
3029 r10_bio->devs[m].addr,
3030 r10_bio->sectors, 0))
3031 md_error(conf->mddev, rdev);
749c55e9 3032 }
9ad1aefc 3033 rdev = conf->mirrors[dev].replacement;
01a69cab
YY
3034 if (r10_bio->devs[m].repl_bio == NULL ||
3035 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
9ad1aefc 3036 continue;
4246a0b6 3037
4e4cbee9 3038 if (!r10_bio->devs[m].repl_bio->bi_status) {
9ad1aefc
N
3039 rdev_clear_badblocks(
3040 rdev,
3041 r10_bio->devs[m].addr,
c6563a8c 3042 r10_bio->sectors, 0);
9ad1aefc
N
3043 } else {
3044 if (!rdev_set_badblocks(
3045 rdev,
3046 r10_bio->devs[m].addr,
3047 r10_bio->sectors, 0))
3048 md_error(conf->mddev, rdev);
3049 }
1a0b7cd8 3050 }
749c55e9
N
3051 put_buf(r10_bio);
3052 } else {
95af587e 3053 bool fail = false;
bd870a16
N
3054 for (m = 0; m < conf->copies; m++) {
3055 int dev = r10_bio->devs[m].devnum;
3056 struct bio *bio = r10_bio->devs[m].bio;
3057 rdev = conf->mirrors[dev].rdev;
3058 if (bio == IO_MADE_GOOD) {
749c55e9
N
3059 rdev_clear_badblocks(
3060 rdev,
3061 r10_bio->devs[m].addr,
c6563a8c 3062 r10_bio->sectors, 0);
749c55e9 3063 rdev_dec_pending(rdev, conf->mddev);
4e4cbee9 3064 } else if (bio != NULL && bio->bi_status) {
95af587e 3065 fail = true;
bd870a16
N
3066 if (!narrow_write_error(r10_bio, m)) {
3067 md_error(conf->mddev, rdev);
3068 set_bit(R10BIO_Degraded,
3069 &r10_bio->state);
3070 }
3071 rdev_dec_pending(rdev, conf->mddev);
749c55e9 3072 }
475b0321
N
3073 bio = r10_bio->devs[m].repl_bio;
3074 rdev = conf->mirrors[dev].replacement;
4ca40c2c 3075 if (rdev && bio == IO_MADE_GOOD) {
475b0321
N
3076 rdev_clear_badblocks(
3077 rdev,
3078 r10_bio->devs[m].addr,
c6563a8c 3079 r10_bio->sectors, 0);
475b0321
N
3080 rdev_dec_pending(rdev, conf->mddev);
3081 }
bd870a16 3082 }
95af587e
N
3083 if (fail) {
3084 spin_lock_irq(&conf->device_lock);
3085 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
23ddba80 3086 conf->nr_queued++;
95af587e 3087 spin_unlock_irq(&conf->device_lock);
cf25ae78
GJ
3088 /*
3089 * In case freeze_array() is waiting for condition
3090 * nr_pending == nr_queued + extra to be true.
3091 */
3092 wake_up(&conf->wait_barrier);
95af587e 3093 md_wakeup_thread(conf->mddev->thread);
c340702c
N
3094 } else {
3095 if (test_bit(R10BIO_WriteError,
3096 &r10_bio->state))
3097 close_write(r10_bio);
95af587e 3098 raid_end_bio_io(r10_bio);
c340702c 3099 }
749c55e9
N
3100 }
3101}
3102
4ed8731d 3103static void raid10d(struct md_thread *thread)
1da177e4 3104{
4ed8731d 3105 struct mddev *mddev = thread->mddev;
9f2c9d12 3106 struct r10bio *r10_bio;
1da177e4 3107 unsigned long flags;
e879a879 3108 struct r10conf *conf = mddev->private;
1da177e4 3109 struct list_head *head = &conf->retry_list;
e1dfa0a2 3110 struct blk_plug plug;
1da177e4
LT
3111
3112 md_check_recovery(mddev);
1da177e4 3113
95af587e 3114 if (!list_empty_careful(&conf->bio_end_io_list) &&
2953079c 3115 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
95af587e
N
3116 LIST_HEAD(tmp);
3117 spin_lock_irqsave(&conf->device_lock, flags);
2953079c 3118 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
23ddba80
SL
3119 while (!list_empty(&conf->bio_end_io_list)) {
3120 list_move(conf->bio_end_io_list.prev, &tmp);
3121 conf->nr_queued--;
3122 }
95af587e
N
3123 }
3124 spin_unlock_irqrestore(&conf->device_lock, flags);
3125 while (!list_empty(&tmp)) {
a452744b
MP
3126 r10_bio = list_first_entry(&tmp, struct r10bio,
3127 retry_list);
95af587e 3128 list_del(&r10_bio->retry_list);
c340702c
N
3129 if (mddev->degraded)
3130 set_bit(R10BIO_Degraded, &r10_bio->state);
3131
3132 if (test_bit(R10BIO_WriteError,
3133 &r10_bio->state))
3134 close_write(r10_bio);
95af587e
N
3135 raid_end_bio_io(r10_bio);
3136 }
3137 }
3138
e1dfa0a2 3139 blk_start_plug(&plug);
1da177e4 3140 for (;;) {
6cce3b23 3141
0021b7bc 3142 flush_pending_writes(conf);
6cce3b23 3143
a35e63ef
N
3144 spin_lock_irqsave(&conf->device_lock, flags);
3145 if (list_empty(head)) {
3146 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 3147 break;
a35e63ef 3148 }
9f2c9d12 3149 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
1da177e4 3150 list_del(head->prev);
4443ae10 3151 conf->nr_queued--;
1da177e4
LT
3152 spin_unlock_irqrestore(&conf->device_lock, flags);
3153
3154 mddev = r10_bio->mddev;
070ec55d 3155 conf = mddev->private;
bd870a16
N
3156 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3157 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9 3158 handle_write_completed(conf, r10_bio);
3ea7daa5
N
3159 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3160 reshape_request_write(mddev, r10_bio);
749c55e9 3161 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
1da177e4 3162 sync_request_write(mddev, r10_bio);
7eaceacc 3163 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
1da177e4 3164 recovery_request_write(mddev, r10_bio);
856e08e2 3165 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
560f8e55 3166 handle_read_error(mddev, r10_bio);
fc9977dd
N
3167 else
3168 WARN_ON_ONCE(1);
560f8e55 3169
1d9d5241 3170 cond_resched();
2953079c 3171 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
de393cde 3172 md_check_recovery(mddev);
1da177e4 3173 }
e1dfa0a2 3174 blk_finish_plug(&plug);
1da177e4
LT
3175}
3176
e879a879 3177static int init_resync(struct r10conf *conf)
1da177e4 3178{
afeee514 3179 int ret, buffs, i;
1da177e4
LT
3180
3181 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
afeee514 3182 BUG_ON(mempool_initialized(&conf->r10buf_pool));
69335ef3 3183 conf->have_replacement = 0;
5cf00fcd 3184 for (i = 0; i < conf->geo.raid_disks; i++)
69335ef3
N
3185 if (conf->mirrors[i].replacement)
3186 conf->have_replacement = 1;
afeee514
KO
3187 ret = mempool_init(&conf->r10buf_pool, buffs,
3188 r10buf_pool_alloc, r10buf_pool_free, conf);
3189 if (ret)
3190 return ret;
1da177e4
LT
3191 conf->next_resync = 0;
3192 return 0;
3193}
3194
208410b5
SL
3195static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3196{
afeee514 3197 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
208410b5
SL
3198 struct rsync_pages *rp;
3199 struct bio *bio;
3200 int nalloc;
3201 int i;
3202
3203 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3204 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3205 nalloc = conf->copies; /* resync */
3206 else
3207 nalloc = 2; /* recovery */
3208
3209 for (i = 0; i < nalloc; i++) {
3210 bio = r10bio->devs[i].bio;
3211 rp = bio->bi_private;
a7c50c94 3212 bio_reset(bio, NULL, 0);
208410b5
SL
3213 bio->bi_private = rp;
3214 bio = r10bio->devs[i].repl_bio;
3215 if (bio) {
3216 rp = bio->bi_private;
a7c50c94 3217 bio_reset(bio, NULL, 0);
208410b5
SL
3218 bio->bi_private = rp;
3219 }
3220 }
3221 return r10bio;
3222}
3223
8db87912
GJ
3224/*
3225 * Set cluster_sync_high since we need other nodes to add the
3226 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3227 */
3228static void raid10_set_cluster_sync_high(struct r10conf *conf)
3229{
3230 sector_t window_size;
3231 int extra_chunk, chunks;
3232
3233 /*
3234 * First, here we define "stripe" as a unit which across
3235 * all member devices one time, so we get chunks by use
3236 * raid_disks / near_copies. Otherwise, if near_copies is
3237 * close to raid_disks, then resync window could increases
3238 * linearly with the increase of raid_disks, which means
3239 * we will suspend a really large IO window while it is not
3240 * necessary. If raid_disks is not divisible by near_copies,
3241 * an extra chunk is needed to ensure the whole "stripe" is
3242 * covered.
3243 */
3244
3245 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3246 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3247 extra_chunk = 0;
3248 else
3249 extra_chunk = 1;
3250 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3251
3252 /*
3253 * At least use a 32M window to align with raid1's resync window
3254 */
3255 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3256 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3257
3258 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3259}
3260
1da177e4
LT
3261/*
3262 * perform a "sync" on one "block"
3263 *
3264 * We need to make sure that no normal I/O request - particularly write
3265 * requests - conflict with active sync requests.
3266 *
3267 * This is achieved by tracking pending requests and a 'barrier' concept
3268 * that can be installed to exclude normal IO requests.
3269 *
3270 * Resync and recovery are handled very differently.
3271 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3272 *
3273 * For resync, we iterate over virtual addresses, read all copies,
3274 * and update if there are differences. If only one copy is live,
3275 * skip it.
3276 * For recovery, we iterate over physical addresses, read a good
3277 * value for each non-in_sync drive, and over-write.
3278 *
3279 * So, for recovery we may have several outstanding complex requests for a
3280 * given address, one for each out-of-sync device. We model this by allocating
3281 * a number of r10_bio structures, one for each out-of-sync device.
3282 * As we setup these structures, we collect all bio's together into a list
3283 * which we then process collectively to add pages, and then process again
ed00aabd 3284 * to pass to submit_bio_noacct.
1da177e4
LT
3285 *
3286 * The r10_bio structures are linked using a borrowed master_bio pointer.
3287 * This link is counted in ->remaining. When the r10_bio that points to NULL
3288 * has its remaining count decremented to 0, the whole complex operation
3289 * is complete.
3290 *
3291 */
3292
849674e4 3293static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
09314799 3294 int *skipped)
1da177e4 3295{
e879a879 3296 struct r10conf *conf = mddev->private;
9f2c9d12 3297 struct r10bio *r10_bio;
1da177e4
LT
3298 struct bio *biolist = NULL, *bio;
3299 sector_t max_sector, nr_sectors;
1da177e4 3300 int i;
6cce3b23 3301 int max_sync;
57dab0bd 3302 sector_t sync_blocks;
1da177e4
LT
3303 sector_t sectors_skipped = 0;
3304 int chunks_skipped = 0;
5cf00fcd 3305 sector_t chunk_mask = conf->geo.chunk_mask;
022e510f 3306 int page_idx = 0;
8d355a46 3307 int error_disk = -1;
1da177e4 3308
7e83ccbe
MW
3309 /*
3310 * Allow skipping a full rebuild for incremental assembly
3311 * of a clean array, like RAID1 does.
3312 */
3313 if (mddev->bitmap == NULL &&
3314 mddev->recovery_cp == MaxSector &&
13765120
N
3315 mddev->reshape_position == MaxSector &&
3316 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7e83ccbe 3317 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
13765120 3318 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7e83ccbe
MW
3319 conf->fullsync == 0) {
3320 *skipped = 1;
13765120 3321 return mddev->dev_sectors - sector_nr;
7e83ccbe
MW
3322 }
3323
a405c6f0
LN
3324 if (!mempool_initialized(&conf->r10buf_pool))
3325 if (init_resync(conf))
3326 return 0;
3327
1da177e4 3328 skipped:
58c0fed4 3329 max_sector = mddev->dev_sectors;
3ea7daa5
N
3330 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3331 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1da177e4
LT
3332 max_sector = mddev->resync_max_sectors;
3333 if (sector_nr >= max_sector) {
8db87912
GJ
3334 conf->cluster_sync_low = 0;
3335 conf->cluster_sync_high = 0;
3336
6cce3b23
N
3337 /* If we aborted, we need to abort the
3338 * sync on the 'current' bitmap chucks (there can
3339 * be several when recovering multiple devices).
3340 * as we may have started syncing it but not finished.
3341 * We can find the current address in
3342 * mddev->curr_resync, but for recovery,
3343 * we need to convert that to several
3344 * virtual addresses.
3345 */
3ea7daa5
N
3346 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3347 end_reshape(conf);
b3968552 3348 close_sync(conf);
3ea7daa5
N
3349 return 0;
3350 }
3351
6cce3b23
N
3352 if (mddev->curr_resync < max_sector) { /* aborted */
3353 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
e64e4018
AS
3354 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3355 &sync_blocks, 1);
5cf00fcd 3356 else for (i = 0; i < conf->geo.raid_disks; i++) {
6cce3b23
N
3357 sector_t sect =
3358 raid10_find_virt(conf, mddev->curr_resync, i);
e64e4018
AS
3359 md_bitmap_end_sync(mddev->bitmap, sect,
3360 &sync_blocks, 1);
6cce3b23 3361 }
9ad1aefc
N
3362 } else {
3363 /* completed sync */
3364 if ((!mddev->bitmap || conf->fullsync)
3365 && conf->have_replacement
3366 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3367 /* Completed a full sync so the replacements
3368 * are now fully recovered.
3369 */
f90145f3
N
3370 rcu_read_lock();
3371 for (i = 0; i < conf->geo.raid_disks; i++) {
3372 struct md_rdev *rdev =
3373 rcu_dereference(conf->mirrors[i].replacement);
3374 if (rdev)
3375 rdev->recovery_offset = MaxSector;
3376 }
3377 rcu_read_unlock();
9ad1aefc 3378 }
6cce3b23 3379 conf->fullsync = 0;
9ad1aefc 3380 }
e64e4018 3381 md_bitmap_close_sync(mddev->bitmap);
1da177e4 3382 close_sync(conf);
57afd89f 3383 *skipped = 1;
1da177e4
LT
3384 return sectors_skipped;
3385 }
3ea7daa5
N
3386
3387 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3388 return reshape_request(mddev, sector_nr, skipped);
3389
5cf00fcd 3390 if (chunks_skipped >= conf->geo.raid_disks) {
8d355a46
LN
3391 pr_err("md/raid10:%s: %s fails\n", mdname(mddev),
3392 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? "resync" : "recovery");
3393 if (error_disk >= 0 &&
3394 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3395 /*
3396 * recovery fails, set mirrors.recovery_disabled,
3397 * device shouldn't be added to there.
3398 */
3399 conf->mirrors[error_disk].recovery_disabled =
3400 mddev->recovery_disabled;
3401 return 0;
3402 }
3403 /*
3404 * if there has been nothing to do on any drive,
3405 * then there is nothing to do at all.
1da177e4 3406 */
57afd89f
N
3407 *skipped = 1;
3408 return (max_sector - sector_nr) + sectors_skipped;
1da177e4
LT
3409 }
3410
c6207277
N
3411 if (max_sector > mddev->resync_max)
3412 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3413
1da177e4
LT
3414 /* make sure whole request will fit in a chunk - if chunks
3415 * are meaningful
3416 */
5cf00fcd
N
3417 if (conf->geo.near_copies < conf->geo.raid_disks &&
3418 max_sector > (sector_nr | chunk_mask))
3419 max_sector = (sector_nr | chunk_mask) + 1;
1da177e4 3420
7ac50447
TM
3421 /*
3422 * If there is non-resync activity waiting for a turn, then let it
3423 * though before starting on this new sync request.
3424 */
3425 if (conf->nr_waiting)
3426 schedule_timeout_uninterruptible(1);
3427
1da177e4
LT
3428 /* Again, very different code for resync and recovery.
3429 * Both must result in an r10bio with a list of bios that
309dca30 3430 * have bi_end_io, bi_sector, bi_bdev set,
1da177e4
LT
3431 * and bi_private set to the r10bio.
3432 * For recovery, we may actually create several r10bios
3433 * with 2 bios in each, that correspond to the bios in the main one.
3434 * In this case, the subordinate r10bios link back through a
3435 * borrowed master_bio pointer, and the counter in the master
3436 * includes a ref from each subordinate.
3437 */
3438 /* First, we decide what to do and set ->bi_end_io
3439 * To end_sync_read if we want to read, and
3440 * end_sync_write if we will want to write.
3441 */
3442
6cce3b23 3443 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1da177e4
LT
3444 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3445 /* recovery... the complicated one */
e875ecea 3446 int j;
1da177e4
LT
3447 r10_bio = NULL;
3448
5cf00fcd 3449 for (i = 0 ; i < conf->geo.raid_disks; i++) {
ab9d47e9 3450 int still_degraded;
9f2c9d12 3451 struct r10bio *rb2;
ab9d47e9
N
3452 sector_t sect;
3453 int must_sync;
e875ecea 3454 int any_working;
dc280d98 3455 struct raid10_info *mirror = &conf->mirrors[i];
f90145f3 3456 struct md_rdev *mrdev, *mreplace;
24afd80d 3457
f90145f3
N
3458 rcu_read_lock();
3459 mrdev = rcu_dereference(mirror->rdev);
3460 mreplace = rcu_dereference(mirror->replacement);
3461
59f8f0b5
LN
3462 if (mrdev && (test_bit(Faulty, &mrdev->flags) ||
3463 test_bit(In_sync, &mrdev->flags)))
3464 mrdev = NULL;
34817a24
LN
3465 if (mreplace && test_bit(Faulty, &mreplace->flags))
3466 mreplace = NULL;
ee37d731 3467
59f8f0b5 3468 if (!mrdev && !mreplace) {
f90145f3 3469 rcu_read_unlock();
ab9d47e9 3470 continue;
f90145f3 3471 }
1da177e4 3472
ab9d47e9
N
3473 still_degraded = 0;
3474 /* want to reconstruct this device */
3475 rb2 = r10_bio;
3476 sect = raid10_find_virt(conf, sector_nr, i);
fc448a18
N
3477 if (sect >= mddev->resync_max_sectors) {
3478 /* last stripe is not complete - don't
3479 * try to recover this sector.
3480 */
f90145f3 3481 rcu_read_unlock();
fc448a18
N
3482 continue;
3483 }
24afd80d
N
3484 /* Unless we are doing a full sync, or a replacement
3485 * we only need to recover the block if it is set in
3486 * the bitmap
ab9d47e9 3487 */
e64e4018
AS
3488 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3489 &sync_blocks, 1);
ab9d47e9
N
3490 if (sync_blocks < max_sync)
3491 max_sync = sync_blocks;
3492 if (!must_sync &&
f90145f3 3493 mreplace == NULL &&
ab9d47e9
N
3494 !conf->fullsync) {
3495 /* yep, skip the sync_blocks here, but don't assume
3496 * that there will never be anything to do here
3497 */
3498 chunks_skipped = -1;
f90145f3 3499 rcu_read_unlock();
ab9d47e9
N
3500 continue;
3501 }
59f8f0b5
LN
3502 if (mrdev)
3503 atomic_inc(&mrdev->nr_pending);
f90145f3
N
3504 if (mreplace)
3505 atomic_inc(&mreplace->nr_pending);
3506 rcu_read_unlock();
6cce3b23 3507
208410b5 3508 r10_bio = raid10_alloc_init_r10buf(conf);
cb8b12b5 3509 r10_bio->state = 0;
ab9d47e9
N
3510 raise_barrier(conf, rb2 != NULL);
3511 atomic_set(&r10_bio->remaining, 0);
18055569 3512
ab9d47e9
N
3513 r10_bio->master_bio = (struct bio*)rb2;
3514 if (rb2)
3515 atomic_inc(&rb2->remaining);
3516 r10_bio->mddev = mddev;
3517 set_bit(R10BIO_IsRecover, &r10_bio->state);
3518 r10_bio->sector = sect;
1da177e4 3519
ab9d47e9
N
3520 raid10_find_phys(conf, r10_bio);
3521
3522 /* Need to check if the array will still be
3523 * degraded
3524 */
f90145f3
N
3525 rcu_read_lock();
3526 for (j = 0; j < conf->geo.raid_disks; j++) {
3527 struct md_rdev *rdev = rcu_dereference(
3528 conf->mirrors[j].rdev);
3529 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
ab9d47e9 3530 still_degraded = 1;
87fc767b 3531 break;
1da177e4 3532 }
f90145f3 3533 }
ab9d47e9 3534
e64e4018
AS
3535 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3536 &sync_blocks, still_degraded);
ab9d47e9 3537
e875ecea 3538 any_working = 0;
ab9d47e9 3539 for (j=0; j<conf->copies;j++) {
e875ecea 3540 int k;
ab9d47e9 3541 int d = r10_bio->devs[j].devnum;
5e570289 3542 sector_t from_addr, to_addr;
f90145f3
N
3543 struct md_rdev *rdev =
3544 rcu_dereference(conf->mirrors[d].rdev);
40c356ce
N
3545 sector_t sector, first_bad;
3546 int bad_sectors;
f90145f3
N
3547 if (!rdev ||
3548 !test_bit(In_sync, &rdev->flags))
ab9d47e9
N
3549 continue;
3550 /* This is where we read from */
e875ecea 3551 any_working = 1;
40c356ce
N
3552 sector = r10_bio->devs[j].addr;
3553
3554 if (is_badblock(rdev, sector, max_sync,
3555 &first_bad, &bad_sectors)) {
3556 if (first_bad > sector)
3557 max_sync = first_bad - sector;
3558 else {
3559 bad_sectors -= (sector
3560 - first_bad);
3561 if (max_sync > bad_sectors)
3562 max_sync = bad_sectors;
3563 continue;
3564 }
3565 }
ab9d47e9
N
3566 bio = r10_bio->devs[0].bio;
3567 bio->bi_next = biolist;
3568 biolist = bio;
ab9d47e9 3569 bio->bi_end_io = end_sync_read;
c34b7ac6 3570 bio->bi_opf = REQ_OP_READ;
8d3ca83d
N
3571 if (test_bit(FailFast, &rdev->flags))
3572 bio->bi_opf |= MD_FAILFAST;
5e570289 3573 from_addr = r10_bio->devs[j].addr;
4f024f37
KO
3574 bio->bi_iter.bi_sector = from_addr +
3575 rdev->data_offset;
74d46992 3576 bio_set_dev(bio, rdev->bdev);
24afd80d
N
3577 atomic_inc(&rdev->nr_pending);
3578 /* and we write to 'i' (if not in_sync) */
ab9d47e9
N
3579
3580 for (k=0; k<conf->copies; k++)
3581 if (r10_bio->devs[k].devnum == i)
3582 break;
3583 BUG_ON(k == conf->copies);
5e570289 3584 to_addr = r10_bio->devs[k].addr;
ab9d47e9 3585 r10_bio->devs[0].devnum = d;
5e570289 3586 r10_bio->devs[0].addr = from_addr;
ab9d47e9 3587 r10_bio->devs[1].devnum = i;
5e570289 3588 r10_bio->devs[1].addr = to_addr;
ab9d47e9 3589
59f8f0b5 3590 if (mrdev) {
24afd80d
N
3591 bio = r10_bio->devs[1].bio;
3592 bio->bi_next = biolist;
3593 biolist = bio;
24afd80d 3594 bio->bi_end_io = end_sync_write;
c34b7ac6 3595 bio->bi_opf = REQ_OP_WRITE;
4f024f37 3596 bio->bi_iter.bi_sector = to_addr
f90145f3 3597 + mrdev->data_offset;
74d46992 3598 bio_set_dev(bio, mrdev->bdev);
24afd80d
N
3599 atomic_inc(&r10_bio->remaining);
3600 } else
3601 r10_bio->devs[1].bio->bi_end_io = NULL;
3602
3603 /* and maybe write to replacement */
3604 bio = r10_bio->devs[1].repl_bio;
3605 if (bio)
3606 bio->bi_end_io = NULL;
34817a24 3607 /* Note: if replace is not NULL, then bio
24afd80d
N
3608 * cannot be NULL as r10buf_pool_alloc will
3609 * have allocated it.
24afd80d 3610 */
34817a24 3611 if (!mreplace)
24afd80d
N
3612 break;
3613 bio->bi_next = biolist;
3614 biolist = bio;
24afd80d 3615 bio->bi_end_io = end_sync_write;
c34b7ac6 3616 bio->bi_opf = REQ_OP_WRITE;
4f024f37 3617 bio->bi_iter.bi_sector = to_addr +
f90145f3 3618 mreplace->data_offset;
74d46992 3619 bio_set_dev(bio, mreplace->bdev);
24afd80d 3620 atomic_inc(&r10_bio->remaining);
ab9d47e9
N
3621 break;
3622 }
f90145f3 3623 rcu_read_unlock();
ab9d47e9 3624 if (j == conf->copies) {
e875ecea
N
3625 /* Cannot recover, so abort the recovery or
3626 * record a bad block */
e875ecea
N
3627 if (any_working) {
3628 /* problem is that there are bad blocks
3629 * on other device(s)
3630 */
3631 int k;
3632 for (k = 0; k < conf->copies; k++)
3633 if (r10_bio->devs[k].devnum == i)
3634 break;
59f8f0b5 3635 if (mrdev && !test_bit(In_sync,
f90145f3 3636 &mrdev->flags)
24afd80d 3637 && !rdev_set_badblocks(
f90145f3 3638 mrdev,
24afd80d
N
3639 r10_bio->devs[k].addr,
3640 max_sync, 0))
3641 any_working = 0;
f90145f3 3642 if (mreplace &&
24afd80d 3643 !rdev_set_badblocks(
f90145f3 3644 mreplace,
e875ecea
N
3645 r10_bio->devs[k].addr,
3646 max_sync, 0))
3647 any_working = 0;
3648 }
3649 if (!any_working) {
3650 if (!test_and_set_bit(MD_RECOVERY_INTR,
3651 &mddev->recovery))
08464e09 3652 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
e875ecea 3653 mdname(mddev));
24afd80d 3654 mirror->recovery_disabled
e875ecea 3655 = mddev->recovery_disabled;
8d355a46
LN
3656 } else {
3657 error_disk = i;
e875ecea 3658 }
e8b84915
N
3659 put_buf(r10_bio);
3660 if (rb2)
3661 atomic_dec(&rb2->remaining);
3662 r10_bio = rb2;
59f8f0b5
LN
3663 if (mrdev)
3664 rdev_dec_pending(mrdev, mddev);
f90145f3
N
3665 if (mreplace)
3666 rdev_dec_pending(mreplace, mddev);
ab9d47e9 3667 break;
1da177e4 3668 }
59f8f0b5
LN
3669 if (mrdev)
3670 rdev_dec_pending(mrdev, mddev);
f90145f3
N
3671 if (mreplace)
3672 rdev_dec_pending(mreplace, mddev);
8d3ca83d
N
3673 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3674 /* Only want this if there is elsewhere to
3675 * read from. 'j' is currently the first
3676 * readable copy.
3677 */
3678 int targets = 1;
3679 for (; j < conf->copies; j++) {
3680 int d = r10_bio->devs[j].devnum;
3681 if (conf->mirrors[d].rdev &&
3682 test_bit(In_sync,
3683 &conf->mirrors[d].rdev->flags))
3684 targets++;
3685 }
3686 if (targets == 1)
3687 r10_bio->devs[0].bio->bi_opf
3688 &= ~MD_FAILFAST;
3689 }
ab9d47e9 3690 }
1da177e4
LT
3691 if (biolist == NULL) {
3692 while (r10_bio) {
9f2c9d12
N
3693 struct r10bio *rb2 = r10_bio;
3694 r10_bio = (struct r10bio*) rb2->master_bio;
1da177e4
LT
3695 rb2->master_bio = NULL;
3696 put_buf(rb2);
3697 }
3698 goto giveup;
3699 }
3700 } else {
3701 /* resync. Schedule a read for every block at this virt offset */
3702 int count = 0;
6cce3b23 3703
8db87912
GJ
3704 /*
3705 * Since curr_resync_completed could probably not update in
3706 * time, and we will set cluster_sync_low based on it.
3707 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3708 * safety reason, which ensures curr_resync_completed is
3709 * updated in bitmap_cond_end_sync.
3710 */
e64e4018
AS
3711 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3712 mddev_is_clustered(mddev) &&
3713 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
78200d45 3714
e64e4018
AS
3715 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3716 &sync_blocks, mddev->degraded) &&
ab9d47e9
N
3717 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3718 &mddev->recovery)) {
6cce3b23
N
3719 /* We can skip this block */
3720 *skipped = 1;
3721 return sync_blocks + sectors_skipped;
3722 }
3723 if (sync_blocks < max_sync)
3724 max_sync = sync_blocks;
208410b5 3725 r10_bio = raid10_alloc_init_r10buf(conf);
cb8b12b5 3726 r10_bio->state = 0;
1da177e4 3727
1da177e4
LT
3728 r10_bio->mddev = mddev;
3729 atomic_set(&r10_bio->remaining, 0);
6cce3b23
N
3730 raise_barrier(conf, 0);
3731 conf->next_resync = sector_nr;
1da177e4
LT
3732
3733 r10_bio->master_bio = NULL;
3734 r10_bio->sector = sector_nr;
3735 set_bit(R10BIO_IsSync, &r10_bio->state);
3736 raid10_find_phys(conf, r10_bio);
5cf00fcd 3737 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
1da177e4 3738
5cf00fcd 3739 for (i = 0; i < conf->copies; i++) {
1da177e4 3740 int d = r10_bio->devs[i].devnum;
40c356ce
N
3741 sector_t first_bad, sector;
3742 int bad_sectors;
f90145f3 3743 struct md_rdev *rdev;
40c356ce 3744
9ad1aefc
N
3745 if (r10_bio->devs[i].repl_bio)
3746 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3747
1da177e4 3748 bio = r10_bio->devs[i].bio;
4e4cbee9 3749 bio->bi_status = BLK_STS_IOERR;
f90145f3
N
3750 rcu_read_lock();
3751 rdev = rcu_dereference(conf->mirrors[d].rdev);
3752 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3753 rcu_read_unlock();
1da177e4 3754 continue;
f90145f3 3755 }
40c356ce 3756 sector = r10_bio->devs[i].addr;
f90145f3 3757 if (is_badblock(rdev, sector, max_sync,
40c356ce
N
3758 &first_bad, &bad_sectors)) {
3759 if (first_bad > sector)
3760 max_sync = first_bad - sector;
3761 else {
3762 bad_sectors -= (sector - first_bad);
3763 if (max_sync > bad_sectors)
91502f09 3764 max_sync = bad_sectors;
f90145f3 3765 rcu_read_unlock();
40c356ce
N
3766 continue;
3767 }
3768 }
f90145f3 3769 atomic_inc(&rdev->nr_pending);
1da177e4
LT
3770 atomic_inc(&r10_bio->remaining);
3771 bio->bi_next = biolist;
3772 biolist = bio;
1da177e4 3773 bio->bi_end_io = end_sync_read;
c34b7ac6 3774 bio->bi_opf = REQ_OP_READ;
1cdd1257 3775 if (test_bit(FailFast, &rdev->flags))
8d3ca83d 3776 bio->bi_opf |= MD_FAILFAST;
f90145f3 3777 bio->bi_iter.bi_sector = sector + rdev->data_offset;
74d46992 3778 bio_set_dev(bio, rdev->bdev);
1da177e4 3779 count++;
9ad1aefc 3780
f90145f3
N
3781 rdev = rcu_dereference(conf->mirrors[d].replacement);
3782 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3783 rcu_read_unlock();
9ad1aefc 3784 continue;
f90145f3
N
3785 }
3786 atomic_inc(&rdev->nr_pending);
9ad1aefc
N
3787
3788 /* Need to set up for writing to the replacement */
3789 bio = r10_bio->devs[i].repl_bio;
4e4cbee9 3790 bio->bi_status = BLK_STS_IOERR;
9ad1aefc
N
3791
3792 sector = r10_bio->devs[i].addr;
9ad1aefc
N
3793 bio->bi_next = biolist;
3794 biolist = bio;
9ad1aefc 3795 bio->bi_end_io = end_sync_write;
c34b7ac6 3796 bio->bi_opf = REQ_OP_WRITE;
1cdd1257 3797 if (test_bit(FailFast, &rdev->flags))
1919cbb2 3798 bio->bi_opf |= MD_FAILFAST;
f90145f3 3799 bio->bi_iter.bi_sector = sector + rdev->data_offset;
74d46992 3800 bio_set_dev(bio, rdev->bdev);
9ad1aefc 3801 count++;
1cdd1257 3802 rcu_read_unlock();
1da177e4
LT
3803 }
3804
3805 if (count < 2) {
3806 for (i=0; i<conf->copies; i++) {
3807 int d = r10_bio->devs[i].devnum;
3808 if (r10_bio->devs[i].bio->bi_end_io)
ab9d47e9
N
3809 rdev_dec_pending(conf->mirrors[d].rdev,
3810 mddev);
9ad1aefc
N
3811 if (r10_bio->devs[i].repl_bio &&
3812 r10_bio->devs[i].repl_bio->bi_end_io)
3813 rdev_dec_pending(
3814 conf->mirrors[d].replacement,
3815 mddev);
1da177e4
LT
3816 }
3817 put_buf(r10_bio);
3818 biolist = NULL;
3819 goto giveup;
3820 }
3821 }
3822
1da177e4 3823 nr_sectors = 0;
6cce3b23
N
3824 if (sector_nr + max_sync < max_sector)
3825 max_sector = sector_nr + max_sync;
1da177e4
LT
3826 do {
3827 struct page *page;
3828 int len = PAGE_SIZE;
1da177e4
LT
3829 if (sector_nr + (len>>9) > max_sector)
3830 len = (max_sector - sector_nr) << 9;
3831 if (len == 0)
3832 break;
3833 for (bio= biolist ; bio ; bio=bio->bi_next) {
f0250618 3834 struct resync_pages *rp = get_resync_pages(bio);
022e510f 3835 page = resync_fetch_page(rp, page_idx);
0c67dd64
JT
3836 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
3837 bio->bi_status = BLK_STS_RESOURCE;
3838 bio_endio(bio);
3839 goto giveup;
3840 }
1da177e4
LT
3841 }
3842 nr_sectors += len>>9;
3843 sector_nr += len>>9;
022e510f 3844 } while (++page_idx < RESYNC_PAGES);
1da177e4
LT
3845 r10_bio->sectors = nr_sectors;
3846
8db87912
GJ
3847 if (mddev_is_clustered(mddev) &&
3848 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3849 /* It is resync not recovery */
3850 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3851 conf->cluster_sync_low = mddev->curr_resync_completed;
3852 raid10_set_cluster_sync_high(conf);
3853 /* Send resync message */
3854 md_cluster_ops->resync_info_update(mddev,
3855 conf->cluster_sync_low,
3856 conf->cluster_sync_high);
3857 }
3858 } else if (mddev_is_clustered(mddev)) {
3859 /* This is recovery not resync */
3860 sector_t sect_va1, sect_va2;
3861 bool broadcast_msg = false;
3862
3863 for (i = 0; i < conf->geo.raid_disks; i++) {
3864 /*
3865 * sector_nr is a device address for recovery, so we
3866 * need translate it to array address before compare
3867 * with cluster_sync_high.
3868 */
3869 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3870
3871 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3872 broadcast_msg = true;
3873 /*
3874 * curr_resync_completed is similar as
3875 * sector_nr, so make the translation too.
3876 */
3877 sect_va2 = raid10_find_virt(conf,
3878 mddev->curr_resync_completed, i);
3879
3880 if (conf->cluster_sync_low == 0 ||
3881 conf->cluster_sync_low > sect_va2)
3882 conf->cluster_sync_low = sect_va2;
3883 }
3884 }
3885 if (broadcast_msg) {
3886 raid10_set_cluster_sync_high(conf);
3887 md_cluster_ops->resync_info_update(mddev,
3888 conf->cluster_sync_low,
3889 conf->cluster_sync_high);
3890 }
3891 }
3892
1da177e4
LT
3893 while (biolist) {
3894 bio = biolist;
3895 biolist = biolist->bi_next;
3896
3897 bio->bi_next = NULL;
f0250618 3898 r10_bio = get_resync_r10bio(bio);
1da177e4
LT
3899 r10_bio->sectors = nr_sectors;
3900
3901 if (bio->bi_end_io == end_sync_read) {
74d46992 3902 md_sync_acct_bio(bio, nr_sectors);
4e4cbee9 3903 bio->bi_status = 0;
ed00aabd 3904 submit_bio_noacct(bio);
1da177e4
LT
3905 }
3906 }
3907
57afd89f
N
3908 if (sectors_skipped)
3909 /* pretend they weren't skipped, it makes
3910 * no important difference in this case
3911 */
3912 md_done_sync(mddev, sectors_skipped, 1);
3913
1da177e4
LT
3914 return sectors_skipped + nr_sectors;
3915 giveup:
3916 /* There is nowhere to write, so all non-sync
e875ecea
N
3917 * drives must be failed or in resync, all drives
3918 * have a bad block, so try the next chunk...
1da177e4 3919 */
09b4068a
N
3920 if (sector_nr + max_sync < max_sector)
3921 max_sector = sector_nr + max_sync;
3922
3923 sectors_skipped += (max_sector - sector_nr);
1da177e4
LT
3924 chunks_skipped ++;
3925 sector_nr = max_sector;
1da177e4 3926 goto skipped;
1da177e4
LT
3927}
3928
80c3a6ce 3929static sector_t
fd01b88c 3930raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce
DW
3931{
3932 sector_t size;
e879a879 3933 struct r10conf *conf = mddev->private;
80c3a6ce
DW
3934
3935 if (!raid_disks)
3ea7daa5
N
3936 raid_disks = min(conf->geo.raid_disks,
3937 conf->prev.raid_disks);
80c3a6ce 3938 if (!sectors)
dab8b292 3939 sectors = conf->dev_sectors;
80c3a6ce 3940
5cf00fcd
N
3941 size = sectors >> conf->geo.chunk_shift;
3942 sector_div(size, conf->geo.far_copies);
80c3a6ce 3943 size = size * raid_disks;
5cf00fcd 3944 sector_div(size, conf->geo.near_copies);
80c3a6ce 3945
5cf00fcd 3946 return size << conf->geo.chunk_shift;
80c3a6ce
DW
3947}
3948
6508fdbf
N
3949static void calc_sectors(struct r10conf *conf, sector_t size)
3950{
3951 /* Calculate the number of sectors-per-device that will
3952 * actually be used, and set conf->dev_sectors and
3953 * conf->stride
3954 */
3955
5cf00fcd
N
3956 size = size >> conf->geo.chunk_shift;
3957 sector_div(size, conf->geo.far_copies);
3958 size = size * conf->geo.raid_disks;
3959 sector_div(size, conf->geo.near_copies);
6508fdbf
N
3960 /* 'size' is now the number of chunks in the array */
3961 /* calculate "used chunks per device" */
3962 size = size * conf->copies;
3963
3964 /* We need to round up when dividing by raid_disks to
3965 * get the stride size.
3966 */
5cf00fcd 3967 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
6508fdbf 3968
5cf00fcd 3969 conf->dev_sectors = size << conf->geo.chunk_shift;
6508fdbf 3970
5cf00fcd
N
3971 if (conf->geo.far_offset)
3972 conf->geo.stride = 1 << conf->geo.chunk_shift;
6508fdbf 3973 else {
5cf00fcd
N
3974 sector_div(size, conf->geo.far_copies);
3975 conf->geo.stride = size << conf->geo.chunk_shift;
6508fdbf
N
3976 }
3977}
dab8b292 3978
deb200d0
N
3979enum geo_type {geo_new, geo_old, geo_start};
3980static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3981{
3982 int nc, fc, fo;
3983 int layout, chunk, disks;
3984 switch (new) {
3985 case geo_old:
3986 layout = mddev->layout;
3987 chunk = mddev->chunk_sectors;
3988 disks = mddev->raid_disks - mddev->delta_disks;
3989 break;
3990 case geo_new:
3991 layout = mddev->new_layout;
3992 chunk = mddev->new_chunk_sectors;
3993 disks = mddev->raid_disks;
3994 break;
3995 default: /* avoid 'may be unused' warnings */
3996 case geo_start: /* new when starting reshape - raid_disks not
3997 * updated yet. */
3998 layout = mddev->new_layout;
3999 chunk = mddev->new_chunk_sectors;
4000 disks = mddev->raid_disks + mddev->delta_disks;
4001 break;
4002 }
8bce6d35 4003 if (layout >> 19)
deb200d0
N
4004 return -1;
4005 if (chunk < (PAGE_SIZE >> 9) ||
4006 !is_power_of_2(chunk))
4007 return -2;
4008 nc = layout & 255;
4009 fc = (layout >> 8) & 255;
4010 fo = layout & (1<<16);
4011 geo->raid_disks = disks;
4012 geo->near_copies = nc;
4013 geo->far_copies = fc;
4014 geo->far_offset = fo;
8bce6d35
N
4015 switch (layout >> 17) {
4016 case 0: /* original layout. simple but not always optimal */
4017 geo->far_set_size = disks;
4018 break;
4019 case 1: /* "improved" layout which was buggy. Hopefully no-one is
4020 * actually using this, but leave code here just in case.*/
4021 geo->far_set_size = disks/fc;
4022 WARN(geo->far_set_size < fc,
4023 "This RAID10 layout does not provide data safety - please backup and create new array\n");
4024 break;
4025 case 2: /* "improved" layout fixed to match documentation */
4026 geo->far_set_size = fc * nc;
4027 break;
4028 default: /* Not a valid layout */
4029 return -1;
4030 }
deb200d0
N
4031 geo->chunk_mask = chunk - 1;
4032 geo->chunk_shift = ffz(~chunk);
4033 return nc*fc;
4034}
4035
c9ac2acd
YK
4036static void raid10_free_conf(struct r10conf *conf)
4037{
4038 if (!conf)
4039 return;
4040
4041 mempool_exit(&conf->r10bio_pool);
4042 kfree(conf->mirrors);
4043 kfree(conf->mirrors_old);
4044 kfree(conf->mirrors_new);
4045 safe_put_page(conf->tmppage);
4046 bioset_exit(&conf->bio_split);
4047 kfree(conf);
4048}
4049
e879a879 4050static struct r10conf *setup_conf(struct mddev *mddev)
1da177e4 4051{
e879a879 4052 struct r10conf *conf = NULL;
dab8b292 4053 int err = -EINVAL;
deb200d0
N
4054 struct geom geo;
4055 int copies;
4056
4057 copies = setup_geo(&geo, mddev, geo_new);
1da177e4 4058
deb200d0 4059 if (copies == -2) {
08464e09
N
4060 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
4061 mdname(mddev), PAGE_SIZE);
dab8b292 4062 goto out;
1da177e4 4063 }
2604b703 4064
deb200d0 4065 if (copies < 2 || copies > mddev->raid_disks) {
08464e09
N
4066 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
4067 mdname(mddev), mddev->new_layout);
1da177e4
LT
4068 goto out;
4069 }
dab8b292
TM
4070
4071 err = -ENOMEM;
e879a879 4072 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
dab8b292 4073 if (!conf)
1da177e4 4074 goto out;
dab8b292 4075
3ea7daa5 4076 /* FIXME calc properly */
6396bb22
KC
4077 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
4078 sizeof(struct raid10_info),
dab8b292
TM
4079 GFP_KERNEL);
4080 if (!conf->mirrors)
4081 goto out;
4443ae10
N
4082
4083 conf->tmppage = alloc_page(GFP_KERNEL);
4084 if (!conf->tmppage)
dab8b292
TM
4085 goto out;
4086
deb200d0
N
4087 conf->geo = geo;
4088 conf->copies = copies;
3f677f9c 4089 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
c7afa803 4090 rbio_pool_free, conf);
afeee514 4091 if (err)
dab8b292
TM
4092 goto out;
4093
afeee514
KO
4094 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
4095 if (err)
fc9977dd
N
4096 goto out;
4097
6508fdbf 4098 calc_sectors(conf, mddev->dev_sectors);
3ea7daa5
N
4099 if (mddev->reshape_position == MaxSector) {
4100 conf->prev = conf->geo;
4101 conf->reshape_progress = MaxSector;
4102 } else {
4103 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
4104 err = -EINVAL;
4105 goto out;
4106 }
4107 conf->reshape_progress = mddev->reshape_position;
4108 if (conf->prev.far_offset)
4109 conf->prev.stride = 1 << conf->prev.chunk_shift;
4110 else
4111 /* far_copies must be 1 */
4112 conf->prev.stride = conf->dev_sectors;
4113 }
299b0685 4114 conf->reshape_safe = conf->reshape_progress;
e7e72bf6 4115 spin_lock_init(&conf->device_lock);
dab8b292 4116 INIT_LIST_HEAD(&conf->retry_list);
95af587e 4117 INIT_LIST_HEAD(&conf->bio_end_io_list);
dab8b292 4118
b9b083f9 4119 seqlock_init(&conf->resync_lock);
dab8b292 4120 init_waitqueue_head(&conf->wait_barrier);
0e5313e2 4121 atomic_set(&conf->nr_pending, 0);
dab8b292 4122
afeee514 4123 err = -ENOMEM;
44693154
YK
4124 rcu_assign_pointer(conf->thread,
4125 md_register_thread(raid10d, mddev, "raid10"));
dab8b292
TM
4126 if (!conf->thread)
4127 goto out;
4128
dab8b292
TM
4129 conf->mddev = mddev;
4130 return conf;
4131
4132 out:
c9ac2acd 4133 raid10_free_conf(conf);
dab8b292
TM
4134 return ERR_PTR(err);
4135}
4136
16ef5101
CH
4137static void raid10_set_io_opt(struct r10conf *conf)
4138{
4139 int raid_disks = conf->geo.raid_disks;
4140
4141 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4142 raid_disks /= conf->geo.near_copies;
4143 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4144 raid_disks);
4145}
4146
849674e4 4147static int raid10_run(struct mddev *mddev)
dab8b292 4148{
e879a879 4149 struct r10conf *conf;
16ef5101 4150 int i, disk_idx;
dc280d98 4151 struct raid10_info *disk;
3cb03002 4152 struct md_rdev *rdev;
dab8b292 4153 sector_t size;
3ea7daa5
N
4154 sector_t min_offset_diff = 0;
4155 int first = 1;
dab8b292 4156
a415c0f1
N
4157 if (mddev_init_writes_pending(mddev) < 0)
4158 return -ENOMEM;
4159
dab8b292
TM
4160 if (mddev->private == NULL) {
4161 conf = setup_conf(mddev);
4162 if (IS_ERR(conf))
4163 return PTR_ERR(conf);
4164 mddev->private = conf;
4165 }
4166 conf = mddev->private;
4167 if (!conf)
4168 goto out;
4169
44693154
YK
4170 rcu_assign_pointer(mddev->thread, conf->thread);
4171 rcu_assign_pointer(conf->thread, NULL);
f0ddb83d 4172
8db87912
GJ
4173 if (mddev_is_clustered(conf->mddev)) {
4174 int fc, fo;
4175
4176 fc = (mddev->layout >> 8) & 255;
4177 fo = mddev->layout & (1<<16);
4178 if (fc > 1 || fo > 0) {
4179 pr_err("only near layout is supported by clustered"
4180 " raid10\n");
43a52123 4181 goto out_free_conf;
8db87912
GJ
4182 }
4183 }
4184
cc4d1efd 4185 if (mddev->queue) {
3deff1a7 4186 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
16ef5101
CH
4187 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4188 raid10_set_io_opt(conf);
cc4d1efd 4189 }
8f6c2e4b 4190
dafb20fa 4191 rdev_for_each(rdev, mddev) {
3ea7daa5 4192 long long diff;
34b343cf 4193
1da177e4 4194 disk_idx = rdev->raid_disk;
f8c9e74f
N
4195 if (disk_idx < 0)
4196 continue;
4197 if (disk_idx >= conf->geo.raid_disks &&
4198 disk_idx >= conf->prev.raid_disks)
1da177e4
LT
4199 continue;
4200 disk = conf->mirrors + disk_idx;
4201
56a2559b
N
4202 if (test_bit(Replacement, &rdev->flags)) {
4203 if (disk->replacement)
4204 goto out_free_conf;
4205 disk->replacement = rdev;
4206 } else {
4207 if (disk->rdev)
4208 goto out_free_conf;
4209 disk->rdev = rdev;
4210 }
3ea7daa5
N
4211 diff = (rdev->new_data_offset - rdev->data_offset);
4212 if (!mddev->reshape_backwards)
4213 diff = -diff;
4214 if (diff < 0)
4215 diff = 0;
4216 if (first || diff < min_offset_diff)
4217 min_offset_diff = diff;
56a2559b 4218
cc4d1efd
JB
4219 if (mddev->gendisk)
4220 disk_stack_limits(mddev->gendisk, rdev->bdev,
4221 rdev->data_offset << 9);
1da177e4
LT
4222
4223 disk->head_position = 0;
6f287ca6 4224 first = 0;
1da177e4 4225 }
3ea7daa5 4226
6d508242 4227 /* need to check that every block has at least one working mirror */
700c7213 4228 if (!enough(conf, -1)) {
08464e09 4229 pr_err("md/raid10:%s: not enough operational mirrors.\n",
6d508242 4230 mdname(mddev));
1da177e4
LT
4231 goto out_free_conf;
4232 }
4233
3ea7daa5
N
4234 if (conf->reshape_progress != MaxSector) {
4235 /* must ensure that shape change is supported */
4236 if (conf->geo.far_copies != 1 &&
4237 conf->geo.far_offset == 0)
4238 goto out_free_conf;
4239 if (conf->prev.far_copies != 1 &&
78eaa0d4 4240 conf->prev.far_offset == 0)
3ea7daa5
N
4241 goto out_free_conf;
4242 }
4243
1da177e4 4244 mddev->degraded = 0;
f8c9e74f
N
4245 for (i = 0;
4246 i < conf->geo.raid_disks
4247 || i < conf->prev.raid_disks;
4248 i++) {
1da177e4
LT
4249
4250 disk = conf->mirrors + i;
4251
56a2559b
N
4252 if (!disk->rdev && disk->replacement) {
4253 /* The replacement is all we have - use it */
4254 disk->rdev = disk->replacement;
4255 disk->replacement = NULL;
4256 clear_bit(Replacement, &disk->rdev->flags);
4257 }
4258
5fd6c1dc 4259 if (!disk->rdev ||
2e333e89 4260 !test_bit(In_sync, &disk->rdev->flags)) {
1da177e4
LT
4261 disk->head_position = 0;
4262 mddev->degraded++;
0b59bb64
N
4263 if (disk->rdev &&
4264 disk->rdev->saved_raid_disk < 0)
8c2e870a 4265 conf->fullsync = 1;
1da177e4 4266 }
bda31539
BC
4267
4268 if (disk->replacement &&
4269 !test_bit(In_sync, &disk->replacement->flags) &&
4270 disk->replacement->saved_raid_disk < 0) {
4271 conf->fullsync = 1;
4272 }
4273
d890fa2b 4274 disk->recovery_disabled = mddev->recovery_disabled - 1;
1da177e4
LT
4275 }
4276
8c6ac868 4277 if (mddev->recovery_cp != MaxSector)
08464e09
N
4278 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4279 mdname(mddev));
4280 pr_info("md/raid10:%s: active with %d out of %d devices\n",
5cf00fcd
N
4281 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4282 conf->geo.raid_disks);
1da177e4
LT
4283 /*
4284 * Ok, everything is just fine now
4285 */
dab8b292
TM
4286 mddev->dev_sectors = conf->dev_sectors;
4287 size = raid10_size(mddev, 0, 0);
4288 md_set_array_sectors(mddev, size);
4289 mddev->resync_max_sectors = size;
46533ff7 4290 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
1da177e4 4291
a91a2785
MP
4292 if (md_integrity_register(mddev))
4293 goto out_free_conf;
4294
3ea7daa5
N
4295 if (conf->reshape_progress != MaxSector) {
4296 unsigned long before_length, after_length;
4297
4298 before_length = ((1 << conf->prev.chunk_shift) *
4299 conf->prev.far_copies);
4300 after_length = ((1 << conf->geo.chunk_shift) *
4301 conf->geo.far_copies);
4302
4303 if (max(before_length, after_length) > min_offset_diff) {
4304 /* This cannot work */
08464e09 4305 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3ea7daa5
N
4306 goto out_free_conf;
4307 }
4308 conf->offset_diff = min_offset_diff;
4309
3ea7daa5
N
4310 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4311 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4312 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4313 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
44693154
YK
4314 rcu_assign_pointer(mddev->sync_thread,
4315 md_register_thread(md_do_sync, mddev, "reshape"));
e406f12d
AP
4316 if (!mddev->sync_thread)
4317 goto out_free_conf;
3ea7daa5
N
4318 }
4319
1da177e4
LT
4320 return 0;
4321
4322out_free_conf:
7eb8ff02 4323 md_unregister_thread(mddev, &mddev->thread);
c9ac2acd 4324 raid10_free_conf(conf);
1da177e4
LT
4325 mddev->private = NULL;
4326out:
4327 return -EIO;
4328}
4329
afa0f557 4330static void raid10_free(struct mddev *mddev, void *priv)
1da177e4 4331{
c9ac2acd 4332 raid10_free_conf(priv);
1da177e4
LT
4333}
4334
b03e0ccb 4335static void raid10_quiesce(struct mddev *mddev, int quiesce)
6cce3b23 4336{
e879a879 4337 struct r10conf *conf = mddev->private;
6cce3b23 4338
b03e0ccb 4339 if (quiesce)
6cce3b23 4340 raise_barrier(conf, 0);
b03e0ccb 4341 else
6cce3b23 4342 lower_barrier(conf);
6cce3b23 4343}
1da177e4 4344
006a09a0
N
4345static int raid10_resize(struct mddev *mddev, sector_t sectors)
4346{
4347 /* Resize of 'far' arrays is not supported.
4348 * For 'near' and 'offset' arrays we can set the
4349 * number of sectors used to be an appropriate multiple
4350 * of the chunk size.
4351 * For 'offset', this is far_copies*chunksize.
4352 * For 'near' the multiplier is the LCM of
4353 * near_copies and raid_disks.
4354 * So if far_copies > 1 && !far_offset, fail.
4355 * Else find LCM(raid_disks, near_copy)*far_copies and
4356 * multiply by chunk_size. Then round to this number.
4357 * This is mostly done by raid10_size()
4358 */
4359 struct r10conf *conf = mddev->private;
4360 sector_t oldsize, size;
4361
f8c9e74f
N
4362 if (mddev->reshape_position != MaxSector)
4363 return -EBUSY;
4364
5cf00fcd 4365 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
006a09a0
N
4366 return -EINVAL;
4367
4368 oldsize = raid10_size(mddev, 0, 0);
4369 size = raid10_size(mddev, sectors, 0);
a4a6125a
N
4370 if (mddev->external_size &&
4371 mddev->array_sectors > size)
006a09a0 4372 return -EINVAL;
a4a6125a 4373 if (mddev->bitmap) {
e64e4018 4374 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
a4a6125a
N
4375 if (ret)
4376 return ret;
4377 }
4378 md_set_array_sectors(mddev, size);
006a09a0
N
4379 if (sectors > mddev->dev_sectors &&
4380 mddev->recovery_cp > oldsize) {
4381 mddev->recovery_cp = oldsize;
4382 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4383 }
6508fdbf
N
4384 calc_sectors(conf, sectors);
4385 mddev->dev_sectors = conf->dev_sectors;
006a09a0
N
4386 mddev->resync_max_sectors = size;
4387 return 0;
4388}
4389
53a6ab4d 4390static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
dab8b292 4391{
3cb03002 4392 struct md_rdev *rdev;
e879a879 4393 struct r10conf *conf;
dab8b292
TM
4394
4395 if (mddev->degraded > 0) {
08464e09
N
4396 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4397 mdname(mddev));
dab8b292
TM
4398 return ERR_PTR(-EINVAL);
4399 }
53a6ab4d 4400 sector_div(size, devs);
dab8b292 4401
dab8b292
TM
4402 /* Set new parameters */
4403 mddev->new_level = 10;
4404 /* new layout: far_copies = 1, near_copies = 2 */
4405 mddev->new_layout = (1<<8) + 2;
4406 mddev->new_chunk_sectors = mddev->chunk_sectors;
4407 mddev->delta_disks = mddev->raid_disks;
dab8b292
TM
4408 mddev->raid_disks *= 2;
4409 /* make sure it will be not marked as dirty */
4410 mddev->recovery_cp = MaxSector;
53a6ab4d 4411 mddev->dev_sectors = size;
dab8b292
TM
4412
4413 conf = setup_conf(mddev);
02214dc5 4414 if (!IS_ERR(conf)) {
dafb20fa 4415 rdev_for_each(rdev, mddev)
53a6ab4d 4416 if (rdev->raid_disk >= 0) {
e93f68a1 4417 rdev->new_raid_disk = rdev->raid_disk * 2;
53a6ab4d
N
4418 rdev->sectors = size;
4419 }
02214dc5
KW
4420 }
4421
dab8b292
TM
4422 return conf;
4423}
4424
fd01b88c 4425static void *raid10_takeover(struct mddev *mddev)
dab8b292 4426{
e373ab10 4427 struct r0conf *raid0_conf;
dab8b292
TM
4428
4429 /* raid10 can take over:
4430 * raid0 - providing it has only two drives
4431 */
4432 if (mddev->level == 0) {
4433 /* for raid0 takeover only one zone is supported */
e373ab10
N
4434 raid0_conf = mddev->private;
4435 if (raid0_conf->nr_strip_zones > 1) {
08464e09
N
4436 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4437 mdname(mddev));
dab8b292
TM
4438 return ERR_PTR(-EINVAL);
4439 }
53a6ab4d
N
4440 return raid10_takeover_raid0(mddev,
4441 raid0_conf->strip_zone->zone_end,
4442 raid0_conf->strip_zone->nb_dev);
dab8b292
TM
4443 }
4444 return ERR_PTR(-EINVAL);
4445}
4446
3ea7daa5
N
4447static int raid10_check_reshape(struct mddev *mddev)
4448{
4449 /* Called when there is a request to change
4450 * - layout (to ->new_layout)
4451 * - chunk size (to ->new_chunk_sectors)
4452 * - raid_disks (by delta_disks)
4453 * or when trying to restart a reshape that was ongoing.
4454 *
4455 * We need to validate the request and possibly allocate
4456 * space if that might be an issue later.
4457 *
4458 * Currently we reject any reshape of a 'far' mode array,
4459 * allow chunk size to change if new is generally acceptable,
4460 * allow raid_disks to increase, and allow
4461 * a switch between 'near' mode and 'offset' mode.
4462 */
4463 struct r10conf *conf = mddev->private;
4464 struct geom geo;
4465
4466 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4467 return -EINVAL;
4468
4469 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4470 /* mustn't change number of copies */
4471 return -EINVAL;
4472 if (geo.far_copies > 1 && !geo.far_offset)
4473 /* Cannot switch to 'far' mode */
4474 return -EINVAL;
4475
4476 if (mddev->array_sectors & geo.chunk_mask)
4477 /* not factor of array size */
4478 return -EINVAL;
4479
3ea7daa5
N
4480 if (!enough(conf, -1))
4481 return -EINVAL;
4482
4483 kfree(conf->mirrors_new);
4484 conf->mirrors_new = NULL;
4485 if (mddev->delta_disks > 0) {
4486 /* allocate new 'mirrors' list */
6396bb22
KC
4487 conf->mirrors_new =
4488 kcalloc(mddev->raid_disks + mddev->delta_disks,
4489 sizeof(struct raid10_info),
4490 GFP_KERNEL);
3ea7daa5
N
4491 if (!conf->mirrors_new)
4492 return -ENOMEM;
4493 }
4494 return 0;
4495}
4496
4497/*
4498 * Need to check if array has failed when deciding whether to:
4499 * - start an array
4500 * - remove non-faulty devices
4501 * - add a spare
4502 * - allow a reshape
4503 * This determination is simple when no reshape is happening.
4504 * However if there is a reshape, we need to carefully check
4505 * both the before and after sections.
4506 * This is because some failed devices may only affect one
4507 * of the two sections, and some non-in_sync devices may
4508 * be insync in the section most affected by failed devices.
4509 */
4510static int calc_degraded(struct r10conf *conf)
4511{
4512 int degraded, degraded2;
4513 int i;
4514
4515 rcu_read_lock();
4516 degraded = 0;
4517 /* 'prev' section first */
4518 for (i = 0; i < conf->prev.raid_disks; i++) {
4519 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4520 if (!rdev || test_bit(Faulty, &rdev->flags))
4521 degraded++;
4522 else if (!test_bit(In_sync, &rdev->flags))
4523 /* When we can reduce the number of devices in
4524 * an array, this might not contribute to
4525 * 'degraded'. It does now.
4526 */
4527 degraded++;
4528 }
4529 rcu_read_unlock();
4530 if (conf->geo.raid_disks == conf->prev.raid_disks)
4531 return degraded;
4532 rcu_read_lock();
4533 degraded2 = 0;
4534 for (i = 0; i < conf->geo.raid_disks; i++) {
4535 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4536 if (!rdev || test_bit(Faulty, &rdev->flags))
4537 degraded2++;
4538 else if (!test_bit(In_sync, &rdev->flags)) {
4539 /* If reshape is increasing the number of devices,
4540 * this section has already been recovered, so
4541 * it doesn't contribute to degraded.
4542 * else it does.
4543 */
4544 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4545 degraded2++;
4546 }
4547 }
4548 rcu_read_unlock();
4549 if (degraded2 > degraded)
4550 return degraded2;
4551 return degraded;
4552}
4553
4554static int raid10_start_reshape(struct mddev *mddev)
4555{
4556 /* A 'reshape' has been requested. This commits
4557 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4558 * This also checks if there are enough spares and adds them
4559 * to the array.
4560 * We currently require enough spares to make the final
4561 * array non-degraded. We also require that the difference
4562 * between old and new data_offset - on each device - is
4563 * enough that we never risk over-writing.
4564 */
4565
4566 unsigned long before_length, after_length;
4567 sector_t min_offset_diff = 0;
4568 int first = 1;
4569 struct geom new;
4570 struct r10conf *conf = mddev->private;
4571 struct md_rdev *rdev;
4572 int spares = 0;
bb63a701 4573 int ret;
3ea7daa5
N
4574
4575 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4576 return -EBUSY;
4577
4578 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4579 return -EINVAL;
4580
4581 before_length = ((1 << conf->prev.chunk_shift) *
4582 conf->prev.far_copies);
4583 after_length = ((1 << conf->geo.chunk_shift) *
4584 conf->geo.far_copies);
4585
4586 rdev_for_each(rdev, mddev) {
4587 if (!test_bit(In_sync, &rdev->flags)
4588 && !test_bit(Faulty, &rdev->flags))
4589 spares++;
4590 if (rdev->raid_disk >= 0) {
4591 long long diff = (rdev->new_data_offset
4592 - rdev->data_offset);
4593 if (!mddev->reshape_backwards)
4594 diff = -diff;
4595 if (diff < 0)
4596 diff = 0;
4597 if (first || diff < min_offset_diff)
4598 min_offset_diff = diff;
b506335e 4599 first = 0;
3ea7daa5
N
4600 }
4601 }
4602
4603 if (max(before_length, after_length) > min_offset_diff)
4604 return -EINVAL;
4605
4606 if (spares < mddev->delta_disks)
4607 return -EINVAL;
4608
4609 conf->offset_diff = min_offset_diff;
4610 spin_lock_irq(&conf->device_lock);
4611 if (conf->mirrors_new) {
4612 memcpy(conf->mirrors_new, conf->mirrors,
dc280d98 4613 sizeof(struct raid10_info)*conf->prev.raid_disks);
3ea7daa5 4614 smp_mb();
c4796e21 4615 kfree(conf->mirrors_old);
3ea7daa5
N
4616 conf->mirrors_old = conf->mirrors;
4617 conf->mirrors = conf->mirrors_new;
4618 conf->mirrors_new = NULL;
4619 }
4620 setup_geo(&conf->geo, mddev, geo_start);
4621 smp_mb();
4622 if (mddev->reshape_backwards) {
4623 sector_t size = raid10_size(mddev, 0, 0);
4624 if (size < mddev->array_sectors) {
4625 spin_unlock_irq(&conf->device_lock);
08464e09
N
4626 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4627 mdname(mddev));
3ea7daa5
N
4628 return -EINVAL;
4629 }
4630 mddev->resync_max_sectors = size;
4631 conf->reshape_progress = size;
4632 } else
4633 conf->reshape_progress = 0;
299b0685 4634 conf->reshape_safe = conf->reshape_progress;
3ea7daa5
N
4635 spin_unlock_irq(&conf->device_lock);
4636
bb63a701 4637 if (mddev->delta_disks && mddev->bitmap) {
afd75628
GJ
4638 struct mdp_superblock_1 *sb = NULL;
4639 sector_t oldsize, newsize;
4640
4641 oldsize = raid10_size(mddev, 0, 0);
4642 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4643
4644 if (!mddev_is_clustered(mddev)) {
4645 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4646 if (ret)
4647 goto abort;
4648 else
4649 goto out;
4650 }
4651
4652 rdev_for_each(rdev, mddev) {
4653 if (rdev->raid_disk > -1 &&
4654 !test_bit(Faulty, &rdev->flags))
4655 sb = page_address(rdev->sb_page);
4656 }
4657
4658 /*
4659 * some node is already performing reshape, and no need to
4660 * call md_bitmap_resize again since it should be called when
4661 * receiving BITMAP_RESIZE msg
4662 */
4663 if ((sb && (le32_to_cpu(sb->feature_map) &
4664 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4665 goto out;
4666
4667 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
bb63a701
N
4668 if (ret)
4669 goto abort;
afd75628
GJ
4670
4671 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4672 if (ret) {
4673 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4674 goto abort;
4675 }
bb63a701 4676 }
afd75628 4677out:
3ea7daa5
N
4678 if (mddev->delta_disks > 0) {
4679 rdev_for_each(rdev, mddev)
4680 if (rdev->raid_disk < 0 &&
4681 !test_bit(Faulty, &rdev->flags)) {
4682 if (raid10_add_disk(mddev, rdev) == 0) {
4683 if (rdev->raid_disk >=
4684 conf->prev.raid_disks)
4685 set_bit(In_sync, &rdev->flags);
4686 else
4687 rdev->recovery_offset = 0;
4688
38ffc01f
DLM
4689 /* Failure here is OK */
4690 sysfs_link_rdev(mddev, rdev);
3ea7daa5
N
4691 }
4692 } else if (rdev->raid_disk >= conf->prev.raid_disks
4693 && !test_bit(Faulty, &rdev->flags)) {
4694 /* This is a spare that was manually added */
4695 set_bit(In_sync, &rdev->flags);
4696 }
4697 }
4698 /* When a reshape changes the number of devices,
4699 * ->degraded is measured against the larger of the
4700 * pre and post numbers.
4701 */
4702 spin_lock_irq(&conf->device_lock);
4703 mddev->degraded = calc_degraded(conf);
4704 spin_unlock_irq(&conf->device_lock);
4705 mddev->raid_disks = conf->geo.raid_disks;
4706 mddev->reshape_position = conf->reshape_progress;
2953079c 4707 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3ea7daa5
N
4708
4709 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4710 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
ea358cd0 4711 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
3ea7daa5
N
4712 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4713 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4714
44693154
YK
4715 rcu_assign_pointer(mddev->sync_thread,
4716 md_register_thread(md_do_sync, mddev, "reshape"));
3ea7daa5 4717 if (!mddev->sync_thread) {
bb63a701
N
4718 ret = -EAGAIN;
4719 goto abort;
3ea7daa5
N
4720 }
4721 conf->reshape_checkpoint = jiffies;
4722 md_wakeup_thread(mddev->sync_thread);
54679486 4723 md_new_event();
3ea7daa5 4724 return 0;
bb63a701
N
4725
4726abort:
4727 mddev->recovery = 0;
4728 spin_lock_irq(&conf->device_lock);
4729 conf->geo = conf->prev;
4730 mddev->raid_disks = conf->geo.raid_disks;
4731 rdev_for_each(rdev, mddev)
4732 rdev->new_data_offset = rdev->data_offset;
4733 smp_wmb();
4734 conf->reshape_progress = MaxSector;
299b0685 4735 conf->reshape_safe = MaxSector;
bb63a701
N
4736 mddev->reshape_position = MaxSector;
4737 spin_unlock_irq(&conf->device_lock);
4738 return ret;
3ea7daa5
N
4739}
4740
4741/* Calculate the last device-address that could contain
4742 * any block from the chunk that includes the array-address 's'
4743 * and report the next address.
4744 * i.e. the address returned will be chunk-aligned and after
4745 * any data that is in the chunk containing 's'.
4746 */
4747static sector_t last_dev_address(sector_t s, struct geom *geo)
4748{
4749 s = (s | geo->chunk_mask) + 1;
4750 s >>= geo->chunk_shift;
4751 s *= geo->near_copies;
4752 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4753 s *= geo->far_copies;
4754 s <<= geo->chunk_shift;
4755 return s;
4756}
4757
4758/* Calculate the first device-address that could contain
4759 * any block from the chunk that includes the array-address 's'.
4760 * This too will be the start of a chunk
4761 */
4762static sector_t first_dev_address(sector_t s, struct geom *geo)
4763{
4764 s >>= geo->chunk_shift;
4765 s *= geo->near_copies;
4766 sector_div(s, geo->raid_disks);
4767 s *= geo->far_copies;
4768 s <<= geo->chunk_shift;
4769 return s;
4770}
4771
4772static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4773 int *skipped)
4774{
4775 /* We simply copy at most one chunk (smallest of old and new)
4776 * at a time, possibly less if that exceeds RESYNC_PAGES,
4777 * or we hit a bad block or something.
4778 * This might mean we pause for normal IO in the middle of
02ec5026 4779 * a chunk, but that is not a problem as mddev->reshape_position
3ea7daa5
N
4780 * can record any location.
4781 *
4782 * If we will want to write to a location that isn't
4783 * yet recorded as 'safe' (i.e. in metadata on disk) then
4784 * we need to flush all reshape requests and update the metadata.
4785 *
4786 * When reshaping forwards (e.g. to more devices), we interpret
4787 * 'safe' as the earliest block which might not have been copied
4788 * down yet. We divide this by previous stripe size and multiply
4789 * by previous stripe length to get lowest device offset that we
4790 * cannot write to yet.
4791 * We interpret 'sector_nr' as an address that we want to write to.
4792 * From this we use last_device_address() to find where we might
4793 * write to, and first_device_address on the 'safe' position.
4794 * If this 'next' write position is after the 'safe' position,
4795 * we must update the metadata to increase the 'safe' position.
4796 *
4797 * When reshaping backwards, we round in the opposite direction
4798 * and perform the reverse test: next write position must not be
4799 * less than current safe position.
4800 *
4801 * In all this the minimum difference in data offsets
4802 * (conf->offset_diff - always positive) allows a bit of slack,
02ec5026 4803 * so next can be after 'safe', but not by more than offset_diff
3ea7daa5
N
4804 *
4805 * We need to prepare all the bios here before we start any IO
4806 * to ensure the size we choose is acceptable to all devices.
4807 * The means one for each copy for write-out and an extra one for
4808 * read-in.
4809 * We store the read-in bio in ->master_bio and the others in
4810 * ->devs[x].bio and ->devs[x].repl_bio.
4811 */
4812 struct r10conf *conf = mddev->private;
4813 struct r10bio *r10_bio;
4814 sector_t next, safe, last;
4815 int max_sectors;
4816 int nr_sectors;
4817 int s;
4818 struct md_rdev *rdev;
4819 int need_flush = 0;
4820 struct bio *blist;
4821 struct bio *bio, *read_bio;
4822 int sectors_done = 0;
f0250618 4823 struct page **pages;
3ea7daa5
N
4824
4825 if (sector_nr == 0) {
4826 /* If restarting in the middle, skip the initial sectors */
4827 if (mddev->reshape_backwards &&
4828 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4829 sector_nr = (raid10_size(mddev, 0, 0)
4830 - conf->reshape_progress);
4831 } else if (!mddev->reshape_backwards &&
4832 conf->reshape_progress > 0)
4833 sector_nr = conf->reshape_progress;
4834 if (sector_nr) {
4835 mddev->curr_resync_completed = sector_nr;
e1a86dbb 4836 sysfs_notify_dirent_safe(mddev->sysfs_completed);
3ea7daa5
N
4837 *skipped = 1;
4838 return sector_nr;
4839 }
4840 }
4841
4842 /* We don't use sector_nr to track where we are up to
4843 * as that doesn't work well for ->reshape_backwards.
4844 * So just use ->reshape_progress.
4845 */
4846 if (mddev->reshape_backwards) {
4847 /* 'next' is the earliest device address that we might
4848 * write to for this chunk in the new layout
4849 */
4850 next = first_dev_address(conf->reshape_progress - 1,
4851 &conf->geo);
4852
4853 /* 'safe' is the last device address that we might read from
4854 * in the old layout after a restart
4855 */
4856 safe = last_dev_address(conf->reshape_safe - 1,
4857 &conf->prev);
4858
4859 if (next + conf->offset_diff < safe)
4860 need_flush = 1;
4861
4862 last = conf->reshape_progress - 1;
4863 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4864 & conf->prev.chunk_mask);
e287308b
ZL
4865 if (sector_nr + RESYNC_SECTORS < last)
4866 sector_nr = last + 1 - RESYNC_SECTORS;
3ea7daa5
N
4867 } else {
4868 /* 'next' is after the last device address that we
4869 * might write to for this chunk in the new layout
4870 */
4871 next = last_dev_address(conf->reshape_progress, &conf->geo);
4872
4873 /* 'safe' is the earliest device address that we might
4874 * read from in the old layout after a restart
4875 */
4876 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4877
4878 /* Need to update metadata if 'next' might be beyond 'safe'
4879 * as that would possibly corrupt data
4880 */
4881 if (next > safe + conf->offset_diff)
4882 need_flush = 1;
4883
4884 sector_nr = conf->reshape_progress;
4885 last = sector_nr | (conf->geo.chunk_mask
4886 & conf->prev.chunk_mask);
4887
e287308b
ZL
4888 if (sector_nr + RESYNC_SECTORS <= last)
4889 last = sector_nr + RESYNC_SECTORS - 1;
3ea7daa5
N
4890 }
4891
4892 if (need_flush ||
4893 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4894 /* Need to update reshape_position in metadata */
c9aa889b 4895 wait_barrier(conf, false);
3ea7daa5
N
4896 mddev->reshape_position = conf->reshape_progress;
4897 if (mddev->reshape_backwards)
4898 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4899 - conf->reshape_progress;
4900 else
4901 mddev->curr_resync_completed = conf->reshape_progress;
4902 conf->reshape_checkpoint = jiffies;
2953079c 4903 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3ea7daa5 4904 md_wakeup_thread(mddev->thread);
2953079c 4905 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
c91abf5a
N
4906 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4907 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4908 allow_barrier(conf);
4909 return sectors_done;
4910 }
3ea7daa5
N
4911 conf->reshape_safe = mddev->reshape_position;
4912 allow_barrier(conf);
4913 }
4914
1d0ffd26 4915 raise_barrier(conf, 0);
3ea7daa5
N
4916read_more:
4917 /* Now schedule reads for blocks from sector_nr to last */
208410b5 4918 r10_bio = raid10_alloc_init_r10buf(conf);
cb8b12b5 4919 r10_bio->state = 0;
1d0ffd26 4920 raise_barrier(conf, 1);
3ea7daa5
N
4921 atomic_set(&r10_bio->remaining, 0);
4922 r10_bio->mddev = mddev;
4923 r10_bio->sector = sector_nr;
4924 set_bit(R10BIO_IsReshape, &r10_bio->state);
4925 r10_bio->sectors = last - sector_nr + 1;
4926 rdev = read_balance(conf, r10_bio, &max_sectors);
4927 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4928
4929 if (!rdev) {
4930 /* Cannot read from here, so need to record bad blocks
4931 * on all the target devices.
4932 */
4933 // FIXME
afeee514 4934 mempool_free(r10_bio, &conf->r10buf_pool);
3ea7daa5
N
4935 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4936 return sectors_done;
4937 }
4938
609be106
CH
4939 read_bio = bio_alloc_bioset(rdev->bdev, RESYNC_PAGES, REQ_OP_READ,
4940 GFP_KERNEL, &mddev->bio_set);
4f024f37 4941 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
3ea7daa5
N
4942 + rdev->data_offset);
4943 read_bio->bi_private = r10_bio;
81fa1520 4944 read_bio->bi_end_io = end_reshape_read;
3ea7daa5
N
4945 r10_bio->master_bio = read_bio;
4946 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4947
7564beda
GJ
4948 /*
4949 * Broadcast RESYNC message to other nodes, so all nodes would not
4950 * write to the region to avoid conflict.
4951 */
4952 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4953 struct mdp_superblock_1 *sb = NULL;
4954 int sb_reshape_pos = 0;
4955
4956 conf->cluster_sync_low = sector_nr;
4957 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4958 sb = page_address(rdev->sb_page);
4959 if (sb) {
4960 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4961 /*
4962 * Set cluster_sync_low again if next address for array
4963 * reshape is less than cluster_sync_low. Since we can't
4964 * update cluster_sync_low until it has finished reshape.
4965 */
4966 if (sb_reshape_pos < conf->cluster_sync_low)
4967 conf->cluster_sync_low = sb_reshape_pos;
4968 }
4969
4970 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4971 conf->cluster_sync_high);
4972 }
4973
3ea7daa5
N
4974 /* Now find the locations in the new layout */
4975 __raid10_find_phys(&conf->geo, r10_bio);
4976
4977 blist = read_bio;
4978 read_bio->bi_next = NULL;
4979
d094d686 4980 rcu_read_lock();
3ea7daa5
N
4981 for (s = 0; s < conf->copies*2; s++) {
4982 struct bio *b;
4983 int d = r10_bio->devs[s/2].devnum;
4984 struct md_rdev *rdev2;
4985 if (s&1) {
d094d686 4986 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
3ea7daa5
N
4987 b = r10_bio->devs[s/2].repl_bio;
4988 } else {
d094d686 4989 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
3ea7daa5
N
4990 b = r10_bio->devs[s/2].bio;
4991 }
4992 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4993 continue;
8be185f2 4994
74d46992 4995 bio_set_dev(b, rdev2->bdev);
4f024f37
KO
4996 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4997 rdev2->new_data_offset;
3ea7daa5 4998 b->bi_end_io = end_reshape_write;
c34b7ac6 4999 b->bi_opf = REQ_OP_WRITE;
3ea7daa5 5000 b->bi_next = blist;
3ea7daa5
N
5001 blist = b;
5002 }
5003
5004 /* Now add as many pages as possible to all of these bios. */
5005
5006 nr_sectors = 0;
f0250618 5007 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
3ea7daa5 5008 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
f0250618 5009 struct page *page = pages[s / (PAGE_SIZE >> 9)];
3ea7daa5
N
5010 int len = (max_sectors - s) << 9;
5011 if (len > PAGE_SIZE)
5012 len = PAGE_SIZE;
5013 for (bio = blist; bio ; bio = bio->bi_next) {
0c67dd64
JT
5014 if (WARN_ON(!bio_add_page(bio, page, len, 0))) {
5015 bio->bi_status = BLK_STS_RESOURCE;
5016 bio_endio(bio);
5017 return sectors_done;
5018 }
3ea7daa5
N
5019 }
5020 sector_nr += len >> 9;
5021 nr_sectors += len >> 9;
5022 }
d094d686 5023 rcu_read_unlock();
3ea7daa5
N
5024 r10_bio->sectors = nr_sectors;
5025
5026 /* Now submit the read */
74d46992 5027 md_sync_acct_bio(read_bio, r10_bio->sectors);
3ea7daa5
N
5028 atomic_inc(&r10_bio->remaining);
5029 read_bio->bi_next = NULL;
ed00aabd 5030 submit_bio_noacct(read_bio);
3ea7daa5
N
5031 sectors_done += nr_sectors;
5032 if (sector_nr <= last)
5033 goto read_more;
5034
1d0ffd26
XN
5035 lower_barrier(conf);
5036
3ea7daa5
N
5037 /* Now that we have done the whole section we can
5038 * update reshape_progress
5039 */
5040 if (mddev->reshape_backwards)
5041 conf->reshape_progress -= sectors_done;
5042 else
5043 conf->reshape_progress += sectors_done;
5044
5045 return sectors_done;
5046}
5047
5048static void end_reshape_request(struct r10bio *r10_bio);
5049static int handle_reshape_read_error(struct mddev *mddev,
5050 struct r10bio *r10_bio);
5051static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
5052{
5053 /* Reshape read completed. Hopefully we have a block
5054 * to write out.
5055 * If we got a read error then we do sync 1-page reads from
5056 * elsewhere until we find the data - or give up.
5057 */
5058 struct r10conf *conf = mddev->private;
5059 int s;
5060
5061 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
5062 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
5063 /* Reshape has been aborted */
5064 md_done_sync(mddev, r10_bio->sectors, 0);
5065 return;
5066 }
5067
5068 /* We definitely have the data in the pages, schedule the
5069 * writes.
5070 */
5071 atomic_set(&r10_bio->remaining, 1);
5072 for (s = 0; s < conf->copies*2; s++) {
5073 struct bio *b;
5074 int d = r10_bio->devs[s/2].devnum;
5075 struct md_rdev *rdev;
d094d686 5076 rcu_read_lock();
3ea7daa5 5077 if (s&1) {
d094d686 5078 rdev = rcu_dereference(conf->mirrors[d].replacement);
3ea7daa5
N
5079 b = r10_bio->devs[s/2].repl_bio;
5080 } else {
d094d686 5081 rdev = rcu_dereference(conf->mirrors[d].rdev);
3ea7daa5
N
5082 b = r10_bio->devs[s/2].bio;
5083 }
d094d686
N
5084 if (!rdev || test_bit(Faulty, &rdev->flags)) {
5085 rcu_read_unlock();
3ea7daa5 5086 continue;
d094d686 5087 }
3ea7daa5 5088 atomic_inc(&rdev->nr_pending);
d094d686 5089 rcu_read_unlock();
74d46992 5090 md_sync_acct_bio(b, r10_bio->sectors);
3ea7daa5
N
5091 atomic_inc(&r10_bio->remaining);
5092 b->bi_next = NULL;
ed00aabd 5093 submit_bio_noacct(b);
3ea7daa5
N
5094 }
5095 end_reshape_request(r10_bio);
5096}
5097
5098static void end_reshape(struct r10conf *conf)
5099{
5100 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5101 return;
5102
5103 spin_lock_irq(&conf->device_lock);
5104 conf->prev = conf->geo;
5105 md_finish_reshape(conf->mddev);
5106 smp_wmb();
5107 conf->reshape_progress = MaxSector;
299b0685 5108 conf->reshape_safe = MaxSector;
3ea7daa5
N
5109 spin_unlock_irq(&conf->device_lock);
5110
c2e4cd57 5111 if (conf->mddev->queue)
16ef5101 5112 raid10_set_io_opt(conf);
3ea7daa5
N
5113 conf->fullsync = 0;
5114}
5115
7564beda
GJ
5116static void raid10_update_reshape_pos(struct mddev *mddev)
5117{
5118 struct r10conf *conf = mddev->private;
5ebaf80b 5119 sector_t lo, hi;
7564beda 5120
5ebaf80b
GJ
5121 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5122 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5123 || mddev->reshape_position == MaxSector)
5124 conf->reshape_progress = mddev->reshape_position;
5125 else
5126 WARN_ON_ONCE(1);
7564beda
GJ
5127}
5128
3ea7daa5
N
5129static int handle_reshape_read_error(struct mddev *mddev,
5130 struct r10bio *r10_bio)
5131{
5132 /* Use sync reads to get the blocks from somewhere else */
5133 int sectors = r10_bio->sectors;
3ea7daa5 5134 struct r10conf *conf = mddev->private;
584ed9fa 5135 struct r10bio *r10b;
3ea7daa5
N
5136 int slot = 0;
5137 int idx = 0;
2d06e3b7
ML
5138 struct page **pages;
5139
8cf05a78 5140 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
584ed9fa
MK
5141 if (!r10b) {
5142 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5143 return -ENOMEM;
5144 }
5145
2d06e3b7
ML
5146 /* reshape IOs share pages from .devs[0].bio */
5147 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
3ea7daa5 5148
e0ee7785
N
5149 r10b->sector = r10_bio->sector;
5150 __raid10_find_phys(&conf->prev, r10b);
3ea7daa5
N
5151
5152 while (sectors) {
5153 int s = sectors;
5154 int success = 0;
5155 int first_slot = slot;
5156
5157 if (s > (PAGE_SIZE >> 9))
5158 s = PAGE_SIZE >> 9;
5159
d094d686 5160 rcu_read_lock();
3ea7daa5 5161 while (!success) {
e0ee7785 5162 int d = r10b->devs[slot].devnum;
d094d686 5163 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
3ea7daa5
N
5164 sector_t addr;
5165 if (rdev == NULL ||
5166 test_bit(Faulty, &rdev->flags) ||
5167 !test_bit(In_sync, &rdev->flags))
5168 goto failed;
5169
e0ee7785 5170 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
d094d686
N
5171 atomic_inc(&rdev->nr_pending);
5172 rcu_read_unlock();
3ea7daa5
N
5173 success = sync_page_io(rdev,
5174 addr,
5175 s << 9,
2d06e3b7 5176 pages[idx],
4ce4c73f 5177 REQ_OP_READ, false);
d094d686
N
5178 rdev_dec_pending(rdev, mddev);
5179 rcu_read_lock();
3ea7daa5
N
5180 if (success)
5181 break;
5182 failed:
5183 slot++;
5184 if (slot >= conf->copies)
5185 slot = 0;
5186 if (slot == first_slot)
5187 break;
5188 }
d094d686 5189 rcu_read_unlock();
3ea7daa5
N
5190 if (!success) {
5191 /* couldn't read this block, must give up */
5192 set_bit(MD_RECOVERY_INTR,
5193 &mddev->recovery);
584ed9fa 5194 kfree(r10b);
3ea7daa5
N
5195 return -EIO;
5196 }
5197 sectors -= s;
5198 idx++;
5199 }
584ed9fa 5200 kfree(r10b);
3ea7daa5
N
5201 return 0;
5202}
5203
4246a0b6 5204static void end_reshape_write(struct bio *bio)
3ea7daa5 5205{
f0250618 5206 struct r10bio *r10_bio = get_resync_r10bio(bio);
3ea7daa5
N
5207 struct mddev *mddev = r10_bio->mddev;
5208 struct r10conf *conf = mddev->private;
5209 int d;
5210 int slot;
5211 int repl;
5212 struct md_rdev *rdev = NULL;
5213
5214 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5215 if (repl)
5216 rdev = conf->mirrors[d].replacement;
5217 if (!rdev) {
5218 smp_mb();
5219 rdev = conf->mirrors[d].rdev;
5220 }
5221
4e4cbee9 5222 if (bio->bi_status) {
3ea7daa5
N
5223 /* FIXME should record badblock */
5224 md_error(mddev, rdev);
5225 }
5226
5227 rdev_dec_pending(rdev, mddev);
5228 end_reshape_request(r10_bio);
5229}
5230
5231static void end_reshape_request(struct r10bio *r10_bio)
5232{
5233 if (!atomic_dec_and_test(&r10_bio->remaining))
5234 return;
5235 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5236 bio_put(r10_bio->master_bio);
5237 put_buf(r10_bio);
5238}
5239
5240static void raid10_finish_reshape(struct mddev *mddev)
5241{
5242 struct r10conf *conf = mddev->private;
5243
5244 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5245 return;
5246
5247 if (mddev->delta_disks > 0) {
3ea7daa5
N
5248 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5249 mddev->recovery_cp = mddev->resync_max_sectors;
5250 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5251 }
8876391e 5252 mddev->resync_max_sectors = mddev->array_sectors;
63aced61
N
5253 } else {
5254 int d;
d094d686 5255 rcu_read_lock();
63aced61
N
5256 for (d = conf->geo.raid_disks ;
5257 d < conf->geo.raid_disks - mddev->delta_disks;
5258 d++) {
d094d686 5259 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
63aced61
N
5260 if (rdev)
5261 clear_bit(In_sync, &rdev->flags);
d094d686 5262 rdev = rcu_dereference(conf->mirrors[d].replacement);
63aced61
N
5263 if (rdev)
5264 clear_bit(In_sync, &rdev->flags);
5265 }
d094d686 5266 rcu_read_unlock();
3ea7daa5
N
5267 }
5268 mddev->layout = mddev->new_layout;
5269 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5270 mddev->reshape_position = MaxSector;
5271 mddev->delta_disks = 0;
5272 mddev->reshape_backwards = 0;
5273}
5274
84fc4b56 5275static struct md_personality raid10_personality =
1da177e4
LT
5276{
5277 .name = "raid10",
2604b703 5278 .level = 10,
1da177e4 5279 .owner = THIS_MODULE,
849674e4
SL
5280 .make_request = raid10_make_request,
5281 .run = raid10_run,
afa0f557 5282 .free = raid10_free,
849674e4
SL
5283 .status = raid10_status,
5284 .error_handler = raid10_error,
1da177e4
LT
5285 .hot_add_disk = raid10_add_disk,
5286 .hot_remove_disk= raid10_remove_disk,
5287 .spare_active = raid10_spare_active,
849674e4 5288 .sync_request = raid10_sync_request,
6cce3b23 5289 .quiesce = raid10_quiesce,
80c3a6ce 5290 .size = raid10_size,
006a09a0 5291 .resize = raid10_resize,
dab8b292 5292 .takeover = raid10_takeover,
3ea7daa5
N
5293 .check_reshape = raid10_check_reshape,
5294 .start_reshape = raid10_start_reshape,
5295 .finish_reshape = raid10_finish_reshape,
7564beda 5296 .update_reshape_pos = raid10_update_reshape_pos,
1da177e4
LT
5297};
5298
5299static int __init raid_init(void)
5300{
2604b703 5301 return register_md_personality(&raid10_personality);
1da177e4
LT
5302}
5303
5304static void raid_exit(void)
5305{
2604b703 5306 unregister_md_personality(&raid10_personality);
1da177e4
LT
5307}
5308
5309module_init(raid_init);
5310module_exit(raid_exit);
5311MODULE_LICENSE("GPL");
0efb9e61 5312MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
1da177e4 5313MODULE_ALIAS("md-personality-9"); /* RAID10 */
d9d166c2 5314MODULE_ALIAS("md-raid10");
2604b703 5315MODULE_ALIAS("md-level-10");