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