2 * raid1.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * RAID-1 management functions.
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/blkdev.h>
37 #include <linux/seq_file.h>
38 #include <linux/ratelimit.h>
44 #define PRINTK(x...) do { if (DEBUG) printk(x); } while (0)
47 * Number of guaranteed r1bios in case of extreme VM load:
49 #define NR_RAID1_BIOS 256
52 static void allow_barrier(conf_t *conf);
53 static void lower_barrier(conf_t *conf);
55 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
57 struct pool_info *pi = data;
58 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
60 /* allocate a r1bio with room for raid_disks entries in the bios array */
61 return kzalloc(size, gfp_flags);
64 static void r1bio_pool_free(void *r1_bio, void *data)
69 #define RESYNC_BLOCK_SIZE (64*1024)
70 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
71 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
72 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
73 #define RESYNC_WINDOW (2048*1024)
75 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
77 struct pool_info *pi = data;
83 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
88 * Allocate bios : 1 for reading, n-1 for writing
90 for (j = pi->raid_disks ; j-- ; ) {
91 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
94 r1_bio->bios[j] = bio;
97 * Allocate RESYNC_PAGES data pages and attach them to
99 * If this is a user-requested check/repair, allocate
100 * RESYNC_PAGES for each bio.
102 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
107 bio = r1_bio->bios[j];
108 for (i = 0; i < RESYNC_PAGES; i++) {
109 page = alloc_page(gfp_flags);
113 bio->bi_io_vec[i].bv_page = page;
117 /* If not user-requests, copy the page pointers to all bios */
118 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
119 for (i=0; i<RESYNC_PAGES ; i++)
120 for (j=1; j<pi->raid_disks; j++)
121 r1_bio->bios[j]->bi_io_vec[i].bv_page =
122 r1_bio->bios[0]->bi_io_vec[i].bv_page;
125 r1_bio->master_bio = NULL;
130 for (j=0 ; j < pi->raid_disks; j++)
131 for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++)
132 put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
135 while ( ++j < pi->raid_disks )
136 bio_put(r1_bio->bios[j]);
137 r1bio_pool_free(r1_bio, data);
141 static void r1buf_pool_free(void *__r1_bio, void *data)
143 struct pool_info *pi = data;
145 r1bio_t *r1bio = __r1_bio;
147 for (i = 0; i < RESYNC_PAGES; i++)
148 for (j = pi->raid_disks; j-- ;) {
150 r1bio->bios[j]->bi_io_vec[i].bv_page !=
151 r1bio->bios[0]->bi_io_vec[i].bv_page)
152 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
154 for (i=0 ; i < pi->raid_disks; i++)
155 bio_put(r1bio->bios[i]);
157 r1bio_pool_free(r1bio, data);
160 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
164 for (i = 0; i < conf->raid_disks; i++) {
165 struct bio **bio = r1_bio->bios + i;
166 if (!BIO_SPECIAL(*bio))
172 static void free_r1bio(r1bio_t *r1_bio)
174 conf_t *conf = r1_bio->mddev->private;
176 put_all_bios(conf, r1_bio);
177 mempool_free(r1_bio, conf->r1bio_pool);
180 static void put_buf(r1bio_t *r1_bio)
182 conf_t *conf = r1_bio->mddev->private;
185 for (i=0; i<conf->raid_disks; i++) {
186 struct bio *bio = r1_bio->bios[i];
188 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
191 mempool_free(r1_bio, conf->r1buf_pool);
196 static void reschedule_retry(r1bio_t *r1_bio)
199 mddev_t *mddev = r1_bio->mddev;
200 conf_t *conf = mddev->private;
202 spin_lock_irqsave(&conf->device_lock, flags);
203 list_add(&r1_bio->retry_list, &conf->retry_list);
205 spin_unlock_irqrestore(&conf->device_lock, flags);
207 wake_up(&conf->wait_barrier);
208 md_wakeup_thread(mddev->thread);
212 * raid_end_bio_io() is called when we have finished servicing a mirrored
213 * operation and are ready to return a success/failure code to the buffer
216 static void call_bio_endio(r1bio_t *r1_bio)
218 struct bio *bio = r1_bio->master_bio;
220 conf_t *conf = r1_bio->mddev->private;
222 if (bio->bi_phys_segments) {
224 spin_lock_irqsave(&conf->device_lock, flags);
225 bio->bi_phys_segments--;
226 done = (bio->bi_phys_segments == 0);
227 spin_unlock_irqrestore(&conf->device_lock, flags);
231 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
232 clear_bit(BIO_UPTODATE, &bio->bi_flags);
236 * Wake up any possible resync thread that waits for the device
243 static void raid_end_bio_io(r1bio_t *r1_bio)
245 struct bio *bio = r1_bio->master_bio;
247 /* if nobody has done the final endio yet, do it now */
248 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
249 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
250 (bio_data_dir(bio) == WRITE) ? "write" : "read",
251 (unsigned long long) bio->bi_sector,
252 (unsigned long long) bio->bi_sector +
253 (bio->bi_size >> 9) - 1);
255 call_bio_endio(r1_bio);
261 * Update disk head position estimator based on IRQ completion info.
263 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
265 conf_t *conf = r1_bio->mddev->private;
267 conf->mirrors[disk].head_position =
268 r1_bio->sector + (r1_bio->sectors);
272 * Find the disk number which triggered given bio
274 static int find_bio_disk(r1bio_t *r1_bio, struct bio *bio)
277 int raid_disks = r1_bio->mddev->raid_disks;
279 for (mirror = 0; mirror < raid_disks; mirror++)
280 if (r1_bio->bios[mirror] == bio)
283 BUG_ON(mirror == raid_disks);
284 update_head_pos(mirror, r1_bio);
289 static void raid1_end_read_request(struct bio *bio, int error)
291 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
292 r1bio_t *r1_bio = bio->bi_private;
294 conf_t *conf = r1_bio->mddev->private;
296 mirror = r1_bio->read_disk;
298 * this branch is our 'one mirror IO has finished' event handler:
300 update_head_pos(mirror, r1_bio);
303 set_bit(R1BIO_Uptodate, &r1_bio->state);
305 /* If all other devices have failed, we want to return
306 * the error upwards rather than fail the last device.
307 * Here we redefine "uptodate" to mean "Don't want to retry"
310 spin_lock_irqsave(&conf->device_lock, flags);
311 if (r1_bio->mddev->degraded == conf->raid_disks ||
312 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
313 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
315 spin_unlock_irqrestore(&conf->device_lock, flags);
319 raid_end_bio_io(r1_bio);
324 char b[BDEVNAME_SIZE];
326 KERN_ERR "md/raid1:%s: %s: "
327 "rescheduling sector %llu\n",
329 bdevname(conf->mirrors[mirror].rdev->bdev,
331 (unsigned long long)r1_bio->sector);
332 set_bit(R1BIO_ReadError, &r1_bio->state);
333 reschedule_retry(r1_bio);
336 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
339 static void close_write(r1bio_t *r1_bio)
341 /* it really is the end of this request */
342 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
343 /* free extra copy of the data pages */
344 int i = r1_bio->behind_page_count;
346 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
347 kfree(r1_bio->behind_bvecs);
348 r1_bio->behind_bvecs = NULL;
350 /* clear the bitmap if all writes complete successfully */
351 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
353 !test_bit(R1BIO_Degraded, &r1_bio->state),
354 test_bit(R1BIO_BehindIO, &r1_bio->state));
355 md_write_end(r1_bio->mddev);
358 static void r1_bio_write_done(r1bio_t *r1_bio)
360 if (!atomic_dec_and_test(&r1_bio->remaining))
363 if (test_bit(R1BIO_WriteError, &r1_bio->state))
364 reschedule_retry(r1_bio);
367 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
368 reschedule_retry(r1_bio);
370 raid_end_bio_io(r1_bio);
374 static void raid1_end_write_request(struct bio *bio, int error)
376 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
377 r1bio_t *r1_bio = bio->bi_private;
378 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
379 conf_t *conf = r1_bio->mddev->private;
380 struct bio *to_put = NULL;
382 mirror = find_bio_disk(r1_bio, bio);
385 * 'one mirror IO has finished' event handler:
388 set_bit(WriteErrorSeen,
389 &conf->mirrors[mirror].rdev->flags);
390 set_bit(R1BIO_WriteError, &r1_bio->state);
393 * Set R1BIO_Uptodate in our master bio, so that we
394 * will return a good error code for to the higher
395 * levels even if IO on some other mirrored buffer
398 * The 'master' represents the composite IO operation
399 * to user-side. So if something waits for IO, then it
400 * will wait for the 'master' bio.
405 r1_bio->bios[mirror] = NULL;
407 set_bit(R1BIO_Uptodate, &r1_bio->state);
409 /* Maybe we can clear some bad blocks. */
410 if (is_badblock(conf->mirrors[mirror].rdev,
411 r1_bio->sector, r1_bio->sectors,
412 &first_bad, &bad_sectors)) {
413 r1_bio->bios[mirror] = IO_MADE_GOOD;
414 set_bit(R1BIO_MadeGood, &r1_bio->state);
419 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
420 atomic_dec(&r1_bio->behind_remaining);
423 * In behind mode, we ACK the master bio once the I/O
424 * has safely reached all non-writemostly
425 * disks. Setting the Returned bit ensures that this
426 * gets done only once -- we don't ever want to return
427 * -EIO here, instead we'll wait
429 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
430 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
431 /* Maybe we can return now */
432 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
433 struct bio *mbio = r1_bio->master_bio;
434 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
435 (unsigned long long) mbio->bi_sector,
436 (unsigned long long) mbio->bi_sector +
437 (mbio->bi_size >> 9) - 1);
438 call_bio_endio(r1_bio);
442 if (r1_bio->bios[mirror] == NULL)
443 rdev_dec_pending(conf->mirrors[mirror].rdev,
447 * Let's see if all mirrored write operations have finished
450 r1_bio_write_done(r1_bio);
458 * This routine returns the disk from which the requested read should
459 * be done. There is a per-array 'next expected sequential IO' sector
460 * number - if this matches on the next IO then we use the last disk.
461 * There is also a per-disk 'last know head position' sector that is
462 * maintained from IRQ contexts, both the normal and the resync IO
463 * completion handlers update this position correctly. If there is no
464 * perfect sequential match then we pick the disk whose head is closest.
466 * If there are 2 mirrors in the same 2 devices, performance degrades
467 * because position is mirror, not device based.
469 * The rdev for the device selected will have nr_pending incremented.
471 static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors)
473 const sector_t this_sector = r1_bio->sector;
475 int best_good_sectors;
485 * Check if we can balance. We can balance on the whole
486 * device if no resync is going on, or below the resync window.
487 * We take the first readable disk when above the resync window.
490 sectors = r1_bio->sectors;
492 best_dist = MaxSector;
493 best_good_sectors = 0;
495 if (conf->mddev->recovery_cp < MaxSector &&
496 (this_sector + sectors >= conf->next_resync)) {
501 start_disk = conf->last_used;
504 for (i = 0 ; i < conf->raid_disks ; i++) {
509 int disk = start_disk + i;
510 if (disk >= conf->raid_disks)
511 disk -= conf->raid_disks;
513 rdev = rcu_dereference(conf->mirrors[disk].rdev);
514 if (r1_bio->bios[disk] == IO_BLOCKED
516 || test_bit(Faulty, &rdev->flags))
518 if (!test_bit(In_sync, &rdev->flags) &&
519 rdev->recovery_offset < this_sector + sectors)
521 if (test_bit(WriteMostly, &rdev->flags)) {
522 /* Don't balance among write-mostly, just
523 * use the first as a last resort */
528 /* This is a reasonable device to use. It might
531 if (is_badblock(rdev, this_sector, sectors,
532 &first_bad, &bad_sectors)) {
533 if (best_dist < MaxSector)
534 /* already have a better device */
536 if (first_bad <= this_sector) {
537 /* cannot read here. If this is the 'primary'
538 * device, then we must not read beyond
539 * bad_sectors from another device..
541 bad_sectors -= (this_sector - first_bad);
542 if (choose_first && sectors > bad_sectors)
543 sectors = bad_sectors;
544 if (best_good_sectors > sectors)
545 best_good_sectors = sectors;
548 sector_t good_sectors = first_bad - this_sector;
549 if (good_sectors > best_good_sectors) {
550 best_good_sectors = good_sectors;
558 best_good_sectors = sectors;
560 dist = abs(this_sector - conf->mirrors[disk].head_position);
562 /* Don't change to another disk for sequential reads */
563 || conf->next_seq_sect == this_sector
565 /* If device is idle, use it */
566 || atomic_read(&rdev->nr_pending) == 0) {
570 if (dist < best_dist) {
576 if (best_disk >= 0) {
577 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
580 atomic_inc(&rdev->nr_pending);
581 if (test_bit(Faulty, &rdev->flags)) {
582 /* cannot risk returning a device that failed
583 * before we inc'ed nr_pending
585 rdev_dec_pending(rdev, conf->mddev);
588 sectors = best_good_sectors;
589 conf->next_seq_sect = this_sector + sectors;
590 conf->last_used = best_disk;
593 *max_sectors = sectors;
598 int md_raid1_congested(mddev_t *mddev, int bits)
600 conf_t *conf = mddev->private;
604 for (i = 0; i < mddev->raid_disks; i++) {
605 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
606 if (rdev && !test_bit(Faulty, &rdev->flags)) {
607 struct request_queue *q = bdev_get_queue(rdev->bdev);
611 /* Note the '|| 1' - when read_balance prefers
612 * non-congested targets, it can be removed
614 if ((bits & (1<<BDI_async_congested)) || 1)
615 ret |= bdi_congested(&q->backing_dev_info, bits);
617 ret &= bdi_congested(&q->backing_dev_info, bits);
623 EXPORT_SYMBOL_GPL(md_raid1_congested);
625 static int raid1_congested(void *data, int bits)
627 mddev_t *mddev = data;
629 return mddev_congested(mddev, bits) ||
630 md_raid1_congested(mddev, bits);
633 static void flush_pending_writes(conf_t *conf)
635 /* Any writes that have been queued but are awaiting
636 * bitmap updates get flushed here.
638 spin_lock_irq(&conf->device_lock);
640 if (conf->pending_bio_list.head) {
642 bio = bio_list_get(&conf->pending_bio_list);
643 spin_unlock_irq(&conf->device_lock);
644 /* flush any pending bitmap writes to
645 * disk before proceeding w/ I/O */
646 bitmap_unplug(conf->mddev->bitmap);
648 while (bio) { /* submit pending writes */
649 struct bio *next = bio->bi_next;
651 generic_make_request(bio);
655 spin_unlock_irq(&conf->device_lock);
659 * Sometimes we need to suspend IO while we do something else,
660 * either some resync/recovery, or reconfigure the array.
661 * To do this we raise a 'barrier'.
662 * The 'barrier' is a counter that can be raised multiple times
663 * to count how many activities are happening which preclude
665 * We can only raise the barrier if there is no pending IO.
666 * i.e. if nr_pending == 0.
667 * We choose only to raise the barrier if no-one is waiting for the
668 * barrier to go down. This means that as soon as an IO request
669 * is ready, no other operations which require a barrier will start
670 * until the IO request has had a chance.
672 * So: regular IO calls 'wait_barrier'. When that returns there
673 * is no backgroup IO happening, It must arrange to call
674 * allow_barrier when it has finished its IO.
675 * backgroup IO calls must call raise_barrier. Once that returns
676 * there is no normal IO happeing. It must arrange to call
677 * lower_barrier when the particular background IO completes.
679 #define RESYNC_DEPTH 32
681 static void raise_barrier(conf_t *conf)
683 spin_lock_irq(&conf->resync_lock);
685 /* Wait until no block IO is waiting */
686 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
687 conf->resync_lock, );
689 /* block any new IO from starting */
692 /* Now wait for all pending IO to complete */
693 wait_event_lock_irq(conf->wait_barrier,
694 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
695 conf->resync_lock, );
697 spin_unlock_irq(&conf->resync_lock);
700 static void lower_barrier(conf_t *conf)
703 BUG_ON(conf->barrier <= 0);
704 spin_lock_irqsave(&conf->resync_lock, flags);
706 spin_unlock_irqrestore(&conf->resync_lock, flags);
707 wake_up(&conf->wait_barrier);
710 static void wait_barrier(conf_t *conf)
712 spin_lock_irq(&conf->resync_lock);
715 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
721 spin_unlock_irq(&conf->resync_lock);
724 static void allow_barrier(conf_t *conf)
727 spin_lock_irqsave(&conf->resync_lock, flags);
729 spin_unlock_irqrestore(&conf->resync_lock, flags);
730 wake_up(&conf->wait_barrier);
733 static void freeze_array(conf_t *conf)
735 /* stop syncio and normal IO and wait for everything to
737 * We increment barrier and nr_waiting, and then
738 * wait until nr_pending match nr_queued+1
739 * This is called in the context of one normal IO request
740 * that has failed. Thus any sync request that might be pending
741 * will be blocked by nr_pending, and we need to wait for
742 * pending IO requests to complete or be queued for re-try.
743 * Thus the number queued (nr_queued) plus this request (1)
744 * must match the number of pending IOs (nr_pending) before
747 spin_lock_irq(&conf->resync_lock);
750 wait_event_lock_irq(conf->wait_barrier,
751 conf->nr_pending == conf->nr_queued+1,
753 flush_pending_writes(conf));
754 spin_unlock_irq(&conf->resync_lock);
756 static void unfreeze_array(conf_t *conf)
758 /* reverse the effect of the freeze */
759 spin_lock_irq(&conf->resync_lock);
762 wake_up(&conf->wait_barrier);
763 spin_unlock_irq(&conf->resync_lock);
767 /* duplicate the data pages for behind I/O
769 static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio)
772 struct bio_vec *bvec;
773 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
775 if (unlikely(!bvecs))
778 bio_for_each_segment(bvec, bio, i) {
780 bvecs[i].bv_page = alloc_page(GFP_NOIO);
781 if (unlikely(!bvecs[i].bv_page))
783 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
784 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
785 kunmap(bvecs[i].bv_page);
786 kunmap(bvec->bv_page);
788 r1_bio->behind_bvecs = bvecs;
789 r1_bio->behind_page_count = bio->bi_vcnt;
790 set_bit(R1BIO_BehindIO, &r1_bio->state);
794 for (i = 0; i < bio->bi_vcnt; i++)
795 if (bvecs[i].bv_page)
796 put_page(bvecs[i].bv_page);
798 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
801 static int make_request(mddev_t *mddev, struct bio * bio)
803 conf_t *conf = mddev->private;
804 mirror_info_t *mirror;
806 struct bio *read_bio;
808 struct bitmap *bitmap;
810 const int rw = bio_data_dir(bio);
811 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
812 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
813 mdk_rdev_t *blocked_rdev;
820 * Register the new request and wait if the reconstruction
821 * thread has put up a bar for new requests.
822 * Continue immediately if no resync is active currently.
825 md_write_start(mddev, bio); /* wait on superblock update early */
827 if (bio_data_dir(bio) == WRITE &&
828 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
829 bio->bi_sector < mddev->suspend_hi) {
830 /* As the suspend_* range is controlled by
831 * userspace, we want an interruptible
836 flush_signals(current);
837 prepare_to_wait(&conf->wait_barrier,
838 &w, TASK_INTERRUPTIBLE);
839 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
840 bio->bi_sector >= mddev->suspend_hi)
844 finish_wait(&conf->wait_barrier, &w);
849 bitmap = mddev->bitmap;
852 * make_request() can abort the operation when READA is being
853 * used and no empty request is available.
856 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
858 r1_bio->master_bio = bio;
859 r1_bio->sectors = bio->bi_size >> 9;
861 r1_bio->mddev = mddev;
862 r1_bio->sector = bio->bi_sector;
864 /* We might need to issue multiple reads to different
865 * devices if there are bad blocks around, so we keep
866 * track of the number of reads in bio->bi_phys_segments.
867 * If this is 0, there is only one r1_bio and no locking
868 * will be needed when requests complete. If it is
869 * non-zero, then it is the number of not-completed requests.
871 bio->bi_phys_segments = 0;
872 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
876 * read balancing logic:
881 rdisk = read_balance(conf, r1_bio, &max_sectors);
884 /* couldn't find anywhere to read from */
885 raid_end_bio_io(r1_bio);
888 mirror = conf->mirrors + rdisk;
890 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
892 /* Reading from a write-mostly device must
893 * take care not to over-take any writes
896 wait_event(bitmap->behind_wait,
897 atomic_read(&bitmap->behind_writes) == 0);
899 r1_bio->read_disk = rdisk;
901 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
902 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
905 r1_bio->bios[rdisk] = read_bio;
907 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
908 read_bio->bi_bdev = mirror->rdev->bdev;
909 read_bio->bi_end_io = raid1_end_read_request;
910 read_bio->bi_rw = READ | do_sync;
911 read_bio->bi_private = r1_bio;
913 if (max_sectors < r1_bio->sectors) {
914 /* could not read all from this device, so we will
915 * need another r1_bio.
918 sectors_handled = (r1_bio->sector + max_sectors
920 r1_bio->sectors = max_sectors;
921 spin_lock_irq(&conf->device_lock);
922 if (bio->bi_phys_segments == 0)
923 bio->bi_phys_segments = 2;
925 bio->bi_phys_segments++;
926 spin_unlock_irq(&conf->device_lock);
927 /* Cannot call generic_make_request directly
928 * as that will be queued in __make_request
929 * and subsequent mempool_alloc might block waiting
930 * for it. So hand bio over to raid1d.
932 reschedule_retry(r1_bio);
934 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
936 r1_bio->master_bio = bio;
937 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
939 r1_bio->mddev = mddev;
940 r1_bio->sector = bio->bi_sector + sectors_handled;
943 generic_make_request(read_bio);
950 /* first select target devices under rcu_lock and
951 * inc refcount on their rdev. Record them by setting
953 * If there are known/acknowledged bad blocks on any device on
954 * which we have seen a write error, we want to avoid writing those
956 * This potentially requires several writes to write around
957 * the bad blocks. Each set of writes gets it's own r1bio
958 * with a set of bios attached.
960 plugged = mddev_check_plugged(mddev);
962 disks = conf->raid_disks;
966 max_sectors = r1_bio->sectors;
967 for (i = 0; i < disks; i++) {
968 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
969 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
970 atomic_inc(&rdev->nr_pending);
974 r1_bio->bios[i] = NULL;
975 if (!rdev || test_bit(Faulty, &rdev->flags)) {
976 set_bit(R1BIO_Degraded, &r1_bio->state);
980 atomic_inc(&rdev->nr_pending);
981 if (test_bit(WriteErrorSeen, &rdev->flags)) {
986 is_bad = is_badblock(rdev, r1_bio->sector,
988 &first_bad, &bad_sectors);
990 /* mustn't write here until the bad block is
992 set_bit(BlockedBadBlocks, &rdev->flags);
996 if (is_bad && first_bad <= r1_bio->sector) {
997 /* Cannot write here at all */
998 bad_sectors -= (r1_bio->sector - first_bad);
999 if (bad_sectors < max_sectors)
1000 /* mustn't write more than bad_sectors
1001 * to other devices yet
1003 max_sectors = bad_sectors;
1004 rdev_dec_pending(rdev, mddev);
1005 /* We don't set R1BIO_Degraded as that
1006 * only applies if the disk is
1007 * missing, so it might be re-added,
1008 * and we want to know to recover this
1010 * In this case the device is here,
1011 * and the fact that this chunk is not
1012 * in-sync is recorded in the bad
1018 int good_sectors = first_bad - r1_bio->sector;
1019 if (good_sectors < max_sectors)
1020 max_sectors = good_sectors;
1023 r1_bio->bios[i] = bio;
1027 if (unlikely(blocked_rdev)) {
1028 /* Wait for this device to become unblocked */
1031 for (j = 0; j < i; j++)
1032 if (r1_bio->bios[j])
1033 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1035 allow_barrier(conf);
1036 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1041 if (max_sectors < r1_bio->sectors) {
1042 /* We are splitting this write into multiple parts, so
1043 * we need to prepare for allocating another r1_bio.
1045 r1_bio->sectors = max_sectors;
1046 spin_lock_irq(&conf->device_lock);
1047 if (bio->bi_phys_segments == 0)
1048 bio->bi_phys_segments = 2;
1050 bio->bi_phys_segments++;
1051 spin_unlock_irq(&conf->device_lock);
1053 sectors_handled = r1_bio->sector + max_sectors - bio->bi_sector;
1055 atomic_set(&r1_bio->remaining, 1);
1056 atomic_set(&r1_bio->behind_remaining, 0);
1059 for (i = 0; i < disks; i++) {
1061 if (!r1_bio->bios[i])
1064 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1065 md_trim_bio(mbio, r1_bio->sector - bio->bi_sector, max_sectors);
1069 * Not if there are too many, or cannot
1070 * allocate memory, or a reader on WriteMostly
1071 * is waiting for behind writes to flush */
1073 (atomic_read(&bitmap->behind_writes)
1074 < mddev->bitmap_info.max_write_behind) &&
1075 !waitqueue_active(&bitmap->behind_wait))
1076 alloc_behind_pages(mbio, r1_bio);
1078 bitmap_startwrite(bitmap, r1_bio->sector,
1080 test_bit(R1BIO_BehindIO,
1084 if (r1_bio->behind_bvecs) {
1085 struct bio_vec *bvec;
1088 /* Yes, I really want the '__' version so that
1089 * we clear any unused pointer in the io_vec, rather
1090 * than leave them unchanged. This is important
1091 * because when we come to free the pages, we won't
1092 * know the original bi_idx, so we just free
1095 __bio_for_each_segment(bvec, mbio, j, 0)
1096 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
1097 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1098 atomic_inc(&r1_bio->behind_remaining);
1101 r1_bio->bios[i] = mbio;
1103 mbio->bi_sector = (r1_bio->sector +
1104 conf->mirrors[i].rdev->data_offset);
1105 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1106 mbio->bi_end_io = raid1_end_write_request;
1107 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
1108 mbio->bi_private = r1_bio;
1110 atomic_inc(&r1_bio->remaining);
1111 spin_lock_irqsave(&conf->device_lock, flags);
1112 bio_list_add(&conf->pending_bio_list, mbio);
1113 spin_unlock_irqrestore(&conf->device_lock, flags);
1115 /* Mustn't call r1_bio_write_done before this next test,
1116 * as it could result in the bio being freed.
1118 if (sectors_handled < (bio->bi_size >> 9)) {
1119 r1_bio_write_done(r1_bio);
1120 /* We need another r1_bio. It has already been counted
1121 * in bio->bi_phys_segments
1123 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1124 r1_bio->master_bio = bio;
1125 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1127 r1_bio->mddev = mddev;
1128 r1_bio->sector = bio->bi_sector + sectors_handled;
1132 r1_bio_write_done(r1_bio);
1134 /* In case raid1d snuck in to freeze_array */
1135 wake_up(&conf->wait_barrier);
1137 if (do_sync || !bitmap || !plugged)
1138 md_wakeup_thread(mddev->thread);
1143 static void status(struct seq_file *seq, mddev_t *mddev)
1145 conf_t *conf = mddev->private;
1148 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1149 conf->raid_disks - mddev->degraded);
1151 for (i = 0; i < conf->raid_disks; i++) {
1152 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1153 seq_printf(seq, "%s",
1154 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1157 seq_printf(seq, "]");
1161 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1163 char b[BDEVNAME_SIZE];
1164 conf_t *conf = mddev->private;
1167 * If it is not operational, then we have already marked it as dead
1168 * else if it is the last working disks, ignore the error, let the
1169 * next level up know.
1170 * else mark the drive as failed
1172 if (test_bit(In_sync, &rdev->flags)
1173 && (conf->raid_disks - mddev->degraded) == 1) {
1175 * Don't fail the drive, act as though we were just a
1176 * normal single drive.
1177 * However don't try a recovery from this drive as
1178 * it is very likely to fail.
1180 conf->recovery_disabled = mddev->recovery_disabled;
1183 set_bit(Blocked, &rdev->flags);
1184 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1185 unsigned long flags;
1186 spin_lock_irqsave(&conf->device_lock, flags);
1188 set_bit(Faulty, &rdev->flags);
1189 spin_unlock_irqrestore(&conf->device_lock, flags);
1191 * if recovery is running, make sure it aborts.
1193 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1195 set_bit(Faulty, &rdev->flags);
1196 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1198 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1199 "md/raid1:%s: Operation continuing on %d devices.\n",
1200 mdname(mddev), bdevname(rdev->bdev, b),
1201 mdname(mddev), conf->raid_disks - mddev->degraded);
1204 static void print_conf(conf_t *conf)
1208 printk(KERN_DEBUG "RAID1 conf printout:\n");
1210 printk(KERN_DEBUG "(!conf)\n");
1213 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1217 for (i = 0; i < conf->raid_disks; i++) {
1218 char b[BDEVNAME_SIZE];
1219 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1221 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1222 i, !test_bit(In_sync, &rdev->flags),
1223 !test_bit(Faulty, &rdev->flags),
1224 bdevname(rdev->bdev,b));
1229 static void close_sync(conf_t *conf)
1232 allow_barrier(conf);
1234 mempool_destroy(conf->r1buf_pool);
1235 conf->r1buf_pool = NULL;
1238 static int raid1_spare_active(mddev_t *mddev)
1241 conf_t *conf = mddev->private;
1243 unsigned long flags;
1246 * Find all failed disks within the RAID1 configuration
1247 * and mark them readable.
1248 * Called under mddev lock, so rcu protection not needed.
1250 for (i = 0; i < conf->raid_disks; i++) {
1251 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1253 && !test_bit(Faulty, &rdev->flags)
1254 && !test_and_set_bit(In_sync, &rdev->flags)) {
1256 sysfs_notify_dirent_safe(rdev->sysfs_state);
1259 spin_lock_irqsave(&conf->device_lock, flags);
1260 mddev->degraded -= count;
1261 spin_unlock_irqrestore(&conf->device_lock, flags);
1268 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1270 conf_t *conf = mddev->private;
1275 int last = mddev->raid_disks - 1;
1277 if (mddev->recovery_disabled == conf->recovery_disabled)
1280 if (rdev->raid_disk >= 0)
1281 first = last = rdev->raid_disk;
1283 for (mirror = first; mirror <= last; mirror++)
1284 if ( !(p=conf->mirrors+mirror)->rdev) {
1286 disk_stack_limits(mddev->gendisk, rdev->bdev,
1287 rdev->data_offset << 9);
1288 /* as we don't honour merge_bvec_fn, we must
1289 * never risk violating it, so limit
1290 * ->max_segments to one lying with a single
1291 * page, as a one page request is never in
1294 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1295 blk_queue_max_segments(mddev->queue, 1);
1296 blk_queue_segment_boundary(mddev->queue,
1297 PAGE_CACHE_SIZE - 1);
1300 p->head_position = 0;
1301 rdev->raid_disk = mirror;
1303 /* As all devices are equivalent, we don't need a full recovery
1304 * if this was recently any drive of the array
1306 if (rdev->saved_raid_disk < 0)
1308 rcu_assign_pointer(p->rdev, rdev);
1311 md_integrity_add_rdev(rdev, mddev);
1316 static int raid1_remove_disk(mddev_t *mddev, int number)
1318 conf_t *conf = mddev->private;
1321 mirror_info_t *p = conf->mirrors+ number;
1326 if (test_bit(In_sync, &rdev->flags) ||
1327 atomic_read(&rdev->nr_pending)) {
1331 /* Only remove non-faulty devices if recovery
1334 if (!test_bit(Faulty, &rdev->flags) &&
1335 mddev->recovery_disabled != conf->recovery_disabled &&
1336 mddev->degraded < conf->raid_disks) {
1342 if (atomic_read(&rdev->nr_pending)) {
1343 /* lost the race, try later */
1348 err = md_integrity_register(mddev);
1357 static void end_sync_read(struct bio *bio, int error)
1359 r1bio_t *r1_bio = bio->bi_private;
1361 update_head_pos(r1_bio->read_disk, r1_bio);
1364 * we have read a block, now it needs to be re-written,
1365 * or re-read if the read failed.
1366 * We don't do much here, just schedule handling by raid1d
1368 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1369 set_bit(R1BIO_Uptodate, &r1_bio->state);
1371 if (atomic_dec_and_test(&r1_bio->remaining))
1372 reschedule_retry(r1_bio);
1375 static void end_sync_write(struct bio *bio, int error)
1377 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1378 r1bio_t *r1_bio = bio->bi_private;
1379 mddev_t *mddev = r1_bio->mddev;
1380 conf_t *conf = mddev->private;
1385 mirror = find_bio_disk(r1_bio, bio);
1388 sector_t sync_blocks = 0;
1389 sector_t s = r1_bio->sector;
1390 long sectors_to_go = r1_bio->sectors;
1391 /* make sure these bits doesn't get cleared. */
1393 bitmap_end_sync(mddev->bitmap, s,
1396 sectors_to_go -= sync_blocks;
1397 } while (sectors_to_go > 0);
1398 set_bit(WriteErrorSeen,
1399 &conf->mirrors[mirror].rdev->flags);
1400 set_bit(R1BIO_WriteError, &r1_bio->state);
1401 } else if (is_badblock(conf->mirrors[mirror].rdev,
1404 &first_bad, &bad_sectors) &&
1405 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1408 &first_bad, &bad_sectors)
1410 set_bit(R1BIO_MadeGood, &r1_bio->state);
1412 if (atomic_dec_and_test(&r1_bio->remaining)) {
1413 int s = r1_bio->sectors;
1414 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1415 test_bit(R1BIO_WriteError, &r1_bio->state))
1416 reschedule_retry(r1_bio);
1419 md_done_sync(mddev, s, uptodate);
1424 static int r1_sync_page_io(mdk_rdev_t *rdev, sector_t sector,
1425 int sectors, struct page *page, int rw)
1427 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1431 set_bit(WriteErrorSeen, &rdev->flags);
1432 /* need to record an error - either for the block or the device */
1433 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1434 md_error(rdev->mddev, rdev);
1438 static int fix_sync_read_error(r1bio_t *r1_bio)
1440 /* Try some synchronous reads of other devices to get
1441 * good data, much like with normal read errors. Only
1442 * read into the pages we already have so we don't
1443 * need to re-issue the read request.
1444 * We don't need to freeze the array, because being in an
1445 * active sync request, there is no normal IO, and
1446 * no overlapping syncs.
1447 * We don't need to check is_badblock() again as we
1448 * made sure that anything with a bad block in range
1449 * will have bi_end_io clear.
1451 mddev_t *mddev = r1_bio->mddev;
1452 conf_t *conf = mddev->private;
1453 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1454 sector_t sect = r1_bio->sector;
1455 int sectors = r1_bio->sectors;
1460 int d = r1_bio->read_disk;
1465 if (s > (PAGE_SIZE>>9))
1468 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1469 /* No rcu protection needed here devices
1470 * can only be removed when no resync is
1471 * active, and resync is currently active
1473 rdev = conf->mirrors[d].rdev;
1474 if (sync_page_io(rdev, sect, s<<9,
1475 bio->bi_io_vec[idx].bv_page,
1482 if (d == conf->raid_disks)
1484 } while (!success && d != r1_bio->read_disk);
1487 char b[BDEVNAME_SIZE];
1489 /* Cannot read from anywhere, this block is lost.
1490 * Record a bad block on each device. If that doesn't
1491 * work just disable and interrupt the recovery.
1492 * Don't fail devices as that won't really help.
1494 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1495 " for block %llu\n",
1497 bdevname(bio->bi_bdev, b),
1498 (unsigned long long)r1_bio->sector);
1499 for (d = 0; d < conf->raid_disks; d++) {
1500 rdev = conf->mirrors[d].rdev;
1501 if (!rdev || test_bit(Faulty, &rdev->flags))
1503 if (!rdev_set_badblocks(rdev, sect, s, 0))
1507 mddev->recovery_disabled = 1;
1508 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1509 md_done_sync(mddev, r1_bio->sectors, 0);
1521 /* write it back and re-read */
1522 while (d != r1_bio->read_disk) {
1524 d = conf->raid_disks;
1526 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1528 rdev = conf->mirrors[d].rdev;
1529 if (r1_sync_page_io(rdev, sect, s,
1530 bio->bi_io_vec[idx].bv_page,
1532 r1_bio->bios[d]->bi_end_io = NULL;
1533 rdev_dec_pending(rdev, mddev);
1537 while (d != r1_bio->read_disk) {
1539 d = conf->raid_disks;
1541 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1543 rdev = conf->mirrors[d].rdev;
1544 if (r1_sync_page_io(rdev, sect, s,
1545 bio->bi_io_vec[idx].bv_page,
1547 atomic_add(s, &rdev->corrected_errors);
1553 set_bit(R1BIO_Uptodate, &r1_bio->state);
1554 set_bit(BIO_UPTODATE, &bio->bi_flags);
1558 static int process_checks(r1bio_t *r1_bio)
1560 /* We have read all readable devices. If we haven't
1561 * got the block, then there is no hope left.
1562 * If we have, then we want to do a comparison
1563 * and skip the write if everything is the same.
1564 * If any blocks failed to read, then we need to
1565 * attempt an over-write
1567 mddev_t *mddev = r1_bio->mddev;
1568 conf_t *conf = mddev->private;
1572 for (primary = 0; primary < conf->raid_disks; primary++)
1573 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1574 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1575 r1_bio->bios[primary]->bi_end_io = NULL;
1576 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1579 r1_bio->read_disk = primary;
1580 for (i = 0; i < conf->raid_disks; i++) {
1582 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1583 struct bio *pbio = r1_bio->bios[primary];
1584 struct bio *sbio = r1_bio->bios[i];
1587 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1590 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1591 for (j = vcnt; j-- ; ) {
1593 p = pbio->bi_io_vec[j].bv_page;
1594 s = sbio->bi_io_vec[j].bv_page;
1595 if (memcmp(page_address(p),
1603 mddev->resync_mismatches += r1_bio->sectors;
1604 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1605 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1606 /* No need to write to this device. */
1607 sbio->bi_end_io = NULL;
1608 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1611 /* fixup the bio for reuse */
1612 sbio->bi_vcnt = vcnt;
1613 sbio->bi_size = r1_bio->sectors << 9;
1615 sbio->bi_phys_segments = 0;
1616 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1617 sbio->bi_flags |= 1 << BIO_UPTODATE;
1618 sbio->bi_next = NULL;
1619 sbio->bi_sector = r1_bio->sector +
1620 conf->mirrors[i].rdev->data_offset;
1621 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1622 size = sbio->bi_size;
1623 for (j = 0; j < vcnt ; j++) {
1625 bi = &sbio->bi_io_vec[j];
1627 if (size > PAGE_SIZE)
1628 bi->bv_len = PAGE_SIZE;
1632 memcpy(page_address(bi->bv_page),
1633 page_address(pbio->bi_io_vec[j].bv_page),
1640 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1642 conf_t *conf = mddev->private;
1644 int disks = conf->raid_disks;
1645 struct bio *bio, *wbio;
1647 bio = r1_bio->bios[r1_bio->read_disk];
1649 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1650 /* ouch - failed to read all of that. */
1651 if (!fix_sync_read_error(r1_bio))
1654 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1655 if (process_checks(r1_bio) < 0)
1660 atomic_set(&r1_bio->remaining, 1);
1661 for (i = 0; i < disks ; i++) {
1662 wbio = r1_bio->bios[i];
1663 if (wbio->bi_end_io == NULL ||
1664 (wbio->bi_end_io == end_sync_read &&
1665 (i == r1_bio->read_disk ||
1666 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
1669 wbio->bi_rw = WRITE;
1670 wbio->bi_end_io = end_sync_write;
1671 atomic_inc(&r1_bio->remaining);
1672 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1674 generic_make_request(wbio);
1677 if (atomic_dec_and_test(&r1_bio->remaining)) {
1678 /* if we're here, all write(s) have completed, so clean up */
1679 md_done_sync(mddev, r1_bio->sectors, 1);
1685 * This is a kernel thread which:
1687 * 1. Retries failed read operations on working mirrors.
1688 * 2. Updates the raid superblock when problems encounter.
1689 * 3. Performs writes following reads for array synchronising.
1692 static void fix_read_error(conf_t *conf, int read_disk,
1693 sector_t sect, int sectors)
1695 mddev_t *mddev = conf->mddev;
1703 if (s > (PAGE_SIZE>>9))
1707 /* Note: no rcu protection needed here
1708 * as this is synchronous in the raid1d thread
1709 * which is the thread that might remove
1710 * a device. If raid1d ever becomes multi-threaded....
1715 rdev = conf->mirrors[d].rdev;
1717 test_bit(In_sync, &rdev->flags) &&
1718 is_badblock(rdev, sect, s,
1719 &first_bad, &bad_sectors) == 0 &&
1720 sync_page_io(rdev, sect, s<<9,
1721 conf->tmppage, READ, false))
1725 if (d == conf->raid_disks)
1728 } while (!success && d != read_disk);
1731 /* Cannot read from anywhere - mark it bad */
1732 mdk_rdev_t *rdev = conf->mirrors[read_disk].rdev;
1733 if (!rdev_set_badblocks(rdev, sect, s, 0))
1734 md_error(mddev, rdev);
1737 /* write it back and re-read */
1739 while (d != read_disk) {
1741 d = conf->raid_disks;
1743 rdev = conf->mirrors[d].rdev;
1745 test_bit(In_sync, &rdev->flags))
1746 r1_sync_page_io(rdev, sect, s,
1747 conf->tmppage, WRITE);
1750 while (d != read_disk) {
1751 char b[BDEVNAME_SIZE];
1753 d = conf->raid_disks;
1755 rdev = conf->mirrors[d].rdev;
1757 test_bit(In_sync, &rdev->flags)) {
1758 if (r1_sync_page_io(rdev, sect, s,
1759 conf->tmppage, READ)) {
1760 atomic_add(s, &rdev->corrected_errors);
1762 "md/raid1:%s: read error corrected "
1763 "(%d sectors at %llu on %s)\n",
1765 (unsigned long long)(sect +
1767 bdevname(rdev->bdev, b));
1776 static void bi_complete(struct bio *bio, int error)
1778 complete((struct completion *)bio->bi_private);
1781 static int submit_bio_wait(int rw, struct bio *bio)
1783 struct completion event;
1786 init_completion(&event);
1787 bio->bi_private = &event;
1788 bio->bi_end_io = bi_complete;
1789 submit_bio(rw, bio);
1790 wait_for_completion(&event);
1792 return test_bit(BIO_UPTODATE, &bio->bi_flags);
1795 static int narrow_write_error(r1bio_t *r1_bio, int i)
1797 mddev_t *mddev = r1_bio->mddev;
1798 conf_t *conf = mddev->private;
1799 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1801 struct bio_vec *vec;
1803 /* bio has the data to be written to device 'i' where
1804 * we just recently had a write error.
1805 * We repeatedly clone the bio and trim down to one block,
1806 * then try the write. Where the write fails we record
1808 * It is conceivable that the bio doesn't exactly align with
1809 * blocks. We must handle this somehow.
1811 * We currently own a reference on the rdev.
1817 int sect_to_write = r1_bio->sectors;
1820 if (rdev->badblocks.shift < 0)
1823 block_sectors = 1 << rdev->badblocks.shift;
1824 sector = r1_bio->sector;
1825 sectors = ((sector + block_sectors)
1826 & ~(sector_t)(block_sectors - 1))
1829 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
1830 vcnt = r1_bio->behind_page_count;
1831 vec = r1_bio->behind_bvecs;
1833 while (vec[idx].bv_page == NULL)
1836 vcnt = r1_bio->master_bio->bi_vcnt;
1837 vec = r1_bio->master_bio->bi_io_vec;
1838 idx = r1_bio->master_bio->bi_idx;
1840 while (sect_to_write) {
1842 if (sectors > sect_to_write)
1843 sectors = sect_to_write;
1844 /* Write at 'sector' for 'sectors'*/
1846 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
1847 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
1848 wbio->bi_sector = r1_bio->sector;
1849 wbio->bi_rw = WRITE;
1850 wbio->bi_vcnt = vcnt;
1851 wbio->bi_size = r1_bio->sectors << 9;
1854 md_trim_bio(wbio, sector - r1_bio->sector, sectors);
1855 wbio->bi_sector += rdev->data_offset;
1856 wbio->bi_bdev = rdev->bdev;
1857 if (submit_bio_wait(WRITE, wbio) == 0)
1859 ok = rdev_set_badblocks(rdev, sector,
1864 sect_to_write -= sectors;
1866 sectors = block_sectors;
1871 static void handle_sync_write_finished(conf_t *conf, r1bio_t *r1_bio)
1874 int s = r1_bio->sectors;
1875 for (m = 0; m < conf->raid_disks ; m++) {
1876 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1877 struct bio *bio = r1_bio->bios[m];
1878 if (bio->bi_end_io == NULL)
1880 if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1881 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
1882 rdev_clear_badblocks(rdev, r1_bio->sector, s);
1884 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1885 test_bit(R1BIO_WriteError, &r1_bio->state)) {
1886 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
1887 md_error(conf->mddev, rdev);
1891 md_done_sync(conf->mddev, s, 1);
1894 static void handle_write_finished(conf_t *conf, r1bio_t *r1_bio)
1897 for (m = 0; m < conf->raid_disks ; m++)
1898 if (r1_bio->bios[m] == IO_MADE_GOOD) {
1899 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1900 rdev_clear_badblocks(rdev,
1903 rdev_dec_pending(rdev, conf->mddev);
1904 } else if (r1_bio->bios[m] != NULL) {
1905 /* This drive got a write error. We need to
1906 * narrow down and record precise write
1909 if (!narrow_write_error(r1_bio, m)) {
1910 md_error(conf->mddev,
1911 conf->mirrors[m].rdev);
1912 /* an I/O failed, we can't clear the bitmap */
1913 set_bit(R1BIO_Degraded, &r1_bio->state);
1915 rdev_dec_pending(conf->mirrors[m].rdev,
1918 if (test_bit(R1BIO_WriteError, &r1_bio->state))
1919 close_write(r1_bio);
1920 raid_end_bio_io(r1_bio);
1923 static void handle_read_error(conf_t *conf, r1bio_t *r1_bio)
1927 mddev_t *mddev = conf->mddev;
1929 char b[BDEVNAME_SIZE];
1932 clear_bit(R1BIO_ReadError, &r1_bio->state);
1933 /* we got a read error. Maybe the drive is bad. Maybe just
1934 * the block and we can fix it.
1935 * We freeze all other IO, and try reading the block from
1936 * other devices. When we find one, we re-write
1937 * and check it that fixes the read error.
1938 * This is all done synchronously while the array is
1941 if (mddev->ro == 0) {
1943 fix_read_error(conf, r1_bio->read_disk,
1944 r1_bio->sector, r1_bio->sectors);
1945 unfreeze_array(conf);
1947 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1949 bio = r1_bio->bios[r1_bio->read_disk];
1950 bdevname(bio->bi_bdev, b);
1952 disk = read_balance(conf, r1_bio, &max_sectors);
1954 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
1955 " read error for block %llu\n",
1956 mdname(mddev), b, (unsigned long long)r1_bio->sector);
1957 raid_end_bio_io(r1_bio);
1959 const unsigned long do_sync
1960 = r1_bio->master_bio->bi_rw & REQ_SYNC;
1962 r1_bio->bios[r1_bio->read_disk] =
1963 mddev->ro ? IO_BLOCKED : NULL;
1966 r1_bio->read_disk = disk;
1967 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
1968 md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
1969 r1_bio->bios[r1_bio->read_disk] = bio;
1970 rdev = conf->mirrors[disk].rdev;
1971 printk_ratelimited(KERN_ERR
1972 "md/raid1:%s: redirecting sector %llu"
1973 " to other mirror: %s\n",
1975 (unsigned long long)r1_bio->sector,
1976 bdevname(rdev->bdev, b));
1977 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1978 bio->bi_bdev = rdev->bdev;
1979 bio->bi_end_io = raid1_end_read_request;
1980 bio->bi_rw = READ | do_sync;
1981 bio->bi_private = r1_bio;
1982 if (max_sectors < r1_bio->sectors) {
1983 /* Drat - have to split this up more */
1984 struct bio *mbio = r1_bio->master_bio;
1985 int sectors_handled = (r1_bio->sector + max_sectors
1987 r1_bio->sectors = max_sectors;
1988 spin_lock_irq(&conf->device_lock);
1989 if (mbio->bi_phys_segments == 0)
1990 mbio->bi_phys_segments = 2;
1992 mbio->bi_phys_segments++;
1993 spin_unlock_irq(&conf->device_lock);
1994 generic_make_request(bio);
1997 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1999 r1_bio->master_bio = mbio;
2000 r1_bio->sectors = (mbio->bi_size >> 9)
2003 set_bit(R1BIO_ReadError, &r1_bio->state);
2004 r1_bio->mddev = mddev;
2005 r1_bio->sector = mbio->bi_sector + sectors_handled;
2009 generic_make_request(bio);
2013 static void raid1d(mddev_t *mddev)
2016 unsigned long flags;
2017 conf_t *conf = mddev->private;
2018 struct list_head *head = &conf->retry_list;
2019 struct blk_plug plug;
2021 md_check_recovery(mddev);
2023 blk_start_plug(&plug);
2026 if (atomic_read(&mddev->plug_cnt) == 0)
2027 flush_pending_writes(conf);
2029 spin_lock_irqsave(&conf->device_lock, flags);
2030 if (list_empty(head)) {
2031 spin_unlock_irqrestore(&conf->device_lock, flags);
2034 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
2035 list_del(head->prev);
2037 spin_unlock_irqrestore(&conf->device_lock, flags);
2039 mddev = r1_bio->mddev;
2040 conf = mddev->private;
2041 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2042 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2043 test_bit(R1BIO_WriteError, &r1_bio->state))
2044 handle_sync_write_finished(conf, r1_bio);
2046 sync_request_write(mddev, r1_bio);
2047 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2048 test_bit(R1BIO_WriteError, &r1_bio->state))
2049 handle_write_finished(conf, r1_bio);
2050 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2051 handle_read_error(conf, r1_bio);
2053 /* just a partial read to be scheduled from separate
2056 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
2059 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2060 md_check_recovery(mddev);
2062 blk_finish_plug(&plug);
2066 static int init_resync(conf_t *conf)
2070 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2071 BUG_ON(conf->r1buf_pool);
2072 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2074 if (!conf->r1buf_pool)
2076 conf->next_resync = 0;
2081 * perform a "sync" on one "block"
2083 * We need to make sure that no normal I/O request - particularly write
2084 * requests - conflict with active sync requests.
2086 * This is achieved by tracking pending requests and a 'barrier' concept
2087 * that can be installed to exclude normal IO requests.
2090 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2092 conf_t *conf = mddev->private;
2095 sector_t max_sector, nr_sectors;
2099 int write_targets = 0, read_targets = 0;
2100 sector_t sync_blocks;
2101 int still_degraded = 0;
2102 int good_sectors = RESYNC_SECTORS;
2103 int min_bad = 0; /* number of sectors that are bad in all devices */
2105 if (!conf->r1buf_pool)
2106 if (init_resync(conf))
2109 max_sector = mddev->dev_sectors;
2110 if (sector_nr >= max_sector) {
2111 /* If we aborted, we need to abort the
2112 * sync on the 'current' bitmap chunk (there will
2113 * only be one in raid1 resync.
2114 * We can find the current addess in mddev->curr_resync
2116 if (mddev->curr_resync < max_sector) /* aborted */
2117 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2119 else /* completed sync */
2122 bitmap_close_sync(mddev->bitmap);
2127 if (mddev->bitmap == NULL &&
2128 mddev->recovery_cp == MaxSector &&
2129 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2130 conf->fullsync == 0) {
2132 return max_sector - sector_nr;
2134 /* before building a request, check if we can skip these blocks..
2135 * This call the bitmap_start_sync doesn't actually record anything
2137 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2138 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2139 /* We can skip this block, and probably several more */
2144 * If there is non-resync activity waiting for a turn,
2145 * and resync is going fast enough,
2146 * then let it though before starting on this new sync request.
2148 if (!go_faster && conf->nr_waiting)
2149 msleep_interruptible(1000);
2151 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2152 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
2153 raise_barrier(conf);
2155 conf->next_resync = sector_nr;
2159 * If we get a correctably read error during resync or recovery,
2160 * we might want to read from a different device. So we
2161 * flag all drives that could conceivably be read from for READ,
2162 * and any others (which will be non-In_sync devices) for WRITE.
2163 * If a read fails, we try reading from something else for which READ
2167 r1_bio->mddev = mddev;
2168 r1_bio->sector = sector_nr;
2170 set_bit(R1BIO_IsSync, &r1_bio->state);
2172 for (i=0; i < conf->raid_disks; i++) {
2174 bio = r1_bio->bios[i];
2176 /* take from bio_init */
2177 bio->bi_next = NULL;
2178 bio->bi_flags &= ~(BIO_POOL_MASK-1);
2179 bio->bi_flags |= 1 << BIO_UPTODATE;
2180 bio->bi_comp_cpu = -1;
2184 bio->bi_phys_segments = 0;
2186 bio->bi_end_io = NULL;
2187 bio->bi_private = NULL;
2189 rdev = rcu_dereference(conf->mirrors[i].rdev);
2191 test_bit(Faulty, &rdev->flags)) {
2193 } else if (!test_bit(In_sync, &rdev->flags)) {
2195 bio->bi_end_io = end_sync_write;
2198 /* may need to read from here */
2199 sector_t first_bad = MaxSector;
2202 if (is_badblock(rdev, sector_nr, good_sectors,
2203 &first_bad, &bad_sectors)) {
2204 if (first_bad > sector_nr)
2205 good_sectors = first_bad - sector_nr;
2207 bad_sectors -= (sector_nr - first_bad);
2209 min_bad > bad_sectors)
2210 min_bad = bad_sectors;
2213 if (sector_nr < first_bad) {
2214 if (test_bit(WriteMostly, &rdev->flags)) {
2222 bio->bi_end_io = end_sync_read;
2226 if (bio->bi_end_io) {
2227 atomic_inc(&rdev->nr_pending);
2228 bio->bi_sector = sector_nr + rdev->data_offset;
2229 bio->bi_bdev = rdev->bdev;
2230 bio->bi_private = r1_bio;
2236 r1_bio->read_disk = disk;
2238 if (read_targets == 0 && min_bad > 0) {
2239 /* These sectors are bad on all InSync devices, so we
2240 * need to mark them bad on all write targets
2243 for (i = 0 ; i < conf->raid_disks ; i++)
2244 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2246 rcu_dereference(conf->mirrors[i].rdev);
2247 ok = rdev_set_badblocks(rdev, sector_nr,
2251 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2256 /* Cannot record the badblocks, so need to
2258 * If there are multiple read targets, could just
2259 * fail the really bad ones ???
2261 conf->recovery_disabled = mddev->recovery_disabled;
2262 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2268 if (min_bad > 0 && min_bad < good_sectors) {
2269 /* only resync enough to reach the next bad->good
2271 good_sectors = min_bad;
2274 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2275 /* extra read targets are also write targets */
2276 write_targets += read_targets-1;
2278 if (write_targets == 0 || read_targets == 0) {
2279 /* There is nowhere to write, so all non-sync
2280 * drives must be failed - so we are finished
2282 sector_t rv = max_sector - sector_nr;
2288 if (max_sector > mddev->resync_max)
2289 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2290 if (max_sector > sector_nr + good_sectors)
2291 max_sector = sector_nr + good_sectors;
2296 int len = PAGE_SIZE;
2297 if (sector_nr + (len>>9) > max_sector)
2298 len = (max_sector - sector_nr) << 9;
2301 if (sync_blocks == 0) {
2302 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2303 &sync_blocks, still_degraded) &&
2305 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2307 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
2308 if ((len >> 9) > sync_blocks)
2309 len = sync_blocks<<9;
2312 for (i=0 ; i < conf->raid_disks; i++) {
2313 bio = r1_bio->bios[i];
2314 if (bio->bi_end_io) {
2315 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2316 if (bio_add_page(bio, page, len, 0) == 0) {
2318 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2321 bio = r1_bio->bios[i];
2322 if (bio->bi_end_io==NULL)
2324 /* remove last page from this bio */
2326 bio->bi_size -= len;
2327 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2333 nr_sectors += len>>9;
2334 sector_nr += len>>9;
2335 sync_blocks -= (len>>9);
2336 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2338 r1_bio->sectors = nr_sectors;
2340 /* For a user-requested sync, we read all readable devices and do a
2343 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2344 atomic_set(&r1_bio->remaining, read_targets);
2345 for (i=0; i<conf->raid_disks; i++) {
2346 bio = r1_bio->bios[i];
2347 if (bio->bi_end_io == end_sync_read) {
2348 md_sync_acct(bio->bi_bdev, nr_sectors);
2349 generic_make_request(bio);
2353 atomic_set(&r1_bio->remaining, 1);
2354 bio = r1_bio->bios[r1_bio->read_disk];
2355 md_sync_acct(bio->bi_bdev, nr_sectors);
2356 generic_make_request(bio);
2362 static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2367 return mddev->dev_sectors;
2370 static conf_t *setup_conf(mddev_t *mddev)
2374 mirror_info_t *disk;
2378 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2382 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2387 conf->tmppage = alloc_page(GFP_KERNEL);
2391 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2392 if (!conf->poolinfo)
2394 conf->poolinfo->raid_disks = mddev->raid_disks;
2395 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2398 if (!conf->r1bio_pool)
2401 conf->poolinfo->mddev = mddev;
2403 spin_lock_init(&conf->device_lock);
2404 list_for_each_entry(rdev, &mddev->disks, same_set) {
2405 int disk_idx = rdev->raid_disk;
2406 if (disk_idx >= mddev->raid_disks
2409 disk = conf->mirrors + disk_idx;
2413 disk->head_position = 0;
2415 conf->raid_disks = mddev->raid_disks;
2416 conf->mddev = mddev;
2417 INIT_LIST_HEAD(&conf->retry_list);
2419 spin_lock_init(&conf->resync_lock);
2420 init_waitqueue_head(&conf->wait_barrier);
2422 bio_list_init(&conf->pending_bio_list);
2424 conf->last_used = -1;
2425 for (i = 0; i < conf->raid_disks; i++) {
2427 disk = conf->mirrors + i;
2430 !test_bit(In_sync, &disk->rdev->flags)) {
2431 disk->head_position = 0;
2434 } else if (conf->last_used < 0)
2436 * The first working device is used as a
2437 * starting point to read balancing.
2439 conf->last_used = i;
2443 if (conf->last_used < 0) {
2444 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
2449 conf->thread = md_register_thread(raid1d, mddev, NULL);
2450 if (!conf->thread) {
2452 "md/raid1:%s: couldn't allocate thread\n",
2461 if (conf->r1bio_pool)
2462 mempool_destroy(conf->r1bio_pool);
2463 kfree(conf->mirrors);
2464 safe_put_page(conf->tmppage);
2465 kfree(conf->poolinfo);
2468 return ERR_PTR(err);
2471 static int run(mddev_t *mddev)
2477 if (mddev->level != 1) {
2478 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
2479 mdname(mddev), mddev->level);
2482 if (mddev->reshape_position != MaxSector) {
2483 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
2488 * copy the already verified devices into our private RAID1
2489 * bookkeeping area. [whatever we allocate in run(),
2490 * should be freed in stop()]
2492 if (mddev->private == NULL)
2493 conf = setup_conf(mddev);
2495 conf = mddev->private;
2498 return PTR_ERR(conf);
2500 list_for_each_entry(rdev, &mddev->disks, same_set) {
2501 if (!mddev->gendisk)
2503 disk_stack_limits(mddev->gendisk, rdev->bdev,
2504 rdev->data_offset << 9);
2505 /* as we don't honour merge_bvec_fn, we must never risk
2506 * violating it, so limit ->max_segments to 1 lying within
2507 * a single page, as a one page request is never in violation.
2509 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2510 blk_queue_max_segments(mddev->queue, 1);
2511 blk_queue_segment_boundary(mddev->queue,
2512 PAGE_CACHE_SIZE - 1);
2516 mddev->degraded = 0;
2517 for (i=0; i < conf->raid_disks; i++)
2518 if (conf->mirrors[i].rdev == NULL ||
2519 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2520 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2523 if (conf->raid_disks - mddev->degraded == 1)
2524 mddev->recovery_cp = MaxSector;
2526 if (mddev->recovery_cp != MaxSector)
2527 printk(KERN_NOTICE "md/raid1:%s: not clean"
2528 " -- starting background reconstruction\n",
2531 "md/raid1:%s: active with %d out of %d mirrors\n",
2532 mdname(mddev), mddev->raid_disks - mddev->degraded,
2536 * Ok, everything is just fine now
2538 mddev->thread = conf->thread;
2539 conf->thread = NULL;
2540 mddev->private = conf;
2542 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
2545 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2546 mddev->queue->backing_dev_info.congested_data = mddev;
2548 return md_integrity_register(mddev);
2551 static int stop(mddev_t *mddev)
2553 conf_t *conf = mddev->private;
2554 struct bitmap *bitmap = mddev->bitmap;
2556 /* wait for behind writes to complete */
2557 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
2558 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2560 /* need to kick something here to make sure I/O goes? */
2561 wait_event(bitmap->behind_wait,
2562 atomic_read(&bitmap->behind_writes) == 0);
2565 raise_barrier(conf);
2566 lower_barrier(conf);
2568 md_unregister_thread(&mddev->thread);
2569 if (conf->r1bio_pool)
2570 mempool_destroy(conf->r1bio_pool);
2571 kfree(conf->mirrors);
2572 kfree(conf->poolinfo);
2574 mddev->private = NULL;
2578 static int raid1_resize(mddev_t *mddev, sector_t sectors)
2580 /* no resync is happening, and there is enough space
2581 * on all devices, so we can resize.
2582 * We need to make sure resync covers any new space.
2583 * If the array is shrinking we should possibly wait until
2584 * any io in the removed space completes, but it hardly seems
2587 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
2588 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2590 set_capacity(mddev->gendisk, mddev->array_sectors);
2591 revalidate_disk(mddev->gendisk);
2592 if (sectors > mddev->dev_sectors &&
2593 mddev->recovery_cp > mddev->dev_sectors) {
2594 mddev->recovery_cp = mddev->dev_sectors;
2595 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2597 mddev->dev_sectors = sectors;
2598 mddev->resync_max_sectors = sectors;
2602 static int raid1_reshape(mddev_t *mddev)
2605 * 1/ resize the r1bio_pool
2606 * 2/ resize conf->mirrors
2608 * We allocate a new r1bio_pool if we can.
2609 * Then raise a device barrier and wait until all IO stops.
2610 * Then resize conf->mirrors and swap in the new r1bio pool.
2612 * At the same time, we "pack" the devices so that all the missing
2613 * devices have the higher raid_disk numbers.
2615 mempool_t *newpool, *oldpool;
2616 struct pool_info *newpoolinfo;
2617 mirror_info_t *newmirrors;
2618 conf_t *conf = mddev->private;
2619 int cnt, raid_disks;
2620 unsigned long flags;
2623 /* Cannot change chunk_size, layout, or level */
2624 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
2625 mddev->layout != mddev->new_layout ||
2626 mddev->level != mddev->new_level) {
2627 mddev->new_chunk_sectors = mddev->chunk_sectors;
2628 mddev->new_layout = mddev->layout;
2629 mddev->new_level = mddev->level;
2633 err = md_allow_write(mddev);
2637 raid_disks = mddev->raid_disks + mddev->delta_disks;
2639 if (raid_disks < conf->raid_disks) {
2641 for (d= 0; d < conf->raid_disks; d++)
2642 if (conf->mirrors[d].rdev)
2644 if (cnt > raid_disks)
2648 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2651 newpoolinfo->mddev = mddev;
2652 newpoolinfo->raid_disks = raid_disks;
2654 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2655 r1bio_pool_free, newpoolinfo);
2660 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
2663 mempool_destroy(newpool);
2667 raise_barrier(conf);
2669 /* ok, everything is stopped */
2670 oldpool = conf->r1bio_pool;
2671 conf->r1bio_pool = newpool;
2673 for (d = d2 = 0; d < conf->raid_disks; d++) {
2674 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2675 if (rdev && rdev->raid_disk != d2) {
2676 sysfs_unlink_rdev(mddev, rdev);
2677 rdev->raid_disk = d2;
2678 sysfs_unlink_rdev(mddev, rdev);
2679 if (sysfs_link_rdev(mddev, rdev))
2681 "md/raid1:%s: cannot register rd%d\n",
2682 mdname(mddev), rdev->raid_disk);
2685 newmirrors[d2++].rdev = rdev;
2687 kfree(conf->mirrors);
2688 conf->mirrors = newmirrors;
2689 kfree(conf->poolinfo);
2690 conf->poolinfo = newpoolinfo;
2692 spin_lock_irqsave(&conf->device_lock, flags);
2693 mddev->degraded += (raid_disks - conf->raid_disks);
2694 spin_unlock_irqrestore(&conf->device_lock, flags);
2695 conf->raid_disks = mddev->raid_disks = raid_disks;
2696 mddev->delta_disks = 0;
2698 conf->last_used = 0; /* just make sure it is in-range */
2699 lower_barrier(conf);
2701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2702 md_wakeup_thread(mddev->thread);
2704 mempool_destroy(oldpool);
2708 static void raid1_quiesce(mddev_t *mddev, int state)
2710 conf_t *conf = mddev->private;
2713 case 2: /* wake for suspend */
2714 wake_up(&conf->wait_barrier);
2717 raise_barrier(conf);
2720 lower_barrier(conf);
2725 static void *raid1_takeover(mddev_t *mddev)
2727 /* raid1 can take over:
2728 * raid5 with 2 devices, any layout or chunk size
2730 if (mddev->level == 5 && mddev->raid_disks == 2) {
2732 mddev->new_level = 1;
2733 mddev->new_layout = 0;
2734 mddev->new_chunk_sectors = 0;
2735 conf = setup_conf(mddev);
2740 return ERR_PTR(-EINVAL);
2743 static struct mdk_personality raid1_personality =
2747 .owner = THIS_MODULE,
2748 .make_request = make_request,
2752 .error_handler = error,
2753 .hot_add_disk = raid1_add_disk,
2754 .hot_remove_disk= raid1_remove_disk,
2755 .spare_active = raid1_spare_active,
2756 .sync_request = sync_request,
2757 .resize = raid1_resize,
2759 .check_reshape = raid1_reshape,
2760 .quiesce = raid1_quiesce,
2761 .takeover = raid1_takeover,
2764 static int __init raid_init(void)
2766 return register_md_personality(&raid1_personality);
2769 static void raid_exit(void)
2771 unregister_md_personality(&raid1_personality);
2774 module_init(raid_init);
2775 module_exit(raid_exit);
2776 MODULE_LICENSE("GPL");
2777 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
2778 MODULE_ALIAS("md-personality-3"); /* RAID1 */
2779 MODULE_ALIAS("md-raid1");
2780 MODULE_ALIAS("md-level-1");