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