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