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