blkcg: move refcnt to blkcg core
[linux-2.6.git] / drivers / md / raid10.c
blob6e8aa213f0d5208d917b8222a56980ab3582b4d6
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);
71 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
73 struct r10conf *conf = data;
74 int size = offsetof(struct r10bio, devs[conf->copies]);
76 /* allocate a r10bio with room for raid_disks entries in the
77 * bios array */
78 return kzalloc(size, gfp_flags);
81 static void r10bio_pool_free(void *r10_bio, void *data)
83 kfree(r10_bio);
86 /* Maximum size of each resync request */
87 #define RESYNC_BLOCK_SIZE (64*1024)
88 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
89 /* amount of memory to reserve for resync requests */
90 #define RESYNC_WINDOW (1024*1024)
91 /* maximum number of concurrent requests, memory permitting */
92 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
95 * When performing a resync, we need to read and compare, so
96 * we need as many pages are there are copies.
97 * When performing a recovery, we need 2 bios, one for read,
98 * one for write (we recover only one drive per r10buf)
101 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
103 struct r10conf *conf = data;
104 struct page *page;
105 struct r10bio *r10_bio;
106 struct bio *bio;
107 int i, j;
108 int nalloc;
110 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
111 if (!r10_bio)
112 return NULL;
114 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
115 nalloc = conf->copies; /* resync */
116 else
117 nalloc = 2; /* recovery */
120 * Allocate bios.
122 for (j = nalloc ; j-- ; ) {
123 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
124 if (!bio)
125 goto out_free_bio;
126 r10_bio->devs[j].bio = bio;
127 if (!conf->have_replacement)
128 continue;
129 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
130 if (!bio)
131 goto out_free_bio;
132 r10_bio->devs[j].repl_bio = bio;
135 * Allocate RESYNC_PAGES data pages and attach them
136 * where needed.
138 for (j = 0 ; j < nalloc; j++) {
139 struct bio *rbio = r10_bio->devs[j].repl_bio;
140 bio = r10_bio->devs[j].bio;
141 for (i = 0; i < RESYNC_PAGES; i++) {
142 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
143 &conf->mddev->recovery)) {
144 /* we can share bv_page's during recovery */
145 struct bio *rbio = r10_bio->devs[0].bio;
146 page = rbio->bi_io_vec[i].bv_page;
147 get_page(page);
148 } else
149 page = alloc_page(gfp_flags);
150 if (unlikely(!page))
151 goto out_free_pages;
153 bio->bi_io_vec[i].bv_page = page;
154 if (rbio)
155 rbio->bi_io_vec[i].bv_page = page;
159 return r10_bio;
161 out_free_pages:
162 for ( ; i > 0 ; i--)
163 safe_put_page(bio->bi_io_vec[i-1].bv_page);
164 while (j--)
165 for (i = 0; i < RESYNC_PAGES ; i++)
166 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
167 j = -1;
168 out_free_bio:
169 while (++j < nalloc) {
170 bio_put(r10_bio->devs[j].bio);
171 if (r10_bio->devs[j].repl_bio)
172 bio_put(r10_bio->devs[j].repl_bio);
174 r10bio_pool_free(r10_bio, conf);
175 return NULL;
178 static void r10buf_pool_free(void *__r10_bio, void *data)
180 int i;
181 struct r10conf *conf = data;
182 struct r10bio *r10bio = __r10_bio;
183 int j;
185 for (j=0; j < conf->copies; j++) {
186 struct bio *bio = r10bio->devs[j].bio;
187 if (bio) {
188 for (i = 0; i < RESYNC_PAGES; i++) {
189 safe_put_page(bio->bi_io_vec[i].bv_page);
190 bio->bi_io_vec[i].bv_page = NULL;
192 bio_put(bio);
194 bio = r10bio->devs[j].repl_bio;
195 if (bio)
196 bio_put(bio);
198 r10bio_pool_free(r10bio, conf);
201 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
203 int i;
205 for (i = 0; i < conf->copies; i++) {
206 struct bio **bio = & r10_bio->devs[i].bio;
207 if (!BIO_SPECIAL(*bio))
208 bio_put(*bio);
209 *bio = NULL;
210 bio = &r10_bio->devs[i].repl_bio;
211 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
212 bio_put(*bio);
213 *bio = NULL;
217 static void free_r10bio(struct r10bio *r10_bio)
219 struct r10conf *conf = r10_bio->mddev->private;
221 put_all_bios(conf, r10_bio);
222 mempool_free(r10_bio, conf->r10bio_pool);
225 static void put_buf(struct r10bio *r10_bio)
227 struct r10conf *conf = r10_bio->mddev->private;
229 mempool_free(r10_bio, conf->r10buf_pool);
231 lower_barrier(conf);
234 static void reschedule_retry(struct r10bio *r10_bio)
236 unsigned long flags;
237 struct mddev *mddev = r10_bio->mddev;
238 struct r10conf *conf = mddev->private;
240 spin_lock_irqsave(&conf->device_lock, flags);
241 list_add(&r10_bio->retry_list, &conf->retry_list);
242 conf->nr_queued ++;
243 spin_unlock_irqrestore(&conf->device_lock, flags);
245 /* wake up frozen array... */
246 wake_up(&conf->wait_barrier);
248 md_wakeup_thread(mddev->thread);
252 * raid_end_bio_io() is called when we have finished servicing a mirrored
253 * operation and are ready to return a success/failure code to the buffer
254 * cache layer.
256 static void raid_end_bio_io(struct r10bio *r10_bio)
258 struct bio *bio = r10_bio->master_bio;
259 int done;
260 struct r10conf *conf = r10_bio->mddev->private;
262 if (bio->bi_phys_segments) {
263 unsigned long flags;
264 spin_lock_irqsave(&conf->device_lock, flags);
265 bio->bi_phys_segments--;
266 done = (bio->bi_phys_segments == 0);
267 spin_unlock_irqrestore(&conf->device_lock, flags);
268 } else
269 done = 1;
270 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
271 clear_bit(BIO_UPTODATE, &bio->bi_flags);
272 if (done) {
273 bio_endio(bio, 0);
275 * Wake up any possible resync thread that waits for the device
276 * to go idle.
278 allow_barrier(conf);
280 free_r10bio(r10_bio);
284 * Update disk head position estimator based on IRQ completion info.
286 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
288 struct r10conf *conf = r10_bio->mddev->private;
290 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
291 r10_bio->devs[slot].addr + (r10_bio->sectors);
295 * Find the disk number which triggered given bio
297 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
298 struct bio *bio, int *slotp, int *replp)
300 int slot;
301 int repl = 0;
303 for (slot = 0; slot < conf->copies; slot++) {
304 if (r10_bio->devs[slot].bio == bio)
305 break;
306 if (r10_bio->devs[slot].repl_bio == bio) {
307 repl = 1;
308 break;
312 BUG_ON(slot == conf->copies);
313 update_head_pos(slot, r10_bio);
315 if (slotp)
316 *slotp = slot;
317 if (replp)
318 *replp = repl;
319 return r10_bio->devs[slot].devnum;
322 static void raid10_end_read_request(struct bio *bio, int error)
324 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
325 struct r10bio *r10_bio = bio->bi_private;
326 int slot, dev;
327 struct md_rdev *rdev;
328 struct r10conf *conf = r10_bio->mddev->private;
331 slot = r10_bio->read_slot;
332 dev = r10_bio->devs[slot].devnum;
333 rdev = r10_bio->devs[slot].rdev;
335 * this branch is our 'one mirror IO has finished' event handler:
337 update_head_pos(slot, r10_bio);
339 if (uptodate) {
341 * Set R10BIO_Uptodate in our master bio, so that
342 * we will return a good error code to the higher
343 * levels even if IO on some other mirrored buffer fails.
345 * The 'master' represents the composite IO operation to
346 * user-side. So if something waits for IO, then it will
347 * wait for the 'master' bio.
349 set_bit(R10BIO_Uptodate, &r10_bio->state);
350 raid_end_bio_io(r10_bio);
351 rdev_dec_pending(rdev, conf->mddev);
352 } else {
354 * oops, read error - keep the refcount on the rdev
356 char b[BDEVNAME_SIZE];
357 printk_ratelimited(KERN_ERR
358 "md/raid10:%s: %s: rescheduling sector %llu\n",
359 mdname(conf->mddev),
360 bdevname(rdev->bdev, b),
361 (unsigned long long)r10_bio->sector);
362 set_bit(R10BIO_ReadError, &r10_bio->state);
363 reschedule_retry(r10_bio);
367 static void close_write(struct r10bio *r10_bio)
369 /* clear the bitmap if all writes complete successfully */
370 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
371 r10_bio->sectors,
372 !test_bit(R10BIO_Degraded, &r10_bio->state),
374 md_write_end(r10_bio->mddev);
377 static void one_write_done(struct r10bio *r10_bio)
379 if (atomic_dec_and_test(&r10_bio->remaining)) {
380 if (test_bit(R10BIO_WriteError, &r10_bio->state))
381 reschedule_retry(r10_bio);
382 else {
383 close_write(r10_bio);
384 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
385 reschedule_retry(r10_bio);
386 else
387 raid_end_bio_io(r10_bio);
392 static void raid10_end_write_request(struct bio *bio, int error)
394 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
395 struct r10bio *r10_bio = bio->bi_private;
396 int dev;
397 int dec_rdev = 1;
398 struct r10conf *conf = r10_bio->mddev->private;
399 int slot, repl;
400 struct md_rdev *rdev = NULL;
402 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
404 if (repl)
405 rdev = conf->mirrors[dev].replacement;
406 if (!rdev) {
407 smp_rmb();
408 repl = 0;
409 rdev = conf->mirrors[dev].rdev;
412 * this branch is our 'one mirror IO has finished' event handler:
414 if (!uptodate) {
415 if (repl)
416 /* Never record new bad blocks to replacement,
417 * just fail it.
419 md_error(rdev->mddev, rdev);
420 else {
421 set_bit(WriteErrorSeen, &rdev->flags);
422 if (!test_and_set_bit(WantReplacement, &rdev->flags))
423 set_bit(MD_RECOVERY_NEEDED,
424 &rdev->mddev->recovery);
425 set_bit(R10BIO_WriteError, &r10_bio->state);
426 dec_rdev = 0;
428 } else {
430 * Set R10BIO_Uptodate in our master bio, so that
431 * we will return a good error code for to the higher
432 * levels even if IO on some other mirrored buffer fails.
434 * The 'master' represents the composite IO operation to
435 * user-side. So if something waits for IO, then it will
436 * wait for the 'master' bio.
438 sector_t first_bad;
439 int bad_sectors;
441 set_bit(R10BIO_Uptodate, &r10_bio->state);
443 /* Maybe we can clear some bad blocks. */
444 if (is_badblock(rdev,
445 r10_bio->devs[slot].addr,
446 r10_bio->sectors,
447 &first_bad, &bad_sectors)) {
448 bio_put(bio);
449 if (repl)
450 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
451 else
452 r10_bio->devs[slot].bio = IO_MADE_GOOD;
453 dec_rdev = 0;
454 set_bit(R10BIO_MadeGood, &r10_bio->state);
460 * Let's see if all mirrored write operations have finished
461 * already.
463 one_write_done(r10_bio);
464 if (dec_rdev)
465 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
469 * RAID10 layout manager
470 * As well as the chunksize and raid_disks count, there are two
471 * parameters: near_copies and far_copies.
472 * near_copies * far_copies must be <= raid_disks.
473 * Normally one of these will be 1.
474 * If both are 1, we get raid0.
475 * If near_copies == raid_disks, we get raid1.
477 * Chunks are laid out in raid0 style with near_copies copies of the
478 * first chunk, followed by near_copies copies of the next chunk and
479 * so on.
480 * If far_copies > 1, then after 1/far_copies of the array has been assigned
481 * as described above, we start again with a device offset of near_copies.
482 * So we effectively have another copy of the whole array further down all
483 * the drives, but with blocks on different drives.
484 * With this layout, and block is never stored twice on the one device.
486 * raid10_find_phys finds the sector offset of a given virtual sector
487 * on each device that it is on.
489 * raid10_find_virt does the reverse mapping, from a device and a
490 * sector offset to a virtual address
493 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
495 int n,f;
496 sector_t sector;
497 sector_t chunk;
498 sector_t stripe;
499 int dev;
501 int slot = 0;
503 /* now calculate first sector/dev */
504 chunk = r10bio->sector >> conf->chunk_shift;
505 sector = r10bio->sector & conf->chunk_mask;
507 chunk *= conf->near_copies;
508 stripe = chunk;
509 dev = sector_div(stripe, conf->raid_disks);
510 if (conf->far_offset)
511 stripe *= conf->far_copies;
513 sector += stripe << conf->chunk_shift;
515 /* and calculate all the others */
516 for (n=0; n < conf->near_copies; n++) {
517 int d = dev;
518 sector_t s = sector;
519 r10bio->devs[slot].addr = sector;
520 r10bio->devs[slot].devnum = d;
521 slot++;
523 for (f = 1; f < conf->far_copies; f++) {
524 d += conf->near_copies;
525 if (d >= conf->raid_disks)
526 d -= conf->raid_disks;
527 s += conf->stride;
528 r10bio->devs[slot].devnum = d;
529 r10bio->devs[slot].addr = s;
530 slot++;
532 dev++;
533 if (dev >= conf->raid_disks) {
534 dev = 0;
535 sector += (conf->chunk_mask + 1);
538 BUG_ON(slot != conf->copies);
541 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
543 sector_t offset, chunk, vchunk;
545 offset = sector & conf->chunk_mask;
546 if (conf->far_offset) {
547 int fc;
548 chunk = sector >> conf->chunk_shift;
549 fc = sector_div(chunk, conf->far_copies);
550 dev -= fc * conf->near_copies;
551 if (dev < 0)
552 dev += conf->raid_disks;
553 } else {
554 while (sector >= conf->stride) {
555 sector -= conf->stride;
556 if (dev < conf->near_copies)
557 dev += conf->raid_disks - conf->near_copies;
558 else
559 dev -= conf->near_copies;
561 chunk = sector >> conf->chunk_shift;
563 vchunk = chunk * conf->raid_disks + dev;
564 sector_div(vchunk, conf->near_copies);
565 return (vchunk << conf->chunk_shift) + offset;
569 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
570 * @q: request queue
571 * @bvm: properties of new bio
572 * @biovec: the request that could be merged to it.
574 * Return amount of bytes we can accept at this offset
575 * If near_copies == raid_disk, there are no striping issues,
576 * but in that case, the function isn't called at all.
578 static int raid10_mergeable_bvec(struct request_queue *q,
579 struct bvec_merge_data *bvm,
580 struct bio_vec *biovec)
582 struct mddev *mddev = q->queuedata;
583 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
584 int max;
585 unsigned int chunk_sectors = mddev->chunk_sectors;
586 unsigned int bio_sectors = bvm->bi_size >> 9;
588 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
589 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
590 if (max <= biovec->bv_len && bio_sectors == 0)
591 return biovec->bv_len;
592 else
593 return max;
597 * This routine returns the disk from which the requested read should
598 * be done. There is a per-array 'next expected sequential IO' sector
599 * number - if this matches on the next IO then we use the last disk.
600 * There is also a per-disk 'last know head position' sector that is
601 * maintained from IRQ contexts, both the normal and the resync IO
602 * completion handlers update this position correctly. If there is no
603 * perfect sequential match then we pick the disk whose head is closest.
605 * If there are 2 mirrors in the same 2 devices, performance degrades
606 * because position is mirror, not device based.
608 * The rdev for the device selected will have nr_pending incremented.
612 * FIXME: possibly should rethink readbalancing and do it differently
613 * depending on near_copies / far_copies geometry.
615 static struct md_rdev *read_balance(struct r10conf *conf,
616 struct r10bio *r10_bio,
617 int *max_sectors)
619 const sector_t this_sector = r10_bio->sector;
620 int disk, slot;
621 int sectors = r10_bio->sectors;
622 int best_good_sectors;
623 sector_t new_distance, best_dist;
624 struct md_rdev *rdev, *best_rdev;
625 int do_balance;
626 int best_slot;
628 raid10_find_phys(conf, r10_bio);
629 rcu_read_lock();
630 retry:
631 sectors = r10_bio->sectors;
632 best_slot = -1;
633 best_rdev = NULL;
634 best_dist = MaxSector;
635 best_good_sectors = 0;
636 do_balance = 1;
638 * Check if we can balance. We can balance on the whole
639 * device if no resync is going on (recovery is ok), or below
640 * the resync window. We take the first readable disk when
641 * above the resync window.
643 if (conf->mddev->recovery_cp < MaxSector
644 && (this_sector + sectors >= conf->next_resync))
645 do_balance = 0;
647 for (slot = 0; slot < conf->copies ; slot++) {
648 sector_t first_bad;
649 int bad_sectors;
650 sector_t dev_sector;
652 if (r10_bio->devs[slot].bio == IO_BLOCKED)
653 continue;
654 disk = r10_bio->devs[slot].devnum;
655 rdev = rcu_dereference(conf->mirrors[disk].replacement);
656 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
657 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
658 rdev = rcu_dereference(conf->mirrors[disk].rdev);
659 if (rdev == NULL)
660 continue;
661 if (test_bit(Faulty, &rdev->flags))
662 continue;
663 if (!test_bit(In_sync, &rdev->flags) &&
664 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
665 continue;
667 dev_sector = r10_bio->devs[slot].addr;
668 if (is_badblock(rdev, dev_sector, sectors,
669 &first_bad, &bad_sectors)) {
670 if (best_dist < MaxSector)
671 /* Already have a better slot */
672 continue;
673 if (first_bad <= dev_sector) {
674 /* Cannot read here. If this is the
675 * 'primary' device, then we must not read
676 * beyond 'bad_sectors' from another device.
678 bad_sectors -= (dev_sector - first_bad);
679 if (!do_balance && sectors > bad_sectors)
680 sectors = bad_sectors;
681 if (best_good_sectors > sectors)
682 best_good_sectors = sectors;
683 } else {
684 sector_t good_sectors =
685 first_bad - dev_sector;
686 if (good_sectors > best_good_sectors) {
687 best_good_sectors = good_sectors;
688 best_slot = slot;
689 best_rdev = rdev;
691 if (!do_balance)
692 /* Must read from here */
693 break;
695 continue;
696 } else
697 best_good_sectors = sectors;
699 if (!do_balance)
700 break;
702 /* This optimisation is debatable, and completely destroys
703 * sequential read speed for 'far copies' arrays. So only
704 * keep it for 'near' arrays, and review those later.
706 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
707 break;
709 /* for far > 1 always use the lowest address */
710 if (conf->far_copies > 1)
711 new_distance = r10_bio->devs[slot].addr;
712 else
713 new_distance = abs(r10_bio->devs[slot].addr -
714 conf->mirrors[disk].head_position);
715 if (new_distance < best_dist) {
716 best_dist = new_distance;
717 best_slot = slot;
718 best_rdev = rdev;
721 if (slot >= conf->copies) {
722 slot = best_slot;
723 rdev = best_rdev;
726 if (slot >= 0) {
727 atomic_inc(&rdev->nr_pending);
728 if (test_bit(Faulty, &rdev->flags)) {
729 /* Cannot risk returning a device that failed
730 * before we inc'ed nr_pending
732 rdev_dec_pending(rdev, conf->mddev);
733 goto retry;
735 r10_bio->read_slot = slot;
736 } else
737 rdev = NULL;
738 rcu_read_unlock();
739 *max_sectors = best_good_sectors;
741 return rdev;
744 static int raid10_congested(void *data, int bits)
746 struct mddev *mddev = data;
747 struct r10conf *conf = mddev->private;
748 int i, ret = 0;
750 if ((bits & (1 << BDI_async_congested)) &&
751 conf->pending_count >= max_queued_requests)
752 return 1;
754 if (mddev_congested(mddev, bits))
755 return 1;
756 rcu_read_lock();
757 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
758 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
759 if (rdev && !test_bit(Faulty, &rdev->flags)) {
760 struct request_queue *q = bdev_get_queue(rdev->bdev);
762 ret |= bdi_congested(&q->backing_dev_info, bits);
765 rcu_read_unlock();
766 return ret;
769 static void flush_pending_writes(struct r10conf *conf)
771 /* Any writes that have been queued but are awaiting
772 * bitmap updates get flushed here.
774 spin_lock_irq(&conf->device_lock);
776 if (conf->pending_bio_list.head) {
777 struct bio *bio;
778 bio = bio_list_get(&conf->pending_bio_list);
779 conf->pending_count = 0;
780 spin_unlock_irq(&conf->device_lock);
781 /* flush any pending bitmap writes to disk
782 * before proceeding w/ I/O */
783 bitmap_unplug(conf->mddev->bitmap);
784 wake_up(&conf->wait_barrier);
786 while (bio) { /* submit pending writes */
787 struct bio *next = bio->bi_next;
788 bio->bi_next = NULL;
789 generic_make_request(bio);
790 bio = next;
792 } else
793 spin_unlock_irq(&conf->device_lock);
796 /* Barriers....
797 * Sometimes we need to suspend IO while we do something else,
798 * either some resync/recovery, or reconfigure the array.
799 * To do this we raise a 'barrier'.
800 * The 'barrier' is a counter that can be raised multiple times
801 * to count how many activities are happening which preclude
802 * normal IO.
803 * We can only raise the barrier if there is no pending IO.
804 * i.e. if nr_pending == 0.
805 * We choose only to raise the barrier if no-one is waiting for the
806 * barrier to go down. This means that as soon as an IO request
807 * is ready, no other operations which require a barrier will start
808 * until the IO request has had a chance.
810 * So: regular IO calls 'wait_barrier'. When that returns there
811 * is no backgroup IO happening, It must arrange to call
812 * allow_barrier when it has finished its IO.
813 * backgroup IO calls must call raise_barrier. Once that returns
814 * there is no normal IO happeing. It must arrange to call
815 * lower_barrier when the particular background IO completes.
818 static void raise_barrier(struct r10conf *conf, int force)
820 BUG_ON(force && !conf->barrier);
821 spin_lock_irq(&conf->resync_lock);
823 /* Wait until no block IO is waiting (unless 'force') */
824 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
825 conf->resync_lock, );
827 /* block any new IO from starting */
828 conf->barrier++;
830 /* Now wait for all pending IO to complete */
831 wait_event_lock_irq(conf->wait_barrier,
832 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
833 conf->resync_lock, );
835 spin_unlock_irq(&conf->resync_lock);
838 static void lower_barrier(struct r10conf *conf)
840 unsigned long flags;
841 spin_lock_irqsave(&conf->resync_lock, flags);
842 conf->barrier--;
843 spin_unlock_irqrestore(&conf->resync_lock, flags);
844 wake_up(&conf->wait_barrier);
847 static void wait_barrier(struct r10conf *conf)
849 spin_lock_irq(&conf->resync_lock);
850 if (conf->barrier) {
851 conf->nr_waiting++;
852 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
853 conf->resync_lock,
855 conf->nr_waiting--;
857 conf->nr_pending++;
858 spin_unlock_irq(&conf->resync_lock);
861 static void allow_barrier(struct r10conf *conf)
863 unsigned long flags;
864 spin_lock_irqsave(&conf->resync_lock, flags);
865 conf->nr_pending--;
866 spin_unlock_irqrestore(&conf->resync_lock, flags);
867 wake_up(&conf->wait_barrier);
870 static void freeze_array(struct r10conf *conf)
872 /* stop syncio and normal IO and wait for everything to
873 * go quiet.
874 * We increment barrier and nr_waiting, and then
875 * wait until nr_pending match nr_queued+1
876 * This is called in the context of one normal IO request
877 * that has failed. Thus any sync request that might be pending
878 * will be blocked by nr_pending, and we need to wait for
879 * pending IO requests to complete or be queued for re-try.
880 * Thus the number queued (nr_queued) plus this request (1)
881 * must match the number of pending IOs (nr_pending) before
882 * we continue.
884 spin_lock_irq(&conf->resync_lock);
885 conf->barrier++;
886 conf->nr_waiting++;
887 wait_event_lock_irq(conf->wait_barrier,
888 conf->nr_pending == conf->nr_queued+1,
889 conf->resync_lock,
890 flush_pending_writes(conf));
892 spin_unlock_irq(&conf->resync_lock);
895 static void unfreeze_array(struct r10conf *conf)
897 /* reverse the effect of the freeze */
898 spin_lock_irq(&conf->resync_lock);
899 conf->barrier--;
900 conf->nr_waiting--;
901 wake_up(&conf->wait_barrier);
902 spin_unlock_irq(&conf->resync_lock);
905 static void make_request(struct mddev *mddev, struct bio * bio)
907 struct r10conf *conf = mddev->private;
908 struct r10bio *r10_bio;
909 struct bio *read_bio;
910 int i;
911 int chunk_sects = conf->chunk_mask + 1;
912 const int rw = bio_data_dir(bio);
913 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
914 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
915 unsigned long flags;
916 struct md_rdev *blocked_rdev;
917 int plugged;
918 int sectors_handled;
919 int max_sectors;
921 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
922 md_flush_request(mddev, bio);
923 return;
926 /* If this request crosses a chunk boundary, we need to
927 * split it. This will only happen for 1 PAGE (or less) requests.
929 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
930 > chunk_sects &&
931 conf->near_copies < conf->raid_disks)) {
932 struct bio_pair *bp;
933 /* Sanity check -- queue functions should prevent this happening */
934 if (bio->bi_vcnt != 1 ||
935 bio->bi_idx != 0)
936 goto bad_map;
937 /* This is a one page bio that upper layers
938 * refuse to split for us, so we need to split it.
940 bp = bio_split(bio,
941 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
943 /* Each of these 'make_request' calls will call 'wait_barrier'.
944 * If the first succeeds but the second blocks due to the resync
945 * thread raising the barrier, we will deadlock because the
946 * IO to the underlying device will be queued in generic_make_request
947 * and will never complete, so will never reduce nr_pending.
948 * So increment nr_waiting here so no new raise_barriers will
949 * succeed, and so the second wait_barrier cannot block.
951 spin_lock_irq(&conf->resync_lock);
952 conf->nr_waiting++;
953 spin_unlock_irq(&conf->resync_lock);
955 make_request(mddev, &bp->bio1);
956 make_request(mddev, &bp->bio2);
958 spin_lock_irq(&conf->resync_lock);
959 conf->nr_waiting--;
960 wake_up(&conf->wait_barrier);
961 spin_unlock_irq(&conf->resync_lock);
963 bio_pair_release(bp);
964 return;
965 bad_map:
966 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
967 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
968 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
970 bio_io_error(bio);
971 return;
974 md_write_start(mddev, bio);
977 * Register the new request and wait if the reconstruction
978 * thread has put up a bar for new requests.
979 * Continue immediately if no resync is active currently.
981 wait_barrier(conf);
983 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
985 r10_bio->master_bio = bio;
986 r10_bio->sectors = bio->bi_size >> 9;
988 r10_bio->mddev = mddev;
989 r10_bio->sector = bio->bi_sector;
990 r10_bio->state = 0;
992 /* We might need to issue multiple reads to different
993 * devices if there are bad blocks around, so we keep
994 * track of the number of reads in bio->bi_phys_segments.
995 * If this is 0, there is only one r10_bio and no locking
996 * will be needed when the request completes. If it is
997 * non-zero, then it is the number of not-completed requests.
999 bio->bi_phys_segments = 0;
1000 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1002 if (rw == READ) {
1004 * read balancing logic:
1006 struct md_rdev *rdev;
1007 int slot;
1009 read_again:
1010 rdev = read_balance(conf, r10_bio, &max_sectors);
1011 if (!rdev) {
1012 raid_end_bio_io(r10_bio);
1013 return;
1015 slot = r10_bio->read_slot;
1017 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1018 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
1019 max_sectors);
1021 r10_bio->devs[slot].bio = read_bio;
1022 r10_bio->devs[slot].rdev = rdev;
1024 read_bio->bi_sector = r10_bio->devs[slot].addr +
1025 rdev->data_offset;
1026 read_bio->bi_bdev = rdev->bdev;
1027 read_bio->bi_end_io = raid10_end_read_request;
1028 read_bio->bi_rw = READ | do_sync;
1029 read_bio->bi_private = r10_bio;
1031 if (max_sectors < r10_bio->sectors) {
1032 /* Could not read all from this device, so we will
1033 * need another r10_bio.
1035 sectors_handled = (r10_bio->sectors + max_sectors
1036 - bio->bi_sector);
1037 r10_bio->sectors = max_sectors;
1038 spin_lock_irq(&conf->device_lock);
1039 if (bio->bi_phys_segments == 0)
1040 bio->bi_phys_segments = 2;
1041 else
1042 bio->bi_phys_segments++;
1043 spin_unlock(&conf->device_lock);
1044 /* Cannot call generic_make_request directly
1045 * as that will be queued in __generic_make_request
1046 * and subsequent mempool_alloc might block
1047 * waiting for it. so hand bio over to raid10d.
1049 reschedule_retry(r10_bio);
1051 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1053 r10_bio->master_bio = bio;
1054 r10_bio->sectors = ((bio->bi_size >> 9)
1055 - sectors_handled);
1056 r10_bio->state = 0;
1057 r10_bio->mddev = mddev;
1058 r10_bio->sector = bio->bi_sector + sectors_handled;
1059 goto read_again;
1060 } else
1061 generic_make_request(read_bio);
1062 return;
1066 * WRITE:
1068 if (conf->pending_count >= max_queued_requests) {
1069 md_wakeup_thread(mddev->thread);
1070 wait_event(conf->wait_barrier,
1071 conf->pending_count < max_queued_requests);
1073 /* first select target devices under rcu_lock and
1074 * inc refcount on their rdev. Record them by setting
1075 * bios[x] to bio
1076 * If there are known/acknowledged bad blocks on any device
1077 * on which we have seen a write error, we want to avoid
1078 * writing to those blocks. This potentially requires several
1079 * writes to write around the bad blocks. Each set of writes
1080 * gets its own r10_bio with a set of bios attached. The number
1081 * of r10_bios is recored in bio->bi_phys_segments just as with
1082 * the read case.
1084 plugged = mddev_check_plugged(mddev);
1086 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1087 raid10_find_phys(conf, r10_bio);
1088 retry_write:
1089 blocked_rdev = NULL;
1090 rcu_read_lock();
1091 max_sectors = r10_bio->sectors;
1093 for (i = 0; i < conf->copies; i++) {
1094 int d = r10_bio->devs[i].devnum;
1095 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1096 struct md_rdev *rrdev = rcu_dereference(
1097 conf->mirrors[d].replacement);
1098 if (rdev == rrdev)
1099 rrdev = NULL;
1100 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1101 atomic_inc(&rdev->nr_pending);
1102 blocked_rdev = rdev;
1103 break;
1105 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1106 atomic_inc(&rrdev->nr_pending);
1107 blocked_rdev = rrdev;
1108 break;
1110 if (rrdev && test_bit(Faulty, &rrdev->flags))
1111 rrdev = NULL;
1113 r10_bio->devs[i].bio = NULL;
1114 r10_bio->devs[i].repl_bio = NULL;
1115 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1116 set_bit(R10BIO_Degraded, &r10_bio->state);
1117 continue;
1119 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1120 sector_t first_bad;
1121 sector_t dev_sector = r10_bio->devs[i].addr;
1122 int bad_sectors;
1123 int is_bad;
1125 is_bad = is_badblock(rdev, dev_sector,
1126 max_sectors,
1127 &first_bad, &bad_sectors);
1128 if (is_bad < 0) {
1129 /* Mustn't write here until the bad block
1130 * is acknowledged
1132 atomic_inc(&rdev->nr_pending);
1133 set_bit(BlockedBadBlocks, &rdev->flags);
1134 blocked_rdev = rdev;
1135 break;
1137 if (is_bad && first_bad <= dev_sector) {
1138 /* Cannot write here at all */
1139 bad_sectors -= (dev_sector - first_bad);
1140 if (bad_sectors < max_sectors)
1141 /* Mustn't write more than bad_sectors
1142 * to other devices yet
1144 max_sectors = bad_sectors;
1145 /* We don't set R10BIO_Degraded as that
1146 * only applies if the disk is missing,
1147 * so it might be re-added, and we want to
1148 * know to recover this chunk.
1149 * In this case the device is here, and the
1150 * fact that this chunk is not in-sync is
1151 * recorded in the bad block log.
1153 continue;
1155 if (is_bad) {
1156 int good_sectors = first_bad - dev_sector;
1157 if (good_sectors < max_sectors)
1158 max_sectors = good_sectors;
1161 r10_bio->devs[i].bio = bio;
1162 atomic_inc(&rdev->nr_pending);
1163 if (rrdev) {
1164 r10_bio->devs[i].repl_bio = bio;
1165 atomic_inc(&rrdev->nr_pending);
1168 rcu_read_unlock();
1170 if (unlikely(blocked_rdev)) {
1171 /* Have to wait for this device to get unblocked, then retry */
1172 int j;
1173 int d;
1175 for (j = 0; j < i; j++) {
1176 if (r10_bio->devs[j].bio) {
1177 d = r10_bio->devs[j].devnum;
1178 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1180 if (r10_bio->devs[j].repl_bio) {
1181 struct md_rdev *rdev;
1182 d = r10_bio->devs[j].devnum;
1183 rdev = conf->mirrors[d].replacement;
1184 if (!rdev) {
1185 /* Race with remove_disk */
1186 smp_mb();
1187 rdev = conf->mirrors[d].rdev;
1189 rdev_dec_pending(rdev, mddev);
1192 allow_barrier(conf);
1193 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1194 wait_barrier(conf);
1195 goto retry_write;
1198 if (max_sectors < r10_bio->sectors) {
1199 /* We are splitting this into multiple parts, so
1200 * we need to prepare for allocating another r10_bio.
1202 r10_bio->sectors = max_sectors;
1203 spin_lock_irq(&conf->device_lock);
1204 if (bio->bi_phys_segments == 0)
1205 bio->bi_phys_segments = 2;
1206 else
1207 bio->bi_phys_segments++;
1208 spin_unlock_irq(&conf->device_lock);
1210 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1212 atomic_set(&r10_bio->remaining, 1);
1213 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1215 for (i = 0; i < conf->copies; i++) {
1216 struct bio *mbio;
1217 int d = r10_bio->devs[i].devnum;
1218 if (!r10_bio->devs[i].bio)
1219 continue;
1221 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1222 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1223 max_sectors);
1224 r10_bio->devs[i].bio = mbio;
1226 mbio->bi_sector = (r10_bio->devs[i].addr+
1227 conf->mirrors[d].rdev->data_offset);
1228 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1229 mbio->bi_end_io = raid10_end_write_request;
1230 mbio->bi_rw = WRITE | do_sync | do_fua;
1231 mbio->bi_private = r10_bio;
1233 atomic_inc(&r10_bio->remaining);
1234 spin_lock_irqsave(&conf->device_lock, flags);
1235 bio_list_add(&conf->pending_bio_list, mbio);
1236 conf->pending_count++;
1237 spin_unlock_irqrestore(&conf->device_lock, flags);
1239 if (!r10_bio->devs[i].repl_bio)
1240 continue;
1242 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1243 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1244 max_sectors);
1245 r10_bio->devs[i].repl_bio = mbio;
1247 /* We are actively writing to the original device
1248 * so it cannot disappear, so the replacement cannot
1249 * become NULL here
1251 mbio->bi_sector = (r10_bio->devs[i].addr+
1252 conf->mirrors[d].replacement->data_offset);
1253 mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
1254 mbio->bi_end_io = raid10_end_write_request;
1255 mbio->bi_rw = WRITE | do_sync | do_fua;
1256 mbio->bi_private = r10_bio;
1258 atomic_inc(&r10_bio->remaining);
1259 spin_lock_irqsave(&conf->device_lock, flags);
1260 bio_list_add(&conf->pending_bio_list, mbio);
1261 conf->pending_count++;
1262 spin_unlock_irqrestore(&conf->device_lock, flags);
1265 /* Don't remove the bias on 'remaining' (one_write_done) until
1266 * after checking if we need to go around again.
1269 if (sectors_handled < (bio->bi_size >> 9)) {
1270 one_write_done(r10_bio);
1271 /* We need another r10_bio. It has already been counted
1272 * in bio->bi_phys_segments.
1274 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1276 r10_bio->master_bio = bio;
1277 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1279 r10_bio->mddev = mddev;
1280 r10_bio->sector = bio->bi_sector + sectors_handled;
1281 r10_bio->state = 0;
1282 goto retry_write;
1284 one_write_done(r10_bio);
1286 /* In case raid10d snuck in to freeze_array */
1287 wake_up(&conf->wait_barrier);
1289 if (do_sync || !mddev->bitmap || !plugged)
1290 md_wakeup_thread(mddev->thread);
1293 static void status(struct seq_file *seq, struct mddev *mddev)
1295 struct r10conf *conf = mddev->private;
1296 int i;
1298 if (conf->near_copies < conf->raid_disks)
1299 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1300 if (conf->near_copies > 1)
1301 seq_printf(seq, " %d near-copies", conf->near_copies);
1302 if (conf->far_copies > 1) {
1303 if (conf->far_offset)
1304 seq_printf(seq, " %d offset-copies", conf->far_copies);
1305 else
1306 seq_printf(seq, " %d far-copies", conf->far_copies);
1308 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1309 conf->raid_disks - mddev->degraded);
1310 for (i = 0; i < conf->raid_disks; i++)
1311 seq_printf(seq, "%s",
1312 conf->mirrors[i].rdev &&
1313 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1314 seq_printf(seq, "]");
1317 /* check if there are enough drives for
1318 * every block to appear on atleast one.
1319 * Don't consider the device numbered 'ignore'
1320 * as we might be about to remove it.
1322 static int enough(struct r10conf *conf, int ignore)
1324 int first = 0;
1326 do {
1327 int n = conf->copies;
1328 int cnt = 0;
1329 while (n--) {
1330 if (conf->mirrors[first].rdev &&
1331 first != ignore)
1332 cnt++;
1333 first = (first+1) % conf->raid_disks;
1335 if (cnt == 0)
1336 return 0;
1337 } while (first != 0);
1338 return 1;
1341 static void error(struct mddev *mddev, struct md_rdev *rdev)
1343 char b[BDEVNAME_SIZE];
1344 struct r10conf *conf = mddev->private;
1347 * If it is not operational, then we have already marked it as dead
1348 * else if it is the last working disks, ignore the error, let the
1349 * next level up know.
1350 * else mark the drive as failed
1352 if (test_bit(In_sync, &rdev->flags)
1353 && !enough(conf, rdev->raid_disk))
1355 * Don't fail the drive, just return an IO error.
1357 return;
1358 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1359 unsigned long flags;
1360 spin_lock_irqsave(&conf->device_lock, flags);
1361 mddev->degraded++;
1362 spin_unlock_irqrestore(&conf->device_lock, flags);
1364 * if recovery is running, make sure it aborts.
1366 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1368 set_bit(Blocked, &rdev->flags);
1369 set_bit(Faulty, &rdev->flags);
1370 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1371 printk(KERN_ALERT
1372 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1373 "md/raid10:%s: Operation continuing on %d devices.\n",
1374 mdname(mddev), bdevname(rdev->bdev, b),
1375 mdname(mddev), conf->raid_disks - mddev->degraded);
1378 static void print_conf(struct r10conf *conf)
1380 int i;
1381 struct mirror_info *tmp;
1383 printk(KERN_DEBUG "RAID10 conf printout:\n");
1384 if (!conf) {
1385 printk(KERN_DEBUG "(!conf)\n");
1386 return;
1388 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1389 conf->raid_disks);
1391 for (i = 0; i < conf->raid_disks; i++) {
1392 char b[BDEVNAME_SIZE];
1393 tmp = conf->mirrors + i;
1394 if (tmp->rdev)
1395 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1396 i, !test_bit(In_sync, &tmp->rdev->flags),
1397 !test_bit(Faulty, &tmp->rdev->flags),
1398 bdevname(tmp->rdev->bdev,b));
1402 static void close_sync(struct r10conf *conf)
1404 wait_barrier(conf);
1405 allow_barrier(conf);
1407 mempool_destroy(conf->r10buf_pool);
1408 conf->r10buf_pool = NULL;
1411 static int raid10_spare_active(struct mddev *mddev)
1413 int i;
1414 struct r10conf *conf = mddev->private;
1415 struct mirror_info *tmp;
1416 int count = 0;
1417 unsigned long flags;
1420 * Find all non-in_sync disks within the RAID10 configuration
1421 * and mark them in_sync
1423 for (i = 0; i < conf->raid_disks; i++) {
1424 tmp = conf->mirrors + i;
1425 if (tmp->replacement
1426 && tmp->replacement->recovery_offset == MaxSector
1427 && !test_bit(Faulty, &tmp->replacement->flags)
1428 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1429 /* Replacement has just become active */
1430 if (!tmp->rdev
1431 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1432 count++;
1433 if (tmp->rdev) {
1434 /* Replaced device not technically faulty,
1435 * but we need to be sure it gets removed
1436 * and never re-added.
1438 set_bit(Faulty, &tmp->rdev->flags);
1439 sysfs_notify_dirent_safe(
1440 tmp->rdev->sysfs_state);
1442 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1443 } else if (tmp->rdev
1444 && !test_bit(Faulty, &tmp->rdev->flags)
1445 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1446 count++;
1447 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1450 spin_lock_irqsave(&conf->device_lock, flags);
1451 mddev->degraded -= count;
1452 spin_unlock_irqrestore(&conf->device_lock, flags);
1454 print_conf(conf);
1455 return count;
1459 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1461 struct r10conf *conf = mddev->private;
1462 int err = -EEXIST;
1463 int mirror;
1464 int first = 0;
1465 int last = conf->raid_disks - 1;
1467 if (mddev->recovery_cp < MaxSector)
1468 /* only hot-add to in-sync arrays, as recovery is
1469 * very different from resync
1471 return -EBUSY;
1472 if (!enough(conf, -1))
1473 return -EINVAL;
1475 if (rdev->raid_disk >= 0)
1476 first = last = rdev->raid_disk;
1478 if (rdev->saved_raid_disk >= first &&
1479 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1480 mirror = rdev->saved_raid_disk;
1481 else
1482 mirror = first;
1483 for ( ; mirror <= last ; mirror++) {
1484 struct mirror_info *p = &conf->mirrors[mirror];
1485 if (p->recovery_disabled == mddev->recovery_disabled)
1486 continue;
1487 if (p->rdev) {
1488 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1489 p->replacement != NULL)
1490 continue;
1491 clear_bit(In_sync, &rdev->flags);
1492 set_bit(Replacement, &rdev->flags);
1493 rdev->raid_disk = mirror;
1494 err = 0;
1495 disk_stack_limits(mddev->gendisk, rdev->bdev,
1496 rdev->data_offset << 9);
1497 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1498 blk_queue_max_segments(mddev->queue, 1);
1499 blk_queue_segment_boundary(mddev->queue,
1500 PAGE_CACHE_SIZE - 1);
1502 conf->fullsync = 1;
1503 rcu_assign_pointer(p->replacement, rdev);
1504 break;
1507 disk_stack_limits(mddev->gendisk, rdev->bdev,
1508 rdev->data_offset << 9);
1509 /* as we don't honour merge_bvec_fn, we must
1510 * never risk violating it, so limit
1511 * ->max_segments to one lying with a single
1512 * page, as a one page request is never in
1513 * violation.
1515 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1516 blk_queue_max_segments(mddev->queue, 1);
1517 blk_queue_segment_boundary(mddev->queue,
1518 PAGE_CACHE_SIZE - 1);
1521 p->head_position = 0;
1522 p->recovery_disabled = mddev->recovery_disabled - 1;
1523 rdev->raid_disk = mirror;
1524 err = 0;
1525 if (rdev->saved_raid_disk != mirror)
1526 conf->fullsync = 1;
1527 rcu_assign_pointer(p->rdev, rdev);
1528 break;
1531 md_integrity_add_rdev(rdev, mddev);
1532 print_conf(conf);
1533 return err;
1536 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1538 struct r10conf *conf = mddev->private;
1539 int err = 0;
1540 int number = rdev->raid_disk;
1541 struct md_rdev **rdevp;
1542 struct mirror_info *p = conf->mirrors + number;
1544 print_conf(conf);
1545 if (rdev == p->rdev)
1546 rdevp = &p->rdev;
1547 else if (rdev == p->replacement)
1548 rdevp = &p->replacement;
1549 else
1550 return 0;
1552 if (test_bit(In_sync, &rdev->flags) ||
1553 atomic_read(&rdev->nr_pending)) {
1554 err = -EBUSY;
1555 goto abort;
1557 /* Only remove faulty devices if recovery
1558 * is not possible.
1560 if (!test_bit(Faulty, &rdev->flags) &&
1561 mddev->recovery_disabled != p->recovery_disabled &&
1562 (!p->replacement || p->replacement == rdev) &&
1563 enough(conf, -1)) {
1564 err = -EBUSY;
1565 goto abort;
1567 *rdevp = NULL;
1568 synchronize_rcu();
1569 if (atomic_read(&rdev->nr_pending)) {
1570 /* lost the race, try later */
1571 err = -EBUSY;
1572 *rdevp = rdev;
1573 goto abort;
1574 } else if (p->replacement) {
1575 /* We must have just cleared 'rdev' */
1576 p->rdev = p->replacement;
1577 clear_bit(Replacement, &p->replacement->flags);
1578 smp_mb(); /* Make sure other CPUs may see both as identical
1579 * but will never see neither -- if they are careful.
1581 p->replacement = NULL;
1582 clear_bit(WantReplacement, &rdev->flags);
1583 } else
1584 /* We might have just remove the Replacement as faulty
1585 * Clear the flag just in case
1587 clear_bit(WantReplacement, &rdev->flags);
1589 err = md_integrity_register(mddev);
1591 abort:
1593 print_conf(conf);
1594 return err;
1598 static void end_sync_read(struct bio *bio, int error)
1600 struct r10bio *r10_bio = bio->bi_private;
1601 struct r10conf *conf = r10_bio->mddev->private;
1602 int d;
1604 d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1606 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1607 set_bit(R10BIO_Uptodate, &r10_bio->state);
1608 else
1609 /* The write handler will notice the lack of
1610 * R10BIO_Uptodate and record any errors etc
1612 atomic_add(r10_bio->sectors,
1613 &conf->mirrors[d].rdev->corrected_errors);
1615 /* for reconstruct, we always reschedule after a read.
1616 * for resync, only after all reads
1618 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1619 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1620 atomic_dec_and_test(&r10_bio->remaining)) {
1621 /* we have read all the blocks,
1622 * do the comparison in process context in raid10d
1624 reschedule_retry(r10_bio);
1628 static void end_sync_request(struct r10bio *r10_bio)
1630 struct mddev *mddev = r10_bio->mddev;
1632 while (atomic_dec_and_test(&r10_bio->remaining)) {
1633 if (r10_bio->master_bio == NULL) {
1634 /* the primary of several recovery bios */
1635 sector_t s = r10_bio->sectors;
1636 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1637 test_bit(R10BIO_WriteError, &r10_bio->state))
1638 reschedule_retry(r10_bio);
1639 else
1640 put_buf(r10_bio);
1641 md_done_sync(mddev, s, 1);
1642 break;
1643 } else {
1644 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1645 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1646 test_bit(R10BIO_WriteError, &r10_bio->state))
1647 reschedule_retry(r10_bio);
1648 else
1649 put_buf(r10_bio);
1650 r10_bio = r10_bio2;
1655 static void end_sync_write(struct bio *bio, int error)
1657 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1658 struct r10bio *r10_bio = bio->bi_private;
1659 struct mddev *mddev = r10_bio->mddev;
1660 struct r10conf *conf = mddev->private;
1661 int d;
1662 sector_t first_bad;
1663 int bad_sectors;
1664 int slot;
1665 int repl;
1666 struct md_rdev *rdev = NULL;
1668 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1669 if (repl)
1670 rdev = conf->mirrors[d].replacement;
1671 if (!rdev) {
1672 smp_mb();
1673 rdev = conf->mirrors[d].rdev;
1676 if (!uptodate) {
1677 if (repl)
1678 md_error(mddev, rdev);
1679 else {
1680 set_bit(WriteErrorSeen, &rdev->flags);
1681 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1682 set_bit(MD_RECOVERY_NEEDED,
1683 &rdev->mddev->recovery);
1684 set_bit(R10BIO_WriteError, &r10_bio->state);
1686 } else if (is_badblock(rdev,
1687 r10_bio->devs[slot].addr,
1688 r10_bio->sectors,
1689 &first_bad, &bad_sectors))
1690 set_bit(R10BIO_MadeGood, &r10_bio->state);
1692 rdev_dec_pending(rdev, mddev);
1694 end_sync_request(r10_bio);
1698 * Note: sync and recover and handled very differently for raid10
1699 * This code is for resync.
1700 * For resync, we read through virtual addresses and read all blocks.
1701 * If there is any error, we schedule a write. The lowest numbered
1702 * drive is authoritative.
1703 * However requests come for physical address, so we need to map.
1704 * For every physical address there are raid_disks/copies virtual addresses,
1705 * which is always are least one, but is not necessarly an integer.
1706 * This means that a physical address can span multiple chunks, so we may
1707 * have to submit multiple io requests for a single sync request.
1710 * We check if all blocks are in-sync and only write to blocks that
1711 * aren't in sync
1713 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1715 struct r10conf *conf = mddev->private;
1716 int i, first;
1717 struct bio *tbio, *fbio;
1719 atomic_set(&r10_bio->remaining, 1);
1721 /* find the first device with a block */
1722 for (i=0; i<conf->copies; i++)
1723 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1724 break;
1726 if (i == conf->copies)
1727 goto done;
1729 first = i;
1730 fbio = r10_bio->devs[i].bio;
1732 /* now find blocks with errors */
1733 for (i=0 ; i < conf->copies ; i++) {
1734 int j, d;
1735 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1737 tbio = r10_bio->devs[i].bio;
1739 if (tbio->bi_end_io != end_sync_read)
1740 continue;
1741 if (i == first)
1742 continue;
1743 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1744 /* We know that the bi_io_vec layout is the same for
1745 * both 'first' and 'i', so we just compare them.
1746 * All vec entries are PAGE_SIZE;
1748 for (j = 0; j < vcnt; j++)
1749 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1750 page_address(tbio->bi_io_vec[j].bv_page),
1751 PAGE_SIZE))
1752 break;
1753 if (j == vcnt)
1754 continue;
1755 mddev->resync_mismatches += r10_bio->sectors;
1756 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1757 /* Don't fix anything. */
1758 continue;
1760 /* Ok, we need to write this bio, either to correct an
1761 * inconsistency or to correct an unreadable block.
1762 * First we need to fixup bv_offset, bv_len and
1763 * bi_vecs, as the read request might have corrupted these
1765 tbio->bi_vcnt = vcnt;
1766 tbio->bi_size = r10_bio->sectors << 9;
1767 tbio->bi_idx = 0;
1768 tbio->bi_phys_segments = 0;
1769 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1770 tbio->bi_flags |= 1 << BIO_UPTODATE;
1771 tbio->bi_next = NULL;
1772 tbio->bi_rw = WRITE;
1773 tbio->bi_private = r10_bio;
1774 tbio->bi_sector = r10_bio->devs[i].addr;
1776 for (j=0; j < vcnt ; j++) {
1777 tbio->bi_io_vec[j].bv_offset = 0;
1778 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1780 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1781 page_address(fbio->bi_io_vec[j].bv_page),
1782 PAGE_SIZE);
1784 tbio->bi_end_io = end_sync_write;
1786 d = r10_bio->devs[i].devnum;
1787 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1788 atomic_inc(&r10_bio->remaining);
1789 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1791 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1792 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1793 generic_make_request(tbio);
1796 /* Now write out to any replacement devices
1797 * that are active
1799 for (i = 0; i < conf->copies; i++) {
1800 int j, d;
1801 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1803 tbio = r10_bio->devs[i].repl_bio;
1804 if (!tbio || !tbio->bi_end_io)
1805 continue;
1806 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
1807 && r10_bio->devs[i].bio != fbio)
1808 for (j = 0; j < vcnt; j++)
1809 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1810 page_address(fbio->bi_io_vec[j].bv_page),
1811 PAGE_SIZE);
1812 d = r10_bio->devs[i].devnum;
1813 atomic_inc(&r10_bio->remaining);
1814 md_sync_acct(conf->mirrors[d].replacement->bdev,
1815 tbio->bi_size >> 9);
1816 generic_make_request(tbio);
1819 done:
1820 if (atomic_dec_and_test(&r10_bio->remaining)) {
1821 md_done_sync(mddev, r10_bio->sectors, 1);
1822 put_buf(r10_bio);
1827 * Now for the recovery code.
1828 * Recovery happens across physical sectors.
1829 * We recover all non-is_sync drives by finding the virtual address of
1830 * each, and then choose a working drive that also has that virt address.
1831 * There is a separate r10_bio for each non-in_sync drive.
1832 * Only the first two slots are in use. The first for reading,
1833 * The second for writing.
1836 static void fix_recovery_read_error(struct r10bio *r10_bio)
1838 /* We got a read error during recovery.
1839 * We repeat the read in smaller page-sized sections.
1840 * If a read succeeds, write it to the new device or record
1841 * a bad block if we cannot.
1842 * If a read fails, record a bad block on both old and
1843 * new devices.
1845 struct mddev *mddev = r10_bio->mddev;
1846 struct r10conf *conf = mddev->private;
1847 struct bio *bio = r10_bio->devs[0].bio;
1848 sector_t sect = 0;
1849 int sectors = r10_bio->sectors;
1850 int idx = 0;
1851 int dr = r10_bio->devs[0].devnum;
1852 int dw = r10_bio->devs[1].devnum;
1854 while (sectors) {
1855 int s = sectors;
1856 struct md_rdev *rdev;
1857 sector_t addr;
1858 int ok;
1860 if (s > (PAGE_SIZE>>9))
1861 s = PAGE_SIZE >> 9;
1863 rdev = conf->mirrors[dr].rdev;
1864 addr = r10_bio->devs[0].addr + sect,
1865 ok = sync_page_io(rdev,
1866 addr,
1867 s << 9,
1868 bio->bi_io_vec[idx].bv_page,
1869 READ, false);
1870 if (ok) {
1871 rdev = conf->mirrors[dw].rdev;
1872 addr = r10_bio->devs[1].addr + sect;
1873 ok = sync_page_io(rdev,
1874 addr,
1875 s << 9,
1876 bio->bi_io_vec[idx].bv_page,
1877 WRITE, false);
1878 if (!ok) {
1879 set_bit(WriteErrorSeen, &rdev->flags);
1880 if (!test_and_set_bit(WantReplacement,
1881 &rdev->flags))
1882 set_bit(MD_RECOVERY_NEEDED,
1883 &rdev->mddev->recovery);
1886 if (!ok) {
1887 /* We don't worry if we cannot set a bad block -
1888 * it really is bad so there is no loss in not
1889 * recording it yet
1891 rdev_set_badblocks(rdev, addr, s, 0);
1893 if (rdev != conf->mirrors[dw].rdev) {
1894 /* need bad block on destination too */
1895 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
1896 addr = r10_bio->devs[1].addr + sect;
1897 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1898 if (!ok) {
1899 /* just abort the recovery */
1900 printk(KERN_NOTICE
1901 "md/raid10:%s: recovery aborted"
1902 " due to read error\n",
1903 mdname(mddev));
1905 conf->mirrors[dw].recovery_disabled
1906 = mddev->recovery_disabled;
1907 set_bit(MD_RECOVERY_INTR,
1908 &mddev->recovery);
1909 break;
1914 sectors -= s;
1915 sect += s;
1916 idx++;
1920 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1922 struct r10conf *conf = mddev->private;
1923 int d;
1924 struct bio *wbio, *wbio2;
1926 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1927 fix_recovery_read_error(r10_bio);
1928 end_sync_request(r10_bio);
1929 return;
1933 * share the pages with the first bio
1934 * and submit the write request
1936 d = r10_bio->devs[1].devnum;
1937 wbio = r10_bio->devs[1].bio;
1938 wbio2 = r10_bio->devs[1].repl_bio;
1939 if (wbio->bi_end_io) {
1940 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1941 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1942 generic_make_request(wbio);
1944 if (wbio2 && wbio2->bi_end_io) {
1945 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
1946 md_sync_acct(conf->mirrors[d].replacement->bdev,
1947 wbio2->bi_size >> 9);
1948 generic_make_request(wbio2);
1954 * Used by fix_read_error() to decay the per rdev read_errors.
1955 * We halve the read error count for every hour that has elapsed
1956 * since the last recorded read error.
1959 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
1961 struct timespec cur_time_mon;
1962 unsigned long hours_since_last;
1963 unsigned int read_errors = atomic_read(&rdev->read_errors);
1965 ktime_get_ts(&cur_time_mon);
1967 if (rdev->last_read_error.tv_sec == 0 &&
1968 rdev->last_read_error.tv_nsec == 0) {
1969 /* first time we've seen a read error */
1970 rdev->last_read_error = cur_time_mon;
1971 return;
1974 hours_since_last = (cur_time_mon.tv_sec -
1975 rdev->last_read_error.tv_sec) / 3600;
1977 rdev->last_read_error = cur_time_mon;
1980 * if hours_since_last is > the number of bits in read_errors
1981 * just set read errors to 0. We do this to avoid
1982 * overflowing the shift of read_errors by hours_since_last.
1984 if (hours_since_last >= 8 * sizeof(read_errors))
1985 atomic_set(&rdev->read_errors, 0);
1986 else
1987 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1990 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
1991 int sectors, struct page *page, int rw)
1993 sector_t first_bad;
1994 int bad_sectors;
1996 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1997 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1998 return -1;
1999 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
2000 /* success */
2001 return 1;
2002 if (rw == WRITE) {
2003 set_bit(WriteErrorSeen, &rdev->flags);
2004 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2005 set_bit(MD_RECOVERY_NEEDED,
2006 &rdev->mddev->recovery);
2008 /* need to record an error - either for the block or the device */
2009 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2010 md_error(rdev->mddev, rdev);
2011 return 0;
2015 * This is a kernel thread which:
2017 * 1. Retries failed read operations on working mirrors.
2018 * 2. Updates the raid superblock when problems encounter.
2019 * 3. Performs writes following reads for array synchronising.
2022 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2024 int sect = 0; /* Offset from r10_bio->sector */
2025 int sectors = r10_bio->sectors;
2026 struct md_rdev*rdev;
2027 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2028 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2030 /* still own a reference to this rdev, so it cannot
2031 * have been cleared recently.
2033 rdev = conf->mirrors[d].rdev;
2035 if (test_bit(Faulty, &rdev->flags))
2036 /* drive has already been failed, just ignore any
2037 more fix_read_error() attempts */
2038 return;
2040 check_decay_read_errors(mddev, rdev);
2041 atomic_inc(&rdev->read_errors);
2042 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2043 char b[BDEVNAME_SIZE];
2044 bdevname(rdev->bdev, b);
2046 printk(KERN_NOTICE
2047 "md/raid10:%s: %s: Raid device exceeded "
2048 "read_error threshold [cur %d:max %d]\n",
2049 mdname(mddev), b,
2050 atomic_read(&rdev->read_errors), max_read_errors);
2051 printk(KERN_NOTICE
2052 "md/raid10:%s: %s: Failing raid device\n",
2053 mdname(mddev), b);
2054 md_error(mddev, conf->mirrors[d].rdev);
2055 return;
2058 while(sectors) {
2059 int s = sectors;
2060 int sl = r10_bio->read_slot;
2061 int success = 0;
2062 int start;
2064 if (s > (PAGE_SIZE>>9))
2065 s = PAGE_SIZE >> 9;
2067 rcu_read_lock();
2068 do {
2069 sector_t first_bad;
2070 int bad_sectors;
2072 d = r10_bio->devs[sl].devnum;
2073 rdev = rcu_dereference(conf->mirrors[d].rdev);
2074 if (rdev &&
2075 test_bit(In_sync, &rdev->flags) &&
2076 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2077 &first_bad, &bad_sectors) == 0) {
2078 atomic_inc(&rdev->nr_pending);
2079 rcu_read_unlock();
2080 success = sync_page_io(rdev,
2081 r10_bio->devs[sl].addr +
2082 sect,
2083 s<<9,
2084 conf->tmppage, READ, false);
2085 rdev_dec_pending(rdev, mddev);
2086 rcu_read_lock();
2087 if (success)
2088 break;
2090 sl++;
2091 if (sl == conf->copies)
2092 sl = 0;
2093 } while (!success && sl != r10_bio->read_slot);
2094 rcu_read_unlock();
2096 if (!success) {
2097 /* Cannot read from anywhere, just mark the block
2098 * as bad on the first device to discourage future
2099 * reads.
2101 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2102 rdev = conf->mirrors[dn].rdev;
2104 if (!rdev_set_badblocks(
2105 rdev,
2106 r10_bio->devs[r10_bio->read_slot].addr
2107 + sect,
2108 s, 0))
2109 md_error(mddev, rdev);
2110 break;
2113 start = sl;
2114 /* write it back and re-read */
2115 rcu_read_lock();
2116 while (sl != r10_bio->read_slot) {
2117 char b[BDEVNAME_SIZE];
2119 if (sl==0)
2120 sl = conf->copies;
2121 sl--;
2122 d = r10_bio->devs[sl].devnum;
2123 rdev = rcu_dereference(conf->mirrors[d].rdev);
2124 if (!rdev ||
2125 !test_bit(In_sync, &rdev->flags))
2126 continue;
2128 atomic_inc(&rdev->nr_pending);
2129 rcu_read_unlock();
2130 if (r10_sync_page_io(rdev,
2131 r10_bio->devs[sl].addr +
2132 sect,
2133 s<<9, conf->tmppage, WRITE)
2134 == 0) {
2135 /* Well, this device is dead */
2136 printk(KERN_NOTICE
2137 "md/raid10:%s: read correction "
2138 "write failed"
2139 " (%d sectors at %llu on %s)\n",
2140 mdname(mddev), s,
2141 (unsigned long long)(
2142 sect + rdev->data_offset),
2143 bdevname(rdev->bdev, b));
2144 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2145 "drive\n",
2146 mdname(mddev),
2147 bdevname(rdev->bdev, b));
2149 rdev_dec_pending(rdev, mddev);
2150 rcu_read_lock();
2152 sl = start;
2153 while (sl != r10_bio->read_slot) {
2154 char b[BDEVNAME_SIZE];
2156 if (sl==0)
2157 sl = conf->copies;
2158 sl--;
2159 d = r10_bio->devs[sl].devnum;
2160 rdev = rcu_dereference(conf->mirrors[d].rdev);
2161 if (!rdev ||
2162 !test_bit(In_sync, &rdev->flags))
2163 continue;
2165 atomic_inc(&rdev->nr_pending);
2166 rcu_read_unlock();
2167 switch (r10_sync_page_io(rdev,
2168 r10_bio->devs[sl].addr +
2169 sect,
2170 s<<9, conf->tmppage,
2171 READ)) {
2172 case 0:
2173 /* Well, this device is dead */
2174 printk(KERN_NOTICE
2175 "md/raid10:%s: unable to read back "
2176 "corrected sectors"
2177 " (%d sectors at %llu on %s)\n",
2178 mdname(mddev), s,
2179 (unsigned long long)(
2180 sect + rdev->data_offset),
2181 bdevname(rdev->bdev, b));
2182 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
2183 "drive\n",
2184 mdname(mddev),
2185 bdevname(rdev->bdev, b));
2186 break;
2187 case 1:
2188 printk(KERN_INFO
2189 "md/raid10:%s: read error corrected"
2190 " (%d sectors at %llu on %s)\n",
2191 mdname(mddev), s,
2192 (unsigned long long)(
2193 sect + rdev->data_offset),
2194 bdevname(rdev->bdev, b));
2195 atomic_add(s, &rdev->corrected_errors);
2198 rdev_dec_pending(rdev, mddev);
2199 rcu_read_lock();
2201 rcu_read_unlock();
2203 sectors -= s;
2204 sect += s;
2208 static void bi_complete(struct bio *bio, int error)
2210 complete((struct completion *)bio->bi_private);
2213 static int submit_bio_wait(int rw, struct bio *bio)
2215 struct completion event;
2216 rw |= REQ_SYNC;
2218 init_completion(&event);
2219 bio->bi_private = &event;
2220 bio->bi_end_io = bi_complete;
2221 submit_bio(rw, bio);
2222 wait_for_completion(&event);
2224 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2227 static int narrow_write_error(struct r10bio *r10_bio, int i)
2229 struct bio *bio = r10_bio->master_bio;
2230 struct mddev *mddev = r10_bio->mddev;
2231 struct r10conf *conf = mddev->private;
2232 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2233 /* bio has the data to be written to slot 'i' where
2234 * we just recently had a write error.
2235 * We repeatedly clone the bio and trim down to one block,
2236 * then try the write. Where the write fails we record
2237 * a bad block.
2238 * It is conceivable that the bio doesn't exactly align with
2239 * blocks. We must handle this.
2241 * We currently own a reference to the rdev.
2244 int block_sectors;
2245 sector_t sector;
2246 int sectors;
2247 int sect_to_write = r10_bio->sectors;
2248 int ok = 1;
2250 if (rdev->badblocks.shift < 0)
2251 return 0;
2253 block_sectors = 1 << rdev->badblocks.shift;
2254 sector = r10_bio->sector;
2255 sectors = ((r10_bio->sector + block_sectors)
2256 & ~(sector_t)(block_sectors - 1))
2257 - sector;
2259 while (sect_to_write) {
2260 struct bio *wbio;
2261 if (sectors > sect_to_write)
2262 sectors = sect_to_write;
2263 /* Write at 'sector' for 'sectors' */
2264 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2265 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2266 wbio->bi_sector = (r10_bio->devs[i].addr+
2267 rdev->data_offset+
2268 (sector - r10_bio->sector));
2269 wbio->bi_bdev = rdev->bdev;
2270 if (submit_bio_wait(WRITE, wbio) == 0)
2271 /* Failure! */
2272 ok = rdev_set_badblocks(rdev, sector,
2273 sectors, 0)
2274 && ok;
2276 bio_put(wbio);
2277 sect_to_write -= sectors;
2278 sector += sectors;
2279 sectors = block_sectors;
2281 return ok;
2284 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2286 int slot = r10_bio->read_slot;
2287 struct bio *bio;
2288 struct r10conf *conf = mddev->private;
2289 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2290 char b[BDEVNAME_SIZE];
2291 unsigned long do_sync;
2292 int max_sectors;
2294 /* we got a read error. Maybe the drive is bad. Maybe just
2295 * the block and we can fix it.
2296 * We freeze all other IO, and try reading the block from
2297 * other devices. When we find one, we re-write
2298 * and check it that fixes the read error.
2299 * This is all done synchronously while the array is
2300 * frozen.
2302 if (mddev->ro == 0) {
2303 freeze_array(conf);
2304 fix_read_error(conf, mddev, r10_bio);
2305 unfreeze_array(conf);
2307 rdev_dec_pending(rdev, mddev);
2309 bio = r10_bio->devs[slot].bio;
2310 bdevname(bio->bi_bdev, b);
2311 r10_bio->devs[slot].bio =
2312 mddev->ro ? IO_BLOCKED : NULL;
2313 read_more:
2314 rdev = read_balance(conf, r10_bio, &max_sectors);
2315 if (rdev == NULL) {
2316 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2317 " read error for block %llu\n",
2318 mdname(mddev), b,
2319 (unsigned long long)r10_bio->sector);
2320 raid_end_bio_io(r10_bio);
2321 bio_put(bio);
2322 return;
2325 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
2326 if (bio)
2327 bio_put(bio);
2328 slot = r10_bio->read_slot;
2329 printk_ratelimited(
2330 KERN_ERR
2331 "md/raid10:%s: %s: redirecting"
2332 "sector %llu to another mirror\n",
2333 mdname(mddev),
2334 bdevname(rdev->bdev, b),
2335 (unsigned long long)r10_bio->sector);
2336 bio = bio_clone_mddev(r10_bio->master_bio,
2337 GFP_NOIO, mddev);
2338 md_trim_bio(bio,
2339 r10_bio->sector - bio->bi_sector,
2340 max_sectors);
2341 r10_bio->devs[slot].bio = bio;
2342 r10_bio->devs[slot].rdev = rdev;
2343 bio->bi_sector = r10_bio->devs[slot].addr
2344 + rdev->data_offset;
2345 bio->bi_bdev = rdev->bdev;
2346 bio->bi_rw = READ | do_sync;
2347 bio->bi_private = r10_bio;
2348 bio->bi_end_io = raid10_end_read_request;
2349 if (max_sectors < r10_bio->sectors) {
2350 /* Drat - have to split this up more */
2351 struct bio *mbio = r10_bio->master_bio;
2352 int sectors_handled =
2353 r10_bio->sector + max_sectors
2354 - mbio->bi_sector;
2355 r10_bio->sectors = max_sectors;
2356 spin_lock_irq(&conf->device_lock);
2357 if (mbio->bi_phys_segments == 0)
2358 mbio->bi_phys_segments = 2;
2359 else
2360 mbio->bi_phys_segments++;
2361 spin_unlock_irq(&conf->device_lock);
2362 generic_make_request(bio);
2363 bio = NULL;
2365 r10_bio = mempool_alloc(conf->r10bio_pool,
2366 GFP_NOIO);
2367 r10_bio->master_bio = mbio;
2368 r10_bio->sectors = (mbio->bi_size >> 9)
2369 - sectors_handled;
2370 r10_bio->state = 0;
2371 set_bit(R10BIO_ReadError,
2372 &r10_bio->state);
2373 r10_bio->mddev = mddev;
2374 r10_bio->sector = mbio->bi_sector
2375 + sectors_handled;
2377 goto read_more;
2378 } else
2379 generic_make_request(bio);
2382 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2384 /* Some sort of write request has finished and it
2385 * succeeded in writing where we thought there was a
2386 * bad block. So forget the bad block.
2387 * Or possibly if failed and we need to record
2388 * a bad block.
2390 int m;
2391 struct md_rdev *rdev;
2393 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2394 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2395 for (m = 0; m < conf->copies; m++) {
2396 int dev = r10_bio->devs[m].devnum;
2397 rdev = conf->mirrors[dev].rdev;
2398 if (r10_bio->devs[m].bio == NULL)
2399 continue;
2400 if (test_bit(BIO_UPTODATE,
2401 &r10_bio->devs[m].bio->bi_flags)) {
2402 rdev_clear_badblocks(
2403 rdev,
2404 r10_bio->devs[m].addr,
2405 r10_bio->sectors);
2406 } else {
2407 if (!rdev_set_badblocks(
2408 rdev,
2409 r10_bio->devs[m].addr,
2410 r10_bio->sectors, 0))
2411 md_error(conf->mddev, rdev);
2413 rdev = conf->mirrors[dev].replacement;
2414 if (r10_bio->devs[m].repl_bio == NULL)
2415 continue;
2416 if (test_bit(BIO_UPTODATE,
2417 &r10_bio->devs[m].repl_bio->bi_flags)) {
2418 rdev_clear_badblocks(
2419 rdev,
2420 r10_bio->devs[m].addr,
2421 r10_bio->sectors);
2422 } else {
2423 if (!rdev_set_badblocks(
2424 rdev,
2425 r10_bio->devs[m].addr,
2426 r10_bio->sectors, 0))
2427 md_error(conf->mddev, rdev);
2430 put_buf(r10_bio);
2431 } else {
2432 for (m = 0; m < conf->copies; m++) {
2433 int dev = r10_bio->devs[m].devnum;
2434 struct bio *bio = r10_bio->devs[m].bio;
2435 rdev = conf->mirrors[dev].rdev;
2436 if (bio == IO_MADE_GOOD) {
2437 rdev_clear_badblocks(
2438 rdev,
2439 r10_bio->devs[m].addr,
2440 r10_bio->sectors);
2441 rdev_dec_pending(rdev, conf->mddev);
2442 } else if (bio != NULL &&
2443 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2444 if (!narrow_write_error(r10_bio, m)) {
2445 md_error(conf->mddev, rdev);
2446 set_bit(R10BIO_Degraded,
2447 &r10_bio->state);
2449 rdev_dec_pending(rdev, conf->mddev);
2451 bio = r10_bio->devs[m].repl_bio;
2452 rdev = conf->mirrors[dev].replacement;
2453 if (rdev && bio == IO_MADE_GOOD) {
2454 rdev_clear_badblocks(
2455 rdev,
2456 r10_bio->devs[m].addr,
2457 r10_bio->sectors);
2458 rdev_dec_pending(rdev, conf->mddev);
2461 if (test_bit(R10BIO_WriteError,
2462 &r10_bio->state))
2463 close_write(r10_bio);
2464 raid_end_bio_io(r10_bio);
2468 static void raid10d(struct mddev *mddev)
2470 struct r10bio *r10_bio;
2471 unsigned long flags;
2472 struct r10conf *conf = mddev->private;
2473 struct list_head *head = &conf->retry_list;
2474 struct blk_plug plug;
2476 md_check_recovery(mddev);
2478 blk_start_plug(&plug);
2479 for (;;) {
2481 flush_pending_writes(conf);
2483 spin_lock_irqsave(&conf->device_lock, flags);
2484 if (list_empty(head)) {
2485 spin_unlock_irqrestore(&conf->device_lock, flags);
2486 break;
2488 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2489 list_del(head->prev);
2490 conf->nr_queued--;
2491 spin_unlock_irqrestore(&conf->device_lock, flags);
2493 mddev = r10_bio->mddev;
2494 conf = mddev->private;
2495 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2496 test_bit(R10BIO_WriteError, &r10_bio->state))
2497 handle_write_completed(conf, r10_bio);
2498 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2499 sync_request_write(mddev, r10_bio);
2500 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2501 recovery_request_write(mddev, r10_bio);
2502 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2503 handle_read_error(mddev, r10_bio);
2504 else {
2505 /* just a partial read to be scheduled from a
2506 * separate context
2508 int slot = r10_bio->read_slot;
2509 generic_make_request(r10_bio->devs[slot].bio);
2512 cond_resched();
2513 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2514 md_check_recovery(mddev);
2516 blk_finish_plug(&plug);
2520 static int init_resync(struct r10conf *conf)
2522 int buffs;
2523 int i;
2525 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2526 BUG_ON(conf->r10buf_pool);
2527 conf->have_replacement = 0;
2528 for (i = 0; i < conf->raid_disks; i++)
2529 if (conf->mirrors[i].replacement)
2530 conf->have_replacement = 1;
2531 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2532 if (!conf->r10buf_pool)
2533 return -ENOMEM;
2534 conf->next_resync = 0;
2535 return 0;
2539 * perform a "sync" on one "block"
2541 * We need to make sure that no normal I/O request - particularly write
2542 * requests - conflict with active sync requests.
2544 * This is achieved by tracking pending requests and a 'barrier' concept
2545 * that can be installed to exclude normal IO requests.
2547 * Resync and recovery are handled very differently.
2548 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2550 * For resync, we iterate over virtual addresses, read all copies,
2551 * and update if there are differences. If only one copy is live,
2552 * skip it.
2553 * For recovery, we iterate over physical addresses, read a good
2554 * value for each non-in_sync drive, and over-write.
2556 * So, for recovery we may have several outstanding complex requests for a
2557 * given address, one for each out-of-sync device. We model this by allocating
2558 * a number of r10_bio structures, one for each out-of-sync device.
2559 * As we setup these structures, we collect all bio's together into a list
2560 * which we then process collectively to add pages, and then process again
2561 * to pass to generic_make_request.
2563 * The r10_bio structures are linked using a borrowed master_bio pointer.
2564 * This link is counted in ->remaining. When the r10_bio that points to NULL
2565 * has its remaining count decremented to 0, the whole complex operation
2566 * is complete.
2570 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
2571 int *skipped, int go_faster)
2573 struct r10conf *conf = mddev->private;
2574 struct r10bio *r10_bio;
2575 struct bio *biolist = NULL, *bio;
2576 sector_t max_sector, nr_sectors;
2577 int i;
2578 int max_sync;
2579 sector_t sync_blocks;
2580 sector_t sectors_skipped = 0;
2581 int chunks_skipped = 0;
2583 if (!conf->r10buf_pool)
2584 if (init_resync(conf))
2585 return 0;
2587 skipped:
2588 max_sector = mddev->dev_sectors;
2589 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2590 max_sector = mddev->resync_max_sectors;
2591 if (sector_nr >= max_sector) {
2592 /* If we aborted, we need to abort the
2593 * sync on the 'current' bitmap chucks (there can
2594 * be several when recovering multiple devices).
2595 * as we may have started syncing it but not finished.
2596 * We can find the current address in
2597 * mddev->curr_resync, but for recovery,
2598 * we need to convert that to several
2599 * virtual addresses.
2601 if (mddev->curr_resync < max_sector) { /* aborted */
2602 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2603 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2604 &sync_blocks, 1);
2605 else for (i=0; i<conf->raid_disks; i++) {
2606 sector_t sect =
2607 raid10_find_virt(conf, mddev->curr_resync, i);
2608 bitmap_end_sync(mddev->bitmap, sect,
2609 &sync_blocks, 1);
2611 } else {
2612 /* completed sync */
2613 if ((!mddev->bitmap || conf->fullsync)
2614 && conf->have_replacement
2615 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2616 /* Completed a full sync so the replacements
2617 * are now fully recovered.
2619 for (i = 0; i < conf->raid_disks; i++)
2620 if (conf->mirrors[i].replacement)
2621 conf->mirrors[i].replacement
2622 ->recovery_offset
2623 = MaxSector;
2625 conf->fullsync = 0;
2627 bitmap_close_sync(mddev->bitmap);
2628 close_sync(conf);
2629 *skipped = 1;
2630 return sectors_skipped;
2632 if (chunks_skipped >= conf->raid_disks) {
2633 /* if there has been nothing to do on any drive,
2634 * then there is nothing to do at all..
2636 *skipped = 1;
2637 return (max_sector - sector_nr) + sectors_skipped;
2640 if (max_sector > mddev->resync_max)
2641 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2643 /* make sure whole request will fit in a chunk - if chunks
2644 * are meaningful
2646 if (conf->near_copies < conf->raid_disks &&
2647 max_sector > (sector_nr | conf->chunk_mask))
2648 max_sector = (sector_nr | conf->chunk_mask) + 1;
2650 * If there is non-resync activity waiting for us then
2651 * put in a delay to throttle resync.
2653 if (!go_faster && conf->nr_waiting)
2654 msleep_interruptible(1000);
2656 /* Again, very different code for resync and recovery.
2657 * Both must result in an r10bio with a list of bios that
2658 * have bi_end_io, bi_sector, bi_bdev set,
2659 * and bi_private set to the r10bio.
2660 * For recovery, we may actually create several r10bios
2661 * with 2 bios in each, that correspond to the bios in the main one.
2662 * In this case, the subordinate r10bios link back through a
2663 * borrowed master_bio pointer, and the counter in the master
2664 * includes a ref from each subordinate.
2666 /* First, we decide what to do and set ->bi_end_io
2667 * To end_sync_read if we want to read, and
2668 * end_sync_write if we will want to write.
2671 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2672 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2673 /* recovery... the complicated one */
2674 int j;
2675 r10_bio = NULL;
2677 for (i=0 ; i<conf->raid_disks; i++) {
2678 int still_degraded;
2679 struct r10bio *rb2;
2680 sector_t sect;
2681 int must_sync;
2682 int any_working;
2683 struct mirror_info *mirror = &conf->mirrors[i];
2685 if ((mirror->rdev == NULL ||
2686 test_bit(In_sync, &mirror->rdev->flags))
2688 (mirror->replacement == NULL ||
2689 test_bit(Faulty,
2690 &mirror->replacement->flags)))
2691 continue;
2693 still_degraded = 0;
2694 /* want to reconstruct this device */
2695 rb2 = r10_bio;
2696 sect = raid10_find_virt(conf, sector_nr, i);
2697 /* Unless we are doing a full sync, or a replacement
2698 * we only need to recover the block if it is set in
2699 * the bitmap
2701 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2702 &sync_blocks, 1);
2703 if (sync_blocks < max_sync)
2704 max_sync = sync_blocks;
2705 if (!must_sync &&
2706 mirror->replacement == NULL &&
2707 !conf->fullsync) {
2708 /* yep, skip the sync_blocks here, but don't assume
2709 * that there will never be anything to do here
2711 chunks_skipped = -1;
2712 continue;
2715 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2716 raise_barrier(conf, rb2 != NULL);
2717 atomic_set(&r10_bio->remaining, 0);
2719 r10_bio->master_bio = (struct bio*)rb2;
2720 if (rb2)
2721 atomic_inc(&rb2->remaining);
2722 r10_bio->mddev = mddev;
2723 set_bit(R10BIO_IsRecover, &r10_bio->state);
2724 r10_bio->sector = sect;
2726 raid10_find_phys(conf, r10_bio);
2728 /* Need to check if the array will still be
2729 * degraded
2731 for (j=0; j<conf->raid_disks; j++)
2732 if (conf->mirrors[j].rdev == NULL ||
2733 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2734 still_degraded = 1;
2735 break;
2738 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2739 &sync_blocks, still_degraded);
2741 any_working = 0;
2742 for (j=0; j<conf->copies;j++) {
2743 int k;
2744 int d = r10_bio->devs[j].devnum;
2745 sector_t from_addr, to_addr;
2746 struct md_rdev *rdev;
2747 sector_t sector, first_bad;
2748 int bad_sectors;
2749 if (!conf->mirrors[d].rdev ||
2750 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2751 continue;
2752 /* This is where we read from */
2753 any_working = 1;
2754 rdev = conf->mirrors[d].rdev;
2755 sector = r10_bio->devs[j].addr;
2757 if (is_badblock(rdev, sector, max_sync,
2758 &first_bad, &bad_sectors)) {
2759 if (first_bad > sector)
2760 max_sync = first_bad - sector;
2761 else {
2762 bad_sectors -= (sector
2763 - first_bad);
2764 if (max_sync > bad_sectors)
2765 max_sync = bad_sectors;
2766 continue;
2769 bio = r10_bio->devs[0].bio;
2770 bio->bi_next = biolist;
2771 biolist = bio;
2772 bio->bi_private = r10_bio;
2773 bio->bi_end_io = end_sync_read;
2774 bio->bi_rw = READ;
2775 from_addr = r10_bio->devs[j].addr;
2776 bio->bi_sector = from_addr + rdev->data_offset;
2777 bio->bi_bdev = rdev->bdev;
2778 atomic_inc(&rdev->nr_pending);
2779 /* and we write to 'i' (if not in_sync) */
2781 for (k=0; k<conf->copies; k++)
2782 if (r10_bio->devs[k].devnum == i)
2783 break;
2784 BUG_ON(k == conf->copies);
2785 to_addr = r10_bio->devs[k].addr;
2786 r10_bio->devs[0].devnum = d;
2787 r10_bio->devs[0].addr = from_addr;
2788 r10_bio->devs[1].devnum = i;
2789 r10_bio->devs[1].addr = to_addr;
2791 rdev = mirror->rdev;
2792 if (!test_bit(In_sync, &rdev->flags)) {
2793 bio = r10_bio->devs[1].bio;
2794 bio->bi_next = biolist;
2795 biolist = bio;
2796 bio->bi_private = r10_bio;
2797 bio->bi_end_io = end_sync_write;
2798 bio->bi_rw = WRITE;
2799 bio->bi_sector = to_addr
2800 + rdev->data_offset;
2801 bio->bi_bdev = rdev->bdev;
2802 atomic_inc(&r10_bio->remaining);
2803 } else
2804 r10_bio->devs[1].bio->bi_end_io = NULL;
2806 /* and maybe write to replacement */
2807 bio = r10_bio->devs[1].repl_bio;
2808 if (bio)
2809 bio->bi_end_io = NULL;
2810 rdev = mirror->replacement;
2811 /* Note: if rdev != NULL, then bio
2812 * cannot be NULL as r10buf_pool_alloc will
2813 * have allocated it.
2814 * So the second test here is pointless.
2815 * But it keeps semantic-checkers happy, and
2816 * this comment keeps human reviewers
2817 * happy.
2819 if (rdev == NULL || bio == NULL ||
2820 test_bit(Faulty, &rdev->flags))
2821 break;
2822 bio->bi_next = biolist;
2823 biolist = bio;
2824 bio->bi_private = r10_bio;
2825 bio->bi_end_io = end_sync_write;
2826 bio->bi_rw = WRITE;
2827 bio->bi_sector = to_addr + rdev->data_offset;
2828 bio->bi_bdev = rdev->bdev;
2829 atomic_inc(&r10_bio->remaining);
2830 break;
2832 if (j == conf->copies) {
2833 /* Cannot recover, so abort the recovery or
2834 * record a bad block */
2835 put_buf(r10_bio);
2836 if (rb2)
2837 atomic_dec(&rb2->remaining);
2838 r10_bio = rb2;
2839 if (any_working) {
2840 /* problem is that there are bad blocks
2841 * on other device(s)
2843 int k;
2844 for (k = 0; k < conf->copies; k++)
2845 if (r10_bio->devs[k].devnum == i)
2846 break;
2847 if (!test_bit(In_sync,
2848 &mirror->rdev->flags)
2849 && !rdev_set_badblocks(
2850 mirror->rdev,
2851 r10_bio->devs[k].addr,
2852 max_sync, 0))
2853 any_working = 0;
2854 if (mirror->replacement &&
2855 !rdev_set_badblocks(
2856 mirror->replacement,
2857 r10_bio->devs[k].addr,
2858 max_sync, 0))
2859 any_working = 0;
2861 if (!any_working) {
2862 if (!test_and_set_bit(MD_RECOVERY_INTR,
2863 &mddev->recovery))
2864 printk(KERN_INFO "md/raid10:%s: insufficient "
2865 "working devices for recovery.\n",
2866 mdname(mddev));
2867 mirror->recovery_disabled
2868 = mddev->recovery_disabled;
2870 break;
2873 if (biolist == NULL) {
2874 while (r10_bio) {
2875 struct r10bio *rb2 = r10_bio;
2876 r10_bio = (struct r10bio*) rb2->master_bio;
2877 rb2->master_bio = NULL;
2878 put_buf(rb2);
2880 goto giveup;
2882 } else {
2883 /* resync. Schedule a read for every block at this virt offset */
2884 int count = 0;
2886 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2888 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2889 &sync_blocks, mddev->degraded) &&
2890 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2891 &mddev->recovery)) {
2892 /* We can skip this block */
2893 *skipped = 1;
2894 return sync_blocks + sectors_skipped;
2896 if (sync_blocks < max_sync)
2897 max_sync = sync_blocks;
2898 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2900 r10_bio->mddev = mddev;
2901 atomic_set(&r10_bio->remaining, 0);
2902 raise_barrier(conf, 0);
2903 conf->next_resync = sector_nr;
2905 r10_bio->master_bio = NULL;
2906 r10_bio->sector = sector_nr;
2907 set_bit(R10BIO_IsSync, &r10_bio->state);
2908 raid10_find_phys(conf, r10_bio);
2909 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2911 for (i=0; i<conf->copies; i++) {
2912 int d = r10_bio->devs[i].devnum;
2913 sector_t first_bad, sector;
2914 int bad_sectors;
2916 if (r10_bio->devs[i].repl_bio)
2917 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
2919 bio = r10_bio->devs[i].bio;
2920 bio->bi_end_io = NULL;
2921 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2922 if (conf->mirrors[d].rdev == NULL ||
2923 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2924 continue;
2925 sector = r10_bio->devs[i].addr;
2926 if (is_badblock(conf->mirrors[d].rdev,
2927 sector, max_sync,
2928 &first_bad, &bad_sectors)) {
2929 if (first_bad > sector)
2930 max_sync = first_bad - sector;
2931 else {
2932 bad_sectors -= (sector - first_bad);
2933 if (max_sync > bad_sectors)
2934 max_sync = max_sync;
2935 continue;
2938 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2939 atomic_inc(&r10_bio->remaining);
2940 bio->bi_next = biolist;
2941 biolist = bio;
2942 bio->bi_private = r10_bio;
2943 bio->bi_end_io = end_sync_read;
2944 bio->bi_rw = READ;
2945 bio->bi_sector = sector +
2946 conf->mirrors[d].rdev->data_offset;
2947 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2948 count++;
2950 if (conf->mirrors[d].replacement == NULL ||
2951 test_bit(Faulty,
2952 &conf->mirrors[d].replacement->flags))
2953 continue;
2955 /* Need to set up for writing to the replacement */
2956 bio = r10_bio->devs[i].repl_bio;
2957 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2959 sector = r10_bio->devs[i].addr;
2960 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2961 bio->bi_next = biolist;
2962 biolist = bio;
2963 bio->bi_private = r10_bio;
2964 bio->bi_end_io = end_sync_write;
2965 bio->bi_rw = WRITE;
2966 bio->bi_sector = sector +
2967 conf->mirrors[d].replacement->data_offset;
2968 bio->bi_bdev = conf->mirrors[d].replacement->bdev;
2969 count++;
2972 if (count < 2) {
2973 for (i=0; i<conf->copies; i++) {
2974 int d = r10_bio->devs[i].devnum;
2975 if (r10_bio->devs[i].bio->bi_end_io)
2976 rdev_dec_pending(conf->mirrors[d].rdev,
2977 mddev);
2978 if (r10_bio->devs[i].repl_bio &&
2979 r10_bio->devs[i].repl_bio->bi_end_io)
2980 rdev_dec_pending(
2981 conf->mirrors[d].replacement,
2982 mddev);
2984 put_buf(r10_bio);
2985 biolist = NULL;
2986 goto giveup;
2990 for (bio = biolist; bio ; bio=bio->bi_next) {
2992 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2993 if (bio->bi_end_io)
2994 bio->bi_flags |= 1 << BIO_UPTODATE;
2995 bio->bi_vcnt = 0;
2996 bio->bi_idx = 0;
2997 bio->bi_phys_segments = 0;
2998 bio->bi_size = 0;
3001 nr_sectors = 0;
3002 if (sector_nr + max_sync < max_sector)
3003 max_sector = sector_nr + max_sync;
3004 do {
3005 struct page *page;
3006 int len = PAGE_SIZE;
3007 if (sector_nr + (len>>9) > max_sector)
3008 len = (max_sector - sector_nr) << 9;
3009 if (len == 0)
3010 break;
3011 for (bio= biolist ; bio ; bio=bio->bi_next) {
3012 struct bio *bio2;
3013 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
3014 if (bio_add_page(bio, page, len, 0))
3015 continue;
3017 /* stop here */
3018 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
3019 for (bio2 = biolist;
3020 bio2 && bio2 != bio;
3021 bio2 = bio2->bi_next) {
3022 /* remove last page from this bio */
3023 bio2->bi_vcnt--;
3024 bio2->bi_size -= len;
3025 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
3027 goto bio_full;
3029 nr_sectors += len>>9;
3030 sector_nr += len>>9;
3031 } while (biolist->bi_vcnt < RESYNC_PAGES);
3032 bio_full:
3033 r10_bio->sectors = nr_sectors;
3035 while (biolist) {
3036 bio = biolist;
3037 biolist = biolist->bi_next;
3039 bio->bi_next = NULL;
3040 r10_bio = bio->bi_private;
3041 r10_bio->sectors = nr_sectors;
3043 if (bio->bi_end_io == end_sync_read) {
3044 md_sync_acct(bio->bi_bdev, nr_sectors);
3045 generic_make_request(bio);
3049 if (sectors_skipped)
3050 /* pretend they weren't skipped, it makes
3051 * no important difference in this case
3053 md_done_sync(mddev, sectors_skipped, 1);
3055 return sectors_skipped + nr_sectors;
3056 giveup:
3057 /* There is nowhere to write, so all non-sync
3058 * drives must be failed or in resync, all drives
3059 * have a bad block, so try the next chunk...
3061 if (sector_nr + max_sync < max_sector)
3062 max_sector = sector_nr + max_sync;
3064 sectors_skipped += (max_sector - sector_nr);
3065 chunks_skipped ++;
3066 sector_nr = max_sector;
3067 goto skipped;
3070 static sector_t
3071 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3073 sector_t size;
3074 struct r10conf *conf = mddev->private;
3076 if (!raid_disks)
3077 raid_disks = conf->raid_disks;
3078 if (!sectors)
3079 sectors = conf->dev_sectors;
3081 size = sectors >> conf->chunk_shift;
3082 sector_div(size, conf->far_copies);
3083 size = size * raid_disks;
3084 sector_div(size, conf->near_copies);
3086 return size << conf->chunk_shift;
3090 static struct r10conf *setup_conf(struct mddev *mddev)
3092 struct r10conf *conf = NULL;
3093 int nc, fc, fo;
3094 sector_t stride, size;
3095 int err = -EINVAL;
3097 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
3098 !is_power_of_2(mddev->new_chunk_sectors)) {
3099 printk(KERN_ERR "md/raid10:%s: chunk size must be "
3100 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
3101 mdname(mddev), PAGE_SIZE);
3102 goto out;
3105 nc = mddev->new_layout & 255;
3106 fc = (mddev->new_layout >> 8) & 255;
3107 fo = mddev->new_layout & (1<<16);
3109 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
3110 (mddev->new_layout >> 17)) {
3111 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3112 mdname(mddev), mddev->new_layout);
3113 goto out;
3116 err = -ENOMEM;
3117 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3118 if (!conf)
3119 goto out;
3121 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
3122 GFP_KERNEL);
3123 if (!conf->mirrors)
3124 goto out;
3126 conf->tmppage = alloc_page(GFP_KERNEL);
3127 if (!conf->tmppage)
3128 goto out;
3131 conf->raid_disks = mddev->raid_disks;
3132 conf->near_copies = nc;
3133 conf->far_copies = fc;
3134 conf->copies = nc*fc;
3135 conf->far_offset = fo;
3136 conf->chunk_mask = mddev->new_chunk_sectors - 1;
3137 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
3139 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3140 r10bio_pool_free, conf);
3141 if (!conf->r10bio_pool)
3142 goto out;
3144 size = mddev->dev_sectors >> conf->chunk_shift;
3145 sector_div(size, fc);
3146 size = size * conf->raid_disks;
3147 sector_div(size, nc);
3148 /* 'size' is now the number of chunks in the array */
3149 /* calculate "used chunks per device" in 'stride' */
3150 stride = size * conf->copies;
3152 /* We need to round up when dividing by raid_disks to
3153 * get the stride size.
3155 stride += conf->raid_disks - 1;
3156 sector_div(stride, conf->raid_disks);
3158 conf->dev_sectors = stride << conf->chunk_shift;
3160 if (fo)
3161 stride = 1;
3162 else
3163 sector_div(stride, fc);
3164 conf->stride = stride << conf->chunk_shift;
3167 spin_lock_init(&conf->device_lock);
3168 INIT_LIST_HEAD(&conf->retry_list);
3170 spin_lock_init(&conf->resync_lock);
3171 init_waitqueue_head(&conf->wait_barrier);
3173 conf->thread = md_register_thread(raid10d, mddev, NULL);
3174 if (!conf->thread)
3175 goto out;
3177 conf->mddev = mddev;
3178 return conf;
3180 out:
3181 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
3182 mdname(mddev));
3183 if (conf) {
3184 if (conf->r10bio_pool)
3185 mempool_destroy(conf->r10bio_pool);
3186 kfree(conf->mirrors);
3187 safe_put_page(conf->tmppage);
3188 kfree(conf);
3190 return ERR_PTR(err);
3193 static int run(struct mddev *mddev)
3195 struct r10conf *conf;
3196 int i, disk_idx, chunk_size;
3197 struct mirror_info *disk;
3198 struct md_rdev *rdev;
3199 sector_t size;
3202 * copy the already verified devices into our private RAID10
3203 * bookkeeping area. [whatever we allocate in run(),
3204 * should be freed in stop()]
3207 if (mddev->private == NULL) {
3208 conf = setup_conf(mddev);
3209 if (IS_ERR(conf))
3210 return PTR_ERR(conf);
3211 mddev->private = conf;
3213 conf = mddev->private;
3214 if (!conf)
3215 goto out;
3217 mddev->thread = conf->thread;
3218 conf->thread = NULL;
3220 chunk_size = mddev->chunk_sectors << 9;
3221 blk_queue_io_min(mddev->queue, chunk_size);
3222 if (conf->raid_disks % conf->near_copies)
3223 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
3224 else
3225 blk_queue_io_opt(mddev->queue, chunk_size *
3226 (conf->raid_disks / conf->near_copies));
3228 list_for_each_entry(rdev, &mddev->disks, same_set) {
3230 disk_idx = rdev->raid_disk;
3231 if (disk_idx >= conf->raid_disks
3232 || disk_idx < 0)
3233 continue;
3234 disk = conf->mirrors + disk_idx;
3236 if (test_bit(Replacement, &rdev->flags)) {
3237 if (disk->replacement)
3238 goto out_free_conf;
3239 disk->replacement = rdev;
3240 } else {
3241 if (disk->rdev)
3242 goto out_free_conf;
3243 disk->rdev = rdev;
3246 disk->rdev = rdev;
3247 disk_stack_limits(mddev->gendisk, rdev->bdev,
3248 rdev->data_offset << 9);
3249 /* as we don't honour merge_bvec_fn, we must never risk
3250 * violating it, so limit max_segments to 1 lying
3251 * within a single page.
3253 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
3254 blk_queue_max_segments(mddev->queue, 1);
3255 blk_queue_segment_boundary(mddev->queue,
3256 PAGE_CACHE_SIZE - 1);
3259 disk->head_position = 0;
3261 /* need to check that every block has at least one working mirror */
3262 if (!enough(conf, -1)) {
3263 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
3264 mdname(mddev));
3265 goto out_free_conf;
3268 mddev->degraded = 0;
3269 for (i = 0; i < conf->raid_disks; i++) {
3271 disk = conf->mirrors + i;
3273 if (!disk->rdev && disk->replacement) {
3274 /* The replacement is all we have - use it */
3275 disk->rdev = disk->replacement;
3276 disk->replacement = NULL;
3277 clear_bit(Replacement, &disk->rdev->flags);
3280 if (!disk->rdev ||
3281 !test_bit(In_sync, &disk->rdev->flags)) {
3282 disk->head_position = 0;
3283 mddev->degraded++;
3284 if (disk->rdev)
3285 conf->fullsync = 1;
3287 disk->recovery_disabled = mddev->recovery_disabled - 1;
3290 if (mddev->recovery_cp != MaxSector)
3291 printk(KERN_NOTICE "md/raid10:%s: not clean"
3292 " -- starting background reconstruction\n",
3293 mdname(mddev));
3294 printk(KERN_INFO
3295 "md/raid10:%s: active with %d out of %d devices\n",
3296 mdname(mddev), conf->raid_disks - mddev->degraded,
3297 conf->raid_disks);
3299 * Ok, everything is just fine now
3301 mddev->dev_sectors = conf->dev_sectors;
3302 size = raid10_size(mddev, 0, 0);
3303 md_set_array_sectors(mddev, size);
3304 mddev->resync_max_sectors = size;
3306 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
3307 mddev->queue->backing_dev_info.congested_data = mddev;
3309 /* Calculate max read-ahead size.
3310 * We need to readahead at least twice a whole stripe....
3311 * maybe...
3314 int stripe = conf->raid_disks *
3315 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3316 stripe /= conf->near_copies;
3317 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
3318 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
3321 if (conf->near_copies < conf->raid_disks)
3322 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
3324 if (md_integrity_register(mddev))
3325 goto out_free_conf;
3327 return 0;
3329 out_free_conf:
3330 md_unregister_thread(&mddev->thread);
3331 if (conf->r10bio_pool)
3332 mempool_destroy(conf->r10bio_pool);
3333 safe_put_page(conf->tmppage);
3334 kfree(conf->mirrors);
3335 kfree(conf);
3336 mddev->private = NULL;
3337 out:
3338 return -EIO;
3341 static int stop(struct mddev *mddev)
3343 struct r10conf *conf = mddev->private;
3345 raise_barrier(conf, 0);
3346 lower_barrier(conf);
3348 md_unregister_thread(&mddev->thread);
3349 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3350 if (conf->r10bio_pool)
3351 mempool_destroy(conf->r10bio_pool);
3352 kfree(conf->mirrors);
3353 kfree(conf);
3354 mddev->private = NULL;
3355 return 0;
3358 static void raid10_quiesce(struct mddev *mddev, int state)
3360 struct r10conf *conf = mddev->private;
3362 switch(state) {
3363 case 1:
3364 raise_barrier(conf, 0);
3365 break;
3366 case 0:
3367 lower_barrier(conf);
3368 break;
3372 static void *raid10_takeover_raid0(struct mddev *mddev)
3374 struct md_rdev *rdev;
3375 struct r10conf *conf;
3377 if (mddev->degraded > 0) {
3378 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3379 mdname(mddev));
3380 return ERR_PTR(-EINVAL);
3383 /* Set new parameters */
3384 mddev->new_level = 10;
3385 /* new layout: far_copies = 1, near_copies = 2 */
3386 mddev->new_layout = (1<<8) + 2;
3387 mddev->new_chunk_sectors = mddev->chunk_sectors;
3388 mddev->delta_disks = mddev->raid_disks;
3389 mddev->raid_disks *= 2;
3390 /* make sure it will be not marked as dirty */
3391 mddev->recovery_cp = MaxSector;
3393 conf = setup_conf(mddev);
3394 if (!IS_ERR(conf)) {
3395 list_for_each_entry(rdev, &mddev->disks, same_set)
3396 if (rdev->raid_disk >= 0)
3397 rdev->new_raid_disk = rdev->raid_disk * 2;
3398 conf->barrier = 1;
3401 return conf;
3404 static void *raid10_takeover(struct mddev *mddev)
3406 struct r0conf *raid0_conf;
3408 /* raid10 can take over:
3409 * raid0 - providing it has only two drives
3411 if (mddev->level == 0) {
3412 /* for raid0 takeover only one zone is supported */
3413 raid0_conf = mddev->private;
3414 if (raid0_conf->nr_strip_zones > 1) {
3415 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3416 " with more than one zone.\n",
3417 mdname(mddev));
3418 return ERR_PTR(-EINVAL);
3420 return raid10_takeover_raid0(mddev);
3422 return ERR_PTR(-EINVAL);
3425 static struct md_personality raid10_personality =
3427 .name = "raid10",
3428 .level = 10,
3429 .owner = THIS_MODULE,
3430 .make_request = make_request,
3431 .run = run,
3432 .stop = stop,
3433 .status = status,
3434 .error_handler = error,
3435 .hot_add_disk = raid10_add_disk,
3436 .hot_remove_disk= raid10_remove_disk,
3437 .spare_active = raid10_spare_active,
3438 .sync_request = sync_request,
3439 .quiesce = raid10_quiesce,
3440 .size = raid10_size,
3441 .takeover = raid10_takeover,
3444 static int __init raid_init(void)
3446 return register_md_personality(&raid10_personality);
3449 static void raid_exit(void)
3451 unregister_md_personality(&raid10_personality);
3454 module_init(raid_init);
3455 module_exit(raid_exit);
3456 MODULE_LICENSE("GPL");
3457 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
3458 MODULE_ALIAS("md-personality-9"); /* RAID10 */
3459 MODULE_ALIAS("md-raid10");
3460 MODULE_ALIAS("md-level-10");
3462 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);