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