md: raid 10 supports TRIM
[GitHub/mt8127/android_kernel_alcatel_ttab.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
N
501 if (dec_rdev)
502 rdev_dec_pending(conf->mirrors[dev].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
b4fdcb02 1058static void make_request(struct mddev *mddev, struct bio * bio)
1da177e4 1059{
e879a879 1060 struct r10conf *conf = mddev->private;
9f2c9d12 1061 struct r10bio *r10_bio;
1da177e4
LT
1062 struct bio *read_bio;
1063 int i;
f8c9e74f 1064 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
5cf00fcd 1065 int chunk_sects = chunk_mask + 1;
a362357b 1066 const int rw = bio_data_dir(bio);
2c7d46ec 1067 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
e9c7469b 1068 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
532a2a3f
SL
1069 const unsigned long do_discard = (bio->bi_rw
1070 & (REQ_DISCARD | REQ_SECURE));
6cce3b23 1071 unsigned long flags;
3cb03002 1072 struct md_rdev *blocked_rdev;
d4432c23
N
1073 int sectors_handled;
1074 int max_sectors;
3ea7daa5 1075 int sectors;
1da177e4 1076
e9c7469b
TH
1077 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
1078 md_flush_request(mddev, bio);
5a7bbad2 1079 return;
e5dcdd80
N
1080 }
1081
1da177e4
LT
1082 /* If this request crosses a chunk boundary, we need to
1083 * split it. This will only happen for 1 PAGE (or less) requests.
1084 */
5cf00fcd
N
1085 if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9)
1086 > chunk_sects
f8c9e74f
N
1087 && (conf->geo.near_copies < conf->geo.raid_disks
1088 || conf->prev.near_copies < conf->prev.raid_disks))) {
1da177e4
LT
1089 struct bio_pair *bp;
1090 /* Sanity check -- queue functions should prevent this happening */
532a2a3f 1091 if ((bio->bi_vcnt != 1 && bio->bi_vcnt != 0) ||
1da177e4
LT
1092 bio->bi_idx != 0)
1093 goto bad_map;
1094 /* This is a one page bio that upper layers
1095 * refuse to split for us, so we need to split it.
1096 */
6feef531 1097 bp = bio_split(bio,
1da177e4 1098 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
51e9ac77
N
1099
1100 /* Each of these 'make_request' calls will call 'wait_barrier'.
1101 * If the first succeeds but the second blocks due to the resync
1102 * thread raising the barrier, we will deadlock because the
1103 * IO to the underlying device will be queued in generic_make_request
1104 * and will never complete, so will never reduce nr_pending.
1105 * So increment nr_waiting here so no new raise_barriers will
1106 * succeed, and so the second wait_barrier cannot block.
1107 */
1108 spin_lock_irq(&conf->resync_lock);
1109 conf->nr_waiting++;
1110 spin_unlock_irq(&conf->resync_lock);
1111
5a7bbad2
CH
1112 make_request(mddev, &bp->bio1);
1113 make_request(mddev, &bp->bio2);
1da177e4 1114
51e9ac77
N
1115 spin_lock_irq(&conf->resync_lock);
1116 conf->nr_waiting--;
1117 wake_up(&conf->wait_barrier);
1118 spin_unlock_irq(&conf->resync_lock);
1119
1da177e4 1120 bio_pair_release(bp);
5a7bbad2 1121 return;
1da177e4 1122 bad_map:
128595ed
N
1123 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1124 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
1da177e4
LT
1125 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1126
6712ecf8 1127 bio_io_error(bio);
5a7bbad2 1128 return;
1da177e4
LT
1129 }
1130
3d310eb7 1131 md_write_start(mddev, bio);
06d91a5f 1132
1da177e4
LT
1133 /*
1134 * Register the new request and wait if the reconstruction
1135 * thread has put up a bar for new requests.
1136 * Continue immediately if no resync is active currently.
1137 */
0a27ec96 1138 wait_barrier(conf);
1da177e4 1139
3ea7daa5
N
1140 sectors = bio->bi_size >> 9;
1141 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1142 bio->bi_sector < conf->reshape_progress &&
1143 bio->bi_sector + sectors > conf->reshape_progress) {
1144 /* IO spans the reshape position. Need to wait for
1145 * reshape to pass
1146 */
1147 allow_barrier(conf);
1148 wait_event(conf->wait_barrier,
1149 conf->reshape_progress <= bio->bi_sector ||
1150 conf->reshape_progress >= bio->bi_sector + sectors);
1151 wait_barrier(conf);
1152 }
1153 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1154 bio_data_dir(bio) == WRITE &&
1155 (mddev->reshape_backwards
1156 ? (bio->bi_sector < conf->reshape_safe &&
1157 bio->bi_sector + sectors > conf->reshape_progress)
1158 : (bio->bi_sector + sectors > conf->reshape_safe &&
1159 bio->bi_sector < conf->reshape_progress))) {
1160 /* Need to update reshape_position in metadata */
1161 mddev->reshape_position = conf->reshape_progress;
1162 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1163 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1164 md_wakeup_thread(mddev->thread);
1165 wait_event(mddev->sb_wait,
1166 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
1167
1168 conf->reshape_safe = mddev->reshape_position;
1169 }
1170
1da177e4
LT
1171 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1172
1173 r10_bio->master_bio = bio;
3ea7daa5 1174 r10_bio->sectors = sectors;
1da177e4
LT
1175
1176 r10_bio->mddev = mddev;
1177 r10_bio->sector = bio->bi_sector;
6cce3b23 1178 r10_bio->state = 0;
1da177e4 1179
856e08e2
N
1180 /* We might need to issue multiple reads to different
1181 * devices if there are bad blocks around, so we keep
1182 * track of the number of reads in bio->bi_phys_segments.
1183 * If this is 0, there is only one r10_bio and no locking
1184 * will be needed when the request completes. If it is
1185 * non-zero, then it is the number of not-completed requests.
1186 */
1187 bio->bi_phys_segments = 0;
1188 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1189
a362357b 1190 if (rw == READ) {
1da177e4
LT
1191 /*
1192 * read balancing logic:
1193 */
96c3fd1f 1194 struct md_rdev *rdev;
856e08e2
N
1195 int slot;
1196
1197read_again:
96c3fd1f
N
1198 rdev = read_balance(conf, r10_bio, &max_sectors);
1199 if (!rdev) {
1da177e4 1200 raid_end_bio_io(r10_bio);
5a7bbad2 1201 return;
1da177e4 1202 }
96c3fd1f 1203 slot = r10_bio->read_slot;
1da177e4 1204
a167f663 1205 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
856e08e2
N
1206 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1207 max_sectors);
1da177e4
LT
1208
1209 r10_bio->devs[slot].bio = read_bio;
abbf098e 1210 r10_bio->devs[slot].rdev = rdev;
1da177e4
LT
1211
1212 read_bio->bi_sector = r10_bio->devs[slot].addr +
f8c9e74f 1213 choose_data_offset(r10_bio, rdev);
96c3fd1f 1214 read_bio->bi_bdev = rdev->bdev;
1da177e4 1215 read_bio->bi_end_io = raid10_end_read_request;
7b6d91da 1216 read_bio->bi_rw = READ | do_sync;
1da177e4
LT
1217 read_bio->bi_private = r10_bio;
1218
856e08e2
N
1219 if (max_sectors < r10_bio->sectors) {
1220 /* Could not read all from this device, so we will
1221 * need another r10_bio.
1222 */
856e08e2
N
1223 sectors_handled = (r10_bio->sectors + max_sectors
1224 - bio->bi_sector);
1225 r10_bio->sectors = max_sectors;
1226 spin_lock_irq(&conf->device_lock);
1227 if (bio->bi_phys_segments == 0)
1228 bio->bi_phys_segments = 2;
1229 else
1230 bio->bi_phys_segments++;
1231 spin_unlock(&conf->device_lock);
1232 /* Cannot call generic_make_request directly
1233 * as that will be queued in __generic_make_request
1234 * and subsequent mempool_alloc might block
1235 * waiting for it. so hand bio over to raid10d.
1236 */
1237 reschedule_retry(r10_bio);
1238
1239 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1240
1241 r10_bio->master_bio = bio;
1242 r10_bio->sectors = ((bio->bi_size >> 9)
1243 - sectors_handled);
1244 r10_bio->state = 0;
1245 r10_bio->mddev = mddev;
1246 r10_bio->sector = bio->bi_sector + sectors_handled;
1247 goto read_again;
1248 } else
1249 generic_make_request(read_bio);
5a7bbad2 1250 return;
1da177e4
LT
1251 }
1252
1253 /*
1254 * WRITE:
1255 */
34db0cd6
N
1256 if (conf->pending_count >= max_queued_requests) {
1257 md_wakeup_thread(mddev->thread);
1258 wait_event(conf->wait_barrier,
1259 conf->pending_count < max_queued_requests);
1260 }
6bfe0b49 1261 /* first select target devices under rcu_lock and
1da177e4
LT
1262 * inc refcount on their rdev. Record them by setting
1263 * bios[x] to bio
d4432c23
N
1264 * If there are known/acknowledged bad blocks on any device
1265 * on which we have seen a write error, we want to avoid
1266 * writing to those blocks. This potentially requires several
1267 * writes to write around the bad blocks. Each set of writes
1268 * gets its own r10_bio with a set of bios attached. The number
1269 * of r10_bios is recored in bio->bi_phys_segments just as with
1270 * the read case.
1da177e4 1271 */
c3b328ac 1272
69335ef3 1273 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1da177e4 1274 raid10_find_phys(conf, r10_bio);
d4432c23 1275retry_write:
cb6969e8 1276 blocked_rdev = NULL;
1da177e4 1277 rcu_read_lock();
d4432c23
N
1278 max_sectors = r10_bio->sectors;
1279
1da177e4
LT
1280 for (i = 0; i < conf->copies; i++) {
1281 int d = r10_bio->devs[i].devnum;
3cb03002 1282 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
475b0321
N
1283 struct md_rdev *rrdev = rcu_dereference(
1284 conf->mirrors[d].replacement);
4ca40c2c
N
1285 if (rdev == rrdev)
1286 rrdev = NULL;
6bfe0b49
DW
1287 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1288 atomic_inc(&rdev->nr_pending);
1289 blocked_rdev = rdev;
1290 break;
1291 }
475b0321
N
1292 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1293 atomic_inc(&rrdev->nr_pending);
1294 blocked_rdev = rrdev;
1295 break;
1296 }
050b6615
N
1297 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1298 || test_bit(Unmerged, &rrdev->flags)))
475b0321
N
1299 rrdev = NULL;
1300
d4432c23 1301 r10_bio->devs[i].bio = NULL;
475b0321 1302 r10_bio->devs[i].repl_bio = NULL;
050b6615
N
1303 if (!rdev || test_bit(Faulty, &rdev->flags) ||
1304 test_bit(Unmerged, &rdev->flags)) {
6cce3b23 1305 set_bit(R10BIO_Degraded, &r10_bio->state);
d4432c23
N
1306 continue;
1307 }
1308 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1309 sector_t first_bad;
1310 sector_t dev_sector = r10_bio->devs[i].addr;
1311 int bad_sectors;
1312 int is_bad;
1313
1314 is_bad = is_badblock(rdev, dev_sector,
1315 max_sectors,
1316 &first_bad, &bad_sectors);
1317 if (is_bad < 0) {
1318 /* Mustn't write here until the bad block
1319 * is acknowledged
1320 */
1321 atomic_inc(&rdev->nr_pending);
1322 set_bit(BlockedBadBlocks, &rdev->flags);
1323 blocked_rdev = rdev;
1324 break;
1325 }
1326 if (is_bad && first_bad <= dev_sector) {
1327 /* Cannot write here at all */
1328 bad_sectors -= (dev_sector - first_bad);
1329 if (bad_sectors < max_sectors)
1330 /* Mustn't write more than bad_sectors
1331 * to other devices yet
1332 */
1333 max_sectors = bad_sectors;
1334 /* We don't set R10BIO_Degraded as that
1335 * only applies if the disk is missing,
1336 * so it might be re-added, and we want to
1337 * know to recover this chunk.
1338 * In this case the device is here, and the
1339 * fact that this chunk is not in-sync is
1340 * recorded in the bad block log.
1341 */
1342 continue;
1343 }
1344 if (is_bad) {
1345 int good_sectors = first_bad - dev_sector;
1346 if (good_sectors < max_sectors)
1347 max_sectors = good_sectors;
1348 }
6cce3b23 1349 }
d4432c23
N
1350 r10_bio->devs[i].bio = bio;
1351 atomic_inc(&rdev->nr_pending);
475b0321
N
1352 if (rrdev) {
1353 r10_bio->devs[i].repl_bio = bio;
1354 atomic_inc(&rrdev->nr_pending);
1355 }
1da177e4
LT
1356 }
1357 rcu_read_unlock();
1358
6bfe0b49
DW
1359 if (unlikely(blocked_rdev)) {
1360 /* Have to wait for this device to get unblocked, then retry */
1361 int j;
1362 int d;
1363
475b0321 1364 for (j = 0; j < i; j++) {
6bfe0b49
DW
1365 if (r10_bio->devs[j].bio) {
1366 d = r10_bio->devs[j].devnum;
1367 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1368 }
475b0321 1369 if (r10_bio->devs[j].repl_bio) {
4ca40c2c 1370 struct md_rdev *rdev;
475b0321 1371 d = r10_bio->devs[j].devnum;
4ca40c2c
N
1372 rdev = conf->mirrors[d].replacement;
1373 if (!rdev) {
1374 /* Race with remove_disk */
1375 smp_mb();
1376 rdev = conf->mirrors[d].rdev;
1377 }
1378 rdev_dec_pending(rdev, mddev);
475b0321
N
1379 }
1380 }
6bfe0b49
DW
1381 allow_barrier(conf);
1382 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1383 wait_barrier(conf);
1384 goto retry_write;
1385 }
1386
d4432c23
N
1387 if (max_sectors < r10_bio->sectors) {
1388 /* We are splitting this into multiple parts, so
1389 * we need to prepare for allocating another r10_bio.
1390 */
1391 r10_bio->sectors = max_sectors;
1392 spin_lock_irq(&conf->device_lock);
1393 if (bio->bi_phys_segments == 0)
1394 bio->bi_phys_segments = 2;
1395 else
1396 bio->bi_phys_segments++;
1397 spin_unlock_irq(&conf->device_lock);
1398 }
1399 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1400
4e78064f 1401 atomic_set(&r10_bio->remaining, 1);
d4432c23 1402 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
06d91a5f 1403
1da177e4
LT
1404 for (i = 0; i < conf->copies; i++) {
1405 struct bio *mbio;
1406 int d = r10_bio->devs[i].devnum;
1407 if (!r10_bio->devs[i].bio)
1408 continue;
1409
a167f663 1410 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
d4432c23
N
1411 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1412 max_sectors);
1da177e4
LT
1413 r10_bio->devs[i].bio = mbio;
1414
d4432c23 1415 mbio->bi_sector = (r10_bio->devs[i].addr+
f8c9e74f
N
1416 choose_data_offset(r10_bio,
1417 conf->mirrors[d].rdev));
1da177e4
LT
1418 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1419 mbio->bi_end_io = raid10_end_write_request;
532a2a3f 1420 mbio->bi_rw = WRITE | do_sync | do_fua | do_discard;
1da177e4
LT
1421 mbio->bi_private = r10_bio;
1422
1423 atomic_inc(&r10_bio->remaining);
4e78064f
N
1424 spin_lock_irqsave(&conf->device_lock, flags);
1425 bio_list_add(&conf->pending_bio_list, mbio);
34db0cd6 1426 conf->pending_count++;
4e78064f 1427 spin_unlock_irqrestore(&conf->device_lock, flags);
10684112 1428 if (!mddev_check_plugged(mddev))
b357f04a 1429 md_wakeup_thread(mddev->thread);
475b0321
N
1430
1431 if (!r10_bio->devs[i].repl_bio)
1432 continue;
1433
1434 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1435 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1436 max_sectors);
1437 r10_bio->devs[i].repl_bio = mbio;
1438
4ca40c2c
N
1439 /* We are actively writing to the original device
1440 * so it cannot disappear, so the replacement cannot
1441 * become NULL here
1442 */
f8c9e74f
N
1443 mbio->bi_sector = (r10_bio->devs[i].addr +
1444 choose_data_offset(
1445 r10_bio,
1446 conf->mirrors[d].replacement));
475b0321
N
1447 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1448 mbio->bi_end_io = raid10_end_write_request;
532a2a3f 1449 mbio->bi_rw = WRITE | do_sync | do_fua | do_discard;
475b0321
N
1450 mbio->bi_private = r10_bio;
1451
1452 atomic_inc(&r10_bio->remaining);
1453 spin_lock_irqsave(&conf->device_lock, flags);
1454 bio_list_add(&conf->pending_bio_list, mbio);
1455 conf->pending_count++;
1456 spin_unlock_irqrestore(&conf->device_lock, flags);
b357f04a
N
1457 if (!mddev_check_plugged(mddev))
1458 md_wakeup_thread(mddev->thread);
1da177e4
LT
1459 }
1460
079fa166
N
1461 /* Don't remove the bias on 'remaining' (one_write_done) until
1462 * after checking if we need to go around again.
1463 */
a35e63ef 1464
d4432c23 1465 if (sectors_handled < (bio->bi_size >> 9)) {
079fa166 1466 one_write_done(r10_bio);
5e570289 1467 /* We need another r10_bio. It has already been counted
d4432c23
N
1468 * in bio->bi_phys_segments.
1469 */
1470 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1471
1472 r10_bio->master_bio = bio;
1473 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1474
1475 r10_bio->mddev = mddev;
1476 r10_bio->sector = bio->bi_sector + sectors_handled;
1477 r10_bio->state = 0;
1478 goto retry_write;
1479 }
079fa166
N
1480 one_write_done(r10_bio);
1481
1482 /* In case raid10d snuck in to freeze_array */
1483 wake_up(&conf->wait_barrier);
1da177e4
LT
1484}
1485
fd01b88c 1486static void status(struct seq_file *seq, struct mddev *mddev)
1da177e4 1487{
e879a879 1488 struct r10conf *conf = mddev->private;
1da177e4
LT
1489 int i;
1490
5cf00fcd 1491 if (conf->geo.near_copies < conf->geo.raid_disks)
9d8f0363 1492 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
5cf00fcd
N
1493 if (conf->geo.near_copies > 1)
1494 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1495 if (conf->geo.far_copies > 1) {
1496 if (conf->geo.far_offset)
1497 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
c93983bf 1498 else
5cf00fcd 1499 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
c93983bf 1500 }
5cf00fcd
N
1501 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1502 conf->geo.raid_disks - mddev->degraded);
1503 for (i = 0; i < conf->geo.raid_disks; i++)
1da177e4
LT
1504 seq_printf(seq, "%s",
1505 conf->mirrors[i].rdev &&
b2d444d7 1506 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1da177e4
LT
1507 seq_printf(seq, "]");
1508}
1509
700c7213
N
1510/* check if there are enough drives for
1511 * every block to appear on atleast one.
1512 * Don't consider the device numbered 'ignore'
1513 * as we might be about to remove it.
1514 */
f8c9e74f 1515static int _enough(struct r10conf *conf, struct geom *geo, int ignore)
700c7213
N
1516{
1517 int first = 0;
1518
1519 do {
1520 int n = conf->copies;
1521 int cnt = 0;
1522 while (n--) {
1523 if (conf->mirrors[first].rdev &&
1524 first != ignore)
1525 cnt++;
f8c9e74f 1526 first = (first+1) % geo->raid_disks;
700c7213
N
1527 }
1528 if (cnt == 0)
1529 return 0;
1530 } while (first != 0);
1531 return 1;
1532}
1533
f8c9e74f
N
1534static int enough(struct r10conf *conf, int ignore)
1535{
1536 return _enough(conf, &conf->geo, ignore) &&
1537 _enough(conf, &conf->prev, ignore);
1538}
1539
fd01b88c 1540static void error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4
LT
1541{
1542 char b[BDEVNAME_SIZE];
e879a879 1543 struct r10conf *conf = mddev->private;
1da177e4
LT
1544
1545 /*
1546 * If it is not operational, then we have already marked it as dead
1547 * else if it is the last working disks, ignore the error, let the
1548 * next level up know.
1549 * else mark the drive as failed
1550 */
b2d444d7 1551 if (test_bit(In_sync, &rdev->flags)
700c7213 1552 && !enough(conf, rdev->raid_disk))
1da177e4
LT
1553 /*
1554 * Don't fail the drive, just return an IO error.
1da177e4
LT
1555 */
1556 return;
c04be0aa
N
1557 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1558 unsigned long flags;
1559 spin_lock_irqsave(&conf->device_lock, flags);
1da177e4 1560 mddev->degraded++;
c04be0aa 1561 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
1562 /*
1563 * if recovery is running, make sure it aborts.
1564 */
dfc70645 1565 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1da177e4 1566 }
de393cde 1567 set_bit(Blocked, &rdev->flags);
b2d444d7 1568 set_bit(Faulty, &rdev->flags);
850b2b42 1569 set_bit(MD_CHANGE_DEVS, &mddev->flags);
067032bc
JP
1570 printk(KERN_ALERT
1571 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1572 "md/raid10:%s: Operation continuing on %d devices.\n",
128595ed 1573 mdname(mddev), bdevname(rdev->bdev, b),
5cf00fcd 1574 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1da177e4
LT
1575}
1576
e879a879 1577static void print_conf(struct r10conf *conf)
1da177e4
LT
1578{
1579 int i;
dc280d98 1580 struct raid10_info *tmp;
1da177e4 1581
128595ed 1582 printk(KERN_DEBUG "RAID10 conf printout:\n");
1da177e4 1583 if (!conf) {
128595ed 1584 printk(KERN_DEBUG "(!conf)\n");
1da177e4
LT
1585 return;
1586 }
5cf00fcd
N
1587 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1588 conf->geo.raid_disks);
1da177e4 1589
5cf00fcd 1590 for (i = 0; i < conf->geo.raid_disks; i++) {
1da177e4
LT
1591 char b[BDEVNAME_SIZE];
1592 tmp = conf->mirrors + i;
1593 if (tmp->rdev)
128595ed 1594 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
b2d444d7
N
1595 i, !test_bit(In_sync, &tmp->rdev->flags),
1596 !test_bit(Faulty, &tmp->rdev->flags),
1da177e4
LT
1597 bdevname(tmp->rdev->bdev,b));
1598 }
1599}
1600
e879a879 1601static void close_sync(struct r10conf *conf)
1da177e4 1602{
0a27ec96
N
1603 wait_barrier(conf);
1604 allow_barrier(conf);
1da177e4
LT
1605
1606 mempool_destroy(conf->r10buf_pool);
1607 conf->r10buf_pool = NULL;
1608}
1609
fd01b88c 1610static int raid10_spare_active(struct mddev *mddev)
1da177e4
LT
1611{
1612 int i;
e879a879 1613 struct r10conf *conf = mddev->private;
dc280d98 1614 struct raid10_info *tmp;
6b965620
N
1615 int count = 0;
1616 unsigned long flags;
1da177e4
LT
1617
1618 /*
1619 * Find all non-in_sync disks within the RAID10 configuration
1620 * and mark them in_sync
1621 */
5cf00fcd 1622 for (i = 0; i < conf->geo.raid_disks; i++) {
1da177e4 1623 tmp = conf->mirrors + i;
4ca40c2c
N
1624 if (tmp->replacement
1625 && tmp->replacement->recovery_offset == MaxSector
1626 && !test_bit(Faulty, &tmp->replacement->flags)
1627 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1628 /* Replacement has just become active */
1629 if (!tmp->rdev
1630 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1631 count++;
1632 if (tmp->rdev) {
1633 /* Replaced device not technically faulty,
1634 * but we need to be sure it gets removed
1635 * and never re-added.
1636 */
1637 set_bit(Faulty, &tmp->rdev->flags);
1638 sysfs_notify_dirent_safe(
1639 tmp->rdev->sysfs_state);
1640 }
1641 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1642 } else if (tmp->rdev
1643 && !test_bit(Faulty, &tmp->rdev->flags)
1644 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 1645 count++;
e6ffbcb6 1646 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1da177e4
LT
1647 }
1648 }
6b965620
N
1649 spin_lock_irqsave(&conf->device_lock, flags);
1650 mddev->degraded -= count;
1651 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
1652
1653 print_conf(conf);
6b965620 1654 return count;
1da177e4
LT
1655}
1656
1657
fd01b88c 1658static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 1659{
e879a879 1660 struct r10conf *conf = mddev->private;
199050ea 1661 int err = -EEXIST;
1da177e4 1662 int mirror;
6c2fce2e 1663 int first = 0;
5cf00fcd 1664 int last = conf->geo.raid_disks - 1;
050b6615 1665 struct request_queue *q = bdev_get_queue(rdev->bdev);
1da177e4
LT
1666
1667 if (mddev->recovery_cp < MaxSector)
1668 /* only hot-add to in-sync arrays, as recovery is
1669 * very different from resync
1670 */
199050ea 1671 return -EBUSY;
f8c9e74f 1672 if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1))
199050ea 1673 return -EINVAL;
1da177e4 1674
a53a6c85 1675 if (rdev->raid_disk >= 0)
6c2fce2e 1676 first = last = rdev->raid_disk;
1da177e4 1677
050b6615
N
1678 if (q->merge_bvec_fn) {
1679 set_bit(Unmerged, &rdev->flags);
1680 mddev->merge_check_needed = 1;
1681 }
1682
2c4193df 1683 if (rdev->saved_raid_disk >= first &&
6cce3b23
N
1684 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1685 mirror = rdev->saved_raid_disk;
1686 else
6c2fce2e 1687 mirror = first;
2bb77736 1688 for ( ; mirror <= last ; mirror++) {
dc280d98 1689 struct raid10_info *p = &conf->mirrors[mirror];
2bb77736
N
1690 if (p->recovery_disabled == mddev->recovery_disabled)
1691 continue;
b7044d41
N
1692 if (p->rdev) {
1693 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1694 p->replacement != NULL)
1695 continue;
1696 clear_bit(In_sync, &rdev->flags);
1697 set_bit(Replacement, &rdev->flags);
1698 rdev->raid_disk = mirror;
1699 err = 0;
1700 disk_stack_limits(mddev->gendisk, rdev->bdev,
1701 rdev->data_offset << 9);
b7044d41
N
1702 conf->fullsync = 1;
1703 rcu_assign_pointer(p->replacement, rdev);
1704 break;
1705 }
1da177e4 1706
2bb77736
N
1707 disk_stack_limits(mddev->gendisk, rdev->bdev,
1708 rdev->data_offset << 9);
1da177e4 1709
2bb77736 1710 p->head_position = 0;
d890fa2b 1711 p->recovery_disabled = mddev->recovery_disabled - 1;
2bb77736
N
1712 rdev->raid_disk = mirror;
1713 err = 0;
1714 if (rdev->saved_raid_disk != mirror)
1715 conf->fullsync = 1;
1716 rcu_assign_pointer(p->rdev, rdev);
1717 break;
1718 }
050b6615
N
1719 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1720 /* Some requests might not have seen this new
1721 * merge_bvec_fn. We must wait for them to complete
1722 * before merging the device fully.
1723 * First we make sure any code which has tested
1724 * our function has submitted the request, then
1725 * we wait for all outstanding requests to complete.
1726 */
1727 synchronize_sched();
1728 raise_barrier(conf, 0);
1729 lower_barrier(conf);
1730 clear_bit(Unmerged, &rdev->flags);
1731 }
ac5e7113 1732 md_integrity_add_rdev(rdev, mddev);
532a2a3f
SL
1733 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
1734 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1735
1da177e4 1736 print_conf(conf);
199050ea 1737 return err;
1da177e4
LT
1738}
1739
b8321b68 1740static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 1741{
e879a879 1742 struct r10conf *conf = mddev->private;
1da177e4 1743 int err = 0;
b8321b68 1744 int number = rdev->raid_disk;
c8ab903e 1745 struct md_rdev **rdevp;
dc280d98 1746 struct raid10_info *p = conf->mirrors + number;
1da177e4
LT
1747
1748 print_conf(conf);
c8ab903e
N
1749 if (rdev == p->rdev)
1750 rdevp = &p->rdev;
1751 else if (rdev == p->replacement)
1752 rdevp = &p->replacement;
1753 else
1754 return 0;
1755
1756 if (test_bit(In_sync, &rdev->flags) ||
1757 atomic_read(&rdev->nr_pending)) {
1758 err = -EBUSY;
1759 goto abort;
1760 }
1761 /* Only remove faulty devices if recovery
1762 * is not possible.
1763 */
1764 if (!test_bit(Faulty, &rdev->flags) &&
1765 mddev->recovery_disabled != p->recovery_disabled &&
4ca40c2c 1766 (!p->replacement || p->replacement == rdev) &&
63aced61 1767 number < conf->geo.raid_disks &&
c8ab903e
N
1768 enough(conf, -1)) {
1769 err = -EBUSY;
1770 goto abort;
1da177e4 1771 }
c8ab903e
N
1772 *rdevp = NULL;
1773 synchronize_rcu();
1774 if (atomic_read(&rdev->nr_pending)) {
1775 /* lost the race, try later */
1776 err = -EBUSY;
1777 *rdevp = rdev;
1778 goto abort;
4ca40c2c
N
1779 } else if (p->replacement) {
1780 /* We must have just cleared 'rdev' */
1781 p->rdev = p->replacement;
1782 clear_bit(Replacement, &p->replacement->flags);
1783 smp_mb(); /* Make sure other CPUs may see both as identical
1784 * but will never see neither -- if they are careful.
1785 */
1786 p->replacement = NULL;
1787 clear_bit(WantReplacement, &rdev->flags);
1788 } else
1789 /* We might have just remove the Replacement as faulty
1790 * Clear the flag just in case
1791 */
1792 clear_bit(WantReplacement, &rdev->flags);
1793
c8ab903e
N
1794 err = md_integrity_register(mddev);
1795
1da177e4
LT
1796abort:
1797
1798 print_conf(conf);
1799 return err;
1800}
1801
1802
6712ecf8 1803static void end_sync_read(struct bio *bio, int error)
1da177e4 1804{
9f2c9d12 1805 struct r10bio *r10_bio = bio->bi_private;
e879a879 1806 struct r10conf *conf = r10_bio->mddev->private;
778ca018 1807 int d;
1da177e4 1808
3ea7daa5
N
1809 if (bio == r10_bio->master_bio) {
1810 /* this is a reshape read */
1811 d = r10_bio->read_slot; /* really the read dev */
1812 } else
1813 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
0eb3ff12
N
1814
1815 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1816 set_bit(R10BIO_Uptodate, &r10_bio->state);
e684e41d
N
1817 else
1818 /* The write handler will notice the lack of
1819 * R10BIO_Uptodate and record any errors etc
1820 */
4dbcdc75
N
1821 atomic_add(r10_bio->sectors,
1822 &conf->mirrors[d].rdev->corrected_errors);
1da177e4
LT
1823
1824 /* for reconstruct, we always reschedule after a read.
1825 * for resync, only after all reads
1826 */
73d5c38a 1827 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1da177e4
LT
1828 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1829 atomic_dec_and_test(&r10_bio->remaining)) {
1830 /* we have read all the blocks,
1831 * do the comparison in process context in raid10d
1832 */
1833 reschedule_retry(r10_bio);
1834 }
1da177e4
LT
1835}
1836
9f2c9d12 1837static void end_sync_request(struct r10bio *r10_bio)
1da177e4 1838{
fd01b88c 1839 struct mddev *mddev = r10_bio->mddev;
dfc70645 1840
1da177e4
LT
1841 while (atomic_dec_and_test(&r10_bio->remaining)) {
1842 if (r10_bio->master_bio == NULL) {
1843 /* the primary of several recovery bios */
73d5c38a 1844 sector_t s = r10_bio->sectors;
1a0b7cd8
N
1845 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1846 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
1847 reschedule_retry(r10_bio);
1848 else
1849 put_buf(r10_bio);
73d5c38a 1850 md_done_sync(mddev, s, 1);
1da177e4
LT
1851 break;
1852 } else {
9f2c9d12 1853 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1a0b7cd8
N
1854 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1855 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
1856 reschedule_retry(r10_bio);
1857 else
1858 put_buf(r10_bio);
1da177e4
LT
1859 r10_bio = r10_bio2;
1860 }
1861 }
1da177e4
LT
1862}
1863
5e570289
N
1864static void end_sync_write(struct bio *bio, int error)
1865{
1866 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
9f2c9d12 1867 struct r10bio *r10_bio = bio->bi_private;
fd01b88c 1868 struct mddev *mddev = r10_bio->mddev;
e879a879 1869 struct r10conf *conf = mddev->private;
5e570289
N
1870 int d;
1871 sector_t first_bad;
1872 int bad_sectors;
1873 int slot;
9ad1aefc 1874 int repl;
4ca40c2c 1875 struct md_rdev *rdev = NULL;
5e570289 1876
9ad1aefc
N
1877 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1878 if (repl)
1879 rdev = conf->mirrors[d].replacement;
547414d1 1880 else
9ad1aefc 1881 rdev = conf->mirrors[d].rdev;
5e570289
N
1882
1883 if (!uptodate) {
9ad1aefc
N
1884 if (repl)
1885 md_error(mddev, rdev);
1886 else {
1887 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
1888 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1889 set_bit(MD_RECOVERY_NEEDED,
1890 &rdev->mddev->recovery);
9ad1aefc
N
1891 set_bit(R10BIO_WriteError, &r10_bio->state);
1892 }
1893 } else if (is_badblock(rdev,
5e570289
N
1894 r10_bio->devs[slot].addr,
1895 r10_bio->sectors,
1896 &first_bad, &bad_sectors))
1897 set_bit(R10BIO_MadeGood, &r10_bio->state);
1898
9ad1aefc 1899 rdev_dec_pending(rdev, mddev);
5e570289
N
1900
1901 end_sync_request(r10_bio);
1902}
1903
1da177e4
LT
1904/*
1905 * Note: sync and recover and handled very differently for raid10
1906 * This code is for resync.
1907 * For resync, we read through virtual addresses and read all blocks.
1908 * If there is any error, we schedule a write. The lowest numbered
1909 * drive is authoritative.
1910 * However requests come for physical address, so we need to map.
1911 * For every physical address there are raid_disks/copies virtual addresses,
1912 * which is always are least one, but is not necessarly an integer.
1913 * This means that a physical address can span multiple chunks, so we may
1914 * have to submit multiple io requests for a single sync request.
1915 */
1916/*
1917 * We check if all blocks are in-sync and only write to blocks that
1918 * aren't in sync
1919 */
9f2c9d12 1920static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1da177e4 1921{
e879a879 1922 struct r10conf *conf = mddev->private;
1da177e4
LT
1923 int i, first;
1924 struct bio *tbio, *fbio;
f4380a91 1925 int vcnt;
1da177e4
LT
1926
1927 atomic_set(&r10_bio->remaining, 1);
1928
1929 /* find the first device with a block */
1930 for (i=0; i<conf->copies; i++)
1931 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1932 break;
1933
1934 if (i == conf->copies)
1935 goto done;
1936
1937 first = i;
1938 fbio = r10_bio->devs[i].bio;
1939
f4380a91 1940 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
1da177e4 1941 /* now find blocks with errors */
0eb3ff12
N
1942 for (i=0 ; i < conf->copies ; i++) {
1943 int j, d;
1da177e4 1944
1da177e4 1945 tbio = r10_bio->devs[i].bio;
0eb3ff12
N
1946
1947 if (tbio->bi_end_io != end_sync_read)
1948 continue;
1949 if (i == first)
1da177e4 1950 continue;
0eb3ff12
N
1951 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1952 /* We know that the bi_io_vec layout is the same for
1953 * both 'first' and 'i', so we just compare them.
1954 * All vec entries are PAGE_SIZE;
1955 */
1956 for (j = 0; j < vcnt; j++)
1957 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1958 page_address(tbio->bi_io_vec[j].bv_page),
5020ad7d 1959 fbio->bi_io_vec[j].bv_len))
0eb3ff12
N
1960 break;
1961 if (j == vcnt)
1962 continue;
1963 mddev->resync_mismatches += r10_bio->sectors;
f84ee364
N
1964 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1965 /* Don't fix anything. */
1966 continue;
0eb3ff12 1967 }
f84ee364
N
1968 /* Ok, we need to write this bio, either to correct an
1969 * inconsistency or to correct an unreadable block.
1da177e4
LT
1970 * First we need to fixup bv_offset, bv_len and
1971 * bi_vecs, as the read request might have corrupted these
1972 */
1973 tbio->bi_vcnt = vcnt;
1974 tbio->bi_size = r10_bio->sectors << 9;
1975 tbio->bi_idx = 0;
1976 tbio->bi_phys_segments = 0;
1da177e4
LT
1977 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1978 tbio->bi_flags |= 1 << BIO_UPTODATE;
1979 tbio->bi_next = NULL;
1980 tbio->bi_rw = WRITE;
1981 tbio->bi_private = r10_bio;
1982 tbio->bi_sector = r10_bio->devs[i].addr;
1983
1984 for (j=0; j < vcnt ; j++) {
1985 tbio->bi_io_vec[j].bv_offset = 0;
1986 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1987
1988 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1989 page_address(fbio->bi_io_vec[j].bv_page),
1990 PAGE_SIZE);
1991 }
1992 tbio->bi_end_io = end_sync_write;
1993
1994 d = r10_bio->devs[i].devnum;
1995 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1996 atomic_inc(&r10_bio->remaining);
1997 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1998
1999 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
2000 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
2001 generic_make_request(tbio);
2002 }
2003
9ad1aefc
N
2004 /* Now write out to any replacement devices
2005 * that are active
2006 */
2007 for (i = 0; i < conf->copies; i++) {
2008 int j, d;
9ad1aefc
N
2009
2010 tbio = r10_bio->devs[i].repl_bio;
2011 if (!tbio || !tbio->bi_end_io)
2012 continue;
2013 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2014 && r10_bio->devs[i].bio != fbio)
2015 for (j = 0; j < vcnt; j++)
2016 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
2017 page_address(fbio->bi_io_vec[j].bv_page),
2018 PAGE_SIZE);
2019 d = r10_bio->devs[i].devnum;
2020 atomic_inc(&r10_bio->remaining);
2021 md_sync_acct(conf->mirrors[d].replacement->bdev,
2022 tbio->bi_size >> 9);
2023 generic_make_request(tbio);
2024 }
2025
1da177e4
LT
2026done:
2027 if (atomic_dec_and_test(&r10_bio->remaining)) {
2028 md_done_sync(mddev, r10_bio->sectors, 1);
2029 put_buf(r10_bio);
2030 }
2031}
2032
2033/*
2034 * Now for the recovery code.
2035 * Recovery happens across physical sectors.
2036 * We recover all non-is_sync drives by finding the virtual address of
2037 * each, and then choose a working drive that also has that virt address.
2038 * There is a separate r10_bio for each non-in_sync drive.
2039 * Only the first two slots are in use. The first for reading,
2040 * The second for writing.
2041 *
2042 */
9f2c9d12 2043static void fix_recovery_read_error(struct r10bio *r10_bio)
5e570289
N
2044{
2045 /* We got a read error during recovery.
2046 * We repeat the read in smaller page-sized sections.
2047 * If a read succeeds, write it to the new device or record
2048 * a bad block if we cannot.
2049 * If a read fails, record a bad block on both old and
2050 * new devices.
2051 */
fd01b88c 2052 struct mddev *mddev = r10_bio->mddev;
e879a879 2053 struct r10conf *conf = mddev->private;
5e570289
N
2054 struct bio *bio = r10_bio->devs[0].bio;
2055 sector_t sect = 0;
2056 int sectors = r10_bio->sectors;
2057 int idx = 0;
2058 int dr = r10_bio->devs[0].devnum;
2059 int dw = r10_bio->devs[1].devnum;
2060
2061 while (sectors) {
2062 int s = sectors;
3cb03002 2063 struct md_rdev *rdev;
5e570289
N
2064 sector_t addr;
2065 int ok;
2066
2067 if (s > (PAGE_SIZE>>9))
2068 s = PAGE_SIZE >> 9;
2069
2070 rdev = conf->mirrors[dr].rdev;
2071 addr = r10_bio->devs[0].addr + sect,
2072 ok = sync_page_io(rdev,
2073 addr,
2074 s << 9,
2075 bio->bi_io_vec[idx].bv_page,
2076 READ, false);
2077 if (ok) {
2078 rdev = conf->mirrors[dw].rdev;
2079 addr = r10_bio->devs[1].addr + sect;
2080 ok = sync_page_io(rdev,
2081 addr,
2082 s << 9,
2083 bio->bi_io_vec[idx].bv_page,
2084 WRITE, false);
b7044d41 2085 if (!ok) {
5e570289 2086 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
2087 if (!test_and_set_bit(WantReplacement,
2088 &rdev->flags))
2089 set_bit(MD_RECOVERY_NEEDED,
2090 &rdev->mddev->recovery);
2091 }
5e570289
N
2092 }
2093 if (!ok) {
2094 /* We don't worry if we cannot set a bad block -
2095 * it really is bad so there is no loss in not
2096 * recording it yet
2097 */
2098 rdev_set_badblocks(rdev, addr, s, 0);
2099
2100 if (rdev != conf->mirrors[dw].rdev) {
2101 /* need bad block on destination too */
3cb03002 2102 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
5e570289
N
2103 addr = r10_bio->devs[1].addr + sect;
2104 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2105 if (!ok) {
2106 /* just abort the recovery */
2107 printk(KERN_NOTICE
2108 "md/raid10:%s: recovery aborted"
2109 " due to read error\n",
2110 mdname(mddev));
2111
2112 conf->mirrors[dw].recovery_disabled
2113 = mddev->recovery_disabled;
2114 set_bit(MD_RECOVERY_INTR,
2115 &mddev->recovery);
2116 break;
2117 }
2118 }
2119 }
2120
2121 sectors -= s;
2122 sect += s;
2123 idx++;
2124 }
2125}
1da177e4 2126
9f2c9d12 2127static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1da177e4 2128{
e879a879 2129 struct r10conf *conf = mddev->private;
c65060ad 2130 int d;
24afd80d 2131 struct bio *wbio, *wbio2;
1da177e4 2132
5e570289
N
2133 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2134 fix_recovery_read_error(r10_bio);
2135 end_sync_request(r10_bio);
2136 return;
2137 }
2138
c65060ad
NK
2139 /*
2140 * share the pages with the first bio
1da177e4
LT
2141 * and submit the write request
2142 */
1da177e4 2143 d = r10_bio->devs[1].devnum;
24afd80d
N
2144 wbio = r10_bio->devs[1].bio;
2145 wbio2 = r10_bio->devs[1].repl_bio;
2146 if (wbio->bi_end_io) {
2147 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2148 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2149 generic_make_request(wbio);
2150 }
2151 if (wbio2 && wbio2->bi_end_io) {
2152 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2153 md_sync_acct(conf->mirrors[d].replacement->bdev,
2154 wbio2->bi_size >> 9);
2155 generic_make_request(wbio2);
2156 }
1da177e4
LT
2157}
2158
2159
1e50915f
RB
2160/*
2161 * Used by fix_read_error() to decay the per rdev read_errors.
2162 * We halve the read error count for every hour that has elapsed
2163 * since the last recorded read error.
2164 *
2165 */
fd01b88c 2166static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
1e50915f
RB
2167{
2168 struct timespec cur_time_mon;
2169 unsigned long hours_since_last;
2170 unsigned int read_errors = atomic_read(&rdev->read_errors);
2171
2172 ktime_get_ts(&cur_time_mon);
2173
2174 if (rdev->last_read_error.tv_sec == 0 &&
2175 rdev->last_read_error.tv_nsec == 0) {
2176 /* first time we've seen a read error */
2177 rdev->last_read_error = cur_time_mon;
2178 return;
2179 }
2180
2181 hours_since_last = (cur_time_mon.tv_sec -
2182 rdev->last_read_error.tv_sec) / 3600;
2183
2184 rdev->last_read_error = cur_time_mon;
2185
2186 /*
2187 * if hours_since_last is > the number of bits in read_errors
2188 * just set read errors to 0. We do this to avoid
2189 * overflowing the shift of read_errors by hours_since_last.
2190 */
2191 if (hours_since_last >= 8 * sizeof(read_errors))
2192 atomic_set(&rdev->read_errors, 0);
2193 else
2194 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2195}
2196
3cb03002 2197static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
58c54fcc
N
2198 int sectors, struct page *page, int rw)
2199{
2200 sector_t first_bad;
2201 int bad_sectors;
2202
2203 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2204 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2205 return -1;
2206 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2207 /* success */
2208 return 1;
b7044d41 2209 if (rw == WRITE) {
58c54fcc 2210 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
2211 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2212 set_bit(MD_RECOVERY_NEEDED,
2213 &rdev->mddev->recovery);
2214 }
58c54fcc
N
2215 /* need to record an error - either for the block or the device */
2216 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2217 md_error(rdev->mddev, rdev);
2218 return 0;
2219}
2220
1da177e4
LT
2221/*
2222 * This is a kernel thread which:
2223 *
2224 * 1. Retries failed read operations on working mirrors.
2225 * 2. Updates the raid superblock when problems encounter.
6814d536 2226 * 3. Performs writes following reads for array synchronising.
1da177e4
LT
2227 */
2228
e879a879 2229static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
6814d536
N
2230{
2231 int sect = 0; /* Offset from r10_bio->sector */
2232 int sectors = r10_bio->sectors;
3cb03002 2233 struct md_rdev*rdev;
1e50915f 2234 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
0544a21d 2235 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1e50915f 2236
7c4e06ff
N
2237 /* still own a reference to this rdev, so it cannot
2238 * have been cleared recently.
2239 */
2240 rdev = conf->mirrors[d].rdev;
1e50915f 2241
7c4e06ff
N
2242 if (test_bit(Faulty, &rdev->flags))
2243 /* drive has already been failed, just ignore any
2244 more fix_read_error() attempts */
2245 return;
1e50915f 2246
7c4e06ff
N
2247 check_decay_read_errors(mddev, rdev);
2248 atomic_inc(&rdev->read_errors);
2249 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2250 char b[BDEVNAME_SIZE];
2251 bdevname(rdev->bdev, b);
1e50915f 2252
7c4e06ff
N
2253 printk(KERN_NOTICE
2254 "md/raid10:%s: %s: Raid device exceeded "
2255 "read_error threshold [cur %d:max %d]\n",
2256 mdname(mddev), b,
2257 atomic_read(&rdev->read_errors), max_read_errors);
2258 printk(KERN_NOTICE
2259 "md/raid10:%s: %s: Failing raid device\n",
2260 mdname(mddev), b);
2261 md_error(mddev, conf->mirrors[d].rdev);
fae8cc5e 2262 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
7c4e06ff 2263 return;
1e50915f 2264 }
1e50915f 2265
6814d536
N
2266 while(sectors) {
2267 int s = sectors;
2268 int sl = r10_bio->read_slot;
2269 int success = 0;
2270 int start;
2271
2272 if (s > (PAGE_SIZE>>9))
2273 s = PAGE_SIZE >> 9;
2274
2275 rcu_read_lock();
2276 do {
8dbed5ce
N
2277 sector_t first_bad;
2278 int bad_sectors;
2279
0544a21d 2280 d = r10_bio->devs[sl].devnum;
6814d536
N
2281 rdev = rcu_dereference(conf->mirrors[d].rdev);
2282 if (rdev &&
050b6615 2283 !test_bit(Unmerged, &rdev->flags) &&
8dbed5ce
N
2284 test_bit(In_sync, &rdev->flags) &&
2285 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2286 &first_bad, &bad_sectors) == 0) {
6814d536
N
2287 atomic_inc(&rdev->nr_pending);
2288 rcu_read_unlock();
2b193363 2289 success = sync_page_io(rdev,
6814d536 2290 r10_bio->devs[sl].addr +
ccebd4c4 2291 sect,
6814d536 2292 s<<9,
ccebd4c4 2293 conf->tmppage, READ, false);
6814d536
N
2294 rdev_dec_pending(rdev, mddev);
2295 rcu_read_lock();
2296 if (success)
2297 break;
2298 }
2299 sl++;
2300 if (sl == conf->copies)
2301 sl = 0;
2302 } while (!success && sl != r10_bio->read_slot);
2303 rcu_read_unlock();
2304
2305 if (!success) {
58c54fcc
N
2306 /* Cannot read from anywhere, just mark the block
2307 * as bad on the first device to discourage future
2308 * reads.
2309 */
6814d536 2310 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
58c54fcc
N
2311 rdev = conf->mirrors[dn].rdev;
2312
2313 if (!rdev_set_badblocks(
2314 rdev,
2315 r10_bio->devs[r10_bio->read_slot].addr
2316 + sect,
fae8cc5e 2317 s, 0)) {
58c54fcc 2318 md_error(mddev, rdev);
fae8cc5e
N
2319 r10_bio->devs[r10_bio->read_slot].bio
2320 = IO_BLOCKED;
2321 }
6814d536
N
2322 break;
2323 }
2324
2325 start = sl;
2326 /* write it back and re-read */
2327 rcu_read_lock();
2328 while (sl != r10_bio->read_slot) {
67b8dc4b 2329 char b[BDEVNAME_SIZE];
0544a21d 2330
6814d536
N
2331 if (sl==0)
2332 sl = conf->copies;
2333 sl--;
2334 d = r10_bio->devs[sl].devnum;
2335 rdev = rcu_dereference(conf->mirrors[d].rdev);
1294b9c9 2336 if (!rdev ||
050b6615 2337 test_bit(Unmerged, &rdev->flags) ||
1294b9c9
N
2338 !test_bit(In_sync, &rdev->flags))
2339 continue;
2340
2341 atomic_inc(&rdev->nr_pending);
2342 rcu_read_unlock();
58c54fcc
N
2343 if (r10_sync_page_io(rdev,
2344 r10_bio->devs[sl].addr +
2345 sect,
055d3747 2346 s, conf->tmppage, WRITE)
1294b9c9
N
2347 == 0) {
2348 /* Well, this device is dead */
2349 printk(KERN_NOTICE
2350 "md/raid10:%s: read correction "
2351 "write failed"
2352 " (%d sectors at %llu on %s)\n",
2353 mdname(mddev), s,
2354 (unsigned long long)(
f8c9e74f
N
2355 sect +
2356 choose_data_offset(r10_bio,
2357 rdev)),
1294b9c9
N
2358 bdevname(rdev->bdev, b));
2359 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2360 "drive\n",
2361 mdname(mddev),
2362 bdevname(rdev->bdev, b));
6814d536 2363 }
1294b9c9
N
2364 rdev_dec_pending(rdev, mddev);
2365 rcu_read_lock();
6814d536
N
2366 }
2367 sl = start;
2368 while (sl != r10_bio->read_slot) {
1294b9c9 2369 char b[BDEVNAME_SIZE];
0544a21d 2370
6814d536
N
2371 if (sl==0)
2372 sl = conf->copies;
2373 sl--;
2374 d = r10_bio->devs[sl].devnum;
2375 rdev = rcu_dereference(conf->mirrors[d].rdev);
1294b9c9
N
2376 if (!rdev ||
2377 !test_bit(In_sync, &rdev->flags))
2378 continue;
6814d536 2379
1294b9c9
N
2380 atomic_inc(&rdev->nr_pending);
2381 rcu_read_unlock();
58c54fcc
N
2382 switch (r10_sync_page_io(rdev,
2383 r10_bio->devs[sl].addr +
2384 sect,
055d3747 2385 s, conf->tmppage,
58c54fcc
N
2386 READ)) {
2387 case 0:
1294b9c9
N
2388 /* Well, this device is dead */
2389 printk(KERN_NOTICE
2390 "md/raid10:%s: unable to read back "
2391 "corrected sectors"
2392 " (%d sectors at %llu on %s)\n",
2393 mdname(mddev), s,
2394 (unsigned long long)(
f8c9e74f
N
2395 sect +
2396 choose_data_offset(r10_bio, rdev)),
1294b9c9
N
2397 bdevname(rdev->bdev, b));
2398 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2399 "drive\n",
2400 mdname(mddev),
2401 bdevname(rdev->bdev, b));
58c54fcc
N
2402 break;
2403 case 1:
1294b9c9
N
2404 printk(KERN_INFO
2405 "md/raid10:%s: read error corrected"
2406 " (%d sectors at %llu on %s)\n",
2407 mdname(mddev), s,
2408 (unsigned long long)(
f8c9e74f
N
2409 sect +
2410 choose_data_offset(r10_bio, rdev)),
1294b9c9
N
2411 bdevname(rdev->bdev, b));
2412 atomic_add(s, &rdev->corrected_errors);
6814d536 2413 }
1294b9c9
N
2414
2415 rdev_dec_pending(rdev, mddev);
2416 rcu_read_lock();
6814d536
N
2417 }
2418 rcu_read_unlock();
2419
2420 sectors -= s;
2421 sect += s;
2422 }
2423}
2424
bd870a16
N
2425static void bi_complete(struct bio *bio, int error)
2426{
2427 complete((struct completion *)bio->bi_private);
2428}
2429
2430static int submit_bio_wait(int rw, struct bio *bio)
2431{
2432 struct completion event;
2433 rw |= REQ_SYNC;
2434
2435 init_completion(&event);
2436 bio->bi_private = &event;
2437 bio->bi_end_io = bi_complete;
2438 submit_bio(rw, bio);
2439 wait_for_completion(&event);
2440
2441 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2442}
2443
9f2c9d12 2444static int narrow_write_error(struct r10bio *r10_bio, int i)
bd870a16
N
2445{
2446 struct bio *bio = r10_bio->master_bio;
fd01b88c 2447 struct mddev *mddev = r10_bio->mddev;
e879a879 2448 struct r10conf *conf = mddev->private;
3cb03002 2449 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
bd870a16
N
2450 /* bio has the data to be written to slot 'i' where
2451 * we just recently had a write error.
2452 * We repeatedly clone the bio and trim down to one block,
2453 * then try the write. Where the write fails we record
2454 * a bad block.
2455 * It is conceivable that the bio doesn't exactly align with
2456 * blocks. We must handle this.
2457 *
2458 * We currently own a reference to the rdev.
2459 */
2460
2461 int block_sectors;
2462 sector_t sector;
2463 int sectors;
2464 int sect_to_write = r10_bio->sectors;
2465 int ok = 1;
2466
2467 if (rdev->badblocks.shift < 0)
2468 return 0;
2469
2470 block_sectors = 1 << rdev->badblocks.shift;
2471 sector = r10_bio->sector;
2472 sectors = ((r10_bio->sector + block_sectors)
2473 & ~(sector_t)(block_sectors - 1))
2474 - sector;
2475
2476 while (sect_to_write) {
2477 struct bio *wbio;
2478 if (sectors > sect_to_write)
2479 sectors = sect_to_write;
2480 /* Write at 'sector' for 'sectors' */
2481 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2482 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2483 wbio->bi_sector = (r10_bio->devs[i].addr+
f8c9e74f 2484 choose_data_offset(r10_bio, rdev) +
bd870a16
N
2485 (sector - r10_bio->sector));
2486 wbio->bi_bdev = rdev->bdev;
2487 if (submit_bio_wait(WRITE, wbio) == 0)
2488 /* Failure! */
2489 ok = rdev_set_badblocks(rdev, sector,
2490 sectors, 0)
2491 && ok;
2492
2493 bio_put(wbio);
2494 sect_to_write -= sectors;
2495 sector += sectors;
2496 sectors = block_sectors;
2497 }
2498 return ok;
2499}
2500
9f2c9d12 2501static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
560f8e55
N
2502{
2503 int slot = r10_bio->read_slot;
560f8e55 2504 struct bio *bio;
e879a879 2505 struct r10conf *conf = mddev->private;
abbf098e 2506 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
560f8e55
N
2507 char b[BDEVNAME_SIZE];
2508 unsigned long do_sync;
856e08e2 2509 int max_sectors;
560f8e55
N
2510
2511 /* we got a read error. Maybe the drive is bad. Maybe just
2512 * the block and we can fix it.
2513 * We freeze all other IO, and try reading the block from
2514 * other devices. When we find one, we re-write
2515 * and check it that fixes the read error.
2516 * This is all done synchronously while the array is
2517 * frozen.
2518 */
fae8cc5e
N
2519 bio = r10_bio->devs[slot].bio;
2520 bdevname(bio->bi_bdev, b);
2521 bio_put(bio);
2522 r10_bio->devs[slot].bio = NULL;
2523
560f8e55
N
2524 if (mddev->ro == 0) {
2525 freeze_array(conf);
2526 fix_read_error(conf, mddev, r10_bio);
2527 unfreeze_array(conf);
fae8cc5e
N
2528 } else
2529 r10_bio->devs[slot].bio = IO_BLOCKED;
2530
abbf098e 2531 rdev_dec_pending(rdev, mddev);
560f8e55 2532
7399c31b 2533read_more:
96c3fd1f
N
2534 rdev = read_balance(conf, r10_bio, &max_sectors);
2535 if (rdev == NULL) {
560f8e55
N
2536 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2537 " read error for block %llu\n",
7399c31b 2538 mdname(mddev), b,
560f8e55
N
2539 (unsigned long long)r10_bio->sector);
2540 raid_end_bio_io(r10_bio);
560f8e55
N
2541 return;
2542 }
2543
2544 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
560f8e55 2545 slot = r10_bio->read_slot;
560f8e55
N
2546 printk_ratelimited(
2547 KERN_ERR
055d3747 2548 "md/raid10:%s: %s: redirecting "
560f8e55
N
2549 "sector %llu to another mirror\n",
2550 mdname(mddev),
2551 bdevname(rdev->bdev, b),
2552 (unsigned long long)r10_bio->sector);
2553 bio = bio_clone_mddev(r10_bio->master_bio,
2554 GFP_NOIO, mddev);
7399c31b
N
2555 md_trim_bio(bio,
2556 r10_bio->sector - bio->bi_sector,
2557 max_sectors);
560f8e55 2558 r10_bio->devs[slot].bio = bio;
abbf098e 2559 r10_bio->devs[slot].rdev = rdev;
560f8e55 2560 bio->bi_sector = r10_bio->devs[slot].addr
f8c9e74f 2561 + choose_data_offset(r10_bio, rdev);
560f8e55
N
2562 bio->bi_bdev = rdev->bdev;
2563 bio->bi_rw = READ | do_sync;
2564 bio->bi_private = r10_bio;
2565 bio->bi_end_io = raid10_end_read_request;
7399c31b
N
2566 if (max_sectors < r10_bio->sectors) {
2567 /* Drat - have to split this up more */
2568 struct bio *mbio = r10_bio->master_bio;
2569 int sectors_handled =
2570 r10_bio->sector + max_sectors
2571 - mbio->bi_sector;
2572 r10_bio->sectors = max_sectors;
2573 spin_lock_irq(&conf->device_lock);
2574 if (mbio->bi_phys_segments == 0)
2575 mbio->bi_phys_segments = 2;
2576 else
2577 mbio->bi_phys_segments++;
2578 spin_unlock_irq(&conf->device_lock);
2579 generic_make_request(bio);
7399c31b
N
2580
2581 r10_bio = mempool_alloc(conf->r10bio_pool,
2582 GFP_NOIO);
2583 r10_bio->master_bio = mbio;
2584 r10_bio->sectors = (mbio->bi_size >> 9)
2585 - sectors_handled;
2586 r10_bio->state = 0;
2587 set_bit(R10BIO_ReadError,
2588 &r10_bio->state);
2589 r10_bio->mddev = mddev;
2590 r10_bio->sector = mbio->bi_sector
2591 + sectors_handled;
2592
2593 goto read_more;
2594 } else
2595 generic_make_request(bio);
560f8e55
N
2596}
2597
e879a879 2598static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
749c55e9
N
2599{
2600 /* Some sort of write request has finished and it
2601 * succeeded in writing where we thought there was a
2602 * bad block. So forget the bad block.
1a0b7cd8
N
2603 * Or possibly if failed and we need to record
2604 * a bad block.
749c55e9
N
2605 */
2606 int m;
3cb03002 2607 struct md_rdev *rdev;
749c55e9
N
2608
2609 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2610 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1a0b7cd8
N
2611 for (m = 0; m < conf->copies; m++) {
2612 int dev = r10_bio->devs[m].devnum;
2613 rdev = conf->mirrors[dev].rdev;
2614 if (r10_bio->devs[m].bio == NULL)
2615 continue;
2616 if (test_bit(BIO_UPTODATE,
749c55e9 2617 &r10_bio->devs[m].bio->bi_flags)) {
749c55e9
N
2618 rdev_clear_badblocks(
2619 rdev,
2620 r10_bio->devs[m].addr,
c6563a8c 2621 r10_bio->sectors, 0);
1a0b7cd8
N
2622 } else {
2623 if (!rdev_set_badblocks(
2624 rdev,
2625 r10_bio->devs[m].addr,
2626 r10_bio->sectors, 0))
2627 md_error(conf->mddev, rdev);
749c55e9 2628 }
9ad1aefc
N
2629 rdev = conf->mirrors[dev].replacement;
2630 if (r10_bio->devs[m].repl_bio == NULL)
2631 continue;
2632 if (test_bit(BIO_UPTODATE,
2633 &r10_bio->devs[m].repl_bio->bi_flags)) {
2634 rdev_clear_badblocks(
2635 rdev,
2636 r10_bio->devs[m].addr,
c6563a8c 2637 r10_bio->sectors, 0);
9ad1aefc
N
2638 } else {
2639 if (!rdev_set_badblocks(
2640 rdev,
2641 r10_bio->devs[m].addr,
2642 r10_bio->sectors, 0))
2643 md_error(conf->mddev, rdev);
2644 }
1a0b7cd8 2645 }
749c55e9
N
2646 put_buf(r10_bio);
2647 } else {
bd870a16
N
2648 for (m = 0; m < conf->copies; m++) {
2649 int dev = r10_bio->devs[m].devnum;
2650 struct bio *bio = r10_bio->devs[m].bio;
2651 rdev = conf->mirrors[dev].rdev;
2652 if (bio == IO_MADE_GOOD) {
749c55e9
N
2653 rdev_clear_badblocks(
2654 rdev,
2655 r10_bio->devs[m].addr,
c6563a8c 2656 r10_bio->sectors, 0);
749c55e9 2657 rdev_dec_pending(rdev, conf->mddev);
bd870a16
N
2658 } else if (bio != NULL &&
2659 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2660 if (!narrow_write_error(r10_bio, m)) {
2661 md_error(conf->mddev, rdev);
2662 set_bit(R10BIO_Degraded,
2663 &r10_bio->state);
2664 }
2665 rdev_dec_pending(rdev, conf->mddev);
749c55e9 2666 }
475b0321
N
2667 bio = r10_bio->devs[m].repl_bio;
2668 rdev = conf->mirrors[dev].replacement;
4ca40c2c 2669 if (rdev && bio == IO_MADE_GOOD) {
475b0321
N
2670 rdev_clear_badblocks(
2671 rdev,
2672 r10_bio->devs[m].addr,
c6563a8c 2673 r10_bio->sectors, 0);
475b0321
N
2674 rdev_dec_pending(rdev, conf->mddev);
2675 }
bd870a16
N
2676 }
2677 if (test_bit(R10BIO_WriteError,
2678 &r10_bio->state))
2679 close_write(r10_bio);
749c55e9
N
2680 raid_end_bio_io(r10_bio);
2681 }
2682}
2683
fd01b88c 2684static void raid10d(struct mddev *mddev)
1da177e4 2685{
9f2c9d12 2686 struct r10bio *r10_bio;
1da177e4 2687 unsigned long flags;
e879a879 2688 struct r10conf *conf = mddev->private;
1da177e4 2689 struct list_head *head = &conf->retry_list;
e1dfa0a2 2690 struct blk_plug plug;
1da177e4
LT
2691
2692 md_check_recovery(mddev);
1da177e4 2693
e1dfa0a2 2694 blk_start_plug(&plug);
1da177e4 2695 for (;;) {
6cce3b23 2696
0021b7bc 2697 flush_pending_writes(conf);
6cce3b23 2698
a35e63ef
N
2699 spin_lock_irqsave(&conf->device_lock, flags);
2700 if (list_empty(head)) {
2701 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 2702 break;
a35e63ef 2703 }
9f2c9d12 2704 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
1da177e4 2705 list_del(head->prev);
4443ae10 2706 conf->nr_queued--;
1da177e4
LT
2707 spin_unlock_irqrestore(&conf->device_lock, flags);
2708
2709 mddev = r10_bio->mddev;
070ec55d 2710 conf = mddev->private;
bd870a16
N
2711 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2712 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9 2713 handle_write_completed(conf, r10_bio);
3ea7daa5
N
2714 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2715 reshape_request_write(mddev, r10_bio);
749c55e9 2716 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
1da177e4 2717 sync_request_write(mddev, r10_bio);
7eaceacc 2718 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
1da177e4 2719 recovery_request_write(mddev, r10_bio);
856e08e2 2720 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
560f8e55 2721 handle_read_error(mddev, r10_bio);
856e08e2
N
2722 else {
2723 /* just a partial read to be scheduled from a
2724 * separate context
2725 */
2726 int slot = r10_bio->read_slot;
2727 generic_make_request(r10_bio->devs[slot].bio);
2728 }
560f8e55 2729
1d9d5241 2730 cond_resched();
de393cde
N
2731 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2732 md_check_recovery(mddev);
1da177e4 2733 }
e1dfa0a2 2734 blk_finish_plug(&plug);
1da177e4
LT
2735}
2736
2737
e879a879 2738static int init_resync(struct r10conf *conf)
1da177e4
LT
2739{
2740 int buffs;
69335ef3 2741 int i;
1da177e4
LT
2742
2743 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
b6385483 2744 BUG_ON(conf->r10buf_pool);
69335ef3 2745 conf->have_replacement = 0;
5cf00fcd 2746 for (i = 0; i < conf->geo.raid_disks; i++)
69335ef3
N
2747 if (conf->mirrors[i].replacement)
2748 conf->have_replacement = 1;
1da177e4
LT
2749 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2750 if (!conf->r10buf_pool)
2751 return -ENOMEM;
2752 conf->next_resync = 0;
2753 return 0;
2754}
2755
2756/*
2757 * perform a "sync" on one "block"
2758 *
2759 * We need to make sure that no normal I/O request - particularly write
2760 * requests - conflict with active sync requests.
2761 *
2762 * This is achieved by tracking pending requests and a 'barrier' concept
2763 * that can be installed to exclude normal IO requests.
2764 *
2765 * Resync and recovery are handled very differently.
2766 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2767 *
2768 * For resync, we iterate over virtual addresses, read all copies,
2769 * and update if there are differences. If only one copy is live,
2770 * skip it.
2771 * For recovery, we iterate over physical addresses, read a good
2772 * value for each non-in_sync drive, and over-write.
2773 *
2774 * So, for recovery we may have several outstanding complex requests for a
2775 * given address, one for each out-of-sync device. We model this by allocating
2776 * a number of r10_bio structures, one for each out-of-sync device.
2777 * As we setup these structures, we collect all bio's together into a list
2778 * which we then process collectively to add pages, and then process again
2779 * to pass to generic_make_request.
2780 *
2781 * The r10_bio structures are linked using a borrowed master_bio pointer.
2782 * This link is counted in ->remaining. When the r10_bio that points to NULL
2783 * has its remaining count decremented to 0, the whole complex operation
2784 * is complete.
2785 *
2786 */
2787
fd01b88c 2788static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
ab9d47e9 2789 int *skipped, int go_faster)
1da177e4 2790{
e879a879 2791 struct r10conf *conf = mddev->private;
9f2c9d12 2792 struct r10bio *r10_bio;
1da177e4
LT
2793 struct bio *biolist = NULL, *bio;
2794 sector_t max_sector, nr_sectors;
1da177e4 2795 int i;
6cce3b23 2796 int max_sync;
57dab0bd 2797 sector_t sync_blocks;
1da177e4
LT
2798 sector_t sectors_skipped = 0;
2799 int chunks_skipped = 0;
5cf00fcd 2800 sector_t chunk_mask = conf->geo.chunk_mask;
1da177e4
LT
2801
2802 if (!conf->r10buf_pool)
2803 if (init_resync(conf))
57afd89f 2804 return 0;
1da177e4
LT
2805
2806 skipped:
58c0fed4 2807 max_sector = mddev->dev_sectors;
3ea7daa5
N
2808 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2809 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1da177e4
LT
2810 max_sector = mddev->resync_max_sectors;
2811 if (sector_nr >= max_sector) {
6cce3b23
N
2812 /* If we aborted, we need to abort the
2813 * sync on the 'current' bitmap chucks (there can
2814 * be several when recovering multiple devices).
2815 * as we may have started syncing it but not finished.
2816 * We can find the current address in
2817 * mddev->curr_resync, but for recovery,
2818 * we need to convert that to several
2819 * virtual addresses.
2820 */
3ea7daa5
N
2821 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2822 end_reshape(conf);
2823 return 0;
2824 }
2825
6cce3b23
N
2826 if (mddev->curr_resync < max_sector) { /* aborted */
2827 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2828 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2829 &sync_blocks, 1);
5cf00fcd 2830 else for (i = 0; i < conf->geo.raid_disks; i++) {
6cce3b23
N
2831 sector_t sect =
2832 raid10_find_virt(conf, mddev->curr_resync, i);
2833 bitmap_end_sync(mddev->bitmap, sect,
2834 &sync_blocks, 1);
2835 }
9ad1aefc
N
2836 } else {
2837 /* completed sync */
2838 if ((!mddev->bitmap || conf->fullsync)
2839 && conf->have_replacement
2840 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2841 /* Completed a full sync so the replacements
2842 * are now fully recovered.
2843 */
5cf00fcd 2844 for (i = 0; i < conf->geo.raid_disks; i++)
9ad1aefc
N
2845 if (conf->mirrors[i].replacement)
2846 conf->mirrors[i].replacement
2847 ->recovery_offset
2848 = MaxSector;
2849 }
6cce3b23 2850 conf->fullsync = 0;
9ad1aefc 2851 }
6cce3b23 2852 bitmap_close_sync(mddev->bitmap);
1da177e4 2853 close_sync(conf);
57afd89f 2854 *skipped = 1;
1da177e4
LT
2855 return sectors_skipped;
2856 }
3ea7daa5
N
2857
2858 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2859 return reshape_request(mddev, sector_nr, skipped);
2860
5cf00fcd 2861 if (chunks_skipped >= conf->geo.raid_disks) {
1da177e4
LT
2862 /* if there has been nothing to do on any drive,
2863 * then there is nothing to do at all..
2864 */
57afd89f
N
2865 *skipped = 1;
2866 return (max_sector - sector_nr) + sectors_skipped;
1da177e4
LT
2867 }
2868
c6207277
N
2869 if (max_sector > mddev->resync_max)
2870 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2871
1da177e4
LT
2872 /* make sure whole request will fit in a chunk - if chunks
2873 * are meaningful
2874 */
5cf00fcd
N
2875 if (conf->geo.near_copies < conf->geo.raid_disks &&
2876 max_sector > (sector_nr | chunk_mask))
2877 max_sector = (sector_nr | chunk_mask) + 1;
1da177e4
LT
2878 /*
2879 * If there is non-resync activity waiting for us then
2880 * put in a delay to throttle resync.
2881 */
0a27ec96 2882 if (!go_faster && conf->nr_waiting)
1da177e4 2883 msleep_interruptible(1000);
1da177e4
LT
2884
2885 /* Again, very different code for resync and recovery.
2886 * Both must result in an r10bio with a list of bios that
2887 * have bi_end_io, bi_sector, bi_bdev set,
2888 * and bi_private set to the r10bio.
2889 * For recovery, we may actually create several r10bios
2890 * with 2 bios in each, that correspond to the bios in the main one.
2891 * In this case, the subordinate r10bios link back through a
2892 * borrowed master_bio pointer, and the counter in the master
2893 * includes a ref from each subordinate.
2894 */
2895 /* First, we decide what to do and set ->bi_end_io
2896 * To end_sync_read if we want to read, and
2897 * end_sync_write if we will want to write.
2898 */
2899
6cce3b23 2900 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1da177e4
LT
2901 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2902 /* recovery... the complicated one */
e875ecea 2903 int j;
1da177e4
LT
2904 r10_bio = NULL;
2905
5cf00fcd 2906 for (i = 0 ; i < conf->geo.raid_disks; i++) {
ab9d47e9 2907 int still_degraded;
9f2c9d12 2908 struct r10bio *rb2;
ab9d47e9
N
2909 sector_t sect;
2910 int must_sync;
e875ecea 2911 int any_working;
dc280d98 2912 struct raid10_info *mirror = &conf->mirrors[i];
24afd80d
N
2913
2914 if ((mirror->rdev == NULL ||
2915 test_bit(In_sync, &mirror->rdev->flags))
2916 &&
2917 (mirror->replacement == NULL ||
2918 test_bit(Faulty,
2919 &mirror->replacement->flags)))
ab9d47e9 2920 continue;
1da177e4 2921
ab9d47e9
N
2922 still_degraded = 0;
2923 /* want to reconstruct this device */
2924 rb2 = r10_bio;
2925 sect = raid10_find_virt(conf, sector_nr, i);
fc448a18
N
2926 if (sect >= mddev->resync_max_sectors) {
2927 /* last stripe is not complete - don't
2928 * try to recover this sector.
2929 */
2930 continue;
2931 }
24afd80d
N
2932 /* Unless we are doing a full sync, or a replacement
2933 * we only need to recover the block if it is set in
2934 * the bitmap
ab9d47e9
N
2935 */
2936 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2937 &sync_blocks, 1);
2938 if (sync_blocks < max_sync)
2939 max_sync = sync_blocks;
2940 if (!must_sync &&
24afd80d 2941 mirror->replacement == NULL &&
ab9d47e9
N
2942 !conf->fullsync) {
2943 /* yep, skip the sync_blocks here, but don't assume
2944 * that there will never be anything to do here
2945 */
2946 chunks_skipped = -1;
2947 continue;
2948 }
6cce3b23 2949
ab9d47e9
N
2950 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2951 raise_barrier(conf, rb2 != NULL);
2952 atomic_set(&r10_bio->remaining, 0);
18055569 2953
ab9d47e9
N
2954 r10_bio->master_bio = (struct bio*)rb2;
2955 if (rb2)
2956 atomic_inc(&rb2->remaining);
2957 r10_bio->mddev = mddev;
2958 set_bit(R10BIO_IsRecover, &r10_bio->state);
2959 r10_bio->sector = sect;
1da177e4 2960
ab9d47e9
N
2961 raid10_find_phys(conf, r10_bio);
2962
2963 /* Need to check if the array will still be
2964 * degraded
2965 */
5cf00fcd 2966 for (j = 0; j < conf->geo.raid_disks; j++)
ab9d47e9
N
2967 if (conf->mirrors[j].rdev == NULL ||
2968 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2969 still_degraded = 1;
87fc767b 2970 break;
1da177e4 2971 }
ab9d47e9
N
2972
2973 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2974 &sync_blocks, still_degraded);
2975
e875ecea 2976 any_working = 0;
ab9d47e9 2977 for (j=0; j<conf->copies;j++) {
e875ecea 2978 int k;
ab9d47e9 2979 int d = r10_bio->devs[j].devnum;
5e570289 2980 sector_t from_addr, to_addr;
3cb03002 2981 struct md_rdev *rdev;
40c356ce
N
2982 sector_t sector, first_bad;
2983 int bad_sectors;
ab9d47e9
N
2984 if (!conf->mirrors[d].rdev ||
2985 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2986 continue;
2987 /* This is where we read from */
e875ecea 2988 any_working = 1;
40c356ce
N
2989 rdev = conf->mirrors[d].rdev;
2990 sector = r10_bio->devs[j].addr;
2991
2992 if (is_badblock(rdev, sector, max_sync,
2993 &first_bad, &bad_sectors)) {
2994 if (first_bad > sector)
2995 max_sync = first_bad - sector;
2996 else {
2997 bad_sectors -= (sector
2998 - first_bad);
2999 if (max_sync > bad_sectors)
3000 max_sync = bad_sectors;
3001 continue;
3002 }
3003 }
ab9d47e9
N
3004 bio = r10_bio->devs[0].bio;
3005 bio->bi_next = biolist;
3006 biolist = bio;
3007 bio->bi_private = r10_bio;
3008 bio->bi_end_io = end_sync_read;
3009 bio->bi_rw = READ;
5e570289 3010 from_addr = r10_bio->devs[j].addr;
24afd80d
N
3011 bio->bi_sector = from_addr + rdev->data_offset;
3012 bio->bi_bdev = rdev->bdev;
3013 atomic_inc(&rdev->nr_pending);
3014 /* and we write to 'i' (if not in_sync) */
ab9d47e9
N
3015
3016 for (k=0; k<conf->copies; k++)
3017 if (r10_bio->devs[k].devnum == i)
3018 break;
3019 BUG_ON(k == conf->copies);
5e570289 3020 to_addr = r10_bio->devs[k].addr;
ab9d47e9 3021 r10_bio->devs[0].devnum = d;
5e570289 3022 r10_bio->devs[0].addr = from_addr;
ab9d47e9 3023 r10_bio->devs[1].devnum = i;
5e570289 3024 r10_bio->devs[1].addr = to_addr;
ab9d47e9 3025
24afd80d
N
3026 rdev = mirror->rdev;
3027 if (!test_bit(In_sync, &rdev->flags)) {
3028 bio = r10_bio->devs[1].bio;
3029 bio->bi_next = biolist;
3030 biolist = bio;
3031 bio->bi_private = r10_bio;
3032 bio->bi_end_io = end_sync_write;
3033 bio->bi_rw = WRITE;
3034 bio->bi_sector = to_addr
3035 + rdev->data_offset;
3036 bio->bi_bdev = rdev->bdev;
3037 atomic_inc(&r10_bio->remaining);
3038 } else
3039 r10_bio->devs[1].bio->bi_end_io = NULL;
3040
3041 /* and maybe write to replacement */
3042 bio = r10_bio->devs[1].repl_bio;
3043 if (bio)
3044 bio->bi_end_io = NULL;
3045 rdev = mirror->replacement;
3046 /* Note: if rdev != NULL, then bio
3047 * cannot be NULL as r10buf_pool_alloc will
3048 * have allocated it.
3049 * So the second test here is pointless.
3050 * But it keeps semantic-checkers happy, and
3051 * this comment keeps human reviewers
3052 * happy.
3053 */
3054 if (rdev == NULL || bio == NULL ||
3055 test_bit(Faulty, &rdev->flags))
3056 break;
3057 bio->bi_next = biolist;
3058 biolist = bio;
3059 bio->bi_private = r10_bio;
3060 bio->bi_end_io = end_sync_write;
3061 bio->bi_rw = WRITE;
3062 bio->bi_sector = to_addr + rdev->data_offset;
3063 bio->bi_bdev = rdev->bdev;
3064 atomic_inc(&r10_bio->remaining);
ab9d47e9
N
3065 break;
3066 }
3067 if (j == conf->copies) {
e875ecea
N
3068 /* Cannot recover, so abort the recovery or
3069 * record a bad block */
ab9d47e9
N
3070 put_buf(r10_bio);
3071 if (rb2)
3072 atomic_dec(&rb2->remaining);
3073 r10_bio = rb2;
e875ecea
N
3074 if (any_working) {
3075 /* problem is that there are bad blocks
3076 * on other device(s)
3077 */
3078 int k;
3079 for (k = 0; k < conf->copies; k++)
3080 if (r10_bio->devs[k].devnum == i)
3081 break;
24afd80d
N
3082 if (!test_bit(In_sync,
3083 &mirror->rdev->flags)
3084 && !rdev_set_badblocks(
3085 mirror->rdev,
3086 r10_bio->devs[k].addr,
3087 max_sync, 0))
3088 any_working = 0;
3089 if (mirror->replacement &&
3090 !rdev_set_badblocks(
3091 mirror->replacement,
e875ecea
N
3092 r10_bio->devs[k].addr,
3093 max_sync, 0))
3094 any_working = 0;
3095 }
3096 if (!any_working) {
3097 if (!test_and_set_bit(MD_RECOVERY_INTR,
3098 &mddev->recovery))
3099 printk(KERN_INFO "md/raid10:%s: insufficient "
3100 "working devices for recovery.\n",
3101 mdname(mddev));
24afd80d 3102 mirror->recovery_disabled
e875ecea
N
3103 = mddev->recovery_disabled;
3104 }
ab9d47e9 3105 break;
1da177e4 3106 }
ab9d47e9 3107 }
1da177e4
LT
3108 if (biolist == NULL) {
3109 while (r10_bio) {
9f2c9d12
N
3110 struct r10bio *rb2 = r10_bio;
3111 r10_bio = (struct r10bio*) rb2->master_bio;
1da177e4
LT
3112 rb2->master_bio = NULL;
3113 put_buf(rb2);
3114 }
3115 goto giveup;
3116 }
3117 } else {
3118 /* resync. Schedule a read for every block at this virt offset */
3119 int count = 0;
6cce3b23 3120
78200d45
N
3121 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3122
6cce3b23
N
3123 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3124 &sync_blocks, mddev->degraded) &&
ab9d47e9
N
3125 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3126 &mddev->recovery)) {
6cce3b23
N
3127 /* We can skip this block */
3128 *skipped = 1;
3129 return sync_blocks + sectors_skipped;
3130 }
3131 if (sync_blocks < max_sync)
3132 max_sync = sync_blocks;
1da177e4
LT
3133 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
3134
1da177e4
LT
3135 r10_bio->mddev = mddev;
3136 atomic_set(&r10_bio->remaining, 0);
6cce3b23
N
3137 raise_barrier(conf, 0);
3138 conf->next_resync = sector_nr;
1da177e4
LT
3139
3140 r10_bio->master_bio = NULL;
3141 r10_bio->sector = sector_nr;
3142 set_bit(R10BIO_IsSync, &r10_bio->state);
3143 raid10_find_phys(conf, r10_bio);
5cf00fcd 3144 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
1da177e4 3145
5cf00fcd 3146 for (i = 0; i < conf->copies; i++) {
1da177e4 3147 int d = r10_bio->devs[i].devnum;
40c356ce
N
3148 sector_t first_bad, sector;
3149 int bad_sectors;
3150
9ad1aefc
N
3151 if (r10_bio->devs[i].repl_bio)
3152 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3153
1da177e4
LT
3154 bio = r10_bio->devs[i].bio;
3155 bio->bi_end_io = NULL;
af03b8e4 3156 clear_bit(BIO_UPTODATE, &bio->bi_flags);
1da177e4 3157 if (conf->mirrors[d].rdev == NULL ||
b2d444d7 3158 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1da177e4 3159 continue;
40c356ce
N
3160 sector = r10_bio->devs[i].addr;
3161 if (is_badblock(conf->mirrors[d].rdev,
3162 sector, max_sync,
3163 &first_bad, &bad_sectors)) {
3164 if (first_bad > sector)
3165 max_sync = first_bad - sector;
3166 else {
3167 bad_sectors -= (sector - first_bad);
3168 if (max_sync > bad_sectors)
3169 max_sync = max_sync;
3170 continue;
3171 }
3172 }
1da177e4
LT
3173 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3174 atomic_inc(&r10_bio->remaining);
3175 bio->bi_next = biolist;
3176 biolist = bio;
3177 bio->bi_private = r10_bio;
3178 bio->bi_end_io = end_sync_read;
802ba064 3179 bio->bi_rw = READ;
40c356ce 3180 bio->bi_sector = sector +
1da177e4
LT
3181 conf->mirrors[d].rdev->data_offset;
3182 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3183 count++;
9ad1aefc
N
3184
3185 if (conf->mirrors[d].replacement == NULL ||
3186 test_bit(Faulty,
3187 &conf->mirrors[d].replacement->flags))
3188 continue;
3189
3190 /* Need to set up for writing to the replacement */
3191 bio = r10_bio->devs[i].repl_bio;
3192 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3193
3194 sector = r10_bio->devs[i].addr;
3195 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3196 bio->bi_next = biolist;
3197 biolist = bio;
3198 bio->bi_private = r10_bio;
3199 bio->bi_end_io = end_sync_write;
3200 bio->bi_rw = WRITE;
3201 bio->bi_sector = sector +
3202 conf->mirrors[d].replacement->data_offset;
3203 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3204 count++;
1da177e4
LT
3205 }
3206
3207 if (count < 2) {
3208 for (i=0; i<conf->copies; i++) {
3209 int d = r10_bio->devs[i].devnum;
3210 if (r10_bio->devs[i].bio->bi_end_io)
ab9d47e9
N
3211 rdev_dec_pending(conf->mirrors[d].rdev,
3212 mddev);
9ad1aefc
N
3213 if (r10_bio->devs[i].repl_bio &&
3214 r10_bio->devs[i].repl_bio->bi_end_io)
3215 rdev_dec_pending(
3216 conf->mirrors[d].replacement,
3217 mddev);
1da177e4
LT
3218 }
3219 put_buf(r10_bio);
3220 biolist = NULL;
3221 goto giveup;
3222 }
3223 }
3224
3225 for (bio = biolist; bio ; bio=bio->bi_next) {
3226
3227 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3228 if (bio->bi_end_io)
3229 bio->bi_flags |= 1 << BIO_UPTODATE;
3230 bio->bi_vcnt = 0;
3231 bio->bi_idx = 0;
3232 bio->bi_phys_segments = 0;
1da177e4
LT
3233 bio->bi_size = 0;
3234 }
3235
3236 nr_sectors = 0;
6cce3b23
N
3237 if (sector_nr + max_sync < max_sector)
3238 max_sector = sector_nr + max_sync;
1da177e4
LT
3239 do {
3240 struct page *page;
3241 int len = PAGE_SIZE;
1da177e4
LT
3242 if (sector_nr + (len>>9) > max_sector)
3243 len = (max_sector - sector_nr) << 9;
3244 if (len == 0)
3245 break;
3246 for (bio= biolist ; bio ; bio=bio->bi_next) {
ab9d47e9 3247 struct bio *bio2;
1da177e4 3248 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
ab9d47e9
N
3249 if (bio_add_page(bio, page, len, 0))
3250 continue;
3251
3252 /* stop here */
3253 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3254 for (bio2 = biolist;
3255 bio2 && bio2 != bio;
3256 bio2 = bio2->bi_next) {
3257 /* remove last page from this bio */
3258 bio2->bi_vcnt--;
3259 bio2->bi_size -= len;
3260 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1da177e4 3261 }
ab9d47e9 3262 goto bio_full;
1da177e4
LT
3263 }
3264 nr_sectors += len>>9;
3265 sector_nr += len>>9;
3266 } while (biolist->bi_vcnt < RESYNC_PAGES);
3267 bio_full:
3268 r10_bio->sectors = nr_sectors;
3269
3270 while (biolist) {
3271 bio = biolist;
3272 biolist = biolist->bi_next;
3273
3274 bio->bi_next = NULL;
3275 r10_bio = bio->bi_private;
3276 r10_bio->sectors = nr_sectors;
3277
3278 if (bio->bi_end_io == end_sync_read) {
3279 md_sync_acct(bio->bi_bdev, nr_sectors);
3280 generic_make_request(bio);
3281 }
3282 }
3283
57afd89f
N
3284 if (sectors_skipped)
3285 /* pretend they weren't skipped, it makes
3286 * no important difference in this case
3287 */
3288 md_done_sync(mddev, sectors_skipped, 1);
3289
1da177e4
LT
3290 return sectors_skipped + nr_sectors;
3291 giveup:
3292 /* There is nowhere to write, so all non-sync
e875ecea
N
3293 * drives must be failed or in resync, all drives
3294 * have a bad block, so try the next chunk...
1da177e4 3295 */
09b4068a
N
3296 if (sector_nr + max_sync < max_sector)
3297 max_sector = sector_nr + max_sync;
3298
3299 sectors_skipped += (max_sector - sector_nr);
1da177e4
LT
3300 chunks_skipped ++;
3301 sector_nr = max_sector;
1da177e4 3302 goto skipped;
1da177e4
LT
3303}
3304
80c3a6ce 3305static sector_t
fd01b88c 3306raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce
DW
3307{
3308 sector_t size;
e879a879 3309 struct r10conf *conf = mddev->private;
80c3a6ce
DW
3310
3311 if (!raid_disks)
3ea7daa5
N
3312 raid_disks = min(conf->geo.raid_disks,
3313 conf->prev.raid_disks);
80c3a6ce 3314 if (!sectors)
dab8b292 3315 sectors = conf->dev_sectors;
80c3a6ce 3316
5cf00fcd
N
3317 size = sectors >> conf->geo.chunk_shift;
3318 sector_div(size, conf->geo.far_copies);
80c3a6ce 3319 size = size * raid_disks;
5cf00fcd 3320 sector_div(size, conf->geo.near_copies);
80c3a6ce 3321
5cf00fcd 3322 return size << conf->geo.chunk_shift;
80c3a6ce
DW
3323}
3324
6508fdbf
N
3325static void calc_sectors(struct r10conf *conf, sector_t size)
3326{
3327 /* Calculate the number of sectors-per-device that will
3328 * actually be used, and set conf->dev_sectors and
3329 * conf->stride
3330 */
3331
5cf00fcd
N
3332 size = size >> conf->geo.chunk_shift;
3333 sector_div(size, conf->geo.far_copies);
3334 size = size * conf->geo.raid_disks;
3335 sector_div(size, conf->geo.near_copies);
6508fdbf
N
3336 /* 'size' is now the number of chunks in the array */
3337 /* calculate "used chunks per device" */
3338 size = size * conf->copies;
3339
3340 /* We need to round up when dividing by raid_disks to
3341 * get the stride size.
3342 */
5cf00fcd 3343 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
6508fdbf 3344
5cf00fcd 3345 conf->dev_sectors = size << conf->geo.chunk_shift;
6508fdbf 3346
5cf00fcd
N
3347 if (conf->geo.far_offset)
3348 conf->geo.stride = 1 << conf->geo.chunk_shift;
6508fdbf 3349 else {
5cf00fcd
N
3350 sector_div(size, conf->geo.far_copies);
3351 conf->geo.stride = size << conf->geo.chunk_shift;
6508fdbf
N
3352 }
3353}
dab8b292 3354
deb200d0
N
3355enum geo_type {geo_new, geo_old, geo_start};
3356static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3357{
3358 int nc, fc, fo;
3359 int layout, chunk, disks;
3360 switch (new) {
3361 case geo_old:
3362 layout = mddev->layout;
3363 chunk = mddev->chunk_sectors;
3364 disks = mddev->raid_disks - mddev->delta_disks;
3365 break;
3366 case geo_new:
3367 layout = mddev->new_layout;
3368 chunk = mddev->new_chunk_sectors;
3369 disks = mddev->raid_disks;
3370 break;
3371 default: /* avoid 'may be unused' warnings */
3372 case geo_start: /* new when starting reshape - raid_disks not
3373 * updated yet. */
3374 layout = mddev->new_layout;
3375 chunk = mddev->new_chunk_sectors;
3376 disks = mddev->raid_disks + mddev->delta_disks;
3377 break;
3378 }
3379 if (layout >> 17)
3380 return -1;
3381 if (chunk < (PAGE_SIZE >> 9) ||
3382 !is_power_of_2(chunk))
3383 return -2;
3384 nc = layout & 255;
3385 fc = (layout >> 8) & 255;
3386 fo = layout & (1<<16);
3387 geo->raid_disks = disks;
3388 geo->near_copies = nc;
3389 geo->far_copies = fc;
3390 geo->far_offset = fo;
3391 geo->chunk_mask = chunk - 1;
3392 geo->chunk_shift = ffz(~chunk);
3393 return nc*fc;
3394}
3395
e879a879 3396static struct r10conf *setup_conf(struct mddev *mddev)
1da177e4 3397{
e879a879 3398 struct r10conf *conf = NULL;
dab8b292 3399 int err = -EINVAL;
deb200d0
N
3400 struct geom geo;
3401 int copies;
3402
3403 copies = setup_geo(&geo, mddev, geo_new);
1da177e4 3404
deb200d0 3405 if (copies == -2) {
128595ed
N
3406 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3407 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3408 mdname(mddev), PAGE_SIZE);
dab8b292 3409 goto out;
1da177e4 3410 }
2604b703 3411
deb200d0 3412 if (copies < 2 || copies > mddev->raid_disks) {
128595ed 3413 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
f73ea873 3414 mdname(mddev), mddev->new_layout);
1da177e4
LT
3415 goto out;
3416 }
dab8b292
TM
3417
3418 err = -ENOMEM;
e879a879 3419 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
dab8b292 3420 if (!conf)
1da177e4 3421 goto out;
dab8b292 3422
3ea7daa5 3423 /* FIXME calc properly */
dc280d98 3424 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
3ea7daa5 3425 max(0,mddev->delta_disks)),
dab8b292
TM
3426 GFP_KERNEL);
3427 if (!conf->mirrors)
3428 goto out;
4443ae10
N
3429
3430 conf->tmppage = alloc_page(GFP_KERNEL);
3431 if (!conf->tmppage)
dab8b292
TM
3432 goto out;
3433
deb200d0
N
3434 conf->geo = geo;
3435 conf->copies = copies;
dab8b292
TM
3436 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3437 r10bio_pool_free, conf);
3438 if (!conf->r10bio_pool)
3439 goto out;
3440
6508fdbf 3441 calc_sectors(conf, mddev->dev_sectors);
3ea7daa5
N
3442 if (mddev->reshape_position == MaxSector) {
3443 conf->prev = conf->geo;
3444 conf->reshape_progress = MaxSector;
3445 } else {
3446 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3447 err = -EINVAL;
3448 goto out;
3449 }
3450 conf->reshape_progress = mddev->reshape_position;
3451 if (conf->prev.far_offset)
3452 conf->prev.stride = 1 << conf->prev.chunk_shift;
3453 else
3454 /* far_copies must be 1 */
3455 conf->prev.stride = conf->dev_sectors;
3456 }
e7e72bf6 3457 spin_lock_init(&conf->device_lock);
dab8b292
TM
3458 INIT_LIST_HEAD(&conf->retry_list);
3459
3460 spin_lock_init(&conf->resync_lock);
3461 init_waitqueue_head(&conf->wait_barrier);
3462
0232605d 3463 conf->thread = md_register_thread(raid10d, mddev, "raid10");
dab8b292
TM
3464 if (!conf->thread)
3465 goto out;
3466
dab8b292
TM
3467 conf->mddev = mddev;
3468 return conf;
3469
3470 out:
3ea7daa5
N
3471 if (err == -ENOMEM)
3472 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3473 mdname(mddev));
dab8b292
TM
3474 if (conf) {
3475 if (conf->r10bio_pool)
3476 mempool_destroy(conf->r10bio_pool);
3477 kfree(conf->mirrors);
3478 safe_put_page(conf->tmppage);
3479 kfree(conf);
3480 }
3481 return ERR_PTR(err);
3482}
3483
fd01b88c 3484static int run(struct mddev *mddev)
dab8b292 3485{
e879a879 3486 struct r10conf *conf;
dab8b292 3487 int i, disk_idx, chunk_size;
dc280d98 3488 struct raid10_info *disk;
3cb03002 3489 struct md_rdev *rdev;
dab8b292 3490 sector_t size;
3ea7daa5
N
3491 sector_t min_offset_diff = 0;
3492 int first = 1;
532a2a3f 3493 bool discard_supported = false;
dab8b292
TM
3494
3495 if (mddev->private == NULL) {
3496 conf = setup_conf(mddev);
3497 if (IS_ERR(conf))
3498 return PTR_ERR(conf);
3499 mddev->private = conf;
3500 }
3501 conf = mddev->private;
3502 if (!conf)
3503 goto out;
3504
dab8b292
TM
3505 mddev->thread = conf->thread;
3506 conf->thread = NULL;
3507
8f6c2e4b 3508 chunk_size = mddev->chunk_sectors << 9;
cc4d1efd 3509 if (mddev->queue) {
532a2a3f
SL
3510 blk_queue_max_discard_sectors(mddev->queue,
3511 mddev->chunk_sectors);
cc4d1efd
JB
3512 blk_queue_io_min(mddev->queue, chunk_size);
3513 if (conf->geo.raid_disks % conf->geo.near_copies)
3514 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3515 else
3516 blk_queue_io_opt(mddev->queue, chunk_size *
3517 (conf->geo.raid_disks / conf->geo.near_copies));
3518 }
8f6c2e4b 3519
dafb20fa 3520 rdev_for_each(rdev, mddev) {
3ea7daa5 3521 long long diff;
aba336bd 3522 struct request_queue *q;
34b343cf 3523
1da177e4 3524 disk_idx = rdev->raid_disk;
f8c9e74f
N
3525 if (disk_idx < 0)
3526 continue;
3527 if (disk_idx >= conf->geo.raid_disks &&
3528 disk_idx >= conf->prev.raid_disks)
1da177e4
LT
3529 continue;
3530 disk = conf->mirrors + disk_idx;
3531
56a2559b
N
3532 if (test_bit(Replacement, &rdev->flags)) {
3533 if (disk->replacement)
3534 goto out_free_conf;
3535 disk->replacement = rdev;
3536 } else {
3537 if (disk->rdev)
3538 goto out_free_conf;
3539 disk->rdev = rdev;
3540 }
aba336bd
N
3541 q = bdev_get_queue(rdev->bdev);
3542 if (q->merge_bvec_fn)
3543 mddev->merge_check_needed = 1;
3ea7daa5
N
3544 diff = (rdev->new_data_offset - rdev->data_offset);
3545 if (!mddev->reshape_backwards)
3546 diff = -diff;
3547 if (diff < 0)
3548 diff = 0;
3549 if (first || diff < min_offset_diff)
3550 min_offset_diff = diff;
56a2559b 3551
cc4d1efd
JB
3552 if (mddev->gendisk)
3553 disk_stack_limits(mddev->gendisk, rdev->bdev,
3554 rdev->data_offset << 9);
1da177e4
LT
3555
3556 disk->head_position = 0;
532a2a3f
SL
3557
3558 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3559 discard_supported = true;
1da177e4 3560 }
3ea7daa5 3561
532a2a3f
SL
3562 if (discard_supported)
3563 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
3564 else
3565 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
3566
6d508242 3567 /* need to check that every block has at least one working mirror */
700c7213 3568 if (!enough(conf, -1)) {
128595ed 3569 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
6d508242 3570 mdname(mddev));
1da177e4
LT
3571 goto out_free_conf;
3572 }
3573
3ea7daa5
N
3574 if (conf->reshape_progress != MaxSector) {
3575 /* must ensure that shape change is supported */
3576 if (conf->geo.far_copies != 1 &&
3577 conf->geo.far_offset == 0)
3578 goto out_free_conf;
3579 if (conf->prev.far_copies != 1 &&
3580 conf->geo.far_offset == 0)
3581 goto out_free_conf;
3582 }
3583
1da177e4 3584 mddev->degraded = 0;
f8c9e74f
N
3585 for (i = 0;
3586 i < conf->geo.raid_disks
3587 || i < conf->prev.raid_disks;
3588 i++) {
1da177e4
LT
3589
3590 disk = conf->mirrors + i;
3591
56a2559b
N
3592 if (!disk->rdev && disk->replacement) {
3593 /* The replacement is all we have - use it */
3594 disk->rdev = disk->replacement;
3595 disk->replacement = NULL;
3596 clear_bit(Replacement, &disk->rdev->flags);
3597 }
3598
5fd6c1dc 3599 if (!disk->rdev ||
2e333e89 3600 !test_bit(In_sync, &disk->rdev->flags)) {
1da177e4
LT
3601 disk->head_position = 0;
3602 mddev->degraded++;
8c2e870a
NB
3603 if (disk->rdev)
3604 conf->fullsync = 1;
1da177e4 3605 }
d890fa2b 3606 disk->recovery_disabled = mddev->recovery_disabled - 1;
1da177e4
LT
3607 }
3608
8c6ac868 3609 if (mddev->recovery_cp != MaxSector)
128595ed 3610 printk(KERN_NOTICE "md/raid10:%s: not clean"
8c6ac868
AN
3611 " -- starting background reconstruction\n",
3612 mdname(mddev));
1da177e4 3613 printk(KERN_INFO
128595ed 3614 "md/raid10:%s: active with %d out of %d devices\n",
5cf00fcd
N
3615 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3616 conf->geo.raid_disks);
1da177e4
LT
3617 /*
3618 * Ok, everything is just fine now
3619 */
dab8b292
TM
3620 mddev->dev_sectors = conf->dev_sectors;
3621 size = raid10_size(mddev, 0, 0);
3622 md_set_array_sectors(mddev, size);
3623 mddev->resync_max_sectors = size;
1da177e4 3624
cc4d1efd 3625 if (mddev->queue) {
5cf00fcd 3626 int stripe = conf->geo.raid_disks *
9d8f0363 3627 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
cc4d1efd
JB
3628 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3629 mddev->queue->backing_dev_info.congested_data = mddev;
3630
3631 /* Calculate max read-ahead size.
3632 * We need to readahead at least twice a whole stripe....
3633 * maybe...
3634 */
5cf00fcd 3635 stripe /= conf->geo.near_copies;
3ea7daa5
N
3636 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3637 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
cc4d1efd 3638 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1da177e4
LT
3639 }
3640
a91a2785
MP
3641
3642 if (md_integrity_register(mddev))
3643 goto out_free_conf;
3644
3ea7daa5
N
3645 if (conf->reshape_progress != MaxSector) {
3646 unsigned long before_length, after_length;
3647
3648 before_length = ((1 << conf->prev.chunk_shift) *
3649 conf->prev.far_copies);
3650 after_length = ((1 << conf->geo.chunk_shift) *
3651 conf->geo.far_copies);
3652
3653 if (max(before_length, after_length) > min_offset_diff) {
3654 /* This cannot work */
3655 printk("md/raid10: offset difference not enough to continue reshape\n");
3656 goto out_free_conf;
3657 }
3658 conf->offset_diff = min_offset_diff;
3659
3660 conf->reshape_safe = conf->reshape_progress;
3661 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3662 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3663 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3664 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3665 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3666 "reshape");
3667 }
3668
1da177e4
LT
3669 return 0;
3670
3671out_free_conf:
01f96c0a 3672 md_unregister_thread(&mddev->thread);
1da177e4
LT
3673 if (conf->r10bio_pool)
3674 mempool_destroy(conf->r10bio_pool);
1345b1d8 3675 safe_put_page(conf->tmppage);
990a8baf 3676 kfree(conf->mirrors);
1da177e4
LT
3677 kfree(conf);
3678 mddev->private = NULL;
3679out:
3680 return -EIO;
3681}
3682
fd01b88c 3683static int stop(struct mddev *mddev)
1da177e4 3684{
e879a879 3685 struct r10conf *conf = mddev->private;
1da177e4 3686
409c57f3
N
3687 raise_barrier(conf, 0);
3688 lower_barrier(conf);
3689
01f96c0a 3690 md_unregister_thread(&mddev->thread);
cc4d1efd
JB
3691 if (mddev->queue)
3692 /* the unplug fn references 'conf'*/
3693 blk_sync_queue(mddev->queue);
3694
1da177e4
LT
3695 if (conf->r10bio_pool)
3696 mempool_destroy(conf->r10bio_pool);
990a8baf 3697 kfree(conf->mirrors);
1da177e4
LT
3698 kfree(conf);
3699 mddev->private = NULL;
3700 return 0;
3701}
3702
fd01b88c 3703static void raid10_quiesce(struct mddev *mddev, int state)
6cce3b23 3704{
e879a879 3705 struct r10conf *conf = mddev->private;
6cce3b23
N
3706
3707 switch(state) {
3708 case 1:
3709 raise_barrier(conf, 0);
3710 break;
3711 case 0:
3712 lower_barrier(conf);
3713 break;
3714 }
6cce3b23 3715}
1da177e4 3716
006a09a0
N
3717static int raid10_resize(struct mddev *mddev, sector_t sectors)
3718{
3719 /* Resize of 'far' arrays is not supported.
3720 * For 'near' and 'offset' arrays we can set the
3721 * number of sectors used to be an appropriate multiple
3722 * of the chunk size.
3723 * For 'offset', this is far_copies*chunksize.
3724 * For 'near' the multiplier is the LCM of
3725 * near_copies and raid_disks.
3726 * So if far_copies > 1 && !far_offset, fail.
3727 * Else find LCM(raid_disks, near_copy)*far_copies and
3728 * multiply by chunk_size. Then round to this number.
3729 * This is mostly done by raid10_size()
3730 */
3731 struct r10conf *conf = mddev->private;
3732 sector_t oldsize, size;
3733
f8c9e74f
N
3734 if (mddev->reshape_position != MaxSector)
3735 return -EBUSY;
3736
5cf00fcd 3737 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
006a09a0
N
3738 return -EINVAL;
3739
3740 oldsize = raid10_size(mddev, 0, 0);
3741 size = raid10_size(mddev, sectors, 0);
a4a6125a
N
3742 if (mddev->external_size &&
3743 mddev->array_sectors > size)
006a09a0 3744 return -EINVAL;
a4a6125a
N
3745 if (mddev->bitmap) {
3746 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3747 if (ret)
3748 return ret;
3749 }
3750 md_set_array_sectors(mddev, size);
006a09a0
N
3751 set_capacity(mddev->gendisk, mddev->array_sectors);
3752 revalidate_disk(mddev->gendisk);
3753 if (sectors > mddev->dev_sectors &&
3754 mddev->recovery_cp > oldsize) {
3755 mddev->recovery_cp = oldsize;
3756 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3757 }
6508fdbf
N
3758 calc_sectors(conf, sectors);
3759 mddev->dev_sectors = conf->dev_sectors;
006a09a0
N
3760 mddev->resync_max_sectors = size;
3761 return 0;
3762}
3763
fd01b88c 3764static void *raid10_takeover_raid0(struct mddev *mddev)
dab8b292 3765{
3cb03002 3766 struct md_rdev *rdev;
e879a879 3767 struct r10conf *conf;
dab8b292
TM
3768
3769 if (mddev->degraded > 0) {
128595ed
N
3770 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3771 mdname(mddev));
dab8b292
TM
3772 return ERR_PTR(-EINVAL);
3773 }
3774
dab8b292
TM
3775 /* Set new parameters */
3776 mddev->new_level = 10;
3777 /* new layout: far_copies = 1, near_copies = 2 */
3778 mddev->new_layout = (1<<8) + 2;
3779 mddev->new_chunk_sectors = mddev->chunk_sectors;
3780 mddev->delta_disks = mddev->raid_disks;
dab8b292
TM
3781 mddev->raid_disks *= 2;
3782 /* make sure it will be not marked as dirty */
3783 mddev->recovery_cp = MaxSector;
3784
3785 conf = setup_conf(mddev);
02214dc5 3786 if (!IS_ERR(conf)) {
dafb20fa 3787 rdev_for_each(rdev, mddev)
e93f68a1
N
3788 if (rdev->raid_disk >= 0)
3789 rdev->new_raid_disk = rdev->raid_disk * 2;
02214dc5
KW
3790 conf->barrier = 1;
3791 }
3792
dab8b292
TM
3793 return conf;
3794}
3795
fd01b88c 3796static void *raid10_takeover(struct mddev *mddev)
dab8b292 3797{
e373ab10 3798 struct r0conf *raid0_conf;
dab8b292
TM
3799
3800 /* raid10 can take over:
3801 * raid0 - providing it has only two drives
3802 */
3803 if (mddev->level == 0) {
3804 /* for raid0 takeover only one zone is supported */
e373ab10
N
3805 raid0_conf = mddev->private;
3806 if (raid0_conf->nr_strip_zones > 1) {
128595ed
N
3807 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3808 " with more than one zone.\n",
3809 mdname(mddev));
dab8b292
TM
3810 return ERR_PTR(-EINVAL);
3811 }
3812 return raid10_takeover_raid0(mddev);
3813 }
3814 return ERR_PTR(-EINVAL);
3815}
3816
3ea7daa5
N
3817static int raid10_check_reshape(struct mddev *mddev)
3818{
3819 /* Called when there is a request to change
3820 * - layout (to ->new_layout)
3821 * - chunk size (to ->new_chunk_sectors)
3822 * - raid_disks (by delta_disks)
3823 * or when trying to restart a reshape that was ongoing.
3824 *
3825 * We need to validate the request and possibly allocate
3826 * space if that might be an issue later.
3827 *
3828 * Currently we reject any reshape of a 'far' mode array,
3829 * allow chunk size to change if new is generally acceptable,
3830 * allow raid_disks to increase, and allow
3831 * a switch between 'near' mode and 'offset' mode.
3832 */
3833 struct r10conf *conf = mddev->private;
3834 struct geom geo;
3835
3836 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3837 return -EINVAL;
3838
3839 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3840 /* mustn't change number of copies */
3841 return -EINVAL;
3842 if (geo.far_copies > 1 && !geo.far_offset)
3843 /* Cannot switch to 'far' mode */
3844 return -EINVAL;
3845
3846 if (mddev->array_sectors & geo.chunk_mask)
3847 /* not factor of array size */
3848 return -EINVAL;
3849
3ea7daa5
N
3850 if (!enough(conf, -1))
3851 return -EINVAL;
3852
3853 kfree(conf->mirrors_new);
3854 conf->mirrors_new = NULL;
3855 if (mddev->delta_disks > 0) {
3856 /* allocate new 'mirrors' list */
3857 conf->mirrors_new = kzalloc(
dc280d98 3858 sizeof(struct raid10_info)
3ea7daa5
N
3859 *(mddev->raid_disks +
3860 mddev->delta_disks),
3861 GFP_KERNEL);
3862 if (!conf->mirrors_new)
3863 return -ENOMEM;
3864 }
3865 return 0;
3866}
3867
3868/*
3869 * Need to check if array has failed when deciding whether to:
3870 * - start an array
3871 * - remove non-faulty devices
3872 * - add a spare
3873 * - allow a reshape
3874 * This determination is simple when no reshape is happening.
3875 * However if there is a reshape, we need to carefully check
3876 * both the before and after sections.
3877 * This is because some failed devices may only affect one
3878 * of the two sections, and some non-in_sync devices may
3879 * be insync in the section most affected by failed devices.
3880 */
3881static int calc_degraded(struct r10conf *conf)
3882{
3883 int degraded, degraded2;
3884 int i;
3885
3886 rcu_read_lock();
3887 degraded = 0;
3888 /* 'prev' section first */
3889 for (i = 0; i < conf->prev.raid_disks; i++) {
3890 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3891 if (!rdev || test_bit(Faulty, &rdev->flags))
3892 degraded++;
3893 else if (!test_bit(In_sync, &rdev->flags))
3894 /* When we can reduce the number of devices in
3895 * an array, this might not contribute to
3896 * 'degraded'. It does now.
3897 */
3898 degraded++;
3899 }
3900 rcu_read_unlock();
3901 if (conf->geo.raid_disks == conf->prev.raid_disks)
3902 return degraded;
3903 rcu_read_lock();
3904 degraded2 = 0;
3905 for (i = 0; i < conf->geo.raid_disks; i++) {
3906 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
3907 if (!rdev || test_bit(Faulty, &rdev->flags))
3908 degraded2++;
3909 else if (!test_bit(In_sync, &rdev->flags)) {
3910 /* If reshape is increasing the number of devices,
3911 * this section has already been recovered, so
3912 * it doesn't contribute to degraded.
3913 * else it does.
3914 */
3915 if (conf->geo.raid_disks <= conf->prev.raid_disks)
3916 degraded2++;
3917 }
3918 }
3919 rcu_read_unlock();
3920 if (degraded2 > degraded)
3921 return degraded2;
3922 return degraded;
3923}
3924
3925static int raid10_start_reshape(struct mddev *mddev)
3926{
3927 /* A 'reshape' has been requested. This commits
3928 * the various 'new' fields and sets MD_RECOVER_RESHAPE
3929 * This also checks if there are enough spares and adds them
3930 * to the array.
3931 * We currently require enough spares to make the final
3932 * array non-degraded. We also require that the difference
3933 * between old and new data_offset - on each device - is
3934 * enough that we never risk over-writing.
3935 */
3936
3937 unsigned long before_length, after_length;
3938 sector_t min_offset_diff = 0;
3939 int first = 1;
3940 struct geom new;
3941 struct r10conf *conf = mddev->private;
3942 struct md_rdev *rdev;
3943 int spares = 0;
bb63a701 3944 int ret;
3ea7daa5
N
3945
3946 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3947 return -EBUSY;
3948
3949 if (setup_geo(&new, mddev, geo_start) != conf->copies)
3950 return -EINVAL;
3951
3952 before_length = ((1 << conf->prev.chunk_shift) *
3953 conf->prev.far_copies);
3954 after_length = ((1 << conf->geo.chunk_shift) *
3955 conf->geo.far_copies);
3956
3957 rdev_for_each(rdev, mddev) {
3958 if (!test_bit(In_sync, &rdev->flags)
3959 && !test_bit(Faulty, &rdev->flags))
3960 spares++;
3961 if (rdev->raid_disk >= 0) {
3962 long long diff = (rdev->new_data_offset
3963 - rdev->data_offset);
3964 if (!mddev->reshape_backwards)
3965 diff = -diff;
3966 if (diff < 0)
3967 diff = 0;
3968 if (first || diff < min_offset_diff)
3969 min_offset_diff = diff;
3970 }
3971 }
3972
3973 if (max(before_length, after_length) > min_offset_diff)
3974 return -EINVAL;
3975
3976 if (spares < mddev->delta_disks)
3977 return -EINVAL;
3978
3979 conf->offset_diff = min_offset_diff;
3980 spin_lock_irq(&conf->device_lock);
3981 if (conf->mirrors_new) {
3982 memcpy(conf->mirrors_new, conf->mirrors,
dc280d98 3983 sizeof(struct raid10_info)*conf->prev.raid_disks);
3ea7daa5
N
3984 smp_mb();
3985 kfree(conf->mirrors_old); /* FIXME and elsewhere */
3986 conf->mirrors_old = conf->mirrors;
3987 conf->mirrors = conf->mirrors_new;
3988 conf->mirrors_new = NULL;
3989 }
3990 setup_geo(&conf->geo, mddev, geo_start);
3991 smp_mb();
3992 if (mddev->reshape_backwards) {
3993 sector_t size = raid10_size(mddev, 0, 0);
3994 if (size < mddev->array_sectors) {
3995 spin_unlock_irq(&conf->device_lock);
3996 printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
3997 mdname(mddev));
3998 return -EINVAL;
3999 }
4000 mddev->resync_max_sectors = size;
4001 conf->reshape_progress = size;
4002 } else
4003 conf->reshape_progress = 0;
4004 spin_unlock_irq(&conf->device_lock);
4005
bb63a701
N
4006 if (mddev->delta_disks && mddev->bitmap) {
4007 ret = bitmap_resize(mddev->bitmap,
4008 raid10_size(mddev, 0,
4009 conf->geo.raid_disks),
4010 0, 0);
4011 if (ret)
4012 goto abort;
4013 }
3ea7daa5
N
4014 if (mddev->delta_disks > 0) {
4015 rdev_for_each(rdev, mddev)
4016 if (rdev->raid_disk < 0 &&
4017 !test_bit(Faulty, &rdev->flags)) {
4018 if (raid10_add_disk(mddev, rdev) == 0) {
4019 if (rdev->raid_disk >=
4020 conf->prev.raid_disks)
4021 set_bit(In_sync, &rdev->flags);
4022 else
4023 rdev->recovery_offset = 0;
4024
4025 if (sysfs_link_rdev(mddev, rdev))
4026 /* Failure here is OK */;
4027 }
4028 } else if (rdev->raid_disk >= conf->prev.raid_disks
4029 && !test_bit(Faulty, &rdev->flags)) {
4030 /* This is a spare that was manually added */
4031 set_bit(In_sync, &rdev->flags);
4032 }
4033 }
4034 /* When a reshape changes the number of devices,
4035 * ->degraded is measured against the larger of the
4036 * pre and post numbers.
4037 */
4038 spin_lock_irq(&conf->device_lock);
4039 mddev->degraded = calc_degraded(conf);
4040 spin_unlock_irq(&conf->device_lock);
4041 mddev->raid_disks = conf->geo.raid_disks;
4042 mddev->reshape_position = conf->reshape_progress;
4043 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4044
4045 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4046 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4047 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4048 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4049
4050 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4051 "reshape");
4052 if (!mddev->sync_thread) {
bb63a701
N
4053 ret = -EAGAIN;
4054 goto abort;
3ea7daa5
N
4055 }
4056 conf->reshape_checkpoint = jiffies;
4057 md_wakeup_thread(mddev->sync_thread);
4058 md_new_event(mddev);
4059 return 0;
bb63a701
N
4060
4061abort:
4062 mddev->recovery = 0;
4063 spin_lock_irq(&conf->device_lock);
4064 conf->geo = conf->prev;
4065 mddev->raid_disks = conf->geo.raid_disks;
4066 rdev_for_each(rdev, mddev)
4067 rdev->new_data_offset = rdev->data_offset;
4068 smp_wmb();
4069 conf->reshape_progress = MaxSector;
4070 mddev->reshape_position = MaxSector;
4071 spin_unlock_irq(&conf->device_lock);
4072 return ret;
3ea7daa5
N
4073}
4074
4075/* Calculate the last device-address that could contain
4076 * any block from the chunk that includes the array-address 's'
4077 * and report the next address.
4078 * i.e. the address returned will be chunk-aligned and after
4079 * any data that is in the chunk containing 's'.
4080 */
4081static sector_t last_dev_address(sector_t s, struct geom *geo)
4082{
4083 s = (s | geo->chunk_mask) + 1;
4084 s >>= geo->chunk_shift;
4085 s *= geo->near_copies;
4086 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4087 s *= geo->far_copies;
4088 s <<= geo->chunk_shift;
4089 return s;
4090}
4091
4092/* Calculate the first device-address that could contain
4093 * any block from the chunk that includes the array-address 's'.
4094 * This too will be the start of a chunk
4095 */
4096static sector_t first_dev_address(sector_t s, struct geom *geo)
4097{
4098 s >>= geo->chunk_shift;
4099 s *= geo->near_copies;
4100 sector_div(s, geo->raid_disks);
4101 s *= geo->far_copies;
4102 s <<= geo->chunk_shift;
4103 return s;
4104}
4105
4106static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4107 int *skipped)
4108{
4109 /* We simply copy at most one chunk (smallest of old and new)
4110 * at a time, possibly less if that exceeds RESYNC_PAGES,
4111 * or we hit a bad block or something.
4112 * This might mean we pause for normal IO in the middle of
4113 * a chunk, but that is not a problem was mddev->reshape_position
4114 * can record any location.
4115 *
4116 * If we will want to write to a location that isn't
4117 * yet recorded as 'safe' (i.e. in metadata on disk) then
4118 * we need to flush all reshape requests and update the metadata.
4119 *
4120 * When reshaping forwards (e.g. to more devices), we interpret
4121 * 'safe' as the earliest block which might not have been copied
4122 * down yet. We divide this by previous stripe size and multiply
4123 * by previous stripe length to get lowest device offset that we
4124 * cannot write to yet.
4125 * We interpret 'sector_nr' as an address that we want to write to.
4126 * From this we use last_device_address() to find where we might
4127 * write to, and first_device_address on the 'safe' position.
4128 * If this 'next' write position is after the 'safe' position,
4129 * we must update the metadata to increase the 'safe' position.
4130 *
4131 * When reshaping backwards, we round in the opposite direction
4132 * and perform the reverse test: next write position must not be
4133 * less than current safe position.
4134 *
4135 * In all this the minimum difference in data offsets
4136 * (conf->offset_diff - always positive) allows a bit of slack,
4137 * so next can be after 'safe', but not by more than offset_disk
4138 *
4139 * We need to prepare all the bios here before we start any IO
4140 * to ensure the size we choose is acceptable to all devices.
4141 * The means one for each copy for write-out and an extra one for
4142 * read-in.
4143 * We store the read-in bio in ->master_bio and the others in
4144 * ->devs[x].bio and ->devs[x].repl_bio.
4145 */
4146 struct r10conf *conf = mddev->private;
4147 struct r10bio *r10_bio;
4148 sector_t next, safe, last;
4149 int max_sectors;
4150 int nr_sectors;
4151 int s;
4152 struct md_rdev *rdev;
4153 int need_flush = 0;
4154 struct bio *blist;
4155 struct bio *bio, *read_bio;
4156 int sectors_done = 0;
4157
4158 if (sector_nr == 0) {
4159 /* If restarting in the middle, skip the initial sectors */
4160 if (mddev->reshape_backwards &&
4161 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4162 sector_nr = (raid10_size(mddev, 0, 0)
4163 - conf->reshape_progress);
4164 } else if (!mddev->reshape_backwards &&
4165 conf->reshape_progress > 0)
4166 sector_nr = conf->reshape_progress;
4167 if (sector_nr) {
4168 mddev->curr_resync_completed = sector_nr;
4169 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4170 *skipped = 1;
4171 return sector_nr;
4172 }
4173 }
4174
4175 /* We don't use sector_nr to track where we are up to
4176 * as that doesn't work well for ->reshape_backwards.
4177 * So just use ->reshape_progress.
4178 */
4179 if (mddev->reshape_backwards) {
4180 /* 'next' is the earliest device address that we might
4181 * write to for this chunk in the new layout
4182 */
4183 next = first_dev_address(conf->reshape_progress - 1,
4184 &conf->geo);
4185
4186 /* 'safe' is the last device address that we might read from
4187 * in the old layout after a restart
4188 */
4189 safe = last_dev_address(conf->reshape_safe - 1,
4190 &conf->prev);
4191
4192 if (next + conf->offset_diff < safe)
4193 need_flush = 1;
4194
4195 last = conf->reshape_progress - 1;
4196 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4197 & conf->prev.chunk_mask);
4198 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4199 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4200 } else {
4201 /* 'next' is after the last device address that we
4202 * might write to for this chunk in the new layout
4203 */
4204 next = last_dev_address(conf->reshape_progress, &conf->geo);
4205
4206 /* 'safe' is the earliest device address that we might
4207 * read from in the old layout after a restart
4208 */
4209 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4210
4211 /* Need to update metadata if 'next' might be beyond 'safe'
4212 * as that would possibly corrupt data
4213 */
4214 if (next > safe + conf->offset_diff)
4215 need_flush = 1;
4216
4217 sector_nr = conf->reshape_progress;
4218 last = sector_nr | (conf->geo.chunk_mask
4219 & conf->prev.chunk_mask);
4220
4221 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4222 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4223 }
4224
4225 if (need_flush ||
4226 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4227 /* Need to update reshape_position in metadata */
4228 wait_barrier(conf);
4229 mddev->reshape_position = conf->reshape_progress;
4230 if (mddev->reshape_backwards)
4231 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4232 - conf->reshape_progress;
4233 else
4234 mddev->curr_resync_completed = conf->reshape_progress;
4235 conf->reshape_checkpoint = jiffies;
4236 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4237 md_wakeup_thread(mddev->thread);
4238 wait_event(mddev->sb_wait, mddev->flags == 0 ||
4239 kthread_should_stop());
4240 conf->reshape_safe = mddev->reshape_position;
4241 allow_barrier(conf);
4242 }
4243
4244read_more:
4245 /* Now schedule reads for blocks from sector_nr to last */
4246 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
4247 raise_barrier(conf, sectors_done != 0);
4248 atomic_set(&r10_bio->remaining, 0);
4249 r10_bio->mddev = mddev;
4250 r10_bio->sector = sector_nr;
4251 set_bit(R10BIO_IsReshape, &r10_bio->state);
4252 r10_bio->sectors = last - sector_nr + 1;
4253 rdev = read_balance(conf, r10_bio, &max_sectors);
4254 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4255
4256 if (!rdev) {
4257 /* Cannot read from here, so need to record bad blocks
4258 * on all the target devices.
4259 */
4260 // FIXME
4261 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4262 return sectors_done;
4263 }
4264
4265 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4266
4267 read_bio->bi_bdev = rdev->bdev;
4268 read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4269 + rdev->data_offset);
4270 read_bio->bi_private = r10_bio;
4271 read_bio->bi_end_io = end_sync_read;
4272 read_bio->bi_rw = READ;
4273 read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
4274 read_bio->bi_flags |= 1 << BIO_UPTODATE;
4275 read_bio->bi_vcnt = 0;
4276 read_bio->bi_idx = 0;
4277 read_bio->bi_size = 0;
4278 r10_bio->master_bio = read_bio;
4279 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4280
4281 /* Now find the locations in the new layout */
4282 __raid10_find_phys(&conf->geo, r10_bio);
4283
4284 blist = read_bio;
4285 read_bio->bi_next = NULL;
4286
4287 for (s = 0; s < conf->copies*2; s++) {
4288 struct bio *b;
4289 int d = r10_bio->devs[s/2].devnum;
4290 struct md_rdev *rdev2;
4291 if (s&1) {
4292 rdev2 = conf->mirrors[d].replacement;
4293 b = r10_bio->devs[s/2].repl_bio;
4294 } else {
4295 rdev2 = conf->mirrors[d].rdev;
4296 b = r10_bio->devs[s/2].bio;
4297 }
4298 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4299 continue;
4300 b->bi_bdev = rdev2->bdev;
4301 b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
4302 b->bi_private = r10_bio;
4303 b->bi_end_io = end_reshape_write;
4304 b->bi_rw = WRITE;
4305 b->bi_flags &= ~(BIO_POOL_MASK - 1);
4306 b->bi_flags |= 1 << BIO_UPTODATE;
4307 b->bi_next = blist;
4308 b->bi_vcnt = 0;
4309 b->bi_idx = 0;
4310 b->bi_size = 0;
4311 blist = b;
4312 }
4313
4314 /* Now add as many pages as possible to all of these bios. */
4315
4316 nr_sectors = 0;
4317 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4318 struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
4319 int len = (max_sectors - s) << 9;
4320 if (len > PAGE_SIZE)
4321 len = PAGE_SIZE;
4322 for (bio = blist; bio ; bio = bio->bi_next) {
4323 struct bio *bio2;
4324 if (bio_add_page(bio, page, len, 0))
4325 continue;
4326
4327 /* Didn't fit, must stop */
4328 for (bio2 = blist;
4329 bio2 && bio2 != bio;
4330 bio2 = bio2->bi_next) {
4331 /* Remove last page from this bio */
4332 bio2->bi_vcnt--;
4333 bio2->bi_size -= len;
4334 bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
4335 }
4336 goto bio_full;
4337 }
4338 sector_nr += len >> 9;
4339 nr_sectors += len >> 9;
4340 }
4341bio_full:
4342 r10_bio->sectors = nr_sectors;
4343
4344 /* Now submit the read */
4345 md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
4346 atomic_inc(&r10_bio->remaining);
4347 read_bio->bi_next = NULL;
4348 generic_make_request(read_bio);
4349 sector_nr += nr_sectors;
4350 sectors_done += nr_sectors;
4351 if (sector_nr <= last)
4352 goto read_more;
4353
4354 /* Now that we have done the whole section we can
4355 * update reshape_progress
4356 */
4357 if (mddev->reshape_backwards)
4358 conf->reshape_progress -= sectors_done;
4359 else
4360 conf->reshape_progress += sectors_done;
4361
4362 return sectors_done;
4363}
4364
4365static void end_reshape_request(struct r10bio *r10_bio);
4366static int handle_reshape_read_error(struct mddev *mddev,
4367 struct r10bio *r10_bio);
4368static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4369{
4370 /* Reshape read completed. Hopefully we have a block
4371 * to write out.
4372 * If we got a read error then we do sync 1-page reads from
4373 * elsewhere until we find the data - or give up.
4374 */
4375 struct r10conf *conf = mddev->private;
4376 int s;
4377
4378 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4379 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4380 /* Reshape has been aborted */
4381 md_done_sync(mddev, r10_bio->sectors, 0);
4382 return;
4383 }
4384
4385 /* We definitely have the data in the pages, schedule the
4386 * writes.
4387 */
4388 atomic_set(&r10_bio->remaining, 1);
4389 for (s = 0; s < conf->copies*2; s++) {
4390 struct bio *b;
4391 int d = r10_bio->devs[s/2].devnum;
4392 struct md_rdev *rdev;
4393 if (s&1) {
4394 rdev = conf->mirrors[d].replacement;
4395 b = r10_bio->devs[s/2].repl_bio;
4396 } else {
4397 rdev = conf->mirrors[d].rdev;
4398 b = r10_bio->devs[s/2].bio;
4399 }
4400 if (!rdev || test_bit(Faulty, &rdev->flags))
4401 continue;
4402 atomic_inc(&rdev->nr_pending);
4403 md_sync_acct(b->bi_bdev, r10_bio->sectors);
4404 atomic_inc(&r10_bio->remaining);
4405 b->bi_next = NULL;
4406 generic_make_request(b);
4407 }
4408 end_reshape_request(r10_bio);
4409}
4410
4411static void end_reshape(struct r10conf *conf)
4412{
4413 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4414 return;
4415
4416 spin_lock_irq(&conf->device_lock);
4417 conf->prev = conf->geo;
4418 md_finish_reshape(conf->mddev);
4419 smp_wmb();
4420 conf->reshape_progress = MaxSector;
4421 spin_unlock_irq(&conf->device_lock);
4422
4423 /* read-ahead size must cover two whole stripes, which is
4424 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4425 */
4426 if (conf->mddev->queue) {
4427 int stripe = conf->geo.raid_disks *
4428 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4429 stripe /= conf->geo.near_copies;
4430 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4431 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4432 }
4433 conf->fullsync = 0;
4434}
4435
4436
4437static int handle_reshape_read_error(struct mddev *mddev,
4438 struct r10bio *r10_bio)
4439{
4440 /* Use sync reads to get the blocks from somewhere else */
4441 int sectors = r10_bio->sectors;
3ea7daa5 4442 struct r10conf *conf = mddev->private;
e0ee7785
N
4443 struct {
4444 struct r10bio r10_bio;
4445 struct r10dev devs[conf->copies];
4446 } on_stack;
4447 struct r10bio *r10b = &on_stack.r10_bio;
3ea7daa5
N
4448 int slot = 0;
4449 int idx = 0;
4450 struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
4451
e0ee7785
N
4452 r10b->sector = r10_bio->sector;
4453 __raid10_find_phys(&conf->prev, r10b);
3ea7daa5
N
4454
4455 while (sectors) {
4456 int s = sectors;
4457 int success = 0;
4458 int first_slot = slot;
4459
4460 if (s > (PAGE_SIZE >> 9))
4461 s = PAGE_SIZE >> 9;
4462
4463 while (!success) {
e0ee7785 4464 int d = r10b->devs[slot].devnum;
3ea7daa5
N
4465 struct md_rdev *rdev = conf->mirrors[d].rdev;
4466 sector_t addr;
4467 if (rdev == NULL ||
4468 test_bit(Faulty, &rdev->flags) ||
4469 !test_bit(In_sync, &rdev->flags))
4470 goto failed;
4471
e0ee7785 4472 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
3ea7daa5
N
4473 success = sync_page_io(rdev,
4474 addr,
4475 s << 9,
4476 bvec[idx].bv_page,
4477 READ, false);
4478 if (success)
4479 break;
4480 failed:
4481 slot++;
4482 if (slot >= conf->copies)
4483 slot = 0;
4484 if (slot == first_slot)
4485 break;
4486 }
4487 if (!success) {
4488 /* couldn't read this block, must give up */
4489 set_bit(MD_RECOVERY_INTR,
4490 &mddev->recovery);
4491 return -EIO;
4492 }
4493 sectors -= s;
4494 idx++;
4495 }
4496 return 0;
4497}
4498
4499static void end_reshape_write(struct bio *bio, int error)
4500{
4501 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4502 struct r10bio *r10_bio = bio->bi_private;
4503 struct mddev *mddev = r10_bio->mddev;
4504 struct r10conf *conf = mddev->private;
4505 int d;
4506 int slot;
4507 int repl;
4508 struct md_rdev *rdev = NULL;
4509
4510 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4511 if (repl)
4512 rdev = conf->mirrors[d].replacement;
4513 if (!rdev) {
4514 smp_mb();
4515 rdev = conf->mirrors[d].rdev;
4516 }
4517
4518 if (!uptodate) {
4519 /* FIXME should record badblock */
4520 md_error(mddev, rdev);
4521 }
4522
4523 rdev_dec_pending(rdev, mddev);
4524 end_reshape_request(r10_bio);
4525}
4526
4527static void end_reshape_request(struct r10bio *r10_bio)
4528{
4529 if (!atomic_dec_and_test(&r10_bio->remaining))
4530 return;
4531 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4532 bio_put(r10_bio->master_bio);
4533 put_buf(r10_bio);
4534}
4535
4536static void raid10_finish_reshape(struct mddev *mddev)
4537{
4538 struct r10conf *conf = mddev->private;
4539
4540 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4541 return;
4542
4543 if (mddev->delta_disks > 0) {
4544 sector_t size = raid10_size(mddev, 0, 0);
4545 md_set_array_sectors(mddev, size);
4546 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4547 mddev->recovery_cp = mddev->resync_max_sectors;
4548 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4549 }
4550 mddev->resync_max_sectors = size;
4551 set_capacity(mddev->gendisk, mddev->array_sectors);
4552 revalidate_disk(mddev->gendisk);
63aced61
N
4553 } else {
4554 int d;
4555 for (d = conf->geo.raid_disks ;
4556 d < conf->geo.raid_disks - mddev->delta_disks;
4557 d++) {
4558 struct md_rdev *rdev = conf->mirrors[d].rdev;
4559 if (rdev)
4560 clear_bit(In_sync, &rdev->flags);
4561 rdev = conf->mirrors[d].replacement;
4562 if (rdev)
4563 clear_bit(In_sync, &rdev->flags);
4564 }
3ea7daa5
N
4565 }
4566 mddev->layout = mddev->new_layout;
4567 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4568 mddev->reshape_position = MaxSector;
4569 mddev->delta_disks = 0;
4570 mddev->reshape_backwards = 0;
4571}
4572
84fc4b56 4573static struct md_personality raid10_personality =
1da177e4
LT
4574{
4575 .name = "raid10",
2604b703 4576 .level = 10,
1da177e4
LT
4577 .owner = THIS_MODULE,
4578 .make_request = make_request,
4579 .run = run,
4580 .stop = stop,
4581 .status = status,
4582 .error_handler = error,
4583 .hot_add_disk = raid10_add_disk,
4584 .hot_remove_disk= raid10_remove_disk,
4585 .spare_active = raid10_spare_active,
4586 .sync_request = sync_request,
6cce3b23 4587 .quiesce = raid10_quiesce,
80c3a6ce 4588 .size = raid10_size,
006a09a0 4589 .resize = raid10_resize,
dab8b292 4590 .takeover = raid10_takeover,
3ea7daa5
N
4591 .check_reshape = raid10_check_reshape,
4592 .start_reshape = raid10_start_reshape,
4593 .finish_reshape = raid10_finish_reshape,
1da177e4
LT
4594};
4595
4596static int __init raid_init(void)
4597{
2604b703 4598 return register_md_personality(&raid10_personality);
1da177e4
LT
4599}
4600
4601static void raid_exit(void)
4602{
2604b703 4603 unregister_md_personality(&raid10_personality);
1da177e4
LT
4604}
4605
4606module_init(raid_init);
4607module_exit(raid_exit);
4608MODULE_LICENSE("GPL");
0efb9e61 4609MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
1da177e4 4610MODULE_ALIAS("md-personality-9"); /* RAID10 */
d9d166c2 4611MODULE_ALIAS("md-raid10");
2604b703 4612MODULE_ALIAS("md-level-10");
34db0cd6
N
4613
4614module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);