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