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