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