eCryptfs: Copy up attributes of the lower target inode after rename
[linux/fpc-iii.git] / drivers / md / raid10.c
blob7a9eef64c4f9355e8816721eddceefee66d91efd
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 bios array */
77 return kzalloc(size, gfp_flags);
80 static void r10bio_pool_free(void *r10_bio, void *data)
82 kfree(r10_bio);
85 /* Maximum size of each resync request */
86 #define RESYNC_BLOCK_SIZE (64*1024)
87 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
88 /* amount of memory to reserve for resync requests */
89 #define RESYNC_WINDOW (1024*1024)
90 /* maximum number of concurrent requests, memory permitting */
91 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
94 * When performing a resync, we need to read and compare, so
95 * we need as many pages are there are copies.
96 * When performing a recovery, we need 2 bios, one for read,
97 * one for write (we recover only one drive per r10buf)
100 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
102 struct r10conf *conf = data;
103 struct page *page;
104 struct r10bio *r10_bio;
105 struct bio *bio;
106 int i, j;
107 int nalloc;
109 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
110 if (!r10_bio)
111 return NULL;
113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
114 nalloc = conf->copies; /* resync */
115 else
116 nalloc = 2; /* recovery */
119 * Allocate bios.
121 for (j = nalloc ; j-- ; ) {
122 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
123 if (!bio)
124 goto out_free_bio;
125 r10_bio->devs[j].bio = bio;
128 * Allocate RESYNC_PAGES data pages and attach them
129 * where needed.
131 for (j = 0 ; j < nalloc; j++) {
132 bio = r10_bio->devs[j].bio;
133 for (i = 0; i < RESYNC_PAGES; i++) {
134 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
135 &conf->mddev->recovery)) {
136 /* we can share bv_page's during recovery */
137 struct bio *rbio = r10_bio->devs[0].bio;
138 page = rbio->bi_io_vec[i].bv_page;
139 get_page(page);
140 } else
141 page = alloc_page(gfp_flags);
142 if (unlikely(!page))
143 goto out_free_pages;
145 bio->bi_io_vec[i].bv_page = page;
149 return r10_bio;
151 out_free_pages:
152 for ( ; i > 0 ; i--)
153 safe_put_page(bio->bi_io_vec[i-1].bv_page);
154 while (j--)
155 for (i = 0; i < RESYNC_PAGES ; i++)
156 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
157 j = -1;
158 out_free_bio:
159 while ( ++j < nalloc )
160 bio_put(r10_bio->devs[j].bio);
161 r10bio_pool_free(r10_bio, conf);
162 return NULL;
165 static void r10buf_pool_free(void *__r10_bio, void *data)
167 int i;
168 struct r10conf *conf = data;
169 struct r10bio *r10bio = __r10_bio;
170 int j;
172 for (j=0; j < conf->copies; j++) {
173 struct bio *bio = r10bio->devs[j].bio;
174 if (bio) {
175 for (i = 0; i < RESYNC_PAGES; i++) {
176 safe_put_page(bio->bi_io_vec[i].bv_page);
177 bio->bi_io_vec[i].bv_page = NULL;
179 bio_put(bio);
182 r10bio_pool_free(r10bio, conf);
185 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
187 int i;
189 for (i = 0; i < conf->copies; i++) {
190 struct bio **bio = & r10_bio->devs[i].bio;
191 if (!BIO_SPECIAL(*bio))
192 bio_put(*bio);
193 *bio = NULL;
197 static void free_r10bio(struct r10bio *r10_bio)
199 struct r10conf *conf = r10_bio->mddev->private;
201 put_all_bios(conf, r10_bio);
202 mempool_free(r10_bio, conf->r10bio_pool);
205 static void put_buf(struct r10bio *r10_bio)
207 struct r10conf *conf = r10_bio->mddev->private;
209 mempool_free(r10_bio, conf->r10buf_pool);
211 lower_barrier(conf);
214 static void reschedule_retry(struct r10bio *r10_bio)
216 unsigned long flags;
217 struct mddev *mddev = r10_bio->mddev;
218 struct r10conf *conf = mddev->private;
220 spin_lock_irqsave(&conf->device_lock, flags);
221 list_add(&r10_bio->retry_list, &conf->retry_list);
222 conf->nr_queued ++;
223 spin_unlock_irqrestore(&conf->device_lock, flags);
225 /* wake up frozen array... */
226 wake_up(&conf->wait_barrier);
228 md_wakeup_thread(mddev->thread);
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
234 * cache layer.
236 static void raid_end_bio_io(struct r10bio *r10_bio)
238 struct bio *bio = r10_bio->master_bio;
239 int done;
240 struct r10conf *conf = r10_bio->mddev->private;
242 if (bio->bi_phys_segments) {
243 unsigned long flags;
244 spin_lock_irqsave(&conf->device_lock, flags);
245 bio->bi_phys_segments--;
246 done = (bio->bi_phys_segments == 0);
247 spin_unlock_irqrestore(&conf->device_lock, flags);
248 } else
249 done = 1;
250 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
251 clear_bit(BIO_UPTODATE, &bio->bi_flags);
252 if (done) {
253 bio_endio(bio, 0);
255 * Wake up any possible resync thread that waits for the device
256 * to go idle.
258 allow_barrier(conf);
260 free_r10bio(r10_bio);
264 * Update disk head position estimator based on IRQ completion info.
266 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
268 struct r10conf *conf = r10_bio->mddev->private;
270 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
271 r10_bio->devs[slot].addr + (r10_bio->sectors);
275 * Find the disk number which triggered given bio
277 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
278 struct bio *bio, int *slotp)
280 int slot;
282 for (slot = 0; slot < conf->copies; slot++)
283 if (r10_bio->devs[slot].bio == bio)
284 break;
286 BUG_ON(slot == conf->copies);
287 update_head_pos(slot, r10_bio);
289 if (slotp)
290 *slotp = slot;
291 return r10_bio->devs[slot].devnum;
294 static void raid10_end_read_request(struct bio *bio, int error)
296 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
297 struct r10bio *r10_bio = bio->bi_private;
298 int slot, dev;
299 struct r10conf *conf = r10_bio->mddev->private;
302 slot = r10_bio->read_slot;
303 dev = r10_bio->devs[slot].devnum;
305 * this branch is our 'one mirror IO has finished' event handler:
307 update_head_pos(slot, r10_bio);
309 if (uptodate) {
311 * Set R10BIO_Uptodate in our master bio, so that
312 * we will return a good error code to the higher
313 * levels even if IO on some other mirrored buffer fails.
315 * The 'master' represents the composite IO operation to
316 * user-side. So if something waits for IO, then it will
317 * wait for the 'master' bio.
319 set_bit(R10BIO_Uptodate, &r10_bio->state);
320 raid_end_bio_io(r10_bio);
321 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
322 } else {
324 * oops, read error - keep the refcount on the rdev
326 char b[BDEVNAME_SIZE];
327 printk_ratelimited(KERN_ERR
328 "md/raid10:%s: %s: rescheduling sector %llu\n",
329 mdname(conf->mddev),
330 bdevname(conf->mirrors[dev].rdev->bdev, b),
331 (unsigned long long)r10_bio->sector);
332 set_bit(R10BIO_ReadError, &r10_bio->state);
333 reschedule_retry(r10_bio);
337 static void close_write(struct r10bio *r10_bio)
339 /* clear the bitmap if all writes complete successfully */
340 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
341 r10_bio->sectors,
342 !test_bit(R10BIO_Degraded, &r10_bio->state),
344 md_write_end(r10_bio->mddev);
347 static void one_write_done(struct r10bio *r10_bio)
349 if (atomic_dec_and_test(&r10_bio->remaining)) {
350 if (test_bit(R10BIO_WriteError, &r10_bio->state))
351 reschedule_retry(r10_bio);
352 else {
353 close_write(r10_bio);
354 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
355 reschedule_retry(r10_bio);
356 else
357 raid_end_bio_io(r10_bio);
362 static void raid10_end_write_request(struct bio *bio, int error)
364 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
365 struct r10bio *r10_bio = bio->bi_private;
366 int dev;
367 int dec_rdev = 1;
368 struct r10conf *conf = r10_bio->mddev->private;
369 int slot;
371 dev = find_bio_disk(conf, r10_bio, bio, &slot);
374 * this branch is our 'one mirror IO has finished' event handler:
376 if (!uptodate) {
377 set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags);
378 set_bit(R10BIO_WriteError, &r10_bio->state);
379 dec_rdev = 0;
380 } else {
382 * Set R10BIO_Uptodate in our master bio, so that
383 * we will return a good error code for to the higher
384 * levels even if IO on some other mirrored buffer fails.
386 * The 'master' represents the composite IO operation to
387 * user-side. So if something waits for IO, then it will
388 * wait for the 'master' bio.
390 sector_t first_bad;
391 int bad_sectors;
393 set_bit(R10BIO_Uptodate, &r10_bio->state);
395 /* Maybe we can clear some bad blocks. */
396 if (is_badblock(conf->mirrors[dev].rdev,
397 r10_bio->devs[slot].addr,
398 r10_bio->sectors,
399 &first_bad, &bad_sectors)) {
400 bio_put(bio);
401 r10_bio->devs[slot].bio = IO_MADE_GOOD;
402 dec_rdev = 0;
403 set_bit(R10BIO_MadeGood, &r10_bio->state);
409 * Let's see if all mirrored write operations have finished
410 * already.
412 one_write_done(r10_bio);
413 if (dec_rdev)
414 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
419 * RAID10 layout manager
420 * As well as the chunksize and raid_disks count, there are two
421 * parameters: near_copies and far_copies.
422 * near_copies * far_copies must be <= raid_disks.
423 * Normally one of these will be 1.
424 * If both are 1, we get raid0.
425 * If near_copies == raid_disks, we get raid1.
427 * Chunks are laid out in raid0 style with near_copies copies of the
428 * first chunk, followed by near_copies copies of the next chunk and
429 * so on.
430 * If far_copies > 1, then after 1/far_copies of the array has been assigned
431 * as described above, we start again with a device offset of near_copies.
432 * So we effectively have another copy of the whole array further down all
433 * the drives, but with blocks on different drives.
434 * With this layout, and block is never stored twice on the one device.
436 * raid10_find_phys finds the sector offset of a given virtual sector
437 * on each device that it is on.
439 * raid10_find_virt does the reverse mapping, from a device and a
440 * sector offset to a virtual address
443 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
445 int n,f;
446 sector_t sector;
447 sector_t chunk;
448 sector_t stripe;
449 int dev;
451 int slot = 0;
453 /* now calculate first sector/dev */
454 chunk = r10bio->sector >> conf->chunk_shift;
455 sector = r10bio->sector & conf->chunk_mask;
457 chunk *= conf->near_copies;
458 stripe = chunk;
459 dev = sector_div(stripe, conf->raid_disks);
460 if (conf->far_offset)
461 stripe *= conf->far_copies;
463 sector += stripe << conf->chunk_shift;
465 /* and calculate all the others */
466 for (n=0; n < conf->near_copies; n++) {
467 int d = dev;
468 sector_t s = sector;
469 r10bio->devs[slot].addr = sector;
470 r10bio->devs[slot].devnum = d;
471 slot++;
473 for (f = 1; f < conf->far_copies; f++) {
474 d += conf->near_copies;
475 if (d >= conf->raid_disks)
476 d -= conf->raid_disks;
477 s += conf->stride;
478 r10bio->devs[slot].devnum = d;
479 r10bio->devs[slot].addr = s;
480 slot++;
482 dev++;
483 if (dev >= conf->raid_disks) {
484 dev = 0;
485 sector += (conf->chunk_mask + 1);
488 BUG_ON(slot != conf->copies);
491 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
493 sector_t offset, chunk, vchunk;
495 offset = sector & conf->chunk_mask;
496 if (conf->far_offset) {
497 int fc;
498 chunk = sector >> conf->chunk_shift;
499 fc = sector_div(chunk, conf->far_copies);
500 dev -= fc * conf->near_copies;
501 if (dev < 0)
502 dev += conf->raid_disks;
503 } else {
504 while (sector >= conf->stride) {
505 sector -= conf->stride;
506 if (dev < conf->near_copies)
507 dev += conf->raid_disks - conf->near_copies;
508 else
509 dev -= conf->near_copies;
511 chunk = sector >> conf->chunk_shift;
513 vchunk = chunk * conf->raid_disks + dev;
514 sector_div(vchunk, conf->near_copies);
515 return (vchunk << conf->chunk_shift) + offset;
519 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
520 * @q: request queue
521 * @bvm: properties of new bio
522 * @biovec: the request that could be merged to it.
524 * Return amount of bytes we can accept at this offset
525 * If near_copies == raid_disk, there are no striping issues,
526 * but in that case, the function isn't called at all.
528 static int raid10_mergeable_bvec(struct request_queue *q,
529 struct bvec_merge_data *bvm,
530 struct bio_vec *biovec)
532 struct mddev *mddev = q->queuedata;
533 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
534 int max;
535 unsigned int chunk_sectors = mddev->chunk_sectors;
536 unsigned int bio_sectors = bvm->bi_size >> 9;
538 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
539 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
540 if (max <= biovec->bv_len && bio_sectors == 0)
541 return biovec->bv_len;
542 else
543 return max;
547 * This routine returns the disk from which the requested read should
548 * be done. There is a per-array 'next expected sequential IO' sector
549 * number - if this matches on the next IO then we use the last disk.
550 * There is also a per-disk 'last know head position' sector that is
551 * maintained from IRQ contexts, both the normal and the resync IO
552 * completion handlers update this position correctly. If there is no
553 * perfect sequential match then we pick the disk whose head is closest.
555 * If there are 2 mirrors in the same 2 devices, performance degrades
556 * because position is mirror, not device based.
558 * The rdev for the device selected will have nr_pending incremented.
562 * FIXME: possibly should rethink readbalancing and do it differently
563 * depending on near_copies / far_copies geometry.
565 static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_sectors)
567 const sector_t this_sector = r10_bio->sector;
568 int disk, slot;
569 int sectors = r10_bio->sectors;
570 int best_good_sectors;
571 sector_t new_distance, best_dist;
572 struct md_rdev *rdev;
573 int do_balance;
574 int best_slot;
576 raid10_find_phys(conf, r10_bio);
577 rcu_read_lock();
578 retry:
579 sectors = r10_bio->sectors;
580 best_slot = -1;
581 best_dist = MaxSector;
582 best_good_sectors = 0;
583 do_balance = 1;
585 * Check if we can balance. We can balance on the whole
586 * device if no resync is going on (recovery is ok), or below
587 * the resync window. We take the first readable disk when
588 * above the resync window.
590 if (conf->mddev->recovery_cp < MaxSector
591 && (this_sector + sectors >= conf->next_resync))
592 do_balance = 0;
594 for (slot = 0; slot < conf->copies ; slot++) {
595 sector_t first_bad;
596 int bad_sectors;
597 sector_t dev_sector;
599 if (r10_bio->devs[slot].bio == IO_BLOCKED)
600 continue;
601 disk = r10_bio->devs[slot].devnum;
602 rdev = rcu_dereference(conf->mirrors[disk].rdev);
603 if (rdev == NULL)
604 continue;
605 if (!test_bit(In_sync, &rdev->flags))
606 continue;
608 dev_sector = r10_bio->devs[slot].addr;
609 if (is_badblock(rdev, dev_sector, sectors,
610 &first_bad, &bad_sectors)) {
611 if (best_dist < MaxSector)
612 /* Already have a better slot */
613 continue;
614 if (first_bad <= dev_sector) {
615 /* Cannot read here. If this is the
616 * 'primary' device, then we must not read
617 * beyond 'bad_sectors' from another device.
619 bad_sectors -= (dev_sector - first_bad);
620 if (!do_balance && sectors > bad_sectors)
621 sectors = bad_sectors;
622 if (best_good_sectors > sectors)
623 best_good_sectors = sectors;
624 } else {
625 sector_t good_sectors =
626 first_bad - dev_sector;
627 if (good_sectors > best_good_sectors) {
628 best_good_sectors = good_sectors;
629 best_slot = slot;
631 if (!do_balance)
632 /* Must read from here */
633 break;
635 continue;
636 } else
637 best_good_sectors = sectors;
639 if (!do_balance)
640 break;
642 /* This optimisation is debatable, and completely destroys
643 * sequential read speed for 'far copies' arrays. So only
644 * keep it for 'near' arrays, and review those later.
646 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
647 break;
649 /* for far > 1 always use the lowest address */
650 if (conf->far_copies > 1)
651 new_distance = r10_bio->devs[slot].addr;
652 else
653 new_distance = abs(r10_bio->devs[slot].addr -
654 conf->mirrors[disk].head_position);
655 if (new_distance < best_dist) {
656 best_dist = new_distance;
657 best_slot = slot;
660 if (slot == conf->copies)
661 slot = best_slot;
663 if (slot >= 0) {
664 disk = r10_bio->devs[slot].devnum;
665 rdev = rcu_dereference(conf->mirrors[disk].rdev);
666 if (!rdev)
667 goto retry;
668 atomic_inc(&rdev->nr_pending);
669 if (test_bit(Faulty, &rdev->flags)) {
670 /* Cannot risk returning a device that failed
671 * before we inc'ed nr_pending
673 rdev_dec_pending(rdev, conf->mddev);
674 goto retry;
676 r10_bio->read_slot = slot;
677 } else
678 disk = -1;
679 rcu_read_unlock();
680 *max_sectors = best_good_sectors;
682 return disk;
685 static int raid10_congested(void *data, int bits)
687 struct mddev *mddev = data;
688 struct r10conf *conf = mddev->private;
689 int i, ret = 0;
691 if ((bits & (1 << BDI_async_congested)) &&
692 conf->pending_count >= max_queued_requests)
693 return 1;
695 if (mddev_congested(mddev, bits))
696 return 1;
697 rcu_read_lock();
698 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
699 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
700 if (rdev && !test_bit(Faulty, &rdev->flags)) {
701 struct request_queue *q = bdev_get_queue(rdev->bdev);
703 ret |= bdi_congested(&q->backing_dev_info, bits);
706 rcu_read_unlock();
707 return ret;
710 static void flush_pending_writes(struct r10conf *conf)
712 /* Any writes that have been queued but are awaiting
713 * bitmap updates get flushed here.
715 spin_lock_irq(&conf->device_lock);
717 if (conf->pending_bio_list.head) {
718 struct bio *bio;
719 bio = bio_list_get(&conf->pending_bio_list);
720 conf->pending_count = 0;
721 spin_unlock_irq(&conf->device_lock);
722 /* flush any pending bitmap writes to disk
723 * before proceeding w/ I/O */
724 bitmap_unplug(conf->mddev->bitmap);
725 wake_up(&conf->wait_barrier);
727 while (bio) { /* submit pending writes */
728 struct bio *next = bio->bi_next;
729 bio->bi_next = NULL;
730 generic_make_request(bio);
731 bio = next;
733 } else
734 spin_unlock_irq(&conf->device_lock);
737 /* Barriers....
738 * Sometimes we need to suspend IO while we do something else,
739 * either some resync/recovery, or reconfigure the array.
740 * To do this we raise a 'barrier'.
741 * The 'barrier' is a counter that can be raised multiple times
742 * to count how many activities are happening which preclude
743 * normal IO.
744 * We can only raise the barrier if there is no pending IO.
745 * i.e. if nr_pending == 0.
746 * We choose only to raise the barrier if no-one is waiting for the
747 * barrier to go down. This means that as soon as an IO request
748 * is ready, no other operations which require a barrier will start
749 * until the IO request has had a chance.
751 * So: regular IO calls 'wait_barrier'. When that returns there
752 * is no backgroup IO happening, It must arrange to call
753 * allow_barrier when it has finished its IO.
754 * backgroup IO calls must call raise_barrier. Once that returns
755 * there is no normal IO happeing. It must arrange to call
756 * lower_barrier when the particular background IO completes.
759 static void raise_barrier(struct r10conf *conf, int force)
761 BUG_ON(force && !conf->barrier);
762 spin_lock_irq(&conf->resync_lock);
764 /* Wait until no block IO is waiting (unless 'force') */
765 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
766 conf->resync_lock, );
768 /* block any new IO from starting */
769 conf->barrier++;
771 /* Now wait for all pending IO to complete */
772 wait_event_lock_irq(conf->wait_barrier,
773 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
774 conf->resync_lock, );
776 spin_unlock_irq(&conf->resync_lock);
779 static void lower_barrier(struct r10conf *conf)
781 unsigned long flags;
782 spin_lock_irqsave(&conf->resync_lock, flags);
783 conf->barrier--;
784 spin_unlock_irqrestore(&conf->resync_lock, flags);
785 wake_up(&conf->wait_barrier);
788 static void wait_barrier(struct r10conf *conf)
790 spin_lock_irq(&conf->resync_lock);
791 if (conf->barrier) {
792 conf->nr_waiting++;
793 /* Wait for the barrier to drop.
794 * However if there are already pending
795 * requests (preventing the barrier from
796 * rising completely), and the
797 * pre-process bio queue isn't empty,
798 * then don't wait, as we need to empty
799 * that queue to get the nr_pending
800 * count down.
802 wait_event_lock_irq(conf->wait_barrier,
803 !conf->barrier ||
804 (conf->nr_pending &&
805 current->bio_list &&
806 !bio_list_empty(current->bio_list)),
807 conf->resync_lock,
809 conf->nr_waiting--;
811 conf->nr_pending++;
812 spin_unlock_irq(&conf->resync_lock);
815 static void allow_barrier(struct r10conf *conf)
817 unsigned long flags;
818 spin_lock_irqsave(&conf->resync_lock, flags);
819 conf->nr_pending--;
820 spin_unlock_irqrestore(&conf->resync_lock, flags);
821 wake_up(&conf->wait_barrier);
824 static void freeze_array(struct r10conf *conf)
826 /* stop syncio and normal IO and wait for everything to
827 * go quiet.
828 * We increment barrier and nr_waiting, and then
829 * wait until nr_pending match nr_queued+1
830 * This is called in the context of one normal IO request
831 * that has failed. Thus any sync request that might be pending
832 * will be blocked by nr_pending, and we need to wait for
833 * pending IO requests to complete or be queued for re-try.
834 * Thus the number queued (nr_queued) plus this request (1)
835 * must match the number of pending IOs (nr_pending) before
836 * we continue.
838 spin_lock_irq(&conf->resync_lock);
839 conf->barrier++;
840 conf->nr_waiting++;
841 wait_event_lock_irq(conf->wait_barrier,
842 conf->nr_pending == conf->nr_queued+1,
843 conf->resync_lock,
844 flush_pending_writes(conf));
846 spin_unlock_irq(&conf->resync_lock);
849 static void unfreeze_array(struct r10conf *conf)
851 /* reverse the effect of the freeze */
852 spin_lock_irq(&conf->resync_lock);
853 conf->barrier--;
854 conf->nr_waiting--;
855 wake_up(&conf->wait_barrier);
856 spin_unlock_irq(&conf->resync_lock);
859 static void make_request(struct mddev *mddev, struct bio * bio)
861 struct r10conf *conf = mddev->private;
862 struct mirror_info *mirror;
863 struct r10bio *r10_bio;
864 struct bio *read_bio;
865 int i;
866 int chunk_sects = conf->chunk_mask + 1;
867 const int rw = bio_data_dir(bio);
868 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
869 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
870 unsigned long flags;
871 struct md_rdev *blocked_rdev;
872 int plugged;
873 int sectors_handled;
874 int max_sectors;
876 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
877 md_flush_request(mddev, bio);
878 return;
881 /* If this request crosses a chunk boundary, we need to
882 * split it. This will only happen for 1 PAGE (or less) requests.
884 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
885 > chunk_sects &&
886 conf->near_copies < conf->raid_disks)) {
887 struct bio_pair *bp;
888 /* Sanity check -- queue functions should prevent this happening */
889 if (bio->bi_vcnt != 1 ||
890 bio->bi_idx != 0)
891 goto bad_map;
892 /* This is a one page bio that upper layers
893 * refuse to split for us, so we need to split it.
895 bp = bio_split(bio,
896 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
898 /* Each of these 'make_request' calls will call 'wait_barrier'.
899 * If the first succeeds but the second blocks due to the resync
900 * thread raising the barrier, we will deadlock because the
901 * IO to the underlying device will be queued in generic_make_request
902 * and will never complete, so will never reduce nr_pending.
903 * So increment nr_waiting here so no new raise_barriers will
904 * succeed, and so the second wait_barrier cannot block.
906 spin_lock_irq(&conf->resync_lock);
907 conf->nr_waiting++;
908 spin_unlock_irq(&conf->resync_lock);
910 make_request(mddev, &bp->bio1);
911 make_request(mddev, &bp->bio2);
913 spin_lock_irq(&conf->resync_lock);
914 conf->nr_waiting--;
915 wake_up(&conf->wait_barrier);
916 spin_unlock_irq(&conf->resync_lock);
918 bio_pair_release(bp);
919 return;
920 bad_map:
921 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
922 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
923 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
925 bio_io_error(bio);
926 return;
929 md_write_start(mddev, bio);
932 * Register the new request and wait if the reconstruction
933 * thread has put up a bar for new requests.
934 * Continue immediately if no resync is active currently.
936 wait_barrier(conf);
938 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
940 r10_bio->master_bio = bio;
941 r10_bio->sectors = bio->bi_size >> 9;
943 r10_bio->mddev = mddev;
944 r10_bio->sector = bio->bi_sector;
945 r10_bio->state = 0;
947 /* We might need to issue multiple reads to different
948 * devices if there are bad blocks around, so we keep
949 * track of the number of reads in bio->bi_phys_segments.
950 * If this is 0, there is only one r10_bio and no locking
951 * will be needed when the request completes. If it is
952 * non-zero, then it is the number of not-completed requests.
954 bio->bi_phys_segments = 0;
955 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
957 if (rw == READ) {
959 * read balancing logic:
961 int disk;
962 int slot;
964 read_again:
965 disk = read_balance(conf, r10_bio, &max_sectors);
966 slot = r10_bio->read_slot;
967 if (disk < 0) {
968 raid_end_bio_io(r10_bio);
969 return;
971 mirror = conf->mirrors + disk;
973 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
974 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
975 max_sectors);
977 r10_bio->devs[slot].bio = read_bio;
979 read_bio->bi_sector = r10_bio->devs[slot].addr +
980 mirror->rdev->data_offset;
981 read_bio->bi_bdev = mirror->rdev->bdev;
982 read_bio->bi_end_io = raid10_end_read_request;
983 read_bio->bi_rw = READ | do_sync;
984 read_bio->bi_private = r10_bio;
986 if (max_sectors < r10_bio->sectors) {
987 /* Could not read all from this device, so we will
988 * need another r10_bio.
990 sectors_handled = (r10_bio->sectors + max_sectors
991 - bio->bi_sector);
992 r10_bio->sectors = max_sectors;
993 spin_lock_irq(&conf->device_lock);
994 if (bio->bi_phys_segments == 0)
995 bio->bi_phys_segments = 2;
996 else
997 bio->bi_phys_segments++;
998 spin_unlock(&conf->device_lock);
999 /* Cannot call generic_make_request directly
1000 * as that will be queued in __generic_make_request
1001 * and subsequent mempool_alloc might block
1002 * waiting for it. so hand bio over to raid10d.
1004 reschedule_retry(r10_bio);
1006 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1008 r10_bio->master_bio = bio;
1009 r10_bio->sectors = ((bio->bi_size >> 9)
1010 - sectors_handled);
1011 r10_bio->state = 0;
1012 r10_bio->mddev = mddev;
1013 r10_bio->sector = bio->bi_sector + sectors_handled;
1014 goto read_again;
1015 } else
1016 generic_make_request(read_bio);
1017 return;
1021 * WRITE:
1023 if (conf->pending_count >= max_queued_requests) {
1024 md_wakeup_thread(mddev->thread);
1025 wait_event(conf->wait_barrier,
1026 conf->pending_count < max_queued_requests);
1028 /* first select target devices under rcu_lock and
1029 * inc refcount on their rdev. Record them by setting
1030 * bios[x] to bio
1031 * If there are known/acknowledged bad blocks on any device
1032 * on which we have seen a write error, we want to avoid
1033 * writing to those blocks. This potentially requires several
1034 * writes to write around the bad blocks. Each set of writes
1035 * gets its own r10_bio with a set of bios attached. The number
1036 * of r10_bios is recored in bio->bi_phys_segments just as with
1037 * the read case.
1039 plugged = mddev_check_plugged(mddev);
1041 raid10_find_phys(conf, r10_bio);
1042 retry_write:
1043 blocked_rdev = NULL;
1044 rcu_read_lock();
1045 max_sectors = r10_bio->sectors;
1047 for (i = 0; i < conf->copies; i++) {
1048 int d = r10_bio->devs[i].devnum;
1049 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1050 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1051 atomic_inc(&rdev->nr_pending);
1052 blocked_rdev = rdev;
1053 break;
1055 r10_bio->devs[i].bio = NULL;
1056 if (!rdev || test_bit(Faulty, &rdev->flags)) {
1057 set_bit(R10BIO_Degraded, &r10_bio->state);
1058 continue;
1060 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1061 sector_t first_bad;
1062 sector_t dev_sector = r10_bio->devs[i].addr;
1063 int bad_sectors;
1064 int is_bad;
1066 is_bad = is_badblock(rdev, dev_sector,
1067 max_sectors,
1068 &first_bad, &bad_sectors);
1069 if (is_bad < 0) {
1070 /* Mustn't write here until the bad block
1071 * is acknowledged
1073 atomic_inc(&rdev->nr_pending);
1074 set_bit(BlockedBadBlocks, &rdev->flags);
1075 blocked_rdev = rdev;
1076 break;
1078 if (is_bad && first_bad <= dev_sector) {
1079 /* Cannot write here at all */
1080 bad_sectors -= (dev_sector - first_bad);
1081 if (bad_sectors < max_sectors)
1082 /* Mustn't write more than bad_sectors
1083 * to other devices yet
1085 max_sectors = bad_sectors;
1086 /* We don't set R10BIO_Degraded as that
1087 * only applies if the disk is missing,
1088 * so it might be re-added, and we want to
1089 * know to recover this chunk.
1090 * In this case the device is here, and the
1091 * fact that this chunk is not in-sync is
1092 * recorded in the bad block log.
1094 continue;
1096 if (is_bad) {
1097 int good_sectors = first_bad - dev_sector;
1098 if (good_sectors < max_sectors)
1099 max_sectors = good_sectors;
1102 r10_bio->devs[i].bio = bio;
1103 atomic_inc(&rdev->nr_pending);
1105 rcu_read_unlock();
1107 if (unlikely(blocked_rdev)) {
1108 /* Have to wait for this device to get unblocked, then retry */
1109 int j;
1110 int d;
1112 for (j = 0; j < i; j++)
1113 if (r10_bio->devs[j].bio) {
1114 d = r10_bio->devs[j].devnum;
1115 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1117 allow_barrier(conf);
1118 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1119 wait_barrier(conf);
1120 goto retry_write;
1123 if (max_sectors < r10_bio->sectors) {
1124 /* We are splitting this into multiple parts, so
1125 * we need to prepare for allocating another r10_bio.
1127 r10_bio->sectors = max_sectors;
1128 spin_lock_irq(&conf->device_lock);
1129 if (bio->bi_phys_segments == 0)
1130 bio->bi_phys_segments = 2;
1131 else
1132 bio->bi_phys_segments++;
1133 spin_unlock_irq(&conf->device_lock);
1135 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1137 atomic_set(&r10_bio->remaining, 1);
1138 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1140 for (i = 0; i < conf->copies; i++) {
1141 struct bio *mbio;
1142 int d = r10_bio->devs[i].devnum;
1143 if (!r10_bio->devs[i].bio)
1144 continue;
1146 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1147 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1148 max_sectors);
1149 r10_bio->devs[i].bio = mbio;
1151 mbio->bi_sector = (r10_bio->devs[i].addr+
1152 conf->mirrors[d].rdev->data_offset);
1153 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1154 mbio->bi_end_io = raid10_end_write_request;
1155 mbio->bi_rw = WRITE | do_sync | do_fua;
1156 mbio->bi_private = r10_bio;
1158 atomic_inc(&r10_bio->remaining);
1159 spin_lock_irqsave(&conf->device_lock, flags);
1160 bio_list_add(&conf->pending_bio_list, mbio);
1161 conf->pending_count++;
1162 spin_unlock_irqrestore(&conf->device_lock, flags);
1165 /* Don't remove the bias on 'remaining' (one_write_done) until
1166 * after checking if we need to go around again.
1169 if (sectors_handled < (bio->bi_size >> 9)) {
1170 one_write_done(r10_bio);
1171 /* We need another r10_bio. It has already been counted
1172 * in bio->bi_phys_segments.
1174 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1176 r10_bio->master_bio = bio;
1177 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1179 r10_bio->mddev = mddev;
1180 r10_bio->sector = bio->bi_sector + sectors_handled;
1181 r10_bio->state = 0;
1182 goto retry_write;
1184 one_write_done(r10_bio);
1186 /* In case raid10d snuck in to freeze_array */
1187 wake_up(&conf->wait_barrier);
1189 if (do_sync || !mddev->bitmap || !plugged)
1190 md_wakeup_thread(mddev->thread);
1193 static void status(struct seq_file *seq, struct mddev *mddev)
1195 struct r10conf *conf = mddev->private;
1196 int i;
1198 if (conf->near_copies < conf->raid_disks)
1199 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1200 if (conf->near_copies > 1)
1201 seq_printf(seq, " %d near-copies", conf->near_copies);
1202 if (conf->far_copies > 1) {
1203 if (conf->far_offset)
1204 seq_printf(seq, " %d offset-copies", conf->far_copies);
1205 else
1206 seq_printf(seq, " %d far-copies", conf->far_copies);
1208 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1209 conf->raid_disks - mddev->degraded);
1210 for (i = 0; i < conf->raid_disks; i++)
1211 seq_printf(seq, "%s",
1212 conf->mirrors[i].rdev &&
1213 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1214 seq_printf(seq, "]");
1217 /* check if there are enough drives for
1218 * every block to appear on atleast one.
1219 * Don't consider the device numbered 'ignore'
1220 * as we might be about to remove it.
1222 static int enough(struct r10conf *conf, int ignore)
1224 int first = 0;
1226 do {
1227 int n = conf->copies;
1228 int cnt = 0;
1229 while (n--) {
1230 if (conf->mirrors[first].rdev &&
1231 first != ignore)
1232 cnt++;
1233 first = (first+1) % conf->raid_disks;
1235 if (cnt == 0)
1236 return 0;
1237 } while (first != 0);
1238 return 1;
1241 static void error(struct mddev *mddev, struct md_rdev *rdev)
1243 char b[BDEVNAME_SIZE];
1244 struct r10conf *conf = mddev->private;
1247 * If it is not operational, then we have already marked it as dead
1248 * else if it is the last working disks, ignore the error, let the
1249 * next level up know.
1250 * else mark the drive as failed
1252 if (test_bit(In_sync, &rdev->flags)
1253 && !enough(conf, rdev->raid_disk))
1255 * Don't fail the drive, just return an IO error.
1257 return;
1258 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1259 unsigned long flags;
1260 spin_lock_irqsave(&conf->device_lock, flags);
1261 mddev->degraded++;
1262 spin_unlock_irqrestore(&conf->device_lock, flags);
1264 * if recovery is running, make sure it aborts.
1266 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1268 set_bit(Blocked, &rdev->flags);
1269 set_bit(Faulty, &rdev->flags);
1270 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1271 printk(KERN_ALERT
1272 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1273 "md/raid10:%s: Operation continuing on %d devices.\n",
1274 mdname(mddev), bdevname(rdev->bdev, b),
1275 mdname(mddev), conf->raid_disks - mddev->degraded);
1278 static void print_conf(struct r10conf *conf)
1280 int i;
1281 struct mirror_info *tmp;
1283 printk(KERN_DEBUG "RAID10 conf printout:\n");
1284 if (!conf) {
1285 printk(KERN_DEBUG "(!conf)\n");
1286 return;
1288 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1289 conf->raid_disks);
1291 for (i = 0; i < conf->raid_disks; i++) {
1292 char b[BDEVNAME_SIZE];
1293 tmp = conf->mirrors + i;
1294 if (tmp->rdev)
1295 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1296 i, !test_bit(In_sync, &tmp->rdev->flags),
1297 !test_bit(Faulty, &tmp->rdev->flags),
1298 bdevname(tmp->rdev->bdev,b));
1302 static void close_sync(struct r10conf *conf)
1304 wait_barrier(conf);
1305 allow_barrier(conf);
1307 mempool_destroy(conf->r10buf_pool);
1308 conf->r10buf_pool = NULL;
1311 static int raid10_spare_active(struct mddev *mddev)
1313 int i;
1314 struct r10conf *conf = mddev->private;
1315 struct mirror_info *tmp;
1316 int count = 0;
1317 unsigned long flags;
1320 * Find all non-in_sync disks within the RAID10 configuration
1321 * and mark them in_sync
1323 for (i = 0; i < conf->raid_disks; i++) {
1324 tmp = conf->mirrors + i;
1325 if (tmp->rdev
1326 && !test_bit(Faulty, &tmp->rdev->flags)
1327 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1328 count++;
1329 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1332 spin_lock_irqsave(&conf->device_lock, flags);
1333 mddev->degraded -= count;
1334 spin_unlock_irqrestore(&conf->device_lock, flags);
1336 print_conf(conf);
1337 return count;
1341 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1343 struct r10conf *conf = mddev->private;
1344 int err = -EEXIST;
1345 int mirror;
1346 int first = 0;
1347 int last = conf->raid_disks - 1;
1349 if (mddev->recovery_cp < MaxSector)
1350 /* only hot-add to in-sync arrays, as recovery is
1351 * very different from resync
1353 return -EBUSY;
1354 if (!enough(conf, -1))
1355 return -EINVAL;
1357 if (rdev->raid_disk >= 0)
1358 first = last = rdev->raid_disk;
1360 if (rdev->saved_raid_disk >= first &&
1361 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1362 mirror = rdev->saved_raid_disk;
1363 else
1364 mirror = first;
1365 for ( ; mirror <= last ; mirror++) {
1366 struct mirror_info *p = &conf->mirrors[mirror];
1367 if (p->recovery_disabled == mddev->recovery_disabled)
1368 continue;
1369 if (p->rdev)
1370 continue;
1372 disk_stack_limits(mddev->gendisk, rdev->bdev,
1373 rdev->data_offset << 9);
1374 /* as we don't honour merge_bvec_fn, we must
1375 * never risk violating it, so limit
1376 * ->max_segments to one lying with a single
1377 * page, as a one page request is never in
1378 * violation.
1380 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1381 blk_queue_max_segments(mddev->queue, 1);
1382 blk_queue_segment_boundary(mddev->queue,
1383 PAGE_CACHE_SIZE - 1);
1386 p->head_position = 0;
1387 p->recovery_disabled = mddev->recovery_disabled - 1;
1388 rdev->raid_disk = mirror;
1389 err = 0;
1390 if (rdev->saved_raid_disk != mirror)
1391 conf->fullsync = 1;
1392 rcu_assign_pointer(p->rdev, rdev);
1393 break;
1396 md_integrity_add_rdev(rdev, mddev);
1397 print_conf(conf);
1398 return err;
1401 static int raid10_remove_disk(struct mddev *mddev, int number)
1403 struct r10conf *conf = mddev->private;
1404 int err = 0;
1405 struct md_rdev *rdev;
1406 struct mirror_info *p = conf->mirrors+ number;
1408 print_conf(conf);
1409 rdev = p->rdev;
1410 if (rdev) {
1411 if (test_bit(In_sync, &rdev->flags) ||
1412 atomic_read(&rdev->nr_pending)) {
1413 err = -EBUSY;
1414 goto abort;
1416 /* Only remove faulty devices in recovery
1417 * is not possible.
1419 if (!test_bit(Faulty, &rdev->flags) &&
1420 mddev->recovery_disabled != p->recovery_disabled &&
1421 enough(conf, -1)) {
1422 err = -EBUSY;
1423 goto abort;
1425 p->rdev = NULL;
1426 synchronize_rcu();
1427 if (atomic_read(&rdev->nr_pending)) {
1428 /* lost the race, try later */
1429 err = -EBUSY;
1430 p->rdev = rdev;
1431 goto abort;
1433 err = md_integrity_register(mddev);
1435 abort:
1437 print_conf(conf);
1438 return err;
1442 static void end_sync_read(struct bio *bio, int error)
1444 struct r10bio *r10_bio = bio->bi_private;
1445 struct r10conf *conf = r10_bio->mddev->private;
1446 int d;
1448 d = find_bio_disk(conf, r10_bio, bio, NULL);
1450 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1451 set_bit(R10BIO_Uptodate, &r10_bio->state);
1452 else
1453 /* The write handler will notice the lack of
1454 * R10BIO_Uptodate and record any errors etc
1456 atomic_add(r10_bio->sectors,
1457 &conf->mirrors[d].rdev->corrected_errors);
1459 /* for reconstruct, we always reschedule after a read.
1460 * for resync, only after all reads
1462 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1463 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1464 atomic_dec_and_test(&r10_bio->remaining)) {
1465 /* we have read all the blocks,
1466 * do the comparison in process context in raid10d
1468 reschedule_retry(r10_bio);
1472 static void end_sync_request(struct r10bio *r10_bio)
1474 struct mddev *mddev = r10_bio->mddev;
1476 while (atomic_dec_and_test(&r10_bio->remaining)) {
1477 if (r10_bio->master_bio == NULL) {
1478 /* the primary of several recovery bios */
1479 sector_t s = r10_bio->sectors;
1480 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1481 test_bit(R10BIO_WriteError, &r10_bio->state))
1482 reschedule_retry(r10_bio);
1483 else
1484 put_buf(r10_bio);
1485 md_done_sync(mddev, s, 1);
1486 break;
1487 } else {
1488 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1489 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1490 test_bit(R10BIO_WriteError, &r10_bio->state))
1491 reschedule_retry(r10_bio);
1492 else
1493 put_buf(r10_bio);
1494 r10_bio = r10_bio2;
1499 static void end_sync_write(struct bio *bio, int error)
1501 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1502 struct r10bio *r10_bio = bio->bi_private;
1503 struct mddev *mddev = r10_bio->mddev;
1504 struct r10conf *conf = mddev->private;
1505 int d;
1506 sector_t first_bad;
1507 int bad_sectors;
1508 int slot;
1510 d = find_bio_disk(conf, r10_bio, bio, &slot);
1512 if (!uptodate) {
1513 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
1514 set_bit(R10BIO_WriteError, &r10_bio->state);
1515 } else if (is_badblock(conf->mirrors[d].rdev,
1516 r10_bio->devs[slot].addr,
1517 r10_bio->sectors,
1518 &first_bad, &bad_sectors))
1519 set_bit(R10BIO_MadeGood, &r10_bio->state);
1521 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1523 end_sync_request(r10_bio);
1527 * Note: sync and recover and handled very differently for raid10
1528 * This code is for resync.
1529 * For resync, we read through virtual addresses and read all blocks.
1530 * If there is any error, we schedule a write. The lowest numbered
1531 * drive is authoritative.
1532 * However requests come for physical address, so we need to map.
1533 * For every physical address there are raid_disks/copies virtual addresses,
1534 * which is always are least one, but is not necessarly an integer.
1535 * This means that a physical address can span multiple chunks, so we may
1536 * have to submit multiple io requests for a single sync request.
1539 * We check if all blocks are in-sync and only write to blocks that
1540 * aren't in sync
1542 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1544 struct r10conf *conf = mddev->private;
1545 int i, first;
1546 struct bio *tbio, *fbio;
1548 atomic_set(&r10_bio->remaining, 1);
1550 /* find the first device with a block */
1551 for (i=0; i<conf->copies; i++)
1552 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1553 break;
1555 if (i == conf->copies)
1556 goto done;
1558 first = i;
1559 fbio = r10_bio->devs[i].bio;
1561 /* now find blocks with errors */
1562 for (i=0 ; i < conf->copies ; i++) {
1563 int j, d;
1564 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1566 tbio = r10_bio->devs[i].bio;
1568 if (tbio->bi_end_io != end_sync_read)
1569 continue;
1570 if (i == first)
1571 continue;
1572 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1573 /* We know that the bi_io_vec layout is the same for
1574 * both 'first' and 'i', so we just compare them.
1575 * All vec entries are PAGE_SIZE;
1577 for (j = 0; j < vcnt; j++)
1578 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1579 page_address(tbio->bi_io_vec[j].bv_page),
1580 PAGE_SIZE))
1581 break;
1582 if (j == vcnt)
1583 continue;
1584 mddev->resync_mismatches += r10_bio->sectors;
1585 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1586 /* Don't fix anything. */
1587 continue;
1589 /* Ok, we need to write this bio, either to correct an
1590 * inconsistency or to correct an unreadable block.
1591 * First we need to fixup bv_offset, bv_len and
1592 * bi_vecs, as the read request might have corrupted these
1594 tbio->bi_vcnt = vcnt;
1595 tbio->bi_size = r10_bio->sectors << 9;
1596 tbio->bi_idx = 0;
1597 tbio->bi_phys_segments = 0;
1598 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1599 tbio->bi_flags |= 1 << BIO_UPTODATE;
1600 tbio->bi_next = NULL;
1601 tbio->bi_rw = WRITE;
1602 tbio->bi_private = r10_bio;
1603 tbio->bi_sector = r10_bio->devs[i].addr;
1605 for (j=0; j < vcnt ; j++) {
1606 tbio->bi_io_vec[j].bv_offset = 0;
1607 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1609 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1610 page_address(fbio->bi_io_vec[j].bv_page),
1611 PAGE_SIZE);
1613 tbio->bi_end_io = end_sync_write;
1615 d = r10_bio->devs[i].devnum;
1616 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1617 atomic_inc(&r10_bio->remaining);
1618 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1620 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1621 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1622 generic_make_request(tbio);
1625 done:
1626 if (atomic_dec_and_test(&r10_bio->remaining)) {
1627 md_done_sync(mddev, r10_bio->sectors, 1);
1628 put_buf(r10_bio);
1633 * Now for the recovery code.
1634 * Recovery happens across physical sectors.
1635 * We recover all non-is_sync drives by finding the virtual address of
1636 * each, and then choose a working drive that also has that virt address.
1637 * There is a separate r10_bio for each non-in_sync drive.
1638 * Only the first two slots are in use. The first for reading,
1639 * The second for writing.
1642 static void fix_recovery_read_error(struct r10bio *r10_bio)
1644 /* We got a read error during recovery.
1645 * We repeat the read in smaller page-sized sections.
1646 * If a read succeeds, write it to the new device or record
1647 * a bad block if we cannot.
1648 * If a read fails, record a bad block on both old and
1649 * new devices.
1651 struct mddev *mddev = r10_bio->mddev;
1652 struct r10conf *conf = mddev->private;
1653 struct bio *bio = r10_bio->devs[0].bio;
1654 sector_t sect = 0;
1655 int sectors = r10_bio->sectors;
1656 int idx = 0;
1657 int dr = r10_bio->devs[0].devnum;
1658 int dw = r10_bio->devs[1].devnum;
1660 while (sectors) {
1661 int s = sectors;
1662 struct md_rdev *rdev;
1663 sector_t addr;
1664 int ok;
1666 if (s > (PAGE_SIZE>>9))
1667 s = PAGE_SIZE >> 9;
1669 rdev = conf->mirrors[dr].rdev;
1670 addr = r10_bio->devs[0].addr + sect,
1671 ok = sync_page_io(rdev,
1672 addr,
1673 s << 9,
1674 bio->bi_io_vec[idx].bv_page,
1675 READ, false);
1676 if (ok) {
1677 rdev = conf->mirrors[dw].rdev;
1678 addr = r10_bio->devs[1].addr + sect;
1679 ok = sync_page_io(rdev,
1680 addr,
1681 s << 9,
1682 bio->bi_io_vec[idx].bv_page,
1683 WRITE, false);
1684 if (!ok)
1685 set_bit(WriteErrorSeen, &rdev->flags);
1687 if (!ok) {
1688 /* We don't worry if we cannot set a bad block -
1689 * it really is bad so there is no loss in not
1690 * recording it yet
1692 rdev_set_badblocks(rdev, addr, s, 0);
1694 if (rdev != conf->mirrors[dw].rdev) {
1695 /* need bad block on destination too */
1696 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
1697 addr = r10_bio->devs[1].addr + sect;
1698 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1699 if (!ok) {
1700 /* just abort the recovery */
1701 printk(KERN_NOTICE
1702 "md/raid10:%s: recovery aborted"
1703 " due to read error\n",
1704 mdname(mddev));
1706 conf->mirrors[dw].recovery_disabled
1707 = mddev->recovery_disabled;
1708 set_bit(MD_RECOVERY_INTR,
1709 &mddev->recovery);
1710 break;
1715 sectors -= s;
1716 sect += s;
1717 idx++;
1721 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
1723 struct r10conf *conf = mddev->private;
1724 int d;
1725 struct bio *wbio;
1727 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1728 fix_recovery_read_error(r10_bio);
1729 end_sync_request(r10_bio);
1730 return;
1734 * share the pages with the first bio
1735 * and submit the write request
1737 wbio = r10_bio->devs[1].bio;
1738 d = r10_bio->devs[1].devnum;
1740 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1741 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1742 generic_make_request(wbio);
1747 * Used by fix_read_error() to decay the per rdev read_errors.
1748 * We halve the read error count for every hour that has elapsed
1749 * since the last recorded read error.
1752 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
1754 struct timespec cur_time_mon;
1755 unsigned long hours_since_last;
1756 unsigned int read_errors = atomic_read(&rdev->read_errors);
1758 ktime_get_ts(&cur_time_mon);
1760 if (rdev->last_read_error.tv_sec == 0 &&
1761 rdev->last_read_error.tv_nsec == 0) {
1762 /* first time we've seen a read error */
1763 rdev->last_read_error = cur_time_mon;
1764 return;
1767 hours_since_last = (cur_time_mon.tv_sec -
1768 rdev->last_read_error.tv_sec) / 3600;
1770 rdev->last_read_error = cur_time_mon;
1773 * if hours_since_last is > the number of bits in read_errors
1774 * just set read errors to 0. We do this to avoid
1775 * overflowing the shift of read_errors by hours_since_last.
1777 if (hours_since_last >= 8 * sizeof(read_errors))
1778 atomic_set(&rdev->read_errors, 0);
1779 else
1780 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1783 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
1784 int sectors, struct page *page, int rw)
1786 sector_t first_bad;
1787 int bad_sectors;
1789 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1790 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1791 return -1;
1792 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1793 /* success */
1794 return 1;
1795 if (rw == WRITE)
1796 set_bit(WriteErrorSeen, &rdev->flags);
1797 /* need to record an error - either for the block or the device */
1798 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1799 md_error(rdev->mddev, rdev);
1800 return 0;
1804 * This is a kernel thread which:
1806 * 1. Retries failed read operations on working mirrors.
1807 * 2. Updates the raid superblock when problems encounter.
1808 * 3. Performs writes following reads for array synchronising.
1811 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
1813 int sect = 0; /* Offset from r10_bio->sector */
1814 int sectors = r10_bio->sectors;
1815 struct md_rdev*rdev;
1816 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1817 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1819 /* still own a reference to this rdev, so it cannot
1820 * have been cleared recently.
1822 rdev = conf->mirrors[d].rdev;
1824 if (test_bit(Faulty, &rdev->flags))
1825 /* drive has already been failed, just ignore any
1826 more fix_read_error() attempts */
1827 return;
1829 check_decay_read_errors(mddev, rdev);
1830 atomic_inc(&rdev->read_errors);
1831 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1832 char b[BDEVNAME_SIZE];
1833 bdevname(rdev->bdev, b);
1835 printk(KERN_NOTICE
1836 "md/raid10:%s: %s: Raid device exceeded "
1837 "read_error threshold [cur %d:max %d]\n",
1838 mdname(mddev), b,
1839 atomic_read(&rdev->read_errors), max_read_errors);
1840 printk(KERN_NOTICE
1841 "md/raid10:%s: %s: Failing raid device\n",
1842 mdname(mddev), b);
1843 md_error(mddev, conf->mirrors[d].rdev);
1844 return;
1847 while(sectors) {
1848 int s = sectors;
1849 int sl = r10_bio->read_slot;
1850 int success = 0;
1851 int start;
1853 if (s > (PAGE_SIZE>>9))
1854 s = PAGE_SIZE >> 9;
1856 rcu_read_lock();
1857 do {
1858 sector_t first_bad;
1859 int bad_sectors;
1861 d = r10_bio->devs[sl].devnum;
1862 rdev = rcu_dereference(conf->mirrors[d].rdev);
1863 if (rdev &&
1864 test_bit(In_sync, &rdev->flags) &&
1865 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1866 &first_bad, &bad_sectors) == 0) {
1867 atomic_inc(&rdev->nr_pending);
1868 rcu_read_unlock();
1869 success = sync_page_io(rdev,
1870 r10_bio->devs[sl].addr +
1871 sect,
1872 s<<9,
1873 conf->tmppage, READ, false);
1874 rdev_dec_pending(rdev, mddev);
1875 rcu_read_lock();
1876 if (success)
1877 break;
1879 sl++;
1880 if (sl == conf->copies)
1881 sl = 0;
1882 } while (!success && sl != r10_bio->read_slot);
1883 rcu_read_unlock();
1885 if (!success) {
1886 /* Cannot read from anywhere, just mark the block
1887 * as bad on the first device to discourage future
1888 * reads.
1890 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1891 rdev = conf->mirrors[dn].rdev;
1893 if (!rdev_set_badblocks(
1894 rdev,
1895 r10_bio->devs[r10_bio->read_slot].addr
1896 + sect,
1897 s, 0))
1898 md_error(mddev, rdev);
1899 break;
1902 start = sl;
1903 /* write it back and re-read */
1904 rcu_read_lock();
1905 while (sl != r10_bio->read_slot) {
1906 char b[BDEVNAME_SIZE];
1908 if (sl==0)
1909 sl = conf->copies;
1910 sl--;
1911 d = r10_bio->devs[sl].devnum;
1912 rdev = rcu_dereference(conf->mirrors[d].rdev);
1913 if (!rdev ||
1914 !test_bit(In_sync, &rdev->flags))
1915 continue;
1917 atomic_inc(&rdev->nr_pending);
1918 rcu_read_unlock();
1919 if (r10_sync_page_io(rdev,
1920 r10_bio->devs[sl].addr +
1921 sect,
1922 s, conf->tmppage, WRITE)
1923 == 0) {
1924 /* Well, this device is dead */
1925 printk(KERN_NOTICE
1926 "md/raid10:%s: read correction "
1927 "write failed"
1928 " (%d sectors at %llu on %s)\n",
1929 mdname(mddev), s,
1930 (unsigned long long)(
1931 sect + rdev->data_offset),
1932 bdevname(rdev->bdev, b));
1933 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1934 "drive\n",
1935 mdname(mddev),
1936 bdevname(rdev->bdev, b));
1938 rdev_dec_pending(rdev, mddev);
1939 rcu_read_lock();
1941 sl = start;
1942 while (sl != r10_bio->read_slot) {
1943 char b[BDEVNAME_SIZE];
1945 if (sl==0)
1946 sl = conf->copies;
1947 sl--;
1948 d = r10_bio->devs[sl].devnum;
1949 rdev = rcu_dereference(conf->mirrors[d].rdev);
1950 if (!rdev ||
1951 !test_bit(In_sync, &rdev->flags))
1952 continue;
1954 atomic_inc(&rdev->nr_pending);
1955 rcu_read_unlock();
1956 switch (r10_sync_page_io(rdev,
1957 r10_bio->devs[sl].addr +
1958 sect,
1959 s, conf->tmppage,
1960 READ)) {
1961 case 0:
1962 /* Well, this device is dead */
1963 printk(KERN_NOTICE
1964 "md/raid10:%s: unable to read back "
1965 "corrected sectors"
1966 " (%d sectors at %llu on %s)\n",
1967 mdname(mddev), s,
1968 (unsigned long long)(
1969 sect + rdev->data_offset),
1970 bdevname(rdev->bdev, b));
1971 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1972 "drive\n",
1973 mdname(mddev),
1974 bdevname(rdev->bdev, b));
1975 break;
1976 case 1:
1977 printk(KERN_INFO
1978 "md/raid10:%s: read error corrected"
1979 " (%d sectors at %llu on %s)\n",
1980 mdname(mddev), s,
1981 (unsigned long long)(
1982 sect + rdev->data_offset),
1983 bdevname(rdev->bdev, b));
1984 atomic_add(s, &rdev->corrected_errors);
1987 rdev_dec_pending(rdev, mddev);
1988 rcu_read_lock();
1990 rcu_read_unlock();
1992 sectors -= s;
1993 sect += s;
1997 static void bi_complete(struct bio *bio, int error)
1999 complete((struct completion *)bio->bi_private);
2002 static int submit_bio_wait(int rw, struct bio *bio)
2004 struct completion event;
2005 rw |= REQ_SYNC;
2007 init_completion(&event);
2008 bio->bi_private = &event;
2009 bio->bi_end_io = bi_complete;
2010 submit_bio(rw, bio);
2011 wait_for_completion(&event);
2013 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2016 static int narrow_write_error(struct r10bio *r10_bio, int i)
2018 struct bio *bio = r10_bio->master_bio;
2019 struct mddev *mddev = r10_bio->mddev;
2020 struct r10conf *conf = mddev->private;
2021 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2022 /* bio has the data to be written to slot 'i' where
2023 * we just recently had a write error.
2024 * We repeatedly clone the bio and trim down to one block,
2025 * then try the write. Where the write fails we record
2026 * a bad block.
2027 * It is conceivable that the bio doesn't exactly align with
2028 * blocks. We must handle this.
2030 * We currently own a reference to the rdev.
2033 int block_sectors;
2034 sector_t sector;
2035 int sectors;
2036 int sect_to_write = r10_bio->sectors;
2037 int ok = 1;
2039 if (rdev->badblocks.shift < 0)
2040 return 0;
2042 block_sectors = 1 << rdev->badblocks.shift;
2043 sector = r10_bio->sector;
2044 sectors = ((r10_bio->sector + block_sectors)
2045 & ~(sector_t)(block_sectors - 1))
2046 - sector;
2048 while (sect_to_write) {
2049 struct bio *wbio;
2050 if (sectors > sect_to_write)
2051 sectors = sect_to_write;
2052 /* Write at 'sector' for 'sectors' */
2053 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2054 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2055 wbio->bi_sector = (r10_bio->devs[i].addr+
2056 rdev->data_offset+
2057 (sector - r10_bio->sector));
2058 wbio->bi_bdev = rdev->bdev;
2059 if (submit_bio_wait(WRITE, wbio) == 0)
2060 /* Failure! */
2061 ok = rdev_set_badblocks(rdev, sector,
2062 sectors, 0)
2063 && ok;
2065 bio_put(wbio);
2066 sect_to_write -= sectors;
2067 sector += sectors;
2068 sectors = block_sectors;
2070 return ok;
2073 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2075 int slot = r10_bio->read_slot;
2076 int mirror = r10_bio->devs[slot].devnum;
2077 struct bio *bio;
2078 struct r10conf *conf = mddev->private;
2079 struct md_rdev *rdev;
2080 char b[BDEVNAME_SIZE];
2081 unsigned long do_sync;
2082 int max_sectors;
2084 /* we got a read error. Maybe the drive is bad. Maybe just
2085 * the block and we can fix it.
2086 * We freeze all other IO, and try reading the block from
2087 * other devices. When we find one, we re-write
2088 * and check it that fixes the read error.
2089 * This is all done synchronously while the array is
2090 * frozen.
2092 if (mddev->ro == 0) {
2093 freeze_array(conf);
2094 fix_read_error(conf, mddev, r10_bio);
2095 unfreeze_array(conf);
2097 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
2099 bio = r10_bio->devs[slot].bio;
2100 bdevname(bio->bi_bdev, b);
2101 r10_bio->devs[slot].bio =
2102 mddev->ro ? IO_BLOCKED : NULL;
2103 read_more:
2104 mirror = read_balance(conf, r10_bio, &max_sectors);
2105 if (mirror == -1) {
2106 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2107 " read error for block %llu\n",
2108 mdname(mddev), b,
2109 (unsigned long long)r10_bio->sector);
2110 raid_end_bio_io(r10_bio);
2111 bio_put(bio);
2112 return;
2115 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
2116 if (bio)
2117 bio_put(bio);
2118 slot = r10_bio->read_slot;
2119 rdev = conf->mirrors[mirror].rdev;
2120 printk_ratelimited(
2121 KERN_ERR
2122 "md/raid10:%s: %s: redirecting "
2123 "sector %llu to another mirror\n",
2124 mdname(mddev),
2125 bdevname(rdev->bdev, b),
2126 (unsigned long long)r10_bio->sector);
2127 bio = bio_clone_mddev(r10_bio->master_bio,
2128 GFP_NOIO, mddev);
2129 md_trim_bio(bio,
2130 r10_bio->sector - bio->bi_sector,
2131 max_sectors);
2132 r10_bio->devs[slot].bio = bio;
2133 bio->bi_sector = r10_bio->devs[slot].addr
2134 + rdev->data_offset;
2135 bio->bi_bdev = rdev->bdev;
2136 bio->bi_rw = READ | do_sync;
2137 bio->bi_private = r10_bio;
2138 bio->bi_end_io = raid10_end_read_request;
2139 if (max_sectors < r10_bio->sectors) {
2140 /* Drat - have to split this up more */
2141 struct bio *mbio = r10_bio->master_bio;
2142 int sectors_handled =
2143 r10_bio->sector + max_sectors
2144 - mbio->bi_sector;
2145 r10_bio->sectors = max_sectors;
2146 spin_lock_irq(&conf->device_lock);
2147 if (mbio->bi_phys_segments == 0)
2148 mbio->bi_phys_segments = 2;
2149 else
2150 mbio->bi_phys_segments++;
2151 spin_unlock_irq(&conf->device_lock);
2152 generic_make_request(bio);
2153 bio = NULL;
2155 r10_bio = mempool_alloc(conf->r10bio_pool,
2156 GFP_NOIO);
2157 r10_bio->master_bio = mbio;
2158 r10_bio->sectors = (mbio->bi_size >> 9)
2159 - sectors_handled;
2160 r10_bio->state = 0;
2161 set_bit(R10BIO_ReadError,
2162 &r10_bio->state);
2163 r10_bio->mddev = mddev;
2164 r10_bio->sector = mbio->bi_sector
2165 + sectors_handled;
2167 goto read_more;
2168 } else
2169 generic_make_request(bio);
2172 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2174 /* Some sort of write request has finished and it
2175 * succeeded in writing where we thought there was a
2176 * bad block. So forget the bad block.
2177 * Or possibly if failed and we need to record
2178 * a bad block.
2180 int m;
2181 struct md_rdev *rdev;
2183 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2184 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2185 for (m = 0; m < conf->copies; m++) {
2186 int dev = r10_bio->devs[m].devnum;
2187 rdev = conf->mirrors[dev].rdev;
2188 if (r10_bio->devs[m].bio == NULL)
2189 continue;
2190 if (test_bit(BIO_UPTODATE,
2191 &r10_bio->devs[m].bio->bi_flags)) {
2192 rdev_clear_badblocks(
2193 rdev,
2194 r10_bio->devs[m].addr,
2195 r10_bio->sectors);
2196 } else {
2197 if (!rdev_set_badblocks(
2198 rdev,
2199 r10_bio->devs[m].addr,
2200 r10_bio->sectors, 0))
2201 md_error(conf->mddev, rdev);
2204 put_buf(r10_bio);
2205 } else {
2206 for (m = 0; m < conf->copies; m++) {
2207 int dev = r10_bio->devs[m].devnum;
2208 struct bio *bio = r10_bio->devs[m].bio;
2209 rdev = conf->mirrors[dev].rdev;
2210 if (bio == IO_MADE_GOOD) {
2211 rdev_clear_badblocks(
2212 rdev,
2213 r10_bio->devs[m].addr,
2214 r10_bio->sectors);
2215 rdev_dec_pending(rdev, conf->mddev);
2216 } else if (bio != NULL &&
2217 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2218 if (!narrow_write_error(r10_bio, m)) {
2219 md_error(conf->mddev, rdev);
2220 set_bit(R10BIO_Degraded,
2221 &r10_bio->state);
2223 rdev_dec_pending(rdev, conf->mddev);
2226 if (test_bit(R10BIO_WriteError,
2227 &r10_bio->state))
2228 close_write(r10_bio);
2229 raid_end_bio_io(r10_bio);
2233 static void raid10d(struct mddev *mddev)
2235 struct r10bio *r10_bio;
2236 unsigned long flags;
2237 struct r10conf *conf = mddev->private;
2238 struct list_head *head = &conf->retry_list;
2239 struct blk_plug plug;
2241 md_check_recovery(mddev);
2243 blk_start_plug(&plug);
2244 for (;;) {
2246 flush_pending_writes(conf);
2248 spin_lock_irqsave(&conf->device_lock, flags);
2249 if (list_empty(head)) {
2250 spin_unlock_irqrestore(&conf->device_lock, flags);
2251 break;
2253 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2254 list_del(head->prev);
2255 conf->nr_queued--;
2256 spin_unlock_irqrestore(&conf->device_lock, flags);
2258 mddev = r10_bio->mddev;
2259 conf = mddev->private;
2260 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2261 test_bit(R10BIO_WriteError, &r10_bio->state))
2262 handle_write_completed(conf, r10_bio);
2263 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2264 sync_request_write(mddev, r10_bio);
2265 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2266 recovery_request_write(mddev, r10_bio);
2267 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2268 handle_read_error(mddev, r10_bio);
2269 else {
2270 /* just a partial read to be scheduled from a
2271 * separate context
2273 int slot = r10_bio->read_slot;
2274 generic_make_request(r10_bio->devs[slot].bio);
2277 cond_resched();
2278 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2279 md_check_recovery(mddev);
2281 blk_finish_plug(&plug);
2285 static int init_resync(struct r10conf *conf)
2287 int buffs;
2289 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2290 BUG_ON(conf->r10buf_pool);
2291 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2292 if (!conf->r10buf_pool)
2293 return -ENOMEM;
2294 conf->next_resync = 0;
2295 return 0;
2299 * perform a "sync" on one "block"
2301 * We need to make sure that no normal I/O request - particularly write
2302 * requests - conflict with active sync requests.
2304 * This is achieved by tracking pending requests and a 'barrier' concept
2305 * that can be installed to exclude normal IO requests.
2307 * Resync and recovery are handled very differently.
2308 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2310 * For resync, we iterate over virtual addresses, read all copies,
2311 * and update if there are differences. If only one copy is live,
2312 * skip it.
2313 * For recovery, we iterate over physical addresses, read a good
2314 * value for each non-in_sync drive, and over-write.
2316 * So, for recovery we may have several outstanding complex requests for a
2317 * given address, one for each out-of-sync device. We model this by allocating
2318 * a number of r10_bio structures, one for each out-of-sync device.
2319 * As we setup these structures, we collect all bio's together into a list
2320 * which we then process collectively to add pages, and then process again
2321 * to pass to generic_make_request.
2323 * The r10_bio structures are linked using a borrowed master_bio pointer.
2324 * This link is counted in ->remaining. When the r10_bio that points to NULL
2325 * has its remaining count decremented to 0, the whole complex operation
2326 * is complete.
2330 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
2331 int *skipped, int go_faster)
2333 struct r10conf *conf = mddev->private;
2334 struct r10bio *r10_bio;
2335 struct bio *biolist = NULL, *bio;
2336 sector_t max_sector, nr_sectors;
2337 int i;
2338 int max_sync;
2339 sector_t sync_blocks;
2340 sector_t sectors_skipped = 0;
2341 int chunks_skipped = 0;
2343 if (!conf->r10buf_pool)
2344 if (init_resync(conf))
2345 return 0;
2347 skipped:
2348 max_sector = mddev->dev_sectors;
2349 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2350 max_sector = mddev->resync_max_sectors;
2351 if (sector_nr >= max_sector) {
2352 /* If we aborted, we need to abort the
2353 * sync on the 'current' bitmap chucks (there can
2354 * be several when recovering multiple devices).
2355 * as we may have started syncing it but not finished.
2356 * We can find the current address in
2357 * mddev->curr_resync, but for recovery,
2358 * we need to convert that to several
2359 * virtual addresses.
2361 if (mddev->curr_resync < max_sector) { /* aborted */
2362 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2363 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2364 &sync_blocks, 1);
2365 else for (i=0; i<conf->raid_disks; i++) {
2366 sector_t sect =
2367 raid10_find_virt(conf, mddev->curr_resync, i);
2368 bitmap_end_sync(mddev->bitmap, sect,
2369 &sync_blocks, 1);
2371 } else /* completed sync */
2372 conf->fullsync = 0;
2374 bitmap_close_sync(mddev->bitmap);
2375 close_sync(conf);
2376 *skipped = 1;
2377 return sectors_skipped;
2379 if (chunks_skipped >= conf->raid_disks) {
2380 /* if there has been nothing to do on any drive,
2381 * then there is nothing to do at all..
2383 *skipped = 1;
2384 return (max_sector - sector_nr) + sectors_skipped;
2387 if (max_sector > mddev->resync_max)
2388 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2390 /* make sure whole request will fit in a chunk - if chunks
2391 * are meaningful
2393 if (conf->near_copies < conf->raid_disks &&
2394 max_sector > (sector_nr | conf->chunk_mask))
2395 max_sector = (sector_nr | conf->chunk_mask) + 1;
2397 * If there is non-resync activity waiting for us then
2398 * put in a delay to throttle resync.
2400 if (!go_faster && conf->nr_waiting)
2401 msleep_interruptible(1000);
2403 /* Again, very different code for resync and recovery.
2404 * Both must result in an r10bio with a list of bios that
2405 * have bi_end_io, bi_sector, bi_bdev set,
2406 * and bi_private set to the r10bio.
2407 * For recovery, we may actually create several r10bios
2408 * with 2 bios in each, that correspond to the bios in the main one.
2409 * In this case, the subordinate r10bios link back through a
2410 * borrowed master_bio pointer, and the counter in the master
2411 * includes a ref from each subordinate.
2413 /* First, we decide what to do and set ->bi_end_io
2414 * To end_sync_read if we want to read, and
2415 * end_sync_write if we will want to write.
2418 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
2419 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2420 /* recovery... the complicated one */
2421 int j;
2422 r10_bio = NULL;
2424 for (i=0 ; i<conf->raid_disks; i++) {
2425 int still_degraded;
2426 struct r10bio *rb2;
2427 sector_t sect;
2428 int must_sync;
2429 int any_working;
2431 if (conf->mirrors[i].rdev == NULL ||
2432 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2433 continue;
2435 still_degraded = 0;
2436 /* want to reconstruct this device */
2437 rb2 = r10_bio;
2438 sect = raid10_find_virt(conf, sector_nr, i);
2439 if (sect >= mddev->resync_max_sectors) {
2440 /* last stripe is not complete - don't
2441 * try to recover this sector.
2443 continue;
2445 /* Unless we are doing a full sync, we only need
2446 * to recover the block if it is set in the bitmap
2448 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2449 &sync_blocks, 1);
2450 if (sync_blocks < max_sync)
2451 max_sync = sync_blocks;
2452 if (!must_sync &&
2453 !conf->fullsync) {
2454 /* yep, skip the sync_blocks here, but don't assume
2455 * that there will never be anything to do here
2457 chunks_skipped = -1;
2458 continue;
2461 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2462 raise_barrier(conf, rb2 != NULL);
2463 atomic_set(&r10_bio->remaining, 0);
2465 r10_bio->master_bio = (struct bio*)rb2;
2466 if (rb2)
2467 atomic_inc(&rb2->remaining);
2468 r10_bio->mddev = mddev;
2469 set_bit(R10BIO_IsRecover, &r10_bio->state);
2470 r10_bio->sector = sect;
2472 raid10_find_phys(conf, r10_bio);
2474 /* Need to check if the array will still be
2475 * degraded
2477 for (j=0; j<conf->raid_disks; j++)
2478 if (conf->mirrors[j].rdev == NULL ||
2479 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2480 still_degraded = 1;
2481 break;
2484 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2485 &sync_blocks, still_degraded);
2487 any_working = 0;
2488 for (j=0; j<conf->copies;j++) {
2489 int k;
2490 int d = r10_bio->devs[j].devnum;
2491 sector_t from_addr, to_addr;
2492 struct md_rdev *rdev;
2493 sector_t sector, first_bad;
2494 int bad_sectors;
2495 if (!conf->mirrors[d].rdev ||
2496 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2497 continue;
2498 /* This is where we read from */
2499 any_working = 1;
2500 rdev = conf->mirrors[d].rdev;
2501 sector = r10_bio->devs[j].addr;
2503 if (is_badblock(rdev, sector, max_sync,
2504 &first_bad, &bad_sectors)) {
2505 if (first_bad > sector)
2506 max_sync = first_bad - sector;
2507 else {
2508 bad_sectors -= (sector
2509 - first_bad);
2510 if (max_sync > bad_sectors)
2511 max_sync = bad_sectors;
2512 continue;
2515 bio = r10_bio->devs[0].bio;
2516 bio->bi_next = biolist;
2517 biolist = bio;
2518 bio->bi_private = r10_bio;
2519 bio->bi_end_io = end_sync_read;
2520 bio->bi_rw = READ;
2521 from_addr = r10_bio->devs[j].addr;
2522 bio->bi_sector = from_addr +
2523 conf->mirrors[d].rdev->data_offset;
2524 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2525 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2526 atomic_inc(&r10_bio->remaining);
2527 /* and we write to 'i' */
2529 for (k=0; k<conf->copies; k++)
2530 if (r10_bio->devs[k].devnum == i)
2531 break;
2532 BUG_ON(k == conf->copies);
2533 bio = r10_bio->devs[1].bio;
2534 bio->bi_next = biolist;
2535 biolist = bio;
2536 bio->bi_private = r10_bio;
2537 bio->bi_end_io = end_sync_write;
2538 bio->bi_rw = WRITE;
2539 to_addr = r10_bio->devs[k].addr;
2540 bio->bi_sector = to_addr +
2541 conf->mirrors[i].rdev->data_offset;
2542 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2544 r10_bio->devs[0].devnum = d;
2545 r10_bio->devs[0].addr = from_addr;
2546 r10_bio->devs[1].devnum = i;
2547 r10_bio->devs[1].addr = to_addr;
2549 break;
2551 if (j == conf->copies) {
2552 /* Cannot recover, so abort the recovery or
2553 * record a bad block */
2554 put_buf(r10_bio);
2555 if (rb2)
2556 atomic_dec(&rb2->remaining);
2557 r10_bio = rb2;
2558 if (any_working) {
2559 /* problem is that there are bad blocks
2560 * on other device(s)
2562 int k;
2563 for (k = 0; k < conf->copies; k++)
2564 if (r10_bio->devs[k].devnum == i)
2565 break;
2566 if (!rdev_set_badblocks(
2567 conf->mirrors[i].rdev,
2568 r10_bio->devs[k].addr,
2569 max_sync, 0))
2570 any_working = 0;
2572 if (!any_working) {
2573 if (!test_and_set_bit(MD_RECOVERY_INTR,
2574 &mddev->recovery))
2575 printk(KERN_INFO "md/raid10:%s: insufficient "
2576 "working devices for recovery.\n",
2577 mdname(mddev));
2578 conf->mirrors[i].recovery_disabled
2579 = mddev->recovery_disabled;
2581 break;
2584 if (biolist == NULL) {
2585 while (r10_bio) {
2586 struct r10bio *rb2 = r10_bio;
2587 r10_bio = (struct r10bio*) rb2->master_bio;
2588 rb2->master_bio = NULL;
2589 put_buf(rb2);
2591 goto giveup;
2593 } else {
2594 /* resync. Schedule a read for every block at this virt offset */
2595 int count = 0;
2597 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2599 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2600 &sync_blocks, mddev->degraded) &&
2601 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2602 &mddev->recovery)) {
2603 /* We can skip this block */
2604 *skipped = 1;
2605 return sync_blocks + sectors_skipped;
2607 if (sync_blocks < max_sync)
2608 max_sync = sync_blocks;
2609 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2611 r10_bio->mddev = mddev;
2612 atomic_set(&r10_bio->remaining, 0);
2613 raise_barrier(conf, 0);
2614 conf->next_resync = sector_nr;
2616 r10_bio->master_bio = NULL;
2617 r10_bio->sector = sector_nr;
2618 set_bit(R10BIO_IsSync, &r10_bio->state);
2619 raid10_find_phys(conf, r10_bio);
2620 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2622 for (i=0; i<conf->copies; i++) {
2623 int d = r10_bio->devs[i].devnum;
2624 sector_t first_bad, sector;
2625 int bad_sectors;
2627 bio = r10_bio->devs[i].bio;
2628 bio->bi_end_io = NULL;
2629 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2630 if (conf->mirrors[d].rdev == NULL ||
2631 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2632 continue;
2633 sector = r10_bio->devs[i].addr;
2634 if (is_badblock(conf->mirrors[d].rdev,
2635 sector, max_sync,
2636 &first_bad, &bad_sectors)) {
2637 if (first_bad > sector)
2638 max_sync = first_bad - sector;
2639 else {
2640 bad_sectors -= (sector - first_bad);
2641 if (max_sync > bad_sectors)
2642 max_sync = max_sync;
2643 continue;
2646 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2647 atomic_inc(&r10_bio->remaining);
2648 bio->bi_next = biolist;
2649 biolist = bio;
2650 bio->bi_private = r10_bio;
2651 bio->bi_end_io = end_sync_read;
2652 bio->bi_rw = READ;
2653 bio->bi_sector = sector +
2654 conf->mirrors[d].rdev->data_offset;
2655 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2656 count++;
2659 if (count < 2) {
2660 for (i=0; i<conf->copies; i++) {
2661 int d = r10_bio->devs[i].devnum;
2662 if (r10_bio->devs[i].bio->bi_end_io)
2663 rdev_dec_pending(conf->mirrors[d].rdev,
2664 mddev);
2666 put_buf(r10_bio);
2667 biolist = NULL;
2668 goto giveup;
2672 for (bio = biolist; bio ; bio=bio->bi_next) {
2674 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2675 if (bio->bi_end_io)
2676 bio->bi_flags |= 1 << BIO_UPTODATE;
2677 bio->bi_vcnt = 0;
2678 bio->bi_idx = 0;
2679 bio->bi_phys_segments = 0;
2680 bio->bi_size = 0;
2683 nr_sectors = 0;
2684 if (sector_nr + max_sync < max_sector)
2685 max_sector = sector_nr + max_sync;
2686 do {
2687 struct page *page;
2688 int len = PAGE_SIZE;
2689 if (sector_nr + (len>>9) > max_sector)
2690 len = (max_sector - sector_nr) << 9;
2691 if (len == 0)
2692 break;
2693 for (bio= biolist ; bio ; bio=bio->bi_next) {
2694 struct bio *bio2;
2695 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2696 if (bio_add_page(bio, page, len, 0))
2697 continue;
2699 /* stop here */
2700 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2701 for (bio2 = biolist;
2702 bio2 && bio2 != bio;
2703 bio2 = bio2->bi_next) {
2704 /* remove last page from this bio */
2705 bio2->bi_vcnt--;
2706 bio2->bi_size -= len;
2707 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2709 goto bio_full;
2711 nr_sectors += len>>9;
2712 sector_nr += len>>9;
2713 } while (biolist->bi_vcnt < RESYNC_PAGES);
2714 bio_full:
2715 r10_bio->sectors = nr_sectors;
2717 while (biolist) {
2718 bio = biolist;
2719 biolist = biolist->bi_next;
2721 bio->bi_next = NULL;
2722 r10_bio = bio->bi_private;
2723 r10_bio->sectors = nr_sectors;
2725 if (bio->bi_end_io == end_sync_read) {
2726 md_sync_acct(bio->bi_bdev, nr_sectors);
2727 generic_make_request(bio);
2731 if (sectors_skipped)
2732 /* pretend they weren't skipped, it makes
2733 * no important difference in this case
2735 md_done_sync(mddev, sectors_skipped, 1);
2737 return sectors_skipped + nr_sectors;
2738 giveup:
2739 /* There is nowhere to write, so all non-sync
2740 * drives must be failed or in resync, all drives
2741 * have a bad block, so try the next chunk...
2743 if (sector_nr + max_sync < max_sector)
2744 max_sector = sector_nr + max_sync;
2746 sectors_skipped += (max_sector - sector_nr);
2747 chunks_skipped ++;
2748 sector_nr = max_sector;
2749 goto skipped;
2752 static sector_t
2753 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2755 sector_t size;
2756 struct r10conf *conf = mddev->private;
2758 if (!raid_disks)
2759 raid_disks = conf->raid_disks;
2760 if (!sectors)
2761 sectors = conf->dev_sectors;
2763 size = sectors >> conf->chunk_shift;
2764 sector_div(size, conf->far_copies);
2765 size = size * raid_disks;
2766 sector_div(size, conf->near_copies);
2768 return size << conf->chunk_shift;
2772 static struct r10conf *setup_conf(struct mddev *mddev)
2774 struct r10conf *conf = NULL;
2775 int nc, fc, fo;
2776 sector_t stride, size;
2777 int err = -EINVAL;
2779 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2780 !is_power_of_2(mddev->new_chunk_sectors)) {
2781 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2782 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2783 mdname(mddev), PAGE_SIZE);
2784 goto out;
2787 nc = mddev->new_layout & 255;
2788 fc = (mddev->new_layout >> 8) & 255;
2789 fo = mddev->new_layout & (1<<16);
2791 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2792 (mddev->new_layout >> 17)) {
2793 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2794 mdname(mddev), mddev->new_layout);
2795 goto out;
2798 err = -ENOMEM;
2799 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
2800 if (!conf)
2801 goto out;
2803 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2804 GFP_KERNEL);
2805 if (!conf->mirrors)
2806 goto out;
2808 conf->tmppage = alloc_page(GFP_KERNEL);
2809 if (!conf->tmppage)
2810 goto out;
2813 conf->raid_disks = mddev->raid_disks;
2814 conf->near_copies = nc;
2815 conf->far_copies = fc;
2816 conf->copies = nc*fc;
2817 conf->far_offset = fo;
2818 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2819 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2821 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2822 r10bio_pool_free, conf);
2823 if (!conf->r10bio_pool)
2824 goto out;
2826 size = mddev->dev_sectors >> conf->chunk_shift;
2827 sector_div(size, fc);
2828 size = size * conf->raid_disks;
2829 sector_div(size, nc);
2830 /* 'size' is now the number of chunks in the array */
2831 /* calculate "used chunks per device" in 'stride' */
2832 stride = size * conf->copies;
2834 /* We need to round up when dividing by raid_disks to
2835 * get the stride size.
2837 stride += conf->raid_disks - 1;
2838 sector_div(stride, conf->raid_disks);
2840 conf->dev_sectors = stride << conf->chunk_shift;
2842 if (fo)
2843 stride = 1;
2844 else
2845 sector_div(stride, fc);
2846 conf->stride = stride << conf->chunk_shift;
2849 spin_lock_init(&conf->device_lock);
2850 INIT_LIST_HEAD(&conf->retry_list);
2852 spin_lock_init(&conf->resync_lock);
2853 init_waitqueue_head(&conf->wait_barrier);
2855 conf->thread = md_register_thread(raid10d, mddev, NULL);
2856 if (!conf->thread)
2857 goto out;
2859 conf->mddev = mddev;
2860 return conf;
2862 out:
2863 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
2864 mdname(mddev));
2865 if (conf) {
2866 if (conf->r10bio_pool)
2867 mempool_destroy(conf->r10bio_pool);
2868 kfree(conf->mirrors);
2869 safe_put_page(conf->tmppage);
2870 kfree(conf);
2872 return ERR_PTR(err);
2875 static int run(struct mddev *mddev)
2877 struct r10conf *conf;
2878 int i, disk_idx, chunk_size;
2879 struct mirror_info *disk;
2880 struct md_rdev *rdev;
2881 sector_t size;
2884 * copy the already verified devices into our private RAID10
2885 * bookkeeping area. [whatever we allocate in run(),
2886 * should be freed in stop()]
2889 if (mddev->private == NULL) {
2890 conf = setup_conf(mddev);
2891 if (IS_ERR(conf))
2892 return PTR_ERR(conf);
2893 mddev->private = conf;
2895 conf = mddev->private;
2896 if (!conf)
2897 goto out;
2899 mddev->thread = conf->thread;
2900 conf->thread = NULL;
2902 chunk_size = mddev->chunk_sectors << 9;
2903 blk_queue_io_min(mddev->queue, chunk_size);
2904 if (conf->raid_disks % conf->near_copies)
2905 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2906 else
2907 blk_queue_io_opt(mddev->queue, chunk_size *
2908 (conf->raid_disks / conf->near_copies));
2910 list_for_each_entry(rdev, &mddev->disks, same_set) {
2912 disk_idx = rdev->raid_disk;
2913 if (disk_idx >= conf->raid_disks
2914 || disk_idx < 0)
2915 continue;
2916 disk = conf->mirrors + disk_idx;
2918 disk->rdev = rdev;
2919 disk_stack_limits(mddev->gendisk, rdev->bdev,
2920 rdev->data_offset << 9);
2921 /* as we don't honour merge_bvec_fn, we must never risk
2922 * violating it, so limit max_segments to 1 lying
2923 * within a single page.
2925 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2926 blk_queue_max_segments(mddev->queue, 1);
2927 blk_queue_segment_boundary(mddev->queue,
2928 PAGE_CACHE_SIZE - 1);
2931 disk->head_position = 0;
2933 /* need to check that every block has at least one working mirror */
2934 if (!enough(conf, -1)) {
2935 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
2936 mdname(mddev));
2937 goto out_free_conf;
2940 mddev->degraded = 0;
2941 for (i = 0; i < conf->raid_disks; i++) {
2943 disk = conf->mirrors + i;
2945 if (!disk->rdev ||
2946 !test_bit(In_sync, &disk->rdev->flags)) {
2947 disk->head_position = 0;
2948 mddev->degraded++;
2949 if (disk->rdev)
2950 conf->fullsync = 1;
2952 disk->recovery_disabled = mddev->recovery_disabled - 1;
2955 if (mddev->recovery_cp != MaxSector)
2956 printk(KERN_NOTICE "md/raid10:%s: not clean"
2957 " -- starting background reconstruction\n",
2958 mdname(mddev));
2959 printk(KERN_INFO
2960 "md/raid10:%s: active with %d out of %d devices\n",
2961 mdname(mddev), conf->raid_disks - mddev->degraded,
2962 conf->raid_disks);
2964 * Ok, everything is just fine now
2966 mddev->dev_sectors = conf->dev_sectors;
2967 size = raid10_size(mddev, 0, 0);
2968 md_set_array_sectors(mddev, size);
2969 mddev->resync_max_sectors = size;
2971 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2972 mddev->queue->backing_dev_info.congested_data = mddev;
2974 /* Calculate max read-ahead size.
2975 * We need to readahead at least twice a whole stripe....
2976 * maybe...
2979 int stripe = conf->raid_disks *
2980 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2981 stripe /= conf->near_copies;
2982 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2983 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2986 if (conf->near_copies < conf->raid_disks)
2987 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2989 if (md_integrity_register(mddev))
2990 goto out_free_conf;
2992 return 0;
2994 out_free_conf:
2995 md_unregister_thread(&mddev->thread);
2996 if (conf->r10bio_pool)
2997 mempool_destroy(conf->r10bio_pool);
2998 safe_put_page(conf->tmppage);
2999 kfree(conf->mirrors);
3000 kfree(conf);
3001 mddev->private = NULL;
3002 out:
3003 return -EIO;
3006 static int stop(struct mddev *mddev)
3008 struct r10conf *conf = mddev->private;
3010 raise_barrier(conf, 0);
3011 lower_barrier(conf);
3013 md_unregister_thread(&mddev->thread);
3014 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3015 if (conf->r10bio_pool)
3016 mempool_destroy(conf->r10bio_pool);
3017 kfree(conf->mirrors);
3018 kfree(conf);
3019 mddev->private = NULL;
3020 return 0;
3023 static void raid10_quiesce(struct mddev *mddev, int state)
3025 struct r10conf *conf = mddev->private;
3027 switch(state) {
3028 case 1:
3029 raise_barrier(conf, 0);
3030 break;
3031 case 0:
3032 lower_barrier(conf);
3033 break;
3037 static void *raid10_takeover_raid0(struct mddev *mddev)
3039 struct md_rdev *rdev;
3040 struct r10conf *conf;
3042 if (mddev->degraded > 0) {
3043 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3044 mdname(mddev));
3045 return ERR_PTR(-EINVAL);
3048 /* Set new parameters */
3049 mddev->new_level = 10;
3050 /* new layout: far_copies = 1, near_copies = 2 */
3051 mddev->new_layout = (1<<8) + 2;
3052 mddev->new_chunk_sectors = mddev->chunk_sectors;
3053 mddev->delta_disks = mddev->raid_disks;
3054 mddev->raid_disks *= 2;
3055 /* make sure it will be not marked as dirty */
3056 mddev->recovery_cp = MaxSector;
3058 conf = setup_conf(mddev);
3059 if (!IS_ERR(conf)) {
3060 list_for_each_entry(rdev, &mddev->disks, same_set)
3061 if (rdev->raid_disk >= 0)
3062 rdev->new_raid_disk = rdev->raid_disk * 2;
3063 conf->barrier = 1;
3066 return conf;
3069 static void *raid10_takeover(struct mddev *mddev)
3071 struct r0conf *raid0_conf;
3073 /* raid10 can take over:
3074 * raid0 - providing it has only two drives
3076 if (mddev->level == 0) {
3077 /* for raid0 takeover only one zone is supported */
3078 raid0_conf = mddev->private;
3079 if (raid0_conf->nr_strip_zones > 1) {
3080 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3081 " with more than one zone.\n",
3082 mdname(mddev));
3083 return ERR_PTR(-EINVAL);
3085 return raid10_takeover_raid0(mddev);
3087 return ERR_PTR(-EINVAL);
3090 static struct md_personality raid10_personality =
3092 .name = "raid10",
3093 .level = 10,
3094 .owner = THIS_MODULE,
3095 .make_request = make_request,
3096 .run = run,
3097 .stop = stop,
3098 .status = status,
3099 .error_handler = error,
3100 .hot_add_disk = raid10_add_disk,
3101 .hot_remove_disk= raid10_remove_disk,
3102 .spare_active = raid10_spare_active,
3103 .sync_request = sync_request,
3104 .quiesce = raid10_quiesce,
3105 .size = raid10_size,
3106 .takeover = raid10_takeover,
3109 static int __init raid_init(void)
3111 return register_md_personality(&raid10_personality);
3114 static void raid_exit(void)
3116 unregister_md_personality(&raid10_personality);
3119 module_init(raid_init);
3120 module_exit(raid_exit);
3121 MODULE_LICENSE("GPL");
3122 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
3123 MODULE_ALIAS("md-personality-9"); /* RAID10 */
3124 MODULE_ALIAS("md-raid10");
3125 MODULE_ALIAS("md-level-10");
3127 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);