USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / md / raid10.c
blob6137d0041d4d49e88b60d88d78909377d3b2e41a
1 /*
2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
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.
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.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
27 #include "md.h"
28 #include "raid10.h"
29 #include "raid0.h"
30 #include "bitmap.h"
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
35 * chunk_size
36 * raid_disks
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
39 * far_offset (stored in bit 16 of layout )
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
47 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
48 * drive.
49 * near_copies and far_copies must be at least one, and their product is at most
50 * raid_disks.
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
58 * Number of guaranteed r10bios in case of extreme VM load:
60 #define NR_RAID10_BIOS 256
62 /* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
64 * for writeback.
66 static int max_queued_requests = 1024;
68 static void allow_barrier(struct r10conf *conf);
69 static void lower_barrier(struct r10conf *conf);
70 static int enough(struct r10conf *conf, int ignore);
72 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
74 struct r10conf *conf = data;
75 int size = offsetof(struct r10bio, devs[conf->copies]);
77 /* allocate a r10bio with room for raid_disks entries in the
78 * bios array */
79 return kzalloc(size, gfp_flags);
82 static void r10bio_pool_free(void *r10_bio, void *data)
84 kfree(r10_bio);
87 /* Maximum size of each resync request */
88 #define RESYNC_BLOCK_SIZE (64*1024)
89 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
90 /* amount of memory to reserve for resync requests */
91 #define RESYNC_WINDOW (1024*1024)
92 /* maximum number of concurrent requests, memory permitting */
93 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
96 * When performing a resync, we need to read and compare, so
97 * we need as many pages are there are copies.
98 * When performing a recovery, we need 2 bios, one for read,
99 * one for write (we recover only one drive per r10buf)
102 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
104 struct r10conf *conf = data;
105 struct page *page;
106 struct r10bio *r10_bio;
107 struct bio *bio;
108 int i, j;
109 int nalloc;
111 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
112 if (!r10_bio)
113 return NULL;
115 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
116 nalloc = conf->copies; /* resync */
117 else
118 nalloc = 2; /* recovery */
121 * Allocate bios.
123 for (j = nalloc ; j-- ; ) {
124 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
125 if (!bio)
126 goto out_free_bio;
127 r10_bio->devs[j].bio = bio;
128 if (!conf->have_replacement)
129 continue;
130 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
131 if (!bio)
132 goto out_free_bio;
133 r10_bio->devs[j].repl_bio = bio;
136 * Allocate RESYNC_PAGES data pages and attach them
137 * where needed.
139 for (j = 0 ; j < nalloc; j++) {
140 struct bio *rbio = r10_bio->devs[j].repl_bio;
141 bio = r10_bio->devs[j].bio;
142 for (i = 0; i < RESYNC_PAGES; i++) {
143 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
144 &conf->mddev->recovery)) {
145 /* we can share bv_page's during recovery */
146 struct bio *rbio = r10_bio->devs[0].bio;
147 page = rbio->bi_io_vec[i].bv_page;
148 get_page(page);
149 } else
150 page = alloc_page(gfp_flags);
151 if (unlikely(!page))
152 goto out_free_pages;
154 bio->bi_io_vec[i].bv_page = page;
155 if (rbio)
156 rbio->bi_io_vec[i].bv_page = page;
160 return r10_bio;
162 out_free_pages:
163 for ( ; i > 0 ; i--)
164 safe_put_page(bio->bi_io_vec[i-1].bv_page);
165 while (j--)
166 for (i = 0; i < RESYNC_PAGES ; i++)
167 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
168 j = -1;
169 out_free_bio:
170 while (++j < nalloc) {
171 bio_put(r10_bio->devs[j].bio);
172 if (r10_bio->devs[j].repl_bio)
173 bio_put(r10_bio->devs[j].repl_bio);
175 r10bio_pool_free(r10_bio, conf);
176 return NULL;
179 static void r10buf_pool_free(void *__r10_bio, void *data)
181 int i;
182 struct r10conf *conf = data;
183 struct r10bio *r10bio = __r10_bio;
184 int j;
186 for (j=0; j < conf->copies; j++) {
187 struct bio *bio = r10bio->devs[j].bio;
188 if (bio) {
189 for (i = 0; i < RESYNC_PAGES; i++) {
190 safe_put_page(bio->bi_io_vec[i].bv_page);
191 bio->bi_io_vec[i].bv_page = NULL;
193 bio_put(bio);
195 bio = r10bio->devs[j].repl_bio;
196 if (bio)
197 bio_put(bio);
199 r10bio_pool_free(r10bio, conf);
202 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
204 int i;
206 for (i = 0; i < conf->copies; i++) {
207 struct bio **bio = & r10_bio->devs[i].bio;
208 if (!BIO_SPECIAL(*bio))
209 bio_put(*bio);
210 *bio = NULL;
211 bio = &r10_bio->devs[i].repl_bio;
212 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
213 bio_put(*bio);
214 *bio = NULL;
218 static void free_r10bio(struct r10bio *r10_bio)
220 struct r10conf *conf = r10_bio->mddev->private;
222 put_all_bios(conf, r10_bio);
223 mempool_free(r10_bio, conf->r10bio_pool);
226 static void put_buf(struct r10bio *r10_bio)
228 struct r10conf *conf = r10_bio->mddev->private;
230 mempool_free(r10_bio, conf->r10buf_pool);
232 lower_barrier(conf);
235 static void reschedule_retry(struct r10bio *r10_bio)
237 unsigned long flags;
238 struct mddev *mddev = r10_bio->mddev;
239 struct r10conf *conf = mddev->private;
241 spin_lock_irqsave(&conf->device_lock, flags);
242 list_add(&r10_bio->retry_list, &conf->retry_list);
243 conf->nr_queued ++;
244 spin_unlock_irqrestore(&conf->device_lock, flags);
246 /* wake up frozen array... */
247 wake_up(&conf->wait_barrier);
249 md_wakeup_thread(mddev->thread);
253 * raid_end_bio_io() is called when we have finished servicing a mirrored
254 * operation and are ready to return a success/failure code to the buffer
255 * cache layer.
257 static void raid_end_bio_io(struct r10bio *r10_bio)
259 struct bio *bio = r10_bio->master_bio;
260 int done;
261 struct r10conf *conf = r10_bio->mddev->private;
263 if (bio->bi_phys_segments) {
264 unsigned long flags;
265 spin_lock_irqsave(&conf->device_lock, flags);
266 bio->bi_phys_segments--;
267 done = (bio->bi_phys_segments == 0);
268 spin_unlock_irqrestore(&conf->device_lock, flags);
269 } else
270 done = 1;
271 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
272 clear_bit(BIO_UPTODATE, &bio->bi_flags);
273 if (done) {
274 bio_endio(bio, 0);
276 * Wake up any possible resync thread that waits for the device
277 * to go idle.
279 allow_barrier(conf);
281 free_r10bio(r10_bio);
285 * Update disk head position estimator based on IRQ completion info.
287 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
289 struct r10conf *conf = r10_bio->mddev->private;
291 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
292 r10_bio->devs[slot].addr + (r10_bio->sectors);
296 * Find the disk number which triggered given bio
298 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
299 struct bio *bio, int *slotp, int *replp)
301 int slot;
302 int repl = 0;
304 for (slot = 0; slot < conf->copies; slot++) {
305 if (r10_bio->devs[slot].bio == bio)
306 break;
307 if (r10_bio->devs[slot].repl_bio == bio) {
308 repl = 1;
309 break;
313 BUG_ON(slot == conf->copies);
314 update_head_pos(slot, r10_bio);
316 if (slotp)
317 *slotp = slot;
318 if (replp)
319 *replp = repl;
320 return r10_bio->devs[slot].devnum;
323 static void raid10_end_read_request(struct bio *bio, int error)
325 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
326 struct r10bio *r10_bio = bio->bi_private;
327 int slot, dev;
328 struct md_rdev *rdev;
329 struct r10conf *conf = r10_bio->mddev->private;
332 slot = r10_bio->read_slot;
333 dev = r10_bio->devs[slot].devnum;
334 rdev = r10_bio->devs[slot].rdev;
336 * this branch is our 'one mirror IO has finished' event handler:
338 update_head_pos(slot, r10_bio);
340 if (uptodate) {
342 * Set R10BIO_Uptodate in our master bio, so that
343 * we will return a good error code to the higher
344 * levels even if IO on some other mirrored buffer fails.
346 * The 'master' represents the composite IO operation to
347 * user-side. So if something waits for IO, then it will
348 * wait for the 'master' bio.
350 set_bit(R10BIO_Uptodate, &r10_bio->state);
351 } else {
352 /* If all other devices that store this block have
353 * failed, we want to return the error upwards rather
354 * than fail the last device. Here we redefine
355 * "uptodate" to mean "Don't want to retry"
357 unsigned long flags;
358 spin_lock_irqsave(&conf->device_lock, flags);
359 if (!enough(conf, rdev->raid_disk))
360 uptodate = 1;
361 spin_unlock_irqrestore(&conf->device_lock, flags);
363 if (uptodate) {
364 raid_end_bio_io(r10_bio);
365 rdev_dec_pending(rdev, conf->mddev);
366 } else {
368 * oops, read error - keep the refcount on the rdev
370 char b[BDEVNAME_SIZE];
371 printk_ratelimited(KERN_ERR
372 "md/raid10:%s: %s: rescheduling sector %llu\n",
373 mdname(conf->mddev),
374 bdevname(rdev->bdev, b),
375 (unsigned long long)r10_bio->sector);
376 set_bit(R10BIO_ReadError, &r10_bio->state);
377 reschedule_retry(r10_bio);
381 static void close_write(struct r10bio *r10_bio)
383 /* clear the bitmap if all writes complete successfully */
384 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
385 r10_bio->sectors,
386 !test_bit(R10BIO_Degraded, &r10_bio->state),
388 md_write_end(r10_bio->mddev);
391 static void one_write_done(struct r10bio *r10_bio)
393 if (atomic_dec_and_test(&r10_bio->remaining)) {
394 if (test_bit(R10BIO_WriteError, &r10_bio->state))
395 reschedule_retry(r10_bio);
396 else {
397 close_write(r10_bio);
398 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
399 reschedule_retry(r10_bio);
400 else
401 raid_end_bio_io(r10_bio);
406 static void raid10_end_write_request(struct bio *bio, int error)
408 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
409 struct r10bio *r10_bio = bio->bi_private;
410 int dev;
411 int dec_rdev = 1;
412 struct r10conf *conf = r10_bio->mddev->private;
413 int slot, repl;
414 struct md_rdev *rdev = NULL;
416 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
418 if (repl)
419 rdev = conf->mirrors[dev].replacement;
420 if (!rdev) {
421 smp_rmb();
422 repl = 0;
423 rdev = conf->mirrors[dev].rdev;
426 * this branch is our 'one mirror IO has finished' event handler:
428 if (!uptodate) {
429 if (repl)
430 /* Never record new bad blocks to replacement,
431 * just fail it.
433 md_error(rdev->mddev, rdev);
434 else {
435 set_bit(WriteErrorSeen, &rdev->flags);
436 if (!test_and_set_bit(WantReplacement, &rdev->flags))
437 set_bit(MD_RECOVERY_NEEDED,
438 &rdev->mddev->recovery);
439 set_bit(R10BIO_WriteError, &r10_bio->state);
440 dec_rdev = 0;
442 } else {
444 * Set R10BIO_Uptodate in our master bio, so that
445 * we will return a good error code for to the higher
446 * levels even if IO on some other mirrored buffer fails.
448 * The 'master' represents the composite IO operation to
449 * user-side. So if something waits for IO, then it will
450 * wait for the 'master' bio.
452 sector_t first_bad;
453 int bad_sectors;
455 set_bit(R10BIO_Uptodate, &r10_bio->state);
457 /* Maybe we can clear some bad blocks. */
458 if (is_badblock(rdev,
459 r10_bio->devs[slot].addr,
460 r10_bio->sectors,
461 &first_bad, &bad_sectors)) {
462 bio_put(bio);
463 if (repl)
464 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
465 else
466 r10_bio->devs[slot].bio = IO_MADE_GOOD;
467 dec_rdev = 0;
468 set_bit(R10BIO_MadeGood, &r10_bio->state);
474 * Let's see if all mirrored write operations have finished
475 * already.
477 one_write_done(r10_bio);
478 if (dec_rdev)
479 rdev_dec_pending(rdev, conf->mddev);
483 * RAID10 layout manager
484 * As well as the chunksize and raid_disks count, there are two
485 * parameters: near_copies and far_copies.
486 * near_copies * far_copies must be <= raid_disks.
487 * Normally one of these will be 1.
488 * If both are 1, we get raid0.
489 * If near_copies == raid_disks, we get raid1.
491 * Chunks are laid out in raid0 style with near_copies copies of the
492 * first chunk, followed by near_copies copies of the next chunk and
493 * so on.
494 * If far_copies > 1, then after 1/far_copies of the array has been assigned
495 * as described above, we start again with a device offset of near_copies.
496 * So we effectively have another copy of the whole array further down all
497 * the drives, but with blocks on different drives.
498 * With this layout, and block is never stored twice on the one device.
500 * raid10_find_phys finds the sector offset of a given virtual sector
501 * on each device that it is on.
503 * raid10_find_virt does the reverse mapping, from a device and a
504 * sector offset to a virtual address
507 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
509 int n,f;
510 sector_t sector;
511 sector_t chunk;
512 sector_t stripe;
513 int dev;
515 int slot = 0;
517 /* now calculate first sector/dev */
518 chunk = r10bio->sector >> conf->chunk_shift;
519 sector = r10bio->sector & conf->chunk_mask;
521 chunk *= conf->near_copies;
522 stripe = chunk;
523 dev = sector_div(stripe, conf->raid_disks);
524 if (conf->far_offset)
525 stripe *= conf->far_copies;
527 sector += stripe << conf->chunk_shift;
529 /* and calculate all the others */
530 for (n=0; n < conf->near_copies; n++) {
531 int d = dev;
532 sector_t s = sector;
533 r10bio->devs[slot].addr = sector;
534 r10bio->devs[slot].devnum = d;
535 slot++;
537 for (f = 1; f < conf->far_copies; f++) {
538 d += conf->near_copies;
539 if (d >= conf->raid_disks)
540 d -= conf->raid_disks;
541 s += conf->stride;
542 r10bio->devs[slot].devnum = d;
543 r10bio->devs[slot].addr = s;
544 slot++;
546 dev++;
547 if (dev >= conf->raid_disks) {
548 dev = 0;
549 sector += (conf->chunk_mask + 1);
552 BUG_ON(slot != conf->copies);
555 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
557 sector_t offset, chunk, vchunk;
559 offset = sector & conf->chunk_mask;
560 if (conf->far_offset) {
561 int fc;
562 chunk = sector >> conf->chunk_shift;
563 fc = sector_div(chunk, conf->far_copies);
564 dev -= fc * conf->near_copies;
565 if (dev < 0)
566 dev += conf->raid_disks;
567 } else {
568 while (sector >= conf->stride) {
569 sector -= conf->stride;
570 if (dev < conf->near_copies)
571 dev += conf->raid_disks - conf->near_copies;
572 else
573 dev -= conf->near_copies;
575 chunk = sector >> conf->chunk_shift;
577 vchunk = chunk * conf->raid_disks + dev;
578 sector_div(vchunk, conf->near_copies);
579 return (vchunk << conf->chunk_shift) + offset;
583 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
584 * @q: request queue
585 * @bvm: properties of new bio
586 * @biovec: the request that could be merged to it.
588 * Return amount of bytes we can accept at this offset
589 * This requires checking for end-of-chunk if near_copies != raid_disks,
590 * and for subordinate merge_bvec_fns if merge_check_needed.
592 static int raid10_mergeable_bvec(struct request_queue *q,
593 struct bvec_merge_data *bvm,
594 struct bio_vec *biovec)
596 struct mddev *mddev = q->queuedata;
597 struct r10conf *conf = mddev->private;
598 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
599 int max;
600 unsigned int chunk_sectors = mddev->chunk_sectors;
601 unsigned int bio_sectors = bvm->bi_size >> 9;
603 if (conf->near_copies < conf->raid_disks) {
604 max = (chunk_sectors - ((sector & (chunk_sectors - 1))
605 + bio_sectors)) << 9;
606 if (max < 0)
607 /* bio_add cannot handle a negative return */
608 max = 0;
609 if (max <= biovec->bv_len && bio_sectors == 0)
610 return biovec->bv_len;
611 } else
612 max = biovec->bv_len;
614 if (mddev->merge_check_needed) {
615 struct {
616 struct r10bio r10_bio;
617 struct r10dev devs[conf->copies];
618 } on_stack;
619 struct r10bio *r10_bio = &on_stack.r10_bio;
620 int s;
621 r10_bio->sector = sector;
622 raid10_find_phys(conf, r10_bio);
623 rcu_read_lock();
624 for (s = 0; s < conf->copies; s++) {
625 int disk = r10_bio->devs[s].devnum;
626 struct md_rdev *rdev = rcu_dereference(
627 conf->mirrors[disk].rdev);
628 if (rdev && !test_bit(Faulty, &rdev->flags)) {
629 struct request_queue *q =
630 bdev_get_queue(rdev->bdev);
631 if (q->merge_bvec_fn) {
632 bvm->bi_sector = r10_bio->devs[s].addr
633 + rdev->data_offset;
634 bvm->bi_bdev = rdev->bdev;
635 max = min(max, q->merge_bvec_fn(
636 q, bvm, biovec));
639 rdev = rcu_dereference(conf->mirrors[disk].replacement);
640 if (rdev && !test_bit(Faulty, &rdev->flags)) {
641 struct request_queue *q =
642 bdev_get_queue(rdev->bdev);
643 if (q->merge_bvec_fn) {
644 bvm->bi_sector = r10_bio->devs[s].addr
645 + rdev->data_offset;
646 bvm->bi_bdev = rdev->bdev;
647 max = min(max, q->merge_bvec_fn(
648 q, bvm, biovec));
652 rcu_read_unlock();
654 return max;
658 * This routine returns the disk from which the requested read should
659 * be done. There is a per-array 'next expected sequential IO' sector
660 * number - if this matches on the next IO then we use the last disk.
661 * There is also a per-disk 'last know head position' sector that is
662 * maintained from IRQ contexts, both the normal and the resync IO
663 * completion handlers update this position correctly. If there is no
664 * perfect sequential match then we pick the disk whose head is closest.
666 * If there are 2 mirrors in the same 2 devices, performance degrades
667 * because position is mirror, not device based.
669 * The rdev for the device selected will have nr_pending incremented.
673 * FIXME: possibly should rethink readbalancing and do it differently
674 * depending on near_copies / far_copies geometry.
676 static struct md_rdev *read_balance(struct r10conf *conf,
677 struct r10bio *r10_bio,
678 int *max_sectors)
680 const sector_t this_sector = r10_bio->sector;
681 int disk, slot;
682 int sectors = r10_bio->sectors;
683 int best_good_sectors;
684 sector_t new_distance, best_dist;
685 struct md_rdev *rdev, *best_rdev;
686 int do_balance;
687 int best_slot;
689 raid10_find_phys(conf, r10_bio);
690 rcu_read_lock();
691 retry:
692 sectors = r10_bio->sectors;
693 best_slot = -1;
694 best_rdev = NULL;
695 best_dist = MaxSector;
696 best_good_sectors = 0;
697 do_balance = 1;
699 * Check if we can balance. We can balance on the whole
700 * device if no resync is going on (recovery is ok), or below
701 * the resync window. We take the first readable disk when
702 * above the resync window.
704 if (conf->mddev->recovery_cp < MaxSector
705 && (this_sector + sectors >= conf->next_resync))
706 do_balance = 0;
708 for (slot = 0; slot < conf->copies ; slot++) {
709 sector_t first_bad;
710 int bad_sectors;
711 sector_t dev_sector;
713 if (r10_bio->devs[slot].bio == IO_BLOCKED)
714 continue;
715 disk = r10_bio->devs[slot].devnum;
716 rdev = rcu_dereference(conf->mirrors[disk].replacement);
717 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
718 test_bit(Unmerged, &rdev->flags) ||
719 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
720 rdev = rcu_dereference(conf->mirrors[disk].rdev);
721 if (rdev == NULL ||
722 test_bit(Faulty, &rdev->flags) ||
723 test_bit(Unmerged, &rdev->flags))
724 continue;
725 if (!test_bit(In_sync, &rdev->flags) &&
726 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
727 continue;
729 dev_sector = r10_bio->devs[slot].addr;
730 if (is_badblock(rdev, dev_sector, sectors,
731 &first_bad, &bad_sectors)) {
732 if (best_dist < MaxSector)
733 /* Already have a better slot */
734 continue;
735 if (first_bad <= dev_sector) {
736 /* Cannot read here. If this is the
737 * 'primary' device, then we must not read
738 * beyond 'bad_sectors' from another device.
740 bad_sectors -= (dev_sector - first_bad);
741 if (!do_balance && sectors > bad_sectors)
742 sectors = bad_sectors;
743 if (best_good_sectors > sectors)
744 best_good_sectors = sectors;
745 } else {
746 sector_t good_sectors =
747 first_bad - dev_sector;
748 if (good_sectors > best_good_sectors) {
749 best_good_sectors = good_sectors;
750 best_slot = slot;
751 best_rdev = rdev;
753 if (!do_balance)
754 /* Must read from here */
755 break;
757 continue;
758 } else
759 best_good_sectors = sectors;
761 if (!do_balance)
762 break;
764 /* This optimisation is debatable, and completely destroys
765 * sequential read speed for 'far copies' arrays. So only
766 * keep it for 'near' arrays, and review those later.
768 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
769 break;
771 /* for far > 1 always use the lowest address */
772 if (conf->far_copies > 1)
773 new_distance = r10_bio->devs[slot].addr;
774 else
775 new_distance = abs(r10_bio->devs[slot].addr -
776 conf->mirrors[disk].head_position);
777 if (new_distance < best_dist) {
778 best_dist = new_distance;
779 best_slot = slot;
780 best_rdev = rdev;
783 if (slot >= conf->copies) {
784 slot = best_slot;
785 rdev = best_rdev;
788 if (slot >= 0) {
789 atomic_inc(&rdev->nr_pending);
790 if (test_bit(Faulty, &rdev->flags)) {
791 /* Cannot risk returning a device that failed
792 * before we inc'ed nr_pending
794 rdev_dec_pending(rdev, conf->mddev);
795 goto retry;
797 r10_bio->read_slot = slot;
798 } else
799 rdev = NULL;
800 rcu_read_unlock();
801 *max_sectors = best_good_sectors;
803 return rdev;
806 static int raid10_congested(void *data, int bits)
808 struct mddev *mddev = data;
809 struct r10conf *conf = mddev->private;
810 int i, ret = 0;
812 if ((bits & (1 << BDI_async_congested)) &&
813 conf->pending_count >= max_queued_requests)
814 return 1;
816 if (mddev_congested(mddev, bits))
817 return 1;
818 rcu_read_lock();
819 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
820 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
821 if (rdev && !test_bit(Faulty, &rdev->flags)) {
822 struct request_queue *q = bdev_get_queue(rdev->bdev);
824 ret |= bdi_congested(&q->backing_dev_info, bits);
827 rcu_read_unlock();
828 return ret;
831 static void flush_pending_writes(struct r10conf *conf)
833 /* Any writes that have been queued but are awaiting
834 * bitmap updates get flushed here.
836 spin_lock_irq(&conf->device_lock);
838 if (conf->pending_bio_list.head) {
839 struct bio *bio;
840 bio = bio_list_get(&conf->pending_bio_list);
841 conf->pending_count = 0;
842 spin_unlock_irq(&conf->device_lock);
843 /* flush any pending bitmap writes to disk
844 * before proceeding w/ I/O */
845 bitmap_unplug(conf->mddev->bitmap);
846 wake_up(&conf->wait_barrier);
848 while (bio) { /* submit pending writes */
849 struct bio *next = bio->bi_next;
850 bio->bi_next = NULL;
851 generic_make_request(bio);
852 bio = next;
854 } else
855 spin_unlock_irq(&conf->device_lock);
858 /* Barriers....
859 * Sometimes we need to suspend IO while we do something else,
860 * either some resync/recovery, or reconfigure the array.
861 * To do this we raise a 'barrier'.
862 * The 'barrier' is a counter that can be raised multiple times
863 * to count how many activities are happening which preclude
864 * normal IO.
865 * We can only raise the barrier if there is no pending IO.
866 * i.e. if nr_pending == 0.
867 * We choose only to raise the barrier if no-one is waiting for the
868 * barrier to go down. This means that as soon as an IO request
869 * is ready, no other operations which require a barrier will start
870 * until the IO request has had a chance.
872 * So: regular IO calls 'wait_barrier'. When that returns there
873 * is no backgroup IO happening, It must arrange to call
874 * allow_barrier when it has finished its IO.
875 * backgroup IO calls must call raise_barrier. Once that returns
876 * there is no normal IO happeing. It must arrange to call
877 * lower_barrier when the particular background IO completes.
880 static void raise_barrier(struct r10conf *conf, int force)
882 BUG_ON(force && !conf->barrier);
883 spin_lock_irq(&conf->resync_lock);
885 /* Wait until no block IO is waiting (unless 'force') */
886 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
887 conf->resync_lock, );
889 /* block any new IO from starting */
890 conf->barrier++;
892 /* Now wait for all pending IO to complete */
893 wait_event_lock_irq(conf->wait_barrier,
894 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
895 conf->resync_lock, );
897 spin_unlock_irq(&conf->resync_lock);
900 static void lower_barrier(struct r10conf *conf)
902 unsigned long flags;
903 spin_lock_irqsave(&conf->resync_lock, flags);
904 conf->barrier--;
905 spin_unlock_irqrestore(&conf->resync_lock, flags);
906 wake_up(&conf->wait_barrier);
909 static void wait_barrier(struct r10conf *conf)
911 spin_lock_irq(&conf->resync_lock);
912 if (conf->barrier) {
913 conf->nr_waiting++;
914 /* Wait for the barrier to drop.
915 * However if there are already pending
916 * requests (preventing the barrier from
917 * rising completely), and the
918 * pre-process bio queue isn't empty,
919 * then don't wait, as we need to empty
920 * that queue to get the nr_pending
921 * count down.
923 wait_event_lock_irq(conf->wait_barrier,
924 !conf->barrier ||
925 (conf->nr_pending &&
926 current->bio_list &&
927 !bio_list_empty(current->bio_list)),
928 conf->resync_lock,
930 conf->nr_waiting--;
932 conf->nr_pending++;
933 spin_unlock_irq(&conf->resync_lock);
936 static void allow_barrier(struct r10conf *conf)
938 unsigned long flags;
939 spin_lock_irqsave(&conf->resync_lock, flags);
940 conf->nr_pending--;
941 spin_unlock_irqrestore(&conf->resync_lock, flags);
942 wake_up(&conf->wait_barrier);
945 static void freeze_array(struct r10conf *conf)
947 /* stop syncio and normal IO and wait for everything to
948 * go quiet.
949 * We increment barrier and nr_waiting, and then
950 * wait until nr_pending match nr_queued+1
951 * This is called in the context of one normal IO request
952 * that has failed. Thus any sync request that might be pending
953 * will be blocked by nr_pending, and we need to wait for
954 * pending IO requests to complete or be queued for re-try.
955 * Thus the number queued (nr_queued) plus this request (1)
956 * must match the number of pending IOs (nr_pending) before
957 * we continue.
959 spin_lock_irq(&conf->resync_lock);
960 conf->barrier++;
961 conf->nr_waiting++;
962 wait_event_lock_irq(conf->wait_barrier,
963 conf->nr_pending == conf->nr_queued+1,
964 conf->resync_lock,
965 flush_pending_writes(conf));
967 spin_unlock_irq(&conf->resync_lock);
970 static void unfreeze_array(struct r10conf *conf)
972 /* reverse the effect of the freeze */
973 spin_lock_irq(&conf->resync_lock);
974 conf->barrier--;
975 conf->nr_waiting--;
976 wake_up(&conf->wait_barrier);
977 spin_unlock_irq(&conf->resync_lock);
980 static void make_request(struct mddev *mddev, struct bio * bio)
982 struct r10conf *conf = mddev->private;
983 struct r10bio *r10_bio;
984 struct bio *read_bio;
985 int i;
986 int chunk_sects = conf->chunk_mask + 1;
987 const int rw = bio_data_dir(bio);
988 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
989 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
990 unsigned long flags;
991 struct md_rdev *blocked_rdev;
992 int plugged;
993 int sectors_handled;
994 int max_sectors;
996 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
997 md_flush_request(mddev, bio);
998 return;
1001 /* If this request crosses a chunk boundary, we need to
1002 * split it. This will only happen for 1 PAGE (or less) requests.
1004 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
1005 > chunk_sects &&
1006 conf->near_copies < conf->raid_disks)) {
1007 struct bio_pair *bp;
1008 /* Sanity check -- queue functions should prevent this happening */
1009 if (bio->bi_vcnt != 1 ||
1010 bio->bi_idx != 0)
1011 goto bad_map;
1012 /* This is a one page bio that upper layers
1013 * refuse to split for us, so we need to split it.
1015 bp = bio_split(bio,
1016 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
1018 /* Each of these 'make_request' calls will call 'wait_barrier'.
1019 * If the first succeeds but the second blocks due to the resync
1020 * thread raising the barrier, we will deadlock because the
1021 * IO to the underlying device will be queued in generic_make_request
1022 * and will never complete, so will never reduce nr_pending.
1023 * So increment nr_waiting here so no new raise_barriers will
1024 * succeed, and so the second wait_barrier cannot block.
1026 spin_lock_irq(&conf->resync_lock);
1027 conf->nr_waiting++;
1028 spin_unlock_irq(&conf->resync_lock);
1030 make_request(mddev, &bp->bio1);
1031 make_request(mddev, &bp->bio2);
1033 spin_lock_irq(&conf->resync_lock);
1034 conf->nr_waiting--;
1035 wake_up(&conf->wait_barrier);
1036 spin_unlock_irq(&conf->resync_lock);
1038 bio_pair_release(bp);
1039 return;
1040 bad_map:
1041 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
1042 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
1043 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
1045 bio_io_error(bio);
1046 return;
1049 md_write_start(mddev, bio);
1052 * Register the new request and wait if the reconstruction
1053 * thread has put up a bar for new requests.
1054 * Continue immediately if no resync is active currently.
1056 wait_barrier(conf);
1058 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1060 r10_bio->master_bio = bio;
1061 r10_bio->sectors = bio->bi_size >> 9;
1063 r10_bio->mddev = mddev;
1064 r10_bio->sector = bio->bi_sector;
1065 r10_bio->state = 0;
1067 /* We might need to issue multiple reads to different
1068 * devices if there are bad blocks around, so we keep
1069 * track of the number of reads in bio->bi_phys_segments.
1070 * If this is 0, there is only one r10_bio and no locking
1071 * will be needed when the request completes. If it is
1072 * non-zero, then it is the number of not-completed requests.
1074 bio->bi_phys_segments = 0;
1075 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1077 if (rw == READ) {
1079 * read balancing logic:
1081 struct md_rdev *rdev;
1082 int slot;
1084 read_again:
1085 rdev = read_balance(conf, r10_bio, &max_sectors);
1086 if (!rdev) {
1087 raid_end_bio_io(r10_bio);
1088 return;
1090 slot = r10_bio->read_slot;
1092 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1093 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1094 max_sectors);
1096 r10_bio->devs[slot].bio = read_bio;
1097 r10_bio->devs[slot].rdev = rdev;
1099 read_bio->bi_sector = r10_bio->devs[slot].addr +
1100 rdev->data_offset;
1101 read_bio->bi_bdev = rdev->bdev;
1102 read_bio->bi_end_io = raid10_end_read_request;
1103 read_bio->bi_rw = READ | do_sync;
1104 read_bio->bi_private = r10_bio;
1106 if (max_sectors < r10_bio->sectors) {
1107 /* Could not read all from this device, so we will
1108 * need another r10_bio.
1110 sectors_handled = (r10_bio->sectors + max_sectors
1111 - bio->bi_sector);
1112 r10_bio->sectors = max_sectors;
1113 spin_lock_irq(&conf->device_lock);
1114 if (bio->bi_phys_segments == 0)
1115 bio->bi_phys_segments = 2;
1116 else
1117 bio->bi_phys_segments++;
1118 spin_unlock(&conf->device_lock);
1119 /* Cannot call generic_make_request directly
1120 * as that will be queued in __generic_make_request
1121 * and subsequent mempool_alloc might block
1122 * waiting for it. so hand bio over to raid10d.
1124 reschedule_retry(r10_bio);
1126 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1128 r10_bio->master_bio = bio;
1129 r10_bio->sectors = ((bio->bi_size >> 9)
1130 - sectors_handled);
1131 r10_bio->state = 0;
1132 r10_bio->mddev = mddev;
1133 r10_bio->sector = bio->bi_sector + sectors_handled;
1134 goto read_again;
1135 } else
1136 generic_make_request(read_bio);
1137 return;
1141 * WRITE:
1143 if (conf->pending_count >= max_queued_requests) {
1144 md_wakeup_thread(mddev->thread);
1145 wait_event(conf->wait_barrier,
1146 conf->pending_count < max_queued_requests);
1148 /* first select target devices under rcu_lock and
1149 * inc refcount on their rdev. Record them by setting
1150 * bios[x] to bio
1151 * If there are known/acknowledged bad blocks on any device
1152 * on which we have seen a write error, we want to avoid
1153 * writing to those blocks. This potentially requires several
1154 * writes to write around the bad blocks. Each set of writes
1155 * gets its own r10_bio with a set of bios attached. The number
1156 * of r10_bios is recored in bio->bi_phys_segments just as with
1157 * the read case.
1159 plugged = mddev_check_plugged(mddev);
1161 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1162 raid10_find_phys(conf, r10_bio);
1163 retry_write:
1164 blocked_rdev = NULL;
1165 rcu_read_lock();
1166 max_sectors = r10_bio->sectors;
1168 for (i = 0; i < conf->copies; i++) {
1169 int d = r10_bio->devs[i].devnum;
1170 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1171 struct md_rdev *rrdev = rcu_dereference(
1172 conf->mirrors[d].replacement);
1173 if (rdev == rrdev)
1174 rrdev = NULL;
1175 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1176 atomic_inc(&rdev->nr_pending);
1177 blocked_rdev = rdev;
1178 break;
1180 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1181 atomic_inc(&rrdev->nr_pending);
1182 blocked_rdev = rrdev;
1183 break;
1185 if (rdev && (test_bit(Faulty, &rdev->flags)
1186 || test_bit(Unmerged, &rdev->flags)))
1187 rdev = NULL;
1188 if (rrdev && (test_bit(Faulty, &rrdev->flags)
1189 || test_bit(Unmerged, &rrdev->flags)))
1190 rrdev = NULL;
1192 r10_bio->devs[i].bio = NULL;
1193 r10_bio->devs[i].repl_bio = NULL;
1195 if (!rdev && !rrdev) {
1196 set_bit(R10BIO_Degraded, &r10_bio->state);
1197 continue;
1199 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1200 sector_t first_bad;
1201 sector_t dev_sector = r10_bio->devs[i].addr;
1202 int bad_sectors;
1203 int is_bad;
1205 is_bad = is_badblock(rdev, dev_sector,
1206 max_sectors,
1207 &first_bad, &bad_sectors);
1208 if (is_bad < 0) {
1209 /* Mustn't write here until the bad block
1210 * is acknowledged
1212 atomic_inc(&rdev->nr_pending);
1213 set_bit(BlockedBadBlocks, &rdev->flags);
1214 blocked_rdev = rdev;
1215 break;
1217 if (is_bad && first_bad <= dev_sector) {
1218 /* Cannot write here at all */
1219 bad_sectors -= (dev_sector - first_bad);
1220 if (bad_sectors < max_sectors)
1221 /* Mustn't write more than bad_sectors
1222 * to other devices yet
1224 max_sectors = bad_sectors;
1225 /* We don't set R10BIO_Degraded as that
1226 * only applies if the disk is missing,
1227 * so it might be re-added, and we want to
1228 * know to recover this chunk.
1229 * In this case the device is here, and the
1230 * fact that this chunk is not in-sync is
1231 * recorded in the bad block log.
1233 continue;
1235 if (is_bad) {
1236 int good_sectors = first_bad - dev_sector;
1237 if (good_sectors < max_sectors)
1238 max_sectors = good_sectors;
1241 if (rdev) {
1242 r10_bio->devs[i].bio = bio;
1243 atomic_inc(&rdev->nr_pending);
1245 if (rrdev) {
1246 r10_bio->devs[i].repl_bio = bio;
1247 atomic_inc(&rrdev->nr_pending);
1250 rcu_read_unlock();
1252 if (unlikely(blocked_rdev)) {
1253 /* Have to wait for this device to get unblocked, then retry */
1254 int j;
1255 int d;
1257 for (j = 0; j < i; j++) {
1258 if (r10_bio->devs[j].bio) {
1259 d = r10_bio->devs[j].devnum;
1260 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1262 if (r10_bio->devs[j].repl_bio) {
1263 struct md_rdev *rdev;
1264 d = r10_bio->devs[j].devnum;
1265 rdev = conf->mirrors[d].replacement;
1266 if (!rdev) {
1267 /* Race with remove_disk */
1268 smp_mb();
1269 rdev = conf->mirrors[d].rdev;
1271 rdev_dec_pending(rdev, mddev);
1274 allow_barrier(conf);
1275 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1276 wait_barrier(conf);
1277 goto retry_write;
1280 if (max_sectors < r10_bio->sectors) {
1281 /* We are splitting this into multiple parts, so
1282 * we need to prepare for allocating another r10_bio.
1284 r10_bio->sectors = max_sectors;
1285 spin_lock_irq(&conf->device_lock);
1286 if (bio->bi_phys_segments == 0)
1287 bio->bi_phys_segments = 2;
1288 else
1289 bio->bi_phys_segments++;
1290 spin_unlock_irq(&conf->device_lock);
1292 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1294 atomic_set(&r10_bio->remaining, 1);
1295 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1297 for (i = 0; i < conf->copies; i++) {
1298 struct bio *mbio;
1299 int d = r10_bio->devs[i].devnum;
1300 if (r10_bio->devs[i].bio) {
1301 struct md_rdev *rdev = conf->mirrors[d].rdev;
1302 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1303 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1304 max_sectors);
1305 r10_bio->devs[i].bio = mbio;
1307 mbio->bi_sector = (r10_bio->devs[i].addr+
1308 rdev->data_offset);
1309 mbio->bi_bdev = rdev->bdev;
1310 mbio->bi_end_io = raid10_end_write_request;
1311 mbio->bi_rw = WRITE | do_sync | do_fua;
1312 mbio->bi_private = r10_bio;
1314 atomic_inc(&r10_bio->remaining);
1315 spin_lock_irqsave(&conf->device_lock, flags);
1316 bio_list_add(&conf->pending_bio_list, mbio);
1317 conf->pending_count++;
1318 spin_unlock_irqrestore(&conf->device_lock, flags);
1321 if (r10_bio->devs[i].repl_bio) {
1322 struct md_rdev *rdev = conf->mirrors[d].replacement;
1323 if (rdev == NULL) {
1324 /* Replacement just got moved to main 'rdev' */
1325 smp_mb();
1326 rdev = conf->mirrors[d].rdev;
1328 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1329 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1330 max_sectors);
1331 r10_bio->devs[i].repl_bio = mbio;
1333 mbio->bi_sector = (r10_bio->devs[i].addr+
1334 rdev->data_offset);
1335 mbio->bi_bdev = rdev->bdev;
1336 mbio->bi_end_io = raid10_end_write_request;
1337 mbio->bi_rw = WRITE | do_sync | do_fua;
1338 mbio->bi_private = r10_bio;
1340 atomic_inc(&r10_bio->remaining);
1341 spin_lock_irqsave(&conf->device_lock, flags);
1342 bio_list_add(&conf->pending_bio_list, mbio);
1343 conf->pending_count++;
1344 spin_unlock_irqrestore(&conf->device_lock, flags);
1348 /* Don't remove the bias on 'remaining' (one_write_done) until
1349 * after checking if we need to go around again.
1352 if (sectors_handled < (bio->bi_size >> 9)) {
1353 one_write_done(r10_bio);
1354 /* We need another r10_bio. It has already been counted
1355 * in bio->bi_phys_segments.
1357 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1359 r10_bio->master_bio = bio;
1360 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1362 r10_bio->mddev = mddev;
1363 r10_bio->sector = bio->bi_sector + sectors_handled;
1364 r10_bio->state = 0;
1365 goto retry_write;
1367 one_write_done(r10_bio);
1369 /* In case raid10d snuck in to freeze_array */
1370 wake_up(&conf->wait_barrier);
1372 if (do_sync || !mddev->bitmap || !plugged)
1373 md_wakeup_thread(mddev->thread);
1376 static void status(struct seq_file *seq, struct mddev *mddev)
1378 struct r10conf *conf = mddev->private;
1379 int i;
1381 if (conf->near_copies < conf->raid_disks)
1382 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1383 if (conf->near_copies > 1)
1384 seq_printf(seq, " %d near-copies", conf->near_copies);
1385 if (conf->far_copies > 1) {
1386 if (conf->far_offset)
1387 seq_printf(seq, " %d offset-copies", conf->far_copies);
1388 else
1389 seq_printf(seq, " %d far-copies", conf->far_copies);
1391 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1392 conf->raid_disks - mddev->degraded);
1393 for (i = 0; i < conf->raid_disks; i++)
1394 seq_printf(seq, "%s",
1395 conf->mirrors[i].rdev &&
1396 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1397 seq_printf(seq, "]");
1400 /* check if there are enough drives for
1401 * every block to appear on atleast one.
1402 * Don't consider the device numbered 'ignore'
1403 * as we might be about to remove it.
1405 static int enough(struct r10conf *conf, int ignore)
1407 int first = 0;
1409 do {
1410 int n = conf->copies;
1411 int cnt = 0;
1412 while (n--) {
1413 if (conf->mirrors[first].rdev &&
1414 first != ignore)
1415 cnt++;
1416 first = (first+1) % conf->raid_disks;
1418 if (cnt == 0)
1419 return 0;
1420 } while (first != 0);
1421 return 1;
1424 static void error(struct mddev *mddev, struct md_rdev *rdev)
1426 char b[BDEVNAME_SIZE];
1427 struct r10conf *conf = mddev->private;
1430 * If it is not operational, then we have already marked it as dead
1431 * else if it is the last working disks, ignore the error, let the
1432 * next level up know.
1433 * else mark the drive as failed
1435 if (test_bit(In_sync, &rdev->flags)
1436 && !enough(conf, rdev->raid_disk))
1438 * Don't fail the drive, just return an IO error.
1440 return;
1441 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1442 unsigned long flags;
1443 spin_lock_irqsave(&conf->device_lock, flags);
1444 mddev->degraded++;
1445 spin_unlock_irqrestore(&conf->device_lock, flags);
1447 * if recovery is running, make sure it aborts.
1449 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1451 set_bit(Blocked, &rdev->flags);
1452 set_bit(Faulty, &rdev->flags);
1453 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1454 printk(KERN_ALERT
1455 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1456 "md/raid10:%s: Operation continuing on %d devices.\n",
1457 mdname(mddev), bdevname(rdev->bdev, b),
1458 mdname(mddev), conf->raid_disks - mddev->degraded);
1461 static void print_conf(struct r10conf *conf)
1463 int i;
1464 struct mirror_info *tmp;
1466 printk(KERN_DEBUG "RAID10 conf printout:\n");
1467 if (!conf) {
1468 printk(KERN_DEBUG "(!conf)\n");
1469 return;
1471 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1472 conf->raid_disks);
1474 for (i = 0; i < conf->raid_disks; i++) {
1475 char b[BDEVNAME_SIZE];
1476 tmp = conf->mirrors + i;
1477 if (tmp->rdev)
1478 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1479 i, !test_bit(In_sync, &tmp->rdev->flags),
1480 !test_bit(Faulty, &tmp->rdev->flags),
1481 bdevname(tmp->rdev->bdev,b));
1485 static void close_sync(struct r10conf *conf)
1487 wait_barrier(conf);
1488 allow_barrier(conf);
1490 mempool_destroy(conf->r10buf_pool);
1491 conf->r10buf_pool = NULL;
1494 static int raid10_spare_active(struct mddev *mddev)
1496 int i;
1497 struct r10conf *conf = mddev->private;
1498 struct mirror_info *tmp;
1499 int count = 0;
1500 unsigned long flags;
1503 * Find all non-in_sync disks within the RAID10 configuration
1504 * and mark them in_sync
1506 for (i = 0; i < conf->raid_disks; i++) {
1507 tmp = conf->mirrors + i;
1508 if (tmp->replacement
1509 && tmp->replacement->recovery_offset == MaxSector
1510 && !test_bit(Faulty, &tmp->replacement->flags)
1511 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1512 /* Replacement has just become active */
1513 if (!tmp->rdev
1514 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1515 count++;
1516 if (tmp->rdev) {
1517 /* Replaced device not technically faulty,
1518 * but we need to be sure it gets removed
1519 * and never re-added.
1521 set_bit(Faulty, &tmp->rdev->flags);
1522 sysfs_notify_dirent_safe(
1523 tmp->rdev->sysfs_state);
1525 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1526 } else if (tmp->rdev
1527 && !test_bit(Faulty, &tmp->rdev->flags)
1528 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1529 count++;
1530 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1533 spin_lock_irqsave(&conf->device_lock, flags);
1534 mddev->degraded -= count;
1535 spin_unlock_irqrestore(&conf->device_lock, flags);
1537 print_conf(conf);
1538 return count;
1542 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1544 struct r10conf *conf = mddev->private;
1545 int err = -EEXIST;
1546 int mirror;
1547 int first = 0;
1548 int last = conf->raid_disks - 1;
1549 struct request_queue *q = bdev_get_queue(rdev->bdev);
1551 if (mddev->recovery_cp < MaxSector)
1552 /* only hot-add to in-sync arrays, as recovery is
1553 * very different from resync
1555 return -EBUSY;
1556 if (rdev->saved_raid_disk < 0 && !enough(conf, -1))
1557 return -EINVAL;
1559 if (rdev->raid_disk >= 0)
1560 first = last = rdev->raid_disk;
1562 if (q->merge_bvec_fn) {
1563 set_bit(Unmerged, &rdev->flags);
1564 mddev->merge_check_needed = 1;
1567 if (rdev->saved_raid_disk >= first &&
1568 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1569 mirror = rdev->saved_raid_disk;
1570 else
1571 mirror = first;
1572 for ( ; mirror <= last ; mirror++) {
1573 struct mirror_info *p = &conf->mirrors[mirror];
1574 if (p->recovery_disabled == mddev->recovery_disabled)
1575 continue;
1576 if (p->rdev) {
1577 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1578 p->replacement != NULL)
1579 continue;
1580 clear_bit(In_sync, &rdev->flags);
1581 set_bit(Replacement, &rdev->flags);
1582 rdev->raid_disk = mirror;
1583 err = 0;
1584 disk_stack_limits(mddev->gendisk, rdev->bdev,
1585 rdev->data_offset << 9);
1586 conf->fullsync = 1;
1587 rcu_assign_pointer(p->replacement, rdev);
1588 break;
1591 disk_stack_limits(mddev->gendisk, rdev->bdev,
1592 rdev->data_offset << 9);
1594 p->head_position = 0;
1595 p->recovery_disabled = mddev->recovery_disabled - 1;
1596 rdev->raid_disk = mirror;
1597 err = 0;
1598 if (rdev->saved_raid_disk != mirror)
1599 conf->fullsync = 1;
1600 rcu_assign_pointer(p->rdev, rdev);
1601 break;
1603 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1604 /* Some requests might not have seen this new
1605 * merge_bvec_fn. We must wait for them to complete
1606 * before merging the device fully.
1607 * First we make sure any code which has tested
1608 * our function has submitted the request, then
1609 * we wait for all outstanding requests to complete.
1611 synchronize_sched();
1612 raise_barrier(conf, 0);
1613 lower_barrier(conf);
1614 clear_bit(Unmerged, &rdev->flags);
1616 md_integrity_add_rdev(rdev, mddev);
1617 print_conf(conf);
1618 return err;
1621 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1623 struct r10conf *conf = mddev->private;
1624 int err = 0;
1625 int number = rdev->raid_disk;
1626 struct md_rdev **rdevp;
1627 struct mirror_info *p = conf->mirrors + number;
1629 print_conf(conf);
1630 if (rdev == p->rdev)
1631 rdevp = &p->rdev;
1632 else if (rdev == p->replacement)
1633 rdevp = &p->replacement;
1634 else
1635 return 0;
1637 if (test_bit(In_sync, &rdev->flags) ||
1638 atomic_read(&rdev->nr_pending)) {
1639 err = -EBUSY;
1640 goto abort;
1642 /* Only remove faulty devices if recovery
1643 * is not possible.
1645 if (!test_bit(Faulty, &rdev->flags) &&
1646 mddev->recovery_disabled != p->recovery_disabled &&
1647 (!p->replacement || p->replacement == rdev) &&
1648 enough(conf, -1)) {
1649 err = -EBUSY;
1650 goto abort;
1652 *rdevp = NULL;
1653 synchronize_rcu();
1654 if (atomic_read(&rdev->nr_pending)) {
1655 /* lost the race, try later */
1656 err = -EBUSY;
1657 *rdevp = rdev;
1658 goto abort;
1659 } else if (p->replacement) {
1660 /* We must have just cleared 'rdev' */
1661 p->rdev = p->replacement;
1662 clear_bit(Replacement, &p->replacement->flags);
1663 smp_mb(); /* Make sure other CPUs may see both as identical
1664 * but will never see neither -- if they are careful.
1666 p->replacement = NULL;
1667 clear_bit(WantReplacement, &rdev->flags);
1668 } else
1669 /* We might have just remove the Replacement as faulty
1670 * Clear the flag just in case
1672 clear_bit(WantReplacement, &rdev->flags);
1674 err = md_integrity_register(mddev);
1676 abort:
1678 print_conf(conf);
1679 return err;
1683 static void end_sync_read(struct bio *bio, int error)
1685 struct r10bio *r10_bio = bio->bi_private;
1686 struct r10conf *conf = r10_bio->mddev->private;
1687 int d;
1689 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1691 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1692 set_bit(R10BIO_Uptodate, &r10_bio->state);
1693 else
1694 /* The write handler will notice the lack of
1695 * R10BIO_Uptodate and record any errors etc
1697 atomic_add(r10_bio->sectors,
1698 &conf->mirrors[d].rdev->corrected_errors);
1700 /* for reconstruct, we always reschedule after a read.
1701 * for resync, only after all reads
1703 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1704 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1705 atomic_dec_and_test(&r10_bio->remaining)) {
1706 /* we have read all the blocks,
1707 * do the comparison in process context in raid10d
1709 reschedule_retry(r10_bio);
1713 static void end_sync_request(struct r10bio *r10_bio)
1715 struct mddev *mddev = r10_bio->mddev;
1717 while (atomic_dec_and_test(&r10_bio->remaining)) {
1718 if (r10_bio->master_bio == NULL) {
1719 /* the primary of several recovery bios */
1720 sector_t s = r10_bio->sectors;
1721 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1722 test_bit(R10BIO_WriteError, &r10_bio->state))
1723 reschedule_retry(r10_bio);
1724 else
1725 put_buf(r10_bio);
1726 md_done_sync(mddev, s, 1);
1727 break;
1728 } else {
1729 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1730 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1731 test_bit(R10BIO_WriteError, &r10_bio->state))
1732 reschedule_retry(r10_bio);
1733 else
1734 put_buf(r10_bio);
1735 r10_bio = r10_bio2;
1740 static void end_sync_write(struct bio *bio, int error)
1742 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1743 struct r10bio *r10_bio = bio->bi_private;
1744 struct mddev *mddev = r10_bio->mddev;
1745 struct r10conf *conf = mddev->private;
1746 int d;
1747 sector_t first_bad;
1748 int bad_sectors;
1749 int slot;
1750 int repl;
1751 struct md_rdev *rdev = NULL;
1753 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1754 if (repl)
1755 rdev = conf->mirrors[d].replacement;
1756 else
1757 rdev = conf->mirrors[d].rdev;
1759 if (!uptodate) {
1760 if (repl)
1761 md_error(mddev, rdev);
1762 else {
1763 set_bit(WriteErrorSeen, &rdev->flags);
1764 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1765 set_bit(MD_RECOVERY_NEEDED,
1766 &rdev->mddev->recovery);
1767 set_bit(R10BIO_WriteError, &r10_bio->state);
1769 } else if (is_badblock(rdev,
1770 r10_bio->devs[slot].addr,
1771 r10_bio->sectors,
1772 &first_bad, &bad_sectors))
1773 set_bit(R10BIO_MadeGood, &r10_bio->state);
1775 rdev_dec_pending(rdev, mddev);
1777 end_sync_request(r10_bio);
1781 * Note: sync and recover and handled very differently for raid10
1782 * This code is for resync.
1783 * For resync, we read through virtual addresses and read all blocks.
1784 * If there is any error, we schedule a write. The lowest numbered
1785 * drive is authoritative.
1786 * However requests come for physical address, so we need to map.
1787 * For every physical address there are raid_disks/copies virtual addresses,
1788 * which is always are least one, but is not necessarly an integer.
1789 * This means that a physical address can span multiple chunks, so we may
1790 * have to submit multiple io requests for a single sync request.
1793 * We check if all blocks are in-sync and only write to blocks that
1794 * aren't in sync
1796 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1798 struct r10conf *conf = mddev->private;
1799 int i, first;
1800 struct bio *tbio, *fbio;
1801 int vcnt;
1803 atomic_set(&r10_bio->remaining, 1);
1805 /* find the first device with a block */
1806 for (i=0; i<conf->copies; i++)
1807 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1808 break;
1810 if (i == conf->copies)
1811 goto done;
1813 first = i;
1814 fbio = r10_bio->devs[i].bio;
1816 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
1817 /* now find blocks with errors */
1818 for (i=0 ; i < conf->copies ; i++) {
1819 int j, d;
1821 tbio = r10_bio->devs[i].bio;
1823 if (tbio->bi_end_io != end_sync_read)
1824 continue;
1825 if (i == first)
1826 continue;
1827 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1828 /* We know that the bi_io_vec layout is the same for
1829 * both 'first' and 'i', so we just compare them.
1830 * All vec entries are PAGE_SIZE;
1832 for (j = 0; j < vcnt; j++)
1833 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1834 page_address(tbio->bi_io_vec[j].bv_page),
1835 fbio->bi_io_vec[j].bv_len))
1836 break;
1837 if (j == vcnt)
1838 continue;
1839 mddev->resync_mismatches += r10_bio->sectors;
1840 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1841 /* Don't fix anything. */
1842 continue;
1844 /* Ok, we need to write this bio, either to correct an
1845 * inconsistency or to correct an unreadable block.
1846 * First we need to fixup bv_offset, bv_len and
1847 * bi_vecs, as the read request might have corrupted these
1849 tbio->bi_vcnt = vcnt;
1850 tbio->bi_size = r10_bio->sectors << 9;
1851 tbio->bi_idx = 0;
1852 tbio->bi_phys_segments = 0;
1853 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1854 tbio->bi_flags |= 1 << BIO_UPTODATE;
1855 tbio->bi_next = NULL;
1856 tbio->bi_rw = WRITE;
1857 tbio->bi_private = r10_bio;
1858 tbio->bi_sector = r10_bio->devs[i].addr;
1860 for (j=0; j < vcnt ; j++) {
1861 tbio->bi_io_vec[j].bv_offset = 0;
1862 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1864 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1865 page_address(fbio->bi_io_vec[j].bv_page),
1866 PAGE_SIZE);
1868 tbio->bi_end_io = end_sync_write;
1870 d = r10_bio->devs[i].devnum;
1871 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1872 atomic_inc(&r10_bio->remaining);
1873 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1875 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1876 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1877 generic_make_request(tbio);
1880 /* Now write out to any replacement devices
1881 * that are active
1883 for (i = 0; i < conf->copies; i++) {
1884 int j, d;
1886 tbio = r10_bio->devs[i].repl_bio;
1887 if (!tbio || !tbio->bi_end_io)
1888 continue;
1889 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1890 && r10_bio->devs[i].bio != fbio)
1891 for (j = 0; j < vcnt; j++)
1892 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1893 page_address(fbio->bi_io_vec[j].bv_page),
1894 PAGE_SIZE);
1895 d = r10_bio->devs[i].devnum;
1896 atomic_inc(&r10_bio->remaining);
1897 md_sync_acct(conf->mirrors[d].replacement->bdev,
1898 tbio->bi_size >> 9);
1899 generic_make_request(tbio);
1902 done:
1903 if (atomic_dec_and_test(&r10_bio->remaining)) {
1904 md_done_sync(mddev, r10_bio->sectors, 1);
1905 put_buf(r10_bio);
1910 * Now for the recovery code.
1911 * Recovery happens across physical sectors.
1912 * We recover all non-is_sync drives by finding the virtual address of
1913 * each, and then choose a working drive that also has that virt address.
1914 * There is a separate r10_bio for each non-in_sync drive.
1915 * Only the first two slots are in use. The first for reading,
1916 * The second for writing.
1919 static void fix_recovery_read_error(struct r10bio *r10_bio)
1921 /* We got a read error during recovery.
1922 * We repeat the read in smaller page-sized sections.
1923 * If a read succeeds, write it to the new device or record
1924 * a bad block if we cannot.
1925 * If a read fails, record a bad block on both old and
1926 * new devices.
1928 struct mddev *mddev = r10_bio->mddev;
1929 struct r10conf *conf = mddev->private;
1930 struct bio *bio = r10_bio->devs[0].bio;
1931 sector_t sect = 0;
1932 int sectors = r10_bio->sectors;
1933 int idx = 0;
1934 int dr = r10_bio->devs[0].devnum;
1935 int dw = r10_bio->devs[1].devnum;
1937 while (sectors) {
1938 int s = sectors;
1939 struct md_rdev *rdev;
1940 sector_t addr;
1941 int ok;
1943 if (s > (PAGE_SIZE>>9))
1944 s = PAGE_SIZE >> 9;
1946 rdev = conf->mirrors[dr].rdev;
1947 addr = r10_bio->devs[0].addr + sect,
1948 ok = sync_page_io(rdev,
1949 addr,
1950 s << 9,
1951 bio->bi_io_vec[idx].bv_page,
1952 READ, false);
1953 if (ok) {
1954 rdev = conf->mirrors[dw].rdev;
1955 addr = r10_bio->devs[1].addr + sect;
1956 ok = sync_page_io(rdev,
1957 addr,
1958 s << 9,
1959 bio->bi_io_vec[idx].bv_page,
1960 WRITE, false);
1961 if (!ok) {
1962 set_bit(WriteErrorSeen, &rdev->flags);
1963 if (!test_and_set_bit(WantReplacement,
1964 &rdev->flags))
1965 set_bit(MD_RECOVERY_NEEDED,
1966 &rdev->mddev->recovery);
1969 if (!ok) {
1970 /* We don't worry if we cannot set a bad block -
1971 * it really is bad so there is no loss in not
1972 * recording it yet
1974 rdev_set_badblocks(rdev, addr, s, 0);
1976 if (rdev != conf->mirrors[dw].rdev) {
1977 /* need bad block on destination too */
1978 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
1979 addr = r10_bio->devs[1].addr + sect;
1980 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1981 if (!ok) {
1982 /* just abort the recovery */
1983 printk(KERN_NOTICE
1984 "md/raid10:%s: recovery aborted"
1985 " due to read error\n",
1986 mdname(mddev));
1988 conf->mirrors[dw].recovery_disabled
1989 = mddev->recovery_disabled;
1990 set_bit(MD_RECOVERY_INTR,
1991 &mddev->recovery);
1992 break;
1997 sectors -= s;
1998 sect += s;
1999 idx++;
2003 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2005 struct r10conf *conf = mddev->private;
2006 int d;
2007 struct bio *wbio, *wbio2;
2009 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2010 fix_recovery_read_error(r10_bio);
2011 end_sync_request(r10_bio);
2012 return;
2016 * share the pages with the first bio
2017 * and submit the write request
2019 d = r10_bio->devs[1].devnum;
2020 wbio = r10_bio->devs[1].bio;
2021 wbio2 = r10_bio->devs[1].repl_bio;
2022 if (wbio->bi_end_io) {
2023 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2024 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
2025 generic_make_request(wbio);
2027 if (wbio2 && wbio2->bi_end_io) {
2028 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2029 md_sync_acct(conf->mirrors[d].replacement->bdev,
2030 wbio2->bi_size >> 9);
2031 generic_make_request(wbio2);
2037 * Used by fix_read_error() to decay the per rdev read_errors.
2038 * We halve the read error count for every hour that has elapsed
2039 * since the last recorded read error.
2042 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2044 struct timespec cur_time_mon;
2045 unsigned long hours_since_last;
2046 unsigned int read_errors = atomic_read(&rdev->read_errors);
2048 ktime_get_ts(&cur_time_mon);
2050 if (rdev->last_read_error.tv_sec == 0 &&
2051 rdev->last_read_error.tv_nsec == 0) {
2052 /* first time we've seen a read error */
2053 rdev->last_read_error = cur_time_mon;
2054 return;
2057 hours_since_last = (cur_time_mon.tv_sec -
2058 rdev->last_read_error.tv_sec) / 3600;
2060 rdev->last_read_error = cur_time_mon;
2063 * if hours_since_last is > the number of bits in read_errors
2064 * just set read errors to 0. We do this to avoid
2065 * overflowing the shift of read_errors by hours_since_last.
2067 if (hours_since_last >= 8 * sizeof(read_errors))
2068 atomic_set(&rdev->read_errors, 0);
2069 else
2070 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2073 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2074 int sectors, struct page *page, int rw)
2076 sector_t first_bad;
2077 int bad_sectors;
2079 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2080 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2081 return -1;
2082 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2083 /* success */
2084 return 1;
2085 if (rw == WRITE) {
2086 set_bit(WriteErrorSeen, &rdev->flags);
2087 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2088 set_bit(MD_RECOVERY_NEEDED,
2089 &rdev->mddev->recovery);
2091 /* need to record an error - either for the block or the device */
2092 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2093 md_error(rdev->mddev, rdev);
2094 return 0;
2098 * This is a kernel thread which:
2100 * 1. Retries failed read operations on working mirrors.
2101 * 2. Updates the raid superblock when problems encounter.
2102 * 3. Performs writes following reads for array synchronising.
2105 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2107 int sect = 0; /* Offset from r10_bio->sector */
2108 int sectors = r10_bio->sectors;
2109 struct md_rdev*rdev;
2110 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2111 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2113 /* still own a reference to this rdev, so it cannot
2114 * have been cleared recently.
2116 rdev = conf->mirrors[d].rdev;
2118 if (test_bit(Faulty, &rdev->flags))
2119 /* drive has already been failed, just ignore any
2120 more fix_read_error() attempts */
2121 return;
2123 check_decay_read_errors(mddev, rdev);
2124 atomic_inc(&rdev->read_errors);
2125 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2126 char b[BDEVNAME_SIZE];
2127 bdevname(rdev->bdev, b);
2129 printk(KERN_NOTICE
2130 "md/raid10:%s: %s: Raid device exceeded "
2131 "read_error threshold [cur %d:max %d]\n",
2132 mdname(mddev), b,
2133 atomic_read(&rdev->read_errors), max_read_errors);
2134 printk(KERN_NOTICE
2135 "md/raid10:%s: %s: Failing raid device\n",
2136 mdname(mddev), b);
2137 md_error(mddev, conf->mirrors[d].rdev);
2138 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2139 return;
2142 while(sectors) {
2143 int s = sectors;
2144 int sl = r10_bio->read_slot;
2145 int success = 0;
2146 int start;
2148 if (s > (PAGE_SIZE>>9))
2149 s = PAGE_SIZE >> 9;
2151 rcu_read_lock();
2152 do {
2153 sector_t first_bad;
2154 int bad_sectors;
2156 d = r10_bio->devs[sl].devnum;
2157 rdev = rcu_dereference(conf->mirrors[d].rdev);
2158 if (rdev &&
2159 !test_bit(Unmerged, &rdev->flags) &&
2160 test_bit(In_sync, &rdev->flags) &&
2161 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2162 &first_bad, &bad_sectors) == 0) {
2163 atomic_inc(&rdev->nr_pending);
2164 rcu_read_unlock();
2165 success = sync_page_io(rdev,
2166 r10_bio->devs[sl].addr +
2167 sect,
2168 s<<9,
2169 conf->tmppage, READ, false);
2170 rdev_dec_pending(rdev, mddev);
2171 rcu_read_lock();
2172 if (success)
2173 break;
2175 sl++;
2176 if (sl == conf->copies)
2177 sl = 0;
2178 } while (!success && sl != r10_bio->read_slot);
2179 rcu_read_unlock();
2181 if (!success) {
2182 /* Cannot read from anywhere, just mark the block
2183 * as bad on the first device to discourage future
2184 * reads.
2186 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2187 rdev = conf->mirrors[dn].rdev;
2189 if (!rdev_set_badblocks(
2190 rdev,
2191 r10_bio->devs[r10_bio->read_slot].addr
2192 + sect,
2193 s, 0)) {
2194 md_error(mddev, rdev);
2195 r10_bio->devs[r10_bio->read_slot].bio
2196 = IO_BLOCKED;
2198 break;
2201 start = sl;
2202 /* write it back and re-read */
2203 rcu_read_lock();
2204 while (sl != r10_bio->read_slot) {
2205 char b[BDEVNAME_SIZE];
2207 if (sl==0)
2208 sl = conf->copies;
2209 sl--;
2210 d = r10_bio->devs[sl].devnum;
2211 rdev = rcu_dereference(conf->mirrors[d].rdev);
2212 if (!rdev ||
2213 test_bit(Unmerged, &rdev->flags) ||
2214 !test_bit(In_sync, &rdev->flags))
2215 continue;
2217 atomic_inc(&rdev->nr_pending);
2218 rcu_read_unlock();
2219 if (r10_sync_page_io(rdev,
2220 r10_bio->devs[sl].addr +
2221 sect,
2222 s, conf->tmppage, WRITE)
2223 == 0) {
2224 /* Well, this device is dead */
2225 printk(KERN_NOTICE
2226 "md/raid10:%s: read correction "
2227 "write failed"
2228 " (%d sectors at %llu on %s)\n",
2229 mdname(mddev), s,
2230 (unsigned long long)(
2231 sect + rdev->data_offset),
2232 bdevname(rdev->bdev, b));
2233 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2234 "drive\n",
2235 mdname(mddev),
2236 bdevname(rdev->bdev, b));
2238 rdev_dec_pending(rdev, mddev);
2239 rcu_read_lock();
2241 sl = start;
2242 while (sl != r10_bio->read_slot) {
2243 char b[BDEVNAME_SIZE];
2245 if (sl==0)
2246 sl = conf->copies;
2247 sl--;
2248 d = r10_bio->devs[sl].devnum;
2249 rdev = rcu_dereference(conf->mirrors[d].rdev);
2250 if (!rdev ||
2251 !test_bit(In_sync, &rdev->flags))
2252 continue;
2254 atomic_inc(&rdev->nr_pending);
2255 rcu_read_unlock();
2256 switch (r10_sync_page_io(rdev,
2257 r10_bio->devs[sl].addr +
2258 sect,
2259 s, conf->tmppage,
2260 READ)) {
2261 case 0:
2262 /* Well, this device is dead */
2263 printk(KERN_NOTICE
2264 "md/raid10:%s: unable to read back "
2265 "corrected sectors"
2266 " (%d sectors at %llu on %s)\n",
2267 mdname(mddev), s,
2268 (unsigned long long)(
2269 sect + rdev->data_offset),
2270 bdevname(rdev->bdev, b));
2271 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2272 "drive\n",
2273 mdname(mddev),
2274 bdevname(rdev->bdev, b));
2275 break;
2276 case 1:
2277 printk(KERN_INFO
2278 "md/raid10:%s: read error corrected"
2279 " (%d sectors at %llu on %s)\n",
2280 mdname(mddev), s,
2281 (unsigned long long)(
2282 sect + rdev->data_offset),
2283 bdevname(rdev->bdev, b));
2284 atomic_add(s, &rdev->corrected_errors);
2287 rdev_dec_pending(rdev, mddev);
2288 rcu_read_lock();
2290 rcu_read_unlock();
2292 sectors -= s;
2293 sect += s;
2297 static void bi_complete(struct bio *bio, int error)
2299 complete((struct completion *)bio->bi_private);
2302 static int submit_bio_wait(int rw, struct bio *bio)
2304 struct completion event;
2305 rw |= REQ_SYNC;
2307 init_completion(&event);
2308 bio->bi_private = &event;
2309 bio->bi_end_io = bi_complete;
2310 submit_bio(rw, bio);
2311 wait_for_completion(&event);
2313 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2316 static int narrow_write_error(struct r10bio *r10_bio, int i)
2318 struct bio *bio = r10_bio->master_bio;
2319 struct mddev *mddev = r10_bio->mddev;
2320 struct r10conf *conf = mddev->private;
2321 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2322 /* bio has the data to be written to slot 'i' where
2323 * we just recently had a write error.
2324 * We repeatedly clone the bio and trim down to one block,
2325 * then try the write. Where the write fails we record
2326 * a bad block.
2327 * It is conceivable that the bio doesn't exactly align with
2328 * blocks. We must handle this.
2330 * We currently own a reference to the rdev.
2333 int block_sectors;
2334 sector_t sector;
2335 int sectors;
2336 int sect_to_write = r10_bio->sectors;
2337 int ok = 1;
2339 if (rdev->badblocks.shift < 0)
2340 return 0;
2342 block_sectors = 1 << rdev->badblocks.shift;
2343 sector = r10_bio->sector;
2344 sectors = ((r10_bio->sector + block_sectors)
2345 & ~(sector_t)(block_sectors - 1))
2346 - sector;
2348 while (sect_to_write) {
2349 struct bio *wbio;
2350 if (sectors > sect_to_write)
2351 sectors = sect_to_write;
2352 /* Write at 'sector' for 'sectors' */
2353 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2354 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2355 wbio->bi_sector = (r10_bio->devs[i].addr+
2356 rdev->data_offset+
2357 (sector - r10_bio->sector));
2358 wbio->bi_bdev = rdev->bdev;
2359 if (submit_bio_wait(WRITE, wbio) == 0)
2360 /* Failure! */
2361 ok = rdev_set_badblocks(rdev, sector,
2362 sectors, 0)
2363 && ok;
2365 bio_put(wbio);
2366 sect_to_write -= sectors;
2367 sector += sectors;
2368 sectors = block_sectors;
2370 return ok;
2373 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2375 int slot = r10_bio->read_slot;
2376 struct bio *bio;
2377 struct r10conf *conf = mddev->private;
2378 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2379 char b[BDEVNAME_SIZE];
2380 unsigned long do_sync;
2381 int max_sectors;
2383 /* we got a read error. Maybe the drive is bad. Maybe just
2384 * the block and we can fix it.
2385 * We freeze all other IO, and try reading the block from
2386 * other devices. When we find one, we re-write
2387 * and check it that fixes the read error.
2388 * This is all done synchronously while the array is
2389 * frozen.
2391 bio = r10_bio->devs[slot].bio;
2392 bdevname(bio->bi_bdev, b);
2393 bio_put(bio);
2394 r10_bio->devs[slot].bio = NULL;
2396 if (mddev->ro == 0) {
2397 freeze_array(conf);
2398 fix_read_error(conf, mddev, r10_bio);
2399 unfreeze_array(conf);
2400 } else
2401 r10_bio->devs[slot].bio = IO_BLOCKED;
2403 rdev_dec_pending(rdev, mddev);
2405 read_more:
2406 rdev = read_balance(conf, r10_bio, &max_sectors);
2407 if (rdev == NULL) {
2408 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2409 " read error for block %llu\n",
2410 mdname(mddev), b,
2411 (unsigned long long)r10_bio->sector);
2412 raid_end_bio_io(r10_bio);
2413 return;
2416 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
2417 slot = r10_bio->read_slot;
2418 printk_ratelimited(
2419 KERN_ERR
2420 "md/raid10:%s: %s: redirecting "
2421 "sector %llu to another mirror\n",
2422 mdname(mddev),
2423 bdevname(rdev->bdev, b),
2424 (unsigned long long)r10_bio->sector);
2425 bio = bio_clone_mddev(r10_bio->master_bio,
2426 GFP_NOIO, mddev);
2427 md_trim_bio(bio,
2428 r10_bio->sector - bio->bi_sector,
2429 max_sectors);
2430 r10_bio->devs[slot].bio = bio;
2431 r10_bio->devs[slot].rdev = rdev;
2432 bio->bi_sector = r10_bio->devs[slot].addr
2433 + rdev->data_offset;
2434 bio->bi_bdev = rdev->bdev;
2435 bio->bi_rw = READ | do_sync;
2436 bio->bi_private = r10_bio;
2437 bio->bi_end_io = raid10_end_read_request;
2438 if (max_sectors < r10_bio->sectors) {
2439 /* Drat - have to split this up more */
2440 struct bio *mbio = r10_bio->master_bio;
2441 int sectors_handled =
2442 r10_bio->sector + max_sectors
2443 - mbio->bi_sector;
2444 r10_bio->sectors = max_sectors;
2445 spin_lock_irq(&conf->device_lock);
2446 if (mbio->bi_phys_segments == 0)
2447 mbio->bi_phys_segments = 2;
2448 else
2449 mbio->bi_phys_segments++;
2450 spin_unlock_irq(&conf->device_lock);
2451 generic_make_request(bio);
2453 r10_bio = mempool_alloc(conf->r10bio_pool,
2454 GFP_NOIO);
2455 r10_bio->master_bio = mbio;
2456 r10_bio->sectors = (mbio->bi_size >> 9)
2457 - sectors_handled;
2458 r10_bio->state = 0;
2459 set_bit(R10BIO_ReadError,
2460 &r10_bio->state);
2461 r10_bio->mddev = mddev;
2462 r10_bio->sector = mbio->bi_sector
2463 + sectors_handled;
2465 goto read_more;
2466 } else
2467 generic_make_request(bio);
2470 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2472 /* Some sort of write request has finished and it
2473 * succeeded in writing where we thought there was a
2474 * bad block. So forget the bad block.
2475 * Or possibly if failed and we need to record
2476 * a bad block.
2478 int m;
2479 struct md_rdev *rdev;
2481 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2482 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2483 for (m = 0; m < conf->copies; m++) {
2484 int dev = r10_bio->devs[m].devnum;
2485 rdev = conf->mirrors[dev].rdev;
2486 if (r10_bio->devs[m].bio == NULL)
2487 continue;
2488 if (test_bit(BIO_UPTODATE,
2489 &r10_bio->devs[m].bio->bi_flags)) {
2490 rdev_clear_badblocks(
2491 rdev,
2492 r10_bio->devs[m].addr,
2493 r10_bio->sectors);
2494 } else {
2495 if (!rdev_set_badblocks(
2496 rdev,
2497 r10_bio->devs[m].addr,
2498 r10_bio->sectors, 0))
2499 md_error(conf->mddev, rdev);
2501 rdev = conf->mirrors[dev].replacement;
2502 if (r10_bio->devs[m].repl_bio == NULL)
2503 continue;
2504 if (test_bit(BIO_UPTODATE,
2505 &r10_bio->devs[m].repl_bio->bi_flags)) {
2506 rdev_clear_badblocks(
2507 rdev,
2508 r10_bio->devs[m].addr,
2509 r10_bio->sectors);
2510 } else {
2511 if (!rdev_set_badblocks(
2512 rdev,
2513 r10_bio->devs[m].addr,
2514 r10_bio->sectors, 0))
2515 md_error(conf->mddev, rdev);
2518 put_buf(r10_bio);
2519 } else {
2520 for (m = 0; m < conf->copies; m++) {
2521 int dev = r10_bio->devs[m].devnum;
2522 struct bio *bio = r10_bio->devs[m].bio;
2523 rdev = conf->mirrors[dev].rdev;
2524 if (bio == IO_MADE_GOOD) {
2525 rdev_clear_badblocks(
2526 rdev,
2527 r10_bio->devs[m].addr,
2528 r10_bio->sectors);
2529 rdev_dec_pending(rdev, conf->mddev);
2530 } else if (bio != NULL &&
2531 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2532 if (!narrow_write_error(r10_bio, m)) {
2533 md_error(conf->mddev, rdev);
2534 set_bit(R10BIO_Degraded,
2535 &r10_bio->state);
2537 rdev_dec_pending(rdev, conf->mddev);
2539 bio = r10_bio->devs[m].repl_bio;
2540 rdev = conf->mirrors[dev].replacement;
2541 if (rdev && bio == IO_MADE_GOOD) {
2542 rdev_clear_badblocks(
2543 rdev,
2544 r10_bio->devs[m].addr,
2545 r10_bio->sectors);
2546 rdev_dec_pending(rdev, conf->mddev);
2549 if (test_bit(R10BIO_WriteError,
2550 &r10_bio->state))
2551 close_write(r10_bio);
2552 raid_end_bio_io(r10_bio);
2556 static void raid10d(struct mddev *mddev)
2558 struct r10bio *r10_bio;
2559 unsigned long flags;
2560 struct r10conf *conf = mddev->private;
2561 struct list_head *head = &conf->retry_list;
2562 struct blk_plug plug;
2564 md_check_recovery(mddev);
2566 blk_start_plug(&plug);
2567 for (;;) {
2569 flush_pending_writes(conf);
2571 spin_lock_irqsave(&conf->device_lock, flags);
2572 if (list_empty(head)) {
2573 spin_unlock_irqrestore(&conf->device_lock, flags);
2574 break;
2576 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2577 list_del(head->prev);
2578 conf->nr_queued--;
2579 spin_unlock_irqrestore(&conf->device_lock, flags);
2581 mddev = r10_bio->mddev;
2582 conf = mddev->private;
2583 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2584 test_bit(R10BIO_WriteError, &r10_bio->state))
2585 handle_write_completed(conf, r10_bio);
2586 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2587 sync_request_write(mddev, r10_bio);
2588 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2589 recovery_request_write(mddev, r10_bio);
2590 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2591 handle_read_error(mddev, r10_bio);
2592 else {
2593 /* just a partial read to be scheduled from a
2594 * separate context
2596 int slot = r10_bio->read_slot;
2597 generic_make_request(r10_bio->devs[slot].bio);
2600 cond_resched();
2601 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2602 md_check_recovery(mddev);
2604 blk_finish_plug(&plug);
2608 static int init_resync(struct r10conf *conf)
2610 int buffs;
2611 int i;
2613 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2614 BUG_ON(conf->r10buf_pool);
2615 conf->have_replacement = 0;
2616 for (i = 0; i < conf->raid_disks; i++)
2617 if (conf->mirrors[i].replacement)
2618 conf->have_replacement = 1;
2619 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2620 if (!conf->r10buf_pool)
2621 return -ENOMEM;
2622 conf->next_resync = 0;
2623 return 0;
2627 * perform a "sync" on one "block"
2629 * We need to make sure that no normal I/O request - particularly write
2630 * requests - conflict with active sync requests.
2632 * This is achieved by tracking pending requests and a 'barrier' concept
2633 * that can be installed to exclude normal IO requests.
2635 * Resync and recovery are handled very differently.
2636 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2638 * For resync, we iterate over virtual addresses, read all copies,
2639 * and update if there are differences. If only one copy is live,
2640 * skip it.
2641 * For recovery, we iterate over physical addresses, read a good
2642 * value for each non-in_sync drive, and over-write.
2644 * So, for recovery we may have several outstanding complex requests for a
2645 * given address, one for each out-of-sync device. We model this by allocating
2646 * a number of r10_bio structures, one for each out-of-sync device.
2647 * As we setup these structures, we collect all bio's together into a list
2648 * which we then process collectively to add pages, and then process again
2649 * to pass to generic_make_request.
2651 * The r10_bio structures are linked using a borrowed master_bio pointer.
2652 * This link is counted in ->remaining. When the r10_bio that points to NULL
2653 * has its remaining count decremented to 0, the whole complex operation
2654 * is complete.
2658 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
2659 int *skipped, int go_faster)
2661 struct r10conf *conf = mddev->private;
2662 struct r10bio *r10_bio;
2663 struct bio *biolist = NULL, *bio;
2664 sector_t max_sector, nr_sectors;
2665 int i;
2666 int max_sync;
2667 sector_t sync_blocks;
2668 sector_t sectors_skipped = 0;
2669 int chunks_skipped = 0;
2671 if (!conf->r10buf_pool)
2672 if (init_resync(conf))
2673 return 0;
2675 skipped:
2676 max_sector = mddev->dev_sectors;
2677 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2678 max_sector = mddev->resync_max_sectors;
2679 if (sector_nr >= max_sector) {
2680 /* If we aborted, we need to abort the
2681 * sync on the 'current' bitmap chucks (there can
2682 * be several when recovering multiple devices).
2683 * as we may have started syncing it but not finished.
2684 * We can find the current address in
2685 * mddev->curr_resync, but for recovery,
2686 * we need to convert that to several
2687 * virtual addresses.
2689 if (mddev->curr_resync < max_sector) { /* aborted */
2690 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2691 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2692 &sync_blocks, 1);
2693 else for (i=0; i<conf->raid_disks; i++) {
2694 sector_t sect =
2695 raid10_find_virt(conf, mddev->curr_resync, i);
2696 bitmap_end_sync(mddev->bitmap, sect,
2697 &sync_blocks, 1);
2699 } else {
2700 /* completed sync */
2701 if ((!mddev->bitmap || conf->fullsync)
2702 && conf->have_replacement
2703 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2704 /* Completed a full sync so the replacements
2705 * are now fully recovered.
2707 for (i = 0; i < conf->raid_disks; i++)
2708 if (conf->mirrors[i].replacement)
2709 conf->mirrors[i].replacement
2710 ->recovery_offset
2711 = MaxSector;
2713 conf->fullsync = 0;
2715 bitmap_close_sync(mddev->bitmap);
2716 close_sync(conf);
2717 *skipped = 1;
2718 return sectors_skipped;
2720 if (chunks_skipped >= conf->raid_disks) {
2721 /* if there has been nothing to do on any drive,
2722 * then there is nothing to do at all..
2724 *skipped = 1;
2725 return (max_sector - sector_nr) + sectors_skipped;
2728 if (max_sector > mddev->resync_max)
2729 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2731 /* make sure whole request will fit in a chunk - if chunks
2732 * are meaningful
2734 if (conf->near_copies < conf->raid_disks &&
2735 max_sector > (sector_nr | conf->chunk_mask))
2736 max_sector = (sector_nr | conf->chunk_mask) + 1;
2738 * If there is non-resync activity waiting for us then
2739 * put in a delay to throttle resync.
2741 if (!go_faster && conf->nr_waiting)
2742 msleep_interruptible(1000);
2744 /* Again, very different code for resync and recovery.
2745 * Both must result in an r10bio with a list of bios that
2746 * have bi_end_io, bi_sector, bi_bdev set,
2747 * and bi_private set to the r10bio.
2748 * For recovery, we may actually create several r10bios
2749 * with 2 bios in each, that correspond to the bios in the main one.
2750 * In this case, the subordinate r10bios link back through a
2751 * borrowed master_bio pointer, and the counter in the master
2752 * includes a ref from each subordinate.
2754 /* First, we decide what to do and set ->bi_end_io
2755 * To end_sync_read if we want to read, and
2756 * end_sync_write if we will want to write.
2759 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2760 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2761 /* recovery... the complicated one */
2762 int j;
2763 r10_bio = NULL;
2765 for (i=0 ; i<conf->raid_disks; i++) {
2766 int still_degraded;
2767 struct r10bio *rb2;
2768 sector_t sect;
2769 int must_sync;
2770 int any_working;
2771 struct mirror_info *mirror = &conf->mirrors[i];
2773 if ((mirror->rdev == NULL ||
2774 test_bit(In_sync, &mirror->rdev->flags))
2776 (mirror->replacement == NULL ||
2777 test_bit(Faulty,
2778 &mirror->replacement->flags)))
2779 continue;
2781 still_degraded = 0;
2782 /* want to reconstruct this device */
2783 rb2 = r10_bio;
2784 sect = raid10_find_virt(conf, sector_nr, i);
2785 if (sect >= mddev->resync_max_sectors) {
2786 /* last stripe is not complete - don't
2787 * try to recover this sector.
2789 continue;
2791 /* Unless we are doing a full sync, or a replacement
2792 * we only need to recover the block if it is set in
2793 * the bitmap
2795 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2796 &sync_blocks, 1);
2797 if (sync_blocks < max_sync)
2798 max_sync = sync_blocks;
2799 if (!must_sync &&
2800 mirror->replacement == NULL &&
2801 !conf->fullsync) {
2802 /* yep, skip the sync_blocks here, but don't assume
2803 * that there will never be anything to do here
2805 chunks_skipped = -1;
2806 continue;
2809 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2810 raise_barrier(conf, rb2 != NULL);
2811 atomic_set(&r10_bio->remaining, 0);
2813 r10_bio->master_bio = (struct bio*)rb2;
2814 if (rb2)
2815 atomic_inc(&rb2->remaining);
2816 r10_bio->mddev = mddev;
2817 set_bit(R10BIO_IsRecover, &r10_bio->state);
2818 r10_bio->sector = sect;
2820 raid10_find_phys(conf, r10_bio);
2822 /* Need to check if the array will still be
2823 * degraded
2825 for (j=0; j<conf->raid_disks; j++)
2826 if (conf->mirrors[j].rdev == NULL ||
2827 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2828 still_degraded = 1;
2829 break;
2832 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2833 &sync_blocks, still_degraded);
2835 any_working = 0;
2836 for (j=0; j<conf->copies;j++) {
2837 int k;
2838 int d = r10_bio->devs[j].devnum;
2839 sector_t from_addr, to_addr;
2840 struct md_rdev *rdev;
2841 sector_t sector, first_bad;
2842 int bad_sectors;
2843 if (!conf->mirrors[d].rdev ||
2844 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2845 continue;
2846 /* This is where we read from */
2847 any_working = 1;
2848 rdev = conf->mirrors[d].rdev;
2849 sector = r10_bio->devs[j].addr;
2851 if (is_badblock(rdev, sector, max_sync,
2852 &first_bad, &bad_sectors)) {
2853 if (first_bad > sector)
2854 max_sync = first_bad - sector;
2855 else {
2856 bad_sectors -= (sector
2857 - first_bad);
2858 if (max_sync > bad_sectors)
2859 max_sync = bad_sectors;
2860 continue;
2863 bio = r10_bio->devs[0].bio;
2864 bio->bi_next = biolist;
2865 biolist = bio;
2866 bio->bi_private = r10_bio;
2867 bio->bi_end_io = end_sync_read;
2868 bio->bi_rw = READ;
2869 from_addr = r10_bio->devs[j].addr;
2870 bio->bi_sector = from_addr + rdev->data_offset;
2871 bio->bi_bdev = rdev->bdev;
2872 atomic_inc(&rdev->nr_pending);
2873 /* and we write to 'i' (if not in_sync) */
2875 for (k=0; k<conf->copies; k++)
2876 if (r10_bio->devs[k].devnum == i)
2877 break;
2878 BUG_ON(k == conf->copies);
2879 to_addr = r10_bio->devs[k].addr;
2880 r10_bio->devs[0].devnum = d;
2881 r10_bio->devs[0].addr = from_addr;
2882 r10_bio->devs[1].devnum = i;
2883 r10_bio->devs[1].addr = to_addr;
2885 rdev = mirror->rdev;
2886 if (!test_bit(In_sync, &rdev->flags)) {
2887 bio = r10_bio->devs[1].bio;
2888 bio->bi_next = biolist;
2889 biolist = bio;
2890 bio->bi_private = r10_bio;
2891 bio->bi_end_io = end_sync_write;
2892 bio->bi_rw = WRITE;
2893 bio->bi_sector = to_addr
2894 + rdev->data_offset;
2895 bio->bi_bdev = rdev->bdev;
2896 atomic_inc(&r10_bio->remaining);
2897 } else
2898 r10_bio->devs[1].bio->bi_end_io = NULL;
2900 /* and maybe write to replacement */
2901 bio = r10_bio->devs[1].repl_bio;
2902 if (bio)
2903 bio->bi_end_io = NULL;
2904 rdev = mirror->replacement;
2905 /* Note: if rdev != NULL, then bio
2906 * cannot be NULL as r10buf_pool_alloc will
2907 * have allocated it.
2908 * So the second test here is pointless.
2909 * But it keeps semantic-checkers happy, and
2910 * this comment keeps human reviewers
2911 * happy.
2913 if (rdev == NULL || bio == NULL ||
2914 test_bit(Faulty, &rdev->flags))
2915 break;
2916 bio->bi_next = biolist;
2917 biolist = bio;
2918 bio->bi_private = r10_bio;
2919 bio->bi_end_io = end_sync_write;
2920 bio->bi_rw = WRITE;
2921 bio->bi_sector = to_addr + rdev->data_offset;
2922 bio->bi_bdev = rdev->bdev;
2923 atomic_inc(&r10_bio->remaining);
2924 break;
2926 if (j == conf->copies) {
2927 /* Cannot recover, so abort the recovery or
2928 * record a bad block */
2929 put_buf(r10_bio);
2930 if (rb2)
2931 atomic_dec(&rb2->remaining);
2932 r10_bio = rb2;
2933 if (any_working) {
2934 /* problem is that there are bad blocks
2935 * on other device(s)
2937 int k;
2938 for (k = 0; k < conf->copies; k++)
2939 if (r10_bio->devs[k].devnum == i)
2940 break;
2941 if (!test_bit(In_sync,
2942 &mirror->rdev->flags)
2943 && !rdev_set_badblocks(
2944 mirror->rdev,
2945 r10_bio->devs[k].addr,
2946 max_sync, 0))
2947 any_working = 0;
2948 if (mirror->replacement &&
2949 !rdev_set_badblocks(
2950 mirror->replacement,
2951 r10_bio->devs[k].addr,
2952 max_sync, 0))
2953 any_working = 0;
2955 if (!any_working) {
2956 if (!test_and_set_bit(MD_RECOVERY_INTR,
2957 &mddev->recovery))
2958 printk(KERN_INFO "md/raid10:%s: insufficient "
2959 "working devices for recovery.\n",
2960 mdname(mddev));
2961 mirror->recovery_disabled
2962 = mddev->recovery_disabled;
2964 break;
2967 if (biolist == NULL) {
2968 while (r10_bio) {
2969 struct r10bio *rb2 = r10_bio;
2970 r10_bio = (struct r10bio*) rb2->master_bio;
2971 rb2->master_bio = NULL;
2972 put_buf(rb2);
2974 goto giveup;
2976 } else {
2977 /* resync. Schedule a read for every block at this virt offset */
2978 int count = 0;
2980 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2982 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2983 &sync_blocks, mddev->degraded) &&
2984 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2985 &mddev->recovery)) {
2986 /* We can skip this block */
2987 *skipped = 1;
2988 return sync_blocks + sectors_skipped;
2990 if (sync_blocks < max_sync)
2991 max_sync = sync_blocks;
2992 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2994 r10_bio->mddev = mddev;
2995 atomic_set(&r10_bio->remaining, 0);
2996 raise_barrier(conf, 0);
2997 conf->next_resync = sector_nr;
2999 r10_bio->master_bio = NULL;
3000 r10_bio->sector = sector_nr;
3001 set_bit(R10BIO_IsSync, &r10_bio->state);
3002 raid10_find_phys(conf, r10_bio);
3003 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
3005 for (i=0; i<conf->copies; i++) {
3006 int d = r10_bio->devs[i].devnum;
3007 sector_t first_bad, sector;
3008 int bad_sectors;
3010 if (r10_bio->devs[i].repl_bio)
3011 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3013 bio = r10_bio->devs[i].bio;
3014 bio->bi_end_io = NULL;
3015 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3016 if (conf->mirrors[d].rdev == NULL ||
3017 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
3018 continue;
3019 sector = r10_bio->devs[i].addr;
3020 if (is_badblock(conf->mirrors[d].rdev,
3021 sector, max_sync,
3022 &first_bad, &bad_sectors)) {
3023 if (first_bad > sector)
3024 max_sync = first_bad - sector;
3025 else {
3026 bad_sectors -= (sector - first_bad);
3027 if (max_sync > bad_sectors)
3028 max_sync = bad_sectors;
3029 continue;
3032 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3033 atomic_inc(&r10_bio->remaining);
3034 bio->bi_next = biolist;
3035 biolist = bio;
3036 bio->bi_private = r10_bio;
3037 bio->bi_end_io = end_sync_read;
3038 bio->bi_rw = READ;
3039 bio->bi_sector = sector +
3040 conf->mirrors[d].rdev->data_offset;
3041 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
3042 count++;
3044 if (conf->mirrors[d].replacement == NULL ||
3045 test_bit(Faulty,
3046 &conf->mirrors[d].replacement->flags))
3047 continue;
3049 /* Need to set up for writing to the replacement */
3050 bio = r10_bio->devs[i].repl_bio;
3051 clear_bit(BIO_UPTODATE, &bio->bi_flags);
3053 sector = r10_bio->devs[i].addr;
3054 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
3055 bio->bi_next = biolist;
3056 biolist = bio;
3057 bio->bi_private = r10_bio;
3058 bio->bi_end_io = end_sync_write;
3059 bio->bi_rw = WRITE;
3060 bio->bi_sector = sector +
3061 conf->mirrors[d].replacement->data_offset;
3062 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
3063 count++;
3066 if (count < 2) {
3067 for (i=0; i<conf->copies; i++) {
3068 int d = r10_bio->devs[i].devnum;
3069 if (r10_bio->devs[i].bio->bi_end_io)
3070 rdev_dec_pending(conf->mirrors[d].rdev,
3071 mddev);
3072 if (r10_bio->devs[i].repl_bio &&
3073 r10_bio->devs[i].repl_bio->bi_end_io)
3074 rdev_dec_pending(
3075 conf->mirrors[d].replacement,
3076 mddev);
3078 put_buf(r10_bio);
3079 biolist = NULL;
3080 goto giveup;
3084 for (bio = biolist; bio ; bio=bio->bi_next) {
3086 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
3087 if (bio->bi_end_io)
3088 bio->bi_flags |= 1 << BIO_UPTODATE;
3089 bio->bi_vcnt = 0;
3090 bio->bi_idx = 0;
3091 bio->bi_phys_segments = 0;
3092 bio->bi_size = 0;
3095 nr_sectors = 0;
3096 if (sector_nr + max_sync < max_sector)
3097 max_sector = sector_nr + max_sync;
3098 do {
3099 struct page *page;
3100 int len = PAGE_SIZE;
3101 if (sector_nr + (len>>9) > max_sector)
3102 len = (max_sector - sector_nr) << 9;
3103 if (len == 0)
3104 break;
3105 for (bio= biolist ; bio ; bio=bio->bi_next) {
3106 struct bio *bio2;
3107 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
3108 if (bio_add_page(bio, page, len, 0))
3109 continue;
3111 /* stop here */
3112 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3113 for (bio2 = biolist;
3114 bio2 && bio2 != bio;
3115 bio2 = bio2->bi_next) {
3116 /* remove last page from this bio */
3117 bio2->bi_vcnt--;
3118 bio2->bi_size -= len;
3119 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
3121 goto bio_full;
3123 nr_sectors += len>>9;
3124 sector_nr += len>>9;
3125 } while (biolist->bi_vcnt < RESYNC_PAGES);
3126 bio_full:
3127 r10_bio->sectors = nr_sectors;
3129 while (biolist) {
3130 bio = biolist;
3131 biolist = biolist->bi_next;
3133 bio->bi_next = NULL;
3134 r10_bio = bio->bi_private;
3135 r10_bio->sectors = nr_sectors;
3137 if (bio->bi_end_io == end_sync_read) {
3138 md_sync_acct(bio->bi_bdev, nr_sectors);
3139 generic_make_request(bio);
3143 if (sectors_skipped)
3144 /* pretend they weren't skipped, it makes
3145 * no important difference in this case
3147 md_done_sync(mddev, sectors_skipped, 1);
3149 return sectors_skipped + nr_sectors;
3150 giveup:
3151 /* There is nowhere to write, so all non-sync
3152 * drives must be failed or in resync, all drives
3153 * have a bad block, so try the next chunk...
3155 if (sector_nr + max_sync < max_sector)
3156 max_sector = sector_nr + max_sync;
3158 sectors_skipped += (max_sector - sector_nr);
3159 chunks_skipped ++;
3160 sector_nr = max_sector;
3161 goto skipped;
3164 static sector_t
3165 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3167 sector_t size;
3168 struct r10conf *conf = mddev->private;
3170 if (!raid_disks)
3171 raid_disks = conf->raid_disks;
3172 if (!sectors)
3173 sectors = conf->dev_sectors;
3175 size = sectors >> conf->chunk_shift;
3176 sector_div(size, conf->far_copies);
3177 size = size * raid_disks;
3178 sector_div(size, conf->near_copies);
3180 return size << conf->chunk_shift;
3183 static void calc_sectors(struct r10conf *conf, sector_t size)
3185 /* Calculate the number of sectors-per-device that will
3186 * actually be used, and set conf->dev_sectors and
3187 * conf->stride
3190 size = size >> conf->chunk_shift;
3191 sector_div(size, conf->far_copies);
3192 size = size * conf->raid_disks;
3193 sector_div(size, conf->near_copies);
3194 /* 'size' is now the number of chunks in the array */
3195 /* calculate "used chunks per device" */
3196 size = size * conf->copies;
3198 /* We need to round up when dividing by raid_disks to
3199 * get the stride size.
3201 size = DIV_ROUND_UP_SECTOR_T(size, conf->raid_disks);
3203 conf->dev_sectors = size << conf->chunk_shift;
3205 if (conf->far_offset)
3206 conf->stride = 1 << conf->chunk_shift;
3207 else {
3208 sector_div(size, conf->far_copies);
3209 conf->stride = size << conf->chunk_shift;
3213 static struct r10conf *setup_conf(struct mddev *mddev)
3215 struct r10conf *conf = NULL;
3216 int nc, fc, fo;
3217 int err = -EINVAL;
3219 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
3220 !is_power_of_2(mddev->new_chunk_sectors)) {
3221 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3222 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3223 mdname(mddev), PAGE_SIZE);
3224 goto out;
3227 nc = mddev->new_layout & 255;
3228 fc = (mddev->new_layout >> 8) & 255;
3229 fo = mddev->new_layout & (1<<16);
3231 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
3232 (mddev->new_layout >> 17)) {
3233 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3234 mdname(mddev), mddev->new_layout);
3235 goto out;
3238 err = -ENOMEM;
3239 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3240 if (!conf)
3241 goto out;
3243 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
3244 GFP_KERNEL);
3245 if (!conf->mirrors)
3246 goto out;
3248 conf->tmppage = alloc_page(GFP_KERNEL);
3249 if (!conf->tmppage)
3250 goto out;
3253 conf->raid_disks = mddev->raid_disks;
3254 conf->near_copies = nc;
3255 conf->far_copies = fc;
3256 conf->copies = nc*fc;
3257 conf->far_offset = fo;
3258 conf->chunk_mask = mddev->new_chunk_sectors - 1;
3259 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
3261 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3262 r10bio_pool_free, conf);
3263 if (!conf->r10bio_pool)
3264 goto out;
3266 calc_sectors(conf, mddev->dev_sectors);
3268 spin_lock_init(&conf->device_lock);
3269 INIT_LIST_HEAD(&conf->retry_list);
3271 spin_lock_init(&conf->resync_lock);
3272 init_waitqueue_head(&conf->wait_barrier);
3274 conf->thread = md_register_thread(raid10d, mddev, NULL);
3275 if (!conf->thread)
3276 goto out;
3278 conf->mddev = mddev;
3279 return conf;
3281 out:
3282 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3283 mdname(mddev));
3284 if (conf) {
3285 if (conf->r10bio_pool)
3286 mempool_destroy(conf->r10bio_pool);
3287 kfree(conf->mirrors);
3288 safe_put_page(conf->tmppage);
3289 kfree(conf);
3291 return ERR_PTR(err);
3294 static int run(struct mddev *mddev)
3296 struct r10conf *conf;
3297 int i, disk_idx, chunk_size;
3298 struct mirror_info *disk;
3299 struct md_rdev *rdev;
3300 sector_t size;
3303 * copy the already verified devices into our private RAID10
3304 * bookkeeping area. [whatever we allocate in run(),
3305 * should be freed in stop()]
3308 if (mddev->private == NULL) {
3309 conf = setup_conf(mddev);
3310 if (IS_ERR(conf))
3311 return PTR_ERR(conf);
3312 mddev->private = conf;
3314 conf = mddev->private;
3315 if (!conf)
3316 goto out;
3318 mddev->thread = conf->thread;
3319 conf->thread = NULL;
3321 chunk_size = mddev->chunk_sectors << 9;
3322 blk_queue_io_min(mddev->queue, chunk_size);
3323 if (conf->raid_disks % conf->near_copies)
3324 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
3325 else
3326 blk_queue_io_opt(mddev->queue, chunk_size *
3327 (conf->raid_disks / conf->near_copies));
3329 rdev_for_each(rdev, mddev) {
3330 struct request_queue *q;
3331 disk_idx = rdev->raid_disk;
3332 if (disk_idx >= conf->raid_disks
3333 || disk_idx < 0)
3334 continue;
3335 disk = conf->mirrors + disk_idx;
3337 if (test_bit(Replacement, &rdev->flags)) {
3338 if (disk->replacement)
3339 goto out_free_conf;
3340 disk->replacement = rdev;
3341 } else {
3342 if (disk->rdev)
3343 goto out_free_conf;
3344 disk->rdev = rdev;
3346 q = bdev_get_queue(rdev->bdev);
3347 if (q->merge_bvec_fn)
3348 mddev->merge_check_needed = 1;
3350 disk_stack_limits(mddev->gendisk, rdev->bdev,
3351 rdev->data_offset << 9);
3353 disk->head_position = 0;
3355 /* need to check that every block has at least one working mirror */
3356 if (!enough(conf, -1)) {
3357 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
3358 mdname(mddev));
3359 goto out_free_conf;
3362 mddev->degraded = 0;
3363 for (i = 0; i < conf->raid_disks; i++) {
3365 disk = conf->mirrors + i;
3367 if (!disk->rdev && disk->replacement) {
3368 /* The replacement is all we have - use it */
3369 disk->rdev = disk->replacement;
3370 disk->replacement = NULL;
3371 clear_bit(Replacement, &disk->rdev->flags);
3374 if (!disk->rdev ||
3375 !test_bit(In_sync, &disk->rdev->flags)) {
3376 disk->head_position = 0;
3377 mddev->degraded++;
3378 if (disk->rdev)
3379 conf->fullsync = 1;
3381 disk->recovery_disabled = mddev->recovery_disabled - 1;
3384 if (mddev->recovery_cp != MaxSector)
3385 printk(KERN_NOTICE "md/raid10:%s: not clean"
3386 " -- starting background reconstruction\n",
3387 mdname(mddev));
3388 printk(KERN_INFO
3389 "md/raid10:%s: active with %d out of %d devices\n",
3390 mdname(mddev), conf->raid_disks - mddev->degraded,
3391 conf->raid_disks);
3393 * Ok, everything is just fine now
3395 mddev->dev_sectors = conf->dev_sectors;
3396 size = raid10_size(mddev, 0, 0);
3397 md_set_array_sectors(mddev, size);
3398 mddev->resync_max_sectors = size;
3400 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3401 mddev->queue->backing_dev_info.congested_data = mddev;
3403 /* Calculate max read-ahead size.
3404 * We need to readahead at least twice a whole stripe....
3405 * maybe...
3408 int stripe = conf->raid_disks *
3409 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3410 stripe /= conf->near_copies;
3411 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3412 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3415 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
3417 if (md_integrity_register(mddev))
3418 goto out_free_conf;
3420 return 0;
3422 out_free_conf:
3423 md_unregister_thread(&mddev->thread);
3424 if (conf->r10bio_pool)
3425 mempool_destroy(conf->r10bio_pool);
3426 safe_put_page(conf->tmppage);
3427 kfree(conf->mirrors);
3428 kfree(conf);
3429 mddev->private = NULL;
3430 out:
3431 return -EIO;
3434 static int stop(struct mddev *mddev)
3436 struct r10conf *conf = mddev->private;
3438 raise_barrier(conf, 0);
3439 lower_barrier(conf);
3441 md_unregister_thread(&mddev->thread);
3442 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3443 if (conf->r10bio_pool)
3444 mempool_destroy(conf->r10bio_pool);
3445 kfree(conf->mirrors);
3446 kfree(conf);
3447 mddev->private = NULL;
3448 return 0;
3451 static void raid10_quiesce(struct mddev *mddev, int state)
3453 struct r10conf *conf = mddev->private;
3455 switch(state) {
3456 case 1:
3457 raise_barrier(conf, 0);
3458 break;
3459 case 0:
3460 lower_barrier(conf);
3461 break;
3465 static int raid10_resize(struct mddev *mddev, sector_t sectors)
3467 /* Resize of 'far' arrays is not supported.
3468 * For 'near' and 'offset' arrays we can set the
3469 * number of sectors used to be an appropriate multiple
3470 * of the chunk size.
3471 * For 'offset', this is far_copies*chunksize.
3472 * For 'near' the multiplier is the LCM of
3473 * near_copies and raid_disks.
3474 * So if far_copies > 1 && !far_offset, fail.
3475 * Else find LCM(raid_disks, near_copy)*far_copies and
3476 * multiply by chunk_size. Then round to this number.
3477 * This is mostly done by raid10_size()
3479 struct r10conf *conf = mddev->private;
3480 sector_t oldsize, size;
3482 if (conf->far_copies > 1 && !conf->far_offset)
3483 return -EINVAL;
3485 oldsize = raid10_size(mddev, 0, 0);
3486 size = raid10_size(mddev, sectors, 0);
3487 md_set_array_sectors(mddev, size);
3488 if (mddev->array_sectors > size)
3489 return -EINVAL;
3490 set_capacity(mddev->gendisk, mddev->array_sectors);
3491 revalidate_disk(mddev->gendisk);
3492 if (sectors > mddev->dev_sectors &&
3493 mddev->recovery_cp > oldsize) {
3494 mddev->recovery_cp = oldsize;
3495 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3497 calc_sectors(conf, sectors);
3498 mddev->dev_sectors = conf->dev_sectors;
3499 mddev->resync_max_sectors = size;
3500 return 0;
3503 static void *raid10_takeover_raid0(struct mddev *mddev)
3505 struct md_rdev *rdev;
3506 struct r10conf *conf;
3508 if (mddev->degraded > 0) {
3509 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3510 mdname(mddev));
3511 return ERR_PTR(-EINVAL);
3514 /* Set new parameters */
3515 mddev->new_level = 10;
3516 /* new layout: far_copies = 1, near_copies = 2 */
3517 mddev->new_layout = (1<<8) + 2;
3518 mddev->new_chunk_sectors = mddev->chunk_sectors;
3519 mddev->delta_disks = mddev->raid_disks;
3520 mddev->raid_disks *= 2;
3521 /* make sure it will be not marked as dirty */
3522 mddev->recovery_cp = MaxSector;
3524 conf = setup_conf(mddev);
3525 if (!IS_ERR(conf)) {
3526 rdev_for_each(rdev, mddev)
3527 if (rdev->raid_disk >= 0)
3528 rdev->new_raid_disk = rdev->raid_disk * 2;
3529 conf->barrier = 1;
3532 return conf;
3535 static void *raid10_takeover(struct mddev *mddev)
3537 struct r0conf *raid0_conf;
3539 /* raid10 can take over:
3540 * raid0 - providing it has only two drives
3542 if (mddev->level == 0) {
3543 /* for raid0 takeover only one zone is supported */
3544 raid0_conf = mddev->private;
3545 if (raid0_conf->nr_strip_zones > 1) {
3546 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3547 " with more than one zone.\n",
3548 mdname(mddev));
3549 return ERR_PTR(-EINVAL);
3551 return raid10_takeover_raid0(mddev);
3553 return ERR_PTR(-EINVAL);
3556 static struct md_personality raid10_personality =
3558 .name = "raid10",
3559 .level = 10,
3560 .owner = THIS_MODULE,
3561 .make_request = make_request,
3562 .run = run,
3563 .stop = stop,
3564 .status = status,
3565 .error_handler = error,
3566 .hot_add_disk = raid10_add_disk,
3567 .hot_remove_disk= raid10_remove_disk,
3568 .spare_active = raid10_spare_active,
3569 .sync_request = sync_request,
3570 .quiesce = raid10_quiesce,
3571 .size = raid10_size,
3572 .resize = raid10_resize,
3573 .takeover = raid10_takeover,
3576 static int __init raid_init(void)
3578 return register_md_personality(&raid10_personality);
3581 static void raid_exit(void)
3583 unregister_md_personality(&raid10_personality);
3586 module_init(raid_init);
3587 module_exit(raid_exit);
3588 MODULE_LICENSE("GPL");
3589 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
3590 MODULE_ALIAS("md-personality-9"); /* RAID10 */
3591 MODULE_ALIAS("md-raid10");
3592 MODULE_ALIAS("md-level-10");
3594 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);