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