]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/md/raid10.c
md/bitmap: ensure to load bitmap when creating via sysfs.
[mirror_ubuntu-hirsute-kernel.git] / drivers / md / raid10.c
CommitLineData
1da177e4
LT
1/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
25985edc 8 * Base on code in raid1.c. See raid1.c for further copyright information.
1da177e4
LT
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
5a0e3ad6 21#include <linux/slab.h>
25570727 22#include <linux/delay.h>
bff61975 23#include <linux/blkdev.h>
056075c7 24#include <linux/module.h>
bff61975 25#include <linux/seq_file.h>
8bda470e 26#include <linux/ratelimit.h>
43b2e5d8 27#include "md.h"
ef740c37 28#include "raid10.h"
dab8b292 29#include "raid0.h"
ef740c37 30#include "bitmap.h"
1da177e4
LT
31
32/*
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
35 * chunk_size
36 * raid_disks
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
c93983bf 39 * far_offset (stored in bit 16 of layout )
1da177e4
LT
40 *
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
c93983bf 47 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
1da177e4
LT
48 * drive.
49 * near_copies and far_copies must be at least one, and their product is at most
50 * raid_disks.
c93983bf
N
51 *
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
1da177e4
LT
55 */
56
57/*
58 * Number of guaranteed r10bios in case of extreme VM load:
59 */
60#define NR_RAID10_BIOS 256
61
34db0cd6
N
62/* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
64 * for writeback.
65 */
66static int max_queued_requests = 1024;
67
e879a879
N
68static void allow_barrier(struct r10conf *conf);
69static void lower_barrier(struct r10conf *conf);
fae8cc5e 70static int enough(struct r10conf *conf, int ignore);
0a27ec96 71
dd0fc66f 72static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
1da177e4 73{
e879a879 74 struct r10conf *conf = data;
9f2c9d12 75 int size = offsetof(struct r10bio, devs[conf->copies]);
1da177e4 76
69335ef3
N
77 /* allocate a r10bio with room for raid_disks entries in the
78 * bios array */
7eaceacc 79 return kzalloc(size, gfp_flags);
1da177e4
LT
80}
81
82static void r10bio_pool_free(void *r10_bio, void *data)
83{
84 kfree(r10_bio);
85}
86
0310fa21 87/* Maximum size of each resync request */
1da177e4 88#define RESYNC_BLOCK_SIZE (64*1024)
1da177e4 89#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
0310fa21
N
90/* amount of memory to reserve for resync requests */
91#define RESYNC_WINDOW (1024*1024)
92/* maximum number of concurrent requests, memory permitting */
93#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
1da177e4
LT
94
95/*
96 * When performing a resync, we need to read and compare, so
97 * we need as many pages are there are copies.
98 * When performing a recovery, we need 2 bios, one for read,
99 * one for write (we recover only one drive per r10buf)
100 *
101 */
dd0fc66f 102static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
1da177e4 103{
e879a879 104 struct r10conf *conf = data;
1da177e4 105 struct page *page;
9f2c9d12 106 struct r10bio *r10_bio;
1da177e4
LT
107 struct bio *bio;
108 int i, j;
109 int nalloc;
110
111 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
7eaceacc 112 if (!r10_bio)
1da177e4 113 return NULL;
1da177e4
LT
114
115 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
116 nalloc = conf->copies; /* resync */
117 else
118 nalloc = 2; /* recovery */
119
120 /*
121 * Allocate bios.
122 */
123 for (j = nalloc ; j-- ; ) {
6746557f 124 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
1da177e4
LT
125 if (!bio)
126 goto out_free_bio;
127 r10_bio->devs[j].bio = bio;
69335ef3
N
128 if (!conf->have_replacement)
129 continue;
130 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
131 if (!bio)
132 goto out_free_bio;
133 r10_bio->devs[j].repl_bio = bio;
1da177e4
LT
134 }
135 /*
136 * Allocate RESYNC_PAGES data pages and attach them
137 * where needed.
138 */
139 for (j = 0 ; j < nalloc; j++) {
69335ef3 140 struct bio *rbio = r10_bio->devs[j].repl_bio;
1da177e4
LT
141 bio = r10_bio->devs[j].bio;
142 for (i = 0; i < RESYNC_PAGES; i++) {
c65060ad
NK
143 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
144 &conf->mddev->recovery)) {
145 /* we can share bv_page's during recovery */
146 struct bio *rbio = r10_bio->devs[0].bio;
147 page = rbio->bi_io_vec[i].bv_page;
148 get_page(page);
149 } else
150 page = alloc_page(gfp_flags);
1da177e4
LT
151 if (unlikely(!page))
152 goto out_free_pages;
153
154 bio->bi_io_vec[i].bv_page = page;
69335ef3
N
155 if (rbio)
156 rbio->bi_io_vec[i].bv_page = page;
1da177e4
LT
157 }
158 }
159
160 return r10_bio;
161
162out_free_pages:
163 for ( ; i > 0 ; i--)
1345b1d8 164 safe_put_page(bio->bi_io_vec[i-1].bv_page);
1da177e4
LT
165 while (j--)
166 for (i = 0; i < RESYNC_PAGES ; i++)
1345b1d8 167 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
1da177e4
LT
168 j = -1;
169out_free_bio:
69335ef3 170 while (++j < nalloc) {
1da177e4 171 bio_put(r10_bio->devs[j].bio);
69335ef3
N
172 if (r10_bio->devs[j].repl_bio)
173 bio_put(r10_bio->devs[j].repl_bio);
174 }
1da177e4
LT
175 r10bio_pool_free(r10_bio, conf);
176 return NULL;
177}
178
179static void r10buf_pool_free(void *__r10_bio, void *data)
180{
181 int i;
e879a879 182 struct r10conf *conf = data;
9f2c9d12 183 struct r10bio *r10bio = __r10_bio;
1da177e4
LT
184 int j;
185
186 for (j=0; j < conf->copies; j++) {
187 struct bio *bio = r10bio->devs[j].bio;
188 if (bio) {
189 for (i = 0; i < RESYNC_PAGES; i++) {
1345b1d8 190 safe_put_page(bio->bi_io_vec[i].bv_page);
1da177e4
LT
191 bio->bi_io_vec[i].bv_page = NULL;
192 }
193 bio_put(bio);
194 }
69335ef3
N
195 bio = r10bio->devs[j].repl_bio;
196 if (bio)
197 bio_put(bio);
1da177e4
LT
198 }
199 r10bio_pool_free(r10bio, conf);
200}
201
e879a879 202static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
1da177e4
LT
203{
204 int i;
205
206 for (i = 0; i < conf->copies; i++) {
207 struct bio **bio = & r10_bio->devs[i].bio;
749c55e9 208 if (!BIO_SPECIAL(*bio))
1da177e4
LT
209 bio_put(*bio);
210 *bio = NULL;
69335ef3
N
211 bio = &r10_bio->devs[i].repl_bio;
212 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
213 bio_put(*bio);
214 *bio = NULL;
1da177e4
LT
215 }
216}
217
9f2c9d12 218static void free_r10bio(struct r10bio *r10_bio)
1da177e4 219{
e879a879 220 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 221
1da177e4
LT
222 put_all_bios(conf, r10_bio);
223 mempool_free(r10_bio, conf->r10bio_pool);
224}
225
9f2c9d12 226static void put_buf(struct r10bio *r10_bio)
1da177e4 227{
e879a879 228 struct r10conf *conf = r10_bio->mddev->private;
1da177e4
LT
229
230 mempool_free(r10_bio, conf->r10buf_pool);
231
0a27ec96 232 lower_barrier(conf);
1da177e4
LT
233}
234
9f2c9d12 235static void reschedule_retry(struct r10bio *r10_bio)
1da177e4
LT
236{
237 unsigned long flags;
fd01b88c 238 struct mddev *mddev = r10_bio->mddev;
e879a879 239 struct r10conf *conf = mddev->private;
1da177e4
LT
240
241 spin_lock_irqsave(&conf->device_lock, flags);
242 list_add(&r10_bio->retry_list, &conf->retry_list);
4443ae10 243 conf->nr_queued ++;
1da177e4
LT
244 spin_unlock_irqrestore(&conf->device_lock, flags);
245
388667be
AJ
246 /* wake up frozen array... */
247 wake_up(&conf->wait_barrier);
248
1da177e4
LT
249 md_wakeup_thread(mddev->thread);
250}
251
252/*
253 * raid_end_bio_io() is called when we have finished servicing a mirrored
254 * operation and are ready to return a success/failure code to the buffer
255 * cache layer.
256 */
9f2c9d12 257static void raid_end_bio_io(struct r10bio *r10_bio)
1da177e4
LT
258{
259 struct bio *bio = r10_bio->master_bio;
856e08e2 260 int done;
e879a879 261 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 262
856e08e2
N
263 if (bio->bi_phys_segments) {
264 unsigned long flags;
265 spin_lock_irqsave(&conf->device_lock, flags);
266 bio->bi_phys_segments--;
267 done = (bio->bi_phys_segments == 0);
268 spin_unlock_irqrestore(&conf->device_lock, flags);
269 } else
270 done = 1;
271 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
272 clear_bit(BIO_UPTODATE, &bio->bi_flags);
273 if (done) {
274 bio_endio(bio, 0);
275 /*
276 * Wake up any possible resync thread that waits for the device
277 * to go idle.
278 */
279 allow_barrier(conf);
280 }
1da177e4
LT
281 free_r10bio(r10_bio);
282}
283
284/*
285 * Update disk head position estimator based on IRQ completion info.
286 */
9f2c9d12 287static inline void update_head_pos(int slot, struct r10bio *r10_bio)
1da177e4 288{
e879a879 289 struct r10conf *conf = r10_bio->mddev->private;
1da177e4
LT
290
291 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
292 r10_bio->devs[slot].addr + (r10_bio->sectors);
293}
294
778ca018
NK
295/*
296 * Find the disk number which triggered given bio
297 */
e879a879 298static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
69335ef3 299 struct bio *bio, int *slotp, int *replp)
778ca018
NK
300{
301 int slot;
69335ef3 302 int repl = 0;
778ca018 303
69335ef3 304 for (slot = 0; slot < conf->copies; slot++) {
778ca018
NK
305 if (r10_bio->devs[slot].bio == bio)
306 break;
69335ef3
N
307 if (r10_bio->devs[slot].repl_bio == bio) {
308 repl = 1;
309 break;
310 }
311 }
778ca018
NK
312
313 BUG_ON(slot == conf->copies);
314 update_head_pos(slot, r10_bio);
315
749c55e9
N
316 if (slotp)
317 *slotp = slot;
69335ef3
N
318 if (replp)
319 *replp = repl;
778ca018
NK
320 return r10_bio->devs[slot].devnum;
321}
322
6712ecf8 323static void raid10_end_read_request(struct bio *bio, int error)
1da177e4
LT
324{
325 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
9f2c9d12 326 struct r10bio *r10_bio = bio->bi_private;
1da177e4 327 int slot, dev;
abbf098e 328 struct md_rdev *rdev;
e879a879 329 struct r10conf *conf = r10_bio->mddev->private;
1da177e4 330
1da177e4
LT
331
332 slot = r10_bio->read_slot;
333 dev = r10_bio->devs[slot].devnum;
abbf098e 334 rdev = r10_bio->devs[slot].rdev;
1da177e4
LT
335 /*
336 * this branch is our 'one mirror IO has finished' event handler:
337 */
4443ae10
N
338 update_head_pos(slot, r10_bio);
339
340 if (uptodate) {
1da177e4
LT
341 /*
342 * Set R10BIO_Uptodate in our master bio, so that
343 * we will return a good error code to the higher
344 * levels even if IO on some other mirrored buffer fails.
345 *
346 * The 'master' represents the composite IO operation to
347 * user-side. So if something waits for IO, then it will
348 * wait for the 'master' bio.
349 */
350 set_bit(R10BIO_Uptodate, &r10_bio->state);
fae8cc5e
N
351 } else {
352 /* If all other devices that store this block have
353 * failed, we want to return the error upwards rather
354 * than fail the last device. Here we redefine
355 * "uptodate" to mean "Don't want to retry"
356 */
357 unsigned long flags;
358 spin_lock_irqsave(&conf->device_lock, flags);
359 if (!enough(conf, rdev->raid_disk))
360 uptodate = 1;
361 spin_unlock_irqrestore(&conf->device_lock, flags);
362 }
363 if (uptodate) {
1da177e4 364 raid_end_bio_io(r10_bio);
abbf098e 365 rdev_dec_pending(rdev, conf->mddev);
4443ae10 366 } else {
1da177e4 367 /*
7c4e06ff 368 * oops, read error - keep the refcount on the rdev
1da177e4
LT
369 */
370 char b[BDEVNAME_SIZE];
8bda470e
CD
371 printk_ratelimited(KERN_ERR
372 "md/raid10:%s: %s: rescheduling sector %llu\n",
373 mdname(conf->mddev),
abbf098e 374 bdevname(rdev->bdev, b),
8bda470e 375 (unsigned long long)r10_bio->sector);
856e08e2 376 set_bit(R10BIO_ReadError, &r10_bio->state);
1da177e4
LT
377 reschedule_retry(r10_bio);
378 }
1da177e4
LT
379}
380
9f2c9d12 381static void close_write(struct r10bio *r10_bio)
bd870a16
N
382{
383 /* clear the bitmap if all writes complete successfully */
384 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
385 r10_bio->sectors,
386 !test_bit(R10BIO_Degraded, &r10_bio->state),
387 0);
388 md_write_end(r10_bio->mddev);
389}
390
9f2c9d12 391static void one_write_done(struct r10bio *r10_bio)
19d5f834
N
392{
393 if (atomic_dec_and_test(&r10_bio->remaining)) {
394 if (test_bit(R10BIO_WriteError, &r10_bio->state))
395 reschedule_retry(r10_bio);
396 else {
397 close_write(r10_bio);
398 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
399 reschedule_retry(r10_bio);
400 else
401 raid_end_bio_io(r10_bio);
402 }
403 }
404}
405
6712ecf8 406static void raid10_end_write_request(struct bio *bio, int error)
1da177e4
LT
407{
408 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
9f2c9d12 409 struct r10bio *r10_bio = bio->bi_private;
778ca018 410 int dev;
749c55e9 411 int dec_rdev = 1;
e879a879 412 struct r10conf *conf = r10_bio->mddev->private;
475b0321 413 int slot, repl;
4ca40c2c 414 struct md_rdev *rdev = NULL;
1da177e4 415
475b0321 416 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1da177e4 417
475b0321
N
418 if (repl)
419 rdev = conf->mirrors[dev].replacement;
4ca40c2c
N
420 if (!rdev) {
421 smp_rmb();
422 repl = 0;
475b0321 423 rdev = conf->mirrors[dev].rdev;
4ca40c2c 424 }
1da177e4
LT
425 /*
426 * this branch is our 'one mirror IO has finished' event handler:
427 */
6cce3b23 428 if (!uptodate) {
475b0321
N
429 if (repl)
430 /* Never record new bad blocks to replacement,
431 * just fail it.
432 */
433 md_error(rdev->mddev, rdev);
434 else {
435 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
436 if (!test_and_set_bit(WantReplacement, &rdev->flags))
437 set_bit(MD_RECOVERY_NEEDED,
438 &rdev->mddev->recovery);
475b0321
N
439 set_bit(R10BIO_WriteError, &r10_bio->state);
440 dec_rdev = 0;
441 }
749c55e9 442 } else {
1da177e4
LT
443 /*
444 * Set R10BIO_Uptodate in our master bio, so that
445 * we will return a good error code for to the higher
446 * levels even if IO on some other mirrored buffer fails.
447 *
448 * The 'master' represents the composite IO operation to
449 * user-side. So if something waits for IO, then it will
450 * wait for the 'master' bio.
451 */
749c55e9
N
452 sector_t first_bad;
453 int bad_sectors;
454
1da177e4
LT
455 set_bit(R10BIO_Uptodate, &r10_bio->state);
456
749c55e9 457 /* Maybe we can clear some bad blocks. */
475b0321 458 if (is_badblock(rdev,
749c55e9
N
459 r10_bio->devs[slot].addr,
460 r10_bio->sectors,
461 &first_bad, &bad_sectors)) {
462 bio_put(bio);
475b0321
N
463 if (repl)
464 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
465 else
466 r10_bio->devs[slot].bio = IO_MADE_GOOD;
749c55e9
N
467 dec_rdev = 0;
468 set_bit(R10BIO_MadeGood, &r10_bio->state);
469 }
470 }
471
1da177e4
LT
472 /*
473 *
474 * Let's see if all mirrored write operations have finished
475 * already.
476 */
19d5f834 477 one_write_done(r10_bio);
749c55e9
N
478 if (dec_rdev)
479 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
1da177e4
LT
480}
481
1da177e4
LT
482/*
483 * RAID10 layout manager
25985edc 484 * As well as the chunksize and raid_disks count, there are two
1da177e4
LT
485 * parameters: near_copies and far_copies.
486 * near_copies * far_copies must be <= raid_disks.
487 * Normally one of these will be 1.
488 * If both are 1, we get raid0.
489 * If near_copies == raid_disks, we get raid1.
490 *
25985edc 491 * Chunks are laid out in raid0 style with near_copies copies of the
1da177e4
LT
492 * first chunk, followed by near_copies copies of the next chunk and
493 * so on.
494 * If far_copies > 1, then after 1/far_copies of the array has been assigned
495 * as described above, we start again with a device offset of near_copies.
496 * So we effectively have another copy of the whole array further down all
497 * the drives, but with blocks on different drives.
498 * With this layout, and block is never stored twice on the one device.
499 *
500 * raid10_find_phys finds the sector offset of a given virtual sector
c93983bf 501 * on each device that it is on.
1da177e4
LT
502 *
503 * raid10_find_virt does the reverse mapping, from a device and a
504 * sector offset to a virtual address
505 */
506
e879a879 507static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
1da177e4
LT
508{
509 int n,f;
510 sector_t sector;
511 sector_t chunk;
512 sector_t stripe;
513 int dev;
514
515 int slot = 0;
516
517 /* now calculate first sector/dev */
518 chunk = r10bio->sector >> conf->chunk_shift;
519 sector = r10bio->sector & conf->chunk_mask;
520
521 chunk *= conf->near_copies;
522 stripe = chunk;
523 dev = sector_div(stripe, conf->raid_disks);
c93983bf
N
524 if (conf->far_offset)
525 stripe *= conf->far_copies;
1da177e4
LT
526
527 sector += stripe << conf->chunk_shift;
528
529 /* and calculate all the others */
530 for (n=0; n < conf->near_copies; n++) {
531 int d = dev;
532 sector_t s = sector;
533 r10bio->devs[slot].addr = sector;
534 r10bio->devs[slot].devnum = d;
535 slot++;
536
537 for (f = 1; f < conf->far_copies; f++) {
538 d += conf->near_copies;
539 if (d >= conf->raid_disks)
540 d -= conf->raid_disks;
541 s += conf->stride;
542 r10bio->devs[slot].devnum = d;
543 r10bio->devs[slot].addr = s;
544 slot++;
545 }
546 dev++;
547 if (dev >= conf->raid_disks) {
548 dev = 0;
549 sector += (conf->chunk_mask + 1);
550 }
551 }
552 BUG_ON(slot != conf->copies);
553}
554
e879a879 555static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
1da177e4
LT
556{
557 sector_t offset, chunk, vchunk;
558
1da177e4 559 offset = sector & conf->chunk_mask;
c93983bf
N
560 if (conf->far_offset) {
561 int fc;
562 chunk = sector >> conf->chunk_shift;
563 fc = sector_div(chunk, conf->far_copies);
564 dev -= fc * conf->near_copies;
565 if (dev < 0)
566 dev += conf->raid_disks;
567 } else {
64a742bc 568 while (sector >= conf->stride) {
c93983bf
N
569 sector -= conf->stride;
570 if (dev < conf->near_copies)
571 dev += conf->raid_disks - conf->near_copies;
572 else
573 dev -= conf->near_copies;
574 }
575 chunk = sector >> conf->chunk_shift;
576 }
1da177e4
LT
577 vchunk = chunk * conf->raid_disks + dev;
578 sector_div(vchunk, conf->near_copies);
579 return (vchunk << conf->chunk_shift) + offset;
580}
581
582/**
583 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
584 * @q: request queue
cc371e66 585 * @bvm: properties of new bio
1da177e4
LT
586 * @biovec: the request that could be merged to it.
587 *
588 * Return amount of bytes we can accept at this offset
589 * If near_copies == raid_disk, there are no striping issues,
590 * but in that case, the function isn't called at all.
591 */
cc371e66
AK
592static int raid10_mergeable_bvec(struct request_queue *q,
593 struct bvec_merge_data *bvm,
594 struct bio_vec *biovec)
1da177e4 595{
fd01b88c 596 struct mddev *mddev = q->queuedata;
cc371e66 597 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
1da177e4 598 int max;
9d8f0363 599 unsigned int chunk_sectors = mddev->chunk_sectors;
cc371e66 600 unsigned int bio_sectors = bvm->bi_size >> 9;
1da177e4
LT
601
602 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
603 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
cc371e66
AK
604 if (max <= biovec->bv_len && bio_sectors == 0)
605 return biovec->bv_len;
1da177e4
LT
606 else
607 return max;
608}
609
610/*
611 * This routine returns the disk from which the requested read should
612 * be done. There is a per-array 'next expected sequential IO' sector
613 * number - if this matches on the next IO then we use the last disk.
614 * There is also a per-disk 'last know head position' sector that is
615 * maintained from IRQ contexts, both the normal and the resync IO
616 * completion handlers update this position correctly. If there is no
617 * perfect sequential match then we pick the disk whose head is closest.
618 *
619 * If there are 2 mirrors in the same 2 devices, performance degrades
620 * because position is mirror, not device based.
621 *
622 * The rdev for the device selected will have nr_pending incremented.
623 */
624
625/*
626 * FIXME: possibly should rethink readbalancing and do it differently
627 * depending on near_copies / far_copies geometry.
628 */
96c3fd1f
N
629static struct md_rdev *read_balance(struct r10conf *conf,
630 struct r10bio *r10_bio,
631 int *max_sectors)
1da177e4 632{
af3a2cd6 633 const sector_t this_sector = r10_bio->sector;
56d99121 634 int disk, slot;
856e08e2
N
635 int sectors = r10_bio->sectors;
636 int best_good_sectors;
56d99121 637 sector_t new_distance, best_dist;
abbf098e 638 struct md_rdev *rdev, *best_rdev;
56d99121
N
639 int do_balance;
640 int best_slot;
1da177e4
LT
641
642 raid10_find_phys(conf, r10_bio);
643 rcu_read_lock();
56d99121 644retry:
856e08e2 645 sectors = r10_bio->sectors;
56d99121 646 best_slot = -1;
abbf098e 647 best_rdev = NULL;
56d99121 648 best_dist = MaxSector;
856e08e2 649 best_good_sectors = 0;
56d99121 650 do_balance = 1;
1da177e4
LT
651 /*
652 * Check if we can balance. We can balance on the whole
6cce3b23
N
653 * device if no resync is going on (recovery is ok), or below
654 * the resync window. We take the first readable disk when
655 * above the resync window.
1da177e4
LT
656 */
657 if (conf->mddev->recovery_cp < MaxSector
56d99121
N
658 && (this_sector + sectors >= conf->next_resync))
659 do_balance = 0;
1da177e4 660
56d99121 661 for (slot = 0; slot < conf->copies ; slot++) {
856e08e2
N
662 sector_t first_bad;
663 int bad_sectors;
664 sector_t dev_sector;
665
56d99121
N
666 if (r10_bio->devs[slot].bio == IO_BLOCKED)
667 continue;
1da177e4 668 disk = r10_bio->devs[slot].devnum;
abbf098e
N
669 rdev = rcu_dereference(conf->mirrors[disk].replacement);
670 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
671 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
672 rdev = rcu_dereference(conf->mirrors[disk].rdev);
56d99121 673 if (rdev == NULL)
1da177e4 674 continue;
abbf098e
N
675 if (test_bit(Faulty, &rdev->flags))
676 continue;
677 if (!test_bit(In_sync, &rdev->flags) &&
678 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
56d99121
N
679 continue;
680
856e08e2
N
681 dev_sector = r10_bio->devs[slot].addr;
682 if (is_badblock(rdev, dev_sector, sectors,
683 &first_bad, &bad_sectors)) {
684 if (best_dist < MaxSector)
685 /* Already have a better slot */
686 continue;
687 if (first_bad <= dev_sector) {
688 /* Cannot read here. If this is the
689 * 'primary' device, then we must not read
690 * beyond 'bad_sectors' from another device.
691 */
692 bad_sectors -= (dev_sector - first_bad);
693 if (!do_balance && sectors > bad_sectors)
694 sectors = bad_sectors;
695 if (best_good_sectors > sectors)
696 best_good_sectors = sectors;
697 } else {
698 sector_t good_sectors =
699 first_bad - dev_sector;
700 if (good_sectors > best_good_sectors) {
701 best_good_sectors = good_sectors;
702 best_slot = slot;
abbf098e 703 best_rdev = rdev;
856e08e2
N
704 }
705 if (!do_balance)
706 /* Must read from here */
707 break;
708 }
709 continue;
710 } else
711 best_good_sectors = sectors;
712
56d99121
N
713 if (!do_balance)
714 break;
1da177e4 715
22dfdf52
N
716 /* This optimisation is debatable, and completely destroys
717 * sequential read speed for 'far copies' arrays. So only
718 * keep it for 'near' arrays, and review those later.
719 */
56d99121 720 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
1da177e4 721 break;
8ed3a195
KS
722
723 /* for far > 1 always use the lowest address */
724 if (conf->far_copies > 1)
56d99121 725 new_distance = r10_bio->devs[slot].addr;
8ed3a195 726 else
56d99121
N
727 new_distance = abs(r10_bio->devs[slot].addr -
728 conf->mirrors[disk].head_position);
729 if (new_distance < best_dist) {
730 best_dist = new_distance;
731 best_slot = slot;
abbf098e 732 best_rdev = rdev;
1da177e4
LT
733 }
734 }
abbf098e 735 if (slot >= conf->copies) {
56d99121 736 slot = best_slot;
abbf098e
N
737 rdev = best_rdev;
738 }
1da177e4 739
56d99121 740 if (slot >= 0) {
56d99121
N
741 atomic_inc(&rdev->nr_pending);
742 if (test_bit(Faulty, &rdev->flags)) {
743 /* Cannot risk returning a device that failed
744 * before we inc'ed nr_pending
745 */
746 rdev_dec_pending(rdev, conf->mddev);
747 goto retry;
748 }
749 r10_bio->read_slot = slot;
750 } else
96c3fd1f 751 rdev = NULL;
1da177e4 752 rcu_read_unlock();
856e08e2 753 *max_sectors = best_good_sectors;
1da177e4 754
96c3fd1f 755 return rdev;
1da177e4
LT
756}
757
0d129228
N
758static int raid10_congested(void *data, int bits)
759{
fd01b88c 760 struct mddev *mddev = data;
e879a879 761 struct r10conf *conf = mddev->private;
0d129228
N
762 int i, ret = 0;
763
34db0cd6
N
764 if ((bits & (1 << BDI_async_congested)) &&
765 conf->pending_count >= max_queued_requests)
766 return 1;
767
3fa841d7
N
768 if (mddev_congested(mddev, bits))
769 return 1;
0d129228 770 rcu_read_lock();
84707f38 771 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
3cb03002 772 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
0d129228 773 if (rdev && !test_bit(Faulty, &rdev->flags)) {
165125e1 774 struct request_queue *q = bdev_get_queue(rdev->bdev);
0d129228
N
775
776 ret |= bdi_congested(&q->backing_dev_info, bits);
777 }
778 }
779 rcu_read_unlock();
780 return ret;
781}
782
e879a879 783static void flush_pending_writes(struct r10conf *conf)
a35e63ef
N
784{
785 /* Any writes that have been queued but are awaiting
786 * bitmap updates get flushed here.
a35e63ef 787 */
a35e63ef
N
788 spin_lock_irq(&conf->device_lock);
789
790 if (conf->pending_bio_list.head) {
791 struct bio *bio;
792 bio = bio_list_get(&conf->pending_bio_list);
34db0cd6 793 conf->pending_count = 0;
a35e63ef
N
794 spin_unlock_irq(&conf->device_lock);
795 /* flush any pending bitmap writes to disk
796 * before proceeding w/ I/O */
797 bitmap_unplug(conf->mddev->bitmap);
34db0cd6 798 wake_up(&conf->wait_barrier);
a35e63ef
N
799
800 while (bio) { /* submit pending writes */
801 struct bio *next = bio->bi_next;
802 bio->bi_next = NULL;
803 generic_make_request(bio);
804 bio = next;
805 }
a35e63ef
N
806 } else
807 spin_unlock_irq(&conf->device_lock);
a35e63ef 808}
7eaceacc 809
0a27ec96
N
810/* Barriers....
811 * Sometimes we need to suspend IO while we do something else,
812 * either some resync/recovery, or reconfigure the array.
813 * To do this we raise a 'barrier'.
814 * The 'barrier' is a counter that can be raised multiple times
815 * to count how many activities are happening which preclude
816 * normal IO.
817 * We can only raise the barrier if there is no pending IO.
818 * i.e. if nr_pending == 0.
819 * We choose only to raise the barrier if no-one is waiting for the
820 * barrier to go down. This means that as soon as an IO request
821 * is ready, no other operations which require a barrier will start
822 * until the IO request has had a chance.
823 *
824 * So: regular IO calls 'wait_barrier'. When that returns there
825 * is no backgroup IO happening, It must arrange to call
826 * allow_barrier when it has finished its IO.
827 * backgroup IO calls must call raise_barrier. Once that returns
828 * there is no normal IO happeing. It must arrange to call
829 * lower_barrier when the particular background IO completes.
1da177e4 830 */
1da177e4 831
e879a879 832static void raise_barrier(struct r10conf *conf, int force)
1da177e4 833{
6cce3b23 834 BUG_ON(force && !conf->barrier);
1da177e4 835 spin_lock_irq(&conf->resync_lock);
0a27ec96 836
6cce3b23
N
837 /* Wait until no block IO is waiting (unless 'force') */
838 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
c3b328ac 839 conf->resync_lock, );
0a27ec96
N
840
841 /* block any new IO from starting */
842 conf->barrier++;
843
c3b328ac 844 /* Now wait for all pending IO to complete */
0a27ec96
N
845 wait_event_lock_irq(conf->wait_barrier,
846 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
c3b328ac 847 conf->resync_lock, );
0a27ec96
N
848
849 spin_unlock_irq(&conf->resync_lock);
850}
851
e879a879 852static void lower_barrier(struct r10conf *conf)
0a27ec96
N
853{
854 unsigned long flags;
855 spin_lock_irqsave(&conf->resync_lock, flags);
856 conf->barrier--;
857 spin_unlock_irqrestore(&conf->resync_lock, flags);
858 wake_up(&conf->wait_barrier);
859}
860
e879a879 861static void wait_barrier(struct r10conf *conf)
0a27ec96
N
862{
863 spin_lock_irq(&conf->resync_lock);
864 if (conf->barrier) {
865 conf->nr_waiting++;
866 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
867 conf->resync_lock,
c3b328ac 868 );
0a27ec96 869 conf->nr_waiting--;
1da177e4 870 }
0a27ec96 871 conf->nr_pending++;
1da177e4
LT
872 spin_unlock_irq(&conf->resync_lock);
873}
874
e879a879 875static void allow_barrier(struct r10conf *conf)
0a27ec96
N
876{
877 unsigned long flags;
878 spin_lock_irqsave(&conf->resync_lock, flags);
879 conf->nr_pending--;
880 spin_unlock_irqrestore(&conf->resync_lock, flags);
881 wake_up(&conf->wait_barrier);
882}
883
e879a879 884static void freeze_array(struct r10conf *conf)
4443ae10
N
885{
886 /* stop syncio and normal IO and wait for everything to
f188593e 887 * go quiet.
4443ae10 888 * We increment barrier and nr_waiting, and then
1c830532
N
889 * wait until nr_pending match nr_queued+1
890 * This is called in the context of one normal IO request
891 * that has failed. Thus any sync request that might be pending
892 * will be blocked by nr_pending, and we need to wait for
893 * pending IO requests to complete or be queued for re-try.
894 * Thus the number queued (nr_queued) plus this request (1)
895 * must match the number of pending IOs (nr_pending) before
896 * we continue.
4443ae10
N
897 */
898 spin_lock_irq(&conf->resync_lock);
899 conf->barrier++;
900 conf->nr_waiting++;
901 wait_event_lock_irq(conf->wait_barrier,
1c830532 902 conf->nr_pending == conf->nr_queued+1,
4443ae10 903 conf->resync_lock,
c3b328ac
N
904 flush_pending_writes(conf));
905
4443ae10
N
906 spin_unlock_irq(&conf->resync_lock);
907}
908
e879a879 909static void unfreeze_array(struct r10conf *conf)
4443ae10
N
910{
911 /* reverse the effect of the freeze */
912 spin_lock_irq(&conf->resync_lock);
913 conf->barrier--;
914 conf->nr_waiting--;
915 wake_up(&conf->wait_barrier);
916 spin_unlock_irq(&conf->resync_lock);
917}
918
b4fdcb02 919static void make_request(struct mddev *mddev, struct bio * bio)
1da177e4 920{
e879a879 921 struct r10conf *conf = mddev->private;
9f2c9d12 922 struct r10bio *r10_bio;
1da177e4
LT
923 struct bio *read_bio;
924 int i;
925 int chunk_sects = conf->chunk_mask + 1;
a362357b 926 const int rw = bio_data_dir(bio);
2c7d46ec 927 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
e9c7469b 928 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
6cce3b23 929 unsigned long flags;
3cb03002 930 struct md_rdev *blocked_rdev;
c3b328ac 931 int plugged;
d4432c23
N
932 int sectors_handled;
933 int max_sectors;
1da177e4 934
e9c7469b
TH
935 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
936 md_flush_request(mddev, bio);
5a7bbad2 937 return;
e5dcdd80
N
938 }
939
1da177e4
LT
940 /* If this request crosses a chunk boundary, we need to
941 * split it. This will only happen for 1 PAGE (or less) requests.
942 */
943 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
944 > chunk_sects &&
945 conf->near_copies < conf->raid_disks)) {
946 struct bio_pair *bp;
947 /* Sanity check -- queue functions should prevent this happening */
948 if (bio->bi_vcnt != 1 ||
949 bio->bi_idx != 0)
950 goto bad_map;
951 /* This is a one page bio that upper layers
952 * refuse to split for us, so we need to split it.
953 */
6feef531 954 bp = bio_split(bio,
1da177e4 955 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
51e9ac77
N
956
957 /* Each of these 'make_request' calls will call 'wait_barrier'.
958 * If the first succeeds but the second blocks due to the resync
959 * thread raising the barrier, we will deadlock because the
960 * IO to the underlying device will be queued in generic_make_request
961 * and will never complete, so will never reduce nr_pending.
962 * So increment nr_waiting here so no new raise_barriers will
963 * succeed, and so the second wait_barrier cannot block.
964 */
965 spin_lock_irq(&conf->resync_lock);
966 conf->nr_waiting++;
967 spin_unlock_irq(&conf->resync_lock);
968
5a7bbad2
CH
969 make_request(mddev, &bp->bio1);
970 make_request(mddev, &bp->bio2);
1da177e4 971
51e9ac77
N
972 spin_lock_irq(&conf->resync_lock);
973 conf->nr_waiting--;
974 wake_up(&conf->wait_barrier);
975 spin_unlock_irq(&conf->resync_lock);
976
1da177e4 977 bio_pair_release(bp);
5a7bbad2 978 return;
1da177e4 979 bad_map:
128595ed
N
980 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
981 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
1da177e4
LT
982 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
983
6712ecf8 984 bio_io_error(bio);
5a7bbad2 985 return;
1da177e4
LT
986 }
987
3d310eb7 988 md_write_start(mddev, bio);
06d91a5f 989
1da177e4
LT
990 /*
991 * Register the new request and wait if the reconstruction
992 * thread has put up a bar for new requests.
993 * Continue immediately if no resync is active currently.
994 */
0a27ec96 995 wait_barrier(conf);
1da177e4 996
1da177e4
LT
997 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
998
999 r10_bio->master_bio = bio;
1000 r10_bio->sectors = bio->bi_size >> 9;
1001
1002 r10_bio->mddev = mddev;
1003 r10_bio->sector = bio->bi_sector;
6cce3b23 1004 r10_bio->state = 0;
1da177e4 1005
856e08e2
N
1006 /* We might need to issue multiple reads to different
1007 * devices if there are bad blocks around, so we keep
1008 * track of the number of reads in bio->bi_phys_segments.
1009 * If this is 0, there is only one r10_bio and no locking
1010 * will be needed when the request completes. If it is
1011 * non-zero, then it is the number of not-completed requests.
1012 */
1013 bio->bi_phys_segments = 0;
1014 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1015
a362357b 1016 if (rw == READ) {
1da177e4
LT
1017 /*
1018 * read balancing logic:
1019 */
96c3fd1f 1020 struct md_rdev *rdev;
856e08e2
N
1021 int slot;
1022
1023read_again:
96c3fd1f
N
1024 rdev = read_balance(conf, r10_bio, &max_sectors);
1025 if (!rdev) {
1da177e4 1026 raid_end_bio_io(r10_bio);
5a7bbad2 1027 return;
1da177e4 1028 }
96c3fd1f 1029 slot = r10_bio->read_slot;
1da177e4 1030
a167f663 1031 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
856e08e2
N
1032 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1033 max_sectors);
1da177e4
LT
1034
1035 r10_bio->devs[slot].bio = read_bio;
abbf098e 1036 r10_bio->devs[slot].rdev = rdev;
1da177e4
LT
1037
1038 read_bio->bi_sector = r10_bio->devs[slot].addr +
96c3fd1f
N
1039 rdev->data_offset;
1040 read_bio->bi_bdev = rdev->bdev;
1da177e4 1041 read_bio->bi_end_io = raid10_end_read_request;
7b6d91da 1042 read_bio->bi_rw = READ | do_sync;
1da177e4
LT
1043 read_bio->bi_private = r10_bio;
1044
856e08e2
N
1045 if (max_sectors < r10_bio->sectors) {
1046 /* Could not read all from this device, so we will
1047 * need another r10_bio.
1048 */
856e08e2
N
1049 sectors_handled = (r10_bio->sectors + max_sectors
1050 - bio->bi_sector);
1051 r10_bio->sectors = max_sectors;
1052 spin_lock_irq(&conf->device_lock);
1053 if (bio->bi_phys_segments == 0)
1054 bio->bi_phys_segments = 2;
1055 else
1056 bio->bi_phys_segments++;
1057 spin_unlock(&conf->device_lock);
1058 /* Cannot call generic_make_request directly
1059 * as that will be queued in __generic_make_request
1060 * and subsequent mempool_alloc might block
1061 * waiting for it. so hand bio over to raid10d.
1062 */
1063 reschedule_retry(r10_bio);
1064
1065 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1066
1067 r10_bio->master_bio = bio;
1068 r10_bio->sectors = ((bio->bi_size >> 9)
1069 - sectors_handled);
1070 r10_bio->state = 0;
1071 r10_bio->mddev = mddev;
1072 r10_bio->sector = bio->bi_sector + sectors_handled;
1073 goto read_again;
1074 } else
1075 generic_make_request(read_bio);
5a7bbad2 1076 return;
1da177e4
LT
1077 }
1078
1079 /*
1080 * WRITE:
1081 */
34db0cd6
N
1082 if (conf->pending_count >= max_queued_requests) {
1083 md_wakeup_thread(mddev->thread);
1084 wait_event(conf->wait_barrier,
1085 conf->pending_count < max_queued_requests);
1086 }
6bfe0b49 1087 /* first select target devices under rcu_lock and
1da177e4
LT
1088 * inc refcount on their rdev. Record them by setting
1089 * bios[x] to bio
d4432c23
N
1090 * If there are known/acknowledged bad blocks on any device
1091 * on which we have seen a write error, we want to avoid
1092 * writing to those blocks. This potentially requires several
1093 * writes to write around the bad blocks. Each set of writes
1094 * gets its own r10_bio with a set of bios attached. The number
1095 * of r10_bios is recored in bio->bi_phys_segments just as with
1096 * the read case.
1da177e4 1097 */
c3b328ac
N
1098 plugged = mddev_check_plugged(mddev);
1099
69335ef3 1100 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1da177e4 1101 raid10_find_phys(conf, r10_bio);
d4432c23 1102retry_write:
cb6969e8 1103 blocked_rdev = NULL;
1da177e4 1104 rcu_read_lock();
d4432c23
N
1105 max_sectors = r10_bio->sectors;
1106
1da177e4
LT
1107 for (i = 0; i < conf->copies; i++) {
1108 int d = r10_bio->devs[i].devnum;
3cb03002 1109 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
475b0321
N
1110 struct md_rdev *rrdev = rcu_dereference(
1111 conf->mirrors[d].replacement);
4ca40c2c
N
1112 if (rdev == rrdev)
1113 rrdev = NULL;
6bfe0b49
DW
1114 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1115 atomic_inc(&rdev->nr_pending);
1116 blocked_rdev = rdev;
1117 break;
1118 }
475b0321
N
1119 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1120 atomic_inc(&rrdev->nr_pending);
1121 blocked_rdev = rrdev;
1122 break;
1123 }
1124 if (rrdev && test_bit(Faulty, &rrdev->flags))
1125 rrdev = NULL;
1126
d4432c23 1127 r10_bio->devs[i].bio = NULL;
475b0321 1128 r10_bio->devs[i].repl_bio = NULL;
d4432c23 1129 if (!rdev || test_bit(Faulty, &rdev->flags)) {
6cce3b23 1130 set_bit(R10BIO_Degraded, &r10_bio->state);
d4432c23
N
1131 continue;
1132 }
1133 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1134 sector_t first_bad;
1135 sector_t dev_sector = r10_bio->devs[i].addr;
1136 int bad_sectors;
1137 int is_bad;
1138
1139 is_bad = is_badblock(rdev, dev_sector,
1140 max_sectors,
1141 &first_bad, &bad_sectors);
1142 if (is_bad < 0) {
1143 /* Mustn't write here until the bad block
1144 * is acknowledged
1145 */
1146 atomic_inc(&rdev->nr_pending);
1147 set_bit(BlockedBadBlocks, &rdev->flags);
1148 blocked_rdev = rdev;
1149 break;
1150 }
1151 if (is_bad && first_bad <= dev_sector) {
1152 /* Cannot write here at all */
1153 bad_sectors -= (dev_sector - first_bad);
1154 if (bad_sectors < max_sectors)
1155 /* Mustn't write more than bad_sectors
1156 * to other devices yet
1157 */
1158 max_sectors = bad_sectors;
1159 /* We don't set R10BIO_Degraded as that
1160 * only applies if the disk is missing,
1161 * so it might be re-added, and we want to
1162 * know to recover this chunk.
1163 * In this case the device is here, and the
1164 * fact that this chunk is not in-sync is
1165 * recorded in the bad block log.
1166 */
1167 continue;
1168 }
1169 if (is_bad) {
1170 int good_sectors = first_bad - dev_sector;
1171 if (good_sectors < max_sectors)
1172 max_sectors = good_sectors;
1173 }
6cce3b23 1174 }
d4432c23
N
1175 r10_bio->devs[i].bio = bio;
1176 atomic_inc(&rdev->nr_pending);
475b0321
N
1177 if (rrdev) {
1178 r10_bio->devs[i].repl_bio = bio;
1179 atomic_inc(&rrdev->nr_pending);
1180 }
1da177e4
LT
1181 }
1182 rcu_read_unlock();
1183
6bfe0b49
DW
1184 if (unlikely(blocked_rdev)) {
1185 /* Have to wait for this device to get unblocked, then retry */
1186 int j;
1187 int d;
1188
475b0321 1189 for (j = 0; j < i; j++) {
6bfe0b49
DW
1190 if (r10_bio->devs[j].bio) {
1191 d = r10_bio->devs[j].devnum;
1192 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1193 }
475b0321 1194 if (r10_bio->devs[j].repl_bio) {
4ca40c2c 1195 struct md_rdev *rdev;
475b0321 1196 d = r10_bio->devs[j].devnum;
4ca40c2c
N
1197 rdev = conf->mirrors[d].replacement;
1198 if (!rdev) {
1199 /* Race with remove_disk */
1200 smp_mb();
1201 rdev = conf->mirrors[d].rdev;
1202 }
1203 rdev_dec_pending(rdev, mddev);
475b0321
N
1204 }
1205 }
6bfe0b49
DW
1206 allow_barrier(conf);
1207 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1208 wait_barrier(conf);
1209 goto retry_write;
1210 }
1211
d4432c23
N
1212 if (max_sectors < r10_bio->sectors) {
1213 /* We are splitting this into multiple parts, so
1214 * we need to prepare for allocating another r10_bio.
1215 */
1216 r10_bio->sectors = max_sectors;
1217 spin_lock_irq(&conf->device_lock);
1218 if (bio->bi_phys_segments == 0)
1219 bio->bi_phys_segments = 2;
1220 else
1221 bio->bi_phys_segments++;
1222 spin_unlock_irq(&conf->device_lock);
1223 }
1224 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1225
4e78064f 1226 atomic_set(&r10_bio->remaining, 1);
d4432c23 1227 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
06d91a5f 1228
1da177e4
LT
1229 for (i = 0; i < conf->copies; i++) {
1230 struct bio *mbio;
1231 int d = r10_bio->devs[i].devnum;
1232 if (!r10_bio->devs[i].bio)
1233 continue;
1234
a167f663 1235 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
d4432c23
N
1236 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1237 max_sectors);
1da177e4
LT
1238 r10_bio->devs[i].bio = mbio;
1239
d4432c23
N
1240 mbio->bi_sector = (r10_bio->devs[i].addr+
1241 conf->mirrors[d].rdev->data_offset);
1da177e4
LT
1242 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1243 mbio->bi_end_io = raid10_end_write_request;
e9c7469b 1244 mbio->bi_rw = WRITE | do_sync | do_fua;
1da177e4
LT
1245 mbio->bi_private = r10_bio;
1246
1247 atomic_inc(&r10_bio->remaining);
4e78064f
N
1248 spin_lock_irqsave(&conf->device_lock, flags);
1249 bio_list_add(&conf->pending_bio_list, mbio);
34db0cd6 1250 conf->pending_count++;
4e78064f 1251 spin_unlock_irqrestore(&conf->device_lock, flags);
475b0321
N
1252
1253 if (!r10_bio->devs[i].repl_bio)
1254 continue;
1255
1256 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1257 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1258 max_sectors);
1259 r10_bio->devs[i].repl_bio = mbio;
1260
4ca40c2c
N
1261 /* We are actively writing to the original device
1262 * so it cannot disappear, so the replacement cannot
1263 * become NULL here
1264 */
475b0321
N
1265 mbio->bi_sector = (r10_bio->devs[i].addr+
1266 conf->mirrors[d].replacement->data_offset);
1267 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1268 mbio->bi_end_io = raid10_end_write_request;
1269 mbio->bi_rw = WRITE | do_sync | do_fua;
1270 mbio->bi_private = r10_bio;
1271
1272 atomic_inc(&r10_bio->remaining);
1273 spin_lock_irqsave(&conf->device_lock, flags);
1274 bio_list_add(&conf->pending_bio_list, mbio);
1275 conf->pending_count++;
1276 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
1277 }
1278
079fa166
N
1279 /* Don't remove the bias on 'remaining' (one_write_done) until
1280 * after checking if we need to go around again.
1281 */
a35e63ef 1282
d4432c23 1283 if (sectors_handled < (bio->bi_size >> 9)) {
079fa166 1284 one_write_done(r10_bio);
5e570289 1285 /* We need another r10_bio. It has already been counted
d4432c23
N
1286 * in bio->bi_phys_segments.
1287 */
1288 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1289
1290 r10_bio->master_bio = bio;
1291 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1292
1293 r10_bio->mddev = mddev;
1294 r10_bio->sector = bio->bi_sector + sectors_handled;
1295 r10_bio->state = 0;
1296 goto retry_write;
1297 }
079fa166
N
1298 one_write_done(r10_bio);
1299
1300 /* In case raid10d snuck in to freeze_array */
1301 wake_up(&conf->wait_barrier);
d4432c23 1302
c3b328ac 1303 if (do_sync || !mddev->bitmap || !plugged)
e3881a68 1304 md_wakeup_thread(mddev->thread);
1da177e4
LT
1305}
1306
fd01b88c 1307static void status(struct seq_file *seq, struct mddev *mddev)
1da177e4 1308{
e879a879 1309 struct r10conf *conf = mddev->private;
1da177e4
LT
1310 int i;
1311
1312 if (conf->near_copies < conf->raid_disks)
9d8f0363 1313 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1da177e4
LT
1314 if (conf->near_copies > 1)
1315 seq_printf(seq, " %d near-copies", conf->near_copies);
c93983bf
N
1316 if (conf->far_copies > 1) {
1317 if (conf->far_offset)
1318 seq_printf(seq, " %d offset-copies", conf->far_copies);
1319 else
1320 seq_printf(seq, " %d far-copies", conf->far_copies);
1321 }
1da177e4 1322 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
76186dd8 1323 conf->raid_disks - mddev->degraded);
1da177e4
LT
1324 for (i = 0; i < conf->raid_disks; i++)
1325 seq_printf(seq, "%s",
1326 conf->mirrors[i].rdev &&
b2d444d7 1327 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1da177e4
LT
1328 seq_printf(seq, "]");
1329}
1330
700c7213
N
1331/* check if there are enough drives for
1332 * every block to appear on atleast one.
1333 * Don't consider the device numbered 'ignore'
1334 * as we might be about to remove it.
1335 */
e879a879 1336static int enough(struct r10conf *conf, int ignore)
700c7213
N
1337{
1338 int first = 0;
1339
1340 do {
1341 int n = conf->copies;
1342 int cnt = 0;
1343 while (n--) {
1344 if (conf->mirrors[first].rdev &&
1345 first != ignore)
1346 cnt++;
1347 first = (first+1) % conf->raid_disks;
1348 }
1349 if (cnt == 0)
1350 return 0;
1351 } while (first != 0);
1352 return 1;
1353}
1354
fd01b88c 1355static void error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4
LT
1356{
1357 char b[BDEVNAME_SIZE];
e879a879 1358 struct r10conf *conf = mddev->private;
1da177e4
LT
1359
1360 /*
1361 * If it is not operational, then we have already marked it as dead
1362 * else if it is the last working disks, ignore the error, let the
1363 * next level up know.
1364 * else mark the drive as failed
1365 */
b2d444d7 1366 if (test_bit(In_sync, &rdev->flags)
700c7213 1367 && !enough(conf, rdev->raid_disk))
1da177e4
LT
1368 /*
1369 * Don't fail the drive, just return an IO error.
1da177e4
LT
1370 */
1371 return;
c04be0aa
N
1372 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1373 unsigned long flags;
1374 spin_lock_irqsave(&conf->device_lock, flags);
1da177e4 1375 mddev->degraded++;
c04be0aa 1376 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
1377 /*
1378 * if recovery is running, make sure it aborts.
1379 */
dfc70645 1380 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1da177e4 1381 }
de393cde 1382 set_bit(Blocked, &rdev->flags);
b2d444d7 1383 set_bit(Faulty, &rdev->flags);
850b2b42 1384 set_bit(MD_CHANGE_DEVS, &mddev->flags);
067032bc
JP
1385 printk(KERN_ALERT
1386 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1387 "md/raid10:%s: Operation continuing on %d devices.\n",
128595ed
N
1388 mdname(mddev), bdevname(rdev->bdev, b),
1389 mdname(mddev), conf->raid_disks - mddev->degraded);
1da177e4
LT
1390}
1391
e879a879 1392static void print_conf(struct r10conf *conf)
1da177e4
LT
1393{
1394 int i;
0f6d02d5 1395 struct mirror_info *tmp;
1da177e4 1396
128595ed 1397 printk(KERN_DEBUG "RAID10 conf printout:\n");
1da177e4 1398 if (!conf) {
128595ed 1399 printk(KERN_DEBUG "(!conf)\n");
1da177e4
LT
1400 return;
1401 }
128595ed 1402 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1da177e4
LT
1403 conf->raid_disks);
1404
1405 for (i = 0; i < conf->raid_disks; i++) {
1406 char b[BDEVNAME_SIZE];
1407 tmp = conf->mirrors + i;
1408 if (tmp->rdev)
128595ed 1409 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
b2d444d7
N
1410 i, !test_bit(In_sync, &tmp->rdev->flags),
1411 !test_bit(Faulty, &tmp->rdev->flags),
1da177e4
LT
1412 bdevname(tmp->rdev->bdev,b));
1413 }
1414}
1415
e879a879 1416static void close_sync(struct r10conf *conf)
1da177e4 1417{
0a27ec96
N
1418 wait_barrier(conf);
1419 allow_barrier(conf);
1da177e4
LT
1420
1421 mempool_destroy(conf->r10buf_pool);
1422 conf->r10buf_pool = NULL;
1423}
1424
fd01b88c 1425static int raid10_spare_active(struct mddev *mddev)
1da177e4
LT
1426{
1427 int i;
e879a879 1428 struct r10conf *conf = mddev->private;
0f6d02d5 1429 struct mirror_info *tmp;
6b965620
N
1430 int count = 0;
1431 unsigned long flags;
1da177e4
LT
1432
1433 /*
1434 * Find all non-in_sync disks within the RAID10 configuration
1435 * and mark them in_sync
1436 */
1437 for (i = 0; i < conf->raid_disks; i++) {
1438 tmp = conf->mirrors + i;
4ca40c2c
N
1439 if (tmp->replacement
1440 && tmp->replacement->recovery_offset == MaxSector
1441 && !test_bit(Faulty, &tmp->replacement->flags)
1442 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1443 /* Replacement has just become active */
1444 if (!tmp->rdev
1445 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1446 count++;
1447 if (tmp->rdev) {
1448 /* Replaced device not technically faulty,
1449 * but we need to be sure it gets removed
1450 * and never re-added.
1451 */
1452 set_bit(Faulty, &tmp->rdev->flags);
1453 sysfs_notify_dirent_safe(
1454 tmp->rdev->sysfs_state);
1455 }
1456 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1457 } else if (tmp->rdev
1458 && !test_bit(Faulty, &tmp->rdev->flags)
1459 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 1460 count++;
e6ffbcb6 1461 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1da177e4
LT
1462 }
1463 }
6b965620
N
1464 spin_lock_irqsave(&conf->device_lock, flags);
1465 mddev->degraded -= count;
1466 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
1467
1468 print_conf(conf);
6b965620 1469 return count;
1da177e4
LT
1470}
1471
1472
fd01b88c 1473static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 1474{
e879a879 1475 struct r10conf *conf = mddev->private;
199050ea 1476 int err = -EEXIST;
1da177e4 1477 int mirror;
6c2fce2e 1478 int first = 0;
84707f38 1479 int last = conf->raid_disks - 1;
1da177e4
LT
1480
1481 if (mddev->recovery_cp < MaxSector)
1482 /* only hot-add to in-sync arrays, as recovery is
1483 * very different from resync
1484 */
199050ea 1485 return -EBUSY;
dc10c643 1486 if (rdev->saved_raid_disk < 0 && !enough(conf, -1))
199050ea 1487 return -EINVAL;
1da177e4 1488
a53a6c85 1489 if (rdev->raid_disk >= 0)
6c2fce2e 1490 first = last = rdev->raid_disk;
1da177e4 1491
2c4193df 1492 if (rdev->saved_raid_disk >= first &&
6cce3b23
N
1493 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1494 mirror = rdev->saved_raid_disk;
1495 else
6c2fce2e 1496 mirror = first;
2bb77736 1497 for ( ; mirror <= last ; mirror++) {
0f6d02d5 1498 struct mirror_info *p = &conf->mirrors[mirror];
2bb77736
N
1499 if (p->recovery_disabled == mddev->recovery_disabled)
1500 continue;
b7044d41
N
1501 if (p->rdev) {
1502 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1503 p->replacement != NULL)
1504 continue;
1505 clear_bit(In_sync, &rdev->flags);
1506 set_bit(Replacement, &rdev->flags);
1507 rdev->raid_disk = mirror;
1508 err = 0;
1509 disk_stack_limits(mddev->gendisk, rdev->bdev,
1510 rdev->data_offset << 9);
1511 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1512 blk_queue_max_segments(mddev->queue, 1);
1513 blk_queue_segment_boundary(mddev->queue,
1514 PAGE_CACHE_SIZE - 1);
1515 }
1516 conf->fullsync = 1;
1517 rcu_assign_pointer(p->replacement, rdev);
1518 break;
1519 }
1da177e4 1520
2bb77736
N
1521 disk_stack_limits(mddev->gendisk, rdev->bdev,
1522 rdev->data_offset << 9);
1523 /* as we don't honour merge_bvec_fn, we must
1524 * never risk violating it, so limit
1525 * ->max_segments to one lying with a single
1526 * page, as a one page request is never in
1527 * violation.
1528 */
1529 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1530 blk_queue_max_segments(mddev->queue, 1);
1531 blk_queue_segment_boundary(mddev->queue,
1532 PAGE_CACHE_SIZE - 1);
1da177e4
LT
1533 }
1534
2bb77736 1535 p->head_position = 0;
d890fa2b 1536 p->recovery_disabled = mddev->recovery_disabled - 1;
2bb77736
N
1537 rdev->raid_disk = mirror;
1538 err = 0;
1539 if (rdev->saved_raid_disk != mirror)
1540 conf->fullsync = 1;
1541 rcu_assign_pointer(p->rdev, rdev);
1542 break;
1543 }
1544
ac5e7113 1545 md_integrity_add_rdev(rdev, mddev);
1da177e4 1546 print_conf(conf);
199050ea 1547 return err;
1da177e4
LT
1548}
1549
b8321b68 1550static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 1551{
e879a879 1552 struct r10conf *conf = mddev->private;
1da177e4 1553 int err = 0;
b8321b68 1554 int number = rdev->raid_disk;
c8ab903e
N
1555 struct md_rdev **rdevp;
1556 struct mirror_info *p = conf->mirrors + number;
1da177e4
LT
1557
1558 print_conf(conf);
c8ab903e
N
1559 if (rdev == p->rdev)
1560 rdevp = &p->rdev;
1561 else if (rdev == p->replacement)
1562 rdevp = &p->replacement;
1563 else
1564 return 0;
1565
1566 if (test_bit(In_sync, &rdev->flags) ||
1567 atomic_read(&rdev->nr_pending)) {
1568 err = -EBUSY;
1569 goto abort;
1570 }
1571 /* Only remove faulty devices if recovery
1572 * is not possible.
1573 */
1574 if (!test_bit(Faulty, &rdev->flags) &&
1575 mddev->recovery_disabled != p->recovery_disabled &&
4ca40c2c 1576 (!p->replacement || p->replacement == rdev) &&
c8ab903e
N
1577 enough(conf, -1)) {
1578 err = -EBUSY;
1579 goto abort;
1da177e4 1580 }
c8ab903e
N
1581 *rdevp = NULL;
1582 synchronize_rcu();
1583 if (atomic_read(&rdev->nr_pending)) {
1584 /* lost the race, try later */
1585 err = -EBUSY;
1586 *rdevp = rdev;
1587 goto abort;
4ca40c2c
N
1588 } else if (p->replacement) {
1589 /* We must have just cleared 'rdev' */
1590 p->rdev = p->replacement;
1591 clear_bit(Replacement, &p->replacement->flags);
1592 smp_mb(); /* Make sure other CPUs may see both as identical
1593 * but will never see neither -- if they are careful.
1594 */
1595 p->replacement = NULL;
1596 clear_bit(WantReplacement, &rdev->flags);
1597 } else
1598 /* We might have just remove the Replacement as faulty
1599 * Clear the flag just in case
1600 */
1601 clear_bit(WantReplacement, &rdev->flags);
1602
c8ab903e
N
1603 err = md_integrity_register(mddev);
1604
1da177e4
LT
1605abort:
1606
1607 print_conf(conf);
1608 return err;
1609}
1610
1611
6712ecf8 1612static void end_sync_read(struct bio *bio, int error)
1da177e4 1613{
9f2c9d12 1614 struct r10bio *r10_bio = bio->bi_private;
e879a879 1615 struct r10conf *conf = r10_bio->mddev->private;
778ca018 1616 int d;
1da177e4 1617
69335ef3 1618 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
0eb3ff12
N
1619
1620 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1621 set_bit(R10BIO_Uptodate, &r10_bio->state);
e684e41d
N
1622 else
1623 /* The write handler will notice the lack of
1624 * R10BIO_Uptodate and record any errors etc
1625 */
4dbcdc75
N
1626 atomic_add(r10_bio->sectors,
1627 &conf->mirrors[d].rdev->corrected_errors);
1da177e4
LT
1628
1629 /* for reconstruct, we always reschedule after a read.
1630 * for resync, only after all reads
1631 */
73d5c38a 1632 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1da177e4
LT
1633 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1634 atomic_dec_and_test(&r10_bio->remaining)) {
1635 /* we have read all the blocks,
1636 * do the comparison in process context in raid10d
1637 */
1638 reschedule_retry(r10_bio);
1639 }
1da177e4
LT
1640}
1641
9f2c9d12 1642static void end_sync_request(struct r10bio *r10_bio)
1da177e4 1643{
fd01b88c 1644 struct mddev *mddev = r10_bio->mddev;
dfc70645 1645
1da177e4
LT
1646 while (atomic_dec_and_test(&r10_bio->remaining)) {
1647 if (r10_bio->master_bio == NULL) {
1648 /* the primary of several recovery bios */
73d5c38a 1649 sector_t s = r10_bio->sectors;
1a0b7cd8
N
1650 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1651 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
1652 reschedule_retry(r10_bio);
1653 else
1654 put_buf(r10_bio);
73d5c38a 1655 md_done_sync(mddev, s, 1);
1da177e4
LT
1656 break;
1657 } else {
9f2c9d12 1658 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1a0b7cd8
N
1659 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1660 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
1661 reschedule_retry(r10_bio);
1662 else
1663 put_buf(r10_bio);
1da177e4
LT
1664 r10_bio = r10_bio2;
1665 }
1666 }
1da177e4
LT
1667}
1668
5e570289
N
1669static void end_sync_write(struct bio *bio, int error)
1670{
1671 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
9f2c9d12 1672 struct r10bio *r10_bio = bio->bi_private;
fd01b88c 1673 struct mddev *mddev = r10_bio->mddev;
e879a879 1674 struct r10conf *conf = mddev->private;
5e570289
N
1675 int d;
1676 sector_t first_bad;
1677 int bad_sectors;
1678 int slot;
9ad1aefc 1679 int repl;
4ca40c2c 1680 struct md_rdev *rdev = NULL;
5e570289 1681
9ad1aefc
N
1682 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1683 if (repl)
1684 rdev = conf->mirrors[d].replacement;
547414d1 1685 else
9ad1aefc 1686 rdev = conf->mirrors[d].rdev;
5e570289
N
1687
1688 if (!uptodate) {
9ad1aefc
N
1689 if (repl)
1690 md_error(mddev, rdev);
1691 else {
1692 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
1693 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1694 set_bit(MD_RECOVERY_NEEDED,
1695 &rdev->mddev->recovery);
9ad1aefc
N
1696 set_bit(R10BIO_WriteError, &r10_bio->state);
1697 }
1698 } else if (is_badblock(rdev,
5e570289
N
1699 r10_bio->devs[slot].addr,
1700 r10_bio->sectors,
1701 &first_bad, &bad_sectors))
1702 set_bit(R10BIO_MadeGood, &r10_bio->state);
1703
9ad1aefc 1704 rdev_dec_pending(rdev, mddev);
5e570289
N
1705
1706 end_sync_request(r10_bio);
1707}
1708
1da177e4
LT
1709/*
1710 * Note: sync and recover and handled very differently for raid10
1711 * This code is for resync.
1712 * For resync, we read through virtual addresses and read all blocks.
1713 * If there is any error, we schedule a write. The lowest numbered
1714 * drive is authoritative.
1715 * However requests come for physical address, so we need to map.
1716 * For every physical address there are raid_disks/copies virtual addresses,
1717 * which is always are least one, but is not necessarly an integer.
1718 * This means that a physical address can span multiple chunks, so we may
1719 * have to submit multiple io requests for a single sync request.
1720 */
1721/*
1722 * We check if all blocks are in-sync and only write to blocks that
1723 * aren't in sync
1724 */
9f2c9d12 1725static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1da177e4 1726{
e879a879 1727 struct r10conf *conf = mddev->private;
1da177e4
LT
1728 int i, first;
1729 struct bio *tbio, *fbio;
1730
1731 atomic_set(&r10_bio->remaining, 1);
1732
1733 /* find the first device with a block */
1734 for (i=0; i<conf->copies; i++)
1735 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1736 break;
1737
1738 if (i == conf->copies)
1739 goto done;
1740
1741 first = i;
1742 fbio = r10_bio->devs[i].bio;
1743
1744 /* now find blocks with errors */
0eb3ff12
N
1745 for (i=0 ; i < conf->copies ; i++) {
1746 int j, d;
1747 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1da177e4 1748
1da177e4 1749 tbio = r10_bio->devs[i].bio;
0eb3ff12
N
1750
1751 if (tbio->bi_end_io != end_sync_read)
1752 continue;
1753 if (i == first)
1da177e4 1754 continue;
0eb3ff12
N
1755 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1756 /* We know that the bi_io_vec layout is the same for
1757 * both 'first' and 'i', so we just compare them.
1758 * All vec entries are PAGE_SIZE;
1759 */
1760 for (j = 0; j < vcnt; j++)
1761 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1762 page_address(tbio->bi_io_vec[j].bv_page),
1763 PAGE_SIZE))
1764 break;
1765 if (j == vcnt)
1766 continue;
1767 mddev->resync_mismatches += r10_bio->sectors;
f84ee364
N
1768 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1769 /* Don't fix anything. */
1770 continue;
0eb3ff12 1771 }
f84ee364
N
1772 /* Ok, we need to write this bio, either to correct an
1773 * inconsistency or to correct an unreadable block.
1da177e4
LT
1774 * First we need to fixup bv_offset, bv_len and
1775 * bi_vecs, as the read request might have corrupted these
1776 */
1777 tbio->bi_vcnt = vcnt;
1778 tbio->bi_size = r10_bio->sectors << 9;
1779 tbio->bi_idx = 0;
1780 tbio->bi_phys_segments = 0;
1da177e4
LT
1781 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1782 tbio->bi_flags |= 1 << BIO_UPTODATE;
1783 tbio->bi_next = NULL;
1784 tbio->bi_rw = WRITE;
1785 tbio->bi_private = r10_bio;
1786 tbio->bi_sector = r10_bio->devs[i].addr;
1787
1788 for (j=0; j < vcnt ; j++) {
1789 tbio->bi_io_vec[j].bv_offset = 0;
1790 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1791
1792 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1793 page_address(fbio->bi_io_vec[j].bv_page),
1794 PAGE_SIZE);
1795 }
1796 tbio->bi_end_io = end_sync_write;
1797
1798 d = r10_bio->devs[i].devnum;
1799 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1800 atomic_inc(&r10_bio->remaining);
1801 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1802
1803 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1804 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1805 generic_make_request(tbio);
1806 }
1807
9ad1aefc
N
1808 /* Now write out to any replacement devices
1809 * that are active
1810 */
1811 for (i = 0; i < conf->copies; i++) {
1812 int j, d;
1813 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1814
1815 tbio = r10_bio->devs[i].repl_bio;
1816 if (!tbio || !tbio->bi_end_io)
1817 continue;
1818 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1819 && r10_bio->devs[i].bio != fbio)
1820 for (j = 0; j < vcnt; j++)
1821 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1822 page_address(fbio->bi_io_vec[j].bv_page),
1823 PAGE_SIZE);
1824 d = r10_bio->devs[i].devnum;
1825 atomic_inc(&r10_bio->remaining);
1826 md_sync_acct(conf->mirrors[d].replacement->bdev,
1827 tbio->bi_size >> 9);
1828 generic_make_request(tbio);
1829 }
1830
1da177e4
LT
1831done:
1832 if (atomic_dec_and_test(&r10_bio->remaining)) {
1833 md_done_sync(mddev, r10_bio->sectors, 1);
1834 put_buf(r10_bio);
1835 }
1836}
1837
1838/*
1839 * Now for the recovery code.
1840 * Recovery happens across physical sectors.
1841 * We recover all non-is_sync drives by finding the virtual address of
1842 * each, and then choose a working drive that also has that virt address.
1843 * There is a separate r10_bio for each non-in_sync drive.
1844 * Only the first two slots are in use. The first for reading,
1845 * The second for writing.
1846 *
1847 */
9f2c9d12 1848static void fix_recovery_read_error(struct r10bio *r10_bio)
5e570289
N
1849{
1850 /* We got a read error during recovery.
1851 * We repeat the read in smaller page-sized sections.
1852 * If a read succeeds, write it to the new device or record
1853 * a bad block if we cannot.
1854 * If a read fails, record a bad block on both old and
1855 * new devices.
1856 */
fd01b88c 1857 struct mddev *mddev = r10_bio->mddev;
e879a879 1858 struct r10conf *conf = mddev->private;
5e570289
N
1859 struct bio *bio = r10_bio->devs[0].bio;
1860 sector_t sect = 0;
1861 int sectors = r10_bio->sectors;
1862 int idx = 0;
1863 int dr = r10_bio->devs[0].devnum;
1864 int dw = r10_bio->devs[1].devnum;
1865
1866 while (sectors) {
1867 int s = sectors;
3cb03002 1868 struct md_rdev *rdev;
5e570289
N
1869 sector_t addr;
1870 int ok;
1871
1872 if (s > (PAGE_SIZE>>9))
1873 s = PAGE_SIZE >> 9;
1874
1875 rdev = conf->mirrors[dr].rdev;
1876 addr = r10_bio->devs[0].addr + sect,
1877 ok = sync_page_io(rdev,
1878 addr,
1879 s << 9,
1880 bio->bi_io_vec[idx].bv_page,
1881 READ, false);
1882 if (ok) {
1883 rdev = conf->mirrors[dw].rdev;
1884 addr = r10_bio->devs[1].addr + sect;
1885 ok = sync_page_io(rdev,
1886 addr,
1887 s << 9,
1888 bio->bi_io_vec[idx].bv_page,
1889 WRITE, false);
b7044d41 1890 if (!ok) {
5e570289 1891 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
1892 if (!test_and_set_bit(WantReplacement,
1893 &rdev->flags))
1894 set_bit(MD_RECOVERY_NEEDED,
1895 &rdev->mddev->recovery);
1896 }
5e570289
N
1897 }
1898 if (!ok) {
1899 /* We don't worry if we cannot set a bad block -
1900 * it really is bad so there is no loss in not
1901 * recording it yet
1902 */
1903 rdev_set_badblocks(rdev, addr, s, 0);
1904
1905 if (rdev != conf->mirrors[dw].rdev) {
1906 /* need bad block on destination too */
3cb03002 1907 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
5e570289
N
1908 addr = r10_bio->devs[1].addr + sect;
1909 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1910 if (!ok) {
1911 /* just abort the recovery */
1912 printk(KERN_NOTICE
1913 "md/raid10:%s: recovery aborted"
1914 " due to read error\n",
1915 mdname(mddev));
1916
1917 conf->mirrors[dw].recovery_disabled
1918 = mddev->recovery_disabled;
1919 set_bit(MD_RECOVERY_INTR,
1920 &mddev->recovery);
1921 break;
1922 }
1923 }
1924 }
1925
1926 sectors -= s;
1927 sect += s;
1928 idx++;
1929 }
1930}
1da177e4 1931
9f2c9d12 1932static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1da177e4 1933{
e879a879 1934 struct r10conf *conf = mddev->private;
c65060ad 1935 int d;
24afd80d 1936 struct bio *wbio, *wbio2;
1da177e4 1937
5e570289
N
1938 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1939 fix_recovery_read_error(r10_bio);
1940 end_sync_request(r10_bio);
1941 return;
1942 }
1943
c65060ad
NK
1944 /*
1945 * share the pages with the first bio
1da177e4
LT
1946 * and submit the write request
1947 */
1da177e4 1948 d = r10_bio->devs[1].devnum;
24afd80d
N
1949 wbio = r10_bio->devs[1].bio;
1950 wbio2 = r10_bio->devs[1].repl_bio;
1951 if (wbio->bi_end_io) {
1952 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1953 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1954 generic_make_request(wbio);
1955 }
1956 if (wbio2 && wbio2->bi_end_io) {
1957 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
1958 md_sync_acct(conf->mirrors[d].replacement->bdev,
1959 wbio2->bi_size >> 9);
1960 generic_make_request(wbio2);
1961 }
1da177e4
LT
1962}
1963
1964
1e50915f
RB
1965/*
1966 * Used by fix_read_error() to decay the per rdev read_errors.
1967 * We halve the read error count for every hour that has elapsed
1968 * since the last recorded read error.
1969 *
1970 */
fd01b88c 1971static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
1e50915f
RB
1972{
1973 struct timespec cur_time_mon;
1974 unsigned long hours_since_last;
1975 unsigned int read_errors = atomic_read(&rdev->read_errors);
1976
1977 ktime_get_ts(&cur_time_mon);
1978
1979 if (rdev->last_read_error.tv_sec == 0 &&
1980 rdev->last_read_error.tv_nsec == 0) {
1981 /* first time we've seen a read error */
1982 rdev->last_read_error = cur_time_mon;
1983 return;
1984 }
1985
1986 hours_since_last = (cur_time_mon.tv_sec -
1987 rdev->last_read_error.tv_sec) / 3600;
1988
1989 rdev->last_read_error = cur_time_mon;
1990
1991 /*
1992 * if hours_since_last is > the number of bits in read_errors
1993 * just set read errors to 0. We do this to avoid
1994 * overflowing the shift of read_errors by hours_since_last.
1995 */
1996 if (hours_since_last >= 8 * sizeof(read_errors))
1997 atomic_set(&rdev->read_errors, 0);
1998 else
1999 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2000}
2001
3cb03002 2002static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
58c54fcc
N
2003 int sectors, struct page *page, int rw)
2004{
2005 sector_t first_bad;
2006 int bad_sectors;
2007
2008 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2009 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2010 return -1;
2011 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2012 /* success */
2013 return 1;
b7044d41 2014 if (rw == WRITE) {
58c54fcc 2015 set_bit(WriteErrorSeen, &rdev->flags);
b7044d41
N
2016 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2017 set_bit(MD_RECOVERY_NEEDED,
2018 &rdev->mddev->recovery);
2019 }
58c54fcc
N
2020 /* need to record an error - either for the block or the device */
2021 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2022 md_error(rdev->mddev, rdev);
2023 return 0;
2024}
2025
1da177e4
LT
2026/*
2027 * This is a kernel thread which:
2028 *
2029 * 1. Retries failed read operations on working mirrors.
2030 * 2. Updates the raid superblock when problems encounter.
6814d536 2031 * 3. Performs writes following reads for array synchronising.
1da177e4
LT
2032 */
2033
e879a879 2034static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
6814d536
N
2035{
2036 int sect = 0; /* Offset from r10_bio->sector */
2037 int sectors = r10_bio->sectors;
3cb03002 2038 struct md_rdev*rdev;
1e50915f 2039 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
0544a21d 2040 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1e50915f 2041
7c4e06ff
N
2042 /* still own a reference to this rdev, so it cannot
2043 * have been cleared recently.
2044 */
2045 rdev = conf->mirrors[d].rdev;
1e50915f 2046
7c4e06ff
N
2047 if (test_bit(Faulty, &rdev->flags))
2048 /* drive has already been failed, just ignore any
2049 more fix_read_error() attempts */
2050 return;
1e50915f 2051
7c4e06ff
N
2052 check_decay_read_errors(mddev, rdev);
2053 atomic_inc(&rdev->read_errors);
2054 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2055 char b[BDEVNAME_SIZE];
2056 bdevname(rdev->bdev, b);
1e50915f 2057
7c4e06ff
N
2058 printk(KERN_NOTICE
2059 "md/raid10:%s: %s: Raid device exceeded "
2060 "read_error threshold [cur %d:max %d]\n",
2061 mdname(mddev), b,
2062 atomic_read(&rdev->read_errors), max_read_errors);
2063 printk(KERN_NOTICE
2064 "md/raid10:%s: %s: Failing raid device\n",
2065 mdname(mddev), b);
2066 md_error(mddev, conf->mirrors[d].rdev);
fae8cc5e 2067 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
7c4e06ff 2068 return;
1e50915f 2069 }
1e50915f 2070
6814d536
N
2071 while(sectors) {
2072 int s = sectors;
2073 int sl = r10_bio->read_slot;
2074 int success = 0;
2075 int start;
2076
2077 if (s > (PAGE_SIZE>>9))
2078 s = PAGE_SIZE >> 9;
2079
2080 rcu_read_lock();
2081 do {
8dbed5ce
N
2082 sector_t first_bad;
2083 int bad_sectors;
2084
0544a21d 2085 d = r10_bio->devs[sl].devnum;
6814d536
N
2086 rdev = rcu_dereference(conf->mirrors[d].rdev);
2087 if (rdev &&
8dbed5ce
N
2088 test_bit(In_sync, &rdev->flags) &&
2089 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2090 &first_bad, &bad_sectors) == 0) {
6814d536
N
2091 atomic_inc(&rdev->nr_pending);
2092 rcu_read_unlock();
2b193363 2093 success = sync_page_io(rdev,
6814d536 2094 r10_bio->devs[sl].addr +
ccebd4c4 2095 sect,
6814d536 2096 s<<9,
ccebd4c4 2097 conf->tmppage, READ, false);
6814d536
N
2098 rdev_dec_pending(rdev, mddev);
2099 rcu_read_lock();
2100 if (success)
2101 break;
2102 }
2103 sl++;
2104 if (sl == conf->copies)
2105 sl = 0;
2106 } while (!success && sl != r10_bio->read_slot);
2107 rcu_read_unlock();
2108
2109 if (!success) {
58c54fcc
N
2110 /* Cannot read from anywhere, just mark the block
2111 * as bad on the first device to discourage future
2112 * reads.
2113 */
6814d536 2114 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
58c54fcc
N
2115 rdev = conf->mirrors[dn].rdev;
2116
2117 if (!rdev_set_badblocks(
2118 rdev,
2119 r10_bio->devs[r10_bio->read_slot].addr
2120 + sect,
fae8cc5e 2121 s, 0)) {
58c54fcc 2122 md_error(mddev, rdev);
fae8cc5e
N
2123 r10_bio->devs[r10_bio->read_slot].bio
2124 = IO_BLOCKED;
2125 }
6814d536
N
2126 break;
2127 }
2128
2129 start = sl;
2130 /* write it back and re-read */
2131 rcu_read_lock();
2132 while (sl != r10_bio->read_slot) {
67b8dc4b 2133 char b[BDEVNAME_SIZE];
0544a21d 2134
6814d536
N
2135 if (sl==0)
2136 sl = conf->copies;
2137 sl--;
2138 d = r10_bio->devs[sl].devnum;
2139 rdev = rcu_dereference(conf->mirrors[d].rdev);
1294b9c9
N
2140 if (!rdev ||
2141 !test_bit(In_sync, &rdev->flags))
2142 continue;
2143
2144 atomic_inc(&rdev->nr_pending);
2145 rcu_read_unlock();
58c54fcc
N
2146 if (r10_sync_page_io(rdev,
2147 r10_bio->devs[sl].addr +
2148 sect,
2149 s<<9, conf->tmppage, WRITE)
1294b9c9
N
2150 == 0) {
2151 /* Well, this device is dead */
2152 printk(KERN_NOTICE
2153 "md/raid10:%s: read correction "
2154 "write failed"
2155 " (%d sectors at %llu on %s)\n",
2156 mdname(mddev), s,
2157 (unsigned long long)(
2158 sect + rdev->data_offset),
2159 bdevname(rdev->bdev, b));
2160 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2161 "drive\n",
2162 mdname(mddev),
2163 bdevname(rdev->bdev, b));
6814d536 2164 }
1294b9c9
N
2165 rdev_dec_pending(rdev, mddev);
2166 rcu_read_lock();
6814d536
N
2167 }
2168 sl = start;
2169 while (sl != r10_bio->read_slot) {
1294b9c9 2170 char b[BDEVNAME_SIZE];
0544a21d 2171
6814d536
N
2172 if (sl==0)
2173 sl = conf->copies;
2174 sl--;
2175 d = r10_bio->devs[sl].devnum;
2176 rdev = rcu_dereference(conf->mirrors[d].rdev);
1294b9c9
N
2177 if (!rdev ||
2178 !test_bit(In_sync, &rdev->flags))
2179 continue;
6814d536 2180
1294b9c9
N
2181 atomic_inc(&rdev->nr_pending);
2182 rcu_read_unlock();
58c54fcc
N
2183 switch (r10_sync_page_io(rdev,
2184 r10_bio->devs[sl].addr +
2185 sect,
2186 s<<9, conf->tmppage,
2187 READ)) {
2188 case 0:
1294b9c9
N
2189 /* Well, this device is dead */
2190 printk(KERN_NOTICE
2191 "md/raid10:%s: unable to read back "
2192 "corrected sectors"
2193 " (%d sectors at %llu on %s)\n",
2194 mdname(mddev), s,
2195 (unsigned long long)(
2196 sect + rdev->data_offset),
2197 bdevname(rdev->bdev, b));
2198 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2199 "drive\n",
2200 mdname(mddev),
2201 bdevname(rdev->bdev, b));
58c54fcc
N
2202 break;
2203 case 1:
1294b9c9
N
2204 printk(KERN_INFO
2205 "md/raid10:%s: read error corrected"
2206 " (%d sectors at %llu on %s)\n",
2207 mdname(mddev), s,
2208 (unsigned long long)(
2209 sect + rdev->data_offset),
2210 bdevname(rdev->bdev, b));
2211 atomic_add(s, &rdev->corrected_errors);
6814d536 2212 }
1294b9c9
N
2213
2214 rdev_dec_pending(rdev, mddev);
2215 rcu_read_lock();
6814d536
N
2216 }
2217 rcu_read_unlock();
2218
2219 sectors -= s;
2220 sect += s;
2221 }
2222}
2223
bd870a16
N
2224static void bi_complete(struct bio *bio, int error)
2225{
2226 complete((struct completion *)bio->bi_private);
2227}
2228
2229static int submit_bio_wait(int rw, struct bio *bio)
2230{
2231 struct completion event;
2232 rw |= REQ_SYNC;
2233
2234 init_completion(&event);
2235 bio->bi_private = &event;
2236 bio->bi_end_io = bi_complete;
2237 submit_bio(rw, bio);
2238 wait_for_completion(&event);
2239
2240 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2241}
2242
9f2c9d12 2243static int narrow_write_error(struct r10bio *r10_bio, int i)
bd870a16
N
2244{
2245 struct bio *bio = r10_bio->master_bio;
fd01b88c 2246 struct mddev *mddev = r10_bio->mddev;
e879a879 2247 struct r10conf *conf = mddev->private;
3cb03002 2248 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
bd870a16
N
2249 /* bio has the data to be written to slot 'i' where
2250 * we just recently had a write error.
2251 * We repeatedly clone the bio and trim down to one block,
2252 * then try the write. Where the write fails we record
2253 * a bad block.
2254 * It is conceivable that the bio doesn't exactly align with
2255 * blocks. We must handle this.
2256 *
2257 * We currently own a reference to the rdev.
2258 */
2259
2260 int block_sectors;
2261 sector_t sector;
2262 int sectors;
2263 int sect_to_write = r10_bio->sectors;
2264 int ok = 1;
2265
2266 if (rdev->badblocks.shift < 0)
2267 return 0;
2268
2269 block_sectors = 1 << rdev->badblocks.shift;
2270 sector = r10_bio->sector;
2271 sectors = ((r10_bio->sector + block_sectors)
2272 & ~(sector_t)(block_sectors - 1))
2273 - sector;
2274
2275 while (sect_to_write) {
2276 struct bio *wbio;
2277 if (sectors > sect_to_write)
2278 sectors = sect_to_write;
2279 /* Write at 'sector' for 'sectors' */
2280 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2281 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2282 wbio->bi_sector = (r10_bio->devs[i].addr+
2283 rdev->data_offset+
2284 (sector - r10_bio->sector));
2285 wbio->bi_bdev = rdev->bdev;
2286 if (submit_bio_wait(WRITE, wbio) == 0)
2287 /* Failure! */
2288 ok = rdev_set_badblocks(rdev, sector,
2289 sectors, 0)
2290 && ok;
2291
2292 bio_put(wbio);
2293 sect_to_write -= sectors;
2294 sector += sectors;
2295 sectors = block_sectors;
2296 }
2297 return ok;
2298}
2299
9f2c9d12 2300static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
560f8e55
N
2301{
2302 int slot = r10_bio->read_slot;
560f8e55 2303 struct bio *bio;
e879a879 2304 struct r10conf *conf = mddev->private;
abbf098e 2305 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
560f8e55
N
2306 char b[BDEVNAME_SIZE];
2307 unsigned long do_sync;
856e08e2 2308 int max_sectors;
560f8e55
N
2309
2310 /* we got a read error. Maybe the drive is bad. Maybe just
2311 * the block and we can fix it.
2312 * We freeze all other IO, and try reading the block from
2313 * other devices. When we find one, we re-write
2314 * and check it that fixes the read error.
2315 * This is all done synchronously while the array is
2316 * frozen.
2317 */
fae8cc5e
N
2318 bio = r10_bio->devs[slot].bio;
2319 bdevname(bio->bi_bdev, b);
2320 bio_put(bio);
2321 r10_bio->devs[slot].bio = NULL;
2322
560f8e55
N
2323 if (mddev->ro == 0) {
2324 freeze_array(conf);
2325 fix_read_error(conf, mddev, r10_bio);
2326 unfreeze_array(conf);
fae8cc5e
N
2327 } else
2328 r10_bio->devs[slot].bio = IO_BLOCKED;
2329
abbf098e 2330 rdev_dec_pending(rdev, mddev);
560f8e55 2331
7399c31b 2332read_more:
96c3fd1f
N
2333 rdev = read_balance(conf, r10_bio, &max_sectors);
2334 if (rdev == NULL) {
560f8e55
N
2335 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2336 " read error for block %llu\n",
7399c31b 2337 mdname(mddev), b,
560f8e55
N
2338 (unsigned long long)r10_bio->sector);
2339 raid_end_bio_io(r10_bio);
560f8e55
N
2340 return;
2341 }
2342
2343 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
560f8e55 2344 slot = r10_bio->read_slot;
560f8e55
N
2345 printk_ratelimited(
2346 KERN_ERR
2347 "md/raid10:%s: %s: redirecting"
2348 "sector %llu to another mirror\n",
2349 mdname(mddev),
2350 bdevname(rdev->bdev, b),
2351 (unsigned long long)r10_bio->sector);
2352 bio = bio_clone_mddev(r10_bio->master_bio,
2353 GFP_NOIO, mddev);
7399c31b
N
2354 md_trim_bio(bio,
2355 r10_bio->sector - bio->bi_sector,
2356 max_sectors);
560f8e55 2357 r10_bio->devs[slot].bio = bio;
abbf098e 2358 r10_bio->devs[slot].rdev = rdev;
560f8e55
N
2359 bio->bi_sector = r10_bio->devs[slot].addr
2360 + rdev->data_offset;
2361 bio->bi_bdev = rdev->bdev;
2362 bio->bi_rw = READ | do_sync;
2363 bio->bi_private = r10_bio;
2364 bio->bi_end_io = raid10_end_read_request;
7399c31b
N
2365 if (max_sectors < r10_bio->sectors) {
2366 /* Drat - have to split this up more */
2367 struct bio *mbio = r10_bio->master_bio;
2368 int sectors_handled =
2369 r10_bio->sector + max_sectors
2370 - mbio->bi_sector;
2371 r10_bio->sectors = max_sectors;
2372 spin_lock_irq(&conf->device_lock);
2373 if (mbio->bi_phys_segments == 0)
2374 mbio->bi_phys_segments = 2;
2375 else
2376 mbio->bi_phys_segments++;
2377 spin_unlock_irq(&conf->device_lock);
2378 generic_make_request(bio);
7399c31b
N
2379
2380 r10_bio = mempool_alloc(conf->r10bio_pool,
2381 GFP_NOIO);
2382 r10_bio->master_bio = mbio;
2383 r10_bio->sectors = (mbio->bi_size >> 9)
2384 - sectors_handled;
2385 r10_bio->state = 0;
2386 set_bit(R10BIO_ReadError,
2387 &r10_bio->state);
2388 r10_bio->mddev = mddev;
2389 r10_bio->sector = mbio->bi_sector
2390 + sectors_handled;
2391
2392 goto read_more;
2393 } else
2394 generic_make_request(bio);
560f8e55
N
2395}
2396
e879a879 2397static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
749c55e9
N
2398{
2399 /* Some sort of write request has finished and it
2400 * succeeded in writing where we thought there was a
2401 * bad block. So forget the bad block.
1a0b7cd8
N
2402 * Or possibly if failed and we need to record
2403 * a bad block.
749c55e9
N
2404 */
2405 int m;
3cb03002 2406 struct md_rdev *rdev;
749c55e9
N
2407
2408 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2409 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1a0b7cd8
N
2410 for (m = 0; m < conf->copies; m++) {
2411 int dev = r10_bio->devs[m].devnum;
2412 rdev = conf->mirrors[dev].rdev;
2413 if (r10_bio->devs[m].bio == NULL)
2414 continue;
2415 if (test_bit(BIO_UPTODATE,
749c55e9 2416 &r10_bio->devs[m].bio->bi_flags)) {
749c55e9
N
2417 rdev_clear_badblocks(
2418 rdev,
2419 r10_bio->devs[m].addr,
2420 r10_bio->sectors);
1a0b7cd8
N
2421 } else {
2422 if (!rdev_set_badblocks(
2423 rdev,
2424 r10_bio->devs[m].addr,
2425 r10_bio->sectors, 0))
2426 md_error(conf->mddev, rdev);
749c55e9 2427 }
9ad1aefc
N
2428 rdev = conf->mirrors[dev].replacement;
2429 if (r10_bio->devs[m].repl_bio == NULL)
2430 continue;
2431 if (test_bit(BIO_UPTODATE,
2432 &r10_bio->devs[m].repl_bio->bi_flags)) {
2433 rdev_clear_badblocks(
2434 rdev,
2435 r10_bio->devs[m].addr,
2436 r10_bio->sectors);
2437 } else {
2438 if (!rdev_set_badblocks(
2439 rdev,
2440 r10_bio->devs[m].addr,
2441 r10_bio->sectors, 0))
2442 md_error(conf->mddev, rdev);
2443 }
1a0b7cd8 2444 }
749c55e9
N
2445 put_buf(r10_bio);
2446 } else {
bd870a16
N
2447 for (m = 0; m < conf->copies; m++) {
2448 int dev = r10_bio->devs[m].devnum;
2449 struct bio *bio = r10_bio->devs[m].bio;
2450 rdev = conf->mirrors[dev].rdev;
2451 if (bio == IO_MADE_GOOD) {
749c55e9
N
2452 rdev_clear_badblocks(
2453 rdev,
2454 r10_bio->devs[m].addr,
2455 r10_bio->sectors);
2456 rdev_dec_pending(rdev, conf->mddev);
bd870a16
N
2457 } else if (bio != NULL &&
2458 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2459 if (!narrow_write_error(r10_bio, m)) {
2460 md_error(conf->mddev, rdev);
2461 set_bit(R10BIO_Degraded,
2462 &r10_bio->state);
2463 }
2464 rdev_dec_pending(rdev, conf->mddev);
749c55e9 2465 }
475b0321
N
2466 bio = r10_bio->devs[m].repl_bio;
2467 rdev = conf->mirrors[dev].replacement;
4ca40c2c 2468 if (rdev && bio == IO_MADE_GOOD) {
475b0321
N
2469 rdev_clear_badblocks(
2470 rdev,
2471 r10_bio->devs[m].addr,
2472 r10_bio->sectors);
2473 rdev_dec_pending(rdev, conf->mddev);
2474 }
bd870a16
N
2475 }
2476 if (test_bit(R10BIO_WriteError,
2477 &r10_bio->state))
2478 close_write(r10_bio);
749c55e9
N
2479 raid_end_bio_io(r10_bio);
2480 }
2481}
2482
fd01b88c 2483static void raid10d(struct mddev *mddev)
1da177e4 2484{
9f2c9d12 2485 struct r10bio *r10_bio;
1da177e4 2486 unsigned long flags;
e879a879 2487 struct r10conf *conf = mddev->private;
1da177e4 2488 struct list_head *head = &conf->retry_list;
e1dfa0a2 2489 struct blk_plug plug;
1da177e4
LT
2490
2491 md_check_recovery(mddev);
1da177e4 2492
e1dfa0a2 2493 blk_start_plug(&plug);
1da177e4 2494 for (;;) {
6cce3b23 2495
7eaceacc 2496 flush_pending_writes(conf);
6cce3b23 2497
a35e63ef
N
2498 spin_lock_irqsave(&conf->device_lock, flags);
2499 if (list_empty(head)) {
2500 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 2501 break;
a35e63ef 2502 }
9f2c9d12 2503 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
1da177e4 2504 list_del(head->prev);
4443ae10 2505 conf->nr_queued--;
1da177e4
LT
2506 spin_unlock_irqrestore(&conf->device_lock, flags);
2507
2508 mddev = r10_bio->mddev;
070ec55d 2509 conf = mddev->private;
bd870a16
N
2510 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2511 test_bit(R10BIO_WriteError, &r10_bio->state))
749c55e9
N
2512 handle_write_completed(conf, r10_bio);
2513 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
1da177e4 2514 sync_request_write(mddev, r10_bio);
7eaceacc 2515 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
1da177e4 2516 recovery_request_write(mddev, r10_bio);
856e08e2 2517 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
560f8e55 2518 handle_read_error(mddev, r10_bio);
856e08e2
N
2519 else {
2520 /* just a partial read to be scheduled from a
2521 * separate context
2522 */
2523 int slot = r10_bio->read_slot;
2524 generic_make_request(r10_bio->devs[slot].bio);
2525 }
560f8e55 2526
1d9d5241 2527 cond_resched();
de393cde
N
2528 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2529 md_check_recovery(mddev);
1da177e4 2530 }
e1dfa0a2 2531 blk_finish_plug(&plug);
1da177e4
LT
2532}
2533
2534
e879a879 2535static int init_resync(struct r10conf *conf)
1da177e4
LT
2536{
2537 int buffs;
69335ef3 2538 int i;
1da177e4
LT
2539
2540 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
b6385483 2541 BUG_ON(conf->r10buf_pool);
69335ef3
N
2542 conf->have_replacement = 0;
2543 for (i = 0; i < conf->raid_disks; i++)
2544 if (conf->mirrors[i].replacement)
2545 conf->have_replacement = 1;
1da177e4
LT
2546 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2547 if (!conf->r10buf_pool)
2548 return -ENOMEM;
2549 conf->next_resync = 0;
2550 return 0;
2551}
2552
2553/*
2554 * perform a "sync" on one "block"
2555 *
2556 * We need to make sure that no normal I/O request - particularly write
2557 * requests - conflict with active sync requests.
2558 *
2559 * This is achieved by tracking pending requests and a 'barrier' concept
2560 * that can be installed to exclude normal IO requests.
2561 *
2562 * Resync and recovery are handled very differently.
2563 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2564 *
2565 * For resync, we iterate over virtual addresses, read all copies,
2566 * and update if there are differences. If only one copy is live,
2567 * skip it.
2568 * For recovery, we iterate over physical addresses, read a good
2569 * value for each non-in_sync drive, and over-write.
2570 *
2571 * So, for recovery we may have several outstanding complex requests for a
2572 * given address, one for each out-of-sync device. We model this by allocating
2573 * a number of r10_bio structures, one for each out-of-sync device.
2574 * As we setup these structures, we collect all bio's together into a list
2575 * which we then process collectively to add pages, and then process again
2576 * to pass to generic_make_request.
2577 *
2578 * The r10_bio structures are linked using a borrowed master_bio pointer.
2579 * This link is counted in ->remaining. When the r10_bio that points to NULL
2580 * has its remaining count decremented to 0, the whole complex operation
2581 * is complete.
2582 *
2583 */
2584
fd01b88c 2585static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
ab9d47e9 2586 int *skipped, int go_faster)
1da177e4 2587{
e879a879 2588 struct r10conf *conf = mddev->private;
9f2c9d12 2589 struct r10bio *r10_bio;
1da177e4
LT
2590 struct bio *biolist = NULL, *bio;
2591 sector_t max_sector, nr_sectors;
1da177e4 2592 int i;
6cce3b23 2593 int max_sync;
57dab0bd 2594 sector_t sync_blocks;
1da177e4
LT
2595 sector_t sectors_skipped = 0;
2596 int chunks_skipped = 0;
2597
2598 if (!conf->r10buf_pool)
2599 if (init_resync(conf))
57afd89f 2600 return 0;
1da177e4
LT
2601
2602 skipped:
58c0fed4 2603 max_sector = mddev->dev_sectors;
1da177e4
LT
2604 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2605 max_sector = mddev->resync_max_sectors;
2606 if (sector_nr >= max_sector) {
6cce3b23
N
2607 /* If we aborted, we need to abort the
2608 * sync on the 'current' bitmap chucks (there can
2609 * be several when recovering multiple devices).
2610 * as we may have started syncing it but not finished.
2611 * We can find the current address in
2612 * mddev->curr_resync, but for recovery,
2613 * we need to convert that to several
2614 * virtual addresses.
2615 */
2616 if (mddev->curr_resync < max_sector) { /* aborted */
2617 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2618 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2619 &sync_blocks, 1);
2620 else for (i=0; i<conf->raid_disks; i++) {
2621 sector_t sect =
2622 raid10_find_virt(conf, mddev->curr_resync, i);
2623 bitmap_end_sync(mddev->bitmap, sect,
2624 &sync_blocks, 1);
2625 }
9ad1aefc
N
2626 } else {
2627 /* completed sync */
2628 if ((!mddev->bitmap || conf->fullsync)
2629 && conf->have_replacement
2630 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2631 /* Completed a full sync so the replacements
2632 * are now fully recovered.
2633 */
2634 for (i = 0; i < conf->raid_disks; i++)
2635 if (conf->mirrors[i].replacement)
2636 conf->mirrors[i].replacement
2637 ->recovery_offset
2638 = MaxSector;
2639 }
6cce3b23 2640 conf->fullsync = 0;
9ad1aefc 2641 }
6cce3b23 2642 bitmap_close_sync(mddev->bitmap);
1da177e4 2643 close_sync(conf);
57afd89f 2644 *skipped = 1;
1da177e4
LT
2645 return sectors_skipped;
2646 }
2647 if (chunks_skipped >= conf->raid_disks) {
2648 /* if there has been nothing to do on any drive,
2649 * then there is nothing to do at all..
2650 */
57afd89f
N
2651 *skipped = 1;
2652 return (max_sector - sector_nr) + sectors_skipped;
1da177e4
LT
2653 }
2654
c6207277
N
2655 if (max_sector > mddev->resync_max)
2656 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2657
1da177e4
LT
2658 /* make sure whole request will fit in a chunk - if chunks
2659 * are meaningful
2660 */
2661 if (conf->near_copies < conf->raid_disks &&
2662 max_sector > (sector_nr | conf->chunk_mask))
2663 max_sector = (sector_nr | conf->chunk_mask) + 1;
2664 /*
2665 * If there is non-resync activity waiting for us then
2666 * put in a delay to throttle resync.
2667 */
0a27ec96 2668 if (!go_faster && conf->nr_waiting)
1da177e4 2669 msleep_interruptible(1000);
1da177e4
LT
2670
2671 /* Again, very different code for resync and recovery.
2672 * Both must result in an r10bio with a list of bios that
2673 * have bi_end_io, bi_sector, bi_bdev set,
2674 * and bi_private set to the r10bio.
2675 * For recovery, we may actually create several r10bios
2676 * with 2 bios in each, that correspond to the bios in the main one.
2677 * In this case, the subordinate r10bios link back through a
2678 * borrowed master_bio pointer, and the counter in the master
2679 * includes a ref from each subordinate.
2680 */
2681 /* First, we decide what to do and set ->bi_end_io
2682 * To end_sync_read if we want to read, and
2683 * end_sync_write if we will want to write.
2684 */
2685
6cce3b23 2686 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1da177e4
LT
2687 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2688 /* recovery... the complicated one */
e875ecea 2689 int j;
1da177e4
LT
2690 r10_bio = NULL;
2691
ab9d47e9
N
2692 for (i=0 ; i<conf->raid_disks; i++) {
2693 int still_degraded;
9f2c9d12 2694 struct r10bio *rb2;
ab9d47e9
N
2695 sector_t sect;
2696 int must_sync;
e875ecea 2697 int any_working;
24afd80d
N
2698 struct mirror_info *mirror = &conf->mirrors[i];
2699
2700 if ((mirror->rdev == NULL ||
2701 test_bit(In_sync, &mirror->rdev->flags))
2702 &&
2703 (mirror->replacement == NULL ||
2704 test_bit(Faulty,
2705 &mirror->replacement->flags)))
ab9d47e9 2706 continue;
1da177e4 2707
ab9d47e9
N
2708 still_degraded = 0;
2709 /* want to reconstruct this device */
2710 rb2 = r10_bio;
2711 sect = raid10_find_virt(conf, sector_nr, i);
24afd80d
N
2712 /* Unless we are doing a full sync, or a replacement
2713 * we only need to recover the block if it is set in
2714 * the bitmap
ab9d47e9
N
2715 */
2716 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2717 &sync_blocks, 1);
2718 if (sync_blocks < max_sync)
2719 max_sync = sync_blocks;
2720 if (!must_sync &&
24afd80d 2721 mirror->replacement == NULL &&
ab9d47e9
N
2722 !conf->fullsync) {
2723 /* yep, skip the sync_blocks here, but don't assume
2724 * that there will never be anything to do here
2725 */
2726 chunks_skipped = -1;
2727 continue;
2728 }
6cce3b23 2729
ab9d47e9
N
2730 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2731 raise_barrier(conf, rb2 != NULL);
2732 atomic_set(&r10_bio->remaining, 0);
18055569 2733
ab9d47e9
N
2734 r10_bio->master_bio = (struct bio*)rb2;
2735 if (rb2)
2736 atomic_inc(&rb2->remaining);
2737 r10_bio->mddev = mddev;
2738 set_bit(R10BIO_IsRecover, &r10_bio->state);
2739 r10_bio->sector = sect;
1da177e4 2740
ab9d47e9
N
2741 raid10_find_phys(conf, r10_bio);
2742
2743 /* Need to check if the array will still be
2744 * degraded
2745 */
2746 for (j=0; j<conf->raid_disks; j++)
2747 if (conf->mirrors[j].rdev == NULL ||
2748 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2749 still_degraded = 1;
87fc767b 2750 break;
1da177e4 2751 }
ab9d47e9
N
2752
2753 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2754 &sync_blocks, still_degraded);
2755
e875ecea 2756 any_working = 0;
ab9d47e9 2757 for (j=0; j<conf->copies;j++) {
e875ecea 2758 int k;
ab9d47e9 2759 int d = r10_bio->devs[j].devnum;
5e570289 2760 sector_t from_addr, to_addr;
3cb03002 2761 struct md_rdev *rdev;
40c356ce
N
2762 sector_t sector, first_bad;
2763 int bad_sectors;
ab9d47e9
N
2764 if (!conf->mirrors[d].rdev ||
2765 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2766 continue;
2767 /* This is where we read from */
e875ecea 2768 any_working = 1;
40c356ce
N
2769 rdev = conf->mirrors[d].rdev;
2770 sector = r10_bio->devs[j].addr;
2771
2772 if (is_badblock(rdev, sector, max_sync,
2773 &first_bad, &bad_sectors)) {
2774 if (first_bad > sector)
2775 max_sync = first_bad - sector;
2776 else {
2777 bad_sectors -= (sector
2778 - first_bad);
2779 if (max_sync > bad_sectors)
2780 max_sync = bad_sectors;
2781 continue;
2782 }
2783 }
ab9d47e9
N
2784 bio = r10_bio->devs[0].bio;
2785 bio->bi_next = biolist;
2786 biolist = bio;
2787 bio->bi_private = r10_bio;
2788 bio->bi_end_io = end_sync_read;
2789 bio->bi_rw = READ;
5e570289 2790 from_addr = r10_bio->devs[j].addr;
24afd80d
N
2791 bio->bi_sector = from_addr + rdev->data_offset;
2792 bio->bi_bdev = rdev->bdev;
2793 atomic_inc(&rdev->nr_pending);
2794 /* and we write to 'i' (if not in_sync) */
ab9d47e9
N
2795
2796 for (k=0; k<conf->copies; k++)
2797 if (r10_bio->devs[k].devnum == i)
2798 break;
2799 BUG_ON(k == conf->copies);
5e570289 2800 to_addr = r10_bio->devs[k].addr;
ab9d47e9 2801 r10_bio->devs[0].devnum = d;
5e570289 2802 r10_bio->devs[0].addr = from_addr;
ab9d47e9 2803 r10_bio->devs[1].devnum = i;
5e570289 2804 r10_bio->devs[1].addr = to_addr;
ab9d47e9 2805
24afd80d
N
2806 rdev = mirror->rdev;
2807 if (!test_bit(In_sync, &rdev->flags)) {
2808 bio = r10_bio->devs[1].bio;
2809 bio->bi_next = biolist;
2810 biolist = bio;
2811 bio->bi_private = r10_bio;
2812 bio->bi_end_io = end_sync_write;
2813 bio->bi_rw = WRITE;
2814 bio->bi_sector = to_addr
2815 + rdev->data_offset;
2816 bio->bi_bdev = rdev->bdev;
2817 atomic_inc(&r10_bio->remaining);
2818 } else
2819 r10_bio->devs[1].bio->bi_end_io = NULL;
2820
2821 /* and maybe write to replacement */
2822 bio = r10_bio->devs[1].repl_bio;
2823 if (bio)
2824 bio->bi_end_io = NULL;
2825 rdev = mirror->replacement;
2826 /* Note: if rdev != NULL, then bio
2827 * cannot be NULL as r10buf_pool_alloc will
2828 * have allocated it.
2829 * So the second test here is pointless.
2830 * But it keeps semantic-checkers happy, and
2831 * this comment keeps human reviewers
2832 * happy.
2833 */
2834 if (rdev == NULL || bio == NULL ||
2835 test_bit(Faulty, &rdev->flags))
2836 break;
2837 bio->bi_next = biolist;
2838 biolist = bio;
2839 bio->bi_private = r10_bio;
2840 bio->bi_end_io = end_sync_write;
2841 bio->bi_rw = WRITE;
2842 bio->bi_sector = to_addr + rdev->data_offset;
2843 bio->bi_bdev = rdev->bdev;
2844 atomic_inc(&r10_bio->remaining);
ab9d47e9
N
2845 break;
2846 }
2847 if (j == conf->copies) {
e875ecea
N
2848 /* Cannot recover, so abort the recovery or
2849 * record a bad block */
ab9d47e9
N
2850 put_buf(r10_bio);
2851 if (rb2)
2852 atomic_dec(&rb2->remaining);
2853 r10_bio = rb2;
e875ecea
N
2854 if (any_working) {
2855 /* problem is that there are bad blocks
2856 * on other device(s)
2857 */
2858 int k;
2859 for (k = 0; k < conf->copies; k++)
2860 if (r10_bio->devs[k].devnum == i)
2861 break;
24afd80d
N
2862 if (!test_bit(In_sync,
2863 &mirror->rdev->flags)
2864 && !rdev_set_badblocks(
2865 mirror->rdev,
2866 r10_bio->devs[k].addr,
2867 max_sync, 0))
2868 any_working = 0;
2869 if (mirror->replacement &&
2870 !rdev_set_badblocks(
2871 mirror->replacement,
e875ecea
N
2872 r10_bio->devs[k].addr,
2873 max_sync, 0))
2874 any_working = 0;
2875 }
2876 if (!any_working) {
2877 if (!test_and_set_bit(MD_RECOVERY_INTR,
2878 &mddev->recovery))
2879 printk(KERN_INFO "md/raid10:%s: insufficient "
2880 "working devices for recovery.\n",
2881 mdname(mddev));
24afd80d 2882 mirror->recovery_disabled
e875ecea
N
2883 = mddev->recovery_disabled;
2884 }
ab9d47e9 2885 break;
1da177e4 2886 }
ab9d47e9 2887 }
1da177e4
LT
2888 if (biolist == NULL) {
2889 while (r10_bio) {
9f2c9d12
N
2890 struct r10bio *rb2 = r10_bio;
2891 r10_bio = (struct r10bio*) rb2->master_bio;
1da177e4
LT
2892 rb2->master_bio = NULL;
2893 put_buf(rb2);
2894 }
2895 goto giveup;
2896 }
2897 } else {
2898 /* resync. Schedule a read for every block at this virt offset */
2899 int count = 0;
6cce3b23 2900
78200d45
N
2901 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2902
6cce3b23
N
2903 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2904 &sync_blocks, mddev->degraded) &&
ab9d47e9
N
2905 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2906 &mddev->recovery)) {
6cce3b23
N
2907 /* We can skip this block */
2908 *skipped = 1;
2909 return sync_blocks + sectors_skipped;
2910 }
2911 if (sync_blocks < max_sync)
2912 max_sync = sync_blocks;
1da177e4
LT
2913 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2914
1da177e4
LT
2915 r10_bio->mddev = mddev;
2916 atomic_set(&r10_bio->remaining, 0);
6cce3b23
N
2917 raise_barrier(conf, 0);
2918 conf->next_resync = sector_nr;
1da177e4
LT
2919
2920 r10_bio->master_bio = NULL;
2921 r10_bio->sector = sector_nr;
2922 set_bit(R10BIO_IsSync, &r10_bio->state);
2923 raid10_find_phys(conf, r10_bio);
2924 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2925
2926 for (i=0; i<conf->copies; i++) {
2927 int d = r10_bio->devs[i].devnum;
40c356ce
N
2928 sector_t first_bad, sector;
2929 int bad_sectors;
2930
9ad1aefc
N
2931 if (r10_bio->devs[i].repl_bio)
2932 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
2933
1da177e4
LT
2934 bio = r10_bio->devs[i].bio;
2935 bio->bi_end_io = NULL;
af03b8e4 2936 clear_bit(BIO_UPTODATE, &bio->bi_flags);
1da177e4 2937 if (conf->mirrors[d].rdev == NULL ||
b2d444d7 2938 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1da177e4 2939 continue;
40c356ce
N
2940 sector = r10_bio->devs[i].addr;
2941 if (is_badblock(conf->mirrors[d].rdev,
2942 sector, max_sync,
2943 &first_bad, &bad_sectors)) {
2944 if (first_bad > sector)
2945 max_sync = first_bad - sector;
2946 else {
2947 bad_sectors -= (sector - first_bad);
2948 if (max_sync > bad_sectors)
2949 max_sync = max_sync;
2950 continue;
2951 }
2952 }
1da177e4
LT
2953 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2954 atomic_inc(&r10_bio->remaining);
2955 bio->bi_next = biolist;
2956 biolist = bio;
2957 bio->bi_private = r10_bio;
2958 bio->bi_end_io = end_sync_read;
802ba064 2959 bio->bi_rw = READ;
40c356ce 2960 bio->bi_sector = sector +
1da177e4
LT
2961 conf->mirrors[d].rdev->data_offset;
2962 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2963 count++;
9ad1aefc
N
2964
2965 if (conf->mirrors[d].replacement == NULL ||
2966 test_bit(Faulty,
2967 &conf->mirrors[d].replacement->flags))
2968 continue;
2969
2970 /* Need to set up for writing to the replacement */
2971 bio = r10_bio->devs[i].repl_bio;
2972 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2973
2974 sector = r10_bio->devs[i].addr;
2975 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2976 bio->bi_next = biolist;
2977 biolist = bio;
2978 bio->bi_private = r10_bio;
2979 bio->bi_end_io = end_sync_write;
2980 bio->bi_rw = WRITE;
2981 bio->bi_sector = sector +
2982 conf->mirrors[d].replacement->data_offset;
2983 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
2984 count++;
1da177e4
LT
2985 }
2986
2987 if (count < 2) {
2988 for (i=0; i<conf->copies; i++) {
2989 int d = r10_bio->devs[i].devnum;
2990 if (r10_bio->devs[i].bio->bi_end_io)
ab9d47e9
N
2991 rdev_dec_pending(conf->mirrors[d].rdev,
2992 mddev);
9ad1aefc
N
2993 if (r10_bio->devs[i].repl_bio &&
2994 r10_bio->devs[i].repl_bio->bi_end_io)
2995 rdev_dec_pending(
2996 conf->mirrors[d].replacement,
2997 mddev);
1da177e4
LT
2998 }
2999 put_buf(r10_bio);
3000 biolist = NULL;
3001 goto giveup;
3002 }
3003 }
3004
3005 for (bio = biolist; bio ; bio=bio->bi_next) {
3006
3007 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3008 if (bio->bi_end_io)
3009 bio->bi_flags |= 1 << BIO_UPTODATE;
3010 bio->bi_vcnt = 0;
3011 bio->bi_idx = 0;
3012 bio->bi_phys_segments = 0;
1da177e4
LT
3013 bio->bi_size = 0;
3014 }
3015
3016 nr_sectors = 0;
6cce3b23
N
3017 if (sector_nr + max_sync < max_sector)
3018 max_sector = sector_nr + max_sync;
1da177e4
LT
3019 do {
3020 struct page *page;
3021 int len = PAGE_SIZE;
1da177e4
LT
3022 if (sector_nr + (len>>9) > max_sector)
3023 len = (max_sector - sector_nr) << 9;
3024 if (len == 0)
3025 break;
3026 for (bio= biolist ; bio ; bio=bio->bi_next) {
ab9d47e9 3027 struct bio *bio2;
1da177e4 3028 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
ab9d47e9
N
3029 if (bio_add_page(bio, page, len, 0))
3030 continue;
3031
3032 /* stop here */
3033 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3034 for (bio2 = biolist;
3035 bio2 && bio2 != bio;
3036 bio2 = bio2->bi_next) {
3037 /* remove last page from this bio */
3038 bio2->bi_vcnt--;
3039 bio2->bi_size -= len;
3040 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1da177e4 3041 }
ab9d47e9 3042 goto bio_full;
1da177e4
LT
3043 }
3044 nr_sectors += len>>9;
3045 sector_nr += len>>9;
3046 } while (biolist->bi_vcnt < RESYNC_PAGES);
3047 bio_full:
3048 r10_bio->sectors = nr_sectors;
3049
3050 while (biolist) {
3051 bio = biolist;
3052 biolist = biolist->bi_next;
3053
3054 bio->bi_next = NULL;
3055 r10_bio = bio->bi_private;
3056 r10_bio->sectors = nr_sectors;
3057
3058 if (bio->bi_end_io == end_sync_read) {
3059 md_sync_acct(bio->bi_bdev, nr_sectors);
3060 generic_make_request(bio);
3061 }
3062 }
3063
57afd89f
N
3064 if (sectors_skipped)
3065 /* pretend they weren't skipped, it makes
3066 * no important difference in this case
3067 */
3068 md_done_sync(mddev, sectors_skipped, 1);
3069
1da177e4
LT
3070 return sectors_skipped + nr_sectors;
3071 giveup:
3072 /* There is nowhere to write, so all non-sync
e875ecea
N
3073 * drives must be failed or in resync, all drives
3074 * have a bad block, so try the next chunk...
1da177e4 3075 */
09b4068a
N
3076 if (sector_nr + max_sync < max_sector)
3077 max_sector = sector_nr + max_sync;
3078
3079 sectors_skipped += (max_sector - sector_nr);
1da177e4
LT
3080 chunks_skipped ++;
3081 sector_nr = max_sector;
1da177e4 3082 goto skipped;
1da177e4
LT
3083}
3084
80c3a6ce 3085static sector_t
fd01b88c 3086raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce
DW
3087{
3088 sector_t size;
e879a879 3089 struct r10conf *conf = mddev->private;
80c3a6ce
DW
3090
3091 if (!raid_disks)
84707f38 3092 raid_disks = conf->raid_disks;
80c3a6ce 3093 if (!sectors)
dab8b292 3094 sectors = conf->dev_sectors;
80c3a6ce
DW
3095
3096 size = sectors >> conf->chunk_shift;
3097 sector_div(size, conf->far_copies);
3098 size = size * raid_disks;
3099 sector_div(size, conf->near_copies);
3100
3101 return size << conf->chunk_shift;
3102}
3103
dab8b292 3104
e879a879 3105static struct r10conf *setup_conf(struct mddev *mddev)
1da177e4 3106{
e879a879 3107 struct r10conf *conf = NULL;
c93983bf 3108 int nc, fc, fo;
1da177e4 3109 sector_t stride, size;
dab8b292 3110 int err = -EINVAL;
1da177e4 3111
f73ea873
MT
3112 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
3113 !is_power_of_2(mddev->new_chunk_sectors)) {
128595ed
N
3114 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3115 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3116 mdname(mddev), PAGE_SIZE);
dab8b292 3117 goto out;
1da177e4 3118 }
2604b703 3119
f73ea873
MT
3120 nc = mddev->new_layout & 255;
3121 fc = (mddev->new_layout >> 8) & 255;
3122 fo = mddev->new_layout & (1<<16);
dab8b292 3123
1da177e4 3124 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
f73ea873 3125 (mddev->new_layout >> 17)) {
128595ed 3126 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
f73ea873 3127 mdname(mddev), mddev->new_layout);
1da177e4
LT
3128 goto out;
3129 }
dab8b292
TM
3130
3131 err = -ENOMEM;
e879a879 3132 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
dab8b292 3133 if (!conf)
1da177e4 3134 goto out;
dab8b292 3135
4443ae10 3136 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
dab8b292
TM
3137 GFP_KERNEL);
3138 if (!conf->mirrors)
3139 goto out;
4443ae10
N
3140
3141 conf->tmppage = alloc_page(GFP_KERNEL);
3142 if (!conf->tmppage)
dab8b292
TM
3143 goto out;
3144
1da177e4 3145
64a742bc 3146 conf->raid_disks = mddev->raid_disks;
1da177e4
LT
3147 conf->near_copies = nc;
3148 conf->far_copies = fc;
3149 conf->copies = nc*fc;
c93983bf 3150 conf->far_offset = fo;
dab8b292
TM
3151 conf->chunk_mask = mddev->new_chunk_sectors - 1;
3152 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
3153
3154 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3155 r10bio_pool_free, conf);
3156 if (!conf->r10bio_pool)
3157 goto out;
3158
58c0fed4 3159 size = mddev->dev_sectors >> conf->chunk_shift;
64a742bc
N
3160 sector_div(size, fc);
3161 size = size * conf->raid_disks;
3162 sector_div(size, nc);
3163 /* 'size' is now the number of chunks in the array */
3164 /* calculate "used chunks per device" in 'stride' */
3165 stride = size * conf->copies;
af03b8e4
N
3166
3167 /* We need to round up when dividing by raid_disks to
3168 * get the stride size.
3169 */
3170 stride += conf->raid_disks - 1;
64a742bc 3171 sector_div(stride, conf->raid_disks);
dab8b292
TM
3172
3173 conf->dev_sectors = stride << conf->chunk_shift;
64a742bc 3174
c93983bf 3175 if (fo)
64a742bc
N
3176 stride = 1;
3177 else
c93983bf 3178 sector_div(stride, fc);
64a742bc
N
3179 conf->stride = stride << conf->chunk_shift;
3180
1da177e4 3181
e7e72bf6 3182 spin_lock_init(&conf->device_lock);
dab8b292
TM
3183 INIT_LIST_HEAD(&conf->retry_list);
3184
3185 spin_lock_init(&conf->resync_lock);
3186 init_waitqueue_head(&conf->wait_barrier);
3187
3188 conf->thread = md_register_thread(raid10d, mddev, NULL);
3189 if (!conf->thread)
3190 goto out;
3191
dab8b292
TM
3192 conf->mddev = mddev;
3193 return conf;
3194
3195 out:
128595ed 3196 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
dab8b292
TM
3197 mdname(mddev));
3198 if (conf) {
3199 if (conf->r10bio_pool)
3200 mempool_destroy(conf->r10bio_pool);
3201 kfree(conf->mirrors);
3202 safe_put_page(conf->tmppage);
3203 kfree(conf);
3204 }
3205 return ERR_PTR(err);
3206}
3207
fd01b88c 3208static int run(struct mddev *mddev)
dab8b292 3209{
e879a879 3210 struct r10conf *conf;
dab8b292 3211 int i, disk_idx, chunk_size;
0f6d02d5 3212 struct mirror_info *disk;
3cb03002 3213 struct md_rdev *rdev;
dab8b292
TM
3214 sector_t size;
3215
3216 /*
3217 * copy the already verified devices into our private RAID10
3218 * bookkeeping area. [whatever we allocate in run(),
3219 * should be freed in stop()]
3220 */
3221
3222 if (mddev->private == NULL) {
3223 conf = setup_conf(mddev);
3224 if (IS_ERR(conf))
3225 return PTR_ERR(conf);
3226 mddev->private = conf;
3227 }
3228 conf = mddev->private;
3229 if (!conf)
3230 goto out;
3231
dab8b292
TM
3232 mddev->thread = conf->thread;
3233 conf->thread = NULL;
3234
8f6c2e4b
MP
3235 chunk_size = mddev->chunk_sectors << 9;
3236 blk_queue_io_min(mddev->queue, chunk_size);
3237 if (conf->raid_disks % conf->near_copies)
3238 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
3239 else
3240 blk_queue_io_opt(mddev->queue, chunk_size *
3241 (conf->raid_disks / conf->near_copies));
3242
159ec1fc 3243 list_for_each_entry(rdev, &mddev->disks, same_set) {
34b343cf 3244
1da177e4 3245 disk_idx = rdev->raid_disk;
84707f38 3246 if (disk_idx >= conf->raid_disks
1da177e4
LT
3247 || disk_idx < 0)
3248 continue;
3249 disk = conf->mirrors + disk_idx;
3250
56a2559b
N
3251 if (test_bit(Replacement, &rdev->flags)) {
3252 if (disk->replacement)
3253 goto out_free_conf;
3254 disk->replacement = rdev;
3255 } else {
3256 if (disk->rdev)
3257 goto out_free_conf;
3258 disk->rdev = rdev;
3259 }
3260
8f6c2e4b
MP
3261 disk_stack_limits(mddev->gendisk, rdev->bdev,
3262 rdev->data_offset << 9);
1da177e4 3263 /* as we don't honour merge_bvec_fn, we must never risk
627a2d3c
N
3264 * violating it, so limit max_segments to 1 lying
3265 * within a single page.
1da177e4 3266 */
627a2d3c
N
3267 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
3268 blk_queue_max_segments(mddev->queue, 1);
3269 blk_queue_segment_boundary(mddev->queue,
3270 PAGE_CACHE_SIZE - 1);
3271 }
1da177e4
LT
3272
3273 disk->head_position = 0;
1da177e4 3274 }
6d508242 3275 /* need to check that every block has at least one working mirror */
700c7213 3276 if (!enough(conf, -1)) {
128595ed 3277 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
6d508242 3278 mdname(mddev));
1da177e4
LT
3279 goto out_free_conf;
3280 }
3281
3282 mddev->degraded = 0;
3283 for (i = 0; i < conf->raid_disks; i++) {
3284
3285 disk = conf->mirrors + i;
3286
56a2559b
N
3287 if (!disk->rdev && disk->replacement) {
3288 /* The replacement is all we have - use it */
3289 disk->rdev = disk->replacement;
3290 disk->replacement = NULL;
3291 clear_bit(Replacement, &disk->rdev->flags);
3292 }
3293
5fd6c1dc 3294 if (!disk->rdev ||
2e333e89 3295 !test_bit(In_sync, &disk->rdev->flags)) {
1da177e4
LT
3296 disk->head_position = 0;
3297 mddev->degraded++;
8c2e870a
NB
3298 if (disk->rdev)
3299 conf->fullsync = 1;
1da177e4 3300 }
d890fa2b 3301 disk->recovery_disabled = mddev->recovery_disabled - 1;
1da177e4
LT
3302 }
3303
8c6ac868 3304 if (mddev->recovery_cp != MaxSector)
128595ed 3305 printk(KERN_NOTICE "md/raid10:%s: not clean"
8c6ac868
AN
3306 " -- starting background reconstruction\n",
3307 mdname(mddev));
1da177e4 3308 printk(KERN_INFO
128595ed 3309 "md/raid10:%s: active with %d out of %d devices\n",
84707f38
N
3310 mdname(mddev), conf->raid_disks - mddev->degraded,
3311 conf->raid_disks);
1da177e4
LT
3312 /*
3313 * Ok, everything is just fine now
3314 */
dab8b292
TM
3315 mddev->dev_sectors = conf->dev_sectors;
3316 size = raid10_size(mddev, 0, 0);
3317 md_set_array_sectors(mddev, size);
3318 mddev->resync_max_sectors = size;
1da177e4 3319
0d129228
N
3320 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3321 mddev->queue->backing_dev_info.congested_data = mddev;
7a5febe9 3322
1da177e4
LT
3323 /* Calculate max read-ahead size.
3324 * We need to readahead at least twice a whole stripe....
3325 * maybe...
3326 */
3327 {
9d8f0363
AN
3328 int stripe = conf->raid_disks *
3329 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
1da177e4
LT
3330 stripe /= conf->near_copies;
3331 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3332 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3333 }
3334
84707f38 3335 if (conf->near_copies < conf->raid_disks)
1da177e4 3336 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
a91a2785
MP
3337
3338 if (md_integrity_register(mddev))
3339 goto out_free_conf;
3340
1da177e4
LT
3341 return 0;
3342
3343out_free_conf:
01f96c0a 3344 md_unregister_thread(&mddev->thread);
1da177e4
LT
3345 if (conf->r10bio_pool)
3346 mempool_destroy(conf->r10bio_pool);
1345b1d8 3347 safe_put_page(conf->tmppage);
990a8baf 3348 kfree(conf->mirrors);
1da177e4
LT
3349 kfree(conf);
3350 mddev->private = NULL;
3351out:
3352 return -EIO;
3353}
3354
fd01b88c 3355static int stop(struct mddev *mddev)
1da177e4 3356{
e879a879 3357 struct r10conf *conf = mddev->private;
1da177e4 3358
409c57f3
N
3359 raise_barrier(conf, 0);
3360 lower_barrier(conf);
3361
01f96c0a 3362 md_unregister_thread(&mddev->thread);
1da177e4
LT
3363 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3364 if (conf->r10bio_pool)
3365 mempool_destroy(conf->r10bio_pool);
990a8baf 3366 kfree(conf->mirrors);
1da177e4
LT
3367 kfree(conf);
3368 mddev->private = NULL;
3369 return 0;
3370}
3371
fd01b88c 3372static void raid10_quiesce(struct mddev *mddev, int state)
6cce3b23 3373{
e879a879 3374 struct r10conf *conf = mddev->private;
6cce3b23
N
3375
3376 switch(state) {
3377 case 1:
3378 raise_barrier(conf, 0);
3379 break;
3380 case 0:
3381 lower_barrier(conf);
3382 break;
3383 }
6cce3b23 3384}
1da177e4 3385
fd01b88c 3386static void *raid10_takeover_raid0(struct mddev *mddev)
dab8b292 3387{
3cb03002 3388 struct md_rdev *rdev;
e879a879 3389 struct r10conf *conf;
dab8b292
TM
3390
3391 if (mddev->degraded > 0) {
128595ed
N
3392 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3393 mdname(mddev));
dab8b292
TM
3394 return ERR_PTR(-EINVAL);
3395 }
3396
dab8b292
TM
3397 /* Set new parameters */
3398 mddev->new_level = 10;
3399 /* new layout: far_copies = 1, near_copies = 2 */
3400 mddev->new_layout = (1<<8) + 2;
3401 mddev->new_chunk_sectors = mddev->chunk_sectors;
3402 mddev->delta_disks = mddev->raid_disks;
dab8b292
TM
3403 mddev->raid_disks *= 2;
3404 /* make sure it will be not marked as dirty */
3405 mddev->recovery_cp = MaxSector;
3406
3407 conf = setup_conf(mddev);
02214dc5 3408 if (!IS_ERR(conf)) {
e93f68a1
N
3409 list_for_each_entry(rdev, &mddev->disks, same_set)
3410 if (rdev->raid_disk >= 0)
3411 rdev->new_raid_disk = rdev->raid_disk * 2;
02214dc5
KW
3412 conf->barrier = 1;
3413 }
3414
dab8b292
TM
3415 return conf;
3416}
3417
fd01b88c 3418static void *raid10_takeover(struct mddev *mddev)
dab8b292 3419{
e373ab10 3420 struct r0conf *raid0_conf;
dab8b292
TM
3421
3422 /* raid10 can take over:
3423 * raid0 - providing it has only two drives
3424 */
3425 if (mddev->level == 0) {
3426 /* for raid0 takeover only one zone is supported */
e373ab10
N
3427 raid0_conf = mddev->private;
3428 if (raid0_conf->nr_strip_zones > 1) {
128595ed
N
3429 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3430 " with more than one zone.\n",
3431 mdname(mddev));
dab8b292
TM
3432 return ERR_PTR(-EINVAL);
3433 }
3434 return raid10_takeover_raid0(mddev);
3435 }
3436 return ERR_PTR(-EINVAL);
3437}
3438
84fc4b56 3439static struct md_personality raid10_personality =
1da177e4
LT
3440{
3441 .name = "raid10",
2604b703 3442 .level = 10,
1da177e4
LT
3443 .owner = THIS_MODULE,
3444 .make_request = make_request,
3445 .run = run,
3446 .stop = stop,
3447 .status = status,
3448 .error_handler = error,
3449 .hot_add_disk = raid10_add_disk,
3450 .hot_remove_disk= raid10_remove_disk,
3451 .spare_active = raid10_spare_active,
3452 .sync_request = sync_request,
6cce3b23 3453 .quiesce = raid10_quiesce,
80c3a6ce 3454 .size = raid10_size,
dab8b292 3455 .takeover = raid10_takeover,
1da177e4
LT
3456};
3457
3458static int __init raid_init(void)
3459{
2604b703 3460 return register_md_personality(&raid10_personality);
1da177e4
LT
3461}
3462
3463static void raid_exit(void)
3464{
2604b703 3465 unregister_md_personality(&raid10_personality);
1da177e4
LT
3466}
3467
3468module_init(raid_init);
3469module_exit(raid_exit);
3470MODULE_LICENSE("GPL");
0efb9e61 3471MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
1da177e4 3472MODULE_ALIAS("md-personality-9"); /* RAID10 */
d9d166c2 3473MODULE_ALIAS("md-raid10");
2604b703 3474MODULE_ALIAS("md-level-10");
34db0cd6
N
3475
3476module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);