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