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