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