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