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