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