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